Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / hwmon / pmbus / adm1275.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
4  * and Digital Power Monitor
5  *
6  * Copyright (c) 2011 Ericsson AB.
7  * Copyright (c) 2018 Guenter Roeck
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/bitops.h>
17 #include <linux/bitfield.h>
18 #include <linux/log2.h>
19 #include "pmbus.h"
20
21 enum chips { adm1075, adm1272, adm1275, adm1276, adm1278, adm1293, adm1294 };
22
23 #define ADM1275_MFR_STATUS_IOUT_WARN2   BIT(0)
24 #define ADM1293_MFR_STATUS_VAUX_UV_WARN BIT(5)
25 #define ADM1293_MFR_STATUS_VAUX_OV_WARN BIT(6)
26
27 #define ADM1275_PEAK_IOUT               0xd0
28 #define ADM1275_PEAK_VIN                0xd1
29 #define ADM1275_PEAK_VOUT               0xd2
30 #define ADM1275_PMON_CONFIG             0xd4
31
32 #define ADM1275_VIN_VOUT_SELECT         BIT(6)
33 #define ADM1275_VRANGE                  BIT(5)
34 #define ADM1075_IRANGE_50               BIT(4)
35 #define ADM1075_IRANGE_25               BIT(3)
36 #define ADM1075_IRANGE_MASK             (BIT(3) | BIT(4))
37
38 #define ADM1272_IRANGE                  BIT(0)
39
40 #define ADM1278_TEMP1_EN                BIT(3)
41 #define ADM1278_VIN_EN                  BIT(2)
42 #define ADM1278_VOUT_EN                 BIT(1)
43
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))
49
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))
54
55 #define ADM1293_VAUX_EN                 BIT(1)
56
57 #define ADM1278_PEAK_TEMP               0xd7
58 #define ADM1275_IOUT_WARN2_LIMIT        0xd7
59 #define ADM1275_DEVICE_CONFIG           0xd8
60
61 #define ADM1275_IOUT_WARN2_SELECT       BIT(4)
62
63 #define ADM1276_PEAK_PIN                0xda
64 #define ADM1075_READ_VAUX               0xdd
65 #define ADM1075_VAUX_OV_WARN_LIMIT      0xde
66 #define ADM1075_VAUX_UV_WARN_LIMIT      0xdf
67 #define ADM1293_IOUT_MIN                0xe3
68 #define ADM1293_PIN_MIN                 0xe4
69 #define ADM1075_VAUX_STATUS             0xf6
70
71 #define ADM1075_VAUX_OV_WARN            BIT(7)
72 #define ADM1075_VAUX_UV_WARN            BIT(6)
73
74 #define ADM1275_VI_AVG_SHIFT            0
75 #define ADM1275_VI_AVG_MASK             GENMASK(ADM1275_VI_AVG_SHIFT + 2, \
76                                                 ADM1275_VI_AVG_SHIFT)
77 #define ADM1275_SAMPLES_AVG_MAX         128
78
79 #define ADM1278_PWR_AVG_SHIFT           11
80 #define ADM1278_PWR_AVG_MASK            GENMASK(ADM1278_PWR_AVG_SHIFT + 2, \
81                                                 ADM1278_PWR_AVG_SHIFT)
82 #define ADM1278_VI_AVG_SHIFT            8
83 #define ADM1278_VI_AVG_MASK             GENMASK(ADM1278_VI_AVG_SHIFT + 2, \
84                                                 ADM1278_VI_AVG_SHIFT)
85
86 struct adm1275_data {
87         int id;
88         bool have_oc_fault;
89         bool have_uc_fault;
90         bool have_vout;
91         bool have_vaux_status;
92         bool have_mfr_vaux_status;
93         bool have_iout_min;
94         bool have_pin_min;
95         bool have_pin_max;
96         bool have_temp_max;
97         bool have_power_sampling;
98         struct pmbus_driver_info info;
99 };
100
101 #define to_adm1275_data(x)  container_of(x, struct adm1275_data, info)
102
103 struct coefficients {
104         s16 m;
105         s16 b;
106         s16 R;
107 };
108
109 static const struct coefficients adm1075_coefficients[] = {
110         [0] = { 27169, 0, -1 },         /* voltage */
111         [1] = { 806, 20475, -1 },       /* current, irange25 */
112         [2] = { 404, 20475, -1 },       /* current, irange50 */
113         [3] = { 8549, 0, -1 },          /* power, irange25 */
114         [4] = { 4279, 0, -1 },          /* power, irange50 */
115 };
116
117 static const struct coefficients adm1272_coefficients[] = {
118         [0] = { 6770, 0, -2 },          /* voltage, vrange 60V */
119         [1] = { 4062, 0, -2 },          /* voltage, vrange 100V */
120         [2] = { 1326, 20480, -1 },      /* current, vsense range 15mV */
121         [3] = { 663, 20480, -1 },       /* current, vsense range 30mV */
122         [4] = { 3512, 0, -2 },          /* power, vrange 60V, irange 15mV */
123         [5] = { 21071, 0, -3 },         /* power, vrange 100V, irange 15mV */
124         [6] = { 17561, 0, -3 },         /* power, vrange 60V, irange 30mV */
125         [7] = { 10535, 0, -3 },         /* power, vrange 100V, irange 30mV */
126         [8] = { 42, 31871, -1 },        /* temperature */
127
128 };
129
130 static const struct coefficients adm1275_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 };
135
136 static const struct coefficients adm1276_coefficients[] = {
137         [0] = { 19199, 0, -2 },         /* voltage, vrange set */
138         [1] = { 6720, 0, -1 },          /* voltage, vrange not set */
139         [2] = { 807, 20475, -1 },       /* current */
140         [3] = { 6043, 0, -2 },          /* power, vrange set */
141         [4] = { 2115, 0, -1 },          /* power, vrange not set */
142 };
143
144 static const struct coefficients adm1278_coefficients[] = {
145         [0] = { 19599, 0, -2 },         /* voltage */
146         [1] = { 800, 20475, -1 },       /* current */
147         [2] = { 6123, 0, -2 },          /* power */
148         [3] = { 42, 31880, -1 },        /* temperature */
149 };
150
151 static const struct coefficients adm1293_coefficients[] = {
152         [0] = { 3333, -1, 0 },          /* voltage, vrange 1.2V */
153         [1] = { 5552, -5, -1 },         /* voltage, vrange 7.4V */
154         [2] = { 19604, -50, -2 },       /* voltage, vrange 21V */
155         [3] = { 8000, -100, -2 },       /* current, irange25 */
156         [4] = { 4000, -100, -2 },       /* current, irange50 */
157         [5] = { 20000, -1000, -3 },     /* current, irange100 */
158         [6] = { 10000, -1000, -3 },     /* current, irange200 */
159         [7] = { 10417, 0, -1 },         /* power, 1.2V, irange25 */
160         [8] = { 5208, 0, -1 },          /* power, 1.2V, irange50 */
161         [9] = { 26042, 0, -2 },         /* power, 1.2V, irange100 */
162         [10] = { 13021, 0, -2 },        /* power, 1.2V, irange200 */
163         [11] = { 17351, 0, -2 },        /* power, 7.4V, irange25 */
164         [12] = { 8676, 0, -2 },         /* power, 7.4V, irange50 */
165         [13] = { 4338, 0, -2 },         /* power, 7.4V, irange100 */
166         [14] = { 21689, 0, -3 },        /* power, 7.4V, irange200 */
167         [15] = { 6126, 0, -2 },         /* power, 21V, irange25 */
168         [16] = { 30631, 0, -3 },        /* power, 21V, irange50 */
169         [17] = { 15316, 0, -3 },        /* power, 21V, irange100 */
170         [18] = { 7658, 0, -3 },         /* power, 21V, irange200 */
171 };
172
173 static int adm1275_read_pmon_config(const struct adm1275_data *data,
174                                     struct i2c_client *client, bool is_power)
175 {
176         int shift, ret;
177         u16 mask;
178
179         /*
180          * The PMON configuration register is a 16-bit register only on chips
181          * supporting power average sampling. On other chips it is an 8-bit
182          * register.
183          */
184         if (data->have_power_sampling) {
185                 ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG);
186                 mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK;
187                 shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT;
188         } else {
189                 ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
190                 mask = ADM1275_VI_AVG_MASK;
191                 shift = ADM1275_VI_AVG_SHIFT;
192         }
193         if (ret < 0)
194                 return ret;
195
196         return (ret & mask) >> shift;
197 }
198
199 static int adm1275_write_pmon_config(const struct adm1275_data *data,
200                                      struct i2c_client *client,
201                                      bool is_power, u16 word)
202 {
203         int shift, ret;
204         u16 mask;
205
206         if (data->have_power_sampling) {
207                 ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG);
208                 mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK;
209                 shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT;
210         } else {
211                 ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
212                 mask = ADM1275_VI_AVG_MASK;
213                 shift = ADM1275_VI_AVG_SHIFT;
214         }
215         if (ret < 0)
216                 return ret;
217
218         word = (ret & ~mask) | ((word << shift) & mask);
219         if (data->have_power_sampling)
220                 ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG,
221                                                 word);
222         else
223                 ret = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONFIG,
224                                                 word);
225
226         return ret;
227 }
228
229 static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
230 {
231         const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
232         const struct adm1275_data *data = to_adm1275_data(info);
233         int ret = 0;
234
235         if (page > 0)
236                 return -ENXIO;
237
238         switch (reg) {
239         case PMBUS_IOUT_UC_FAULT_LIMIT:
240                 if (!data->have_uc_fault)
241                         return -ENXIO;
242                 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
243                 break;
244         case PMBUS_IOUT_OC_FAULT_LIMIT:
245                 if (!data->have_oc_fault)
246                         return -ENXIO;
247                 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
248                 break;
249         case PMBUS_VOUT_OV_WARN_LIMIT:
250                 if (data->have_vout)
251                         return -ENODATA;
252                 ret = pmbus_read_word_data(client, 0,
253                                            ADM1075_VAUX_OV_WARN_LIMIT);
254                 break;
255         case PMBUS_VOUT_UV_WARN_LIMIT:
256                 if (data->have_vout)
257                         return -ENODATA;
258                 ret = pmbus_read_word_data(client, 0,
259                                            ADM1075_VAUX_UV_WARN_LIMIT);
260                 break;
261         case PMBUS_READ_VOUT:
262                 if (data->have_vout)
263                         return -ENODATA;
264                 ret = pmbus_read_word_data(client, 0, ADM1075_READ_VAUX);
265                 break;
266         case PMBUS_VIRT_READ_IOUT_MIN:
267                 if (!data->have_iout_min)
268                         return -ENXIO;
269                 ret = pmbus_read_word_data(client, 0, ADM1293_IOUT_MIN);
270                 break;
271         case PMBUS_VIRT_READ_IOUT_MAX:
272                 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
273                 break;
274         case PMBUS_VIRT_READ_VOUT_MAX:
275                 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
276                 break;
277         case PMBUS_VIRT_READ_VIN_MAX:
278                 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
279                 break;
280         case PMBUS_VIRT_READ_PIN_MIN:
281                 if (!data->have_pin_min)
282                         return -ENXIO;
283                 ret = pmbus_read_word_data(client, 0, ADM1293_PIN_MIN);
284                 break;
285         case PMBUS_VIRT_READ_PIN_MAX:
286                 if (!data->have_pin_max)
287                         return -ENXIO;
288                 ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN);
289                 break;
290         case PMBUS_VIRT_READ_TEMP_MAX:
291                 if (!data->have_temp_max)
292                         return -ENXIO;
293                 ret = pmbus_read_word_data(client, 0, ADM1278_PEAK_TEMP);
294                 break;
295         case PMBUS_VIRT_RESET_IOUT_HISTORY:
296         case PMBUS_VIRT_RESET_VOUT_HISTORY:
297         case PMBUS_VIRT_RESET_VIN_HISTORY:
298                 break;
299         case PMBUS_VIRT_RESET_PIN_HISTORY:
300                 if (!data->have_pin_max)
301                         return -ENXIO;
302                 break;
303         case PMBUS_VIRT_RESET_TEMP_HISTORY:
304                 if (!data->have_temp_max)
305                         return -ENXIO;
306                 break;
307         case PMBUS_VIRT_POWER_SAMPLES:
308                 if (!data->have_power_sampling)
309                         return -ENXIO;
310                 ret = adm1275_read_pmon_config(data, client, true);
311                 if (ret < 0)
312                         break;
313                 ret = BIT(ret);
314                 break;
315         case PMBUS_VIRT_IN_SAMPLES:
316         case PMBUS_VIRT_CURR_SAMPLES:
317                 ret = adm1275_read_pmon_config(data, client, false);
318                 if (ret < 0)
319                         break;
320                 ret = BIT(ret);
321                 break;
322         default:
323                 ret = -ENODATA;
324                 break;
325         }
326         return ret;
327 }
328
329 static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
330                                    u16 word)
331 {
332         const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
333         const struct adm1275_data *data = to_adm1275_data(info);
334         int ret;
335
336         if (page > 0)
337                 return -ENXIO;
338
339         switch (reg) {
340         case PMBUS_IOUT_UC_FAULT_LIMIT:
341         case PMBUS_IOUT_OC_FAULT_LIMIT:
342                 ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
343                                             word);
344                 break;
345         case PMBUS_VIRT_RESET_IOUT_HISTORY:
346                 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
347                 if (!ret && data->have_iout_min)
348                         ret = pmbus_write_word_data(client, 0,
349                                                     ADM1293_IOUT_MIN, 0);
350                 break;
351         case PMBUS_VIRT_RESET_VOUT_HISTORY:
352                 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
353                 break;
354         case PMBUS_VIRT_RESET_VIN_HISTORY:
355                 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
356                 break;
357         case PMBUS_VIRT_RESET_PIN_HISTORY:
358                 ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0);
359                 if (!ret && data->have_pin_min)
360                         ret = pmbus_write_word_data(client, 0,
361                                                     ADM1293_PIN_MIN, 0);
362                 break;
363         case PMBUS_VIRT_RESET_TEMP_HISTORY:
364                 ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0);
365                 break;
366         case PMBUS_VIRT_POWER_SAMPLES:
367                 if (!data->have_power_sampling)
368                         return -ENXIO;
369                 word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
370                 ret = adm1275_write_pmon_config(data, client, true,
371                                                 ilog2(word));
372                 break;
373         case PMBUS_VIRT_IN_SAMPLES:
374         case PMBUS_VIRT_CURR_SAMPLES:
375                 word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
376                 ret = adm1275_write_pmon_config(data, client, false,
377                                                 ilog2(word));
378                 break;
379         default:
380                 ret = -ENODATA;
381                 break;
382         }
383         return ret;
384 }
385
386 static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
387 {
388         const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
389         const struct adm1275_data *data = to_adm1275_data(info);
390         int mfr_status, ret;
391
392         if (page > 0)
393                 return -ENXIO;
394
395         switch (reg) {
396         case PMBUS_STATUS_IOUT:
397                 ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
398                 if (ret < 0)
399                         break;
400                 if (!data->have_oc_fault && !data->have_uc_fault)
401                         break;
402                 mfr_status = pmbus_read_byte_data(client, page,
403                                                   PMBUS_STATUS_MFR_SPECIFIC);
404                 if (mfr_status < 0)
405                         return mfr_status;
406                 if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
407                         ret |= data->have_oc_fault ?
408                           PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
409                 }
410                 break;
411         case PMBUS_STATUS_VOUT:
412                 if (data->have_vout)
413                         return -ENODATA;
414                 ret = 0;
415                 if (data->have_vaux_status) {
416                         mfr_status = pmbus_read_byte_data(client, 0,
417                                                           ADM1075_VAUX_STATUS);
418                         if (mfr_status < 0)
419                                 return mfr_status;
420                         if (mfr_status & ADM1075_VAUX_OV_WARN)
421                                 ret |= PB_VOLTAGE_OV_WARNING;
422                         if (mfr_status & ADM1075_VAUX_UV_WARN)
423                                 ret |= PB_VOLTAGE_UV_WARNING;
424                 } else if (data->have_mfr_vaux_status) {
425                         mfr_status = pmbus_read_byte_data(client, page,
426                                                 PMBUS_STATUS_MFR_SPECIFIC);
427                         if (mfr_status < 0)
428                                 return mfr_status;
429                         if (mfr_status & ADM1293_MFR_STATUS_VAUX_OV_WARN)
430                                 ret |= PB_VOLTAGE_OV_WARNING;
431                         if (mfr_status & ADM1293_MFR_STATUS_VAUX_UV_WARN)
432                                 ret |= PB_VOLTAGE_UV_WARNING;
433                 }
434                 break;
435         default:
436                 ret = -ENODATA;
437                 break;
438         }
439         return ret;
440 }
441
442 static const struct i2c_device_id adm1275_id[] = {
443         { "adm1075", adm1075 },
444         { "adm1272", adm1272 },
445         { "adm1275", adm1275 },
446         { "adm1276", adm1276 },
447         { "adm1278", adm1278 },
448         { "adm1293", adm1293 },
449         { "adm1294", adm1294 },
450         { }
451 };
452 MODULE_DEVICE_TABLE(i2c, adm1275_id);
453
454 static int adm1275_probe(struct i2c_client *client,
455                          const struct i2c_device_id *id)
456 {
457         u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
458         int config, device_config;
459         int ret;
460         struct pmbus_driver_info *info;
461         struct adm1275_data *data;
462         const struct i2c_device_id *mid;
463         const struct coefficients *coefficients;
464         int vindex = -1, voindex = -1, cindex = -1, pindex = -1;
465         int tindex = -1;
466         u32 shunt;
467
468         if (!i2c_check_functionality(client->adapter,
469                                      I2C_FUNC_SMBUS_READ_BYTE_DATA
470                                      | I2C_FUNC_SMBUS_BLOCK_DATA))
471                 return -ENODEV;
472
473         ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
474         if (ret < 0) {
475                 dev_err(&client->dev, "Failed to read Manufacturer ID\n");
476                 return ret;
477         }
478         if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
479                 dev_err(&client->dev, "Unsupported Manufacturer ID\n");
480                 return -ENODEV;
481         }
482
483         ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
484         if (ret < 0) {
485                 dev_err(&client->dev, "Failed to read Manufacturer Model\n");
486                 return ret;
487         }
488         for (mid = adm1275_id; mid->name[0]; mid++) {
489                 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
490                         break;
491         }
492         if (!mid->name[0]) {
493                 dev_err(&client->dev, "Unsupported device\n");
494                 return -ENODEV;
495         }
496
497         if (id->driver_data != mid->driver_data)
498                 dev_notice(&client->dev,
499                            "Device mismatch: Configured %s, detected %s\n",
500                            id->name, mid->name);
501
502         config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
503         if (config < 0)
504                 return config;
505
506         device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG);
507         if (device_config < 0)
508                 return device_config;
509
510         data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
511                             GFP_KERNEL);
512         if (!data)
513                 return -ENOMEM;
514
515         if (of_property_read_u32(client->dev.of_node,
516                                  "shunt-resistor-micro-ohms", &shunt))
517                 shunt = 1000; /* 1 mOhm if not set via DT */
518
519         if (shunt == 0)
520                 return -EINVAL;
521
522         data->id = mid->driver_data;
523
524         info = &data->info;
525
526         info->pages = 1;
527         info->format[PSC_VOLTAGE_IN] = direct;
528         info->format[PSC_VOLTAGE_OUT] = direct;
529         info->format[PSC_CURRENT_OUT] = direct;
530         info->format[PSC_POWER] = direct;
531         info->format[PSC_TEMPERATURE] = direct;
532         info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
533                         PMBUS_HAVE_SAMPLES;
534
535         info->read_word_data = adm1275_read_word_data;
536         info->read_byte_data = adm1275_read_byte_data;
537         info->write_word_data = adm1275_write_word_data;
538
539         switch (data->id) {
540         case adm1075:
541                 if (device_config & ADM1275_IOUT_WARN2_SELECT)
542                         data->have_oc_fault = true;
543                 else
544                         data->have_uc_fault = true;
545                 data->have_pin_max = true;
546                 data->have_vaux_status = true;
547
548                 coefficients = adm1075_coefficients;
549                 vindex = 0;
550                 switch (config & ADM1075_IRANGE_MASK) {
551                 case ADM1075_IRANGE_25:
552                         cindex = 1;
553                         pindex = 3;
554                         break;
555                 case ADM1075_IRANGE_50:
556                         cindex = 2;
557                         pindex = 4;
558                         break;
559                 default:
560                         dev_err(&client->dev, "Invalid input current range");
561                         break;
562                 }
563
564                 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
565                   | PMBUS_HAVE_STATUS_INPUT;
566                 if (config & ADM1275_VIN_VOUT_SELECT)
567                         info->func[0] |=
568                           PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
569                 break;
570         case adm1272:
571                 data->have_vout = true;
572                 data->have_pin_max = true;
573                 data->have_temp_max = true;
574                 data->have_power_sampling = true;
575
576                 coefficients = adm1272_coefficients;
577                 vindex = (config & ADM1275_VRANGE) ? 1 : 0;
578                 cindex = (config & ADM1272_IRANGE) ? 3 : 2;
579                 /* pindex depends on the combination of the above */
580                 switch (config & (ADM1275_VRANGE | ADM1272_IRANGE)) {
581                 case 0:
582                 default:
583                         pindex = 4;
584                         break;
585                 case ADM1275_VRANGE:
586                         pindex = 5;
587                         break;
588                 case ADM1272_IRANGE:
589                         pindex = 6;
590                         break;
591                 case ADM1275_VRANGE | ADM1272_IRANGE:
592                         pindex = 7;
593                         break;
594                 }
595                 tindex = 8;
596
597                 info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
598                         PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
599
600                 /* Enable VOUT if not enabled (it is disabled by default) */
601                 if (!(config & ADM1278_VOUT_EN)) {
602                         config |= ADM1278_VOUT_EN;
603                         ret = i2c_smbus_write_byte_data(client,
604                                                         ADM1275_PMON_CONFIG,
605                                                         config);
606                         if (ret < 0) {
607                                 dev_err(&client->dev,
608                                         "Failed to enable VOUT monitoring\n");
609                                 return -ENODEV;
610                         }
611                 }
612
613                 if (config & ADM1278_TEMP1_EN)
614                         info->func[0] |=
615                                 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
616                 if (config & ADM1278_VIN_EN)
617                         info->func[0] |= PMBUS_HAVE_VIN;
618                 break;
619         case adm1275:
620                 if (device_config & ADM1275_IOUT_WARN2_SELECT)
621                         data->have_oc_fault = true;
622                 else
623                         data->have_uc_fault = true;
624                 data->have_vout = true;
625
626                 coefficients = adm1275_coefficients;
627                 vindex = (config & ADM1275_VRANGE) ? 0 : 1;
628                 cindex = 2;
629
630                 if (config & ADM1275_VIN_VOUT_SELECT)
631                         info->func[0] |=
632                           PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
633                 else
634                         info->func[0] |=
635                           PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
636                 break;
637         case adm1276:
638                 if (device_config & ADM1275_IOUT_WARN2_SELECT)
639                         data->have_oc_fault = true;
640                 else
641                         data->have_uc_fault = true;
642                 data->have_vout = true;
643                 data->have_pin_max = true;
644
645                 coefficients = adm1276_coefficients;
646                 vindex = (config & ADM1275_VRANGE) ? 0 : 1;
647                 cindex = 2;
648                 pindex = (config & ADM1275_VRANGE) ? 3 : 4;
649
650                 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
651                   | PMBUS_HAVE_STATUS_INPUT;
652                 if (config & ADM1275_VIN_VOUT_SELECT)
653                         info->func[0] |=
654                           PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
655                 break;
656         case adm1278:
657                 data->have_vout = true;
658                 data->have_pin_max = true;
659                 data->have_temp_max = true;
660                 data->have_power_sampling = true;
661
662                 coefficients = adm1278_coefficients;
663                 vindex = 0;
664                 cindex = 1;
665                 pindex = 2;
666                 tindex = 3;
667
668                 info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
669                         PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
670
671                 /* Enable VOUT if not enabled (it is disabled by default) */
672                 if (!(config & ADM1278_VOUT_EN)) {
673                         config |= ADM1278_VOUT_EN;
674                         ret = i2c_smbus_write_byte_data(client,
675                                                         ADM1275_PMON_CONFIG,
676                                                         config);
677                         if (ret < 0) {
678                                 dev_err(&client->dev,
679                                         "Failed to enable VOUT monitoring\n");
680                                 return -ENODEV;
681                         }
682                 }
683
684                 if (config & ADM1278_TEMP1_EN)
685                         info->func[0] |=
686                                 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
687                 if (config & ADM1278_VIN_EN)
688                         info->func[0] |= PMBUS_HAVE_VIN;
689                 break;
690         case adm1293:
691         case adm1294:
692                 data->have_iout_min = true;
693                 data->have_pin_min = true;
694                 data->have_pin_max = true;
695                 data->have_mfr_vaux_status = true;
696                 data->have_power_sampling = true;
697
698                 coefficients = adm1293_coefficients;
699
700                 voindex = 0;
701                 switch (config & ADM1293_VIN_SEL_MASK) {
702                 case ADM1293_VIN_SEL_012:       /* 1.2V */
703                         vindex = 0;
704                         break;
705                 case ADM1293_VIN_SEL_074:       /* 7.4V */
706                         vindex = 1;
707                         break;
708                 case ADM1293_VIN_SEL_210:       /* 21V */
709                         vindex = 2;
710                         break;
711                 default:                        /* disabled */
712                         break;
713                 }
714
715                 switch (config & ADM1293_IRANGE_MASK) {
716                 case ADM1293_IRANGE_25:
717                         cindex = 3;
718                         break;
719                 case ADM1293_IRANGE_50:
720                         cindex = 4;
721                         break;
722                 case ADM1293_IRANGE_100:
723                         cindex = 5;
724                         break;
725                 case ADM1293_IRANGE_200:
726                         cindex = 6;
727                         break;
728                 }
729
730                 if (vindex >= 0)
731                         pindex = 7 + vindex * 4 + (cindex - 3);
732
733                 if (config & ADM1293_VAUX_EN)
734                         info->func[0] |=
735                                 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
736
737                 info->func[0] |= PMBUS_HAVE_PIN |
738                         PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
739
740                 break;
741         default:
742                 dev_err(&client->dev, "Unsupported device\n");
743                 return -ENODEV;
744         }
745
746         if (voindex < 0)
747                 voindex = vindex;
748         if (vindex >= 0) {
749                 info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m;
750                 info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b;
751                 info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R;
752         }
753         if (voindex >= 0) {
754                 info->m[PSC_VOLTAGE_OUT] = coefficients[voindex].m;
755                 info->b[PSC_VOLTAGE_OUT] = coefficients[voindex].b;
756                 info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R;
757         }
758         if (cindex >= 0) {
759                 /* Scale current with sense resistor value */
760                 info->m[PSC_CURRENT_OUT] =
761                         coefficients[cindex].m * shunt / 1000;
762                 info->b[PSC_CURRENT_OUT] = coefficients[cindex].b;
763                 info->R[PSC_CURRENT_OUT] = coefficients[cindex].R;
764         }
765         if (pindex >= 0) {
766                 info->m[PSC_POWER] =
767                         coefficients[pindex].m * shunt / 1000;
768                 info->b[PSC_POWER] = coefficients[pindex].b;
769                 info->R[PSC_POWER] = coefficients[pindex].R;
770         }
771         if (tindex >= 0) {
772                 info->m[PSC_TEMPERATURE] = coefficients[tindex].m;
773                 info->b[PSC_TEMPERATURE] = coefficients[tindex].b;
774                 info->R[PSC_TEMPERATURE] = coefficients[tindex].R;
775         }
776
777         return pmbus_do_probe(client, id, info);
778 }
779
780 static struct i2c_driver adm1275_driver = {
781         .driver = {
782                    .name = "adm1275",
783                    },
784         .probe = adm1275_probe,
785         .remove = pmbus_do_remove,
786         .id_table = adm1275_id,
787 };
788
789 module_i2c_driver(adm1275_driver);
790
791 MODULE_AUTHOR("Guenter Roeck");
792 MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
793 MODULE_LICENSE("GPL");