1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
5 * Copyright (C) 2003-2010 Jean Delvare <jdelvare@suse.de>
6 *
7 * Based on the lm83 driver. The LM90 is a sensor chip made by National
8 * Semiconductor. It reports up to two temperatures (its own plus up to
9 * one external one) with a 0.125 deg resolution (1 deg for local
10 * temperature) and a 3-4 deg accuracy.
11 *
12 * This driver also supports the LM89 and LM99, two other sensor chips
13 * made by National Semiconductor. Both have an increased remote
14 * temperature measurement accuracy (1 degree), and the LM99
15 * additionally shifts remote temperatures (measured and limits) by 16
16 * degrees, which allows for higher temperatures measurement.
17 * Note that there is no way to differentiate between both chips.
18 * When device is auto-detected, the driver will assume an LM99.
19 *
20 * This driver also supports the LM86, another sensor chip made by
21 * National Semiconductor. It is exactly similar to the LM90 except it
22 * has a higher accuracy.
23 *
24 * This driver also supports the ADM1032, a sensor chip made by Analog
25 * Devices. That chip is similar to the LM90, with a few differences
26 * that are not handled by this driver. Among others, it has a higher
27 * accuracy than the LM90, much like the LM86 does.
28 *
29 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
30 * chips made by Maxim. These chips are similar to the LM86.
31 * Note that there is no easy way to differentiate between the three
32 * variants. We use the device address to detect MAX6659, which will result
33 * in a detection as max6657 if it is on address 0x4c. The extra address
34 * and features of the MAX6659 are only supported if the chip is configured
35 * explicitly as max6659, or if its address is not 0x4c.
36 * These chips lack the remote temperature offset feature.
37 *
38 * This driver also supports the MAX6654 chip made by Maxim. This chip can be
39 * at 9 different addresses, similar to MAX6680/MAX6681. The MAX6654 is similar
40 * to MAX6657/MAX6658/MAX6659, but does not support critical temperature
41 * limits. Extended range is available by setting the configuration register
42 * accordingly, and is done during initialization. Extended precision is only
43 * available at conversion rates of 1 Hz and slower. Note that extended
44 * precision is not enabled by default, as this driver initializes all chips
45 * to 2 Hz by design.
46 *
47 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
48 * MAX6692 chips made by Maxim. These are again similar to the LM86,
49 * but they use unsigned temperature values and can report temperatures
50 * from 0 to 145 degrees.
51 *
52 * This driver also supports the MAX6680 and MAX6681, two other sensor
53 * chips made by Maxim. These are quite similar to the other Maxim
54 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
55 * be treated identically.
56 *
57 * This driver also supports the MAX6695 and MAX6696, two other sensor
58 * chips made by Maxim. These are also quite similar to other Maxim
59 * chips, but support three temperature sensors instead of two. MAX6695
60 * and MAX6696 only differ in the pinout so they can be treated identically.
61 *
62 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
63 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
64 * and extended mode. They are mostly compatible with LM90 except for a data
65 * format difference for the temperature value registers.
66 *
67 * This driver also supports the SA56004 from Philips. This device is
68 * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
69 *
70 * This driver also supports the G781 from GMT. This device is compatible
71 * with the ADM1032.
72 *
73 * This driver also supports TMP451 and TMP461 from Texas Instruments.
74 * Those devices are supported in both compatibility and extended mode.
75 * They are mostly compatible with ADT7461 except for local temperature
76 * low byte register and max conversion rate.
77 *
78 * Since the LM90 was the first chipset supported by this driver, most
79 * comments will refer to this chipset, but are actually general and
80 * concern all supported chipsets, unless mentioned otherwise.
81 */
82
83 #include <linux/module.h>
84 #include <linux/init.h>
85 #include <linux/slab.h>
86 #include <linux/jiffies.h>
87 #include <linux/i2c.h>
88 #include <linux/hwmon.h>
89 #include <linux/err.h>
90 #include <linux/mutex.h>
91 #include <linux/of_device.h>
92 #include <linux/sysfs.h>
93 #include <linux/interrupt.h>
94 #include <linux/regulator/consumer.h>
95
96 /*
97 * Addresses to scan
98 * Address is fully defined internally and cannot be changed except for
99 * MAX6659, MAX6680 and MAX6681.
100 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
101 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
102 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
103 * have address 0x4d.
104 * MAX6647 has address 0x4e.
105 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
106 * MAX6654, MAX6680, and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29,
107 * 0x2a, 0x2b, 0x4c, 0x4d or 0x4e.
108 * SA56004 can have address 0x48 through 0x4F.
109 */
110
111 static const unsigned short normal_i2c[] = {
112 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
113 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
114
115 enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
116 max6646, w83l771, max6696, sa56004, g781, tmp451, tmp461, max6654 };
117
118 /*
119 * The LM90 registers
120 */
121
122 #define LM90_REG_R_MAN_ID 0xFE
123 #define LM90_REG_R_CHIP_ID 0xFF
124 #define LM90_REG_R_CONFIG1 0x03
125 #define LM90_REG_W_CONFIG1 0x09
126 #define LM90_REG_R_CONFIG2 0xBF
127 #define LM90_REG_W_CONFIG2 0xBF
128 #define LM90_REG_R_CONVRATE 0x04
129 #define LM90_REG_W_CONVRATE 0x0A
130 #define LM90_REG_R_STATUS 0x02
131 #define LM90_REG_R_LOCAL_TEMP 0x00
132 #define LM90_REG_R_LOCAL_HIGH 0x05
133 #define LM90_REG_W_LOCAL_HIGH 0x0B
134 #define LM90_REG_R_LOCAL_LOW 0x06
135 #define LM90_REG_W_LOCAL_LOW 0x0C
136 #define LM90_REG_R_LOCAL_CRIT 0x20
137 #define LM90_REG_W_LOCAL_CRIT 0x20
138 #define LM90_REG_R_REMOTE_TEMPH 0x01
139 #define LM90_REG_R_REMOTE_TEMPL 0x10
140 #define LM90_REG_R_REMOTE_OFFSH 0x11
141 #define LM90_REG_W_REMOTE_OFFSH 0x11
142 #define LM90_REG_R_REMOTE_OFFSL 0x12
143 #define LM90_REG_W_REMOTE_OFFSL 0x12
144 #define LM90_REG_R_REMOTE_HIGHH 0x07
145 #define LM90_REG_W_REMOTE_HIGHH 0x0D
146 #define LM90_REG_R_REMOTE_HIGHL 0x13
147 #define LM90_REG_W_REMOTE_HIGHL 0x13
148 #define LM90_REG_R_REMOTE_LOWH 0x08
149 #define LM90_REG_W_REMOTE_LOWH 0x0E
150 #define LM90_REG_R_REMOTE_LOWL 0x14
151 #define LM90_REG_W_REMOTE_LOWL 0x14
152 #define LM90_REG_R_REMOTE_CRIT 0x19
153 #define LM90_REG_W_REMOTE_CRIT 0x19
154 #define LM90_REG_R_TCRIT_HYST 0x21
155 #define LM90_REG_W_TCRIT_HYST 0x21
156
157 /* MAX6646/6647/6649/6654/6657/6658/6659/6695/6696 registers */
158
159 #define MAX6657_REG_R_LOCAL_TEMPL 0x11
160 #define MAX6696_REG_R_STATUS2 0x12
161 #define MAX6659_REG_R_REMOTE_EMERG 0x16
162 #define MAX6659_REG_W_REMOTE_EMERG 0x16
163 #define MAX6659_REG_R_LOCAL_EMERG 0x17
164 #define MAX6659_REG_W_LOCAL_EMERG 0x17
165
166 /* SA56004 registers */
167
168 #define SA56004_REG_R_LOCAL_TEMPL 0x22
169
170 #define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
171
172 /* TMP451/TMP461 registers */
173 #define TMP451_REG_R_LOCAL_TEMPL 0x15
174 #define TMP451_REG_CONALERT 0x22
175
176 #define TMP461_REG_CHEN 0x16
177 #define TMP461_REG_DFC 0x24
178
179 /*
180 * Device flags
181 */
182 #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
183 /* Device features */
184 #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
185 #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
186 #define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
187 #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
188 #define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
189 #define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
190 #define LM90_HAVE_EXTENDED_TEMP (1 << 8) /* extended temperature support*/
191 #define LM90_PAUSE_FOR_CONFIG (1 << 9) /* Pause conversion for config */
192 #define LM90_HAVE_CRIT (1 << 10)/* Chip supports CRIT/OVERT register */
193 #define LM90_HAVE_CRIT_ALRM_SWP (1 << 11)/* critical alarm bits swapped */
194
195 /* LM90 status */
196 #define LM90_STATUS_LTHRM (1 << 0) /* local THERM limit tripped */
197 #define LM90_STATUS_RTHRM (1 << 1) /* remote THERM limit tripped */
198 #define LM90_STATUS_ROPEN (1 << 2) /* remote is an open circuit */
199 #define LM90_STATUS_RLOW (1 << 3) /* remote low temp limit tripped */
200 #define LM90_STATUS_RHIGH (1 << 4) /* remote high temp limit tripped */
201 #define LM90_STATUS_LLOW (1 << 5) /* local low temp limit tripped */
202 #define LM90_STATUS_LHIGH (1 << 6) /* local high temp limit tripped */
203 #define LM90_STATUS_BUSY (1 << 7) /* conversion is ongoing */
204
205 #define MAX6696_STATUS2_R2THRM (1 << 1) /* remote2 THERM limit tripped */
206 #define MAX6696_STATUS2_R2OPEN (1 << 2) /* remote2 is an open circuit */
207 #define MAX6696_STATUS2_R2LOW (1 << 3) /* remote2 low temp limit tripped */
208 #define MAX6696_STATUS2_R2HIGH (1 << 4) /* remote2 high temp limit tripped */
209 #define MAX6696_STATUS2_ROT2 (1 << 5) /* remote emergency limit tripped */
210 #define MAX6696_STATUS2_R2OT2 (1 << 6) /* remote2 emergency limit tripped */
211 #define MAX6696_STATUS2_LOT2 (1 << 7) /* local emergency limit tripped */
212
213 /*
214 * Driver data (common to all clients)
215 */
216
217 static const struct i2c_device_id lm90_id[] = {
218 { "adm1032", adm1032 },
219 { "adt7461", adt7461 },
220 { "adt7461a", adt7461 },
221 { "g781", g781 },
222 { "lm90", lm90 },
223 { "lm86", lm86 },
224 { "lm89", lm86 },
225 { "lm99", lm99 },
226 { "max6646", max6646 },
227 { "max6647", max6646 },
228 { "max6649", max6646 },
229 { "max6654", max6654 },
230 { "max6657", max6657 },
231 { "max6658", max6657 },
232 { "max6659", max6659 },
233 { "max6680", max6680 },
234 { "max6681", max6680 },
235 { "max6695", max6696 },
236 { "max6696", max6696 },
237 { "nct1008", adt7461 },
238 { "w83l771", w83l771 },
239 { "sa56004", sa56004 },
240 { "tmp451", tmp451 },
241 { "tmp461", tmp461 },
242 { }
243 };
244 MODULE_DEVICE_TABLE(i2c, lm90_id);
245
246 static const struct of_device_id __maybe_unused lm90_of_match[] = {
247 {
248 .compatible = "adi,adm1032",
249 .data = (void *)adm1032
250 },
251 {
252 .compatible = "adi,adt7461",
253 .data = (void *)adt7461
254 },
255 {
256 .compatible = "adi,adt7461a",
257 .data = (void *)adt7461
258 },
259 {
260 .compatible = "gmt,g781",
261 .data = (void *)g781
262 },
263 {
264 .compatible = "national,lm90",
265 .data = (void *)lm90
266 },
267 {
268 .compatible = "national,lm86",
269 .data = (void *)lm86
270 },
271 {
272 .compatible = "national,lm89",
273 .data = (void *)lm86
274 },
275 {
276 .compatible = "national,lm99",
277 .data = (void *)lm99
278 },
279 {
280 .compatible = "dallas,max6646",
281 .data = (void *)max6646
282 },
283 {
284 .compatible = "dallas,max6647",
285 .data = (void *)max6646
286 },
287 {
288 .compatible = "dallas,max6649",
289 .data = (void *)max6646
290 },
291 {
292 .compatible = "dallas,max6654",
293 .data = (void *)max6654
294 },
295 {
296 .compatible = "dallas,max6657",
297 .data = (void *)max6657
298 },
299 {
300 .compatible = "dallas,max6658",
301 .data = (void *)max6657
302 },
303 {
304 .compatible = "dallas,max6659",
305 .data = (void *)max6659
306 },
307 {
308 .compatible = "dallas,max6680",
309 .data = (void *)max6680
310 },
311 {
312 .compatible = "dallas,max6681",
313 .data = (void *)max6680
314 },
315 {
316 .compatible = "dallas,max6695",
317 .data = (void *)max6696
318 },
319 {
320 .compatible = "dallas,max6696",
321 .data = (void *)max6696
322 },
323 {
324 .compatible = "onnn,nct1008",
325 .data = (void *)adt7461
326 },
327 {
328 .compatible = "winbond,w83l771",
329 .data = (void *)w83l771
330 },
331 {
332 .compatible = "nxp,sa56004",
333 .data = (void *)sa56004
334 },
335 {
336 .compatible = "ti,tmp451",
337 .data = (void *)tmp451
338 },
339 {
340 .compatible = "ti,tmp461",
341 .data = (void *)tmp461
342 },
343 { },
344 };
345 MODULE_DEVICE_TABLE(of, lm90_of_match);
346
347 /*
348 * chip type specific parameters
349 */
350 struct lm90_params {
351 u32 flags; /* Capabilities */
352 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
353 /* Upper 8 bits for max6695/96 */
354 u8 max_convrate; /* Maximum conversion rate register value */
355 u8 reg_local_ext; /* Extended local temp register (optional) */
356 };
357
358 static const struct lm90_params lm90_params[] = {
359 [adm1032] = {
360 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
361 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT,
362 .alert_alarms = 0x7c,
363 .max_convrate = 10,
364 },
365 [adt7461] = {
366 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
367 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
368 | LM90_HAVE_CRIT,
369 .alert_alarms = 0x7c,
370 .max_convrate = 10,
371 },
372 [g781] = {
373 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
374 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT,
375 .alert_alarms = 0x7c,
376 .max_convrate = 7,
377 },
378 [lm86] = {
379 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
380 | LM90_HAVE_CRIT,
381 .alert_alarms = 0x7b,
382 .max_convrate = 9,
383 },
384 [lm90] = {
385 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
386 | LM90_HAVE_CRIT,
387 .alert_alarms = 0x7b,
388 .max_convrate = 9,
389 },
390 [lm99] = {
391 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
392 | LM90_HAVE_CRIT,
393 .alert_alarms = 0x7b,
394 .max_convrate = 9,
395 },
396 [max6646] = {
397 .flags = LM90_HAVE_CRIT | LM90_HAVE_BROKEN_ALERT,
398 .alert_alarms = 0x7c,
399 .max_convrate = 6,
400 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
401 },
402 [max6654] = {
403 .flags = LM90_HAVE_BROKEN_ALERT,
404 .alert_alarms = 0x7c,
405 .max_convrate = 7,
406 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
407 },
408 [max6657] = {
409 .flags = LM90_PAUSE_FOR_CONFIG | LM90_HAVE_CRIT,
410 .alert_alarms = 0x7c,
411 .max_convrate = 8,
412 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
413 },
414 [max6659] = {
415 .flags = LM90_HAVE_EMERGENCY | LM90_HAVE_CRIT,
416 .alert_alarms = 0x7c,
417 .max_convrate = 8,
418 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
419 },
420 [max6680] = {
421 .flags = LM90_HAVE_OFFSET | LM90_HAVE_CRIT
422 | LM90_HAVE_CRIT_ALRM_SWP | LM90_HAVE_BROKEN_ALERT,
423 .alert_alarms = 0x7c,
424 .max_convrate = 7,
425 },
426 [max6696] = {
427 .flags = LM90_HAVE_EMERGENCY
428 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3 | LM90_HAVE_CRIT,
429 .alert_alarms = 0x1c7c,
430 .max_convrate = 6,
431 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
432 },
433 [w83l771] = {
434 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT,
435 .alert_alarms = 0x7c,
436 .max_convrate = 8,
437 },
438 [sa56004] = {
439 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT,
440 .alert_alarms = 0x7b,
441 .max_convrate = 9,
442 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
443 },
444 [tmp451] = {
445 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
446 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT,
447 .alert_alarms = 0x7c,
448 .max_convrate = 9,
449 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
450 },
451 [tmp461] = {
452 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
453 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT,
454 .alert_alarms = 0x7c,
455 .max_convrate = 9,
456 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
457 },
458 };
459
460 /*
461 * TEMP8 register index
462 */
463 enum lm90_temp8_reg_index {
464 LOCAL_LOW = 0,
465 LOCAL_HIGH,
466 LOCAL_CRIT,
467 REMOTE_CRIT,
468 LOCAL_EMERG, /* max6659 and max6695/96 */
469 REMOTE_EMERG, /* max6659 and max6695/96 */
470 REMOTE2_CRIT, /* max6695/96 only */
471 REMOTE2_EMERG, /* max6695/96 only */
472 TEMP8_REG_NUM
473 };
474
475 /*
476 * TEMP11 register index
477 */
478 enum lm90_temp11_reg_index {
479 REMOTE_TEMP = 0,
480 REMOTE_LOW,
481 REMOTE_HIGH,
482 REMOTE_OFFSET, /* except max6646, max6657/58/59, and max6695/96 */
483 LOCAL_TEMP,
484 REMOTE2_TEMP, /* max6695/96 only */
485 REMOTE2_LOW, /* max6695/96 only */
486 REMOTE2_HIGH, /* max6695/96 only */
487 TEMP11_REG_NUM
488 };
489
490 /*
491 * Client data (each client gets its own)
492 */
493
494 struct lm90_data {
495 struct i2c_client *client;
496 u32 channel_config[4];
497 struct hwmon_channel_info temp_info;
498 const struct hwmon_channel_info *info[3];
499 struct hwmon_chip_info chip;
500 struct mutex update_lock;
501 bool valid; /* true if register values are valid */
502 unsigned long last_updated; /* in jiffies */
503 int kind;
504 u32 flags;
505
506 unsigned int update_interval; /* in milliseconds */
507
508 u8 config; /* Current configuration register value */
509 u8 config_orig; /* Original configuration register value */
510 u8 convrate_orig; /* Original conversion rate register value */
511 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
512 /* Upper 8 bits for max6695/96 */
513 u8 max_convrate; /* Maximum conversion rate */
514 u8 reg_local_ext; /* local extension register offset */
515
516 /* registers values */
517 s8 temp8[TEMP8_REG_NUM];
518 s16 temp11[TEMP11_REG_NUM];
519 u8 temp_hyst;
520 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
521 };
522
523 /*
524 * Support functions
525 */
526
527 /*
528 * The ADM1032 supports PEC but not on write byte transactions, so we need
529 * to explicitly ask for a transaction without PEC.
530 */
adm1032_write_byte(struct i2c_client * client,u8 value)531 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
532 {
533 return i2c_smbus_xfer(client->adapter, client->addr,
534 client->flags & ~I2C_CLIENT_PEC,
535 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
536 }
537
538 /*
539 * It is assumed that client->update_lock is held (unless we are in
540 * detection or initialization steps). This matters when PEC is enabled,
541 * because we don't want the address pointer to change between the write
542 * byte and the read byte transactions.
543 */
lm90_read_reg(struct i2c_client * client,u8 reg)544 static int lm90_read_reg(struct i2c_client *client, u8 reg)
545 {
546 int err;
547
548 if (client->flags & I2C_CLIENT_PEC) {
549 err = adm1032_write_byte(client, reg);
550 if (err >= 0)
551 err = i2c_smbus_read_byte(client);
552 } else
553 err = i2c_smbus_read_byte_data(client, reg);
554
555 return err;
556 }
557
lm90_read16(struct i2c_client * client,u8 regh,u8 regl)558 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
559 {
560 int oldh, newh, l;
561
562 /*
563 * There is a trick here. We have to read two registers to have the
564 * sensor temperature, but we have to beware a conversion could occur
565 * between the readings. The datasheet says we should either use
566 * the one-shot conversion register, which we don't want to do
567 * (disables hardware monitoring) or monitor the busy bit, which is
568 * impossible (we can't read the values and monitor that bit at the
569 * exact same time). So the solution used here is to read the high
570 * byte once, then the low byte, then the high byte again. If the new
571 * high byte matches the old one, then we have a valid reading. Else
572 * we have to read the low byte again, and now we believe we have a
573 * correct reading.
574 */
575 oldh = lm90_read_reg(client, regh);
576 if (oldh < 0)
577 return oldh;
578 l = lm90_read_reg(client, regl);
579 if (l < 0)
580 return l;
581 newh = lm90_read_reg(client, regh);
582 if (newh < 0)
583 return newh;
584 if (oldh != newh) {
585 l = lm90_read_reg(client, regl);
586 if (l < 0)
587 return l;
588 }
589 return (newh << 8) | l;
590 }
591
lm90_update_confreg(struct lm90_data * data,u8 config)592 static int lm90_update_confreg(struct lm90_data *data, u8 config)
593 {
594 if (data->config != config) {
595 int err;
596
597 err = i2c_smbus_write_byte_data(data->client,
598 LM90_REG_W_CONFIG1,
599 config);
600 if (err)
601 return err;
602 data->config = config;
603 }
604 return 0;
605 }
606
607 /*
608 * client->update_lock must be held when calling this function (unless we are
609 * in detection or initialization steps), and while a remote channel other
610 * than channel 0 is selected. Also, calling code must make sure to re-select
611 * external channel 0 before releasing the lock. This is necessary because
612 * various registers have different meanings as a result of selecting a
613 * non-default remote channel.
614 */
lm90_select_remote_channel(struct lm90_data * data,int channel)615 static int lm90_select_remote_channel(struct lm90_data *data, int channel)
616 {
617 int err = 0;
618
619 if (data->kind == max6696) {
620 u8 config = data->config & ~0x08;
621
622 if (channel)
623 config |= 0x08;
624 err = lm90_update_confreg(data, config);
625 }
626 return err;
627 }
628
lm90_write_convrate(struct lm90_data * data,int val)629 static int lm90_write_convrate(struct lm90_data *data, int val)
630 {
631 u8 config = data->config;
632 int err;
633
634 /* Save config and pause conversion */
635 if (data->flags & LM90_PAUSE_FOR_CONFIG) {
636 err = lm90_update_confreg(data, config | 0x40);
637 if (err < 0)
638 return err;
639 }
640
641 /* Set conv rate */
642 err = i2c_smbus_write_byte_data(data->client, LM90_REG_W_CONVRATE, val);
643
644 /* Revert change to config */
645 lm90_update_confreg(data, config);
646
647 return err;
648 }
649
650 /*
651 * Set conversion rate.
652 * client->update_lock must be held when calling this function (unless we are
653 * in detection or initialization steps).
654 */
lm90_set_convrate(struct i2c_client * client,struct lm90_data * data,unsigned int interval)655 static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
656 unsigned int interval)
657 {
658 unsigned int update_interval;
659 int i, err;
660
661 /* Shift calculations to avoid rounding errors */
662 interval <<= 6;
663
664 /* find the nearest update rate */
665 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
666 i < data->max_convrate; i++, update_interval >>= 1)
667 if (interval >= update_interval * 3 / 4)
668 break;
669
670 err = lm90_write_convrate(data, i);
671 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
672 return err;
673 }
674
lm90_update_limits(struct device * dev)675 static int lm90_update_limits(struct device *dev)
676 {
677 struct lm90_data *data = dev_get_drvdata(dev);
678 struct i2c_client *client = data->client;
679 int val;
680
681 if (data->flags & LM90_HAVE_CRIT) {
682 val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
683 if (val < 0)
684 return val;
685 data->temp8[LOCAL_CRIT] = val;
686
687 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
688 if (val < 0)
689 return val;
690 data->temp8[REMOTE_CRIT] = val;
691
692 val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
693 if (val < 0)
694 return val;
695 data->temp_hyst = val;
696 }
697
698 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
699 if (val < 0)
700 return val;
701 data->temp11[REMOTE_LOW] = val << 8;
702
703 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
704 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
705 if (val < 0)
706 return val;
707 data->temp11[REMOTE_LOW] |= val;
708 }
709
710 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
711 if (val < 0)
712 return val;
713 data->temp11[REMOTE_HIGH] = val << 8;
714
715 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
716 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
717 if (val < 0)
718 return val;
719 data->temp11[REMOTE_HIGH] |= val;
720 }
721
722 if (data->flags & LM90_HAVE_OFFSET) {
723 val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
724 LM90_REG_R_REMOTE_OFFSL);
725 if (val < 0)
726 return val;
727 data->temp11[REMOTE_OFFSET] = val;
728 }
729
730 if (data->flags & LM90_HAVE_EMERGENCY) {
731 val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
732 if (val < 0)
733 return val;
734 data->temp8[LOCAL_EMERG] = val;
735
736 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
737 if (val < 0)
738 return val;
739 data->temp8[REMOTE_EMERG] = val;
740 }
741
742 if (data->kind == max6696) {
743 val = lm90_select_remote_channel(data, 1);
744 if (val < 0)
745 return val;
746
747 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
748 if (val < 0)
749 return val;
750 data->temp8[REMOTE2_CRIT] = val;
751
752 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
753 if (val < 0)
754 return val;
755 data->temp8[REMOTE2_EMERG] = val;
756
757 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
758 if (val < 0)
759 return val;
760 data->temp11[REMOTE2_LOW] = val << 8;
761
762 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
763 if (val < 0)
764 return val;
765 data->temp11[REMOTE2_HIGH] = val << 8;
766
767 lm90_select_remote_channel(data, 0);
768 }
769
770 return 0;
771 }
772
lm90_update_device(struct device * dev)773 static int lm90_update_device(struct device *dev)
774 {
775 struct lm90_data *data = dev_get_drvdata(dev);
776 struct i2c_client *client = data->client;
777 unsigned long next_update;
778 int val;
779
780 if (!data->valid) {
781 val = lm90_update_limits(dev);
782 if (val < 0)
783 return val;
784 }
785
786 next_update = data->last_updated +
787 msecs_to_jiffies(data->update_interval);
788 if (time_after(jiffies, next_update) || !data->valid) {
789 dev_dbg(&client->dev, "Updating lm90 data.\n");
790
791 data->valid = false;
792
793 val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
794 if (val < 0)
795 return val;
796 data->temp8[LOCAL_LOW] = val;
797
798 val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
799 if (val < 0)
800 return val;
801 data->temp8[LOCAL_HIGH] = val;
802
803 if (data->reg_local_ext) {
804 val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
805 data->reg_local_ext);
806 if (val < 0)
807 return val;
808 data->temp11[LOCAL_TEMP] = val;
809 } else {
810 val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
811 if (val < 0)
812 return val;
813 data->temp11[LOCAL_TEMP] = val << 8;
814 }
815 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
816 LM90_REG_R_REMOTE_TEMPL);
817 if (val < 0)
818 return val;
819 data->temp11[REMOTE_TEMP] = val;
820
821 val = lm90_read_reg(client, LM90_REG_R_STATUS);
822 if (val < 0)
823 return val;
824 data->alarms = val & ~LM90_STATUS_BUSY;
825
826 if (data->kind == max6696) {
827 val = lm90_select_remote_channel(data, 1);
828 if (val < 0)
829 return val;
830
831 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
832 LM90_REG_R_REMOTE_TEMPL);
833 if (val < 0) {
834 lm90_select_remote_channel(data, 0);
835 return val;
836 }
837 data->temp11[REMOTE2_TEMP] = val;
838
839 lm90_select_remote_channel(data, 0);
840
841 val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
842 if (val < 0)
843 return val;
844 data->alarms |= val << 8;
845 }
846
847 /*
848 * Re-enable ALERT# output if it was originally enabled and
849 * relevant alarms are all clear
850 */
851 if (!(data->config_orig & 0x80) &&
852 !(data->alarms & data->alert_alarms)) {
853 if (data->config & 0x80) {
854 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
855 lm90_update_confreg(data, data->config & ~0x80);
856 }
857 }
858
859 data->last_updated = jiffies;
860 data->valid = true;
861 }
862
863 return 0;
864 }
865
866 /*
867 * Conversions
868 * For local temperatures and limits, critical limits and the hysteresis
869 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
870 * For remote temperatures and limits, it uses signed 11-bit values with
871 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
872 * Maxim chips use unsigned values.
873 */
874
temp_from_s8(s8 val)875 static inline int temp_from_s8(s8 val)
876 {
877 return val * 1000;
878 }
879
temp_from_u8(u8 val)880 static inline int temp_from_u8(u8 val)
881 {
882 return val * 1000;
883 }
884
temp_from_s16(s16 val)885 static inline int temp_from_s16(s16 val)
886 {
887 return val / 32 * 125;
888 }
889
temp_from_u16(u16 val)890 static inline int temp_from_u16(u16 val)
891 {
892 return val / 32 * 125;
893 }
894
temp_to_s8(long val)895 static s8 temp_to_s8(long val)
896 {
897 if (val <= -128000)
898 return -128;
899 if (val >= 127000)
900 return 127;
901 if (val < 0)
902 return (val - 500) / 1000;
903 return (val + 500) / 1000;
904 }
905
temp_to_u8(long val)906 static u8 temp_to_u8(long val)
907 {
908 if (val <= 0)
909 return 0;
910 if (val >= 255000)
911 return 255;
912 return (val + 500) / 1000;
913 }
914
temp_to_s16(long val)915 static s16 temp_to_s16(long val)
916 {
917 if (val <= -128000)
918 return 0x8000;
919 if (val >= 127875)
920 return 0x7FE0;
921 if (val < 0)
922 return (val - 62) / 125 * 32;
923 return (val + 62) / 125 * 32;
924 }
925
hyst_to_reg(long val)926 static u8 hyst_to_reg(long val)
927 {
928 if (val <= 0)
929 return 0;
930 if (val >= 30500)
931 return 31;
932 return (val + 500) / 1000;
933 }
934
935 /*
936 * ADT7461 in compatibility mode is almost identical to LM90 except that
937 * attempts to write values that are outside the range 0 < temp < 127 are
938 * treated as the boundary value.
939 *
940 * ADT7461 in "extended mode" operation uses unsigned integers offset by
941 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
942 */
temp_from_u8_adt7461(struct lm90_data * data,u8 val)943 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
944 {
945 if (data->flags & LM90_FLAG_ADT7461_EXT)
946 return (val - 64) * 1000;
947 return temp_from_s8(val);
948 }
949
temp_from_u16_adt7461(struct lm90_data * data,u16 val)950 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
951 {
952 if (data->flags & LM90_FLAG_ADT7461_EXT)
953 return (val - 0x4000) / 64 * 250;
954 return temp_from_s16(val);
955 }
956
temp_to_u8_adt7461(struct lm90_data * data,long val)957 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
958 {
959 if (data->flags & LM90_FLAG_ADT7461_EXT) {
960 if (val <= -64000)
961 return 0;
962 if (val >= 191000)
963 return 0xFF;
964 return (val + 500 + 64000) / 1000;
965 }
966 if (val <= 0)
967 return 0;
968 if (val >= 127000)
969 return 127;
970 return (val + 500) / 1000;
971 }
972
temp_to_u16_adt7461(struct lm90_data * data,long val)973 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
974 {
975 if (data->flags & LM90_FLAG_ADT7461_EXT) {
976 if (val <= -64000)
977 return 0;
978 if (val >= 191750)
979 return 0xFFC0;
980 return (val + 64000 + 125) / 250 * 64;
981 }
982 if (val <= 0)
983 return 0;
984 if (val >= 127750)
985 return 0x7FC0;
986 return (val + 125) / 250 * 64;
987 }
988
989 /* pec used for ADM1032 only */
pec_show(struct device * dev,struct device_attribute * dummy,char * buf)990 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
991 char *buf)
992 {
993 struct i2c_client *client = to_i2c_client(dev);
994
995 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
996 }
997
pec_store(struct device * dev,struct device_attribute * dummy,const char * buf,size_t count)998 static ssize_t pec_store(struct device *dev, struct device_attribute *dummy,
999 const char *buf, size_t count)
1000 {
1001 struct i2c_client *client = to_i2c_client(dev);
1002 long val;
1003 int err;
1004
1005 err = kstrtol(buf, 10, &val);
1006 if (err < 0)
1007 return err;
1008
1009 switch (val) {
1010 case 0:
1011 client->flags &= ~I2C_CLIENT_PEC;
1012 break;
1013 case 1:
1014 client->flags |= I2C_CLIENT_PEC;
1015 break;
1016 default:
1017 return -EINVAL;
1018 }
1019
1020 return count;
1021 }
1022
1023 static DEVICE_ATTR_RW(pec);
1024
lm90_get_temp11(struct lm90_data * data,int index)1025 static int lm90_get_temp11(struct lm90_data *data, int index)
1026 {
1027 s16 temp11 = data->temp11[index];
1028 int temp;
1029
1030 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1031 temp = temp_from_u16_adt7461(data, temp11);
1032 else if (data->kind == max6646)
1033 temp = temp_from_u16(temp11);
1034 else
1035 temp = temp_from_s16(temp11);
1036
1037 /* +16 degrees offset for temp2 for the LM99 */
1038 if (data->kind == lm99 && index <= 2)
1039 temp += 16000;
1040
1041 return temp;
1042 }
1043
lm90_set_temp11(struct lm90_data * data,int index,long val)1044 static int lm90_set_temp11(struct lm90_data *data, int index, long val)
1045 {
1046 static struct reg {
1047 u8 high;
1048 u8 low;
1049 } reg[] = {
1050 [REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1051 [REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
1052 [REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
1053 [REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1054 [REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
1055 };
1056 struct i2c_client *client = data->client;
1057 struct reg *regp = ®[index];
1058 int err;
1059
1060 /* +16 degrees offset for temp2 for the LM99 */
1061 if (data->kind == lm99 && index <= 2)
1062 val -= 16000;
1063
1064 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1065 data->temp11[index] = temp_to_u16_adt7461(data, val);
1066 else if (data->kind == max6646)
1067 data->temp11[index] = temp_to_u8(val) << 8;
1068 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1069 data->temp11[index] = temp_to_s16(val);
1070 else
1071 data->temp11[index] = temp_to_s8(val) << 8;
1072
1073 lm90_select_remote_channel(data, index >= 3);
1074 err = i2c_smbus_write_byte_data(client, regp->high,
1075 data->temp11[index] >> 8);
1076 if (err < 0)
1077 return err;
1078 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1079 err = i2c_smbus_write_byte_data(client, regp->low,
1080 data->temp11[index] & 0xff);
1081
1082 lm90_select_remote_channel(data, 0);
1083 return err;
1084 }
1085
lm90_get_temp8(struct lm90_data * data,int index)1086 static int lm90_get_temp8(struct lm90_data *data, int index)
1087 {
1088 s8 temp8 = data->temp8[index];
1089 int temp;
1090
1091 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1092 temp = temp_from_u8_adt7461(data, temp8);
1093 else if (data->kind == max6646)
1094 temp = temp_from_u8(temp8);
1095 else
1096 temp = temp_from_s8(temp8);
1097
1098 /* +16 degrees offset for temp2 for the LM99 */
1099 if (data->kind == lm99 && index == 3)
1100 temp += 16000;
1101
1102 return temp;
1103 }
1104
lm90_set_temp8(struct lm90_data * data,int index,long val)1105 static int lm90_set_temp8(struct lm90_data *data, int index, long val)
1106 {
1107 static const u8 reg[TEMP8_REG_NUM] = {
1108 LM90_REG_W_LOCAL_LOW,
1109 LM90_REG_W_LOCAL_HIGH,
1110 LM90_REG_W_LOCAL_CRIT,
1111 LM90_REG_W_REMOTE_CRIT,
1112 MAX6659_REG_W_LOCAL_EMERG,
1113 MAX6659_REG_W_REMOTE_EMERG,
1114 LM90_REG_W_REMOTE_CRIT,
1115 MAX6659_REG_W_REMOTE_EMERG,
1116 };
1117 struct i2c_client *client = data->client;
1118 int err;
1119
1120 /* +16 degrees offset for temp2 for the LM99 */
1121 if (data->kind == lm99 && index == 3)
1122 val -= 16000;
1123
1124 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1125 data->temp8[index] = temp_to_u8_adt7461(data, val);
1126 else if (data->kind == max6646)
1127 data->temp8[index] = temp_to_u8(val);
1128 else
1129 data->temp8[index] = temp_to_s8(val);
1130
1131 lm90_select_remote_channel(data, index >= 6);
1132 err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
1133 lm90_select_remote_channel(data, 0);
1134
1135 return err;
1136 }
1137
lm90_get_temphyst(struct lm90_data * data,int index)1138 static int lm90_get_temphyst(struct lm90_data *data, int index)
1139 {
1140 int temp;
1141
1142 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1143 temp = temp_from_u8_adt7461(data, data->temp8[index]);
1144 else if (data->kind == max6646)
1145 temp = temp_from_u8(data->temp8[index]);
1146 else
1147 temp = temp_from_s8(data->temp8[index]);
1148
1149 /* +16 degrees offset for temp2 for the LM99 */
1150 if (data->kind == lm99 && index == 3)
1151 temp += 16000;
1152
1153 return temp - temp_from_s8(data->temp_hyst);
1154 }
1155
lm90_set_temphyst(struct lm90_data * data,long val)1156 static int lm90_set_temphyst(struct lm90_data *data, long val)
1157 {
1158 struct i2c_client *client = data->client;
1159 int temp;
1160 int err;
1161
1162 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1163 temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1164 else if (data->kind == max6646)
1165 temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1166 else
1167 temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1168
1169 data->temp_hyst = hyst_to_reg(temp - val);
1170 err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1171 data->temp_hyst);
1172 return err;
1173 }
1174
1175 static const u8 lm90_temp_index[3] = {
1176 LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1177 };
1178
1179 static const u8 lm90_temp_min_index[3] = {
1180 LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1181 };
1182
1183 static const u8 lm90_temp_max_index[3] = {
1184 LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1185 };
1186
1187 static const u8 lm90_temp_crit_index[3] = {
1188 LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1189 };
1190
1191 static const u8 lm90_temp_emerg_index[3] = {
1192 LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1193 };
1194
1195 static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
1196 static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
1197 static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1198 static const u8 lm90_crit_alarm_bits_swapped[3] = { 1, 0, 9 };
1199 static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1200 static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1201
lm90_temp_read(struct device * dev,u32 attr,int channel,long * val)1202 static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1203 {
1204 struct lm90_data *data = dev_get_drvdata(dev);
1205 int err;
1206
1207 mutex_lock(&data->update_lock);
1208 err = lm90_update_device(dev);
1209 mutex_unlock(&data->update_lock);
1210 if (err)
1211 return err;
1212
1213 switch (attr) {
1214 case hwmon_temp_input:
1215 *val = lm90_get_temp11(data, lm90_temp_index[channel]);
1216 break;
1217 case hwmon_temp_min_alarm:
1218 *val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1219 break;
1220 case hwmon_temp_max_alarm:
1221 *val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1222 break;
1223 case hwmon_temp_crit_alarm:
1224 if (data->flags & LM90_HAVE_CRIT_ALRM_SWP)
1225 *val = (data->alarms >> lm90_crit_alarm_bits_swapped[channel]) & 1;
1226 else
1227 *val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1228 break;
1229 case hwmon_temp_emergency_alarm:
1230 *val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1231 break;
1232 case hwmon_temp_fault:
1233 *val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1234 break;
1235 case hwmon_temp_min:
1236 if (channel == 0)
1237 *val = lm90_get_temp8(data,
1238 lm90_temp_min_index[channel]);
1239 else
1240 *val = lm90_get_temp11(data,
1241 lm90_temp_min_index[channel]);
1242 break;
1243 case hwmon_temp_max:
1244 if (channel == 0)
1245 *val = lm90_get_temp8(data,
1246 lm90_temp_max_index[channel]);
1247 else
1248 *val = lm90_get_temp11(data,
1249 lm90_temp_max_index[channel]);
1250 break;
1251 case hwmon_temp_crit:
1252 *val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1253 break;
1254 case hwmon_temp_crit_hyst:
1255 *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1256 break;
1257 case hwmon_temp_emergency:
1258 *val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1259 break;
1260 case hwmon_temp_emergency_hyst:
1261 *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1262 break;
1263 case hwmon_temp_offset:
1264 *val = lm90_get_temp11(data, REMOTE_OFFSET);
1265 break;
1266 default:
1267 return -EOPNOTSUPP;
1268 }
1269 return 0;
1270 }
1271
lm90_temp_write(struct device * dev,u32 attr,int channel,long val)1272 static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1273 {
1274 struct lm90_data *data = dev_get_drvdata(dev);
1275 int err;
1276
1277 mutex_lock(&data->update_lock);
1278
1279 err = lm90_update_device(dev);
1280 if (err)
1281 goto error;
1282
1283 switch (attr) {
1284 case hwmon_temp_min:
1285 if (channel == 0)
1286 err = lm90_set_temp8(data,
1287 lm90_temp_min_index[channel],
1288 val);
1289 else
1290 err = lm90_set_temp11(data,
1291 lm90_temp_min_index[channel],
1292 val);
1293 break;
1294 case hwmon_temp_max:
1295 if (channel == 0)
1296 err = lm90_set_temp8(data,
1297 lm90_temp_max_index[channel],
1298 val);
1299 else
1300 err = lm90_set_temp11(data,
1301 lm90_temp_max_index[channel],
1302 val);
1303 break;
1304 case hwmon_temp_crit:
1305 err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1306 break;
1307 case hwmon_temp_crit_hyst:
1308 err = lm90_set_temphyst(data, val);
1309 break;
1310 case hwmon_temp_emergency:
1311 err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1312 break;
1313 case hwmon_temp_offset:
1314 err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1315 break;
1316 default:
1317 err = -EOPNOTSUPP;
1318 break;
1319 }
1320 error:
1321 mutex_unlock(&data->update_lock);
1322
1323 return err;
1324 }
1325
lm90_temp_is_visible(const void * data,u32 attr,int channel)1326 static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1327 {
1328 switch (attr) {
1329 case hwmon_temp_input:
1330 case hwmon_temp_min_alarm:
1331 case hwmon_temp_max_alarm:
1332 case hwmon_temp_crit_alarm:
1333 case hwmon_temp_emergency_alarm:
1334 case hwmon_temp_emergency_hyst:
1335 case hwmon_temp_fault:
1336 return 0444;
1337 case hwmon_temp_min:
1338 case hwmon_temp_max:
1339 case hwmon_temp_crit:
1340 case hwmon_temp_emergency:
1341 case hwmon_temp_offset:
1342 return 0644;
1343 case hwmon_temp_crit_hyst:
1344 if (channel == 0)
1345 return 0644;
1346 return 0444;
1347 default:
1348 return 0;
1349 }
1350 }
1351
lm90_chip_read(struct device * dev,u32 attr,int channel,long * val)1352 static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1353 {
1354 struct lm90_data *data = dev_get_drvdata(dev);
1355 int err;
1356
1357 mutex_lock(&data->update_lock);
1358 err = lm90_update_device(dev);
1359 mutex_unlock(&data->update_lock);
1360 if (err)
1361 return err;
1362
1363 switch (attr) {
1364 case hwmon_chip_update_interval:
1365 *val = data->update_interval;
1366 break;
1367 case hwmon_chip_alarms:
1368 *val = data->alarms;
1369 break;
1370 default:
1371 return -EOPNOTSUPP;
1372 }
1373
1374 return 0;
1375 }
1376
lm90_chip_write(struct device * dev,u32 attr,int channel,long val)1377 static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1378 {
1379 struct lm90_data *data = dev_get_drvdata(dev);
1380 struct i2c_client *client = data->client;
1381 int err;
1382
1383 mutex_lock(&data->update_lock);
1384
1385 err = lm90_update_device(dev);
1386 if (err)
1387 goto error;
1388
1389 switch (attr) {
1390 case hwmon_chip_update_interval:
1391 err = lm90_set_convrate(client, data,
1392 clamp_val(val, 0, 100000));
1393 break;
1394 default:
1395 err = -EOPNOTSUPP;
1396 break;
1397 }
1398 error:
1399 mutex_unlock(&data->update_lock);
1400
1401 return err;
1402 }
1403
lm90_chip_is_visible(const void * data,u32 attr,int channel)1404 static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1405 {
1406 switch (attr) {
1407 case hwmon_chip_update_interval:
1408 return 0644;
1409 case hwmon_chip_alarms:
1410 return 0444;
1411 default:
1412 return 0;
1413 }
1414 }
1415
lm90_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)1416 static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1417 u32 attr, int channel, long *val)
1418 {
1419 switch (type) {
1420 case hwmon_chip:
1421 return lm90_chip_read(dev, attr, channel, val);
1422 case hwmon_temp:
1423 return lm90_temp_read(dev, attr, channel, val);
1424 default:
1425 return -EOPNOTSUPP;
1426 }
1427 }
1428
lm90_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)1429 static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1430 u32 attr, int channel, long val)
1431 {
1432 switch (type) {
1433 case hwmon_chip:
1434 return lm90_chip_write(dev, attr, channel, val);
1435 case hwmon_temp:
1436 return lm90_temp_write(dev, attr, channel, val);
1437 default:
1438 return -EOPNOTSUPP;
1439 }
1440 }
1441
lm90_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)1442 static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1443 u32 attr, int channel)
1444 {
1445 switch (type) {
1446 case hwmon_chip:
1447 return lm90_chip_is_visible(data, attr, channel);
1448 case hwmon_temp:
1449 return lm90_temp_is_visible(data, attr, channel);
1450 default:
1451 return 0;
1452 }
1453 }
1454
1455 /* Return 0 if detection is successful, -ENODEV otherwise */
lm90_detect(struct i2c_client * client,struct i2c_board_info * info)1456 static int lm90_detect(struct i2c_client *client,
1457 struct i2c_board_info *info)
1458 {
1459 struct i2c_adapter *adapter = client->adapter;
1460 int address = client->addr;
1461 const char *name = NULL;
1462 int man_id, chip_id, config1, config2, convrate;
1463
1464 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1465 return -ENODEV;
1466
1467 /* detection and identification */
1468 man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1469 chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1470 config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1471 convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1472 if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
1473 return -ENODEV;
1474
1475 if (man_id == 0x01 || man_id == 0x5C || man_id == 0xA1) {
1476 config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1477 if (config2 < 0)
1478 return -ENODEV;
1479 }
1480
1481 if ((address == 0x4C || address == 0x4D)
1482 && man_id == 0x01) { /* National Semiconductor */
1483 if ((config1 & 0x2A) == 0x00
1484 && (config2 & 0xF8) == 0x00
1485 && convrate <= 0x09) {
1486 if (address == 0x4C
1487 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1488 name = "lm90";
1489 } else
1490 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1491 name = "lm99";
1492 dev_info(&adapter->dev,
1493 "Assuming LM99 chip at 0x%02x\n",
1494 address);
1495 dev_info(&adapter->dev,
1496 "If it is an LM89, instantiate it "
1497 "with the new_device sysfs "
1498 "interface\n");
1499 } else
1500 if (address == 0x4C
1501 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1502 name = "lm86";
1503 }
1504 }
1505 } else
1506 if ((address == 0x4C || address == 0x4D)
1507 && man_id == 0x41) { /* Analog Devices */
1508 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1509 && (config1 & 0x3F) == 0x00
1510 && convrate <= 0x0A) {
1511 name = "adm1032";
1512 /*
1513 * The ADM1032 supports PEC, but only if combined
1514 * transactions are not used.
1515 */
1516 if (i2c_check_functionality(adapter,
1517 I2C_FUNC_SMBUS_BYTE))
1518 info->flags |= I2C_CLIENT_PEC;
1519 } else
1520 if (chip_id == 0x51 /* ADT7461 */
1521 && (config1 & 0x1B) == 0x00
1522 && convrate <= 0x0A) {
1523 name = "adt7461";
1524 } else
1525 if (chip_id == 0x57 /* ADT7461A, NCT1008 */
1526 && (config1 & 0x1B) == 0x00
1527 && convrate <= 0x0A) {
1528 name = "adt7461a";
1529 }
1530 } else
1531 if (man_id == 0x4D) { /* Maxim */
1532 int emerg, emerg2, status2;
1533
1534 /*
1535 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1536 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1537 * exists, both readings will reflect the same value. Otherwise,
1538 * the readings will be different.
1539 */
1540 emerg = i2c_smbus_read_byte_data(client,
1541 MAX6659_REG_R_REMOTE_EMERG);
1542 man_id = i2c_smbus_read_byte_data(client,
1543 LM90_REG_R_MAN_ID);
1544 emerg2 = i2c_smbus_read_byte_data(client,
1545 MAX6659_REG_R_REMOTE_EMERG);
1546 status2 = i2c_smbus_read_byte_data(client,
1547 MAX6696_REG_R_STATUS2);
1548 if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
1549 return -ENODEV;
1550
1551 /*
1552 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1553 * register. Reading from that address will return the last
1554 * read value, which in our case is those of the man_id
1555 * register. Likewise, the config1 register seems to lack a
1556 * low nibble, so the value will be those of the previous
1557 * read, so in our case those of the man_id register.
1558 * MAX6659 has a third set of upper temperature limit registers.
1559 * Those registers also return values on MAX6657 and MAX6658,
1560 * thus the only way to detect MAX6659 is by its address.
1561 * For this reason it will be mis-detected as MAX6657 if its
1562 * address is 0x4C.
1563 */
1564 if (chip_id == man_id
1565 && (address == 0x4C || address == 0x4D || address == 0x4E)
1566 && (config1 & 0x1F) == (man_id & 0x0F)
1567 && convrate <= 0x09) {
1568 if (address == 0x4C)
1569 name = "max6657";
1570 else
1571 name = "max6659";
1572 } else
1573 /*
1574 * Even though MAX6695 and MAX6696 do not have a chip ID
1575 * register, reading it returns 0x01. Bit 4 of the config1
1576 * register is unused and should return zero when read. Bit 0 of
1577 * the status2 register is unused and should return zero when
1578 * read.
1579 *
1580 * MAX6695 and MAX6696 have an additional set of temperature
1581 * limit registers. We can detect those chips by checking if
1582 * one of those registers exists.
1583 */
1584 if (chip_id == 0x01
1585 && (config1 & 0x10) == 0x00
1586 && (status2 & 0x01) == 0x00
1587 && emerg == emerg2
1588 && convrate <= 0x07) {
1589 name = "max6696";
1590 } else
1591 /*
1592 * The chip_id register of the MAX6680 and MAX6681 holds the
1593 * revision of the chip. The lowest bit of the config1 register
1594 * is unused and should return zero when read, so should the
1595 * second to last bit of config1 (software reset).
1596 */
1597 if (chip_id == 0x01
1598 && (config1 & 0x03) == 0x00
1599 && convrate <= 0x07) {
1600 name = "max6680";
1601 } else
1602 /*
1603 * The chip_id register of the MAX6646/6647/6649 holds the
1604 * revision of the chip. The lowest 6 bits of the config1
1605 * register are unused and should return zero when read.
1606 */
1607 if (chip_id == 0x59
1608 && (config1 & 0x3f) == 0x00
1609 && convrate <= 0x07) {
1610 name = "max6646";
1611 } else
1612 /*
1613 * The chip_id of the MAX6654 holds the revision of the chip.
1614 * The lowest 3 bits of the config1 register are unused and
1615 * should return zero when read.
1616 */
1617 if (chip_id == 0x08
1618 && (config1 & 0x07) == 0x00
1619 && convrate <= 0x07) {
1620 name = "max6654";
1621 }
1622 } else
1623 if (address == 0x4C
1624 && man_id == 0x5C) { /* Winbond/Nuvoton */
1625 if ((config1 & 0x2A) == 0x00
1626 && (config2 & 0xF8) == 0x00) {
1627 if (chip_id == 0x01 /* W83L771W/G */
1628 && convrate <= 0x09) {
1629 name = "w83l771";
1630 } else
1631 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1632 && convrate <= 0x08) {
1633 name = "w83l771";
1634 }
1635 }
1636 } else
1637 if (address >= 0x48 && address <= 0x4F
1638 && man_id == 0xA1) { /* NXP Semiconductor/Philips */
1639 if (chip_id == 0x00
1640 && (config1 & 0x2A) == 0x00
1641 && (config2 & 0xFE) == 0x00
1642 && convrate <= 0x09) {
1643 name = "sa56004";
1644 }
1645 } else
1646 if ((address == 0x4C || address == 0x4D)
1647 && man_id == 0x47) { /* GMT */
1648 if (chip_id == 0x01 /* G781 */
1649 && (config1 & 0x3F) == 0x00
1650 && convrate <= 0x08)
1651 name = "g781";
1652 } else
1653 if (man_id == 0x55 && chip_id == 0x00 &&
1654 (config1 & 0x1B) == 0x00 && convrate <= 0x09) {
1655 int local_ext, conalert, chen, dfc;
1656
1657 local_ext = i2c_smbus_read_byte_data(client,
1658 TMP451_REG_R_LOCAL_TEMPL);
1659 conalert = i2c_smbus_read_byte_data(client,
1660 TMP451_REG_CONALERT);
1661 chen = i2c_smbus_read_byte_data(client, TMP461_REG_CHEN);
1662 dfc = i2c_smbus_read_byte_data(client, TMP461_REG_DFC);
1663
1664 if ((local_ext & 0x0F) == 0x00 &&
1665 (conalert & 0xf1) == 0x01 &&
1666 (chen & 0xfc) == 0x00 &&
1667 (dfc & 0xfc) == 0x00) {
1668 if (address == 0x4c && !(chen & 0x03))
1669 name = "tmp451";
1670 else if (address >= 0x48 && address <= 0x4f)
1671 name = "tmp461";
1672 }
1673 }
1674
1675 if (!name) { /* identification failed */
1676 dev_dbg(&adapter->dev,
1677 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1678 "chip_id=0x%02X)\n", address, man_id, chip_id);
1679 return -ENODEV;
1680 }
1681
1682 strlcpy(info->type, name, I2C_NAME_SIZE);
1683
1684 return 0;
1685 }
1686
lm90_restore_conf(void * _data)1687 static void lm90_restore_conf(void *_data)
1688 {
1689 struct lm90_data *data = _data;
1690 struct i2c_client *client = data->client;
1691
1692 /* Restore initial configuration */
1693 lm90_write_convrate(data, data->convrate_orig);
1694 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1695 data->config_orig);
1696 }
1697
lm90_init_client(struct i2c_client * client,struct lm90_data * data)1698 static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
1699 {
1700 int config, convrate;
1701
1702 convrate = lm90_read_reg(client, LM90_REG_R_CONVRATE);
1703 if (convrate < 0)
1704 return convrate;
1705 data->convrate_orig = convrate;
1706
1707 /*
1708 * Start the conversions.
1709 */
1710 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1711 if (config < 0)
1712 return config;
1713 data->config_orig = config;
1714 data->config = config;
1715
1716 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
1717
1718 /* Check Temperature Range Select */
1719 if (data->flags & LM90_HAVE_EXTENDED_TEMP) {
1720 if (config & 0x04)
1721 data->flags |= LM90_FLAG_ADT7461_EXT;
1722 }
1723
1724 /*
1725 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1726 * 0.125 degree resolution) and range (0x08, extend range
1727 * to -64 degree) mode for the remote temperature sensor.
1728 */
1729 if (data->kind == max6680)
1730 config |= 0x18;
1731
1732 /*
1733 * Put MAX6654 into extended range (0x20, extend minimum range from
1734 * 0 degrees to -64 degrees). Note that extended resolution is not
1735 * possible on the MAX6654 unless conversion rate is set to 1 Hz or
1736 * slower, which is intentionally not done by default.
1737 */
1738 if (data->kind == max6654)
1739 config |= 0x20;
1740
1741 /*
1742 * Select external channel 0 for max6695/96
1743 */
1744 if (data->kind == max6696)
1745 config &= ~0x08;
1746
1747 config &= 0xBF; /* run */
1748 lm90_update_confreg(data, config);
1749
1750 return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
1751 }
1752
lm90_is_tripped(struct i2c_client * client,u16 * status)1753 static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
1754 {
1755 struct lm90_data *data = i2c_get_clientdata(client);
1756 int st, st2 = 0;
1757
1758 st = lm90_read_reg(client, LM90_REG_R_STATUS);
1759 if (st < 0)
1760 return false;
1761
1762 if (data->kind == max6696) {
1763 st2 = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
1764 if (st2 < 0)
1765 return false;
1766 }
1767
1768 *status = st | (st2 << 8);
1769
1770 if ((st & 0x7f) == 0 && (st2 & 0xfe) == 0)
1771 return false;
1772
1773 if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1774 (st2 & MAX6696_STATUS2_LOT2))
1775 dev_warn(&client->dev,
1776 "temp%d out of range, please check!\n", 1);
1777 if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1778 (st2 & MAX6696_STATUS2_ROT2))
1779 dev_warn(&client->dev,
1780 "temp%d out of range, please check!\n", 2);
1781 if (st & LM90_STATUS_ROPEN)
1782 dev_warn(&client->dev,
1783 "temp%d diode open, please check!\n", 2);
1784 if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1785 MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
1786 dev_warn(&client->dev,
1787 "temp%d out of range, please check!\n", 3);
1788 if (st2 & MAX6696_STATUS2_R2OPEN)
1789 dev_warn(&client->dev,
1790 "temp%d diode open, please check!\n", 3);
1791
1792 return true;
1793 }
1794
lm90_irq_thread(int irq,void * dev_id)1795 static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
1796 {
1797 struct i2c_client *client = dev_id;
1798 u16 status;
1799
1800 if (lm90_is_tripped(client, &status))
1801 return IRQ_HANDLED;
1802 else
1803 return IRQ_NONE;
1804 }
1805
lm90_remove_pec(void * dev)1806 static void lm90_remove_pec(void *dev)
1807 {
1808 device_remove_file(dev, &dev_attr_pec);
1809 }
1810
lm90_regulator_disable(void * regulator)1811 static void lm90_regulator_disable(void *regulator)
1812 {
1813 regulator_disable(regulator);
1814 }
1815
1816
1817 static const struct hwmon_ops lm90_ops = {
1818 .is_visible = lm90_is_visible,
1819 .read = lm90_read,
1820 .write = lm90_write,
1821 };
1822
lm90_probe(struct i2c_client * client)1823 static int lm90_probe(struct i2c_client *client)
1824 {
1825 struct device *dev = &client->dev;
1826 struct i2c_adapter *adapter = client->adapter;
1827 struct hwmon_channel_info *info;
1828 struct regulator *regulator;
1829 struct device *hwmon_dev;
1830 struct lm90_data *data;
1831 int err;
1832
1833 regulator = devm_regulator_get(dev, "vcc");
1834 if (IS_ERR(regulator))
1835 return PTR_ERR(regulator);
1836
1837 err = regulator_enable(regulator);
1838 if (err < 0) {
1839 dev_err(dev, "Failed to enable regulator: %d\n", err);
1840 return err;
1841 }
1842
1843 err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
1844 if (err)
1845 return err;
1846
1847 data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
1848 if (!data)
1849 return -ENOMEM;
1850
1851 data->client = client;
1852 i2c_set_clientdata(client, data);
1853 mutex_init(&data->update_lock);
1854
1855 /* Set the device type */
1856 if (client->dev.of_node)
1857 data->kind = (enum chips)of_device_get_match_data(&client->dev);
1858 else
1859 data->kind = i2c_match_id(lm90_id, client)->driver_data;
1860 if (data->kind == adm1032) {
1861 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1862 client->flags &= ~I2C_CLIENT_PEC;
1863 }
1864
1865 /*
1866 * Different devices have different alarm bits triggering the
1867 * ALERT# output
1868 */
1869 data->alert_alarms = lm90_params[data->kind].alert_alarms;
1870
1871 /* Set chip capabilities */
1872 data->flags = lm90_params[data->kind].flags;
1873
1874 data->chip.ops = &lm90_ops;
1875 data->chip.info = data->info;
1876
1877 data->info[0] = HWMON_CHANNEL_INFO(chip,
1878 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL | HWMON_C_ALARMS);
1879 data->info[1] = &data->temp_info;
1880
1881 info = &data->temp_info;
1882 info->type = hwmon_temp;
1883 info->config = data->channel_config;
1884
1885 data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1886 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM;
1887 data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1888 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM | HWMON_T_FAULT;
1889
1890 if (data->flags & LM90_HAVE_CRIT) {
1891 data->channel_config[0] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
1892 data->channel_config[1] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
1893 }
1894
1895 if (data->flags & LM90_HAVE_OFFSET)
1896 data->channel_config[1] |= HWMON_T_OFFSET;
1897
1898 if (data->flags & LM90_HAVE_EMERGENCY) {
1899 data->channel_config[0] |= HWMON_T_EMERGENCY |
1900 HWMON_T_EMERGENCY_HYST;
1901 data->channel_config[1] |= HWMON_T_EMERGENCY |
1902 HWMON_T_EMERGENCY_HYST;
1903 }
1904
1905 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1906 data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
1907 data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
1908 }
1909
1910 if (data->flags & LM90_HAVE_TEMP3) {
1911 data->channel_config[2] = HWMON_T_INPUT |
1912 HWMON_T_MIN | HWMON_T_MAX |
1913 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
1914 HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST |
1915 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
1916 HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY_ALARM |
1917 HWMON_T_FAULT;
1918 }
1919
1920 data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
1921
1922 /* Set maximum conversion rate */
1923 data->max_convrate = lm90_params[data->kind].max_convrate;
1924
1925 /* Initialize the LM90 chip */
1926 err = lm90_init_client(client, data);
1927 if (err < 0) {
1928 dev_err(dev, "Failed to initialize device\n");
1929 return err;
1930 }
1931
1932 /*
1933 * The 'pec' attribute is attached to the i2c device and thus created
1934 * separately.
1935 */
1936 if (client->flags & I2C_CLIENT_PEC) {
1937 err = device_create_file(dev, &dev_attr_pec);
1938 if (err)
1939 return err;
1940 err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
1941 if (err)
1942 return err;
1943 }
1944
1945 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
1946 data, &data->chip,
1947 NULL);
1948 if (IS_ERR(hwmon_dev))
1949 return PTR_ERR(hwmon_dev);
1950
1951 if (client->irq) {
1952 dev_dbg(dev, "IRQ: %d\n", client->irq);
1953 err = devm_request_threaded_irq(dev, client->irq,
1954 NULL, lm90_irq_thread,
1955 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1956 "lm90", client);
1957 if (err < 0) {
1958 dev_err(dev, "cannot request IRQ %d\n", client->irq);
1959 return err;
1960 }
1961 }
1962
1963 return 0;
1964 }
1965
lm90_alert(struct i2c_client * client,enum i2c_alert_protocol type,unsigned int flag)1966 static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
1967 unsigned int flag)
1968 {
1969 u16 alarms;
1970
1971 if (type != I2C_PROTOCOL_SMBUS_ALERT)
1972 return;
1973
1974 if (lm90_is_tripped(client, &alarms)) {
1975 /*
1976 * Disable ALERT# output, because these chips don't implement
1977 * SMBus alert correctly; they should only hold the alert line
1978 * low briefly.
1979 */
1980 struct lm90_data *data = i2c_get_clientdata(client);
1981
1982 if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
1983 (alarms & data->alert_alarms)) {
1984 dev_dbg(&client->dev, "Disabling ALERT#\n");
1985 lm90_update_confreg(data, data->config | 0x80);
1986 }
1987 } else {
1988 dev_info(&client->dev, "Everything OK\n");
1989 }
1990 }
1991
1992 static struct i2c_driver lm90_driver = {
1993 .class = I2C_CLASS_HWMON,
1994 .driver = {
1995 .name = "lm90",
1996 .of_match_table = of_match_ptr(lm90_of_match),
1997 },
1998 .probe_new = lm90_probe,
1999 .alert = lm90_alert,
2000 .id_table = lm90_id,
2001 .detect = lm90_detect,
2002 .address_list = normal_i2c,
2003 };
2004
2005 module_i2c_driver(lm90_driver);
2006
2007 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
2008 MODULE_DESCRIPTION("LM90/ADM1032 driver");
2009 MODULE_LICENSE("GPL");
2010