]>
Commit | Line | Data |
---|---|---|
f1fce597 TK |
1 | /* |
2 | * Battery and Power Management code for the Sharp SL-5x00 | |
3 | * | |
4 | * Copyright (C) 2009 Thomas Kunze | |
5 | * | |
6 | * based on tosa_battery.c | |
7 | * | |
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. | |
11 | * | |
12 | */ | |
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> | |
21 | ||
22 | #include <asm/mach/sharpsl_param.h> | |
23 | #include <asm/mach-types.h> | |
24 | #include <mach/collie.h> | |
25 | ||
26 | static DEFINE_MUTEX(bat_lock); /* protects gpio pins */ | |
27 | static struct work_struct bat_work; | |
28 | static struct ucb1x00 *ucb; | |
f1300e7f | 29 | static int wakeup_enabled; |
f1fce597 TK |
30 | |
31 | struct collie_bat { | |
32 | int status; | |
33 | struct power_supply psy; | |
34 | int full_chrg; | |
35 | ||
36 | struct mutex work_lock; /* protects data */ | |
37 | ||
38 | bool (*is_present)(struct collie_bat *bat); | |
39 | int gpio_full; | |
40 | int gpio_charge_on; | |
41 | ||
42 | int technology; | |
43 | ||
44 | int gpio_bat; | |
45 | int adc_bat; | |
46 | int adc_bat_divider; | |
47 | int bat_max; | |
48 | int bat_min; | |
49 | ||
50 | int gpio_temp; | |
51 | int adc_temp; | |
52 | int adc_temp_divider; | |
53 | }; | |
54 | ||
55 | static struct collie_bat collie_bat_main; | |
56 | ||
57 | static unsigned long collie_read_bat(struct collie_bat *bat) | |
58 | { | |
59 | unsigned long value = 0; | |
60 | ||
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; | |
72 | ||
73 | return value; | |
74 | } | |
75 | ||
76 | static unsigned long collie_read_temp(struct collie_bat *bat) | |
77 | { | |
78 | unsigned long value = 0; | |
79 | if (bat->gpio_temp < 0 || bat->adc_temp < 0) | |
80 | return 0; | |
81 | ||
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); | |
90 | ||
91 | value = value * 10000 / bat->adc_temp_divider; | |
92 | ||
93 | return value; | |
94 | } | |
95 | ||
96 | static int collie_bat_get_property(struct power_supply *psy, | |
97 | enum power_supply_property psp, | |
98 | union power_supply_propval *val) | |
99 | { | |
100 | int ret = 0; | |
101 | struct collie_bat *bat = container_of(psy, struct collie_bat, psy); | |
102 | ||
103 | if (bat->is_present && !bat->is_present(bat) | |
104 | && psp != POWER_SUPPLY_PROP_PRESENT) { | |
105 | return -ENODEV; | |
106 | } | |
107 | ||
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; | |
139 | } | |
140 | return ret; | |
141 | } | |
142 | ||
143 | static void collie_bat_external_power_changed(struct power_supply *psy) | |
144 | { | |
145 | schedule_work(&bat_work); | |
146 | } | |
147 | ||
148 | static irqreturn_t collie_bat_gpio_isr(int irq, void *data) | |
149 | { | |
629bcb4b | 150 | pr_info("collie_bat_gpio irq\n"); |
f1fce597 TK |
151 | schedule_work(&bat_work); |
152 | return IRQ_HANDLED; | |
153 | } | |
154 | ||
155 | static void collie_bat_update(struct collie_bat *bat) | |
156 | { | |
157 | int old; | |
158 | struct power_supply *psy = &bat->psy; | |
159 | ||
160 | mutex_lock(&bat->work_lock); | |
161 | ||
162 | old = bat->status; | |
163 | ||
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); | |
172 | } | |
173 | ||
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); | |
178 | ||
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; | |
184 | } | |
185 | } else { | |
186 | gpio_set_value(bat->gpio_charge_on, 0); | |
187 | bat->status = POWER_SUPPLY_STATUS_DISCHARGING; | |
188 | } | |
189 | ||
190 | if (old != bat->status) | |
191 | power_supply_changed(psy); | |
192 | ||
193 | mutex_unlock(&bat->work_lock); | |
194 | } | |
195 | ||
196 | static void collie_bat_work(struct work_struct *work) | |
197 | { | |
198 | collie_bat_update(&collie_bat_main); | |
199 | } | |
200 | ||
201 | ||
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, | |
211 | }; | |
212 | ||
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, | |
221 | }; | |
222 | ||
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, | |
234 | }, | |
235 | ||
236 | .gpio_full = COLLIE_GPIO_CO, | |
237 | .gpio_charge_on = COLLIE_GPIO_CHARGE_ON, | |
238 | ||
239 | .technology = POWER_SUPPLY_TECHNOLOGY_LIPO, | |
240 | ||
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, | |
246 | ||
247 | .gpio_temp = COLLIE_GPIO_TMP_ON, | |
248 | .adc_temp = UCB_ADC_INP_AD0, | |
249 | .adc_temp_divider = 10000, | |
250 | }; | |
251 | ||
252 | static struct collie_bat collie_bat_bu = { | |
253 | .status = POWER_SUPPLY_STATUS_UNKNOWN, | |
254 | .full_chrg = -1, | |
255 | ||
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, | |
263 | }, | |
264 | ||
265 | .gpio_full = -1, | |
266 | .gpio_charge_on = -1, | |
267 | ||
268 | .technology = POWER_SUPPLY_TECHNOLOGY_LiMn, | |
269 | ||
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, | |
275 | ||
276 | .gpio_temp = -1, | |
277 | .adc_temp = -1, | |
278 | .adc_temp_divider = -1, | |
279 | }; | |
280 | ||
389cd203 AL |
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" }, | |
f1fce597 TK |
288 | }; |
289 | ||
290 | #ifdef CONFIG_PM | |
1ba64114 | 291 | static int collie_bat_suspend(struct ucb1x00_dev *dev) |
f1fce597 TK |
292 | { |
293 | /* flush all pending status updates */ | |
43829731 | 294 | flush_work(&bat_work); |
f1300e7f DES |
295 | |
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; | |
301 | ||
f1fce597 TK |
302 | return 0; |
303 | } | |
304 | ||
305 | static int collie_bat_resume(struct ucb1x00_dev *dev) | |
306 | { | |
f1300e7f DES |
307 | if (wakeup_enabled) |
308 | disable_irq_wake(gpio_to_irq(COLLIE_GPIO_CO)); | |
309 | ||
f1fce597 TK |
310 | /* things may have changed while we were away */ |
311 | schedule_work(&bat_work); | |
312 | return 0; | |
313 | } | |
314 | #else | |
315 | #define collie_bat_suspend NULL | |
316 | #define collie_bat_resume NULL | |
317 | #endif | |
318 | ||
c8afa640 | 319 | static int collie_bat_probe(struct ucb1x00_dev *dev) |
f1fce597 TK |
320 | { |
321 | int ret; | |
f1fce597 TK |
322 | |
323 | if (!machine_is_collie()) | |
324 | return -ENODEV; | |
325 | ||
326 | ucb = dev->ucb; | |
327 | ||
389cd203 AL |
328 | ret = gpio_request_array(collie_batt_gpios, |
329 | ARRAY_SIZE(collie_batt_gpios)); | |
330 | if (ret) | |
331 | return ret; | |
f1fce597 TK |
332 | |
333 | mutex_init(&collie_bat_main.work_lock); | |
334 | ||
335 | INIT_WORK(&bat_work, collie_bat_work); | |
336 | ||
2dc9215d | 337 | ret = power_supply_register(&dev->ucb->dev, &collie_bat_main.psy, NULL); |
f1fce597 TK |
338 | if (ret) |
339 | goto err_psy_reg_main; | |
2dc9215d | 340 | ret = power_supply_register(&dev->ucb->dev, &collie_bat_bu.psy, NULL); |
f1fce597 TK |
341 | if (ret) |
342 | goto err_psy_reg_bu; | |
343 | ||
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); | |
f1300e7f DES |
348 | if (ret) |
349 | goto err_irq; | |
350 | ||
351 | device_init_wakeup(&ucb->dev, 1); | |
352 | schedule_work(&bat_work); | |
353 | ||
354 | return 0; | |
355 | ||
356 | err_irq: | |
f1fce597 TK |
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: | |
361 | ||
362 | /* see comment in collie_bat_remove */ | |
bc51e7ff | 363 | cancel_work_sync(&bat_work); |
389cd203 | 364 | gpio_free_array(collie_batt_gpios, ARRAY_SIZE(collie_batt_gpios)); |
f1fce597 TK |
365 | return ret; |
366 | } | |
367 | ||
415ec69f | 368 | static void collie_bat_remove(struct ucb1x00_dev *dev) |
f1fce597 | 369 | { |
f1fce597 TK |
370 | free_irq(gpio_to_irq(COLLIE_GPIO_CO), &collie_bat_main); |
371 | ||
372 | power_supply_unregister(&collie_bat_bu.psy); | |
373 | power_supply_unregister(&collie_bat_main.psy); | |
374 | ||
375 | /* | |
bc51e7ff TH |
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. | |
f1fce597 | 379 | */ |
bc51e7ff | 380 | cancel_work_sync(&bat_work); |
389cd203 | 381 | gpio_free_array(collie_batt_gpios, ARRAY_SIZE(collie_batt_gpios)); |
f1fce597 TK |
382 | } |
383 | ||
384 | static struct ucb1x00_driver collie_bat_driver = { | |
385 | .add = collie_bat_probe, | |
28ea73f4 | 386 | .remove = collie_bat_remove, |
f1fce597 TK |
387 | .suspend = collie_bat_suspend, |
388 | .resume = collie_bat_resume, | |
389 | }; | |
390 | ||
391 | static int __init collie_bat_init(void) | |
392 | { | |
393 | return ucb1x00_register_driver(&collie_bat_driver); | |
394 | } | |
395 | ||
396 | static void __exit collie_bat_exit(void) | |
397 | { | |
398 | ucb1x00_unregister_driver(&collie_bat_driver); | |
399 | } | |
400 | ||
401 | module_init(collie_bat_init); | |
402 | module_exit(collie_bat_exit); | |
403 | ||
404 | MODULE_LICENSE("GPL"); | |
405 | MODULE_AUTHOR("Thomas Kunze"); | |
406 | MODULE_DESCRIPTION("Collie battery driver"); |