blob: 708bf0e7e4ac2781b27eacc97b0dfa0726da6f27 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
Jean Delvare95238362010-03-05 22:17:14 +01004 * Copyright (C) 2003-2010 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Based on the lm83 driver. The LM90 is a sensor chip made by National
7 * Semiconductor. It reports up to two temperatures (its own plus up to
8 * one external one) with a 0.125 deg resolution (1 deg for local
Jean Delvarea874a102008-10-17 17:51:10 +02009 * temperature) and a 3-4 deg accuracy.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This driver also supports the LM89 and LM99, two other sensor chips
12 * made by National Semiconductor. Both have an increased remote
13 * temperature measurement accuracy (1 degree), and the LM99
14 * additionally shifts remote temperatures (measured and limits) by 16
Jean Delvare97ae60b2008-10-26 17:04:39 +010015 * degrees, which allows for higher temperatures measurement.
Steven Cole44bbe872005-05-03 18:21:25 -060016 * Note that there is no way to differentiate between both chips.
Jean Delvare97ae60b2008-10-26 17:04:39 +010017 * When device is auto-detected, the driver will assume an LM99.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
19 * This driver also supports the LM86, another sensor chip made by
20 * National Semiconductor. It is exactly similar to the LM90 except it
21 * has a higher accuracy.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
23 * This driver also supports the ADM1032, a sensor chip made by Analog
24 * Devices. That chip is similar to the LM90, with a few differences
Jean Delvarea874a102008-10-17 17:51:10 +020025 * that are not handled by this driver. Among others, it has a higher
26 * accuracy than the LM90, much like the LM86 does.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
Jean Delvarea874a102008-10-17 17:51:10 +020029 * chips made by Maxim. These chips are similar to the LM86.
Steven Cole44bbe872005-05-03 18:21:25 -060030 * Note that there is no easy way to differentiate between the three
Guenter Roeck69487082010-10-28 20:31:43 +020031 * variants. We use the device address to detect MAX6659, which will result
32 * in a detection as max6657 if it is on address 0x4c. The extra address
33 * and features of the MAX6659 are only supported if the chip is configured
34 * explicitly as max6659, or if its address is not 0x4c.
35 * These chips lack the remote temperature offset feature.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 *
Darrick J. Wong1a51e062009-03-12 13:36:38 +010037 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
38 * MAX6692 chips made by Maxim. These are again similar to the LM86,
39 * but they use unsigned temperature values and can report temperatures
40 * from 0 to 145 degrees.
Ben Hutchings271dabf2008-10-17 17:51:11 +020041 *
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040042 * This driver also supports the MAX6680 and MAX6681, two other sensor
43 * chips made by Maxim. These are quite similar to the other Maxim
Jean Delvarea874a102008-10-17 17:51:10 +020044 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
45 * be treated identically.
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040046 *
Guenter Roeck06e1c0a2010-10-28 20:31:43 +020047 * This driver also supports the MAX6695 and MAX6696, two other sensor
48 * chips made by Maxim. These are also quite similar to other Maxim
49 * chips, but support three temperature sensors instead of two. MAX6695
50 * and MAX6696 only differ in the pinout so they can be treated identically.
51 *
Guenter Roeck5a4e5e62011-04-29 16:33:35 +020052 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
53 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
54 * and extended mode. They are mostly compatible with LM90 except for a data
55 * format difference for the temperature value registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 *
Stijn Devriendt2ef01792011-06-06 10:40:45 +000057 * This driver also supports the SA56004 from Philips. This device is
58 * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
59 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * Since the LM90 was the first chipset supported by this driver, most
61 * comments will refer to this chipset, but are actually general and
62 * concern all supported chipsets, unless mentioned otherwise.
63 *
64 * This program is free software; you can redistribute it and/or modify
65 * it under the terms of the GNU General Public License as published by
66 * the Free Software Foundation; either version 2 of the License, or
67 * (at your option) any later version.
68 *
69 * This program is distributed in the hope that it will be useful,
70 * but WITHOUT ANY WARRANTY; without even the implied warranty of
71 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72 * GNU General Public License for more details.
73 *
74 * You should have received a copy of the GNU General Public License
75 * along with this program; if not, write to the Free Software
76 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
77 */
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/module.h>
80#include <linux/init.h>
81#include <linux/slab.h>
82#include <linux/jiffies.h>
83#include <linux/i2c.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020084#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040085#include <linux/hwmon.h>
86#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010087#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020088#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
91 * Addresses to scan
92 * Address is fully defined internally and cannot be changed except for
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040093 * MAX6659, MAX6680 and MAX6681.
Guenter Roeck5a4e5e62011-04-29 16:33:35 +020094 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
95 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
96 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
97 * have address 0x4d.
Ben Hutchings271dabf2008-10-17 17:51:11 +020098 * MAX6647 has address 0x4e.
Guenter Roeck13c84952010-10-28 20:31:43 +020099 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400100 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
101 * 0x4c, 0x4d or 0x4e.
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000102 * SA56004 can have address 0x48 through 0x4F.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
104
Mark M. Hoffman25e9c862008-02-17 22:28:03 -0500105static const unsigned short normal_i2c[] = {
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000106 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
107 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Guenter Roeck13c84952010-10-28 20:31:43 +0200109enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000110 max6646, w83l771, max6696, sa56004 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/*
113 * The LM90 registers
114 */
115
116#define LM90_REG_R_MAN_ID 0xFE
117#define LM90_REG_R_CHIP_ID 0xFF
118#define LM90_REG_R_CONFIG1 0x03
119#define LM90_REG_W_CONFIG1 0x09
120#define LM90_REG_R_CONFIG2 0xBF
121#define LM90_REG_W_CONFIG2 0xBF
122#define LM90_REG_R_CONVRATE 0x04
123#define LM90_REG_W_CONVRATE 0x0A
124#define LM90_REG_R_STATUS 0x02
125#define LM90_REG_R_LOCAL_TEMP 0x00
126#define LM90_REG_R_LOCAL_HIGH 0x05
127#define LM90_REG_W_LOCAL_HIGH 0x0B
128#define LM90_REG_R_LOCAL_LOW 0x06
129#define LM90_REG_W_LOCAL_LOW 0x0C
130#define LM90_REG_R_LOCAL_CRIT 0x20
131#define LM90_REG_W_LOCAL_CRIT 0x20
132#define LM90_REG_R_REMOTE_TEMPH 0x01
133#define LM90_REG_R_REMOTE_TEMPL 0x10
134#define LM90_REG_R_REMOTE_OFFSH 0x11
135#define LM90_REG_W_REMOTE_OFFSH 0x11
136#define LM90_REG_R_REMOTE_OFFSL 0x12
137#define LM90_REG_W_REMOTE_OFFSL 0x12
138#define LM90_REG_R_REMOTE_HIGHH 0x07
139#define LM90_REG_W_REMOTE_HIGHH 0x0D
140#define LM90_REG_R_REMOTE_HIGHL 0x13
141#define LM90_REG_W_REMOTE_HIGHL 0x13
142#define LM90_REG_R_REMOTE_LOWH 0x08
143#define LM90_REG_W_REMOTE_LOWH 0x0E
144#define LM90_REG_R_REMOTE_LOWL 0x14
145#define LM90_REG_W_REMOTE_LOWL 0x14
146#define LM90_REG_R_REMOTE_CRIT 0x19
147#define LM90_REG_W_REMOTE_CRIT 0x19
148#define LM90_REG_R_TCRIT_HYST 0x21
149#define LM90_REG_W_TCRIT_HYST 0x21
150
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200151/* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
Jean Delvaref65e1702008-10-17 17:51:09 +0200152
153#define MAX6657_REG_R_LOCAL_TEMPL 0x11
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200154#define MAX6696_REG_R_STATUS2 0x12
Guenter Roeck69487082010-10-28 20:31:43 +0200155#define MAX6659_REG_R_REMOTE_EMERG 0x16
156#define MAX6659_REG_W_REMOTE_EMERG 0x16
157#define MAX6659_REG_R_LOCAL_EMERG 0x17
158#define MAX6659_REG_W_LOCAL_EMERG 0x17
Jean Delvaref65e1702008-10-17 17:51:09 +0200159
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000160/* SA56004 registers */
161
162#define SA56004_REG_R_LOCAL_TEMPL 0x22
163
Guenter Roeck0c01b642010-10-28 20:31:44 +0200164#define LM90_DEF_CONVRATE_RVAL 6 /* Def conversion rate register value */
165#define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
Nate Case23b2d472008-10-17 17:51:10 +0200168 * Device flags
169 */
Guenter Roeck88073bb2010-10-28 20:31:43 +0200170#define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
171/* Device features */
172#define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
173#define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */
174#define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
Guenter Roeck69487082010-10-28 20:31:43 +0200175#define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200176#define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
177#define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
Guenter Roeck11793242010-10-28 20:31:44 +0200178#define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
Nate Case23b2d472008-10-17 17:51:10 +0200179
180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * Driver data (common to all clients)
182 */
183
Jean Delvare9b0e8522008-07-16 19:30:15 +0200184static const struct i2c_device_id lm90_id[] = {
185 { "adm1032", adm1032 },
186 { "adt7461", adt7461 },
Guenter Roeck5a4e5e62011-04-29 16:33:35 +0200187 { "adt7461a", adt7461 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200188 { "lm90", lm90 },
189 { "lm86", lm86 },
Jean Delvare97ae60b2008-10-26 17:04:39 +0100190 { "lm89", lm86 },
191 { "lm99", lm99 },
Ben Hutchings271dabf2008-10-17 17:51:11 +0200192 { "max6646", max6646 },
193 { "max6647", max6646 },
194 { "max6649", max6646 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200195 { "max6657", max6657 },
196 { "max6658", max6657 },
Guenter Roeck13c84952010-10-28 20:31:43 +0200197 { "max6659", max6659 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200198 { "max6680", max6680 },
199 { "max6681", max6680 },
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200200 { "max6695", max6696 },
201 { "max6696", max6696 },
Guenter Roeck5a4e5e62011-04-29 16:33:35 +0200202 { "nct1008", adt7461 },
Jean Delvare6771ea12010-03-05 22:17:13 +0100203 { "w83l771", w83l771 },
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000204 { "sa56004", sa56004 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200205 { }
206};
207MODULE_DEVICE_TABLE(i2c, lm90_id);
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200210 * chip type specific parameters
211 */
212struct lm90_params {
213 u32 flags; /* Capabilities */
214 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
215 /* Upper 8 bits for max6695/96 */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200216 u8 max_convrate; /* Maximum conversion rate register value */
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000217 u8 reg_local_ext; /* Local extension register if
218 LM90_HAVE_LOCAL_EXT is set*/
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200219};
220
221static const struct lm90_params lm90_params[] = {
222 [adm1032] = {
Guenter Roeck11793242010-10-28 20:31:44 +0200223 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
224 | LM90_HAVE_BROKEN_ALERT,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200225 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200226 .max_convrate = 10,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200227 },
228 [adt7461] = {
Guenter Roeck11793242010-10-28 20:31:44 +0200229 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
230 | LM90_HAVE_BROKEN_ALERT,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200231 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200232 .max_convrate = 10,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200233 },
234 [lm86] = {
235 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
236 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200237 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200238 },
239 [lm90] = {
240 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
241 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200242 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200243 },
244 [lm99] = {
245 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
246 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200247 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200248 },
249 [max6646] = {
250 .flags = LM90_HAVE_LOCAL_EXT,
251 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200252 .max_convrate = 6,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000253 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200254 },
255 [max6657] = {
256 .flags = LM90_HAVE_LOCAL_EXT,
257 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200258 .max_convrate = 8,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000259 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200260 },
261 [max6659] = {
262 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY,
263 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200264 .max_convrate = 8,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000265 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200266 },
267 [max6680] = {
268 .flags = LM90_HAVE_OFFSET,
269 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200270 .max_convrate = 7,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200271 },
272 [max6696] = {
273 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY
274 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
275 .alert_alarms = 0x187c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200276 .max_convrate = 6,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000277 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200278 },
279 [w83l771] = {
280 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
281 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200282 .max_convrate = 8,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200283 },
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000284 [sa56004] = {
285 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
286 | LM90_HAVE_LOCAL_EXT,
287 .alert_alarms = 0x7b,
288 .max_convrate = 9,
289 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
290 },
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200291};
292
293/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * Client data (each client gets its own)
295 */
296
297struct lm90_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700298 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100299 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 char valid; /* zero until following fields are valid */
301 unsigned long last_updated; /* in jiffies */
302 int kind;
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200303 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Guenter Roeck0c01b642010-10-28 20:31:44 +0200305 int update_interval; /* in milliseconds */
306
Jean Delvare95238362010-03-05 22:17:14 +0100307 u8 config_orig; /* Original configuration register value */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200308 u8 convrate_orig; /* Original conversion rate register value */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200309 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
310 /* Upper 8 bits for max6695/96 */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200311 u8 max_convrate; /* Maximum conversion rate */
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000312 u8 reg_local_ext; /* local extension register offset */
Jean Delvare95238362010-03-05 22:17:14 +0100313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* registers values */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200315 s8 temp8[8]; /* 0: local low limit
Jean Delvaref65e1702008-10-17 17:51:09 +0200316 1: local high limit
317 2: local critical limit
Guenter Roeck69487082010-10-28 20:31:43 +0200318 3: remote critical limit
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200319 4: local emergency limit (max6659 and max6695/96)
320 5: remote emergency limit (max6659 and max6695/96)
321 6: remote 2 critical limit (max6695/96 only)
322 7: remote 2 emergency limit (max6695/96 only) */
323 s16 temp11[8]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200324 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200325 2: remote high limit
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200326 3: remote offset (except max6646, max6657/58/59,
327 and max6695/96)
328 4: local input
329 5: remote 2 input (max6695/96 only)
330 6: remote 2 low limit (max6695/96 only)
331 7: remote 2 high limit (ma6695/96 only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 u8 temp_hyst;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200333 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
335
336/*
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200337 * Support functions
338 */
339
340/*
341 * The ADM1032 supports PEC but not on write byte transactions, so we need
342 * to explicitly ask for a transaction without PEC.
343 */
344static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
345{
346 return i2c_smbus_xfer(client->adapter, client->addr,
347 client->flags & ~I2C_CLIENT_PEC,
348 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
349}
350
351/*
352 * It is assumed that client->update_lock is held (unless we are in
353 * detection or initialization steps). This matters when PEC is enabled,
354 * because we don't want the address pointer to change between the write
355 * byte and the read byte transactions.
356 */
357static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
358{
359 int err;
360
361 if (client->flags & I2C_CLIENT_PEC) {
362 err = adm1032_write_byte(client, reg);
363 if (err >= 0)
364 err = i2c_smbus_read_byte(client);
365 } else
366 err = i2c_smbus_read_byte_data(client, reg);
367
368 if (err < 0) {
369 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
370 reg, err);
371 return err;
372 }
373 *value = err;
374
375 return 0;
376}
377
378static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
379{
380 int err;
381 u8 oldh, newh, l;
382
383 /*
384 * There is a trick here. We have to read two registers to have the
385 * sensor temperature, but we have to beware a conversion could occur
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300386 * between the readings. The datasheet says we should either use
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200387 * the one-shot conversion register, which we don't want to do
388 * (disables hardware monitoring) or monitor the busy bit, which is
389 * impossible (we can't read the values and monitor that bit at the
390 * exact same time). So the solution used here is to read the high
391 * byte once, then the low byte, then the high byte again. If the new
392 * high byte matches the old one, then we have a valid reading. Else
393 * we have to read the low byte again, and now we believe we have a
394 * correct reading.
395 */
396 if ((err = lm90_read_reg(client, regh, &oldh))
397 || (err = lm90_read_reg(client, regl, &l))
398 || (err = lm90_read_reg(client, regh, &newh)))
399 return err;
400 if (oldh != newh) {
401 err = lm90_read_reg(client, regl, &l);
402 if (err)
403 return err;
404 }
405 *value = (newh << 8) | l;
406
407 return 0;
408}
409
410/*
411 * client->update_lock must be held when calling this function (unless we are
412 * in detection or initialization steps), and while a remote channel other
413 * than channel 0 is selected. Also, calling code must make sure to re-select
414 * external channel 0 before releasing the lock. This is necessary because
415 * various registers have different meanings as a result of selecting a
416 * non-default remote channel.
417 */
418static inline void lm90_select_remote_channel(struct i2c_client *client,
419 struct lm90_data *data,
420 int channel)
421{
422 u8 config;
423
424 if (data->kind == max6696) {
425 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
426 config &= ~0x08;
427 if (channel)
428 config |= 0x08;
429 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
430 config);
431 }
432}
433
Guenter Roeck0c01b642010-10-28 20:31:44 +0200434/*
435 * Set conversion rate.
436 * client->update_lock must be held when calling this function (unless we are
437 * in detection or initialization steps).
438 */
439static void lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
440 unsigned int interval)
441{
442 int i;
443 unsigned int update_interval;
444
445 /* Shift calculations to avoid rounding errors */
446 interval <<= 6;
447
448 /* find the nearest update rate */
449 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
450 i < data->max_convrate; i++, update_interval >>= 1)
451 if (interval >= update_interval * 3 / 4)
452 break;
453
454 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, i);
455 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
456}
457
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200458static struct lm90_data *lm90_update_device(struct device *dev)
459{
460 struct i2c_client *client = to_i2c_client(dev);
461 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck0c01b642010-10-28 20:31:44 +0200462 unsigned long next_update;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200463
464 mutex_lock(&data->update_lock);
465
Guenter Roeck0c01b642010-10-28 20:31:44 +0200466 next_update = data->last_updated
467 + msecs_to_jiffies(data->update_interval) + 1;
468 if (time_after(jiffies, next_update) || !data->valid) {
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200469 u8 h, l;
470 u8 alarms;
471
472 dev_dbg(&client->dev, "Updating lm90 data.\n");
473 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
474 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
475 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
476 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
477 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
478
479 if (data->flags & LM90_HAVE_LOCAL_EXT) {
480 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000481 data->reg_local_ext,
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200482 &data->temp11[4]);
483 } else {
484 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
485 &h) == 0)
486 data->temp11[4] = h << 8;
487 }
488 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
489 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
490
491 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
492 data->temp11[1] = h << 8;
493 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
494 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
495 &l) == 0)
496 data->temp11[1] |= l;
497 }
498 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
499 data->temp11[2] = h << 8;
500 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
501 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
502 &l) == 0)
503 data->temp11[2] |= l;
504 }
505
506 if (data->flags & LM90_HAVE_OFFSET) {
507 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
508 &h) == 0
509 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
510 &l) == 0)
511 data->temp11[3] = (h << 8) | l;
512 }
513 if (data->flags & LM90_HAVE_EMERGENCY) {
514 lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
515 &data->temp8[4]);
516 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
517 &data->temp8[5]);
518 }
519 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
520 data->alarms = alarms; /* save as 16 bit value */
521
522 if (data->kind == max6696) {
523 lm90_select_remote_channel(client, data, 1);
524 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
525 &data->temp8[6]);
526 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
527 &data->temp8[7]);
528 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
529 LM90_REG_R_REMOTE_TEMPL, &data->temp11[5]);
530 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h))
531 data->temp11[6] = h << 8;
532 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h))
533 data->temp11[7] = h << 8;
534 lm90_select_remote_channel(client, data, 0);
535
536 if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2,
537 &alarms))
538 data->alarms |= alarms << 8;
539 }
540
541 /* Re-enable ALERT# output if it was originally enabled and
542 * relevant alarms are all clear */
543 if ((data->config_orig & 0x80) == 0
544 && (data->alarms & data->alert_alarms) == 0) {
545 u8 config;
546
547 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
548 if (config & 0x80) {
549 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
550 i2c_smbus_write_byte_data(client,
551 LM90_REG_W_CONFIG1,
552 config & ~0x80);
553 }
554 }
555
556 data->last_updated = jiffies;
557 data->valid = 1;
558 }
559
560 mutex_unlock(&data->update_lock);
561
562 return data;
563}
564
565/*
Nate Casecea50fe2008-10-17 17:51:10 +0200566 * Conversions
567 * For local temperatures and limits, critical limits and the hysteresis
568 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
569 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200570 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
571 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200572 */
573
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200574static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200575{
576 return val * 1000;
577}
578
Ben Hutchings271dabf2008-10-17 17:51:11 +0200579static inline int temp_from_u8(u8 val)
580{
581 return val * 1000;
582}
583
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200584static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200585{
586 return val / 32 * 125;
587}
588
Ben Hutchings271dabf2008-10-17 17:51:11 +0200589static inline int temp_from_u16(u16 val)
590{
591 return val / 32 * 125;
592}
593
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200594static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200595{
596 if (val <= -128000)
597 return -128;
598 if (val >= 127000)
599 return 127;
600 if (val < 0)
601 return (val - 500) / 1000;
602 return (val + 500) / 1000;
603}
604
Ben Hutchings271dabf2008-10-17 17:51:11 +0200605static u8 temp_to_u8(long val)
606{
607 if (val <= 0)
608 return 0;
609 if (val >= 255000)
610 return 255;
611 return (val + 500) / 1000;
612}
613
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200614static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200615{
616 if (val <= -128000)
617 return 0x8000;
618 if (val >= 127875)
619 return 0x7FE0;
620 if (val < 0)
621 return (val - 62) / 125 * 32;
622 return (val + 62) / 125 * 32;
623}
624
625static u8 hyst_to_reg(long val)
626{
627 if (val <= 0)
628 return 0;
629 if (val >= 30500)
630 return 31;
631 return (val + 500) / 1000;
632}
633
634/*
Nate Case23b2d472008-10-17 17:51:10 +0200635 * ADT7461 in compatibility mode is almost identical to LM90 except that
636 * attempts to write values that are outside the range 0 < temp < 127 are
637 * treated as the boundary value.
638 *
639 * ADT7461 in "extended mode" operation uses unsigned integers offset by
640 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200641 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200642static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200643{
Nate Case23b2d472008-10-17 17:51:10 +0200644 if (data->flags & LM90_FLAG_ADT7461_EXT)
645 return (val - 64) * 1000;
646 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200647 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200648}
649
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200650static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200651{
Nate Case23b2d472008-10-17 17:51:10 +0200652 if (data->flags & LM90_FLAG_ADT7461_EXT)
653 return (val - 0x4000) / 64 * 250;
654 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200655 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200656}
657
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200658static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200659{
660 if (data->flags & LM90_FLAG_ADT7461_EXT) {
661 if (val <= -64000)
662 return 0;
663 if (val >= 191000)
664 return 0xFF;
665 return (val + 500 + 64000) / 1000;
666 } else {
667 if (val <= 0)
668 return 0;
669 if (val >= 127000)
670 return 127;
671 return (val + 500) / 1000;
672 }
673}
674
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200675static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200676{
677 if (data->flags & LM90_FLAG_ADT7461_EXT) {
678 if (val <= -64000)
679 return 0;
680 if (val >= 191750)
681 return 0xFFC0;
682 return (val + 64000 + 125) / 250 * 64;
683 } else {
684 if (val <= 0)
685 return 0;
686 if (val >= 127750)
687 return 0x7FC0;
688 return (val + 125) / 250 * 64;
689 }
Nate Casecea50fe2008-10-17 17:51:10 +0200690}
691
692/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 * Sysfs stuff
694 */
695
Jean Delvare30d73942005-06-05 21:27:28 +0200696static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
697 char *buf)
698{
699 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
700 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200701 int temp;
702
703 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200704 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200705 else if (data->kind == max6646)
706 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200707 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200708 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200709
Jean Delvare97ae60b2008-10-26 17:04:39 +0100710 /* +16 degrees offset for temp2 for the LM99 */
711 if (data->kind == lm99 && attr->index == 3)
712 temp += 16000;
713
Nate Case23b2d472008-10-17 17:51:10 +0200714 return sprintf(buf, "%d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Jean Delvare30d73942005-06-05 21:27:28 +0200717static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
718 const char *buf, size_t count)
719{
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200720 static const u8 reg[8] = {
Jean Delvare30d73942005-06-05 21:27:28 +0200721 LM90_REG_W_LOCAL_LOW,
722 LM90_REG_W_LOCAL_HIGH,
723 LM90_REG_W_LOCAL_CRIT,
724 LM90_REG_W_REMOTE_CRIT,
Guenter Roeck69487082010-10-28 20:31:43 +0200725 MAX6659_REG_W_LOCAL_EMERG,
726 MAX6659_REG_W_REMOTE_EMERG,
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200727 LM90_REG_W_REMOTE_CRIT,
728 MAX6659_REG_W_REMOTE_EMERG,
Jean Delvare30d73942005-06-05 21:27:28 +0200729 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Jean Delvare30d73942005-06-05 21:27:28 +0200731 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
732 struct i2c_client *client = to_i2c_client(dev);
733 struct lm90_data *data = i2c_get_clientdata(client);
Jean Delvare30d73942005-06-05 21:27:28 +0200734 int nr = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200735 long val;
736 int err;
737
738 err = strict_strtol(buf, 10, &val);
739 if (err < 0)
740 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jean Delvare97ae60b2008-10-26 17:04:39 +0100742 /* +16 degrees offset for temp2 for the LM99 */
743 if (data->kind == lm99 && attr->index == 3)
744 val -= 16000;
745
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100746 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200747 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200748 data->temp8[nr] = temp_to_u8_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200749 else if (data->kind == max6646)
750 data->temp8[nr] = temp_to_u8(val);
Jean Delvare30d73942005-06-05 21:27:28 +0200751 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200752 data->temp8[nr] = temp_to_s8(val);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200753
754 lm90_select_remote_channel(client, data, nr >= 6);
Jean Delvaref65e1702008-10-17 17:51:09 +0200755 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200756 lm90_select_remote_channel(client, data, 0);
757
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100758 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200759 return count;
760}
761
762static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
763 char *buf)
764{
Guenter Roeck96512862010-10-28 20:31:43 +0200765 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200766 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200767 int temp;
768
769 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200770 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200771 else if (data->kind == max6646)
772 temp = temp_from_u16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200773 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200774 temp = temp_from_s16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200775
Jean Delvare97ae60b2008-10-26 17:04:39 +0100776 /* +16 degrees offset for temp2 for the LM99 */
777 if (data->kind == lm99 && attr->index <= 2)
778 temp += 16000;
779
Nate Case23b2d472008-10-17 17:51:10 +0200780 return sprintf(buf, "%d\n", temp);
Jean Delvare30d73942005-06-05 21:27:28 +0200781}
782
783static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
784 const char *buf, size_t count)
785{
Guenter Roeck96512862010-10-28 20:31:43 +0200786 struct {
787 u8 high;
788 u8 low;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200789 int channel;
790 } reg[5] = {
791 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 },
792 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 },
793 { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 },
794 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 },
795 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
Jean Delvare30d73942005-06-05 21:27:28 +0200796 };
797
Guenter Roeck96512862010-10-28 20:31:43 +0200798 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200799 struct i2c_client *client = to_i2c_client(dev);
800 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck96512862010-10-28 20:31:43 +0200801 int nr = attr->nr;
802 int index = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200803 long val;
804 int err;
805
806 err = strict_strtol(buf, 10, &val);
807 if (err < 0)
808 return err;
Jean Delvare30d73942005-06-05 21:27:28 +0200809
Jean Delvare97ae60b2008-10-26 17:04:39 +0100810 /* +16 degrees offset for temp2 for the LM99 */
Guenter Roeck96512862010-10-28 20:31:43 +0200811 if (data->kind == lm99 && index <= 2)
Jean Delvare97ae60b2008-10-26 17:04:39 +0100812 val -= 16000;
813
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100814 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200815 if (data->kind == adt7461)
Guenter Roeck96512862010-10-28 20:31:43 +0200816 data->temp11[index] = temp_to_u16_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200817 else if (data->kind == max6646)
Guenter Roeck96512862010-10-28 20:31:43 +0200818 data->temp11[index] = temp_to_u8(val) << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +0200819 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200820 data->temp11[index] = temp_to_s16(val);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200821 else
Guenter Roeck96512862010-10-28 20:31:43 +0200822 data->temp11[index] = temp_to_s8(val) << 8;
Jean Delvare5f502a82008-10-17 17:51:09 +0200823
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200824 lm90_select_remote_channel(client, data, reg[nr].channel);
Guenter Roeck96512862010-10-28 20:31:43 +0200825 i2c_smbus_write_byte_data(client, reg[nr].high,
826 data->temp11[index] >> 8);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200827 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200828 i2c_smbus_write_byte_data(client, reg[nr].low,
829 data->temp11[index] & 0xff);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200830 lm90_select_remote_channel(client, data, 0);
831
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100832 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200833 return count;
834}
835
Guenter Roeck11e57812010-10-28 20:31:42 +0200836static ssize_t show_temphyst(struct device *dev,
837 struct device_attribute *devattr,
Jean Delvare30d73942005-06-05 21:27:28 +0200838 char *buf)
839{
840 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
841 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200842 int temp;
843
844 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200845 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Jean Delvareec38fa22008-10-26 17:04:39 +0100846 else if (data->kind == max6646)
847 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200848 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200849 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200850
Jean Delvare97ae60b2008-10-26 17:04:39 +0100851 /* +16 degrees offset for temp2 for the LM99 */
852 if (data->kind == lm99 && attr->index == 3)
853 temp += 16000;
854
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200855 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
Jean Delvare30d73942005-06-05 21:27:28 +0200856}
857
858static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
859 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 struct i2c_client *client = to_i2c_client(dev);
862 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck11e57812010-10-28 20:31:42 +0200863 long val;
864 int err;
Jean Delvareec38fa22008-10-26 17:04:39 +0100865 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Guenter Roeck11e57812010-10-28 20:31:42 +0200867 err = strict_strtol(buf, 10, &val);
868 if (err < 0)
869 return err;
870
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100871 mutex_lock(&data->update_lock);
Jean Delvareec38fa22008-10-26 17:04:39 +0100872 if (data->kind == adt7461)
873 temp = temp_from_u8_adt7461(data, data->temp8[2]);
874 else if (data->kind == max6646)
875 temp = temp_from_u8(data->temp8[2]);
876 else
877 temp = temp_from_s8(data->temp8[2]);
878
879 data->temp_hyst = hyst_to_reg(temp - val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
Jean Delvareec38fa22008-10-26 17:04:39 +0100881 data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100882 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return count;
884}
885
Jean Delvare30d73942005-06-05 21:27:28 +0200886static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
887 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 struct lm90_data *data = lm90_update_device(dev);
890 return sprintf(buf, "%d\n", data->alarms);
891}
892
Jean Delvare2d457712006-09-24 20:52:15 +0200893static ssize_t show_alarm(struct device *dev, struct device_attribute
894 *devattr, char *buf)
895{
896 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
897 struct lm90_data *data = lm90_update_device(dev);
898 int bitnr = attr->index;
899
900 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
901}
902
Guenter Roeck0c01b642010-10-28 20:31:44 +0200903static ssize_t show_update_interval(struct device *dev,
904 struct device_attribute *attr, char *buf)
905{
906 struct lm90_data *data = dev_get_drvdata(dev);
907
908 return sprintf(buf, "%u\n", data->update_interval);
909}
910
911static ssize_t set_update_interval(struct device *dev,
912 struct device_attribute *attr,
913 const char *buf, size_t count)
914{
915 struct i2c_client *client = to_i2c_client(dev);
916 struct lm90_data *data = i2c_get_clientdata(client);
917 unsigned long val;
918 int err;
919
920 err = strict_strtoul(buf, 10, &val);
921 if (err)
922 return err;
923
924 mutex_lock(&data->update_lock);
925 lm90_set_convrate(client, data, val);
926 mutex_unlock(&data->update_lock);
927
928 return count;
929}
930
Guenter Roeck96512862010-10-28 20:31:43 +0200931static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
932static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200933static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200934 set_temp8, 0);
Guenter Roeck96512862010-10-28 20:31:43 +0200935static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
936 set_temp11, 0, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200937static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200938 set_temp8, 1);
Guenter Roeck96512862010-10-28 20:31:43 +0200939static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
940 set_temp11, 1, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200941static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200942 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200943static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200944 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200945static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200946 set_temphyst, 2);
947static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Guenter Roeck96512862010-10-28 20:31:43 +0200948static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
949 set_temp11, 2, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200950
951/* Individual alarm files */
952static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
953static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400954static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200955static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
956static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
957static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
958static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
959/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
961
Guenter Roeck0c01b642010-10-28 20:31:44 +0200962static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
963 set_update_interval);
964
Jean Delvare0e39e012006-09-24 21:16:40 +0200965static struct attribute *lm90_attributes[] = {
966 &sensor_dev_attr_temp1_input.dev_attr.attr,
967 &sensor_dev_attr_temp2_input.dev_attr.attr,
968 &sensor_dev_attr_temp1_min.dev_attr.attr,
969 &sensor_dev_attr_temp2_min.dev_attr.attr,
970 &sensor_dev_attr_temp1_max.dev_attr.attr,
971 &sensor_dev_attr_temp2_max.dev_attr.attr,
972 &sensor_dev_attr_temp1_crit.dev_attr.attr,
973 &sensor_dev_attr_temp2_crit.dev_attr.attr,
974 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
975 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
976
977 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
978 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400979 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200980 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
981 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
982 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
983 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
984 &dev_attr_alarms.attr,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200985 &dev_attr_update_interval.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200986 NULL
987};
988
989static const struct attribute_group lm90_group = {
990 .attrs = lm90_attributes,
991};
992
Guenter Roeck69487082010-10-28 20:31:43 +0200993/*
994 * Additional attributes for devices with emergency sensors
995 */
996static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
997 set_temp8, 4);
998static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
999 set_temp8, 5);
1000static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
1001 NULL, 4);
1002static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
1003 NULL, 5);
1004
1005static struct attribute *lm90_emergency_attributes[] = {
1006 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
1007 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
1008 &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
1009 &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
1010 NULL
1011};
1012
1013static const struct attribute_group lm90_emergency_group = {
1014 .attrs = lm90_emergency_attributes,
1015};
1016
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001017static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15);
1018static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13);
1019
1020static struct attribute *lm90_emergency_alarm_attributes[] = {
1021 &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
1022 &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
1023 NULL
1024};
1025
1026static const struct attribute_group lm90_emergency_alarm_group = {
1027 .attrs = lm90_emergency_alarm_attributes,
1028};
1029
1030/*
1031 * Additional attributes for devices with 3 temperature sensors
1032 */
1033static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL, 0, 5);
1034static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11,
1035 set_temp11, 3, 6);
1036static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11,
1037 set_temp11, 4, 7);
1038static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8,
1039 set_temp8, 6);
1040static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL, 6);
1041static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8,
1042 set_temp8, 7);
1043static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst,
1044 NULL, 7);
1045
1046static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
1047static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10);
1048static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
1049static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12);
1050static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14);
1051
1052static struct attribute *lm90_temp3_attributes[] = {
1053 &sensor_dev_attr_temp3_input.dev_attr.attr,
1054 &sensor_dev_attr_temp3_min.dev_attr.attr,
1055 &sensor_dev_attr_temp3_max.dev_attr.attr,
1056 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1057 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1058 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
1059 &sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr,
1060
1061 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1062 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
1063 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
1064 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
1065 &sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr,
1066 NULL
1067};
1068
1069static const struct attribute_group lm90_temp3_group = {
1070 .attrs = lm90_temp3_attributes,
1071};
1072
Jean Delvarec3df5802005-10-26 21:39:40 +02001073/* pec used for ADM1032 only */
1074static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
1075 char *buf)
1076{
1077 struct i2c_client *client = to_i2c_client(dev);
1078 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
1079}
1080
1081static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
1082 const char *buf, size_t count)
1083{
1084 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck11e57812010-10-28 20:31:42 +02001085 long val;
1086 int err;
1087
1088 err = strict_strtol(buf, 10, &val);
1089 if (err < 0)
1090 return err;
Jean Delvarec3df5802005-10-26 21:39:40 +02001091
1092 switch (val) {
1093 case 0:
1094 client->flags &= ~I2C_CLIENT_PEC;
1095 break;
1096 case 1:
1097 client->flags |= I2C_CLIENT_PEC;
1098 break;
1099 default:
1100 return -EINVAL;
1101 }
1102
1103 return count;
1104}
1105
1106static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108/*
1109 * Real code
1110 */
1111
Jean Delvare9b0e8522008-07-16 19:30:15 +02001112/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001113static int lm90_detect(struct i2c_client *new_client,
Jean Delvare9b0e8522008-07-16 19:30:15 +02001114 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Jean Delvare9b0e8522008-07-16 19:30:15 +02001116 struct i2c_adapter *adapter = new_client->adapter;
1117 int address = new_client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +01001118 const char *name = NULL;
1119 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +02001122 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Jean Delvare8f2fa772009-12-09 20:35:53 +01001124 /* detection and identification */
1125 if ((man_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +01001126 LM90_REG_R_MAN_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +01001127 || (chip_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +01001128 LM90_REG_R_CHIP_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +01001129 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +01001130 LM90_REG_R_CONFIG1)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +01001131 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +01001132 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +01001133 return -ENODEV;
1134
1135 if ((address == 0x4C || address == 0x4D)
1136 && man_id == 0x01) { /* National Semiconductor */
1137 int reg_config2;
1138
1139 reg_config2 = i2c_smbus_read_byte_data(new_client,
1140 LM90_REG_R_CONFIG2);
1141 if (reg_config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001142 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Jean Delvare8f2fa772009-12-09 20:35:53 +01001144 if ((reg_config1 & 0x2A) == 0x00
1145 && (reg_config2 & 0xF8) == 0x00
1146 && reg_convrate <= 0x09) {
1147 if (address == 0x4C
1148 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1149 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +01001151 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1152 name = "lm99";
1153 dev_info(&adapter->dev,
1154 "Assuming LM99 chip at 0x%02x\n",
1155 address);
1156 dev_info(&adapter->dev,
1157 "If it is an LM89, instantiate it "
1158 "with the new_device sysfs "
1159 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001160 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +01001161 if (address == 0x4C
1162 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1163 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001166 } else
1167 if ((address == 0x4C || address == 0x4D)
1168 && man_id == 0x41) { /* Analog Devices */
1169 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1170 && (reg_config1 & 0x3F) == 0x00
1171 && reg_convrate <= 0x0A) {
1172 name = "adm1032";
1173 /* The ADM1032 supports PEC, but only if combined
1174 transactions are not used. */
1175 if (i2c_check_functionality(adapter,
1176 I2C_FUNC_SMBUS_BYTE))
1177 info->flags |= I2C_CLIENT_PEC;
1178 } else
1179 if (chip_id == 0x51 /* ADT7461 */
1180 && (reg_config1 & 0x1B) == 0x00
1181 && reg_convrate <= 0x0A) {
1182 name = "adt7461";
Guenter Roeck5a4e5e62011-04-29 16:33:35 +02001183 } else
1184 if (chip_id == 0x57 /* ADT7461A, NCT1008 */
1185 && (reg_config1 & 0x1B) == 0x00
1186 && reg_convrate <= 0x0A) {
1187 name = "adt7461a";
Jean Delvare8f2fa772009-12-09 20:35:53 +01001188 }
1189 } else
1190 if (man_id == 0x4D) { /* Maxim */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001191 int reg_emerg, reg_emerg2, reg_status2;
1192
1193 /*
1194 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1195 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1196 * exists, both readings will reflect the same value. Otherwise,
1197 * the readings will be different.
1198 */
1199 if ((reg_emerg = i2c_smbus_read_byte_data(new_client,
1200 MAX6659_REG_R_REMOTE_EMERG)) < 0
1201 || i2c_smbus_read_byte_data(new_client, LM90_REG_R_MAN_ID) < 0
1202 || (reg_emerg2 = i2c_smbus_read_byte_data(new_client,
1203 MAX6659_REG_R_REMOTE_EMERG)) < 0
1204 || (reg_status2 = i2c_smbus_read_byte_data(new_client,
1205 MAX6696_REG_R_STATUS2)) < 0)
1206 return -ENODEV;
1207
Jean Delvare8f2fa772009-12-09 20:35:53 +01001208 /*
1209 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1210 * register. Reading from that address will return the last
1211 * read value, which in our case is those of the man_id
1212 * register. Likewise, the config1 register seems to lack a
1213 * low nibble, so the value will be those of the previous
1214 * read, so in our case those of the man_id register.
Guenter Roeck13c84952010-10-28 20:31:43 +02001215 * MAX6659 has a third set of upper temperature limit registers.
1216 * Those registers also return values on MAX6657 and MAX6658,
1217 * thus the only way to detect MAX6659 is by its address.
1218 * For this reason it will be mis-detected as MAX6657 if its
1219 * address is 0x4C.
Jean Delvare8f2fa772009-12-09 20:35:53 +01001220 */
1221 if (chip_id == man_id
Guenter Roeck13c84952010-10-28 20:31:43 +02001222 && (address == 0x4C || address == 0x4D || address == 0x4E)
Jean Delvare8f2fa772009-12-09 20:35:53 +01001223 && (reg_config1 & 0x1F) == (man_id & 0x0F)
1224 && reg_convrate <= 0x09) {
Guenter Roeck13c84952010-10-28 20:31:43 +02001225 if (address == 0x4C)
1226 name = "max6657";
1227 else
1228 name = "max6659";
Jean Delvare8f2fa772009-12-09 20:35:53 +01001229 } else
1230 /*
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001231 * Even though MAX6695 and MAX6696 do not have a chip ID
1232 * register, reading it returns 0x01. Bit 4 of the config1
1233 * register is unused and should return zero when read. Bit 0 of
1234 * the status2 register is unused and should return zero when
1235 * read.
1236 *
1237 * MAX6695 and MAX6696 have an additional set of temperature
1238 * limit registers. We can detect those chips by checking if
1239 * one of those registers exists.
1240 */
1241 if (chip_id == 0x01
1242 && (reg_config1 & 0x10) == 0x00
1243 && (reg_status2 & 0x01) == 0x00
1244 && reg_emerg == reg_emerg2
1245 && reg_convrate <= 0x07) {
1246 name = "max6696";
1247 } else
1248 /*
Jean Delvare8f2fa772009-12-09 20:35:53 +01001249 * The chip_id register of the MAX6680 and MAX6681 holds the
1250 * revision of the chip. The lowest bit of the config1 register
1251 * is unused and should return zero when read, so should the
1252 * second to last bit of config1 (software reset).
1253 */
1254 if (chip_id == 0x01
1255 && (reg_config1 & 0x03) == 0x00
1256 && reg_convrate <= 0x07) {
1257 name = "max6680";
1258 } else
1259 /*
1260 * The chip_id register of the MAX6646/6647/6649 holds the
1261 * revision of the chip. The lowest 6 bits of the config1
1262 * register are unused and should return zero when read.
1263 */
1264 if (chip_id == 0x59
1265 && (reg_config1 & 0x3f) == 0x00
1266 && reg_convrate <= 0x07) {
1267 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
Jean Delvare6771ea12010-03-05 22:17:13 +01001269 } else
1270 if (address == 0x4C
1271 && man_id == 0x5C) { /* Winbond/Nuvoton */
Jean Delvarec4f99a22010-10-28 20:31:44 +02001272 int reg_config2;
1273
1274 reg_config2 = i2c_smbus_read_byte_data(new_client,
1275 LM90_REG_R_CONFIG2);
1276 if (reg_config2 < 0)
1277 return -ENODEV;
1278
1279 if ((reg_config1 & 0x2A) == 0x00
1280 && (reg_config2 & 0xF8) == 0x00) {
1281 if (chip_id == 0x01 /* W83L771W/G */
1282 && reg_convrate <= 0x09) {
1283 name = "w83l771";
1284 } else
1285 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1286 && reg_convrate <= 0x08) {
1287 name = "w83l771";
1288 }
Jean Delvare6771ea12010-03-05 22:17:13 +01001289 }
Stijn Devriendt2ef01792011-06-06 10:40:45 +00001290 } else
1291 if (man_id == 0xA1) { /* NXP Semiconductor/Philips */
1292 if (chip_id == 0x00 && address >= 0x48 && address <= 0x4F) {
1293 name = "sa56004";
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296
Jean Delvare8f2fa772009-12-09 20:35:53 +01001297 if (!name) { /* identification failed */
1298 dev_dbg(&adapter->dev,
1299 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1300 "chip_id=0x%02X)\n", address, man_id, chip_id);
1301 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001303
Jean Delvare9b0e8522008-07-16 19:30:15 +02001304 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Jean Delvare9b0e8522008-07-16 19:30:15 +02001306 return 0;
1307}
1308
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001309static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
1310{
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001311 if (data->flags & LM90_HAVE_TEMP3)
1312 sysfs_remove_group(&client->dev.kobj, &lm90_temp3_group);
1313 if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
1314 sysfs_remove_group(&client->dev.kobj,
1315 &lm90_emergency_alarm_group);
Guenter Roeck69487082010-10-28 20:31:43 +02001316 if (data->flags & LM90_HAVE_EMERGENCY)
1317 sysfs_remove_group(&client->dev.kobj,
1318 &lm90_emergency_group);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001319 if (data->flags & LM90_HAVE_OFFSET)
1320 device_remove_file(&client->dev,
1321 &sensor_dev_attr_temp2_offset.dev_attr);
1322 device_remove_file(&client->dev, &dev_attr_pec);
1323 sysfs_remove_group(&client->dev.kobj, &lm90_group);
1324}
1325
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001326static void lm90_init_client(struct i2c_client *client)
1327{
Guenter Roeck0c01b642010-10-28 20:31:44 +02001328 u8 config, convrate;
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001329 struct lm90_data *data = i2c_get_clientdata(client);
1330
Guenter Roeck0c01b642010-10-28 20:31:44 +02001331 if (lm90_read_reg(client, LM90_REG_R_CONVRATE, &convrate) < 0) {
1332 dev_warn(&client->dev, "Failed to read convrate register!\n");
1333 convrate = LM90_DEF_CONVRATE_RVAL;
1334 }
1335 data->convrate_orig = convrate;
1336
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001337 /*
1338 * Start the conversions.
1339 */
Guenter Roeck0c01b642010-10-28 20:31:44 +02001340 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001341 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
1342 dev_warn(&client->dev, "Initialization failed!\n");
1343 return;
1344 }
1345 data->config_orig = config;
1346
1347 /* Check Temperature Range Select */
1348 if (data->kind == adt7461) {
1349 if (config & 0x04)
1350 data->flags |= LM90_FLAG_ADT7461_EXT;
1351 }
1352
1353 /*
1354 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1355 * 0.125 degree resolution) and range (0x08, extend range
1356 * to -64 degree) mode for the remote temperature sensor.
1357 */
1358 if (data->kind == max6680)
1359 config |= 0x18;
1360
1361 /*
1362 * Select external channel 0 for max6695/96
1363 */
1364 if (data->kind == max6696)
1365 config &= ~0x08;
1366
1367 config &= 0xBF; /* run */
1368 if (config != data->config_orig) /* Only write if changed */
1369 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1370}
1371
Jean Delvare9b0e8522008-07-16 19:30:15 +02001372static int lm90_probe(struct i2c_client *new_client,
1373 const struct i2c_device_id *id)
1374{
1375 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
1376 struct lm90_data *data;
1377 int err;
1378
1379 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
1380 if (!data) {
1381 err = -ENOMEM;
1382 goto exit;
1383 }
1384 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001385 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Jean Delvare9b0e8522008-07-16 19:30:15 +02001387 /* Set the device type */
1388 data->kind = id->driver_data;
1389 if (data->kind == adm1032) {
1390 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1391 new_client->flags &= ~I2C_CLIENT_PEC;
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Jean Delvare53de3342010-03-05 22:17:15 +01001394 /* Different devices have different alarm bits triggering the
1395 * ALERT# output */
Guenter Roeck4667bcb2010-10-28 20:31:43 +02001396 data->alert_alarms = lm90_params[data->kind].alert_alarms;
Jean Delvare53de3342010-03-05 22:17:15 +01001397
Guenter Roeck88073bb2010-10-28 20:31:43 +02001398 /* Set chip capabilities */
Guenter Roeck4667bcb2010-10-28 20:31:43 +02001399 data->flags = lm90_params[data->kind].flags;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001400
Guenter Roeck0c01b642010-10-28 20:31:44 +02001401 /* Set maximum conversion rate */
1402 data->max_convrate = lm90_params[data->kind].max_convrate;
1403
Stijn Devriendt2ef01792011-06-06 10:40:45 +00001404 if (data->flags & LM90_HAVE_LOCAL_EXT) {
1405 data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
1406 WARN_ON(data->reg_local_ext == 0);
1407 }
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /* Initialize the LM90 chip */
1410 lm90_init_client(new_client);
1411
1412 /* Register sysfs hooks */
Guenter Roeck11e57812010-10-28 20:31:42 +02001413 err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
1414 if (err)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001415 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +02001416 if (new_client->flags & I2C_CLIENT_PEC) {
Guenter Roeck11e57812010-10-28 20:31:42 +02001417 err = device_create_file(&new_client->dev, &dev_attr_pec);
1418 if (err)
Jean Delvare0e39e012006-09-24 21:16:40 +02001419 goto exit_remove_files;
1420 }
Guenter Roeck88073bb2010-10-28 20:31:43 +02001421 if (data->flags & LM90_HAVE_OFFSET) {
Guenter Roeck11e57812010-10-28 20:31:42 +02001422 err = device_create_file(&new_client->dev,
1423 &sensor_dev_attr_temp2_offset.dev_attr);
1424 if (err)
Jean Delvare69f2f962007-09-05 14:15:37 +02001425 goto exit_remove_files;
1426 }
Guenter Roeck69487082010-10-28 20:31:43 +02001427 if (data->flags & LM90_HAVE_EMERGENCY) {
1428 err = sysfs_create_group(&new_client->dev.kobj,
1429 &lm90_emergency_group);
1430 if (err)
1431 goto exit_remove_files;
1432 }
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001433 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1434 err = sysfs_create_group(&new_client->dev.kobj,
1435 &lm90_emergency_alarm_group);
1436 if (err)
1437 goto exit_remove_files;
1438 }
1439 if (data->flags & LM90_HAVE_TEMP3) {
1440 err = sysfs_create_group(&new_client->dev.kobj,
1441 &lm90_temp3_group);
1442 if (err)
1443 goto exit_remove_files;
1444 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001445
Tony Jones1beeffe2007-08-20 13:46:20 -07001446 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1447 if (IS_ERR(data->hwmon_dev)) {
1448 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001449 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001450 }
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return 0;
1453
Jean Delvare0e39e012006-09-24 21:16:40 +02001454exit_remove_files:
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001455 lm90_remove_files(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456exit_free:
1457 kfree(data);
1458exit:
1459 return err;
1460}
1461
Jean Delvare9b0e8522008-07-16 19:30:15 +02001462static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001464 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Tony Jones1beeffe2007-08-20 13:46:20 -07001466 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001467 lm90_remove_files(client, data);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001468
Jean Delvare95238362010-03-05 22:17:14 +01001469 /* Restore initial configuration */
Guenter Roeck0c01b642010-10-28 20:31:44 +02001470 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
1471 data->convrate_orig);
Jean Delvare95238362010-03-05 22:17:14 +01001472 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1473 data->config_orig);
1474
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001475 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return 0;
1477}
1478
Jean Delvare53de3342010-03-05 22:17:15 +01001479static void lm90_alert(struct i2c_client *client, unsigned int flag)
1480{
1481 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001482 u8 config, alarms, alarms2 = 0;
Jean Delvare53de3342010-03-05 22:17:15 +01001483
1484 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001485
1486 if (data->kind == max6696)
1487 lm90_read_reg(client, MAX6696_REG_R_STATUS2, &alarms2);
1488
1489 if ((alarms & 0x7f) == 0 && (alarms2 & 0xfe) == 0) {
Jean Delvare53de3342010-03-05 22:17:15 +01001490 dev_info(&client->dev, "Everything OK\n");
1491 } else {
1492 if (alarms & 0x61)
1493 dev_warn(&client->dev,
1494 "temp%d out of range, please check!\n", 1);
1495 if (alarms & 0x1a)
1496 dev_warn(&client->dev,
1497 "temp%d out of range, please check!\n", 2);
1498 if (alarms & 0x04)
1499 dev_warn(&client->dev,
1500 "temp%d diode open, please check!\n", 2);
1501
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001502 if (alarms2 & 0x18)
1503 dev_warn(&client->dev,
1504 "temp%d out of range, please check!\n", 3);
1505
Jean Delvare53de3342010-03-05 22:17:15 +01001506 /* Disable ALERT# output, because these chips don't implement
1507 SMBus alert correctly; they should only hold the alert line
1508 low briefly. */
Guenter Roeck11793242010-10-28 20:31:44 +02001509 if ((data->flags & LM90_HAVE_BROKEN_ALERT)
Jean Delvare53de3342010-03-05 22:17:15 +01001510 && (alarms & data->alert_alarms)) {
1511 dev_dbg(&client->dev, "Disabling ALERT#\n");
1512 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1513 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1514 config | 0x80);
1515 }
1516 }
1517}
1518
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001519static struct i2c_driver lm90_driver = {
1520 .class = I2C_CLASS_HWMON,
1521 .driver = {
1522 .name = "lm90",
1523 },
1524 .probe = lm90_probe,
1525 .remove = lm90_remove,
1526 .alert = lm90_alert,
1527 .id_table = lm90_id,
1528 .detect = lm90_detect,
1529 .address_list = normal_i2c,
1530};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532static int __init sensors_lm90_init(void)
1533{
1534 return i2c_add_driver(&lm90_driver);
1535}
1536
1537static void __exit sensors_lm90_exit(void)
1538{
1539 i2c_del_driver(&lm90_driver);
1540}
1541
1542MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1543MODULE_DESCRIPTION("LM90/ADM1032 driver");
1544MODULE_LICENSE("GPL");
1545
1546module_init(sensors_lm90_init);
1547module_exit(sensors_lm90_exit);