spitz_pm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Battery and Power Management code for the Sharp SL-Cxx00
  3. *
  4. * Copyright (c) 2005 Richard Purdie
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/stat.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/platform_device.h>
  18. #include <asm/apm.h>
  19. #include <asm/irq.h>
  20. #include <asm/mach-types.h>
  21. #include <asm/hardware.h>
  22. #include <asm/hardware/scoop.h>
  23. #include <asm/arch/sharpsl.h>
  24. #include <asm/arch/spitz.h>
  25. #include <asm/arch/pxa-regs.h>
  26. #include "sharpsl.h"
  27. static int spitz_last_ac_status;
  28. static void spitz_charger_init(void)
  29. {
  30. pxa_gpio_mode(SPITZ_GPIO_KEY_INT | GPIO_IN);
  31. pxa_gpio_mode(SPITZ_GPIO_SYNC | GPIO_IN);
  32. }
  33. static void spitz_charge_led(int val)
  34. {
  35. if (val == SHARPSL_LED_ERROR) {
  36. dev_dbg(sharpsl_pm.dev, "Charge LED Error\n");
  37. } else if (val == SHARPSL_LED_ON) {
  38. dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
  39. set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_ORANGE);
  40. } else {
  41. dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
  42. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_ORANGE);
  43. }
  44. }
  45. static void spitz_measure_temp(int on)
  46. {
  47. if (on)
  48. set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_ADC_TEMP_ON);
  49. else
  50. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_ADC_TEMP_ON);
  51. }
  52. static void spitz_charge(int on)
  53. {
  54. if (on) {
  55. if (sharpsl_pm.flags & SHARPSL_SUSPENDED) {
  56. set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_B);
  57. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_CHRG_ON);
  58. } else {
  59. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_B);
  60. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_CHRG_ON);
  61. }
  62. } else {
  63. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_B);
  64. set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_CHRG_ON);
  65. }
  66. }
  67. static void spitz_discharge(int on)
  68. {
  69. if (on)
  70. set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_A);
  71. else
  72. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_A);
  73. }
  74. /* HACK - For unknown reasons, accurate voltage readings are only made with a load
  75. on the power bus which the green led on spitz provides */
  76. static void spitz_discharge1(int on)
  77. {
  78. if (on)
  79. set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_GREEN);
  80. else
  81. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_GREEN);
  82. }
  83. static void spitz_presuspend(void)
  84. {
  85. spitz_last_ac_status = STATUS_AC_IN();
  86. /* GPIO Sleep Register */
  87. PGSR0 = 0x00144018;
  88. PGSR1 = 0x00EF0000;
  89. if (machine_is_akita()) {
  90. PGSR2 = 0x2121C000;
  91. PGSR3 = 0x00600400;
  92. } else {
  93. PGSR2 = 0x0121C000;
  94. PGSR3 = 0x00600000;
  95. }
  96. PGSR0 &= ~SPITZ_GPIO_G0_STROBE_BIT;
  97. PGSR1 &= ~SPITZ_GPIO_G1_STROBE_BIT;
  98. PGSR2 &= ~SPITZ_GPIO_G2_STROBE_BIT;
  99. PGSR3 &= ~SPITZ_GPIO_G3_STROBE_BIT;
  100. PGSR2 |= GPIO_bit(SPITZ_GPIO_KEY_STROBE0);
  101. pxa_gpio_mode(GPIO18_RDY|GPIO_OUT | GPIO_DFLT_HIGH);
  102. PRER = GPIO_bit(SPITZ_GPIO_KEY_INT);
  103. PFER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
  104. PWER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET) | PWER_RTC;
  105. PKWR = GPIO_bit(SPITZ_GPIO_SYNC) | GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
  106. PKSR = 0xffffffff; // clear
  107. /* nRESET_OUT Disable */
  108. PSLR |= PSLR_SL_ROD;
  109. /* Clear reset status */
  110. RCSR = RCSR_HWR | RCSR_WDR | RCSR_SMR | RCSR_GPR;
  111. /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */
  112. PCFR = PCFR_GPR_EN | PCFR_OPDE;
  113. }
  114. static void spitz_postsuspend(void)
  115. {
  116. pxa_gpio_mode(GPIO18_RDY_MD);
  117. pxa_gpio_mode(10 | GPIO_IN);
  118. }
  119. static int spitz_should_wakeup(unsigned int resume_on_alarm)
  120. {
  121. int is_resume = 0;
  122. int acin = STATUS_AC_IN();
  123. if (spitz_last_ac_status != acin) {
  124. if (acin) {
  125. /* charge on */
  126. sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
  127. dev_dbg(sharpsl_pm.dev, "AC Inserted\n");
  128. } else {
  129. /* charge off */
  130. dev_dbg(sharpsl_pm.dev, "AC Removed\n");
  131. CHARGE_LED_OFF();
  132. CHARGE_OFF();
  133. sharpsl_pm.charge_mode = CHRG_OFF;
  134. }
  135. spitz_last_ac_status = acin;
  136. /* Return to suspend as this must be what we were woken for */
  137. return 0;
  138. }
  139. if (PEDR & GPIO_bit(SPITZ_GPIO_KEY_INT))
  140. is_resume |= GPIO_bit(SPITZ_GPIO_KEY_INT);
  141. if (PKSR & GPIO_bit(SPITZ_GPIO_SYNC))
  142. is_resume |= GPIO_bit(SPITZ_GPIO_SYNC);
  143. if (resume_on_alarm && (PEDR & PWER_RTC))
  144. is_resume |= PWER_RTC;
  145. dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
  146. return is_resume;
  147. }
  148. static unsigned long spitz_charger_wakeup(void)
  149. {
  150. return (~GPLR0 & GPIO_bit(SPITZ_GPIO_KEY_INT)) | (GPLR0 & GPIO_bit(SPITZ_GPIO_SYNC));
  151. }
  152. static int spitz_acin_status(void)
  153. {
  154. return (((~GPLR(SPITZ_GPIO_AC_IN)) & GPIO_bit(SPITZ_GPIO_AC_IN)) != 0);
  155. }
  156. struct sharpsl_charger_machinfo spitz_pm_machinfo = {
  157. .init = spitz_charger_init,
  158. .gpio_batlock = SPITZ_GPIO_BAT_COVER,
  159. .gpio_acin = SPITZ_GPIO_AC_IN,
  160. .gpio_batfull = SPITZ_GPIO_CHRG_FULL,
  161. .gpio_fatal = SPITZ_GPIO_FATAL_BAT,
  162. .status_acin = spitz_acin_status,
  163. .discharge = spitz_discharge,
  164. .discharge1 = spitz_discharge1,
  165. .charge = spitz_charge,
  166. .chargeled = spitz_charge_led,
  167. .measure_temp = spitz_measure_temp,
  168. .presuspend = spitz_presuspend,
  169. .postsuspend = spitz_postsuspend,
  170. .charger_wakeup = spitz_charger_wakeup,
  171. .should_wakeup = spitz_should_wakeup,
  172. .bat_levels = 40,
  173. .bat_levels_noac = spitz_battery_levels_noac,
  174. .bat_levels_acin = spitz_battery_levels_acin,
  175. .status_high_acin = 188,
  176. .status_low_acin = 178,
  177. .status_high_noac = 185,
  178. .status_low_noac = 175,
  179. };
  180. static struct platform_device *spitzpm_device;
  181. static int __devinit spitzpm_init(void)
  182. {
  183. int ret;
  184. spitzpm_device = platform_device_alloc("sharpsl-pm", -1);
  185. if (!spitzpm_device)
  186. return -ENOMEM;
  187. spitzpm_device->dev.platform_data = &spitz_pm_machinfo;
  188. ret = platform_device_add(spitzpm_device);
  189. if (ret)
  190. platform_device_put(spitzpm_device);
  191. return ret;
  192. }
  193. static void spitzpm_exit(void)
  194. {
  195. platform_device_unregister(spitzpm_device);
  196. }
  197. module_init(spitzpm_init);
  198. module_exit(spitzpm_exit);