blob: 578897a1c45fc8e469b493c530b470d45acc47cc [file] [log] [blame]
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001/*
2 * mt65xx pinctrl driver based on Allwinner A1X pinctrl driver.
3 * Copyright (c) 2014 MediaTek Inc.
4 * Author: Hongzhou.Yang <hongzhou.yang@mediatek.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/io.h>
Linus Walleij11aa6792015-12-08 22:06:23 +010017#include <linux/gpio/driver.h>
Hongzhou Yanga6df4102015-01-21 13:28:15 +080018#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_address.h>
21#include <linux/of_device.h>
22#include <linux/of_irq.h>
23#include <linux/pinctrl/consumer.h>
24#include <linux/pinctrl/machine.h>
25#include <linux/pinctrl/pinconf.h>
26#include <linux/pinctrl/pinconf-generic.h>
27#include <linux/pinctrl/pinctrl.h>
28#include <linux/pinctrl/pinmux.h>
29#include <linux/platform_device.h>
30#include <linux/slab.h>
31#include <linux/bitops.h>
32#include <linux/regmap.h>
33#include <linux/mfd/syscon.h>
Maoguang Mengd9819eb2015-01-21 13:28:16 +080034#include <linux/delay.h>
Hongzhou Yang30f010f2015-01-27 15:13:55 +080035#include <linux/interrupt.h>
Maoguang Meng58a5e1b2015-08-14 16:38:06 +080036#include <linux/pm.h>
Hongzhou Yanga6df4102015-01-21 13:28:15 +080037#include <dt-bindings/pinctrl/mt65xx.h>
38
39#include "../core.h"
40#include "../pinconf.h"
41#include "../pinctrl-utils.h"
42#include "pinctrl-mtk-common.h"
43
44#define MAX_GPIO_MODE_PER_REG 5
45#define GPIO_MODE_BITS 3
Biao Huang59ee9c92016-02-03 09:24:46 +080046#define GPIO_MODE_PREFIX "GPIO"
Hongzhou Yanga6df4102015-01-21 13:28:15 +080047
48static const char * const mtk_gpio_functions[] = {
49 "func0", "func1", "func2", "func3",
50 "func4", "func5", "func6", "func7",
Biao Huang148b95e2016-01-27 09:24:42 +080051 "func8", "func9", "func10", "func11",
52 "func12", "func13", "func14", "func15",
Hongzhou Yanga6df4102015-01-21 13:28:15 +080053};
54
55/*
56 * There are two base address for pull related configuration
57 * in mt8135, and different GPIO pins use different base address.
58 * When pin number greater than type1_start and less than type1_end,
59 * should use the second base address.
60 */
61static struct regmap *mtk_get_regmap(struct mtk_pinctrl *pctl,
62 unsigned long pin)
63{
64 if (pin >= pctl->devdata->type1_start && pin < pctl->devdata->type1_end)
65 return pctl->regmap2;
66 return pctl->regmap1;
67}
68
69static unsigned int mtk_get_port(struct mtk_pinctrl *pctl, unsigned long pin)
70{
71 /* Different SoC has different mask and port shift. */
72 return ((pin >> 4) & pctl->devdata->port_mask)
73 << pctl->devdata->port_shf;
74}
75
76static int mtk_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
77 struct pinctrl_gpio_range *range, unsigned offset,
78 bool input)
79{
80 unsigned int reg_addr;
81 unsigned int bit;
82 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
83
84 reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
85 bit = BIT(offset & 0xf);
86
Biao Huang148b95e2016-01-27 09:24:42 +080087 if (pctl->devdata->spec_dir_set)
88 pctl->devdata->spec_dir_set(&reg_addr, offset);
89
Hongzhou Yanga6df4102015-01-21 13:28:15 +080090 if (input)
91 /* Different SoC has different alignment offset. */
92 reg_addr = CLR_ADDR(reg_addr, pctl);
93 else
94 reg_addr = SET_ADDR(reg_addr, pctl);
95
96 regmap_write(mtk_get_regmap(pctl, offset), reg_addr, bit);
97 return 0;
98}
99
100static void mtk_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
101{
102 unsigned int reg_addr;
103 unsigned int bit;
Linus Walleij11aa6792015-12-08 22:06:23 +0100104 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800105
106 reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dout_offset;
107 bit = BIT(offset & 0xf);
108
109 if (value)
110 reg_addr = SET_ADDR(reg_addr, pctl);
111 else
112 reg_addr = CLR_ADDR(reg_addr, pctl);
113
114 regmap_write(mtk_get_regmap(pctl, offset), reg_addr, bit);
115}
116
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700117static int mtk_pconf_set_ies_smt(struct mtk_pinctrl *pctl, unsigned pin,
118 int value, enum pin_config_param arg)
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800119{
120 unsigned int reg_addr, offset;
121 unsigned int bit;
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700122
123 /**
124 * Due to some soc are not support ies/smt config, add this special
125 * control to handle it.
126 */
127 if (!pctl->devdata->spec_ies_smt_set &&
128 pctl->devdata->ies_offset == MTK_PINCTRL_NOT_SUPPORT &&
129 arg == PIN_CONFIG_INPUT_ENABLE)
130 return -EINVAL;
131
132 if (!pctl->devdata->spec_ies_smt_set &&
133 pctl->devdata->smt_offset == MTK_PINCTRL_NOT_SUPPORT &&
134 arg == PIN_CONFIG_INPUT_SCHMITT_ENABLE)
135 return -EINVAL;
Hongzhou Yang30f010f2015-01-27 15:13:55 +0800136
137 /*
138 * Due to some pins are irregular, their input enable and smt
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700139 * control register are discontinuous, so we need this special handle.
Hongzhou Yang30f010f2015-01-27 15:13:55 +0800140 */
141 if (pctl->devdata->spec_ies_smt_set) {
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700142 return pctl->devdata->spec_ies_smt_set(mtk_get_regmap(pctl, pin),
143 pin, pctl->devdata->port_align, value, arg);
Hongzhou Yang30f010f2015-01-27 15:13:55 +0800144 }
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800145
146 bit = BIT(pin & 0xf);
147
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700148 if (arg == PIN_CONFIG_INPUT_ENABLE)
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800149 offset = pctl->devdata->ies_offset;
150 else
151 offset = pctl->devdata->smt_offset;
152
153 if (value)
154 reg_addr = SET_ADDR(mtk_get_port(pctl, pin) + offset, pctl);
155 else
156 reg_addr = CLR_ADDR(mtk_get_port(pctl, pin) + offset, pctl);
157
158 regmap_write(mtk_get_regmap(pctl, pin), reg_addr, bit);
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700159 return 0;
160}
161
162int mtk_pconf_spec_set_ies_smt_range(struct regmap *regmap,
163 const struct mtk_pin_ies_smt_set *ies_smt_infos, unsigned int info_num,
164 unsigned int pin, unsigned char align, int value)
165{
166 unsigned int i, reg_addr, bit;
167
168 for (i = 0; i < info_num; i++) {
169 if (pin >= ies_smt_infos[i].start &&
170 pin <= ies_smt_infos[i].end) {
171 break;
172 }
173 }
174
175 if (i == info_num)
176 return -EINVAL;
177
178 if (value)
179 reg_addr = ies_smt_infos[i].offset + align;
180 else
181 reg_addr = ies_smt_infos[i].offset + (align << 1);
182
183 bit = BIT(ies_smt_infos[i].bit);
184 regmap_write(regmap, reg_addr, bit);
185 return 0;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800186}
187
188static const struct mtk_pin_drv_grp *mtk_find_pin_drv_grp_by_pin(
189 struct mtk_pinctrl *pctl, unsigned long pin) {
190 int i;
191
192 for (i = 0; i < pctl->devdata->n_pin_drv_grps; i++) {
193 const struct mtk_pin_drv_grp *pin_drv =
194 pctl->devdata->pin_drv_grp + i;
195 if (pin == pin_drv->pin)
196 return pin_drv;
197 }
198
199 return NULL;
200}
201
202static int mtk_pconf_set_driving(struct mtk_pinctrl *pctl,
203 unsigned int pin, unsigned char driving)
204{
205 const struct mtk_pin_drv_grp *pin_drv;
206 unsigned int val;
207 unsigned int bits, mask, shift;
208 const struct mtk_drv_group_desc *drv_grp;
209
210 if (pin >= pctl->devdata->npins)
211 return -EINVAL;
212
213 pin_drv = mtk_find_pin_drv_grp_by_pin(pctl, pin);
214 if (!pin_drv || pin_drv->grp > pctl->devdata->n_grp_cls)
215 return -EINVAL;
216
217 drv_grp = pctl->devdata->grp_desc + pin_drv->grp;
218 if (driving >= drv_grp->min_drv && driving <= drv_grp->max_drv
219 && !(driving % drv_grp->step)) {
220 val = driving / drv_grp->step - 1;
221 bits = drv_grp->high_bit - drv_grp->low_bit + 1;
222 mask = BIT(bits) - 1;
223 shift = pin_drv->bit + drv_grp->low_bit;
224 mask <<= shift;
225 val <<= shift;
226 return regmap_update_bits(mtk_get_regmap(pctl, pin),
227 pin_drv->offset, mask, val);
228 }
229
230 return -EINVAL;
231}
232
Yingjoe Chene73fe272015-05-18 23:11:15 -0700233int mtk_pctrl_spec_pull_set_samereg(struct regmap *regmap,
234 const struct mtk_pin_spec_pupd_set_samereg *pupd_infos,
235 unsigned int info_num, unsigned int pin,
236 unsigned char align, bool isup, unsigned int r1r0)
237{
238 unsigned int i;
239 unsigned int reg_pupd, reg_set, reg_rst;
240 unsigned int bit_pupd, bit_r0, bit_r1;
241 const struct mtk_pin_spec_pupd_set_samereg *spec_pupd_pin;
242 bool find = false;
243
244 for (i = 0; i < info_num; i++) {
245 if (pin == pupd_infos[i].pin) {
246 find = true;
247 break;
248 }
249 }
250
251 if (!find)
252 return -EINVAL;
253
254 spec_pupd_pin = pupd_infos + i;
255 reg_set = spec_pupd_pin->offset + align;
256 reg_rst = spec_pupd_pin->offset + (align << 1);
257
258 if (isup)
259 reg_pupd = reg_rst;
260 else
261 reg_pupd = reg_set;
262
263 bit_pupd = BIT(spec_pupd_pin->pupd_bit);
264 regmap_write(regmap, reg_pupd, bit_pupd);
265
266 bit_r0 = BIT(spec_pupd_pin->r0_bit);
267 bit_r1 = BIT(spec_pupd_pin->r1_bit);
268
269 switch (r1r0) {
270 case MTK_PUPD_SET_R1R0_00:
271 regmap_write(regmap, reg_rst, bit_r0);
272 regmap_write(regmap, reg_rst, bit_r1);
273 break;
274 case MTK_PUPD_SET_R1R0_01:
275 regmap_write(regmap, reg_set, bit_r0);
276 regmap_write(regmap, reg_rst, bit_r1);
277 break;
278 case MTK_PUPD_SET_R1R0_10:
279 regmap_write(regmap, reg_rst, bit_r0);
280 regmap_write(regmap, reg_set, bit_r1);
281 break;
282 case MTK_PUPD_SET_R1R0_11:
283 regmap_write(regmap, reg_set, bit_r0);
284 regmap_write(regmap, reg_set, bit_r1);
285 break;
286 default:
287 return -EINVAL;
288 }
289
290 return 0;
291}
292
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800293static int mtk_pconf_set_pull_select(struct mtk_pinctrl *pctl,
294 unsigned int pin, bool enable, bool isup, unsigned int arg)
295{
296 unsigned int bit;
297 unsigned int reg_pullen, reg_pullsel;
298 int ret;
299
300 /* Some pins' pull setting are very different,
301 * they have separate pull up/down bit, R0 and R1
302 * resistor bit, so we need this special handle.
303 */
304 if (pctl->devdata->spec_pull_set) {
305 ret = pctl->devdata->spec_pull_set(mtk_get_regmap(pctl, pin),
306 pin, pctl->devdata->port_align, isup, arg);
307 if (!ret)
308 return 0;
309 }
310
311 /* For generic pull config, default arg value should be 0 or 1. */
312 if (arg != 0 && arg != 1) {
313 dev_err(pctl->dev, "invalid pull-up argument %d on pin %d .\n",
314 arg, pin);
315 return -EINVAL;
316 }
317
318 bit = BIT(pin & 0xf);
319 if (enable)
320 reg_pullen = SET_ADDR(mtk_get_port(pctl, pin) +
321 pctl->devdata->pullen_offset, pctl);
322 else
323 reg_pullen = CLR_ADDR(mtk_get_port(pctl, pin) +
324 pctl->devdata->pullen_offset, pctl);
325
326 if (isup)
327 reg_pullsel = SET_ADDR(mtk_get_port(pctl, pin) +
328 pctl->devdata->pullsel_offset, pctl);
329 else
330 reg_pullsel = CLR_ADDR(mtk_get_port(pctl, pin) +
331 pctl->devdata->pullsel_offset, pctl);
332
333 regmap_write(mtk_get_regmap(pctl, pin), reg_pullen, bit);
334 regmap_write(mtk_get_regmap(pctl, pin), reg_pullsel, bit);
335 return 0;
336}
337
338static int mtk_pconf_parse_conf(struct pinctrl_dev *pctldev,
339 unsigned int pin, enum pin_config_param param,
340 enum pin_config_param arg)
341{
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700342 int ret = 0;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800343 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
344
345 switch (param) {
346 case PIN_CONFIG_BIAS_DISABLE:
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700347 ret = mtk_pconf_set_pull_select(pctl, pin, false, false, arg);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800348 break;
349 case PIN_CONFIG_BIAS_PULL_UP:
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700350 ret = mtk_pconf_set_pull_select(pctl, pin, true, true, arg);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800351 break;
352 case PIN_CONFIG_BIAS_PULL_DOWN:
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700353 ret = mtk_pconf_set_pull_select(pctl, pin, true, false, arg);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800354 break;
355 case PIN_CONFIG_INPUT_ENABLE:
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700356 ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800357 break;
358 case PIN_CONFIG_OUTPUT:
359 mtk_gpio_set(pctl->chip, pin, arg);
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700360 ret = mtk_pmx_gpio_set_direction(pctldev, NULL, pin, false);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800361 break;
362 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700363 ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800364 break;
365 case PIN_CONFIG_DRIVE_STRENGTH:
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700366 ret = mtk_pconf_set_driving(pctl, pin, arg);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800367 break;
368 default:
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700369 ret = -EINVAL;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800370 }
371
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700372 return ret;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800373}
374
375static int mtk_pconf_group_get(struct pinctrl_dev *pctldev,
376 unsigned group,
377 unsigned long *config)
378{
379 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
380
381 *config = pctl->groups[group].config;
382
383 return 0;
384}
385
386static int mtk_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
387 unsigned long *configs, unsigned num_configs)
388{
389 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
390 struct mtk_pinctrl_group *g = &pctl->groups[group];
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700391 int i, ret;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800392
393 for (i = 0; i < num_configs; i++) {
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700394 ret = mtk_pconf_parse_conf(pctldev, g->pin,
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800395 pinconf_to_config_param(configs[i]),
396 pinconf_to_config_argument(configs[i]));
Hongzhou Yang25d76b22015-05-18 23:11:16 -0700397 if (ret < 0)
398 return ret;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800399
400 g->config = configs[i];
401 }
402
403 return 0;
404}
405
406static const struct pinconf_ops mtk_pconf_ops = {
407 .pin_config_group_get = mtk_pconf_group_get,
408 .pin_config_group_set = mtk_pconf_group_set,
409};
410
411static struct mtk_pinctrl_group *
412mtk_pctrl_find_group_by_pin(struct mtk_pinctrl *pctl, u32 pin)
413{
414 int i;
415
416 for (i = 0; i < pctl->ngroups; i++) {
417 struct mtk_pinctrl_group *grp = pctl->groups + i;
418
419 if (grp->pin == pin)
420 return grp;
421 }
422
423 return NULL;
424}
425
426static const struct mtk_desc_function *mtk_pctrl_find_function_by_pin(
427 struct mtk_pinctrl *pctl, u32 pin_num, u32 fnum)
428{
429 const struct mtk_desc_pin *pin = pctl->devdata->pins + pin_num;
430 const struct mtk_desc_function *func = pin->functions;
431
432 while (func && func->name) {
433 if (func->muxval == fnum)
434 return func;
435 func++;
436 }
437
438 return NULL;
439}
440
441static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl *pctl,
442 u32 pin_num, u32 fnum)
443{
444 int i;
445
446 for (i = 0; i < pctl->devdata->npins; i++) {
447 const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
448
449 if (pin->pin.number == pin_num) {
450 const struct mtk_desc_function *func =
451 pin->functions;
452
453 while (func && func->name) {
454 if (func->muxval == fnum)
455 return true;
456 func++;
457 }
458
459 break;
460 }
461 }
462
463 return false;
464}
465
466static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl *pctl,
467 u32 pin, u32 fnum, struct mtk_pinctrl_group *grp,
468 struct pinctrl_map **map, unsigned *reserved_maps,
469 unsigned *num_maps)
470{
471 bool ret;
472
473 if (*num_maps == *reserved_maps)
474 return -ENOSPC;
475
476 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
477 (*map)[*num_maps].data.mux.group = grp->name;
478
479 ret = mtk_pctrl_is_function_valid(pctl, pin, fnum);
480 if (!ret) {
481 dev_err(pctl->dev, "invalid function %d on pin %d .\n",
482 fnum, pin);
483 return -EINVAL;
484 }
485
486 (*map)[*num_maps].data.mux.function = mtk_gpio_functions[fnum];
487 (*num_maps)++;
488
489 return 0;
490}
491
492static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
493 struct device_node *node,
494 struct pinctrl_map **map,
495 unsigned *reserved_maps,
496 unsigned *num_maps)
497{
498 struct property *pins;
499 u32 pinfunc, pin, func;
500 int num_pins, num_funcs, maps_per_pin;
501 unsigned long *configs;
502 unsigned int num_configs;
503 bool has_config = 0;
504 int i, err;
505 unsigned reserve = 0;
506 struct mtk_pinctrl_group *grp;
507 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
508
509 pins = of_find_property(node, "pinmux", NULL);
510 if (!pins) {
511 dev_err(pctl->dev, "missing pins property in node %s .\n",
512 node->name);
513 return -EINVAL;
514 }
515
Hongzhou Yangc445cac2015-02-11 23:56:11 -0800516 err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
517 &num_configs);
Hongzhou Yangb04a23b2015-11-17 14:33:41 -0800518 if (err)
519 return err;
520
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800521 if (num_configs)
522 has_config = 1;
523
524 num_pins = pins->length / sizeof(u32);
525 num_funcs = num_pins;
526 maps_per_pin = 0;
527 if (num_funcs)
528 maps_per_pin++;
529 if (has_config && num_pins >= 1)
530 maps_per_pin++;
531
Hongzhou Yangb04a23b2015-11-17 14:33:41 -0800532 if (!num_pins || !maps_per_pin) {
533 err = -EINVAL;
534 goto exit;
535 }
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800536
537 reserve = num_pins * maps_per_pin;
538
539 err = pinctrl_utils_reserve_map(pctldev, map,
540 reserved_maps, num_maps, reserve);
541 if (err < 0)
Hongzhou Yangb04a23b2015-11-17 14:33:41 -0800542 goto exit;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800543
544 for (i = 0; i < num_pins; i++) {
545 err = of_property_read_u32_index(node, "pinmux",
546 i, &pinfunc);
547 if (err)
Hongzhou Yangb04a23b2015-11-17 14:33:41 -0800548 goto exit;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800549
550 pin = MTK_GET_PIN_NO(pinfunc);
551 func = MTK_GET_PIN_FUNC(pinfunc);
552
553 if (pin >= pctl->devdata->npins ||
554 func >= ARRAY_SIZE(mtk_gpio_functions)) {
555 dev_err(pctl->dev, "invalid pins value.\n");
556 err = -EINVAL;
Hongzhou Yangb04a23b2015-11-17 14:33:41 -0800557 goto exit;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800558 }
559
560 grp = mtk_pctrl_find_group_by_pin(pctl, pin);
561 if (!grp) {
562 dev_err(pctl->dev, "unable to match pin %d to group\n",
563 pin);
Hongzhou Yangb04a23b2015-11-17 14:33:41 -0800564 err = -EINVAL;
565 goto exit;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800566 }
567
568 err = mtk_pctrl_dt_node_to_map_func(pctl, pin, func, grp, map,
569 reserved_maps, num_maps);
570 if (err < 0)
Hongzhou Yangb04a23b2015-11-17 14:33:41 -0800571 goto exit;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800572
573 if (has_config) {
574 err = pinctrl_utils_add_map_configs(pctldev, map,
575 reserved_maps, num_maps, grp->name,
576 configs, num_configs,
577 PIN_MAP_TYPE_CONFIGS_GROUP);
578 if (err < 0)
Hongzhou Yangb04a23b2015-11-17 14:33:41 -0800579 goto exit;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800580 }
581 }
582
Hongzhou Yangb04a23b2015-11-17 14:33:41 -0800583 err = 0;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800584
Hongzhou Yangb04a23b2015-11-17 14:33:41 -0800585exit:
586 kfree(configs);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800587 return err;
588}
589
590static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
591 struct device_node *np_config,
592 struct pinctrl_map **map, unsigned *num_maps)
593{
594 struct device_node *np;
595 unsigned reserved_maps;
596 int ret;
597
598 *map = NULL;
599 *num_maps = 0;
600 reserved_maps = 0;
601
602 for_each_child_of_node(np_config, np) {
603 ret = mtk_pctrl_dt_subnode_to_map(pctldev, np, map,
604 &reserved_maps, num_maps);
605 if (ret < 0) {
606 pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
Julia Lawall4fc8a4b2015-12-21 17:39:48 +0100607 of_node_put(np);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800608 return ret;
609 }
610 }
611
612 return 0;
613}
614
615static int mtk_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
616{
617 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
618
619 return pctl->ngroups;
620}
621
622static const char *mtk_pctrl_get_group_name(struct pinctrl_dev *pctldev,
623 unsigned group)
624{
625 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
626
627 return pctl->groups[group].name;
628}
629
630static int mtk_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
631 unsigned group,
632 const unsigned **pins,
633 unsigned *num_pins)
634{
635 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
636
637 *pins = (unsigned *)&pctl->groups[group].pin;
638 *num_pins = 1;
639
640 return 0;
641}
642
643static const struct pinctrl_ops mtk_pctrl_ops = {
644 .dt_node_to_map = mtk_pctrl_dt_node_to_map,
645 .dt_free_map = pinctrl_utils_dt_free_map,
646 .get_groups_count = mtk_pctrl_get_groups_count,
647 .get_group_name = mtk_pctrl_get_group_name,
648 .get_group_pins = mtk_pctrl_get_group_pins,
649};
650
651static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
652{
653 return ARRAY_SIZE(mtk_gpio_functions);
654}
655
656static const char *mtk_pmx_get_func_name(struct pinctrl_dev *pctldev,
657 unsigned selector)
658{
659 return mtk_gpio_functions[selector];
660}
661
662static int mtk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
663 unsigned function,
664 const char * const **groups,
665 unsigned * const num_groups)
666{
667 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
668
669 *groups = pctl->grp_names;
670 *num_groups = pctl->ngroups;
671
672 return 0;
673}
674
675static int mtk_pmx_set_mode(struct pinctrl_dev *pctldev,
676 unsigned long pin, unsigned long mode)
677{
678 unsigned int reg_addr;
679 unsigned char bit;
680 unsigned int val;
681 unsigned int mask = (1L << GPIO_MODE_BITS) - 1;
682 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
683
Biao Huang148b95e2016-01-27 09:24:42 +0800684 if (pctl->devdata->spec_pinmux_set)
685 pctl->devdata->spec_pinmux_set(mtk_get_regmap(pctl, pin),
686 pin, mode);
687
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800688 reg_addr = ((pin / MAX_GPIO_MODE_PER_REG) << pctl->devdata->port_shf)
689 + pctl->devdata->pinmux_offset;
690
Biao Huang148b95e2016-01-27 09:24:42 +0800691 mode &= mask;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800692 bit = pin % MAX_GPIO_MODE_PER_REG;
693 mask <<= (GPIO_MODE_BITS * bit);
694 val = (mode << (GPIO_MODE_BITS * bit));
695 return regmap_update_bits(mtk_get_regmap(pctl, pin),
696 reg_addr, mask, val);
697}
698
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800699static const struct mtk_desc_pin *
700mtk_find_pin_by_eint_num(struct mtk_pinctrl *pctl, unsigned int eint_num)
701{
702 int i;
703 const struct mtk_desc_pin *pin;
704
705 for (i = 0; i < pctl->devdata->npins; i++) {
706 pin = pctl->devdata->pins + i;
707 if (pin->eint.eintnum == eint_num)
708 return pin;
709 }
710
711 return NULL;
712}
713
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800714static int mtk_pmx_set_mux(struct pinctrl_dev *pctldev,
715 unsigned function,
716 unsigned group)
717{
718 bool ret;
719 const struct mtk_desc_function *desc;
720 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
721 struct mtk_pinctrl_group *g = pctl->groups + group;
722
723 ret = mtk_pctrl_is_function_valid(pctl, g->pin, function);
724 if (!ret) {
Colin Ian Kingc70336c2015-08-03 00:10:45 +0100725 dev_err(pctl->dev, "invalid function %d on group %d .\n",
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800726 function, group);
727 return -EINVAL;
728 }
729
730 desc = mtk_pctrl_find_function_by_pin(pctl, g->pin, function);
731 if (!desc)
732 return -EINVAL;
733 mtk_pmx_set_mode(pctldev, g->pin, desc->muxval);
734 return 0;
735}
736
Biao Huang59ee9c92016-02-03 09:24:46 +0800737static int mtk_pmx_find_gpio_mode(struct mtk_pinctrl *pctl,
738 unsigned offset)
739{
740 const struct mtk_desc_pin *pin = pctl->devdata->pins + offset;
741 const struct mtk_desc_function *func = pin->functions;
742
743 while (func && func->name) {
744 if (!strncmp(func->name, GPIO_MODE_PREFIX,
745 sizeof(GPIO_MODE_PREFIX)-1))
746 return func->muxval;
747 func++;
748 }
749 return -EINVAL;
750}
751
752static int mtk_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
753 struct pinctrl_gpio_range *range,
754 unsigned offset)
755{
Andrzej Hajda740f5b02016-02-11 15:34:08 +0100756 int muxval;
Biao Huang59ee9c92016-02-03 09:24:46 +0800757 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
758
759 muxval = mtk_pmx_find_gpio_mode(pctl, offset);
760
761 if (muxval < 0) {
762 dev_err(pctl->dev, "invalid gpio pin %d.\n", offset);
763 return -EINVAL;
764 }
765
766 mtk_pmx_set_mode(pctldev, offset, muxval);
Biao Huang31763d32016-02-17 03:16:33 +0800767 mtk_pconf_set_ies_smt(pctl, offset, 1, PIN_CONFIG_INPUT_ENABLE);
Biao Huang59ee9c92016-02-03 09:24:46 +0800768
769 return 0;
770}
771
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800772static const struct pinmux_ops mtk_pmx_ops = {
773 .get_functions_count = mtk_pmx_get_funcs_cnt,
774 .get_function_name = mtk_pmx_get_func_name,
775 .get_function_groups = mtk_pmx_get_func_groups,
776 .set_mux = mtk_pmx_set_mux,
777 .gpio_set_direction = mtk_pmx_gpio_set_direction,
Biao Huang59ee9c92016-02-03 09:24:46 +0800778 .gpio_request_enable = mtk_pmx_gpio_request_enable,
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800779};
780
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800781static int mtk_gpio_direction_input(struct gpio_chip *chip,
782 unsigned offset)
783{
784 return pinctrl_gpio_direction_input(chip->base + offset);
785}
786
787static int mtk_gpio_direction_output(struct gpio_chip *chip,
788 unsigned offset, int value)
789{
790 mtk_gpio_set(chip, offset, value);
791 return pinctrl_gpio_direction_output(chip->base + offset);
792}
793
794static int mtk_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
795{
796 unsigned int reg_addr;
797 unsigned int bit;
798 unsigned int read_val = 0;
799
Linus Walleij11aa6792015-12-08 22:06:23 +0100800 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800801
802 reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
803 bit = BIT(offset & 0xf);
Biao Huang148b95e2016-01-27 09:24:42 +0800804
805 if (pctl->devdata->spec_dir_set)
806 pctl->devdata->spec_dir_set(&reg_addr, offset);
807
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800808 regmap_read(pctl->regmap1, reg_addr, &read_val);
Hongzhou Yangf97c2302015-11-17 14:17:13 -0800809 return !(read_val & bit);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800810}
811
812static int mtk_gpio_get(struct gpio_chip *chip, unsigned offset)
813{
814 unsigned int reg_addr;
815 unsigned int bit;
816 unsigned int read_val = 0;
Linus Walleij11aa6792015-12-08 22:06:23 +0100817 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800818
Hongzhou Yangf97c2302015-11-17 14:17:13 -0800819 reg_addr = mtk_get_port(pctl, offset) +
820 pctl->devdata->din_offset;
Hongzhou Yanga6df4102015-01-21 13:28:15 +0800821
822 bit = BIT(offset & 0xf);
823 regmap_read(pctl->regmap1, reg_addr, &read_val);
824 return !!(read_val & bit);
825}
826
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800827static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
828{
829 const struct mtk_desc_pin *pin;
Linus Walleij11aa6792015-12-08 22:06:23 +0100830 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800831 int irq;
832
833 pin = pctl->devdata->pins + offset;
834 if (pin->eint.eintnum == NO_EINT_SUPPORT)
835 return -EINVAL;
836
837 irq = irq_find_mapping(pctl->domain, pin->eint.eintnum);
838 if (!irq)
839 return -EINVAL;
840
841 return irq;
842}
843
844static int mtk_pinctrl_irq_request_resources(struct irq_data *d)
845{
846 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
847 const struct mtk_desc_pin *pin;
848 int ret;
849
850 pin = mtk_find_pin_by_eint_num(pctl, d->hwirq);
851
852 if (!pin) {
853 dev_err(pctl->dev, "Can not find pin\n");
854 return -EINVAL;
855 }
856
857 ret = gpiochip_lock_as_irq(pctl->chip, pin->pin.number);
858 if (ret) {
859 dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
860 irqd_to_hwirq(d));
861 return ret;
862 }
863
864 /* set mux to INT mode */
865 mtk_pmx_set_mode(pctl->pctl_dev, pin->pin.number, pin->eint.eintmux);
866
867 return 0;
868}
869
870static void mtk_pinctrl_irq_release_resources(struct irq_data *d)
871{
872 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
873 const struct mtk_desc_pin *pin;
874
875 pin = mtk_find_pin_by_eint_num(pctl, d->hwirq);
876
877 if (!pin) {
878 dev_err(pctl->dev, "Can not find pin\n");
879 return;
880 }
881
882 gpiochip_unlock_as_irq(pctl->chip, pin->pin.number);
883}
884
885static void __iomem *mtk_eint_get_offset(struct mtk_pinctrl *pctl,
886 unsigned int eint_num, unsigned int offset)
887{
888 unsigned int eint_base = 0;
889 void __iomem *reg;
890
891 if (eint_num >= pctl->devdata->ap_num)
892 eint_base = pctl->devdata->ap_num;
893
894 reg = pctl->eint_reg_base + offset + ((eint_num - eint_base) / 32) * 4;
895
896 return reg;
897}
898
899/*
900 * mtk_can_en_debounce: Check the EINT number is able to enable debounce or not
901 * @eint_num: the EINT number to setmtk_pinctrl
902 */
903static unsigned int mtk_eint_can_en_debounce(struct mtk_pinctrl *pctl,
904 unsigned int eint_num)
905{
906 unsigned int sens;
907 unsigned int bit = BIT(eint_num % 32);
908 const struct mtk_eint_offsets *eint_offsets =
909 &pctl->devdata->eint_offsets;
910
911 void __iomem *reg = mtk_eint_get_offset(pctl, eint_num,
912 eint_offsets->sens);
913
914 if (readl(reg) & bit)
915 sens = MT_LEVEL_SENSITIVE;
916 else
917 sens = MT_EDGE_SENSITIVE;
918
919 if ((eint_num < pctl->devdata->db_cnt) && (sens != MT_EDGE_SENSITIVE))
920 return 1;
921 else
922 return 0;
923}
924
925/*
926 * mtk_eint_get_mask: To get the eint mask
927 * @eint_num: the EINT number to get
928 */
929static unsigned int mtk_eint_get_mask(struct mtk_pinctrl *pctl,
930 unsigned int eint_num)
931{
932 unsigned int bit = BIT(eint_num % 32);
933 const struct mtk_eint_offsets *eint_offsets =
934 &pctl->devdata->eint_offsets;
935
936 void __iomem *reg = mtk_eint_get_offset(pctl, eint_num,
937 eint_offsets->mask);
938
939 return !!(readl(reg) & bit);
940}
941
Yingjoe Chen3221f402015-01-27 14:15:26 +0800942static int mtk_eint_flip_edge(struct mtk_pinctrl *pctl, int hwirq)
943{
944 int start_level, curr_level;
945 unsigned int reg_offset;
946 const struct mtk_eint_offsets *eint_offsets = &(pctl->devdata->eint_offsets);
Javier Martinez Canillasb4b05b92015-08-29 01:25:01 +0200947 u32 mask = BIT(hwirq & 0x1f);
Yingjoe Chen3221f402015-01-27 14:15:26 +0800948 u32 port = (hwirq >> 5) & eint_offsets->port_mask;
949 void __iomem *reg = pctl->eint_reg_base + (port << 2);
950 const struct mtk_desc_pin *pin;
951
952 pin = mtk_find_pin_by_eint_num(pctl, hwirq);
953 curr_level = mtk_gpio_get(pctl->chip, pin->pin.number);
954 do {
955 start_level = curr_level;
956 if (start_level)
957 reg_offset = eint_offsets->pol_clr;
958 else
959 reg_offset = eint_offsets->pol_set;
960 writel(mask, reg + reg_offset);
961
962 curr_level = mtk_gpio_get(pctl->chip, pin->pin.number);
963 } while (start_level != curr_level);
964
965 return start_level;
966}
967
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800968static void mtk_eint_mask(struct irq_data *d)
969{
970 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
971 const struct mtk_eint_offsets *eint_offsets =
972 &pctl->devdata->eint_offsets;
973 u32 mask = BIT(d->hwirq & 0x1f);
974 void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
975 eint_offsets->mask_set);
976
977 writel(mask, reg);
978}
979
980static void mtk_eint_unmask(struct irq_data *d)
981{
982 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
983 const struct mtk_eint_offsets *eint_offsets =
984 &pctl->devdata->eint_offsets;
985 u32 mask = BIT(d->hwirq & 0x1f);
986 void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
987 eint_offsets->mask_clr);
988
989 writel(mask, reg);
Yingjoe Chen3221f402015-01-27 14:15:26 +0800990
991 if (pctl->eint_dual_edges[d->hwirq])
992 mtk_eint_flip_edge(pctl, d->hwirq);
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800993}
994
995static int mtk_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
996 unsigned debounce)
997{
Linus Walleij58383c72015-11-04 09:56:26 +0100998 struct mtk_pinctrl *pctl = dev_get_drvdata(chip->parent);
Maoguang Mengd9819eb2015-01-21 13:28:16 +0800999 int eint_num, virq, eint_offset;
1000 unsigned int set_offset, bit, clr_bit, clr_offset, rst, i, unmask, dbnc;
1001 static const unsigned int dbnc_arr[] = {0 , 1, 16, 32, 64, 128, 256};
1002 const struct mtk_desc_pin *pin;
1003 struct irq_data *d;
1004
1005 pin = pctl->devdata->pins + offset;
1006 if (pin->eint.eintnum == NO_EINT_SUPPORT)
1007 return -EINVAL;
1008
1009 eint_num = pin->eint.eintnum;
1010 virq = irq_find_mapping(pctl->domain, eint_num);
1011 eint_offset = (eint_num % 4) * 8;
1012 d = irq_get_irq_data(virq);
1013
1014 set_offset = (eint_num / 4) * 4 + pctl->devdata->eint_offsets.dbnc_set;
1015 clr_offset = (eint_num / 4) * 4 + pctl->devdata->eint_offsets.dbnc_clr;
1016 if (!mtk_eint_can_en_debounce(pctl, eint_num))
1017 return -ENOSYS;
1018
1019 dbnc = ARRAY_SIZE(dbnc_arr);
1020 for (i = 0; i < ARRAY_SIZE(dbnc_arr); i++) {
1021 if (debounce <= dbnc_arr[i]) {
1022 dbnc = i;
1023 break;
1024 }
1025 }
1026
1027 if (!mtk_eint_get_mask(pctl, eint_num)) {
1028 mtk_eint_mask(d);
1029 unmask = 1;
Colin Ian King74d77e52015-04-20 10:59:17 -05001030 } else {
1031 unmask = 0;
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001032 }
1033
1034 clr_bit = 0xff << eint_offset;
1035 writel(clr_bit, pctl->eint_reg_base + clr_offset);
1036
1037 bit = ((dbnc << EINT_DBNC_SET_DBNC_BITS) | EINT_DBNC_SET_EN) <<
1038 eint_offset;
1039 rst = EINT_DBNC_RST_BIT << eint_offset;
1040 writel(rst | bit, pctl->eint_reg_base + set_offset);
1041
1042 /* Delay a while (more than 2T) to wait for hw debounce counter reset
1043 work correctly */
1044 udelay(1);
1045 if (unmask == 1)
1046 mtk_eint_unmask(d);
1047
1048 return 0;
1049}
1050
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001051static struct gpio_chip mtk_gpio_chip = {
1052 .owner = THIS_MODULE,
Jonas Gorski98c85d52015-10-11 17:34:19 +02001053 .request = gpiochip_generic_request,
1054 .free = gpiochip_generic_free,
Hongzhou Yangf97c2302015-11-17 14:17:13 -08001055 .get_direction = mtk_gpio_get_direction,
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001056 .direction_input = mtk_gpio_direction_input,
1057 .direction_output = mtk_gpio_direction_output,
1058 .get = mtk_gpio_get,
1059 .set = mtk_gpio_set,
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001060 .to_irq = mtk_gpio_to_irq,
1061 .set_debounce = mtk_gpio_set_debounce,
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001062 .of_gpio_n_cells = 2,
1063};
1064
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001065static int mtk_eint_set_type(struct irq_data *d,
1066 unsigned int type)
1067{
1068 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1069 const struct mtk_eint_offsets *eint_offsets =
1070 &pctl->devdata->eint_offsets;
1071 u32 mask = BIT(d->hwirq & 0x1f);
1072 void __iomem *reg;
1073
1074 if (((type & IRQ_TYPE_EDGE_BOTH) && (type & IRQ_TYPE_LEVEL_MASK)) ||
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001075 ((type & IRQ_TYPE_LEVEL_MASK) == IRQ_TYPE_LEVEL_MASK)) {
1076 dev_err(pctl->dev, "Can't configure IRQ%d (EINT%lu) for type 0x%X\n",
1077 d->irq, d->hwirq, type);
1078 return -EINVAL;
1079 }
1080
Yingjoe Chen3221f402015-01-27 14:15:26 +08001081 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
1082 pctl->eint_dual_edges[d->hwirq] = 1;
1083 else
1084 pctl->eint_dual_edges[d->hwirq] = 0;
1085
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001086 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING)) {
1087 reg = mtk_eint_get_offset(pctl, d->hwirq,
1088 eint_offsets->pol_clr);
1089 writel(mask, reg);
1090 } else {
1091 reg = mtk_eint_get_offset(pctl, d->hwirq,
1092 eint_offsets->pol_set);
1093 writel(mask, reg);
1094 }
1095
1096 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
1097 reg = mtk_eint_get_offset(pctl, d->hwirq,
1098 eint_offsets->sens_clr);
1099 writel(mask, reg);
1100 } else {
1101 reg = mtk_eint_get_offset(pctl, d->hwirq,
1102 eint_offsets->sens_set);
1103 writel(mask, reg);
1104 }
1105
Yingjoe Chen3221f402015-01-27 14:15:26 +08001106 if (pctl->eint_dual_edges[d->hwirq])
1107 mtk_eint_flip_edge(pctl, d->hwirq);
1108
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001109 return 0;
1110}
1111
Maoguang Meng58a5e1b2015-08-14 16:38:06 +08001112static int mtk_eint_irq_set_wake(struct irq_data *d, unsigned int on)
1113{
1114 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1115 int shift = d->hwirq & 0x1f;
1116 int reg = d->hwirq >> 5;
1117
1118 if (on)
1119 pctl->wake_mask[reg] |= BIT(shift);
1120 else
1121 pctl->wake_mask[reg] &= ~BIT(shift);
1122
1123 return 0;
1124}
1125
1126static void mtk_eint_chip_write_mask(const struct mtk_eint_offsets *chip,
1127 void __iomem *eint_reg_base, u32 *buf)
1128{
1129 int port;
1130 void __iomem *reg;
1131
1132 for (port = 0; port < chip->ports; port++) {
1133 reg = eint_reg_base + (port << 2);
1134 writel_relaxed(~buf[port], reg + chip->mask_set);
1135 writel_relaxed(buf[port], reg + chip->mask_clr);
1136 }
1137}
1138
1139static void mtk_eint_chip_read_mask(const struct mtk_eint_offsets *chip,
1140 void __iomem *eint_reg_base, u32 *buf)
1141{
1142 int port;
1143 void __iomem *reg;
1144
1145 for (port = 0; port < chip->ports; port++) {
1146 reg = eint_reg_base + chip->mask + (port << 2);
1147 buf[port] = ~readl_relaxed(reg);
1148 /* Mask is 0 when irq is enabled, and 1 when disabled. */
1149 }
1150}
1151
1152static int mtk_eint_suspend(struct device *device)
1153{
1154 void __iomem *reg;
1155 struct mtk_pinctrl *pctl = dev_get_drvdata(device);
1156 const struct mtk_eint_offsets *eint_offsets =
1157 &pctl->devdata->eint_offsets;
1158
1159 reg = pctl->eint_reg_base;
1160 mtk_eint_chip_read_mask(eint_offsets, reg, pctl->cur_mask);
1161 mtk_eint_chip_write_mask(eint_offsets, reg, pctl->wake_mask);
1162
1163 return 0;
1164}
1165
1166static int mtk_eint_resume(struct device *device)
1167{
1168 struct mtk_pinctrl *pctl = dev_get_drvdata(device);
1169 const struct mtk_eint_offsets *eint_offsets =
1170 &pctl->devdata->eint_offsets;
1171
1172 mtk_eint_chip_write_mask(eint_offsets,
1173 pctl->eint_reg_base, pctl->cur_mask);
1174
1175 return 0;
1176}
1177
1178const struct dev_pm_ops mtk_eint_pm_ops = {
1179 .suspend = mtk_eint_suspend,
1180 .resume = mtk_eint_resume,
1181};
1182
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001183static void mtk_eint_ack(struct irq_data *d)
1184{
1185 struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1186 const struct mtk_eint_offsets *eint_offsets =
1187 &pctl->devdata->eint_offsets;
1188 u32 mask = BIT(d->hwirq & 0x1f);
1189 void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
1190 eint_offsets->ack);
1191
1192 writel(mask, reg);
1193}
1194
1195static struct irq_chip mtk_pinctrl_irq_chip = {
1196 .name = "mt-eint",
Maoguang Meng58a5e1b2015-08-14 16:38:06 +08001197 .irq_disable = mtk_eint_mask,
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001198 .irq_mask = mtk_eint_mask,
1199 .irq_unmask = mtk_eint_unmask,
1200 .irq_ack = mtk_eint_ack,
1201 .irq_set_type = mtk_eint_set_type,
Maoguang Meng58a5e1b2015-08-14 16:38:06 +08001202 .irq_set_wake = mtk_eint_irq_set_wake,
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001203 .irq_request_resources = mtk_pinctrl_irq_request_resources,
1204 .irq_release_resources = mtk_pinctrl_irq_release_resources,
1205};
1206
1207static unsigned int mtk_eint_init(struct mtk_pinctrl *pctl)
1208{
1209 const struct mtk_eint_offsets *eint_offsets =
1210 &pctl->devdata->eint_offsets;
1211 void __iomem *reg = pctl->eint_reg_base + eint_offsets->dom_en;
1212 unsigned int i;
1213
1214 for (i = 0; i < pctl->devdata->ap_num; i += 32) {
1215 writel(0xffffffff, reg);
1216 reg += 4;
1217 }
1218 return 0;
1219}
1220
1221static inline void
1222mtk_eint_debounce_process(struct mtk_pinctrl *pctl, int index)
1223{
1224 unsigned int rst, ctrl_offset;
1225 unsigned int bit, dbnc;
1226 const struct mtk_eint_offsets *eint_offsets =
1227 &pctl->devdata->eint_offsets;
1228
1229 ctrl_offset = (index / 4) * 4 + eint_offsets->dbnc_ctrl;
1230 dbnc = readl(pctl->eint_reg_base + ctrl_offset);
1231 bit = EINT_DBNC_SET_EN << ((index % 4) * 8);
1232 if ((bit & dbnc) > 0) {
1233 ctrl_offset = (index / 4) * 4 + eint_offsets->dbnc_set;
1234 rst = EINT_DBNC_RST_BIT << ((index % 4) * 8);
1235 writel(rst, pctl->eint_reg_base + ctrl_offset);
1236 }
1237}
1238
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +02001239static void mtk_eint_irq_handler(struct irq_desc *desc)
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001240{
Jiang Liu5663bb22015-06-04 12:13:16 +08001241 struct irq_chip *chip = irq_desc_get_chip(desc);
1242 struct mtk_pinctrl *pctl = irq_desc_get_handler_data(desc);
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001243 unsigned int status, eint_num;
1244 int offset, index, virq;
1245 const struct mtk_eint_offsets *eint_offsets =
1246 &pctl->devdata->eint_offsets;
1247 void __iomem *reg = mtk_eint_get_offset(pctl, 0, eint_offsets->stat);
Yingjoe Chen3221f402015-01-27 14:15:26 +08001248 int dual_edges, start_level, curr_level;
1249 const struct mtk_desc_pin *pin;
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001250
1251 chained_irq_enter(chip, desc);
1252 for (eint_num = 0; eint_num < pctl->devdata->ap_num; eint_num += 32) {
1253 status = readl(reg);
1254 reg += 4;
1255 while (status) {
1256 offset = __ffs(status);
1257 index = eint_num + offset;
1258 virq = irq_find_mapping(pctl->domain, index);
1259 status &= ~BIT(offset);
1260
Yingjoe Chen3221f402015-01-27 14:15:26 +08001261 dual_edges = pctl->eint_dual_edges[index];
1262 if (dual_edges) {
1263 /* Clear soft-irq in case we raised it
1264 last time */
1265 writel(BIT(offset), reg - eint_offsets->stat +
1266 eint_offsets->soft_clr);
1267
1268 pin = mtk_find_pin_by_eint_num(pctl, index);
1269 start_level = mtk_gpio_get(pctl->chip,
1270 pin->pin.number);
1271 }
1272
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001273 generic_handle_irq(virq);
1274
Yingjoe Chen3221f402015-01-27 14:15:26 +08001275 if (dual_edges) {
1276 curr_level = mtk_eint_flip_edge(pctl, index);
1277
1278 /* If level changed, we might lost one edge
1279 interrupt, raised it through soft-irq */
1280 if (start_level != curr_level)
1281 writel(BIT(offset), reg -
1282 eint_offsets->stat +
1283 eint_offsets->soft_set);
1284 }
1285
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001286 if (index < pctl->devdata->db_cnt)
1287 mtk_eint_debounce_process(pctl , index);
1288 }
1289 }
1290 chained_irq_exit(chip, desc);
1291}
1292
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001293static int mtk_pctrl_build_state(struct platform_device *pdev)
1294{
1295 struct mtk_pinctrl *pctl = platform_get_drvdata(pdev);
1296 int i;
1297
1298 pctl->ngroups = pctl->devdata->npins;
1299
1300 /* Allocate groups */
Axel Lin0206caa2015-03-12 21:53:32 +08001301 pctl->groups = devm_kcalloc(&pdev->dev, pctl->ngroups,
1302 sizeof(*pctl->groups), GFP_KERNEL);
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001303 if (!pctl->groups)
1304 return -ENOMEM;
1305
1306 /* We assume that one pin is one group, use pin name as group name. */
Axel Lin0206caa2015-03-12 21:53:32 +08001307 pctl->grp_names = devm_kcalloc(&pdev->dev, pctl->ngroups,
1308 sizeof(*pctl->grp_names), GFP_KERNEL);
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001309 if (!pctl->grp_names)
1310 return -ENOMEM;
1311
1312 for (i = 0; i < pctl->devdata->npins; i++) {
1313 const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
1314 struct mtk_pinctrl_group *group = pctl->groups + i;
1315
1316 group->name = pin->pin.name;
1317 group->pin = pin->pin.number;
1318
1319 pctl->grp_names[i] = pin->pin.name;
1320 }
1321
1322 return 0;
1323}
1324
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001325int mtk_pctrl_init(struct platform_device *pdev,
Hongzhou Yangfc59e662015-05-18 23:11:17 -07001326 const struct mtk_pinctrl_devdata *data,
1327 struct regmap *regmap)
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001328{
1329 struct pinctrl_pin_desc *pins;
1330 struct mtk_pinctrl *pctl;
1331 struct device_node *np = pdev->dev.of_node, *node;
1332 struct property *prop;
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001333 struct resource *res;
Maoguang Meng58a5e1b2015-08-14 16:38:06 +08001334 int i, ret, irq, ports_buf;
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001335
1336 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
1337 if (!pctl)
1338 return -ENOMEM;
1339
1340 platform_set_drvdata(pdev, pctl);
1341
1342 prop = of_find_property(np, "pins-are-numbered", NULL);
1343 if (!prop) {
Hongzhou Yangc445cac2015-02-11 23:56:11 -08001344 dev_err(&pdev->dev, "only support pins-are-numbered format\n");
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001345 return -EINVAL;
1346 }
1347
1348 node = of_parse_phandle(np, "mediatek,pctl-regmap", 0);
1349 if (node) {
1350 pctl->regmap1 = syscon_node_to_regmap(node);
1351 if (IS_ERR(pctl->regmap1))
1352 return PTR_ERR(pctl->regmap1);
Hongzhou Yangfc59e662015-05-18 23:11:17 -07001353 } else if (regmap) {
1354 pctl->regmap1 = regmap;
1355 } else {
1356 dev_err(&pdev->dev, "Pinctrl node has not register regmap.\n");
1357 return -EINVAL;
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001358 }
1359
1360 /* Only 8135 has two base addr, other SoCs have only one. */
1361 node = of_parse_phandle(np, "mediatek,pctl-regmap", 1);
1362 if (node) {
1363 pctl->regmap2 = syscon_node_to_regmap(node);
1364 if (IS_ERR(pctl->regmap2))
1365 return PTR_ERR(pctl->regmap2);
1366 }
1367
1368 pctl->devdata = data;
1369 ret = mtk_pctrl_build_state(pdev);
1370 if (ret) {
1371 dev_err(&pdev->dev, "build state failed: %d\n", ret);
1372 return -EINVAL;
1373 }
1374
Axel Lin0206caa2015-03-12 21:53:32 +08001375 pins = devm_kcalloc(&pdev->dev, pctl->devdata->npins, sizeof(*pins),
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001376 GFP_KERNEL);
1377 if (!pins)
1378 return -ENOMEM;
1379
1380 for (i = 0; i < pctl->devdata->npins; i++)
1381 pins[i] = pctl->devdata->pins[i].pin;
Hongzhou Yangd48c2c02015-08-25 17:32:45 -07001382
1383 pctl->pctl_desc.name = dev_name(&pdev->dev);
1384 pctl->pctl_desc.owner = THIS_MODULE;
1385 pctl->pctl_desc.pins = pins;
1386 pctl->pctl_desc.npins = pctl->devdata->npins;
1387 pctl->pctl_desc.confops = &mtk_pconf_ops;
1388 pctl->pctl_desc.pctlops = &mtk_pctrl_ops;
1389 pctl->pctl_desc.pmxops = &mtk_pmx_ops;
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001390 pctl->dev = &pdev->dev;
Hongzhou Yangd48c2c02015-08-25 17:32:45 -07001391
1392 pctl->pctl_dev = pinctrl_register(&pctl->pctl_desc, &pdev->dev, pctl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001393 if (IS_ERR(pctl->pctl_dev)) {
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001394 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001395 return PTR_ERR(pctl->pctl_dev);
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001396 }
1397
1398 pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
1399 if (!pctl->chip) {
1400 ret = -ENOMEM;
1401 goto pctrl_error;
1402 }
1403
Hongzhou Yangfc63d852015-05-27 02:43:55 -07001404 *pctl->chip = mtk_gpio_chip;
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001405 pctl->chip->ngpio = pctl->devdata->npins;
1406 pctl->chip->label = dev_name(&pdev->dev);
Linus Walleij58383c72015-11-04 09:56:26 +01001407 pctl->chip->parent = &pdev->dev;
Hongzhou Yangfc59e662015-05-18 23:11:17 -07001408 pctl->chip->base = -1;
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001409
Linus Walleij11aa6792015-12-08 22:06:23 +01001410 ret = gpiochip_add_data(pctl->chip, pctl);
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001411 if (ret) {
1412 ret = -EINVAL;
1413 goto pctrl_error;
1414 }
1415
1416 /* Register the GPIO to pin mappings. */
1417 ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
1418 0, 0, pctl->devdata->npins);
1419 if (ret) {
1420 ret = -EINVAL;
1421 goto chip_error;
1422 }
1423
Hongzhou Yangfc63d852015-05-27 02:43:55 -07001424 if (!of_property_read_bool(np, "interrupt-controller"))
Hongzhou Yangfc59e662015-05-18 23:11:17 -07001425 return 0;
1426
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001427 /* Get EINT register base from dts. */
1428 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1429 if (!res) {
1430 dev_err(&pdev->dev, "Unable to get Pinctrl resource\n");
1431 ret = -EINVAL;
1432 goto chip_error;
1433 }
1434
1435 pctl->eint_reg_base = devm_ioremap_resource(&pdev->dev, res);
1436 if (IS_ERR(pctl->eint_reg_base)) {
1437 ret = -EINVAL;
1438 goto chip_error;
1439 }
1440
Maoguang Meng58a5e1b2015-08-14 16:38:06 +08001441 ports_buf = pctl->devdata->eint_offsets.ports;
1442 pctl->wake_mask = devm_kcalloc(&pdev->dev, ports_buf,
1443 sizeof(*pctl->wake_mask), GFP_KERNEL);
1444 if (!pctl->wake_mask) {
1445 ret = -ENOMEM;
1446 goto chip_error;
1447 }
1448
1449 pctl->cur_mask = devm_kcalloc(&pdev->dev, ports_buf,
1450 sizeof(*pctl->cur_mask), GFP_KERNEL);
1451 if (!pctl->cur_mask) {
1452 ret = -ENOMEM;
1453 goto chip_error;
1454 }
1455
Axel Lin0206caa2015-03-12 21:53:32 +08001456 pctl->eint_dual_edges = devm_kcalloc(&pdev->dev, pctl->devdata->ap_num,
1457 sizeof(int), GFP_KERNEL);
Yingjoe Chen3221f402015-01-27 14:15:26 +08001458 if (!pctl->eint_dual_edges) {
1459 ret = -ENOMEM;
1460 goto chip_error;
1461 }
1462
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001463 irq = irq_of_parse_and_map(np, 0);
1464 if (!irq) {
1465 dev_err(&pdev->dev, "couldn't parse and map irq\n");
1466 ret = -EINVAL;
Axel Lin61a35572015-03-12 21:52:33 +08001467 goto chip_error;
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001468 }
1469
1470 pctl->domain = irq_domain_add_linear(np,
1471 pctl->devdata->ap_num, &irq_domain_simple_ops, NULL);
1472 if (!pctl->domain) {
1473 dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
1474 ret = -ENOMEM;
Axel Lin61a35572015-03-12 21:52:33 +08001475 goto chip_error;
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001476 }
1477
1478 mtk_eint_init(pctl);
1479 for (i = 0; i < pctl->devdata->ap_num; i++) {
1480 int virq = irq_create_mapping(pctl->domain, i);
1481
1482 irq_set_chip_and_handler(virq, &mtk_pinctrl_irq_chip,
1483 handle_level_irq);
1484 irq_set_chip_data(virq, pctl);
Javier Martinez Canillase4411892015-09-16 10:28:30 +02001485 }
Maoguang Mengd9819eb2015-01-21 13:28:16 +08001486
Thomas Gleixner1e105922015-06-21 20:16:09 +02001487 irq_set_chained_handler_and_data(irq, mtk_eint_irq_handler, pctl);
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001488 return 0;
1489
Hongzhou Yanga6df4102015-01-21 13:28:15 +08001490chip_error:
1491 gpiochip_remove(pctl->chip);
1492pctrl_error:
1493 pinctrl_unregister(pctl->pctl_dev);
1494 return ret;
1495}
1496
1497MODULE_LICENSE("GPL");
1498MODULE_DESCRIPTION("MediaTek Pinctrl Driver");
1499MODULE_AUTHOR("Hongzhou Yang <hongzhou.yang@mediatek.com>");