pinctrl-sirf.c 23 KB

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