sspd_rts.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*****************************************************************************
  2. *
  3. * Copyright Andes Technology Corporation 2007-2008
  4. * All Rights Reserved.
  5. *
  6. * Revision History:
  7. *
  8. * Mar.16.2008 Created.
  9. ****************************************************************************/
  10. #include "hal.h"
  11. #include "sspd_rts.h"
  12. #include "bsp_hal.h"
  13. #define RTS_PRESSED_Z1_MIN 0x10
  14. #define RTS_PRESSED_Z2_MAX 0xfe0
  15. typedef struct _SSPD_RTS_CONTEXT{
  16. hal_bh_t hisr;
  17. hal_semaphore_t *usr_ievent;
  18. struct ts_data *usr_idata;
  19. } SSPD_RTS_CONTEXT;
  20. SSPD_RTS_CONTEXT rts_ctxt;
  21. #define SSPD_HISR_STACK_SIZE 1024
  22. static uint32_t sspd_rts_hisr_stack[SSPD_HISR_STACK_SIZE];
  23. static void _sspd_rts_lisr(int vector){
  24. int x = 0, y = 0, z1 = 0, z2 = 0, p = 0;
  25. DEBUG(0, 1, "Enter\n");
  26. if (vector != RTS_LISR_VECTOR)
  27. hal_system_error(HAL_ERR_UNHANDLED_INTERRUPT);
  28. /* Disable #PENIRQ interrupt */
  29. uint32_t prv_msk = hal_intc_irq_mask(RTS_LISR_VECTOR);
  30. /* Clear #PENIRQ interrupt status */
  31. hal_intc_irq_clean(RTS_LISR_VECTOR);
  32. /* Enable higher priority interrupt */
  33. GIE_ENABLE();
  34. //FIXME
  35. #if 0
  36. #if(NO_EXTERNAL_INT_CTL==0)
  37. /* Disable #PENIRQ interrupt temporarily */
  38. CLRB32(INTC_HW1_ER, RTS_LISR_VECTOR);
  39. /* Clear #PENIRQ interrupt status */
  40. SETB32(INTC_HW1_CLR, RTS_LISR_VECTOR);
  41. #else
  42. /* FIXME add clear ts interrupt */
  43. #endif
  44. #endif
  45. _sspd_rts_probe(&x, &y, &z1, &z2, &p);
  46. _sspd_rts_probe(&x, &y, &z1, &z2, &p);
  47. _sspd_rts_probe(&x, &y, &z1, &z2, &p);
  48. rts_ctxt.usr_idata->x = x;
  49. rts_ctxt.usr_idata->y = y;
  50. rts_ctxt.usr_idata->z1 = z1;
  51. rts_ctxt.usr_idata->z2 = z2;
  52. DEBUG(0, 1, "%4d, %4d, %4d, %4d\n", x, y, z1, z2);
  53. if (z1 < 100) {
  54. /* Disable GIE to prevent nested self */
  55. GIE_DISABLE();
  56. /*
  57. * Clear #PENIRQ interrupt status again because _sspd_rts_probe would trigger #PENIRQ interrupt
  58. * Please reference ADS7846 Spec.
  59. */
  60. hal_intc_irq_clean(RTS_LISR_VECTOR);
  61. /* Re-enable touch interrupt */
  62. hal_intc_irq_unmask(prv_msk);
  63. return;
  64. }
  65. hal_raise_bh(&rts_ctxt.hisr);
  66. // TODO
  67. // It is a walk around since interrupt priority
  68. // we should change the hisr
  69. hal_intc_irq_unmask(prv_msk);
  70. hal_intc_irq_disable(RTS_LISR_VECTOR);
  71. }
  72. static inline void ts_hisr(void *param){
  73. hal_bh_t *bh = param;
  74. while (1){
  75. DEBUG(0, 1, "before\n");
  76. hal_pend_semaphore(&bh->sem, HAL_SUSPEND);
  77. DEBUG(0, 1, "after\n");
  78. hal_post_semaphore(rts_ctxt.usr_ievent);
  79. #ifndef CONFIG_PLAT_QEMU
  80. hal_sleep(300);
  81. #endif
  82. hal_intc_irq_clean(RTS_LISR_VECTOR);
  83. HAL_INTC_IRQ_ATOMIC_ENABLE(RTS_LISR_VECTOR);
  84. // SETB32(INTC_HW1_ER, RTS_LISR_VECTOR);
  85. // SETB32(INTC_HW1_CLR, RTS_LISR_VECTOR);
  86. }
  87. }
  88. static void _sspd_rts_set_sclk(int sclk){
  89. int sclk_div; /* serial clock rate divisor */
  90. if (sclk > RTS_ADS7846_DCLK_MAX){
  91. DEBUG(1, 1, "Warning : SCLK exceed allowable range! Truncation is performed.\n");
  92. sclk = RTS_ADS7846_DCLK_MAX;
  93. }
  94. /*
  95. * sclk source:
  96. * PLL3 (SSPCLK = PLL3/6) AG101 internal clk
  97. * GPIO25 (SSPCLK = GPIO25) AG101 external clk
  98. * OSCCLK (SSPCLK = OSCCLK * 6 / 6) Leopard
  99. *
  100. * calculate sclk_div from internal PLL3
  101. *
  102. * sclk_div = (SSPCLK / sclk / 2) - 1
  103. * = ((PLL3 / 6) / sclk / 2) - 1
  104. * = ((OSCCLK * 30 / 6) / sclk / 2) - 1
  105. */
  106. /*
  107. * setup PMU SSP clock source
  108. *
  109. * PMU_SSPCLKSEL: MFPSR[6]
  110. * 0: SSPCLK
  111. * 1: GPIO25
  112. *
  113. * PMU_AC97CLKOUTSEL: MFPSR[13]
  114. * 0: GPIO
  115. * 1: AC97CLK out
  116. */
  117. /* [2008-03-20] SSP1 only works with internal clock source on AG101 and Leopard EVBs. */
  118. #if 0 /*(MB_SSP_EXT_CLK) */
  119. sclk_div = (24768000 / (sclk << 1)) - 1;
  120. SSPD_TRACE(("\nSSPCLK: GPIO25\n"));
  121. SETB32(PMU_MFPSR, PMU_SSPCLKSEL_BIT);
  122. #else
  123. sclk_div = (MB_OSCCLK / (2 * sclk)) - 1;
  124. DEBUG(1, 1, "SSPCLK: PLL\n");
  125. CLRB32(PMU_MFPSR, PMU_SSPCLKSEL_BIT);
  126. #endif
  127. DEBUG(1, 1, "sclk : %d, sclk_div : 0x%04x\n", sclk, sclk_div);
  128. /*
  129. * setup SSP SCLKDIV
  130. * PDL : (padding data length) not used
  131. * SDL : (serial data length) 8-1 (8 bits)
  132. * SCLKDIV : sclk_div
  133. */
  134. #ifndef CONFIG_PLAT_QEMU
  135. OUT32(SSPC_CR1, ((7 << SSPC_C1_SDL_SHIFT) & SSPC_C1_SDL_MASK) | /* bit data length */
  136. ((sclk_div << SSPC_C1_SCLKDIV_SHIFT) & SSPC_C1_SCLKDIV_MASK)); /* sclkdiv */
  137. #else
  138. OUT32(SSPC_CR1, ((23 << SSPC_C1_SDL_SHIFT) & SSPC_C1_SDL_MASK) | /* bit data length */
  139. ((sclk_div << SSPC_C1_SCLKDIV_SHIFT) & SSPC_C1_SCLKDIV_MASK)); /* sclkdiv */
  140. #endif
  141. }
  142. int _sspd_rts_init(struct ts_dev *ts){
  143. int status = HAL_SUCCESS;
  144. int core_intl;
  145. core_intl = hal_global_int_ctl(HAL_DISABLE_INTERRUPTS);
  146. /* SSP controler initialization - SPI */
  147. /* Disable all SSP interrupts, and set DMA trigger FIFO threshold to 0. */
  148. OUT32(SSPC_INTCR, 0);
  149. /* check ts interrupt vector*/
  150. if (ts->penirq){
  151. status = hal_register_isr(RTS_LISR_VECTOR, _sspd_rts_lisr, (void*)0);
  152. if (status != HAL_SUCCESS){
  153. DEBUG(1, 1, "Failed to register SSPD driver LISR!\n");
  154. return status;
  155. }
  156. rts_ctxt.usr_ievent = ts->event_obj;
  157. rts_ctxt.usr_idata = ts->event_data;
  158. rts_ctxt.hisr.th.fn = ts_hisr;
  159. rts_ctxt.hisr.th.ptos = &sspd_rts_hisr_stack[SSPD_HISR_STACK_SIZE];
  160. rts_ctxt.hisr.th.stack_size = sizeof(sspd_rts_hisr_stack);
  161. rts_ctxt.hisr.th.prio = CONFIG_TSD_HISR_PRIORITY;
  162. rts_ctxt.hisr.th.name = "TS BH";
  163. rts_ctxt.hisr.th.arg = &rts_ctxt.hisr;
  164. status = hal_create_bh(&rts_ctxt.hisr);
  165. if (status != HAL_SUCCESS){
  166. DEBUG(1, 1, "Failed to create SSPD-RTS driver HISR!\n");
  167. return status;
  168. }
  169. /* INTC */
  170. /* - Disable #PENIRQ interrupt */
  171. hal_intc_irq_disable(RTS_LISR_VECTOR);
  172. /* - Clear #PENIRQ interrupt status */
  173. hal_intc_irq_clean(RTS_LISR_VECTOR);
  174. /* - Setup #PENIRQ interrupt trigger mode - edge trigger */
  175. /* - Setup #PENIRQ interrupt trigger level - assert low */
  176. hal_intc_irq_config(RTS_LISR_VECTOR, IRQ_EDGE_TRIGGER, IRQ_ACTIVE_LOW);
  177. if (ts->penirq_en){
  178. /* - Enable #PENIRQ interrupt */
  179. hal_intc_irq_enable(RTS_LISR_VECTOR);
  180. }
  181. }
  182. /* Reset SSP controller */
  183. SETB32(SSPC_CR2, SSPC_C2_SSPRST_BIT);
  184. /* Disable SSP data out */
  185. CLRR32(SSPC_CR2, SSPC_C2_SSPEN_MASK | SSPC_C2_TXDOE_MASK);
  186. /* setup sspc clock */
  187. _sspd_rts_set_sclk(RTS_ADS7846_DCLK_DEFAULT);
  188. _nds_kwait(RTS_RESET_WAIT);
  189. /* Set SSP frame format to SPI */
  190. OUT32(SSPC_CR0, ((0 << SSPC_C0_SCLKPH_BIT) & SSPC_C0_SCLKPH_MASK) | /* phase (1 not working for 16clk mode) */
  191. ((0 << SSPC_C0_SCLKPO_BIT) & SSPC_C0_SCLKPO_MASK) | /* polarity */
  192. ((SSPC_SSP_MASTER << SSPC_C0_OPM_SHIFT) & SSPC_C0_OPM_MASK) | /* operation mode */
  193. ((0 << SSPC_C0_LBM_BIT) & SSPC_C0_LBM_MASK) | /* loopback */
  194. ((SSPC_MOTO_SPI << SSPC_C0_FFMT_SHIFT) & SSPC_C0_FFMT_MASK)); /* frame format */
  195. /* Clear FIFO garbage */
  196. SETR32(SSPC_CR2, SSPC_C2_RXFCLR_MASK | SSPC_C2_TXFCLR_MASK);
  197. /* Restore CPU interrupt controller to previous level */
  198. hal_global_int_ctl(core_intl);
  199. return status;
  200. }
  201. int _sspd_rts_probe(int *x, int *y, int *z1, int *z2, int *pressed){
  202. uint32_t data[12];
  203. int t, i;
  204. /* Clear FIFO garbage */
  205. SETR32(SSPC_CR2, SSPC_C2_RXFCLR_MASK | SSPC_C2_TXFCLR_MASK);
  206. /* Enable SSP */
  207. SETB32(SSPC_CR2, SSPC_C2_SSPEN_BIT);
  208. /* Disable SSP data out temporarily */
  209. CLRB32(SSPC_CR2, SSPC_C2_TXDOE_BIT);
  210. /* [hw-limit] Wait until the ssp controller get ready */
  211. // _nds_kwait(RTS_DIN_WAIT);
  212. t = 0;
  213. while (((IN32(SSPC_SR) & SSPC_SR_BUSY_MASK) != 0) && (t++ < RTS_DIN_TIMEOUT))
  214. ;
  215. DEBUG(0, 1, "[RTS] SR : 0x%08lx\n", IN32(SSPC_SR));
  216. /*
  217. * ------------------------------------------------------------------------
  218. * Timing of 16-clock-cycle per conversion
  219. *
  220. * power-up read y read x power down (full cycle)
  221. * --------- --------- --------- ---------------
  222. * dout (bytes) ctrl 0 ctrl 0 ctrl 0 ctrl 0 0 ____
  223. * din (bytes) 0 msb lsb msb lsb msb lsb msb lsb ____
  224. * ^^^^ ^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^
  225. * don't care x1 or y1 x2 or y2 don't care
  226. *
  227. * x = (x1 + x2) / 2 (if averaging was expected)
  228. * y = (y1 + y2) / 2 (if averaging was expected)
  229. *
  230. * Note: Watch out SSP FIFO depth (12 for AG101/Leopard)
  231. * ------------------------------------------------------------------------
  232. */
  233. /* SPI dout ... */
  234. #ifndef CONFIG_PLAT_QEMU
  235. /* power up */
  236. OUT32(SSPC_DR, RTS_ADS7846_CTL_RY);
  237. OUT32(SSPC_DR, 0);
  238. /* read y */
  239. OUT32(SSPC_DR, RTS_ADS7846_CTL_RY);
  240. OUT32(SSPC_DR, 0);
  241. /* read x */
  242. OUT32(SSPC_DR, RTS_ADS7846_CTL_RX);
  243. OUT32(SSPC_DR, 0);
  244. /* read z1 */
  245. OUT32(SSPC_DR, RTS_ADS7846_CTL_RZ1);
  246. OUT32(SSPC_DR, 0);
  247. /* read z2 && power down */
  248. OUT32(SSPC_DR, RTS_ADS7846_CTL_RZ2_PD);
  249. OUT32(SSPC_DR, 0);
  250. OUT32(SSPC_DR, 0);
  251. #else
  252. /* power up */
  253. OUT32(SSPC_DR, RTS_ADS7846_CTL_RY << 16);
  254. OUT32(SSPC_DR, 0);
  255. /* read y */
  256. OUT32(SSPC_DR, RTS_ADS7846_CTL_RY << 16);
  257. OUT32(SSPC_DR, 0);
  258. /* read x */
  259. OUT32(SSPC_DR, RTS_ADS7846_CTL_RX << 16);
  260. OUT32(SSPC_DR, 0);
  261. /* read z1 */
  262. OUT32(SSPC_DR, RTS_ADS7846_CTL_RZ1 << 16);
  263. OUT32(SSPC_DR, 0);
  264. /* read z2 && power down */
  265. OUT32(SSPC_DR, RTS_ADS7846_CTL_RZ2_PD << 16);
  266. OUT32(SSPC_DR, 0);
  267. OUT32(SSPC_DR, 0);
  268. #endif
  269. /* Enable SSP-TX out */
  270. SETB32(SSPC_CR2, SSPC_C2_TXDOE_BIT);
  271. /* SPI din ... */
  272. for (i = 0; i < 11; ++i){
  273. /* Wait until data ready */
  274. t = 0;
  275. while ((IN32(SSPC_SR) & SSPC_SR_RFVE_MASK) == 0){
  276. if ( ++t > RTS_DIN_TIMEOUT){
  277. DEBUG(1, 1, "rts spi timeout at data[%d]\n", i);
  278. goto _timeout;
  279. }
  280. }
  281. /* Read data byte */
  282. data[i] = IN32(SSPC_DR);
  283. DEBUG(0, 1, "[RTS] data[%d] %d, 0x%x\n", i, data[i], data[i]);
  284. }
  285. /* Disable SSP data out */
  286. CLRR32(SSPC_CR2, SSPC_C2_SSPEN_MASK | SSPC_C2_TXDOE_MASK);
  287. /* Compose final data (12-bits or 8-bits) */
  288. #if ((RTS_ADS7846_RY & RTS_ADS7846_MODE_MASK) == (RTS_ADS7846_8_BITS << RTS_ADS7846_MODE_SHIFT))
  289. *y = RTS_ADS7846_8BITS_DATA(data[3], data[4]);
  290. *x = RTS_ADS7846_8BITS_DATA(data[5], data[6]);
  291. *z1 = RTS_ADS7846_8BITS_DATA(data[7], data[8]);
  292. *z2 = RTS_ADS7846_8BITS_DATA(data[9], data[10]);
  293. #else
  294. *y = RTS_ADS7846_12BITS_DATA(data[3], data[4]);
  295. *x = RTS_ADS7846_12BITS_DATA(data[5], data[6]);
  296. *z1 = RTS_ADS7846_12BITS_DATA(data[7], data[8]);
  297. *z2 = RTS_ADS7846_12BITS_DATA(data[9], data[10]);
  298. #endif
  299. #ifndef CONFIG_PLAT_QEMU
  300. DEBUG(0, 1, "[RTS] y - %04d, msb(0x%02lx) lsb(0x%02lx)\n", *y, (data[3] & 0xff), (data[4] & 0xff));
  301. DEBUG(0, 1, "[RTS] x - %04d, msb(0x%02lx) lsb(0x%02lx)\n", *x, (data[5] & 0xff), (data[6] & 0xff));
  302. DEBUG(0, 1, "[RTS] z1 - %04d, msb(0x%02lx) lsb(0x%02lx)\n", *z1, (data[7] & 0xff), (data[8] & 0xff));
  303. DEBUG(0, 1, "[RTS] z2 - %04d, msb(0x%02lx) lsb(0x%02lx)\n", *z2, (data[9] & 0xff), (data[10] & 0xff));
  304. #else
  305. DEBUG(0, 1, "[RTS] y - %d, msb(0x%x)%d, lsb(0x%x)%d\n", *y, data[3],data[3], data[4], data[4]);
  306. DEBUG(0, 1, "[RTS] x - %d, msb(0x%x)%d, lsb(0x%x)%d\n", *x, data[5],data[5], data[6], data[6]);
  307. DEBUG(0, 1, "[RTS] z1 - %d, msb(0x%x)%d, lsb(0x%x)%d\n", *z1, data[7],data[7], data[8], data[8]);
  308. DEBUG(0, 1, "[RTS] z2 - %d, msb(0x%x)%d, lsb(0x%x)%d\n", *z2, data[9],data[9], data[10], data[10]);
  309. #endif
  310. if ((*z1 < RTS_PRESSED_Z1_MIN) && (*z2 >= RTS_PRESSED_Z2_MAX))
  311. *pressed = 0;
  312. else
  313. *pressed = 1;
  314. return HAL_SUCCESS;
  315. _timeout:
  316. return HAL_FAILURE;
  317. }
  318. void ts_adjust(struct ts_dev *ts, int ts_x, int ts_y, int *x, int *y)
  319. {
  320. *x = (ts->lcd_width * (ts_x - ts->left)) / (ts->right - ts->left);
  321. *y = (ts->lcd_height * (ts_y - ts->top)) / (ts->bottom - ts->top);
  322. DEBUG(0, 0, "adj (x, y) = (%4d, %4d)\n", *x, *y);
  323. }
  324. void ts_raw_value(struct ts_dev *ts, int *x, int *y)
  325. {
  326. hal_pend_semaphore(&ts->sem, HAL_SUSPEND);
  327. *x = ts->data.x;
  328. *y = ts->data.y;
  329. DEBUG(0, 0, "raw (x, y) = (%4d, %4d)\n", *x, *y);
  330. }
  331. void ts_value(struct ts_dev *ts, int *x, int *y)
  332. {
  333. int raw_x, raw_y;
  334. ts_raw_value(ts, &raw_x, &raw_y);
  335. ts_adjust(ts, raw_x, raw_y, x, y);
  336. }
  337. void ts_calibrate(struct ts_dev *ts,
  338. void (*draw_cross)(void *param, int x, int y),
  339. int count)
  340. {
  341. int i = 0;
  342. int left = 0, right = 0, top = 0, bottom = 0;
  343. for (i = 0; i < count; i++) {
  344. int raw_x = 0, raw_y = 0;
  345. DEBUG(0, 0, "(left, top) = ");
  346. draw_cross(NULL, ts->lcd_width * 1 / 5, ts->lcd_height * 1 / 5);
  347. ts_raw_value(ts, &raw_x, &raw_y);
  348. left = ((left * i) + raw_x) / (i + 1);
  349. top = ((top * i) + raw_y) / (i + 1);
  350. DEBUG(0, 0, "(%4d, %4d) || (x, y) = (%4d, %4d)\n", left, top, raw_x, raw_y);
  351. DEBUG(0, 0, "(right, bottom) = ");
  352. draw_cross(NULL, ts->lcd_width * 4 / 5, ts->lcd_height * 4 / 5);
  353. ts_raw_value(ts, &raw_x, &raw_y);
  354. right = ((right * i) + raw_x) / (i + 1);
  355. bottom = ((bottom * i) + raw_y) / (i + 1);
  356. DEBUG(0, 0, "(%4d, %4d) || (x, y) = (%4d, %4d)\n", right, bottom, raw_x, raw_y);
  357. }
  358. ts->left = left - (right - left) / 3;
  359. ts->right = right + (right - left) / 3;
  360. ts->top = top - (bottom - top) / 3;
  361. ts->bottom = bottom + (bottom - top) / 3;
  362. }