From patchwork Mon Feb 23 14:53:11 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 23562 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from yx-out-2122.google.com (yx-out-2122.google.com [74.125.44.24]) by ozlabs.org (Postfix) with ESMTP id F1A48DDEF3 for ; Tue, 24 Feb 2009 01:53:29 +1100 (EST) Received: by yx-out-2122.google.com with SMTP id 8so678126yxj.11 for ; Mon, 23 Feb 2009 06:53:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=beta; h=domainkey-signature:received:received:x-sender:x-apparently-to :received:received:received-spf:authentication-results:received :received:received:received:x-authentication-warning:from:to:cc :subject:date:message-id:x-mailer:in-reply-to:references:x-antiabuse :x-antiabuse:x-antiabuse:x-antiabuse:x-antiabuse:x-source :x-source-args:x-source-dir:reply-to:sender:precedence:x-google-loop :mailing-list:list-id:list-post:list-help:list-unsubscribe :x-beenthere-env:x-beenthere; bh=gx5A8Ss9/DIAu9zWP1mLKeUi5WUQzgj1FZu1GzkSa6c=; b=yV3vN+fVh2e8K2J2A4vjBwqw3zlwdyWZ+4KUUjwuDSiHPFvQ28ypJksSmGEMvIXMkZ QxrAtDbTdiwWPQ0keUXQYtsbUo27NfNtUM2up7Y02+wyThA8nY8WZ+7cSOXk8luY6D9A X4UFwK2Nhgv98HIF/b9FVSjHTDAA2s6FoVQfo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlegroups.com; s=beta; h=x-sender:x-apparently-to:received-spf:authentication-results :x-authentication-warning:from:to:cc:subject:date:message-id :x-mailer:in-reply-to:references:x-antiabuse:x-source:x-source-args :x-source-dir:reply-to:sender:precedence:x-google-loop:mailing-list :list-id:list-post:list-help:list-unsubscribe:x-beenthere-env :x-beenthere; b=AcI+E9e2LdVC7KAImQlMQ8wIgxU0bQUDzPxVnFgQkOro3U7N4Y74iDM61r+sdnEHht iNCBWQgwdiFSZ/jKVAbj6ejCn06W5aUtD4v5iOmKaOeue79lxDItsnanaiuIiNwMwfbY JhXBeYOeBPgUy1s6ITwB91TQnY8/k3wsxkFLA= Received: by 10.90.100.20 with SMTP id x20mr549616agb.9.1235400805029; Mon, 23 Feb 2009 06:53:25 -0800 (PST) Received: by 10.177.127.42 with SMTP id e42gr1932yqn.0; Mon, 23 Feb 2009 06:53:25 -0800 (PST) X-Sender: mike@compulab.co.il X-Apparently-To: rtc-linux@googlegroups.com Received: by 10.100.8.4 with SMTP id 4mr4614066anh.10.1235400804511; Mon, 23 Feb 2009 06:53:24 -0800 (PST) Received: from cathcart.site5.com (cathcart.site5.com [74.54.107.137]) by mx.google.com with ESMTP id 16si1319392gxk.5.2009.02.23.06.53.24; Mon, 23 Feb 2009 06:53:24 -0800 (PST) Received-SPF: neutral (google.com: 74.54.107.137 is neither permitted nor denied by best guess record for domain of mike@compulab.co.il) client-ip=74.54.107.137; Authentication-Results: mx.google.com; spf=neutral (google.com: 74.54.107.137 is neither permitted nor denied by best guess record for domain of mike@compulab.co.il) smtp.mail=mike@compulab.co.il Received: from [192.114.83.142] (helo=mail.linux-boards.com) by cathcart.site5.com with esmtpsa (TLSv1:DES-CBC3-SHA:168) (Exim 4.69) (envelope-from ) id 1LbcBH-0004Xv-9E; Mon, 23 Feb 2009 08:53:16 -0600 Received: from gentoodev.compulab.local (mike-pc.compulab.local [10.1.1.95]) by mail.linux-boards.com (8.12.5/8.12.8) with ESMTP id n1NErDTW013753; Mon, 23 Feb 2009 16:53:13 +0200 Received: from gentoodev.compulab.local (localhost [127.0.0.1]) by gentoodev.compulab.local (8.14.0/8.14.0) with ESMTP id n1NErEDY010567; Mon, 23 Feb 2009 16:53:14 +0200 Received: (from mike@localhost) by gentoodev.compulab.local (8.14.0/8.14.0/Submit) id n1NErElV010566; Mon, 23 Feb 2009 16:53:14 +0200 X-Authentication-Warning: gentoodev.compulab.local: mike set sender to mike@compulab.co.il using -f From: Mike Rapoport To: a.zummo@towertech.it Cc: rtc-linux@googlegroups.com, raph@8d.com, Mike Rapoport Subject: [rtc-linux] [PATCH] rtc-v3020: add ability to access v3020 chip with GPIOs Date: Mon, 23 Feb 2009 16:53:11 +0200 Message-Id: X-Mailer: git-send-email 1.5.6.4 In-Reply-To: References: X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - cathcart.site5.com X-AntiAbuse: Original Domain - googlegroups.com X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - compulab.co.il X-Source: X-Source-Args: X-Source-Dir: Reply-To: rtc-linux@googlegroups.com Sender: rtc-linux@googlegroups.com Precedence: bulk X-Google-Loop: groups Mailing-List: list rtc-linux@googlegroups.com; contact rtc-linux+owner@googlegroups.com List-Id: List-Post: List-Help: List-Unsubscribe: , X-BeenThere-Env: rtc-linux@googlegroups.com X-BeenThere: rtc-linux@googlegroups.com 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 --- 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 #include #include +#include #include #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