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>
31 enum chips { lm25056, lm25066, lm5064, lm5066, lm5066i };
33 #define LM25066_READ_VAUX 0xd0
34 #define LM25066_MFR_READ_IIN 0xd1
35 #define LM25066_MFR_READ_PIN 0xd2
36 #define LM25066_MFR_IIN_OC_WARN_LIMIT 0xd3
37 #define LM25066_MFR_PIN_OP_WARN_LIMIT 0xd4
38 #define LM25066_READ_PIN_PEAK 0xd5
39 #define LM25066_CLEAR_PIN_PEAK 0xd6
40 #define LM25066_DEVICE_SETUP 0xd9
41 #define LM25066_READ_AVG_VIN 0xdc
42 #define LM25066_READ_AVG_VOUT 0xdd
43 #define LM25066_READ_AVG_IIN 0xde
44 #define LM25066_READ_AVG_PIN 0xdf
46 #define LM25066_DEV_SETUP_CL BIT(4) /* Current limit */
50 #define LM25056_VAUX_OV_WARN_LIMIT 0xe3
51 #define LM25056_VAUX_UV_WARN_LIMIT 0xe4
53 #define LM25056_MFR_STS_VAUX_OV_WARN BIT(1)
54 #define LM25056_MFR_STS_VAUX_UV_WARN BIT(0)
60 #define PSC_CURRENT_IN_L (PSC_NUM_CLASSES)
61 #define PSC_POWER_L (PSC_NUM_CLASSES + 1)
63 static struct __coeff lm25066_coeff[][PSC_NUM_CLASSES + 2] = {
75 [PSC_CURRENT_IN_L] = {
102 [PSC_VOLTAGE_OUT] = {
112 [PSC_CURRENT_IN_L] = {
127 [PSC_TEMPERATURE] = {
137 [PSC_VOLTAGE_OUT] = {
147 [PSC_CURRENT_IN_L] = {
162 [PSC_TEMPERATURE] = {
172 [PSC_VOLTAGE_OUT] = {
182 [PSC_CURRENT_IN_L] = {
197 [PSC_TEMPERATURE] = {
207 [PSC_VOLTAGE_OUT] = {
217 [PSC_CURRENT_IN_L] = {
232 [PSC_TEMPERATURE] = {
238 struct lm25066_data {
240 u16 rlimit; /* Maximum register value */
241 struct pmbus_driver_info info;
244 #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
246 static int lm25066_read_word_data(struct i2c_client *client, int page, int reg)
248 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
249 const struct lm25066_data *data = to_lm25066_data(info);
253 case PMBUS_VIRT_READ_VMON:
254 ret = pmbus_read_word_data(client, 0, LM25066_READ_VAUX);
257 /* Adjust returned value to match VIN coefficients */
260 /* VIN: 6.14 mV VAUX: 293 uV LSB */
261 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
264 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
265 ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
268 /* VIN: 4.53 mV VAUX: 700 uV LSB */
269 ret = DIV_ROUND_CLOSEST(ret * 70, 453);
273 /* VIN: 2.18 mV VAUX: 725 uV LSB */
274 ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
279 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
282 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
284 case PMBUS_IIN_OC_WARN_LIMIT:
285 ret = pmbus_read_word_data(client, 0,
286 LM25066_MFR_IIN_OC_WARN_LIMIT);
288 case PMBUS_PIN_OP_WARN_LIMIT:
289 ret = pmbus_read_word_data(client, 0,
290 LM25066_MFR_PIN_OP_WARN_LIMIT);
292 case PMBUS_VIRT_READ_VIN_AVG:
293 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
295 case PMBUS_VIRT_READ_VOUT_AVG:
296 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
298 case PMBUS_VIRT_READ_IIN_AVG:
299 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
301 case PMBUS_VIRT_READ_PIN_AVG:
302 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
304 case PMBUS_VIRT_READ_PIN_MAX:
305 ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
307 case PMBUS_VIRT_RESET_PIN_HISTORY:
317 static int lm25056_read_word_data(struct i2c_client *client, int page, int reg)
322 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
323 ret = pmbus_read_word_data(client, 0,
324 LM25056_VAUX_UV_WARN_LIMIT);
327 /* Adjust returned value to match VIN coefficients */
328 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
330 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
331 ret = pmbus_read_word_data(client, 0,
332 LM25056_VAUX_OV_WARN_LIMIT);
335 /* Adjust returned value to match VIN coefficients */
336 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
339 ret = lm25066_read_word_data(client, page, reg);
345 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
350 case PMBUS_VIRT_STATUS_VMON:
351 ret = pmbus_read_byte_data(client, 0,
352 PMBUS_STATUS_MFR_SPECIFIC);
356 if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
357 s |= PB_VOLTAGE_UV_WARNING;
358 if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
359 s |= PB_VOLTAGE_OV_WARNING;
369 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
372 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
373 const struct lm25066_data *data = to_lm25066_data(info);
377 case PMBUS_POUT_OP_FAULT_LIMIT:
378 case PMBUS_POUT_OP_WARN_LIMIT:
379 case PMBUS_VOUT_UV_WARN_LIMIT:
380 case PMBUS_OT_FAULT_LIMIT:
381 case PMBUS_OT_WARN_LIMIT:
382 case PMBUS_IIN_OC_FAULT_LIMIT:
383 case PMBUS_VIN_UV_WARN_LIMIT:
384 case PMBUS_VIN_UV_FAULT_LIMIT:
385 case PMBUS_VIN_OV_FAULT_LIMIT:
386 case PMBUS_VIN_OV_WARN_LIMIT:
387 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
388 ret = pmbus_write_word_data(client, 0, reg, word);
389 pmbus_clear_cache(client);
391 case PMBUS_IIN_OC_WARN_LIMIT:
392 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
393 ret = pmbus_write_word_data(client, 0,
394 LM25066_MFR_IIN_OC_WARN_LIMIT,
396 pmbus_clear_cache(client);
398 case PMBUS_PIN_OP_WARN_LIMIT:
399 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
400 ret = pmbus_write_word_data(client, 0,
401 LM25066_MFR_PIN_OP_WARN_LIMIT,
403 pmbus_clear_cache(client);
405 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
406 /* Adjust from VIN coefficients (for LM25056) */
407 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
408 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
409 ret = pmbus_write_word_data(client, 0,
410 LM25056_VAUX_UV_WARN_LIMIT, word);
411 pmbus_clear_cache(client);
413 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
414 /* Adjust from VIN coefficients (for LM25056) */
415 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
416 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
417 ret = pmbus_write_word_data(client, 0,
418 LM25056_VAUX_OV_WARN_LIMIT, word);
419 pmbus_clear_cache(client);
421 case PMBUS_VIRT_RESET_PIN_HISTORY:
422 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
431 static int lm25066_probe(struct i2c_client *client,
432 const struct i2c_device_id *id)
435 struct lm25066_data *data;
436 struct pmbus_driver_info *info;
437 struct __coeff *coeff;
439 if (!i2c_check_functionality(client->adapter,
440 I2C_FUNC_SMBUS_READ_BYTE_DATA))
443 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
448 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
452 data->id = id->driver_data;
456 info->format[PSC_VOLTAGE_IN] = direct;
457 info->format[PSC_VOLTAGE_OUT] = direct;
458 info->format[PSC_CURRENT_IN] = direct;
459 info->format[PSC_TEMPERATURE] = direct;
460 info->format[PSC_POWER] = direct;
462 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
463 | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
464 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
466 if (data->id == lm25056) {
467 info->func[0] |= PMBUS_HAVE_STATUS_VMON;
468 info->read_word_data = lm25056_read_word_data;
469 info->read_byte_data = lm25056_read_byte_data;
470 data->rlimit = 0x0fff;
472 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
473 info->read_word_data = lm25066_read_word_data;
474 data->rlimit = 0x0fff;
476 info->write_word_data = lm25066_write_word_data;
478 coeff = &lm25066_coeff[data->id][0];
479 info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
480 info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
481 info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
482 info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
483 info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
484 info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
485 info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
486 info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
487 info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
488 info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
489 info->R[PSC_POWER] = coeff[PSC_POWER].R;
490 if (config & LM25066_DEV_SETUP_CL) {
491 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
492 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b;
493 info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
494 info->b[PSC_POWER] = coeff[PSC_POWER_L].b;
496 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
497 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
498 info->m[PSC_POWER] = coeff[PSC_POWER].m;
499 info->b[PSC_POWER] = coeff[PSC_POWER].b;
502 return pmbus_do_probe(client, id, info);
505 static const struct i2c_device_id lm25066_id[] = {
506 {"lm25056", lm25056},
507 {"lm25066", lm25066},
510 {"lm5066i", lm5066i},
514 MODULE_DEVICE_TABLE(i2c, lm25066_id);
516 /* This is the driver that will be inserted */
517 static struct i2c_driver lm25066_driver = {
521 .probe = lm25066_probe,
522 .remove = pmbus_do_remove,
523 .id_table = lm25066_id,
526 module_i2c_driver(lm25066_driver);
528 MODULE_AUTHOR("Guenter Roeck");
529 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
530 MODULE_LICENSE("GPL");