drv_dsi_touch.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-11-26 bigmagic first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <drivers/touch.h>
  13. #include <ioremap.h>
  14. #include <mmu.h>
  15. #ifdef RT_USING_SMART
  16. #include <lwp.h>
  17. #include <lwp_user_mm.h>
  18. #endif
  19. #include <hypercall.h>
  20. #ifdef BSP_USING_TOUCH
  21. #include "mbox.h"
  22. #include "drv_dsi_touch.h"
  23. #define DBG_TAG "dsi_touch"
  24. #define DBG_LVL DBG_INFO
  25. #include <rtdbg.h>
  26. static rt_touch_t touch_device = RT_NULL;
  27. static struct rt_semaphore dsi_touch_ack;
  28. static rt_uint32_t touch_x;
  29. static rt_uint32_t touch_y;
  30. static rt_uint32_t touch_state = 0;
  31. static rt_thread_t dsi_touch_tid = RT_NULL;
  32. #define DSI_TOUCH_THREAD_STACK_SIZE (4096)
  33. #define DSI_TOUCH_THREAD_PRIORITY (25)
  34. #define DSI_TOUCH_THREAD_TIMESLICE (10)
  35. #define MAXIMUM_SUPPORTED_POINTS (10)
  36. struct touch_regs
  37. {
  38. uint8_t device_mode;
  39. uint8_t gesture_id;
  40. uint8_t num_points;
  41. struct touch
  42. {
  43. uint8_t xh;
  44. uint8_t xl;
  45. uint8_t yh;
  46. uint8_t yl;
  47. uint8_t res1;
  48. uint8_t res2;
  49. } point[MAXIMUM_SUPPORTED_POINTS];
  50. };
  51. struct drv_mouse_device
  52. {
  53. struct rt_device parent;
  54. struct rt_touch_data touchdata;
  55. int channel;
  56. };
  57. struct drv_mouse_device _mouse;
  58. static void post_event(rt_uint16_t x, rt_uint16_t y, rt_uint8_t event)
  59. {
  60. struct rt_touch_data *minfo = &_mouse.touchdata;
  61. struct rt_channel_msg ch_msg;
  62. LOG_D("event:%d, x:%d, y:%d\n", event, x, y);
  63. minfo->x_coordinate = x;
  64. minfo->y_coordinate = y;
  65. minfo->event = event;
  66. ch_msg.type = RT_CHANNEL_RAW;
  67. ch_msg.u.d = (void *)(size_t)event;
  68. rt_channel_send(_mouse.channel, &ch_msg);
  69. }
  70. static void dsi_touch_thread_entry(void *param)
  71. {
  72. static volatile unsigned long touchbuf;
  73. touchbuf = bcm271x_mbox_get_touch();
  74. if (touchbuf == RT_NULL)
  75. {
  76. rt_kprintf("init dsi touch err!\n");
  77. return;
  78. }
  79. #ifdef BSP_USING_VM_MODE
  80. if (rt_hv_stage2_map((unsigned long)touchbuf, 0x1000))
  81. {
  82. rt_kprintf("alloc mmio from hyper fail!\n");
  83. return;
  84. }
  85. #endif
  86. touchbuf = (unsigned long)rt_ioremap((void *)touchbuf, 0x1000);
  87. while (1)
  88. {
  89. struct touch_regs *regs = (struct touch_regs *)touchbuf;
  90. if ((regs->num_points > 0) && (regs->num_points < MAXIMUM_SUPPORTED_POINTS))
  91. {
  92. /* only one touch point */
  93. touch_x = (((int)regs->point[0].xh & 0xf) << 8) + regs->point[0].xl;
  94. touch_y = (((int)regs->point[0].yh & 0xf) << 8) + regs->point[0].yl;
  95. if (!touch_state)
  96. {
  97. post_event(touch_x, touch_y, RT_TOUCH_EVENT_DOWN);
  98. }
  99. else
  100. {
  101. post_event(touch_x, touch_y, RT_TOUCH_EVENT_MOVE);
  102. }
  103. touch_state = 1;
  104. }
  105. else
  106. {
  107. if (touch_state)
  108. {
  109. post_event(touch_x, touch_y, RT_TOUCH_EVENT_UP);
  110. }
  111. touch_state = 0;
  112. }
  113. rt_thread_mdelay(1);
  114. }
  115. }
  116. static rt_size_t dsi_read_point(struct rt_touch_device *touch, void *buf, rt_size_t read_num)
  117. {
  118. rt_uint16_t* touchxy = (rt_uint16_t *)buf;
  119. if ((read_num != 0) && (touch_state == 1))
  120. {
  121. touchxy[0] = touch_x;
  122. touchxy[1] = touch_y;
  123. touch_state = 0;
  124. return read_num;
  125. }
  126. else
  127. {
  128. return 0;
  129. }
  130. }
  131. static rt_err_t dsi_control(struct rt_touch_device *device, int cmd, void *data)
  132. {
  133. return RT_EOK;
  134. }
  135. static struct rt_touch_ops dsi_touch_ops =
  136. {
  137. .touch_readpoint = dsi_read_point,
  138. .touch_control = dsi_control,
  139. };
  140. static rt_err_t drv_mouse_init(struct rt_device *device)
  141. {
  142. struct drv_mouse_device *mouse = (struct drv_mouse_device*)device;
  143. return RT_EOK;
  144. }
  145. static rt_err_t drv_mouse_control(struct rt_device *device, int cmd, void *args)
  146. {
  147. switch (cmd)
  148. {
  149. #define CMD_MOUSE_SET_NOTIFY 0 /* arg is shmid, in the shm, a sem point is given */
  150. case CMD_MOUSE_SET_NOTIFY:
  151. *(unsigned long *)args = (rt_uint32_t)(unsigned long)lwp_map_user_phy(lwp_self(), RT_NULL, (uint32_t*)((size_t)&_mouse.touchdata + PV_OFFSET), sizeof(struct rt_touch_data), 1);
  152. break;
  153. default:
  154. break;
  155. }
  156. return RT_EOK;
  157. }
  158. #ifdef RT_USING_DEVICE_OPS
  159. const static struct rt_device_ops _mouse_ops =
  160. {
  161. drv_mouse_init,
  162. RT_NULL,
  163. RT_NULL,
  164. RT_NULL,
  165. RT_NULL,
  166. drv_mouse_control
  167. };
  168. #endif
  169. static int hw_dsi_touch_init(void)
  170. {
  171. struct rt_device *device = &_mouse.parent;
  172. #ifdef RT_USING_DEVICE_OPS
  173. device->ops = &_mouse_ops;
  174. #else
  175. device->init = drv_mouse_init;
  176. device->open = RT_NULL;
  177. device->close = RT_NULL;
  178. device->read = RT_NULL;
  179. device->write = RT_NULL;
  180. device->control = drv_mouse_control;
  181. #endif
  182. rt_device_register(device, "mouse", RT_DEVICE_FLAG_RDWR);
  183. _mouse.channel = rt_channel_open("mouse", O_CREAT);
  184. /* touch sem */
  185. rt_sem_init(&dsi_touch_ack, "dsi_touch_ack", 0, RT_IPC_FLAG_FIFO);
  186. dsi_touch_tid = rt_thread_create("dsi_touch",
  187. dsi_touch_thread_entry, RT_NULL,
  188. DSI_TOUCH_THREAD_STACK_SIZE,
  189. DSI_TOUCH_THREAD_PRIORITY, DSI_TOUCH_THREAD_TIMESLICE);
  190. if (dsi_touch_tid != RT_NULL)
  191. {
  192. rt_thread_startup(dsi_touch_tid);
  193. }
  194. touch_device = (rt_touch_t)rt_malloc(sizeof(struct rt_touch_device));
  195. if (touch_device == RT_NULL)
  196. {
  197. return -RT_ERROR;
  198. }
  199. /* register touch device */
  200. touch_device->info.type = RT_TOUCH_TYPE_RESISTANCE;
  201. touch_device->info.vendor = RT_TOUCH_VENDOR_UNKNOWN;
  202. touch_device->ops = &dsi_touch_ops;
  203. rt_hw_touch_register(touch_device, "dsi_touch", RT_DEVICE_FLAG_INT_RX, RT_NULL);
  204. return 0;
  205. }
  206. INIT_APP_EXPORT(hw_dsi_touch_init);
  207. #endif /* BSP_USING_TOUCH */