1 /*
2  * Driver for the ST Microelectronics SPEAr pinmux
3  *
4  * Copyright (C) 2012 ST Microelectronics
5  * Viresh Kumar <vireshk@kernel.org>
6  *
7  * Inspired from:
8  * - U300 Pinctl drivers
9  * - Tegra Pinctl drivers
10  *
11  * This file is licensed under the terms of the GNU General Public
12  * License version 2. This program is licensed "as is" without any
13  * warranty of any kind, whether express or implied.
14  */
15 
16 #include <linux/err.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_address.h>
21 #include <linux/platform_device.h>
22 #include <linux/seq_file.h>
23 #include <linux/slab.h>
24 
25 #include <linux/pinctrl/machine.h>
26 #include <linux/pinctrl/pinctrl.h>
27 #include <linux/pinctrl/pinmux.h>
28 
29 #include "pinctrl-spear.h"
30 
31 #define DRIVER_NAME "spear-pinmux"
32 
33 static void muxregs_endisable(struct spear_pmx *pmx,
34 		struct spear_muxreg *muxregs, u8 count, bool enable)
35 {
36 	struct spear_muxreg *muxreg;
37 	u32 val, temp, j;
38 
39 	for (j = 0; j < count; j++) {
40 		muxreg = &muxregs[j];
41 
42 		val = pmx_readl(pmx, muxreg->reg);
43 		val &= ~muxreg->mask;
44 
45 		if (enable)
46 			temp = muxreg->val;
47 		else
48 			temp = ~muxreg->val;
49 
50 		val |= muxreg->mask & temp;
51 		pmx_writel(pmx, val, muxreg->reg);
52 	}
53 }
54 
55 static int set_mode(struct spear_pmx *pmx, int mode)
56 {
57 	struct spear_pmx_mode *pmx_mode = NULL;
58 	int i;
59 	u32 val;
60 
61 	if (!pmx->machdata->pmx_modes || !pmx->machdata->npmx_modes)
62 		return -EINVAL;
63 
64 	for (i = 0; i < pmx->machdata->npmx_modes; i++) {
65 		if (pmx->machdata->pmx_modes[i]->mode == (1 << mode)) {
66 			pmx_mode = pmx->machdata->pmx_modes[i];
67 			break;
68 		}
69 	}
70 
71 	if (!pmx_mode)
72 		return -EINVAL;
73 
74 	val = pmx_readl(pmx, pmx_mode->reg);
75 	val &= ~pmx_mode->mask;
76 	val |= pmx_mode->val;
77 	pmx_writel(pmx, val, pmx_mode->reg);
78 
79 	pmx->machdata->mode = pmx_mode->mode;
80 	dev_info(pmx->dev, "Configured Mode: %s with id: %x\n\n",
81 			pmx_mode->name ? pmx_mode->name : "no_name",
82 			pmx_mode->reg);
83 
84 	return 0;
85 }
86 
87 void pmx_init_gpio_pingroup_addr(struct spear_gpio_pingroup *gpio_pingroup,
88 				 unsigned count, u16 reg)
89 {
90 	int i, j;
91 
92 	for (i = 0; i < count; i++)
93 		for (j = 0; j < gpio_pingroup[i].nmuxregs; j++)
94 			gpio_pingroup[i].muxregs[j].reg = reg;
95 }
96 
97 void pmx_init_addr(struct spear_pinctrl_machdata *machdata, u16 reg)
98 {
99 	struct spear_pingroup *pgroup;
100 	struct spear_modemux *modemux;
101 	int i, j, group;
102 
103 	for (group = 0; group < machdata->ngroups; group++) {
104 		pgroup = machdata->groups[group];
105 
106 		for (i = 0; i < pgroup->nmodemuxs; i++) {
107 			modemux = &pgroup->modemuxs[i];
108 
109 			for (j = 0; j < modemux->nmuxregs; j++)
110 				if (modemux->muxregs[j].reg == 0xFFFF)
111 					modemux->muxregs[j].reg = reg;
112 		}
113 	}
114 }
115 
116 static int spear_pinctrl_get_groups_cnt(struct pinctrl_dev *pctldev)
117 {
118 	struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
119 
120 	return pmx->machdata->ngroups;
121 }
122 
123 static const char *spear_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
124 		unsigned group)
125 {
126 	struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
127 
128 	return pmx->machdata->groups[group]->name;
129 }
130 
131 static int spear_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
132 		unsigned group, const unsigned **pins, unsigned *num_pins)
133 {
134 	struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
135 
136 	*pins = pmx->machdata->groups[group]->pins;
137 	*num_pins = pmx->machdata->groups[group]->npins;
138 
139 	return 0;
140 }
141 
142 static void spear_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
143 		struct seq_file *s, unsigned offset)
144 {
145 	seq_printf(s, " " DRIVER_NAME);
146 }
147 
148 static int spear_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
149 					struct device_node *np_config,
150 					struct pinctrl_map **map,
151 					unsigned *num_maps)
152 {
153 	struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
154 	struct device_node *np;
155 	struct property *prop;
156 	const char *function, *group;
157 	int ret, index = 0, count = 0;
158 
159 	/* calculate number of maps required */
160 	for_each_child_of_node(np_config, np) {
161 		ret = of_property_read_string(np, "st,function", &function);
162 		if (ret < 0) {
163 			of_node_put(np);
164 			return ret;
165 		}
166 
167 		ret = of_property_count_strings(np, "st,pins");
168 		if (ret < 0) {
169 			of_node_put(np);
170 			return ret;
171 		}
172 
173 		count += ret;
174 	}
175 
176 	if (!count) {
177 		dev_err(pmx->dev, "No child nodes passed via DT\n");
178 		return -ENODEV;
179 	}
180 
181 	*map = kcalloc(count, sizeof(**map), GFP_KERNEL);
182 	if (!*map)
183 		return -ENOMEM;
184 
185 	for_each_child_of_node(np_config, np) {
186 		of_property_read_string(np, "st,function", &function);
187 		of_property_for_each_string(np, "st,pins", prop, group) {
188 			(*map)[index].type = PIN_MAP_TYPE_MUX_GROUP;
189 			(*map)[index].data.mux.group = group;
190 			(*map)[index].data.mux.function = function;
191 			index++;
192 		}
193 	}
194 
195 	*num_maps = count;
196 
197 	return 0;
198 }
199 
200 static void spear_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
201 				      struct pinctrl_map *map,
202 				      unsigned num_maps)
203 {
204 	kfree(map);
205 }
206 
207 static const struct pinctrl_ops spear_pinctrl_ops = {
208 	.get_groups_count = spear_pinctrl_get_groups_cnt,
209 	.get_group_name = spear_pinctrl_get_group_name,
210 	.get_group_pins = spear_pinctrl_get_group_pins,
211 	.pin_dbg_show = spear_pinctrl_pin_dbg_show,
212 	.dt_node_to_map = spear_pinctrl_dt_node_to_map,
213 	.dt_free_map = spear_pinctrl_dt_free_map,
214 };
215 
216 static int spear_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
217 {
218 	struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
219 
220 	return pmx->machdata->nfunctions;
221 }
222 
223 static const char *spear_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
224 		unsigned function)
225 {
226 	struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
227 
228 	return pmx->machdata->functions[function]->name;
229 }
230 
231 static int spear_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
232 		unsigned function, const char *const **groups,
233 		unsigned * const ngroups)
234 {
235 	struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
236 
237 	*groups = pmx->machdata->functions[function]->groups;
238 	*ngroups = pmx->machdata->functions[function]->ngroups;
239 
240 	return 0;
241 }
242 
243 static int spear_pinctrl_endisable(struct pinctrl_dev *pctldev,
244 		unsigned function, unsigned group, bool enable)
245 {
246 	struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
247 	const struct spear_pingroup *pgroup;
248 	const struct spear_modemux *modemux;
249 	int i;
250 	bool found = false;
251 
252 	pgroup = pmx->machdata->groups[group];
253 
254 	for (i = 0; i < pgroup->nmodemuxs; i++) {
255 		modemux = &pgroup->modemuxs[i];
256 
257 		/* SoC have any modes */
258 		if (pmx->machdata->modes_supported) {
259 			if (!(pmx->machdata->mode & modemux->modes))
260 				continue;
261 		}
262 
263 		found = true;
264 		muxregs_endisable(pmx, modemux->muxregs, modemux->nmuxregs,
265 				enable);
266 	}
267 
268 	if (!found) {
269 		dev_err(pmx->dev, "pinmux group: %s not supported\n",
270 				pgroup->name);
271 		return -ENODEV;
272 	}
273 
274 	return 0;
275 }
276 
277 static int spear_pinctrl_set_mux(struct pinctrl_dev *pctldev, unsigned function,
278 		unsigned group)
279 {
280 	return spear_pinctrl_endisable(pctldev, function, group, true);
281 }
282 
283 /* gpio with pinmux */
284 static struct spear_gpio_pingroup *get_gpio_pingroup(struct spear_pmx *pmx,
285 		unsigned pin)
286 {
287 	struct spear_gpio_pingroup *gpio_pingroup;
288 	int i, j;
289 
290 	if (!pmx->machdata->gpio_pingroups)
291 		return NULL;
292 
293 	for (i = 0; i < pmx->machdata->ngpio_pingroups; i++) {
294 		gpio_pingroup = &pmx->machdata->gpio_pingroups[i];
295 
296 		for (j = 0; j < gpio_pingroup->npins; j++) {
297 			if (gpio_pingroup->pins[j] == pin)
298 				return gpio_pingroup;
299 		}
300 	}
301 
302 	return NULL;
303 }
304 
305 static int gpio_request_endisable(struct pinctrl_dev *pctldev,
306 		struct pinctrl_gpio_range *range, unsigned offset, bool enable)
307 {
308 	struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
309 	struct spear_pinctrl_machdata *machdata = pmx->machdata;
310 	struct spear_gpio_pingroup *gpio_pingroup;
311 
312 	/*
313 	 * Some SoC have configuration options applicable to group of pins,
314 	 * rather than a single pin.
315 	 */
316 	gpio_pingroup = get_gpio_pingroup(pmx, offset);
317 	if (gpio_pingroup)
318 		muxregs_endisable(pmx, gpio_pingroup->muxregs,
319 				gpio_pingroup->nmuxregs, enable);
320 
321 	/*
322 	 * SoC may need some extra configurations, or configurations for single
323 	 * pin
324 	 */
325 	if (machdata->gpio_request_endisable)
326 		machdata->gpio_request_endisable(pmx, offset, enable);
327 
328 	return 0;
329 }
330 
331 static int gpio_request_enable(struct pinctrl_dev *pctldev,
332 		struct pinctrl_gpio_range *range, unsigned offset)
333 {
334 	return gpio_request_endisable(pctldev, range, offset, true);
335 }
336 
337 static void gpio_disable_free(struct pinctrl_dev *pctldev,
338 		struct pinctrl_gpio_range *range, unsigned offset)
339 {
340 	gpio_request_endisable(pctldev, range, offset, false);
341 }
342 
343 static const struct pinmux_ops spear_pinmux_ops = {
344 	.get_functions_count = spear_pinctrl_get_funcs_count,
345 	.get_function_name = spear_pinctrl_get_func_name,
346 	.get_function_groups = spear_pinctrl_get_func_groups,
347 	.set_mux = spear_pinctrl_set_mux,
348 	.gpio_request_enable = gpio_request_enable,
349 	.gpio_disable_free = gpio_disable_free,
350 };
351 
352 static struct pinctrl_desc spear_pinctrl_desc = {
353 	.name = DRIVER_NAME,
354 	.pctlops = &spear_pinctrl_ops,
355 	.pmxops = &spear_pinmux_ops,
356 	.owner = THIS_MODULE,
357 };
358 
359 int spear_pinctrl_probe(struct platform_device *pdev,
360 			struct spear_pinctrl_machdata *machdata)
361 {
362 	struct device_node *np = pdev->dev.of_node;
363 	struct spear_pmx *pmx;
364 
365 	if (!machdata)
366 		return -ENODEV;
367 
368 	pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
369 	if (!pmx)
370 		return -ENOMEM;
371 
372 	pmx->regmap = device_node_to_regmap(np);
373 	if (IS_ERR(pmx->regmap)) {
374 		dev_err(&pdev->dev, "Init regmap failed (%pe).\n",
375 			pmx->regmap);
376 		return PTR_ERR(pmx->regmap);
377 	}
378 
379 	pmx->dev = &pdev->dev;
380 	pmx->machdata = machdata;
381 
382 	/* configure mode, if supported by SoC */
383 	if (machdata->modes_supported) {
384 		int mode = 0;
385 
386 		if (of_property_read_u32(np, "st,pinmux-mode", &mode)) {
387 			dev_err(&pdev->dev, "OF: pinmux mode not passed\n");
388 			return -EINVAL;
389 		}
390 
391 		if (set_mode(pmx, mode)) {
392 			dev_err(&pdev->dev, "OF: Couldn't configure mode: %x\n",
393 					mode);
394 			return -EINVAL;
395 		}
396 	}
397 
398 	platform_set_drvdata(pdev, pmx);
399 
400 	spear_pinctrl_desc.pins = machdata->pins;
401 	spear_pinctrl_desc.npins = machdata->npins;
402 
403 	pmx->pctl = devm_pinctrl_register(&pdev->dev, &spear_pinctrl_desc, pmx);
404 	if (IS_ERR(pmx->pctl)) {
405 		dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
406 		return PTR_ERR(pmx->pctl);
407 	}
408 
409 	return 0;
410 }
411