]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/power/s3c_adc_battery.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[karo-tx-linux.git] / drivers / power / s3c_adc_battery.c
1 /*
2  *      iPAQ h1930/h1940/rx1950 battery controller driver
3  *      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>
23 #include <linux/module.h>
24
25 #include <plat/adc.h>
26
27 #define BAT_POLL_INTERVAL               10000 /* ms */
28 #define JITTER_DELAY                    500 /* ms */
29
30 struct s3c_adc_bat {
31         struct power_supply             psy;
32         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
42 static struct delayed_work bat_work;
43
44 static 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
50 static 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
66 static 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
72 static int s3c_adc_backup_bat_get_property(struct power_supply *psy,
73                                 enum power_supply_property psp,
74                                 union power_supply_propval *val)
75 {
76         struct s3c_adc_bat *bat = container_of(psy, struct s3c_adc_bat, psy);
77
78         if (!bat) {
79                 dev_err(psy->dev, "%s: no battery infos ?!\n", __func__);
80                 return -EINVAL;
81         }
82
83         if (bat->volt_value < 0 ||
84                 jiffies_to_msecs(jiffies - bat->timestamp) >
85                         BAT_POLL_INTERVAL) {
86                 bat->volt_value = gather_samples(bat->client,
87                         bat->pdata->backup_volt_samples,
88                         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
108 static struct s3c_adc_bat backup_bat = {
109         .psy = {
110                 .name           = "backup-battery",
111                 .type           = POWER_SUPPLY_TYPE_BATTERY,
112                 .properties     = s3c_adc_backup_bat_props,
113                 .num_properties = ARRAY_SIZE(s3c_adc_backup_bat_props),
114                 .get_property   = s3c_adc_backup_bat_get_property,
115                 .use_for_apm    = 1,
116         },
117 };
118
119 static 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
128 static int calc_full_volt(int volt_val, int cur_val, int impedance)
129 {
130         return volt_val + cur_val * impedance / 1000;
131 }
132
133 static 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
140 static int s3c_adc_bat_get_property(struct power_supply *psy,
141                                     enum power_supply_property psp,
142                                     union power_supply_propval *val)
143 {
144         struct s3c_adc_bat *bat = container_of(psy, struct s3c_adc_bat, psy);
145
146         int new_level;
147         int full_volt;
148         const struct s3c_adc_bat_thresh *lut = bat->pdata->lut_noac;
149         unsigned int lut_size = bat->pdata->lut_noac_cnt;
150
151         if (!bat) {
152                 dev_err(psy->dev, "no battery infos ?!\n");
153                 return -EINVAL;
154         }
155
156         if (bat->volt_value < 0 || bat->cur_value < 0 ||
157                 jiffies_to_msecs(jiffies - bat->timestamp) >
158                         BAT_POLL_INTERVAL) {
159                 bat->volt_value = gather_samples(bat->client,
160                         bat->pdata->volt_samples,
161                         bat->pdata->volt_channel) * bat->pdata->volt_mult;
162                 bat->cur_value = gather_samples(bat->client,
163                         bat->pdata->current_samples,
164                         bat->pdata->current_channel) * bat->pdata->current_mult;
165                 bat->timestamp = jiffies;
166         }
167
168         if (bat->cable_plugged &&
169                 ((bat->pdata->gpio_charge_finished < 0) ||
170                 !charge_finished(bat))) {
171                 lut = bat->pdata->lut_acin;
172                 lut_size = bat->pdata->lut_acin_cnt;
173         }
174
175         new_level = 100000;
176         full_volt = calc_full_volt((bat->volt_value / 1000),
177                 (bat->cur_value / 1000), bat->pdata->internal_impedance);
178
179         if (full_volt < calc_full_volt(lut->volt, lut->cur,
180                 bat->pdata->internal_impedance)) {
181                 lut_size--;
182                 while (lut_size--) {
183                         int lut_volt1;
184                         int lut_volt2;
185
186                         lut_volt1 = calc_full_volt(lut[0].volt, lut[0].cur,
187                                 bat->pdata->internal_impedance);
188                         lut_volt2 = calc_full_volt(lut[1].volt, lut[1].cur,
189                                 bat->pdata->internal_impedance);
190                         if (full_volt < lut_volt1 && full_volt >= lut_volt2) {
191                                 new_level = (lut[1].level +
192                                         (lut[0].level - lut[1].level) *
193                                         (full_volt - lut_volt2) /
194                                         (lut_volt1 - lut_volt2)) * 1000;
195                                 break;
196                         }
197                         new_level = lut[1].level * 1000;
198                         lut++;
199                 }
200         }
201
202         bat->level = new_level;
203
204         switch (psp) {
205         case POWER_SUPPLY_PROP_STATUS:
206                 if (bat->pdata->gpio_charge_finished < 0)
207                         val->intval = bat->level == 100000 ?
208                                 POWER_SUPPLY_STATUS_FULL : bat->status;
209                 else
210                         val->intval = bat->status;
211                 return 0;
212         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
213                 val->intval = 100000;
214                 return 0;
215         case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
216                 val->intval = 0;
217                 return 0;
218         case POWER_SUPPLY_PROP_CHARGE_NOW:
219                 val->intval = bat->level;
220                 return 0;
221         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
222                 val->intval = bat->volt_value;
223                 return 0;
224         case POWER_SUPPLY_PROP_CURRENT_NOW:
225                 val->intval = bat->cur_value;
226                 return 0;
227         default:
228                 return -EINVAL;
229         }
230 }
231
232 static struct s3c_adc_bat main_bat = {
233         .psy = {
234                 .name                   = "main-battery",
235                 .type                   = POWER_SUPPLY_TYPE_BATTERY,
236                 .properties             = s3c_adc_main_bat_props,
237                 .num_properties         = ARRAY_SIZE(s3c_adc_main_bat_props),
238                 .get_property           = s3c_adc_bat_get_property,
239                 .external_power_changed = s3c_adc_bat_ext_power_changed,
240                 .use_for_apm            = 1,
241         },
242 };
243
244 static void s3c_adc_bat_work(struct work_struct *work)
245 {
246         struct s3c_adc_bat *bat = &main_bat;
247         int is_charged;
248         int is_plugged;
249         static int was_plugged;
250
251         is_plugged = power_supply_am_i_supplied(&bat->psy);
252         bat->cable_plugged = is_plugged;
253         if (is_plugged != was_plugged) {
254                 was_plugged = is_plugged;
255                 if (is_plugged) {
256                         if (bat->pdata->enable_charger)
257                                 bat->pdata->enable_charger();
258                         bat->status = POWER_SUPPLY_STATUS_CHARGING;
259                 } else {
260                         if (bat->pdata->disable_charger)
261                                 bat->pdata->disable_charger();
262                         bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
263                 }
264         } else {
265                 if ((bat->pdata->gpio_charge_finished >= 0) && is_plugged) {
266                         is_charged = charge_finished(&main_bat);
267                         if (is_charged) {
268                                 if (bat->pdata->disable_charger)
269                                         bat->pdata->disable_charger();
270                                 bat->status = POWER_SUPPLY_STATUS_FULL;
271                         } else {
272                                 if (bat->pdata->enable_charger)
273                                         bat->pdata->enable_charger();
274                                 bat->status = POWER_SUPPLY_STATUS_CHARGING;
275                         }
276                 }
277         }
278
279         power_supply_changed(&bat->psy);
280 }
281
282 static irqreturn_t s3c_adc_bat_charged(int irq, void *dev_id)
283 {
284         schedule_delayed_work(&bat_work,
285                 msecs_to_jiffies(JITTER_DELAY));
286         return IRQ_HANDLED;
287 }
288
289 static int s3c_adc_bat_probe(struct platform_device *pdev)
290 {
291         struct s3c_adc_client   *client;
292         struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
293         int ret;
294
295         client = s3c_adc_register(pdev, NULL, NULL, 0);
296         if (IS_ERR(client)) {
297                 dev_err(&pdev->dev, "cannot register adc\n");
298                 return PTR_ERR(client);
299         }
300
301         platform_set_drvdata(pdev, client);
302
303         main_bat.client = client;
304         main_bat.pdata = pdata;
305         main_bat.volt_value = -1;
306         main_bat.cur_value = -1;
307         main_bat.cable_plugged = 0;
308         main_bat.status = POWER_SUPPLY_STATUS_DISCHARGING;
309
310         ret = power_supply_register(&pdev->dev, &main_bat.psy);
311         if (ret)
312                 goto err_reg_main;
313         if (pdata->backup_volt_mult) {
314                 backup_bat.client = client;
315                 backup_bat.pdata = pdev->dev.platform_data;
316                 backup_bat.volt_value = -1;
317                 ret = power_supply_register(&pdev->dev, &backup_bat.psy);
318                 if (ret)
319                         goto err_reg_backup;
320         }
321
322         INIT_DELAYED_WORK(&bat_work, s3c_adc_bat_work);
323
324         if (pdata->gpio_charge_finished >= 0) {
325                 ret = gpio_request(pdata->gpio_charge_finished, "charged");
326                 if (ret)
327                         goto err_gpio;
328
329                 ret = request_irq(gpio_to_irq(pdata->gpio_charge_finished),
330                                 s3c_adc_bat_charged,
331                                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
332                                 "battery charged", NULL);
333                 if (ret)
334                         goto err_irq;
335         }
336
337         if (pdata->init) {
338                 ret = pdata->init();
339                 if (ret)
340                         goto err_platform;
341         }
342
343         dev_info(&pdev->dev, "successfully loaded\n");
344         device_init_wakeup(&pdev->dev, 1);
345
346         /* Schedule timer to check current status */
347         schedule_delayed_work(&bat_work,
348                 msecs_to_jiffies(JITTER_DELAY));
349
350         return 0;
351
352 err_platform:
353         if (pdata->gpio_charge_finished >= 0)
354                 free_irq(gpio_to_irq(pdata->gpio_charge_finished), NULL);
355 err_irq:
356         if (pdata->gpio_charge_finished >= 0)
357                 gpio_free(pdata->gpio_charge_finished);
358 err_gpio:
359         if (pdata->backup_volt_mult)
360                 power_supply_unregister(&backup_bat.psy);
361 err_reg_backup:
362         power_supply_unregister(&main_bat.psy);
363 err_reg_main:
364         return ret;
365 }
366
367 static int s3c_adc_bat_remove(struct platform_device *pdev)
368 {
369         struct s3c_adc_client *client = platform_get_drvdata(pdev);
370         struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
371
372         power_supply_unregister(&main_bat.psy);
373         if (pdata->backup_volt_mult)
374                 power_supply_unregister(&backup_bat.psy);
375
376         s3c_adc_release(client);
377
378         if (pdata->gpio_charge_finished >= 0) {
379                 free_irq(gpio_to_irq(pdata->gpio_charge_finished), NULL);
380                 gpio_free(pdata->gpio_charge_finished);
381         }
382
383         cancel_delayed_work(&bat_work);
384
385         if (pdata->exit)
386                 pdata->exit();
387
388         return 0;
389 }
390
391 #ifdef CONFIG_PM
392 static int s3c_adc_bat_suspend(struct platform_device *pdev,
393         pm_message_t state)
394 {
395         struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
396
397         if (pdata->gpio_charge_finished >= 0) {
398                 if (device_may_wakeup(&pdev->dev))
399                         enable_irq_wake(
400                                 gpio_to_irq(pdata->gpio_charge_finished));
401                 else {
402                         disable_irq(gpio_to_irq(pdata->gpio_charge_finished));
403                         main_bat.pdata->disable_charger();
404                 }
405         }
406
407         return 0;
408 }
409
410 static int s3c_adc_bat_resume(struct platform_device *pdev)
411 {
412         struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
413
414         if (pdata->gpio_charge_finished >= 0) {
415                 if (device_may_wakeup(&pdev->dev))
416                         disable_irq_wake(
417                                 gpio_to_irq(pdata->gpio_charge_finished));
418                 else
419                         enable_irq(gpio_to_irq(pdata->gpio_charge_finished));
420         }
421
422         /* Schedule timer to check current status */
423         schedule_delayed_work(&bat_work,
424                 msecs_to_jiffies(JITTER_DELAY));
425
426         return 0;
427 }
428 #else
429 #define s3c_adc_bat_suspend NULL
430 #define s3c_adc_bat_resume NULL
431 #endif
432
433 static struct platform_driver s3c_adc_bat_driver = {
434         .driver         = {
435                 .name   = "s3c-adc-battery",
436         },
437         .probe          = s3c_adc_bat_probe,
438         .remove         = s3c_adc_bat_remove,
439         .suspend        = s3c_adc_bat_suspend,
440         .resume         = s3c_adc_bat_resume,
441 };
442
443 module_platform_driver(s3c_adc_bat_driver);
444
445 MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
446 MODULE_DESCRIPTION("iPAQ H1930/H1940/RX1950 battery controller driver");
447 MODULE_LICENSE("GPL");