GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / hwmon / pmbus / lm25066.c
1 /*
2  * Hardware monitoring driver for LM25056 / LM25063 / LM25066 / LM5064 / LM5066
3  *
4  * Copyright (c) 2011 Ericsson AB.
5  * Copyright (c) 2013 Guenter Roeck
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  * 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.
20  */
21
22 #include <linux/bitops.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include "pmbus.h"
30
31 enum chips { lm25056, lm25063, lm25066, lm5064, lm5066 };
32
33 #define LM25066_READ_VAUX               0xd0
34 #define LM25066_MFR_READ_IIN            0xd1
35 #define LM25066_MFR_READ_PIN            0xd2
36 #define LM25066_MFR_IIN_OC_WARN_LIMIT   0xd3
37 #define LM25066_MFR_PIN_OP_WARN_LIMIT   0xd4
38 #define LM25066_READ_PIN_PEAK           0xd5
39 #define LM25066_CLEAR_PIN_PEAK          0xd6
40 #define LM25066_DEVICE_SETUP            0xd9
41 #define LM25066_READ_AVG_VIN            0xdc
42 #define LM25066_READ_AVG_VOUT           0xdd
43 #define LM25066_READ_AVG_IIN            0xde
44 #define LM25066_READ_AVG_PIN            0xdf
45
46 #define LM25066_DEV_SETUP_CL            BIT(4)  /* Current limit */
47
48 /* LM25056 only */
49
50 #define LM25056_VAUX_OV_WARN_LIMIT      0xe3
51 #define LM25056_VAUX_UV_WARN_LIMIT      0xe4
52
53 #define LM25056_MFR_STS_VAUX_OV_WARN    BIT(1)
54 #define LM25056_MFR_STS_VAUX_UV_WARN    BIT(0)
55
56 /* LM25063 only */
57
58 #define LM25063_READ_VOUT_MAX           0xe5
59 #define LM25063_READ_VOUT_MIN           0xe6
60
61 struct __coeff {
62         short m, b, R;
63 };
64
65 #define PSC_CURRENT_IN_L        (PSC_NUM_CLASSES)
66 #define PSC_POWER_L             (PSC_NUM_CLASSES + 1)
67
68 static struct __coeff lm25066_coeff[5][PSC_NUM_CLASSES + 2] = {
69         [lm25056] = {
70                 [PSC_VOLTAGE_IN] = {
71                         .m = 16296,
72                         .b = 1343,
73                         .R = -2,
74                 },
75                 [PSC_CURRENT_IN] = {
76                         .m = 13797,
77                         .b = -1833,
78                         .R = -2,
79                 },
80                 [PSC_CURRENT_IN_L] = {
81                         .m = 6726,
82                         .b = -537,
83                         .R = -2,
84                 },
85                 [PSC_POWER] = {
86                         .m = 5501,
87                         .b = -2908,
88                         .R = -3,
89                 },
90                 [PSC_POWER_L] = {
91                         .m = 26882,
92                         .b = -5646,
93                         .R = -4,
94                 },
95                 [PSC_TEMPERATURE] = {
96                         .m = 1580,
97                         .b = -14500,
98                         .R = -2,
99                 },
100         },
101         [lm25066] = {
102                 [PSC_VOLTAGE_IN] = {
103                         .m = 22070,
104                         .b = -1800,
105                         .R = -2,
106                 },
107                 [PSC_VOLTAGE_OUT] = {
108                         .m = 22070,
109                         .b = -1800,
110                         .R = -2,
111                 },
112                 [PSC_CURRENT_IN] = {
113                         .m = 13661,
114                         .b = -5200,
115                         .R = -2,
116                 },
117                 [PSC_CURRENT_IN_L] = {
118                         .m = 6852,
119                         .b = -3100,
120                         .R = -2,
121                 },
122                 [PSC_POWER] = {
123                         .m = 736,
124                         .b = -3300,
125                         .R = -2,
126                 },
127                 [PSC_POWER_L] = {
128                         .m = 369,
129                         .b = -1900,
130                         .R = -2,
131                 },
132                 [PSC_TEMPERATURE] = {
133                         .m = 16,
134                 },
135         },
136         [lm25063] = {
137                 [PSC_VOLTAGE_IN] = {
138                         .m = 16000,
139                         .R = -2,
140                 },
141                 [PSC_VOLTAGE_OUT] = {
142                         .m = 16000,
143                         .R = -2,
144                 },
145                 [PSC_CURRENT_IN] = {
146                         .m = 10000,
147                         .R = -2,
148                 },
149                 [PSC_CURRENT_IN_L] = {
150                         .m = 10000,
151                         .R = -2,
152                 },
153                 [PSC_POWER] = {
154                         .m = 5000,
155                         .R = -3,
156                 },
157                 [PSC_POWER_L] = {
158                         .m = 5000,
159                         .R = -3,
160                 },
161                 [PSC_TEMPERATURE] = {
162                         .m = 15596,
163                         .R = -3,
164                 },
165         },
166         [lm5064] = {
167                 [PSC_VOLTAGE_IN] = {
168                         .m = 4611,
169                         .b = -642,
170                         .R = -2,
171                 },
172                 [PSC_VOLTAGE_OUT] = {
173                         .m = 4621,
174                         .b = 423,
175                         .R = -2,
176                 },
177                 [PSC_CURRENT_IN] = {
178                         .m = 10742,
179                         .b = 1552,
180                         .R = -2,
181                 },
182                 [PSC_CURRENT_IN_L] = {
183                         .m = 5456,
184                         .b = 2118,
185                         .R = -2,
186                 },
187                 [PSC_POWER] = {
188                         .m = 1204,
189                         .b = 8524,
190                         .R = -3,
191                 },
192                 [PSC_POWER_L] = {
193                         .m = 612,
194                         .b = 11202,
195                         .R = -3,
196                 },
197                 [PSC_TEMPERATURE] = {
198                         .m = 16,
199                 },
200         },
201         [lm5066] = {
202                 [PSC_VOLTAGE_IN] = {
203                         .m = 4587,
204                         .b = -1200,
205                         .R = -2,
206                 },
207                 [PSC_VOLTAGE_OUT] = {
208                         .m = 4587,
209                         .b = -2400,
210                         .R = -2,
211                 },
212                 [PSC_CURRENT_IN] = {
213                         .m = 10753,
214                         .b = -1200,
215                         .R = -2,
216                 },
217                 [PSC_CURRENT_IN_L] = {
218                         .m = 5405,
219                         .b = -600,
220                         .R = -2,
221                 },
222                 [PSC_POWER] = {
223                         .m = 1204,
224                         .b = -6000,
225                         .R = -3,
226                 },
227                 [PSC_POWER_L] = {
228                         .m = 605,
229                         .b = -8000,
230                         .R = -3,
231                 },
232                 [PSC_TEMPERATURE] = {
233                         .m = 16,
234                 },
235         },
236 };
237
238 struct lm25066_data {
239         int id;
240         u16 rlimit;                     /* Maximum register value */
241         struct pmbus_driver_info info;
242 };
243
244 #define to_lm25066_data(x)  container_of(x, struct lm25066_data, info)
245
246 static int lm25066_read_word_data(struct i2c_client *client, int page, int reg)
247 {
248         const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
249         const struct lm25066_data *data = to_lm25066_data(info);
250         int ret;
251
252         switch (reg) {
253         case PMBUS_VIRT_READ_VMON:
254                 ret = pmbus_read_word_data(client, 0, LM25066_READ_VAUX);
255                 if (ret < 0)
256                         break;
257                 /* Adjust returned value to match VIN coefficients */
258                 switch (data->id) {
259                 case lm25056:
260                         /* VIN: 6.14 mV VAUX: 293 uV LSB */
261                         ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
262                         break;
263                 case lm25063:
264                         /* VIN: 6.25 mV VAUX: 200.0 uV LSB */
265                         ret = DIV_ROUND_CLOSEST(ret * 20, 625);
266                         break;
267                 case lm25066:
268                         /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
269                         ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
270                         break;
271                 case lm5064:
272                         /* VIN: 4.53 mV VAUX: 700 uV LSB */
273                         ret = DIV_ROUND_CLOSEST(ret * 70, 453);
274                         break;
275                 case lm5066:
276                         /* VIN: 2.18 mV VAUX: 725 uV LSB */
277                         ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
278                         break;
279                 }
280                 break;
281         case PMBUS_READ_IIN:
282                 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
283                 break;
284         case PMBUS_READ_PIN:
285                 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
286                 break;
287         case PMBUS_IIN_OC_WARN_LIMIT:
288                 ret = pmbus_read_word_data(client, 0,
289                                            LM25066_MFR_IIN_OC_WARN_LIMIT);
290                 break;
291         case PMBUS_PIN_OP_WARN_LIMIT:
292                 ret = pmbus_read_word_data(client, 0,
293                                            LM25066_MFR_PIN_OP_WARN_LIMIT);
294                 break;
295         case PMBUS_VIRT_READ_VIN_AVG:
296                 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
297                 break;
298         case PMBUS_VIRT_READ_VOUT_AVG:
299                 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
300                 break;
301         case PMBUS_VIRT_READ_IIN_AVG:
302                 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
303                 break;
304         case PMBUS_VIRT_READ_PIN_AVG:
305                 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
306                 break;
307         case PMBUS_VIRT_READ_PIN_MAX:
308                 ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
309                 break;
310         case PMBUS_VIRT_RESET_PIN_HISTORY:
311                 ret = 0;
312                 break;
313         default:
314                 ret = -ENODATA;
315                 break;
316         }
317         return ret;
318 }
319
320 static int lm25063_read_word_data(struct i2c_client *client, int page, int reg)
321 {
322         int ret;
323
324         switch (reg) {
325         case PMBUS_VIRT_READ_VOUT_MAX:
326                 ret = pmbus_read_word_data(client, 0, LM25063_READ_VOUT_MAX);
327                 break;
328         case PMBUS_VIRT_READ_VOUT_MIN:
329                 ret = pmbus_read_word_data(client, 0, LM25063_READ_VOUT_MIN);
330                 break;
331         default:
332                 ret = lm25066_read_word_data(client, page, reg);
333                 break;
334         }
335         return ret;
336 }
337
338 static int lm25056_read_word_data(struct i2c_client *client, int page, int reg)
339 {
340         int ret;
341
342         switch (reg) {
343         case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
344                 ret = pmbus_read_word_data(client, 0,
345                                            LM25056_VAUX_UV_WARN_LIMIT);
346                 if (ret < 0)
347                         break;
348                 /* Adjust returned value to match VIN coefficients */
349                 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
350                 break;
351         case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
352                 ret = pmbus_read_word_data(client, 0,
353                                            LM25056_VAUX_OV_WARN_LIMIT);
354                 if (ret < 0)
355                         break;
356                 /* Adjust returned value to match VIN coefficients */
357                 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
358                 break;
359         default:
360                 ret = lm25066_read_word_data(client, page, reg);
361                 break;
362         }
363         return ret;
364 }
365
366 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
367 {
368         int ret, s;
369
370         switch (reg) {
371         case PMBUS_VIRT_STATUS_VMON:
372                 ret = pmbus_read_byte_data(client, 0,
373                                            PMBUS_STATUS_MFR_SPECIFIC);
374                 if (ret < 0)
375                         break;
376                 s = 0;
377                 if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
378                         s |= PB_VOLTAGE_UV_WARNING;
379                 if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
380                         s |= PB_VOLTAGE_OV_WARNING;
381                 ret = s;
382                 break;
383         default:
384                 ret = -ENODATA;
385                 break;
386         }
387         return ret;
388 }
389
390 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
391                                    u16 word)
392 {
393         const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
394         const struct lm25066_data *data = to_lm25066_data(info);
395         int ret;
396
397         switch (reg) {
398         case PMBUS_POUT_OP_FAULT_LIMIT:
399         case PMBUS_POUT_OP_WARN_LIMIT:
400         case PMBUS_VOUT_UV_WARN_LIMIT:
401         case PMBUS_OT_FAULT_LIMIT:
402         case PMBUS_OT_WARN_LIMIT:
403         case PMBUS_IIN_OC_FAULT_LIMIT:
404         case PMBUS_VIN_UV_WARN_LIMIT:
405         case PMBUS_VIN_UV_FAULT_LIMIT:
406         case PMBUS_VIN_OV_FAULT_LIMIT:
407         case PMBUS_VIN_OV_WARN_LIMIT:
408                 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
409                 ret = pmbus_write_word_data(client, 0, reg, word);
410                 pmbus_clear_cache(client);
411                 break;
412         case PMBUS_IIN_OC_WARN_LIMIT:
413                 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
414                 ret = pmbus_write_word_data(client, 0,
415                                             LM25066_MFR_IIN_OC_WARN_LIMIT,
416                                             word);
417                 pmbus_clear_cache(client);
418                 break;
419         case PMBUS_PIN_OP_WARN_LIMIT:
420                 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
421                 ret = pmbus_write_word_data(client, 0,
422                                             LM25066_MFR_PIN_OP_WARN_LIMIT,
423                                             word);
424                 pmbus_clear_cache(client);
425                 break;
426         case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
427                 /* Adjust from VIN coefficients (for LM25056) */
428                 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
429                 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
430                 ret = pmbus_write_word_data(client, 0,
431                                             LM25056_VAUX_UV_WARN_LIMIT, word);
432                 pmbus_clear_cache(client);
433                 break;
434         case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
435                 /* Adjust from VIN coefficients (for LM25056) */
436                 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
437                 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
438                 ret = pmbus_write_word_data(client, 0,
439                                             LM25056_VAUX_OV_WARN_LIMIT, word);
440                 pmbus_clear_cache(client);
441                 break;
442         case PMBUS_VIRT_RESET_PIN_HISTORY:
443                 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
444                 break;
445         default:
446                 ret = -ENODATA;
447                 break;
448         }
449         return ret;
450 }
451
452 static int lm25066_probe(struct i2c_client *client,
453                           const struct i2c_device_id *id)
454 {
455         int config;
456         struct lm25066_data *data;
457         struct pmbus_driver_info *info;
458         struct __coeff *coeff;
459
460         if (!i2c_check_functionality(client->adapter,
461                                      I2C_FUNC_SMBUS_READ_BYTE_DATA))
462                 return -ENODEV;
463
464         data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
465                             GFP_KERNEL);
466         if (!data)
467                 return -ENOMEM;
468
469         config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
470         if (config < 0)
471                 return config;
472
473         data->id = id->driver_data;
474         info = &data->info;
475
476         info->pages = 1;
477         info->format[PSC_VOLTAGE_IN] = direct;
478         info->format[PSC_VOLTAGE_OUT] = direct;
479         info->format[PSC_CURRENT_IN] = direct;
480         info->format[PSC_TEMPERATURE] = direct;
481         info->format[PSC_POWER] = direct;
482
483         info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
484           | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
485           | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
486
487         if (data->id == lm25056) {
488                 info->func[0] |= PMBUS_HAVE_STATUS_VMON;
489                 info->read_word_data = lm25056_read_word_data;
490                 info->read_byte_data = lm25056_read_byte_data;
491                 data->rlimit = 0x0fff;
492         } else if (data->id == lm25063) {
493                 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
494                   | PMBUS_HAVE_POUT;
495                 info->read_word_data = lm25063_read_word_data;
496                 data->rlimit = 0xffff;
497         } else {
498                 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
499                 info->read_word_data = lm25066_read_word_data;
500                 data->rlimit = 0x0fff;
501         }
502         info->write_word_data = lm25066_write_word_data;
503
504         coeff = &lm25066_coeff[data->id][0];
505         info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
506         info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
507         info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
508         info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
509         info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
510         info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
511         info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
512         info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
513         info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
514         info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
515         info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
516         info->b[PSC_POWER] = coeff[PSC_POWER].b;
517         info->R[PSC_POWER] = coeff[PSC_POWER].R;
518         if (config & LM25066_DEV_SETUP_CL) {
519                 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
520                 info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
521         } else {
522                 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
523                 info->m[PSC_POWER] = coeff[PSC_POWER].m;
524         }
525
526         return pmbus_do_probe(client, id, info);
527 }
528
529 static const struct i2c_device_id lm25066_id[] = {
530         {"lm25056", lm25056},
531         {"lm25063", lm25063},
532         {"lm25066", lm25066},
533         {"lm5064", lm5064},
534         {"lm5066", lm5066},
535         { }
536 };
537
538 MODULE_DEVICE_TABLE(i2c, lm25066_id);
539
540 /* This is the driver that will be inserted */
541 static struct i2c_driver lm25066_driver = {
542         .driver = {
543                    .name = "lm25066",
544                    },
545         .probe = lm25066_probe,
546         .remove = pmbus_do_remove,
547         .id_table = lm25066_id,
548 };
549
550 module_i2c_driver(lm25066_driver);
551
552 MODULE_AUTHOR("Guenter Roeck");
553 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
554 MODULE_LICENSE("GPL");