2 * Pin controller and GPIO driver for Amlogic Meson SoCs
4 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
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.
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/>.
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.
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.
24 * For each of the two power domains (regular and always-on) there are
25 * 4 different register ranges that control the following properties
28 * 2) pull enable/disable
30 * 4) GPIO direction, output value, input value
32 * In some cases the register ranges for pull enable and pull
33 * direction are the same and thus there are only 3 register ranges.
35 * Every pinmux group can be enabled by a specific bit in the first
36 * register range of the domain; when all groups for a given pin are
37 * disabled the pin acts as a GPIO.
39 * For the pull and GPIO configuration every bank uses a contiguous
40 * set of bits in the register sets described above; the same register
41 * can be shared by more banks with different offsets.
43 * In addition to this there are some registers shared between all
44 * banks that control the IRQ functionality. This feature is not
45 * supported at the moment by the driver.
48 #include <linux/device.h>
49 #include <linux/gpio.h>
50 #include <linux/init.h>
53 #include <linux/of_address.h>
54 #include <linux/pinctrl/pinconf-generic.h>
55 #include <linux/pinctrl/pinconf.h>
56 #include <linux/pinctrl/pinctrl.h>
57 #include <linux/pinctrl/pinmux.h>
58 #include <linux/platform_device.h>
59 #include <linux/regmap.h>
60 #include <linux/seq_file.h>
63 #include "../pinctrl-utils.h"
64 #include "pinctrl-meson.h"
67 * meson_get_bank() - find the bank containing a given pin
69 * @domain: the domain containing the pin
70 * @pin: the pin number
71 * @bank: the found bank
73 * Return: 0 on success, a negative value on error
75 static int meson_get_bank(struct meson_domain
*domain
, unsigned int pin
,
76 struct meson_bank
**bank
)
80 for (i
= 0; i
< domain
->data
->num_banks
; i
++) {
81 if (pin
>= domain
->data
->banks
[i
].first
&&
82 pin
<= domain
->data
->banks
[i
].last
) {
83 *bank
= &domain
->data
->banks
[i
];
92 * meson_get_domain_and_bank() - find domain and bank containing a given pin
94 * @pc: Meson pin controller device
95 * @pin: the pin number
96 * @domain: the found domain
97 * @bank: the found bank
99 * Return: 0 on success, a negative value on error
101 static int meson_get_domain_and_bank(struct meson_pinctrl
*pc
, unsigned int pin
,
102 struct meson_domain
**domain
,
103 struct meson_bank
**bank
)
105 struct meson_domain
*d
;
109 if (pin
>= d
->data
->pin_base
&&
110 pin
< d
->data
->pin_base
+ d
->data
->num_pins
) {
112 return meson_get_bank(d
, pin
, bank
);
119 * meson_calc_reg_and_bit() - calculate register and bit for a pin
121 * @bank: the bank containing the pin
122 * @pin: the pin number
123 * @reg_type: the type of register needed (pull-enable, pull, etc...)
124 * @reg: the computed register offset
125 * @bit: the computed bit
127 static void meson_calc_reg_and_bit(struct meson_bank
*bank
, unsigned int pin
,
128 enum meson_reg_type reg_type
,
129 unsigned int *reg
, unsigned int *bit
)
131 struct meson_reg_desc
*desc
= &bank
->regs
[reg_type
];
133 *reg
= desc
->reg
* 4;
134 *bit
= desc
->bit
+ pin
- bank
->first
;
137 static int meson_get_groups_count(struct pinctrl_dev
*pcdev
)
139 struct meson_pinctrl
*pc
= pinctrl_dev_get_drvdata(pcdev
);
141 return pc
->data
->num_groups
;
144 static const char *meson_get_group_name(struct pinctrl_dev
*pcdev
,
147 struct meson_pinctrl
*pc
= pinctrl_dev_get_drvdata(pcdev
);
149 return pc
->data
->groups
[selector
].name
;
152 static int meson_get_group_pins(struct pinctrl_dev
*pcdev
, unsigned selector
,
153 const unsigned **pins
, unsigned *num_pins
)
155 struct meson_pinctrl
*pc
= pinctrl_dev_get_drvdata(pcdev
);
157 *pins
= pc
->data
->groups
[selector
].pins
;
158 *num_pins
= pc
->data
->groups
[selector
].num_pins
;
163 static void meson_pin_dbg_show(struct pinctrl_dev
*pcdev
, struct seq_file
*s
,
166 seq_printf(s
, " %s", dev_name(pcdev
->dev
));
169 static const struct pinctrl_ops meson_pctrl_ops
= {
170 .get_groups_count
= meson_get_groups_count
,
171 .get_group_name
= meson_get_group_name
,
172 .get_group_pins
= meson_get_group_pins
,
173 .dt_node_to_map
= pinconf_generic_dt_node_to_map_all
,
174 .dt_free_map
= pinctrl_utils_dt_free_map
,
175 .pin_dbg_show
= meson_pin_dbg_show
,
179 * meson_pmx_disable_other_groups() - disable other groups using a given pin
181 * @pc: meson pin controller device
182 * @pin: number of the pin
183 * @sel_group: index of the selected group, or -1 if none
185 * The function disables all pinmux groups using a pin except the
186 * selected one. If @sel_group is -1 all groups are disabled, leaving
187 * the pin in GPIO mode.
189 static void meson_pmx_disable_other_groups(struct meson_pinctrl
*pc
,
190 unsigned int pin
, int sel_group
)
192 struct meson_pmx_group
*group
;
193 struct meson_domain
*domain
;
196 for (i
= 0; i
< pc
->data
->num_groups
; i
++) {
197 group
= &pc
->data
->groups
[i
];
198 if (group
->is_gpio
|| i
== sel_group
)
201 for (j
= 0; j
< group
->num_pins
; j
++) {
202 if (group
->pins
[j
] == pin
) {
203 /* We have found a group using the pin */
205 regmap_update_bits(domain
->reg_mux
,
213 static int meson_pmx_set_mux(struct pinctrl_dev
*pcdev
, unsigned func_num
,
216 struct meson_pinctrl
*pc
= pinctrl_dev_get_drvdata(pcdev
);
217 struct meson_pmx_func
*func
= &pc
->data
->funcs
[func_num
];
218 struct meson_pmx_group
*group
= &pc
->data
->groups
[group_num
];
219 struct meson_domain
*domain
= pc
->domain
;
222 dev_dbg(pc
->dev
, "enable function %s, group %s\n", func
->name
,
226 * Disable groups using the same pin.
227 * The selected group is not disabled to avoid glitches.
229 for (i
= 0; i
< group
->num_pins
; i
++)
230 meson_pmx_disable_other_groups(pc
, group
->pins
[i
], group_num
);
232 /* Function 0 (GPIO) doesn't need any additional setting */
234 ret
= regmap_update_bits(domain
->reg_mux
, group
->reg
* 4,
235 BIT(group
->bit
), BIT(group
->bit
));
240 static int meson_pmx_request_gpio(struct pinctrl_dev
*pcdev
,
241 struct pinctrl_gpio_range
*range
,
244 struct meson_pinctrl
*pc
= pinctrl_dev_get_drvdata(pcdev
);
246 meson_pmx_disable_other_groups(pc
, range
->pin_base
+ offset
, -1);
251 static int meson_pmx_get_funcs_count(struct pinctrl_dev
*pcdev
)
253 struct meson_pinctrl
*pc
= pinctrl_dev_get_drvdata(pcdev
);
255 return pc
->data
->num_funcs
;
258 static const char *meson_pmx_get_func_name(struct pinctrl_dev
*pcdev
,
261 struct meson_pinctrl
*pc
= pinctrl_dev_get_drvdata(pcdev
);
263 return pc
->data
->funcs
[selector
].name
;
266 static int meson_pmx_get_groups(struct pinctrl_dev
*pcdev
, unsigned selector
,
267 const char * const **groups
,
268 unsigned * const num_groups
)
270 struct meson_pinctrl
*pc
= pinctrl_dev_get_drvdata(pcdev
);
272 *groups
= pc
->data
->funcs
[selector
].groups
;
273 *num_groups
= pc
->data
->funcs
[selector
].num_groups
;
278 static const struct pinmux_ops meson_pmx_ops
= {
279 .set_mux
= meson_pmx_set_mux
,
280 .get_functions_count
= meson_pmx_get_funcs_count
,
281 .get_function_name
= meson_pmx_get_func_name
,
282 .get_function_groups
= meson_pmx_get_groups
,
283 .gpio_request_enable
= meson_pmx_request_gpio
,
286 static int meson_pinconf_set(struct pinctrl_dev
*pcdev
, unsigned int pin
,
287 unsigned long *configs
, unsigned num_configs
)
289 struct meson_pinctrl
*pc
= pinctrl_dev_get_drvdata(pcdev
);
290 struct meson_domain
*domain
;
291 struct meson_bank
*bank
;
292 enum pin_config_param param
;
293 unsigned int reg
, bit
;
297 ret
= meson_get_domain_and_bank(pc
, pin
, &domain
, &bank
);
301 for (i
= 0; i
< num_configs
; i
++) {
302 param
= pinconf_to_config_param(configs
[i
]);
303 arg
= pinconf_to_config_argument(configs
[i
]);
306 case PIN_CONFIG_BIAS_DISABLE
:
307 dev_dbg(pc
->dev
, "pin %u: disable bias\n", pin
);
309 meson_calc_reg_and_bit(bank
, pin
, REG_PULL
, ®
, &bit
);
310 ret
= regmap_update_bits(domain
->reg_pull
, reg
,
315 case PIN_CONFIG_BIAS_PULL_UP
:
316 dev_dbg(pc
->dev
, "pin %u: enable pull-up\n", pin
);
318 meson_calc_reg_and_bit(bank
, pin
, REG_PULLEN
,
320 ret
= regmap_update_bits(domain
->reg_pullen
, reg
,
325 meson_calc_reg_and_bit(bank
, pin
, REG_PULL
, ®
, &bit
);
326 ret
= regmap_update_bits(domain
->reg_pull
, reg
,
331 case PIN_CONFIG_BIAS_PULL_DOWN
:
332 dev_dbg(pc
->dev
, "pin %u: enable pull-down\n", pin
);
334 meson_calc_reg_and_bit(bank
, pin
, REG_PULLEN
,
336 ret
= regmap_update_bits(domain
->reg_pullen
, reg
,
341 meson_calc_reg_and_bit(bank
, pin
, REG_PULL
, ®
, &bit
);
342 ret
= regmap_update_bits(domain
->reg_pull
, reg
,
355 static int meson_pinconf_get_pull(struct meson_pinctrl
*pc
, unsigned int pin
)
357 struct meson_domain
*domain
;
358 struct meson_bank
*bank
;
359 unsigned int reg
, bit
, val
;
362 ret
= meson_get_domain_and_bank(pc
, pin
, &domain
, &bank
);
366 meson_calc_reg_and_bit(bank
, pin
, REG_PULLEN
, ®
, &bit
);
368 ret
= regmap_read(domain
->reg_pullen
, reg
, &val
);
372 if (!(val
& BIT(bit
))) {
373 conf
= PIN_CONFIG_BIAS_DISABLE
;
375 meson_calc_reg_and_bit(bank
, pin
, REG_PULL
, ®
, &bit
);
377 ret
= regmap_read(domain
->reg_pull
, reg
, &val
);
382 conf
= PIN_CONFIG_BIAS_PULL_UP
;
384 conf
= PIN_CONFIG_BIAS_PULL_DOWN
;
390 static int meson_pinconf_get(struct pinctrl_dev
*pcdev
, unsigned int pin
,
391 unsigned long *config
)
393 struct meson_pinctrl
*pc
= pinctrl_dev_get_drvdata(pcdev
);
394 enum pin_config_param param
= pinconf_to_config_param(*config
);
398 case PIN_CONFIG_BIAS_DISABLE
:
399 case PIN_CONFIG_BIAS_PULL_DOWN
:
400 case PIN_CONFIG_BIAS_PULL_UP
:
401 if (meson_pinconf_get_pull(pc
, pin
) == param
)
410 *config
= pinconf_to_config_packed(param
, arg
);
411 dev_dbg(pc
->dev
, "pinconf for pin %u is %lu\n", pin
, *config
);
416 static int meson_pinconf_group_set(struct pinctrl_dev
*pcdev
,
417 unsigned int num_group
,
418 unsigned long *configs
, unsigned num_configs
)
420 struct meson_pinctrl
*pc
= pinctrl_dev_get_drvdata(pcdev
);
421 struct meson_pmx_group
*group
= &pc
->data
->groups
[num_group
];
424 dev_dbg(pc
->dev
, "set pinconf for group %s\n", group
->name
);
426 for (i
= 0; i
< group
->num_pins
; i
++) {
427 meson_pinconf_set(pcdev
, group
->pins
[i
], configs
,
434 static int meson_pinconf_group_get(struct pinctrl_dev
*pcdev
,
435 unsigned int group
, unsigned long *config
)
440 static const struct pinconf_ops meson_pinconf_ops
= {
441 .pin_config_get
= meson_pinconf_get
,
442 .pin_config_set
= meson_pinconf_set
,
443 .pin_config_group_get
= meson_pinconf_group_get
,
444 .pin_config_group_set
= meson_pinconf_group_set
,
448 static int meson_gpio_request(struct gpio_chip
*chip
, unsigned gpio
)
450 return pinctrl_request_gpio(chip
->base
+ gpio
);
453 static void meson_gpio_free(struct gpio_chip
*chip
, unsigned gpio
)
455 struct meson_domain
*domain
= gpiochip_get_data(chip
);
457 pinctrl_free_gpio(domain
->data
->pin_base
+ gpio
);
460 static int meson_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
462 struct meson_domain
*domain
= gpiochip_get_data(chip
);
463 unsigned int reg
, bit
, pin
;
464 struct meson_bank
*bank
;
467 pin
= domain
->data
->pin_base
+ gpio
;
468 ret
= meson_get_bank(domain
, pin
, &bank
);
472 meson_calc_reg_and_bit(bank
, pin
, REG_DIR
, ®
, &bit
);
474 return regmap_update_bits(domain
->reg_gpio
, reg
, BIT(bit
), BIT(bit
));
477 static int meson_gpio_direction_output(struct gpio_chip
*chip
, unsigned gpio
,
480 struct meson_domain
*domain
= gpiochip_get_data(chip
);
481 unsigned int reg
, bit
, pin
;
482 struct meson_bank
*bank
;
485 pin
= domain
->data
->pin_base
+ gpio
;
486 ret
= meson_get_bank(domain
, pin
, &bank
);
490 meson_calc_reg_and_bit(bank
, pin
, REG_DIR
, ®
, &bit
);
491 ret
= regmap_update_bits(domain
->reg_gpio
, reg
, BIT(bit
), 0);
495 meson_calc_reg_and_bit(bank
, pin
, REG_OUT
, ®
, &bit
);
496 return regmap_update_bits(domain
->reg_gpio
, reg
, BIT(bit
),
497 value
? BIT(bit
) : 0);
500 static void meson_gpio_set(struct gpio_chip
*chip
, unsigned gpio
, int value
)
502 struct meson_domain
*domain
= gpiochip_get_data(chip
);
503 unsigned int reg
, bit
, pin
;
504 struct meson_bank
*bank
;
507 pin
= domain
->data
->pin_base
+ gpio
;
508 ret
= meson_get_bank(domain
, pin
, &bank
);
512 meson_calc_reg_and_bit(bank
, pin
, REG_OUT
, ®
, &bit
);
513 regmap_update_bits(domain
->reg_gpio
, reg
, BIT(bit
),
514 value
? BIT(bit
) : 0);
517 static int meson_gpio_get(struct gpio_chip
*chip
, unsigned gpio
)
519 struct meson_domain
*domain
= gpiochip_get_data(chip
);
520 unsigned int reg
, bit
, val
, pin
;
521 struct meson_bank
*bank
;
524 pin
= domain
->data
->pin_base
+ gpio
;
525 ret
= meson_get_bank(domain
, pin
, &bank
);
529 meson_calc_reg_and_bit(bank
, pin
, REG_IN
, ®
, &bit
);
530 regmap_read(domain
->reg_gpio
, reg
, &val
);
532 return !!(val
& BIT(bit
));
535 static const struct of_device_id meson_pinctrl_dt_match
[] = {
537 .compatible
= "amlogic,meson8-cbus-pinctrl",
538 .data
= &meson8_cbus_pinctrl_data
,
541 .compatible
= "amlogic,meson8b-cbus-pinctrl",
542 .data
= &meson8b_cbus_pinctrl_data
,
545 .compatible
= "amlogic,meson8-aobus-pinctrl",
546 .data
= &meson8_aobus_pinctrl_data
,
549 .compatible
= "amlogic,meson8b-aobus-pinctrl",
550 .data
= &meson8b_aobus_pinctrl_data
,
555 static int meson_gpiolib_register(struct meson_pinctrl
*pc
)
557 struct meson_domain
*domain
;
562 domain
->chip
.label
= domain
->data
->name
;
563 domain
->chip
.parent
= pc
->dev
;
564 domain
->chip
.request
= meson_gpio_request
;
565 domain
->chip
.free
= meson_gpio_free
;
566 domain
->chip
.direction_input
= meson_gpio_direction_input
;
567 domain
->chip
.direction_output
= meson_gpio_direction_output
;
568 domain
->chip
.get
= meson_gpio_get
;
569 domain
->chip
.set
= meson_gpio_set
;
570 domain
->chip
.base
= domain
->data
->pin_base
;
571 domain
->chip
.ngpio
= domain
->data
->num_pins
;
572 domain
->chip
.can_sleep
= false;
573 domain
->chip
.of_node
= domain
->of_node
;
574 domain
->chip
.of_gpio_n_cells
= 2;
576 ret
= gpiochip_add_data(&domain
->chip
, domain
);
578 dev_err(pc
->dev
, "can't add gpio chip %s\n",
583 ret
= gpiochip_add_pin_range(&domain
->chip
, dev_name(pc
->dev
),
584 0, domain
->data
->pin_base
,
587 dev_err(pc
->dev
, "can't add pin range\n");
593 gpiochip_remove(&pc
->domain
->chip
);
598 static struct regmap_config meson_regmap_config
= {
604 static struct regmap
*meson_map_resource(struct meson_pinctrl
*pc
,
605 struct device_node
*node
, char *name
)
611 i
= of_property_match_string(node
, "reg-names", name
);
612 if (of_address_to_resource(node
, i
, &res
))
613 return ERR_PTR(-ENOENT
);
615 base
= devm_ioremap_resource(pc
->dev
, &res
);
617 return ERR_CAST(base
);
619 meson_regmap_config
.max_register
= resource_size(&res
) - 4;
620 meson_regmap_config
.name
= devm_kasprintf(pc
->dev
, GFP_KERNEL
,
623 if (!meson_regmap_config
.name
)
624 return ERR_PTR(-ENOMEM
);
626 return devm_regmap_init_mmio(pc
->dev
, base
, &meson_regmap_config
);
629 static int meson_pinctrl_parse_dt(struct meson_pinctrl
*pc
,
630 struct device_node
*node
)
632 struct device_node
*np
;
633 struct meson_domain
*domain
;
636 for_each_child_of_node(node
, np
) {
637 if (!of_find_property(np
, "gpio-controller", NULL
))
642 if (num_domains
!= 1) {
643 dev_err(pc
->dev
, "wrong number of subnodes\n");
647 pc
->domain
= devm_kzalloc(pc
->dev
, sizeof(struct meson_domain
), GFP_KERNEL
);
652 domain
->data
= pc
->data
->domain_data
;
654 for_each_child_of_node(node
, np
) {
655 if (!of_find_property(np
, "gpio-controller", NULL
))
658 domain
->of_node
= np
;
660 domain
->reg_mux
= meson_map_resource(pc
, np
, "mux");
661 if (IS_ERR(domain
->reg_mux
)) {
662 dev_err(pc
->dev
, "mux registers not found\n");
663 return PTR_ERR(domain
->reg_mux
);
666 domain
->reg_pull
= meson_map_resource(pc
, np
, "pull");
667 if (IS_ERR(domain
->reg_pull
)) {
668 dev_err(pc
->dev
, "pull registers not found\n");
669 return PTR_ERR(domain
->reg_pull
);
672 domain
->reg_pullen
= meson_map_resource(pc
, np
, "pull-enable");
673 /* Use pull region if pull-enable one is not present */
674 if (IS_ERR(domain
->reg_pullen
))
675 domain
->reg_pullen
= domain
->reg_pull
;
677 domain
->reg_gpio
= meson_map_resource(pc
, np
, "gpio");
678 if (IS_ERR(domain
->reg_gpio
)) {
679 dev_err(pc
->dev
, "gpio registers not found\n");
680 return PTR_ERR(domain
->reg_gpio
);
689 static int meson_pinctrl_probe(struct platform_device
*pdev
)
691 const struct of_device_id
*match
;
692 struct device
*dev
= &pdev
->dev
;
693 struct meson_pinctrl
*pc
;
696 pc
= devm_kzalloc(dev
, sizeof(struct meson_pinctrl
), GFP_KERNEL
);
701 match
= of_match_node(meson_pinctrl_dt_match
, pdev
->dev
.of_node
);
702 pc
->data
= (struct meson_pinctrl_data
*) match
->data
;
704 ret
= meson_pinctrl_parse_dt(pc
, pdev
->dev
.of_node
);
708 pc
->desc
.name
= "pinctrl-meson";
709 pc
->desc
.owner
= THIS_MODULE
;
710 pc
->desc
.pctlops
= &meson_pctrl_ops
;
711 pc
->desc
.pmxops
= &meson_pmx_ops
;
712 pc
->desc
.confops
= &meson_pinconf_ops
;
713 pc
->desc
.pins
= pc
->data
->pins
;
714 pc
->desc
.npins
= pc
->data
->num_pins
;
716 pc
->pcdev
= pinctrl_register(&pc
->desc
, pc
->dev
, pc
);
717 if (IS_ERR(pc
->pcdev
)) {
718 dev_err(pc
->dev
, "can't register pinctrl device");
719 return PTR_ERR(pc
->pcdev
);
722 ret
= meson_gpiolib_register(pc
);
724 pinctrl_unregister(pc
->pcdev
);
731 static struct platform_driver meson_pinctrl_driver
= {
732 .probe
= meson_pinctrl_probe
,
734 .name
= "meson-pinctrl",
735 .of_match_table
= meson_pinctrl_dt_match
,
738 builtin_platform_driver(meson_pinctrl_driver
);