2 * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
3 * and Digital Power Monitor
5 * Copyright (c) 2011 Ericsson AB.
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.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/err.h>
22 #include <linux/slab.h>
23 #include <linux/i2c.h>
24 #include <linux/bitops.h>
27 enum chips
{ adm1075
, adm1275
, adm1276
, adm1293
, adm1294
};
29 #define ADM1275_MFR_STATUS_IOUT_WARN2 BIT(0)
30 #define ADM1293_MFR_STATUS_VAUX_UV_WARN BIT(5)
31 #define ADM1293_MFR_STATUS_VAUX_OV_WARN BIT(6)
33 #define ADM1275_PEAK_IOUT 0xd0
34 #define ADM1275_PEAK_VIN 0xd1
35 #define ADM1275_PEAK_VOUT 0xd2
36 #define ADM1275_PMON_CONFIG 0xd4
38 #define ADM1275_VIN_VOUT_SELECT BIT(6)
39 #define ADM1275_VRANGE BIT(5)
40 #define ADM1075_IRANGE_50 BIT(4)
41 #define ADM1075_IRANGE_25 BIT(3)
42 #define ADM1075_IRANGE_MASK (BIT(3) | BIT(4))
44 #define ADM1293_IRANGE_25 0
45 #define ADM1293_IRANGE_50 BIT(6)
46 #define ADM1293_IRANGE_100 BIT(7)
47 #define ADM1293_IRANGE_200 (BIT(6) | BIT(7))
48 #define ADM1293_IRANGE_MASK (BIT(6) | BIT(7))
50 #define ADM1293_VIN_SEL_012 BIT(2)
51 #define ADM1293_VIN_SEL_074 BIT(3)
52 #define ADM1293_VIN_SEL_210 (BIT(2) | BIT(3))
53 #define ADM1293_VIN_SEL_MASK (BIT(2) | BIT(3))
55 #define ADM1293_VAUX_EN BIT(1)
57 #define ADM1275_IOUT_WARN2_LIMIT 0xd7
58 #define ADM1275_DEVICE_CONFIG 0xd8
60 #define ADM1275_IOUT_WARN2_SELECT BIT(4)
62 #define ADM1276_PEAK_PIN 0xda
63 #define ADM1075_READ_VAUX 0xdd
64 #define ADM1075_VAUX_OV_WARN_LIMIT 0xde
65 #define ADM1075_VAUX_UV_WARN_LIMIT 0xdf
66 #define ADM1293_IOUT_MIN 0xe3
67 #define ADM1293_PIN_MIN 0xe4
68 #define ADM1075_VAUX_STATUS 0xf6
70 #define ADM1075_VAUX_OV_WARN BIT(7)
71 #define ADM1075_VAUX_UV_WARN BIT(6)
78 bool have_vaux_status
;
79 bool have_mfr_vaux_status
;
83 struct pmbus_driver_info info
;
86 #define to_adm1275_data(x) container_of(x, struct adm1275_data, info)
94 static const struct coefficients adm1075_coefficients
[] = {
95 [0] = { 27169, 0, -1 }, /* voltage */
96 [1] = { 806, 20475, -1 }, /* current, irange25 */
97 [2] = { 404, 20475, -1 }, /* current, irange50 */
98 [3] = { 0, -1, 8549 }, /* power, irange25 */
99 [4] = { 0, -1, 4279 }, /* power, irange50 */
102 static const struct coefficients adm1275_coefficients
[] = {
103 [0] = { 19199, 0, -2 }, /* voltage, vrange set */
104 [1] = { 6720, 0, -1 }, /* voltage, vrange not set */
105 [2] = { 807, 20475, -1 }, /* current */
108 static const struct coefficients adm1276_coefficients
[] = {
109 [0] = { 19199, 0, -2 }, /* voltage, vrange set */
110 [1] = { 6720, 0, -1 }, /* voltage, vrange not set */
111 [2] = { 807, 20475, -1 }, /* current */
112 [3] = { 6043, 0, -2 }, /* power, vrange set */
113 [4] = { 2115, 0, -1 }, /* power, vrange not set */
116 static const struct coefficients adm1293_coefficients
[] = {
117 [0] = { 3333, -1, 0 }, /* voltage, vrange 1.2V */
118 [1] = { 5552, -5, -1 }, /* voltage, vrange 7.4V */
119 [2] = { 19604, -50, -2 }, /* voltage, vrange 21V */
120 [3] = { 8000, -100, -2 }, /* current, irange25 */
121 [4] = { 4000, -100, -2 }, /* current, irange50 */
122 [5] = { 20000, -1000, -3 }, /* current, irange100 */
123 [6] = { 10000, -1000, -3 }, /* current, irange200 */
124 [7] = { 10417, 0, -1 }, /* power, 1.2V, irange25 */
125 [8] = { 5208, 0, -1 }, /* power, 1.2V, irange50 */
126 [9] = { 26042, 0, -2 }, /* power, 1.2V, irange100 */
127 [10] = { 13021, 0, -2 }, /* power, 1.2V, irange200 */
128 [11] = { 17351, 0, -2 }, /* power, 7.4V, irange25 */
129 [12] = { 8676, 0, -2 }, /* power, 7.4V, irange50 */
130 [13] = { 4338, 0, -2 }, /* power, 7.4V, irange100 */
131 [14] = { 21689, 0, -3 }, /* power, 7.4V, irange200 */
132 [15] = { 6126, 0, -2 }, /* power, 21V, irange25 */
133 [16] = { 30631, 0, -3 }, /* power, 21V, irange50 */
134 [17] = { 15316, 0, -3 }, /* power, 21V, irange100 */
135 [18] = { 7658, 0, -3 }, /* power, 21V, irange200 */
138 static int adm1275_read_word_data(struct i2c_client
*client
, int page
, int reg
)
140 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
141 const struct adm1275_data
*data
= to_adm1275_data(info
);
148 case PMBUS_IOUT_UC_FAULT_LIMIT
:
149 if (!data
->have_uc_fault
)
151 ret
= pmbus_read_word_data(client
, 0, ADM1275_IOUT_WARN2_LIMIT
);
153 case PMBUS_IOUT_OC_FAULT_LIMIT
:
154 if (!data
->have_oc_fault
)
156 ret
= pmbus_read_word_data(client
, 0, ADM1275_IOUT_WARN2_LIMIT
);
158 case PMBUS_VOUT_OV_WARN_LIMIT
:
161 ret
= pmbus_read_word_data(client
, 0,
162 ADM1075_VAUX_OV_WARN_LIMIT
);
164 case PMBUS_VOUT_UV_WARN_LIMIT
:
167 ret
= pmbus_read_word_data(client
, 0,
168 ADM1075_VAUX_UV_WARN_LIMIT
);
170 case PMBUS_READ_VOUT
:
173 ret
= pmbus_read_word_data(client
, 0, ADM1075_READ_VAUX
);
175 case PMBUS_VIRT_READ_IOUT_MIN
:
176 if (!data
->have_iout_min
)
178 ret
= pmbus_read_word_data(client
, 0, ADM1293_IOUT_MIN
);
180 case PMBUS_VIRT_READ_IOUT_MAX
:
181 ret
= pmbus_read_word_data(client
, 0, ADM1275_PEAK_IOUT
);
183 case PMBUS_VIRT_READ_VOUT_MAX
:
184 ret
= pmbus_read_word_data(client
, 0, ADM1275_PEAK_VOUT
);
186 case PMBUS_VIRT_READ_VIN_MAX
:
187 ret
= pmbus_read_word_data(client
, 0, ADM1275_PEAK_VIN
);
189 case PMBUS_VIRT_READ_PIN_MIN
:
190 if (!data
->have_pin_min
)
192 ret
= pmbus_read_word_data(client
, 0, ADM1293_PIN_MIN
);
194 case PMBUS_VIRT_READ_PIN_MAX
:
195 if (!data
->have_pin_max
)
197 ret
= pmbus_read_word_data(client
, 0, ADM1276_PEAK_PIN
);
199 case PMBUS_VIRT_RESET_IOUT_HISTORY
:
200 case PMBUS_VIRT_RESET_VOUT_HISTORY
:
201 case PMBUS_VIRT_RESET_VIN_HISTORY
:
203 case PMBUS_VIRT_RESET_PIN_HISTORY
:
204 if (!data
->have_pin_max
)
214 static int adm1275_write_word_data(struct i2c_client
*client
, int page
, int reg
,
217 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
218 const struct adm1275_data
*data
= to_adm1275_data(info
);
225 case PMBUS_IOUT_UC_FAULT_LIMIT
:
226 case PMBUS_IOUT_OC_FAULT_LIMIT
:
227 ret
= pmbus_write_word_data(client
, 0, ADM1275_IOUT_WARN2_LIMIT
,
230 case PMBUS_VIRT_RESET_IOUT_HISTORY
:
231 ret
= pmbus_write_word_data(client
, 0, ADM1275_PEAK_IOUT
, 0);
232 if (!ret
&& data
->have_iout_min
)
233 ret
= pmbus_write_word_data(client
, 0,
234 ADM1293_IOUT_MIN
, 0);
236 case PMBUS_VIRT_RESET_VOUT_HISTORY
:
237 ret
= pmbus_write_word_data(client
, 0, ADM1275_PEAK_VOUT
, 0);
239 case PMBUS_VIRT_RESET_VIN_HISTORY
:
240 ret
= pmbus_write_word_data(client
, 0, ADM1275_PEAK_VIN
, 0);
242 case PMBUS_VIRT_RESET_PIN_HISTORY
:
243 ret
= pmbus_write_word_data(client
, 0, ADM1276_PEAK_PIN
, 0);
244 if (!ret
&& data
->have_pin_min
)
245 ret
= pmbus_write_word_data(client
, 0,
255 static int adm1275_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
257 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
258 const struct adm1275_data
*data
= to_adm1275_data(info
);
265 case PMBUS_STATUS_IOUT
:
266 ret
= pmbus_read_byte_data(client
, page
, PMBUS_STATUS_IOUT
);
269 if (!data
->have_oc_fault
&& !data
->have_uc_fault
)
271 mfr_status
= pmbus_read_byte_data(client
, page
,
272 PMBUS_STATUS_MFR_SPECIFIC
);
275 if (mfr_status
& ADM1275_MFR_STATUS_IOUT_WARN2
) {
276 ret
|= data
->have_oc_fault
?
277 PB_IOUT_OC_FAULT
: PB_IOUT_UC_FAULT
;
280 case PMBUS_STATUS_VOUT
:
284 if (data
->have_vaux_status
) {
285 mfr_status
= pmbus_read_byte_data(client
, 0,
286 ADM1075_VAUX_STATUS
);
289 if (mfr_status
& ADM1075_VAUX_OV_WARN
)
290 ret
|= PB_VOLTAGE_OV_WARNING
;
291 if (mfr_status
& ADM1075_VAUX_UV_WARN
)
292 ret
|= PB_VOLTAGE_UV_WARNING
;
293 } else if (data
->have_mfr_vaux_status
) {
294 mfr_status
= pmbus_read_byte_data(client
, page
,
295 PMBUS_STATUS_MFR_SPECIFIC
);
298 if (mfr_status
& ADM1293_MFR_STATUS_VAUX_OV_WARN
)
299 ret
|= PB_VOLTAGE_OV_WARNING
;
300 if (mfr_status
& ADM1293_MFR_STATUS_VAUX_UV_WARN
)
301 ret
|= PB_VOLTAGE_UV_WARNING
;
311 static const struct i2c_device_id adm1275_id
[] = {
312 { "adm1075", adm1075
},
313 { "adm1275", adm1275
},
314 { "adm1276", adm1276
},
315 { "adm1293", adm1293
},
316 { "adm1294", adm1294
},
319 MODULE_DEVICE_TABLE(i2c
, adm1275_id
);
321 static int adm1275_probe(struct i2c_client
*client
,
322 const struct i2c_device_id
*id
)
324 u8 block_buffer
[I2C_SMBUS_BLOCK_MAX
+ 1];
325 int config
, device_config
;
327 struct pmbus_driver_info
*info
;
328 struct adm1275_data
*data
;
329 const struct i2c_device_id
*mid
;
330 const struct coefficients
*coefficients
;
331 int vindex
= -1, voindex
= -1, cindex
= -1, pindex
= -1;
333 if (!i2c_check_functionality(client
->adapter
,
334 I2C_FUNC_SMBUS_READ_BYTE_DATA
335 | I2C_FUNC_SMBUS_BLOCK_DATA
))
338 ret
= i2c_smbus_read_block_data(client
, PMBUS_MFR_ID
, block_buffer
);
340 dev_err(&client
->dev
, "Failed to read Manufacturer ID\n");
343 if (ret
!= 3 || strncmp(block_buffer
, "ADI", 3)) {
344 dev_err(&client
->dev
, "Unsupported Manufacturer ID\n");
348 ret
= i2c_smbus_read_block_data(client
, PMBUS_MFR_MODEL
, block_buffer
);
350 dev_err(&client
->dev
, "Failed to read Manufacturer Model\n");
353 for (mid
= adm1275_id
; mid
->name
[0]; mid
++) {
354 if (!strncasecmp(mid
->name
, block_buffer
, strlen(mid
->name
)))
358 dev_err(&client
->dev
, "Unsupported device\n");
362 if (id
->driver_data
!= mid
->driver_data
)
363 dev_notice(&client
->dev
,
364 "Device mismatch: Configured %s, detected %s\n",
365 id
->name
, mid
->name
);
367 config
= i2c_smbus_read_byte_data(client
, ADM1275_PMON_CONFIG
);
371 device_config
= i2c_smbus_read_byte_data(client
, ADM1275_DEVICE_CONFIG
);
372 if (device_config
< 0)
373 return device_config
;
375 data
= devm_kzalloc(&client
->dev
, sizeof(struct adm1275_data
),
380 data
->id
= mid
->driver_data
;
385 info
->format
[PSC_VOLTAGE_IN
] = direct
;
386 info
->format
[PSC_VOLTAGE_OUT
] = direct
;
387 info
->format
[PSC_CURRENT_OUT
] = direct
;
388 info
->format
[PSC_POWER
] = direct
;
389 info
->func
[0] = PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
;
391 info
->read_word_data
= adm1275_read_word_data
;
392 info
->read_byte_data
= adm1275_read_byte_data
;
393 info
->write_word_data
= adm1275_write_word_data
;
397 if (device_config
& ADM1275_IOUT_WARN2_SELECT
)
398 data
->have_oc_fault
= true;
400 data
->have_uc_fault
= true;
401 data
->have_pin_max
= true;
402 data
->have_vaux_status
= true;
404 coefficients
= adm1075_coefficients
;
406 switch (config
& ADM1075_IRANGE_MASK
) {
407 case ADM1075_IRANGE_25
:
411 case ADM1075_IRANGE_50
:
416 dev_err(&client
->dev
, "Invalid input current range");
420 info
->func
[0] |= PMBUS_HAVE_VIN
| PMBUS_HAVE_PIN
421 | PMBUS_HAVE_STATUS_INPUT
;
422 if (config
& ADM1275_VIN_VOUT_SELECT
)
424 PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
;
427 if (device_config
& ADM1275_IOUT_WARN2_SELECT
)
428 data
->have_oc_fault
= true;
430 data
->have_uc_fault
= true;
431 data
->have_vout
= true;
433 coefficients
= adm1275_coefficients
;
434 vindex
= (config
& ADM1275_VRANGE
) ? 0 : 1;
437 if (config
& ADM1275_VIN_VOUT_SELECT
)
439 PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
;
442 PMBUS_HAVE_VIN
| PMBUS_HAVE_STATUS_INPUT
;
445 if (device_config
& ADM1275_IOUT_WARN2_SELECT
)
446 data
->have_oc_fault
= true;
448 data
->have_uc_fault
= true;
449 data
->have_vout
= true;
450 data
->have_pin_max
= true;
452 coefficients
= adm1276_coefficients
;
453 vindex
= (config
& ADM1275_VRANGE
) ? 0 : 1;
455 pindex
= (config
& ADM1275_VRANGE
) ? 3 : 4;
457 info
->func
[0] |= PMBUS_HAVE_VIN
| PMBUS_HAVE_PIN
458 | PMBUS_HAVE_STATUS_INPUT
;
459 if (config
& ADM1275_VIN_VOUT_SELECT
)
461 PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
;
465 data
->have_iout_min
= true;
466 data
->have_pin_min
= true;
467 data
->have_pin_max
= true;
468 data
->have_mfr_vaux_status
= true;
470 coefficients
= adm1293_coefficients
;
473 switch (config
& ADM1293_VIN_SEL_MASK
) {
474 case ADM1293_VIN_SEL_012
: /* 1.2V */
477 case ADM1293_VIN_SEL_074
: /* 7.4V */
480 case ADM1293_VIN_SEL_210
: /* 21V */
483 default: /* disabled */
487 switch (config
& ADM1293_IRANGE_MASK
) {
488 case ADM1293_IRANGE_25
:
491 case ADM1293_IRANGE_50
:
494 case ADM1293_IRANGE_100
:
497 case ADM1293_IRANGE_200
:
503 pindex
= 7 + vindex
* 4 + (cindex
- 3);
505 if (config
& ADM1293_VAUX_EN
)
507 PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
;
509 info
->func
[0] |= PMBUS_HAVE_PIN
|
510 PMBUS_HAVE_VIN
| PMBUS_HAVE_STATUS_INPUT
;
514 dev_err(&client
->dev
, "Unsupported device\n");
521 info
->m
[PSC_VOLTAGE_IN
] = coefficients
[vindex
].m
;
522 info
->b
[PSC_VOLTAGE_IN
] = coefficients
[vindex
].b
;
523 info
->R
[PSC_VOLTAGE_IN
] = coefficients
[vindex
].R
;
526 info
->m
[PSC_VOLTAGE_OUT
] = coefficients
[voindex
].m
;
527 info
->b
[PSC_VOLTAGE_OUT
] = coefficients
[voindex
].b
;
528 info
->R
[PSC_VOLTAGE_OUT
] = coefficients
[voindex
].R
;
531 info
->m
[PSC_CURRENT_OUT
] = coefficients
[cindex
].m
;
532 info
->b
[PSC_CURRENT_OUT
] = coefficients
[cindex
].b
;
533 info
->R
[PSC_CURRENT_OUT
] = coefficients
[cindex
].R
;
536 info
->m
[PSC_POWER
] = coefficients
[pindex
].m
;
537 info
->b
[PSC_POWER
] = coefficients
[pindex
].b
;
538 info
->R
[PSC_POWER
] = coefficients
[pindex
].R
;
541 return pmbus_do_probe(client
, id
, info
);
544 static struct i2c_driver adm1275_driver
= {
548 .probe
= adm1275_probe
,
549 .remove
= pmbus_do_remove
,
550 .id_table
= adm1275_id
,
553 module_i2c_driver(adm1275_driver
);
555 MODULE_AUTHOR("Guenter Roeck");
556 MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
557 MODULE_LICENSE("GPL");