* [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs
@ 2009-03-02 11:40 Mike Rapoport
2009-03-02 11:40 ` [PATCH] rtc-v3020: coding style cleanup Mike Rapoport
2009-03-02 11:40 ` [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs Mike Rapoport
0 siblings, 2 replies; 11+ messages in thread
From: Mike Rapoport @ 2009-03-02 11:40 UTC (permalink / raw)
To: alessandro.zummo; +Cc: rtc-linux, linux-kernel, raph, Mike Rapoport
The v3020 RTC can be connected to GPIOs as well as to memory-like interface.
Add ability to use GPIO bit-bang for v3020 read-write access.
The first patch in the serie is just coding style cleanup.
Mike Rapoport (2):
rtc-v3020: coding style cleanup
rtc-v3020: add ability to access v3020 chip with GPIOs
drivers/rtc/rtc-v3020.c | 230 ++++++++++++++++++++++++++++++++++++--------
include/linux/rtc-v3020.h | 6 +
2 files changed, 194 insertions(+), 42 deletions(-)
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH] rtc-v3020: coding style cleanup
2009-03-02 11:40 [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs Mike Rapoport
@ 2009-03-02 11:40 ` Mike Rapoport
2009-03-02 11:43 ` Alessandro Zummo
2009-03-02 11:40 ` [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs Mike Rapoport
1 sibling, 1 reply; 11+ messages in thread
From: Mike Rapoport @ 2009-03-02 11:40 UTC (permalink / raw)
To: alessandro.zummo; +Cc: rtc-linux, linux-kernel, raph, Mike Rapoport
Signed-off-by: Mike Rapoport <mike@compulab.co.il>
---
drivers/rtc/rtc-v3020.c | 40 ++++++++++++++++++----------------------
1 files changed, 18 insertions(+), 22 deletions(-)
diff --git a/drivers/rtc/rtc-v3020.c b/drivers/rtc/rtc-v3020.c
index 14d4f03..66955cc 100644
--- a/drivers/rtc/rtc-v3020.c
+++ b/drivers/rtc/rtc-v3020.c
@@ -28,7 +28,7 @@
#include <linux/rtc-v3020.h>
#include <linux/delay.h>
-#include <asm/io.h>
+#include <linux/io.h>
#undef DEBUG
@@ -63,7 +63,7 @@ static void v3020_set_reg(struct v3020 *chip, unsigned char address,
static unsigned char v3020_get_reg(struct v3020 *chip, unsigned char address)
{
- unsigned int data=0;
+ unsigned int data = 0;
int i;
for (i = 0; i < 4; i++) {
@@ -106,16 +106,14 @@ static int v3020_read_time(struct device *dev, struct rtc_time *dt)
tmp = v3020_get_reg(chip, V3020_YEAR);
dt->tm_year = bcd2bin(tmp)+100;
-#ifdef DEBUG
- printk("\n%s : Read RTC values\n",__func__);
- printk("tm_hour: %i\n",dt->tm_hour);
- printk("tm_min : %i\n",dt->tm_min);
- printk("tm_sec : %i\n",dt->tm_sec);
- printk("tm_year: %i\n",dt->tm_year);
- printk("tm_mon : %i\n",dt->tm_mon);
- printk("tm_mday: %i\n",dt->tm_mday);
- printk("tm_wday: %i\n",dt->tm_wday);
-#endif
+ dev_dbg(dev, "\n%s : Read RTC values\n", __func__);
+ dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
+ dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
+ dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
+ dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
+ dev_dbg(dev, "tm_mon : %i\n", dt->tm_mon);
+ dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
+ dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
return 0;
}
@@ -125,15 +123,13 @@ static int v3020_set_time(struct device *dev, struct rtc_time *dt)
{
struct v3020 *chip = dev_get_drvdata(dev);
-#ifdef DEBUG
- printk("\n%s : Setting RTC values\n",__func__);
- printk("tm_sec : %i\n",dt->tm_sec);
- printk("tm_min : %i\n",dt->tm_min);
- printk("tm_hour: %i\n",dt->tm_hour);
- printk("tm_mday: %i\n",dt->tm_mday);
- printk("tm_wday: %i\n",dt->tm_wday);
- printk("tm_year: %i\n",dt->tm_year);
-#endif
+ dev_dbg(dev, "\n%s : Setting RTC values\n", __func__);
+ dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
+ dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
+ dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
+ dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
+ dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
+ dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
/* Write all the values to ram... */
v3020_set_reg(chip, V3020_SECONDS, bin2bcd(dt->tm_sec));
@@ -191,7 +187,7 @@ static int rtc_probe(struct platform_device *pdev)
/* Test chip by doing a write/read sequence
* to the chip ram */
v3020_set_reg(chip, V3020_SECONDS, 0x33);
- if(v3020_get_reg(chip, V3020_SECONDS) != 0x33) {
+ if (v3020_get_reg(chip, V3020_SECONDS) != 0x33) {
retval = -ENODEV;
goto err_io;
}
--
1.5.6.4
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs
2009-03-02 11:40 [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs Mike Rapoport
2009-03-02 11:40 ` [PATCH] rtc-v3020: coding style cleanup Mike Rapoport
@ 2009-03-02 11:40 ` Mike Rapoport
2009-03-02 11:47 ` Alessandro Zummo
2009-03-02 22:36 ` Andrew Morton
1 sibling, 2 replies; 11+ messages in thread
From: Mike Rapoport @ 2009-03-02 11:40 UTC (permalink / raw)
To: alessandro.zummo; +Cc: rtc-linux, linux-kernel, raph, Mike Rapoport
The v3020 RTC can be connected to GPIOs as well as to memory-like interface.
Add ability to use GPIO bit-bang for v3020 read-write access.
Signed-off-by: Mike Rapoport <mike@compulab.co.il>
---
drivers/rtc/rtc-v3020.c | 190 ++++++++++++++++++++++++++++++++++++++++-----
include/linux/rtc-v3020.h | 6 ++
2 files changed, 176 insertions(+), 20 deletions(-)
diff --git a/drivers/rtc/rtc-v3020.c b/drivers/rtc/rtc-v3020.c
index 66955cc..4c330f2 100644
--- a/drivers/rtc/rtc-v3020.c
+++ b/drivers/rtc/rtc-v3020.c
@@ -27,17 +27,162 @@
#include <linux/bcd.h>
#include <linux/rtc-v3020.h>
#include <linux/delay.h>
+#include <linux/gpio.h>
#include <linux/io.h>
#undef DEBUG
+struct v3020;
+
+struct v3020_chip_ops {
+ int (*map_io)(struct v3020 *chip, struct platform_device *pdev,
+ struct v3020_platform_data *pdata);
+ void (*unmap_io)(struct v3020 *chip);
+ unsigned char (*read_bit)(struct v3020 *chip);
+ void (*write_bit)(struct v3020 *chip, unsigned char bit);
+};
+
+#define V3020_CS 0
+#define V3020_WR 1
+#define V3020_RD 2
+#define V3020_IO 3
+
+struct v3020_gpio {
+ const char *name;
+ unsigned int gpio;
+};
+
struct v3020 {
+ /* MMIO access */
void __iomem *ioaddress;
int leftshift;
+
+ /* GPIO access */
+ struct v3020_gpio *gpio;
+
+ struct v3020_chip_ops *ops;
+
struct rtc_device *rtc;
};
+
+static int v3020_mmio_map(struct v3020 *chip, struct platform_device *pdev,
+ struct v3020_platform_data *pdata)
+{
+ if (pdev->num_resources != 1)
+ return -EBUSY;
+
+ if (pdev->resource[0].flags != IORESOURCE_MEM)
+ return -EBUSY;
+
+ chip->leftshift = pdata->leftshift;
+ chip->ioaddress = ioremap(pdev->resource[0].start, 1);
+ if (chip->ioaddress == NULL)
+ return -EBUSY;
+
+ return 0;
+}
+
+static void v3020_mmio_unmap(struct v3020 *chip)
+{
+ iounmap(chip->ioaddress);
+}
+
+static void v3020_mmio_write_bit(struct v3020 *chip, unsigned char bit)
+{
+ writel(bit << chip->leftshift, chip->ioaddress);
+}
+
+static unsigned char v3020_mmio_read_bit(struct v3020 *chip)
+{
+ return readl(chip->ioaddress) & (1 << chip->leftshift);
+}
+
+static struct v3020_chip_ops v3020_mmio_ops = {
+ .map_io = v3020_mmio_map,
+ .unmap_io = v3020_mmio_unmap,
+ .read_bit = v3020_mmio_read_bit,
+ .write_bit = v3020_mmio_write_bit,
+};
+
+static struct v3020_gpio v3020_gpio[] = {
+ { "RTC CS", 0 },
+ { "RTC WR", 0 },
+ { "RTC RD", 0 },
+ { "RTC IO", 0 },
+};
+
+static int v3020_gpio_map(struct v3020 *chip, struct platform_device *pdev,
+ struct v3020_platform_data *pdata)
+{
+ int i, err;
+
+ v3020_gpio[V3020_CS].gpio = pdata->gpio_cs;
+ v3020_gpio[V3020_WR].gpio = pdata->gpio_wr;
+ v3020_gpio[V3020_RD].gpio = pdata->gpio_rd;
+ v3020_gpio[V3020_IO].gpio = pdata->gpio_io;
+
+ for (i = 0; i < ARRAY_SIZE(v3020_gpio); i++) {
+ err = gpio_request(v3020_gpio[i].gpio, v3020_gpio[i].name);
+ if (err)
+ goto err_request;
+
+ gpio_direction_output(v3020_gpio[i].gpio, 1);
+ }
+
+ chip->gpio = v3020_gpio;
+
+ return 0;
+
+err_request:
+ for (; i >= 0; i--)
+ gpio_free(v3020_gpio[i].gpio);
+
+ return err;
+}
+
+static void v3020_gpio_unmap(struct v3020 *chip)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(v3020_gpio); i++)
+ gpio_free(v3020_gpio[i].gpio);
+}
+
+static void v3020_gpio_write_bit(struct v3020 *chip, unsigned char bit)
+{
+ gpio_direction_output(chip->gpio[V3020_IO].gpio, bit);
+ gpio_set_value(chip->gpio[V3020_CS].gpio, 0);
+ gpio_set_value(chip->gpio[V3020_WR].gpio, 0);
+ udelay(1);
+ gpio_set_value(chip->gpio[V3020_WR].gpio, 1);
+ gpio_set_value(chip->gpio[V3020_CS].gpio, 1);
+}
+
+static unsigned char v3020_gpio_read_bit(struct v3020 *chip)
+{
+ int bit;
+
+ gpio_direction_input(chip->gpio[V3020_IO].gpio);
+ gpio_set_value(chip->gpio[V3020_CS].gpio, 0);
+ gpio_set_value(chip->gpio[V3020_RD].gpio, 0);
+ udelay(1);
+ bit = !!gpio_get_value(chip->gpio[V3020_IO].gpio);
+ udelay(1);
+ gpio_set_value(chip->gpio[V3020_RD].gpio, 1);
+ gpio_set_value(chip->gpio[V3020_CS].gpio, 1);
+
+ return bit;
+}
+
+static struct v3020_chip_ops v3020_gpio_ops = {
+ .map_io = v3020_gpio_map,
+ .unmap_io = v3020_gpio_unmap,
+ .read_bit = v3020_gpio_read_bit,
+ .write_bit = v3020_gpio_write_bit,
+};
+
static void v3020_set_reg(struct v3020 *chip, unsigned char address,
unsigned char data)
{
@@ -46,7 +191,7 @@ static void v3020_set_reg(struct v3020 *chip, unsigned char address,
tmp = address;
for (i = 0; i < 4; i++) {
- writel((tmp & 1) << chip->leftshift, chip->ioaddress);
+ chip->ops->write_bit(chip, (tmp & 1));
tmp >>= 1;
udelay(1);
}
@@ -54,7 +199,7 @@ static void v3020_set_reg(struct v3020 *chip, unsigned char address,
/* Commands dont have data */
if (!V3020_IS_COMMAND(address)) {
for (i = 0; i < 8; i++) {
- writel((data & 1) << chip->leftshift, chip->ioaddress);
+ chip->ops->write_bit(chip, (data & 1));
data >>= 1;
udelay(1);
}
@@ -67,14 +212,14 @@ static unsigned char v3020_get_reg(struct v3020 *chip, unsigned char address)
int i;
for (i = 0; i < 4; i++) {
- writel((address & 1) << chip->leftshift, chip->ioaddress);
+ chip->ops->write_bit(chip, (address & 1));
address >>= 1;
udelay(1);
}
for (i = 0; i < 8; i++) {
data >>= 1;
- if (readl(chip->ioaddress) & (1 << chip->leftshift))
+ if (chip->ops->read_bit(chip))
data |= 0x80;
udelay(1);
}
@@ -164,25 +309,23 @@ static int rtc_probe(struct platform_device *pdev)
int i;
int temp;
- if (pdev->num_resources != 1)
- return -EBUSY;
-
- if (pdev->resource[0].flags != IORESOURCE_MEM)
- return -EBUSY;
-
chip = kzalloc(sizeof *chip, GFP_KERNEL);
if (!chip)
return -ENOMEM;
- chip->leftshift = pdata->leftshift;
- chip->ioaddress = ioremap(pdev->resource[0].start, 1);
- if (chip->ioaddress == NULL)
+ if (pdata->use_gpio)
+ chip->ops = &v3020_gpio_ops;
+ else
+ chip->ops = &v3020_mmio_ops;
+
+ retval = chip->ops->map_io(chip, pdev, pdata);
+ if (retval)
goto err_chip;
/* Make sure the v3020 expects a communication cycle
* by reading 8 times */
for (i = 0; i < 8; i++)
- temp = readl(chip->ioaddress);
+ temp = chip->ops->read_bit(chip);
/* Test chip by doing a write/read sequence
* to the chip ram */
@@ -196,10 +339,17 @@ static int rtc_probe(struct platform_device *pdev)
* are all disabled */
v3020_set_reg(chip, V3020_STATUS_0, 0x0);
- dev_info(&pdev->dev, "Chip available at physical address 0x%llx,"
- "data connected to D%d\n",
- (unsigned long long)pdev->resource[0].start,
- chip->leftshift);
+ if (pdata->use_gpio)
+ dev_info(&pdev->dev, "Chip available at GPIOs "
+ "%d, %d, %d, %d\n",
+ chip->gpio[V3020_CS].gpio, chip->gpio[V3020_WR].gpio,
+ chip->gpio[V3020_RD].gpio, chip->gpio[V3020_IO].gpio);
+ else
+ dev_info(&pdev->dev, "Chip available at "
+ "physical address 0x%llx,"
+ "data connected to D%d\n",
+ (unsigned long long)pdev->resource[0].start,
+ chip->leftshift);
platform_set_drvdata(pdev, chip);
@@ -214,7 +364,7 @@ static int rtc_probe(struct platform_device *pdev)
return 0;
err_io:
- iounmap(chip->ioaddress);
+ chip->ops->unmap_io(chip);
err_chip:
kfree(chip);
@@ -229,7 +379,7 @@ static int rtc_remove(struct platform_device *dev)
if (rtc)
rtc_device_unregister(rtc);
- iounmap(chip->ioaddress);
+ chip->ops->unmap_io(chip);
kfree(chip);
return 0;
diff --git a/include/linux/rtc-v3020.h b/include/linux/rtc-v3020.h
index bf74e63..8ba646e 100644
--- a/include/linux/rtc-v3020.h
+++ b/include/linux/rtc-v3020.h
@@ -14,6 +14,12 @@
* is used depends on the board. */
struct v3020_platform_data {
int leftshift; /* (1<<(leftshift)) & readl() */
+
+ int use_gpio:1;
+ unsigned int gpio_cs;
+ unsigned int gpio_wr;
+ unsigned int gpio_rd;
+ unsigned int gpio_io;
};
#define V3020_STATUS_0 0x00
--
1.5.6.4
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH] rtc-v3020: coding style cleanup
2009-03-02 11:40 ` [PATCH] rtc-v3020: coding style cleanup Mike Rapoport
@ 2009-03-02 11:43 ` Alessandro Zummo
0 siblings, 0 replies; 11+ messages in thread
From: Alessandro Zummo @ 2009-03-02 11:43 UTC (permalink / raw)
To: Mike Rapoport; +Cc: rtc-linux, linux-kernel, raph, akpm
On Mon, 2 Mar 2009 13:40:56 +0200
Mike Rapoport <mike@compulab.co.il> wrote:
> Signed-off-by: Mike Rapoport <mike@compulab.co.il>
Acked-by: Alessandro Zummo <a.zummo@towertech.it>
--
Best regards,
Alessandro Zummo,
Tower Technologies - Torino, Italy
http://www.towertech.it
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs
2009-03-02 11:40 ` [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs Mike Rapoport
@ 2009-03-02 11:47 ` Alessandro Zummo
2009-03-02 12:18 ` Mike Rapoport
2009-03-02 22:36 ` Andrew Morton
1 sibling, 1 reply; 11+ messages in thread
From: Alessandro Zummo @ 2009-03-02 11:47 UTC (permalink / raw)
To: Mike Rapoport; +Cc: rtc-linux, linux-kernel, raph
On Mon, 2 Mar 2009 13:40:57 +0200
Mike Rapoport <mike@compulab.co.il> wrote:
> The v3020 RTC can be connected to GPIOs as well as to memory-like interface.
> Add ability to use GPIO bit-bang for v3020 read-write access.
>
> Signed-off-by: Mike Rapoport <mike@compulab.co.il>
For the rtc part you have my
Acked-by: Alessandro Zummo <a.zummo@towertech.it>
please send the patch to the GPIO maintainer . Once you
have his Acked-by and the one of the driver's author you can
send it to Andrew for inclusion.
--
Best regards,
Alessandro Zummo,
Tower Technologies - Torino, Italy
http://www.towertech.it
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs
2009-03-02 11:47 ` Alessandro Zummo
@ 2009-03-02 12:18 ` Mike Rapoport
2009-03-03 21:08 ` David Brownell
0 siblings, 1 reply; 11+ messages in thread
From: Mike Rapoport @ 2009-03-02 12:18 UTC (permalink / raw)
To: David Brownell; +Cc: Alessandro Zummo, rtc-linux, linux-kernel, raph
David,
Can you please review and Ack the GPIO part of the below patch?
Alessandro Zummo wrote:
> On Mon, 2 Mar 2009 13:40:57 +0200
> Mike Rapoport <mike@compulab.co.il> wrote:
>
>> The v3020 RTC can be connected to GPIOs as well as to memory-like interface.
>> Add ability to use GPIO bit-bang for v3020 read-write access.
>>
>> Signed-off-by: Mike Rapoport <mike@compulab.co.il>
>
> For the rtc part you have my
>
> Acked-by: Alessandro Zummo <a.zummo@towertech.it>
>
> please send the patch to the GPIO maintainer . Once you
> have his Acked-by and the one of the driver's author you can
> send it to Andrew for inclusion.
>
Signed-off-by: Mike Rapoport <mike@compulab.co.il>
---
drivers/rtc/rtc-v3020.c | 190 ++++++++++++++++++++++++++++++++++++++++-----
include/linux/rtc-v3020.h | 6 ++
2 files changed, 176 insertions(+), 20 deletions(-)
diff --git a/drivers/rtc/rtc-v3020.c b/drivers/rtc/rtc-v3020.c
index 66955cc..4c330f2 100644
--- a/drivers/rtc/rtc-v3020.c
+++ b/drivers/rtc/rtc-v3020.c
@@ -27,17 +27,162 @@
#include <linux/bcd.h>
#include <linux/rtc-v3020.h>
#include <linux/delay.h>
+#include <linux/gpio.h>
#include <linux/io.h>
#undef DEBUG
+struct v3020;
+
+struct v3020_chip_ops {
+ int (*map_io)(struct v3020 *chip, struct platform_device *pdev,
+ struct v3020_platform_data *pdata);
+ void (*unmap_io)(struct v3020 *chip);
+ unsigned char (*read_bit)(struct v3020 *chip);
+ void (*write_bit)(struct v3020 *chip, unsigned char bit);
+};
+
+#define V3020_CS 0
+#define V3020_WR 1
+#define V3020_RD 2
+#define V3020_IO 3
+
+struct v3020_gpio {
+ const char *name;
+ unsigned int gpio;
+};
+
struct v3020 {
+ /* MMIO access */
void __iomem *ioaddress;
int leftshift;
+
+ /* GPIO access */
+ struct v3020_gpio *gpio;
+
+ struct v3020_chip_ops *ops;
+
struct rtc_device *rtc;
};
+
+static int v3020_mmio_map(struct v3020 *chip, struct platform_device *pdev,
+ struct v3020_platform_data *pdata)
+{
+ if (pdev->num_resources != 1)
+ return -EBUSY;
+
+ if (pdev->resource[0].flags != IORESOURCE_MEM)
+ return -EBUSY;
+
+ chip->leftshift = pdata->leftshift;
+ chip->ioaddress = ioremap(pdev->resource[0].start, 1);
+ if (chip->ioaddress == NULL)
+ return -EBUSY;
+
+ return 0;
+}
+
+static void v3020_mmio_unmap(struct v3020 *chip)
+{
+ iounmap(chip->ioaddress);
+}
+
+static void v3020_mmio_write_bit(struct v3020 *chip, unsigned char bit)
+{
+ writel(bit << chip->leftshift, chip->ioaddress);
+}
+
+static unsigned char v3020_mmio_read_bit(struct v3020 *chip)
+{
+ return readl(chip->ioaddress) & (1 << chip->leftshift);
+}
+
+static struct v3020_chip_ops v3020_mmio_ops = {
+ .map_io = v3020_mmio_map,
+ .unmap_io = v3020_mmio_unmap,
+ .read_bit = v3020_mmio_read_bit,
+ .write_bit = v3020_mmio_write_bit,
+};
+
+static struct v3020_gpio v3020_gpio[] = {
+ { "RTC CS", 0 },
+ { "RTC WR", 0 },
+ { "RTC RD", 0 },
+ { "RTC IO", 0 },
+};
+
+static int v3020_gpio_map(struct v3020 *chip, struct platform_device *pdev,
+ struct v3020_platform_data *pdata)
+{
+ int i, err;
+
+ v3020_gpio[V3020_CS].gpio = pdata->gpio_cs;
+ v3020_gpio[V3020_WR].gpio = pdata->gpio_wr;
+ v3020_gpio[V3020_RD].gpio = pdata->gpio_rd;
+ v3020_gpio[V3020_IO].gpio = pdata->gpio_io;
+
+ for (i = 0; i < ARRAY_SIZE(v3020_gpio); i++) {
+ err = gpio_request(v3020_gpio[i].gpio, v3020_gpio[i].name);
+ if (err)
+ goto err_request;
+
+ gpio_direction_output(v3020_gpio[i].gpio, 1);
+ }
+
+ chip->gpio = v3020_gpio;
+
+ return 0;
+
+err_request:
+ for (; i >= 0; i--)
+ gpio_free(v3020_gpio[i].gpio);
+
+ return err;
+}
+
+static void v3020_gpio_unmap(struct v3020 *chip)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(v3020_gpio); i++)
+ gpio_free(v3020_gpio[i].gpio);
+}
+
+static void v3020_gpio_write_bit(struct v3020 *chip, unsigned char bit)
+{
+ gpio_direction_output(chip->gpio[V3020_IO].gpio, bit);
+ gpio_set_value(chip->gpio[V3020_CS].gpio, 0);
+ gpio_set_value(chip->gpio[V3020_WR].gpio, 0);
+ udelay(1);
+ gpio_set_value(chip->gpio[V3020_WR].gpio, 1);
+ gpio_set_value(chip->gpio[V3020_CS].gpio, 1);
+}
+
+static unsigned char v3020_gpio_read_bit(struct v3020 *chip)
+{
+ int bit;
+
+ gpio_direction_input(chip->gpio[V3020_IO].gpio);
+ gpio_set_value(chip->gpio[V3020_CS].gpio, 0);
+ gpio_set_value(chip->gpio[V3020_RD].gpio, 0);
+ udelay(1);
+ bit = !!gpio_get_value(chip->gpio[V3020_IO].gpio);
+ udelay(1);
+ gpio_set_value(chip->gpio[V3020_RD].gpio, 1);
+ gpio_set_value(chip->gpio[V3020_CS].gpio, 1);
+
+ return bit;
+}
+
+static struct v3020_chip_ops v3020_gpio_ops = {
+ .map_io = v3020_gpio_map,
+ .unmap_io = v3020_gpio_unmap,
+ .read_bit = v3020_gpio_read_bit,
+ .write_bit = v3020_gpio_write_bit,
+};
+
static void v3020_set_reg(struct v3020 *chip, unsigned char address,
unsigned char data)
{
@@ -46,7 +191,7 @@ static void v3020_set_reg(struct v3020 *chip, unsigned char address,
tmp = address;
for (i = 0; i < 4; i++) {
- writel((tmp & 1) << chip->leftshift, chip->ioaddress);
+ chip->ops->write_bit(chip, (tmp & 1));
tmp >>= 1;
udelay(1);
}
@@ -54,7 +199,7 @@ static void v3020_set_reg(struct v3020 *chip, unsigned char address,
/* Commands dont have data */
if (!V3020_IS_COMMAND(address)) {
for (i = 0; i < 8; i++) {
- writel((data & 1) << chip->leftshift, chip->ioaddress);
+ chip->ops->write_bit(chip, (data & 1));
data >>= 1;
udelay(1);
}
@@ -67,14 +212,14 @@ static unsigned char v3020_get_reg(struct v3020 *chip, unsigned char address)
int i;
for (i = 0; i < 4; i++) {
- writel((address & 1) << chip->leftshift, chip->ioaddress);
+ chip->ops->write_bit(chip, (address & 1));
address >>= 1;
udelay(1);
}
for (i = 0; i < 8; i++) {
data >>= 1;
- if (readl(chip->ioaddress) & (1 << chip->leftshift))
+ if (chip->ops->read_bit(chip))
data |= 0x80;
udelay(1);
}
@@ -164,25 +309,23 @@ static int rtc_probe(struct platform_device *pdev)
int i;
int temp;
- if (pdev->num_resources != 1)
- return -EBUSY;
-
- if (pdev->resource[0].flags != IORESOURCE_MEM)
- return -EBUSY;
-
chip = kzalloc(sizeof *chip, GFP_KERNEL);
if (!chip)
return -ENOMEM;
- chip->leftshift = pdata->leftshift;
- chip->ioaddress = ioremap(pdev->resource[0].start, 1);
- if (chip->ioaddress == NULL)
+ if (pdata->use_gpio)
+ chip->ops = &v3020_gpio_ops;
+ else
+ chip->ops = &v3020_mmio_ops;
+
+ retval = chip->ops->map_io(chip, pdev, pdata);
+ if (retval)
goto err_chip;
/* Make sure the v3020 expects a communication cycle
* by reading 8 times */
for (i = 0; i < 8; i++)
- temp = readl(chip->ioaddress);
+ temp = chip->ops->read_bit(chip);
/* Test chip by doing a write/read sequence
* to the chip ram */
@@ -196,10 +339,17 @@ static int rtc_probe(struct platform_device *pdev)
* are all disabled */
v3020_set_reg(chip, V3020_STATUS_0, 0x0);
- dev_info(&pdev->dev, "Chip available at physical address 0x%llx,"
- "data connected to D%d\n",
- (unsigned long long)pdev->resource[0].start,
- chip->leftshift);
+ if (pdata->use_gpio)
+ dev_info(&pdev->dev, "Chip available at GPIOs "
+ "%d, %d, %d, %d\n",
+ chip->gpio[V3020_CS].gpio, chip->gpio[V3020_WR].gpio,
+ chip->gpio[V3020_RD].gpio, chip->gpio[V3020_IO].gpio);
+ else
+ dev_info(&pdev->dev, "Chip available at "
+ "physical address 0x%llx,"
+ "data connected to D%d\n",
+ (unsigned long long)pdev->resource[0].start,
+ chip->leftshift);
platform_set_drvdata(pdev, chip);
@@ -214,7 +364,7 @@ static int rtc_probe(struct platform_device *pdev)
return 0;
err_io:
- iounmap(chip->ioaddress);
+ chip->ops->unmap_io(chip);
err_chip:
kfree(chip);
@@ -229,7 +379,7 @@ static int rtc_remove(struct platform_device *dev)
if (rtc)
rtc_device_unregister(rtc);
- iounmap(chip->ioaddress);
+ chip->ops->unmap_io(chip);
kfree(chip);
return 0;
diff --git a/include/linux/rtc-v3020.h b/include/linux/rtc-v3020.h
index bf74e63..8ba646e 100644
--- a/include/linux/rtc-v3020.h
+++ b/include/linux/rtc-v3020.h
@@ -14,6 +14,12 @@
* is used depends on the board. */
struct v3020_platform_data {
int leftshift; /* (1<<(leftshift)) & readl() */
+
+ int use_gpio:1;
+ unsigned int gpio_cs;
+ unsigned int gpio_wr;
+ unsigned int gpio_rd;
+ unsigned int gpio_io;
};
#define V3020_STATUS_0 0x00
--
1.5.6.4
--
Sincerely yours,
Mike.
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs
2009-03-02 11:40 ` [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs Mike Rapoport
2009-03-02 11:47 ` Alessandro Zummo
@ 2009-03-02 22:36 ` Andrew Morton
2009-03-03 6:16 ` Mike Rapoport
1 sibling, 1 reply; 11+ messages in thread
From: Andrew Morton @ 2009-03-02 22:36 UTC (permalink / raw)
To: Mike Rapoport
Cc: alessandro.zummo, rtc-linux, linux-kernel, raph, mike,
David Brownell
On Mon, 2 Mar 2009 13:40:57 +0200
Mike Rapoport <mike@compulab.co.il> wrote:
> +static int v3020_gpio_map(struct v3020 *chip, struct platform_device *pdev,
> + struct v3020_platform_data *pdata)
> +{
> + int i, err;
> +
> + v3020_gpio[V3020_CS].gpio = pdata->gpio_cs;
> + v3020_gpio[V3020_WR].gpio = pdata->gpio_wr;
> + v3020_gpio[V3020_RD].gpio = pdata->gpio_rd;
> + v3020_gpio[V3020_IO].gpio = pdata->gpio_io;
> +
> + for (i = 0; i < ARRAY_SIZE(v3020_gpio); i++) {
> + err = gpio_request(v3020_gpio[i].gpio, v3020_gpio[i].name);
> + if (err)
> + goto err_request;
> +
> + gpio_direction_output(v3020_gpio[i].gpio, 1);
> + }
> +
> + chip->gpio = v3020_gpio;
> +
> + return 0;
> +
> +err_request:
> + for (; i >= 0; i--)
> + gpio_free(v3020_gpio[i].gpio);
> +
> + return err;
> +}
It needs this fix, I think?
fix off-by-one in error path
--- a/drivers/rtc/rtc-v3020.c~rtc-v3020-add-ability-to-access-v3020-chip-with-gpios-fix
+++ a/drivers/rtc/rtc-v3020.c
@@ -136,7 +136,7 @@ static int v3020_gpio_map(struct v3020 *
return 0;
err_request:
- for (; i >= 0; i--)
+ while (--i >= 0)
gpio_free(v3020_gpio[i].gpio);
return err;
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs
2009-03-02 22:36 ` Andrew Morton
@ 2009-03-03 6:16 ` Mike Rapoport
0 siblings, 0 replies; 11+ messages in thread
From: Mike Rapoport @ 2009-03-03 6:16 UTC (permalink / raw)
To: Andrew Morton
Cc: alessandro.zummo, rtc-linux, linux-kernel, raph, David Brownell
Andrew Morton wrote:
> On Mon, 2 Mar 2009 13:40:57 +0200
> Mike Rapoport <mike@compulab.co.il> wrote:
>
>> +static int v3020_gpio_map(struct v3020 *chip, struct platform_device *pdev,
>> + struct v3020_platform_data *pdata)
>> +{
>> + int i, err;
>> +
>> + v3020_gpio[V3020_CS].gpio = pdata->gpio_cs;
>> + v3020_gpio[V3020_WR].gpio = pdata->gpio_wr;
>> + v3020_gpio[V3020_RD].gpio = pdata->gpio_rd;
>> + v3020_gpio[V3020_IO].gpio = pdata->gpio_io;
>> +
>> + for (i = 0; i < ARRAY_SIZE(v3020_gpio); i++) {
>> + err = gpio_request(v3020_gpio[i].gpio, v3020_gpio[i].name);
>> + if (err)
>> + goto err_request;
>> +
>> + gpio_direction_output(v3020_gpio[i].gpio, 1);
>> + }
>> +
>> + chip->gpio = v3020_gpio;
>> +
>> + return 0;
>> +
>> +err_request:
>> + for (; i >= 0; i--)
>> + gpio_free(v3020_gpio[i].gpio);
>> +
>> + return err;
>> +}
>
> It needs this fix, I think?
Indeed. Thanks.
>
> fix off-by-one in error path
>
> --- a/drivers/rtc/rtc-v3020.c~rtc-v3020-add-ability-to-access-v3020-chip-with-gpios-fix
> +++ a/drivers/rtc/rtc-v3020.c
> @@ -136,7 +136,7 @@ static int v3020_gpio_map(struct v3020 *
> return 0;
>
> err_request:
> - for (; i >= 0; i--)
> + while (--i >= 0)
> gpio_free(v3020_gpio[i].gpio);
>
> return err;
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
>
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs
2009-03-02 12:18 ` Mike Rapoport
@ 2009-03-03 21:08 ` David Brownell
2009-03-08 10:03 ` Mike Rapoport
0 siblings, 1 reply; 11+ messages in thread
From: David Brownell @ 2009-03-03 21:08 UTC (permalink / raw)
To: Mike Rapoport; +Cc: Alessandro Zummo, rtc-linux, linux-kernel, raph
On Monday 02 March 2009, Mike Rapoport wrote:
> +
> +#define V3020_CS 0
> +#define V3020_WR 1
> +#define V3020_RD 2
> +#define V3020_IO 3
> +
> +struct v3020_gpio {
> + const char *name;
> + unsigned int gpio;
> +};
> +
> +static struct v3020_gpio v3020_gpio[] = {
> + { "RTC CS", 0 },
> + { "RTC WR", 0 },
> + { "RTC RD", 0 },
> + { "RTC IO", 0 },
> +};
After Andrew's fault path cleanup it looks OK, but I see
no point to that GPIO struct with the name. You know the
name based on the position, if you ever need one. Take
that away, and you can add my ack.
Also, best not to have a single static struct; there's
no need to add a driver restriction that there be only
one of these chips in a system.
- Dave
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs
2009-03-03 21:08 ` David Brownell
@ 2009-03-08 10:03 ` Mike Rapoport
2009-03-19 21:25 ` David Brownell
0 siblings, 1 reply; 11+ messages in thread
From: Mike Rapoport @ 2009-03-08 10:03 UTC (permalink / raw)
To: David Brownell
Cc: Alessandro Zummo, rtc-linux, linux-kernel, raph, Andrew Morton
David,
David Brownell wrote:
> On Monday 02 March 2009, Mike Rapoport wrote:
>> +
>> +#define V3020_CS 0
>> +#define V3020_WR 1
>> +#define V3020_RD 2
>> +#define V3020_IO 3
>> +
>> +struct v3020_gpio {
>> + const char *name;
>> + unsigned int gpio;
>> +};
>> +
>
>> +static struct v3020_gpio v3020_gpio[] = {
>> + { "RTC CS", 0 },
>> + { "RTC WR", 0 },
>> + { "RTC RD", 0 },
>> + { "RTC IO", 0 },
>> +};
>
> After Andrew's fault path cleanup it looks OK, but I see
> no point to that GPIO struct with the name. You know the
> name based on the position, if you ever need one. Take
> that away, and you can add my ack.
>
>
> Also, best not to have a single static struct; there's
> no need to add a driver restriction that there be only
> one of these chips in a system.
What about the below version?
> - Dave
>
Signed-off-by: Mike Rapoport <mike@compulab.co.il>
CC: Alessandro Zummo <alessandro.zummo@towertech.it>
CC: David Brownell <david-b@pacbell.net>
CC: Andrew Morton <akpm@linux-foundation.org>
---
drivers/rtc/rtc-v3020.c | 183 ++++++++++++++++++++++++++++++++++++++++-----
include/linux/rtc-v3020.h | 6 ++
2 files changed, 169 insertions(+), 20 deletions(-)
diff --git a/drivers/rtc/rtc-v3020.c b/drivers/rtc/rtc-v3020.c
index 66955cc..9f29929 100644
--- a/drivers/rtc/rtc-v3020.c
+++ b/drivers/rtc/rtc-v3020.c
@@ -27,17 +27,155 @@
#include <linux/bcd.h>
#include <linux/rtc-v3020.h>
#include <linux/delay.h>
+#include <linux/gpio.h>
#include <linux/io.h>
#undef DEBUG
+struct v3020;
+
+struct v3020_chip_ops {
+ int (*map_io)(struct v3020 *chip, struct platform_device *pdev,
+ struct v3020_platform_data *pdata);
+ void (*unmap_io)(struct v3020 *chip);
+ unsigned char (*read_bit)(struct v3020 *chip);
+ void (*write_bit)(struct v3020 *chip, unsigned char bit);
+};
+
+#define V3020_CS 0
+#define V3020_WR 1
+#define V3020_RD 2
+#define V3020_IO 3
+
struct v3020 {
+ /* MMIO access */
void __iomem *ioaddress;
int leftshift;
+
+ /* GPIO access */
+ unsigned int gpio[4];
+
+ struct v3020_chip_ops *ops;
+
struct rtc_device *rtc;
};
+
+static int v3020_mmio_map(struct v3020 *chip, struct platform_device *pdev,
+ struct v3020_platform_data *pdata)
+{
+ if (pdev->num_resources != 1)
+ return -EBUSY;
+
+ if (pdev->resource[0].flags != IORESOURCE_MEM)
+ return -EBUSY;
+
+ chip->leftshift = pdata->leftshift;
+ chip->ioaddress = ioremap(pdev->resource[0].start, 1);
+ if (chip->ioaddress == NULL)
+ return -EBUSY;
+
+ return 0;
+}
+
+static void v3020_mmio_unmap(struct v3020 *chip)
+{
+ iounmap(chip->ioaddress);
+}
+
+static void v3020_mmio_write_bit(struct v3020 *chip, unsigned char bit)
+{
+ writel(bit << chip->leftshift, chip->ioaddress);
+}
+
+static unsigned char v3020_mmio_read_bit(struct v3020 *chip)
+{
+ return readl(chip->ioaddress) & (1 << chip->leftshift);
+}
+
+static struct v3020_chip_ops v3020_mmio_ops = {
+ .map_io = v3020_mmio_map,
+ .unmap_io = v3020_mmio_unmap,
+ .read_bit = v3020_mmio_read_bit,
+ .write_bit = v3020_mmio_write_bit,
+};
+
+static const char *v3020_gpio_names[] = {
+ "RTC CS",
+ "RTC WR",
+ "RTC RD",
+ "RTC IO",
+};
+
+static int v3020_gpio_map(struct v3020 *chip, struct platform_device *pdev,
+ struct v3020_platform_data *pdata)
+{
+ int i, err;
+
+ chip->gpio[V3020_CS] = pdata->gpio_cs;
+ chip->gpio[V3020_WR] = pdata->gpio_wr;
+ chip->gpio[V3020_RD] = pdata->gpio_rd;
+ chip->gpio[V3020_IO] = pdata->gpio_io;
+
+ for (i = 0; i < ARRAY_SIZE(chip->gpio); i++) {
+ err = gpio_request(chip->gpio[i], v3020_gpio_names[i]);
+ if (err)
+ goto err_request;
+
+ gpio_direction_output(chip->gpio[i], 1);
+ }
+
+ return 0;
+
+err_request:
+ while (--i >= 0)
+ gpio_free(chip->gpio[i]);
+
+ return err;
+}
+
+static void v3020_gpio_unmap(struct v3020 *chip)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(chip->gpio); i++)
+ gpio_free(chip->gpio[i]);
+}
+
+static void v3020_gpio_write_bit(struct v3020 *chip, unsigned char bit)
+{
+ gpio_direction_output(chip->gpio[V3020_IO], bit);
+ gpio_set_value(chip->gpio[V3020_CS], 0);
+ gpio_set_value(chip->gpio[V3020_WR], 0);
+ udelay(1);
+ gpio_set_value(chip->gpio[V3020_WR], 1);
+ gpio_set_value(chip->gpio[V3020_CS], 1);
+}
+
+static unsigned char v3020_gpio_read_bit(struct v3020 *chip)
+{
+ int bit;
+
+ gpio_direction_input(chip->gpio[V3020_IO]);
+ gpio_set_value(chip->gpio[V3020_CS], 0);
+ gpio_set_value(chip->gpio[V3020_RD], 0);
+ udelay(1);
+ bit = !!gpio_get_value(chip->gpio[V3020_IO]);
+ udelay(1);
+ gpio_set_value(chip->gpio[V3020_RD], 1);
+ gpio_set_value(chip->gpio[V3020_CS], 1);
+
+ return bit;
+}
+
+static struct v3020_chip_ops v3020_gpio_ops = {
+ .map_io = v3020_gpio_map,
+ .unmap_io = v3020_gpio_unmap,
+ .read_bit = v3020_gpio_read_bit,
+ .write_bit = v3020_gpio_write_bit,
+};
+
static void v3020_set_reg(struct v3020 *chip, unsigned char address,
unsigned char data)
{
@@ -46,7 +184,7 @@ static void v3020_set_reg(struct v3020 *chip, unsigned char address,
tmp = address;
for (i = 0; i < 4; i++) {
- writel((tmp & 1) << chip->leftshift, chip->ioaddress);
+ chip->ops->write_bit(chip, (tmp & 1));
tmp >>= 1;
udelay(1);
}
@@ -54,7 +192,7 @@ static void v3020_set_reg(struct v3020 *chip, unsigned char address,
/* Commands dont have data */
if (!V3020_IS_COMMAND(address)) {
for (i = 0; i < 8; i++) {
- writel((data & 1) << chip->leftshift, chip->ioaddress);
+ chip->ops->write_bit(chip, (data & 1));
data >>= 1;
udelay(1);
}
@@ -67,14 +205,14 @@ static unsigned char v3020_get_reg(struct v3020 *chip, unsigned char address)
int i;
for (i = 0; i < 4; i++) {
- writel((address & 1) << chip->leftshift, chip->ioaddress);
+ chip->ops->write_bit(chip, (address & 1));
address >>= 1;
udelay(1);
}
for (i = 0; i < 8; i++) {
data >>= 1;
- if (readl(chip->ioaddress) & (1 << chip->leftshift))
+ if (chip->ops->read_bit(chip))
data |= 0x80;
udelay(1);
}
@@ -164,25 +302,23 @@ static int rtc_probe(struct platform_device *pdev)
int i;
int temp;
- if (pdev->num_resources != 1)
- return -EBUSY;
-
- if (pdev->resource[0].flags != IORESOURCE_MEM)
- return -EBUSY;
-
chip = kzalloc(sizeof *chip, GFP_KERNEL);
if (!chip)
return -ENOMEM;
- chip->leftshift = pdata->leftshift;
- chip->ioaddress = ioremap(pdev->resource[0].start, 1);
- if (chip->ioaddress == NULL)
+ if (pdata->use_gpio)
+ chip->ops = &v3020_gpio_ops;
+ else
+ chip->ops = &v3020_mmio_ops;
+
+ retval = chip->ops->map_io(chip, pdev, pdata);
+ if (retval)
goto err_chip;
/* Make sure the v3020 expects a communication cycle
* by reading 8 times */
for (i = 0; i < 8; i++)
- temp = readl(chip->ioaddress);
+ temp = chip->ops->read_bit(chip);
/* Test chip by doing a write/read sequence
* to the chip ram */
@@ -196,10 +332,17 @@ static int rtc_probe(struct platform_device *pdev)
* are all disabled */
v3020_set_reg(chip, V3020_STATUS_0, 0x0);
- dev_info(&pdev->dev, "Chip available at physical address 0x%llx,"
- "data connected to D%d\n",
- (unsigned long long)pdev->resource[0].start,
- chip->leftshift);
+ if (pdata->use_gpio)
+ dev_info(&pdev->dev, "Chip available at GPIOs "
+ "%d, %d, %d, %d\n",
+ chip->gpio[V3020_CS], chip->gpio[V3020_WR],
+ chip->gpio[V3020_RD], chip->gpio[V3020_IO]);
+ else
+ dev_info(&pdev->dev, "Chip available at "
+ "physical address 0x%llx,"
+ "data connected to D%d\n",
+ (unsigned long long)pdev->resource[0].start,
+ chip->leftshift);
platform_set_drvdata(pdev, chip);
@@ -214,7 +357,7 @@ static int rtc_probe(struct platform_device *pdev)
return 0;
err_io:
- iounmap(chip->ioaddress);
+ chip->ops->unmap_io(chip);
err_chip:
kfree(chip);
@@ -229,7 +372,7 @@ static int rtc_remove(struct platform_device *dev)
if (rtc)
rtc_device_unregister(rtc);
- iounmap(chip->ioaddress);
+ chip->ops->unmap_io(chip);
kfree(chip);
return 0;
diff --git a/include/linux/rtc-v3020.h b/include/linux/rtc-v3020.h
index bf74e63..8ba646e 100644
--- a/include/linux/rtc-v3020.h
+++ b/include/linux/rtc-v3020.h
@@ -14,6 +14,12 @@
* is used depends on the board. */
struct v3020_platform_data {
int leftshift; /* (1<<(leftshift)) & readl() */
+
+ int use_gpio:1;
+ unsigned int gpio_cs;
+ unsigned int gpio_wr;
+ unsigned int gpio_rd;
+ unsigned int gpio_io;
};
#define V3020_STATUS_0 0x00
--
1.5.6.4
--
Sincerely yours,
Mike.
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs
2009-03-08 10:03 ` Mike Rapoport
@ 2009-03-19 21:25 ` David Brownell
0 siblings, 0 replies; 11+ messages in thread
From: David Brownell @ 2009-03-19 21:25 UTC (permalink / raw)
To: Mike Rapoport
Cc: Alessandro Zummo, rtc-linux, linux-kernel, raph, Andrew Morton
On Sunday 08 March 2009, Mike Rapoport wrote:
>
> What about the below version?
Looks OK.
> Signed-off-by: Mike Rapoport <mike@compulab.co.il>
> CC: Alessandro Zummo <alessandro.zummo@towertech.it>
> CC: David Brownell <david-b@pacbell.net>
> CC: Andrew Morton <akpm@linux-foundation.org>
> ---
> drivers/rtc/rtc-v3020.c | 183 ++++++++++++++++++++++++++++++++++++++++-----
> include/linux/rtc-v3020.h | 6 ++
> 2 files changed, 169 insertions(+), 20 deletions(-)
>
> diff --git a/drivers/rtc/rtc-v3020.c b/drivers/rtc/rtc-v3020.c
> index 66955cc..9f29929 100644
> --- a/drivers/rtc/rtc-v3020.c
> +++ b/drivers/rtc/rtc-v3020.c
> @@ -27,17 +27,155 @@
> #include <linux/bcd.h>
> #include <linux/rtc-v3020.h>
> #include <linux/delay.h>
> +#include <linux/gpio.h>
>
> #include <linux/io.h>
>
> #undef DEBUG
>
> +struct v3020;
> +
> +struct v3020_chip_ops {
> + int (*map_io)(struct v3020 *chip, struct platform_device *pdev,
> + struct v3020_platform_data *pdata);
> + void (*unmap_io)(struct v3020 *chip);
> + unsigned char (*read_bit)(struct v3020 *chip);
> + void (*write_bit)(struct v3020 *chip, unsigned char bit);
> +};
> +
> +#define V3020_CS 0
> +#define V3020_WR 1
> +#define V3020_RD 2
> +#define V3020_IO 3
> +
> struct v3020 {
> + /* MMIO access */
> void __iomem *ioaddress;
> int leftshift;
> +
> + /* GPIO access */
> + unsigned int gpio[4];
> +
> + struct v3020_chip_ops *ops;
> +
> struct rtc_device *rtc;
> };
>
> +
> +static int v3020_mmio_map(struct v3020 *chip, struct platform_device *pdev,
> + struct v3020_platform_data *pdata)
> +{
> + if (pdev->num_resources != 1)
> + return -EBUSY;
> +
> + if (pdev->resource[0].flags != IORESOURCE_MEM)
> + return -EBUSY;
> +
> + chip->leftshift = pdata->leftshift;
> + chip->ioaddress = ioremap(pdev->resource[0].start, 1);
> + if (chip->ioaddress == NULL)
> + return -EBUSY;
> +
> + return 0;
> +}
> +
> +static void v3020_mmio_unmap(struct v3020 *chip)
> +{
> + iounmap(chip->ioaddress);
> +}
> +
> +static void v3020_mmio_write_bit(struct v3020 *chip, unsigned char bit)
> +{
> + writel(bit << chip->leftshift, chip->ioaddress);
> +}
> +
> +static unsigned char v3020_mmio_read_bit(struct v3020 *chip)
> +{
> + return readl(chip->ioaddress) & (1 << chip->leftshift);
> +}
> +
> +static struct v3020_chip_ops v3020_mmio_ops = {
> + .map_io = v3020_mmio_map,
> + .unmap_io = v3020_mmio_unmap,
> + .read_bit = v3020_mmio_read_bit,
> + .write_bit = v3020_mmio_write_bit,
> +};
> +
> +static const char *v3020_gpio_names[] = {
> + "RTC CS",
> + "RTC WR",
> + "RTC RD",
> + "RTC IO",
> +};
> +
> +static int v3020_gpio_map(struct v3020 *chip, struct platform_device *pdev,
> + struct v3020_platform_data *pdata)
> +{
> + int i, err;
> +
> + chip->gpio[V3020_CS] = pdata->gpio_cs;
> + chip->gpio[V3020_WR] = pdata->gpio_wr;
> + chip->gpio[V3020_RD] = pdata->gpio_rd;
> + chip->gpio[V3020_IO] = pdata->gpio_io;
> +
> + for (i = 0; i < ARRAY_SIZE(chip->gpio); i++) {
> + err = gpio_request(chip->gpio[i], v3020_gpio_names[i]);
> + if (err)
> + goto err_request;
> +
> + gpio_direction_output(chip->gpio[i], 1);
> + }
> +
> + return 0;
> +
> +err_request:
> + while (--i >= 0)
> + gpio_free(chip->gpio[i]);
> +
> + return err;
> +}
> +
> +static void v3020_gpio_unmap(struct v3020 *chip)
> +{
> + int i;
> +
> + for (i = 0; i < ARRAY_SIZE(chip->gpio); i++)
> + gpio_free(chip->gpio[i]);
> +}
> +
> +static void v3020_gpio_write_bit(struct v3020 *chip, unsigned char bit)
> +{
> + gpio_direction_output(chip->gpio[V3020_IO], bit);
> + gpio_set_value(chip->gpio[V3020_CS], 0);
> + gpio_set_value(chip->gpio[V3020_WR], 0);
> + udelay(1);
> + gpio_set_value(chip->gpio[V3020_WR], 1);
> + gpio_set_value(chip->gpio[V3020_CS], 1);
> +}
> +
> +static unsigned char v3020_gpio_read_bit(struct v3020 *chip)
> +{
> + int bit;
> +
> + gpio_direction_input(chip->gpio[V3020_IO]);
> + gpio_set_value(chip->gpio[V3020_CS], 0);
> + gpio_set_value(chip->gpio[V3020_RD], 0);
> + udelay(1);
> + bit = !!gpio_get_value(chip->gpio[V3020_IO]);
> + udelay(1);
> + gpio_set_value(chip->gpio[V3020_RD], 1);
> + gpio_set_value(chip->gpio[V3020_CS], 1);
> +
> + return bit;
> +}
> +
> +static struct v3020_chip_ops v3020_gpio_ops = {
> + .map_io = v3020_gpio_map,
> + .unmap_io = v3020_gpio_unmap,
> + .read_bit = v3020_gpio_read_bit,
> + .write_bit = v3020_gpio_write_bit,
> +};
> +
> static void v3020_set_reg(struct v3020 *chip, unsigned char address,
> unsigned char data)
> {
> @@ -46,7 +184,7 @@ static void v3020_set_reg(struct v3020 *chip, unsigned char address,
>
> tmp = address;
> for (i = 0; i < 4; i++) {
> - writel((tmp & 1) << chip->leftshift, chip->ioaddress);
> + chip->ops->write_bit(chip, (tmp & 1));
> tmp >>= 1;
> udelay(1);
> }
> @@ -54,7 +192,7 @@ static void v3020_set_reg(struct v3020 *chip, unsigned char address,
> /* Commands dont have data */
> if (!V3020_IS_COMMAND(address)) {
> for (i = 0; i < 8; i++) {
> - writel((data & 1) << chip->leftshift, chip->ioaddress);
> + chip->ops->write_bit(chip, (data & 1));
> data >>= 1;
> udelay(1);
> }
> @@ -67,14 +205,14 @@ static unsigned char v3020_get_reg(struct v3020 *chip, unsigned char address)
> int i;
>
> for (i = 0; i < 4; i++) {
> - writel((address & 1) << chip->leftshift, chip->ioaddress);
> + chip->ops->write_bit(chip, (address & 1));
> address >>= 1;
> udelay(1);
> }
>
> for (i = 0; i < 8; i++) {
> data >>= 1;
> - if (readl(chip->ioaddress) & (1 << chip->leftshift))
> + if (chip->ops->read_bit(chip))
> data |= 0x80;
> udelay(1);
> }
> @@ -164,25 +302,23 @@ static int rtc_probe(struct platform_device *pdev)
> int i;
> int temp;
>
> - if (pdev->num_resources != 1)
> - return -EBUSY;
> -
> - if (pdev->resource[0].flags != IORESOURCE_MEM)
> - return -EBUSY;
> -
> chip = kzalloc(sizeof *chip, GFP_KERNEL);
> if (!chip)
> return -ENOMEM;
>
> - chip->leftshift = pdata->leftshift;
> - chip->ioaddress = ioremap(pdev->resource[0].start, 1);
> - if (chip->ioaddress == NULL)
> + if (pdata->use_gpio)
> + chip->ops = &v3020_gpio_ops;
> + else
> + chip->ops = &v3020_mmio_ops;
> +
> + retval = chip->ops->map_io(chip, pdev, pdata);
> + if (retval)
> goto err_chip;
>
> /* Make sure the v3020 expects a communication cycle
> * by reading 8 times */
> for (i = 0; i < 8; i++)
> - temp = readl(chip->ioaddress);
> + temp = chip->ops->read_bit(chip);
>
> /* Test chip by doing a write/read sequence
> * to the chip ram */
> @@ -196,10 +332,17 @@ static int rtc_probe(struct platform_device *pdev)
> * are all disabled */
> v3020_set_reg(chip, V3020_STATUS_0, 0x0);
>
> - dev_info(&pdev->dev, "Chip available at physical address 0x%llx,"
> - "data connected to D%d\n",
> - (unsigned long long)pdev->resource[0].start,
> - chip->leftshift);
> + if (pdata->use_gpio)
> + dev_info(&pdev->dev, "Chip available at GPIOs "
> + "%d, %d, %d, %d\n",
> + chip->gpio[V3020_CS], chip->gpio[V3020_WR],
> + chip->gpio[V3020_RD], chip->gpio[V3020_IO]);
> + else
> + dev_info(&pdev->dev, "Chip available at "
> + "physical address 0x%llx,"
> + "data connected to D%d\n",
> + (unsigned long long)pdev->resource[0].start,
> + chip->leftshift);
>
> platform_set_drvdata(pdev, chip);
>
> @@ -214,7 +357,7 @@ static int rtc_probe(struct platform_device *pdev)
> return 0;
>
> err_io:
> - iounmap(chip->ioaddress);
> + chip->ops->unmap_io(chip);
> err_chip:
> kfree(chip);
>
> @@ -229,7 +372,7 @@ static int rtc_remove(struct platform_device *dev)
> if (rtc)
> rtc_device_unregister(rtc);
>
> - iounmap(chip->ioaddress);
> + chip->ops->unmap_io(chip);
> kfree(chip);
>
> return 0;
> diff --git a/include/linux/rtc-v3020.h b/include/linux/rtc-v3020.h
> index bf74e63..8ba646e 100644
> --- a/include/linux/rtc-v3020.h
> +++ b/include/linux/rtc-v3020.h
> @@ -14,6 +14,12 @@
> * is used depends on the board. */
> struct v3020_platform_data {
> int leftshift; /* (1<<(leftshift)) & readl() */
> +
> + int use_gpio:1;
> + unsigned int gpio_cs;
> + unsigned int gpio_wr;
> + unsigned int gpio_rd;
> + unsigned int gpio_io;
> };
>
> #define V3020_STATUS_0 0x00
> --
> 1.5.6.4
>
>
>
>
> --
> Sincerely yours,
> Mike.
>
>
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2009-03-19 21:26 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-02 11:40 [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs Mike Rapoport
2009-03-02 11:40 ` [PATCH] rtc-v3020: coding style cleanup Mike Rapoport
2009-03-02 11:43 ` Alessandro Zummo
2009-03-02 11:40 ` [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs Mike Rapoport
2009-03-02 11:47 ` Alessandro Zummo
2009-03-02 12:18 ` Mike Rapoport
2009-03-03 21:08 ` David Brownell
2009-03-08 10:03 ` Mike Rapoport
2009-03-19 21:25 ` David Brownell
2009-03-02 22:36 ` Andrew Morton
2009-03-03 6:16 ` Mike Rapoport
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox