2018-06-08 14:16:31 +02:00
// SPDX-License-Identifier: GPL-2.0
2018-03-15 07:22:35 +11: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 12:34:08 +01:00
# include <linux/pinctrl/pinconf-generic.h>
2018-03-15 07:22:35 +11: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>
2023-03-18 00:29:54 +03:00
# include "pinctrl-mtmips.h"
2020-12-08 08:55:23 +01:00
# include "../core.h"
# include "../pinctrl-utils.h"
2018-03-15 07:22:35 +11:00
# define SYSC_REG_GPIO_MODE 0x60
# define SYSC_REG_GPIO_MODE2 0x64
2023-03-18 00:29:54 +03:00
struct mtmips_priv {
2018-03-15 07:22:35 +11:00
struct device * dev ;
struct pinctrl_pin_desc * pads ;
struct pinctrl_desc * desc ;
2023-03-18 00:29:54 +03:00
struct mtmips_pmx_func * * func ;
2018-03-15 07:22:35 +11:00
int func_count ;
2023-03-18 00:29:54 +03:00
struct mtmips_pmx_group * groups ;
2018-03-15 07:22:35 +11:00
const char * * group_names ;
int group_count ;
2018-06-28 21:03:03 +02:00
u8 * gpio ;
2018-03-15 07:22:35 +11:00
int max_pins ;
} ;
2023-03-18 00:29:54 +03:00
static int mtmips_get_group_count ( struct pinctrl_dev * pctrldev )
2018-03-15 07:22:35 +11:00
{
2023-03-18 00:29:54 +03:00
struct mtmips_priv * p = pinctrl_dev_get_drvdata ( pctrldev ) ;
2018-03-15 07:22:35 +11:00
return p - > group_count ;
}
2023-03-18 00:29:54 +03:00
static const char * mtmips_get_group_name ( struct pinctrl_dev * pctrldev ,
2018-06-08 14:16:32 +02:00
unsigned int group )
2018-03-15 07:22:35 +11:00
{
2023-03-18 00:29:54 +03:00
struct mtmips_priv * p = pinctrl_dev_get_drvdata ( pctrldev ) ;
2018-03-15 07:22:35 +11:00
2018-06-28 21:03:07 +02:00
return ( group > = p - > group_count ) ? NULL : p - > group_names [ group ] ;
2018-03-15 07:22:35 +11:00
}
2023-03-18 00:29:54 +03:00
static int mtmips_get_group_pins ( struct pinctrl_dev * pctrldev ,
2018-06-08 14:16:32 +02:00
unsigned int group ,
const unsigned int * * pins ,
unsigned int * num_pins )
2018-03-15 07:22:35 +11:00
{
2023-03-18 00:29:54 +03:00
struct mtmips_priv * p = pinctrl_dev_get_drvdata ( pctrldev ) ;
2018-03-15 07:22:35 +11: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 ;
}
2023-03-18 00:29:54 +03:00
static const struct pinctrl_ops mtmips_pctrl_ops = {
. get_groups_count = mtmips_get_group_count ,
. get_group_name = mtmips_get_group_name ,
. get_group_pins = mtmips_get_group_pins ,
2019-01-04 12:34:08 +01:00
. dt_node_to_map = pinconf_generic_dt_node_to_map_all ,
. dt_free_map = pinconf_generic_dt_free_map ,
2018-03-15 07:22:35 +11:00
} ;
2023-03-18 00:29:54 +03:00
static int mtmips_pmx_func_count ( struct pinctrl_dev * pctrldev )
2018-03-15 07:22:35 +11:00
{
2023-03-18 00:29:54 +03:00
struct mtmips_priv * p = pinctrl_dev_get_drvdata ( pctrldev ) ;
2018-03-15 07:22:35 +11:00
return p - > func_count ;
}
2023-03-18 00:29:54 +03:00
static const char * mtmips_pmx_func_name ( struct pinctrl_dev * pctrldev ,
2018-06-28 21:03:00 +02:00
unsigned int func )
2018-03-15 07:22:35 +11:00
{
2023-03-18 00:29:54 +03:00
struct mtmips_priv * p = pinctrl_dev_get_drvdata ( pctrldev ) ;
2018-03-15 07:22:35 +11:00
return p - > func [ func ] - > name ;
}
2023-03-18 00:29:54 +03:00
static int mtmips_pmx_group_get_groups ( struct pinctrl_dev * pctrldev ,
2018-06-28 21:03:00 +02:00
unsigned int func ,
const char * const * * groups ,
unsigned int * const num_groups )
2018-03-15 07:22:35 +11:00
{
2023-03-18 00:29:54 +03:00
struct mtmips_priv * p = pinctrl_dev_get_drvdata ( pctrldev ) ;
2018-03-15 07:22:35 +11: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 ;
}
2023-03-18 00:29:54 +03:00
static int mtmips_pmx_group_enable ( struct pinctrl_dev * pctrldev ,
2018-06-28 21:03:00 +02:00
unsigned int func , unsigned int group )
2018-03-15 07:22:35 +11:00
{
2023-03-18 00:29:54 +03:00
struct mtmips_priv * p = pinctrl_dev_get_drvdata ( pctrldev ) ;
2018-06-28 21:03:01 +02:00
u32 mode = 0 ;
2018-03-15 07:22:35 +11:00
u32 reg = SYSC_REG_GPIO_MODE ;
int i ;
int shift ;
/* dont allow double use */
if ( p - > groups [ group ] . enabled ) {
2018-06-28 21:02:58 +02:00
dev_err ( p - > dev , " %s is already enabled \n " ,
p - > groups [ group ] . name ) ;
2021-06-04 07:53:37 +02:00
return 0 ;
2018-03-15 07:22:35 +11: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 ;
}
2023-03-18 00:29:54 +03:00
static int mtmips_pmx_group_gpio_request_enable ( struct pinctrl_dev * pctrldev ,
2018-06-28 21:03:00 +02:00
struct pinctrl_gpio_range * range ,
unsigned int pin )
2018-03-15 07:22:35 +11:00
{
2023-03-18 00:29:54 +03:00
struct mtmips_priv * p = pinctrl_dev_get_drvdata ( pctrldev ) ;
2018-03-15 07:22:35 +11:00
if ( ! p - > gpio [ pin ] ) {
dev_err ( p - > dev , " pin %d is not set to gpio mux \n " , pin ) ;
return - EINVAL ;
}
return 0 ;
}
2023-03-18 00:29:54 +03:00
static const struct pinmux_ops mtmips_pmx_group_ops = {
. get_functions_count = mtmips_pmx_func_count ,
. get_function_name = mtmips_pmx_func_name ,
. get_function_groups = mtmips_pmx_group_get_groups ,
. set_mux = mtmips_pmx_group_enable ,
. gpio_request_enable = mtmips_pmx_group_gpio_request_enable ,
2018-03-15 07:22:35 +11:00
} ;
2023-03-18 00:29:54 +03:00
static struct pinctrl_desc mtmips_pctrl_desc = {
2018-03-15 07:22:35 +11:00
. owner = THIS_MODULE ,
2023-03-18 00:29:54 +03:00
. name = " mtmips-pinctrl " ,
. pctlops = & mtmips_pctrl_ops ,
. pmxops = & mtmips_pmx_group_ops ,
2018-03-15 07:22:35 +11:00
} ;
2023-03-18 00:29:54 +03:00
static struct mtmips_pmx_func gpio_func = {
2018-03-15 07:22:35 +11:00
. name = " gpio " ,
} ;
2023-03-18 00:29:54 +03:00
static int mtmips_pinctrl_index ( struct mtmips_priv * p )
2018-03-15 07:22:35 +11:00
{
2023-03-18 00:29:54 +03:00
struct mtmips_pmx_group * mux = p - > groups ;
2018-03-15 07:22:35 +11: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 21:02:58 +02:00
p - > group_names = devm_kcalloc ( p - > dev , p - > group_count ,
sizeof ( char * ) , GFP_KERNEL ) ;
2018-03-15 07:22:35 +11:00
if ( ! p - > group_names )
2020-12-13 17:17:16 +01:00
return - ENOMEM ;
2018-03-15 07:22:35 +11: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 17:17:15 +01: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-12 14:07:58 -07:00
gpio_func . groups = devm_kcalloc ( p - > dev , p - > group_count , sizeof ( int ) ,
GFP_KERNEL ) ;
2020-12-13 17:17:15 +01:00
if ( ! p - > func | | ! gpio_func . groups )
return - ENOMEM ;
2018-03-15 07:22:35 +11: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 17:17:15 +01:00
p - > func [ c ] = & gpio_func ;
2018-03-15 07:22:35 +11: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 17:17:15 +01:00
p - > func [ c ] = & p - > groups [ i ] . func [ j ] ;
p - > func [ c ] - > groups = devm_kzalloc ( p - > dev , sizeof ( int ) ,
2018-06-28 21:02:58 +02:00
GFP_KERNEL ) ;
2020-12-13 17:17:17 +01:00
if ( ! p - > func [ c ] - > groups )
return - ENOMEM ;
2020-12-13 17:17:15 +01:00
p - > func [ c ] - > groups [ 0 ] = i ;
p - > func [ c ] - > group_count = 1 ;
2018-03-15 07:22:35 +11:00
c + + ;
}
}
return 0 ;
}
2023-03-18 00:29:54 +03:00
static int mtmips_pinctrl_pins ( struct mtmips_priv * p )
2018-03-15 07:22:35 +11:00
{
int i , j ;
2018-06-28 21:02:58 +02:00
/*
* loop over the functions and initialize the pins array .
* also work out the highest pin used .
*/
2018-03-15 07:22:35 +11: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-12 14:07:58 -07:00
p - > func [ i ] - > pins = devm_kcalloc ( p - > dev ,
p - > func [ i ] - > pin_count ,
sizeof ( int ) ,
GFP_KERNEL ) ;
2022-07-10 23:49:22 +08:00
if ( ! p - > func [ i ] - > pins )
return - ENOMEM ;
2018-03-15 07:22:35 +11: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 21:03:03 +02:00
p - > gpio = devm_kcalloc ( p - > dev , p - > max_pins , sizeof ( u8 ) , GFP_KERNEL ) ;
2018-03-15 07:22:35 +11:00
/* the pads needed to tell pinctrl about our pins */
2018-06-28 21:02:58 +02:00
p - > pads = devm_kcalloc ( p - > dev , p - > max_pins ,
sizeof ( struct pinctrl_pin_desc ) , GFP_KERNEL ) ;
2020-12-13 17:17:18 +01:00
if ( ! p - > pads | | ! p - > gpio )
2018-03-15 07:22:35 +11:00
return - ENOMEM ;
2018-06-28 21:03:03 +02:00
memset ( p - > gpio , 1 , sizeof ( u8 ) * p - > max_pins ) ;
2018-03-15 07:22:35 +11: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 14:16:33 +02:00
if ( ! name )
2018-03-15 07:22:35 +11: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 ;
}
2023-03-18 00:29:54 +03:00
int mtmips_pinctrl_init ( struct platform_device * pdev ,
struct mtmips_pmx_group * data )
2018-03-15 07:22:35 +11:00
{
2023-03-18 00:29:54 +03:00
struct mtmips_priv * p ;
2018-03-15 07:22:35 +11:00
struct pinctrl_dev * dev ;
2020-12-13 17:17:19 +01:00
int err ;
2018-03-15 07:22:35 +11:00
2021-06-04 13:51:54 +02:00
if ( ! data )
2018-06-08 14:16:34 +02:00
return - ENOTSUPP ;
2018-03-15 07:22:35 +11:00
/* setup the private data */
2023-03-18 00:29:54 +03:00
p = devm_kzalloc ( & pdev - > dev , sizeof ( struct mtmips_priv ) , GFP_KERNEL ) ;
2018-03-15 07:22:35 +11:00
if ( ! p )
return - ENOMEM ;
p - > dev = & pdev - > dev ;
2023-03-18 00:29:54 +03:00
p - > desc = & mtmips_pctrl_desc ;
2021-06-04 13:51:54 +02:00
p - > groups = data ;
2018-03-15 07:22:35 +11:00
platform_set_drvdata ( pdev , p ) ;
/* init the device */
2023-03-18 00:29:54 +03:00
err = mtmips_pinctrl_index ( p ) ;
2020-12-13 17:17:19 +01:00
if ( err ) {
2018-03-15 07:22:35 +11:00
dev_err ( & pdev - > dev , " failed to load index \n " ) ;
2020-12-13 17:17:19 +01:00
return err ;
2018-03-15 07:22:35 +11:00
}
2020-12-13 17:17:19 +01:00
2023-03-18 00:29:54 +03:00
err = mtmips_pinctrl_pins ( p ) ;
2020-12-13 17:17:19 +01:00
if ( err ) {
2018-03-15 07:22:35 +11:00
dev_err ( & pdev - > dev , " failed to load pins \n " ) ;
2020-12-13 17:17:19 +01:00
return err ;
2018-03-15 07:22:35 +11:00
}
dev = pinctrl_register ( p - > desc , & pdev - > dev , p ) ;
2020-12-13 17:17:20 +01:00
return PTR_ERR_OR_ZERO ( dev ) ;
2018-03-15 07:22:35 +11:00
}