2 * Hardware monitoring driver for LM25056 / LM25063 / 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/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/err.h>
26 #include <linux/slab.h>
27 #include <linux/i2c.h>
30 enum chips
{ lm25056
, lm25063
, lm25066
, lm5064
, lm5066
};
32 #define LM25066_READ_VAUX 0xd0
33 #define LM25066_MFR_READ_IIN 0xd1
34 #define LM25066_MFR_READ_PIN 0xd2
35 #define LM25066_MFR_IIN_OC_WARN_LIMIT 0xd3
36 #define LM25066_MFR_PIN_OP_WARN_LIMIT 0xd4
37 #define LM25066_READ_PIN_PEAK 0xd5
38 #define LM25066_CLEAR_PIN_PEAK 0xd6
39 #define LM25066_DEVICE_SETUP 0xd9
40 #define LM25066_READ_AVG_VIN 0xdc
41 #define LM25066_READ_AVG_VOUT 0xdd
42 #define LM25066_READ_AVG_IIN 0xde
43 #define LM25066_READ_AVG_PIN 0xdf
45 #define LM25066_DEV_SETUP_CL (1 << 4) /* Current limit */
49 #define LM25056_VAUX_OV_WARN_LIMIT 0xe3
50 #define LM25056_VAUX_UV_WARN_LIMIT 0xe4
52 #define LM25056_MFR_STS_VAUX_OV_WARN (1 << 1)
53 #define LM25056_MFR_STS_VAUX_UV_WARN (1 << 0)
57 #define LM25063_READ_VOUT_MAX 0xe5
58 #define LM25063_READ_VOUT_MIN 0xe6
64 #define PSC_CURRENT_IN_L (PSC_NUM_CLASSES)
65 #define PSC_POWER_L (PSC_NUM_CLASSES + 1)
67 static struct __coeff lm25066_coeff
[5][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
] = {
159 [PSC_VOLTAGE_OUT
] = {
167 [PSC_CURRENT_IN_L
] = {
179 [PSC_TEMPERATURE
] = {
188 [PSC_VOLTAGE_OUT
] = {
196 [PSC_CURRENT_IN_L
] = {
208 [PSC_TEMPERATURE
] = {
214 struct lm25066_data
{
216 u16 rlimit
; /* Maximum register value */
217 struct pmbus_driver_info info
;
220 #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
222 static int lm25066_read_word_data(struct i2c_client
*client
, int page
, int reg
)
224 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
225 const struct lm25066_data
*data
= to_lm25066_data(info
);
229 case PMBUS_VIRT_READ_VMON
:
230 ret
= pmbus_read_word_data(client
, 0, LM25066_READ_VAUX
);
233 /* Adjust returned value to match VIN coefficients */
236 /* VIN: 6.14 mV VAUX: 293 uV LSB */
237 ret
= DIV_ROUND_CLOSEST(ret
* 293, 6140);
240 /* VIN: 6.25 mV VAUX: 200.0 uV LSB */
241 ret
= DIV_ROUND_CLOSEST(ret
* 20, 625);
244 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
245 ret
= DIV_ROUND_CLOSEST(ret
* 2832, 45400);
248 /* VIN: 4.53 mV VAUX: 700 uV LSB */
249 ret
= DIV_ROUND_CLOSEST(ret
* 70, 453);
252 /* VIN: 2.18 mV VAUX: 725 uV LSB */
253 ret
= DIV_ROUND_CLOSEST(ret
* 725, 2180);
258 ret
= pmbus_read_word_data(client
, 0, LM25066_MFR_READ_IIN
);
261 ret
= pmbus_read_word_data(client
, 0, LM25066_MFR_READ_PIN
);
263 case PMBUS_IIN_OC_WARN_LIMIT
:
264 ret
= pmbus_read_word_data(client
, 0,
265 LM25066_MFR_IIN_OC_WARN_LIMIT
);
267 case PMBUS_PIN_OP_WARN_LIMIT
:
268 ret
= pmbus_read_word_data(client
, 0,
269 LM25066_MFR_PIN_OP_WARN_LIMIT
);
271 case PMBUS_VIRT_READ_VIN_AVG
:
272 ret
= pmbus_read_word_data(client
, 0, LM25066_READ_AVG_VIN
);
274 case PMBUS_VIRT_READ_VOUT_AVG
:
275 ret
= pmbus_read_word_data(client
, 0, LM25066_READ_AVG_VOUT
);
277 case PMBUS_VIRT_READ_IIN_AVG
:
278 ret
= pmbus_read_word_data(client
, 0, LM25066_READ_AVG_IIN
);
280 case PMBUS_VIRT_READ_PIN_AVG
:
281 ret
= pmbus_read_word_data(client
, 0, LM25066_READ_AVG_PIN
);
283 case PMBUS_VIRT_READ_PIN_MAX
:
284 ret
= pmbus_read_word_data(client
, 0, LM25066_READ_PIN_PEAK
);
286 case PMBUS_VIRT_RESET_PIN_HISTORY
:
296 static int lm25063_read_word_data(struct i2c_client
*client
, int page
, int reg
)
301 case PMBUS_VIRT_READ_VOUT_MAX
:
302 ret
= pmbus_read_word_data(client
, 0, LM25063_READ_VOUT_MAX
);
304 case PMBUS_VIRT_READ_VOUT_MIN
:
305 ret
= pmbus_read_word_data(client
, 0, LM25063_READ_VOUT_MIN
);
308 ret
= lm25066_read_word_data(client
, page
, reg
);
314 static int lm25056_read_word_data(struct i2c_client
*client
, int page
, int reg
)
319 case PMBUS_VIRT_VMON_UV_WARN_LIMIT
:
320 ret
= pmbus_read_word_data(client
, 0,
321 LM25056_VAUX_UV_WARN_LIMIT
);
324 /* Adjust returned value to match VIN coefficients */
325 ret
= DIV_ROUND_CLOSEST(ret
* 293, 6140);
327 case PMBUS_VIRT_VMON_OV_WARN_LIMIT
:
328 ret
= pmbus_read_word_data(client
, 0,
329 LM25056_VAUX_OV_WARN_LIMIT
);
332 /* Adjust returned value to match VIN coefficients */
333 ret
= DIV_ROUND_CLOSEST(ret
* 293, 6140);
336 ret
= lm25066_read_word_data(client
, page
, reg
);
342 static int lm25056_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
347 case PMBUS_VIRT_STATUS_VMON
:
348 ret
= pmbus_read_byte_data(client
, 0,
349 PMBUS_STATUS_MFR_SPECIFIC
);
353 if (ret
& LM25056_MFR_STS_VAUX_UV_WARN
)
354 s
|= PB_VOLTAGE_UV_WARNING
;
355 if (ret
& LM25056_MFR_STS_VAUX_OV_WARN
)
356 s
|= PB_VOLTAGE_OV_WARNING
;
366 static int lm25066_write_word_data(struct i2c_client
*client
, int page
, int reg
,
369 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
370 const struct lm25066_data
*data
= to_lm25066_data(info
);
374 case PMBUS_POUT_OP_FAULT_LIMIT
:
375 case PMBUS_POUT_OP_WARN_LIMIT
:
376 case PMBUS_VOUT_UV_WARN_LIMIT
:
377 case PMBUS_OT_FAULT_LIMIT
:
378 case PMBUS_OT_WARN_LIMIT
:
379 case PMBUS_IIN_OC_FAULT_LIMIT
:
380 case PMBUS_VIN_UV_WARN_LIMIT
:
381 case PMBUS_VIN_UV_FAULT_LIMIT
:
382 case PMBUS_VIN_OV_FAULT_LIMIT
:
383 case PMBUS_VIN_OV_WARN_LIMIT
:
384 word
= ((s16
)word
< 0) ? 0 : clamp_val(word
, 0, data
->rlimit
);
385 ret
= pmbus_write_word_data(client
, 0, reg
, word
);
386 pmbus_clear_cache(client
);
388 case PMBUS_IIN_OC_WARN_LIMIT
:
389 word
= ((s16
)word
< 0) ? 0 : clamp_val(word
, 0, data
->rlimit
);
390 ret
= pmbus_write_word_data(client
, 0,
391 LM25066_MFR_IIN_OC_WARN_LIMIT
,
393 pmbus_clear_cache(client
);
395 case PMBUS_PIN_OP_WARN_LIMIT
:
396 word
= ((s16
)word
< 0) ? 0 : clamp_val(word
, 0, data
->rlimit
);
397 ret
= pmbus_write_word_data(client
, 0,
398 LM25066_MFR_PIN_OP_WARN_LIMIT
,
400 pmbus_clear_cache(client
);
402 case PMBUS_VIRT_VMON_UV_WARN_LIMIT
:
403 /* Adjust from VIN coefficients (for LM25056) */
404 word
= DIV_ROUND_CLOSEST((int)word
* 6140, 293);
405 word
= ((s16
)word
< 0) ? 0 : clamp_val(word
, 0, data
->rlimit
);
406 ret
= pmbus_write_word_data(client
, 0,
407 LM25056_VAUX_UV_WARN_LIMIT
, word
);
408 pmbus_clear_cache(client
);
410 case PMBUS_VIRT_VMON_OV_WARN_LIMIT
:
411 /* Adjust from VIN coefficients (for LM25056) */
412 word
= DIV_ROUND_CLOSEST((int)word
* 6140, 293);
413 word
= ((s16
)word
< 0) ? 0 : clamp_val(word
, 0, data
->rlimit
);
414 ret
= pmbus_write_word_data(client
, 0,
415 LM25056_VAUX_OV_WARN_LIMIT
, word
);
416 pmbus_clear_cache(client
);
418 case PMBUS_VIRT_RESET_PIN_HISTORY
:
419 ret
= pmbus_write_byte(client
, 0, LM25066_CLEAR_PIN_PEAK
);
428 static int lm25066_probe(struct i2c_client
*client
,
429 const struct i2c_device_id
*id
)
432 struct lm25066_data
*data
;
433 struct pmbus_driver_info
*info
;
434 struct __coeff
*coeff
;
436 if (!i2c_check_functionality(client
->adapter
,
437 I2C_FUNC_SMBUS_READ_BYTE_DATA
))
440 data
= devm_kzalloc(&client
->dev
, sizeof(struct lm25066_data
),
445 config
= i2c_smbus_read_byte_data(client
, LM25066_DEVICE_SETUP
);
449 data
->id
= id
->driver_data
;
453 info
->format
[PSC_VOLTAGE_IN
] = direct
;
454 info
->format
[PSC_VOLTAGE_OUT
] = direct
;
455 info
->format
[PSC_CURRENT_IN
] = direct
;
456 info
->format
[PSC_TEMPERATURE
] = direct
;
457 info
->format
[PSC_POWER
] = direct
;
459 info
->func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_VMON
460 | PMBUS_HAVE_PIN
| PMBUS_HAVE_IIN
| PMBUS_HAVE_STATUS_INPUT
461 | PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
;
463 if (data
->id
== lm25056
) {
464 info
->func
[0] |= PMBUS_HAVE_STATUS_VMON
;
465 info
->read_word_data
= lm25056_read_word_data
;
466 info
->read_byte_data
= lm25056_read_byte_data
;
467 data
->rlimit
= 0x0fff;
468 } else if (data
->id
== lm25063
) {
469 info
->func
[0] |= PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
471 info
->read_word_data
= lm25063_read_word_data
;
472 data
->rlimit
= 0xffff;
474 info
->func
[0] |= PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
;
475 info
->read_word_data
= lm25066_read_word_data
;
476 data
->rlimit
= 0x0fff;
478 info
->write_word_data
= lm25066_write_word_data
;
480 coeff
= &lm25066_coeff
[data
->id
][0];
481 info
->m
[PSC_TEMPERATURE
] = coeff
[PSC_TEMPERATURE
].m
;
482 info
->b
[PSC_TEMPERATURE
] = coeff
[PSC_TEMPERATURE
].b
;
483 info
->R
[PSC_TEMPERATURE
] = coeff
[PSC_TEMPERATURE
].R
;
484 info
->m
[PSC_VOLTAGE_IN
] = coeff
[PSC_VOLTAGE_IN
].m
;
485 info
->b
[PSC_VOLTAGE_IN
] = coeff
[PSC_VOLTAGE_IN
].b
;
486 info
->R
[PSC_VOLTAGE_IN
] = coeff
[PSC_VOLTAGE_IN
].R
;
487 info
->m
[PSC_VOLTAGE_OUT
] = coeff
[PSC_VOLTAGE_OUT
].m
;
488 info
->b
[PSC_VOLTAGE_OUT
] = coeff
[PSC_VOLTAGE_OUT
].b
;
489 info
->R
[PSC_VOLTAGE_OUT
] = coeff
[PSC_VOLTAGE_OUT
].R
;
490 info
->b
[PSC_CURRENT_IN
] = coeff
[PSC_CURRENT_IN
].b
;
491 info
->R
[PSC_CURRENT_IN
] = coeff
[PSC_CURRENT_IN
].R
;
492 info
->b
[PSC_POWER
] = coeff
[PSC_POWER
].b
;
493 info
->R
[PSC_POWER
] = coeff
[PSC_POWER
].R
;
494 if (config
& LM25066_DEV_SETUP_CL
) {
495 info
->m
[PSC_CURRENT_IN
] = coeff
[PSC_CURRENT_IN_L
].m
;
496 info
->m
[PSC_POWER
] = coeff
[PSC_POWER_L
].m
;
498 info
->m
[PSC_CURRENT_IN
] = coeff
[PSC_CURRENT_IN
].m
;
499 info
->m
[PSC_POWER
] = coeff
[PSC_POWER
].m
;
502 return pmbus_do_probe(client
, id
, info
);
505 static const struct i2c_device_id lm25066_id
[] = {
506 {"lm25056", lm25056
},
507 {"lm25063", lm25063
},
508 {"lm25066", lm25066
},
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");