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