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>
20 enum chips
{ lm25056
, lm25066
, lm5064
, lm5066
, lm5066i
};
22 #define LM25066_READ_VAUX 0xd0
23 #define LM25066_MFR_READ_IIN 0xd1
24 #define LM25066_MFR_READ_PIN 0xd2
25 #define LM25066_MFR_IIN_OC_WARN_LIMIT 0xd3
26 #define LM25066_MFR_PIN_OP_WARN_LIMIT 0xd4
27 #define LM25066_READ_PIN_PEAK 0xd5
28 #define LM25066_CLEAR_PIN_PEAK 0xd6
29 #define LM25066_DEVICE_SETUP 0xd9
30 #define LM25066_READ_AVG_VIN 0xdc
31 #define LM25066_SAMPLES_FOR_AVG 0xdb
32 #define LM25066_READ_AVG_VOUT 0xdd
33 #define LM25066_READ_AVG_IIN 0xde
34 #define LM25066_READ_AVG_PIN 0xdf
36 #define LM25066_DEV_SETUP_CL BIT(4) /* Current limit */
38 #define LM25066_SAMPLES_FOR_AVG_MAX 4096
42 #define LM25056_VAUX_OV_WARN_LIMIT 0xe3
43 #define LM25056_VAUX_UV_WARN_LIMIT 0xe4
45 #define LM25056_MFR_STS_VAUX_OV_WARN BIT(1)
46 #define LM25056_MFR_STS_VAUX_UV_WARN BIT(0)
52 #define PSC_CURRENT_IN_L (PSC_NUM_CLASSES)
53 #define PSC_POWER_L (PSC_NUM_CLASSES + 1)
55 static const struct __coeff lm25066_coeff
[][PSC_NUM_CLASSES
+ 2] = {
67 [PSC_CURRENT_IN_L
] = {
104 [PSC_CURRENT_IN_L
] = {
119 [PSC_TEMPERATURE
] = {
129 [PSC_VOLTAGE_OUT
] = {
139 [PSC_CURRENT_IN_L
] = {
154 [PSC_TEMPERATURE
] = {
164 [PSC_VOLTAGE_OUT
] = {
174 [PSC_CURRENT_IN_L
] = {
189 [PSC_TEMPERATURE
] = {
199 [PSC_VOLTAGE_OUT
] = {
209 [PSC_CURRENT_IN_L
] = {
224 [PSC_TEMPERATURE
] = {
230 struct lm25066_data
{
232 u16 rlimit
; /* Maximum register value */
233 struct pmbus_driver_info info
;
236 #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
238 static int lm25066_read_word_data(struct i2c_client
*client
, int page
,
241 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
242 const struct lm25066_data
*data
= to_lm25066_data(info
);
246 case PMBUS_VIRT_READ_VMON
:
247 ret
= pmbus_read_word_data(client
, 0, 0xff, LM25066_READ_VAUX
);
250 /* Adjust returned value to match VIN coefficients */
253 /* VIN: 6.14 mV VAUX: 293 uV LSB */
254 ret
= DIV_ROUND_CLOSEST(ret
* 293, 6140);
257 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
258 ret
= DIV_ROUND_CLOSEST(ret
* 2832, 45400);
261 /* VIN: 4.53 mV VAUX: 700 uV LSB */
262 ret
= DIV_ROUND_CLOSEST(ret
* 70, 453);
266 /* VIN: 2.18 mV VAUX: 725 uV LSB */
267 ret
= DIV_ROUND_CLOSEST(ret
* 725, 2180);
272 ret
= pmbus_read_word_data(client
, 0, 0xff,
273 LM25066_MFR_READ_IIN
);
276 ret
= pmbus_read_word_data(client
, 0, 0xff,
277 LM25066_MFR_READ_PIN
);
279 case PMBUS_IIN_OC_WARN_LIMIT
:
280 ret
= pmbus_read_word_data(client
, 0, 0xff,
281 LM25066_MFR_IIN_OC_WARN_LIMIT
);
283 case PMBUS_PIN_OP_WARN_LIMIT
:
284 ret
= pmbus_read_word_data(client
, 0, 0xff,
285 LM25066_MFR_PIN_OP_WARN_LIMIT
);
287 case PMBUS_VIRT_READ_VIN_AVG
:
288 ret
= pmbus_read_word_data(client
, 0, 0xff,
289 LM25066_READ_AVG_VIN
);
291 case PMBUS_VIRT_READ_VOUT_AVG
:
292 ret
= pmbus_read_word_data(client
, 0, 0xff,
293 LM25066_READ_AVG_VOUT
);
295 case PMBUS_VIRT_READ_IIN_AVG
:
296 ret
= pmbus_read_word_data(client
, 0, 0xff,
297 LM25066_READ_AVG_IIN
);
299 case PMBUS_VIRT_READ_PIN_AVG
:
300 ret
= pmbus_read_word_data(client
, 0, 0xff,
301 LM25066_READ_AVG_PIN
);
303 case PMBUS_VIRT_READ_PIN_MAX
:
304 ret
= pmbus_read_word_data(client
, 0, 0xff,
305 LM25066_READ_PIN_PEAK
);
307 case PMBUS_VIRT_RESET_PIN_HISTORY
:
310 case PMBUS_VIRT_SAMPLES
:
311 ret
= pmbus_read_byte_data(client
, 0, LM25066_SAMPLES_FOR_AVG
);
323 static int lm25056_read_word_data(struct i2c_client
*client
, int page
,
329 case PMBUS_VIRT_VMON_UV_WARN_LIMIT
:
330 ret
= pmbus_read_word_data(client
, 0, 0xff,
331 LM25056_VAUX_UV_WARN_LIMIT
);
334 /* Adjust returned value to match VIN coefficients */
335 ret
= DIV_ROUND_CLOSEST(ret
* 293, 6140);
337 case PMBUS_VIRT_VMON_OV_WARN_LIMIT
:
338 ret
= pmbus_read_word_data(client
, 0, 0xff,
339 LM25056_VAUX_OV_WARN_LIMIT
);
342 /* Adjust returned value to match VIN coefficients */
343 ret
= DIV_ROUND_CLOSEST(ret
* 293, 6140);
346 ret
= lm25066_read_word_data(client
, page
, phase
, reg
);
352 static int lm25056_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
357 case PMBUS_VIRT_STATUS_VMON
:
358 ret
= pmbus_read_byte_data(client
, 0,
359 PMBUS_STATUS_MFR_SPECIFIC
);
363 if (ret
& LM25056_MFR_STS_VAUX_UV_WARN
)
364 s
|= PB_VOLTAGE_UV_WARNING
;
365 if (ret
& LM25056_MFR_STS_VAUX_OV_WARN
)
366 s
|= PB_VOLTAGE_OV_WARNING
;
376 static int lm25066_write_word_data(struct i2c_client
*client
, int page
, int reg
,
379 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
380 const struct lm25066_data
*data
= to_lm25066_data(info
);
384 case PMBUS_POUT_OP_FAULT_LIMIT
:
385 case PMBUS_POUT_OP_WARN_LIMIT
:
386 case PMBUS_VOUT_UV_WARN_LIMIT
:
387 case PMBUS_OT_FAULT_LIMIT
:
388 case PMBUS_OT_WARN_LIMIT
:
389 case PMBUS_IIN_OC_FAULT_LIMIT
:
390 case PMBUS_VIN_UV_WARN_LIMIT
:
391 case PMBUS_VIN_UV_FAULT_LIMIT
:
392 case PMBUS_VIN_OV_FAULT_LIMIT
:
393 case PMBUS_VIN_OV_WARN_LIMIT
:
394 word
= ((s16
)word
< 0) ? 0 : clamp_val(word
, 0, data
->rlimit
);
395 ret
= pmbus_write_word_data(client
, 0, reg
, word
);
397 case PMBUS_IIN_OC_WARN_LIMIT
:
398 word
= ((s16
)word
< 0) ? 0 : clamp_val(word
, 0, data
->rlimit
);
399 ret
= pmbus_write_word_data(client
, 0,
400 LM25066_MFR_IIN_OC_WARN_LIMIT
,
403 case PMBUS_PIN_OP_WARN_LIMIT
:
404 word
= ((s16
)word
< 0) ? 0 : clamp_val(word
, 0, data
->rlimit
);
405 ret
= pmbus_write_word_data(client
, 0,
406 LM25066_MFR_PIN_OP_WARN_LIMIT
,
409 case PMBUS_VIRT_VMON_UV_WARN_LIMIT
:
410 /* Adjust from VIN coefficients (for LM25056) */
411 word
= DIV_ROUND_CLOSEST((int)word
* 6140, 293);
412 word
= ((s16
)word
< 0) ? 0 : clamp_val(word
, 0, data
->rlimit
);
413 ret
= pmbus_write_word_data(client
, 0,
414 LM25056_VAUX_UV_WARN_LIMIT
, word
);
416 case PMBUS_VIRT_VMON_OV_WARN_LIMIT
:
417 /* Adjust from VIN coefficients (for LM25056) */
418 word
= DIV_ROUND_CLOSEST((int)word
* 6140, 293);
419 word
= ((s16
)word
< 0) ? 0 : clamp_val(word
, 0, data
->rlimit
);
420 ret
= pmbus_write_word_data(client
, 0,
421 LM25056_VAUX_OV_WARN_LIMIT
, word
);
423 case PMBUS_VIRT_RESET_PIN_HISTORY
:
424 ret
= pmbus_write_byte(client
, 0, LM25066_CLEAR_PIN_PEAK
);
426 case PMBUS_VIRT_SAMPLES
:
427 word
= clamp_val(word
, 1, LM25066_SAMPLES_FOR_AVG_MAX
);
428 ret
= pmbus_write_byte_data(client
, 0, LM25066_SAMPLES_FOR_AVG
,
438 #if IS_ENABLED(CONFIG_SENSORS_LM25066_REGULATOR)
439 static const struct regulator_desc lm25066_reg_desc
[] = {
440 PMBUS_REGULATOR_ONE("vout"),
444 static const struct i2c_device_id lm25066_id
[] = {
445 {"lm25056", lm25056
},
446 {"lm25066", lm25066
},
449 {"lm5066i", lm5066i
},
452 MODULE_DEVICE_TABLE(i2c
, lm25066_id
);
454 static const struct of_device_id __maybe_unused lm25066_of_match
[] = {
455 { .compatible
= "ti,lm25056", .data
= (void *)lm25056
, },
456 { .compatible
= "ti,lm25066", .data
= (void *)lm25066
, },
457 { .compatible
= "ti,lm5064", .data
= (void *)lm5064
, },
458 { .compatible
= "ti,lm5066", .data
= (void *)lm5066
, },
459 { .compatible
= "ti,lm5066i", .data
= (void *)lm5066i
, },
462 MODULE_DEVICE_TABLE(of
, lm25066_of_match
);
464 static int lm25066_probe(struct i2c_client
*client
)
468 struct lm25066_data
*data
;
469 struct pmbus_driver_info
*info
;
470 const struct __coeff
*coeff
;
472 if (!i2c_check_functionality(client
->adapter
,
473 I2C_FUNC_SMBUS_READ_BYTE_DATA
))
476 data
= devm_kzalloc(&client
->dev
, sizeof(struct lm25066_data
),
481 config
= i2c_smbus_read_byte_data(client
, LM25066_DEVICE_SETUP
);
485 data
->id
= (enum chips
)(unsigned long)i2c_get_match_data(client
);
490 info
->format
[PSC_VOLTAGE_IN
] = direct
;
491 info
->format
[PSC_VOLTAGE_OUT
] = direct
;
492 info
->format
[PSC_CURRENT_IN
] = direct
;
493 info
->format
[PSC_TEMPERATURE
] = direct
;
494 info
->format
[PSC_POWER
] = direct
;
496 info
->func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_VMON
497 | PMBUS_HAVE_PIN
| PMBUS_HAVE_IIN
| PMBUS_HAVE_STATUS_INPUT
498 | PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
| PMBUS_HAVE_SAMPLES
;
500 if (data
->id
== lm25056
) {
501 info
->func
[0] |= PMBUS_HAVE_STATUS_VMON
;
502 info
->read_word_data
= lm25056_read_word_data
;
503 info
->read_byte_data
= lm25056_read_byte_data
;
504 data
->rlimit
= 0x0fff;
506 info
->func
[0] |= PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
;
507 info
->read_word_data
= lm25066_read_word_data
;
508 data
->rlimit
= 0x0fff;
510 info
->write_word_data
= lm25066_write_word_data
;
512 coeff
= &lm25066_coeff
[data
->id
][0];
513 info
->m
[PSC_TEMPERATURE
] = coeff
[PSC_TEMPERATURE
].m
;
514 info
->b
[PSC_TEMPERATURE
] = coeff
[PSC_TEMPERATURE
].b
;
515 info
->R
[PSC_TEMPERATURE
] = coeff
[PSC_TEMPERATURE
].R
;
516 info
->m
[PSC_VOLTAGE_IN
] = coeff
[PSC_VOLTAGE_IN
].m
;
517 info
->b
[PSC_VOLTAGE_IN
] = coeff
[PSC_VOLTAGE_IN
].b
;
518 info
->R
[PSC_VOLTAGE_IN
] = coeff
[PSC_VOLTAGE_IN
].R
;
519 info
->m
[PSC_VOLTAGE_OUT
] = coeff
[PSC_VOLTAGE_OUT
].m
;
520 info
->b
[PSC_VOLTAGE_OUT
] = coeff
[PSC_VOLTAGE_OUT
].b
;
521 info
->R
[PSC_VOLTAGE_OUT
] = coeff
[PSC_VOLTAGE_OUT
].R
;
522 info
->R
[PSC_CURRENT_IN
] = coeff
[PSC_CURRENT_IN
].R
;
523 info
->R
[PSC_POWER
] = coeff
[PSC_POWER
].R
;
524 if (config
& LM25066_DEV_SETUP_CL
) {
525 info
->m
[PSC_CURRENT_IN
] = coeff
[PSC_CURRENT_IN_L
].m
;
526 info
->b
[PSC_CURRENT_IN
] = coeff
[PSC_CURRENT_IN_L
].b
;
527 info
->m
[PSC_POWER
] = coeff
[PSC_POWER_L
].m
;
528 info
->b
[PSC_POWER
] = coeff
[PSC_POWER_L
].b
;
530 info
->m
[PSC_CURRENT_IN
] = coeff
[PSC_CURRENT_IN
].m
;
531 info
->b
[PSC_CURRENT_IN
] = coeff
[PSC_CURRENT_IN
].b
;
532 info
->m
[PSC_POWER
] = coeff
[PSC_POWER
].m
;
533 info
->b
[PSC_POWER
] = coeff
[PSC_POWER
].b
;
537 * Values in the TI datasheets are normalized for a 1mOhm sense
538 * resistor; assume that unless DT specifies a value explicitly.
540 if (of_property_read_u32(client
->dev
.of_node
, "shunt-resistor-micro-ohms", &shunt
))
543 info
->m
[PSC_CURRENT_IN
] = info
->m
[PSC_CURRENT_IN
] * shunt
/ 1000;
544 info
->m
[PSC_POWER
] = info
->m
[PSC_POWER
] * shunt
/ 1000;
546 #if IS_ENABLED(CONFIG_SENSORS_LM25066_REGULATOR)
547 /* LM25056 doesn't support OPERATION */
548 if (data
->id
!= lm25056
) {
549 info
->num_regulators
= ARRAY_SIZE(lm25066_reg_desc
);
550 info
->reg_desc
= lm25066_reg_desc
;
554 return pmbus_do_probe(client
, info
);
557 /* This is the driver that will be inserted */
558 static struct i2c_driver lm25066_driver
= {
561 .of_match_table
= of_match_ptr(lm25066_of_match
),
563 .probe
= lm25066_probe
,
564 .id_table
= lm25066_id
,
567 module_i2c_driver(lm25066_driver
);
569 MODULE_AUTHOR("Guenter Roeck");
570 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
571 MODULE_LICENSE("GPL");
572 MODULE_IMPORT_NS(PMBUS
);