2 * Hardware monitoring driver for LM25056 / LM25063 / LM25066 / LM5064 / LM5066
4 * Copyright (c) 2011 Ericsson AB.
5 * Copyright (c) 2013 Guenter Roeck
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/bitops.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
31 enum chips { lm25056, lm25063, lm25066, lm5064, lm5066, lm5066i };
33 #define LM25066_READ_VAUX 0xd0
34 #define LM25066_MFR_READ_IIN 0xd1
35 #define LM25066_MFR_READ_PIN 0xd2
36 #define LM25066_MFR_IIN_OC_WARN_LIMIT 0xd3
37 #define LM25066_MFR_PIN_OP_WARN_LIMIT 0xd4
38 #define LM25066_READ_PIN_PEAK 0xd5
39 #define LM25066_CLEAR_PIN_PEAK 0xd6
40 #define LM25066_DEVICE_SETUP 0xd9
41 #define LM25066_READ_AVG_VIN 0xdc
42 #define LM25066_READ_AVG_VOUT 0xdd
43 #define LM25066_READ_AVG_IIN 0xde
44 #define LM25066_READ_AVG_PIN 0xdf
46 #define LM25066_DEV_SETUP_CL BIT(4) /* Current limit */
50 #define LM25056_VAUX_OV_WARN_LIMIT 0xe3
51 #define LM25056_VAUX_UV_WARN_LIMIT 0xe4
53 #define LM25056_MFR_STS_VAUX_OV_WARN BIT(1)
54 #define LM25056_MFR_STS_VAUX_UV_WARN BIT(0)
58 #define LM25063_READ_VOUT_MAX 0xe5
59 #define LM25063_READ_VOUT_MIN 0xe6
65 #define PSC_CURRENT_IN_L (PSC_NUM_CLASSES)
66 #define PSC_POWER_L (PSC_NUM_CLASSES + 1)
68 static struct __coeff lm25066_coeff[6][PSC_NUM_CLASSES + 2] = {
80 [PSC_CURRENT_IN_L] = {
107 [PSC_VOLTAGE_OUT] = {
117 [PSC_CURRENT_IN_L] = {
132 [PSC_TEMPERATURE] = {
141 [PSC_VOLTAGE_OUT] = {
149 [PSC_CURRENT_IN_L] = {
161 [PSC_TEMPERATURE] = {
172 [PSC_VOLTAGE_OUT] = {
182 [PSC_CURRENT_IN_L] = {
197 [PSC_TEMPERATURE] = {
207 [PSC_VOLTAGE_OUT] = {
217 [PSC_CURRENT_IN_L] = {
232 [PSC_TEMPERATURE] = {
242 [PSC_VOLTAGE_OUT] = {
252 [PSC_CURRENT_IN_L] = {
267 [PSC_TEMPERATURE] = {
273 struct lm25066_data {
275 u16 rlimit; /* Maximum register value */
276 struct pmbus_driver_info info;
279 #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
281 static int lm25066_read_word_data(struct i2c_client *client, int page, int reg)
283 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
284 const struct lm25066_data *data = to_lm25066_data(info);
288 case PMBUS_VIRT_READ_VMON:
289 ret = pmbus_read_word_data(client, 0, LM25066_READ_VAUX);
292 /* Adjust returned value to match VIN coefficients */
295 /* VIN: 6.14 mV VAUX: 293 uV LSB */
296 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
299 /* VIN: 6.25 mV VAUX: 200.0 uV LSB */
300 ret = DIV_ROUND_CLOSEST(ret * 20, 625);
303 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
304 ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
307 /* VIN: 4.53 mV VAUX: 700 uV LSB */
308 ret = DIV_ROUND_CLOSEST(ret * 70, 453);
312 /* VIN: 2.18 mV VAUX: 725 uV LSB */
313 ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
318 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
321 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
323 case PMBUS_IIN_OC_WARN_LIMIT:
324 ret = pmbus_read_word_data(client, 0,
325 LM25066_MFR_IIN_OC_WARN_LIMIT);
327 case PMBUS_PIN_OP_WARN_LIMIT:
328 ret = pmbus_read_word_data(client, 0,
329 LM25066_MFR_PIN_OP_WARN_LIMIT);
331 case PMBUS_VIRT_READ_VIN_AVG:
332 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
334 case PMBUS_VIRT_READ_VOUT_AVG:
335 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
337 case PMBUS_VIRT_READ_IIN_AVG:
338 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
340 case PMBUS_VIRT_READ_PIN_AVG:
341 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
343 case PMBUS_VIRT_READ_PIN_MAX:
344 ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
346 case PMBUS_VIRT_RESET_PIN_HISTORY:
356 static int lm25063_read_word_data(struct i2c_client *client, int page, int reg)
361 case PMBUS_VIRT_READ_VOUT_MAX:
362 ret = pmbus_read_word_data(client, 0, LM25063_READ_VOUT_MAX);
364 case PMBUS_VIRT_READ_VOUT_MIN:
365 ret = pmbus_read_word_data(client, 0, LM25063_READ_VOUT_MIN);
368 ret = lm25066_read_word_data(client, page, reg);
374 static int lm25056_read_word_data(struct i2c_client *client, int page, int reg)
379 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
380 ret = pmbus_read_word_data(client, 0,
381 LM25056_VAUX_UV_WARN_LIMIT);
384 /* Adjust returned value to match VIN coefficients */
385 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
387 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
388 ret = pmbus_read_word_data(client, 0,
389 LM25056_VAUX_OV_WARN_LIMIT);
392 /* Adjust returned value to match VIN coefficients */
393 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
396 ret = lm25066_read_word_data(client, page, reg);
402 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
407 case PMBUS_VIRT_STATUS_VMON:
408 ret = pmbus_read_byte_data(client, 0,
409 PMBUS_STATUS_MFR_SPECIFIC);
413 if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
414 s |= PB_VOLTAGE_UV_WARNING;
415 if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
416 s |= PB_VOLTAGE_OV_WARNING;
426 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
429 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
430 const struct lm25066_data *data = to_lm25066_data(info);
434 case PMBUS_POUT_OP_FAULT_LIMIT:
435 case PMBUS_POUT_OP_WARN_LIMIT:
436 case PMBUS_VOUT_UV_WARN_LIMIT:
437 case PMBUS_OT_FAULT_LIMIT:
438 case PMBUS_OT_WARN_LIMIT:
439 case PMBUS_IIN_OC_FAULT_LIMIT:
440 case PMBUS_VIN_UV_WARN_LIMIT:
441 case PMBUS_VIN_UV_FAULT_LIMIT:
442 case PMBUS_VIN_OV_FAULT_LIMIT:
443 case PMBUS_VIN_OV_WARN_LIMIT:
444 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
445 ret = pmbus_write_word_data(client, 0, reg, word);
446 pmbus_clear_cache(client);
448 case PMBUS_IIN_OC_WARN_LIMIT:
449 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
450 ret = pmbus_write_word_data(client, 0,
451 LM25066_MFR_IIN_OC_WARN_LIMIT,
453 pmbus_clear_cache(client);
455 case PMBUS_PIN_OP_WARN_LIMIT:
456 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
457 ret = pmbus_write_word_data(client, 0,
458 LM25066_MFR_PIN_OP_WARN_LIMIT,
460 pmbus_clear_cache(client);
462 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
463 /* Adjust from VIN coefficients (for LM25056) */
464 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
465 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
466 ret = pmbus_write_word_data(client, 0,
467 LM25056_VAUX_UV_WARN_LIMIT, word);
468 pmbus_clear_cache(client);
470 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
471 /* Adjust from VIN coefficients (for LM25056) */
472 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
473 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
474 ret = pmbus_write_word_data(client, 0,
475 LM25056_VAUX_OV_WARN_LIMIT, word);
476 pmbus_clear_cache(client);
478 case PMBUS_VIRT_RESET_PIN_HISTORY:
479 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
488 static int lm25066_probe(struct i2c_client *client,
489 const struct i2c_device_id *id)
492 struct lm25066_data *data;
493 struct pmbus_driver_info *info;
494 struct __coeff *coeff;
496 if (!i2c_check_functionality(client->adapter,
497 I2C_FUNC_SMBUS_READ_BYTE_DATA))
500 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
505 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
509 data->id = id->driver_data;
513 info->format[PSC_VOLTAGE_IN] = direct;
514 info->format[PSC_VOLTAGE_OUT] = direct;
515 info->format[PSC_CURRENT_IN] = direct;
516 info->format[PSC_TEMPERATURE] = direct;
517 info->format[PSC_POWER] = direct;
519 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
520 | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
521 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
523 if (data->id == lm25056) {
524 info->func[0] |= PMBUS_HAVE_STATUS_VMON;
525 info->read_word_data = lm25056_read_word_data;
526 info->read_byte_data = lm25056_read_byte_data;
527 data->rlimit = 0x0fff;
528 } else if (data->id == lm25063) {
529 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
531 info->read_word_data = lm25063_read_word_data;
532 data->rlimit = 0xffff;
534 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
535 info->read_word_data = lm25066_read_word_data;
536 data->rlimit = 0x0fff;
538 info->write_word_data = lm25066_write_word_data;
540 coeff = &lm25066_coeff[data->id][0];
541 info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
542 info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
543 info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
544 info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
545 info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
546 info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
547 info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
548 info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
549 info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
550 info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
551 info->R[PSC_POWER] = coeff[PSC_POWER].R;
552 if (config & LM25066_DEV_SETUP_CL) {
553 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
554 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b;
555 info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
556 info->b[PSC_POWER] = coeff[PSC_POWER_L].b;
558 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
559 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
560 info->m[PSC_POWER] = coeff[PSC_POWER].m;
561 info->b[PSC_POWER] = coeff[PSC_POWER].b;
564 return pmbus_do_probe(client, id, info);
567 static const struct i2c_device_id lm25066_id[] = {
568 {"lm25056", lm25056},
569 {"lm25063", lm25063},
570 {"lm25066", lm25066},
573 {"lm5066i", lm5066i},
577 MODULE_DEVICE_TABLE(i2c, lm25066_id);
579 /* This is the driver that will be inserted */
580 static struct i2c_driver lm25066_driver = {
584 .probe = lm25066_probe,
585 .remove = pmbus_do_remove,
586 .id_table = lm25066_id,
589 module_i2c_driver(lm25066_driver);
591 MODULE_AUTHOR("Guenter Roeck");
592 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
593 MODULE_LICENSE("GPL");