spitz_pm.c 7.0 KB

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