pinctrl-sirf.c 23 KB

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