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>
18 struct max8997_dev
*iodev
;
19 struct power_supply
*battery
;
22 static enum power_supply_property max8997_battery_props
[] = {
23 POWER_SUPPLY_PROP_STATUS
, /* "FULL" or "NOT FULL" only. */
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 max8997_battery_get_property(struct power_supply
*psy
,
30 enum power_supply_property psp
,
31 union power_supply_propval
*val
)
33 struct charger_data
*charger
= power_supply_get_drvdata(psy
);
34 struct i2c_client
*i2c
= charger
->iodev
->i2c
;
39 case POWER_SUPPLY_PROP_STATUS
:
41 ret
= max8997_read_reg(i2c
, MAX8997_REG_STATUS4
, ®
);
44 if ((reg
& (1 << 0)) == 0x1)
45 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
48 case POWER_SUPPLY_PROP_PRESENT
:
50 ret
= max8997_read_reg(i2c
, MAX8997_REG_STATUS4
, ®
);
53 if ((reg
& (1 << 2)) == 0x0)
57 case POWER_SUPPLY_PROP_ONLINE
:
59 ret
= max8997_read_reg(i2c
, MAX8997_REG_STATUS4
, ®
);
74 static const struct power_supply_desc max8997_battery_desc
= {
75 .name
= "max8997_pmic",
76 .type
= POWER_SUPPLY_TYPE_BATTERY
,
77 .get_property
= max8997_battery_get_property
,
78 .properties
= max8997_battery_props
,
79 .num_properties
= ARRAY_SIZE(max8997_battery_props
),
82 static int max8997_battery_probe(struct platform_device
*pdev
)
85 struct charger_data
*charger
;
86 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
87 struct max8997_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
88 struct power_supply_config psy_cfg
= {};
94 int val
= (pdata
->eoc_mA
- 50) / 10;
100 ret
= max8997_update_reg(iodev
->i2c
,
101 MAX8997_REG_MBCCTRL5
, val
, 0xf);
103 dev_err(&pdev
->dev
, "Cannot use i2c bus.\n");
108 switch (pdata
->timeout
) {
110 ret
= max8997_update_reg(iodev
->i2c
, MAX8997_REG_MBCCTRL1
,
114 ret
= max8997_update_reg(iodev
->i2c
, MAX8997_REG_MBCCTRL1
,
118 ret
= max8997_update_reg(iodev
->i2c
, MAX8997_REG_MBCCTRL1
,
122 ret
= max8997_update_reg(iodev
->i2c
, MAX8997_REG_MBCCTRL1
,
126 dev_err(&pdev
->dev
, "incorrect timeout value (%d)\n",
131 dev_err(&pdev
->dev
, "Cannot use i2c bus.\n");
135 charger
= devm_kzalloc(&pdev
->dev
, sizeof(*charger
), GFP_KERNEL
);
139 platform_set_drvdata(pdev
, charger
);
142 charger
->dev
= &pdev
->dev
;
143 charger
->iodev
= iodev
;
145 psy_cfg
.drv_data
= charger
;
147 charger
->battery
= devm_power_supply_register(&pdev
->dev
,
148 &max8997_battery_desc
,
150 if (IS_ERR(charger
->battery
)) {
151 dev_err(&pdev
->dev
, "failed: power supply register\n");
152 return PTR_ERR(charger
->battery
);
158 static const struct platform_device_id max8997_battery_id
[] = {
159 { "max8997-battery", 0 },
162 MODULE_DEVICE_TABLE(platform
, max8997_battery_id
);
164 static struct platform_driver max8997_battery_driver
= {
166 .name
= "max8997-battery",
168 .probe
= max8997_battery_probe
,
169 .id_table
= max8997_battery_id
,
172 static int __init
max8997_battery_init(void)
174 return platform_driver_register(&max8997_battery_driver
);
176 subsys_initcall(max8997_battery_init
);
178 static void __exit
max8997_battery_cleanup(void)
180 platform_driver_unregister(&max8997_battery_driver
);
182 module_exit(max8997_battery_cleanup
);
184 MODULE_DESCRIPTION("MAXIM 8997/8966 battery control driver");
185 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
186 MODULE_LICENSE("GPL");