Skip to content

Commit

Permalink
Merge branch 'i2c-for-linus' of git://aeryn.fluff.org.uk/bjdooks/linux
Browse files Browse the repository at this point in the history
* 'i2c-for-linus' of git://aeryn.fluff.org.uk/bjdooks/linux:
  i2c: Blackfin I2C Driver: Functional power management support
  i2c: Documentation: upgrading clients HOWTO
  i2c: S3C24XX I2C frequency scaling support.
  i2c: i2c_gpio: keep probe resident for hotplugged devices.
  i2c: S3C2410: Pass the I2C bus number via drivers platform data
  • Loading branch information
Linus Torvalds committed Jul 28, 2008
2 parents a013869 + 958585f commit 9ffc169
Show file tree
Hide file tree
Showing 5 changed files with 426 additions and 29 deletions.
281 changes: 281 additions & 0 deletions Documentation/i2c/upgrading-clients
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
Upgrading I2C Drivers to the new 2.6 Driver Model
=================================================

Ben Dooks <ben-linux@fluff.org>

Introduction
------------

This guide outlines how to alter existing Linux 2.6 client drivers from
the old to the new new binding methods.


Example old-style driver
------------------------


struct example_state {
struct i2c_client client;
....
};

static struct i2c_driver example_driver;

static unsigned short ignore[] = { I2C_CLIENT_END };
static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };

I2C_CLIENT_INSMOD;

static int example_attach(struct i2c_adapter *adap, int addr, int kind)
{
struct example_state *state;
struct device *dev = &adap->dev; /* to use for dev_ reports */
int ret;

state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
if (state == NULL) {
dev_err(dev, "failed to create our state\n");
return -ENOMEM;
}

example->client.addr = addr;
example->client.flags = 0;
example->client.adapter = adap;

i2c_set_clientdata(&state->i2c_client, state);
strlcpy(client->i2c_client.name, "example", I2C_NAME_SIZE);

ret = i2c_attach_client(&state->i2c_client);
if (ret < 0) {
dev_err(dev, "failed to attach client\n");
kfree(state);
return ret;
}

dev = &state->i2c_client.dev;

/* rest of the initialisation goes here. */

dev_info(dev, "example client created\n");

return 0;
}

static int __devexit example_detach(struct i2c_client *client)
{
struct example_state *state = i2c_get_clientdata(client);

i2c_detach_client(client);
kfree(state);
return 0;
}

static int example_attach_adapter(struct i2c_adapter *adap)
{
return i2c_probe(adap, &addr_data, example_attach);
}

static struct i2c_driver example_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "example",
},
.attach_adapter = example_attach_adapter,
.detach_client = __devexit_p(example_detach),
.suspend = example_suspend,
.resume = example_resume,
};


Updating the client
-------------------

The new style binding model will check against a list of supported
devices and their associated address supplied by the code registering
the busses. This means that the driver .attach_adapter and
.detach_adapter methods can be removed, along with the addr_data,
as follows:

- static struct i2c_driver example_driver;

- static unsigned short ignore[] = { I2C_CLIENT_END };
- static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };

- I2C_CLIENT_INSMOD;

- static int example_attach_adapter(struct i2c_adapter *adap)
- {
- return i2c_probe(adap, &addr_data, example_attach);
- }

static struct i2c_driver example_driver = {
- .attach_adapter = example_attach_adapter,
- .detach_client = __devexit_p(example_detach),
}

Add the probe and remove methods to the i2c_driver, as so:

static struct i2c_driver example_driver = {
+ .probe = example_probe,
+ .remove = __devexit_p(example_remove),
}

Change the example_attach method to accept the new parameters
which include the i2c_client that it will be working with:

- static int example_attach(struct i2c_adapter *adap, int addr, int kind)
+ static int example_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)

Change the name of example_attach to example_probe to align it with the
i2c_driver entry names. The rest of the probe routine will now need to be
changed as the i2c_client has already been setup for use.

The necessary client fields have already been setup before
the probe function is called, so the following client setup
can be removed:

- example->client.addr = addr;
- example->client.flags = 0;
- example->client.adapter = adap;
-
- strlcpy(client->i2c_client.name, "example", I2C_NAME_SIZE);

The i2c_set_clientdata is now:

- i2c_set_clientdata(&state->client, state);
+ i2c_set_clientdata(client, state);

The call to i2c_attach_client is no longer needed, if the probe
routine exits successfully, then the driver will be automatically
attached by the core. Change the probe routine as so:

- ret = i2c_attach_client(&state->i2c_client);
- if (ret < 0) {
- dev_err(dev, "failed to attach client\n");
- kfree(state);
- return ret;
- }


Remove the storage of 'struct i2c_client' from the 'struct example_state'
as we are provided with the i2c_client in our example_probe. Instead we
store a pointer to it for when it is needed.

