1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Hardware monitoring driver for LTC2978 and compatible chips.
4 *
5 * Copyright (c) 2011 Ericsson AB.
6 * Copyright (c) 2013, 2014, 2015 Guenter Roeck
7 * Copyright (c) 2015 Linear Technology
8 * Copyright (c) 2018 Analog Devices Inc.
9 */
10
11 #include <linux/delay.h>
12 #include <linux/jiffies.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/i2c.h>
19 #include <linux/regulator/driver.h>
20 #include "pmbus.h"
21
22 enum chips {
23 /* Managers */
24 ltc2972, ltc2974, ltc2975, ltc2977, ltc2978, ltc2979, ltc2980,
25 /* Controllers */
26 ltc3880, ltc3882, ltc3883, ltc3884, ltc3886, ltc3887, ltc3889, ltc7132,
27 ltc7841, ltc7880,
28 /* Modules */
29 ltm2987, ltm4664, ltm4675, ltm4676, ltm4677, ltm4678, ltm4680, ltm4686,
30 ltm4700,
31 };
32
33 /* Common for all chips */
34 #define LTC2978_MFR_VOUT_PEAK 0xdd
35 #define LTC2978_MFR_VIN_PEAK 0xde
36 #define LTC2978_MFR_TEMPERATURE_PEAK 0xdf
37 #define LTC2978_MFR_SPECIAL_ID 0xe7 /* Undocumented on LTC3882 */
38 #define LTC2978_MFR_COMMON 0xef
39
40 /* LTC2974, LTC2975, LCT2977, LTC2980, LTC2978, and LTM2987 */
41 #define LTC2978_MFR_VOUT_MIN 0xfb
42 #define LTC2978_MFR_VIN_MIN 0xfc
43 #define LTC2978_MFR_TEMPERATURE_MIN 0xfd
44
45 /* LTC2974, LTC2975 */
46 #define LTC2974_MFR_IOUT_PEAK 0xd7
47 #define LTC2974_MFR_IOUT_MIN 0xd8
48
49 /* LTC3880, LTC3882, LTC3883, LTC3887, LTM4675, LTM4676, LTC7132 */
50 #define LTC3880_MFR_IOUT_PEAK 0xd7
51 #define LTC3880_MFR_CLEAR_PEAKS 0xe3
52 #define LTC3880_MFR_TEMPERATURE2_PEAK 0xf4
53
54 /* LTC3883, LTC3884, LTC3886, LTC3889, LTC7132, LTC7841 and LTC7880 only */
55 #define LTC3883_MFR_IIN_PEAK 0xe1
56
57 /* LTC2975 only */
58 #define LTC2975_MFR_IIN_PEAK 0xc4
59 #define LTC2975_MFR_IIN_MIN 0xc5
60 #define LTC2975_MFR_PIN_PEAK 0xc6
61 #define LTC2975_MFR_PIN_MIN 0xc7
62
63 #define LTC2978_ID_MASK 0xfff0
64
65 #define LTC2972_ID 0x0310
66 #define LTC2974_ID 0x0210
67 #define LTC2975_ID 0x0220
68 #define LTC2977_ID 0x0130
69 #define LTC2978_ID_REV1 0x0110 /* Early revision */
70 #define LTC2978_ID_REV2 0x0120
71 #define LTC2979_ID_A 0x8060
72 #define LTC2979_ID_B 0x8070
73 #define LTC2980_ID_A 0x8030 /* A/B for two die IDs */
74 #define LTC2980_ID_B 0x8040
75 #define LTC3880_ID 0x4020
76 #define LTC3882_ID 0x4200
77 #define LTC3882_ID_D1 0x4240 /* Dash 1 */
78 #define LTC3883_ID 0x4300
79 #define LTC3884_ID 0x4C00
80 #define LTC3886_ID 0x4600
81 #define LTC3887_ID 0x4700
82 #define LTC3889_ID 0x4900
83 #define LTC7132_ID 0x4CE0
84 #define LTC7841_ID 0x40D0
85 #define LTC7880_ID 0x49E0
86 #define LTM2987_ID_A 0x8010 /* A/B for two die IDs */
87 #define LTM2987_ID_B 0x8020
88 #define LTM4664_ID 0x4120
89 #define LTM4675_ID 0x47a0
90 #define LTM4676_ID_REV1 0x4400
91 #define LTM4676_ID_REV2 0x4480
92 #define LTM4676A_ID 0x47e0
93 #define LTM4677_ID_REV1 0x47B0
94 #define LTM4677_ID_REV2 0x47D0
95 #define LTM4678_ID_REV1 0x4100
96 #define LTM4678_ID_REV2 0x4110
97 #define LTM4680_ID 0x4140
98 #define LTM4686_ID 0x4770
99 #define LTM4700_ID 0x4130
100
101 #define LTC2972_NUM_PAGES 2
102 #define LTC2974_NUM_PAGES 4
103 #define LTC2978_NUM_PAGES 8
104 #define LTC3880_NUM_PAGES 2
105 #define LTC3883_NUM_PAGES 1
106
107 #define LTC_POLL_TIMEOUT 100 /* in milli-seconds */
108
109 #define LTC_NOT_BUSY BIT(6)
110 #define LTC_NOT_PENDING BIT(5)
111
112 /*
113 * LTC2978 clears peak data whenever the CLEAR_FAULTS command is executed, which
114 * happens pretty much each time chip data is updated. Raw peak data therefore
115 * does not provide much value. To be able to provide useful peak data, keep an
116 * internal cache of measured peak data, which is only cleared if an explicit
117 * "clear peak" command is executed for the sensor in question.
118 */
119
120 struct ltc2978_data {
121 enum chips id;
122 u16 vin_min, vin_max;
123 u16 temp_min[LTC2974_NUM_PAGES], temp_max[LTC2974_NUM_PAGES];
124 u16 vout_min[LTC2978_NUM_PAGES], vout_max[LTC2978_NUM_PAGES];
125 u16 iout_min[LTC2974_NUM_PAGES], iout_max[LTC2974_NUM_PAGES];
126 u16 iin_min, iin_max;
127 u16 pin_min, pin_max;
128 u16 temp2_max;
129 struct pmbus_driver_info info;
130 u32 features;
131 };
132 #define to_ltc2978_data(x) container_of(x, struct ltc2978_data, info)
133
134 #define FEAT_CLEAR_PEAKS BIT(0)
135 #define FEAT_NEEDS_POLLING BIT(1)
136
137 #define has_clear_peaks(d) ((d)->features & FEAT_CLEAR_PEAKS)
138 #define needs_polling(d) ((d)->features & FEAT_NEEDS_POLLING)
139
ltc_wait_ready(struct i2c_client * client)140 static int ltc_wait_ready(struct i2c_client *client)
141 {
142 unsigned long timeout = jiffies + msecs_to_jiffies(LTC_POLL_TIMEOUT);
143 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
144 struct ltc2978_data *data = to_ltc2978_data(info);
145 int status;
146 u8 mask;
147
148 if (!needs_polling(data))
149 return 0;
150
151 /*
152 * LTC3883 does not support LTC_NOT_PENDING, even though
153 * the datasheet claims that it does.
154 */
155 mask = LTC_NOT_BUSY;
156 if (data->id != ltc3883)
157 mask |= LTC_NOT_PENDING;
158
159 do {
160 status = pmbus_read_byte_data(client, 0, LTC2978_MFR_COMMON);
161 if (status == -EBADMSG || status == -ENXIO) {
162 /* PEC error or NACK: chip may be busy, try again */
163 usleep_range(50, 100);
164 continue;
165 }
166 if (status < 0)
167 return status;
168
169 if ((status & mask) == mask)
170 return 0;
171
172 usleep_range(50, 100);
173 } while (time_before(jiffies, timeout));
174
175 return -ETIMEDOUT;
176 }
177
ltc_read_word_data(struct i2c_client * client,int page,int phase,int reg)178 static int ltc_read_word_data(struct i2c_client *client, int page, int phase,
179 int reg)
180 {
181 int ret;
182
183 ret = ltc_wait_ready(client);
184 if (ret < 0)
185 return ret;
186
187 return pmbus_read_word_data(client, page, 0xff, reg);
188 }
189
ltc_read_byte_data(struct i2c_client * client,int page,int reg)190 static int ltc_read_byte_data(struct i2c_client *client, int page, int reg)
191 {
192 int ret;
193
194 ret = ltc_wait_ready(client);
195 if (ret < 0)
196 return ret;
197
198 return pmbus_read_byte_data(client, page, reg);
199 }
200
ltc_write_byte_data(struct i2c_client * client,int page,int reg,u8 value)201 static int ltc_write_byte_data(struct i2c_client *client, int page, int reg, u8 value)
202 {
203 int ret;
204
205 ret = ltc_wait_ready(client);
206 if (ret < 0)
207 return ret;
208
209 return pmbus_write_byte_data(client, page, reg, value);
210 }
211
ltc_write_byte(struct i2c_client * client,int page,u8 byte)212 static int ltc_write_byte(struct i2c_client *client, int page, u8 byte)
213 {
214 int ret;
215
216 ret = ltc_wait_ready(client);
217 if (ret < 0)
218 return ret;
219
220 return pmbus_write_byte(client, page, byte);
221 }
222
lin11_to_val(int data)223 static inline int lin11_to_val(int data)
224 {
225 s16 e = ((s16)data) >> 11;
226 s32 m = (((s16)(data << 5)) >> 5);
227
228 /*
229 * mantissa is 10 bit + sign, exponent adds up to 15 bit.
230 * Add 6 bit to exponent for maximum accuracy (10 + 15 + 6 = 31).
231 */
232 e += 6;
233 return (e < 0 ? m >> -e : m << e);
234 }
235
ltc_get_max(struct ltc2978_data * data,struct i2c_client * client,int page,int reg,u16 * pmax)236 static int ltc_get_max(struct ltc2978_data *data, struct i2c_client *client,
237 int page, int reg, u16 *pmax)
238 {
239 int ret;
240
241 ret = ltc_read_word_data(client, page, 0xff, reg);
242 if (ret >= 0) {
243 if (lin11_to_val(ret) > lin11_to_val(*pmax))
244 *pmax = ret;
245 ret = *pmax;
246 }
247 return ret;
248 }
249
ltc_get_min(struct ltc2978_data * data,struct i2c_client * client,int page,int reg,u16 * pmin)250 static int ltc_get_min(struct ltc2978_data *data, struct i2c_client *client,
251 int page, int reg, u16 *pmin)
252 {
253 int ret;
254
255 ret = ltc_read_word_data(client, page, 0xff, reg);
256 if (ret >= 0) {
257 if (lin11_to_val(ret) < lin11_to_val(*pmin))
258 *pmin = ret;
259 ret = *pmin;
260 }
261 return ret;
262 }
263
ltc2978_read_word_data_common(struct i2c_client * client,int page,int reg)264 static int ltc2978_read_word_data_common(struct i2c_client *client, int page,
265 int reg)
266 {
267 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
268 struct ltc2978_data *data = to_ltc2978_data(info);
269 int ret;
270
271 switch (reg) {
272 case PMBUS_VIRT_READ_VIN_MAX:
273 ret = ltc_get_max(data, client, page, LTC2978_MFR_VIN_PEAK,
274 &data->vin_max);
275 break;
276 case PMBUS_VIRT_READ_VOUT_MAX:
277 ret = ltc_read_word_data(client, page, 0xff,
278 LTC2978_MFR_VOUT_PEAK);
279 if (ret >= 0) {
280 /*
281 * VOUT is 16 bit unsigned with fixed exponent,
282 * so we can compare it directly
283 */
284 if (ret > data->vout_max[page])
285 data->vout_max[page] = ret;
286 ret = data->vout_max[page];
287 }
288 break;
289 case PMBUS_VIRT_READ_TEMP_MAX:
290 ret = ltc_get_max(data, client, page,
291 LTC2978_MFR_TEMPERATURE_PEAK,
292 &data->temp_max[page]);
293 break;
294 case PMBUS_VIRT_RESET_VOUT_HISTORY:
295 case PMBUS_VIRT_RESET_VIN_HISTORY:
296 case PMBUS_VIRT_RESET_TEMP_HISTORY:
297 ret = 0;
298 break;
299 default:
300 ret = ltc_wait_ready(client);
301 if (ret < 0)
302 return ret;
303 ret = -ENODATA;
304 break;
305 }
306 return ret;
307 }
308
ltc2978_read_word_data(struct i2c_client * client,int page,int phase,int reg)309 static int ltc2978_read_word_data(struct i2c_client *client, int page,
310 int phase, int reg)
311 {
312 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
313 struct ltc2978_data *data = to_ltc2978_data(info);
314 int ret;
315
316 switch (reg) {
317 case PMBUS_VIRT_READ_VIN_MIN:
318 ret = ltc_get_min(data, client, page, LTC2978_MFR_VIN_MIN,
319 &data->vin_min);
320 break;
321 case PMBUS_VIRT_READ_VOUT_MIN:
322 ret = ltc_read_word_data(client, page, phase,
323 LTC2978_MFR_VOUT_MIN);
324 if (ret >= 0) {
325 /*
326 * VOUT_MIN is known to not be supported on some lots
327 * of LTC2978 revision 1, and will return the maximum
328 * possible voltage if read. If VOUT_MAX is valid and
329 * lower than the reading of VOUT_MIN, use it instead.
330 */
331 if (data->vout_max[page] && ret > data->vout_max[page])
332 ret = data->vout_max[page];
333 if (ret < data->vout_min[page])
334 data->vout_min[page] = ret;
335 ret = data->vout_min[page];
336 }
337 break;
338 case PMBUS_VIRT_READ_TEMP_MIN:
339 ret = ltc_get_min(data, client, page,
340 LTC2978_MFR_TEMPERATURE_MIN,
341 &data->temp_min[page]);
342 break;
343 case PMBUS_VIRT_READ_IOUT_MAX:
344 case PMBUS_VIRT_RESET_IOUT_HISTORY:
345 case PMBUS_VIRT_READ_TEMP2_MAX:
346 case PMBUS_VIRT_RESET_TEMP2_HISTORY:
347 ret = -ENXIO;
348 break;
349 default:
350 ret = ltc2978_read_word_data_common(client, page, reg);
351 break;
352 }
353 return ret;
354 }
355
ltc2974_read_word_data(struct i2c_client * client,int page,int phase,int reg)356 static int ltc2974_read_word_data(struct i2c_client *client, int page,
357 int phase, int reg)
358 {
359 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
360 struct ltc2978_data *data = to_ltc2978_data(info);
361 int ret;
362
363 switch (reg) {
364 case PMBUS_VIRT_READ_IOUT_MAX:
365 ret = ltc_get_max(data, client, page, LTC2974_MFR_IOUT_PEAK,
366 &data->iout_max[page]);
367 break;
368 case PMBUS_VIRT_READ_IOUT_MIN:
369 ret = ltc_get_min(data, client, page, LTC2974_MFR_IOUT_MIN,
370 &data->iout_min[page]);
371 break;
372 case PMBUS_VIRT_RESET_IOUT_HISTORY:
373 ret = 0;
374 break;
375 default:
376 ret = ltc2978_read_word_data(client, page, phase, reg);
377 break;
378 }
379 return ret;
380 }
381
ltc2975_read_word_data(struct i2c_client * client,int page,int phase,int reg)382 static int ltc2975_read_word_data(struct i2c_client *client, int page,
383 int phase, int reg)
384 {
385 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
386 struct ltc2978_data *data = to_ltc2978_data(info);
387 int ret;
388
389 switch (reg) {
390 case PMBUS_VIRT_READ_IIN_MAX:
391 ret = ltc_get_max(data, client, page, LTC2975_MFR_IIN_PEAK,
392 &data->iin_max);
393 break;
394 case PMBUS_VIRT_READ_IIN_MIN:
395 ret = ltc_get_min(data, client, page, LTC2975_MFR_IIN_MIN,
396 &data->iin_min);
397 break;
398 case PMBUS_VIRT_READ_PIN_MAX:
399 ret = ltc_get_max(data, client, page, LTC2975_MFR_PIN_PEAK,
400 &data->pin_max);
401 break;
402 case PMBUS_VIRT_READ_PIN_MIN:
403 ret = ltc_get_min(data, client, page, LTC2975_MFR_PIN_MIN,
404 &data->pin_min);
405 break;
406 case PMBUS_VIRT_RESET_IIN_HISTORY:
407 case PMBUS_VIRT_RESET_PIN_HISTORY:
408 ret = 0;
409 break;
410 default:
411 ret = ltc2978_read_word_data(client, page, phase, reg);
412 break;
413 }
414 return ret;
415 }
416
ltc3880_read_word_data(struct i2c_client * client,int page,int phase,int reg)417 static int ltc3880_read_word_data(struct i2c_client *client, int page,
418 int phase, int reg)
419 {
420 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
421 struct ltc2978_data *data = to_ltc2978_data(info);
422 int ret;
423
424 switch (reg) {
425 case PMBUS_VIRT_READ_IOUT_MAX:
426 ret = ltc_get_max(data, client, page, LTC3880_MFR_IOUT_PEAK,
427 &data->iout_max[page]);
428 break;
429 case PMBUS_VIRT_READ_TEMP2_MAX:
430 ret = ltc_get_max(data, client, page,
431 LTC3880_MFR_TEMPERATURE2_PEAK,
432 &data->temp2_max);
433 break;
434 case PMBUS_VIRT_READ_VIN_MIN:
435 case PMBUS_VIRT_READ_VOUT_MIN:
436 case PMBUS_VIRT_READ_TEMP_MIN:
437 ret = -ENXIO;
438 break;
439 case PMBUS_VIRT_RESET_IOUT_HISTORY:
440 case PMBUS_VIRT_RESET_TEMP2_HISTORY:
441 ret = 0;
442 break;
443 default:
444 ret = ltc2978_read_word_data_common(client, page, reg);
445 break;
446 }
447 return ret;
448 }
449
ltc3883_read_word_data(struct i2c_client * client,int page,int phase,int reg)450 static int ltc3883_read_word_data(struct i2c_client *client, int page,
451 int phase, int reg)
452 {
453 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
454 struct ltc2978_data *data = to_ltc2978_data(info);
455 int ret;
456
457 switch (reg) {
458 case PMBUS_VIRT_READ_IIN_MAX:
459 ret = ltc_get_max(data, client, page, LTC3883_MFR_IIN_PEAK,
460 &data->iin_max);
461 break;
462 case PMBUS_VIRT_RESET_IIN_HISTORY:
463 ret = 0;
464 break;
465 default:
466 ret = ltc3880_read_word_data(client, page, phase, reg);
467 break;
468 }
469 return ret;
470 }
471
ltc2978_clear_peaks(struct ltc2978_data * data,struct i2c_client * client,int page)472 static int ltc2978_clear_peaks(struct ltc2978_data *data,
473 struct i2c_client *client, int page)
474 {
475 int ret;
476
477 if (has_clear_peaks(data))
478 ret = ltc_write_byte(client, 0, LTC3880_MFR_CLEAR_PEAKS);
479 else
480 ret = ltc_write_byte(client, page, PMBUS_CLEAR_FAULTS);
481
482 return ret;
483 }
484
ltc2978_write_word_data(struct i2c_client * client,int page,int reg,u16 word)485 static int ltc2978_write_word_data(struct i2c_client *client, int page,
486 int reg, u16 word)
487 {
488 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
489 struct ltc2978_data *data = to_ltc2978_data(info);
490 int ret;
491
492 switch (reg) {
493 case PMBUS_VIRT_RESET_IIN_HISTORY:
494 data->iin_max = 0x7c00;
495 data->iin_min = 0x7bff;
496 ret = ltc2978_clear_peaks(data, client, 0);
497 break;
498 case PMBUS_VIRT_RESET_PIN_HISTORY:
499 data->pin_max = 0x7c00;
500 data->pin_min = 0x7bff;
501 ret = ltc2978_clear_peaks(data, client, 0);
502 break;
503 case PMBUS_VIRT_RESET_IOUT_HISTORY:
504 data->iout_max[page] = 0x7c00;
505 data->iout_min[page] = 0xfbff;
506 ret = ltc2978_clear_peaks(data, client, page);
507 break;
508 case PMBUS_VIRT_RESET_TEMP2_HISTORY:
509 data->temp2_max = 0x7c00;
510 ret = ltc2978_clear_peaks(data, client, page);
511 break;
512 case PMBUS_VIRT_RESET_VOUT_HISTORY:
513 data->vout_min[page] = 0xffff;
514 data->vout_max[page] = 0;
515 ret = ltc2978_clear_peaks(data, client, page);
516 break;
517 case PMBUS_VIRT_RESET_VIN_HISTORY:
518 data->vin_min = 0x7bff;
519 data->vin_max = 0x7c00;
520 ret = ltc2978_clear_peaks(data, client, page);
521 break;
522 case PMBUS_VIRT_RESET_TEMP_HISTORY:
523 data->temp_min[page] = 0x7bff;
524 data->temp_max[page] = 0x7c00;
525 ret = ltc2978_clear_peaks(data, client, page);
526 break;
527 default:
528 ret = ltc_wait_ready(client);
529 if (ret < 0)
530 return ret;
531 ret = -ENODATA;
532 break;
533 }
534 return ret;
535 }
536
537 static const struct i2c_device_id ltc2978_id[] = {
538 {"ltc2972", ltc2972},
539 {"ltc2974", ltc2974},
540 {"ltc2975", ltc2975},
541 {"ltc2977", ltc2977},
542 {"ltc2978", ltc2978},
543 {"ltc2979", ltc2979},
544 {"ltc2980", ltc2980},
545 {"ltc3880", ltc3880},
546 {"ltc3882", ltc3882},
547 {"ltc3883", ltc3883},
548 {"ltc3884", ltc3884},
549 {"ltc3886", ltc3886},
550 {"ltc3887", ltc3887},
551 {"ltc3889", ltc3889},
552 {"ltc7132", ltc7132},
553 {"ltc7841", ltc7841},
554 {"ltc7880", ltc7880},
555 {"ltm2987", ltm2987},
556 {"ltm4664", ltm4664},
557 {"ltm4675", ltm4675},
558 {"ltm4676", ltm4676},
559 {"ltm4677", ltm4677},
560 {"ltm4678", ltm4678},
561 {"ltm4680", ltm4680},
562 {"ltm4686", ltm4686},
563 {"ltm4700", ltm4700},
564 {}
565 };
566 MODULE_DEVICE_TABLE(i2c, ltc2978_id);
567
568 #if IS_ENABLED(CONFIG_SENSORS_LTC2978_REGULATOR)
569 #define LTC2978_ADC_RES 0xFFFF
570 #define LTC2978_N_ADC 122
571 #define LTC2978_MAX_UV (LTC2978_ADC_RES * LTC2978_N_ADC)
572 #define LTC2978_UV_STEP 1000
573 #define LTC2978_N_VOLTAGES ((LTC2978_MAX_UV / LTC2978_UV_STEP) + 1)
574
575 static const struct regulator_desc ltc2978_reg_desc[] = {
576 PMBUS_REGULATOR_STEP("vout", 0, LTC2978_N_VOLTAGES, LTC2978_UV_STEP, 0),
577 PMBUS_REGULATOR_STEP("vout", 1, LTC2978_N_VOLTAGES, LTC2978_UV_STEP, 0),
578 PMBUS_REGULATOR_STEP("vout", 2, LTC2978_N_VOLTAGES, LTC2978_UV_STEP, 0),
579 PMBUS_REGULATOR_STEP("vout", 3, LTC2978_N_VOLTAGES, LTC2978_UV_STEP, 0),
580 PMBUS_REGULATOR_STEP("vout", 4, LTC2978_N_VOLTAGES, LTC2978_UV_STEP, 0),
581 PMBUS_REGULATOR_STEP("vout", 5, LTC2978_N_VOLTAGES, LTC2978_UV_STEP, 0),
582 PMBUS_REGULATOR_STEP("vout", 6, LTC2978_N_VOLTAGES, LTC2978_UV_STEP, 0),
583 PMBUS_REGULATOR_STEP("vout", 7, LTC2978_N_VOLTAGES, LTC2978_UV_STEP, 0),
584 };
585
586 static const struct regulator_desc ltc2978_reg_desc_default[] = {
587 PMBUS_REGULATOR("vout", 0),
588 PMBUS_REGULATOR("vout", 1),
589 PMBUS_REGULATOR("vout", 2),
590 PMBUS_REGULATOR("vout", 3),
591 PMBUS_REGULATOR("vout", 4),
592 PMBUS_REGULATOR("vout", 5),
593 PMBUS_REGULATOR("vout", 6),
594 PMBUS_REGULATOR("vout", 7),
595 };
596 #endif /* CONFIG_SENSORS_LTC2978_REGULATOR */
597
ltc2978_get_id(struct i2c_client * client)598 static int ltc2978_get_id(struct i2c_client *client)
599 {
600 int chip_id;
601
602 chip_id = i2c_smbus_read_word_data(client, LTC2978_MFR_SPECIAL_ID);
603 if (chip_id < 0) {
604 const struct i2c_device_id *id;
605 u8 buf[I2C_SMBUS_BLOCK_MAX];
606 int ret;
607
608 if (!i2c_check_functionality(client->adapter,
609 I2C_FUNC_SMBUS_READ_BLOCK_DATA))
610 return -ENODEV;
611
612 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
613 if (ret < 0)
614 return ret;
615 if (ret < 3 || strncmp(buf, "LTC", 3))
616 return -ENODEV;
617
618 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
619 if (ret < 0)
620 return ret;
621 for (id = <c2978_id[0]; strlen(id->name); id++) {
622 if (!strncasecmp(id->name, buf, strlen(id->name)))
623 return (int)id->driver_data;
624 }
625 return -ENODEV;
626 }
627
628 chip_id &= LTC2978_ID_MASK;
629
630 if (chip_id == LTC2972_ID)
631 return ltc2972;
632 else if (chip_id == LTC2974_ID)
633 return ltc2974;
634 else if (chip_id == LTC2975_ID)
635 return ltc2975;
636 else if (chip_id == LTC2977_ID)
637 return ltc2977;
638 else if (chip_id == LTC2978_ID_REV1 || chip_id == LTC2978_ID_REV2)
639 return ltc2978;
640 else if (chip_id == LTC2979_ID_A || chip_id == LTC2979_ID_B)
641 return ltc2979;
642 else if (chip_id == LTC2980_ID_A || chip_id == LTC2980_ID_B)
643 return ltc2980;
644 else if (chip_id == LTC3880_ID)
645 return ltc3880;
646 else if (chip_id == LTC3882_ID || chip_id == LTC3882_ID_D1)
647 return ltc3882;
648 else if (chip_id == LTC3883_ID)
649 return ltc3883;
650 else if (chip_id == LTC3884_ID)
651 return ltc3884;
652 else if (chip_id == LTC3886_ID)
653 return ltc3886;
654 else if (chip_id == LTC3887_ID)
655 return ltc3887;
656 else if (chip_id == LTC3889_ID)
657 return ltc3889;
658 else if (chip_id == LTC7132_ID)
659 return ltc7132;
660 else if (chip_id == LTC7841_ID)
661 return ltc7841;
662 else if (chip_id == LTC7880_ID)
663 return ltc7880;
664 else if (chip_id == LTM2987_ID_A || chip_id == LTM2987_ID_B)
665 return ltm2987;
666 else if (chip_id == LTM4664_ID)
667 return ltm4664;
668 else if (chip_id == LTM4675_ID)
669 return ltm4675;
670 else if (chip_id == LTM4676_ID_REV1 || chip_id == LTM4676_ID_REV2 ||
671 chip_id == LTM4676A_ID)
672 return ltm4676;
673 else if (chip_id == LTM4677_ID_REV1 || chip_id == LTM4677_ID_REV2)
674 return ltm4677;
675 else if (chip_id == LTM4678_ID_REV1 || chip_id == LTM4678_ID_REV2)
676 return ltm4678;
677 else if (chip_id == LTM4680_ID)
678 return ltm4680;
679 else if (chip_id == LTM4686_ID)
680 return ltm4686;
681 else if (chip_id == LTM4700_ID)
682 return ltm4700;
683
684 dev_err(&client->dev, "Unsupported chip ID 0x%x\n", chip_id);
685 return -ENODEV;
686 }
687
ltc2978_probe(struct i2c_client * client)688 static int ltc2978_probe(struct i2c_client *client)
689 {
690 int i, chip_id;
691 struct ltc2978_data *data;
692 struct pmbus_driver_info *info;
693 const struct i2c_device_id *id;
694
695 if (!i2c_check_functionality(client->adapter,
696 I2C_FUNC_SMBUS_READ_WORD_DATA))
697 return -ENODEV;
698
699 data = devm_kzalloc(&client->dev, sizeof(struct ltc2978_data),
700 GFP_KERNEL);
701 if (!data)
702 return -ENOMEM;
703
704 chip_id = ltc2978_get_id(client);
705 if (chip_id < 0)
706 return chip_id;
707
708 data->id = chip_id;
709 id = i2c_match_id(ltc2978_id, client);
710 if (data->id != id->driver_data)
711 dev_warn(&client->dev,
712 "Device mismatch: Configured %s (%d), detected %d\n",
713 id->name,
714 (int) id->driver_data,
715 chip_id);
716
717 info = &data->info;
718 info->write_word_data = ltc2978_write_word_data;
719 info->write_byte = ltc_write_byte;
720 info->write_byte_data = ltc_write_byte_data;
721 info->read_word_data = ltc_read_word_data;
722 info->read_byte_data = ltc_read_byte_data;
723
724 data->vin_min = 0x7bff;
725 data->vin_max = 0x7c00;
726 for (i = 0; i < ARRAY_SIZE(data->vout_min); i++)
727 data->vout_min[i] = 0xffff;
728 for (i = 0; i < ARRAY_SIZE(data->iout_min); i++)
729 data->iout_min[i] = 0xfbff;
730 for (i = 0; i < ARRAY_SIZE(data->iout_max); i++)
731 data->iout_max[i] = 0x7c00;
732 for (i = 0; i < ARRAY_SIZE(data->temp_min); i++)
733 data->temp_min[i] = 0x7bff;
734 for (i = 0; i < ARRAY_SIZE(data->temp_max); i++)
735 data->temp_max[i] = 0x7c00;
736 data->temp2_max = 0x7c00;
737
738 switch (data->id) {
739 case ltc2972:
740 info->read_word_data = ltc2975_read_word_data;
741 info->pages = LTC2972_NUM_PAGES;
742 info->func[0] = PMBUS_HAVE_IIN | PMBUS_HAVE_PIN
743 | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
744 | PMBUS_HAVE_TEMP2;
745 for (i = 0; i < info->pages; i++) {
746 info->func[i] |= PMBUS_HAVE_VOUT
747 | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
748 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
749 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
750 }
751 break;
752 case ltc2974:
753 info->read_word_data = ltc2974_read_word_data;
754 info->pages = LTC2974_NUM_PAGES;
755 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
756 | PMBUS_HAVE_TEMP2;
757 for (i = 0; i < info->pages; i++) {
758 info->func[i] |= PMBUS_HAVE_VOUT
759 | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
760 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
761 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
762 }
763 break;
764 case ltc2975:
765 info->read_word_data = ltc2975_read_word_data;
766 info->pages = LTC2974_NUM_PAGES;
767 info->func[0] = PMBUS_HAVE_IIN | PMBUS_HAVE_PIN
768 | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
769 | PMBUS_HAVE_TEMP2;
770 for (i = 0; i < info->pages; i++) {
771 info->func[i] |= PMBUS_HAVE_VOUT
772 | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
773 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
774 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
775 }
776 break;
777
778 case ltc2977:
779 case ltc2978:
780 case ltc2979:
781 case ltc2980:
782 case ltm2987:
783 info->read_word_data = ltc2978_read_word_data;
784 info->pages = LTC2978_NUM_PAGES;
785 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
786 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
787 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
788 for (i = 1; i < LTC2978_NUM_PAGES; i++) {
789 info->func[i] = PMBUS_HAVE_VOUT
790 | PMBUS_HAVE_STATUS_VOUT;
791 }
792 break;
793 case ltc3880:
794 case ltc3887:
795 case ltm4675:
796 case ltm4676:
797 case ltm4677:
798 case ltm4686:
799 data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
800 info->read_word_data = ltc3880_read_word_data;
801 info->pages = LTC3880_NUM_PAGES;
802 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
803 | PMBUS_HAVE_STATUS_INPUT
804 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
805 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
806 | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
807 | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
808 info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
809 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
810 | PMBUS_HAVE_POUT
811 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
812 break;
813 case ltc3882:
814 data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
815 info->read_word_data = ltc3880_read_word_data;
816 info->pages = LTC3880_NUM_PAGES;
817 info->func[0] = PMBUS_HAVE_VIN
818 | PMBUS_HAVE_STATUS_INPUT
819 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
820 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
821 | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
822 | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
823 info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
824 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
825 | PMBUS_HAVE_POUT
826 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
827 break;
828 case ltc3883:
829 data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
830 info->read_word_data = ltc3883_read_word_data;
831 info->pages = LTC3883_NUM_PAGES;
832 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
833 | PMBUS_HAVE_STATUS_INPUT
834 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
835 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
836 | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
837 | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
838 break;
839 case ltc3884:
840 case ltc3886:
841 case ltc3889:
842 case ltc7132:
843 case ltc7880:
844 case ltm4664:
845 case ltm4678:
846 case ltm4680:
847 case ltm4700:
848 data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
849 info->read_word_data = ltc3883_read_word_data;
850 info->pages = LTC3880_NUM_PAGES;
851 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
852 | PMBUS_HAVE_STATUS_INPUT
853 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
854 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
855 | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
856 | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
857 info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
858 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
859 | PMBUS_HAVE_POUT
860 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
861 break;
862 case ltc7841:
863 data->features |= FEAT_CLEAR_PEAKS;
864 info->read_word_data = ltc3883_read_word_data;
865 info->pages = LTC3883_NUM_PAGES;
866 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
867 | PMBUS_HAVE_STATUS_INPUT
868 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
869 | PMBUS_HAVE_IOUT
870 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
871 break;
872 default:
873 return -ENODEV;
874 }
875
876 #if IS_ENABLED(CONFIG_SENSORS_LTC2978_REGULATOR)
877 info->num_regulators = info->pages;
878 switch (data->id) {
879 case ltc2972:
880 case ltc2974:
881 case ltc2975:
882 case ltc2977:
883 case ltc2978:
884 case ltc2979:
885 case ltc2980:
886 case ltm2987:
887 info->reg_desc = ltc2978_reg_desc;
888 if (info->num_regulators > ARRAY_SIZE(ltc2978_reg_desc)) {
889 dev_warn(&client->dev, "num_regulators too large!");
890 info->num_regulators = ARRAY_SIZE(ltc2978_reg_desc);
891 }
892 break;
893 default:
894 info->reg_desc = ltc2978_reg_desc_default;
895 if (info->num_regulators > ARRAY_SIZE(ltc2978_reg_desc_default)) {
896 dev_warn(&client->dev, "num_regulators too large!");
897 info->num_regulators =
898 ARRAY_SIZE(ltc2978_reg_desc_default);
899 }
900 break;
901 }
902 #endif
903
904 return pmbus_do_probe(client, info);
905 }
906
907
908 #ifdef CONFIG_OF
909 static const struct of_device_id ltc2978_of_match[] = {
910 { .compatible = "lltc,ltc2972" },
911 { .compatible = "lltc,ltc2974" },
912 { .compatible = "lltc,ltc2975" },
913 { .compatible = "lltc,ltc2977" },
914 { .compatible = "lltc,ltc2978" },
915 { .compatible = "lltc,ltc2979" },
916 { .compatible = "lltc,ltc2980" },
917 { .compatible = "lltc,ltc3880" },
918 { .compatible = "lltc,ltc3882" },
919 { .compatible = "lltc,ltc3883" },
920 { .compatible = "lltc,ltc3884" },
921 { .compatible = "lltc,ltc3886" },
922 { .compatible = "lltc,ltc3887" },
923 { .compatible = "lltc,ltc3889" },
924 { .compatible = "lltc,ltc7132" },
925 { .compatible = "lltc,ltc7841" },
926 { .compatible = "lltc,ltc7880" },
927 { .compatible = "lltc,ltm2987" },
928 { .compatible = "lltc,ltm4664" },
929 { .compatible = "lltc,ltm4675" },
930 { .compatible = "lltc,ltm4676" },
931 { .compatible = "lltc,ltm4677" },
932 { .compatible = "lltc,ltm4678" },
933 { .compatible = "lltc,ltm4680" },
934 { .compatible = "lltc,ltm4686" },
935 { .compatible = "lltc,ltm4700" },
936 { }
937 };
938 MODULE_DEVICE_TABLE(of, ltc2978_of_match);
939 #endif
940
941 static struct i2c_driver ltc2978_driver = {
942 .driver = {
943 .name = "ltc2978",
944 .of_match_table = of_match_ptr(ltc2978_of_match),
945 },
946 .probe = ltc2978_probe,
947 .id_table = ltc2978_id,
948 };
949
950 module_i2c_driver(ltc2978_driver);
951
952 MODULE_AUTHOR("Guenter Roeck");
953 MODULE_DESCRIPTION("PMBus driver for LTC2978 and compatible chips");
954 MODULE_LICENSE("GPL");
955 MODULE_IMPORT_NS("PMBUS");
956