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/module.h>
10 #include <linux/slab.h>
11 #include <linux/platform_device.h>
12 #include <linux/power_supply.h>
13 #include <linux/mfd/max8997.h>
14 #include <linux/mfd/max8997-private.h>
16 /* MAX8997_REG_STATUS4 */
17 #define DCINOK_SHIFT 1
18 #define DCINOK_MASK (1 << DCINOK_SHIFT)
19 #define DETBAT_SHIFT 2
20 #define DETBAT_MASK (1 << DETBAT_SHIFT)
22 /* MAX8997_REG_MBCCTRL1 */
24 #define TFCH_MASK (7 << TFCH_SHIFT)
26 /* MAX8997_REG_MBCCTRL5 */
27 #define ITOPOFF_SHIFT 0
28 #define ITOPOFF_MASK (0xF << ITOPOFF_SHIFT)
32 struct max8997_dev
*iodev
;
33 struct power_supply
*battery
;
36 static enum power_supply_property max8997_battery_props
[] = {
37 POWER_SUPPLY_PROP_STATUS
, /* "FULL", "CHARGING" or "DISCHARGING". */
38 POWER_SUPPLY_PROP_PRESENT
, /* the presence of battery */
39 POWER_SUPPLY_PROP_ONLINE
, /* charger is active or not */
42 /* Note that the charger control is done by a current regulator "CHARGER" */
43 static int max8997_battery_get_property(struct power_supply
*psy
,
44 enum power_supply_property psp
,
45 union power_supply_propval
*val
)
47 struct charger_data
*charger
= power_supply_get_drvdata(psy
);
48 struct i2c_client
*i2c
= charger
->iodev
->i2c
;
53 case POWER_SUPPLY_PROP_STATUS
:
55 ret
= max8997_read_reg(i2c
, MAX8997_REG_STATUS4
, ®
);
58 if ((reg
& (1 << 0)) == 0x1)
59 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
60 else if ((reg
& DCINOK_MASK
))
61 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
63 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
66 case POWER_SUPPLY_PROP_PRESENT
:
68 ret
= max8997_read_reg(i2c
, MAX8997_REG_STATUS4
, ®
);
71 if ((reg
& DETBAT_MASK
) == 0x0)
75 case POWER_SUPPLY_PROP_ONLINE
:
77 ret
= max8997_read_reg(i2c
, MAX8997_REG_STATUS4
, ®
);
80 if (reg
& DCINOK_MASK
)
91 static const struct power_supply_desc max8997_battery_desc
= {
92 .name
= "max8997_pmic",
93 .type
= POWER_SUPPLY_TYPE_BATTERY
,
94 .get_property
= max8997_battery_get_property
,
95 .properties
= max8997_battery_props
,
96 .num_properties
= ARRAY_SIZE(max8997_battery_props
),
99 static int max8997_battery_probe(struct platform_device
*pdev
)
102 struct charger_data
*charger
;
103 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
104 struct i2c_client
*i2c
= iodev
->i2c
;
105 struct max8997_platform_data
*pdata
= iodev
->pdata
;
106 struct power_supply_config psy_cfg
= {};
109 dev_err(&pdev
->dev
, "No platform data supplied.\n");
114 int val
= (pdata
->eoc_mA
- 50) / 10;
120 ret
= max8997_update_reg(i2c
, MAX8997_REG_MBCCTRL5
,
121 val
<< ITOPOFF_SHIFT
, ITOPOFF_MASK
);
123 dev_err(&pdev
->dev
, "Cannot use i2c bus.\n");
127 switch (pdata
->timeout
) {
129 ret
= max8997_update_reg(i2c
, MAX8997_REG_MBCCTRL1
,
130 0x2 << TFCH_SHIFT
, TFCH_MASK
);
133 ret
= max8997_update_reg(i2c
, MAX8997_REG_MBCCTRL1
,
134 0x3 << TFCH_SHIFT
, TFCH_MASK
);
137 ret
= max8997_update_reg(i2c
, MAX8997_REG_MBCCTRL1
,
138 0x4 << TFCH_SHIFT
, TFCH_MASK
);
141 ret
= max8997_update_reg(i2c
, MAX8997_REG_MBCCTRL1
,
142 0x7 << TFCH_SHIFT
, TFCH_MASK
);
145 dev_err(&pdev
->dev
, "incorrect timeout value (%d)\n",
150 dev_err(&pdev
->dev
, "Cannot use i2c bus.\n");
154 charger
= devm_kzalloc(&pdev
->dev
, sizeof(*charger
), GFP_KERNEL
);
158 platform_set_drvdata(pdev
, charger
);
160 charger
->dev
= &pdev
->dev
;
161 charger
->iodev
= iodev
;
163 psy_cfg
.drv_data
= charger
;
165 charger
->battery
= devm_power_supply_register(&pdev
->dev
,
166 &max8997_battery_desc
,
168 if (IS_ERR(charger
->battery
)) {
169 dev_err(&pdev
->dev
, "failed: power supply register\n");
170 return PTR_ERR(charger
->battery
);
176 static const struct platform_device_id max8997_battery_id
[] = {
177 { "max8997-battery", 0 },
180 MODULE_DEVICE_TABLE(platform
, max8997_battery_id
);
182 static struct platform_driver max8997_battery_driver
= {
184 .name
= "max8997-battery",
186 .probe
= max8997_battery_probe
,
187 .id_table
= max8997_battery_id
,
189 module_platform_driver(max8997_battery_driver
);
191 MODULE_DESCRIPTION("MAXIM 8997/8966 battery control driver");
192 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
193 MODULE_LICENSE("GPL");