xref: /openbmc/linux/drivers/rtc/rtc-rx6110.c (revision 0300b114)
1 /*
2  * Driver for the Epson RTC module RX-6110 SA
3  *
4  * Copyright(C) 2015 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
5  * Copyright(C) SEIKO EPSON CORPORATION 2013. All rights reserved.
6  *
7  * This driver software is distributed as is, without any warranty of any kind,
8  * either express or implied as further specified in the GNU Public License.
9  * This software may be used and distributed according to the terms of the GNU
10  * Public License, version 2 as published by the Free Software Foundation.
11  * See the file COPYING in the main directory of this archive for more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include <linux/bcd.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/of_gpio.h>
22 #include <linux/regmap.h>
23 #include <linux/rtc.h>
24 #include <linux/spi/spi.h>
25 
26 /* RX-6110 Register definitions */
27 #define RX6110_REG_SEC		0x10
28 #define RX6110_REG_MIN		0x11
29 #define RX6110_REG_HOUR		0x12
30 #define RX6110_REG_WDAY		0x13
31 #define RX6110_REG_MDAY		0x14
32 #define RX6110_REG_MONTH	0x15
33 #define RX6110_REG_YEAR		0x16
34 #define RX6110_REG_RES1		0x17
35 #define RX6110_REG_ALMIN	0x18
36 #define RX6110_REG_ALHOUR	0x19
37 #define RX6110_REG_ALWDAY	0x1A
38 #define RX6110_REG_TCOUNT0	0x1B
39 #define RX6110_REG_TCOUNT1	0x1C
40 #define RX6110_REG_EXT		0x1D
41 #define RX6110_REG_FLAG		0x1E
42 #define RX6110_REG_CTRL		0x1F
43 #define RX6110_REG_USER0	0x20
44 #define RX6110_REG_USER1	0x21
45 #define RX6110_REG_USER2	0x22
46 #define RX6110_REG_USER3	0x23
47 #define RX6110_REG_USER4	0x24
48 #define RX6110_REG_USER5	0x25
49 #define RX6110_REG_USER6	0x26
50 #define RX6110_REG_USER7	0x27
51 #define RX6110_REG_USER8	0x28
52 #define RX6110_REG_USER9	0x29
53 #define RX6110_REG_USERA	0x2A
54 #define RX6110_REG_USERB	0x2B
55 #define RX6110_REG_USERC	0x2C
56 #define RX6110_REG_USERD	0x2D
57 #define RX6110_REG_USERE	0x2E
58 #define RX6110_REG_USERF	0x2F
59 #define RX6110_REG_RES2		0x30
60 #define RX6110_REG_RES3		0x31
61 #define RX6110_REG_IRQ		0x32
62 
63 #define RX6110_BIT_ALARM_EN		BIT(7)
64 
65 /* Extension Register (1Dh) bit positions */
66 #define RX6110_BIT_EXT_TSEL0		BIT(0)
67 #define RX6110_BIT_EXT_TSEL1		BIT(1)
68 #define RX6110_BIT_EXT_TSEL2		BIT(2)
69 #define RX6110_BIT_EXT_WADA		BIT(3)
70 #define RX6110_BIT_EXT_TE		BIT(4)
71 #define RX6110_BIT_EXT_USEL		BIT(5)
72 #define RX6110_BIT_EXT_FSEL0		BIT(6)
73 #define RX6110_BIT_EXT_FSEL1		BIT(7)
74 
75 /* Flag Register (1Eh) bit positions */
76 #define RX6110_BIT_FLAG_VLF		BIT(1)
77 #define RX6110_BIT_FLAG_AF		BIT(3)
78 #define RX6110_BIT_FLAG_TF		BIT(4)
79 #define RX6110_BIT_FLAG_UF		BIT(5)
80 
81 /* Control Register (1Fh) bit positions */
82 #define RX6110_BIT_CTRL_TBKE		BIT(0)
83 #define RX6110_BIT_CTRL_TBKON		BIT(1)
84 #define RX6110_BIT_CTRL_TSTP		BIT(2)
85 #define RX6110_BIT_CTRL_AIE		BIT(3)
86 #define RX6110_BIT_CTRL_TIE		BIT(4)
87 #define RX6110_BIT_CTRL_UIE		BIT(5)
88 #define RX6110_BIT_CTRL_STOP		BIT(6)
89 #define RX6110_BIT_CTRL_TEST		BIT(7)
90 
91 enum {
92 	RTC_SEC = 0,
93 	RTC_MIN,
94 	RTC_HOUR,
95 	RTC_WDAY,
96 	RTC_MDAY,
97 	RTC_MONTH,
98 	RTC_YEAR,
99 	RTC_NR_TIME
100 };
101 
102 #define RX6110_DRIVER_NAME		"rx6110"
103 
104 struct rx6110_data {
105 	struct rtc_device *rtc;
106 	struct regmap *regmap;
107 };
108 
109 /**
110  * rx6110_rtc_tm_to_data - convert rtc_time to native time encoding
111  *
112  * @tm: holds date and time
113  * @data: holds the encoding in rx6110 native form
114  */
115 static int rx6110_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
116 {
117 	pr_debug("%s: date %ptRr\n", __func__, tm);
118 
119 	/*
120 	 * The year in the RTC is a value between 0 and 99.
121 	 * Assume that this represents the current century
122 	 * and disregard all other values.
123 	 */
124 	if (tm->tm_year < 100 || tm->tm_year >= 200)
125 		return -EINVAL;
126 
127 	data[RTC_SEC] = bin2bcd(tm->tm_sec);
128 	data[RTC_MIN] = bin2bcd(tm->tm_min);
129 	data[RTC_HOUR] = bin2bcd(tm->tm_hour);
130 	data[RTC_WDAY] = BIT(bin2bcd(tm->tm_wday));
131 	data[RTC_MDAY] = bin2bcd(tm->tm_mday);
132 	data[RTC_MONTH] = bin2bcd(tm->tm_mon + 1);
133 	data[RTC_YEAR] = bin2bcd(tm->tm_year % 100);
134 
135 	return 0;
136 }
137 
138 /**
139  * rx6110_data_to_rtc_tm - convert native time encoding to rtc_time
140  *
141  * @data: holds the encoding in rx6110 native form
142  * @tm: holds date and time
143  */
144 static int rx6110_data_to_rtc_tm(u8 *data, struct rtc_time *tm)
145 {
146 	tm->tm_sec = bcd2bin(data[RTC_SEC] & 0x7f);
147 	tm->tm_min = bcd2bin(data[RTC_MIN] & 0x7f);
148 	/* only 24-hour clock */
149 	tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
150 	tm->tm_wday = ffs(data[RTC_WDAY] & 0x7f);
151 	tm->tm_mday = bcd2bin(data[RTC_MDAY] & 0x3f);
152 	tm->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1f) - 1;
153 	tm->tm_year = bcd2bin(data[RTC_YEAR]) + 100;
154 
155 	pr_debug("%s: date %ptRr\n", __func__, tm);
156 
157 	/*
158 	 * The year in the RTC is a value between 0 and 99.
159 	 * Assume that this represents the current century
160 	 * and disregard all other values.
161 	 */
162 	if (tm->tm_year < 100 || tm->tm_year >= 200)
163 		return -EINVAL;
164 
165 	return 0;
166 }
167 
168 /**
169  * rx6110_set_time - set the current time in the rx6110 registers
170  *
171  * @dev: the rtc device in use
172  * @tm: holds date and time
173  *
174  * BUG: The HW assumes every year that is a multiple of 4 to be a leap
175  * year. Next time this is wrong is 2100, which will not be a leap year
176  *
177  * Note: If STOP is not set/cleared, the clock will start when the seconds
178  *       register is written
179  *
180  */
181 static int rx6110_set_time(struct device *dev, struct rtc_time *tm)
182 {
183 	struct rx6110_data *rx6110 = dev_get_drvdata(dev);
184 	u8 data[RTC_NR_TIME];
185 	int ret;
186 
187 	ret = rx6110_rtc_tm_to_data(tm, data);
188 	if (ret < 0)
189 		return ret;
190 
191 	/* set STOP bit before changing clock/calendar */
192 	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
193 				 RX6110_BIT_CTRL_STOP, RX6110_BIT_CTRL_STOP);
194 	if (ret)
195 		return ret;
196 
197 	ret = regmap_bulk_write(rx6110->regmap, RX6110_REG_SEC, data,
198 				RTC_NR_TIME);
199 	if (ret)
200 		return ret;
201 
202 	/* The time in the RTC is valid. Be sure to have VLF cleared. */
203 	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
204 				 RX6110_BIT_FLAG_VLF, 0);
205 	if (ret)
206 		return ret;
207 
208 	/* clear STOP bit after changing clock/calendar */
209 	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
210 				 RX6110_BIT_CTRL_STOP, 0);
211 
212 	return ret;
213 }
214 
215 /**
216  * rx6110_get_time - get the current time from the rx6110 registers
217  * @dev: the rtc device in use
218  * @tm: holds date and time
219  */
220 static int rx6110_get_time(struct device *dev, struct rtc_time *tm)
221 {
222 	struct rx6110_data *rx6110 = dev_get_drvdata(dev);
223 	u8 data[RTC_NR_TIME];
224 	int flags;
225 	int ret;
226 
227 	ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
228 	if (ret)
229 		return -EINVAL;
230 
231 	/* check for VLF Flag (set at power-on) */
232 	if ((flags & RX6110_BIT_FLAG_VLF)) {
233 		dev_warn(dev, "Voltage low, data is invalid.\n");
234 		return -EINVAL;
235 	}
236 
237 	/* read registers to date */
238 	ret = regmap_bulk_read(rx6110->regmap, RX6110_REG_SEC, data,
239 			       RTC_NR_TIME);
240 	if (ret)
241 		return ret;
242 
243 	ret = rx6110_data_to_rtc_tm(data, tm);
244 	if (ret)
245 		return ret;
246 
247 	dev_dbg(dev, "%s: date %ptRr\n", __func__, tm);
248 
249 	return 0;
250 }
251 
252 static const struct reg_sequence rx6110_default_regs[] = {
253 	{ RX6110_REG_RES1,   0xB8 },
254 	{ RX6110_REG_RES2,   0x00 },
255 	{ RX6110_REG_RES3,   0x10 },
256 	{ RX6110_REG_IRQ,    0x00 },
257 	{ RX6110_REG_ALMIN,  0x00 },
258 	{ RX6110_REG_ALHOUR, 0x00 },
259 	{ RX6110_REG_ALWDAY, 0x00 },
260 };
261 
262 /**
263  * rx6110_init - initialize the rx6110 registers
264  *
265  * @rx6110: pointer to the rx6110 struct in use
266  *
267  */
268 static int rx6110_init(struct rx6110_data *rx6110)
269 {
270 	struct rtc_device *rtc = rx6110->rtc;
271 	int flags;
272 	int ret;
273 
274 	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_EXT,
275 				 RX6110_BIT_EXT_TE, 0);
276 	if (ret)
277 		return ret;
278 
279 	ret = regmap_register_patch(rx6110->regmap, rx6110_default_regs,
280 				    ARRAY_SIZE(rx6110_default_regs));
281 	if (ret)
282 		return ret;
283 
284 	ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
285 	if (ret)
286 		return ret;
287 
288 	/* check for VLF Flag (set at power-on) */
289 	if ((flags & RX6110_BIT_FLAG_VLF))
290 		dev_warn(&rtc->dev, "Voltage low, data loss detected.\n");
291 
292 	/* check for Alarm Flag */
293 	if (flags & RX6110_BIT_FLAG_AF)
294 		dev_warn(&rtc->dev, "An alarm may have been missed.\n");
295 
296 	/* check for Periodic Timer Flag */
297 	if (flags & RX6110_BIT_FLAG_TF)
298 		dev_warn(&rtc->dev, "Periodic timer was detected\n");
299 
300 	/* check for Update Timer Flag */
301 	if (flags & RX6110_BIT_FLAG_UF)
302 		dev_warn(&rtc->dev, "Update timer was detected\n");
303 
304 	/* clear all flags BUT VLF */
305 	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
306 				 RX6110_BIT_FLAG_AF |
307 				 RX6110_BIT_FLAG_UF |
308 				 RX6110_BIT_FLAG_TF,
309 				 0);
310 
311 	return ret;
312 }
313 
314 static const struct rtc_class_ops rx6110_rtc_ops = {
315 	.read_time = rx6110_get_time,
316 	.set_time = rx6110_set_time,
317 };
318 
319 static struct regmap_config regmap_spi_config = {
320 	.reg_bits = 8,
321 	.val_bits = 8,
322 	.max_register = RX6110_REG_IRQ,
323 	.read_flag_mask = 0x80,
324 };
325 
326 /**
327  * rx6110_probe - initialize rtc driver
328  * @spi: pointer to spi device
329  */
330 static int rx6110_probe(struct spi_device *spi)
331 {
332 	struct rx6110_data *rx6110;
333 	int err;
334 
335 	if ((spi->bits_per_word && spi->bits_per_word != 8) ||
336 	    (spi->max_speed_hz > 2000000) ||
337 	    (spi->mode != (SPI_CS_HIGH | SPI_CPOL | SPI_CPHA))) {
338 		dev_warn(&spi->dev, "SPI settings: bits_per_word: %d, max_speed_hz: %d, mode: %xh\n",
339 			 spi->bits_per_word, spi->max_speed_hz, spi->mode);
340 		dev_warn(&spi->dev, "driving device in an unsupported mode");
341 	}
342 
343 	rx6110 = devm_kzalloc(&spi->dev, sizeof(*rx6110), GFP_KERNEL);
344 	if (!rx6110)
345 		return -ENOMEM;
346 
347 	rx6110->regmap = devm_regmap_init_spi(spi, &regmap_spi_config);
348 	if (IS_ERR(rx6110->regmap)) {
349 		dev_err(&spi->dev, "regmap init failed for rtc rx6110\n");
350 		return PTR_ERR(rx6110->regmap);
351 	}
352 
353 	spi_set_drvdata(spi, rx6110);
354 
355 	rx6110->rtc = devm_rtc_device_register(&spi->dev,
356 					       RX6110_DRIVER_NAME,
357 					       &rx6110_rtc_ops, THIS_MODULE);
358 
359 	if (IS_ERR(rx6110->rtc))
360 		return PTR_ERR(rx6110->rtc);
361 
362 	err = rx6110_init(rx6110);
363 	if (err)
364 		return err;
365 
366 	rx6110->rtc->max_user_freq = 1;
367 
368 	return 0;
369 }
370 
371 static int rx6110_remove(struct spi_device *spi)
372 {
373 	return 0;
374 }
375 
376 static const struct spi_device_id rx6110_id[] = {
377 	{ "rx6110", 0 },
378 	{ }
379 };
380 MODULE_DEVICE_TABLE(spi, rx6110_id);
381 
382 static struct spi_driver rx6110_driver = {
383 	.driver = {
384 		.name = RX6110_DRIVER_NAME,
385 	},
386 	.probe		= rx6110_probe,
387 	.remove		= rx6110_remove,
388 	.id_table	= rx6110_id,
389 };
390 
391 module_spi_driver(rx6110_driver);
392 
393 MODULE_AUTHOR("Val Krutov <val.krutov@erd.epson.com>");
394 MODULE_DESCRIPTION("RX-6110 SA RTC driver");
395 MODULE_LICENSE("GPL");
396