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