corgi_pm.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Battery and Power Management code for the Sharp SL-C7xx
  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/corgi.h>
  25. #include <asm/arch/pxa-regs.h>
  26. #include "sharpsl.h"
  27. static void corgi_charger_init(void)
  28. {
  29. pxa_gpio_mode(CORGI_GPIO_ADC_TEMP_ON | GPIO_OUT);
  30. pxa_gpio_mode(CORGI_GPIO_CHRG_ON | GPIO_OUT);
  31. pxa_gpio_mode(CORGI_GPIO_CHRG_UKN | GPIO_OUT);
  32. pxa_gpio_mode(CORGI_GPIO_KEY_INT | GPIO_IN);
  33. sharpsl_pm_pxa_init();
  34. }
  35. static void corgi_measure_temp(int on)
  36. {
  37. if (on)
  38. GPSR(CORGI_GPIO_ADC_TEMP_ON) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON);
  39. else
  40. GPCR(CORGI_GPIO_ADC_TEMP_ON) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON);
  41. }
  42. static void corgi_charge(int on)
  43. {
  44. if (on) {
  45. if (machine_is_corgi() && (sharpsl_pm.flags & SHARPSL_SUSPENDED)) {
  46. GPCR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
  47. GPSR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
  48. } else {
  49. GPSR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
  50. GPCR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
  51. }
  52. } else {
  53. GPCR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
  54. GPCR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
  55. }
  56. }
  57. static void corgi_discharge(int on)
  58. {
  59. if (on)
  60. GPSR(CORGI_GPIO_DISCHARGE_ON) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON);
  61. else
  62. GPCR(CORGI_GPIO_DISCHARGE_ON) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON);
  63. }
  64. static void corgi_presuspend(void)
  65. {
  66. int i;
  67. unsigned long wakeup_mask;
  68. /* charging , so CHARGE_ON bit is HIGH during OFF. */
  69. if (READ_GPIO_BIT(CORGI_GPIO_CHRG_ON))
  70. PGSR1 |= GPIO_bit(CORGI_GPIO_CHRG_ON);
  71. else
  72. PGSR1 &= ~GPIO_bit(CORGI_GPIO_CHRG_ON);
  73. if (READ_GPIO_BIT(CORGI_GPIO_LED_ORANGE))
  74. PGSR0 |= GPIO_bit(CORGI_GPIO_LED_ORANGE);
  75. else
  76. PGSR0 &= ~GPIO_bit(CORGI_GPIO_LED_ORANGE);
  77. if (READ_GPIO_BIT(CORGI_GPIO_CHRG_UKN))
  78. PGSR1 |= GPIO_bit(CORGI_GPIO_CHRG_UKN);
  79. else
  80. PGSR1 &= ~GPIO_bit(CORGI_GPIO_CHRG_UKN);
  81. /* Resume on keyboard power key */
  82. PGSR2 = (PGSR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(0);
  83. wakeup_mask = GPIO_bit(CORGI_GPIO_KEY_INT) | GPIO_bit(CORGI_GPIO_WAKEUP) | GPIO_bit(CORGI_GPIO_AC_IN) | GPIO_bit(CORGI_GPIO_CHRG_FULL);
  84. if (!machine_is_corgi())
  85. wakeup_mask |= GPIO_bit(CORGI_GPIO_MAIN_BAT_LOW);
  86. PWER = wakeup_mask | PWER_RTC;
  87. PRER = wakeup_mask;
  88. PFER = wakeup_mask;
  89. for (i = 0; i <=15; i++) {
  90. if (PRER & PFER & GPIO_bit(i)) {
  91. if (GPLR0 & GPIO_bit(i) )
  92. PRER &= ~GPIO_bit(i);
  93. else
  94. PFER &= ~GPIO_bit(i);
  95. }
  96. }
  97. }
  98. static void corgi_postsuspend(void)
  99. {
  100. }
  101. /*
  102. * Check what brought us out of the suspend.
  103. * Return: 0 to sleep, otherwise wake
  104. */
  105. static int corgi_should_wakeup(unsigned int resume_on_alarm)
  106. {
  107. int is_resume = 0;
  108. dev_dbg(sharpsl_pm.dev, "GPLR0 = %x,%x\n", GPLR0, PEDR);
  109. if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) {
  110. if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
  111. /* charge on */
  112. dev_dbg(sharpsl_pm.dev, "ac insert\n");
  113. sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
  114. } else {
  115. /* charge off */
  116. dev_dbg(sharpsl_pm.dev, "ac remove\n");
  117. sharpsl_pm_led(SHARPSL_LED_OFF);
  118. sharpsl_pm.machinfo->charge(0);
  119. sharpsl_pm.charge_mode = CHRG_OFF;
  120. }
  121. }
  122. if ((PEDR & GPIO_bit(CORGI_GPIO_CHRG_FULL)))
  123. dev_dbg(sharpsl_pm.dev, "Charge full interrupt\n");
  124. if (PEDR & GPIO_bit(CORGI_GPIO_KEY_INT))
  125. is_resume |= GPIO_bit(CORGI_GPIO_KEY_INT);
  126. if (PEDR & GPIO_bit(CORGI_GPIO_WAKEUP))
  127. is_resume |= GPIO_bit(CORGI_GPIO_WAKEUP);
  128. if (resume_on_alarm && (PEDR & PWER_RTC))
  129. is_resume |= PWER_RTC;
  130. dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
  131. return is_resume;
  132. }
  133. static unsigned long corgi_charger_wakeup(void)
  134. {
  135. return ~GPLR0 & ( GPIO_bit(CORGI_GPIO_AC_IN) | GPIO_bit(CORGI_GPIO_KEY_INT) | GPIO_bit(CORGI_GPIO_WAKEUP) );
  136. }
  137. unsigned long corgipm_read_devdata(int type)
  138. {
  139. switch(type) {
  140. case SHARPSL_STATUS_ACIN:
  141. return ((GPLR(CORGI_GPIO_AC_IN) & GPIO_bit(CORGI_GPIO_AC_IN)) != 0);
  142. case SHARPSL_STATUS_LOCK:
  143. return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
  144. case SHARPSL_STATUS_CHRGFULL:
  145. return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
  146. case SHARPSL_STATUS_FATAL:
  147. return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
  148. case SHARPSL_ACIN_VOLT:
  149. return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
  150. case SHARPSL_BATT_TEMP:
  151. return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
  152. case SHARPSL_BATT_VOLT:
  153. default:
  154. return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
  155. }
  156. }
  157. static struct sharpsl_charger_machinfo corgi_pm_machinfo = {
  158. .init = corgi_charger_init,
  159. .exit = sharpsl_pm_pxa_remove,
  160. .gpio_batlock = CORGI_GPIO_BAT_COVER,
  161. .gpio_acin = CORGI_GPIO_AC_IN,
  162. .gpio_batfull = CORGI_GPIO_CHRG_FULL,
  163. .discharge = corgi_discharge,
  164. .charge = corgi_charge,
  165. .measure_temp = corgi_measure_temp,
  166. .presuspend = corgi_presuspend,
  167. .postsuspend = corgi_postsuspend,
  168. .read_devdata = corgipm_read_devdata,
  169. .charger_wakeup = corgi_charger_wakeup,
  170. .should_wakeup = corgi_should_wakeup,
  171. .bat_levels = 40,
  172. .bat_levels_noac = spitz_battery_levels_noac,
  173. .bat_levels_acin = spitz_battery_levels_acin,
  174. .status_high_acin = 188,
  175. .status_low_acin = 178,
  176. .status_high_noac = 185,
  177. .status_low_noac = 175,
  178. };
  179. static struct platform_device *corgipm_device;
  180. static int __devinit corgipm_init(void)
  181. {
  182. int ret;
  183. corgipm_device = platform_device_alloc("sharpsl-pm", -1);
  184. if (!corgipm_device)
  185. return -ENOMEM;
  186. corgipm_device->dev.platform_data = &corgi_pm_machinfo;
  187. ret = platform_device_add(corgipm_device);
  188. if (ret)
  189. platform_device_put(corgipm_device);
  190. return ret;
  191. }
  192. static void corgipm_exit(void)
  193. {
  194. platform_device_unregister(corgipm_device);
  195. }
  196. module_init(corgipm_init);
  197. module_exit(corgipm_exit);