2 * Battery driver for CPCAP PMIC
4 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
6 * Some parts of the code based on earlie Motorola mapphone Linux kernel
9 * Copyright (C) 2009-2010 Motorola, Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
16 * kind, whether express or implied; without even the implied warranty
17 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <linux/delay.h>
22 #include <linux/err.h>
23 #include <linux/interrupt.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/power_supply.h>
29 #include <linux/reboot.h>
30 #include <linux/regmap.h>
32 #include <linux/iio/consumer.h>
33 #include <linux/iio/types.h>
34 #include <linux/mfd/motorola-cpcap.h>
36 #include <asm/div64.h>
39 * Register bit defines for CPCAP_REG_BPEOL. Some of these seem to
40 * map to MC13783UG.pdf "Table 5-19. Register 13, Power Control 0"
41 * to enable BATTDETEN, LOBAT and EOL features. We currently use
42 * LOBAT interrupts instead of EOL.
44 #define CPCAP_REG_BPEOL_BIT_EOL9 BIT(9) /* Set for EOL irq */
45 #define CPCAP_REG_BPEOL_BIT_EOL8 BIT(8) /* Set for EOL irq */
46 #define CPCAP_REG_BPEOL_BIT_UNKNOWN7 BIT(7)
47 #define CPCAP_REG_BPEOL_BIT_UNKNOWN6 BIT(6)
48 #define CPCAP_REG_BPEOL_BIT_UNKNOWN5 BIT(5)
49 #define CPCAP_REG_BPEOL_BIT_EOL_MULTI BIT(4) /* Set for multiple EOL irqs */
50 #define CPCAP_REG_BPEOL_BIT_UNKNOWN3 BIT(3)
51 #define CPCAP_REG_BPEOL_BIT_UNKNOWN2 BIT(2)
52 #define CPCAP_REG_BPEOL_BIT_BATTDETEN BIT(1) /* Enable battery detect */
53 #define CPCAP_REG_BPEOL_BIT_EOLSEL BIT(0) /* BPDET = 0, EOL = 1 */
55 #define CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS 250
58 CPCAP_BATTERY_IIO_BATTDET,
59 CPCAP_BATTERY_IIO_VOLTAGE,
60 CPCAP_BATTERY_IIO_CHRG_CURRENT,
61 CPCAP_BATTERY_IIO_BATT_CURRENT,
65 enum cpcap_battery_irq_action {
66 CPCAP_BATTERY_IRQ_ACTION_NONE,
67 CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW,
68 CPCAP_BATTERY_IRQ_ACTION_POWEROFF,
71 struct cpcap_interrupt_desc {
73 struct list_head node;
75 enum cpcap_battery_irq_action action;
78 struct cpcap_battery_config {
81 struct power_supply_info info;
84 struct cpcap_coulomb_counter_data {
85 s32 sample; /* 24-bits */
87 s16 offset; /* 10-bits */
90 enum cpcap_battery_state {
91 CPCAP_BATTERY_STATE_PREVIOUS,
92 CPCAP_BATTERY_STATE_LATEST,
93 CPCAP_BATTERY_STATE_NR,
96 struct cpcap_battery_state_data {
102 struct cpcap_coulomb_counter_data cc;
105 struct cpcap_battery_ddata {
108 struct list_head irq_list;
109 struct iio_channel *channels[CPCAP_BATTERY_IIO_NR];
110 struct power_supply *psy;
111 struct cpcap_battery_config config;
112 struct cpcap_battery_state_data state[CPCAP_BATTERY_STATE_NR];
118 #define CPCAP_NO_BATTERY -400
120 static struct cpcap_battery_state_data *
121 cpcap_battery_get_state(struct cpcap_battery_ddata *ddata,
122 enum cpcap_battery_state state)
124 if (state >= CPCAP_BATTERY_STATE_NR)
127 return &ddata->state[state];
130 static struct cpcap_battery_state_data *
131 cpcap_battery_latest(struct cpcap_battery_ddata *ddata)
133 return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_LATEST);
136 static struct cpcap_battery_state_data *
137 cpcap_battery_previous(struct cpcap_battery_ddata *ddata)
139 return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_PREVIOUS);
142 static int cpcap_charger_battery_temperature(struct cpcap_battery_ddata *ddata,
145 struct iio_channel *channel;
148 channel = ddata->channels[CPCAP_BATTERY_IIO_BATTDET];
149 error = iio_read_channel_processed(channel, value);
151 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
152 *value = CPCAP_NO_BATTERY;
162 static int cpcap_battery_get_voltage(struct cpcap_battery_ddata *ddata)
164 struct iio_channel *channel;
165 int error, value = 0;
167 channel = ddata->channels[CPCAP_BATTERY_IIO_VOLTAGE];
168 error = iio_read_channel_processed(channel, &value);
170 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
178 static int cpcap_battery_get_current(struct cpcap_battery_ddata *ddata)
180 struct iio_channel *channel;
181 int error, value = 0;
183 channel = ddata->channels[CPCAP_BATTERY_IIO_BATT_CURRENT];
184 error = iio_read_channel_processed(channel, &value);
186 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
195 * cpcap_battery_cc_raw_div - calculate and divide coulomb counter μAms values
196 * @ddata: device driver data
197 * @sample: coulomb counter sample value
198 * @accumulator: coulomb counter integrator value
199 * @offset: coulomb counter offset value
200 * @divider: conversion divider
202 * Note that cc_lsb and cc_dur values are from Motorola Linux kernel
203 * function data_get_avg_curr_ua() and seem to be based on measured test
204 * results. It also has the following comment:
206 * Adjustment factors are applied here as a temp solution per the test
207 * results. Need to work out a formal solution for this adjustment.
209 * A coulomb counter for similar hardware seems to be documented in
210 * "TWL6030 Gas Gauging Basics (Rev. A)" swca095a.pdf in chapter
211 * "10 Calculating Accumulated Current". We however follow what the
212 * Motorola mapphone Linux kernel is doing as there may be either a
213 * TI or ST coulomb counter in the PMIC.
215 static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata,
216 u32 sample, s32 accumulator,
217 s16 offset, u32 divider)
224 sample &= 0xffffff; /* 24-bits, unsigned */
225 offset &= 0x7ff; /* 10-bits, signed */
227 switch (ddata->vendor) {
228 case CPCAP_VENDOR_ST:
229 cc_lsb = 95374; /* μAms per LSB */
231 case CPCAP_VENDOR_TI:
232 cc_lsb = 91501; /* μAms per LSB */
239 acc = acc - ((s64)sample * offset);
240 cc_lsb = (cc_lsb * ddata->config.cd_factor) / 1000;
248 do_div(tmp, divider);
257 /* 3600000μAms = 1μAh */
258 static int cpcap_battery_cc_to_uah(struct cpcap_battery_ddata *ddata,
259 u32 sample, s32 accumulator,
262 return cpcap_battery_cc_raw_div(ddata, sample,
267 static int cpcap_battery_cc_to_ua(struct cpcap_battery_ddata *ddata,
268 u32 sample, s32 accumulator,
271 return cpcap_battery_cc_raw_div(ddata, sample,
274 CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS);
278 * cpcap_battery_read_accumulated - reads cpcap coulomb counter
279 * @ddata: device driver data
280 * @regs: coulomb counter values
282 * Based on Motorola mapphone kernel function data_read_regs().
283 * Looking at the registers, the coulomb counter seems similar to
284 * the coulomb counter in TWL6030. See "TWL6030 Gas Gauging Basics
285 * (Rev. A) swca095a.pdf for "10 Calculating Accumulated Current".
287 * Note that swca095a.pdf instructs to stop the coulomb counter
288 * before reading to avoid values changing. Motorola mapphone
289 * Linux kernel does not do it, so let's assume they've verified
290 * the data produced is correct.
293 cpcap_battery_read_accumulated(struct cpcap_battery_ddata *ddata,
294 struct cpcap_coulomb_counter_data *ccd)
296 u16 buf[7]; /* CPCAP_REG_CC1 to CCI */
300 ccd->accumulator = 0;
303 /* Read coulomb counter register range */
304 error = regmap_bulk_read(ddata->reg, CPCAP_REG_CCS1,
305 buf, ARRAY_SIZE(buf));
309 /* Sample value CPCAP_REG_CCS1 & 2 */
310 ccd->sample = (buf[1] & 0x0fff) << 16;
311 ccd->sample |= buf[0];
313 /* Accumulator value CPCAP_REG_CCA1 & 2 */
314 ccd->accumulator = ((s16)buf[3]) << 16;
315 ccd->accumulator |= buf[2];
317 /* Offset value CPCAP_REG_CCO */
318 ccd->offset = buf[5];
320 /* Adjust offset based on mode value CPCAP_REG_CCM? */
322 ccd->offset |= 0xfc00;
324 return cpcap_battery_cc_to_uah(ddata,
331 * cpcap_battery_cc_get_avg_current - read cpcap coulumb counter
332 * @ddata: cpcap battery driver device data
334 static int cpcap_battery_cc_get_avg_current(struct cpcap_battery_ddata *ddata)
336 int value, acc, error;
340 if (ddata->vendor == CPCAP_VENDOR_ST)
343 /* Coulomb counter integrator */
344 error = regmap_read(ddata->reg, CPCAP_REG_CCI, &value);
348 if ((ddata->vendor == CPCAP_VENDOR_TI) && (value > 0x2000))
349 value = value | 0xc000;
353 /* Coulomb counter sample time */
354 error = regmap_read(ddata->reg, CPCAP_REG_CCM, &value);
361 offset = value | 0xfc00;
363 return cpcap_battery_cc_to_ua(ddata, sample, acc, offset);
366 static bool cpcap_battery_full(struct cpcap_battery_ddata *ddata)
368 struct cpcap_battery_state_data *state = cpcap_battery_latest(ddata);
370 /* Basically anything that measures above 4347000 is full */
371 if (state->voltage >= (ddata->config.info.voltage_max_design - 4000))
377 static int cpcap_battery_update_status(struct cpcap_battery_ddata *ddata)
379 struct cpcap_battery_state_data state, *latest, *previous;
383 memset(&state, 0, sizeof(state));
386 latest = cpcap_battery_latest(ddata);
388 s64 delta_ms = ktime_to_ms(ktime_sub(now, latest->time));
390 if (delta_ms < CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS)
395 state.voltage = cpcap_battery_get_voltage(ddata);
396 state.current_ua = cpcap_battery_get_current(ddata);
397 state.counter_uah = cpcap_battery_read_accumulated(ddata, &state.cc);
399 error = cpcap_charger_battery_temperature(ddata,
404 previous = cpcap_battery_previous(ddata);
405 memcpy(previous, latest, sizeof(*previous));
406 memcpy(latest, &state, sizeof(*latest));
411 static enum power_supply_property cpcap_battery_props[] = {
412 POWER_SUPPLY_PROP_STATUS,
413 POWER_SUPPLY_PROP_PRESENT,
414 POWER_SUPPLY_PROP_TECHNOLOGY,
415 POWER_SUPPLY_PROP_VOLTAGE_NOW,
416 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
417 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
418 POWER_SUPPLY_PROP_CURRENT_AVG,
419 POWER_SUPPLY_PROP_CURRENT_NOW,
420 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
421 POWER_SUPPLY_PROP_CHARGE_COUNTER,
422 POWER_SUPPLY_PROP_POWER_NOW,
423 POWER_SUPPLY_PROP_POWER_AVG,
424 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
425 POWER_SUPPLY_PROP_SCOPE,
426 POWER_SUPPLY_PROP_TEMP,
429 static int cpcap_battery_get_property(struct power_supply *psy,
430 enum power_supply_property psp,
431 union power_supply_propval *val)
433 struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
434 struct cpcap_battery_state_data *latest, *previous;
440 cached = cpcap_battery_update_status(ddata);
444 latest = cpcap_battery_latest(ddata);
445 previous = cpcap_battery_previous(ddata);
448 case POWER_SUPPLY_PROP_PRESENT:
449 if (latest->temperature > CPCAP_NO_BATTERY)
454 case POWER_SUPPLY_PROP_STATUS:
455 if (cpcap_battery_full(ddata)) {
456 val->intval = POWER_SUPPLY_STATUS_FULL;
459 if (cpcap_battery_cc_get_avg_current(ddata) < 0)
460 val->intval = POWER_SUPPLY_STATUS_CHARGING;
462 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
464 case POWER_SUPPLY_PROP_TECHNOLOGY:
465 val->intval = ddata->config.info.technology;
467 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
468 val->intval = cpcap_battery_get_voltage(ddata);
470 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
471 val->intval = ddata->config.info.voltage_max_design;
473 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
474 val->intval = ddata->config.info.voltage_min_design;
476 case POWER_SUPPLY_PROP_CURRENT_AVG:
478 val->intval = cpcap_battery_cc_get_avg_current(ddata);
481 sample = latest->cc.sample - previous->cc.sample;
482 accumulator = latest->cc.accumulator - previous->cc.accumulator;
483 val->intval = cpcap_battery_cc_to_ua(ddata, sample,
487 case POWER_SUPPLY_PROP_CURRENT_NOW:
488 val->intval = latest->current_ua;
490 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
491 val->intval = latest->counter_uah;
493 case POWER_SUPPLY_PROP_POWER_NOW:
494 tmp = (latest->voltage / 10000) * latest->current_ua;
495 val->intval = div64_s64(tmp, 100);
497 case POWER_SUPPLY_PROP_POWER_AVG:
499 tmp = cpcap_battery_cc_get_avg_current(ddata);
500 tmp *= (latest->voltage / 10000);
501 val->intval = div64_s64(tmp, 100);
504 sample = latest->cc.sample - previous->cc.sample;
505 accumulator = latest->cc.accumulator - previous->cc.accumulator;
506 tmp = cpcap_battery_cc_to_ua(ddata, sample, accumulator,
508 tmp *= ((latest->voltage + previous->voltage) / 20000);
509 val->intval = div64_s64(tmp, 100);
511 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
512 if (cpcap_battery_full(ddata))
513 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
514 else if (latest->voltage >= 3750000)
515 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
516 else if (latest->voltage >= 3300000)
517 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
518 else if (latest->voltage > 3100000)
519 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
520 else if (latest->voltage <= 3100000)
521 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
523 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
525 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
526 val->intval = ddata->config.info.charge_full_design;
528 case POWER_SUPPLY_PROP_SCOPE:
529 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
531 case POWER_SUPPLY_PROP_TEMP:
532 val->intval = latest->temperature;
541 static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
543 struct cpcap_battery_ddata *ddata = data;
544 struct cpcap_battery_state_data *latest;
545 struct cpcap_interrupt_desc *d;
547 if (!atomic_read(&ddata->active))
550 list_for_each_entry(d, &ddata->irq_list, node) {
558 latest = cpcap_battery_latest(ddata);
561 case CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW:
562 if (latest->counter_uah >= 0)
563 dev_warn(ddata->dev, "Battery low at 3.3V!\n");
565 case CPCAP_BATTERY_IRQ_ACTION_POWEROFF:
566 if (latest->counter_uah >= 0) {
567 dev_emerg(ddata->dev,
568 "Battery empty at 3.1V, powering off\n");
569 orderly_poweroff(true);
576 power_supply_changed(ddata->psy);
581 static int cpcap_battery_init_irq(struct platform_device *pdev,
582 struct cpcap_battery_ddata *ddata,
585 struct cpcap_interrupt_desc *d;
588 irq = platform_get_irq_byname(pdev, name);
592 error = devm_request_threaded_irq(ddata->dev, irq, NULL,
593 cpcap_battery_irq_thread,
597 dev_err(ddata->dev, "could not get irq %s: %i\n",
603 d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
610 if (!strncmp(name, "lowbph", 6))
611 d->action = CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW;
612 else if (!strncmp(name, "lowbpl", 6))
613 d->action = CPCAP_BATTERY_IRQ_ACTION_POWEROFF;
615 list_add(&d->node, &ddata->irq_list);
620 static int cpcap_battery_init_interrupts(struct platform_device *pdev,
621 struct cpcap_battery_ddata *ddata)
623 const char * const cpcap_battery_irqs[] = {
624 "eol", "lowbph", "lowbpl",
625 "chrgcurr1", "battdetb"
629 for (i = 0; i < ARRAY_SIZE(cpcap_battery_irqs); i++) {
630 error = cpcap_battery_init_irq(pdev, ddata,
631 cpcap_battery_irqs[i]);
636 /* Enable low battery interrupts for 3.3V high and 3.1V low */
637 error = regmap_update_bits(ddata->reg, CPCAP_REG_BPEOL,
639 CPCAP_REG_BPEOL_BIT_BATTDETEN);
646 static int cpcap_battery_init_iio(struct cpcap_battery_ddata *ddata)
648 const char * const names[CPCAP_BATTERY_IIO_NR] = {
649 "battdetb", "battp", "chg_isense", "batti",
653 for (i = 0; i < CPCAP_BATTERY_IIO_NR; i++) {
654 ddata->channels[i] = devm_iio_channel_get(ddata->dev,
656 if (IS_ERR(ddata->channels[i])) {
657 error = PTR_ERR(ddata->channels[i]);
661 if (!ddata->channels[i]->indio_dev) {
670 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
677 * Based on the values from Motorola mapphone Linux kernel. In the
678 * the Motorola mapphone Linux kernel tree the value for pm_cd_factor
679 * is passed to the kernel via device tree. If it turns out to be
680 * something device specific we can consider that too later.
682 * And looking at the battery full and shutdown values for the stock
683 * kernel on droid 4, full is 4351000 and software initiates shutdown
684 * at 3078000. The device will die around 2743000.
686 static const struct cpcap_battery_config cpcap_battery_default_data = {
689 .info.technology = POWER_SUPPLY_TECHNOLOGY_LION,
690 .info.voltage_max_design = 4351000,
691 .info.voltage_min_design = 3100000,
692 .info.charge_full_design = 1740000,
696 static const struct of_device_id cpcap_battery_id_table[] = {
698 .compatible = "motorola,cpcap-battery",
699 .data = &cpcap_battery_default_data,
703 MODULE_DEVICE_TABLE(of, cpcap_battery_id_table);
706 static int cpcap_battery_probe(struct platform_device *pdev)
708 struct power_supply_desc *psy_desc;
709 struct cpcap_battery_ddata *ddata;
710 const struct of_device_id *match;
711 struct power_supply_config psy_cfg = {};
714 match = of_match_device(of_match_ptr(cpcap_battery_id_table),
720 dev_err(&pdev->dev, "no configuration data found\n");
725 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
729 INIT_LIST_HEAD(&ddata->irq_list);
730 ddata->dev = &pdev->dev;
731 memcpy(&ddata->config, match->data, sizeof(ddata->config));
733 ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
737 error = cpcap_get_vendor(ddata->dev, ddata->reg, &ddata->vendor);
741 platform_set_drvdata(pdev, ddata);
743 error = regmap_update_bits(ddata->reg, CPCAP_REG_CCM,
744 0xffff, ddata->config.ccm);
748 error = cpcap_battery_init_interrupts(pdev, ddata);
752 error = cpcap_battery_init_iio(ddata);
756 psy_desc = devm_kzalloc(ddata->dev, sizeof(*psy_desc), GFP_KERNEL);
760 psy_desc->name = "battery",
761 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY,
762 psy_desc->properties = cpcap_battery_props,
763 psy_desc->num_properties = ARRAY_SIZE(cpcap_battery_props),
764 psy_desc->get_property = cpcap_battery_get_property,
766 psy_cfg.of_node = pdev->dev.of_node;
767 psy_cfg.drv_data = ddata;
769 ddata->psy = devm_power_supply_register(ddata->dev, psy_desc,
771 error = PTR_ERR_OR_ZERO(ddata->psy);
773 dev_err(ddata->dev, "failed to register power supply\n");
777 atomic_set(&ddata->active, 1);
782 static int cpcap_battery_remove(struct platform_device *pdev)
784 struct cpcap_battery_ddata *ddata = platform_get_drvdata(pdev);
787 atomic_set(&ddata->active, 0);
788 error = regmap_update_bits(ddata->reg, CPCAP_REG_BPEOL,
791 dev_err(&pdev->dev, "could not disable: %i\n", error);
796 static struct platform_driver cpcap_battery_driver = {
798 .name = "cpcap_battery",
799 .of_match_table = of_match_ptr(cpcap_battery_id_table),
801 .probe = cpcap_battery_probe,
802 .remove = cpcap_battery_remove,
804 module_platform_driver(cpcap_battery_driver);
806 MODULE_LICENSE("GPL v2");
807 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
808 MODULE_DESCRIPTION("CPCAP PMIC Battery Driver");