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/consumer.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>
25 #include <linux/soc/samsung/s3c-adc.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
;
34 struct gpio_desc
*charge_finished
;
37 unsigned int timestamp
;
43 static struct delayed_work bat_work
;
45 static void s3c_adc_bat_ext_power_changed(struct power_supply
*psy
)
47 schedule_delayed_work(&bat_work
,
48 msecs_to_jiffies(JITTER_DELAY
));
51 static int gather_samples(struct s3c_adc_client
*client
, int num
, int channel
)
55 /* default to 1 if nothing is set */
60 for (i
= 0; i
< num
; i
++)
61 value
+= s3c_adc_read(client
, channel
);
67 static enum power_supply_property s3c_adc_backup_bat_props
[] = {
68 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
69 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
70 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
73 static int s3c_adc_backup_bat_get_property(struct power_supply
*psy
,
74 enum power_supply_property psp
,
75 union power_supply_propval
*val
)
77 struct s3c_adc_bat
*bat
= power_supply_get_drvdata(psy
);
80 dev_err(&psy
->dev
, "%s: no battery infos ?!\n", __func__
);
84 if (bat
->volt_value
< 0 ||
85 jiffies_to_msecs(jiffies
- bat
->timestamp
) >
87 bat
->volt_value
= gather_samples(bat
->client
,
88 bat
->pdata
->backup_volt_samples
,
89 bat
->pdata
->backup_volt_channel
);
90 bat
->volt_value
*= bat
->pdata
->backup_volt_mult
;
91 bat
->timestamp
= jiffies
;
95 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
96 val
->intval
= bat
->volt_value
;
98 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
99 val
->intval
= bat
->pdata
->backup_volt_min
;
101 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
102 val
->intval
= bat
->pdata
->backup_volt_max
;
109 static const struct power_supply_desc backup_bat_desc
= {
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
,
118 static struct s3c_adc_bat backup_bat
;
120 static enum power_supply_property s3c_adc_main_bat_props
[] = {
121 POWER_SUPPLY_PROP_STATUS
,
122 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
123 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
,
124 POWER_SUPPLY_PROP_CHARGE_NOW
,
125 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
126 POWER_SUPPLY_PROP_CURRENT_NOW
,
129 static int calc_full_volt(int volt_val
, int cur_val
, int impedance
)
131 return volt_val
+ cur_val
* impedance
/ 1000;
134 static int charge_finished(struct s3c_adc_bat
*bat
)
136 return gpiod_get_value(bat
->charge_finished
);
139 static int s3c_adc_bat_get_property(struct power_supply
*psy
,
140 enum power_supply_property psp
,
141 union power_supply_propval
*val
)
143 struct s3c_adc_bat
*bat
= power_supply_get_drvdata(psy
);
147 const struct s3c_adc_bat_thresh
*lut
;
148 unsigned int lut_size
;
151 dev_err(&psy
->dev
, "no battery infos ?!\n");
155 lut
= bat
->pdata
->lut_noac
;
156 lut_size
= bat
->pdata
->lut_noac_cnt
;
158 if (bat
->volt_value
< 0 || bat
->cur_value
< 0 ||
159 jiffies_to_msecs(jiffies
- bat
->timestamp
) >
161 bat
->volt_value
= gather_samples(bat
->client
,
162 bat
->pdata
->volt_samples
,
163 bat
->pdata
->volt_channel
) * bat
->pdata
->volt_mult
;
164 bat
->cur_value
= gather_samples(bat
->client
,
165 bat
->pdata
->current_samples
,
166 bat
->pdata
->current_channel
) * bat
->pdata
->current_mult
;
167 bat
->timestamp
= jiffies
;
170 if (bat
->cable_plugged
&&
171 (!bat
->charge_finished
||
172 !charge_finished(bat
))) {
173 lut
= bat
->pdata
->lut_acin
;
174 lut_size
= bat
->pdata
->lut_acin_cnt
;
178 full_volt
= calc_full_volt((bat
->volt_value
/ 1000),
179 (bat
->cur_value
/ 1000), bat
->pdata
->internal_impedance
);
181 if (full_volt
< calc_full_volt(lut
->volt
, lut
->cur
,
182 bat
->pdata
->internal_impedance
)) {
188 lut_volt1
= calc_full_volt(lut
[0].volt
, lut
[0].cur
,
189 bat
->pdata
->internal_impedance
);
190 lut_volt2
= calc_full_volt(lut
[1].volt
, lut
[1].cur
,
191 bat
->pdata
->internal_impedance
);
192 if (full_volt
< lut_volt1
&& full_volt
>= lut_volt2
) {
193 new_level
= (lut
[1].level
+
194 (lut
[0].level
- lut
[1].level
) *
195 (full_volt
- lut_volt2
) /
196 (lut_volt1
- lut_volt2
)) * 1000;
199 new_level
= lut
[1].level
* 1000;
204 bat
->level
= new_level
;
207 case POWER_SUPPLY_PROP_STATUS
:
208 if (!bat
->charge_finished
)
209 val
->intval
= bat
->level
== 100000 ?
210 POWER_SUPPLY_STATUS_FULL
: bat
->status
;
212 val
->intval
= bat
->status
;
214 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
215 val
->intval
= 100000;
217 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
:
220 case POWER_SUPPLY_PROP_CHARGE_NOW
:
221 val
->intval
= bat
->level
;
223 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
224 val
->intval
= bat
->volt_value
;
226 case POWER_SUPPLY_PROP_CURRENT_NOW
:
227 val
->intval
= bat
->cur_value
;
234 static const struct power_supply_desc main_bat_desc
= {
235 .name
= "main-battery",
236 .type
= POWER_SUPPLY_TYPE_BATTERY
,
237 .properties
= s3c_adc_main_bat_props
,
238 .num_properties
= ARRAY_SIZE(s3c_adc_main_bat_props
),
239 .get_property
= s3c_adc_bat_get_property
,
240 .external_power_changed
= s3c_adc_bat_ext_power_changed
,
244 static struct s3c_adc_bat main_bat
;
246 static void s3c_adc_bat_work(struct work_struct
*work
)
248 struct s3c_adc_bat
*bat
= &main_bat
;
251 static int was_plugged
;
253 is_plugged
= power_supply_am_i_supplied(bat
->psy
);
254 bat
->cable_plugged
= is_plugged
;
255 if (is_plugged
!= was_plugged
) {
256 was_plugged
= is_plugged
;
258 if (bat
->pdata
->enable_charger
)
259 bat
->pdata
->enable_charger();
260 bat
->status
= POWER_SUPPLY_STATUS_CHARGING
;
262 if (bat
->pdata
->disable_charger
)
263 bat
->pdata
->disable_charger();
264 bat
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
267 if (bat
->charge_finished
&& is_plugged
) {
268 is_charged
= charge_finished(&main_bat
);
270 if (bat
->pdata
->disable_charger
)
271 bat
->pdata
->disable_charger();
272 bat
->status
= POWER_SUPPLY_STATUS_FULL
;
274 if (bat
->pdata
->enable_charger
)
275 bat
->pdata
->enable_charger();
276 bat
->status
= POWER_SUPPLY_STATUS_CHARGING
;
281 power_supply_changed(bat
->psy
);
284 static irqreturn_t
s3c_adc_bat_charged(int irq
, void *dev_id
)
286 schedule_delayed_work(&bat_work
,
287 msecs_to_jiffies(JITTER_DELAY
));
291 static int s3c_adc_bat_probe(struct platform_device
*pdev
)
293 struct s3c_adc_client
*client
;
294 struct s3c_adc_bat_pdata
*pdata
= pdev
->dev
.platform_data
;
295 struct power_supply_config psy_cfg
= {};
296 struct gpio_desc
*gpiod
;
299 client
= s3c_adc_register(pdev
, NULL
, NULL
, 0);
300 if (IS_ERR(client
)) {
301 dev_err(&pdev
->dev
, "cannot register adc\n");
302 return PTR_ERR(client
);
305 platform_set_drvdata(pdev
, client
);
307 gpiod
= devm_gpiod_get_optional(&pdev
->dev
, "charge-status", GPIOD_IN
);
309 /* Could be probe deferral etc */
310 ret
= PTR_ERR(gpiod
);
311 dev_err(&pdev
->dev
, "no GPIO %d\n", ret
);
315 main_bat
.client
= client
;
316 main_bat
.pdata
= pdata
;
317 main_bat
.charge_finished
= gpiod
;
318 main_bat
.volt_value
= -1;
319 main_bat
.cur_value
= -1;
320 main_bat
.cable_plugged
= 0;
321 main_bat
.status
= POWER_SUPPLY_STATUS_DISCHARGING
;
322 psy_cfg
.drv_data
= &main_bat
;
324 main_bat
.psy
= power_supply_register(&pdev
->dev
, &main_bat_desc
, &psy_cfg
);
325 if (IS_ERR(main_bat
.psy
)) {
326 ret
= PTR_ERR(main_bat
.psy
);
329 if (pdata
->backup_volt_mult
) {
330 const struct power_supply_config backup_psy_cfg
331 = { .drv_data
= &backup_bat
, };
333 backup_bat
.client
= client
;
334 backup_bat
.pdata
= pdev
->dev
.platform_data
;
335 backup_bat
.charge_finished
= gpiod
;
336 backup_bat
.volt_value
= -1;
337 backup_bat
.psy
= power_supply_register(&pdev
->dev
,
340 if (IS_ERR(backup_bat
.psy
)) {
341 ret
= PTR_ERR(backup_bat
.psy
);
346 INIT_DELAYED_WORK(&bat_work
, s3c_adc_bat_work
);
349 ret
= request_irq(gpiod_to_irq(gpiod
),
351 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
352 "battery charged", NULL
);
363 dev_info(&pdev
->dev
, "successfully loaded\n");
364 device_init_wakeup(&pdev
->dev
, 1);
366 /* Schedule timer to check current status */
367 schedule_delayed_work(&bat_work
,
368 msecs_to_jiffies(JITTER_DELAY
));
374 free_irq(gpiod_to_irq(gpiod
), NULL
);
376 if (pdata
->backup_volt_mult
)
377 power_supply_unregister(backup_bat
.psy
);
379 power_supply_unregister(main_bat
.psy
);
384 static int s3c_adc_bat_remove(struct platform_device
*pdev
)
386 struct s3c_adc_client
*client
= platform_get_drvdata(pdev
);
387 struct s3c_adc_bat_pdata
*pdata
= pdev
->dev
.platform_data
;
389 power_supply_unregister(main_bat
.psy
);
390 if (pdata
->backup_volt_mult
)
391 power_supply_unregister(backup_bat
.psy
);
393 s3c_adc_release(client
);
395 if (main_bat
.charge_finished
)
396 free_irq(gpiod_to_irq(main_bat
.charge_finished
), NULL
);
398 cancel_delayed_work(&bat_work
);
407 static int s3c_adc_bat_suspend(struct platform_device
*pdev
,
410 struct s3c_adc_bat_pdata
*pdata
= pdev
->dev
.platform_data
;
412 if (main_bat
.charge_finished
) {
413 if (device_may_wakeup(&pdev
->dev
))
415 gpiod_to_irq(main_bat
.charge_finished
));
417 disable_irq(gpiod_to_irq(main_bat
.charge_finished
));
418 main_bat
.pdata
->disable_charger();
425 static int s3c_adc_bat_resume(struct platform_device
*pdev
)
427 struct s3c_adc_bat_pdata
*pdata
= pdev
->dev
.platform_data
;
429 if (main_bat
.charge_finished
) {
430 if (device_may_wakeup(&pdev
->dev
))
432 gpiod_to_irq(main_bat
.charge_finished
));
434 enable_irq(gpiod_to_irq(main_bat
.charge_finished
));
437 /* Schedule timer to check current status */
438 schedule_delayed_work(&bat_work
,
439 msecs_to_jiffies(JITTER_DELAY
));
444 #define s3c_adc_bat_suspend NULL
445 #define s3c_adc_bat_resume NULL
448 static struct platform_driver s3c_adc_bat_driver
= {
450 .name
= "s3c-adc-battery",
452 .probe
= s3c_adc_bat_probe
,
453 .remove
= s3c_adc_bat_remove
,
454 .suspend
= s3c_adc_bat_suspend
,
455 .resume
= s3c_adc_bat_resume
,
458 module_platform_driver(s3c_adc_bat_driver
);
460 MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
461 MODULE_DESCRIPTION("iPAQ H1930/H1940/RX1950 battery controller driver");
462 MODULE_LICENSE("GPL");