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