Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Magnus Damm | 75b6102 | 2008-02-06 01:38:54 -0800 | [diff] [blame] | 2 | /* |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 3 | * Driver for Epson RTC-9701JE |
| 4 | * |
| 5 | * Copyright (C) 2008 Magnus Damm |
| 6 | * |
Magnus Damm | 75b6102 | 2008-02-06 01:38:54 -0800 | [diff] [blame] | 7 | * Based on rtc-max6902.c |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 8 | * |
| 9 | * Copyright (C) 2006 8D Technologies inc. |
| 10 | * Copyright (C) 2004 Compulab Ltd. |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/rtc.h> |
| 19 | #include <linux/spi/spi.h> |
| 20 | #include <linux/bcd.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/bitops.h> |
| 23 | |
| 24 | #define RSECCNT 0x00 /* Second Counter */ |
| 25 | #define RMINCNT 0x01 /* Minute Counter */ |
| 26 | #define RHRCNT 0x02 /* Hour Counter */ |
| 27 | #define RWKCNT 0x03 /* Week Counter */ |
| 28 | #define RDAYCNT 0x04 /* Day Counter */ |
| 29 | #define RMONCNT 0x05 /* Month Counter */ |
| 30 | #define RYRCNT 0x06 /* Year Counter */ |
| 31 | #define R100CNT 0x07 /* Y100 Counter */ |
| 32 | #define RMINAR 0x08 /* Minute Alarm */ |
| 33 | #define RHRAR 0x09 /* Hour Alarm */ |
| 34 | #define RWKAR 0x0a /* Week/Day Alarm */ |
| 35 | #define RTIMCNT 0x0c /* Interval Timer */ |
| 36 | #define REXT 0x0d /* Extension Register */ |
| 37 | #define RFLAG 0x0e /* RTC Flag Register */ |
| 38 | #define RCR 0x0f /* RTC Control Register */ |
| 39 | |
| 40 | static int write_reg(struct device *dev, int address, unsigned char data) |
| 41 | { |
| 42 | struct spi_device *spi = to_spi_device(dev); |
| 43 | unsigned char buf[2]; |
| 44 | |
| 45 | buf[0] = address & 0x7f; |
| 46 | buf[1] = data; |
| 47 | |
| 48 | return spi_write(spi, buf, ARRAY_SIZE(buf)); |
| 49 | } |
| 50 | |
| 51 | static int read_regs(struct device *dev, unsigned char *regs, int no_regs) |
| 52 | { |
| 53 | struct spi_device *spi = to_spi_device(dev); |
| 54 | u8 txbuf[1], rxbuf[1]; |
| 55 | int k, ret; |
| 56 | |
| 57 | ret = 0; |
| 58 | |
| 59 | for (k = 0; ret == 0 && k < no_regs; k++) { |
| 60 | txbuf[0] = 0x80 | regs[k]; |
| 61 | ret = spi_write_then_read(spi, txbuf, 1, rxbuf, 1); |
| 62 | regs[k] = rxbuf[0]; |
| 63 | } |
| 64 | |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | static int r9701_get_datetime(struct device *dev, struct rtc_time *dt) |
| 69 | { |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 70 | int ret; |
| 71 | unsigned char buf[] = { RSECCNT, RMINCNT, RHRCNT, |
| 72 | RDAYCNT, RMONCNT, RYRCNT }; |
| 73 | |
Magnus Damm | 75b6102 | 2008-02-06 01:38:54 -0800 | [diff] [blame] | 74 | ret = read_regs(dev, buf, ARRAY_SIZE(buf)); |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 75 | if (ret) |
| 76 | return ret; |
| 77 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 78 | dt->tm_sec = bcd2bin(buf[0]); /* RSECCNT */ |
| 79 | dt->tm_min = bcd2bin(buf[1]); /* RMINCNT */ |
| 80 | dt->tm_hour = bcd2bin(buf[2]); /* RHRCNT */ |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 81 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 82 | dt->tm_mday = bcd2bin(buf[3]); /* RDAYCNT */ |
| 83 | dt->tm_mon = bcd2bin(buf[4]) - 1; /* RMONCNT */ |
| 84 | dt->tm_year = bcd2bin(buf[5]) + 100; /* RYRCNT */ |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 85 | |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 86 | return 0; |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static int r9701_set_datetime(struct device *dev, struct rtc_time *dt) |
| 90 | { |
Alexandre Belloni | 35331b5 | 2020-10-15 21:11:35 +0200 | [diff] [blame] | 91 | int ret; |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 92 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 93 | ret = write_reg(dev, RHRCNT, bin2bcd(dt->tm_hour)); |
| 94 | ret = ret ? ret : write_reg(dev, RMINCNT, bin2bcd(dt->tm_min)); |
| 95 | ret = ret ? ret : write_reg(dev, RSECCNT, bin2bcd(dt->tm_sec)); |
| 96 | ret = ret ? ret : write_reg(dev, RDAYCNT, bin2bcd(dt->tm_mday)); |
| 97 | ret = ret ? ret : write_reg(dev, RMONCNT, bin2bcd(dt->tm_mon + 1)); |
| 98 | ret = ret ? ret : write_reg(dev, RYRCNT, bin2bcd(dt->tm_year - 100)); |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 99 | |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | static const struct rtc_class_ops r9701_rtc_ops = { |
| 104 | .read_time = r9701_get_datetime, |
| 105 | .set_time = r9701_set_datetime, |
| 106 | }; |
| 107 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 108 | static int r9701_probe(struct spi_device *spi) |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 109 | { |
| 110 | struct rtc_device *rtc; |
| 111 | unsigned char tmp; |
| 112 | int res; |
| 113 | |
Anatolij Gustschin | 73737b8 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 114 | tmp = R100CNT; |
| 115 | res = read_regs(&spi->dev, &tmp, 1); |
| 116 | if (res || tmp != 0x20) { |
| 117 | dev_err(&spi->dev, "cannot read RTC register\n"); |
| 118 | return -ENODEV; |
| 119 | } |
| 120 | |
Alexandre Belloni | dfe13cf | 2020-10-15 21:11:34 +0200 | [diff] [blame] | 121 | rtc = devm_rtc_allocate_device(&spi->dev); |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 122 | if (IS_ERR(rtc)) |
| 123 | return PTR_ERR(rtc); |
| 124 | |
Jingoo Han | fbf52c0 | 2013-04-29 16:20:19 -0700 | [diff] [blame] | 125 | spi_set_drvdata(spi, rtc); |
Alexandre Belloni | dfe13cf | 2020-10-15 21:11:34 +0200 | [diff] [blame] | 126 | rtc->ops = &r9701_rtc_ops; |
Alexandre Belloni | 35331b5 | 2020-10-15 21:11:35 +0200 | [diff] [blame] | 127 | rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; |
| 128 | rtc->range_max = RTC_TIMESTAMP_END_2099; |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 129 | |
Bartosz Golaszewski | fdcfd85 | 2020-11-09 17:34:08 +0100 | [diff] [blame] | 130 | return devm_rtc_register_device(rtc); |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 133 | static struct spi_driver r9701_driver = { |
| 134 | .driver = { |
| 135 | .name = "rtc-r9701", |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 136 | }, |
| 137 | .probe = r9701_probe, |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
Axel Lin | 109e941 | 2012-03-23 15:02:30 -0700 | [diff] [blame] | 140 | module_spi_driver(r9701_driver); |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 141 | |
| 142 | MODULE_DESCRIPTION("r9701 spi RTC driver"); |
| 143 | MODULE_AUTHOR("Magnus Damm <damm@opensource.se>"); |
| 144 | MODULE_LICENSE("GPL"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 145 | MODULE_ALIAS("spi:rtc-r9701"); |