xref: /openbmc/linux/drivers/hwmon/pmbus/ltc2978.c (revision 76426e23)
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(struct i2c_client *client, int page, u8 byte)
200 {
201 	int ret;
202 
203 	ret = ltc_wait_ready(client);
204 	if (ret < 0)
205 		return ret;
206 
207 	return pmbus_write_byte(client, page, byte);
208 }
209 
210 static inline int lin11_to_val(int data)
211 {
212 	s16 e = ((s16)data) >> 11;
213 	s32 m = (((s16)(data << 5)) >> 5);
214 
215 	/*
216 	 * mantissa is 10 bit + sign, exponent adds up to 15 bit.
217 	 * Add 6 bit to exponent for maximum accuracy (10 + 15 + 6 = 31).
218 	 */
219 	e += 6;
220 	return (e < 0 ? m >> -e : m << e);
221 }
222 
223 static int ltc_get_max(struct ltc2978_data *data, struct i2c_client *client,
224 		       int page, int reg, u16 *pmax)
225 {
226 	int ret;
227 
228 	ret = ltc_read_word_data(client, page, 0xff, reg);
229 	if (ret >= 0) {
230 		if (lin11_to_val(ret) > lin11_to_val(*pmax))
231 			*pmax = ret;
232 		ret = *pmax;
233 	}
234 	return ret;
235 }
236 
237 static int ltc_get_min(struct ltc2978_data *data, struct i2c_client *client,
238 		       int page, int reg, u16 *pmin)
239 {
240 	int ret;
241 
242 	ret = ltc_read_word_data(client, page, 0xff, reg);
243 	if (ret >= 0) {
244 		if (lin11_to_val(ret) < lin11_to_val(*pmin))
245 			*pmin = ret;
246 		ret = *pmin;
247 	}
248 	return ret;
249 }
250 
251 static int ltc2978_read_word_data_common(struct i2c_client *client, int page,
252 					 int reg)
253 {
254 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
255 	struct ltc2978_data *data = to_ltc2978_data(info);
256 	int ret;
257 
258 	switch (reg) {
259 	case PMBUS_VIRT_READ_VIN_MAX:
260 		ret = ltc_get_max(data, client, page, LTC2978_MFR_VIN_PEAK,
261 				  &data->vin_max);
262 		break;
263 	case PMBUS_VIRT_READ_VOUT_MAX:
264 		ret = ltc_read_word_data(client, page, 0xff,
265 					 LTC2978_MFR_VOUT_PEAK);
266 		if (ret >= 0) {
267 			/*
268 			 * VOUT is 16 bit unsigned with fixed exponent,
269 			 * so we can compare it directly
270 			 */
271 			if (ret > data->vout_max[page])
272 				data->vout_max[page] = ret;
273 			ret = data->vout_max[page];
274 		}
275 		break;
276 	case PMBUS_VIRT_READ_TEMP_MAX:
277 		ret = ltc_get_max(data, client, page,
278 				  LTC2978_MFR_TEMPERATURE_PEAK,
279 				  &data->temp_max[page]);
280 		break;
281 	case PMBUS_VIRT_RESET_VOUT_HISTORY:
282 	case PMBUS_VIRT_RESET_VIN_HISTORY:
283 	case PMBUS_VIRT_RESET_TEMP_HISTORY:
284 		ret = 0;
285 		break;
286 	default:
287 		ret = ltc_wait_ready(client);
288 		if (ret < 0)
289 			return ret;
290 		ret = -ENODATA;
291 		break;
292 	}
293 	return ret;
294 }
295 
296 static int ltc2978_read_word_data(struct i2c_client *client, int page,
297 				  int phase, int reg)
298 {
299 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
300 	struct ltc2978_data *data = to_ltc2978_data(info);
301 	int ret;
302 
303 	switch (reg) {
304 	case PMBUS_VIRT_READ_VIN_MIN:
305 		ret = ltc_get_min(data, client, page, LTC2978_MFR_VIN_MIN,
306 				  &data->vin_min);
307 		break;
308 	case PMBUS_VIRT_READ_VOUT_MIN:
309 		ret = ltc_read_word_data(client, page, phase,
310 					 LTC2978_MFR_VOUT_MIN);
311 		if (ret >= 0) {
312 			/*
313 			 * VOUT_MIN is known to not be supported on some lots
314 			 * of LTC2978 revision 1, and will return the maximum
315 			 * possible voltage if read. If VOUT_MAX is valid and
316 			 * lower than the reading of VOUT_MIN, use it instead.
317 			 */
318 			if (data->vout_max[page] && ret > data->vout_max[page])
319 				ret = data->vout_max[page];
320 			if (ret < data->vout_min[page])
321 				data->vout_min[page] = ret;
322 			ret = data->vout_min[page];
323 		}
324 		break;
325 	case PMBUS_VIRT_READ_TEMP_MIN:
326 		ret = ltc_get_min(data, client, page,
327 				  LTC2978_MFR_TEMPERATURE_MIN,
328 				  &data->temp_min[page]);
329 		break;
330 	case PMBUS_VIRT_READ_IOUT_MAX:
331 	case PMBUS_VIRT_RESET_IOUT_HISTORY:
332 	case PMBUS_VIRT_READ_TEMP2_MAX:
333 	case PMBUS_VIRT_RESET_TEMP2_HISTORY:
334 		ret = -ENXIO;
335 		break;
336 	default:
337 		ret = ltc2978_read_word_data_common(client, page, reg);
338 		break;
339 	}
340 	return ret;
341 }
342 
343 static int ltc2974_read_word_data(struct i2c_client *client, int page,
344 				  int phase, int reg)
345 {
346 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
347 	struct ltc2978_data *data = to_ltc2978_data(info);
348 	int ret;
349 
350 	switch (reg) {
351 	case PMBUS_VIRT_READ_IOUT_MAX:
352 		ret = ltc_get_max(data, client, page, LTC2974_MFR_IOUT_PEAK,
353 				  &data->iout_max[page]);
354 		break;
355 	case PMBUS_VIRT_READ_IOUT_MIN:
356 		ret = ltc_get_min(data, client, page, LTC2974_MFR_IOUT_MIN,
357 				  &data->iout_min[page]);
358 		break;
359 	case PMBUS_VIRT_RESET_IOUT_HISTORY:
360 		ret = 0;
361 		break;
362 	default:
363 		ret = ltc2978_read_word_data(client, page, phase, reg);
364 		break;
365 	}
366 	return ret;
367 }
368 
369 static int ltc2975_read_word_data(struct i2c_client *client, int page,
370 				  int phase, int reg)
371 {
372 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
373 	struct ltc2978_data *data = to_ltc2978_data(info);
374 	int ret;
375 
376 	switch (reg) {
377 	case PMBUS_VIRT_READ_IIN_MAX:
378 		ret = ltc_get_max(data, client, page, LTC2975_MFR_IIN_PEAK,
379 				  &data->iin_max);
380 		break;
381 	case PMBUS_VIRT_READ_IIN_MIN:
382 		ret = ltc_get_min(data, client, page, LTC2975_MFR_IIN_MIN,
383 				  &data->iin_min);
384 		break;
385 	case PMBUS_VIRT_READ_PIN_MAX:
386 		ret = ltc_get_max(data, client, page, LTC2975_MFR_PIN_PEAK,
387 				  &data->pin_max);
388 		break;
389 	case PMBUS_VIRT_READ_PIN_MIN:
390 		ret = ltc_get_min(data, client, page, LTC2975_MFR_PIN_MIN,
391 				  &data->pin_min);
392 		break;
393 	case PMBUS_VIRT_RESET_IIN_HISTORY:
394 	case PMBUS_VIRT_RESET_PIN_HISTORY:
395 		ret = 0;
396 		break;
397 	default:
398 		ret = ltc2978_read_word_data(client, page, phase, reg);
399 		break;
400 	}
401 	return ret;
402 }
403 
404 static int ltc3880_read_word_data(struct i2c_client *client, int page,
405 				  int phase, int reg)
406 {
407 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
408 	struct ltc2978_data *data = to_ltc2978_data(info);
409 	int ret;
410 
411 	switch (reg) {
412 	case PMBUS_VIRT_READ_IOUT_MAX:
413 		ret = ltc_get_max(data, client, page, LTC3880_MFR_IOUT_PEAK,
414 				  &data->iout_max[page]);
415 		break;
416 	case PMBUS_VIRT_READ_TEMP2_MAX:
417 		ret = ltc_get_max(data, client, page,
418 				  LTC3880_MFR_TEMPERATURE2_PEAK,
419 				  &data->temp2_max);
420 		break;
421 	case PMBUS_VIRT_READ_VIN_MIN:
422 	case PMBUS_VIRT_READ_VOUT_MIN:
423 	case PMBUS_VIRT_READ_TEMP_MIN:
424 		ret = -ENXIO;
425 		break;
426 	case PMBUS_VIRT_RESET_IOUT_HISTORY:
427 	case PMBUS_VIRT_RESET_TEMP2_HISTORY:
428 		ret = 0;
429 		break;
430 	default:
431 		ret = ltc2978_read_word_data_common(client, page, reg);
432 		break;
433 	}
434 	return ret;
435 }
436 
437 static int ltc3883_read_word_data(struct i2c_client *client, int page,
438 				  int phase, int reg)
439 {
440 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
441 	struct ltc2978_data *data = to_ltc2978_data(info);
442 	int ret;
443 
444 	switch (reg) {
445 	case PMBUS_VIRT_READ_IIN_MAX:
446 		ret = ltc_get_max(data, client, page, LTC3883_MFR_IIN_PEAK,
447 				  &data->iin_max);
448 		break;
449 	case PMBUS_VIRT_RESET_IIN_HISTORY:
450 		ret = 0;
451 		break;
452 	default:
453 		ret = ltc3880_read_word_data(client, page, phase, reg);
454 		break;
455 	}
456 	return ret;
457 }
458 
459 static int ltc2978_clear_peaks(struct ltc2978_data *data,
460 			       struct i2c_client *client, int page)
461 {
462 	int ret;
463 
464 	if (has_clear_peaks(data))
465 		ret = ltc_write_byte(client, 0, LTC3880_MFR_CLEAR_PEAKS);
466 	else
467 		ret = ltc_write_byte(client, page, PMBUS_CLEAR_FAULTS);
468 
469 	return ret;
470 }
471 
472 static int ltc2978_write_word_data(struct i2c_client *client, int page,
473 				    int reg, u16 word)
474 {
475 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
476 	struct ltc2978_data *data = to_ltc2978_data(info);
477 	int ret;
478 
479 	switch (reg) {
480 	case PMBUS_VIRT_RESET_IIN_HISTORY:
481 		data->iin_max = 0x7c00;
482 		data->iin_min = 0x7bff;
483 		ret = ltc2978_clear_peaks(data, client, 0);
484 		break;
485 	case PMBUS_VIRT_RESET_PIN_HISTORY:
486 		data->pin_max = 0x7c00;
487 		data->pin_min = 0x7bff;
488 		ret = ltc2978_clear_peaks(data, client, 0);
489 		break;
490 	case PMBUS_VIRT_RESET_IOUT_HISTORY:
491 		data->iout_max[page] = 0x7c00;
492 		data->iout_min[page] = 0xfbff;
493 		ret = ltc2978_clear_peaks(data, client, page);
494 		break;
495 	case PMBUS_VIRT_RESET_TEMP2_HISTORY:
496 		data->temp2_max = 0x7c00;
497 		ret = ltc2978_clear_peaks(data, client, page);
498 		break;
499 	case PMBUS_VIRT_RESET_VOUT_HISTORY:
500 		data->vout_min[page] = 0xffff;
501 		data->vout_max[page] = 0;
502 		ret = ltc2978_clear_peaks(data, client, page);
503 		break;
504 	case PMBUS_VIRT_RESET_VIN_HISTORY:
505 		data->vin_min = 0x7bff;
506 		data->vin_max = 0x7c00;
507 		ret = ltc2978_clear_peaks(data, client, page);
508 		break;
509 	case PMBUS_VIRT_RESET_TEMP_HISTORY:
510 		data->temp_min[page] = 0x7bff;
511 		data->temp_max[page] = 0x7c00;
512 		ret = ltc2978_clear_peaks(data, client, page);
513 		break;
514 	default:
515 		ret = ltc_wait_ready(client);
516 		if (ret < 0)
517 			return ret;
518 		ret = -ENODATA;
519 		break;
520 	}
521 	return ret;
522 }
523 
524 static const struct i2c_device_id ltc2978_id[] = {
525 	{"ltc2972", ltc2972},
526 	{"ltc2974", ltc2974},
527 	{"ltc2975", ltc2975},
528 	{"ltc2977", ltc2977},
529 	{"ltc2978", ltc2978},
530 	{"ltc2979", ltc2979},
531 	{"ltc2980", ltc2980},
532 	{"ltc3880", ltc3880},
533 	{"ltc3882", ltc3882},
534 	{"ltc3883", ltc3883},
535 	{"ltc3884", ltc3884},
536 	{"ltc3886", ltc3886},
537 	{"ltc3887", ltc3887},
538 	{"ltc3889", ltc3889},
539 	{"ltc7880", ltc7880},
540 	{"ltm2987", ltm2987},
541 	{"ltm4664", ltm4664},
542 	{"ltm4675", ltm4675},
543 	{"ltm4676", ltm4676},
544 	{"ltm4677", ltm4677},
545 	{"ltm4678", ltm4678},
546 	{"ltm4680", ltm4680},
547 	{"ltm4686", ltm4686},
548 	{"ltm4700", ltm4700},
549 	{}
550 };
551 MODULE_DEVICE_TABLE(i2c, ltc2978_id);
552 
553 #if IS_ENABLED(CONFIG_SENSORS_LTC2978_REGULATOR)
554 static const struct regulator_desc ltc2978_reg_desc[] = {
555 	PMBUS_REGULATOR("vout", 0),
556 	PMBUS_REGULATOR("vout", 1),
557 	PMBUS_REGULATOR("vout", 2),
558 	PMBUS_REGULATOR("vout", 3),
559 	PMBUS_REGULATOR("vout", 4),
560 	PMBUS_REGULATOR("vout", 5),
561 	PMBUS_REGULATOR("vout", 6),
562 	PMBUS_REGULATOR("vout", 7),
563 };
564 #endif /* CONFIG_SENSORS_LTC2978_REGULATOR */
565 
566 static int ltc2978_get_id(struct i2c_client *client)
567 {
568 	int chip_id;
569 
570 	chip_id = i2c_smbus_read_word_data(client, LTC2978_MFR_SPECIAL_ID);
571 	if (chip_id < 0) {
572 		const struct i2c_device_id *id;
573 		u8 buf[I2C_SMBUS_BLOCK_MAX];
574 		int ret;
575 
576 		if (!i2c_check_functionality(client->adapter,
577 					     I2C_FUNC_SMBUS_READ_BLOCK_DATA))
578 			return -ENODEV;
579 
580 		ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
581 		if (ret < 0)
582 			return ret;
583 		if (ret < 3 || strncmp(buf, "LTC", 3))
584 			return -ENODEV;
585 
586 		ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
587 		if (ret < 0)
588 			return ret;
589 		for (id = &ltc2978_id[0]; strlen(id->name); id++) {
590 			if (!strncasecmp(id->name, buf, strlen(id->name)))
591 				return (int)id->driver_data;
592 		}
593 		return -ENODEV;
594 	}
595 
596 	chip_id &= LTC2978_ID_MASK;
597 
598 	if (chip_id == LTC2972_ID)
599 		return ltc2972;
600 	else if (chip_id == LTC2974_ID)
601 		return ltc2974;
602 	else if (chip_id == LTC2975_ID)
603 		return ltc2975;
604 	else if (chip_id == LTC2977_ID)
605 		return ltc2977;
606 	else if (chip_id == LTC2978_ID_REV1 || chip_id == LTC2978_ID_REV2)
607 		return ltc2978;
608 	else if (chip_id == LTC2979_ID_A || chip_id == LTC2979_ID_B)
609 		return ltc2979;
610 	else if (chip_id == LTC2980_ID_A || chip_id == LTC2980_ID_B)
611 		return ltc2980;
612 	else if (chip_id == LTC3880_ID)
613 		return ltc3880;
614 	else if (chip_id == LTC3882_ID || chip_id == LTC3882_ID_D1)
615 		return ltc3882;
616 	else if (chip_id == LTC3883_ID)
617 		return ltc3883;
618 	else if (chip_id == LTC3884_ID)
619 		return ltc3884;
620 	else if (chip_id == LTC3886_ID)
621 		return ltc3886;
622 	else if (chip_id == LTC3887_ID)
623 		return ltc3887;
624 	else if (chip_id == LTC3889_ID)
625 		return ltc3889;
626 	else if (chip_id == LTC7880_ID)
627 		return ltc7880;
628 	else if (chip_id == LTM2987_ID_A || chip_id == LTM2987_ID_B)
629 		return ltm2987;
630 	else if (chip_id == LTM4664_ID)
631 		return ltm4664;
632 	else if (chip_id == LTM4675_ID)
633 		return ltm4675;
634 	else if (chip_id == LTM4676_ID_REV1 || chip_id == LTM4676_ID_REV2 ||
635 		 chip_id == LTM4676A_ID)
636 		return ltm4676;
637 	else if (chip_id == LTM4677_ID_REV1 || chip_id == LTM4677_ID_REV2)
638 		return ltm4677;
639 	else if (chip_id == LTM4678_ID_REV1 || chip_id == LTM4678_ID_REV2)
640 		return ltm4678;
641 	else if (chip_id == LTM4680_ID)
642 		return ltm4680;
643 	else if (chip_id == LTM4686_ID)
644 		return ltm4686;
645 	else if (chip_id == LTM4700_ID)
646 		return ltm4700;
647 
648 	dev_err(&client->dev, "Unsupported chip ID 0x%x\n", chip_id);
649 	return -ENODEV;
650 }
651 
652 static int ltc2978_probe(struct i2c_client *client,
653 			 const struct i2c_device_id *id)
654 {
655 	int i, chip_id;
656 	struct ltc2978_data *data;
657 	struct pmbus_driver_info *info;
658 
659 	if (!i2c_check_functionality(client->adapter,
660 				     I2C_FUNC_SMBUS_READ_WORD_DATA))
661 		return -ENODEV;
662 
663 	data = devm_kzalloc(&client->dev, sizeof(struct ltc2978_data),
664 			    GFP_KERNEL);
665 	if (!data)
666 		return -ENOMEM;
667 
668 	chip_id = ltc2978_get_id(client);
669 	if (chip_id < 0)
670 		return chip_id;
671 
672 	data->id = chip_id;
673 	if (data->id != id->driver_data)
674 		dev_warn(&client->dev,
675 			 "Device mismatch: Configured %s, detected %s\n",
676 			 id->name,
677 			 ltc2978_id[data->id].name);
678 
679 	info = &data->info;
680 	info->write_word_data = ltc2978_write_word_data;
681 	info->write_byte = ltc_write_byte;
682 	info->read_word_data = ltc_read_word_data;
683 	info->read_byte_data = ltc_read_byte_data;
684 
685 	data->vin_min = 0x7bff;
686 	data->vin_max = 0x7c00;
687 	for (i = 0; i < ARRAY_SIZE(data->vout_min); i++)
688 		data->vout_min[i] = 0xffff;
689 	for (i = 0; i < ARRAY_SIZE(data->iout_min); i++)
690 		data->iout_min[i] = 0xfbff;
691 	for (i = 0; i < ARRAY_SIZE(data->iout_max); i++)
692 		data->iout_max[i] = 0x7c00;
693 	for (i = 0; i < ARRAY_SIZE(data->temp_min); i++)
694 		data->temp_min[i] = 0x7bff;
695 	for (i = 0; i < ARRAY_SIZE(data->temp_max); i++)
696 		data->temp_max[i] = 0x7c00;
697 	data->temp2_max = 0x7c00;
698 
699 	switch (data->id) {
700 	case ltc2972:
701 		info->read_word_data = ltc2975_read_word_data;
702 		info->pages = LTC2972_NUM_PAGES;
703 		info->func[0] = PMBUS_HAVE_IIN | PMBUS_HAVE_PIN
704 		  | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
705 		  | PMBUS_HAVE_TEMP2;
706 		for (i = 0; i < info->pages; i++) {
707 			info->func[i] |= PMBUS_HAVE_VOUT
708 			  | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
709 			  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
710 			  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
711 		}
712 		break;
713 	case ltc2974:
714 		info->read_word_data = ltc2974_read_word_data;
715 		info->pages = LTC2974_NUM_PAGES;
716 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
717 		  | PMBUS_HAVE_TEMP2;
718 		for (i = 0; i < info->pages; i++) {
719 			info->func[i] |= PMBUS_HAVE_VOUT
720 			  | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
721 			  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
722 			  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
723 		}
724 		break;
725 	case ltc2975:
726 		info->read_word_data = ltc2975_read_word_data;
727 		info->pages = LTC2974_NUM_PAGES;
728 		info->func[0] = PMBUS_HAVE_IIN | PMBUS_HAVE_PIN
729 		  | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
730 		  | PMBUS_HAVE_TEMP2;
731 		for (i = 0; i < info->pages; i++) {
732 			info->func[i] |= PMBUS_HAVE_VOUT
733 			  | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
734 			  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
735 			  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
736 		}
737 		break;
738 
739 	case ltc2977:
740 	case ltc2978:
741 	case ltc2979:
742 	case ltc2980:
743 	case ltm2987:
744 		info->read_word_data = ltc2978_read_word_data;
745 		info->pages = LTC2978_NUM_PAGES;
746 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
747 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
748 		  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
749 		for (i = 1; i < LTC2978_NUM_PAGES; i++) {
750 			info->func[i] = PMBUS_HAVE_VOUT
751 			  | PMBUS_HAVE_STATUS_VOUT;
752 		}
753 		break;
754 	case ltc3880:
755 	case ltc3887:
756 	case ltm4675:
757 	case ltm4676:
758 	case ltm4677:
759 	case ltm4686:
760 		data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
761 		info->read_word_data = ltc3880_read_word_data;
762 		info->pages = LTC3880_NUM_PAGES;
763 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
764 		  | PMBUS_HAVE_STATUS_INPUT
765 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
766 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
767 		  | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
768 		  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
769 		info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
770 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
771 		  | PMBUS_HAVE_POUT
772 		  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
773 		break;
774 	case ltc3882:
775 		data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
776 		info->read_word_data = ltc3880_read_word_data;
777 		info->pages = LTC3880_NUM_PAGES;
778 		info->func[0] = PMBUS_HAVE_VIN
779 		  | PMBUS_HAVE_STATUS_INPUT
780 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
781 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
782 		  | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
783 		  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
784 		info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
785 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
786 		  | PMBUS_HAVE_POUT
787 		  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
788 		break;
789 	case ltc3883:
790 		data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
791 		info->read_word_data = ltc3883_read_word_data;
792 		info->pages = LTC3883_NUM_PAGES;
793 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
794 		  | PMBUS_HAVE_STATUS_INPUT
795 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
796 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
797 		  | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
798 		  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
799 		break;
800 	case ltc3884:
801 	case ltc3886:
802 	case ltc3889:
803 	case ltc7880:
804 	case ltm4664:
805 	case ltm4678:
806 	case ltm4680:
807 	case ltm4700:
808 		data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
809 		info->read_word_data = ltc3883_read_word_data;
810 		info->pages = LTC3880_NUM_PAGES;
811 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
812 		  | PMBUS_HAVE_STATUS_INPUT
813 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
814 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
815 		  | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
816 		  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
817 		info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
818 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
819 		  | PMBUS_HAVE_POUT
820 		  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
821 		break;
822 	default:
823 		return -ENODEV;
824 	}
825 
826 #if IS_ENABLED(CONFIG_SENSORS_LTC2978_REGULATOR)
827 	info->num_regulators = info->pages;
828 	info->reg_desc = ltc2978_reg_desc;
829 	if (info->num_regulators > ARRAY_SIZE(ltc2978_reg_desc)) {
830 		dev_err(&client->dev, "num_regulators too large!");
831 		info->num_regulators = ARRAY_SIZE(ltc2978_reg_desc);
832 	}
833 #endif
834 
835 	return pmbus_do_probe(client, id, info);
836 }
837 
838 
839 #ifdef CONFIG_OF
840 static const struct of_device_id ltc2978_of_match[] = {
841 	{ .compatible = "lltc,ltc2972" },
842 	{ .compatible = "lltc,ltc2974" },
843 	{ .compatible = "lltc,ltc2975" },
844 	{ .compatible = "lltc,ltc2977" },
845 	{ .compatible = "lltc,ltc2978" },
846 	{ .compatible = "lltc,ltc2979" },
847 	{ .compatible = "lltc,ltc2980" },
848 	{ .compatible = "lltc,ltc3880" },
849 	{ .compatible = "lltc,ltc3882" },
850 	{ .compatible = "lltc,ltc3883" },
851 	{ .compatible = "lltc,ltc3884" },
852 	{ .compatible = "lltc,ltc3886" },
853 	{ .compatible = "lltc,ltc3887" },
854 	{ .compatible = "lltc,ltc3889" },
855 	{ .compatible = "lltc,ltc7880" },
856 	{ .compatible = "lltc,ltm2987" },
857 	{ .compatible = "lltc,ltm4664" },
858 	{ .compatible = "lltc,ltm4675" },
859 	{ .compatible = "lltc,ltm4676" },
860 	{ .compatible = "lltc,ltm4677" },
861 	{ .compatible = "lltc,ltm4678" },
862 	{ .compatible = "lltc,ltm4680" },
863 	{ .compatible = "lltc,ltm4686" },
864 	{ .compatible = "lltc,ltm4700" },
865 	{ }
866 };
867 MODULE_DEVICE_TABLE(of, ltc2978_of_match);
868 #endif
869 
870 static struct i2c_driver ltc2978_driver = {
871 	.driver = {
872 		   .name = "ltc2978",
873 		   .of_match_table = of_match_ptr(ltc2978_of_match),
874 		   },
875 	.probe = ltc2978_probe,
876 	.remove = pmbus_do_remove,
877 	.id_table = ltc2978_id,
878 };
879 
880 module_i2c_driver(ltc2978_driver);
881 
882 MODULE_AUTHOR("Guenter Roeck");
883 MODULE_DESCRIPTION("PMBus driver for LTC2978 and compatible chips");
884 MODULE_LICENSE("GPL");
885