blob: fedc5818fab7b74b96b5da2f81b77d4a992c6baf [file] [log] [blame]
anish kumare60fea72012-09-21 17:10:00 +01001/*
2 * Generic battery driver code using IIO
3 * Copyright (C) 2012, Anish Kumar <anish198519851985@gmail.com>
4 * based on jz4740-battery.c
5 * based on s3c_adc_battery.c
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file COPYING in the main directory of this archive for
9 * more details.
10 *
11 */
12#include <linux/interrupt.h>
13#include <linux/platform_device.h>
14#include <linux/power_supply.h>
15#include <linux/gpio.h>
16#include <linux/err.h>
17#include <linux/timer.h>
18#include <linux/jiffies.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/iio/consumer.h>
24#include <linux/iio/types.h>
25#include <linux/power/generic-adc-battery.h>
26
27#define JITTER_DEFAULT 10 /* hope 10ms is enough */
28
29enum gab_chan_type {
30 GAB_VOLTAGE = 0,
31 GAB_CURRENT,
32 GAB_POWER,
33 GAB_MAX_CHAN_TYPE
34};
35
36/*
37 * gab_chan_name suggests the standard channel names for commonly used
38 * channel types.
39 */
40static const char *const gab_chan_name[] = {
41 [GAB_VOLTAGE] = "voltage",
42 [GAB_CURRENT] = "current",
43 [GAB_POWER] = "power",
44};
45
46struct gab {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010047 struct power_supply *psy;
48 struct power_supply_desc psy_desc;
anish kumare60fea72012-09-21 17:10:00 +010049 struct iio_channel *channel[GAB_MAX_CHAN_TYPE];
50 struct gab_platform_data *pdata;
51 struct delayed_work bat_work;
52 int level;
53 int status;
54 bool cable_plugged;
55};
56
57static struct gab *to_generic_bat(struct power_supply *psy)
58{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010059 return power_supply_get_drvdata(psy);
anish kumare60fea72012-09-21 17:10:00 +010060}
61
62static void gab_ext_power_changed(struct power_supply *psy)
63{
64 struct gab *adc_bat = to_generic_bat(psy);
65
66 schedule_delayed_work(&adc_bat->bat_work, msecs_to_jiffies(0));
67}
68
69static const enum power_supply_property gab_props[] = {
70 POWER_SUPPLY_PROP_STATUS,
71 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
72 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
73 POWER_SUPPLY_PROP_CHARGE_NOW,
74 POWER_SUPPLY_PROP_VOLTAGE_NOW,
75 POWER_SUPPLY_PROP_CURRENT_NOW,
76 POWER_SUPPLY_PROP_TECHNOLOGY,
77 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
78 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
79 POWER_SUPPLY_PROP_MODEL_NAME,
80};
81
82/*
83 * This properties are set based on the received platform data and this
84 * should correspond one-to-one with enum chan_type.
85 */
86static const enum power_supply_property gab_dyn_props[] = {
87 POWER_SUPPLY_PROP_VOLTAGE_NOW,
88 POWER_SUPPLY_PROP_CURRENT_NOW,
89 POWER_SUPPLY_PROP_POWER_NOW,
90};
91
92static bool gab_charge_finished(struct gab *adc_bat)
93{
94 struct gab_platform_data *pdata = adc_bat->pdata;
95 bool ret = gpio_get_value(pdata->gpio_charge_finished);
96 bool inv = pdata->gpio_inverted;
97
98 if (!gpio_is_valid(pdata->gpio_charge_finished))
99 return false;
100 return ret ^ inv;
101}
102
103static int gab_get_status(struct gab *adc_bat)
104{
105 struct gab_platform_data *pdata = adc_bat->pdata;
106 struct power_supply_info *bat_info;
107
108 bat_info = &pdata->battery_info;
109 if (adc_bat->level == bat_info->charge_full_design)
110 return POWER_SUPPLY_STATUS_FULL;
111 return adc_bat->status;
112}
113
114static enum gab_chan_type gab_prop_to_chan(enum power_supply_property psp)
115{
116 switch (psp) {
117 case POWER_SUPPLY_PROP_POWER_NOW:
118 return GAB_POWER;
119 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
120 return GAB_VOLTAGE;
121 case POWER_SUPPLY_PROP_CURRENT_NOW:
122 return GAB_CURRENT;
123 default:
124 WARN_ON(1);
125 break;
126 }
127 return GAB_POWER;
128}
129
130static int read_channel(struct gab *adc_bat, enum power_supply_property psp,
131 int *result)
132{
133 int ret;
134 int chan_index;
135
136 chan_index = gab_prop_to_chan(psp);
137 ret = iio_read_channel_processed(adc_bat->channel[chan_index],
138 result);
139 if (ret < 0)
140 pr_err("read channel error\n");
141 return ret;
142}
143
144static int gab_get_property(struct power_supply *psy,
145 enum power_supply_property psp, union power_supply_propval *val)
146{
147 struct gab *adc_bat;
148 struct gab_platform_data *pdata;
149 struct power_supply_info *bat_info;
150 int result = 0;
151 int ret = 0;
152
153 adc_bat = to_generic_bat(psy);
154 if (!adc_bat) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100155 dev_err(&psy->dev, "no battery infos ?!\n");
anish kumare60fea72012-09-21 17:10:00 +0100156 return -EINVAL;
157 }
158 pdata = adc_bat->pdata;
159 bat_info = &pdata->battery_info;
160
161 switch (psp) {
162 case POWER_SUPPLY_PROP_STATUS:
Nicolas Saenz Julienne05954392015-03-08 19:21:20 +0000163 val->intval = gab_get_status(adc_bat);
anish kumare60fea72012-09-21 17:10:00 +0100164 break;
165 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
166 val->intval = 0;
167 break;
168 case POWER_SUPPLY_PROP_CHARGE_NOW:
169 val->intval = pdata->cal_charge(result);
170 break;
171 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
172 case POWER_SUPPLY_PROP_CURRENT_NOW:
173 case POWER_SUPPLY_PROP_POWER_NOW:
174 ret = read_channel(adc_bat, psp, &result);
175 if (ret < 0)
176 goto err;
177 val->intval = result;
178 break;
179 case POWER_SUPPLY_PROP_TECHNOLOGY:
180 val->intval = bat_info->technology;
181 break;
182 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
183 val->intval = bat_info->voltage_min_design;
184 break;
185 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
186 val->intval = bat_info->voltage_max_design;
187 break;
188 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
189 val->intval = bat_info->charge_full_design;
190 break;
191 case POWER_SUPPLY_PROP_MODEL_NAME:
192 val->strval = bat_info->name;
193 break;
194 default:
195 return -EINVAL;
196 }
197err:
198 return ret;
199}
200
201static void gab_work(struct work_struct *work)
202{
203 struct gab *adc_bat;
204 struct gab_platform_data *pdata;
205 struct delayed_work *delayed_work;
206 bool is_plugged;
207 int status;
208
209 delayed_work = container_of(work, struct delayed_work, work);
210 adc_bat = container_of(delayed_work, struct gab, bat_work);
211 pdata = adc_bat->pdata;
212 status = adc_bat->status;
213
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100214 is_plugged = power_supply_am_i_supplied(adc_bat->psy);
anish kumare60fea72012-09-21 17:10:00 +0100215 adc_bat->cable_plugged = is_plugged;
216
217 if (!is_plugged)
218 adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
219 else if (gab_charge_finished(adc_bat))
220 adc_bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
221 else
222 adc_bat->status = POWER_SUPPLY_STATUS_CHARGING;
223
224 if (status != adc_bat->status)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100225 power_supply_changed(adc_bat->psy);
anish kumare60fea72012-09-21 17:10:00 +0100226}
227
228static irqreturn_t gab_charged(int irq, void *dev_id)
229{
230 struct gab *adc_bat = dev_id;
231 struct gab_platform_data *pdata = adc_bat->pdata;
232 int delay;
233
234 delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
235 schedule_delayed_work(&adc_bat->bat_work,
236 msecs_to_jiffies(delay));
237 return IRQ_HANDLED;
238}
239
Bill Pembertonc8afa642012-11-19 13:22:23 -0500240static int gab_probe(struct platform_device *pdev)
anish kumare60fea72012-09-21 17:10:00 +0100241{
242 struct gab *adc_bat;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100243 struct power_supply_desc *psy_desc;
244 struct power_supply_config psy_cfg = {};
anish kumare60fea72012-09-21 17:10:00 +0100245 struct gab_platform_data *pdata = pdev->dev.platform_data;
246 enum power_supply_property *properties;
247 int ret = 0;
248 int chan;
249 int index = 0;
250
251 adc_bat = devm_kzalloc(&pdev->dev, sizeof(*adc_bat), GFP_KERNEL);
252 if (!adc_bat) {
253 dev_err(&pdev->dev, "failed to allocate memory\n");
254 return -ENOMEM;
255 }
256
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100257 psy_cfg.drv_data = adc_bat;
258 psy_desc = &adc_bat->psy_desc;
259 psy_desc->name = pdata->battery_info.name;
anish kumare60fea72012-09-21 17:10:00 +0100260
261 /* bootup default values for the battery */
262 adc_bat->cable_plugged = false;
263 adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100264 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
265 psy_desc->get_property = gab_get_property;
266 psy_desc->external_power_changed = gab_ext_power_changed;
anish kumare60fea72012-09-21 17:10:00 +0100267 adc_bat->pdata = pdata;
268
anish kumare60fea72012-09-21 17:10:00 +0100269 /*
270 * copying the static properties and allocating extra memory for holding
271 * the extra configurable properties received from platform data.
272 */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100273 psy_desc->properties = kcalloc(ARRAY_SIZE(gab_props) +
anish kumare60fea72012-09-21 17:10:00 +0100274 ARRAY_SIZE(gab_chan_name),
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100275 sizeof(*psy_desc->properties),
276 GFP_KERNEL);
277 if (!psy_desc->properties) {
anish kumare60fea72012-09-21 17:10:00 +0100278 ret = -ENOMEM;
279 goto first_mem_fail;
280 }
281
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100282 memcpy(psy_desc->properties, gab_props, sizeof(gab_props));
Dan Carpentera77d60a2012-09-29 10:13:46 +0300283 properties = (enum power_supply_property *)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100284 ((char *)psy_desc->properties + sizeof(gab_props));
anish kumare60fea72012-09-21 17:10:00 +0100285
286 /*
287 * getting channel from iio and copying the battery properties
288 * based on the channel supported by consumer device.
289 */
290 for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000291 adc_bat->channel[chan] = iio_channel_get(&pdev->dev,
292 gab_chan_name[chan]);
anish kumare60fea72012-09-21 17:10:00 +0100293 if (IS_ERR(adc_bat->channel[chan])) {
294 ret = PTR_ERR(adc_bat->channel[chan]);
Dan Carpenter64d26f22013-02-14 10:26:43 +0300295 adc_bat->channel[chan] = NULL;
anish kumare60fea72012-09-21 17:10:00 +0100296 } else {
297 /* copying properties for supported channels only */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100298 memcpy(properties + sizeof(*(psy_desc->properties)) * index,
anish kumare60fea72012-09-21 17:10:00 +0100299 &gab_dyn_props[chan],
300 sizeof(gab_dyn_props[chan]));
301 index++;
302 }
303 }
304
305 /* none of the channels are supported so let's bail out */
Axel Lind211c6e2013-05-25 01:41:44 +0800306 if (index == 0) {
307 ret = -ENODEV;
anish kumare60fea72012-09-21 17:10:00 +0100308 goto second_mem_fail;
Axel Lind211c6e2013-05-25 01:41:44 +0800309 }
anish kumare60fea72012-09-21 17:10:00 +0100310
311 /*
312 * Total number of properties is equal to static properties
313 * plus the dynamic properties.Some properties may not be set
314 * as come channels may be not be supported by the device.So
315 * we need to take care of that.
316 */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100317 psy_desc->num_properties = ARRAY_SIZE(gab_props) + index;
anish kumare60fea72012-09-21 17:10:00 +0100318
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100319 adc_bat->psy = power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
320 if (IS_ERR(adc_bat->psy)) {
321 ret = PTR_ERR(adc_bat->psy);
anish kumare60fea72012-09-21 17:10:00 +0100322 goto err_reg_fail;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100323 }
anish kumare60fea72012-09-21 17:10:00 +0100324
325 INIT_DELAYED_WORK(&adc_bat->bat_work, gab_work);
326
327 if (gpio_is_valid(pdata->gpio_charge_finished)) {
328 int irq;
329 ret = gpio_request(pdata->gpio_charge_finished, "charged");
330 if (ret)
331 goto gpio_req_fail;
332
333 irq = gpio_to_irq(pdata->gpio_charge_finished);
334 ret = request_any_context_irq(irq, gab_charged,
335 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
336 "battery charged", adc_bat);
Axel Lina5af0922012-11-16 17:09:22 +0800337 if (ret < 0)
anish kumare60fea72012-09-21 17:10:00 +0100338 goto err_gpio;
339 }
340
341 platform_set_drvdata(pdev, adc_bat);
342
343 /* Schedule timer to check current status */
344 schedule_delayed_work(&adc_bat->bat_work,
345 msecs_to_jiffies(0));
346 return 0;
347
348err_gpio:
349 gpio_free(pdata->gpio_charge_finished);
350gpio_req_fail:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100351 power_supply_unregister(adc_bat->psy);
anish kumare60fea72012-09-21 17:10:00 +0100352err_reg_fail:
Dan Carpenter64d26f22013-02-14 10:26:43 +0300353 for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
354 if (adc_bat->channel[chan])
355 iio_channel_release(adc_bat->channel[chan]);
356 }
anish kumare60fea72012-09-21 17:10:00 +0100357second_mem_fail:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100358 kfree(psy_desc->properties);
anish kumare60fea72012-09-21 17:10:00 +0100359first_mem_fail:
360 return ret;
361}
362
Bill Pemberton415ec692012-11-19 13:26:07 -0500363static int gab_remove(struct platform_device *pdev)
anish kumare60fea72012-09-21 17:10:00 +0100364{
365 int chan;
366 struct gab *adc_bat = platform_get_drvdata(pdev);
367 struct gab_platform_data *pdata = adc_bat->pdata;
368
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100369 power_supply_unregister(adc_bat->psy);
anish kumare60fea72012-09-21 17:10:00 +0100370
371 if (gpio_is_valid(pdata->gpio_charge_finished)) {
372 free_irq(gpio_to_irq(pdata->gpio_charge_finished), adc_bat);
373 gpio_free(pdata->gpio_charge_finished);
374 }
375
Dan Carpenter64d26f22013-02-14 10:26:43 +0300376 for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
377 if (adc_bat->channel[chan])
378 iio_channel_release(adc_bat->channel[chan]);
379 }
anish kumare60fea72012-09-21 17:10:00 +0100380
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100381 kfree(adc_bat->psy_desc.properties);
anish kumare60fea72012-09-21 17:10:00 +0100382 cancel_delayed_work(&adc_bat->bat_work);
383 return 0;
384}
385
386#ifdef CONFIG_PM
387static int gab_suspend(struct device *dev)
388{
389 struct gab *adc_bat = dev_get_drvdata(dev);
390
391 cancel_delayed_work_sync(&adc_bat->bat_work);
392 adc_bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
393 return 0;
394}
395
396static int gab_resume(struct device *dev)
397{
398 struct gab *adc_bat = dev_get_drvdata(dev);
399 struct gab_platform_data *pdata = adc_bat->pdata;
400 int delay;
401
402 delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
403
404 /* Schedule timer to check current status */
405 schedule_delayed_work(&adc_bat->bat_work,
406 msecs_to_jiffies(delay));
407 return 0;
408}
409
410static const struct dev_pm_ops gab_pm_ops = {
411 .suspend = gab_suspend,
412 .resume = gab_resume,
413};
414
415#define GAB_PM_OPS (&gab_pm_ops)
416#else
417#define GAB_PM_OPS (NULL)
418#endif
419
420static struct platform_driver gab_driver = {
421 .driver = {
422 .name = "generic-adc-battery",
anish kumare60fea72012-09-21 17:10:00 +0100423 .pm = GAB_PM_OPS
424 },
425 .probe = gab_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500426 .remove = gab_remove,
anish kumare60fea72012-09-21 17:10:00 +0100427};
428module_platform_driver(gab_driver);
429
430MODULE_AUTHOR("anish kumar <anish198519851985@gmail.com>");
431MODULE_DESCRIPTION("generic battery driver using IIO");
432MODULE_LICENSE("GPL");