Linux 4.19.133
[linux/fpc-iii.git] / drivers / power / supply / cpcap-battery.c
blobe183a22de71538685d9e700041d079fd5ee4fda2
1 /*
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
7 * drivers:
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
57 enum {
58 CPCAP_BATTERY_IIO_BATTDET,
59 CPCAP_BATTERY_IIO_VOLTAGE,
60 CPCAP_BATTERY_IIO_CHRG_CURRENT,
61 CPCAP_BATTERY_IIO_BATT_CURRENT,
62 CPCAP_BATTERY_IIO_NR,
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 {
72 const char *name;
73 struct list_head node;
74 int irq;
75 enum cpcap_battery_irq_action action;
78 struct cpcap_battery_config {
79 int ccm;
80 int cd_factor;
81 struct power_supply_info info;
84 struct cpcap_coulomb_counter_data {
85 s32 sample; /* 24 or 32 bits */
86 s32 accumulator;
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 {
97 int voltage;
98 int current_ua;
99 int counter_uah;
100 int temperature;
101 ktime_t time;
102 struct cpcap_coulomb_counter_data cc;
105 struct cpcap_battery_ddata {
106 struct device *dev;
107 struct regmap *reg;
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];
113 atomic_t active;
114 int status;
115 u16 vendor;
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)
125 return NULL;
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,
143 int *value)
145 struct iio_channel *channel;
146 int error;
148 channel = ddata->channels[CPCAP_BATTERY_IIO_BATTDET];
149 error = iio_read_channel_processed(channel, value);
150 if (error < 0) {
151 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
152 *value = CPCAP_NO_BATTERY;
154 return error;
157 *value /= 100;
159 return 0;
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);
169 if (error < 0) {
170 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
172 return 0;
175 return value * 1000;
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);
185 if (error < 0) {
186 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
188 return 0;
191 return value * 1000;
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 s32 sample, s32 accumulator,
217 s16 offset, u32 divider)
219 s64 acc;
220 u64 tmp;
221 int avg_current;
222 u32 cc_lsb;
224 if (!divider)
225 return 0;
227 offset &= 0x7ff; /* 10-bits, signed */
229 switch (ddata->vendor) {
230 case CPCAP_VENDOR_ST:
231 cc_lsb = 95374; /* μAms per LSB */
232 break;
233 case CPCAP_VENDOR_TI:
234 cc_lsb = 91501; /* μAms per LSB */
235 break;
236 default:
237 return -EINVAL;
240 acc = accumulator;
241 acc = acc - ((s64)sample * offset);
242 cc_lsb = (cc_lsb * ddata->config.cd_factor) / 1000;
244 if (acc >= 0)
245 tmp = acc;
246 else
247 tmp = acc * -1;
249 tmp = tmp * cc_lsb;
250 do_div(tmp, divider);
251 avg_current = tmp;
253 if (acc >= 0)
254 return -avg_current;
255 else
256 return avg_current;
259 /* 3600000μAms = 1μAh */
260 static int cpcap_battery_cc_to_uah(struct cpcap_battery_ddata *ddata,
261 s32 sample, s32 accumulator,
262 s16 offset)
264 return cpcap_battery_cc_raw_div(ddata, sample,
265 accumulator, offset,
266 3600000);
269 static int cpcap_battery_cc_to_ua(struct cpcap_battery_ddata *ddata,
270 s32 sample, s32 accumulator,
271 s16 offset)
273 return cpcap_battery_cc_raw_div(ddata, sample,
274 accumulator, offset,
275 sample *
276 CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS);
280 * cpcap_battery_read_accumulated - reads cpcap coulomb counter
281 * @ddata: device driver data
282 * @regs: coulomb counter values
284 * Based on Motorola mapphone kernel function data_read_regs().
285 * Looking at the registers, the coulomb counter seems similar to
286 * the coulomb counter in TWL6030. See "TWL6030 Gas Gauging Basics
287 * (Rev. A) swca095a.pdf for "10 Calculating Accumulated Current".
289 * Note that swca095a.pdf instructs to stop the coulomb counter
290 * before reading to avoid values changing. Motorola mapphone
291 * Linux kernel does not do it, so let's assume they've verified
292 * the data produced is correct.
294 static int
295 cpcap_battery_read_accumulated(struct cpcap_battery_ddata *ddata,
296 struct cpcap_coulomb_counter_data *ccd)
298 u16 buf[7]; /* CPCAP_REG_CC1 to CCI */
299 int error;
301 ccd->sample = 0;
302 ccd->accumulator = 0;
303 ccd->offset = 0;
305 /* Read coulomb counter register range */
306 error = regmap_bulk_read(ddata->reg, CPCAP_REG_CCS1,
307 buf, ARRAY_SIZE(buf));
308 if (error)
309 return 0;
311 /* Sample value CPCAP_REG_CCS1 & 2 */
312 ccd->sample = (buf[1] & 0x0fff) << 16;
313 ccd->sample |= buf[0];
314 if (ddata->vendor == CPCAP_VENDOR_TI)
315 ccd->sample = sign_extend32(24, ccd->sample);
317 /* Accumulator value CPCAP_REG_CCA1 & 2 */
318 ccd->accumulator = ((s16)buf[3]) << 16;
319 ccd->accumulator |= buf[2];
321 /* Offset value CPCAP_REG_CCO */
322 ccd->offset = buf[5];
324 /* Adjust offset based on mode value CPCAP_REG_CCM? */
325 if (buf[4] >= 0x200)
326 ccd->offset |= 0xfc00;
328 return cpcap_battery_cc_to_uah(ddata,
329 ccd->sample,
330 ccd->accumulator,
331 ccd->offset);
335 * cpcap_battery_cc_get_avg_current - read cpcap coulumb counter
336 * @ddata: cpcap battery driver device data
338 static int cpcap_battery_cc_get_avg_current(struct cpcap_battery_ddata *ddata)
340 int value, acc, error;
341 s32 sample = 1;
342 s16 offset;
344 if (ddata->vendor == CPCAP_VENDOR_ST)
345 sample = 4;
347 /* Coulomb counter integrator */
348 error = regmap_read(ddata->reg, CPCAP_REG_CCI, &value);
349 if (error)
350 return error;
352 if ((ddata->vendor == CPCAP_VENDOR_TI) && (value > 0x2000))
353 value = value | 0xc000;
355 acc = (s16)value;
357 /* Coulomb counter sample time */
358 error = regmap_read(ddata->reg, CPCAP_REG_CCM, &value);
359 if (error)
360 return error;
362 if (value < 0x200)
363 offset = value;
364 else
365 offset = value | 0xfc00;
367 return cpcap_battery_cc_to_ua(ddata, sample, acc, offset);
370 static bool cpcap_battery_full(struct cpcap_battery_ddata *ddata)
372 struct cpcap_battery_state_data *state = cpcap_battery_latest(ddata);
374 /* Basically anything that measures above 4347000 is full */
375 if (state->voltage >= (ddata->config.info.voltage_max_design - 4000))
376 return true;
378 return false;
381 static int cpcap_battery_update_status(struct cpcap_battery_ddata *ddata)
383 struct cpcap_battery_state_data state, *latest, *previous;
384 ktime_t now;
385 int error;
387 memset(&state, 0, sizeof(state));
388 now = ktime_get();
390 latest = cpcap_battery_latest(ddata);
391 if (latest) {
392 s64 delta_ms = ktime_to_ms(ktime_sub(now, latest->time));
394 if (delta_ms < CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS)
395 return delta_ms;
398 state.time = now;
399 state.voltage = cpcap_battery_get_voltage(ddata);
400 state.current_ua = cpcap_battery_get_current(ddata);
401 state.counter_uah = cpcap_battery_read_accumulated(ddata, &state.cc);
403 error = cpcap_charger_battery_temperature(ddata,
404 &state.temperature);
405 if (error)
406 return error;
408 previous = cpcap_battery_previous(ddata);
409 memcpy(previous, latest, sizeof(*previous));
410 memcpy(latest, &state, sizeof(*latest));
412 return 0;
415 static enum power_supply_property cpcap_battery_props[] = {
416 POWER_SUPPLY_PROP_STATUS,
417 POWER_SUPPLY_PROP_PRESENT,
418 POWER_SUPPLY_PROP_TECHNOLOGY,
419 POWER_SUPPLY_PROP_VOLTAGE_NOW,
420 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
421 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
422 POWER_SUPPLY_PROP_CURRENT_AVG,
423 POWER_SUPPLY_PROP_CURRENT_NOW,
424 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
425 POWER_SUPPLY_PROP_CHARGE_COUNTER,
426 POWER_SUPPLY_PROP_POWER_NOW,
427 POWER_SUPPLY_PROP_POWER_AVG,
428 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
429 POWER_SUPPLY_PROP_SCOPE,
430 POWER_SUPPLY_PROP_TEMP,
433 static int cpcap_battery_get_property(struct power_supply *psy,
434 enum power_supply_property psp,
435 union power_supply_propval *val)
437 struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
438 struct cpcap_battery_state_data *latest, *previous;
439 u32 sample;
440 s32 accumulator;
441 int cached;
442 s64 tmp;
444 cached = cpcap_battery_update_status(ddata);
445 if (cached < 0)
446 return cached;
448 latest = cpcap_battery_latest(ddata);
449 previous = cpcap_battery_previous(ddata);
451 switch (psp) {
452 case POWER_SUPPLY_PROP_PRESENT:
453 if (latest->temperature > CPCAP_NO_BATTERY)
454 val->intval = 1;
455 else
456 val->intval = 0;
457 break;
458 case POWER_SUPPLY_PROP_STATUS:
459 if (cpcap_battery_full(ddata)) {
460 val->intval = POWER_SUPPLY_STATUS_FULL;
461 break;
463 if (cpcap_battery_cc_get_avg_current(ddata) < 0)
464 val->intval = POWER_SUPPLY_STATUS_CHARGING;
465 else
466 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
467 break;
468 case POWER_SUPPLY_PROP_TECHNOLOGY:
469 val->intval = ddata->config.info.technology;
470 break;
471 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
472 val->intval = cpcap_battery_get_voltage(ddata);
473 break;
474 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
475 val->intval = ddata->config.info.voltage_max_design;
476 break;
477 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
478 val->intval = ddata->config.info.voltage_min_design;
479 break;
480 case POWER_SUPPLY_PROP_CURRENT_AVG:
481 if (cached) {
482 val->intval = cpcap_battery_cc_get_avg_current(ddata);
483 break;
485 sample = latest->cc.sample - previous->cc.sample;
486 accumulator = latest->cc.accumulator - previous->cc.accumulator;
487 val->intval = cpcap_battery_cc_to_ua(ddata, sample,
488 accumulator,
489 latest->cc.offset);
490 break;
491 case POWER_SUPPLY_PROP_CURRENT_NOW:
492 val->intval = latest->current_ua;
493 break;
494 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
495 val->intval = latest->counter_uah;
496 break;
497 case POWER_SUPPLY_PROP_POWER_NOW:
498 tmp = (latest->voltage / 10000) * latest->current_ua;
499 val->intval = div64_s64(tmp, 100);
500 break;
501 case POWER_SUPPLY_PROP_POWER_AVG:
502 if (cached) {
503 tmp = cpcap_battery_cc_get_avg_current(ddata);
504 tmp *= (latest->voltage / 10000);
505 val->intval = div64_s64(tmp, 100);
506 break;
508 sample = latest->cc.sample - previous->cc.sample;
509 accumulator = latest->cc.accumulator - previous->cc.accumulator;
510 tmp = cpcap_battery_cc_to_ua(ddata, sample, accumulator,
511 latest->cc.offset);
512 tmp *= ((latest->voltage + previous->voltage) / 20000);
513 val->intval = div64_s64(tmp, 100);
514 break;
515 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
516 if (cpcap_battery_full(ddata))
517 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
518 else if (latest->voltage >= 3750000)
519 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
520 else if (latest->voltage >= 3300000)
521 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
522 else if (latest->voltage > 3100000)
523 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
524 else if (latest->voltage <= 3100000)
525 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
526 else
527 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
528 break;
529 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
530 val->intval = ddata->config.info.charge_full_design;
531 break;
532 case POWER_SUPPLY_PROP_SCOPE:
533 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
534 break;
535 case POWER_SUPPLY_PROP_TEMP:
536 val->intval = latest->temperature;
537 break;
538 default:
539 return -EINVAL;
542 return 0;
545 static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
547 struct cpcap_battery_ddata *ddata = data;
548 struct cpcap_battery_state_data *latest;
549 struct cpcap_interrupt_desc *d;
551 if (!atomic_read(&ddata->active))
552 return IRQ_NONE;
554 list_for_each_entry(d, &ddata->irq_list, node) {
555 if (irq == d->irq)
556 break;
559 if (!d)
560 return IRQ_NONE;
562 latest = cpcap_battery_latest(ddata);
564 switch (d->action) {
565 case CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW:
566 if (latest->counter_uah >= 0)
567 dev_warn(ddata->dev, "Battery low at 3.3V!\n");
568 break;
569 case CPCAP_BATTERY_IRQ_ACTION_POWEROFF:
570 if (latest->counter_uah >= 0) {
571 dev_emerg(ddata->dev,
572 "Battery empty at 3.1V, powering off\n");
573 orderly_poweroff(true);
575 break;
576 default:
577 break;
580 power_supply_changed(ddata->psy);
582 return IRQ_HANDLED;
585 static int cpcap_battery_init_irq(struct platform_device *pdev,
586 struct cpcap_battery_ddata *ddata,
587 const char *name)
589 struct cpcap_interrupt_desc *d;
590 int irq, error;
592 irq = platform_get_irq_byname(pdev, name);
593 if (irq < 0)
594 return irq;
596 error = devm_request_threaded_irq(ddata->dev, irq, NULL,
597 cpcap_battery_irq_thread,
598 IRQF_SHARED,
599 name, ddata);
600 if (error) {
601 dev_err(ddata->dev, "could not get irq %s: %i\n",
602 name, error);
604 return error;
607 d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
608 if (!d)
609 return -ENOMEM;
611 d->name = name;
612 d->irq = irq;
614 if (!strncmp(name, "lowbph", 6))
615 d->action = CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW;
616 else if (!strncmp(name, "lowbpl", 6))
617 d->action = CPCAP_BATTERY_IRQ_ACTION_POWEROFF;
619 list_add(&d->node, &ddata->irq_list);
621 return 0;
624 static int cpcap_battery_init_interrupts(struct platform_device *pdev,
625 struct cpcap_battery_ddata *ddata)
627 const char * const cpcap_battery_irqs[] = {
628 "eol", "lowbph", "lowbpl",
629 "chrgcurr1", "battdetb"
631 int i, error;
633 for (i = 0; i < ARRAY_SIZE(cpcap_battery_irqs); i++) {
634 error = cpcap_battery_init_irq(pdev, ddata,
635 cpcap_battery_irqs[i]);
636 if (error)
637 return error;
640 /* Enable low battery interrupts for 3.3V high and 3.1V low */
641 error = regmap_update_bits(ddata->reg, CPCAP_REG_BPEOL,
642 0xffff,
643 CPCAP_REG_BPEOL_BIT_BATTDETEN);
644 if (error)
645 return error;
647 return 0;
650 static int cpcap_battery_init_iio(struct cpcap_battery_ddata *ddata)
652 const char * const names[CPCAP_BATTERY_IIO_NR] = {
653 "battdetb", "battp", "chg_isense", "batti",
655 int error, i;
657 for (i = 0; i < CPCAP_BATTERY_IIO_NR; i++) {
658 ddata->channels[i] = devm_iio_channel_get(ddata->dev,
659 names[i]);
660 if (IS_ERR(ddata->channels[i])) {
661 error = PTR_ERR(ddata->channels[i]);
662 goto out_err;
665 if (!ddata->channels[i]->indio_dev) {
666 error = -ENXIO;
667 goto out_err;
671 return 0;
673 out_err:
674 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
675 error);
677 return error;
681 * Based on the values from Motorola mapphone Linux kernel. In the
682 * the Motorola mapphone Linux kernel tree the value for pm_cd_factor
683 * is passed to the kernel via device tree. If it turns out to be
684 * something device specific we can consider that too later.
686 * And looking at the battery full and shutdown values for the stock
687 * kernel on droid 4, full is 4351000 and software initiates shutdown
688 * at 3078000. The device will die around 2743000.
690 static const struct cpcap_battery_config cpcap_battery_default_data = {
691 .ccm = 0x3ff,
692 .cd_factor = 0x3cc,
693 .info.technology = POWER_SUPPLY_TECHNOLOGY_LION,
694 .info.voltage_max_design = 4351000,
695 .info.voltage_min_design = 3100000,
696 .info.charge_full_design = 1740000,
699 #ifdef CONFIG_OF
700 static const struct of_device_id cpcap_battery_id_table[] = {
702 .compatible = "motorola,cpcap-battery",
703 .data = &cpcap_battery_default_data,
707 MODULE_DEVICE_TABLE(of, cpcap_battery_id_table);
708 #endif
710 static int cpcap_battery_probe(struct platform_device *pdev)
712 struct power_supply_desc *psy_desc;
713 struct cpcap_battery_ddata *ddata;
714 const struct of_device_id *match;
715 struct power_supply_config psy_cfg = {};
716 int error;
718 match = of_match_device(of_match_ptr(cpcap_battery_id_table),
719 &pdev->dev);
720 if (!match)
721 return -EINVAL;
723 if (!match->data) {
724 dev_err(&pdev->dev, "no configuration data found\n");
726 return -ENODEV;
729 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
730 if (!ddata)
731 return -ENOMEM;
733 INIT_LIST_HEAD(&ddata->irq_list);
734 ddata->dev = &pdev->dev;
735 memcpy(&ddata->config, match->data, sizeof(ddata->config));
737 ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
738 if (!ddata->reg)
739 return -ENODEV;
741 error = cpcap_get_vendor(ddata->dev, ddata->reg, &ddata->vendor);
742 if (error)
743 return error;
745 platform_set_drvdata(pdev, ddata);
747 error = regmap_update_bits(ddata->reg, CPCAP_REG_CCM,
748 0xffff, ddata->config.ccm);
749 if (error)
750 return error;
752 error = cpcap_battery_init_interrupts(pdev, ddata);
753 if (error)
754 return error;
756 error = cpcap_battery_init_iio(ddata);
757 if (error)
758 return error;
760 psy_desc = devm_kzalloc(ddata->dev, sizeof(*psy_desc), GFP_KERNEL);
761 if (!psy_desc)
762 return -ENOMEM;
764 psy_desc->name = "battery",
765 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY,
766 psy_desc->properties = cpcap_battery_props,
767 psy_desc->num_properties = ARRAY_SIZE(cpcap_battery_props),
768 psy_desc->get_property = cpcap_battery_get_property,
770 psy_cfg.of_node = pdev->dev.of_node;
771 psy_cfg.drv_data = ddata;
773 ddata->psy = devm_power_supply_register(ddata->dev, psy_desc,
774 &psy_cfg);
775 error = PTR_ERR_OR_ZERO(ddata->psy);
776 if (error) {
777 dev_err(ddata->dev, "failed to register power supply\n");
778 return error;
781 atomic_set(&ddata->active, 1);
783 return 0;
786 static int cpcap_battery_remove(struct platform_device *pdev)
788 struct cpcap_battery_ddata *ddata = platform_get_drvdata(pdev);
789 int error;
791 atomic_set(&ddata->active, 0);
792 error = regmap_update_bits(ddata->reg, CPCAP_REG_BPEOL,
793 0xffff, 0);
794 if (error)
795 dev_err(&pdev->dev, "could not disable: %i\n", error);
797 return 0;
800 static struct platform_driver cpcap_battery_driver = {
801 .driver = {
802 .name = "cpcap_battery",
803 .of_match_table = of_match_ptr(cpcap_battery_id_table),
805 .probe = cpcap_battery_probe,
806 .remove = cpcap_battery_remove,
808 module_platform_driver(cpcap_battery_driver);
810 MODULE_LICENSE("GPL v2");
811 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
812 MODULE_DESCRIPTION("CPCAP PMIC Battery Driver");