struct example_state {
- struct i2c_client client;
+ struct i2c_client *client;

the new i2c client as so:

- struct device *dev = &adap->dev; /* to use for dev_ reports */
+ struct device *dev = &i2c_client->dev; /* to use for dev_ reports */

And remove the change after our client is attached, as the driver no
longer needs to register a new client structure with the core:

- dev = &state->i2c_client.dev;

In the probe routine, ensure that the new state has the client stored
in it:

static int example_probe(struct i2c_client *i2c_client,
const struct i2c_device_id *id)
{
struct example_state *state;
struct device *dev = &i2c_client->dev;
int ret;

state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
if (state == NULL) {
dev_err(dev, "failed to create our state\n");
return -ENOMEM;
}

+ state->client = i2c_client;

Update the detach method, by changing the name to _remove and
to delete the i2c_detach_client call. It is possible that you
can also remove the ret variable as it is not not needed for
any of the core functions.

- static int __devexit example_detach(struct i2c_client *client)
+ static int __devexit example_remove(struct i2c_client *client)
{
struct example_state *state = i2c_get_clientdata(client);

- i2c_detach_client(client);

And finally ensure that we have the correct ID table for the i2c-core
and other utilities:

+ struct i2c_device_id example_idtable[] = {
+ { "example", 0 },
+ { }
+};
+
+MODULE_DEVICE_TABLE(i2c, example_idtable);

static struct i2c_driver example_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "example",
},
+ .id_table = example_ids,


Our driver should now look like this:

struct example_state {
struct i2c_client *client;
....
};

static int example_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct example_state *state;
struct device *dev = &client->dev;

state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
if (state == NULL) {
dev_err(dev, "failed to create our state\n");
return -ENOMEM;
}

state->client = client;
i2c_set_clientdata(client, state);

/* rest of the initialisation goes here. */

dev_info(dev, "example client created\n");

return 0;
}

static int __devexit example_remove(struct i2c_client *client)
{
struct example_state *state = i2c_get_clientdata(client);

kfree(state);
return 0;
}

static struct i2c_device_id example_idtable[] = {
{ "example", 0 },
{ }
};

MODULE_DEVICE_TABLE(i2c, example_idtable);

static struct i2c_driver example_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "example",
},
.id_table = example_idtable,
.probe = example_probe,
.remove = __devexit_p(example_remove),
.suspend = example_suspend,
.resume = example_resume,
};
35 changes: 24 additions & 11 deletions drivers/i2c/busses/i2c-bfin-twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ struct bfin_twi_iface {
struct i2c_msg *pmsg;
int msg_num;
int cur_msg;
u16 saved_clkdiv;
u16 saved_control;
void __iomem *regs_base;
};

Expand Down Expand Up @@ -565,32 +567,43 @@ static u32 bfin_twi_functionality(struct i2c_adapter *adap)
I2C_FUNC_I2C;
}


static struct i2c_algorithm bfin_twi_algorithm = {
.master_xfer = bfin_twi_master_xfer,
.smbus_xfer = bfin_twi_smbus_xfer,
.functionality = bfin_twi_functionality,
};


static int i2c_bfin_twi_suspend(struct platform_device *dev, pm_message_t state)
static int i2c_bfin_twi_suspend(struct platform_device *pdev, pm_message_t state)
{
struct bfin_twi_iface *iface = platform_get_drvdata(dev);
struct bfin_twi_iface *iface = platform_get_drvdata(pdev);

iface->saved_clkdiv = read_CLKDIV(iface);
iface->saved_control = read_CONTROL(iface);

free_irq(iface->irq, iface);

/* Disable TWI */
write_CONTROL(iface, read_CONTROL(iface) & ~TWI_ENA);
SSYNC();
write_CONTROL(iface, iface->saved_control & ~TWI_ENA);

return 0;
}

static int i2c_bfin_twi_resume(struct platform_device *dev)
static int i2c_bfin_twi_resume(struct platform_device *pdev)
{
struct bfin_twi_iface *iface = platform_get_drvdata(dev);
struct bfin_twi_iface *iface = platform_get_drvdata(pdev);

/* Enable TWI */
write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
SSYNC();
int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
IRQF_DISABLED, pdev->name, iface);
if (rc) {
dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
return -ENODEV;
}

/* Resume TWI interface clock as specified */
write_CLKDIV(iface, iface->saved_clkdiv);

/* Resume TWI */
write_CONTROL(iface, iface->saved_control);

return 0;
}
Expand Down
9 changes: 5 additions & 4 deletions drivers/i2c/busses/i2c-gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static int i2c_gpio_getscl(void *data)
return gpio_get_value(pdata->scl_pin);
}

static int __init i2c_gpio_probe(struct platform_device *pdev)
static int __devinit i2c_gpio_probe(struct platform_device *pdev)
{
struct i2c_gpio_platform_data *pdata;
struct i2c_algo_bit_data *bit_data;
Expand Down Expand Up @@ -174,7 +174,7 @@ static int __init i2c_gpio_probe(struct platform_device *pdev)
return ret;
}

static int __exit i2c_gpio_remove(struct platform_device *pdev)
static int __devexit i2c_gpio_remove(struct platform_device *pdev)
{
struct i2c_gpio_platform_data *pdata;
struct i2c_adapter *adap;
Expand All @@ -196,14 +196,15 @@ static struct platform_driver i2c_gpio_driver = {
.name = "i2c-gpio",
.owner = THIS_MODULE,
},
.remove = __exit_p(i2c_gpio_remove),
.probe = i2c_gpio_probe,
.remove = __devexit_p(i2c_gpio_remove),
};

static int __init i2c_gpio_init(void)
{
int ret;

ret = platform_driver_probe(&i2c_gpio_driver, i2c_gpio_probe);
ret = platform_driver_register(&i2c_gpio_driver);
if (ret)
printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);

Expand Down
Loading

0 comments on commit 9ffc169

Please sign in to comment.