1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Hardware monitoring driver for LM25056 / LM25066 / LM5064 / LM5066
5 * Copyright (c) 2011 Ericsson AB.
6 * Copyright (c) 2013 Guenter Roeck
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>
19 enum chips { lm25056, lm25066, lm5064, lm5066, lm5066i };
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
35 #define LM25066_DEV_SETUP_CL BIT(4) /* Current limit */
37 #define LM25066_SAMPLES_FOR_AVG_MAX 4096
41 #define LM25056_VAUX_OV_WARN_LIMIT 0xe3
42 #define LM25056_VAUX_UV_WARN_LIMIT 0xe4
44 #define LM25056_MFR_STS_VAUX_OV_WARN BIT(1)
45 #define LM25056_MFR_STS_VAUX_UV_WARN BIT(0)
51 #define PSC_CURRENT_IN_L (PSC_NUM_CLASSES)
52 #define PSC_POWER_L (PSC_NUM_CLASSES + 1)
54 static struct __coeff lm25066_coeff[][PSC_NUM_CLASSES + 2] = {
66 [PSC_CURRENT_IN_L] = {
103 [PSC_CURRENT_IN_L] = {
118 [PSC_TEMPERATURE] = {
128 [PSC_VOLTAGE_OUT] = {
138 [PSC_CURRENT_IN_L] = {
153 [PSC_TEMPERATURE] = {
163 [PSC_VOLTAGE_OUT] = {
173 [PSC_CURRENT_IN_L] = {
188 [PSC_TEMPERATURE] = {
198 [PSC_VOLTAGE_OUT] = {
208 [PSC_CURRENT_IN_L] = {
223 [PSC_TEMPERATURE] = {
229 struct lm25066_data {
231 u16 rlimit; /* Maximum register value */
232 struct pmbus_driver_info info;
235 #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
237 static const struct i2c_device_id lm25066_id[];
239 static int lm25066_read_word_data(struct i2c_client *client, int page,
242 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
243 const struct lm25066_data *data = to_lm25066_data(info);
247 case PMBUS_VIRT_READ_VMON:
248 ret = pmbus_read_word_data(client, 0, 0xff, LM25066_READ_VAUX);
251 /* Adjust returned value to match VIN coefficients */
254 /* VIN: 6.14 mV VAUX: 293 uV LSB */
255 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
258 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
259 ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
262 /* VIN: 4.53 mV VAUX: 700 uV LSB */
263 ret = DIV_ROUND_CLOSEST(ret * 70, 453);
267 /* VIN: 2.18 mV VAUX: 725 uV LSB */
268 ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
273 ret = pmbus_read_word_data(client, 0, 0xff,
274 LM25066_MFR_READ_IIN);
277 ret = pmbus_read_word_data(client, 0, 0xff,
278 LM25066_MFR_READ_PIN);
280 case PMBUS_IIN_OC_WARN_LIMIT:
281 ret = pmbus_read_word_data(client, 0, 0xff,
282 LM25066_MFR_IIN_OC_WARN_LIMIT);
284 case PMBUS_PIN_OP_WARN_LIMIT:
285 ret = pmbus_read_word_data(client, 0, 0xff,
286 LM25066_MFR_PIN_OP_WARN_LIMIT);
288 case PMBUS_VIRT_READ_VIN_AVG:
289 ret = pmbus_read_word_data(client, 0, 0xff,
290 LM25066_READ_AVG_VIN);
292 case PMBUS_VIRT_READ_VOUT_AVG:
293 ret = pmbus_read_word_data(client, 0, 0xff,
294 LM25066_READ_AVG_VOUT);
296 case PMBUS_VIRT_READ_IIN_AVG:
297 ret = pmbus_read_word_data(client, 0, 0xff,
298 LM25066_READ_AVG_IIN);
300 case PMBUS_VIRT_READ_PIN_AVG:
301 ret = pmbus_read_word_data(client, 0, 0xff,
302 LM25066_READ_AVG_PIN);
304 case PMBUS_VIRT_READ_PIN_MAX:
305 ret = pmbus_read_word_data(client, 0, 0xff,
306 LM25066_READ_PIN_PEAK);
308 case PMBUS_VIRT_RESET_PIN_HISTORY:
311 case PMBUS_VIRT_SAMPLES:
312 ret = pmbus_read_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG);
324 static int lm25056_read_word_data(struct i2c_client *client, int page,
330 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
331 ret = pmbus_read_word_data(client, 0, 0xff,
332 LM25056_VAUX_UV_WARN_LIMIT);
335 /* Adjust returned value to match VIN coefficients */
336 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
338 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
339 ret = pmbus_read_word_data(client, 0, 0xff,
340 LM25056_VAUX_OV_WARN_LIMIT);
343 /* Adjust returned value to match VIN coefficients */
344 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
347 ret = lm25066_read_word_data(client, page, phase, reg);
353 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
358 case PMBUS_VIRT_STATUS_VMON:
359 ret = pmbus_read_byte_data(client, 0,
360 PMBUS_STATUS_MFR_SPECIFIC);
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;
377 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
380 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
381 const struct lm25066_data *data = to_lm25066_data(info);
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);
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,
404 pmbus_clear_cache(client);
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,
411 pmbus_clear_cache(client);
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);
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);
429 case PMBUS_VIRT_RESET_PIN_HISTORY:
430 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
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,
444 static int lm25066_probe(struct i2c_client *client)
447 struct lm25066_data *data;
448 struct pmbus_driver_info *info;
449 struct __coeff *coeff;
451 if (!i2c_check_functionality(client->adapter,
452 I2C_FUNC_SMBUS_READ_BYTE_DATA))
455 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
460 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
464 data->id = i2c_match_id(lm25066_id, client)->driver_data;
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;
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;
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;
484 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
485 info->read_word_data = lm25066_read_word_data;
486 data->rlimit = 0x0fff;
488 info->write_word_data = lm25066_write_word_data;
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;
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;
514 return pmbus_do_probe(client, info);
517 static const struct i2c_device_id lm25066_id[] = {
518 {"lm25056", lm25056},
519 {"lm25066", lm25066},
522 {"lm5066i", lm5066i},
526 MODULE_DEVICE_TABLE(i2c, lm25066_id);
528 /* This is the driver that will be inserted */
529 static struct i2c_driver lm25066_driver = {
533 .probe_new = lm25066_probe,
534 .remove = pmbus_do_remove,
535 .id_table = lm25066_id,
538 module_i2c_driver(lm25066_driver);
540 MODULE_AUTHOR("Guenter Roeck");
541 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
542 MODULE_LICENSE("GPL");