diff options
Diffstat (limited to 'drivers/rtc/rtc-rs5c348.c')
| -rw-r--r-- | drivers/rtc/rtc-rs5c348.c | 84 |
1 files changed, 32 insertions, 52 deletions
diff --git a/drivers/rtc/rtc-rs5c348.c b/drivers/rtc/rtc-rs5c348.c index f7a90a116a39..fec633f80789 100644 --- a/drivers/rtc/rtc-rs5c348.c +++ b/drivers/rtc/rtc-rs5c348.c @@ -1,12 +1,9 @@ +// SPDX-License-Identifier: GPL-2.0-only /* * A SPI driver for the Ricoh RS5C348 RTC * * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp> * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * * The board specific init code should provide characteristics of this * device: * Mode 1 (High-Active, Shift-Then-Sample), High Avtive CS @@ -25,8 +22,6 @@ #include <linux/spi/spi.h> #include <linux/module.h> -#define DRV_VERSION "0.2" - #define RS5C348_REG_SECS 0 #define RS5C348_REG_MINS 1 #define RS5C348_REG_HOURS 2 @@ -64,10 +59,21 @@ static int rs5c348_rtc_set_time(struct device *dev, struct rtc_time *tm) { struct spi_device *spi = to_spi_device(dev); - struct rs5c348_plat_data *pdata = spi->dev.platform_data; + struct rs5c348_plat_data *pdata = dev_get_platdata(&spi->dev); u8 txbuf[5+7], *txp; int ret; + ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_CTL2)); + if (ret < 0) + return ret; + if (ret & RS5C348_BIT_XSTP) { + txbuf[0] = RS5C348_CMD_W(RS5C348_REG_CTL2); + txbuf[1] = 0; + ret = spi_write_then_read(spi, txbuf, 2, NULL, 0); + if (ret < 0) + return ret; + } + /* Transfer 5 bytes before writing SEC. This gives 31us for carry. */ txp = txbuf; txbuf[0] = RS5C348_CMD_R(RS5C348_REG_CTL2); /* cmd, ctl2 */ @@ -100,10 +106,20 @@ static int rs5c348_rtc_read_time(struct device *dev, struct rtc_time *tm) { struct spi_device *spi = to_spi_device(dev); - struct rs5c348_plat_data *pdata = spi->dev.platform_data; + struct rs5c348_plat_data *pdata = dev_get_platdata(&spi->dev); u8 txbuf[5], rxbuf[7]; int ret; + ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_CTL2)); + if (ret < 0) + return ret; + if (ret & RS5C348_BIT_VDET) + dev_warn(&spi->dev, "voltage-low detected.\n"); + if (ret & RS5C348_BIT_XSTP) { + dev_warn(&spi->dev, "oscillator-stop detected.\n"); + return -EINVAL; + } + /* Transfer 5 byte befores reading SEC. This gives 31us for carry. */ txbuf[0] = RS5C348_CMD_R(RS5C348_REG_CTL2); /* cmd, ctl2 */ txbuf[1] = 0; /* dummy */ @@ -137,11 +153,6 @@ rs5c348_rtc_read_time(struct device *dev, struct rtc_time *tm) tm->tm_year = bcd2bin(rxbuf[RS5C348_REG_YEAR]) + ((rxbuf[RS5C348_REG_MONTH] & RS5C348_BIT_Y2K) ? 100 : 0); - if (rtc_valid_tm(tm) < 0) { - dev_err(&spi->dev, "retrieved date/time is not valid.\n"); - rtc_time_to_tm(0, tm); - } - return 0; } @@ -150,8 +161,6 @@ static const struct rtc_class_ops rs5c348_rtc_ops = { .set_time = rs5c348_rtc_set_time, }; -static struct spi_driver rs5c348_driver; - static int rs5c348_probe(struct spi_device *spi) { int ret; @@ -168,60 +177,32 @@ static int rs5c348_probe(struct spi_device *spi) ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_SECS)); if (ret < 0 || (ret & 0x80)) { dev_err(&spi->dev, "not found.\n"); - goto kfree_exit; + return ret; } - dev_info(&spi->dev, "chip found, driver version " DRV_VERSION "\n"); dev_info(&spi->dev, "spiclk %u KHz.\n", (spi->max_speed_hz + 500) / 1000); - /* turn RTC on if it was not on */ - ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_CTL2)); - if (ret < 0) - goto kfree_exit; - if (ret & (RS5C348_BIT_XSTP | RS5C348_BIT_VDET)) { - u8 buf[2]; - struct rtc_time tm; - if (ret & RS5C348_BIT_VDET) - dev_warn(&spi->dev, "voltage-low detected.\n"); - if (ret & RS5C348_BIT_XSTP) - dev_warn(&spi->dev, "oscillator-stop detected.\n"); - rtc_time_to_tm(0, &tm); /* 1970/1/1 */ - ret = rs5c348_rtc_set_time(&spi->dev, &tm); - if (ret < 0) - goto kfree_exit; - buf[0] = RS5C348_CMD_W(RS5C348_REG_CTL2); - buf[1] = 0; - ret = spi_write_then_read(spi, buf, sizeof(buf), NULL, 0); - if (ret < 0) - goto kfree_exit; - } - ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_CTL1)); if (ret < 0) - goto kfree_exit; + return ret; if (ret & RS5C348_BIT_24H) pdata->rtc_24h = 1; - rtc = devm_rtc_device_register(&spi->dev, rs5c348_driver.driver.name, - &rs5c348_rtc_ops, THIS_MODULE); - - if (IS_ERR(rtc)) { - ret = PTR_ERR(rtc); - goto kfree_exit; - } + rtc = devm_rtc_allocate_device(&spi->dev); + if (IS_ERR(rtc)) + return PTR_ERR(rtc); pdata->rtc = rtc; - return 0; - kfree_exit: - return ret; + rtc->ops = &rs5c348_rtc_ops; + + return devm_rtc_register_device(rtc); } static struct spi_driver rs5c348_driver = { .driver = { .name = "rtc-rs5c348", - .owner = THIS_MODULE, }, .probe = rs5c348_probe, }; @@ -231,5 +212,4 @@ module_spi_driver(rs5c348_driver); MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>"); MODULE_DESCRIPTION("Ricoh RS5C348 RTC driver"); MODULE_LICENSE("GPL"); -MODULE_VERSION(DRV_VERSION); MODULE_ALIAS("spi:rtc-rs5c348"); |
