xref: /linux/drivers/pinctrl/meson/pinctrl-amlogic-a4.c (revision cf9610f9116d55c5a66ef9f1faecacabd93a9322)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 /*
3  * Copyright (c) 2024 Amlogic, Inc. All rights reserved.
4  * Author: Xianwei Zhao <xianwei.zhao@amlogic.com>
5  */
6 
7 #include <linux/err.h>
8 #include <linux/gpio/driver.h>
9 #include <linux/init.h>
10 #include <linux/io.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_address.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/seq_file.h>
17 #include <linux/slab.h>
18 #include <linux/string_helpers.h>
19 
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/pinctrl/pinconf.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinmux.h>
24 #include <dt-bindings/pinctrl/amlogic,pinctrl.h>
25 
26 #include "../core.h"
27 #include "../pinctrl-utils.h"
28 #include "../pinconf.h"
29 
30 #define gpio_chip_to_bank(chip) \
31 		container_of(chip, struct aml_gpio_bank, gpio_chip)
32 
33 #define AML_REG_PULLEN		0
34 #define AML_REG_PULL		1
35 #define AML_REG_DIR		2
36 #define AML_REG_OUT		3
37 #define AML_REG_IN		4
38 #define AML_REG_DS		5
39 #define AML_NUM_REG		6
40 
41 enum aml_pinconf_drv {
42 	PINCONF_DRV_500UA,
43 	PINCONF_DRV_2500UA,
44 	PINCONF_DRV_3000UA,
45 	PINCONF_DRV_4000UA,
46 };
47 
48 struct aml_pio_control {
49 	u32 gpio_offset;
50 	u32 reg_offset[AML_NUM_REG];
51 	u32 bit_offset[AML_NUM_REG];
52 };
53 
54 /*
55  * partial bank(subordinate) pins mux config use other bank(main) mux registgers
56  * m_bank_id:	the main bank which pin_id from 0, but register bit not from bit 0
57  * m_bit_offs:	bit offset the main bank mux register
58  * s_bit_offs:	start bit that subordinate bank use mux register
59  * sid:         start pin_id of subordinate bank
60  * eid:         end pin_id of subordinate bank
61  * next:	subordinate bank reused multiple other bank groups.
62  */
63 struct multi_mux {
64 	unsigned int m_bank_id;
65 	unsigned int m_bit_offs;
66 	unsigned int s_bit_offs;
67 	unsigned int sid;
68 	unsigned int eid;
69 	const struct multi_mux *next;
70 };
71 
72 struct aml_pctl_data {
73 	unsigned int number;
74 	const struct multi_mux *p_mux;
75 };
76 
77 struct aml_pmx_func {
78 	const char	*name;
79 	const char	**groups;
80 	unsigned int	ngroups;
81 };
82 
83 struct aml_pctl_group {
84 	const char		*name;
85 	unsigned int		npins;
86 	unsigned int		*pins;
87 	unsigned int		*func;
88 };
89 
90 struct aml_gpio_bank {
91 	struct gpio_chip		gpio_chip;
92 	struct aml_pio_control		pc;
93 	u32				bank_id;
94 	u32				mux_bit_offs;
95 	unsigned int			pin_base;
96 	struct regmap			*reg_mux;
97 	struct regmap			*reg_gpio;
98 	struct regmap			*reg_ds;
99 	const struct multi_mux		*p_mux;
100 };
101 
102 struct aml_pinctrl {
103 	struct device			*dev;
104 	struct pinctrl_dev		*pctl;
105 	struct aml_gpio_bank		*banks;
106 	int				nbanks;
107 	struct aml_pmx_func		*functions;
108 	int				nfunctions;
109 	struct aml_pctl_group		*groups;
110 	int				ngroups;
111 
112 	const struct aml_pctl_data	*data;
113 };
114 
115 static const unsigned int aml_bit_strides[AML_NUM_REG] = {
116 	1, 1, 1, 1, 1, 2
117 };
118 
119 static const unsigned int aml_def_regoffs[AML_NUM_REG] = {
120 	3, 4, 2, 1, 0, 7
121 };
122 
123 static const char *aml_bank_name[31] = {
124 "GPIOA", "GPIOB", "GPIOC", "GPIOD", "GPIOE", "GPIOF", "GPIOG",
125 "GPIOH", "GPIOI", "GPIOJ", "GPIOK", "GPIOL", "GPIOM", "GPION",
126 "GPIOO", "GPIOP", "GPIOQ", "GPIOR", "GPIOS", "GPIOT", "GPIOU",
127 "GPIOV", "GPIOW", "GPIOX", "GPIOY", "GPIOZ", "GPIODV", "GPIOAO",
128 "GPIOCC", "TEST_N", "ANALOG"
129 };
130 
131 static const struct multi_mux multi_mux_a9[] = {
132 	{
133 		.m_bank_id = AMLOGIC_GPIO_C,
134 		.m_bit_offs = 4,
135 		.s_bit_offs = 0,
136 		.sid = (AMLOGIC_GPIO_D << 8) + 16,
137 		.eid = (AMLOGIC_GPIO_D << 8) + 16,
138 		.next = &multi_mux_a9[1],
139 	}, {
140 		.m_bank_id = AMLOGIC_GPIO_AO,
141 		.m_bit_offs = 0,
142 		.s_bit_offs = 52,
143 		.sid = (AMLOGIC_GPIO_D << 8) + 17,
144 		.eid = (AMLOGIC_GPIO_D << 8) + 17,
145 		.next = NULL,
146 	}, {
147 		.m_bank_id = AMLOGIC_GPIO_A,
148 		.m_bit_offs = 0,
149 		.s_bit_offs = 80,
150 		.sid = (AMLOGIC_GPIO_Y << 8) + 8,
151 		.eid = (AMLOGIC_GPIO_Y << 8) + 9,
152 		.next = NULL,
153 	}, {
154 		.m_bank_id = AMLOGIC_GPIO_CC,
155 		.m_bit_offs = 24,
156 		.s_bit_offs = 0,
157 		.sid = (AMLOGIC_GPIO_X << 8) + 16,
158 		.eid = (AMLOGIC_GPIO_X << 8) + 17,
159 		.next = NULL,
160 	},
161 };
162 
163 static const struct aml_pctl_data a9_priv_data = {
164 	.number = ARRAY_SIZE(multi_mux_a9),
165 	.p_mux = multi_mux_a9,
166 };
167 
168 static const struct multi_mux multi_mux_s7[] = {
169 	{
170 		.m_bank_id = AMLOGIC_GPIO_CC,
171 		.m_bit_offs = 24,
172 		.s_bit_offs = 0,
173 		.sid = (AMLOGIC_GPIO_X << 8) + 16,
174 		.eid = (AMLOGIC_GPIO_X << 8) + 19,
175 		.next = NULL,
176 	},
177 };
178 
179 static const struct aml_pctl_data s7_priv_data = {
180 	.number = ARRAY_SIZE(multi_mux_s7),
181 	.p_mux = multi_mux_s7,
182 };
183 
184 static const struct multi_mux multi_mux_s6[] = {
185 	{
186 		.m_bank_id = AMLOGIC_GPIO_CC,
187 		.m_bit_offs = 24,
188 		.s_bit_offs = 0,
189 		.sid = (AMLOGIC_GPIO_X << 8) + 16,
190 		.eid = (AMLOGIC_GPIO_X << 8) + 19,
191 		.next = NULL,
192 	}, {
193 		.m_bank_id = AMLOGIC_GPIO_F,
194 		.m_bit_offs = 4,
195 		.s_bit_offs = 0,
196 		.sid = (AMLOGIC_GPIO_D << 8) + 6,
197 		.eid = (AMLOGIC_GPIO_D << 8) + 6,
198 		.next = NULL,
199 	},
200 };
201 
202 static const struct aml_pctl_data s6_priv_data = {
203 	.number = ARRAY_SIZE(multi_mux_s6),
204 	.p_mux = multi_mux_s6,
205 };
206 
aml_pmx_calc_reg_and_offset(struct pinctrl_gpio_range * range,unsigned int pin,unsigned int * reg,unsigned int * offset)207 static int aml_pmx_calc_reg_and_offset(struct pinctrl_gpio_range *range,
208 				       unsigned int pin, unsigned int *reg,
209 				       unsigned int *offset)
210 {
211 	unsigned int shift;
212 
213 	shift = ((pin - range->pin_base) << 2) + *offset;
214 	*reg = (shift / 32) * 4;
215 	*offset = shift % 32;
216 
217 	return 0;
218 }
219 
aml_pctl_set_function(struct aml_pinctrl * info,struct pinctrl_gpio_range * range,int pin_id,int func)220 static int aml_pctl_set_function(struct aml_pinctrl *info,
221 				 struct pinctrl_gpio_range *range,
222 				 int pin_id, int func)
223 {
224 	struct aml_gpio_bank *bank = gpio_chip_to_bank(range->gc);
225 	unsigned int shift;
226 	int reg;
227 	int i, loop_count;
228 	unsigned int offset = bank->mux_bit_offs;
229 	const struct multi_mux *p_mux;
230 
231 	/* peculiar mux reg set */
232 	loop_count = 10;
233 	p_mux = bank->p_mux;
234 	while (p_mux && loop_count) {
235 		if (pin_id >= p_mux->sid && pin_id <= p_mux->eid) {
236 			bank = NULL;
237 			for (i = 0; i < info->nbanks; i++) {
238 				if (info->banks[i].bank_id == p_mux->m_bank_id) {
239 					bank = &info->banks[i];
240 					break;
241 				}
242 			}
243 
244 			if (!bank || !bank->reg_mux)
245 				return -EINVAL;
246 
247 			shift = ((pin_id - p_mux->sid) << 2) + p_mux->s_bit_offs;
248 			reg = (shift / 32) * 4;
249 			offset = shift % 32;
250 			return regmap_update_bits(bank->reg_mux, reg,
251 					0xf << offset, (func & 0xf) << offset);
252 		}
253 		p_mux = p_mux->next;
254 		loop_count--;
255 	}
256 
257 	/* normal mux reg set */
258 	if (!bank->reg_mux)
259 		return 0;
260 
261 	aml_pmx_calc_reg_and_offset(range, pin_id, &reg, &offset);
262 	return regmap_update_bits(bank->reg_mux, reg,
263 			0xf << offset, (func & 0xf) << offset);
264 }
265 
aml_pmx_get_funcs_count(struct pinctrl_dev * pctldev)266 static int aml_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
267 {
268 	struct aml_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
269 
270 	return info->nfunctions;
271 }
272 
aml_pmx_get_fname(struct pinctrl_dev * pctldev,unsigned int selector)273 static const char *aml_pmx_get_fname(struct pinctrl_dev *pctldev,
274 				     unsigned int selector)
275 {
276 	struct aml_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
277 
278 	return info->functions[selector].name;
279 }
280 
aml_pmx_get_groups(struct pinctrl_dev * pctldev,unsigned int selector,const char * const ** grps,unsigned * const ngrps)281 static int aml_pmx_get_groups(struct pinctrl_dev *pctldev,
282 			      unsigned int selector,
283 			      const char * const **grps,
284 			      unsigned * const ngrps)
285 {
286 	struct aml_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
287 
288 	*grps = info->functions[selector].groups;
289 	*ngrps = info->functions[selector].ngroups;
290 
291 	return 0;
292 }
293 
aml_pmx_set_mux(struct pinctrl_dev * pctldev,unsigned int fselector,unsigned int group_id)294 static int aml_pmx_set_mux(struct pinctrl_dev *pctldev, unsigned int fselector,
295 			   unsigned int group_id)
296 {
297 	struct aml_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
298 	struct aml_pctl_group *group = &info->groups[group_id];
299 	struct pinctrl_gpio_range *range;
300 	int i;
301 
302 	for (i = 0; i < group->npins; i++) {
303 		range =  pinctrl_find_gpio_range_from_pin_nolock(pctldev, group->pins[i]);
304 		aml_pctl_set_function(info, range, group->pins[i], group->func[i]);
305 	}
306 
307 	return 0;
308 }
309 
aml_pmx_request_gpio(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int pin)310 static int aml_pmx_request_gpio(struct pinctrl_dev *pctldev,
311 				struct pinctrl_gpio_range *range,
312 				unsigned int pin)
313 {
314 	struct aml_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
315 
316 	return aml_pctl_set_function(info, range, pin, 0);
317 }
318 
319 static const struct pinmux_ops aml_pmx_ops = {
320 	.set_mux		= aml_pmx_set_mux,
321 	.get_functions_count	= aml_pmx_get_funcs_count,
322 	.get_function_name	= aml_pmx_get_fname,
323 	.get_function_groups	= aml_pmx_get_groups,
324 	.gpio_request_enable	= aml_pmx_request_gpio,
325 };
326 
aml_calc_reg_and_bit(struct pinctrl_gpio_range * range,unsigned int pin,unsigned int reg_type,unsigned int * reg,unsigned int * bit)327 static int aml_calc_reg_and_bit(struct pinctrl_gpio_range *range,
328 				unsigned int pin,
329 				unsigned int reg_type,
330 				unsigned int *reg, unsigned int *bit)
331 {
332 	struct aml_gpio_bank *bank = gpio_chip_to_bank(range->gc);
333 
334 	*bit = (pin - range->pin_base) * aml_bit_strides[reg_type]
335 		+ bank->pc.bit_offset[reg_type];
336 	*reg = (bank->pc.reg_offset[reg_type] + (*bit / 32)) * 4;
337 	*bit &= 0x1f;
338 
339 	return 0;
340 }
341 
aml_pinconf_get_pull(struct aml_pinctrl * info,unsigned int pin)342 static int aml_pinconf_get_pull(struct aml_pinctrl *info, unsigned int pin)
343 {
344 	struct pinctrl_gpio_range *range =
345 			 pinctrl_find_gpio_range_from_pin_nolock(info->pctl, pin);
346 	struct aml_gpio_bank *bank = gpio_chip_to_bank(range->gc);
347 	unsigned int reg, bit, val;
348 	int ret, conf;
349 
350 	aml_calc_reg_and_bit(range, pin, AML_REG_PULLEN, &reg, &bit);
351 
352 	ret = regmap_read(bank->reg_gpio, reg, &val);
353 	if (ret)
354 		return ret;
355 
356 	if (!(val & BIT(bit))) {
357 		conf = PIN_CONFIG_BIAS_DISABLE;
358 	} else {
359 		aml_calc_reg_and_bit(range, pin, AML_REG_PULL, &reg, &bit);
360 
361 		ret = regmap_read(bank->reg_gpio, reg, &val);
362 		if (ret)
363 			return ret;
364 
365 		if (val & BIT(bit))
366 			conf = PIN_CONFIG_BIAS_PULL_UP;
367 		else
368 			conf = PIN_CONFIG_BIAS_PULL_DOWN;
369 	}
370 
371 	return conf;
372 }
373 
aml_pinconf_get_drive_strength(struct aml_pinctrl * info,unsigned int pin,u16 * drive_strength_ua)374 static int aml_pinconf_get_drive_strength(struct aml_pinctrl *info,
375 					  unsigned int pin,
376 					  u16 *drive_strength_ua)
377 {
378 	struct pinctrl_gpio_range *range =
379 			 pinctrl_find_gpio_range_from_pin_nolock(info->pctl, pin);
380 	struct aml_gpio_bank *bank = gpio_chip_to_bank(range->gc);
381 	unsigned int reg, bit;
382 	unsigned int val;
383 	int ret;
384 
385 	if (!bank->reg_ds)
386 		return -EOPNOTSUPP;
387 
388 	aml_calc_reg_and_bit(range, pin, AML_REG_DS, &reg, &bit);
389 	ret = regmap_read(bank->reg_ds, reg, &val);
390 	if (ret)
391 		return ret;
392 
393 	switch ((val >> bit) & 0x3) {
394 	case PINCONF_DRV_500UA:
395 		*drive_strength_ua = 500;
396 		break;
397 	case PINCONF_DRV_2500UA:
398 		*drive_strength_ua = 2500;
399 		break;
400 	case PINCONF_DRV_3000UA:
401 		*drive_strength_ua = 3000;
402 		break;
403 	case PINCONF_DRV_4000UA:
404 		*drive_strength_ua = 4000;
405 		break;
406 	default:
407 		return -EINVAL;
408 	}
409 
410 	return 0;
411 }
412 
aml_pinconf_get_gpio_bit(struct aml_pinctrl * info,unsigned int pin,unsigned int reg_type)413 static int aml_pinconf_get_gpio_bit(struct aml_pinctrl *info,
414 				    unsigned int pin,
415 				    unsigned int reg_type)
416 {
417 	struct pinctrl_gpio_range *range =
418 			 pinctrl_find_gpio_range_from_pin_nolock(info->pctl, pin);
419 	struct aml_gpio_bank *bank = gpio_chip_to_bank(range->gc);
420 	unsigned int reg, bit, val;
421 	int ret;
422 
423 	aml_calc_reg_and_bit(range, pin, reg_type, &reg, &bit);
424 	ret = regmap_read(bank->reg_gpio, reg, &val);
425 	if (ret)
426 		return ret;
427 
428 	return BIT(bit) & val ? 1 : 0;
429 }
430 
aml_pinconf_get_output(struct aml_pinctrl * info,unsigned int pin)431 static int aml_pinconf_get_output(struct aml_pinctrl *info,
432 				  unsigned int pin)
433 {
434 	int ret = aml_pinconf_get_gpio_bit(info, pin, AML_REG_DIR);
435 
436 	if (ret < 0)
437 		return ret;
438 
439 	return !ret;
440 }
441 
aml_pinconf_get_drive(struct aml_pinctrl * info,unsigned int pin)442 static int aml_pinconf_get_drive(struct aml_pinctrl *info,
443 				 unsigned int pin)
444 {
445 	return aml_pinconf_get_gpio_bit(info, pin, AML_REG_OUT);
446 }
447 
aml_pinconf_get(struct pinctrl_dev * pcdev,unsigned int pin,unsigned long * config)448 static int aml_pinconf_get(struct pinctrl_dev *pcdev, unsigned int pin,
449 			   unsigned long *config)
450 {
451 	struct aml_pinctrl *info = pinctrl_dev_get_drvdata(pcdev);
452 	enum pin_config_param param = pinconf_to_config_param(*config);
453 	u16 arg;
454 	int ret;
455 
456 	switch (param) {
457 	case PIN_CONFIG_BIAS_DISABLE:
458 	case PIN_CONFIG_BIAS_PULL_DOWN:
459 	case PIN_CONFIG_BIAS_PULL_UP:
460 		if (aml_pinconf_get_pull(info, pin) == param)
461 			arg = 1;
462 		else
463 			return -EINVAL;
464 		break;
465 	case PIN_CONFIG_DRIVE_STRENGTH_UA:
466 		ret = aml_pinconf_get_drive_strength(info, pin, &arg);
467 		if (ret)
468 			return ret;
469 		break;
470 	case PIN_CONFIG_OUTPUT_ENABLE:
471 		ret = aml_pinconf_get_output(info, pin);
472 		if (ret < 0)
473 			return -EINVAL;
474 		arg = ret;
475 		break;
476 	case PIN_CONFIG_INPUT_ENABLE:
477 		ret = aml_pinconf_get_output(info, pin);
478 		if (ret < 0)
479 			return -EINVAL;
480 		arg = !ret;
481 		break;
482 	case PIN_CONFIG_LEVEL:
483 		ret = aml_pinconf_get_output(info, pin);
484 		if (ret <= 0)
485 			return -EINVAL;
486 
487 		ret = aml_pinconf_get_drive(info, pin);
488 		if (ret < 0)
489 			return -EINVAL;
490 
491 		arg = ret;
492 		break;
493 
494 	default:
495 		return -ENOTSUPP;
496 	}
497 
498 	*config = pinconf_to_config_packed(param, arg);
499 	dev_dbg(info->dev, "pinconf for pin %u is %lu\n", pin, *config);
500 
501 	return 0;
502 }
503 
aml_pinconf_disable_bias(struct aml_pinctrl * info,unsigned int pin)504 static int aml_pinconf_disable_bias(struct aml_pinctrl *info,
505 				    unsigned int pin)
506 {
507 	struct pinctrl_gpio_range *range =
508 			 pinctrl_find_gpio_range_from_pin_nolock(info->pctl, pin);
509 	struct aml_gpio_bank *bank = gpio_chip_to_bank(range->gc);
510 	unsigned int reg, bit = 0;
511 
512 	aml_calc_reg_and_bit(range, pin, AML_REG_PULLEN, &reg, &bit);
513 
514 	return regmap_update_bits(bank->reg_gpio, reg, BIT(bit), 0);
515 }
516 
aml_pinconf_enable_bias(struct aml_pinctrl * info,unsigned int pin,bool pull_up)517 static int aml_pinconf_enable_bias(struct aml_pinctrl *info, unsigned int pin,
518 				   bool pull_up)
519 {
520 	struct pinctrl_gpio_range *range =
521 			 pinctrl_find_gpio_range_from_pin_nolock(info->pctl, pin);
522 	struct aml_gpio_bank *bank = gpio_chip_to_bank(range->gc);
523 	unsigned int reg, bit, val = 0;
524 	int ret;
525 
526 	aml_calc_reg_and_bit(range, pin, AML_REG_PULL, &reg, &bit);
527 	if (pull_up)
528 		val = BIT(bit);
529 
530 	ret = regmap_update_bits(bank->reg_gpio, reg, BIT(bit), val);
531 	if (ret)
532 		return ret;
533 
534 	aml_calc_reg_and_bit(range, pin, AML_REG_PULLEN, &reg, &bit);
535 	return regmap_update_bits(bank->reg_gpio, reg, BIT(bit), BIT(bit));
536 }
537 
aml_pinconf_set_drive_strength(struct aml_pinctrl * info,unsigned int pin,u16 drive_strength_ua)538 static int aml_pinconf_set_drive_strength(struct aml_pinctrl *info,
539 					  unsigned int pin,
540 					  u16 drive_strength_ua)
541 {
542 	struct pinctrl_gpio_range *range =
543 			 pinctrl_find_gpio_range_from_pin_nolock(info->pctl, pin);
544 	struct aml_gpio_bank *bank = gpio_chip_to_bank(range->gc);
545 	unsigned int reg, bit, ds_val;
546 
547 	if (!bank->reg_ds) {
548 		dev_err(info->dev, "drive-strength not supported\n");
549 		return -EOPNOTSUPP;
550 	}
551 
552 	aml_calc_reg_and_bit(range, pin, AML_REG_DS, &reg, &bit);
553 
554 	if (drive_strength_ua <= 500) {
555 		ds_val = PINCONF_DRV_500UA;
556 	} else if (drive_strength_ua <= 2500) {
557 		ds_val = PINCONF_DRV_2500UA;
558 	} else if (drive_strength_ua <= 3000) {
559 		ds_val = PINCONF_DRV_3000UA;
560 	} else if (drive_strength_ua <= 4000) {
561 		ds_val = PINCONF_DRV_4000UA;
562 	} else {
563 		dev_warn_once(info->dev,
564 			      "pin %u: invalid drive-strength : %d , default to 4mA\n",
565 			      pin, drive_strength_ua);
566 		ds_val = PINCONF_DRV_4000UA;
567 	}
568 
569 	return regmap_update_bits(bank->reg_ds, reg, 0x3 << bit, ds_val << bit);
570 }
571 
aml_pinconf_set_gpio_bit(struct aml_pinctrl * info,unsigned int pin,unsigned int reg_type,bool arg)572 static int aml_pinconf_set_gpio_bit(struct aml_pinctrl *info,
573 				    unsigned int pin,
574 				    unsigned int reg_type,
575 				    bool arg)
576 {
577 	struct pinctrl_gpio_range *range =
578 			 pinctrl_find_gpio_range_from_pin_nolock(info->pctl, pin);
579 	struct aml_gpio_bank *bank = gpio_chip_to_bank(range->gc);
580 	unsigned int reg, bit;
581 
582 	aml_calc_reg_and_bit(range, pin, reg_type, &reg, &bit);
583 	return regmap_update_bits(bank->reg_gpio, reg, BIT(bit),
584 				  arg ? BIT(bit) : 0);
585 }
586 
aml_pinconf_set_output(struct aml_pinctrl * info,unsigned int pin,bool out)587 static int aml_pinconf_set_output(struct aml_pinctrl *info,
588 				  unsigned int pin,
589 				  bool out)
590 {
591 	return aml_pinconf_set_gpio_bit(info, pin, AML_REG_DIR, !out);
592 }
593 
aml_pinconf_set_drive(struct aml_pinctrl * info,unsigned int pin,bool high)594 static int aml_pinconf_set_drive(struct aml_pinctrl *info,
595 				 unsigned int pin,
596 				 bool high)
597 {
598 	return aml_pinconf_set_gpio_bit(info, pin, AML_REG_OUT, high);
599 }
600 
aml_pinconf_set_output_drive(struct aml_pinctrl * info,unsigned int pin,bool high)601 static int aml_pinconf_set_output_drive(struct aml_pinctrl *info,
602 					unsigned int pin,
603 					bool high)
604 {
605 	int ret;
606 
607 	ret = aml_pinconf_set_drive(info, pin, high);
608 	if (ret)
609 		return ret;
610 
611 	return aml_pinconf_set_output(info, pin, true);
612 }
613 
aml_pinconf_set(struct pinctrl_dev * pcdev,unsigned int pin,unsigned long * configs,unsigned int num_configs)614 static int aml_pinconf_set(struct pinctrl_dev *pcdev, unsigned int pin,
615 			   unsigned long *configs, unsigned int num_configs)
616 {
617 	struct aml_pinctrl *info = pinctrl_dev_get_drvdata(pcdev);
618 	enum pin_config_param param;
619 	unsigned int arg = 0;
620 	int i, ret;
621 
622 	for (i = 0; i < num_configs; i++) {
623 		param = pinconf_to_config_param(configs[i]);
624 
625 		switch (param) {
626 		case PIN_CONFIG_DRIVE_STRENGTH_UA:
627 		case PIN_CONFIG_OUTPUT_ENABLE:
628 		case PIN_CONFIG_INPUT_ENABLE:
629 		case PIN_CONFIG_LEVEL:
630 			arg = pinconf_to_config_argument(configs[i]);
631 			break;
632 
633 		default:
634 			break;
635 		}
636 
637 		switch (param) {
638 		case PIN_CONFIG_BIAS_DISABLE:
639 			ret = aml_pinconf_disable_bias(info, pin);
640 			break;
641 		case PIN_CONFIG_BIAS_PULL_UP:
642 			ret = aml_pinconf_enable_bias(info, pin, true);
643 			break;
644 		case PIN_CONFIG_BIAS_PULL_DOWN:
645 			ret = aml_pinconf_enable_bias(info, pin, false);
646 			break;
647 		case PIN_CONFIG_DRIVE_STRENGTH_UA:
648 			ret = aml_pinconf_set_drive_strength(info, pin, arg);
649 			break;
650 		case PIN_CONFIG_OUTPUT_ENABLE:
651 			ret = aml_pinconf_set_output(info, pin, arg);
652 			break;
653 		case PIN_CONFIG_INPUT_ENABLE:
654 			ret = aml_pinconf_set_output(info, pin, !arg);
655 			break;
656 		case PIN_CONFIG_LEVEL:
657 			ret = aml_pinconf_set_output_drive(info, pin, arg);
658 			break;
659 		default:
660 			ret = -ENOTSUPP;
661 		}
662 
663 		if (ret)
664 			return ret;
665 	}
666 
667 	return 0;
668 }
669 
aml_pinconf_group_set(struct pinctrl_dev * pcdev,unsigned int num_group,unsigned long * configs,unsigned int num_configs)670 static int aml_pinconf_group_set(struct pinctrl_dev *pcdev,
671 				 unsigned int num_group,
672 				 unsigned long *configs,
673 				 unsigned int num_configs)
674 {
675 	struct aml_pinctrl *info = pinctrl_dev_get_drvdata(pcdev);
676 	int i;
677 
678 	for (i = 0; i < info->groups[num_group].npins; i++) {
679 		aml_pinconf_set(pcdev, info->groups[num_group].pins[i], configs,
680 				num_configs);
681 	}
682 
683 	return 0;
684 }
685 
aml_pinconf_group_get(struct pinctrl_dev * pcdev,unsigned int group,unsigned long * config)686 static int aml_pinconf_group_get(struct pinctrl_dev *pcdev,
687 				 unsigned int group, unsigned long *config)
688 {
689 	return -EOPNOTSUPP;
690 }
691 
692 static const struct pinconf_ops aml_pinconf_ops = {
693 	.pin_config_get		= aml_pinconf_get,
694 	.pin_config_set		= aml_pinconf_set,
695 	.pin_config_group_get	= aml_pinconf_group_get,
696 	.pin_config_group_set	= aml_pinconf_group_set,
697 	.is_generic		= true,
698 };
699 
aml_get_groups_count(struct pinctrl_dev * pctldev)700 static int aml_get_groups_count(struct pinctrl_dev *pctldev)
701 {
702 	struct aml_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
703 
704 	return info->ngroups;
705 }
706 
aml_get_group_name(struct pinctrl_dev * pctldev,unsigned int selector)707 static const char *aml_get_group_name(struct pinctrl_dev *pctldev,
708 				      unsigned int selector)
709 {
710 	struct aml_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
711 
712 	return info->groups[selector].name;
713 }
714 
aml_get_group_pins(struct pinctrl_dev * pctldev,unsigned int selector,const unsigned int ** pins,unsigned int * npins)715 static int aml_get_group_pins(struct pinctrl_dev *pctldev,
716 			      unsigned int selector, const unsigned int **pins,
717 			      unsigned int *npins)
718 {
719 	struct aml_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
720 
721 	if (selector >= info->ngroups)
722 		return -EINVAL;
723 
724 	*pins = info->groups[selector].pins;
725 	*npins = info->groups[selector].npins;
726 
727 	return 0;
728 }
729 
aml_pin_dbg_show(struct pinctrl_dev * pcdev,struct seq_file * s,unsigned int offset)730 static void aml_pin_dbg_show(struct pinctrl_dev *pcdev, struct seq_file *s,
731 			     unsigned int offset)
732 {
733 	seq_printf(s, " %s", dev_name(pcdev->dev));
734 }
735 
aml_dt_node_to_map_pinmux(struct pinctrl_dev * pctldev,struct device_node * np,struct pinctrl_map ** map,unsigned int * num_maps)736 static int aml_dt_node_to_map_pinmux(struct pinctrl_dev *pctldev,
737 				     struct device_node *np,
738 				     struct pinctrl_map **map,
739 				     unsigned int *num_maps)
740 {
741 	struct device *dev = pctldev->dev;
742 	unsigned long *configs = NULL;
743 	unsigned int num_configs = 0;
744 	struct property *prop;
745 	unsigned int reserved_maps;
746 	int reserve;
747 	int ret;
748 
749 	prop = of_find_property(np, "pinmux", NULL);
750 	if (!prop) {
751 		dev_info(dev, "Missing pinmux property\n");
752 		return -ENOENT;
753 	}
754 
755 	struct device_node *pnode __free(device_node) = of_get_parent(np);
756 	if (!pnode) {
757 		dev_info(dev, "Missing function node\n");
758 		return -EINVAL;
759 	}
760 
761 	reserved_maps = 0;
762 	*map = NULL;
763 	*num_maps = 0;
764 
765 	ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
766 					      &num_configs);
767 	if (ret < 0) {
768 		dev_err(dev, "%pOF: could not parse node property\n", np);
769 		return ret;
770 	}
771 
772 	reserve = 1;
773 	if (num_configs)
774 		reserve++;
775 
776 	ret = pinctrl_utils_reserve_map(pctldev, map, &reserved_maps,
777 					num_maps, reserve);
778 	if (ret < 0)
779 		goto exit;
780 
781 	ret = pinctrl_utils_add_map_mux(pctldev, map,
782 					&reserved_maps, num_maps, np->name,
783 					pnode->name);
784 	if (ret < 0)
785 		goto exit;
786 
787 	if (num_configs) {
788 		ret = pinctrl_utils_add_map_configs(pctldev, map, &reserved_maps,
789 						    num_maps, np->name, configs,
790 						    num_configs, PIN_MAP_TYPE_CONFIGS_GROUP);
791 		if (ret < 0)
792 			goto exit;
793 	}
794 
795 exit:
796 	kfree(configs);
797 	if (ret)
798 		pinctrl_utils_free_map(pctldev, *map, *num_maps);
799 
800 	return ret;
801 }
802 
803 static const struct pinctrl_ops aml_pctrl_ops = {
804 	.get_groups_count	= aml_get_groups_count,
805 	.get_group_name		= aml_get_group_name,
806 	.get_group_pins		= aml_get_group_pins,
807 	.dt_node_to_map		= aml_dt_node_to_map_pinmux,
808 	.dt_free_map		= pinconf_generic_dt_free_map,
809 	.pin_dbg_show		= aml_pin_dbg_show,
810 };
811 
aml_pctl_parse_functions(struct device_node * np,struct aml_pinctrl * info,u32 index,int * grp_index)812 static int aml_pctl_parse_functions(struct device_node *np,
813 				    struct aml_pinctrl *info, u32 index,
814 				    int *grp_index)
815 {
816 	struct device *dev = info->dev;
817 	struct aml_pmx_func *func;
818 	struct aml_pctl_group *grp;
819 	int ret, i;
820 
821 	func = &info->functions[index];
822 	func->name = np->name;
823 	func->ngroups = of_get_child_count(np);
824 	if (func->ngroups == 0)
825 		return dev_err_probe(dev, -EINVAL, "No groups defined\n");
826 
827 	func->groups = devm_kcalloc(dev, func->ngroups, sizeof(*func->groups), GFP_KERNEL);
828 	if (!func->groups)
829 		return -ENOMEM;
830 
831 	i = 0;
832 	for_each_child_of_node_scoped(np, child) {
833 		func->groups[i++] = child->name;
834 		grp = &info->groups[*grp_index];
835 		grp->name = child->name;
836 		*grp_index += 1;
837 		ret = pinconf_generic_parse_dt_pinmux(child, dev, &grp->pins,
838 						      &grp->func, &grp->npins);
839 		if (ret) {
840 			dev_err(dev, "function :%s, groups:%s fail\n", func->name, child->name);
841 			return ret;
842 		}
843 	}
844 	dev_dbg(dev, "Function[%d\t name:%s,\tgroups:%d]\n", index, func->name, func->ngroups);
845 
846 	return 0;
847 }
848 
aml_bank_pins(struct device_node * np)849 static u32 aml_bank_pins(struct device_node *np)
850 {
851 	struct of_phandle_args of_args;
852 
853 	if (of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
854 					     0, &of_args))
855 		return 0;
856 
857 	of_node_put(of_args.np);
858 	return of_args.args[2];
859 }
860 
aml_bank_number(struct device_node * np)861 static int aml_bank_number(struct device_node *np)
862 {
863 	struct of_phandle_args of_args;
864 
865 	if (of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
866 					     0, &of_args))
867 		return -EINVAL;
868 
869 	of_node_put(of_args.np);
870 	return of_args.args[1] >> 8;
871 }
872 
aml_count_pins(struct device_node * np)873 static unsigned int aml_count_pins(struct device_node *np)
874 {
875 	struct device_node *child;
876 	unsigned int pins = 0;
877 
878 	for_each_child_of_node(np, child) {
879 		if (of_property_read_bool(child, "gpio-controller"))
880 			pins += aml_bank_pins(child);
881 	}
882 
883 	return pins;
884 }
885 
886 /*
887  * A pinctrl device contains two types of nodes. The one named GPIO
888  * bank which includes gpio-controller property. The other one named
889  * function which includes one or more pin groups. The pin group
890  * include pinmux property(global index in pinctrl dev, and mux vlaue
891  * in mux reg) and pin configuration properties.
892  */
aml_pctl_dt_child_count(struct aml_pinctrl * info,struct device_node * np)893 static void aml_pctl_dt_child_count(struct aml_pinctrl *info,
894 				    struct device_node *np)
895 {
896 	struct device_node *child;
897 
898 	for_each_child_of_node(np, child) {
899 		if (of_property_read_bool(child, "gpio-controller")) {
900 			info->nbanks++;
901 		} else {
902 			info->nfunctions++;
903 			info->ngroups += of_get_child_count(child);
904 		}
905 	}
906 }
907 
aml_map_resource(struct device * dev,unsigned int id,struct device_node * node,char * name)908 static struct regmap *aml_map_resource(struct device *dev, unsigned int id,
909 				       struct device_node *node, char *name)
910 {
911 	struct resource res;
912 	void __iomem *base;
913 	int i;
914 
915 	struct regmap_config aml_regmap_config = {
916 		.reg_bits = 32,
917 		.val_bits = 32,
918 		.reg_stride = 4,
919 	};
920 
921 	i = of_property_match_string(node, "reg-names", name);
922 	if (i < 0)
923 		return NULL;
924 	if (of_address_to_resource(node, i, &res))
925 		return NULL;
926 	base = devm_ioremap_resource(dev, &res);
927 	if (IS_ERR(base))
928 		return ERR_CAST(base);
929 
930 	aml_regmap_config.max_register = resource_size(&res) - 4;
931 	aml_regmap_config.name = devm_kasprintf(dev, GFP_KERNEL,
932 						"%s-%s", aml_bank_name[id], name);
933 	if (!aml_regmap_config.name)
934 		return ERR_PTR(-ENOMEM);
935 
936 	return devm_regmap_init_mmio(dev, base, &aml_regmap_config);
937 }
938 
aml_gpio_calc_reg_and_bit(struct aml_gpio_bank * bank,unsigned int reg_type,unsigned int gpio,unsigned int * reg,unsigned int * bit)939 static inline int aml_gpio_calc_reg_and_bit(struct aml_gpio_bank *bank,
940 					    unsigned int reg_type,
941 					    unsigned int gpio,
942 					    unsigned int *reg,
943 					    unsigned int *bit)
944 {
945 	*bit = gpio * aml_bit_strides[reg_type] + bank->pc.bit_offset[reg_type];
946 	*reg = (bank->pc.reg_offset[reg_type] + (*bit / 32)) * 4;
947 	*bit &= 0x1f;
948 
949 	return 0;
950 }
951 
aml_gpio_get_direction(struct gpio_chip * chip,unsigned int gpio)952 static int aml_gpio_get_direction(struct gpio_chip *chip, unsigned int gpio)
953 {
954 	struct aml_gpio_bank *bank = gpiochip_get_data(chip);
955 	unsigned int bit, reg, val;
956 	int ret;
957 
958 	aml_gpio_calc_reg_and_bit(bank, AML_REG_DIR, gpio, &reg, &bit);
959 
960 	ret = regmap_read(bank->reg_gpio, reg, &val);
961 	if (ret)
962 		return ret;
963 
964 	return BIT(bit) & val ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
965 }
966 
aml_gpio_direction_input(struct gpio_chip * chip,unsigned int gpio)967 static int aml_gpio_direction_input(struct gpio_chip *chip, unsigned int gpio)
968 {
969 	struct aml_gpio_bank *bank = gpiochip_get_data(chip);
970 	unsigned int bit, reg;
971 
972 	aml_gpio_calc_reg_and_bit(bank, AML_REG_DIR, gpio, &reg, &bit);
973 
974 	return regmap_update_bits(bank->reg_gpio, reg, BIT(bit), BIT(bit));
975 }
976 
aml_gpio_direction_output(struct gpio_chip * chip,unsigned int gpio,int value)977 static int aml_gpio_direction_output(struct gpio_chip *chip, unsigned int gpio,
978 				     int value)
979 {
980 	struct aml_gpio_bank *bank = gpiochip_get_data(chip);
981 	unsigned int bit, reg;
982 	int ret;
983 
984 	aml_gpio_calc_reg_and_bit(bank, AML_REG_OUT, gpio, &reg, &bit);
985 	ret = regmap_update_bits(bank->reg_gpio, reg, BIT(bit),
986 				 value ? BIT(bit) : 0);
987 	if (ret < 0)
988 		return ret;
989 
990 	aml_gpio_calc_reg_and_bit(bank, AML_REG_DIR, gpio, &reg, &bit);
991 	return regmap_update_bits(bank->reg_gpio, reg, BIT(bit), 0);
992 }
993 
aml_gpio_set(struct gpio_chip * chip,unsigned int gpio,int value)994 static int aml_gpio_set(struct gpio_chip *chip, unsigned int gpio, int value)
995 {
996 	struct aml_gpio_bank *bank = gpiochip_get_data(chip);
997 	unsigned int bit, reg;
998 
999 	aml_gpio_calc_reg_and_bit(bank, AML_REG_OUT, gpio, &reg, &bit);
1000 
1001 	return regmap_update_bits(bank->reg_gpio, reg, BIT(bit),
1002 				  value ? BIT(bit) : 0);
1003 }
1004 
aml_gpio_get(struct gpio_chip * chip,unsigned int gpio)1005 static int aml_gpio_get(struct gpio_chip *chip, unsigned int gpio)
1006 {
1007 	struct aml_gpio_bank *bank = gpiochip_get_data(chip);
1008 	unsigned int reg, bit, val;
1009 
1010 	aml_gpio_calc_reg_and_bit(bank, AML_REG_IN, gpio, &reg, &bit);
1011 	regmap_read(bank->reg_gpio, reg, &val);
1012 
1013 	return !!(val & BIT(bit));
1014 }
1015 
1016 static const struct gpio_chip aml_gpio_template = {
1017 	.request		= gpiochip_generic_request,
1018 	.free			= gpiochip_generic_free,
1019 	.set_config		= gpiochip_generic_config,
1020 	.set			= aml_gpio_set,
1021 	.get			= aml_gpio_get,
1022 	.direction_input	= aml_gpio_direction_input,
1023 	.direction_output	= aml_gpio_direction_output,
1024 	.get_direction		= aml_gpio_get_direction,
1025 	.can_sleep		= true,
1026 };
1027 
init_bank_register_bit(struct aml_pinctrl * info,struct aml_gpio_bank * bank)1028 static void init_bank_register_bit(struct aml_pinctrl *info,
1029 				   struct aml_gpio_bank *bank)
1030 {
1031 	const struct aml_pctl_data *data = info->data;
1032 	const struct multi_mux *p_mux;
1033 	int i;
1034 
1035 	for (i = 0; i < AML_NUM_REG; i++) {
1036 		bank->pc.reg_offset[i] = aml_def_regoffs[i];
1037 		bank->pc.bit_offset[i] = 0;
1038 	}
1039 
1040 	bank->mux_bit_offs = 0;
1041 
1042 	if (data) {
1043 		for (i = 0; i < data->number; i++) {
1044 			p_mux = &data->p_mux[i];
1045 			if (bank->bank_id == p_mux->m_bank_id) {
1046 				bank->mux_bit_offs = p_mux->m_bit_offs;
1047 				break;
1048 			}
1049 			if (p_mux->sid >> 8 == bank->bank_id) {
1050 				bank->p_mux = p_mux;
1051 				break;
1052 			}
1053 		}
1054 	}
1055 }
1056 
aml_gpiolib_register_bank(struct aml_pinctrl * info,int bank_nr,struct device_node * np)1057 static int aml_gpiolib_register_bank(struct aml_pinctrl *info,
1058 				     int bank_nr, struct device_node *np)
1059 {
1060 	struct aml_gpio_bank *bank = &info->banks[bank_nr];
1061 	struct device *dev = info->dev;
1062 	int ret = 0;
1063 
1064 	ret = aml_bank_number(np);
1065 	if (ret < 0) {
1066 		dev_err(dev, "get num=%d bank identity fail\n", bank_nr);
1067 		return -EINVAL;
1068 	}
1069 	bank->bank_id = ret;
1070 
1071 	bank->reg_mux = aml_map_resource(dev, bank->bank_id, np, "mux");
1072 	if (IS_ERR_OR_NULL(bank->reg_mux)) {
1073 		if (bank->bank_id == AMLOGIC_GPIO_TEST_N ||
1074 		    bank->bank_id == AMLOGIC_GPIO_ANALOG)
1075 			bank->reg_mux = NULL;
1076 		else
1077 			return dev_err_probe(dev, bank->reg_mux ? PTR_ERR(bank->reg_mux) : -ENOENT,
1078 					     "mux registers not found\n");
1079 	}
1080 
1081 	bank->reg_gpio = aml_map_resource(dev, bank->bank_id, np, "gpio");
1082 	if (IS_ERR_OR_NULL(bank->reg_gpio))
1083 		return dev_err_probe(dev, bank->reg_gpio ? PTR_ERR(bank->reg_gpio) : -ENOENT,
1084 				     "gpio registers not found\n");
1085 
1086 	bank->reg_ds = aml_map_resource(dev, bank->bank_id, np, "ds");
1087 	if (IS_ERR_OR_NULL(bank->reg_ds)) {
1088 		dev_dbg(info->dev, "ds registers not found - skipping\n");
1089 		bank->reg_ds = bank->reg_gpio;
1090 	}
1091 
1092 	bank->gpio_chip = aml_gpio_template;
1093 	bank->gpio_chip.base = -1;
1094 	bank->gpio_chip.ngpio = aml_bank_pins(np);
1095 	bank->gpio_chip.fwnode = of_fwnode_handle(np);
1096 	bank->gpio_chip.parent = dev;
1097 
1098 	init_bank_register_bit(info, bank);
1099 	bank->gpio_chip.label = aml_bank_name[bank->bank_id];
1100 
1101 	bank->pin_base = bank->bank_id << 8;
1102 
1103 	return 0;
1104 }
1105 
aml_pctl_probe_dt(struct platform_device * pdev,struct pinctrl_desc * pctl_desc,struct aml_pinctrl * info)1106 static int aml_pctl_probe_dt(struct platform_device *pdev,
1107 			     struct pinctrl_desc *pctl_desc,
1108 			     struct aml_pinctrl *info)
1109 {
1110 	struct device *dev = &pdev->dev;
1111 	struct pinctrl_pin_desc *pdesc;
1112 	struct device_node *np = dev->of_node;
1113 	int grp_index = 0;
1114 	int i = 0, j = 0, k = 0, bank;
1115 	int ret = 0;
1116 
1117 	aml_pctl_dt_child_count(info, np);
1118 	if (!info->nbanks)
1119 		return dev_err_probe(dev, -EINVAL, "you need at least one gpio bank\n");
1120 
1121 	dev_dbg(dev, "nbanks = %d\n", info->nbanks);
1122 	dev_dbg(dev, "nfunctions = %d\n", info->nfunctions);
1123 	dev_dbg(dev, "ngroups = %d\n", info->ngroups);
1124 
1125 	info->functions = devm_kcalloc(dev, info->nfunctions, sizeof(*info->functions), GFP_KERNEL);
1126 
1127 	info->groups = devm_kcalloc(dev, info->ngroups, sizeof(*info->groups), GFP_KERNEL);
1128 
1129 	info->banks = devm_kcalloc(dev, info->nbanks, sizeof(*info->banks), GFP_KERNEL);
1130 
1131 	if (!info->functions || !info->groups || !info->banks)
1132 		return -ENOMEM;
1133 
1134 	info->data = (struct aml_pctl_data *)of_device_get_match_data(dev);
1135 
1136 	pctl_desc->npins = aml_count_pins(np);
1137 
1138 	pdesc =	devm_kcalloc(dev, pctl_desc->npins, sizeof(*pdesc), GFP_KERNEL);
1139 	if (!pdesc)
1140 		return -ENOMEM;
1141 
1142 	pctl_desc->pins = pdesc;
1143 
1144 	bank = 0;
1145 	for_each_child_of_node_scoped(np, child) {
1146 		if (of_property_read_bool(child, "gpio-controller")) {
1147 			const char *bank_name = NULL;
1148 			char **pin_names;
1149 
1150 			ret = aml_gpiolib_register_bank(info, bank, child);
1151 			if (ret)
1152 				return ret;
1153 
1154 			k = info->banks[bank].pin_base;
1155 			bank_name = info->banks[bank].gpio_chip.label;
1156 
1157 			pin_names = devm_kasprintf_strarray(dev, bank_name,
1158 							    info->banks[bank].gpio_chip.ngpio);
1159 			if (IS_ERR(pin_names))
1160 				return PTR_ERR(pin_names);
1161 
1162 			for (j = 0; j < info->banks[bank].gpio_chip.ngpio; j++, k++) {
1163 				pdesc->number = k;
1164 				pdesc->name = pin_names[j];
1165 				pdesc++;
1166 			}
1167 			bank++;
1168 		} else {
1169 			ret = aml_pctl_parse_functions(child, info,
1170 						       i++, &grp_index);
1171 			if (ret)
1172 				return ret;
1173 		}
1174 	}
1175 
1176 	return 0;
1177 }
1178 
aml_pctl_probe(struct platform_device * pdev)1179 static int aml_pctl_probe(struct platform_device *pdev)
1180 {
1181 	struct device *dev = &pdev->dev;
1182 	struct aml_pinctrl *info;
1183 	struct pinctrl_desc *pctl_desc;
1184 	int ret, i;
1185 
1186 	pctl_desc = devm_kzalloc(dev, sizeof(*pctl_desc), GFP_KERNEL);
1187 	if (!pctl_desc)
1188 		return -ENOMEM;
1189 
1190 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1191 	if (!info)
1192 		return -ENOMEM;
1193 
1194 	info->dev = dev;
1195 	platform_set_drvdata(pdev, info);
1196 	ret = aml_pctl_probe_dt(pdev, pctl_desc, info);
1197 	if (ret)
1198 		return ret;
1199 
1200 	pctl_desc->owner	= THIS_MODULE;
1201 	pctl_desc->pctlops	= &aml_pctrl_ops;
1202 	pctl_desc->pmxops	= &aml_pmx_ops;
1203 	pctl_desc->confops	= &aml_pinconf_ops;
1204 	pctl_desc->name		= dev_name(dev);
1205 
1206 	info->pctl = devm_pinctrl_register(dev, pctl_desc, info);
1207 	if (IS_ERR(info->pctl))
1208 		return dev_err_probe(dev, PTR_ERR(info->pctl), "Failed pinctrl registration\n");
1209 
1210 	for (i = 0; i < info->nbanks; i++) {
1211 		ret  = gpiochip_add_data(&info->banks[i].gpio_chip, &info->banks[i]);
1212 		if (ret)
1213 			return dev_err_probe(dev, ret, "Failed to add gpiochip(%d)!\n", i);
1214 	}
1215 
1216 	return 0;
1217 }
1218 
1219 static const struct of_device_id aml_pctl_of_match[] = {
1220 	{ .compatible = "amlogic,pinctrl-a4", },
1221 	{ .compatible = "amlogic,pinctrl-a9", .data = &a9_priv_data, },
1222 	{ .compatible = "amlogic,pinctrl-s7", .data = &s7_priv_data, },
1223 	{ .compatible = "amlogic,pinctrl-s6", .data = &s6_priv_data, },
1224 	{ /* sentinel */ }
1225 };
1226 MODULE_DEVICE_TABLE(of, aml_pctl_of_match);
1227 
1228 static struct platform_driver aml_pctl_driver = {
1229 	.driver = {
1230 		.name = "amlogic-pinctrl",
1231 		.of_match_table = aml_pctl_of_match,
1232 	},
1233 	.probe = aml_pctl_probe,
1234 };
1235 module_platform_driver(aml_pctl_driver);
1236 
1237 MODULE_AUTHOR("Xianwei Zhao <xianwei.zhao@amlogic.com>");
1238 MODULE_DESCRIPTION("Pin controller and GPIO driver for Amlogic SoC");
1239 MODULE_LICENSE("Dual BSD/GPL");
1240