2 * Hardware monitoring driver for LM25056 / 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>
29 #include <linux/log2.h>
32 enum chips { lm25056, lm25066, lm5064, lm5066, lm5066i };
34 #define LM25066_READ_VAUX 0xd0
35 #define LM25066_MFR_READ_IIN 0xd1
36 #define LM25066_MFR_READ_PIN 0xd2
37 #define LM25066_MFR_IIN_OC_WARN_LIMIT 0xd3
38 #define LM25066_MFR_PIN_OP_WARN_LIMIT 0xd4
39 #define LM25066_READ_PIN_PEAK 0xd5
40 #define LM25066_CLEAR_PIN_PEAK 0xd6
41 #define LM25066_DEVICE_SETUP 0xd9
42 #define LM25066_READ_AVG_VIN 0xdc
43 #define LM25066_SAMPLES_FOR_AVG 0xdb
44 #define LM25066_READ_AVG_VOUT 0xdd
45 #define LM25066_READ_AVG_IIN 0xde
46 #define LM25066_READ_AVG_PIN 0xdf
48 #define LM25066_DEV_SETUP_CL BIT(4) /* Current limit */
50 #define LM25066_SAMPLES_FOR_AVG_MAX 4096
54 #define LM25056_VAUX_OV_WARN_LIMIT 0xe3
55 #define LM25056_VAUX_UV_WARN_LIMIT 0xe4
57 #define LM25056_MFR_STS_VAUX_OV_WARN BIT(1)
58 #define LM25056_MFR_STS_VAUX_UV_WARN BIT(0)
64 #define PSC_CURRENT_IN_L (PSC_NUM_CLASSES)
65 #define PSC_POWER_L (PSC_NUM_CLASSES + 1)
67 static struct __coeff lm25066_coeff[6][PSC_NUM_CLASSES + 2] = {
77 [PSC_CURRENT_IN_L] = {
100 [PSC_VOLTAGE_OUT] = {
108 [PSC_CURRENT_IN_L] = {
120 [PSC_TEMPERATURE] = {
129 [PSC_VOLTAGE_OUT] = {
137 [PSC_CURRENT_IN_L] = {
149 [PSC_TEMPERATURE] = {
158 [PSC_VOLTAGE_OUT] = {
166 [PSC_CURRENT_IN_L] = {
178 [PSC_TEMPERATURE] = {
188 [PSC_VOLTAGE_OUT] = {
198 [PSC_CURRENT_IN_L] = {
213 [PSC_TEMPERATURE] = {
219 struct lm25066_data {
221 u16 rlimit; /* Maximum register value */
222 struct pmbus_driver_info info;
225 #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
227 static int lm25066_read_word_data(struct i2c_client *client, int page, int reg)
229 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
230 const struct lm25066_data *data = to_lm25066_data(info);
234 case PMBUS_VIRT_READ_VMON:
235 ret = pmbus_read_word_data(client, 0, LM25066_READ_VAUX);
238 /* Adjust returned value to match VIN coefficients */
241 /* VIN: 6.14 mV VAUX: 293 uV LSB */
242 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
245 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
246 ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
249 /* VIN: 4.53 mV VAUX: 700 uV LSB */
250 ret = DIV_ROUND_CLOSEST(ret * 70, 453);
254 /* VIN: 2.18 mV VAUX: 725 uV LSB */
255 ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
260 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
263 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
265 case PMBUS_IIN_OC_WARN_LIMIT:
266 ret = pmbus_read_word_data(client, 0,
267 LM25066_MFR_IIN_OC_WARN_LIMIT);
269 case PMBUS_PIN_OP_WARN_LIMIT:
270 ret = pmbus_read_word_data(client, 0,
271 LM25066_MFR_PIN_OP_WARN_LIMIT);
273 case PMBUS_VIRT_READ_VIN_AVG:
274 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
276 case PMBUS_VIRT_READ_VOUT_AVG:
277 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
279 case PMBUS_VIRT_READ_IIN_AVG:
280 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
282 case PMBUS_VIRT_READ_PIN_AVG:
283 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
285 case PMBUS_VIRT_READ_PIN_MAX:
286 ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
288 case PMBUS_VIRT_RESET_PIN_HISTORY:
291 case PMBUS_VIRT_SAMPLES:
292 ret = pmbus_read_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG);
304 static int lm25056_read_word_data(struct i2c_client *client, int page, int reg)
309 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
310 ret = pmbus_read_word_data(client, 0,
311 LM25056_VAUX_UV_WARN_LIMIT);
314 /* Adjust returned value to match VIN coefficients */
315 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
317 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
318 ret = pmbus_read_word_data(client, 0,
319 LM25056_VAUX_OV_WARN_LIMIT);
322 /* Adjust returned value to match VIN coefficients */
323 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
326 ret = lm25066_read_word_data(client, page, reg);
332 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
337 case PMBUS_VIRT_STATUS_VMON:
338 ret = pmbus_read_byte_data(client, 0,
339 PMBUS_STATUS_MFR_SPECIFIC);
343 if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
344 s |= PB_VOLTAGE_UV_WARNING;
345 if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
346 s |= PB_VOLTAGE_OV_WARNING;
356 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
359 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
360 const struct lm25066_data *data = to_lm25066_data(info);
364 case PMBUS_POUT_OP_FAULT_LIMIT:
365 case PMBUS_POUT_OP_WARN_LIMIT:
366 case PMBUS_VOUT_UV_WARN_LIMIT:
367 case PMBUS_OT_FAULT_LIMIT:
368 case PMBUS_OT_WARN_LIMIT:
369 case PMBUS_IIN_OC_FAULT_LIMIT:
370 case PMBUS_VIN_UV_WARN_LIMIT:
371 case PMBUS_VIN_UV_FAULT_LIMIT:
372 case PMBUS_VIN_OV_FAULT_LIMIT:
373 case PMBUS_VIN_OV_WARN_LIMIT:
374 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
375 ret = pmbus_write_word_data(client, 0, reg, word);
376 pmbus_clear_cache(client);
378 case PMBUS_IIN_OC_WARN_LIMIT:
379 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
380 ret = pmbus_write_word_data(client, 0,
381 LM25066_MFR_IIN_OC_WARN_LIMIT,
383 pmbus_clear_cache(client);
385 case PMBUS_PIN_OP_WARN_LIMIT:
386 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
387 ret = pmbus_write_word_data(client, 0,
388 LM25066_MFR_PIN_OP_WARN_LIMIT,
390 pmbus_clear_cache(client);
392 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
393 /* Adjust from VIN coefficients (for LM25056) */
394 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
395 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
396 ret = pmbus_write_word_data(client, 0,
397 LM25056_VAUX_UV_WARN_LIMIT, word);
398 pmbus_clear_cache(client);
400 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
401 /* Adjust from VIN coefficients (for LM25056) */
402 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
403 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
404 ret = pmbus_write_word_data(client, 0,
405 LM25056_VAUX_OV_WARN_LIMIT, word);
406 pmbus_clear_cache(client);
408 case PMBUS_VIRT_RESET_PIN_HISTORY:
409 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
411 case PMBUS_VIRT_SAMPLES:
412 word = clamp_val(word, 1, LM25066_SAMPLES_FOR_AVG_MAX);
413 ret = pmbus_write_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG,
423 static int lm25066_probe(struct i2c_client *client,
424 const struct i2c_device_id *id)
427 struct lm25066_data *data;
428 struct pmbus_driver_info *info;
429 struct __coeff *coeff;
431 if (!i2c_check_functionality(client->adapter,
432 I2C_FUNC_SMBUS_READ_BYTE_DATA))
435 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
440 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
444 data->id = id->driver_data;
448 info->format[PSC_VOLTAGE_IN] = direct;
449 info->format[PSC_VOLTAGE_OUT] = direct;
450 info->format[PSC_CURRENT_IN] = direct;
451 info->format[PSC_TEMPERATURE] = direct;
452 info->format[PSC_POWER] = direct;
454 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
455 | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
456 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_SAMPLES;
458 if (data->id == lm25056) {
459 info->func[0] |= PMBUS_HAVE_STATUS_VMON;
460 info->read_word_data = lm25056_read_word_data;
461 info->read_byte_data = lm25056_read_byte_data;
462 data->rlimit = 0x0fff;
464 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
465 info->read_word_data = lm25066_read_word_data;
466 data->rlimit = 0x0fff;
468 info->write_word_data = lm25066_write_word_data;
470 coeff = &lm25066_coeff[data->id][0];
471 info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
472 info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
473 info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
474 info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
475 info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
476 info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
477 info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
478 info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
479 info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
480 info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
481 info->R[PSC_POWER] = coeff[PSC_POWER].R;
482 if (config & LM25066_DEV_SETUP_CL) {
483 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
484 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b;
485 info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
486 info->b[PSC_POWER] = coeff[PSC_POWER_L].b;
488 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
489 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
490 info->m[PSC_POWER] = coeff[PSC_POWER].m;
491 info->b[PSC_POWER] = coeff[PSC_POWER].b;
494 return pmbus_do_probe(client, id, info);
497 static const struct i2c_device_id lm25066_id[] = {
498 {"lm25056", lm25056},
499 {"lm25066", lm25066},
502 {"lm5066i", lm5066i},
506 MODULE_DEVICE_TABLE(i2c, lm25066_id);
508 /* This is the driver that will be inserted */
509 static struct i2c_driver lm25066_driver = {
513 .probe = lm25066_probe,
514 .remove = pmbus_do_remove,
515 .id_table = lm25066_id,
518 module_i2c_driver(lm25066_driver);
520 MODULE_AUTHOR("Guenter Roeck");
521 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
522 MODULE_LICENSE("GPL");