1 // SPDX-License-Identifier: GPL-2.0-only
3 * Battery charger driver for RT5033
5 * Copyright (C) 2014 Samsung Electronics, Co., Ltd.
6 * Author: Beomho Seo <beomho.seo@samsung.com>
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>
15 #include <linux/platform_device.h>
16 #include <linux/power_supply.h>
17 #include <linux/regmap.h>
18 #include <linux/mfd/rt5033-private.h>
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
;
28 struct rt5033_charger
{
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
;
43 static int rt5033_get_charger_state(struct rt5033_charger
*charger
)
45 struct regmap
*regmap
= charger
->regmap
;
46 unsigned int reg_data
;
50 return POWER_SUPPLY_STATUS_UNKNOWN
;
52 regmap_read(regmap
, RT5033_REG_CHG_STAT
, ®_data
);
54 switch (reg_data
& RT5033_CHG_STAT_MASK
) {
55 case RT5033_CHG_STAT_DISCHARGING
:
56 state
= POWER_SUPPLY_STATUS_DISCHARGING
;
58 case RT5033_CHG_STAT_CHARGING
:
59 state
= POWER_SUPPLY_STATUS_CHARGING
;
61 case RT5033_CHG_STAT_FULL
:
62 state
= POWER_SUPPLY_STATUS_FULL
;
64 case RT5033_CHG_STAT_NOT_CHARGING
:
65 state
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
68 state
= POWER_SUPPLY_STATUS_UNKNOWN
;
71 /* For OTG mode, RT5033 would still report "charging" */
73 state
= POWER_SUPPLY_STATUS_DISCHARGING
;
78 static int rt5033_get_charger_type(struct rt5033_charger
*charger
)
80 struct regmap
*regmap
= charger
->regmap
;
81 unsigned int reg_data
;
84 regmap_read(regmap
, RT5033_REG_CHG_STAT
, ®_data
);
86 switch (reg_data
& RT5033_CHG_STAT_TYPE_MASK
) {
87 case RT5033_CHG_STAT_TYPE_FAST
:
88 state
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
90 case RT5033_CHG_STAT_TYPE_PRE
:
91 state
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
94 state
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
100 static int rt5033_get_charger_current_limit(struct rt5033_charger
*charger
)
102 struct regmap
*regmap
= charger
->regmap
;
103 unsigned int state
, reg_data
, data
;
105 regmap_read(regmap
, RT5033_REG_CHG_CTRL5
, ®_data
);
107 state
= (reg_data
& RT5033_CHGCTRL5_ICHG_MASK
)
108 >> RT5033_CHGCTRL5_ICHG_SHIFT
;
110 data
= RT5033_CHARGER_FAST_CURRENT_MIN
+
111 RT5033_CHARGER_FAST_CURRENT_STEP_NUM
* state
;
116 static int rt5033_get_charger_const_voltage(struct rt5033_charger
*charger
)
118 struct regmap
*regmap
= charger
->regmap
;
119 unsigned int state
, reg_data
, data
;
121 regmap_read(regmap
, RT5033_REG_CHG_CTRL2
, ®_data
);
123 state
= (reg_data
& RT5033_CHGCTRL2_CV_MASK
)
124 >> RT5033_CHGCTRL2_CV_SHIFT
;
126 data
= RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN
+
127 RT5033_CHARGER_CONST_VOLTAGE_STEP_NUM
* state
;
132 static inline int rt5033_init_const_charge(struct rt5033_charger
*charger
)
134 struct rt5033_charger_data
*chg
= &charger
->chg
;
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");
147 if (chg
->const_uvolt
== RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN
)
149 else if (chg
->const_uvolt
== RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MAX
)
150 reg_data
= RT5033_CV_MAX_VOLTAGE
;
152 val
= chg
->const_uvolt
;
153 val
-= RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN
;
154 val
/= RT5033_CHARGER_CONST_VOLTAGE_STEP_NUM
;
158 ret
= regmap_update_bits(charger
->regmap
, RT5033_REG_CHG_CTRL2
,
159 RT5033_CHGCTRL2_CV_MASK
,
160 reg_data
<< RT5033_CHGCTRL2_CV_SHIFT
);
162 dev_err(charger
->dev
, "Failed regmap update\n");
166 /* Store that value for later usage */
167 charger
->cv_regval
= reg_data
;
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");
177 if (chg
->eoc_uamp
== RT5033_CHARGER_EOC_MIN
)
179 else if (chg
->eoc_uamp
== RT5033_CHARGER_EOC_MAX
)
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
;
196 ret
= regmap_update_bits(charger
->regmap
, RT5033_REG_CHG_CTRL4
,
197 RT5033_CHGCTRL4_EOC_MASK
, reg_data
);
199 dev_err(charger
->dev
, "Failed regmap update\n");
206 static inline int rt5033_init_fast_charge(struct rt5033_charger
*charger
)
208 struct rt5033_charger_data
*chg
= &charger
->chg
;
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
);
217 dev_err(charger
->dev
, "Failed regmap update\n");
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");
229 if (chg
->fast_uamp
== RT5033_CHARGER_FAST_CURRENT_MIN
)
231 else if (chg
->fast_uamp
== RT5033_CHARGER_FAST_CURRENT_MAX
)
232 reg_data
= RT5033_CHG_MAX_CURRENT
;
234 val
= chg
->fast_uamp
;
235 val
-= RT5033_CHARGER_FAST_CURRENT_MIN
;
236 val
/= RT5033_CHARGER_FAST_CURRENT_STEP_NUM
;
240 ret
= regmap_update_bits(charger
->regmap
, RT5033_REG_CHG_CTRL5
,
241 RT5033_CHGCTRL5_ICHG_MASK
,
242 reg_data
<< RT5033_CHGCTRL5_ICHG_SHIFT
);
244 dev_err(charger
->dev
, "Failed regmap update\n");
251 static inline int rt5033_init_pre_charge(struct rt5033_charger
*charger
)
253 struct rt5033_charger_data
*chg
= &charger
->chg
;
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");
266 if (chg
->pre_uvolt
== RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MIN
)
268 else if (chg
->pre_uvolt
== RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MAX
)
271 val
= chg
->pre_uvolt
;
272 val
-= RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MIN
;
273 val
/= RT5033_CHARGER_PRE_THRESHOLD_STEP_NUM
;
277 ret
= regmap_update_bits(charger
->regmap
, RT5033_REG_CHG_CTRL5
,
278 RT5033_CHGCTRL5_VPREC_MASK
, reg_data
);
280 dev_err(charger
->dev
, "Failed regmap update\n");
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");
292 if (chg
->pre_uamp
== RT5033_CHARGER_PRE_CURRENT_LIMIT_MIN
)
294 else if (chg
->pre_uamp
== RT5033_CHARGER_PRE_CURRENT_LIMIT_MAX
)
295 reg_data
= RT5033_CHG_MAX_PRE_CURRENT
;
298 val
-= RT5033_CHARGER_PRE_CURRENT_LIMIT_MIN
;
299 val
/= RT5033_CHARGER_PRE_CURRENT_STEP_NUM
;
303 ret
= regmap_update_bits(charger
->regmap
, RT5033_REG_CHG_CTRL4
,
304 RT5033_CHGCTRL4_IPREC_MASK
,
305 reg_data
<< RT5033_CHGCTRL4_IPREC_SHIFT
);
307 dev_err(charger
->dev
, "Failed regmap update\n");
314 static int rt5033_charger_reg_init(struct rt5033_charger
*charger
)
318 /* Enable charging termination */
319 ret
= regmap_update_bits(charger
->regmap
, RT5033_REG_CHG_CTRL1
,
320 RT5033_CHGCTRL1_TE_EN_MASK
, RT5033_TE_ENABLE
);
322 dev_err(charger
->dev
, "Failed to enable charging termination.\n");
327 * Disable minimum input voltage regulation (MIVR), this improves
328 * the charging performance.
330 ret
= regmap_update_bits(charger
->regmap
, RT5033_REG_CHG_CTRL4
,
331 RT5033_CHGCTRL4_MIVR_MASK
, RT5033_CHARGER_MIVR_DISABLE
);
333 dev_err(charger
->dev
, "Failed to disable MIVR.\n");
337 ret
= rt5033_init_pre_charge(charger
);
341 ret
= rt5033_init_fast_charge(charger
);
345 ret
= rt5033_init_const_charge(charger
);
352 static int rt5033_charger_set_otg(struct rt5033_charger
*charger
)
356 mutex_lock(&charger
->lock
);
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
);
363 dev_err(charger
->dev
, "Failed set OTG boost v_out\n");
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
);
372 dev_err(charger
->dev
, "Failed to update OTG mode.\n");
377 /* In case someone switched from charging to OTG directly */
379 charger
->online
= false;
384 mutex_unlock(&charger
->lock
);
389 static int rt5033_charger_unset_otg(struct rt5033_charger
*charger
)
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
);
400 dev_err(charger
->dev
, "Failed to restore constant voltage\n");
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
);
408 dev_err(charger
->dev
, "Failed to update charger mode.\n");
412 charger
->otg
= false;
417 static int rt5033_charger_set_charging(struct rt5033_charger
*charger
)
421 mutex_lock(&charger
->lock
);
423 /* In case someone switched from OTG to charging directly */
425 ret
= rt5033_charger_unset_otg(charger
);
427 mutex_unlock(&charger
->lock
);
432 charger
->online
= true;
434 mutex_unlock(&charger
->lock
);
439 static int rt5033_charger_set_mivr(struct rt5033_charger
*charger
)
443 mutex_lock(&charger
->lock
);
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.
452 ret
= regmap_update_bits(charger
->regmap
, RT5033_REG_CHG_CTRL4
,
453 RT5033_CHGCTRL4_MIVR_MASK
, RT5033_CHARGER_MIVR_4600MV
);
455 dev_err(charger
->dev
, "Failed to set MIVR level.\n");
456 mutex_unlock(&charger
->lock
);
460 charger
->mivr_enabled
= true;
462 mutex_unlock(&charger
->lock
);
464 /* Beyond this, do the same steps like setting charging */
465 rt5033_charger_set_charging(charger
);
470 static int rt5033_charger_set_disconnect(struct rt5033_charger
*charger
)
474 mutex_lock(&charger
->lock
);
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
);
483 dev_err(charger
->dev
, "Failed to disable MIVR.\n");
488 charger
->mivr_enabled
= false;
492 ret
= rt5033_charger_unset_otg(charger
);
500 charger
->online
= false;
503 mutex_unlock(&charger
->lock
);
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
,
518 static int rt5033_charger_get_property(struct power_supply
*psy
,
519 enum power_supply_property psp
,
520 union power_supply_propval
*val
)
522 struct rt5033_charger
*charger
= power_supply_get_drvdata(psy
);
525 case POWER_SUPPLY_PROP_STATUS
:
526 val
->intval
= rt5033_get_charger_state(charger
);
528 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
529 val
->intval
= rt5033_get_charger_type(charger
);
531 case POWER_SUPPLY_PROP_CURRENT_MAX
:
532 val
->intval
= rt5033_get_charger_current_limit(charger
);
534 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
535 val
->intval
= rt5033_get_charger_const_voltage(charger
);
537 case POWER_SUPPLY_PROP_MODEL_NAME
:
538 val
->strval
= RT5033_CHARGER_MODEL
;
540 case POWER_SUPPLY_PROP_MANUFACTURER
:
541 val
->strval
= RT5033_MANUFACTURER
;
543 case POWER_SUPPLY_PROP_ONLINE
:
544 val
->intval
= charger
->online
;
553 static int rt5033_charger_dt_init(struct rt5033_charger
*charger
)
555 struct rt5033_charger_data
*chg
= &charger
->chg
;
556 struct power_supply_battery_info
*info
;
559 ret
= power_supply_get_battery_info(charger
->psy
, &info
);
561 return dev_err_probe(charger
->dev
, -EINVAL
,
562 "missing battery info\n");
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
;
574 static void rt5033_charger_extcon_work(struct work_struct
*work
)
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
;
582 for (connector
= EXTCON_USB_HOST
; connector
<= EXTCON_CHG_USB_PD
;
584 state
= extcon_get_state(edev
, connector
);
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
598 case EXTCON_CHG_USB_SDP
:
599 ret
= rt5033_charger_set_mivr(charger
);
601 dev_err(charger
->dev
, "failed to set USB mode\n");
604 dev_info(charger
->dev
, "USB mode. connector type: %d\n",
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
:
613 case EXTCON_CHG_USB_PD
:
614 ret
= rt5033_charger_set_charging(charger
);
616 dev_err(charger
->dev
, "failed to set charging\n");
619 dev_info(charger
->dev
, "charging. connector type: %d\n",
622 case EXTCON_USB_HOST
:
623 ret
= rt5033_charger_set_otg(charger
);
625 dev_err(charger
->dev
, "failed to set OTG\n");
628 dev_info(charger
->dev
, "OTG enabled\n");
631 ret
= rt5033_charger_set_disconnect(charger
);
633 dev_err(charger
->dev
, "failed to set disconnect\n");
636 dev_info(charger
->dev
, "disconnected\n");
640 power_supply_changed(charger
->psy
);
643 static int rt5033_charger_extcon_notifier(struct notifier_block
*nb
,
644 unsigned long event
, void *param
)
646 struct rt5033_charger
*charger
=
647 container_of(nb
, struct rt5033_charger
, extcon_nb
);
649 schedule_work(&charger
->extcon_work
);
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
,
662 static int rt5033_charger_probe(struct platform_device
*pdev
)
664 struct rt5033_charger
*charger
;
665 struct power_supply_config psy_cfg
= {};
666 struct device_node
*np_conn
, *np_edev
;
669 charger
= devm_kzalloc(&pdev
->dev
, sizeof(*charger
), GFP_KERNEL
);
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
);
678 psy_cfg
.of_node
= pdev
->dev
.of_node
;
679 psy_cfg
.drv_data
= charger
;
681 charger
->psy
= devm_power_supply_register(charger
->dev
,
682 &rt5033_charger_desc
,
684 if (IS_ERR(charger
->psy
))
685 return dev_err_probe(charger
->dev
, PTR_ERR(charger
->psy
),
686 "Failed to register power supply\n");
688 ret
= rt5033_charger_dt_init(charger
);
692 ret
= rt5033_charger_reg_init(charger
);
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.
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");
708 ret
= devm_work_autocancel(charger
->dev
, &charger
->extcon_work
,
709 rt5033_charger_extcon_work
);
711 dev_err(charger
->dev
, "failed to initialize extcon work\n");
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
);
719 dev_err(charger
->dev
, "failed to register extcon notifier\n");
726 static const struct platform_device_id rt5033_charger_id
[] = {
727 { "rt5033-charger", },
730 MODULE_DEVICE_TABLE(platform
, rt5033_charger_id
);
732 static const struct of_device_id rt5033_charger_of_match
[] = {
733 { .compatible
= "richtek,rt5033-charger", },
736 MODULE_DEVICE_TABLE(of
, rt5033_charger_of_match
);
738 static struct platform_driver rt5033_charger_driver
= {
740 .name
= "rt5033-charger",
741 .of_match_table
= rt5033_charger_of_match
,
743 .probe
= rt5033_charger_probe
,
744 .id_table
= rt5033_charger_id
,
746 module_platform_driver(rt5033_charger_driver
);
748 MODULE_DESCRIPTION("Richtek RT5033 charger driver");
749 MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
750 MODULE_LICENSE("GPL v2");