1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the Epson RTC module RX-6110 SA
5 * Copyright(C) 2015 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
6 * Copyright(C) SEIKO EPSON CORPORATION 2013. All rights reserved.
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/rtc.h>
16 #include <linux/of_device.h>
17 #include <linux/spi/spi.h>
18 #include <linux/i2c.h>
20 /* RX-6110 Register definitions */
21 #define RX6110_REG_SEC 0x10
22 #define RX6110_REG_MIN 0x11
23 #define RX6110_REG_HOUR 0x12
24 #define RX6110_REG_WDAY 0x13
25 #define RX6110_REG_MDAY 0x14
26 #define RX6110_REG_MONTH 0x15
27 #define RX6110_REG_YEAR 0x16
28 #define RX6110_REG_RES1 0x17
29 #define RX6110_REG_ALMIN 0x18
30 #define RX6110_REG_ALHOUR 0x19
31 #define RX6110_REG_ALWDAY 0x1A
32 #define RX6110_REG_TCOUNT0 0x1B
33 #define RX6110_REG_TCOUNT1 0x1C
34 #define RX6110_REG_EXT 0x1D
35 #define RX6110_REG_FLAG 0x1E
36 #define RX6110_REG_CTRL 0x1F
37 #define RX6110_REG_USER0 0x20
38 #define RX6110_REG_USER1 0x21
39 #define RX6110_REG_USER2 0x22
40 #define RX6110_REG_USER3 0x23
41 #define RX6110_REG_USER4 0x24
42 #define RX6110_REG_USER5 0x25
43 #define RX6110_REG_USER6 0x26
44 #define RX6110_REG_USER7 0x27
45 #define RX6110_REG_USER8 0x28
46 #define RX6110_REG_USER9 0x29
47 #define RX6110_REG_USERA 0x2A
48 #define RX6110_REG_USERB 0x2B
49 #define RX6110_REG_USERC 0x2C
50 #define RX6110_REG_USERD 0x2D
51 #define RX6110_REG_USERE 0x2E
52 #define RX6110_REG_USERF 0x2F
53 #define RX6110_REG_RES2 0x30
54 #define RX6110_REG_RES3 0x31
55 #define RX6110_REG_IRQ 0x32
57 #define RX6110_BIT_ALARM_EN BIT(7)
59 /* Extension Register (1Dh) bit positions */
60 #define RX6110_BIT_EXT_TSEL0 BIT(0)
61 #define RX6110_BIT_EXT_TSEL1 BIT(1)
62 #define RX6110_BIT_EXT_TSEL2 BIT(2)
63 #define RX6110_BIT_EXT_WADA BIT(3)
64 #define RX6110_BIT_EXT_TE BIT(4)
65 #define RX6110_BIT_EXT_USEL BIT(5)
66 #define RX6110_BIT_EXT_FSEL0 BIT(6)
67 #define RX6110_BIT_EXT_FSEL1 BIT(7)
69 /* Flag Register (1Eh) bit positions */
70 #define RX6110_BIT_FLAG_VLF BIT(1)
71 #define RX6110_BIT_FLAG_AF BIT(3)
72 #define RX6110_BIT_FLAG_TF BIT(4)
73 #define RX6110_BIT_FLAG_UF BIT(5)
75 /* Control Register (1Fh) bit positions */
76 #define RX6110_BIT_CTRL_TBKE BIT(0)
77 #define RX6110_BIT_CTRL_TBKON BIT(1)
78 #define RX6110_BIT_CTRL_TSTP BIT(2)
79 #define RX6110_BIT_CTRL_AIE BIT(3)
80 #define RX6110_BIT_CTRL_TIE BIT(4)
81 #define RX6110_BIT_CTRL_UIE BIT(5)
82 #define RX6110_BIT_CTRL_STOP BIT(6)
83 #define RX6110_BIT_CTRL_TEST BIT(7)
96 #define RX6110_DRIVER_NAME "rx6110"
99 struct rtc_device *rtc;
100 struct regmap *regmap;
104 * rx6110_rtc_tm_to_data - convert rtc_time to native time encoding
106 * @tm: holds date and time
107 * @data: holds the encoding in rx6110 native form
109 static int rx6110_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
111 pr_debug("%s: date %ptRr\n", __func__, tm);
114 * The year in the RTC is a value between 0 and 99.
115 * Assume that this represents the current century
116 * and disregard all other values.
118 if (tm->tm_year < 100 || tm->tm_year >= 200)
121 data[RTC_SEC] = bin2bcd(tm->tm_sec);
122 data[RTC_MIN] = bin2bcd(tm->tm_min);
123 data[RTC_HOUR] = bin2bcd(tm->tm_hour);
124 data[RTC_WDAY] = BIT(bin2bcd(tm->tm_wday));
125 data[RTC_MDAY] = bin2bcd(tm->tm_mday);
126 data[RTC_MONTH] = bin2bcd(tm->tm_mon + 1);
127 data[RTC_YEAR] = bin2bcd(tm->tm_year % 100);
133 * rx6110_data_to_rtc_tm - convert native time encoding to rtc_time
135 * @data: holds the encoding in rx6110 native form
136 * @tm: holds date and time
138 static int rx6110_data_to_rtc_tm(u8 *data, struct rtc_time *tm)
140 tm->tm_sec = bcd2bin(data[RTC_SEC] & 0x7f);
141 tm->tm_min = bcd2bin(data[RTC_MIN] & 0x7f);
142 /* only 24-hour clock */
143 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
144 tm->tm_wday = ffs(data[RTC_WDAY] & 0x7f);
145 tm->tm_mday = bcd2bin(data[RTC_MDAY] & 0x3f);
146 tm->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1f) - 1;
147 tm->tm_year = bcd2bin(data[RTC_YEAR]) + 100;
149 pr_debug("%s: date %ptRr\n", __func__, tm);
152 * The year in the RTC is a value between 0 and 99.
153 * Assume that this represents the current century
154 * and disregard all other values.
156 if (tm->tm_year < 100 || tm->tm_year >= 200)
163 * rx6110_set_time - set the current time in the rx6110 registers
165 * @dev: the rtc device in use
166 * @tm: holds date and time
168 * BUG: The HW assumes every year that is a multiple of 4 to be a leap
169 * year. Next time this is wrong is 2100, which will not be a leap year
171 * Note: If STOP is not set/cleared, the clock will start when the seconds
172 * register is written
175 static int rx6110_set_time(struct device *dev, struct rtc_time *tm)
177 struct rx6110_data *rx6110 = dev_get_drvdata(dev);
178 u8 data[RTC_NR_TIME];
181 ret = rx6110_rtc_tm_to_data(tm, data);
185 /* set STOP bit before changing clock/calendar */
186 ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
187 RX6110_BIT_CTRL_STOP, RX6110_BIT_CTRL_STOP);
191 ret = regmap_bulk_write(rx6110->regmap, RX6110_REG_SEC, data,
196 /* The time in the RTC is valid. Be sure to have VLF cleared. */
197 ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
198 RX6110_BIT_FLAG_VLF, 0);
202 /* clear STOP bit after changing clock/calendar */
203 ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
204 RX6110_BIT_CTRL_STOP, 0);
210 * rx6110_get_time - get the current time from the rx6110 registers
211 * @dev: the rtc device in use
212 * @tm: holds date and time
214 static int rx6110_get_time(struct device *dev, struct rtc_time *tm)
216 struct rx6110_data *rx6110 = dev_get_drvdata(dev);
217 u8 data[RTC_NR_TIME];
221 ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
225 /* check for VLF Flag (set at power-on) */
226 if ((flags & RX6110_BIT_FLAG_VLF)) {
227 dev_warn(dev, "Voltage low, data is invalid.\n");
231 /* read registers to date */
232 ret = regmap_bulk_read(rx6110->regmap, RX6110_REG_SEC, data,
237 ret = rx6110_data_to_rtc_tm(data, tm);
241 dev_dbg(dev, "%s: date %ptRr\n", __func__, tm);
246 static const struct reg_sequence rx6110_default_regs[] = {
247 { RX6110_REG_RES1, 0xB8 },
248 { RX6110_REG_RES2, 0x00 },
249 { RX6110_REG_RES3, 0x10 },
250 { RX6110_REG_IRQ, 0x00 },
251 { RX6110_REG_ALMIN, 0x00 },
252 { RX6110_REG_ALHOUR, 0x00 },
253 { RX6110_REG_ALWDAY, 0x00 },
257 * rx6110_init - initialize the rx6110 registers
259 * @rx6110: pointer to the rx6110 struct in use
262 static int rx6110_init(struct rx6110_data *rx6110)
264 struct rtc_device *rtc = rx6110->rtc;
268 ret = regmap_update_bits(rx6110->regmap, RX6110_REG_EXT,
269 RX6110_BIT_EXT_TE, 0);
273 ret = regmap_register_patch(rx6110->regmap, rx6110_default_regs,
274 ARRAY_SIZE(rx6110_default_regs));
278 ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
282 /* check for VLF Flag (set at power-on) */
283 if ((flags & RX6110_BIT_FLAG_VLF))
284 dev_warn(&rtc->dev, "Voltage low, data loss detected.\n");
286 /* check for Alarm Flag */
287 if (flags & RX6110_BIT_FLAG_AF)
288 dev_warn(&rtc->dev, "An alarm may have been missed.\n");
290 /* check for Periodic Timer Flag */
291 if (flags & RX6110_BIT_FLAG_TF)
292 dev_warn(&rtc->dev, "Periodic timer was detected\n");
294 /* check for Update Timer Flag */
295 if (flags & RX6110_BIT_FLAG_UF)
296 dev_warn(&rtc->dev, "Update timer was detected\n");
298 /* clear all flags BUT VLF */
299 ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
308 static const struct rtc_class_ops rx6110_rtc_ops = {
309 .read_time = rx6110_get_time,
310 .set_time = rx6110_set_time,
313 static int rx6110_probe(struct rx6110_data *rx6110, struct device *dev)
317 rx6110->rtc = devm_rtc_device_register(dev,
319 &rx6110_rtc_ops, THIS_MODULE);
321 if (IS_ERR(rx6110->rtc))
322 return PTR_ERR(rx6110->rtc);
324 err = rx6110_init(rx6110);
328 rx6110->rtc->max_user_freq = 1;
333 #if IS_ENABLED(CONFIG_SPI_MASTER)
334 static struct regmap_config regmap_spi_config = {
337 .max_register = RX6110_REG_IRQ,
338 .read_flag_mask = 0x80,
342 * rx6110_spi_probe - initialize rtc driver
343 * @spi: pointer to spi device
345 static int rx6110_spi_probe(struct spi_device *spi)
347 struct rx6110_data *rx6110;
349 if ((spi->bits_per_word && spi->bits_per_word != 8) ||
350 (spi->max_speed_hz > 2000000) ||
351 (spi->mode != (SPI_CS_HIGH | SPI_CPOL | SPI_CPHA))) {
352 dev_warn(&spi->dev, "SPI settings: bits_per_word: %d, max_speed_hz: %d, mode: %xh\n",
353 spi->bits_per_word, spi->max_speed_hz, spi->mode);
354 dev_warn(&spi->dev, "driving device in an unsupported mode");
357 rx6110 = devm_kzalloc(&spi->dev, sizeof(*rx6110), GFP_KERNEL);
361 rx6110->regmap = devm_regmap_init_spi(spi, ®map_spi_config);
362 if (IS_ERR(rx6110->regmap)) {
363 dev_err(&spi->dev, "regmap init failed for rtc rx6110\n");
364 return PTR_ERR(rx6110->regmap);
367 spi_set_drvdata(spi, rx6110);
369 return rx6110_probe(rx6110, &spi->dev);
372 static const struct spi_device_id rx6110_spi_id[] = {
376 MODULE_DEVICE_TABLE(spi, rx6110_spi_id);
378 static const __maybe_unused struct of_device_id rx6110_spi_of_match[] = {
379 { .compatible = "epson,rx6110" },
382 MODULE_DEVICE_TABLE(of, rx6110_spi_of_match);
384 static struct spi_driver rx6110_spi_driver = {
386 .name = RX6110_DRIVER_NAME,
387 .of_match_table = of_match_ptr(rx6110_spi_of_match),
389 .probe = rx6110_spi_probe,
390 .id_table = rx6110_spi_id,
393 static int rx6110_spi_register(void)
395 return spi_register_driver(&rx6110_spi_driver);
398 static void rx6110_spi_unregister(void)
400 spi_unregister_driver(&rx6110_spi_driver);
403 static int rx6110_spi_register(void)
408 static void rx6110_spi_unregister(void)
411 #endif /* CONFIG_SPI_MASTER */
413 #if IS_ENABLED(CONFIG_I2C)
414 static struct regmap_config regmap_i2c_config = {
417 .max_register = RX6110_REG_IRQ,
418 .read_flag_mask = 0x80,
421 static int rx6110_i2c_probe(struct i2c_client *client)
423 struct i2c_adapter *adapter = client->adapter;
424 struct rx6110_data *rx6110;
426 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
427 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
428 dev_err(&adapter->dev,
429 "doesn't support required functionality\n");
433 rx6110 = devm_kzalloc(&client->dev, sizeof(*rx6110), GFP_KERNEL);
437 rx6110->regmap = devm_regmap_init_i2c(client, ®map_i2c_config);
438 if (IS_ERR(rx6110->regmap)) {
439 dev_err(&client->dev, "regmap init failed for rtc rx6110\n");
440 return PTR_ERR(rx6110->regmap);
443 i2c_set_clientdata(client, rx6110);
445 return rx6110_probe(rx6110, &client->dev);
448 static const struct acpi_device_id rx6110_i2c_acpi_match[] = {
452 MODULE_DEVICE_TABLE(acpi, rx6110_i2c_acpi_match);
454 static const struct i2c_device_id rx6110_i2c_id[] = {
458 MODULE_DEVICE_TABLE(i2c, rx6110_i2c_id);
460 static struct i2c_driver rx6110_i2c_driver = {
462 .name = RX6110_DRIVER_NAME,
463 .acpi_match_table = rx6110_i2c_acpi_match,
465 .probe = rx6110_i2c_probe,
466 .id_table = rx6110_i2c_id,
469 static int rx6110_i2c_register(void)
471 return i2c_add_driver(&rx6110_i2c_driver);
474 static void rx6110_i2c_unregister(void)
476 i2c_del_driver(&rx6110_i2c_driver);
479 static int rx6110_i2c_register(void)
484 static void rx6110_i2c_unregister(void)
487 #endif /* CONFIG_I2C */
489 static int __init rx6110_module_init(void)
493 ret = rx6110_spi_register();
497 ret = rx6110_i2c_register();
499 rx6110_spi_unregister();
503 module_init(rx6110_module_init);
505 static void __exit rx6110_module_exit(void)
507 rx6110_spi_unregister();
508 rx6110_i2c_unregister();
510 module_exit(rx6110_module_exit);
512 MODULE_AUTHOR("Val Krutov <val.krutov@erd.epson.com>");
513 MODULE_DESCRIPTION("RX-6110 SA RTC driver");
514 MODULE_LICENSE("GPL");