GNU Linux-libre 4.14.328-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, lm5066i };
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[6][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         [lm5066i] = {
237                 [PSC_VOLTAGE_IN] = {
238                         .m = 4617,
239                         .b = -140,
240                         .R = -2,
241                 },
242                 [PSC_VOLTAGE_OUT] = {
243                         .m = 4602,
244                         .b = 500,
245                         .R = -2,
246                 },
247                 [PSC_CURRENT_IN] = {
248                         .m = 15076,
249                         .b = -504,
250                         .R = -2,
251                 },
252                 [PSC_CURRENT_IN_L] = {
253                         .m = 7645,
254                         .b = 100,
255                         .R = -2,
256                 },
257                 [PSC_POWER] = {
258                         .m = 1701,
259                         .b = -4000,
260                         .R = -3,
261                 },
262                 [PSC_POWER_L] = {
263                         .m = 861,
264                         .b = -965,
265                         .R = -3,
266                 },
267                 [PSC_TEMPERATURE] = {
268                         .m = 16,
269                 },
270         },
271 };
272
273 struct lm25066_data {
274         int id;
275         u16 rlimit;                     /* Maximum register value */
276         struct pmbus_driver_info info;
277 };
278
279 #define to_lm25066_data(x)  container_of(x, struct lm25066_data, info)
280
281 static int lm25066_read_word_data(struct i2c_client *client, int page, int reg)
282 {
283         const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
284         const struct lm25066_data *data = to_lm25066_data(info);
285         int ret;
286
287         switch (reg) {
288         case PMBUS_VIRT_READ_VMON:
289                 ret = pmbus_read_word_data(client, 0, LM25066_READ_VAUX);
290                 if (ret < 0)
291                         break;
292                 /* Adjust returned value to match VIN coefficients */
293                 switch (data->id) {
294                 case lm25056:
295                         /* VIN: 6.14 mV VAUX: 293 uV LSB */
296                         ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
297                         break;
298                 case lm25063:
299                         /* VIN: 6.25 mV VAUX: 200.0 uV LSB */
300                         ret = DIV_ROUND_CLOSEST(ret * 20, 625);
301                         break;
302                 case lm25066:
303                         /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
304                         ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
305                         break;
306                 case lm5064:
307                         /* VIN: 4.53 mV VAUX: 700 uV LSB */
308                         ret = DIV_ROUND_CLOSEST(ret * 70, 453);
309                         break;
310                 case lm5066:
311                 case lm5066i:
312                         /* VIN: 2.18 mV VAUX: 725 uV LSB */
313                         ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
314                         break;
315                 }
316                 break;
317         case PMBUS_READ_IIN:
318                 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
319                 break;
320         case PMBUS_READ_PIN:
321                 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
322                 break;
323         case PMBUS_IIN_OC_WARN_LIMIT:
324                 ret = pmbus_read_word_data(client, 0,
325                                            LM25066_MFR_IIN_OC_WARN_LIMIT);
326                 break;
327         case PMBUS_PIN_OP_WARN_LIMIT:
328                 ret = pmbus_read_word_data(client, 0,
329                                            LM25066_MFR_PIN_OP_WARN_LIMIT);
330                 break;
331         case PMBUS_VIRT_READ_VIN_AVG:
332                 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
333                 break;
334         case PMBUS_VIRT_READ_VOUT_AVG:
335                 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
336                 break;
337         case PMBUS_VIRT_READ_IIN_AVG:
338                 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
339                 break;
340         case PMBUS_VIRT_READ_PIN_AVG:
341                 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
342                 break;
343         case PMBUS_VIRT_READ_PIN_MAX:
344                 ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
345                 break;
346         case PMBUS_VIRT_RESET_PIN_HISTORY:
347                 ret = 0;
348                 break;
349         default:
350                 ret = -ENODATA;
351                 break;
352         }
353         return ret;
354 }
355
356 static int lm25063_read_word_data(struct i2c_client *client, int page, int reg)
357 {
358         int ret;
359
360         switch (reg) {
361         case PMBUS_VIRT_READ_VOUT_MAX:
362                 ret = pmbus_read_word_data(client, 0, LM25063_READ_VOUT_MAX);
363                 break;
364         case PMBUS_VIRT_READ_VOUT_MIN:
365                 ret = pmbus_read_word_data(client, 0, LM25063_READ_VOUT_MIN);
366                 break;
367         default:
368                 ret = lm25066_read_word_data(client, page, reg);
369                 break;
370         }
371         return ret;
372 }
373
374 static int lm25056_read_word_data(struct i2c_client *client, int page, int reg)
375 {
376         int ret;
377
378         switch (reg) {
379         case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
380                 ret = pmbus_read_word_data(client, 0,
381                                            LM25056_VAUX_UV_WARN_LIMIT);
382                 if (ret < 0)
383                         break;
384                 /* Adjust returned value to match VIN coefficients */
385                 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
386                 break;
387         case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
388                 ret = pmbus_read_word_data(client, 0,
389                                            LM25056_VAUX_OV_WARN_LIMIT);
390                 if (ret < 0)
391                         break;
392                 /* Adjust returned value to match VIN coefficients */
393                 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
394                 break;
395         default:
396                 ret = lm25066_read_word_data(client, page, reg);
397                 break;
398         }
399         return ret;
400 }
401
402 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
403 {
404         int ret, s;
405
406         switch (reg) {
407         case PMBUS_VIRT_STATUS_VMON:
408                 ret = pmbus_read_byte_data(client, 0,
409                                            PMBUS_STATUS_MFR_SPECIFIC);
410                 if (ret < 0)
411                         break;
412                 s = 0;
413                 if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
414                         s |= PB_VOLTAGE_UV_WARNING;
415                 if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
416                         s |= PB_VOLTAGE_OV_WARNING;
417                 ret = s;
418                 break;
419         default:
420                 ret = -ENODATA;
421                 break;
422         }
423         return ret;
424 }
425
426 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
427                                    u16 word)
428 {
429         const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
430         const struct lm25066_data *data = to_lm25066_data(info);
431         int ret;
432
433         switch (reg) {
434         case PMBUS_POUT_OP_FAULT_LIMIT:
435         case PMBUS_POUT_OP_WARN_LIMIT:
436         case PMBUS_VOUT_UV_WARN_LIMIT:
437         case PMBUS_OT_FAULT_LIMIT:
438         case PMBUS_OT_WARN_LIMIT:
439         case PMBUS_IIN_OC_FAULT_LIMIT:
440         case PMBUS_VIN_UV_WARN_LIMIT:
441         case PMBUS_VIN_UV_FAULT_LIMIT:
442         case PMBUS_VIN_OV_FAULT_LIMIT:
443         case PMBUS_VIN_OV_WARN_LIMIT:
444                 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
445                 ret = pmbus_write_word_data(client, 0, reg, word);
446                 pmbus_clear_cache(client);
447                 break;
448         case PMBUS_IIN_OC_WARN_LIMIT:
449                 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
450                 ret = pmbus_write_word_data(client, 0,
451                                             LM25066_MFR_IIN_OC_WARN_LIMIT,
452                                             word);
453                 pmbus_clear_cache(client);
454                 break;
455         case PMBUS_PIN_OP_WARN_LIMIT:
456                 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
457                 ret = pmbus_write_word_data(client, 0,
458                                             LM25066_MFR_PIN_OP_WARN_LIMIT,
459                                             word);
460                 pmbus_clear_cache(client);
461                 break;
462         case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
463                 /* Adjust from VIN coefficients (for LM25056) */
464                 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
465                 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
466                 ret = pmbus_write_word_data(client, 0,
467                                             LM25056_VAUX_UV_WARN_LIMIT, word);
468                 pmbus_clear_cache(client);
469                 break;
470         case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
471                 /* Adjust from VIN coefficients (for LM25056) */
472                 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
473                 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
474                 ret = pmbus_write_word_data(client, 0,
475                                             LM25056_VAUX_OV_WARN_LIMIT, word);
476                 pmbus_clear_cache(client);
477                 break;
478         case PMBUS_VIRT_RESET_PIN_HISTORY:
479                 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
480                 break;
481         default:
482                 ret = -ENODATA;
483                 break;
484         }
485         return ret;
486 }
487
488 static int lm25066_probe(struct i2c_client *client,
489                           const struct i2c_device_id *id)
490 {
491         int config;
492         struct lm25066_data *data;
493         struct pmbus_driver_info *info;
494         struct __coeff *coeff;
495
496         if (!i2c_check_functionality(client->adapter,
497                                      I2C_FUNC_SMBUS_READ_BYTE_DATA))
498                 return -ENODEV;
499
500         data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
501                             GFP_KERNEL);
502         if (!data)
503                 return -ENOMEM;
504
505         config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
506         if (config < 0)
507                 return config;
508
509         data->id = id->driver_data;
510         info = &data->info;
511
512         info->pages = 1;
513         info->format[PSC_VOLTAGE_IN] = direct;
514         info->format[PSC_VOLTAGE_OUT] = direct;
515         info->format[PSC_CURRENT_IN] = direct;
516         info->format[PSC_TEMPERATURE] = direct;
517         info->format[PSC_POWER] = direct;
518
519         info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
520           | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
521           | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
522
523         if (data->id == lm25056) {
524                 info->func[0] |= PMBUS_HAVE_STATUS_VMON;
525                 info->read_word_data = lm25056_read_word_data;
526                 info->read_byte_data = lm25056_read_byte_data;
527                 data->rlimit = 0x0fff;
528         } else if (data->id == lm25063) {
529                 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
530                   | PMBUS_HAVE_POUT;
531                 info->read_word_data = lm25063_read_word_data;
532                 data->rlimit = 0xffff;
533         } else {
534                 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
535                 info->read_word_data = lm25066_read_word_data;
536                 data->rlimit = 0x0fff;
537         }
538         info->write_word_data = lm25066_write_word_data;
539
540         coeff = &lm25066_coeff[data->id][0];
541         info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
542         info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
543         info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
544         info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
545         info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
546         info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
547         info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
548         info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
549         info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
550         info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
551         info->R[PSC_POWER] = coeff[PSC_POWER].R;
552         if (config & LM25066_DEV_SETUP_CL) {
553                 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
554                 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b;
555                 info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
556                 info->b[PSC_POWER] = coeff[PSC_POWER_L].b;
557         } else {
558                 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
559                 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
560                 info->m[PSC_POWER] = coeff[PSC_POWER].m;
561                 info->b[PSC_POWER] = coeff[PSC_POWER].b;
562         }
563
564         return pmbus_do_probe(client, id, info);
565 }
566
567 static const struct i2c_device_id lm25066_id[] = {
568         {"lm25056", lm25056},
569         {"lm25063", lm25063},
570         {"lm25066", lm25066},
571         {"lm5064", lm5064},
572         {"lm5066", lm5066},
573         {"lm5066i", lm5066i},
574         { }
575 };
576
577 MODULE_DEVICE_TABLE(i2c, lm25066_id);
578
579 /* This is the driver that will be inserted */
580 static struct i2c_driver lm25066_driver = {
581         .driver = {
582                    .name = "lm25066",
583                    },
584         .probe = lm25066_probe,
585         .remove = pmbus_do_remove,
586         .id_table = lm25066_id,
587 };
588
589 module_i2c_driver(lm25066_driver);
590
591 MODULE_AUTHOR("Guenter Roeck");
592 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
593 MODULE_LICENSE("GPL");