* [PATCH] rtc: add support for maxim dallas rtc ds1347
@ 2014-03-21 14:35 RAGHAVENDRA GANIGA
2014-03-21 16:14 ` [rtc-linux] " Alessandro Zummo
0 siblings, 1 reply; 8+ messages in thread
From: RAGHAVENDRA GANIGA @ 2014-03-21 14:35 UTC (permalink / raw)
To: a.zummo, linux-kernel, rtc-linux
>From 0fd7f9ab5c0cb5e857b7b4d6893a8e68ac7763fd Mon Sep 17 00:00:00 2001
From: Raghavendra Ganiga <ravi23ganiga@gmail.com>
Date: Fri, 21 Mar 2014 19:53:36 +0530
Subject: [PATCH] rtc: add support for maxim dallas rtc ds1347
This is a patch to add support for
maxim dallas rtc ds1347
Signed-off-by: Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
---
drivers/rtc/Kconfig | 12 ++++
drivers/rtc/Makefile | 1 +
drivers/rtc/rtc-ds1347.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 180 insertions(+)
create mode 100644 drivers/rtc/rtc-ds1347.c
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index db933de..2e565f8 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -573,6 +573,18 @@ config RTC_DRV_DS1305
This driver can also be built as a module. If so, the module
will be called rtc-ds1305.
+config RTC_DRV_DS1347
+ tristate "Dallas/Maxim DS1347"
+ help
+ If you say yes here you get support for the
+ Dallas/Maxim DS1347 chips.
+
+ This driver only supports the RTC feature, and not other chip
+ features such as alarms.
+
+ This driver can also be built as a module. If so, the module
+ will be called rtc-ds1347.
+
config RTC_DRV_DS1390
tristate "Dallas/Maxim DS1390/93/94"
help
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index b427bf7..40a0991 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_RTC_DRV_DS1286) += rtc-ds1286.o
obj-$(CONFIG_RTC_DRV_DS1302) += rtc-ds1302.o
obj-$(CONFIG_RTC_DRV_DS1305) += rtc-ds1305.o
obj-$(CONFIG_RTC_DRV_DS1307) += rtc-ds1307.o
+obj-$(CONFIG_RTC_DRV_DS1347) += rtc-ds1347.o
obj-$(CONFIG_RTC_DRV_DS1374) += rtc-ds1374.o
obj-$(CONFIG_RTC_DRV_DS1390) += rtc-ds1390.o
obj-$(CONFIG_RTC_DRV_DS1511) += rtc-ds1511.o
diff --git a/drivers/rtc/rtc-ds1347.c b/drivers/rtc/rtc-ds1347.c
new file mode 100644
index 0000000..fffa51f
--- /dev/null
+++ b/drivers/rtc/rtc-ds1347.c
@@ -0,0 +1,167 @@
+/* rtc-ds1347.c
+ *
+ * Driver for Dallas Semiconductor DS1347 Low Current, SPI Compatible
+ * Real Time Clock
+ *
+ * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
+ *
+ * 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 <linux/init.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/platform_device.h>
+#include <linux/rtc.h>
+#include <linux/spi/spi.h>
+#include <linux/bcd.h>
+
+/* Registers in ds1347 rtc */
+
+#define DS1347_SECONDS_REG 0x01
+#define DS1347_MINUTES_REG 0x03
+#define DS1347_HOURS_REG 0x05
+#define DS1347_DATE_REG 0x07
+#define DS1347_MONTH_REG 0x09
+#define DS1347_DAY_REG 0x0B
+#define DS1347_YEAR_REG 0x0D
+#define DS1347_CONTROL_REG 0x0F
+#define DS1347_STATUS_REG 0x17
+#define DS1347_CLOCK_BURST 0x3F
+
+static int ds1347_read_reg(struct device *dev, unsigned char address,
+ unsigned char *data)
+{
+ struct spi_device *spi = to_spi_device(dev);
+
+ *data = address | 0x80;
+
+ return spi_write_then_read(spi, data, 1, data, 1);
+}
+
+static int ds1347_write_reg(struct device *dev, unsigned char address,
+ unsigned char data)
+{
+ struct spi_device *spi = to_spi_device(dev);
+ unsigned char buf[2];
+
+ buf[0] = address & 0x7F;
+ buf[1] = data;
+
+ return spi_write_then_read(spi, buf, 2, NULL, 0);
+}
+
+static int ds1347_read_time(struct device *dev, struct rtc_time *dt)
+{
+ struct spi_device *spi = to_spi_device(dev);
+ int err;
+ unsigned char buf[8];
+
+ buf[0] = DS1347_CLOCK_BURST | 0x80;
+
+ err = spi_write_then_read(spi, buf, 1, buf, 8);
+ if (err)
+ return err;
+
+ dt->tm_sec = bcd2bin(buf[0]);
+ dt->tm_min = bcd2bin(buf[1]);
+ dt->tm_hour = bcd2bin(buf[2] & 0x3F);
+ dt->tm_mday = bcd2bin(buf[3]);
+ dt->tm_mon = bcd2bin(buf[4]) - 1;
+ dt->tm_wday = bcd2bin(buf[5]) - 1;
+ dt->tm_year = bcd2bin(buf[6]) + 100;
+
+ return rtc_valid_tm(dt);
+}
+
+static int ds1347_set_time(struct device *dev, struct rtc_time *dt)
+{
+ struct spi_device *spi = to_spi_device(dev);
+ unsigned char buf[9];
+
+ buf[0] = DS1347_CLOCK_BURST & 0x7F;
+ buf[1] = bin2bcd(dt->tm_sec);
+ buf[2] = bin2bcd(dt->tm_min);
+ buf[3] = (bin2bcd(dt->tm_hour) & 0x3F);
+ buf[4] = bin2bcd(dt->tm_mday);
+ buf[5] = bin2bcd(dt->tm_mon + 1);
+ buf[6] = bin2bcd(dt->tm_wday + 1);
+
+ /* year in linux is from 1900 i.e in range of 100
+ in rtc it is from 00 to 99 */
+ dt->tm_year = dt->tm_year % 100;
+
+ buf[7] = bin2bcd(dt->tm_year);
+ buf[8] = bin2bcd(0x00);
+
+ /* write the rtc settings */
+ return spi_write_then_read(spi, buf, 9, NULL, 0);
+}
+
+static const struct rtc_class_ops ds1347_rtc_ops = {
+ .read_time = ds1347_read_time,
+ .set_time = ds1347_set_time,
+};
+
+static int ds1347_probe(struct spi_device *spi)
+{
+ struct rtc_device *rtc;
+ unsigned char data;
+ int res;
+
+ /* spi setup with ds1347 in mode 3 and bits per word as 8 */
+ spi->mode = SPI_MODE_3;
+ spi->bits_per_word = 8;
+ spi_setup(spi);
+
+ /* RTC Settings */
+ res = ds1347_read_reg(&spi->dev, DS1347_SECONDS_REG, &data);
+ if (res)
+ return res;
+
+ /* Disable the write protect of rtc */
+ ds1347_read_reg(&spi->dev, DS1347_CONTROL_REG, &data);
+ data = data & ~(1<<7);
+ ds1347_write_reg(&spi->dev, DS1347_CONTROL_REG, data);
+
+ /* Enable the oscillator , disable the oscillator stop flag,
+ and glitch filter to reduce current consumption */
+ ds1347_read_reg(&spi->dev, DS1347_STATUS_REG, &data);
+ data = data & 0x1B;
+ ds1347_write_reg(&spi->dev, DS1347_STATUS_REG, data);
+
+ /* display the settings */
+ ds1347_read_reg(&spi->dev, DS1347_CONTROL_REG, &data);
+ dev_info(&spi->dev, "DS1347 RTC CTRL Reg = 0x%02x\n", data);
+
+ ds1347_read_reg(&spi->dev, DS1347_STATUS_REG, &data);
+ dev_info(&spi->dev, "DS1347 RTC Status Reg = 0x%02x\n", data);
+
+ rtc = devm_rtc_device_register(&spi->dev, "ds1347",
+ &ds1347_rtc_ops, THIS_MODULE);
+
+ if (IS_ERR(rtc))
+ return PTR_ERR(rtc);
+
+ spi_set_drvdata(spi, rtc);
+
+ return 0;
+}
+
+static struct spi_driver ds1347_driver = {
+ .driver = {
+ .name = "ds1347",
+ .owner = THIS_MODULE,
+ },
+ .probe = ds1347_probe,
+};
+
+module_spi_driver(ds1347_driver);
+
+MODULE_DESCRIPTION("DS1347 SPI RTC DRIVER");
+MODULE_AUTHOR("Raghavendra C Ganiga <ravi23ganiga@gmail.com>");
+MODULE_LICENSE("GPL v2");
+
--
1.7.10.4
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [rtc-linux] [PATCH] rtc: add support for maxim dallas rtc ds1347
2014-03-21 14:35 [PATCH] rtc: add support for maxim dallas rtc ds1347 RAGHAVENDRA GANIGA
@ 2014-03-21 16:14 ` Alessandro Zummo
0 siblings, 0 replies; 8+ messages in thread
From: Alessandro Zummo @ 2014-03-21 16:14 UTC (permalink / raw)
To: rtc-linux; +Cc: ravi23ganiga, linux-kernel, akpm
On Fri, 21 Mar 2014 20:05:40 +0530
RAGHAVENDRA GANIGA <ravi23ganiga@gmail.com> wrote:
> From 0fd7f9ab5c0cb5e857b7b4d6893a8e68ac7763fd Mon Sep 17 00:00:00 2001
> From: Raghavendra Ganiga <ravi23ganiga@gmail.com>
> Date: Fri, 21 Mar 2014 19:53:36 +0530
> Subject: [PATCH] rtc: add support for maxim dallas rtc ds1347
>
> This is a patch to add support for
> maxim dallas rtc ds1347
>
> Signed-off-by: Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
Acked-by: Alessandro Zummo <a.zummo@towertech.it>
--
Best regards,
Alessandro Zummo,
Tower Technologies - Torino, Italy
http://www.towertech.it
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] rtc: add support for maxim dallas rtc ds1347
2014-03-20 21:01 ` Alessandro Zummo
@ 2014-03-20 21:08 ` Joe Perches
0 siblings, 0 replies; 8+ messages in thread
From: Joe Perches @ 2014-03-20 21:08 UTC (permalink / raw)
To: Alessandro Zummo; +Cc: RAGHAVENDRA GANIGA, linux-kernel, rtc-linux
On Thu, 2014-03-20 at 22:01 +0100, Alessandro Zummo wrote:
> On Thu, 20 Mar 2014 12:19:55 -0700
> Joe Perches <joe@perches.com> wrote:
> > This depends on how long you want to live, but it could be
> > while (dt->tm_year >= 100)
> dt->tm_year % 100 ?
Sure, if you don't mind the divide.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] rtc: add support for maxim dallas rtc ds1347
2014-03-20 19:19 ` Joe Perches
@ 2014-03-20 21:01 ` Alessandro Zummo
2014-03-20 21:08 ` Joe Perches
0 siblings, 1 reply; 8+ messages in thread
From: Alessandro Zummo @ 2014-03-20 21:01 UTC (permalink / raw)
To: Joe Perches; +Cc: RAGHAVENDRA GANIGA, linux-kernel, rtc-linux
On Thu, 20 Mar 2014 12:19:55 -0700
Joe Perches <joe@perches.com> wrote:
> This depends on how long you want to live, but it could be
>
> while (dt->tm_year >= 100)
dt->tm_year % 100 ?
--
Best regards,
Alessandro Zummo,
Tower Technologies - Torino, Italy
http://www.towertech.it
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] rtc: add support for maxim dallas rtc ds1347
2014-03-20 19:00 RAGHAVENDRA GANIGA
@ 2014-03-20 19:19 ` Joe Perches
2014-03-20 21:01 ` Alessandro Zummo
0 siblings, 1 reply; 8+ messages in thread
From: Joe Perches @ 2014-03-20 19:19 UTC (permalink / raw)
To: RAGHAVENDRA GANIGA; +Cc: a.zummo, linux-kernel, rtc-linux
On Fri, 2014-03-21 at 00:30 +0530, RAGHAVENDRA GANIGA wrote:
> This is a patch to add support for
> maxim dallas rtc ds1347
[]
> diff --git a/drivers/rtc/rtc-ds1347.c b/drivers/rtc/rtc-ds1347.c
[]
> +static int ds1347_read_time(struct device *dev, struct rtc_time *dt)
> +{
[]
> + /* year in linux is from 1900 i.e in range of 100
> + in rtc it is from 00 to 99 so subtract
> + the linux year by 100 */
> + if (dt->tm_year >= 100)
> + dt->tm_year -= 100;
This depends on how long you want to live, but it could be
while (dt->tm_year >= 100)
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH] rtc: add support for maxim dallas rtc ds1347
@ 2014-03-20 19:00 RAGHAVENDRA GANIGA
2014-03-20 19:19 ` Joe Perches
0 siblings, 1 reply; 8+ messages in thread
From: RAGHAVENDRA GANIGA @ 2014-03-20 19:00 UTC (permalink / raw)
To: a.zummo, linux-kernel, rtc-linux
>From f18fc68778c210863ca0968e4448375c89e9bb06 Mon Sep 17 00:00:00 2001
From: Raghavendra Ganiga <ravi23ganiga@gmail.com>
Date: Fri, 21 Mar 2014 00:22:32 +0530
Subject: [PATCH] rtc: add support for maxim dallas rtc ds1347
This is a patch to add support for
maxim dallas rtc ds1347
Signed-off-by: Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
---
drivers/rtc/Kconfig | 12 ++++
drivers/rtc/Makefile | 1 +
drivers/rtc/rtc-ds1347.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 182 insertions(+)
create mode 100644 drivers/rtc/rtc-ds1347.c
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index db933de..2e565f8 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -573,6 +573,18 @@ config RTC_DRV_DS1305
This driver can also be built as a module. If so, the module
will be called rtc-ds1305.
+config RTC_DRV_DS1347
+ tristate "Dallas/Maxim DS1347"
+ help
+ If you say yes here you get support for the
+ Dallas/Maxim DS1347 chips.
+
+ This driver only supports the RTC feature, and not other chip
+ features such as alarms.
+
+ This driver can also be built as a module. If so, the module
+ will be called rtc-ds1347.
+
config RTC_DRV_DS1390
tristate "Dallas/Maxim DS1390/93/94"
help
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index b427bf7..40a0991 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_RTC_DRV_DS1286) += rtc-ds1286.o
obj-$(CONFIG_RTC_DRV_DS1302) += rtc-ds1302.o
obj-$(CONFIG_RTC_DRV_DS1305) += rtc-ds1305.o
obj-$(CONFIG_RTC_DRV_DS1307) += rtc-ds1307.o
+obj-$(CONFIG_RTC_DRV_DS1347) += rtc-ds1347.o
obj-$(CONFIG_RTC_DRV_DS1374) += rtc-ds1374.o
obj-$(CONFIG_RTC_DRV_DS1390) += rtc-ds1390.o
obj-$(CONFIG_RTC_DRV_DS1511) += rtc-ds1511.o
diff --git a/drivers/rtc/rtc-ds1347.c b/drivers/rtc/rtc-ds1347.c
new file mode 100644
index 0000000..986275e
--- /dev/null
+++ b/drivers/rtc/rtc-ds1347.c
@@ -0,0 +1,169 @@
+/* rtc-ds1347.c
+ *
+ * Driver for Dallas Semiconductor DS1347 Low Current, SPI Compatible
+ * Real Time Clock
+ *
+ * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
+ *
+ * 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 <linux/init.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/platform_device.h>
+#include <linux/rtc.h>
+#include <linux/spi/spi.h>
+#include <linux/bcd.h>
+
+/* Registers in ds1347 rtc */
+
+#define DS1347_SECONDS_REG 0x01
+#define DS1347_MINUTES_REG 0x03
+#define DS1347_HOURS_REG 0x05
+#define DS1347_DATE_REG 0x07
+#define DS1347_MONTH_REG 0x09
+#define DS1347_DAY_REG 0x0B
+#define DS1347_YEAR_REG 0x0D
+#define DS1347_CONTROL_REG 0x0F
+#define DS1347_STATUS_REG 0x17
+#define DS1347_CLOCK_BURST 0x3F
+
+static int ds1347_read_reg(struct device *dev, unsigned char address,
+ unsigned char *data)
+{
+ struct spi_device *spi = to_spi_device(dev);
+
+ *data = address | 0x80;
+
+ return spi_write_then_read(spi, data, 1, data, 1);
+}
+
+static int ds1347_write_reg(struct device *dev, unsigned char address,
+ unsigned char data)
+{
+ struct spi_device *spi = to_spi_device(dev);
+ unsigned char buf[2];
+
+ buf[0] = address & 0x7F;
+ buf[1] = data;
+
+ return spi_write_then_read(spi, buf, 2, NULL, 0);
+}
+
+static int ds1347_read_time(struct device *dev, struct rtc_time *dt)
+{
+ struct spi_device *spi = to_spi_device(dev);
+ int err;
+ unsigned char buf[8];
+
+ buf[0] = DS1347_CLOCK_BURST | 0x80;
+
+ err = spi_write_then_read(spi, buf, 1, buf, 8);
+ if (err)
+ return err;
+
+ dt->tm_sec = bcd2bin(buf[0]);
+ dt->tm_min = bcd2bin(buf[1]);
+ dt->tm_hour = bcd2bin(buf[2] & 0x3F);
+ dt->tm_mday = bcd2bin(buf[3]);
+ dt->tm_mon = bcd2bin(buf[4]) - 1;
+ dt->tm_wday = bcd2bin(buf[5]) - 1;
+ dt->tm_year = bcd2bin(buf[6]) + 100;
+
+ return rtc_valid_tm(dt);
+}
+
+static int ds1347_set_time(struct device *dev, struct rtc_time *dt)
+{
+ struct spi_device *spi = to_spi_device(dev);
+ unsigned char buf[9];
+
+ buf[0] = DS1347_CLOCK_BURST & 0x7F;
+ buf[1] = bin2bcd(dt->tm_sec);
+ buf[2] = bin2bcd(dt->tm_min);
+ buf[3] = (bin2bcd(dt->tm_hour) & 0x3F);
+ buf[4] = bin2bcd(dt->tm_mday);
+ buf[5] = bin2bcd(dt->tm_mon + 1);
+ buf[6] = bin2bcd(dt->tm_wday + 1);
+
+ /* year in linux is from 1900 i.e in range of 100
+ in rtc it is from 00 to 99 so subtract
+ the linux year by 100 */
+ if (dt->tm_year >= 100)
+ dt->tm_year -= 100;
+
+ buf[7] = bin2bcd(dt->tm_year);
+ buf[8] = bin2bcd(0x00);
+
+ /* write the rtc settings */
+ return spi_write_then_read(spi, buf, 9, NULL, 0);
+}
+
+static const struct rtc_class_ops ds1347_rtc_ops = {
+ .read_time = ds1347_read_time,
+ .set_time = ds1347_set_time,
+};
+
+static int ds1347_probe(struct spi_device *spi)
+{
+ struct rtc_device *rtc;
+ unsigned char data;
+ int res;
+
+ /* spi setup with ds1347 in mode 3 and bits per word as 8 */
+ spi->mode = SPI_MODE_3;
+ spi->bits_per_word = 8;
+ spi_setup(spi);
+
+ /* RTC Settings */
+ res = ds1347_read_reg(&spi->dev, DS1347_SECONDS_REG, &data);
+ if (res)
+ return res;
+
+ /* Disable the write protect of rtc */
+ ds1347_read_reg(&spi->dev, DS1347_CONTROL_REG, &data);
+ data = data & ~(1<<7);
+ ds1347_write_reg(&spi->dev, DS1347_CONTROL_REG, data);
+
+ /* Enable the oscillator , disable the oscillator stop flag,
+ and glitch filter to reduce current consumption */
+ ds1347_read_reg(&spi->dev, DS1347_STATUS_REG, &data);
+ data = data & 0x1B;
+ ds1347_write_reg(&spi->dev, DS1347_STATUS_REG, data);
+
+ /* display the settings */
+ ds1347_read_reg(&spi->dev, DS1347_CONTROL_REG, &data);
+ dev_info(&spi->dev, "DS1347 RTC CTRL Reg = 0x%02x\n", data);
+
+ ds1347_read_reg(&spi->dev, DS1347_STATUS_REG, &data);
+ dev_info(&spi->dev, "DS1347 RTC Status Reg = 0x%02x\n", data);
+
+ rtc = devm_rtc_device_register(&spi->dev, "ds1347",
+ &ds1347_rtc_ops, THIS_MODULE);
+
+ if (IS_ERR(rtc))
+ return PTR_ERR(rtc);
+
+ spi_set_drvdata(spi, rtc);
+
+ return 0;
+}
+
+static struct spi_driver ds1347_driver = {
+ .driver = {
+ .name = "ds1347",
+ .owner = THIS_MODULE,
+ },
+ .probe = ds1347_probe,
+};
+
+module_spi_driver(ds1347_driver);
+
+MODULE_DESCRIPTION("DS1347 SPI RTC DRIVER");
+MODULE_AUTHOR("Raghavendra C Ganiga <ravi23ganiga@gmail.com>");
+MODULE_LICENSE("GPL v2");
+
--
1.7.10.4
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] rtc: add support for maxim dallas rtc ds1347
2014-03-20 16:25 RAGHAVENDRA GANIGA
@ 2014-03-20 18:22 ` Alessandro Zummo
0 siblings, 0 replies; 8+ messages in thread
From: Alessandro Zummo @ 2014-03-20 18:22 UTC (permalink / raw)
To: RAGHAVENDRA GANIGA; +Cc: linux-kernel, rtc-linux
On Thu, 20 Mar 2014 21:55:33 +0530
RAGHAVENDRA GANIGA <ravi23ganiga@gmail.com> wrote:
Thanks for your submission!
just a few comments below:
> ---
> drivers/rtc/Kconfig | 12 ++++
> drivers/rtc/Makefile | 1 +
> drivers/rtc/rtc-ds1347.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 188 insertions(+)
> create mode 100644 drivers/rtc/rtc-ds1347.c
>
> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> index db933de..2e565f8 100644
> --- a/drivers/rtc/Kconfig
> +++ b/drivers/rtc/Kconfig
> @@ -573,6 +573,18 @@ config RTC_DRV_DS1305
> This driver can also be built as a module. If so, the module
> will be called rtc-ds1305.
>
> +config RTC_DRV_DS1347
> + tristate "Dallas/Maxim DS1347"
> + help
> + If you say yes here you get support for the
> + Dallas/Maxim DS1347 chips.
> +
> + This driver only supports the RTC feature, and not other chip
> + features such as alarms.
> +
> + This driver can also be built as a module. If so, the module
> + will be called rtc-ds1347.
> +
> config RTC_DRV_DS1390
> tristate "Dallas/Maxim DS1390/93/94"
> help
> diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> index b427bf7..40a0991 100644
> --- a/drivers/rtc/Makefile
> +++ b/drivers/rtc/Makefile
> @@ -40,6 +40,7 @@ obj-$(CONFIG_RTC_DRV_DS1286) += rtc-ds1286.o
> obj-$(CONFIG_RTC_DRV_DS1302) += rtc-ds1302.o
> obj-$(CONFIG_RTC_DRV_DS1305) += rtc-ds1305.o
> obj-$(CONFIG_RTC_DRV_DS1307) += rtc-ds1307.o
> +obj-$(CONFIG_RTC_DRV_DS1347) += rtc-ds1347.o
> obj-$(CONFIG_RTC_DRV_DS1374) += rtc-ds1374.o
> obj-$(CONFIG_RTC_DRV_DS1390) += rtc-ds1390.o
> obj-$(CONFIG_RTC_DRV_DS1511) += rtc-ds1511.o
> diff --git a/drivers/rtc/rtc-ds1347.c b/drivers/rtc/rtc-ds1347.c
> new file mode 100644
> index 0000000..3c9a934
> --- /dev/null
> +++ b/drivers/rtc/rtc-ds1347.c
> @@ -0,0 +1,175 @@
> +/* rtc-ds1347.c
> + *
> + * Driver for Dallas Semiconductor DS1347 Low Current, SPI Compatible
> + * Real Time Clock
> + *
> + * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
> + *
> + * 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 <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/device.h>
> +#include <linux/platform_device.h>
> +#include <linux/rtc.h>
> +#include <linux/spi/spi.h>
> +#include <linux/bcd.h>
> +
> +/*Registers in ds1347 rtc*/
> +
> +#define DS1347_SECONDS_REG 0x01
> +#define DS1347_MINUTES_REG 0x03
> +#define DS1347_HOURS_REG 0x05
> +#define DS1347_DATE_REG 0x07
> +#define DS1347_MONTH_REG 0x09
> +#define DS1347_DAY_REG 0x0B
> +#define DS1347_YEAR_REG 0x0D
> +#define DS1347_CONTROL_REG 0x0F
> +#define DS1347_STATUS_REG 0x17
> +#define DS1347_CLOCK_BURST 0x3F
> +
> +
> +
> +static int ds1347_read_reg(struct device *dev, unsigned char address,
> + unsigned char *data)
> +{
> + struct spi_device *spi = to_spi_device(dev);
> +
> + *data = address | 0x80;
> +
> + return spi_write_then_read(spi, data, 1, data, 1);
> +}
> +
> +static int ds1347_write_reg(struct device *dev, unsigned char address,
> + unsigned char data)
> +{
> + struct spi_device *spi = to_spi_device(dev);
> + unsigned char buf[2];
> +
> + buf[0] = address & 0x7F;
> + buf[1] = data;
> +
> + return spi_write_then_read(spi, buf, 2, NULL, 0);
> +}
> +
> +static int ds1347_read_time(struct device *dev, struct rtc_time *dt)
> +{
> + struct spi_device *spi = to_spi_device(dev);
> + int err;
> + unsigned char buf[8];
> +
> + buf[0] = DS1347_CLOCK_BURST | 0x80;
> +
> + err = spi_write_then_read(spi, buf, 1, buf, 8);
> + if (err)
> + return err;
> +
> + dt->tm_sec = bcd2bin(buf[0]);
> + dt->tm_min = bcd2bin(buf[1]);
> + dt->tm_hour = bcd2bin(buf[2] & 0x3F);
> + dt->tm_mday = bcd2bin(buf[3]);
> + dt->tm_mon = bcd2bin(buf[4]) - 1;
> + dt->tm_wday = bcd2bin(buf[5]) - 1;
> + dt->tm_year = bcd2bin(buf[6]) + 100;
> +
> + return rtc_valid_tm(dt);
> +}
> +
> +static int ds1347_set_time(struct device *dev, struct rtc_time *dt)
> +{
> + struct spi_device *spi = to_spi_device(dev);
> + unsigned char buf[9];
> +
> + buf[0] = DS1347_CLOCK_BURST & 0x7F;
> + buf[1] = bin2bcd(dt->tm_sec);
> + buf[2] = bin2bcd(dt->tm_min);
> + buf[3] = (bin2bcd(dt->tm_hour) & 0x3F);
> + buf[4] = bin2bcd(dt->tm_mday);
> + buf[5] = bin2bcd(dt->tm_mon + 1);
> + buf[6] = bin2bcd(dt->tm_wday + 1);
> +
> + /*year in linux is from 1900 i.e in range of 100
> + in rtc it is from 00 to 99 so subtract
please leave a space after /*
> + the linux year by 100*/
and before */
(ditto for the other comments)
> + if (dt->tm_year >= 100)
> + dt->tm_year -= 100;
> +
> + buf[7] = bin2bcd(dt->tm_year);
> + buf[8] = bin2bcd(0x00);
> +
> + /*write the rtc settings*/
> + return spi_write_then_read(spi, buf, 9, NULL, 0);
> +}
> +
> +
> +
extra white lines
> +static const struct rtc_class_ops ds1347_rtc_ops = {
> + .read_time = ds1347_read_time,
> + .set_time = ds1347_set_time,
> +};
> +
> +static int ds1347_probe(struct spi_device *spi)
> +{
> + struct rtc_device *rtc;
> + unsigned char data;
> + int res;
> +
> + /*spi setup with ds1347 in mode 3 and bits per word as 8*/
> + spi->mode = SPI_MODE_3;
> + spi->bits_per_word = 8;
> + spi_setup(spi);
> +
> + /*RTC Settings*/
> +
unnecessary comment
> + /*check if spi read is successfull*/
unnecessary comment, this is pretty clear
> + res = ds1347_read_reg(&spi->dev, DS1347_SECONDS_REG, &data);
> + if (res)
> + return res;
> +
> + /*Disable the write protect of rtc*/
> + ds1347_read_reg(&spi->dev, DS1347_CONTROL_REG, &data);
> + data = data & ~(1<<7);
> + ds1347_write_reg(&spi->dev, DS1347_CONTROL_REG, data);
> +
> + /*Enable the oscillator , disable the oscillator stop flag,
> + and glitch filter to reduce current consumption*/
> + ds1347_read_reg(&spi->dev, DS1347_STATUS_REG, &data);
> + data = data & 0x1B;
> + ds1347_write_reg(&spi->dev, DS1347_STATUS_REG, data);
> +
> + /*display the settings*/
> + ds1347_read_reg(&spi->dev, DS1347_CONTROL_REG, &data);
> + dev_info(&spi->dev, "DS1347 RTC CTRL Reg = 0x%02x\n", data);
> +
> + ds1347_read_reg(&spi->dev, DS1347_STATUS_REG, &data);
> + dev_info(&spi->dev, "DS1347 RTC Status Reg = 0x%02x\n", data);
> +
> + rtc = devm_rtc_device_register(&spi->dev, "ds1347",
> + &ds1347_rtc_ops, THIS_MODULE);
> +
> + if (IS_ERR(rtc))
> + return PTR_ERR(rtc);
> +
> + spi_set_drvdata(spi, rtc);
> +
> + return 0;
> +}
> +
> +static struct spi_driver ds1347_driver = {
> + .driver = {
> + .name = "ds1347",
> + .owner = THIS_MODULE,
> + },
> + .probe = ds1347_probe,
> +};
> +
> +module_spi_driver(ds1347_driver);
> +
> +MODULE_DESCRIPTION("DS1347 SPI RTC DRIVER");
> +MODULE_AUTHOR("Raghavendra C Ganiga <ravi23ganiga@gmail.com>");
> +MODULE_LICENSE("GPL");
should be GPL v2
as you declared in the comments at the top of the file.
--
Best regards,
Alessandro Zummo,
Tower Technologies - Torino, Italy
http://www.towertech.it
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH] rtc: add support for maxim dallas rtc ds1347
@ 2014-03-20 16:25 RAGHAVENDRA GANIGA
2014-03-20 18:22 ` Alessandro Zummo
0 siblings, 1 reply; 8+ messages in thread
From: RAGHAVENDRA GANIGA @ 2014-03-20 16:25 UTC (permalink / raw)
To: a.zummo, linux-kernel, rtc-linux
>From 64f99d0497c6de5157bc4931b47aadc266f5961e Mon Sep 17 00:00:00 2001
From: Raghavendra Ganiga <ravi23ganiga@gmail.com>
Date: Thu, 20 Mar 2014 00:53:30 +0530
Subject: [PATCH] rtc: add support for maxim dallas rtc ds1347
This is a patch to add support for
maxim dallas rtc ds1347
Signed-off-by: Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
---
drivers/rtc/Kconfig | 12 ++++
drivers/rtc/Makefile | 1 +
drivers/rtc/rtc-ds1347.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 188 insertions(+)
create mode 100644 drivers/rtc/rtc-ds1347.c
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index db933de..2e565f8 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -573,6 +573,18 @@ config RTC_DRV_DS1305
This driver can also be built as a module. If so, the module
will be called rtc-ds1305.
+config RTC_DRV_DS1347
+ tristate "Dallas/Maxim DS1347"
+ help
+ If you say yes here you get support for the
+ Dallas/Maxim DS1347 chips.
+
+ This driver only supports the RTC feature, and not other chip
+ features such as alarms.
+
+ This driver can also be built as a module. If so, the module
+ will be called rtc-ds1347.
+
config RTC_DRV_DS1390
tristate "Dallas/Maxim DS1390/93/94"
help
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index b427bf7..40a0991 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_RTC_DRV_DS1286) += rtc-ds1286.o
obj-$(CONFIG_RTC_DRV_DS1302) += rtc-ds1302.o
obj-$(CONFIG_RTC_DRV_DS1305) += rtc-ds1305.o
obj-$(CONFIG_RTC_DRV_DS1307) += rtc-ds1307.o
+obj-$(CONFIG_RTC_DRV_DS1347) += rtc-ds1347.o
obj-$(CONFIG_RTC_DRV_DS1374) += rtc-ds1374.o
obj-$(CONFIG_RTC_DRV_DS1390) += rtc-ds1390.o
obj-$(CONFIG_RTC_DRV_DS1511) += rtc-ds1511.o
diff --git a/drivers/rtc/rtc-ds1347.c b/drivers/rtc/rtc-ds1347.c
new file mode 100644
index 0000000..3c9a934
--- /dev/null
+++ b/drivers/rtc/rtc-ds1347.c
@@ -0,0 +1,175 @@
+/* rtc-ds1347.c
+ *
+ * Driver for Dallas Semiconductor DS1347 Low Current, SPI Compatible
+ * Real Time Clock
+ *
+ * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
+ *
+ * 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 <linux/init.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/platform_device.h>
+#include <linux/rtc.h>
+#include <linux/spi/spi.h>
+#include <linux/bcd.h>
+
+/*Registers in ds1347 rtc*/
+
+#define DS1347_SECONDS_REG 0x01
+#define DS1347_MINUTES_REG 0x03
+#define DS1347_HOURS_REG 0x05
+#define DS1347_DATE_REG 0x07
+#define DS1347_MONTH_REG 0x09
+#define DS1347_DAY_REG 0x0B
+#define DS1347_YEAR_REG 0x0D
+#define DS1347_CONTROL_REG 0x0F
+#define DS1347_STATUS_REG 0x17
+#define DS1347_CLOCK_BURST 0x3F
+
+
+
+static int ds1347_read_reg(struct device *dev, unsigned char address,
+ unsigned char *data)
+{
+ struct spi_device *spi = to_spi_device(dev);
+
+ *data = address | 0x80;
+
+ return spi_write_then_read(spi, data, 1, data, 1);
+}
+
+static int ds1347_write_reg(struct device *dev, unsigned char address,
+ unsigned char data)
+{
+ struct spi_device *spi = to_spi_device(dev);
+ unsigned char buf[2];
+
+ buf[0] = address & 0x7F;
+ buf[1] = data;
+
+ return spi_write_then_read(spi, buf, 2, NULL, 0);
+}
+
+static int ds1347_read_time(struct device *dev, struct rtc_time *dt)
+{
+ struct spi_device *spi = to_spi_device(dev);
+ int err;
+ unsigned char buf[8];
+
+ buf[0] = DS1347_CLOCK_BURST | 0x80;
+
+ err = spi_write_then_read(spi, buf, 1, buf, 8);
+ if (err)
+ return err;
+
+ dt->tm_sec = bcd2bin(buf[0]);
+ dt->tm_min = bcd2bin(buf[1]);
+ dt->tm_hour = bcd2bin(buf[2] & 0x3F);
+ dt->tm_mday = bcd2bin(buf[3]);
+ dt->tm_mon = bcd2bin(buf[4]) - 1;
+ dt->tm_wday = bcd2bin(buf[5]) - 1;
+ dt->tm_year = bcd2bin(buf[6]) + 100;
+
+ return rtc_valid_tm(dt);
+}
+
+static int ds1347_set_time(struct device *dev, struct rtc_time *dt)
+{
+ struct spi_device *spi = to_spi_device(dev);
+ unsigned char buf[9];
+
+ buf[0] = DS1347_CLOCK_BURST & 0x7F;
+ buf[1] = bin2bcd(dt->tm_sec);
+ buf[2] = bin2bcd(dt->tm_min);
+ buf[3] = (bin2bcd(dt->tm_hour) & 0x3F);
+ buf[4] = bin2bcd(dt->tm_mday);
+ buf[5] = bin2bcd(dt->tm_mon + 1);
+ buf[6] = bin2bcd(dt->tm_wday + 1);
+
+ /*year in linux is from 1900 i.e in range of 100
+ in rtc it is from 00 to 99 so subtract
+ the linux year by 100*/
+ if (dt->tm_year >= 100)
+ dt->tm_year -= 100;
+
+ buf[7] = bin2bcd(dt->tm_year);
+ buf[8] = bin2bcd(0x00);
+
+ /*write the rtc settings*/
+ return spi_write_then_read(spi, buf, 9, NULL, 0);
+}
+
+
+
+static const struct rtc_class_ops ds1347_rtc_ops = {
+ .read_time = ds1347_read_time,
+ .set_time = ds1347_set_time,
+};
+
+static int ds1347_probe(struct spi_device *spi)
+{
+ struct rtc_device *rtc;
+ unsigned char data;
+ int res;
+
+ /*spi setup with ds1347 in mode 3 and bits per word as 8*/
+ spi->mode = SPI_MODE_3;
+ spi->bits_per_word = 8;
+ spi_setup(spi);
+
+ /*RTC Settings*/
+
+ /*check if spi read is successfull*/
+ res = ds1347_read_reg(&spi->dev, DS1347_SECONDS_REG, &data);
+ if (res)
+ return res;
+
+ /*Disable the write protect of rtc*/
+ ds1347_read_reg(&spi->dev, DS1347_CONTROL_REG, &data);
+ data = data & ~(1<<7);
+ ds1347_write_reg(&spi->dev, DS1347_CONTROL_REG, data);
+
+ /*Enable the oscillator , disable the oscillator stop flag,
+ and glitch filter to reduce current consumption*/
+ ds1347_read_reg(&spi->dev, DS1347_STATUS_REG, &data);
+ data = data & 0x1B;
+ ds1347_write_reg(&spi->dev, DS1347_STATUS_REG, data);
+
+ /*display the settings*/
+ ds1347_read_reg(&spi->dev, DS1347_CONTROL_REG, &data);
+ dev_info(&spi->dev, "DS1347 RTC CTRL Reg = 0x%02x\n", data);
+
+ ds1347_read_reg(&spi->dev, DS1347_STATUS_REG, &data);
+ dev_info(&spi->dev, "DS1347 RTC Status Reg = 0x%02x\n", data);
+
+ rtc = devm_rtc_device_register(&spi->dev, "ds1347",
+ &ds1347_rtc_ops, THIS_MODULE);
+
+ if (IS_ERR(rtc))
+ return PTR_ERR(rtc);
+
+ spi_set_drvdata(spi, rtc);
+
+ return 0;
+}
+
+static struct spi_driver ds1347_driver = {
+ .driver = {
+ .name = "ds1347",
+ .owner = THIS_MODULE,
+ },
+ .probe = ds1347_probe,
+};
+
+module_spi_driver(ds1347_driver);
+
+MODULE_DESCRIPTION("DS1347 SPI RTC DRIVER");
+MODULE_AUTHOR("Raghavendra C Ganiga <ravi23ganiga@gmail.com>");
+MODULE_LICENSE("GPL");
+
--
1.7.10.4
^ permalink raw reply related [flat|nested] 8+ messages in thread
end of thread, other threads:[~2014-03-21 16:14 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-21 14:35 [PATCH] rtc: add support for maxim dallas rtc ds1347 RAGHAVENDRA GANIGA
2014-03-21 16:14 ` [rtc-linux] " Alessandro Zummo
-- strict thread matches above, loose matches on Subject: below --
2014-03-20 19:00 RAGHAVENDRA GANIGA
2014-03-20 19:19 ` Joe Perches
2014-03-20 21:01 ` Alessandro Zummo
2014-03-20 21:08 ` Joe Perches
2014-03-20 16:25 RAGHAVENDRA GANIGA
2014-03-20 18:22 ` Alessandro Zummo
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.