1 // SPDX-License-Identifier: GPL-2.0-only
3 * Battery and Power Management code for the Sharp SL-5x00
5 * Copyright (C) 2009 Thomas Kunze
7 * based on tosa_battery.c
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/power_supply.h>
12 #include <linux/delay.h>
13 #include <linux/spinlock.h>
14 #include <linux/interrupt.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/gpio/machine.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/mfd/ucb1x00.h>
20 #include <asm/mach/sharpsl_param.h>
21 #include <asm/mach-types.h>
22 #include <mach/collie.h>
24 static DEFINE_MUTEX(bat_lock
); /* protects gpio pins */
25 static struct work_struct bat_work
;
26 static struct ucb1x00
*ucb
;
30 struct power_supply
*psy
;
33 struct mutex work_lock
; /* protects data */
35 bool (*is_present
)(struct collie_bat
*bat
);
36 struct gpio_desc
*gpio_full
;
37 struct gpio_desc
*gpio_charge_on
;
41 struct gpio_desc
*gpio_bat
;
47 struct gpio_desc
*gpio_temp
;
52 static struct collie_bat collie_bat_main
;
54 static unsigned long collie_read_bat(struct collie_bat
*bat
)
56 unsigned long value
= 0;
58 if (!bat
->gpio_bat
|| bat
->adc_bat
< 0)
60 mutex_lock(&bat_lock
);
61 gpiod_set_value(bat
->gpio_bat
, 1);
63 ucb1x00_adc_enable(ucb
);
64 value
= ucb1x00_adc_read(ucb
, bat
->adc_bat
, UCB_SYNC
);
65 ucb1x00_adc_disable(ucb
);
66 gpiod_set_value(bat
->gpio_bat
, 0);
67 mutex_unlock(&bat_lock
);
68 value
= value
* 1000000 / bat
->adc_bat_divider
;
73 static unsigned long collie_read_temp(struct collie_bat
*bat
)
75 unsigned long value
= 0;
76 if (!bat
->gpio_temp
|| bat
->adc_temp
< 0)
79 mutex_lock(&bat_lock
);
80 gpiod_set_value(bat
->gpio_temp
, 1);
82 ucb1x00_adc_enable(ucb
);
83 value
= ucb1x00_adc_read(ucb
, bat
->adc_temp
, UCB_SYNC
);
84 ucb1x00_adc_disable(ucb
);
85 gpiod_set_value(bat
->gpio_temp
, 0);
86 mutex_unlock(&bat_lock
);
88 value
= value
* 10000 / bat
->adc_temp_divider
;
93 static int collie_bat_get_property(struct power_supply
*psy
,
94 enum power_supply_property psp
,
95 union power_supply_propval
*val
)
98 struct collie_bat
*bat
= power_supply_get_drvdata(psy
);
100 if (bat
->is_present
&& !bat
->is_present(bat
)
101 && psp
!= POWER_SUPPLY_PROP_PRESENT
) {
106 case POWER_SUPPLY_PROP_STATUS
:
107 val
->intval
= bat
->status
;
109 case POWER_SUPPLY_PROP_TECHNOLOGY
:
110 val
->intval
= bat
->technology
;
112 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
113 val
->intval
= collie_read_bat(bat
);
115 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
116 if (bat
->full_chrg
== -1)
117 val
->intval
= bat
->bat_max
;
119 val
->intval
= bat
->full_chrg
;
121 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
122 val
->intval
= bat
->bat_max
;
124 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
125 val
->intval
= bat
->bat_min
;
127 case POWER_SUPPLY_PROP_TEMP
:
128 val
->intval
= collie_read_temp(bat
);
130 case POWER_SUPPLY_PROP_PRESENT
:
131 val
->intval
= bat
->is_present
? bat
->is_present(bat
) : 1;
140 static void collie_bat_external_power_changed(struct power_supply
*psy
)
142 schedule_work(&bat_work
);
145 static irqreturn_t
collie_bat_gpio_isr(int irq
, void *data
)
147 pr_info("collie_bat_gpio irq\n");
148 schedule_work(&bat_work
);
152 static void collie_bat_update(struct collie_bat
*bat
)
155 struct power_supply
*psy
= bat
->psy
;
157 mutex_lock(&bat
->work_lock
);
161 if (bat
->is_present
&& !bat
->is_present(bat
)) {
162 printk(KERN_NOTICE
"%s not present\n", psy
->desc
->name
);
163 bat
->status
= POWER_SUPPLY_STATUS_UNKNOWN
;
165 } else if (power_supply_am_i_supplied(psy
)) {
166 if (bat
->status
== POWER_SUPPLY_STATUS_DISCHARGING
) {
167 gpiod_set_value(bat
->gpio_charge_on
, 1);
171 if (gpiod_get_value(bat
->gpio_full
)) {
172 if (old
== POWER_SUPPLY_STATUS_CHARGING
||
173 bat
->full_chrg
== -1)
174 bat
->full_chrg
= collie_read_bat(bat
);
176 gpiod_set_value(bat
->gpio_charge_on
, 0);
177 bat
->status
= POWER_SUPPLY_STATUS_FULL
;
179 gpiod_set_value(bat
->gpio_charge_on
, 1);
180 bat
->status
= POWER_SUPPLY_STATUS_CHARGING
;
183 gpiod_set_value(bat
->gpio_charge_on
, 0);
184 bat
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
187 if (old
!= bat
->status
)
188 power_supply_changed(psy
);
190 mutex_unlock(&bat
->work_lock
);
193 static void collie_bat_work(struct work_struct
*work
)
195 collie_bat_update(&collie_bat_main
);
199 static enum power_supply_property collie_bat_main_props
[] = {
200 POWER_SUPPLY_PROP_STATUS
,
201 POWER_SUPPLY_PROP_TECHNOLOGY
,
202 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
203 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
204 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
205 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
206 POWER_SUPPLY_PROP_PRESENT
,
207 POWER_SUPPLY_PROP_TEMP
,
210 static enum power_supply_property collie_bat_bu_props
[] = {
211 POWER_SUPPLY_PROP_STATUS
,
212 POWER_SUPPLY_PROP_TECHNOLOGY
,
213 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
214 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
215 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
216 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
217 POWER_SUPPLY_PROP_PRESENT
,
220 static const struct power_supply_desc collie_bat_main_desc
= {
221 .name
= "main-battery",
222 .type
= POWER_SUPPLY_TYPE_BATTERY
,
223 .properties
= collie_bat_main_props
,
224 .num_properties
= ARRAY_SIZE(collie_bat_main_props
),
225 .get_property
= collie_bat_get_property
,
226 .external_power_changed
= collie_bat_external_power_changed
,
230 static struct collie_bat collie_bat_main
= {
231 .status
= POWER_SUPPLY_STATUS_DISCHARGING
,
236 .gpio_charge_on
= NULL
,
238 .technology
= POWER_SUPPLY_TECHNOLOGY_LIPO
,
241 .adc_bat
= UCB_ADC_INP_AD1
,
242 .adc_bat_divider
= 155,
244 .bat_min
= 1551 * 1000000 / 414,
247 .adc_temp
= UCB_ADC_INP_AD0
,
248 .adc_temp_divider
= 10000,
251 static const struct power_supply_desc collie_bat_bu_desc
= {
252 .name
= "backup-battery",
253 .type
= POWER_SUPPLY_TYPE_BATTERY
,
254 .properties
= collie_bat_bu_props
,
255 .num_properties
= ARRAY_SIZE(collie_bat_bu_props
),
256 .get_property
= collie_bat_get_property
,
257 .external_power_changed
= collie_bat_external_power_changed
,
260 static struct collie_bat collie_bat_bu
= {
261 .status
= POWER_SUPPLY_STATUS_UNKNOWN
,
266 .gpio_charge_on
= NULL
,
268 .technology
= POWER_SUPPLY_TECHNOLOGY_LiMn
,
271 .adc_bat
= UCB_ADC_INP_AD1
,
272 .adc_bat_divider
= 155,
278 .adc_temp_divider
= -1,
281 /* Obtained but unused GPIO */
282 static struct gpio_desc
*collie_mbat_low
;
285 static int wakeup_enabled
;
287 static int collie_bat_suspend(struct ucb1x00_dev
*dev
)
289 /* flush all pending status updates */
290 flush_work(&bat_work
);
292 if (device_may_wakeup(&dev
->ucb
->dev
) &&
293 collie_bat_main
.status
== POWER_SUPPLY_STATUS_CHARGING
)
294 wakeup_enabled
= !enable_irq_wake(gpiod_to_irq(collie_bat_main
.gpio_full
));
301 static int collie_bat_resume(struct ucb1x00_dev
*dev
)
304 disable_irq_wake(gpiod_to_irq(collie_bat_main
.gpio_full
));
306 /* things may have changed while we were away */
307 schedule_work(&bat_work
);
311 #define collie_bat_suspend NULL
312 #define collie_bat_resume NULL
315 static int collie_bat_probe(struct ucb1x00_dev
*dev
)
318 struct power_supply_config psy_main_cfg
= {}, psy_bu_cfg
= {};
319 struct gpio_chip
*gc
= &dev
->ucb
->gpio
;
321 if (!machine_is_collie())
326 /* Obtain all the main battery GPIOs */
327 collie_bat_main
.gpio_full
= gpiod_get(&dev
->ucb
->dev
,
330 if (IS_ERR(collie_bat_main
.gpio_full
))
331 return PTR_ERR(collie_bat_main
.gpio_full
);
333 collie_mbat_low
= gpiod_get(&dev
->ucb
->dev
,
336 if (IS_ERR(collie_mbat_low
)) {
337 ret
= PTR_ERR(collie_mbat_low
);
338 goto err_put_gpio_full
;
341 collie_bat_main
.gpio_charge_on
= gpiod_get(&dev
->ucb
->dev
,
344 if (IS_ERR(collie_bat_main
.gpio_charge_on
)) {
345 ret
= PTR_ERR(collie_bat_main
.gpio_charge_on
);
346 goto err_put_mbat_low
;
349 /* COLLIE_GPIO_MBAT_ON = GPIO 7 on the UCB (TC35143) */
350 collie_bat_main
.gpio_bat
= gpiochip_request_own_desc(gc
,
355 if (IS_ERR(collie_bat_main
.gpio_bat
)) {
356 ret
= PTR_ERR(collie_bat_main
.gpio_bat
);
357 goto err_put_gpio_charge_on
;
360 /* COLLIE_GPIO_TMP_ON = GPIO 9 on the UCB (TC35143) */
361 collie_bat_main
.gpio_temp
= gpiochip_request_own_desc(gc
,
366 if (IS_ERR(collie_bat_main
.gpio_temp
)) {
367 ret
= PTR_ERR(collie_bat_main
.gpio_temp
);
368 goto err_free_gpio_bat
;
372 * Obtain the backup battery COLLIE_GPIO_BBAT_ON which is
373 * GPIO 8 on the UCB (TC35143)
375 collie_bat_bu
.gpio_bat
= gpiochip_request_own_desc(gc
,
380 if (IS_ERR(collie_bat_bu
.gpio_bat
)) {
381 ret
= PTR_ERR(collie_bat_bu
.gpio_bat
);
382 goto err_free_gpio_temp
;
385 mutex_init(&collie_bat_main
.work_lock
);
387 INIT_WORK(&bat_work
, collie_bat_work
);
389 psy_main_cfg
.drv_data
= &collie_bat_main
;
390 collie_bat_main
.psy
= power_supply_register(&dev
->ucb
->dev
,
391 &collie_bat_main_desc
,
393 if (IS_ERR(collie_bat_main
.psy
)) {
394 ret
= PTR_ERR(collie_bat_main
.psy
);
395 goto err_psy_reg_main
;
398 psy_bu_cfg
.drv_data
= &collie_bat_bu
;
399 collie_bat_bu
.psy
= power_supply_register(&dev
->ucb
->dev
,
402 if (IS_ERR(collie_bat_bu
.psy
)) {
403 ret
= PTR_ERR(collie_bat_bu
.psy
);
407 ret
= request_irq(gpio_to_irq(COLLIE_GPIO_CO
),
409 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
410 "main full", &collie_bat_main
);
414 device_init_wakeup(&ucb
->dev
, 1);
415 schedule_work(&bat_work
);
420 power_supply_unregister(collie_bat_bu
.psy
);
422 power_supply_unregister(collie_bat_main
.psy
);
424 /* see comment in collie_bat_remove */
425 cancel_work_sync(&bat_work
);
426 gpiochip_free_own_desc(collie_bat_bu
.gpio_bat
);
428 gpiochip_free_own_desc(collie_bat_main
.gpio_temp
);
430 gpiochip_free_own_desc(collie_bat_main
.gpio_bat
);
431 err_put_gpio_charge_on
:
432 gpiod_put(collie_bat_main
.gpio_charge_on
);
434 gpiod_put(collie_mbat_low
);
436 gpiod_put(collie_bat_main
.gpio_full
);
441 static void collie_bat_remove(struct ucb1x00_dev
*dev
)
443 free_irq(gpio_to_irq(COLLIE_GPIO_CO
), &collie_bat_main
);
444 power_supply_unregister(collie_bat_bu
.psy
);
445 power_supply_unregister(collie_bat_main
.psy
);
447 /* These are obtained from the machine */
448 gpiod_put(collie_bat_main
.gpio_full
);
449 gpiod_put(collie_mbat_low
);
450 gpiod_put(collie_bat_main
.gpio_charge_on
);
451 /* These are directly from the UCB so let's free them */
452 gpiochip_free_own_desc(collie_bat_main
.gpio_bat
);
453 gpiochip_free_own_desc(collie_bat_main
.gpio_temp
);
454 gpiochip_free_own_desc(collie_bat_bu
.gpio_bat
);
456 * Now cancel the bat_work. We won't get any more schedules,
457 * since all sources (isr and external_power_changed) are
460 cancel_work_sync(&bat_work
);
463 static struct ucb1x00_driver collie_bat_driver
= {
464 .add
= collie_bat_probe
,
465 .remove
= collie_bat_remove
,
466 .suspend
= collie_bat_suspend
,
467 .resume
= collie_bat_resume
,
470 static int __init
collie_bat_init(void)
472 return ucb1x00_register_driver(&collie_bat_driver
);
475 static void __exit
collie_bat_exit(void)
477 ucb1x00_unregister_driver(&collie_bat_driver
);
480 module_init(collie_bat_init
);
481 module_exit(collie_bat_exit
);
483 MODULE_LICENSE("GPL");
484 MODULE_AUTHOR("Thomas Kunze");
485 MODULE_DESCRIPTION("Collie battery driver");