Merge branch 'x86-topology-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / drivers / hwmon / lm75.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
4  *       monitoring
5  * Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
6  */
7
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/jiffies.h>
12 #include <linux/i2c.h>
13 #include <linux/hwmon.h>
14 #include <linux/hwmon-sysfs.h>
15 #include <linux/err.h>
16 #include <linux/of_device.h>
17 #include <linux/of.h>
18 #include <linux/regmap.h>
19 #include "lm75.h"
20
21
22 /*
23  * This driver handles the LM75 and compatible digital temperature sensors.
24  */
25
26 enum lm75_type {                /* keep sorted in alphabetical order */
27         adt75,
28         ds1775,
29         ds75,
30         ds7505,
31         g751,
32         lm75,
33         lm75a,
34         lm75b,
35         max6625,
36         max6626,
37         max31725,
38         mcp980x,
39         stds75,
40         stlm75,
41         tcn75,
42         tmp100,
43         tmp101,
44         tmp105,
45         tmp112,
46         tmp175,
47         tmp275,
48         tmp75,
49         tmp75b,
50         tmp75c,
51 };
52
53 /* Addresses scanned */
54 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
55                                         0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
56
57 /* The LM75 registers */
58 #define LM75_REG_TEMP           0x00
59 #define LM75_REG_CONF           0x01
60 #define LM75_REG_HYST           0x02
61 #define LM75_REG_MAX            0x03
62
63 /* Each client has this additional data */
64 struct lm75_data {
65         struct i2c_client       *client;
66         struct regmap           *regmap;
67         u8                      orig_conf;
68         u8                      resolution;     /* In bits, between 9 and 16 */
69         u8                      resolution_limits;
70         unsigned int            sample_time;    /* In ms */
71 };
72
73 /*-----------------------------------------------------------------------*/
74
75 static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
76 {
77         return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
78 }
79
80 static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
81                      u32 attr, int channel, long *val)
82 {
83         struct lm75_data *data = dev_get_drvdata(dev);
84         unsigned int regval;
85         int err, reg;
86
87         switch (type) {
88         case hwmon_chip:
89                 switch (attr) {
90                 case hwmon_chip_update_interval:
91                         *val = data->sample_time;
92                         break;
93                 default:
94                         return -EINVAL;
95                 }
96                 break;
97         case hwmon_temp:
98                 switch (attr) {
99                 case hwmon_temp_input:
100                         reg = LM75_REG_TEMP;
101                         break;
102                 case hwmon_temp_max:
103                         reg = LM75_REG_MAX;
104                         break;
105                 case hwmon_temp_max_hyst:
106                         reg = LM75_REG_HYST;
107                         break;
108                 default:
109                         return -EINVAL;
110                 }
111                 err = regmap_read(data->regmap, reg, &regval);
112                 if (err < 0)
113                         return err;
114
115                 *val = lm75_reg_to_mc(regval, data->resolution);
116                 break;
117         default:
118                 return -EINVAL;
119         }
120         return 0;
121 }
122
123 static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
124                       u32 attr, int channel, long temp)
125 {
126         struct lm75_data *data = dev_get_drvdata(dev);
127         u8 resolution;
128         int reg;
129
130         if (type != hwmon_temp)
131                 return -EINVAL;
132
133         switch (attr) {
134         case hwmon_temp_max:
135                 reg = LM75_REG_MAX;
136                 break;
137         case hwmon_temp_max_hyst:
138                 reg = LM75_REG_HYST;
139                 break;
140         default:
141                 return -EINVAL;
142         }
143
144         /*
145          * Resolution of limit registers is assumed to be the same as the
146          * temperature input register resolution unless given explicitly.
147          */
148         if (data->resolution_limits)
149                 resolution = data->resolution_limits;
150         else
151                 resolution = data->resolution;
152
153         temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
154         temp = DIV_ROUND_CLOSEST(temp  << (resolution - 8),
155                                  1000) << (16 - resolution);
156
157         return regmap_write(data->regmap, reg, temp);
158 }
159
160 static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
161                                u32 attr, int channel)
162 {
163         switch (type) {
164         case hwmon_chip:
165                 switch (attr) {
166                 case hwmon_chip_update_interval:
167                         return 0444;
168                 }
169                 break;
170         case hwmon_temp:
171                 switch (attr) {
172                 case hwmon_temp_input:
173                         return 0444;
174                 case hwmon_temp_max:
175                 case hwmon_temp_max_hyst:
176                         return 0644;
177                 }
178                 break;
179         default:
180                 break;
181         }
182         return 0;
183 }
184
185 static const struct hwmon_channel_info *lm75_info[] = {
186         HWMON_CHANNEL_INFO(chip,
187                            HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
188         HWMON_CHANNEL_INFO(temp,
189                            HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
190         NULL
191 };
192
193 static const struct hwmon_ops lm75_hwmon_ops = {
194         .is_visible = lm75_is_visible,
195         .read = lm75_read,
196         .write = lm75_write,
197 };
198
199 static const struct hwmon_chip_info lm75_chip_info = {
200         .ops = &lm75_hwmon_ops,
201         .info = lm75_info,
202 };
203
204 static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
205 {
206         return reg != LM75_REG_TEMP;
207 }
208
209 static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
210 {
211         return reg == LM75_REG_TEMP;
212 }
213
214 static const struct regmap_config lm75_regmap_config = {
215         .reg_bits = 8,
216         .val_bits = 16,
217         .max_register = LM75_REG_MAX,
218         .writeable_reg = lm75_is_writeable_reg,
219         .volatile_reg = lm75_is_volatile_reg,
220         .val_format_endian = REGMAP_ENDIAN_BIG,
221         .cache_type = REGCACHE_RBTREE,
222         .use_single_read = true,
223         .use_single_write = true,
224 };
225
226 static void lm75_remove(void *data)
227 {
228         struct lm75_data *lm75 = data;
229         struct i2c_client *client = lm75->client;
230
231         i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
232 }
233
234 static int
235 lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
236 {
237         struct device *dev = &client->dev;
238         struct device *hwmon_dev;
239         struct lm75_data *data;
240         int status, err;
241         u8 set_mask, clr_mask;
242         int new;
243         enum lm75_type kind;
244
245         if (client->dev.of_node)
246                 kind = (enum lm75_type)of_device_get_match_data(&client->dev);
247         else
248                 kind = id->driver_data;
249
250         if (!i2c_check_functionality(client->adapter,
251                         I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
252                 return -EIO;
253
254         data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
255         if (!data)
256                 return -ENOMEM;
257
258         data->client = client;
259
260         data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
261         if (IS_ERR(data->regmap))
262                 return PTR_ERR(data->regmap);
263
264         /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
265          * Then tweak to be more precise when appropriate.
266          */
267         set_mask = 0;
268         clr_mask = LM75_SHUTDOWN;               /* continuous conversions */
269
270         switch (kind) {
271         case adt75:
272                 clr_mask |= 1 << 5;             /* not one-shot mode */
273                 data->resolution = 12;
274                 data->sample_time = MSEC_PER_SEC / 8;
275                 break;
276         case ds1775:
277         case ds75:
278         case stds75:
279                 clr_mask |= 3 << 5;
280                 set_mask |= 2 << 5;             /* 11-bit mode */
281                 data->resolution = 11;
282                 data->sample_time = MSEC_PER_SEC;
283                 break;
284         case stlm75:
285                 data->resolution = 9;
286                 data->sample_time = MSEC_PER_SEC / 5;
287                 break;
288         case ds7505:
289                 set_mask |= 3 << 5;             /* 12-bit mode */
290                 data->resolution = 12;
291                 data->sample_time = MSEC_PER_SEC / 4;
292                 break;
293         case g751:
294         case lm75:
295         case lm75a:
296                 data->resolution = 9;
297                 data->sample_time = MSEC_PER_SEC / 2;
298                 break;
299         case lm75b:
300                 data->resolution = 11;
301                 data->sample_time = MSEC_PER_SEC / 4;
302                 break;
303         case max6625:
304                 data->resolution = 9;
305                 data->sample_time = MSEC_PER_SEC / 4;
306                 break;
307         case max6626:
308                 data->resolution = 12;
309                 data->resolution_limits = 9;
310                 data->sample_time = MSEC_PER_SEC / 4;
311                 break;
312         case max31725:
313                 data->resolution = 16;
314                 data->sample_time = MSEC_PER_SEC / 8;
315                 break;
316         case tcn75:
317                 data->resolution = 9;
318                 data->sample_time = MSEC_PER_SEC / 8;
319                 break;
320         case mcp980x:
321                 data->resolution_limits = 9;
322                 /* fall through */
323         case tmp100:
324         case tmp101:
325                 set_mask |= 3 << 5;             /* 12-bit mode */
326                 data->resolution = 12;
327                 data->sample_time = MSEC_PER_SEC;
328                 clr_mask |= 1 << 7;             /* not one-shot mode */
329                 break;
330         case tmp112:
331                 set_mask |= 3 << 5;             /* 12-bit mode */
332                 clr_mask |= 1 << 7;             /* not one-shot mode */
333                 data->resolution = 12;
334                 data->sample_time = MSEC_PER_SEC / 4;
335                 break;
336         case tmp105:
337         case tmp175:
338         case tmp275:
339         case tmp75:
340                 set_mask |= 3 << 5;             /* 12-bit mode */
341                 clr_mask |= 1 << 7;             /* not one-shot mode */
342                 data->resolution = 12;
343                 data->sample_time = MSEC_PER_SEC / 2;
344                 break;
345         case tmp75b:  /* not one-shot mode, Conversion rate 37Hz */
346                 clr_mask |= 1 << 15 | 0x3 << 13;
347                 data->resolution = 12;
348                 data->sample_time = MSEC_PER_SEC / 37;
349                 break;
350         case tmp75c:
351                 clr_mask |= 1 << 5;             /* not one-shot mode */
352                 data->resolution = 12;
353                 data->sample_time = MSEC_PER_SEC / 4;
354                 break;
355         }
356
357         /* configure as specified */
358         status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
359         if (status < 0) {
360                 dev_dbg(dev, "Can't read config? %d\n", status);
361                 return status;
362         }
363         data->orig_conf = status;
364         new = status & ~clr_mask;
365         new |= set_mask;
366         if (status != new)
367                 i2c_smbus_write_byte_data(client, LM75_REG_CONF, new);
368
369         err = devm_add_action_or_reset(dev, lm75_remove, data);
370         if (err)
371                 return err;
372
373         dev_dbg(dev, "Config %02x\n", new);
374
375         hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
376                                                          data, &lm75_chip_info,
377                                                          NULL);
378         if (IS_ERR(hwmon_dev))
379                 return PTR_ERR(hwmon_dev);
380
381         dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
382
383         return 0;
384 }
385
386 static const struct i2c_device_id lm75_ids[] = {
387         { "adt75", adt75, },
388         { "ds1775", ds1775, },
389         { "ds75", ds75, },
390         { "ds7505", ds7505, },
391         { "g751", g751, },
392         { "lm75", lm75, },
393         { "lm75a", lm75a, },
394         { "lm75b", lm75b, },
395         { "max6625", max6625, },
396         { "max6626", max6626, },
397         { "max31725", max31725, },
398         { "max31726", max31725, },
399         { "mcp980x", mcp980x, },
400         { "stds75", stds75, },
401         { "stlm75", stlm75, },
402         { "tcn75", tcn75, },
403         { "tmp100", tmp100, },
404         { "tmp101", tmp101, },
405         { "tmp105", tmp105, },
406         { "tmp112", tmp112, },
407         { "tmp175", tmp175, },
408         { "tmp275", tmp275, },
409         { "tmp75", tmp75, },
410         { "tmp75b", tmp75b, },
411         { "tmp75c", tmp75c, },
412         { /* LIST END */ }
413 };
414 MODULE_DEVICE_TABLE(i2c, lm75_ids);
415
416 static const struct of_device_id __maybe_unused lm75_of_match[] = {
417         {
418                 .compatible = "adi,adt75",
419                 .data = (void *)adt75
420         },
421         {
422                 .compatible = "dallas,ds1775",
423                 .data = (void *)ds1775
424         },
425         {
426                 .compatible = "dallas,ds75",
427                 .data = (void *)ds75
428         },
429         {
430                 .compatible = "dallas,ds7505",
431                 .data = (void *)ds7505
432         },
433         {
434                 .compatible = "gmt,g751",
435                 .data = (void *)g751
436         },
437         {
438                 .compatible = "national,lm75",
439                 .data = (void *)lm75
440         },
441         {
442                 .compatible = "national,lm75a",
443                 .data = (void *)lm75a
444         },
445         {
446                 .compatible = "national,lm75b",
447                 .data = (void *)lm75b
448         },
449         {
450                 .compatible = "maxim,max6625",
451                 .data = (void *)max6625
452         },
453         {
454                 .compatible = "maxim,max6626",
455                 .data = (void *)max6626
456         },
457         {
458                 .compatible = "maxim,max31725",
459                 .data = (void *)max31725
460         },
461         {
462                 .compatible = "maxim,max31726",
463                 .data = (void *)max31725
464         },
465         {
466                 .compatible = "maxim,mcp980x",
467                 .data = (void *)mcp980x
468         },
469         {
470                 .compatible = "st,stds75",
471                 .data = (void *)stds75
472         },
473         {
474                 .compatible = "st,stlm75",
475                 .data = (void *)stlm75
476         },
477         {
478                 .compatible = "microchip,tcn75",
479                 .data = (void *)tcn75
480         },
481         {
482                 .compatible = "ti,tmp100",
483                 .data = (void *)tmp100
484         },
485         {
486                 .compatible = "ti,tmp101",
487                 .data = (void *)tmp101
488         },
489         {
490                 .compatible = "ti,tmp105",
491                 .data = (void *)tmp105
492         },
493         {
494                 .compatible = "ti,tmp112",
495                 .data = (void *)tmp112
496         },
497         {
498                 .compatible = "ti,tmp175",
499                 .data = (void *)tmp175
500         },
501         {
502                 .compatible = "ti,tmp275",
503                 .data = (void *)tmp275
504         },
505         {
506                 .compatible = "ti,tmp75",
507                 .data = (void *)tmp75
508         },
509         {
510                 .compatible = "ti,tmp75b",
511                 .data = (void *)tmp75b
512         },
513         {
514                 .compatible = "ti,tmp75c",
515                 .data = (void *)tmp75c
516         },
517         { },
518 };
519 MODULE_DEVICE_TABLE(of, lm75_of_match);
520
521 #define LM75A_ID 0xA1
522
523 /* Return 0 if detection is successful, -ENODEV otherwise */
524 static int lm75_detect(struct i2c_client *new_client,
525                        struct i2c_board_info *info)
526 {
527         struct i2c_adapter *adapter = new_client->adapter;
528         int i;
529         int conf, hyst, os;
530         bool is_lm75a = 0;
531
532         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
533                                      I2C_FUNC_SMBUS_WORD_DATA))
534                 return -ENODEV;
535
536         /*
537          * Now, we do the remaining detection. There is no identification-
538          * dedicated register so we have to rely on several tricks:
539          * unused bits, registers cycling over 8-address boundaries,
540          * addresses 0x04-0x07 returning the last read value.
541          * The cycling+unused addresses combination is not tested,
542          * since it would significantly slow the detection down and would
543          * hardly add any value.
544          *
545          * The National Semiconductor LM75A is different than earlier
546          * LM75s.  It has an ID byte of 0xaX (where X is the chip
547          * revision, with 1 being the only revision in existence) in
548          * register 7, and unused registers return 0xff rather than the
549          * last read value.
550          *
551          * Note that this function only detects the original National
552          * Semiconductor LM75 and the LM75A. Clones from other vendors
553          * aren't detected, on purpose, because they are typically never
554          * found on PC hardware. They are found on embedded designs where
555          * they can be instantiated explicitly so detection is not needed.
556          * The absence of identification registers on all these clones
557          * would make their exhaustive detection very difficult and weak,
558          * and odds are that the driver would bind to unsupported devices.
559          */
560
561         /* Unused bits */
562         conf = i2c_smbus_read_byte_data(new_client, 1);
563         if (conf & 0xe0)
564                 return -ENODEV;
565
566         /* First check for LM75A */
567         if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
568                 /* LM75A returns 0xff on unused registers so
569                    just to be sure we check for that too. */
570                 if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
571                  || i2c_smbus_read_byte_data(new_client, 5) != 0xff
572                  || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
573                         return -ENODEV;
574                 is_lm75a = 1;
575                 hyst = i2c_smbus_read_byte_data(new_client, 2);
576                 os = i2c_smbus_read_byte_data(new_client, 3);
577         } else { /* Traditional style LM75 detection */
578                 /* Unused addresses */
579                 hyst = i2c_smbus_read_byte_data(new_client, 2);
580                 if (i2c_smbus_read_byte_data(new_client, 4) != hyst
581                  || i2c_smbus_read_byte_data(new_client, 5) != hyst
582                  || i2c_smbus_read_byte_data(new_client, 6) != hyst
583                  || i2c_smbus_read_byte_data(new_client, 7) != hyst)
584                         return -ENODEV;
585                 os = i2c_smbus_read_byte_data(new_client, 3);
586                 if (i2c_smbus_read_byte_data(new_client, 4) != os
587                  || i2c_smbus_read_byte_data(new_client, 5) != os
588                  || i2c_smbus_read_byte_data(new_client, 6) != os
589                  || i2c_smbus_read_byte_data(new_client, 7) != os)
590                         return -ENODEV;
591         }
592         /*
593          * It is very unlikely that this is a LM75 if both
594          * hysteresis and temperature limit registers are 0.
595          */
596         if (hyst == 0 && os == 0)
597                 return -ENODEV;
598
599         /* Addresses cycling */
600         for (i = 8; i <= 248; i += 40) {
601                 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
602                  || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
603                  || i2c_smbus_read_byte_data(new_client, i + 3) != os)
604                         return -ENODEV;
605                 if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
606                                 != LM75A_ID)
607                         return -ENODEV;
608         }
609
610         strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
611
612         return 0;
613 }
614
615 #ifdef CONFIG_PM
616 static int lm75_suspend(struct device *dev)
617 {
618         int status;
619         struct i2c_client *client = to_i2c_client(dev);
620         status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
621         if (status < 0) {
622                 dev_dbg(&client->dev, "Can't read config? %d\n", status);
623                 return status;
624         }
625         status = status | LM75_SHUTDOWN;
626         i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
627         return 0;
628 }
629
630 static int lm75_resume(struct device *dev)
631 {
632         int status;
633         struct i2c_client *client = to_i2c_client(dev);
634         status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
635         if (status < 0) {
636                 dev_dbg(&client->dev, "Can't read config? %d\n", status);
637                 return status;
638         }
639         status = status & ~LM75_SHUTDOWN;
640         i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
641         return 0;
642 }
643
644 static const struct dev_pm_ops lm75_dev_pm_ops = {
645         .suspend        = lm75_suspend,
646         .resume         = lm75_resume,
647 };
648 #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
649 #else
650 #define LM75_DEV_PM_OPS NULL
651 #endif /* CONFIG_PM */
652
653 static struct i2c_driver lm75_driver = {
654         .class          = I2C_CLASS_HWMON,
655         .driver = {
656                 .name   = "lm75",
657                 .of_match_table = of_match_ptr(lm75_of_match),
658                 .pm     = LM75_DEV_PM_OPS,
659         },
660         .probe          = lm75_probe,
661         .id_table       = lm75_ids,
662         .detect         = lm75_detect,
663         .address_list   = normal_i2c,
664 };
665
666 module_i2c_driver(lm75_driver);
667
668 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
669 MODULE_DESCRIPTION("LM75 driver");
670 MODULE_LICENSE("GPL");