Merge branch 'r6040-next'
[linux/fpc-iii.git] / drivers / pinctrl / vt8500 / pinctrl-wmt.c
blobcbc6386316783daabc445cb26d9d7d56d9673380
1 /*
2 * Pinctrl driver for the Wondermedia SoC's
4 * Copyright (c) 2013 Tony Prisk <linux@prisktech.co.nz>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
16 #include <linux/err.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/irq.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_irq.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/pinctrl/machine.h>
26 #include <linux/pinctrl/pinconf.h>
27 #include <linux/pinctrl/pinconf-generic.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/pinmux.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
33 #include "pinctrl-wmt.h"
35 static inline void wmt_setbits(struct wmt_pinctrl_data *data, u32 reg,
36 u32 mask)
38 u32 val;
40 val = readl_relaxed(data->base + reg);
41 val |= mask;
42 writel_relaxed(val, data->base + reg);
45 static inline void wmt_clearbits(struct wmt_pinctrl_data *data, u32 reg,
46 u32 mask)
48 u32 val;
50 val = readl_relaxed(data->base + reg);
51 val &= ~mask;
52 writel_relaxed(val, data->base + reg);
55 enum wmt_func_sel {
56 WMT_FSEL_GPIO_IN = 0,
57 WMT_FSEL_GPIO_OUT = 1,
58 WMT_FSEL_ALT = 2,
59 WMT_FSEL_COUNT = 3,
62 static const char * const wmt_functions[WMT_FSEL_COUNT] = {
63 [WMT_FSEL_GPIO_IN] = "gpio_in",
64 [WMT_FSEL_GPIO_OUT] = "gpio_out",
65 [WMT_FSEL_ALT] = "alt",
68 static int wmt_pmx_get_functions_count(struct pinctrl_dev *pctldev)
70 return WMT_FSEL_COUNT;
73 static const char *wmt_pmx_get_function_name(struct pinctrl_dev *pctldev,
74 unsigned selector)
76 return wmt_functions[selector];
79 static int wmt_pmx_get_function_groups(struct pinctrl_dev *pctldev,
80 unsigned selector,
81 const char * const **groups,
82 unsigned * const num_groups)
84 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
86 /* every pin does every function */
87 *groups = data->groups;
88 *num_groups = data->ngroups;
90 return 0;
93 static int wmt_set_pinmux(struct wmt_pinctrl_data *data, unsigned func,
94 unsigned pin)
96 u32 bank = WMT_BANK_FROM_PIN(pin);
97 u32 bit = WMT_BIT_FROM_PIN(pin);
98 u32 reg_en = data->banks[bank].reg_en;
99 u32 reg_dir = data->banks[bank].reg_dir;
101 if (reg_dir == NO_REG) {
102 dev_err(data->dev, "pin:%d no direction register defined\n",
103 pin);
104 return -EINVAL;
108 * If reg_en == NO_REG, we assume it is a dedicated GPIO and cannot be
109 * disabled (as on VT8500) and that no alternate function is available.
111 switch (func) {
112 case WMT_FSEL_GPIO_IN:
113 if (reg_en != NO_REG)
114 wmt_setbits(data, reg_en, BIT(bit));
115 wmt_clearbits(data, reg_dir, BIT(bit));
116 break;
117 case WMT_FSEL_GPIO_OUT:
118 if (reg_en != NO_REG)
119 wmt_setbits(data, reg_en, BIT(bit));
120 wmt_setbits(data, reg_dir, BIT(bit));
121 break;
122 case WMT_FSEL_ALT:
123 if (reg_en == NO_REG) {
124 dev_err(data->dev, "pin:%d no alt function available\n",
125 pin);
126 return -EINVAL;
128 wmt_clearbits(data, reg_en, BIT(bit));
131 return 0;
134 static int wmt_pmx_set_mux(struct pinctrl_dev *pctldev,
135 unsigned func_selector,
136 unsigned group_selector)
138 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
139 u32 pinnum = data->pins[group_selector].number;
141 return wmt_set_pinmux(data, func_selector, pinnum);
144 static void wmt_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
145 struct pinctrl_gpio_range *range,
146 unsigned offset)
148 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
150 /* disable by setting GPIO_IN */
151 wmt_set_pinmux(data, WMT_FSEL_GPIO_IN, offset);
154 static int wmt_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
155 struct pinctrl_gpio_range *range,
156 unsigned offset,
157 bool input)
159 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
161 wmt_set_pinmux(data, (input ? WMT_FSEL_GPIO_IN : WMT_FSEL_GPIO_OUT),
162 offset);
164 return 0;
167 static struct pinmux_ops wmt_pinmux_ops = {
168 .get_functions_count = wmt_pmx_get_functions_count,
169 .get_function_name = wmt_pmx_get_function_name,
170 .get_function_groups = wmt_pmx_get_function_groups,
171 .set_mux = wmt_pmx_set_mux,
172 .gpio_disable_free = wmt_pmx_gpio_disable_free,
173 .gpio_set_direction = wmt_pmx_gpio_set_direction,
176 static int wmt_get_groups_count(struct pinctrl_dev *pctldev)
178 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
180 return data->ngroups;
183 static const char *wmt_get_group_name(struct pinctrl_dev *pctldev,
184 unsigned selector)
186 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
188 return data->groups[selector];
191 static int wmt_get_group_pins(struct pinctrl_dev *pctldev,
192 unsigned selector,
193 const unsigned **pins,
194 unsigned *num_pins)
196 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
198 *pins = &data->pins[selector].number;
199 *num_pins = 1;
201 return 0;
204 static int wmt_pctl_find_group_by_pin(struct wmt_pinctrl_data *data, u32 pin)
206 int i;
208 for (i = 0; i < data->npins; i++) {
209 if (data->pins[i].number == pin)
210 return i;
213 return -EINVAL;
216 static int wmt_pctl_dt_node_to_map_func(struct wmt_pinctrl_data *data,
217 struct device_node *np,
218 u32 pin, u32 fnum,
219 struct pinctrl_map **maps)
221 int group;
222 struct pinctrl_map *map = *maps;
224 if (fnum >= ARRAY_SIZE(wmt_functions)) {
225 dev_err(data->dev, "invalid wm,function %d\n", fnum);
226 return -EINVAL;
229 group = wmt_pctl_find_group_by_pin(data, pin);
230 if (group < 0) {
231 dev_err(data->dev, "unable to match pin %d to group\n", pin);
232 return group;
235 map->type = PIN_MAP_TYPE_MUX_GROUP;
236 map->data.mux.group = data->groups[group];
237 map->data.mux.function = wmt_functions[fnum];
238 (*maps)++;
240 return 0;
243 static int wmt_pctl_dt_node_to_map_pull(struct wmt_pinctrl_data *data,
244 struct device_node *np,
245 u32 pin, u32 pull,
246 struct pinctrl_map **maps)
248 int group;
249 unsigned long *configs;
250 struct pinctrl_map *map = *maps;
252 if (pull > 2) {
253 dev_err(data->dev, "invalid wm,pull %d\n", pull);
254 return -EINVAL;
257 group = wmt_pctl_find_group_by_pin(data, pin);
258 if (group < 0) {
259 dev_err(data->dev, "unable to match pin %d to group\n", pin);
260 return group;
263 configs = kzalloc(sizeof(*configs), GFP_KERNEL);
264 if (!configs)
265 return -ENOMEM;
267 switch (pull) {
268 case 0:
269 configs[0] = PIN_CONFIG_BIAS_DISABLE;
270 break;
271 case 1:
272 configs[0] = PIN_CONFIG_BIAS_PULL_DOWN;
273 break;
274 case 2:
275 configs[0] = PIN_CONFIG_BIAS_PULL_UP;
276 break;
277 default:
278 configs[0] = PIN_CONFIG_BIAS_DISABLE;
279 dev_err(data->dev, "invalid pull state %d - disabling\n", pull);
282 map->type = PIN_MAP_TYPE_CONFIGS_PIN;
283 map->data.configs.group_or_pin = data->groups[group];
284 map->data.configs.configs = configs;
285 map->data.configs.num_configs = 1;
286 (*maps)++;
288 return 0;
291 static void wmt_pctl_dt_free_map(struct pinctrl_dev *pctldev,
292 struct pinctrl_map *maps,
293 unsigned num_maps)
295 int i;
297 for (i = 0; i < num_maps; i++)
298 if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
299 kfree(maps[i].data.configs.configs);
301 kfree(maps);
304 static int wmt_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
305 struct device_node *np,
306 struct pinctrl_map **map,
307 unsigned *num_maps)
309 struct pinctrl_map *maps, *cur_map;
310 struct property *pins, *funcs, *pulls;
311 u32 pin, func, pull;
312 int num_pins, num_funcs, num_pulls, maps_per_pin;
313 int i, err;
314 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
316 pins = of_find_property(np, "wm,pins", NULL);
317 if (!pins) {
318 dev_err(data->dev, "missing wmt,pins property\n");
319 return -EINVAL;
322 funcs = of_find_property(np, "wm,function", NULL);
323 pulls = of_find_property(np, "wm,pull", NULL);
325 if (!funcs && !pulls) {
326 dev_err(data->dev, "neither wm,function nor wm,pull specified\n");
327 return -EINVAL;
331 * The following lines calculate how many values are defined for each
332 * of the properties.
334 num_pins = pins->length / sizeof(u32);
335 num_funcs = funcs ? (funcs->length / sizeof(u32)) : 0;
336 num_pulls = pulls ? (pulls->length / sizeof(u32)) : 0;
338 if (num_funcs > 1 && num_funcs != num_pins) {
339 dev_err(data->dev, "wm,function must have 1 or %d entries\n",
340 num_pins);
341 return -EINVAL;
344 if (num_pulls > 1 && num_pulls != num_pins) {
345 dev_err(data->dev, "wm,pull must have 1 or %d entries\n",
346 num_pins);
347 return -EINVAL;
350 maps_per_pin = 0;
351 if (num_funcs)
352 maps_per_pin++;
353 if (num_pulls)
354 maps_per_pin++;
356 cur_map = maps = kzalloc(num_pins * maps_per_pin * sizeof(*maps),
357 GFP_KERNEL);
358 if (!maps)
359 return -ENOMEM;
361 for (i = 0; i < num_pins; i++) {
362 err = of_property_read_u32_index(np, "wm,pins", i, &pin);
363 if (err)
364 goto fail;
366 if (pin >= (data->nbanks * 32)) {
367 dev_err(data->dev, "invalid wm,pins value\n");
368 err = -EINVAL;
369 goto fail;
372 if (num_funcs) {
373 err = of_property_read_u32_index(np, "wm,function",
374 (num_funcs > 1 ? i : 0), &func);
375 if (err)
376 goto fail;
378 err = wmt_pctl_dt_node_to_map_func(data, np, pin, func,
379 &cur_map);
380 if (err)
381 goto fail;
384 if (num_pulls) {
385 err = of_property_read_u32_index(np, "wm,pull",
386 (num_pulls > 1 ? i : 0), &pull);
387 if (err)
388 goto fail;
390 err = wmt_pctl_dt_node_to_map_pull(data, np, pin, pull,
391 &cur_map);
392 if (err)
393 goto fail;
396 *map = maps;
397 *num_maps = num_pins * maps_per_pin;
398 return 0;
401 * The fail path removes any maps that have been allocated. The fail path is
402 * only called from code after maps has been kzalloc'd. It is also safe to
403 * pass 'num_pins * maps_per_pin' as the map count even though we probably
404 * failed before all the mappings were read as all maps are allocated at once,
405 * and configs are only allocated for .type = PIN_MAP_TYPE_CONFIGS_PIN - there
406 * is no failpath where a config can be allocated without .type being set.
408 fail:
409 wmt_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
410 return err;
413 static struct pinctrl_ops wmt_pctl_ops = {
414 .get_groups_count = wmt_get_groups_count,
415 .get_group_name = wmt_get_group_name,
416 .get_group_pins = wmt_get_group_pins,
417 .dt_node_to_map = wmt_pctl_dt_node_to_map,
418 .dt_free_map = wmt_pctl_dt_free_map,
421 static int wmt_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
422 unsigned long *config)
424 return -ENOTSUPP;
427 static int wmt_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
428 unsigned long *configs, unsigned num_configs)
430 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
431 enum pin_config_param param;
432 u16 arg;
433 u32 bank = WMT_BANK_FROM_PIN(pin);
434 u32 bit = WMT_BIT_FROM_PIN(pin);
435 u32 reg_pull_en = data->banks[bank].reg_pull_en;
436 u32 reg_pull_cfg = data->banks[bank].reg_pull_cfg;
437 int i;
439 if ((reg_pull_en == NO_REG) || (reg_pull_cfg == NO_REG)) {
440 dev_err(data->dev, "bias functions not supported on pin %d\n",
441 pin);
442 return -EINVAL;
445 for (i = 0; i < num_configs; i++) {
446 param = pinconf_to_config_param(configs[i]);
447 arg = pinconf_to_config_argument(configs[i]);
449 if ((param == PIN_CONFIG_BIAS_PULL_DOWN) ||
450 (param == PIN_CONFIG_BIAS_PULL_UP)) {
451 if (arg == 0)
452 param = PIN_CONFIG_BIAS_DISABLE;
455 switch (param) {
456 case PIN_CONFIG_BIAS_DISABLE:
457 wmt_clearbits(data, reg_pull_en, BIT(bit));
458 break;
459 case PIN_CONFIG_BIAS_PULL_DOWN:
460 wmt_clearbits(data, reg_pull_cfg, BIT(bit));
461 wmt_setbits(data, reg_pull_en, BIT(bit));
462 break;
463 case PIN_CONFIG_BIAS_PULL_UP:
464 wmt_setbits(data, reg_pull_cfg, BIT(bit));
465 wmt_setbits(data, reg_pull_en, BIT(bit));
466 break;
467 default:
468 dev_err(data->dev, "unknown pinconf param\n");
469 return -EINVAL;
471 } /* for each config */
473 return 0;
476 static struct pinconf_ops wmt_pinconf_ops = {
477 .pin_config_get = wmt_pinconf_get,
478 .pin_config_set = wmt_pinconf_set,
481 static struct pinctrl_desc wmt_desc = {
482 .owner = THIS_MODULE,
483 .name = "pinctrl-wmt",
484 .pctlops = &wmt_pctl_ops,
485 .pmxops = &wmt_pinmux_ops,
486 .confops = &wmt_pinconf_ops,
489 static int wmt_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
491 struct wmt_pinctrl_data *data = gpiochip_get_data(chip);
492 u32 bank = WMT_BANK_FROM_PIN(offset);
493 u32 bit = WMT_BIT_FROM_PIN(offset);
494 u32 reg_dir = data->banks[bank].reg_dir;
495 u32 val;
497 val = readl_relaxed(data->base + reg_dir);
498 if (val & BIT(bit))
499 return GPIOF_DIR_OUT;
500 else
501 return GPIOF_DIR_IN;
504 static int wmt_gpio_get_value(struct gpio_chip *chip, unsigned offset)
506 struct wmt_pinctrl_data *data = gpiochip_get_data(chip);
507 u32 bank = WMT_BANK_FROM_PIN(offset);
508 u32 bit = WMT_BIT_FROM_PIN(offset);
509 u32 reg_data_in = data->banks[bank].reg_data_in;
511 if (reg_data_in == NO_REG) {
512 dev_err(data->dev, "no data in register defined\n");
513 return -EINVAL;
516 return !!(readl_relaxed(data->base + reg_data_in) & BIT(bit));
519 static void wmt_gpio_set_value(struct gpio_chip *chip, unsigned offset,
520 int val)
522 struct wmt_pinctrl_data *data = gpiochip_get_data(chip);
523 u32 bank = WMT_BANK_FROM_PIN(offset);
524 u32 bit = WMT_BIT_FROM_PIN(offset);
525 u32 reg_data_out = data->banks[bank].reg_data_out;
527 if (reg_data_out == NO_REG) {
528 dev_err(data->dev, "no data out register defined\n");
529 return;
532 if (val)
533 wmt_setbits(data, reg_data_out, BIT(bit));
534 else
535 wmt_clearbits(data, reg_data_out, BIT(bit));
538 static int wmt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
540 return pinctrl_gpio_direction_input(chip->base + offset);
543 static int wmt_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
544 int value)
546 wmt_gpio_set_value(chip, offset, value);
547 return pinctrl_gpio_direction_output(chip->base + offset);
550 static struct gpio_chip wmt_gpio_chip = {
551 .label = "gpio-wmt",
552 .owner = THIS_MODULE,
553 .request = gpiochip_generic_request,
554 .free = gpiochip_generic_free,
555 .get_direction = wmt_gpio_get_direction,
556 .direction_input = wmt_gpio_direction_input,
557 .direction_output = wmt_gpio_direction_output,
558 .get = wmt_gpio_get_value,
559 .set = wmt_gpio_set_value,
560 .can_sleep = false,
563 int wmt_pinctrl_probe(struct platform_device *pdev,
564 struct wmt_pinctrl_data *data)
566 int err;
567 struct resource *res;
569 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
570 data->base = devm_ioremap_resource(&pdev->dev, res);
571 if (IS_ERR(data->base))
572 return PTR_ERR(data->base);
574 wmt_desc.pins = data->pins;
575 wmt_desc.npins = data->npins;
577 data->gpio_chip = wmt_gpio_chip;
578 data->gpio_chip.parent = &pdev->dev;
579 data->gpio_chip.of_node = pdev->dev.of_node;
580 data->gpio_chip.ngpio = data->nbanks * 32;
582 platform_set_drvdata(pdev, data);
584 data->dev = &pdev->dev;
586 data->pctl_dev = devm_pinctrl_register(&pdev->dev, &wmt_desc, data);
587 if (IS_ERR(data->pctl_dev)) {
588 dev_err(&pdev->dev, "Failed to register pinctrl\n");
589 return PTR_ERR(data->pctl_dev);
592 err = gpiochip_add_data(&data->gpio_chip, data);
593 if (err) {
594 dev_err(&pdev->dev, "could not add GPIO chip\n");
595 return err;
598 err = gpiochip_add_pin_range(&data->gpio_chip, dev_name(data->dev),
599 0, 0, data->nbanks * 32);
600 if (err)
601 goto fail_range;
603 dev_info(&pdev->dev, "Pin controller initialized\n");
605 return 0;
607 fail_range:
608 gpiochip_remove(&data->gpio_chip);
609 return err;
612 int wmt_pinctrl_remove(struct platform_device *pdev)
614 struct wmt_pinctrl_data *data = platform_get_drvdata(pdev);
616 gpiochip_remove(&data->gpio_chip);
618 return 0;