1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/err.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/platform_device.h>
26#include <linux/power_supply.h>
27#include <linux/mfd/max8998.h>
28#include <linux/mfd/max8998-private.h>
29
30struct max8998_battery_data {
31 struct device *dev;
32 struct max8998_dev *iodev;
33 struct power_supply *battery;
34};
35
36static enum power_supply_property max8998_battery_props[] = {
37 POWER_SUPPLY_PROP_PRESENT,
38 POWER_SUPPLY_PROP_ONLINE,
39};
40
41
42static int max8998_battery_get_property(struct power_supply *psy,
43 enum power_supply_property psp,
44 union power_supply_propval *val)
45{
46 struct max8998_battery_data *max8998 = power_supply_get_drvdata(psy);
47 struct i2c_client *i2c = max8998->iodev->i2c;
48 int ret;
49 u8 reg;
50
51 switch (psp) {
52 case POWER_SUPPLY_PROP_PRESENT:
53 ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, ®);
54 if (ret)
55 return ret;
56 if (reg & (1 << 4))
57 val->intval = 0;
58 else
59 val->intval = 1;
60 break;
61 case POWER_SUPPLY_PROP_ONLINE:
62 ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, ®);
63 if (ret)
64 return ret;
65 if (reg & (1 << 3))
66 val->intval = 0;
67 else
68 val->intval = 1;
69 break;
70 default:
71 return -EINVAL;
72 }
73
74 return 0;
75}
76
77static const struct power_supply_desc max8998_battery_desc = {
78 .name = "max8998_pmic",
79 .type = POWER_SUPPLY_TYPE_BATTERY,
80 .get_property = max8998_battery_get_property,
81 .properties = max8998_battery_props,
82 .num_properties = ARRAY_SIZE(max8998_battery_props),
83};
84
85static int max8998_battery_probe(struct platform_device *pdev)
86{
87 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
88 struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
89 struct power_supply_config psy_cfg = {};
90 struct max8998_battery_data *max8998;
91 struct i2c_client *i2c;
92 int ret = 0;
93
94 if (!pdata) {
95 dev_err(pdev->dev.parent, "No platform init data supplied\n");
96 return -ENODEV;
97 }
98
99 max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_battery_data),
100 GFP_KERNEL);
101 if (!max8998)
102 return -ENOMEM;
103
104 max8998->dev = &pdev->dev;
105 max8998->iodev = iodev;
106 platform_set_drvdata(pdev, max8998);
107 i2c = max8998->iodev->i2c;
108
109
110
111
112 if (pdata->eoc >= 10 && pdata->eoc <= 45) {
113 max8998_update_reg(i2c, MAX8998_REG_CHGR1,
114 (pdata->eoc / 5 - 2) << 5, 0x7 << 5);
115 } else if (pdata->eoc == 0) {
116 dev_dbg(max8998->dev,
117 "EOC value not set: leave it unchanged.\n");
118 } else {
119 dev_err(max8998->dev, "Invalid EOC value\n");
120 return -EINVAL;
121 }
122
123
124 switch (pdata->restart) {
125 case 100:
126 max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x1 << 3, 0x3 << 3);
127 break;
128 case 150:
129 max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x0 << 3, 0x3 << 3);
130 break;
131 case 200:
132 max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x2 << 3, 0x3 << 3);
133 break;
134 case -1:
135 max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x3 << 3, 0x3 << 3);
136 break;
137 case 0:
138 dev_dbg(max8998->dev,
139 "Restart Level not set: leave it unchanged.\n");
140 break;
141 default:
142 dev_err(max8998->dev, "Invalid Restart Level\n");
143 return -EINVAL;
144 }
145
146
147 switch (pdata->timeout) {
148 case 5:
149 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x0 << 4, 0x3 << 4);
150 break;
151 case 6:
152 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x1 << 4, 0x3 << 4);
153 break;
154 case 7:
155 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x2 << 4, 0x3 << 4);
156 break;
157 case -1:
158 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x3 << 4, 0x3 << 4);
159 break;
160 case 0:
161 dev_dbg(max8998->dev,
162 "Full Timeout not set: leave it unchanged.\n");
163 break;
164 default:
165 dev_err(max8998->dev, "Invalid Full Timeout value\n");
166 return -EINVAL;
167 }
168
169 psy_cfg.drv_data = max8998;
170
171 max8998->battery = devm_power_supply_register(max8998->dev,
172 &max8998_battery_desc,
173 &psy_cfg);
174 if (IS_ERR(max8998->battery)) {
175 ret = PTR_ERR(max8998->battery);
176 dev_err(max8998->dev, "failed: power supply register: %d\n",
177 ret);
178 return ret;
179 }
180
181 return 0;
182}
183
184static const struct platform_device_id max8998_battery_id[] = {
185 { "max8998-battery", TYPE_MAX8998 },
186 { }
187};
188
189static struct platform_driver max8998_battery_driver = {
190 .driver = {
191 .name = "max8998-battery",
192 },
193 .probe = max8998_battery_probe,
194 .id_table = max8998_battery_id,
195};
196
197module_platform_driver(max8998_battery_driver);
198
199MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
200MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
201MODULE_LICENSE("GPL");
202MODULE_ALIAS("platform:max8998-battery");
203