Blame view

drivers/power/generic-adc-battery.c 10.9 KB
e60fea794   anish kumar   power: battery: G...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  /*
   * Generic battery driver code using IIO
   * Copyright (C) 2012, Anish Kumar <anish198519851985@gmail.com>
   * based on jz4740-battery.c
   * based on s3c_adc_battery.c
   *
   * This file is subject to the terms and conditions of the GNU General Public
   * License.  See the file COPYING in the main directory of this archive for
   * more details.
   *
   */
  #include <linux/interrupt.h>
  #include <linux/platform_device.h>
  #include <linux/power_supply.h>
  #include <linux/gpio.h>
  #include <linux/err.h>
  #include <linux/timer.h>
  #include <linux/jiffies.h>
  #include <linux/errno.h>
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/slab.h>
  #include <linux/iio/consumer.h>
  #include <linux/iio/types.h>
  #include <linux/power/generic-adc-battery.h>
  
  #define JITTER_DEFAULT 10 /* hope 10ms is enough */
  
  enum gab_chan_type {
  	GAB_VOLTAGE = 0,
  	GAB_CURRENT,
  	GAB_POWER,
  	GAB_MAX_CHAN_TYPE
  };
  
  /*
   * gab_chan_name suggests the standard channel names for commonly used
   * channel types.
   */
  static const char *const gab_chan_name[] = {
  	[GAB_VOLTAGE]	= "voltage",
  	[GAB_CURRENT]	= "current",
  	[GAB_POWER]		= "power",
  };
  
  struct gab {
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
47
48
  	struct power_supply		*psy;
  	struct power_supply_desc	psy_desc;
e60fea794   anish kumar   power: battery: G...
49
50
51
52
53
54
55
56
57
58
  	struct iio_channel	*channel[GAB_MAX_CHAN_TYPE];
  	struct gab_platform_data	*pdata;
  	struct delayed_work bat_work;
  	int	level;
  	int	status;
  	bool cable_plugged;
  };
  
  static struct gab *to_generic_bat(struct power_supply *psy)
  {
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
59
  	return power_supply_get_drvdata(psy);
e60fea794   anish kumar   power: battery: G...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  }
  
  static void gab_ext_power_changed(struct power_supply *psy)
  {
  	struct gab *adc_bat = to_generic_bat(psy);
  
  	schedule_delayed_work(&adc_bat->bat_work, msecs_to_jiffies(0));
  }
  
  static const enum power_supply_property gab_props[] = {
  	POWER_SUPPLY_PROP_STATUS,
  	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  	POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
  	POWER_SUPPLY_PROP_CHARGE_NOW,
  	POWER_SUPPLY_PROP_VOLTAGE_NOW,
  	POWER_SUPPLY_PROP_CURRENT_NOW,
  	POWER_SUPPLY_PROP_TECHNOLOGY,
  	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  	POWER_SUPPLY_PROP_MODEL_NAME,
  };
  
  /*
   * This properties are set based on the received platform data and this
   * should correspond one-to-one with enum chan_type.
   */
  static const enum power_supply_property gab_dyn_props[] = {
  	POWER_SUPPLY_PROP_VOLTAGE_NOW,
  	POWER_SUPPLY_PROP_CURRENT_NOW,
  	POWER_SUPPLY_PROP_POWER_NOW,
  };
  
  static bool gab_charge_finished(struct gab *adc_bat)
  {
  	struct gab_platform_data *pdata = adc_bat->pdata;
  	bool ret = gpio_get_value(pdata->gpio_charge_finished);
  	bool inv = pdata->gpio_inverted;
  
  	if (!gpio_is_valid(pdata->gpio_charge_finished))
  		return false;
  	return ret ^ inv;
  }
  
  static int gab_get_status(struct gab *adc_bat)
  {
  	struct gab_platform_data *pdata = adc_bat->pdata;
  	struct power_supply_info *bat_info;
  
  	bat_info = &pdata->battery_info;
  	if (adc_bat->level == bat_info->charge_full_design)
  		return POWER_SUPPLY_STATUS_FULL;
  	return adc_bat->status;
  }
  
  static enum gab_chan_type gab_prop_to_chan(enum power_supply_property psp)
  {
  	switch (psp) {
  	case POWER_SUPPLY_PROP_POWER_NOW:
  		return GAB_POWER;
  	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  		return GAB_VOLTAGE;
  	case POWER_SUPPLY_PROP_CURRENT_NOW:
  		return GAB_CURRENT;
  	default:
  		WARN_ON(1);
  		break;
  	}
  	return GAB_POWER;
  }
  
  static int read_channel(struct gab *adc_bat, enum power_supply_property psp,
  		int *result)
  {
  	int ret;
  	int chan_index;
  
  	chan_index = gab_prop_to_chan(psp);
  	ret = iio_read_channel_processed(adc_bat->channel[chan_index],
  			result);
  	if (ret < 0)
  		pr_err("read channel error
  ");
  	return ret;
  }
  
  static int gab_get_property(struct power_supply *psy,
  		enum power_supply_property psp, union power_supply_propval *val)
  {
  	struct gab *adc_bat;
  	struct gab_platform_data *pdata;
  	struct power_supply_info *bat_info;
  	int result = 0;
  	int ret = 0;
  
  	adc_bat = to_generic_bat(psy);
  	if (!adc_bat) {
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
156
157
  		dev_err(&psy->dev, "no battery infos ?!
  ");
e60fea794   anish kumar   power: battery: G...
158
159
160
161
162
163
164
  		return -EINVAL;
  	}
  	pdata = adc_bat->pdata;
  	bat_info = &pdata->battery_info;
  
  	switch (psp) {
  	case POWER_SUPPLY_PROP_STATUS:
0595439a0   Nicolas Saenz Julienne   power: generic-ad...
165
  		val->intval = gab_get_status(adc_bat);
e60fea794   anish kumar   power: battery: G...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  		break;
  	case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
  		val->intval = 0;
  		break;
  	case POWER_SUPPLY_PROP_CHARGE_NOW:
  		val->intval = pdata->cal_charge(result);
  		break;
  	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  	case POWER_SUPPLY_PROP_CURRENT_NOW:
  	case POWER_SUPPLY_PROP_POWER_NOW:
  		ret = read_channel(adc_bat, psp, &result);
  		if (ret < 0)
  			goto err;
  		val->intval = result;
  		break;
  	case POWER_SUPPLY_PROP_TECHNOLOGY:
  		val->intval = bat_info->technology;
  		break;
  	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  		val->intval = bat_info->voltage_min_design;
  		break;
  	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  		val->intval = bat_info->voltage_max_design;
  		break;
  	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  		val->intval = bat_info->charge_full_design;
  		break;
  	case POWER_SUPPLY_PROP_MODEL_NAME:
  		val->strval = bat_info->name;
  		break;
  	default:
  		return -EINVAL;
  	}
  err:
  	return ret;
  }
  
  static void gab_work(struct work_struct *work)
  {
  	struct gab *adc_bat;
  	struct gab_platform_data *pdata;
  	struct delayed_work *delayed_work;
  	bool is_plugged;
  	int status;
127d28684   Geliang Tang   power: generic-ad...
210
  	delayed_work = to_delayed_work(work);
e60fea794   anish kumar   power: battery: G...
211
212
213
  	adc_bat = container_of(delayed_work, struct gab, bat_work);
  	pdata = adc_bat->pdata;
  	status = adc_bat->status;
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
214
  	is_plugged = power_supply_am_i_supplied(adc_bat->psy);
e60fea794   anish kumar   power: battery: G...
215
216
217
218
219
220
221
222
223
224
  	adc_bat->cable_plugged = is_plugged;
  
  	if (!is_plugged)
  		adc_bat->status =  POWER_SUPPLY_STATUS_DISCHARGING;
  	else if (gab_charge_finished(adc_bat))
  		adc_bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  	else
  		adc_bat->status = POWER_SUPPLY_STATUS_CHARGING;
  
  	if (status != adc_bat->status)
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
225
  		power_supply_changed(adc_bat->psy);
e60fea794   anish kumar   power: battery: G...
226
227
228
229
230
231
232
233
234
235
236
237
238
  }
  
  static irqreturn_t gab_charged(int irq, void *dev_id)
  {
  	struct gab *adc_bat = dev_id;
  	struct gab_platform_data *pdata = adc_bat->pdata;
  	int delay;
  
  	delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
  	schedule_delayed_work(&adc_bat->bat_work,
  			msecs_to_jiffies(delay));
  	return IRQ_HANDLED;
  }
c8afa6406   Bill Pemberton   power: remove use...
239
  static int gab_probe(struct platform_device *pdev)
e60fea794   anish kumar   power: battery: G...
240
241
  {
  	struct gab *adc_bat;
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
242
243
  	struct power_supply_desc *psy_desc;
  	struct power_supply_config psy_cfg = {};
e60fea794   anish kumar   power: battery: G...
244
245
246
247
248
249
250
251
252
253
254
255
  	struct gab_platform_data *pdata = pdev->dev.platform_data;
  	enum power_supply_property *properties;
  	int ret = 0;
  	int chan;
  	int index = 0;
  
  	adc_bat = devm_kzalloc(&pdev->dev, sizeof(*adc_bat), GFP_KERNEL);
  	if (!adc_bat) {
  		dev_err(&pdev->dev, "failed to allocate memory
  ");
  		return -ENOMEM;
  	}
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
256
257
258
  	psy_cfg.drv_data = adc_bat;
  	psy_desc = &adc_bat->psy_desc;
  	psy_desc->name = pdata->battery_info.name;
e60fea794   anish kumar   power: battery: G...
259
260
261
262
  
  	/* bootup default values for the battery */
  	adc_bat->cable_plugged = false;
  	adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
263
264
265
  	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  	psy_desc->get_property = gab_get_property;
  	psy_desc->external_power_changed = gab_ext_power_changed;
e60fea794   anish kumar   power: battery: G...
266
  	adc_bat->pdata = pdata;
e60fea794   anish kumar   power: battery: G...
267
268
269
270
  	/*
  	 * copying the static properties and allocating extra memory for holding
  	 * the extra configurable properties received from platform data.
  	 */
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
271
  	psy_desc->properties = kcalloc(ARRAY_SIZE(gab_props) +
e60fea794   anish kumar   power: battery: G...
272
  					ARRAY_SIZE(gab_chan_name),
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
273
274
275
  					sizeof(*psy_desc->properties),
  					GFP_KERNEL);
  	if (!psy_desc->properties) {
e60fea794   anish kumar   power: battery: G...
276
277
278
  		ret = -ENOMEM;
  		goto first_mem_fail;
  	}
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
279
  	memcpy(psy_desc->properties, gab_props, sizeof(gab_props));
a77d60aec   Dan Carpenter   generic-adc-batte...
280
  	properties = (enum power_supply_property *)
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
281
  			((char *)psy_desc->properties + sizeof(gab_props));
e60fea794   anish kumar   power: battery: G...
282
283
284
285
286
287
  
  	/*
  	 * getting channel from iio and copying the battery properties
  	 * based on the channel supported by consumer device.
  	 */
  	for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
5aa57f0a6   Guenter Roeck   iio: Update iio_c...
288
289
  		adc_bat->channel[chan] = iio_channel_get(&pdev->dev,
  							 gab_chan_name[chan]);
e60fea794   anish kumar   power: battery: G...
290
291
  		if (IS_ERR(adc_bat->channel[chan])) {
  			ret = PTR_ERR(adc_bat->channel[chan]);
64d26f225   Dan Carpenter   generic-adc-batte...
292
  			adc_bat->channel[chan] = NULL;
e60fea794   anish kumar   power: battery: G...
293
294
  		} else {
  			/* copying properties for supported channels only */
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
295
  			memcpy(properties + sizeof(*(psy_desc->properties)) * index,
e60fea794   anish kumar   power: battery: G...
296
297
298
299
300
301
302
  					&gab_dyn_props[chan],
  					sizeof(gab_dyn_props[chan]));
  			index++;
  		}
  	}
  
  	/* none of the channels are supported so let's bail out */
d211c6e82   Axel Lin   generic-adc-batte...
303
304
  	if (index == 0) {
  		ret = -ENODEV;
e60fea794   anish kumar   power: battery: G...
305
  		goto second_mem_fail;
d211c6e82   Axel Lin   generic-adc-batte...
306
  	}
e60fea794   anish kumar   power: battery: G...
307
308
309
310
311
312
313
  
  	/*
  	 * Total number of properties is equal to static properties
  	 * plus the dynamic properties.Some properties may not be set
  	 * as come channels may be not be supported by the device.So
  	 * we need to take care of that.
  	 */
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
314
  	psy_desc->num_properties = ARRAY_SIZE(gab_props) + index;
e60fea794   anish kumar   power: battery: G...
315

297d716f6   Krzysztof Kozlowski   power_supply: Cha...
316
317
318
  	adc_bat->psy = power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
  	if (IS_ERR(adc_bat->psy)) {
  		ret = PTR_ERR(adc_bat->psy);
e60fea794   anish kumar   power: battery: G...
319
  		goto err_reg_fail;
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
320
  	}
e60fea794   anish kumar   power: battery: G...
321
322
323
324
325
326
327
328
329
330
331
332
333
  
  	INIT_DELAYED_WORK(&adc_bat->bat_work, gab_work);
  
  	if (gpio_is_valid(pdata->gpio_charge_finished)) {
  		int irq;
  		ret = gpio_request(pdata->gpio_charge_finished, "charged");
  		if (ret)
  			goto gpio_req_fail;
  
  		irq = gpio_to_irq(pdata->gpio_charge_finished);
  		ret = request_any_context_irq(irq, gab_charged,
  				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  				"battery charged", adc_bat);
a5af09224   Axel Lin   generic-adc-batte...
334
  		if (ret < 0)
e60fea794   anish kumar   power: battery: G...
335
336
337
338
339
340
341
342
343
344
345
346
347
  			goto err_gpio;
  	}
  
  	platform_set_drvdata(pdev, adc_bat);
  
  	/* Schedule timer to check current status */
  	schedule_delayed_work(&adc_bat->bat_work,
  			msecs_to_jiffies(0));
  	return 0;
  
  err_gpio:
  	gpio_free(pdata->gpio_charge_finished);
  gpio_req_fail:
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
348
  	power_supply_unregister(adc_bat->psy);
e60fea794   anish kumar   power: battery: G...
349
  err_reg_fail:
64d26f225   Dan Carpenter   generic-adc-batte...
350
351
352
353
  	for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  		if (adc_bat->channel[chan])
  			iio_channel_release(adc_bat->channel[chan]);
  	}
e60fea794   anish kumar   power: battery: G...
354
  second_mem_fail:
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
355
  	kfree(psy_desc->properties);
e60fea794   anish kumar   power: battery: G...
356
357
358
  first_mem_fail:
  	return ret;
  }
415ec69fb   Bill Pemberton   power: remove use...
359
  static int gab_remove(struct platform_device *pdev)
e60fea794   anish kumar   power: battery: G...
360
361
362
363
  {
  	int chan;
  	struct gab *adc_bat = platform_get_drvdata(pdev);
  	struct gab_platform_data *pdata = adc_bat->pdata;
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
364
  	power_supply_unregister(adc_bat->psy);
e60fea794   anish kumar   power: battery: G...
365
366
367
368
369
  
  	if (gpio_is_valid(pdata->gpio_charge_finished)) {
  		free_irq(gpio_to_irq(pdata->gpio_charge_finished), adc_bat);
  		gpio_free(pdata->gpio_charge_finished);
  	}
64d26f225   Dan Carpenter   generic-adc-batte...
370
371
372
373
  	for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  		if (adc_bat->channel[chan])
  			iio_channel_release(adc_bat->channel[chan]);
  	}
e60fea794   anish kumar   power: battery: G...
374

297d716f6   Krzysztof Kozlowski   power_supply: Cha...
375
  	kfree(adc_bat->psy_desc.properties);
e60fea794   anish kumar   power: battery: G...
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
  	cancel_delayed_work(&adc_bat->bat_work);
  	return 0;
  }
  
  #ifdef CONFIG_PM
  static int gab_suspend(struct device *dev)
  {
  	struct gab *adc_bat = dev_get_drvdata(dev);
  
  	cancel_delayed_work_sync(&adc_bat->bat_work);
  	adc_bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
  	return 0;
  }
  
  static int gab_resume(struct device *dev)
  {
  	struct gab *adc_bat = dev_get_drvdata(dev);
  	struct gab_platform_data *pdata = adc_bat->pdata;
  	int delay;
  
  	delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
  
  	/* Schedule timer to check current status */
  	schedule_delayed_work(&adc_bat->bat_work,
  			msecs_to_jiffies(delay));
  	return 0;
  }
  
  static const struct dev_pm_ops gab_pm_ops = {
  	.suspend        = gab_suspend,
  	.resume         = gab_resume,
  };
  
  #define GAB_PM_OPS       (&gab_pm_ops)
  #else
  #define GAB_PM_OPS       (NULL)
  #endif
  
  static struct platform_driver gab_driver = {
  	.driver		= {
  		.name	= "generic-adc-battery",
e60fea794   anish kumar   power: battery: G...
417
418
419
  		.pm	= GAB_PM_OPS
  	},
  	.probe		= gab_probe,
28ea73f4c   Bill Pemberton   power: remove use...
420
  	.remove		= gab_remove,
e60fea794   anish kumar   power: battery: G...
421
422
423
424
425
426
  };
  module_platform_driver(gab_driver);
  
  MODULE_AUTHOR("anish kumar <anish198519851985@gmail.com>");
  MODULE_DESCRIPTION("generic battery driver using IIO");
  MODULE_LICENSE("GPL");