Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Driver for the Epson RTC module RX-6110 SA
0004  *
0005  * Copyright(C) 2015 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
0006  * Copyright(C) SEIKO EPSON CORPORATION 2013. All rights reserved.
0007  */
0008 
0009 #include <linux/bcd.h>
0010 #include <linux/init.h>
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/of_gpio.h>
0014 #include <linux/regmap.h>
0015 #include <linux/rtc.h>
0016 #include <linux/of.h>
0017 #include <linux/of_device.h>
0018 #include <linux/spi/spi.h>
0019 #include <linux/i2c.h>
0020 
0021 /* RX-6110 Register definitions */
0022 #define RX6110_REG_SEC      0x10
0023 #define RX6110_REG_MIN      0x11
0024 #define RX6110_REG_HOUR     0x12
0025 #define RX6110_REG_WDAY     0x13
0026 #define RX6110_REG_MDAY     0x14
0027 #define RX6110_REG_MONTH    0x15
0028 #define RX6110_REG_YEAR     0x16
0029 #define RX6110_REG_RES1     0x17
0030 #define RX6110_REG_ALMIN    0x18
0031 #define RX6110_REG_ALHOUR   0x19
0032 #define RX6110_REG_ALWDAY   0x1A
0033 #define RX6110_REG_TCOUNT0  0x1B
0034 #define RX6110_REG_TCOUNT1  0x1C
0035 #define RX6110_REG_EXT      0x1D
0036 #define RX6110_REG_FLAG     0x1E
0037 #define RX6110_REG_CTRL     0x1F
0038 #define RX6110_REG_USER0    0x20
0039 #define RX6110_REG_USER1    0x21
0040 #define RX6110_REG_USER2    0x22
0041 #define RX6110_REG_USER3    0x23
0042 #define RX6110_REG_USER4    0x24
0043 #define RX6110_REG_USER5    0x25
0044 #define RX6110_REG_USER6    0x26
0045 #define RX6110_REG_USER7    0x27
0046 #define RX6110_REG_USER8    0x28
0047 #define RX6110_REG_USER9    0x29
0048 #define RX6110_REG_USERA    0x2A
0049 #define RX6110_REG_USERB    0x2B
0050 #define RX6110_REG_USERC    0x2C
0051 #define RX6110_REG_USERD    0x2D
0052 #define RX6110_REG_USERE    0x2E
0053 #define RX6110_REG_USERF    0x2F
0054 #define RX6110_REG_RES2     0x30
0055 #define RX6110_REG_RES3     0x31
0056 #define RX6110_REG_IRQ      0x32
0057 
0058 #define RX6110_BIT_ALARM_EN     BIT(7)
0059 
0060 /* Extension Register (1Dh) bit positions */
0061 #define RX6110_BIT_EXT_TSEL0        BIT(0)
0062 #define RX6110_BIT_EXT_TSEL1        BIT(1)
0063 #define RX6110_BIT_EXT_TSEL2        BIT(2)
0064 #define RX6110_BIT_EXT_WADA     BIT(3)
0065 #define RX6110_BIT_EXT_TE       BIT(4)
0066 #define RX6110_BIT_EXT_USEL     BIT(5)
0067 #define RX6110_BIT_EXT_FSEL0        BIT(6)
0068 #define RX6110_BIT_EXT_FSEL1        BIT(7)
0069 
0070 /* Flag Register (1Eh) bit positions */
0071 #define RX6110_BIT_FLAG_VLF     BIT(1)
0072 #define RX6110_BIT_FLAG_AF      BIT(3)
0073 #define RX6110_BIT_FLAG_TF      BIT(4)
0074 #define RX6110_BIT_FLAG_UF      BIT(5)
0075 
0076 /* Control Register (1Fh) bit positions */
0077 #define RX6110_BIT_CTRL_TBKE        BIT(0)
0078 #define RX6110_BIT_CTRL_TBKON       BIT(1)
0079 #define RX6110_BIT_CTRL_TSTP        BIT(2)
0080 #define RX6110_BIT_CTRL_AIE     BIT(3)
0081 #define RX6110_BIT_CTRL_TIE     BIT(4)
0082 #define RX6110_BIT_CTRL_UIE     BIT(5)
0083 #define RX6110_BIT_CTRL_STOP        BIT(6)
0084 #define RX6110_BIT_CTRL_TEST        BIT(7)
0085 
0086 enum {
0087     RTC_SEC = 0,
0088     RTC_MIN,
0089     RTC_HOUR,
0090     RTC_WDAY,
0091     RTC_MDAY,
0092     RTC_MONTH,
0093     RTC_YEAR,
0094     RTC_NR_TIME
0095 };
0096 
0097 #define RX6110_DRIVER_NAME      "rx6110"
0098 
0099 struct rx6110_data {
0100     struct rtc_device *rtc;
0101     struct regmap *regmap;
0102 };
0103 
0104 /**
0105  * rx6110_rtc_tm_to_data - convert rtc_time to native time encoding
0106  *
0107  * @tm: holds date and time
0108  * @data: holds the encoding in rx6110 native form
0109  */
0110 static int rx6110_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
0111 {
0112     pr_debug("%s: date %ptRr\n", __func__, tm);
0113 
0114     /*
0115      * The year in the RTC is a value between 0 and 99.
0116      * Assume that this represents the current century
0117      * and disregard all other values.
0118      */
0119     if (tm->tm_year < 100 || tm->tm_year >= 200)
0120         return -EINVAL;
0121 
0122     data[RTC_SEC] = bin2bcd(tm->tm_sec);
0123     data[RTC_MIN] = bin2bcd(tm->tm_min);
0124     data[RTC_HOUR] = bin2bcd(tm->tm_hour);
0125     data[RTC_WDAY] = BIT(bin2bcd(tm->tm_wday));
0126     data[RTC_MDAY] = bin2bcd(tm->tm_mday);
0127     data[RTC_MONTH] = bin2bcd(tm->tm_mon + 1);
0128     data[RTC_YEAR] = bin2bcd(tm->tm_year % 100);
0129 
0130     return 0;
0131 }
0132 
0133 /**
0134  * rx6110_data_to_rtc_tm - convert native time encoding to rtc_time
0135  *
0136  * @data: holds the encoding in rx6110 native form
0137  * @tm: holds date and time
0138  */
0139 static int rx6110_data_to_rtc_tm(u8 *data, struct rtc_time *tm)
0140 {
0141     tm->tm_sec = bcd2bin(data[RTC_SEC] & 0x7f);
0142     tm->tm_min = bcd2bin(data[RTC_MIN] & 0x7f);
0143     /* only 24-hour clock */
0144     tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
0145     tm->tm_wday = ffs(data[RTC_WDAY] & 0x7f);
0146     tm->tm_mday = bcd2bin(data[RTC_MDAY] & 0x3f);
0147     tm->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1f) - 1;
0148     tm->tm_year = bcd2bin(data[RTC_YEAR]) + 100;
0149 
0150     pr_debug("%s: date %ptRr\n", __func__, tm);
0151 
0152     /*
0153      * The year in the RTC is a value between 0 and 99.
0154      * Assume that this represents the current century
0155      * and disregard all other values.
0156      */
0157     if (tm->tm_year < 100 || tm->tm_year >= 200)
0158         return -EINVAL;
0159 
0160     return 0;
0161 }
0162 
0163 /**
0164  * rx6110_set_time - set the current time in the rx6110 registers
0165  *
0166  * @dev: the rtc device in use
0167  * @tm: holds date and time
0168  *
0169  * BUG: The HW assumes every year that is a multiple of 4 to be a leap
0170  * year. Next time this is wrong is 2100, which will not be a leap year
0171  *
0172  * Note: If STOP is not set/cleared, the clock will start when the seconds
0173  *       register is written
0174  *
0175  */
0176 static int rx6110_set_time(struct device *dev, struct rtc_time *tm)
0177 {
0178     struct rx6110_data *rx6110 = dev_get_drvdata(dev);
0179     u8 data[RTC_NR_TIME];
0180     int ret;
0181 
0182     ret = rx6110_rtc_tm_to_data(tm, data);
0183     if (ret < 0)
0184         return ret;
0185 
0186     /* set STOP bit before changing clock/calendar */
0187     ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
0188                  RX6110_BIT_CTRL_STOP, RX6110_BIT_CTRL_STOP);
0189     if (ret)
0190         return ret;
0191 
0192     ret = regmap_bulk_write(rx6110->regmap, RX6110_REG_SEC, data,
0193                 RTC_NR_TIME);
0194     if (ret)
0195         return ret;
0196 
0197     /* The time in the RTC is valid. Be sure to have VLF cleared. */
0198     ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
0199                  RX6110_BIT_FLAG_VLF, 0);
0200     if (ret)
0201         return ret;
0202 
0203     /* clear STOP bit after changing clock/calendar */
0204     ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
0205                  RX6110_BIT_CTRL_STOP, 0);
0206 
0207     return ret;
0208 }
0209 
0210 /**
0211  * rx6110_get_time - get the current time from the rx6110 registers
0212  * @dev: the rtc device in use
0213  * @tm: holds date and time
0214  */
0215 static int rx6110_get_time(struct device *dev, struct rtc_time *tm)
0216 {
0217     struct rx6110_data *rx6110 = dev_get_drvdata(dev);
0218     u8 data[RTC_NR_TIME];
0219     int flags;
0220     int ret;
0221 
0222     ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
0223     if (ret)
0224         return -EINVAL;
0225 
0226     /* check for VLF Flag (set at power-on) */
0227     if ((flags & RX6110_BIT_FLAG_VLF)) {
0228         dev_warn(dev, "Voltage low, data is invalid.\n");
0229         return -EINVAL;
0230     }
0231 
0232     /* read registers to date */
0233     ret = regmap_bulk_read(rx6110->regmap, RX6110_REG_SEC, data,
0234                    RTC_NR_TIME);
0235     if (ret)
0236         return ret;
0237 
0238     ret = rx6110_data_to_rtc_tm(data, tm);
0239     if (ret)
0240         return ret;
0241 
0242     dev_dbg(dev, "%s: date %ptRr\n", __func__, tm);
0243 
0244     return 0;
0245 }
0246 
0247 static const struct reg_sequence rx6110_default_regs[] = {
0248     { RX6110_REG_RES1,   0xB8 },
0249     { RX6110_REG_RES2,   0x00 },
0250     { RX6110_REG_RES3,   0x10 },
0251     { RX6110_REG_IRQ,    0x00 },
0252     { RX6110_REG_ALMIN,  0x00 },
0253     { RX6110_REG_ALHOUR, 0x00 },
0254     { RX6110_REG_ALWDAY, 0x00 },
0255 };
0256 
0257 /**
0258  * rx6110_init - initialize the rx6110 registers
0259  *
0260  * @rx6110: pointer to the rx6110 struct in use
0261  *
0262  */
0263 static int rx6110_init(struct rx6110_data *rx6110)
0264 {
0265     struct rtc_device *rtc = rx6110->rtc;
0266     int flags;
0267     int ret;
0268 
0269     ret = regmap_update_bits(rx6110->regmap, RX6110_REG_EXT,
0270                  RX6110_BIT_EXT_TE, 0);
0271     if (ret)
0272         return ret;
0273 
0274     ret = regmap_register_patch(rx6110->regmap, rx6110_default_regs,
0275                     ARRAY_SIZE(rx6110_default_regs));
0276     if (ret)
0277         return ret;
0278 
0279     ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
0280     if (ret)
0281         return ret;
0282 
0283     /* check for VLF Flag (set at power-on) */
0284     if ((flags & RX6110_BIT_FLAG_VLF))
0285         dev_warn(&rtc->dev, "Voltage low, data loss detected.\n");
0286 
0287     /* check for Alarm Flag */
0288     if (flags & RX6110_BIT_FLAG_AF)
0289         dev_warn(&rtc->dev, "An alarm may have been missed.\n");
0290 
0291     /* check for Periodic Timer Flag */
0292     if (flags & RX6110_BIT_FLAG_TF)
0293         dev_warn(&rtc->dev, "Periodic timer was detected\n");
0294 
0295     /* check for Update Timer Flag */
0296     if (flags & RX6110_BIT_FLAG_UF)
0297         dev_warn(&rtc->dev, "Update timer was detected\n");
0298 
0299     /* clear all flags BUT VLF */
0300     ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
0301                  RX6110_BIT_FLAG_AF |
0302                  RX6110_BIT_FLAG_UF |
0303                  RX6110_BIT_FLAG_TF,
0304                  0);
0305 
0306     return ret;
0307 }
0308 
0309 static const struct rtc_class_ops rx6110_rtc_ops = {
0310     .read_time = rx6110_get_time,
0311     .set_time = rx6110_set_time,
0312 };
0313 
0314 static int rx6110_probe(struct rx6110_data *rx6110, struct device *dev)
0315 {
0316     int err;
0317 
0318     rx6110->rtc = devm_rtc_device_register(dev,
0319                            RX6110_DRIVER_NAME,
0320                            &rx6110_rtc_ops, THIS_MODULE);
0321 
0322     if (IS_ERR(rx6110->rtc))
0323         return PTR_ERR(rx6110->rtc);
0324 
0325     err = rx6110_init(rx6110);
0326     if (err)
0327         return err;
0328 
0329     rx6110->rtc->max_user_freq = 1;
0330 
0331     return 0;
0332 }
0333 
0334 #if IS_ENABLED(CONFIG_SPI_MASTER)
0335 static struct regmap_config regmap_spi_config = {
0336     .reg_bits = 8,
0337     .val_bits = 8,
0338     .max_register = RX6110_REG_IRQ,
0339     .read_flag_mask = 0x80,
0340 };
0341 
0342 /**
0343  * rx6110_spi_probe - initialize rtc driver
0344  * @spi: pointer to spi device
0345  */
0346 static int rx6110_spi_probe(struct spi_device *spi)
0347 {
0348     struct rx6110_data *rx6110;
0349 
0350     if ((spi->bits_per_word && spi->bits_per_word != 8) ||
0351         (spi->max_speed_hz > 2000000) ||
0352         (spi->mode != (SPI_CS_HIGH | SPI_CPOL | SPI_CPHA))) {
0353         dev_warn(&spi->dev, "SPI settings: bits_per_word: %d, max_speed_hz: %d, mode: %xh\n",
0354              spi->bits_per_word, spi->max_speed_hz, spi->mode);
0355         dev_warn(&spi->dev, "driving device in an unsupported mode");
0356     }
0357 
0358     rx6110 = devm_kzalloc(&spi->dev, sizeof(*rx6110), GFP_KERNEL);
0359     if (!rx6110)
0360         return -ENOMEM;
0361 
0362     rx6110->regmap = devm_regmap_init_spi(spi, &regmap_spi_config);
0363     if (IS_ERR(rx6110->regmap)) {
0364         dev_err(&spi->dev, "regmap init failed for rtc rx6110\n");
0365         return PTR_ERR(rx6110->regmap);
0366     }
0367 
0368     spi_set_drvdata(spi, rx6110);
0369 
0370     return rx6110_probe(rx6110, &spi->dev);
0371 }
0372 
0373 static const struct spi_device_id rx6110_spi_id[] = {
0374     { "rx6110", 0 },
0375     { }
0376 };
0377 MODULE_DEVICE_TABLE(spi, rx6110_spi_id);
0378 
0379 static const struct of_device_id rx6110_spi_of_match[] = {
0380     { .compatible = "epson,rx6110" },
0381     { },
0382 };
0383 MODULE_DEVICE_TABLE(of, rx6110_spi_of_match);
0384 
0385 static struct spi_driver rx6110_spi_driver = {
0386     .driver = {
0387         .name = RX6110_DRIVER_NAME,
0388         .of_match_table = of_match_ptr(rx6110_spi_of_match),
0389     },
0390     .probe      = rx6110_spi_probe,
0391     .id_table   = rx6110_spi_id,
0392 };
0393 
0394 static int rx6110_spi_register(void)
0395 {
0396     return spi_register_driver(&rx6110_spi_driver);
0397 }
0398 
0399 static void rx6110_spi_unregister(void)
0400 {
0401     spi_unregister_driver(&rx6110_spi_driver);
0402 }
0403 #else
0404 static int rx6110_spi_register(void)
0405 {
0406     return 0;
0407 }
0408 
0409 static void rx6110_spi_unregister(void)
0410 {
0411 }
0412 #endif /* CONFIG_SPI_MASTER */
0413 
0414 #if IS_ENABLED(CONFIG_I2C)
0415 static struct regmap_config regmap_i2c_config = {
0416     .reg_bits = 8,
0417     .val_bits = 8,
0418     .max_register = RX6110_REG_IRQ,
0419     .read_flag_mask = 0x80,
0420 };
0421 
0422 static int rx6110_i2c_probe(struct i2c_client *client)
0423 {
0424     struct i2c_adapter *adapter = client->adapter;
0425     struct rx6110_data *rx6110;
0426 
0427     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
0428                 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
0429         dev_err(&adapter->dev,
0430             "doesn't support required functionality\n");
0431         return -EIO;
0432     }
0433 
0434     rx6110 = devm_kzalloc(&client->dev, sizeof(*rx6110), GFP_KERNEL);
0435     if (!rx6110)
0436         return -ENOMEM;
0437 
0438     rx6110->regmap = devm_regmap_init_i2c(client, &regmap_i2c_config);
0439     if (IS_ERR(rx6110->regmap)) {
0440         dev_err(&client->dev, "regmap init failed for rtc rx6110\n");
0441         return PTR_ERR(rx6110->regmap);
0442     }
0443 
0444     i2c_set_clientdata(client, rx6110);
0445 
0446     return rx6110_probe(rx6110, &client->dev);
0447 }
0448 
0449 static const struct acpi_device_id rx6110_i2c_acpi_match[] = {
0450     { "SECC6110" },
0451     { }
0452 };
0453 MODULE_DEVICE_TABLE(acpi, rx6110_i2c_acpi_match);
0454 
0455 static const struct i2c_device_id rx6110_i2c_id[] = {
0456     { "rx6110", 0 },
0457     { }
0458 };
0459 MODULE_DEVICE_TABLE(i2c, rx6110_i2c_id);
0460 
0461 static struct i2c_driver rx6110_i2c_driver = {
0462     .driver = {
0463         .name = RX6110_DRIVER_NAME,
0464         .acpi_match_table = rx6110_i2c_acpi_match,
0465     },
0466     .probe_new  = rx6110_i2c_probe,
0467     .id_table   = rx6110_i2c_id,
0468 };
0469 
0470 static int rx6110_i2c_register(void)
0471 {
0472     return i2c_add_driver(&rx6110_i2c_driver);
0473 }
0474 
0475 static void rx6110_i2c_unregister(void)
0476 {
0477     i2c_del_driver(&rx6110_i2c_driver);
0478 }
0479 #else
0480 static int rx6110_i2c_register(void)
0481 {
0482     return 0;
0483 }
0484 
0485 static void rx6110_i2c_unregister(void)
0486 {
0487 }
0488 #endif /* CONFIG_I2C */
0489 
0490 static int __init rx6110_module_init(void)
0491 {
0492     int ret;
0493 
0494     ret = rx6110_spi_register();
0495     if (ret)
0496         return ret;
0497 
0498     ret = rx6110_i2c_register();
0499     if (ret)
0500         rx6110_spi_unregister();
0501 
0502     return ret;
0503 }
0504 module_init(rx6110_module_init);
0505 
0506 static void __exit rx6110_module_exit(void)
0507 {
0508     rx6110_spi_unregister();
0509     rx6110_i2c_unregister();
0510 }
0511 module_exit(rx6110_module_exit);
0512 
0513 MODULE_AUTHOR("Val Krutov <val.krutov@erd.epson.com>");
0514 MODULE_DESCRIPTION("RX-6110 SA RTC driver");
0515 MODULE_LICENSE("GPL");