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
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
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>
27 #define JITTER_DEFAULT 10 /* hope 10ms is enough */
37 * gab_chan_name suggests the standard channel names for commonly used
40 static const char *const gab_chan_name
[] = {
41 [GAB_VOLTAGE
] = "voltage",
42 [GAB_CURRENT
] = "current",
43 [GAB_POWER
] = "power",
47 struct power_supply psy
;
48 struct iio_channel
*channel
[GAB_MAX_CHAN_TYPE
];
49 struct gab_platform_data
*pdata
;
50 struct delayed_work bat_work
;
56 static struct gab
*to_generic_bat(struct power_supply
*psy
)
58 return container_of(psy
, struct gab
, psy
);
61 static void gab_ext_power_changed(struct power_supply
*psy
)
63 struct gab
*adc_bat
= to_generic_bat(psy
);
65 schedule_delayed_work(&adc_bat
->bat_work
, msecs_to_jiffies(0));
68 static const enum power_supply_property gab_props
[] = {
69 POWER_SUPPLY_PROP_STATUS
,
70 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
71 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
,
72 POWER_SUPPLY_PROP_CHARGE_NOW
,
73 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
74 POWER_SUPPLY_PROP_CURRENT_NOW
,
75 POWER_SUPPLY_PROP_TECHNOLOGY
,
76 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
77 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
78 POWER_SUPPLY_PROP_MODEL_NAME
,
82 * This properties are set based on the received platform data and this
83 * should correspond one-to-one with enum chan_type.
85 static const enum power_supply_property gab_dyn_props
[] = {
86 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
87 POWER_SUPPLY_PROP_CURRENT_NOW
,
88 POWER_SUPPLY_PROP_POWER_NOW
,
91 static bool gab_charge_finished(struct gab
*adc_bat
)
93 struct gab_platform_data
*pdata
= adc_bat
->pdata
;
94 bool ret
= gpio_get_value(pdata
->gpio_charge_finished
);
95 bool inv
= pdata
->gpio_inverted
;
97 if (!gpio_is_valid(pdata
->gpio_charge_finished
))
102 static int gab_get_status(struct gab
*adc_bat
)
104 struct gab_platform_data
*pdata
= adc_bat
->pdata
;
105 struct power_supply_info
*bat_info
;
107 bat_info
= &pdata
->battery_info
;
108 if (adc_bat
->level
== bat_info
->charge_full_design
)
109 return POWER_SUPPLY_STATUS_FULL
;
110 return adc_bat
->status
;
113 static enum gab_chan_type
gab_prop_to_chan(enum power_supply_property psp
)
116 case POWER_SUPPLY_PROP_POWER_NOW
:
118 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
120 case POWER_SUPPLY_PROP_CURRENT_NOW
:
129 static int read_channel(struct gab
*adc_bat
, enum power_supply_property psp
,
135 chan_index
= gab_prop_to_chan(psp
);
136 ret
= iio_read_channel_processed(adc_bat
->channel
[chan_index
],
139 pr_err("read channel error\n");
143 static int gab_get_property(struct power_supply
*psy
,
144 enum power_supply_property psp
, union power_supply_propval
*val
)
147 struct gab_platform_data
*pdata
;
148 struct power_supply_info
*bat_info
;
152 adc_bat
= to_generic_bat(psy
);
154 dev_err(psy
->dev
, "no battery infos ?!\n");
157 pdata
= adc_bat
->pdata
;
158 bat_info
= &pdata
->battery_info
;
161 case POWER_SUPPLY_PROP_STATUS
:
162 gab_get_status(adc_bat
);
164 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
:
167 case POWER_SUPPLY_PROP_CHARGE_NOW
:
168 val
->intval
= pdata
->cal_charge(result
);
170 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
171 case POWER_SUPPLY_PROP_CURRENT_NOW
:
172 case POWER_SUPPLY_PROP_POWER_NOW
:
173 ret
= read_channel(adc_bat
, psp
, &result
);
176 val
->intval
= result
;
178 case POWER_SUPPLY_PROP_TECHNOLOGY
:
179 val
->intval
= bat_info
->technology
;
181 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
182 val
->intval
= bat_info
->voltage_min_design
;
184 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
185 val
->intval
= bat_info
->voltage_max_design
;
187 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
188 val
->intval
= bat_info
->charge_full_design
;
190 case POWER_SUPPLY_PROP_MODEL_NAME
:
191 val
->strval
= bat_info
->name
;
200 static void gab_work(struct work_struct
*work
)
203 struct gab_platform_data
*pdata
;
204 struct delayed_work
*delayed_work
;
208 delayed_work
= container_of(work
, struct delayed_work
, work
);
209 adc_bat
= container_of(delayed_work
, struct gab
, bat_work
);
210 pdata
= adc_bat
->pdata
;
211 status
= adc_bat
->status
;
213 is_plugged
= power_supply_am_i_supplied(&adc_bat
->psy
);
214 adc_bat
->cable_plugged
= is_plugged
;
217 adc_bat
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
218 else if (gab_charge_finished(adc_bat
))
219 adc_bat
->status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
221 adc_bat
->status
= POWER_SUPPLY_STATUS_CHARGING
;
223 if (status
!= adc_bat
->status
)
224 power_supply_changed(&adc_bat
->psy
);
227 static irqreturn_t
gab_charged(int irq
, void *dev_id
)
229 struct gab
*adc_bat
= dev_id
;
230 struct gab_platform_data
*pdata
= adc_bat
->pdata
;
233 delay
= pdata
->jitter_delay
? pdata
->jitter_delay
: JITTER_DEFAULT
;
234 schedule_delayed_work(&adc_bat
->bat_work
,
235 msecs_to_jiffies(delay
));
239 static int gab_probe(struct platform_device
*pdev
)
242 struct power_supply
*psy
;
243 struct gab_platform_data
*pdata
= pdev
->dev
.platform_data
;
244 enum power_supply_property
*properties
;
249 adc_bat
= devm_kzalloc(&pdev
->dev
, sizeof(*adc_bat
), GFP_KERNEL
);
251 dev_err(&pdev
->dev
, "failed to allocate memory\n");
256 psy
->name
= pdata
->battery_info
.name
;
258 /* bootup default values for the battery */
259 adc_bat
->cable_plugged
= false;
260 adc_bat
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
261 psy
->type
= POWER_SUPPLY_TYPE_BATTERY
;
262 psy
->get_property
= gab_get_property
;
263 psy
->external_power_changed
= gab_ext_power_changed
;
264 adc_bat
->pdata
= pdata
;
267 * copying the static properties and allocating extra memory for holding
268 * the extra configurable properties received from platform data.
270 psy
->properties
= kcalloc(ARRAY_SIZE(gab_props
) +
271 ARRAY_SIZE(gab_chan_name
),
272 sizeof(*psy
->properties
), GFP_KERNEL
);
273 if (!psy
->properties
) {
278 memcpy(psy
->properties
, gab_props
, sizeof(gab_props
));
279 properties
= (enum power_supply_property
*)
280 ((char *)psy
->properties
+ sizeof(gab_props
));
283 * getting channel from iio and copying the battery properties
284 * based on the channel supported by consumer device.
286 for (chan
= 0; chan
< ARRAY_SIZE(gab_chan_name
); chan
++) {
287 adc_bat
->channel
[chan
] = iio_channel_get(&pdev
->dev
,
288 gab_chan_name
[chan
]);
289 if (IS_ERR(adc_bat
->channel
[chan
])) {
290 ret
= PTR_ERR(adc_bat
->channel
[chan
]);
291 adc_bat
->channel
[chan
] = NULL
;
293 /* copying properties for supported channels only */
294 memcpy(properties
+ sizeof(*(psy
->properties
)) * index
,
295 &gab_dyn_props
[chan
],
296 sizeof(gab_dyn_props
[chan
]));
301 /* none of the channels are supported so let's bail out */
304 goto second_mem_fail
;
308 * Total number of properties is equal to static properties
309 * plus the dynamic properties.Some properties may not be set
310 * as come channels may be not be supported by the device.So
311 * we need to take care of that.
313 psy
->num_properties
= ARRAY_SIZE(gab_props
) + index
;
315 ret
= power_supply_register(&pdev
->dev
, psy
);
319 INIT_DELAYED_WORK(&adc_bat
->bat_work
, gab_work
);
321 if (gpio_is_valid(pdata
->gpio_charge_finished
)) {
323 ret
= gpio_request(pdata
->gpio_charge_finished
, "charged");
327 irq
= gpio_to_irq(pdata
->gpio_charge_finished
);
328 ret
= request_any_context_irq(irq
, gab_charged
,
329 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
330 "battery charged", adc_bat
);
335 platform_set_drvdata(pdev
, adc_bat
);
337 /* Schedule timer to check current status */
338 schedule_delayed_work(&adc_bat
->bat_work
,
339 msecs_to_jiffies(0));
343 gpio_free(pdata
->gpio_charge_finished
);
345 power_supply_unregister(psy
);
347 for (chan
= 0; chan
< ARRAY_SIZE(gab_chan_name
); chan
++) {
348 if (adc_bat
->channel
[chan
])
349 iio_channel_release(adc_bat
->channel
[chan
]);
352 kfree(psy
->properties
);
357 static int gab_remove(struct platform_device
*pdev
)
360 struct gab
*adc_bat
= platform_get_drvdata(pdev
);
361 struct gab_platform_data
*pdata
= adc_bat
->pdata
;
363 power_supply_unregister(&adc_bat
->psy
);
365 if (gpio_is_valid(pdata
->gpio_charge_finished
)) {
366 free_irq(gpio_to_irq(pdata
->gpio_charge_finished
), adc_bat
);
367 gpio_free(pdata
->gpio_charge_finished
);
370 for (chan
= 0; chan
< ARRAY_SIZE(gab_chan_name
); chan
++) {
371 if (adc_bat
->channel
[chan
])
372 iio_channel_release(adc_bat
->channel
[chan
]);
375 kfree(adc_bat
->psy
.properties
);
376 cancel_delayed_work(&adc_bat
->bat_work
);
381 static int gab_suspend(struct device
*dev
)
383 struct gab
*adc_bat
= dev_get_drvdata(dev
);
385 cancel_delayed_work_sync(&adc_bat
->bat_work
);
386 adc_bat
->status
= POWER_SUPPLY_STATUS_UNKNOWN
;
390 static int gab_resume(struct device
*dev
)
392 struct gab
*adc_bat
= dev_get_drvdata(dev
);
393 struct gab_platform_data
*pdata
= adc_bat
->pdata
;
396 delay
= pdata
->jitter_delay
? pdata
->jitter_delay
: JITTER_DEFAULT
;
398 /* Schedule timer to check current status */
399 schedule_delayed_work(&adc_bat
->bat_work
,
400 msecs_to_jiffies(delay
));
404 static const struct dev_pm_ops gab_pm_ops
= {
405 .suspend
= gab_suspend
,
406 .resume
= gab_resume
,
409 #define GAB_PM_OPS (&gab_pm_ops)
411 #define GAB_PM_OPS (NULL)
414 static struct platform_driver gab_driver
= {
416 .name
= "generic-adc-battery",
417 .owner
= THIS_MODULE
,
421 .remove
= gab_remove
,
423 module_platform_driver(gab_driver
);
425 MODULE_AUTHOR("anish kumar <anish198519851985@gmail.com>");
426 MODULE_DESCRIPTION("generic battery driver using IIO");
427 MODULE_LICENSE("GPL");