3 * Driver for Dallas Semiconductor DS1347 Low Current, SPI Compatible
6 * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/platform_device.h>
18 #include <linux/rtc.h>
19 #include <linux/spi/spi.h>
20 #include <linux/bcd.h>
22 /* Registers in ds1347 rtc */
24 #define DS1347_SECONDS_REG 0x01
25 #define DS1347_MINUTES_REG 0x03
26 #define DS1347_HOURS_REG 0x05
27 #define DS1347_DATE_REG 0x07
28 #define DS1347_MONTH_REG 0x09
29 #define DS1347_DAY_REG 0x0B
30 #define DS1347_YEAR_REG 0x0D
31 #define DS1347_CONTROL_REG 0x0F
32 #define DS1347_STATUS_REG 0x17
33 #define DS1347_CLOCK_BURST 0x3F
35 static int ds1347_read_reg(struct device
*dev
, unsigned char address
,
38 struct spi_device
*spi
= to_spi_device(dev
);
40 *data
= address
| 0x80;
42 return spi_write_then_read(spi
, data
, 1, data
, 1);
45 static int ds1347_write_reg(struct device
*dev
, unsigned char address
,
48 struct spi_device
*spi
= to_spi_device(dev
);
51 buf
[0] = address
& 0x7F;
54 return spi_write_then_read(spi
, buf
, 2, NULL
, 0);
57 static int ds1347_read_time(struct device
*dev
, struct rtc_time
*dt
)
59 struct spi_device
*spi
= to_spi_device(dev
);
63 buf
[0] = DS1347_CLOCK_BURST
| 0x80;
65 err
= spi_write_then_read(spi
, buf
, 1, buf
, 8);
69 dt
->tm_sec
= bcd2bin(buf
[0]);
70 dt
->tm_min
= bcd2bin(buf
[1]);
71 dt
->tm_hour
= bcd2bin(buf
[2] & 0x3F);
72 dt
->tm_mday
= bcd2bin(buf
[3]);
73 dt
->tm_mon
= bcd2bin(buf
[4]) - 1;
74 dt
->tm_wday
= bcd2bin(buf
[5]) - 1;
75 dt
->tm_year
= bcd2bin(buf
[6]) + 100;
77 return rtc_valid_tm(dt
);
80 static int ds1347_set_time(struct device
*dev
, struct rtc_time
*dt
)
82 struct spi_device
*spi
= to_spi_device(dev
);
85 buf
[0] = DS1347_CLOCK_BURST
& 0x7F;
86 buf
[1] = bin2bcd(dt
->tm_sec
);
87 buf
[2] = bin2bcd(dt
->tm_min
);
88 buf
[3] = (bin2bcd(dt
->tm_hour
) & 0x3F);
89 buf
[4] = bin2bcd(dt
->tm_mday
);
90 buf
[5] = bin2bcd(dt
->tm_mon
+ 1);
91 buf
[6] = bin2bcd(dt
->tm_wday
+ 1);
93 /* year in linux is from 1900 i.e in range of 100
94 in rtc it is from 00 to 99 */
95 dt
->tm_year
= dt
->tm_year
% 100;
97 buf
[7] = bin2bcd(dt
->tm_year
);
98 buf
[8] = bin2bcd(0x00);
100 /* write the rtc settings */
101 return spi_write_then_read(spi
, buf
, 9, NULL
, 0);
104 static const struct rtc_class_ops ds1347_rtc_ops
= {
105 .read_time
= ds1347_read_time
,
106 .set_time
= ds1347_set_time
,
109 static int ds1347_probe(struct spi_device
*spi
)
111 struct rtc_device
*rtc
;
115 /* spi setup with ds1347 in mode 3 and bits per word as 8 */
116 spi
->mode
= SPI_MODE_3
;
117 spi
->bits_per_word
= 8;
121 res
= ds1347_read_reg(&spi
->dev
, DS1347_SECONDS_REG
, &data
);
125 /* Disable the write protect of rtc */
126 ds1347_read_reg(&spi
->dev
, DS1347_CONTROL_REG
, &data
);
127 data
= data
& ~(1<<7);
128 ds1347_write_reg(&spi
->dev
, DS1347_CONTROL_REG
, data
);
130 /* Enable the oscillator , disable the oscillator stop flag,
131 and glitch filter to reduce current consumption */
132 ds1347_read_reg(&spi
->dev
, DS1347_STATUS_REG
, &data
);
134 ds1347_write_reg(&spi
->dev
, DS1347_STATUS_REG
, data
);
136 /* display the settings */
137 ds1347_read_reg(&spi
->dev
, DS1347_CONTROL_REG
, &data
);
138 dev_info(&spi
->dev
, "DS1347 RTC CTRL Reg = 0x%02x\n", data
);
140 ds1347_read_reg(&spi
->dev
, DS1347_STATUS_REG
, &data
);
141 dev_info(&spi
->dev
, "DS1347 RTC Status Reg = 0x%02x\n", data
);
143 rtc
= devm_rtc_device_register(&spi
->dev
, "ds1347",
144 &ds1347_rtc_ops
, THIS_MODULE
);
149 spi_set_drvdata(spi
, rtc
);
154 static struct spi_driver ds1347_driver
= {
157 .owner
= THIS_MODULE
,
159 .probe
= ds1347_probe
,
162 module_spi_driver(ds1347_driver
);
164 MODULE_DESCRIPTION("DS1347 SPI RTC DRIVER");
165 MODULE_AUTHOR("Raghavendra C Ganiga <ravi23ganiga@gmail.com>");
166 MODULE_LICENSE("GPL v2");