gpio_acts.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. static struct device *g_gpio_dev[GPIO_MAX_GRP + 1];
  16. struct gpio_acts_config {
  17. /* gpio_driver_config needs to be first */
  18. struct gpio_driver_config common;
  19. uint32_t base;
  20. uint8_t grp;
  21. };
  22. struct gpio_acts_runtime {
  23. /* gpio_data needs to be first */
  24. struct gpio_driver_data common;
  25. sys_slist_t cb;
  26. };
  27. #define DEV_CFG(dev) \
  28. ((const struct gpio_acts_config * const)(dev)->config)
  29. #define DEV_DATA(dev) \
  30. ((struct gpio_acts_runtime * const)(dev)->data)
  31. static int gpio_acts_port_get_raw(const struct device *dev, uint32_t *value)
  32. {
  33. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  34. int pin_num = cfg->grp*32;
  35. unsigned int key;
  36. key = irq_lock();
  37. *value = sys_read32(GPIO_REG_IDAT(cfg->base, pin_num));
  38. irq_unlock(key);
  39. return 0;
  40. }
  41. static int gpio_acts_port_set_masked_raw(const struct device *dev, uint32_t mask,
  42. uint32_t value)
  43. {
  44. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  45. int pin_num = cfg->grp*32;
  46. unsigned int val, key;
  47. key = irq_lock();
  48. val = sys_read32(GPIO_REG_ODAT(cfg->base, pin_num));
  49. val = (val&~mask) | (mask & value);
  50. sys_write32(val, GPIO_REG_ODAT(cfg->base, pin_num));
  51. irq_unlock(key);
  52. return 0;
  53. }
  54. static int gpio_acts_port_set_bits_raw(const struct device *dev, uint32_t mask)
  55. {
  56. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  57. int pin_num = cfg->grp*32;
  58. unsigned int key;
  59. key = irq_lock();
  60. sys_write32(mask, GPIO_REG_BSR(cfg->base, pin_num));
  61. irq_unlock(key);
  62. return 0;
  63. }
  64. static int gpio_acts_port_clear_bits_raw(const struct device *dev, uint32_t mask)
  65. {
  66. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  67. int pin_num = cfg->grp*32;
  68. unsigned int key;
  69. key = irq_lock();
  70. sys_write32(mask, GPIO_REG_BRR(cfg->base, pin_num));
  71. irq_unlock(key);
  72. return 0;
  73. }
  74. static int gpio_acts_config(const struct device *dev, gpio_pin_t pin,
  75. gpio_flags_t flags)
  76. {
  77. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  78. int pin_num = cfg->grp*32 + pin;
  79. unsigned int val, key, set_val, set_mask;
  80. set_val = 0;
  81. set_mask = 0xfff;
  82. if (flags & GPIO_OUTPUT){
  83. if (flags & GPIO_OUTPUT_INIT_HIGH) {
  84. /* Set the pin. */
  85. gpio_acts_port_set_bits_raw(dev, BIT(pin));
  86. }
  87. if (flags & GPIO_OUTPUT_INIT_LOW) {
  88. /* Clear the pin. */
  89. gpio_acts_port_clear_bits_raw(dev, BIT(pin));
  90. }
  91. set_val = GPIO_CTL_GPIO_OUTEN;
  92. }else if (flags & GPIO_INPUT){
  93. set_val = GPIO_CTL_GPIO_INEN;
  94. }
  95. if (flags & GPIO_PULL_UP)
  96. set_val |= GPIO_CTL_PULLUP;
  97. if (flags & GPIO_PULL_DOWN)
  98. set_val |= GPIO_CTL_PULLDOWN;
  99. if (flags & GPIO_INT_DEBOUNCE)
  100. set_val |= GPIO_CTL_SMIT;
  101. key = irq_lock();
  102. val = sys_read32(GPIO_REG_CTL(cfg->base, pin_num));
  103. val &= ~set_mask;
  104. val |= set_val;
  105. sys_write32(val, GPIO_REG_CTL(cfg->base, pin_num));
  106. irq_unlock(key);
  107. return 0;;
  108. }
  109. static int gpio_acts_port_toggle_bits(const struct device *dev, uint32_t mask)
  110. {
  111. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  112. int pin_num = cfg->grp*32;
  113. unsigned int val, key;
  114. key = irq_lock();
  115. val = sys_read32(GPIO_REG_ODAT(cfg->base, pin_num));
  116. val ^= mask;
  117. sys_write32(val, GPIO_REG_ODAT(cfg->base, pin_num));
  118. irq_unlock(key);
  119. return 0;
  120. }
  121. static int gpio_acts_pin_interrupt_configure(const struct device *dev,
  122. gpio_pin_t pin, enum gpio_int_mode mode,
  123. enum gpio_int_trig trig)
  124. {
  125. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  126. int pin_num, reg_addr;
  127. unsigned int val, key, set_val, set_mask;
  128. #if defined(CONFIG_WIO) && (CONFIG_WIO == 1)
  129. if(cfg->grp != 3){
  130. pin_num = cfg->grp*32 + pin;
  131. reg_addr = GPIO_REG_CTL(cfg->base, pin_num);
  132. }else{
  133. pin_num = pin;
  134. reg_addr = WIO_REG_CTL(pin_num);
  135. }
  136. #else
  137. pin_num = cfg->grp*32 + pin;
  138. reg_addr = GPIO_REG_CTL(cfg->base, pin_num);
  139. #endif
  140. //must clear mfp mask to GPIO function
  141. set_mask = GPIO_CTL_INTC_MASK | GPIO_CTL_INTC_EN | GPIO_CTL_INC_TRIGGER_MASK | GPIO_CTL_MFP_MASK;
  142. set_val = GPIO_CTL_INTC_MASK | GPIO_CTL_INTC_EN;
  143. if(cfg->grp == 3){
  144. if(mode != GPIO_INT_MODE_DISABLED){
  145. //wio must set input enable
  146. set_val |= GPIO_CTL_GPIO_INEN | GPIO_CTL_PULLUP;
  147. }
  148. }
  149. if(mode == GPIO_INT_MODE_DISABLED){
  150. set_val = 0;
  151. }else if(mode == GPIO_INT_MODE_LEVEL){
  152. if(trig == GPIO_INT_TRIG_LOW)
  153. set_val |= GPIO_CTL_INC_TRIGGER_LOW_LEVEL;
  154. else
  155. set_val |= GPIO_CTL_INC_TRIGGER_HIGH_LEVEL;
  156. }else{ //GPIO_INT_MODE_EDGE
  157. if(trig == GPIO_INT_TRIG_LOW)
  158. set_val |= GPIO_CTL_INC_TRIGGER_FALLING_EDGE;
  159. else if(trig == GPIO_INT_TRIG_HIGH)
  160. set_val |= GPIO_CTL_INC_TRIGGER_RISING_EDGE;
  161. else
  162. set_val |= GPIO_CTL_INC_TRIGGER_DUAL_EDGE;
  163. }
  164. key = irq_lock();
  165. val = sys_read32(reg_addr);
  166. val &= ~set_mask;
  167. val |= set_val;
  168. sys_write32(val, reg_addr);
  169. irq_unlock(key);
  170. return 0;
  171. }
  172. static void gpio_acts_isr(void *arg)
  173. {
  174. struct device *dev = (struct device *)arg;
  175. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  176. struct gpio_acts_runtime *context;
  177. uint32_t int_stat, i;
  178. for(i = 0; i < GPIO_MAX_IRQ_GRP; i++ ){
  179. int_stat = sys_read32(GPIO_REG_IRQ_PD(cfg->base, i*32));
  180. if(int_stat){
  181. if(g_gpio_dev[i] != NULL){
  182. context = g_gpio_dev[i]->data;
  183. gpio_fire_callbacks(&context->cb, dev, int_stat);
  184. }else{
  185. printk("gpio-irq:err,grp=%d,stat=0x%x\n", i, int_stat);
  186. }
  187. sys_write32(int_stat, GPIO_REG_IRQ_PD(cfg->base, i*32));
  188. }
  189. }
  190. #if defined(CONFIG_WIO) && (CONFIG_WIO == 1)
  191. for(i = 0; i < GPIO_WIO_MAX_PIN_NUM; i++ ){
  192. int_stat = sys_read32(WIO_REG_CTL(i)) & WIO_CTL_INT_PD_MASK;
  193. if(int_stat){
  194. if(g_gpio_dev[GPIO_MAX_IRQ_GRP] != NULL){
  195. context = g_gpio_dev[GPIO_MAX_IRQ_GRP]->data;
  196. gpio_fire_callbacks(&context->cb, dev, (1 << i));
  197. }else{
  198. printk("WIO-irq:err,grp=%d,stat=0x%x\n", i, int_stat);
  199. }
  200. sys_write32(sys_read32(WIO_REG_CTL(i)), WIO_REG_CTL(i));
  201. }
  202. }
  203. #endif
  204. }
  205. static int gpio_acts_manage_callback(const struct device *port,
  206. struct gpio_callback *callback,
  207. bool set)
  208. {
  209. struct gpio_acts_runtime *context = port->data;
  210. return gpio_manage_callback(&context->cb, callback, set);
  211. }
  212. #if 0
  213. static int gpio_acts_irq_set(const struct device *port,
  214. gpio_pin_t pin, int eable)
  215. {
  216. const struct gpio_acts_config * const cfg = DEV_CFG(port);
  217. int pin_num = cfg->grp*32 + pin;
  218. unsigned int val, key;
  219. key = irq_lock();
  220. val = sys_read32(GPIO_REG_CTL(cfg->base, pin_num));
  221. if(eable)
  222. val |= (GPIO_CTL_INTC_MASK | GPIO_CTL_INTC_EN);
  223. else
  224. val &= ~(GPIO_CTL_INTC_MASK | GPIO_CTL_INTC_EN);
  225. sys_write32(val, GPIO_REG_CTL(cfg->base, pin_num));
  226. irq_unlock(key);
  227. return 0;
  228. }
  229. #endif
  230. static const struct gpio_driver_api gpio_acts_api = {
  231. .pin_configure = gpio_acts_config,
  232. .port_get_raw = gpio_acts_port_get_raw,
  233. .port_set_masked_raw = gpio_acts_port_set_masked_raw,
  234. .port_set_bits_raw = gpio_acts_port_set_bits_raw,
  235. .port_clear_bits_raw = gpio_acts_port_clear_bits_raw,
  236. .port_toggle_bits = gpio_acts_port_toggle_bits,
  237. .pin_interrupt_configure = gpio_acts_pin_interrupt_configure,
  238. .manage_callback = gpio_acts_manage_callback,
  239. };
  240. static void port_acts_config_func(const struct device *dev);
  241. int gpio_acts_init(const struct device *dev)
  242. {
  243. static uint8_t b_init=0;
  244. const struct gpio_acts_config * const cfg = DEV_CFG(dev);
  245. if(cfg->grp <= GPIO_MAX_GRP) {
  246. if(g_gpio_dev[cfg->grp] != NULL){
  247. //printk("gpio:grp=%d is register\n", cfg->grp);
  248. }else{
  249. g_gpio_dev[cfg->grp] = (struct device *)dev; /*save for irq func*/
  250. //printk("gpio:grp=%d ok\n", cfg->grp);
  251. }
  252. }else{
  253. //printk("gpio:grp=%d err\n", cfg->grp);
  254. return 0;
  255. }
  256. if(b_init == 0){
  257. port_acts_config_func(dev);
  258. b_init = 1;
  259. printk("gpio irq init\n");
  260. }
  261. return 0;
  262. }
  263. #define GPIO_ACTS_INIT(n, devname) \
  264. static const struct gpio_acts_config port_##n##_acts_config = { \
  265. .common = { \
  266. .port_pin_mask = 0xffffffff,\
  267. }, \
  268. .base = GPIO_REG_BASE, \
  269. .grp = n, \
  270. }; \
  271. \
  272. static struct gpio_acts_runtime port_##n##_acts_runtime; \
  273. \
  274. DEVICE_DEFINE(port_##n##_acts, devname, \
  275. gpio_acts_init, NULL, &port_##n##_acts_runtime, \
  276. &port_##n##_acts_config, PRE_KERNEL_1, \
  277. CONFIG_KERNEL_INIT_PRIORITY_OBJECTS, \
  278. &gpio_acts_api);
  279. /*DT_INST_FOREACH_STATUS_OKAY(GPIO_ACTS_INIT)*/
  280. #if defined(CONFIG_GPIO_A) && (CONFIG_GPIO_A == 1)
  281. GPIO_ACTS_INIT(0, CONFIG_GPIO_A_NAME)
  282. #endif
  283. #if defined(CONFIG_GPIO_B) && (CONFIG_GPIO_B == 1)
  284. GPIO_ACTS_INIT(1, CONFIG_GPIO_B_NAME)
  285. #endif
  286. #if defined(CONFIG_GPIO_C) && (CONFIG_GPIO_C == 1)
  287. GPIO_ACTS_INIT(2, CONFIG_GPIO_C_NAME)
  288. #endif
  289. #if defined(CONFIG_WIO) && (CONFIG_WIO == 1)
  290. GPIO_ACTS_INIT(3, CONFIG_WIO_NAME)
  291. #endif
  292. static void port_acts_config_func(const struct device *dev)
  293. {
  294. IRQ_CONNECT(IRQ_ID_GPIO, CONFIG_GPIO_IRQ_PRI,
  295. gpio_acts_isr,
  296. DEVICE_GET(port_0_acts), 0);
  297. irq_enable(IRQ_ID_GPIO);
  298. }