blob: 0aee6bc4a484bb2c00d6cf5c432ddd45f3310a60 [file] [log] [blame]
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001/*
2 * mt65xx pinctrl driver based on Allwinner A1X pinctrl driver.
3 * Copyright (c) 2014 MediaTek Inc.
4 * Author: Hongzhou.Yang <hongzhou.yang@mediatek.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/io.h>
17#include <linux/gpio.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_address.h>
21#include <linux/of_device.h>
22#include <linux/of_irq.h>
23#include <linux/pinctrl/consumer.h>
24#include <linux/pinctrl/machine.h>
25#include <linux/pinctrl/pinconf.h>
26#include <linux/pinctrl/pinconf-generic.h>
27#include <linux/pinctrl/pinctrl.h>
28#include <linux/pinctrl/pinmux.h>
29#include <linux/platform_device.h>
30#include <linux/slab.h>
31#include <linux/bitops.h>
32#include <linux/regmap.h>
33#include <linux/mfd/syscon.h>
Maoguang Mengd9819eb2015-01-21 13:28:16 +080034#include <linux/delay.h>
Hongzhou Yang30f010f2015-01-27 15:13:55 +080035#include <linux/interrupt.h>
Hongzhou Yanga6df4102015-01-21 13:28:15 +080036#include <dt-bindings/pinctrl/mt65xx.h>
37
38#include "../core.h"
39#include "../pinconf.h"
40#include "../pinctrl-utils.h"
41#include "pinctrl-mtk-common.h"
42
43#define MAX_GPIO_MODE_PER_REG 5
44#define GPIO_MODE_BITS 3
45
46static const char * const mtk_gpio_functions[] = {
47 "func0", "func1", "func2", "func3",
48 "func4", "func5", "func6", "func7",
49};
50
51/*
52 * There are two base address for pull related configuration
53 * in mt8135, and different GPIO pins use different base address.
54 * When pin number greater than type1_start and less than type1_end,
55 * should use the second base address.
56 */
57static struct regmap *mtk_get_regmap(struct mtk_pinctrl *pctl,
58 unsigned long pin)
59{
60 if (pin >= pctl->devdata->type1_start && pin < pctl->devdata->type1_end)
61 return pctl->regmap2;
62 return pctl->regmap1;
63}
64
65static unsigned int mtk_get_port(struct mtk_pinctrl *pctl, unsigned long pin)
66{
67 /* Different SoC has different mask and port shift. */
68 return ((pin >> 4) & pctl->devdata->port_mask)
69 << pctl->devdata->port_shf;
70}
71
72static int mtk_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
73 struct pinctrl_gpio_range *range, unsigned offset,
74 bool input)
75{
76 unsigned int reg_addr;
77 unsigned int bit;
78 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
79
80 reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
81 bit = BIT(offset & 0xf);
82
83 if (input)
84 /* Different SoC has different alignment offset. */
85 reg_addr = CLR_ADDR(reg_addr, pctl);
86 else
87 reg_addr = SET_ADDR(reg_addr, pctl);
88
89 regmap_write(mtk_get_regmap(pctl, offset), reg_addr, bit);
90 return 0;
91}
92
93static void mtk_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
94{
95 unsigned int reg_addr;
96 unsigned int bit;
97 struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
98
99 reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dout_offset;
100 bit = BIT(offset & 0xf);
101
102 if (value)
103 reg_addr = SET_ADDR(reg_addr, pctl);
104 else
105 reg_addr = CLR_ADDR(reg_addr, pctl);
106
107 regmap_write(mtk_get_regmap(pctl, offset), reg_addr, bit);
108}
109
110static void mtk_pconf_set_ies_smt(struct mtk_pinctrl *pctl, unsigned pin,
111 int value, enum pin_config_param param)
112{
113 unsigned int reg_addr, offset;
114 unsigned int bit;
Hongzhou Yang30f010f2015-01-27 15:13:55 +0800115 int ret;
116
117 /*
118 * Due to some pins are irregular, their input enable and smt
119 * control register are discontinuous, but they are mapping together.
120 * So we need this special handle.
121 */
122 if (pctl->devdata->spec_ies_smt_set) {
123 ret = pctl->devdata->spec_ies_smt_set(mtk_get_regmap(pctl, pin),
124 pin, pctl->devdata->port_align, value);
125 if (!ret)
126 return;
127 }
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800128
129 bit = BIT(pin & 0xf);
130
131 if (param == PIN_CONFIG_INPUT_ENABLE)
132 offset = pctl->devdata->ies_offset;
133 else
134 offset = pctl->devdata->smt_offset;
135
136 if (value)
137 reg_addr = SET_ADDR(mtk_get_port(pctl, pin) + offset, pctl);
138 else
139 reg_addr = CLR_ADDR(mtk_get_port(pctl, pin) + offset, pctl);
140
141 regmap_write(mtk_get_regmap(pctl, pin), reg_addr, bit);
142}
143
144static const struct mtk_pin_drv_grp *mtk_find_pin_drv_grp_by_pin(
145 struct mtk_pinctrl *pctl, unsigned long pin) {
146 int i;
147
148 for (i = 0; i < pctl->devdata->n_pin_drv_grps; i++) {
149 const struct mtk_pin_drv_grp *pin_drv =
150 pctl->devdata->pin_drv_grp + i;
151 if (pin == pin_drv->pin)
152 return pin_drv;
153 }
154
155 return NULL;
156}
157
158static int mtk_pconf_set_driving(struct mtk_pinctrl *pctl,
159 unsigned int pin, unsigned char driving)
160{
161 const struct mtk_pin_drv_grp *pin_drv;
162 unsigned int val;
163 unsigned int bits, mask, shift;
164 const struct mtk_drv_group_desc *drv_grp;
165
166 if (pin >= pctl->devdata->npins)
167 return -EINVAL;
168
169 pin_drv = mtk_find_pin_drv_grp_by_pin(pctl, pin);
170 if (!pin_drv || pin_drv->grp > pctl->devdata->n_grp_cls)
171 return -EINVAL;
172
173 drv_grp = pctl->devdata->grp_desc + pin_drv->grp;
174 if (driving >= drv_grp->min_drv && driving <= drv_grp->max_drv
175 && !(driving % drv_grp->step)) {
176 val = driving / drv_grp->step - 1;
177 bits = drv_grp->high_bit - drv_grp->low_bit + 1;
178 mask = BIT(bits) - 1;
179 shift = pin_drv->bit + drv_grp->low_bit;
180 mask <<= shift;
181 val <<= shift;
182 return regmap_update_bits(mtk_get_regmap(pctl, pin),
183 pin_drv->offset, mask, val);
184 }
185
186 return -EINVAL;
187}
188
Yingjoe Chene73fe272015-05-18 23:11:15 -0700189int mtk_pctrl_spec_pull_set_samereg(struct regmap *regmap,
190 const struct mtk_pin_spec_pupd_set_samereg *pupd_infos,
191 unsigned int info_num, unsigned int pin,
192 unsigned char align, bool isup, unsigned int r1r0)
193{
194 unsigned int i;
195 unsigned int reg_pupd, reg_set, reg_rst;
196 unsigned int bit_pupd, bit_r0, bit_r1;
197 const struct mtk_pin_spec_pupd_set_samereg *spec_pupd_pin;
198 bool find = false;
199
200 for (i = 0; i < info_num; i++) {
201 if (pin == pupd_infos[i].pin) {
202 find = true;
203 break;
204 }
205 }
206
207 if (!find)
208 return -EINVAL;
209
210 spec_pupd_pin = pupd_infos + i;
211 reg_set = spec_pupd_pin->offset + align;
212 reg_rst = spec_pupd_pin->offset + (align << 1);
213
214 if (isup)
215 reg_pupd = reg_rst;
216 else
217 reg_pupd = reg_set;
218
219 bit_pupd = BIT(spec_pupd_pin->pupd_bit);
220 regmap_write(regmap, reg_pupd, bit_pupd);
221
222 bit_r0 = BIT(spec_pupd_pin->r0_bit);
223 bit_r1 = BIT(spec_pupd_pin->r1_bit);
224
225 switch (r1r0) {
226 case MTK_PUPD_SET_R1R0_00:
227 regmap_write(regmap, reg_rst, bit_r0);
228 regmap_write(regmap, reg_rst, bit_r1);
229 break;
230 case MTK_PUPD_SET_R1R0_01:
231 regmap_write(regmap, reg_set, bit_r0);
232 regmap_write(regmap, reg_rst, bit_r1);
233 break;
234 case MTK_PUPD_SET_R1R0_10:
235 regmap_write(regmap, reg_rst, bit_r0);
236 regmap_write(regmap, reg_set, bit_r1);
237 break;
238 case MTK_PUPD_SET_R1R0_11:
239 regmap_write(regmap, reg_set, bit_r0);
240 regmap_write(regmap, reg_set, bit_r1);
241 break;
242 default:
243 return -EINVAL;
244 }
245
246 return 0;
247}
248
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800249static int mtk_pconf_set_pull_select(struct mtk_pinctrl *pctl,
250 unsigned int pin, bool enable, bool isup, unsigned int arg)
251{
252 unsigned int bit;
253 unsigned int reg_pullen, reg_pullsel;
254 int ret;
255
256 /* Some pins' pull setting are very different,
257 * they have separate pull up/down bit, R0 and R1
258 * resistor bit, so we need this special handle.
259 */
260 if (pctl->devdata->spec_pull_set) {
261 ret = pctl->devdata->spec_pull_set(mtk_get_regmap(pctl, pin),
262 pin, pctl->devdata->port_align, isup, arg);
263 if (!ret)
264 return 0;
265 }
266
267 /* For generic pull config, default arg value should be 0 or 1. */
268 if (arg != 0 && arg != 1) {
269 dev_err(pctl->dev, "invalid pull-up argument %d on pin %d .\n",
270 arg, pin);
271 return -EINVAL;
272 }
273
274 bit = BIT(pin & 0xf);
275 if (enable)
276 reg_pullen = SET_ADDR(mtk_get_port(pctl, pin) +
277 pctl->devdata->pullen_offset, pctl);
278 else
279 reg_pullen = CLR_ADDR(mtk_get_port(pctl, pin) +
280 pctl->devdata->pullen_offset, pctl);
281
282 if (isup)
283 reg_pullsel = SET_ADDR(mtk_get_port(pctl, pin) +
284 pctl->devdata->pullsel_offset, pctl);
285 else
286 reg_pullsel = CLR_ADDR(mtk_get_port(pctl, pin) +
287 pctl->devdata->pullsel_offset, pctl);
288
289 regmap_write(mtk_get_regmap(pctl, pin), reg_pullen, bit);
290 regmap_write(mtk_get_regmap(pctl, pin), reg_pullsel, bit);
291 return 0;
292}
293
294static int mtk_pconf_parse_conf(struct pinctrl_dev *pctldev,
295 unsigned int pin, enum pin_config_param param,
296 enum pin_config_param arg)
297{
298 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
299
300 switch (param) {
301 case PIN_CONFIG_BIAS_DISABLE:
302 mtk_pconf_set_pull_select(pctl, pin, false, false, arg);
303 break;
304 case PIN_CONFIG_BIAS_PULL_UP:
305 mtk_pconf_set_pull_select(pctl, pin, true, true, arg);
306 break;
307 case PIN_CONFIG_BIAS_PULL_DOWN:
308 mtk_pconf_set_pull_select(pctl, pin, true, false, arg);
309 break;
310 case PIN_CONFIG_INPUT_ENABLE:
311 mtk_pconf_set_ies_smt(pctl, pin, arg, param);
312 break;
313 case PIN_CONFIG_OUTPUT:
314 mtk_gpio_set(pctl->chip, pin, arg);
315 mtk_pmx_gpio_set_direction(pctldev, NULL, pin, false);
316 break;
317 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
318 mtk_pconf_set_ies_smt(pctl, pin, arg, param);
319 break;
320 case PIN_CONFIG_DRIVE_STRENGTH:
321 mtk_pconf_set_driving(pctl, pin, arg);
322 break;
323 default:
324 return -EINVAL;
325 }
326
327 return 0;
328}
329
330static int mtk_pconf_group_get(struct pinctrl_dev *pctldev,
331 unsigned group,
332 unsigned long *config)
333{
334 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
335
336 *config = pctl->groups[group].config;
337
338 return 0;
339}
340
341static int mtk_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
342 unsigned long *configs, unsigned num_configs)
343{
344 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
345 struct mtk_pinctrl_group *g = &pctl->groups[group];
346 int i;
347
348 for (i = 0; i < num_configs; i++) {
349 mtk_pconf_parse_conf(pctldev, g->pin,
350 pinconf_to_config_param(configs[i]),
351 pinconf_to_config_argument(configs[i]));
352
353 g->config = configs[i];
354 }
355
356 return 0;
357}
358
359static const struct pinconf_ops mtk_pconf_ops = {
360 .pin_config_group_get = mtk_pconf_group_get,
361 .pin_config_group_set = mtk_pconf_group_set,
362};
363
364static struct mtk_pinctrl_group *
365mtk_pctrl_find_group_by_pin(struct mtk_pinctrl *pctl, u32 pin)
366{
367 int i;
368
369 for (i = 0; i < pctl->ngroups; i++) {
370 struct mtk_pinctrl_group *grp = pctl->groups + i;
371
372 if (grp->pin == pin)
373 return grp;
374 }
375
376 return NULL;
377}
378
379static const struct mtk_desc_function *mtk_pctrl_find_function_by_pin(
380 struct mtk_pinctrl *pctl, u32 pin_num, u32 fnum)
381{
382 const struct mtk_desc_pin *pin = pctl->devdata->pins + pin_num;
383 const struct mtk_desc_function *func = pin->functions;
384
385 while (func && func->name) {
386 if (func->muxval == fnum)
387 return func;
388 func++;
389 }
390
391 return NULL;
392}
393
394static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl *pctl,
395 u32 pin_num, u32 fnum)
396{
397 int i;
398
399 for (i = 0; i < pctl->devdata->npins; i++) {
400 const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
401
402 if (pin->pin.number == pin_num) {
403 const struct mtk_desc_function *func =
404 pin->functions;
405
406 while (func && func->name) {
407 if (func->muxval == fnum)
408 return true;
409 func++;
410 }
411
412 break;
413 }
414 }
415
416 return false;
417}
418
419static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl *pctl,
420 u32 pin, u32 fnum, struct mtk_pinctrl_group *grp,
421 struct pinctrl_map **map, unsigned *reserved_maps,
422 unsigned *num_maps)
423{
424 bool ret;
425
426 if (*num_maps == *reserved_maps)
427 return -ENOSPC;
428
429 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
430 (*map)[*num_maps].data.mux.group = grp->name;
431
432 ret = mtk_pctrl_is_function_valid(pctl, pin, fnum);
433 if (!ret) {
434 dev_err(pctl->dev, "invalid function %d on pin %d .\n",
435 fnum, pin);
436 return -EINVAL;
437 }
438
439 (*map)[*num_maps].data.mux.function = mtk_gpio_functions[fnum];
440 (*num_maps)++;
441
442 return 0;
443}
444
445static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
446 struct device_node *node,
447 struct pinctrl_map **map,
448 unsigned *reserved_maps,
449 unsigned *num_maps)
450{
451 struct property *pins;
452 u32 pinfunc, pin, func;
453 int num_pins, num_funcs, maps_per_pin;
454 unsigned long *configs;
455 unsigned int num_configs;
456 bool has_config = 0;
457 int i, err;
458 unsigned reserve = 0;
459 struct mtk_pinctrl_group *grp;
460 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
461
462 pins = of_find_property(node, "pinmux", NULL);
463 if (!pins) {
464 dev_err(pctl->dev, "missing pins property in node %s .\n",
465 node->name);
466 return -EINVAL;
467 }
468
Hongzhou Yangc445cac2015-02-11 23:56:11 -0800469 err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
470 &num_configs);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800471 if (num_configs)
472 has_config = 1;
473
474 num_pins = pins->length / sizeof(u32);
475 num_funcs = num_pins;
476 maps_per_pin = 0;
477 if (num_funcs)
478 maps_per_pin++;
479 if (has_config && num_pins >= 1)
480 maps_per_pin++;
481
482 if (!num_pins || !maps_per_pin)
483 return -EINVAL;
484
485 reserve = num_pins * maps_per_pin;
486
487 err = pinctrl_utils_reserve_map(pctldev, map,
488 reserved_maps, num_maps, reserve);
489 if (err < 0)
490 goto fail;
491
492 for (i = 0; i < num_pins; i++) {
493 err = of_property_read_u32_index(node, "pinmux",
494 i, &pinfunc);
495 if (err)
496 goto fail;
497
498 pin = MTK_GET_PIN_NO(pinfunc);
499 func = MTK_GET_PIN_FUNC(pinfunc);
500
501 if (pin >= pctl->devdata->npins ||
502 func >= ARRAY_SIZE(mtk_gpio_functions)) {
503 dev_err(pctl->dev, "invalid pins value.\n");
504 err = -EINVAL;
505 goto fail;
506 }
507
508 grp = mtk_pctrl_find_group_by_pin(pctl, pin);
509 if (!grp) {
510 dev_err(pctl->dev, "unable to match pin %d to group\n",
511 pin);
512 return -EINVAL;
513 }
514
515 err = mtk_pctrl_dt_node_to_map_func(pctl, pin, func, grp, map,
516 reserved_maps, num_maps);
517 if (err < 0)
518 goto fail;
519
520 if (has_config) {
521 err = pinctrl_utils_add_map_configs(pctldev, map,
522 reserved_maps, num_maps, grp->name,
523 configs, num_configs,
524 PIN_MAP_TYPE_CONFIGS_GROUP);
525 if (err < 0)
526 goto fail;
527 }
528 }
529
530 return 0;
531
532fail:
533 return err;
534}
535
536static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
537 struct device_node *np_config,
538 struct pinctrl_map **map, unsigned *num_maps)
539{
540 struct device_node *np;
541 unsigned reserved_maps;
542 int ret;
543
544 *map = NULL;
545 *num_maps = 0;
546 reserved_maps = 0;
547
548 for_each_child_of_node(np_config, np) {
549 ret = mtk_pctrl_dt_subnode_to_map(pctldev, np, map,
550 &reserved_maps, num_maps);
551 if (ret < 0) {
552 pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
553 return ret;
554 }
555 }
556
557 return 0;
558}
559
560static int mtk_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
561{
562 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
563
564 return pctl->ngroups;
565}
566
567static const char *mtk_pctrl_get_group_name(struct pinctrl_dev *pctldev,
568 unsigned group)
569{
570 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
571
572 return pctl->groups[group].name;
573}
574
575static int mtk_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
576 unsigned group,
577 const unsigned **pins,
578 unsigned *num_pins)
579{
580 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
581
582 *pins = (unsigned *)&pctl->groups[group].pin;
583 *num_pins = 1;
584
585 return 0;
586}
587
588static const struct pinctrl_ops mtk_pctrl_ops = {
589 .dt_node_to_map = mtk_pctrl_dt_node_to_map,
590 .dt_free_map = pinctrl_utils_dt_free_map,
591 .get_groups_count = mtk_pctrl_get_groups_count,
592 .get_group_name = mtk_pctrl_get_group_name,
593 .get_group_pins = mtk_pctrl_get_group_pins,
594};
595
596static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
597{
598 return ARRAY_SIZE(mtk_gpio_functions);
599}
600
601static const char *mtk_pmx_get_func_name(struct pinctrl_dev *pctldev,
602 unsigned selector)
603{
604 return mtk_gpio_functions[selector];
605}
606
607static int mtk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
608 unsigned function,
609 const char * const **groups,
610 unsigned * const num_groups)
611{
612 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
613
614 *groups = pctl->grp_names;
615 *num_groups = pctl->ngroups;
616
617 return 0;
618}
619
620static int mtk_pmx_set_mode(struct pinctrl_dev *pctldev,
621 unsigned long pin, unsigned long mode)
622{
623 unsigned int reg_addr;
624 unsigned char bit;
625 unsigned int val;
626 unsigned int mask = (1L << GPIO_MODE_BITS) - 1;
627 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
628
629 reg_addr = ((pin / MAX_GPIO_MODE_PER_REG) << pctl->devdata->port_shf)
630 + pctl->devdata->pinmux_offset;
631
632 bit = pin % MAX_GPIO_MODE_PER_REG;
633 mask <<= (GPIO_MODE_BITS * bit);
634 val = (mode << (GPIO_MODE_BITS * bit));
635 return regmap_update_bits(mtk_get_regmap(pctl, pin),
636 reg_addr, mask, val);
637}
638
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800639static const struct mtk_desc_pin *
640mtk_find_pin_by_eint_num(struct mtk_pinctrl *pctl, unsigned int eint_num)
641{
642 int i;
643 const struct mtk_desc_pin *pin;
644
645 for (i = 0; i < pctl->devdata->npins; i++) {
646 pin = pctl->devdata->pins + i;
647 if (pin->eint.eintnum == eint_num)
648 return pin;
649 }
650
651 return NULL;
652}
653
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800654static int mtk_pmx_set_mux(struct pinctrl_dev *pctldev,
655 unsigned function,
656 unsigned group)
657{
658 bool ret;
659 const struct mtk_desc_function *desc;
660 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
661 struct mtk_pinctrl_group *g = pctl->groups + group;
662
663 ret = mtk_pctrl_is_function_valid(pctl, g->pin, function);
664 if (!ret) {
665 dev_err(pctl->dev, "invaild function %d on group %d .\n",
666 function, group);
667 return -EINVAL;
668 }
669
670 desc = mtk_pctrl_find_function_by_pin(pctl, g->pin, function);
671 if (!desc)
672 return -EINVAL;
673 mtk_pmx_set_mode(pctldev, g->pin, desc->muxval);
674 return 0;
675}
676
677static const struct pinmux_ops mtk_pmx_ops = {
678 .get_functions_count = mtk_pmx_get_funcs_cnt,
679 .get_function_name = mtk_pmx_get_func_name,
680 .get_function_groups = mtk_pmx_get_func_groups,
681 .set_mux = mtk_pmx_set_mux,
682 .gpio_set_direction = mtk_pmx_gpio_set_direction,
683};
684
685static int mtk_gpio_request(struct gpio_chip *chip, unsigned offset)
686{
687 return pinctrl_request_gpio(chip->base + offset);
688}
689
690static void mtk_gpio_free(struct gpio_chip *chip, unsigned offset)
691{
692 pinctrl_free_gpio(chip->base + offset);
693}
694
695static int mtk_gpio_direction_input(struct gpio_chip *chip,
696 unsigned offset)
697{
698 return pinctrl_gpio_direction_input(chip->base + offset);
699}
700
701static int mtk_gpio_direction_output(struct gpio_chip *chip,
702 unsigned offset, int value)
703{
704 mtk_gpio_set(chip, offset, value);
705 return pinctrl_gpio_direction_output(chip->base + offset);
706}
707
708static int mtk_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
709{
710 unsigned int reg_addr;
711 unsigned int bit;
712 unsigned int read_val = 0;
713
714 struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
715
716 reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
717 bit = BIT(offset & 0xf);
718 regmap_read(pctl->regmap1, reg_addr, &read_val);
719 return !!(read_val & bit);
720}
721
722static int mtk_gpio_get(struct gpio_chip *chip, unsigned offset)
723{
724 unsigned int reg_addr;
725 unsigned int bit;
726 unsigned int read_val = 0;
727 struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
728
729 if (mtk_gpio_get_direction(chip, offset))
730 reg_addr = mtk_get_port(pctl, offset) +
731 pctl->devdata->dout_offset;
732 else
733 reg_addr = mtk_get_port(pctl, offset) +
734 pctl->devdata->din_offset;
735
736 bit = BIT(offset & 0xf);
737 regmap_read(pctl->regmap1, reg_addr, &read_val);
738 return !!(read_val & bit);
739}
740
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800741static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
742{
743 const struct mtk_desc_pin *pin;
744 struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
745 int irq;
746
747 pin = pctl->devdata->pins + offset;
748 if (pin->eint.eintnum == NO_EINT_SUPPORT)
749 return -EINVAL;
750
751 irq = irq_find_mapping(pctl->domain, pin->eint.eintnum);
752 if (!irq)
753 return -EINVAL;
754
755 return irq;
756}
757
758static int mtk_pinctrl_irq_request_resources(struct irq_data *d)
759{
760 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
761 const struct mtk_desc_pin *pin;
762 int ret;
763
764 pin = mtk_find_pin_by_eint_num(pctl, d->hwirq);
765
766 if (!pin) {
767 dev_err(pctl->dev, "Can not find pin\n");
768 return -EINVAL;
769 }
770
771 ret = gpiochip_lock_as_irq(pctl->chip, pin->pin.number);
772 if (ret) {
773 dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
774 irqd_to_hwirq(d));
775 return ret;
776 }
777
778 /* set mux to INT mode */
779 mtk_pmx_set_mode(pctl->pctl_dev, pin->pin.number, pin->eint.eintmux);
780
781 return 0;
782}
783
784static void mtk_pinctrl_irq_release_resources(struct irq_data *d)
785{
786 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
787 const struct mtk_desc_pin *pin;
788
789 pin = mtk_find_pin_by_eint_num(pctl, d->hwirq);
790
791 if (!pin) {
792 dev_err(pctl->dev, "Can not find pin\n");
793 return;
794 }
795
796 gpiochip_unlock_as_irq(pctl->chip, pin->pin.number);
797}
798
799static void __iomem *mtk_eint_get_offset(struct mtk_pinctrl *pctl,
800 unsigned int eint_num, unsigned int offset)
801{
802 unsigned int eint_base = 0;
803 void __iomem *reg;
804
805 if (eint_num >= pctl->devdata->ap_num)
806 eint_base = pctl->devdata->ap_num;
807
808 reg = pctl->eint_reg_base + offset + ((eint_num - eint_base) / 32) * 4;
809
810 return reg;
811}
812
813/*
814 * mtk_can_en_debounce: Check the EINT number is able to enable debounce or not
815 * @eint_num: the EINT number to setmtk_pinctrl
816 */
817static unsigned int mtk_eint_can_en_debounce(struct mtk_pinctrl *pctl,
818 unsigned int eint_num)
819{
820 unsigned int sens;
821 unsigned int bit = BIT(eint_num % 32);
822 const struct mtk_eint_offsets *eint_offsets =
823 &pctl->devdata->eint_offsets;
824
825 void __iomem *reg = mtk_eint_get_offset(pctl, eint_num,
826 eint_offsets->sens);
827
828 if (readl(reg) & bit)
829 sens = MT_LEVEL_SENSITIVE;
830 else
831 sens = MT_EDGE_SENSITIVE;
832
833 if ((eint_num < pctl->devdata->db_cnt) && (sens != MT_EDGE_SENSITIVE))
834 return 1;
835 else
836 return 0;
837}
838
839/*
840 * mtk_eint_get_mask: To get the eint mask
841 * @eint_num: the EINT number to get
842 */
843static unsigned int mtk_eint_get_mask(struct mtk_pinctrl *pctl,
844 unsigned int eint_num)
845{
846 unsigned int bit = BIT(eint_num % 32);
847 const struct mtk_eint_offsets *eint_offsets =
848 &pctl->devdata->eint_offsets;
849
850 void __iomem *reg = mtk_eint_get_offset(pctl, eint_num,
851 eint_offsets->mask);
852
853 return !!(readl(reg) & bit);
854}
855
Yingjoe Chen3221f402015-01-27 14:15:26 +0800856static int mtk_eint_flip_edge(struct mtk_pinctrl *pctl, int hwirq)
857{
858 int start_level, curr_level;
859 unsigned int reg_offset;
860 const struct mtk_eint_offsets *eint_offsets = &(pctl->devdata->eint_offsets);
861 u32 mask = 1 << (hwirq & 0x1f);
862 u32 port = (hwirq >> 5) & eint_offsets->port_mask;
863 void __iomem *reg = pctl->eint_reg_base + (port << 2);
864 const struct mtk_desc_pin *pin;
865
866 pin = mtk_find_pin_by_eint_num(pctl, hwirq);
867 curr_level = mtk_gpio_get(pctl->chip, pin->pin.number);
868 do {
869 start_level = curr_level;
870 if (start_level)
871 reg_offset = eint_offsets->pol_clr;
872 else
873 reg_offset = eint_offsets->pol_set;
874 writel(mask, reg + reg_offset);
875
876 curr_level = mtk_gpio_get(pctl->chip, pin->pin.number);
877 } while (start_level != curr_level);
878
879 return start_level;
880}
881
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800882static void mtk_eint_mask(struct irq_data *d)
883{
884 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
885 const struct mtk_eint_offsets *eint_offsets =
886 &pctl->devdata->eint_offsets;
887 u32 mask = BIT(d->hwirq & 0x1f);
888 void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
889 eint_offsets->mask_set);
890
891 writel(mask, reg);
892}
893
894static void mtk_eint_unmask(struct irq_data *d)
895{
896 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
897 const struct mtk_eint_offsets *eint_offsets =
898 &pctl->devdata->eint_offsets;
899 u32 mask = BIT(d->hwirq & 0x1f);
900 void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
901 eint_offsets->mask_clr);
902
903 writel(mask, reg);
Yingjoe Chen3221f402015-01-27 14:15:26 +0800904
905 if (pctl->eint_dual_edges[d->hwirq])
906 mtk_eint_flip_edge(pctl, d->hwirq);
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800907}
908
909static int mtk_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
910 unsigned debounce)
911{
912 struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
913 int eint_num, virq, eint_offset;
914 unsigned int set_offset, bit, clr_bit, clr_offset, rst, i, unmask, dbnc;
915 static const unsigned int dbnc_arr[] = {0 , 1, 16, 32, 64, 128, 256};
916 const struct mtk_desc_pin *pin;
917 struct irq_data *d;
918
919 pin = pctl->devdata->pins + offset;
920 if (pin->eint.eintnum == NO_EINT_SUPPORT)
921 return -EINVAL;
922
923 eint_num = pin->eint.eintnum;
924 virq = irq_find_mapping(pctl->domain, eint_num);
925 eint_offset = (eint_num % 4) * 8;
926 d = irq_get_irq_data(virq);
927
928 set_offset = (eint_num / 4) * 4 + pctl->devdata->eint_offsets.dbnc_set;
929 clr_offset = (eint_num / 4) * 4 + pctl->devdata->eint_offsets.dbnc_clr;
930 if (!mtk_eint_can_en_debounce(pctl, eint_num))
931 return -ENOSYS;
932
933 dbnc = ARRAY_SIZE(dbnc_arr);
934 for (i = 0; i < ARRAY_SIZE(dbnc_arr); i++) {
935 if (debounce <= dbnc_arr[i]) {
936 dbnc = i;
937 break;
938 }
939 }
940
941 if (!mtk_eint_get_mask(pctl, eint_num)) {
942 mtk_eint_mask(d);
943 unmask = 1;
944 }
945
946 clr_bit = 0xff << eint_offset;
947 writel(clr_bit, pctl->eint_reg_base + clr_offset);
948
949 bit = ((dbnc << EINT_DBNC_SET_DBNC_BITS) | EINT_DBNC_SET_EN) <<
950 eint_offset;
951 rst = EINT_DBNC_RST_BIT << eint_offset;
952 writel(rst | bit, pctl->eint_reg_base + set_offset);
953
954 /* Delay a while (more than 2T) to wait for hw debounce counter reset
955 work correctly */
956 udelay(1);
957 if (unmask == 1)
958 mtk_eint_unmask(d);
959
960 return 0;
961}
962
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800963static struct gpio_chip mtk_gpio_chip = {
964 .owner = THIS_MODULE,
965 .request = mtk_gpio_request,
966 .free = mtk_gpio_free,
967 .direction_input = mtk_gpio_direction_input,
968 .direction_output = mtk_gpio_direction_output,
969 .get = mtk_gpio_get,
970 .set = mtk_gpio_set,
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800971 .to_irq = mtk_gpio_to_irq,
972 .set_debounce = mtk_gpio_set_debounce,
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800973 .of_gpio_n_cells = 2,
974};
975
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800976static int mtk_eint_set_type(struct irq_data *d,
977 unsigned int type)
978{
979 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
980 const struct mtk_eint_offsets *eint_offsets =
981 &pctl->devdata->eint_offsets;
982 u32 mask = BIT(d->hwirq & 0x1f);
983 void __iomem *reg;
984
985 if (((type & IRQ_TYPE_EDGE_BOTH) && (type & IRQ_TYPE_LEVEL_MASK)) ||
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800986 ((type & IRQ_TYPE_LEVEL_MASK) == IRQ_TYPE_LEVEL_MASK)) {
987 dev_err(pctl->dev, "Can't configure IRQ%d (EINT%lu) for type 0x%X\n",
988 d->irq, d->hwirq, type);
989 return -EINVAL;
990 }
991
Yingjoe Chen3221f402015-01-27 14:15:26 +0800992 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
993 pctl->eint_dual_edges[d->hwirq] = 1;
994 else
995 pctl->eint_dual_edges[d->hwirq] = 0;
996
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800997 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING)) {
998 reg = mtk_eint_get_offset(pctl, d->hwirq,
999 eint_offsets->pol_clr);
1000 writel(mask, reg);
1001 } else {
1002 reg = mtk_eint_get_offset(pctl, d->hwirq,
1003 eint_offsets->pol_set);
1004 writel(mask, reg);
1005 }
1006
1007 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
1008 reg = mtk_eint_get_offset(pctl, d->hwirq,
1009 eint_offsets->sens_clr);
1010 writel(mask, reg);
1011 } else {
1012 reg = mtk_eint_get_offset(pctl, d->hwirq,
1013 eint_offsets->sens_set);
1014 writel(mask, reg);
1015 }
1016
Yingjoe Chen3221f402015-01-27 14:15:26 +08001017 if (pctl->eint_dual_edges[d->hwirq])
1018 mtk_eint_flip_edge(pctl, d->hwirq);
1019
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001020 return 0;
1021}
1022
1023static void mtk_eint_ack(struct irq_data *d)
1024{
1025 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1026 const struct mtk_eint_offsets *eint_offsets =
1027 &pctl->devdata->eint_offsets;
1028 u32 mask = BIT(d->hwirq & 0x1f);
1029 void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
1030 eint_offsets->ack);
1031
1032 writel(mask, reg);
1033}
1034
1035static struct irq_chip mtk_pinctrl_irq_chip = {
1036 .name = "mt-eint",
1037 .irq_mask = mtk_eint_mask,
1038 .irq_unmask = mtk_eint_unmask,
1039 .irq_ack = mtk_eint_ack,
1040 .irq_set_type = mtk_eint_set_type,
1041 .irq_request_resources = mtk_pinctrl_irq_request_resources,
1042 .irq_release_resources = mtk_pinctrl_irq_release_resources,
1043};
1044
1045static unsigned int mtk_eint_init(struct mtk_pinctrl *pctl)
1046{
1047 const struct mtk_eint_offsets *eint_offsets =
1048 &pctl->devdata->eint_offsets;
1049 void __iomem *reg = pctl->eint_reg_base + eint_offsets->dom_en;
1050 unsigned int i;
1051
1052 for (i = 0; i < pctl->devdata->ap_num; i += 32) {
1053 writel(0xffffffff, reg);
1054 reg += 4;
1055 }
1056 return 0;
1057}
1058
1059static inline void
1060mtk_eint_debounce_process(struct mtk_pinctrl *pctl, int index)
1061{
1062 unsigned int rst, ctrl_offset;
1063 unsigned int bit, dbnc;
1064 const struct mtk_eint_offsets *eint_offsets =
1065 &pctl->devdata->eint_offsets;
1066
1067 ctrl_offset = (index / 4) * 4 + eint_offsets->dbnc_ctrl;
1068 dbnc = readl(pctl->eint_reg_base + ctrl_offset);
1069 bit = EINT_DBNC_SET_EN << ((index % 4) * 8);
1070 if ((bit & dbnc) > 0) {
1071 ctrl_offset = (index / 4) * 4 + eint_offsets->dbnc_set;
1072 rst = EINT_DBNC_RST_BIT << ((index % 4) * 8);
1073 writel(rst, pctl->eint_reg_base + ctrl_offset);
1074 }
1075}
1076
1077static void mtk_eint_irq_handler(unsigned irq, struct irq_desc *desc)
1078{
1079 struct irq_chip *chip = irq_get_chip(irq);
1080 struct mtk_pinctrl *pctl = irq_get_handler_data(irq);
1081 unsigned int status, eint_num;
1082 int offset, index, virq;
1083 const struct mtk_eint_offsets *eint_offsets =
1084 &pctl->devdata->eint_offsets;
1085 void __iomem *reg = mtk_eint_get_offset(pctl, 0, eint_offsets->stat);
Yingjoe Chen3221f402015-01-27 14:15:26 +08001086 int dual_edges, start_level, curr_level;
1087 const struct mtk_desc_pin *pin;
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001088
1089 chained_irq_enter(chip, desc);
1090 for (eint_num = 0; eint_num < pctl->devdata->ap_num; eint_num += 32) {
1091 status = readl(reg);
1092 reg += 4;
1093 while (status) {
1094 offset = __ffs(status);
1095 index = eint_num + offset;
1096 virq = irq_find_mapping(pctl->domain, index);
1097 status &= ~BIT(offset);
1098
Yingjoe Chen3221f402015-01-27 14:15:26 +08001099 dual_edges = pctl->eint_dual_edges[index];
1100 if (dual_edges) {
1101 /* Clear soft-irq in case we raised it
1102 last time */
1103 writel(BIT(offset), reg - eint_offsets->stat +
1104 eint_offsets->soft_clr);
1105
1106 pin = mtk_find_pin_by_eint_num(pctl, index);
1107 start_level = mtk_gpio_get(pctl->chip,
1108 pin->pin.number);
1109 }
1110
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001111 generic_handle_irq(virq);
1112
Yingjoe Chen3221f402015-01-27 14:15:26 +08001113 if (dual_edges) {
1114 curr_level = mtk_eint_flip_edge(pctl, index);
1115
1116 /* If level changed, we might lost one edge
1117 interrupt, raised it through soft-irq */
1118 if (start_level != curr_level)
1119 writel(BIT(offset), reg -
1120 eint_offsets->stat +
1121 eint_offsets->soft_set);
1122 }
1123
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001124 if (index < pctl->devdata->db_cnt)
1125 mtk_eint_debounce_process(pctl , index);
1126 }
1127 }
1128 chained_irq_exit(chip, desc);
1129}
1130
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001131static int mtk_pctrl_build_state(struct platform_device *pdev)
1132{
1133 struct mtk_pinctrl *pctl = platform_get_drvdata(pdev);
1134 int i;
1135
1136 pctl->ngroups = pctl->devdata->npins;
1137
1138 /* Allocate groups */
Axel Lin0206caa2015-03-12 21:53:32 +08001139 pctl->groups = devm_kcalloc(&pdev->dev, pctl->ngroups,
1140 sizeof(*pctl->groups), GFP_KERNEL);
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001141 if (!pctl->groups)
1142 return -ENOMEM;
1143
1144 /* We assume that one pin is one group, use pin name as group name. */
Axel Lin0206caa2015-03-12 21:53:32 +08001145 pctl->grp_names = devm_kcalloc(&pdev->dev, pctl->ngroups,
1146 sizeof(*pctl->grp_names), GFP_KERNEL);
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001147 if (!pctl->grp_names)
1148 return -ENOMEM;
1149
1150 for (i = 0; i < pctl->devdata->npins; i++) {
1151 const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
1152 struct mtk_pinctrl_group *group = pctl->groups + i;
1153
1154 group->name = pin->pin.name;
1155 group->pin = pin->pin.number;
1156
1157 pctl->grp_names[i] = pin->pin.name;
1158 }
1159
1160 return 0;
1161}
1162
1163static struct pinctrl_desc mtk_pctrl_desc = {
1164 .confops = &mtk_pconf_ops,
1165 .pctlops = &mtk_pctrl_ops,
1166 .pmxops = &mtk_pmx_ops,
1167};
1168
1169int mtk_pctrl_init(struct platform_device *pdev,
1170 const struct mtk_pinctrl_devdata *data)
1171{
1172 struct pinctrl_pin_desc *pins;
1173 struct mtk_pinctrl *pctl;
1174 struct device_node *np = pdev->dev.of_node, *node;
1175 struct property *prop;
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001176 struct resource *res;
1177 int i, ret, irq;
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001178
1179 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
1180 if (!pctl)
1181 return -ENOMEM;
1182
1183 platform_set_drvdata(pdev, pctl);
1184
1185 prop = of_find_property(np, "pins-are-numbered", NULL);
1186 if (!prop) {
Hongzhou Yangc445cac2015-02-11 23:56:11 -08001187 dev_err(&pdev->dev, "only support pins-are-numbered format\n");
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001188 return -EINVAL;
1189 }
1190
1191 node = of_parse_phandle(np, "mediatek,pctl-regmap", 0);
1192 if (node) {
1193 pctl->regmap1 = syscon_node_to_regmap(node);
1194 if (IS_ERR(pctl->regmap1))
1195 return PTR_ERR(pctl->regmap1);
1196 }
1197
1198 /* Only 8135 has two base addr, other SoCs have only one. */
1199 node = of_parse_phandle(np, "mediatek,pctl-regmap", 1);
1200 if (node) {
1201 pctl->regmap2 = syscon_node_to_regmap(node);
1202 if (IS_ERR(pctl->regmap2))
1203 return PTR_ERR(pctl->regmap2);
1204 }
1205
1206 pctl->devdata = data;
1207 ret = mtk_pctrl_build_state(pdev);
1208 if (ret) {
1209 dev_err(&pdev->dev, "build state failed: %d\n", ret);
1210 return -EINVAL;
1211 }
1212
Axel Lin0206caa2015-03-12 21:53:32 +08001213 pins = devm_kcalloc(&pdev->dev, pctl->devdata->npins, sizeof(*pins),
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001214 GFP_KERNEL);
1215 if (!pins)
1216 return -ENOMEM;
1217
1218 for (i = 0; i < pctl->devdata->npins; i++)
1219 pins[i] = pctl->devdata->pins[i].pin;
1220 mtk_pctrl_desc.name = dev_name(&pdev->dev);
1221 mtk_pctrl_desc.owner = THIS_MODULE;
1222 mtk_pctrl_desc.pins = pins;
1223 mtk_pctrl_desc.npins = pctl->devdata->npins;
1224 pctl->dev = &pdev->dev;
1225 pctl->pctl_dev = pinctrl_register(&mtk_pctrl_desc, &pdev->dev, pctl);
1226 if (!pctl->pctl_dev) {
1227 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
1228 return -EINVAL;
1229 }
1230
1231 pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
1232 if (!pctl->chip) {
1233 ret = -ENOMEM;
1234 goto pctrl_error;
1235 }
1236
1237 pctl->chip = &mtk_gpio_chip;
1238 pctl->chip->ngpio = pctl->devdata->npins;
1239 pctl->chip->label = dev_name(&pdev->dev);
1240 pctl->chip->dev = &pdev->dev;
1241 pctl->chip->base = 0;
1242
1243 ret = gpiochip_add(pctl->chip);
1244 if (ret) {
1245 ret = -EINVAL;
1246 goto pctrl_error;
1247 }
1248
1249 /* Register the GPIO to pin mappings. */
1250 ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
1251 0, 0, pctl->devdata->npins);
1252 if (ret) {
1253 ret = -EINVAL;
1254 goto chip_error;
1255 }
1256
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001257 /* Get EINT register base from dts. */
1258 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1259 if (!res) {
1260 dev_err(&pdev->dev, "Unable to get Pinctrl resource\n");
1261 ret = -EINVAL;
1262 goto chip_error;
1263 }
1264
1265 pctl->eint_reg_base = devm_ioremap_resource(&pdev->dev, res);
1266 if (IS_ERR(pctl->eint_reg_base)) {
1267 ret = -EINVAL;
1268 goto chip_error;
1269 }
1270
Axel Lin0206caa2015-03-12 21:53:32 +08001271 pctl->eint_dual_edges = devm_kcalloc(&pdev->dev, pctl->devdata->ap_num,
1272 sizeof(int), GFP_KERNEL);
Yingjoe Chen3221f402015-01-27 14:15:26 +08001273 if (!pctl->eint_dual_edges) {
1274 ret = -ENOMEM;
1275 goto chip_error;
1276 }
1277
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001278 irq = irq_of_parse_and_map(np, 0);
1279 if (!irq) {
1280 dev_err(&pdev->dev, "couldn't parse and map irq\n");
1281 ret = -EINVAL;
Axel Lin61a35572015-03-12 21:52:33 +08001282 goto chip_error;
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001283 }
1284
1285 pctl->domain = irq_domain_add_linear(np,
1286 pctl->devdata->ap_num, &irq_domain_simple_ops, NULL);
1287 if (!pctl->domain) {
1288 dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
1289 ret = -ENOMEM;
Axel Lin61a35572015-03-12 21:52:33 +08001290 goto chip_error;
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001291 }
1292
1293 mtk_eint_init(pctl);
1294 for (i = 0; i < pctl->devdata->ap_num; i++) {
1295 int virq = irq_create_mapping(pctl->domain, i);
1296
1297 irq_set_chip_and_handler(virq, &mtk_pinctrl_irq_chip,
1298 handle_level_irq);
1299 irq_set_chip_data(virq, pctl);
1300 set_irq_flags(virq, IRQF_VALID);
1301 };
1302
1303 irq_set_chained_handler(irq, mtk_eint_irq_handler);
1304 irq_set_handler_data(irq, pctl);
1305 set_irq_flags(irq, IRQF_VALID);
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001306 return 0;
1307
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001308chip_error:
1309 gpiochip_remove(pctl->chip);
1310pctrl_error:
1311 pinctrl_unregister(pctl->pctl_dev);
1312 return ret;
1313}
1314
1315MODULE_LICENSE("GPL");
1316MODULE_DESCRIPTION("MediaTek Pinctrl Driver");
1317MODULE_AUTHOR("Hongzhou Yang <hongzhou.yang@mediatek.com>");