xref: /openbmc/linux/drivers/hwmon/pmbus/ltc2978.c (revision 53a2a90d)
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 static const struct regulator_desc ltc2978_reg_desc[] = {
566 	PMBUS_REGULATOR("vout", 0),
567 	PMBUS_REGULATOR("vout", 1),
568 	PMBUS_REGULATOR("vout", 2),
569 	PMBUS_REGULATOR("vout", 3),
570 	PMBUS_REGULATOR("vout", 4),
571 	PMBUS_REGULATOR("vout", 5),
572 	PMBUS_REGULATOR("vout", 6),
573 	PMBUS_REGULATOR("vout", 7),
574 };
575 #endif /* CONFIG_SENSORS_LTC2978_REGULATOR */
576 
577 static int ltc2978_get_id(struct i2c_client *client)
578 {
579 	int chip_id;
580 
581 	chip_id = i2c_smbus_read_word_data(client, LTC2978_MFR_SPECIAL_ID);
582 	if (chip_id < 0) {
583 		const struct i2c_device_id *id;
584 		u8 buf[I2C_SMBUS_BLOCK_MAX];
585 		int ret;
586 
587 		if (!i2c_check_functionality(client->adapter,
588 					     I2C_FUNC_SMBUS_READ_BLOCK_DATA))
589 			return -ENODEV;
590 
591 		ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
592 		if (ret < 0)
593 			return ret;
594 		if (ret < 3 || strncmp(buf, "LTC", 3))
595 			return -ENODEV;
596 
597 		ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
598 		if (ret < 0)
599 			return ret;
600 		for (id = &ltc2978_id[0]; strlen(id->name); id++) {
601 			if (!strncasecmp(id->name, buf, strlen(id->name)))
602 				return (int)id->driver_data;
603 		}
604 		return -ENODEV;
605 	}
606 
607 	chip_id &= LTC2978_ID_MASK;
608 
609 	if (chip_id == LTC2972_ID)
610 		return ltc2972;
611 	else if (chip_id == LTC2974_ID)
612 		return ltc2974;
613 	else if (chip_id == LTC2975_ID)
614 		return ltc2975;
615 	else if (chip_id == LTC2977_ID)
616 		return ltc2977;
617 	else if (chip_id == LTC2978_ID_REV1 || chip_id == LTC2978_ID_REV2)
618 		return ltc2978;
619 	else if (chip_id == LTC2979_ID_A || chip_id == LTC2979_ID_B)
620 		return ltc2979;
621 	else if (chip_id == LTC2980_ID_A || chip_id == LTC2980_ID_B)
622 		return ltc2980;
623 	else if (chip_id == LTC3880_ID)
624 		return ltc3880;
625 	else if (chip_id == LTC3882_ID || chip_id == LTC3882_ID_D1)
626 		return ltc3882;
627 	else if (chip_id == LTC3883_ID)
628 		return ltc3883;
629 	else if (chip_id == LTC3884_ID)
630 		return ltc3884;
631 	else if (chip_id == LTC3886_ID)
632 		return ltc3886;
633 	else if (chip_id == LTC3887_ID)
634 		return ltc3887;
635 	else if (chip_id == LTC3889_ID)
636 		return ltc3889;
637 	else if (chip_id == LTC7880_ID)
638 		return ltc7880;
639 	else if (chip_id == LTM2987_ID_A || chip_id == LTM2987_ID_B)
640 		return ltm2987;
641 	else if (chip_id == LTM4664_ID)
642 		return ltm4664;
643 	else if (chip_id == LTM4675_ID)
644 		return ltm4675;
645 	else if (chip_id == LTM4676_ID_REV1 || chip_id == LTM4676_ID_REV2 ||
646 		 chip_id == LTM4676A_ID)
647 		return ltm4676;
648 	else if (chip_id == LTM4677_ID_REV1 || chip_id == LTM4677_ID_REV2)
649 		return ltm4677;
650 	else if (chip_id == LTM4678_ID_REV1 || chip_id == LTM4678_ID_REV2)
651 		return ltm4678;
652 	else if (chip_id == LTM4680_ID)
653 		return ltm4680;
654 	else if (chip_id == LTM4686_ID)
655 		return ltm4686;
656 	else if (chip_id == LTM4700_ID)
657 		return ltm4700;
658 
659 	dev_err(&client->dev, "Unsupported chip ID 0x%x\n", chip_id);
660 	return -ENODEV;
661 }
662 
663 static int ltc2978_probe(struct i2c_client *client)
664 {
665 	int i, chip_id;
666 	struct ltc2978_data *data;
667 	struct pmbus_driver_info *info;
668 	const struct i2c_device_id *id;
669 
670 	if (!i2c_check_functionality(client->adapter,
671 				     I2C_FUNC_SMBUS_READ_WORD_DATA))
672 		return -ENODEV;
673 
674 	data = devm_kzalloc(&client->dev, sizeof(struct ltc2978_data),
675 			    GFP_KERNEL);
676 	if (!data)
677 		return -ENOMEM;
678 
679 	chip_id = ltc2978_get_id(client);
680 	if (chip_id < 0)
681 		return chip_id;
682 
683 	data->id = chip_id;
684 	id = i2c_match_id(ltc2978_id, client);
685 	if (data->id != id->driver_data)
686 		dev_warn(&client->dev,
687 			 "Device mismatch: Configured %s (%d), detected %d\n",
688 			 id->name,
689 			 (int) id->driver_data,
690 			 chip_id);
691 
692 	info = &data->info;
693 	info->write_word_data = ltc2978_write_word_data;
694 	info->write_byte = ltc_write_byte;
695 	info->write_byte_data = ltc_write_byte_data;
696 	info->read_word_data = ltc_read_word_data;
697 	info->read_byte_data = ltc_read_byte_data;
698 
699 	data->vin_min = 0x7bff;
700 	data->vin_max = 0x7c00;
701 	for (i = 0; i < ARRAY_SIZE(data->vout_min); i++)
702 		data->vout_min[i] = 0xffff;
703 	for (i = 0; i < ARRAY_SIZE(data->iout_min); i++)
704 		data->iout_min[i] = 0xfbff;
705 	for (i = 0; i < ARRAY_SIZE(data->iout_max); i++)
706 		data->iout_max[i] = 0x7c00;
707 	for (i = 0; i < ARRAY_SIZE(data->temp_min); i++)
708 		data->temp_min[i] = 0x7bff;
709 	for (i = 0; i < ARRAY_SIZE(data->temp_max); i++)
710 		data->temp_max[i] = 0x7c00;
711 	data->temp2_max = 0x7c00;
712 
713 	switch (data->id) {
714 	case ltc2972:
715 		info->read_word_data = ltc2975_read_word_data;
716 		info->pages = LTC2972_NUM_PAGES;
717 		info->func[0] = PMBUS_HAVE_IIN | PMBUS_HAVE_PIN
718 		  | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
719 		  | PMBUS_HAVE_TEMP2;
720 		for (i = 0; i < info->pages; i++) {
721 			info->func[i] |= PMBUS_HAVE_VOUT
722 			  | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
723 			  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
724 			  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
725 		}
726 		break;
727 	case ltc2974:
728 		info->read_word_data = ltc2974_read_word_data;
729 		info->pages = LTC2974_NUM_PAGES;
730 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
731 		  | PMBUS_HAVE_TEMP2;
732 		for (i = 0; i < info->pages; i++) {
733 			info->func[i] |= PMBUS_HAVE_VOUT
734 			  | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
735 			  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
736 			  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
737 		}
738 		break;
739 	case ltc2975:
740 		info->read_word_data = ltc2975_read_word_data;
741 		info->pages = LTC2974_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 
753 	case ltc2977:
754 	case ltc2978:
755 	case ltc2979:
756 	case ltc2980:
757 	case ltm2987:
758 		info->read_word_data = ltc2978_read_word_data;
759 		info->pages = LTC2978_NUM_PAGES;
760 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
761 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
762 		  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
763 		for (i = 1; i < LTC2978_NUM_PAGES; i++) {
764 			info->func[i] = PMBUS_HAVE_VOUT
765 			  | PMBUS_HAVE_STATUS_VOUT;
766 		}
767 		break;
768 	case ltc3880:
769 	case ltc3887:
770 	case ltm4675:
771 	case ltm4676:
772 	case ltm4677:
773 	case ltm4686:
774 		data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
775 		info->read_word_data = ltc3880_read_word_data;
776 		info->pages = LTC3880_NUM_PAGES;
777 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
778 		  | PMBUS_HAVE_STATUS_INPUT
779 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
780 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
781 		  | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
782 		  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
783 		info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
784 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
785 		  | PMBUS_HAVE_POUT
786 		  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
787 		break;
788 	case ltc3882:
789 		data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
790 		info->read_word_data = ltc3880_read_word_data;
791 		info->pages = LTC3880_NUM_PAGES;
792 		info->func[0] = PMBUS_HAVE_VIN
793 		  | PMBUS_HAVE_STATUS_INPUT
794 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
795 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
796 		  | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
797 		  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
798 		info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
799 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
800 		  | PMBUS_HAVE_POUT
801 		  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
802 		break;
803 	case ltc3883:
804 		data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
805 		info->read_word_data = ltc3883_read_word_data;
806 		info->pages = LTC3883_NUM_PAGES;
807 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
808 		  | PMBUS_HAVE_STATUS_INPUT
809 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
810 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
811 		  | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
812 		  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
813 		break;
814 	case ltc3884:
815 	case ltc3886:
816 	case ltc3889:
817 	case ltc7880:
818 	case ltm4664:
819 	case ltm4678:
820 	case ltm4680:
821 	case ltm4700:
822 		data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
823 		info->read_word_data = ltc3883_read_word_data;
824 		info->pages = LTC3880_NUM_PAGES;
825 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
826 		  | PMBUS_HAVE_STATUS_INPUT
827 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
828 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
829 		  | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
830 		  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
831 		info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
832 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
833 		  | PMBUS_HAVE_POUT
834 		  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
835 		break;
836 	default:
837 		return -ENODEV;
838 	}
839 
840 #if IS_ENABLED(CONFIG_SENSORS_LTC2978_REGULATOR)
841 	info->num_regulators = info->pages;
842 	info->reg_desc = ltc2978_reg_desc;
843 	if (info->num_regulators > ARRAY_SIZE(ltc2978_reg_desc)) {
844 		dev_err(&client->dev, "num_regulators too large!");
845 		info->num_regulators = ARRAY_SIZE(ltc2978_reg_desc);
846 	}
847 #endif
848 
849 	return pmbus_do_probe(client, info);
850 }
851 
852 
853 #ifdef CONFIG_OF
854 static const struct of_device_id ltc2978_of_match[] = {
855 	{ .compatible = "lltc,ltc2972" },
856 	{ .compatible = "lltc,ltc2974" },
857 	{ .compatible = "lltc,ltc2975" },
858 	{ .compatible = "lltc,ltc2977" },
859 	{ .compatible = "lltc,ltc2978" },
860 	{ .compatible = "lltc,ltc2979" },
861 	{ .compatible = "lltc,ltc2980" },
862 	{ .compatible = "lltc,ltc3880" },
863 	{ .compatible = "lltc,ltc3882" },
864 	{ .compatible = "lltc,ltc3883" },
865 	{ .compatible = "lltc,ltc3884" },
866 	{ .compatible = "lltc,ltc3886" },
867 	{ .compatible = "lltc,ltc3887" },
868 	{ .compatible = "lltc,ltc3889" },
869 	{ .compatible = "lltc,ltc7880" },
870 	{ .compatible = "lltc,ltm2987" },
871 	{ .compatible = "lltc,ltm4664" },
872 	{ .compatible = "lltc,ltm4675" },
873 	{ .compatible = "lltc,ltm4676" },
874 	{ .compatible = "lltc,ltm4677" },
875 	{ .compatible = "lltc,ltm4678" },
876 	{ .compatible = "lltc,ltm4680" },
877 	{ .compatible = "lltc,ltm4686" },
878 	{ .compatible = "lltc,ltm4700" },
879 	{ }
880 };
881 MODULE_DEVICE_TABLE(of, ltc2978_of_match);
882 #endif
883 
884 static struct i2c_driver ltc2978_driver = {
885 	.driver = {
886 		   .name = "ltc2978",
887 		   .of_match_table = of_match_ptr(ltc2978_of_match),
888 		   },
889 	.probe_new = ltc2978_probe,
890 	.id_table = ltc2978_id,
891 };
892 
893 module_i2c_driver(ltc2978_driver);
894 
895 MODULE_AUTHOR("Guenter Roeck");
896 MODULE_DESCRIPTION("PMBus driver for LTC2978 and compatible chips");
897 MODULE_LICENSE("GPL");
898 MODULE_IMPORT_NS(PMBUS);
899