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 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
,
56 static int s3c_adc_backup_bat_get_property(struct power_supply
*psy
,
57 enum power_supply_property psp
,
58 union power_supply_propval
*val
)
60 struct s3c_adc_bat
*bat
= container_of(psy
, struct s3c_adc_bat
, psy
);
63 dev_err(psy
->dev
, "%s: no battery infos ?!\n", __func__
);
67 if (bat
->volt_value
< 0 ||
68 jiffies_to_msecs(jiffies
- bat
->timestamp
) >
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
;
77 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
78 val
->intval
= bat
->volt_value
;
80 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
81 val
->intval
= bat
->pdata
->backup_volt_min
;
83 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
84 val
->intval
= bat
->pdata
->backup_volt_max
;
91 static struct s3c_adc_bat backup_bat
= {
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
,
102 static 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
,
111 static int calc_full_volt(int volt_val
, int cur_val
, int impedance
)
113 return volt_val
+ cur_val
* impedance
/ 1000;
116 static int charge_finished(struct s3c_adc_bat
*bat
)
118 return bat
->pdata
->gpio_inverted
?
119 !gpio_get_value(bat
->pdata
->gpio_charge_finished
) :
120 gpio_get_value(bat
->pdata
->gpio_charge_finished
);
123 static int s3c_adc_bat_get_property(struct power_supply
*psy
,
124 enum power_supply_property psp
,
125 union power_supply_propval
*val
)
127 struct s3c_adc_bat
*bat
= container_of(psy
, struct s3c_adc_bat
, psy
);
131 const struct s3c_adc_bat_thresh
*lut
= bat
->pdata
->lut_noac
;
132 unsigned int lut_size
= bat
->pdata
->lut_noac_cnt
;
135 dev_err(psy
->dev
, "no battery infos ?!\n");
139 if (bat
->volt_value
< 0 || bat
->cur_value
< 0 ||
140 jiffies_to_msecs(jiffies
- bat
->timestamp
) >
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
;
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
;
157 full_volt
= calc_full_volt((bat
->volt_value
/ 1000),
158 (bat
->cur_value
/ 1000), bat
->pdata
->internal_impedance
);
160 if (full_volt
< calc_full_volt(lut
->volt
, lut
->cur
,
161 bat
->pdata
->internal_impedance
)) {
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;
178 new_level
= lut
[1].level
* 1000;
183 bat
->level
= new_level
;
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
;
191 val
->intval
= bat
->status
;
193 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
194 val
->intval
= 100000;
196 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
:
199 case POWER_SUPPLY_PROP_CHARGE_NOW
:
200 val
->intval
= bat
->level
;
202 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
203 val
->intval
= bat
->volt_value
;
205 case POWER_SUPPLY_PROP_CURRENT_NOW
:
206 val
->intval
= bat
->cur_value
;
213 static struct s3c_adc_bat main_bat
= {
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
,
225 static void s3c_adc_bat_work(struct work_struct
*work
)
227 struct s3c_adc_bat
*bat
= &main_bat
;
230 static int was_plugged
;
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
;
237 if (bat
->pdata
->enable_charger
)
238 bat
->pdata
->enable_charger();
239 bat
->status
= POWER_SUPPLY_STATUS_CHARGING
;
241 if (bat
->pdata
->disable_charger
)
242 bat
->pdata
->disable_charger();
243 bat
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
246 if ((bat
->pdata
->gpio_charge_finished
>= 0) && is_plugged
) {
247 is_charged
= charge_finished(&main_bat
);
249 if (bat
->pdata
->disable_charger
)
250 bat
->pdata
->disable_charger();
251 bat
->status
= POWER_SUPPLY_STATUS_FULL
;
253 if (bat
->pdata
->enable_charger
)
254 bat
->pdata
->enable_charger();
255 bat
->status
= POWER_SUPPLY_STATUS_CHARGING
;
260 power_supply_changed(&bat
->psy
);
263 static irqreturn_t
s3c_adc_bat_charged(int irq
, void *dev_id
)
265 schedule_delayed_work(&bat_work
,
266 msecs_to_jiffies(JITTER_DELAY
));
270 static int __devinit
s3c_adc_bat_probe(struct platform_device
*pdev
)
272 struct s3c_adc_client
*client
;
273 struct s3c_adc_bat_pdata
*pdata
= pdev
->dev
.platform_data
;
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
);
282 platform_set_drvdata(pdev
, client
);
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
;
291 ret
= power_supply_register(&pdev
->dev
, &main_bat
.psy
);
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
);
303 INIT_DELAYED_WORK(&bat_work
, s3c_adc_bat_work
);
305 if (pdata
->gpio_charge_finished
>= 0) {
306 ret
= gpio_request(pdata
->gpio_charge_finished
, "charged");
310 ret
= request_irq(gpio_to_irq(pdata
->gpio_charge_finished
),
312 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
313 "battery charged", NULL
);
324 dev_info(&pdev
->dev
, "successfully loaded\n");
325 device_init_wakeup(&pdev
->dev
, 1);
327 /* Schedule timer to check current status */
328 schedule_delayed_work(&bat_work
,
329 msecs_to_jiffies(JITTER_DELAY
));
334 if (pdata
->gpio_charge_finished
>= 0)
335 free_irq(gpio_to_irq(pdata
->gpio_charge_finished
), NULL
);
337 if (pdata
->gpio_charge_finished
>= 0)
338 gpio_free(pdata
->gpio_charge_finished
);
340 if (pdata
->backup_volt_mult
)
341 power_supply_unregister(&backup_bat
.psy
);
343 power_supply_unregister(&main_bat
.psy
);
348 static int s3c_adc_bat_remove(struct platform_device
*pdev
)
350 struct s3c_adc_client
*client
= platform_get_drvdata(pdev
);
351 struct s3c_adc_bat_pdata
*pdata
= pdev
->dev
.platform_data
;
353 power_supply_unregister(&main_bat
.psy
);
354 if (pdata
->backup_volt_mult
)
355 power_supply_unregister(&backup_bat
.psy
);
357 s3c_adc_release(client
);
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
);
364 cancel_delayed_work(&bat_work
);
373 static int s3c_adc_bat_suspend(struct platform_device
*pdev
,
376 struct s3c_adc_bat_pdata
*pdata
= pdev
->dev
.platform_data
;
378 if (pdata
->gpio_charge_finished
>= 0) {
379 if (device_may_wakeup(&pdev
->dev
))
381 gpio_to_irq(pdata
->gpio_charge_finished
));
383 disable_irq(gpio_to_irq(pdata
->gpio_charge_finished
));
384 main_bat
.pdata
->disable_charger();
391 static int s3c_adc_bat_resume(struct platform_device
*pdev
)
393 struct s3c_adc_bat_pdata
*pdata
= pdev
->dev
.platform_data
;
395 if (pdata
->gpio_charge_finished
>= 0) {
396 if (device_may_wakeup(&pdev
->dev
))
398 gpio_to_irq(pdata
->gpio_charge_finished
));
400 enable_irq(gpio_to_irq(pdata
->gpio_charge_finished
));
403 /* Schedule timer to check current status */
404 schedule_delayed_work(&bat_work
,
405 msecs_to_jiffies(JITTER_DELAY
));
410 #define s3c_adc_bat_suspend NULL
411 #define s3c_adc_bat_resume NULL
414 static struct platform_driver s3c_adc_bat_driver
= {
416 .name
= "s3c-adc-battery",
418 .probe
= s3c_adc_bat_probe
,
419 .remove
= s3c_adc_bat_remove
,
420 .suspend
= s3c_adc_bat_suspend
,
421 .resume
= s3c_adc_bat_resume
,
424 static int __init
s3c_adc_bat_init(void)
426 return platform_driver_register(&s3c_adc_bat_driver
);
428 module_init(s3c_adc_bat_init
);
430 static void __exit
s3c_adc_bat_exit(void)
432 platform_driver_unregister(&s3c_adc_bat_driver
);
434 module_exit(s3c_adc_bat_exit
);
436 MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
437 MODULE_DESCRIPTION("iPAQ H1930/H1940/RX1950 battery controller driver");
438 MODULE_LICENSE("GPL");