spitz_pm.c 6.9 KB

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