rtc: add a timestamp for year 0
Alexandre Belloni
maintainerPATCH 1/10 rtc: add a timestamp for year 0
A few RTCs handle dates from year 0 to year 9999. Add a timestamp even if years before 1970 will probably never be used.
--- include/linux/rtc.h | 1 + 1 file changed, 1 insertion(+)
include/linux/rtc.h +1 -0
@@ -160,6 +160,7 @@ struct rtc_device {#define to_rtc_device (d) container_of(d, struct rtc_device, dev)/* useful timestamps */ +#define RTC_TIMESTAMP_BEGIN_0000 -62167219200ULL/* 0000-01-01 00:00:00 */ #define RTC_TIMESTAMP_BEGIN_1900 -2208988800LL/* 1900-01-01 00:00:00 */ #define RTC_TIMESTAMP_BEGIN_2000 946684800LL/* 2000-01-01 00:00:00 */ #define RTC_TIMESTAMP_END_2063 2966371199LL/* 2063-12-31 23:59:59 */
Signature · 1 line
2.21.0
Alexandre Belloni
maintainerPATCH 2/10 rtc: ds1347: remove verbose messages
Printing debugging (and opaque) information is not useful and only clutters the boot log. Remove those messages.
--- drivers/rtc/rtc-ds1347.c | 7 ------- 1 file changed, 7 deletions(-)
drivers/rtc/rtc-ds1347.c +0 -7
@@ -141,13 +141,6 @@ static int ds1347_probe(struct spi_device *spi) data = data &0x1B ;regmap_write (map,DS1347_STATUS_REG , data); -/* display the settings */ -regmap_read (map,DS1347_CONTROL_REG , &data); -dev_info (&spi->dev,"DS1347 RTC CTRL Reg = 0x%02x\n" , data); - -regmap_read (map,DS1347_STATUS_REG , &data); -dev_info (&spi->dev,"DS1347 RTC Status Reg = 0x%02x\n" , data); - rtc =devm_rtc_device_register (&spi->dev,"ds1347" , &ds1347_rtc_ops,THIS_MODULE );
Signature · 1 line
2.21.0
Alexandre Belloni
maintainerPATCH 3/10 rtc: ds1347: remove useless read
In reply to Alexandre BelloniDS1347_SECONDS_REG is read at probe time but the value is simply discarded. Remove that useless read.
--- drivers/rtc/rtc-ds1347.c | 6 ------ 1 file changed, 6 deletions(-)
drivers/rtc/rtc-ds1347.c +0 -6
@@ -102,7 +102,6 @@ static int ds1347_probe(struct spi_device *spi)struct regmap_config config;struct regmap *map;unsigned int data; -int res;memset (&config,0 ,sizeof (config)); config.reg_bits =8 ;
@@ -125,11 +124,6 @@ static int ds1347_probe(struct spi_device *spi)spi_set_drvdata (spi, map); -/* RTC Settings */ - res =regmap_read (map,DS1347_SECONDS_REG , &data); -if (res) -return res; -/* Disable the write protect of rtc */ regmap_read (map,DS1347_CONTROL_REG , &data); data = data & ~(1 <<7 );
Signature · 1 line
2.21.0
Alexandre Belloni
maintainerPATCH 4/10 rtc: ds1347: simplify getting .driver_data
In reply to Alexandre BelloniGet 'driver_data' from 'struct device' directly. Going via spi_device is an unnecessary step.
--- drivers/rtc/rtc-ds1347.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/rtc/rtc-ds1347.c +2 -8
@@ -43,13 +43,10 @@ static const struct regmap_access_table ds1347_access_table = {static int ds1347_read_time (struct device *dev,struct rtc_time *dt) { -struct spi_device *spi =to_spi_device (dev); -struct regmap *map; +struct regmap *map =dev_get_drvdata (dev);int err;unsigned char buf[8 ]; - map =spi_get_drvdata (spi); - err =regmap_bulk_read (map,DS1347_CLOCK_BURST , buf,8 );if (err)return err;
@@ -67,12 +64,9 @@ static int ds1347_read_time(struct device *dev, struct rtc_time *dt)static int ds1347_set_time (struct device *dev,struct rtc_time *dt) { -struct spi_device *spi =to_spi_device (dev); -struct regmap *map; +struct regmap *map =dev_get_drvdata (dev);unsigned char buf[8 ]; - map =spi_get_drvdata (spi); - buf[0 ] =bin2bcd (dt->tm_sec); buf[1 ] =bin2bcd (dt->tm_min); buf[2 ] = (bin2bcd (dt->tm_hour) &0x3F );
Signature · 1 line
2.21.0
Alexandre Belloni
maintainerPATCH 5/10 rtc: ds1347: mask ALM OUT when reading time
In reply to Alexandre BelloniBit 7 of the minutes registers is ALM OUT. It indicates an alarm fired. Mask it out when reading the time.
--- drivers/rtc/rtc-ds1347.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rtc/rtc-ds1347.c +1 -1
@@ -52,7 +52,7 @@ static int ds1347_read_time(struct device *dev, struct rtc_time *dt)return err; dt->tm_sec =bcd2bin (buf[0 ]); - dt->tm_min =bcd2bin (buf[1 ]); + dt->tm_min =bcd2bin (buf[1 ] &0x7f ); dt->tm_hour =bcd2bin (buf[2 ] &0x3F ); dt->tm_mday =bcd2bin (buf[3 ]); dt->tm_mon =bcd2bin (buf[4 ]) -1 ;
Signature · 1 line
2.21.0
Alexandre Belloni
maintainerPATCH 6/10 rtc: ds1347: convert to devm_rtc_allocate_device
In reply to Alexandre BelloniThis allows further improvement of the driver.
--- drivers/rtc/rtc-ds1347.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/rtc/rtc-ds1347.c +4 -4
@@ -129,13 +129,13 @@ static int ds1347_probe(struct spi_device *spi) data = data &0x1B ;regmap_write (map,DS1347_STATUS_REG , data); - rtc =devm_rtc_device_register (&spi->dev,"ds1347" , - &ds1347_rtc_ops,THIS_MODULE ); - + rtc =devm_rtc_allocate_device (&spi->dev);if (IS_ERR (rtc))return PTR_ERR (rtc); -return 0 ; + rtc->ops = &ds1347_rtc_ops; + +return rtc_register_device (rtc); }static struct spi_driver ds1347_driver = {
Signature · 1 line
2.21.0
Alexandre Belloni
maintainerPATCH 7/10 rtc: ds1347: set range
In reply to Alexandre BelloniThe DS1347 handle dates from year 0000 to 9999. Leap years are claimed to be handled correctly in the datasheet.
--- drivers/rtc/rtc-ds1347.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/rtc/rtc-ds1347.c +2 -0
@@ -134,6 +134,8 @@ static int ds1347_probe(struct spi_device *spi)return PTR_ERR (rtc); rtc->ops = &ds1347_rtc_ops; + rtc->range_min =RTC_TIMESTAMP_BEGIN_0000 ; + rtc->range_max =RTC_TIMESTAMP_END_9999 ;return rtc_register_device (rtc); }
Signature · 1 line
2.21.0
Alexandre Belloni
maintainerPATCH 8/10 rtc: ds1347: properly handle oscillator failures
In reply to Alexandre BelloniThe comment in the probe function stating that it disables oscillator stop detection and glitch filtering is incorrect as it sets bits 3 and 4 while it should be setting 5 and 6 to achieve that. Then, it is safe to assume that the oscillator failure detection is actually enabled.
Properly handle oscillator failures by returning -EINVAL when the time and date are know to be incorrect and reset the flag when the time is set.
--- drivers/rtc/rtc-ds1347.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-)
drivers/rtc/rtc-ds1347.c +23 -7
@@ -29,6 +29,9 @@#define DS1347_STATUS_REG 0x17#define DS1347_CLOCK_BURST 0x3F +#define DS1347_NEOSC_BIT BIT(7) +#define DS1347_OSF_BIT BIT(2) +static const struct regmap_range ds1347_ranges[] = { { .range_min =DS1347_SECONDS_REG ,
@@ -44,9 +47,17 @@ static const struct regmap_access_table ds1347_access_table = {static int ds1347_read_time (struct device *dev,struct rtc_time *dt) {struct regmap *map =dev_get_drvdata (dev); +unsigned int status;int err;unsigned char buf[8 ]; + err =regmap_read (map,DS1347_STATUS_REG , &status); +if (err) +return err; + +if (status &DS1347_OSF_BIT ) +return -EINVAL ; + err =regmap_bulk_read (map,DS1347_CLOCK_BURST , buf,8 );if (err)return err;
@@ -66,6 +77,12 @@ static int ds1347_set_time(struct device *dev, struct rtc_time *dt) {struct regmap *map =dev_get_drvdata (dev);unsigned char buf[8 ]; +int err; + + err =regmap_update_bits (map,DS1347_STATUS_REG , +DS1347_NEOSC_BIT ,DS1347_NEOSC_BIT ); +if (err) +return err; buf[0 ] =bin2bcd (dt->tm_sec); buf[1 ] =bin2bcd (dt->tm_min);
@@ -82,7 +99,12 @@ static int ds1347_set_time(struct device *dev, struct rtc_time *dt) buf[7 ] =bin2bcd (0x00 );/* write the rtc settings */ -return regmap_bulk_write (map,DS1347_CLOCK_BURST , buf,8 ); + err =regmap_bulk_write (map,DS1347_CLOCK_BURST , buf,8 ); +if (err) +return err; + +return regmap_update_bits (map,DS1347_STATUS_REG , +DS1347_NEOSC_BIT |DS1347_OSF_BIT ,0 ); }static const struct rtc_class_ops ds1347_rtc_ops = {
@@ -123,12 +145,6 @@ static int ds1347_probe(struct spi_device *spi) data = data & ~(1 <<7 );regmap_write (map,DS1347_CONTROL_REG , data); -/* Enable the oscillator , disable the oscillator stop flag, -and glitch filter to reduce current consumption */ -regmap_read (map,DS1347_STATUS_REG , &data); - data = data &0x1B ; -regmap_write (map,DS1347_STATUS_REG , data); - rtc =devm_rtc_allocate_device (&spi->dev);if (IS_ERR (rtc))return PTR_ERR (rtc);
Signature · 1 line
2.21.0
Alexandre Belloni
maintainerPATCH 9/10 rtc: ds1347: use regmap_update_bits
In reply to Alexandre BelloniUse regmap_update_bits instead of open coding. Also add proper error handling.
--- drivers/rtc/rtc-ds1347.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/rtc/rtc-ds1347.c +6 -4
@@ -29,6 +29,8 @@#define DS1347_STATUS_REG 0x17#define DS1347_CLOCK_BURST 0x3F +#define DS1347_WP_BIT BIT(7) +#define DS1347_NEOSC_BIT BIT(7)#define DS1347_OSF_BIT BIT(2)
@@ -117,7 +119,7 @@ static int ds1347_probe(struct spi_device *spi)struct rtc_device *rtc;struct regmap_config config;struct regmap *map; -unsigned int data; +int err;memset (&config,0 ,sizeof (config)); config.reg_bits =8 ;
@@ -141,9 +143,9 @@ static int ds1347_probe(struct spi_device *spi)spi_set_drvdata (spi, map);/* Disable the write protect of rtc */ -regmap_read (map,DS1347_CONTROL_REG , &data); - data = data & ~(1 <<7 ); -regmap_write (map,DS1347_CONTROL_REG , data); + err =regmap_update_bits (map,DS1347_CONTROL_REG ,DS1347_WP_BIT ,0 ); +if (err) +return err; rtc =devm_rtc_allocate_device (&spi->dev);if (IS_ERR (rtc))
Signature · 1 line
2.21.0
Alexandre Belloni
maintainerPATCH 10/10 rtc: ds1347: handle century register
In reply to Alexandre BelloniThe DS1347 can handle years from 0 to 9999, add century register support.
--- drivers/rtc/rtc-ds1347.c | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-)
drivers/rtc/rtc-ds1347.c +24 -13
@@ -26,6 +26,7 @@#define DS1347_DAY_REG 0x0B#define DS1347_YEAR_REG 0x0D#define DS1347_CONTROL_REG 0x0F +#define DS1347_CENTURY_REG 0x13#define DS1347_STATUS_REG 0x17#define DS1347_CLOCK_BURST 0x3F
@@ -49,9 +50,9 @@ static const struct regmap_access_table ds1347_access_table = {static int ds1347_read_time (struct device *dev,struct rtc_time *dt) {struct regmap *map =dev_get_drvdata (dev); -unsigned int status; -int err; +unsigned int status, century, secs;unsigned char buf[8 ]; +int err; err =regmap_read (map,DS1347_STATUS_REG , &status);if (err)
@@ -60,9 +61,19 @@ static int ds1347_read_time(struct device *dev, struct rtc_time *dt)if (status &DS1347_OSF_BIT )return -EINVAL ; - err =regmap_bulk_read (map,DS1347_CLOCK_BURST , buf,8 ); -if (err) -return err; +do { + err =regmap_bulk_read (map,DS1347_CLOCK_BURST , buf,8 ); +if (err) +return err; + + err =regmap_read (map,DS1347_CENTURY_REG , ¢ury); +if (err) +return err; + + err =regmap_read (map,DS1347_SECONDS_REG , &secs); +if (err) +return err; + }while (buf[0 ] != secs); dt->tm_sec =bcd2bin (buf[0 ]); dt->tm_min =bcd2bin (buf[1 ] &0x7f );
@@ -70,7 +81,7 @@ static int ds1347_read_time(struct device *dev, struct rtc_time *dt) dt->tm_mday =bcd2bin (buf[3 ]); dt->tm_mon =bcd2bin (buf[4 ]) -1 ; dt->tm_wday =bcd2bin (buf[5 ]) -1 ; - dt->tm_year =bcd2bin (buf[6 ]) +100 ; + dt->tm_year = (bcd2bin (century) *100 ) +bcd2bin (buf[6 ]) -1900 ;return 0 ; }
@@ -78,6 +89,7 @@ static int ds1347_read_time(struct device *dev, struct rtc_time *dt)static int ds1347_set_time (struct device *dev,struct rtc_time *dt) {struct regmap *map =dev_get_drvdata (dev); +unsigned int century;unsigned char buf[8 ];int err;
@@ -92,19 +104,18 @@ static int ds1347_set_time(struct device *dev, struct rtc_time *dt) buf[3 ] =bin2bcd (dt->tm_mday); buf[4 ] =bin2bcd (dt->tm_mon +1 ); buf[5 ] =bin2bcd (dt->tm_wday +1 ); - -/* year in linux is from 1900 i.e in range of 100 -in rtc it is from 00 to 99 */ - dt->tm_year = dt->tm_year %100 ; - - buf[6 ] =bin2bcd (dt->tm_year); + buf[6 ] =bin2bcd (dt->tm_year %100 ); buf[7 ] =bin2bcd (0x00 ); -/* write the rtc settings */ err =regmap_bulk_write (map,DS1347_CLOCK_BURST , buf,8 );if (err)return err; + century = (dt->tm_year /100 ) +19 ; + err =regmap_write (map,DS1347_CENTURY_REG , century); +if (err) +return err; +return regmap_update_bits (map,DS1347_STATUS_REG ,DS1347_NEOSC_BIT |DS1347_OSF_BIT ,0 ); }
Signature · 1 line
2.21.0