ARM: pmu: add support for interrupt-affinity property
[linux/fpc-iii.git] / drivers / power / collie_battery.c
blob594e4dbc2d51030df093e021df20f9d3a3352aca
1 /*
2 * Battery and Power Management code for the Sharp SL-5x00
4 * Copyright (C) 2009 Thomas Kunze
6 * based on tosa_battery.c
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/power_supply.h>
16 #include <linux/delay.h>
17 #include <linux/spinlock.h>
18 #include <linux/interrupt.h>
19 #include <linux/gpio.h>
20 #include <linux/mfd/ucb1x00.h>
22 #include <asm/mach/sharpsl_param.h>
23 #include <asm/mach-types.h>
24 #include <mach/collie.h>
26 static DEFINE_MUTEX(bat_lock); /* protects gpio pins */
27 static struct work_struct bat_work;
28 static struct ucb1x00 *ucb;
29 static int wakeup_enabled;
31 struct collie_bat {
32 int status;
33 struct power_supply psy;
34 int full_chrg;
36 struct mutex work_lock; /* protects data */
38 bool (*is_present)(struct collie_bat *bat);
39 int gpio_full;
40 int gpio_charge_on;
42 int technology;
44 int gpio_bat;
45 int adc_bat;
46 int adc_bat_divider;
47 int bat_max;
48 int bat_min;
50 int gpio_temp;
51 int adc_temp;
52 int adc_temp_divider;
55 static struct collie_bat collie_bat_main;
57 static unsigned long collie_read_bat(struct collie_bat *bat)
59 unsigned long value = 0;
61 if (bat->gpio_bat < 0 || bat->adc_bat < 0)
62 return 0;
63 mutex_lock(&bat_lock);
64 gpio_set_value(bat->gpio_bat, 1);
65 msleep(5);
66 ucb1x00_adc_enable(ucb);
67 value = ucb1x00_adc_read(ucb, bat->adc_bat, UCB_SYNC);
68 ucb1x00_adc_disable(ucb);
69 gpio_set_value(bat->gpio_bat, 0);
70 mutex_unlock(&bat_lock);
71 value = value * 1000000 / bat->adc_bat_divider;
73 return value;
76 static unsigned long collie_read_temp(struct collie_bat *bat)
78 unsigned long value = 0;
79 if (bat->gpio_temp < 0 || bat->adc_temp < 0)
80 return 0;
82 mutex_lock(&bat_lock);
83 gpio_set_value(bat->gpio_temp, 1);
84 msleep(5);
85 ucb1x00_adc_enable(ucb);
86 value = ucb1x00_adc_read(ucb, bat->adc_temp, UCB_SYNC);
87 ucb1x00_adc_disable(ucb);
88 gpio_set_value(bat->gpio_temp, 0);
89 mutex_unlock(&bat_lock);
91 value = value * 10000 / bat->adc_temp_divider;
93 return value;
96 static int collie_bat_get_property(struct power_supply *psy,
97 enum power_supply_property psp,
98 union power_supply_propval *val)
100 int ret = 0;
101 struct collie_bat *bat = container_of(psy, struct collie_bat, psy);
103 if (bat->is_present && !bat->is_present(bat)
104 && psp != POWER_SUPPLY_PROP_PRESENT) {
105 return -ENODEV;
108 switch (psp) {
109 case POWER_SUPPLY_PROP_STATUS:
110 val->intval = bat->status;
111 break;
112 case POWER_SUPPLY_PROP_TECHNOLOGY:
113 val->intval = bat->technology;
114 break;
115 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
116 val->intval = collie_read_bat(bat);
117 break;
118 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
119 if (bat->full_chrg == -1)
120 val->intval = bat->bat_max;
121 else
122 val->intval = bat->full_chrg;
123 break;
124 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
125 val->intval = bat->bat_max;
126 break;
127 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
128 val->intval = bat->bat_min;
129 break;
130 case POWER_SUPPLY_PROP_TEMP:
131 val->intval = collie_read_temp(bat);
132 break;
133 case POWER_SUPPLY_PROP_PRESENT:
134 val->intval = bat->is_present ? bat->is_present(bat) : 1;
135 break;
136 default:
137 ret = -EINVAL;
138 break;
140 return ret;
143 static void collie_bat_external_power_changed(struct power_supply *psy)
145 schedule_work(&bat_work);
148 static irqreturn_t collie_bat_gpio_isr(int irq, void *data)
150 pr_info("collie_bat_gpio irq\n");
151 schedule_work(&bat_work);
152 return IRQ_HANDLED;
155 static void collie_bat_update(struct collie_bat *bat)
157 int old;
158 struct power_supply *psy = &bat->psy;
160 mutex_lock(&bat->work_lock);
162 old = bat->status;
164 if (bat->is_present && !bat->is_present(bat)) {
165 printk(KERN_NOTICE "%s not present\n", psy->name);
166 bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
167 bat->full_chrg = -1;
168 } else if (power_supply_am_i_supplied(psy)) {
169 if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING) {
170 gpio_set_value(bat->gpio_charge_on, 1);
171 mdelay(15);
174 if (gpio_get_value(bat->gpio_full)) {
175 if (old == POWER_SUPPLY_STATUS_CHARGING ||
176 bat->full_chrg == -1)
177 bat->full_chrg = collie_read_bat(bat);
179 gpio_set_value(bat->gpio_charge_on, 0);
180 bat->status = POWER_SUPPLY_STATUS_FULL;
181 } else {
182 gpio_set_value(bat->gpio_charge_on, 1);
183 bat->status = POWER_SUPPLY_STATUS_CHARGING;
185 } else {
186 gpio_set_value(bat->gpio_charge_on, 0);
187 bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
190 if (old != bat->status)
191 power_supply_changed(psy);
193 mutex_unlock(&bat->work_lock);
196 static void collie_bat_work(struct work_struct *work)
198 collie_bat_update(&collie_bat_main);
202 static enum power_supply_property collie_bat_main_props[] = {
203 POWER_SUPPLY_PROP_STATUS,
204 POWER_SUPPLY_PROP_TECHNOLOGY,
205 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
206 POWER_SUPPLY_PROP_VOLTAGE_NOW,
207 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
208 POWER_SUPPLY_PROP_VOLTAGE_MAX,
209 POWER_SUPPLY_PROP_PRESENT,
210 POWER_SUPPLY_PROP_TEMP,
213 static enum power_supply_property collie_bat_bu_props[] = {
214 POWER_SUPPLY_PROP_STATUS,
215 POWER_SUPPLY_PROP_TECHNOLOGY,
216 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
217 POWER_SUPPLY_PROP_VOLTAGE_NOW,
218 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
219 POWER_SUPPLY_PROP_VOLTAGE_MAX,
220 POWER_SUPPLY_PROP_PRESENT,
223 static struct collie_bat collie_bat_main = {
224 .status = POWER_SUPPLY_STATUS_DISCHARGING,
225 .full_chrg = -1,
226 .psy = {
227 .name = "main-battery",
228 .type = POWER_SUPPLY_TYPE_BATTERY,
229 .properties = collie_bat_main_props,
230 .num_properties = ARRAY_SIZE(collie_bat_main_props),
231 .get_property = collie_bat_get_property,
232 .external_power_changed = collie_bat_external_power_changed,
233 .use_for_apm = 1,
236 .gpio_full = COLLIE_GPIO_CO,
237 .gpio_charge_on = COLLIE_GPIO_CHARGE_ON,
239 .technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
241 .gpio_bat = COLLIE_GPIO_MBAT_ON,
242 .adc_bat = UCB_ADC_INP_AD1,
243 .adc_bat_divider = 155,
244 .bat_max = 4310000,
245 .bat_min = 1551 * 1000000 / 414,
247 .gpio_temp = COLLIE_GPIO_TMP_ON,
248 .adc_temp = UCB_ADC_INP_AD0,
249 .adc_temp_divider = 10000,
252 static struct collie_bat collie_bat_bu = {
253 .status = POWER_SUPPLY_STATUS_UNKNOWN,
254 .full_chrg = -1,
256 .psy = {
257 .name = "backup-battery",
258 .type = POWER_SUPPLY_TYPE_BATTERY,
259 .properties = collie_bat_bu_props,
260 .num_properties = ARRAY_SIZE(collie_bat_bu_props),
261 .get_property = collie_bat_get_property,
262 .external_power_changed = collie_bat_external_power_changed,
265 .gpio_full = -1,
266 .gpio_charge_on = -1,
268 .technology = POWER_SUPPLY_TECHNOLOGY_LiMn,
270 .gpio_bat = COLLIE_GPIO_BBAT_ON,
271 .adc_bat = UCB_ADC_INP_AD1,
272 .adc_bat_divider = 155,
273 .bat_max = 3000000,
274 .bat_min = 1900000,
276 .gpio_temp = -1,
277 .adc_temp = -1,
278 .adc_temp_divider = -1,
281 static struct gpio collie_batt_gpios[] = {
282 { COLLIE_GPIO_CO, GPIOF_IN, "main battery full" },
283 { COLLIE_GPIO_MAIN_BAT_LOW, GPIOF_IN, "main battery low" },
284 { COLLIE_GPIO_CHARGE_ON, GPIOF_OUT_INIT_LOW, "main charge on" },
285 { COLLIE_GPIO_MBAT_ON, GPIOF_OUT_INIT_LOW, "main battery" },
286 { COLLIE_GPIO_TMP_ON, GPIOF_OUT_INIT_LOW, "main battery temp" },
287 { COLLIE_GPIO_BBAT_ON, GPIOF_OUT_INIT_LOW, "backup battery" },
290 #ifdef CONFIG_PM
291 static int collie_bat_suspend(struct ucb1x00_dev *dev)
293 /* flush all pending status updates */
294 flush_work(&bat_work);
296 if (device_may_wakeup(&dev->ucb->dev) &&
297 collie_bat_main.status == POWER_SUPPLY_STATUS_CHARGING)
298 wakeup_enabled = !enable_irq_wake(gpio_to_irq(COLLIE_GPIO_CO));
299 else
300 wakeup_enabled = 0;
302 return 0;
305 static int collie_bat_resume(struct ucb1x00_dev *dev)
307 if (wakeup_enabled)
308 disable_irq_wake(gpio_to_irq(COLLIE_GPIO_CO));
310 /* things may have changed while we were away */
311 schedule_work(&bat_work);
312 return 0;
314 #else
315 #define collie_bat_suspend NULL
316 #define collie_bat_resume NULL
317 #endif
319 static int collie_bat_probe(struct ucb1x00_dev *dev)
321 int ret;
323 if (!machine_is_collie())
324 return -ENODEV;
326 ucb = dev->ucb;
328 ret = gpio_request_array(collie_batt_gpios,
329 ARRAY_SIZE(collie_batt_gpios));
330 if (ret)
331 return ret;
333 mutex_init(&collie_bat_main.work_lock);
335 INIT_WORK(&bat_work, collie_bat_work);
337 ret = power_supply_register(&dev->ucb->dev, &collie_bat_main.psy);
338 if (ret)
339 goto err_psy_reg_main;
340 ret = power_supply_register(&dev->ucb->dev, &collie_bat_bu.psy);
341 if (ret)
342 goto err_psy_reg_bu;
344 ret = request_irq(gpio_to_irq(COLLIE_GPIO_CO),
345 collie_bat_gpio_isr,
346 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
347 "main full", &collie_bat_main);
348 if (ret)
349 goto err_irq;
351 device_init_wakeup(&ucb->dev, 1);
352 schedule_work(&bat_work);
354 return 0;
356 err_irq:
357 power_supply_unregister(&collie_bat_bu.psy);
358 err_psy_reg_bu:
359 power_supply_unregister(&collie_bat_main.psy);
360 err_psy_reg_main:
362 /* see comment in collie_bat_remove */
363 cancel_work_sync(&bat_work);
364 gpio_free_array(collie_batt_gpios, ARRAY_SIZE(collie_batt_gpios));
365 return ret;
368 static void collie_bat_remove(struct ucb1x00_dev *dev)
370 free_irq(gpio_to_irq(COLLIE_GPIO_CO), &collie_bat_main);
372 power_supply_unregister(&collie_bat_bu.psy);
373 power_supply_unregister(&collie_bat_main.psy);
376 * Now cancel the bat_work. We won't get any more schedules,
377 * since all sources (isr and external_power_changed) are
378 * unregistered now.
380 cancel_work_sync(&bat_work);
381 gpio_free_array(collie_batt_gpios, ARRAY_SIZE(collie_batt_gpios));
384 static struct ucb1x00_driver collie_bat_driver = {
385 .add = collie_bat_probe,
386 .remove = collie_bat_remove,
387 .suspend = collie_bat_suspend,
388 .resume = collie_bat_resume,
391 static int __init collie_bat_init(void)
393 return ucb1x00_register_driver(&collie_bat_driver);
396 static void __exit collie_bat_exit(void)
398 ucb1x00_unregister_driver(&collie_bat_driver);
401 module_init(collie_bat_init);
402 module_exit(collie_bat_exit);
404 MODULE_LICENSE("GPL");
405 MODULE_AUTHOR("Thomas Kunze");
406 MODULE_DESCRIPTION("Collie battery driver");