1 // SPDX-License-Identifier: GPL-2.0+
3 // max8998_charger.c - Power supply consumer driver for the Maxim 8998/LP3974
5 // Copyright (C) 2009-2010 Samsung Electronics
6 // MyungJoo Ham <myungjoo.ham@samsung.com>
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/slab.h>
12 #include <linux/platform_device.h>
13 #include <linux/power_supply.h>
14 #include <linux/mfd/max8998.h>
15 #include <linux/mfd/max8998-private.h>
17 struct max8998_battery_data
{
19 struct max8998_dev
*iodev
;
20 struct power_supply
*battery
;
23 static enum power_supply_property max8998_battery_props
[] = {
24 POWER_SUPPLY_PROP_PRESENT
, /* the presence of battery */
25 POWER_SUPPLY_PROP_ONLINE
, /* charger is active or not */
26 POWER_SUPPLY_PROP_STATUS
, /* charger is charging/discharging/full */
29 /* Note that the charger control is done by a current regulator "CHARGER" */
30 static int max8998_battery_get_property(struct power_supply
*psy
,
31 enum power_supply_property psp
,
32 union power_supply_propval
*val
)
34 struct max8998_battery_data
*max8998
= power_supply_get_drvdata(psy
);
35 struct i2c_client
*i2c
= max8998
->iodev
->i2c
;
40 case POWER_SUPPLY_PROP_PRESENT
:
41 ret
= max8998_read_reg(i2c
, MAX8998_REG_STATUS2
, ®
);
49 case POWER_SUPPLY_PROP_ONLINE
:
50 ret
= max8998_read_reg(i2c
, MAX8998_REG_STATUS2
, ®
);
60 case POWER_SUPPLY_PROP_STATUS
:
61 ret
= max8998_read_reg(i2c
, MAX8998_REG_STATUS2
, ®
);
65 if (!(reg
& (1 << 5))) {
66 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
69 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
70 else if (reg
& (1 << 3))
71 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
73 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
83 static const struct power_supply_desc max8998_battery_desc
= {
84 .name
= "max8998_pmic",
85 .type
= POWER_SUPPLY_TYPE_BATTERY
,
86 .get_property
= max8998_battery_get_property
,
87 .properties
= max8998_battery_props
,
88 .num_properties
= ARRAY_SIZE(max8998_battery_props
),
91 static int max8998_battery_probe(struct platform_device
*pdev
)
93 struct max8998_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
94 struct max8998_platform_data
*pdata
= iodev
->pdata
;
95 struct power_supply_config psy_cfg
= {};
96 struct max8998_battery_data
*max8998
;
97 struct i2c_client
*i2c
;
101 dev_err(pdev
->dev
.parent
, "No platform init data supplied\n");
105 max8998
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8998_battery_data
),
110 max8998
->dev
= &pdev
->dev
;
111 max8998
->iodev
= iodev
;
112 platform_set_drvdata(pdev
, max8998
);
113 i2c
= max8998
->iodev
->i2c
;
115 /* Setup "End of Charge" */
116 /* If EOC value equals 0,
117 * remain value set from bootloader or default value */
118 if (pdata
->eoc
>= 10 && pdata
->eoc
<= 45) {
119 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
,
120 (pdata
->eoc
/ 5 - 2) << 5, 0x7 << 5);
121 } else if (pdata
->eoc
== 0) {
122 dev_dbg(max8998
->dev
,
123 "EOC value not set: leave it unchanged.\n");
125 dev_err(max8998
->dev
, "Invalid EOC value\n");
129 /* Setup Charge Restart Level */
130 switch (pdata
->restart
) {
132 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x1 << 3, 0x3 << 3);
135 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x0 << 3, 0x3 << 3);
138 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x2 << 3, 0x3 << 3);
141 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x3 << 3, 0x3 << 3);
144 dev_dbg(max8998
->dev
,
145 "Restart Level not set: leave it unchanged.\n");
148 dev_err(max8998
->dev
, "Invalid Restart Level\n");
152 /* Setup Charge Full Timeout */
153 switch (pdata
->timeout
) {
155 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x0 << 4, 0x3 << 4);
158 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x1 << 4, 0x3 << 4);
161 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x2 << 4, 0x3 << 4);
164 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x3 << 4, 0x3 << 4);
167 dev_dbg(max8998
->dev
,
168 "Full Timeout not set: leave it unchanged.\n");
171 dev_err(max8998
->dev
, "Invalid Full Timeout value\n");
175 psy_cfg
.drv_data
= max8998
;
177 max8998
->battery
= devm_power_supply_register(max8998
->dev
,
178 &max8998_battery_desc
,
180 if (IS_ERR(max8998
->battery
)) {
181 ret
= PTR_ERR(max8998
->battery
);
182 dev_err(max8998
->dev
, "failed: power supply register: %d\n",
190 static const struct platform_device_id max8998_battery_id
[] = {
191 { "max8998-battery", TYPE_MAX8998
},
195 static struct platform_driver max8998_battery_driver
= {
197 .name
= "max8998-battery",
199 .probe
= max8998_battery_probe
,
200 .id_table
= max8998_battery_id
,
203 module_platform_driver(max8998_battery_driver
);
205 MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
206 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
207 MODULE_LICENSE("GPL");
208 MODULE_ALIAS("platform:max8998-battery");