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, int reg)
216 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
217 const struct lm25066_data *data = to_lm25066_data(info);
221 case PMBUS_VIRT_READ_VMON:
222 ret = pmbus_read_word_data(client, 0, LM25066_READ_VAUX);
225 /* Adjust returned value to match VIN coefficients */
228 /* VIN: 6.14 mV VAUX: 293 uV LSB */
229 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
232 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
233 ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
236 /* VIN: 4.53 mV VAUX: 700 uV LSB */
237 ret = DIV_ROUND_CLOSEST(ret * 70, 453);
241 /* VIN: 2.18 mV VAUX: 725 uV LSB */
242 ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
247 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
250 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
252 case PMBUS_IIN_OC_WARN_LIMIT:
253 ret = pmbus_read_word_data(client, 0,
254 LM25066_MFR_IIN_OC_WARN_LIMIT);
256 case PMBUS_PIN_OP_WARN_LIMIT:
257 ret = pmbus_read_word_data(client, 0,
258 LM25066_MFR_PIN_OP_WARN_LIMIT);
260 case PMBUS_VIRT_READ_VIN_AVG:
261 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
263 case PMBUS_VIRT_READ_VOUT_AVG:
264 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
266 case PMBUS_VIRT_READ_IIN_AVG:
267 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
269 case PMBUS_VIRT_READ_PIN_AVG:
270 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
272 case PMBUS_VIRT_READ_PIN_MAX:
273 ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
275 case PMBUS_VIRT_RESET_PIN_HISTORY:
278 case PMBUS_VIRT_SAMPLES:
279 ret = pmbus_read_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG);
291 static int lm25056_read_word_data(struct i2c_client *client, int page, int reg)
296 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
297 ret = pmbus_read_word_data(client, 0,
298 LM25056_VAUX_UV_WARN_LIMIT);
301 /* Adjust returned value to match VIN coefficients */
302 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
304 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
305 ret = pmbus_read_word_data(client, 0,
306 LM25056_VAUX_OV_WARN_LIMIT);
309 /* Adjust returned value to match VIN coefficients */
310 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
313 ret = lm25066_read_word_data(client, page, reg);
319 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
324 case PMBUS_VIRT_STATUS_VMON:
325 ret = pmbus_read_byte_data(client, 0,
326 PMBUS_STATUS_MFR_SPECIFIC);
330 if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
331 s |= PB_VOLTAGE_UV_WARNING;
332 if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
333 s |= PB_VOLTAGE_OV_WARNING;
343 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
346 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
347 const struct lm25066_data *data = to_lm25066_data(info);
351 case PMBUS_POUT_OP_FAULT_LIMIT:
352 case PMBUS_POUT_OP_WARN_LIMIT:
353 case PMBUS_VOUT_UV_WARN_LIMIT:
354 case PMBUS_OT_FAULT_LIMIT:
355 case PMBUS_OT_WARN_LIMIT:
356 case PMBUS_IIN_OC_FAULT_LIMIT:
357 case PMBUS_VIN_UV_WARN_LIMIT:
358 case PMBUS_VIN_UV_FAULT_LIMIT:
359 case PMBUS_VIN_OV_FAULT_LIMIT:
360 case PMBUS_VIN_OV_WARN_LIMIT:
361 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
362 ret = pmbus_write_word_data(client, 0, reg, word);
363 pmbus_clear_cache(client);
365 case PMBUS_IIN_OC_WARN_LIMIT:
366 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
367 ret = pmbus_write_word_data(client, 0,
368 LM25066_MFR_IIN_OC_WARN_LIMIT,
370 pmbus_clear_cache(client);
372 case PMBUS_PIN_OP_WARN_LIMIT:
373 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
374 ret = pmbus_write_word_data(client, 0,
375 LM25066_MFR_PIN_OP_WARN_LIMIT,
377 pmbus_clear_cache(client);
379 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
380 /* Adjust from VIN coefficients (for LM25056) */
381 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
382 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
383 ret = pmbus_write_word_data(client, 0,
384 LM25056_VAUX_UV_WARN_LIMIT, word);
385 pmbus_clear_cache(client);
387 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
388 /* Adjust from VIN coefficients (for LM25056) */
389 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
390 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
391 ret = pmbus_write_word_data(client, 0,
392 LM25056_VAUX_OV_WARN_LIMIT, word);
393 pmbus_clear_cache(client);
395 case PMBUS_VIRT_RESET_PIN_HISTORY:
396 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
398 case PMBUS_VIRT_SAMPLES:
399 word = clamp_val(word, 1, LM25066_SAMPLES_FOR_AVG_MAX);
400 ret = pmbus_write_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG,
410 static int lm25066_probe(struct i2c_client *client,
411 const struct i2c_device_id *id)
414 struct lm25066_data *data;
415 struct pmbus_driver_info *info;
416 struct __coeff *coeff;
418 if (!i2c_check_functionality(client->adapter,
419 I2C_FUNC_SMBUS_READ_BYTE_DATA))
422 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
427 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
431 data->id = id->driver_data;
435 info->format[PSC_VOLTAGE_IN] = direct;
436 info->format[PSC_VOLTAGE_OUT] = direct;
437 info->format[PSC_CURRENT_IN] = direct;
438 info->format[PSC_TEMPERATURE] = direct;
439 info->format[PSC_POWER] = direct;
441 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
442 | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
443 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_SAMPLES;
445 if (data->id == lm25056) {
446 info->func[0] |= PMBUS_HAVE_STATUS_VMON;
447 info->read_word_data = lm25056_read_word_data;
448 info->read_byte_data = lm25056_read_byte_data;
449 data->rlimit = 0x0fff;
451 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
452 info->read_word_data = lm25066_read_word_data;
453 data->rlimit = 0x0fff;
455 info->write_word_data = lm25066_write_word_data;
457 coeff = &lm25066_coeff[data->id][0];
458 info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
459 info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
460 info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
461 info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
462 info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
463 info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
464 info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
465 info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
466 info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
467 info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
468 info->R[PSC_POWER] = coeff[PSC_POWER].R;
469 if (config & LM25066_DEV_SETUP_CL) {
470 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
471 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b;
472 info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
473 info->b[PSC_POWER] = coeff[PSC_POWER_L].b;
475 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
476 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
477 info->m[PSC_POWER] = coeff[PSC_POWER].m;
478 info->b[PSC_POWER] = coeff[PSC_POWER].b;
481 return pmbus_do_probe(client, id, info);
484 static const struct i2c_device_id lm25066_id[] = {
485 {"lm25056", lm25056},
486 {"lm25066", lm25066},
489 {"lm5066i", lm5066i},
493 MODULE_DEVICE_TABLE(i2c, lm25066_id);
495 /* This is the driver that will be inserted */
496 static struct i2c_driver lm25066_driver = {
500 .probe = lm25066_probe,
501 .remove = pmbus_do_remove,
502 .id_table = lm25066_id,
505 module_i2c_driver(lm25066_driver);
507 MODULE_AUTHOR("Guenter Roeck");
508 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
509 MODULE_LICENSE("GPL");