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