xref: /linux/drivers/pinctrl/pinctrl-max7360.c (revision 68a052239fc4b351e961f698b824f7654a346091)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2025 Bootlin
4  *
5  * Author: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>
6  */
7 
8 #include <linux/array_size.h>
9 #include <linux/dev_printk.h>
10 #include <linux/device.h>
11 #include <linux/device/devres.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/mfd/max7360.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/stddef.h>
19 
20 #include <linux/pinctrl/pinctrl.h>
21 #include <linux/pinctrl/pinconf-generic.h>
22 #include <linux/pinctrl/pinmux.h>
23 
24 #include "core.h"
25 #include "pinmux.h"
26 
27 struct max7360_pinctrl {
28 	struct pinctrl_dev *pctldev;
29 	struct pinctrl_desc pinctrl_desc;
30 };
31 
32 static const struct pinctrl_pin_desc max7360_pins[] = {
33 	PINCTRL_PIN(0, "PORT0"),
34 	PINCTRL_PIN(1, "PORT1"),
35 	PINCTRL_PIN(2, "PORT2"),
36 	PINCTRL_PIN(3, "PORT3"),
37 	PINCTRL_PIN(4, "PORT4"),
38 	PINCTRL_PIN(5, "PORT5"),
39 	PINCTRL_PIN(6, "PORT6"),
40 	PINCTRL_PIN(7, "PORT7"),
41 };
42 
43 static const unsigned int port0_pins[] = {0};
44 static const unsigned int port1_pins[] = {1};
45 static const unsigned int port2_pins[] = {2};
46 static const unsigned int port3_pins[] = {3};
47 static const unsigned int port4_pins[] = {4};
48 static const unsigned int port5_pins[] = {5};
49 static const unsigned int port6_pins[] = {6};
50 static const unsigned int port7_pins[] = {7};
51 static const unsigned int rotary_pins[] = {6, 7};
52 
53 static const struct pingroup max7360_groups[] = {
54 	PINCTRL_PINGROUP("PORT0", port0_pins, ARRAY_SIZE(port0_pins)),
55 	PINCTRL_PINGROUP("PORT1", port1_pins, ARRAY_SIZE(port1_pins)),
56 	PINCTRL_PINGROUP("PORT2", port2_pins, ARRAY_SIZE(port2_pins)),
57 	PINCTRL_PINGROUP("PORT3", port3_pins, ARRAY_SIZE(port3_pins)),
58 	PINCTRL_PINGROUP("PORT4", port4_pins, ARRAY_SIZE(port4_pins)),
59 	PINCTRL_PINGROUP("PORT5", port5_pins, ARRAY_SIZE(port5_pins)),
60 	PINCTRL_PINGROUP("PORT6", port6_pins, ARRAY_SIZE(port6_pins)),
61 	PINCTRL_PINGROUP("PORT7", port7_pins, ARRAY_SIZE(port7_pins)),
62 	PINCTRL_PINGROUP("ROTARY", rotary_pins, ARRAY_SIZE(rotary_pins)),
63 };
64 
65 static int max7360_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
66 {
67 	return ARRAY_SIZE(max7360_groups);
68 }
69 
70 static const char *max7360_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
71 						  unsigned int group)
72 {
73 	return max7360_groups[group].name;
74 }
75 
76 static int max7360_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
77 					  unsigned int group,
78 					  const unsigned int **pins,
79 					  unsigned int *num_pins)
80 {
81 	*pins = max7360_groups[group].pins;
82 	*num_pins = max7360_groups[group].npins;
83 	return 0;
84 }
85 
86 static const struct pinctrl_ops max7360_pinctrl_ops = {
87 	.get_groups_count = max7360_pinctrl_get_groups_count,
88 	.get_group_name = max7360_pinctrl_get_group_name,
89 	.get_group_pins = max7360_pinctrl_get_group_pins,
90 #ifdef CONFIG_OF
91 	.dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
92 	.dt_free_map = pinconf_generic_dt_free_map,
93 #endif
94 };
95 
96 static const char * const simple_groups[] = {
97 	"PORT0", "PORT1", "PORT2", "PORT3",
98 	"PORT4", "PORT5", "PORT6", "PORT7",
99 };
100 
101 static const char * const rotary_groups[] = { "ROTARY" };
102 
103 #define MAX7360_PINCTRL_FN_GPIO		0
104 #define MAX7360_PINCTRL_FN_PWM		1
105 #define MAX7360_PINCTRL_FN_ROTARY	2
106 static const struct pinfunction max7360_functions[] = {
107 	[MAX7360_PINCTRL_FN_GPIO] = PINCTRL_PINFUNCTION("gpio", simple_groups,
108 							ARRAY_SIZE(simple_groups)),
109 	[MAX7360_PINCTRL_FN_PWM] = PINCTRL_PINFUNCTION("pwm", simple_groups,
110 						       ARRAY_SIZE(simple_groups)),
111 	[MAX7360_PINCTRL_FN_ROTARY] = PINCTRL_PINFUNCTION("rotary", rotary_groups,
112 							  ARRAY_SIZE(rotary_groups)),
113 };
114 
115 static int max7360_get_functions_count(struct pinctrl_dev *pctldev)
116 {
117 	return ARRAY_SIZE(max7360_functions);
118 }
119 
120 static const char *max7360_get_function_name(struct pinctrl_dev *pctldev, unsigned int selector)
121 {
122 	return max7360_functions[selector].name;
123 }
124 
125 static int max7360_get_function_groups(struct pinctrl_dev *pctldev, unsigned int selector,
126 				       const char * const **groups,
127 				       unsigned int * const num_groups)
128 {
129 	*groups = max7360_functions[selector].groups;
130 	*num_groups = max7360_functions[selector].ngroups;
131 
132 	return 0;
133 }
134 
135 static int max7360_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
136 			   unsigned int group)
137 {
138 	struct regmap *regmap = dev_get_regmap(pctldev->dev->parent, NULL);
139 	int val;
140 
141 	/*
142 	 * GPIO and PWM functions are the same: we only need to handle the
143 	 * rotary encoder function, on pins 6 and 7.
144 	 */
145 	if (max7360_groups[group].pins[0] >= 6) {
146 		if (selector == MAX7360_PINCTRL_FN_ROTARY)
147 			val = MAX7360_GPIO_CFG_RTR_EN;
148 		else
149 			val = 0;
150 
151 		return regmap_write_bits(regmap, MAX7360_REG_GPIOCFG, MAX7360_GPIO_CFG_RTR_EN, val);
152 	}
153 
154 	return 0;
155 }
156 
157 static const struct pinmux_ops max7360_pmxops = {
158 	.get_functions_count = max7360_get_functions_count,
159 	.get_function_name = max7360_get_function_name,
160 	.get_function_groups = max7360_get_function_groups,
161 	.set_mux = max7360_set_mux,
162 	.strict = true,
163 };
164 
165 static int max7360_pinctrl_probe(struct platform_device *pdev)
166 {
167 	struct regmap *regmap;
168 	struct pinctrl_desc *pd;
169 	struct max7360_pinctrl *chip;
170 	struct device *dev = &pdev->dev;
171 
172 	regmap = dev_get_regmap(dev->parent, NULL);
173 	if (!regmap)
174 		return dev_err_probe(dev, -ENODEV, "Could not get parent regmap\n");
175 
176 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
177 	if (!chip)
178 		return -ENOMEM;
179 
180 	pd = &chip->pinctrl_desc;
181 
182 	pd->pctlops = &max7360_pinctrl_ops;
183 	pd->pmxops = &max7360_pmxops;
184 	pd->name = dev_name(dev);
185 	pd->pins = max7360_pins;
186 	pd->npins = MAX7360_MAX_GPIO;
187 	pd->owner = THIS_MODULE;
188 
189 	/*
190 	 * This MFD sub-device does not have any associated device tree node:
191 	 * properties are stored in the device node of the parent (MFD) device
192 	 * and this same node is used in phandles of client devices.
193 	 * Reuse this device tree node here, as otherwise the pinctrl subsystem
194 	 * would be confused by this topology.
195 	 */
196 	device_set_of_node_from_dev(dev, dev->parent);
197 
198 	chip->pctldev = devm_pinctrl_register(dev, pd, chip);
199 	if (IS_ERR(chip->pctldev))
200 		return dev_err_probe(dev, PTR_ERR(chip->pctldev), "can't register controller\n");
201 
202 	return 0;
203 }
204 
205 static struct platform_driver max7360_pinctrl_driver = {
206 	.driver = {
207 		.name	= "max7360-pinctrl",
208 	},
209 	.probe		= max7360_pinctrl_probe,
210 };
211 module_platform_driver(max7360_pinctrl_driver);
212 
213 MODULE_DESCRIPTION("MAX7360 pinctrl driver");
214 MODULE_AUTHOR("Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>");
215 MODULE_LICENSE("GPL");
216