1/*
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 {
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;
52	int	level;
53	int	status;
54	bool cable_plugged;
55};
56
57static struct gab *to_generic_bat(struct power_supply *psy)
58{
59	return power_supply_get_drvdata(psy);
60}
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	else
142		*result *= 1000;
143
144	return ret;
145}
146
147static int gab_get_property(struct power_supply *psy,
148		enum power_supply_property psp, union power_supply_propval *val)
149{
150	struct gab *adc_bat;
151	struct gab_platform_data *pdata;
152	struct power_supply_info *bat_info;
153	int result = 0;
154	int ret = 0;
155
156	adc_bat = to_generic_bat(psy);
157	if (!adc_bat) {
158		dev_err(&psy->dev, "no battery infos ?!\n");
159		return -EINVAL;
160	}
161	pdata = adc_bat->pdata;
162	bat_info = &pdata->battery_info;
163
164	switch (psp) {
165	case POWER_SUPPLY_PROP_STATUS:
166		val->intval = gab_get_status(adc_bat);
167		break;
168	case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
169		val->intval = 0;
170		break;
171	case POWER_SUPPLY_PROP_CHARGE_NOW:
172		val->intval = pdata->cal_charge(result);
173		break;
174	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
175	case POWER_SUPPLY_PROP_CURRENT_NOW:
176	case POWER_SUPPLY_PROP_POWER_NOW:
177		ret = read_channel(adc_bat, psp, &result);
178		if (ret < 0)
179			goto err;
180		val->intval = result;
181		break;
182	case POWER_SUPPLY_PROP_TECHNOLOGY:
183		val->intval = bat_info->technology;
184		break;
185	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
186		val->intval = bat_info->voltage_min_design;
187		break;
188	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
189		val->intval = bat_info->voltage_max_design;
190		break;
191	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
192		val->intval = bat_info->charge_full_design;
193		break;
194	case POWER_SUPPLY_PROP_MODEL_NAME:
195		val->strval = bat_info->name;
196		break;
197	default:
198		return -EINVAL;
199	}
200err:
201	return ret;
202}
203
204static void gab_work(struct work_struct *work)
205{
206	struct gab *adc_bat;
207	struct delayed_work *delayed_work;
208	bool is_plugged;
209	int status;
210
211	delayed_work = to_delayed_work(work);
212	adc_bat = container_of(delayed_work, struct gab, bat_work);
213	status = adc_bat->status;
214
215	is_plugged = power_supply_am_i_supplied(adc_bat->psy);
216	adc_bat->cable_plugged = is_plugged;
217
218	if (!is_plugged)
219		adc_bat->status =  POWER_SUPPLY_STATUS_DISCHARGING;
220	else if (gab_charge_finished(adc_bat))
221		adc_bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
222	else
223		adc_bat->status = POWER_SUPPLY_STATUS_CHARGING;
224
225	if (status != adc_bat->status)
226		power_supply_changed(adc_bat->psy);
227}
228
229static irqreturn_t gab_charged(int irq, void *dev_id)
230{
231	struct gab *adc_bat = dev_id;
232	struct gab_platform_data *pdata = adc_bat->pdata;
233	int delay;
234
235	delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
236	schedule_delayed_work(&adc_bat->bat_work,
237			msecs_to_jiffies(delay));
238	return IRQ_HANDLED;
239}
240
241static int gab_probe(struct platform_device *pdev)
242{
243	struct gab *adc_bat;
244	struct power_supply_desc *psy_desc;
245	struct power_supply_config psy_cfg = {};
246	struct gab_platform_data *pdata = pdev->dev.platform_data;
247	enum power_supply_property *properties;
248	int ret = 0;
249	int chan;
250	int index = ARRAY_SIZE(gab_props);
251	bool any = false;
252
253	adc_bat = devm_kzalloc(&pdev->dev, sizeof(*adc_bat), GFP_KERNEL);
254	if (!adc_bat) {
255		dev_err(&pdev->dev, "failed to allocate memory\n");
256		return -ENOMEM;
257	}
258
259	psy_cfg.drv_data = adc_bat;
260	psy_desc = &adc_bat->psy_desc;
261	psy_desc->name = pdata->battery_info.name;
262
263	/* bootup default values for the battery */
264	adc_bat->cable_plugged = false;
265	adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
266	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
267	psy_desc->get_property = gab_get_property;
268	psy_desc->external_power_changed = gab_ext_power_changed;
269	adc_bat->pdata = pdata;
270
271	/*
272	 * copying the static properties and allocating extra memory for holding
273	 * the extra configurable properties received from platform data.
274	 */
275	properties = kcalloc(ARRAY_SIZE(gab_props) +
276			     ARRAY_SIZE(gab_chan_name),
277			     sizeof(*properties),
278			     GFP_KERNEL);
279	if (!properties) {
280		ret = -ENOMEM;
281		goto first_mem_fail;
282	}
283
284	memcpy(properties, gab_props, sizeof(gab_props));
285
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++) {
291		adc_bat->channel[chan] = iio_channel_get(&pdev->dev,
292							 gab_chan_name[chan]);
293		if (IS_ERR(adc_bat->channel[chan])) {
294			ret = PTR_ERR(adc_bat->channel[chan]);
295			adc_bat->channel[chan] = NULL;
296		} else {
297			/* copying properties for supported channels only */
298			int index2;
299
300			for (index2 = 0; index2 < index; index2++) {
301				if (properties[index2] == gab_dyn_props[chan])
302					break;	/* already known */
303			}
304			if (index2 == index)	/* really new */
305				properties[index++] = gab_dyn_props[chan];
306			any = true;
307		}
308	}
309
310	/* none of the channels are supported so let's bail out */
311	if (!any) {
312		ret = -ENODEV;
313		goto second_mem_fail;
314	}
315
316	/*
317	 * Total number of properties is equal to static properties
318	 * plus the dynamic properties.Some properties may not be set
319	 * as come channels may be not be supported by the device.So
320	 * we need to take care of that.
321	 */
322	psy_desc->properties = properties;
323	psy_desc->num_properties = index;
324
325	adc_bat->psy = power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
326	if (IS_ERR(adc_bat->psy)) {
327		ret = PTR_ERR(adc_bat->psy);
328		goto err_reg_fail;
329	}
330
331	INIT_DELAYED_WORK(&adc_bat->bat_work, gab_work);
332
333	if (gpio_is_valid(pdata->gpio_charge_finished)) {
334		int irq;
335		ret = gpio_request(pdata->gpio_charge_finished, "charged");
336		if (ret)
337			goto gpio_req_fail;
338
339		irq = gpio_to_irq(pdata->gpio_charge_finished);
340		ret = request_any_context_irq(irq, gab_charged,
341				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
342				"battery charged", adc_bat);
343		if (ret < 0)
344			goto err_gpio;
345	}
346
347	platform_set_drvdata(pdev, adc_bat);
348
349	/* Schedule timer to check current status */
350	schedule_delayed_work(&adc_bat->bat_work,
351			msecs_to_jiffies(0));
352	return 0;
353
354err_gpio:
355	gpio_free(pdata->gpio_charge_finished);
356gpio_req_fail:
357	power_supply_unregister(adc_bat->psy);
358err_reg_fail:
359	for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
360		if (adc_bat->channel[chan])
361			iio_channel_release(adc_bat->channel[chan]);
362	}
363second_mem_fail:
364	kfree(properties);
365first_mem_fail:
366	return ret;
367}
368
369static int gab_remove(struct platform_device *pdev)
370{
371	int chan;
372	struct gab *adc_bat = platform_get_drvdata(pdev);
373	struct gab_platform_data *pdata = adc_bat->pdata;
374
375	power_supply_unregister(adc_bat->psy);
376
377	if (gpio_is_valid(pdata->gpio_charge_finished)) {
378		free_irq(gpio_to_irq(pdata->gpio_charge_finished), adc_bat);
379		gpio_free(pdata->gpio_charge_finished);
380	}
381
382	for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
383		if (adc_bat->channel[chan])
384			iio_channel_release(adc_bat->channel[chan]);
385	}
386
387	kfree(adc_bat->psy_desc.properties);
388	cancel_delayed_work_sync(&adc_bat->bat_work);
389	return 0;
390}
391
392static int __maybe_unused gab_suspend(struct device *dev)
393{
394	struct gab *adc_bat = dev_get_drvdata(dev);
395
396	cancel_delayed_work_sync(&adc_bat->bat_work);
397	adc_bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
398	return 0;
399}
400
401static int __maybe_unused gab_resume(struct device *dev)
402{
403	struct gab *adc_bat = dev_get_drvdata(dev);
404	struct gab_platform_data *pdata = adc_bat->pdata;
405	int delay;
406
407	delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
408
409	/* Schedule timer to check current status */
410	schedule_delayed_work(&adc_bat->bat_work,
411			msecs_to_jiffies(delay));
412	return 0;
413}
414
415static SIMPLE_DEV_PM_OPS(gab_pm_ops, gab_suspend, gab_resume);
416
417static struct platform_driver gab_driver = {
418	.driver		= {
419		.name	= "generic-adc-battery",
420		.pm	= &gab_pm_ops,
421	},
422	.probe		= gab_probe,
423	.remove		= gab_remove,
424};
425module_platform_driver(gab_driver);
426
427MODULE_AUTHOR("anish kumar <anish198519851985@gmail.com>");
428MODULE_DESCRIPTION("generic battery driver using IIO");
429MODULE_LICENSE("GPL");
430