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[6][PSC_NUM_CLASSES + 2] = {
64 [PSC_CURRENT_IN_L] = {
95 [PSC_CURRENT_IN_L] = {
107 [PSC_TEMPERATURE] = {
116 [PSC_VOLTAGE_OUT] = {
124 [PSC_CURRENT_IN_L] = {
136 [PSC_TEMPERATURE] = {
145 [PSC_VOLTAGE_OUT] = {
153 [PSC_CURRENT_IN_L] = {
165 [PSC_TEMPERATURE] = {
175 [PSC_VOLTAGE_OUT] = {
185 [PSC_CURRENT_IN_L] = {
200 [PSC_TEMPERATURE] = {
206 struct lm25066_data {
208 u16 rlimit; /* Maximum register value */
209 struct pmbus_driver_info info;
212 #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
214 static const struct i2c_device_id lm25066_id[];
216 static int lm25066_read_word_data(struct i2c_client *client, int page,
219 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
220 const struct lm25066_data *data = to_lm25066_data(info);
224 case PMBUS_VIRT_READ_VMON:
225 ret = pmbus_read_word_data(client, 0, 0xff, LM25066_READ_VAUX);
228 /* Adjust returned value to match VIN coefficients */
231 /* VIN: 6.14 mV VAUX: 293 uV LSB */
232 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
235 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
236 ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
239 /* VIN: 4.53 mV VAUX: 700 uV LSB */
240 ret = DIV_ROUND_CLOSEST(ret * 70, 453);
244 /* VIN: 2.18 mV VAUX: 725 uV LSB */
245 ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
250 ret = pmbus_read_word_data(client, 0, 0xff,
251 LM25066_MFR_READ_IIN);
254 ret = pmbus_read_word_data(client, 0, 0xff,
255 LM25066_MFR_READ_PIN);
257 case PMBUS_IIN_OC_WARN_LIMIT:
258 ret = pmbus_read_word_data(client, 0, 0xff,
259 LM25066_MFR_IIN_OC_WARN_LIMIT);
261 case PMBUS_PIN_OP_WARN_LIMIT:
262 ret = pmbus_read_word_data(client, 0, 0xff,
263 LM25066_MFR_PIN_OP_WARN_LIMIT);
265 case PMBUS_VIRT_READ_VIN_AVG:
266 ret = pmbus_read_word_data(client, 0, 0xff,
267 LM25066_READ_AVG_VIN);
269 case PMBUS_VIRT_READ_VOUT_AVG:
270 ret = pmbus_read_word_data(client, 0, 0xff,
271 LM25066_READ_AVG_VOUT);
273 case PMBUS_VIRT_READ_IIN_AVG:
274 ret = pmbus_read_word_data(client, 0, 0xff,
275 LM25066_READ_AVG_IIN);
277 case PMBUS_VIRT_READ_PIN_AVG:
278 ret = pmbus_read_word_data(client, 0, 0xff,
279 LM25066_READ_AVG_PIN);
281 case PMBUS_VIRT_READ_PIN_MAX:
282 ret = pmbus_read_word_data(client, 0, 0xff,
283 LM25066_READ_PIN_PEAK);
285 case PMBUS_VIRT_RESET_PIN_HISTORY:
288 case PMBUS_VIRT_SAMPLES:
289 ret = pmbus_read_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG);
301 static int lm25056_read_word_data(struct i2c_client *client, int page,
307 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
308 ret = pmbus_read_word_data(client, 0, 0xff,
309 LM25056_VAUX_UV_WARN_LIMIT);
312 /* Adjust returned value to match VIN coefficients */
313 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
315 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
316 ret = pmbus_read_word_data(client, 0, 0xff,
317 LM25056_VAUX_OV_WARN_LIMIT);
320 /* Adjust returned value to match VIN coefficients */
321 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
324 ret = lm25066_read_word_data(client, page, phase, reg);
330 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
335 case PMBUS_VIRT_STATUS_VMON:
336 ret = pmbus_read_byte_data(client, 0,
337 PMBUS_STATUS_MFR_SPECIFIC);
341 if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
342 s |= PB_VOLTAGE_UV_WARNING;
343 if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
344 s |= PB_VOLTAGE_OV_WARNING;
354 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
357 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
358 const struct lm25066_data *data = to_lm25066_data(info);
362 case PMBUS_POUT_OP_FAULT_LIMIT:
363 case PMBUS_POUT_OP_WARN_LIMIT:
364 case PMBUS_VOUT_UV_WARN_LIMIT:
365 case PMBUS_OT_FAULT_LIMIT:
366 case PMBUS_OT_WARN_LIMIT:
367 case PMBUS_IIN_OC_FAULT_LIMIT:
368 case PMBUS_VIN_UV_WARN_LIMIT:
369 case PMBUS_VIN_UV_FAULT_LIMIT:
370 case PMBUS_VIN_OV_FAULT_LIMIT:
371 case PMBUS_VIN_OV_WARN_LIMIT:
372 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
373 ret = pmbus_write_word_data(client, 0, reg, word);
374 pmbus_clear_cache(client);
376 case PMBUS_IIN_OC_WARN_LIMIT:
377 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
378 ret = pmbus_write_word_data(client, 0,
379 LM25066_MFR_IIN_OC_WARN_LIMIT,
381 pmbus_clear_cache(client);
383 case PMBUS_PIN_OP_WARN_LIMIT:
384 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
385 ret = pmbus_write_word_data(client, 0,
386 LM25066_MFR_PIN_OP_WARN_LIMIT,
388 pmbus_clear_cache(client);
390 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
391 /* Adjust from VIN coefficients (for LM25056) */
392 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
393 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
394 ret = pmbus_write_word_data(client, 0,
395 LM25056_VAUX_UV_WARN_LIMIT, word);
396 pmbus_clear_cache(client);
398 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
399 /* Adjust from VIN coefficients (for LM25056) */
400 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
401 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
402 ret = pmbus_write_word_data(client, 0,
403 LM25056_VAUX_OV_WARN_LIMIT, word);
404 pmbus_clear_cache(client);
406 case PMBUS_VIRT_RESET_PIN_HISTORY:
407 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
409 case PMBUS_VIRT_SAMPLES:
410 word = clamp_val(word, 1, LM25066_SAMPLES_FOR_AVG_MAX);
411 ret = pmbus_write_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG,
421 static int lm25066_probe(struct i2c_client *client)
424 struct lm25066_data *data;
425 struct pmbus_driver_info *info;
426 struct __coeff *coeff;
428 if (!i2c_check_functionality(client->adapter,
429 I2C_FUNC_SMBUS_READ_BYTE_DATA))
432 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
437 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
441 data->id = i2c_match_id(lm25066_id, client)->driver_data;
445 info->format[PSC_VOLTAGE_IN] = direct;
446 info->format[PSC_VOLTAGE_OUT] = direct;
447 info->format[PSC_CURRENT_IN] = direct;
448 info->format[PSC_TEMPERATURE] = direct;
449 info->format[PSC_POWER] = direct;
451 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
452 | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
453 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_SAMPLES;
455 if (data->id == lm25056) {
456 info->func[0] |= PMBUS_HAVE_STATUS_VMON;
457 info->read_word_data = lm25056_read_word_data;
458 info->read_byte_data = lm25056_read_byte_data;
459 data->rlimit = 0x0fff;
461 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
462 info->read_word_data = lm25066_read_word_data;
463 data->rlimit = 0x0fff;
465 info->write_word_data = lm25066_write_word_data;
467 coeff = &lm25066_coeff[data->id][0];
468 info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
469 info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
470 info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
471 info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
472 info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
473 info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
474 info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
475 info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
476 info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
477 info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
478 info->R[PSC_POWER] = coeff[PSC_POWER].R;
479 if (config & LM25066_DEV_SETUP_CL) {
480 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
481 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b;
482 info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
483 info->b[PSC_POWER] = coeff[PSC_POWER_L].b;
485 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
486 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
487 info->m[PSC_POWER] = coeff[PSC_POWER].m;
488 info->b[PSC_POWER] = coeff[PSC_POWER].b;
491 return pmbus_do_probe(client, info);
494 static const struct i2c_device_id lm25066_id[] = {
495 {"lm25056", lm25056},
496 {"lm25066", lm25066},
499 {"lm5066i", lm5066i},
503 MODULE_DEVICE_TABLE(i2c, lm25066_id);
505 /* This is the driver that will be inserted */
506 static struct i2c_driver lm25066_driver = {
510 .probe_new = lm25066_probe,
511 .id_table = lm25066_id,
514 module_i2c_driver(lm25066_driver);
516 MODULE_AUTHOR("Guenter Roeck");
517 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
518 MODULE_LICENSE("GPL");