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