GNU Linux-libre 4.19.268-gnu1
[releases.git] / drivers / power / supply / max17042_battery.c
1 /*
2  * Fuel gauge driver for Maxim 17042 / 8966 / 8997
3  *  Note that Maxim 8966 and 8997 are mfd and this is its subdevice.
4  *
5  * Copyright (C) 2011 Samsung Electronics
6  * MyungJoo Ham <myungjoo.ham@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * This driver is based on max17040_battery.c
23  */
24
25 #include <linux/acpi.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/pm.h>
33 #include <linux/mod_devicetable.h>
34 #include <linux/power_supply.h>
35 #include <linux/power/max17042_battery.h>
36 #include <linux/of.h>
37 #include <linux/regmap.h>
38
39 /* Status register bits */
40 #define STATUS_POR_BIT         (1 << 1)
41 #define STATUS_BST_BIT         (1 << 3)
42 #define STATUS_VMN_BIT         (1 << 8)
43 #define STATUS_TMN_BIT         (1 << 9)
44 #define STATUS_SMN_BIT         (1 << 10)
45 #define STATUS_BI_BIT          (1 << 11)
46 #define STATUS_VMX_BIT         (1 << 12)
47 #define STATUS_TMX_BIT         (1 << 13)
48 #define STATUS_SMX_BIT         (1 << 14)
49 #define STATUS_BR_BIT          (1 << 15)
50
51 /* Interrupt mask bits */
52 #define CONFIG_ALRT_BIT_ENBL    (1 << 2)
53 #define STATUS_INTR_SOCMIN_BIT  (1 << 10)
54 #define STATUS_INTR_SOCMAX_BIT  (1 << 14)
55
56 #define VFSOC0_LOCK             0x0000
57 #define VFSOC0_UNLOCK           0x0080
58 #define MODEL_UNLOCK1   0X0059
59 #define MODEL_UNLOCK2   0X00C4
60 #define MODEL_LOCK1             0X0000
61 #define MODEL_LOCK2             0X0000
62
63 #define dQ_ACC_DIV      0x4
64 #define dP_ACC_100      0x1900
65 #define dP_ACC_200      0x3200
66
67 #define MAX17042_VMAX_TOLERANCE         50 /* 50 mV */
68
69 struct max17042_chip {
70         struct i2c_client *client;
71         struct regmap *regmap;
72         struct power_supply *battery;
73         enum max170xx_chip_type chip_type;
74         struct max17042_platform_data *pdata;
75         struct work_struct work;
76         int    init_complete;
77 };
78
79 static enum power_supply_property max17042_battery_props[] = {
80         POWER_SUPPLY_PROP_STATUS,
81         POWER_SUPPLY_PROP_PRESENT,
82         POWER_SUPPLY_PROP_TECHNOLOGY,
83         POWER_SUPPLY_PROP_CYCLE_COUNT,
84         POWER_SUPPLY_PROP_VOLTAGE_MAX,
85         POWER_SUPPLY_PROP_VOLTAGE_MIN,
86         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
87         POWER_SUPPLY_PROP_VOLTAGE_NOW,
88         POWER_SUPPLY_PROP_VOLTAGE_AVG,
89         POWER_SUPPLY_PROP_VOLTAGE_OCV,
90         POWER_SUPPLY_PROP_CAPACITY,
91         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
92         POWER_SUPPLY_PROP_CHARGE_FULL,
93         POWER_SUPPLY_PROP_CHARGE_NOW,
94         POWER_SUPPLY_PROP_CHARGE_COUNTER,
95         POWER_SUPPLY_PROP_TEMP,
96         POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
97         POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
98         POWER_SUPPLY_PROP_TEMP_MIN,
99         POWER_SUPPLY_PROP_TEMP_MAX,
100         POWER_SUPPLY_PROP_HEALTH,
101         POWER_SUPPLY_PROP_SCOPE,
102         POWER_SUPPLY_PROP_CURRENT_NOW,
103         POWER_SUPPLY_PROP_CURRENT_AVG,
104 };
105
106 static int max17042_get_temperature(struct max17042_chip *chip, int *temp)
107 {
108         int ret;
109         u32 data;
110         struct regmap *map = chip->regmap;
111
112         ret = regmap_read(map, MAX17042_TEMP, &data);
113         if (ret < 0)
114                 return ret;
115
116         *temp = sign_extend32(data, 15);
117         /* The value is converted into deci-centigrade scale */
118         /* Units of LSB = 1 / 256 degree Celsius */
119         *temp = *temp * 10 / 256;
120         return 0;
121 }
122
123 static int max17042_get_status(struct max17042_chip *chip, int *status)
124 {
125         int ret, charge_full, charge_now;
126         int avg_current;
127         u32 data;
128
129         ret = power_supply_am_i_supplied(chip->battery);
130         if (ret < 0) {
131                 *status = POWER_SUPPLY_STATUS_UNKNOWN;
132                 return 0;
133         }
134         if (ret == 0) {
135                 *status = POWER_SUPPLY_STATUS_DISCHARGING;
136                 return 0;
137         }
138
139         /*
140          * The MAX170xx has builtin end-of-charge detection and will update
141          * FullCAP to match RepCap when it detects end of charging.
142          *
143          * When this cycle the battery gets charged to a higher (calculated)
144          * capacity then the previous cycle then FullCAP will get updated
145          * contineously once end-of-charge detection kicks in, so allow the
146          * 2 to differ a bit.
147          */
148
149         ret = regmap_read(chip->regmap, MAX17042_FullCAP, &charge_full);
150         if (ret < 0)
151                 return ret;
152
153         ret = regmap_read(chip->regmap, MAX17042_RepCap, &charge_now);
154         if (ret < 0)
155                 return ret;
156
157         if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD) {
158                 *status = POWER_SUPPLY_STATUS_FULL;
159                 return 0;
160         }
161
162         /*
163          * Even though we are supplied, we may still be discharging if the
164          * supply is e.g. only delivering 5V 0.5A. Check current if available.
165          */
166         if (!chip->pdata->enable_current_sense) {
167                 *status = POWER_SUPPLY_STATUS_CHARGING;
168                 return 0;
169         }
170
171         ret = regmap_read(chip->regmap, MAX17042_AvgCurrent, &data);
172         if (ret < 0)
173                 return ret;
174
175         avg_current = sign_extend32(data, 15);
176         avg_current *= 1562500 / chip->pdata->r_sns;
177
178         if (avg_current > 0)
179                 *status = POWER_SUPPLY_STATUS_CHARGING;
180         else
181                 *status = POWER_SUPPLY_STATUS_DISCHARGING;
182
183         return 0;
184 }
185
186 static int max17042_get_battery_health(struct max17042_chip *chip, int *health)
187 {
188         int temp, vavg, vbatt, ret;
189         u32 val;
190
191         ret = regmap_read(chip->regmap, MAX17042_AvgVCELL, &val);
192         if (ret < 0)
193                 goto health_error;
194
195         /* bits [0-3] unused */
196         vavg = val * 625 / 8;
197         /* Convert to millivolts */
198         vavg /= 1000;
199
200         ret = regmap_read(chip->regmap, MAX17042_VCELL, &val);
201         if (ret < 0)
202                 goto health_error;
203
204         /* bits [0-3] unused */
205         vbatt = val * 625 / 8;
206         /* Convert to millivolts */
207         vbatt /= 1000;
208
209         if (vavg < chip->pdata->vmin) {
210                 *health = POWER_SUPPLY_HEALTH_DEAD;
211                 goto out;
212         }
213
214         if (vbatt > chip->pdata->vmax + MAX17042_VMAX_TOLERANCE) {
215                 *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
216                 goto out;
217         }
218
219         ret = max17042_get_temperature(chip, &temp);
220         if (ret < 0)
221                 goto health_error;
222
223         if (temp < chip->pdata->temp_min) {
224                 *health = POWER_SUPPLY_HEALTH_COLD;
225                 goto out;
226         }
227
228         if (temp > chip->pdata->temp_max) {
229                 *health = POWER_SUPPLY_HEALTH_OVERHEAT;
230                 goto out;
231         }
232
233         *health = POWER_SUPPLY_HEALTH_GOOD;
234
235 out:
236         return 0;
237
238 health_error:
239         return ret;
240 }
241
242 static int max17042_get_property(struct power_supply *psy,
243                             enum power_supply_property psp,
244                             union power_supply_propval *val)
245 {
246         struct max17042_chip *chip = power_supply_get_drvdata(psy);
247         struct regmap *map = chip->regmap;
248         int ret;
249         u32 data;
250         u64 data64;
251
252         if (!chip->init_complete)
253                 return -EAGAIN;
254
255         switch (psp) {
256         case POWER_SUPPLY_PROP_STATUS:
257                 ret = max17042_get_status(chip, &val->intval);
258                 if (ret < 0)
259                         return ret;
260                 break;
261         case POWER_SUPPLY_PROP_PRESENT:
262                 ret = regmap_read(map, MAX17042_STATUS, &data);
263                 if (ret < 0)
264                         return ret;
265
266                 if (data & MAX17042_STATUS_BattAbsent)
267                         val->intval = 0;
268                 else
269                         val->intval = 1;
270                 break;
271         case POWER_SUPPLY_PROP_TECHNOLOGY:
272                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
273                 break;
274         case POWER_SUPPLY_PROP_CYCLE_COUNT:
275                 ret = regmap_read(map, MAX17042_Cycles, &data);
276                 if (ret < 0)
277                         return ret;
278
279                 val->intval = data;
280                 break;
281         case POWER_SUPPLY_PROP_VOLTAGE_MAX:
282                 ret = regmap_read(map, MAX17042_MinMaxVolt, &data);
283                 if (ret < 0)
284                         return ret;
285
286                 val->intval = data >> 8;
287                 val->intval *= 20000; /* Units of LSB = 20mV */
288                 break;
289         case POWER_SUPPLY_PROP_VOLTAGE_MIN:
290                 ret = regmap_read(map, MAX17042_MinMaxVolt, &data);
291                 if (ret < 0)
292                         return ret;
293
294                 val->intval = (data & 0xff) * 20000; /* Units of 20mV */
295                 break;
296         case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
297                 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
298                         ret = regmap_read(map, MAX17042_V_empty, &data);
299                 else
300                         ret = regmap_read(map, MAX17047_V_empty, &data);
301                 if (ret < 0)
302                         return ret;
303
304                 val->intval = data >> 7;
305                 val->intval *= 10000; /* Units of LSB = 10mV */
306                 break;
307         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
308                 ret = regmap_read(map, MAX17042_VCELL, &data);
309                 if (ret < 0)
310                         return ret;
311
312                 val->intval = data * 625 / 8;
313                 break;
314         case POWER_SUPPLY_PROP_VOLTAGE_AVG:
315                 ret = regmap_read(map, MAX17042_AvgVCELL, &data);
316                 if (ret < 0)
317                         return ret;
318
319                 val->intval = data * 625 / 8;
320                 break;
321         case POWER_SUPPLY_PROP_VOLTAGE_OCV:
322                 ret = regmap_read(map, MAX17042_OCVInternal, &data);
323                 if (ret < 0)
324                         return ret;
325
326                 val->intval = data * 625 / 8;
327                 break;
328         case POWER_SUPPLY_PROP_CAPACITY:
329                 if (chip->pdata->enable_current_sense)
330                         ret = regmap_read(map, MAX17042_RepSOC, &data);
331                 else
332                         ret = regmap_read(map, MAX17042_VFSOC, &data);
333                 if (ret < 0)
334                         return ret;
335
336                 val->intval = data >> 8;
337                 break;
338         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
339                 ret = regmap_read(map, MAX17042_DesignCap, &data);
340                 if (ret < 0)
341                         return ret;
342
343                 data64 = data * 5000000ll;
344                 do_div(data64, chip->pdata->r_sns);
345                 val->intval = data64;
346                 break;
347         case POWER_SUPPLY_PROP_CHARGE_FULL:
348                 ret = regmap_read(map, MAX17042_FullCAP, &data);
349                 if (ret < 0)
350                         return ret;
351
352                 data64 = data * 5000000ll;
353                 do_div(data64, chip->pdata->r_sns);
354                 val->intval = data64;
355                 break;
356         case POWER_SUPPLY_PROP_CHARGE_NOW:
357                 ret = regmap_read(map, MAX17042_RepCap, &data);
358                 if (ret < 0)
359                         return ret;
360
361                 data64 = data * 5000000ll;
362                 do_div(data64, chip->pdata->r_sns);
363                 val->intval = data64;
364                 break;
365         case POWER_SUPPLY_PROP_CHARGE_COUNTER:
366                 ret = regmap_read(map, MAX17042_QH, &data);
367                 if (ret < 0)
368                         return ret;
369
370                 val->intval = data * 1000 / 2;
371                 break;
372         case POWER_SUPPLY_PROP_TEMP:
373                 ret = max17042_get_temperature(chip, &val->intval);
374                 if (ret < 0)
375                         return ret;
376                 break;
377         case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
378                 ret = regmap_read(map, MAX17042_TALRT_Th, &data);
379                 if (ret < 0)
380                         return ret;
381                 /* LSB is Alert Minimum. In deci-centigrade */
382                 val->intval = sign_extend32(data & 0xff, 7) * 10;
383                 break;
384         case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
385                 ret = regmap_read(map, MAX17042_TALRT_Th, &data);
386                 if (ret < 0)
387                         return ret;
388                 /* MSB is Alert Maximum. In deci-centigrade */
389                 val->intval = sign_extend32(data >> 8, 7) * 10;
390                 break;
391         case POWER_SUPPLY_PROP_TEMP_MIN:
392                 val->intval = chip->pdata->temp_min;
393                 break;
394         case POWER_SUPPLY_PROP_TEMP_MAX:
395                 val->intval = chip->pdata->temp_max;
396                 break;
397         case POWER_SUPPLY_PROP_HEALTH:
398                 ret = max17042_get_battery_health(chip, &val->intval);
399                 if (ret < 0)
400                         return ret;
401                 break;
402         case POWER_SUPPLY_PROP_SCOPE:
403                 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
404                 break;
405         case POWER_SUPPLY_PROP_CURRENT_NOW:
406                 if (chip->pdata->enable_current_sense) {
407                         ret = regmap_read(map, MAX17042_Current, &data);
408                         if (ret < 0)
409                                 return ret;
410
411                         val->intval = sign_extend32(data, 15);
412                         val->intval *= 1562500 / chip->pdata->r_sns;
413                 } else {
414                         return -EINVAL;
415                 }
416                 break;
417         case POWER_SUPPLY_PROP_CURRENT_AVG:
418                 if (chip->pdata->enable_current_sense) {
419                         ret = regmap_read(map, MAX17042_AvgCurrent, &data);
420                         if (ret < 0)
421                                 return ret;
422
423                         val->intval = sign_extend32(data, 15);
424                         val->intval *= 1562500 / chip->pdata->r_sns;
425                 } else {
426                         return -EINVAL;
427                 }
428                 break;
429         default:
430                 return -EINVAL;
431         }
432         return 0;
433 }
434
435 static int max17042_set_property(struct power_supply *psy,
436                             enum power_supply_property psp,
437                             const union power_supply_propval *val)
438 {
439         struct max17042_chip *chip = power_supply_get_drvdata(psy);
440         struct regmap *map = chip->regmap;
441         int ret = 0;
442         u32 data;
443         int8_t temp;
444
445         switch (psp) {
446         case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
447                 ret = regmap_read(map, MAX17042_TALRT_Th, &data);
448                 if (ret < 0)
449                         return ret;
450
451                 /* Input in deci-centigrade, convert to centigrade */
452                 temp = val->intval / 10;
453                 /* force min < max */
454                 if (temp >= (int8_t)(data >> 8))
455                         temp = (int8_t)(data >> 8) - 1;
456                 /* Write both MAX and MIN ALERT */
457                 data = (data & 0xff00) + temp;
458                 ret = regmap_write(map, MAX17042_TALRT_Th, data);
459                 break;
460         case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
461                 ret = regmap_read(map, MAX17042_TALRT_Th, &data);
462                 if (ret < 0)
463                         return ret;
464
465                 /* Input in Deci-Centigrade, convert to centigrade */
466                 temp = val->intval / 10;
467                 /* force max > min */
468                 if (temp <= (int8_t)(data & 0xff))
469                         temp = (int8_t)(data & 0xff) + 1;
470                 /* Write both MAX and MIN ALERT */
471                 data = (data & 0xff) + (temp << 8);
472                 ret = regmap_write(map, MAX17042_TALRT_Th, data);
473                 break;
474         default:
475                 ret = -EINVAL;
476         }
477
478         return ret;
479 }
480
481 static int max17042_property_is_writeable(struct power_supply *psy,
482                 enum power_supply_property psp)
483 {
484         int ret;
485
486         switch (psp) {
487         case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
488         case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
489                 ret = 1;
490                 break;
491         default:
492                 ret = 0;
493         }
494
495         return ret;
496 }
497
498 static void max17042_external_power_changed(struct power_supply *psy)
499 {
500         power_supply_changed(psy);
501 }
502
503 static int max17042_write_verify_reg(struct regmap *map, u8 reg, u32 value)
504 {
505         int retries = 8;
506         int ret;
507         u32 read_value;
508
509         do {
510                 ret = regmap_write(map, reg, value);
511                 regmap_read(map, reg, &read_value);
512                 if (read_value != value) {
513                         ret = -EIO;
514                         retries--;
515                 }
516         } while (retries && read_value != value);
517
518         if (ret < 0)
519                 pr_err("%s: err %d\n", __func__, ret);
520
521         return ret;
522 }
523
524 static inline void max17042_override_por(struct regmap *map,
525                                          u8 reg, u16 value)
526 {
527         if (value)
528                 regmap_write(map, reg, value);
529 }
530
531 static inline void max10742_unlock_model(struct max17042_chip *chip)
532 {
533         struct regmap *map = chip->regmap;
534
535         regmap_write(map, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
536         regmap_write(map, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
537 }
538
539 static inline void max10742_lock_model(struct max17042_chip *chip)
540 {
541         struct regmap *map = chip->regmap;
542
543         regmap_write(map, MAX17042_MLOCKReg1, MODEL_LOCK1);
544         regmap_write(map, MAX17042_MLOCKReg2, MODEL_LOCK2);
545 }
546
547 static inline void max17042_write_model_data(struct max17042_chip *chip,
548                                         u8 addr, int size)
549 {
550         struct regmap *map = chip->regmap;
551         int i;
552
553         for (i = 0; i < size; i++)
554                 regmap_write(map, addr + i,
555                         chip->pdata->config_data->cell_char_tbl[i]);
556 }
557
558 static inline void max17042_read_model_data(struct max17042_chip *chip,
559                                         u8 addr, u16 *data, int size)
560 {
561         struct regmap *map = chip->regmap;
562         int i;
563         u32 tmp;
564
565         for (i = 0; i < size; i++) {
566                 regmap_read(map, addr + i, &tmp);
567                 data[i] = (u16)tmp;
568         }
569 }
570
571 static inline int max17042_model_data_compare(struct max17042_chip *chip,
572                                         u16 *data1, u16 *data2, int size)
573 {
574         int i;
575
576         if (memcmp(data1, data2, size)) {
577                 dev_err(&chip->client->dev, "%s compare failed\n", __func__);
578                 for (i = 0; i < size; i++)
579                         dev_info(&chip->client->dev, "0x%x, 0x%x",
580                                 data1[i], data2[i]);
581                 dev_info(&chip->client->dev, "\n");
582                 return -EINVAL;
583         }
584         return 0;
585 }
586
587 static int max17042_init_model(struct max17042_chip *chip)
588 {
589         int ret;
590         int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
591         u16 *temp_data;
592
593         temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
594         if (!temp_data)
595                 return -ENOMEM;
596
597         max10742_unlock_model(chip);
598         max17042_write_model_data(chip, MAX17042_MODELChrTbl,
599                                 table_size);
600         max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
601                                 table_size);
602
603         ret = max17042_model_data_compare(
604                 chip,
605                 chip->pdata->config_data->cell_char_tbl,
606                 temp_data,
607                 table_size);
608
609         max10742_lock_model(chip);
610         kfree(temp_data);
611
612         return ret;
613 }
614
615 static int max17042_verify_model_lock(struct max17042_chip *chip)
616 {
617         int i;
618         int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
619         u16 *temp_data;
620         int ret = 0;
621
622         temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
623         if (!temp_data)
624                 return -ENOMEM;
625
626         max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
627                                 table_size);
628         for (i = 0; i < table_size; i++)
629                 if (temp_data[i])
630                         ret = -EINVAL;
631
632         kfree(temp_data);
633         return ret;
634 }
635
636 static void max17042_write_config_regs(struct max17042_chip *chip)
637 {
638         struct max17042_config_data *config = chip->pdata->config_data;
639         struct regmap *map = chip->regmap;
640
641         regmap_write(map, MAX17042_CONFIG, config->config);
642         regmap_write(map, MAX17042_LearnCFG, config->learn_cfg);
643         regmap_write(map, MAX17042_FilterCFG,
644                         config->filter_cfg);
645         regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg);
646         if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047 ||
647                         chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)
648                 regmap_write(map, MAX17047_FullSOCThr,
649                                                 config->full_soc_thresh);
650 }
651
652 static void  max17042_write_custom_regs(struct max17042_chip *chip)
653 {
654         struct max17042_config_data *config = chip->pdata->config_data;
655         struct regmap *map = chip->regmap;
656
657         max17042_write_verify_reg(map, MAX17042_RCOMP0, config->rcomp0);
658         max17042_write_verify_reg(map, MAX17042_TempCo, config->tcompc0);
659         max17042_write_verify_reg(map, MAX17042_ICHGTerm, config->ichgt_term);
660         if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) {
661                 regmap_write(map, MAX17042_EmptyTempCo, config->empty_tempco);
662                 max17042_write_verify_reg(map, MAX17042_K_empty0,
663                                         config->kempty0);
664         } else {
665                 max17042_write_verify_reg(map, MAX17047_QRTbl00,
666                                                 config->qrtbl00);
667                 max17042_write_verify_reg(map, MAX17047_QRTbl10,
668                                                 config->qrtbl10);
669                 max17042_write_verify_reg(map, MAX17047_QRTbl20,
670                                                 config->qrtbl20);
671                 max17042_write_verify_reg(map, MAX17047_QRTbl30,
672                                                 config->qrtbl30);
673         }
674 }
675
676 static void max17042_update_capacity_regs(struct max17042_chip *chip)
677 {
678         struct max17042_config_data *config = chip->pdata->config_data;
679         struct regmap *map = chip->regmap;
680
681         max17042_write_verify_reg(map, MAX17042_FullCAP,
682                                 config->fullcap);
683         regmap_write(map, MAX17042_DesignCap, config->design_cap);
684         max17042_write_verify_reg(map, MAX17042_FullCAPNom,
685                                 config->fullcapnom);
686 }
687
688 static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
689 {
690         unsigned int vfSoc;
691         struct regmap *map = chip->regmap;
692
693         regmap_read(map, MAX17042_VFSOC, &vfSoc);
694         regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
695         max17042_write_verify_reg(map, MAX17042_VFSOC0, vfSoc);
696         regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
697 }
698
699 static void max17042_load_new_capacity_params(struct max17042_chip *chip)
700 {
701         u32 full_cap0, rep_cap, dq_acc, vfSoc;
702         u32 rem_cap;
703
704         struct max17042_config_data *config = chip->pdata->config_data;
705         struct regmap *map = chip->regmap;
706
707         regmap_read(map, MAX17042_FullCAP0, &full_cap0);
708         regmap_read(map, MAX17042_VFSOC, &vfSoc);
709
710         /* fg_vfSoc needs to shifted by 8 bits to get the
711          * perc in 1% accuracy, to get the right rem_cap multiply
712          * full_cap0, fg_vfSoc and devide by 100
713          */
714         rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
715         max17042_write_verify_reg(map, MAX17042_RemCap, rem_cap);
716
717         rep_cap = rem_cap;
718         max17042_write_verify_reg(map, MAX17042_RepCap, rep_cap);
719
720         /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
721         dq_acc = config->fullcap / dQ_ACC_DIV;
722         max17042_write_verify_reg(map, MAX17042_dQacc, dq_acc);
723         max17042_write_verify_reg(map, MAX17042_dPacc, dP_ACC_200);
724
725         max17042_write_verify_reg(map, MAX17042_FullCAP,
726                         config->fullcap);
727         regmap_write(map, MAX17042_DesignCap,
728                         config->design_cap);
729         max17042_write_verify_reg(map, MAX17042_FullCAPNom,
730                         config->fullcapnom);
731         /* Update SOC register with new SOC */
732         regmap_write(map, MAX17042_RepSOC, vfSoc);
733 }
734
735 /*
736  * Block write all the override values coming from platform data.
737  * This function MUST be called before the POR initialization proceedure
738  * specified by maxim.
739  */
740 static inline void max17042_override_por_values(struct max17042_chip *chip)
741 {
742         struct regmap *map = chip->regmap;
743         struct max17042_config_data *config = chip->pdata->config_data;
744
745         max17042_override_por(map, MAX17042_TGAIN, config->tgain);
746         max17042_override_por(map, MAX17042_TOFF, config->toff);
747         max17042_override_por(map, MAX17042_CGAIN, config->cgain);
748         max17042_override_por(map, MAX17042_COFF, config->coff);
749
750         max17042_override_por(map, MAX17042_VALRT_Th, config->valrt_thresh);
751         max17042_override_por(map, MAX17042_TALRT_Th, config->talrt_thresh);
752         max17042_override_por(map, MAX17042_SALRT_Th,
753                                                 config->soc_alrt_thresh);
754         max17042_override_por(map, MAX17042_CONFIG, config->config);
755         max17042_override_por(map, MAX17042_SHDNTIMER, config->shdntimer);
756
757         max17042_override_por(map, MAX17042_DesignCap, config->design_cap);
758         max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term);
759
760         max17042_override_por(map, MAX17042_AtRate, config->at_rate);
761         max17042_override_por(map, MAX17042_LearnCFG, config->learn_cfg);
762         max17042_override_por(map, MAX17042_FilterCFG, config->filter_cfg);
763         max17042_override_por(map, MAX17042_RelaxCFG, config->relax_cfg);
764         max17042_override_por(map, MAX17042_MiscCFG, config->misc_cfg);
765         max17042_override_por(map, MAX17042_MaskSOC, config->masksoc);
766
767         max17042_override_por(map, MAX17042_FullCAP, config->fullcap);
768         max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom);
769         if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
770                 max17042_override_por(map, MAX17042_SOC_empty,
771                                                 config->socempty);
772         max17042_override_por(map, MAX17042_LAvg_empty, config->lavg_empty);
773         max17042_override_por(map, MAX17042_dQacc, config->dqacc);
774         max17042_override_por(map, MAX17042_dPacc, config->dpacc);
775
776         if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
777                 max17042_override_por(map, MAX17042_V_empty, config->vempty);
778         else
779                 max17042_override_por(map, MAX17047_V_empty, config->vempty);
780         max17042_override_por(map, MAX17042_TempNom, config->temp_nom);
781         max17042_override_por(map, MAX17042_TempLim, config->temp_lim);
782         max17042_override_por(map, MAX17042_FCTC, config->fctc);
783         max17042_override_por(map, MAX17042_RCOMP0, config->rcomp0);
784         max17042_override_por(map, MAX17042_TempCo, config->tcompc0);
785         if (chip->chip_type) {
786                 max17042_override_por(map, MAX17042_EmptyTempCo,
787                                                 config->empty_tempco);
788                 max17042_override_por(map, MAX17042_K_empty0,
789                                                 config->kempty0);
790         }
791 }
792
793 static int max17042_init_chip(struct max17042_chip *chip)
794 {
795         struct regmap *map = chip->regmap;
796         int ret;
797
798         max17042_override_por_values(chip);
799         /* After Power up, the MAX17042 requires 500mS in order
800          * to perform signal debouncing and initial SOC reporting
801          */
802         msleep(500);
803
804         /* Initialize configaration */
805         max17042_write_config_regs(chip);
806
807         /* write cell characterization data */
808         ret = max17042_init_model(chip);
809         if (ret) {
810                 dev_err(&chip->client->dev, "%s init failed\n",
811                         __func__);
812                 return -EIO;
813         }
814
815         ret = max17042_verify_model_lock(chip);
816         if (ret) {
817                 dev_err(&chip->client->dev, "%s lock verify failed\n",
818                         __func__);
819                 return -EIO;
820         }
821         /* write custom parameters */
822         max17042_write_custom_regs(chip);
823
824         /* update capacity params */
825         max17042_update_capacity_regs(chip);
826
827         /* delay must be atleast 350mS to allow VFSOC
828          * to be calculated from the new configuration
829          */
830         msleep(350);
831
832         /* reset vfsoc0 reg */
833         max17042_reset_vfsoc0_reg(chip);
834
835         /* load new capacity params */
836         max17042_load_new_capacity_params(chip);
837
838         /* Init complete, Clear the POR bit */
839         regmap_update_bits(map, MAX17042_STATUS, STATUS_POR_BIT, 0x0);
840         return 0;
841 }
842
843 static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
844 {
845         struct regmap *map = chip->regmap;
846         u32 soc, soc_tr;
847
848         /* program interrupt thesholds such that we should
849          * get interrupt for every 'off' perc change in the soc
850          */
851         regmap_read(map, MAX17042_RepSOC, &soc);
852         soc >>= 8;
853         soc_tr = (soc + off) << 8;
854         if (off < soc)
855                 soc_tr |= soc - off;
856         regmap_write(map, MAX17042_SALRT_Th, soc_tr);
857 }
858
859 static irqreturn_t max17042_thread_handler(int id, void *dev)
860 {
861         struct max17042_chip *chip = dev;
862         u32 val;
863         int ret;
864
865         ret = regmap_read(chip->regmap, MAX17042_STATUS, &val);
866         if (ret)
867                 return IRQ_HANDLED;
868
869         if ((val & STATUS_INTR_SOCMIN_BIT) ||
870                 (val & STATUS_INTR_SOCMAX_BIT)) {
871                 dev_info(&chip->client->dev, "SOC threshold INTR\n");
872                 max17042_set_soc_threshold(chip, 1);
873         }
874
875         power_supply_changed(chip->battery);
876         return IRQ_HANDLED;
877 }
878
879 static void max17042_init_worker(struct work_struct *work)
880 {
881         struct max17042_chip *chip = container_of(work,
882                                 struct max17042_chip, work);
883         int ret;
884
885         /* Initialize registers according to values from the platform data */
886         if (chip->pdata->enable_por_init && chip->pdata->config_data) {
887                 ret = max17042_init_chip(chip);
888                 if (ret)
889                         return;
890         }
891
892         chip->init_complete = 1;
893 }
894
895 #ifdef CONFIG_OF
896 static struct max17042_platform_data *
897 max17042_get_of_pdata(struct max17042_chip *chip)
898 {
899         struct device *dev = &chip->client->dev;
900         struct device_node *np = dev->of_node;
901         u32 prop;
902         struct max17042_platform_data *pdata;
903
904         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
905         if (!pdata)
906                 return NULL;
907
908         /*
909          * Require current sense resistor value to be specified for
910          * current-sense functionality to be enabled at all.
911          */
912         if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
913                 pdata->r_sns = prop;
914                 pdata->enable_current_sense = true;
915         }
916
917         if (of_property_read_s32(np, "maxim,cold-temp", &pdata->temp_min))
918                 pdata->temp_min = INT_MIN;
919         if (of_property_read_s32(np, "maxim,over-heat-temp", &pdata->temp_max))
920                 pdata->temp_max = INT_MAX;
921         if (of_property_read_s32(np, "maxim,dead-volt", &pdata->vmin))
922                 pdata->vmin = INT_MIN;
923         if (of_property_read_s32(np, "maxim,over-volt", &pdata->vmax))
924                 pdata->vmax = INT_MAX;
925
926         return pdata;
927 }
928 #endif
929
930 static struct max17042_reg_data max17047_default_pdata_init_regs[] = {
931         /*
932          * Some firmwares do not set FullSOCThr, Enable End-of-Charge Detection
933          * when the voltage FG reports 95%, as recommended in the datasheet.
934          */
935         { MAX17047_FullSOCThr, MAX17042_BATTERY_FULL << 8 },
936 };
937
938 static struct max17042_platform_data *
939 max17042_get_default_pdata(struct max17042_chip *chip)
940 {
941         struct device *dev = &chip->client->dev;
942         struct max17042_platform_data *pdata;
943         int ret, misc_cfg;
944
945         /*
946          * The MAX17047 gets used on x86 where we might not have pdata, assume
947          * the firmware will already have initialized the fuel-gauge and provide
948          * default values for the non init bits to make things work.
949          */
950         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
951         if (!pdata)
952                 return pdata;
953
954         if (chip->chip_type != MAXIM_DEVICE_TYPE_MAX17042) {
955                 pdata->init_data = max17047_default_pdata_init_regs;
956                 pdata->num_init_data =
957                         ARRAY_SIZE(max17047_default_pdata_init_regs);
958         }
959
960         ret = regmap_read(chip->regmap, MAX17042_MiscCFG, &misc_cfg);
961         if (ret < 0)
962                 return NULL;
963
964         /* If bits 0-1 are set to 3 then only Voltage readings are used */
965         if ((misc_cfg & 0x3) == 0x3)
966                 pdata->enable_current_sense = false;
967         else
968                 pdata->enable_current_sense = true;
969
970         pdata->vmin = MAX17042_DEFAULT_VMIN;
971         pdata->vmax = MAX17042_DEFAULT_VMAX;
972         pdata->temp_min = MAX17042_DEFAULT_TEMP_MIN;
973         pdata->temp_max = MAX17042_DEFAULT_TEMP_MAX;
974
975         return pdata;
976 }
977
978 static struct max17042_platform_data *
979 max17042_get_pdata(struct max17042_chip *chip)
980 {
981         struct device *dev = &chip->client->dev;
982
983 #ifdef CONFIG_OF
984         if (dev->of_node)
985                 return max17042_get_of_pdata(chip);
986 #endif
987         if (dev->platform_data)
988                 return dev->platform_data;
989
990         return max17042_get_default_pdata(chip);
991 }
992
993 static const struct regmap_config max17042_regmap_config = {
994         .reg_bits = 8,
995         .val_bits = 16,
996         .val_format_endian = REGMAP_ENDIAN_NATIVE,
997 };
998
999 static const struct power_supply_desc max17042_psy_desc = {
1000         .name           = "max170xx_battery",
1001         .type           = POWER_SUPPLY_TYPE_BATTERY,
1002         .get_property   = max17042_get_property,
1003         .set_property   = max17042_set_property,
1004         .property_is_writeable  = max17042_property_is_writeable,
1005         .external_power_changed = max17042_external_power_changed,
1006         .properties     = max17042_battery_props,
1007         .num_properties = ARRAY_SIZE(max17042_battery_props),
1008 };
1009
1010 static const struct power_supply_desc max17042_no_current_sense_psy_desc = {
1011         .name           = "max170xx_battery",
1012         .type           = POWER_SUPPLY_TYPE_BATTERY,
1013         .get_property   = max17042_get_property,
1014         .set_property   = max17042_set_property,
1015         .property_is_writeable  = max17042_property_is_writeable,
1016         .properties     = max17042_battery_props,
1017         .num_properties = ARRAY_SIZE(max17042_battery_props) - 2,
1018 };
1019
1020 static int max17042_probe(struct i2c_client *client,
1021                         const struct i2c_device_id *id)
1022 {
1023         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
1024         const struct power_supply_desc *max17042_desc = &max17042_psy_desc;
1025         struct power_supply_config psy_cfg = {};
1026         const struct acpi_device_id *acpi_id = NULL;
1027         struct device *dev = &client->dev;
1028         struct max17042_chip *chip;
1029         int ret;
1030         int i;
1031         u32 val;
1032
1033         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
1034                 return -EIO;
1035
1036         chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1037         if (!chip)
1038                 return -ENOMEM;
1039
1040         chip->client = client;
1041         if (id) {
1042                 chip->chip_type = id->driver_data;
1043         } else {
1044                 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
1045                 if (!acpi_id)
1046                         return -ENODEV;
1047
1048                 chip->chip_type = acpi_id->driver_data;
1049         }
1050         chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config);
1051         if (IS_ERR(chip->regmap)) {
1052                 dev_err(&client->dev, "Failed to initialize regmap\n");
1053                 return -EINVAL;
1054         }
1055
1056         chip->pdata = max17042_get_pdata(chip);
1057         if (!chip->pdata) {
1058                 dev_err(&client->dev, "no platform data provided\n");
1059                 return -EINVAL;
1060         }
1061
1062         i2c_set_clientdata(client, chip);
1063         psy_cfg.drv_data = chip;
1064         psy_cfg.of_node = dev->of_node;
1065
1066         /* When current is not measured,
1067          * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
1068         if (!chip->pdata->enable_current_sense)
1069                 max17042_desc = &max17042_no_current_sense_psy_desc;
1070
1071         if (chip->pdata->r_sns == 0)
1072                 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
1073
1074         if (chip->pdata->init_data)
1075                 for (i = 0; i < chip->pdata->num_init_data; i++)
1076                         regmap_write(chip->regmap,
1077                                         chip->pdata->init_data[i].addr,
1078                                         chip->pdata->init_data[i].data);
1079
1080         if (!chip->pdata->enable_current_sense) {
1081                 regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000);
1082                 regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003);
1083                 regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007);
1084         }
1085
1086         chip->battery = devm_power_supply_register(&client->dev, max17042_desc,
1087                                                    &psy_cfg);
1088         if (IS_ERR(chip->battery)) {
1089                 dev_err(&client->dev, "failed: power supply register\n");
1090                 return PTR_ERR(chip->battery);
1091         }
1092
1093         if (client->irq) {
1094                 unsigned int flags = IRQF_ONESHOT;
1095
1096                 /*
1097                  * On ACPI systems the IRQ may be handled by ACPI-event code,
1098                  * so we need to share (if the ACPI code is willing to share).
1099                  */
1100                 if (acpi_id)
1101                         flags |= IRQF_SHARED | IRQF_PROBE_SHARED;
1102
1103                 ret = devm_request_threaded_irq(&client->dev, client->irq,
1104                                                 NULL,
1105                                                 max17042_thread_handler, flags,
1106                                                 chip->battery->desc->name,
1107                                                 chip);
1108                 if (!ret) {
1109                         regmap_update_bits(chip->regmap, MAX17042_CONFIG,
1110                                         CONFIG_ALRT_BIT_ENBL,
1111                                         CONFIG_ALRT_BIT_ENBL);
1112                         max17042_set_soc_threshold(chip, 1);
1113                 } else {
1114                         client->irq = 0;
1115                         if (ret != -EBUSY)
1116                                 dev_err(&client->dev, "Failed to get IRQ\n");
1117                 }
1118         }
1119         /* Not able to update the charge threshold when exceeded? -> disable */
1120         if (!client->irq)
1121                 regmap_write(chip->regmap, MAX17042_SALRT_Th, 0xff00);
1122
1123         regmap_read(chip->regmap, MAX17042_STATUS, &val);
1124         if (val & STATUS_POR_BIT) {
1125                 INIT_WORK(&chip->work, max17042_init_worker);
1126                 schedule_work(&chip->work);
1127         } else {
1128                 chip->init_complete = 1;
1129         }
1130
1131         return 0;
1132 }
1133
1134 #ifdef CONFIG_PM_SLEEP
1135 static int max17042_suspend(struct device *dev)
1136 {
1137         struct max17042_chip *chip = dev_get_drvdata(dev);
1138
1139         /*
1140          * disable the irq and enable irq_wake
1141          * capability to the interrupt line.
1142          */
1143         if (chip->client->irq) {
1144                 disable_irq(chip->client->irq);
1145                 enable_irq_wake(chip->client->irq);
1146         }
1147
1148         return 0;
1149 }
1150
1151 static int max17042_resume(struct device *dev)
1152 {
1153         struct max17042_chip *chip = dev_get_drvdata(dev);
1154
1155         if (chip->client->irq) {
1156                 disable_irq_wake(chip->client->irq);
1157                 enable_irq(chip->client->irq);
1158                 /* re-program the SOC thresholds to 1% change */
1159                 max17042_set_soc_threshold(chip, 1);
1160         }
1161
1162         return 0;
1163 }
1164 #endif
1165
1166 static SIMPLE_DEV_PM_OPS(max17042_pm_ops, max17042_suspend,
1167                         max17042_resume);
1168
1169 #ifdef CONFIG_ACPI
1170 static const struct acpi_device_id max17042_acpi_match[] = {
1171         { "MAX17047", MAXIM_DEVICE_TYPE_MAX17047 },
1172         { }
1173 };
1174 MODULE_DEVICE_TABLE(acpi, max17042_acpi_match);
1175 #endif
1176
1177 #ifdef CONFIG_OF
1178 static const struct of_device_id max17042_dt_match[] = {
1179         { .compatible = "maxim,max17042" },
1180         { .compatible = "maxim,max17047" },
1181         { .compatible = "maxim,max17050" },
1182         { },
1183 };
1184 MODULE_DEVICE_TABLE(of, max17042_dt_match);
1185 #endif
1186
1187 static const struct i2c_device_id max17042_id[] = {
1188         { "max17042", MAXIM_DEVICE_TYPE_MAX17042 },
1189         { "max17047", MAXIM_DEVICE_TYPE_MAX17047 },
1190         { "max17050", MAXIM_DEVICE_TYPE_MAX17050 },
1191         { }
1192 };
1193 MODULE_DEVICE_TABLE(i2c, max17042_id);
1194
1195 static struct i2c_driver max17042_i2c_driver = {
1196         .driver = {
1197                 .name   = "max17042",
1198                 .acpi_match_table = ACPI_PTR(max17042_acpi_match),
1199                 .of_match_table = of_match_ptr(max17042_dt_match),
1200                 .pm     = &max17042_pm_ops,
1201         },
1202         .probe          = max17042_probe,
1203         .id_table       = max17042_id,
1204 };
1205 module_i2c_driver(max17042_i2c_driver);
1206
1207 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1208 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
1209 MODULE_LICENSE("GPL");