2 * iPAQ h1930/h1940/rx1950 battery controller driver
3 * Copyright (c) Vasily Khoruzhick
4 * Based on h1940_battery.c by Arnaud Patard
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
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>
27 #define BAT_POLL_INTERVAL 10000 /* ms */
28 #define JITTER_DELAY 500 /* ms */
31 struct power_supply psy
;
32 struct s3c_adc_client
*client
;
33 struct s3c_adc_bat_pdata
*pdata
;
36 unsigned int timestamp
;
42 static struct delayed_work bat_work
;
44 static void s3c_adc_bat_ext_power_changed(struct power_supply
*psy
)
46 schedule_delayed_work(&bat_work
,
47 msecs_to_jiffies(JITTER_DELAY
));
50 static int gather_samples(struct s3c_adc_client
*client
, int num
, int channel
)
54 /* default to 1 if nothing is set */
59 for (i
= 0; i
< num
; i
++)
60 value
+= s3c_adc_read(client
, channel
);
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
,
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
)
76 struct s3c_adc_bat
*bat
= container_of(psy
, struct s3c_adc_bat
, psy
);
79 dev_err(psy
->dev
, "%s: no battery infos ?!\n", __func__
);
83 if (bat
->volt_value
< 0 ||
84 jiffies_to_msecs(jiffies
- bat
->timestamp
) >
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
;
94 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
95 val
->intval
= bat
->volt_value
;
97 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
98 val
->intval
= bat
->pdata
->backup_volt_min
;
100 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
101 val
->intval
= bat
->pdata
->backup_volt_max
;
108 static struct s3c_adc_bat backup_bat
= {
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
,
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
,
128 static int calc_full_volt(int volt_val
, int cur_val
, int impedance
)
130 return volt_val
+ cur_val
* impedance
/ 1000;
133 static int charge_finished(struct s3c_adc_bat
*bat
)
135 return bat
->pdata
->gpio_inverted
?
136 !gpio_get_value(bat
->pdata
->gpio_charge_finished
) :
137 gpio_get_value(bat
->pdata
->gpio_charge_finished
);
140 static int s3c_adc_bat_get_property(struct power_supply
*psy
,
141 enum power_supply_property psp
,
142 union power_supply_propval
*val
)
144 struct s3c_adc_bat
*bat
= container_of(psy
, struct s3c_adc_bat
, psy
);
148 const struct s3c_adc_bat_thresh
*lut
;
149 unsigned int lut_size
;
152 dev_err(psy
->dev
, "no battery infos ?!\n");
156 lut
= bat
->pdata
->lut_noac
;
157 lut_size
= bat
->pdata
->lut_noac_cnt
;
159 if (bat
->volt_value
< 0 || bat
->cur_value
< 0 ||
160 jiffies_to_msecs(jiffies
- bat
->timestamp
) >
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
;
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
;
179 full_volt
= calc_full_volt((bat
->volt_value
/ 1000),
180 (bat
->cur_value
/ 1000), bat
->pdata
->internal_impedance
);
182 if (full_volt
< calc_full_volt(lut
->volt
, lut
->cur
,
183 bat
->pdata
->internal_impedance
)) {
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;
200 new_level
= lut
[1].level
* 1000;
205 bat
->level
= new_level
;
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
;
213 val
->intval
= bat
->status
;
215 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
216 val
->intval
= 100000;
218 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
:
221 case POWER_SUPPLY_PROP_CHARGE_NOW
:
222 val
->intval
= bat
->level
;
224 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
225 val
->intval
= bat
->volt_value
;
227 case POWER_SUPPLY_PROP_CURRENT_NOW
:
228 val
->intval
= bat
->cur_value
;
235 static struct s3c_adc_bat main_bat
= {
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
,
247 static void s3c_adc_bat_work(struct work_struct
*work
)
249 struct s3c_adc_bat
*bat
= &main_bat
;
252 static int was_plugged
;
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
;
259 if (bat
->pdata
->enable_charger
)
260 bat
->pdata
->enable_charger();
261 bat
->status
= POWER_SUPPLY_STATUS_CHARGING
;
263 if (bat
->pdata
->disable_charger
)
264 bat
->pdata
->disable_charger();
265 bat
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
268 if ((bat
->pdata
->gpio_charge_finished
>= 0) && is_plugged
) {
269 is_charged
= charge_finished(&main_bat
);
271 if (bat
->pdata
->disable_charger
)
272 bat
->pdata
->disable_charger();
273 bat
->status
= POWER_SUPPLY_STATUS_FULL
;
275 if (bat
->pdata
->enable_charger
)
276 bat
->pdata
->enable_charger();
277 bat
->status
= POWER_SUPPLY_STATUS_CHARGING
;
282 power_supply_changed(&bat
->psy
);
285 static irqreturn_t
s3c_adc_bat_charged(int irq
, void *dev_id
)
287 schedule_delayed_work(&bat_work
,
288 msecs_to_jiffies(JITTER_DELAY
));
292 static int s3c_adc_bat_probe(struct platform_device
*pdev
)
294 struct s3c_adc_client
*client
;
295 struct s3c_adc_bat_pdata
*pdata
= pdev
->dev
.platform_data
;
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
);
304 platform_set_drvdata(pdev
, client
);
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
;
313 ret
= power_supply_register(&pdev
->dev
, &main_bat
.psy
);
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
);
325 INIT_DELAYED_WORK(&bat_work
, s3c_adc_bat_work
);
327 if (pdata
->gpio_charge_finished
>= 0) {
328 ret
= gpio_request(pdata
->gpio_charge_finished
, "charged");
332 ret
= request_irq(gpio_to_irq(pdata
->gpio_charge_finished
),
334 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
335 "battery charged", NULL
);
346 dev_info(&pdev
->dev
, "successfully loaded\n");
347 device_init_wakeup(&pdev
->dev
, 1);
349 /* Schedule timer to check current status */
350 schedule_delayed_work(&bat_work
,
351 msecs_to_jiffies(JITTER_DELAY
));
356 if (pdata
->gpio_charge_finished
>= 0)
357 free_irq(gpio_to_irq(pdata
->gpio_charge_finished
), NULL
);
359 if (pdata
->gpio_charge_finished
>= 0)
360 gpio_free(pdata
->gpio_charge_finished
);
362 if (pdata
->backup_volt_mult
)
363 power_supply_unregister(&backup_bat
.psy
);
365 power_supply_unregister(&main_bat
.psy
);
370 static int s3c_adc_bat_remove(struct platform_device
*pdev
)
372 struct s3c_adc_client
*client
= platform_get_drvdata(pdev
);
373 struct s3c_adc_bat_pdata
*pdata
= pdev
->dev
.platform_data
;
375 power_supply_unregister(&main_bat
.psy
);
376 if (pdata
->backup_volt_mult
)
377 power_supply_unregister(&backup_bat
.psy
);
379 s3c_adc_release(client
);
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
);
386 cancel_delayed_work(&bat_work
);
395 static int s3c_adc_bat_suspend(struct platform_device
*pdev
,
398 struct s3c_adc_bat_pdata
*pdata
= pdev
->dev
.platform_data
;
400 if (pdata
->gpio_charge_finished
>= 0) {
401 if (device_may_wakeup(&pdev
->dev
))
403 gpio_to_irq(pdata
->gpio_charge_finished
));
405 disable_irq(gpio_to_irq(pdata
->gpio_charge_finished
));
406 main_bat
.pdata
->disable_charger();
413 static int s3c_adc_bat_resume(struct platform_device
*pdev
)
415 struct s3c_adc_bat_pdata
*pdata
= pdev
->dev
.platform_data
;
417 if (pdata
->gpio_charge_finished
>= 0) {
418 if (device_may_wakeup(&pdev
->dev
))
420 gpio_to_irq(pdata
->gpio_charge_finished
));
422 enable_irq(gpio_to_irq(pdata
->gpio_charge_finished
));
425 /* Schedule timer to check current status */
426 schedule_delayed_work(&bat_work
,
427 msecs_to_jiffies(JITTER_DELAY
));
432 #define s3c_adc_bat_suspend NULL
433 #define s3c_adc_bat_resume NULL
436 static struct platform_driver s3c_adc_bat_driver
= {
438 .name
= "s3c-adc-battery",
440 .probe
= s3c_adc_bat_probe
,
441 .remove
= s3c_adc_bat_remove
,
442 .suspend
= s3c_adc_bat_suspend
,
443 .resume
= s3c_adc_bat_resume
,
446 module_platform_driver(s3c_adc_bat_driver
);
448 MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
449 MODULE_DESCRIPTION("iPAQ H1930/H1940/RX1950 battery controller driver");
450 MODULE_LICENSE("GPL");