corgi_pm.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/gpio.h>
  17. #include <linux/gpio-pxa.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/apm-emulation.h>
  21. #include <linux/io.h>
  22. #include <asm/irq.h>
  23. #include <asm/mach-types.h>
  24. #include <mach/hardware.h>
  25. #include <mach/corgi.h>
  26. #include <mach/pxa2xx-regs.h>
  27. #include "sharpsl_pm.h"
  28. #include "generic.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 struct gpio charger_gpios[] = {
  36. { CORGI_GPIO_ADC_TEMP_ON, GPIOF_OUT_INIT_LOW, "ADC Temp On" },
  37. { CORGI_GPIO_CHRG_ON, GPIOF_OUT_INIT_LOW, "Charger On" },
  38. { CORGI_GPIO_CHRG_UKN, GPIOF_OUT_INIT_LOW, "Charger Unknown" },
  39. { CORGI_GPIO_AC_IN, GPIOF_IN, "Charger Detection" },
  40. { CORGI_GPIO_KEY_INT, GPIOF_IN, "Key Interrupt" },
  41. { CORGI_GPIO_WAKEUP, GPIOF_IN, "System wakeup notification" },
  42. };
  43. static void corgi_charger_init(void)
  44. {
  45. gpio_request_array(ARRAY_AND_SIZE(charger_gpios));
  46. }
  47. static void corgi_measure_temp(int on)
  48. {
  49. gpio_set_value(CORGI_GPIO_ADC_TEMP_ON, on);
  50. }
  51. static void corgi_charge(int on)
  52. {
  53. if (on) {
  54. if (machine_is_corgi() && (sharpsl_pm.flags & SHARPSL_SUSPENDED)) {
  55. gpio_set_value(CORGI_GPIO_CHRG_ON, 0);
  56. gpio_set_value(CORGI_GPIO_CHRG_UKN, 1);
  57. } else {
  58. gpio_set_value(CORGI_GPIO_CHRG_ON, 1);
  59. gpio_set_value(CORGI_GPIO_CHRG_UKN, 0);
  60. }
  61. } else {
  62. gpio_set_value(CORGI_GPIO_CHRG_ON, 0);
  63. gpio_set_value(CORGI_GPIO_CHRG_UKN, 0);
  64. }
  65. }
  66. static void corgi_discharge(int on)
  67. {
  68. gpio_set_value(CORGI_GPIO_DISCHARGE_ON, on);
  69. }
  70. static void corgi_presuspend(void)
  71. {
  72. }
  73. static void corgi_postsuspend(void)
  74. {
  75. }
  76. /*
  77. * Check what brought us out of the suspend.
  78. * Return: 0 to sleep, otherwise wake
  79. */
  80. static int corgi_should_wakeup(unsigned int resume_on_alarm)
  81. {
  82. int is_resume = 0;
  83. dev_dbg(sharpsl_pm.dev, "PEDR = %x, GPIO_AC_IN = %d, "
  84. "GPIO_CHRG_FULL = %d, GPIO_KEY_INT = %d, GPIO_WAKEUP = %d\n",
  85. PEDR, gpio_get_value(CORGI_GPIO_AC_IN),
  86. gpio_get_value(CORGI_GPIO_CHRG_FULL),
  87. gpio_get_value(CORGI_GPIO_KEY_INT),
  88. gpio_get_value(CORGI_GPIO_WAKEUP));
  89. if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) {
  90. if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
  91. /* charge on */
  92. dev_dbg(sharpsl_pm.dev, "ac insert\n");
  93. sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
  94. } else {
  95. /* charge off */
  96. dev_dbg(sharpsl_pm.dev, "ac remove\n");
  97. sharpsl_pm_led(SHARPSL_LED_OFF);
  98. sharpsl_pm.machinfo->charge(0);
  99. sharpsl_pm.charge_mode = CHRG_OFF;
  100. }
  101. }
  102. if ((PEDR & GPIO_bit(CORGI_GPIO_CHRG_FULL)))
  103. dev_dbg(sharpsl_pm.dev, "Charge full interrupt\n");
  104. if (PEDR & GPIO_bit(CORGI_GPIO_KEY_INT))
  105. is_resume |= GPIO_bit(CORGI_GPIO_KEY_INT);
  106. if (PEDR & GPIO_bit(CORGI_GPIO_WAKEUP))
  107. is_resume |= GPIO_bit(CORGI_GPIO_WAKEUP);
  108. if (resume_on_alarm && (PEDR & PWER_RTC))
  109. is_resume |= PWER_RTC;
  110. dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
  111. return is_resume;
  112. }
  113. static bool corgi_charger_wakeup(void)
  114. {
  115. return !gpio_get_value(CORGI_GPIO_AC_IN) ||
  116. !gpio_get_value(CORGI_GPIO_KEY_INT) ||
  117. !gpio_get_value(CORGI_GPIO_WAKEUP);
  118. }
  119. unsigned long corgipm_read_devdata(int type)
  120. {
  121. switch(type) {
  122. case SHARPSL_STATUS_ACIN:
  123. return !gpio_get_value(CORGI_GPIO_AC_IN);
  124. case SHARPSL_STATUS_LOCK:
  125. return gpio_get_value(sharpsl_pm.machinfo->gpio_batlock);
  126. case SHARPSL_STATUS_CHRGFULL:
  127. return gpio_get_value(sharpsl_pm.machinfo->gpio_batfull);
  128. case SHARPSL_STATUS_FATAL:
  129. return gpio_get_value(sharpsl_pm.machinfo->gpio_fatal);
  130. case SHARPSL_ACIN_VOLT:
  131. return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
  132. case SHARPSL_BATT_TEMP:
  133. return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
  134. case SHARPSL_BATT_VOLT:
  135. default:
  136. return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
  137. }
  138. }
  139. static struct sharpsl_charger_machinfo corgi_pm_machinfo = {
  140. .init = corgi_charger_init,
  141. .exit = NULL,
  142. .gpio_batlock = CORGI_GPIO_BAT_COVER,
  143. .gpio_acin = CORGI_GPIO_AC_IN,
  144. .gpio_batfull = CORGI_GPIO_CHRG_FULL,
  145. .discharge = corgi_discharge,
  146. .charge = corgi_charge,
  147. .measure_temp = corgi_measure_temp,
  148. .presuspend = corgi_presuspend,
  149. .postsuspend = corgi_postsuspend,
  150. .read_devdata = corgipm_read_devdata,
  151. .charger_wakeup = corgi_charger_wakeup,
  152. .should_wakeup = corgi_should_wakeup,
  153. #if defined(CONFIG_LCD_CORGI)
  154. .backlight_limit = corgi_lcd_limit_intensity,
  155. #endif
  156. .charge_on_volt = SHARPSL_CHARGE_ON_VOLT,
  157. .charge_on_temp = SHARPSL_CHARGE_ON_TEMP,
  158. .charge_acin_high = SHARPSL_CHARGE_ON_ACIN_HIGH,
  159. .charge_acin_low = SHARPSL_CHARGE_ON_ACIN_LOW,
  160. .fatal_acin_volt = SHARPSL_FATAL_ACIN_VOLT,
  161. .fatal_noacin_volt= SHARPSL_FATAL_NOACIN_VOLT,
  162. .bat_levels = 40,
  163. .bat_levels_noac = sharpsl_battery_levels_noac,
  164. .bat_levels_acin = sharpsl_battery_levels_acin,
  165. .status_high_acin = 188,
  166. .status_low_acin = 178,
  167. .status_high_noac = 185,
  168. .status_low_noac = 175,
  169. };
  170. static struct platform_device *corgipm_device;
  171. static int corgipm_init(void)
  172. {
  173. int ret;
  174. if (!machine_is_corgi() && !machine_is_shepherd()
  175. && !machine_is_husky())
  176. return -ENODEV;
  177. corgipm_device = platform_device_alloc("sharpsl-pm", -1);
  178. if (!corgipm_device)
  179. return -ENOMEM;
  180. if (!machine_is_corgi())
  181. corgi_pm_machinfo.batfull_irq = 1;
  182. corgipm_device->dev.platform_data = &corgi_pm_machinfo;
  183. ret = platform_device_add(corgipm_device);
  184. if (ret)
  185. platform_device_put(corgipm_device);
  186. return ret;
  187. }
  188. static void corgipm_exit(void)
  189. {
  190. platform_device_unregister(corgipm_device);
  191. }
  192. module_init(corgipm_init);
  193. module_exit(corgipm_exit);