gpio_acts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Copyright (c) 2018 Justin Watson
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <errno.h>
  7. #include <kernel.h>
  8. #include <device.h>
  9. #include <init.h>
  10. #include <soc.h>
  11. #include <drivers/gpio.h>
  12. #include <board_cfg.h>
  13. #include "gpio_utils.h"
  14. #define GPIO_MAX_GRP GPIO_MAX_GROUPS
  15. #define WIO_DELAY_TIME (200)
  16. static struct device *g_gpio_dev[GPIO_MAX_GRP + 1];
  17. struct gpio_acts_config {
  18. /* gpio_driver_config needs to be first */
  19. struct gpio_driver_config common;
  20. uint32_t base;
  21. uint8_t grp;
  22. };
  23. struct gpio_acts_runtime {
  24. /* gpio_data needs to be first */
  25. struct gpio_driver_data common;
  26. sys_slist_t cb;
  27. };
  28. #define DEV_CFG(dev) \
  29. ((const struct gpio_acts_config * const)(dev)->config)
  30. #define DEV_DATA(dev) \
  31. ((struct gpio_acts_runtime * const)(dev)->data)
  32. static int gpio_acts_port_get_raw(const struct device *dev, uint32_t *value)
  33. {
  34. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  35. int pin_num = cfg->grp*32;
  36. unsigned int key;
  37. key = irq_lock();
  38. #if defined(CONFIG_WIO) && (CONFIG_WIO == 1)
  39. if(cfg->grp != 3){
  40. *value = sys_read32(GPIO_REG_IDAT(cfg->base, pin_num));
  41. }else{
  42. *value = 0;
  43. for(pin_num = 0; pin_num < 5; pin_num++){
  44. *value |= ((sys_read32(WIO_REG_CTL(pin_num)) >> 16) & 0x01) << pin_num;
  45. }
  46. }
  47. #else
  48. *value = sys_read32(GPIO_REG_IDAT(cfg->base, pin_num));
  49. #endif
  50. irq_unlock(key);
  51. return 0;
  52. }
  53. static int gpio_acts_port_set_masked_raw(const struct device *dev, uint32_t mask,
  54. uint32_t value)
  55. {
  56. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  57. int pin_num = cfg->grp*32;
  58. unsigned int val, key;
  59. key = irq_lock();
  60. #if defined(CONFIG_WIO) && (CONFIG_WIO == 1)
  61. if(cfg->grp != 3){
  62. val = sys_read32(GPIO_REG_ODAT(cfg->base, pin_num));
  63. val = (val&~mask) | (mask & value);
  64. sys_write32(val, GPIO_REG_ODAT(cfg->base, pin_num));
  65. }else{
  66. val = 0;
  67. for(pin_num = 0; pin_num < 5; pin_num++){
  68. val |= ((sys_read32(WIO_REG_CTL(pin_num)) >> 16) & 0x01) << pin_num;
  69. }
  70. val = (val&~mask) | (mask & value);
  71. for(pin_num = 0; pin_num < 5; pin_num++){
  72. if(val & (1 << pin_num)){
  73. sys_write32(sys_read32(WIO_REG_CTL(pin_num)) | (1 << 16), WIO_REG_CTL(pin_num));
  74. }else{
  75. sys_write32(sys_read32(WIO_REG_CTL(pin_num)) & (~(1 << 16)), WIO_REG_CTL(pin_num));
  76. }
  77. }
  78. }
  79. #else
  80. val = sys_read32(GPIO_REG_ODAT(cfg->base, pin_num));
  81. val = (val&~mask) | (mask & value);
  82. sys_write32(val, GPIO_REG_ODAT(cfg->base, pin_num));
  83. #endif
  84. irq_unlock(key);
  85. if(cfg->grp == 3){
  86. k_busy_wait(WIO_DELAY_TIME);
  87. }
  88. return 0;
  89. }
  90. static int gpio_acts_port_set_bits_raw(const struct device *dev, uint32_t mask)
  91. {
  92. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  93. int pin_num = cfg->grp*32;
  94. unsigned int key;
  95. key = irq_lock();
  96. #if defined(CONFIG_WIO) && (CONFIG_WIO == 1)
  97. if(cfg->grp != 3){
  98. sys_write32(mask, GPIO_REG_BSR(cfg->base, pin_num));
  99. }else{
  100. for(pin_num = 0; pin_num < 5; pin_num++){
  101. if(mask & (1 << pin_num)){
  102. sys_write32(sys_read32(WIO_REG_CTL(pin_num)) | (1 << 16), WIO_REG_CTL(pin_num));
  103. }
  104. }
  105. }
  106. #else
  107. sys_write32(mask, GPIO_REG_BSR(cfg->base, pin_num));
  108. #endif
  109. irq_unlock(key);
  110. if(cfg->grp == 3){
  111. k_busy_wait(WIO_DELAY_TIME);
  112. }
  113. return 0;
  114. }
  115. static int gpio_acts_port_clear_bits_raw(const struct device *dev, uint32_t mask)
  116. {
  117. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  118. int pin_num = cfg->grp*32;
  119. unsigned int key;
  120. key = irq_lock();
  121. #if defined(CONFIG_WIO) && (CONFIG_WIO == 1)
  122. if(cfg->grp != 3){
  123. sys_write32(mask, GPIO_REG_BRR(cfg->base, pin_num));
  124. }else{
  125. for(pin_num = 0; pin_num < 5; pin_num++){
  126. if(mask & (1 << pin_num)){
  127. sys_write32(sys_read32(WIO_REG_CTL(pin_num)) & (~(1 << 16)), WIO_REG_CTL(pin_num));
  128. }
  129. }
  130. }
  131. #else
  132. sys_write32(mask, GPIO_REG_BRR(cfg->base, pin_num));
  133. #endif
  134. irq_unlock(key);
  135. if(cfg->grp == 3){
  136. k_busy_wait(WIO_DELAY_TIME);
  137. }
  138. return 0;
  139. }
  140. static int gpio_acts_config(const struct device *dev, gpio_pin_t pin,
  141. gpio_flags_t flags)
  142. {
  143. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  144. int pin_num = cfg->grp*32 + pin;
  145. unsigned int val, key, set_val, set_mask;
  146. int reg_addr;
  147. set_val = 0;
  148. set_mask = 0xfff;
  149. if (flags & GPIO_OUTPUT){
  150. if (flags & GPIO_OUTPUT_INIT_HIGH) {
  151. /* Set the pin. */
  152. gpio_acts_port_set_bits_raw(dev, BIT(pin));
  153. }
  154. if (flags & GPIO_OUTPUT_INIT_LOW) {
  155. /* Clear the pin. */
  156. gpio_acts_port_clear_bits_raw(dev, BIT(pin));
  157. }
  158. set_val = GPIO_CTL_GPIO_OUTEN;
  159. }else if (flags & GPIO_INPUT){
  160. set_val = GPIO_CTL_GPIO_INEN;
  161. }
  162. if (flags & GPIO_PULL_UP)
  163. set_val |= GPIO_CTL_PULLUP;
  164. if (flags & GPIO_PULL_DOWN)
  165. set_val |= GPIO_CTL_PULLDOWN;
  166. if (flags & GPIO_INT_DEBOUNCE)
  167. set_val |= GPIO_CTL_SMIT;
  168. key = irq_lock();
  169. #if defined(CONFIG_WIO) && (CONFIG_WIO == 1)
  170. if(cfg->grp != 3){
  171. reg_addr = GPIO_REG_CTL(cfg->base, pin_num);
  172. }else{
  173. reg_addr = WIO_REG_CTL(pin);
  174. }
  175. #else
  176. reg_addr = GPIO_REG_CTL(cfg->base, pin_num);
  177. #endif
  178. val = sys_read32(reg_addr);
  179. val &= ~set_mask;
  180. val |= set_val;
  181. sys_write32(val, reg_addr);
  182. irq_unlock(key);
  183. if(cfg->grp == 3){
  184. k_busy_wait(WIO_DELAY_TIME);
  185. }
  186. return 0;;
  187. }
  188. static int gpio_acts_port_toggle_bits(const struct device *dev, uint32_t mask)
  189. {
  190. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  191. int pin_num = cfg->grp*32;
  192. unsigned int val, key;
  193. key = irq_lock();
  194. #if defined(CONFIG_WIO) && (CONFIG_WIO == 1)
  195. if(cfg->grp != 3){
  196. val = sys_read32(GPIO_REG_ODAT(cfg->base, pin_num));
  197. val ^= mask;
  198. sys_write32(val, GPIO_REG_ODAT(cfg->base, pin_num));
  199. }else{
  200. for(pin_num = 0; pin_num < 5; pin_num++){
  201. if(mask & (1 << pin_num)){
  202. sys_write32(sys_read32(WIO_REG_CTL(pin_num)) ^ (1 << 16), WIO_REG_CTL(pin_num));
  203. }
  204. }
  205. }
  206. #else
  207. val = sys_read32(GPIO_REG_ODAT(cfg->base, pin_num));
  208. val ^= mask;
  209. sys_write32(val, GPIO_REG_ODAT(cfg->base, pin_num));
  210. #endif
  211. irq_unlock(key);
  212. if(cfg->grp == 3){
  213. k_busy_wait(WIO_DELAY_TIME);
  214. }
  215. return 0;
  216. }
  217. static int gpio_acts_pin_interrupt_configure(const struct device *dev,
  218. gpio_pin_t pin, enum gpio_int_mode mode,
  219. enum gpio_int_trig trig)
  220. {
  221. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  222. int pin_num, reg_addr;
  223. unsigned int val, key, set_val, set_mask;
  224. #if defined(CONFIG_WIO) && (CONFIG_WIO == 1)
  225. if(cfg->grp != 3){
  226. pin_num = cfg->grp*32 + pin;
  227. reg_addr = GPIO_REG_CTL(cfg->base, pin_num);
  228. }else{
  229. pin_num = pin;
  230. reg_addr = WIO_REG_CTL(pin_num);
  231. }
  232. #else
  233. pin_num = cfg->grp*32 + pin;
  234. reg_addr = GPIO_REG_CTL(cfg->base, pin_num);
  235. #endif
  236. //must clear mfp mask to GPIO function
  237. set_mask = GPIO_CTL_INTC_MASK | GPIO_CTL_INTC_EN | GPIO_CTL_INC_TRIGGER_MASK | GPIO_CTL_MFP_MASK;
  238. set_val = GPIO_CTL_INTC_MASK | GPIO_CTL_INTC_EN;
  239. if(cfg->grp == 3){
  240. if(mode != GPIO_INT_MODE_DISABLED){
  241. //wio must set input enable
  242. set_val |= GPIO_CTL_GPIO_INEN | GPIO_CTL_PULLUP;
  243. }
  244. }
  245. if(mode == GPIO_INT_MODE_DISABLED){
  246. set_val = 0;
  247. }else if(mode == GPIO_INT_MODE_LEVEL){
  248. if(trig == GPIO_INT_TRIG_LOW)
  249. set_val |= GPIO_CTL_INC_TRIGGER_LOW_LEVEL;
  250. else
  251. set_val |= GPIO_CTL_INC_TRIGGER_HIGH_LEVEL;
  252. }else{ //GPIO_INT_MODE_EDGE
  253. if(trig == GPIO_INT_TRIG_LOW)
  254. set_val |= GPIO_CTL_INC_TRIGGER_FALLING_EDGE;
  255. else if(trig == GPIO_INT_TRIG_HIGH)
  256. set_val |= GPIO_CTL_INC_TRIGGER_RISING_EDGE;
  257. else
  258. set_val |= GPIO_CTL_INC_TRIGGER_DUAL_EDGE;
  259. }
  260. key = irq_lock();
  261. val = sys_read32(reg_addr);
  262. val &= ~set_mask;
  263. val |= set_val;
  264. sys_write32(val, reg_addr);
  265. irq_unlock(key);
  266. if(cfg->grp == 3){
  267. k_busy_wait(WIO_DELAY_TIME);
  268. }
  269. return 0;
  270. }
  271. static void gpio_acts_isr(void *arg)
  272. {
  273. struct device *dev = (struct device *)arg;
  274. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  275. struct gpio_acts_runtime *context;
  276. uint32_t int_stat, i;
  277. for(i = 0; i < GPIO_MAX_IRQ_GRP; i++ ){
  278. int_stat = sys_read32(GPIO_REG_IRQ_PD(cfg->base, i*32));
  279. if(int_stat){
  280. if(g_gpio_dev[i] != NULL){
  281. context = g_gpio_dev[i]->data;
  282. gpio_fire_callbacks(&context->cb, dev, int_stat);
  283. }else{
  284. printk("gpio-irq:err,grp=%d,stat=0x%x\n", i, int_stat);
  285. }
  286. sys_write32(int_stat, GPIO_REG_IRQ_PD(cfg->base, i*32));
  287. }
  288. }
  289. #if defined(CONFIG_WIO) && (CONFIG_WIO == 1)
  290. for(i = 0; i < GPIO_WIO_MAX_PIN_NUM; i++ ){
  291. int_stat = sys_read32(WIO_REG_CTL(i)) & WIO_CTL_INT_PD_MASK;
  292. if(int_stat){
  293. if(g_gpio_dev[GPIO_MAX_IRQ_GRP] != NULL){
  294. context = g_gpio_dev[GPIO_MAX_IRQ_GRP]->data;
  295. gpio_fire_callbacks(&context->cb, dev, (1 << i));
  296. }else{
  297. printk("WIO-irq:err,grp=%d,stat=0x%x\n", i, int_stat);
  298. }
  299. sys_write32(sys_read32(WIO_REG_CTL(i)), WIO_REG_CTL(i));
  300. }
  301. }
  302. #endif
  303. }
  304. static int gpio_acts_manage_callback(const struct device *port,
  305. struct gpio_callback *callback,
  306. bool set)
  307. {
  308. struct gpio_acts_runtime *context = port->data;
  309. return gpio_manage_callback(&context->cb, callback, set);
  310. }
  311. #if 0
  312. static int gpio_acts_irq_set(const struct device *port,
  313. gpio_pin_t pin, int eable)
  314. {
  315. const struct gpio_acts_config * const cfg = DEV_CFG(port);
  316. int pin_num = cfg->grp*32 + pin;
  317. unsigned int val, key;
  318. key = irq_lock();
  319. val = sys_read32(GPIO_REG_CTL(cfg->base, pin_num));
  320. if(eable)
  321. val |= (GPIO_CTL_INTC_MASK | GPIO_CTL_INTC_EN);
  322. else
  323. val &= ~(GPIO_CTL_INTC_MASK | GPIO_CTL_INTC_EN);
  324. sys_write32(val, GPIO_REG_CTL(cfg->base, pin_num));
  325. irq_unlock(key);
  326. return 0;
  327. }
  328. #endif
  329. static const struct gpio_driver_api gpio_acts_api = {
  330. .pin_configure = gpio_acts_config,
  331. .port_get_raw = gpio_acts_port_get_raw,
  332. .port_set_masked_raw = gpio_acts_port_set_masked_raw,
  333. .port_set_bits_raw = gpio_acts_port_set_bits_raw,
  334. .port_clear_bits_raw = gpio_acts_port_clear_bits_raw,
  335. .port_toggle_bits = gpio_acts_port_toggle_bits,
  336. .pin_interrupt_configure = gpio_acts_pin_interrupt_configure,
  337. .manage_callback = gpio_acts_manage_callback,
  338. };
  339. static void port_acts_config_func(const struct device *dev);
  340. int gpio_acts_init(const struct device *dev)
  341. {
  342. static uint8_t b_init=0;
  343. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  344. if(cfg->grp <= GPIO_MAX_GRP) {
  345. if(g_gpio_dev[cfg->grp] != NULL){
  346. //printk("gpio:grp=%d is register\n", cfg->grp);
  347. }else{
  348. g_gpio_dev[cfg->grp] = (struct device *)dev; /*save for irq func*/
  349. //printk("gpio:grp=%d ok\n", cfg->grp);
  350. }
  351. }else{
  352. //printk("gpio:grp=%d err\n", cfg->grp);
  353. return 0;
  354. }
  355. if(b_init == 0){
  356. port_acts_config_func(dev);
  357. b_init = 1;
  358. printk("gpio irq init\n");
  359. }
  360. return 0;
  361. }
  362. #define GPIO_ACTS_INIT(n, devname) \
  363. static const struct gpio_acts_config port_##n##_acts_config = { \
  364. .common = { \
  365. .port_pin_mask = 0xffffffff,\
  366. }, \
  367. .base = GPIO_REG_BASE, \
  368. .grp = n, \
  369. }; \
  370. \
  371. static struct gpio_acts_runtime port_##n##_acts_runtime; \
  372. \
  373. DEVICE_DEFINE(port_##n##_acts, devname, \
  374. gpio_acts_init, NULL, &port_##n##_acts_runtime, \
  375. &port_##n##_acts_config, PRE_KERNEL_1, \
  376. CONFIG_KERNEL_INIT_PRIORITY_OBJECTS, \
  377. &gpio_acts_api);
  378. /*DT_INST_FOREACH_STATUS_OKAY(GPIO_ACTS_INIT)*/
  379. #if defined(CONFIG_GPIO_A) && (CONFIG_GPIO_A == 1)
  380. GPIO_ACTS_INIT(0, CONFIG_GPIO_A_NAME)
  381. #endif
  382. #if defined(CONFIG_GPIO_B) && (CONFIG_GPIO_B == 1)
  383. GPIO_ACTS_INIT(1, CONFIG_GPIO_B_NAME)
  384. #endif
  385. #if defined(CONFIG_GPIO_C) && (CONFIG_GPIO_C == 1)
  386. GPIO_ACTS_INIT(2, CONFIG_GPIO_C_NAME)
  387. #endif
  388. #if defined(CONFIG_WIO) && (CONFIG_WIO == 1)
  389. GPIO_ACTS_INIT(3, CONFIG_WIO_NAME)
  390. #endif
  391. static void port_acts_config_func(const struct device *dev)
  392. {
  393. IRQ_CONNECT(IRQ_ID_GPIO, CONFIG_GPIO_IRQ_PRI,
  394. gpio_acts_isr,
  395. DEVICE_GET(port_0_acts), 0);
  396. irq_enable(IRQ_ID_GPIO);
  397. }