collie_pm.c 7.3 KB

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