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