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