spitz_pm.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <linux/apm-emulation.h>
  19. #include <asm/irq.h>
  20. #include <asm/mach-types.h>
  21. #include <mach/hardware.h>
  22. #include <mach/sharpsl.h>
  23. #include <mach/spitz.h>
  24. #include <mach/pxa2xx-regs.h>
  25. #include <mach/pxa2xx-gpio.h>
  26. #include "sharpsl.h"
  27. #define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */
  28. #define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */
  29. #define SHARPSL_CHARGE_ON_ACIN_HIGH 0x9b /* 6V */
  30. #define SHARPSL_CHARGE_ON_ACIN_LOW 0x34 /* 2V */
  31. #define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */
  32. #define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */
  33. static int spitz_last_ac_status;
  34. static void spitz_charger_init(void)
  35. {
  36. pxa_gpio_mode(SPITZ_GPIO_KEY_INT | GPIO_IN);
  37. pxa_gpio_mode(SPITZ_GPIO_SYNC | GPIO_IN);
  38. }
  39. static void spitz_measure_temp(int on)
  40. {
  41. gpio_set_value(SPITZ_GPIO_ADC_TEMP_ON, on);
  42. }
  43. static void spitz_charge(int on)
  44. {
  45. if (on) {
  46. if (sharpsl_pm.flags & SHARPSL_SUSPENDED) {
  47. gpio_set_value(SPITZ_GPIO_JK_B, 1);
  48. gpio_set_value(SPITZ_GPIO_CHRG_ON, 0);
  49. } else {
  50. gpio_set_value(SPITZ_GPIO_JK_B, 0);
  51. gpio_set_value(SPITZ_GPIO_CHRG_ON, 0);
  52. }
  53. } else {
  54. gpio_set_value(SPITZ_GPIO_JK_B, 0);
  55. gpio_set_value(SPITZ_GPIO_CHRG_ON, 1);
  56. }
  57. }
  58. static void spitz_discharge(int on)
  59. {
  60. gpio_set_value(SPITZ_GPIO_JK_A, on);
  61. }
  62. /* HACK - For unknown reasons, accurate voltage readings are only made with a load
  63. on the power bus which the green led on spitz provides */
  64. static void spitz_discharge1(int on)
  65. {
  66. gpio_set_value(SPITZ_GPIO_LED_GREEN, on);
  67. }
  68. static void spitz_presuspend(void)
  69. {
  70. spitz_last_ac_status = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
  71. /* GPIO Sleep Register */
  72. PGSR0 = 0x00144018;
  73. PGSR1 = 0x00EF0000;
  74. if (machine_is_akita()) {
  75. PGSR2 = 0x2121C000;
  76. PGSR3 = 0x00600400;
  77. } else {
  78. PGSR2 = 0x0121C000;
  79. PGSR3 = 0x00600000;
  80. }
  81. PGSR0 &= ~SPITZ_GPIO_G0_STROBE_BIT;
  82. PGSR1 &= ~SPITZ_GPIO_G1_STROBE_BIT;
  83. PGSR2 &= ~SPITZ_GPIO_G2_STROBE_BIT;
  84. PGSR3 &= ~SPITZ_GPIO_G3_STROBE_BIT;
  85. PGSR2 |= GPIO_bit(SPITZ_GPIO_KEY_STROBE0);
  86. pxa_gpio_mode(GPIO18_RDY|GPIO_OUT | GPIO_DFLT_HIGH);
  87. PRER = GPIO_bit(SPITZ_GPIO_KEY_INT);
  88. PFER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
  89. PWER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET) | PWER_RTC;
  90. PKWR = GPIO_bit(SPITZ_GPIO_SYNC) | GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
  91. PKSR = 0xffffffff; // clear
  92. /* nRESET_OUT Disable */
  93. PSLR |= PSLR_SL_ROD;
  94. /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */
  95. PCFR = PCFR_GPR_EN | PCFR_OPDE;
  96. }
  97. static void spitz_postsuspend(void)
  98. {
  99. pxa_gpio_mode(GPIO18_RDY_MD);
  100. pxa_gpio_mode(10 | GPIO_IN);
  101. }
  102. static int spitz_should_wakeup(unsigned int resume_on_alarm)
  103. {
  104. int is_resume = 0;
  105. int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
  106. if (spitz_last_ac_status != acin) {
  107. if (acin) {
  108. /* charge on */
  109. sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
  110. dev_dbg(sharpsl_pm.dev, "AC Inserted\n");
  111. } else {
  112. /* charge off */
  113. dev_dbg(sharpsl_pm.dev, "AC Removed\n");
  114. sharpsl_pm_led(SHARPSL_LED_OFF);
  115. sharpsl_pm.machinfo->charge(0);
  116. sharpsl_pm.charge_mode = CHRG_OFF;
  117. }
  118. spitz_last_ac_status = acin;
  119. /* Return to suspend as this must be what we were woken for */
  120. return 0;
  121. }
  122. if (PEDR & GPIO_bit(SPITZ_GPIO_KEY_INT))
  123. is_resume |= GPIO_bit(SPITZ_GPIO_KEY_INT);
  124. if (PKSR & GPIO_bit(SPITZ_GPIO_SYNC))
  125. is_resume |= GPIO_bit(SPITZ_GPIO_SYNC);
  126. if (resume_on_alarm && (PEDR & PWER_RTC))
  127. is_resume |= PWER_RTC;
  128. dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
  129. return is_resume;
  130. }
  131. static unsigned long spitz_charger_wakeup(void)
  132. {
  133. return (~GPLR0 & GPIO_bit(SPITZ_GPIO_KEY_INT)) | (GPLR0 & GPIO_bit(SPITZ_GPIO_SYNC));
  134. }
  135. unsigned long spitzpm_read_devdata(int type)
  136. {
  137. switch(type) {
  138. case SHARPSL_STATUS_ACIN:
  139. return (((~GPLR(SPITZ_GPIO_AC_IN)) & GPIO_bit(SPITZ_GPIO_AC_IN)) != 0);
  140. case SHARPSL_STATUS_LOCK:
  141. return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
  142. case SHARPSL_STATUS_CHRGFULL:
  143. return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
  144. case SHARPSL_STATUS_FATAL:
  145. return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
  146. case SHARPSL_ACIN_VOLT:
  147. return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
  148. case SHARPSL_BATT_TEMP:
  149. return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
  150. case SHARPSL_BATT_VOLT:
  151. default:
  152. return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
  153. }
  154. }
  155. struct sharpsl_charger_machinfo spitz_pm_machinfo = {
  156. .init = spitz_charger_init,
  157. .exit = NULL,
  158. .gpio_batlock = SPITZ_GPIO_BAT_COVER,
  159. .gpio_acin = SPITZ_GPIO_AC_IN,
  160. .gpio_batfull = SPITZ_GPIO_CHRG_FULL,
  161. .batfull_irq = 1,
  162. .gpio_fatal = SPITZ_GPIO_FATAL_BAT,
  163. .discharge = spitz_discharge,
  164. .discharge1 = spitz_discharge1,
  165. .charge = spitz_charge,
  166. .measure_temp = spitz_measure_temp,
  167. .presuspend = spitz_presuspend,
  168. .postsuspend = spitz_postsuspend,
  169. .read_devdata = spitzpm_read_devdata,
  170. .charger_wakeup = spitz_charger_wakeup,
  171. .should_wakeup = spitz_should_wakeup,
  172. #if defined(CONFIG_LCD_CORGI)
  173. .backlight_limit = corgi_lcd_limit_intensity,
  174. #elif defined(CONFIG_BACKLIGHT_CORGI)
  175. .backlight_limit = corgibl_limit_intensity,
  176. #endif
  177. .charge_on_volt = SHARPSL_CHARGE_ON_VOLT,
  178. .charge_on_temp = SHARPSL_CHARGE_ON_TEMP,
  179. .charge_acin_high = SHARPSL_CHARGE_ON_ACIN_HIGH,
  180. .charge_acin_low = SHARPSL_CHARGE_ON_ACIN_LOW,
  181. .fatal_acin_volt = SHARPSL_FATAL_ACIN_VOLT,
  182. .fatal_noacin_volt= SHARPSL_FATAL_NOACIN_VOLT,
  183. .bat_levels = 40,
  184. .bat_levels_noac = spitz_battery_levels_noac,
  185. .bat_levels_acin = spitz_battery_levels_acin,
  186. .status_high_acin = 188,
  187. .status_low_acin = 178,
  188. .status_high_noac = 185,
  189. .status_low_noac = 175,
  190. };
  191. static struct platform_device *spitzpm_device;
  192. static int __devinit spitzpm_init(void)
  193. {
  194. int ret;
  195. if (!machine_is_spitz() && !machine_is_akita()
  196. && !machine_is_borzoi())
  197. return -ENODEV;
  198. spitzpm_device = platform_device_alloc("sharpsl-pm", -1);
  199. if (!spitzpm_device)
  200. return -ENOMEM;
  201. spitzpm_device->dev.platform_data = &spitz_pm_machinfo;
  202. ret = platform_device_add(spitzpm_device);
  203. if (ret)
  204. platform_device_put(spitzpm_device);
  205. return ret;
  206. }
  207. static void spitzpm_exit(void)
  208. {
  209. platform_device_unregister(spitzpm_device);
  210. }
  211. module_init(spitzpm_init);
  212. module_exit(spitzpm_exit);