pinctrl-sirf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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,
  52. unsigned selector,
  53. const unsigned **pins,
  54. unsigned *num_pins)
  55. {
  56. *pins = sirfsoc_pin_groups[selector].pins;
  57. *num_pins = sirfsoc_pin_groups[selector].num_pins;
  58. return 0;
  59. }
  60. static void sirfsoc_pin_dbg_show(struct pinctrl_dev *pctldev,
  61. struct seq_file *s, unsigned offset)
  62. {
  63. seq_printf(s, " " DRIVER_NAME);
  64. }
  65. static int sirfsoc_dt_node_to_map(struct pinctrl_dev *pctldev,
  66. struct device_node *np_config,
  67. struct pinctrl_map **map, unsigned *num_maps)
  68. {
  69. struct sirfsoc_pmx *spmx = pinctrl_dev_get_drvdata(pctldev);
  70. struct device_node *np;
  71. struct property *prop;
  72. const char *function, *group;
  73. int ret, index = 0, count = 0;
  74. /* calculate number of maps required */
  75. for_each_child_of_node(np_config, np) {
  76. ret = of_property_read_string(np, "sirf,function", &function);
  77. if (ret < 0)
  78. return ret;
  79. ret = of_property_count_strings(np, "sirf,pins");
  80. if (ret < 0)
  81. return ret;
  82. count += ret;
  83. }
  84. if (!count) {
  85. dev_err(spmx->dev, "No child nodes passed via DT\n");
  86. return -ENODEV;
  87. }
  88. *map = kzalloc(sizeof(**map) * count, GFP_KERNEL);
  89. if (!*map)
  90. return -ENOMEM;
  91. for_each_child_of_node(np_config, np) {
  92. of_property_read_string(np, "sirf,function", &function);
  93. of_property_for_each_string(np, "sirf,pins", prop, group) {
  94. (*map)[index].type = PIN_MAP_TYPE_MUX_GROUP;
  95. (*map)[index].data.mux.group = group;
  96. (*map)[index].data.mux.function = function;
  97. index++;
  98. }
  99. }
  100. *num_maps = count;
  101. return 0;
  102. }
  103. static void sirfsoc_dt_free_map(struct pinctrl_dev *pctldev,
  104. struct pinctrl_map *map, unsigned num_maps)
  105. {
  106. kfree(map);
  107. }
  108. static struct pinctrl_ops sirfsoc_pctrl_ops = {
  109. .get_groups_count = sirfsoc_get_groups_count,
  110. .get_group_name = sirfsoc_get_group_name,
  111. .get_group_pins = sirfsoc_get_group_pins,
  112. .pin_dbg_show = sirfsoc_pin_dbg_show,
  113. .dt_node_to_map = sirfsoc_dt_node_to_map,
  114. .dt_free_map = sirfsoc_dt_free_map,
  115. };
  116. static struct sirfsoc_pmx_func *sirfsoc_pmx_functions;
  117. static int sirfsoc_pmxfunc_cnt;
  118. static void sirfsoc_pinmux_endisable(struct sirfsoc_pmx *spmx,
  119. unsigned selector, bool enable)
  120. {
  121. int i;
  122. const struct sirfsoc_padmux *mux =
  123. sirfsoc_pmx_functions[selector].padmux;
  124. const struct sirfsoc_muxmask *mask = mux->muxmask;
  125. for (i = 0; i < mux->muxmask_counts; i++) {
  126. u32 muxval;
  127. if (!spmx->is_marco) {
  128. muxval = readl(spmx->gpio_virtbase +
  129. SIRFSOC_GPIO_PAD_EN(mask[i].group));
  130. if (enable)
  131. muxval = muxval & ~mask[i].mask;
  132. else
  133. muxval = muxval | mask[i].mask;
  134. writel(muxval, spmx->gpio_virtbase +
  135. SIRFSOC_GPIO_PAD_EN(mask[i].group));
  136. } else {
  137. if (enable)
  138. writel(mask[i].mask, spmx->gpio_virtbase +
  139. SIRFSOC_GPIO_PAD_EN_CLR(mask[i].group));
  140. else
  141. writel(mask[i].mask, spmx->gpio_virtbase +
  142. SIRFSOC_GPIO_PAD_EN(mask[i].group));
  143. }
  144. }
  145. if (mux->funcmask && enable) {
  146. u32 func_en_val;
  147. func_en_val =
  148. readl(spmx->rsc_virtbase + mux->ctrlreg);
  149. func_en_val =
  150. (func_en_val & ~mux->funcmask) | (mux->funcval);
  151. writel(func_en_val, spmx->rsc_virtbase + mux->ctrlreg);
  152. }
  153. }
  154. static int sirfsoc_pinmux_set_mux(struct pinctrl_dev *pmxdev,
  155. unsigned selector,
  156. unsigned group)
  157. {
  158. struct sirfsoc_pmx *spmx;
  159. spmx = pinctrl_dev_get_drvdata(pmxdev);
  160. sirfsoc_pinmux_endisable(spmx, selector, true);
  161. return 0;
  162. }
  163. static int sirfsoc_pinmux_get_funcs_count(struct pinctrl_dev *pmxdev)
  164. {
  165. return sirfsoc_pmxfunc_cnt;
  166. }
  167. static const char *sirfsoc_pinmux_get_func_name(struct pinctrl_dev *pctldev,
  168. unsigned selector)
  169. {
  170. return sirfsoc_pmx_functions[selector].name;
  171. }
  172. static int sirfsoc_pinmux_get_groups(struct pinctrl_dev *pctldev,
  173. unsigned selector,
  174. const char * const **groups,
  175. unsigned * const num_groups)
  176. {
  177. *groups = sirfsoc_pmx_functions[selector].groups;
  178. *num_groups = sirfsoc_pmx_functions[selector].num_groups;
  179. return 0;
  180. }
  181. static int sirfsoc_pinmux_request_gpio(struct pinctrl_dev *pmxdev,
  182. struct pinctrl_gpio_range *range, unsigned offset)
  183. {
  184. struct sirfsoc_pmx *spmx;
  185. int group = range->id;
  186. u32 muxval;
  187. spmx = pinctrl_dev_get_drvdata(pmxdev);
  188. if (!spmx->is_marco) {
  189. muxval = readl(spmx->gpio_virtbase +
  190. SIRFSOC_GPIO_PAD_EN(group));
  191. muxval = muxval | (1 << (offset - range->pin_base));
  192. writel(muxval, spmx->gpio_virtbase +
  193. SIRFSOC_GPIO_PAD_EN(group));
  194. } else {
  195. writel(1 << (offset - range->pin_base), spmx->gpio_virtbase +
  196. SIRFSOC_GPIO_PAD_EN(group));
  197. }
  198. return 0;
  199. }
  200. static struct pinmux_ops sirfsoc_pinmux_ops = {
  201. .set_mux = sirfsoc_pinmux_set_mux,
  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. .of_match_table = pinmux_ids,
  339. #ifdef CONFIG_PM_SLEEP
  340. .pm = &sirfsoc_pinmux_pm_ops,
  341. #endif
  342. },
  343. .probe = sirfsoc_pinmux_probe,
  344. };
  345. static int __init sirfsoc_pinmux_init(void)
  346. {
  347. return platform_driver_register(&sirfsoc_pinmux_driver);
  348. }
  349. arch_initcall(sirfsoc_pinmux_init);
  350. static inline struct sirfsoc_gpio_chip *to_sirfsoc_gpio(struct gpio_chip *gc)
  351. {
  352. return container_of(gc, struct sirfsoc_gpio_chip, chip.gc);
  353. }
  354. static inline struct sirfsoc_gpio_bank *
  355. sirfsoc_gpio_to_bank(struct sirfsoc_gpio_chip *sgpio, unsigned int offset)
  356. {
  357. return &sgpio->sgpio_bank[offset / SIRFSOC_GPIO_BANK_SIZE];
  358. }
  359. static inline int sirfsoc_gpio_to_bankoff(unsigned int offset)
  360. {
  361. return offset % SIRFSOC_GPIO_BANK_SIZE;
  362. }
  363. static void sirfsoc_gpio_irq_ack(struct irq_data *d)
  364. {
  365. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  366. struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
  367. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
  368. int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
  369. u32 val, offset;
  370. unsigned long flags;
  371. offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
  372. spin_lock_irqsave(&sgpio_lock, flags);
  373. val = readl(sgpio->chip.regs + offset);
  374. writel(val, sgpio->chip.regs + offset);
  375. spin_unlock_irqrestore(&sgpio_lock, flags);
  376. }
  377. static void __sirfsoc_gpio_irq_mask(struct sirfsoc_gpio_chip *sgpio,
  378. struct sirfsoc_gpio_bank *bank,
  379. int idx)
  380. {
  381. u32 val, offset;
  382. unsigned long flags;
  383. offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
  384. spin_lock_irqsave(&sgpio_lock, flags);
  385. val = readl(sgpio->chip.regs + offset);
  386. val &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK;
  387. val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK;
  388. writel(val, sgpio->chip.regs + offset);
  389. spin_unlock_irqrestore(&sgpio_lock, flags);
  390. }
  391. static void sirfsoc_gpio_irq_mask(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. __sirfsoc_gpio_irq_mask(sgpio, bank, d->hwirq % SIRFSOC_GPIO_BANK_SIZE);
  397. }
  398. static void sirfsoc_gpio_irq_unmask(struct irq_data *d)
  399. {
  400. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  401. struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
  402. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
  403. int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
  404. u32 val, offset;
  405. unsigned long flags;
  406. offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
  407. spin_lock_irqsave(&sgpio_lock, flags);
  408. val = readl(sgpio->chip.regs + offset);
  409. val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK;
  410. val |= SIRFSOC_GPIO_CTL_INTR_EN_MASK;
  411. writel(val, sgpio->chip.regs + offset);
  412. spin_unlock_irqrestore(&sgpio_lock, flags);
  413. }
  414. static int sirfsoc_gpio_irq_type(struct irq_data *d, unsigned type)
  415. {
  416. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  417. struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
  418. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
  419. int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
  420. u32 val, offset;
  421. unsigned long flags;
  422. offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
  423. spin_lock_irqsave(&sgpio_lock, flags);
  424. val = readl(sgpio->chip.regs + offset);
  425. val &= ~(SIRFSOC_GPIO_CTL_INTR_STS_MASK | SIRFSOC_GPIO_CTL_OUT_EN_MASK);
  426. switch (type) {
  427. case IRQ_TYPE_NONE:
  428. break;
  429. case IRQ_TYPE_EDGE_RISING:
  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_EDGE_FALLING:
  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. case IRQ_TYPE_EDGE_BOTH:
  440. val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK |
  441. SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
  442. SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
  443. break;
  444. case IRQ_TYPE_LEVEL_LOW:
  445. val &= ~(SIRFSOC_GPIO_CTL_INTR_HIGH_MASK |
  446. SIRFSOC_GPIO_CTL_INTR_TYPE_MASK);
  447. val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK;
  448. break;
  449. case IRQ_TYPE_LEVEL_HIGH:
  450. val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK;
  451. val &= ~(SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
  452. SIRFSOC_GPIO_CTL_INTR_TYPE_MASK);
  453. break;
  454. }
  455. writel(val, sgpio->chip.regs + offset);
  456. spin_unlock_irqrestore(&sgpio_lock, flags);
  457. return 0;
  458. }
  459. static struct irq_chip sirfsoc_irq_chip = {
  460. .name = "sirf-gpio-irq",
  461. .irq_ack = sirfsoc_gpio_irq_ack,
  462. .irq_mask = sirfsoc_gpio_irq_mask,
  463. .irq_unmask = sirfsoc_gpio_irq_unmask,
  464. .irq_set_type = sirfsoc_gpio_irq_type,
  465. };
  466. static void sirfsoc_gpio_handle_irq(unsigned int irq, struct irq_desc *desc)
  467. {
  468. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  469. struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
  470. struct sirfsoc_gpio_bank *bank;
  471. u32 status, ctrl;
  472. int idx = 0;
  473. struct irq_chip *chip = irq_get_chip(irq);
  474. int i;
  475. for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
  476. bank = &sgpio->sgpio_bank[i];
  477. if (bank->parent_irq == irq)
  478. break;
  479. }
  480. BUG_ON(i == SIRFSOC_GPIO_NO_OF_BANKS);
  481. chained_irq_enter(chip, desc);
  482. status = readl(sgpio->chip.regs + SIRFSOC_GPIO_INT_STATUS(bank->id));
  483. if (!status) {
  484. printk(KERN_WARNING
  485. "%s: gpio id %d status %#x no interrupt is flaged\n",
  486. __func__, bank->id, status);
  487. handle_bad_irq(irq, desc);
  488. return;
  489. }
  490. while (status) {
  491. ctrl = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, idx));
  492. /*
  493. * Here we must check whether the corresponding GPIO's interrupt
  494. * has been enabled, otherwise just skip it
  495. */
  496. if ((status & 0x1) && (ctrl & SIRFSOC_GPIO_CTL_INTR_EN_MASK)) {
  497. pr_debug("%s: gpio id %d idx %d happens\n",
  498. __func__, bank->id, idx);
  499. generic_handle_irq(irq_find_mapping(gc->irqdomain, idx +
  500. bank->id * SIRFSOC_GPIO_BANK_SIZE));
  501. }
  502. idx++;
  503. status = status >> 1;
  504. }
  505. chained_irq_exit(chip, desc);
  506. }
  507. static inline void sirfsoc_gpio_set_input(struct sirfsoc_gpio_chip *sgpio,
  508. unsigned ctrl_offset)
  509. {
  510. u32 val;
  511. val = readl(sgpio->chip.regs + ctrl_offset);
  512. val &= ~SIRFSOC_GPIO_CTL_OUT_EN_MASK;
  513. writel(val, sgpio->chip.regs + ctrl_offset);
  514. }
  515. static int sirfsoc_gpio_request(struct gpio_chip *chip, unsigned offset)
  516. {
  517. struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
  518. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
  519. unsigned long flags;
  520. if (pinctrl_request_gpio(chip->base + offset))
  521. return -ENODEV;
  522. spin_lock_irqsave(&bank->lock, flags);
  523. /*
  524. * default status:
  525. * set direction as input and mask irq
  526. */
  527. sirfsoc_gpio_set_input(sgpio, SIRFSOC_GPIO_CTRL(bank->id, offset));
  528. __sirfsoc_gpio_irq_mask(sgpio, bank, offset);
  529. spin_unlock_irqrestore(&bank->lock, flags);
  530. return 0;
  531. }
  532. static void sirfsoc_gpio_free(struct gpio_chip *chip, unsigned offset)
  533. {
  534. struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
  535. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
  536. unsigned long flags;
  537. spin_lock_irqsave(&bank->lock, flags);
  538. __sirfsoc_gpio_irq_mask(sgpio, bank, offset);
  539. sirfsoc_gpio_set_input(sgpio, SIRFSOC_GPIO_CTRL(bank->id, offset));
  540. spin_unlock_irqrestore(&bank->lock, flags);
  541. pinctrl_free_gpio(chip->base + offset);
  542. }
  543. static int sirfsoc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  544. {
  545. struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
  546. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, gpio);
  547. int idx = sirfsoc_gpio_to_bankoff(gpio);
  548. unsigned long flags;
  549. unsigned offset;
  550. offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
  551. spin_lock_irqsave(&bank->lock, flags);
  552. sirfsoc_gpio_set_input(sgpio, offset);
  553. spin_unlock_irqrestore(&bank->lock, flags);
  554. return 0;
  555. }
  556. static inline void sirfsoc_gpio_set_output(struct sirfsoc_gpio_chip *sgpio,
  557. struct sirfsoc_gpio_bank *bank,
  558. unsigned offset,
  559. int value)
  560. {
  561. u32 out_ctrl;
  562. unsigned long flags;
  563. spin_lock_irqsave(&bank->lock, flags);
  564. out_ctrl = readl(sgpio->chip.regs + offset);
  565. if (value)
  566. out_ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK;
  567. else
  568. out_ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK;
  569. out_ctrl &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK;
  570. out_ctrl |= SIRFSOC_GPIO_CTL_OUT_EN_MASK;
  571. writel(out_ctrl, sgpio->chip.regs + offset);
  572. spin_unlock_irqrestore(&bank->lock, flags);
  573. }
  574. static int sirfsoc_gpio_direction_output(struct gpio_chip *chip,
  575. unsigned gpio, int value)
  576. {
  577. struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
  578. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, gpio);
  579. int idx = sirfsoc_gpio_to_bankoff(gpio);
  580. u32 offset;
  581. unsigned long flags;
  582. offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
  583. spin_lock_irqsave(&sgpio_lock, flags);
  584. sirfsoc_gpio_set_output(sgpio, bank, offset, value);
  585. spin_unlock_irqrestore(&sgpio_lock, flags);
  586. return 0;
  587. }
  588. static int sirfsoc_gpio_get_value(struct gpio_chip *chip, unsigned offset)
  589. {
  590. struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
  591. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
  592. u32 val;
  593. unsigned long flags;
  594. spin_lock_irqsave(&bank->lock, flags);
  595. val = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
  596. spin_unlock_irqrestore(&bank->lock, flags);
  597. return !!(val & SIRFSOC_GPIO_CTL_DATAIN_MASK);
  598. }
  599. static void sirfsoc_gpio_set_value(struct gpio_chip *chip, unsigned offset,
  600. int value)
  601. {
  602. struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
  603. struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
  604. u32 ctrl;
  605. unsigned long flags;
  606. spin_lock_irqsave(&bank->lock, flags);
  607. ctrl = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
  608. if (value)
  609. ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK;
  610. else
  611. ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK;
  612. writel(ctrl, sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
  613. spin_unlock_irqrestore(&bank->lock, flags);
  614. }
  615. static void sirfsoc_gpio_set_pullup(struct sirfsoc_gpio_chip *sgpio,
  616. const u32 *pullups)
  617. {
  618. int i, n;
  619. const unsigned long *p = (const unsigned long *)pullups;
  620. for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
  621. for_each_set_bit(n, p + i, BITS_PER_LONG) {
  622. u32 offset = SIRFSOC_GPIO_CTRL(i, n);
  623. u32 val = readl(sgpio->chip.regs + offset);
  624. val |= SIRFSOC_GPIO_CTL_PULL_MASK;
  625. val |= SIRFSOC_GPIO_CTL_PULL_HIGH;
  626. writel(val, sgpio->chip.regs + offset);
  627. }
  628. }
  629. }
  630. static void sirfsoc_gpio_set_pulldown(struct sirfsoc_gpio_chip *sgpio,
  631. const u32 *pulldowns)
  632. {
  633. int i, n;
  634. const unsigned long *p = (const unsigned long *)pulldowns;
  635. for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
  636. for_each_set_bit(n, p + i, BITS_PER_LONG) {
  637. u32 offset = SIRFSOC_GPIO_CTRL(i, n);
  638. u32 val = readl(sgpio->chip.regs + offset);
  639. val |= SIRFSOC_GPIO_CTL_PULL_MASK;
  640. val &= ~SIRFSOC_GPIO_CTL_PULL_HIGH;
  641. writel(val, sgpio->chip.regs + offset);
  642. }
  643. }
  644. }
  645. static int sirfsoc_gpio_probe(struct device_node *np)
  646. {
  647. int i, err = 0;
  648. static struct sirfsoc_gpio_chip *sgpio;
  649. struct sirfsoc_gpio_bank *bank;
  650. void __iomem *regs;
  651. struct platform_device *pdev;
  652. bool is_marco = false;
  653. u32 pullups[SIRFSOC_GPIO_NO_OF_BANKS], pulldowns[SIRFSOC_GPIO_NO_OF_BANKS];
  654. pdev = of_find_device_by_node(np);
  655. if (!pdev)
  656. return -ENODEV;
  657. sgpio = devm_kzalloc(&pdev->dev, sizeof(*sgpio), GFP_KERNEL);
  658. if (!sgpio)
  659. return -ENOMEM;
  660. regs = of_iomap(np, 0);
  661. if (!regs)
  662. return -ENOMEM;
  663. if (of_device_is_compatible(np, "sirf,marco-pinctrl"))
  664. is_marco = 1;
  665. sgpio->chip.gc.request = sirfsoc_gpio_request;
  666. sgpio->chip.gc.free = sirfsoc_gpio_free;
  667. sgpio->chip.gc.direction_input = sirfsoc_gpio_direction_input;
  668. sgpio->chip.gc.get = sirfsoc_gpio_get_value;
  669. sgpio->chip.gc.direction_output = sirfsoc_gpio_direction_output;
  670. sgpio->chip.gc.set = sirfsoc_gpio_set_value;
  671. sgpio->chip.gc.base = 0;
  672. sgpio->chip.gc.ngpio = SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS;
  673. sgpio->chip.gc.label = kstrdup(np->full_name, GFP_KERNEL);
  674. sgpio->chip.gc.of_node = np;
  675. sgpio->chip.gc.of_xlate = sirfsoc_gpio_of_xlate;
  676. sgpio->chip.gc.of_gpio_n_cells = 2;
  677. sgpio->chip.gc.dev = &pdev->dev;
  678. sgpio->chip.regs = regs;
  679. sgpio->is_marco = is_marco;
  680. err = gpiochip_add(&sgpio->chip.gc);
  681. if (err) {
  682. dev_err(&pdev->dev, "%s: error in probe function with status %d\n",
  683. np->full_name, err);
  684. goto out;
  685. }
  686. err = gpiochip_irqchip_add(&sgpio->chip.gc,
  687. &sirfsoc_irq_chip,
  688. 0, handle_level_irq,
  689. IRQ_TYPE_NONE);
  690. if (err) {
  691. dev_err(&pdev->dev,
  692. "could not connect irqchip to gpiochip\n");
  693. goto out_banks;
  694. }
  695. for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
  696. bank = &sgpio->sgpio_bank[i];
  697. spin_lock_init(&bank->lock);
  698. bank->parent_irq = platform_get_irq(pdev, i);
  699. if (bank->parent_irq < 0) {
  700. err = bank->parent_irq;
  701. goto out_banks;
  702. }
  703. gpiochip_set_chained_irqchip(&sgpio->chip.gc,
  704. &sirfsoc_irq_chip,
  705. bank->parent_irq,
  706. sirfsoc_gpio_handle_irq);
  707. }
  708. err = gpiochip_add_pin_range(&sgpio->chip.gc, dev_name(&pdev->dev),
  709. 0, 0, SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS);
  710. if (err) {
  711. dev_err(&pdev->dev,
  712. "could not add gpiochip pin range\n");
  713. goto out_no_range;
  714. }
  715. if (!of_property_read_u32_array(np, "sirf,pullups", pullups,
  716. SIRFSOC_GPIO_NO_OF_BANKS))
  717. sirfsoc_gpio_set_pullup(sgpio, pullups);
  718. if (!of_property_read_u32_array(np, "sirf,pulldowns", pulldowns,
  719. SIRFSOC_GPIO_NO_OF_BANKS))
  720. sirfsoc_gpio_set_pulldown(sgpio, pulldowns);
  721. return 0;
  722. out_no_range:
  723. out_banks:
  724. gpiochip_remove(&sgpio->chip.gc);
  725. out:
  726. iounmap(regs);
  727. return err;
  728. }
  729. static int __init sirfsoc_gpio_init(void)
  730. {
  731. struct device_node *np;
  732. np = of_find_matching_node(NULL, pinmux_ids);
  733. if (!np)
  734. return -ENODEV;
  735. return sirfsoc_gpio_probe(np);
  736. }
  737. subsys_initcall(sirfsoc_gpio_init);
  738. MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>");
  739. MODULE_AUTHOR("Yuping Luo <yuping.luo@csr.com>");
  740. MODULE_AUTHOR("Barry Song <baohua.song@csr.com>");
  741. MODULE_DESCRIPTION("SIRFSOC pin control driver");
  742. MODULE_LICENSE("GPL");