1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Battery charger driver for RT5033
4 *
5 * Copyright (C) 2014 Samsung Electronics, Co., Ltd.
6 * Author: Beomho Seo <beomho.seo@samsung.com>
7 */
8
9 #include <linux/devm-helpers.h>
10 #include <linux/extcon.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/power_supply.h>
17 #include <linux/regmap.h>
18 #include <linux/mfd/rt5033-private.h>
19
20 struct rt5033_charger_data {
21 unsigned int pre_uamp;
22 unsigned int pre_uvolt;
23 unsigned int const_uvolt;
24 unsigned int eoc_uamp;
25 unsigned int fast_uamp;
26 };
27
28 struct rt5033_charger {
29 struct device *dev;
30 struct regmap *regmap;
31 struct power_supply *psy;
32 struct rt5033_charger_data chg;
33 struct extcon_dev *edev;
34 struct notifier_block extcon_nb;
35 struct work_struct extcon_work;
36 struct mutex lock;
37 bool online;
38 bool otg;
39 bool mivr_enabled;
40 u8 cv_regval;
41 };
42
rt5033_get_charger_state(struct rt5033_charger * charger)43 static int rt5033_get_charger_state(struct rt5033_charger *charger)
44 {
45 struct regmap *regmap = charger->regmap;
46 unsigned int reg_data;
47 int state;
48
49 if (!regmap)
50 return POWER_SUPPLY_STATUS_UNKNOWN;
51
52 regmap_read(regmap, RT5033_REG_CHG_STAT, ®_data);
53
54 switch (reg_data & RT5033_CHG_STAT_MASK) {
55 case RT5033_CHG_STAT_DISCHARGING:
56 state = POWER_SUPPLY_STATUS_DISCHARGING;
57 break;
58 case RT5033_CHG_STAT_CHARGING:
59 state = POWER_SUPPLY_STATUS_CHARGING;
60 break;
61 case RT5033_CHG_STAT_FULL:
62 state = POWER_SUPPLY_STATUS_FULL;
63 break;
64 case RT5033_CHG_STAT_NOT_CHARGING:
65 state = POWER_SUPPLY_STATUS_NOT_CHARGING;
66 break;
67 default:
68 state = POWER_SUPPLY_STATUS_UNKNOWN;
69 }
70
71 /* For OTG mode, RT5033 would still report "charging" */
72 if (charger->otg)
73 state = POWER_SUPPLY_STATUS_DISCHARGING;
74
75 return state;
76 }
77
rt5033_get_charger_type(struct rt5033_charger * charger)78 static int rt5033_get_charger_type(struct rt5033_charger *charger)
79 {
80 struct regmap *regmap = charger->regmap;
81 unsigned int reg_data;
82 int state;
83
84 regmap_read(regmap, RT5033_REG_CHG_STAT, ®_data);
85
86 switch (reg_data & RT5033_CHG_STAT_TYPE_MASK) {
87 case RT5033_CHG_STAT_TYPE_FAST:
88 state = POWER_SUPPLY_CHARGE_TYPE_FAST;
89 break;
90 case RT5033_CHG_STAT_TYPE_PRE:
91 state = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
92 break;
93 default:
94 state = POWER_SUPPLY_CHARGE_TYPE_NONE;
95 }
96
97 return state;
98 }
99
rt5033_get_charger_current_limit(struct rt5033_charger * charger)100 static int rt5033_get_charger_current_limit(struct rt5033_charger *charger)
101 {
102 struct regmap *regmap = charger->regmap;
103 unsigned int state, reg_data, data;
104
105 regmap_read(regmap, RT5033_REG_CHG_CTRL5, ®_data);
106
107 state = (reg_data & RT5033_CHGCTRL5_ICHG_MASK)
108 >> RT5033_CHGCTRL5_ICHG_SHIFT;
109
110 data = RT5033_CHARGER_FAST_CURRENT_MIN +
111 RT5033_CHARGER_FAST_CURRENT_STEP_NUM * state;
112
113 return data;
114 }
115
rt5033_get_charger_const_voltage(struct rt5033_charger * charger)116 static int rt5033_get_charger_const_voltage(struct rt5033_charger *charger)
117 {
118 struct regmap *regmap = charger->regmap;
119 unsigned int state, reg_data, data;
120
121 regmap_read(regmap, RT5033_REG_CHG_CTRL2, ®_data);
122
123 state = (reg_data & RT5033_CHGCTRL2_CV_MASK)
124 >> RT5033_CHGCTRL2_CV_SHIFT;
125
126 data = RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN +
127 RT5033_CHARGER_CONST_VOLTAGE_STEP_NUM * state;
128
129 return data;
130 }
131
rt5033_init_const_charge(struct rt5033_charger * charger)132 static inline int rt5033_init_const_charge(struct rt5033_charger *charger)
133 {
134 struct rt5033_charger_data *chg = &charger->chg;
135 int ret;
136 unsigned int val;
137 u8 reg_data;
138
139 /* Set constant voltage mode */
140 if (chg->const_uvolt < RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN ||
141 chg->const_uvolt > RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MAX) {
142 dev_err(charger->dev,
143 "Value 'constant-charge-voltage-max-microvolt' out of range\n");
144 return -EINVAL;
145 }
146
147 if (chg->const_uvolt == RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN)
148 reg_data = 0x00;
149 else if (chg->const_uvolt == RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MAX)
150 reg_data = RT5033_CV_MAX_VOLTAGE;
151 else {
152 val = chg->const_uvolt;
153 val -= RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN;
154 val /= RT5033_CHARGER_CONST_VOLTAGE_STEP_NUM;
155 reg_data = val;
156 }
157
158 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL2,
159 RT5033_CHGCTRL2_CV_MASK,
160 reg_data << RT5033_CHGCTRL2_CV_SHIFT);
161 if (ret) {
162 dev_err(charger->dev, "Failed regmap update\n");
163 return -EINVAL;
164 }
165
166 /* Store that value for later usage */
167 charger->cv_regval = reg_data;
168
169 /* Set end of charge current */
170 if (chg->eoc_uamp < RT5033_CHARGER_EOC_MIN ||
171 chg->eoc_uamp > RT5033_CHARGER_EOC_MAX) {
172 dev_err(charger->dev,
173 "Value 'charge-term-current-microamp' out of range\n");
174 return -EINVAL;
175 }
176
177 if (chg->eoc_uamp == RT5033_CHARGER_EOC_MIN)
178 reg_data = 0x01;
179 else if (chg->eoc_uamp == RT5033_CHARGER_EOC_MAX)
180 reg_data = 0x07;
181 else {
182 val = chg->eoc_uamp;
183 if (val < RT5033_CHARGER_EOC_REF) {
184 val -= RT5033_CHARGER_EOC_MIN;
185 val /= RT5033_CHARGER_EOC_STEP_NUM1;
186 reg_data = 0x01 + val;
187 } else if (val > RT5033_CHARGER_EOC_REF) {
188 val -= RT5033_CHARGER_EOC_REF;
189 val /= RT5033_CHARGER_EOC_STEP_NUM2;
190 reg_data = 0x04 + val;
191 } else {
192 reg_data = 0x04;
193 }
194 }
195
196 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL4,
197 RT5033_CHGCTRL4_EOC_MASK, reg_data);
198 if (ret) {
199 dev_err(charger->dev, "Failed regmap update\n");
200 return -EINVAL;
201 }
202
203 return 0;
204 }
205
rt5033_init_fast_charge(struct rt5033_charger * charger)206 static inline int rt5033_init_fast_charge(struct rt5033_charger *charger)
207 {
208 struct rt5033_charger_data *chg = &charger->chg;
209 int ret;
210 unsigned int val;
211 u8 reg_data;
212
213 /* Set limit input current */
214 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL1,
215 RT5033_CHGCTRL1_IAICR_MASK, RT5033_AICR_2000_MODE);
216 if (ret) {
217 dev_err(charger->dev, "Failed regmap update\n");
218 return -EINVAL;
219 }
220
221 /* Set fast-charge mode charging current */
222 if (chg->fast_uamp < RT5033_CHARGER_FAST_CURRENT_MIN ||
223 chg->fast_uamp > RT5033_CHARGER_FAST_CURRENT_MAX) {
224 dev_err(charger->dev,
225 "Value 'constant-charge-current-max-microamp' out of range\n");
226 return -EINVAL;
227 }
228
229 if (chg->fast_uamp == RT5033_CHARGER_FAST_CURRENT_MIN)
230 reg_data = 0x00;
231 else if (chg->fast_uamp == RT5033_CHARGER_FAST_CURRENT_MAX)
232 reg_data = RT5033_CHG_MAX_CURRENT;
233 else {
234 val = chg->fast_uamp;
235 val -= RT5033_CHARGER_FAST_CURRENT_MIN;
236 val /= RT5033_CHARGER_FAST_CURRENT_STEP_NUM;
237 reg_data = val;
238 }
239
240 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL5,
241 RT5033_CHGCTRL5_ICHG_MASK,
242 reg_data << RT5033_CHGCTRL5_ICHG_SHIFT);
243 if (ret) {
244 dev_err(charger->dev, "Failed regmap update\n");
245 return -EINVAL;
246 }
247
248 return 0;
249 }
250
rt5033_init_pre_charge(struct rt5033_charger * charger)251 static inline int rt5033_init_pre_charge(struct rt5033_charger *charger)
252 {
253 struct rt5033_charger_data *chg = &charger->chg;
254 int ret;
255 unsigned int val;
256 u8 reg_data;
257
258 /* Set pre-charge threshold voltage */
259 if (chg->pre_uvolt < RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MIN ||
260 chg->pre_uvolt > RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MAX) {
261 dev_err(charger->dev,
262 "Value 'precharge-upper-limit-microvolt' out of range\n");
263 return -EINVAL;
264 }
265
266 if (chg->pre_uvolt == RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MIN)
267 reg_data = 0x00;
268 else if (chg->pre_uvolt == RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MAX)
269 reg_data = 0x0f;
270 else {
271 val = chg->pre_uvolt;
272 val -= RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MIN;
273 val /= RT5033_CHARGER_PRE_THRESHOLD_STEP_NUM;
274 reg_data = val;
275 }
276
277 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL5,
278 RT5033_CHGCTRL5_VPREC_MASK, reg_data);
279 if (ret) {
280 dev_err(charger->dev, "Failed regmap update\n");
281 return -EINVAL;
282 }
283
284 /* Set pre-charge mode charging current */
285 if (chg->pre_uamp < RT5033_CHARGER_PRE_CURRENT_LIMIT_MIN ||
286 chg->pre_uamp > RT5033_CHARGER_PRE_CURRENT_LIMIT_MAX) {
287 dev_err(charger->dev,
288 "Value 'precharge-current-microamp' out of range\n");
289 return -EINVAL;
290 }
291
292 if (chg->pre_uamp == RT5033_CHARGER_PRE_CURRENT_LIMIT_MIN)
293 reg_data = 0x00;
294 else if (chg->pre_uamp == RT5033_CHARGER_PRE_CURRENT_LIMIT_MAX)
295 reg_data = RT5033_CHG_MAX_PRE_CURRENT;
296 else {
297 val = chg->pre_uamp;
298 val -= RT5033_CHARGER_PRE_CURRENT_LIMIT_MIN;
299 val /= RT5033_CHARGER_PRE_CURRENT_STEP_NUM;
300 reg_data = val;
301 }
302
303 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL4,
304 RT5033_CHGCTRL4_IPREC_MASK,
305 reg_data << RT5033_CHGCTRL4_IPREC_SHIFT);
306 if (ret) {
307 dev_err(charger->dev, "Failed regmap update\n");
308 return -EINVAL;
309 }
310
311 return 0;
312 }
313
rt5033_charger_reg_init(struct rt5033_charger * charger)314 static int rt5033_charger_reg_init(struct rt5033_charger *charger)
315 {
316 int ret = 0;
317
318 /* Enable charging termination */
319 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL1,
320 RT5033_CHGCTRL1_TE_EN_MASK, RT5033_TE_ENABLE);
321 if (ret) {
322 dev_err(charger->dev, "Failed to enable charging termination.\n");
323 return -EINVAL;
324 }
325
326 /*
327 * Disable minimum input voltage regulation (MIVR), this improves
328 * the charging performance.
329 */
330 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL4,
331 RT5033_CHGCTRL4_MIVR_MASK, RT5033_CHARGER_MIVR_DISABLE);
332 if (ret) {
333 dev_err(charger->dev, "Failed to disable MIVR.\n");
334 return -EINVAL;
335 }
336
337 ret = rt5033_init_pre_charge(charger);
338 if (ret)
339 return ret;
340
341 ret = rt5033_init_fast_charge(charger);
342 if (ret)
343 return ret;
344
345 ret = rt5033_init_const_charge(charger);
346 if (ret)
347 return ret;
348
349 return 0;
350 }
351
rt5033_charger_set_otg(struct rt5033_charger * charger)352 static int rt5033_charger_set_otg(struct rt5033_charger *charger)
353 {
354 int ret;
355
356 mutex_lock(&charger->lock);
357
358 /* Set OTG boost v_out to 5 volts */
359 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL2,
360 RT5033_CHGCTRL2_CV_MASK,
361 0x37 << RT5033_CHGCTRL2_CV_SHIFT);
362 if (ret) {
363 dev_err(charger->dev, "Failed set OTG boost v_out\n");
364 ret = -EINVAL;
365 goto out_unlock;
366 }
367
368 /* Set operation mode to OTG */
369 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL1,
370 RT5033_CHGCTRL1_MODE_MASK, RT5033_BOOST_MODE);
371 if (ret) {
372 dev_err(charger->dev, "Failed to update OTG mode.\n");
373 ret = -EINVAL;
374 goto out_unlock;
375 }
376
377 /* In case someone switched from charging to OTG directly */
378 if (charger->online)
379 charger->online = false;
380
381 charger->otg = true;
382
383 out_unlock:
384 mutex_unlock(&charger->lock);
385
386 return ret;
387 }
388
rt5033_charger_unset_otg(struct rt5033_charger * charger)389 static int rt5033_charger_unset_otg(struct rt5033_charger *charger)
390 {
391 int ret;
392 u8 data;
393
394 /* Restore constant voltage for charging */
395 data = charger->cv_regval;
396 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL2,
397 RT5033_CHGCTRL2_CV_MASK,
398 data << RT5033_CHGCTRL2_CV_SHIFT);
399 if (ret) {
400 dev_err(charger->dev, "Failed to restore constant voltage\n");
401 return -EINVAL;
402 }
403
404 /* Set operation mode to charging */
405 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL1,
406 RT5033_CHGCTRL1_MODE_MASK, RT5033_CHARGER_MODE);
407 if (ret) {
408 dev_err(charger->dev, "Failed to update charger mode.\n");
409 return -EINVAL;
410 }
411
412 charger->otg = false;
413
414 return 0;
415 }
416
rt5033_charger_set_charging(struct rt5033_charger * charger)417 static int rt5033_charger_set_charging(struct rt5033_charger *charger)
418 {
419 int ret;
420
421 mutex_lock(&charger->lock);
422
423 /* In case someone switched from OTG to charging directly */
424 if (charger->otg) {
425 ret = rt5033_charger_unset_otg(charger);
426 if (ret) {
427 mutex_unlock(&charger->lock);
428 return -EINVAL;
429 }
430 }
431
432 charger->online = true;
433
434 mutex_unlock(&charger->lock);
435
436 return 0;
437 }
438
rt5033_charger_set_mivr(struct rt5033_charger * charger)439 static int rt5033_charger_set_mivr(struct rt5033_charger *charger)
440 {
441 int ret;
442
443 mutex_lock(&charger->lock);
444
445 /*
446 * When connected via USB connector type SDP (Standard Downstream Port),
447 * the minimum input voltage regulation (MIVR) should be enabled. It
448 * prevents an input voltage drop due to insufficient current provided
449 * by the adapter or USB input. As a downside, it may reduces the
450 * charging current and thus slows the charging.
451 */
452 ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL4,
453 RT5033_CHGCTRL4_MIVR_MASK, RT5033_CHARGER_MIVR_4600MV);
454 if (ret) {
455 dev_err(charger->dev, "Failed to set MIVR level.\n");
456 mutex_unlock(&charger->lock);
457 return -EINVAL;
458 }
459
460 charger->mivr_enabled = true;
461
462 mutex_unlock(&charger->lock);
463
464 /* Beyond this, do the same steps like setting charging */
465 rt5033_charger_set_charging(charger);
466
467 return 0;
468 }
469
rt5033_charger_set_disconnect(struct rt5033_charger * charger)470 static int rt5033_charger_set_disconnect(struct rt5033_charger *charger)
471 {
472 int ret = 0;
473
474 mutex_lock(&charger->lock);
475
476 /* Disable MIVR if enabled */
477 if (charger->mivr_enabled) {
478 ret = regmap_update_bits(charger->regmap,
479 RT5033_REG_CHG_CTRL4,
480 RT5033_CHGCTRL4_MIVR_MASK,
481 RT5033_CHARGER_MIVR_DISABLE);
482 if (ret) {
483 dev_err(charger->dev, "Failed to disable MIVR.\n");
484 ret = -EINVAL;
485 goto out_unlock;
486 }
487
488 charger->mivr_enabled = false;
489 }
490
491 if (charger->otg) {
492 ret = rt5033_charger_unset_otg(charger);
493 if (ret) {
494 ret = -EINVAL;
495 goto out_unlock;
496 }
497 }
498
499 if (charger->online)
500 charger->online = false;
501
502 out_unlock:
503 mutex_unlock(&charger->lock);
504
505 return ret;
506 }
507
508 static enum power_supply_property rt5033_charger_props[] = {
509 POWER_SUPPLY_PROP_STATUS,
510 POWER_SUPPLY_PROP_CHARGE_TYPE,
511 POWER_SUPPLY_PROP_CURRENT_MAX,
512 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
513 POWER_SUPPLY_PROP_MODEL_NAME,
514 POWER_SUPPLY_PROP_MANUFACTURER,
515 POWER_SUPPLY_PROP_ONLINE,
516 };
517
rt5033_charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)518 static int rt5033_charger_get_property(struct power_supply *psy,
519 enum power_supply_property psp,
520 union power_supply_propval *val)
521 {
522 struct rt5033_charger *charger = power_supply_get_drvdata(psy);
523
524 switch (psp) {
525 case POWER_SUPPLY_PROP_STATUS:
526 val->intval = rt5033_get_charger_state(charger);
527 break;
528 case POWER_SUPPLY_PROP_CHARGE_TYPE:
529 val->intval = rt5033_get_charger_type(charger);
530 break;
531 case POWER_SUPPLY_PROP_CURRENT_MAX:
532 val->intval = rt5033_get_charger_current_limit(charger);
533 break;
534 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
535 val->intval = rt5033_get_charger_const_voltage(charger);
536 break;
537 case POWER_SUPPLY_PROP_MODEL_NAME:
538 val->strval = RT5033_CHARGER_MODEL;
539 break;
540 case POWER_SUPPLY_PROP_MANUFACTURER:
541 val->strval = RT5033_MANUFACTURER;
542 break;
543 case POWER_SUPPLY_PROP_ONLINE:
544 val->intval = charger->online;
545 break;
546 default:
547 return -EINVAL;
548 }
549
550 return 0;
551 }
552
rt5033_charger_dt_init(struct rt5033_charger * charger)553 static int rt5033_charger_dt_init(struct rt5033_charger *charger)
554 {
555 struct rt5033_charger_data *chg = &charger->chg;
556 struct power_supply_battery_info *info;
557 int ret;
558
559 ret = power_supply_get_battery_info(charger->psy, &info);
560 if (ret)
561 return dev_err_probe(charger->dev, -EINVAL,
562 "missing battery info\n");
563
564 /* Assign data. Validity will be checked in the init functions. */
565 chg->pre_uamp = info->precharge_current_ua;
566 chg->fast_uamp = info->constant_charge_current_max_ua;
567 chg->eoc_uamp = info->charge_term_current_ua;
568 chg->pre_uvolt = info->precharge_voltage_max_uv;
569 chg->const_uvolt = info->constant_charge_voltage_max_uv;
570
571 return 0;
572 }
573
rt5033_charger_extcon_work(struct work_struct * work)574 static void rt5033_charger_extcon_work(struct work_struct *work)
575 {
576 struct rt5033_charger *charger =
577 container_of(work, struct rt5033_charger, extcon_work);
578 struct extcon_dev *edev = charger->edev;
579 int connector, state;
580 int ret;
581
582 for (connector = EXTCON_USB_HOST; connector <= EXTCON_CHG_USB_PD;
583 connector++) {
584 state = extcon_get_state(edev, connector);
585 if (state == 1)
586 break;
587 }
588
589 /*
590 * Adding a delay between extcon notification and extcon action. This
591 * makes extcon action execution more reliable. Without the delay the
592 * execution sometimes fails, possibly because the chip is busy or not
593 * ready.
594 */
595 msleep(100);
596
597 switch (connector) {
598 case EXTCON_CHG_USB_SDP:
599 ret = rt5033_charger_set_mivr(charger);
600 if (ret) {
601 dev_err(charger->dev, "failed to set USB mode\n");
602 break;
603 }
604 dev_info(charger->dev, "USB mode. connector type: %d\n",
605 connector);
606 break;
607 case EXTCON_CHG_USB_DCP:
608 case EXTCON_CHG_USB_CDP:
609 case EXTCON_CHG_USB_ACA:
610 case EXTCON_CHG_USB_FAST:
611 case EXTCON_CHG_USB_SLOW:
612 case EXTCON_CHG_WPT:
613 case EXTCON_CHG_USB_PD:
614 ret = rt5033_charger_set_charging(charger);
615 if (ret) {
616 dev_err(charger->dev, "failed to set charging\n");
617 break;
618 }
619 dev_info(charger->dev, "charging. connector type: %d\n",
620 connector);
621 break;
622 case EXTCON_USB_HOST:
623 ret = rt5033_charger_set_otg(charger);
624 if (ret) {
625 dev_err(charger->dev, "failed to set OTG\n");
626 break;
627 }
628 dev_info(charger->dev, "OTG enabled\n");
629 break;
630 default:
631 ret = rt5033_charger_set_disconnect(charger);
632 if (ret) {
633 dev_err(charger->dev, "failed to set disconnect\n");
634 break;
635 }
636 dev_info(charger->dev, "disconnected\n");
637 break;
638 }
639
640 power_supply_changed(charger->psy);
641 }
642
rt5033_charger_extcon_notifier(struct notifier_block * nb,unsigned long event,void * param)643 static int rt5033_charger_extcon_notifier(struct notifier_block *nb,
644 unsigned long event, void *param)
645 {
646 struct rt5033_charger *charger =
647 container_of(nb, struct rt5033_charger, extcon_nb);
648
649 schedule_work(&charger->extcon_work);
650
651 return NOTIFY_OK;
652 }
653
654 static const struct power_supply_desc rt5033_charger_desc = {
655 .name = "rt5033-charger",
656 .type = POWER_SUPPLY_TYPE_USB,
657 .properties = rt5033_charger_props,
658 .num_properties = ARRAY_SIZE(rt5033_charger_props),
659 .get_property = rt5033_charger_get_property,
660 };
661
rt5033_charger_probe(struct platform_device * pdev)662 static int rt5033_charger_probe(struct platform_device *pdev)
663 {
664 struct rt5033_charger *charger;
665 struct power_supply_config psy_cfg = {};
666 struct device_node *np_conn, *np_edev;
667 int ret;
668
669 charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
670 if (!charger)
671 return -ENOMEM;
672
673 platform_set_drvdata(pdev, charger);
674 charger->dev = &pdev->dev;
675 charger->regmap = dev_get_regmap(pdev->dev.parent, NULL);
676 mutex_init(&charger->lock);
677
678 psy_cfg.of_node = pdev->dev.of_node;
679 psy_cfg.drv_data = charger;
680
681 charger->psy = devm_power_supply_register(charger->dev,
682 &rt5033_charger_desc,
683 &psy_cfg);
684 if (IS_ERR(charger->psy))
685 return dev_err_probe(charger->dev, PTR_ERR(charger->psy),
686 "Failed to register power supply\n");
687
688 ret = rt5033_charger_dt_init(charger);
689 if (ret)
690 return ret;
691
692 ret = rt5033_charger_reg_init(charger);
693 if (ret)
694 return ret;
695
696 /*
697 * Extcon support is not vital for the charger to work. If no extcon
698 * is available, just emit a warning and leave the probe function.
699 */
700 np_conn = of_parse_phandle(pdev->dev.of_node, "richtek,usb-connector", 0);
701 np_edev = of_get_parent(np_conn);
702 charger->edev = extcon_find_edev_by_node(np_edev);
703 if (IS_ERR(charger->edev)) {
704 dev_warn(charger->dev, "no extcon device found in device-tree\n");
705 goto out;
706 }
707
708 ret = devm_work_autocancel(charger->dev, &charger->extcon_work,
709 rt5033_charger_extcon_work);
710 if (ret) {
711 dev_err(charger->dev, "failed to initialize extcon work\n");
712 return ret;
713 }
714
715 charger->extcon_nb.notifier_call = rt5033_charger_extcon_notifier;
716 ret = devm_extcon_register_notifier_all(charger->dev, charger->edev,
717 &charger->extcon_nb);
718 if (ret) {
719 dev_err(charger->dev, "failed to register extcon notifier\n");
720 return ret;
721 }
722 out:
723 return 0;
724 }
725
726 static const struct platform_device_id rt5033_charger_id[] = {
727 { "rt5033-charger", },
728 { }
729 };
730 MODULE_DEVICE_TABLE(platform, rt5033_charger_id);
731
732 static const struct of_device_id rt5033_charger_of_match[] = {
733 { .compatible = "richtek,rt5033-charger", },
734 { }
735 };
736 MODULE_DEVICE_TABLE(of, rt5033_charger_of_match);
737
738 static struct platform_driver rt5033_charger_driver = {
739 .driver = {
740 .name = "rt5033-charger",
741 .of_match_table = rt5033_charger_of_match,
742 },
743 .probe = rt5033_charger_probe,
744 .id_table = rt5033_charger_id,
745 };
746 module_platform_driver(rt5033_charger_driver);
747
748 MODULE_DESCRIPTION("Richtek RT5033 charger driver");
749 MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
750 MODULE_LICENSE("GPL v2");
751