From patchwork Wed Jan 10 19:31:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Reichel X-Patchwork-Id: 858506 X-Patchwork-Delegate: sbabic@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 3zH0KY4j81z9s0g for ; Thu, 11 Jan 2018 07:02:25 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 1BD34C21E37; Wed, 10 Jan 2018 20:00:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 0C43FC21E97; Wed, 10 Jan 2018 19:58:28 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id D7977C21DA5; Wed, 10 Jan 2018 19:32:10 +0000 (UTC) Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227]) by lists.denx.de (Postfix) with ESMTPS id E4F02C21DF4 for ; Wed, 10 Jan 2018 19:32:08 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: sre) with ESMTPSA id 5661F2602DA From: Sebastian Reichel To: Sebastian Reichel , Stefano Babic , u-boot@lists.denx.de Date: Wed, 10 Jan 2018 20:31:27 +0100 Message-Id: <20180110193138.11169-5-sebastian.reichel@collabora.co.uk> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180110193138.11169-1-sebastian.reichel@collabora.co.uk> References: <20180110193138.11169-1-sebastian.reichel@collabora.co.uk> X-Mailman-Approved-At: Wed, 10 Jan 2018 19:58:21 +0000 Cc: Martyn Welch , Nandor Han Subject: [U-Boot] [PATCHv2 04/15] rtc: adding RX8010SJ rtc driver X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: Nandor Han Add a new driver for RX8010SJ rtc chip. The driver implements both formats of U-Boot driver model. Signed-off-by: Nandor Han Signed-off-by: Martyn Welch Signed-off-by: Sebastian Reichel --- drivers/rtc/Kconfig | 6 + drivers/rtc/Makefile | 1 + drivers/rtc/rx8010sj.c | 378 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 385 insertions(+) create mode 100644 drivers/rtc/rx8010sj.c diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 2964bb221106..95ac0312439d 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -30,6 +30,12 @@ config RTC_DS1307 Support for Dallas Semiconductor (now Maxim) DS1307 and DS1338/9 and compatible Real Time Clock devices. +config RTC_RX8010SJ + bool "Enable RX8010SJ driver" + depends on DM_RTC + help + Support for Epson RX8010SJ Real Time Clock devices. + config RTC_S35392A bool "Enable S35392A driver" select BITREVERSE diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 7a8f97a05fe1..9723fb774c57 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -48,6 +48,7 @@ obj-$(CONFIG_RTC_PT7C4338) += pt7c4338.o obj-$(CONFIG_RTC_RS5C372A) += rs5c372.o obj-$(CONFIG_RTC_RV3029) += rv3029.o obj-$(CONFIG_RTC_RX8025) += rx8025.o +obj-$(CONFIG_RTC_RX8010SJ) += rx8010sj.o obj-$(CONFIG_RTC_S3C24X0) += s3c24x0_rtc.o obj-$(CONFIG_RTC_S35392A) += s35392a.o obj-$(CONFIG_SANDBOX) += sandbox_rtc.o diff --git a/drivers/rtc/rx8010sj.c b/drivers/rtc/rx8010sj.c new file mode 100644 index 000000000000..81560e16cef1 --- /dev/null +++ b/drivers/rtc/rx8010sj.c @@ -0,0 +1,378 @@ +/* + * Epson RX8010 RTC driver. + * + * Copyright (c) 2017, General Electric Company + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +/*---------------------------------------------------------------------*/ +/* #undef DEBUG_RTC */ + +#ifdef DEBUG_RTC +#define DEBUGR(fmt, args...) printf(fmt, ##args) +#else +#define DEBUGR(fmt, args...) +#endif +/*---------------------------------------------------------------------*/ + +#ifndef CONFIG_SYS_I2C_RTC_ADDR +# define CONFIG_SYS_I2C_RTC_ADDR 0x32 +#endif + +/* + * RTC register addresses + */ +#define RX8010_SEC 0x10 +#define RX8010_MIN 0x11 +#define RX8010_HOUR 0x12 +#define RX8010_WDAY 0x13 +#define RX8010_MDAY 0x14 +#define RX8010_MONTH 0x15 +#define RX8010_YEAR 0x16 +#define RX8010_YEAR 0x16 +#define RX8010_RESV17 0x17 +#define RX8010_ALMIN 0x18 +#define RX8010_ALHOUR 0x19 +#define RX8010_ALWDAY 0x1A +#define RX8010_TCOUNT0 0x1B +#define RX8010_TCOUNT1 0x1C +#define RX8010_EXT 0x1D +#define RX8010_FLAG 0x1E +#define RX8010_CTRL 0x1F +/* 0x20 to 0x2F are user registers */ +#define RX8010_RESV30 0x30 +#define RX8010_RESV31 0x32 +#define RX8010_IRQ 0x32 + +#define RX8010_EXT_WADA BIT(3) + +#define RX8010_FLAG_VLF BIT(1) +#define RX8010_FLAG_AF BIT(3) +#define RX8010_FLAG_TF BIT(4) +#define RX8010_FLAG_UF BIT(5) + +#define RX8010_CTRL_AIE BIT(3) +#define RX8010_CTRL_UIE BIT(5) +#define RX8010_CTRL_STOP BIT(6) +#define RX8010_CTRL_TEST BIT(7) + +#define RX8010_ALARM_AE BIT(7) + +#ifdef CONFIG_DM_RTC + +#define DEV_TYPE struct udevice + +#else + +/* Local udevice */ +struct ludevice { + u8 chip; +}; + +#define DEV_TYPE struct ludevice + +#endif + +static int rx8010sj_rtc_read8(DEV_TYPE *dev, unsigned int reg) +{ + u8 val; + int ret; + +#ifdef CONFIG_DM_RTC + ret = dm_i2c_read(dev, reg, &val, sizeof(val)); +#else + ret = i2c_read(dev->chip, reg, 1, &val, 1); +#endif + + return ret < 0 ? ret : val; +} + +static int rx8010sj_rtc_write8(DEV_TYPE *dev, unsigned int reg, int val) +{ + int ret; + u8 lval = val; + +#ifdef CONFIG_DM_RTC + ret = dm_i2c_write(dev, reg, &lval, 1); +#else + ret = i2c_write(dev->chip, reg, 1, &lval, 1); +#endif + + return ret < 0 ? ret : 0; +} + +static int validate_time(const struct rtc_time *tm) +{ + if ((tm->tm_year < 2000) || (tm->tm_year > 2099)) + return -EINVAL; + + if ((tm->tm_mon < 1) || (tm->tm_mon > 12)) + return -EINVAL; + + if ((tm->tm_mday < 1) || (tm->tm_mday > 31)) + return -EINVAL; + + if ((tm->tm_wday < 0) || (tm->tm_wday > 6)) + return -EINVAL; + + if ((tm->tm_hour < 0) || (tm->tm_hour > 23)) + return -EINVAL; + + if ((tm->tm_min < 0) || (tm->tm_min > 59)) + return -EINVAL; + + if ((tm->tm_sec < 0) || (tm->tm_sec > 59)) + return -EINVAL; + + return 0; +} + +void rx8010sj_rtc_init(DEV_TYPE *dev) +{ + u8 ctrl[2]; + int need_clear = 0, ret = 0; + + /* Initialize reserved registers as specified in datasheet */ + ret = rx8010sj_rtc_write8(dev, RX8010_RESV17, 0xD8); + if (ret < 0) + goto error; + + ret = rx8010sj_rtc_write8(dev, RX8010_RESV30, 0x00); + if (ret < 0) + goto error; + + ret = rx8010sj_rtc_write8(dev, RX8010_RESV31, 0x08); + if (ret < 0) + goto error; + + ret = rx8010sj_rtc_write8(dev, RX8010_IRQ, 0x00); + if (ret < 0) + goto error; + + for (int i = 0; i < 2; i++) { + ret = rx8010sj_rtc_read8(dev, RX8010_FLAG + i); + if (ret < 0) + goto error; + + ctrl[i] = ret; + } + + if (ctrl[0] & RX8010_FLAG_VLF) + printf("RTC low voltage detected\n"); + + if (ctrl[0] & RX8010_FLAG_AF) { + printf("Alarm was detected\n"); + need_clear = 1; + } + + if (ctrl[0] & RX8010_FLAG_TF) + need_clear = 1; + + if (ctrl[0] & RX8010_FLAG_UF) + need_clear = 1; + + if (need_clear) { + ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF); + ret = rx8010sj_rtc_write8(dev, RX8010_FLAG, ctrl[0]); + if (ret < 0) + goto error; + } + + return; + +error: + printf("Error rtc init.\n"); +} + +/* Get the current time from the RTC */ +static int rx8010sj_rtc_get(DEV_TYPE *dev, struct rtc_time *tmp) +{ + u8 date[7]; + int flagreg; + int ret; + + flagreg = rx8010sj_rtc_read8(dev, RX8010_FLAG); + if (flagreg < 0) { + DEBUGR("Error reading from RTC. err: %d\n", flagreg); + return -EIO; + } + + if (flagreg & RX8010_FLAG_VLF) { + DEBUGR("RTC low voltage detected\n"); + return -EINVAL; + } + + for (int i = 0; i < 7; i++) { + ret = rx8010sj_rtc_read8(dev, RX8010_SEC + i); + if (ret < 0) { + DEBUGR("Error reading from RTC. err: %d\n", ret); + return -EIO; + } + date[i] = ret; + } + + tmp->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f); + tmp->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f); + tmp->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f); + tmp->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f); + tmp->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f); + tmp->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 2000; + tmp->tm_wday = 0; + tmp->tm_yday = 0; + tmp->tm_isdst = 0; + + DEBUGR("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", + tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, + tmp->tm_hour, tmp->tm_min, tmp->tm_sec); + + return 0; +} + +/* Set the RTC */ +static int rx8010sj_rtc_set(DEV_TYPE *dev, const struct rtc_time *tm) +{ + u8 date[7]; + int ctrl, flagreg; + int ret; + + ret = validate_time(tm); + if (ret < 0) + return -EINVAL; + + /* set STOP bit before changing clock/calendar */ + ctrl = rx8010sj_rtc_read8(dev, RX8010_CTRL); + if (ctrl < 0) + return ctrl; + ret = rx8010sj_rtc_write8(dev, RX8010_CTRL, ctrl | RX8010_CTRL_STOP); + if (ret < 0) + return ret; + + date[RX8010_SEC - RX8010_SEC] = bin2bcd(tm->tm_sec); + date[RX8010_MIN - RX8010_SEC] = bin2bcd(tm->tm_min); + date[RX8010_HOUR - RX8010_SEC] = bin2bcd(tm->tm_hour); + date[RX8010_MDAY - RX8010_SEC] = bin2bcd(tm->tm_mday); + date[RX8010_MONTH - RX8010_SEC] = bin2bcd(tm->tm_mon); + date[RX8010_YEAR - RX8010_SEC] = bin2bcd(tm->tm_year - 2000); + date[RX8010_WDAY - RX8010_SEC] = bin2bcd(tm->tm_wday); + + for (int i = 0; i < 7; i++) { + ret = rx8010sj_rtc_write8(dev, RX8010_SEC + i, date[i]); + if (ret < 0) { + DEBUGR("Error writing to RTC. err: %d\n", ret); + return -EIO; + } + } + + /* clear STOP bit after changing clock/calendar */ + ctrl = rx8010sj_rtc_read8(dev, RX8010_CTRL); + if (ctrl < 0) + return ctrl; + + ret = rx8010sj_rtc_write8(dev, RX8010_CTRL, ctrl & ~RX8010_CTRL_STOP); + if (ret < 0) + return ret; + + flagreg = rx8010sj_rtc_read8(dev, RX8010_FLAG); + if (flagreg < 0) + return flagreg; + + if (flagreg & RX8010_FLAG_VLF) + ret = rx8010sj_rtc_write8(dev, RX8010_FLAG, + flagreg & ~RX8010_FLAG_VLF); + + return 0; +} + +/* Reset the RTC. */ +static int rx8010sj_rtc_reset(DEV_TYPE *dev) +{ + /* Not needed */ + return 0; +} + +#ifndef CONFIG_DM_RTC + +int rtc_get(struct rtc_time *tm) +{ + struct ludevice dev = { + .chip = CONFIG_SYS_I2C_RTC_ADDR, + }; + + return rx8010sj_rtc_get(&dev, tm); +} + +int rtc_set(struct rtc_time *tm) +{ + struct ludevice dev = { + .chip = CONFIG_SYS_I2C_RTC_ADDR, + }; + + return rx8010sj_rtc_set(&dev, tm); +} + +void rtc_reset(void) +{ + struct ludevice dev = { + .chip = CONFIG_SYS_I2C_RTC_ADDR, + }; + + rx8010sj_rtc_reset(&dev); +} + +void rtc_init(void) +{ + struct ludevice dev = { + .chip = CONFIG_SYS_I2C_RTC_ADDR, + }; + + rx8010sj_rtc_init(&dev); +} + +#else + +static int rx8010sj_probe(struct udevice *dev) +{ + rx8010sj_rtc_init(&dev); + + return 0; +} + +static const struct rtc_ops rx8010sj_rtc_ops = { + .get = rx8010sj_rtc_get, + .set = rx8010sj_rtc_set, + .read8 = rx8010sj_rtc_read8, + .write8 = rx8010sj_rtc_write8, + .reset = rx8010sj_rtc_reset, +}; + +static const struct udevice_id rx8010sj_rtc_ids[] = { + { .compatible = "epson,rx8010sj-rtc" }, + { } +}; + +U_BOOT_DRIVER(rx8010sj_rtc) = { + .name = "rx8010sj_rtc", + .id = UCLASS_RTC, + .probe = rx8010sj_probe, + .of_match = rx8010sj_rtc_ids, + .ops = &rx8010sj_rtc_ops, +}; + +#endif