corgi_pm.c 7.0 KB

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