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
32 #include "../gpio/gpiolib.h"
62 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
77 return pctldev->desc->name; in pinctrl_dev_get_name()
83 return dev_name(pctldev->dev); in pinctrl_dev_get_devname()
89 return pctldev->driver_data; in pinctrl_dev_get_drvdata()
94 * get_pinctrl_dev_from_devname() - look up pin controller device
110 if (!strcmp(dev_name(pctldev->dev), devname)) { in get_pinctrl_dev_from_devname()
129 if (device_match_of_node(pctldev->dev, np)) { in get_pinctrl_dev_from_of_node()
140 * pin_get_from_name() - look up a pin number from a name
149 for (i = 0; i < pctldev->desc->npins; i++) { in pin_get_from_name()
152 pin = pctldev->desc->pins[i].number; in pin_get_from_name()
155 if (desc && !strcmp(name, desc->name)) in pin_get_from_name()
159 return -EINVAL; in pin_get_from_name()
163 * pin_get_name() - look up a pin name from a pin id
173 dev_err(pctldev->dev, "failed to get pin(%d) name\n", in pin_get_name()
178 return desc->name; in pin_get_name()
182 /* Deletes a range of pin descriptors */
192 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, in pinctrl_free_pindescs()
195 radix_tree_delete(&pctldev->pin_desc_tree, in pinctrl_free_pindescs()
197 if (pindesc->dynamic_name) in pinctrl_free_pindescs()
198 kfree(pindesc->name); in pinctrl_free_pindescs()
210 pindesc = pin_desc_get(pctldev, pin->number); in pinctrl_register_one_pin()
212 dev_err(pctldev->dev, "pin %d already registered\n", in pinctrl_register_one_pin()
213 pin->number); in pinctrl_register_one_pin()
214 return -EINVAL; in pinctrl_register_one_pin()
219 return -ENOMEM; in pinctrl_register_one_pin()
222 pindesc->pctldev = pctldev; in pinctrl_register_one_pin()
224 mutex_init(&pindesc->mux_lock); in pinctrl_register_one_pin()
228 if (pin->name) { in pinctrl_register_one_pin()
229 pindesc->name = pin->name; in pinctrl_register_one_pin()
231 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number); in pinctrl_register_one_pin()
232 if (!pindesc->name) { in pinctrl_register_one_pin()
233 error = -ENOMEM; in pinctrl_register_one_pin()
236 pindesc->dynamic_name = true; in pinctrl_register_one_pin()
239 pindesc->drv_data = pin->drv_data; in pinctrl_register_one_pin()
241 error = radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc); in pinctrl_register_one_pin()
246 pin->number, pindesc->name, pctldev->desc->name); in pinctrl_register_one_pin()
271 * gpio_to_pin() - GPIO range GPIO number to pin number translation
272 * @range: GPIO range used for the translation
273 * @gpio: gpio pin to translate to a pin number
275 * Finds the pin number for a given GPIO using the specified GPIO range
276 * as a base for translation. The distinction between linear GPIO ranges
277 * and pin list based GPIO ranges is managed correctly by this function.
279 * This function assumes the gpio is part of the specified GPIO range, use
283 static inline int gpio_to_pin(struct pinctrl_gpio_range *range, in gpio_to_pin() argument
284 unsigned int gpio) in gpio_to_pin() argument
286 unsigned int offset = gpio - range->base; in gpio_to_pin()
287 if (range->pins) in gpio_to_pin()
288 return range->pins[offset]; in gpio_to_pin()
290 return range->pin_base + offset; in gpio_to_pin()
294 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
296 * @gpio: gpio pin to check taken from the global GPIO pin space
298 * Tries to match a GPIO pin number to the ranges handled by a certain pin
299 * controller, return the range or NULL
302 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio) in pinctrl_match_gpio_range() argument
304 struct pinctrl_gpio_range *range; in pinctrl_match_gpio_range() local
306 mutex_lock(&pctldev->mutex); in pinctrl_match_gpio_range()
308 list_for_each_entry(range, &pctldev->gpio_ranges, node) { in pinctrl_match_gpio_range()
309 /* Check if we're in the valid range */ in pinctrl_match_gpio_range()
310 if (gpio >= range->base && in pinctrl_match_gpio_range()
311 gpio < range->base + range->npins) { in pinctrl_match_gpio_range()
312 mutex_unlock(&pctldev->mutex); in pinctrl_match_gpio_range()
313 return range; in pinctrl_match_gpio_range()
316 mutex_unlock(&pctldev->mutex); in pinctrl_match_gpio_range()
321 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
322 * the same GPIO chip are in range
323 * @gpio: gpio pin to check taken from the global GPIO pin space
327 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
328 * of the same GPIO chip don't have back-end pinctrl interface.
330 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
334 static bool pinctrl_ready_for_gpio_range(unsigned gpio) in pinctrl_ready_for_gpio_range() argument
337 struct pinctrl_gpio_range *range = NULL; in pinctrl_ready_for_gpio_range() local
339 * FIXME: "gpio" here is a number in the global GPIO numberspace. in pinctrl_ready_for_gpio_range()
340 * get rid of this from the ranges eventually and get the GPIO in pinctrl_ready_for_gpio_range()
343 struct gpio_chip *chip = gpiod_to_chip(gpio_to_desc(gpio)); in pinctrl_ready_for_gpio_range()
345 if (WARN(!chip, "no gpio_chip for gpio%i?", gpio)) in pinctrl_ready_for_gpio_range()
353 mutex_lock(&pctldev->mutex); in pinctrl_ready_for_gpio_range()
354 list_for_each_entry(range, &pctldev->gpio_ranges, node) { in pinctrl_ready_for_gpio_range()
355 /* Check if any gpio range overlapped with gpio chip */ in pinctrl_ready_for_gpio_range()
356 if (range->base + range->npins - 1 < chip->base || in pinctrl_ready_for_gpio_range()
357 range->base > chip->base + chip->ngpio - 1) in pinctrl_ready_for_gpio_range()
359 mutex_unlock(&pctldev->mutex); in pinctrl_ready_for_gpio_range()
363 mutex_unlock(&pctldev->mutex); in pinctrl_ready_for_gpio_range()
371 static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; } in pinctrl_ready_for_gpio_range() argument
375 * pinctrl_get_device_gpio_range() - find device for GPIO range
376 * @gpio: the pin to locate the pin controller for
378 * @outrange: the GPIO range if found
380 * Find the pin controller handling a certain GPIO pin from the pinspace of
381 * the GPIO subsystem, return the device and the matching GPIO range. Returns
382 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
385 static int pinctrl_get_device_gpio_range(unsigned gpio, in pinctrl_get_device_gpio_range() argument
395 struct pinctrl_gpio_range *range; in pinctrl_get_device_gpio_range() local
397 range = pinctrl_match_gpio_range(pctldev, gpio); in pinctrl_get_device_gpio_range()
398 if (range) { in pinctrl_get_device_gpio_range()
400 *outrange = range; in pinctrl_get_device_gpio_range()
408 return -EPROBE_DEFER; in pinctrl_get_device_gpio_range()
412 * pinctrl_add_gpio_range() - register a GPIO range for a controller
413 * @pctldev: pin controller device to add the range to
414 * @range: the GPIO range to add
416 * This adds a range of GPIOs to be handled by a certain pin controller. Call
420 struct pinctrl_gpio_range *range) in pinctrl_add_gpio_range() argument
422 mutex_lock(&pctldev->mutex); in pinctrl_add_gpio_range()
423 list_add_tail(&range->node, &pctldev->gpio_ranges); in pinctrl_add_gpio_range()
424 mutex_unlock(&pctldev->mutex); in pinctrl_add_gpio_range()
440 struct pinctrl_gpio_range *range) in pinctrl_find_and_add_gpio_range() argument
449 * range need to defer probing. in pinctrl_find_and_add_gpio_range()
452 return ERR_PTR(-EPROBE_DEFER); in pinctrl_find_and_add_gpio_range()
454 pinctrl_add_gpio_range(pctldev, range); in pinctrl_find_and_add_gpio_range()
463 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; in pinctrl_get_group_pins()
466 if (!pctlops->get_group_pins) in pinctrl_get_group_pins()
467 return -EINVAL; in pinctrl_get_group_pins()
473 return pctlops->get_group_pins(pctldev, gs, pins, num_pins); in pinctrl_get_group_pins()
481 struct pinctrl_gpio_range *range; in pinctrl_find_gpio_range_from_pin_nolock() local
484 list_for_each_entry(range, &pctldev->gpio_ranges, node) { in pinctrl_find_gpio_range_from_pin_nolock()
485 /* Check if we're in the valid range */ in pinctrl_find_gpio_range_from_pin_nolock()
486 if (range->pins) { in pinctrl_find_gpio_range_from_pin_nolock()
488 for (a = 0; a < range->npins; a++) { in pinctrl_find_gpio_range_from_pin_nolock()
489 if (range->pins[a] == pin) in pinctrl_find_gpio_range_from_pin_nolock()
490 return range; in pinctrl_find_gpio_range_from_pin_nolock()
492 } else if (pin >= range->pin_base && in pinctrl_find_gpio_range_from_pin_nolock()
493 pin < range->pin_base + range->npins) in pinctrl_find_gpio_range_from_pin_nolock()
494 return range; in pinctrl_find_gpio_range_from_pin_nolock()
502 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
504 * @pin: a controller-local number to find the range for
510 struct pinctrl_gpio_range *range; in pinctrl_find_gpio_range_from_pin() local
512 mutex_lock(&pctldev->mutex); in pinctrl_find_gpio_range_from_pin()
513 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin); in pinctrl_find_gpio_range_from_pin()
514 mutex_unlock(&pctldev->mutex); in pinctrl_find_gpio_range_from_pin()
516 return range; in pinctrl_find_gpio_range_from_pin()
521 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
522 * @pctldev: pin controller device to remove the range from
523 * @range: the GPIO range to remove
526 struct pinctrl_gpio_range *range) in pinctrl_remove_gpio_range() argument
528 mutex_lock(&pctldev->mutex); in pinctrl_remove_gpio_range()
529 list_del(&range->node); in pinctrl_remove_gpio_range()
530 mutex_unlock(&pctldev->mutex); in pinctrl_remove_gpio_range()
537 * pinctrl_generic_get_group_count() - returns the number of pin groups
542 return pctldev->num_groups; in pinctrl_generic_get_group_count()
547 * pinctrl_generic_get_group_name() - returns the name of a pin group
556 group = radix_tree_lookup(&pctldev->pin_group_tree, in pinctrl_generic_get_group_name()
561 return group->name; in pinctrl_generic_get_group_name()
566 * pinctrl_generic_get_group_pins() - gets the pin group pins
579 group = radix_tree_lookup(&pctldev->pin_group_tree, in pinctrl_generic_get_group_pins()
582 dev_err(pctldev->dev, "%s could not find pingroup%i\n", in pinctrl_generic_get_group_pins()
584 return -EINVAL; in pinctrl_generic_get_group_pins()
587 *pins = group->pins; in pinctrl_generic_get_group_pins()
588 *num_pins = group->num_pins; in pinctrl_generic_get_group_pins()
595 * pinctrl_generic_get_group() - returns a pin group based on the number
604 group = radix_tree_lookup(&pctldev->pin_group_tree, in pinctrl_generic_get_group()
616 const struct pinctrl_ops *ops = pctldev->desc->pctlops; in pinctrl_generic_group_name_to_selector()
617 int ngroups = ops->get_groups_count(pctldev); in pinctrl_generic_group_name_to_selector()
622 const char *gname = ops->get_group_name(pctldev, selector); in pinctrl_generic_group_name_to_selector()
630 return -EINVAL; in pinctrl_generic_group_name_to_selector()
634 * pinctrl_generic_add_group() - adds a new pin group
650 return -EINVAL; in pinctrl_generic_add_group()
656 selector = pctldev->num_groups; in pinctrl_generic_add_group()
658 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL); in pinctrl_generic_add_group()
660 return -ENOMEM; in pinctrl_generic_add_group()
662 group->name = name; in pinctrl_generic_add_group()
663 group->pins = pins; in pinctrl_generic_add_group()
664 group->num_pins = num_pins; in pinctrl_generic_add_group()
665 group->data = data; in pinctrl_generic_add_group()
667 error = radix_tree_insert(&pctldev->pin_group_tree, selector, group); in pinctrl_generic_add_group()
671 pctldev->num_groups++; in pinctrl_generic_add_group()
678 * pinctrl_generic_remove_group() - removes a numbered pin group
689 group = radix_tree_lookup(&pctldev->pin_group_tree, in pinctrl_generic_remove_group()
692 return -ENOENT; in pinctrl_generic_remove_group()
694 radix_tree_delete(&pctldev->pin_group_tree, selector); in pinctrl_generic_remove_group()
695 devm_kfree(pctldev->dev, group); in pinctrl_generic_remove_group()
697 pctldev->num_groups--; in pinctrl_generic_remove_group()
704 * pinctrl_generic_free_groups() - removes all pin groups
715 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0) in pinctrl_generic_free_groups()
716 radix_tree_delete(&pctldev->pin_group_tree, iter.index); in pinctrl_generic_free_groups()
718 pctldev->num_groups = 0; in pinctrl_generic_free_groups()
728 * pinctrl_get_group_selector() - returns the group selector for a group
735 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; in pinctrl_get_group_selector()
736 unsigned ngroups = pctlops->get_groups_count(pctldev); in pinctrl_get_group_selector()
740 const char *gname = pctlops->get_group_name(pctldev, in pinctrl_get_group_selector()
743 dev_dbg(pctldev->dev, in pinctrl_get_group_selector()
753 dev_err(pctldev->dev, "does not have pin group %s\n", in pinctrl_get_group_selector()
756 return -EINVAL; in pinctrl_get_group_selector()
759 bool pinctrl_gpio_can_use_line(unsigned gpio) in pinctrl_gpio_can_use_line() argument
762 struct pinctrl_gpio_range *range; in pinctrl_gpio_can_use_line() local
767 * Try to obtain GPIO range, if it fails in pinctrl_gpio_can_use_line()
768 * we're probably dealing with GPIO driver in pinctrl_gpio_can_use_line()
769 * without a backing pin controller - bail out. in pinctrl_gpio_can_use_line()
771 if (pinctrl_get_device_gpio_range(gpio, &pctldev, &range)) in pinctrl_gpio_can_use_line()
774 mutex_lock(&pctldev->mutex); in pinctrl_gpio_can_use_line()
777 pin = gpio_to_pin(range, gpio); in pinctrl_gpio_can_use_line()
781 mutex_unlock(&pctldev->mutex); in pinctrl_gpio_can_use_line()
788 * pinctrl_gpio_request() - request a single pin to be used as GPIO
789 * @gpio: the GPIO pin number from the GPIO subsystem number space
791 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
793 * shall *NOT* request GPIO pins to be muxed in.
795 int pinctrl_gpio_request(unsigned gpio) in pinctrl_gpio_request() argument
798 struct pinctrl_gpio_range *range; in pinctrl_gpio_request() local
802 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); in pinctrl_gpio_request()
804 if (pinctrl_ready_for_gpio_range(gpio)) in pinctrl_gpio_request()
809 mutex_lock(&pctldev->mutex); in pinctrl_gpio_request()
812 pin = gpio_to_pin(range, gpio); in pinctrl_gpio_request()
814 ret = pinmux_request_gpio(pctldev, range, pin, gpio); in pinctrl_gpio_request()
816 mutex_unlock(&pctldev->mutex); in pinctrl_gpio_request()
823 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
824 * @gpio: the GPIO pin number from the GPIO subsystem number space
826 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
828 * shall *NOT* request GPIO pins to be muxed out.
830 void pinctrl_gpio_free(unsigned gpio) in pinctrl_gpio_free() argument
833 struct pinctrl_gpio_range *range; in pinctrl_gpio_free() local
837 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); in pinctrl_gpio_free()
841 mutex_lock(&pctldev->mutex); in pinctrl_gpio_free()
844 pin = gpio_to_pin(range, gpio); in pinctrl_gpio_free()
846 pinmux_free_gpio(pctldev, pin, range); in pinctrl_gpio_free()
848 mutex_unlock(&pctldev->mutex); in pinctrl_gpio_free()
852 static int pinctrl_gpio_direction(unsigned gpio, bool input) in pinctrl_gpio_direction() argument
855 struct pinctrl_gpio_range *range; in pinctrl_gpio_direction() local
859 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); in pinctrl_gpio_direction()
864 mutex_lock(&pctldev->mutex); in pinctrl_gpio_direction()
867 pin = gpio_to_pin(range, gpio); in pinctrl_gpio_direction()
868 ret = pinmux_gpio_direction(pctldev, range, pin, input); in pinctrl_gpio_direction()
870 mutex_unlock(&pctldev->mutex); in pinctrl_gpio_direction()
876 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
877 * @gpio: the GPIO pin number from the GPIO subsystem number space
879 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
881 * drivers shall *NOT* touch pin control GPIO calls.
883 int pinctrl_gpio_direction_input(unsigned gpio) in pinctrl_gpio_direction_input() argument
885 return pinctrl_gpio_direction(gpio, true); in pinctrl_gpio_direction_input()
890 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
891 * @gpio: the GPIO pin number from the GPIO subsystem number space
893 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
895 * drivers shall *NOT* touch pin control GPIO calls.
897 int pinctrl_gpio_direction_output(unsigned gpio) in pinctrl_gpio_direction_output() argument
899 return pinctrl_gpio_direction(gpio, false); in pinctrl_gpio_direction_output()
904 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
905 * @gpio: the GPIO pin number from the GPIO subsystem number space
906 * @config: the configuration to apply to the GPIO
908 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
909 * they need to call the underlying pin controller to change GPIO config
912 int pinctrl_gpio_set_config(unsigned gpio, unsigned long config) in pinctrl_gpio_set_config() argument
915 struct pinctrl_gpio_range *range; in pinctrl_gpio_set_config() local
919 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); in pinctrl_gpio_set_config()
923 mutex_lock(&pctldev->mutex); in pinctrl_gpio_set_config()
924 pin = gpio_to_pin(range, gpio); in pinctrl_gpio_set_config()
926 mutex_unlock(&pctldev->mutex); in pinctrl_gpio_set_config()
937 list_for_each_entry(state, &p->states, node) in find_state()
938 if (!strcmp(state->name, name)) in find_state()
951 return ERR_PTR(-ENOMEM); in create_state()
953 state->name = name; in create_state()
954 INIT_LIST_HEAD(&state->settings); in create_state()
956 list_add_tail(&state->node, &p->states); in create_state()
968 state = find_state(p, map->name); in add_setting()
970 state = create_state(p, map->name); in add_setting()
974 if (map->type == PIN_MAP_TYPE_DUMMY_STATE) in add_setting()
979 return -ENOMEM; in add_setting()
981 setting->type = map->type; in add_setting()
984 setting->pctldev = pctldev; in add_setting()
986 setting->pctldev = in add_setting()
987 get_pinctrl_dev_from_devname(map->ctrl_dev_name); in add_setting()
988 if (!setting->pctldev) { in add_setting()
991 if (!strcmp(map->ctrl_dev_name, map->dev_name)) in add_setting()
992 return -ENODEV; in add_setting()
997 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe", in add_setting()
998 map->ctrl_dev_name); in add_setting()
999 return -EPROBE_DEFER; in add_setting()
1002 setting->dev_name = map->dev_name; in add_setting()
1004 switch (map->type) { in add_setting()
1013 ret = -EINVAL; in add_setting()
1021 list_add_tail(&setting->node, &state->settings); in add_setting()
1032 if (p->dev == dev) { in find_pinctrl()
1059 return ERR_PTR(-ENOMEM); in create_pinctrl()
1060 p->dev = dev; in create_pinctrl()
1061 INIT_LIST_HEAD(&p->states); in create_pinctrl()
1062 INIT_LIST_HEAD(&p->dt_maps); in create_pinctrl()
1076 if (strcmp(map->dev_name, devname)) in create_pinctrl()
1086 strcmp(dev_name(pctldev->dev), map->ctrl_dev_name)) in create_pinctrl()
1093 * - Defer, if the pinctrl device is not yet available in create_pinctrl()
1094 * - Fail, if the pinctrl device is not yet available, in create_pinctrl()
1099 * If the error returned was not -EPROBE_DEFER then we in create_pinctrl()
1101 * an -EPROBE_DEFER later, as that is the worst case. in create_pinctrl()
1103 if (ret == -EPROBE_DEFER) { in create_pinctrl()
1117 kref_init(&p->users); in create_pinctrl()
1121 list_add_tail(&p->node, &pinctrl_list); in create_pinctrl()
1128 * pinctrl_get() - retrieves the pinctrl handle for a device
1136 return ERR_PTR(-EINVAL); in pinctrl_get()
1146 kref_get(&p->users); in pinctrl_get()
1157 switch (setting->type) { in pinctrl_free_setting()
1178 list_for_each_entry_safe(state, n1, &p->states, node) { in pinctrl_free()
1179 list_for_each_entry_safe(setting, n2, &state->settings, node) { in pinctrl_free()
1180 pinctrl_free_setting(state == p->state, setting); in pinctrl_free()
1181 list_del(&setting->node); in pinctrl_free()
1184 list_del(&state->node); in pinctrl_free()
1191 list_del(&p->node); in pinctrl_free()
1197 * pinctrl_release() - release the pinctrl handle
1208 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
1213 kref_put(&p->users, pinctrl_release); in pinctrl_put()
1218 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1231 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n", in pinctrl_lookup_state()
1235 state = ERR_PTR(-ENODEV); in pinctrl_lookup_state()
1245 if (pctldev->desc->link_consumers) in pinctrl_link_add()
1246 device_link_add(consumer, pctldev->dev, in pinctrl_link_add()
1252 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
1259 struct pinctrl_state *old_state = READ_ONCE(p->state); in pinctrl_commit_state()
1266 * still owned by the new state will be re-acquired by the call in pinctrl_commit_state()
1269 list_for_each_entry(setting, &old_state->settings, node) { in pinctrl_commit_state()
1270 if (setting->type != PIN_MAP_TYPE_MUX_GROUP) in pinctrl_commit_state()
1276 p->state = NULL; in pinctrl_commit_state()
1278 /* Apply all the settings for the new state - pinmux first */ in pinctrl_commit_state()
1279 list_for_each_entry(setting, &state->settings, node) { in pinctrl_commit_state()
1280 switch (setting->type) { in pinctrl_commit_state()
1289 ret = -EINVAL; in pinctrl_commit_state()
1297 if (p != setting->pctldev->p) in pinctrl_commit_state()
1298 pinctrl_link_add(setting->pctldev, p->dev); in pinctrl_commit_state()
1301 /* Apply all the settings for the new state - pinconf after */ in pinctrl_commit_state()
1302 list_for_each_entry(setting, &state->settings, node) { in pinctrl_commit_state()
1303 switch (setting->type) { in pinctrl_commit_state()
1312 ret = -EINVAL; in pinctrl_commit_state()
1321 if (p != setting->pctldev->p) in pinctrl_commit_state()
1322 pinctrl_link_add(setting->pctldev, p->dev); in pinctrl_commit_state()
1325 p->state = state; in pinctrl_commit_state()
1330 dev_err(p->dev, "Error applying setting, reverse things back\n"); in pinctrl_commit_state()
1332 list_for_each_entry(setting2, &state->settings, node) { in pinctrl_commit_state()
1333 if (&setting2->node == &setting->node) in pinctrl_commit_state()
1342 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP) in pinctrl_commit_state()
1346 /* There's no infinite recursive loop here because p->state is NULL */ in pinctrl_commit_state()
1354 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1360 if (p->state == state) in pinctrl_select_state()
1373 * devm_pinctrl_get() - Resource managed pinctrl_get()
1385 return ERR_PTR(-ENOMEM); in devm_pinctrl_get()
1407 * devm_pinctrl_put() - Resource managed pinctrl_put()
1416 WARN_ON(devres_release(p->dev, devm_pinctrl_release, in devm_pinctrl_put()
1422 * pinctrl_register_mappings() - register a set of pin controller mappings
1423 * @maps: the pincontrol mappings table to register. Note the pinctrl-core
1441 return -EINVAL; in pinctrl_register_mappings()
1447 return -EINVAL; in pinctrl_register_mappings()
1454 return -EINVAL; in pinctrl_register_mappings()
1474 return -EINVAL; in pinctrl_register_mappings()
1480 return -ENOMEM; in pinctrl_register_mappings()
1482 maps_node->maps = maps; in pinctrl_register_mappings()
1483 maps_node->num_maps = num_maps; in pinctrl_register_mappings()
1486 list_add_tail(&maps_node->node, &pinctrl_maps); in pinctrl_register_mappings()
1494 * pinctrl_unregister_mappings() - unregister a set of pin controller mappings
1504 if (maps_node->maps == map) { in pinctrl_unregister_mappings()
1505 list_del(&maps_node->node); in pinctrl_unregister_mappings()
1516 * pinctrl_force_sleep() - turn a given controller device into sleep state
1521 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep)) in pinctrl_force_sleep()
1522 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep); in pinctrl_force_sleep()
1528 * pinctrl_force_default() - turn a given controller device into default state
1533 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default)) in pinctrl_force_default()
1534 return pinctrl_commit_state(pctldev->p, pctldev->hog_default); in pinctrl_force_default()
1540 * pinctrl_init_done() - tell pinctrl probe is done
1549 struct dev_pin_info *pins = dev->pins; in pinctrl_init_done()
1555 if (IS_ERR(pins->init_state)) in pinctrl_init_done()
1558 if (pins->p->state != pins->init_state) in pinctrl_init_done()
1561 if (IS_ERR(pins->default_state)) in pinctrl_init_done()
1564 ret = pinctrl_select_state(pins->p, pins->default_state); in pinctrl_init_done()
1574 struct dev_pin_info *pins = dev->pins; in pinctrl_select_bound_state()
1579 ret = pinctrl_select_state(pins->p, state); in pinctrl_select_bound_state()
1582 state->name); in pinctrl_select_bound_state()
1587 * pinctrl_select_default_state() - select default pinctrl state
1592 if (!dev->pins) in pinctrl_select_default_state()
1595 return pinctrl_select_bound_state(dev, dev->pins->default_state); in pinctrl_select_default_state()
1602 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1612 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1617 if (!dev->pins) in pinctrl_pm_select_sleep_state()
1620 return pinctrl_select_bound_state(dev, dev->pins->sleep_state); in pinctrl_pm_select_sleep_state()
1625 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1630 if (!dev->pins) in pinctrl_pm_select_idle_state()
1633 return pinctrl_select_bound_state(dev, dev->pins->idle_state); in pinctrl_pm_select_idle_state()
1642 struct pinctrl_dev *pctldev = s->private; in pinctrl_pins_show()
1643 const struct pinctrl_ops *ops = pctldev->desc->pctlops; in pinctrl_pins_show()
1646 struct pinctrl_gpio_range *range; in pinctrl_pins_show() local
1651 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins); in pinctrl_pins_show()
1653 mutex_lock(&pctldev->mutex); in pinctrl_pins_show()
1656 for (i = 0; i < pctldev->desc->npins; i++) { in pinctrl_pins_show()
1659 pin = pctldev->desc->pins[i].number; in pinctrl_pins_show()
1665 seq_printf(s, "pin %d (%s) ", pin, desc->name); in pinctrl_pins_show()
1668 gpio_num = -1; in pinctrl_pins_show()
1669 list_for_each_entry(range, &pctldev->gpio_ranges, node) { in pinctrl_pins_show()
1670 if ((pin >= range->pin_base) && in pinctrl_pins_show()
1671 (pin < (range->pin_base + range->npins))) { in pinctrl_pins_show()
1672 gpio_num = range->base + (pin - range->pin_base); in pinctrl_pins_show()
1678 * FIXME: gpio_num comes from the global GPIO numberspace. in pinctrl_pins_show()
1679 * we need to get rid of the range->base eventually and in pinctrl_pins_show()
1686 seq_printf(s, "%u:%s ", gpio_num - chip->gpiodev->base, chip->label); in pinctrl_pins_show()
1691 /* Driver-specific info per pin */ in pinctrl_pins_show()
1692 if (ops->pin_dbg_show) in pinctrl_pins_show()
1693 ops->pin_dbg_show(pctldev, s, pin); in pinctrl_pins_show()
1698 mutex_unlock(&pctldev->mutex); in pinctrl_pins_show()
1706 struct pinctrl_dev *pctldev = s->private; in pinctrl_groups_show()
1707 const struct pinctrl_ops *ops = pctldev->desc->pctlops; in pinctrl_groups_show()
1710 mutex_lock(&pctldev->mutex); in pinctrl_groups_show()
1712 ngroups = ops->get_groups_count(pctldev); in pinctrl_groups_show()
1718 const char *gname = ops->get_group_name(pctldev, selector); in pinctrl_groups_show()
1723 if (ops->get_group_pins) in pinctrl_groups_show()
1724 ret = ops->get_group_pins(pctldev, selector, in pinctrl_groups_show()
1734 mutex_unlock(&pctldev->mutex); in pinctrl_groups_show()
1735 return -EINVAL; in pinctrl_groups_show()
1744 mutex_unlock(&pctldev->mutex); in pinctrl_groups_show()
1752 struct pinctrl_dev *pctldev = s->private; in pinctrl_gpioranges_show()
1753 struct pinctrl_gpio_range *range; in pinctrl_gpioranges_show() local
1755 seq_puts(s, "GPIO ranges handled:\n"); in pinctrl_gpioranges_show()
1757 mutex_lock(&pctldev->mutex); in pinctrl_gpioranges_show()
1760 list_for_each_entry(range, &pctldev->gpio_ranges, node) { in pinctrl_gpioranges_show()
1761 if (range->pins) { in pinctrl_gpioranges_show()
1763 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {", in pinctrl_gpioranges_show()
1764 range->id, range->name, in pinctrl_gpioranges_show()
1765 range->base, (range->base + range->npins - 1)); in pinctrl_gpioranges_show()
1766 for (a = 0; a < range->npins - 1; a++) in pinctrl_gpioranges_show()
1767 seq_printf(s, "%u, ", range->pins[a]); in pinctrl_gpioranges_show()
1768 seq_printf(s, "%u}\n", range->pins[a]); in pinctrl_gpioranges_show()
1771 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n", in pinctrl_gpioranges_show()
1772 range->id, range->name, in pinctrl_gpioranges_show()
1773 range->base, (range->base + range->npins - 1), in pinctrl_gpioranges_show()
1774 range->pin_base, in pinctrl_gpioranges_show()
1775 (range->pin_base + range->npins - 1)); in pinctrl_gpioranges_show()
1778 mutex_unlock(&pctldev->mutex); in pinctrl_gpioranges_show()
1793 seq_printf(s, "%s ", pctldev->desc->name); in pinctrl_devices_show()
1794 if (pctldev->desc->pmxops) in pinctrl_devices_show()
1798 if (pctldev->desc->confops) in pinctrl_devices_show()
1837 map->dev_name, map->name, map_type(map->type), in pinctrl_maps_show()
1838 map->type); in pinctrl_maps_show()
1840 if (map->type != PIN_MAP_TYPE_DUMMY_STATE) in pinctrl_maps_show()
1842 map->ctrl_dev_name); in pinctrl_maps_show()
1844 switch (map->type) { in pinctrl_maps_show()
1876 dev_name(p->dev), in pinctrl_show()
1877 p->state ? p->state->name : "none"); in pinctrl_show()
1879 list_for_each_entry(state, &p->states, node) { in pinctrl_show()
1880 seq_printf(s, " state: %s\n", state->name); in pinctrl_show()
1882 list_for_each_entry(setting, &state->settings, node) { in pinctrl_show()
1883 struct pinctrl_dev *pctldev = setting->pctldev; in pinctrl_show()
1886 map_type(setting->type), in pinctrl_show()
1889 switch (setting->type) { in pinctrl_show()
1917 if (pctldev->desc->name && in pinctrl_init_device_debugfs()
1918 strcmp(dev_name(pctldev->dev), pctldev->desc->name)) { in pinctrl_init_device_debugfs()
1919 debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL, in pinctrl_init_device_debugfs()
1920 "%s-%s", dev_name(pctldev->dev), in pinctrl_init_device_debugfs()
1921 pctldev->desc->name); in pinctrl_init_device_debugfs()
1924 dev_name(pctldev->dev)); in pinctrl_init_device_debugfs()
1928 debugfs_name = dev_name(pctldev->dev); in pinctrl_init_device_debugfs()
1932 pctldev->device_root = device_root; in pinctrl_init_device_debugfs()
1936 dev_name(pctldev->dev)); in pinctrl_init_device_debugfs()
1943 debugfs_create_file("gpio-ranges", 0444, in pinctrl_init_device_debugfs()
1945 if (pctldev->desc->pmxops) in pinctrl_init_device_debugfs()
1947 if (pctldev->desc->confops) in pinctrl_init_device_debugfs()
1953 debugfs_remove_recursive(pctldev->device_root); in pinctrl_remove_device_debugfs()
1965 debugfs_create_file("pinctrl-devices", 0444, in pinctrl_init_debugfs()
1967 debugfs_create_file("pinctrl-maps", 0444, in pinctrl_init_debugfs()
1969 debugfs_create_file("pinctrl-handles", 0444, in pinctrl_init_debugfs()
1991 const struct pinctrl_ops *ops = pctldev->desc->pctlops; in pinctrl_check_ops()
1994 !ops->get_groups_count || in pinctrl_check_ops()
1995 !ops->get_group_name) in pinctrl_check_ops()
1996 return -EINVAL; in pinctrl_check_ops()
2002 * pinctrl_init_controller() - init a pin controller device
2015 return ERR_PTR(-EINVAL); in pinctrl_init_controller()
2016 if (!pctldesc->name) in pinctrl_init_controller()
2017 return ERR_PTR(-EINVAL); in pinctrl_init_controller()
2021 return ERR_PTR(-ENOMEM); in pinctrl_init_controller()
2024 pctldev->owner = pctldesc->owner; in pinctrl_init_controller()
2025 pctldev->desc = pctldesc; in pinctrl_init_controller()
2026 pctldev->driver_data = driver_data; in pinctrl_init_controller()
2027 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL); in pinctrl_init_controller()
2029 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL); in pinctrl_init_controller()
2032 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL); in pinctrl_init_controller()
2034 INIT_LIST_HEAD(&pctldev->gpio_ranges); in pinctrl_init_controller()
2035 INIT_LIST_HEAD(&pctldev->node); in pinctrl_init_controller()
2036 pctldev->dev = dev; in pinctrl_init_controller()
2037 mutex_init(&pctldev->mutex); in pinctrl_init_controller()
2047 if (pctldesc->pmxops) { in pinctrl_init_controller()
2054 if (pctldesc->confops) { in pinctrl_init_controller()
2061 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins); in pinctrl_init_controller()
2062 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins); in pinctrl_init_controller()
2065 pinctrl_free_pindescs(pctldev, pctldesc->pins, in pinctrl_init_controller()
2066 pctldesc->npins); in pinctrl_init_controller()
2073 mutex_destroy(&pctldev->mutex); in pinctrl_init_controller()
2080 pinctrl_free_pindescs(pctldev, pctldesc->pins, in pinctrl_uninit_controller()
2081 pctldesc->npins); in pinctrl_uninit_controller()
2082 mutex_destroy(&pctldev->mutex); in pinctrl_uninit_controller()
2088 pctldev->p = create_pinctrl(pctldev->dev, pctldev); in pinctrl_claim_hogs()
2089 if (PTR_ERR(pctldev->p) == -ENODEV) { in pinctrl_claim_hogs()
2090 dev_dbg(pctldev->dev, "no hogs found\n"); in pinctrl_claim_hogs()
2095 if (IS_ERR(pctldev->p)) { in pinctrl_claim_hogs()
2096 dev_err(pctldev->dev, "error claiming hogs: %li\n", in pinctrl_claim_hogs()
2097 PTR_ERR(pctldev->p)); in pinctrl_claim_hogs()
2099 return PTR_ERR(pctldev->p); in pinctrl_claim_hogs()
2102 pctldev->hog_default = in pinctrl_claim_hogs()
2103 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT); in pinctrl_claim_hogs()
2104 if (IS_ERR(pctldev->hog_default)) { in pinctrl_claim_hogs()
2105 dev_dbg(pctldev->dev, in pinctrl_claim_hogs()
2108 if (pinctrl_select_state(pctldev->p, in pinctrl_claim_hogs()
2109 pctldev->hog_default)) in pinctrl_claim_hogs()
2110 dev_err(pctldev->dev, in pinctrl_claim_hogs()
2114 pctldev->hog_sleep = in pinctrl_claim_hogs()
2115 pinctrl_lookup_state(pctldev->p, in pinctrl_claim_hogs()
2117 if (IS_ERR(pctldev->hog_sleep)) in pinctrl_claim_hogs()
2118 dev_dbg(pctldev->dev, in pinctrl_claim_hogs()
2130 dev_err(pctldev->dev, "could not claim hogs: %i\n", error); in pinctrl_enable()
2135 list_add_tail(&pctldev->node, &pinctrldev_list); in pinctrl_enable()
2145 * pinctrl_register() - register a pin controller device
2176 * pinctrl_register_and_init() - register and init pin controller device
2208 * pinctrl_unregister() - unregister pinmux
2215 struct pinctrl_gpio_range *range, *n; in pinctrl_unregister() local
2220 mutex_lock(&pctldev->mutex); in pinctrl_unregister()
2222 mutex_unlock(&pctldev->mutex); in pinctrl_unregister()
2224 if (!IS_ERR_OR_NULL(pctldev->p)) in pinctrl_unregister()
2225 pinctrl_put(pctldev->p); in pinctrl_unregister()
2228 mutex_lock(&pctldev->mutex); in pinctrl_unregister()
2230 list_del(&pctldev->node); in pinctrl_unregister()
2234 pinctrl_free_pindescs(pctldev, pctldev->desc->pins, in pinctrl_unregister()
2235 pctldev->desc->npins); in pinctrl_unregister()
2236 /* remove gpio ranges map */ in pinctrl_unregister()
2237 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node) in pinctrl_unregister()
2238 list_del(&range->node); in pinctrl_unregister()
2240 mutex_unlock(&pctldev->mutex); in pinctrl_unregister()
2241 mutex_destroy(&pctldev->mutex); in pinctrl_unregister()
2265 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2283 return ERR_PTR(-ENOMEM); in devm_pinctrl_register()
2299 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2319 return -ENOMEM; in devm_pinctrl_register_and_init()
2335 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().