collie_pm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #include <linux/module.h>
  14. #include <linux/stat.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/delay.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <asm/irq.h>
  22. #include <asm/mach-types.h>
  23. #include <asm/hardware.h>
  24. #include <asm/hardware/scoop.h>
  25. #include <asm/dma.h>
  26. #include <asm/arch/collie.h>
  27. #include <asm/mach/sharpsl_param.h>
  28. #include <asm/hardware/sharpsl_pm.h>
  29. #include "../drivers/mfd/ucb1x00.h"
  30. static struct ucb1x00 *ucb;
  31. static int ad_revise;
  32. #define ADCtoPower(x) ((330 * x * 2) / 1024)
  33. static void collie_charger_init(void)
  34. {
  35. int err;
  36. if (sharpsl_param.adadj != -1) {
  37. ad_revise = sharpsl_param.adadj;
  38. }
  39. /* Register interrupt handler. */
  40. if ((err = request_irq(COLLIE_IRQ_GPIO_AC_IN, sharpsl_ac_isr, SA_INTERRUPT,
  41. "ACIN", sharpsl_ac_isr))) {
  42. printk("Could not get irq %d.\n", COLLIE_IRQ_GPIO_AC_IN);
  43. return;
  44. }
  45. if ((err = request_irq(COLLIE_IRQ_GPIO_CO, sharpsl_chrg_full_isr, SA_INTERRUPT,
  46. "CO", sharpsl_chrg_full_isr))) {
  47. free_irq(COLLIE_IRQ_GPIO_AC_IN, sharpsl_ac_isr);
  48. printk("Could not get irq %d.\n", COLLIE_IRQ_GPIO_CO);
  49. return;
  50. }
  51. ucb1x00_io_set_dir(ucb, 0, COLLIE_TC35143_GPIO_MBAT_ON | COLLIE_TC35143_GPIO_TMP_ON |
  52. COLLIE_TC35143_GPIO_BBAT_ON);
  53. return;
  54. }
  55. static void collie_measure_temp(int on)
  56. {
  57. if (on)
  58. ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_TMP_ON, 0);
  59. else
  60. ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_TMP_ON);
  61. }
  62. static void collie_charge(int on)
  63. {
  64. if (on) {
  65. printk("Should start charger\n");
  66. } else {
  67. printk("Should stop charger\n");
  68. }
  69. #ifdef I_AM_SURE
  70. /* Zaurus seems to contain LTC1731 ; it should know when to
  71. * stop charging itself, so setting charge on should be
  72. * relatively harmless (as long as it is not done too often).
  73. */
  74. #define CF_BUF_CTRL_BASE 0xF0800000
  75. #define SCOOP_REG(adr) (*(volatile unsigned short*)(CF_BUF_CTRL_BASE+(adr)))
  76. #define SCOOP_REG_GPWR SCOOP_REG(SCOOP_GPWR)
  77. if (on) {
  78. set_scoop_gpio(&colliescoop_device.dev, COLLIE_SCP_CHARGE_ON);
  79. } else {
  80. reset_scoop_gpio(&colliescoop_device.dev, COLLIE_SCP_CHARGE_ON);
  81. }
  82. #endif
  83. }
  84. static void collie_discharge(int on)
  85. {
  86. }
  87. static void collie_discharge1(int on)
  88. {
  89. }
  90. static void collie_presuspend(void)
  91. {
  92. }
  93. static void collie_postsuspend(void)
  94. {
  95. }
  96. static int collie_should_wakeup(unsigned int resume_on_alarm)
  97. {
  98. return 0;
  99. }
  100. static unsigned long collie_charger_wakeup(void)
  101. {
  102. return 0;
  103. }
  104. int collie_read_backup_battery(void)
  105. {
  106. int voltage;
  107. ucb1x00_adc_enable(ucb);
  108. /* Gives 75..130 */
  109. ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_BBAT_ON, 0);
  110. voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD1, UCB_SYNC);
  111. ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_BBAT_ON);
  112. ucb1x00_adc_disable(ucb);
  113. printk("Backup battery = %d(%d)\n", ADCtoPower(voltage), voltage);
  114. return ADCtoPower(voltage);
  115. }
  116. int collie_read_main_battery(void)
  117. {
  118. int voltage, voltage_rev, voltage_volts;
  119. ucb1x00_adc_enable(ucb);
  120. ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_BBAT_ON);
  121. ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_MBAT_ON, 0);
  122. /* gives values 160..255 with battery removed... and
  123. 145..255 with battery inserted. (on AC), goes as low as
  124. 80 on DC. */
  125. voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD1, UCB_SYNC);
  126. ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_MBAT_ON);
  127. ucb1x00_adc_disable(ucb);
  128. voltage_rev = voltage + ((ad_revise * voltage) / 652);
  129. voltage_volts = ADCtoPower(voltage_rev);
  130. printk("Main battery = %d(%d)\n", voltage_volts, voltage);
  131. if (voltage != -1)
  132. return voltage_volts;
  133. else
  134. return voltage;
  135. }
  136. int collie_read_temp(void)
  137. {
  138. int voltage;
  139. /* According to Sharp, temp must be > 973, main battery must be < 465,
  140. FIXME: sharpsl_pm.c has both conditions negated? FIXME: values
  141. are way out of range? */
  142. ucb1x00_adc_enable(ucb);
  143. ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_TMP_ON, 0);
  144. /* >1010 = battery removed, 460 = 22C ?, higer = lower temp ? */
  145. voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD0, UCB_SYNC);
  146. ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_TMP_ON);
  147. ucb1x00_adc_disable(ucb);
  148. printk("Battery temp = %d\n", voltage);
  149. return voltage;
  150. }
  151. static unsigned long read_devdata(int which)
  152. {
  153. switch (which) {
  154. case SHARPSL_BATT_VOLT:
  155. return collie_read_main_battery();
  156. case SHARPSL_BATT_TEMP:
  157. return collie_read_temp();
  158. case SHARPSL_ACIN_VOLT:
  159. return 0x1;
  160. case SHARPSL_STATUS_ACIN: {
  161. int ret = GPLR & COLLIE_GPIO_AC_IN;
  162. printk("AC status = %d\n", ret);
  163. return ret;
  164. }
  165. case SHARPSL_STATUS_FATAL: {
  166. int ret = GPLR & COLLIE_GPIO_MAIN_BAT_LOW;
  167. printk("Fatal bat = %d\n", ret);
  168. return ret;
  169. }
  170. default:
  171. return ~0;
  172. }
  173. }
  174. struct battery_thresh collie_battery_levels[] = {
  175. { 368, 100},
  176. { 358, 25},
  177. { 356, 5},
  178. { 0, 0},
  179. };
  180. struct sharpsl_charger_machinfo collie_pm_machinfo = {
  181. .init = collie_charger_init,
  182. .read_devdata = read_devdata,
  183. .discharge = collie_discharge,
  184. .discharge1 = collie_discharge1,
  185. .charge = collie_charge,
  186. .measure_temp = collie_measure_temp,
  187. .presuspend = collie_presuspend,
  188. .postsuspend = collie_postsuspend,
  189. .charger_wakeup = collie_charger_wakeup,
  190. .should_wakeup = collie_should_wakeup,
  191. .bat_levels = 3,
  192. .bat_levels_noac = collie_battery_levels,
  193. .bat_levels_acin = collie_battery_levels,
  194. .status_high_acin = 368,
  195. .status_low_acin = 358,
  196. .status_high_noac = 368,
  197. .status_low_noac = 358,
  198. };
  199. static int __init collie_pm_ucb_add(struct ucb1x00_dev *pdev)
  200. {
  201. sharpsl_pm.machinfo = &collie_pm_machinfo;
  202. ucb = pdev->ucb;
  203. return 0;
  204. }
  205. static struct ucb1x00_driver collie_pm_ucb_driver = {
  206. .add = collie_pm_ucb_add,
  207. };
  208. static struct platform_device *collie_pm_device;
  209. static int __init collie_pm_init(void)
  210. {
  211. int ret;
  212. collie_pm_device = platform_device_alloc("sharpsl-pm", -1);
  213. if (!collie_pm_device)
  214. return -ENOMEM;
  215. collie_pm_device->dev.platform_data = &collie_pm_machinfo;
  216. ret = platform_device_add(collie_pm_device);
  217. if (ret)
  218. platform_device_put(collie_pm_device);
  219. if (!ret)
  220. ret = ucb1x00_register_driver(&collie_pm_ucb_driver);
  221. return ret;
  222. }
  223. static void __exit collie_pm_exit(void)
  224. {
  225. ucb1x00_unregister_driver(&collie_pm_ucb_driver);
  226. platform_device_unregister(collie_pm_device);
  227. }
  228. module_init(collie_pm_init);
  229. module_exit(collie_pm_exit);