2019-06-04 11:11:33 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2010-05-19 11:24:31 +04:00
/*
2014-02-01 10:36:28 +04:00
* LEDs driver for Freescale MC13783 / MC13892 / MC34708
2010-05-19 11:24:31 +04:00
*
* Copyright ( C ) 2010 Philippe Rétornaz
*
* Based on leds - da903x :
* Copyright ( C ) 2008 Compulab , Ltd .
* Mike Rapoport < mike @ compulab . co . il >
*
* Copyright ( C ) 2006 - 2008 Marvell International Ltd .
* Eric Miao < eric . miao @ marvell . com >
*/
# include <linux/module.h>
# include <linux/kernel.h>
# include <linux/platform_device.h>
# include <linux/leds.h>
2014-02-01 10:37:23 +04:00
# include <linux/of.h>
2011-08-24 17:28:20 +04:00
# include <linux/mfd/mc13xxx.h>
2010-05-19 11:24:31 +04:00
2013-06-10 20:59:30 +04:00
struct mc13xxx_led_devtype {
int led_min ;
int led_max ;
int num_regs ;
2014-02-01 10:36:28 +04:00
u32 ledctrl_base ;
2013-06-10 20:59:30 +04:00
} ;
struct mc13xxx_led {
2010-05-19 11:24:31 +04:00
struct led_classdev cdev ;
int id ;
2014-02-01 10:36:28 +04:00
struct mc13xxx_leds * leds ;
2010-05-19 11:24:31 +04:00
} ;
2013-06-10 20:59:30 +04:00
struct mc13xxx_leds {
2014-02-01 10:36:28 +04:00
struct mc13xxx * master ;
2013-06-10 20:59:30 +04:00
struct mc13xxx_led_devtype * devtype ;
int num_leds ;
2014-02-01 10:37:23 +04:00
struct mc13xxx_led * led ;
2013-06-10 20:59:30 +04:00
} ;
2014-02-01 10:36:30 +04:00
static unsigned int mc13xxx_max_brightness ( int id )
{
if ( id > = MC13783_LED_MD & & id < = MC13783_LED_KP )
return 0x0f ;
else if ( id > = MC13783_LED_R1 & & id < = MC13783_LED_B3 )
return 0x1f ;
return 0x3f ;
}
2015-08-20 13:52:08 +03:00
static int mc13xxx_led_set ( struct led_classdev * led_cdev ,
enum led_brightness value )
2010-05-19 11:24:31 +04:00
{
2015-08-20 13:52:08 +03:00
struct mc13xxx_led * led =
container_of ( led_cdev , struct mc13xxx_led , cdev ) ;
2014-02-01 10:36:28 +04:00
struct mc13xxx_leds * leds = led - > leds ;
2014-02-01 10:36:30 +04:00
unsigned int reg , bank , off , shift ;
2010-05-19 11:24:31 +04:00
switch ( led - > id ) {
case MC13783_LED_MD :
case MC13783_LED_AD :
case MC13783_LED_KP :
2014-02-01 10:36:28 +04:00
reg = 2 ;
shift = 9 + ( led - > id - MC13783_LED_MD ) * 4 ;
2010-05-19 11:24:31 +04:00
break ;
case MC13783_LED_R1 :
case MC13783_LED_G1 :
case MC13783_LED_B1 :
case MC13783_LED_R2 :
case MC13783_LED_G2 :
case MC13783_LED_B2 :
case MC13783_LED_R3 :
case MC13783_LED_G3 :
case MC13783_LED_B3 :
off = led - > id - MC13783_LED_R1 ;
2013-06-10 20:59:30 +04:00
bank = off / 3 ;
2014-02-01 10:36:28 +04:00
reg = 3 + bank ;
2013-06-10 20:59:30 +04:00
shift = ( off - bank * 3 ) * 5 + 6 ;
2010-05-19 11:24:31 +04:00
break ;
2013-06-10 20:59:31 +04:00
case MC13892_LED_MD :
case MC13892_LED_AD :
case MC13892_LED_KP :
2016-10-09 16:37:47 +03:00
off = led - > id - MC13892_LED_MD ;
reg = off / 2 ;
shift = 3 + ( off - reg * 2 ) * 12 ;
2013-06-10 20:59:31 +04:00
break ;
case MC13892_LED_R :
case MC13892_LED_G :
case MC13892_LED_B :
off = led - > id - MC13892_LED_R ;
bank = off / 2 ;
2014-02-01 10:36:28 +04:00
reg = 2 + bank ;
2013-06-10 20:59:31 +04:00
shift = ( off - bank * 2 ) * 12 + 3 ;
break ;
2014-02-01 10:36:28 +04:00
case MC34708_LED_R :
case MC34708_LED_G :
reg = 0 ;
shift = 3 + ( led - > id - MC34708_LED_R ) * 12 ;
break ;
2013-06-10 20:59:30 +04:00
default :
BUG ( ) ;
2010-05-19 11:24:31 +04:00
}
2015-08-20 13:52:08 +03:00
return mc13xxx_reg_rmw ( leds - > master , leds - > devtype - > ledctrl_base + reg ,
2014-02-01 10:36:30 +04:00
mc13xxx_max_brightness ( led - > id ) < < shift ,
2015-08-20 13:52:08 +03:00
value < < shift ) ;
2010-05-19 11:24:31 +04:00
}
2014-02-01 10:37:23 +04:00
# ifdef CONFIG_OF
static struct mc13xxx_leds_platform_data __init * mc13xxx_led_probe_dt (
struct platform_device * pdev )
{
struct mc13xxx_leds * leds = platform_get_drvdata ( pdev ) ;
struct mc13xxx_leds_platform_data * pdata ;
struct device_node * parent , * child ;
struct device * dev = & pdev - > dev ;
int i = 0 , ret = - ENODATA ;
pdata = devm_kzalloc ( dev , sizeof ( * pdata ) , GFP_KERNEL ) ;
if ( ! pdata )
return ERR_PTR ( - ENOMEM ) ;
2020-09-18 01:32:54 +03:00
parent = of_get_child_by_name ( dev_of_node ( dev - > parent ) , " leds " ) ;
2014-02-01 10:37:23 +04:00
if ( ! parent )
goto out_node_put ;
ret = of_property_read_u32_array ( parent , " led-control " ,
pdata - > led_control ,
leds - > devtype - > num_regs ) ;
if ( ret )
goto out_node_put ;
2020-09-18 01:32:56 +03:00
pdata - > num_leds = of_get_available_child_count ( parent ) ;
2014-02-01 10:37:23 +04:00
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
pdata - > led = devm_kcalloc ( dev , pdata - > num_leds , sizeof ( * pdata - > led ) ,
2014-02-01 10:37:23 +04:00
GFP_KERNEL ) ;
if ( ! pdata - > led ) {
ret = - ENOMEM ;
goto out_node_put ;
}
2020-09-18 01:32:56 +03:00
for_each_available_child_of_node ( parent , child ) {
2014-02-01 10:37:23 +04:00
const char * str ;
u32 tmp ;
if ( of_property_read_u32 ( child , " reg " , & tmp ) )
continue ;
pdata - > led [ i ] . id = leds - > devtype - > led_min + tmp ;
if ( ! of_property_read_string ( child , " label " , & str ) )
pdata - > led [ i ] . name = str ;
if ( ! of_property_read_string ( child , " linux,default-trigger " ,
& str ) )
pdata - > led [ i ] . default_trigger = str ;
i + + ;
}
pdata - > num_leds = i ;
ret = i > 0 ? 0 : - ENODATA ;
out_node_put :
of_node_put ( parent ) ;
return ret ? ERR_PTR ( ret ) : pdata ;
}
# else
static inline struct mc13xxx_leds_platform_data __init * mc13xxx_led_probe_dt (
struct platform_device * pdev )
{
return ERR_PTR ( - ENOSYS ) ;
}
# endif
2013-06-10 20:59:30 +04:00
static int __init mc13xxx_led_probe ( struct platform_device * pdev )
2010-05-19 11:24:31 +04:00
{
2014-02-01 10:36:28 +04:00
struct device * dev = & pdev - > dev ;
struct mc13xxx_leds_platform_data * pdata = dev_get_platdata ( dev ) ;
struct mc13xxx * mcdev = dev_get_drvdata ( dev - > parent ) ;
2013-06-10 20:59:30 +04:00
struct mc13xxx_led_devtype * devtype =
( struct mc13xxx_led_devtype * ) pdev - > id_entry - > driver_data ;
struct mc13xxx_leds * leds ;
2014-02-01 10:37:23 +04:00
int i , id , ret = - ENODATA ;
2014-02-01 10:36:28 +04:00
u32 init_led = 0 ;
2013-06-10 20:59:30 +04:00
2014-02-01 10:37:23 +04:00
leds = devm_kzalloc ( dev , sizeof ( * leds ) , GFP_KERNEL ) ;
2013-06-10 20:59:30 +04:00
if ( ! leds )
2010-05-19 11:24:31 +04:00
return - ENOMEM ;
2013-06-10 20:59:30 +04:00
leds - > devtype = devtype ;
2014-02-01 10:36:28 +04:00
leds - > master = mcdev ;
2013-06-10 20:59:30 +04:00
platform_set_drvdata ( pdev , leds ) ;
2020-09-18 01:32:54 +03:00
if ( dev_of_node ( dev - > parent ) ) {
2014-02-01 10:37:23 +04:00
pdata = mc13xxx_led_probe_dt ( pdev ) ;
if ( IS_ERR ( pdata ) )
return PTR_ERR ( pdata ) ;
} else if ( ! pdata )
return - ENODATA ;
leds - > num_leds = pdata - > num_leds ;
if ( ( leds - > num_leds < 1 ) | |
( leds - > num_leds > ( devtype - > led_max - devtype - > led_min + 1 ) ) ) {
dev_err ( dev , " Invalid LED count %d \n " , leds - > num_leds ) ;
return - EINVAL ;
}
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
leds - > led = devm_kcalloc ( dev , leds - > num_leds , sizeof ( * leds - > led ) ,
2014-02-01 10:37:23 +04:00
GFP_KERNEL ) ;
if ( ! leds - > led )
return - ENOMEM ;
2013-06-10 20:59:30 +04:00
for ( i = 0 ; i < devtype - > num_regs ; i + + ) {
2014-02-01 10:36:28 +04:00
ret = mc13xxx_reg_write ( mcdev , leds - > devtype - > ledctrl_base + i ,
pdata - > led_control [ i ] ) ;
2013-06-10 20:59:30 +04:00
if ( ret )
2013-12-07 10:22:19 +04:00
return ret ;
2010-05-19 11:24:31 +04:00
}
2014-02-01 10:37:23 +04:00
for ( i = 0 ; i < leds - > num_leds ; i + + ) {
2013-06-10 20:59:30 +04:00
const char * name , * trig ;
2010-05-19 11:24:31 +04:00
2013-06-10 20:59:30 +04:00
ret = - EINVAL ;
id = pdata - > led [ i ] . id ;
name = pdata - > led [ i ] . name ;
trig = pdata - > led [ i ] . default_trigger ;
if ( ( id > devtype - > led_max ) | | ( id < devtype - > led_min ) ) {
2014-02-01 10:36:28 +04:00
dev_err ( dev , " Invalid ID %i \n " , id ) ;
2013-06-10 20:59:30 +04:00
break ;
2010-05-19 11:24:31 +04:00
}
2013-06-10 20:59:30 +04:00
if ( init_led & ( 1 < < id ) ) {
2014-02-01 10:36:28 +04:00
dev_warn ( dev , " LED %i already initialized \n " , id ) ;
2013-06-10 20:59:30 +04:00
break ;
2010-05-19 11:24:31 +04:00
}
2013-06-10 20:59:30 +04:00
init_led | = 1 < < id ;
leds - > led [ i ] . id = id ;
2014-02-01 10:36:28 +04:00
leds - > led [ i ] . leds = leds ;
2013-06-10 20:59:30 +04:00
leds - > led [ i ] . cdev . name = name ;
leds - > led [ i ] . cdev . default_trigger = trig ;
2014-02-01 10:36:29 +04:00
leds - > led [ i ] . cdev . flags = LED_CORE_SUSPENDRESUME ;
2015-08-20 13:52:08 +03:00
leds - > led [ i ] . cdev . brightness_set_blocking = mc13xxx_led_set ;
2014-02-01 10:36:30 +04:00
leds - > led [ i ] . cdev . max_brightness = mc13xxx_max_brightness ( id ) ;
2010-05-19 11:24:31 +04:00
2014-02-01 10:36:28 +04:00
ret = led_classdev_register ( dev - > parent , & leds - > led [ i ] . cdev ) ;
2010-05-19 11:24:31 +04:00
if ( ret ) {
2014-02-01 10:36:28 +04:00
dev_err ( dev , " Failed to register LED %i \n " , id ) ;
2013-06-10 20:59:30 +04:00
break ;
2010-05-19 11:24:31 +04:00
}
}
2013-06-10 20:59:30 +04:00
if ( ret )
2015-08-20 13:52:08 +03:00
while ( - - i > = 0 )
2013-06-10 20:59:30 +04:00
led_classdev_unregister ( & leds - > led [ i ] . cdev ) ;
2010-05-19 11:24:31 +04:00
return ret ;
}
2023-09-17 16:09:47 +03:00
static void mc13xxx_led_remove ( struct platform_device * pdev )
2010-05-19 11:24:31 +04:00
{
2013-06-10 20:59:30 +04:00
struct mc13xxx_leds * leds = platform_get_drvdata ( pdev ) ;
2010-05-19 11:24:31 +04:00
int i ;
2015-08-20 13:52:08 +03:00
for ( i = 0 ; i < leds - > num_leds ; i + + )
2013-06-10 20:59:30 +04:00
led_classdev_unregister ( & leds - > led [ i ] . cdev ) ;
2010-05-19 11:24:31 +04:00
}
2013-06-10 20:59:30 +04:00
static const struct mc13xxx_led_devtype mc13783_led_devtype = {
. led_min = MC13783_LED_MD ,
. led_max = MC13783_LED_B3 ,
. num_regs = 6 ,
2014-02-01 10:36:28 +04:00
. ledctrl_base = 51 ,
2013-06-10 20:59:30 +04:00
} ;
2013-06-10 20:59:31 +04:00
static const struct mc13xxx_led_devtype mc13892_led_devtype = {
. led_min = MC13892_LED_MD ,
. led_max = MC13892_LED_B ,
. num_regs = 4 ,
2014-02-01 10:36:28 +04:00
. ledctrl_base = 51 ,
} ;
static const struct mc13xxx_led_devtype mc34708_led_devtype = {
. led_min = MC34708_LED_R ,
. led_max = MC34708_LED_G ,
. num_regs = 1 ,
. ledctrl_base = 54 ,
2013-06-10 20:59:31 +04:00
} ;
2013-06-10 20:59:30 +04:00
static const struct platform_device_id mc13xxx_led_id_table [ ] = {
{ " mc13783-led " , ( kernel_ulong_t ) & mc13783_led_devtype , } ,
2013-06-10 20:59:31 +04:00
{ " mc13892-led " , ( kernel_ulong_t ) & mc13892_led_devtype , } ,
2014-02-01 10:36:28 +04:00
{ " mc34708-led " , ( kernel_ulong_t ) & mc34708_led_devtype , } ,
2013-06-10 20:59:30 +04:00
{ }
} ;
MODULE_DEVICE_TABLE ( platform , mc13xxx_led_id_table ) ;
static struct platform_driver mc13xxx_led_driver = {
2010-05-19 11:24:31 +04:00
. driver = {
2013-06-10 20:59:30 +04:00
. name = " mc13xxx-led " ,
2010-05-19 11:24:31 +04:00
} ,
2023-09-17 16:09:47 +03:00
. remove_new = mc13xxx_led_remove ,
2013-06-10 20:59:30 +04:00
. id_table = mc13xxx_led_id_table ,
2010-05-19 11:24:31 +04:00
} ;
2013-06-10 20:59:30 +04:00
module_platform_driver_probe ( mc13xxx_led_driver , mc13xxx_led_probe ) ;
2010-05-19 11:24:31 +04:00
2013-06-10 20:59:30 +04:00
MODULE_DESCRIPTION ( " LEDs driver for Freescale MC13XXX PMIC " ) ;
2010-05-19 11:24:31 +04:00
MODULE_AUTHOR ( " Philippe Retornaz <philippe.retornaz@epfl.ch> " ) ;
MODULE_LICENSE ( " GPL " ) ;