Magnus Damm | 75b6102 | 2008-02-06 01:38:54 -0800 | [diff] [blame] | 1 | /* |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 2 | * Driver for Epson RTC-9701JE |
| 3 | * |
| 4 | * Copyright (C) 2008 Magnus Damm |
| 5 | * |
Magnus Damm | 75b6102 | 2008-02-06 01:38:54 -0800 | [diff] [blame] | 6 | * Based on rtc-max6902.c |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 7 | * |
| 8 | * Copyright (C) 2006 8D Technologies inc. |
| 9 | * Copyright (C) 2004 Compulab Ltd. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 17 | #include <linux/kernel.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/rtc.h> |
| 22 | #include <linux/spi/spi.h> |
| 23 | #include <linux/bcd.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/bitops.h> |
| 26 | |
| 27 | #define RSECCNT 0x00 /* Second Counter */ |
| 28 | #define RMINCNT 0x01 /* Minute Counter */ |
| 29 | #define RHRCNT 0x02 /* Hour Counter */ |
| 30 | #define RWKCNT 0x03 /* Week Counter */ |
| 31 | #define RDAYCNT 0x04 /* Day Counter */ |
| 32 | #define RMONCNT 0x05 /* Month Counter */ |
| 33 | #define RYRCNT 0x06 /* Year Counter */ |
| 34 | #define R100CNT 0x07 /* Y100 Counter */ |
| 35 | #define RMINAR 0x08 /* Minute Alarm */ |
| 36 | #define RHRAR 0x09 /* Hour Alarm */ |
| 37 | #define RWKAR 0x0a /* Week/Day Alarm */ |
| 38 | #define RTIMCNT 0x0c /* Interval Timer */ |
| 39 | #define REXT 0x0d /* Extension Register */ |
| 40 | #define RFLAG 0x0e /* RTC Flag Register */ |
| 41 | #define RCR 0x0f /* RTC Control Register */ |
| 42 | |
| 43 | static int write_reg(struct device *dev, int address, unsigned char data) |
| 44 | { |
| 45 | struct spi_device *spi = to_spi_device(dev); |
| 46 | unsigned char buf[2]; |
| 47 | |
| 48 | buf[0] = address & 0x7f; |
| 49 | buf[1] = data; |
| 50 | |
| 51 | return spi_write(spi, buf, ARRAY_SIZE(buf)); |
| 52 | } |
| 53 | |
| 54 | static int read_regs(struct device *dev, unsigned char *regs, int no_regs) |
| 55 | { |
| 56 | struct spi_device *spi = to_spi_device(dev); |
| 57 | u8 txbuf[1], rxbuf[1]; |
| 58 | int k, ret; |
| 59 | |
| 60 | ret = 0; |
| 61 | |
| 62 | for (k = 0; ret == 0 && k < no_regs; k++) { |
| 63 | txbuf[0] = 0x80 | regs[k]; |
| 64 | ret = spi_write_then_read(spi, txbuf, 1, rxbuf, 1); |
| 65 | regs[k] = rxbuf[0]; |
| 66 | } |
| 67 | |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | static int r9701_get_datetime(struct device *dev, struct rtc_time *dt) |
| 72 | { |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 73 | int ret; |
| 74 | unsigned char buf[] = { RSECCNT, RMINCNT, RHRCNT, |
| 75 | RDAYCNT, RMONCNT, RYRCNT }; |
| 76 | |
Magnus Damm | 75b6102 | 2008-02-06 01:38:54 -0800 | [diff] [blame] | 77 | ret = read_regs(dev, buf, ARRAY_SIZE(buf)); |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 78 | if (ret) |
| 79 | return ret; |
| 80 | |
| 81 | memset(dt, 0, sizeof(*dt)); |
| 82 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 83 | dt->tm_sec = bcd2bin(buf[0]); /* RSECCNT */ |
| 84 | dt->tm_min = bcd2bin(buf[1]); /* RMINCNT */ |
| 85 | dt->tm_hour = bcd2bin(buf[2]); /* RHRCNT */ |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 86 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 87 | dt->tm_mday = bcd2bin(buf[3]); /* RDAYCNT */ |
| 88 | dt->tm_mon = bcd2bin(buf[4]) - 1; /* RMONCNT */ |
| 89 | dt->tm_year = bcd2bin(buf[5]) + 100; /* RYRCNT */ |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 90 | |
| 91 | /* the rtc device may contain illegal values on power up |
| 92 | * according to the data sheet. make sure they are valid. |
| 93 | */ |
| 94 | |
Magnus Damm | 75b6102 | 2008-02-06 01:38:54 -0800 | [diff] [blame] | 95 | return rtc_valid_tm(dt); |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int r9701_set_datetime(struct device *dev, struct rtc_time *dt) |
| 99 | { |
| 100 | int ret, year; |
| 101 | |
| 102 | year = dt->tm_year + 1900; |
| 103 | if (year >= 2100 || year < 2000) |
| 104 | return -EINVAL; |
| 105 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 106 | ret = write_reg(dev, RHRCNT, bin2bcd(dt->tm_hour)); |
| 107 | ret = ret ? ret : write_reg(dev, RMINCNT, bin2bcd(dt->tm_min)); |
| 108 | ret = ret ? ret : write_reg(dev, RSECCNT, bin2bcd(dt->tm_sec)); |
| 109 | ret = ret ? ret : write_reg(dev, RDAYCNT, bin2bcd(dt->tm_mday)); |
| 110 | ret = ret ? ret : write_reg(dev, RMONCNT, bin2bcd(dt->tm_mon + 1)); |
| 111 | ret = ret ? ret : write_reg(dev, RYRCNT, bin2bcd(dt->tm_year - 100)); |
Magnus Damm | 75b6102 | 2008-02-06 01:38:54 -0800 | [diff] [blame] | 112 | ret = ret ? ret : write_reg(dev, RWKCNT, 1 << dt->tm_wday); |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static const struct rtc_class_ops r9701_rtc_ops = { |
| 118 | .read_time = r9701_get_datetime, |
| 119 | .set_time = r9701_set_datetime, |
| 120 | }; |
| 121 | |
| 122 | static int __devinit r9701_probe(struct spi_device *spi) |
| 123 | { |
| 124 | struct rtc_device *rtc; |
Andreas Dumberger | bb58da0 | 2012-04-12 12:49:12 -0700 | [diff] [blame] | 125 | struct rtc_time dt; |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 126 | unsigned char tmp; |
| 127 | int res; |
| 128 | |
Anatolij Gustschin | 73737b8 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 129 | tmp = R100CNT; |
| 130 | res = read_regs(&spi->dev, &tmp, 1); |
| 131 | if (res || tmp != 0x20) { |
| 132 | dev_err(&spi->dev, "cannot read RTC register\n"); |
| 133 | return -ENODEV; |
| 134 | } |
| 135 | |
Andreas Dumberger | bb58da0 | 2012-04-12 12:49:12 -0700 | [diff] [blame] | 136 | /* |
| 137 | * The device seems to be present. Now check if the registers |
| 138 | * contain invalid values. If so, try to write a default date: |
| 139 | * 2000/1/1 00:00:00 |
| 140 | */ |
Devendra Naga | aa11ec5 | 2012-07-30 14:41:45 -0700 | [diff] [blame] | 141 | if (r9701_get_datetime(&spi->dev, &dt)) { |
Andreas Dumberger | bb58da0 | 2012-04-12 12:49:12 -0700 | [diff] [blame] | 142 | dev_info(&spi->dev, "trying to repair invalid date/time\n"); |
| 143 | dt.tm_sec = 0; |
| 144 | dt.tm_min = 0; |
| 145 | dt.tm_hour = 0; |
| 146 | dt.tm_mday = 1; |
| 147 | dt.tm_mon = 0; |
| 148 | dt.tm_year = 100; |
| 149 | |
Andrew Morton | 8e74b6e | 2012-07-30 14:41:47 -0700 | [diff] [blame] | 150 | if (r9701_set_datetime(&spi->dev, &dt) || |
| 151 | r9701_get_datetime(&spi->dev, &dt)) { |
Andreas Dumberger | bb58da0 | 2012-04-12 12:49:12 -0700 | [diff] [blame] | 152 | dev_err(&spi->dev, "cannot repair RTC register\n"); |
| 153 | return -ENODEV; |
| 154 | } |
| 155 | } |
| 156 | |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 157 | rtc = rtc_device_register("r9701", |
| 158 | &spi->dev, &r9701_rtc_ops, THIS_MODULE); |
| 159 | if (IS_ERR(rtc)) |
| 160 | return PTR_ERR(rtc); |
| 161 | |
| 162 | dev_set_drvdata(&spi->dev, rtc); |
| 163 | |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int __devexit r9701_remove(struct spi_device *spi) |
| 168 | { |
| 169 | struct rtc_device *rtc = dev_get_drvdata(&spi->dev); |
| 170 | |
| 171 | rtc_device_unregister(rtc); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static struct spi_driver r9701_driver = { |
| 176 | .driver = { |
| 177 | .name = "rtc-r9701", |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 178 | .owner = THIS_MODULE, |
| 179 | }, |
| 180 | .probe = r9701_probe, |
| 181 | .remove = __devexit_p(r9701_remove), |
| 182 | }; |
| 183 | |
Axel Lin | 109e941 | 2012-03-23 15:02:30 -0700 | [diff] [blame] | 184 | module_spi_driver(r9701_driver); |
Magnus Damm | 2805b96 | 2008-02-06 01:38:53 -0800 | [diff] [blame] | 185 | |
| 186 | MODULE_DESCRIPTION("r9701 spi RTC driver"); |
| 187 | MODULE_AUTHOR("Magnus Damm <damm@opensource.se>"); |
| 188 | MODULE_LICENSE("GPL"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 189 | MODULE_ALIAS("spi:rtc-r9701"); |