]> Git Repo - linux.git/blob - drivers/power/supply/sc27xx_fuel_gauge.c
efi/x86: add headroom to decompressor BSS to account for setup block
[linux.git] / drivers / power / supply / sc27xx_fuel_gauge.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Spreadtrum Communications Inc.
3
4 #include <linux/gpio/consumer.h>
5 #include <linux/iio/consumer.h>
6 #include <linux/interrupt.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/nvmem-consumer.h>
10 #include <linux/of.h>
11 #include <linux/platform_device.h>
12 #include <linux/power_supply.h>
13 #include <linux/regmap.h>
14 #include <linux/slab.h>
15
16 /* PMIC global control registers definition */
17 #define SC27XX_MODULE_EN0               0xc08
18 #define SC27XX_CLK_EN0                  0xc18
19 #define SC27XX_FGU_EN                   BIT(7)
20 #define SC27XX_FGU_RTC_EN               BIT(6)
21
22 /* FGU registers definition */
23 #define SC27XX_FGU_START                0x0
24 #define SC27XX_FGU_CONFIG               0x4
25 #define SC27XX_FGU_ADC_CONFIG           0x8
26 #define SC27XX_FGU_STATUS               0xc
27 #define SC27XX_FGU_INT_EN               0x10
28 #define SC27XX_FGU_INT_CLR              0x14
29 #define SC27XX_FGU_INT_STS              0x1c
30 #define SC27XX_FGU_VOLTAGE              0x20
31 #define SC27XX_FGU_OCV                  0x24
32 #define SC27XX_FGU_POCV                 0x28
33 #define SC27XX_FGU_CURRENT              0x2c
34 #define SC27XX_FGU_LOW_OVERLOAD         0x34
35 #define SC27XX_FGU_CLBCNT_SETH          0x50
36 #define SC27XX_FGU_CLBCNT_SETL          0x54
37 #define SC27XX_FGU_CLBCNT_DELTH         0x58
38 #define SC27XX_FGU_CLBCNT_DELTL         0x5c
39 #define SC27XX_FGU_CLBCNT_VALH          0x68
40 #define SC27XX_FGU_CLBCNT_VALL          0x6c
41 #define SC27XX_FGU_CLBCNT_QMAXL         0x74
42 #define SC27XX_FGU_USER_AREA_SET        0xa0
43 #define SC27XX_FGU_USER_AREA_CLEAR      0xa4
44 #define SC27XX_FGU_USER_AREA_STATUS     0xa8
45
46 #define SC27XX_WRITE_SELCLB_EN          BIT(0)
47 #define SC27XX_FGU_CLBCNT_MASK          GENMASK(15, 0)
48 #define SC27XX_FGU_CLBCNT_SHIFT         16
49 #define SC27XX_FGU_LOW_OVERLOAD_MASK    GENMASK(12, 0)
50
51 #define SC27XX_FGU_INT_MASK             GENMASK(9, 0)
52 #define SC27XX_FGU_LOW_OVERLOAD_INT     BIT(0)
53 #define SC27XX_FGU_CLBCNT_DELTA_INT     BIT(2)
54
55 #define SC27XX_FGU_MODE_AREA_MASK       GENMASK(15, 12)
56 #define SC27XX_FGU_CAP_AREA_MASK        GENMASK(11, 0)
57 #define SC27XX_FGU_MODE_AREA_SHIFT      12
58
59 #define SC27XX_FGU_FIRST_POWERTON       GENMASK(3, 0)
60 #define SC27XX_FGU_DEFAULT_CAP          GENMASK(11, 0)
61 #define SC27XX_FGU_NORMAIL_POWERTON     0x5
62
63 #define SC27XX_FGU_CUR_BASIC_ADC        8192
64 #define SC27XX_FGU_SAMPLE_HZ            2
65 /* micro Ohms */
66 #define SC27XX_FGU_IDEAL_RESISTANCE     20000
67
68 /*
69  * struct sc27xx_fgu_data: describe the FGU device
70  * @regmap: regmap for register access
71  * @dev: platform device
72  * @battery: battery power supply
73  * @base: the base offset for the controller
74  * @lock: protect the structure
75  * @gpiod: GPIO for battery detection
76  * @channel: IIO channel to get battery temperature
77  * @charge_chan: IIO channel to get charge voltage
78  * @internal_resist: the battery internal resistance in mOhm
79  * @total_cap: the total capacity of the battery in mAh
80  * @init_cap: the initial capacity of the battery in mAh
81  * @alarm_cap: the alarm capacity
82  * @init_clbcnt: the initial coulomb counter
83  * @max_volt: the maximum constant input voltage in millivolt
84  * @min_volt: the minimum drained battery voltage in microvolt
85  * @table_len: the capacity table length
86  * @resist_table_len: the resistance table length
87  * @cur_1000ma_adc: ADC value corresponding to 1000 mA
88  * @vol_1000mv_adc: ADC value corresponding to 1000 mV
89  * @calib_resist: the real resistance of coulomb counter chip in uOhm
90  * @cap_table: capacity table with corresponding ocv
91  * @resist_table: resistance percent table with corresponding temperature
92  */
93 struct sc27xx_fgu_data {
94         struct regmap *regmap;
95         struct device *dev;
96         struct power_supply *battery;
97         u32 base;
98         struct mutex lock;
99         struct gpio_desc *gpiod;
100         struct iio_channel *channel;
101         struct iio_channel *charge_chan;
102         bool bat_present;
103         int internal_resist;
104         int total_cap;
105         int init_cap;
106         int alarm_cap;
107         int init_clbcnt;
108         int max_volt;
109         int min_volt;
110         int table_len;
111         int resist_table_len;
112         int cur_1000ma_adc;
113         int vol_1000mv_adc;
114         int calib_resist;
115         struct power_supply_battery_ocv_table *cap_table;
116         struct power_supply_resistance_temp_table *resist_table;
117 };
118
119 static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity);
120 static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data,
121                                             int cap, bool int_mode);
122 static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap);
123 static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data *data, int *temp);
124
125 static const char * const sc27xx_charger_supply_name[] = {
126         "sc2731_charger",
127         "sc2720_charger",
128         "sc2721_charger",
129         "sc2723_charger",
130 };
131
132 static int sc27xx_fgu_adc_to_current(struct sc27xx_fgu_data *data, int adc)
133 {
134         return DIV_ROUND_CLOSEST(adc * 1000, data->cur_1000ma_adc);
135 }
136
137 static int sc27xx_fgu_adc_to_voltage(struct sc27xx_fgu_data *data, int adc)
138 {
139         return DIV_ROUND_CLOSEST(adc * 1000, data->vol_1000mv_adc);
140 }
141
142 static int sc27xx_fgu_voltage_to_adc(struct sc27xx_fgu_data *data, int vol)
143 {
144         return DIV_ROUND_CLOSEST(vol * data->vol_1000mv_adc, 1000);
145 }
146
147 static bool sc27xx_fgu_is_first_poweron(struct sc27xx_fgu_data *data)
148 {
149         int ret, status, cap, mode;
150
151         ret = regmap_read(data->regmap,
152                           data->base + SC27XX_FGU_USER_AREA_STATUS, &status);
153         if (ret)
154                 return false;
155
156         /*
157          * We use low 4 bits to save the last battery capacity and high 12 bits
158          * to save the system boot mode.
159          */
160         mode = (status & SC27XX_FGU_MODE_AREA_MASK) >> SC27XX_FGU_MODE_AREA_SHIFT;
161         cap = status & SC27XX_FGU_CAP_AREA_MASK;
162
163         /*
164          * When FGU has been powered down, the user area registers became
165          * default value (0xffff), which can be used to valid if the system is
166          * first power on or not.
167          */
168         if (mode == SC27XX_FGU_FIRST_POWERTON || cap == SC27XX_FGU_DEFAULT_CAP)
169                 return true;
170
171         return false;
172 }
173
174 static int sc27xx_fgu_save_boot_mode(struct sc27xx_fgu_data *data,
175                                      int boot_mode)
176 {
177         int ret;
178
179         ret = regmap_update_bits(data->regmap,
180                                  data->base + SC27XX_FGU_USER_AREA_CLEAR,
181                                  SC27XX_FGU_MODE_AREA_MASK,
182                                  SC27XX_FGU_MODE_AREA_MASK);
183         if (ret)
184                 return ret;
185
186         /*
187          * Since the user area registers are put on power always-on region,
188          * then these registers changing time will be a little long. Thus
189          * here we should delay 200us to wait until values are updated
190          * successfully according to the datasheet.
191          */
192         udelay(200);
193
194         ret = regmap_update_bits(data->regmap,
195                                  data->base + SC27XX_FGU_USER_AREA_SET,
196                                  SC27XX_FGU_MODE_AREA_MASK,
197                                  boot_mode << SC27XX_FGU_MODE_AREA_SHIFT);
198         if (ret)
199                 return ret;
200
201         /*
202          * Since the user area registers are put on power always-on region,
203          * then these registers changing time will be a little long. Thus
204          * here we should delay 200us to wait until values are updated
205          * successfully according to the datasheet.
206          */
207         udelay(200);
208
209         /*
210          * According to the datasheet, we should set the USER_AREA_CLEAR to 0 to
211          * make the user area data available, otherwise we can not save the user
212          * area data.
213          */
214         return regmap_update_bits(data->regmap,
215                                   data->base + SC27XX_FGU_USER_AREA_CLEAR,
216                                   SC27XX_FGU_MODE_AREA_MASK, 0);
217 }
218
219 static int sc27xx_fgu_save_last_cap(struct sc27xx_fgu_data *data, int cap)
220 {
221         int ret;
222
223         ret = regmap_update_bits(data->regmap,
224                                  data->base + SC27XX_FGU_USER_AREA_CLEAR,
225                                  SC27XX_FGU_CAP_AREA_MASK,
226                                  SC27XX_FGU_CAP_AREA_MASK);
227         if (ret)
228                 return ret;
229
230         /*
231          * Since the user area registers are put on power always-on region,
232          * then these registers changing time will be a little long. Thus
233          * here we should delay 200us to wait until values are updated
234          * successfully according to the datasheet.
235          */
236         udelay(200);
237
238         ret = regmap_update_bits(data->regmap,
239                                  data->base + SC27XX_FGU_USER_AREA_SET,
240                                  SC27XX_FGU_CAP_AREA_MASK, cap);
241         if (ret)
242                 return ret;
243
244         /*
245          * Since the user area registers are put on power always-on region,
246          * then these registers changing time will be a little long. Thus
247          * here we should delay 200us to wait until values are updated
248          * successfully according to the datasheet.
249          */
250         udelay(200);
251
252         /*
253          * According to the datasheet, we should set the USER_AREA_CLEAR to 0 to
254          * make the user area data available, otherwise we can not save the user
255          * area data.
256          */
257         return regmap_update_bits(data->regmap,
258                                   data->base + SC27XX_FGU_USER_AREA_CLEAR,
259                                   SC27XX_FGU_CAP_AREA_MASK, 0);
260 }
261
262 static int sc27xx_fgu_read_last_cap(struct sc27xx_fgu_data *data, int *cap)
263 {
264         int ret, value;
265
266         ret = regmap_read(data->regmap,
267                           data->base + SC27XX_FGU_USER_AREA_STATUS, &value);
268         if (ret)
269                 return ret;
270
271         *cap = value & SC27XX_FGU_CAP_AREA_MASK;
272         return 0;
273 }
274
275 /*
276  * When system boots on, we can not read battery capacity from coulomb
277  * registers, since now the coulomb registers are invalid. So we should
278  * calculate the battery open circuit voltage, and get current battery
279  * capacity according to the capacity table.
280  */
281 static int sc27xx_fgu_get_boot_capacity(struct sc27xx_fgu_data *data, int *cap)
282 {
283         int volt, cur, oci, ocv, ret;
284         bool is_first_poweron = sc27xx_fgu_is_first_poweron(data);
285
286         /*
287          * If system is not the first power on, we should use the last saved
288          * battery capacity as the initial battery capacity. Otherwise we should
289          * re-calculate the initial battery capacity.
290          */
291         if (!is_first_poweron) {
292                 ret = sc27xx_fgu_read_last_cap(data, cap);
293                 if (ret)
294                         return ret;
295
296                 return sc27xx_fgu_save_boot_mode(data, SC27XX_FGU_NORMAIL_POWERTON);
297         }
298
299         /*
300          * After system booting on, the SC27XX_FGU_CLBCNT_QMAXL register saved
301          * the first sampled open circuit current.
302          */
303         ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_QMAXL,
304                           &cur);
305         if (ret)
306                 return ret;
307
308         cur <<= 1;
309         oci = sc27xx_fgu_adc_to_current(data, cur - SC27XX_FGU_CUR_BASIC_ADC);
310
311         /*
312          * Should get the OCV from SC27XX_FGU_POCV register at the system
313          * beginning. It is ADC values reading from registers which need to
314          * convert the corresponding voltage.
315          */
316         ret = regmap_read(data->regmap, data->base + SC27XX_FGU_POCV, &volt);
317         if (ret)
318                 return ret;
319
320         volt = sc27xx_fgu_adc_to_voltage(data, volt);
321         ocv = volt * 1000 - oci * data->internal_resist;
322
323         /*
324          * Parse the capacity table to look up the correct capacity percent
325          * according to current battery's corresponding OCV values.
326          */
327         *cap = power_supply_ocv2cap_simple(data->cap_table, data->table_len,
328                                            ocv);
329
330         ret = sc27xx_fgu_save_last_cap(data, *cap);
331         if (ret)
332                 return ret;
333
334         return sc27xx_fgu_save_boot_mode(data, SC27XX_FGU_NORMAIL_POWERTON);
335 }
336
337 static int sc27xx_fgu_set_clbcnt(struct sc27xx_fgu_data *data, int clbcnt)
338 {
339         int ret;
340
341         ret = regmap_update_bits(data->regmap,
342                                  data->base + SC27XX_FGU_CLBCNT_SETL,
343                                  SC27XX_FGU_CLBCNT_MASK, clbcnt);
344         if (ret)
345                 return ret;
346
347         ret = regmap_update_bits(data->regmap,
348                                  data->base + SC27XX_FGU_CLBCNT_SETH,
349                                  SC27XX_FGU_CLBCNT_MASK,
350                                  clbcnt >> SC27XX_FGU_CLBCNT_SHIFT);
351         if (ret)
352                 return ret;
353
354         return regmap_update_bits(data->regmap, data->base + SC27XX_FGU_START,
355                                  SC27XX_WRITE_SELCLB_EN,
356                                  SC27XX_WRITE_SELCLB_EN);
357 }
358
359 static int sc27xx_fgu_get_clbcnt(struct sc27xx_fgu_data *data, int *clb_cnt)
360 {
361         int ccl, cch, ret;
362
363         ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_VALL,
364                           &ccl);
365         if (ret)
366                 return ret;
367
368         ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_VALH,
369                           &cch);
370         if (ret)
371                 return ret;
372
373         *clb_cnt = ccl & SC27XX_FGU_CLBCNT_MASK;
374         *clb_cnt |= (cch & SC27XX_FGU_CLBCNT_MASK) << SC27XX_FGU_CLBCNT_SHIFT;
375
376         return 0;
377 }
378
379 static int sc27xx_fgu_get_capacity(struct sc27xx_fgu_data *data, int *cap)
380 {
381         int ret, cur_clbcnt, delta_clbcnt, delta_cap, temp;
382
383         /* Get current coulomb counters firstly */
384         ret = sc27xx_fgu_get_clbcnt(data, &cur_clbcnt);
385         if (ret)
386                 return ret;
387
388         delta_clbcnt = cur_clbcnt - data->init_clbcnt;
389
390         /*
391          * Convert coulomb counter to delta capacity (mAh), and set multiplier
392          * as 10 to improve the precision.
393          */
394         temp = DIV_ROUND_CLOSEST(delta_clbcnt * 10, 36 * SC27XX_FGU_SAMPLE_HZ);
395         temp = sc27xx_fgu_adc_to_current(data, temp / 1000);
396
397         /*
398          * Convert to capacity percent of the battery total capacity,
399          * and multiplier is 100 too.
400          */
401         delta_cap = DIV_ROUND_CLOSEST(temp * 100, data->total_cap);
402         *cap = delta_cap + data->init_cap;
403
404         /* Calibrate the battery capacity in a normal range. */
405         sc27xx_fgu_capacity_calibration(data, *cap, false);
406
407         return 0;
408 }
409
410 static int sc27xx_fgu_get_vbat_vol(struct sc27xx_fgu_data *data, int *val)
411 {
412         int ret, vol;
413
414         ret = regmap_read(data->regmap, data->base + SC27XX_FGU_VOLTAGE, &vol);
415         if (ret)
416                 return ret;
417
418         /*
419          * It is ADC values reading from registers which need to convert to
420          * corresponding voltage values.
421          */
422         *val = sc27xx_fgu_adc_to_voltage(data, vol);
423
424         return 0;
425 }
426
427 static int sc27xx_fgu_get_current(struct sc27xx_fgu_data *data, int *val)
428 {
429         int ret, cur;
430
431         ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CURRENT, &cur);
432         if (ret)
433                 return ret;
434
435         /*
436          * It is ADC values reading from registers which need to convert to
437          * corresponding current values.
438          */
439         *val = sc27xx_fgu_adc_to_current(data, cur - SC27XX_FGU_CUR_BASIC_ADC);
440
441         return 0;
442 }
443
444 static int sc27xx_fgu_get_vbat_ocv(struct sc27xx_fgu_data *data, int *val)
445 {
446         int vol, cur, ret, temp, resistance;
447
448         ret = sc27xx_fgu_get_vbat_vol(data, &vol);
449         if (ret)
450                 return ret;
451
452         ret = sc27xx_fgu_get_current(data, &cur);
453         if (ret)
454                 return ret;
455
456         resistance = data->internal_resist;
457         if (data->resist_table_len > 0) {
458                 ret = sc27xx_fgu_get_temp(data, &temp);
459                 if (ret)
460                         return ret;
461
462                 resistance = power_supply_temp2resist_simple(data->resist_table,
463                                                 data->resist_table_len, temp);
464                 resistance = data->internal_resist * resistance / 100;
465         }
466
467         /* Return the battery OCV in micro volts. */
468         *val = vol * 1000 - cur * resistance;
469
470         return 0;
471 }
472
473 static int sc27xx_fgu_get_charge_vol(struct sc27xx_fgu_data *data, int *val)
474 {
475         int ret, vol;
476
477         ret = iio_read_channel_processed(data->charge_chan, &vol);
478         if (ret < 0)
479                 return ret;
480
481         *val = vol * 1000;
482         return 0;
483 }
484
485 static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data *data, int *temp)
486 {
487         return iio_read_channel_processed(data->channel, temp);
488 }
489
490 static int sc27xx_fgu_get_health(struct sc27xx_fgu_data *data, int *health)
491 {
492         int ret, vol;
493
494         ret = sc27xx_fgu_get_vbat_vol(data, &vol);
495         if (ret)
496                 return ret;
497
498         if (vol > data->max_volt)
499                 *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
500         else
501                 *health = POWER_SUPPLY_HEALTH_GOOD;
502
503         return 0;
504 }
505
506 static int sc27xx_fgu_get_status(struct sc27xx_fgu_data *data, int *status)
507 {
508         union power_supply_propval val;
509         struct power_supply *psy;
510         int i, ret = -EINVAL;
511
512         for (i = 0; i < ARRAY_SIZE(sc27xx_charger_supply_name); i++) {
513                 psy = power_supply_get_by_name(sc27xx_charger_supply_name[i]);
514                 if (!psy)
515                         continue;
516
517                 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS,
518                                                 &val);
519                 power_supply_put(psy);
520                 if (ret)
521                         return ret;
522
523                 *status = val.intval;
524         }
525
526         return ret;
527 }
528
529 static int sc27xx_fgu_get_property(struct power_supply *psy,
530                                    enum power_supply_property psp,
531                                    union power_supply_propval *val)
532 {
533         struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
534         int ret = 0;
535         int value;
536
537         mutex_lock(&data->lock);
538
539         switch (psp) {
540         case POWER_SUPPLY_PROP_STATUS:
541                 ret = sc27xx_fgu_get_status(data, &value);
542                 if (ret)
543                         goto error;
544
545                 val->intval = value;
546                 break;
547
548         case POWER_SUPPLY_PROP_HEALTH:
549                 ret = sc27xx_fgu_get_health(data, &value);
550                 if (ret)
551                         goto error;
552
553                 val->intval = value;
554                 break;
555
556         case POWER_SUPPLY_PROP_PRESENT:
557                 val->intval = data->bat_present;
558                 break;
559
560         case POWER_SUPPLY_PROP_TEMP:
561                 ret = sc27xx_fgu_get_temp(data, &value);
562                 if (ret)
563                         goto error;
564
565                 val->intval = value;
566                 break;
567
568         case POWER_SUPPLY_PROP_TECHNOLOGY:
569                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
570                 break;
571
572         case POWER_SUPPLY_PROP_CAPACITY:
573                 ret = sc27xx_fgu_get_capacity(data, &value);
574                 if (ret)
575                         goto error;
576
577                 val->intval = value;
578                 break;
579
580         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
581                 ret = sc27xx_fgu_get_vbat_vol(data, &value);
582                 if (ret)
583                         goto error;
584
585                 val->intval = value * 1000;
586                 break;
587
588         case POWER_SUPPLY_PROP_VOLTAGE_OCV:
589                 ret = sc27xx_fgu_get_vbat_ocv(data, &value);
590                 if (ret)
591                         goto error;
592
593                 val->intval = value;
594                 break;
595
596         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
597                 ret = sc27xx_fgu_get_charge_vol(data, &value);
598                 if (ret)
599                         goto error;
600
601                 val->intval = value;
602                 break;
603
604         case POWER_SUPPLY_PROP_CURRENT_NOW:
605         case POWER_SUPPLY_PROP_CURRENT_AVG:
606                 ret = sc27xx_fgu_get_current(data, &value);
607                 if (ret)
608                         goto error;
609
610                 val->intval = value * 1000;
611                 break;
612
613         case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
614                 val->intval = data->total_cap * 1000;
615                 break;
616
617         default:
618                 ret = -EINVAL;
619                 break;
620         }
621
622 error:
623         mutex_unlock(&data->lock);
624         return ret;
625 }
626
627 static int sc27xx_fgu_set_property(struct power_supply *psy,
628                                    enum power_supply_property psp,
629                                    const union power_supply_propval *val)
630 {
631         struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
632         int ret;
633
634         mutex_lock(&data->lock);
635
636         switch (psp) {
637         case POWER_SUPPLY_PROP_CAPACITY:
638                 ret = sc27xx_fgu_save_last_cap(data, val->intval);
639                 if (ret < 0)
640                         dev_err(data->dev, "failed to save battery capacity\n");
641                 break;
642
643         case POWER_SUPPLY_PROP_CALIBRATE:
644                 sc27xx_fgu_adjust_cap(data, val->intval);
645                 ret = 0;
646                 break;
647
648         default:
649                 ret = -EINVAL;
650         }
651
652         mutex_unlock(&data->lock);
653
654         return ret;
655 }
656
657 static void sc27xx_fgu_external_power_changed(struct power_supply *psy)
658 {
659         struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
660
661         power_supply_changed(data->battery);
662 }
663
664 static int sc27xx_fgu_property_is_writeable(struct power_supply *psy,
665                                             enum power_supply_property psp)
666 {
667         return psp == POWER_SUPPLY_PROP_CAPACITY ||
668                 psp == POWER_SUPPLY_PROP_CALIBRATE;
669 }
670
671 static enum power_supply_property sc27xx_fgu_props[] = {
672         POWER_SUPPLY_PROP_STATUS,
673         POWER_SUPPLY_PROP_HEALTH,
674         POWER_SUPPLY_PROP_PRESENT,
675         POWER_SUPPLY_PROP_TEMP,
676         POWER_SUPPLY_PROP_TECHNOLOGY,
677         POWER_SUPPLY_PROP_CAPACITY,
678         POWER_SUPPLY_PROP_VOLTAGE_NOW,
679         POWER_SUPPLY_PROP_VOLTAGE_OCV,
680         POWER_SUPPLY_PROP_CURRENT_NOW,
681         POWER_SUPPLY_PROP_CURRENT_AVG,
682         POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
683         POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
684         POWER_SUPPLY_PROP_CALIBRATE,
685 };
686
687 static const struct power_supply_desc sc27xx_fgu_desc = {
688         .name                   = "sc27xx-fgu",
689         .type                   = POWER_SUPPLY_TYPE_BATTERY,
690         .properties             = sc27xx_fgu_props,
691         .num_properties         = ARRAY_SIZE(sc27xx_fgu_props),
692         .get_property           = sc27xx_fgu_get_property,
693         .set_property           = sc27xx_fgu_set_property,
694         .external_power_changed = sc27xx_fgu_external_power_changed,
695         .property_is_writeable  = sc27xx_fgu_property_is_writeable,
696 };
697
698 static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap)
699 {
700         int ret;
701
702         data->init_cap = cap;
703         ret = sc27xx_fgu_get_clbcnt(data, &data->init_clbcnt);
704         if (ret)
705                 dev_err(data->dev, "failed to get init coulomb counter\n");
706 }
707
708 static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data,
709                                             int cap, bool int_mode)
710 {
711         int ret, ocv, chg_sts, adc;
712
713         ret = sc27xx_fgu_get_vbat_ocv(data, &ocv);
714         if (ret) {
715                 dev_err(data->dev, "get battery ocv error.\n");
716                 return;
717         }
718
719         ret = sc27xx_fgu_get_status(data, &chg_sts);
720         if (ret) {
721                 dev_err(data->dev, "get charger status error.\n");
722                 return;
723         }
724
725         /*
726          * If we are in charging mode, then we do not need to calibrate the
727          * lower capacity.
728          */
729         if (chg_sts == POWER_SUPPLY_STATUS_CHARGING)
730                 return;
731
732         if ((ocv > data->cap_table[0].ocv && cap < 100) || cap > 100) {
733                 /*
734                  * If current OCV value is larger than the max OCV value in
735                  * OCV table, or the current capacity is larger than 100,
736                  * we should force the inititial capacity to 100.
737                  */
738                 sc27xx_fgu_adjust_cap(data, 100);
739         } else if (ocv <= data->cap_table[data->table_len - 1].ocv) {
740                 /*
741                  * If current OCV value is leass than the minimum OCV value in
742                  * OCV table, we should force the inititial capacity to 0.
743                  */
744                 sc27xx_fgu_adjust_cap(data, 0);
745         } else if ((ocv > data->cap_table[data->table_len - 1].ocv && cap <= 0) ||
746                    (ocv > data->min_volt && cap <= data->alarm_cap)) {
747                 /*
748                  * If current OCV value is not matchable with current capacity,
749                  * we should re-calculate current capacity by looking up the
750                  * OCV table.
751                  */
752                 int cur_cap = power_supply_ocv2cap_simple(data->cap_table,
753                                                           data->table_len, ocv);
754
755                 sc27xx_fgu_adjust_cap(data, cur_cap);
756         } else if (ocv <= data->min_volt) {
757                 /*
758                  * If current OCV value is less than the low alarm voltage, but
759                  * current capacity is larger than the alarm capacity, we should
760                  * adjust the inititial capacity to alarm capacity.
761                  */
762                 if (cap > data->alarm_cap) {
763                         sc27xx_fgu_adjust_cap(data, data->alarm_cap);
764                 } else {
765                         int cur_cap;
766
767                         /*
768                          * If current capacity is equal with 0 or less than 0
769                          * (some error occurs), we should adjust inititial
770                          * capacity to the capacity corresponding to current OCV
771                          * value.
772                          */
773                         cur_cap = power_supply_ocv2cap_simple(data->cap_table,
774                                                               data->table_len,
775                                                               ocv);
776                         sc27xx_fgu_adjust_cap(data, cur_cap);
777                 }
778
779                 if (!int_mode)
780                         return;
781
782                 /*
783                  * After adjusting the battery capacity, we should set the
784                  * lowest alarm voltage instead.
785                  */
786                 data->min_volt = data->cap_table[data->table_len - 1].ocv;
787                 data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table,
788                                                               data->table_len,
789                                                               data->min_volt);
790
791                 adc = sc27xx_fgu_voltage_to_adc(data, data->min_volt / 1000);
792                 regmap_update_bits(data->regmap,
793                                    data->base + SC27XX_FGU_LOW_OVERLOAD,
794                                    SC27XX_FGU_LOW_OVERLOAD_MASK, adc);
795         }
796 }
797
798 static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id)
799 {
800         struct sc27xx_fgu_data *data = dev_id;
801         int ret, cap;
802         u32 status;
803
804         mutex_lock(&data->lock);
805
806         ret = regmap_read(data->regmap, data->base + SC27XX_FGU_INT_STS,
807                           &status);
808         if (ret)
809                 goto out;
810
811         ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR,
812                                  status, status);
813         if (ret)
814                 goto out;
815
816         /*
817          * When low overload voltage interrupt happens, we should calibrate the
818          * battery capacity in lower voltage stage.
819          */
820         if (!(status & SC27XX_FGU_LOW_OVERLOAD_INT))
821                 goto out;
822
823         ret = sc27xx_fgu_get_capacity(data, &cap);
824         if (ret)
825                 goto out;
826
827         sc27xx_fgu_capacity_calibration(data, cap, true);
828
829 out:
830         mutex_unlock(&data->lock);
831
832         power_supply_changed(data->battery);
833         return IRQ_HANDLED;
834 }
835
836 static irqreturn_t sc27xx_fgu_bat_detection(int irq, void *dev_id)
837 {
838         struct sc27xx_fgu_data *data = dev_id;
839         int state;
840
841         mutex_lock(&data->lock);
842
843         state = gpiod_get_value_cansleep(data->gpiod);
844         if (state < 0) {
845                 dev_err(data->dev, "failed to get gpio state\n");
846                 mutex_unlock(&data->lock);
847                 return IRQ_RETVAL(state);
848         }
849
850         data->bat_present = !!state;
851
852         mutex_unlock(&data->lock);
853
854         power_supply_changed(data->battery);
855         return IRQ_HANDLED;
856 }
857
858 static void sc27xx_fgu_disable(void *_data)
859 {
860         struct sc27xx_fgu_data *data = _data;
861
862         regmap_update_bits(data->regmap, SC27XX_CLK_EN0, SC27XX_FGU_RTC_EN, 0);
863         regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, SC27XX_FGU_EN, 0);
864 }
865
866 static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity)
867 {
868         /*
869          * Get current capacity (mAh) = battery total capacity (mAh) *
870          * current capacity percent (capacity / 100).
871          */
872         int cur_cap = DIV_ROUND_CLOSEST(data->total_cap * capacity, 100);
873
874         /*
875          * Convert current capacity (mAh) to coulomb counter according to the
876          * formula: 1 mAh =3.6 coulomb.
877          */
878         return DIV_ROUND_CLOSEST(cur_cap * 36 * data->cur_1000ma_adc * SC27XX_FGU_SAMPLE_HZ, 10);
879 }
880
881 static int sc27xx_fgu_calibration(struct sc27xx_fgu_data *data)
882 {
883         struct nvmem_cell *cell;
884         int calib_data, cal_4200mv;
885         void *buf;
886         size_t len;
887
888         cell = nvmem_cell_get(data->dev, "fgu_calib");
889         if (IS_ERR(cell))
890                 return PTR_ERR(cell);
891
892         buf = nvmem_cell_read(cell, &len);
893         nvmem_cell_put(cell);
894
895         if (IS_ERR(buf))
896                 return PTR_ERR(buf);
897
898         memcpy(&calib_data, buf, min(len, sizeof(u32)));
899
900         /*
901          * Get the ADC value corresponding to 4200 mV from eFuse controller
902          * according to below formula. Then convert to ADC values corresponding
903          * to 1000 mV and 1000 mA.
904          */
905         cal_4200mv = (calib_data & 0x1ff) + 6963 - 4096 - 256;
906         data->vol_1000mv_adc = DIV_ROUND_CLOSEST(cal_4200mv * 10, 42);
907         data->cur_1000ma_adc =
908                 DIV_ROUND_CLOSEST(data->vol_1000mv_adc * 4 * data->calib_resist,
909                                   SC27XX_FGU_IDEAL_RESISTANCE);
910
911         kfree(buf);
912         return 0;
913 }
914
915 static int sc27xx_fgu_hw_init(struct sc27xx_fgu_data *data)
916 {
917         struct power_supply_battery_info info = { };
918         struct power_supply_battery_ocv_table *table;
919         int ret, delta_clbcnt, alarm_adc;
920
921         ret = power_supply_get_battery_info(data->battery, &info);
922         if (ret) {
923                 dev_err(data->dev, "failed to get battery information\n");
924                 return ret;
925         }
926
927         data->total_cap = info.charge_full_design_uah / 1000;
928         data->max_volt = info.constant_charge_voltage_max_uv / 1000;
929         data->internal_resist = info.factory_internal_resistance_uohm / 1000;
930         data->min_volt = info.voltage_min_design_uv;
931
932         /*
933          * For SC27XX fuel gauge device, we only use one ocv-capacity
934          * table in normal temperature 20 Celsius.
935          */
936         table = power_supply_find_ocv2cap_table(&info, 20, &data->table_len);
937         if (!table)
938                 return -EINVAL;
939
940         data->cap_table = devm_kmemdup(data->dev, table,
941                                        data->table_len * sizeof(*table),
942                                        GFP_KERNEL);
943         if (!data->cap_table) {
944                 power_supply_put_battery_info(data->battery, &info);
945                 return -ENOMEM;
946         }
947
948         data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table,
949                                                       data->table_len,
950                                                       data->min_volt);
951         if (!data->alarm_cap)
952                 data->alarm_cap += 1;
953
954         data->resist_table_len = info.resist_table_size;
955         if (data->resist_table_len > 0) {
956                 data->resist_table = devm_kmemdup(data->dev, info.resist_table,
957                                                   data->resist_table_len *
958                                                   sizeof(struct power_supply_resistance_temp_table),
959                                                   GFP_KERNEL);
960                 if (!data->resist_table) {
961                         power_supply_put_battery_info(data->battery, &info);
962                         return -ENOMEM;
963                 }
964         }
965
966         power_supply_put_battery_info(data->battery, &info);
967
968         ret = sc27xx_fgu_calibration(data);
969         if (ret)
970                 return ret;
971
972         /* Enable the FGU module */
973         ret = regmap_update_bits(data->regmap, SC27XX_MODULE_EN0,
974                                  SC27XX_FGU_EN, SC27XX_FGU_EN);
975         if (ret) {
976                 dev_err(data->dev, "failed to enable fgu\n");
977                 return ret;
978         }
979
980         /* Enable the FGU RTC clock to make it work */
981         ret = regmap_update_bits(data->regmap, SC27XX_CLK_EN0,
982                                  SC27XX_FGU_RTC_EN, SC27XX_FGU_RTC_EN);
983         if (ret) {
984                 dev_err(data->dev, "failed to enable fgu RTC clock\n");
985                 goto disable_fgu;
986         }
987
988         ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR,
989                                  SC27XX_FGU_INT_MASK, SC27XX_FGU_INT_MASK);
990         if (ret) {
991                 dev_err(data->dev, "failed to clear interrupt status\n");
992                 goto disable_clk;
993         }
994
995         /*
996          * Set the voltage low overload threshold, which means when the battery
997          * voltage is lower than this threshold, the controller will generate
998          * one interrupt to notify.
999          */
1000         alarm_adc = sc27xx_fgu_voltage_to_adc(data, data->min_volt / 1000);
1001         ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_LOW_OVERLOAD,
1002                                  SC27XX_FGU_LOW_OVERLOAD_MASK, alarm_adc);
1003         if (ret) {
1004                 dev_err(data->dev, "failed to set fgu low overload\n");
1005                 goto disable_clk;
1006         }
1007
1008         /*
1009          * Set the coulomb counter delta threshold, that means when the coulomb
1010          * counter change is multiples of the delta threshold, the controller
1011          * will generate one interrupt to notify the users to update the battery
1012          * capacity. Now we set the delta threshold as a counter value of 1%
1013          * capacity.
1014          */
1015         delta_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, 1);
1016
1017         ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_CLBCNT_DELTL,
1018                                  SC27XX_FGU_CLBCNT_MASK, delta_clbcnt);
1019         if (ret) {
1020                 dev_err(data->dev, "failed to set low delta coulomb counter\n");
1021                 goto disable_clk;
1022         }
1023
1024         ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_CLBCNT_DELTH,
1025                                  SC27XX_FGU_CLBCNT_MASK,
1026                                  delta_clbcnt >> SC27XX_FGU_CLBCNT_SHIFT);
1027         if (ret) {
1028                 dev_err(data->dev, "failed to set high delta coulomb counter\n");
1029                 goto disable_clk;
1030         }
1031
1032         /*
1033          * Get the boot battery capacity when system powers on, which is used to
1034          * initialize the coulomb counter. After that, we can read the coulomb
1035          * counter to measure the battery capacity.
1036          */
1037         ret = sc27xx_fgu_get_boot_capacity(data, &data->init_cap);
1038         if (ret) {
1039                 dev_err(data->dev, "failed to get boot capacity\n");
1040                 goto disable_clk;
1041         }
1042
1043         /*
1044          * Convert battery capacity to the corresponding initial coulomb counter
1045          * and set into coulomb counter registers.
1046          */
1047         data->init_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, data->init_cap);
1048         ret = sc27xx_fgu_set_clbcnt(data, data->init_clbcnt);
1049         if (ret) {
1050                 dev_err(data->dev, "failed to initialize coulomb counter\n");
1051                 goto disable_clk;
1052         }
1053
1054         return 0;
1055
1056 disable_clk:
1057         regmap_update_bits(data->regmap, SC27XX_CLK_EN0, SC27XX_FGU_RTC_EN, 0);
1058 disable_fgu:
1059         regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, SC27XX_FGU_EN, 0);
1060
1061         return ret;
1062 }
1063
1064 static int sc27xx_fgu_probe(struct platform_device *pdev)
1065 {
1066         struct device *dev = &pdev->dev;
1067         struct device_node *np = dev->of_node;
1068         struct power_supply_config fgu_cfg = { };
1069         struct sc27xx_fgu_data *data;
1070         int ret, irq;
1071
1072         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1073         if (!data)
1074                 return -ENOMEM;
1075
1076         data->regmap = dev_get_regmap(dev->parent, NULL);
1077         if (!data->regmap) {
1078                 dev_err(dev, "failed to get regmap\n");
1079                 return -ENODEV;
1080         }
1081
1082         ret = device_property_read_u32(dev, "reg", &data->base);
1083         if (ret) {
1084                 dev_err(dev, "failed to get fgu address\n");
1085                 return ret;
1086         }
1087
1088         ret = device_property_read_u32(&pdev->dev,
1089                                        "sprd,calib-resistance-micro-ohms",
1090                                        &data->calib_resist);
1091         if (ret) {
1092                 dev_err(&pdev->dev,
1093                         "failed to get fgu calibration resistance\n");
1094                 return ret;
1095         }
1096
1097         data->channel = devm_iio_channel_get(dev, "bat-temp");
1098         if (IS_ERR(data->channel)) {
1099                 dev_err(dev, "failed to get IIO channel\n");
1100                 return PTR_ERR(data->channel);
1101         }
1102
1103         data->charge_chan = devm_iio_channel_get(dev, "charge-vol");
1104         if (IS_ERR(data->charge_chan)) {
1105                 dev_err(dev, "failed to get charge IIO channel\n");
1106                 return PTR_ERR(data->charge_chan);
1107         }
1108
1109         data->gpiod = devm_gpiod_get(dev, "bat-detect", GPIOD_IN);
1110         if (IS_ERR(data->gpiod)) {
1111                 dev_err(dev, "failed to get battery detection GPIO\n");
1112                 return PTR_ERR(data->gpiod);
1113         }
1114
1115         ret = gpiod_get_value_cansleep(data->gpiod);
1116         if (ret < 0) {
1117                 dev_err(dev, "failed to get gpio state\n");
1118                 return ret;
1119         }
1120
1121         data->bat_present = !!ret;
1122         mutex_init(&data->lock);
1123         data->dev = dev;
1124         platform_set_drvdata(pdev, data);
1125
1126         fgu_cfg.drv_data = data;
1127         fgu_cfg.of_node = np;
1128         data->battery = devm_power_supply_register(dev, &sc27xx_fgu_desc,
1129                                                    &fgu_cfg);
1130         if (IS_ERR(data->battery)) {
1131                 dev_err(dev, "failed to register power supply\n");
1132                 return PTR_ERR(data->battery);
1133         }
1134
1135         ret = sc27xx_fgu_hw_init(data);
1136         if (ret) {
1137                 dev_err(dev, "failed to initialize fgu hardware\n");
1138                 return ret;
1139         }
1140
1141         ret = devm_add_action_or_reset(dev, sc27xx_fgu_disable, data);
1142         if (ret) {
1143                 dev_err(dev, "failed to add fgu disable action\n");
1144                 return ret;
1145         }
1146
1147         irq = platform_get_irq(pdev, 0);
1148         if (irq < 0) {
1149                 dev_err(dev, "no irq resource specified\n");
1150                 return irq;
1151         }
1152
1153         ret = devm_request_threaded_irq(data->dev, irq, NULL,
1154                                         sc27xx_fgu_interrupt,
1155                                         IRQF_NO_SUSPEND | IRQF_ONESHOT,
1156                                         pdev->name, data);
1157         if (ret) {
1158                 dev_err(data->dev, "failed to request fgu IRQ\n");
1159                 return ret;
1160         }
1161
1162         irq = gpiod_to_irq(data->gpiod);
1163         if (irq < 0) {
1164                 dev_err(dev, "failed to translate GPIO to IRQ\n");
1165                 return irq;
1166         }
1167
1168         ret = devm_request_threaded_irq(dev, irq, NULL,
1169                                         sc27xx_fgu_bat_detection,
1170                                         IRQF_ONESHOT | IRQF_TRIGGER_RISING |
1171                                         IRQF_TRIGGER_FALLING,
1172                                         pdev->name, data);
1173         if (ret) {
1174                 dev_err(dev, "failed to request IRQ\n");
1175                 return ret;
1176         }
1177
1178         return 0;
1179 }
1180
1181 #ifdef CONFIG_PM_SLEEP
1182 static int sc27xx_fgu_resume(struct device *dev)
1183 {
1184         struct sc27xx_fgu_data *data = dev_get_drvdata(dev);
1185         int ret;
1186
1187         ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
1188                                  SC27XX_FGU_LOW_OVERLOAD_INT |
1189                                  SC27XX_FGU_CLBCNT_DELTA_INT, 0);
1190         if (ret) {
1191                 dev_err(data->dev, "failed to disable fgu interrupts\n");
1192                 return ret;
1193         }
1194
1195         return 0;
1196 }
1197
1198 static int sc27xx_fgu_suspend(struct device *dev)
1199 {
1200         struct sc27xx_fgu_data *data = dev_get_drvdata(dev);
1201         int ret, status, ocv;
1202
1203         ret = sc27xx_fgu_get_status(data, &status);
1204         if (ret)
1205                 return ret;
1206
1207         /*
1208          * If we are charging, then no need to enable the FGU interrupts to
1209          * adjust the battery capacity.
1210          */
1211         if (status != POWER_SUPPLY_STATUS_NOT_CHARGING &&
1212             status != POWER_SUPPLY_STATUS_DISCHARGING)
1213                 return 0;
1214
1215         ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
1216                                  SC27XX_FGU_LOW_OVERLOAD_INT,
1217                                  SC27XX_FGU_LOW_OVERLOAD_INT);
1218         if (ret) {
1219                 dev_err(data->dev, "failed to enable low voltage interrupt\n");
1220                 return ret;
1221         }
1222
1223         ret = sc27xx_fgu_get_vbat_ocv(data, &ocv);
1224         if (ret)
1225                 goto disable_int;
1226
1227         /*
1228          * If current OCV is less than the minimum voltage, we should enable the
1229          * coulomb counter threshold interrupt to notify events to adjust the
1230          * battery capacity.
1231          */
1232         if (ocv < data->min_volt) {
1233                 ret = regmap_update_bits(data->regmap,
1234                                          data->base + SC27XX_FGU_INT_EN,
1235                                          SC27XX_FGU_CLBCNT_DELTA_INT,
1236                                          SC27XX_FGU_CLBCNT_DELTA_INT);
1237                 if (ret) {
1238                         dev_err(data->dev,
1239                                 "failed to enable coulomb threshold int\n");
1240                         goto disable_int;
1241                 }
1242         }
1243
1244         return 0;
1245
1246 disable_int:
1247         regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
1248                            SC27XX_FGU_LOW_OVERLOAD_INT, 0);
1249         return ret;
1250 }
1251 #endif
1252
1253 static const struct dev_pm_ops sc27xx_fgu_pm_ops = {
1254         SET_SYSTEM_SLEEP_PM_OPS(sc27xx_fgu_suspend, sc27xx_fgu_resume)
1255 };
1256
1257 static const struct of_device_id sc27xx_fgu_of_match[] = {
1258         { .compatible = "sprd,sc2731-fgu", },
1259         { }
1260 };
1261
1262 static struct platform_driver sc27xx_fgu_driver = {
1263         .probe = sc27xx_fgu_probe,
1264         .driver = {
1265                 .name = "sc27xx-fgu",
1266                 .of_match_table = sc27xx_fgu_of_match,
1267                 .pm = &sc27xx_fgu_pm_ops,
1268         }
1269 };
1270
1271 module_platform_driver(sc27xx_fgu_driver);
1272
1273 MODULE_DESCRIPTION("Spreadtrum SC27XX PMICs Fual Gauge Unit Driver");
1274 MODULE_LICENSE("GPL v2");
This page took 0.114728 seconds and 4 git commands to generate.