• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Pin controller and GPIO driver for Amlogic Meson SoCs
3  *
4  * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * You should have received a copy of the GNU General Public License
11  * along with this program. If not, see <http://www.gnu.org/licenses/>.
12  */
13 
14 /*
15  * The available pins are organized in banks (A,B,C,D,E,X,Y,Z,AO,
16  * BOOT,CARD for meson6, X,Y,DV,H,Z,AO,BOOT,CARD for meson8 and
17  * X,Y,DV,H,AO,BOOT,CARD,DIF for meson8b) and each bank has a
18  * variable number of pins.
19  *
20  * The AO bank is special because it belongs to the Always-On power
21  * domain which can't be powered off; the bank also uses a set of
22  * registers different from the other banks.
23  *
24  * For each pin controller there are 4 different register ranges that
25  * control the following properties of the pins:
26  *  1) pin muxing
27  *  2) pull enable/disable
28  *  3) pull up/down
29  *  4) GPIO direction, output value, input value
30  *
31  * In some cases the register ranges for pull enable and pull
32  * direction are the same and thus there are only 3 register ranges.
33  *
34  * Every pinmux group can be enabled by a specific bit in the first
35  * register range; when all groups for a given pin are disabled the
36  * pin acts as a GPIO.
37  *
38  * For the pull and GPIO configuration every bank uses a contiguous
39  * set of bits in the register sets described above; the same register
40  * can be shared by more banks with different offsets.
41  *
42  * In addition to this there are some registers shared between all
43  * banks that control the IRQ functionality. This feature is not
44  * supported at the moment by the driver.
45  */
46 
47 #include <linux/device.h>
48 #include <linux/gpio.h>
49 #include <linux/init.h>
50 #include <linux/io.h>
51 #include <linux/of.h>
52 #include <linux/of_address.h>
53 #include <linux/pinctrl/pinconf-generic.h>
54 #include <linux/pinctrl/pinconf.h>
55 #include <linux/pinctrl/pinctrl.h>
56 #include <linux/pinctrl/pinmux.h>
57 #include <linux/platform_device.h>
58 #include <linux/regmap.h>
59 #include <linux/seq_file.h>
60 
61 #include "../core.h"
62 #include "../pinctrl-utils.h"
63 #include "pinctrl-meson.h"
64 
65 /**
66  * meson_get_bank() - find the bank containing a given pin
67  *
68  * @pc:		the pinctrl instance
69  * @pin:	the pin number
70  * @bank:	the found bank
71  *
72  * Return:	0 on success, a negative value on error
73  */
meson_get_bank(struct meson_pinctrl * pc,unsigned int pin,struct meson_bank ** bank)74 static int meson_get_bank(struct meson_pinctrl *pc, unsigned int pin,
75 			  struct meson_bank **bank)
76 {
77 	int i;
78 
79 	for (i = 0; i < pc->data->num_banks; i++) {
80 		if (pin >= pc->data->banks[i].first &&
81 		    pin <= pc->data->banks[i].last) {
82 			*bank = &pc->data->banks[i];
83 			return 0;
84 		}
85 	}
86 
87 	return -EINVAL;
88 }
89 
90 /**
91  * meson_calc_reg_and_bit() - calculate register and bit for a pin
92  *
93  * @bank:	the bank containing the pin
94  * @pin:	the pin number
95  * @reg_type:	the type of register needed (pull-enable, pull, etc...)
96  * @reg:	the computed register offset
97  * @bit:	the computed bit
98  */
meson_calc_reg_and_bit(struct meson_bank * bank,unsigned int pin,enum meson_reg_type reg_type,unsigned int * reg,unsigned int * bit)99 static void meson_calc_reg_and_bit(struct meson_bank *bank, unsigned int pin,
100 				   enum meson_reg_type reg_type,
101 				   unsigned int *reg, unsigned int *bit)
102 {
103 	struct meson_reg_desc *desc = &bank->regs[reg_type];
104 
105 	*reg = desc->reg * 4;
106 	*bit = desc->bit + pin - bank->first;
107 }
108 
meson_get_groups_count(struct pinctrl_dev * pcdev)109 static int meson_get_groups_count(struct pinctrl_dev *pcdev)
110 {
111 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
112 
113 	return pc->data->num_groups;
114 }
115 
meson_get_group_name(struct pinctrl_dev * pcdev,unsigned selector)116 static const char *meson_get_group_name(struct pinctrl_dev *pcdev,
117 					unsigned selector)
118 {
119 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
120 
121 	return pc->data->groups[selector].name;
122 }
123 
meson_get_group_pins(struct pinctrl_dev * pcdev,unsigned selector,const unsigned ** pins,unsigned * num_pins)124 static int meson_get_group_pins(struct pinctrl_dev *pcdev, unsigned selector,
125 				const unsigned **pins, unsigned *num_pins)
126 {
127 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
128 
129 	*pins = pc->data->groups[selector].pins;
130 	*num_pins = pc->data->groups[selector].num_pins;
131 
132 	return 0;
133 }
134 
meson_pin_dbg_show(struct pinctrl_dev * pcdev,struct seq_file * s,unsigned offset)135 static void meson_pin_dbg_show(struct pinctrl_dev *pcdev, struct seq_file *s,
136 			       unsigned offset)
137 {
138 	seq_printf(s, " %s", dev_name(pcdev->dev));
139 }
140 
141 static const struct pinctrl_ops meson_pctrl_ops = {
142 	.get_groups_count	= meson_get_groups_count,
143 	.get_group_name		= meson_get_group_name,
144 	.get_group_pins		= meson_get_group_pins,
145 	.dt_node_to_map		= pinconf_generic_dt_node_to_map_all,
146 	.dt_free_map		= pinctrl_utils_free_map,
147 	.pin_dbg_show		= meson_pin_dbg_show,
148 };
149 
150 /**
151  * meson_pmx_disable_other_groups() - disable other groups using a given pin
152  *
153  * @pc:		meson pin controller device
154  * @pin:	number of the pin
155  * @sel_group:	index of the selected group, or -1 if none
156  *
157  * The function disables all pinmux groups using a pin except the
158  * selected one. If @sel_group is -1 all groups are disabled, leaving
159  * the pin in GPIO mode.
160  */
meson_pmx_disable_other_groups(struct meson_pinctrl * pc,unsigned int pin,int sel_group)161 static void meson_pmx_disable_other_groups(struct meson_pinctrl *pc,
162 					   unsigned int pin, int sel_group)
163 {
164 	struct meson_pmx_group *group;
165 	int i, j;
166 
167 	for (i = 0; i < pc->data->num_groups; i++) {
168 		group = &pc->data->groups[i];
169 		if (group->is_gpio || i == sel_group)
170 			continue;
171 
172 		for (j = 0; j < group->num_pins; j++) {
173 			if (group->pins[j] == pin) {
174 				/* We have found a group using the pin */
175 				regmap_update_bits(pc->reg_mux,
176 						   group->reg * 4,
177 						   BIT(group->bit), 0);
178 			}
179 		}
180 	}
181 }
182 
meson_pmx_set_mux(struct pinctrl_dev * pcdev,unsigned func_num,unsigned group_num)183 static int meson_pmx_set_mux(struct pinctrl_dev *pcdev, unsigned func_num,
184 			     unsigned group_num)
185 {
186 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
187 	struct meson_pmx_func *func = &pc->data->funcs[func_num];
188 	struct meson_pmx_group *group = &pc->data->groups[group_num];
189 	int i, ret = 0;
190 
191 	dev_dbg(pc->dev, "enable function %s, group %s\n", func->name,
192 		group->name);
193 
194 	/*
195 	 * Disable groups using the same pin.
196 	 * The selected group is not disabled to avoid glitches.
197 	 */
198 	for (i = 0; i < group->num_pins; i++)
199 		meson_pmx_disable_other_groups(pc, group->pins[i], group_num);
200 
201 	/* Function 0 (GPIO) doesn't need any additional setting */
202 	if (func_num)
203 		ret = regmap_update_bits(pc->reg_mux, group->reg * 4,
204 					 BIT(group->bit), BIT(group->bit));
205 
206 	return ret;
207 }
208 
meson_pmx_request_gpio(struct pinctrl_dev * pcdev,struct pinctrl_gpio_range * range,unsigned offset)209 static int meson_pmx_request_gpio(struct pinctrl_dev *pcdev,
210 				  struct pinctrl_gpio_range *range,
211 				  unsigned offset)
212 {
213 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
214 
215 	meson_pmx_disable_other_groups(pc, offset, -1);
216 
217 	return 0;
218 }
219 
meson_pmx_get_funcs_count(struct pinctrl_dev * pcdev)220 static int meson_pmx_get_funcs_count(struct pinctrl_dev *pcdev)
221 {
222 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
223 
224 	return pc->data->num_funcs;
225 }
226 
meson_pmx_get_func_name(struct pinctrl_dev * pcdev,unsigned selector)227 static const char *meson_pmx_get_func_name(struct pinctrl_dev *pcdev,
228 					   unsigned selector)
229 {
230 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
231 
232 	return pc->data->funcs[selector].name;
233 }
234 
meson_pmx_get_groups(struct pinctrl_dev * pcdev,unsigned selector,const char * const ** groups,unsigned * const num_groups)235 static int meson_pmx_get_groups(struct pinctrl_dev *pcdev, unsigned selector,
236 				const char * const **groups,
237 				unsigned * const num_groups)
238 {
239 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
240 
241 	*groups = pc->data->funcs[selector].groups;
242 	*num_groups = pc->data->funcs[selector].num_groups;
243 
244 	return 0;
245 }
246 
247 static const struct pinmux_ops meson_pmx_ops = {
248 	.set_mux = meson_pmx_set_mux,
249 	.get_functions_count = meson_pmx_get_funcs_count,
250 	.get_function_name = meson_pmx_get_func_name,
251 	.get_function_groups = meson_pmx_get_groups,
252 	.gpio_request_enable = meson_pmx_request_gpio,
253 };
254 
meson_pinconf_set(struct pinctrl_dev * pcdev,unsigned int pin,unsigned long * configs,unsigned num_configs)255 static int meson_pinconf_set(struct pinctrl_dev *pcdev, unsigned int pin,
256 			     unsigned long *configs, unsigned num_configs)
257 {
258 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
259 	struct meson_bank *bank;
260 	enum pin_config_param param;
261 	unsigned int reg, bit;
262 	int i, ret;
263 
264 	ret = meson_get_bank(pc, pin, &bank);
265 	if (ret)
266 		return ret;
267 
268 	for (i = 0; i < num_configs; i++) {
269 		param = pinconf_to_config_param(configs[i]);
270 
271 		switch (param) {
272 		case PIN_CONFIG_BIAS_DISABLE:
273 			dev_dbg(pc->dev, "pin %u: disable bias\n", pin);
274 
275 			meson_calc_reg_and_bit(bank, pin, REG_PULLEN, &reg,
276 					       &bit);
277 			ret = regmap_update_bits(pc->reg_pullen, reg,
278 						 BIT(bit), 0);
279 			if (ret)
280 				return ret;
281 			break;
282 		case PIN_CONFIG_BIAS_PULL_UP:
283 			dev_dbg(pc->dev, "pin %u: enable pull-up\n", pin);
284 
285 			meson_calc_reg_and_bit(bank, pin, REG_PULLEN,
286 					       &reg, &bit);
287 			ret = regmap_update_bits(pc->reg_pullen, reg,
288 						 BIT(bit), BIT(bit));
289 			if (ret)
290 				return ret;
291 
292 			meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
293 			ret = regmap_update_bits(pc->reg_pull, reg,
294 						 BIT(bit), BIT(bit));
295 			if (ret)
296 				return ret;
297 			break;
298 		case PIN_CONFIG_BIAS_PULL_DOWN:
299 			dev_dbg(pc->dev, "pin %u: enable pull-down\n", pin);
300 
301 			meson_calc_reg_and_bit(bank, pin, REG_PULLEN,
302 					       &reg, &bit);
303 			ret = regmap_update_bits(pc->reg_pullen, reg,
304 						 BIT(bit), BIT(bit));
305 			if (ret)
306 				return ret;
307 
308 			meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
309 			ret = regmap_update_bits(pc->reg_pull, reg,
310 						 BIT(bit), 0);
311 			if (ret)
312 				return ret;
313 			break;
314 		default:
315 			return -ENOTSUPP;
316 		}
317 	}
318 
319 	return 0;
320 }
321 
meson_pinconf_get_pull(struct meson_pinctrl * pc,unsigned int pin)322 static int meson_pinconf_get_pull(struct meson_pinctrl *pc, unsigned int pin)
323 {
324 	struct meson_bank *bank;
325 	unsigned int reg, bit, val;
326 	int ret, conf;
327 
328 	ret = meson_get_bank(pc, pin, &bank);
329 	if (ret)
330 		return ret;
331 
332 	meson_calc_reg_and_bit(bank, pin, REG_PULLEN, &reg, &bit);
333 
334 	ret = regmap_read(pc->reg_pullen, reg, &val);
335 	if (ret)
336 		return ret;
337 
338 	if (!(val & BIT(bit))) {
339 		conf = PIN_CONFIG_BIAS_DISABLE;
340 	} else {
341 		meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
342 
343 		ret = regmap_read(pc->reg_pull, reg, &val);
344 		if (ret)
345 			return ret;
346 
347 		if (val & BIT(bit))
348 			conf = PIN_CONFIG_BIAS_PULL_UP;
349 		else
350 			conf = PIN_CONFIG_BIAS_PULL_DOWN;
351 	}
352 
353 	return conf;
354 }
355 
meson_pinconf_get(struct pinctrl_dev * pcdev,unsigned int pin,unsigned long * config)356 static int meson_pinconf_get(struct pinctrl_dev *pcdev, unsigned int pin,
357 			     unsigned long *config)
358 {
359 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
360 	enum pin_config_param param = pinconf_to_config_param(*config);
361 	u16 arg;
362 
363 	switch (param) {
364 	case PIN_CONFIG_BIAS_DISABLE:
365 	case PIN_CONFIG_BIAS_PULL_DOWN:
366 	case PIN_CONFIG_BIAS_PULL_UP:
367 		if (meson_pinconf_get_pull(pc, pin) == param)
368 			arg = 1;
369 		else
370 			return -EINVAL;
371 		break;
372 	default:
373 		return -ENOTSUPP;
374 	}
375 
376 	*config = pinconf_to_config_packed(param, arg);
377 	dev_dbg(pc->dev, "pinconf for pin %u is %lu\n", pin, *config);
378 
379 	return 0;
380 }
381 
meson_pinconf_group_set(struct pinctrl_dev * pcdev,unsigned int num_group,unsigned long * configs,unsigned num_configs)382 static int meson_pinconf_group_set(struct pinctrl_dev *pcdev,
383 				   unsigned int num_group,
384 				   unsigned long *configs, unsigned num_configs)
385 {
386 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
387 	struct meson_pmx_group *group = &pc->data->groups[num_group];
388 	int i;
389 
390 	dev_dbg(pc->dev, "set pinconf for group %s\n", group->name);
391 
392 	for (i = 0; i < group->num_pins; i++) {
393 		meson_pinconf_set(pcdev, group->pins[i], configs,
394 				  num_configs);
395 	}
396 
397 	return 0;
398 }
399 
meson_pinconf_group_get(struct pinctrl_dev * pcdev,unsigned int group,unsigned long * config)400 static int meson_pinconf_group_get(struct pinctrl_dev *pcdev,
401 				   unsigned int group, unsigned long *config)
402 {
403 	return -ENOSYS;
404 }
405 
406 static const struct pinconf_ops meson_pinconf_ops = {
407 	.pin_config_get		= meson_pinconf_get,
408 	.pin_config_set		= meson_pinconf_set,
409 	.pin_config_group_get	= meson_pinconf_group_get,
410 	.pin_config_group_set	= meson_pinconf_group_set,
411 	.is_generic		= true,
412 };
413 
meson_gpio_request(struct gpio_chip * chip,unsigned gpio)414 static int meson_gpio_request(struct gpio_chip *chip, unsigned gpio)
415 {
416 	return pinctrl_request_gpio(chip->base + gpio);
417 }
418 
meson_gpio_free(struct gpio_chip * chip,unsigned gpio)419 static void meson_gpio_free(struct gpio_chip *chip, unsigned gpio)
420 {
421 	struct meson_pinctrl *pc = gpiochip_get_data(chip);
422 
423 	pinctrl_free_gpio(pc->data->pin_base + gpio);
424 }
425 
meson_gpio_direction_input(struct gpio_chip * chip,unsigned gpio)426 static int meson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
427 {
428 	struct meson_pinctrl *pc = gpiochip_get_data(chip);
429 	unsigned int reg, bit, pin;
430 	struct meson_bank *bank;
431 	int ret;
432 
433 	pin = pc->data->pin_base + gpio;
434 	ret = meson_get_bank(pc, pin, &bank);
435 	if (ret)
436 		return ret;
437 
438 	meson_calc_reg_and_bit(bank, pin, REG_DIR, &reg, &bit);
439 
440 	return regmap_update_bits(pc->reg_gpio, reg, BIT(bit), BIT(bit));
441 }
442 
meson_gpio_direction_output(struct gpio_chip * chip,unsigned gpio,int value)443 static int meson_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
444 				       int value)
445 {
446 	struct meson_pinctrl *pc = gpiochip_get_data(chip);
447 	unsigned int reg, bit, pin;
448 	struct meson_bank *bank;
449 	int ret;
450 
451 	pin = pc->data->pin_base + gpio;
452 	ret = meson_get_bank(pc, pin, &bank);
453 	if (ret)
454 		return ret;
455 
456 	meson_calc_reg_and_bit(bank, pin, REG_DIR, &reg, &bit);
457 	ret = regmap_update_bits(pc->reg_gpio, reg, BIT(bit), 0);
458 	if (ret)
459 		return ret;
460 
461 	meson_calc_reg_and_bit(bank, pin, REG_OUT, &reg, &bit);
462 	return regmap_update_bits(pc->reg_gpio, reg, BIT(bit),
463 				  value ? BIT(bit) : 0);
464 }
465 
meson_gpio_set(struct gpio_chip * chip,unsigned gpio,int value)466 static void meson_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
467 {
468 	struct meson_pinctrl *pc = gpiochip_get_data(chip);
469 	unsigned int reg, bit, pin;
470 	struct meson_bank *bank;
471 	int ret;
472 
473 	pin = pc->data->pin_base + gpio;
474 	ret = meson_get_bank(pc, pin, &bank);
475 	if (ret)
476 		return;
477 
478 	meson_calc_reg_and_bit(bank, pin, REG_OUT, &reg, &bit);
479 	regmap_update_bits(pc->reg_gpio, reg, BIT(bit),
480 			   value ? BIT(bit) : 0);
481 }
482 
meson_gpio_get(struct gpio_chip * chip,unsigned gpio)483 static int meson_gpio_get(struct gpio_chip *chip, unsigned gpio)
484 {
485 	struct meson_pinctrl *pc = gpiochip_get_data(chip);
486 	unsigned int reg, bit, val, pin;
487 	struct meson_bank *bank;
488 	int ret;
489 
490 	pin = pc->data->pin_base + gpio;
491 	ret = meson_get_bank(pc, pin, &bank);
492 	if (ret)
493 		return ret;
494 
495 	meson_calc_reg_and_bit(bank, pin, REG_IN, &reg, &bit);
496 	regmap_read(pc->reg_gpio, reg, &val);
497 
498 	return !!(val & BIT(bit));
499 }
500 
501 static const struct of_device_id meson_pinctrl_dt_match[] = {
502 	{
503 		.compatible = "amlogic,meson8-cbus-pinctrl",
504 		.data = &meson8_cbus_pinctrl_data,
505 	},
506 	{
507 		.compatible = "amlogic,meson8b-cbus-pinctrl",
508 		.data = &meson8b_cbus_pinctrl_data,
509 	},
510 	{
511 		.compatible = "amlogic,meson8-aobus-pinctrl",
512 		.data = &meson8_aobus_pinctrl_data,
513 	},
514 	{
515 		.compatible = "amlogic,meson8b-aobus-pinctrl",
516 		.data = &meson8b_aobus_pinctrl_data,
517 	},
518 	{
519 		.compatible = "amlogic,meson-gxbb-periphs-pinctrl",
520 		.data = &meson_gxbb_periphs_pinctrl_data,
521 	},
522 	{
523 		.compatible = "amlogic,meson-gxbb-aobus-pinctrl",
524 		.data = &meson_gxbb_aobus_pinctrl_data,
525 	},
526 	{
527 		.compatible = "amlogic,meson-gxl-periphs-pinctrl",
528 		.data = &meson_gxl_periphs_pinctrl_data,
529 	},
530 	{
531 		.compatible = "amlogic,meson-gxl-aobus-pinctrl",
532 		.data = &meson_gxl_aobus_pinctrl_data,
533 	},
534 	{ },
535 };
536 
meson_gpiolib_register(struct meson_pinctrl * pc)537 static int meson_gpiolib_register(struct meson_pinctrl *pc)
538 {
539 	int ret;
540 
541 	pc->chip.label = pc->data->name;
542 	pc->chip.parent = pc->dev;
543 	pc->chip.request = meson_gpio_request;
544 	pc->chip.free = meson_gpio_free;
545 	pc->chip.direction_input = meson_gpio_direction_input;
546 	pc->chip.direction_output = meson_gpio_direction_output;
547 	pc->chip.get = meson_gpio_get;
548 	pc->chip.set = meson_gpio_set;
549 	pc->chip.base = pc->data->pin_base;
550 	pc->chip.ngpio = pc->data->num_pins;
551 	pc->chip.can_sleep = false;
552 	pc->chip.of_node = pc->of_node;
553 	pc->chip.of_gpio_n_cells = 2;
554 
555 	ret = gpiochip_add_data(&pc->chip, pc);
556 	if (ret) {
557 		dev_err(pc->dev, "can't add gpio chip %s\n",
558 			pc->data->name);
559 		return ret;
560 	}
561 
562 	return 0;
563 }
564 
565 static struct regmap_config meson_regmap_config = {
566 	.reg_bits = 32,
567 	.val_bits = 32,
568 	.reg_stride = 4,
569 };
570 
meson_map_resource(struct meson_pinctrl * pc,struct device_node * node,char * name)571 static struct regmap *meson_map_resource(struct meson_pinctrl *pc,
572 					 struct device_node *node, char *name)
573 {
574 	struct resource res;
575 	void __iomem *base;
576 	int i;
577 
578 	i = of_property_match_string(node, "reg-names", name);
579 	if (of_address_to_resource(node, i, &res))
580 		return ERR_PTR(-ENOENT);
581 
582 	base = devm_ioremap_resource(pc->dev, &res);
583 	if (IS_ERR(base))
584 		return ERR_CAST(base);
585 
586 	meson_regmap_config.max_register = resource_size(&res) - 4;
587 	meson_regmap_config.name = devm_kasprintf(pc->dev, GFP_KERNEL,
588 						  "%s-%s", node->name,
589 						  name);
590 	if (!meson_regmap_config.name)
591 		return ERR_PTR(-ENOMEM);
592 
593 	return devm_regmap_init_mmio(pc->dev, base, &meson_regmap_config);
594 }
595 
meson_pinctrl_parse_dt(struct meson_pinctrl * pc,struct device_node * node)596 static int meson_pinctrl_parse_dt(struct meson_pinctrl *pc,
597 				  struct device_node *node)
598 {
599 	struct device_node *np, *gpio_np = NULL;
600 
601 	for_each_child_of_node(node, np) {
602 		if (!of_find_property(np, "gpio-controller", NULL))
603 			continue;
604 		if (gpio_np) {
605 			dev_err(pc->dev, "multiple gpio nodes\n");
606 			return -EINVAL;
607 		}
608 		gpio_np = np;
609 	}
610 
611 	if (!gpio_np) {
612 		dev_err(pc->dev, "no gpio node found\n");
613 		return -EINVAL;
614 	}
615 
616 	pc->of_node = gpio_np;
617 
618 	pc->reg_mux = meson_map_resource(pc, gpio_np, "mux");
619 	if (IS_ERR(pc->reg_mux)) {
620 		dev_err(pc->dev, "mux registers not found\n");
621 		return PTR_ERR(pc->reg_mux);
622 	}
623 
624 	pc->reg_pull = meson_map_resource(pc, gpio_np, "pull");
625 	if (IS_ERR(pc->reg_pull)) {
626 		dev_err(pc->dev, "pull registers not found\n");
627 		return PTR_ERR(pc->reg_pull);
628 	}
629 
630 	pc->reg_pullen = meson_map_resource(pc, gpio_np, "pull-enable");
631 	/* Use pull region if pull-enable one is not present */
632 	if (IS_ERR(pc->reg_pullen))
633 		pc->reg_pullen = pc->reg_pull;
634 
635 	pc->reg_gpio = meson_map_resource(pc, gpio_np, "gpio");
636 	if (IS_ERR(pc->reg_gpio)) {
637 		dev_err(pc->dev, "gpio registers not found\n");
638 		return PTR_ERR(pc->reg_gpio);
639 	}
640 
641 	return 0;
642 }
643 
meson_pinctrl_probe(struct platform_device * pdev)644 static int meson_pinctrl_probe(struct platform_device *pdev)
645 {
646 	const struct of_device_id *match;
647 	struct device *dev = &pdev->dev;
648 	struct meson_pinctrl *pc;
649 	int ret;
650 
651 	pc = devm_kzalloc(dev, sizeof(struct meson_pinctrl), GFP_KERNEL);
652 	if (!pc)
653 		return -ENOMEM;
654 
655 	pc->dev = dev;
656 	match = of_match_node(meson_pinctrl_dt_match, pdev->dev.of_node);
657 	pc->data = (struct meson_pinctrl_data *) match->data;
658 
659 	ret = meson_pinctrl_parse_dt(pc, pdev->dev.of_node);
660 	if (ret)
661 		return ret;
662 
663 	pc->desc.name		= "pinctrl-meson";
664 	pc->desc.owner		= THIS_MODULE;
665 	pc->desc.pctlops	= &meson_pctrl_ops;
666 	pc->desc.pmxops		= &meson_pmx_ops;
667 	pc->desc.confops	= &meson_pinconf_ops;
668 	pc->desc.pins		= pc->data->pins;
669 	pc->desc.npins		= pc->data->num_pins;
670 
671 	pc->pcdev = devm_pinctrl_register(pc->dev, &pc->desc, pc);
672 	if (IS_ERR(pc->pcdev)) {
673 		dev_err(pc->dev, "can't register pinctrl device");
674 		return PTR_ERR(pc->pcdev);
675 	}
676 
677 	return meson_gpiolib_register(pc);
678 }
679 
680 static struct platform_driver meson_pinctrl_driver = {
681 	.probe		= meson_pinctrl_probe,
682 	.driver = {
683 		.name	= "meson-pinctrl",
684 		.of_match_table = meson_pinctrl_dt_match,
685 	},
686 };
687 builtin_platform_driver(meson_pinctrl_driver);
688