Lines Matching +full:gpio +full:- +full:range

1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2011-2012 ST-Ericsson SA
6 * Written on behalf of Linaro for ST-Ericsson
7 * Based on bits of regulator core, gpio core and clk core
24 #include <linux/radix-tree.h>
38 const struct pinmux_ops *ops = pctldev->desc->pmxops; in pinmux_check_ops()
44 !ops->get_functions_count || in pinmux_check_ops()
45 !ops->get_function_name || in pinmux_check_ops()
46 !ops->get_function_groups || in pinmux_check_ops()
47 !ops->set_mux) { in pinmux_check_ops()
48 dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n"); in pinmux_check_ops()
49 return -EINVAL; in pinmux_check_ops()
52 nfuncs = ops->get_functions_count(pctldev); in pinmux_check_ops()
54 const char *fname = ops->get_function_name(pctldev, in pinmux_check_ops()
57 dev_err(pctldev->dev, "pinmux ops has no name for function%u\n", in pinmux_check_ops()
59 return -EINVAL; in pinmux_check_ops()
69 if (!map->data.mux.function) { in pinmux_validate_map()
71 map->name, i); in pinmux_validate_map()
72 return -EINVAL; in pinmux_validate_map()
79 * pinmux_can_be_used_for_gpio() - check if a specific pin
80 * is either muxed to a different function or used as gpio.
86 * menaning that the gpio can be used.
91 const struct pinmux_ops *ops = pctldev->desc->pmxops; in pinmux_can_be_used_for_gpio()
97 guard(mutex)(&desc->mux_lock); in pinmux_can_be_used_for_gpio()
98 if (ops->strict && desc->mux_usecount) in pinmux_can_be_used_for_gpio()
101 return !(ops->strict && !!desc->gpio_owner); in pinmux_can_be_used_for_gpio()
105 * pin_request() - request a single pin to be muxed in, typically for GPIO
109 * name that controls its mux function, or the requested GPIO name
110 * @gpio_range: the range matching the GPIO pin if this is a request for a
111 * single GPIO pin
118 const struct pinmux_ops *ops = pctldev->desc->pmxops; in pin_request()
119 int status = -EINVAL; in pin_request()
123 dev_err(pctldev->dev, in pin_request()
129 dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n", in pin_request()
130 pin, desc->name, owner); in pin_request()
132 scoped_guard(mutex, &desc->mux_lock) { in pin_request()
133 if ((!gpio_range || ops->strict) && in pin_request()
134 desc->mux_usecount && strcmp(desc->mux_owner, owner)) { in pin_request()
135 dev_err(pctldev->dev, in pin_request()
137 desc->name, desc->mux_owner, owner); in pin_request()
141 if ((gpio_range || ops->strict) && desc->gpio_owner) { in pin_request()
142 dev_err(pctldev->dev, in pin_request()
144 desc->name, desc->gpio_owner, owner); in pin_request()
149 desc->gpio_owner = owner; in pin_request()
151 desc->mux_usecount++; in pin_request()
152 if (desc->mux_usecount > 1) in pin_request()
155 desc->mux_owner = owner; in pin_request()
160 if (!try_module_get(pctldev->owner)) { in pin_request()
161 dev_err(pctldev->dev, in pin_request()
164 status = -EINVAL; in pin_request()
172 if (gpio_range && ops->gpio_request_enable) in pin_request()
173 /* This requests and enables a single GPIO pin */ in pin_request()
174 status = ops->gpio_request_enable(pctldev, gpio_range, pin); in pin_request()
175 else if (ops->request) in pin_request()
176 status = ops->request(pctldev, pin); in pin_request()
181 dev_err(pctldev->dev, "request() failed for pin %d\n", pin); in pin_request()
182 module_put(pctldev->owner); in pin_request()
187 scoped_guard(mutex, &desc->mux_lock) { in pin_request()
189 desc->gpio_owner = NULL; in pin_request()
191 desc->mux_usecount--; in pin_request()
192 if (!desc->mux_usecount) in pin_request()
193 desc->mux_owner = NULL; in pin_request()
199 dev_err(pctldev->dev, "pin-%d (%s) status %d\n", in pin_request()
206 * pin_free() - release a single muxed in pin so something else can be muxed
209 * @gpio_range: the range matching the GPIO pin if this is a request for a
210 * single GPIO pin
214 * once the pin is free. This is done for GPIO request functions.
219 const struct pinmux_ops *ops = pctldev->desc->pmxops; in pin_free()
225 dev_err(pctldev->dev, in pin_free()
230 scoped_guard(mutex, &desc->mux_lock) { in pin_free()
235 if (WARN_ON(!desc->mux_usecount)) in pin_free()
237 desc->mux_usecount--; in pin_free()
238 if (desc->mux_usecount) in pin_free()
247 if (gpio_range && ops->gpio_disable_free) in pin_free()
248 ops->gpio_disable_free(pctldev, gpio_range, pin); in pin_free()
249 else if (ops->free) in pin_free()
250 ops->free(pctldev, pin); in pin_free()
252 scoped_guard(mutex, &desc->mux_lock) { in pin_free()
254 owner = desc->gpio_owner; in pin_free()
255 desc->gpio_owner = NULL; in pin_free()
257 owner = desc->mux_owner; in pin_free()
258 desc->mux_owner = NULL; in pin_free()
259 desc->mux_setting = NULL; in pin_free()
263 module_put(pctldev->owner); in pin_free()
269 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
271 * @pin: the pin to mux in for GPIO
272 * @range: the applicable GPIO range
273 * @gpio: number of requested GPIO
276 struct pinctrl_gpio_range *range, in pinmux_request_gpio() argument
277 unsigned pin, unsigned gpio) in pinmux_request_gpio() argument
283 owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio); in pinmux_request_gpio()
285 return -ENOMEM; in pinmux_request_gpio()
287 ret = pin_request(pctldev, pin, owner, range); in pinmux_request_gpio()
295 * pinmux_free_gpio() - release a pin from GPIO muxing
297 * @pin: the affected currently GPIO-muxed in pin
298 * @range: applicable GPIO range
301 struct pinctrl_gpio_range *range) in pinmux_free_gpio() argument
305 owner = pin_free(pctldev, pin, range); in pinmux_free_gpio()
310 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
312 * @range: applicable GPIO range
313 * @pin: the affected GPIO pin in this controller
317 struct pinctrl_gpio_range *range, in pinmux_gpio_direction() argument
323 ops = pctldev->desc->pmxops; in pinmux_gpio_direction()
325 if (ops->gpio_set_direction) in pinmux_gpio_direction()
326 ret = ops->gpio_set_direction(pctldev, range, pin, input); in pinmux_gpio_direction()
336 const struct pinmux_ops *ops = pctldev->desc->pmxops; in pinmux_func_name_to_selector()
337 unsigned nfuncs = ops->get_functions_count(pctldev); in pinmux_func_name_to_selector()
342 const char *fname = ops->get_function_name(pctldev, selector); in pinmux_func_name_to_selector()
350 return -EINVAL; in pinmux_func_name_to_selector()
356 struct pinctrl_dev *pctldev = setting->pctldev; in pinmux_map_to_setting()
357 const struct pinmux_ops *pmxops = pctldev->desc->pmxops; in pinmux_map_to_setting()
364 dev_err(pctldev->dev, "does not support mux function\n"); in pinmux_map_to_setting()
365 return -EINVAL; in pinmux_map_to_setting()
368 ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function); in pinmux_map_to_setting()
370 dev_err(pctldev->dev, "invalid function %s in map table\n", in pinmux_map_to_setting()
371 map->data.mux.function); in pinmux_map_to_setting()
374 setting->data.mux.func = ret; in pinmux_map_to_setting()
376 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func, in pinmux_map_to_setting()
379 dev_err(pctldev->dev, "can't query groups for function %s\n", in pinmux_map_to_setting()
380 map->data.mux.function); in pinmux_map_to_setting()
384 dev_err(pctldev->dev, in pinmux_map_to_setting()
386 map->data.mux.function); in pinmux_map_to_setting()
387 return -EINVAL; in pinmux_map_to_setting()
389 if (map->data.mux.group) { in pinmux_map_to_setting()
390 group = map->data.mux.group; in pinmux_map_to_setting()
393 dev_err(pctldev->dev, in pinmux_map_to_setting()
395 group, map->data.mux.function); in pinmux_map_to_setting()
404 dev_err(pctldev->dev, "invalid group %s in map table\n", in pinmux_map_to_setting()
405 map->data.mux.group); in pinmux_map_to_setting()
408 setting->data.mux.group = ret; in pinmux_map_to_setting()
420 struct pinctrl_dev *pctldev = setting->pctldev; in pinmux_enable_setting()
421 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; in pinmux_enable_setting()
422 const struct pinmux_ops *ops = pctldev->desc->pmxops; in pinmux_enable_setting()
429 if (pctlops->get_group_pins) in pinmux_enable_setting()
430 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, in pinmux_enable_setting()
437 gname = pctlops->get_group_name(pctldev, in pinmux_enable_setting()
438 setting->data.mux.group); in pinmux_enable_setting()
439 dev_warn(pctldev->dev, in pinmux_enable_setting()
447 ret = pin_request(pctldev, pins[i], setting->dev_name, NULL); in pinmux_enable_setting()
453 pname = desc ? desc->name : "non-existing"; in pinmux_enable_setting()
454 gname = pctlops->get_group_name(pctldev, in pinmux_enable_setting()
455 setting->data.mux.group); in pinmux_enable_setting()
456 dev_err(pctldev->dev, in pinmux_enable_setting()
469 dev_warn(pctldev->dev, in pinmux_enable_setting()
474 scoped_guard(mutex, &desc->mux_lock) in pinmux_enable_setting()
475 desc->mux_setting = &(setting->data.mux); in pinmux_enable_setting()
478 ret = ops->set_mux(pctldev, setting->data.mux.func, in pinmux_enable_setting()
479 setting->data.mux.group); in pinmux_enable_setting()
490 scoped_guard(mutex, &desc->mux_lock) in pinmux_enable_setting()
491 desc->mux_setting = NULL; in pinmux_enable_setting()
496 while (--i >= 0) in pinmux_enable_setting()
504 struct pinctrl_dev *pctldev = setting->pctldev; in pinmux_disable_setting()
505 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; in pinmux_disable_setting()
513 if (pctlops->get_group_pins) in pinmux_disable_setting()
514 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, in pinmux_disable_setting()
520 gname = pctlops->get_group_name(pctldev, in pinmux_disable_setting()
521 setting->data.mux.group); in pinmux_disable_setting()
522 dev_warn(pctldev->dev, in pinmux_disable_setting()
532 dev_warn(pctldev->dev, in pinmux_disable_setting()
537 scoped_guard(mutex, &desc->mux_lock) in pinmux_disable_setting()
538 is_equal = (desc->mux_setting == &(setting->data.mux)); in pinmux_disable_setting()
545 gname = pctlops->get_group_name(pctldev, in pinmux_disable_setting()
546 setting->data.mux.group); in pinmux_disable_setting()
547 dev_warn(pctldev->dev, in pinmux_disable_setting()
549 "deactivating group %s - it is already " in pinmux_disable_setting()
551 pins[i], desc->name, gname); in pinmux_disable_setting()
561 struct pinctrl_dev *pctldev = s->private; in pinmux_functions_show()
562 const struct pinmux_ops *pmxops = pctldev->desc->pmxops; in pinmux_functions_show()
569 mutex_lock(&pctldev->mutex); in pinmux_functions_show()
570 nfuncs = pmxops->get_functions_count(pctldev); in pinmux_functions_show()
572 const char *func = pmxops->get_function_name(pctldev, in pinmux_functions_show()
579 ret = pmxops->get_function_groups(pctldev, func_selector, in pinmux_functions_show()
596 mutex_unlock(&pctldev->mutex); in pinmux_functions_show()
603 struct pinctrl_dev *pctldev = s->private; in pinmux_pins_show()
604 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; in pinmux_pins_show()
605 const struct pinmux_ops *pmxops = pctldev->desc->pmxops; in pinmux_pins_show()
612 if (pmxops->strict) in pinmux_pins_show()
619 mutex_lock(&pctldev->mutex); in pinmux_pins_show()
622 for (i = 0; i < pctldev->desc->npins; i++) { in pinmux_pins_show()
626 pin = pctldev->desc->pins[i].number; in pinmux_pins_show()
632 scoped_guard(mutex, &desc->mux_lock) { in pinmux_pins_show()
633 if (desc->mux_owner && in pinmux_pins_show()
634 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev))) in pinmux_pins_show()
637 if (pmxops->strict) { in pinmux_pins_show()
638 if (desc->mux_owner) in pinmux_pins_show()
640 pin, desc->name, desc->mux_owner, in pinmux_pins_show()
642 else if (desc->gpio_owner) in pinmux_pins_show()
643 seq_printf(s, "pin %d (%s): GPIO %s", in pinmux_pins_show()
644 pin, desc->name, desc->gpio_owner); in pinmux_pins_show()
647 pin, desc->name); in pinmux_pins_show()
649 /* For non-strict controllers */ in pinmux_pins_show()
650 seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name, in pinmux_pins_show()
651 desc->mux_owner ? desc->mux_owner in pinmux_pins_show()
653 desc->gpio_owner ? desc->gpio_owner in pinmux_pins_show()
654 : "(GPIO UNCLAIMED)", in pinmux_pins_show()
659 if (desc->mux_setting) in pinmux_pins_show()
661 pmxops->get_function_name(pctldev, in pinmux_pins_show()
662 desc->mux_setting->func), in pinmux_pins_show()
663 pctlops->get_group_name(pctldev, in pinmux_pins_show()
664 desc->mux_setting->group)); in pinmux_pins_show()
670 mutex_unlock(&pctldev->mutex); in pinmux_pins_show()
678 map->data.mux.group ? map->data.mux.group : "(default)", in pinmux_show_map()
679 map->data.mux.function); in pinmux_show_map()
685 struct pinctrl_dev *pctldev = setting->pctldev; in pinmux_show_setting()
686 const struct pinmux_ops *pmxops = pctldev->desc->pmxops; in pinmux_show_setting()
687 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; in pinmux_show_setting()
690 pctlops->get_group_name(pctldev, setting->data.mux.group), in pinmux_show_setting()
691 setting->data.mux.group, in pinmux_show_setting()
692 pmxops->get_function_name(pctldev, setting->data.mux.func), in pinmux_show_setting()
693 setting->data.mux.func); in pinmux_show_setting()
702 struct seq_file *sfile = file->private_data; in pinmux_select()
703 struct pinctrl_dev *pctldev = sfile->private; in pinmux_select()
704 const struct pinmux_ops *pmxops = pctldev->desc->pmxops; in pinmux_select()
717 ret = -EINVAL; in pinmux_select()
724 ret = -EINVAL; in pinmux_select()
733 ret = -EINVAL; in pinmux_select()
739 dev_err(pctldev->dev, "invalid function %s in map table\n", fname); in pinmux_select()
744 ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups); in pinmux_select()
746 dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname); in pinmux_select()
752 dev_err(pctldev->dev, "invalid group %s", gname); in pinmux_select()
761 ret = pmxops->set_mux(pctldev, fsel, gsel); in pinmux_select()
763 dev_err(pctldev->dev, "set_mux() failed: %d", ret); in pinmux_select()
776 return single_open(file, NULL, inode->i_private); in pinmux_select_open()
790 debugfs_create_file("pinmux-functions", 0444, in pinmux_init_device_debugfs()
792 debugfs_create_file("pinmux-pins", 0444, in pinmux_init_device_debugfs()
794 debugfs_create_file("pinmux-select", 0200, in pinmux_init_device_debugfs()
803 * pinmux_generic_get_function_count() - returns number of functions
808 return pctldev->num_functions; in pinmux_generic_get_function_count()
813 * pinmux_generic_get_function_name() - returns the function name
823 function = radix_tree_lookup(&pctldev->pin_function_tree, in pinmux_generic_get_function_name()
828 return function->name; in pinmux_generic_get_function_name()
833 * pinmux_generic_get_function_groups() - gets the function groups
846 function = radix_tree_lookup(&pctldev->pin_function_tree, in pinmux_generic_get_function_groups()
849 dev_err(pctldev->dev, "%s could not find function%i\n", in pinmux_generic_get_function_groups()
851 return -EINVAL; in pinmux_generic_get_function_groups()
853 *groups = function->group_names; in pinmux_generic_get_function_groups()
854 *num_groups = function->num_group_names; in pinmux_generic_get_function_groups()
861 * pinmux_generic_get_function() - returns a function based on the number
870 function = radix_tree_lookup(&pctldev->pin_function_tree, in pinmux_generic_get_function()
880 * pinmux_generic_add_function() - adds a function group
897 return -EINVAL; in pinmux_generic_add_function()
903 selector = pctldev->num_functions; in pinmux_generic_add_function()
905 function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL); in pinmux_generic_add_function()
907 return -ENOMEM; in pinmux_generic_add_function()
909 function->name = name; in pinmux_generic_add_function()
910 function->group_names = groups; in pinmux_generic_add_function()
911 function->num_group_names = num_groups; in pinmux_generic_add_function()
912 function->data = data; in pinmux_generic_add_function()
914 error = radix_tree_insert(&pctldev->pin_function_tree, selector, function); in pinmux_generic_add_function()
918 pctldev->num_functions++; in pinmux_generic_add_function()
925 * pinmux_generic_remove_function() - removes a numbered function
936 function = radix_tree_lookup(&pctldev->pin_function_tree, in pinmux_generic_remove_function()
939 return -ENOENT; in pinmux_generic_remove_function()
941 radix_tree_delete(&pctldev->pin_function_tree, selector); in pinmux_generic_remove_function()
942 devm_kfree(pctldev->dev, function); in pinmux_generic_remove_function()
944 pctldev->num_functions--; in pinmux_generic_remove_function()
951 * pinmux_generic_free_functions() - removes all functions
963 radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0) in pinmux_generic_free_functions()
964 radix_tree_delete(&pctldev->pin_function_tree, iter.index); in pinmux_generic_free_functions()
966 pctldev->num_functions = 0; in pinmux_generic_free_functions()