1 // SPDX-License-Identifier: GPL-2.0-only
4 * Driver for Dallas Semiconductor DS1347 Low Current, SPI Compatible
7 * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/platform_device.h>
14 #include <linux/rtc.h>
15 #include <linux/spi/spi.h>
16 #include <linux/bcd.h>
17 #include <linux/regmap.h>
19 /* Registers in ds1347 rtc */
21 #define DS1347_SECONDS_REG 0x01
22 #define DS1347_MINUTES_REG 0x03
23 #define DS1347_HOURS_REG 0x05
24 #define DS1347_DATE_REG 0x07
25 #define DS1347_MONTH_REG 0x09
26 #define DS1347_DAY_REG 0x0B
27 #define DS1347_YEAR_REG 0x0D
28 #define DS1347_CONTROL_REG 0x0F
29 #define DS1347_STATUS_REG 0x17
30 #define DS1347_CLOCK_BURST 0x3F
32 static const struct regmap_range ds1347_ranges[] = {
34 .range_min = DS1347_SECONDS_REG,
35 .range_max = DS1347_STATUS_REG,
39 static const struct regmap_access_table ds1347_access_table = {
40 .yes_ranges = ds1347_ranges,
41 .n_yes_ranges = ARRAY_SIZE(ds1347_ranges),
44 static int ds1347_read_time(struct device *dev, struct rtc_time *dt)
46 struct regmap *map = dev_get_drvdata(dev);
50 err = regmap_bulk_read(map, DS1347_CLOCK_BURST, buf, 8);
54 dt->tm_sec = bcd2bin(buf[0]);
55 dt->tm_min = bcd2bin(buf[1]);
56 dt->tm_hour = bcd2bin(buf[2] & 0x3F);
57 dt->tm_mday = bcd2bin(buf[3]);
58 dt->tm_mon = bcd2bin(buf[4]) - 1;
59 dt->tm_wday = bcd2bin(buf[5]) - 1;
60 dt->tm_year = bcd2bin(buf[6]) + 100;
65 static int ds1347_set_time(struct device *dev, struct rtc_time *dt)
67 struct regmap *map = dev_get_drvdata(dev);
70 buf[0] = bin2bcd(dt->tm_sec);
71 buf[1] = bin2bcd(dt->tm_min);
72 buf[2] = (bin2bcd(dt->tm_hour) & 0x3F);
73 buf[3] = bin2bcd(dt->tm_mday);
74 buf[4] = bin2bcd(dt->tm_mon + 1);
75 buf[5] = bin2bcd(dt->tm_wday + 1);
77 /* year in linux is from 1900 i.e in range of 100
78 in rtc it is from 00 to 99 */
79 dt->tm_year = dt->tm_year % 100;
81 buf[6] = bin2bcd(dt->tm_year);
82 buf[7] = bin2bcd(0x00);
84 /* write the rtc settings */
85 return regmap_bulk_write(map, DS1347_CLOCK_BURST, buf, 8);
88 static const struct rtc_class_ops ds1347_rtc_ops = {
89 .read_time = ds1347_read_time,
90 .set_time = ds1347_set_time,
93 static int ds1347_probe(struct spi_device *spi)
95 struct rtc_device *rtc;
96 struct regmap_config config;
100 memset(&config, 0, sizeof(config));
103 config.read_flag_mask = 0x80;
104 config.max_register = 0x3F;
105 config.wr_table = &ds1347_access_table;
107 /* spi setup with ds1347 in mode 3 and bits per word as 8 */
108 spi->mode = SPI_MODE_3;
109 spi->bits_per_word = 8;
112 map = devm_regmap_init_spi(spi, &config);
115 dev_err(&spi->dev, "ds1347 regmap init spi failed\n");
119 spi_set_drvdata(spi, map);
121 /* Disable the write protect of rtc */
122 regmap_read(map, DS1347_CONTROL_REG, &data);
123 data = data & ~(1<<7);
124 regmap_write(map, DS1347_CONTROL_REG, data);
126 /* Enable the oscillator , disable the oscillator stop flag,
127 and glitch filter to reduce current consumption */
128 regmap_read(map, DS1347_STATUS_REG, &data);
130 regmap_write(map, DS1347_STATUS_REG, data);
132 rtc = devm_rtc_device_register(&spi->dev, "ds1347",
133 &ds1347_rtc_ops, THIS_MODULE);
141 static struct spi_driver ds1347_driver = {
145 .probe = ds1347_probe,
148 module_spi_driver(ds1347_driver);
150 MODULE_DESCRIPTION("DS1347 SPI RTC DRIVER");
151 MODULE_AUTHOR("Raghavendra C Ganiga <ravi23ganiga@gmail.com>");
152 MODULE_LICENSE("GPL v2");