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