collie_pm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Based on spitz_pm.c and sharp code.
  3. *
  4. * Copyright (C) 2001 SHARP
  5. * Copyright 2005 Pavel Machek <pavel@suse.cz>
  6. *
  7. * Distribute under GPLv2.
  8. *
  9. * Li-ion batteries are angry beasts, and they like to explode. This driver is not finished,
  10. * and sometimes charges them when it should not. If it makes angry lithium to come your way...
  11. * ...well, you have been warned.
  12. *
  13. * Actually, this should be quite safe, it seems sharp leaves charger enabled by default,
  14. * and my collie did not explode (yet).
  15. */
  16. #include <linux/module.h>
  17. #include <linux/stat.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/delay.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/gpio.h>
  25. #include <asm/irq.h>
  26. #include <mach/hardware.h>
  27. #include <asm/hardware/scoop.h>
  28. #include <mach/dma.h>
  29. #include <mach/collie.h>
  30. #include <asm/mach/sharpsl_param.h>
  31. #include <asm/hardware/sharpsl_pm.h>
  32. #include "../drivers/mfd/ucb1x00.h"
  33. static struct ucb1x00 *ucb;
  34. static int ad_revise;
  35. #define ADCtoPower(x) ((330 * x * 2) / 1024)
  36. static void collie_charger_init(void)
  37. {
  38. int err;
  39. if (sharpsl_param.adadj != -1)
  40. ad_revise = sharpsl_param.adadj;
  41. /* Register interrupt handler. */
  42. if ((err = request_irq(COLLIE_IRQ_GPIO_AC_IN, sharpsl_ac_isr, IRQF_DISABLED,
  43. "ACIN", sharpsl_ac_isr))) {
  44. printk("Could not get irq %d.\n", COLLIE_IRQ_GPIO_AC_IN);
  45. return;
  46. }
  47. if ((err = request_irq(COLLIE_IRQ_GPIO_CO, sharpsl_chrg_full_isr, IRQF_DISABLED,
  48. "CO", sharpsl_chrg_full_isr))) {
  49. free_irq(COLLIE_IRQ_GPIO_AC_IN, sharpsl_ac_isr);
  50. printk("Could not get irq %d.\n", COLLIE_IRQ_GPIO_CO);
  51. return;
  52. }
  53. gpio_request(COLLIE_GPIO_CHARGE_ON, "charge on");
  54. gpio_direction_output(COLLIE_GPIO_CHARGE_ON, 1);
  55. ucb1x00_io_set_dir(ucb, 0, COLLIE_TC35143_GPIO_MBAT_ON | COLLIE_TC35143_GPIO_TMP_ON |
  56. COLLIE_TC35143_GPIO_BBAT_ON);
  57. return;
  58. }
  59. static void collie_measure_temp(int on)
  60. {
  61. if (on)
  62. ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_TMP_ON, 0);
  63. else
  64. ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_TMP_ON);
  65. }
  66. static void collie_charge(int on)
  67. {
  68. /* Zaurus seems to contain LTC1731; it should know when to
  69. * stop charging itself, so setting charge on should be
  70. * relatively harmless (as long as it is not done too often).
  71. */
  72. gpio_set_value(COLLIE_GPIO_CHARGE_ON, on);
  73. }
  74. static void collie_discharge(int on)
  75. {
  76. }
  77. static void collie_discharge1(int on)
  78. {
  79. }
  80. static void collie_presuspend(void)
  81. {
  82. }
  83. static void collie_postsuspend(void)
  84. {
  85. }
  86. static int collie_should_wakeup(unsigned int resume_on_alarm)
  87. {
  88. return 0;
  89. }
  90. static unsigned long collie_charger_wakeup(void)
  91. {
  92. return 0;
  93. }
  94. int collie_read_backup_battery(void)
  95. {
  96. int voltage;
  97. ucb1x00_adc_enable(ucb);
  98. ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_BBAT_ON, 0);
  99. voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD1, UCB_SYNC);
  100. ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_BBAT_ON);
  101. ucb1x00_adc_disable(ucb);
  102. printk("Backup battery = %d(%d)\n", ADCtoPower(voltage), voltage);
  103. return ADCtoPower(voltage);
  104. }
  105. int collie_read_main_battery(void)
  106. {
  107. int voltage, voltage_rev, voltage_volts;
  108. ucb1x00_adc_enable(ucb);
  109. ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_BBAT_ON);
  110. ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_MBAT_ON, 0);
  111. mdelay(1);
  112. voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD1, UCB_SYNC);
  113. ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_MBAT_ON);
  114. ucb1x00_adc_disable(ucb);
  115. voltage_rev = voltage + ((ad_revise * voltage) / 652);
  116. voltage_volts = ADCtoPower(voltage_rev);
  117. printk("Main battery = %d(%d)\n", voltage_volts, voltage);
  118. if (voltage != -1)
  119. return voltage_volts;
  120. else
  121. return voltage;
  122. }
  123. int collie_read_temp(void)
  124. {
  125. int voltage;
  126. /* According to Sharp, temp must be > 973, main battery must be < 465,
  127. FIXME: sharpsl_pm.c has both conditions negated? FIXME: values
  128. are way out of range? */
  129. ucb1x00_adc_enable(ucb);
  130. ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_TMP_ON, 0);
  131. /* >1010 = battery removed, 460 = 22C ?, higher = lower temp ? */
  132. voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD0, UCB_SYNC);
  133. ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_TMP_ON);
  134. ucb1x00_adc_disable(ucb);
  135. printk("Battery temp = %d\n", voltage);
  136. return voltage;
  137. }
  138. static unsigned long read_devdata(int which)
  139. {
  140. switch (which) {
  141. case SHARPSL_BATT_VOLT:
  142. return collie_read_main_battery();
  143. case SHARPSL_BATT_TEMP:
  144. return collie_read_temp();
  145. case SHARPSL_ACIN_VOLT:
  146. return 500;
  147. case SHARPSL_STATUS_ACIN: {
  148. int ret = GPLR & COLLIE_GPIO_AC_IN;
  149. printk("AC status = %d\n", ret);
  150. return ret;
  151. }
  152. case SHARPSL_STATUS_FATAL: {
  153. int ret = GPLR & COLLIE_GPIO_MAIN_BAT_LOW;
  154. printk("Fatal bat = %d\n", ret);
  155. return ret;
  156. }
  157. default:
  158. return ~0;
  159. }
  160. }
  161. struct battery_thresh collie_battery_levels_acin[] = {
  162. { 420, 100},
  163. { 417, 95},
  164. { 415, 90},
  165. { 413, 80},
  166. { 411, 75},
  167. { 408, 70},
  168. { 406, 60},
  169. { 403, 50},
  170. { 398, 40},
  171. { 391, 25},
  172. { 10, 5},
  173. { 0, 0},
  174. };
  175. struct battery_thresh collie_battery_levels[] = {
  176. { 394, 100},
  177. { 390, 95},
  178. { 380, 90},
  179. { 370, 80},
  180. { 368, 75}, /* From sharp code: battery high with frontlight */
  181. { 366, 70}, /* 60..90 -- fake values invented by me for testing */
  182. { 364, 60},
  183. { 362, 50},
  184. { 360, 40},
  185. { 358, 25}, /* From sharp code: battery low with frontlight */
  186. { 356, 5}, /* From sharp code: battery verylow with frontlight */
  187. { 0, 0},
  188. };
  189. struct sharpsl_charger_machinfo collie_pm_machinfo = {
  190. .init = collie_charger_init,
  191. .read_devdata = read_devdata,
  192. .discharge = collie_discharge,
  193. .discharge1 = collie_discharge1,
  194. .charge = collie_charge,
  195. .measure_temp = collie_measure_temp,
  196. .presuspend = collie_presuspend,
  197. .postsuspend = collie_postsuspend,
  198. .charger_wakeup = collie_charger_wakeup,
  199. .should_wakeup = collie_should_wakeup,
  200. .bat_levels = 12,
  201. .bat_levels_noac = collie_battery_levels,
  202. .bat_levels_acin = collie_battery_levels_acin,
  203. .status_high_acin = 368,
  204. .status_low_acin = 358,
  205. .status_high_noac = 368,
  206. .status_low_noac = 358,
  207. .charge_on_volt = 350, /* spitz uses 2.90V, but lets play it safe. */
  208. .charge_on_temp = 550,
  209. .charge_acin_high = 550, /* collie does not seem to have sensor for this, anyway */
  210. .charge_acin_low = 450, /* ignored, too */
  211. .fatal_acin_volt = 356,
  212. .fatal_noacin_volt = 356,
  213. .batfull_irq = 1, /* We do not want periodical charge restarts */
  214. };
  215. static int __init collie_pm_ucb_add(struct ucb1x00_dev *pdev)
  216. {
  217. sharpsl_pm.machinfo = &collie_pm_machinfo;
  218. ucb = pdev->ucb;
  219. return 0;
  220. }
  221. static struct ucb1x00_driver collie_pm_ucb_driver = {
  222. .add = collie_pm_ucb_add,
  223. };
  224. static struct platform_device *collie_pm_device;
  225. static int __init collie_pm_init(void)
  226. {
  227. int ret;
  228. collie_pm_device = platform_device_alloc("sharpsl-pm", -1);
  229. if (!collie_pm_device)
  230. return -ENOMEM;
  231. collie_pm_device->dev.platform_data = &collie_pm_machinfo;
  232. ret = platform_device_add(collie_pm_device);
  233. if (ret)
  234. platform_device_put(collie_pm_device);
  235. if (!ret)
  236. ret = ucb1x00_register_driver(&collie_pm_ucb_driver);
  237. return ret;
  238. }
  239. static void __exit collie_pm_exit(void)
  240. {
  241. ucb1x00_unregister_driver(&collie_pm_ucb_driver);
  242. platform_device_unregister(collie_pm_device);
  243. }
  244. module_init(collie_pm_init);
  245. module_exit(collie_pm_exit);