From patchwork Sun Mar 31 21:56:09 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Clouter X-Patchwork-Id: 232632 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-wi0-x23e.google.com (mail-wi0-x23e.google.com [IPv6:2a00:1450:400c:c05::23e]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 891D02C018B for ; Mon, 1 Apr 2013 08:56:41 +1100 (EST) Received: by mail-wi0-f190.google.com with SMTP id c10sf502377wiw.7 for ; Sun, 31 Mar 2013 14:56:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20120806; h=x-received:mime-version:x-beenthere:x-received:received-spf:from:to :cc:subject:date:message-id:x-mailer:in-reply-to:references :x-wormnet-spam-score:x-wormnet-spam-report:x-original-sender :x-original-authentication-results:reply-to:precedence:mailing-list :list-id:x-google-group-id:list-post:list-help:list-archive:sender :list-subscribe:list-unsubscribe:content-type; bh=2+x/nHImh7LpMr/RrxiVMDykB1HL5CXzV+JefIu+SHk=; b=y1Oupw9Mlh6UHYb0h2o9PEllX4AWF1UoeY/JmV1p4aVPIrTA1/HvSPeL03i4ziB/OM CQ+QuP3G/czctO0oDbHSZ0qpb9+UpE1GljMSN/ykHW4qmfCM6pOEIVqi0XpMt3bG0A+B hcSV52M8vNKu/vJy8gHVOPmb0niq0jmOxEDLpE/gWNfR4V53cHXyvCgA4ZBW9Yoxltxl r0lA3CB1bmVxC8+r6+gincZV14SgjMYfWeDzwzItVDU+E5vxvqG5aWAE84O7Uz3LafVV xmUs58udzTsydrhUpUAmtLyKgOlwIMLpgfgQg3NZKa3xJWWg46sbguis8FIlkS1fTF2d 4jiA== X-Received: by 10.180.96.40 with SMTP id dp8mr319878wib.0.1364766997656; Sun, 31 Mar 2013 14:56:37 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: rtc-linux@googlegroups.com Received: by 10.180.92.68 with SMTP id ck4ls298494wib.24.canary; Sun, 31 Mar 2013 14:56:37 -0700 (PDT) X-Received: by 10.180.95.3 with SMTP id dg3mr1201801wib.4.1364766997376; Sun, 31 Mar 2013 14:56:37 -0700 (PDT) Received: from marmot.wormnet.eu (marmot.wormnet.eu. [2a03:9800:10:2d:3203:b3a5:8bdc:5b68]) by gmr-mx.google.com with ESMTPS id ek20si336285wic.0.2013.03.31.14.56.37 (version=TLSv1 cipher=RC4-SHA bits=128/128); Sun, 31 Mar 2013 14:56:37 -0700 (PDT) Received-SPF: neutral (google.com: 2a03:9800:10:2d:3203:b3a5:8bdc:5b68 is neither permitted nor denied by best guess record for domain of prvs=0802e14cf1=alex@digriz.org.uk) client-ip=2a03:9800:10:2d:3203:b3a5:8bdc:5b68; Received: from [2a01:348:45:1000:d0c7:5dd8:2ab9:8725] (helo=berk.digriz.org.uk) by marmot.wormnet.eu with smtp (Exim 4.80) (envelope-from ) id 1UMQEb-0005Vy-Dk; Sun, 31 Mar 2013 22:56:20 +0100 Received: (nullmailer pid 7140 invoked by uid 1000); Sun, 31 Mar 2013 21:56:13 -0000 From: Alexander Clouter To: Alessandro Zummo , Hartley Sweeten , Grant Likely , Jason Cooper , Ryan Mallon , Rob Herring Cc: devicetree-discuss@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org, rtc-linux@googlegroups.com, Alexander Clouter Subject: [rtc-linux] [PATCH 1/3] rtc: rtc-m48t86: add devicetree bindings Date: Sun, 31 Mar 2013 22:56:09 +0100 Message-Id: <1364766971-5914-2-git-send-email-alex@digriz.org.uk> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1364766971-5914-1-git-send-email-alex@digriz.org.uk> References: <1364766971-5914-1-git-send-email-alex@digriz.org.uk> X-WormNet-Spam-Score: -1.1 (-) X-WormNet-Spam-Report: from marmot.wormnet.eu -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.8 RDNS_NONE Delivered to internal network by a host with no rDNS Tokens: new, 243; hammy, 148; neutral, 132; spammy, 3. X-Original-Sender: alex@digriz.org.uk X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 2a03:9800:10:2d:3203:b3a5:8bdc:5b68 is neither permitted nor denied by best guess record for domain of prvs=0802e14cf1=alex@digriz.org.uk) smtp.mail=prvs=0802e14cf1=alex@digriz.org.uk; dkim=pass header.i=@digriz.org.uk Reply-To: rtc-linux@googlegroups.com Precedence: list Mailing-list: list rtc-linux@googlegroups.com; contact rtc-linux+owners@googlegroups.com List-ID: X-Google-Group-Id: 712029733259 List-Post: , List-Help: , List-Archive: Sender: rtc-linux@googlegroups.com List-Subscribe: , List-Unsubscribe: , This patch allows rtc-m48t86 to be used via devicetree. Signed-off-by: Alexander Clouter --- .../devicetree/bindings/rtc/rtc-m48t86.txt | 17 ++ drivers/rtc/rtc-m48t86.c | 254 +++++++++++++++----- include/linux/m48t86.h | 16 -- 3 files changed, 213 insertions(+), 74 deletions(-) create mode 100644 Documentation/devicetree/bindings/rtc/rtc-m48t86.txt delete mode 100644 include/linux/m48t86.h diff --git a/Documentation/devicetree/bindings/rtc/rtc-m48t86.txt b/Documentation/devicetree/bindings/rtc/rtc-m48t86.txt new file mode 100644 index 0000000..1336c98 --- /dev/null +++ b/Documentation/devicetree/bindings/rtc/rtc-m48t86.txt @@ -0,0 +1,17 @@ +ST M48T86 / Dallas DS12887 RTC driver + +Required properties: +- compatible: "rtc-m48t86" +- reg: array of two address ranges of the RTC index and data registers +- reg-names: must have "rtc_index" and "rtc_data" present + +Example: + +rtc@808 { + #address-cells = <1>; + #size-cells = <1>; + compatible = "rtc-m48t86"; + reg = <0x808 0x04>, + <0x80c 0x04>; + reg-names = "rtc_index", "rtc_data"; +}; diff --git a/drivers/rtc/rtc-m48t86.c b/drivers/rtc/rtc-m48t86.c index 2ffbcac..47e9fd1 100644 --- a/drivers/rtc/rtc-m48t86.c +++ b/drivers/rtc/rtc-m48t86.c @@ -16,8 +16,10 @@ #include #include #include -#include #include +#include +#include +#include #define M48T86_REG_SEC 0x00 #define M48T86_REG_SECALRM 0x01 @@ -41,40 +43,61 @@ #define DRV_VERSION "0.1" +struct m48t86_rtc_private_data { + void __iomem *io_index; + void __iomem *io_data; + + struct rtc_device *rtc; +}; + +static unsigned char m48t86_rtc_readbyte( + struct m48t86_rtc_private_data *priv, + unsigned long addr) +{ + writeb(addr, priv->io_index); + return readb(priv->io_data); +} + +static void m48t86_rtc_writebyte( + struct m48t86_rtc_private_data *priv, + unsigned char value, unsigned long addr) +{ + writeb(addr, priv->io_index); + writeb(value, priv->io_data); +} static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm) { unsigned char reg; - struct platform_device *pdev = to_platform_device(dev); - struct m48t86_ops *ops = pdev->dev.platform_data; + struct m48t86_rtc_private_data *priv = dev_get_drvdata(dev); - reg = ops->readbyte(M48T86_REG_B); + reg = m48t86_rtc_readbyte(priv, M48T86_REG_B); if (reg & M48T86_REG_B_DM) { /* data (binary) mode */ - tm->tm_sec = ops->readbyte(M48T86_REG_SEC); - tm->tm_min = ops->readbyte(M48T86_REG_MIN); - tm->tm_hour = ops->readbyte(M48T86_REG_HOUR) & 0x3F; - tm->tm_mday = ops->readbyte(M48T86_REG_DOM); + tm->tm_sec = m48t86_rtc_readbyte(priv, M48T86_REG_SEC); + tm->tm_min = m48t86_rtc_readbyte(priv, M48T86_REG_MIN); + tm->tm_hour = m48t86_rtc_readbyte(priv, M48T86_REG_HOUR) & 0x3F; + tm->tm_mday = m48t86_rtc_readbyte(priv, M48T86_REG_DOM); /* tm_mon is 0-11 */ - tm->tm_mon = ops->readbyte(M48T86_REG_MONTH) - 1; - tm->tm_year = ops->readbyte(M48T86_REG_YEAR) + 100; - tm->tm_wday = ops->readbyte(M48T86_REG_DOW); + tm->tm_mon = m48t86_rtc_readbyte(priv, M48T86_REG_MONTH) - 1; + tm->tm_year = m48t86_rtc_readbyte(priv, M48T86_REG_YEAR) + 100; + tm->tm_wday = m48t86_rtc_readbyte(priv, M48T86_REG_DOW); } else { /* bcd mode */ - tm->tm_sec = bcd2bin(ops->readbyte(M48T86_REG_SEC)); - tm->tm_min = bcd2bin(ops->readbyte(M48T86_REG_MIN)); - tm->tm_hour = bcd2bin(ops->readbyte(M48T86_REG_HOUR) & 0x3F); - tm->tm_mday = bcd2bin(ops->readbyte(M48T86_REG_DOM)); + tm->tm_sec = bcd2bin(m48t86_rtc_readbyte(priv, M48T86_REG_SEC)); + tm->tm_min = bcd2bin(m48t86_rtc_readbyte(priv, M48T86_REG_MIN)); + tm->tm_hour = bcd2bin(m48t86_rtc_readbyte(priv, M48T86_REG_HOUR) & 0x3F); + tm->tm_mday = bcd2bin(m48t86_rtc_readbyte(priv, M48T86_REG_DOM)); /* tm_mon is 0-11 */ - tm->tm_mon = bcd2bin(ops->readbyte(M48T86_REG_MONTH)) - 1; - tm->tm_year = bcd2bin(ops->readbyte(M48T86_REG_YEAR)) + 100; - tm->tm_wday = bcd2bin(ops->readbyte(M48T86_REG_DOW)); + tm->tm_mon = bcd2bin(m48t86_rtc_readbyte(priv, M48T86_REG_MONTH)) - 1; + tm->tm_year = bcd2bin(m48t86_rtc_readbyte(priv, M48T86_REG_YEAR)) + 100; + tm->tm_wday = bcd2bin(m48t86_rtc_readbyte(priv, M48T86_REG_DOW)); } /* correct the hour if the clock is in 12h mode */ if (!(reg & M48T86_REG_B_H24)) - if (ops->readbyte(M48T86_REG_HOUR) & 0x80) + if (m48t86_rtc_readbyte(priv, M48T86_REG_HOUR) & 0x80) tm->tm_hour += 12; return rtc_valid_tm(tm); @@ -83,38 +106,37 @@ static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm) static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm) { unsigned char reg; - struct platform_device *pdev = to_platform_device(dev); - struct m48t86_ops *ops = pdev->dev.platform_data; + struct m48t86_rtc_private_data *priv = dev_get_drvdata(dev); - reg = ops->readbyte(M48T86_REG_B); + reg = m48t86_rtc_readbyte(priv, M48T86_REG_B); /* update flag and 24h mode */ reg |= M48T86_REG_B_SET | M48T86_REG_B_H24; - ops->writebyte(reg, M48T86_REG_B); + m48t86_rtc_writebyte(priv, reg, M48T86_REG_B); if (reg & M48T86_REG_B_DM) { /* data (binary) mode */ - ops->writebyte(tm->tm_sec, M48T86_REG_SEC); - ops->writebyte(tm->tm_min, M48T86_REG_MIN); - ops->writebyte(tm->tm_hour, M48T86_REG_HOUR); - ops->writebyte(tm->tm_mday, M48T86_REG_DOM); - ops->writebyte(tm->tm_mon + 1, M48T86_REG_MONTH); - ops->writebyte(tm->tm_year % 100, M48T86_REG_YEAR); - ops->writebyte(tm->tm_wday, M48T86_REG_DOW); + m48t86_rtc_writebyte(priv, tm->tm_sec, M48T86_REG_SEC); + m48t86_rtc_writebyte(priv, tm->tm_min, M48T86_REG_MIN); + m48t86_rtc_writebyte(priv, tm->tm_hour, M48T86_REG_HOUR); + m48t86_rtc_writebyte(priv, tm->tm_mday, M48T86_REG_DOM); + m48t86_rtc_writebyte(priv, tm->tm_mon + 1, M48T86_REG_MONTH); + m48t86_rtc_writebyte(priv, tm->tm_year % 100, M48T86_REG_YEAR); + m48t86_rtc_writebyte(priv, tm->tm_wday, M48T86_REG_DOW); } else { /* bcd mode */ - ops->writebyte(bin2bcd(tm->tm_sec), M48T86_REG_SEC); - ops->writebyte(bin2bcd(tm->tm_min), M48T86_REG_MIN); - ops->writebyte(bin2bcd(tm->tm_hour), M48T86_REG_HOUR); - ops->writebyte(bin2bcd(tm->tm_mday), M48T86_REG_DOM); - ops->writebyte(bin2bcd(tm->tm_mon + 1), M48T86_REG_MONTH); - ops->writebyte(bin2bcd(tm->tm_year % 100), M48T86_REG_YEAR); - ops->writebyte(bin2bcd(tm->tm_wday), M48T86_REG_DOW); + m48t86_rtc_writebyte(priv, bin2bcd(tm->tm_sec), M48T86_REG_SEC); + m48t86_rtc_writebyte(priv, bin2bcd(tm->tm_min), M48T86_REG_MIN); + m48t86_rtc_writebyte(priv, bin2bcd(tm->tm_hour), M48T86_REG_HOUR); + m48t86_rtc_writebyte(priv, bin2bcd(tm->tm_mday), M48T86_REG_DOM); + m48t86_rtc_writebyte(priv, bin2bcd(tm->tm_mon + 1), M48T86_REG_MONTH); + m48t86_rtc_writebyte(priv, bin2bcd(tm->tm_year % 100), M48T86_REG_YEAR); + m48t86_rtc_writebyte(priv, bin2bcd(tm->tm_wday), M48T86_REG_DOW); } /* update ended */ reg &= ~M48T86_REG_B_SET; - ops->writebyte(reg, M48T86_REG_B); + m48t86_rtc_writebyte(priv, reg, M48T86_REG_B); return 0; } @@ -122,15 +144,14 @@ static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm) static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq) { unsigned char reg; - struct platform_device *pdev = to_platform_device(dev); - struct m48t86_ops *ops = pdev->dev.platform_data; + struct m48t86_rtc_private_data *priv = dev_get_drvdata(dev); - reg = ops->readbyte(M48T86_REG_B); + reg = m48t86_rtc_readbyte(priv, M48T86_REG_B); seq_printf(seq, "mode\t\t: %s\n", (reg & M48T86_REG_B_DM) ? "binary" : "bcd"); - reg = ops->readbyte(M48T86_REG_D); + reg = m48t86_rtc_readbyte(priv, M48T86_REG_D); seq_printf(seq, "battery\t\t: %s\n", (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted"); @@ -144,42 +165,159 @@ static const struct rtc_class_ops m48t86_rtc_ops = { .proc = m48t86_rtc_proc, }; -static int m48t86_rtc_probe(struct platform_device *dev) +/* + * The RTC chip has 114 bytes upper bytes that can be used as user storage + * space which we can use to test if the chip is present; for example it is + * an optional feature and not all boards will have it present. + * + * I've used the method Technologic Systems use in their rtc7800.c example + * for the detection. + * + * TODO: track down a guinea pig without an RTC to see if we can work out a + * better RTC detection routine + */ +static int m48t86_rtc_detect(struct device *dev) +{ + struct m48t86_rtc_private_data *priv = dev_get_drvdata(dev); + + unsigned char tmp_rtc0, tmp_rtc1; + + tmp_rtc0 = m48t86_rtc_readbyte(priv, 126); + tmp_rtc1 = m48t86_rtc_readbyte(priv, 127); + + m48t86_rtc_writebyte(priv, 0x00, 126); + m48t86_rtc_writebyte(priv, 0x55, 127); + if (m48t86_rtc_readbyte(priv, 127) == 0x55) { + m48t86_rtc_writebyte(priv, 0xaa, 127); + if (m48t86_rtc_readbyte(priv, 127) == 0xaa + && m48t86_rtc_readbyte(priv, 126) == 0x00) { + m48t86_rtc_writebyte(priv, tmp_rtc0, 126); + m48t86_rtc_writebyte(priv, tmp_rtc1, 127); + + return 0; + } + } + + dev_info(dev, "RTC not found\n"); + return -ENODEV; +} + +static int m48t86_rtc_probe(struct platform_device *pdev) { + struct m48t86_rtc_private_data *priv; + struct resource *res_index, *res_data; unsigned char reg; - struct m48t86_ops *ops = dev->dev.platform_data; - struct rtc_device *rtc = rtc_device_register("m48t86", - &dev->dev, &m48t86_rtc_ops, THIS_MODULE); + int err; + + res_index = platform_get_resource_byname( + pdev, IORESOURCE_MEM, "rtc_index"); + if (!res_index) + return -ENXIO; + res_data = platform_get_resource_byname( + pdev, IORESOURCE_MEM, "rtc_data"); + if (!res_data) + return -ENXIO; + + /* Allocate memory for the device structure (and zero it) */ + priv = kzalloc(sizeof(struct m48t86_rtc_private_data), GFP_KERNEL); + if (!priv) { + dev_err(&pdev->dev, "failed to allocate device structure.\n"); + return -ENOMEM; + } + + if (!request_mem_region(res_index->start, resource_size(res_index), + dev_name(&pdev->dev))) { + dev_err(&pdev->dev, "request_mem_region failed\n"); + err = -EBUSY; + goto out_free; + } + if (!request_mem_region(res_data->start, resource_size(res_data), + dev_name(&pdev->dev))) { + dev_err(&pdev->dev, "request_mem_region failed\n"); + err = -EBUSY; + goto out_release_io_index; + } + + priv->io_index = ioremap(res_index->start, resource_size(res_index)); + if (priv->io_index == NULL) { + dev_err(&pdev->dev, "ioremap failed\n"); + err = -EIO; + goto out_release_io_data; + } + priv->io_data = ioremap(res_data->start, resource_size(res_data)); + if (priv->io_data == NULL) { + dev_err(&pdev->dev, "ioremap failed\n"); + err = -EIO; + goto out_ctrl; + } + + platform_set_drvdata(pdev, priv); - if (IS_ERR(rtc)) - return PTR_ERR(rtc); + err = m48t86_rtc_detect(&pdev->dev); + if (err) + goto out; - platform_set_drvdata(dev, rtc); + priv->rtc = rtc_device_register("m48t86", + &pdev->dev, &m48t86_rtc_ops, THIS_MODULE); + if (IS_ERR(priv->rtc)) { + err = PTR_ERR(priv->rtc); + goto out; + } /* read battery status */ - reg = ops->readbyte(M48T86_REG_D); - dev_info(&dev->dev, "battery %s\n", + reg = m48t86_rtc_readbyte(priv, M48T86_REG_D); + dev_info(&pdev->dev, "battery %s\n", (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted"); return 0; + +out: + platform_set_drvdata(pdev, NULL); + iounmap(priv->io_data); +out_ctrl: + iounmap(priv->io_index); +out_release_io_data: + release_mem_region(res_data->start, resource_size(res_data)); +out_release_io_index: + release_mem_region(res_index->start, resource_size(res_index)); +out_free: + kfree(priv); + return err; } -static int m48t86_rtc_remove(struct platform_device *dev) +static int m48t86_rtc_remove(struct platform_device *pdev) { - struct rtc_device *rtc = platform_get_drvdata(dev); + struct m48t86_rtc_private_data *priv = platform_get_drvdata(pdev); + struct resource *res; + + rtc_device_unregister(priv->rtc); - if (rtc) - rtc_device_unregister(rtc); + platform_set_drvdata(pdev, NULL); - platform_set_drvdata(dev, NULL); + iounmap(priv->io_data); + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc_data"); + release_mem_region(res->start, resource_size(res)); + + iounmap(priv->io_index); + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc_index"); + release_mem_region(res->start, resource_size(res)); + + kfree(priv); return 0; } +static const struct of_device_id m48t86_rtc_match[] = { + { .compatible = "rtc-m48t86" }, + {}, +}; +MODULE_DEVICE_TABLE(of, m48t86_rtc_match); + static struct platform_driver m48t86_rtc_platform_driver = { .driver = { - .name = "rtc-m48t86", - .owner = THIS_MODULE, + .name = "rtc-m48t86", + .owner = THIS_MODULE, + .of_match_table = m48t86_rtc_match, }, .probe = m48t86_rtc_probe, .remove = m48t86_rtc_remove, diff --git a/include/linux/m48t86.h b/include/linux/m48t86.h deleted file mode 100644 index 915d6b4..0000000 --- a/include/linux/m48t86.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * ST M48T86 / Dallas DS12887 RTC driver - * Copyright (c) 2006 Tower Technologies - * - * Author: Alessandro Zummo - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. -*/ - -struct m48t86_ops -{ - void (*writebyte)(unsigned char value, unsigned long addr); - unsigned char (*readbyte)(unsigned long addr); -};