1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Spreadtrum Communications Inc.
4 #include <linux/module.h>
5 #include <linux/platform_device.h>
6 #include <linux/power_supply.h>
7 #include <linux/usb/phy.h>
8 #include <linux/regmap.h>
9 #include <linux/notifier.h>
12 /* PMIC global registers definition */
13 #define SC2731_CHARGE_STATUS 0xedc
14 #define SC2731_CHARGE_FULL BIT(4)
15 #define SC2731_MODULE_EN1 0xc0c
16 #define SC2731_CHARGE_EN BIT(5)
18 /* SC2731 switch charger registers definition */
19 #define SC2731_CHG_CFG0 0x0
20 #define SC2731_CHG_CFG1 0x4
21 #define SC2731_CHG_CFG2 0x8
22 #define SC2731_CHG_CFG3 0xc
23 #define SC2731_CHG_CFG4 0x10
24 #define SC2731_CHG_CFG5 0x28
26 /* SC2731_CHG_CFG0 register definition */
27 #define SC2731_PRECHG_RNG_SHIFT 11
28 #define SC2731_PRECHG_RNG_MASK GENMASK(12, 11)
30 #define SC2731_TERMINATION_VOL_MASK GENMASK(2, 1)
31 #define SC2731_TERMINATION_VOL_SHIFT 1
32 #define SC2731_TERMINATION_VOL_CAL_MASK GENMASK(8, 3)
33 #define SC2731_TERMINATION_VOL_CAL_SHIFT 3
34 #define SC2731_TERMINATION_CUR_MASK GENMASK(2, 0)
36 #define SC2731_CC_EN BIT(13)
37 #define SC2731_CHARGER_PD BIT(0)
39 /* SC2731_CHG_CFG1 register definition */
40 #define SC2731_CUR_MASK GENMASK(5, 0)
42 /* SC2731_CHG_CFG5 register definition */
43 #define SC2731_CUR_LIMIT_SHIFT 8
44 #define SC2731_CUR_LIMIT_MASK GENMASK(9, 8)
46 /* Default current definition (unit is mA) */
47 #define SC2731_CURRENT_LIMIT_100 100
48 #define SC2731_CURRENT_LIMIT_500 500
49 #define SC2731_CURRENT_LIMIT_900 900
50 #define SC2731_CURRENT_LIMIT_2000 2000
51 #define SC2731_CURRENT_PRECHG 450
52 #define SC2731_CURRENT_STEP 50
54 struct sc2731_charger_info
{
56 struct regmap
*regmap
;
57 struct usb_phy
*usb_phy
;
58 struct notifier_block usb_notify
;
59 struct power_supply
*psy_usb
;
60 struct work_struct work
;
67 static void sc2731_charger_stop_charge(struct sc2731_charger_info
*info
)
69 regmap_update_bits(info
->regmap
, info
->base
+ SC2731_CHG_CFG0
,
72 regmap_update_bits(info
->regmap
, info
->base
+ SC2731_CHG_CFG0
,
73 SC2731_CHARGER_PD
, SC2731_CHARGER_PD
);
76 static int sc2731_charger_start_charge(struct sc2731_charger_info
*info
)
80 /* Enable charger constant current mode */
81 ret
= regmap_update_bits(info
->regmap
, info
->base
+ SC2731_CHG_CFG0
,
82 SC2731_CC_EN
, SC2731_CC_EN
);
87 return regmap_update_bits(info
->regmap
, info
->base
+ SC2731_CHG_CFG0
,
88 SC2731_CHARGER_PD
, 0);
91 static int sc2731_charger_set_current_limit(struct sc2731_charger_info
*info
,
96 if (limit
<= SC2731_CURRENT_LIMIT_100
)
98 else if (limit
<= SC2731_CURRENT_LIMIT_500
)
100 else if (limit
<= SC2731_CURRENT_LIMIT_900
)
105 return regmap_update_bits(info
->regmap
, info
->base
+ SC2731_CHG_CFG5
,
106 SC2731_CUR_LIMIT_MASK
,
107 val
<< SC2731_CUR_LIMIT_SHIFT
);
110 static int sc2731_charger_set_current(struct sc2731_charger_info
*info
, u32 cur
)
115 if (cur
> SC2731_CURRENT_LIMIT_2000
)
116 cur
= SC2731_CURRENT_LIMIT_2000
;
117 else if (cur
< SC2731_CURRENT_PRECHG
)
118 cur
= SC2731_CURRENT_PRECHG
;
120 /* Calculate the step value, each step is 50 mA */
121 val
= (cur
- SC2731_CURRENT_PRECHG
) / SC2731_CURRENT_STEP
;
123 /* Set pre-charge current as 450 mA */
124 ret
= regmap_update_bits(info
->regmap
, info
->base
+ SC2731_CHG_CFG0
,
125 SC2731_PRECHG_RNG_MASK
,
126 0x3 << SC2731_PRECHG_RNG_SHIFT
);
130 return regmap_update_bits(info
->regmap
, info
->base
+ SC2731_CHG_CFG1
,
131 SC2731_CUR_MASK
, val
);
134 static int sc2731_charger_get_status(struct sc2731_charger_info
*info
)
139 ret
= regmap_read(info
->regmap
, SC2731_CHARGE_STATUS
, &val
);
143 if (val
& SC2731_CHARGE_FULL
)
144 return POWER_SUPPLY_STATUS_FULL
;
146 return POWER_SUPPLY_STATUS_CHARGING
;
149 static int sc2731_charger_get_current(struct sc2731_charger_info
*info
,
155 ret
= regmap_read(info
->regmap
, info
->base
+ SC2731_CHG_CFG1
, &val
);
159 val
&= SC2731_CUR_MASK
;
160 *cur
= val
* SC2731_CURRENT_STEP
+ SC2731_CURRENT_PRECHG
;
165 static int sc2731_charger_get_current_limit(struct sc2731_charger_info
*info
,
171 ret
= regmap_read(info
->regmap
, info
->base
+ SC2731_CHG_CFG5
, &val
);
175 val
= (val
& SC2731_CUR_LIMIT_MASK
) >> SC2731_CUR_LIMIT_SHIFT
;
179 *cur
= SC2731_CURRENT_LIMIT_100
;
183 *cur
= SC2731_CURRENT_LIMIT_2000
;
187 *cur
= SC2731_CURRENT_LIMIT_900
;
191 *cur
= SC2731_CURRENT_LIMIT_500
;
202 sc2731_charger_usb_set_property(struct power_supply
*psy
,
203 enum power_supply_property psp
,
204 const union power_supply_propval
*val
)
206 struct sc2731_charger_info
*info
= power_supply_get_drvdata(psy
);
209 mutex_lock(&info
->lock
);
211 if (!info
->charging
) {
212 mutex_unlock(&info
->lock
);
217 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
218 ret
= sc2731_charger_set_current(info
, val
->intval
/ 1000);
220 dev_err(info
->dev
, "set charge current failed\n");
223 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
224 ret
= sc2731_charger_set_current_limit(info
,
227 dev_err(info
->dev
, "set input current limit failed\n");
234 mutex_unlock(&info
->lock
);
238 static int sc2731_charger_usb_get_property(struct power_supply
*psy
,
239 enum power_supply_property psp
,
240 union power_supply_propval
*val
)
242 struct sc2731_charger_info
*info
= power_supply_get_drvdata(psy
);
246 mutex_lock(&info
->lock
);
249 case POWER_SUPPLY_PROP_STATUS
:
251 val
->intval
= sc2731_charger_get_status(info
);
253 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
256 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
257 if (!info
->charging
) {
260 ret
= sc2731_charger_get_current(info
, &cur
);
264 val
->intval
= cur
* 1000;
268 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
269 if (!info
->charging
) {
272 ret
= sc2731_charger_get_current_limit(info
, &cur
);
276 val
->intval
= cur
* 1000;
285 mutex_unlock(&info
->lock
);
289 static int sc2731_charger_property_is_writeable(struct power_supply
*psy
,
290 enum power_supply_property psp
)
295 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
296 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
307 static enum power_supply_property sc2731_usb_props
[] = {
308 POWER_SUPPLY_PROP_STATUS
,
309 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
310 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
313 static const struct power_supply_desc sc2731_charger_desc
= {
314 .name
= "sc2731_charger",
315 .type
= POWER_SUPPLY_TYPE_USB
,
316 .properties
= sc2731_usb_props
,
317 .num_properties
= ARRAY_SIZE(sc2731_usb_props
),
318 .get_property
= sc2731_charger_usb_get_property
,
319 .set_property
= sc2731_charger_usb_set_property
,
320 .property_is_writeable
= sc2731_charger_property_is_writeable
,
323 static void sc2731_charger_work(struct work_struct
*data
)
325 struct sc2731_charger_info
*info
=
326 container_of(data
, struct sc2731_charger_info
, work
);
329 mutex_lock(&info
->lock
);
331 if (info
->limit
> 0 && !info
->charging
) {
332 /* set current limitation and start to charge */
333 ret
= sc2731_charger_set_current_limit(info
, info
->limit
);
337 ret
= sc2731_charger_set_current(info
, info
->limit
);
341 ret
= sc2731_charger_start_charge(info
);
345 info
->charging
= true;
346 } else if (!info
->limit
&& info
->charging
) {
348 info
->charging
= false;
349 sc2731_charger_stop_charge(info
);
353 mutex_unlock(&info
->lock
);
356 static int sc2731_charger_usb_change(struct notifier_block
*nb
,
357 unsigned long limit
, void *data
)
359 struct sc2731_charger_info
*info
=
360 container_of(nb
, struct sc2731_charger_info
, usb_notify
);
364 schedule_work(&info
->work
);
369 static int sc2731_charger_hw_init(struct sc2731_charger_info
*info
)
371 struct power_supply_battery_info bat_info
= { };
372 u32 term_currrent
, term_voltage
, cur_val
, vol_val
;
375 /* Enable charger module */
376 ret
= regmap_update_bits(info
->regmap
, SC2731_MODULE_EN1
,
377 SC2731_CHARGE_EN
, SC2731_CHARGE_EN
);
381 ret
= power_supply_get_battery_info(info
->psy_usb
, &bat_info
);
383 dev_warn(info
->dev
, "no battery information is supplied\n");
386 * If no battery information is supplied, we should set
387 * default charge termination current to 120 mA, and default
388 * charge termination voltage to 4.35V.
393 term_currrent
= bat_info
.charge_term_current_ua
/ 1000;
395 if (term_currrent
<= 90)
397 else if (term_currrent
>= 265)
400 cur_val
= ((term_currrent
- 90) / 25) + 1;
402 term_voltage
= bat_info
.constant_charge_voltage_max_uv
/ 1000;
404 if (term_voltage
> 4500)
407 if (term_voltage
> 4200)
408 vol_val
= (term_voltage
- 4200) / 100;
412 power_supply_put_battery_info(info
->psy_usb
, &bat_info
);
415 /* Set charge termination current */
416 ret
= regmap_update_bits(info
->regmap
, info
->base
+ SC2731_CHG_CFG2
,
417 SC2731_TERMINATION_CUR_MASK
, cur_val
);
421 /* Set charge termination voltage */
422 ret
= regmap_update_bits(info
->regmap
, info
->base
+ SC2731_CHG_CFG0
,
423 SC2731_TERMINATION_VOL_MASK
|
424 SC2731_TERMINATION_VOL_CAL_MASK
,
425 (vol_val
<< SC2731_TERMINATION_VOL_SHIFT
) |
426 (0x6 << SC2731_TERMINATION_VOL_CAL_SHIFT
));
433 regmap_update_bits(info
->regmap
, SC2731_MODULE_EN1
, SC2731_CHARGE_EN
, 0);
437 static void sc2731_charger_detect_status(struct sc2731_charger_info
*info
)
439 unsigned int min
, max
;
442 * If the USB charger status has been USB_CHARGER_PRESENT before
443 * registering the notifier, we should start to charge with getting
444 * the charge current.
446 if (info
->usb_phy
->chg_state
!= USB_CHARGER_PRESENT
)
449 usb_phy_get_charger_current(info
->usb_phy
, &min
, &max
);
452 schedule_work(&info
->work
);
455 static int sc2731_charger_probe(struct platform_device
*pdev
)
457 struct device_node
*np
= pdev
->dev
.of_node
;
458 struct sc2731_charger_info
*info
;
459 struct power_supply_config charger_cfg
= { };
462 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
466 mutex_init(&info
->lock
);
467 info
->dev
= &pdev
->dev
;
468 INIT_WORK(&info
->work
, sc2731_charger_work
);
470 info
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
472 dev_err(&pdev
->dev
, "failed to get charger regmap\n");
476 ret
= of_property_read_u32(np
, "reg", &info
->base
);
478 dev_err(&pdev
->dev
, "failed to get register address\n");
482 charger_cfg
.drv_data
= info
;
483 charger_cfg
.of_node
= np
;
484 info
->psy_usb
= devm_power_supply_register(&pdev
->dev
,
485 &sc2731_charger_desc
,
487 if (IS_ERR(info
->psy_usb
)) {
488 dev_err(&pdev
->dev
, "failed to register power supply\n");
489 return PTR_ERR(info
->psy_usb
);
492 ret
= sc2731_charger_hw_init(info
);
496 info
->usb_phy
= devm_usb_get_phy_by_phandle(&pdev
->dev
, "phys", 0);
497 if (IS_ERR(info
->usb_phy
)) {
498 dev_err(&pdev
->dev
, "failed to find USB phy\n");
499 return PTR_ERR(info
->usb_phy
);
502 info
->usb_notify
.notifier_call
= sc2731_charger_usb_change
;
503 ret
= usb_register_notifier(info
->usb_phy
, &info
->usb_notify
);
505 dev_err(&pdev
->dev
, "failed to register notifier: %d\n", ret
);
509 sc2731_charger_detect_status(info
);
514 static int sc2731_charger_remove(struct platform_device
*pdev
)
516 struct sc2731_charger_info
*info
= platform_get_drvdata(pdev
);
518 usb_unregister_notifier(info
->usb_phy
, &info
->usb_notify
);
523 static const struct of_device_id sc2731_charger_of_match
[] = {
524 { .compatible
= "sprd,sc2731-charger", },
528 static struct platform_driver sc2731_charger_driver
= {
530 .name
= "sc2731-charger",
531 .of_match_table
= sc2731_charger_of_match
,
533 .probe
= sc2731_charger_probe
,
534 .remove
= sc2731_charger_remove
,
537 module_platform_driver(sc2731_charger_driver
);
539 MODULE_DESCRIPTION("Spreadtrum SC2731 Charger Driver");
540 MODULE_LICENSE("GPL v2");