sharpsl_pm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. #undef DEBUG
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/apm-emulation.h>
  20. #include <linux/timer.h>
  21. #include <linux/delay.h>
  22. #include <linux/leds.h>
  23. #include <linux/suspend.h>
  24. #include <linux/gpio.h>
  25. #include <linux/io.h>
  26. #include <asm/mach-types.h>
  27. #include "pm.h"
  28. #include <mach/pxa2xx-regs.h>
  29. #include "regs-rtc.h"
  30. #include "sharpsl_pm.h"
  31. /*
  32. * Constants
  33. */
  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 */
  38. #define SHARPSL_WAIT_CO_TIME 15 /* 15 sec */
  39. #define SHARPSL_WAIT_DISCHARGE_ON 100 /* 100 msec */
  40. #define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP 10 /* 10 msec */
  41. #define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT 10 /* 10 msec */
  42. #define SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN 10 /* 10 msec */
  43. #define SHARPSL_CHARGE_WAIT_TIME 15 /* 15 msec */
  44. #define SHARPSL_CHARGE_CO_CHECK_TIME 5 /* 5 msec */
  45. #define SHARPSL_CHARGE_RETRY_CNT 1 /* eqv. 10 min */
  46. /*
  47. * Prototypes
  48. */
  49. #ifdef CONFIG_PM
  50. static int sharpsl_off_charge_battery(void);
  51. static int sharpsl_check_battery_voltage(void);
  52. #endif
  53. static int sharpsl_check_battery_temp(void);
  54. static int sharpsl_ac_check(void);
  55. static int sharpsl_average_value(int ad);
  56. static void sharpsl_average_clear(void);
  57. static void sharpsl_charge_toggle(struct work_struct *private_);
  58. static void sharpsl_battery_thread(struct work_struct *private_);
  59. /*
  60. * Variables
  61. */
  62. struct sharpsl_pm_status sharpsl_pm;
  63. static DECLARE_DELAYED_WORK(toggle_charger, sharpsl_charge_toggle);
  64. static DECLARE_DELAYED_WORK(sharpsl_bat, sharpsl_battery_thread);
  65. DEFINE_LED_TRIGGER(sharpsl_charge_led_trigger);
  66. struct battery_thresh sharpsl_battery_levels_acin[] = {
  67. { 213, 100},
  68. { 212, 98},
  69. { 211, 95},
  70. { 210, 93},
  71. { 209, 90},
  72. { 208, 88},
  73. { 207, 85},
  74. { 206, 83},
  75. { 205, 80},
  76. { 204, 78},
  77. { 203, 75},
  78. { 202, 73},
  79. { 201, 70},
  80. { 200, 68},
  81. { 199, 65},
  82. { 198, 63},
  83. { 197, 60},
  84. { 196, 58},
  85. { 195, 55},
  86. { 194, 53},
  87. { 193, 50},
  88. { 192, 48},
  89. { 192, 45},
  90. { 191, 43},
  91. { 191, 40},
  92. { 190, 38},
  93. { 190, 35},
  94. { 189, 33},
  95. { 188, 30},
  96. { 187, 28},
  97. { 186, 25},
  98. { 185, 23},
  99. { 184, 20},
  100. { 183, 18},
  101. { 182, 15},
  102. { 181, 13},
  103. { 180, 10},
  104. { 179, 8},
  105. { 178, 5},
  106. { 0, 0},
  107. };
  108. struct battery_thresh sharpsl_battery_levels_noac[] = {
  109. { 213, 100},
  110. { 212, 98},
  111. { 211, 95},
  112. { 210, 93},
  113. { 209, 90},
  114. { 208, 88},
  115. { 207, 85},
  116. { 206, 83},
  117. { 205, 80},
  118. { 204, 78},
  119. { 203, 75},
  120. { 202, 73},
  121. { 201, 70},
  122. { 200, 68},
  123. { 199, 65},
  124. { 198, 63},
  125. { 197, 60},
  126. { 196, 58},
  127. { 195, 55},
  128. { 194, 53},
  129. { 193, 50},
  130. { 192, 48},
  131. { 191, 45},
  132. { 190, 43},
  133. { 189, 40},
  134. { 188, 38},
  135. { 187, 35},
  136. { 186, 33},
  137. { 185, 30},
  138. { 184, 28},
  139. { 183, 25},
  140. { 182, 23},
  141. { 181, 20},
  142. { 180, 18},
  143. { 179, 15},
  144. { 178, 13},
  145. { 177, 10},
  146. { 176, 8},
  147. { 175, 5},
  148. { 0, 0},
  149. };
  150. /* MAX1111 Commands */
  151. #define MAXCTRL_PD0 (1u << 0)
  152. #define MAXCTRL_PD1 (1u << 1)
  153. #define MAXCTRL_SGL (1u << 2)
  154. #define MAXCTRL_UNI (1u << 3)
  155. #define MAXCTRL_SEL_SH 4
  156. #define MAXCTRL_STR (1u << 7)
  157. extern int max1111_read_channel(int);
  158. /*
  159. * Read MAX1111 ADC
  160. */
  161. int sharpsl_pm_pxa_read_max1111(int channel)
  162. {
  163. /* Ugly, better move this function into another module */
  164. if (machine_is_tosa())
  165. return 0;
  166. /* max1111 accepts channels from 0-3, however,
  167. * it is encoded from 0-7 here in the code.
  168. */
  169. return max1111_read_channel(channel >> 1);
  170. }
  171. static int get_percentage(int voltage)
  172. {
  173. int i = sharpsl_pm.machinfo->bat_levels - 1;
  174. int bl_status = sharpsl_pm.machinfo->backlight_get_status ? sharpsl_pm.machinfo->backlight_get_status() : 0;
  175. struct battery_thresh *thresh;
  176. if (sharpsl_pm.charge_mode == CHRG_ON)
  177. thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_acin_bl : sharpsl_pm.machinfo->bat_levels_acin;
  178. else
  179. thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_noac_bl : sharpsl_pm.machinfo->bat_levels_noac;
  180. while (i > 0 && (voltage > thresh[i].voltage))
  181. i--;
  182. return thresh[i].percentage;
  183. }
  184. static int get_apm_status(int voltage)
  185. {
  186. int low_thresh, high_thresh;
  187. if (sharpsl_pm.charge_mode == CHRG_ON) {
  188. high_thresh = sharpsl_pm.machinfo->status_high_acin;
  189. low_thresh = sharpsl_pm.machinfo->status_low_acin;
  190. } else {
  191. high_thresh = sharpsl_pm.machinfo->status_high_noac;
  192. low_thresh = sharpsl_pm.machinfo->status_low_noac;
  193. }
  194. if (voltage >= high_thresh)
  195. return APM_BATTERY_STATUS_HIGH;
  196. if (voltage >= low_thresh)
  197. return APM_BATTERY_STATUS_LOW;
  198. return APM_BATTERY_STATUS_CRITICAL;
  199. }
  200. void sharpsl_battery_kick(void)
  201. {
  202. schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125));
  203. }
  204. EXPORT_SYMBOL(sharpsl_battery_kick);
  205. static void sharpsl_battery_thread(struct work_struct *private_)
  206. {
  207. int voltage, percent, apm_status, i;
  208. if (!sharpsl_pm.machinfo)
  209. return;
  210. sharpsl_pm.battstat.ac_status = (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN) ? APM_AC_ONLINE : APM_AC_OFFLINE);
  211. /* Corgi cannot confirm when battery fully charged so periodically kick! */
  212. if (!sharpsl_pm.machinfo->batfull_irq && (sharpsl_pm.charge_mode == CHRG_ON)
  213. && time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_ON_TIME_INTERVAL))
  214. schedule_delayed_work(&toggle_charger, 0);
  215. for (i = 0; i < 5; i++) {
  216. voltage = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
  217. if (voltage > 0)
  218. break;
  219. }
  220. if (voltage <= 0) {
  221. voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage;
  222. dev_warn(sharpsl_pm.dev, "Warning: Cannot read main battery!\n");
  223. }
  224. voltage = sharpsl_average_value(voltage);
  225. apm_status = get_apm_status(voltage);
  226. percent = get_percentage(voltage);
  227. /* At low battery voltages, the voltage has a tendency to start
  228. creeping back up so we try to avoid this here */
  229. if ((sharpsl_pm.battstat.ac_status == APM_AC_ONLINE)
  230. || (apm_status == APM_BATTERY_STATUS_HIGH)
  231. || percent <= sharpsl_pm.battstat.mainbat_percent) {
  232. sharpsl_pm.battstat.mainbat_voltage = voltage;
  233. sharpsl_pm.battstat.mainbat_status = apm_status;
  234. sharpsl_pm.battstat.mainbat_percent = percent;
  235. }
  236. dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %ld\n", voltage,
  237. sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies);
  238. /* Suspend if critical battery level */
  239. if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
  240. && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL)
  241. && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) {
  242. sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
  243. dev_err(sharpsl_pm.dev, "Fatal Off\n");
  244. apm_queue_event(APM_CRITICAL_SUSPEND);
  245. }
  246. schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME);
  247. }
  248. void sharpsl_pm_led(int val)
  249. {
  250. if (val == SHARPSL_LED_ERROR) {
  251. dev_err(sharpsl_pm.dev, "Charging Error!\n");
  252. } else if (val == SHARPSL_LED_ON) {
  253. dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
  254. led_trigger_event(sharpsl_charge_led_trigger, LED_FULL);
  255. } else {
  256. dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
  257. led_trigger_event(sharpsl_charge_led_trigger, LED_OFF);
  258. }
  259. }
  260. static void sharpsl_charge_on(void)
  261. {
  262. dev_dbg(sharpsl_pm.dev, "Turning Charger On\n");
  263. sharpsl_pm.full_count = 0;
  264. sharpsl_pm.charge_mode = CHRG_ON;
  265. schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250));
  266. schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500));
  267. }
  268. static void sharpsl_charge_off(void)
  269. {
  270. dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n");
  271. sharpsl_pm.machinfo->charge(0);
  272. sharpsl_pm_led(SHARPSL_LED_OFF);
  273. sharpsl_pm.charge_mode = CHRG_OFF;
  274. schedule_delayed_work(&sharpsl_bat, 0);
  275. }
  276. static void sharpsl_charge_error(void)
  277. {
  278. sharpsl_pm_led(SHARPSL_LED_ERROR);
  279. sharpsl_pm.machinfo->charge(0);
  280. sharpsl_pm.charge_mode = CHRG_ERROR;
  281. }
  282. static void sharpsl_charge_toggle(struct work_struct *private_)
  283. {
  284. dev_dbg(sharpsl_pm.dev, "Toggling Charger at time: %lx\n", jiffies);
  285. if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
  286. sharpsl_charge_off();
  287. return;
  288. } else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) {
  289. sharpsl_charge_error();
  290. return;
  291. }
  292. sharpsl_pm_led(SHARPSL_LED_ON);
  293. sharpsl_pm.machinfo->charge(0);
  294. mdelay(SHARPSL_CHARGE_WAIT_TIME);
  295. sharpsl_pm.machinfo->charge(1);
  296. sharpsl_pm.charge_start_time = jiffies;
  297. }
  298. static void sharpsl_ac_timer(struct timer_list *unused)
  299. {
  300. int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
  301. dev_dbg(sharpsl_pm.dev, "AC Status: %d\n", acin);
  302. sharpsl_average_clear();
  303. if (acin && (sharpsl_pm.charge_mode != CHRG_ON))
  304. sharpsl_charge_on();
  305. else if (sharpsl_pm.charge_mode == CHRG_ON)
  306. sharpsl_charge_off();
  307. schedule_delayed_work(&sharpsl_bat, 0);
  308. }
  309. static irqreturn_t sharpsl_ac_isr(int irq, void *dev_id)
  310. {
  311. /* Delay the event slightly to debounce */
  312. /* Must be a smaller delay than the chrg_full_isr below */
  313. mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
  314. return IRQ_HANDLED;
  315. }
  316. static void sharpsl_chrg_full_timer(struct timer_list *unused)
  317. {
  318. dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies);
  319. sharpsl_pm.full_count++;
  320. if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
  321. dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n");
  322. if (sharpsl_pm.charge_mode == CHRG_ON)
  323. sharpsl_charge_off();
  324. } else if (sharpsl_pm.full_count < 2) {
  325. dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n");
  326. schedule_delayed_work(&toggle_charger, 0);
  327. } else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) {
  328. dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n");
  329. schedule_delayed_work(&toggle_charger, 0);
  330. } else {
  331. sharpsl_charge_off();
  332. sharpsl_pm.charge_mode = CHRG_DONE;
  333. dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n");
  334. }
  335. }
  336. /* Charging Finished Interrupt (Not present on Corgi) */
  337. /* Can trigger at the same time as an AC status change so
  338. delay until after that has been processed */
  339. static irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id)
  340. {
  341. if (sharpsl_pm.flags & SHARPSL_SUSPENDED)
  342. return IRQ_HANDLED;
  343. /* delay until after any ac interrupt */
  344. mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500));
  345. return IRQ_HANDLED;
  346. }
  347. static irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id)
  348. {
  349. int is_fatal = 0;
  350. if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) {
  351. dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n");
  352. is_fatal = 1;
  353. }
  354. if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL)) {
  355. dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n");
  356. is_fatal = 1;
  357. }
  358. if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) {
  359. sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
  360. apm_queue_event(APM_CRITICAL_SUSPEND);
  361. }
  362. return IRQ_HANDLED;
  363. }
  364. /*
  365. * Maintain an average of the last 10 readings
  366. */
  367. #define SHARPSL_CNV_VALUE_NUM 10
  368. static int sharpsl_ad_index;
  369. static void sharpsl_average_clear(void)
  370. {
  371. sharpsl_ad_index = 0;
  372. }
  373. static int sharpsl_average_value(int ad)
  374. {
  375. int i, ad_val = 0;
  376. static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1];
  377. if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) {
  378. sharpsl_ad_index = 0;
  379. return ad;
  380. }
  381. sharpsl_ad[sharpsl_ad_index] = ad;
  382. sharpsl_ad_index++;
  383. if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) {
  384. for (i = 0; i < (SHARPSL_CNV_VALUE_NUM-1); i++)
  385. sharpsl_ad[i] = sharpsl_ad[i+1];
  386. sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1;
  387. }
  388. for (i = 0; i < sharpsl_ad_index; i++)
  389. ad_val += sharpsl_ad[i];
  390. return ad_val / sharpsl_ad_index;
  391. }
  392. /*
  393. * Take an array of 5 integers, remove the maximum and minimum values
  394. * and return the average.
  395. */
  396. static int get_select_val(int *val)
  397. {
  398. int i, j, k, temp, sum = 0;
  399. /* Find MAX val */
  400. temp = val[0];
  401. j = 0;
  402. for (i = 1; i < 5; i++) {
  403. if (temp < val[i]) {
  404. temp = val[i];
  405. j = i;
  406. }
  407. }
  408. /* Find MIN val */
  409. temp = val[4];
  410. k = 4;
  411. for (i = 3; i >= 0; i--) {
  412. if (temp > val[i]) {
  413. temp = val[i];
  414. k = i;
  415. }
  416. }
  417. for (i = 0; i < 5; i++)
  418. if (i != j && i != k)
  419. sum += val[i];
  420. 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]);
  421. return sum/3;
  422. }
  423. static int sharpsl_check_battery_temp(void)
  424. {
  425. int val, i, buff[5];
  426. /* Check battery temperature */
  427. for (i = 0; i < 5; i++) {
  428. mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
  429. sharpsl_pm.machinfo->measure_temp(1);
  430. mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
  431. buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_TEMP);
  432. sharpsl_pm.machinfo->measure_temp(0);
  433. }
  434. val = get_select_val(buff);
  435. dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val);
  436. if (val > sharpsl_pm.machinfo->charge_on_temp) {
  437. printk(KERN_WARNING "Not charging: temperature out of limits.\n");
  438. return -1;
  439. }
  440. return 0;
  441. }
  442. #ifdef CONFIG_PM
  443. static int sharpsl_check_battery_voltage(void)
  444. {
  445. int val, i, buff[5];
  446. /* disable charge, enable discharge */
  447. sharpsl_pm.machinfo->charge(0);
  448. sharpsl_pm.machinfo->discharge(1);
  449. mdelay(SHARPSL_WAIT_DISCHARGE_ON);
  450. if (sharpsl_pm.machinfo->discharge1)
  451. sharpsl_pm.machinfo->discharge1(1);
  452. /* Check battery voltage */
  453. for (i = 0; i < 5; i++) {
  454. buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
  455. mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
  456. }
  457. if (sharpsl_pm.machinfo->discharge1)
  458. sharpsl_pm.machinfo->discharge1(0);
  459. sharpsl_pm.machinfo->discharge(0);
  460. val = get_select_val(buff);
  461. dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val);
  462. if (val < sharpsl_pm.machinfo->charge_on_volt)
  463. return -1;
  464. return 0;
  465. }
  466. #endif
  467. static int sharpsl_ac_check(void)
  468. {
  469. int temp, i, buff[5];
  470. for (i = 0; i < 5; i++) {
  471. buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_ACIN_VOLT);
  472. mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN);
  473. }
  474. temp = get_select_val(buff);
  475. dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n", temp);
  476. if ((temp > sharpsl_pm.machinfo->charge_acin_high) || (temp < sharpsl_pm.machinfo->charge_acin_low)) {
  477. dev_err(sharpsl_pm.dev, "Error: AC check failed: voltage %d.\n", temp);
  478. return -1;
  479. }
  480. return 0;
  481. }
  482. #ifdef CONFIG_PM
  483. static int sharpsl_pm_suspend(struct platform_device *pdev, pm_message_t state)
  484. {
  485. sharpsl_pm.flags |= SHARPSL_SUSPENDED;
  486. flush_delayed_work(&toggle_charger);
  487. flush_delayed_work(&sharpsl_bat);
  488. if (sharpsl_pm.charge_mode == CHRG_ON)
  489. sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
  490. else
  491. sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
  492. return 0;
  493. }
  494. static int sharpsl_pm_resume(struct platform_device *pdev)
  495. {
  496. /* Clear the reset source indicators as they break the bootloader upon reboot */
  497. RCSR = 0x0f;
  498. sharpsl_average_clear();
  499. sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED;
  500. sharpsl_pm.flags &= ~SHARPSL_SUSPENDED;
  501. return 0;
  502. }
  503. static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
  504. {
  505. dev_dbg(sharpsl_pm.dev, "Time is: %08x\n", RCNR);
  506. dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n", sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG);
  507. /* not charging and AC-IN! */
  508. if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN))) {
  509. dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n");
  510. sharpsl_pm.charge_mode = CHRG_OFF;
  511. sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
  512. sharpsl_off_charge_battery();
  513. }
  514. sharpsl_pm.machinfo->presuspend();
  515. PEDR = 0xffffffff; /* clear it */
  516. sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE;
  517. if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) {
  518. RTSR &= RTSR_ALE;
  519. RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND;
  520. dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n", RTAR);
  521. sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE;
  522. } else if (alarm_enable) {
  523. RTSR &= RTSR_ALE;
  524. RTAR = alarm_time;
  525. dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n", RTAR);
  526. } else {
  527. dev_dbg(sharpsl_pm.dev, "No alarms set.\n");
  528. }
  529. pxa_pm_enter(state);
  530. sharpsl_pm.machinfo->postsuspend();
  531. dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n", PEDR);
  532. }
  533. static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
  534. {
  535. if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable)) {
  536. if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) {
  537. dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n");
  538. corgi_goto_sleep(alarm_time, alarm_enable, state);
  539. return 1;
  540. }
  541. if (sharpsl_off_charge_battery()) {
  542. dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n");
  543. corgi_goto_sleep(alarm_time, alarm_enable, state);
  544. return 1;
  545. }
  546. dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n");
  547. }
  548. if ((!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) ||
  549. (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL))) {
  550. dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n");
  551. corgi_goto_sleep(alarm_time, alarm_enable, state);
  552. return 1;
  553. }
  554. return 0;
  555. }
  556. static int corgi_pxa_pm_enter(suspend_state_t state)
  557. {
  558. unsigned long alarm_time = RTAR;
  559. unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0);
  560. dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n");
  561. corgi_goto_sleep(alarm_time, alarm_status, state);
  562. while (corgi_enter_suspend(alarm_time, alarm_status, state))
  563. {}
  564. if (sharpsl_pm.machinfo->earlyresume)
  565. sharpsl_pm.machinfo->earlyresume();
  566. dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n");
  567. return 0;
  568. }
  569. static int sharpsl_off_charge_error(void)
  570. {
  571. dev_err(sharpsl_pm.dev, "Offline Charger: Error occurred.\n");
  572. sharpsl_pm.machinfo->charge(0);
  573. sharpsl_pm_led(SHARPSL_LED_ERROR);
  574. sharpsl_pm.charge_mode = CHRG_ERROR;
  575. return 1;
  576. }
  577. /*
  578. * Charging Control while suspended
  579. * Return 1 - go straight to sleep
  580. * Return 0 - sleep or wakeup depending on other factors
  581. */
  582. static int sharpsl_off_charge_battery(void)
  583. {
  584. int time;
  585. dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode);
  586. if (sharpsl_pm.charge_mode == CHRG_OFF) {
  587. dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n");
  588. /* AC Check */
  589. if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0))
  590. return sharpsl_off_charge_error();
  591. /* Start Charging */
  592. sharpsl_pm_led(SHARPSL_LED_ON);
  593. sharpsl_pm.machinfo->charge(0);
  594. mdelay(SHARPSL_CHARGE_WAIT_TIME);
  595. sharpsl_pm.machinfo->charge(1);
  596. sharpsl_pm.charge_mode = CHRG_ON;
  597. sharpsl_pm.full_count = 0;
  598. return 1;
  599. } else if (sharpsl_pm.charge_mode != CHRG_ON) {
  600. return 1;
  601. }
  602. if (sharpsl_pm.full_count == 0) {
  603. int time;
  604. dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n");
  605. if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0))
  606. return sharpsl_off_charge_error();
  607. sharpsl_pm.machinfo->charge(0);
  608. mdelay(SHARPSL_CHARGE_WAIT_TIME);
  609. sharpsl_pm.machinfo->charge(1);
  610. sharpsl_pm.charge_mode = CHRG_ON;
  611. mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
  612. time = RCNR;
  613. while (1) {
  614. /* Check if any wakeup event had occurred */
  615. if (sharpsl_pm.machinfo->charger_wakeup())
  616. return 0;
  617. /* Check for timeout */
  618. if ((RCNR - time) > SHARPSL_WAIT_CO_TIME)
  619. return 1;
  620. if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
  621. dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occurred. Retrying to check\n");
  622. sharpsl_pm.full_count++;
  623. sharpsl_pm.machinfo->charge(0);
  624. mdelay(SHARPSL_CHARGE_WAIT_TIME);
  625. sharpsl_pm.machinfo->charge(1);
  626. return 1;
  627. }
  628. }
  629. }
  630. dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n");
  631. mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
  632. time = RCNR;
  633. while (1) {
  634. /* Check if any wakeup event had occurred */
  635. if (sharpsl_pm.machinfo->charger_wakeup())
  636. return 0;
  637. /* Check for timeout */
  638. if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) {
  639. if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) {
  640. dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n");
  641. sharpsl_pm.full_count = 0;
  642. }
  643. sharpsl_pm.full_count++;
  644. return 1;
  645. }
  646. if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
  647. dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n");
  648. sharpsl_pm_led(SHARPSL_LED_OFF);
  649. sharpsl_pm.machinfo->charge(0);
  650. sharpsl_pm.charge_mode = CHRG_DONE;
  651. return 1;
  652. }
  653. }
  654. }
  655. #else
  656. #define sharpsl_pm_suspend NULL
  657. #define sharpsl_pm_resume NULL
  658. #endif
  659. static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf)
  660. {
  661. return sprintf(buf, "%d\n", sharpsl_pm.battstat.mainbat_percent);
  662. }
  663. static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
  664. {
  665. return sprintf(buf, "%d\n", sharpsl_pm.battstat.mainbat_voltage);
  666. }
  667. static DEVICE_ATTR_RO(battery_percentage);
  668. static DEVICE_ATTR_RO(battery_voltage);
  669. extern void (*apm_get_power_status)(struct apm_power_info *);
  670. static void sharpsl_apm_get_power_status(struct apm_power_info *info)
  671. {
  672. info->ac_line_status = sharpsl_pm.battstat.ac_status;
  673. if (sharpsl_pm.charge_mode == CHRG_ON)
  674. info->battery_status = APM_BATTERY_STATUS_CHARGING;
  675. else
  676. info->battery_status = sharpsl_pm.battstat.mainbat_status;
  677. info->battery_flag = (1 << info->battery_status);
  678. info->battery_life = sharpsl_pm.battstat.mainbat_percent;
  679. }
  680. #ifdef CONFIG_PM
  681. static const struct platform_suspend_ops sharpsl_pm_ops = {
  682. .prepare = pxa_pm_prepare,
  683. .finish = pxa_pm_finish,
  684. .enter = corgi_pxa_pm_enter,
  685. .valid = suspend_valid_only_mem,
  686. };
  687. #endif
  688. static int sharpsl_pm_probe(struct platform_device *pdev)
  689. {
  690. int ret, irq;
  691. if (!pdev->dev.platform_data)
  692. return -EINVAL;
  693. sharpsl_pm.dev = &pdev->dev;
  694. sharpsl_pm.machinfo = pdev->dev.platform_data;
  695. sharpsl_pm.charge_mode = CHRG_OFF;
  696. sharpsl_pm.flags = 0;
  697. timer_setup(&sharpsl_pm.ac_timer, sharpsl_ac_timer, 0);
  698. timer_setup(&sharpsl_pm.chrg_full_timer, sharpsl_chrg_full_timer, 0);
  699. led_trigger_register_simple("sharpsl-charge", &sharpsl_charge_led_trigger);
  700. sharpsl_pm.machinfo->init();
  701. gpio_request(sharpsl_pm.machinfo->gpio_acin, "AC IN");
  702. gpio_direction_input(sharpsl_pm.machinfo->gpio_acin);
  703. gpio_request(sharpsl_pm.machinfo->gpio_batfull, "Battery Full");
  704. gpio_direction_input(sharpsl_pm.machinfo->gpio_batfull);
  705. gpio_request(sharpsl_pm.machinfo->gpio_batlock, "Battery Lock");
  706. gpio_direction_input(sharpsl_pm.machinfo->gpio_batlock);
  707. /* Register interrupt handlers */
  708. irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_acin);
  709. if (request_irq(irq, sharpsl_ac_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "AC Input Detect", sharpsl_ac_isr)) {
  710. dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
  711. }
  712. irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_batlock);
  713. if (request_irq(irq, sharpsl_fatal_isr, IRQF_TRIGGER_FALLING, "Battery Cover", sharpsl_fatal_isr)) {
  714. dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
  715. }
  716. if (sharpsl_pm.machinfo->gpio_fatal) {
  717. irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_fatal);
  718. if (request_irq(irq, sharpsl_fatal_isr, IRQF_TRIGGER_FALLING, "Fatal Battery", sharpsl_fatal_isr)) {
  719. dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
  720. }
  721. }
  722. if (sharpsl_pm.machinfo->batfull_irq) {
  723. /* Register interrupt handler. */
  724. irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_batfull);
  725. if (request_irq(irq, sharpsl_chrg_full_isr, IRQF_TRIGGER_RISING, "CO", sharpsl_chrg_full_isr)) {
  726. dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
  727. }
  728. }
  729. ret = device_create_file(&pdev->dev, &dev_attr_battery_percentage);
  730. ret |= device_create_file(&pdev->dev, &dev_attr_battery_voltage);
  731. if (ret != 0)
  732. dev_warn(&pdev->dev, "Failed to register attributes (%d)\n", ret);
  733. apm_get_power_status = sharpsl_apm_get_power_status;
  734. #ifdef CONFIG_PM
  735. suspend_set_ops(&sharpsl_pm_ops);
  736. #endif
  737. mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
  738. return 0;
  739. }
  740. static int sharpsl_pm_remove(struct platform_device *pdev)
  741. {
  742. suspend_set_ops(NULL);
  743. device_remove_file(&pdev->dev, &dev_attr_battery_percentage);
  744. device_remove_file(&pdev->dev, &dev_attr_battery_voltage);
  745. led_trigger_unregister_simple(sharpsl_charge_led_trigger);
  746. free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr);
  747. free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr);
  748. if (sharpsl_pm.machinfo->gpio_fatal)
  749. free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_fatal), sharpsl_fatal_isr);
  750. if (sharpsl_pm.machinfo->batfull_irq)
  751. free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr);
  752. gpio_free(sharpsl_pm.machinfo->gpio_batlock);
  753. gpio_free(sharpsl_pm.machinfo->gpio_batfull);
  754. gpio_free(sharpsl_pm.machinfo->gpio_acin);
  755. if (sharpsl_pm.machinfo->exit)
  756. sharpsl_pm.machinfo->exit();
  757. del_timer_sync(&sharpsl_pm.chrg_full_timer);
  758. del_timer_sync(&sharpsl_pm.ac_timer);
  759. return 0;
  760. }
  761. static struct platform_driver sharpsl_pm_driver = {
  762. .probe = sharpsl_pm_probe,
  763. .remove = sharpsl_pm_remove,
  764. .suspend = sharpsl_pm_suspend,
  765. .resume = sharpsl_pm_resume,
  766. .driver = {
  767. .name = "sharpsl-pm",
  768. },
  769. };
  770. static int sharpsl_pm_init(void)
  771. {
  772. return platform_driver_register(&sharpsl_pm_driver);
  773. }
  774. static void sharpsl_pm_exit(void)
  775. {
  776. platform_driver_unregister(&sharpsl_pm_driver);
  777. }
  778. late_initcall(sharpsl_pm_init);
  779. module_exit(sharpsl_pm_exit);