pinctrl-sirf.c 24 KB

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