From patchwork Sat Dec 24 16:47:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: venkat.prashanth2498@gmail.com X-Patchwork-Id: 708674 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-qk0-x23a.google.com (mail-qk0-x23a.google.com [IPv6:2607:f8b0:400d:c09::23a]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3tmB540bngz9sf9 for ; Sun, 25 Dec 2016 03:47:35 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.b="JePE5K3Y"; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="uEIHs6x/"; dkim-atps=neutral Received: by mail-qk0-x23a.google.com with SMTP id t184sf31608787qkd.0 for ; Sat, 24 Dec 2016 08:47:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding:x-original-sender :x-original-authentication-results:reply-to:precedence:mailing-list :list-id:x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=UwQCwwuUCB5W4QJJxKGX37VDpL8dy6XfjZf2CyCTcbY=; b=JePE5K3YO1OGyXMtgbU/1VUR7qL4HLncbzyGrQpL4u4JrGmRjVF9I8L1gIz5NpXVbX UO+k3csOYs32Gj8UoMi7//Y76x5rbFsVEuaMwyytROAbcgg1hGQL1cd2polrfyslPGOB 0t0EptpIyq7Sqr2Z8nG12jcR8W67lSfrgXMiD9hw6Fo0edTu8PeHM4Ubf27u1opBaiS4 QJrvwskw7YsuP3wzJgsq2618NZzFrO9+JUY+L6aNoMlLQRRIAp63bAQMQhau66V1Ek93 gOSZhrJZzs4TgKX6JYfgJyr7ryZO8nVRcCSDxP+SKOGutwvaMFaLavN6Z31+Ov2i+Uwt +9IA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding:x-original-sender :x-original-authentication-results:reply-to:precedence:mailing-list :list-id:x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=UwQCwwuUCB5W4QJJxKGX37VDpL8dy6XfjZf2CyCTcbY=; b=uEIHs6x/HYvmGg1Ip9nybarX5OAMyckkr5Q9R/QxImf3w6MU0C2hAWI3Odw2kBeHB1 7ee62jbzobmmQZC+/h+Ft+CZV2CQvD2XOGjikniQowbOqpDNbmH7hQvrp1Gi5DAz0748 rRHYh8NkW34munnQz+6wa+NV7MOIz6Wg0fLoLyOq88mntWHNM705GbkJ0vLyCsImdz2B T+E+Db9nBDAJC5As1QjHECZyr/vGMDPVYh2YIeb6Xbp6f/9IxWav3TiX7IWWNGh/R7LW ApBfpiC5DJIoYaPm7jCeB2TNOXCn8GSyu2E5gHOGnbu944kvCQttfaMQZ5lKi6ofVbid wQYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding:x-original-sender :x-original-authentication-results:reply-to:precedence:mailing-list :list-id:x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=UwQCwwuUCB5W4QJJxKGX37VDpL8dy6XfjZf2CyCTcbY=; b=qxMXhcr3v2UHzqA5Pre3yOdZYKZy8pEhbRfUubJM0abeLEbl4d5jBOaVTDzijdIKG+ cYm5Be7f8qe+t4Y4Pv/71Gytrx6R1SbSqQT3xiwtq4gLf6AED0Wuz0p0tc8Ih+5/y1Pp wT53vzQOv3xrPu06xRQWTSdqfJV8SpQ+yTTltB+85K2xDwXh2O0IJkRIEPeQyQuLPE/i o/V3CYEXyEN0bh9Kk5snjvq2G5WU2CplQLk5BnWxyHTiboUQtbwqqJPWLfErmk8PSAF3 uySX4Jkg6fhWN2Tz/p69w19WQKBiqw+bsnZKMnH71Y4BJYGbt/RJVZOmouf0ZfFd0Qz4 0hbw== Sender: rtc-linux@googlegroups.com X-Gm-Message-State: AIkVDXIsUtHHuQVc60gI2RR1/hCPwDblegWDAi4UAgGcdOnpl+2pq38b0peIL3yRjrCL4A== X-Received: by 10.36.121.17 with SMTP id z17mr1264904itc.10.1482598053202; Sat, 24 Dec 2016 08:47:33 -0800 (PST) X-BeenThere: rtc-linux@googlegroups.com Received: by 10.107.31.76 with SMTP id f73ls7022782iof.26.gmail; Sat, 24 Dec 2016 08:47:32 -0800 (PST) X-Received: by 10.99.47.130 with SMTP id v124mr10635441pgv.37.1482598052736; Sat, 24 Dec 2016 08:47:32 -0800 (PST) Received: from mail-pg0-x244.google.com (mail-pg0-x244.google.com. [2607:f8b0:400e:c05::244]) by gmr-mx.google.com with ESMTPS id r1si10250688pfg.4.2016.12.24.08.47.32 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 24 Dec 2016 08:47:32 -0800 (PST) Received-SPF: pass (google.com: domain of venkat.prashanth2498@gmail.com designates 2607:f8b0:400e:c05::244 as permitted sender) client-ip=2607:f8b0:400e:c05::244; Received: by mail-pg0-x244.google.com with SMTP id g1so4126292pgn.0 for ; Sat, 24 Dec 2016 08:47:32 -0800 (PST) X-Received: by 10.84.177.67 with SMTP id w61mr40608110plb.139.1482598052439; Sat, 24 Dec 2016 08:47:32 -0800 (PST) Received: from ubuntu.localdomain ([117.195.252.146]) by smtp.gmail.com with ESMTPSA id e84sm25521019pfl.79.2016.12.24.08.47.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 24 Dec 2016 08:47:31 -0800 (PST) From: Venkat Prashanth B U To: alexandre.belloni@free-electrons.com Cc: a.zummo@towertech.it, rtc-linux@googlegroups.com, linux-kernel@vger.kernel.org, Venkat Prashanth B U Subject: [rtc-linux] [PATCH] rtc: add support for maxim dallas ds1682 Date: Sat, 24 Dec 2016 08:47:14 -0800 Message-Id: <1482598034-3046-1-git-send-email-venkat.prashanth2498@gmail.com> X-Mailer: git-send-email 1.9.2 MIME-Version: 1.0 X-Original-Sender: venkat.prashanth2498@gmail.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@gmail.com; spf=pass (google.com: domain of venkat.prashanth2498@gmail.com designates 2607:f8b0:400e:c05::244 as permitted sender) smtp.mailfrom=venkat.prashanth2498@gmail.com; dmarc=pass (p=NONE dis=NONE) header.from=gmail.com Reply-To: rtc-linux@googlegroups.com Precedence: list Mailing-list: list rtc-linux@googlegroups.com; contact rtc-linux+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: rtc-linux@googlegroups.com X-Google-Group-Id: 712029733259 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , This is a patch to add support for maxim dallas ds1682 total elapsed time recorder Signed‐off‐by: Venkat Prashanth B U --- --- drivers/rtc/Kconfig | 9 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-ds1682.c | 288 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 298 insertions(+) diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index e859d14..bcab91f 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -275,6 +275,15 @@ config RTC_DRV_DS1672 This driver can also be built as a module. If so, the module will be called rtc-ds1672. +config RTC_DRV_DS1682 + tristate "Dallas/Maxim DS1682" + help + If you say yes here you get support for the + Dallas/Maxim DS1682 total elapsed time recorder. + + This driver can also be built as a module. If so, the module + will be called rtc-ds1682. + config RTC_DRV_HYM8563 tristate "Haoyu Microelectronics HYM8563" depends on OF diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 1ac694a..70bb28c 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -58,6 +58,7 @@ obj-$(CONFIG_RTC_DRV_DS1390) += rtc-ds1390.o obj-$(CONFIG_RTC_DRV_DS1511) += rtc-ds1511.o obj-$(CONFIG_RTC_DRV_DS1553) += rtc-ds1553.o obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o +obj-$(CONFIG_RTC_DRV_DS1682) += rtc-ds1682.o obj-$(CONFIG_RTC_DRV_DS1685_FAMILY) += rtc-ds1685.o obj-$(CONFIG_RTC_DRV_DS1742) += rtc-ds1742.o obj-$(CONFIG_RTC_DRV_DS2404) += rtc-ds2404.o diff --git a/drivers/rtc/rtc-ds1682.c b/drivers/rtc/rtc-ds1682.c index e69de29..fad440e 100644 --- a/drivers/rtc/rtc-ds1682.c +++ b/drivers/rtc/rtc-ds1682.c @@ -0,0 +1,288 @@ +/* Driver for Dallas Semiconductor DS1682 2 wire total elapsed + * time recorder + * + * Author : Venkat Prashanth B U + * + * 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. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define DS1682_READ_MEMORY_CMD 0x1D + +#ifndef __LINUX_DS1682_H +#define __LINUX_DS1682_H + +const struct ds1682_platform_data { + + + unsigned int gpio_rst; + unsigned int gpio_clk; + unsigned int gpio_dq; +}; +#endif + +const struct ds1682; + +const struct ds1682_chip_ops { + int (*map_io)(const struct ds1682 *chip, struct platform_device *pdev, + const struct ds1682_platform_data *pdata); + void (*unmap_io)(const struct ds1682 *chip); +}; + +#define DS1682_RST 0 +#define DS1682_CLK 1 +#define DS1682_DQ 2 + +const struct ds1682_gpio { + const char *name; + unsigned int gpio; +}; + +const struct ds1682 { + const struct ds1682_gpio *gpio; + const struct ds1682_chip_ops *ops; + const struct rtc_device *rtc; +}; + +const struct ds1682_gpio ds1682_gpio[] = { + { "RTC RST", 0 }, + { "RTC CLK", 0 }, + { "RTC DQ", 0 }, +}; + +int ds1682_gpio_map(const struct ds1682 *chip, struct platform_device *pdev, + const struct ds1682_platform_data *pdata) +{ + int i, err; + + ds1682_gpio[DS1682_RST].gpio = pdata->gpio_rst; + ds1682_gpio[DS1682_CLK].gpio = pdata->gpio_clk; + ds1682_gpio[DS1682_DQ].gpio = pdata->gpio_dq; + + for (i = 0; i < ARRAY_SIZE(ds1682_gpio); i++) { + err = gpio_request(ds1682_gpio[i].gpio, ds1682_gpio[i].name); + if (err) { + dev_err(&pdev->dev, "error mapping gpio %s: %d\n", + ds1682_gpio[i].name, err); + goto err_request; + } + if (i != DS1682_DQ) + gpio_direction_output(ds1682_gpio[i].gpio, 1); + } + + chip->gpio = ds1682_gpio; + return 0; + +err_request: + while (--i >= 0) + gpio_free(ds1682_gpio[i].gpio); + return err; +} + +static void ds1682_gpio_unmap(const struct ds1682 *chip) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(ds1682_gpio); i++) + gpio_free(ds1682_gpio[i].gpio); +} + +static const struct ds1682_chip_ops ds1682_gpio_ops = { + .map_io = ds1682_gpio_map, + .unmap_io = ds1682_gpio_unmap, +}; + +static void ds1682_reset(const struct device *dev) +{ + gpio_set_value(ds1682_gpio[DS1682_RST].gpio, 0); + udelay(1000); + gpio_set_value(ds1682_gpio[DS1682_RST].gpio, 1); + gpio_set_value(ds1682_gpio[DS1682_CLK].gpio, 0); + gpio_direction_output(ds1682_gpio[DS1682_DQ].gpio, 0); + udelay(10); +} + +static void ds1682_write_byte(const struct device *dev, u8 byte) +{ + int i; + + gpio_direction_output(ds1682_gpio[DS1682_DQ].gpio, 1); + for (i = 0; i < 8; i++) { + gpio_set_value(ds1682_gpio[DS1682_DQ].gpio, byte & (1 << i)); + udelay(10); + gpio_set_value(ds1682_gpio[DS1682_CLK].gpio, 1); + udelay(10); + gpio_set_value(ds1682_gpio[DS1682_CLK].gpio, 0); + udelay(10); + } +} + +static u8 ds1682_read_byte(const struct device *dev) +{ + int i; + u8 ret = 0; + + gpio_direction_input(ds1682_gpio[DS1682_DQ].gpio); + + for (i = 0; i < 8; i++) { + gpio_set_value(ds1682_gpio[DS1682_CLK].gpio, 0); + udelay(10); + if (gpio_get_value(ds1682_gpio[DS1682_DQ].gpio)) + ret |= 1 << i; + gpio_set_value(ds1682_gpio[DS1682_CLK].gpio, 1); + udelay(10); + } + return ret; +} + +static void ds1682_read_memory(const struct device *dev, u16 offset, + int length, u8 *out) +{ + ds1682_reset(dev); + ds1682_write_byte(dev, DS1682_READ_MEMORY_CMD); + ds1682_write_byte(dev, offset & 0xff); + ds1682_write_byte(dev, (offset >> 8) & 0xff); + while (length--) + *out++ = ds1682_read_byte(dev); +} + +static void ds1682_write_memory(const struct device *dev, u16 offset, + int length, u8 *out) +{ + int i; + u8 ta01, ta02, es; + + ds1682_reset(dev); + ds1682_write_byte(dev, offset & 0xff); + ds1682_write_byte(dev, (offset >> 8) & 0xff); + + for (i = 0; i < length; i++) + ds1682_write_byte(dev, out[i]); + + ds1682_reset(dev); + + ta01 = ds1682_read_byte(dev); + ta02 = ds1682_read_byte(dev); + es = ds1682_read_byte(dev); + + for (i = 0; i < length; i++) { + if (out[i] != ds1682_read_byte(dev)) { + dev_err(dev, "read invalid data\n"); + return; + } + } + + ds1682_reset(dev); + ds1682_write_byte(dev, ta01); + ds1682_write_byte(dev, ta02); + ds1682_write_byte(dev, es); + gpio_direction_input(ds1682_gpio[DS1682_DQ].gpio); + while (gpio_get_value(ds1682_gpio[DS1682_DQ].gpio)) + ; +} + +static void ds1682_enable_osc(const struct device *dev) +{ + u8 in[1] = { 0x10 }; /* enable oscillator */ + + ds1682_write_memory(dev, 0x201, 1, in); +} + +static int ds1682_read_time(const struct device *dev, struct rtc_time *dt) +{ + unsigned long time = 0; + + ds1682_read_memory(dev, 0x203, 4, (u8 *)&time); + time = le32_to_cpu(time); + rtc_time_to_tm(time, dt); + return rtc_valid_tm(dt); +} + +static int ds1682_set_mmss(const struct device *dev, unsigned long secs) +{ + u32 time = cpu_to_le32(secs); + + ds1682_write_memory(dev, 0x203, 4, (u8 *)&time); + return 0; +} + +static const struct rtc_class_ops ds1682_rtc_ops = { + .read_time = ds1682_read_time, + .set_mmss = ds1682_set_mmss, +}; + +static int rtc_probe(const struct platform_device *pdev) +{ +const struct ds1682_platform_data *pdata = dev_get_platdata(&pdev->dev); + const struct ds1682 *chip; + int retval = -EBUSY; + +chip = devm_kzalloc(&pdev->dev, sizeof(const struct ds1682), GFP_KERNEL); + if (!chip) + return -ENOMEM; + + chip->ops = &ds1682_gpio_ops; + + retval = chip->ops->map_io(chip, pdev, pdata); + if (retval) + goto err_chip; + + dev_info(&pdev->dev, "using GPIOs RST:%d, CLK:%d, DQ:%d\n", + chip->gpio[DS1682_RST].gpio, chip->gpio[DS1682_CLK].gpio, + chip->gpio[DS1682_DQ].gpio); + + platform_set_drvdata(pdev, chip); + + chip->rtc = devm_rtc_device_register(&pdev->dev, "ds1682", + &ds1682_rtc_ops, THIS_MODULE); + if (IS_ERR(chip->rtc)) { + retval = PTR_ERR(chip->rtc); + goto err_io; + } + + ds1682_enable_osc(&pdev->dev); + return 0; + +err_io: + chip->ops->unmap_io(chip); +err_chip: + return retval; +} + +static int rtc_remove(const struct platform_device *dev) +{ +const struct ds1682 *chip = platform_get_drvdata(dev); + + chip->ops->unmap_io(chip); + + return 0; +} + +const struct platform_driver rtc_device_driver = { + .probe = rtc_probe, + .remove = rtc_remove, + .driver = { + .name = "ds1682", + }, +}; +module_platform_driver(rtc_device_driver); + +MODULE_DESCRIPTION("DS1682 Total Elapsed Time Recorder"); +MODULE_AUTHOR("Venkat Prashanth B U "); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:ds1682"); +