collie_pm.c 7.3 KB

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