forked from Minki/linux
ae6b4d8588
This add per-pin and per-group pin config interfaces for biasing, driving and other such electronic properties. The details of passed configurations are passed in an opaque unsigned long which may be dereferences to integer types, structs or lists on either side of the configuration interface. ChangeLog v1->v2: - Clear split of terminology: we now have pin controllers, and those may support two interfaces using vtables: pin multiplexing and pin configuration. - Break out pin configuration to its own C file, controllers may implement only config without mux, and vice versa, so keep each sub-functionality of pin controllers separate. Introduce CONFIG_PINCONF in Kconfig. - Implement some core logic around pin configuration in the pinconf.c file. - Remove UNKNOWN config states, these were just surplus baggage. - Remove FLOAT config state - HIGH_IMPEDANCE should be enough for everyone. - PIN_CONFIG_POWER_SOURCE added to handle switching the power supply for the pin logic between different sources - Explicit DISABLE config enums to turn schmitt-trigger, wakeup etc OFF. - Update documentation to reflect all the recent reasoning. ChangeLog v2->v3: - Twist API around to pass around arrays of config tuples instead of (param, value) pairs everywhere. - Explicit drive strength semantics for push/pull and similar drive modes, this shall be the number of drive stages vs nominal load impedance, which should match the actual electronics used in push/pull CMOS or TTY totempoles. - Drop load capacitance configuration - I probably don't know what I'm doing here so leave it out. - Drop PIN_CONFIG_INPUT_SCHMITT_OFF, instead the argument zero to PIN_CONFIG_INPUT_SCHMITT turns schmitt trigger off. - Drop PIN_CONFIG_NORMAL_POWER_MODE and have a well defined argument to PIN_CONFIG_LOW_POWER_MODE to get out of it instead. - Drop PIN_CONFIG_WAKEUP_ENABLE/DISABLE and just use PIN_CONFIG_WAKEUP with defined value zero to turn wakeup off. - Add PIN_CONFIG_INPUT_DEBOUNCE for configuring debounce time on input lines. - Fix a bug when we tried to configure pins for pin controllers without pinconf support. - Initialized debugfs properly so it works. - Initialize the mutex properly and lock around config tampering sections. - Check the return value from get_initial_config() properly. ChangeLog v3->v4: - Export the pin_config_get(), pin_config_set() and pin_config_group() functions. - Drop the entire concept of just getting initial config and keeping track of pin states internally, instead ask the pins what state they are in. Previous idea was plain wrong, if the device cannot keep track of its state, the driver should do it. - Drop the generic configuration layout, it seems this impose too much restriction on some pin controllers, so let them do things the way they want and split off support for generic config as an optional add-on. ChangeLog v4->v5: - Introduce two symmetric driver calls for group configuration, .pin_config_group_[get|set] and corresponding external calls. - Remove generic semantic meanings of return values from config calls, these belong in the generic config patch. Just pass the return value through instead. - Add a debugfs entry "pinconf-groups" to read status from group configuration only, also slam in a per-group debug callback in the pinconf_ops so custom drivers can display something meaningful for their pins. - Fix some dangling newline. - Drop dangling #else clause. - Update documentation to match the above. ChangeLog v5->v6: - Change to using a pin name as parameter for the [get|set]_config() functions, as suggested by Stephen Warren. This is more natural as names will be what a developer has access to in written documentation etc. ChangeLog v6->v7: - Refactor out by-pin and by-name get/set functions, only expose the by-name functions externally, expose the by-pin functions internally. - Show supported pin control functionality in the debugfs pinctrl-devices file. Acked-by: Stephen Warren <swarren@nvidia.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
303 lines
7.7 KiB
C
303 lines
7.7 KiB
C
/*
|
|
* Core driver for the pin config portions of the pin control subsystem
|
|
*
|
|
* Copyright (C) 2011 ST-Ericsson SA
|
|
* Written on behalf of Linaro for ST-Ericsson
|
|
*
|
|
* Author: Linus Walleij <linus.walleij@linaro.org>
|
|
*
|
|
* License terms: GNU General Public License (GPL) version 2
|
|
*/
|
|
#define pr_fmt(fmt) "pinconfig core: " fmt
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/init.h>
|
|
#include <linux/device.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/debugfs.h>
|
|
#include <linux/seq_file.h>
|
|
#include <linux/pinctrl/machine.h>
|
|
#include <linux/pinctrl/pinctrl.h>
|
|
#include <linux/pinctrl/pinconf.h>
|
|
#include "core.h"
|
|
#include "pinconf.h"
|
|
|
|
int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
|
|
unsigned long *config)
|
|
{
|
|
const struct pinconf_ops *ops = pctldev->desc->confops;
|
|
|
|
if (!ops || !ops->pin_config_get) {
|
|
dev_err(&pctldev->dev, "cannot get pin configuration, missing "
|
|
"pin_config_get() function in driver\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
return ops->pin_config_get(pctldev, pin, config);
|
|
}
|
|
|
|
/**
|
|
* pin_config_get() - get the configuration of a single pin parameter
|
|
* @pctldev: pin controller device for this pin
|
|
* @name: name of the pin to get the config for
|
|
* @config: the config pointed to by this argument will be filled in with the
|
|
* current pin state, it can be used directly by drivers as a numeral, or
|
|
* it can be dereferenced to any struct.
|
|
*/
|
|
int pin_config_get(struct pinctrl_dev *pctldev, const char *name,
|
|
unsigned long *config)
|
|
{
|
|
int pin;
|
|
|
|
pin = pin_get_from_name(pctldev, name);
|
|
if (pin < 0)
|
|
return pin;
|
|
|
|
return pin_config_get_for_pin(pctldev, pin, config);
|
|
}
|
|
EXPORT_SYMBOL(pin_config_get);
|
|
|
|
int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
|
|
unsigned long config)
|
|
{
|
|
const struct pinconf_ops *ops = pctldev->desc->confops;
|
|
int ret;
|
|
|
|
if (!ops || !ops->pin_config_set) {
|
|
dev_err(&pctldev->dev, "cannot configure pin, missing "
|
|
"config function in driver\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
ret = ops->pin_config_set(pctldev, pin, config);
|
|
if (ret) {
|
|
dev_err(&pctldev->dev,
|
|
"unable to set pin configuration on pin %d\n", pin);
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* pin_config_set() - set the configuration of a single pin parameter
|
|
* @pctldev: pin controller device for this pin
|
|
* @name: name of the pin to set the config for
|
|
* @config: the config in this argument will contain the desired pin state, it
|
|
* can be used directly by drivers as a numeral, or it can be dereferenced
|
|
* to any struct.
|
|
*/
|
|
int pin_config_set(struct pinctrl_dev *pctldev, const char *name,
|
|
unsigned long config)
|
|
{
|
|
int pin;
|
|
|
|
pin = pin_get_from_name(pctldev, name);
|
|
if (pin < 0)
|
|
return pin;
|
|
|
|
return pin_config_set_for_pin(pctldev, pin, config);
|
|
}
|
|
EXPORT_SYMBOL(pin_config_set);
|
|
|
|
int pin_config_group_get(struct pinctrl_dev *pctldev, const char *pin_group,
|
|
unsigned long *config)
|
|
{
|
|
const struct pinconf_ops *ops = pctldev->desc->confops;
|
|
int selector;
|
|
|
|
if (!ops || !ops->pin_config_group_get) {
|
|
dev_err(&pctldev->dev, "cannot get configuration for pin "
|
|
"group, missing group config get function in "
|
|
"driver\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
selector = pinctrl_get_group_selector(pctldev, pin_group);
|
|
if (selector < 0)
|
|
return selector;
|
|
|
|
return ops->pin_config_group_get(pctldev, selector, config);
|
|
}
|
|
EXPORT_SYMBOL(pin_config_group_get);
|
|
|
|
|
|
int pin_config_group_set(struct pinctrl_dev *pctldev, const char *pin_group,
|
|
unsigned long config)
|
|
{
|
|
const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
|
|
const struct pinconf_ops *ops = pctldev->desc->confops;
|
|
int selector;
|
|
const unsigned *pins;
|
|
unsigned num_pins;
|
|
int ret;
|
|
int i;
|
|
|
|
if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
|
|
dev_err(&pctldev->dev, "cannot configure pin group, missing "
|
|
"config function in driver\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
selector = pinctrl_get_group_selector(pctldev, pin_group);
|
|
if (selector < 0)
|
|
return selector;
|
|
|
|
ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
|
|
if (ret) {
|
|
dev_err(&pctldev->dev, "cannot configure pin group, error "
|
|
"getting pins\n");
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* If the pin controller supports handling entire groups we use that
|
|
* capability.
|
|
*/
|
|
if (ops->pin_config_group_set) {
|
|
ret = ops->pin_config_group_set(pctldev, selector, config);
|
|
/*
|
|
* If the pin controller prefer that a certain group be handled
|
|
* pin-by-pin as well, it returns -EAGAIN.
|
|
*/
|
|
if (ret != -EAGAIN)
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* If the controller cannot handle entire groups, we configure each pin
|
|
* individually.
|
|
*/
|
|
if (!ops->pin_config_set)
|
|
return 0;
|
|
|
|
for (i = 0; i < num_pins; i++) {
|
|
ret = ops->pin_config_set(pctldev, pins[i], config);
|
|
if (ret < 0)
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(pin_config_group_set);
|
|
|
|
int pinconf_check_ops(const struct pinconf_ops *ops)
|
|
{
|
|
/* We must be able to read out pin status */
|
|
if (!ops->pin_config_get && !ops->pin_config_group_get)
|
|
return -EINVAL;
|
|
/* We have to be able to config the pins in SOME way */
|
|
if (!ops->pin_config_set && !ops->pin_config_group_set)
|
|
return -EINVAL;
|
|
return 0;
|
|
}
|
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
|
|
static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
|
|
struct seq_file *s, int pin)
|
|
{
|
|
const struct pinconf_ops *ops = pctldev->desc->confops;
|
|
|
|
if (ops && ops->pin_config_dbg_show)
|
|
ops->pin_config_dbg_show(pctldev, s, pin);
|
|
}
|
|
|
|
static int pinconf_pins_show(struct seq_file *s, void *what)
|
|
{
|
|
struct pinctrl_dev *pctldev = s->private;
|
|
unsigned pin;
|
|
|
|
seq_puts(s, "Pin config settings per pin\n");
|
|
seq_puts(s, "Format: pin (name): pinmux setting array\n");
|
|
|
|
/* The highest pin number need to be included in the loop, thus <= */
|
|
for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
|
|
struct pin_desc *desc;
|
|
|
|
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");
|
|
|
|
pinconf_dump_pin(pctldev, s, pin);
|
|
|
|
seq_printf(s, "\n");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void pinconf_dump_group(struct pinctrl_dev *pctldev,
|
|
struct seq_file *s, unsigned selector,
|
|
const char *gname)
|
|
{
|
|
const struct pinconf_ops *ops = pctldev->desc->confops;
|
|
|
|
if (ops && ops->pin_config_group_dbg_show)
|
|
ops->pin_config_group_dbg_show(pctldev, s, selector);
|
|
}
|
|
|
|
static int pinconf_groups_show(struct seq_file *s, void *what)
|
|
{
|
|
struct pinctrl_dev *pctldev = s->private;
|
|
const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
|
|
const struct pinconf_ops *ops = pctldev->desc->confops;
|
|
unsigned selector = 0;
|
|
|
|
if (!ops || !ops->pin_config_group_get)
|
|
return 0;
|
|
|
|
seq_puts(s, "Pin config settings per pin group\n");
|
|
seq_puts(s, "Format: group (name): pinmux setting array\n");
|
|
|
|
while (pctlops->list_groups(pctldev, selector) >= 0) {
|
|
const char *gname = pctlops->get_group_name(pctldev, selector);
|
|
|
|
seq_printf(s, "%u (%s):", selector, gname);
|
|
pinconf_dump_group(pctldev, s, selector, gname);
|
|
selector++;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int pinconf_pins_open(struct inode *inode, struct file *file)
|
|
{
|
|
return single_open(file, pinconf_pins_show, inode->i_private);
|
|
}
|
|
|
|
static int pinconf_groups_open(struct inode *inode, struct file *file)
|
|
{
|
|
return single_open(file, pinconf_groups_show, inode->i_private);
|
|
}
|
|
|
|
static const struct file_operations pinconf_pins_ops = {
|
|
.open = pinconf_pins_open,
|
|
.read = seq_read,
|
|
.llseek = seq_lseek,
|
|
.release = single_release,
|
|
};
|
|
|
|
static const struct file_operations pinconf_groups_ops = {
|
|
.open = pinconf_groups_open,
|
|
.read = seq_read,
|
|
.llseek = seq_lseek,
|
|
.release = single_release,
|
|
};
|
|
|
|
void pinconf_init_device_debugfs(struct dentry *devroot,
|
|
struct pinctrl_dev *pctldev)
|
|
{
|
|
debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
|
|
devroot, pctldev, &pinconf_pins_ops);
|
|
debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
|
|
devroot, pctldev, &pinconf_groups_ops);
|
|
}
|
|
|
|
#endif
|