Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * lm90.c - Part of lm_sensors, Linux kernel modules for hardware | |
3 | * monitoring | |
95238364 | 4 | * Copyright (C) 2003-2010 Jean Delvare <khali@linux-fr.org> |
1da177e4 LT |
5 | * |
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 | |
a874a10c | 9 | * temperature) and a 3-4 deg accuracy. |
1da177e4 LT |
10 | * |
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 | |
97ae60bb | 15 | * degrees, which allows for higher temperatures measurement. |
44bbe87e | 16 | * Note that there is no way to differentiate between both chips. |
97ae60bb | 17 | * When device is auto-detected, the driver will assume an LM99. |
1da177e4 LT |
18 | * |
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. | |
1da177e4 LT |
22 | * |
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 | |
a874a10c JD |
25 | * that are not handled by this driver. Among others, it has a higher |
26 | * accuracy than the LM90, much like the LM86 does. | |
1da177e4 LT |
27 | * |
28 | * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor | |
a874a10c | 29 | * chips made by Maxim. These chips are similar to the LM86. |
44bbe87e | 30 | * Note that there is no easy way to differentiate between the three |
6948708d GR |
31 | * 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. | |
1da177e4 | 36 | * |
1a51e068 DW |
37 | * 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. | |
271dabf5 | 41 | * |
32c82a93 RB |
42 | * This driver also supports the MAX6680 and MAX6681, two other sensor |
43 | * chips made by Maxim. These are quite similar to the other Maxim | |
a874a10c JD |
44 | * chips. The MAX6680 and MAX6681 only differ in the pinout so they can |
45 | * be treated identically. | |
32c82a93 | 46 | * |
23b2d477 NC |
47 | * This driver also supports the ADT7461 chip from Analog Devices. |
48 | * It's supported in both compatibility and extended mode. It is mostly | |
49 | * compatible with LM90 except for a data format difference for the | |
50 | * temperature value registers. | |
1da177e4 LT |
51 | * |
52 | * Since the LM90 was the first chipset supported by this driver, most | |
53 | * comments will refer to this chipset, but are actually general and | |
54 | * concern all supported chipsets, unless mentioned otherwise. | |
55 | * | |
56 | * This program is free software; you can redistribute it and/or modify | |
57 | * it under the terms of the GNU General Public License as published by | |
58 | * the Free Software Foundation; either version 2 of the License, or | |
59 | * (at your option) any later version. | |
60 | * | |
61 | * This program is distributed in the hope that it will be useful, | |
62 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
63 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
64 | * GNU General Public License for more details. | |
65 | * | |
66 | * You should have received a copy of the GNU General Public License | |
67 | * along with this program; if not, write to the Free Software | |
68 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
69 | */ | |
70 | ||
1da177e4 LT |
71 | #include <linux/module.h> |
72 | #include <linux/init.h> | |
73 | #include <linux/slab.h> | |
74 | #include <linux/jiffies.h> | |
75 | #include <linux/i2c.h> | |
10c08f81 | 76 | #include <linux/hwmon-sysfs.h> |
943b0830 MH |
77 | #include <linux/hwmon.h> |
78 | #include <linux/err.h> | |
9a61bf63 | 79 | #include <linux/mutex.h> |
0e39e01c | 80 | #include <linux/sysfs.h> |
1da177e4 LT |
81 | |
82 | /* | |
83 | * Addresses to scan | |
84 | * Address is fully defined internally and cannot be changed except for | |
32c82a93 | 85 | * MAX6659, MAX6680 and MAX6681. |
271dabf5 BH |
86 | * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657 |
87 | * and MAX6658 have address 0x4c. | |
88 | * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d. | |
89 | * MAX6647 has address 0x4e. | |
13c84951 | 90 | * MAX6659 can have address 0x4c, 0x4d or 0x4e. |
32c82a93 RB |
91 | * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, |
92 | * 0x4c, 0x4d or 0x4e. | |
1da177e4 LT |
93 | */ |
94 | ||
25e9c86d MH |
95 | static const unsigned short normal_i2c[] = { |
96 | 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; | |
1da177e4 | 97 | |
13c84951 GR |
98 | enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680, |
99 | max6646, w83l771 }; | |
1da177e4 LT |
100 | |
101 | /* | |
102 | * The LM90 registers | |
103 | */ | |
104 | ||
105 | #define LM90_REG_R_MAN_ID 0xFE | |
106 | #define LM90_REG_R_CHIP_ID 0xFF | |
107 | #define LM90_REG_R_CONFIG1 0x03 | |
108 | #define LM90_REG_W_CONFIG1 0x09 | |
109 | #define LM90_REG_R_CONFIG2 0xBF | |
110 | #define LM90_REG_W_CONFIG2 0xBF | |
111 | #define LM90_REG_R_CONVRATE 0x04 | |
112 | #define LM90_REG_W_CONVRATE 0x0A | |
113 | #define LM90_REG_R_STATUS 0x02 | |
114 | #define LM90_REG_R_LOCAL_TEMP 0x00 | |
115 | #define LM90_REG_R_LOCAL_HIGH 0x05 | |
116 | #define LM90_REG_W_LOCAL_HIGH 0x0B | |
117 | #define LM90_REG_R_LOCAL_LOW 0x06 | |
118 | #define LM90_REG_W_LOCAL_LOW 0x0C | |
119 | #define LM90_REG_R_LOCAL_CRIT 0x20 | |
120 | #define LM90_REG_W_LOCAL_CRIT 0x20 | |
121 | #define LM90_REG_R_REMOTE_TEMPH 0x01 | |
122 | #define LM90_REG_R_REMOTE_TEMPL 0x10 | |
123 | #define LM90_REG_R_REMOTE_OFFSH 0x11 | |
124 | #define LM90_REG_W_REMOTE_OFFSH 0x11 | |
125 | #define LM90_REG_R_REMOTE_OFFSL 0x12 | |
126 | #define LM90_REG_W_REMOTE_OFFSL 0x12 | |
127 | #define LM90_REG_R_REMOTE_HIGHH 0x07 | |
128 | #define LM90_REG_W_REMOTE_HIGHH 0x0D | |
129 | #define LM90_REG_R_REMOTE_HIGHL 0x13 | |
130 | #define LM90_REG_W_REMOTE_HIGHL 0x13 | |
131 | #define LM90_REG_R_REMOTE_LOWH 0x08 | |
132 | #define LM90_REG_W_REMOTE_LOWH 0x0E | |
133 | #define LM90_REG_R_REMOTE_LOWL 0x14 | |
134 | #define LM90_REG_W_REMOTE_LOWL 0x14 | |
135 | #define LM90_REG_R_REMOTE_CRIT 0x19 | |
136 | #define LM90_REG_W_REMOTE_CRIT 0x19 | |
137 | #define LM90_REG_R_TCRIT_HYST 0x21 | |
138 | #define LM90_REG_W_TCRIT_HYST 0x21 | |
139 | ||
271dabf5 | 140 | /* MAX6646/6647/6649/6657/6658/6659 registers */ |
f65e1708 JD |
141 | |
142 | #define MAX6657_REG_R_LOCAL_TEMPL 0x11 | |
6948708d GR |
143 | #define MAX6659_REG_R_REMOTE_EMERG 0x16 |
144 | #define MAX6659_REG_W_REMOTE_EMERG 0x16 | |
145 | #define MAX6659_REG_R_LOCAL_EMERG 0x17 | |
146 | #define MAX6659_REG_W_LOCAL_EMERG 0x17 | |
f65e1708 | 147 | |
23b2d477 NC |
148 | /* |
149 | * Device flags | |
150 | */ | |
88073bb1 GR |
151 | #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */ |
152 | /* Device features */ | |
153 | #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */ | |
154 | #define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */ | |
155 | #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */ | |
6948708d | 156 | #define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */ |
23b2d477 | 157 | |
1da177e4 LT |
158 | /* |
159 | * Functions declaration | |
160 | */ | |
161 | ||
310ec792 | 162 | static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info); |
9b0e8526 JD |
163 | static int lm90_probe(struct i2c_client *client, |
164 | const struct i2c_device_id *id); | |
1da177e4 | 165 | static void lm90_init_client(struct i2c_client *client); |
53de3342 | 166 | static void lm90_alert(struct i2c_client *client, unsigned int flag); |
9b0e8526 | 167 | static int lm90_remove(struct i2c_client *client); |
1da177e4 LT |
168 | static struct lm90_data *lm90_update_device(struct device *dev); |
169 | ||
170 | /* | |
171 | * Driver data (common to all clients) | |
172 | */ | |
173 | ||
9b0e8526 JD |
174 | static const struct i2c_device_id lm90_id[] = { |
175 | { "adm1032", adm1032 }, | |
176 | { "adt7461", adt7461 }, | |
177 | { "lm90", lm90 }, | |
178 | { "lm86", lm86 }, | |
97ae60bb JD |
179 | { "lm89", lm86 }, |
180 | { "lm99", lm99 }, | |
271dabf5 BH |
181 | { "max6646", max6646 }, |
182 | { "max6647", max6646 }, | |
183 | { "max6649", max6646 }, | |
9b0e8526 JD |
184 | { "max6657", max6657 }, |
185 | { "max6658", max6657 }, | |
13c84951 | 186 | { "max6659", max6659 }, |
9b0e8526 JD |
187 | { "max6680", max6680 }, |
188 | { "max6681", max6680 }, | |
6771ea1f | 189 | { "w83l771", w83l771 }, |
9b0e8526 JD |
190 | { } |
191 | }; | |
192 | MODULE_DEVICE_TABLE(i2c, lm90_id); | |
193 | ||
1da177e4 | 194 | static struct i2c_driver lm90_driver = { |
9b0e8526 | 195 | .class = I2C_CLASS_HWMON, |
cdaf7934 | 196 | .driver = { |
cdaf7934 LR |
197 | .name = "lm90", |
198 | }, | |
9b0e8526 JD |
199 | .probe = lm90_probe, |
200 | .remove = lm90_remove, | |
53de3342 | 201 | .alert = lm90_alert, |
9b0e8526 JD |
202 | .id_table = lm90_id, |
203 | .detect = lm90_detect, | |
c3813d6a | 204 | .address_list = normal_i2c, |
1da177e4 LT |
205 | }; |
206 | ||
207 | /* | |
208 | * Client data (each client gets its own) | |
209 | */ | |
210 | ||
211 | struct lm90_data { | |
1beeffe4 | 212 | struct device *hwmon_dev; |
9a61bf63 | 213 | struct mutex update_lock; |
1da177e4 LT |
214 | char valid; /* zero until following fields are valid */ |
215 | unsigned long last_updated; /* in jiffies */ | |
216 | int kind; | |
23b2d477 | 217 | int flags; |
1da177e4 | 218 | |
95238364 | 219 | u8 config_orig; /* Original configuration register value */ |
53de3342 | 220 | u8 alert_alarms; /* Which alarm bits trigger ALERT# */ |
95238364 | 221 | |
1da177e4 | 222 | /* registers values */ |
6948708d | 223 | s8 temp8[6]; /* 0: local low limit |
f65e1708 JD |
224 | 1: local high limit |
225 | 2: local critical limit | |
6948708d GR |
226 | 3: remote critical limit |
227 | 4: local emergency limit (max6659 only) | |
228 | 5: remote emergency limit (max6659 only) */ | |
f65e1708 | 229 | s16 temp11[5]; /* 0: remote input |
30d7394b | 230 | 1: remote low limit |
69f2f96d | 231 | 2: remote high limit |
13c84951 | 232 | 3: remote offset (except max6646 and max6657/58/59) |
f65e1708 | 233 | 4: local input */ |
1da177e4 LT |
234 | u8 temp_hyst; |
235 | u8 alarms; /* bitvector */ | |
236 | }; | |
237 | ||
cea50fe2 NC |
238 | /* |
239 | * Conversions | |
240 | * For local temperatures and limits, critical limits and the hysteresis | |
241 | * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius. | |
242 | * For remote temperatures and limits, it uses signed 11-bit values with | |
271dabf5 BH |
243 | * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some |
244 | * Maxim chips use unsigned values. | |
cea50fe2 NC |
245 | */ |
246 | ||
9d4d3834 | 247 | static inline int temp_from_s8(s8 val) |
cea50fe2 NC |
248 | { |
249 | return val * 1000; | |
250 | } | |
251 | ||
271dabf5 BH |
252 | static inline int temp_from_u8(u8 val) |
253 | { | |
254 | return val * 1000; | |
255 | } | |
256 | ||
9d4d3834 | 257 | static inline int temp_from_s16(s16 val) |
cea50fe2 NC |
258 | { |
259 | return val / 32 * 125; | |
260 | } | |
261 | ||
271dabf5 BH |
262 | static inline int temp_from_u16(u16 val) |
263 | { | |
264 | return val / 32 * 125; | |
265 | } | |
266 | ||
9d4d3834 | 267 | static s8 temp_to_s8(long val) |
cea50fe2 NC |
268 | { |
269 | if (val <= -128000) | |
270 | return -128; | |
271 | if (val >= 127000) | |
272 | return 127; | |
273 | if (val < 0) | |
274 | return (val - 500) / 1000; | |
275 | return (val + 500) / 1000; | |
276 | } | |
277 | ||
271dabf5 BH |
278 | static u8 temp_to_u8(long val) |
279 | { | |
280 | if (val <= 0) | |
281 | return 0; | |
282 | if (val >= 255000) | |
283 | return 255; | |
284 | return (val + 500) / 1000; | |
285 | } | |
286 | ||
9d4d3834 | 287 | static s16 temp_to_s16(long val) |
cea50fe2 NC |
288 | { |
289 | if (val <= -128000) | |
290 | return 0x8000; | |
291 | if (val >= 127875) | |
292 | return 0x7FE0; | |
293 | if (val < 0) | |
294 | return (val - 62) / 125 * 32; | |
295 | return (val + 62) / 125 * 32; | |
296 | } | |
297 | ||
298 | static u8 hyst_to_reg(long val) | |
299 | { | |
300 | if (val <= 0) | |
301 | return 0; | |
302 | if (val >= 30500) | |
303 | return 31; | |
304 | return (val + 500) / 1000; | |
305 | } | |
306 | ||
307 | /* | |
23b2d477 NC |
308 | * ADT7461 in compatibility mode is almost identical to LM90 except that |
309 | * attempts to write values that are outside the range 0 < temp < 127 are | |
310 | * treated as the boundary value. | |
311 | * | |
312 | * ADT7461 in "extended mode" operation uses unsigned integers offset by | |
313 | * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC. | |
cea50fe2 | 314 | */ |
9d4d3834 | 315 | static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val) |
cea50fe2 | 316 | { |
23b2d477 NC |
317 | if (data->flags & LM90_FLAG_ADT7461_EXT) |
318 | return (val - 64) * 1000; | |
319 | else | |
9d4d3834 | 320 | return temp_from_s8(val); |
cea50fe2 NC |
321 | } |
322 | ||
9d4d3834 | 323 | static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val) |
cea50fe2 | 324 | { |
23b2d477 NC |
325 | if (data->flags & LM90_FLAG_ADT7461_EXT) |
326 | return (val - 0x4000) / 64 * 250; | |
327 | else | |
9d4d3834 | 328 | return temp_from_s16(val); |
23b2d477 NC |
329 | } |
330 | ||
9d4d3834 | 331 | static u8 temp_to_u8_adt7461(struct lm90_data *data, long val) |
23b2d477 NC |
332 | { |
333 | if (data->flags & LM90_FLAG_ADT7461_EXT) { | |
334 | if (val <= -64000) | |
335 | return 0; | |
336 | if (val >= 191000) | |
337 | return 0xFF; | |
338 | return (val + 500 + 64000) / 1000; | |
339 | } else { | |
340 | if (val <= 0) | |
341 | return 0; | |
342 | if (val >= 127000) | |
343 | return 127; | |
344 | return (val + 500) / 1000; | |
345 | } | |
346 | } | |
347 | ||
9d4d3834 | 348 | static u16 temp_to_u16_adt7461(struct lm90_data *data, long val) |
23b2d477 NC |
349 | { |
350 | if (data->flags & LM90_FLAG_ADT7461_EXT) { | |
351 | if (val <= -64000) | |
352 | return 0; | |
353 | if (val >= 191750) | |
354 | return 0xFFC0; | |
355 | return (val + 64000 + 125) / 250 * 64; | |
356 | } else { | |
357 | if (val <= 0) | |
358 | return 0; | |
359 | if (val >= 127750) | |
360 | return 0x7FC0; | |
361 | return (val + 125) / 250 * 64; | |
362 | } | |
cea50fe2 NC |
363 | } |
364 | ||
1da177e4 LT |
365 | /* |
366 | * Sysfs stuff | |
367 | */ | |
368 | ||
30d7394b JD |
369 | static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr, |
370 | char *buf) | |
371 | { | |
372 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
373 | struct lm90_data *data = lm90_update_device(dev); | |
23b2d477 NC |
374 | int temp; |
375 | ||
376 | if (data->kind == adt7461) | |
9d4d3834 | 377 | temp = temp_from_u8_adt7461(data, data->temp8[attr->index]); |
271dabf5 BH |
378 | else if (data->kind == max6646) |
379 | temp = temp_from_u8(data->temp8[attr->index]); | |
23b2d477 | 380 | else |
9d4d3834 | 381 | temp = temp_from_s8(data->temp8[attr->index]); |
23b2d477 | 382 | |
97ae60bb JD |
383 | /* +16 degrees offset for temp2 for the LM99 */ |
384 | if (data->kind == lm99 && attr->index == 3) | |
385 | temp += 16000; | |
386 | ||
23b2d477 | 387 | return sprintf(buf, "%d\n", temp); |
30d7394b JD |
388 | } |
389 | ||
390 | static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, | |
391 | const char *buf, size_t count) | |
392 | { | |
6948708d | 393 | static const u8 reg[6] = { |
30d7394b JD |
394 | LM90_REG_W_LOCAL_LOW, |
395 | LM90_REG_W_LOCAL_HIGH, | |
396 | LM90_REG_W_LOCAL_CRIT, | |
397 | LM90_REG_W_REMOTE_CRIT, | |
6948708d GR |
398 | MAX6659_REG_W_LOCAL_EMERG, |
399 | MAX6659_REG_W_REMOTE_EMERG, | |
30d7394b JD |
400 | }; |
401 | ||
402 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
403 | struct i2c_client *client = to_i2c_client(dev); | |
404 | struct lm90_data *data = i2c_get_clientdata(client); | |
30d7394b | 405 | int nr = attr->index; |
11e57812 GR |
406 | long val; |
407 | int err; | |
408 | ||
409 | err = strict_strtol(buf, 10, &val); | |
410 | if (err < 0) | |
411 | return err; | |
30d7394b | 412 | |
97ae60bb JD |
413 | /* +16 degrees offset for temp2 for the LM99 */ |
414 | if (data->kind == lm99 && attr->index == 3) | |
415 | val -= 16000; | |
416 | ||
9a61bf63 | 417 | mutex_lock(&data->update_lock); |
30d7394b | 418 | if (data->kind == adt7461) |
9d4d3834 | 419 | data->temp8[nr] = temp_to_u8_adt7461(data, val); |
271dabf5 BH |
420 | else if (data->kind == max6646) |
421 | data->temp8[nr] = temp_to_u8(val); | |
30d7394b | 422 | else |
9d4d3834 | 423 | data->temp8[nr] = temp_to_s8(val); |
f65e1708 | 424 | i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]); |
9a61bf63 | 425 | mutex_unlock(&data->update_lock); |
30d7394b | 426 | return count; |
1da177e4 | 427 | } |
30d7394b JD |
428 | |
429 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, | |
430 | char *buf) | |
431 | { | |
96512861 | 432 | struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr); |
30d7394b | 433 | struct lm90_data *data = lm90_update_device(dev); |
23b2d477 NC |
434 | int temp; |
435 | ||
436 | if (data->kind == adt7461) | |
9d4d3834 | 437 | temp = temp_from_u16_adt7461(data, data->temp11[attr->index]); |
271dabf5 BH |
438 | else if (data->kind == max6646) |
439 | temp = temp_from_u16(data->temp11[attr->index]); | |
23b2d477 | 440 | else |
9d4d3834 | 441 | temp = temp_from_s16(data->temp11[attr->index]); |
23b2d477 | 442 | |
97ae60bb JD |
443 | /* +16 degrees offset for temp2 for the LM99 */ |
444 | if (data->kind == lm99 && attr->index <= 2) | |
445 | temp += 16000; | |
446 | ||
23b2d477 | 447 | return sprintf(buf, "%d\n", temp); |
1da177e4 | 448 | } |
30d7394b JD |
449 | |
450 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, | |
451 | const char *buf, size_t count) | |
452 | { | |
96512861 GR |
453 | struct { |
454 | u8 high; | |
455 | u8 low; | |
456 | } reg[3] = { | |
457 | { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL }, | |
458 | { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }, | |
459 | { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL } | |
30d7394b JD |
460 | }; |
461 | ||
96512861 | 462 | struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr); |
30d7394b JD |
463 | struct i2c_client *client = to_i2c_client(dev); |
464 | struct lm90_data *data = i2c_get_clientdata(client); | |
96512861 GR |
465 | int nr = attr->nr; |
466 | int index = attr->index; | |
11e57812 GR |
467 | long val; |
468 | int err; | |
469 | ||
470 | err = strict_strtol(buf, 10, &val); | |
471 | if (err < 0) | |
472 | return err; | |
30d7394b | 473 | |
97ae60bb | 474 | /* +16 degrees offset for temp2 for the LM99 */ |
96512861 | 475 | if (data->kind == lm99 && index <= 2) |
97ae60bb JD |
476 | val -= 16000; |
477 | ||
9a61bf63 | 478 | mutex_lock(&data->update_lock); |
30d7394b | 479 | if (data->kind == adt7461) |
96512861 | 480 | data->temp11[index] = temp_to_u16_adt7461(data, val); |
271dabf5 | 481 | else if (data->kind == max6646) |
96512861 | 482 | data->temp11[index] = temp_to_u8(val) << 8; |
88073bb1 | 483 | else if (data->flags & LM90_HAVE_REM_LIMIT_EXT) |
96512861 | 484 | data->temp11[index] = temp_to_s16(val); |
88073bb1 | 485 | else |
96512861 | 486 | data->temp11[index] = temp_to_s8(val) << 8; |
5f502a83 | 487 | |
96512861 GR |
488 | i2c_smbus_write_byte_data(client, reg[nr].high, |
489 | data->temp11[index] >> 8); | |
88073bb1 | 490 | if (data->flags & LM90_HAVE_REM_LIMIT_EXT) |
96512861 GR |
491 | i2c_smbus_write_byte_data(client, reg[nr].low, |
492 | data->temp11[index] & 0xff); | |
9a61bf63 | 493 | mutex_unlock(&data->update_lock); |
30d7394b | 494 | return count; |
1da177e4 | 495 | } |
30d7394b | 496 | |
11e57812 GR |
497 | static ssize_t show_temphyst(struct device *dev, |
498 | struct device_attribute *devattr, | |
30d7394b JD |
499 | char *buf) |
500 | { | |
501 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
502 | struct lm90_data *data = lm90_update_device(dev); | |
23b2d477 NC |
503 | int temp; |
504 | ||
505 | if (data->kind == adt7461) | |
9d4d3834 | 506 | temp = temp_from_u8_adt7461(data, data->temp8[attr->index]); |
ec38fa2b JD |
507 | else if (data->kind == max6646) |
508 | temp = temp_from_u8(data->temp8[attr->index]); | |
23b2d477 | 509 | else |
9d4d3834 | 510 | temp = temp_from_s8(data->temp8[attr->index]); |
23b2d477 | 511 | |
97ae60bb JD |
512 | /* +16 degrees offset for temp2 for the LM99 */ |
513 | if (data->kind == lm99 && attr->index == 3) | |
514 | temp += 16000; | |
515 | ||
9d4d3834 | 516 | return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst)); |
1da177e4 | 517 | } |
1da177e4 | 518 | |
30d7394b JD |
519 | static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy, |
520 | const char *buf, size_t count) | |
1da177e4 LT |
521 | { |
522 | struct i2c_client *client = to_i2c_client(dev); | |
523 | struct lm90_data *data = i2c_get_clientdata(client); | |
11e57812 GR |
524 | long val; |
525 | int err; | |
ec38fa2b | 526 | int temp; |
1da177e4 | 527 | |
11e57812 GR |
528 | err = strict_strtol(buf, 10, &val); |
529 | if (err < 0) | |
530 | return err; | |
531 | ||
9a61bf63 | 532 | mutex_lock(&data->update_lock); |
ec38fa2b JD |
533 | if (data->kind == adt7461) |
534 | temp = temp_from_u8_adt7461(data, data->temp8[2]); | |
535 | else if (data->kind == max6646) | |
536 | temp = temp_from_u8(data->temp8[2]); | |
537 | else | |
538 | temp = temp_from_s8(data->temp8[2]); | |
539 | ||
540 | data->temp_hyst = hyst_to_reg(temp - val); | |
1da177e4 | 541 | i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST, |
ec38fa2b | 542 | data->temp_hyst); |
9a61bf63 | 543 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
544 | return count; |
545 | } | |
546 | ||
30d7394b JD |
547 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, |
548 | char *buf) | |
1da177e4 LT |
549 | { |
550 | struct lm90_data *data = lm90_update_device(dev); | |
551 | return sprintf(buf, "%d\n", data->alarms); | |
552 | } | |
553 | ||
2d45771e JD |
554 | static ssize_t show_alarm(struct device *dev, struct device_attribute |
555 | *devattr, char *buf) | |
556 | { | |
557 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
558 | struct lm90_data *data = lm90_update_device(dev); | |
559 | int bitnr = attr->index; | |
560 | ||
561 | return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1); | |
562 | } | |
563 | ||
96512861 GR |
564 | static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4); |
565 | static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0); | |
30d7394b | 566 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8, |
f65e1708 | 567 | set_temp8, 0); |
96512861 GR |
568 | static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11, |
569 | set_temp11, 0, 1); | |
30d7394b | 570 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8, |
f65e1708 | 571 | set_temp8, 1); |
96512861 GR |
572 | static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11, |
573 | set_temp11, 1, 2); | |
30d7394b | 574 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8, |
f65e1708 | 575 | set_temp8, 2); |
30d7394b | 576 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8, |
f65e1708 | 577 | set_temp8, 3); |
30d7394b | 578 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst, |
f65e1708 JD |
579 | set_temphyst, 2); |
580 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3); | |
96512861 GR |
581 | static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, |
582 | set_temp11, 2, 3); | |
2d45771e JD |
583 | |
584 | /* Individual alarm files */ | |
585 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0); | |
586 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1); | |
7817a39e | 587 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); |
2d45771e JD |
588 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); |
589 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); | |
590 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5); | |
591 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); | |
592 | /* Raw alarm file for compatibility */ | |
1da177e4 LT |
593 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
594 | ||
0e39e01c JD |
595 | static struct attribute *lm90_attributes[] = { |
596 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
597 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
598 | &sensor_dev_attr_temp1_min.dev_attr.attr, | |
599 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
600 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
601 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
602 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | |
603 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | |
604 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, | |
605 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, | |
606 | ||
607 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | |
608 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | |
7817a39e | 609 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
0e39e01c JD |
610 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
611 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | |
612 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | |
613 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | |
614 | &dev_attr_alarms.attr, | |
615 | NULL | |
616 | }; | |
617 | ||
618 | static const struct attribute_group lm90_group = { | |
619 | .attrs = lm90_attributes, | |
620 | }; | |
621 | ||
6948708d GR |
622 | /* |
623 | * Additional attributes for devices with emergency sensors | |
624 | */ | |
625 | static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8, | |
626 | set_temp8, 4); | |
627 | static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8, | |
628 | set_temp8, 5); | |
629 | static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst, | |
630 | NULL, 4); | |
631 | static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst, | |
632 | NULL, 5); | |
633 | ||
634 | static struct attribute *lm90_emergency_attributes[] = { | |
635 | &sensor_dev_attr_temp1_emergency.dev_attr.attr, | |
636 | &sensor_dev_attr_temp2_emergency.dev_attr.attr, | |
637 | &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr, | |
638 | &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr, | |
639 | NULL | |
640 | }; | |
641 | ||
642 | static const struct attribute_group lm90_emergency_group = { | |
643 | .attrs = lm90_emergency_attributes, | |
644 | }; | |
645 | ||
c3df5806 JD |
646 | /* pec used for ADM1032 only */ |
647 | static ssize_t show_pec(struct device *dev, struct device_attribute *dummy, | |
648 | char *buf) | |
649 | { | |
650 | struct i2c_client *client = to_i2c_client(dev); | |
651 | return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC)); | |
652 | } | |
653 | ||
654 | static ssize_t set_pec(struct device *dev, struct device_attribute *dummy, | |
655 | const char *buf, size_t count) | |
656 | { | |
657 | struct i2c_client *client = to_i2c_client(dev); | |
11e57812 GR |
658 | long val; |
659 | int err; | |
660 | ||
661 | err = strict_strtol(buf, 10, &val); | |
662 | if (err < 0) | |
663 | return err; | |
c3df5806 JD |
664 | |
665 | switch (val) { | |
666 | case 0: | |
667 | client->flags &= ~I2C_CLIENT_PEC; | |
668 | break; | |
669 | case 1: | |
670 | client->flags |= I2C_CLIENT_PEC; | |
671 | break; | |
672 | default: | |
673 | return -EINVAL; | |
674 | } | |
675 | ||
676 | return count; | |
677 | } | |
678 | ||
679 | static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec); | |
680 | ||
1da177e4 LT |
681 | /* |
682 | * Real code | |
683 | */ | |
684 | ||
11e57812 GR |
685 | /* |
686 | * The ADM1032 supports PEC but not on write byte transactions, so we need | |
687 | * to explicitly ask for a transaction without PEC. | |
688 | */ | |
c3df5806 JD |
689 | static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value) |
690 | { | |
691 | return i2c_smbus_xfer(client->adapter, client->addr, | |
692 | client->flags & ~I2C_CLIENT_PEC, | |
693 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); | |
694 | } | |
695 | ||
11e57812 GR |
696 | /* |
697 | * It is assumed that client->update_lock is held (unless we are in | |
698 | * detection or initialization steps). This matters when PEC is enabled, | |
699 | * because we don't want the address pointer to change between the write | |
700 | * byte and the read byte transactions. | |
701 | */ | |
702 | static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value) | |
8256fe0f JD |
703 | { |
704 | int err; | |
705 | ||
11e57812 GR |
706 | if (client->flags & I2C_CLIENT_PEC) { |
707 | err = adm1032_write_byte(client, reg); | |
708 | if (err >= 0) | |
709 | err = i2c_smbus_read_byte(client); | |
710 | } else | |
711 | err = i2c_smbus_read_byte_data(client, reg); | |
8256fe0f JD |
712 | |
713 | if (err < 0) { | |
714 | dev_warn(&client->dev, "Register %#02x read failed (%d)\n", | |
715 | reg, err); | |
716 | return err; | |
717 | } | |
718 | *value = err; | |
719 | ||
720 | return 0; | |
721 | } | |
722 | ||
9b0e8526 | 723 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec792 | 724 | static int lm90_detect(struct i2c_client *new_client, |
9b0e8526 | 725 | struct i2c_board_info *info) |
1da177e4 | 726 | { |
9b0e8526 JD |
727 | struct i2c_adapter *adapter = new_client->adapter; |
728 | int address = new_client->addr; | |
8f2fa77c JD |
729 | const char *name = NULL; |
730 | int man_id, chip_id, reg_config1, reg_convrate; | |
1da177e4 LT |
731 | |
732 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
9b0e8526 | 733 | return -ENODEV; |
1da177e4 | 734 | |
8f2fa77c JD |
735 | /* detection and identification */ |
736 | if ((man_id = i2c_smbus_read_byte_data(new_client, | |
e0ae87a4 | 737 | LM90_REG_R_MAN_ID)) < 0 |
8f2fa77c | 738 | || (chip_id = i2c_smbus_read_byte_data(new_client, |
e0ae87a4 | 739 | LM90_REG_R_CHIP_ID)) < 0 |
8f2fa77c | 740 | || (reg_config1 = i2c_smbus_read_byte_data(new_client, |
e0ae87a4 | 741 | LM90_REG_R_CONFIG1)) < 0 |
8f2fa77c | 742 | || (reg_convrate = i2c_smbus_read_byte_data(new_client, |
e0ae87a4 | 743 | LM90_REG_R_CONVRATE)) < 0) |
8f2fa77c JD |
744 | return -ENODEV; |
745 | ||
746 | if ((address == 0x4C || address == 0x4D) | |
747 | && man_id == 0x01) { /* National Semiconductor */ | |
748 | int reg_config2; | |
749 | ||
750 | reg_config2 = i2c_smbus_read_byte_data(new_client, | |
751 | LM90_REG_R_CONFIG2); | |
752 | if (reg_config2 < 0) | |
9b0e8526 | 753 | return -ENODEV; |
8f2fa77c JD |
754 | |
755 | if ((reg_config1 & 0x2A) == 0x00 | |
756 | && (reg_config2 & 0xF8) == 0x00 | |
757 | && reg_convrate <= 0x09) { | |
758 | if (address == 0x4C | |
759 | && (chip_id & 0xF0) == 0x20) { /* LM90 */ | |
760 | name = "lm90"; | |
32c82a93 | 761 | } else |
8f2fa77c JD |
762 | if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */ |
763 | name = "lm99"; | |
764 | dev_info(&adapter->dev, | |
765 | "Assuming LM99 chip at 0x%02x\n", | |
766 | address); | |
767 | dev_info(&adapter->dev, | |
768 | "If it is an LM89, instantiate it " | |
769 | "with the new_device sysfs " | |
770 | "interface\n"); | |
271dabf5 | 771 | } else |
8f2fa77c JD |
772 | if (address == 0x4C |
773 | && (chip_id & 0xF0) == 0x10) { /* LM86 */ | |
774 | name = "lm86"; | |
1da177e4 LT |
775 | } |
776 | } | |
8f2fa77c JD |
777 | } else |
778 | if ((address == 0x4C || address == 0x4D) | |
779 | && man_id == 0x41) { /* Analog Devices */ | |
780 | if ((chip_id & 0xF0) == 0x40 /* ADM1032 */ | |
781 | && (reg_config1 & 0x3F) == 0x00 | |
782 | && reg_convrate <= 0x0A) { | |
783 | name = "adm1032"; | |
784 | /* The ADM1032 supports PEC, but only if combined | |
785 | transactions are not used. */ | |
786 | if (i2c_check_functionality(adapter, | |
787 | I2C_FUNC_SMBUS_BYTE)) | |
788 | info->flags |= I2C_CLIENT_PEC; | |
789 | } else | |
790 | if (chip_id == 0x51 /* ADT7461 */ | |
791 | && (reg_config1 & 0x1B) == 0x00 | |
792 | && reg_convrate <= 0x0A) { | |
793 | name = "adt7461"; | |
794 | } | |
795 | } else | |
796 | if (man_id == 0x4D) { /* Maxim */ | |
797 | /* | |
798 | * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id | |
799 | * register. Reading from that address will return the last | |
800 | * read value, which in our case is those of the man_id | |
801 | * register. Likewise, the config1 register seems to lack a | |
802 | * low nibble, so the value will be those of the previous | |
803 | * read, so in our case those of the man_id register. | |
13c84951 GR |
804 | * MAX6659 has a third set of upper temperature limit registers. |
805 | * Those registers also return values on MAX6657 and MAX6658, | |
806 | * thus the only way to detect MAX6659 is by its address. | |
807 | * For this reason it will be mis-detected as MAX6657 if its | |
808 | * address is 0x4C. | |
8f2fa77c JD |
809 | */ |
810 | if (chip_id == man_id | |
13c84951 | 811 | && (address == 0x4C || address == 0x4D || address == 0x4E) |
8f2fa77c JD |
812 | && (reg_config1 & 0x1F) == (man_id & 0x0F) |
813 | && reg_convrate <= 0x09) { | |
13c84951 GR |
814 | if (address == 0x4C) |
815 | name = "max6657"; | |
816 | else | |
817 | name = "max6659"; | |
8f2fa77c JD |
818 | } else |
819 | /* | |
820 | * The chip_id register of the MAX6680 and MAX6681 holds the | |
821 | * revision of the chip. The lowest bit of the config1 register | |
822 | * is unused and should return zero when read, so should the | |
823 | * second to last bit of config1 (software reset). | |
824 | */ | |
825 | if (chip_id == 0x01 | |
826 | && (reg_config1 & 0x03) == 0x00 | |
827 | && reg_convrate <= 0x07) { | |
828 | name = "max6680"; | |
829 | } else | |
830 | /* | |
831 | * The chip_id register of the MAX6646/6647/6649 holds the | |
832 | * revision of the chip. The lowest 6 bits of the config1 | |
833 | * register are unused and should return zero when read. | |
834 | */ | |
835 | if (chip_id == 0x59 | |
836 | && (reg_config1 & 0x3f) == 0x00 | |
837 | && reg_convrate <= 0x07) { | |
838 | name = "max6646"; | |
1da177e4 | 839 | } |
6771ea1f JD |
840 | } else |
841 | if (address == 0x4C | |
842 | && man_id == 0x5C) { /* Winbond/Nuvoton */ | |
843 | if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */ | |
844 | && (reg_config1 & 0x2A) == 0x00 | |
845 | && reg_convrate <= 0x08) { | |
846 | name = "w83l771"; | |
847 | } | |
1da177e4 LT |
848 | } |
849 | ||
8f2fa77c JD |
850 | if (!name) { /* identification failed */ |
851 | dev_dbg(&adapter->dev, | |
852 | "Unsupported chip at 0x%02x (man_id=0x%02X, " | |
853 | "chip_id=0x%02X)\n", address, man_id, chip_id); | |
854 | return -ENODEV; | |
1da177e4 | 855 | } |
8f2fa77c | 856 | |
9b0e8526 JD |
857 | strlcpy(info->type, name, I2C_NAME_SIZE); |
858 | ||
859 | return 0; | |
860 | } | |
861 | ||
b6fc1bac GR |
862 | static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data) |
863 | { | |
6948708d GR |
864 | if (data->flags & LM90_HAVE_EMERGENCY) |
865 | sysfs_remove_group(&client->dev.kobj, | |
866 | &lm90_emergency_group); | |
b6fc1bac GR |
867 | if (data->flags & LM90_HAVE_OFFSET) |
868 | device_remove_file(&client->dev, | |
869 | &sensor_dev_attr_temp2_offset.dev_attr); | |
870 | device_remove_file(&client->dev, &dev_attr_pec); | |
871 | sysfs_remove_group(&client->dev.kobj, &lm90_group); | |
872 | } | |
873 | ||
9b0e8526 JD |
874 | static int lm90_probe(struct i2c_client *new_client, |
875 | const struct i2c_device_id *id) | |
876 | { | |
877 | struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent); | |
878 | struct lm90_data *data; | |
879 | int err; | |
1da177e4 | 880 | |
9b0e8526 JD |
881 | data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL); |
882 | if (!data) { | |
883 | err = -ENOMEM; | |
884 | goto exit; | |
885 | } | |
886 | i2c_set_clientdata(new_client, data); | |
9a61bf63 | 887 | mutex_init(&data->update_lock); |
1da177e4 | 888 | |
9b0e8526 JD |
889 | /* Set the device type */ |
890 | data->kind = id->driver_data; | |
891 | if (data->kind == adm1032) { | |
892 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) | |
893 | new_client->flags &= ~I2C_CLIENT_PEC; | |
894 | } | |
1da177e4 | 895 | |
53de3342 JD |
896 | /* Different devices have different alarm bits triggering the |
897 | * ALERT# output */ | |
898 | switch (data->kind) { | |
899 | case lm90: | |
900 | case lm99: | |
901 | case lm86: | |
902 | data->alert_alarms = 0x7b; | |
903 | break; | |
904 | default: | |
905 | data->alert_alarms = 0x7c; | |
906 | break; | |
907 | } | |
908 | ||
88073bb1 | 909 | /* Set chip capabilities */ |
13c84951 GR |
910 | if (data->kind != max6657 && data->kind != max6659 |
911 | && data->kind != max6646) | |
88073bb1 GR |
912 | data->flags |= LM90_HAVE_OFFSET; |
913 | ||
13c84951 GR |
914 | if (data->kind == max6657 || data->kind == max6659 |
915 | || data->kind == max6646) | |
88073bb1 GR |
916 | data->flags |= LM90_HAVE_LOCAL_EXT; |
917 | ||
13c84951 GR |
918 | if (data->kind != max6657 && data->kind != max6659 |
919 | && data->kind != max6646 && data->kind != max6680) | |
88073bb1 GR |
920 | data->flags |= LM90_HAVE_REM_LIMIT_EXT; |
921 | ||
6948708d GR |
922 | if (data->kind == max6659) |
923 | data->flags |= LM90_HAVE_EMERGENCY; | |
924 | ||
1da177e4 LT |
925 | /* Initialize the LM90 chip */ |
926 | lm90_init_client(new_client); | |
927 | ||
928 | /* Register sysfs hooks */ | |
11e57812 GR |
929 | err = sysfs_create_group(&new_client->dev.kobj, &lm90_group); |
930 | if (err) | |
9b0e8526 | 931 | goto exit_free; |
0e39e01c | 932 | if (new_client->flags & I2C_CLIENT_PEC) { |
11e57812 GR |
933 | err = device_create_file(&new_client->dev, &dev_attr_pec); |
934 | if (err) | |
0e39e01c JD |
935 | goto exit_remove_files; |
936 | } | |
88073bb1 | 937 | if (data->flags & LM90_HAVE_OFFSET) { |
11e57812 GR |
938 | err = device_create_file(&new_client->dev, |
939 | &sensor_dev_attr_temp2_offset.dev_attr); | |
940 | if (err) | |
69f2f96d JD |
941 | goto exit_remove_files; |
942 | } | |
6948708d GR |
943 | if (data->flags & LM90_HAVE_EMERGENCY) { |
944 | err = sysfs_create_group(&new_client->dev.kobj, | |
945 | &lm90_emergency_group); | |
946 | if (err) | |
947 | goto exit_remove_files; | |
948 | } | |
0e39e01c | 949 | |
1beeffe4 TJ |
950 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
951 | if (IS_ERR(data->hwmon_dev)) { | |
952 | err = PTR_ERR(data->hwmon_dev); | |
0e39e01c | 953 | goto exit_remove_files; |
943b0830 MH |
954 | } |
955 | ||
1da177e4 LT |
956 | return 0; |
957 | ||
0e39e01c | 958 | exit_remove_files: |
b6fc1bac | 959 | lm90_remove_files(new_client, data); |
1da177e4 LT |
960 | exit_free: |
961 | kfree(data); | |
962 | exit: | |
963 | return err; | |
964 | } | |
965 | ||
966 | static void lm90_init_client(struct i2c_client *client) | |
967 | { | |
95238364 | 968 | u8 config; |
32c82a93 | 969 | struct lm90_data *data = i2c_get_clientdata(client); |
1da177e4 LT |
970 | |
971 | /* | |
972 | * Start the conversions. | |
973 | */ | |
974 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, | |
975 | 5); /* 2 Hz */ | |
8256fe0f JD |
976 | if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) { |
977 | dev_warn(&client->dev, "Initialization failed!\n"); | |
978 | return; | |
979 | } | |
95238364 | 980 | data->config_orig = config; |
32c82a93 | 981 | |
23b2d477 NC |
982 | /* Check Temperature Range Select */ |
983 | if (data->kind == adt7461) { | |
984 | if (config & 0x04) | |
985 | data->flags |= LM90_FLAG_ADT7461_EXT; | |
986 | } | |
987 | ||
32c82a93 RB |
988 | /* |
989 | * Put MAX6680/MAX8881 into extended resolution (bit 0x10, | |
990 | * 0.125 degree resolution) and range (0x08, extend range | |
991 | * to -64 degree) mode for the remote temperature sensor. | |
992 | */ | |
11e57812 | 993 | if (data->kind == max6680) |
32c82a93 | 994 | config |= 0x18; |
32c82a93 RB |
995 | |
996 | config &= 0xBF; /* run */ | |
95238364 | 997 | if (config != data->config_orig) /* Only write if changed */ |
32c82a93 | 998 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config); |
1da177e4 LT |
999 | } |
1000 | ||
9b0e8526 | 1001 | static int lm90_remove(struct i2c_client *client) |
1da177e4 | 1002 | { |
943b0830 | 1003 | struct lm90_data *data = i2c_get_clientdata(client); |
1da177e4 | 1004 | |
1beeffe4 | 1005 | hwmon_device_unregister(data->hwmon_dev); |
b6fc1bac | 1006 | lm90_remove_files(client, data); |
943b0830 | 1007 | |
95238364 JD |
1008 | /* Restore initial configuration */ |
1009 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, | |
1010 | data->config_orig); | |
1011 | ||
943b0830 | 1012 | kfree(data); |
1da177e4 LT |
1013 | return 0; |
1014 | } | |
1015 | ||
53de3342 JD |
1016 | static void lm90_alert(struct i2c_client *client, unsigned int flag) |
1017 | { | |
1018 | struct lm90_data *data = i2c_get_clientdata(client); | |
1019 | u8 config, alarms; | |
1020 | ||
1021 | lm90_read_reg(client, LM90_REG_R_STATUS, &alarms); | |
1022 | if ((alarms & 0x7f) == 0) { | |
1023 | dev_info(&client->dev, "Everything OK\n"); | |
1024 | } else { | |
1025 | if (alarms & 0x61) | |
1026 | dev_warn(&client->dev, | |
1027 | "temp%d out of range, please check!\n", 1); | |
1028 | if (alarms & 0x1a) | |
1029 | dev_warn(&client->dev, | |
1030 | "temp%d out of range, please check!\n", 2); | |
1031 | if (alarms & 0x04) | |
1032 | dev_warn(&client->dev, | |
1033 | "temp%d diode open, please check!\n", 2); | |
1034 | ||
1035 | /* Disable ALERT# output, because these chips don't implement | |
1036 | SMBus alert correctly; they should only hold the alert line | |
1037 | low briefly. */ | |
1038 | if ((data->kind == adm1032 || data->kind == adt7461) | |
1039 | && (alarms & data->alert_alarms)) { | |
1040 | dev_dbg(&client->dev, "Disabling ALERT#\n"); | |
1041 | lm90_read_reg(client, LM90_REG_R_CONFIG1, &config); | |
1042 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, | |
1043 | config | 0x80); | |
1044 | } | |
1045 | } | |
1046 | } | |
1047 | ||
6388a388 JD |
1048 | static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value) |
1049 | { | |
1050 | int err; | |
1051 | u8 oldh, newh, l; | |
1052 | ||
1053 | /* | |
1054 | * There is a trick here. We have to read two registers to have the | |
1055 | * sensor temperature, but we have to beware a conversion could occur | |
1056 | * inbetween the readings. The datasheet says we should either use | |
1057 | * the one-shot conversion register, which we don't want to do | |
1058 | * (disables hardware monitoring) or monitor the busy bit, which is | |
1059 | * impossible (we can't read the values and monitor that bit at the | |
1060 | * exact same time). So the solution used here is to read the high | |
1061 | * byte once, then the low byte, then the high byte again. If the new | |
1062 | * high byte matches the old one, then we have a valid reading. Else | |
1063 | * we have to read the low byte again, and now we believe we have a | |
1064 | * correct reading. | |
1065 | */ | |
1066 | if ((err = lm90_read_reg(client, regh, &oldh)) | |
1067 | || (err = lm90_read_reg(client, regl, &l)) | |
1068 | || (err = lm90_read_reg(client, regh, &newh))) | |
1069 | return err; | |
1070 | if (oldh != newh) { | |
1071 | err = lm90_read_reg(client, regl, &l); | |
1072 | if (err) | |
1073 | return err; | |
1074 | } | |
1075 | *value = (newh << 8) | l; | |
1076 | ||
1077 | return 0; | |
1078 | } | |
1079 | ||
1da177e4 LT |
1080 | static struct lm90_data *lm90_update_device(struct device *dev) |
1081 | { | |
1082 | struct i2c_client *client = to_i2c_client(dev); | |
1083 | struct lm90_data *data = i2c_get_clientdata(client); | |
1084 | ||
9a61bf63 | 1085 | mutex_lock(&data->update_lock); |
1da177e4 | 1086 | |
8c3c7a25 IS |
1087 | if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10) |
1088 | || !data->valid) { | |
6388a388 | 1089 | u8 h, l; |
1da177e4 LT |
1090 | |
1091 | dev_dbg(&client->dev, "Updating lm90 data.\n"); | |
f65e1708 JD |
1092 | lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]); |
1093 | lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]); | |
1094 | lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]); | |
1095 | lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]); | |
8256fe0f | 1096 | lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst); |
1da177e4 | 1097 | |
88073bb1 | 1098 | if (data->flags & LM90_HAVE_LOCAL_EXT) { |
f65e1708 JD |
1099 | lm90_read16(client, LM90_REG_R_LOCAL_TEMP, |
1100 | MAX6657_REG_R_LOCAL_TEMPL, | |
1101 | &data->temp11[4]); | |
1102 | } else { | |
1103 | if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP, | |
1104 | &h) == 0) | |
1105 | data->temp11[4] = h << 8; | |
1106 | } | |
6388a388 JD |
1107 | lm90_read16(client, LM90_REG_R_REMOTE_TEMPH, |
1108 | LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]); | |
1109 | ||
5f502a83 JD |
1110 | if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) { |
1111 | data->temp11[1] = h << 8; | |
88073bb1 | 1112 | if ((data->flags & LM90_HAVE_REM_LIMIT_EXT) |
5f502a83 JD |
1113 | && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL, |
1114 | &l) == 0) | |
1115 | data->temp11[1] |= l; | |
1116 | } | |
1117 | if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) { | |
1118 | data->temp11[2] = h << 8; | |
88073bb1 | 1119 | if ((data->flags & LM90_HAVE_REM_LIMIT_EXT) |
5f502a83 JD |
1120 | && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL, |
1121 | &l) == 0) | |
1122 | data->temp11[2] |= l; | |
1123 | } | |
1124 | ||
88073bb1 | 1125 | if (data->flags & LM90_HAVE_OFFSET) { |
69f2f96d | 1126 | if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH, |
6388a388 | 1127 | &h) == 0 |
69f2f96d JD |
1128 | && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL, |
1129 | &l) == 0) | |
6388a388 | 1130 | data->temp11[3] = (h << 8) | l; |
69f2f96d | 1131 | } |
6948708d GR |
1132 | if (data->flags & LM90_HAVE_EMERGENCY) { |
1133 | lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG, | |
1134 | &data->temp8[4]); | |
1135 | lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG, | |
1136 | &data->temp8[5]); | |
1137 | } | |
8256fe0f | 1138 | lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms); |
1da177e4 | 1139 | |
53de3342 JD |
1140 | /* Re-enable ALERT# output if it was originally enabled and |
1141 | * relevant alarms are all clear */ | |
1142 | if ((data->config_orig & 0x80) == 0 | |
1143 | && (data->alarms & data->alert_alarms) == 0) { | |
1144 | u8 config; | |
1145 | ||
1146 | lm90_read_reg(client, LM90_REG_R_CONFIG1, &config); | |
1147 | if (config & 0x80) { | |
1148 | dev_dbg(&client->dev, "Re-enabling ALERT#\n"); | |
1149 | i2c_smbus_write_byte_data(client, | |
1150 | LM90_REG_W_CONFIG1, | |
1151 | config & ~0x80); | |
1152 | } | |
1153 | } | |
1154 | ||
1da177e4 LT |
1155 | data->last_updated = jiffies; |
1156 | data->valid = 1; | |
1157 | } | |
1158 | ||
9a61bf63 | 1159 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1160 | |
1161 | return data; | |
1162 | } | |
1163 | ||
1164 | static int __init sensors_lm90_init(void) | |
1165 | { | |
1166 | return i2c_add_driver(&lm90_driver); | |
1167 | } | |
1168 | ||
1169 | static void __exit sensors_lm90_exit(void) | |
1170 | { | |
1171 | i2c_del_driver(&lm90_driver); | |
1172 | } | |
1173 | ||
1174 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); | |
1175 | MODULE_DESCRIPTION("LM90/ADM1032 driver"); | |
1176 | MODULE_LICENSE("GPL"); | |
1177 | ||
1178 | module_init(sensors_lm90_init); | |
1179 | module_exit(sensors_lm90_exit); |