From patchwork Wed Jul 10 14:00:48 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonas Jensen X-Patchwork-Id: 2825735 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 85AC3C0AB2 for ; Wed, 10 Jul 2013 14:02:07 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7713D20173 for ; Wed, 10 Jul 2013 14:02:02 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9C6C720161 for ; Wed, 10 Jul 2013 14:02:00 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1Uwuxh-0001u2-SE; Wed, 10 Jul 2013 14:01:42 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1Uwuxf-0005Yf-G2; Wed, 10 Jul 2013 14:01:39 +0000 Received: from mail-lb0-x234.google.com ([2a00:1450:4010:c04::234]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1Uwuxc-0005X9-4l for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2013 14:01:37 +0000 Received: by mail-lb0-f180.google.com with SMTP id o10so5745925lbi.39 for ; Wed, 10 Jul 2013 07:01:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer; bh=qsqecWOjpshmHKY/MpL+TL912da3o1hr6NjiDTCDJ7Y=; b=Xi8L4jv+lC5EpeFUrhgHWcliFZ8MD8z6wuBcgoBc3seg25HKc2KlQ52ZkbhgtRG6zt 2gU7SYhdCRxle7qihXudMvdnCv4ewSEMKl9FoQ8S/i3ruAXuXjauZU2Vyv5nThLxtbXX IeMb2asuRxD0zNIsVNPc8aP3LmNNvXSG2kyH2qizPbTF4b56i+AEY1jOUJ3PYY7VMhon WPm228FjiCU7KCFy6vMARbbglC5FUMdEH5EsLpzpK9bf2YKoDmPnEzxP8sB0xmJpFtOK 0evtHj3NGSlYhh7t9gjhuxFHuUwswzFf0nr4cZIT5EEDH9pKwFbvOSliXNpKWYwcdJ6u CQkA== X-Received: by 10.112.54.161 with SMTP id k1mr15152327lbp.21.1373464874017; Wed, 10 Jul 2013 07:01:14 -0700 (PDT) Received: from Ildjarn.ildjarn.botech.se (static-213-115-41-10.sme.bredbandsbolaget.se. [213.115.41.10]) by mx.google.com with ESMTPSA id rx1sm10811666lbb.0.2013.07.10.07.01.11 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 10 Jul 2013 07:01:12 -0700 (PDT) From: Jonas Jensen To: rtc-linux@googlegroups.com Subject: [PATCH] rtc: Add MOXA ART RTC driver Date: Wed, 10 Jul 2013 16:00:48 +0200 Message-Id: <1373464848-28146-1-git-send-email-jonas.jensen@gmail.com> X-Mailer: git-send-email 1.8.2.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130710_100136_499586_22DB2892 X-CRM114-Status: GOOD ( 22.81 ) X-Spam-Score: -2.0 (--) Cc: a.zummo@towertech.it, arm@kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Jonas Jensen X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add RTC driver for MOXA ART SoCs. Signed-off-by: Jonas Jensen --- Notes: Applies to next-20130703 drivers/rtc/Kconfig | 9 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-moxart.c | 335 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 345 insertions(+) create mode 100644 drivers/rtc/rtc-moxart.c diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index b983813..7035955 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -1233,6 +1233,15 @@ config RTC_DRV_SNVS This driver can also be built as a module, if so, the module will be called "rtc-snvs". +config RTC_DRV_MOXART + tristate "MOXA ART RTC" + help + If you say yes here you get support for the MOXA ART + RTC module. + + This driver can also be built as a module. If so, the module + will be called rtc-moxart + comment "HID Sensor RTC drivers" config RTC_DRV_HID_SENSOR_TIME diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index c33f86f..c0381fd 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -77,6 +77,7 @@ obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o obj-$(CONFIG_RTC_DRV_MAX77686) += rtc-max77686.o obj-$(CONFIG_RTC_DRV_MC13XXX) += rtc-mc13xxx.o obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o +obj-$(CONFIG_RTC_DRV_MOXART) += rtc-moxart.o obj-$(CONFIG_RTC_DRV_MPC5121) += rtc-mpc5121.o obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o obj-$(CONFIG_RTC_DRV_NUC900) += rtc-nuc900.o diff --git a/drivers/rtc/rtc-moxart.c b/drivers/rtc/rtc-moxart.c new file mode 100644 index 0000000..cfdbedc --- /dev/null +++ b/drivers/rtc/rtc-moxart.c @@ -0,0 +1,335 @@ +/* + * MOXA ART RTC driver. + * + * Copyright (C) 2013 Jonas Jensen + * + * Jonas Jensen + * + * Based on code from + * Moxa Technology Co., Ltd. + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include + +#define GPIO_EM1240_HIGH 1 +#define GPIO_EM1240_LOW 0 +#define GPIO_EM1240_OUTPUT 1 +#define GPIO_EM1240_INPUT 0 + +#define GPIO_RTC_SCLK (5) +#define GPIO_RTC_DATA (6) +#define GPIO_RTC_RESET (7) + +#define GPIO_RTC_RESERVED 0x0C +#define GPIO_RTC_DATA_SET 0x10 +#define GPIO_RTC_DATA_CLEAR 0x14 +#define GPIO_RTC_PIN_PULL_ENABLE 0x18 +#define GPIO_RTC_PIN_PULL_TYPE 0x1C +#define GPIO_RTC_INT_ENABLE 0x20 +#define GPIO_RTC_INT_RAW_STATE 0x24 +#define GPIO_RTC_INT_MASKED_STATE 0x28 +#define GPIO_RTC_INT_MASK 0x2C +#define GPIO_RTC_INT_CLEAR 0x30 +#define GPIO_RTC_INT_TRIGGER 0x34 +#define GPIO_RTC_INT_BOTH 0x38 +#define GPIO_RTC_INT_RISE_NEG 0x3C +#define GPIO_RTC_BOUNCE_ENABLE 0x40 +#define GPIO_RTC_BOUNCE_PRE_SCALE 0x44 + +#define GPIO_RTC_PROTECT_W 0x8E +#define GPIO_RTC_PROTECT_R 0x8F +#define GPIO_RTC_YEAR_W 0x8C +#define GPIO_RTC_YEAR_R 0x8D +#define GPIO_RTC_DAY_W 0x8A +#define GPIO_RTC_DAY_R 0x8B +#define GPIO_RTC_MONTH_W 0x88 +#define GPIO_RTC_MONTH_R 0x89 +#define GPIO_RTC_DATE_W 0x86 +#define GPIO_RTC_DATE_R 0x87 +#define GPIO_RTC_HOURS_W 0x84 +#define GPIO_RTC_HOURS_R 0x85 +#define GPIO_RTC_MINUTES_W 0x82 +#define GPIO_RTC_MINUTES_R 0x83 +#define GPIO_RTC_SECONDS_W 0x80 +#define GPIO_RTC_SECONDS_R 0x81 +#define GPIO_RTC_DELAY_TIME 8 /* 8 usecond */ +#define GPIO_RTC_IS_OPEN 0x01 /* /dev/rtc in use */ +#define GPIO_PIO(x) (1 << x) + +struct rtc_plat_data { + struct rtc_device *rtc; +}; + +static spinlock_t rtc_lock; +static int day_of_year[12] = { 0, 31, 59, 90, 120, 151, 181, + 212, 243, 273, 304, 334 }; + +void moxart_rtc_write_byte(u8 data) +{ + int i; + + for (i = 0; i < 8; i++, data >>= 1) { + gpio_set_value(GPIO_RTC_SCLK, GPIO_EM1240_LOW); + gpio_set_value(GPIO_RTC_DATA, ((data & 1) == 1)); + udelay(GPIO_RTC_DELAY_TIME); + gpio_set_value(GPIO_RTC_SCLK, GPIO_EM1240_HIGH); + udelay(GPIO_RTC_DELAY_TIME); + } +} + +u8 moxart_rtc_read_byte(void) +{ + int i; + u8 data = 0; + + for (i = 0; i < 8; i++) { + gpio_set_value(GPIO_RTC_SCLK, GPIO_EM1240_LOW); + udelay(GPIO_RTC_DELAY_TIME); + gpio_set_value(GPIO_RTC_SCLK, GPIO_EM1240_HIGH); + if (gpio_get_value(GPIO_RTC_DATA)) + data |= (1 << i); + udelay(GPIO_RTC_DELAY_TIME); + } + return data; +} + +static u8 moxart_rtc_read_register(u8 cmd) +{ + u8 data; + unsigned long flags; + + local_irq_save(flags); + + gpio_direction_output(GPIO_RTC_DATA, 0); + gpio_set_value(GPIO_RTC_RESET, GPIO_EM1240_HIGH); + udelay(GPIO_RTC_DELAY_TIME); + moxart_rtc_write_byte(cmd); + gpio_direction_input(GPIO_RTC_DATA); + udelay(GPIO_RTC_DELAY_TIME); + data = moxart_rtc_read_byte(); + gpio_set_value(GPIO_RTC_SCLK, GPIO_EM1240_LOW); + gpio_set_value(GPIO_RTC_RESET, GPIO_EM1240_LOW); + udelay(GPIO_RTC_DELAY_TIME); + + local_irq_restore(flags); + + return data; +} + +static void moxart_rtc_write_register(u8 cmd, u8 data) +{ + unsigned long flags; + + local_irq_save(flags); + + gpio_direction_output(GPIO_RTC_DATA, 0); + gpio_set_value(GPIO_RTC_RESET, GPIO_EM1240_HIGH); + udelay(GPIO_RTC_DELAY_TIME); + moxart_rtc_write_byte(cmd); + moxart_rtc_write_byte(data); + gpio_set_value(GPIO_RTC_SCLK, GPIO_EM1240_LOW); + gpio_set_value(GPIO_RTC_RESET, GPIO_EM1240_LOW); + udelay(GPIO_RTC_DELAY_TIME); + + local_irq_restore(flags); +} + +static int moxart_rtc_set_time(struct device *dev, struct rtc_time *tm) +{ + spin_lock_irq(&rtc_lock); + + moxart_rtc_write_register(GPIO_RTC_PROTECT_W, 0); + moxart_rtc_write_register(GPIO_RTC_YEAR_W, + (((tm->tm_year - 100) / 10) << 4) | + ((tm->tm_year - 100) % 10)); + + moxart_rtc_write_register(GPIO_RTC_MONTH_W, + (((tm->tm_mon + 1) / 10) << 4) | + ((tm->tm_mon + 1) % 10)); + + moxart_rtc_write_register(GPIO_RTC_DATE_W, + ((tm->tm_mday / 10) << 4) | + (tm->tm_mday % 10)); + + moxart_rtc_write_register(GPIO_RTC_HOURS_W, + ((tm->tm_hour / 10) << 4) | + (tm->tm_hour % 10)); + + moxart_rtc_write_register(GPIO_RTC_MINUTES_W, + ((tm->tm_min / 10) << 4) | + (tm->tm_min % 10)); + + moxart_rtc_write_register(GPIO_RTC_SECONDS_W, + ((tm->tm_sec / 10) << 4) | + (tm->tm_sec % 10)); + + moxart_rtc_write_register(GPIO_RTC_PROTECT_W, 0x80); + + spin_unlock_irq(&rtc_lock); + + dev_dbg(dev, "%s: success tm_year=%d tm_mon=%d\n" + "tm_mday=%d tm_hour=%d tm_min=%d tm_sec=%d\n", + __func__, tm->tm_year, tm->tm_mon, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); + + return 0; +} + +static int moxart_rtc_read_time(struct device *dev, struct rtc_time *tm) +{ + unsigned char v; + + spin_lock_irq(&rtc_lock); + + v = moxart_rtc_read_register(GPIO_RTC_SECONDS_R); + tm->tm_sec = (((v & 0x70) >> 4) * 10) + (v & 0x0F); + + v = moxart_rtc_read_register(GPIO_RTC_MINUTES_R); + tm->tm_min = (((v & 0x70) >> 4) * 10) + (v & 0x0F); + + v = moxart_rtc_read_register(GPIO_RTC_HOURS_R); + if (v & 0x80) { /* 12-hour mode */ + tm->tm_hour = (((v & 0x10) >> 4) * 10) + (v & 0x0F); + if (v & 0x20) { /* PM mode */ + tm->tm_hour += 12; + if (tm->tm_hour >= 24) + tm->tm_hour = 0; + } + } else { /* 24-hour mode */ + tm->tm_hour = (((v & 0x30) >> 4) * 10) + (v & 0x0F); + } + + v = moxart_rtc_read_register(GPIO_RTC_DATE_R); + tm->tm_mday = (((v & 0x30) >> 4) * 10) + (v & 0x0F); + + v = moxart_rtc_read_register(GPIO_RTC_MONTH_R); + tm->tm_mon = (((v & 0x10) >> 4) * 10) + (v & 0x0F); + tm->tm_mon--; + + v = moxart_rtc_read_register(GPIO_RTC_YEAR_R); + tm->tm_year = (((v & 0xF0) >> 4) * 10) + (v & 0x0F); + tm->tm_year += 100; + if (tm->tm_year <= 69) + tm->tm_year += 100; + + v = moxart_rtc_read_register(GPIO_RTC_DAY_R); + tm->tm_wday = (v & 0x0f) - 1; + tm->tm_yday = day_of_year[tm->tm_mon]; + tm->tm_yday += (tm->tm_mday - 1); + if (tm->tm_mon >= 2) { + if (!(tm->tm_year % 4) && (tm->tm_year % 100)) + tm->tm_yday++; + } + + tm->tm_isdst = 0; + + spin_unlock_irq(&rtc_lock); + + return 0; +} + +static int moxart_rtc_ioctl(struct device *dev, unsigned int cmd, + unsigned long arg) +{ + switch (cmd) { + default: + return -ENOIOCTLCMD; + } + + return 0; +} + +static const struct rtc_class_ops moxart_rtc_ops = { + .read_time = moxart_rtc_read_time, + .set_time = moxart_rtc_set_time, + .ioctl = moxart_rtc_ioctl, +}; + +static int moxart_rtc_probe(struct platform_device *pdev) +{ + struct rtc_device *rtc; + struct rtc_plat_data *pdata = NULL; + int ret = 0; + + pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); + if (!pdata) { + dev_err(&pdev->dev, "devm_kzalloc failed\n"); + return -ENOMEM; + } + + spin_lock_init(&rtc_lock); + + gpio_request(GPIO_RTC_DATA, "rtc_data"); + gpio_request(GPIO_RTC_SCLK, "rtc_sclk"); + gpio_request(GPIO_RTC_RESET, "rtc_reset"); + + gpio_direction_output(GPIO_RTC_RESET, 0); + gpio_direction_output(GPIO_RTC_SCLK, 0); + + rtc = rtc_device_register(pdev->name, &pdev->dev, + &moxart_rtc_ops, THIS_MODULE); + if (IS_ERR(rtc)) { + ret = PTR_ERR(rtc); + goto out; + } + + pdata->rtc = rtc; + platform_set_drvdata(pdev, pdata); + + dev_dbg(&pdev->dev, "%s\n", __func__); + + if (ret) + goto out; + + return 0; + + out: + if (pdata->rtc) + rtc_device_unregister(pdata->rtc); + devm_kfree(&pdev->dev, pdata); + return ret; +} + +static int moxart_rtc_remove(struct platform_device *pdev) +{ + struct rtc_plat_data *pdata = platform_get_drvdata(pdev); + + gpio_free(GPIO_RTC_DATA); + gpio_free(GPIO_RTC_SCLK); + gpio_free(GPIO_RTC_RESET); + + rtc_device_unregister(pdata->rtc); + devm_kfree(&pdev->dev, pdata); + + return 0; +} + +static const struct of_device_id moxart_rtc_match[] = { + { .compatible = "moxa,moxart-rtc" }, + { }, +}; + +static struct platform_driver moxart_rtc_driver = { + .probe = moxart_rtc_probe, + .remove = moxart_rtc_remove, + .driver = { + .name = "moxart-rtc", + .owner = THIS_MODULE, + .of_match_table = moxart_rtc_match, + }, +}; +module_platform_driver(moxart_rtc_driver); + +MODULE_DESCRIPTION("MOXART RTC driver"); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Jonas Jensen ");