pinctrl-sirf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*
  2. * pinmux driver for CSR SiRFprimaII
  3. *
  4. * Authors:
  5. * Rongjun Ying <rongjun.ying@csr.com>
  6. * Yuping Luo <yuping.luo@csr.com>
  7. * Barry Song <baohua.song@csr.com>
  8. *
  9. * Copyright (c) 2011 - 2014 Cambridge Silicon Radio Limited, a CSR plc group
  10. * company.
  11. *
  12. * Licensed under GPLv2 or later.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/irq.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/io.h>
  18. #include <linux/slab.h>
  19. #include <linux/err.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include <linux/pinctrl/consumer.h>
  23. #include <linux/pinctrl/machine.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/bitops.h>
  29. #include <linux/gpio.h>
  30. #include <linux/of_gpio.h>
  31. #include "pinctrl-sirf.h"
  32. #define DRIVER_NAME "pinmux-sirf"
  33. struct sirfsoc_gpio_bank {
  34. int id;
  35. int parent_irq;
  36. spinlock_t lock;
  37. };
  38. struct sirfsoc_gpio_chip {
  39. struct of_mm_gpio_chip chip;
  40. struct sirfsoc_gpio_bank sgpio_bank[SIRFSOC_GPIO_NO_OF_BANKS];
  41. spinlock_t lock;
  42. };
  43. static struct sirfsoc_pin_group *sirfsoc_pin_groups;
  44. static int sirfsoc_pingrp_cnt;
  45. static int sirfsoc_get_groups_count(struct pinctrl_dev *pctldev)
  46. {
  47. return sirfsoc_pingrp_cnt;
  48. }
  49. static const char *sirfsoc_get_group_name(struct pinctrl_dev *pctldev,
  50. unsigned selector)
  51. {
  52. return sirfsoc_pin_groups[selector].name;
  53. }
  54. static int sirfsoc_get_group_pins(struct pinctrl_dev *pctldev,
  55. unsigned selector,
  56. const unsigned **pins,
  57. unsigned *num_pins)
  58. {
  59. *pins = sirfsoc_pin_groups[selector].pins;
  60. *num_pins = sirfsoc_pin_groups[selector].num_pins;
  61. return 0;
  62. }
  63. static void sirfsoc_pin_dbg_show(struct pinctrl_dev *pctldev,
  64. struct seq_file *s, unsigned offset)
  65. {
  66. seq_printf(s, " " DRIVER_NAME);
  67. }
  68. static int sirfsoc_dt_node_to_map(struct pinctrl_dev *pctldev,
  69. struct device_node *np_config,
  70. struct pinctrl_map **map, unsigned *num_maps)
  71. {
  72. struct sirfsoc_pmx *spmx = pinctrl_dev_get_drvdata(pctldev);
  73. struct device_node *np;
  74. struct property *prop;
  75. const char *function, *group;
  76. int ret, index = 0, count = 0;
  77. /* calculate number of maps required */
  78. for_each_child_of_node(np_config, np) {
  79. ret = of_property_read_string(np, "sirf,function", &function);
  80. if (ret < 0) {
  81. of_node_put(np);
  82. return ret;
  83. }
  84. ret = of_property_count_strings(np, "sirf,pins");
  85. if (ret < 0) {
  86. of_node_put(np);
  87. return ret;
  88. }
  89. count += ret;
  90. }
  91. if (!count) {
  92. dev_err(spmx->dev, "No child nodes passed via DT\n");
  93. return -ENODEV;
  94. }
  95. *map = kzalloc(sizeof(**map) * count, GFP_KERNEL);
  96. if (!*map)
  97. return -ENOMEM;
  98. for_each_child_of_node(np_config, np) {
  99. of_property_read_string(np, "sirf,function", &function);
  100. of_property_for_each_string(np, "sirf,pins", prop, group) {
  101. (*map)[index].type = PIN_MAP_TYPE_MUX_GROUP;
  102. (*map)[index].data.mux.group = group;
  103. (*map)[index].data.mux.function = function;
  104. index++;
  105. }
  106. }
  107. *num_maps = count;
  108. return 0;
  109. }
  110. static void sirfsoc_dt_free_map(struct pinctrl_dev *pctldev,
  111. struct pinctrl_map *map, unsigned num_maps)
  112. {
  113. kfree(map);
  114. }
  115. static struct pinctrl_ops sirfsoc_pctrl_ops = {
  116. .get_groups_count = sirfsoc_get_groups_count,
  117. .get_group_name = sirfsoc_get_group_name,
  118. .get_group_pins = sirfsoc_get_group_pins,
  119. .pin_dbg_show = sirfsoc_pin_dbg_show,
  120. .dt_node_to_map = sirfsoc_dt_node_to_map,
  121. .dt_free_map = sirfsoc_dt_free_map,
  122. };
  123. static struct sirfsoc_pmx_func *sirfsoc_pmx_functions;
  124. static int sirfsoc_pmxfunc_cnt;
  125. static void sirfsoc_pinmux_endisable(struct sirfsoc_pmx *spmx,
  126. unsigned selector, bool enable)
  127. {
  128. int i;
  129. const struct sirfsoc_padmux *mux =
  130. sirfsoc_pmx_functions[selector].padmux;
  131. const struct sirfsoc_muxmask *mask = mux->muxmask;
  132. for (i = 0; i < mux->muxmask_counts; i++) {
  133. u32 muxval;
  134. muxval = readl(spmx->gpio_virtbase +
  135. SIRFSOC_GPIO_PAD_EN(mask[i].group));
  136. if (enable)
  137. muxval = muxval & ~mask[i].mask;
  138. else
  139. muxval = muxval | mask[i].mask;
  140. writel(muxval, spmx->gpio_virtbase +
  141. SIRFSOC_GPIO_PAD_EN(mask[i].group));
  142. }
  143. if (mux->funcmask && enable) {
  144. u32 func_en_val;
  145. func_en_val =
  146. readl(spmx->rsc_virtbase + mux->ctrlreg);
  147. func_en_val =
  148. (func_en_val & ~mux->funcmask) | (mux->funcval);
  149. writel(func_en_val, spmx->rsc_virtbase + mux->ctrlreg);
  150. }
  151. }
  152. static int sirfsoc_pinmux_set_mux(struct pinctrl_dev *pmxdev,
  153. unsigned selector,
  154. unsigned group)
  155. {
  156. struct sirfsoc_pmx *spmx;
  157. spmx = pinctrl_dev_get_drvdata(pmxdev);
  158. sirfsoc_pinmux_endisable(spmx, selector, true);
  159. return 0;
  160. }
  161. static int sirfsoc_pinmux_get_funcs_count(struct pinctrl_dev *pmxdev)
  162. {
  163. return sirfsoc_pmxfunc_cnt;
  164. }
  165. static const char *sirfsoc_pinmux_get_func_name(struct pinctrl_dev *pctldev,
  166. unsigned selector)
  167. {
  168. return sirfsoc_pmx_functions[selector].name;
  169. }
  170. static int sirfsoc_pinmux_get_groups(struct pinctrl_dev *pctldev,
  171. unsigned selector,
  172. const char * const **groups,
  173. unsigned * const num_groups)
  174. {
  175. *groups = sirfsoc_pmx_functions[selector].groups;
  176. *num_groups = sirfsoc_pmx_functions[selector].num_groups;
  177. return 0;
  178. }
  179. static int sirfsoc_pinmux_request_gpio(struct pinctrl_dev *pmxdev,
  180. struct pinctrl_gpio_range *range, unsigned offset)
  181. {
  182. struct sirfsoc_pmx *spmx;
  183. int group = range->id;
  184. u32 muxval;
  185. spmx = pinctrl_dev_get_drvdata(pmxdev);
  186. muxval = readl(spmx->gpio_virtbase +
  187. SIRFSOC_GPIO_PAD_EN(group));
  188. muxval = muxval | (1 << (offset - range->pin_base));
  189. writel(muxval, spmx->gpio_virtbase +
  190. SIRFSOC_GPIO_PAD_EN(group));
  191. return 0;
  192. }
  193. static struct pinmux_ops sirfsoc_pinmux_ops = {
  194. .set_mux = sirfsoc_pinmux_set_mux,
  195. .get_functions_count = sirfsoc_pinmux_get_funcs_count,
  196. .get_function_name = sirfsoc_pinmux_get_func_name,
  197. .get_function_groups = sirfsoc_pinmux_get_groups,
  198. .gpio_request_enable = sirfsoc_pinmux_request_gpio,
  199. };
  200. static struct pinctrl_desc sirfsoc_pinmux_desc = {
  201. .name = DRIVER_NAME,
  202. .pctlops = &sirfsoc_pctrl_ops,
  203. .pmxops = &sirfsoc_pinmux_ops,
  204. .owner = THIS_MODULE,
  205. };
  206. static void __iomem *sirfsoc_rsc_of_iomap(void)
  207. {
  208. const struct of_device_id rsc_ids[] = {
  209. { .compatible = "sirf,prima2-rsc" },
  210. {}
  211. };
  212. struct device_node *np;
  213. np = of_find_matching_node(NULL, rsc_ids);
  214. if (!np)
  215. panic("unable to find compatible rsc node in dtb\n");
  216. return of_iomap(np, 0);
  217. }
  218. static int sirfsoc_gpio_of_xlate(struct gpio_chip *gc,
  219. const struct of_phandle_args *gpiospec,
  220. u32 *flags)
  221. {
  222. if (gpiospec->args[0] > SIRFSOC_GPIO_NO_OF_BANKS * SIRFSOC_GPIO_BANK_SIZE)
  223. return -EINVAL;
  224. if (flags)
  225. *flags = gpiospec->args[1];
  226. return gpiospec->args[0];
  227. }
  228. static const struct of_device_id pinmux_ids[] = {
  229. { .compatible = "sirf,prima2-pinctrl", .data = &prima2_pinctrl_data, },
  230. { .compatible = "sirf,atlas6-pinctrl", .data = &atlas6_pinctrl_data, },
  231. {}
  232. };
  233. static int sirfsoc_pinmux_probe(struct platform_device *pdev)
  234. {
  235. int ret;
  236. struct sirfsoc_pmx *spmx;
  237. struct device_node *np = pdev->dev.of_node;
  238. const struct sirfsoc_pinctrl_data *pdata;
  239. /* Create state holders etc for this driver */
  240. spmx = devm_kzalloc(&pdev->dev, sizeof(*spmx), GFP_KERNEL);
  241. if (!spmx)
  242. return -ENOMEM;
  243. spmx->dev = &pdev->dev;
  244. platform_set_drvdata(pdev, spmx);
  245. spmx->gpio_virtbase = of_iomap(np, 0);
  246. if (!spmx->gpio_virtbase) {
  247. dev_err(&pdev->dev, "can't map gpio registers\n");
  248. return -ENOMEM;
  249. }
  250. spmx->rsc_virtbase = sirfsoc_rsc_of_iomap();
  251. if (!spmx->rsc_virtbase) {
  252. ret = -ENOMEM;
  253. dev_err(&pdev->dev, "can't map rsc registers\n");
  254. goto out_no_rsc_remap;
  255. }
  256. pdata = of_match_node(pinmux_ids, np)->data;
  257. sirfsoc_pin_groups = pdata->grps;
  258. sirfsoc_pingrp_cnt = pdata->grps_cnt;
  259. sirfsoc_pmx_functions = pdata->funcs;
  260. sirfsoc_pmxfunc_cnt = pdata->funcs_cnt;
  261. sirfsoc_pinmux_desc.pins = pdata->pads;
  262. sirfsoc_pinmux_desc.npins = pdata->pads_cnt;
  263. /* Now register the pin controller and all pins it handles */
  264. spmx->pmx = pinctrl_register(&sirfsoc_pinmux_desc, &pdev->dev, spmx);
  265. if (IS_ERR(spmx->pmx)) {
  266. dev_err(&pdev->dev, "could not register SIRFSOC pinmux driver\n");
  267. ret = PTR_ERR(spmx->pmx);
  268. goto out_no_pmx;
  269. }
  270. dev_info(&pdev->dev, "initialized SIRFSOC pinmux driver\n");
  271. return 0;
  272. out_no_pmx:
  273. iounmap(spmx->rsc_virtbase);
  274. out_no_rsc_remap:
  275. iounmap(spmx->gpio_virtbase);
  276. return ret;
  277. }
  278. #ifdef CONFIG_PM_SLEEP
  279. static int sirfsoc_pinmux_suspend_noirq(struct device *dev)
  280. {
  281. int i, j;
  282. struct sirfsoc_pmx *spmx = dev_get_drvdata(dev);
  283. for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
  284. for (j = 0; j < SIRFSOC_GPIO_BANK_SIZE; j++) {
  285. spmx->gpio_regs[i][j] = readl(spmx->gpio_virtbase +
  286. SIRFSOC_GPIO_CTRL(i, j));
  287. }
  288. spmx->ints_regs[i] = readl(spmx->gpio_virtbase +
  289. SIRFSOC_GPIO_INT_STATUS(i));
  290. spmx->paden_regs[i] = readl(spmx->gpio_virtbase +
  291. SIRFSOC_GPIO_PAD_EN(i));
  292. }
  293. spmx->dspen_regs = readl(spmx->gpio_virtbase + SIRFSOC_GPIO_DSP_EN0);
  294. for (i = 0; i < 3; i++)
  295. spmx->rsc_regs[i] = readl(spmx->rsc_virtbase + 4 * i);
  296. return 0;
  297. }
  298. static int sirfsoc_pinmux_resume_noirq(struct device *dev)
  299. {
  300. int i, j;
  301. struct sirfsoc_pmx *spmx = dev_get_drvdata(dev);
  302. for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
  303. for (j = 0; j < SIRFSOC_GPIO_BANK_SIZE; j++) {
  304. writel(spmx->gpio_regs[i][j], spmx->gpio_virtbase +
  305. SIRFSOC_GPIO_CTRL(i, j));
  306. }
  307. writel(spmx->ints_regs[i], spmx->gpio_virtbase +
  308. SIRFSOC_GPIO_INT_STATUS(i));
  309. writel(spmx->paden_regs[i], spmx->gpio_virtbase +
  310. SIRFSOC_GPIO_PAD_EN(i));
  311. }
  312. writel(spmx->dspen_regs, spmx->gpio_virtbase + SIRFSOC_GPIO_DSP_EN0);
  313. for (i = 0; i < 3; i++)
  314. writel(spmx->rsc_regs[i], spmx->rsc_virtbase + 4 * i);
  315. return 0;
  316. }
  317. static const struct dev_pm_ops sirfsoc_pinmux_pm_ops = {
  318. .suspend_noirq = sirfsoc_pinmux_suspend_noirq,
  319. .resume_noirq = sirfsoc_pinmux_resume_noirq,
  320. .freeze_noirq = sirfsoc_pinmux_suspend_noirq,
  321. .restore_noirq = sirfsoc_pinmux_resume_noirq,
  322. };
  323. #endif
  324. static struct platform_driver sirfsoc_pinmux_driver = {
  325. .driver = {
  326. .name = DRIVER_NAME,
  327. .of_match_table = pinmux_ids,
  328. #ifdef CONFIG_PM_SLEEP
  329. .pm = &sirfsoc_pinmux_pm_ops,
  330. #endif
  331. },
  332. .probe = sirfsoc_pinmux_probe,
  333. };
  334. static int __init sirfsoc_pinmux_init(void)
  335. {
  336. return platform_driver_register(&sirfsoc_pinmux_driver);
  337. }
  338. arch_initcall(sirfsoc_pinmux_init);
  339. static inline struct sirfsoc_gpio_bank *
  340. sirfsoc_gpio_to_bank(struct sirfsoc_gpio_chip *sgpio, unsigned int offset)
  341. {
  342. return &sgpio->sgpio_bank[offset / SIRFSOC_GPIO_BANK_SIZE];
  343. }
  344. static inline int sirfsoc_gpio_to_bankoff(unsigned int offset)
  345. {
  346. return offset % SIRFSOC_GPIO_BANK_SIZE;
  347. }
  348. static void sirfsoc_gpio_irq_ack(struct irq_data *d)
  349. {
  350. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  351. struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc);
  352. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
  353. int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
  354. u32 val, offset;
  355. unsigned long flags;
  356. offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
  357. spin_lock_irqsave(&sgpio->lock, flags);
  358. val = readl(sgpio->chip.regs + offset);
  359. writel(val, sgpio->chip.regs + offset);
  360. spin_unlock_irqrestore(&sgpio->lock, flags);
  361. }
  362. static void __sirfsoc_gpio_irq_mask(struct sirfsoc_gpio_chip *sgpio,
  363. struct sirfsoc_gpio_bank *bank,
  364. int idx)
  365. {
  366. u32 val, offset;
  367. unsigned long flags;
  368. offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
  369. spin_lock_irqsave(&sgpio->lock, flags);
  370. val = readl(sgpio->chip.regs + offset);
  371. val &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK;
  372. val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK;
  373. writel(val, sgpio->chip.regs + offset);
  374. spin_unlock_irqrestore(&sgpio->lock, flags);
  375. }
  376. static void sirfsoc_gpio_irq_mask(struct irq_data *d)
  377. {
  378. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  379. struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc);
  380. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
  381. __sirfsoc_gpio_irq_mask(sgpio, bank, d->hwirq % SIRFSOC_GPIO_BANK_SIZE);
  382. }
  383. static void sirfsoc_gpio_irq_unmask(struct irq_data *d)
  384. {
  385. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  386. struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc);
  387. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
  388. int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
  389. u32 val, offset;
  390. unsigned long flags;
  391. offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
  392. spin_lock_irqsave(&sgpio->lock, flags);
  393. val = readl(sgpio->chip.regs + offset);
  394. val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK;
  395. val |= SIRFSOC_GPIO_CTL_INTR_EN_MASK;
  396. writel(val, sgpio->chip.regs + offset);
  397. spin_unlock_irqrestore(&sgpio->lock, flags);
  398. }
  399. static int sirfsoc_gpio_irq_type(struct irq_data *d, unsigned type)
  400. {
  401. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  402. struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc);
  403. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
  404. int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
  405. u32 val, offset;
  406. unsigned long flags;
  407. offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
  408. spin_lock_irqsave(&sgpio->lock, flags);
  409. val = readl(sgpio->chip.regs + offset);
  410. val &= ~(SIRFSOC_GPIO_CTL_INTR_STS_MASK | SIRFSOC_GPIO_CTL_OUT_EN_MASK);
  411. switch (type) {
  412. case IRQ_TYPE_NONE:
  413. break;
  414. case IRQ_TYPE_EDGE_RISING:
  415. val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK |
  416. SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
  417. val &= ~SIRFSOC_GPIO_CTL_INTR_LOW_MASK;
  418. break;
  419. case IRQ_TYPE_EDGE_FALLING:
  420. val &= ~SIRFSOC_GPIO_CTL_INTR_HIGH_MASK;
  421. val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
  422. SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
  423. break;
  424. case IRQ_TYPE_EDGE_BOTH:
  425. val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK |
  426. SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
  427. SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
  428. break;
  429. case IRQ_TYPE_LEVEL_LOW:
  430. val &= ~(SIRFSOC_GPIO_CTL_INTR_HIGH_MASK |
  431. SIRFSOC_GPIO_CTL_INTR_TYPE_MASK);
  432. val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK;
  433. break;
  434. case IRQ_TYPE_LEVEL_HIGH:
  435. val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK;
  436. val &= ~(SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
  437. SIRFSOC_GPIO_CTL_INTR_TYPE_MASK);
  438. break;
  439. }
  440. writel(val, sgpio->chip.regs + offset);
  441. spin_unlock_irqrestore(&sgpio->lock, flags);
  442. return 0;
  443. }
  444. static struct irq_chip sirfsoc_irq_chip = {
  445. .name = "sirf-gpio-irq",
  446. .irq_ack = sirfsoc_gpio_irq_ack,
  447. .irq_mask = sirfsoc_gpio_irq_mask,
  448. .irq_unmask = sirfsoc_gpio_irq_unmask,
  449. .irq_set_type = sirfsoc_gpio_irq_type,
  450. };
  451. static void sirfsoc_gpio_handle_irq(struct irq_desc *desc)
  452. {
  453. unsigned int irq = irq_desc_get_irq(desc);
  454. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  455. struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc);
  456. struct sirfsoc_gpio_bank *bank;
  457. u32 status, ctrl;
  458. int idx = 0;
  459. struct irq_chip *chip = irq_desc_get_chip(desc);
  460. int i;
  461. for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
  462. bank = &sgpio->sgpio_bank[i];
  463. if (bank->parent_irq == irq)
  464. break;
  465. }
  466. BUG_ON(i == SIRFSOC_GPIO_NO_OF_BANKS);
  467. chained_irq_enter(chip, desc);
  468. status = readl(sgpio->chip.regs + SIRFSOC_GPIO_INT_STATUS(bank->id));
  469. if (!status) {
  470. printk(KERN_WARNING
  471. "%s: gpio id %d status %#x no interrupt is flagged\n",
  472. __func__, bank->id, status);
  473. handle_bad_irq(desc);
  474. return;
  475. }
  476. while (status) {
  477. ctrl = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, idx));
  478. /*
  479. * Here we must check whether the corresponding GPIO's interrupt
  480. * has been enabled, otherwise just skip it
  481. */
  482. if ((status & 0x1) && (ctrl & SIRFSOC_GPIO_CTL_INTR_EN_MASK)) {
  483. pr_debug("%s: gpio id %d idx %d happens\n",
  484. __func__, bank->id, idx);
  485. generic_handle_irq(irq_find_mapping(gc->irqdomain, idx +
  486. bank->id * SIRFSOC_GPIO_BANK_SIZE));
  487. }
  488. idx++;
  489. status = status >> 1;
  490. }
  491. chained_irq_exit(chip, desc);
  492. }
  493. static inline void sirfsoc_gpio_set_input(struct sirfsoc_gpio_chip *sgpio,
  494. unsigned ctrl_offset)
  495. {
  496. u32 val;
  497. val = readl(sgpio->chip.regs + ctrl_offset);
  498. val &= ~SIRFSOC_GPIO_CTL_OUT_EN_MASK;
  499. writel(val, sgpio->chip.regs + ctrl_offset);
  500. }
  501. static int sirfsoc_gpio_request(struct gpio_chip *chip, unsigned offset)
  502. {
  503. struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip);
  504. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
  505. unsigned long flags;
  506. if (pinctrl_request_gpio(chip->base + offset))
  507. return -ENODEV;
  508. spin_lock_irqsave(&bank->lock, flags);
  509. /*
  510. * default status:
  511. * set direction as input and mask irq
  512. */
  513. sirfsoc_gpio_set_input(sgpio, SIRFSOC_GPIO_CTRL(bank->id, offset));
  514. __sirfsoc_gpio_irq_mask(sgpio, bank, offset);
  515. spin_unlock_irqrestore(&bank->lock, flags);
  516. return 0;
  517. }
  518. static void sirfsoc_gpio_free(struct gpio_chip *chip, unsigned offset)
  519. {
  520. struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip);
  521. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
  522. unsigned long flags;
  523. spin_lock_irqsave(&bank->lock, flags);
  524. __sirfsoc_gpio_irq_mask(sgpio, bank, offset);
  525. sirfsoc_gpio_set_input(sgpio, SIRFSOC_GPIO_CTRL(bank->id, offset));
  526. spin_unlock_irqrestore(&bank->lock, flags);
  527. pinctrl_free_gpio(chip->base + offset);
  528. }
  529. static int sirfsoc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  530. {
  531. struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip);
  532. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, gpio);
  533. int idx = sirfsoc_gpio_to_bankoff(gpio);
  534. unsigned long flags;
  535. unsigned offset;
  536. offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
  537. spin_lock_irqsave(&bank->lock, flags);
  538. sirfsoc_gpio_set_input(sgpio, offset);
  539. spin_unlock_irqrestore(&bank->lock, flags);
  540. return 0;
  541. }
  542. static inline void sirfsoc_gpio_set_output(struct sirfsoc_gpio_chip *sgpio,
  543. struct sirfsoc_gpio_bank *bank,
  544. unsigned offset,
  545. int value)
  546. {
  547. u32 out_ctrl;
  548. unsigned long flags;
  549. spin_lock_irqsave(&bank->lock, flags);
  550. out_ctrl = readl(sgpio->chip.regs + offset);
  551. if (value)
  552. out_ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK;
  553. else
  554. out_ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK;
  555. out_ctrl &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK;
  556. out_ctrl |= SIRFSOC_GPIO_CTL_OUT_EN_MASK;
  557. writel(out_ctrl, sgpio->chip.regs + offset);
  558. spin_unlock_irqrestore(&bank->lock, flags);
  559. }
  560. static int sirfsoc_gpio_direction_output(struct gpio_chip *chip,
  561. unsigned gpio, int value)
  562. {
  563. struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip);
  564. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, gpio);
  565. int idx = sirfsoc_gpio_to_bankoff(gpio);
  566. u32 offset;
  567. unsigned long flags;
  568. offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
  569. spin_lock_irqsave(&sgpio->lock, flags);
  570. sirfsoc_gpio_set_output(sgpio, bank, offset, value);
  571. spin_unlock_irqrestore(&sgpio->lock, flags);
  572. return 0;
  573. }
  574. static int sirfsoc_gpio_get_value(struct gpio_chip *chip, unsigned offset)
  575. {
  576. struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip);
  577. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
  578. u32 val;
  579. unsigned long flags;
  580. spin_lock_irqsave(&bank->lock, flags);
  581. val = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
  582. spin_unlock_irqrestore(&bank->lock, flags);
  583. return !!(val & SIRFSOC_GPIO_CTL_DATAIN_MASK);
  584. }
  585. static void sirfsoc_gpio_set_value(struct gpio_chip *chip, unsigned offset,
  586. int value)
  587. {
  588. struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip);
  589. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
  590. u32 ctrl;
  591. unsigned long flags;
  592. spin_lock_irqsave(&bank->lock, flags);
  593. ctrl = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
  594. if (value)
  595. ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK;
  596. else
  597. ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK;
  598. writel(ctrl, sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
  599. spin_unlock_irqrestore(&bank->lock, flags);
  600. }
  601. static void sirfsoc_gpio_set_pullup(struct sirfsoc_gpio_chip *sgpio,
  602. const u32 *pullups)
  603. {
  604. int i, n;
  605. const unsigned long *p = (const unsigned long *)pullups;
  606. for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
  607. for_each_set_bit(n, p + i, BITS_PER_LONG) {
  608. u32 offset = SIRFSOC_GPIO_CTRL(i, n);
  609. u32 val = readl(sgpio->chip.regs + offset);
  610. val |= SIRFSOC_GPIO_CTL_PULL_MASK;
  611. val |= SIRFSOC_GPIO_CTL_PULL_HIGH;
  612. writel(val, sgpio->chip.regs + offset);
  613. }
  614. }
  615. }
  616. static void sirfsoc_gpio_set_pulldown(struct sirfsoc_gpio_chip *sgpio,
  617. const u32 *pulldowns)
  618. {
  619. int i, n;
  620. const unsigned long *p = (const unsigned long *)pulldowns;
  621. for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
  622. for_each_set_bit(n, p + i, BITS_PER_LONG) {
  623. u32 offset = SIRFSOC_GPIO_CTRL(i, n);
  624. u32 val = readl(sgpio->chip.regs + offset);
  625. val |= SIRFSOC_GPIO_CTL_PULL_MASK;
  626. val &= ~SIRFSOC_GPIO_CTL_PULL_HIGH;
  627. writel(val, sgpio->chip.regs + offset);
  628. }
  629. }
  630. }
  631. static int sirfsoc_gpio_probe(struct device_node *np)
  632. {
  633. int i, err = 0;
  634. static struct sirfsoc_gpio_chip *sgpio;
  635. struct sirfsoc_gpio_bank *bank;
  636. void __iomem *regs;
  637. struct platform_device *pdev;
  638. u32 pullups[SIRFSOC_GPIO_NO_OF_BANKS], pulldowns[SIRFSOC_GPIO_NO_OF_BANKS];
  639. pdev = of_find_device_by_node(np);
  640. if (!pdev)
  641. return -ENODEV;
  642. sgpio = devm_kzalloc(&pdev->dev, sizeof(*sgpio), GFP_KERNEL);
  643. if (!sgpio)
  644. return -ENOMEM;
  645. spin_lock_init(&sgpio->lock);
  646. regs = of_iomap(np, 0);
  647. if (!regs)
  648. return -ENOMEM;
  649. sgpio->chip.gc.request = sirfsoc_gpio_request;
  650. sgpio->chip.gc.free = sirfsoc_gpio_free;
  651. sgpio->chip.gc.direction_input = sirfsoc_gpio_direction_input;
  652. sgpio->chip.gc.get = sirfsoc_gpio_get_value;
  653. sgpio->chip.gc.direction_output = sirfsoc_gpio_direction_output;
  654. sgpio->chip.gc.set = sirfsoc_gpio_set_value;
  655. sgpio->chip.gc.base = 0;
  656. sgpio->chip.gc.ngpio = SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS;
  657. sgpio->chip.gc.label = kstrdup(np->full_name, GFP_KERNEL);
  658. sgpio->chip.gc.of_node = np;
  659. sgpio->chip.gc.of_xlate = sirfsoc_gpio_of_xlate;
  660. sgpio->chip.gc.of_gpio_n_cells = 2;
  661. sgpio->chip.gc.parent = &pdev->dev;
  662. sgpio->chip.regs = regs;
  663. err = gpiochip_add_data(&sgpio->chip.gc, sgpio);
  664. if (err) {
  665. dev_err(&pdev->dev, "%s: error in probe function with status %d\n",
  666. np->full_name, err);
  667. goto out;
  668. }
  669. err = gpiochip_irqchip_add(&sgpio->chip.gc,
  670. &sirfsoc_irq_chip,
  671. 0, handle_level_irq,
  672. IRQ_TYPE_NONE);
  673. if (err) {
  674. dev_err(&pdev->dev,
  675. "could not connect irqchip to gpiochip\n");
  676. goto out_banks;
  677. }
  678. for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
  679. bank = &sgpio->sgpio_bank[i];
  680. spin_lock_init(&bank->lock);
  681. bank->parent_irq = platform_get_irq(pdev, i);
  682. if (bank->parent_irq < 0) {
  683. err = bank->parent_irq;
  684. goto out_banks;
  685. }
  686. gpiochip_set_chained_irqchip(&sgpio->chip.gc,
  687. &sirfsoc_irq_chip,
  688. bank->parent_irq,
  689. sirfsoc_gpio_handle_irq);
  690. }
  691. err = gpiochip_add_pin_range(&sgpio->chip.gc, dev_name(&pdev->dev),
  692. 0, 0, SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS);
  693. if (err) {
  694. dev_err(&pdev->dev,
  695. "could not add gpiochip pin range\n");
  696. goto out_no_range;
  697. }
  698. if (!of_property_read_u32_array(np, "sirf,pullups", pullups,
  699. SIRFSOC_GPIO_NO_OF_BANKS))
  700. sirfsoc_gpio_set_pullup(sgpio, pullups);
  701. if (!of_property_read_u32_array(np, "sirf,pulldowns", pulldowns,
  702. SIRFSOC_GPIO_NO_OF_BANKS))
  703. sirfsoc_gpio_set_pulldown(sgpio, pulldowns);
  704. return 0;
  705. out_no_range:
  706. out_banks:
  707. gpiochip_remove(&sgpio->chip.gc);
  708. out:
  709. iounmap(regs);
  710. return err;
  711. }
  712. static int __init sirfsoc_gpio_init(void)
  713. {
  714. struct device_node *np;
  715. np = of_find_matching_node(NULL, pinmux_ids);
  716. if (!np)
  717. return -ENODEV;
  718. return sirfsoc_gpio_probe(np);
  719. }
  720. subsys_initcall(sirfsoc_gpio_init);