Received: by 10.223.164.202 with SMTP id h10csp1223281wrb; Sun, 26 Nov 2017 22:49:45 -0800 (PST) X-Google-Smtp-Source: AGs4zMaiT18pLUaMGbPO4xvDqDi0XcuBoabmLpwJuNbp923DSbmtbQOyUmMwDpPF140E2bPzslaN X-Received: by 10.84.160.197 with SMTP id v5mr14836466plg.206.1511765385735; Sun, 26 Nov 2017 22:49:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511765385; cv=none; d=google.com; s=arc-20160816; b=xmE2fAP7SoF4yVwmpm9RShlU7RKxTSXCowauwuwijnqWg5Nr0r3+EeuDWYlciss57A RQqepCTu+MiIx+rwcO3MmMIv/lzOOtnvhkcbm/HXqeTuyeVH4R2peGhtqtWF48BjZcki Q8G5AHsLG7AZhiE/QPY/xgIbSaUouAixgtqY50RKcX0FvoTn4zim15dT54MYUijnSIUC u01+3XCbZLuXBu45ScK7xujDAIQXJbjvbexzetmtLeIxFhezlIsO29zKUOdMsZER+lk7 9SH3G9GhTvnk9QZNOTzizOtMIoBN9Z/VQD3ACnhl6jU5nmqwqhZHNU6ZabdRVqR28lVy EkCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=3n0zQcJhQnkDvqiO3D93DRxyoFS50dI1Uf1qsgBmoos=; b=LYfdDYhMYvEsQB3iuBRJ7Ar68tHvQegFG4ooAHEmNWaaY2pSO5V7wYJTdZMNovX9z0 DLrZTQ5QKlfvxrhSO1fLD2eZ0Zs1xysK00mjWpE/vxKqCgYd6/wmGqz9RAUc9Dw1lhxs giaCOVzV6Cz9E8HbosE51CZic6IjFX8qrIkkuBmiJZgR4uWuscwStmuFq7teW4N/8Q/+ EDuUpyh56wk2FqrwRhw05NnAIDP1HTy3qXS7qRa6keKd+a+1O+zq/Mx0Dz7Q1lJw+WPT PZREGYchzcbBpqWrVdNrAEYNpREWysQMYp3fU4YATGqpNmXtDLZteQnnNsawKLjiMQkB n/pQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oVdOyfT1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n8si15830304pgf.450.2017.11.26.22.49.33; Sun, 26 Nov 2017 22:49:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oVdOyfT1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751327AbdK0Gsu (ORCPT + 78 others); Mon, 27 Nov 2017 01:48:50 -0500 Received: from mail-pf0-f195.google.com ([209.85.192.195]:34609 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750838AbdK0Gst (ORCPT ); Mon, 27 Nov 2017 01:48:49 -0500 Received: by mail-pf0-f195.google.com with SMTP id k24so7210396pfb.1 for ; Sun, 26 Nov 2017 22:48:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=3n0zQcJhQnkDvqiO3D93DRxyoFS50dI1Uf1qsgBmoos=; b=oVdOyfT1r6HJFbUTMEjOkcdXvxVrOm8bhQ/V0sOa43/F5i3XnAJ6YPIVd1FrJwcb1C ebB4eTjkKRJutcUzxGcCO9mpQXAGlSbl0AkFDX3cv6nKffPgd5k1B0dzlkSx+sNEmvOa S4v09qxrDIV6mVhHmot5j2ggwqy5FsVUgqxiryv9xu8yJ/M6+iyWHQM7IyAiYFR/zu32 Qt+NlxWmDDZR702Ed0+xNmhiFPSacCTtzQYtMZQEUHwZFec2DBdA6e+guQbAmMc06xEa jxNGg8MGtHdOR53hkk33tSyzoWjNjnjCRAqshLHjBOGK4H7yfFcCf9rIKp2UBI1X4Lwu tzmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=3n0zQcJhQnkDvqiO3D93DRxyoFS50dI1Uf1qsgBmoos=; b=ubpuUK7pBmG/uBrSu7MIbEMgb22ag9bXw1RzeYbQ1LIWnQL8aXuI9msj0iNL7+c9D6 1VwOSBBLOtLLZ2wt1hQMpzGKGKy2X7lzkEeFw028s9PoMd5dvbp5DHAX9V6O4JT6o+5v QHNYCWFtivDnQFMGIV5h3Pc8qW5AldLKOAhLMQBDo0on+Fx45jwM0yKxMZHumFSLcWto /inkGCC8T/bymNEnNeUooR23sCNzFxOxFUkHT6vHWdpsSXVZ0PhnAavDk+RXOMhkLg0Y 2HGG8e6J903fn7qOI2zeJVVrrmeW1GIGkmQEVDblGsKTMQhNmrB2f76/8yb2aHsJUr2g 1orw== X-Gm-Message-State: AJaThX6NSbFID3vDcaHlwBHx4N0anl7l/k9lzq0X4dL9b1WIF1Z+Q0yk /8S8PKw3101wGwtmvgZtuZBjfA== X-Received: by 10.99.123.90 with SMTP id k26mr35937806pgn.33.1511765328861; Sun, 26 Nov 2017 22:48:48 -0800 (PST) Received: from km1.krizna.com ([183.82.119.88]) by smtp.gmail.com with ESMTPSA id 140sm41396759pgd.85.2017.11.26.22.48.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 26 Nov 2017 22:48:48 -0800 (PST) From: venkat.prashanth2498@gmail.com To: alexandre.belloni@free-electrons.com Cc: rtc-linux@googlegroups.com, linux-kernel@vger.kernel.org, a.zummo@towertech.it, Venkat Prashanth B U Subject: [PATCH] rtc: add support for maxim dallas ds1682 Date: Mon, 27 Nov 2017 12:18:44 +0530 Message-Id: <1511765324-3645-1-git-send-email-venkat.prashanth2498@gmail.com> X-Mailer: git-send-email 1.9.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Venkat Prashanth B U Add supporting driver for Dallas Semiconductor DS1682 2 wire total elapsed time recorder. Signed-off-by: Venkat Prashanth B U --- drivers/rtc/rtc-ds1682.c | 284 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 284 insertions(+) diff --git a/drivers/rtc/rtc-ds1682.c b/drivers/rtc/rtc-ds1682.c index e69de29..a21c94d 100644 --- a/drivers/rtc/rtc-ds1682.c +++ b/drivers/rtc/rtc-ds1682.c @@ -0,0 +1,284 @@ +/* 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"); -- 1.9.1 From 1584611247352510176@xxx Mon Nov 20 18:37:28 +0000 2017 X-GM-THRID: 1584611247352510176 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread