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