123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635 |
- /*
- * Core driver for the pin control subsystem
- *
- * Copyright (C) 2011-2012 ST-Ericsson SA
- * Written on behalf of Linaro for ST-Ericsson
- * Based on bits of regulator core, gpio core and clk core
- *
- * Author: Linus Walleij <linus.walleij@linaro.org>
- *
- * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
- *
- * License terms: GNU General Public License (GPL) version 2
- */
- #define pr_fmt(fmt) "pinctrl core: " fmt
- #include <linux/kernel.h>
- #include <linux/kref.h>
- #include <linux/export.h>
- #include <linux/init.h>
- #include <linux/device.h>
- #include <linux/slab.h>
- #include <linux/err.h>
- #include <linux/list.h>
- #include <linux/sysfs.h>
- #include <linux/debugfs.h>
- #include <linux/seq_file.h>
- #include <linux/pinctrl/consumer.h>
- #include <linux/pinctrl/pinctrl.h>
- #include <linux/pinctrl/machine.h>
- #include "core.h"
- #include "devicetree.h"
- #include "pinmux.h"
- #include "pinconf.h"
- static bool pinctrl_dummy_state;
- /* Mutex taken by all entry points */
- DEFINE_MUTEX(pinctrl_mutex);
- /* Global list of pin control devices (struct pinctrl_dev) */
- LIST_HEAD(pinctrldev_list);
- /* List of pin controller handles (struct pinctrl) */
- static LIST_HEAD(pinctrl_list);
- /* List of pinctrl maps (struct pinctrl_maps) */
- LIST_HEAD(pinctrl_maps);
- /**
- * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
- *
- * Usually this function is called by platforms without pinctrl driver support
- * but run with some shared drivers using pinctrl APIs.
- * After calling this function, the pinctrl core will return successfully
- * with creating a dummy state for the driver to keep going smoothly.
- */
- void pinctrl_provide_dummies(void)
- {
- pinctrl_dummy_state = true;
- }
- const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
- {
- /* We're not allowed to register devices without name */
- return pctldev->desc->name;
- }
- EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
- const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
- {
- return dev_name(pctldev->dev);
- }
- EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
- void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
- {
- return pctldev->driver_data;
- }
- EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
- /**
- * get_pinctrl_dev_from_devname() - look up pin controller device
- * @devname: the name of a device instance, as returned by dev_name()
- *
- * Looks up a pin control device matching a certain device name or pure device
- * pointer, the pure device pointer will take precedence.
- */
- struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
- {
- struct pinctrl_dev *pctldev = NULL;
- bool found = false;
- if (!devname)
- return NULL;
- list_for_each_entry(pctldev, &pinctrldev_list, node) {
- if (!strcmp(dev_name(pctldev->dev), devname)) {
- /* Matched on device name */
- found = true;
- break;
- }
- }
- return found ? pctldev : NULL;
- }
- /**
- * pin_get_from_name() - look up a pin number from a name
- * @pctldev: the pin control device to lookup the pin on
- * @name: the name of the pin to look up
- */
- int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
- {
- unsigned i, pin;
- /* The pin number can be retrived from the pin controller descriptor */
- for (i = 0; i < pctldev->desc->npins; i++) {
- struct pin_desc *desc;
- pin = pctldev->desc->pins[i].number;
- desc = pin_desc_get(pctldev, pin);
- /* Pin space may be sparse */
- if (desc == NULL)
- continue;
- if (desc->name && !strcmp(name, desc->name))
- return pin;
- }
- return -EINVAL;
- }
- /**
- * pin_get_name_from_id() - look up a pin name from a pin id
- * @pctldev: the pin control device to lookup the pin on
- * @name: the name of the pin to look up
- */
- const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
- {
- const struct pin_desc *desc;
- desc = pin_desc_get(pctldev, pin);
- if (desc == NULL) {
- dev_err(pctldev->dev, "failed to get pin(%d) name\n",
- pin);
- return NULL;
- }
- return desc->name;
- }
- /**
- * pin_is_valid() - check if pin exists on controller
- * @pctldev: the pin control device to check the pin on
- * @pin: pin to check, use the local pin controller index number
- *
- * This tells us whether a certain pin exist on a certain pin controller or
- * not. Pin lists may be sparse, so some pins may not exist.
- */
- bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
- {
- struct pin_desc *pindesc;
- if (pin < 0)
- return false;
- mutex_lock(&pinctrl_mutex);
- pindesc = pin_desc_get(pctldev, pin);
- mutex_unlock(&pinctrl_mutex);
- return pindesc != NULL;
- }
- EXPORT_SYMBOL_GPL(pin_is_valid);
- /* Deletes a range of pin descriptors */
- static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
- const struct pinctrl_pin_desc *pins,
- unsigned num_pins)
- {
- int i;
- for (i = 0; i < num_pins; i++) {
- struct pin_desc *pindesc;
- pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
- pins[i].number);
- if (pindesc != NULL) {
- radix_tree_delete(&pctldev->pin_desc_tree,
- pins[i].number);
- if (pindesc->dynamic_name)
- kfree(pindesc->name);
- }
- kfree(pindesc);
- }
- }
- static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
- unsigned number, const char *name)
- {
- struct pin_desc *pindesc;
- pindesc = pin_desc_get(pctldev, number);
- if (pindesc != NULL) {
- pr_err("pin %d already registered on %s\n", number,
- pctldev->desc->name);
- return -EINVAL;
- }
- pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
- if (pindesc == NULL) {
- dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
- return -ENOMEM;
- }
- /* Set owner */
- pindesc->pctldev = pctldev;
- /* Copy basic pin info */
- if (name) {
- pindesc->name = name;
- } else {
- pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
- if (pindesc->name == NULL) {
- kfree(pindesc);
- return -ENOMEM;
- }
- pindesc->dynamic_name = true;
- }
- radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
- pr_debug("registered pin %d (%s) on %s\n",
- number, pindesc->name, pctldev->desc->name);
- return 0;
- }
- static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
- struct pinctrl_pin_desc const *pins,
- unsigned num_descs)
- {
- unsigned i;
- int ret = 0;
- for (i = 0; i < num_descs; i++) {
- ret = pinctrl_register_one_pin(pctldev,
- pins[i].number, pins[i].name);
- if (ret)
- return ret;
- }
- return 0;
- }
- /**
- * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
- * @pctldev: pin controller device to check
- * @gpio: gpio pin to check taken from the global GPIO pin space
- *
- * Tries to match a GPIO pin number to the ranges handled by a certain pin
- * controller, return the range or NULL
- */
- static struct pinctrl_gpio_range *
- pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
- {
- struct pinctrl_gpio_range *range = NULL;
- /* Loop over the ranges */
- list_for_each_entry(range, &pctldev->gpio_ranges, node) {
- /* Check if we're in the valid range */
- if (gpio >= range->base &&
- gpio < range->base + range->npins) {
- return range;
- }
- }
- return NULL;
- }
- /**
- * pinctrl_get_device_gpio_range() - find device for GPIO range
- * @gpio: the pin to locate the pin controller for
- * @outdev: the pin control device if found
- * @outrange: the GPIO range if found
- *
- * Find the pin controller handling a certain GPIO pin from the pinspace of
- * the GPIO subsystem, return the device and the matching GPIO range. Returns
- * -EPROBE_DEFER if the GPIO range could not be found in any device since it
- * may still have not been registered.
- */
- static int pinctrl_get_device_gpio_range(unsigned gpio,
- struct pinctrl_dev **outdev,
- struct pinctrl_gpio_range **outrange)
- {
- struct pinctrl_dev *pctldev = NULL;
- /* Loop over the pin controllers */
- list_for_each_entry(pctldev, &pinctrldev_list, node) {
- struct pinctrl_gpio_range *range;
- range = pinctrl_match_gpio_range(pctldev, gpio);
- if (range != NULL) {
- *outdev = pctldev;
- *outrange = range;
- return 0;
- }
- }
- return -EPROBE_DEFER;
- }
- /**
- * pinctrl_add_gpio_range() - register a GPIO range for a controller
- * @pctldev: pin controller device to add the range to
- * @range: the GPIO range to add
- *
- * This adds a range of GPIOs to be handled by a certain pin controller. Call
- * this to register handled ranges after registering your pin controller.
- */
- void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
- struct pinctrl_gpio_range *range)
- {
- mutex_lock(&pinctrl_mutex);
- list_add_tail(&range->node, &pctldev->gpio_ranges);
- mutex_unlock(&pinctrl_mutex);
- }
- EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
- void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
- struct pinctrl_gpio_range *ranges,
- unsigned nranges)
- {
- int i;
- for (i = 0; i < nranges; i++)
- pinctrl_add_gpio_range(pctldev, &ranges[i]);
- }
- EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
- struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
- struct pinctrl_gpio_range *range)
- {
- struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname);
- /*
- * If we can't find this device, let's assume that is because
- * it has not probed yet, so the driver trying to register this
- * range need to defer probing.
- */
- if (!pctldev)
- return ERR_PTR(-EPROBE_DEFER);
- pinctrl_add_gpio_range(pctldev, range);
- return pctldev;
- }
- EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
- /**
- * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
- * @pctldev: the pin controller device to look in
- * @pin: a controller-local number to find the range for
- */
- struct pinctrl_gpio_range *
- pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
- unsigned int pin)
- {
- struct pinctrl_gpio_range *range = NULL;
- /* Loop over the ranges */
- list_for_each_entry(range, &pctldev->gpio_ranges, node) {
- /* Check if we're in the valid range */
- if (pin >= range->pin_base &&
- pin < range->pin_base + range->npins) {
- return range;
- }
- }
- return NULL;
- }
- EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
- /**
- * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
- * @pctldev: pin controller device to remove the range from
- * @range: the GPIO range to remove
- */
- void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
- struct pinctrl_gpio_range *range)
- {
- mutex_lock(&pinctrl_mutex);
- list_del(&range->node);
- mutex_unlock(&pinctrl_mutex);
- }
- EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
- /**
- * pinctrl_get_group_selector() - returns the group selector for a group
- * @pctldev: the pin controller handling the group
- * @pin_group: the pin group to look up
- */
- int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
- const char *pin_group)
- {
- const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
- unsigned ngroups = pctlops->get_groups_count(pctldev);
- unsigned group_selector = 0;
- while (group_selector < ngroups) {
- const char *gname = pctlops->get_group_name(pctldev,
- group_selector);
- if (!strcmp(gname, pin_group)) {
- dev_dbg(pctldev->dev,
- "found group selector %u for %s\n",
- group_selector,
- pin_group);
- return group_selector;
- }
- group_selector++;
- }
- dev_err(pctldev->dev, "does not have pin group %s\n",
- pin_group);
- return -EINVAL;
- }
- /**
- * pinctrl_request_gpio() - request a single pin to be used in as GPIO
- * @gpio: the GPIO pin number from the GPIO subsystem number space
- *
- * This function should *ONLY* be used from gpiolib-based GPIO drivers,
- * as part of their gpio_request() semantics, platforms and individual drivers
- * shall *NOT* request GPIO pins to be muxed in.
- */
- int pinctrl_request_gpio(unsigned gpio)
- {
- struct pinctrl_dev *pctldev;
- struct pinctrl_gpio_range *range;
- int ret;
- int pin;
- mutex_lock(&pinctrl_mutex);
- ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
- if (ret) {
- mutex_unlock(&pinctrl_mutex);
- return ret;
- }
- /* Convert to the pin controllers number space */
- pin = gpio - range->base + range->pin_base;
- ret = pinmux_request_gpio(pctldev, range, pin, gpio);
- mutex_unlock(&pinctrl_mutex);
- return ret;
- }
- EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
- /**
- * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
- * @gpio: the GPIO pin number from the GPIO subsystem number space
- *
- * This function should *ONLY* be used from gpiolib-based GPIO drivers,
- * as part of their gpio_free() semantics, platforms and individual drivers
- * shall *NOT* request GPIO pins to be muxed out.
- */
- void pinctrl_free_gpio(unsigned gpio)
- {
- struct pinctrl_dev *pctldev;
- struct pinctrl_gpio_range *range;
- int ret;
- int pin;
- mutex_lock(&pinctrl_mutex);
- ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
- if (ret) {
- mutex_unlock(&pinctrl_mutex);
- return;
- }
- /* Convert to the pin controllers number space */
- pin = gpio - range->base + range->pin_base;
- pinmux_free_gpio(pctldev, pin, range);
- mutex_unlock(&pinctrl_mutex);
- }
- EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
- static int pinctrl_gpio_direction(unsigned gpio, bool input)
- {
- struct pinctrl_dev *pctldev;
- struct pinctrl_gpio_range *range;
- int ret;
- int pin;
- ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
- if (ret)
- return ret;
- /* Convert to the pin controllers number space */
- pin = gpio - range->base + range->pin_base;
- return pinmux_gpio_direction(pctldev, range, pin, input);
- }
- /**
- * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
- * @gpio: the GPIO pin number from the GPIO subsystem number space
- *
- * This function should *ONLY* be used from gpiolib-based GPIO drivers,
- * as part of their gpio_direction_input() semantics, platforms and individual
- * drivers shall *NOT* touch pin control GPIO calls.
- */
- int pinctrl_gpio_direction_input(unsigned gpio)
- {
- int ret;
- mutex_lock(&pinctrl_mutex);
- ret = pinctrl_gpio_direction(gpio, true);
- mutex_unlock(&pinctrl_mutex);
- return ret;
- }
- EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
- /**
- * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
- * @gpio: the GPIO pin number from the GPIO subsystem number space
- *
- * This function should *ONLY* be used from gpiolib-based GPIO drivers,
- * as part of their gpio_direction_output() semantics, platforms and individual
- * drivers shall *NOT* touch pin control GPIO calls.
- */
- int pinctrl_gpio_direction_output(unsigned gpio)
- {
- int ret;
- mutex_lock(&pinctrl_mutex);
- ret = pinctrl_gpio_direction(gpio, false);
- mutex_unlock(&pinctrl_mutex);
- return ret;
- }
- EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
- static struct pinctrl_state *find_state(struct pinctrl *p,
- const char *name)
- {
- struct pinctrl_state *state;
- list_for_each_entry(state, &p->states, node)
- if (!strcmp(state->name, name))
- return state;
- return NULL;
- }
- static struct pinctrl_state *create_state(struct pinctrl *p,
- const char *name)
- {
- struct pinctrl_state *state;
- state = kzalloc(sizeof(*state), GFP_KERNEL);
- if (state == NULL) {
- dev_err(p->dev,
- "failed to alloc struct pinctrl_state\n");
- return ERR_PTR(-ENOMEM);
- }
- state->name = name;
- INIT_LIST_HEAD(&state->settings);
- list_add_tail(&state->node, &p->states);
- return state;
- }
- static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
- {
- struct pinctrl_state *state;
- struct pinctrl_setting *setting;
- int ret;
- state = find_state(p, map->name);
- if (!state)
- state = create_state(p, map->name);
- if (IS_ERR(state))
- return PTR_ERR(state);
- if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
- return 0;
- setting = kzalloc(sizeof(*setting), GFP_KERNEL);
- if (setting == NULL) {
- dev_err(p->dev,
- "failed to alloc struct pinctrl_setting\n");
- return -ENOMEM;
- }
- setting->type = map->type;
- setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
- if (setting->pctldev == NULL) {
- kfree(setting);
- /* Do not defer probing of hogs (circular loop) */
- if (!strcmp(map->ctrl_dev_name, map->dev_name))
- return -ENODEV;
- /*
- * OK let us guess that the driver is not there yet, and
- * let's defer obtaining this pinctrl handle to later...
- */
- dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
- map->ctrl_dev_name);
- return -EPROBE_DEFER;
- }
- setting->dev_name = map->dev_name;
- switch (map->type) {
- case PIN_MAP_TYPE_MUX_GROUP:
- ret = pinmux_map_to_setting(map, setting);
- break;
- case PIN_MAP_TYPE_CONFIGS_PIN:
- case PIN_MAP_TYPE_CONFIGS_GROUP:
- ret = pinconf_map_to_setting(map, setting);
- break;
- default:
- ret = -EINVAL;
- break;
- }
- if (ret < 0) {
- kfree(setting);
- return ret;
- }
- list_add_tail(&setting->node, &state->settings);
- return 0;
- }
- static struct pinctrl *find_pinctrl(struct device *dev)
- {
- struct pinctrl *p;
- list_for_each_entry(p, &pinctrl_list, node)
- if (p->dev == dev)
- return p;
- return NULL;
- }
- static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
- static struct pinctrl *create_pinctrl(struct device *dev)
- {
- struct pinctrl *p;
- const char *devname;
- struct pinctrl_maps *maps_node;
- int i;
- struct pinctrl_map const *map;
- int ret;
- /*
- * create the state cookie holder struct pinctrl for each
- * mapping, this is what consumers will get when requesting
- * a pin control handle with pinctrl_get()
- */
- p = kzalloc(sizeof(*p), GFP_KERNEL);
- if (p == NULL) {
- dev_err(dev, "failed to alloc struct pinctrl\n");
- return ERR_PTR(-ENOMEM);
- }
- p->dev = dev;
- INIT_LIST_HEAD(&p->states);
- INIT_LIST_HEAD(&p->dt_maps);
- ret = pinctrl_dt_to_map(p);
- if (ret < 0) {
- kfree(p);
- return ERR_PTR(ret);
- }
- devname = dev_name(dev);
- /* Iterate over the pin control maps to locate the right ones */
- for_each_maps(maps_node, i, map) {
- /* Map must be for this device */
- if (strcmp(map->dev_name, devname))
- continue;
- ret = add_setting(p, map);
- /*
- * At this point the adding of a setting may:
- *
- * - Defer, if the pinctrl device is not yet available
- * - Fail, if the pinctrl device is not yet available,
- * AND the setting is a hog. We cannot defer that, since
- * the hog will kick in immediately after the device
- * is registered.
- *
- * If the error returned was not -EPROBE_DEFER then we
- * accumulate the errors to see if we end up with
- * an -EPROBE_DEFER later, as that is the worst case.
- */
- if (ret == -EPROBE_DEFER) {
- pinctrl_put_locked(p, false);
- return ERR_PTR(ret);
- }
- }
- if (ret < 0) {
- /* If some other error than deferral occured, return here */
- pinctrl_put_locked(p, false);
- return ERR_PTR(ret);
- }
- kref_init(&p->users);
- /* Add the pinctrl handle to the global list */
- list_add_tail(&p->node, &pinctrl_list);
- return p;
- }
- static struct pinctrl *pinctrl_get_locked(struct device *dev)
- {
- struct pinctrl *p;
- if (WARN_ON(!dev))
- return ERR_PTR(-EINVAL);
- /*
- * See if somebody else (such as the device core) has already
- * obtained a handle to the pinctrl for this device. In that case,
- * return another pointer to it.
- */
- p = find_pinctrl(dev);
- if (p != NULL) {
- dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
- kref_get(&p->users);
- return p;
- }
- return create_pinctrl(dev);
- }
- /**
- * pinctrl_get() - retrieves the pinctrl handle for a device
- * @dev: the device to obtain the handle for
- */
- struct pinctrl *pinctrl_get(struct device *dev)
- {
- struct pinctrl *p;
- mutex_lock(&pinctrl_mutex);
- p = pinctrl_get_locked(dev);
- mutex_unlock(&pinctrl_mutex);
- return p;
- }
- EXPORT_SYMBOL_GPL(pinctrl_get);
- static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
- {
- struct pinctrl_state *state, *n1;
- struct pinctrl_setting *setting, *n2;
- list_for_each_entry_safe(state, n1, &p->states, node) {
- list_for_each_entry_safe(setting, n2, &state->settings, node) {
- switch (setting->type) {
- case PIN_MAP_TYPE_MUX_GROUP:
- if (state == p->state)
- pinmux_disable_setting(setting);
- pinmux_free_setting(setting);
- break;
- case PIN_MAP_TYPE_CONFIGS_PIN:
- case PIN_MAP_TYPE_CONFIGS_GROUP:
- pinconf_free_setting(setting);
- break;
- default:
- break;
- }
- list_del(&setting->node);
- kfree(setting);
- }
- list_del(&state->node);
- kfree(state);
- }
- pinctrl_dt_free_maps(p);
- if (inlist)
- list_del(&p->node);
- kfree(p);
- }
- /**
- * pinctrl_release() - release the pinctrl handle
- * @kref: the kref in the pinctrl being released
- */
- static void pinctrl_release(struct kref *kref)
- {
- struct pinctrl *p = container_of(kref, struct pinctrl, users);
- pinctrl_put_locked(p, true);
- }
- /**
- * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
- * @p: the pinctrl handle to release
- */
- void pinctrl_put(struct pinctrl *p)
- {
- mutex_lock(&pinctrl_mutex);
- kref_put(&p->users, pinctrl_release);
- mutex_unlock(&pinctrl_mutex);
- }
- EXPORT_SYMBOL_GPL(pinctrl_put);
- static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
- const char *name)
- {
- struct pinctrl_state *state;
- state = find_state(p, name);
- if (!state) {
- if (pinctrl_dummy_state) {
- /* create dummy state */
- dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
- name);
- state = create_state(p, name);
- } else
- state = ERR_PTR(-ENODEV);
- }
- return state;
- }
- /**
- * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
- * @p: the pinctrl handle to retrieve the state from
- * @name: the state name to retrieve
- */
- struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
- {
- struct pinctrl_state *s;
- mutex_lock(&pinctrl_mutex);
- s = pinctrl_lookup_state_locked(p, name);
- mutex_unlock(&pinctrl_mutex);
- return s;
- }
- EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
- static int pinctrl_select_state_locked(struct pinctrl *p,
- struct pinctrl_state *state)
- {
- struct pinctrl_setting *setting, *setting2;
- int ret;
- if (p->state == state)
- return 0;
- if (p->state) {
- /*
- * The set of groups with a mux configuration in the old state
- * may not be identical to the set of groups with a mux setting
- * in the new state. While this might be unusual, it's entirely
- * possible for the "user"-supplied mapping table to be written
- * that way. For each group that was configured in the old state
- * but not in the new state, this code puts that group into a
- * safe/disabled state.
- */
- list_for_each_entry(setting, &p->state->settings, node) {
- bool found = false;
- if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
- continue;
- list_for_each_entry(setting2, &state->settings, node) {
- if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
- continue;
- if (setting2->data.mux.group ==
- setting->data.mux.group) {
- found = true;
- break;
- }
- }
- if (!found)
- pinmux_disable_setting(setting);
- }
- }
- p->state = state;
- /* Apply all the settings for the new state */
- list_for_each_entry(setting, &state->settings, node) {
- switch (setting->type) {
- case PIN_MAP_TYPE_MUX_GROUP:
- ret = pinmux_enable_setting(setting);
- break;
- case PIN_MAP_TYPE_CONFIGS_PIN:
- case PIN_MAP_TYPE_CONFIGS_GROUP:
- ret = pinconf_apply_setting(setting);
- break;
- default:
- ret = -EINVAL;
- break;
- }
- if (ret < 0) {
- /* FIXME: Difficult to return to prev state */
- return ret;
- }
- }
- return 0;
- }
- /**
- * pinctrl_select() - select/activate/program a pinctrl state to HW
- * @p: the pinctrl handle for the device that requests configuratio
- * @state: the state handle to select/activate/program
- */
- int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
- {
- int ret;
- mutex_lock(&pinctrl_mutex);
- ret = pinctrl_select_state_locked(p, state);
- mutex_unlock(&pinctrl_mutex);
- return ret;
- }
- EXPORT_SYMBOL_GPL(pinctrl_select_state);
- static void devm_pinctrl_release(struct device *dev, void *res)
- {
- pinctrl_put(*(struct pinctrl **)res);
- }
- /**
- * struct devm_pinctrl_get() - Resource managed pinctrl_get()
- * @dev: the device to obtain the handle for
- *
- * If there is a need to explicitly destroy the returned struct pinctrl,
- * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
- */
- struct pinctrl *devm_pinctrl_get(struct device *dev)
- {
- struct pinctrl **ptr, *p;
- ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
- if (!ptr)
- return ERR_PTR(-ENOMEM);
- p = pinctrl_get(dev);
- if (!IS_ERR(p)) {
- *ptr = p;
- devres_add(dev, ptr);
- } else {
- devres_free(ptr);
- }
- return p;
- }
- EXPORT_SYMBOL_GPL(devm_pinctrl_get);
- static int devm_pinctrl_match(struct device *dev, void *res, void *data)
- {
- struct pinctrl **p = res;
- return *p == data;
- }
- /**
- * devm_pinctrl_put() - Resource managed pinctrl_put()
- * @p: the pinctrl handle to release
- *
- * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
- * this function will not need to be called and the resource management
- * code will ensure that the resource is freed.
- */
- void devm_pinctrl_put(struct pinctrl *p)
- {
- WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
- devm_pinctrl_match, p));
- pinctrl_put(p);
- }
- EXPORT_SYMBOL_GPL(devm_pinctrl_put);
- int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
- bool dup, bool locked)
- {
- int i, ret;
- struct pinctrl_maps *maps_node;
- pr_debug("add %d pinmux maps\n", num_maps);
- /* First sanity check the new mapping */
- for (i = 0; i < num_maps; i++) {
- if (!maps[i].dev_name) {
- pr_err("failed to register map %s (%d): no device given\n",
- maps[i].name, i);
- return -EINVAL;
- }
- if (!maps[i].name) {
- pr_err("failed to register map %d: no map name given\n",
- i);
- return -EINVAL;
- }
- if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
- !maps[i].ctrl_dev_name) {
- pr_err("failed to register map %s (%d): no pin control device given\n",
- maps[i].name, i);
- return -EINVAL;
- }
- switch (maps[i].type) {
- case PIN_MAP_TYPE_DUMMY_STATE:
- break;
- case PIN_MAP_TYPE_MUX_GROUP:
- ret = pinmux_validate_map(&maps[i], i);
- if (ret < 0)
- return ret;
- break;
- case PIN_MAP_TYPE_CONFIGS_PIN:
- case PIN_MAP_TYPE_CONFIGS_GROUP:
- ret = pinconf_validate_map(&maps[i], i);
- if (ret < 0)
- return ret;
- break;
- default:
- pr_err("failed to register map %s (%d): invalid type given\n",
- maps[i].name, i);
- return -EINVAL;
- }
- }
- maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
- if (!maps_node) {
- pr_err("failed to alloc struct pinctrl_maps\n");
- return -ENOMEM;
- }
- maps_node->num_maps = num_maps;
- if (dup) {
- maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
- GFP_KERNEL);
- if (!maps_node->maps) {
- pr_err("failed to duplicate mapping table\n");
- kfree(maps_node);
- return -ENOMEM;
- }
- } else {
- maps_node->maps = maps;
- }
- if (!locked)
- mutex_lock(&pinctrl_mutex);
- list_add_tail(&maps_node->node, &pinctrl_maps);
- if (!locked)
- mutex_unlock(&pinctrl_mutex);
- return 0;
- }
- /**
- * pinctrl_register_mappings() - register a set of pin controller mappings
- * @maps: the pincontrol mappings table to register. This should probably be
- * marked with __initdata so it can be discarded after boot. This
- * function will perform a shallow copy for the mapping entries.
- * @num_maps: the number of maps in the mapping table
- */
- int pinctrl_register_mappings(struct pinctrl_map const *maps,
- unsigned num_maps)
- {
- return pinctrl_register_map(maps, num_maps, true, false);
- }
- void pinctrl_unregister_map(struct pinctrl_map const *map)
- {
- struct pinctrl_maps *maps_node;
- list_for_each_entry(maps_node, &pinctrl_maps, node) {
- if (maps_node->maps == map) {
- list_del(&maps_node->node);
- kfree(maps_node);
- return;
- }
- }
- }
- /**
- * pinctrl_force_sleep() - turn a given controller device into sleep state
- * @pctldev: pin controller device
- */
- int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
- {
- if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
- return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
- return 0;
- }
- EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
- /**
- * pinctrl_force_default() - turn a given controller device into default state
- * @pctldev: pin controller device
- */
- int pinctrl_force_default(struct pinctrl_dev *pctldev)
- {
- if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
- return pinctrl_select_state(pctldev->p, pctldev->hog_default);
- return 0;
- }
- EXPORT_SYMBOL_GPL(pinctrl_force_default);
- #ifdef CONFIG_DEBUG_FS
- static int pinctrl_pins_show(struct seq_file *s, void *what)
- {
- struct pinctrl_dev *pctldev = s->private;
- const struct pinctrl_ops *ops = pctldev->desc->pctlops;
- unsigned i, pin;
- seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
- mutex_lock(&pinctrl_mutex);
- /* The pin number can be retrived from the pin controller descriptor */
- for (i = 0; i < pctldev->desc->npins; i++) {
- struct pin_desc *desc;
- pin = pctldev->desc->pins[i].number;
- desc = pin_desc_get(pctldev, pin);
- /* Pin space may be sparse */
- if (desc == NULL)
- continue;
- seq_printf(s, "pin %d (%s) ", pin,
- desc->name ? desc->name : "unnamed");
- /* Driver-specific info per pin */
- if (ops->pin_dbg_show)
- ops->pin_dbg_show(pctldev, s, pin);
- seq_puts(s, "\n");
- }
- mutex_unlock(&pinctrl_mutex);
- return 0;
- }
- static int pinctrl_groups_show(struct seq_file *s, void *what)
- {
- struct pinctrl_dev *pctldev = s->private;
- const struct pinctrl_ops *ops = pctldev->desc->pctlops;
- unsigned ngroups, selector = 0;
- ngroups = ops->get_groups_count(pctldev);
- mutex_lock(&pinctrl_mutex);
- seq_puts(s, "registered pin groups:\n");
- while (selector < ngroups) {
- const unsigned *pins;
- unsigned num_pins;
- const char *gname = ops->get_group_name(pctldev, selector);
- const char *pname;
- int ret;
- int i;
- ret = ops->get_group_pins(pctldev, selector,
- &pins, &num_pins);
- if (ret)
- seq_printf(s, "%s [ERROR GETTING PINS]\n",
- gname);
- else {
- seq_printf(s, "group: %s\n", gname);
- for (i = 0; i < num_pins; i++) {
- pname = pin_get_name(pctldev, pins[i]);
- if (WARN_ON(!pname)) {
- mutex_unlock(&pinctrl_mutex);
- return -EINVAL;
- }
- seq_printf(s, "pin %d (%s)\n", pins[i], pname);
- }
- seq_puts(s, "\n");
- }
- selector++;
- }
- mutex_unlock(&pinctrl_mutex);
- return 0;
- }
- static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
- {
- struct pinctrl_dev *pctldev = s->private;
- struct pinctrl_gpio_range *range = NULL;
- seq_puts(s, "GPIO ranges handled:\n");
- mutex_lock(&pinctrl_mutex);
- /* Loop over the ranges */
- list_for_each_entry(range, &pctldev->gpio_ranges, node) {
- seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
- range->id, range->name,
- range->base, (range->base + range->npins - 1),
- range->pin_base,
- (range->pin_base + range->npins - 1));
- }
- mutex_unlock(&pinctrl_mutex);
- return 0;
- }
- static int pinctrl_devices_show(struct seq_file *s, void *what)
- {
- struct pinctrl_dev *pctldev;
- seq_puts(s, "name [pinmux] [pinconf]\n");
- mutex_lock(&pinctrl_mutex);
- list_for_each_entry(pctldev, &pinctrldev_list, node) {
- seq_printf(s, "%s ", pctldev->desc->name);
- if (pctldev->desc->pmxops)
- seq_puts(s, "yes ");
- else
- seq_puts(s, "no ");
- if (pctldev->desc->confops)
- seq_puts(s, "yes");
- else
- seq_puts(s, "no");
- seq_puts(s, "\n");
- }
- mutex_unlock(&pinctrl_mutex);
- return 0;
- }
- static inline const char *map_type(enum pinctrl_map_type type)
- {
- static const char * const names[] = {
- "INVALID",
- "DUMMY_STATE",
- "MUX_GROUP",
- "CONFIGS_PIN",
- "CONFIGS_GROUP",
- };
- if (type >= ARRAY_SIZE(names))
- return "UNKNOWN";
- return names[type];
- }
- static int pinctrl_maps_show(struct seq_file *s, void *what)
- {
- struct pinctrl_maps *maps_node;
- int i;
- struct pinctrl_map const *map;
- seq_puts(s, "Pinctrl maps:\n");
- mutex_lock(&pinctrl_mutex);
- for_each_maps(maps_node, i, map) {
- seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
- map->dev_name, map->name, map_type(map->type),
- map->type);
- if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
- seq_printf(s, "controlling device %s\n",
- map->ctrl_dev_name);
- switch (map->type) {
- case PIN_MAP_TYPE_MUX_GROUP:
- pinmux_show_map(s, map);
- break;
- case PIN_MAP_TYPE_CONFIGS_PIN:
- case PIN_MAP_TYPE_CONFIGS_GROUP:
- pinconf_show_map(s, map);
- break;
- default:
- break;
- }
- seq_printf(s, "\n");
- }
- mutex_unlock(&pinctrl_mutex);
- return 0;
- }
- static int pinctrl_show(struct seq_file *s, void *what)
- {
- struct pinctrl *p;
- struct pinctrl_state *state;
- struct pinctrl_setting *setting;
- seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
- mutex_lock(&pinctrl_mutex);
- list_for_each_entry(p, &pinctrl_list, node) {
- seq_printf(s, "device: %s current state: %s\n",
- dev_name(p->dev),
- p->state ? p->state->name : "none");
- list_for_each_entry(state, &p->states, node) {
- seq_printf(s, " state: %s\n", state->name);
- list_for_each_entry(setting, &state->settings, node) {
- struct pinctrl_dev *pctldev = setting->pctldev;
- seq_printf(s, " type: %s controller %s ",
- map_type(setting->type),
- pinctrl_dev_get_name(pctldev));
- switch (setting->type) {
- case PIN_MAP_TYPE_MUX_GROUP:
- pinmux_show_setting(s, setting);
- break;
- case PIN_MAP_TYPE_CONFIGS_PIN:
- case PIN_MAP_TYPE_CONFIGS_GROUP:
- pinconf_show_setting(s, setting);
- break;
- default:
- break;
- }
- }
- }
- }
- mutex_unlock(&pinctrl_mutex);
- return 0;
- }
- static int pinctrl_pins_open(struct inode *inode, struct file *file)
- {
- return single_open(file, pinctrl_pins_show, inode->i_private);
- }
- static int pinctrl_groups_open(struct inode *inode, struct file *file)
- {
- return single_open(file, pinctrl_groups_show, inode->i_private);
- }
- static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
- {
- return single_open(file, pinctrl_gpioranges_show, inode->i_private);
- }
- static int pinctrl_devices_open(struct inode *inode, struct file *file)
- {
- return single_open(file, pinctrl_devices_show, NULL);
- }
- static int pinctrl_maps_open(struct inode *inode, struct file *file)
- {
- return single_open(file, pinctrl_maps_show, NULL);
- }
- static int pinctrl_open(struct inode *inode, struct file *file)
- {
- return single_open(file, pinctrl_show, NULL);
- }
- static const struct file_operations pinctrl_pins_ops = {
- .open = pinctrl_pins_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static const struct file_operations pinctrl_groups_ops = {
- .open = pinctrl_groups_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static const struct file_operations pinctrl_gpioranges_ops = {
- .open = pinctrl_gpioranges_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static const struct file_operations pinctrl_devices_ops = {
- .open = pinctrl_devices_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static const struct file_operations pinctrl_maps_ops = {
- .open = pinctrl_maps_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static const struct file_operations pinctrl_ops = {
- .open = pinctrl_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static struct dentry *debugfs_root;
- static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
- {
- struct dentry *device_root;
- device_root = debugfs_create_dir(dev_name(pctldev->dev),
- debugfs_root);
- pctldev->device_root = device_root;
- if (IS_ERR(device_root) || !device_root) {
- pr_warn("failed to create debugfs directory for %s\n",
- dev_name(pctldev->dev));
- return;
- }
- debugfs_create_file("pins", S_IFREG | S_IRUGO,
- device_root, pctldev, &pinctrl_pins_ops);
- debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
- device_root, pctldev, &pinctrl_groups_ops);
- debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
- device_root, pctldev, &pinctrl_gpioranges_ops);
- pinmux_init_device_debugfs(device_root, pctldev);
- pinconf_init_device_debugfs(device_root, pctldev);
- }
- static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
- {
- debugfs_remove_recursive(pctldev->device_root);
- }
- static void pinctrl_init_debugfs(void)
- {
- debugfs_root = debugfs_create_dir("pinctrl", NULL);
- if (IS_ERR(debugfs_root) || !debugfs_root) {
- pr_warn("failed to create debugfs directory\n");
- debugfs_root = NULL;
- return;
- }
- debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
- debugfs_root, NULL, &pinctrl_devices_ops);
- debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
- debugfs_root, NULL, &pinctrl_maps_ops);
- debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
- debugfs_root, NULL, &pinctrl_ops);
- }
- #else /* CONFIG_DEBUG_FS */
- static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
- {
- }
- static void pinctrl_init_debugfs(void)
- {
- }
- static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
- {
- }
- #endif
- static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
- {
- const struct pinctrl_ops *ops = pctldev->desc->pctlops;
- if (!ops ||
- !ops->get_groups_count ||
- !ops->get_group_name ||
- !ops->get_group_pins)
- return -EINVAL;
- if (ops->dt_node_to_map && !ops->dt_free_map)
- return -EINVAL;
- return 0;
- }
- /**
- * pinctrl_register() - register a pin controller device
- * @pctldesc: descriptor for this pin controller
- * @dev: parent device for this pin controller
- * @driver_data: private pin controller data for this pin controller
- */
- struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
- struct device *dev, void *driver_data)
- {
- struct pinctrl_dev *pctldev;
- int ret;
- if (!pctldesc)
- return NULL;
- if (!pctldesc->name)
- return NULL;
- pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
- if (pctldev == NULL) {
- dev_err(dev, "failed to alloc struct pinctrl_dev\n");
- return NULL;
- }
- /* Initialize pin control device struct */
- pctldev->owner = pctldesc->owner;
- pctldev->desc = pctldesc;
- pctldev->driver_data = driver_data;
- INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
- INIT_LIST_HEAD(&pctldev->gpio_ranges);
- pctldev->dev = dev;
- /* check core ops for sanity */
- if (pinctrl_check_ops(pctldev)) {
- dev_err(dev, "pinctrl ops lacks necessary functions\n");
- goto out_err;
- }
- /* If we're implementing pinmuxing, check the ops for sanity */
- if (pctldesc->pmxops) {
- if (pinmux_check_ops(pctldev))
- goto out_err;
- }
- /* If we're implementing pinconfig, check the ops for sanity */
- if (pctldesc->confops) {
- if (pinconf_check_ops(pctldev))
- goto out_err;
- }
- /* Register all the pins */
- dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
- ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
- if (ret) {
- dev_err(dev, "error during pin registration\n");
- pinctrl_free_pindescs(pctldev, pctldesc->pins,
- pctldesc->npins);
- goto out_err;
- }
- mutex_lock(&pinctrl_mutex);
- list_add_tail(&pctldev->node, &pinctrldev_list);
- pctldev->p = pinctrl_get_locked(pctldev->dev);
- if (!IS_ERR(pctldev->p)) {
- pctldev->hog_default =
- pinctrl_lookup_state_locked(pctldev->p,
- PINCTRL_STATE_DEFAULT);
- if (IS_ERR(pctldev->hog_default)) {
- dev_dbg(dev, "failed to lookup the default state\n");
- } else {
- if (pinctrl_select_state_locked(pctldev->p,
- pctldev->hog_default))
- dev_err(dev,
- "failed to select default state\n");
- }
- pctldev->hog_sleep =
- pinctrl_lookup_state_locked(pctldev->p,
- PINCTRL_STATE_SLEEP);
- if (IS_ERR(pctldev->hog_sleep))
- dev_dbg(dev, "failed to lookup the sleep state\n");
- }
- mutex_unlock(&pinctrl_mutex);
- pinctrl_init_device_debugfs(pctldev);
- return pctldev;
- out_err:
- kfree(pctldev);
- return NULL;
- }
- EXPORT_SYMBOL_GPL(pinctrl_register);
- /**
- * pinctrl_unregister() - unregister pinmux
- * @pctldev: pin controller to unregister
- *
- * Called by pinmux drivers to unregister a pinmux.
- */
- void pinctrl_unregister(struct pinctrl_dev *pctldev)
- {
- struct pinctrl_gpio_range *range, *n;
- if (pctldev == NULL)
- return;
- pinctrl_remove_device_debugfs(pctldev);
- mutex_lock(&pinctrl_mutex);
- if (!IS_ERR(pctldev->p))
- pinctrl_put_locked(pctldev->p, true);
- /* TODO: check that no pinmuxes are still active? */
- list_del(&pctldev->node);
- /* Destroy descriptor tree */
- pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
- pctldev->desc->npins);
- /* remove gpio ranges map */
- list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
- list_del(&range->node);
- kfree(pctldev);
- mutex_unlock(&pinctrl_mutex);
- }
- EXPORT_SYMBOL_GPL(pinctrl_unregister);
- static int __init pinctrl_init(void)
- {
- pr_info("initialized pinctrl subsystem\n");
- pinctrl_init_debugfs();
- return 0;
- }
- /* init early since many drivers really need to initialized pinmux early */
- core_initcall(pinctrl_init);
|