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 pmbus_clear_cache(client);
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 pmbus_clear_cache(client);
405 break;
406 case PMBUS_PIN_OP_WARN_LIMIT:
407 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
408 ret = pmbus_write_word_data(client, 0,
409 LM25066_MFR_PIN_OP_WARN_LIMIT,
410 word);
411 pmbus_clear_cache(client);
412 break;
413 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
414 /* Adjust from VIN coefficients (for LM25056) */
415 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
416 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
417 ret = pmbus_write_word_data(client, 0,
418 LM25056_VAUX_UV_WARN_LIMIT, word);
419 pmbus_clear_cache(client);
420 break;
421 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
422 /* Adjust from VIN coefficients (for LM25056) */
423 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
424 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
425 ret = pmbus_write_word_data(client, 0,
426 LM25056_VAUX_OV_WARN_LIMIT, word);
427 pmbus_clear_cache(client);
428 break;
429 case PMBUS_VIRT_RESET_PIN_HISTORY:
430 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
431 break;
432 case PMBUS_VIRT_SAMPLES:
433 word = clamp_val(word, 1, LM25066_SAMPLES_FOR_AVG_MAX);
434 ret = pmbus_write_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG,
435 ilog2(word));
436 break;
437 default:
438 ret = -ENODATA;
439 break;
440 }
441 return ret;
442 }
443
lm25066_probe(struct i2c_client * client)444 static int lm25066_probe(struct i2c_client *client)
445 {
446 int config;
447 struct lm25066_data *data;
448 struct pmbus_driver_info *info;
449 struct __coeff *coeff;
450
451 if (!i2c_check_functionality(client->adapter,
452 I2C_FUNC_SMBUS_READ_BYTE_DATA))
453 return -ENODEV;
454
455 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
456 GFP_KERNEL);
457 if (!data)
458 return -ENOMEM;
459
460 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
461 if (config < 0)
462 return config;
463
464 data->id = i2c_match_id(lm25066_id, client)->driver_data;
465 info = &data->info;
466
467 info->pages = 1;
468 info->format[PSC_VOLTAGE_IN] = direct;
469 info->format[PSC_VOLTAGE_OUT] = direct;
470 info->format[PSC_CURRENT_IN] = direct;
471 info->format[PSC_TEMPERATURE] = direct;
472 info->format[PSC_POWER] = direct;
473
474 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
475 | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
476 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_SAMPLES;
477
478 if (data->id == lm25056) {
479 info->func[0] |= PMBUS_HAVE_STATUS_VMON;
480 info->read_word_data = lm25056_read_word_data;
481 info->read_byte_data = lm25056_read_byte_data;
482 data->rlimit = 0x0fff;
483 } else {
484 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
485 info->read_word_data = lm25066_read_word_data;
486 data->rlimit = 0x0fff;
487 }
488 info->write_word_data = lm25066_write_word_data;
489
490 coeff = &lm25066_coeff[data->id][0];
491 info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
492 info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
493 info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
494 info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
495 info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
496 info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
497 info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
498 info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
499 info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
500 info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
501 info->R[PSC_POWER] = coeff[PSC_POWER].R;
502 if (config & LM25066_DEV_SETUP_CL) {
503 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
504 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b;
505 info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
506 info->b[PSC_POWER] = coeff[PSC_POWER_L].b;
507 } else {
508 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
509 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
510 info->m[PSC_POWER] = coeff[PSC_POWER].m;
511 info->b[PSC_POWER] = coeff[PSC_POWER].b;
512 }
513
514 return pmbus_do_probe(client, info);
515 }
516
517 static const struct i2c_device_id lm25066_id[] = {
518 {"lm25056", lm25056},
519 {"lm25066", lm25066},
520 {"lm5064", lm5064},
521 {"lm5066", lm5066},
522 {"lm5066i", lm5066i},
523 { }
524 };
525
526 MODULE_DEVICE_TABLE(i2c, lm25066_id);
527
528 /* This is the driver that will be inserted */
529 static struct i2c_driver lm25066_driver = {
530 .driver = {
531 .name = "lm25066",
532 },
533 .probe_new = lm25066_probe,
534 .remove = pmbus_do_remove,
535 .id_table = lm25066_id,
536 };
537
538 module_i2c_driver(lm25066_driver);
539
540 MODULE_AUTHOR("Guenter Roeck");
541 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
542 MODULE_LICENSE("GPL");
543