blob: 37be541e057d117d50fa631e788a08fdd13e4db7 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * Motorola CPCAP PMIC battery charger driver
3 *
4 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
5 *
6 * Rewritten for Linux power framework with some parts based on
7 * on earlier driver found in the Motorola Linux kernel:
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 in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
20
21#include <linux/atomic.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/err.h>
26#include <linux/interrupt.h>
27#include <linux/notifier.h>
28#include <linux/of.h>
29#include <linux/of_platform.h>
30#include <linux/platform_device.h>
31#include <linux/power_supply.h>
32#include <linux/regmap.h>
33
34#include <linux/gpio/consumer.h>
35#include <linux/usb/phy_companion.h>
36#include <linux/phy/omap_usb.h>
37#include <linux/usb/otg.h>
38#include <linux/iio/consumer.h>
39#include <linux/mfd/motorola-cpcap.h>
40
41/*
42 * CPCAP_REG_CRM register bits. For documentation of somewhat similar hardware,
43 * see NXP "MC13783 Power Management and Audio Circuit Users's Guide"
44 * MC13783UG.pdf chapter "8.5 Battery Interface Register Summary". The registers
45 * and values for CPCAP are different, but some of the internal components seem
46 * similar. Also see the Motorola Linux kernel cpcap-regbits.h. CPCAP_REG_CHRGR_1
47 * bits that seem to describe the CRM register.
48 */
49#define CPCAP_REG_CRM_UNUSED_641_15 BIT(15) /* 641 = register number */
50#define CPCAP_REG_CRM_UNUSED_641_14 BIT(14) /* 641 = register number */
51#define CPCAP_REG_CRM_CHRG_LED_EN BIT(13) /* Charger LED */
52#define CPCAP_REG_CRM_RVRSMODE BIT(12) /* USB VBUS output enable */
53#define CPCAP_REG_CRM_ICHRG_TR1 BIT(11) /* Trickle charge current */
54#define CPCAP_REG_CRM_ICHRG_TR0 BIT(10)
55#define CPCAP_REG_CRM_FET_OVRD BIT(9) /* 0 = hardware, 1 = FET_CTRL */
56#define CPCAP_REG_CRM_FET_CTRL BIT(8) /* BPFET 1 if FET_OVRD set */
57#define CPCAP_REG_CRM_VCHRG3 BIT(7) /* Charge voltage bits */
58#define CPCAP_REG_CRM_VCHRG2 BIT(6)
59#define CPCAP_REG_CRM_VCHRG1 BIT(5)
60#define CPCAP_REG_CRM_VCHRG0 BIT(4)
61#define CPCAP_REG_CRM_ICHRG3 BIT(3) /* Charge current bits */
62#define CPCAP_REG_CRM_ICHRG2 BIT(2)
63#define CPCAP_REG_CRM_ICHRG1 BIT(1)
64#define CPCAP_REG_CRM_ICHRG0 BIT(0)
65
66/* CPCAP_REG_CRM trickle charge voltages */
67#define CPCAP_REG_CRM_TR(val) (((val) & 0x3) << 10)
68#define CPCAP_REG_CRM_TR_0A00 CPCAP_REG_CRM_TR(0x0)
69#define CPCAP_REG_CRM_TR_0A24 CPCAP_REG_CRM_TR(0x1)
70#define CPCAP_REG_CRM_TR_0A48 CPCAP_REG_CRM_TR(0x2)
71#define CPCAP_REG_CRM_TR_0A72 CPCAP_REG_CRM_TR(0x4)
72
73/*
74 * CPCAP_REG_CRM charge voltages based on the ADC channel 1 values.
75 * Note that these register bits don't match MC13783UG.pdf VCHRG
76 * register bits.
77 */
78#define CPCAP_REG_CRM_VCHRG(val) (((val) & 0xf) << 4)
79#define CPCAP_REG_CRM_VCHRG_3V80 CPCAP_REG_CRM_VCHRG(0x0)
80#define CPCAP_REG_CRM_VCHRG_4V10 CPCAP_REG_CRM_VCHRG(0x1)
81#define CPCAP_REG_CRM_VCHRG_4V12 CPCAP_REG_CRM_VCHRG(0x2)
82#define CPCAP_REG_CRM_VCHRG_4V15 CPCAP_REG_CRM_VCHRG(0x3)
83#define CPCAP_REG_CRM_VCHRG_4V17 CPCAP_REG_CRM_VCHRG(0x4)
84#define CPCAP_REG_CRM_VCHRG_4V20 CPCAP_REG_CRM_VCHRG(0x5)
85#define CPCAP_REG_CRM_VCHRG_4V23 CPCAP_REG_CRM_VCHRG(0x6)
86#define CPCAP_REG_CRM_VCHRG_4V25 CPCAP_REG_CRM_VCHRG(0x7)
87#define CPCAP_REG_CRM_VCHRG_4V27 CPCAP_REG_CRM_VCHRG(0x8)
88#define CPCAP_REG_CRM_VCHRG_4V30 CPCAP_REG_CRM_VCHRG(0x9)
89#define CPCAP_REG_CRM_VCHRG_4V33 CPCAP_REG_CRM_VCHRG(0xa)
90#define CPCAP_REG_CRM_VCHRG_4V35 CPCAP_REG_CRM_VCHRG(0xb)
91#define CPCAP_REG_CRM_VCHRG_4V38 CPCAP_REG_CRM_VCHRG(0xc)
92#define CPCAP_REG_CRM_VCHRG_4V40 CPCAP_REG_CRM_VCHRG(0xd)
93#define CPCAP_REG_CRM_VCHRG_4V42 CPCAP_REG_CRM_VCHRG(0xe)
94#define CPCAP_REG_CRM_VCHRG_4V44 CPCAP_REG_CRM_VCHRG(0xf)
95
96/*
97 * CPCAP_REG_CRM charge currents. These seem to match MC13783UG.pdf
98 * values in "Table 8-3. Charge Path Regulator Current Limit
99 * Characteristics" for the nominal values.
100 */
101#define CPCAP_REG_CRM_ICHRG(val) (((val) & 0xf) << 0)
102#define CPCAP_REG_CRM_ICHRG_0A000 CPCAP_REG_CRM_ICHRG(0x0)
103#define CPCAP_REG_CRM_ICHRG_0A070 CPCAP_REG_CRM_ICHRG(0x1)
104#define CPCAP_REG_CRM_ICHRG_0A177 CPCAP_REG_CRM_ICHRG(0x2)
105#define CPCAP_REG_CRM_ICHRG_0A266 CPCAP_REG_CRM_ICHRG(0x3)
106#define CPCAP_REG_CRM_ICHRG_0A355 CPCAP_REG_CRM_ICHRG(0x4)
107#define CPCAP_REG_CRM_ICHRG_0A443 CPCAP_REG_CRM_ICHRG(0x5)
108#define CPCAP_REG_CRM_ICHRG_0A532 CPCAP_REG_CRM_ICHRG(0x6)
109#define CPCAP_REG_CRM_ICHRG_0A621 CPCAP_REG_CRM_ICHRG(0x7)
110#define CPCAP_REG_CRM_ICHRG_0A709 CPCAP_REG_CRM_ICHRG(0x8)
111#define CPCAP_REG_CRM_ICHRG_0A798 CPCAP_REG_CRM_ICHRG(0x9)
112#define CPCAP_REG_CRM_ICHRG_0A886 CPCAP_REG_CRM_ICHRG(0xa)
113#define CPCAP_REG_CRM_ICHRG_0A975 CPCAP_REG_CRM_ICHRG(0xb)
114#define CPCAP_REG_CRM_ICHRG_1A064 CPCAP_REG_CRM_ICHRG(0xc)
115#define CPCAP_REG_CRM_ICHRG_1A152 CPCAP_REG_CRM_ICHRG(0xd)
116#define CPCAP_REG_CRM_ICHRG_1A596 CPCAP_REG_CRM_ICHRG(0xe)
117#define CPCAP_REG_CRM_ICHRG_NO_LIMIT CPCAP_REG_CRM_ICHRG(0xf)
118
119enum {
120 CPCAP_CHARGER_IIO_BATTDET,
121 CPCAP_CHARGER_IIO_VOLTAGE,
122 CPCAP_CHARGER_IIO_VBUS,
123 CPCAP_CHARGER_IIO_CHRG_CURRENT,
124 CPCAP_CHARGER_IIO_BATT_CURRENT,
125 CPCAP_CHARGER_IIO_NR,
126};
127
128struct cpcap_charger_ddata {
129 struct device *dev;
130 struct regmap *reg;
131 struct list_head irq_list;
132 struct delayed_work detect_work;
133 struct delayed_work vbus_work;
134 struct gpio_desc *gpio[2]; /* gpio_reven0 & 1 */
135
136 struct iio_channel *channels[CPCAP_CHARGER_IIO_NR];
137
138 struct power_supply *usb;
139
140 struct phy_companion comparator; /* For USB VBUS */
141 bool vbus_enabled;
142 atomic_t active;
143
144 int status;
145};
146
147struct cpcap_interrupt_desc {
148 int irq;
149 struct list_head node;
150 const char *name;
151};
152
153struct cpcap_charger_ints_state {
154 bool chrg_det;
155 bool rvrs_chrg;
156 bool vbusov;
157
158 bool chrg_se1b;
159 bool rvrs_mode;
160 bool chrgcurr1;
161 bool vbusvld;
162
163 bool battdetb;
164};
165
166static enum power_supply_property cpcap_charger_props[] = {
167 POWER_SUPPLY_PROP_STATUS,
168 POWER_SUPPLY_PROP_ONLINE,
169 POWER_SUPPLY_PROP_VOLTAGE_NOW,
170 POWER_SUPPLY_PROP_CURRENT_NOW,
171};
172
173static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata)
174{
175 struct iio_channel *channel;
176 int error, value;
177
178 channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET];
179 error = iio_read_channel_raw(channel, &value);
180 if (error < 0) {
181 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
182
183 return false;
184 }
185
186 return value == 1;
187}
188
189static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata)
190{
191 struct iio_channel *channel;
192 int error, value = 0;
193
194 channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE];
195 error = iio_read_channel_processed(channel, &value);
196 if (error < 0) {
197 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
198
199 return 0;
200 }
201
202 return value;
203}
204
205static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata)
206{
207 struct iio_channel *channel;
208 int error, value = 0;
209
210 channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT];
211 error = iio_read_channel_processed(channel, &value);
212 if (error < 0) {
213 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
214
215 return 0;
216 }
217
218 return value;
219}
220
221static int cpcap_charger_get_property(struct power_supply *psy,
222 enum power_supply_property psp,
223 union power_supply_propval *val)
224{
225 struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
226
227 switch (psp) {
228 case POWER_SUPPLY_PROP_STATUS:
229 val->intval = ddata->status;
230 break;
231 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
232 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
233 val->intval = cpcap_charger_get_charge_voltage(ddata) *
234 1000;
235 else
236 val->intval = 0;
237 break;
238 case POWER_SUPPLY_PROP_CURRENT_NOW:
239 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
240 val->intval = cpcap_charger_get_charge_current(ddata) *
241 1000;
242 else
243 val->intval = 0;
244 break;
245 case POWER_SUPPLY_PROP_ONLINE:
246 val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING;
247 break;
248 default:
249 return -EINVAL;
250 }
251
252 return 0;
253}
254
255static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
256 bool enabled)
257{
258 if (!ddata->gpio[0])
259 return;
260
261 gpiod_set_value(ddata->gpio[0], enabled);
262}
263
264static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata,
265 bool enabled)
266{
267 if (!ddata->gpio[1])
268 return;
269
270 gpiod_set_value(ddata->gpio[1], enabled);
271}
272
273static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata,
274 int max_voltage, int charge_current,
275 int trickle_current)
276{
277 bool enable;
278 int error;
279
280 enable = (charge_current || trickle_current);
281 dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable);
282
283 if (!enable) {
284 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
285 0x3fff,
286 CPCAP_REG_CRM_FET_OVRD |
287 CPCAP_REG_CRM_FET_CTRL);
288 if (error) {
289 ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
290 goto out_err;
291 }
292
293 ddata->status = POWER_SUPPLY_STATUS_DISCHARGING;
294
295 return 0;
296 }
297
298 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff,
299 CPCAP_REG_CRM_CHRG_LED_EN |
300 trickle_current |
301 CPCAP_REG_CRM_FET_OVRD |
302 CPCAP_REG_CRM_FET_CTRL |
303 max_voltage |
304 charge_current);
305 if (error) {
306 ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
307 goto out_err;
308 }
309
310 ddata->status = POWER_SUPPLY_STATUS_CHARGING;
311
312 return 0;
313
314out_err:
315 dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
316
317 return error;
318}
319
320static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata)
321{
322 int error, value = 0;
323 struct iio_channel *channel =
324 ddata->channels[CPCAP_CHARGER_IIO_VBUS];
325
326 error = iio_read_channel_processed(channel, &value);
327 if (error >= 0)
328 return value > 3900 ? true : false;
329
330 dev_err(ddata->dev, "error reading VBUS: %i\n", error);
331
332 return false;
333}
334
335/* VBUS control functions for the USB PHY companion */
336
337static void cpcap_charger_vbus_work(struct work_struct *work)
338{
339 struct cpcap_charger_ddata *ddata;
340 bool vbus = false;
341 int error;
342
343 ddata = container_of(work, struct cpcap_charger_ddata,
344 vbus_work.work);
345
346 if (ddata->vbus_enabled) {
347 vbus = cpcap_charger_vbus_valid(ddata);
348 if (vbus) {
349 dev_info(ddata->dev, "VBUS already provided\n");
350
351 return;
352 }
353
354 cpcap_charger_set_cable_path(ddata, false);
355 cpcap_charger_set_inductive_path(ddata, false);
356
357 error = cpcap_charger_set_state(ddata, 0, 0, 0);
358 if (error)
359 goto out_err;
360
361 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
362 CPCAP_REG_CRM_RVRSMODE,
363 CPCAP_REG_CRM_RVRSMODE);
364 if (error)
365 goto out_err;
366 } else {
367 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
368 CPCAP_REG_CRM_RVRSMODE, 0);
369 if (error)
370 goto out_err;
371
372 cpcap_charger_set_cable_path(ddata, true);
373 cpcap_charger_set_inductive_path(ddata, true);
374 }
375
376 return;
377
378out_err:
379 dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__,
380 ddata->vbus_enabled ? "enable" : "disable", error);
381}
382
383static int cpcap_charger_set_vbus(struct phy_companion *comparator,
384 bool enabled)
385{
386 struct cpcap_charger_ddata *ddata =
387 container_of(comparator, struct cpcap_charger_ddata,
388 comparator);
389
390 ddata->vbus_enabled = enabled;
391 schedule_delayed_work(&ddata->vbus_work, 0);
392
393 return 0;
394}
395
396/* Charger interrupt handling functions */
397
398static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata,
399 struct cpcap_charger_ints_state *s)
400{
401 int val, error;
402
403 error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
404 if (error)
405 return error;
406
407 s->chrg_det = val & BIT(13);
408 s->rvrs_chrg = val & BIT(12);
409 s->vbusov = val & BIT(11);
410
411 error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
412 if (error)
413 return error;
414
415 s->chrg_se1b = val & BIT(13);
416 s->rvrs_mode = val & BIT(6);
417 s->chrgcurr1 = val & BIT(4);
418 s->vbusvld = val & BIT(3);
419
420 error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
421 if (error)
422 return error;
423
424 s->battdetb = val & BIT(6);
425
426 return 0;
427}
428
429static void cpcap_usb_detect(struct work_struct *work)
430{
431 struct cpcap_charger_ddata *ddata;
432 struct cpcap_charger_ints_state s;
433 int error;
434
435 ddata = container_of(work, struct cpcap_charger_ddata,
436 detect_work.work);
437
438 error = cpcap_charger_get_ints_state(ddata, &s);
439 if (error)
440 return;
441
442 if (cpcap_charger_vbus_valid(ddata) && s.chrgcurr1) {
443 int max_current;
444
445 if (cpcap_charger_battery_found(ddata))
446 max_current = CPCAP_REG_CRM_ICHRG_1A596;
447 else
448 max_current = CPCAP_REG_CRM_ICHRG_0A532;
449
450 error = cpcap_charger_set_state(ddata,
451 CPCAP_REG_CRM_VCHRG_4V35,
452 max_current, 0);
453 if (error)
454 goto out_err;
455 } else {
456 error = cpcap_charger_set_state(ddata, 0, 0, 0);
457 if (error)
458 goto out_err;
459 }
460
461 power_supply_changed(ddata->usb);
462 return;
463
464out_err:
465 dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
466}
467
468static irqreturn_t cpcap_charger_irq_thread(int irq, void *data)
469{
470 struct cpcap_charger_ddata *ddata = data;
471
472 if (!atomic_read(&ddata->active))
473 return IRQ_NONE;
474
475 schedule_delayed_work(&ddata->detect_work, 0);
476
477 return IRQ_HANDLED;
478}
479
480static int cpcap_usb_init_irq(struct platform_device *pdev,
481 struct cpcap_charger_ddata *ddata,
482 const char *name)
483{
484 struct cpcap_interrupt_desc *d;
485 int irq, error;
486
487 irq = platform_get_irq_byname(pdev, name);
488 if (irq < 0)
489 return -ENODEV;
490
491 error = devm_request_threaded_irq(ddata->dev, irq, NULL,
492 cpcap_charger_irq_thread,
493 IRQF_SHARED,
494 name, ddata);
495 if (error) {
496 dev_err(ddata->dev, "could not get irq %s: %i\n",
497 name, error);
498
499 return error;
500 }
501
502 d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
503 if (!d)
504 return -ENOMEM;
505
506 d->name = name;
507 d->irq = irq;
508 list_add(&d->node, &ddata->irq_list);
509
510 return 0;
511}
512
513static const char * const cpcap_charger_irqs[] = {
514 /* REG_INT_0 */
515 "chrg_det", "rvrs_chrg",
516
517 /* REG_INT1 */
518 "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr1", "vbusvld",
519
520 /* REG_INT_3 */
521 "battdetb",
522};
523
524static int cpcap_usb_init_interrupts(struct platform_device *pdev,
525 struct cpcap_charger_ddata *ddata)
526{
527 int i, error;
528
529 for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) {
530 error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]);
531 if (error)
532 return error;
533 }
534
535 return 0;
536}
537
538static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata)
539{
540 int i;
541
542 for (i = 0; i < 2; i++) {
543 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
544 i, GPIOD_OUT_HIGH);
545 if (IS_ERR(ddata->gpio[i])) {
546 dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
547 i, PTR_ERR(ddata->gpio[i]));
548 ddata->gpio[i] = NULL;
549 }
550 }
551}
552
553static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
554{
555 const char * const names[CPCAP_CHARGER_IIO_NR] = {
556 "battdetb", "battp", "vbus", "chg_isense", "batti",
557 };
558 int error, i;
559
560 for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) {
561 ddata->channels[i] = devm_iio_channel_get(ddata->dev,
562 names[i]);
563 if (IS_ERR(ddata->channels[i])) {
564 error = PTR_ERR(ddata->channels[i]);
565 goto out_err;
566 }
567
568 if (!ddata->channels[i]->indio_dev) {
569 error = -ENXIO;
570 goto out_err;
571 }
572 }
573
574 return 0;
575
576out_err:
577 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
578 error);
579
580 return error;
581}
582
583static const struct power_supply_desc cpcap_charger_usb_desc = {
584 .name = "usb",
585 .type = POWER_SUPPLY_TYPE_USB,
586 .properties = cpcap_charger_props,
587 .num_properties = ARRAY_SIZE(cpcap_charger_props),
588 .get_property = cpcap_charger_get_property,
589};
590
591#ifdef CONFIG_OF
592static const struct of_device_id cpcap_charger_id_table[] = {
593 {
594 .compatible = "motorola,mapphone-cpcap-charger",
595 },
596 {},
597};
598MODULE_DEVICE_TABLE(of, cpcap_charger_id_table);
599#endif
600
601static int cpcap_charger_probe(struct platform_device *pdev)
602{
603 struct cpcap_charger_ddata *ddata;
604 const struct of_device_id *of_id;
605 struct power_supply_config psy_cfg = {};
606 int error;
607
608 of_id = of_match_device(of_match_ptr(cpcap_charger_id_table),
609 &pdev->dev);
610 if (!of_id)
611 return -EINVAL;
612
613 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
614 if (!ddata)
615 return -ENOMEM;
616
617 ddata->dev = &pdev->dev;
618
619 ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
620 if (!ddata->reg)
621 return -ENODEV;
622
623 INIT_LIST_HEAD(&ddata->irq_list);
624 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
625 INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work);
626 platform_set_drvdata(pdev, ddata);
627
628 error = cpcap_charger_init_iio(ddata);
629 if (error)
630 return error;
631
632 atomic_set(&ddata->active, 1);
633
634 psy_cfg.of_node = pdev->dev.of_node;
635 psy_cfg.drv_data = ddata;
636
637 ddata->usb = devm_power_supply_register(ddata->dev,
638 &cpcap_charger_usb_desc,
639 &psy_cfg);
640 if (IS_ERR(ddata->usb)) {
641 error = PTR_ERR(ddata->usb);
642 dev_err(ddata->dev, "failed to register USB charger: %i\n",
643 error);
644
645 return error;
646 }
647
648 error = cpcap_usb_init_interrupts(pdev, ddata);
649 if (error)
650 return error;
651
652 ddata->comparator.set_vbus = cpcap_charger_set_vbus;
653 error = omap_usb2_set_comparator(&ddata->comparator);
654 if (error == -ENODEV) {
655 dev_info(ddata->dev, "charger needs phy, deferring probe\n");
656 return -EPROBE_DEFER;
657 }
658
659 cpcap_charger_init_optional_gpios(ddata);
660
661 schedule_delayed_work(&ddata->detect_work, 0);
662
663 return 0;
664}
665
666static int cpcap_charger_remove(struct platform_device *pdev)
667{
668 struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev);
669 int error;
670
671 atomic_set(&ddata->active, 0);
672 error = omap_usb2_set_comparator(NULL);
673 if (error)
674 dev_warn(ddata->dev, "could not clear USB comparator: %i\n",
675 error);
676
677 error = cpcap_charger_set_state(ddata, 0, 0, 0);
678 if (error)
679 dev_warn(ddata->dev, "could not clear charger: %i\n",
680 error);
681 cancel_delayed_work_sync(&ddata->vbus_work);
682 cancel_delayed_work_sync(&ddata->detect_work);
683
684 return 0;
685}
686
687static struct platform_driver cpcap_charger_driver = {
688 .probe = cpcap_charger_probe,
689 .driver = {
690 .name = "cpcap-charger",
691 .of_match_table = of_match_ptr(cpcap_charger_id_table),
692 },
693 .remove = cpcap_charger_remove,
694};
695module_platform_driver(cpcap_charger_driver);
696
697MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
698MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
699MODULE_LICENSE("GPL v2");
700MODULE_ALIAS("platform:cpcap-charger");