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