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 int lm25066_read_word_data(struct i2c_client *client, int page,
217 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
218 const struct lm25066_data *data = to_lm25066_data(info);
222 case PMBUS_VIRT_READ_VMON:
223 ret = pmbus_read_word_data(client, 0, 0xff, LM25066_READ_VAUX);
226 /* Adjust returned value to match VIN coefficients */
229 /* VIN: 6.14 mV VAUX: 293 uV LSB */
230 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
233 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
234 ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
237 /* VIN: 4.53 mV VAUX: 700 uV LSB */
238 ret = DIV_ROUND_CLOSEST(ret * 70, 453);
242 /* VIN: 2.18 mV VAUX: 725 uV LSB */
243 ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
248 ret = pmbus_read_word_data(client, 0, 0xff,
249 LM25066_MFR_READ_IIN);
252 ret = pmbus_read_word_data(client, 0, 0xff,
253 LM25066_MFR_READ_PIN);
255 case PMBUS_IIN_OC_WARN_LIMIT:
256 ret = pmbus_read_word_data(client, 0, 0xff,
257 LM25066_MFR_IIN_OC_WARN_LIMIT);
259 case PMBUS_PIN_OP_WARN_LIMIT:
260 ret = pmbus_read_word_data(client, 0, 0xff,
261 LM25066_MFR_PIN_OP_WARN_LIMIT);
263 case PMBUS_VIRT_READ_VIN_AVG:
264 ret = pmbus_read_word_data(client, 0, 0xff,
265 LM25066_READ_AVG_VIN);
267 case PMBUS_VIRT_READ_VOUT_AVG:
268 ret = pmbus_read_word_data(client, 0, 0xff,
269 LM25066_READ_AVG_VOUT);
271 case PMBUS_VIRT_READ_IIN_AVG:
272 ret = pmbus_read_word_data(client, 0, 0xff,
273 LM25066_READ_AVG_IIN);
275 case PMBUS_VIRT_READ_PIN_AVG:
276 ret = pmbus_read_word_data(client, 0, 0xff,
277 LM25066_READ_AVG_PIN);
279 case PMBUS_VIRT_READ_PIN_MAX:
280 ret = pmbus_read_word_data(client, 0, 0xff,
281 LM25066_READ_PIN_PEAK);
283 case PMBUS_VIRT_RESET_PIN_HISTORY:
286 case PMBUS_VIRT_SAMPLES:
287 ret = pmbus_read_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG);
299 static int lm25056_read_word_data(struct i2c_client *client, int page,
305 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
306 ret = pmbus_read_word_data(client, 0, 0xff,
307 LM25056_VAUX_UV_WARN_LIMIT);
310 /* Adjust returned value to match VIN coefficients */
311 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
313 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
314 ret = pmbus_read_word_data(client, 0, 0xff,
315 LM25056_VAUX_OV_WARN_LIMIT);
318 /* Adjust returned value to match VIN coefficients */
319 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
322 ret = lm25066_read_word_data(client, page, phase, reg);
328 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
333 case PMBUS_VIRT_STATUS_VMON:
334 ret = pmbus_read_byte_data(client, 0,
335 PMBUS_STATUS_MFR_SPECIFIC);
339 if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
340 s |= PB_VOLTAGE_UV_WARNING;
341 if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
342 s |= PB_VOLTAGE_OV_WARNING;
352 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
355 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
356 const struct lm25066_data *data = to_lm25066_data(info);
360 case PMBUS_POUT_OP_FAULT_LIMIT:
361 case PMBUS_POUT_OP_WARN_LIMIT:
362 case PMBUS_VOUT_UV_WARN_LIMIT:
363 case PMBUS_OT_FAULT_LIMIT:
364 case PMBUS_OT_WARN_LIMIT:
365 case PMBUS_IIN_OC_FAULT_LIMIT:
366 case PMBUS_VIN_UV_WARN_LIMIT:
367 case PMBUS_VIN_UV_FAULT_LIMIT:
368 case PMBUS_VIN_OV_FAULT_LIMIT:
369 case PMBUS_VIN_OV_WARN_LIMIT:
370 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
371 ret = pmbus_write_word_data(client, 0, reg, word);
372 pmbus_clear_cache(client);
374 case PMBUS_IIN_OC_WARN_LIMIT:
375 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
376 ret = pmbus_write_word_data(client, 0,
377 LM25066_MFR_IIN_OC_WARN_LIMIT,
379 pmbus_clear_cache(client);
381 case PMBUS_PIN_OP_WARN_LIMIT:
382 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
383 ret = pmbus_write_word_data(client, 0,
384 LM25066_MFR_PIN_OP_WARN_LIMIT,
386 pmbus_clear_cache(client);
388 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
389 /* Adjust from VIN coefficients (for LM25056) */
390 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
391 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
392 ret = pmbus_write_word_data(client, 0,
393 LM25056_VAUX_UV_WARN_LIMIT, word);
394 pmbus_clear_cache(client);
396 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
397 /* Adjust from VIN coefficients (for LM25056) */
398 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
399 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
400 ret = pmbus_write_word_data(client, 0,
401 LM25056_VAUX_OV_WARN_LIMIT, word);
402 pmbus_clear_cache(client);
404 case PMBUS_VIRT_RESET_PIN_HISTORY:
405 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
407 case PMBUS_VIRT_SAMPLES:
408 word = clamp_val(word, 1, LM25066_SAMPLES_FOR_AVG_MAX);
409 ret = pmbus_write_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG,
419 static int lm25066_probe(struct i2c_client *client,
420 const struct i2c_device_id *id)
423 struct lm25066_data *data;
424 struct pmbus_driver_info *info;
425 struct __coeff *coeff;
427 if (!i2c_check_functionality(client->adapter,
428 I2C_FUNC_SMBUS_READ_BYTE_DATA))
431 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
436 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
440 data->id = id->driver_data;
444 info->format[PSC_VOLTAGE_IN] = direct;
445 info->format[PSC_VOLTAGE_OUT] = direct;
446 info->format[PSC_CURRENT_IN] = direct;
447 info->format[PSC_TEMPERATURE] = direct;
448 info->format[PSC_POWER] = direct;
450 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
451 | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
452 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_SAMPLES;
454 if (data->id == lm25056) {
455 info->func[0] |= PMBUS_HAVE_STATUS_VMON;
456 info->read_word_data = lm25056_read_word_data;
457 info->read_byte_data = lm25056_read_byte_data;
458 data->rlimit = 0x0fff;
460 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
461 info->read_word_data = lm25066_read_word_data;
462 data->rlimit = 0x0fff;
464 info->write_word_data = lm25066_write_word_data;
466 coeff = &lm25066_coeff[data->id][0];
467 info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
468 info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
469 info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
470 info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
471 info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
472 info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
473 info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
474 info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
475 info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
476 info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
477 info->R[PSC_POWER] = coeff[PSC_POWER].R;
478 if (config & LM25066_DEV_SETUP_CL) {
479 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
480 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b;
481 info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
482 info->b[PSC_POWER] = coeff[PSC_POWER_L].b;
484 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
485 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
486 info->m[PSC_POWER] = coeff[PSC_POWER].m;
487 info->b[PSC_POWER] = coeff[PSC_POWER].b;
490 return pmbus_do_probe(client, id, info);
493 static const struct i2c_device_id lm25066_id[] = {
494 {"lm25056", lm25056},
495 {"lm25066", lm25066},
498 {"lm5066i", lm5066i},
502 MODULE_DEVICE_TABLE(i2c, lm25066_id);
504 /* This is the driver that will be inserted */
505 static struct i2c_driver lm25066_driver = {
509 .probe = lm25066_probe,
510 .remove = pmbus_do_remove,
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");