1 /*
2  * Battery driver for CPCAP PMIC
3  *
4  * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
5  *
6  * Some parts of the code based on earlie Motorola mapphone Linux kernel
7  * drivers:
8  *
9  * Copyright (C) 2009-2010 Motorola, Inc.
10  *
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.
14 
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.
19  */
20 
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>
31 
32 #include <linux/iio/consumer.h>
33 #include <linux/iio/types.h>
34 #include <linux/mfd/motorola-cpcap.h>
35 
36 #include <asm/div64.h>
37 
38 /*
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.
43  */
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 */
54 
55 #define CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS	250
56 
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,
63 };
64 
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,
69 };
70 
71 struct cpcap_interrupt_desc {
72 	const char *name;
73 	struct list_head node;
74 	int irq;
75 	enum cpcap_battery_irq_action action;
76 };
77 
78 struct cpcap_battery_config {
79 	int ccm;
80 	int cd_factor;
81 	struct power_supply_info info;
82 };
83 
84 struct cpcap_coulomb_counter_data {
85 	s32 sample;		/* 24-bits */
86 	s32 accumulator;
87 	s16 offset;		/* 10-bits */
88 };
89 
90 enum cpcap_battery_state {
91 	CPCAP_BATTERY_STATE_PREVIOUS,
92 	CPCAP_BATTERY_STATE_LATEST,
93 	CPCAP_BATTERY_STATE_NR,
94 };
95 
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;
103 };
104 
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;
116 };
117 
118 #define CPCAP_NO_BATTERY	-400
119 
120 static struct cpcap_battery_state_data *
121 cpcap_battery_get_state(struct cpcap_battery_ddata *ddata,
122 			enum cpcap_battery_state state)
123 {
124 	if (state >= CPCAP_BATTERY_STATE_NR)
125 		return NULL;
126 
127 	return &ddata->state[state];
128 }
129 
130 static struct cpcap_battery_state_data *
131 cpcap_battery_latest(struct cpcap_battery_ddata *ddata)
132 {
133 	return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_LATEST);
134 }
135 
136 static struct cpcap_battery_state_data *
137 cpcap_battery_previous(struct cpcap_battery_ddata *ddata)
138 {
139 	return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_PREVIOUS);
140 }
141 
142 static int cpcap_charger_battery_temperature(struct cpcap_battery_ddata *ddata,
143 					     int *value)
144 {
145 	struct iio_channel *channel;
146 	int error;
147 
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;
153 
154 		return error;
155 	}
156 
157 	*value /= 100;
158 
159 	return 0;
160 }
161 
162 static int cpcap_battery_get_voltage(struct cpcap_battery_ddata *ddata)
163 {
164 	struct iio_channel *channel;
165 	int error, value = 0;
166 
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);
171 
172 		return 0;
173 	}
174 
175 	return value * 1000;
176 }
177 
178 static int cpcap_battery_get_current(struct cpcap_battery_ddata *ddata)
179 {
180 	struct iio_channel *channel;
181 	int error, value = 0;
182 
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);
187 
188 		return 0;
189 	}
190 
191 	return value * 1000;
192 }
193 
194 /**
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
201  *
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:
205  *
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.
208  *
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.
214  */
215 static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata,
216 				    u32 sample, s32 accumulator,
217 				    s16 offset, u32 divider)
218 {
219 	s64 acc;
220 	u64 tmp;
221 	int avg_current;
222 	u32 cc_lsb;
223 
224 	if (!divider)
225 		return 0;
226 
227 	sample &= 0xffffff;		/* 24-bits, unsigned */
228 	offset &= 0x7ff;		/* 10-bits, signed */
229 
230 	switch (ddata->vendor) {
231 	case CPCAP_VENDOR_ST:
232 		cc_lsb = 95374;		/* μAms per LSB */
233 		break;
234 	case CPCAP_VENDOR_TI:
235 		cc_lsb = 91501;		/* μAms per LSB */
236 		break;
237 	default:
238 		return -EINVAL;
239 	}
240 
241 	acc = accumulator;
242 	acc = acc - ((s64)sample * offset);
243 	cc_lsb = (cc_lsb * ddata->config.cd_factor) / 1000;
244 
245 	if (acc >=  0)
246 		tmp = acc;
247 	else
248 		tmp = acc * -1;
249 
250 	tmp = tmp * cc_lsb;
251 	do_div(tmp, divider);
252 	avg_current = tmp;
253 
254 	if (acc >= 0)
255 		return -avg_current;
256 	else
257 		return avg_current;
258 }
259 
260 /* 3600000μAms = 1μAh */
261 static int cpcap_battery_cc_to_uah(struct cpcap_battery_ddata *ddata,
262 				   u32 sample, s32 accumulator,
263 				   s16 offset)
264 {
265 	return cpcap_battery_cc_raw_div(ddata, sample,
266 					accumulator, offset,
267 					3600000);
268 }
269 
270 static int cpcap_battery_cc_to_ua(struct cpcap_battery_ddata *ddata,
271 				  u32 sample, s32 accumulator,
272 				  s16 offset)
273 {
274 	return cpcap_battery_cc_raw_div(ddata, sample,
275 					accumulator, offset,
276 					sample *
277 					CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS);
278 }
279 
280 /**
281  * cpcap_battery_read_accumulated - reads cpcap coulomb counter
282  * @ddata: device driver data
283  * @regs: coulomb counter values
284  *
285  * Based on Motorola mapphone kernel function data_read_regs().
286  * Looking at the registers, the coulomb counter seems similar to
287  * the coulomb counter in TWL6030. See "TWL6030 Gas Gauging Basics
288  * (Rev. A) swca095a.pdf for "10 Calculating Accumulated Current".
289  *
290  * Note that swca095a.pdf instructs to stop the coulomb counter
291  * before reading to avoid values changing. Motorola mapphone
292  * Linux kernel does not do it, so let's assume they've verified
293  * the data produced is correct.
294  */
295 static int
296 cpcap_battery_read_accumulated(struct cpcap_battery_ddata *ddata,
297 			       struct cpcap_coulomb_counter_data *ccd)
298 {
299 	u16 buf[7];	/* CPCAP_REG_CC1 to CCI */
300 	int error;
301 
302 	ccd->sample = 0;
303 	ccd->accumulator = 0;
304 	ccd->offset = 0;
305 
306 	/* Read coulomb counter register range */
307 	error = regmap_bulk_read(ddata->reg, CPCAP_REG_CCS1,
308 				 buf, ARRAY_SIZE(buf));
309 	if (error)
310 		return 0;
311 
312 	/* Sample value CPCAP_REG_CCS1 & 2 */
313 	ccd->sample = (buf[1] & 0x0fff) << 16;
314 	ccd->sample |= buf[0];
315 
316 	/* Accumulator value CPCAP_REG_CCA1 & 2 */
317 	ccd->accumulator = ((s16)buf[3]) << 16;
318 	ccd->accumulator |= buf[2];
319 
320 	/* Offset value CPCAP_REG_CCO */
321 	ccd->offset = buf[5];
322 
323 	/* Adjust offset based on mode value CPCAP_REG_CCM? */
324 	if (buf[4] >= 0x200)
325 		ccd->offset |= 0xfc00;
326 
327 	return cpcap_battery_cc_to_uah(ddata,
328 				       ccd->sample,
329 				       ccd->accumulator,
330 				       ccd->offset);
331 }
332 
333 /**
334  * cpcap_battery_cc_get_avg_current - read cpcap coulumb counter
335  * @ddata: cpcap battery driver device data
336  */
337 static int cpcap_battery_cc_get_avg_current(struct cpcap_battery_ddata *ddata)
338 {
339 	int value, acc, error;
340 	s32 sample = 1;
341 	s16 offset;
342 
343 	if (ddata->vendor == CPCAP_VENDOR_ST)
344 		sample = 4;
345 
346 	/* Coulomb counter integrator */
347 	error = regmap_read(ddata->reg, CPCAP_REG_CCI, &value);
348 	if (error)
349 		return error;
350 
351 	if ((ddata->vendor == CPCAP_VENDOR_TI) && (value > 0x2000))
352 		value = value | 0xc000;
353 
354 	acc = (s16)value;
355 
356 	/* Coulomb counter sample time */
357 	error = regmap_read(ddata->reg, CPCAP_REG_CCM, &value);
358 	if (error)
359 		return error;
360 
361 	if (value < 0x200)
362 		offset = value;
363 	else
364 		offset = value | 0xfc00;
365 
366 	return cpcap_battery_cc_to_ua(ddata, sample, acc, offset);
367 }
368 
369 static bool cpcap_battery_full(struct cpcap_battery_ddata *ddata)
370 {
371 	struct cpcap_battery_state_data *state = cpcap_battery_latest(ddata);
372 
373 	/* Basically anything that measures above 4347000 is full */
374 	if (state->voltage >= (ddata->config.info.voltage_max_design - 4000))
375 		return true;
376 
377 	return false;
378 }
379 
380 static int cpcap_battery_update_status(struct cpcap_battery_ddata *ddata)
381 {
382 	struct cpcap_battery_state_data state, *latest, *previous;
383 	ktime_t now;
384 	int error;
385 
386 	memset(&state, 0, sizeof(state));
387 	now = ktime_get();
388 
389 	latest = cpcap_battery_latest(ddata);
390 	if (latest) {
391 		s64 delta_ms = ktime_to_ms(ktime_sub(now, latest->time));
392 
393 		if (delta_ms < CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS)
394 			return delta_ms;
395 	}
396 
397 	state.time = now;
398 	state.voltage = cpcap_battery_get_voltage(ddata);
399 	state.current_ua = cpcap_battery_get_current(ddata);
400 	state.counter_uah = cpcap_battery_read_accumulated(ddata, &state.cc);
401 
402 	error = cpcap_charger_battery_temperature(ddata,
403 						  &state.temperature);
404 	if (error)
405 		return error;
406 
407 	previous = cpcap_battery_previous(ddata);
408 	memcpy(previous, latest, sizeof(*previous));
409 	memcpy(latest, &state, sizeof(*latest));
410 
411 	return 0;
412 }
413 
414 static enum power_supply_property cpcap_battery_props[] = {
415 	POWER_SUPPLY_PROP_STATUS,
416 	POWER_SUPPLY_PROP_PRESENT,
417 	POWER_SUPPLY_PROP_TECHNOLOGY,
418 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
419 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
420 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
421 	POWER_SUPPLY_PROP_CURRENT_AVG,
422 	POWER_SUPPLY_PROP_CURRENT_NOW,
423 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
424 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
425 	POWER_SUPPLY_PROP_POWER_NOW,
426 	POWER_SUPPLY_PROP_POWER_AVG,
427 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
428 	POWER_SUPPLY_PROP_SCOPE,
429 	POWER_SUPPLY_PROP_TEMP,
430 };
431 
432 static int cpcap_battery_get_property(struct power_supply *psy,
433 				      enum power_supply_property psp,
434 				      union power_supply_propval *val)
435 {
436 	struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
437 	struct cpcap_battery_state_data *latest, *previous;
438 	u32 sample;
439 	s32 accumulator;
440 	int cached;
441 	s64 tmp;
442 
443 	cached = cpcap_battery_update_status(ddata);
444 	if (cached < 0)
445 		return cached;
446 
447 	latest = cpcap_battery_latest(ddata);
448 	previous = cpcap_battery_previous(ddata);
449 
450 	switch (psp) {
451 	case POWER_SUPPLY_PROP_PRESENT:
452 		if (latest->temperature > CPCAP_NO_BATTERY)
453 			val->intval = 1;
454 		else
455 			val->intval = 0;
456 		break;
457 	case POWER_SUPPLY_PROP_STATUS:
458 		if (cpcap_battery_full(ddata)) {
459 			val->intval = POWER_SUPPLY_STATUS_FULL;
460 			break;
461 		}
462 		if (cpcap_battery_cc_get_avg_current(ddata) < 0)
463 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
464 		else
465 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
466 		break;
467 	case POWER_SUPPLY_PROP_TECHNOLOGY:
468 		val->intval = ddata->config.info.technology;
469 		break;
470 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
471 		val->intval = cpcap_battery_get_voltage(ddata);
472 		break;
473 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
474 		val->intval = ddata->config.info.voltage_max_design;
475 		break;
476 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
477 		val->intval = ddata->config.info.voltage_min_design;
478 		break;
479 	case POWER_SUPPLY_PROP_CURRENT_AVG:
480 		if (cached) {
481 			val->intval = cpcap_battery_cc_get_avg_current(ddata);
482 			break;
483 		}
484 		sample = latest->cc.sample - previous->cc.sample;
485 		accumulator = latest->cc.accumulator - previous->cc.accumulator;
486 		val->intval = cpcap_battery_cc_to_ua(ddata, sample,
487 						     accumulator,
488 						     latest->cc.offset);
489 		break;
490 	case POWER_SUPPLY_PROP_CURRENT_NOW:
491 		val->intval = latest->current_ua;
492 		break;
493 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
494 		val->intval = latest->counter_uah;
495 		break;
496 	case POWER_SUPPLY_PROP_POWER_NOW:
497 		tmp = (latest->voltage / 10000) * latest->current_ua;
498 		val->intval = div64_s64(tmp, 100);
499 		break;
500 	case POWER_SUPPLY_PROP_POWER_AVG:
501 		if (cached) {
502 			tmp = cpcap_battery_cc_get_avg_current(ddata);
503 			tmp *= (latest->voltage / 10000);
504 			val->intval = div64_s64(tmp, 100);
505 			break;
506 		}
507 		sample = latest->cc.sample - previous->cc.sample;
508 		accumulator = latest->cc.accumulator - previous->cc.accumulator;
509 		tmp = cpcap_battery_cc_to_ua(ddata, sample, accumulator,
510 					     latest->cc.offset);
511 		tmp *= ((latest->voltage + previous->voltage) / 20000);
512 		val->intval = div64_s64(tmp, 100);
513 		break;
514 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
515 		if (cpcap_battery_full(ddata))
516 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
517 		else if (latest->voltage >= 3750000)
518 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
519 		else if (latest->voltage >= 3300000)
520 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
521 		else if (latest->voltage > 3100000)
522 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
523 		else if (latest->voltage <= 3100000)
524 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
525 		else
526 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
527 		break;
528 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
529 		val->intval = ddata->config.info.charge_full_design;
530 		break;
531 	case POWER_SUPPLY_PROP_SCOPE:
532 		val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
533 		break;
534 	case POWER_SUPPLY_PROP_TEMP:
535 		val->intval = latest->temperature;
536 		break;
537 	default:
538 		return -EINVAL;
539 	}
540 
541 	return 0;
542 }
543 
544 static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
545 {
546 	struct cpcap_battery_ddata *ddata = data;
547 	struct cpcap_battery_state_data *latest;
548 	struct cpcap_interrupt_desc *d;
549 
550 	if (!atomic_read(&ddata->active))
551 		return IRQ_NONE;
552 
553 	list_for_each_entry(d, &ddata->irq_list, node) {
554 		if (irq == d->irq)
555 			break;
556 	}
557 
558 	if (!d)
559 		return IRQ_NONE;
560 
561 	latest = cpcap_battery_latest(ddata);
562 
563 	switch (d->action) {
564 	case CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW:
565 		if (latest->counter_uah >= 0)
566 			dev_warn(ddata->dev, "Battery low at 3.3V!\n");
567 		break;
568 	case CPCAP_BATTERY_IRQ_ACTION_POWEROFF:
569 		if (latest->counter_uah >= 0) {
570 			dev_emerg(ddata->dev,
571 				  "Battery empty at 3.1V, powering off\n");
572 			orderly_poweroff(true);
573 		}
574 		break;
575 	default:
576 		break;
577 	}
578 
579 	power_supply_changed(ddata->psy);
580 
581 	return IRQ_HANDLED;
582 }
583 
584 static int cpcap_battery_init_irq(struct platform_device *pdev,
585 				  struct cpcap_battery_ddata *ddata,
586 				  const char *name)
587 {
588 	struct cpcap_interrupt_desc *d;
589 	int irq, error;
590 
591 	irq = platform_get_irq_byname(pdev, name);
592 	if (irq < 0)
593 		return irq;
594 
595 	error = devm_request_threaded_irq(ddata->dev, irq, NULL,
596 					  cpcap_battery_irq_thread,
597 					  IRQF_SHARED,
598 					  name, ddata);
599 	if (error) {
600 		dev_err(ddata->dev, "could not get irq %s: %i\n",
601 			name, error);
602 
603 		return error;
604 	}
605 
606 	d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
607 	if (!d)
608 		return -ENOMEM;
609 
610 	d->name = name;
611 	d->irq = irq;
612 
613 	if (!strncmp(name, "lowbph", 6))
614 		d->action = CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW;
615 	else if (!strncmp(name, "lowbpl", 6))
616 		d->action = CPCAP_BATTERY_IRQ_ACTION_POWEROFF;
617 
618 	list_add(&d->node, &ddata->irq_list);
619 
620 	return 0;
621 }
622 
623 static int cpcap_battery_init_interrupts(struct platform_device *pdev,
624 					 struct cpcap_battery_ddata *ddata)
625 {
626 	static const char * const cpcap_battery_irqs[] = {
627 		"eol", "lowbph", "lowbpl",
628 		"chrgcurr1", "battdetb"
629 	};
630 	int i, error;
631 
632 	for (i = 0; i < ARRAY_SIZE(cpcap_battery_irqs); i++) {
633 		error = cpcap_battery_init_irq(pdev, ddata,
634 					       cpcap_battery_irqs[i]);
635 		if (error)
636 			return error;
637 	}
638 
639 	/* Enable low battery interrupts for 3.3V high and 3.1V low */
640 	error = regmap_update_bits(ddata->reg, CPCAP_REG_BPEOL,
641 				   0xffff,
642 				   CPCAP_REG_BPEOL_BIT_BATTDETEN);
643 	if (error)
644 		return error;
645 
646 	return 0;
647 }
648 
649 static int cpcap_battery_init_iio(struct cpcap_battery_ddata *ddata)
650 {
651 	const char * const names[CPCAP_BATTERY_IIO_NR] = {
652 		"battdetb", "battp", "chg_isense", "batti",
653 	};
654 	int error, i;
655 
656 	for (i = 0; i < CPCAP_BATTERY_IIO_NR; i++) {
657 		ddata->channels[i] = devm_iio_channel_get(ddata->dev,
658 							  names[i]);
659 		if (IS_ERR(ddata->channels[i])) {
660 			error = PTR_ERR(ddata->channels[i]);
661 			goto out_err;
662 		}
663 
664 		if (!ddata->channels[i]->indio_dev) {
665 			error = -ENXIO;
666 			goto out_err;
667 		}
668 	}
669 
670 	return 0;
671 
672 out_err:
673 	dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
674 		error);
675 
676 	return error;
677 }
678 
679 /*
680  * Based on the values from Motorola mapphone Linux kernel. In the
681  * the Motorola mapphone Linux kernel tree the value for pm_cd_factor
682  * is passed to the kernel via device tree. If it turns out to be
683  * something device specific we can consider that too later.
684  *
685  * And looking at the battery full and shutdown values for the stock
686  * kernel on droid 4, full is 4351000 and software initiates shutdown
687  * at 3078000. The device will die around 2743000.
688  */
689 static const struct cpcap_battery_config cpcap_battery_default_data = {
690 	.ccm = 0x3ff,
691 	.cd_factor = 0x3cc,
692 	.info.technology = POWER_SUPPLY_TECHNOLOGY_LION,
693 	.info.voltage_max_design = 4351000,
694 	.info.voltage_min_design = 3100000,
695 	.info.charge_full_design = 1740000,
696 };
697 
698 #ifdef CONFIG_OF
699 static const struct of_device_id cpcap_battery_id_table[] = {
700 	{
701 		.compatible = "motorola,cpcap-battery",
702 		.data = &cpcap_battery_default_data,
703 	},
704 	{},
705 };
706 MODULE_DEVICE_TABLE(of, cpcap_battery_id_table);
707 #endif
708 
709 static int cpcap_battery_probe(struct platform_device *pdev)
710 {
711 	struct power_supply_desc *psy_desc;
712 	struct cpcap_battery_ddata *ddata;
713 	const struct of_device_id *match;
714 	struct power_supply_config psy_cfg = {};
715 	int error;
716 
717 	match = of_match_device(of_match_ptr(cpcap_battery_id_table),
718 				&pdev->dev);
719 	if (!match)
720 		return -EINVAL;
721 
722 	if (!match->data) {
723 		dev_err(&pdev->dev, "no configuration data found\n");
724 
725 		return -ENODEV;
726 	}
727 
728 	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
729 	if (!ddata)
730 		return -ENOMEM;
731 
732 	INIT_LIST_HEAD(&ddata->irq_list);
733 	ddata->dev = &pdev->dev;
734 	memcpy(&ddata->config, match->data, sizeof(ddata->config));
735 
736 	ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
737 	if (!ddata->reg)
738 		return -ENODEV;
739 
740 	error = cpcap_get_vendor(ddata->dev, ddata->reg, &ddata->vendor);
741 	if (error)
742 		return error;
743 
744 	platform_set_drvdata(pdev, ddata);
745 
746 	error = regmap_update_bits(ddata->reg, CPCAP_REG_CCM,
747 				   0xffff, ddata->config.ccm);
748 	if (error)
749 		return error;
750 
751 	error = cpcap_battery_init_interrupts(pdev, ddata);
752 	if (error)
753 		return error;
754 
755 	error = cpcap_battery_init_iio(ddata);
756 	if (error)
757 		return error;
758 
759 	psy_desc = devm_kzalloc(ddata->dev, sizeof(*psy_desc), GFP_KERNEL);
760 	if (!psy_desc)
761 		return -ENOMEM;
762 
763 	psy_desc->name = "battery",
764 	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY,
765 	psy_desc->properties = cpcap_battery_props,
766 	psy_desc->num_properties = ARRAY_SIZE(cpcap_battery_props),
767 	psy_desc->get_property = cpcap_battery_get_property,
768 
769 	psy_cfg.of_node = pdev->dev.of_node;
770 	psy_cfg.drv_data = ddata;
771 
772 	ddata->psy = devm_power_supply_register(ddata->dev, psy_desc,
773 						&psy_cfg);
774 	error = PTR_ERR_OR_ZERO(ddata->psy);
775 	if (error) {
776 		dev_err(ddata->dev, "failed to register power supply\n");
777 		return error;
778 	}
779 
780 	atomic_set(&ddata->active, 1);
781 
782 	return 0;
783 }
784 
785 static int cpcap_battery_remove(struct platform_device *pdev)
786 {
787 	struct cpcap_battery_ddata *ddata = platform_get_drvdata(pdev);
788 	int error;
789 
790 	atomic_set(&ddata->active, 0);
791 	error = regmap_update_bits(ddata->reg, CPCAP_REG_BPEOL,
792 				   0xffff, 0);
793 	if (error)
794 		dev_err(&pdev->dev, "could not disable: %i\n", error);
795 
796 	return 0;
797 }
798 
799 static struct platform_driver cpcap_battery_driver = {
800 	.driver	= {
801 		.name		= "cpcap_battery",
802 		.of_match_table = of_match_ptr(cpcap_battery_id_table),
803 	},
804 	.probe	= cpcap_battery_probe,
805 	.remove = cpcap_battery_remove,
806 };
807 module_platform_driver(cpcap_battery_driver);
808 
809 MODULE_LICENSE("GPL v2");
810 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
811 MODULE_DESCRIPTION("CPCAP PMIC Battery Driver");
812