Merge tag 'sh-for-v6.6-tag1' of git://git.kernel.org/pub/scm/linux/kernel/git/glaubit...
[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.h>
17 #include <linux/regmap.h>
18 #include <linux/util_macros.h>
19 #include <linux/regulator/consumer.h>
20 #include "lm75.h"
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         at30ts74,
29         ds1775,
30         ds75,
31         ds7505,
32         g751,
33         lm75,
34         lm75a,
35         lm75b,
36         max6625,
37         max6626,
38         max31725,
39         mcp980x,
40         pct2075,
41         stds75,
42         stlm75,
43         tcn75,
44         tmp100,
45         tmp101,
46         tmp105,
47         tmp112,
48         tmp175,
49         tmp275,
50         tmp75,
51         tmp75b,
52         tmp75c,
53         tmp1075,
54 };
55
56 /**
57  * struct lm75_params - lm75 configuration parameters.
58  * @set_mask:           Bits to set in configuration register when configuring
59  *                      the chip.
60  * @clr_mask:           Bits to clear in configuration register when configuring
61  *                      the chip.
62  * @default_resolution: Default number of bits to represent the temperature
63  *                      value.
64  * @resolution_limits:  Limit register resolution. Optional. Should be set if
65  *                      the resolution of limit registers does not match the
66  *                      resolution of the temperature register.
67  * @resolutions:        List of resolutions associated with sample times.
68  *                      Optional. Should be set if num_sample_times is larger
69  *                      than 1, and if the resolution changes with sample times.
70  *                      If set, number of entries must match num_sample_times.
71  * @default_sample_time:Sample time to be set by default.
72  * @num_sample_times:   Number of possible sample times to be set. Optional.
73  *                      Should be set if the number of sample times is larger
74  *                      than one.
75  * @sample_times:       All the possible sample times to be set. Mandatory if
76  *                      num_sample_times is larger than 1. If set, number of
77  *                      entries must match num_sample_times.
78  */
79
80 struct lm75_params {
81         u8                      set_mask;
82         u8                      clr_mask;
83         u8                      default_resolution;
84         u8                      resolution_limits;
85         const u8                *resolutions;
86         unsigned int            default_sample_time;
87         u8                      num_sample_times;
88         const unsigned int      *sample_times;
89 };
90
91 /* Addresses scanned */
92 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
93                                         0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
94
95 /* The LM75 registers */
96 #define LM75_REG_TEMP           0x00
97 #define LM75_REG_CONF           0x01
98 #define LM75_REG_HYST           0x02
99 #define LM75_REG_MAX            0x03
100 #define PCT2075_REG_IDLE        0x04
101
102 /* Each client has this additional data */
103 struct lm75_data {
104         struct i2c_client               *client;
105         struct regmap                   *regmap;
106         struct regulator                *vs;
107         u8                              orig_conf;
108         u8                              current_conf;
109         u8                              resolution;     /* In bits, 9 to 16 */
110         unsigned int                    sample_time;    /* In ms */
111         enum lm75_type                  kind;
112         const struct lm75_params        *params;
113 };
114
115 /*-----------------------------------------------------------------------*/
116
117 static const u8 lm75_sample_set_masks[] = { 0 << 5, 1 << 5, 2 << 5, 3 << 5 };
118
119 #define LM75_SAMPLE_CLEAR_MASK  (3 << 5)
120
121 /* The structure below stores the configuration values of the supported devices.
122  * In case of being supported multiple configurations, the default one must
123  * always be the first element of the array
124  */
125 static const struct lm75_params device_params[] = {
126         [adt75] = {
127                 .clr_mask = 1 << 5,     /* not one-shot mode */
128                 .default_resolution = 12,
129                 .default_sample_time = MSEC_PER_SEC / 10,
130         },
131         [at30ts74] = {
132                 .set_mask = 3 << 5,     /* 12-bit mode*/
133                 .default_resolution = 12,
134                 .default_sample_time = 200,
135                 .num_sample_times = 4,
136                 .sample_times = (unsigned int []){ 25, 50, 100, 200 },
137                 .resolutions = (u8 []) {9, 10, 11, 12 },
138         },
139         [ds1775] = {
140                 .clr_mask = 3 << 5,
141                 .set_mask = 2 << 5,     /* 11-bit mode */
142                 .default_resolution = 11,
143                 .default_sample_time = 500,
144                 .num_sample_times = 4,
145                 .sample_times = (unsigned int []){ 125, 250, 500, 1000 },
146                 .resolutions = (u8 []) {9, 10, 11, 12 },
147         },
148         [ds75] = {
149                 .clr_mask = 3 << 5,
150                 .set_mask = 2 << 5,     /* 11-bit mode */
151                 .default_resolution = 11,
152                 .default_sample_time = 600,
153                 .num_sample_times = 4,
154                 .sample_times = (unsigned int []){ 150, 300, 600, 1200 },
155                 .resolutions = (u8 []) {9, 10, 11, 12 },
156         },
157         [stds75] = {
158                 .clr_mask = 3 << 5,
159                 .set_mask = 2 << 5,     /* 11-bit mode */
160                 .default_resolution = 11,
161                 .default_sample_time = 600,
162                 .num_sample_times = 4,
163                 .sample_times = (unsigned int []){ 150, 300, 600, 1200 },
164                 .resolutions = (u8 []) {9, 10, 11, 12 },
165         },
166         [stlm75] = {
167                 .default_resolution = 9,
168                 .default_sample_time = MSEC_PER_SEC / 6,
169         },
170         [ds7505] = {
171                 .set_mask = 3 << 5,     /* 12-bit mode*/
172                 .default_resolution = 12,
173                 .default_sample_time = 200,
174                 .num_sample_times = 4,
175                 .sample_times = (unsigned int []){ 25, 50, 100, 200 },
176                 .resolutions = (u8 []) {9, 10, 11, 12 },
177         },
178         [g751] = {
179                 .default_resolution = 9,
180                 .default_sample_time = MSEC_PER_SEC / 10,
181         },
182         [lm75] = {
183                 .default_resolution = 9,
184                 .default_sample_time = MSEC_PER_SEC / 10,
185         },
186         [lm75a] = {
187                 .default_resolution = 9,
188                 .default_sample_time = MSEC_PER_SEC / 10,
189         },
190         [lm75b] = {
191                 .default_resolution = 11,
192                 .default_sample_time = MSEC_PER_SEC / 10,
193         },
194         [max6625] = {
195                 .default_resolution = 9,
196                 .default_sample_time = MSEC_PER_SEC / 7,
197         },
198         [max6626] = {
199                 .default_resolution = 12,
200                 .default_sample_time = MSEC_PER_SEC / 7,
201                 .resolution_limits = 9,
202         },
203         [max31725] = {
204                 .default_resolution = 16,
205                 .default_sample_time = MSEC_PER_SEC / 20,
206         },
207         [tcn75] = {
208                 .default_resolution = 9,
209                 .default_sample_time = MSEC_PER_SEC / 18,
210         },
211         [pct2075] = {
212                 .default_resolution = 11,
213                 .default_sample_time = MSEC_PER_SEC / 10,
214                 .num_sample_times = 31,
215                 .sample_times = (unsigned int []){ 100, 200, 300, 400, 500, 600,
216                 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700,
217                 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700,
218                 2800, 2900, 3000, 3100 },
219         },
220         [mcp980x] = {
221                 .set_mask = 3 << 5,     /* 12-bit mode */
222                 .clr_mask = 1 << 7,     /* not one-shot mode */
223                 .default_resolution = 12,
224                 .resolution_limits = 9,
225                 .default_sample_time = 240,
226                 .num_sample_times = 4,
227                 .sample_times = (unsigned int []){ 30, 60, 120, 240 },
228                 .resolutions = (u8 []) {9, 10, 11, 12 },
229         },
230         [tmp100] = {
231                 .set_mask = 3 << 5,     /* 12-bit mode */
232                 .clr_mask = 1 << 7,     /* not one-shot mode */
233                 .default_resolution = 12,
234                 .default_sample_time = 320,
235                 .num_sample_times = 4,
236                 .sample_times = (unsigned int []){ 40, 80, 160, 320 },
237                 .resolutions = (u8 []) {9, 10, 11, 12 },
238         },
239         [tmp101] = {
240                 .set_mask = 3 << 5,     /* 12-bit mode */
241                 .clr_mask = 1 << 7,     /* not one-shot mode */
242                 .default_resolution = 12,
243                 .default_sample_time = 320,
244                 .num_sample_times = 4,
245                 .sample_times = (unsigned int []){ 40, 80, 160, 320 },
246                 .resolutions = (u8 []) {9, 10, 11, 12 },
247         },
248         [tmp105] = {
249                 .set_mask = 3 << 5,     /* 12-bit mode */
250                 .clr_mask = 1 << 7,     /* not one-shot mode*/
251                 .default_resolution = 12,
252                 .default_sample_time = 220,
253                 .num_sample_times = 4,
254                 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
255                 .resolutions = (u8 []) {9, 10, 11, 12 },
256         },
257         [tmp112] = {
258                 .set_mask = 3 << 5,     /* 8 samples / second */
259                 .clr_mask = 1 << 7,     /* no one-shot mode*/
260                 .default_resolution = 12,
261                 .default_sample_time = 125,
262                 .num_sample_times = 4,
263                 .sample_times = (unsigned int []){ 125, 250, 1000, 4000 },
264         },
265         [tmp175] = {
266                 .set_mask = 3 << 5,     /* 12-bit mode */
267                 .clr_mask = 1 << 7,     /* not one-shot mode*/
268                 .default_resolution = 12,
269                 .default_sample_time = 220,
270                 .num_sample_times = 4,
271                 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
272                 .resolutions = (u8 []) {9, 10, 11, 12 },
273         },
274         [tmp275] = {
275                 .set_mask = 3 << 5,     /* 12-bit mode */
276                 .clr_mask = 1 << 7,     /* not one-shot mode*/
277                 .default_resolution = 12,
278                 .default_sample_time = 220,
279                 .num_sample_times = 4,
280                 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
281                 .resolutions = (u8 []) {9, 10, 11, 12 },
282         },
283         [tmp75] = {
284                 .set_mask = 3 << 5,     /* 12-bit mode */
285                 .clr_mask = 1 << 7,     /* not one-shot mode*/
286                 .default_resolution = 12,
287                 .default_sample_time = 220,
288                 .num_sample_times = 4,
289                 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
290                 .resolutions = (u8 []) {9, 10, 11, 12 },
291         },
292         [tmp75b] = { /* not one-shot mode, Conversion rate 37Hz */
293                 .clr_mask = 1 << 7 | 3 << 5,
294                 .default_resolution = 12,
295                 .default_sample_time = MSEC_PER_SEC / 37,
296                 .sample_times = (unsigned int []){ MSEC_PER_SEC / 37,
297                         MSEC_PER_SEC / 18,
298                         MSEC_PER_SEC / 9, MSEC_PER_SEC / 4 },
299                 .num_sample_times = 4,
300         },
301         [tmp75c] = {
302                 .clr_mask = 1 << 5,     /*not one-shot mode*/
303                 .default_resolution = 12,
304                 .default_sample_time = MSEC_PER_SEC / 12,
305         },
306         [tmp1075] = { /* not one-shot mode, 27.5 ms sample rate */
307                 .clr_mask = 1 << 5 | 1 << 6 | 1 << 7,
308                 .default_resolution = 12,
309                 .default_sample_time = 28,
310                 .num_sample_times = 4,
311                 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
312         }
313 };
314
315 static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
316 {
317         return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
318 }
319
320 static int lm75_write_config(struct lm75_data *data, u8 set_mask,
321                              u8 clr_mask)
322 {
323         u8 value;
324
325         clr_mask |= LM75_SHUTDOWN;
326         value = data->current_conf & ~clr_mask;
327         value |= set_mask;
328
329         if (data->current_conf != value) {
330                 s32 err;
331
332                 err = i2c_smbus_write_byte_data(data->client, LM75_REG_CONF,
333                                                 value);
334                 if (err)
335                         return err;
336                 data->current_conf = value;
337         }
338         return 0;
339 }
340
341 static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
342                      u32 attr, int channel, long *val)
343 {
344         struct lm75_data *data = dev_get_drvdata(dev);
345         unsigned int regval;
346         int err, reg;
347
348         switch (type) {
349         case hwmon_chip:
350                 switch (attr) {
351                 case hwmon_chip_update_interval:
352                         *val = data->sample_time;
353                         break;
354                 default:
355                         return -EINVAL;
356                 }
357                 break;
358         case hwmon_temp:
359                 switch (attr) {
360                 case hwmon_temp_input:
361                         reg = LM75_REG_TEMP;
362                         break;
363                 case hwmon_temp_max:
364                         reg = LM75_REG_MAX;
365                         break;
366                 case hwmon_temp_max_hyst:
367                         reg = LM75_REG_HYST;
368                         break;
369                 default:
370                         return -EINVAL;
371                 }
372                 err = regmap_read(data->regmap, reg, &regval);
373                 if (err < 0)
374                         return err;
375
376                 *val = lm75_reg_to_mc(regval, data->resolution);
377                 break;
378         default:
379                 return -EINVAL;
380         }
381         return 0;
382 }
383
384 static int lm75_write_temp(struct device *dev, u32 attr, long temp)
385 {
386         struct lm75_data *data = dev_get_drvdata(dev);
387         u8 resolution;
388         int reg;
389
390         switch (attr) {
391         case hwmon_temp_max:
392                 reg = LM75_REG_MAX;
393                 break;
394         case hwmon_temp_max_hyst:
395                 reg = LM75_REG_HYST;
396                 break;
397         default:
398                 return -EINVAL;
399         }
400
401         /*
402          * Resolution of limit registers is assumed to be the same as the
403          * temperature input register resolution unless given explicitly.
404          */
405         if (data->params->resolution_limits)
406                 resolution = data->params->resolution_limits;
407         else
408                 resolution = data->resolution;
409
410         temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
411         temp = DIV_ROUND_CLOSEST(temp  << (resolution - 8),
412                                  1000) << (16 - resolution);
413
414         return regmap_write(data->regmap, reg, (u16)temp);
415 }
416
417 static int lm75_update_interval(struct device *dev, long val)
418 {
419         struct lm75_data *data = dev_get_drvdata(dev);
420         unsigned int reg;
421         u8 index;
422         s32 err;
423
424         index = find_closest(val, data->params->sample_times,
425                              (int)data->params->num_sample_times);
426
427         switch (data->kind) {
428         default:
429                 err = lm75_write_config(data, lm75_sample_set_masks[index],
430                                         LM75_SAMPLE_CLEAR_MASK);
431                 if (err)
432                         return err;
433
434                 data->sample_time = data->params->sample_times[index];
435                 if (data->params->resolutions)
436                         data->resolution = data->params->resolutions[index];
437                 break;
438         case tmp112:
439                 err = regmap_read(data->regmap, LM75_REG_CONF, &reg);
440                 if (err < 0)
441                         return err;
442                 reg &= ~0x00c0;
443                 reg |= (3 - index) << 6;
444                 err = regmap_write(data->regmap, LM75_REG_CONF, reg);
445                 if (err < 0)
446                         return err;
447                 data->sample_time = data->params->sample_times[index];
448                 break;
449         case pct2075:
450                 err = i2c_smbus_write_byte_data(data->client, PCT2075_REG_IDLE,
451                                                 index + 1);
452                 if (err)
453                         return err;
454                 data->sample_time = data->params->sample_times[index];
455                 break;
456         }
457         return 0;
458 }
459
460 static int lm75_write_chip(struct device *dev, u32 attr, long val)
461 {
462         switch (attr) {
463         case hwmon_chip_update_interval:
464                 return lm75_update_interval(dev, val);
465         default:
466                 return -EINVAL;
467         }
468         return 0;
469 }
470
471 static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
472                       u32 attr, int channel, long val)
473 {
474         switch (type) {
475         case hwmon_chip:
476                 return lm75_write_chip(dev, attr, val);
477         case hwmon_temp:
478                 return lm75_write_temp(dev, attr, val);
479         default:
480                 return -EINVAL;
481         }
482         return 0;
483 }
484
485 static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
486                                u32 attr, int channel)
487 {
488         const struct lm75_data *config_data = data;
489
490         switch (type) {
491         case hwmon_chip:
492                 switch (attr) {
493                 case hwmon_chip_update_interval:
494                         if (config_data->params->num_sample_times > 1)
495                                 return 0644;
496                         return 0444;
497                 }
498                 break;
499         case hwmon_temp:
500                 switch (attr) {
501                 case hwmon_temp_input:
502                         return 0444;
503                 case hwmon_temp_max:
504                 case hwmon_temp_max_hyst:
505                         return 0644;
506                 }
507                 break;
508         default:
509                 break;
510         }
511         return 0;
512 }
513
514 static const struct hwmon_channel_info * const lm75_info[] = {
515         HWMON_CHANNEL_INFO(chip,
516                            HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
517         HWMON_CHANNEL_INFO(temp,
518                            HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
519         NULL
520 };
521
522 static const struct hwmon_ops lm75_hwmon_ops = {
523         .is_visible = lm75_is_visible,
524         .read = lm75_read,
525         .write = lm75_write,
526 };
527
528 static const struct hwmon_chip_info lm75_chip_info = {
529         .ops = &lm75_hwmon_ops,
530         .info = lm75_info,
531 };
532
533 static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
534 {
535         return reg != LM75_REG_TEMP;
536 }
537
538 static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
539 {
540         return reg == LM75_REG_TEMP || reg == LM75_REG_CONF;
541 }
542
543 static const struct regmap_config lm75_regmap_config = {
544         .reg_bits = 8,
545         .val_bits = 16,
546         .max_register = PCT2075_REG_IDLE,
547         .writeable_reg = lm75_is_writeable_reg,
548         .volatile_reg = lm75_is_volatile_reg,
549         .val_format_endian = REGMAP_ENDIAN_BIG,
550         .cache_type = REGCACHE_MAPLE,
551         .use_single_read = true,
552         .use_single_write = true,
553 };
554
555 static void lm75_disable_regulator(void *data)
556 {
557         struct lm75_data *lm75 = data;
558
559         regulator_disable(lm75->vs);
560 }
561
562 static void lm75_remove(void *data)
563 {
564         struct lm75_data *lm75 = data;
565         struct i2c_client *client = lm75->client;
566
567         i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
568 }
569
570 static const struct i2c_device_id lm75_ids[];
571
572 static int lm75_probe(struct i2c_client *client)
573 {
574         struct device *dev = &client->dev;
575         struct device *hwmon_dev;
576         struct lm75_data *data;
577         int status, err;
578         enum lm75_type kind;
579
580         if (client->dev.of_node)
581                 kind = (uintptr_t)of_device_get_match_data(&client->dev);
582         else
583                 kind = i2c_match_id(lm75_ids, client)->driver_data;
584
585         if (!i2c_check_functionality(client->adapter,
586                         I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
587                 return -EIO;
588
589         data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
590         if (!data)
591                 return -ENOMEM;
592
593         data->client = client;
594         data->kind = kind;
595
596         data->vs = devm_regulator_get(dev, "vs");
597         if (IS_ERR(data->vs))
598                 return PTR_ERR(data->vs);
599
600         data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
601         if (IS_ERR(data->regmap))
602                 return PTR_ERR(data->regmap);
603
604         /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
605          * Then tweak to be more precise when appropriate.
606          */
607
608         data->params = &device_params[data->kind];
609
610         /* Save default sample time and resolution*/
611         data->sample_time = data->params->default_sample_time;
612         data->resolution = data->params->default_resolution;
613
614         /* Enable the power */
615         err = regulator_enable(data->vs);
616         if (err) {
617                 dev_err(dev, "failed to enable regulator: %d\n", err);
618                 return err;
619         }
620
621         err = devm_add_action_or_reset(dev, lm75_disable_regulator, data);
622         if (err)
623                 return err;
624
625         /* Cache original configuration */
626         status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
627         if (status < 0) {
628                 dev_dbg(dev, "Can't read config? %d\n", status);
629                 return status;
630         }
631         data->orig_conf = status;
632         data->current_conf = status;
633
634         err = lm75_write_config(data, data->params->set_mask,
635                                 data->params->clr_mask);
636         if (err)
637                 return err;
638
639         err = devm_add_action_or_reset(dev, lm75_remove, data);
640         if (err)
641                 return err;
642
643         hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
644                                                          data, &lm75_chip_info,
645                                                          NULL);
646         if (IS_ERR(hwmon_dev))
647                 return PTR_ERR(hwmon_dev);
648
649         dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
650
651         return 0;
652 }
653
654 static const struct i2c_device_id lm75_ids[] = {
655         { "adt75", adt75, },
656         { "at30ts74", at30ts74, },
657         { "ds1775", ds1775, },
658         { "ds75", ds75, },
659         { "ds7505", ds7505, },
660         { "g751", g751, },
661         { "lm75", lm75, },
662         { "lm75a", lm75a, },
663         { "lm75b", lm75b, },
664         { "max6625", max6625, },
665         { "max6626", max6626, },
666         { "max31725", max31725, },
667         { "max31726", max31725, },
668         { "mcp980x", mcp980x, },
669         { "pct2075", pct2075, },
670         { "stds75", stds75, },
671         { "stlm75", stlm75, },
672         { "tcn75", tcn75, },
673         { "tmp100", tmp100, },
674         { "tmp101", tmp101, },
675         { "tmp105", tmp105, },
676         { "tmp112", tmp112, },
677         { "tmp175", tmp175, },
678         { "tmp275", tmp275, },
679         { "tmp75", tmp75, },
680         { "tmp75b", tmp75b, },
681         { "tmp75c", tmp75c, },
682         { "tmp1075", tmp1075, },
683         { /* LIST END */ }
684 };
685 MODULE_DEVICE_TABLE(i2c, lm75_ids);
686
687 static const struct of_device_id __maybe_unused lm75_of_match[] = {
688         {
689                 .compatible = "adi,adt75",
690                 .data = (void *)adt75
691         },
692         {
693                 .compatible = "atmel,at30ts74",
694                 .data = (void *)at30ts74
695         },
696         {
697                 .compatible = "dallas,ds1775",
698                 .data = (void *)ds1775
699         },
700         {
701                 .compatible = "dallas,ds75",
702                 .data = (void *)ds75
703         },
704         {
705                 .compatible = "dallas,ds7505",
706                 .data = (void *)ds7505
707         },
708         {
709                 .compatible = "gmt,g751",
710                 .data = (void *)g751
711         },
712         {
713                 .compatible = "national,lm75",
714                 .data = (void *)lm75
715         },
716         {
717                 .compatible = "national,lm75a",
718                 .data = (void *)lm75a
719         },
720         {
721                 .compatible = "national,lm75b",
722                 .data = (void *)lm75b
723         },
724         {
725                 .compatible = "maxim,max6625",
726                 .data = (void *)max6625
727         },
728         {
729                 .compatible = "maxim,max6626",
730                 .data = (void *)max6626
731         },
732         {
733                 .compatible = "maxim,max31725",
734                 .data = (void *)max31725
735         },
736         {
737                 .compatible = "maxim,max31726",
738                 .data = (void *)max31725
739         },
740         {
741                 .compatible = "maxim,mcp980x",
742                 .data = (void *)mcp980x
743         },
744         {
745                 .compatible = "nxp,pct2075",
746                 .data = (void *)pct2075
747         },
748         {
749                 .compatible = "st,stds75",
750                 .data = (void *)stds75
751         },
752         {
753                 .compatible = "st,stlm75",
754                 .data = (void *)stlm75
755         },
756         {
757                 .compatible = "microchip,tcn75",
758                 .data = (void *)tcn75
759         },
760         {
761                 .compatible = "ti,tmp100",
762                 .data = (void *)tmp100
763         },
764         {
765                 .compatible = "ti,tmp101",
766                 .data = (void *)tmp101
767         },
768         {
769                 .compatible = "ti,tmp105",
770                 .data = (void *)tmp105
771         },
772         {
773                 .compatible = "ti,tmp112",
774                 .data = (void *)tmp112
775         },
776         {
777                 .compatible = "ti,tmp175",
778                 .data = (void *)tmp175
779         },
780         {
781                 .compatible = "ti,tmp275",
782                 .data = (void *)tmp275
783         },
784         {
785                 .compatible = "ti,tmp75",
786                 .data = (void *)tmp75
787         },
788         {
789                 .compatible = "ti,tmp75b",
790                 .data = (void *)tmp75b
791         },
792         {
793                 .compatible = "ti,tmp75c",
794                 .data = (void *)tmp75c
795         },
796         {
797                 .compatible = "ti,tmp1075",
798                 .data = (void *)tmp1075
799         },
800         { },
801 };
802 MODULE_DEVICE_TABLE(of, lm75_of_match);
803
804 #define LM75A_ID 0xA1
805
806 /* Return 0 if detection is successful, -ENODEV otherwise */
807 static int lm75_detect(struct i2c_client *new_client,
808                        struct i2c_board_info *info)
809 {
810         struct i2c_adapter *adapter = new_client->adapter;
811         int i;
812         int conf, hyst, os;
813         bool is_lm75a = 0;
814
815         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
816                                      I2C_FUNC_SMBUS_WORD_DATA))
817                 return -ENODEV;
818
819         /*
820          * Now, we do the remaining detection. There is no identification-
821          * dedicated register so we have to rely on several tricks:
822          * unused bits, registers cycling over 8-address boundaries,
823          * addresses 0x04-0x07 returning the last read value.
824          * The cycling+unused addresses combination is not tested,
825          * since it would significantly slow the detection down and would
826          * hardly add any value.
827          *
828          * The National Semiconductor LM75A is different than earlier
829          * LM75s.  It has an ID byte of 0xaX (where X is the chip
830          * revision, with 1 being the only revision in existence) in
831          * register 7, and unused registers return 0xff rather than the
832          * last read value.
833          *
834          * Note that this function only detects the original National
835          * Semiconductor LM75 and the LM75A. Clones from other vendors
836          * aren't detected, on purpose, because they are typically never
837          * found on PC hardware. They are found on embedded designs where
838          * they can be instantiated explicitly so detection is not needed.
839          * The absence of identification registers on all these clones
840          * would make their exhaustive detection very difficult and weak,
841          * and odds are that the driver would bind to unsupported devices.
842          */
843
844         /* Unused bits */
845         conf = i2c_smbus_read_byte_data(new_client, 1);
846         if (conf & 0xe0)
847                 return -ENODEV;
848
849         /* First check for LM75A */
850         if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
851                 /*
852                  * LM75A returns 0xff on unused registers so
853                  * just to be sure we check for that too.
854                  */
855                 if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
856                  || i2c_smbus_read_byte_data(new_client, 5) != 0xff
857                  || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
858                         return -ENODEV;
859                 is_lm75a = 1;
860                 hyst = i2c_smbus_read_byte_data(new_client, 2);
861                 os = i2c_smbus_read_byte_data(new_client, 3);
862         } else { /* Traditional style LM75 detection */
863                 /* Unused addresses */
864                 hyst = i2c_smbus_read_byte_data(new_client, 2);
865                 if (i2c_smbus_read_byte_data(new_client, 4) != hyst
866                  || i2c_smbus_read_byte_data(new_client, 5) != hyst
867                  || i2c_smbus_read_byte_data(new_client, 6) != hyst
868                  || i2c_smbus_read_byte_data(new_client, 7) != hyst)
869                         return -ENODEV;
870                 os = i2c_smbus_read_byte_data(new_client, 3);
871                 if (i2c_smbus_read_byte_data(new_client, 4) != os
872                  || i2c_smbus_read_byte_data(new_client, 5) != os
873                  || i2c_smbus_read_byte_data(new_client, 6) != os
874                  || i2c_smbus_read_byte_data(new_client, 7) != os)
875                         return -ENODEV;
876         }
877         /*
878          * It is very unlikely that this is a LM75 if both
879          * hysteresis and temperature limit registers are 0.
880          */
881         if (hyst == 0 && os == 0)
882                 return -ENODEV;
883
884         /* Addresses cycling */
885         for (i = 8; i <= 248; i += 40) {
886                 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
887                  || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
888                  || i2c_smbus_read_byte_data(new_client, i + 3) != os)
889                         return -ENODEV;
890                 if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
891                                 != LM75A_ID)
892                         return -ENODEV;
893         }
894
895         strscpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
896
897         return 0;
898 }
899
900 #ifdef CONFIG_PM
901 static int lm75_suspend(struct device *dev)
902 {
903         int status;
904         struct i2c_client *client = to_i2c_client(dev);
905
906         status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
907         if (status < 0) {
908                 dev_dbg(&client->dev, "Can't read config? %d\n", status);
909                 return status;
910         }
911         status = status | LM75_SHUTDOWN;
912         i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
913         return 0;
914 }
915
916 static int lm75_resume(struct device *dev)
917 {
918         int status;
919         struct i2c_client *client = to_i2c_client(dev);
920
921         status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
922         if (status < 0) {
923                 dev_dbg(&client->dev, "Can't read config? %d\n", status);
924                 return status;
925         }
926         status = status & ~LM75_SHUTDOWN;
927         i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
928         return 0;
929 }
930
931 static const struct dev_pm_ops lm75_dev_pm_ops = {
932         .suspend        = lm75_suspend,
933         .resume         = lm75_resume,
934 };
935 #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
936 #else
937 #define LM75_DEV_PM_OPS NULL
938 #endif /* CONFIG_PM */
939
940 static struct i2c_driver lm75_driver = {
941         .class          = I2C_CLASS_HWMON,
942         .driver = {
943                 .name   = "lm75",
944                 .of_match_table = of_match_ptr(lm75_of_match),
945                 .pm     = LM75_DEV_PM_OPS,
946         },
947         .probe          = lm75_probe,
948         .id_table       = lm75_ids,
949         .detect         = lm75_detect,
950         .address_list   = normal_i2c,
951 };
952
953 module_i2c_driver(lm75_driver);
954
955 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
956 MODULE_DESCRIPTION("LM75 driver");
957 MODULE_LICENSE("GPL");