1 // SPDX-License-Identifier: GPL-2.0+
3 // max8997_charger.c - Power supply consumer driver for the Maxim 8997/8966
5 // Copyright (C) 2011 Samsung Electronics
6 // MyungJoo Ham <myungjoo.ham@samsung.com>
9 #include <linux/extcon.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/platform_device.h>
13 #include <linux/power_supply.h>
14 #include <linux/mfd/max8997.h>
15 #include <linux/mfd/max8997-private.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/devm-helpers.h>
19 /* MAX8997_REG_STATUS4 */
20 #define DCINOK_SHIFT 1
21 #define DCINOK_MASK (1 << DCINOK_SHIFT)
22 #define DETBAT_SHIFT 2
23 #define DETBAT_MASK (1 << DETBAT_SHIFT)
25 /* MAX8997_REG_MBCCTRL1 */
27 #define TFCH_MASK (7 << TFCH_SHIFT)
29 /* MAX8997_REG_MBCCTRL5 */
30 #define ITOPOFF_SHIFT 0
31 #define ITOPOFF_MASK (0xF << ITOPOFF_SHIFT)
35 struct max8997_dev
*iodev
;
36 struct power_supply
*battery
;
37 struct regulator
*reg
;
38 struct extcon_dev
*edev
;
39 struct notifier_block extcon_nb
;
40 struct work_struct extcon_work
;
43 static enum power_supply_property max8997_battery_props
[] = {
44 POWER_SUPPLY_PROP_STATUS
, /* "FULL", "CHARGING" or "DISCHARGING". */
45 POWER_SUPPLY_PROP_PRESENT
, /* the presence of battery */
46 POWER_SUPPLY_PROP_ONLINE
, /* charger is active or not */
49 /* Note that the charger control is done by a current regulator "CHARGER" */
50 static int max8997_battery_get_property(struct power_supply
*psy
,
51 enum power_supply_property psp
,
52 union power_supply_propval
*val
)
54 struct charger_data
*charger
= power_supply_get_drvdata(psy
);
55 struct i2c_client
*i2c
= charger
->iodev
->i2c
;
60 case POWER_SUPPLY_PROP_STATUS
:
62 ret
= max8997_read_reg(i2c
, MAX8997_REG_STATUS4
, ®
);
65 if ((reg
& (1 << 0)) == 0x1)
66 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
67 else if ((reg
& DCINOK_MASK
))
68 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
70 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
73 case POWER_SUPPLY_PROP_PRESENT
:
75 ret
= max8997_read_reg(i2c
, MAX8997_REG_STATUS4
, ®
);
78 if ((reg
& DETBAT_MASK
) == 0x0)
82 case POWER_SUPPLY_PROP_ONLINE
:
84 ret
= max8997_read_reg(i2c
, MAX8997_REG_STATUS4
, ®
);
87 if (reg
& DCINOK_MASK
)
98 static void max8997_battery_extcon_evt_worker(struct work_struct
*work
)
100 struct charger_data
*charger
=
101 container_of(work
, struct charger_data
, extcon_work
);
102 struct extcon_dev
*edev
= charger
->edev
;
105 if (extcon_get_state(edev
, EXTCON_CHG_USB_SDP
) > 0) {
106 dev_dbg(charger
->dev
, "USB SDP charger is connected\n");
107 current_limit
= 450000;
108 } else if (extcon_get_state(edev
, EXTCON_CHG_USB_DCP
) > 0) {
109 dev_dbg(charger
->dev
, "USB DCP charger is connected\n");
110 current_limit
= 650000;
111 } else if (extcon_get_state(edev
, EXTCON_CHG_USB_FAST
) > 0) {
112 dev_dbg(charger
->dev
, "USB FAST charger is connected\n");
113 current_limit
= 650000;
114 } else if (extcon_get_state(edev
, EXTCON_CHG_USB_SLOW
) > 0) {
115 dev_dbg(charger
->dev
, "USB SLOW charger is connected\n");
116 current_limit
= 650000;
117 } else if (extcon_get_state(edev
, EXTCON_CHG_USB_CDP
) > 0) {
118 dev_dbg(charger
->dev
, "USB CDP charger is connected\n");
119 current_limit
= 650000;
121 dev_dbg(charger
->dev
, "USB charger is disconnected\n");
125 if (current_limit
> 0) {
126 int ret
= regulator_set_current_limit(charger
->reg
, current_limit
, current_limit
);
129 dev_err(charger
->dev
, "failed to set current limit: %d\n", ret
);
132 ret
= regulator_enable(charger
->reg
);
134 dev_err(charger
->dev
, "failed to enable regulator: %d\n", ret
);
136 int ret
= regulator_disable(charger
->reg
);
139 dev_err(charger
->dev
, "failed to disable regulator: %d\n", ret
);
143 static int max8997_battery_extcon_evt(struct notifier_block
*nb
,
144 unsigned long event
, void *param
)
146 struct charger_data
*charger
=
147 container_of(nb
, struct charger_data
, extcon_nb
);
148 schedule_work(&charger
->extcon_work
);
152 static const struct power_supply_desc max8997_battery_desc
= {
153 .name
= "max8997_pmic",
154 .type
= POWER_SUPPLY_TYPE_BATTERY
,
155 .get_property
= max8997_battery_get_property
,
156 .properties
= max8997_battery_props
,
157 .num_properties
= ARRAY_SIZE(max8997_battery_props
),
160 static int max8997_battery_probe(struct platform_device
*pdev
)
163 struct charger_data
*charger
;
164 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
165 struct device_node
*np
= pdev
->dev
.of_node
;
166 struct i2c_client
*i2c
= iodev
->i2c
;
167 struct max8997_platform_data
*pdata
= iodev
->pdata
;
168 struct power_supply_config psy_cfg
= {};
171 dev_err(&pdev
->dev
, "No platform data supplied.\n");
176 int val
= (pdata
->eoc_mA
- 50) / 10;
182 ret
= max8997_update_reg(i2c
, MAX8997_REG_MBCCTRL5
,
183 val
<< ITOPOFF_SHIFT
, ITOPOFF_MASK
);
185 dev_err(&pdev
->dev
, "Cannot use i2c bus.\n");
189 switch (pdata
->timeout
) {
191 ret
= max8997_update_reg(i2c
, MAX8997_REG_MBCCTRL1
,
192 0x2 << TFCH_SHIFT
, TFCH_MASK
);
195 ret
= max8997_update_reg(i2c
, MAX8997_REG_MBCCTRL1
,
196 0x3 << TFCH_SHIFT
, TFCH_MASK
);
199 ret
= max8997_update_reg(i2c
, MAX8997_REG_MBCCTRL1
,
200 0x4 << TFCH_SHIFT
, TFCH_MASK
);
203 ret
= max8997_update_reg(i2c
, MAX8997_REG_MBCCTRL1
,
204 0x7 << TFCH_SHIFT
, TFCH_MASK
);
207 dev_err(&pdev
->dev
, "incorrect timeout value (%d)\n",
212 dev_err(&pdev
->dev
, "Cannot use i2c bus.\n");
216 charger
= devm_kzalloc(&pdev
->dev
, sizeof(*charger
), GFP_KERNEL
);
220 platform_set_drvdata(pdev
, charger
);
222 charger
->dev
= &pdev
->dev
;
223 charger
->iodev
= iodev
;
225 psy_cfg
.drv_data
= charger
;
227 charger
->battery
= devm_power_supply_register(&pdev
->dev
,
228 &max8997_battery_desc
,
230 if (IS_ERR(charger
->battery
)) {
231 dev_err(&pdev
->dev
, "failed: power supply register\n");
232 return PTR_ERR(charger
->battery
);
235 // grab regulator from parent device's node
236 pdev
->dev
.of_node
= iodev
->dev
->of_node
;
237 charger
->reg
= devm_regulator_get_optional(&pdev
->dev
, "charger");
238 pdev
->dev
.of_node
= np
;
239 if (IS_ERR(charger
->reg
)) {
240 if (PTR_ERR(charger
->reg
) == -EPROBE_DEFER
)
241 return -EPROBE_DEFER
;
242 dev_info(&pdev
->dev
, "couldn't get charger regulator\n");
244 charger
->edev
= extcon_get_extcon_dev("max8997-muic");
245 if (IS_ERR(charger
->edev
)) {
246 dev_err_probe(charger
->dev
, PTR_ERR(charger
->edev
),
247 "couldn't get extcon device: max8997-muic\n");
248 return PTR_ERR(charger
->edev
);
251 if (!IS_ERR(charger
->reg
) && !IS_ERR_OR_NULL(charger
->edev
)) {
252 ret
= devm_work_autocancel(&pdev
->dev
, &charger
->extcon_work
,
253 max8997_battery_extcon_evt_worker
);
255 dev_err(&pdev
->dev
, "failed to add extcon evt stop action: %d\n", ret
);
258 charger
->extcon_nb
.notifier_call
= max8997_battery_extcon_evt
;
259 ret
= devm_extcon_register_notifier_all(&pdev
->dev
, charger
->edev
,
260 &charger
->extcon_nb
);
262 dev_err(&pdev
->dev
, "failed to register extcon notifier\n");
270 static const struct platform_device_id max8997_battery_id
[] = {
271 { "max8997-battery", 0 },
274 MODULE_DEVICE_TABLE(platform
, max8997_battery_id
);
276 static struct platform_driver max8997_battery_driver
= {
278 .name
= "max8997-battery",
280 .probe
= max8997_battery_probe
,
281 .id_table
= max8997_battery_id
,
283 module_platform_driver(max8997_battery_driver
);
285 MODULE_DESCRIPTION("MAXIM 8997/8966 battery control driver");
286 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
287 MODULE_LICENSE("GPL");