xref: /openbmc/linux/drivers/pinctrl/meson/pinctrl-meson8-pmx.c (revision 3c910ecbdda4227abd145967774f92b1a3341493)
1*3c910ecbSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ce385aa2SJerome Brunet /*
3ce385aa2SJerome Brunet  * First generation of pinmux driver for Amlogic Meson SoCs
4ce385aa2SJerome Brunet  *
5ce385aa2SJerome Brunet  * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
6ce385aa2SJerome Brunet  * Copyright (C) 2017 Jerome Brunet  <jbrunet@baylibre.com>
7ce385aa2SJerome Brunet  */
8ce385aa2SJerome Brunet 
9ce385aa2SJerome Brunet /* For this first generation of pinctrl driver every pinmux group can be
10ce385aa2SJerome Brunet  * enabled by a specific bit in the first register range. When all groups for
11ce385aa2SJerome Brunet  * a given pin are disabled the pin acts as a GPIO.
12ce385aa2SJerome Brunet  */
13ce385aa2SJerome Brunet #include <linux/device.h>
14ce385aa2SJerome Brunet #include <linux/regmap.h>
15ce385aa2SJerome Brunet #include <linux/pinctrl/pinctrl.h>
16ce385aa2SJerome Brunet #include <linux/pinctrl/pinmux.h>
17ce385aa2SJerome Brunet 
18ce385aa2SJerome Brunet #include "pinctrl-meson.h"
19ce385aa2SJerome Brunet #include "pinctrl-meson8-pmx.h"
20ce385aa2SJerome Brunet 
21ce385aa2SJerome Brunet /**
22ce385aa2SJerome Brunet  * meson8_pmx_disable_other_groups() - disable other groups using a given pin
23ce385aa2SJerome Brunet  *
24ce385aa2SJerome Brunet  * @pc:		meson pin controller device
25ce385aa2SJerome Brunet  * @pin:	number of the pin
26ce385aa2SJerome Brunet  * @sel_group:	index of the selected group, or -1 if none
27ce385aa2SJerome Brunet  *
28ce385aa2SJerome Brunet  * The function disables all pinmux groups using a pin except the
29ce385aa2SJerome Brunet  * selected one. If @sel_group is -1 all groups are disabled, leaving
30ce385aa2SJerome Brunet  * the pin in GPIO mode.
31ce385aa2SJerome Brunet  */
32ce385aa2SJerome Brunet static void meson8_pmx_disable_other_groups(struct meson_pinctrl *pc,
33ce385aa2SJerome Brunet 					    unsigned int pin, int sel_group)
34ce385aa2SJerome Brunet {
35ce385aa2SJerome Brunet 	struct meson_pmx_group *group;
36ce385aa2SJerome Brunet 	struct meson8_pmx_data *pmx_data;
37ce385aa2SJerome Brunet 	int i, j;
38ce385aa2SJerome Brunet 
39ce385aa2SJerome Brunet 	for (i = 0; i < pc->data->num_groups; i++) {
40ce385aa2SJerome Brunet 		group = &pc->data->groups[i];
41ce385aa2SJerome Brunet 		pmx_data = (struct meson8_pmx_data *)group->data;
42ce385aa2SJerome Brunet 		if (pmx_data->is_gpio || i == sel_group)
43ce385aa2SJerome Brunet 			continue;
44ce385aa2SJerome Brunet 
45ce385aa2SJerome Brunet 		for (j = 0; j < group->num_pins; j++) {
46ce385aa2SJerome Brunet 			if (group->pins[j] == pin) {
47ce385aa2SJerome Brunet 				/* We have found a group using the pin */
48ce385aa2SJerome Brunet 				regmap_update_bits(pc->reg_mux,
49ce385aa2SJerome Brunet 						   pmx_data->reg * 4,
50ce385aa2SJerome Brunet 						   BIT(pmx_data->bit), 0);
51ce385aa2SJerome Brunet 			}
52ce385aa2SJerome Brunet 		}
53ce385aa2SJerome Brunet 	}
54ce385aa2SJerome Brunet }
55ce385aa2SJerome Brunet 
56ce385aa2SJerome Brunet static int meson8_pmx_set_mux(struct pinctrl_dev *pcdev, unsigned func_num,
57ce385aa2SJerome Brunet 			      unsigned group_num)
58ce385aa2SJerome Brunet {
59ce385aa2SJerome Brunet 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
60ce385aa2SJerome Brunet 	struct meson_pmx_func *func = &pc->data->funcs[func_num];
61ce385aa2SJerome Brunet 	struct meson_pmx_group *group = &pc->data->groups[group_num];
62ce385aa2SJerome Brunet 	struct meson8_pmx_data *pmx_data =
63ce385aa2SJerome Brunet 		(struct meson8_pmx_data *)group->data;
64ce385aa2SJerome Brunet 	int i, ret = 0;
65ce385aa2SJerome Brunet 
66ce385aa2SJerome Brunet 	dev_dbg(pc->dev, "enable function %s, group %s\n", func->name,
67ce385aa2SJerome Brunet 		group->name);
68ce385aa2SJerome Brunet 
69ce385aa2SJerome Brunet 	/*
70ce385aa2SJerome Brunet 	 * Disable groups using the same pin.
71ce385aa2SJerome Brunet 	 * The selected group is not disabled to avoid glitches.
72ce385aa2SJerome Brunet 	 */
73ce385aa2SJerome Brunet 	for (i = 0; i < group->num_pins; i++)
74ce385aa2SJerome Brunet 		meson8_pmx_disable_other_groups(pc, group->pins[i], group_num);
75ce385aa2SJerome Brunet 
76ce385aa2SJerome Brunet 	/* Function 0 (GPIO) doesn't need any additional setting */
77ce385aa2SJerome Brunet 	if (func_num)
78ce385aa2SJerome Brunet 		ret = regmap_update_bits(pc->reg_mux, pmx_data->reg * 4,
79ce385aa2SJerome Brunet 					 BIT(pmx_data->bit),
80ce385aa2SJerome Brunet 					 BIT(pmx_data->bit));
81ce385aa2SJerome Brunet 
82ce385aa2SJerome Brunet 	return ret;
83ce385aa2SJerome Brunet }
84ce385aa2SJerome Brunet 
85ce385aa2SJerome Brunet static int meson8_pmx_request_gpio(struct pinctrl_dev *pcdev,
86ce385aa2SJerome Brunet 				   struct pinctrl_gpio_range *range,
87ce385aa2SJerome Brunet 				   unsigned offset)
88ce385aa2SJerome Brunet {
89ce385aa2SJerome Brunet 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
90ce385aa2SJerome Brunet 
91ce385aa2SJerome Brunet 	meson8_pmx_disable_other_groups(pc, offset, -1);
92ce385aa2SJerome Brunet 
93ce385aa2SJerome Brunet 	return 0;
94ce385aa2SJerome Brunet }
95ce385aa2SJerome Brunet 
96ce385aa2SJerome Brunet const struct pinmux_ops meson8_pmx_ops = {
97ce385aa2SJerome Brunet 	.set_mux = meson8_pmx_set_mux,
98ce385aa2SJerome Brunet 	.get_functions_count = meson_pmx_get_funcs_count,
99ce385aa2SJerome Brunet 	.get_function_name = meson_pmx_get_func_name,
100ce385aa2SJerome Brunet 	.get_function_groups = meson_pmx_get_groups,
101ce385aa2SJerome Brunet 	.gpio_request_enable = meson8_pmx_request_gpio,
102ce385aa2SJerome Brunet };
103