hal_gpio.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. * Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
  3. *
  4. * Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
  5. * the the People's Republic of China and other countries.
  6. * All Allwinner Technology Co.,Ltd. trademarks are used with permission.
  7. *
  8. * DISCLAIMER
  9. * THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
  10. * IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
  11. * IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
  12. * ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
  13. * ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
  14. * COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
  15. * YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
  16. *
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
  19. * PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
  20. * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
  21. * THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
  22. * OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  23. * IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  28. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  30. * OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <stdio.h>
  33. #include <string.h>
  34. /* #include <init.h> */
  35. #include <hal_mem.h>
  36. #include <hal_gpio.h>
  37. #include <hal_log.h>
  38. #include "gpio.h"
  39. #include <sunxi_hal_common.h>
  40. #include <interrupt.h>
  41. static const struct gpio_desc **g_gpio_desc = NULL;
  42. /*
  43. * The following inlines stuffs a configuration parameter and data value
  44. * into and out of an unsigned long argument, as used by the generic pin config
  45. * system. We put the parameter in the lower 8 bits and the argument in the
  46. * upper 24 bits.
  47. */
  48. static inline pin_config_param_t pinconf_to_config_param(unsigned long config)
  49. {
  50. return (pin_config_param_t)(config & 0xffUL);
  51. }
  52. static inline gpio_pin_t pinconf_to_config_argument(unsigned long config)
  53. {
  54. return (uint32_t)((config >> 8) & 0xffffffUL);
  55. }
  56. static inline uint64_t pinconf_to_config_packed(pin_config_param_t param,
  57. unsigned long argument)
  58. {
  59. return GPIO_CFG_PACK(param, argument);
  60. }
  61. /*
  62. * The sunXi PIO registers are organized as is:
  63. * 0x00 - 0x0c Muxing values.
  64. * 8 pins per register, each pin having a 4bits value
  65. * 0x10 Pin values
  66. * 32 bits per register, each pin corresponding to one bit
  67. * 0x14 - 0x18 Drive level
  68. * 16 pins per register, each pin having a 2bits value
  69. * 0x1c - 0x20 Pull-Up values
  70. * 16 pins per register, each pin having a 2bits value
  71. *
  72. * This is for the first bank. Each bank will have the same layout,
  73. * with an offset being a multiple of 0x24.
  74. *
  75. * The following functions calculate from the pin number the register
  76. * and the bit offset that we should access.
  77. */
  78. static inline uint32_t gpio_mux_reg(gpio_pin_t pin)
  79. {
  80. pin %= BANK_BOUNDARY;
  81. uint32_t bank = pin / PINS_PER_BANK;
  82. uint32_t offset = bank * BANK_MEM_SIZE;
  83. offset += MUX_REGS_OFFSET;
  84. offset += pin % PINS_PER_BANK / MUX_PINS_PER_REG * 0x04;
  85. return round_down(offset, 4);
  86. }
  87. static inline uint32_t gpio_mux_offset(gpio_pin_t pin)
  88. {
  89. gpio_pin_t pin_num = pin % MUX_PINS_PER_REG;
  90. return pin_num * MUX_PINS_BITS;
  91. }
  92. static inline uint32_t gpio_data_reg(gpio_pin_t pin)
  93. {
  94. pin %= BANK_BOUNDARY;
  95. uint32_t bank = pin / PINS_PER_BANK;
  96. uint32_t offset = bank * BANK_MEM_SIZE;
  97. offset += DATA_REGS_OFFSET;
  98. offset += pin % PINS_PER_BANK / DATA_PINS_PER_REG * 0x04;
  99. return round_down(offset, 4);
  100. }
  101. static inline uint32_t gpio_data_offset(gpio_pin_t pin)
  102. {
  103. gpio_pin_t pin_num = pin % DATA_PINS_PER_REG;
  104. return pin_num * DATA_PINS_BITS;
  105. }
  106. static inline uint32_t gpio_dlevel_reg(gpio_pin_t pin)
  107. {
  108. pin %= BANK_BOUNDARY;
  109. uint32_t bank = pin / PINS_PER_BANK;
  110. uint32_t offset = bank * BANK_MEM_SIZE;
  111. offset += DLEVEL_REGS_OFFSET;
  112. offset += pin % PINS_PER_BANK / DLEVEL_PINS_PER_REG * 0x04;
  113. return round_down(offset, 4);
  114. }
  115. static inline uint32_t gpio_dlevel_offset(gpio_pin_t pin)
  116. {
  117. gpio_pin_t pin_num = pin % DLEVEL_PINS_PER_REG;
  118. return pin_num * DLEVEL_PINS_BITS;
  119. }
  120. static inline uint32_t gpio_pull_reg(gpio_pin_t pin)
  121. {
  122. pin %= BANK_BOUNDARY;
  123. uint32_t bank = pin / PINS_PER_BANK;
  124. uint32_t offset = bank * BANK_MEM_SIZE;
  125. offset += PULL_REGS_OFFSET;
  126. offset += pin % PINS_PER_BANK / PULL_PINS_PER_REG * 0x04;
  127. return round_down(offset, 4);
  128. }
  129. static inline uint32_t gpio_pull_offset(gpio_pin_t pin)
  130. {
  131. gpio_pin_t pin_num = pin % PULL_PINS_PER_REG;
  132. return pin_num * PULL_PINS_BITS;
  133. }
  134. static inline uint32_t gpio_irq_ctrl_reg_from_bank(u8 bank, unsigned bank_base)
  135. {
  136. return IRQ_CTRL_REG + (bank_base + bank) * IRQ_MEM_SIZE;
  137. }
  138. static inline uint32_t gpio_irq_ctrl_reg(uint32_t irq, unsigned bank_base)
  139. {
  140. uint32_t bank = irq / IRQ_PER_BANK;
  141. return gpio_irq_ctrl_reg_from_bank(bank, bank_base);
  142. }
  143. static inline uint32_t gpio_irq_ctrl_offset(uint32_t irq)
  144. {
  145. uint32_t offset = irq % IRQ_CTRL_IRQ_PER_REG;
  146. return offset * IRQ_CTRL_IRQ_BITS;
  147. }
  148. static inline uint32_t gpio_get_pin_base_from_bank(u8 bank, unsigned bank_base)
  149. {
  150. return (bank_base + bank) * IRQ_MEM_SIZE;
  151. }
  152. static inline uint32_t gpio_irq_status_reg_from_bank(u8 bank, unsigned bank_base)
  153. {
  154. return IRQ_STATUS_REG + (bank_base + bank) * IRQ_MEM_SIZE;
  155. }
  156. static inline uint32_t gpio_irq_status_reg(uint32_t irq, unsigned bank_base)
  157. {
  158. uint32_t bank = irq / IRQ_PER_BANK;
  159. return gpio_irq_status_reg_from_bank(bank, bank_base);
  160. }
  161. static inline uint32_t gpio_irq_debounce_from_bank(u8 bank, unsigned bank_base)
  162. {
  163. return IRQ_DEBOUNCE_REG + (bank_base + bank) * IRQ_MEM_SIZE;
  164. }
  165. static inline uint32_t gpio_irq_debounce_reg(uint32_t irq, unsigned bank_base)
  166. {
  167. uint32_t bank = irq / IRQ_PER_BANK;
  168. return gpio_irq_debounce_from_bank(bank, bank_base);
  169. }
  170. static inline uint32_t gpio_irq_status_offset(uint32_t irq)
  171. {
  172. uint32_t index = irq % IRQ_STATUS_IRQ_PER_REG;
  173. return index * IRQ_STATUS_IRQ_BITS;
  174. }
  175. static inline uint32_t gpio_irq_cfg_reg(uint32_t irq, unsigned bank_base)
  176. {
  177. uint32_t bank = irq / IRQ_PER_BANK;
  178. uint32_t reg = (irq % IRQ_PER_BANK) / IRQ_CFG_IRQ_PER_REG * 0x04;
  179. return IRQ_CFG_REG + (bank_base + bank) * IRQ_MEM_SIZE + reg;
  180. }
  181. static inline uint32_t gpio_irq_cfg_offset(uint32_t irq)
  182. {
  183. uint32_t index = irq % IRQ_CFG_IRQ_PER_REG;
  184. return index * IRQ_CFG_IRQ_BITS;
  185. }
  186. static int gpio_pconf_reg(gpio_pin_t pin, pin_config_param_t param,
  187. uint32_t *offset, uint32_t *shift, uint32_t *mask)
  188. {
  189. switch (param)
  190. {
  191. case GPIO_TYPE_DRV:
  192. *offset = gpio_dlevel_reg(pin);
  193. *shift = gpio_dlevel_offset(pin);
  194. *mask = DLEVEL_PINS_MASK;
  195. break;
  196. case GPIO_TYPE_PUD:
  197. *offset = gpio_pull_reg(pin);
  198. *shift = gpio_pull_offset(pin);
  199. *mask = PULL_PINS_MASK;
  200. break;
  201. case GPIO_TYPE_DAT:
  202. *offset = gpio_data_reg(pin);
  203. *shift = gpio_data_offset(pin);
  204. *mask = DATA_PINS_MASK;
  205. break;
  206. case GPIO_TYPE_FUNC:
  207. *offset = gpio_mux_reg(pin);
  208. *shift = gpio_mux_offset(pin);
  209. *mask = MUX_PINS_MASK;
  210. break;
  211. default:
  212. GPIO_ERR("Invalid mux type");
  213. return -1;
  214. }
  215. return 0;
  216. }
  217. static uint32_t count_gpio_bank_mask(void)
  218. {
  219. uint32_t max_bank = (uint32_t)GPIO_MAX_BANK;
  220. uint32_t mask = 0;
  221. do
  222. {
  223. mask |= 1 << (max_bank / PINS_PER_BANK);
  224. max_bank -= PINS_PER_BANK;
  225. if (max_bank == 0)
  226. {
  227. mask |= 1;
  228. }
  229. } while (max_bank);
  230. return mask;
  231. }
  232. static struct gpio_desc *pin_to_gpio_desc(gpio_pin_t pin)
  233. {
  234. if (pin < BANK_BOUNDARY) /* CPUX domain */
  235. {
  236. return (struct gpio_desc *)g_gpio_desc[0];
  237. }
  238. else /* CPUS domain */
  239. {
  240. return (struct gpio_desc *)g_gpio_desc[1];
  241. }
  242. return NULL;
  243. }
  244. static struct gpio_desc *irq_to_gpio_desc(uint32_t irq)
  245. {
  246. int i, j;
  247. struct gpio_desc *gpio_desc;
  248. for (i = 0; g_gpio_desc[i] != NULL; i++)
  249. {
  250. gpio_desc = (struct gpio_desc *)g_gpio_desc[i];
  251. for (j = 0; j < gpio_desc->irq_arry_size; j++)
  252. {
  253. if (gpio_desc->irq[j] == irq)
  254. {
  255. return gpio_desc;
  256. }
  257. }
  258. }
  259. GPIO_ERR("gpio to irq error!");
  260. return NULL;
  261. }
  262. static struct gpio_desc *virq_to_gpio_desc(uint32_t irq)
  263. {
  264. int i, j;
  265. struct gpio_desc *gpio_desc;
  266. for (i = 0; g_gpio_desc[i] != NULL; i++)
  267. {
  268. gpio_desc = (struct gpio_desc *)g_gpio_desc[i];
  269. for (j = 0; j < gpio_desc->irq_banks * IRQ_PER_BANK; j++)
  270. {
  271. if (gpio_desc->irq_desc[j].virq == irq)
  272. {
  273. return gpio_desc;
  274. }
  275. }
  276. }
  277. GPIO_ERR("gpio to virq error!");
  278. return NULL;
  279. }
  280. static void gpio_irq_ack(struct gpio_desc *gpio_desc, int i)
  281. {
  282. struct gpio_irq_desc *dirq = &gpio_desc->irq_desc[i];
  283. uint32_t hw_irq = dirq->virq - gpio_desc->virq_offset - GPIO_IRQ_START;
  284. unsigned bank_base = gpio_desc->irq_bank_base[hw_irq / IRQ_PER_BANK];
  285. uint32_t reg = gpio_irq_status_reg(hw_irq, bank_base);
  286. uint32_t status_idx = gpio_irq_status_offset(hw_irq);
  287. /* clear the pending */
  288. hal_writel(1 << status_idx, gpio_desc->membase + reg);
  289. }
  290. static irqreturn_t bad_gpio_irq_handle(int dummy, void *data)
  291. {
  292. GPIO_INFO("No irq registered handler for this calling !!");
  293. return 0;
  294. }
  295. static void gpio_irq_set_type(struct gpio_desc *gpio_desc, int irq_num, unsigned long type)
  296. {
  297. struct gpio_irq_desc *dirq = &gpio_desc->irq_desc[irq_num];
  298. uint32_t hw_irq = dirq->virq - gpio_desc->virq_offset - GPIO_IRQ_START;
  299. unsigned bank_base = gpio_desc->irq_bank_base[hw_irq / IRQ_PER_BANK];
  300. uint32_t reg = gpio_irq_cfg_reg(hw_irq, bank_base);
  301. uint32_t index = gpio_irq_cfg_offset(hw_irq);
  302. uint32_t mode, regval;
  303. switch (type)
  304. {
  305. case IRQ_TYPE_EDGE_RISING:
  306. mode = IRQ_EDGE_RISING;
  307. break;
  308. case IRQ_TYPE_EDGE_FALLING:
  309. mode = IRQ_EDGE_FALLING;
  310. break;
  311. case IRQ_TYPE_EDGE_BOTH:
  312. mode = IRQ_EDGE_BOTH;
  313. break;
  314. case IRQ_TYPE_LEVEL_HIGH:
  315. mode = IRQ_LEVEL_HIGH;
  316. break;
  317. case IRQ_TYPE_LEVEL_LOW:
  318. mode = IRQ_LEVEL_LOW;
  319. break;
  320. default:
  321. mode = IRQ_EDGE_RISING;
  322. }
  323. /*should use spin lock protect here*/
  324. regval = hal_readl(gpio_desc->membase + reg);
  325. regval &= ~(IRQ_CFG_IRQ_MASK << index);
  326. hal_writel(regval | (mode << index), gpio_desc->membase + reg);
  327. //regval = hal_readl(gpio_desc->membase + reg);
  328. //GPIO_ERR("gpio_desc->membase + reg: 0x%x\n", regval);
  329. }
  330. static irqreturn_t gpio_irq_handle(int dummy, void *data)
  331. {
  332. uint32_t hwirq = *((uint32_t *)data);
  333. uint32_t bank, reg, val, base_bank;
  334. struct gpio_desc *gpio_desc = irq_to_gpio_desc(hwirq);
  335. if (gpio_desc == NULL)
  336. {
  337. return 0;
  338. }
  339. for (bank = 0; bank < gpio_desc->irq_banks; bank ++)
  340. {
  341. if (hwirq == gpio_desc->irq[bank])
  342. {
  343. break;
  344. }
  345. }
  346. if (bank == gpio_desc->irq_banks)
  347. {
  348. return 0;
  349. }
  350. base_bank = gpio_desc->irq_bank_base[bank];
  351. reg = gpio_irq_status_reg_from_bank(bank, base_bank);
  352. val = hal_readl(gpio_desc->membase + reg);
  353. GPIO_INFO("hwirq = %ld, gpio_desc address is 0x%lx.", hwirq, gpio_desc->membase);
  354. GPIO_INFO("base_bank is %ld, hwirq is %ld, val is %ld.", base_bank, hwirq, val);
  355. if (val)
  356. {
  357. uint32_t irqoffset;
  358. uint32_t irq_pin;
  359. int i;
  360. for (irqoffset = 0; irqoffset < IRQ_PER_BANK; irqoffset++)
  361. {
  362. if ((1 << irqoffset) & val)
  363. {
  364. break;
  365. }
  366. }
  367. if (irqoffset >= IRQ_PER_BANK)
  368. {
  369. GPIO_INFO("return");
  370. return 0;
  371. }
  372. irq_pin = ((base_bank + bank) * IRQ_PER_BANK) + irqoffset + gpio_desc->virq_offset;
  373. for (i = 0; i < gpio_desc->irq_desc_size; i++)
  374. {
  375. if (irq_pin == gpio_desc->irq_desc[i].pin)
  376. {
  377. break;
  378. }
  379. }
  380. if (i >= gpio_desc->irq_desc_size)
  381. {
  382. return 0;
  383. }
  384. if (gpio_desc->irq_desc[i].irq_attach != NULL)
  385. {
  386. gpio_desc->irq_desc[i].irq_attach(gpio_desc->irq_desc[i].data);
  387. }
  388. else
  389. {
  390. gpio_desc->irq_desc[i].handle_irq(gpio_desc->irq_desc[i].virq, gpio_desc->irq_desc[i].data);
  391. }
  392. gpio_irq_ack(gpio_desc, i);
  393. }
  394. return 0;
  395. }
  396. bool hal_gpio_check_valid(gpio_pin_t pin)
  397. {
  398. uint32_t bank = pin / PINS_PER_BANK;
  399. uint32_t mask = count_gpio_bank_mask();
  400. if (!((1 << bank) & mask))
  401. {
  402. return false;
  403. }
  404. return true;
  405. }
  406. static int gpio_conf_set(gpio_pin_t pin, unsigned long *gpio_config)
  407. {
  408. struct gpio_desc *gpio_desc = pin_to_gpio_desc(pin);
  409. if (gpio_desc == NULL)
  410. {
  411. GPIO_ERR("gpio_desc is not inited");
  412. return -1;
  413. }
  414. unsigned long config = (unsigned long)gpio_config;
  415. uint32_t offset, shift, mask, reg;
  416. uint32_t arg;
  417. pin_config_param_t param;
  418. int ret;
  419. param = pinconf_to_config_param(config);
  420. arg = pinconf_to_config_argument(config);
  421. ret = gpio_pconf_reg(pin, param, &offset, &shift, &mask);
  422. if (ret < 0)
  423. {
  424. GPIO_ERR("can't get reg for pin %u", pin);
  425. return -1;
  426. }
  427. /* fix me: shuold we keep spin_lock to protect here?*/
  428. reg = hal_readl(gpio_desc->membase + offset);
  429. reg &= ~(mask << shift);
  430. hal_writel(reg | arg << shift, gpio_desc->membase + offset);
  431. return 0;
  432. }
  433. static int gpio_conf_get(gpio_pin_t pin, unsigned long *gpio_config)
  434. {
  435. struct gpio_desc *gpio_desc = pin_to_gpio_desc(pin);
  436. if (gpio_desc == NULL)
  437. {
  438. GPIO_ERR("gpio_desc is not inited");
  439. return -1;
  440. }
  441. uint32_t offset, shift, mask;
  442. uint32_t arg, val;
  443. pin_config_param_t param = pinconf_to_config_param(*gpio_config);
  444. int ret = 0;
  445. ret = gpio_pconf_reg(pin, param, &offset, &shift, &mask);
  446. if (ret < 0)
  447. {
  448. GPIO_ERR("can't get reg for pin %u", pin);
  449. return -1;
  450. }
  451. val = (hal_readl(gpio_desc->membase + offset) >> shift) & mask;
  452. switch (param)
  453. {
  454. case GPIO_TYPE_DRV:
  455. case GPIO_TYPE_DAT:
  456. case GPIO_TYPE_PUD:
  457. case GPIO_TYPE_FUNC:
  458. arg = val;
  459. break;
  460. default:
  461. ret = -1;
  462. GPIO_ERR("Invalid mux type");
  463. return -1;
  464. }
  465. if (!ret)
  466. {
  467. *gpio_config = pinconf_to_config_packed(param, arg);
  468. }
  469. return ret;
  470. }
  471. int hal_gpio_get_data(gpio_pin_t pin, gpio_data_t *data)
  472. {
  473. unsigned long config;
  474. int ret = 0;
  475. if (NULL == data)
  476. {
  477. ret = -1;
  478. GPIO_ERR("Invalid parameter!");
  479. return ret;
  480. }
  481. config = GPIO_CFG_PACK(GPIO_TYPE_DAT, 0xffffff);
  482. ret = gpio_conf_get(pin, &config);
  483. if (ret < 0)
  484. {
  485. GPIO_ERR("get conf error!");
  486. return ret;
  487. }
  488. *data = GPIO_CFG_UNPACK_VALUE(config);
  489. return ret;
  490. }
  491. int hal_gpio_set_data(gpio_pin_t pin, gpio_data_t data)
  492. {
  493. unsigned long config;
  494. int ret = 0;
  495. config = GPIO_CFG_PACK(GPIO_TYPE_DAT, data);
  496. ret = gpio_conf_set(pin, (unsigned long *)config);
  497. if (ret < 0)
  498. {
  499. GPIO_ERR("set conf error!");
  500. return ret;
  501. }
  502. return ret;
  503. }
  504. int hal_gpio_set_direction(gpio_pin_t pin, gpio_direction_t direction)
  505. {
  506. unsigned long config;
  507. int ret = 0;
  508. config = GPIO_CFG_PACK(GPIO_TYPE_FUNC, direction);
  509. ret = gpio_conf_set(pin, (unsigned long *)config);
  510. if (ret < 0)
  511. {
  512. GPIO_ERR("set conf error!");
  513. return ret;
  514. }
  515. return ret;
  516. }
  517. int hal_gpio_get_direction(gpio_pin_t pin, gpio_direction_t *direction)
  518. {
  519. unsigned long config;
  520. int ret = 0;
  521. if (NULL == direction)
  522. {
  523. ret = -1;
  524. GPIO_ERR("Invalid parameter!");
  525. return ret;
  526. }
  527. config = GPIO_CFG_PACK(GPIO_TYPE_FUNC, 0xffffff);
  528. ret = gpio_conf_get(pin, &config);
  529. if (ret < 0)
  530. {
  531. GPIO_ERR("get conf error!");
  532. return ret;
  533. }
  534. *direction = GPIO_CFG_UNPACK_VALUE(config);
  535. return ret;
  536. }
  537. int hal_gpio_set_pull(gpio_pin_t pin, gpio_pull_status_t pull)
  538. {
  539. unsigned long config;
  540. int ret = 0;
  541. config = GPIO_CFG_PACK(GPIO_TYPE_PUD, pull);
  542. ret = gpio_conf_set(pin, (unsigned long *)config);
  543. if (ret < 0)
  544. {
  545. GPIO_ERR("set conf error!");
  546. return ret;
  547. }
  548. return ret;
  549. }
  550. int hal_gpio_get_pull(gpio_pin_t pin, gpio_pull_status_t *pull)
  551. {
  552. unsigned long config;
  553. int ret = 0;
  554. if (NULL == pull)
  555. {
  556. ret = -1;
  557. GPIO_ERR("Invalid parameter!");
  558. return ret;
  559. }
  560. config = GPIO_CFG_PACK(GPIO_TYPE_PUD, 0xffffff);
  561. ret = gpio_conf_get(pin, &config);
  562. if (ret < 0)
  563. {
  564. GPIO_ERR("get conf error!");
  565. return ret;
  566. }
  567. *pull = GPIO_CFG_UNPACK_VALUE(config);
  568. return ret;
  569. }
  570. int hal_gpio_set_driving_level(gpio_pin_t pin, gpio_driving_level_t level)
  571. {
  572. unsigned long config;
  573. int ret = 0;
  574. config = GPIO_CFG_PACK(GPIO_TYPE_DRV, level);
  575. ret = gpio_conf_set(pin, (unsigned long *)config);
  576. if (ret < 0)
  577. {
  578. GPIO_ERR("set conf error!");
  579. return ret;
  580. }
  581. return ret;
  582. }
  583. int hal_gpio_get_driving_level(gpio_pin_t pin, gpio_driving_level_t *level)
  584. {
  585. unsigned long config;
  586. int ret = 0;
  587. if (NULL == level)
  588. {
  589. ret = -1;
  590. GPIO_ERR("Invalid parameter!");
  591. return ret;
  592. }
  593. config = GPIO_CFG_PACK(GPIO_TYPE_DRV, 0xffffff);
  594. ret = gpio_conf_get(pin, &config);
  595. if (ret < 0)
  596. {
  597. GPIO_ERR("get conf error!");
  598. return ret;
  599. }
  600. *level = GPIO_CFG_UNPACK_VALUE(config);
  601. return ret;
  602. }
  603. int hal_gpio_pinmux_set_function(gpio_pin_t pin, gpio_muxsel_t function_index)
  604. {
  605. unsigned long config;
  606. int ret = 0;
  607. config = GPIO_CFG_PACK(GPIO_TYPE_FUNC, function_index);
  608. ret = gpio_conf_set(pin, (unsigned long *)config);
  609. if (ret < 0)
  610. {
  611. GPIO_ERR("set pin mux error!");
  612. return ret;
  613. }
  614. return ret;
  615. }
  616. int hal_gpio_sel_vol_mode(gpio_pin_t pin, gpio_power_mode_t pm_sel)
  617. {
  618. uint32_t bank, temp;
  619. struct gpio_desc *gpio_desc;
  620. gpio_desc = pin_to_gpio_desc(pin);
  621. if (gpio_desc == NULL)
  622. {
  623. return -1;
  624. }
  625. bank = (pin - gpio_desc->pin_base) / PINS_PER_BANK;
  626. temp = hal_readl(gpio_desc->membase + POWER_MODE_SEL);
  627. temp |= (pm_sel << bank);
  628. hal_writel(temp, gpio_desc->membase + POWER_MODE_SEL);
  629. if (bank == 5)
  630. {
  631. temp = hal_readl(gpio_desc->membase + POWER_VOL_SEL);
  632. temp &= ~(1 >> 0);
  633. temp |= (!pm_sel);
  634. hal_writel(temp, gpio_desc->membase + POWER_VOL_SEL);
  635. }
  636. return 0;
  637. }
  638. int hal_gpio_set_debounce(gpio_pin_t pin, unsigned value)
  639. {
  640. uint32_t irq, hw_irq, reg, reg_val;
  641. struct gpio_desc *gpio_desc;
  642. unsigned bank_base;
  643. unsigned int val_clk_select, val_clk_per_scale;
  644. int ret = 0;
  645. gpio_desc = pin_to_gpio_desc(pin);
  646. if (gpio_desc == NULL)
  647. {
  648. return -1;
  649. }
  650. ret = hal_gpio_to_irq(pin, &irq);
  651. if (ret < 0)
  652. {
  653. GPIO_ERR("gpio to irq error");
  654. return -1;
  655. }
  656. hw_irq = irq - gpio_desc->virq_offset - GPIO_IRQ_START;
  657. bank_base = gpio_desc->irq_bank_base[hw_irq / IRQ_PER_BANK];
  658. reg = gpio_irq_debounce_reg(hw_irq, bank_base);
  659. reg_val = hal_readl(gpio_desc->membase + reg);
  660. val_clk_select = value & 1;
  661. val_clk_per_scale = (value >> 4) & 0x07;
  662. /*set debounce pio interrupt clock select */
  663. reg_val &= ~(1 << 0);
  664. reg_val |= val_clk_select;
  665. /* set debounce clock pre scale */
  666. reg_val &= ~(7 << 4);
  667. reg_val |= val_clk_per_scale << 4;
  668. hal_writel(reg_val, gpio_desc->membase + reg);
  669. return 0;
  670. }
  671. int hal_gpio_to_irq(gpio_pin_t pin, uint32_t *irq)
  672. {
  673. int i = 0;
  674. struct gpio_desc *gpio_desc = pin_to_gpio_desc(pin);
  675. for (i = 0; i < gpio_desc->irq_banks * IRQ_PER_BANK; i++)
  676. {
  677. if (pin != gpio_desc->irq_desc[i].pin)
  678. {
  679. continue;
  680. }
  681. GPIO_INFO("gpio %lu to irq %lu succeed!", pin, gpio_desc->irq_desc[i].virq);
  682. *irq = gpio_desc->irq_desc[i].virq;
  683. return 0;
  684. }
  685. return -1;
  686. }
  687. int hal_gpio_irq_attach(uint32_t irq, void (*hdle)(void *), unsigned long flags, void *data)
  688. {
  689. struct gpio_desc *gpio_desc = virq_to_gpio_desc(irq);
  690. GPIO_INFO("gpio_desc address is 0x%lx.", gpio_desc->membase);
  691. int irq_max_num = gpio_desc->irq_desc_size + GPIO_IRQ_START;
  692. int ret = 0;
  693. irq -= gpio_desc->virq_offset;
  694. if (irq >= GPIO_IRQ_START && irq < irq_max_num)
  695. {
  696. if (hdle && gpio_desc->irq_desc[irq - GPIO_IRQ_START].irq_attach == NULL)
  697. {
  698. gpio_desc->irq_desc[irq - GPIO_IRQ_START].irq_attach = hdle;
  699. gpio_desc->irq_desc[irq - GPIO_IRQ_START].flags = flags;
  700. gpio_desc->irq_desc[irq - GPIO_IRQ_START].data = data;
  701. }
  702. /*set irq tpye*/
  703. gpio_irq_set_type(gpio_desc, irq - GPIO_IRQ_START, flags);
  704. /*set pin mux*/
  705. ret = hal_gpio_pinmux_set_function(gpio_desc->irq_desc[irq - GPIO_IRQ_START].pin, GPIO_MUXSEL_EINT);
  706. if (ret < 0)
  707. {
  708. GPIO_ERR("set pin mux error!");
  709. return -1;
  710. }
  711. GPIO_INFO("request irq %lu succeed!", irq);
  712. return irq;
  713. }
  714. GPIO_ERR("Wrong irq NO.(%u) to request !!", (unsigned int)irq);
  715. return -1;
  716. }
  717. int hal_gpio_irq_request(uint32_t irq, irq_handler_t hdle, unsigned long flags, void *data)
  718. {
  719. struct gpio_desc *gpio_desc = virq_to_gpio_desc(irq);
  720. GPIO_INFO("gpio_desc address is 0x%lx.", gpio_desc->membase);
  721. int irq_max_num = gpio_desc->irq_desc_size + GPIO_IRQ_START;
  722. int ret = 0;
  723. irq -= gpio_desc->virq_offset;
  724. if (irq >= GPIO_IRQ_START && irq < irq_max_num)
  725. {
  726. if (hdle && gpio_desc->irq_desc[irq - GPIO_IRQ_START].handle_irq == bad_gpio_irq_handle)
  727. {
  728. gpio_desc->irq_desc[irq - GPIO_IRQ_START].handle_irq = hdle;
  729. gpio_desc->irq_desc[irq - GPIO_IRQ_START].flags = flags;
  730. gpio_desc->irq_desc[irq - GPIO_IRQ_START].data = data;
  731. }
  732. /*set irq tpye*/
  733. gpio_irq_set_type(gpio_desc, irq - GPIO_IRQ_START, flags);
  734. /*set pin mux*/
  735. ret = hal_gpio_pinmux_set_function(gpio_desc->irq_desc[irq - GPIO_IRQ_START].pin, GPIO_MUXSEL_EINT);
  736. if (ret < 0)
  737. {
  738. GPIO_ERR("set pin mux error!");
  739. return -1;
  740. }
  741. GPIO_INFO("request irq %lu succeed!", irq);
  742. return irq;
  743. }
  744. GPIO_ERR("Wrong irq NO.(%u) to request !!", (unsigned int)irq);
  745. return -1;
  746. }
  747. int hal_gpio_irq_free(uint32_t irq)
  748. {
  749. struct gpio_desc *gpio_desc = virq_to_gpio_desc(irq);
  750. int irq_max_num = gpio_desc->irq_desc_size + GPIO_IRQ_START;
  751. irq -= gpio_desc->virq_offset;
  752. if (irq >= GPIO_IRQ_START && irq < irq_max_num)
  753. {
  754. gpio_desc->irq_desc[irq - GPIO_IRQ_START].irq_attach = NULL;
  755. gpio_desc->irq_desc[irq - GPIO_IRQ_START].handle_irq = bad_gpio_irq_handle;
  756. gpio_desc->irq_desc[irq - GPIO_IRQ_START].flags = 0;
  757. gpio_desc->irq_desc[irq - GPIO_IRQ_START].data = NULL;
  758. GPIO_INFO("free irq %lu succeed!", irq);
  759. return irq;
  760. }
  761. GPIO_ERR("Wrong irq NO.(%u) to free !!", (unsigned int)irq);
  762. return -1;
  763. }
  764. int hal_gpio_irq_enable(uint32_t irq)
  765. {
  766. struct gpio_desc *gpio_desc = virq_to_gpio_desc(irq);
  767. GPIO_INFO("1:gpio_desc address is 0x%lx.", gpio_desc->membase);
  768. int irq_max_num = gpio_desc->irq_desc_size + GPIO_IRQ_START;
  769. uint32_t hw_irq = irq - gpio_desc->virq_offset - GPIO_IRQ_START;
  770. unsigned bank_base = gpio_desc->irq_bank_base[hw_irq / IRQ_PER_BANK];
  771. uint32_t reg = gpio_irq_ctrl_reg(hw_irq, bank_base);
  772. uint32_t index = gpio_irq_ctrl_offset(hw_irq);
  773. uint32_t val = 0;
  774. irq -= gpio_desc->virq_offset;
  775. if (irq < GPIO_IRQ_START || irq >= irq_max_num)
  776. {
  777. GPIO_ERR("Wrong irq NO.(%u) to enable !!", (unsigned int)irq);
  778. return -1;
  779. }
  780. /*clear pending*/
  781. gpio_irq_ack(gpio_desc, hw_irq);
  782. /*unmask the irq,should keep spin lock to protect*/
  783. val = hal_readl(gpio_desc->membase + reg);
  784. hal_writel(val | (1 << index), gpio_desc->membase + reg);
  785. return 0;
  786. }
  787. #ifdef CONFIG_STANDBY
  788. struct gpio_pm_reg_cache gpio_pm_reg;
  789. static int gpio_pm_alloc_mem(uint32_t desc_index, uint32_t mem_size)
  790. {
  791. if (desc_index > 1)
  792. {
  793. GPIO_ERR("index[%d] exceed desc_index range!", desc_index);
  794. return -1;
  795. }
  796. gpio_pm_reg.reg_dump[desc_index] = hal_malloc(mem_size);
  797. if (gpio_pm_reg.reg_dump[desc_index] == NULL)
  798. {
  799. GPIO_ERR("malloc reg_mem[%d] error!", desc_index);
  800. return -1;
  801. }
  802. gpio_pm_reg.reg_dump_size[desc_index] = mem_size;
  803. return 0;
  804. }
  805. int hal_gpio_suspend()
  806. {
  807. int i;
  808. void *mem = NULL;
  809. uint32_t mem_size;
  810. uint32_t flags;
  811. struct gpio_desc *gpio_desc = NULL;
  812. GPIO_INFO("gpio suspend\n");
  813. flags = hal_interrupt_save();
  814. for (i = 0; g_gpio_desc[i] != NULL; i++) {
  815. gpio_desc = (struct gpio_desc *)g_gpio_desc[i];
  816. mem = gpio_pm_reg.reg_dump[i];
  817. mem_size = gpio_pm_reg.reg_dump_size[i];
  818. if (mem != NULL)
  819. memcpy(mem, (uint32_t *)gpio_desc->membase, mem_size);
  820. }
  821. hal_interrupt_restore(flags);
  822. return 0;
  823. }
  824. int hal_gpio_resume()
  825. {
  826. int i;
  827. void *mem = NULL;
  828. uint32_t mem_size;
  829. uint32_t flags;
  830. struct gpio_desc *gpio_desc = NULL;
  831. flags = hal_interrupt_save();
  832. for (i = 0; g_gpio_desc[i] != NULL; i++) {
  833. gpio_desc = (struct gpio_desc *)g_gpio_desc[i];
  834. mem = gpio_pm_reg.reg_dump[i];
  835. mem_size = gpio_pm_reg.reg_dump_size[i];
  836. if (gpio_pm_reg.reg_dump[i] != NULL)
  837. memcpy((uint32_t *)gpio_desc->membase, mem, mem_size);
  838. }
  839. hal_interrupt_restore(flags);
  840. GPIO_INFO("gpio resume");
  841. return 0;
  842. }
  843. #else
  844. static int gpio_pm_alloc_mem(uint32_t desc_index, uint32_t mem_size)
  845. {
  846. return 0;
  847. }
  848. #endif
  849. int hal_gpio_irq_disable(uint32_t irq)
  850. {
  851. struct gpio_desc *gpio_desc = virq_to_gpio_desc(irq);
  852. GPIO_INFO("gpio_desc address is 0x%lx.", gpio_desc->membase);
  853. int irq_max_num = gpio_desc->irq_desc_size + GPIO_IRQ_START;
  854. uint32_t hw_irq = irq - gpio_desc->virq_offset - GPIO_IRQ_START;
  855. unsigned bank_base = gpio_desc->irq_bank_base[hw_irq / IRQ_PER_BANK];
  856. uint32_t reg = gpio_irq_ctrl_reg(hw_irq, bank_base);
  857. uint32_t index = gpio_irq_ctrl_offset(hw_irq);
  858. uint32_t val = 0;
  859. irq -= gpio_desc->virq_offset;
  860. if (irq < GPIO_IRQ_START || irq >= irq_max_num)
  861. {
  862. GPIO_ERR("Wrong irq NO.(%u) to enable !!", (unsigned int)irq);
  863. return -1;
  864. }
  865. /*mask the irq,should keep spin lock to protect*/
  866. val = hal_readl(gpio_desc->membase + reg);
  867. hal_writel(val & ~(1 << index), gpio_desc->membase + reg);
  868. return 0;
  869. }
  870. int hal_gpio_init(void)
  871. {
  872. int i, j, ret;
  873. struct gpio_desc *gpio_desc = NULL;
  874. struct gpio_irq_desc *irq_desc = NULL;
  875. int irq_desc_array_size = 0;
  876. char *irqname = NULL;
  877. /* initialize g_gpio_desc */
  878. g_gpio_desc = gpio_get_platform_desc();
  879. if (g_gpio_desc == NULL)
  880. {
  881. GPIO_ERR("initialize global platform desc failed!");
  882. return -1;
  883. }
  884. for (j = 0; g_gpio_desc[j] != NULL; j++)
  885. {
  886. gpio_desc = (struct gpio_desc *)g_gpio_desc[j];
  887. irq_desc_array_size = gpio_desc->irq_banks * IRQ_PER_BANK;
  888. gpio_desc->irq_desc_size = irq_desc_array_size;
  889. irq_desc = (struct gpio_irq_desc *)hal_malloc(irq_desc_array_size * sizeof(struct gpio_irq_desc));
  890. if (irq_desc == NULL)
  891. {
  892. GPIO_ERR("alloc memory failed!");
  893. return -1;
  894. }
  895. ret = gpio_pm_alloc_mem(j, gpio_desc->resource_size);
  896. if (ret)
  897. {
  898. GPIO_ERR("gpio[%d] pm alloc mem err!", j);
  899. return ret;
  900. }
  901. memset(irq_desc, 0, irq_desc_array_size * sizeof(struct gpio_irq_desc));
  902. for (i = 0; i < irq_desc_array_size; i++)
  903. {
  904. unsigned int j = i / IRQ_PER_BANK;
  905. unsigned int k = i % IRQ_PER_BANK;
  906. unsigned bank_base = gpio_desc->irq_bank_base[j];
  907. irq_desc[i].pin = gpio_get_pin_base_from_bank(j, bank_base) + gpio_desc->virq_offset + k;
  908. irq_desc[i].virq = GPIO_IRQ_START + gpio_desc->virq_offset + i;
  909. irq_desc[i].handle_irq = bad_gpio_irq_handle;
  910. irq_desc[i].irq_attach = NULL;
  911. }
  912. gpio_desc->irq_desc = irq_desc;
  913. for (i = 0; i < gpio_desc->irq_banks; i++)
  914. {
  915. /* mask all irq */
  916. unsigned bank_base = gpio_desc->irq_bank_base[i];
  917. hal_writel(0, gpio_desc->membase +
  918. gpio_irq_ctrl_reg_from_bank(i, bank_base));
  919. /* clear pending flags */
  920. hal_writel(0xffffffff, gpio_desc->membase +
  921. gpio_irq_status_reg_from_bank(i, bank_base));
  922. }
  923. /* request irq */
  924. for (i = 0; i < gpio_desc->irq_arry_size; i++)
  925. {
  926. irqname = (char *)hal_malloc(12);
  927. if(irqname == NULL)
  928. {
  929. GPIO_ERR("fatal error, malloc failure.");
  930. return -1;
  931. }
  932. memset(irqname, 0x00, 12);
  933. sprintf(irqname, "gpio-ctl%d%d", j, i);
  934. ret = request_irq(gpio_desc->irq[i], gpio_irq_handle, 0, irqname, (void *)&gpio_desc->irq[i]);
  935. #if 0
  936. rt_hw_interrupt_install(gpio_desc->irq[i], gpio_irq_handle, RT_NULL, irqname);
  937. rt_hw_interrupt_umask(gpio_desc->irq[i]);
  938. #endif
  939. }
  940. #if 1
  941. /* enable irq */
  942. for (i = 0; i < gpio_desc->irq_arry_size; i++)
  943. {
  944. enable_irq(gpio_desc->irq[i]);
  945. }
  946. #endif
  947. }
  948. GPIO_INFO("gpio init success!");
  949. return 0;
  950. }