xref: /openbmc/linux/drivers/hwmon/pmbus/lm25066.c (revision 4a075bd4)
1 /*
2  * Hardware monitoring driver for LM25056 / LM25066 / LM5064 / LM5066
3  *
4  * Copyright (c) 2011 Ericsson AB.
5  * Copyright (c) 2013 Guenter Roeck
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include <linux/bitops.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/log2.h>
30 #include "pmbus.h"
31 
32 enum chips { lm25056, lm25066, lm5064, lm5066, lm5066i };
33 
34 #define LM25066_READ_VAUX		0xd0
35 #define LM25066_MFR_READ_IIN		0xd1
36 #define LM25066_MFR_READ_PIN		0xd2
37 #define LM25066_MFR_IIN_OC_WARN_LIMIT	0xd3
38 #define LM25066_MFR_PIN_OP_WARN_LIMIT	0xd4
39 #define LM25066_READ_PIN_PEAK		0xd5
40 #define LM25066_CLEAR_PIN_PEAK		0xd6
41 #define LM25066_DEVICE_SETUP		0xd9
42 #define LM25066_READ_AVG_VIN		0xdc
43 #define LM25066_SAMPLES_FOR_AVG		0xdb
44 #define LM25066_READ_AVG_VOUT		0xdd
45 #define LM25066_READ_AVG_IIN		0xde
46 #define LM25066_READ_AVG_PIN		0xdf
47 
48 #define LM25066_DEV_SETUP_CL		BIT(4)	/* Current limit */
49 
50 #define LM25066_SAMPLES_FOR_AVG_MAX	4096
51 
52 /* LM25056 only */
53 
54 #define LM25056_VAUX_OV_WARN_LIMIT	0xe3
55 #define LM25056_VAUX_UV_WARN_LIMIT	0xe4
56 
57 #define LM25056_MFR_STS_VAUX_OV_WARN	BIT(1)
58 #define LM25056_MFR_STS_VAUX_UV_WARN	BIT(0)
59 
60 struct __coeff {
61 	short m, b, R;
62 };
63 
64 #define PSC_CURRENT_IN_L	(PSC_NUM_CLASSES)
65 #define PSC_POWER_L		(PSC_NUM_CLASSES + 1)
66 
67 static struct __coeff lm25066_coeff[6][PSC_NUM_CLASSES + 2] = {
68 	[lm25056] = {
69 		[PSC_VOLTAGE_IN] = {
70 			.m = 16296,
71 			.R = -2,
72 		},
73 		[PSC_CURRENT_IN] = {
74 			.m = 13797,
75 			.R = -2,
76 		},
77 		[PSC_CURRENT_IN_L] = {
78 			.m = 6726,
79 			.R = -2,
80 		},
81 		[PSC_POWER] = {
82 			.m = 5501,
83 			.R = -3,
84 		},
85 		[PSC_POWER_L] = {
86 			.m = 26882,
87 			.R = -4,
88 		},
89 		[PSC_TEMPERATURE] = {
90 			.m = 1580,
91 			.b = -14500,
92 			.R = -2,
93 		},
94 	},
95 	[lm25066] = {
96 		[PSC_VOLTAGE_IN] = {
97 			.m = 22070,
98 			.R = -2,
99 		},
100 		[PSC_VOLTAGE_OUT] = {
101 			.m = 22070,
102 			.R = -2,
103 		},
104 		[PSC_CURRENT_IN] = {
105 			.m = 13661,
106 			.R = -2,
107 		},
108 		[PSC_CURRENT_IN_L] = {
109 			.m = 6852,
110 			.R = -2,
111 		},
112 		[PSC_POWER] = {
113 			.m = 736,
114 			.R = -2,
115 		},
116 		[PSC_POWER_L] = {
117 			.m = 369,
118 			.R = -2,
119 		},
120 		[PSC_TEMPERATURE] = {
121 			.m = 16,
122 		},
123 	},
124 	[lm5064] = {
125 		[PSC_VOLTAGE_IN] = {
126 			.m = 4611,
127 			.R = -2,
128 		},
129 		[PSC_VOLTAGE_OUT] = {
130 			.m = 4621,
131 			.R = -2,
132 		},
133 		[PSC_CURRENT_IN] = {
134 			.m = 10742,
135 			.R = -2,
136 		},
137 		[PSC_CURRENT_IN_L] = {
138 			.m = 5456,
139 			.R = -2,
140 		},
141 		[PSC_POWER] = {
142 			.m = 1204,
143 			.R = -3,
144 		},
145 		[PSC_POWER_L] = {
146 			.m = 612,
147 			.R = -3,
148 		},
149 		[PSC_TEMPERATURE] = {
150 			.m = 16,
151 		},
152 	},
153 	[lm5066] = {
154 		[PSC_VOLTAGE_IN] = {
155 			.m = 4587,
156 			.R = -2,
157 		},
158 		[PSC_VOLTAGE_OUT] = {
159 			.m = 4587,
160 			.R = -2,
161 		},
162 		[PSC_CURRENT_IN] = {
163 			.m = 10753,
164 			.R = -2,
165 		},
166 		[PSC_CURRENT_IN_L] = {
167 			.m = 5405,
168 			.R = -2,
169 		},
170 		[PSC_POWER] = {
171 			.m = 1204,
172 			.R = -3,
173 		},
174 		[PSC_POWER_L] = {
175 			.m = 605,
176 			.R = -3,
177 		},
178 		[PSC_TEMPERATURE] = {
179 			.m = 16,
180 		},
181 	},
182 	[lm5066i] = {
183 		[PSC_VOLTAGE_IN] = {
184 			.m = 4617,
185 			.b = -140,
186 			.R = -2,
187 		},
188 		[PSC_VOLTAGE_OUT] = {
189 			.m = 4602,
190 			.b = 500,
191 			.R = -2,
192 		},
193 		[PSC_CURRENT_IN] = {
194 			.m = 15076,
195 			.b = -504,
196 			.R = -2,
197 		},
198 		[PSC_CURRENT_IN_L] = {
199 			.m = 7645,
200 			.b = 100,
201 			.R = -2,
202 		},
203 		[PSC_POWER] = {
204 			.m = 1701,
205 			.b = -4000,
206 			.R = -3,
207 		},
208 		[PSC_POWER_L] = {
209 			.m = 861,
210 			.b = -965,
211 			.R = -3,
212 		},
213 		[PSC_TEMPERATURE] = {
214 			.m = 16,
215 		},
216 	},
217 };
218 
219 struct lm25066_data {
220 	int id;
221 	u16 rlimit;			/* Maximum register value */
222 	struct pmbus_driver_info info;
223 };
224 
225 #define to_lm25066_data(x)  container_of(x, struct lm25066_data, info)
226 
227 static int lm25066_read_word_data(struct i2c_client *client, int page, int reg)
228 {
229 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
230 	const struct lm25066_data *data = to_lm25066_data(info);
231 	int ret;
232 
233 	switch (reg) {
234 	case PMBUS_VIRT_READ_VMON:
235 		ret = pmbus_read_word_data(client, 0, LM25066_READ_VAUX);
236 		if (ret < 0)
237 			break;
238 		/* Adjust returned value to match VIN coefficients */
239 		switch (data->id) {
240 		case lm25056:
241 			/* VIN: 6.14 mV VAUX: 293 uV LSB */
242 			ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
243 			break;
244 		case lm25066:
245 			/* VIN: 4.54 mV VAUX: 283.2 uV LSB */
246 			ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
247 			break;
248 		case lm5064:
249 			/* VIN: 4.53 mV VAUX: 700 uV LSB */
250 			ret = DIV_ROUND_CLOSEST(ret * 70, 453);
251 			break;
252 		case lm5066:
253 		case lm5066i:
254 			/* VIN: 2.18 mV VAUX: 725 uV LSB */
255 			ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
256 			break;
257 		}
258 		break;
259 	case PMBUS_READ_IIN:
260 		ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
261 		break;
262 	case PMBUS_READ_PIN:
263 		ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
264 		break;
265 	case PMBUS_IIN_OC_WARN_LIMIT:
266 		ret = pmbus_read_word_data(client, 0,
267 					   LM25066_MFR_IIN_OC_WARN_LIMIT);
268 		break;
269 	case PMBUS_PIN_OP_WARN_LIMIT:
270 		ret = pmbus_read_word_data(client, 0,
271 					   LM25066_MFR_PIN_OP_WARN_LIMIT);
272 		break;
273 	case PMBUS_VIRT_READ_VIN_AVG:
274 		ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
275 		break;
276 	case PMBUS_VIRT_READ_VOUT_AVG:
277 		ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
278 		break;
279 	case PMBUS_VIRT_READ_IIN_AVG:
280 		ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
281 		break;
282 	case PMBUS_VIRT_READ_PIN_AVG:
283 		ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
284 		break;
285 	case PMBUS_VIRT_READ_PIN_MAX:
286 		ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
287 		break;
288 	case PMBUS_VIRT_RESET_PIN_HISTORY:
289 		ret = 0;
290 		break;
291 	case PMBUS_VIRT_SAMPLES:
292 		ret = pmbus_read_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG);
293 		if (ret < 0)
294 			break;
295 		ret = 1 << ret;
296 		break;
297 	default:
298 		ret = -ENODATA;
299 		break;
300 	}
301 	return ret;
302 }
303 
304 static int lm25056_read_word_data(struct i2c_client *client, int page, int reg)
305 {
306 	int ret;
307 
308 	switch (reg) {
309 	case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
310 		ret = pmbus_read_word_data(client, 0,
311 					   LM25056_VAUX_UV_WARN_LIMIT);
312 		if (ret < 0)
313 			break;
314 		/* Adjust returned value to match VIN coefficients */
315 		ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
316 		break;
317 	case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
318 		ret = pmbus_read_word_data(client, 0,
319 					   LM25056_VAUX_OV_WARN_LIMIT);
320 		if (ret < 0)
321 			break;
322 		/* Adjust returned value to match VIN coefficients */
323 		ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
324 		break;
325 	default:
326 		ret = lm25066_read_word_data(client, page, reg);
327 		break;
328 	}
329 	return ret;
330 }
331 
332 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
333 {
334 	int ret, s;
335 
336 	switch (reg) {
337 	case PMBUS_VIRT_STATUS_VMON:
338 		ret = pmbus_read_byte_data(client, 0,
339 					   PMBUS_STATUS_MFR_SPECIFIC);
340 		if (ret < 0)
341 			break;
342 		s = 0;
343 		if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
344 			s |= PB_VOLTAGE_UV_WARNING;
345 		if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
346 			s |= PB_VOLTAGE_OV_WARNING;
347 		ret = s;
348 		break;
349 	default:
350 		ret = -ENODATA;
351 		break;
352 	}
353 	return ret;
354 }
355 
356 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
357 				   u16 word)
358 {
359 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
360 	const struct lm25066_data *data = to_lm25066_data(info);
361 	int ret;
362 
363 	switch (reg) {
364 	case PMBUS_POUT_OP_FAULT_LIMIT:
365 	case PMBUS_POUT_OP_WARN_LIMIT:
366 	case PMBUS_VOUT_UV_WARN_LIMIT:
367 	case PMBUS_OT_FAULT_LIMIT:
368 	case PMBUS_OT_WARN_LIMIT:
369 	case PMBUS_IIN_OC_FAULT_LIMIT:
370 	case PMBUS_VIN_UV_WARN_LIMIT:
371 	case PMBUS_VIN_UV_FAULT_LIMIT:
372 	case PMBUS_VIN_OV_FAULT_LIMIT:
373 	case PMBUS_VIN_OV_WARN_LIMIT:
374 		word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
375 		ret = pmbus_write_word_data(client, 0, reg, word);
376 		pmbus_clear_cache(client);
377 		break;
378 	case PMBUS_IIN_OC_WARN_LIMIT:
379 		word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
380 		ret = pmbus_write_word_data(client, 0,
381 					    LM25066_MFR_IIN_OC_WARN_LIMIT,
382 					    word);
383 		pmbus_clear_cache(client);
384 		break;
385 	case PMBUS_PIN_OP_WARN_LIMIT:
386 		word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
387 		ret = pmbus_write_word_data(client, 0,
388 					    LM25066_MFR_PIN_OP_WARN_LIMIT,
389 					    word);
390 		pmbus_clear_cache(client);
391 		break;
392 	case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
393 		/* Adjust from VIN coefficients (for LM25056) */
394 		word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
395 		word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
396 		ret = pmbus_write_word_data(client, 0,
397 					    LM25056_VAUX_UV_WARN_LIMIT, word);
398 		pmbus_clear_cache(client);
399 		break;
400 	case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
401 		/* Adjust from VIN coefficients (for LM25056) */
402 		word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
403 		word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
404 		ret = pmbus_write_word_data(client, 0,
405 					    LM25056_VAUX_OV_WARN_LIMIT, word);
406 		pmbus_clear_cache(client);
407 		break;
408 	case PMBUS_VIRT_RESET_PIN_HISTORY:
409 		ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
410 		break;
411 	case PMBUS_VIRT_SAMPLES:
412 		word = clamp_val(word, 1, LM25066_SAMPLES_FOR_AVG_MAX);
413 		ret = pmbus_write_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG,
414 					    ilog2(word));
415 		break;
416 	default:
417 		ret = -ENODATA;
418 		break;
419 	}
420 	return ret;
421 }
422 
423 static int lm25066_probe(struct i2c_client *client,
424 			  const struct i2c_device_id *id)
425 {
426 	int config;
427 	struct lm25066_data *data;
428 	struct pmbus_driver_info *info;
429 	struct __coeff *coeff;
430 
431 	if (!i2c_check_functionality(client->adapter,
432 				     I2C_FUNC_SMBUS_READ_BYTE_DATA))
433 		return -ENODEV;
434 
435 	data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
436 			    GFP_KERNEL);
437 	if (!data)
438 		return -ENOMEM;
439 
440 	config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
441 	if (config < 0)
442 		return config;
443 
444 	data->id = id->driver_data;
445 	info = &data->info;
446 
447 	info->pages = 1;
448 	info->format[PSC_VOLTAGE_IN] = direct;
449 	info->format[PSC_VOLTAGE_OUT] = direct;
450 	info->format[PSC_CURRENT_IN] = direct;
451 	info->format[PSC_TEMPERATURE] = direct;
452 	info->format[PSC_POWER] = direct;
453 
454 	info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
455 	  | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
456 	  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_SAMPLES;
457 
458 	if (data->id == lm25056) {
459 		info->func[0] |= PMBUS_HAVE_STATUS_VMON;
460 		info->read_word_data = lm25056_read_word_data;
461 		info->read_byte_data = lm25056_read_byte_data;
462 		data->rlimit = 0x0fff;
463 	} else {
464 		info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
465 		info->read_word_data = lm25066_read_word_data;
466 		data->rlimit = 0x0fff;
467 	}
468 	info->write_word_data = lm25066_write_word_data;
469 
470 	coeff = &lm25066_coeff[data->id][0];
471 	info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
472 	info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
473 	info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
474 	info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
475 	info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
476 	info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
477 	info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
478 	info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
479 	info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
480 	info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
481 	info->R[PSC_POWER] = coeff[PSC_POWER].R;
482 	if (config & LM25066_DEV_SETUP_CL) {
483 		info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
484 		info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b;
485 		info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
486 		info->b[PSC_POWER] = coeff[PSC_POWER_L].b;
487 	} else {
488 		info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
489 		info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
490 		info->m[PSC_POWER] = coeff[PSC_POWER].m;
491 		info->b[PSC_POWER] = coeff[PSC_POWER].b;
492 	}
493 
494 	return pmbus_do_probe(client, id, info);
495 }
496 
497 static const struct i2c_device_id lm25066_id[] = {
498 	{"lm25056", lm25056},
499 	{"lm25066", lm25066},
500 	{"lm5064", lm5064},
501 	{"lm5066", lm5066},
502 	{"lm5066i", lm5066i},
503 	{ }
504 };
505 
506 MODULE_DEVICE_TABLE(i2c, lm25066_id);
507 
508 /* This is the driver that will be inserted */
509 static struct i2c_driver lm25066_driver = {
510 	.driver = {
511 		   .name = "lm25066",
512 		   },
513 	.probe = lm25066_probe,
514 	.remove = pmbus_do_remove,
515 	.id_table = lm25066_id,
516 };
517 
518 module_i2c_driver(lm25066_driver);
519 
520 MODULE_AUTHOR("Guenter Roeck");
521 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
522 MODULE_LICENSE("GPL");
523