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