Richard Purdie | 078abcf | 2005-11-10 17:42:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Battery and Power Management code for the Sharp SL-C7xx and SL-Cxx00 |
| 3 | * series of PDAs |
| 4 | * |
| 5 | * Copyright (c) 2004-2005 Richard Purdie |
| 6 | * |
| 7 | * Based on code written by Sharp for 2.4 kernels |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #undef DEBUG |
| 16 | |
| 17 | #include <linux/module.h> |
Richard Purdie | 078abcf | 2005-11-10 17:42:29 +0000 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Richard Purdie | 078abcf | 2005-11-10 17:42:29 +0000 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
Richard Purdie | c5e1ae9 | 2005-11-12 18:53:48 +0000 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Russell King | 61fde51 | 2007-02-26 21:04:29 +0000 | [diff] [blame] | 21 | #include <linux/apm-emulation.h> |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 22 | #include <linux/timer.h> |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 23 | #include <linux/delay.h> |
| 24 | #include <linux/leds.h> |
| 25 | #include <linux/suspend.h> |
Dmitry Eremin-Solenikov | d5af277 | 2009-03-28 18:18:54 +0300 | [diff] [blame] | 26 | #include <linux/gpio.h> |
Richard Purdie | 078abcf | 2005-11-10 17:42:29 +0000 | [diff] [blame] | 27 | |
Richard Purdie | 078abcf | 2005-11-10 17:42:29 +0000 | [diff] [blame] | 28 | #include <asm/mach-types.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 29 | #include <mach/pm.h> |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 30 | #include <mach/pxa2xx-regs.h> |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 31 | #include <mach/regs-rtc.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 32 | #include <mach/sharpsl.h> |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 33 | #include <mach/sharpsl_pm.h> |
| 34 | |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 35 | #include "sharpsl.h" |
| 36 | |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 37 | /* |
| 38 | * Constants |
| 39 | */ |
| 40 | #define SHARPSL_CHARGE_ON_TIME_INTERVAL (msecs_to_jiffies(1*60*1000)) /* 1 min */ |
| 41 | #define SHARPSL_CHARGE_FINISH_TIME (msecs_to_jiffies(10*60*1000)) /* 10 min */ |
| 42 | #define SHARPSL_BATCHK_TIME (msecs_to_jiffies(15*1000)) /* 15 sec */ |
| 43 | #define SHARPSL_BATCHK_TIME_SUSPEND (60*10) /* 10 min */ |
| 44 | |
| 45 | #define SHARPSL_WAIT_CO_TIME 15 /* 15 sec */ |
| 46 | #define SHARPSL_WAIT_DISCHARGE_ON 100 /* 100 msec */ |
| 47 | #define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP 10 /* 10 msec */ |
| 48 | #define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT 10 /* 10 msec */ |
| 49 | #define SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN 10 /* 10 msec */ |
| 50 | #define SHARPSL_CHARGE_WAIT_TIME 15 /* 15 msec */ |
| 51 | #define SHARPSL_CHARGE_CO_CHECK_TIME 5 /* 5 msec */ |
| 52 | #define SHARPSL_CHARGE_RETRY_CNT 1 /* eqv. 10 min */ |
| 53 | |
| 54 | /* |
| 55 | * Prototypes |
| 56 | */ |
| 57 | #ifdef CONFIG_PM |
| 58 | static int sharpsl_off_charge_battery(void); |
| 59 | static int sharpsl_check_battery_voltage(void); |
| 60 | static int sharpsl_fatal_check(void); |
| 61 | #endif |
| 62 | static int sharpsl_check_battery_temp(void); |
| 63 | static int sharpsl_ac_check(void); |
| 64 | static int sharpsl_average_value(int ad); |
| 65 | static void sharpsl_average_clear(void); |
| 66 | static void sharpsl_charge_toggle(struct work_struct *private_); |
| 67 | static void sharpsl_battery_thread(struct work_struct *private_); |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | * Variables |
| 72 | */ |
| 73 | struct sharpsl_pm_status sharpsl_pm; |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 74 | static DECLARE_DELAYED_WORK(toggle_charger, sharpsl_charge_toggle); |
| 75 | static DECLARE_DELAYED_WORK(sharpsl_bat, sharpsl_battery_thread); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 76 | DEFINE_LED_TRIGGER(sharpsl_charge_led_trigger); |
| 77 | |
| 78 | |
Richard Purdie | 078abcf | 2005-11-10 17:42:29 +0000 | [diff] [blame] | 79 | |
Pavel Machek | 0ba01eb | 2009-10-22 22:16:34 +0200 | [diff] [blame] | 80 | struct battery_thresh sharpsl_battery_levels_acin[] = { |
Richard Purdie | 078abcf | 2005-11-10 17:42:29 +0000 | [diff] [blame] | 81 | { 213, 100}, |
| 82 | { 212, 98}, |
| 83 | { 211, 95}, |
| 84 | { 210, 93}, |
| 85 | { 209, 90}, |
| 86 | { 208, 88}, |
| 87 | { 207, 85}, |
| 88 | { 206, 83}, |
| 89 | { 205, 80}, |
| 90 | { 204, 78}, |
| 91 | { 203, 75}, |
| 92 | { 202, 73}, |
| 93 | { 201, 70}, |
| 94 | { 200, 68}, |
| 95 | { 199, 65}, |
| 96 | { 198, 63}, |
| 97 | { 197, 60}, |
| 98 | { 196, 58}, |
| 99 | { 195, 55}, |
| 100 | { 194, 53}, |
| 101 | { 193, 50}, |
| 102 | { 192, 48}, |
| 103 | { 192, 45}, |
| 104 | { 191, 43}, |
| 105 | { 191, 40}, |
| 106 | { 190, 38}, |
| 107 | { 190, 35}, |
| 108 | { 189, 33}, |
| 109 | { 188, 30}, |
| 110 | { 187, 28}, |
| 111 | { 186, 25}, |
| 112 | { 185, 23}, |
| 113 | { 184, 20}, |
| 114 | { 183, 18}, |
| 115 | { 182, 15}, |
| 116 | { 181, 13}, |
| 117 | { 180, 10}, |
| 118 | { 179, 8}, |
| 119 | { 178, 5}, |
| 120 | { 0, 0}, |
| 121 | }; |
| 122 | |
Pavel Machek | 0ba01eb | 2009-10-22 22:16:34 +0200 | [diff] [blame] | 123 | struct battery_thresh sharpsl_battery_levels_noac[] = { |
Richard Purdie | 078abcf | 2005-11-10 17:42:29 +0000 | [diff] [blame] | 124 | { 213, 100}, |
| 125 | { 212, 98}, |
| 126 | { 211, 95}, |
| 127 | { 210, 93}, |
| 128 | { 209, 90}, |
| 129 | { 208, 88}, |
| 130 | { 207, 85}, |
| 131 | { 206, 83}, |
| 132 | { 205, 80}, |
| 133 | { 204, 78}, |
| 134 | { 203, 75}, |
| 135 | { 202, 73}, |
| 136 | { 201, 70}, |
| 137 | { 200, 68}, |
| 138 | { 199, 65}, |
| 139 | { 198, 63}, |
| 140 | { 197, 60}, |
| 141 | { 196, 58}, |
| 142 | { 195, 55}, |
| 143 | { 194, 53}, |
| 144 | { 193, 50}, |
| 145 | { 192, 48}, |
| 146 | { 191, 45}, |
| 147 | { 190, 43}, |
| 148 | { 189, 40}, |
| 149 | { 188, 38}, |
| 150 | { 187, 35}, |
| 151 | { 186, 33}, |
| 152 | { 185, 30}, |
| 153 | { 184, 28}, |
| 154 | { 183, 25}, |
| 155 | { 182, 23}, |
| 156 | { 181, 20}, |
| 157 | { 180, 18}, |
| 158 | { 179, 15}, |
| 159 | { 178, 13}, |
| 160 | { 177, 10}, |
| 161 | { 176, 8}, |
| 162 | { 175, 5}, |
| 163 | { 0, 0}, |
| 164 | }; |
| 165 | |
Eric Miao | 25af3b0 | 2008-10-21 09:42:29 +0800 | [diff] [blame] | 166 | /* MAX1111 Commands */ |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 167 | #define MAXCTRL_PD0 (1u << 0) |
| 168 | #define MAXCTRL_PD1 (1u << 1) |
| 169 | #define MAXCTRL_SGL (1u << 2) |
| 170 | #define MAXCTRL_UNI (1u << 3) |
Eric Miao | 25af3b0 | 2008-10-21 09:42:29 +0800 | [diff] [blame] | 171 | #define MAXCTRL_SEL_SH 4 |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 172 | #define MAXCTRL_STR (1u << 7) |
Eric Miao | 25af3b0 | 2008-10-21 09:42:29 +0800 | [diff] [blame] | 173 | |
Richard Purdie | 078abcf | 2005-11-10 17:42:29 +0000 | [diff] [blame] | 174 | /* |
| 175 | * Read MAX1111 ADC |
| 176 | */ |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 177 | int sharpsl_pm_pxa_read_max1111(int channel) |
Richard Purdie | 078abcf | 2005-11-10 17:42:29 +0000 | [diff] [blame] | 178 | { |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 179 | /* Ugly, better move this function into another module */ |
| 180 | if (machine_is_tosa()) |
Richard Purdie | f8703dc | 2006-06-19 19:58:52 +0100 | [diff] [blame] | 181 | return 0; |
| 182 | |
Eric Miao | 45e2a9b | 2008-10-21 11:36:19 +0800 | [diff] [blame] | 183 | #ifdef CONFIG_CORGI_SSP_DEPRECATED |
| 184 | return corgi_ssp_max1111_get((channel << MAXCTRL_SEL_SH) | MAXCTRL_PD0 | MAXCTRL_PD1 |
| 185 | | MAXCTRL_SGL | MAXCTRL_UNI | MAXCTRL_STR); |
| 186 | #else |
Eric Miao | 25af3b0 | 2008-10-21 09:42:29 +0800 | [diff] [blame] | 187 | extern int max1111_read_channel(int); |
| 188 | |
Eric Miao | f16177c | 2008-08-29 06:19:32 +0800 | [diff] [blame] | 189 | /* max1111 accepts channels from 0-3, however, |
| 190 | * it is encoded from 0-7 here in the code. |
| 191 | */ |
| 192 | return max1111_read_channel(channel >> 1); |
Eric Miao | 25af3b0 | 2008-10-21 09:42:29 +0800 | [diff] [blame] | 193 | #endif |
Richard Purdie | 078abcf | 2005-11-10 17:42:29 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 196 | static int get_percentage(int voltage) |
| 197 | { |
| 198 | int i = sharpsl_pm.machinfo->bat_levels - 1; |
| 199 | int bl_status = sharpsl_pm.machinfo->backlight_get_status ? sharpsl_pm.machinfo->backlight_get_status() : 0; |
| 200 | struct battery_thresh *thresh; |
| 201 | |
| 202 | if (sharpsl_pm.charge_mode == CHRG_ON) |
| 203 | thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_acin_bl : sharpsl_pm.machinfo->bat_levels_acin; |
| 204 | else |
| 205 | thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_noac_bl : sharpsl_pm.machinfo->bat_levels_noac; |
| 206 | |
| 207 | while (i > 0 && (voltage > thresh[i].voltage)) |
| 208 | i--; |
| 209 | |
| 210 | return thresh[i].percentage; |
| 211 | } |
| 212 | |
| 213 | static int get_apm_status(int voltage) |
| 214 | { |
| 215 | int low_thresh, high_thresh; |
| 216 | |
| 217 | if (sharpsl_pm.charge_mode == CHRG_ON) { |
| 218 | high_thresh = sharpsl_pm.machinfo->status_high_acin; |
| 219 | low_thresh = sharpsl_pm.machinfo->status_low_acin; |
| 220 | } else { |
| 221 | high_thresh = sharpsl_pm.machinfo->status_high_noac; |
| 222 | low_thresh = sharpsl_pm.machinfo->status_low_noac; |
| 223 | } |
| 224 | |
| 225 | if (voltage >= high_thresh) |
| 226 | return APM_BATTERY_STATUS_HIGH; |
| 227 | if (voltage >= low_thresh) |
| 228 | return APM_BATTERY_STATUS_LOW; |
| 229 | return APM_BATTERY_STATUS_CRITICAL; |
| 230 | } |
| 231 | |
| 232 | void sharpsl_battery_kick(void) |
| 233 | { |
| 234 | schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125)); |
| 235 | } |
| 236 | EXPORT_SYMBOL(sharpsl_battery_kick); |
| 237 | |
| 238 | |
| 239 | static void sharpsl_battery_thread(struct work_struct *private_) |
| 240 | { |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 241 | int voltage, percent, apm_status, i; |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 242 | |
| 243 | if (!sharpsl_pm.machinfo) |
| 244 | return; |
| 245 | |
| 246 | sharpsl_pm.battstat.ac_status = (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN) ? APM_AC_ONLINE : APM_AC_OFFLINE); |
| 247 | |
| 248 | /* Corgi cannot confirm when battery fully charged so periodically kick! */ |
| 249 | if (!sharpsl_pm.machinfo->batfull_irq && (sharpsl_pm.charge_mode == CHRG_ON) |
| 250 | && time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_ON_TIME_INTERVAL)) |
| 251 | schedule_delayed_work(&toggle_charger, 0); |
| 252 | |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 253 | for (i = 0; i < 5; i++) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 254 | voltage = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT); |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 255 | if (voltage > 0) |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 256 | break; |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 257 | } |
| 258 | if (voltage <= 0) { |
| 259 | voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage; |
| 260 | dev_warn(sharpsl_pm.dev, "Warning: Cannot read main battery!\n"); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | voltage = sharpsl_average_value(voltage); |
| 264 | apm_status = get_apm_status(voltage); |
| 265 | percent = get_percentage(voltage); |
| 266 | |
| 267 | /* At low battery voltages, the voltage has a tendency to start |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 268 | creeping back up so we try to avoid this here */ |
| 269 | if ((sharpsl_pm.battstat.ac_status == APM_AC_ONLINE) |
| 270 | || (apm_status == APM_BATTERY_STATUS_HIGH) |
| 271 | || percent <= sharpsl_pm.battstat.mainbat_percent) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 272 | sharpsl_pm.battstat.mainbat_voltage = voltage; |
| 273 | sharpsl_pm.battstat.mainbat_status = apm_status; |
| 274 | sharpsl_pm.battstat.mainbat_percent = percent; |
| 275 | } |
| 276 | |
| 277 | dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %ld\n", voltage, |
| 278 | sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies); |
| 279 | |
| 280 | #ifdef CONFIG_BACKLIGHT_CORGI |
| 281 | /* If battery is low. limit backlight intensity to save power. */ |
| 282 | if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE) |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 283 | && ((sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_LOW) |
| 284 | || (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL))) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 285 | if (!(sharpsl_pm.flags & SHARPSL_BL_LIMIT)) { |
| 286 | sharpsl_pm.machinfo->backlight_limit(1); |
| 287 | sharpsl_pm.flags |= SHARPSL_BL_LIMIT; |
| 288 | } |
| 289 | } else if (sharpsl_pm.flags & SHARPSL_BL_LIMIT) { |
| 290 | sharpsl_pm.machinfo->backlight_limit(0); |
| 291 | sharpsl_pm.flags &= ~SHARPSL_BL_LIMIT; |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | /* Suspend if critical battery level */ |
| 296 | if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE) |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 297 | && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL) |
| 298 | && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 299 | sharpsl_pm.flags |= SHARPSL_APM_QUEUED; |
| 300 | dev_err(sharpsl_pm.dev, "Fatal Off\n"); |
| 301 | apm_queue_event(APM_CRITICAL_SUSPEND); |
| 302 | } |
| 303 | |
| 304 | schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME); |
| 305 | } |
| 306 | |
| 307 | void sharpsl_pm_led(int val) |
| 308 | { |
| 309 | if (val == SHARPSL_LED_ERROR) { |
| 310 | dev_err(sharpsl_pm.dev, "Charging Error!\n"); |
| 311 | } else if (val == SHARPSL_LED_ON) { |
| 312 | dev_dbg(sharpsl_pm.dev, "Charge LED On\n"); |
| 313 | led_trigger_event(sharpsl_charge_led_trigger, LED_FULL); |
| 314 | } else { |
| 315 | dev_dbg(sharpsl_pm.dev, "Charge LED Off\n"); |
| 316 | led_trigger_event(sharpsl_charge_led_trigger, LED_OFF); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | static void sharpsl_charge_on(void) |
| 321 | { |
| 322 | dev_dbg(sharpsl_pm.dev, "Turning Charger On\n"); |
| 323 | |
| 324 | sharpsl_pm.full_count = 0; |
| 325 | sharpsl_pm.charge_mode = CHRG_ON; |
| 326 | schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250)); |
| 327 | schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500)); |
| 328 | } |
| 329 | |
| 330 | static void sharpsl_charge_off(void) |
| 331 | { |
| 332 | dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n"); |
| 333 | |
| 334 | sharpsl_pm.machinfo->charge(0); |
| 335 | sharpsl_pm_led(SHARPSL_LED_OFF); |
| 336 | sharpsl_pm.charge_mode = CHRG_OFF; |
| 337 | |
| 338 | schedule_delayed_work(&sharpsl_bat, 0); |
| 339 | } |
| 340 | |
| 341 | static void sharpsl_charge_error(void) |
| 342 | { |
| 343 | sharpsl_pm_led(SHARPSL_LED_ERROR); |
| 344 | sharpsl_pm.machinfo->charge(0); |
| 345 | sharpsl_pm.charge_mode = CHRG_ERROR; |
| 346 | } |
| 347 | |
| 348 | static void sharpsl_charge_toggle(struct work_struct *private_) |
| 349 | { |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 350 | dev_dbg(sharpsl_pm.dev, "Toggling Charger at time: %lx\n", jiffies); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 351 | |
| 352 | if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) { |
| 353 | sharpsl_charge_off(); |
| 354 | return; |
| 355 | } else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) { |
| 356 | sharpsl_charge_error(); |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | sharpsl_pm_led(SHARPSL_LED_ON); |
| 361 | sharpsl_pm.machinfo->charge(0); |
| 362 | mdelay(SHARPSL_CHARGE_WAIT_TIME); |
| 363 | sharpsl_pm.machinfo->charge(1); |
| 364 | |
| 365 | sharpsl_pm.charge_start_time = jiffies; |
| 366 | } |
| 367 | |
| 368 | static void sharpsl_ac_timer(unsigned long data) |
| 369 | { |
| 370 | int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN); |
| 371 | |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 372 | dev_dbg(sharpsl_pm.dev, "AC Status: %d\n", acin); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 373 | |
| 374 | sharpsl_average_clear(); |
| 375 | if (acin && (sharpsl_pm.charge_mode != CHRG_ON)) |
| 376 | sharpsl_charge_on(); |
| 377 | else if (sharpsl_pm.charge_mode == CHRG_ON) |
| 378 | sharpsl_charge_off(); |
| 379 | |
| 380 | schedule_delayed_work(&sharpsl_bat, 0); |
| 381 | } |
| 382 | |
| 383 | |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 384 | static irqreturn_t sharpsl_ac_isr(int irq, void *dev_id) |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 385 | { |
| 386 | /* Delay the event slightly to debounce */ |
| 387 | /* Must be a smaller delay than the chrg_full_isr below */ |
| 388 | mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250)); |
| 389 | |
| 390 | return IRQ_HANDLED; |
| 391 | } |
| 392 | |
| 393 | static void sharpsl_chrg_full_timer(unsigned long data) |
| 394 | { |
| 395 | dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies); |
| 396 | |
| 397 | sharpsl_pm.full_count++; |
| 398 | |
| 399 | if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) { |
| 400 | dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n"); |
| 401 | if (sharpsl_pm.charge_mode == CHRG_ON) |
| 402 | sharpsl_charge_off(); |
| 403 | } else if (sharpsl_pm.full_count < 2) { |
| 404 | dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n"); |
| 405 | schedule_delayed_work(&toggle_charger, 0); |
| 406 | } else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) { |
| 407 | dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n"); |
| 408 | schedule_delayed_work(&toggle_charger, 0); |
| 409 | } else { |
| 410 | sharpsl_charge_off(); |
| 411 | sharpsl_pm.charge_mode = CHRG_DONE; |
| 412 | dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n"); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* Charging Finished Interrupt (Not present on Corgi) */ |
| 417 | /* Can trigger at the same time as an AC status change so |
| 418 | delay until after that has been processed */ |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 419 | static irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id) |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 420 | { |
| 421 | if (sharpsl_pm.flags & SHARPSL_SUSPENDED) |
| 422 | return IRQ_HANDLED; |
| 423 | |
| 424 | /* delay until after any ac interrupt */ |
| 425 | mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500)); |
| 426 | |
| 427 | return IRQ_HANDLED; |
| 428 | } |
| 429 | |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 430 | static irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id) |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 431 | { |
| 432 | int is_fatal = 0; |
| 433 | |
| 434 | if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) { |
| 435 | dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n"); |
| 436 | is_fatal = 1; |
| 437 | } |
| 438 | |
| 439 | if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL)) { |
| 440 | dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n"); |
| 441 | is_fatal = 1; |
| 442 | } |
| 443 | |
| 444 | if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) { |
| 445 | sharpsl_pm.flags |= SHARPSL_APM_QUEUED; |
| 446 | apm_queue_event(APM_CRITICAL_SUSPEND); |
| 447 | } |
| 448 | |
| 449 | return IRQ_HANDLED; |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Maintain an average of the last 10 readings |
| 454 | */ |
| 455 | #define SHARPSL_CNV_VALUE_NUM 10 |
| 456 | static int sharpsl_ad_index; |
| 457 | |
| 458 | static void sharpsl_average_clear(void) |
| 459 | { |
| 460 | sharpsl_ad_index = 0; |
| 461 | } |
| 462 | |
| 463 | static int sharpsl_average_value(int ad) |
| 464 | { |
| 465 | int i, ad_val = 0; |
| 466 | static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1]; |
| 467 | |
| 468 | if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) { |
| 469 | sharpsl_ad_index = 0; |
| 470 | return ad; |
| 471 | } |
| 472 | |
| 473 | sharpsl_ad[sharpsl_ad_index] = ad; |
| 474 | sharpsl_ad_index++; |
| 475 | if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) { |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 476 | for (i = 0; i < (SHARPSL_CNV_VALUE_NUM-1); i++) |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 477 | sharpsl_ad[i] = sharpsl_ad[i+1]; |
| 478 | sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1; |
| 479 | } |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 480 | for (i = 0; i < sharpsl_ad_index; i++) |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 481 | ad_val += sharpsl_ad[i]; |
| 482 | |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 483 | return ad_val / sharpsl_ad_index; |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Take an array of 5 integers, remove the maximum and minimum values |
| 488 | * and return the average. |
| 489 | */ |
| 490 | static int get_select_val(int *val) |
| 491 | { |
| 492 | int i, j, k, temp, sum = 0; |
| 493 | |
| 494 | /* Find MAX val */ |
| 495 | temp = val[0]; |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 496 | j = 0; |
| 497 | for (i = 1; i < 5; i++) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 498 | if (temp < val[i]) { |
| 499 | temp = val[i]; |
| 500 | j = i; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /* Find MIN val */ |
| 505 | temp = val[4]; |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 506 | k = 4; |
| 507 | for (i = 3; i >= 0; i--) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 508 | if (temp > val[i]) { |
| 509 | temp = val[i]; |
| 510 | k = i; |
| 511 | } |
| 512 | } |
| 513 | |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 514 | for (i = 0; i < 5; i++) |
| 515 | if (i != j && i != k) |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 516 | sum += val[i]; |
| 517 | |
| 518 | 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]); |
| 519 | |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 520 | return sum/3; |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static int sharpsl_check_battery_temp(void) |
| 524 | { |
| 525 | int val, i, buff[5]; |
| 526 | |
| 527 | /* Check battery temperature */ |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 528 | for (i = 0; i < 5; i++) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 529 | mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP); |
| 530 | sharpsl_pm.machinfo->measure_temp(1); |
| 531 | mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP); |
| 532 | buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_TEMP); |
| 533 | sharpsl_pm.machinfo->measure_temp(0); |
| 534 | } |
| 535 | |
| 536 | val = get_select_val(buff); |
| 537 | |
| 538 | dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val); |
| 539 | if (val > sharpsl_pm.machinfo->charge_on_temp) { |
| 540 | printk(KERN_WARNING "Not charging: temperature out of limits.\n"); |
| 541 | return -1; |
| 542 | } |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | #ifdef CONFIG_PM |
| 548 | static int sharpsl_check_battery_voltage(void) |
| 549 | { |
| 550 | int val, i, buff[5]; |
| 551 | |
| 552 | /* disable charge, enable discharge */ |
| 553 | sharpsl_pm.machinfo->charge(0); |
| 554 | sharpsl_pm.machinfo->discharge(1); |
| 555 | mdelay(SHARPSL_WAIT_DISCHARGE_ON); |
| 556 | |
| 557 | if (sharpsl_pm.machinfo->discharge1) |
| 558 | sharpsl_pm.machinfo->discharge1(1); |
| 559 | |
| 560 | /* Check battery voltage */ |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 561 | for (i = 0; i < 5; i++) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 562 | buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT); |
| 563 | mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT); |
| 564 | } |
| 565 | |
| 566 | if (sharpsl_pm.machinfo->discharge1) |
| 567 | sharpsl_pm.machinfo->discharge1(0); |
| 568 | |
| 569 | sharpsl_pm.machinfo->discharge(0); |
| 570 | |
| 571 | val = get_select_val(buff); |
| 572 | dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val); |
| 573 | |
| 574 | if (val < sharpsl_pm.machinfo->charge_on_volt) |
| 575 | return -1; |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | #endif |
| 580 | |
| 581 | static int sharpsl_ac_check(void) |
| 582 | { |
| 583 | int temp, i, buff[5]; |
| 584 | |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 585 | for (i = 0; i < 5; i++) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 586 | buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_ACIN_VOLT); |
| 587 | mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN); |
| 588 | } |
| 589 | |
| 590 | temp = get_select_val(buff); |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 591 | dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n", temp); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 592 | |
| 593 | if ((temp > sharpsl_pm.machinfo->charge_acin_high) || (temp < sharpsl_pm.machinfo->charge_acin_low)) { |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 594 | dev_err(sharpsl_pm.dev, "Error: AC check failed: voltage %d.\n", temp); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 595 | return -1; |
| 596 | } |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | #ifdef CONFIG_PM |
| 602 | static int sharpsl_pm_suspend(struct platform_device *pdev, pm_message_t state) |
| 603 | { |
| 604 | sharpsl_pm.flags |= SHARPSL_SUSPENDED; |
| 605 | flush_scheduled_work(); |
| 606 | |
| 607 | if (sharpsl_pm.charge_mode == CHRG_ON) |
| 608 | sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG; |
| 609 | else |
| 610 | sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG; |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static int sharpsl_pm_resume(struct platform_device *pdev) |
| 616 | { |
| 617 | /* Clear the reset source indicators as they break the bootloader upon reboot */ |
| 618 | RCSR = 0x0f; |
| 619 | sharpsl_average_clear(); |
| 620 | sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED; |
| 621 | sharpsl_pm.flags &= ~SHARPSL_SUSPENDED; |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state) |
| 627 | { |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 628 | dev_dbg(sharpsl_pm.dev, "Time is: %08x\n", RCNR); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 629 | |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 630 | dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n", sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 631 | /* not charging and AC-IN! */ |
| 632 | |
| 633 | if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN))) { |
| 634 | dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n"); |
| 635 | sharpsl_pm.charge_mode = CHRG_OFF; |
| 636 | sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG; |
| 637 | sharpsl_off_charge_battery(); |
| 638 | } |
| 639 | |
| 640 | sharpsl_pm.machinfo->presuspend(); |
| 641 | |
| 642 | PEDR = 0xffffffff; /* clear it */ |
| 643 | |
| 644 | sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE; |
| 645 | if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) { |
| 646 | RTSR &= RTSR_ALE; |
| 647 | RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND; |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 648 | dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n", RTAR); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 649 | sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE; |
| 650 | } else if (alarm_enable) { |
| 651 | RTSR &= RTSR_ALE; |
| 652 | RTAR = alarm_time; |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 653 | dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n", RTAR); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 654 | } else { |
| 655 | dev_dbg(sharpsl_pm.dev, "No alarms set.\n"); |
| 656 | } |
| 657 | |
| 658 | pxa_pm_enter(state); |
| 659 | |
| 660 | sharpsl_pm.machinfo->postsuspend(); |
| 661 | |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 662 | dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n", PEDR); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state) |
| 666 | { |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 667 | if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable)) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 668 | if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) { |
| 669 | dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n"); |
| 670 | corgi_goto_sleep(alarm_time, alarm_enable, state); |
| 671 | return 1; |
| 672 | } |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 673 | if (sharpsl_off_charge_battery()) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 674 | dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n"); |
| 675 | corgi_goto_sleep(alarm_time, alarm_enable, state); |
| 676 | return 1; |
| 677 | } |
| 678 | dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n"); |
| 679 | } |
| 680 | |
Pavel Machek | 99f329a | 2009-09-06 07:28:40 +0200 | [diff] [blame] | 681 | if ((!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) || |
| 682 | (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL))) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 683 | dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n"); |
| 684 | corgi_goto_sleep(alarm_time, alarm_enable, state); |
| 685 | return 1; |
| 686 | } |
| 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | static int corgi_pxa_pm_enter(suspend_state_t state) |
| 692 | { |
| 693 | unsigned long alarm_time = RTAR; |
| 694 | unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0); |
| 695 | |
| 696 | dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n"); |
| 697 | |
| 698 | corgi_goto_sleep(alarm_time, alarm_status, state); |
| 699 | |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 700 | while (corgi_enter_suspend(alarm_time, alarm_status, state)) |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 701 | {} |
| 702 | |
| 703 | if (sharpsl_pm.machinfo->earlyresume) |
| 704 | sharpsl_pm.machinfo->earlyresume(); |
| 705 | |
| 706 | dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n"); |
| 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * Check for fatal battery errors |
| 713 | * Fatal returns -1 |
| 714 | */ |
| 715 | static int sharpsl_fatal_check(void) |
| 716 | { |
| 717 | int buff[5], temp, i, acin; |
| 718 | |
| 719 | dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check entered\n"); |
| 720 | |
| 721 | /* Check AC-Adapter */ |
| 722 | acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN); |
| 723 | |
| 724 | if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) { |
| 725 | sharpsl_pm.machinfo->charge(0); |
| 726 | udelay(100); |
| 727 | sharpsl_pm.machinfo->discharge(1); /* enable discharge */ |
| 728 | mdelay(SHARPSL_WAIT_DISCHARGE_ON); |
| 729 | } |
| 730 | |
| 731 | if (sharpsl_pm.machinfo->discharge1) |
| 732 | sharpsl_pm.machinfo->discharge1(1); |
| 733 | |
| 734 | /* Check battery : check inserting battery ? */ |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 735 | for (i = 0; i < 5; i++) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 736 | buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT); |
| 737 | mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT); |
| 738 | } |
| 739 | |
| 740 | if (sharpsl_pm.machinfo->discharge1) |
| 741 | sharpsl_pm.machinfo->discharge1(0); |
| 742 | |
| 743 | if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) { |
| 744 | udelay(100); |
| 745 | sharpsl_pm.machinfo->charge(1); |
| 746 | sharpsl_pm.machinfo->discharge(0); |
| 747 | } |
| 748 | |
| 749 | temp = get_select_val(buff); |
| 750 | dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check: acin: %d, discharge voltage: %d, no discharge: %ld\n", acin, temp, sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT)); |
| 751 | |
| 752 | if ((acin && (temp < sharpsl_pm.machinfo->fatal_acin_volt)) || |
| 753 | (!acin && (temp < sharpsl_pm.machinfo->fatal_noacin_volt))) |
| 754 | return -1; |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | static int sharpsl_off_charge_error(void) |
| 759 | { |
| 760 | dev_err(sharpsl_pm.dev, "Offline Charger: Error occurred.\n"); |
| 761 | sharpsl_pm.machinfo->charge(0); |
| 762 | sharpsl_pm_led(SHARPSL_LED_ERROR); |
| 763 | sharpsl_pm.charge_mode = CHRG_ERROR; |
| 764 | return 1; |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * Charging Control while suspended |
| 769 | * Return 1 - go straight to sleep |
| 770 | * Return 0 - sleep or wakeup depending on other factors |
| 771 | */ |
| 772 | static int sharpsl_off_charge_battery(void) |
| 773 | { |
| 774 | int time; |
| 775 | |
| 776 | dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode); |
| 777 | |
| 778 | if (sharpsl_pm.charge_mode == CHRG_OFF) { |
| 779 | dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n"); |
| 780 | |
| 781 | /* AC Check */ |
| 782 | if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0)) |
| 783 | return sharpsl_off_charge_error(); |
| 784 | |
| 785 | /* Start Charging */ |
| 786 | sharpsl_pm_led(SHARPSL_LED_ON); |
| 787 | sharpsl_pm.machinfo->charge(0); |
| 788 | mdelay(SHARPSL_CHARGE_WAIT_TIME); |
| 789 | sharpsl_pm.machinfo->charge(1); |
| 790 | |
| 791 | sharpsl_pm.charge_mode = CHRG_ON; |
| 792 | sharpsl_pm.full_count = 0; |
| 793 | |
| 794 | return 1; |
| 795 | } else if (sharpsl_pm.charge_mode != CHRG_ON) { |
| 796 | return 1; |
| 797 | } |
| 798 | |
| 799 | if (sharpsl_pm.full_count == 0) { |
| 800 | int time; |
| 801 | |
| 802 | dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n"); |
| 803 | |
| 804 | if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0)) |
| 805 | return sharpsl_off_charge_error(); |
| 806 | |
| 807 | sharpsl_pm.machinfo->charge(0); |
| 808 | mdelay(SHARPSL_CHARGE_WAIT_TIME); |
| 809 | sharpsl_pm.machinfo->charge(1); |
| 810 | sharpsl_pm.charge_mode = CHRG_ON; |
| 811 | |
| 812 | mdelay(SHARPSL_CHARGE_CO_CHECK_TIME); |
| 813 | |
| 814 | time = RCNR; |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 815 | while (1) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 816 | /* Check if any wakeup event had occurred */ |
| 817 | if (sharpsl_pm.machinfo->charger_wakeup() != 0) |
| 818 | return 0; |
| 819 | /* Check for timeout */ |
| 820 | if ((RCNR - time) > SHARPSL_WAIT_CO_TIME) |
| 821 | return 1; |
| 822 | if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) { |
| 823 | dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occurred. Retrying to check\n"); |
| 824 | sharpsl_pm.full_count++; |
| 825 | sharpsl_pm.machinfo->charge(0); |
| 826 | mdelay(SHARPSL_CHARGE_WAIT_TIME); |
| 827 | sharpsl_pm.machinfo->charge(1); |
| 828 | return 1; |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n"); |
| 834 | |
| 835 | mdelay(SHARPSL_CHARGE_CO_CHECK_TIME); |
| 836 | |
| 837 | time = RCNR; |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 838 | while (1) { |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 839 | /* Check if any wakeup event had occurred */ |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 840 | if (sharpsl_pm.machinfo->charger_wakeup()) |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 841 | return 0; |
| 842 | /* Check for timeout */ |
| 843 | if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) { |
| 844 | if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) { |
| 845 | dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n"); |
| 846 | sharpsl_pm.full_count = 0; |
| 847 | } |
| 848 | sharpsl_pm.full_count++; |
| 849 | return 1; |
| 850 | } |
| 851 | if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) { |
| 852 | dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n"); |
| 853 | sharpsl_pm_led(SHARPSL_LED_OFF); |
| 854 | sharpsl_pm.machinfo->charge(0); |
| 855 | sharpsl_pm.charge_mode = CHRG_DONE; |
| 856 | return 1; |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | #else |
| 861 | #define sharpsl_pm_suspend NULL |
| 862 | #define sharpsl_pm_resume NULL |
| 863 | #endif |
| 864 | |
| 865 | static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 866 | { |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 867 | return sprintf(buf, "%d\n", sharpsl_pm.battstat.mainbat_percent); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 871 | { |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 872 | return sprintf(buf, "%d\n", sharpsl_pm.battstat.mainbat_voltage); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static DEVICE_ATTR(battery_percentage, 0444, battery_percentage_show, NULL); |
| 876 | static DEVICE_ATTR(battery_voltage, 0444, battery_voltage_show, NULL); |
| 877 | |
| 878 | extern void (*apm_get_power_status)(struct apm_power_info *); |
| 879 | |
| 880 | static void sharpsl_apm_get_power_status(struct apm_power_info *info) |
| 881 | { |
| 882 | info->ac_line_status = sharpsl_pm.battstat.ac_status; |
| 883 | |
| 884 | if (sharpsl_pm.charge_mode == CHRG_ON) |
| 885 | info->battery_status = APM_BATTERY_STATUS_CHARGING; |
| 886 | else |
| 887 | info->battery_status = sharpsl_pm.battstat.mainbat_status; |
| 888 | |
| 889 | info->battery_flag = (1 << info->battery_status); |
| 890 | info->battery_life = sharpsl_pm.battstat.mainbat_percent; |
| 891 | } |
| 892 | |
| 893 | #ifdef CONFIG_PM |
| 894 | static struct platform_suspend_ops sharpsl_pm_ops = { |
Pavel Machek | 51cdd92 | 2009-06-11 23:25:09 +0800 | [diff] [blame] | 895 | .prepare = pxa_pm_prepare, |
| 896 | .finish = pxa_pm_finish, |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 897 | .enter = corgi_pxa_pm_enter, |
| 898 | .valid = suspend_valid_only_mem, |
| 899 | }; |
| 900 | #endif |
| 901 | |
Uwe Kleine-König | 91a99df | 2010-02-04 20:56:49 +0100 | [diff] [blame] | 902 | static int __devinit sharpsl_pm_probe(struct platform_device *pdev) |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 903 | { |
| 904 | int ret; |
| 905 | |
| 906 | if (!pdev->dev.platform_data) |
| 907 | return -EINVAL; |
| 908 | |
| 909 | sharpsl_pm.dev = &pdev->dev; |
| 910 | sharpsl_pm.machinfo = pdev->dev.platform_data; |
| 911 | sharpsl_pm.charge_mode = CHRG_OFF; |
| 912 | sharpsl_pm.flags = 0; |
| 913 | |
| 914 | init_timer(&sharpsl_pm.ac_timer); |
| 915 | sharpsl_pm.ac_timer.function = sharpsl_ac_timer; |
| 916 | |
| 917 | init_timer(&sharpsl_pm.chrg_full_timer); |
| 918 | sharpsl_pm.chrg_full_timer.function = sharpsl_chrg_full_timer; |
| 919 | |
| 920 | led_trigger_register_simple("sharpsl-charge", &sharpsl_charge_led_trigger); |
| 921 | |
| 922 | sharpsl_pm.machinfo->init(); |
| 923 | |
Dmitry Eremin-Solenikov | d5af277 | 2009-03-28 18:18:54 +0300 | [diff] [blame] | 924 | gpio_request(sharpsl_pm.machinfo->gpio_acin, "AC IN"); |
| 925 | gpio_direction_input(sharpsl_pm.machinfo->gpio_acin); |
| 926 | gpio_request(sharpsl_pm.machinfo->gpio_batfull, "Battery Full"); |
| 927 | gpio_direction_input(sharpsl_pm.machinfo->gpio_batfull); |
| 928 | gpio_request(sharpsl_pm.machinfo->gpio_batlock, "Battery Lock"); |
| 929 | gpio_direction_input(sharpsl_pm.machinfo->gpio_batlock); |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 930 | |
| 931 | /* Register interrupt handlers */ |
Dmitry Eremin-Solenikov | 5452537 | 2009-03-28 18:18:53 +0300 | [diff] [blame] | 932 | if (request_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr, IRQF_DISABLED | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "AC Input Detect", sharpsl_ac_isr)) { |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 933 | dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin)); |
| 934 | } |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 935 | |
Dmitry Eremin-Solenikov | 5452537 | 2009-03-28 18:18:53 +0300 | [diff] [blame] | 936 | if (request_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr, IRQF_DISABLED | IRQF_TRIGGER_FALLING, "Battery Cover", sharpsl_fatal_isr)) { |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 937 | dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock)); |
| 938 | } |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 939 | |
| 940 | if (sharpsl_pm.machinfo->gpio_fatal) { |
Dmitry Eremin-Solenikov | 5452537 | 2009-03-28 18:18:53 +0300 | [diff] [blame] | 941 | if (request_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_fatal), sharpsl_fatal_isr, IRQF_DISABLED | IRQF_TRIGGER_FALLING, "Fatal Battery", sharpsl_fatal_isr)) { |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 942 | dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", IRQ_GPIO(sharpsl_pm.machinfo->gpio_fatal)); |
| 943 | } |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 944 | } |
| 945 | |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 946 | if (sharpsl_pm.machinfo->batfull_irq) { |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 947 | /* Register interrupt handler. */ |
Dmitry Eremin-Solenikov | 5452537 | 2009-03-28 18:18:53 +0300 | [diff] [blame] | 948 | if (request_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr, IRQF_DISABLED | IRQF_TRIGGER_RISING, "CO", sharpsl_chrg_full_isr)) { |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 949 | dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull)); |
| 950 | } |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 951 | } |
| 952 | |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 953 | ret = device_create_file(&pdev->dev, &dev_attr_battery_percentage); |
| 954 | ret |= device_create_file(&pdev->dev, &dev_attr_battery_voltage); |
| 955 | if (ret != 0) |
| 956 | dev_warn(&pdev->dev, "Failed to register attributes (%d)\n", ret); |
| 957 | |
| 958 | apm_get_power_status = sharpsl_apm_get_power_status; |
| 959 | |
| 960 | #ifdef CONFIG_PM |
| 961 | suspend_set_ops(&sharpsl_pm_ops); |
| 962 | #endif |
| 963 | |
| 964 | mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250)); |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | static int sharpsl_pm_remove(struct platform_device *pdev) |
| 970 | { |
| 971 | suspend_set_ops(NULL); |
| 972 | |
| 973 | device_remove_file(&pdev->dev, &dev_attr_battery_percentage); |
| 974 | device_remove_file(&pdev->dev, &dev_attr_battery_voltage); |
| 975 | |
| 976 | led_trigger_unregister_simple(sharpsl_charge_led_trigger); |
| 977 | |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 978 | free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr); |
| 979 | free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr); |
| 980 | |
| 981 | if (sharpsl_pm.machinfo->gpio_fatal) |
| 982 | free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_fatal), sharpsl_fatal_isr); |
| 983 | |
| 984 | if (sharpsl_pm.machinfo->batfull_irq) |
| 985 | free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr); |
| 986 | |
Dmitry Eremin-Solenikov | d5af277 | 2009-03-28 18:18:54 +0300 | [diff] [blame] | 987 | gpio_free(sharpsl_pm.machinfo->gpio_batlock); |
| 988 | gpio_free(sharpsl_pm.machinfo->gpio_batfull); |
| 989 | gpio_free(sharpsl_pm.machinfo->gpio_acin); |
| 990 | |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 991 | if (sharpsl_pm.machinfo->exit) |
| 992 | sharpsl_pm.machinfo->exit(); |
Dmitry Eremin-Solenikov | 78731d3 | 2009-03-28 18:18:51 +0300 | [diff] [blame] | 993 | |
| 994 | del_timer_sync(&sharpsl_pm.chrg_full_timer); |
| 995 | del_timer_sync(&sharpsl_pm.ac_timer); |
| 996 | |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | static struct platform_driver sharpsl_pm_driver = { |
| 1001 | .probe = sharpsl_pm_probe, |
| 1002 | .remove = sharpsl_pm_remove, |
| 1003 | .suspend = sharpsl_pm_suspend, |
| 1004 | .resume = sharpsl_pm_resume, |
| 1005 | .driver = { |
| 1006 | .name = "sharpsl-pm", |
| 1007 | }, |
| 1008 | }; |
| 1009 | |
| 1010 | static int __devinit sharpsl_pm_init(void) |
| 1011 | { |
| 1012 | return platform_driver_register(&sharpsl_pm_driver); |
| 1013 | } |
| 1014 | |
| 1015 | static void sharpsl_pm_exit(void) |
| 1016 | { |
| 1017 | platform_driver_unregister(&sharpsl_pm_driver); |
| 1018 | } |
| 1019 | |
| 1020 | late_initcall(sharpsl_pm_init); |
| 1021 | module_exit(sharpsl_pm_exit); |