xref: /openbmc/linux/drivers/hwmon/pmbus/ltc2978.c (revision 275876e2)
1 /*
2  * Hardware monitoring driver for LTC2974, LTC2977, LTC2978, LTC3880,
3  * LTC3883, and LTM4676
4  *
5  * Copyright (c) 2011 Ericsson AB.
6  * Copyright (c) 2013, 2014 Guenter Roeck
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/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/i2c.h>
25 #include "pmbus.h"
26 
27 enum chips { ltc2974, ltc2977, ltc2978, ltc3880, ltc3883, ltm4676 };
28 
29 /* Common for all chips */
30 #define LTC2978_MFR_VOUT_PEAK		0xdd
31 #define LTC2978_MFR_VIN_PEAK		0xde
32 #define LTC2978_MFR_TEMPERATURE_PEAK	0xdf
33 #define LTC2978_MFR_SPECIAL_ID		0xe7
34 
35 /* LTC2974, LCT2977, and LTC2978 */
36 #define LTC2978_MFR_VOUT_MIN		0xfb
37 #define LTC2978_MFR_VIN_MIN		0xfc
38 #define LTC2978_MFR_TEMPERATURE_MIN	0xfd
39 
40 /* LTC2974 only */
41 #define LTC2974_MFR_IOUT_PEAK		0xd7
42 #define LTC2974_MFR_IOUT_MIN		0xd8
43 
44 /* LTC3880, LTC3883, and LTM4676 */
45 #define LTC3880_MFR_IOUT_PEAK		0xd7
46 #define LTC3880_MFR_CLEAR_PEAKS		0xe3
47 #define LTC3880_MFR_TEMPERATURE2_PEAK	0xf4
48 
49 /* LTC3883 only */
50 #define LTC3883_MFR_IIN_PEAK		0xe1
51 
52 #define LTC2974_ID_REV1			0x0212
53 #define LTC2974_ID_REV2			0x0213
54 #define LTC2977_ID			0x0130
55 #define LTC2978_ID_REV1			0x0121
56 #define LTC2978_ID_REV2			0x0122
57 #define LTC2978A_ID			0x0124
58 #define LTC3880_ID			0x4000
59 #define LTC3880_ID_MASK			0xff00
60 #define LTC3883_ID			0x4300
61 #define LTC3883_ID_MASK			0xff00
62 #define LTM4676_ID			0x4480	/* datasheet claims 0x440X */
63 #define LTM4676_ID_MASK			0xfff0
64 
65 #define LTC2974_NUM_PAGES		4
66 #define LTC2978_NUM_PAGES		8
67 #define LTC3880_NUM_PAGES		2
68 #define LTC3883_NUM_PAGES		1
69 
70 /*
71  * LTC2978 clears peak data whenever the CLEAR_FAULTS command is executed, which
72  * happens pretty much each time chip data is updated. Raw peak data therefore
73  * does not provide much value. To be able to provide useful peak data, keep an
74  * internal cache of measured peak data, which is only cleared if an explicit
75  * "clear peak" command is executed for the sensor in question.
76  */
77 
78 struct ltc2978_data {
79 	enum chips id;
80 	u16 vin_min, vin_max;
81 	u16 temp_min[LTC2974_NUM_PAGES], temp_max[LTC2974_NUM_PAGES];
82 	u16 vout_min[LTC2978_NUM_PAGES], vout_max[LTC2978_NUM_PAGES];
83 	u16 iout_min[LTC2974_NUM_PAGES], iout_max[LTC2974_NUM_PAGES];
84 	u16 iin_max;
85 	u16 temp2_max;
86 	struct pmbus_driver_info info;
87 };
88 
89 #define to_ltc2978_data(x)  container_of(x, struct ltc2978_data, info)
90 
91 static inline int lin11_to_val(int data)
92 {
93 	s16 e = ((s16)data) >> 11;
94 	s32 m = (((s16)(data << 5)) >> 5);
95 
96 	/*
97 	 * mantissa is 10 bit + sign, exponent adds up to 15 bit.
98 	 * Add 6 bit to exponent for maximum accuracy (10 + 15 + 6 = 31).
99 	 */
100 	e += 6;
101 	return (e < 0 ? m >> -e : m << e);
102 }
103 
104 static int ltc2978_read_word_data_common(struct i2c_client *client, int page,
105 					 int reg)
106 {
107 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
108 	struct ltc2978_data *data = to_ltc2978_data(info);
109 	int ret;
110 
111 	switch (reg) {
112 	case PMBUS_VIRT_READ_VIN_MAX:
113 		ret = pmbus_read_word_data(client, page, LTC2978_MFR_VIN_PEAK);
114 		if (ret >= 0) {
115 			if (lin11_to_val(ret) > lin11_to_val(data->vin_max))
116 				data->vin_max = ret;
117 			ret = data->vin_max;
118 		}
119 		break;
120 	case PMBUS_VIRT_READ_VOUT_MAX:
121 		ret = pmbus_read_word_data(client, page, LTC2978_MFR_VOUT_PEAK);
122 		if (ret >= 0) {
123 			/*
124 			 * VOUT is 16 bit unsigned with fixed exponent,
125 			 * so we can compare it directly
126 			 */
127 			if (ret > data->vout_max[page])
128 				data->vout_max[page] = ret;
129 			ret = data->vout_max[page];
130 		}
131 		break;
132 	case PMBUS_VIRT_READ_TEMP_MAX:
133 		ret = pmbus_read_word_data(client, page,
134 					   LTC2978_MFR_TEMPERATURE_PEAK);
135 		if (ret >= 0) {
136 			if (lin11_to_val(ret)
137 			    > lin11_to_val(data->temp_max[page]))
138 				data->temp_max[page] = ret;
139 			ret = data->temp_max[page];
140 		}
141 		break;
142 	case PMBUS_VIRT_RESET_VOUT_HISTORY:
143 	case PMBUS_VIRT_RESET_VIN_HISTORY:
144 	case PMBUS_VIRT_RESET_TEMP_HISTORY:
145 		ret = 0;
146 		break;
147 	default:
148 		ret = -ENODATA;
149 		break;
150 	}
151 	return ret;
152 }
153 
154 static int ltc2978_read_word_data(struct i2c_client *client, int page, int reg)
155 {
156 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
157 	struct ltc2978_data *data = to_ltc2978_data(info);
158 	int ret;
159 
160 	switch (reg) {
161 	case PMBUS_VIRT_READ_VIN_MIN:
162 		ret = pmbus_read_word_data(client, page, LTC2978_MFR_VIN_MIN);
163 		if (ret >= 0) {
164 			if (lin11_to_val(ret) < lin11_to_val(data->vin_min))
165 				data->vin_min = ret;
166 			ret = data->vin_min;
167 		}
168 		break;
169 	case PMBUS_VIRT_READ_VOUT_MIN:
170 		ret = pmbus_read_word_data(client, page, LTC2978_MFR_VOUT_MIN);
171 		if (ret >= 0) {
172 			/*
173 			 * VOUT_MIN is known to not be supported on some lots
174 			 * of LTC2978 revision 1, and will return the maximum
175 			 * possible voltage if read. If VOUT_MAX is valid and
176 			 * lower than the reading of VOUT_MIN, use it instead.
177 			 */
178 			if (data->vout_max[page] && ret > data->vout_max[page])
179 				ret = data->vout_max[page];
180 			if (ret < data->vout_min[page])
181 				data->vout_min[page] = ret;
182 			ret = data->vout_min[page];
183 		}
184 		break;
185 	case PMBUS_VIRT_READ_TEMP_MIN:
186 		ret = pmbus_read_word_data(client, page,
187 					   LTC2978_MFR_TEMPERATURE_MIN);
188 		if (ret >= 0) {
189 			if (lin11_to_val(ret)
190 			    < lin11_to_val(data->temp_min[page]))
191 				data->temp_min[page] = ret;
192 			ret = data->temp_min[page];
193 		}
194 		break;
195 	case PMBUS_VIRT_READ_IOUT_MAX:
196 	case PMBUS_VIRT_RESET_IOUT_HISTORY:
197 	case PMBUS_VIRT_READ_TEMP2_MAX:
198 	case PMBUS_VIRT_RESET_TEMP2_HISTORY:
199 		ret = -ENXIO;
200 		break;
201 	default:
202 		ret = ltc2978_read_word_data_common(client, page, reg);
203 		break;
204 	}
205 	return ret;
206 }
207 
208 static int ltc2974_read_word_data(struct i2c_client *client, int page, int reg)
209 {
210 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
211 	struct ltc2978_data *data = to_ltc2978_data(info);
212 	int ret;
213 
214 	switch (reg) {
215 	case PMBUS_VIRT_READ_IOUT_MAX:
216 		ret = pmbus_read_word_data(client, page, LTC2974_MFR_IOUT_PEAK);
217 		if (ret >= 0) {
218 			if (lin11_to_val(ret)
219 			    > lin11_to_val(data->iout_max[page]))
220 				data->iout_max[page] = ret;
221 			ret = data->iout_max[page];
222 		}
223 		break;
224 	case PMBUS_VIRT_READ_IOUT_MIN:
225 		ret = pmbus_read_word_data(client, page, LTC2974_MFR_IOUT_MIN);
226 		if (ret >= 0) {
227 			if (lin11_to_val(ret)
228 			    < lin11_to_val(data->iout_min[page]))
229 				data->iout_min[page] = ret;
230 			ret = data->iout_min[page];
231 		}
232 		break;
233 	case PMBUS_VIRT_RESET_IOUT_HISTORY:
234 		ret = 0;
235 		break;
236 	default:
237 		ret = ltc2978_read_word_data(client, page, reg);
238 		break;
239 	}
240 	return ret;
241 }
242 
243 static int ltc3880_read_word_data(struct i2c_client *client, int page, int reg)
244 {
245 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
246 	struct ltc2978_data *data = to_ltc2978_data(info);
247 	int ret;
248 
249 	switch (reg) {
250 	case PMBUS_VIRT_READ_IOUT_MAX:
251 		ret = pmbus_read_word_data(client, page, LTC3880_MFR_IOUT_PEAK);
252 		if (ret >= 0) {
253 			if (lin11_to_val(ret)
254 			    > lin11_to_val(data->iout_max[page]))
255 				data->iout_max[page] = ret;
256 			ret = data->iout_max[page];
257 		}
258 		break;
259 	case PMBUS_VIRT_READ_TEMP2_MAX:
260 		ret = pmbus_read_word_data(client, page,
261 					   LTC3880_MFR_TEMPERATURE2_PEAK);
262 		if (ret >= 0) {
263 			if (lin11_to_val(ret) > lin11_to_val(data->temp2_max))
264 				data->temp2_max = ret;
265 			ret = data->temp2_max;
266 		}
267 		break;
268 	case PMBUS_VIRT_READ_VIN_MIN:
269 	case PMBUS_VIRT_READ_VOUT_MIN:
270 	case PMBUS_VIRT_READ_TEMP_MIN:
271 		ret = -ENXIO;
272 		break;
273 	case PMBUS_VIRT_RESET_IOUT_HISTORY:
274 	case PMBUS_VIRT_RESET_TEMP2_HISTORY:
275 		ret = 0;
276 		break;
277 	default:
278 		ret = ltc2978_read_word_data_common(client, page, reg);
279 		break;
280 	}
281 	return ret;
282 }
283 
284 static int ltc3883_read_word_data(struct i2c_client *client, int page, int reg)
285 {
286 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
287 	struct ltc2978_data *data = to_ltc2978_data(info);
288 	int ret;
289 
290 	switch (reg) {
291 	case PMBUS_VIRT_READ_IIN_MAX:
292 		ret = pmbus_read_word_data(client, page, LTC3883_MFR_IIN_PEAK);
293 		if (ret >= 0) {
294 			if (lin11_to_val(ret)
295 			    > lin11_to_val(data->iin_max))
296 				data->iin_max = ret;
297 			ret = data->iin_max;
298 		}
299 		break;
300 	case PMBUS_VIRT_RESET_IIN_HISTORY:
301 		ret = 0;
302 		break;
303 	default:
304 		ret = ltc3880_read_word_data(client, page, reg);
305 		break;
306 	}
307 	return ret;
308 }
309 
310 static int ltc2978_clear_peaks(struct i2c_client *client, int page,
311 			       enum chips id)
312 {
313 	int ret;
314 
315 	if (id == ltc3880 || id == ltc3883)
316 		ret = pmbus_write_byte(client, 0, LTC3880_MFR_CLEAR_PEAKS);
317 	else
318 		ret = pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
319 
320 	return ret;
321 }
322 
323 static int ltc2978_write_word_data(struct i2c_client *client, int page,
324 				    int reg, u16 word)
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_RESET_IIN_HISTORY:
332 		data->iin_max = 0x7c00;
333 		ret = ltc2978_clear_peaks(client, page, data->id);
334 		break;
335 	case PMBUS_VIRT_RESET_IOUT_HISTORY:
336 		data->iout_max[page] = 0x7c00;
337 		data->iout_min[page] = 0xfbff;
338 		ret = ltc2978_clear_peaks(client, page, data->id);
339 		break;
340 	case PMBUS_VIRT_RESET_TEMP2_HISTORY:
341 		data->temp2_max = 0x7c00;
342 		ret = ltc2978_clear_peaks(client, page, data->id);
343 		break;
344 	case PMBUS_VIRT_RESET_VOUT_HISTORY:
345 		data->vout_min[page] = 0xffff;
346 		data->vout_max[page] = 0;
347 		ret = ltc2978_clear_peaks(client, page, data->id);
348 		break;
349 	case PMBUS_VIRT_RESET_VIN_HISTORY:
350 		data->vin_min = 0x7bff;
351 		data->vin_max = 0x7c00;
352 		ret = ltc2978_clear_peaks(client, page, data->id);
353 		break;
354 	case PMBUS_VIRT_RESET_TEMP_HISTORY:
355 		data->temp_min[page] = 0x7bff;
356 		data->temp_max[page] = 0x7c00;
357 		ret = ltc2978_clear_peaks(client, page, data->id);
358 		break;
359 	default:
360 		ret = -ENODATA;
361 		break;
362 	}
363 	return ret;
364 }
365 
366 static const struct i2c_device_id ltc2978_id[] = {
367 	{"ltc2974", ltc2974},
368 	{"ltc2977", ltc2977},
369 	{"ltc2978", ltc2978},
370 	{"ltc3880", ltc3880},
371 	{"ltc3883", ltc3883},
372 	{"ltm4676", ltm4676},
373 	{}
374 };
375 MODULE_DEVICE_TABLE(i2c, ltc2978_id);
376 
377 static int ltc2978_probe(struct i2c_client *client,
378 			 const struct i2c_device_id *id)
379 {
380 	int chip_id, i;
381 	struct ltc2978_data *data;
382 	struct pmbus_driver_info *info;
383 
384 	if (!i2c_check_functionality(client->adapter,
385 				     I2C_FUNC_SMBUS_READ_WORD_DATA))
386 		return -ENODEV;
387 
388 	data = devm_kzalloc(&client->dev, sizeof(struct ltc2978_data),
389 			    GFP_KERNEL);
390 	if (!data)
391 		return -ENOMEM;
392 
393 	chip_id = i2c_smbus_read_word_data(client, LTC2978_MFR_SPECIAL_ID);
394 	if (chip_id < 0)
395 		return chip_id;
396 
397 	if (chip_id == LTC2974_ID_REV1 || chip_id == LTC2974_ID_REV2) {
398 		data->id = ltc2974;
399 	} else if (chip_id == LTC2977_ID) {
400 		data->id = ltc2977;
401 	} else if (chip_id == LTC2978_ID_REV1 || chip_id == LTC2978_ID_REV2 ||
402 		   chip_id == LTC2978A_ID) {
403 		data->id = ltc2978;
404 	} else if ((chip_id & LTC3880_ID_MASK) == LTC3880_ID) {
405 		data->id = ltc3880;
406 	} else if ((chip_id & LTC3883_ID_MASK) == LTC3883_ID) {
407 		data->id = ltc3883;
408 	} else if ((chip_id & LTM4676_ID_MASK) == LTM4676_ID) {
409 		data->id = ltm4676;
410 	} else {
411 		dev_err(&client->dev, "Unsupported chip ID 0x%x\n", chip_id);
412 		return -ENODEV;
413 	}
414 	if (data->id != id->driver_data)
415 		dev_warn(&client->dev,
416 			 "Device mismatch: Configured %s, detected %s\n",
417 			 id->name,
418 			 ltc2978_id[data->id].name);
419 
420 	info = &data->info;
421 	info->write_word_data = ltc2978_write_word_data;
422 
423 	data->vin_min = 0x7bff;
424 	data->vin_max = 0x7c00;
425 	for (i = 0; i < ARRAY_SIZE(data->vout_min); i++)
426 		data->vout_min[i] = 0xffff;
427 	for (i = 0; i < ARRAY_SIZE(data->iout_min); i++)
428 		data->iout_min[i] = 0xfbff;
429 	for (i = 0; i < ARRAY_SIZE(data->iout_max); i++)
430 		data->iout_max[i] = 0x7c00;
431 	for (i = 0; i < ARRAY_SIZE(data->temp_min); i++)
432 		data->temp_min[i] = 0x7bff;
433 	for (i = 0; i < ARRAY_SIZE(data->temp_max); i++)
434 		data->temp_max[i] = 0x7c00;
435 	data->temp2_max = 0x7c00;
436 
437 	switch (data->id) {
438 	case ltc2974:
439 		info->read_word_data = ltc2974_read_word_data;
440 		info->pages = LTC2974_NUM_PAGES;
441 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
442 		  | PMBUS_HAVE_TEMP2;
443 		for (i = 0; i < info->pages; i++) {
444 			info->func[i] |= PMBUS_HAVE_VOUT
445 			  | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
446 			  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
447 			  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
448 		}
449 		break;
450 	case ltc2977:
451 	case ltc2978:
452 		info->read_word_data = ltc2978_read_word_data;
453 		info->pages = LTC2978_NUM_PAGES;
454 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
455 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
456 		  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
457 		for (i = 1; i < LTC2978_NUM_PAGES; i++) {
458 			info->func[i] = PMBUS_HAVE_VOUT
459 			  | PMBUS_HAVE_STATUS_VOUT;
460 		}
461 		break;
462 	case ltc3880:
463 	case ltm4676:
464 		info->read_word_data = ltc3880_read_word_data;
465 		info->pages = LTC3880_NUM_PAGES;
466 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
467 		  | PMBUS_HAVE_STATUS_INPUT
468 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
469 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
470 		  | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
471 		  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
472 		info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
473 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
474 		  | PMBUS_HAVE_POUT
475 		  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
476 		break;
477 	case ltc3883:
478 		info->read_word_data = ltc3883_read_word_data;
479 		info->pages = LTC3883_NUM_PAGES;
480 		info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
481 		  | PMBUS_HAVE_STATUS_INPUT
482 		  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
483 		  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
484 		  | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
485 		  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
486 		break;
487 	default:
488 		return -ENODEV;
489 	}
490 	return pmbus_do_probe(client, id, info);
491 }
492 
493 /* This is the driver that will be inserted */
494 static struct i2c_driver ltc2978_driver = {
495 	.driver = {
496 		   .name = "ltc2978",
497 		   },
498 	.probe = ltc2978_probe,
499 	.remove = pmbus_do_remove,
500 	.id_table = ltc2978_id,
501 };
502 
503 module_i2c_driver(ltc2978_driver);
504 
505 MODULE_AUTHOR("Guenter Roeck");
506 MODULE_DESCRIPTION("PMBus driver for LTC2974, LTC2978, LTC3880, LTC3883, and LTM4676");
507 MODULE_LICENSE("GPL");
508