hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/drivers/rtc/rtc-ds1347.c
....@@ -1,14 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* rtc-ds1347.c
23 *
34 * Driver for Dallas Semiconductor DS1347 Low Current, SPI Compatible
45 * Real Time Clock
56 *
67 * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
7
- *
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.
11
- *
128 */
139
1410 #include <linux/init.h>
....@@ -30,8 +26,14 @@
3026 #define DS1347_DAY_REG 0x0B
3127 #define DS1347_YEAR_REG 0x0D
3228 #define DS1347_CONTROL_REG 0x0F
29
+#define DS1347_CENTURY_REG 0x13
3330 #define DS1347_STATUS_REG 0x17
3431 #define DS1347_CLOCK_BURST 0x3F
32
+
33
+#define DS1347_WP_BIT BIT(7)
34
+
35
+#define DS1347_NEOSC_BIT BIT(7)
36
+#define DS1347_OSF_BIT BIT(2)
3537
3638 static const struct regmap_range ds1347_ranges[] = {
3739 {
....@@ -47,35 +49,54 @@
4749
4850 static int ds1347_read_time(struct device *dev, struct rtc_time *dt)
4951 {
50
- struct spi_device *spi = to_spi_device(dev);
51
- struct regmap *map;
52
- int err;
52
+ struct regmap *map = dev_get_drvdata(dev);
53
+ unsigned int status, century, secs;
5354 unsigned char buf[8];
55
+ int err;
5456
55
- map = spi_get_drvdata(spi);
56
-
57
- err = regmap_bulk_read(map, DS1347_CLOCK_BURST, buf, 8);
57
+ err = regmap_read(map, DS1347_STATUS_REG, &status);
5858 if (err)
5959 return err;
6060
61
+ if (status & DS1347_OSF_BIT)
62
+ return -EINVAL;
63
+
64
+ do {
65
+ err = regmap_bulk_read(map, DS1347_CLOCK_BURST, buf, 8);
66
+ if (err)
67
+ return err;
68
+
69
+ err = regmap_read(map, DS1347_CENTURY_REG, &century);
70
+ if (err)
71
+ return err;
72
+
73
+ err = regmap_read(map, DS1347_SECONDS_REG, &secs);
74
+ if (err)
75
+ return err;
76
+ } while (buf[0] != secs);
77
+
6178 dt->tm_sec = bcd2bin(buf[0]);
62
- dt->tm_min = bcd2bin(buf[1]);
79
+ dt->tm_min = bcd2bin(buf[1] & 0x7f);
6380 dt->tm_hour = bcd2bin(buf[2] & 0x3F);
6481 dt->tm_mday = bcd2bin(buf[3]);
6582 dt->tm_mon = bcd2bin(buf[4]) - 1;
6683 dt->tm_wday = bcd2bin(buf[5]) - 1;
67
- dt->tm_year = bcd2bin(buf[6]) + 100;
84
+ dt->tm_year = (bcd2bin(century) * 100) + bcd2bin(buf[6]) - 1900;
6885
6986 return 0;
7087 }
7188
7289 static int ds1347_set_time(struct device *dev, struct rtc_time *dt)
7390 {
74
- struct spi_device *spi = to_spi_device(dev);
75
- struct regmap *map;
91
+ struct regmap *map = dev_get_drvdata(dev);
92
+ unsigned int century;
7693 unsigned char buf[8];
94
+ int err;
7795
78
- map = spi_get_drvdata(spi);
96
+ err = regmap_update_bits(map, DS1347_STATUS_REG,
97
+ DS1347_NEOSC_BIT, DS1347_NEOSC_BIT);
98
+ if (err)
99
+ return err;
79100
80101 buf[0] = bin2bcd(dt->tm_sec);
81102 buf[1] = bin2bcd(dt->tm_min);
....@@ -83,16 +104,20 @@
83104 buf[3] = bin2bcd(dt->tm_mday);
84105 buf[4] = bin2bcd(dt->tm_mon + 1);
85106 buf[5] = bin2bcd(dt->tm_wday + 1);
86
-
87
- /* year in linux is from 1900 i.e in range of 100
88
- in rtc it is from 00 to 99 */
89
- dt->tm_year = dt->tm_year % 100;
90
-
91
- buf[6] = bin2bcd(dt->tm_year);
107
+ buf[6] = bin2bcd(dt->tm_year % 100);
92108 buf[7] = bin2bcd(0x00);
93109
94
- /* write the rtc settings */
95
- return regmap_bulk_write(map, DS1347_CLOCK_BURST, buf, 8);
110
+ err = regmap_bulk_write(map, DS1347_CLOCK_BURST, buf, 8);
111
+ if (err)
112
+ return err;
113
+
114
+ century = (dt->tm_year / 100) + 19;
115
+ err = regmap_write(map, DS1347_CENTURY_REG, bin2bcd(century));
116
+ if (err)
117
+ return err;
118
+
119
+ return regmap_update_bits(map, DS1347_STATUS_REG,
120
+ DS1347_NEOSC_BIT | DS1347_OSF_BIT, 0);
96121 }
97122
98123 static const struct rtc_class_ops ds1347_rtc_ops = {
....@@ -105,8 +130,7 @@
105130 struct rtc_device *rtc;
106131 struct regmap_config config;
107132 struct regmap *map;
108
- unsigned int data;
109
- int res;
133
+ int err;
110134
111135 memset(&config, 0, sizeof(config));
112136 config.reg_bits = 8;
....@@ -129,36 +153,20 @@
129153
130154 spi_set_drvdata(spi, map);
131155
132
- /* RTC Settings */
133
- res = regmap_read(map, DS1347_SECONDS_REG, &data);
134
- if (res)
135
- return res;
136
-
137156 /* Disable the write protect of rtc */
138
- regmap_read(map, DS1347_CONTROL_REG, &data);
139
- data = data & ~(1<<7);
140
- regmap_write(map, DS1347_CONTROL_REG, data);
157
+ err = regmap_update_bits(map, DS1347_CONTROL_REG, DS1347_WP_BIT, 0);
158
+ if (err)
159
+ return err;
141160
142
- /* Enable the oscillator , disable the oscillator stop flag,
143
- and glitch filter to reduce current consumption */
144
- regmap_read(map, DS1347_STATUS_REG, &data);
145
- data = data & 0x1B;
146
- regmap_write(map, DS1347_STATUS_REG, data);
147
-
148
- /* display the settings */
149
- regmap_read(map, DS1347_CONTROL_REG, &data);
150
- dev_info(&spi->dev, "DS1347 RTC CTRL Reg = 0x%02x\n", data);
151
-
152
- regmap_read(map, DS1347_STATUS_REG, &data);
153
- dev_info(&spi->dev, "DS1347 RTC Status Reg = 0x%02x\n", data);
154
-
155
- rtc = devm_rtc_device_register(&spi->dev, "ds1347",
156
- &ds1347_rtc_ops, THIS_MODULE);
157
-
161
+ rtc = devm_rtc_allocate_device(&spi->dev);
158162 if (IS_ERR(rtc))
159163 return PTR_ERR(rtc);
160164
161
- return 0;
165
+ rtc->ops = &ds1347_rtc_ops;
166
+ rtc->range_min = RTC_TIMESTAMP_BEGIN_0000;
167
+ rtc->range_max = RTC_TIMESTAMP_END_9999;
168
+
169
+ return rtc_register_device(rtc);
162170 }
163171
164172 static struct spi_driver ds1347_driver = {