Re: [PATCH 19 5/5] Convert pfc8563 i2c driver from old style to new style

From: Laurent Pinchart
Date: Thu Apr 10 2008 - 05:14:29 EST


Hi Jon,

On Saturday 12 January 2008 03:47, Jon Smirl wrote:
> Convert pfc8563 i2c driver from old style to new style. The
> driver is also modified to support device tree names via the
> i2c mod alias mechanism.

The patch breaks the pfc8563 driver.

The old style driver allocated memory for a pcf8563 structure that
encapsulated an i2c_client instance. Various functions use container_of
on an i2c_client instance to get a pointer to the pcf8563 structure.

The new style driver gets rid of the pcf8563 structure allocation, as the
i2c_client structure is now allocated by I2C code.

Here is an incremental patch that fixes the issue.

From 1eac5a8e10e085c3a77c6ba7ed9dac9a39024915 Mon Sep 17 00:00:00 2001
From: Laurent Pinchart <laurentp@xxxxxxxxxxxxxxxxx>
Date: Thu, 10 Apr 2008 11:12:25 +0200
Subject: [PATCH] Fix pcf8563 breakage introduced by the conversion from old style to new style

The old style I2C driver used to allocate a pcf8563 instance that encapsulated
an i2c_client instance. The i2c_client instance is now allocated by I2C core.

This patch fixes the driver by storing the pcf8563 instance in the I2C client
data field.

Signed-off-by: Laurent Pinchart <laurentp@xxxxxxxxxxxxxxxxx>
---
drivers/rtc/rtc-pcf8563.c | 29 ++++++++++++++++++-----------
1 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/drivers/rtc/rtc-pcf8563.c b/drivers/rtc/rtc-pcf8563.c
index e1ea2a0..7aab24e 100644
--- a/drivers/rtc/rtc-pcf8563.c
+++ b/drivers/rtc/rtc-pcf8563.c
@@ -50,7 +50,7 @@
#define PCF8563_MO_C 0x80 /* century */

struct pcf8563 {
- struct i2c_client client;
+ struct rtc_device *rtc;
/*
* The meaning of MO_C bit varies by the chip type.
* From PCF8563 datasheet: this bit is toggled when the years
@@ -74,7 +74,7 @@ struct pcf8563 {
*/
static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm)
{
- struct pcf8563 *pcf8563 = container_of(client, struct pcf8563, client);
+ struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
unsigned char buf[13] = { PCF8563_REG_ST1 };

struct i2c_msg msgs[] = {
@@ -131,7 +131,7 @@ static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm)

static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm)
{
- struct pcf8563 *pcf8563 = container_of(client, struct pcf8563, client);
+ struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
int i, err;
unsigned char buf[9];

@@ -252,10 +252,10 @@ static const struct rtc_class_ops pcf8563_rtc_ops = {

static int pcf8563_remove(struct i2c_client *client)
{
- struct rtc_device *rtc = i2c_get_clientdata(client);
+ struct pcf8563 *pcf8563 = i2c_get_clientdata(client);

- if (rtc)
- rtc_device_unregister(rtc);
+ if (pcf8563->rtc)
+ rtc_device_unregister(pcf8563->rtc);

return 0;
}
@@ -274,26 +274,33 @@ static struct i2c_driver pcf8563_driver = {
.name = "rtc-pcf8563",
},
.id = I2C_DRIVERID_PCF8563,
- .probe = &pcf8563_probe,
- .remove = &pcf8563_remove,
+ .probe = &pcf8563_probe,
+ .remove = &pcf8563_remove,
.id_table = pcf8563_id,
};

static int pcf8563_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
- int result;
+ struct pcf8563 *pcf8563;
struct rtc_device *rtc;
+ int result;

result = pcf8563_validate_client(client);
if (result)
return result;

+ if ((pcf8563 = kzalloc(sizeof(*pcf8563), GFP_KERNEL)) == NULL)
+ return -ENOMEM;
+
rtc = rtc_device_register(pcf8563_driver.driver.name, &client->dev,
&pcf8563_rtc_ops, THIS_MODULE);
- if (IS_ERR(rtc))
+ if (IS_ERR(rtc)) {
+ kfree(pcf8563);
return PTR_ERR(rtc);
+ }

- i2c_set_clientdata(client, rtc);
+ pcf8563->rtc = rtc;
+ i2c_set_clientdata(client, pcf8563);

return 0;
}
--
1.5.0

--
Laurent Pinchart
CSE Semaphore Belgium

Chaussee de Bruxelles, 732A
B-1410 Waterloo
Belgium

T +32 (2) 387 42 59
F +32 (2) 387 42 75

Attachment: pgp00000.pgp
Description: PGP signature