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