xref: /linux/drivers/pinctrl/vt8500/pinctrl-wmt.c (revision 1fd1dc41724319406b0aff221a352a400b0ddfc5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Pinctrl driver for the Wondermedia SoC's
4  *
5  * Copyright (c) 2013 Tony Prisk <linux@prisktech.co.nz>
6  */
7 
8 #include <linux/err.h>
9 #include <linux/gpio/driver.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/irq.h>
13 #include <linux/of.h>
14 #include <linux/of_irq.h>
15 #include <linux/pinctrl/consumer.h>
16 #include <linux/pinctrl/machine.h>
17 #include <linux/pinctrl/pinconf.h>
18 #include <linux/pinctrl/pinconf-generic.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 
24 #include "pinctrl-wmt.h"
25 
26 static inline void wmt_setbits(struct wmt_pinctrl_data *data, u32 reg,
27 				 u32 mask)
28 {
29 	u32 val;
30 
31 	val = readl_relaxed(data->base + reg);
32 	val |= mask;
33 	writel_relaxed(val, data->base + reg);
34 }
35 
36 static inline void wmt_clearbits(struct wmt_pinctrl_data *data, u32 reg,
37 				   u32 mask)
38 {
39 	u32 val;
40 
41 	val = readl_relaxed(data->base + reg);
42 	val &= ~mask;
43 	writel_relaxed(val, data->base + reg);
44 }
45 
46 enum wmt_func_sel {
47 	WMT_FSEL_GPIO_IN = 0,
48 	WMT_FSEL_GPIO_OUT = 1,
49 	WMT_FSEL_ALT = 2,
50 	WMT_FSEL_COUNT = 3,
51 };
52 
53 static const char * const wmt_functions[WMT_FSEL_COUNT] = {
54 	[WMT_FSEL_GPIO_IN] = "gpio_in",
55 	[WMT_FSEL_GPIO_OUT] = "gpio_out",
56 	[WMT_FSEL_ALT] = "alt",
57 };
58 
59 static int wmt_pmx_get_functions_count(struct pinctrl_dev *pctldev)
60 {
61 	return WMT_FSEL_COUNT;
62 }
63 
64 static const char *wmt_pmx_get_function_name(struct pinctrl_dev *pctldev,
65 					     unsigned selector)
66 {
67 	return wmt_functions[selector];
68 }
69 
70 static int wmt_pmx_get_function_groups(struct pinctrl_dev *pctldev,
71 				       unsigned selector,
72 				       const char * const **groups,
73 				       unsigned * const num_groups)
74 {
75 	struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
76 
77 	/* every pin does every function */
78 	*groups = data->groups;
79 	*num_groups = data->ngroups;
80 
81 	return 0;
82 }
83 
84 static int wmt_set_pinmux(struct wmt_pinctrl_data *data, unsigned func,
85 			  unsigned pin)
86 {
87 	u32 bank = WMT_BANK_FROM_PIN(pin);
88 	u32 bit = WMT_BIT_FROM_PIN(pin);
89 	u32 reg_en = data->banks[bank].reg_en;
90 	u32 reg_dir = data->banks[bank].reg_dir;
91 
92 	if (reg_dir == NO_REG) {
93 		dev_err(data->dev, "pin:%d no direction register defined\n",
94 			pin);
95 		return -EINVAL;
96 	}
97 
98 	/*
99 	 * If reg_en == NO_REG, we assume it is a dedicated GPIO and cannot be
100 	 * disabled (as on VT8500) and that no alternate function is available.
101 	 */
102 	switch (func) {
103 	case WMT_FSEL_GPIO_IN:
104 		if (reg_en != NO_REG)
105 			wmt_setbits(data, reg_en, BIT(bit));
106 		wmt_clearbits(data, reg_dir, BIT(bit));
107 		break;
108 	case WMT_FSEL_GPIO_OUT:
109 		if (reg_en != NO_REG)
110 			wmt_setbits(data, reg_en, BIT(bit));
111 		wmt_setbits(data, reg_dir, BIT(bit));
112 		break;
113 	case WMT_FSEL_ALT:
114 		if (reg_en == NO_REG) {
115 			dev_err(data->dev, "pin:%d no alt function available\n",
116 				pin);
117 			return -EINVAL;
118 		}
119 		wmt_clearbits(data, reg_en, BIT(bit));
120 	}
121 
122 	return 0;
123 }
124 
125 static int wmt_pmx_set_mux(struct pinctrl_dev *pctldev,
126 			   unsigned func_selector,
127 			   unsigned group_selector)
128 {
129 	struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
130 	u32 pinnum = data->pins[group_selector].number;
131 
132 	return wmt_set_pinmux(data, func_selector, pinnum);
133 }
134 
135 static void wmt_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
136 				      struct pinctrl_gpio_range *range,
137 				      unsigned offset)
138 {
139 	struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
140 
141 	/* disable by setting GPIO_IN */
142 	wmt_set_pinmux(data, WMT_FSEL_GPIO_IN, offset);
143 }
144 
145 static int wmt_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
146 				      struct pinctrl_gpio_range *range,
147 				      unsigned offset,
148 				      bool input)
149 {
150 	struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
151 
152 	wmt_set_pinmux(data, (input ? WMT_FSEL_GPIO_IN : WMT_FSEL_GPIO_OUT),
153 		       offset);
154 
155 	return 0;
156 }
157 
158 static const struct pinmux_ops wmt_pinmux_ops = {
159 	.get_functions_count = wmt_pmx_get_functions_count,
160 	.get_function_name = wmt_pmx_get_function_name,
161 	.get_function_groups = wmt_pmx_get_function_groups,
162 	.set_mux = wmt_pmx_set_mux,
163 	.gpio_disable_free = wmt_pmx_gpio_disable_free,
164 	.gpio_set_direction = wmt_pmx_gpio_set_direction,
165 };
166 
167 static int wmt_get_groups_count(struct pinctrl_dev *pctldev)
168 {
169 	struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
170 
171 	return data->ngroups;
172 }
173 
174 static const char *wmt_get_group_name(struct pinctrl_dev *pctldev,
175 				      unsigned selector)
176 {
177 	struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
178 
179 	return data->groups[selector];
180 }
181 
182 static int wmt_get_group_pins(struct pinctrl_dev *pctldev,
183 			      unsigned selector,
184 			      const unsigned **pins,
185 			      unsigned *num_pins)
186 {
187 	struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
188 
189 	*pins = &data->pins[selector].number;
190 	*num_pins = 1;
191 
192 	return 0;
193 }
194 
195 static int wmt_pctl_find_group_by_pin(struct wmt_pinctrl_data *data, u32 pin)
196 {
197 	int i;
198 
199 	for (i = 0; i < data->npins; i++) {
200 		if (data->pins[i].number == pin)
201 			return i;
202 	}
203 
204 	return -EINVAL;
205 }
206 
207 static int wmt_pctl_dt_node_to_map_func(struct wmt_pinctrl_data *data,
208 					struct device_node *np,
209 					u32 pin, u32 fnum,
210 					struct pinctrl_map **maps)
211 {
212 	int group;
213 	struct pinctrl_map *map = *maps;
214 
215 	if (fnum >= ARRAY_SIZE(wmt_functions)) {
216 		dev_err(data->dev, "invalid wm,function %d\n", fnum);
217 		return -EINVAL;
218 	}
219 
220 	group = wmt_pctl_find_group_by_pin(data, pin);
221 	if (group < 0) {
222 		dev_err(data->dev, "unable to match pin %d to group\n", pin);
223 		return group;
224 	}
225 
226 	map->type = PIN_MAP_TYPE_MUX_GROUP;
227 	map->data.mux.group = data->groups[group];
228 	map->data.mux.function = wmt_functions[fnum];
229 	(*maps)++;
230 
231 	return 0;
232 }
233 
234 static int wmt_pctl_dt_node_to_map_pull(struct wmt_pinctrl_data *data,
235 					struct device_node *np,
236 					u32 pin, u32 pull,
237 					struct pinctrl_map **maps)
238 {
239 	int group;
240 	unsigned long *configs;
241 	struct pinctrl_map *map = *maps;
242 
243 	if (pull > 2) {
244 		dev_err(data->dev, "invalid wm,pull %d\n", pull);
245 		return -EINVAL;
246 	}
247 
248 	group = wmt_pctl_find_group_by_pin(data, pin);
249 	if (group < 0) {
250 		dev_err(data->dev, "unable to match pin %d to group\n", pin);
251 		return group;
252 	}
253 
254 	configs = kzalloc_obj(*configs);
255 	if (!configs)
256 		return -ENOMEM;
257 
258 	switch (pull) {
259 	case 0:
260 		configs[0] = PIN_CONFIG_BIAS_DISABLE;
261 		break;
262 	case 1:
263 		configs[0] = PIN_CONFIG_BIAS_PULL_DOWN;
264 		break;
265 	case 2:
266 		configs[0] = PIN_CONFIG_BIAS_PULL_UP;
267 		break;
268 	default:
269 		configs[0] = PIN_CONFIG_BIAS_DISABLE;
270 		dev_err(data->dev, "invalid pull state %d - disabling\n", pull);
271 	}
272 
273 	map->type = PIN_MAP_TYPE_CONFIGS_PIN;
274 	map->data.configs.group_or_pin = data->groups[group];
275 	map->data.configs.configs = configs;
276 	map->data.configs.num_configs = 1;
277 	(*maps)++;
278 
279 	return 0;
280 }
281 
282 static void wmt_pctl_dt_free_map(struct pinctrl_dev *pctldev,
283 				 struct pinctrl_map *maps,
284 				 unsigned num_maps)
285 {
286 	int i;
287 
288 	for (i = 0; i < num_maps; i++)
289 		if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
290 			kfree(maps[i].data.configs.configs);
291 
292 	kfree(maps);
293 }
294 
295 static int wmt_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
296 				   struct device_node *np,
297 				   struct pinctrl_map **map,
298 				   unsigned *num_maps)
299 {
300 	struct pinctrl_map *maps, *cur_map;
301 	struct property *pins, *funcs, *pulls;
302 	u32 pin, func, pull;
303 	int num_pins, num_funcs, num_pulls, maps_per_pin;
304 	int i, err;
305 	struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
306 
307 	pins = of_find_property(np, "wm,pins", NULL);
308 	if (!pins) {
309 		dev_err(data->dev, "missing wmt,pins property\n");
310 		return -EINVAL;
311 	}
312 
313 	funcs = of_find_property(np, "wm,function", NULL);
314 	pulls = of_find_property(np, "wm,pull", NULL);
315 
316 	if (!funcs && !pulls) {
317 		dev_err(data->dev, "neither wm,function nor wm,pull specified\n");
318 		return -EINVAL;
319 	}
320 
321 	/*
322 	 * The following lines calculate how many values are defined for each
323 	 * of the properties.
324 	 */
325 	num_pins = pins->length / sizeof(u32);
326 	num_funcs = funcs ? (funcs->length / sizeof(u32)) : 0;
327 	num_pulls = pulls ? (pulls->length / sizeof(u32)) : 0;
328 
329 	if (num_funcs > 1 && num_funcs != num_pins) {
330 		dev_err(data->dev, "wm,function must have 1 or %d entries\n",
331 			num_pins);
332 		return -EINVAL;
333 	}
334 
335 	if (num_pulls > 1 && num_pulls != num_pins) {
336 		dev_err(data->dev, "wm,pull must have 1 or %d entries\n",
337 			num_pins);
338 		return -EINVAL;
339 	}
340 
341 	maps_per_pin = 0;
342 	if (num_funcs)
343 		maps_per_pin++;
344 	if (num_pulls)
345 		maps_per_pin++;
346 
347 	cur_map = maps = kzalloc_objs(*maps, num_pins * maps_per_pin);
348 	if (!maps)
349 		return -ENOMEM;
350 
351 	for (i = 0; i < num_pins; i++) {
352 		err = of_property_read_u32_index(np, "wm,pins", i, &pin);
353 		if (err)
354 			goto fail;
355 
356 		if (pin >= (data->nbanks * 32)) {
357 			dev_err(data->dev, "invalid wm,pins value\n");
358 			err = -EINVAL;
359 			goto fail;
360 		}
361 
362 		if (num_funcs) {
363 			err = of_property_read_u32_index(np, "wm,function",
364 						(num_funcs > 1 ? i : 0), &func);
365 			if (err)
366 				goto fail;
367 
368 			err = wmt_pctl_dt_node_to_map_func(data, np, pin, func,
369 							   &cur_map);
370 			if (err)
371 				goto fail;
372 		}
373 
374 		if (num_pulls) {
375 			err = of_property_read_u32_index(np, "wm,pull",
376 						(num_pulls > 1 ? i : 0), &pull);
377 			if (err)
378 				goto fail;
379 
380 			err = wmt_pctl_dt_node_to_map_pull(data, np, pin, pull,
381 							   &cur_map);
382 			if (err)
383 				goto fail;
384 		}
385 	}
386 	*map = maps;
387 	*num_maps = num_pins * maps_per_pin;
388 	return 0;
389 
390 /*
391  * The fail path removes any maps that have been allocated. The fail path is
392  * only called from code after maps has been kzalloc'd. It is also safe to
393  * pass 'num_pins * maps_per_pin' as the map count even though we probably
394  * failed before all the mappings were read as all maps are allocated at once,
395  * and configs are only allocated for .type = PIN_MAP_TYPE_CONFIGS_PIN - there
396  * is no failpath where a config can be allocated without .type being set.
397  */
398 fail:
399 	wmt_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
400 	return err;
401 }
402 
403 static const struct pinctrl_ops wmt_pctl_ops = {
404 	.get_groups_count = wmt_get_groups_count,
405 	.get_group_name	= wmt_get_group_name,
406 	.get_group_pins	= wmt_get_group_pins,
407 	.dt_node_to_map = wmt_pctl_dt_node_to_map,
408 	.dt_free_map = wmt_pctl_dt_free_map,
409 };
410 
411 static int wmt_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
412 			   unsigned long *config)
413 {
414 	return -ENOTSUPP;
415 }
416 
417 static int wmt_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
418 			   unsigned long *configs, unsigned num_configs)
419 {
420 	struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
421 	enum pin_config_param param;
422 	u32 arg;
423 	u32 bank = WMT_BANK_FROM_PIN(pin);
424 	u32 bit = WMT_BIT_FROM_PIN(pin);
425 	u32 reg_pull_en = data->banks[bank].reg_pull_en;
426 	u32 reg_pull_cfg = data->banks[bank].reg_pull_cfg;
427 	int i;
428 
429 	if ((reg_pull_en == NO_REG) || (reg_pull_cfg == NO_REG)) {
430 		dev_err(data->dev, "bias functions not supported on pin %d\n",
431 			pin);
432 		return -EINVAL;
433 	}
434 
435 	for (i = 0; i < num_configs; i++) {
436 		param = pinconf_to_config_param(configs[i]);
437 		arg = pinconf_to_config_argument(configs[i]);
438 
439 		if ((param == PIN_CONFIG_BIAS_PULL_DOWN) ||
440 		    (param == PIN_CONFIG_BIAS_PULL_UP)) {
441 			if (arg == 0)
442 				param = PIN_CONFIG_BIAS_DISABLE;
443 		}
444 
445 		switch (param) {
446 		case PIN_CONFIG_BIAS_DISABLE:
447 			wmt_clearbits(data, reg_pull_en, BIT(bit));
448 			break;
449 		case PIN_CONFIG_BIAS_PULL_DOWN:
450 			wmt_clearbits(data, reg_pull_cfg, BIT(bit));
451 			wmt_setbits(data, reg_pull_en, BIT(bit));
452 			break;
453 		case PIN_CONFIG_BIAS_PULL_UP:
454 			wmt_setbits(data, reg_pull_cfg, BIT(bit));
455 			wmt_setbits(data, reg_pull_en, BIT(bit));
456 			break;
457 		default:
458 			dev_err(data->dev, "unknown pinconf param\n");
459 			return -EINVAL;
460 		}
461 	} /* for each config */
462 
463 	return 0;
464 }
465 
466 static const struct pinconf_ops wmt_pinconf_ops = {
467 	.pin_config_get = wmt_pinconf_get,
468 	.pin_config_set = wmt_pinconf_set,
469 };
470 
471 static struct pinctrl_desc wmt_desc = {
472 	.owner = THIS_MODULE,
473 	.name = "pinctrl-wmt",
474 	.pctlops = &wmt_pctl_ops,
475 	.pmxops = &wmt_pinmux_ops,
476 	.confops = &wmt_pinconf_ops,
477 };
478 
479 static int wmt_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
480 {
481 	struct wmt_pinctrl_data *data = gpiochip_get_data(chip);
482 	u32 bank = WMT_BANK_FROM_PIN(offset);
483 	u32 bit = WMT_BIT_FROM_PIN(offset);
484 	u32 reg_dir = data->banks[bank].reg_dir;
485 	u32 val;
486 
487 	val = readl_relaxed(data->base + reg_dir);
488 	if (val & BIT(bit))
489 		return GPIO_LINE_DIRECTION_OUT;
490 
491 	return GPIO_LINE_DIRECTION_IN;
492 }
493 
494 static int wmt_gpio_get_value(struct gpio_chip *chip, unsigned offset)
495 {
496 	struct wmt_pinctrl_data *data = gpiochip_get_data(chip);
497 	u32 bank = WMT_BANK_FROM_PIN(offset);
498 	u32 bit = WMT_BIT_FROM_PIN(offset);
499 	u32 reg_data_in = data->banks[bank].reg_data_in;
500 
501 	if (reg_data_in == NO_REG) {
502 		dev_err(data->dev, "no data in register defined\n");
503 		return -EINVAL;
504 	}
505 
506 	return !!(readl_relaxed(data->base + reg_data_in) & BIT(bit));
507 }
508 
509 static int wmt_gpio_set_value(struct gpio_chip *chip, unsigned int offset,
510 			      int val)
511 {
512 	struct wmt_pinctrl_data *data = gpiochip_get_data(chip);
513 	u32 bank = WMT_BANK_FROM_PIN(offset);
514 	u32 bit = WMT_BIT_FROM_PIN(offset);
515 	u32 reg_data_out = data->banks[bank].reg_data_out;
516 
517 	if (reg_data_out == NO_REG) {
518 		dev_err(data->dev, "no data out register defined\n");
519 		return -EINVAL;
520 	}
521 
522 	if (val)
523 		wmt_setbits(data, reg_data_out, BIT(bit));
524 	else
525 		wmt_clearbits(data, reg_data_out, BIT(bit));
526 
527 	return 0;
528 }
529 
530 static int wmt_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
531 				     int value)
532 {
533 	int ret;
534 
535 	ret = wmt_gpio_set_value(chip, offset, value);
536 	if (ret)
537 		return ret;
538 
539 	return pinctrl_gpio_direction_output(chip, offset);
540 }
541 
542 static const struct gpio_chip wmt_gpio_chip = {
543 	.label = "gpio-wmt",
544 	.owner = THIS_MODULE,
545 	.request = gpiochip_generic_request,
546 	.free = gpiochip_generic_free,
547 	.get_direction = wmt_gpio_get_direction,
548 	.direction_input = pinctrl_gpio_direction_input,
549 	.direction_output = wmt_gpio_direction_output,
550 	.get = wmt_gpio_get_value,
551 	.set = wmt_gpio_set_value,
552 	.can_sleep = false,
553 };
554 
555 int wmt_pinctrl_probe(struct platform_device *pdev,
556 		      struct wmt_pinctrl_data *data)
557 {
558 	int err;
559 
560 	data->base = devm_platform_ioremap_resource(pdev, 0);
561 	if (IS_ERR(data->base))
562 		return PTR_ERR(data->base);
563 
564 	wmt_desc.pins = data->pins;
565 	wmt_desc.npins = data->npins;
566 
567 	data->gpio_chip = wmt_gpio_chip;
568 	data->gpio_chip.parent = &pdev->dev;
569 	data->gpio_chip.ngpio = data->nbanks * 32;
570 
571 	platform_set_drvdata(pdev, data);
572 
573 	data->dev = &pdev->dev;
574 
575 	data->pctl_dev = devm_pinctrl_register(&pdev->dev, &wmt_desc, data);
576 	if (IS_ERR(data->pctl_dev)) {
577 		dev_err(&pdev->dev, "Failed to register pinctrl\n");
578 		return PTR_ERR(data->pctl_dev);
579 	}
580 
581 	err = gpiochip_add_data(&data->gpio_chip, data);
582 	if (err) {
583 		dev_err(&pdev->dev, "could not add GPIO chip\n");
584 		return err;
585 	}
586 
587 	err = gpiochip_add_pin_range(&data->gpio_chip, dev_name(data->dev),
588 				     0, 0, data->nbanks * 32);
589 	if (err)
590 		goto fail_range;
591 
592 	dev_info(&pdev->dev, "Pin controller initialized\n");
593 
594 	return 0;
595 
596 fail_range:
597 	gpiochip_remove(&data->gpio_chip);
598 	return err;
599 }
600