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