Commit | Line | Data |
---|---|---|
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> |
22e73183 | 16 | #include <linux/of.h> |
e65365fe | 17 | #include <linux/regmap.h> |
4b5be3c1 | 18 | #include <linux/util_macros.h> |
707d151b | 19 | #include <linux/regulator/consumer.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 | 26 | enum lm75_type { /* keep sorted in alphabetical order */ |
e96f9d89 | 27 | adt75, |
c851b715 | 28 | at30ts74, |
1f86df49 | 29 | ds1775, |
9ebd3d82 | 30 | ds75, |
3fbc81e3 | 31 | ds7505, |
c98d6c65 | 32 | g751, |
1f86df49 | 33 | lm75, |
9ebd3d82 | 34 | lm75a, |
799fc602 | 35 | lm75b, |
9ebd3d82 DB |
36 | max6625, |
37 | max6626, | |
a54ca77a | 38 | max31725, |
9ebd3d82 | 39 | mcp980x, |
557c7ffa | 40 | pct2075, |
9ebd3d82 | 41 | stds75, |
2e9a41bb | 42 | stlm75, |
9ebd3d82 DB |
43 | tcn75, |
44 | tmp100, | |
45 | tmp101, | |
6d034059 | 46 | tmp105, |
c83959f8 | 47 | tmp112, |
9ebd3d82 DB |
48 | tmp175, |
49 | tmp275, | |
50 | tmp75, | |
39abe9d8 | 51 | tmp75b, |
9c32e815 | 52 | tmp75c, |
ec081f91 | 53 | tmp1075, |
9ebd3d82 DB |
54 | }; |
55 | ||
dcb12653 IPPS |
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. | |
7f1a300f IPPS |
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. | |
dcb12653 IPPS |
78 | */ |
79 | ||
80 | struct lm75_params { | |
7f1a300f IPPS |
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; | |
dcb12653 IPPS |
89 | }; |
90 | ||
8ff69eeb | 91 | /* Addresses scanned */ |
25e9c86d | 92 | static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c, |
1da177e4 | 93 | 0x4d, 0x4e, 0x4f, I2C_CLIENT_END }; |
1da177e4 | 94 | |
1da177e4 | 95 | /* The LM75 registers */ |
e65365fe | 96 | #define LM75_REG_TEMP 0x00 |
1da177e4 | 97 | #define LM75_REG_CONF 0x01 |
e65365fe GR |
98 | #define LM75_REG_HYST 0x02 |
99 | #define LM75_REG_MAX 0x03 | |
d7a85cde | 100 | #define PCT2075_REG_IDLE 0x04 |
1da177e4 LT |
101 | |
102 | /* Each client has this additional data */ | |
103 | struct lm75_data { | |
dcb12653 IPPS |
104 | struct i2c_client *client; |
105 | struct regmap *regmap; | |
707d151b | 106 | struct regulator *vs; |
dcb12653 IPPS |
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; | |
1da177e4 LT |
113 | }; |
114 | ||
01a52397 | 115 | /*-----------------------------------------------------------------------*/ |
7db0db3f GR |
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 | ||
dcb12653 IPPS |
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, | |
35cd1804 | 129 | .default_sample_time = MSEC_PER_SEC / 10, |
dcb12653 | 130 | }, |
c851b715 PR |
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 | }, | |
dcb12653 IPPS |
139 | [ds1775] = { |
140 | .clr_mask = 3 << 5, | |
141 | .set_mask = 2 << 5, /* 11-bit mode */ | |
142 | .default_resolution = 11, | |
35cd1804 | 143 | .default_sample_time = 500, |
7db0db3f | 144 | .num_sample_times = 4, |
35cd1804 | 145 | .sample_times = (unsigned int []){ 125, 250, 500, 1000 }, |
7db0db3f | 146 | .resolutions = (u8 []) {9, 10, 11, 12 }, |
dcb12653 IPPS |
147 | }, |
148 | [ds75] = { | |
149 | .clr_mask = 3 << 5, | |
150 | .set_mask = 2 << 5, /* 11-bit mode */ | |
151 | .default_resolution = 11, | |
7db0db3f GR |
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 }, | |
dcb12653 IPPS |
156 | }, |
157 | [stds75] = { | |
158 | .clr_mask = 3 << 5, | |
159 | .set_mask = 2 << 5, /* 11-bit mode */ | |
160 | .default_resolution = 11, | |
7db0db3f GR |
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 }, | |
dcb12653 IPPS |
165 | }, |
166 | [stlm75] = { | |
167 | .default_resolution = 9, | |
35cd1804 | 168 | .default_sample_time = MSEC_PER_SEC / 6, |
dcb12653 IPPS |
169 | }, |
170 | [ds7505] = { | |
171 | .set_mask = 3 << 5, /* 12-bit mode*/ | |
172 | .default_resolution = 12, | |
7db0db3f GR |
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 }, | |
dcb12653 IPPS |
177 | }, |
178 | [g751] = { | |
179 | .default_resolution = 9, | |
35cd1804 | 180 | .default_sample_time = MSEC_PER_SEC / 10, |
dcb12653 IPPS |
181 | }, |
182 | [lm75] = { | |
183 | .default_resolution = 9, | |
35cd1804 | 184 | .default_sample_time = MSEC_PER_SEC / 10, |
dcb12653 IPPS |
185 | }, |
186 | [lm75a] = { | |
187 | .default_resolution = 9, | |
35cd1804 | 188 | .default_sample_time = MSEC_PER_SEC / 10, |
dcb12653 IPPS |
189 | }, |
190 | [lm75b] = { | |
191 | .default_resolution = 11, | |
35cd1804 | 192 | .default_sample_time = MSEC_PER_SEC / 10, |
dcb12653 IPPS |
193 | }, |
194 | [max6625] = { | |
195 | .default_resolution = 9, | |
35cd1804 | 196 | .default_sample_time = MSEC_PER_SEC / 7, |
dcb12653 IPPS |
197 | }, |
198 | [max6626] = { | |
199 | .default_resolution = 12, | |
35cd1804 | 200 | .default_sample_time = MSEC_PER_SEC / 7, |
dcb12653 IPPS |
201 | .resolution_limits = 9, |
202 | }, | |
203 | [max31725] = { | |
204 | .default_resolution = 16, | |
35cd1804 | 205 | .default_sample_time = MSEC_PER_SEC / 20, |
dcb12653 IPPS |
206 | }, |
207 | [tcn75] = { | |
208 | .default_resolution = 9, | |
35cd1804 | 209 | .default_sample_time = MSEC_PER_SEC / 18, |
dcb12653 IPPS |
210 | }, |
211 | [pct2075] = { | |
212 | .default_resolution = 11, | |
213 | .default_sample_time = MSEC_PER_SEC / 10, | |
d7a85cde GR |
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 }, | |
dcb12653 IPPS |
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, | |
7db0db3f GR |
225 | .default_sample_time = 240, |
226 | .num_sample_times = 4, | |
35cd1804 | 227 | .sample_times = (unsigned int []){ 30, 60, 120, 240 }, |
7db0db3f | 228 | .resolutions = (u8 []) {9, 10, 11, 12 }, |
dcb12653 IPPS |
229 | }, |
230 | [tmp100] = { | |
231 | .set_mask = 3 << 5, /* 12-bit mode */ | |
232 | .clr_mask = 1 << 7, /* not one-shot mode */ | |
233 | .default_resolution = 12, | |
7db0db3f GR |
234 | .default_sample_time = 320, |
235 | .num_sample_times = 4, | |
35cd1804 | 236 | .sample_times = (unsigned int []){ 40, 80, 160, 320 }, |
7db0db3f | 237 | .resolutions = (u8 []) {9, 10, 11, 12 }, |
dcb12653 IPPS |
238 | }, |
239 | [tmp101] = { | |
240 | .set_mask = 3 << 5, /* 12-bit mode */ | |
241 | .clr_mask = 1 << 7, /* not one-shot mode */ | |
242 | .default_resolution = 12, | |
7db0db3f GR |
243 | .default_sample_time = 320, |
244 | .num_sample_times = 4, | |
35cd1804 | 245 | .sample_times = (unsigned int []){ 40, 80, 160, 320 }, |
7db0db3f | 246 | .resolutions = (u8 []) {9, 10, 11, 12 }, |
dcb12653 | 247 | }, |
7db0db3f | 248 | [tmp105] = { |
dcb12653 | 249 | .set_mask = 3 << 5, /* 12-bit mode */ |
7db0db3f | 250 | .clr_mask = 1 << 7, /* not one-shot mode*/ |
dcb12653 | 251 | .default_resolution = 12, |
7db0db3f GR |
252 | .default_sample_time = 220, |
253 | .num_sample_times = 4, | |
35cd1804 | 254 | .sample_times = (unsigned int []){ 28, 55, 110, 220 }, |
7db0db3f | 255 | .resolutions = (u8 []) {9, 10, 11, 12 }, |
dcb12653 | 256 | }, |
7db0db3f | 257 | [tmp112] = { |
35cd1804 | 258 | .set_mask = 3 << 5, /* 8 samples / second */ |
7db0db3f | 259 | .clr_mask = 1 << 7, /* no one-shot mode*/ |
dcb12653 | 260 | .default_resolution = 12, |
35cd1804 | 261 | .default_sample_time = 125, |
cee04a01 GR |
262 | .num_sample_times = 4, |
263 | .sample_times = (unsigned int []){ 125, 250, 1000, 4000 }, | |
dcb12653 IPPS |
264 | }, |
265 | [tmp175] = { | |
266 | .set_mask = 3 << 5, /* 12-bit mode */ | |
267 | .clr_mask = 1 << 7, /* not one-shot mode*/ | |
268 | .default_resolution = 12, | |
7db0db3f GR |
269 | .default_sample_time = 220, |
270 | .num_sample_times = 4, | |
35cd1804 | 271 | .sample_times = (unsigned int []){ 28, 55, 110, 220 }, |
7db0db3f | 272 | .resolutions = (u8 []) {9, 10, 11, 12 }, |
dcb12653 IPPS |
273 | }, |
274 | [tmp275] = { | |
275 | .set_mask = 3 << 5, /* 12-bit mode */ | |
276 | .clr_mask = 1 << 7, /* not one-shot mode*/ | |
277 | .default_resolution = 12, | |
7db0db3f GR |
278 | .default_sample_time = 220, |
279 | .num_sample_times = 4, | |
35cd1804 | 280 | .sample_times = (unsigned int []){ 28, 55, 110, 220 }, |
7db0db3f | 281 | .resolutions = (u8 []) {9, 10, 11, 12 }, |
dcb12653 IPPS |
282 | }, |
283 | [tmp75] = { | |
284 | .set_mask = 3 << 5, /* 12-bit mode */ | |
285 | .clr_mask = 1 << 7, /* not one-shot mode*/ | |
286 | .default_resolution = 12, | |
7db0db3f GR |
287 | .default_sample_time = 220, |
288 | .num_sample_times = 4, | |
35cd1804 | 289 | .sample_times = (unsigned int []){ 28, 55, 110, 220 }, |
7db0db3f | 290 | .resolutions = (u8 []) {9, 10, 11, 12 }, |
dcb12653 IPPS |
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, | |
7f1a300f IPPS |
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, | |
dcb12653 IPPS |
300 | }, |
301 | [tmp75c] = { | |
302 | .clr_mask = 1 << 5, /*not one-shot mode*/ | |
303 | .default_resolution = 12, | |
35cd1804 | 304 | .default_sample_time = MSEC_PER_SEC / 12, |
ec081f91 RM |
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 }, | |
dcb12653 IPPS |
312 | } |
313 | }; | |
01a52397 | 314 | |
22e73183 EV |
315 | static inline long lm75_reg_to_mc(s16 temp, u8 resolution) |
316 | { | |
317 | return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8); | |
318 | } | |
319 | ||
58608cfe IPPS |
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 | ||
08b02433 GR |
341 | static int lm75_read(struct device *dev, enum hwmon_sensor_types type, |
342 | u32 attr, int channel, long *val) | |
22e73183 | 343 | { |
e65365fe | 344 | struct lm75_data *data = dev_get_drvdata(dev); |
08b02433 GR |
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; | |
ccffe776 | 353 | break; |
08b02433 GR |
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, ®val); | |
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 | } | |
22e73183 EV |
381 | return 0; |
382 | } | |
383 | ||
4b5be3c1 | 384 | static int lm75_write_temp(struct device *dev, u32 attr, long temp) |
9ca8e40c | 385 | { |
e65365fe | 386 | struct lm75_data *data = dev_get_drvdata(dev); |
87d0621a | 387 | u8 resolution; |
08b02433 GR |
388 | int reg; |
389 | ||
08b02433 GR |
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 | } | |
9ca8e40c | 400 | |
87d0621a JD |
401 | /* |
402 | * Resolution of limit registers is assumed to be the same as the | |
403 | * temperature input register resolution unless given explicitly. | |
404 | */ | |
dcb12653 IPPS |
405 | if (data->params->resolution_limits) |
406 | resolution = data->params->resolution_limits; | |
87d0621a JD |
407 | else |
408 | resolution = data->resolution; | |
409 | ||
87d0621a | 410 | temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX); |
e65365fe GR |
411 | temp = DIV_ROUND_CLOSEST(temp << (resolution - 8), |
412 | 1000) << (16 - resolution); | |
e65365fe | 413 | |
7d82fcc9 | 414 | return regmap_write(data->regmap, reg, (u16)temp); |
1da177e4 | 415 | } |
1da177e4 | 416 | |
040b106f | 417 | static int lm75_update_interval(struct device *dev, long val) |
4b5be3c1 IPPS |
418 | { |
419 | struct lm75_data *data = dev_get_drvdata(dev); | |
cee04a01 | 420 | unsigned int reg; |
4b5be3c1 IPPS |
421 | u8 index; |
422 | s32 err; | |
423 | ||
040b106f GR |
424 | index = find_closest(val, data->params->sample_times, |
425 | (int)data->params->num_sample_times); | |
4b5be3c1 | 426 | |
cee04a01 GR |
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; | |
040b106f | 433 | |
cee04a01 GR |
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, ®); | |
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; | |
d7a85cde GR |
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; | |
cee04a01 | 456 | } |
040b106f GR |
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); | |
4b5be3c1 IPPS |
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 | ||
08b02433 GR |
485 | static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type, |
486 | u32 attr, int channel) | |
5f7e5e29 | 487 | { |
4b5be3c1 IPPS |
488 | const struct lm75_data *config_data = data; |
489 | ||
08b02433 GR |
490 | switch (type) { |
491 | case hwmon_chip: | |
492 | switch (attr) { | |
493 | case hwmon_chip_update_interval: | |
4b5be3c1 IPPS |
494 | if (config_data->params->num_sample_times > 1) |
495 | return 0644; | |
e6ab6e0e | 496 | return 0444; |
08b02433 GR |
497 | } |
498 | break; | |
499 | case hwmon_temp: | |
500 | switch (attr) { | |
501 | case hwmon_temp_input: | |
e6ab6e0e | 502 | return 0444; |
08b02433 GR |
503 | case hwmon_temp_max: |
504 | case hwmon_temp_max_hyst: | |
e6ab6e0e | 505 | return 0644; |
08b02433 GR |
506 | } |
507 | break; | |
508 | default: | |
509 | break; | |
510 | } | |
511 | return 0; | |
5f7e5e29 GR |
512 | } |
513 | ||
3ee2ceca | 514 | static const struct hwmon_channel_info * const lm75_info[] = { |
e4f6fed1 GR |
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), | |
08b02433 GR |
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 | }; | |
9ebd3d82 | 532 | |
e65365fe GR |
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 | { | |
cee04a01 | 540 | return reg == LM75_REG_TEMP || reg == LM75_REG_CONF; |
e65365fe GR |
541 | } |
542 | ||
543 | static const struct regmap_config lm75_regmap_config = { | |
544 | .reg_bits = 8, | |
545 | .val_bits = 16, | |
d7a85cde | 546 | .max_register = PCT2075_REG_IDLE, |
e65365fe GR |
547 | .writeable_reg = lm75_is_writeable_reg, |
548 | .volatile_reg = lm75_is_volatile_reg, | |
549 | .val_format_endian = REGMAP_ENDIAN_BIG, | |
30841ce3 | 550 | .cache_type = REGCACHE_MAPLE, |
1c96a2f6 DF |
551 | .use_single_read = true, |
552 | .use_single_write = true, | |
e65365fe GR |
553 | }; |
554 | ||
707d151b AB |
555 | static void lm75_disable_regulator(void *data) |
556 | { | |
557 | struct lm75_data *lm75 = data; | |
558 | ||
559 | regulator_disable(lm75->vs); | |
560 | } | |
561 | ||
9e37d3e2 GR |
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 | ||
67487038 SK |
570 | static const struct i2c_device_id lm75_ids[]; |
571 | ||
572 | static int lm75_probe(struct i2c_client *client) | |
9ebd3d82 | 573 | { |
d663ec49 | 574 | struct device *dev = &client->dev; |
9e37d3e2 | 575 | struct device *hwmon_dev; |
9ebd3d82 | 576 | struct lm75_data *data; |
90e2b545 | 577 | int status, err; |
e97a45f1 JMC |
578 | enum lm75_type kind; |
579 | ||
580 | if (client->dev.of_node) | |
c7e07faa | 581 | kind = (uintptr_t)of_device_get_match_data(&client->dev); |
e97a45f1 | 582 | else |
67487038 | 583 | kind = i2c_match_id(lm75_ids, client)->driver_data; |
9ebd3d82 DB |
584 | |
585 | if (!i2c_check_functionality(client->adapter, | |
586 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) | |
587 | return -EIO; | |
588 | ||
d663ec49 | 589 | data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL); |
9ebd3d82 DB |
590 | if (!data) |
591 | return -ENOMEM; | |
592 | ||
d663ec49 | 593 | data->client = client; |
dcb12653 | 594 | data->kind = kind; |
e65365fe | 595 | |
707d151b AB |
596 | data->vs = devm_regulator_get(dev, "vs"); |
597 | if (IS_ERR(data->vs)) | |
598 | return PTR_ERR(data->vs); | |
599 | ||
e65365fe GR |
600 | data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config); |
601 | if (IS_ERR(data->regmap)) | |
602 | return PTR_ERR(data->regmap); | |
9ebd3d82 DB |
603 | |
604 | /* Set to LM75 resolution (9 bits, 1/2 degree C) and range. | |
605 | * Then tweak to be more precise when appropriate. | |
606 | */ | |
9ebd3d82 | 607 | |
dcb12653 IPPS |
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 | ||
707d151b AB |
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 | ||
dcb12653 | 625 | /* Cache original configuration */ |
38aefb41 | 626 | status = i2c_smbus_read_byte_data(client, LM75_REG_CONF); |
9ebd3d82 | 627 | if (status < 0) { |
d663ec49 | 628 | dev_dbg(dev, "Can't read config? %d\n", status); |
13ac7a01 | 629 | return status; |
9ebd3d82 DB |
630 | } |
631 | data->orig_conf = status; | |
58608cfe | 632 | data->current_conf = status; |
9ebd3d82 | 633 | |
58608cfe IPPS |
634 | err = lm75_write_config(data, data->params->set_mask, |
635 | data->params->clr_mask); | |
90e2b545 GR |
636 | if (err) |
637 | return err; | |
9ebd3d82 | 638 | |
58608cfe IPPS |
639 | err = devm_add_action_or_reset(dev, lm75_remove, data); |
640 | if (err) | |
641 | return err; | |
22e73183 | 642 | |
08b02433 GR |
643 | hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, |
644 | data, &lm75_chip_info, | |
645 | NULL); | |
9e37d3e2 GR |
646 | if (IS_ERR(hwmon_dev)) |
647 | return PTR_ERR(hwmon_dev); | |
9ebd3d82 | 648 | |
9e37d3e2 | 649 | dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name); |
9ebd3d82 | 650 | |
9ebd3d82 DB |
651 | return 0; |
652 | } | |
653 | ||
654 | static const struct i2c_device_id lm75_ids[] = { | |
e96f9d89 | 655 | { "adt75", adt75, }, |
c851b715 | 656 | { "at30ts74", at30ts74, }, |
9ebd3d82 DB |
657 | { "ds1775", ds1775, }, |
658 | { "ds75", ds75, }, | |
3fbc81e3 | 659 | { "ds7505", ds7505, }, |
c98d6c65 | 660 | { "g751", g751, }, |
9ebd3d82 DB |
661 | { "lm75", lm75, }, |
662 | { "lm75a", lm75a, }, | |
799fc602 | 663 | { "lm75b", lm75b, }, |
9ebd3d82 DB |
664 | { "max6625", max6625, }, |
665 | { "max6626", max6626, }, | |
a54ca77a KY |
666 | { "max31725", max31725, }, |
667 | { "max31726", max31725, }, | |
9ebd3d82 | 668 | { "mcp980x", mcp980x, }, |
557c7ffa | 669 | { "pct2075", pct2075, }, |
9ebd3d82 | 670 | { "stds75", stds75, }, |
2e9a41bb | 671 | { "stlm75", stlm75, }, |
9ebd3d82 DB |
672 | { "tcn75", tcn75, }, |
673 | { "tmp100", tmp100, }, | |
674 | { "tmp101", tmp101, }, | |
6d034059 | 675 | { "tmp105", tmp105, }, |
c83959f8 | 676 | { "tmp112", tmp112, }, |
9ebd3d82 DB |
677 | { "tmp175", tmp175, }, |
678 | { "tmp275", tmp275, }, | |
679 | { "tmp75", tmp75, }, | |
39abe9d8 | 680 | { "tmp75b", tmp75b, }, |
9c32e815 | 681 | { "tmp75c", tmp75c, }, |
ec081f91 | 682 | { "tmp1075", tmp1075, }, |
9ebd3d82 DB |
683 | { /* LIST END */ } |
684 | }; | |
685 | MODULE_DEVICE_TABLE(i2c, lm75_ids); | |
686 | ||
ffa83e78 | 687 | static const struct of_device_id __maybe_unused lm75_of_match[] = { |
e97a45f1 JMC |
688 | { |
689 | .compatible = "adi,adt75", | |
690 | .data = (void *)adt75 | |
691 | }, | |
c851b715 PR |
692 | { |
693 | .compatible = "atmel,at30ts74", | |
694 | .data = (void *)at30ts74 | |
695 | }, | |
e97a45f1 JMC |
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 | }, | |
a54ca77a KY |
732 | { |
733 | .compatible = "maxim,max31725", | |
734 | .data = (void *)max31725 | |
735 | }, | |
736 | { | |
737 | .compatible = "maxim,max31726", | |
738 | .data = (void *)max31725 | |
739 | }, | |
e97a45f1 JMC |
740 | { |
741 | .compatible = "maxim,mcp980x", | |
742 | .data = (void *)mcp980x | |
743 | }, | |
557c7ffa DM |
744 | { |
745 | .compatible = "nxp,pct2075", | |
746 | .data = (void *)pct2075 | |
747 | }, | |
e97a45f1 JMC |
748 | { |
749 | .compatible = "st,stds75", | |
750 | .data = (void *)stds75 | |
751 | }, | |
2e9a41bb JT |
752 | { |
753 | .compatible = "st,stlm75", | |
754 | .data = (void *)stlm75 | |
755 | }, | |
e97a45f1 JMC |
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 | }, | |
39abe9d8 IPPS |
788 | { |
789 | .compatible = "ti,tmp75b", | |
790 | .data = (void *)tmp75b | |
791 | }, | |
e97a45f1 JMC |
792 | { |
793 | .compatible = "ti,tmp75c", | |
794 | .data = (void *)tmp75c | |
795 | }, | |
ec081f91 RM |
796 | { |
797 | .compatible = "ti,tmp1075", | |
798 | .data = (void *)tmp1075 | |
799 | }, | |
e97a45f1 JMC |
800 | { }, |
801 | }; | |
802 | MODULE_DEVICE_TABLE(of, lm75_of_match); | |
803 | ||
05e82fe4 LS |
804 | #define LM75A_ID 0xA1 |
805 | ||
8ff69eeb | 806 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec792 | 807 | static int lm75_detect(struct i2c_client *new_client, |
8ff69eeb | 808 | struct i2c_board_info *info) |
1da177e4 | 809 | { |
8ff69eeb | 810 | struct i2c_adapter *adapter = new_client->adapter; |
1da177e4 | 811 | int i; |
e76f67b5 | 812 | int conf, hyst, os; |
05e82fe4 | 813 | bool is_lm75a = 0; |
1da177e4 | 814 | |
1da177e4 LT |
815 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
816 | I2C_FUNC_SMBUS_WORD_DATA)) | |
8ff69eeb | 817 | return -ENODEV; |
1da177e4 | 818 | |
426343ef JD |
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 | */ | |
1da177e4 | 843 | |
e76f67b5 | 844 | /* Unused bits */ |
52df6440 | 845 | conf = i2c_smbus_read_byte_data(new_client, 1); |
e76f67b5 JD |
846 | if (conf & 0xe0) |
847 | return -ENODEV; | |
05e82fe4 LS |
848 | |
849 | /* First check for LM75A */ | |
850 | if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) { | |
8cbf2172 MO |
851 | /* |
852 | * LM75A returns 0xff on unused registers so | |
853 | * just to be sure we check for that too. | |
854 | */ | |
05e82fe4 LS |
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; | |
e76f67b5 JD |
860 | hyst = i2c_smbus_read_byte_data(new_client, 2); |
861 | os = i2c_smbus_read_byte_data(new_client, 3); | |
05e82fe4 LS |
862 | } else { /* Traditional style LM75 detection */ |
863 | /* Unused addresses */ | |
e76f67b5 JD |
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) | |
05e82fe4 | 869 | return -ENODEV; |
e76f67b5 JD |
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) | |
05e82fe4 LS |
875 | return -ENODEV; |
876 | } | |
4ad40cc5 GR |
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; | |
1da177e4 | 883 | |
52df6440 | 884 | /* Addresses cycling */ |
e76f67b5 | 885 | for (i = 8; i <= 248; i += 40) { |
52df6440 | 886 | if (i2c_smbus_read_byte_data(new_client, i + 1) != conf |
e76f67b5 JD |
887 | || i2c_smbus_read_byte_data(new_client, i + 2) != hyst |
888 | || i2c_smbus_read_byte_data(new_client, i + 3) != os) | |
52df6440 | 889 | return -ENODEV; |
05e82fe4 LS |
890 | if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7) |
891 | != LM75A_ID) | |
892 | return -ENODEV; | |
1da177e4 LT |
893 | } |
894 | ||
f2f394db | 895 | strscpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE); |
c1685f61 | 896 | |
1da177e4 | 897 | return 0; |
01a52397 DB |
898 | } |
899 | ||
9914518e SD |
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); | |
8cbf2172 | 905 | |
38aefb41 | 906 | status = i2c_smbus_read_byte_data(client, LM75_REG_CONF); |
9914518e SD |
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; | |
38aefb41 | 912 | i2c_smbus_write_byte_data(client, LM75_REG_CONF, status); |
9914518e SD |
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); | |
8cbf2172 | 920 | |
38aefb41 | 921 | status = i2c_smbus_read_byte_data(client, LM75_REG_CONF); |
9914518e SD |
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; | |
38aefb41 | 927 | i2c_smbus_write_byte_data(client, LM75_REG_CONF, status); |
9914518e SD |
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 | ||
8ff69eeb JD |
940 | static struct i2c_driver lm75_driver = { |
941 | .class = I2C_CLASS_HWMON, | |
01a52397 | 942 | .driver = { |
8ff69eeb | 943 | .name = "lm75", |
e97a45f1 | 944 | .of_match_table = of_match_ptr(lm75_of_match), |
9914518e | 945 | .pm = LM75_DEV_PM_OPS, |
01a52397 | 946 | }, |
1975d167 | 947 | .probe = lm75_probe, |
8ff69eeb JD |
948 | .id_table = lm75_ids, |
949 | .detect = lm75_detect, | |
c3813d6a | 950 | .address_list = normal_i2c, |
01a52397 DB |
951 | }; |
952 | ||
f0967eea | 953 | module_i2c_driver(lm75_driver); |
1da177e4 LT |
954 | |
955 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"); | |
956 | MODULE_DESCRIPTION("LM75 driver"); | |
957 | MODULE_LICENSE("GPL"); |