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