blob: 0ffe5cd3abf627fc14516384e7718601604571c8 [file] [log] [blame]
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +03001/*
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002 * iPAQ h1930/h1940/rx1950 battery controller driver
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +03003 * Copyright (c) Vasily Khoruzhick
4 * Based on h1940_battery.c by Arnaud Patard
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive for
8 * more details.
9 *
10 */
11
12#include <linux/interrupt.h>
13#include <linux/platform_device.h>
14#include <linux/power_supply.h>
15#include <linux/leds.h>
16#include <linux/gpio.h>
17#include <linux/err.h>
18#include <linux/timer.h>
19#include <linux/jiffies.h>
20#include <linux/s3c_adc_battery.h>
21#include <linux/errno.h>
22#include <linux/init.h>
Vasily Khoruzhick815efa12011-08-12 17:55:18 +030023#include <linux/module.h>
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +030024
25#include <plat/adc.h>
26
27#define BAT_POLL_INTERVAL 10000 /* ms */
28#define JITTER_DELAY 500 /* ms */
29
30struct s3c_adc_bat {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010031 struct power_supply *psy;
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +030032 struct s3c_adc_client *client;
33 struct s3c_adc_bat_pdata *pdata;
34 int volt_value;
35 int cur_value;
36 unsigned int timestamp;
37 int level;
38 int status;
39 int cable_plugged:1;
40};
41
42static struct delayed_work bat_work;
43
44static void s3c_adc_bat_ext_power_changed(struct power_supply *psy)
45{
46 schedule_delayed_work(&bat_work,
47 msecs_to_jiffies(JITTER_DELAY));
48}
49
Heiko Stübner34aed732011-12-29 12:52:07 +010050static int gather_samples(struct s3c_adc_client *client, int num, int channel)
51{
52 int value, i;
53
54 /* default to 1 if nothing is set */
55 if (num < 1)
56 num = 1;
57
58 value = 0;
59 for (i = 0; i < num; i++)
60 value += s3c_adc_read(client, channel);
61 value /= num;
62
63 return value;
64}
65
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +030066static enum power_supply_property s3c_adc_backup_bat_props[] = {
67 POWER_SUPPLY_PROP_VOLTAGE_NOW,
68 POWER_SUPPLY_PROP_VOLTAGE_MIN,
69 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
70};
71
72static int s3c_adc_backup_bat_get_property(struct power_supply *psy,
73 enum power_supply_property psp,
74 union power_supply_propval *val)
75{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010076 struct s3c_adc_bat *bat = power_supply_get_drvdata(psy);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +030077
78 if (!bat) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010079 dev_err(&psy->dev, "%s: no battery infos ?!\n", __func__);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +030080 return -EINVAL;
81 }
82
83 if (bat->volt_value < 0 ||
84 jiffies_to_msecs(jiffies - bat->timestamp) >
85 BAT_POLL_INTERVAL) {
Heiko Stübner34aed732011-12-29 12:52:07 +010086 bat->volt_value = gather_samples(bat->client,
87 bat->pdata->backup_volt_samples,
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +030088 bat->pdata->backup_volt_channel);
89 bat->volt_value *= bat->pdata->backup_volt_mult;
90 bat->timestamp = jiffies;
91 }
92
93 switch (psp) {
94 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
95 val->intval = bat->volt_value;
96 return 0;
97 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
98 val->intval = bat->pdata->backup_volt_min;
99 return 0;
100 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
101 val->intval = bat->pdata->backup_volt_max;
102 return 0;
103 default:
104 return -EINVAL;
105 }
106}
107
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100108static const struct power_supply_desc backup_bat_desc = {
109 .name = "backup-battery",
110 .type = POWER_SUPPLY_TYPE_BATTERY,
111 .properties = s3c_adc_backup_bat_props,
112 .num_properties = ARRAY_SIZE(s3c_adc_backup_bat_props),
113 .get_property = s3c_adc_backup_bat_get_property,
114 .use_for_apm = 1,
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300115};
116
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100117static struct s3c_adc_bat backup_bat;
118
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300119static enum power_supply_property s3c_adc_main_bat_props[] = {
120 POWER_SUPPLY_PROP_STATUS,
121 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
122 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
123 POWER_SUPPLY_PROP_CHARGE_NOW,
124 POWER_SUPPLY_PROP_VOLTAGE_NOW,
125 POWER_SUPPLY_PROP_CURRENT_NOW,
126};
127
128static int calc_full_volt(int volt_val, int cur_val, int impedance)
129{
130 return volt_val + cur_val * impedance / 1000;
131}
132
Vasily Khoruzhickc66ae9b2010-12-13 12:26:21 +0200133static int charge_finished(struct s3c_adc_bat *bat)
134{
135 return bat->pdata->gpio_inverted ?
136 !gpio_get_value(bat->pdata->gpio_charge_finished) :
137 gpio_get_value(bat->pdata->gpio_charge_finished);
138}
139
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300140static int s3c_adc_bat_get_property(struct power_supply *psy,
141 enum power_supply_property psp,
142 union power_supply_propval *val)
143{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100144 struct s3c_adc_bat *bat = power_supply_get_drvdata(psy);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300145
146 int new_level;
147 int full_volt;
Syam Sidhardhanc6cc9fc2013-02-25 04:33:25 +0530148 const struct s3c_adc_bat_thresh *lut;
149 unsigned int lut_size;
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300150
151 if (!bat) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100152 dev_err(&psy->dev, "no battery infos ?!\n");
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300153 return -EINVAL;
154 }
155
Syam Sidhardhanc6cc9fc2013-02-25 04:33:25 +0530156 lut = bat->pdata->lut_noac;
157 lut_size = bat->pdata->lut_noac_cnt;
158
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300159 if (bat->volt_value < 0 || bat->cur_value < 0 ||
160 jiffies_to_msecs(jiffies - bat->timestamp) >
161 BAT_POLL_INTERVAL) {
Heiko Stübner34aed732011-12-29 12:52:07 +0100162 bat->volt_value = gather_samples(bat->client,
163 bat->pdata->volt_samples,
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300164 bat->pdata->volt_channel) * bat->pdata->volt_mult;
Heiko Stübner34aed732011-12-29 12:52:07 +0100165 bat->cur_value = gather_samples(bat->client,
166 bat->pdata->current_samples,
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300167 bat->pdata->current_channel) * bat->pdata->current_mult;
168 bat->timestamp = jiffies;
169 }
170
171 if (bat->cable_plugged &&
172 ((bat->pdata->gpio_charge_finished < 0) ||
Vasily Khoruzhickc66ae9b2010-12-13 12:26:21 +0200173 !charge_finished(bat))) {
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300174 lut = bat->pdata->lut_acin;
175 lut_size = bat->pdata->lut_acin_cnt;
176 }
177
178 new_level = 100000;
179 full_volt = calc_full_volt((bat->volt_value / 1000),
180 (bat->cur_value / 1000), bat->pdata->internal_impedance);
181
182 if (full_volt < calc_full_volt(lut->volt, lut->cur,
183 bat->pdata->internal_impedance)) {
184 lut_size--;
185 while (lut_size--) {
186 int lut_volt1;
187 int lut_volt2;
188
189 lut_volt1 = calc_full_volt(lut[0].volt, lut[0].cur,
190 bat->pdata->internal_impedance);
191 lut_volt2 = calc_full_volt(lut[1].volt, lut[1].cur,
192 bat->pdata->internal_impedance);
193 if (full_volt < lut_volt1 && full_volt >= lut_volt2) {
194 new_level = (lut[1].level +
195 (lut[0].level - lut[1].level) *
196 (full_volt - lut_volt2) /
197 (lut_volt1 - lut_volt2)) * 1000;
198 break;
199 }
200 new_level = lut[1].level * 1000;
201 lut++;
202 }
203 }
204
205 bat->level = new_level;
206
207 switch (psp) {
208 case POWER_SUPPLY_PROP_STATUS:
209 if (bat->pdata->gpio_charge_finished < 0)
210 val->intval = bat->level == 100000 ?
211 POWER_SUPPLY_STATUS_FULL : bat->status;
212 else
213 val->intval = bat->status;
214 return 0;
215 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
216 val->intval = 100000;
217 return 0;
218 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
219 val->intval = 0;
220 return 0;
221 case POWER_SUPPLY_PROP_CHARGE_NOW:
222 val->intval = bat->level;
223 return 0;
224 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
225 val->intval = bat->volt_value;
226 return 0;
227 case POWER_SUPPLY_PROP_CURRENT_NOW:
228 val->intval = bat->cur_value;
229 return 0;
230 default:
231 return -EINVAL;
232 }
233}
234
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100235static const struct power_supply_desc main_bat_desc = {
236 .name = "main-battery",
237 .type = POWER_SUPPLY_TYPE_BATTERY,
238 .properties = s3c_adc_main_bat_props,
239 .num_properties = ARRAY_SIZE(s3c_adc_main_bat_props),
240 .get_property = s3c_adc_bat_get_property,
241 .external_power_changed = s3c_adc_bat_ext_power_changed,
242 .use_for_apm = 1,
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300243};
244
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100245static struct s3c_adc_bat main_bat;
246
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300247static void s3c_adc_bat_work(struct work_struct *work)
248{
249 struct s3c_adc_bat *bat = &main_bat;
250 int is_charged;
251 int is_plugged;
252 static int was_plugged;
253
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100254 is_plugged = power_supply_am_i_supplied(bat->psy);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300255 bat->cable_plugged = is_plugged;
256 if (is_plugged != was_plugged) {
257 was_plugged = is_plugged;
258 if (is_plugged) {
259 if (bat->pdata->enable_charger)
260 bat->pdata->enable_charger();
261 bat->status = POWER_SUPPLY_STATUS_CHARGING;
262 } else {
263 if (bat->pdata->disable_charger)
264 bat->pdata->disable_charger();
265 bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
266 }
267 } else {
268 if ((bat->pdata->gpio_charge_finished >= 0) && is_plugged) {
Vasily Khoruzhickc66ae9b2010-12-13 12:26:21 +0200269 is_charged = charge_finished(&main_bat);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300270 if (is_charged) {
271 if (bat->pdata->disable_charger)
272 bat->pdata->disable_charger();
273 bat->status = POWER_SUPPLY_STATUS_FULL;
274 } else {
275 if (bat->pdata->enable_charger)
276 bat->pdata->enable_charger();
277 bat->status = POWER_SUPPLY_STATUS_CHARGING;
278 }
279 }
280 }
281
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100282 power_supply_changed(bat->psy);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300283}
284
285static irqreturn_t s3c_adc_bat_charged(int irq, void *dev_id)
286{
287 schedule_delayed_work(&bat_work,
288 msecs_to_jiffies(JITTER_DELAY));
289 return IRQ_HANDLED;
290}
291
Bill Pembertonc8afa642012-11-19 13:22:23 -0500292static int s3c_adc_bat_probe(struct platform_device *pdev)
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300293{
294 struct s3c_adc_client *client;
295 struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
296 int ret;
297
298 client = s3c_adc_register(pdev, NULL, NULL, 0);
299 if (IS_ERR(client)) {
300 dev_err(&pdev->dev, "cannot register adc\n");
301 return PTR_ERR(client);
302 }
303
304 platform_set_drvdata(pdev, client);
305
306 main_bat.client = client;
307 main_bat.pdata = pdata;
308 main_bat.volt_value = -1;
309 main_bat.cur_value = -1;
310 main_bat.cable_plugged = 0;
311 main_bat.status = POWER_SUPPLY_STATUS_DISCHARGING;
312
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100313 main_bat.psy = power_supply_register(&pdev->dev, &main_bat_desc, NULL);
314 if (IS_ERR(main_bat.psy)) {
315 ret = PTR_ERR(main_bat.psy);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300316 goto err_reg_main;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100317 }
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300318 if (pdata->backup_volt_mult) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100319 const struct power_supply_config psy_cfg
320 = { .drv_data = &backup_bat, };
321
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300322 backup_bat.client = client;
323 backup_bat.pdata = pdev->dev.platform_data;
324 backup_bat.volt_value = -1;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100325 backup_bat.psy = power_supply_register(&pdev->dev,
326 &backup_bat_desc,
327 &psy_cfg);
328 if (IS_ERR(backup_bat.psy)) {
329 ret = PTR_ERR(backup_bat.psy);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300330 goto err_reg_backup;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100331 }
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300332 }
333
334 INIT_DELAYED_WORK(&bat_work, s3c_adc_bat_work);
335
336 if (pdata->gpio_charge_finished >= 0) {
337 ret = gpio_request(pdata->gpio_charge_finished, "charged");
338 if (ret)
339 goto err_gpio;
340
341 ret = request_irq(gpio_to_irq(pdata->gpio_charge_finished),
342 s3c_adc_bat_charged,
343 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
344 "battery charged", NULL);
345 if (ret)
346 goto err_irq;
347 }
348
349 if (pdata->init) {
350 ret = pdata->init();
351 if (ret)
352 goto err_platform;
353 }
354
355 dev_info(&pdev->dev, "successfully loaded\n");
356 device_init_wakeup(&pdev->dev, 1);
357
358 /* Schedule timer to check current status */
359 schedule_delayed_work(&bat_work,
360 msecs_to_jiffies(JITTER_DELAY));
361
362 return 0;
363
364err_platform:
365 if (pdata->gpio_charge_finished >= 0)
366 free_irq(gpio_to_irq(pdata->gpio_charge_finished), NULL);
367err_irq:
368 if (pdata->gpio_charge_finished >= 0)
369 gpio_free(pdata->gpio_charge_finished);
370err_gpio:
371 if (pdata->backup_volt_mult)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100372 power_supply_unregister(backup_bat.psy);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300373err_reg_backup:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100374 power_supply_unregister(main_bat.psy);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300375err_reg_main:
376 return ret;
377}
378
379static int s3c_adc_bat_remove(struct platform_device *pdev)
380{
381 struct s3c_adc_client *client = platform_get_drvdata(pdev);
382 struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
383
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100384 power_supply_unregister(main_bat.psy);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300385 if (pdata->backup_volt_mult)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100386 power_supply_unregister(backup_bat.psy);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300387
388 s3c_adc_release(client);
389
390 if (pdata->gpio_charge_finished >= 0) {
391 free_irq(gpio_to_irq(pdata->gpio_charge_finished), NULL);
392 gpio_free(pdata->gpio_charge_finished);
393 }
394
395 cancel_delayed_work(&bat_work);
396
397 if (pdata->exit)
398 pdata->exit();
399
400 return 0;
401}
402
403#ifdef CONFIG_PM
404static int s3c_adc_bat_suspend(struct platform_device *pdev,
405 pm_message_t state)
406{
407 struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
408
409 if (pdata->gpio_charge_finished >= 0) {
410 if (device_may_wakeup(&pdev->dev))
411 enable_irq_wake(
412 gpio_to_irq(pdata->gpio_charge_finished));
413 else {
414 disable_irq(gpio_to_irq(pdata->gpio_charge_finished));
415 main_bat.pdata->disable_charger();
416 }
417 }
418
419 return 0;
420}
421
422static int s3c_adc_bat_resume(struct platform_device *pdev)
423{
424 struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
425
426 if (pdata->gpio_charge_finished >= 0) {
427 if (device_may_wakeup(&pdev->dev))
428 disable_irq_wake(
429 gpio_to_irq(pdata->gpio_charge_finished));
430 else
431 enable_irq(gpio_to_irq(pdata->gpio_charge_finished));
432 }
433
434 /* Schedule timer to check current status */
435 schedule_delayed_work(&bat_work,
436 msecs_to_jiffies(JITTER_DELAY));
437
438 return 0;
439}
440#else
Heiko Stuebnerf8d878d2011-03-14 21:12:39 +0100441#define s3c_adc_bat_suspend NULL
442#define s3c_adc_bat_resume NULL
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300443#endif
444
445static struct platform_driver s3c_adc_bat_driver = {
446 .driver = {
447 .name = "s3c-adc-battery",
448 },
449 .probe = s3c_adc_bat_probe,
450 .remove = s3c_adc_bat_remove,
451 .suspend = s3c_adc_bat_suspend,
452 .resume = s3c_adc_bat_resume,
453};
454
Axel Lin300bac72011-11-26 12:01:10 +0800455module_platform_driver(s3c_adc_bat_driver);
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300456
457MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400458MODULE_DESCRIPTION("iPAQ H1930/H1940/RX1950 battery controller driver");
Vasily Khoruzhick808be4b2010-07-17 13:57:03 +0300459MODULE_LICENSE("GPL");