These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / hwmon / pmbus / adm1275.c
1 /*
2  * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
3  * and Digital Power Monitor
4  *
5  * Copyright (c) 2011 Ericsson AB.
6  *
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.
11  *
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.
16  */
17
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>
25 #include "pmbus.h"
26
27 enum chips { adm1075, adm1275, adm1276, adm1293, adm1294 };
28
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)
32
33 #define ADM1275_PEAK_IOUT               0xd0
34 #define ADM1275_PEAK_VIN                0xd1
35 #define ADM1275_PEAK_VOUT               0xd2
36 #define ADM1275_PMON_CONFIG             0xd4
37
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))
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 ADM1275_IOUT_WARN2_LIMIT        0xd7
58 #define ADM1275_DEVICE_CONFIG           0xd8
59
60 #define ADM1275_IOUT_WARN2_SELECT       BIT(4)
61
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
69
70 #define ADM1075_VAUX_OV_WARN            BIT(7)
71 #define ADM1075_VAUX_UV_WARN            BIT(6)
72
73 struct adm1275_data {
74         int id;
75         bool have_oc_fault;
76         bool have_uc_fault;
77         bool have_vout;
78         bool have_vaux_status;
79         bool have_mfr_vaux_status;
80         bool have_iout_min;
81         bool have_pin_min;
82         bool have_pin_max;
83         struct pmbus_driver_info info;
84 };
85
86 #define to_adm1275_data(x)  container_of(x, struct adm1275_data, info)
87
88 struct coefficients {
89         s16 m;
90         s16 b;
91         s16 R;
92 };
93
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 */
100 };
101
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 */
106 };
107
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 */
114 };
115
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 */
136 };
137
138 static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
139 {
140         const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
141         const struct adm1275_data *data = to_adm1275_data(info);
142         int ret = 0;
143
144         if (page)
145                 return -ENXIO;
146
147         switch (reg) {
148         case PMBUS_IOUT_UC_FAULT_LIMIT:
149                 if (!data->have_uc_fault)
150                         return -ENXIO;
151                 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
152                 break;
153         case PMBUS_IOUT_OC_FAULT_LIMIT:
154                 if (!data->have_oc_fault)
155                         return -ENXIO;
156                 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
157                 break;
158         case PMBUS_VOUT_OV_WARN_LIMIT:
159                 if (data->have_vout)
160                         return -ENODATA;
161                 ret = pmbus_read_word_data(client, 0,
162                                            ADM1075_VAUX_OV_WARN_LIMIT);
163                 break;
164         case PMBUS_VOUT_UV_WARN_LIMIT:
165                 if (data->have_vout)
166                         return -ENODATA;
167                 ret = pmbus_read_word_data(client, 0,
168                                            ADM1075_VAUX_UV_WARN_LIMIT);
169                 break;
170         case PMBUS_READ_VOUT:
171                 if (data->have_vout)
172                         return -ENODATA;
173                 ret = pmbus_read_word_data(client, 0, ADM1075_READ_VAUX);
174                 break;
175         case PMBUS_VIRT_READ_IOUT_MIN:
176                 if (!data->have_iout_min)
177                         return -ENXIO;
178                 ret = pmbus_read_word_data(client, 0, ADM1293_IOUT_MIN);
179                 break;
180         case PMBUS_VIRT_READ_IOUT_MAX:
181                 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
182                 break;
183         case PMBUS_VIRT_READ_VOUT_MAX:
184                 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
185                 break;
186         case PMBUS_VIRT_READ_VIN_MAX:
187                 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
188                 break;
189         case PMBUS_VIRT_READ_PIN_MIN:
190                 if (!data->have_pin_min)
191                         return -ENXIO;
192                 ret = pmbus_read_word_data(client, 0, ADM1293_PIN_MIN);
193                 break;
194         case PMBUS_VIRT_READ_PIN_MAX:
195                 if (!data->have_pin_max)
196                         return -ENXIO;
197                 ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN);
198                 break;
199         case PMBUS_VIRT_RESET_IOUT_HISTORY:
200         case PMBUS_VIRT_RESET_VOUT_HISTORY:
201         case PMBUS_VIRT_RESET_VIN_HISTORY:
202                 break;
203         case PMBUS_VIRT_RESET_PIN_HISTORY:
204                 if (!data->have_pin_max)
205                         return -ENXIO;
206                 break;
207         default:
208                 ret = -ENODATA;
209                 break;
210         }
211         return ret;
212 }
213
214 static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
215                                    u16 word)
216 {
217         const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
218         const struct adm1275_data *data = to_adm1275_data(info);
219         int ret;
220
221         if (page)
222                 return -ENXIO;
223
224         switch (reg) {
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,
228                                             word);
229                 break;
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);
235                 break;
236         case PMBUS_VIRT_RESET_VOUT_HISTORY:
237                 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
238                 break;
239         case PMBUS_VIRT_RESET_VIN_HISTORY:
240                 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
241                 break;
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,
246                                                     ADM1293_PIN_MIN, 0);
247                 break;
248         default:
249                 ret = -ENODATA;
250                 break;
251         }
252         return ret;
253 }
254
255 static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
256 {
257         const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
258         const struct adm1275_data *data = to_adm1275_data(info);
259         int mfr_status, ret;
260
261         if (page > 0)
262                 return -ENXIO;
263
264         switch (reg) {
265         case PMBUS_STATUS_IOUT:
266                 ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
267                 if (ret < 0)
268                         break;
269                 if (!data->have_oc_fault && !data->have_uc_fault)
270                         break;
271                 mfr_status = pmbus_read_byte_data(client, page,
272                                                   PMBUS_STATUS_MFR_SPECIFIC);
273                 if (mfr_status < 0)
274                         return mfr_status;
275                 if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
276                         ret |= data->have_oc_fault ?
277                           PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
278                 }
279                 break;
280         case PMBUS_STATUS_VOUT:
281                 if (data->have_vout)
282                         return -ENODATA;
283                 ret = 0;
284                 if (data->have_vaux_status) {
285                         mfr_status = pmbus_read_byte_data(client, 0,
286                                                           ADM1075_VAUX_STATUS);
287                         if (mfr_status < 0)
288                                 return mfr_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);
296                         if (mfr_status < 0)
297                                 return mfr_status;
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;
302                 }
303                 break;
304         default:
305                 ret = -ENODATA;
306                 break;
307         }
308         return ret;
309 }
310
311 static const struct i2c_device_id adm1275_id[] = {
312         { "adm1075", adm1075 },
313         { "adm1275", adm1275 },
314         { "adm1276", adm1276 },
315         { "adm1293", adm1293 },
316         { "adm1294", adm1294 },
317         { }
318 };
319 MODULE_DEVICE_TABLE(i2c, adm1275_id);
320
321 static int adm1275_probe(struct i2c_client *client,
322                          const struct i2c_device_id *id)
323 {
324         u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
325         int config, device_config;
326         int ret;
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;
332
333         if (!i2c_check_functionality(client->adapter,
334                                      I2C_FUNC_SMBUS_READ_BYTE_DATA
335                                      | I2C_FUNC_SMBUS_BLOCK_DATA))
336                 return -ENODEV;
337
338         ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
339         if (ret < 0) {
340                 dev_err(&client->dev, "Failed to read Manufacturer ID\n");
341                 return ret;
342         }
343         if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
344                 dev_err(&client->dev, "Unsupported Manufacturer ID\n");
345                 return -ENODEV;
346         }
347
348         ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
349         if (ret < 0) {
350                 dev_err(&client->dev, "Failed to read Manufacturer Model\n");
351                 return ret;
352         }
353         for (mid = adm1275_id; mid->name[0]; mid++) {
354                 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
355                         break;
356         }
357         if (!mid->name[0]) {
358                 dev_err(&client->dev, "Unsupported device\n");
359                 return -ENODEV;
360         }
361
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);
366
367         config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
368         if (config < 0)
369                 return config;
370
371         device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG);
372         if (device_config < 0)
373                 return device_config;
374
375         data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
376                             GFP_KERNEL);
377         if (!data)
378                 return -ENOMEM;
379
380         data->id = mid->driver_data;
381
382         info = &data->info;
383
384         info->pages = 1;
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;
390
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;
394
395         switch (data->id) {
396         case adm1075:
397                 if (device_config & ADM1275_IOUT_WARN2_SELECT)
398                         data->have_oc_fault = true;
399                 else
400                         data->have_uc_fault = true;
401                 data->have_pin_max = true;
402                 data->have_vaux_status = true;
403
404                 coefficients = adm1075_coefficients;
405                 vindex = 0;
406                 switch (config & ADM1075_IRANGE_MASK) {
407                 case ADM1075_IRANGE_25:
408                         cindex = 1;
409                         pindex = 3;
410                         break;
411                 case ADM1075_IRANGE_50:
412                         cindex = 2;
413                         pindex = 4;
414                         break;
415                 default:
416                         dev_err(&client->dev, "Invalid input current range");
417                         break;
418                 }
419
420                 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
421                   | PMBUS_HAVE_STATUS_INPUT;
422                 if (config & ADM1275_VIN_VOUT_SELECT)
423                         info->func[0] |=
424                           PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
425                 break;
426         case adm1275:
427                 if (device_config & ADM1275_IOUT_WARN2_SELECT)
428                         data->have_oc_fault = true;
429                 else
430                         data->have_uc_fault = true;
431                 data->have_vout = true;
432
433                 coefficients = adm1275_coefficients;
434                 vindex = (config & ADM1275_VRANGE) ? 0 : 1;
435                 cindex = 2;
436
437                 if (config & ADM1275_VIN_VOUT_SELECT)
438                         info->func[0] |=
439                           PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
440                 else
441                         info->func[0] |=
442                           PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
443                 break;
444         case adm1276:
445                 if (device_config & ADM1275_IOUT_WARN2_SELECT)
446                         data->have_oc_fault = true;
447                 else
448                         data->have_uc_fault = true;
449                 data->have_vout = true;
450                 data->have_pin_max = true;
451
452                 coefficients = adm1276_coefficients;
453                 vindex = (config & ADM1275_VRANGE) ? 0 : 1;
454                 cindex = 2;
455                 pindex = (config & ADM1275_VRANGE) ? 3 : 4;
456
457                 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
458                   | PMBUS_HAVE_STATUS_INPUT;
459                 if (config & ADM1275_VIN_VOUT_SELECT)
460                         info->func[0] |=
461                           PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
462                 break;
463         case adm1293:
464         case adm1294:
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;
469
470                 coefficients = adm1293_coefficients;
471
472                 voindex = 0;
473                 switch (config & ADM1293_VIN_SEL_MASK) {
474                 case ADM1293_VIN_SEL_012:       /* 1.2V */
475                         vindex = 0;
476                         break;
477                 case ADM1293_VIN_SEL_074:       /* 7.4V */
478                         vindex = 1;
479                         break;
480                 case ADM1293_VIN_SEL_210:       /* 21V */
481                         vindex = 2;
482                         break;
483                 default:                        /* disabled */
484                         break;
485                 }
486
487                 switch (config & ADM1293_IRANGE_MASK) {
488                 case ADM1293_IRANGE_25:
489                         cindex = 3;
490                         break;
491                 case ADM1293_IRANGE_50:
492                         cindex = 4;
493                         break;
494                 case ADM1293_IRANGE_100:
495                         cindex = 5;
496                         break;
497                 case ADM1293_IRANGE_200:
498                         cindex = 6;
499                         break;
500                 }
501
502                 if (vindex >= 0)
503                         pindex = 7 + vindex * 4 + (cindex - 3);
504
505                 if (config & ADM1293_VAUX_EN)
506                         info->func[0] |=
507                                 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
508
509                 info->func[0] |= PMBUS_HAVE_PIN |
510                         PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
511
512                 break;
513         default:
514                 dev_err(&client->dev, "Unsupported device\n");
515                 return -ENODEV;
516         }
517
518         if (voindex < 0)
519                 voindex = vindex;
520         if (vindex >= 0) {
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;
524         }
525         if (voindex >= 0) {
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;
529         }
530         if (cindex >= 0) {
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;
534         }
535         if (pindex >= 0) {
536                 info->m[PSC_POWER] = coefficients[pindex].m;
537                 info->b[PSC_POWER] = coefficients[pindex].b;
538                 info->R[PSC_POWER] = coefficients[pindex].R;
539         }
540
541         return pmbus_do_probe(client, id, info);
542 }
543
544 static struct i2c_driver adm1275_driver = {
545         .driver = {
546                    .name = "adm1275",
547                    },
548         .probe = adm1275_probe,
549         .remove = pmbus_do_remove,
550         .id_table = adm1275_id,
551 };
552
553 module_i2c_driver(adm1275_driver);
554
555 MODULE_AUTHOR("Guenter Roeck");
556 MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
557 MODULE_LICENSE("GPL");