2019-05-27 08:55:01 +02:00
// SPDX-License-Identifier: GPL-2.0-or-later
2012-09-13 17:41:43 +02:00
/*
* Marvell MVEBU pinctrl core driver
*
* Authors : Sebastian Hesselbarth < sebastian . hesselbarth @ gmail . com >
* Thomas Petazzoni < thomas . petazzoni @ free - electrons . com >
*/
# include <linux/platform_device.h>
# include <linux/slab.h>
# include <linux/io.h>
# include <linux/of.h>
# include <linux/of_address.h>
# include <linux/of_platform.h>
# include <linux/err.h>
2018-09-13 13:58:21 +02:00
# include <linux/gpio/driver.h>
2012-09-13 17:41:43 +02:00
# include <linux/pinctrl/machine.h>
# include <linux/pinctrl/pinconf.h>
# include <linux/pinctrl/pinctrl.h>
# include <linux/pinctrl/pinmux.h>
2017-01-13 11:03:35 +00:00
# include <linux/mfd/syscon.h>
# include <linux/regmap.h>
2012-09-13 17:41:43 +02:00
# include "pinctrl-mvebu.h"
# define MPPS_PER_REG 8
# define MPP_BITS 4
# define MPP_MASK 0xf
struct mvebu_pinctrl_function {
const char * name ;
const char * * groups ;
unsigned num_groups ;
} ;
struct mvebu_pinctrl_group {
const char * name ;
2017-01-13 11:03:15 +00:00
const struct mvebu_mpp_ctrl * ctrl ;
2017-01-13 11:03:20 +00:00
struct mvebu_mpp_ctrl_data * data ;
2012-09-13 17:41:43 +02:00
struct mvebu_mpp_ctrl_setting * settings ;
unsigned num_settings ;
unsigned gid ;
unsigned * pins ;
unsigned npins ;
} ;
struct mvebu_pinctrl {
struct device * dev ;
struct pinctrl_dev * pctldev ;
struct pinctrl_desc desc ;
struct mvebu_pinctrl_group * groups ;
unsigned num_groups ;
struct mvebu_pinctrl_function * functions ;
unsigned num_functions ;
u8 variant ;
} ;
2017-01-13 11:03:25 +00:00
int mvebu_mmio_mpp_ctrl_get ( struct mvebu_mpp_ctrl_data * data ,
unsigned int pid , unsigned long * config )
{
unsigned off = ( pid / MVEBU_MPPS_PER_REG ) * MVEBU_MPP_BITS ;
unsigned shift = ( pid % MVEBU_MPPS_PER_REG ) * MVEBU_MPP_BITS ;
* config = ( readl ( data - > base + off ) > > shift ) & MVEBU_MPP_MASK ;
return 0 ;
}
int mvebu_mmio_mpp_ctrl_set ( struct mvebu_mpp_ctrl_data * data ,
unsigned int pid , unsigned long config )
{
unsigned off = ( pid / MVEBU_MPPS_PER_REG ) * MVEBU_MPP_BITS ;
unsigned shift = ( pid % MVEBU_MPPS_PER_REG ) * MVEBU_MPP_BITS ;
unsigned long reg ;
reg = readl ( data - > base + off ) & ~ ( MVEBU_MPP_MASK < < shift ) ;
writel ( reg | ( config < < shift ) , data - > base + off ) ;
return 0 ;
}
2012-09-13 17:41:43 +02:00
static struct mvebu_pinctrl_group * mvebu_pinctrl_find_group_by_pid (
struct mvebu_pinctrl * pctl , unsigned pid )
{
unsigned n ;
for ( n = 0 ; n < pctl - > num_groups ; n + + ) {
if ( pid > = pctl - > groups [ n ] . pins [ 0 ] & &
pid < pctl - > groups [ n ] . pins [ 0 ] +
pctl - > groups [ n ] . npins )
return & pctl - > groups [ n ] ;
}
return NULL ;
}
static struct mvebu_pinctrl_group * mvebu_pinctrl_find_group_by_name (
struct mvebu_pinctrl * pctl , const char * name )
{
unsigned n ;
for ( n = 0 ; n < pctl - > num_groups ; n + + ) {
if ( strcmp ( name , pctl - > groups [ n ] . name ) = = 0 )
return & pctl - > groups [ n ] ;
}
return NULL ;
}
static struct mvebu_mpp_ctrl_setting * mvebu_pinctrl_find_setting_by_val (
struct mvebu_pinctrl * pctl , struct mvebu_pinctrl_group * grp ,
unsigned long config )
{
unsigned n ;
for ( n = 0 ; n < grp - > num_settings ; n + + ) {
if ( config = = grp - > settings [ n ] . val ) {
if ( ! pctl - > variant | | ( pctl - > variant &
grp - > settings [ n ] . variant ) )
return & grp - > settings [ n ] ;
}
}
return NULL ;
}
static struct mvebu_mpp_ctrl_setting * mvebu_pinctrl_find_setting_by_name (
struct mvebu_pinctrl * pctl , struct mvebu_pinctrl_group * grp ,
const char * name )
{
unsigned n ;
for ( n = 0 ; n < grp - > num_settings ; n + + ) {
if ( strcmp ( name , grp - > settings [ n ] . name ) = = 0 ) {
if ( ! pctl - > variant | | ( pctl - > variant &
grp - > settings [ n ] . variant ) )
return & grp - > settings [ n ] ;
}
}
return NULL ;
}
static struct mvebu_mpp_ctrl_setting * mvebu_pinctrl_find_gpio_setting (
struct mvebu_pinctrl * pctl , struct mvebu_pinctrl_group * grp )
{
unsigned n ;
for ( n = 0 ; n < grp - > num_settings ; n + + ) {
if ( grp - > settings [ n ] . flags &
( MVEBU_SETTING_GPO | MVEBU_SETTING_GPI ) ) {
if ( ! pctl - > variant | | ( pctl - > variant &
grp - > settings [ n ] . variant ) )
return & grp - > settings [ n ] ;
}
}
return NULL ;
}
static struct mvebu_pinctrl_function * mvebu_pinctrl_find_function_by_name (
struct mvebu_pinctrl * pctl , const char * name )
{
unsigned n ;
for ( n = 0 ; n < pctl - > num_functions ; n + + ) {
if ( strcmp ( name , pctl - > functions [ n ] . name ) = = 0 )
return & pctl - > functions [ n ] ;
}
return NULL ;
}
static int mvebu_pinconf_group_get ( struct pinctrl_dev * pctldev ,
unsigned gid , unsigned long * config )
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
struct mvebu_pinctrl_group * grp = & pctl - > groups [ gid ] ;
if ( ! grp - > ctrl )
return - EINVAL ;
2017-01-13 11:03:20 +00:00
return grp - > ctrl - > mpp_get ( grp - > data , grp - > pins [ 0 ] , config ) ;
2012-09-13 17:41:43 +02:00
}
static int mvebu_pinconf_group_set ( struct pinctrl_dev * pctldev ,
2013-08-27 11:32:12 -07:00
unsigned gid , unsigned long * configs ,
unsigned num_configs )
2012-09-13 17:41:43 +02:00
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
struct mvebu_pinctrl_group * grp = & pctl - > groups [ gid ] ;
2013-08-27 11:32:12 -07:00
int i , ret ;
2012-09-13 17:41:43 +02:00
if ( ! grp - > ctrl )
return - EINVAL ;
2013-08-27 11:32:12 -07:00
for ( i = 0 ; i < num_configs ; i + + ) {
2017-01-13 11:03:20 +00:00
ret = grp - > ctrl - > mpp_set ( grp - > data , grp - > pins [ 0 ] , configs [ i ] ) ;
2013-08-27 11:32:12 -07:00
if ( ret )
return ret ;
} /* for each config */
return 0 ;
2012-09-13 17:41:43 +02:00
}
static void mvebu_pinconf_group_dbg_show ( struct pinctrl_dev * pctldev ,
struct seq_file * s , unsigned gid )
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
struct mvebu_pinctrl_group * grp = & pctl - > groups [ gid ] ;
struct mvebu_mpp_ctrl_setting * curr ;
unsigned long config ;
unsigned n ;
if ( mvebu_pinconf_group_get ( pctldev , gid , & config ) )
return ;
curr = mvebu_pinctrl_find_setting_by_val ( pctl , grp , config ) ;
if ( curr ) {
seq_printf ( s , " current: %s " , curr - > name ) ;
if ( curr - > subname )
seq_printf ( s , " (%s) " , curr - > subname ) ;
if ( curr - > flags & ( MVEBU_SETTING_GPO | MVEBU_SETTING_GPI ) ) {
2017-01-12 17:12:30 +01:00
seq_putc ( s , ' ( ' ) ;
2012-09-13 17:41:43 +02:00
if ( curr - > flags & MVEBU_SETTING_GPI )
2017-01-12 17:12:30 +01:00
seq_putc ( s , ' i ' ) ;
2012-09-13 17:41:43 +02:00
if ( curr - > flags & MVEBU_SETTING_GPO )
2017-01-12 17:12:30 +01:00
seq_putc ( s , ' o ' ) ;
seq_putc ( s , ' ) ' ) ;
2012-09-13 17:41:43 +02:00
}
2017-01-12 16:51:00 +01:00
} else {
seq_puts ( s , " current: UNKNOWN " ) ;
}
2012-09-13 17:41:43 +02:00
if ( grp - > num_settings > 1 ) {
2017-01-12 16:51:00 +01:00
seq_puts ( s , " , available = [ " ) ;
2012-09-13 17:41:43 +02:00
for ( n = 0 ; n < grp - > num_settings ; n + + ) {
if ( curr = = & grp - > settings [ n ] )
continue ;
/* skip unsupported settings for this variant */
if ( pctl - > variant & &
! ( pctl - > variant & grp - > settings [ n ] . variant ) )
continue ;
seq_printf ( s , " %s " , grp - > settings [ n ] . name ) ;
if ( grp - > settings [ n ] . subname )
seq_printf ( s , " (%s) " , grp - > settings [ n ] . subname ) ;
if ( grp - > settings [ n ] . flags &
( MVEBU_SETTING_GPO | MVEBU_SETTING_GPI ) ) {
2017-01-12 17:12:30 +01:00
seq_putc ( s , ' ( ' ) ;
2012-09-13 17:41:43 +02:00
if ( grp - > settings [ n ] . flags & MVEBU_SETTING_GPI )
2017-01-12 17:12:30 +01:00
seq_putc ( s , ' i ' ) ;
2012-09-13 17:41:43 +02:00
if ( grp - > settings [ n ] . flags & MVEBU_SETTING_GPO )
2017-01-12 17:12:30 +01:00
seq_putc ( s , ' o ' ) ;
seq_putc ( s , ' ) ' ) ;
2012-09-13 17:41:43 +02:00
}
}
2017-01-12 16:51:00 +01:00
seq_puts ( s , " ] " ) ;
2012-09-13 17:41:43 +02:00
}
}
2013-02-16 10:25:07 +01:00
static const struct pinconf_ops mvebu_pinconf_ops = {
2012-09-13 17:41:43 +02:00
. pin_config_group_get = mvebu_pinconf_group_get ,
. pin_config_group_set = mvebu_pinconf_group_set ,
. pin_config_group_dbg_show = mvebu_pinconf_group_dbg_show ,
} ;
static int mvebu_pinmux_get_funcs_count ( struct pinctrl_dev * pctldev )
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
return pctl - > num_functions ;
}
static const char * mvebu_pinmux_get_func_name ( struct pinctrl_dev * pctldev ,
unsigned fid )
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
return pctl - > functions [ fid ] . name ;
}
static int mvebu_pinmux_get_groups ( struct pinctrl_dev * pctldev , unsigned fid ,
const char * const * * groups ,
unsigned * const num_groups )
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
* groups = pctl - > functions [ fid ] . groups ;
* num_groups = pctl - > functions [ fid ] . num_groups ;
return 0 ;
}
2014-09-03 13:02:56 +02:00
static int mvebu_pinmux_set ( struct pinctrl_dev * pctldev , unsigned fid ,
unsigned gid )
2012-09-13 17:41:43 +02:00
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
struct mvebu_pinctrl_function * func = & pctl - > functions [ fid ] ;
struct mvebu_pinctrl_group * grp = & pctl - > groups [ gid ] ;
struct mvebu_mpp_ctrl_setting * setting ;
int ret ;
2013-08-27 11:32:12 -07:00
unsigned long config ;
2012-09-13 17:41:43 +02:00
setting = mvebu_pinctrl_find_setting_by_name ( pctl , grp ,
func - > name ) ;
if ( ! setting ) {
dev_err ( pctl - > dev ,
" unable to find setting %s in group %s \n " ,
func - > name , func - > groups [ gid ] ) ;
return - EINVAL ;
}
2013-08-27 11:32:12 -07:00
config = setting - > val ;
ret = mvebu_pinconf_group_set ( pctldev , grp - > gid , & config , 1 ) ;
2012-09-13 17:41:43 +02:00
if ( ret ) {
dev_err ( pctl - > dev , " cannot set group %s to %s \n " ,
func - > groups [ gid ] , func - > name ) ;
return ret ;
}
return 0 ;
}
static int mvebu_pinmux_gpio_request_enable ( struct pinctrl_dev * pctldev ,
struct pinctrl_gpio_range * range , unsigned offset )
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
struct mvebu_pinctrl_group * grp ;
struct mvebu_mpp_ctrl_setting * setting ;
2013-08-27 11:32:12 -07:00
unsigned long config ;
2012-09-13 17:41:43 +02:00
grp = mvebu_pinctrl_find_group_by_pid ( pctl , offset ) ;
if ( ! grp )
return - EINVAL ;
if ( grp - > ctrl - > mpp_gpio_req )
2017-01-13 11:03:20 +00:00
return grp - > ctrl - > mpp_gpio_req ( grp - > data , offset ) ;
2012-09-13 17:41:43 +02:00
setting = mvebu_pinctrl_find_gpio_setting ( pctl , grp ) ;
if ( ! setting )
return - ENOTSUPP ;
2013-08-27 11:32:12 -07:00
config = setting - > val ;
return mvebu_pinconf_group_set ( pctldev , grp - > gid , & config , 1 ) ;
2012-09-13 17:41:43 +02:00
}
static int mvebu_pinmux_gpio_set_direction ( struct pinctrl_dev * pctldev ,
struct pinctrl_gpio_range * range , unsigned offset , bool input )
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
struct mvebu_pinctrl_group * grp ;
struct mvebu_mpp_ctrl_setting * setting ;
grp = mvebu_pinctrl_find_group_by_pid ( pctl , offset ) ;
if ( ! grp )
return - EINVAL ;
if ( grp - > ctrl - > mpp_gpio_dir )
2017-01-13 11:03:20 +00:00
return grp - > ctrl - > mpp_gpio_dir ( grp - > data , offset , input ) ;
2012-09-13 17:41:43 +02:00
setting = mvebu_pinctrl_find_gpio_setting ( pctl , grp ) ;
if ( ! setting )
return - ENOTSUPP ;
if ( ( input & & ( setting - > flags & MVEBU_SETTING_GPI ) ) | |
( ! input & & ( setting - > flags & MVEBU_SETTING_GPO ) ) )
return 0 ;
return - ENOTSUPP ;
}
2013-02-16 10:25:07 +01:00
static const struct pinmux_ops mvebu_pinmux_ops = {
2012-09-13 17:41:43 +02:00
. get_functions_count = mvebu_pinmux_get_funcs_count ,
. get_function_name = mvebu_pinmux_get_func_name ,
. get_function_groups = mvebu_pinmux_get_groups ,
. gpio_request_enable = mvebu_pinmux_gpio_request_enable ,
. gpio_set_direction = mvebu_pinmux_gpio_set_direction ,
2014-09-03 13:02:56 +02:00
. set_mux = mvebu_pinmux_set ,
2012-09-13 17:41:43 +02:00
} ;
static int mvebu_pinctrl_get_groups_count ( struct pinctrl_dev * pctldev )
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
return pctl - > num_groups ;
}
static const char * mvebu_pinctrl_get_group_name ( struct pinctrl_dev * pctldev ,
unsigned gid )
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
return pctl - > groups [ gid ] . name ;
}
static int mvebu_pinctrl_get_group_pins ( struct pinctrl_dev * pctldev ,
unsigned gid , const unsigned * * pins ,
unsigned * num_pins )
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
* pins = pctl - > groups [ gid ] . pins ;
* num_pins = pctl - > groups [ gid ] . npins ;
return 0 ;
}
static int mvebu_pinctrl_dt_node_to_map ( struct pinctrl_dev * pctldev ,
struct device_node * np ,
struct pinctrl_map * * map ,
unsigned * num_maps )
{
struct mvebu_pinctrl * pctl = pinctrl_dev_get_drvdata ( pctldev ) ;
struct property * prop ;
const char * function ;
const char * group ;
int ret , nmaps , n ;
* map = NULL ;
* num_maps = 0 ;
ret = of_property_read_string ( np , " marvell,function " , & function ) ;
if ( ret ) {
dev_err ( pctl - > dev ,
2018-08-27 20:52:41 -05:00
" missing marvell,function in node %pOFn \n " , np ) ;
2012-09-13 17:41:43 +02:00
return 0 ;
}
nmaps = of_property_count_strings ( np , " marvell,pins " ) ;
if ( nmaps < 0 ) {
dev_err ( pctl - > dev ,
2018-08-27 20:52:41 -05:00
" missing marvell,pins in node %pOFn \n " , np ) ;
2012-09-13 17:41:43 +02:00
return 0 ;
}
2017-01-12 16:00:15 +01:00
* map = kmalloc_array ( nmaps , sizeof ( * * map ) , GFP_KERNEL ) ;
2017-01-12 16:15:34 +01:00
if ( ! * map )
2012-09-13 17:41:43 +02:00
return - ENOMEM ;
n = 0 ;
of_property_for_each_string ( np , " marvell,pins " , prop , group ) {
struct mvebu_pinctrl_group * grp =
mvebu_pinctrl_find_group_by_name ( pctl , group ) ;
if ( ! grp ) {
dev_err ( pctl - > dev , " unknown pin %s " , group ) ;
continue ;
}
if ( ! mvebu_pinctrl_find_setting_by_name ( pctl , grp , function ) ) {
dev_err ( pctl - > dev , " unsupported function %s on pin %s " ,
function , group ) ;
continue ;
}
( * map ) [ n ] . type = PIN_MAP_TYPE_MUX_GROUP ;
( * map ) [ n ] . data . mux . group = group ;
( * map ) [ n ] . data . mux . function = function ;
n + + ;
}
* num_maps = nmaps ;
return 0 ;
}
static void mvebu_pinctrl_dt_free_map ( struct pinctrl_dev * pctldev ,
struct pinctrl_map * map , unsigned num_maps )
{
kfree ( map ) ;
}
2013-02-16 10:25:07 +01:00
static const struct pinctrl_ops mvebu_pinctrl_ops = {
2012-09-13 17:41:43 +02:00
. get_groups_count = mvebu_pinctrl_get_groups_count ,
. get_group_name = mvebu_pinctrl_get_group_name ,
. get_group_pins = mvebu_pinctrl_get_group_pins ,
. dt_node_to_map = mvebu_pinctrl_dt_node_to_map ,
. dt_free_map = mvebu_pinctrl_dt_free_map ,
} ;
2013-03-16 12:44:32 +01:00
static int _add_function ( struct mvebu_pinctrl_function * funcs , int * funcsize ,
const char * name )
2012-09-13 17:41:43 +02:00
{
2013-03-16 12:44:32 +01:00
if ( * funcsize < = 0 )
return - EOVERFLOW ;
2012-09-13 17:41:43 +02:00
while ( funcs - > num_groups ) {
/* function already there */
if ( strcmp ( funcs - > name , name ) = = 0 ) {
funcs - > num_groups + + ;
return - EEXIST ;
}
funcs + + ;
}
2013-03-16 12:44:32 +01:00
/* append new unique function */
2012-09-13 17:41:43 +02:00
funcs - > name = name ;
funcs - > num_groups = 1 ;
2013-03-16 12:44:32 +01:00
( * funcsize ) - - ;
2012-09-13 17:41:43 +02:00
return 0 ;
}
2012-12-21 13:10:23 -08:00
static int mvebu_pinctrl_build_functions ( struct platform_device * pdev ,
struct mvebu_pinctrl * pctl )
2012-09-13 17:41:43 +02:00
{
struct mvebu_pinctrl_function * funcs ;
2013-03-16 12:44:32 +01:00
int num = 0 , funcsize = pctl - > desc . npins ;
2012-09-13 17:41:43 +02:00
int n , s ;
/* we allocate functions for number of pins and hope
2013-03-16 12:44:32 +01:00
* there are fewer unique functions than pins available */
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
funcs = devm_kcalloc ( & pdev - > dev ,
funcsize , sizeof ( struct mvebu_pinctrl_function ) ,
GFP_KERNEL ) ;
2012-09-13 17:41:43 +02:00
if ( ! funcs )
return - ENOMEM ;
for ( n = 0 ; n < pctl - > num_groups ; n + + ) {
struct mvebu_pinctrl_group * grp = & pctl - > groups [ n ] ;
for ( s = 0 ; s < grp - > num_settings ; s + + ) {
2013-03-16 12:44:32 +01:00
int ret ;
2012-09-13 17:41:43 +02:00
/* skip unsupported settings on this variant */
if ( pctl - > variant & &
! ( pctl - > variant & grp - > settings [ s ] . variant ) )
continue ;
/* check for unique functions and count groups */
2013-03-16 12:44:32 +01:00
ret = _add_function ( funcs , & funcsize ,
grp - > settings [ s ] . name ) ;
if ( ret = = - EOVERFLOW )
dev_err ( & pdev - > dev ,
" More functions than pins(%d) \n " ,
pctl - > desc . npins ) ;
if ( ret < 0 )
2012-09-13 17:41:43 +02:00
continue ;
num + + ;
}
}
pctl - > num_functions = num ;
pctl - > functions = funcs ;
for ( n = 0 ; n < pctl - > num_groups ; n + + ) {
struct mvebu_pinctrl_group * grp = & pctl - > groups [ n ] ;
for ( s = 0 ; s < grp - > num_settings ; s + + ) {
struct mvebu_pinctrl_function * f ;
const char * * groups ;
/* skip unsupported settings on this variant */
if ( pctl - > variant & &
! ( pctl - > variant & grp - > settings [ s ] . variant ) )
continue ;
f = mvebu_pinctrl_find_function_by_name ( pctl ,
grp - > settings [ s ] . name ) ;
/* allocate group name array if not done already */
if ( ! f - > groups ) {
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
f - > groups = devm_kcalloc ( & pdev - > dev ,
f - > num_groups ,
sizeof ( char * ) ,
2012-09-13 17:41:43 +02:00
GFP_KERNEL ) ;
if ( ! f - > groups )
return - ENOMEM ;
}
/* find next free group name and assign current name */
groups = f - > groups ;
while ( * groups )
groups + + ;
* groups = grp - > name ;
}
}
return 0 ;
}
2012-12-21 13:10:23 -08:00
int mvebu_pinctrl_probe ( struct platform_device * pdev )
2012-09-13 17:41:43 +02:00
{
struct mvebu_pinctrl_soc_info * soc = dev_get_platdata ( & pdev - > dev ) ;
struct mvebu_pinctrl * pctl ;
struct pinctrl_pin_desc * pdesc ;
unsigned gid , n , k ;
2014-01-30 23:38:12 +01:00
unsigned size , noname = 0 ;
char * noname_buf ;
void * p ;
2012-09-13 17:41:43 +02:00
int ret ;
if ( ! soc | | ! soc - > controls | | ! soc - > modes ) {
dev_err ( & pdev - > dev , " wrong pinctrl soc info \n " ) ;
return - EINVAL ;
}
pctl = devm_kzalloc ( & pdev - > dev , sizeof ( struct mvebu_pinctrl ) ,
GFP_KERNEL ) ;
2017-01-12 16:15:34 +01:00
if ( ! pctl )
2012-09-13 17:41:43 +02:00
return - ENOMEM ;
pctl - > desc . name = dev_name ( & pdev - > dev ) ;
pctl - > desc . owner = THIS_MODULE ;
pctl - > desc . pctlops = & mvebu_pinctrl_ops ;
pctl - > desc . pmxops = & mvebu_pinmux_ops ;
pctl - > desc . confops = & mvebu_pinconf_ops ;
pctl - > variant = soc - > variant ;
pctl - > dev = & pdev - > dev ;
platform_set_drvdata ( pdev , pctl ) ;
/* count controls and create names for mvebu generic
register controls ; also does sanity checks */
pctl - > num_groups = 0 ;
pctl - > desc . npins = 0 ;
for ( n = 0 ; n < soc - > ncontrols ; n + + ) {
2017-01-13 11:03:15 +00:00
const struct mvebu_mpp_ctrl * ctrl = & soc - > controls [ n ] ;
2012-09-13 17:41:43 +02:00
pctl - > desc . npins + = ctrl - > npins ;
2014-01-31 00:01:22 +01:00
/* initialize control's pins[] array */
2012-09-13 17:41:43 +02:00
for ( k = 0 ; k < ctrl - > npins ; k + + )
ctrl - > pins [ k ] = ctrl - > pid + k ;
2014-01-31 00:01:22 +01:00
/*
* We allow to pass controls with NULL name that we treat
* as a range of one - pin groups with generic mvebu register
* controls .
*/
if ( ! ctrl - > name ) {
pctl - > num_groups + = ctrl - > npins ;
noname + = ctrl - > npins ;
} else {
2012-09-13 17:41:43 +02:00
pctl - > num_groups + = 1 ;
}
}
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
pdesc = devm_kcalloc ( & pdev - > dev ,
pctl - > desc . npins ,
sizeof ( struct pinctrl_pin_desc ) ,
GFP_KERNEL ) ;
2017-01-12 16:15:34 +01:00
if ( ! pdesc )
2012-09-13 17:41:43 +02:00
return - ENOMEM ;
for ( n = 0 ; n < pctl - > desc . npins ; n + + )
pdesc [ n ] . number = n ;
pctl - > desc . pins = pdesc ;
2014-01-30 23:38:12 +01:00
/*
* allocate groups and name buffers for unnamed groups .
*/
size = pctl - > num_groups * sizeof ( * pctl - > groups ) + noname * 8 ;
p = devm_kzalloc ( & pdev - > dev , size , GFP_KERNEL ) ;
2017-12-19 22:30:36 +01:00
if ( ! p )
2012-09-13 17:41:43 +02:00
return - ENOMEM ;
2017-12-19 22:30:36 +01:00
2014-01-30 23:38:12 +01:00
pctl - > groups = p ;
noname_buf = p + pctl - > num_groups * sizeof ( * pctl - > groups ) ;
2012-09-13 17:41:43 +02:00
/* assign mpp controls to groups */
gid = 0 ;
for ( n = 0 ; n < soc - > ncontrols ; n + + ) {
2017-01-13 11:03:15 +00:00
const struct mvebu_mpp_ctrl * ctrl = & soc - > controls [ n ] ;
2017-01-13 11:03:20 +00:00
struct mvebu_mpp_ctrl_data * data = soc - > control_data ?
& soc - > control_data [ n ] : NULL ;
2012-09-13 17:41:43 +02:00
pctl - > groups [ gid ] . gid = gid ;
pctl - > groups [ gid ] . ctrl = ctrl ;
2017-01-13 11:03:20 +00:00
pctl - > groups [ gid ] . data = data ;
2012-09-13 17:41:43 +02:00
pctl - > groups [ gid ] . name = ctrl - > name ;
pctl - > groups [ gid ] . pins = ctrl - > pins ;
pctl - > groups [ gid ] . npins = ctrl - > npins ;
2014-01-31 00:01:22 +01:00
/*
* We treat unnamed controls as a range of one - pin groups
* with generic mvebu register controls . Use one group for
* each in this range and assign a default group name .
*/
if ( ! ctrl - > name ) {
2014-01-30 23:38:12 +01:00
pctl - > groups [ gid ] . name = noname_buf ;
2012-09-13 17:41:43 +02:00
pctl - > groups [ gid ] . npins = 1 ;
2014-01-30 23:38:12 +01:00
sprintf ( noname_buf , " mpp%d " , ctrl - > pid + 0 ) ;
noname_buf + = 8 ;
2012-09-13 17:41:43 +02:00
for ( k = 1 ; k < ctrl - > npins ; k + + ) {
gid + + ;
pctl - > groups [ gid ] . gid = gid ;
pctl - > groups [ gid ] . ctrl = ctrl ;
2017-01-13 11:03:20 +00:00
pctl - > groups [ gid ] . data = data ;
2014-01-30 23:38:12 +01:00
pctl - > groups [ gid ] . name = noname_buf ;
2012-09-13 17:41:43 +02:00
pctl - > groups [ gid ] . pins = & ctrl - > pins [ k ] ;
pctl - > groups [ gid ] . npins = 1 ;
2014-01-30 23:38:12 +01:00
sprintf ( noname_buf , " mpp%d " , ctrl - > pid + k ) ;
noname_buf + = 8 ;
2012-09-13 17:41:43 +02:00
}
}
gid + + ;
}
/* assign mpp modes to groups */
for ( n = 0 ; n < soc - > nmodes ; n + + ) {
struct mvebu_mpp_mode * mode = & soc - > modes [ n ] ;
2015-11-28 11:26:49 +01:00
struct mvebu_mpp_ctrl_setting * set = & mode - > settings [ 0 ] ;
struct mvebu_pinctrl_group * grp ;
2012-09-13 17:41:43 +02:00
unsigned num_settings ;
2016-02-01 23:48:23 +01:00
unsigned supp_settings ;
2012-09-13 17:41:43 +02:00
2016-02-01 23:48:23 +01:00
for ( num_settings = 0 , supp_settings = 0 ; ; set + + ) {
2012-09-13 17:41:43 +02:00
if ( ! set - > name )
break ;
2016-02-01 23:48:23 +01:00
num_settings + + ;
2012-09-13 17:41:43 +02:00
/* skip unsupported settings for this variant */
if ( pctl - > variant & & ! ( pctl - > variant & set - > variant ) )
continue ;
2016-02-01 23:48:23 +01:00
supp_settings + + ;
2015-11-28 11:26:49 +01:00
2012-09-13 17:41:43 +02:00
/* find gpio/gpo/gpi settings */
if ( strcmp ( set - > name , " gpio " ) = = 0 )
set - > flags = MVEBU_SETTING_GPI |
MVEBU_SETTING_GPO ;
else if ( strcmp ( set - > name , " gpo " ) = = 0 )
set - > flags = MVEBU_SETTING_GPO ;
else if ( strcmp ( set - > name , " gpi " ) = = 0 )
set - > flags = MVEBU_SETTING_GPI ;
}
2015-11-28 11:26:49 +01:00
/* skip modes with no settings for this variant */
2016-02-01 23:48:23 +01:00
if ( ! supp_settings )
2015-11-28 11:26:49 +01:00
continue ;
grp = mvebu_pinctrl_find_group_by_pid ( pctl , mode - > pid ) ;
if ( ! grp ) {
dev_warn ( & pdev - > dev , " unknown pinctrl group %d \n " ,
mode - > pid ) ;
continue ;
}
2012-09-13 17:41:43 +02:00
grp - > settings = mode - > settings ;
grp - > num_settings = num_settings ;
}
ret = mvebu_pinctrl_build_functions ( pdev , pctl ) ;
if ( ret ) {
dev_err ( & pdev - > dev , " unable to build functions \n " ) ;
return ret ;
}
2016-02-24 14:44:07 +05:30
pctl - > pctldev = devm_pinctrl_register ( & pdev - > dev , & pctl - > desc , pctl ) ;
2015-06-09 13:01:16 +09:00
if ( IS_ERR ( pctl - > pctldev ) ) {
2012-09-13 17:41:43 +02:00
dev_err ( & pdev - > dev , " unable to register pinctrl driver \n " ) ;
2015-06-09 13:01:16 +09:00
return PTR_ERR ( pctl - > pctldev ) ;
2012-09-13 17:41:43 +02:00
}
dev_info ( & pdev - > dev , " registered pinctrl driver \n " ) ;
/* register gpio ranges */
for ( n = 0 ; n < soc - > ngpioranges ; n + + )
pinctrl_add_gpio_range ( pctl - > pctldev , & soc - > gpioranges [ n ] ) ;
return 0 ;
}
2017-01-13 11:03:25 +00:00
/*
* mvebu_pinctrl_simple_mmio_probe - probe a simple mmio pinctrl
* @ pdev : platform device ( with platform data already attached )
*
* Initialise a simple ( single base address ) mmio pinctrl driver ,
* assigning the MMIO base address to all mvebu mpp ctrl instances .
*/
int mvebu_pinctrl_simple_mmio_probe ( struct platform_device * pdev )
{
struct mvebu_pinctrl_soc_info * soc = dev_get_platdata ( & pdev - > dev ) ;
struct mvebu_mpp_ctrl_data * mpp_data ;
void __iomem * base ;
int i ;
2019-10-16 22:12:17 +08:00
base = devm_platform_ioremap_resource ( pdev , 0 ) ;
2017-01-13 11:03:25 +00:00
if ( IS_ERR ( base ) )
return PTR_ERR ( base ) ;
mpp_data = devm_kcalloc ( & pdev - > dev , soc - > ncontrols , sizeof ( * mpp_data ) ,
GFP_KERNEL ) ;
if ( ! mpp_data )
return - ENOMEM ;
for ( i = 0 ; i < soc - > ncontrols ; i + + )
mpp_data [ i ] . base = base ;
soc - > control_data = mpp_data ;
return mvebu_pinctrl_probe ( pdev ) ;
}
2017-01-13 11:03:35 +00:00
int mvebu_regmap_mpp_ctrl_get ( struct mvebu_mpp_ctrl_data * data ,
unsigned int pid , unsigned long * config )
{
unsigned off = ( pid / MVEBU_MPPS_PER_REG ) * MVEBU_MPP_BITS ;
unsigned shift = ( pid % MVEBU_MPPS_PER_REG ) * MVEBU_MPP_BITS ;
unsigned int val ;
int err ;
err = regmap_read ( data - > regmap . map , data - > regmap . offset + off , & val ) ;
if ( err )
return err ;
* config = ( val > > shift ) & MVEBU_MPP_MASK ;
return 0 ;
}
int mvebu_regmap_mpp_ctrl_set ( struct mvebu_mpp_ctrl_data * data ,
unsigned int pid , unsigned long config )
{
unsigned off = ( pid / MVEBU_MPPS_PER_REG ) * MVEBU_MPP_BITS ;
unsigned shift = ( pid % MVEBU_MPPS_PER_REG ) * MVEBU_MPP_BITS ;
return regmap_update_bits ( data - > regmap . map , data - > regmap . offset + off ,
MVEBU_MPP_MASK < < shift , config < < shift ) ;
}
int mvebu_pinctrl_simple_regmap_probe ( struct platform_device * pdev ,
2017-06-12 17:34:53 +02:00
struct device * syscon_dev , u32 offset )
2017-01-13 11:03:35 +00:00
{
struct mvebu_pinctrl_soc_info * soc = dev_get_platdata ( & pdev - > dev ) ;
struct mvebu_mpp_ctrl_data * mpp_data ;
struct regmap * regmap ;
int i ;
regmap = syscon_node_to_regmap ( syscon_dev - > of_node ) ;
if ( IS_ERR ( regmap ) )
return PTR_ERR ( regmap ) ;
mpp_data = devm_kcalloc ( & pdev - > dev , soc - > ncontrols , sizeof ( * mpp_data ) ,
GFP_KERNEL ) ;
if ( ! mpp_data )
return - ENOMEM ;
for ( i = 0 ; i < soc - > ncontrols ; i + + ) {
mpp_data [ i ] . regmap . map = regmap ;
mpp_data [ i ] . regmap . offset = offset ;
}
soc - > control_data = mpp_data ;
return mvebu_pinctrl_probe ( pdev ) ;
}