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