Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / arch / arm / mach-pxa / sharpsl_pm.c
blob72fa2e3fd35318e5a63c121ed7990a56a56b134c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Battery and Power Management code for the Sharp SL-C7xx and SL-Cxx00
4 * series of PDAs
6 * Copyright (c) 2004-2005 Richard Purdie
8 * Based on code written by Sharp for 2.4 kernels
9 */
11 #undef DEBUG
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
17 #include <linux/apm-emulation.h>
18 #include <linux/timer.h>
19 #include <linux/delay.h>
20 #include <linux/leds.h>
21 #include <linux/suspend.h>
22 #include <linux/gpio.h>
23 #include <linux/io.h>
25 #include <asm/mach-types.h>
26 #include "pm.h"
27 #include "pxa2xx-regs.h"
28 #include "regs-rtc.h"
29 #include "sharpsl_pm.h"
32 * Constants
34 #define SHARPSL_CHARGE_ON_TIME_INTERVAL (msecs_to_jiffies(1*60*1000)) /* 1 min */
35 #define SHARPSL_CHARGE_FINISH_TIME (msecs_to_jiffies(10*60*1000)) /* 10 min */
36 #define SHARPSL_BATCHK_TIME (msecs_to_jiffies(15*1000)) /* 15 sec */
37 #define SHARPSL_BATCHK_TIME_SUSPEND (60*10) /* 10 min */
39 #define SHARPSL_WAIT_CO_TIME 15 /* 15 sec */
40 #define SHARPSL_WAIT_DISCHARGE_ON 100 /* 100 msec */
41 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP 10 /* 10 msec */
42 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT 10 /* 10 msec */
43 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN 10 /* 10 msec */
44 #define SHARPSL_CHARGE_WAIT_TIME 15 /* 15 msec */
45 #define SHARPSL_CHARGE_CO_CHECK_TIME 5 /* 5 msec */
46 #define SHARPSL_CHARGE_RETRY_CNT 1 /* eqv. 10 min */
49 * Prototypes
51 #ifdef CONFIG_PM
52 static int sharpsl_off_charge_battery(void);
53 static int sharpsl_check_battery_voltage(void);
54 #endif
55 static int sharpsl_check_battery_temp(void);
56 static int sharpsl_ac_check(void);
57 static int sharpsl_average_value(int ad);
58 static void sharpsl_average_clear(void);
59 static void sharpsl_charge_toggle(struct work_struct *private_);
60 static void sharpsl_battery_thread(struct work_struct *private_);
64 * Variables
66 struct sharpsl_pm_status sharpsl_pm;
67 static DECLARE_DELAYED_WORK(toggle_charger, sharpsl_charge_toggle);
68 static DECLARE_DELAYED_WORK(sharpsl_bat, sharpsl_battery_thread);
69 DEFINE_LED_TRIGGER(sharpsl_charge_led_trigger);
73 struct battery_thresh sharpsl_battery_levels_acin[] = {
74 { 213, 100},
75 { 212, 98},
76 { 211, 95},
77 { 210, 93},
78 { 209, 90},
79 { 208, 88},
80 { 207, 85},
81 { 206, 83},
82 { 205, 80},
83 { 204, 78},
84 { 203, 75},
85 { 202, 73},
86 { 201, 70},
87 { 200, 68},
88 { 199, 65},
89 { 198, 63},
90 { 197, 60},
91 { 196, 58},
92 { 195, 55},
93 { 194, 53},
94 { 193, 50},
95 { 192, 48},
96 { 192, 45},
97 { 191, 43},
98 { 191, 40},
99 { 190, 38},
100 { 190, 35},
101 { 189, 33},
102 { 188, 30},
103 { 187, 28},
104 { 186, 25},
105 { 185, 23},
106 { 184, 20},
107 { 183, 18},
108 { 182, 15},
109 { 181, 13},
110 { 180, 10},
111 { 179, 8},
112 { 178, 5},
113 { 0, 0},
116 struct battery_thresh sharpsl_battery_levels_noac[] = {
117 { 213, 100},
118 { 212, 98},
119 { 211, 95},
120 { 210, 93},
121 { 209, 90},
122 { 208, 88},
123 { 207, 85},
124 { 206, 83},
125 { 205, 80},
126 { 204, 78},
127 { 203, 75},
128 { 202, 73},
129 { 201, 70},
130 { 200, 68},
131 { 199, 65},
132 { 198, 63},
133 { 197, 60},
134 { 196, 58},
135 { 195, 55},
136 { 194, 53},
137 { 193, 50},
138 { 192, 48},
139 { 191, 45},
140 { 190, 43},
141 { 189, 40},
142 { 188, 38},
143 { 187, 35},
144 { 186, 33},
145 { 185, 30},
146 { 184, 28},
147 { 183, 25},
148 { 182, 23},
149 { 181, 20},
150 { 180, 18},
151 { 179, 15},
152 { 178, 13},
153 { 177, 10},
154 { 176, 8},
155 { 175, 5},
156 { 0, 0},
159 /* MAX1111 Commands */
160 #define MAXCTRL_PD0 (1u << 0)
161 #define MAXCTRL_PD1 (1u << 1)
162 #define MAXCTRL_SGL (1u << 2)
163 #define MAXCTRL_UNI (1u << 3)
164 #define MAXCTRL_SEL_SH 4
165 #define MAXCTRL_STR (1u << 7)
167 extern int max1111_read_channel(int);
169 * Read MAX1111 ADC
171 int sharpsl_pm_pxa_read_max1111(int channel)
173 /* max1111 accepts channels from 0-3, however,
174 * it is encoded from 0-7 here in the code.
176 return max1111_read_channel(channel >> 1);
179 static int get_percentage(int voltage)
181 int i = sharpsl_pm.machinfo->bat_levels - 1;
182 int bl_status = sharpsl_pm.machinfo->backlight_get_status ? sharpsl_pm.machinfo->backlight_get_status() : 0;
183 struct battery_thresh *thresh;
185 if (sharpsl_pm.charge_mode == CHRG_ON)
186 thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_acin_bl : sharpsl_pm.machinfo->bat_levels_acin;
187 else
188 thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_noac_bl : sharpsl_pm.machinfo->bat_levels_noac;
190 while (i > 0 && (voltage > thresh[i].voltage))
191 i--;
193 return thresh[i].percentage;
196 static int get_apm_status(int voltage)
198 int low_thresh, high_thresh;
200 if (sharpsl_pm.charge_mode == CHRG_ON) {
201 high_thresh = sharpsl_pm.machinfo->status_high_acin;
202 low_thresh = sharpsl_pm.machinfo->status_low_acin;
203 } else {
204 high_thresh = sharpsl_pm.machinfo->status_high_noac;
205 low_thresh = sharpsl_pm.machinfo->status_low_noac;
208 if (voltage >= high_thresh)
209 return APM_BATTERY_STATUS_HIGH;
210 if (voltage >= low_thresh)
211 return APM_BATTERY_STATUS_LOW;
212 return APM_BATTERY_STATUS_CRITICAL;
215 void sharpsl_battery_kick(void)
217 schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125));
220 static void sharpsl_battery_thread(struct work_struct *private_)
222 int voltage, percent, apm_status, i;
224 if (!sharpsl_pm.machinfo)
225 return;
227 sharpsl_pm.battstat.ac_status = (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN) ? APM_AC_ONLINE : APM_AC_OFFLINE);
229 /* Corgi cannot confirm when battery fully charged so periodically kick! */
230 if (!sharpsl_pm.machinfo->batfull_irq && (sharpsl_pm.charge_mode == CHRG_ON)
231 && time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_ON_TIME_INTERVAL))
232 schedule_delayed_work(&toggle_charger, 0);
234 for (i = 0; i < 5; i++) {
235 voltage = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
236 if (voltage > 0)
237 break;
239 if (voltage <= 0) {
240 voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage;
241 dev_warn(sharpsl_pm.dev, "Warning: Cannot read main battery!\n");
244 voltage = sharpsl_average_value(voltage);
245 apm_status = get_apm_status(voltage);
246 percent = get_percentage(voltage);
248 /* At low battery voltages, the voltage has a tendency to start
249 creeping back up so we try to avoid this here */
250 if ((sharpsl_pm.battstat.ac_status == APM_AC_ONLINE)
251 || (apm_status == APM_BATTERY_STATUS_HIGH)
252 || percent <= sharpsl_pm.battstat.mainbat_percent) {
253 sharpsl_pm.battstat.mainbat_voltage = voltage;
254 sharpsl_pm.battstat.mainbat_status = apm_status;
255 sharpsl_pm.battstat.mainbat_percent = percent;
258 dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %ld\n", voltage,
259 sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies);
261 /* Suspend if critical battery level */
262 if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
263 && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL)
264 && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) {
265 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
266 dev_err(sharpsl_pm.dev, "Fatal Off\n");
267 apm_queue_event(APM_CRITICAL_SUSPEND);
270 schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME);
273 void sharpsl_pm_led(int val)
275 if (val == SHARPSL_LED_ERROR) {
276 dev_err(sharpsl_pm.dev, "Charging Error!\n");
277 } else if (val == SHARPSL_LED_ON) {
278 dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
279 led_trigger_event(sharpsl_charge_led_trigger, LED_FULL);
280 } else {
281 dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
282 led_trigger_event(sharpsl_charge_led_trigger, LED_OFF);
286 static void sharpsl_charge_on(void)
288 dev_dbg(sharpsl_pm.dev, "Turning Charger On\n");
290 sharpsl_pm.full_count = 0;
291 sharpsl_pm.charge_mode = CHRG_ON;
292 schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250));
293 schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500));
296 static void sharpsl_charge_off(void)
298 dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n");
300 sharpsl_pm.machinfo->charge(0);
301 sharpsl_pm_led(SHARPSL_LED_OFF);
302 sharpsl_pm.charge_mode = CHRG_OFF;
304 schedule_delayed_work(&sharpsl_bat, 0);
307 static void sharpsl_charge_error(void)
309 sharpsl_pm_led(SHARPSL_LED_ERROR);
310 sharpsl_pm.machinfo->charge(0);
311 sharpsl_pm.charge_mode = CHRG_ERROR;
314 static void sharpsl_charge_toggle(struct work_struct *private_)
316 dev_dbg(sharpsl_pm.dev, "Toggling Charger at time: %lx\n", jiffies);
318 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
319 sharpsl_charge_off();
320 return;
321 } else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) {
322 sharpsl_charge_error();
323 return;
326 sharpsl_pm_led(SHARPSL_LED_ON);
327 sharpsl_pm.machinfo->charge(0);
328 mdelay(SHARPSL_CHARGE_WAIT_TIME);
329 sharpsl_pm.machinfo->charge(1);
331 sharpsl_pm.charge_start_time = jiffies;
334 static void sharpsl_ac_timer(struct timer_list *unused)
336 int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
338 dev_dbg(sharpsl_pm.dev, "AC Status: %d\n", acin);
340 sharpsl_average_clear();
341 if (acin && (sharpsl_pm.charge_mode != CHRG_ON))
342 sharpsl_charge_on();
343 else if (sharpsl_pm.charge_mode == CHRG_ON)
344 sharpsl_charge_off();
346 schedule_delayed_work(&sharpsl_bat, 0);
350 static irqreturn_t sharpsl_ac_isr(int irq, void *dev_id)
352 /* Delay the event slightly to debounce */
353 /* Must be a smaller delay than the chrg_full_isr below */
354 mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
356 return IRQ_HANDLED;
359 static void sharpsl_chrg_full_timer(struct timer_list *unused)
361 dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies);
363 sharpsl_pm.full_count++;
365 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
366 dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n");
367 if (sharpsl_pm.charge_mode == CHRG_ON)
368 sharpsl_charge_off();
369 } else if (sharpsl_pm.full_count < 2) {
370 dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n");
371 schedule_delayed_work(&toggle_charger, 0);
372 } else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) {
373 dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n");
374 schedule_delayed_work(&toggle_charger, 0);
375 } else {
376 sharpsl_charge_off();
377 sharpsl_pm.charge_mode = CHRG_DONE;
378 dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n");
382 /* Charging Finished Interrupt (Not present on Corgi) */
383 /* Can trigger at the same time as an AC status change so
384 delay until after that has been processed */
385 static irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id)
387 if (sharpsl_pm.flags & SHARPSL_SUSPENDED)
388 return IRQ_HANDLED;
390 /* delay until after any ac interrupt */
391 mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500));
393 return IRQ_HANDLED;
396 static irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id)
398 int is_fatal = 0;
400 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) {
401 dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n");
402 is_fatal = 1;
405 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL)) {
406 dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n");
407 is_fatal = 1;
410 if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) {
411 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
412 apm_queue_event(APM_CRITICAL_SUSPEND);
415 return IRQ_HANDLED;
419 * Maintain an average of the last 10 readings
421 #define SHARPSL_CNV_VALUE_NUM 10
422 static int sharpsl_ad_index;
424 static void sharpsl_average_clear(void)
426 sharpsl_ad_index = 0;
429 static int sharpsl_average_value(int ad)
431 int i, ad_val = 0;
432 static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1];
434 if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) {
435 sharpsl_ad_index = 0;
436 return ad;
439 sharpsl_ad[sharpsl_ad_index] = ad;
440 sharpsl_ad_index++;
441 if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) {
442 for (i = 0; i < (SHARPSL_CNV_VALUE_NUM-1); i++)
443 sharpsl_ad[i] = sharpsl_ad[i+1];
444 sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1;
446 for (i = 0; i < sharpsl_ad_index; i++)
447 ad_val += sharpsl_ad[i];
449 return ad_val / sharpsl_ad_index;
453 * Take an array of 5 integers, remove the maximum and minimum values
454 * and return the average.
456 static int get_select_val(int *val)
458 int i, j, k, temp, sum = 0;
460 /* Find MAX val */
461 temp = val[0];
462 j = 0;
463 for (i = 1; i < 5; i++) {
464 if (temp < val[i]) {
465 temp = val[i];
466 j = i;
470 /* Find MIN val */
471 temp = val[4];
472 k = 4;
473 for (i = 3; i >= 0; i--) {
474 if (temp > val[i]) {
475 temp = val[i];
476 k = i;
480 for (i = 0; i < 5; i++)
481 if (i != j && i != k)
482 sum += val[i];
484 dev_dbg(sharpsl_pm.dev, "Average: %d from values: %d, %d, %d, %d, %d\n", sum/3, val[0], val[1], val[2], val[3], val[4]);
486 return sum/3;
489 static int sharpsl_check_battery_temp(void)
491 int val, i, buff[5];
493 /* Check battery temperature */
494 for (i = 0; i < 5; i++) {
495 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
496 sharpsl_pm.machinfo->measure_temp(1);
497 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
498 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_TEMP);
499 sharpsl_pm.machinfo->measure_temp(0);
502 val = get_select_val(buff);
504 dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val);
505 if (val > sharpsl_pm.machinfo->charge_on_temp) {
506 printk(KERN_WARNING "Not charging: temperature out of limits.\n");
507 return -1;
510 return 0;
513 #ifdef CONFIG_PM
514 static int sharpsl_check_battery_voltage(void)
516 int val, i, buff[5];
518 /* disable charge, enable discharge */
519 sharpsl_pm.machinfo->charge(0);
520 sharpsl_pm.machinfo->discharge(1);
521 mdelay(SHARPSL_WAIT_DISCHARGE_ON);
523 if (sharpsl_pm.machinfo->discharge1)
524 sharpsl_pm.machinfo->discharge1(1);
526 /* Check battery voltage */
527 for (i = 0; i < 5; i++) {
528 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
529 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
532 if (sharpsl_pm.machinfo->discharge1)
533 sharpsl_pm.machinfo->discharge1(0);
535 sharpsl_pm.machinfo->discharge(0);
537 val = get_select_val(buff);
538 dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val);
540 if (val < sharpsl_pm.machinfo->charge_on_volt)
541 return -1;
543 return 0;
545 #endif
547 static int sharpsl_ac_check(void)
549 int temp, i, buff[5];
551 for (i = 0; i < 5; i++) {
552 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_ACIN_VOLT);
553 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN);
556 temp = get_select_val(buff);
557 dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n", temp);
559 if ((temp > sharpsl_pm.machinfo->charge_acin_high) || (temp < sharpsl_pm.machinfo->charge_acin_low)) {
560 dev_err(sharpsl_pm.dev, "Error: AC check failed: voltage %d.\n", temp);
561 return -1;
564 return 0;
567 #ifdef CONFIG_PM
568 static int sharpsl_pm_suspend(struct platform_device *pdev, pm_message_t state)
570 sharpsl_pm.flags |= SHARPSL_SUSPENDED;
571 flush_delayed_work(&toggle_charger);
572 flush_delayed_work(&sharpsl_bat);
574 if (sharpsl_pm.charge_mode == CHRG_ON)
575 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
576 else
577 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
579 return 0;
582 static int sharpsl_pm_resume(struct platform_device *pdev)
584 /* Clear the reset source indicators as they break the bootloader upon reboot */
585 RCSR = 0x0f;
586 sharpsl_average_clear();
587 sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED;
588 sharpsl_pm.flags &= ~SHARPSL_SUSPENDED;
590 return 0;
593 static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
595 dev_dbg(sharpsl_pm.dev, "Time is: %08x\n", RCNR);
597 dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n", sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG);
598 /* not charging and AC-IN! */
600 if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN))) {
601 dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n");
602 sharpsl_pm.charge_mode = CHRG_OFF;
603 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
604 sharpsl_off_charge_battery();
607 sharpsl_pm.machinfo->presuspend();
609 PEDR = 0xffffffff; /* clear it */
611 sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE;
612 if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) {
613 RTSR &= RTSR_ALE;
614 RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND;
615 dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n", RTAR);
616 sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE;
617 } else if (alarm_enable) {
618 RTSR &= RTSR_ALE;
619 RTAR = alarm_time;
620 dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n", RTAR);
621 } else {
622 dev_dbg(sharpsl_pm.dev, "No alarms set.\n");
625 pxa_pm_enter(state);
627 sharpsl_pm.machinfo->postsuspend();
629 dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n", PEDR);
632 static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
634 if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable)) {
635 if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) {
636 dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n");
637 corgi_goto_sleep(alarm_time, alarm_enable, state);
638 return 1;
640 if (sharpsl_off_charge_battery()) {
641 dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n");
642 corgi_goto_sleep(alarm_time, alarm_enable, state);
643 return 1;
645 dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n");
648 if ((!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) ||
649 (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL))) {
650 dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n");
651 corgi_goto_sleep(alarm_time, alarm_enable, state);
652 return 1;
655 return 0;
658 static int corgi_pxa_pm_enter(suspend_state_t state)
660 unsigned long alarm_time = RTAR;
661 unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0);
663 dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n");
665 corgi_goto_sleep(alarm_time, alarm_status, state);
667 while (corgi_enter_suspend(alarm_time, alarm_status, state))
670 if (sharpsl_pm.machinfo->earlyresume)
671 sharpsl_pm.machinfo->earlyresume();
673 dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n");
675 return 0;
678 static int sharpsl_off_charge_error(void)
680 dev_err(sharpsl_pm.dev, "Offline Charger: Error occurred.\n");
681 sharpsl_pm.machinfo->charge(0);
682 sharpsl_pm_led(SHARPSL_LED_ERROR);
683 sharpsl_pm.charge_mode = CHRG_ERROR;
684 return 1;
688 * Charging Control while suspended
689 * Return 1 - go straight to sleep
690 * Return 0 - sleep or wakeup depending on other factors
692 static int sharpsl_off_charge_battery(void)
694 int time;
696 dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode);
698 if (sharpsl_pm.charge_mode == CHRG_OFF) {
699 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n");
701 /* AC Check */
702 if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0))
703 return sharpsl_off_charge_error();
705 /* Start Charging */
706 sharpsl_pm_led(SHARPSL_LED_ON);
707 sharpsl_pm.machinfo->charge(0);
708 mdelay(SHARPSL_CHARGE_WAIT_TIME);
709 sharpsl_pm.machinfo->charge(1);
711 sharpsl_pm.charge_mode = CHRG_ON;
712 sharpsl_pm.full_count = 0;
714 return 1;
715 } else if (sharpsl_pm.charge_mode != CHRG_ON) {
716 return 1;
719 if (sharpsl_pm.full_count == 0) {
720 int time;
722 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n");
724 if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0))
725 return sharpsl_off_charge_error();
727 sharpsl_pm.machinfo->charge(0);
728 mdelay(SHARPSL_CHARGE_WAIT_TIME);
729 sharpsl_pm.machinfo->charge(1);
730 sharpsl_pm.charge_mode = CHRG_ON;
732 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
734 time = RCNR;
735 while (1) {
736 /* Check if any wakeup event had occurred */
737 if (sharpsl_pm.machinfo->charger_wakeup())
738 return 0;
739 /* Check for timeout */
740 if ((RCNR - time) > SHARPSL_WAIT_CO_TIME)
741 return 1;
742 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
743 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occurred. Retrying to check\n");
744 sharpsl_pm.full_count++;
745 sharpsl_pm.machinfo->charge(0);
746 mdelay(SHARPSL_CHARGE_WAIT_TIME);
747 sharpsl_pm.machinfo->charge(1);
748 return 1;
753 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n");
755 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
757 time = RCNR;
758 while (1) {
759 /* Check if any wakeup event had occurred */
760 if (sharpsl_pm.machinfo->charger_wakeup())
761 return 0;
762 /* Check for timeout */
763 if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) {
764 if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) {
765 dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n");
766 sharpsl_pm.full_count = 0;
768 sharpsl_pm.full_count++;
769 return 1;
771 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
772 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n");
773 sharpsl_pm_led(SHARPSL_LED_OFF);
774 sharpsl_pm.machinfo->charge(0);
775 sharpsl_pm.charge_mode = CHRG_DONE;
776 return 1;
780 #else
781 #define sharpsl_pm_suspend NULL
782 #define sharpsl_pm_resume NULL
783 #endif
785 static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf)
787 return sprintf(buf, "%d\n", sharpsl_pm.battstat.mainbat_percent);
790 static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
792 return sprintf(buf, "%d\n", sharpsl_pm.battstat.mainbat_voltage);
795 static DEVICE_ATTR_RO(battery_percentage);
796 static DEVICE_ATTR_RO(battery_voltage);
798 extern void (*apm_get_power_status)(struct apm_power_info *);
800 static void sharpsl_apm_get_power_status(struct apm_power_info *info)
802 info->ac_line_status = sharpsl_pm.battstat.ac_status;
804 if (sharpsl_pm.charge_mode == CHRG_ON)
805 info->battery_status = APM_BATTERY_STATUS_CHARGING;
806 else
807 info->battery_status = sharpsl_pm.battstat.mainbat_status;
809 info->battery_flag = (1 << info->battery_status);
810 info->battery_life = sharpsl_pm.battstat.mainbat_percent;
813 #ifdef CONFIG_PM
814 static const struct platform_suspend_ops sharpsl_pm_ops = {
815 .prepare = pxa_pm_prepare,
816 .finish = pxa_pm_finish,
817 .enter = corgi_pxa_pm_enter,
818 .valid = suspend_valid_only_mem,
820 #endif
822 static int sharpsl_pm_probe(struct platform_device *pdev)
824 int ret, irq;
826 if (!pdev->dev.platform_data)
827 return -EINVAL;
829 sharpsl_pm.dev = &pdev->dev;
830 sharpsl_pm.machinfo = pdev->dev.platform_data;
831 sharpsl_pm.charge_mode = CHRG_OFF;
832 sharpsl_pm.flags = 0;
834 timer_setup(&sharpsl_pm.ac_timer, sharpsl_ac_timer, 0);
836 timer_setup(&sharpsl_pm.chrg_full_timer, sharpsl_chrg_full_timer, 0);
838 led_trigger_register_simple("sharpsl-charge", &sharpsl_charge_led_trigger);
840 sharpsl_pm.machinfo->init();
842 gpio_request(sharpsl_pm.machinfo->gpio_acin, "AC IN");
843 gpio_direction_input(sharpsl_pm.machinfo->gpio_acin);
844 gpio_request(sharpsl_pm.machinfo->gpio_batfull, "Battery Full");
845 gpio_direction_input(sharpsl_pm.machinfo->gpio_batfull);
846 gpio_request(sharpsl_pm.machinfo->gpio_batlock, "Battery Lock");
847 gpio_direction_input(sharpsl_pm.machinfo->gpio_batlock);
849 /* Register interrupt handlers */
850 irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_acin);
851 if (request_irq(irq, sharpsl_ac_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "AC Input Detect", sharpsl_ac_isr)) {
852 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
855 irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_batlock);
856 if (request_irq(irq, sharpsl_fatal_isr, IRQF_TRIGGER_FALLING, "Battery Cover", sharpsl_fatal_isr)) {
857 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
860 if (sharpsl_pm.machinfo->gpio_fatal) {
861 irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_fatal);
862 if (request_irq(irq, sharpsl_fatal_isr, IRQF_TRIGGER_FALLING, "Fatal Battery", sharpsl_fatal_isr)) {
863 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
867 if (sharpsl_pm.machinfo->batfull_irq) {
868 /* Register interrupt handler. */
869 irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_batfull);
870 if (request_irq(irq, sharpsl_chrg_full_isr, IRQF_TRIGGER_RISING, "CO", sharpsl_chrg_full_isr)) {
871 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
875 ret = device_create_file(&pdev->dev, &dev_attr_battery_percentage);
876 ret |= device_create_file(&pdev->dev, &dev_attr_battery_voltage);
877 if (ret != 0)
878 dev_warn(&pdev->dev, "Failed to register attributes (%d)\n", ret);
880 apm_get_power_status = sharpsl_apm_get_power_status;
882 #ifdef CONFIG_PM
883 suspend_set_ops(&sharpsl_pm_ops);
884 #endif
886 mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
888 return 0;
891 static void sharpsl_pm_remove(struct platform_device *pdev)
893 suspend_set_ops(NULL);
895 device_remove_file(&pdev->dev, &dev_attr_battery_percentage);
896 device_remove_file(&pdev->dev, &dev_attr_battery_voltage);
898 led_trigger_unregister_simple(sharpsl_charge_led_trigger);
900 free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr);
901 free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr);
903 if (sharpsl_pm.machinfo->gpio_fatal)
904 free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_fatal), sharpsl_fatal_isr);
906 if (sharpsl_pm.machinfo->batfull_irq)
907 free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr);
909 gpio_free(sharpsl_pm.machinfo->gpio_batlock);
910 gpio_free(sharpsl_pm.machinfo->gpio_batfull);
911 gpio_free(sharpsl_pm.machinfo->gpio_acin);
913 if (sharpsl_pm.machinfo->exit)
914 sharpsl_pm.machinfo->exit();
916 del_timer_sync(&sharpsl_pm.chrg_full_timer);
917 del_timer_sync(&sharpsl_pm.ac_timer);
920 static struct platform_driver sharpsl_pm_driver = {
921 .probe = sharpsl_pm_probe,
922 .remove_new = sharpsl_pm_remove,
923 .suspend = sharpsl_pm_suspend,
924 .resume = sharpsl_pm_resume,
925 .driver = {
926 .name = "sharpsl-pm",
930 static int sharpsl_pm_init(void)
932 return platform_driver_register(&sharpsl_pm_driver);
935 static void sharpsl_pm_exit(void)
937 platform_driver_unregister(&sharpsl_pm_driver);
940 late_initcall(sharpsl_pm_init);
941 module_exit(sharpsl_pm_exit);