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 */
28 /* Note that the charger control is done by a current regulator "CHARGER" */
29 static int max8998_battery_get_property(struct power_supply
*psy
,
30 enum power_supply_property psp
,
31 union power_supply_propval
*val
)
33 struct max8998_battery_data
*max8998
= power_supply_get_drvdata(psy
);
34 struct i2c_client
*i2c
= max8998
->iodev
->i2c
;
39 case POWER_SUPPLY_PROP_PRESENT
:
40 ret
= max8998_read_reg(i2c
, MAX8998_REG_STATUS2
, ®
);
48 case POWER_SUPPLY_PROP_ONLINE
:
49 ret
= max8998_read_reg(i2c
, MAX8998_REG_STATUS2
, ®
);
64 static const struct power_supply_desc max8998_battery_desc
= {
65 .name
= "max8998_pmic",
66 .type
= POWER_SUPPLY_TYPE_BATTERY
,
67 .get_property
= max8998_battery_get_property
,
68 .properties
= max8998_battery_props
,
69 .num_properties
= ARRAY_SIZE(max8998_battery_props
),
72 static int max8998_battery_probe(struct platform_device
*pdev
)
74 struct max8998_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
75 struct max8998_platform_data
*pdata
= iodev
->pdata
;
76 struct power_supply_config psy_cfg
= {};
77 struct max8998_battery_data
*max8998
;
78 struct i2c_client
*i2c
;
82 dev_err(pdev
->dev
.parent
, "No platform init data supplied\n");
86 max8998
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8998_battery_data
),
91 max8998
->dev
= &pdev
->dev
;
92 max8998
->iodev
= iodev
;
93 platform_set_drvdata(pdev
, max8998
);
94 i2c
= max8998
->iodev
->i2c
;
96 /* Setup "End of Charge" */
97 /* If EOC value equals 0,
98 * remain value set from bootloader or default value */
99 if (pdata
->eoc
>= 10 && pdata
->eoc
<= 45) {
100 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
,
101 (pdata
->eoc
/ 5 - 2) << 5, 0x7 << 5);
102 } else if (pdata
->eoc
== 0) {
103 dev_dbg(max8998
->dev
,
104 "EOC value not set: leave it unchanged.\n");
106 dev_err(max8998
->dev
, "Invalid EOC value\n");
110 /* Setup Charge Restart Level */
111 switch (pdata
->restart
) {
113 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x1 << 3, 0x3 << 3);
116 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x0 << 3, 0x3 << 3);
119 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x2 << 3, 0x3 << 3);
122 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x3 << 3, 0x3 << 3);
125 dev_dbg(max8998
->dev
,
126 "Restart Level not set: leave it unchanged.\n");
129 dev_err(max8998
->dev
, "Invalid Restart Level\n");
133 /* Setup Charge Full Timeout */
134 switch (pdata
->timeout
) {
136 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x0 << 4, 0x3 << 4);
139 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x1 << 4, 0x3 << 4);
142 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x2 << 4, 0x3 << 4);
145 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x3 << 4, 0x3 << 4);
148 dev_dbg(max8998
->dev
,
149 "Full Timeout not set: leave it unchanged.\n");
152 dev_err(max8998
->dev
, "Invalid Full Timeout value\n");
156 psy_cfg
.drv_data
= max8998
;
158 max8998
->battery
= devm_power_supply_register(max8998
->dev
,
159 &max8998_battery_desc
,
161 if (IS_ERR(max8998
->battery
)) {
162 ret
= PTR_ERR(max8998
->battery
);
163 dev_err(max8998
->dev
, "failed: power supply register: %d\n",
171 static const struct platform_device_id max8998_battery_id
[] = {
172 { "max8998-battery", TYPE_MAX8998
},
176 static struct platform_driver max8998_battery_driver
= {
178 .name
= "max8998-battery",
180 .probe
= max8998_battery_probe
,
181 .id_table
= max8998_battery_id
,
184 module_platform_driver(max8998_battery_driver
);
186 MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
187 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
188 MODULE_LICENSE("GPL");
189 MODULE_ALIAS("platform:max8998-battery");