.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* rtc-ds1347.c |
---|
2 | 3 | * |
---|
3 | 4 | * Driver for Dallas Semiconductor DS1347 Low Current, SPI Compatible |
---|
4 | 5 | * Real Time Clock |
---|
5 | 6 | * |
---|
6 | 7 | * 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 | | - * |
---|
12 | 8 | */ |
---|
13 | 9 | |
---|
14 | 10 | #include <linux/init.h> |
---|
.. | .. |
---|
30 | 26 | #define DS1347_DAY_REG 0x0B |
---|
31 | 27 | #define DS1347_YEAR_REG 0x0D |
---|
32 | 28 | #define DS1347_CONTROL_REG 0x0F |
---|
| 29 | +#define DS1347_CENTURY_REG 0x13 |
---|
33 | 30 | #define DS1347_STATUS_REG 0x17 |
---|
34 | 31 | #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) |
---|
35 | 37 | |
---|
36 | 38 | static const struct regmap_range ds1347_ranges[] = { |
---|
37 | 39 | { |
---|
.. | .. |
---|
47 | 49 | |
---|
48 | 50 | static int ds1347_read_time(struct device *dev, struct rtc_time *dt) |
---|
49 | 51 | { |
---|
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; |
---|
53 | 54 | unsigned char buf[8]; |
---|
| 55 | + int err; |
---|
54 | 56 | |
---|
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); |
---|
58 | 58 | if (err) |
---|
59 | 59 | return err; |
---|
60 | 60 | |
---|
| 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, ¢ury); |
---|
| 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 | + |
---|
61 | 78 | dt->tm_sec = bcd2bin(buf[0]); |
---|
62 | | - dt->tm_min = bcd2bin(buf[1]); |
---|
| 79 | + dt->tm_min = bcd2bin(buf[1] & 0x7f); |
---|
63 | 80 | dt->tm_hour = bcd2bin(buf[2] & 0x3F); |
---|
64 | 81 | dt->tm_mday = bcd2bin(buf[3]); |
---|
65 | 82 | dt->tm_mon = bcd2bin(buf[4]) - 1; |
---|
66 | 83 | 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; |
---|
68 | 85 | |
---|
69 | 86 | return 0; |
---|
70 | 87 | } |
---|
71 | 88 | |
---|
72 | 89 | static int ds1347_set_time(struct device *dev, struct rtc_time *dt) |
---|
73 | 90 | { |
---|
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; |
---|
76 | 93 | unsigned char buf[8]; |
---|
| 94 | + int err; |
---|
77 | 95 | |
---|
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; |
---|
79 | 100 | |
---|
80 | 101 | buf[0] = bin2bcd(dt->tm_sec); |
---|
81 | 102 | buf[1] = bin2bcd(dt->tm_min); |
---|
.. | .. |
---|
83 | 104 | buf[3] = bin2bcd(dt->tm_mday); |
---|
84 | 105 | buf[4] = bin2bcd(dt->tm_mon + 1); |
---|
85 | 106 | 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); |
---|
92 | 108 | buf[7] = bin2bcd(0x00); |
---|
93 | 109 | |
---|
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); |
---|
96 | 121 | } |
---|
97 | 122 | |
---|
98 | 123 | static const struct rtc_class_ops ds1347_rtc_ops = { |
---|
.. | .. |
---|
105 | 130 | struct rtc_device *rtc; |
---|
106 | 131 | struct regmap_config config; |
---|
107 | 132 | struct regmap *map; |
---|
108 | | - unsigned int data; |
---|
109 | | - int res; |
---|
| 133 | + int err; |
---|
110 | 134 | |
---|
111 | 135 | memset(&config, 0, sizeof(config)); |
---|
112 | 136 | config.reg_bits = 8; |
---|
.. | .. |
---|
129 | 153 | |
---|
130 | 154 | spi_set_drvdata(spi, map); |
---|
131 | 155 | |
---|
132 | | - /* RTC Settings */ |
---|
133 | | - res = regmap_read(map, DS1347_SECONDS_REG, &data); |
---|
134 | | - if (res) |
---|
135 | | - return res; |
---|
136 | | - |
---|
137 | 156 | /* 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; |
---|
141 | 160 | |
---|
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); |
---|
158 | 162 | if (IS_ERR(rtc)) |
---|
159 | 163 | return PTR_ERR(rtc); |
---|
160 | 164 | |
---|
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); |
---|
162 | 170 | } |
---|
163 | 171 | |
---|
164 | 172 | static struct spi_driver ds1347_driver = { |
---|