2018-06-08 15:16:31 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2018-03-14 23:22:35 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 John Crispin <blogic@openwrt.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/pinctrl/pinctrl.h>
|
|
|
|
#include <linux/pinctrl/pinconf.h>
|
2019-01-04 14:34:08 +03:00
|
|
|
#include <linux/pinctrl/pinconf-generic.h>
|
2018-03-14 23:22:35 +03:00
|
|
|
#include <linux/pinctrl/pinmux.h>
|
|
|
|
#include <linux/pinctrl/consumer.h>
|
|
|
|
#include <linux/pinctrl/machine.h>
|
|
|
|
|
|
|
|
#include <asm/mach-ralink/ralink_regs.h>
|
|
|
|
#include <asm/mach-ralink/mt7620.h>
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
#include "pinctrl-ralink.h"
|
2020-12-08 10:55:23 +03:00
|
|
|
#include "../core.h"
|
|
|
|
#include "../pinctrl-utils.h"
|
2018-03-14 23:22:35 +03:00
|
|
|
|
|
|
|
#define SYSC_REG_GPIO_MODE 0x60
|
|
|
|
#define SYSC_REG_GPIO_MODE2 0x64
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_priv {
|
2018-03-14 23:22:35 +03:00
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
struct pinctrl_pin_desc *pads;
|
|
|
|
struct pinctrl_desc *desc;
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_pmx_func **func;
|
2018-03-14 23:22:35 +03:00
|
|
|
int func_count;
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_pmx_group *groups;
|
2018-03-14 23:22:35 +03:00
|
|
|
const char **group_names;
|
|
|
|
int group_count;
|
|
|
|
|
2018-06-28 22:03:03 +03:00
|
|
|
u8 *gpio;
|
2018-03-14 23:22:35 +03:00
|
|
|
int max_pins;
|
|
|
|
};
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
static int ralink_get_group_count(struct pinctrl_dev *pctrldev)
|
2018-03-14 23:22:35 +03:00
|
|
|
{
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
|
2018-03-14 23:22:35 +03:00
|
|
|
|
|
|
|
return p->group_count;
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
static const char *ralink_get_group_name(struct pinctrl_dev *pctrldev,
|
2018-06-08 15:16:32 +03:00
|
|
|
unsigned int group)
|
2018-03-14 23:22:35 +03:00
|
|
|
{
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
|
2018-03-14 23:22:35 +03:00
|
|
|
|
2018-06-28 22:03:07 +03:00
|
|
|
return (group >= p->group_count) ? NULL : p->group_names[group];
|
2018-03-14 23:22:35 +03:00
|
|
|
}
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
static int ralink_get_group_pins(struct pinctrl_dev *pctrldev,
|
2018-06-08 15:16:32 +03:00
|
|
|
unsigned int group,
|
|
|
|
const unsigned int **pins,
|
|
|
|
unsigned int *num_pins)
|
2018-03-14 23:22:35 +03:00
|
|
|
{
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
|
2018-03-14 23:22:35 +03:00
|
|
|
|
|
|
|
if (group >= p->group_count)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*pins = p->groups[group].func[0].pins;
|
|
|
|
*num_pins = p->groups[group].func[0].pin_count;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
static const struct pinctrl_ops ralink_pctrl_ops = {
|
|
|
|
.get_groups_count = ralink_get_group_count,
|
|
|
|
.get_group_name = ralink_get_group_name,
|
|
|
|
.get_group_pins = ralink_get_group_pins,
|
2019-01-04 14:34:08 +03:00
|
|
|
.dt_node_to_map = pinconf_generic_dt_node_to_map_all,
|
|
|
|
.dt_free_map = pinconf_generic_dt_free_map,
|
2018-03-14 23:22:35 +03:00
|
|
|
};
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
static int ralink_pmx_func_count(struct pinctrl_dev *pctrldev)
|
2018-03-14 23:22:35 +03:00
|
|
|
{
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
|
2018-03-14 23:22:35 +03:00
|
|
|
|
|
|
|
return p->func_count;
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
static const char *ralink_pmx_func_name(struct pinctrl_dev *pctrldev,
|
2018-06-28 22:03:00 +03:00
|
|
|
unsigned int func)
|
2018-03-14 23:22:35 +03:00
|
|
|
{
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
|
2018-03-14 23:22:35 +03:00
|
|
|
|
|
|
|
return p->func[func]->name;
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
static int ralink_pmx_group_get_groups(struct pinctrl_dev *pctrldev,
|
2018-06-28 22:03:00 +03:00
|
|
|
unsigned int func,
|
|
|
|
const char * const **groups,
|
|
|
|
unsigned int * const num_groups)
|
2018-03-14 23:22:35 +03:00
|
|
|
{
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
|
2018-03-14 23:22:35 +03:00
|
|
|
|
|
|
|
if (p->func[func]->group_count == 1)
|
|
|
|
*groups = &p->group_names[p->func[func]->groups[0]];
|
|
|
|
else
|
|
|
|
*groups = p->group_names;
|
|
|
|
|
|
|
|
*num_groups = p->func[func]->group_count;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
static int ralink_pmx_group_enable(struct pinctrl_dev *pctrldev,
|
2018-06-28 22:03:00 +03:00
|
|
|
unsigned int func, unsigned int group)
|
2018-03-14 23:22:35 +03:00
|
|
|
{
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
|
2018-06-28 22:03:01 +03:00
|
|
|
u32 mode = 0;
|
2018-03-14 23:22:35 +03:00
|
|
|
u32 reg = SYSC_REG_GPIO_MODE;
|
|
|
|
int i;
|
|
|
|
int shift;
|
|
|
|
|
|
|
|
/* dont allow double use */
|
|
|
|
if (p->groups[group].enabled) {
|
2018-06-28 22:02:58 +03:00
|
|
|
dev_err(p->dev, "%s is already enabled\n",
|
|
|
|
p->groups[group].name);
|
2021-06-04 08:53:37 +03:00
|
|
|
return 0;
|
2018-03-14 23:22:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
p->groups[group].enabled = 1;
|
|
|
|
p->func[func]->enabled = 1;
|
|
|
|
|
|
|
|
shift = p->groups[group].shift;
|
|
|
|
if (shift >= 32) {
|
|
|
|
shift -= 32;
|
|
|
|
reg = SYSC_REG_GPIO_MODE2;
|
|
|
|
}
|
|
|
|
mode = rt_sysc_r32(reg);
|
|
|
|
mode &= ~(p->groups[group].mask << shift);
|
|
|
|
|
|
|
|
/* mark the pins as gpio */
|
|
|
|
for (i = 0; i < p->groups[group].func[0].pin_count; i++)
|
|
|
|
p->gpio[p->groups[group].func[0].pins[i]] = 1;
|
|
|
|
|
|
|
|
/* function 0 is gpio and needs special handling */
|
|
|
|
if (func == 0) {
|
|
|
|
mode |= p->groups[group].gpio << shift;
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < p->func[func]->pin_count; i++)
|
|
|
|
p->gpio[p->func[func]->pins[i]] = 0;
|
|
|
|
mode |= p->func[func]->value << shift;
|
|
|
|
}
|
|
|
|
rt_sysc_w32(mode, reg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
static int ralink_pmx_group_gpio_request_enable(struct pinctrl_dev *pctrldev,
|
2018-06-28 22:03:00 +03:00
|
|
|
struct pinctrl_gpio_range *range,
|
|
|
|
unsigned int pin)
|
2018-03-14 23:22:35 +03:00
|
|
|
{
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
|
2018-03-14 23:22:35 +03:00
|
|
|
|
|
|
|
if (!p->gpio[pin]) {
|
|
|
|
dev_err(p->dev, "pin %d is not set to gpio mux\n", pin);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
static const struct pinmux_ops ralink_pmx_group_ops = {
|
|
|
|
.get_functions_count = ralink_pmx_func_count,
|
|
|
|
.get_function_name = ralink_pmx_func_name,
|
|
|
|
.get_function_groups = ralink_pmx_group_get_groups,
|
|
|
|
.set_mux = ralink_pmx_group_enable,
|
|
|
|
.gpio_request_enable = ralink_pmx_group_gpio_request_enable,
|
2018-03-14 23:22:35 +03:00
|
|
|
};
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
static struct pinctrl_desc ralink_pctrl_desc = {
|
2018-03-14 23:22:35 +03:00
|
|
|
.owner = THIS_MODULE,
|
2022-04-14 20:39:05 +03:00
|
|
|
.name = "ralink-pinctrl",
|
2022-04-14 20:39:04 +03:00
|
|
|
.pctlops = &ralink_pctrl_ops,
|
|
|
|
.pmxops = &ralink_pmx_group_ops,
|
2018-03-14 23:22:35 +03:00
|
|
|
};
|
|
|
|
|
2022-04-14 20:39:04 +03:00
|
|
|
static struct ralink_pmx_func gpio_func = {
|
2018-03-14 23:22:35 +03:00
|
|
|
.name = "gpio",
|
|
|
|
};
|
|
|
|
|
2022-04-14 20:39:05 +03:00
|
|
|
static int ralink_pinctrl_index(struct ralink_priv *p)
|
2018-03-14 23:22:35 +03:00
|
|
|
{
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_pmx_group *mux = p->groups;
|
2018-03-14 23:22:35 +03:00
|
|
|
int i, j, c = 0;
|
|
|
|
|
|
|
|
/* count the mux functions */
|
|
|
|
while (mux->name) {
|
|
|
|
p->group_count++;
|
|
|
|
mux++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate the group names array needed by the gpio function */
|
2018-06-28 22:02:58 +03:00
|
|
|
p->group_names = devm_kcalloc(p->dev, p->group_count,
|
|
|
|
sizeof(char *), GFP_KERNEL);
|
2018-03-14 23:22:35 +03:00
|
|
|
if (!p->group_names)
|
2020-12-13 19:17:16 +03:00
|
|
|
return -ENOMEM;
|
2018-03-14 23:22:35 +03:00
|
|
|
|
|
|
|
for (i = 0; i < p->group_count; i++) {
|
|
|
|
p->group_names[i] = p->groups[i].name;
|
|
|
|
p->func_count += p->groups[i].func_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we have a dummy function[0] for gpio */
|
|
|
|
p->func_count++;
|
|
|
|
|
|
|
|
/* allocate our function and group mapping index buffers */
|
2020-12-13 19:17:15 +03:00
|
|
|
p->func = devm_kcalloc(p->dev, p->func_count,
|
|
|
|
sizeof(*p->func), GFP_KERNEL);
|
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:
devm_kzalloc(handle, a * b, gfp)
with:
devm_kcalloc(handle, a * b, gfp)
as well as handling cases of:
devm_kzalloc(handle, a * b * c, gfp)
with:
devm_kzalloc(handle, array3_size(a, b, c), gfp)
as it's slightly less ugly than:
devm_kcalloc(handle, array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
devm_kzalloc(handle, 4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@
(
devm_kzalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
devm_kzalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@
(
devm_kzalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@
(
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * E2
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * (E2)
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 00:07:58 +03:00
|
|
|
gpio_func.groups = devm_kcalloc(p->dev, p->group_count, sizeof(int),
|
|
|
|
GFP_KERNEL);
|
2020-12-13 19:17:15 +03:00
|
|
|
if (!p->func || !gpio_func.groups)
|
|
|
|
return -ENOMEM;
|
2018-03-14 23:22:35 +03:00
|
|
|
|
|
|
|
/* add a backpointer to the function so it knows its group */
|
|
|
|
gpio_func.group_count = p->group_count;
|
|
|
|
for (i = 0; i < gpio_func.group_count; i++)
|
|
|
|
gpio_func.groups[i] = i;
|
|
|
|
|
2020-12-13 19:17:15 +03:00
|
|
|
p->func[c] = &gpio_func;
|
2018-03-14 23:22:35 +03:00
|
|
|
c++;
|
|
|
|
|
|
|
|
/* add remaining functions */
|
|
|
|
for (i = 0; i < p->group_count; i++) {
|
|
|
|
for (j = 0; j < p->groups[i].func_count; j++) {
|
2020-12-13 19:17:15 +03:00
|
|
|
p->func[c] = &p->groups[i].func[j];
|
|
|
|
p->func[c]->groups = devm_kzalloc(p->dev, sizeof(int),
|
2018-06-28 22:02:58 +03:00
|
|
|
GFP_KERNEL);
|
2020-12-13 19:17:17 +03:00
|
|
|
if (!p->func[c]->groups)
|
|
|
|
return -ENOMEM;
|
2020-12-13 19:17:15 +03:00
|
|
|
p->func[c]->groups[0] = i;
|
|
|
|
p->func[c]->group_count = 1;
|
2018-03-14 23:22:35 +03:00
|
|
|
c++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:39:05 +03:00
|
|
|
static int ralink_pinctrl_pins(struct ralink_priv *p)
|
2018-03-14 23:22:35 +03:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
2018-06-28 22:02:58 +03:00
|
|
|
/*
|
|
|
|
* loop over the functions and initialize the pins array.
|
|
|
|
* also work out the highest pin used.
|
|
|
|
*/
|
2018-03-14 23:22:35 +03:00
|
|
|
for (i = 0; i < p->func_count; i++) {
|
|
|
|
int pin;
|
|
|
|
|
|
|
|
if (!p->func[i]->pin_count)
|
|
|
|
continue;
|
|
|
|
|
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:
devm_kzalloc(handle, a * b, gfp)
with:
devm_kcalloc(handle, a * b, gfp)
as well as handling cases of:
devm_kzalloc(handle, a * b * c, gfp)
with:
devm_kzalloc(handle, array3_size(a, b, c), gfp)
as it's slightly less ugly than:
devm_kcalloc(handle, array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
devm_kzalloc(handle, 4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@
(
devm_kzalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
devm_kzalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@
(
devm_kzalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@
(
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * E2
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * (E2)
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 00:07:58 +03:00
|
|
|
p->func[i]->pins = devm_kcalloc(p->dev,
|
|
|
|
p->func[i]->pin_count,
|
|
|
|
sizeof(int),
|
|
|
|
GFP_KERNEL);
|
2022-07-10 18:49:22 +03:00
|
|
|
if (!p->func[i]->pins)
|
|
|
|
return -ENOMEM;
|
2018-03-14 23:22:35 +03:00
|
|
|
for (j = 0; j < p->func[i]->pin_count; j++)
|
|
|
|
p->func[i]->pins[j] = p->func[i]->pin_first + j;
|
|
|
|
|
|
|
|
pin = p->func[i]->pin_first + p->func[i]->pin_count;
|
|
|
|
if (pin > p->max_pins)
|
|
|
|
p->max_pins = pin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the buffer that tells us which pins are gpio */
|
2018-06-28 22:03:03 +03:00
|
|
|
p->gpio = devm_kcalloc(p->dev, p->max_pins, sizeof(u8), GFP_KERNEL);
|
2018-03-14 23:22:35 +03:00
|
|
|
/* the pads needed to tell pinctrl about our pins */
|
2018-06-28 22:02:58 +03:00
|
|
|
p->pads = devm_kcalloc(p->dev, p->max_pins,
|
|
|
|
sizeof(struct pinctrl_pin_desc), GFP_KERNEL);
|
2020-12-13 19:17:18 +03:00
|
|
|
if (!p->pads || !p->gpio)
|
2018-03-14 23:22:35 +03:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-06-28 22:03:03 +03:00
|
|
|
memset(p->gpio, 1, sizeof(u8) * p->max_pins);
|
2018-03-14 23:22:35 +03:00
|
|
|
for (i = 0; i < p->func_count; i++) {
|
|
|
|
if (!p->func[i]->pin_count)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (j = 0; j < p->func[i]->pin_count; j++)
|
|
|
|
p->gpio[p->func[i]->pins[j]] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pin 0 is always a gpio */
|
|
|
|
p->gpio[0] = 1;
|
|
|
|
|
|
|
|
/* set the pads */
|
|
|
|
for (i = 0; i < p->max_pins; i++) {
|
|
|
|
/* strlen("ioXY") + 1 = 5 */
|
|
|
|
char *name = devm_kzalloc(p->dev, 5, GFP_KERNEL);
|
|
|
|
|
2018-06-08 15:16:33 +03:00
|
|
|
if (!name)
|
2018-03-14 23:22:35 +03:00
|
|
|
return -ENOMEM;
|
|
|
|
snprintf(name, 5, "io%d", i);
|
|
|
|
p->pads[i].number = i;
|
|
|
|
p->pads[i].name = name;
|
|
|
|
}
|
|
|
|
p->desc->pins = p->pads;
|
|
|
|
p->desc->npins = p->max_pins;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:39:05 +03:00
|
|
|
int ralink_pinctrl_init(struct platform_device *pdev,
|
|
|
|
struct ralink_pmx_group *data)
|
2018-03-14 23:22:35 +03:00
|
|
|
{
|
2022-04-14 20:39:04 +03:00
|
|
|
struct ralink_priv *p;
|
2018-03-14 23:22:35 +03:00
|
|
|
struct pinctrl_dev *dev;
|
2020-12-13 19:17:19 +03:00
|
|
|
int err;
|
2018-03-14 23:22:35 +03:00
|
|
|
|
2021-06-04 14:51:54 +03:00
|
|
|
if (!data)
|
2018-06-08 15:16:34 +03:00
|
|
|
return -ENOTSUPP;
|
2018-03-14 23:22:35 +03:00
|
|
|
|
|
|
|
/* setup the private data */
|
2022-04-14 20:39:04 +03:00
|
|
|
p = devm_kzalloc(&pdev->dev, sizeof(struct ralink_priv), GFP_KERNEL);
|
2018-03-14 23:22:35 +03:00
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
p->dev = &pdev->dev;
|
2022-04-14 20:39:04 +03:00
|
|
|
p->desc = &ralink_pctrl_desc;
|
2021-06-04 14:51:54 +03:00
|
|
|
p->groups = data;
|
2018-03-14 23:22:35 +03:00
|
|
|
platform_set_drvdata(pdev, p);
|
|
|
|
|
|
|
|
/* init the device */
|
2022-04-14 20:39:05 +03:00
|
|
|
err = ralink_pinctrl_index(p);
|
2020-12-13 19:17:19 +03:00
|
|
|
if (err) {
|
2018-03-14 23:22:35 +03:00
|
|
|
dev_err(&pdev->dev, "failed to load index\n");
|
2020-12-13 19:17:19 +03:00
|
|
|
return err;
|
2018-03-14 23:22:35 +03:00
|
|
|
}
|
2020-12-13 19:17:19 +03:00
|
|
|
|
2022-04-14 20:39:05 +03:00
|
|
|
err = ralink_pinctrl_pins(p);
|
2020-12-13 19:17:19 +03:00
|
|
|
if (err) {
|
2018-03-14 23:22:35 +03:00
|
|
|
dev_err(&pdev->dev, "failed to load pins\n");
|
2020-12-13 19:17:19 +03:00
|
|
|
return err;
|
2018-03-14 23:22:35 +03:00
|
|
|
}
|
|
|
|
dev = pinctrl_register(p->desc, &pdev->dev, p);
|
|
|
|
|
2020-12-13 19:17:20 +03:00
|
|
|
return PTR_ERR_OR_ZERO(dev);
|
2018-03-14 23:22:35 +03:00
|
|
|
}
|