2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2017-03-22 10:26:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) EETS GmbH, 2017, Felix Brack <f.brack@eets.ch>
|
2021-04-11 07:39:49 +00:00
|
|
|
* Copyright (C) 2021 Dario Binacchi <dariobin@libero.it>
|
2017-03-22 10:26:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2017-05-17 23:18:03 +00:00
|
|
|
#include <dm.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <dm/device_compat.h>
|
2021-04-11 07:39:49 +00:00
|
|
|
#include <dm/devres.h>
|
2021-08-24 10:16:31 +00:00
|
|
|
#include <dm/of_access.h>
|
2017-03-22 10:26:44 +00:00
|
|
|
#include <dm/pinctrl.h>
|
2018-03-04 16:20:11 +00:00
|
|
|
#include <linux/libfdt.h>
|
2021-04-11 07:39:49 +00:00
|
|
|
#include <linux/list.h>
|
2017-03-22 10:26:44 +00:00
|
|
|
#include <asm/io.h>
|
2021-04-11 07:39:49 +00:00
|
|
|
#include <sort.h>
|
2017-03-22 10:26:44 +00:00
|
|
|
|
2021-04-11 07:39:39 +00:00
|
|
|
/**
|
|
|
|
* struct single_pdata - platform data
|
|
|
|
* @base: first configuration register
|
|
|
|
* @offset: index of last configuration register
|
|
|
|
* @mask: configuration-value mask bits
|
|
|
|
* @width: configuration register bit width
|
|
|
|
* @bits_per_mux: true if one register controls more than one pin
|
|
|
|
*/
|
2017-03-22 10:26:44 +00:00
|
|
|
struct single_pdata {
|
2021-04-11 07:39:39 +00:00
|
|
|
fdt_addr_t base;
|
|
|
|
int offset;
|
|
|
|
u32 mask;
|
2021-04-11 07:39:44 +00:00
|
|
|
u32 width;
|
2021-12-03 15:18:53 +00:00
|
|
|
u32 args_count;
|
2019-06-10 18:15:55 +00:00
|
|
|
bool bits_per_mux;
|
2017-03-22 10:26:44 +00:00
|
|
|
};
|
|
|
|
|
2021-04-11 07:39:49 +00:00
|
|
|
/**
|
|
|
|
* struct single_func - pinctrl function
|
|
|
|
* @node: list node
|
|
|
|
* @name: pinctrl function name
|
|
|
|
* @npins: number of entries in pins array
|
|
|
|
* @pins: pins array
|
|
|
|
*/
|
|
|
|
struct single_func {
|
|
|
|
struct list_head node;
|
|
|
|
const char *name;
|
|
|
|
unsigned int npins;
|
|
|
|
unsigned int *pins;
|
|
|
|
};
|
|
|
|
|
2021-08-24 10:16:31 +00:00
|
|
|
/**
|
|
|
|
* struct single_gpiofunc_range - pin ranges with same mux value of gpio fun
|
|
|
|
* @offset: offset base of pins
|
|
|
|
* @npins: number pins with the same mux value of gpio function
|
|
|
|
* @gpiofunc: mux value of gpio function
|
|
|
|
* @node: list node
|
|
|
|
*/
|
|
|
|
struct single_gpiofunc_range {
|
|
|
|
u32 offset;
|
|
|
|
u32 npins;
|
|
|
|
u32 gpiofunc;
|
|
|
|
struct list_head node;
|
|
|
|
};
|
|
|
|
|
2021-04-11 07:39:47 +00:00
|
|
|
/**
|
|
|
|
* struct single_priv - private data
|
|
|
|
* @bits_per_pin: number of bits per pin
|
|
|
|
* @npins: number of selectable pins
|
2021-04-11 07:39:48 +00:00
|
|
|
* @pin_name: temporary buffer to store the pin name
|
2021-08-24 10:16:31 +00:00
|
|
|
* @functions: list pin functions
|
|
|
|
* @gpiofuncs: list gpio functions
|
2021-04-11 07:39:47 +00:00
|
|
|
*/
|
|
|
|
struct single_priv {
|
2021-04-11 07:39:50 +00:00
|
|
|
#if (IS_ENABLED(CONFIG_SANDBOX))
|
|
|
|
u32 *sandbox_regs;
|
|
|
|
#endif
|
2021-04-11 07:39:47 +00:00
|
|
|
unsigned int bits_per_pin;
|
|
|
|
unsigned int npins;
|
2021-04-11 07:39:48 +00:00
|
|
|
char pin_name[PINNAME_SIZE];
|
2021-04-11 07:39:49 +00:00
|
|
|
struct list_head functions;
|
2021-08-24 10:16:31 +00:00
|
|
|
struct list_head gpiofuncs;
|
2021-04-11 07:39:47 +00:00
|
|
|
};
|
|
|
|
|
2021-04-11 07:39:39 +00:00
|
|
|
/**
|
|
|
|
* struct single_fdt_bits_cfg - pin configuration
|
|
|
|
*
|
|
|
|
* This structure is used for the pin configuration parameters in case
|
|
|
|
* the register controls more than one pin.
|
|
|
|
*
|
|
|
|
* @reg: configuration register offset
|
|
|
|
* @val: configuration register value
|
|
|
|
* @mask: configuration register mask
|
|
|
|
*/
|
2019-06-10 18:15:55 +00:00
|
|
|
struct single_fdt_bits_cfg {
|
2021-04-11 07:39:39 +00:00
|
|
|
fdt32_t reg;
|
|
|
|
fdt32_t val;
|
|
|
|
fdt32_t mask;
|
2019-06-10 18:15:55 +00:00
|
|
|
};
|
|
|
|
|
2021-04-11 07:39:50 +00:00
|
|
|
#if (!IS_ENABLED(CONFIG_SANDBOX))
|
|
|
|
|
2021-04-11 07:39:46 +00:00
|
|
|
static unsigned int single_read(struct udevice *dev, fdt_addr_t reg)
|
|
|
|
{
|
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
|
|
|
|
|
|
|
switch (pdata->width) {
|
|
|
|
case 8:
|
|
|
|
return readb(reg);
|
|
|
|
case 16:
|
|
|
|
return readw(reg);
|
|
|
|
default: /* 32 bits */
|
|
|
|
return readl(reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return readb(reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void single_write(struct udevice *dev, unsigned int val, fdt_addr_t reg)
|
|
|
|
{
|
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
|
|
|
|
|
|
|
switch (pdata->width) {
|
|
|
|
case 8:
|
|
|
|
writeb(val, reg);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
writew(val, reg);
|
|
|
|
break;
|
|
|
|
default: /* 32 bits */
|
|
|
|
writel(val, reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 07:39:50 +00:00
|
|
|
#else /* CONFIG_SANDBOX */
|
|
|
|
|
|
|
|
static unsigned int single_read(struct udevice *dev, fdt_addr_t reg)
|
|
|
|
{
|
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return priv->sandbox_regs[reg];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void single_write(struct udevice *dev, unsigned int val, fdt_addr_t reg)
|
|
|
|
{
|
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
priv->sandbox_regs[reg] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_SANDBOX */
|
|
|
|
|
2021-04-11 07:39:49 +00:00
|
|
|
/**
|
|
|
|
* single_get_pin_by_offset() - get a pin based on the register offset
|
|
|
|
* @dev: single driver instance
|
|
|
|
* @offset: register offset from the base
|
|
|
|
*/
|
|
|
|
static int single_get_pin_by_offset(struct udevice *dev, unsigned int offset)
|
|
|
|
{
|
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
if (offset > pdata->offset) {
|
|
|
|
dev_err(dev, "mux offset out of range: 0x%x (0x%x)\n",
|
|
|
|
offset, pdata->offset);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pdata->bits_per_mux)
|
|
|
|
return (offset * BITS_PER_BYTE) / priv->bits_per_pin;
|
|
|
|
|
|
|
|
return offset / (pdata->width / BITS_PER_BYTE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int single_get_offset_by_pin(struct udevice *dev, unsigned int pin)
|
|
|
|
{
|
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
|
|
|
unsigned int mux_bytes;
|
|
|
|
|
|
|
|
if (pin >= priv->npins)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
mux_bytes = pdata->width / BITS_PER_BYTE;
|
|
|
|
if (pdata->bits_per_mux) {
|
|
|
|
int byte_num;
|
|
|
|
|
|
|
|
byte_num = (priv->bits_per_pin * pin) / BITS_PER_BYTE;
|
|
|
|
return (byte_num / mux_bytes) * mux_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pin * mux_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *single_get_pin_function(struct udevice *dev,
|
|
|
|
unsigned int pin)
|
|
|
|
{
|
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
|
|
|
struct single_func *func;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
list_for_each_entry(func, &priv->functions, node) {
|
|
|
|
for (i = 0; i < func->npins; i++) {
|
|
|
|
if (pin == func->pins[i])
|
|
|
|
return func->name;
|
|
|
|
|
|
|
|
if (pin < func->pins[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int single_get_pin_muxing(struct udevice *dev, unsigned int pin,
|
|
|
|
char *buf, int size)
|
|
|
|
{
|
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
|
|
|
fdt_addr_t reg;
|
|
|
|
const char *fname;
|
|
|
|
unsigned int val;
|
|
|
|
int offset, pin_shift = 0;
|
|
|
|
|
|
|
|
offset = single_get_offset_by_pin(dev, pin);
|
|
|
|
if (offset < 0)
|
|
|
|
return offset;
|
|
|
|
|
|
|
|
reg = pdata->base + offset;
|
|
|
|
val = single_read(dev, reg);
|
|
|
|
|
|
|
|
if (pdata->bits_per_mux)
|
|
|
|
pin_shift = pin % (pdata->width / priv->bits_per_pin) *
|
|
|
|
priv->bits_per_pin;
|
|
|
|
|
|
|
|
val &= (pdata->mask << pin_shift);
|
|
|
|
fname = single_get_pin_function(dev, pin);
|
|
|
|
snprintf(buf, size, "%pa 0x%08x %s", ®, val,
|
|
|
|
fname ? fname : "UNCLAIMED");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-24 10:16:32 +00:00
|
|
|
static int single_request(struct udevice *dev, int pin, int flags)
|
|
|
|
{
|
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
|
|
|
struct single_gpiofunc_range *frange = NULL;
|
|
|
|
struct list_head *pos, *tmp;
|
|
|
|
phys_addr_t reg;
|
|
|
|
int mux_bytes = 0;
|
|
|
|
u32 data;
|
|
|
|
|
|
|
|
/* If function mask is null, needn't enable it. */
|
|
|
|
if (!pdata->mask)
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
|
|
|
list_for_each_safe(pos, tmp, &priv->gpiofuncs) {
|
|
|
|
frange = list_entry(pos, struct single_gpiofunc_range, node);
|
|
|
|
if ((pin >= frange->offset + frange->npins) ||
|
|
|
|
pin < frange->offset)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
mux_bytes = pdata->width / BITS_PER_BYTE;
|
|
|
|
reg = pdata->base + pin * mux_bytes;
|
|
|
|
|
|
|
|
data = single_read(dev, reg);
|
|
|
|
data &= ~pdata->mask;
|
|
|
|
data |= frange->gpiofunc;
|
|
|
|
single_write(dev, data, reg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-11 07:39:49 +00:00
|
|
|
static struct single_func *single_allocate_function(struct udevice *dev,
|
|
|
|
unsigned int group_pins)
|
|
|
|
{
|
|
|
|
struct single_func *func;
|
|
|
|
|
|
|
|
func = devm_kmalloc(dev, sizeof(*func), GFP_KERNEL);
|
|
|
|
if (!func)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
func->pins = devm_kmalloc(dev, sizeof(unsigned int) * group_pins,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!func->pins)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int single_pin_compare(const void *s1, const void *s2)
|
|
|
|
{
|
|
|
|
int pin1 = *(const unsigned int *)s1;
|
|
|
|
int pin2 = *(const unsigned int *)s2;
|
|
|
|
|
|
|
|
return pin1 - pin2;
|
|
|
|
}
|
|
|
|
|
2017-03-22 10:26:44 +00:00
|
|
|
/**
|
|
|
|
* single_configure_pins() - Configure pins based on FDT data
|
|
|
|
*
|
|
|
|
* @dev: Pointer to single pin configuration device which is the parent of
|
|
|
|
* the pins node holding the pin configuration data.
|
|
|
|
* @pins: Pointer to the first element of an array of register/value pairs
|
2021-12-03 15:18:53 +00:00
|
|
|
* of type 'u32'. Each such pair describes the pin to be configured
|
|
|
|
* and the value to be used for configuration.
|
|
|
|
* The value can either be a simple value if #pinctrl-cells = 1
|
|
|
|
* or a configuration value and a pin mux mode value if it is 2
|
2017-03-22 10:26:44 +00:00
|
|
|
* This pointer points to a 'pinctrl-single,pins' property in the
|
|
|
|
* device-tree.
|
|
|
|
* @size: Size of the 'pins' array in bytes.
|
2021-12-03 15:18:53 +00:00
|
|
|
* The number of cells in the array therefore equals to
|
|
|
|
* 'size / sizeof(u32)'.
|
2021-04-11 07:39:49 +00:00
|
|
|
* @fname: Function name.
|
2017-03-22 10:26:44 +00:00
|
|
|
*/
|
|
|
|
static int single_configure_pins(struct udevice *dev,
|
2021-12-03 15:18:53 +00:00
|
|
|
const u32 *pins,
|
2021-04-11 07:39:49 +00:00
|
|
|
int size, const char *fname)
|
2017-03-22 10:26:44 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
2021-04-11 07:39:49 +00:00
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
2021-12-03 15:18:53 +00:00
|
|
|
int stride = pdata->args_count + 1;
|
|
|
|
int n, pin, count = size / sizeof(u32);
|
2021-04-11 07:39:49 +00:00
|
|
|
struct single_func *func;
|
2021-04-11 07:39:40 +00:00
|
|
|
phys_addr_t reg;
|
2021-12-03 15:18:53 +00:00
|
|
|
u32 offset, val, mux;
|
2017-03-22 10:26:44 +00:00
|
|
|
|
2021-04-11 07:39:45 +00:00
|
|
|
/* If function mask is null, needn't enable it. */
|
|
|
|
if (!pdata->mask)
|
|
|
|
return 0;
|
|
|
|
|
2021-04-11 07:39:49 +00:00
|
|
|
func = single_allocate_function(dev, count);
|
|
|
|
if (IS_ERR(func))
|
|
|
|
return PTR_ERR(func);
|
|
|
|
|
|
|
|
func->name = fname;
|
|
|
|
func->npins = 0;
|
2021-12-03 15:18:53 +00:00
|
|
|
for (n = 0; n < count; n += stride) {
|
|
|
|
offset = fdt32_to_cpu(pins[n]);
|
2021-04-22 20:28:56 +00:00
|
|
|
if (offset > pdata->offset) {
|
2021-04-11 07:39:49 +00:00
|
|
|
dev_err(dev, " invalid register offset 0x%x\n",
|
2021-04-11 07:39:41 +00:00
|
|
|
offset);
|
2017-03-22 10:26:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-11 07:39:41 +00:00
|
|
|
|
2021-12-03 15:18:53 +00:00
|
|
|
/* if the pinctrl-cells is 2 then the second cell contains the mux */
|
|
|
|
if (stride == 3)
|
|
|
|
mux = fdt32_to_cpu(pins[n + 2]);
|
|
|
|
else
|
|
|
|
mux = 0;
|
|
|
|
|
2021-04-11 07:39:41 +00:00
|
|
|
reg = pdata->base + offset;
|
2021-12-03 15:18:53 +00:00
|
|
|
val = (fdt32_to_cpu(pins[n + 1]) | mux) & pdata->mask;
|
2021-04-11 07:39:49 +00:00
|
|
|
pin = single_get_pin_by_offset(dev, offset);
|
|
|
|
if (pin < 0) {
|
|
|
|
dev_err(dev, " failed to get pin by offset %x\n",
|
|
|
|
offset);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-04-11 07:39:46 +00:00
|
|
|
single_write(dev, (single_read(dev, reg) & ~pdata->mask) | val,
|
|
|
|
reg);
|
2021-04-11 07:39:42 +00:00
|
|
|
dev_dbg(dev, " reg/val %pa/0x%08x\n", ®, val);
|
2021-04-11 07:39:49 +00:00
|
|
|
func->pins[func->npins] = pin;
|
|
|
|
func->npins++;
|
2017-03-22 10:26:44 +00:00
|
|
|
}
|
2021-04-11 07:39:49 +00:00
|
|
|
|
|
|
|
qsort(func->pins, func->npins, sizeof(func->pins[0]),
|
|
|
|
single_pin_compare);
|
|
|
|
list_add(&func->node, &priv->functions);
|
2017-03-22 10:26:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-10 18:15:55 +00:00
|
|
|
static int single_configure_bits(struct udevice *dev,
|
|
|
|
const struct single_fdt_bits_cfg *pins,
|
2021-04-11 07:39:49 +00:00
|
|
|
int size, const char *fname)
|
2019-06-10 18:15:55 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
2021-04-11 07:39:49 +00:00
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
|
|
|
int n, pin, count = size / sizeof(struct single_fdt_bits_cfg);
|
|
|
|
int npins_in_reg, pin_num_from_lsb;
|
|
|
|
struct single_func *func;
|
2021-04-11 07:39:40 +00:00
|
|
|
phys_addr_t reg;
|
2021-04-11 07:39:49 +00:00
|
|
|
u32 offset, val, mask, bit_pos, val_pos, mask_pos, submask;
|
|
|
|
|
2021-04-22 16:35:58 +00:00
|
|
|
/* If function mask is null, needn't enable it. */
|
|
|
|
if (!pdata->mask)
|
|
|
|
return 0;
|
|
|
|
|
2021-04-11 07:39:49 +00:00
|
|
|
npins_in_reg = pdata->width / priv->bits_per_pin;
|
|
|
|
func = single_allocate_function(dev, count * npins_in_reg);
|
|
|
|
if (IS_ERR(func))
|
|
|
|
return PTR_ERR(func);
|
2019-06-10 18:15:55 +00:00
|
|
|
|
2021-04-11 07:39:49 +00:00
|
|
|
func->name = fname;
|
|
|
|
func->npins = 0;
|
2019-06-10 18:15:55 +00:00
|
|
|
for (n = 0; n < count; n++, pins++) {
|
2021-04-11 07:39:41 +00:00
|
|
|
offset = fdt32_to_cpu(pins->reg);
|
2021-04-22 20:28:56 +00:00
|
|
|
if (offset > pdata->offset) {
|
2021-04-11 07:39:41 +00:00
|
|
|
dev_dbg(dev, " invalid register offset 0x%x\n",
|
|
|
|
offset);
|
2019-06-10 18:15:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-11 07:39:41 +00:00
|
|
|
|
|
|
|
reg = pdata->base + offset;
|
2019-06-10 18:15:55 +00:00
|
|
|
|
2021-04-11 07:39:49 +00:00
|
|
|
pin = single_get_pin_by_offset(dev, offset);
|
|
|
|
if (pin < 0) {
|
|
|
|
dev_err(dev, " failed to get pin by offset 0x%pa\n",
|
|
|
|
®);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-10 18:15:55 +00:00
|
|
|
mask = fdt32_to_cpu(pins->mask);
|
|
|
|
val = fdt32_to_cpu(pins->val) & mask;
|
2021-04-11 07:39:46 +00:00
|
|
|
single_write(dev, (single_read(dev, reg) & ~mask) | val, reg);
|
2021-04-11 07:39:42 +00:00
|
|
|
dev_dbg(dev, " reg/val %pa/0x%08x\n", ®, val);
|
2021-04-11 07:39:49 +00:00
|
|
|
|
|
|
|
while (mask) {
|
|
|
|
bit_pos = __ffs(mask);
|
|
|
|
pin_num_from_lsb = bit_pos / priv->bits_per_pin;
|
|
|
|
mask_pos = pdata->mask << bit_pos;
|
|
|
|
val_pos = val & mask_pos;
|
|
|
|
submask = mask & mask_pos;
|
|
|
|
|
|
|
|
if ((mask & mask_pos) == 0) {
|
|
|
|
dev_err(dev, "Invalid mask at 0x%x\n", offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mask &= ~mask_pos;
|
|
|
|
|
|
|
|
if (submask != mask_pos) {
|
|
|
|
dev_warn(dev,
|
|
|
|
"Invalid submask 0x%x at 0x%x\n",
|
|
|
|
submask, offset);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
func->pins[func->npins] = pin + pin_num_from_lsb;
|
|
|
|
func->npins++;
|
|
|
|
}
|
2019-06-10 18:15:55 +00:00
|
|
|
}
|
2021-04-11 07:39:49 +00:00
|
|
|
|
|
|
|
qsort(func->pins, func->npins, sizeof(func->pins[0]),
|
|
|
|
single_pin_compare);
|
|
|
|
list_add(&func->node, &priv->functions);
|
2019-06-10 18:15:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-03-22 10:26:44 +00:00
|
|
|
static int single_set_state(struct udevice *dev,
|
|
|
|
struct udevice *config)
|
|
|
|
{
|
2021-12-03 15:18:53 +00:00
|
|
|
const u32 *prop;
|
2019-06-10 18:15:55 +00:00
|
|
|
const struct single_fdt_bits_cfg *prop_bits;
|
2017-03-22 10:26:44 +00:00
|
|
|
int len;
|
|
|
|
|
2020-04-22 17:25:31 +00:00
|
|
|
prop = dev_read_prop(config, "pinctrl-single,pins", &len);
|
2019-06-10 18:15:55 +00:00
|
|
|
|
2017-03-22 10:26:44 +00:00
|
|
|
if (prop) {
|
|
|
|
dev_dbg(dev, "configuring pins for %s\n", config->name);
|
2021-12-03 15:18:53 +00:00
|
|
|
if (len % sizeof(u32)) {
|
2017-03-22 10:26:44 +00:00
|
|
|
dev_dbg(dev, " invalid pin configuration in fdt\n");
|
|
|
|
return -FDT_ERR_BADSTRUCTURE;
|
|
|
|
}
|
2021-04-11 07:39:49 +00:00
|
|
|
single_configure_pins(dev, prop, len, config->name);
|
2019-06-10 18:15:55 +00:00
|
|
|
return 0;
|
2017-03-22 10:26:44 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 18:15:55 +00:00
|
|
|
/* pinctrl-single,pins not found so check for pinctrl-single,bits */
|
2020-04-22 17:25:31 +00:00
|
|
|
prop_bits = dev_read_prop(config, "pinctrl-single,bits", &len);
|
2019-06-10 18:15:55 +00:00
|
|
|
if (prop_bits) {
|
|
|
|
dev_dbg(dev, "configuring pins for %s\n", config->name);
|
|
|
|
if (len % sizeof(struct single_fdt_bits_cfg)) {
|
|
|
|
dev_dbg(dev, " invalid bits configuration in fdt\n");
|
|
|
|
return -FDT_ERR_BADSTRUCTURE;
|
|
|
|
}
|
2021-04-11 07:39:49 +00:00
|
|
|
single_configure_bits(dev, prop_bits, len, config->name);
|
2019-06-10 18:15:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Neither 'pinctrl-single,pins' nor 'pinctrl-single,bits' were found */
|
2017-03-22 10:26:44 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2021-04-11 07:39:48 +00:00
|
|
|
static const char *single_get_pin_name(struct udevice *dev,
|
|
|
|
unsigned int selector)
|
|
|
|
{
|
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
if (selector >= priv->npins)
|
|
|
|
snprintf(priv->pin_name, PINNAME_SIZE, "Error");
|
|
|
|
else
|
|
|
|
snprintf(priv->pin_name, PINNAME_SIZE, "PIN%u", selector);
|
|
|
|
|
|
|
|
return priv->pin_name;
|
|
|
|
}
|
|
|
|
|
2021-04-11 07:39:47 +00:00
|
|
|
static int single_get_pins_count(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return priv->npins;
|
|
|
|
}
|
|
|
|
|
2021-08-24 10:16:31 +00:00
|
|
|
static int single_add_gpio_func(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
|
|
|
const char *propname = "pinctrl-single,gpio-range";
|
|
|
|
const char *cellname = "#pinctrl-single,gpio-range-cells";
|
|
|
|
struct single_gpiofunc_range *range;
|
|
|
|
struct ofnode_phandle_args gpiospec;
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), propname,
|
|
|
|
cellname, 0, i, &gpiospec);
|
|
|
|
/* Do not treat it as error. Only treat it as end condition. */
|
|
|
|
if (ret) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
range = devm_kzalloc(dev, sizeof(*range), GFP_KERNEL);
|
|
|
|
if (!range) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
range->offset = gpiospec.args[0];
|
|
|
|
range->npins = gpiospec.args[1];
|
|
|
|
range->gpiofunc = gpiospec.args[2];
|
|
|
|
list_add_tail(&range->node, &priv->gpiofuncs);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-11 07:39:47 +00:00
|
|
|
static int single_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
|
|
|
struct single_priv *priv = dev_get_priv(dev);
|
|
|
|
u32 size;
|
|
|
|
|
2021-04-11 07:39:49 +00:00
|
|
|
INIT_LIST_HEAD(&priv->functions);
|
2021-08-24 10:16:31 +00:00
|
|
|
INIT_LIST_HEAD(&priv->gpiofuncs);
|
2021-04-11 07:39:49 +00:00
|
|
|
|
2021-04-11 07:39:47 +00:00
|
|
|
size = pdata->offset + pdata->width / BITS_PER_BYTE;
|
2021-04-11 07:39:50 +00:00
|
|
|
#if (CONFIG_IS_ENABLED(SANDBOX))
|
|
|
|
priv->sandbox_regs =
|
|
|
|
devm_kzalloc(dev, size * sizeof(*priv->sandbox_regs),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!priv->sandbox_regs)
|
|
|
|
return -ENOMEM;
|
|
|
|
#endif
|
|
|
|
|
2021-05-14 01:39:28 +00:00
|
|
|
/* looks like a possible divide by 0, but data->width avoids this */
|
2021-04-11 07:39:47 +00:00
|
|
|
priv->npins = size / (pdata->width / BITS_PER_BYTE);
|
|
|
|
if (pdata->bits_per_mux) {
|
2021-04-22 16:35:58 +00:00
|
|
|
if (!pdata->mask) {
|
|
|
|
dev_err(dev, "function mask needs to be non-zero\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-04-11 07:39:47 +00:00
|
|
|
priv->bits_per_pin = fls(pdata->mask);
|
|
|
|
priv->npins *= (pdata->width / priv->bits_per_pin);
|
|
|
|
}
|
|
|
|
|
2021-08-24 10:16:31 +00:00
|
|
|
if (single_add_gpio_func(dev))
|
|
|
|
dev_dbg(dev, "gpio functions are not added\n");
|
|
|
|
|
2021-04-11 07:39:47 +00:00
|
|
|
dev_dbg(dev, "%d pins\n", priv->npins);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int single_of_to_plat(struct udevice *dev)
|
2017-03-22 10:26:44 +00:00
|
|
|
{
|
|
|
|
fdt_addr_t addr;
|
2021-04-11 07:39:43 +00:00
|
|
|
fdt_size_t size;
|
2020-12-23 02:30:28 +00:00
|
|
|
struct single_pdata *pdata = dev_get_plat(dev);
|
2021-04-11 07:39:44 +00:00
|
|
|
int ret;
|
2017-03-22 10:26:44 +00:00
|
|
|
|
2021-04-11 07:39:44 +00:00
|
|
|
ret = dev_read_u32(dev, "pinctrl-single,register-width", &pdata->width);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "missing register width\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-22 10:26:44 +00:00
|
|
|
|
2021-04-11 07:39:46 +00:00
|
|
|
switch (pdata->width) {
|
|
|
|
case 8:
|
|
|
|
case 16:
|
|
|
|
case 32:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err(dev, "wrong register width\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:10:34 +00:00
|
|
|
addr = dev_read_addr_size_index(dev, 0, &size);
|
2021-04-11 07:39:43 +00:00
|
|
|
if (addr == FDT_ADDR_T_NONE) {
|
2021-05-07 09:10:34 +00:00
|
|
|
dev_err(dev, "failed to get base register address\n");
|
2021-04-11 07:39:43 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pdata->offset = size - pdata->width / BITS_PER_BYTE;
|
2017-03-22 10:26:44 +00:00
|
|
|
pdata->base = addr;
|
|
|
|
|
2021-04-11 07:39:45 +00:00
|
|
|
ret = dev_read_u32(dev, "pinctrl-single,function-mask", &pdata->mask);
|
|
|
|
if (ret) {
|
|
|
|
pdata->mask = 0;
|
|
|
|
dev_warn(dev, "missing function register mask\n");
|
|
|
|
}
|
|
|
|
|
2020-01-13 10:34:55 +00:00
|
|
|
pdata->bits_per_mux = dev_read_bool(dev, "pinctrl-single,bit-per-mux");
|
2019-06-10 18:15:55 +00:00
|
|
|
|
2021-12-03 15:18:53 +00:00
|
|
|
/* If no pinctrl-cells is present, default to old style of 2 cells with
|
|
|
|
* bits per mux and 1 cell otherwise.
|
|
|
|
*/
|
|
|
|
ret = dev_read_u32(dev, "#pinctrl-cells", &pdata->args_count);
|
|
|
|
if (ret)
|
|
|
|
pdata->args_count = pdata->bits_per_mux ? 2 : 1;
|
|
|
|
|
2017-03-22 10:26:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct pinctrl_ops single_pinctrl_ops = {
|
2021-04-11 07:39:47 +00:00
|
|
|
.get_pins_count = single_get_pins_count,
|
2021-04-11 07:39:48 +00:00
|
|
|
.get_pin_name = single_get_pin_name,
|
2017-03-22 10:26:44 +00:00
|
|
|
.set_state = single_set_state,
|
2021-04-11 07:39:49 +00:00
|
|
|
.get_pin_muxing = single_get_pin_muxing,
|
2021-08-24 10:16:32 +00:00
|
|
|
.request = single_request,
|
2017-03-22 10:26:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id single_pinctrl_match[] = {
|
|
|
|
{ .compatible = "pinctrl-single" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(single_pinctrl) = {
|
|
|
|
.name = "single-pinctrl",
|
|
|
|
.id = UCLASS_PINCTRL,
|
|
|
|
.of_match = single_pinctrl_match,
|
|
|
|
.ops = &single_pinctrl_ops,
|
2020-12-03 23:55:18 +00:00
|
|
|
.plat_auto = sizeof(struct single_pdata),
|
2021-04-11 07:39:47 +00:00
|
|
|
.priv_auto = sizeof(struct single_priv),
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = single_of_to_plat,
|
2021-04-11 07:39:47 +00:00
|
|
|
.probe = single_probe,
|
2017-03-22 10:26:44 +00:00
|
|
|
};
|