Commit 80a97ccd authored by Peter Ujfalusi's avatar Peter Ujfalusi Committed by Samuel Ortiz

mfd: twl-core: Collect global variables behind one private structure (global)

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: default avatarPeter Ujfalusi <peter.ujfalusi@ti.com>
Signed-off-by: default avatarSamuel Ortiz <sameo@linux.intel.com>
parent 6382a061
...@@ -141,33 +141,28 @@ ...@@ -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 */ /* Structure for each TWL4030/TWL6030 Slave */
struct twl_client { struct twl_client {
struct i2c_client *client; struct i2c_client *client;
struct regmap *regmap; struct regmap *regmap;
}; };
static struct twl_client *twl_modules;
/* mapping the module id to slave id and base address */ /* mapping the module id to slave id and base address */
struct twl_mapping { struct twl_mapping {
unsigned char sid; /* Slave ID */ unsigned char sid; /* Slave ID */
unsigned char base; /* base address */ 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[] = { static struct twl_mapping twl4030_map[] = {
/* /*
...@@ -300,6 +295,12 @@ static inline int twl_get_last_module(void) ...@@ -300,6 +295,12 @@ static inline int twl_get_last_module(void)
/* Exported Functions */ /* 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 * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
* @mod_no: module number * @mod_no: module number
...@@ -322,16 +323,17 @@ int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes) ...@@ -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); pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
return -EPERM; return -EPERM;
} }
if (unlikely(!inuse)) { if (unlikely(!twl_priv->ready)) {
pr_err("%s: not initialized\n", DRIVER_NAME); pr_err("%s: not initialized\n", DRIVER_NAME);
return -EPERM; return -EPERM;
} }
sid = twl_map[mod_no].sid; sid = twl_priv->twl_map[mod_no].sid;
twl = &twl_modules[sid]; twl = &twl_priv->twl_modules[sid];
ret = regmap_bulk_write(twl->regmap, twl_map[mod_no].base + reg, ret = regmap_bulk_write(twl->regmap,
value, num_bytes); twl_priv->twl_map[mod_no].base + reg, value,
num_bytes);
if (ret) if (ret)
pr_err("%s: Write failed (mod %d, reg 0x%02x count %d)\n", 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) ...@@ -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); pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
return -EPERM; return -EPERM;
} }
if (unlikely(!inuse)) { if (unlikely(!twl_priv->ready)) {
pr_err("%s: not initialized\n", DRIVER_NAME); pr_err("%s: not initialized\n", DRIVER_NAME);
return -EPERM; return -EPERM;
} }
sid = twl_map[mod_no].sid; sid = twl_priv->twl_map[mod_no].sid;
twl = &twl_modules[sid]; twl = &twl_priv->twl_modules[sid];
ret = regmap_bulk_read(twl->regmap, twl_map[mod_no].base + reg, ret = regmap_bulk_read(twl->regmap,
value, num_bytes); twl_priv->twl_map[mod_no].base + reg, value,
num_bytes);
if (ret) if (ret)
pr_err("%s: Read failed (mod %d, reg 0x%02x count %d)\n", pr_err("%s: Read failed (mod %d, reg 0x%02x count %d)\n",
...@@ -425,7 +428,7 @@ static int twl_read_idcode_register(void) ...@@ -425,7 +428,7 @@ static int twl_read_idcode_register(void)
goto fail; goto fail;
} }
err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode), err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_priv->twl_idcode),
REG_IDCODE_7_0, 4); REG_IDCODE_7_0, 4);
if (err) { if (err) {
pr_err("TWL4030: unable to read IDCODE -%d\n", err); pr_err("TWL4030: unable to read IDCODE -%d\n", err);
...@@ -446,7 +449,7 @@ static int twl_read_idcode_register(void) ...@@ -446,7 +449,7 @@ static int twl_read_idcode_register(void)
*/ */
int twl_get_type(void) int twl_get_type(void)
{ {
return TWL_SIL_TYPE(twl_idcode); return TWL_SIL_TYPE(twl_priv->twl_idcode);
} }
EXPORT_SYMBOL_GPL(twl_get_type); EXPORT_SYMBOL_GPL(twl_get_type);
...@@ -457,7 +460,7 @@ EXPORT_SYMBOL_GPL(twl_get_type); ...@@ -457,7 +460,7 @@ EXPORT_SYMBOL_GPL(twl_get_type);
*/ */
int twl_get_version(void) int twl_get_version(void)
{ {
return TWL_SIL_REV(twl_idcode); return TWL_SIL_REV(twl_priv->twl_idcode);
} }
EXPORT_SYMBOL_GPL(twl_get_version); EXPORT_SYMBOL_GPL(twl_get_version);
...@@ -506,8 +509,8 @@ add_numbered_child(unsigned mod_no, const char *name, int num, ...@@ -506,8 +509,8 @@ add_numbered_child(unsigned mod_no, const char *name, int num,
pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no); pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
return ERR_PTR(-EPERM); return ERR_PTR(-EPERM);
} }
sid = twl_map[mod_no].sid; sid = twl_priv->twl_map[mod_no].sid;
twl = &twl_modules[sid]; twl = &twl_priv->twl_modules[sid];
pdev = platform_device_alloc(name, num); pdev = platform_device_alloc(name, num);
if (!pdev) { if (!pdev) {
...@@ -1143,13 +1146,13 @@ static int twl_remove(struct i2c_client *client) ...@@ -1143,13 +1146,13 @@ static int twl_remove(struct i2c_client *client)
num_slaves = twl_get_num_slaves(); num_slaves = twl_get_num_slaves();
for (i = 0; i < num_slaves; i++) { for (i = 0; i < num_slaves; i++) {
struct twl_client *twl = &twl_modules[i]; struct twl_client *twl = &twl_priv->twl_modules[i];
if (twl->client && twl->client != client) if (twl->client && twl->client != client)
i2c_unregister_device(twl->client); i2c_unregister_device(twl->client);
twl_modules[i].client = NULL; twl->client = NULL;
} }
inuse = false; twl_priv->ready = false;
return 0; return 0;
} }
...@@ -1170,7 +1173,7 @@ twl_probe(struct i2c_client *client, const struct i2c_device_id *id) ...@@ -1170,7 +1173,7 @@ twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
return -EINVAL; return -EINVAL;
} }
if (inuse) { if (twl_priv) {
dev_dbg(&client->dev, "only one instance of %s allowed\n", dev_dbg(&client->dev, "only one instance of %s allowed\n",
DRIVER_NAME); DRIVER_NAME);
return -EBUSY; return -EBUSY;
...@@ -1194,31 +1197,38 @@ twl_probe(struct i2c_client *client, const struct i2c_device_id *id) ...@@ -1194,31 +1197,38 @@ twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
goto free; goto free;
} }
twl_priv = devm_kzalloc(&client->dev, sizeof(struct twl_private),
GFP_KERNEL);
if (!twl_priv) {
status = -ENOMEM;
goto free;
}
if ((id->driver_data) & TWL6030_CLASS) { if ((id->driver_data) & TWL6030_CLASS) {
twl_id = TWL6030_CLASS_ID; twl_priv->twl_id = TWL6030_CLASS_ID;
twl_map = &twl6030_map[0]; twl_priv->twl_map = &twl6030_map[0];
/* The charger base address is different in twl6025 */ /* The charger base address is different in twl6025 */
if ((id->driver_data) & TWL6025_SUBCLASS) if ((id->driver_data) & TWL6025_SUBCLASS)
twl_map[TWL_MODULE_MAIN_CHARGE].base = twl_priv->twl_map[TWL_MODULE_MAIN_CHARGE].base =
TWL6025_BASEADD_CHARGER; TWL6025_BASEADD_CHARGER;
twl_regmap_config = twl6030_regmap_config; twl_regmap_config = twl6030_regmap_config;
} else { } else {
twl_id = TWL4030_CLASS_ID; twl_priv->twl_id = TWL4030_CLASS_ID;
twl_map = &twl4030_map[0]; twl_priv->twl_map = &twl4030_map[0];
twl_regmap_config = twl4030_regmap_config; twl_regmap_config = twl4030_regmap_config;
} }
num_slaves = twl_get_num_slaves(); num_slaves = twl_get_num_slaves();
twl_modules = devm_kzalloc(&client->dev, twl_priv->twl_modules = devm_kzalloc(&client->dev,
sizeof(struct twl_client) * num_slaves, sizeof(struct twl_client) * num_slaves,
GFP_KERNEL); GFP_KERNEL);
if (!twl_modules) { if (!twl_priv->twl_modules) {
status = -ENOMEM; status = -ENOMEM;
goto free; goto free;
} }
for (i = 0; i < num_slaves; i++) { for (i = 0; i < num_slaves; i++) {
struct twl_client *twl = &twl_modules[i]; struct twl_client *twl = &twl_priv->twl_modules[i];
if (i == 0) { if (i == 0) {
twl->client = client; twl->client = client;
...@@ -1244,13 +1254,13 @@ twl_probe(struct i2c_client *client, const struct i2c_device_id *id) ...@@ -1244,13 +1254,13 @@ twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
} }
} }
inuse = true; twl_priv->ready = true;
/* setup clock framework */ /* setup clock framework */
clocks_init(&pdev->dev, pdata ? pdata->clock : NULL); clocks_init(&pdev->dev, pdata ? pdata->clock : NULL);
/* read TWL IDCODE Register */ /* read TWL IDCODE Register */
if (twl_id == TWL4030_CLASS_ID) { if (twl_class_is_4030()) {
status = twl_read_idcode_register(); status = twl_read_idcode_register();
WARN(status < 0, "Error: reading twl_idcode register value\n"); WARN(status < 0, "Error: reading twl_idcode register value\n");
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment