quad_decoder_acts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Copyright (c) 2017 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief TP Keyboard driver for Actions SoC
  9. */
  10. #include <errno.h>
  11. #include <kernel.h>
  12. #include <string.h>
  13. #include <init.h>
  14. #include <irq.h>
  15. #include <drivers/adc.h>
  16. #include <drivers/input/input_dev.h>
  17. #include <sys/util.h>
  18. #include <sys/byteorder.h>
  19. #include <board.h>
  20. #include <soc_pmu.h>
  21. #include <logging/log.h>
  22. #include <device.h>
  23. #include <drivers/gpio.h>
  24. #include <soc.h>
  25. #include <string.h>
  26. LOG_MODULE_REGISTER(quad_decoder, CONFIG_SYS_LOG_INPUT_DEV_LEVEL);
  27. #define DT_DRV_COMPAT actions_qd
  28. /* qd en reg */
  29. #define QD_EN_ZRDIE (0x1 << 8)
  30. #define QD_EN_ZFDIE (0x1 << 7)
  31. #define QD_EN_YRDIE (0x1 << 6)
  32. #define QD_EN_YFDIE (0x1 << 5)
  33. #define QD_EN_XRDIE (0x1 << 4)
  34. #define QD_EN_XFDIE (0x1 << 3)
  35. #define QD_EN_QDZE (0x1 << 2)
  36. #define QD_EN_QDYE (0x1 << 1)
  37. #define QD_EN_QDXE (0x1 << 0)
  38. /* qd ctl reg */
  39. #define QD_CTL_DT(X) (X << 9)
  40. #define QD_CTL_DT_MASK QD_CTL_DT(0X7)
  41. #define QD_CTL_ZCM(X) (X << 7)
  42. #define QD_CTL_YCM(X) (X << 5)
  43. #define QD_CTL_XCM(X) (X << 3)
  44. #define QD_CTL_ZCDC (1 << 2)
  45. #define QD_CTL_YCDC (1 << 1)
  46. #define QD_CTL_XCDC (1 << 0)
  47. /* qd state reg */
  48. #define QD_STATE_ZRDIP (1 << 8)
  49. #define QD_STATE_ZFDIP (1 << 7)
  50. #define QD_STATE_YRDIP (1 << 6)
  51. #define QD_STATE_YFDIP (1 << 5)
  52. #define QD_STATE_XRDIP (1 << 4)
  53. #define QD_STATE_XFDIP (1 << 3)
  54. #define QD_STATE_CLEAR_PENDING (QD_STATE_ZRDIP | QD_STATE_ZFDIP | QD_STATE_YRDIP | QD_STATE_YFDIP | QD_STATE_XRDIP | QD_STATE_XFDIP)
  55. #define QD_STATE_ZTER (1 << 2)
  56. #define QD_STATE_YTER (1 << 1)
  57. #define QD_STATE_XTER (1 << 0)
  58. struct quad_decoder_acts_controller {
  59. volatile uint32_t qd_en;
  60. volatile uint32_t qd_ctl;
  61. volatile uint32_t qd_state;
  62. volatile uint32_t x_forward_counter;
  63. volatile uint32_t x_reverse_counter;
  64. volatile uint32_t y_forward_counter;
  65. volatile uint32_t y_reverse_counter;
  66. volatile uint32_t z_forward_counter;
  67. volatile uint32_t z_reverse_counter;
  68. };
  69. struct acts_quad_decoder_data {
  70. input_notify_t notify;
  71. struct k_delayed_work timer;
  72. const struct device *this_dev;
  73. uint32_t timestamp;
  74. uint16_t prev_x_forward;
  75. uint16_t prev_x_reserve;
  76. uint16_t prev_y_forward;
  77. uint16_t prev_y_reserve;
  78. uint16_t prev_z_forward;
  79. uint16_t prev_z_reserve;
  80. };
  81. struct acts_quad_decoder_config {
  82. struct mxkeypad_acts_controller *base;
  83. void (*irq_config_func)(void);
  84. uint8_t clock_id;
  85. uint8_t reset_id;
  86. uint8_t pinmux_size;
  87. uint32_t poll_total_ms;
  88. uint16_t poll_interval_ms;
  89. const struct acts_pin_config *pinmux;
  90. };
  91. static struct acts_quad_decoder_data quad_decoder_acts_ddata;
  92. static const struct acts_pin_config pins_quad_decoder[] = {FOREACH_PIN_MFP(0)};
  93. static void quad_decoder_acts_irq_config(void);
  94. static void quad_decoder_acts_disable(struct device *dev);
  95. static const struct acts_quad_decoder_config quad_decoder_acts_cdata = {
  96. .base = (struct quad_decoder_acts_controller *)DT_INST_REG_ADDR(0),
  97. .irq_config_func = quad_decoder_acts_irq_config,
  98. .pinmux = pins_quad_decoder,
  99. .pinmux_size = ARRAY_SIZE(pins_quad_decoder),
  100. .clock_id = DT_INST_PROP(0, clkid),
  101. .reset_id = DT_INST_PROP(0, rstid),
  102. .poll_interval_ms = DT_INST_PROP(0, poll_interval_ms),
  103. .poll_total_ms = DT_INST_PROP(0, poll_total_ms),
  104. };
  105. static void quad_decoder_acts_irq_disable(struct quad_decoder_acts_controller *qd)
  106. {
  107. if(DT_INST_PROP(0, x_direct))
  108. qd->qd_en = qd->qd_en&(~(QD_EN_XRDIE | QD_EN_XFDIE));
  109. if(DT_INST_PROP(0, y_direct))
  110. qd->qd_en = qd->qd_en&(~(QD_EN_YRDIE | QD_EN_YFDIE));
  111. if(DT_INST_PROP(0, z_direct))
  112. qd->qd_en = qd->qd_en&(~(QD_EN_ZRDIE | QD_EN_ZFDIE));
  113. return;
  114. }
  115. static void quad_decoder_acts_irq_enable(struct quad_decoder_acts_controller *qd)
  116. {
  117. if(DT_INST_PROP(0, x_direct))
  118. qd->qd_en |= QD_EN_XRDIE | QD_EN_XFDIE;
  119. if(DT_INST_PROP(0, y_direct))
  120. qd->qd_en |= QD_EN_YRDIE | QD_EN_YFDIE;
  121. if(DT_INST_PROP(0, z_direct))
  122. qd->qd_en |= QD_EN_ZRDIE | QD_EN_ZFDIE;
  123. return;
  124. }
  125. static void quad_decoder_acts_isr(void *arg)
  126. {
  127. struct device *dev = (struct device *)arg;
  128. struct acts_quad_decoder_data *data = dev->data;
  129. const struct acts_quad_decoder_config *cfg = dev->config;
  130. struct quad_decoder_acts_controller *qd = cfg->base;
  131. quad_decoder_acts_irq_enable(qd);
  132. qd->qd_state |= QD_STATE_CLEAR_PENDING;
  133. data->timestamp = k_uptime_get_32();
  134. k_delayed_work_submit(&data->timer, K_NO_WAIT);//K_MSEC(cfg->poll_interval_ms));
  135. }
  136. static void quad_decoder_acts_report_key(struct acts_quad_decoder_data *data,
  137. int key_code, int value)
  138. {
  139. struct input_value val;
  140. if (data->notify) {
  141. val.keypad.type = EV_KEY;
  142. val.keypad.code = key_code;
  143. val.keypad.value = value;
  144. LOG_DBG("report key_code %d value %d",
  145. key_code, value);
  146. data->notify(NULL, &val);
  147. }
  148. }
  149. static void quad_decoder_acts_set_clk(const struct acts_quad_decoder_config *cfg, uint32_t freq_hz)
  150. {
  151. #if 1
  152. clk_set_rate(cfg->clock_id, freq_hz);
  153. k_busy_wait(100);
  154. #endif
  155. }
  156. static void quad_decoder_acts_poll(struct k_work *work)
  157. {
  158. struct acts_quad_decoder_data *data = CONTAINER_OF(work, struct acts_quad_decoder_data, timer);
  159. const struct device *dev = data->this_dev;
  160. const struct acts_quad_decoder_config *cfg = dev->config;
  161. struct quad_decoder_acts_controller *qd = cfg->base;
  162. uint8_t time_update;
  163. signed long int decoder_value;
  164. time_update = 0;
  165. if(data->prev_x_forward != qd->x_forward_counter) {//x_f
  166. decoder_value = qd->x_forward_counter - data->prev_x_forward;
  167. decoder_value = (decoder_value > 0) ? decoder_value : (decoder_value + 65535);
  168. quad_decoder_acts_report_key(data, 1, decoder_value);
  169. data->prev_x_forward = qd->x_forward_counter;
  170. time_update = 1;
  171. }
  172. if(data->prev_x_reserve != qd->x_reverse_counter) {//x_r
  173. decoder_value = qd->x_reverse_counter - data->prev_x_reserve;
  174. decoder_value = (decoder_value > 0) ? decoder_value : (decoder_value + 65535);
  175. quad_decoder_acts_report_key(data, 2, decoder_value);
  176. data->prev_x_reserve = qd->x_reverse_counter;
  177. time_update = 1;
  178. }
  179. if(data->prev_y_forward != qd->y_forward_counter) {//y_f
  180. decoder_value = qd->y_forward_counter - data->prev_y_forward;
  181. decoder_value = (decoder_value > 0) ? decoder_value : (decoder_value + 65535);
  182. quad_decoder_acts_report_key(data, 3, decoder_value);
  183. data->prev_y_forward = qd->y_forward_counter;
  184. time_update = 1;
  185. }
  186. if(data->prev_y_reserve != qd->y_reverse_counter) {//y_r
  187. decoder_value = qd->y_reverse_counter - data->prev_y_reserve;
  188. decoder_value = (decoder_value > 0) ? decoder_value : (decoder_value + 65535);
  189. quad_decoder_acts_report_key(data, 4, decoder_value);
  190. data->prev_y_reserve = qd->y_reverse_counter;
  191. time_update = 1;
  192. }
  193. if(data->prev_z_forward != qd->z_forward_counter) {//z_f
  194. decoder_value = qd->z_forward_counter - data->prev_z_forward;
  195. decoder_value = (decoder_value > 0) ? decoder_value : (decoder_value + 65535);
  196. quad_decoder_acts_report_key(data, 5, decoder_value);
  197. data->prev_z_forward = qd->z_forward_counter;
  198. time_update = 1;
  199. }
  200. if(data->prev_z_reserve != qd->z_reverse_counter) {//z_r
  201. decoder_value = qd->z_reverse_counter - data->prev_z_reserve;
  202. decoder_value = (decoder_value > 0) ? decoder_value : (decoder_value + 65535);
  203. quad_decoder_acts_report_key(data, 6, decoder_value);
  204. data->prev_z_reserve = qd->z_reverse_counter;
  205. time_update = 1;
  206. }
  207. if(time_update)
  208. data->timestamp = k_uptime_get_32();
  209. out:
  210. if ((k_uptime_get_32() - data->timestamp) > cfg->poll_total_ms) {
  211. quad_decoder_acts_irq_enable(qd);
  212. } else {
  213. k_delayed_work_submit(&data->timer, K_MSEC(cfg->poll_interval_ms));
  214. }
  215. }
  216. static void quad_decoder_acts_enable(struct device *dev)
  217. {
  218. const struct acts_quad_decoder_config *cfg = dev->config;
  219. struct quad_decoder_acts_controller *qd = cfg->base;
  220. LOG_DBG("enable quad_decoder");
  221. if(DT_INST_PROP(0, x_direct)) {
  222. qd->qd_en |= (qd->qd_en & (~QD_EN_QDXE)) | QD_EN_QDXE;
  223. qd->qd_en |= QD_EN_XRDIE | QD_EN_XFDIE;
  224. qd->x_forward_counter = 0;
  225. qd->x_reverse_counter = 0;
  226. }
  227. if(DT_INST_PROP(0, y_direct)) {
  228. qd->qd_en |= (qd->qd_en & (~QD_EN_QDYE)) | QD_EN_QDYE;
  229. qd->qd_en |= QD_EN_YRDIE | QD_EN_YFDIE;
  230. qd->y_forward_counter = 0;
  231. qd->y_reverse_counter = 0;
  232. }
  233. if(DT_INST_PROP(0, z_direct)) {
  234. qd->qd_en |= (qd->qd_en & (~QD_EN_QDZE)) | QD_EN_QDZE;
  235. qd->qd_en |= QD_EN_ZRDIE | QD_EN_ZFDIE;
  236. qd->z_forward_counter = 0;
  237. qd->z_reverse_counter = 0;
  238. }
  239. qd->qd_ctl = (qd->qd_ctl & (~ QD_CTL_DT_MASK)) | QD_CTL_DT(2);//DT3
  240. }
  241. static void quad_decoder_acts_disable(struct device *dev)
  242. {
  243. const struct acts_quad_decoder_config *cfg = dev->config;
  244. struct quad_decoder_acts_controller *qd = cfg->base;
  245. LOG_DBG("disable quad_decoder");
  246. qd->qd_en = 0;
  247. qd->qd_ctl = (qd->qd_ctl & (~ QD_CTL_DT_MASK)) | QD_CTL_DT(0);//DT1
  248. }
  249. static void quad_decoder_acts_inquiry(struct device *dev, struct input_value *val)
  250. {
  251. LOG_DBG("inquiry quad_decoder");
  252. }
  253. static void quad_decoder_acts_register_notify(struct device *dev, input_notify_t notify)
  254. {
  255. LOG_DBG("register notify 0x%x", (uint32_t)notify);
  256. struct acts_quad_decoder_data *quad_decoder = dev->data;
  257. quad_decoder->notify = notify;
  258. }
  259. static void quad_decoder_acts_unregister_notify(struct device *dev, input_notify_t notify)
  260. {
  261. LOG_DBG("unregister notify 0x%x", (uint32_t)notify);
  262. struct acts_quad_decoder_data *quad_decoder = dev->data;
  263. quad_decoder->notify = NULL;
  264. }
  265. const struct input_dev_driver_api quad_decoder_acts_driver_api = {
  266. .enable = quad_decoder_acts_enable,
  267. .disable = quad_decoder_acts_disable,
  268. .inquiry = quad_decoder_acts_inquiry,
  269. .register_notify = quad_decoder_acts_register_notify,
  270. .unregister_notify = quad_decoder_acts_unregister_notify,
  271. };
  272. static void notify(struct device *dev, struct input_value *val)
  273. {
  274. printk("the key change: key[%d]:%d\n", val->keypad.code, val->keypad.value);
  275. }
  276. static void qd_test(const struct device *dev)
  277. {
  278. //sys_write32(0x18, 0x40068200);
  279. //*((REG32)(DEBUGOE)) = 0x3ff4f0;
  280. //sys_write32(0x30480, 0x40068208);
  281. quad_decoder_acts_register_notify(dev, notify);
  282. quad_decoder_acts_enable(dev);
  283. while(1);
  284. }
  285. static int quad_decoder_acts_init(const struct device *dev)
  286. {
  287. struct acts_quad_decoder_data *data = dev->data;
  288. const struct acts_quad_decoder_config *cfg = dev->config;
  289. const struct acts_pin_config *pinconf = cfg->pinmux;
  290. struct quad_decoder_acts_controller *qd = cfg->base;
  291. acts_pinmux_setup_pins(cfg->pinmux, cfg->pinmux_size);
  292. quad_decoder_acts_set_clk(cfg, 4000);
  293. /* enable QD controller clock */
  294. acts_clock_peripheral_enable(cfg->clock_id);
  295. /* reset QD controller */
  296. acts_reset_peripheral(cfg->reset_id);
  297. data->this_dev = dev;
  298. qd->qd_en = 0;
  299. data->prev_x_forward = qd->x_forward_counter;
  300. data->prev_x_reserve = qd->x_reverse_counter;
  301. data->prev_y_forward = qd->y_forward_counter;
  302. data->prev_y_reserve = qd->y_reverse_counter;
  303. data->prev_z_forward = qd->z_forward_counter;
  304. data->prev_z_reserve = qd->z_reverse_counter;
  305. qd->qd_ctl = QD_CTL_DT(0);//debounce config
  306. if(DT_INST_PROP(0, x_direct)) {
  307. qd->qd_ctl |= QD_CTL_XCDC;//init x direct
  308. qd->qd_ctl |= QD_CTL_XCM(2);// (0:0x1 mode, 1:0x2 mode, 2 : 0x4 mode)
  309. }
  310. if(DT_INST_PROP(0, y_direct)) {
  311. qd->qd_ctl |= QD_CTL_YCDC;//init y direct
  312. qd->qd_ctl |= QD_CTL_YCM(0);// (0:0x1 mode, 1:0x2 mode, 2 : 0x4 mode)
  313. }
  314. if(DT_INST_PROP(0, z_direct)) {
  315. qd->qd_ctl |= QD_CTL_ZCDC;//init z direct
  316. qd->qd_ctl |= QD_CTL_ZCM(0);// (0:0x1 mode, 1:0x2 mode, 2 : 0x4 mode)
  317. }
  318. cfg->irq_config_func();
  319. k_delayed_work_init(&data->timer, quad_decoder_acts_poll);
  320. // qd_test(dev);
  321. return 0;
  322. }
  323. #if DT_NODE_HAS_STATUS(DT_DRV_INST(0), okay)
  324. DEVICE_DEFINE(quad_decoder, DT_INST_LABEL(0),
  325. quad_decoder_acts_init, NULL,
  326. &quad_decoder_acts_ddata, &quad_decoder_acts_cdata,
  327. POST_KERNEL, 60,
  328. &quad_decoder_acts_driver_api);
  329. static void quad_decoder_acts_irq_config(void)
  330. {
  331. IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority),
  332. quad_decoder_acts_isr,
  333. DEVICE_GET(quad_decoder), 0);
  334. irq_enable(DT_INST_IRQN(0));
  335. }
  336. #endif // DT_NODE_HAS_STATUS