1 // SPDX-License-Identifier: GPL-2.0-only
3 * Battery driver for CPCAP PMIC
5 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
7 * Some parts of the code based on earlier Motorola mapphone Linux kernel
10 * Copyright (C) 2009-2010 Motorola, Inc.
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h>
21 #include <linux/reboot.h>
22 #include <linux/regmap.h>
23 #include <linux/nvmem-consumer.h>
24 #include <linux/moduleparam.h>
26 #include <linux/iio/consumer.h>
27 #include <linux/iio/types.h>
28 #include <linux/mfd/motorola-cpcap.h>
31 * Register bit defines for CPCAP_REG_BPEOL. Some of these seem to
32 * map to MC13783UG.pdf "Table 5-19. Register 13, Power Control 0"
33 * to enable BATTDETEN, LOBAT and EOL features. We currently use
34 * LOBAT interrupts instead of EOL.
36 #define CPCAP_REG_BPEOL_BIT_EOL9 BIT(9) /* Set for EOL irq */
37 #define CPCAP_REG_BPEOL_BIT_EOL8 BIT(8) /* Set for EOL irq */
38 #define CPCAP_REG_BPEOL_BIT_UNKNOWN7 BIT(7)
39 #define CPCAP_REG_BPEOL_BIT_UNKNOWN6 BIT(6)
40 #define CPCAP_REG_BPEOL_BIT_UNKNOWN5 BIT(5)
41 #define CPCAP_REG_BPEOL_BIT_EOL_MULTI BIT(4) /* Set for multiple EOL irqs */
42 #define CPCAP_REG_BPEOL_BIT_UNKNOWN3 BIT(3)
43 #define CPCAP_REG_BPEOL_BIT_UNKNOWN2 BIT(2)
44 #define CPCAP_REG_BPEOL_BIT_BATTDETEN BIT(1) /* Enable battery detect */
45 #define CPCAP_REG_BPEOL_BIT_EOLSEL BIT(0) /* BPDET = 0, EOL = 1 */
48 * Register bit defines for CPCAP_REG_CCC1. These seem similar to the twl6030
49 * coulomb counter registers rather than the mc13892 registers. Both twl6030
50 * and mc13892 set bits 2 and 1 to reset and clear registers. But mc13892
51 * sets bit 0 to start the coulomb counter while twl6030 sets bit 0 to stop
52 * the coulomb counter like cpcap does. So for now, we use the twl6030 style
53 * naming for the registers.
55 #define CPCAP_REG_CCC1_ACTIVE_MODE1 BIT(4) /* Update rate */
56 #define CPCAP_REG_CCC1_ACTIVE_MODE0 BIT(3) /* Update rate */
57 #define CPCAP_REG_CCC1_AUTOCLEAR BIT(2) /* Resets sample registers */
58 #define CPCAP_REG_CCC1_CAL_EN BIT(1) /* Clears after write in 1s */
59 #define CPCAP_REG_CCC1_PAUSE BIT(0) /* Stop counters, allow write */
60 #define CPCAP_REG_CCC1_RESET_MASK (CPCAP_REG_CCC1_AUTOCLEAR | \
61 CPCAP_REG_CCC1_CAL_EN)
63 #define CPCAP_REG_CCCC2_RATE1 BIT(5)
64 #define CPCAP_REG_CCCC2_RATE0 BIT(4)
65 #define CPCAP_REG_CCCC2_ENABLE BIT(3)
67 #define CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS 250
69 #define CPCAP_BATTERY_EB41_HW4X_ID 0x9E
70 #define CPCAP_BATTERY_BW8X_ID 0x98
73 CPCAP_BATTERY_IIO_BATTDET,
74 CPCAP_BATTERY_IIO_VOLTAGE,
75 CPCAP_BATTERY_IIO_CHRG_CURRENT,
76 CPCAP_BATTERY_IIO_BATT_CURRENT,
80 enum cpcap_battery_irq_action {
81 CPCAP_BATTERY_IRQ_ACTION_NONE,
82 CPCAP_BATTERY_IRQ_ACTION_CC_CAL_DONE,
83 CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW,
84 CPCAP_BATTERY_IRQ_ACTION_POWEROFF,
87 struct cpcap_interrupt_desc {
89 struct list_head node;
91 enum cpcap_battery_irq_action action;
94 struct cpcap_battery_config {
96 struct power_supply_info info;
97 struct power_supply_battery_info bat;
100 struct cpcap_coulomb_counter_data {
101 s32 sample; /* 24 or 32 bits */
103 s16 offset; /* 9 bits */
104 s16 integrator; /* 13 or 16 bits */
107 enum cpcap_battery_state {
108 CPCAP_BATTERY_STATE_PREVIOUS,
109 CPCAP_BATTERY_STATE_LATEST,
110 CPCAP_BATTERY_STATE_EMPTY,
111 CPCAP_BATTERY_STATE_FULL,
112 CPCAP_BATTERY_STATE_NR,
115 struct cpcap_battery_state_data {
121 struct cpcap_coulomb_counter_data cc;
124 struct cpcap_battery_ddata {
127 struct list_head irq_list;
128 struct iio_channel *channels[CPCAP_BATTERY_IIO_NR];
129 struct power_supply *psy;
130 struct cpcap_battery_config config;
131 struct cpcap_battery_state_data state[CPCAP_BATTERY_STATE_NR];
132 u32 cc_lsb; /* μAms per LSB */
138 unsigned int is_full:1;
141 #define CPCAP_NO_BATTERY -400
143 static bool ignore_temperature_probe;
144 module_param(ignore_temperature_probe, bool, 0660);
146 static struct cpcap_battery_state_data *
147 cpcap_battery_get_state(struct cpcap_battery_ddata *ddata,
148 enum cpcap_battery_state state)
150 if (state >= CPCAP_BATTERY_STATE_NR)
153 return &ddata->state[state];
156 static struct cpcap_battery_state_data *
157 cpcap_battery_latest(struct cpcap_battery_ddata *ddata)
159 return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_LATEST);
162 static struct cpcap_battery_state_data *
163 cpcap_battery_previous(struct cpcap_battery_ddata *ddata)
165 return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_PREVIOUS);
168 static struct cpcap_battery_state_data *
169 cpcap_battery_get_empty(struct cpcap_battery_ddata *ddata)
171 return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_EMPTY);
174 static struct cpcap_battery_state_data *
175 cpcap_battery_get_full(struct cpcap_battery_ddata *ddata)
177 return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_FULL);
180 static int cpcap_charger_battery_temperature(struct cpcap_battery_ddata *ddata,
183 struct iio_channel *channel;
186 channel = ddata->channels[CPCAP_BATTERY_IIO_BATTDET];
187 error = iio_read_channel_processed(channel, value);
189 if (!ignore_temperature_probe)
190 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
191 *value = CPCAP_NO_BATTERY;
201 static int cpcap_battery_get_voltage(struct cpcap_battery_ddata *ddata)
203 struct iio_channel *channel;
204 int error, value = 0;
206 channel = ddata->channels[CPCAP_BATTERY_IIO_VOLTAGE];
207 error = iio_read_channel_processed(channel, &value);
209 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
217 static int cpcap_battery_get_current(struct cpcap_battery_ddata *ddata)
219 struct iio_channel *channel;
220 int error, value = 0;
222 channel = ddata->channels[CPCAP_BATTERY_IIO_BATT_CURRENT];
223 error = iio_read_channel_processed(channel, &value);
225 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
234 * cpcap_battery_cc_raw_div - calculate and divide coulomb counter μAms values
235 * @ddata: device driver data
236 * @sample: coulomb counter sample value
237 * @accumulator: coulomb counter integrator value
238 * @offset: coulomb counter offset value
239 * @divider: conversion divider
241 * Note that cc_lsb and cc_dur values are from Motorola Linux kernel
242 * function data_get_avg_curr_ua() and seem to be based on measured test
243 * results. It also has the following comment:
245 * Adjustment factors are applied here as a temp solution per the test
246 * results. Need to work out a formal solution for this adjustment.
248 * A coulomb counter for similar hardware seems to be documented in
249 * "TWL6030 Gas Gauging Basics (Rev. A)" swca095a.pdf in chapter
250 * "10 Calculating Accumulated Current". We however follow what the
251 * Motorola mapphone Linux kernel is doing as there may be either a
252 * TI or ST coulomb counter in the PMIC.
254 static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata,
255 s32 sample, s32 accumulator,
256 s16 offset, u32 divider)
264 acc -= (s64)sample * offset;
265 acc *= ddata->cc_lsb;
267 acc = div_s64(acc, divider);
272 /* 3600000μAms = 1μAh */
273 static int cpcap_battery_cc_to_uah(struct cpcap_battery_ddata *ddata,
274 s32 sample, s32 accumulator,
277 return cpcap_battery_cc_raw_div(ddata, sample,
282 static int cpcap_battery_cc_to_ua(struct cpcap_battery_ddata *ddata,
283 s32 sample, s32 accumulator,
286 return cpcap_battery_cc_raw_div(ddata, sample,
289 CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS);
293 * cpcap_battery_read_accumulated - reads cpcap coulomb counter
294 * @ddata: device driver data
295 * @ccd: coulomb counter values
297 * Based on Motorola mapphone kernel function data_read_regs().
298 * Looking at the registers, the coulomb counter seems similar to
299 * the coulomb counter in TWL6030. See "TWL6030 Gas Gauging Basics
300 * (Rev. A) swca095a.pdf for "10 Calculating Accumulated Current".
302 * Note that swca095a.pdf instructs to stop the coulomb counter
303 * before reading to avoid values changing. Motorola mapphone
304 * Linux kernel does not do it, so let's assume they've verified
305 * the data produced is correct.
308 cpcap_battery_read_accumulated(struct cpcap_battery_ddata *ddata,
309 struct cpcap_coulomb_counter_data *ccd)
311 u16 buf[7]; /* CPCAP_REG_CCS1 to CCI */
315 ccd->accumulator = 0;
319 /* Read coulomb counter register range */
320 error = regmap_bulk_read(ddata->reg, CPCAP_REG_CCS1,
321 buf, ARRAY_SIZE(buf));
325 /* Sample value CPCAP_REG_CCS1 & 2 */
326 ccd->sample = (buf[1] & 0x0fff) << 16;
327 ccd->sample |= buf[0];
328 if (ddata->vendor == CPCAP_VENDOR_TI)
329 ccd->sample = sign_extend32(24, ccd->sample);
331 /* Accumulator value CPCAP_REG_CCA1 & 2 */
332 ccd->accumulator = ((s16)buf[3]) << 16;
333 ccd->accumulator |= buf[2];
336 * Coulomb counter calibration offset is CPCAP_REG_CCM,
337 * REG_CCO seems unused
339 ccd->offset = buf[4];
340 ccd->offset = sign_extend32(ccd->offset, 9);
342 /* Integrator register CPCAP_REG_CCI */
343 if (ddata->vendor == CPCAP_VENDOR_TI)
344 ccd->integrator = sign_extend32(buf[6], 13);
346 ccd->integrator = (s16)buf[6];
348 return cpcap_battery_cc_to_uah(ddata,
356 * Based on the values from Motorola mapphone Linux kernel for the
357 * stock Droid 4 battery eb41. In the Motorola mapphone Linux
358 * kernel tree the value for pm_cd_factor is passed to the kernel
359 * via device tree. If it turns out to be something device specific
360 * we can consider that too later. These values are also fine for
363 * And looking at the battery full and shutdown values for the stock
364 * kernel on droid 4, full is 4351000 and software initiates shutdown
365 * at 3078000. The device will die around 2743000.
367 static const struct cpcap_battery_config cpcap_battery_eb41_data = {
369 .info.technology = POWER_SUPPLY_TECHNOLOGY_LION,
370 .info.voltage_max_design = 4351000,
371 .info.voltage_min_design = 3100000,
372 .info.charge_full_design = 1740000,
373 .bat.constant_charge_voltage_max_uv = 4200000,
376 /* Values for the extended Droid Bionic battery bw8x. */
377 static const struct cpcap_battery_config cpcap_battery_bw8x_data = {
379 .info.technology = POWER_SUPPLY_TECHNOLOGY_LION,
380 .info.voltage_max_design = 4200000,
381 .info.voltage_min_design = 3200000,
382 .info.charge_full_design = 2760000,
383 .bat.constant_charge_voltage_max_uv = 4200000,
387 * Safe values for any lipo battery likely to fit into a mapphone
390 static const struct cpcap_battery_config cpcap_battery_unkown_data = {
392 .info.technology = POWER_SUPPLY_TECHNOLOGY_LION,
393 .info.voltage_max_design = 4200000,
394 .info.voltage_min_design = 3200000,
395 .info.charge_full_design = 3000000,
396 .bat.constant_charge_voltage_max_uv = 4200000,
399 static int cpcap_battery_match_nvmem(struct device *dev, const void *data)
401 if (strcmp(dev_name(dev), "89-500029ba0f73") == 0)
407 static void cpcap_battery_detect_battery_type(struct cpcap_battery_ddata *ddata)
409 struct nvmem_device *nvmem;
412 ddata->check_nvmem = false;
414 nvmem = nvmem_device_find(NULL, &cpcap_battery_match_nvmem);
415 if (IS_ERR_OR_NULL(nvmem)) {
416 ddata->check_nvmem = true;
417 dev_info_once(ddata->dev, "Can not find battery nvmem device. Assuming generic lipo battery\n");
418 } else if (nvmem_device_read(nvmem, 2, 1, &battery_id) < 0) {
420 ddata->check_nvmem = true;
421 dev_warn(ddata->dev, "Can not read battery nvmem device. Assuming generic lipo battery\n");
424 switch (battery_id) {
425 case CPCAP_BATTERY_EB41_HW4X_ID:
426 ddata->config = cpcap_battery_eb41_data;
428 case CPCAP_BATTERY_BW8X_ID:
429 ddata->config = cpcap_battery_bw8x_data;
432 ddata->config = cpcap_battery_unkown_data;
437 * cpcap_battery_cc_get_avg_current - read cpcap coulumb counter
438 * @ddata: cpcap battery driver device data
440 static int cpcap_battery_cc_get_avg_current(struct cpcap_battery_ddata *ddata)
442 int value, acc, error;
446 /* Coulomb counter integrator */
447 error = regmap_read(ddata->reg, CPCAP_REG_CCI, &value);
451 if (ddata->vendor == CPCAP_VENDOR_TI) {
452 acc = sign_extend32(value, 13);
459 /* Coulomb counter calibration offset */
460 error = regmap_read(ddata->reg, CPCAP_REG_CCM, &value);
464 offset = sign_extend32(value, 9);
466 return cpcap_battery_cc_to_ua(ddata, sample, acc, offset);
469 static int cpcap_battery_get_charger_status(struct cpcap_battery_ddata *ddata,
472 union power_supply_propval prop;
473 struct power_supply *charger;
476 charger = power_supply_get_by_name("usb");
480 error = power_supply_get_property(charger, POWER_SUPPLY_PROP_STATUS,
483 *val = POWER_SUPPLY_STATUS_UNKNOWN;
487 power_supply_put(charger);
492 static bool cpcap_battery_full(struct cpcap_battery_ddata *ddata)
494 struct cpcap_battery_state_data *state = cpcap_battery_latest(ddata);
498 error = cpcap_battery_get_charger_status(ddata, &val);
501 case POWER_SUPPLY_STATUS_DISCHARGING:
502 dev_dbg(ddata->dev, "charger disconnected\n");
505 case POWER_SUPPLY_STATUS_FULL:
506 dev_dbg(ddata->dev, "charger full status\n");
515 * The full battery voltage here can be inaccurate, it's used just to
516 * filter out any trickle charging events. We clear the is_full status
517 * on charger disconnect above anyways.
519 vfull = ddata->config.bat.constant_charge_voltage_max_uv - 120000;
521 if (ddata->is_full && state->voltage < vfull)
524 return ddata->is_full;
527 static bool cpcap_battery_low(struct cpcap_battery_ddata *ddata)
529 struct cpcap_battery_state_data *state = cpcap_battery_latest(ddata);
532 if (state->current_ua > 0 && (state->voltage <= 3350000 || is_low))
540 static int cpcap_battery_update_status(struct cpcap_battery_ddata *ddata)
542 struct cpcap_battery_state_data state, *latest, *previous,
547 memset(&state, 0, sizeof(state));
550 latest = cpcap_battery_latest(ddata);
552 s64 delta_ms = ktime_to_ms(ktime_sub(now, latest->time));
554 if (delta_ms < CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS)
559 state.voltage = cpcap_battery_get_voltage(ddata);
560 state.current_ua = cpcap_battery_get_current(ddata);
561 state.counter_uah = cpcap_battery_read_accumulated(ddata, &state.cc);
563 error = cpcap_charger_battery_temperature(ddata,
568 previous = cpcap_battery_previous(ddata);
569 memcpy(previous, latest, sizeof(*previous));
570 memcpy(latest, &state, sizeof(*latest));
572 if (cpcap_battery_full(ddata)) {
573 full = cpcap_battery_get_full(ddata);
574 memcpy(full, latest, sizeof(*full));
576 empty = cpcap_battery_get_empty(ddata);
577 if (empty->voltage && empty->voltage != -1) {
580 empty->counter_uah - full->counter_uah;
581 } else if (ddata->charge_full) {
584 full->counter_uah + ddata->charge_full;
586 } else if (cpcap_battery_low(ddata)) {
587 empty = cpcap_battery_get_empty(ddata);
588 memcpy(empty, latest, sizeof(*empty));
590 full = cpcap_battery_get_full(ddata);
594 empty->counter_uah - full->counter_uah;
602 * Update battery status when cpcap-charger calls power_supply_changed().
603 * This allows us to detect battery full condition before the charger
606 static void cpcap_battery_external_power_changed(struct power_supply *psy)
608 union power_supply_propval prop;
610 power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS, &prop);
613 static enum power_supply_property cpcap_battery_props[] = {
614 POWER_SUPPLY_PROP_STATUS,
615 POWER_SUPPLY_PROP_PRESENT,
616 POWER_SUPPLY_PROP_TECHNOLOGY,
617 POWER_SUPPLY_PROP_VOLTAGE_NOW,
618 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
619 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
620 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
621 POWER_SUPPLY_PROP_CURRENT_AVG,
622 POWER_SUPPLY_PROP_CURRENT_NOW,
623 POWER_SUPPLY_PROP_CHARGE_FULL,
624 POWER_SUPPLY_PROP_CHARGE_NOW,
625 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
626 POWER_SUPPLY_PROP_CHARGE_COUNTER,
627 POWER_SUPPLY_PROP_POWER_NOW,
628 POWER_SUPPLY_PROP_POWER_AVG,
629 POWER_SUPPLY_PROP_CAPACITY,
630 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
631 POWER_SUPPLY_PROP_SCOPE,
632 POWER_SUPPLY_PROP_TEMP,
635 static int cpcap_battery_get_property(struct power_supply *psy,
636 enum power_supply_property psp,
637 union power_supply_propval *val)
639 struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
640 struct cpcap_battery_state_data *latest, *previous, *empty;
646 cached = cpcap_battery_update_status(ddata);
650 latest = cpcap_battery_latest(ddata);
651 previous = cpcap_battery_previous(ddata);
653 if (ddata->check_nvmem)
654 cpcap_battery_detect_battery_type(ddata);
657 case POWER_SUPPLY_PROP_PRESENT:
658 if (latest->temperature > CPCAP_NO_BATTERY || ignore_temperature_probe)
663 case POWER_SUPPLY_PROP_STATUS:
664 if (cpcap_battery_full(ddata)) {
665 val->intval = POWER_SUPPLY_STATUS_FULL;
668 if (cpcap_battery_cc_get_avg_current(ddata) < 0)
669 val->intval = POWER_SUPPLY_STATUS_CHARGING;
671 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
673 case POWER_SUPPLY_PROP_TECHNOLOGY:
674 val->intval = ddata->config.info.technology;
676 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
677 val->intval = cpcap_battery_get_voltage(ddata);
679 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
680 val->intval = ddata->config.info.voltage_max_design;
682 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
683 val->intval = ddata->config.info.voltage_min_design;
685 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
686 val->intval = ddata->config.bat.constant_charge_voltage_max_uv;
688 case POWER_SUPPLY_PROP_CURRENT_AVG:
689 sample = latest->cc.sample - previous->cc.sample;
691 val->intval = cpcap_battery_cc_get_avg_current(ddata);
694 accumulator = latest->cc.accumulator - previous->cc.accumulator;
695 val->intval = cpcap_battery_cc_to_ua(ddata, sample,
699 case POWER_SUPPLY_PROP_CURRENT_NOW:
700 val->intval = latest->current_ua;
702 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
703 val->intval = latest->counter_uah;
705 case POWER_SUPPLY_PROP_POWER_NOW:
706 tmp = (latest->voltage / 10000) * latest->current_ua;
707 val->intval = div64_s64(tmp, 100);
709 case POWER_SUPPLY_PROP_POWER_AVG:
710 sample = latest->cc.sample - previous->cc.sample;
712 tmp = cpcap_battery_cc_get_avg_current(ddata);
713 tmp *= (latest->voltage / 10000);
714 val->intval = div64_s64(tmp, 100);
717 accumulator = latest->cc.accumulator - previous->cc.accumulator;
718 tmp = cpcap_battery_cc_to_ua(ddata, sample, accumulator,
720 tmp *= ((latest->voltage + previous->voltage) / 20000);
721 val->intval = div64_s64(tmp, 100);
723 case POWER_SUPPLY_PROP_CAPACITY:
724 empty = cpcap_battery_get_empty(ddata);
725 if (!empty->voltage || !ddata->charge_full)
727 /* (ddata->charge_full / 200) is needed for rounding */
728 val->intval = empty->counter_uah - latest->counter_uah +
729 ddata->charge_full / 200;
730 val->intval = clamp(val->intval, 0, ddata->charge_full);
731 val->intval = val->intval * 100 / ddata->charge_full;
733 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
734 if (cpcap_battery_full(ddata))
735 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
736 else if (latest->voltage >= 3750000)
737 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
738 else if (latest->voltage >= 3300000)
739 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
740 else if (latest->voltage > 3100000)
741 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
742 else if (latest->voltage <= 3100000)
743 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
745 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
747 case POWER_SUPPLY_PROP_CHARGE_NOW:
748 empty = cpcap_battery_get_empty(ddata);
751 val->intval = empty->counter_uah - latest->counter_uah;
752 if (val->intval < 0) {
753 /* Assume invalid config if CHARGE_NOW is -20% */
754 if (ddata->charge_full && abs(val->intval) > ddata->charge_full/5) {
756 ddata->charge_full = 0;
760 } else if (ddata->charge_full && ddata->charge_full < val->intval) {
761 /* Assume invalid config if CHARGE_NOW exceeds CHARGE_FULL by 20% */
762 if (val->intval > (6*ddata->charge_full)/5) {
764 ddata->charge_full = 0;
767 val->intval = ddata->charge_full;
770 case POWER_SUPPLY_PROP_CHARGE_FULL:
771 if (!ddata->charge_full)
773 val->intval = ddata->charge_full;
775 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
776 val->intval = ddata->config.info.charge_full_design;
778 case POWER_SUPPLY_PROP_SCOPE:
779 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
781 case POWER_SUPPLY_PROP_TEMP:
782 if (ignore_temperature_probe)
784 val->intval = latest->temperature;
793 static int cpcap_battery_update_charger(struct cpcap_battery_ddata *ddata,
794 int const_charge_voltage)
796 union power_supply_propval prop;
797 union power_supply_propval val;
798 struct power_supply *charger;
801 charger = power_supply_get_by_name("usb");
805 error = power_supply_get_property(charger,
806 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
811 /* Allow charger const voltage lower than battery const voltage */
812 if (const_charge_voltage > prop.intval)
815 val.intval = const_charge_voltage;
817 error = power_supply_set_property(charger,
818 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
821 power_supply_put(charger);
826 static int cpcap_battery_set_property(struct power_supply *psy,
827 enum power_supply_property psp,
828 const union power_supply_propval *val)
830 struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
833 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
834 if (val->intval < ddata->config.info.voltage_min_design)
836 if (val->intval > ddata->config.info.voltage_max_design)
839 ddata->config.bat.constant_charge_voltage_max_uv = val->intval;
841 return cpcap_battery_update_charger(ddata, val->intval);
842 case POWER_SUPPLY_PROP_CHARGE_FULL:
845 if (val->intval > (6*ddata->config.info.charge_full_design)/5)
848 ddata->charge_full = val->intval;
858 static int cpcap_battery_property_is_writeable(struct power_supply *psy,
859 enum power_supply_property psp)
862 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
863 case POWER_SUPPLY_PROP_CHARGE_FULL:
870 static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
872 struct cpcap_battery_ddata *ddata = data;
873 struct cpcap_battery_state_data *latest;
874 struct cpcap_interrupt_desc *d;
876 if (!atomic_read(&ddata->active))
879 list_for_each_entry(d, &ddata->irq_list, node) {
884 if (list_entry_is_head(d, &ddata->irq_list, node))
887 latest = cpcap_battery_latest(ddata);
890 case CPCAP_BATTERY_IRQ_ACTION_CC_CAL_DONE:
891 dev_info(ddata->dev, "Coulomb counter calibration done\n");
893 case CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW:
894 if (latest->current_ua >= 0)
895 dev_warn(ddata->dev, "Battery low at %imV!\n",
896 latest->voltage / 1000);
898 case CPCAP_BATTERY_IRQ_ACTION_POWEROFF:
899 if (latest->current_ua >= 0 && latest->voltage <= 3200000) {
900 dev_emerg(ddata->dev,
901 "Battery empty at %imV, powering off\n",
902 latest->voltage / 1000);
903 orderly_poweroff(true);
910 power_supply_changed(ddata->psy);
915 static int cpcap_battery_init_irq(struct platform_device *pdev,
916 struct cpcap_battery_ddata *ddata,
919 struct cpcap_interrupt_desc *d;
922 irq = platform_get_irq_byname(pdev, name);
926 error = devm_request_threaded_irq(ddata->dev, irq, NULL,
927 cpcap_battery_irq_thread,
928 IRQF_SHARED | IRQF_ONESHOT,
931 dev_err(ddata->dev, "could not get irq %s: %i\n",
937 d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
944 if (!strncmp(name, "cccal", 5))
945 d->action = CPCAP_BATTERY_IRQ_ACTION_CC_CAL_DONE;
946 else if (!strncmp(name, "lowbph", 6))
947 d->action = CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW;
948 else if (!strncmp(name, "lowbpl", 6))
949 d->action = CPCAP_BATTERY_IRQ_ACTION_POWEROFF;
951 list_add(&d->node, &ddata->irq_list);
956 static int cpcap_battery_init_interrupts(struct platform_device *pdev,
957 struct cpcap_battery_ddata *ddata)
959 static const char * const cpcap_battery_irqs[] = {
960 "eol", "lowbph", "lowbpl",
961 "chrgcurr1", "battdetb"
965 for (i = 0; i < ARRAY_SIZE(cpcap_battery_irqs); i++) {
966 error = cpcap_battery_init_irq(pdev, ddata,
967 cpcap_battery_irqs[i]);
972 /* Enable calibration interrupt if already available in dts */
973 cpcap_battery_init_irq(pdev, ddata, "cccal");
975 /* Enable low battery interrupts for 3.3V high and 3.1V low */
976 error = regmap_update_bits(ddata->reg, CPCAP_REG_BPEOL,
978 CPCAP_REG_BPEOL_BIT_BATTDETEN);
985 static int cpcap_battery_init_iio(struct cpcap_battery_ddata *ddata)
987 const char * const names[CPCAP_BATTERY_IIO_NR] = {
988 "battdetb", "battp", "chg_isense", "batti",
992 for (i = 0; i < CPCAP_BATTERY_IIO_NR; i++) {
993 ddata->channels[i] = devm_iio_channel_get(ddata->dev,
995 if (IS_ERR(ddata->channels[i])) {
996 error = PTR_ERR(ddata->channels[i]);
1000 if (!ddata->channels[i]->indio_dev) {
1009 return dev_err_probe(ddata->dev, error,
1010 "could not initialize VBUS or ID IIO\n");
1013 /* Calibrate coulomb counter */
1014 static int cpcap_battery_calibrate(struct cpcap_battery_ddata *ddata)
1016 int error, ccc1, value;
1017 unsigned long timeout;
1019 error = regmap_read(ddata->reg, CPCAP_REG_CCC1, &ccc1);
1023 timeout = jiffies + msecs_to_jiffies(6000);
1025 /* Start calibration */
1026 error = regmap_update_bits(ddata->reg, CPCAP_REG_CCC1,
1028 CPCAP_REG_CCC1_CAL_EN);
1032 while (time_before(jiffies, timeout)) {
1033 error = regmap_read(ddata->reg, CPCAP_REG_CCC1, &value);
1037 if (!(value & CPCAP_REG_CCC1_CAL_EN))
1040 error = regmap_read(ddata->reg, CPCAP_REG_CCM, &value);
1047 /* Read calibration offset from CCM */
1048 error = regmap_read(ddata->reg, CPCAP_REG_CCM, &value);
1052 dev_info(ddata->dev, "calibration done: 0x%04x\n", value);
1056 dev_err(ddata->dev, "%s: error %i\n", __func__, error);
1058 error = regmap_update_bits(ddata->reg, CPCAP_REG_CCC1,
1061 dev_err(ddata->dev, "%s: restore error %i\n",
1068 static const struct of_device_id cpcap_battery_id_table[] = {
1070 .compatible = "motorola,cpcap-battery",
1074 MODULE_DEVICE_TABLE(of, cpcap_battery_id_table);
1077 static const struct power_supply_desc cpcap_charger_battery_desc = {
1079 .type = POWER_SUPPLY_TYPE_BATTERY,
1080 .properties = cpcap_battery_props,
1081 .num_properties = ARRAY_SIZE(cpcap_battery_props),
1082 .get_property = cpcap_battery_get_property,
1083 .set_property = cpcap_battery_set_property,
1084 .property_is_writeable = cpcap_battery_property_is_writeable,
1085 .external_power_changed = cpcap_battery_external_power_changed,
1088 static int cpcap_battery_probe(struct platform_device *pdev)
1090 struct cpcap_battery_ddata *ddata;
1091 struct power_supply_config psy_cfg = {};
1094 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
1098 cpcap_battery_detect_battery_type(ddata);
1100 INIT_LIST_HEAD(&ddata->irq_list);
1101 ddata->dev = &pdev->dev;
1103 ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
1107 error = cpcap_get_vendor(ddata->dev, ddata->reg, &ddata->vendor);
1111 switch (ddata->vendor) {
1112 case CPCAP_VENDOR_ST:
1113 ddata->cc_lsb = 95374; /* μAms per LSB */
1115 case CPCAP_VENDOR_TI:
1116 ddata->cc_lsb = 91501; /* μAms per LSB */
1121 ddata->cc_lsb = (ddata->cc_lsb * ddata->config.cd_factor) / 1000;
1123 platform_set_drvdata(pdev, ddata);
1125 error = cpcap_battery_init_interrupts(pdev, ddata);
1129 error = cpcap_battery_init_iio(ddata);
1133 psy_cfg.of_node = pdev->dev.of_node;
1134 psy_cfg.drv_data = ddata;
1136 ddata->psy = devm_power_supply_register(ddata->dev,
1137 &cpcap_charger_battery_desc,
1139 error = PTR_ERR_OR_ZERO(ddata->psy);
1141 dev_err(ddata->dev, "failed to register power supply\n");
1145 atomic_set(&ddata->active, 1);
1147 error = cpcap_battery_calibrate(ddata);
1154 static int cpcap_battery_remove(struct platform_device *pdev)
1156 struct cpcap_battery_ddata *ddata = platform_get_drvdata(pdev);
1159 atomic_set(&ddata->active, 0);
1160 error = regmap_update_bits(ddata->reg, CPCAP_REG_BPEOL,
1163 dev_err(&pdev->dev, "could not disable: %i\n", error);
1168 static struct platform_driver cpcap_battery_driver = {
1170 .name = "cpcap_battery",
1171 .of_match_table = of_match_ptr(cpcap_battery_id_table),
1173 .probe = cpcap_battery_probe,
1174 .remove = cpcap_battery_remove,
1176 module_platform_driver(cpcap_battery_driver);
1178 MODULE_LICENSE("GPL v2");
1179 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
1180 MODULE_DESCRIPTION("CPCAP PMIC Battery Driver");