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 <linux/math.h>
18 #include <linux/of.h>
19 #include "pmbus.h"
20
21 enum chips { lm25056, lm25066, lm5064, lm5066, lm5066i };
22
23 #define LM25066_READ_VAUX 0xd0
24 #define LM25066_MFR_READ_IIN 0xd1
25 #define LM25066_MFR_READ_PIN 0xd2
26 #define LM25066_MFR_IIN_OC_WARN_LIMIT 0xd3
27 #define LM25066_MFR_PIN_OP_WARN_LIMIT 0xd4
28 #define LM25066_READ_PIN_PEAK 0xd5
29 #define LM25066_CLEAR_PIN_PEAK 0xd6
30 #define LM25066_DEVICE_SETUP 0xd9
31 #define LM25066_READ_AVG_VIN 0xdc
32 #define LM25066_SAMPLES_FOR_AVG 0xdb
33 #define LM25066_READ_AVG_VOUT 0xdd
34 #define LM25066_READ_AVG_IIN 0xde
35 #define LM25066_READ_AVG_PIN 0xdf
36
37 #define LM25066_DEV_SETUP_CL BIT(4) /* Current limit */
38 #define LM25066_DEV_SETUP_CL_CFG BIT(2) /* Current limit configuration */
39
40 #define LM25066_SAMPLES_FOR_AVG_MAX 4096
41
42 /* LM25056 only */
43
44 #define LM25056_VAUX_OV_WARN_LIMIT 0xe3
45 #define LM25056_VAUX_UV_WARN_LIMIT 0xe4
46
47 #define LM25056_MFR_STS_VAUX_OV_WARN BIT(1)
48 #define LM25056_MFR_STS_VAUX_UV_WARN BIT(0)
49
50 struct __coeff {
51 short m, b, R;
52 };
53
54 #define PSC_CURRENT_IN_L (PSC_NUM_CLASSES)
55 #define PSC_POWER_L (PSC_NUM_CLASSES + 1)
56
57 static const struct __coeff lm25066_coeff[][PSC_NUM_CLASSES + 2] = {
58 [lm25056] = {
59 [PSC_VOLTAGE_IN] = {
60 .m = 16296,
61 .b = 1343,
62 .R = -2,
63 },
64 [PSC_CURRENT_IN] = {
65 .m = 13797,
66 .b = -1833,
67 .R = -2,
68 },
69 [PSC_CURRENT_IN_L] = {
70 .m = 6726,
71 .b = -537,
72 .R = -2,
73 },
74 [PSC_POWER] = {
75 .m = 5501,
76 .b = -2908,
77 .R = -3,
78 },
79 [PSC_POWER_L] = {
80 .m = 26882,
81 .b = -5646,
82 .R = -4,
83 },
84 [PSC_TEMPERATURE] = {
85 .m = 1580,
86 .b = -14500,
87 .R = -2,
88 },
89 },
90 [lm25066] = {
91 [PSC_VOLTAGE_IN] = {
92 .m = 22070,
93 .b = -1800,
94 .R = -2,
95 },
96 [PSC_VOLTAGE_OUT] = {
97 .m = 22070,
98 .b = -1800,
99 .R = -2,
100 },
101 [PSC_CURRENT_IN] = {
102 .m = 13661,
103 .b = -5200,
104 .R = -2,
105 },
106 [PSC_CURRENT_IN_L] = {
107 .m = 6854,
108 .b = -3100,
109 .R = -2,
110 },
111 [PSC_POWER] = {
112 .m = 736,
113 .b = -3300,
114 .R = -2,
115 },
116 [PSC_POWER_L] = {
117 .m = 369,
118 .b = -1900,
119 .R = -2,
120 },
121 [PSC_TEMPERATURE] = {
122 .m = 16,
123 },
124 },
125 [lm5064] = {
126 [PSC_VOLTAGE_IN] = {
127 .m = 4611,
128 .b = -642,
129 .R = -2,
130 },
131 [PSC_VOLTAGE_OUT] = {
132 .m = 4621,
133 .b = 423,
134 .R = -2,
135 },
136 [PSC_CURRENT_IN] = {
137 .m = 5456,
138 .b = 2118,
139 .R = -2,
140 },
141 [PSC_CURRENT_IN_L] = {
142 .m = 10742,
143 .b = 1552,
144 .R = -2,
145 },
146 [PSC_POWER] = {
147 .m = 612,
148 .b = 11202,
149 .R = -3,
150 },
151 [PSC_POWER_L] = {
152 .m = 1204,
153 .b = 8524,
154 .R = -3,
155 },
156 [PSC_TEMPERATURE] = {
157 .m = 16,
158 },
159 },
160 [lm5066] = {
161 [PSC_VOLTAGE_IN] = {
162 .m = 4587,
163 .b = -1200,
164 .R = -2,
165 },
166 [PSC_VOLTAGE_OUT] = {
167 .m = 4587,
168 .b = -2400,
169 .R = -2,
170 },
171 [PSC_CURRENT_IN] = {
172 .m = 5405,
173 .b = -600,
174 .R = -2,
175 },
176 [PSC_CURRENT_IN_L] = {
177 .m = 10753,
178 .b = -1200,
179 .R = -2,
180 },
181 [PSC_POWER] = {
182 .m = 605,
183 .b = -8000,
184 .R = -3,
185 },
186 [PSC_POWER_L] = {
187 .m = 1204,
188 .b = -6000,
189 .R = -3,
190 },
191 [PSC_TEMPERATURE] = {
192 .m = 16,
193 },
194 },
195 [lm5066i] = {
196 [PSC_VOLTAGE_IN] = {
197 .m = 4617,
198 .b = -140,
199 .R = -2,
200 },
201 [PSC_VOLTAGE_OUT] = {
202 .m = 4602,
203 .b = 500,
204 .R = -2,
205 },
206 [PSC_CURRENT_IN] = {
207 .m = 7645,
208 .b = 100,
209 .R = -2,
210 },
211 [PSC_CURRENT_IN_L] = {
212 .m = 15076,
213 .b = -504,
214 .R = -2,
215 },
216 [PSC_POWER] = {
217 .m = 861,
218 .b = -965,
219 .R = -3,
220 },
221 [PSC_POWER_L] = {
222 .m = 1701,
223 .b = -4000,
224 .R = -3,
225 },
226 [PSC_TEMPERATURE] = {
227 .m = 16,
228 },
229 },
230 };
231
232 struct lm25066_data {
233 int id;
234 u16 rlimit; /* Maximum register value */
235 struct pmbus_driver_info info;
236 };
237
238 #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
239
lm25066_read_word_data(struct i2c_client * client,int page,int phase,int reg)240 static int lm25066_read_word_data(struct i2c_client *client, int page,
241 int phase, int reg)
242 {
243 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
244 const struct lm25066_data *data = to_lm25066_data(info);
245 int ret;
246
247 switch (reg) {
248 case PMBUS_VIRT_READ_VMON:
249 ret = pmbus_read_word_data(client, 0, 0xff, LM25066_READ_VAUX);
250 if (ret < 0)
251 break;
252 /* Adjust returned value to match VIN coefficients */
253 switch (data->id) {
254 case lm25056:
255 /* VIN: 6.14 mV VAUX: 293 uV LSB */
256 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
257 break;
258 case lm25066:
259 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
260 ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
261 break;
262 case lm5064:
263 /* VIN: 4.53 mV VAUX: 700 uV LSB */
264 ret = DIV_ROUND_CLOSEST(ret * 70, 453);
265 break;
266 case lm5066:
267 case lm5066i:
268 /* VIN: 2.18 mV VAUX: 725 uV LSB */
269 ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
270 break;
271 }
272 break;
273 case PMBUS_READ_IIN:
274 ret = pmbus_read_word_data(client, 0, 0xff,
275 LM25066_MFR_READ_IIN);
276 break;
277 case PMBUS_READ_PIN:
278 ret = pmbus_read_word_data(client, 0, 0xff,
279 LM25066_MFR_READ_PIN);
280 break;
281 case PMBUS_IIN_OC_WARN_LIMIT:
282 ret = pmbus_read_word_data(client, 0, 0xff,
283 LM25066_MFR_IIN_OC_WARN_LIMIT);
284 break;
285 case PMBUS_PIN_OP_WARN_LIMIT:
286 ret = pmbus_read_word_data(client, 0, 0xff,
287 LM25066_MFR_PIN_OP_WARN_LIMIT);
288 break;
289 case PMBUS_VIRT_READ_VIN_AVG:
290 ret = pmbus_read_word_data(client, 0, 0xff,
291 LM25066_READ_AVG_VIN);
292 break;
293 case PMBUS_VIRT_READ_VOUT_AVG:
294 ret = pmbus_read_word_data(client, 0, 0xff,
295 LM25066_READ_AVG_VOUT);
296 break;
297 case PMBUS_VIRT_READ_IIN_AVG:
298 ret = pmbus_read_word_data(client, 0, 0xff,
299 LM25066_READ_AVG_IIN);
300 break;
301 case PMBUS_VIRT_READ_PIN_AVG:
302 ret = pmbus_read_word_data(client, 0, 0xff,
303 LM25066_READ_AVG_PIN);
304 break;
305 case PMBUS_VIRT_READ_PIN_MAX:
306 ret = pmbus_read_word_data(client, 0, 0xff,
307 LM25066_READ_PIN_PEAK);
308 break;
309 case PMBUS_VIRT_RESET_PIN_HISTORY:
310 ret = 0;
311 break;
312 case PMBUS_VIRT_SAMPLES:
313 ret = pmbus_read_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG);
314 if (ret < 0)
315 break;
316 ret = 1 << ret;
317 break;
318 default:
319 ret = -ENODATA;
320 break;
321 }
322 return ret;
323 }
324
lm25056_read_word_data(struct i2c_client * client,int page,int phase,int reg)325 static int lm25056_read_word_data(struct i2c_client *client, int page,
326 int phase, int reg)
327 {
328 int ret;
329
330 switch (reg) {
331 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
332 ret = pmbus_read_word_data(client, 0, 0xff,
333 LM25056_VAUX_UV_WARN_LIMIT);
334 if (ret < 0)
335 break;
336 /* Adjust returned value to match VIN coefficients */
337 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
338 break;
339 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
340 ret = pmbus_read_word_data(client, 0, 0xff,
341 LM25056_VAUX_OV_WARN_LIMIT);
342 if (ret < 0)
343 break;
344 /* Adjust returned value to match VIN coefficients */
345 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
346 break;
347 default:
348 ret = lm25066_read_word_data(client, page, phase, reg);
349 break;
350 }
351 return ret;
352 }
353
lm25056_read_byte_data(struct i2c_client * client,int page,int reg)354 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
355 {
356 int ret, s;
357
358 switch (reg) {
359 case PMBUS_VIRT_STATUS_VMON:
360 ret = pmbus_read_byte_data(client, 0,
361 PMBUS_STATUS_MFR_SPECIFIC);
362 if (ret < 0)
363 break;
364 s = 0;
365 if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
366 s |= PB_VOLTAGE_UV_WARNING;
367 if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
368 s |= PB_VOLTAGE_OV_WARNING;
369 ret = s;
370 break;
371 default:
372 ret = -ENODATA;
373 break;
374 }
375 return ret;
376 }
377
lm25066_write_word_data(struct i2c_client * client,int page,int reg,u16 word)378 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
379 u16 word)
380 {
381 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
382 const struct lm25066_data *data = to_lm25066_data(info);
383 int ret;
384
385 switch (reg) {
386 case PMBUS_POUT_OP_FAULT_LIMIT:
387 case PMBUS_POUT_OP_WARN_LIMIT:
388 case PMBUS_VOUT_UV_WARN_LIMIT:
389 case PMBUS_OT_FAULT_LIMIT:
390 case PMBUS_OT_WARN_LIMIT:
391 case PMBUS_IIN_OC_FAULT_LIMIT:
392 case PMBUS_VIN_UV_WARN_LIMIT:
393 case PMBUS_VIN_UV_FAULT_LIMIT:
394 case PMBUS_VIN_OV_FAULT_LIMIT:
395 case PMBUS_VIN_OV_WARN_LIMIT:
396 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
397 ret = pmbus_write_word_data(client, 0, reg, word);
398 break;
399 case PMBUS_IIN_OC_WARN_LIMIT:
400 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
401 ret = pmbus_write_word_data(client, 0,
402 LM25066_MFR_IIN_OC_WARN_LIMIT,
403 word);
404 break;
405 case PMBUS_PIN_OP_WARN_LIMIT:
406 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
407 ret = pmbus_write_word_data(client, 0,
408 LM25066_MFR_PIN_OP_WARN_LIMIT,
409 word);
410 break;
411 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
412 /* Adjust from VIN coefficients (for LM25056) */
413 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
414 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
415 ret = pmbus_write_word_data(client, 0,
416 LM25056_VAUX_UV_WARN_LIMIT, word);
417 break;
418 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
419 /* Adjust from VIN coefficients (for LM25056) */
420 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
421 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
422 ret = pmbus_write_word_data(client, 0,
423 LM25056_VAUX_OV_WARN_LIMIT, word);
424 break;
425 case PMBUS_VIRT_RESET_PIN_HISTORY:
426 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
427 break;
428 case PMBUS_VIRT_SAMPLES:
429 word = clamp_val(word, 1, LM25066_SAMPLES_FOR_AVG_MAX);
430 ret = pmbus_write_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG,
431 ilog2(word));
432 break;
433 default:
434 ret = -ENODATA;
435 break;
436 }
437 return ret;
438 }
439
440 #if IS_ENABLED(CONFIG_SENSORS_LM25066_REGULATOR)
441 static const struct regulator_desc lm25066_reg_desc[] = {
442 PMBUS_REGULATOR_ONE_NODE("vout"),
443 };
444 #endif
445
446 static const struct i2c_device_id lm25066_id[] = {
447 { .name = "lm25056", .driver_data = lm25056 },
448 { .name = "lm25066", .driver_data = lm25066 },
449 { .name = "lm5064", .driver_data = lm5064 },
450 { .name = "lm5066", .driver_data = lm5066 },
451 { .name = "lm5066i", .driver_data = lm5066i },
452 { }
453 };
454 MODULE_DEVICE_TABLE(i2c, lm25066_id);
455
456 static const struct of_device_id __maybe_unused lm25066_of_match[] = {
457 { .compatible = "ti,lm25056", .data = (void *)lm25056, },
458 { .compatible = "ti,lm25066", .data = (void *)lm25066, },
459 { .compatible = "ti,lm5064", .data = (void *)lm5064, },
460 { .compatible = "ti,lm5066", .data = (void *)lm5066, },
461 { .compatible = "ti,lm5066i", .data = (void *)lm5066i, },
462 { },
463 };
464 MODULE_DEVICE_TABLE(of, lm25066_of_match);
465
lm25066_probe(struct i2c_client * client)466 static int lm25066_probe(struct i2c_client *client)
467 {
468 int config;
469 u32 shunt;
470 struct lm25066_data *data;
471 struct pmbus_driver_info *info;
472 const struct __coeff *coeff;
473
474 if (!i2c_check_functionality(client->adapter,
475 I2C_FUNC_SMBUS_READ_BYTE_DATA))
476 return -ENODEV;
477
478 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
479 GFP_KERNEL);
480 if (!data)
481 return -ENOMEM;
482
483 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
484 if (config < 0)
485 return config;
486
487 data->id = (enum chips)(unsigned long)i2c_get_match_data(client);
488
489 if (data->id != lm25056) {
490 int config_new = config;
491 const char *cl_setting;
492 int ret;
493
494 if (!of_property_read_string(client->dev.of_node,
495 "ti,current-range", &cl_setting)) {
496 config_new |= LM25066_DEV_SETUP_CL_CFG;
497 if (strcmp(cl_setting, "high") == 0) {
498 if (data->id == lm25066)
499 config_new |= LM25066_DEV_SETUP_CL;
500 else
501 config_new &= ~LM25066_DEV_SETUP_CL;
502 } else if (strcmp(cl_setting, "low") == 0) {
503 if (data->id == lm25066)
504 config_new &= ~LM25066_DEV_SETUP_CL;
505 else
506 config_new |= LM25066_DEV_SETUP_CL;
507 } else {
508 dev_err(&client->dev,
509 "invalid current-range setting: %s\n",
510 cl_setting);
511 return -EINVAL;
512 }
513 }
514
515 if (config_new != config) {
516 ret = i2c_smbus_write_byte_data(client,
517 LM25066_DEVICE_SETUP,
518 config_new);
519 if (ret < 0)
520 return ret;
521 config = config_new;
522 }
523 }
524
525 info = &data->info;
526
527 info->pages = 1;
528 info->format[PSC_VOLTAGE_IN] = direct;
529 info->format[PSC_VOLTAGE_OUT] = direct;
530 info->format[PSC_CURRENT_IN] = direct;
531 info->format[PSC_TEMPERATURE] = direct;
532 info->format[PSC_POWER] = direct;
533
534 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
535 | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
536 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_SAMPLES;
537
538 if (data->id == lm25056) {
539 info->func[0] |= PMBUS_HAVE_STATUS_VMON;
540 info->read_word_data = lm25056_read_word_data;
541 info->read_byte_data = lm25056_read_byte_data;
542 data->rlimit = 0x0fff;
543 } else {
544 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
545 info->read_word_data = lm25066_read_word_data;
546 data->rlimit = 0x0fff;
547 }
548 info->write_word_data = lm25066_write_word_data;
549
550 coeff = &lm25066_coeff[data->id][0];
551 info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
552 info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
553 info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
554 info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
555 info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
556 info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
557 info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
558 info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
559 info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
560 if (config & LM25066_DEV_SETUP_CL) {
561 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
562 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b;
563 info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].R;
564 info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
565 info->b[PSC_POWER] = coeff[PSC_POWER_L].b;
566 info->R[PSC_POWER] = coeff[PSC_POWER_L].R;
567 } else {
568 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
569 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
570 info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
571 info->m[PSC_POWER] = coeff[PSC_POWER].m;
572 info->b[PSC_POWER] = coeff[PSC_POWER].b;
573 info->R[PSC_POWER] = coeff[PSC_POWER].R;
574 }
575
576 /*
577 * Values in the TI datasheets are normalized for a 1mOhm sense
578 * resistor; assume that unless DT specifies a value explicitly.
579 */
580 if (of_property_read_u32(client->dev.of_node, "shunt-resistor-micro-ohms", &shunt))
581 shunt = 1000;
582
583 info->m[PSC_CURRENT_IN] = DIV_ROUND_CLOSEST_ULL((u64)info->m[PSC_CURRENT_IN] * shunt, 1000);
584 info->m[PSC_POWER] = DIV_ROUND_CLOSEST_ULL((u64)info->m[PSC_POWER] * shunt, 1000);
585
586 #if IS_ENABLED(CONFIG_SENSORS_LM25066_REGULATOR)
587 /* LM25056 doesn't support OPERATION */
588 if (data->id != lm25056) {
589 info->num_regulators = ARRAY_SIZE(lm25066_reg_desc);
590 info->reg_desc = lm25066_reg_desc;
591 }
592 #endif
593
594 return pmbus_do_probe(client, info);
595 }
596
597 /* This is the driver that will be inserted */
598 static struct i2c_driver lm25066_driver = {
599 .driver = {
600 .name = "lm25066",
601 .of_match_table = of_match_ptr(lm25066_of_match),
602 },
603 .probe = lm25066_probe,
604 .id_table = lm25066_id,
605 };
606
607 module_i2c_driver(lm25066_driver);
608
609 MODULE_AUTHOR("Guenter Roeck");
610 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
611 MODULE_LICENSE("GPL");
612 MODULE_IMPORT_NS("PMBUS");
613