Re: [PATCH v2 09/11] mfd: twl-core: Collect global variables behindone private structure (global)

From: Jon Hunter
Date: Fri Feb 08 2013 - 13:57:16 EST



On 01/16/2013 07:53 AM, Peter Ujfalusi wrote:
> Gather the global variables under a single structure and allocate it with
> devm_kzalloc(). It is easier to see them and if in the future we try to add
> support for multiple instance of twl in the system it is going to be much
> simpler.
>
> Signed-off-by: Peter Ujfalusi <peter.ujfalusi@xxxxxx>
> ---
> drivers/mfd/twl-core.c | 104 +++++++++++++++++++++++++++----------------------
> 1 file changed, 57 insertions(+), 47 deletions(-)
>
> diff --git a/drivers/mfd/twl-core.c b/drivers/mfd/twl-core.c
> index 1827088..e2895a4 100644
> --- a/drivers/mfd/twl-core.c
> +++ b/drivers/mfd/twl-core.c
> @@ -141,33 +141,28 @@
>
> /*----------------------------------------------------------------------*/
>
> -/* is driver active, bound to a chip? */
> -static bool inuse;
> -
> -/* TWL IDCODE Register value */
> -static u32 twl_idcode;
> -
> -static unsigned int twl_id;
> -unsigned int twl_rev(void)
> -{
> - return twl_id;
> -}
> -EXPORT_SYMBOL(twl_rev);
> -
> /* Structure for each TWL4030/TWL6030 Slave */
> struct twl_client {
> struct i2c_client *client;
> struct regmap *regmap;
> };
>
> -static struct twl_client *twl_modules;
> -
> /* mapping the module id to slave id and base address */
> struct twl_mapping {
> unsigned char sid; /* Slave ID */
> unsigned char base; /* base address */
> };
> -static struct twl_mapping *twl_map;
> +
> +struct twl_private {
> + bool ready; /* The core driver is ready to be used */
> + u32 twl_idcode; /* TWL IDCODE Register value */
> + unsigned int twl_id;
> +
> + struct twl_mapping *twl_map;
> + struct twl_client *twl_modules;
> +};
> +
> +static struct twl_private *twl_priv;
>
> static struct twl_mapping twl4030_map[] = {
> /*
> @@ -300,6 +295,12 @@ static inline int twl_get_last_module(void)
>
> /* Exported Functions */
>
> +unsigned int twl_rev(void)
> +{
> + return twl_priv ? twl_priv->twl_id : 0;
> +}
> +EXPORT_SYMBOL(twl_rev);
> +
> /**
> * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
> * @mod_no: module number
> @@ -322,16 +323,17 @@ int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
> pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
> return -EPERM;
> }
> - if (unlikely(!inuse)) {
> + if (unlikely(!twl_priv->ready)) {

This is causing the kernel to panic on all my omap2 boards when booting linux-next
because twl_priv is not initialised yet.

> pr_err("%s: not initialized\n", DRIVER_NAME);
> return -EPERM;
> }
>
> - sid = twl_map[mod_no].sid;
> - twl = &twl_modules[sid];
> + sid = twl_priv->twl_map[mod_no].sid;
> + twl = &twl_priv->twl_modules[sid];
>
> - ret = regmap_bulk_write(twl->regmap, twl_map[mod_no].base + reg,
> - value, num_bytes);
> + ret = regmap_bulk_write(twl->regmap,
> + twl_priv->twl_map[mod_no].base + reg, value,
> + num_bytes);
>
> if (ret)
> pr_err("%s: Write failed (mod %d, reg 0x%02x count %d)\n",
> @@ -360,16 +362,17 @@ int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
> pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
> return -EPERM;
> }
> - if (unlikely(!inuse)) {
> + if (unlikely(!twl_priv->ready)) {

Same problem here.

Here is a fix ...