blob: 08a794d8c9745c4347256957f4298619741cfc77 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +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/driver.h>
18#include <linux/of.h>
19#include <linux/of_address.h>
20#include <linux/of_device.h>
21#include <linux/of_irq.h>
22#include <linux/pinctrl/consumer.h>
23#include <linux/pinctrl/machine.h>
24#include <linux/pinctrl/pinconf.h>
25#include <linux/pinctrl/pinconf-generic.h>
26#include <linux/pinctrl/pinctrl.h>
27#include <linux/pinctrl/pinmux.h>
28#include <linux/platform_device.h>
29#include <linux/slab.h>
30#include <linux/bitops.h>
31#include <linux/regmap.h>
32#include <linux/mfd/syscon.h>
33#include <linux/delay.h>
34#include <linux/interrupt.h>
35#include <linux/pm.h>
36#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#define GPIO_MODE_PREFIX "GPIO"
46
47static const char * const mtk_gpio_functions[] = {
48 "func0", "func1", "func2", "func3",
49 "func4", "func5", "func6", "func7",
50 "func8", "func9", "func10", "func11",
51 "func12", "func13", "func14", "func15",
52};
53static const struct mtk_pin_info *mtk_pinctrl_get_gpio_array(int pin, int size,
54 const struct mtk_pin_info pArray[])
55{
56 int i = 0;
57
58 for (i = 0; i < size; i++) {
59 if (pin == pArray[i].pin)
60 return &pArray[i];
61 }
62 return NULL;
63}
64
65int mtk_pinctrl_set_gpio_value(struct mtk_pinctrl *pctl, int pin,
66 bool value, int size, const struct mtk_pin_info pin_info[])
67{
68 unsigned int reg_bit, reg_set_addr, reg_rst_addr;
69 const struct mtk_pin_info *spec_pin_info;
70 struct regmap *regmap;
71 unsigned char port_align;
72 unsigned char bit_width;
73 unsigned int mask, reg_value;
74
75 spec_pin_info = mtk_pinctrl_get_gpio_array(pin, size, pin_info);
76
77 if (spec_pin_info != NULL) {
78 port_align = pctl->devdata->port_align;
79 reg_set_addr = spec_pin_info->offset + port_align;
80 reg_rst_addr = spec_pin_info->offset + (port_align << 1);
81 reg_bit = BIT(spec_pin_info->bit);
82 regmap = pctl->regmap[spec_pin_info->ip_num];
83 reg_value = value << spec_pin_info->bit;
84 bit_width = spec_pin_info->width;
85 mask = (BIT(bit_width) - 1) << spec_pin_info->bit;
86 return regmap_update_bits(regmap,
87 spec_pin_info->offset, mask, reg_value);
88 } else {
89 return -EPERM;
90 }
91 return 0;
92}
93
94int mtk_pinctrl_update_gpio_value(struct mtk_pinctrl *pctl, int pin,
95 unsigned char value, int size, const struct mtk_pin_info pin_info[])
96{
97 unsigned int reg_update_addr;
98 unsigned int mask, reg_value;
99 const struct mtk_pin_info *spec_update_pin;
100 struct regmap *regmap;
101 unsigned char bit_width;
102
103 spec_update_pin = mtk_pinctrl_get_gpio_array(pin, size, pin_info);
104
105 if (spec_update_pin != NULL) {
106 reg_update_addr = spec_update_pin->offset;
107 regmap = pctl->regmap[spec_update_pin->ip_num];
108 reg_value = value << spec_update_pin->bit;
109 bit_width = spec_update_pin->width;
110 mask = (BIT(bit_width) - 1) << spec_update_pin->bit;
111 return regmap_update_bits(regmap,
112 reg_update_addr, mask, reg_value);
113 } else {
114 return -EPERM;
115 }
116
117 return 0;
118}
119
120int mtk_pinctrl_get_gpio_value(struct mtk_pinctrl *pctl,
121 int pin, int size, const struct mtk_pin_info pin_info[])
122{
123 unsigned int reg_value, reg_get_addr;
124 const struct mtk_pin_info *spec_pin_info;
125 struct regmap *regmap;
126 unsigned char bit_width, reg_bit;
127
128 spec_pin_info = mtk_pinctrl_get_gpio_array(pin, size, pin_info);
129
130 if (spec_pin_info != NULL) {
131 reg_get_addr = spec_pin_info->offset;
132 bit_width = spec_pin_info->width;
133 reg_bit = spec_pin_info->bit;
134 regmap = pctl->regmap[spec_pin_info->ip_num];
135 regmap_read(regmap, reg_get_addr, &reg_value);
136 return ((reg_value >> reg_bit) & (BIT(bit_width) - 1));
137 } else {
138 return -EPERM;
139 }
140
141 return 0;
142}
143
144/*
145 * There are two base address for pull related configuration
146 * in mt8135, and different GPIO pins use different base address.
147 * When pin number greater than type1_start and less than type1_end,
148 * should use the second base address.
149 */
150static struct regmap *mtk_get_regmap(struct mtk_pinctrl *pctl,
151 unsigned long pin)
152{
153 if (pin >= pctl->devdata->type1_start && pin < pctl->devdata->type1_end)
154 return pctl->regmap2;
155 return pctl->regmap1;
156}
157
158static unsigned int mtk_get_port(struct mtk_pinctrl *pctl, unsigned long pin)
159{
160 /* Different SoC has different mask and port shift. */
161 return ((pin >> pctl->devdata->port_pin_shf) & pctl->devdata->port_mask)
162 << pctl->devdata->port_shf;
163}
164
165static int mtk_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
166 struct pinctrl_gpio_range *range, unsigned offset,
167 bool input)
168{
169 unsigned int reg_addr;
170 unsigned int bit;
171 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
172
173 reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
174 bit = BIT(offset & pctl->devdata->port_mask);
175
176 if (pctl->devdata->spec_dir_set)
177 pctl->devdata->spec_dir_set(&reg_addr, offset);
178
179 if (input)
180 /* Different SoC has different alignment offset. */
181 reg_addr = CLR_ADDR(reg_addr, pctl);
182 else
183 reg_addr = SET_ADDR(reg_addr, pctl);
184
185 regmap_write(mtk_get_regmap(pctl, offset), reg_addr, bit);
186 return 0;
187}
188
189static void mtk_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
190{
191 unsigned int reg_addr;
192 unsigned int bit;
193 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
194
195 reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dout_offset;
196 bit = BIT(offset & pctl->devdata->port_mask);
197
198 if (value)
199 reg_addr = SET_ADDR(reg_addr, pctl);
200 else
201 reg_addr = CLR_ADDR(reg_addr, pctl);
202
203 regmap_write(mtk_get_regmap(pctl, offset), reg_addr, bit);
204}
205
206static int mtk_pconf_set_ies_smt(struct mtk_pinctrl *pctl, unsigned pin,
207 int value, enum pin_config_param arg)
208{
209 unsigned int reg_addr, offset;
210 unsigned int bit;
211
212 /**
213 * Due to some soc are not support ies/smt config, add this special
214 * control to handle it.
215 */
216 if (!pctl->devdata->spec_ies_smt_set &&
217 pctl->devdata->ies_offset == MTK_PINCTRL_NOT_SUPPORT &&
218 arg == PIN_CONFIG_INPUT_ENABLE)
219 return -EINVAL;
220
221 if (!pctl->devdata->spec_ies_smt_set &&
222 pctl->devdata->smt_offset == MTK_PINCTRL_NOT_SUPPORT &&
223 arg == PIN_CONFIG_INPUT_SCHMITT_ENABLE)
224 return -EINVAL;
225
226 /*
227 * Due to some pins are irregular, their input enable and smt
228 * control register are discontinuous, so we need this special handle.
229 */
230 if (pctl->devdata->spec_ies_smt_set) {
231 return pctl->devdata->spec_ies_smt_set(pctl,
232 mtk_get_regmap(pctl, pin), pin,
233 pctl->devdata->port_align, value, arg);
234 }
235
236 bit = BIT(pin & 0xf);
237
238 if (arg == PIN_CONFIG_INPUT_ENABLE)
239 offset = pctl->devdata->ies_offset;
240 else
241 offset = pctl->devdata->smt_offset;
242
243 if (value)
244 reg_addr = SET_ADDR(mtk_get_port(pctl, pin) + offset, pctl);
245 else
246 reg_addr = CLR_ADDR(mtk_get_port(pctl, pin) + offset, pctl);
247
248 regmap_write(mtk_get_regmap(pctl, pin), reg_addr, bit);
249 return 0;
250}
251
252int mtk_pconf_spec_set_ies_smt_range(struct regmap *regmap,
253 const struct mtk_pin_ies_smt_set *ies_smt_infos, unsigned int info_num,
254 unsigned int pin, unsigned char align, int value)
255{
256 unsigned int i, reg_addr, bit;
257
258 for (i = 0; i < info_num; i++) {
259 if (pin >= ies_smt_infos[i].start &&
260 pin <= ies_smt_infos[i].end) {
261 break;
262 }
263 }
264
265 if (i == info_num)
266 return -EINVAL;
267
268 if (value)
269 reg_addr = ies_smt_infos[i].offset + align;
270 else
271 reg_addr = ies_smt_infos[i].offset + (align << 1);
272
273 bit = BIT(ies_smt_infos[i].bit);
274 regmap_write(regmap, reg_addr, bit);
275 return 0;
276}
277
278static const struct mtk_pin_drv_grp *mtk_find_pin_drv_grp_by_pin(
279 struct mtk_pinctrl *pctl, unsigned long pin) {
280 int i;
281
282 for (i = 0; i < pctl->devdata->n_pin_drv_grps; i++) {
283 const struct mtk_pin_drv_grp *pin_drv =
284 pctl->devdata->pin_drv_grp + i;
285 if (pin == pin_drv->pin)
286 return pin_drv;
287 }
288
289 return NULL;
290}
291
292static int mtk_pinctrl_set_gpio_driving(struct mtk_pinctrl *pctl,
293 int pin, unsigned char driving)
294{
295 return mtk_pinctrl_update_gpio_value(pctl, pin, driving,
296 pctl->devdata->n_pin_drv, pctl->devdata->pin_drv_grps);
297}
298
299static int mtk_pconf_set_driving(struct mtk_pinctrl *pctl,
300 unsigned int pin, unsigned char driving)
301{
302 const struct mtk_pin_drv_grp *pin_drv;
303 unsigned int val;
304 unsigned int bits, mask, shift;
305 const struct mtk_drv_group_desc *drv_grp;
306
307 if (pctl->devdata->pin_drv_grps) {
308 return mtk_pinctrl_set_gpio_driving(pctl,
309 pin, driving);
310 }
311
312 if (pin >= pctl->devdata->npins)
313 return -EINVAL;
314
315 pin_drv = mtk_find_pin_drv_grp_by_pin(pctl, pin);
316 if (!pin_drv || pin_drv->grp > pctl->devdata->n_grp_cls)
317 return -EINVAL;
318
319 drv_grp = pctl->devdata->grp_desc + pin_drv->grp;
320 if (driving >= drv_grp->min_drv && driving <= drv_grp->max_drv
321 && !(driving % drv_grp->step)) {
322 val = driving / drv_grp->step - 1;
323 bits = drv_grp->high_bit - drv_grp->low_bit + 1;
324 mask = BIT(bits) - 1;
325 shift = pin_drv->bit + drv_grp->low_bit;
326 mask <<= shift;
327 val <<= shift;
328 return regmap_update_bits(mtk_get_regmap(pctl, pin),
329 pin_drv->offset, mask, val);
330 }
331
332 return -EINVAL;
333}
334
335int mtk_pctrl_spec_pull_set_samereg(struct mtk_pinctrl *pctl,
336 struct regmap *regmap,
337 const struct mtk_pin_spec_pupd_set_samereg *pupd_infos,
338 unsigned int info_num, unsigned int pin,
339 unsigned char align, bool isup, unsigned int r1r0)
340{
341 unsigned int i;
342 unsigned int reg_pupd, reg_set, reg_rst;
343 unsigned int bit_pupd, bit_r0, bit_r1;
344 const struct mtk_pin_spec_pupd_set_samereg *spec_pupd_pin;
345 bool find = false;
346
347 for (i = 0; i < info_num; i++) {
348 if (pin == pupd_infos[i].pin) {
349 find = true;
350 break;
351 }
352 }
353
354 if (!find)
355 return -EINVAL;
356
357 spec_pupd_pin = pupd_infos + i;
358 reg_set = spec_pupd_pin->offset + align;
359 reg_rst = spec_pupd_pin->offset + (align << 1);
360
361 if (isup)
362 reg_pupd = reg_rst;
363 else
364 reg_pupd = reg_set;
365
366 if (spec_pupd_pin->ip_num != 0)
367 regmap = pctl->regmap[spec_pupd_pin->ip_num];
368 bit_pupd = BIT(spec_pupd_pin->pupd_bit);
369 regmap_write(regmap, reg_pupd, bit_pupd);
370
371 bit_r0 = BIT(spec_pupd_pin->r0_bit);
372 bit_r1 = BIT(spec_pupd_pin->r1_bit);
373
374 switch (r1r0) {
375 case MTK_PUPD_SET_R1R0_00:
376 regmap_write(regmap, reg_rst, bit_r0);
377 regmap_write(regmap, reg_rst, bit_r1);
378 break;
379 case MTK_PUPD_SET_R1R0_01:
380 regmap_write(regmap, reg_set, bit_r0);
381 regmap_write(regmap, reg_rst, bit_r1);
382 break;
383 case MTK_PUPD_SET_R1R0_10:
384 regmap_write(regmap, reg_rst, bit_r0);
385 regmap_write(regmap, reg_set, bit_r1);
386 break;
387 case MTK_PUPD_SET_R1R0_11:
388 regmap_write(regmap, reg_set, bit_r0);
389 regmap_write(regmap, reg_set, bit_r1);
390 break;
391 default:
392 return -EINVAL;
393 }
394
395 return 0;
396}
397
398static int mtk_pconf_set_pull_select(struct mtk_pinctrl *pctl,
399 unsigned int pin, bool enable, bool isup, unsigned int arg)
400{
401 unsigned int bit;
402 unsigned int reg_pullen, reg_pullsel, r1r0;
403 int ret;
404
405 /* Some pins' pull setting are very different,
406 * they have separate pull up/down bit, R0 and R1
407 * resistor bit, so we need this special handle.
408 */
409 if (pctl->devdata->spec_pull_set) {
410 /* For special pins, bias-disable is set by R1R0,
411 * the parameter should be "MTK_PUPD_SET_R1R0_00".
412 */
413 r1r0 = enable ? arg : MTK_PUPD_SET_R1R0_00;
414 ret = pctl->devdata->spec_pull_set(pctl,
415 mtk_get_regmap(pctl, pin), pin,
416 pctl->devdata->port_align, isup, r1r0);
417 if (!ret)
418 return 0;
419 }
420
421 if (pctl->devdata->pin_pullen_grps ||
422 pctl->devdata->pin_pullsel_grps) {
423 mtk_pinctrl_set_gpio_value(pctl, pin, enable,
424 pctl->devdata->n_pin_pullen,
425 pctl->devdata->pin_pullen_grps);
426 mtk_pinctrl_set_gpio_value(pctl, pin, isup,
427 pctl->devdata->n_pin_pullsel,
428 pctl->devdata->pin_pullsel_grps);
429 return 0;
430 }
431
432 /* For generic pull config, default arg value should be 0 or 1. */
433 if (arg != 0 && arg != 1) {
434 dev_err(pctl->dev, "invalid pull-up argument %d on pin %d .\n",
435 arg, pin);
436 return -EINVAL;
437 }
438
439 bit = BIT(pin & pctl->devdata->port_mask);
440 if (enable)
441 reg_pullen = SET_ADDR(mtk_get_port(pctl, pin) +
442 pctl->devdata->pullen_offset, pctl);
443 else
444 reg_pullen = CLR_ADDR(mtk_get_port(pctl, pin) +
445 pctl->devdata->pullen_offset, pctl);
446
447 if (isup)
448 reg_pullsel = SET_ADDR(mtk_get_port(pctl, pin) +
449 pctl->devdata->pullsel_offset, pctl);
450 else
451 reg_pullsel = CLR_ADDR(mtk_get_port(pctl, pin) +
452 pctl->devdata->pullsel_offset, pctl);
453
454 regmap_write(mtk_get_regmap(pctl, pin), reg_pullen, bit);
455 regmap_write(mtk_get_regmap(pctl, pin), reg_pullsel, bit);
456 return 0;
457}
458
459static int mtk_pconf_parse_conf(struct pinctrl_dev *pctldev,
460 unsigned int pin, enum pin_config_param param,
461 enum pin_config_param arg)
462{
463 int ret = 0;
464 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
465
466 switch (param) {
467 case PIN_CONFIG_BIAS_DISABLE:
468 ret = mtk_pconf_set_pull_select(pctl, pin, false, false, arg);
469 break;
470 case PIN_CONFIG_BIAS_PULL_UP:
471 ret = mtk_pconf_set_pull_select(pctl, pin, true, true, arg);
472 break;
473 case PIN_CONFIG_BIAS_PULL_DOWN:
474 ret = mtk_pconf_set_pull_select(pctl, pin, true, false, arg);
475 break;
476 case PIN_CONFIG_INPUT_ENABLE:
477 mtk_pmx_gpio_set_direction(pctldev, NULL, pin, true);
478 ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
479 break;
480 case PIN_CONFIG_OUTPUT:
481 mtk_gpio_set(pctl->chip, pin, arg);
482 ret = mtk_pmx_gpio_set_direction(pctldev, NULL, pin, false);
483 break;
484 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
485 mtk_pmx_gpio_set_direction(pctldev, NULL, pin, true);
486 ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
487 break;
488 case PIN_CONFIG_DRIVE_STRENGTH:
489 ret = mtk_pconf_set_driving(pctl, pin, arg);
490 break;
491 default:
492 ret = -EINVAL;
493 }
494
495 return ret;
496}
497
498static int mtk_pconf_group_get(struct pinctrl_dev *pctldev,
499 unsigned group,
500 unsigned long *config)
501{
502 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
503
504 *config = pctl->groups[group].config;
505
506 return 0;
507}
508
509static int mtk_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
510 unsigned long *configs, unsigned num_configs)
511{
512 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
513 struct mtk_pinctrl_group *g = &pctl->groups[group];
514 int i, ret;
515
516 for (i = 0; i < num_configs; i++) {
517 ret = mtk_pconf_parse_conf(pctldev, g->pin,
518 pinconf_to_config_param(configs[i]),
519 pinconf_to_config_argument(configs[i]));
520 if (ret < 0)
521 return ret;
522
523 g->config = configs[i];
524 }
525
526 return 0;
527}
528
529static const struct pinconf_ops mtk_pconf_ops = {
530 .pin_config_group_get = mtk_pconf_group_get,
531 .pin_config_group_set = mtk_pconf_group_set,
532};
533
534static struct mtk_pinctrl_group *
535mtk_pctrl_find_group_by_pin(struct mtk_pinctrl *pctl, u32 pin)
536{
537 int i;
538
539 for (i = 0; i < pctl->ngroups; i++) {
540 struct mtk_pinctrl_group *grp = pctl->groups + i;
541
542 if (grp->pin == pin)
543 return grp;
544 }
545
546 return NULL;
547}
548
549static const struct mtk_desc_function *mtk_pctrl_find_function_by_pin(
550 struct mtk_pinctrl *pctl, u32 pin_num, u32 fnum)
551{
552 const struct mtk_desc_pin *pin = pctl->devdata->pins + pin_num;
553 const struct mtk_desc_function *func = pin->functions;
554
555 while (func && func->name) {
556 if (func->muxval == fnum)
557 return func;
558 func++;
559 }
560
561 return NULL;
562}
563
564static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl *pctl,
565 u32 pin_num, u32 fnum)
566{
567 int i;
568
569 for (i = 0; i < pctl->devdata->npins; i++) {
570 const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
571
572 if (pin->pin.number == pin_num) {
573 const struct mtk_desc_function *func =
574 pin->functions;
575
576 while (func && func->name) {
577 if (func->muxval == fnum)
578 return true;
579 func++;
580 }
581
582 break;
583 }
584 }
585
586 return false;
587}
588
589static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl *pctl,
590 u32 pin, u32 fnum, struct mtk_pinctrl_group *grp,
591 struct pinctrl_map **map, unsigned *reserved_maps,
592 unsigned *num_maps)
593{
594 bool ret;
595
596 if (*num_maps == *reserved_maps)
597 return -ENOSPC;
598
599 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
600 (*map)[*num_maps].data.mux.group = grp->name;
601
602 ret = mtk_pctrl_is_function_valid(pctl, pin, fnum);
603 if (!ret) {
604 dev_err(pctl->dev, "invalid function %d on pin %d .\n",
605 fnum, pin);
606 return -EINVAL;
607 }
608
609 (*map)[*num_maps].data.mux.function = mtk_gpio_functions[fnum];
610 (*num_maps)++;
611
612 return 0;
613}
614
615static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
616 struct device_node *node,
617 struct pinctrl_map **map,
618 unsigned *reserved_maps,
619 unsigned *num_maps)
620{
621 struct property *pins;
622 u32 pinfunc, pin, func;
623 int num_pins, num_funcs, maps_per_pin;
624 unsigned long *configs;
625 unsigned int num_configs;
626 bool has_config = false;
627 int i, err;
628 unsigned reserve = 0;
629 struct mtk_pinctrl_group *grp;
630 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
631
632 pins = of_find_property(node, "pinmux", NULL);
633 if (!pins) {
634 dev_err(pctl->dev, "missing pins property in node %s .\n",
635 node->name);
636 return -EINVAL;
637 }
638
639 err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
640 &num_configs);
641 if (err)
642 return err;
643
644 if (num_configs)
645 has_config = true;
646
647 num_pins = pins->length / sizeof(u32);
648 num_funcs = num_pins;
649 maps_per_pin = 0;
650 if (num_funcs)
651 maps_per_pin++;
652 if (has_config && num_pins >= 1)
653 maps_per_pin++;
654
655 if (!num_pins || !maps_per_pin) {
656 err = -EINVAL;
657 goto exit;
658 }
659
660 reserve = num_pins * maps_per_pin;
661
662 err = pinctrl_utils_reserve_map(pctldev, map,
663 reserved_maps, num_maps, reserve);
664 if (err < 0)
665 goto exit;
666
667 for (i = 0; i < num_pins; i++) {
668 err = of_property_read_u32_index(node, "pinmux",
669 i, &pinfunc);
670 if (err)
671 goto exit;
672
673 pin = MTK_GET_PIN_NO(pinfunc);
674 func = MTK_GET_PIN_FUNC(pinfunc);
675
676 if (pin >= pctl->devdata->npins ||
677 func >= ARRAY_SIZE(mtk_gpio_functions)) {
678 dev_err(pctl->dev, "invalid pins value.\n");
679 err = -EINVAL;
680 goto exit;
681 }
682
683 grp = mtk_pctrl_find_group_by_pin(pctl, pin);
684 if (!grp) {
685 dev_err(pctl->dev, "unable to match pin %d to group\n",
686 pin);
687 err = -EINVAL;
688 goto exit;
689 }
690
691 err = mtk_pctrl_dt_node_to_map_func(pctl, pin, func, grp, map,
692 reserved_maps, num_maps);
693 if (err < 0)
694 goto exit;
695
696 if (has_config) {
697 err = pinctrl_utils_add_map_configs(pctldev, map,
698 reserved_maps, num_maps, grp->name,
699 configs, num_configs,
700 PIN_MAP_TYPE_CONFIGS_GROUP);
701 if (err < 0)
702 goto exit;
703 }
704 }
705
706 err = 0;
707
708exit:
709 kfree(configs);
710 return err;
711}
712
713static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
714 struct device_node *np_config,
715 struct pinctrl_map **map, unsigned *num_maps)
716{
717 struct device_node *np;
718 unsigned reserved_maps;
719 int ret;
720
721 *map = NULL;
722 *num_maps = 0;
723 reserved_maps = 0;
724
725 for_each_child_of_node(np_config, np) {
726 ret = mtk_pctrl_dt_subnode_to_map(pctldev, np, map,
727 &reserved_maps, num_maps);
728 if (ret < 0) {
729 pinctrl_utils_free_map(pctldev, *map, *num_maps);
730 of_node_put(np);
731 return ret;
732 }
733 }
734
735 return 0;
736}
737
738static int mtk_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
739{
740 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
741
742 return pctl->ngroups;
743}
744
745static const char *mtk_pctrl_get_group_name(struct pinctrl_dev *pctldev,
746 unsigned group)
747{
748 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
749
750 return pctl->groups[group].name;
751}
752
753static int mtk_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
754 unsigned group,
755 const unsigned **pins,
756 unsigned *num_pins)
757{
758 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
759
760 *pins = (unsigned *)&pctl->groups[group].pin;
761 *num_pins = 1;
762
763 return 0;
764}
765
766static const struct pinctrl_ops mtk_pctrl_ops = {
767 .dt_node_to_map = mtk_pctrl_dt_node_to_map,
768 .dt_free_map = pinctrl_utils_free_map,
769 .get_groups_count = mtk_pctrl_get_groups_count,
770 .get_group_name = mtk_pctrl_get_group_name,
771 .get_group_pins = mtk_pctrl_get_group_pins,
772};
773
774static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
775{
776 return ARRAY_SIZE(mtk_gpio_functions);
777}
778
779static const char *mtk_pmx_get_func_name(struct pinctrl_dev *pctldev,
780 unsigned selector)
781{
782 return mtk_gpio_functions[selector];
783}
784
785static int mtk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
786 unsigned function,
787 const char * const **groups,
788 unsigned * const num_groups)
789{
790 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
791
792 *groups = pctl->grp_names;
793 *num_groups = pctl->ngroups;
794
795 return 0;
796}
797
798static int mtk_pmx_set_mode(struct pinctrl_dev *pctldev,
799 unsigned long pin, unsigned long mode)
800{
801 unsigned int reg_addr;
802 unsigned char bit;
803 unsigned int val;
804 unsigned int mask = (1L << GPIO_MODE_BITS) - 1;
805 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
806
807 if (pctl->devdata->spec_pinmux_set) {
808 pctl->devdata->spec_pinmux_set(mtk_get_regmap(pctl, pin),
809 pin, mode);
810 return 0;
811 }
812
813 reg_addr = ((pin / MAX_GPIO_MODE_PER_REG) << pctl->devdata->port_shf)
814 + pctl->devdata->pinmux_offset;
815
816 mode &= mask;
817 bit = pin % MAX_GPIO_MODE_PER_REG;
818 mask <<= (GPIO_MODE_BITS * bit);
819 val = (mode << (GPIO_MODE_BITS * bit));
820 return regmap_update_bits(mtk_get_regmap(pctl, pin),
821 reg_addr, mask, val);
822}
823
824static const struct mtk_desc_pin *
825mtk_find_pin_by_eint_num(struct mtk_pinctrl *pctl, unsigned int eint_num)
826{
827 int i;
828 const struct mtk_desc_pin *pin;
829
830 for (i = 0; i < pctl->devdata->npins; i++) {
831 pin = pctl->devdata->pins + i;
832 if (pin->eint.eintnum == eint_num)
833 return pin;
834 }
835
836 return NULL;
837}
838
839static int mtk_pmx_set_mux(struct pinctrl_dev *pctldev,
840 unsigned function,
841 unsigned group)
842{
843 bool ret;
844 const struct mtk_desc_function *desc;
845 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
846 struct mtk_pinctrl_group *g = pctl->groups + group;
847
848 ret = mtk_pctrl_is_function_valid(pctl, g->pin, function);
849 if (!ret) {
850 dev_err(pctl->dev, "invalid function %d on group %d .\n",
851 function, group);
852 return -EINVAL;
853 }
854
855 desc = mtk_pctrl_find_function_by_pin(pctl, g->pin, function);
856 if (!desc)
857 return -EINVAL;
858 mtk_pmx_set_mode(pctldev, g->pin, desc->muxval);
859 return 0;
860}
861
862static int mtk_pmx_find_gpio_mode(struct mtk_pinctrl *pctl,
863 unsigned offset)
864{
865 const struct mtk_desc_pin *pin = pctl->devdata->pins + offset;
866 const struct mtk_desc_function *func = pin->functions;
867
868 while (func && func->name) {
869 if (!strncmp(func->name, GPIO_MODE_PREFIX,
870 sizeof(GPIO_MODE_PREFIX)-1))
871 return func->muxval;
872 func++;
873 }
874 return -EINVAL;
875}
876
877static int mtk_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
878 struct pinctrl_gpio_range *range,
879 unsigned offset)
880{
881 int muxval;
882 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
883
884 muxval = mtk_pmx_find_gpio_mode(pctl, offset);
885
886 if (muxval < 0) {
887 dev_err(pctl->dev, "invalid gpio pin %d.\n", offset);
888 return -EINVAL;
889 }
890
891 mtk_pmx_set_mode(pctldev, offset, muxval);
892 mtk_pconf_set_ies_smt(pctl, offset, 1, PIN_CONFIG_INPUT_ENABLE);
893
894 return 0;
895}
896
897static const struct pinmux_ops mtk_pmx_ops = {
898 .get_functions_count = mtk_pmx_get_funcs_cnt,
899 .get_function_name = mtk_pmx_get_func_name,
900 .get_function_groups = mtk_pmx_get_func_groups,
901 .set_mux = mtk_pmx_set_mux,
902 .gpio_set_direction = mtk_pmx_gpio_set_direction,
903 .gpio_request_enable = mtk_pmx_gpio_request_enable,
904};
905
906static int mtk_gpio_direction_input(struct gpio_chip *chip,
907 unsigned offset)
908{
909 return pinctrl_gpio_direction_input(chip->base + offset);
910}
911
912static int mtk_gpio_direction_output(struct gpio_chip *chip,
913 unsigned offset, int value)
914{
915 mtk_gpio_set(chip, offset, value);
916 return pinctrl_gpio_direction_output(chip->base + offset);
917}
918
919static int mtk_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
920{
921 unsigned int reg_addr;
922 unsigned int bit;
923 unsigned int read_val = 0;
924
925 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
926
927 reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
928 bit = BIT(offset & 0xf);
929
930 if (pctl->devdata->spec_dir_set)
931 pctl->devdata->spec_dir_set(&reg_addr, offset);
932
933 regmap_read(pctl->regmap1, reg_addr, &read_val);
934 return !(read_val & bit);
935}
936
937static int mtk_gpio_get(struct gpio_chip *chip, unsigned offset)
938{
939 unsigned int reg_addr;
940 unsigned int bit;
941 unsigned int read_val = 0;
942 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
943
944 reg_addr = mtk_get_port(pctl, offset) +
945 pctl->devdata->din_offset;
946
947 bit = BIT(offset & 0xf);
948 regmap_read(pctl->regmap1, reg_addr, &read_val);
949 return !!(read_val & bit);
950}
951
952static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
953{
954 const struct mtk_desc_pin *pin;
955 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
956 int irq;
957
958 pin = pctl->devdata->pins + offset;
959 if (pin->eint.eintnum == NO_EINT_SUPPORT)
960 return -EINVAL;
961
962 irq = irq_find_mapping(pctl->domain, pin->eint.eintnum);
963 if (!irq)
964 return -EINVAL;
965
966 return irq;
967}
968
969static int mtk_pinctrl_irq_request_resources(struct irq_data *d)
970{
971 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
972 const struct mtk_desc_pin *pin;
973 int ret;
974
975 pin = mtk_find_pin_by_eint_num(pctl, d->hwirq);
976
977 if (!pin) {
978 dev_err(pctl->dev, "Can not find pin\n");
979 return -EINVAL;
980 }
981
982 ret = gpiochip_lock_as_irq(pctl->chip, pin->pin.number);
983 if (ret) {
984 dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
985 irqd_to_hwirq(d));
986 return ret;
987 }
988
989 /* set mux to INT mode */
990 mtk_pmx_set_mode(pctl->pctl_dev, pin->pin.number, pin->eint.eintmux);
991 /* set gpio direction to input */
992 mtk_pmx_gpio_set_direction(pctl->pctl_dev, NULL, pin->pin.number, true);
993 /* set input-enable */
994 mtk_pconf_set_ies_smt(pctl, pin->pin.number, 1, PIN_CONFIG_INPUT_ENABLE);
995
996 return 0;
997}
998
999static void mtk_pinctrl_irq_release_resources(struct irq_data *d)
1000{
1001 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1002 const struct mtk_desc_pin *pin;
1003
1004 pin = mtk_find_pin_by_eint_num(pctl, d->hwirq);
1005
1006 if (!pin) {
1007 dev_err(pctl->dev, "Can not find pin\n");
1008 return;
1009 }
1010
1011 gpiochip_unlock_as_irq(pctl->chip, pin->pin.number);
1012}
1013
1014static void __iomem *mtk_eint_get_offset(struct mtk_pinctrl *pctl,
1015 unsigned int eint_num, unsigned int offset)
1016{
1017 unsigned int eint_base = 0;
1018 void __iomem *reg;
1019
1020 if (eint_num >= pctl->devdata->ap_num)
1021 eint_base = pctl->devdata->ap_num;
1022
1023 reg = pctl->eint_reg_base + offset + ((eint_num - eint_base) / 32) * 4;
1024
1025 return reg;
1026}
1027
1028/*
1029 * mtk_can_en_debounce: Check the EINT number is able to enable debounce or not
1030 * @eint_num: the EINT number to setmtk_pinctrl
1031 */
1032static unsigned int mtk_eint_can_en_debounce(struct mtk_pinctrl *pctl,
1033 unsigned int eint_num)
1034{
1035 unsigned int sens;
1036 unsigned int bit = BIT(eint_num % 32);
1037 const struct mtk_eint_offsets *eint_offsets =
1038 &pctl->devdata->eint_offsets;
1039
1040 void __iomem *reg = mtk_eint_get_offset(pctl, eint_num,
1041 eint_offsets->sens);
1042
1043 if (readl(reg) & bit)
1044 sens = MT_LEVEL_SENSITIVE;
1045 else
1046 sens = MT_EDGE_SENSITIVE;
1047
1048 if ((eint_num < pctl->devdata->db_cnt) && (sens != MT_EDGE_SENSITIVE))
1049 return 1;
1050 else
1051 return 0;
1052}
1053
1054/*
1055 * mtk_eint_get_mask: To get the eint mask
1056 * @eint_num: the EINT number to get
1057 */
1058static unsigned int mtk_eint_get_mask(struct mtk_pinctrl *pctl,
1059 unsigned int eint_num)
1060{
1061 unsigned int bit = BIT(eint_num % 32);
1062 const struct mtk_eint_offsets *eint_offsets =
1063 &pctl->devdata->eint_offsets;
1064
1065 void __iomem *reg = mtk_eint_get_offset(pctl, eint_num,
1066 eint_offsets->mask);
1067
1068 return !!(readl(reg) & bit);
1069}
1070
1071static int mtk_eint_flip_edge(struct mtk_pinctrl *pctl, int hwirq)
1072{
1073 int start_level, curr_level;
1074 unsigned int reg_offset;
1075 const struct mtk_eint_offsets *eint_offsets = &(pctl->devdata->eint_offsets);
1076 u32 mask = BIT(hwirq & 0x1f);
1077 u32 port = (hwirq >> 5) & eint_offsets->port_mask;
1078 void __iomem *reg = pctl->eint_reg_base + (port << 2);
1079 const struct mtk_desc_pin *pin;
1080
1081 pin = mtk_find_pin_by_eint_num(pctl, hwirq);
1082 curr_level = mtk_gpio_get(pctl->chip, pin->pin.number);
1083 do {
1084 start_level = curr_level;
1085 if (start_level)
1086 reg_offset = eint_offsets->pol_clr;
1087 else
1088 reg_offset = eint_offsets->pol_set;
1089 writel(mask, reg + reg_offset);
1090
1091 curr_level = mtk_gpio_get(pctl->chip, pin->pin.number);
1092 } while (start_level != curr_level);
1093
1094 return start_level;
1095}
1096
1097static void mtk_eint_mask(struct irq_data *d)
1098{
1099 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1100 const struct mtk_eint_offsets *eint_offsets =
1101 &pctl->devdata->eint_offsets;
1102 u32 mask = BIT(d->hwirq & 0x1f);
1103 void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
1104 eint_offsets->mask_set);
1105
1106 writel(mask, reg);
1107}
1108
1109static void mtk_eint_unmask(struct irq_data *d)
1110{
1111 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1112 const struct mtk_eint_offsets *eint_offsets =
1113 &pctl->devdata->eint_offsets;
1114 u32 mask = BIT(d->hwirq & 0x1f);
1115 void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
1116 eint_offsets->mask_clr);
1117
1118 writel(mask, reg);
1119
1120 if (pctl->eint_dual_edges[d->hwirq])
1121 mtk_eint_flip_edge(pctl, d->hwirq);
1122}
1123
1124static int mtk_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
1125 unsigned debounce)
1126{
1127 struct mtk_pinctrl *pctl = dev_get_drvdata(chip->parent);
1128 int eint_num, virq, eint_offset;
1129 unsigned int set_offset, bit, clr_bit, clr_offset, rst, i, unmask, dbnc;
1130 static const unsigned int debounce_time[] = {500, 1000, 16000, 32000, 64000,
1131 128000, 256000};
1132 const struct mtk_desc_pin *pin;
1133 struct irq_data *d;
1134
1135 pin = pctl->devdata->pins + offset;
1136 if (pin->eint.eintnum == NO_EINT_SUPPORT)
1137 return -EINVAL;
1138
1139 eint_num = pin->eint.eintnum;
1140 virq = irq_find_mapping(pctl->domain, eint_num);
1141 eint_offset = (eint_num % 4) * 8;
1142 d = irq_get_irq_data(virq);
1143
1144 set_offset = (eint_num / 4) * 4 + pctl->devdata->eint_offsets.dbnc_set;
1145 clr_offset = (eint_num / 4) * 4 + pctl->devdata->eint_offsets.dbnc_clr;
1146 if (!mtk_eint_can_en_debounce(pctl, eint_num))
1147 return -ENOSYS;
1148
1149 dbnc = ARRAY_SIZE(debounce_time);
1150 for (i = 0; i < ARRAY_SIZE(debounce_time); i++) {
1151 if (debounce <= debounce_time[i]) {
1152 dbnc = i;
1153 break;
1154 }
1155 }
1156
1157 if (!mtk_eint_get_mask(pctl, eint_num)) {
1158 mtk_eint_mask(d);
1159 unmask = 1;
1160 } else {
1161 unmask = 0;
1162 }
1163
1164 clr_bit = 0xff << eint_offset;
1165 writel(clr_bit, pctl->eint_reg_base + clr_offset);
1166
1167 bit = ((dbnc << EINT_DBNC_SET_DBNC_BITS) | EINT_DBNC_SET_EN) <<
1168 eint_offset;
1169 rst = EINT_DBNC_RST_BIT << eint_offset;
1170 writel(rst | bit, pctl->eint_reg_base + set_offset);
1171
1172 /* Delay a while (more than 2T) to wait for hw debounce counter reset
1173 work correctly */
1174 udelay(1);
1175 if (unmask == 1)
1176 mtk_eint_unmask(d);
1177
1178 return 0;
1179}
1180
1181static int mtk_gpio_set_config(struct gpio_chip *chip, unsigned offset,
1182 unsigned long config)
1183{
1184 u32 debounce;
1185
1186 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
1187 return -ENOTSUPP;
1188
1189 debounce = pinconf_to_config_argument(config);
1190 return mtk_gpio_set_debounce(chip, offset, debounce);
1191}
1192
1193#if defined(CONFIG_PINCTRL_MTK_DEBUG)
1194static ssize_t mtk_gpio_show_pin(struct device *dev,
1195 struct device_attribute *attr, char *buf)
1196{
1197 int len = 0;
1198 int bufLen = (int)PAGE_SIZE;
1199
1200 len += snprintf(buf+len, bufLen-len,
1201 "regmap[index]:[base register]\n");
1202
1203 len += snprintf(buf+len, bufLen-len, "[%d]:[%x]\n", 0, 0x10005000);
1204 len += snprintf(buf+len, bufLen-len, "[%d]:[%x]\n", 1, 0x11f20000);
1205 len += snprintf(buf+len, bufLen-len, "[%d]:[%x]\n", 2, 0x11e80000);
1206 len += snprintf(buf+len, bufLen-len, "[%d]:[%x]\n", 3, 0x11e70000);
1207 len += snprintf(buf+len, bufLen-len, "[%d]:[%x]\n", 4, 0x11e90000);
1208 len += snprintf(buf+len, bufLen-len, "[%d]:[%x]\n", 5, 0x11d30000);
1209 len += snprintf(buf+len, bufLen-len, "[%d]:[%x]\n", 6, 0x11d20000);
1210 len += snprintf(buf+len, bufLen-len, "[%d]:[%x]\n", 7, 0x11c50000);
1211 len += snprintf(buf+len, bufLen-len, "[%d]:[%x]\n", 8, 0x11f30000);
1212
1213 len += snprintf(buf+len, bufLen-len,
1214 "sample cmd: echo wr index offset val > mt_gpio .\n");
1215 len += snprintf(buf+len, bufLen-len,
1216 "sample cmd: echo rr index offset > mt_gpio .\n");
1217
1218 return len;
1219}
1220
1221static ssize_t mtk_gpio_store_pin(struct device *dev,
1222 struct device_attribute *attr, const char *buf, size_t count)
1223{
1224 u32 index, offset, old_val, val, new_val;
1225 struct mtk_pinctrl *pctl = dev_get_drvdata(dev);
1226
1227 if ((strncmp(buf, "wr", 2) == 0)
1228 && (sscanf(buf+2, "%d %x %x", &index, &offset, &val) == 3)) {
1229 if ((index == 0) && (offset >= 0xf60)) {
1230 pr_info("index(%d),offset(0x%x) invalid.\n",
1231 index, offset);
1232 goto err_return;
1233 } else if ((index == 1) && (offset >= 0xc0)) {
1234 pr_info("index(%d),offset(0x%x) invalid.\n",
1235 index, offset);
1236 goto err_return;
1237 } else if ((index == 2) && (offset >= 0xd0)) {
1238 pr_info("index(%d),offset(0x%x) invalid.\n",
1239 index, offset);
1240 goto err_return;
1241 } else if ((index == 3) && (offset >= 0xd0)) {
1242 pr_info("index(%d),offset(0x%x) invalid.\n",
1243 index, offset);
1244 goto err_return;
1245 } else if ((index == 4) && (offset >= 0xb0)) {
1246 pr_info("index(%d),offset(0x%x) invalid.\n",
1247 index, offset);
1248 goto err_return;
1249 } else if ((index == 5) && (offset >= 0xc0)) {
1250 pr_info("index(%d),offset(0x%x) invalid.\n",
1251 index, offset);
1252 goto err_return;
1253 } else if ((index == 6) && (offset >= 0xd0)) {
1254 pr_info("index(%d),offset(0x%x) invalid.\n",
1255 index, offset);
1256 goto err_return;
1257 } else if ((index == 7) && (offset >= 0xc0)) {
1258 pr_info("index(%d),offset(0x%x) invalid.\n",
1259 index, offset);
1260 goto err_return;
1261 } else if ((index == 8) && (offset >= 0xe0)) {
1262 pr_info("index(%d),offset(0x%x) invalid.\n",
1263 index, offset);
1264 goto err_return;
1265 }
1266
1267 regmap_read(pctl->regmap[index], offset, &old_val);
1268 regmap_write(pctl->regmap[index], offset, val);
1269 regmap_read(pctl->regmap[index], offset, &new_val);
1270 pr_info("old_val(0x%x)->new_val(0x%x).\n", old_val, new_val);
1271 } else if ((strncmp(buf, "rr", 2) == 0)
1272 && (sscanf(buf+2, "%d %x", &index, &offset) == 2)) {
1273 if ((index == 0) && (offset >= 0xf60)) {
1274 pr_info("index(%d),offset(0x%x) invalid.\n",
1275 index, offset);
1276 goto err_return;
1277 } else if ((index == 1) && (offset >= 0xc0)) {
1278 pr_info("index(%d),offset(0x%x) invalid.\n",
1279 index, offset);
1280 goto err_return;
1281 } else if ((index == 2) && (offset >= 0xd0)) {
1282 pr_info("index(%d),offset(0x%x) invalid.\n",
1283 index, offset);
1284 goto err_return;
1285 } else if ((index == 3) && (offset >= 0xd0)) {
1286 pr_info("index(%d),offset(0x%x) invalid.\n",
1287 index, offset);
1288 goto err_return;
1289 } else if ((index == 4) && (offset >= 0xb0)) {
1290 pr_info("index(%d),offset(0x%x) invalid.\n",
1291 index, offset);
1292 goto err_return;
1293 } else if ((index == 5) && (offset >= 0xc0)) {
1294 pr_info("index(%d),offset(0x%x) invalid.\n",
1295 index, offset);
1296 goto err_return;
1297 } else if ((index == 6) && (offset >= 0xd0)) {
1298 pr_info("index(%d),offset(0x%x) invalid.\n",
1299 index, offset);
1300 goto err_return;
1301 } else if ((index == 7) && (offset >= 0xc0)) {
1302 pr_info("index(%d),offset(0x%x) invalid.\n",
1303 index, offset);
1304 goto err_return;
1305 } else if ((index == 8) && (offset >= 0xe0)) {
1306 pr_info("index(%d),offset(0x%x) invalid.\n",
1307 index, offset);
1308 goto err_return;
1309 }
1310
1311 regmap_read(pctl->regmap[index], offset, &val);
1312 pr_info("regmap[%d]+offset(%x)=0x%x.\n", index, offset, val);
1313 } else {
1314
1315 }
1316
1317 return (ssize_t)count;
1318
1319err_return:
1320
1321 return (ssize_t)0;
1322}
1323
1324static DEVICE_ATTR(mt_gpio, 0664, mtk_gpio_show_pin, mtk_gpio_store_pin);
1325
1326static struct device_attribute *gpio_attr_list[] = {
1327 &dev_attr_mt_gpio,
1328};
1329
1330static int mtk_gpio_create_attr(struct device *dev)
1331{
1332 int idx, err = 0;
1333 int num = (int)ARRAY_SIZE(gpio_attr_list);
1334
1335 if (dev == NULL)
1336 return -EINVAL;
1337
1338 for (idx = 0; idx < num; idx++) {
1339 err = device_create_file(dev, gpio_attr_list[idx]);
1340 if (err > 0)
1341 break;
1342 }
1343
1344 return err;
1345}
1346#endif
1347
1348static const struct gpio_chip mtk_gpio_chip = {
1349 .owner = THIS_MODULE,
1350 .request = gpiochip_generic_request,
1351 .free = gpiochip_generic_free,
1352 .get_direction = mtk_gpio_get_direction,
1353 .direction_input = mtk_gpio_direction_input,
1354 .direction_output = mtk_gpio_direction_output,
1355 .get = mtk_gpio_get,
1356 .set = mtk_gpio_set,
1357 .to_irq = mtk_gpio_to_irq,
1358 .set_config = mtk_gpio_set_config,
1359 .of_gpio_n_cells = 2,
1360};
1361
1362static int mtk_eint_set_type(struct irq_data *d,
1363 unsigned int type)
1364{
1365 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1366 const struct mtk_eint_offsets *eint_offsets =
1367 &pctl->devdata->eint_offsets;
1368 u32 mask = BIT(d->hwirq & 0x1f);
1369 void __iomem *reg;
1370
1371 if (((type & IRQ_TYPE_EDGE_BOTH) && (type & IRQ_TYPE_LEVEL_MASK)) ||
1372 ((type & IRQ_TYPE_LEVEL_MASK) == IRQ_TYPE_LEVEL_MASK)) {
1373 dev_err(pctl->dev, "Can't configure IRQ%d (EINT%lu) for type 0x%X\n",
1374 d->irq, d->hwirq, type);
1375 return -EINVAL;
1376 }
1377
1378 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
1379 pctl->eint_dual_edges[d->hwirq] = 1;
1380 else
1381 pctl->eint_dual_edges[d->hwirq] = 0;
1382
1383 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING)) {
1384 reg = mtk_eint_get_offset(pctl, d->hwirq,
1385 eint_offsets->pol_clr);
1386 writel(mask, reg);
1387 } else {
1388 reg = mtk_eint_get_offset(pctl, d->hwirq,
1389 eint_offsets->pol_set);
1390 writel(mask, reg);
1391 }
1392
1393 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
1394 reg = mtk_eint_get_offset(pctl, d->hwirq,
1395 eint_offsets->sens_clr);
1396 writel(mask, reg);
1397 } else {
1398 reg = mtk_eint_get_offset(pctl, d->hwirq,
1399 eint_offsets->sens_set);
1400 writel(mask, reg);
1401 }
1402
1403 if (pctl->eint_dual_edges[d->hwirq])
1404 mtk_eint_flip_edge(pctl, d->hwirq);
1405
1406 return 0;
1407}
1408
1409static int mtk_eint_irq_set_wake(struct irq_data *d, unsigned int on)
1410{
1411 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1412 int shift = d->hwirq & 0x1f;
1413 int reg = d->hwirq >> 5;
1414
1415 if (on)
1416 pctl->wake_mask[reg] |= BIT(shift);
1417 else
1418 pctl->wake_mask[reg] &= ~BIT(shift);
1419
1420 return 0;
1421}
1422
1423static void mtk_eint_chip_write_mask(const struct mtk_eint_offsets *chip,
1424 void __iomem *eint_reg_base, u32 *buf)
1425{
1426 int port;
1427 void __iomem *reg;
1428
1429 for (port = 0; port < chip->ports; port++) {
1430 reg = eint_reg_base + (port << 2);
1431 writel_relaxed(~buf[port], reg + chip->mask_set);
1432 writel_relaxed(buf[port], reg + chip->mask_clr);
1433 }
1434}
1435
1436static void mtk_eint_chip_read_mask(const struct mtk_eint_offsets *chip,
1437 void __iomem *eint_reg_base, u32 *buf)
1438{
1439 int port;
1440 void __iomem *reg;
1441
1442 for (port = 0; port < chip->ports; port++) {
1443 reg = eint_reg_base + chip->mask + (port << 2);
1444 buf[port] = ~readl_relaxed(reg);
1445 /* Mask is 0 when irq is enabled, and 1 when disabled. */
1446 }
1447}
1448
1449static int mtk_eint_suspend(struct device *device)
1450{
1451 void __iomem *reg;
1452 struct mtk_pinctrl *pctl = dev_get_drvdata(device);
1453 const struct mtk_eint_offsets *eint_offsets =
1454 &pctl->devdata->eint_offsets;
1455
1456 reg = pctl->eint_reg_base;
1457 mtk_eint_chip_read_mask(eint_offsets, reg, pctl->cur_mask);
1458 mtk_eint_chip_write_mask(eint_offsets, reg, pctl->wake_mask);
1459
1460 return 0;
1461}
1462
1463static int mtk_eint_resume(struct device *device)
1464{
1465 struct mtk_pinctrl *pctl = dev_get_drvdata(device);
1466 const struct mtk_eint_offsets *eint_offsets =
1467 &pctl->devdata->eint_offsets;
1468
1469 mtk_eint_chip_write_mask(eint_offsets,
1470 pctl->eint_reg_base, pctl->cur_mask);
1471
1472 return 0;
1473}
1474
1475const struct dev_pm_ops mtk_eint_pm_ops = {
1476 .suspend_noirq = mtk_eint_suspend,
1477 .resume_noirq = mtk_eint_resume,
1478};
1479
1480static void mtk_eint_ack(struct irq_data *d)
1481{
1482 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1483 const struct mtk_eint_offsets *eint_offsets =
1484 &pctl->devdata->eint_offsets;
1485 u32 mask = BIT(d->hwirq & 0x1f);
1486 void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
1487 eint_offsets->ack);
1488
1489 writel(mask, reg);
1490}
1491
1492static struct irq_chip mtk_pinctrl_irq_chip = {
1493 .name = "mt-eint",
1494 .irq_disable = mtk_eint_mask,
1495 .irq_mask = mtk_eint_mask,
1496 .irq_unmask = mtk_eint_unmask,
1497 .irq_ack = mtk_eint_ack,
1498 .irq_set_type = mtk_eint_set_type,
1499 .irq_set_wake = mtk_eint_irq_set_wake,
1500 .irq_request_resources = mtk_pinctrl_irq_request_resources,
1501 .irq_release_resources = mtk_pinctrl_irq_release_resources,
1502};
1503
1504static unsigned int mtk_eint_init(struct mtk_pinctrl *pctl)
1505{
1506 const struct mtk_eint_offsets *eint_offsets =
1507 &pctl->devdata->eint_offsets;
1508 void __iomem *reg = pctl->eint_reg_base + eint_offsets->dom_en;
1509 unsigned int i;
1510
1511 for (i = 0; i < pctl->devdata->ap_num; i += 32) {
1512 writel(0xffffffff, reg);
1513 reg += 4;
1514 }
1515 return 0;
1516}
1517
1518static inline void
1519mtk_eint_debounce_process(struct mtk_pinctrl *pctl, int index)
1520{
1521 unsigned int rst, ctrl_offset;
1522 unsigned int bit, dbnc;
1523 const struct mtk_eint_offsets *eint_offsets =
1524 &pctl->devdata->eint_offsets;
1525
1526 ctrl_offset = (index / 4) * 4 + eint_offsets->dbnc_ctrl;
1527 dbnc = readl(pctl->eint_reg_base + ctrl_offset);
1528 bit = EINT_DBNC_SET_EN << ((index % 4) * 8);
1529 if ((bit & dbnc) > 0) {
1530 ctrl_offset = (index / 4) * 4 + eint_offsets->dbnc_set;
1531 rst = EINT_DBNC_RST_BIT << ((index % 4) * 8);
1532 writel(rst, pctl->eint_reg_base + ctrl_offset);
1533 }
1534}
1535
1536static void mtk_eint_irq_handler(struct irq_desc *desc)
1537{
1538 struct irq_chip *chip = irq_desc_get_chip(desc);
1539 struct mtk_pinctrl *pctl = irq_desc_get_handler_data(desc);
1540 unsigned int status, eint_num;
1541 int offset, index, virq;
1542 const struct mtk_eint_offsets *eint_offsets =
1543 &pctl->devdata->eint_offsets;
1544 void __iomem *reg = mtk_eint_get_offset(pctl, 0, eint_offsets->stat);
1545 int dual_edges, start_level, curr_level;
1546 const struct mtk_desc_pin *pin;
1547
1548 chained_irq_enter(chip, desc);
1549 for (eint_num = 0;
1550 eint_num < pctl->devdata->ap_num;
1551 eint_num += 32, reg += 4) {
1552 status = readl(reg);
1553 while (status) {
1554 offset = __ffs(status);
1555 index = eint_num + offset;
1556 virq = irq_find_mapping(pctl->domain, index);
1557 status &= ~BIT(offset);
1558
1559 dual_edges = pctl->eint_dual_edges[index];
1560 if (dual_edges) {
1561 /* Clear soft-irq in case we raised it
1562 last time */
1563 writel(BIT(offset), reg - eint_offsets->stat +
1564 eint_offsets->soft_clr);
1565
1566 pin = mtk_find_pin_by_eint_num(pctl, index);
1567 start_level = mtk_gpio_get(pctl->chip,
1568 pin->pin.number);
1569 }
1570
1571 generic_handle_irq(virq);
1572
1573 if (dual_edges) {
1574 curr_level = mtk_eint_flip_edge(pctl, index);
1575
1576 /* If level changed, we might lost one edge
1577 interrupt, raised it through soft-irq */
1578 if (start_level != curr_level)
1579 writel(BIT(offset), reg -
1580 eint_offsets->stat +
1581 eint_offsets->soft_set);
1582 }
1583
1584 if (index < pctl->devdata->db_cnt)
1585 mtk_eint_debounce_process(pctl , index);
1586 }
1587 }
1588 chained_irq_exit(chip, desc);
1589}
1590
1591static int mtk_pctrl_build_state(struct platform_device *pdev)
1592{
1593 struct mtk_pinctrl *pctl = platform_get_drvdata(pdev);
1594 int i;
1595
1596 pctl->ngroups = pctl->devdata->npins;
1597
1598 /* Allocate groups */
1599 pctl->groups = devm_kcalloc(&pdev->dev, pctl->ngroups,
1600 sizeof(*pctl->groups), GFP_KERNEL);
1601 if (!pctl->groups)
1602 return -ENOMEM;
1603
1604 /* We assume that one pin is one group, use pin name as group name. */
1605 pctl->grp_names = devm_kcalloc(&pdev->dev, pctl->ngroups,
1606 sizeof(*pctl->grp_names), GFP_KERNEL);
1607 if (!pctl->grp_names)
1608 return -ENOMEM;
1609
1610 for (i = 0; i < pctl->devdata->npins; i++) {
1611 const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
1612 struct mtk_pinctrl_group *group = pctl->groups + i;
1613
1614 group->name = pin->pin.name;
1615 group->pin = pin->pin.number;
1616
1617 pctl->grp_names[i] = pin->pin.name;
1618 }
1619
1620 return 0;
1621}
1622
1623int mtk_pctrl_init(struct platform_device *pdev,
1624 const struct mtk_pinctrl_devdata *data,
1625 struct regmap *regmap)
1626{
1627 struct pinctrl_pin_desc *pins;
1628 struct mtk_pinctrl *pctl;
1629 struct device_node *np = pdev->dev.of_node, *node;
1630 struct property *prop;
1631 struct resource *res;
1632 int i, ret, irq, ports_buf;
1633
1634 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
1635 if (!pctl)
1636 return -ENOMEM;
1637
1638 platform_set_drvdata(pdev, pctl);
1639
1640 prop = of_find_property(np, "pins-are-numbered", NULL);
1641 if (!prop) {
1642 dev_err(&pdev->dev, "only support pins-are-numbered format\n");
1643 return -EINVAL;
1644 }
1645
1646 node = of_parse_phandle(np, "mediatek,pctl-regmap", 0);
1647 if (node) {
1648 pctl->regmap1 = syscon_node_to_regmap(node);
1649 if (IS_ERR(pctl->regmap1))
1650 return PTR_ERR(pctl->regmap1);
1651 } else if (regmap) {
1652 pctl->regmap1 = regmap;
1653 } else {
1654 dev_err(&pdev->dev, "Pinctrl node has not register regmap.\n");
1655 return -EINVAL;
1656 }
1657
1658 /* Only 8135 has two base addr, other SoCs have only one. */
1659 node = of_parse_phandle(np, "mediatek,pctl-regmap", 1);
1660 if (node) {
1661 pctl->regmap2 = syscon_node_to_regmap(node);
1662 if (IS_ERR(pctl->regmap2))
1663 return PTR_ERR(pctl->regmap2);
1664 }
1665
1666 if (data->regmap_num > 2) {
1667 for (i = 0; i <= data->regmap_num; i++) {
1668 node = of_parse_phandle(np, "mediatek,pctl-regmap", i);
1669 if (node) {
1670 pctl->regmap[i] = syscon_node_to_regmap(node);
1671 if (IS_ERR(pctl->regmap[i]))
1672 return PTR_ERR(pctl->regmap[i]);
1673 }
1674 }
1675 }
1676
1677 pctl->devdata = data;
1678 ret = mtk_pctrl_build_state(pdev);
1679 if (ret) {
1680 dev_err(&pdev->dev, "build state failed: %d\n", ret);
1681 return -EINVAL;
1682 }
1683
1684 pins = devm_kcalloc(&pdev->dev, pctl->devdata->npins, sizeof(*pins),
1685 GFP_KERNEL);
1686 if (!pins)
1687 return -ENOMEM;
1688
1689 for (i = 0; i < pctl->devdata->npins; i++)
1690 pins[i] = pctl->devdata->pins[i].pin;
1691
1692 pctl->pctl_desc.name = dev_name(&pdev->dev);
1693 pctl->pctl_desc.owner = THIS_MODULE;
1694 pctl->pctl_desc.pins = pins;
1695 pctl->pctl_desc.npins = pctl->devdata->npins;
1696 pctl->pctl_desc.confops = &mtk_pconf_ops;
1697 pctl->pctl_desc.pctlops = &mtk_pctrl_ops;
1698 pctl->pctl_desc.pmxops = &mtk_pmx_ops;
1699 pctl->dev = &pdev->dev;
1700
1701 pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, &pctl->pctl_desc,
1702 pctl);
1703 if (IS_ERR(pctl->pctl_dev)) {
1704 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
1705 return PTR_ERR(pctl->pctl_dev);
1706 }
1707
1708 pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
1709 if (!pctl->chip)
1710 return -ENOMEM;
1711
1712 *pctl->chip = mtk_gpio_chip;
1713 pctl->chip->ngpio = pctl->devdata->npins;
1714 pctl->chip->label = dev_name(&pdev->dev);
1715 pctl->chip->parent = &pdev->dev;
1716 pctl->chip->base = -1;
1717
1718 ret = gpiochip_add_data(pctl->chip, pctl);
1719 if (ret)
1720 return -EINVAL;
1721
1722 /* Register the GPIO to pin mappings. */
1723 ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
1724 0, 0, pctl->devdata->npins);
1725 if (ret) {
1726 ret = -EINVAL;
1727 goto chip_error;
1728 }
1729
1730#if defined(CONFIG_PINCTRL_MTK_DEBUG)
1731 ret = mtk_gpio_create_attr(&pdev->dev);
1732 if (ret < 0)
1733 pr_warn("mtk_gpio create attribute error\n");
1734#endif
1735
1736 if (!of_property_read_bool(np, "interrupt-controller"))
1737 return 0;
1738
1739 /* Get EINT register base from dts. */
1740 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1741 if (!res) {
1742 dev_err(&pdev->dev, "Unable to get Pinctrl resource\n");
1743 ret = -EINVAL;
1744 goto chip_error;
1745 }
1746
1747 pctl->eint_reg_base = devm_ioremap_resource(&pdev->dev, res);
1748 if (IS_ERR(pctl->eint_reg_base)) {
1749 ret = -EINVAL;
1750 goto chip_error;
1751 }
1752
1753 ports_buf = pctl->devdata->eint_offsets.ports;
1754 pctl->wake_mask = devm_kcalloc(&pdev->dev, ports_buf,
1755 sizeof(*pctl->wake_mask), GFP_KERNEL);
1756 if (!pctl->wake_mask) {
1757 ret = -ENOMEM;
1758 goto chip_error;
1759 }
1760
1761 pctl->cur_mask = devm_kcalloc(&pdev->dev, ports_buf,
1762 sizeof(*pctl->cur_mask), GFP_KERNEL);
1763 if (!pctl->cur_mask) {
1764 ret = -ENOMEM;
1765 goto chip_error;
1766 }
1767
1768 pctl->eint_dual_edges = devm_kcalloc(&pdev->dev, pctl->devdata->ap_num,
1769 sizeof(int), GFP_KERNEL);
1770 if (!pctl->eint_dual_edges) {
1771 ret = -ENOMEM;
1772 goto chip_error;
1773 }
1774
1775 irq = irq_of_parse_and_map(np, 0);
1776 if (!irq) {
1777 dev_err(&pdev->dev, "couldn't parse and map irq\n");
1778 ret = -EINVAL;
1779 goto chip_error;
1780 }
1781
1782 pctl->domain = irq_domain_add_linear(np,
1783 pctl->devdata->ap_num, &irq_domain_simple_ops, NULL);
1784 if (!pctl->domain) {
1785 dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
1786 ret = -ENOMEM;
1787 goto chip_error;
1788 }
1789
1790 mtk_eint_init(pctl);
1791 for (i = 0; i < pctl->devdata->ap_num; i++) {
1792 int virq = irq_create_mapping(pctl->domain, i);
1793
1794 irq_set_chip_and_handler(virq, &mtk_pinctrl_irq_chip,
1795 handle_level_irq);
1796 irq_set_chip_data(virq, pctl);
1797 }
1798
1799 irq_set_chained_handler_and_data(irq, mtk_eint_irq_handler, pctl);
1800 return 0;
1801
1802chip_error:
1803 gpiochip_remove(pctl->chip);
1804 return ret;
1805}