2 * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
3 * and Digital Power Monitor
5 * Copyright (c) 2011 Ericsson AB.
6 * Copyright (c) 2018 Guenter Roeck
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/i2c.h>
25 #include <linux/bitops.h>
28 enum chips { adm1075, adm1272, adm1275, adm1276, adm1278, adm1293, adm1294 };
30 #define ADM1275_MFR_STATUS_IOUT_WARN2 BIT(0)
31 #define ADM1293_MFR_STATUS_VAUX_UV_WARN BIT(5)
32 #define ADM1293_MFR_STATUS_VAUX_OV_WARN BIT(6)
34 #define ADM1275_PEAK_IOUT 0xd0
35 #define ADM1275_PEAK_VIN 0xd1
36 #define ADM1275_PEAK_VOUT 0xd2
37 #define ADM1275_PMON_CONFIG 0xd4
39 #define ADM1275_VIN_VOUT_SELECT BIT(6)
40 #define ADM1275_VRANGE BIT(5)
41 #define ADM1075_IRANGE_50 BIT(4)
42 #define ADM1075_IRANGE_25 BIT(3)
43 #define ADM1075_IRANGE_MASK (BIT(3) | BIT(4))
45 #define ADM1272_IRANGE BIT(0)
47 #define ADM1278_TEMP1_EN BIT(3)
48 #define ADM1278_VIN_EN BIT(2)
49 #define ADM1278_VOUT_EN BIT(1)
51 #define ADM1293_IRANGE_25 0
52 #define ADM1293_IRANGE_50 BIT(6)
53 #define ADM1293_IRANGE_100 BIT(7)
54 #define ADM1293_IRANGE_200 (BIT(6) | BIT(7))
55 #define ADM1293_IRANGE_MASK (BIT(6) | BIT(7))
57 #define ADM1293_VIN_SEL_012 BIT(2)
58 #define ADM1293_VIN_SEL_074 BIT(3)
59 #define ADM1293_VIN_SEL_210 (BIT(2) | BIT(3))
60 #define ADM1293_VIN_SEL_MASK (BIT(2) | BIT(3))
62 #define ADM1293_VAUX_EN BIT(1)
64 #define ADM1278_PEAK_TEMP 0xd7
65 #define ADM1275_IOUT_WARN2_LIMIT 0xd7
66 #define ADM1275_DEVICE_CONFIG 0xd8
68 #define ADM1275_IOUT_WARN2_SELECT BIT(4)
70 #define ADM1276_PEAK_PIN 0xda
71 #define ADM1075_READ_VAUX 0xdd
72 #define ADM1075_VAUX_OV_WARN_LIMIT 0xde
73 #define ADM1075_VAUX_UV_WARN_LIMIT 0xdf
74 #define ADM1293_IOUT_MIN 0xe3
75 #define ADM1293_PIN_MIN 0xe4
76 #define ADM1075_VAUX_STATUS 0xf6
78 #define ADM1075_VAUX_OV_WARN BIT(7)
79 #define ADM1075_VAUX_UV_WARN BIT(6)
86 bool have_vaux_status;
87 bool have_mfr_vaux_status;
92 struct pmbus_driver_info info;
95 #define to_adm1275_data(x) container_of(x, struct adm1275_data, info)
103 static const struct coefficients adm1075_coefficients[] = {
104 [0] = { 27169, 0, -1 }, /* voltage */
105 [1] = { 806, 20475, -1 }, /* current, irange25 */
106 [2] = { 404, 20475, -1 }, /* current, irange50 */
107 [3] = { 8549, 0, -1 }, /* power, irange25 */
108 [4] = { 4279, 0, -1 }, /* power, irange50 */
111 static const struct coefficients adm1272_coefficients[] = {
112 [0] = { 6770, 0, -2 }, /* voltage, vrange 60V */
113 [1] = { 4062, 0, -2 }, /* voltage, vrange 100V */
114 [2] = { 1326, 20480, -1 }, /* current, vsense range 15mV */
115 [3] = { 663, 20480, -1 }, /* current, vsense range 30mV */
116 [4] = { 3512, 0, -2 }, /* power, vrange 60V, irange 15mV */
117 [5] = { 21071, 0, -3 }, /* power, vrange 100V, irange 15mV */
118 [6] = { 17561, 0, -3 }, /* power, vrange 60V, irange 30mV */
119 [7] = { 10535, 0, -3 }, /* power, vrange 100V, irange 30mV */
120 [8] = { 42, 31871, -1 }, /* temperature */
124 static const struct coefficients adm1275_coefficients[] = {
125 [0] = { 19199, 0, -2 }, /* voltage, vrange set */
126 [1] = { 6720, 0, -1 }, /* voltage, vrange not set */
127 [2] = { 807, 20475, -1 }, /* current */
130 static const struct coefficients adm1276_coefficients[] = {
131 [0] = { 19199, 0, -2 }, /* voltage, vrange set */
132 [1] = { 6720, 0, -1 }, /* voltage, vrange not set */
133 [2] = { 807, 20475, -1 }, /* current */
134 [3] = { 6043, 0, -2 }, /* power, vrange set */
135 [4] = { 2115, 0, -1 }, /* power, vrange not set */
138 static const struct coefficients adm1278_coefficients[] = {
139 [0] = { 19599, 0, -2 }, /* voltage */
140 [1] = { 800, 20475, -1 }, /* current */
141 [2] = { 6123, 0, -2 }, /* power */
142 [3] = { 42, 31880, -1 }, /* temperature */
145 static const struct coefficients adm1293_coefficients[] = {
146 [0] = { 3333, -1, 0 }, /* voltage, vrange 1.2V */
147 [1] = { 5552, -5, -1 }, /* voltage, vrange 7.4V */
148 [2] = { 19604, -50, -2 }, /* voltage, vrange 21V */
149 [3] = { 8000, -100, -2 }, /* current, irange25 */
150 [4] = { 4000, -100, -2 }, /* current, irange50 */
151 [5] = { 20000, -1000, -3 }, /* current, irange100 */
152 [6] = { 10000, -1000, -3 }, /* current, irange200 */
153 [7] = { 10417, 0, -1 }, /* power, 1.2V, irange25 */
154 [8] = { 5208, 0, -1 }, /* power, 1.2V, irange50 */
155 [9] = { 26042, 0, -2 }, /* power, 1.2V, irange100 */
156 [10] = { 13021, 0, -2 }, /* power, 1.2V, irange200 */
157 [11] = { 17351, 0, -2 }, /* power, 7.4V, irange25 */
158 [12] = { 8676, 0, -2 }, /* power, 7.4V, irange50 */
159 [13] = { 4338, 0, -2 }, /* power, 7.4V, irange100 */
160 [14] = { 21689, 0, -3 }, /* power, 7.4V, irange200 */
161 [15] = { 6126, 0, -2 }, /* power, 21V, irange25 */
162 [16] = { 30631, 0, -3 }, /* power, 21V, irange50 */
163 [17] = { 15316, 0, -3 }, /* power, 21V, irange100 */
164 [18] = { 7658, 0, -3 }, /* power, 21V, irange200 */
167 static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
169 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
170 const struct adm1275_data *data = to_adm1275_data(info);
177 case PMBUS_IOUT_UC_FAULT_LIMIT:
178 if (!data->have_uc_fault)
180 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
182 case PMBUS_IOUT_OC_FAULT_LIMIT:
183 if (!data->have_oc_fault)
185 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
187 case PMBUS_VOUT_OV_WARN_LIMIT:
190 ret = pmbus_read_word_data(client, 0,
191 ADM1075_VAUX_OV_WARN_LIMIT);
193 case PMBUS_VOUT_UV_WARN_LIMIT:
196 ret = pmbus_read_word_data(client, 0,
197 ADM1075_VAUX_UV_WARN_LIMIT);
199 case PMBUS_READ_VOUT:
202 ret = pmbus_read_word_data(client, 0, ADM1075_READ_VAUX);
204 case PMBUS_VIRT_READ_IOUT_MIN:
205 if (!data->have_iout_min)
207 ret = pmbus_read_word_data(client, 0, ADM1293_IOUT_MIN);
209 case PMBUS_VIRT_READ_IOUT_MAX:
210 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
212 case PMBUS_VIRT_READ_VOUT_MAX:
213 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
215 case PMBUS_VIRT_READ_VIN_MAX:
216 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
218 case PMBUS_VIRT_READ_PIN_MIN:
219 if (!data->have_pin_min)
221 ret = pmbus_read_word_data(client, 0, ADM1293_PIN_MIN);
223 case PMBUS_VIRT_READ_PIN_MAX:
224 if (!data->have_pin_max)
226 ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN);
228 case PMBUS_VIRT_READ_TEMP_MAX:
229 if (!data->have_temp_max)
231 ret = pmbus_read_word_data(client, 0, ADM1278_PEAK_TEMP);
233 case PMBUS_VIRT_RESET_IOUT_HISTORY:
234 case PMBUS_VIRT_RESET_VOUT_HISTORY:
235 case PMBUS_VIRT_RESET_VIN_HISTORY:
237 case PMBUS_VIRT_RESET_PIN_HISTORY:
238 if (!data->have_pin_max)
241 case PMBUS_VIRT_RESET_TEMP_HISTORY:
242 if (!data->have_temp_max)
252 static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
255 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
256 const struct adm1275_data *data = to_adm1275_data(info);
263 case PMBUS_IOUT_UC_FAULT_LIMIT:
264 case PMBUS_IOUT_OC_FAULT_LIMIT:
265 ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
268 case PMBUS_VIRT_RESET_IOUT_HISTORY:
269 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
270 if (!ret && data->have_iout_min)
271 ret = pmbus_write_word_data(client, 0,
272 ADM1293_IOUT_MIN, 0);
274 case PMBUS_VIRT_RESET_VOUT_HISTORY:
275 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
277 case PMBUS_VIRT_RESET_VIN_HISTORY:
278 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
280 case PMBUS_VIRT_RESET_PIN_HISTORY:
281 ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0);
282 if (!ret && data->have_pin_min)
283 ret = pmbus_write_word_data(client, 0,
286 case PMBUS_VIRT_RESET_TEMP_HISTORY:
287 ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0);
296 static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
298 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
299 const struct adm1275_data *data = to_adm1275_data(info);
306 case PMBUS_STATUS_IOUT:
307 ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
310 if (!data->have_oc_fault && !data->have_uc_fault)
312 mfr_status = pmbus_read_byte_data(client, page,
313 PMBUS_STATUS_MFR_SPECIFIC);
316 if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
317 ret |= data->have_oc_fault ?
318 PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
321 case PMBUS_STATUS_VOUT:
325 if (data->have_vaux_status) {
326 mfr_status = pmbus_read_byte_data(client, 0,
327 ADM1075_VAUX_STATUS);
330 if (mfr_status & ADM1075_VAUX_OV_WARN)
331 ret |= PB_VOLTAGE_OV_WARNING;
332 if (mfr_status & ADM1075_VAUX_UV_WARN)
333 ret |= PB_VOLTAGE_UV_WARNING;
334 } else if (data->have_mfr_vaux_status) {
335 mfr_status = pmbus_read_byte_data(client, page,
336 PMBUS_STATUS_MFR_SPECIFIC);
339 if (mfr_status & ADM1293_MFR_STATUS_VAUX_OV_WARN)
340 ret |= PB_VOLTAGE_OV_WARNING;
341 if (mfr_status & ADM1293_MFR_STATUS_VAUX_UV_WARN)
342 ret |= PB_VOLTAGE_UV_WARNING;
352 static const struct i2c_device_id adm1275_id[] = {
353 { "adm1075", adm1075 },
354 { "adm1272", adm1272 },
355 { "adm1275", adm1275 },
356 { "adm1276", adm1276 },
357 { "adm1278", adm1278 },
358 { "adm1293", adm1293 },
359 { "adm1294", adm1294 },
362 MODULE_DEVICE_TABLE(i2c, adm1275_id);
364 static int adm1275_probe(struct i2c_client *client,
365 const struct i2c_device_id *id)
367 s32 (*config_read_fn)(const struct i2c_client *client, u8 reg);
368 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
369 int config, device_config;
371 struct pmbus_driver_info *info;
372 struct adm1275_data *data;
373 const struct i2c_device_id *mid;
374 const struct coefficients *coefficients;
375 int vindex = -1, voindex = -1, cindex = -1, pindex = -1;
378 if (!i2c_check_functionality(client->adapter,
379 I2C_FUNC_SMBUS_READ_BYTE_DATA
380 | I2C_FUNC_SMBUS_BLOCK_DATA))
383 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
385 dev_err(&client->dev, "Failed to read Manufacturer ID\n");
388 if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
389 dev_err(&client->dev, "Unsupported Manufacturer ID\n");
393 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
395 dev_err(&client->dev, "Failed to read Manufacturer Model\n");
398 for (mid = adm1275_id; mid->name[0]; mid++) {
399 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
403 dev_err(&client->dev, "Unsupported device\n");
407 if (id->driver_data != mid->driver_data)
408 dev_notice(&client->dev,
409 "Device mismatch: Configured %s, detected %s\n",
410 id->name, mid->name);
412 if (mid->driver_data == adm1272 || mid->driver_data == adm1278 ||
413 mid->driver_data == adm1293 || mid->driver_data == adm1294)
414 config_read_fn = i2c_smbus_read_word_data;
416 config_read_fn = i2c_smbus_read_byte_data;
417 config = config_read_fn(client, ADM1275_PMON_CONFIG);
421 device_config = config_read_fn(client, ADM1275_DEVICE_CONFIG);
422 if (device_config < 0)
423 return device_config;
425 data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
430 data->id = mid->driver_data;
435 info->format[PSC_VOLTAGE_IN] = direct;
436 info->format[PSC_VOLTAGE_OUT] = direct;
437 info->format[PSC_CURRENT_OUT] = direct;
438 info->format[PSC_POWER] = direct;
439 info->format[PSC_TEMPERATURE] = direct;
440 info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
442 info->read_word_data = adm1275_read_word_data;
443 info->read_byte_data = adm1275_read_byte_data;
444 info->write_word_data = adm1275_write_word_data;
448 if (device_config & ADM1275_IOUT_WARN2_SELECT)
449 data->have_oc_fault = true;
451 data->have_uc_fault = true;
452 data->have_pin_max = true;
453 data->have_vaux_status = true;
455 coefficients = adm1075_coefficients;
457 switch (config & ADM1075_IRANGE_MASK) {
458 case ADM1075_IRANGE_25:
462 case ADM1075_IRANGE_50:
467 dev_err(&client->dev, "Invalid input current range");
471 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
472 | PMBUS_HAVE_STATUS_INPUT;
473 if (config & ADM1275_VIN_VOUT_SELECT)
475 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
478 data->have_vout = true;
479 data->have_pin_max = true;
480 data->have_temp_max = true;
482 coefficients = adm1272_coefficients;
483 vindex = (config & ADM1275_VRANGE) ? 1 : 0;
484 cindex = (config & ADM1272_IRANGE) ? 3 : 2;
485 /* pindex depends on the combination of the above */
486 switch (config & (ADM1275_VRANGE | ADM1272_IRANGE)) {
497 case ADM1275_VRANGE | ADM1272_IRANGE:
503 info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
504 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
506 /* Enable VOUT if not enabled (it is disabled by default) */
507 if (!(config & ADM1278_VOUT_EN)) {
508 config |= ADM1278_VOUT_EN;
509 ret = i2c_smbus_write_byte_data(client,
513 dev_err(&client->dev,
514 "Failed to enable VOUT monitoring\n");
519 if (config & ADM1278_TEMP1_EN)
521 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
522 if (config & ADM1278_VIN_EN)
523 info->func[0] |= PMBUS_HAVE_VIN;
526 if (device_config & ADM1275_IOUT_WARN2_SELECT)
527 data->have_oc_fault = true;
529 data->have_uc_fault = true;
530 data->have_vout = true;
532 coefficients = adm1275_coefficients;
533 vindex = (config & ADM1275_VRANGE) ? 0 : 1;
536 if (config & ADM1275_VIN_VOUT_SELECT)
538 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
541 PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
544 if (device_config & ADM1275_IOUT_WARN2_SELECT)
545 data->have_oc_fault = true;
547 data->have_uc_fault = true;
548 data->have_vout = true;
549 data->have_pin_max = true;
551 coefficients = adm1276_coefficients;
552 vindex = (config & ADM1275_VRANGE) ? 0 : 1;
554 pindex = (config & ADM1275_VRANGE) ? 3 : 4;
556 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
557 | PMBUS_HAVE_STATUS_INPUT;
558 if (config & ADM1275_VIN_VOUT_SELECT)
560 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
563 data->have_vout = true;
564 data->have_pin_max = true;
565 data->have_temp_max = true;
567 coefficients = adm1278_coefficients;
573 info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
574 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
576 /* Enable VOUT if not enabled (it is disabled by default) */
577 if (!(config & ADM1278_VOUT_EN)) {
578 config |= ADM1278_VOUT_EN;
579 ret = i2c_smbus_write_byte_data(client,
583 dev_err(&client->dev,
584 "Failed to enable VOUT monitoring\n");
589 if (config & ADM1278_TEMP1_EN)
591 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
592 if (config & ADM1278_VIN_EN)
593 info->func[0] |= PMBUS_HAVE_VIN;
597 data->have_iout_min = true;
598 data->have_pin_min = true;
599 data->have_pin_max = true;
600 data->have_mfr_vaux_status = true;
602 coefficients = adm1293_coefficients;
605 switch (config & ADM1293_VIN_SEL_MASK) {
606 case ADM1293_VIN_SEL_012: /* 1.2V */
609 case ADM1293_VIN_SEL_074: /* 7.4V */
612 case ADM1293_VIN_SEL_210: /* 21V */
615 default: /* disabled */
619 switch (config & ADM1293_IRANGE_MASK) {
620 case ADM1293_IRANGE_25:
623 case ADM1293_IRANGE_50:
626 case ADM1293_IRANGE_100:
629 case ADM1293_IRANGE_200:
635 pindex = 7 + vindex * 4 + (cindex - 3);
637 if (config & ADM1293_VAUX_EN)
639 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
641 info->func[0] |= PMBUS_HAVE_PIN |
642 PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
646 dev_err(&client->dev, "Unsupported device\n");
653 info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m;
654 info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b;
655 info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R;
658 info->m[PSC_VOLTAGE_OUT] = coefficients[voindex].m;
659 info->b[PSC_VOLTAGE_OUT] = coefficients[voindex].b;
660 info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R;
663 info->m[PSC_CURRENT_OUT] = coefficients[cindex].m;
664 info->b[PSC_CURRENT_OUT] = coefficients[cindex].b;
665 info->R[PSC_CURRENT_OUT] = coefficients[cindex].R;
668 info->m[PSC_POWER] = coefficients[pindex].m;
669 info->b[PSC_POWER] = coefficients[pindex].b;
670 info->R[PSC_POWER] = coefficients[pindex].R;
673 info->m[PSC_TEMPERATURE] = coefficients[tindex].m;
674 info->b[PSC_TEMPERATURE] = coefficients[tindex].b;
675 info->R[PSC_TEMPERATURE] = coefficients[tindex].R;
678 return pmbus_do_probe(client, id, info);
681 static struct i2c_driver adm1275_driver = {
685 .probe = adm1275_probe,
686 .remove = pmbus_do_remove,
687 .id_table = adm1275_id,
690 module_i2c_driver(adm1275_driver);
692 MODULE_AUTHOR("Guenter Roeck");
693 MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
694 MODULE_LICENSE("GPL");