1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Battery measurement code for Zipit Z2
4 *
5 * Copyright (C) 2009 Peter Edwards <sweetlilmre@gmail.com>
6 */
7
8 #include <linux/module.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/power_supply.h>
14 #include <linux/slab.h>
15 #include <linux/z2_battery.h>
16
17 #define Z2_DEFAULT_NAME "Z2"
18
19 struct z2_charger {
20 struct z2_battery_info *info;
21 struct gpio_desc *charge_gpiod;
22 int bat_status;
23 struct i2c_client *client;
24 struct power_supply *batt_ps;
25 struct power_supply_desc batt_ps_desc;
26 struct mutex work_lock;
27 struct work_struct bat_work;
28 };
29
z2_read_bat(struct z2_charger * charger)30 static unsigned long z2_read_bat(struct z2_charger *charger)
31 {
32 int data;
33 data = i2c_smbus_read_byte_data(charger->client,
34 charger->info->batt_I2C_reg);
35 if (data < 0)
36 return 0;
37
38 return data * charger->info->batt_mult / charger->info->batt_div;
39 }
40
z2_batt_get_property(struct power_supply * batt_ps,enum power_supply_property psp,union power_supply_propval * val)41 static int z2_batt_get_property(struct power_supply *batt_ps,
42 enum power_supply_property psp,
43 union power_supply_propval *val)
44 {
45 struct z2_charger *charger = power_supply_get_drvdata(batt_ps);
46 struct z2_battery_info *info = charger->info;
47
48 switch (psp) {
49 case POWER_SUPPLY_PROP_STATUS:
50 val->intval = charger->bat_status;
51 break;
52 case POWER_SUPPLY_PROP_TECHNOLOGY:
53 val->intval = info->batt_tech;
54 break;
55 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
56 if (info->batt_I2C_reg >= 0)
57 val->intval = z2_read_bat(charger);
58 else
59 return -EINVAL;
60 break;
61 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
62 if (info->max_voltage >= 0)
63 val->intval = info->max_voltage;
64 else
65 return -EINVAL;
66 break;
67 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
68 if (info->min_voltage >= 0)
69 val->intval = info->min_voltage;
70 else
71 return -EINVAL;
72 break;
73 case POWER_SUPPLY_PROP_PRESENT:
74 val->intval = 1;
75 break;
76 default:
77 return -EINVAL;
78 }
79
80 return 0;
81 }
82
z2_batt_ext_power_changed(struct power_supply * batt_ps)83 static void z2_batt_ext_power_changed(struct power_supply *batt_ps)
84 {
85 struct z2_charger *charger = power_supply_get_drvdata(batt_ps);
86
87 schedule_work(&charger->bat_work);
88 }
89
z2_batt_update(struct z2_charger * charger)90 static void z2_batt_update(struct z2_charger *charger)
91 {
92 int old_status = charger->bat_status;
93
94 mutex_lock(&charger->work_lock);
95
96 charger->bat_status = charger->charge_gpiod ?
97 (gpiod_get_value(charger->charge_gpiod) ?
98 POWER_SUPPLY_STATUS_CHARGING :
99 POWER_SUPPLY_STATUS_DISCHARGING) :
100 POWER_SUPPLY_STATUS_UNKNOWN;
101
102 if (old_status != charger->bat_status) {
103 pr_debug("%s: %i -> %i\n", charger->batt_ps->desc->name,
104 old_status,
105 charger->bat_status);
106 power_supply_changed(charger->batt_ps);
107 }
108
109 mutex_unlock(&charger->work_lock);
110 }
111
z2_batt_work(struct work_struct * work)112 static void z2_batt_work(struct work_struct *work)
113 {
114 struct z2_charger *charger;
115 charger = container_of(work, struct z2_charger, bat_work);
116 z2_batt_update(charger);
117 }
118
z2_charge_switch_irq(int irq,void * devid)119 static irqreturn_t z2_charge_switch_irq(int irq, void *devid)
120 {
121 struct z2_charger *charger = devid;
122 schedule_work(&charger->bat_work);
123 return IRQ_HANDLED;
124 }
125
z2_batt_ps_init(struct z2_charger * charger,int props)126 static int z2_batt_ps_init(struct z2_charger *charger, int props)
127 {
128 int i = 0;
129 enum power_supply_property *prop;
130 struct z2_battery_info *info = charger->info;
131
132 if (charger->charge_gpiod)
133 props++; /* POWER_SUPPLY_PROP_STATUS */
134 if (info->batt_tech >= 0)
135 props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
136 if (info->batt_I2C_reg >= 0)
137 props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
138 if (info->max_voltage >= 0)
139 props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
140 if (info->min_voltage >= 0)
141 props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
142
143 prop = kcalloc(props, sizeof(*prop), GFP_KERNEL);
144 if (!prop)
145 return -ENOMEM;
146
147 prop[i++] = POWER_SUPPLY_PROP_PRESENT;
148 if (charger->charge_gpiod)
149 prop[i++] = POWER_SUPPLY_PROP_STATUS;
150 if (info->batt_tech >= 0)
151 prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
152 if (info->batt_I2C_reg >= 0)
153 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
154 if (info->max_voltage >= 0)
155 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
156 if (info->min_voltage >= 0)
157 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
158
159 if (!info->batt_name) {
160 dev_info(&charger->client->dev,
161 "Please consider setting proper battery "
162 "name in platform definition file, falling "
163 "back to name \" Z2_DEFAULT_NAME \"\n");
164 charger->batt_ps_desc.name = Z2_DEFAULT_NAME;
165 } else
166 charger->batt_ps_desc.name = info->batt_name;
167
168 charger->batt_ps_desc.properties = prop;
169 charger->batt_ps_desc.num_properties = props;
170 charger->batt_ps_desc.type = POWER_SUPPLY_TYPE_BATTERY;
171 charger->batt_ps_desc.get_property = z2_batt_get_property;
172 charger->batt_ps_desc.external_power_changed =
173 z2_batt_ext_power_changed;
174 charger->batt_ps_desc.use_for_apm = 1;
175
176 return 0;
177 }
178
z2_batt_probe(struct i2c_client * client,const struct i2c_device_id * id)179 static int z2_batt_probe(struct i2c_client *client,
180 const struct i2c_device_id *id)
181 {
182 int ret = 0;
183 int props = 1; /* POWER_SUPPLY_PROP_PRESENT */
184 struct z2_charger *charger;
185 struct z2_battery_info *info = client->dev.platform_data;
186 struct power_supply_config psy_cfg = {};
187
188 if (info == NULL) {
189 dev_err(&client->dev,
190 "Please set platform device platform_data"
191 " to a valid z2_battery_info pointer!\n");
192 return -EINVAL;
193 }
194
195 charger = kzalloc(sizeof(*charger), GFP_KERNEL);
196 if (charger == NULL)
197 return -ENOMEM;
198
199 charger->bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
200 charger->info = info;
201 charger->client = client;
202 i2c_set_clientdata(client, charger);
203 psy_cfg.drv_data = charger;
204
205 mutex_init(&charger->work_lock);
206
207 charger->charge_gpiod = devm_gpiod_get_optional(&client->dev,
208 NULL, GPIOD_IN);
209 if (IS_ERR(charger->charge_gpiod)) {
210 ret = dev_err_probe(&client->dev,
211 PTR_ERR(charger->charge_gpiod),
212 "failed to get charge GPIO\n");
213 goto err;
214 }
215
216 if (charger->charge_gpiod) {
217 gpiod_set_consumer_name(charger->charge_gpiod, "BATT CHRG");
218
219 irq_set_irq_type(gpiod_to_irq(charger->charge_gpiod),
220 IRQ_TYPE_EDGE_BOTH);
221 ret = request_irq(gpiod_to_irq(charger->charge_gpiod),
222 z2_charge_switch_irq, 0,
223 "AC Detect", charger);
224 if (ret)
225 goto err;
226 }
227
228 ret = z2_batt_ps_init(charger, props);
229 if (ret)
230 goto err3;
231
232 INIT_WORK(&charger->bat_work, z2_batt_work);
233
234 charger->batt_ps = power_supply_register(&client->dev,
235 &charger->batt_ps_desc,
236 &psy_cfg);
237 if (IS_ERR(charger->batt_ps)) {
238 ret = PTR_ERR(charger->batt_ps);
239 goto err4;
240 }
241
242 schedule_work(&charger->bat_work);
243
244 return 0;
245
246 err4:
247 kfree(charger->batt_ps_desc.properties);
248 err3:
249 if (charger->charge_gpiod)
250 free_irq(gpiod_to_irq(charger->charge_gpiod), charger);
251 err:
252 kfree(charger);
253 return ret;
254 }
255
z2_batt_remove(struct i2c_client * client)256 static int z2_batt_remove(struct i2c_client *client)
257 {
258 struct z2_charger *charger = i2c_get_clientdata(client);
259
260 cancel_work_sync(&charger->bat_work);
261 power_supply_unregister(charger->batt_ps);
262
263 kfree(charger->batt_ps_desc.properties);
264 if (charger->charge_gpiod)
265 free_irq(gpiod_to_irq(charger->charge_gpiod), charger);
266
267 kfree(charger);
268
269 return 0;
270 }
271
272 #ifdef CONFIG_PM
z2_batt_suspend(struct device * dev)273 static int z2_batt_suspend(struct device *dev)
274 {
275 struct i2c_client *client = to_i2c_client(dev);
276 struct z2_charger *charger = i2c_get_clientdata(client);
277
278 flush_work(&charger->bat_work);
279 return 0;
280 }
281
z2_batt_resume(struct device * dev)282 static int z2_batt_resume(struct device *dev)
283 {
284 struct i2c_client *client = to_i2c_client(dev);
285 struct z2_charger *charger = i2c_get_clientdata(client);
286
287 schedule_work(&charger->bat_work);
288 return 0;
289 }
290
291 static const struct dev_pm_ops z2_battery_pm_ops = {
292 .suspend = z2_batt_suspend,
293 .resume = z2_batt_resume,
294 };
295
296 #define Z2_BATTERY_PM_OPS (&z2_battery_pm_ops)
297
298 #else
299 #define Z2_BATTERY_PM_OPS (NULL)
300 #endif
301
302 static const struct i2c_device_id z2_batt_id[] = {
303 { "aer915", 0 },
304 { }
305 };
306 MODULE_DEVICE_TABLE(i2c, z2_batt_id);
307
308 static struct i2c_driver z2_batt_driver = {
309 .driver = {
310 .name = "z2-battery",
311 .pm = Z2_BATTERY_PM_OPS
312 },
313 .probe = z2_batt_probe,
314 .remove = z2_batt_remove,
315 .id_table = z2_batt_id,
316 };
317 module_i2c_driver(z2_batt_driver);
318
319 MODULE_LICENSE("GPL");
320 MODULE_AUTHOR("Peter Edwards <sweetlilmre@gmail.com>");
321 MODULE_DESCRIPTION("Zipit Z2 battery driver");
322