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/consumer.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 power_supply_desc psy_desc
;
49 struct iio_channel
*channel
[GAB_MAX_CHAN_TYPE
];
50 struct gab_platform_data
*pdata
;
51 struct delayed_work bat_work
;
55 struct gpio_desc
*charge_finished
;
58 static struct gab
*to_generic_bat(struct power_supply
*psy
)
60 return power_supply_get_drvdata(psy
);
63 static void gab_ext_power_changed(struct power_supply
*psy
)
65 struct gab
*adc_bat
= to_generic_bat(psy
);
67 schedule_delayed_work(&adc_bat
->bat_work
, msecs_to_jiffies(0));
70 static const enum power_supply_property gab_props
[] = {
71 POWER_SUPPLY_PROP_STATUS
,
72 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
73 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
,
74 POWER_SUPPLY_PROP_CHARGE_NOW
,
75 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
76 POWER_SUPPLY_PROP_CURRENT_NOW
,
77 POWER_SUPPLY_PROP_TECHNOLOGY
,
78 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
79 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
80 POWER_SUPPLY_PROP_MODEL_NAME
,
84 * This properties are set based on the received platform data and this
85 * should correspond one-to-one with enum chan_type.
87 static const enum power_supply_property gab_dyn_props
[] = {
88 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
89 POWER_SUPPLY_PROP_CURRENT_NOW
,
90 POWER_SUPPLY_PROP_POWER_NOW
,
93 static bool gab_charge_finished(struct gab
*adc_bat
)
95 if (!adc_bat
->charge_finished
)
97 return gpiod_get_value(adc_bat
->charge_finished
);
100 static int gab_get_status(struct gab
*adc_bat
)
102 struct gab_platform_data
*pdata
= adc_bat
->pdata
;
103 struct power_supply_info
*bat_info
;
105 bat_info
= &pdata
->battery_info
;
106 if (adc_bat
->level
== bat_info
->charge_full_design
)
107 return POWER_SUPPLY_STATUS_FULL
;
108 return adc_bat
->status
;
111 static enum gab_chan_type
gab_prop_to_chan(enum power_supply_property psp
)
114 case POWER_SUPPLY_PROP_POWER_NOW
:
116 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
118 case POWER_SUPPLY_PROP_CURRENT_NOW
:
127 static int read_channel(struct gab
*adc_bat
, enum power_supply_property psp
,
133 chan_index
= gab_prop_to_chan(psp
);
134 ret
= iio_read_channel_processed(adc_bat
->channel
[chan_index
],
137 pr_err("read channel error\n");
141 static int gab_get_property(struct power_supply
*psy
,
142 enum power_supply_property psp
, union power_supply_propval
*val
)
145 struct gab_platform_data
*pdata
;
146 struct power_supply_info
*bat_info
;
150 adc_bat
= to_generic_bat(psy
);
152 dev_err(&psy
->dev
, "no battery infos ?!\n");
155 pdata
= adc_bat
->pdata
;
156 bat_info
= &pdata
->battery_info
;
159 case POWER_SUPPLY_PROP_STATUS
:
160 val
->intval
= gab_get_status(adc_bat
);
162 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
:
165 case POWER_SUPPLY_PROP_CHARGE_NOW
:
166 val
->intval
= pdata
->cal_charge(result
);
168 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
169 case POWER_SUPPLY_PROP_CURRENT_NOW
:
170 case POWER_SUPPLY_PROP_POWER_NOW
:
171 ret
= read_channel(adc_bat
, psp
, &result
);
174 val
->intval
= result
;
176 case POWER_SUPPLY_PROP_TECHNOLOGY
:
177 val
->intval
= bat_info
->technology
;
179 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
180 val
->intval
= bat_info
->voltage_min_design
;
182 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
183 val
->intval
= bat_info
->voltage_max_design
;
185 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
186 val
->intval
= bat_info
->charge_full_design
;
188 case POWER_SUPPLY_PROP_MODEL_NAME
:
189 val
->strval
= bat_info
->name
;
198 static void gab_work(struct work_struct
*work
)
201 struct delayed_work
*delayed_work
;
205 delayed_work
= to_delayed_work(work
);
206 adc_bat
= container_of(delayed_work
, struct gab
, bat_work
);
207 status
= adc_bat
->status
;
209 is_plugged
= power_supply_am_i_supplied(adc_bat
->psy
);
210 adc_bat
->cable_plugged
= is_plugged
;
213 adc_bat
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
214 else if (gab_charge_finished(adc_bat
))
215 adc_bat
->status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
217 adc_bat
->status
= POWER_SUPPLY_STATUS_CHARGING
;
219 if (status
!= adc_bat
->status
)
220 power_supply_changed(adc_bat
->psy
);
223 static irqreturn_t
gab_charged(int irq
, void *dev_id
)
225 struct gab
*adc_bat
= dev_id
;
226 struct gab_platform_data
*pdata
= adc_bat
->pdata
;
229 delay
= pdata
->jitter_delay
? pdata
->jitter_delay
: JITTER_DEFAULT
;
230 schedule_delayed_work(&adc_bat
->bat_work
,
231 msecs_to_jiffies(delay
));
235 static int gab_probe(struct platform_device
*pdev
)
238 struct power_supply_desc
*psy_desc
;
239 struct power_supply_config psy_cfg
= {};
240 struct gab_platform_data
*pdata
= pdev
->dev
.platform_data
;
241 enum power_supply_property
*properties
;
244 int index
= ARRAY_SIZE(gab_props
);
247 adc_bat
= devm_kzalloc(&pdev
->dev
, sizeof(*adc_bat
), GFP_KERNEL
);
249 dev_err(&pdev
->dev
, "failed to allocate memory\n");
253 psy_cfg
.drv_data
= adc_bat
;
254 psy_desc
= &adc_bat
->psy_desc
;
255 psy_desc
->name
= pdata
->battery_info
.name
;
257 /* bootup default values for the battery */
258 adc_bat
->cable_plugged
= false;
259 adc_bat
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
260 psy_desc
->type
= POWER_SUPPLY_TYPE_BATTERY
;
261 psy_desc
->get_property
= gab_get_property
;
262 psy_desc
->external_power_changed
= gab_ext_power_changed
;
263 adc_bat
->pdata
= pdata
;
266 * copying the static properties and allocating extra memory for holding
267 * the extra configurable properties received from platform data.
269 properties
= kcalloc(ARRAY_SIZE(gab_props
) +
270 ARRAY_SIZE(gab_chan_name
),
278 memcpy(properties
, gab_props
, sizeof(gab_props
));
281 * getting channel from iio and copying the battery properties
282 * based on the channel supported by consumer device.
284 for (chan
= 0; chan
< ARRAY_SIZE(gab_chan_name
); chan
++) {
285 adc_bat
->channel
[chan
] = iio_channel_get(&pdev
->dev
,
286 gab_chan_name
[chan
]);
287 if (IS_ERR(adc_bat
->channel
[chan
])) {
288 ret
= PTR_ERR(adc_bat
->channel
[chan
]);
289 adc_bat
->channel
[chan
] = NULL
;
291 /* copying properties for supported channels only */
294 for (index2
= 0; index2
< index
; index2
++) {
295 if (properties
[index2
] == gab_dyn_props
[chan
])
296 break; /* already known */
298 if (index2
== index
) /* really new */
299 properties
[index
++] = gab_dyn_props
[chan
];
304 /* none of the channels are supported so let's bail out */
307 goto second_mem_fail
;
311 * Total number of properties is equal to static properties
312 * plus the dynamic properties.Some properties may not be set
313 * as come channels may be not be supported by the device.So
314 * we need to take care of that.
316 psy_desc
->properties
= properties
;
317 psy_desc
->num_properties
= index
;
319 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
);
325 INIT_DELAYED_WORK(&adc_bat
->bat_work
, gab_work
);
327 adc_bat
->charge_finished
= devm_gpiod_get_optional(&pdev
->dev
,
328 "charged", GPIOD_IN
);
329 if (adc_bat
->charge_finished
) {
332 irq
= gpiod_to_irq(adc_bat
->charge_finished
);
333 ret
= request_any_context_irq(irq
, gab_charged
,
334 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
335 "battery charged", adc_bat
);
340 platform_set_drvdata(pdev
, adc_bat
);
342 /* Schedule timer to check current status */
343 schedule_delayed_work(&adc_bat
->bat_work
,
344 msecs_to_jiffies(0));
348 power_supply_unregister(adc_bat
->psy
);
350 for (chan
= 0; chan
< ARRAY_SIZE(gab_chan_name
); chan
++) {
351 if (adc_bat
->channel
[chan
])
352 iio_channel_release(adc_bat
->channel
[chan
]);
360 static int gab_remove(struct platform_device
*pdev
)
363 struct gab
*adc_bat
= platform_get_drvdata(pdev
);
365 power_supply_unregister(adc_bat
->psy
);
367 if (adc_bat
->charge_finished
)
368 free_irq(gpiod_to_irq(adc_bat
->charge_finished
), adc_bat
);
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_desc
.properties
);
376 cancel_delayed_work(&adc_bat
->bat_work
);
380 static int __maybe_unused
gab_suspend(struct device
*dev
)
382 struct gab
*adc_bat
= dev_get_drvdata(dev
);
384 cancel_delayed_work_sync(&adc_bat
->bat_work
);
385 adc_bat
->status
= POWER_SUPPLY_STATUS_UNKNOWN
;
389 static int __maybe_unused
gab_resume(struct device
*dev
)
391 struct gab
*adc_bat
= dev_get_drvdata(dev
);
392 struct gab_platform_data
*pdata
= adc_bat
->pdata
;
395 delay
= pdata
->jitter_delay
? pdata
->jitter_delay
: JITTER_DEFAULT
;
397 /* Schedule timer to check current status */
398 schedule_delayed_work(&adc_bat
->bat_work
,
399 msecs_to_jiffies(delay
));
403 static SIMPLE_DEV_PM_OPS(gab_pm_ops
, gab_suspend
, gab_resume
);
405 static struct platform_driver gab_driver
= {
407 .name
= "generic-adc-battery",
411 .remove
= gab_remove
,
413 module_platform_driver(gab_driver
);
415 MODULE_AUTHOR("anish kumar <anish198519851985@gmail.com>");
416 MODULE_DESCRIPTION("generic battery driver using IIO");
417 MODULE_LICENSE("GPL");