blob: 014f5b1fee551f9ba586fe68908049236c97e0c6 [file] [log] [blame]
Barry Song3370dc92013-05-14 22:17:58 +08001/*
2 * pinmux driver for CSR SiRFprimaII
3 *
Barry Song019c12f2014-02-12 21:54:47 +08004 * Copyright (c) 2011 - 2014 Cambridge Silicon Radio Limited, a CSR plc group
5 * company.
Barry Song3370dc92013-05-14 22:17:58 +08006 *
7 * Licensed under GPLv2 or later.
8 */
9
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/irq.h>
13#include <linux/platform_device.h>
14#include <linux/io.h>
15#include <linux/slab.h>
16#include <linux/err.h>
Barry Song3370dc92013-05-14 22:17:58 +080017#include <linux/pinctrl/pinctrl.h>
18#include <linux/pinctrl/pinmux.h>
19#include <linux/pinctrl/consumer.h>
20#include <linux/pinctrl/machine.h>
21#include <linux/of.h>
22#include <linux/of_address.h>
23#include <linux/of_device.h>
24#include <linux/of_platform.h>
25#include <linux/bitops.h>
26#include <linux/gpio.h>
27#include <linux/of_gpio.h>
Barry Song3370dc92013-05-14 22:17:58 +080028
29#include "pinctrl-sirf.h"
30
31#define DRIVER_NAME "pinmux-sirf"
32
33struct sirfsoc_gpio_bank {
Barry Song3370dc92013-05-14 22:17:58 +080034 int id;
35 int parent_irq;
36 spinlock_t lock;
Barry Song3370dc92013-05-14 22:17:58 +080037};
38
Barry Songc5eb7572014-04-15 14:43:46 +080039struct sirfsoc_gpio_chip {
40 struct of_mm_gpio_chip chip;
Barry Songc5eb7572014-04-15 14:43:46 +080041 bool is_marco; /* for marco, some registers are different with prima2 */
42 struct sirfsoc_gpio_bank sgpio_bank[SIRFSOC_GPIO_NO_OF_BANKS];
43};
44
Barry Song3370dc92013-05-14 22:17:58 +080045static DEFINE_SPINLOCK(sgpio_lock);
46
47static struct sirfsoc_pin_group *sirfsoc_pin_groups;
48static int sirfsoc_pingrp_cnt;
49
50static int sirfsoc_get_groups_count(struct pinctrl_dev *pctldev)
51{
52 return sirfsoc_pingrp_cnt;
53}
54
55static const char *sirfsoc_get_group_name(struct pinctrl_dev *pctldev,
56 unsigned selector)
57{
58 return sirfsoc_pin_groups[selector].name;
59}
60
61static int sirfsoc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
62 const unsigned **pins,
63 unsigned *num_pins)
64{
65 *pins = sirfsoc_pin_groups[selector].pins;
66 *num_pins = sirfsoc_pin_groups[selector].num_pins;
67 return 0;
68}
69
70static void sirfsoc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
71 unsigned offset)
72{
73 seq_printf(s, " " DRIVER_NAME);
74}
75
76static int sirfsoc_dt_node_to_map(struct pinctrl_dev *pctldev,
77 struct device_node *np_config,
78 struct pinctrl_map **map, unsigned *num_maps)
79{
80 struct sirfsoc_pmx *spmx = pinctrl_dev_get_drvdata(pctldev);
81 struct device_node *np;
82 struct property *prop;
83 const char *function, *group;
84 int ret, index = 0, count = 0;
85
86 /* calculate number of maps required */
87 for_each_child_of_node(np_config, np) {
88 ret = of_property_read_string(np, "sirf,function", &function);
89 if (ret < 0)
90 return ret;
91
92 ret = of_property_count_strings(np, "sirf,pins");
93 if (ret < 0)
94 return ret;
95
96 count += ret;
97 }
98
99 if (!count) {
100 dev_err(spmx->dev, "No child nodes passed via DT\n");
101 return -ENODEV;
102 }
103
104 *map = kzalloc(sizeof(**map) * count, GFP_KERNEL);
105 if (!*map)
106 return -ENOMEM;
107
108 for_each_child_of_node(np_config, np) {
109 of_property_read_string(np, "sirf,function", &function);
110 of_property_for_each_string(np, "sirf,pins", prop, group) {
111 (*map)[index].type = PIN_MAP_TYPE_MUX_GROUP;
112 (*map)[index].data.mux.group = group;
113 (*map)[index].data.mux.function = function;
114 index++;
115 }
116 }
117
118 *num_maps = count;
119
120 return 0;
121}
122
123static void sirfsoc_dt_free_map(struct pinctrl_dev *pctldev,
124 struct pinctrl_map *map, unsigned num_maps)
125{
126 kfree(map);
127}
128
129static struct pinctrl_ops sirfsoc_pctrl_ops = {
130 .get_groups_count = sirfsoc_get_groups_count,
131 .get_group_name = sirfsoc_get_group_name,
132 .get_group_pins = sirfsoc_get_group_pins,
133 .pin_dbg_show = sirfsoc_pin_dbg_show,
134 .dt_node_to_map = sirfsoc_dt_node_to_map,
135 .dt_free_map = sirfsoc_dt_free_map,
136};
137
138static struct sirfsoc_pmx_func *sirfsoc_pmx_functions;
139static int sirfsoc_pmxfunc_cnt;
140
141static void sirfsoc_pinmux_endisable(struct sirfsoc_pmx *spmx, unsigned selector,
142 bool enable)
143{
144 int i;
145 const struct sirfsoc_padmux *mux = sirfsoc_pmx_functions[selector].padmux;
146 const struct sirfsoc_muxmask *mask = mux->muxmask;
147
148 for (i = 0; i < mux->muxmask_counts; i++) {
149 u32 muxval;
150 if (!spmx->is_marco) {
151 muxval = readl(spmx->gpio_virtbase + SIRFSOC_GPIO_PAD_EN(mask[i].group));
152 if (enable)
153 muxval = muxval & ~mask[i].mask;
154 else
155 muxval = muxval | mask[i].mask;
156 writel(muxval, spmx->gpio_virtbase + SIRFSOC_GPIO_PAD_EN(mask[i].group));
157 } else {
158 if (enable)
159 writel(mask[i].mask, spmx->gpio_virtbase +
160 SIRFSOC_GPIO_PAD_EN_CLR(mask[i].group));
161 else
162 writel(mask[i].mask, spmx->gpio_virtbase +
163 SIRFSOC_GPIO_PAD_EN(mask[i].group));
164 }
165 }
166
167 if (mux->funcmask && enable) {
168 u32 func_en_val;
Rong Wang6a08a922013-09-29 22:27:59 +0800169
Barry Song3370dc92013-05-14 22:17:58 +0800170 func_en_val =
Rong Wang6a08a922013-09-29 22:27:59 +0800171 readl(spmx->rsc_virtbase + mux->ctrlreg);
Barry Song3370dc92013-05-14 22:17:58 +0800172 func_en_val =
Rong Wang6a08a922013-09-29 22:27:59 +0800173 (func_en_val & ~mux->funcmask) | (mux->funcval);
174 writel(func_en_val, spmx->rsc_virtbase + mux->ctrlreg);
Barry Song3370dc92013-05-14 22:17:58 +0800175 }
176}
177
178static int sirfsoc_pinmux_enable(struct pinctrl_dev *pmxdev, unsigned selector,
179 unsigned group)
180{
181 struct sirfsoc_pmx *spmx;
182
183 spmx = pinctrl_dev_get_drvdata(pmxdev);
184 sirfsoc_pinmux_endisable(spmx, selector, true);
185
186 return 0;
187}
188
189static void sirfsoc_pinmux_disable(struct pinctrl_dev *pmxdev, unsigned selector,
190 unsigned group)
191{
192 struct sirfsoc_pmx *spmx;
193
194 spmx = pinctrl_dev_get_drvdata(pmxdev);
195 sirfsoc_pinmux_endisable(spmx, selector, false);
196}
197
198static int sirfsoc_pinmux_get_funcs_count(struct pinctrl_dev *pmxdev)
199{
200 return sirfsoc_pmxfunc_cnt;
201}
202
203static const char *sirfsoc_pinmux_get_func_name(struct pinctrl_dev *pctldev,
204 unsigned selector)
205{
206 return sirfsoc_pmx_functions[selector].name;
207}
208
209static int sirfsoc_pinmux_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
210 const char * const **groups,
211 unsigned * const num_groups)
212{
213 *groups = sirfsoc_pmx_functions[selector].groups;
214 *num_groups = sirfsoc_pmx_functions[selector].num_groups;
215 return 0;
216}
217
218static int sirfsoc_pinmux_request_gpio(struct pinctrl_dev *pmxdev,
219 struct pinctrl_gpio_range *range, unsigned offset)
220{
221 struct sirfsoc_pmx *spmx;
222
223 int group = range->id;
224
225 u32 muxval;
226
227 spmx = pinctrl_dev_get_drvdata(pmxdev);
228
229 if (!spmx->is_marco) {
230 muxval = readl(spmx->gpio_virtbase + SIRFSOC_GPIO_PAD_EN(group));
231 muxval = muxval | (1 << (offset - range->pin_base));
232 writel(muxval, spmx->gpio_virtbase + SIRFSOC_GPIO_PAD_EN(group));
233 } else {
234 writel(1 << (offset - range->pin_base), spmx->gpio_virtbase +
235 SIRFSOC_GPIO_PAD_EN(group));
236 }
237
238 return 0;
239}
240
241static struct pinmux_ops sirfsoc_pinmux_ops = {
242 .enable = sirfsoc_pinmux_enable,
243 .disable = sirfsoc_pinmux_disable,
244 .get_functions_count = sirfsoc_pinmux_get_funcs_count,
245 .get_function_name = sirfsoc_pinmux_get_func_name,
246 .get_function_groups = sirfsoc_pinmux_get_groups,
247 .gpio_request_enable = sirfsoc_pinmux_request_gpio,
248};
249
250static struct pinctrl_desc sirfsoc_pinmux_desc = {
251 .name = DRIVER_NAME,
252 .pctlops = &sirfsoc_pctrl_ops,
253 .pmxops = &sirfsoc_pinmux_ops,
254 .owner = THIS_MODULE,
255};
256
Barry Song3370dc92013-05-14 22:17:58 +0800257static void __iomem *sirfsoc_rsc_of_iomap(void)
258{
259 const struct of_device_id rsc_ids[] = {
260 { .compatible = "sirf,prima2-rsc" },
261 { .compatible = "sirf,marco-rsc" },
262 {}
263 };
264 struct device_node *np;
265
266 np = of_find_matching_node(NULL, rsc_ids);
267 if (!np)
268 panic("unable to find compatible rsc node in dtb\n");
269
270 return of_iomap(np, 0);
271}
272
273static int sirfsoc_gpio_of_xlate(struct gpio_chip *gc,
Barry Songc5eb7572014-04-15 14:43:46 +0800274 const struct of_phandle_args *gpiospec,
275 u32 *flags)
Barry Song3370dc92013-05-14 22:17:58 +0800276{
Barry Songc5eb7572014-04-15 14:43:46 +0800277 if (gpiospec->args[0] > SIRFSOC_GPIO_NO_OF_BANKS * SIRFSOC_GPIO_BANK_SIZE)
Rongjun Ying9c956902013-07-04 15:55:28 +0800278 return -EINVAL;
Barry Song3370dc92013-05-14 22:17:58 +0800279
Barry Songc5eb7572014-04-15 14:43:46 +0800280 if (flags)
Rongjun Ying9c956902013-07-04 15:55:28 +0800281 *flags = gpiospec->args[1];
Barry Song3370dc92013-05-14 22:17:58 +0800282
Barry Songc5eb7572014-04-15 14:43:46 +0800283 return gpiospec->args[0];
Barry Song3370dc92013-05-14 22:17:58 +0800284}
285
286static const struct of_device_id pinmux_ids[] = {
287 { .compatible = "sirf,prima2-pinctrl", .data = &prima2_pinctrl_data, },
288 { .compatible = "sirf,atlas6-pinctrl", .data = &atlas6_pinctrl_data, },
289 { .compatible = "sirf,marco-pinctrl", .data = &prima2_pinctrl_data, },
290 {}
291};
292
293static int sirfsoc_pinmux_probe(struct platform_device *pdev)
294{
295 int ret;
296 struct sirfsoc_pmx *spmx;
297 struct device_node *np = pdev->dev.of_node;
298 const struct sirfsoc_pinctrl_data *pdata;
Barry Song3370dc92013-05-14 22:17:58 +0800299
300 /* Create state holders etc for this driver */
301 spmx = devm_kzalloc(&pdev->dev, sizeof(*spmx), GFP_KERNEL);
302 if (!spmx)
303 return -ENOMEM;
304
305 spmx->dev = &pdev->dev;
306
307 platform_set_drvdata(pdev, spmx);
308
309 spmx->gpio_virtbase = of_iomap(np, 0);
310 if (!spmx->gpio_virtbase) {
311 dev_err(&pdev->dev, "can't map gpio registers\n");
312 return -ENOMEM;
313 }
314
315 spmx->rsc_virtbase = sirfsoc_rsc_of_iomap();
316 if (!spmx->rsc_virtbase) {
317 ret = -ENOMEM;
318 dev_err(&pdev->dev, "can't map rsc registers\n");
319 goto out_no_rsc_remap;
320 }
321
322 if (of_device_is_compatible(np, "sirf,marco-pinctrl"))
323 spmx->is_marco = 1;
324
325 pdata = of_match_node(pinmux_ids, np)->data;
326 sirfsoc_pin_groups = pdata->grps;
327 sirfsoc_pingrp_cnt = pdata->grps_cnt;
328 sirfsoc_pmx_functions = pdata->funcs;
329 sirfsoc_pmxfunc_cnt = pdata->funcs_cnt;
330 sirfsoc_pinmux_desc.pins = pdata->pads;
331 sirfsoc_pinmux_desc.npins = pdata->pads_cnt;
332
333
334 /* Now register the pin controller and all pins it handles */
335 spmx->pmx = pinctrl_register(&sirfsoc_pinmux_desc, &pdev->dev, spmx);
336 if (!spmx->pmx) {
337 dev_err(&pdev->dev, "could not register SIRFSOC pinmux driver\n");
338 ret = -EINVAL;
339 goto out_no_pmx;
340 }
341
Barry Song3370dc92013-05-14 22:17:58 +0800342 dev_info(&pdev->dev, "initialized SIRFSOC pinmux driver\n");
343
344 return 0;
345
346out_no_pmx:
347 iounmap(spmx->rsc_virtbase);
348out_no_rsc_remap:
349 iounmap(spmx->gpio_virtbase);
350 return ret;
351}
352
Barry Songbc8d25a2013-05-16 11:17:09 +0800353#ifdef CONFIG_PM_SLEEP
354static int sirfsoc_pinmux_suspend_noirq(struct device *dev)
355{
356 int i, j;
357 struct sirfsoc_pmx *spmx = dev_get_drvdata(dev);
358
359 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
360 for (j = 0; j < SIRFSOC_GPIO_BANK_SIZE; j++) {
361 spmx->gpio_regs[i][j] = readl(spmx->gpio_virtbase +
362 SIRFSOC_GPIO_CTRL(i, j));
363 }
364 spmx->ints_regs[i] = readl(spmx->gpio_virtbase +
365 SIRFSOC_GPIO_INT_STATUS(i));
366 spmx->paden_regs[i] = readl(spmx->gpio_virtbase +
367 SIRFSOC_GPIO_PAD_EN(i));
368 }
369 spmx->dspen_regs = readl(spmx->gpio_virtbase + SIRFSOC_GPIO_DSP_EN0);
370
371 for (i = 0; i < 3; i++)
372 spmx->rsc_regs[i] = readl(spmx->rsc_virtbase + 4 * i);
373
374 return 0;
375}
376
377static int sirfsoc_pinmux_resume_noirq(struct device *dev)
378{
379 int i, j;
380 struct sirfsoc_pmx *spmx = dev_get_drvdata(dev);
381
382 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
383 for (j = 0; j < SIRFSOC_GPIO_BANK_SIZE; j++) {
384 writel(spmx->gpio_regs[i][j], spmx->gpio_virtbase +
385 SIRFSOC_GPIO_CTRL(i, j));
386 }
387 writel(spmx->ints_regs[i], spmx->gpio_virtbase +
388 SIRFSOC_GPIO_INT_STATUS(i));
389 writel(spmx->paden_regs[i], spmx->gpio_virtbase +
390 SIRFSOC_GPIO_PAD_EN(i));
391 }
392 writel(spmx->dspen_regs, spmx->gpio_virtbase + SIRFSOC_GPIO_DSP_EN0);
393
394 for (i = 0; i < 3; i++)
395 writel(spmx->rsc_regs[i], spmx->rsc_virtbase + 4 * i);
396
397 return 0;
398}
399
400static const struct dev_pm_ops sirfsoc_pinmux_pm_ops = {
401 .suspend_noirq = sirfsoc_pinmux_suspend_noirq,
402 .resume_noirq = sirfsoc_pinmux_resume_noirq,
Barry Songf6b17882013-07-04 15:59:53 +0800403 .freeze_noirq = sirfsoc_pinmux_suspend_noirq,
404 .restore_noirq = sirfsoc_pinmux_resume_noirq,
Barry Songbc8d25a2013-05-16 11:17:09 +0800405};
406#endif
407
Barry Song3370dc92013-05-14 22:17:58 +0800408static struct platform_driver sirfsoc_pinmux_driver = {
409 .driver = {
410 .name = DRIVER_NAME,
411 .owner = THIS_MODULE,
412 .of_match_table = pinmux_ids,
Barry Songbc8d25a2013-05-16 11:17:09 +0800413#ifdef CONFIG_PM_SLEEP
414 .pm = &sirfsoc_pinmux_pm_ops,
415#endif
Barry Song3370dc92013-05-14 22:17:58 +0800416 },
417 .probe = sirfsoc_pinmux_probe,
418};
419
420static int __init sirfsoc_pinmux_init(void)
421{
422 return platform_driver_register(&sirfsoc_pinmux_driver);
423}
424arch_initcall(sirfsoc_pinmux_init);
425
Linus Walleij294d1352014-04-23 23:08:02 +0200426static inline struct sirfsoc_gpio_chip *to_sirfsoc_gpio(struct gpio_chip *gc)
Barry Song3370dc92013-05-14 22:17:58 +0800427{
Linus Walleij294d1352014-04-23 23:08:02 +0200428 return container_of(gc, struct sirfsoc_gpio_chip, chip.gc);
Barry Song3370dc92013-05-14 22:17:58 +0800429}
430
Linus Walleij294d1352014-04-23 23:08:02 +0200431static inline struct sirfsoc_gpio_bank *
432sirfsoc_gpio_to_bank(struct sirfsoc_gpio_chip *sgpio, unsigned int offset)
Barry Song3370dc92013-05-14 22:17:58 +0800433{
Linus Walleij294d1352014-04-23 23:08:02 +0200434 return &sgpio->sgpio_bank[offset / SIRFSOC_GPIO_BANK_SIZE];
435}
436
437static inline int sirfsoc_gpio_to_bankoff(unsigned int offset)
438{
439 return offset % SIRFSOC_GPIO_BANK_SIZE;
Barry Song3370dc92013-05-14 22:17:58 +0800440}
Linus Walleij7420d2d2014-04-15 14:43:47 +0800441
Barry Song3370dc92013-05-14 22:17:58 +0800442static void sirfsoc_gpio_irq_ack(struct irq_data *d)
443{
Linus Walleij294d1352014-04-23 23:08:02 +0200444 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
445 struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
446 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
447 int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
Barry Song3370dc92013-05-14 22:17:58 +0800448 u32 val, offset;
449 unsigned long flags;
450
451 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
452
453 spin_lock_irqsave(&sgpio_lock, flags);
454
Linus Walleij294d1352014-04-23 23:08:02 +0200455 val = readl(sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800456
Linus Walleij294d1352014-04-23 23:08:02 +0200457 writel(val, sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800458
459 spin_unlock_irqrestore(&sgpio_lock, flags);
460}
461
Linus Walleij294d1352014-04-23 23:08:02 +0200462static void __sirfsoc_gpio_irq_mask(struct sirfsoc_gpio_chip *sgpio,
463 struct sirfsoc_gpio_bank *bank,
464 int idx)
Barry Song3370dc92013-05-14 22:17:58 +0800465{
466 u32 val, offset;
467 unsigned long flags;
468
469 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
470
471 spin_lock_irqsave(&sgpio_lock, flags);
472
Linus Walleij294d1352014-04-23 23:08:02 +0200473 val = readl(sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800474 val &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK;
475 val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK;
Linus Walleij294d1352014-04-23 23:08:02 +0200476 writel(val, sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800477
478 spin_unlock_irqrestore(&sgpio_lock, flags);
479}
480
481static void sirfsoc_gpio_irq_mask(struct irq_data *d)
482{
Linus Walleij294d1352014-04-23 23:08:02 +0200483 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
484 struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
485 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
Barry Song3370dc92013-05-14 22:17:58 +0800486
Linus Walleij294d1352014-04-23 23:08:02 +0200487 __sirfsoc_gpio_irq_mask(sgpio, bank, d->hwirq % SIRFSOC_GPIO_BANK_SIZE);
Barry Song3370dc92013-05-14 22:17:58 +0800488}
489
490static void sirfsoc_gpio_irq_unmask(struct irq_data *d)
491{
Linus Walleij294d1352014-04-23 23:08:02 +0200492 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
493 struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
494 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
495 int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
Barry Song3370dc92013-05-14 22:17:58 +0800496 u32 val, offset;
497 unsigned long flags;
498
499 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
500
501 spin_lock_irqsave(&sgpio_lock, flags);
502
Linus Walleij294d1352014-04-23 23:08:02 +0200503 val = readl(sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800504 val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK;
505 val |= SIRFSOC_GPIO_CTL_INTR_EN_MASK;
Linus Walleij294d1352014-04-23 23:08:02 +0200506 writel(val, sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800507
508 spin_unlock_irqrestore(&sgpio_lock, flags);
509}
510
511static int sirfsoc_gpio_irq_type(struct irq_data *d, unsigned type)
512{
Linus Walleij294d1352014-04-23 23:08:02 +0200513 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
514 struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
515 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
516 int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
Barry Song3370dc92013-05-14 22:17:58 +0800517 u32 val, offset;
518 unsigned long flags;
519
520 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
521
522 spin_lock_irqsave(&sgpio_lock, flags);
523
Linus Walleij294d1352014-04-23 23:08:02 +0200524 val = readl(sgpio->chip.regs + offset);
Barry Songb07ddcd2014-01-11 16:48:43 +0800525 val &= ~(SIRFSOC_GPIO_CTL_INTR_STS_MASK | SIRFSOC_GPIO_CTL_OUT_EN_MASK);
Barry Song3370dc92013-05-14 22:17:58 +0800526
527 switch (type) {
528 case IRQ_TYPE_NONE:
529 break;
530 case IRQ_TYPE_EDGE_RISING:
531 val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
532 val &= ~SIRFSOC_GPIO_CTL_INTR_LOW_MASK;
533 break;
534 case IRQ_TYPE_EDGE_FALLING:
535 val &= ~SIRFSOC_GPIO_CTL_INTR_HIGH_MASK;
536 val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
537 break;
538 case IRQ_TYPE_EDGE_BOTH:
539 val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK | SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
540 SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
541 break;
542 case IRQ_TYPE_LEVEL_LOW:
543 val &= ~(SIRFSOC_GPIO_CTL_INTR_HIGH_MASK | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK);
544 val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK;
545 break;
546 case IRQ_TYPE_LEVEL_HIGH:
547 val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK;
548 val &= ~(SIRFSOC_GPIO_CTL_INTR_LOW_MASK | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK);
549 break;
550 }
551
Linus Walleij294d1352014-04-23 23:08:02 +0200552 writel(val, sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800553
554 spin_unlock_irqrestore(&sgpio_lock, flags);
555
556 return 0;
557}
558
559static struct irq_chip sirfsoc_irq_chip = {
560 .name = "sirf-gpio-irq",
561 .irq_ack = sirfsoc_gpio_irq_ack,
562 .irq_mask = sirfsoc_gpio_irq_mask,
563 .irq_unmask = sirfsoc_gpio_irq_unmask,
564 .irq_set_type = sirfsoc_gpio_irq_type,
565};
566
567static void sirfsoc_gpio_handle_irq(unsigned int irq, struct irq_desc *desc)
568{
Linus Walleij294d1352014-04-23 23:08:02 +0200569 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
570 struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
Linus Walleij7420d2d2014-04-15 14:43:47 +0800571 struct sirfsoc_gpio_bank *bank;
Barry Song3370dc92013-05-14 22:17:58 +0800572 u32 status, ctrl;
573 int idx = 0;
574 struct irq_chip *chip = irq_get_chip(irq);
Linus Walleij7420d2d2014-04-15 14:43:47 +0800575 int i;
576
Barry Song648e42e2014-05-25 16:54:23 +0800577 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
Linus Walleij29c7f1f2014-05-30 09:52:43 +0200578 bank = &sgpio->sgpio_bank[i];
Linus Walleij7420d2d2014-04-15 14:43:47 +0800579 if (bank->parent_irq == irq)
580 break;
581 }
Barry Song648e42e2014-05-25 16:54:23 +0800582 BUG_ON(i == SIRFSOC_GPIO_NO_OF_BANKS);
Barry Song3370dc92013-05-14 22:17:58 +0800583
584 chained_irq_enter(chip, desc);
585
Linus Walleij294d1352014-04-23 23:08:02 +0200586 status = readl(sgpio->chip.regs + SIRFSOC_GPIO_INT_STATUS(bank->id));
Barry Song3370dc92013-05-14 22:17:58 +0800587 if (!status) {
588 printk(KERN_WARNING
589 "%s: gpio id %d status %#x no interrupt is flaged\n",
590 __func__, bank->id, status);
591 handle_bad_irq(irq, desc);
592 return;
593 }
594
595 while (status) {
Linus Walleij294d1352014-04-23 23:08:02 +0200596 ctrl = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, idx));
Barry Song3370dc92013-05-14 22:17:58 +0800597
598 /*
599 * Here we must check whether the corresponding GPIO's interrupt
600 * has been enabled, otherwise just skip it
601 */
602 if ((status & 0x1) && (ctrl & SIRFSOC_GPIO_CTL_INTR_EN_MASK)) {
603 pr_debug("%s: gpio id %d idx %d happens\n",
604 __func__, bank->id, idx);
Linus Walleij294d1352014-04-23 23:08:02 +0200605 generic_handle_irq(irq_find_mapping(gc->irqdomain, idx +
Barry Song8daeffb2014-01-11 16:48:42 +0800606 bank->id * SIRFSOC_GPIO_BANK_SIZE));
Barry Song3370dc92013-05-14 22:17:58 +0800607 }
608
609 idx++;
610 status = status >> 1;
611 }
612
613 chained_irq_exit(chip, desc);
614}
615
Linus Walleij294d1352014-04-23 23:08:02 +0200616static inline void sirfsoc_gpio_set_input(struct sirfsoc_gpio_chip *sgpio,
617 unsigned ctrl_offset)
Barry Song3370dc92013-05-14 22:17:58 +0800618{
619 u32 val;
620
Linus Walleij294d1352014-04-23 23:08:02 +0200621 val = readl(sgpio->chip.regs + ctrl_offset);
Barry Song3370dc92013-05-14 22:17:58 +0800622 val &= ~SIRFSOC_GPIO_CTL_OUT_EN_MASK;
Linus Walleij294d1352014-04-23 23:08:02 +0200623 writel(val, sgpio->chip.regs + ctrl_offset);
Barry Song3370dc92013-05-14 22:17:58 +0800624}
625
626static int sirfsoc_gpio_request(struct gpio_chip *chip, unsigned offset)
627{
Linus Walleij294d1352014-04-23 23:08:02 +0200628 struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
629 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
Barry Song3370dc92013-05-14 22:17:58 +0800630 unsigned long flags;
631
632 if (pinctrl_request_gpio(chip->base + offset))
633 return -ENODEV;
634
635 spin_lock_irqsave(&bank->lock, flags);
636
637 /*
638 * default status:
639 * set direction as input and mask irq
640 */
Linus Walleij294d1352014-04-23 23:08:02 +0200641 sirfsoc_gpio_set_input(sgpio, SIRFSOC_GPIO_CTRL(bank->id, offset));
642 __sirfsoc_gpio_irq_mask(sgpio, bank, offset);
Barry Song3370dc92013-05-14 22:17:58 +0800643
644 spin_unlock_irqrestore(&bank->lock, flags);
645
646 return 0;
647}
648
649static void sirfsoc_gpio_free(struct gpio_chip *chip, unsigned offset)
650{
Linus Walleij294d1352014-04-23 23:08:02 +0200651 struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
652 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
Barry Song3370dc92013-05-14 22:17:58 +0800653 unsigned long flags;
654
655 spin_lock_irqsave(&bank->lock, flags);
656
Linus Walleij294d1352014-04-23 23:08:02 +0200657 __sirfsoc_gpio_irq_mask(sgpio, bank, offset);
658 sirfsoc_gpio_set_input(sgpio, SIRFSOC_GPIO_CTRL(bank->id, offset));
Barry Song3370dc92013-05-14 22:17:58 +0800659
660 spin_unlock_irqrestore(&bank->lock, flags);
661
662 pinctrl_free_gpio(chip->base + offset);
663}
664
665static int sirfsoc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
666{
Linus Walleij294d1352014-04-23 23:08:02 +0200667 struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
668 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, gpio);
Barry Songc5eb7572014-04-15 14:43:46 +0800669 int idx = sirfsoc_gpio_to_bankoff(gpio);
Barry Song3370dc92013-05-14 22:17:58 +0800670 unsigned long flags;
671 unsigned offset;
672
673 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
674
675 spin_lock_irqsave(&bank->lock, flags);
676
Linus Walleij294d1352014-04-23 23:08:02 +0200677 sirfsoc_gpio_set_input(sgpio, offset);
Barry Song3370dc92013-05-14 22:17:58 +0800678
679 spin_unlock_irqrestore(&bank->lock, flags);
680
681 return 0;
682}
683
Linus Walleij294d1352014-04-23 23:08:02 +0200684static inline void sirfsoc_gpio_set_output(struct sirfsoc_gpio_chip *sgpio,
685 struct sirfsoc_gpio_bank *bank,
686 unsigned offset,
687 int value)
Barry Song3370dc92013-05-14 22:17:58 +0800688{
689 u32 out_ctrl;
690 unsigned long flags;
691
692 spin_lock_irqsave(&bank->lock, flags);
693
Linus Walleij294d1352014-04-23 23:08:02 +0200694 out_ctrl = readl(sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800695 if (value)
696 out_ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK;
697 else
698 out_ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK;
699
700 out_ctrl &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK;
701 out_ctrl |= SIRFSOC_GPIO_CTL_OUT_EN_MASK;
Linus Walleij294d1352014-04-23 23:08:02 +0200702 writel(out_ctrl, sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800703
704 spin_unlock_irqrestore(&bank->lock, flags);
705}
706
707static int sirfsoc_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, int value)
708{
Linus Walleij294d1352014-04-23 23:08:02 +0200709 struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
710 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, gpio);
Barry Songc5eb7572014-04-15 14:43:46 +0800711 int idx = sirfsoc_gpio_to_bankoff(gpio);
Barry Song3370dc92013-05-14 22:17:58 +0800712 u32 offset;
713 unsigned long flags;
714
715 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
716
717 spin_lock_irqsave(&sgpio_lock, flags);
718
Linus Walleij294d1352014-04-23 23:08:02 +0200719 sirfsoc_gpio_set_output(sgpio, bank, offset, value);
Barry Song3370dc92013-05-14 22:17:58 +0800720
721 spin_unlock_irqrestore(&sgpio_lock, flags);
722
723 return 0;
724}
725
726static int sirfsoc_gpio_get_value(struct gpio_chip *chip, unsigned offset)
727{
Linus Walleij294d1352014-04-23 23:08:02 +0200728 struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
729 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
Barry Song3370dc92013-05-14 22:17:58 +0800730 u32 val;
731 unsigned long flags;
732
733 spin_lock_irqsave(&bank->lock, flags);
734
Linus Walleij294d1352014-04-23 23:08:02 +0200735 val = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
Barry Song3370dc92013-05-14 22:17:58 +0800736
737 spin_unlock_irqrestore(&bank->lock, flags);
738
739 return !!(val & SIRFSOC_GPIO_CTL_DATAIN_MASK);
740}
741
742static void sirfsoc_gpio_set_value(struct gpio_chip *chip, unsigned offset,
743 int value)
744{
Linus Walleij294d1352014-04-23 23:08:02 +0200745 struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
746 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
Barry Song3370dc92013-05-14 22:17:58 +0800747 u32 ctrl;
748 unsigned long flags;
749
750 spin_lock_irqsave(&bank->lock, flags);
751
Linus Walleij294d1352014-04-23 23:08:02 +0200752 ctrl = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
Barry Song3370dc92013-05-14 22:17:58 +0800753 if (value)
754 ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK;
755 else
756 ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK;
Linus Walleij294d1352014-04-23 23:08:02 +0200757 writel(ctrl, sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
Barry Song3370dc92013-05-14 22:17:58 +0800758
759 spin_unlock_irqrestore(&bank->lock, flags);
760}
761
Linus Walleij294d1352014-04-23 23:08:02 +0200762static void sirfsoc_gpio_set_pullup(struct sirfsoc_gpio_chip *sgpio,
763 const u32 *pullups)
Barry Song3370dc92013-05-14 22:17:58 +0800764{
765 int i, n;
766 const unsigned long *p = (const unsigned long *)pullups;
767
768 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
769 for_each_set_bit(n, p + i, BITS_PER_LONG) {
770 u32 offset = SIRFSOC_GPIO_CTRL(i, n);
Linus Walleij294d1352014-04-23 23:08:02 +0200771 u32 val = readl(sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800772 val |= SIRFSOC_GPIO_CTL_PULL_MASK;
773 val |= SIRFSOC_GPIO_CTL_PULL_HIGH;
Linus Walleij294d1352014-04-23 23:08:02 +0200774 writel(val, sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800775 }
776 }
777}
778
Linus Walleij294d1352014-04-23 23:08:02 +0200779static void sirfsoc_gpio_set_pulldown(struct sirfsoc_gpio_chip *sgpio,
780 const u32 *pulldowns)
Barry Song3370dc92013-05-14 22:17:58 +0800781{
782 int i, n;
783 const unsigned long *p = (const unsigned long *)pulldowns;
784
785 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
786 for_each_set_bit(n, p + i, BITS_PER_LONG) {
787 u32 offset = SIRFSOC_GPIO_CTRL(i, n);
Linus Walleij294d1352014-04-23 23:08:02 +0200788 u32 val = readl(sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800789 val |= SIRFSOC_GPIO_CTL_PULL_MASK;
790 val &= ~SIRFSOC_GPIO_CTL_PULL_HIGH;
Linus Walleij294d1352014-04-23 23:08:02 +0200791 writel(val, sgpio->chip.regs + offset);
Barry Song3370dc92013-05-14 22:17:58 +0800792 }
793 }
794}
795
796static int sirfsoc_gpio_probe(struct device_node *np)
797{
798 int i, err = 0;
Linus Walleij294d1352014-04-23 23:08:02 +0200799 static struct sirfsoc_gpio_chip *sgpio;
Barry Song3370dc92013-05-14 22:17:58 +0800800 struct sirfsoc_gpio_bank *bank;
Jingoo Han2c9fdcf2013-08-06 18:14:12 +0900801 void __iomem *regs;
Barry Song3370dc92013-05-14 22:17:58 +0800802 struct platform_device *pdev;
803 bool is_marco = false;
804
805 u32 pullups[SIRFSOC_GPIO_NO_OF_BANKS], pulldowns[SIRFSOC_GPIO_NO_OF_BANKS];
806
807 pdev = of_find_device_by_node(np);
808 if (!pdev)
809 return -ENODEV;
810
Linus Walleij294d1352014-04-23 23:08:02 +0200811 sgpio = devm_kzalloc(&pdev->dev, sizeof(*sgpio), GFP_KERNEL);
812 if (!sgpio)
813 return -ENOMEM;
814
Barry Song3370dc92013-05-14 22:17:58 +0800815 regs = of_iomap(np, 0);
816 if (!regs)
817 return -ENOMEM;
818
819 if (of_device_is_compatible(np, "sirf,marco-pinctrl"))
820 is_marco = 1;
821
Linus Walleij294d1352014-04-23 23:08:02 +0200822 sgpio->chip.gc.request = sirfsoc_gpio_request;
823 sgpio->chip.gc.free = sirfsoc_gpio_free;
824 sgpio->chip.gc.direction_input = sirfsoc_gpio_direction_input;
825 sgpio->chip.gc.get = sirfsoc_gpio_get_value;
826 sgpio->chip.gc.direction_output = sirfsoc_gpio_direction_output;
827 sgpio->chip.gc.set = sirfsoc_gpio_set_value;
828 sgpio->chip.gc.base = 0;
829 sgpio->chip.gc.ngpio = SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS;
830 sgpio->chip.gc.label = kstrdup(np->full_name, GFP_KERNEL);
831 sgpio->chip.gc.of_node = np;
832 sgpio->chip.gc.of_xlate = sirfsoc_gpio_of_xlate;
833 sgpio->chip.gc.of_gpio_n_cells = 2;
834 sgpio->chip.gc.dev = &pdev->dev;
835 sgpio->chip.regs = regs;
836 sgpio->is_marco = is_marco;
Barry Songc5eb7572014-04-15 14:43:46 +0800837
Linus Walleij294d1352014-04-23 23:08:02 +0200838 err = gpiochip_add(&sgpio->chip.gc);
Barry Songc5eb7572014-04-15 14:43:46 +0800839 if (err) {
Linus Walleij7420d2d2014-04-15 14:43:47 +0800840 dev_err(&pdev->dev, "%s: error in probe function with status %d\n",
Barry Songc5eb7572014-04-15 14:43:46 +0800841 np->full_name, err);
842 goto out;
843 }
844
Linus Walleij294d1352014-04-23 23:08:02 +0200845 err = gpiochip_irqchip_add(&sgpio->chip.gc,
Linus Walleij7420d2d2014-04-15 14:43:47 +0800846 &sirfsoc_irq_chip,
847 0, handle_level_irq,
848 IRQ_TYPE_NONE);
849 if (err) {
850 dev_err(&pdev->dev,
851 "could not connect irqchip to gpiochip\n");
852 goto out;
853 }
854
Barry Song3370dc92013-05-14 22:17:58 +0800855 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
Linus Walleij294d1352014-04-23 23:08:02 +0200856 bank = &sgpio->sgpio_bank[i];
Barry Song3370dc92013-05-14 22:17:58 +0800857 spin_lock_init(&bank->lock);
Barry Song3370dc92013-05-14 22:17:58 +0800858 bank->parent_irq = platform_get_irq(pdev, i);
859 if (bank->parent_irq < 0) {
860 err = bank->parent_irq;
Linus Walleij294d1352014-04-23 23:08:02 +0200861 goto out_banks;
Barry Song3370dc92013-05-14 22:17:58 +0800862 }
863
Linus Walleij294d1352014-04-23 23:08:02 +0200864 gpiochip_set_chained_irqchip(&sgpio->chip.gc,
Linus Walleij7420d2d2014-04-15 14:43:47 +0800865 &sirfsoc_irq_chip,
866 bank->parent_irq,
867 sirfsoc_gpio_handle_irq);
Barry Song3370dc92013-05-14 22:17:58 +0800868 }
869
Linus Walleij294d1352014-04-23 23:08:02 +0200870 err = gpiochip_add_pin_range(&sgpio->chip.gc, dev_name(&pdev->dev),
871 0, 0, SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS);
872 if (err) {
873 dev_err(&pdev->dev,
874 "could not add gpiochip pin range\n");
875 goto out_no_range;
876 }
877
Barry Song3370dc92013-05-14 22:17:58 +0800878 if (!of_property_read_u32_array(np, "sirf,pullups", pullups,
879 SIRFSOC_GPIO_NO_OF_BANKS))
Linus Walleij294d1352014-04-23 23:08:02 +0200880 sirfsoc_gpio_set_pullup(sgpio, pullups);
Barry Song3370dc92013-05-14 22:17:58 +0800881
882 if (!of_property_read_u32_array(np, "sirf,pulldowns", pulldowns,
883 SIRFSOC_GPIO_NO_OF_BANKS))
Linus Walleij294d1352014-04-23 23:08:02 +0200884 sirfsoc_gpio_set_pulldown(sgpio, pulldowns);
Barry Song3370dc92013-05-14 22:17:58 +0800885
886 return 0;
887
Linus Walleij294d1352014-04-23 23:08:02 +0200888out_no_range:
889out_banks:
890 if (gpiochip_remove(&sgpio->chip.gc))
891 dev_err(&pdev->dev, "could not remove gpio chip\n");
Barry Song3370dc92013-05-14 22:17:58 +0800892out:
893 iounmap(regs);
894 return err;
895}
896
897static int __init sirfsoc_gpio_init(void)
898{
899
900 struct device_node *np;
901
902 np = of_find_matching_node(NULL, pinmux_ids);
903
904 if (!np)
905 return -ENODEV;
906
907 return sirfsoc_gpio_probe(np);
908}
909subsys_initcall(sirfsoc_gpio_init);
910
911MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>, "
912 "Yuping Luo <yuping.luo@csr.com>, "
913 "Barry Song <baohua.song@csr.com>");
914MODULE_DESCRIPTION("SIRFSOC pin control driver");
915MODULE_LICENSE("GPL");