| 1 | |
| 2 | |
| 3 | *
|
| 4 | * written by Torben Hohn <[email protected]>
|
| 5 | *
|
| 6 | * Based on:
|
| 7 | * drivers/rtc/rtc-max6902.c
|
| 8 | *
|
| 9 | * Copyright (C) 2006 8D Technologies inc.
|
| 10 | * Copyright (C) 2004 Compulab Ltd.
|
| 11 | *
|
| 12 | * Driver for MAX6902 spi RTC
|
| 13 | *
|
| 14 | * and based on:
|
| 15 | * drivers/rtc/rtc-rx8581.c
|
| 16 | *
|
| 17 | * An I2C driver for the Epson RX8581 RTC
|
| 18 | *
|
| 19 | * Author: Martyn Welch <[email protected]>
|
| 20 | * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
|
| 21 | *
|
| 22 | * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC)
|
| 23 | * Copyright 2005-06 Tower Technologies
|
| 24 |
|
| 25 |
|
| 26 | #include <linux/module.h>
|
| 27 | #include <linux/kernel.h>
|
| 28 | #include <linux/platform_device.h>
|
| 29 | #include <linux/init.h>
|
| 30 | #include <linux/rtc.h>
|
| 31 | #include <linux/spi/spi.h>
|
| 32 | #include <linux/bcd.h>
|
| 33 |
|
| 34 | #define RX4581_REG_SC 0x00
|
| 35 | #define RX4581_REG_MN 0x01
|
| 36 | #define RX4581_REG_HR 0x02
|
| 37 | #define RX4581_REG_DW 0x03
|
| 38 | #define RX4581_REG_DM 0x04
|
| 39 | #define RX4581_REG_MO 0x05
|
| 40 | #define RX4581_REG_YR 0x06
|
| 41 | #define RX4581_REG_RAM 0x07
|
| 42 | #define RX4581_REG_AMN 0x08
|
| 43 | #define RX4581_REG_AHR 0x09
|
| 44 | #define RX4581_REG_ADM 0x0A
|
| 45 | #define RX4581_REG_ADW 0x0A
|
| 46 | #define RX4581_REG_TMR0 0x0B
|
| 47 | #define RX4581_REG_TMR1 0x0C
|
| 48 | #define RX4581_REG_EXT 0x0D
|
| 49 | #define RX4581_REG_FLAG 0x0E
|
| 50 | #define RX4581_REG_CTRL 0x0F
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 | #define RX4581_FLAG_UF 0x20
|
| 55 | #define RX4581_FLAG_TF 0x10
|
| 56 | #define RX4581_FLAG_AF 0x08
|
| 57 | #define RX4581_FLAG_VLF 0x02
|
| 58 |
|
| 59 |
|
| 60 | #define RX4581_CTRL_UIE 0x20
|
| 61 | #define RX4581_CTRL_TIE 0x10
|
| 62 | #define RX4581_CTRL_AIE 0x08
|
| 63 | #define RX4581_CTRL_STOP 0x02
|
| 64 | #define RX4581_CTRL_RESET 0x01
|
| 65 |
|
| 66 | static int rx4581_set_reg(struct device *dev, unsigned char address,
|
| 67 | unsigned char data)
|
| 68 | {
|
| 69 | struct spi_device *spi = to_spi_device(dev);
|
| 70 | unsigned char buf[2];
|
| 71 |
|
| 72 |
|
| 73 | buf[0] = address & 0x0f;
|
| 74 | buf[1] = data;
|
| 75 |
|
| 76 | return spi_write_then_read(spi, buf, 2, NULL, 0);
|
| 77 | }
|
| 78 |
|
| 79 | static int rx4581_get_reg(struct device *dev, unsigned char address,
|
| 80 | unsigned char *data)
|
| 81 | {
|
| 82 | struct spi_device *spi = to_spi_device(dev);
|
| 83 |
|
| 84 |
|
| 85 | *data = address | 0x80;
|
| 86 |
|
| 87 | return spi_write_then_read(spi, data, 1, data, 1);
|
| 88 | }
|
| 89 |
|
| 90 | |
| 91 | * In the routines that deal directly with the rx8581 hardware, we use
|
| 92 | * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
|
| 93 |
|
| 94 | static int rx4581_get_datetime(struct device *dev, struct rtc_time *tm)
|
| 95 | {
|
| 96 | struct spi_device *spi = to_spi_device(dev);
|
| 97 | unsigned char date[7];
|
| 98 | unsigned char data;
|
| 99 | int err;
|
| 100 |
|
| 101 | |
| 102 | * time and date then re-read the "update flag". If the update flag
|
| 103 | * has been set, we know that the time has changed during the read so
|
| 104 | * we repeat the whole process again.
|
| 105 |
|
| 106 | err = rx4581_get_reg(dev, RX4581_REG_FLAG, &data);
|
| 107 | if (err != 0) {
|
| 108 | dev_err(dev, "Unable to read device flags\n");
|
| 109 | return -EIO;
|
| 110 | }
|
| 111 |
|
| 112 | do {
|
| 113 |
|
| 114 | if (data & RX4581_FLAG_UF) {
|
| 115 | err = rx4581_set_reg(dev,
|
| 116 | RX4581_REG_FLAG, (data & ~RX4581_FLAG_UF));
|
| 117 | if (err != 0) {
|
| 118 | dev_err(dev, "Unable to write device "
|
| 119 | "flags\n");
|
| 120 | return -EIO;
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 |
|
| 125 | date[0] = 0x80;
|
| 126 | err = spi_write_then_read(spi, date, 1, date, 7);
|
| 127 | if (err < 0) {
|
| 128 | dev_err(dev, "Unable to read date\n");
|
| 129 | return -EIO;
|
| 130 | }
|
| 131 |
|
| 132 |
|
| 133 | err = rx4581_get_reg(dev, RX4581_REG_FLAG, &data);
|
| 134 | if (err != 0) {
|
| 135 | dev_err(dev, "Unable to read device flags\n");
|
| 136 | return -EIO;
|
| 137 | }
|
| 138 | } while (data & RX4581_FLAG_UF);
|
| 139 |
|
| 140 | if (data & RX4581_FLAG_VLF)
|
| 141 | dev_info(dev,
|
| 142 | "low voltage detected, date/time is not reliable.\n");
|
| 143 |
|
| 144 | dev_dbg(dev,
|
| 145 | "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
|
| 146 | "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
|
| 147 | __func__,
|
| 148 | date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
|
| 149 |
|
| 150 | tm->tm_sec = bcd2bin(date[RX4581_REG_SC] & 0x7F);
|
| 151 | tm->tm_min = bcd2bin(date[RX4581_REG_MN] & 0x7F);
|
| 152 | tm->tm_hour = bcd2bin(date[RX4581_REG_HR] & 0x3F);
|
| 153 | tm->tm_wday = ilog2(date[RX4581_REG_DW] & 0x7F);
|
| 154 | tm->tm_mday = bcd2bin(date[RX4581_REG_DM] & 0x3F);
|
| 155 | tm->tm_mon = bcd2bin(date[RX4581_REG_MO] & 0x1F) - 1;
|
| 156 | tm->tm_year = bcd2bin(date[RX4581_REG_YR]);
|
| 157 | if (tm->tm_year < 70)
|
| 158 | tm->tm_year += 100;
|
| 159 |
|
| 160 |
|
| 161 | dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
|
| 162 | "mday=%d, mon=%d, year=%d, wday=%d\n",
|
| 163 | __func__,
|
| 164 | tm->tm_sec, tm->tm_min, tm->tm_hour,
|
| 165 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
|
| 166 |
|
| 167 | return 0;
|
| 168 | }
|
| 169 |
|
| 170 | static int rx4581_set_datetime(struct device *dev, struct rtc_time *tm)
|
| 171 | {
|
| 172 | struct spi_device *spi = to_spi_device(dev);
|
| 173 | int err;
|
| 174 | unsigned char buf[8], data;
|
| 175 |
|
| 176 | dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
|
| 177 | "mday=%d, mon=%d, year=%d, wday=%d\n",
|
| 178 | __func__,
|
| 179 | tm->tm_sec, tm->tm_min, tm->tm_hour,
|
| 180 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
|
| 181 |
|
| 182 | buf[0] = 0x00;
|
| 183 |
|
| 184 | buf[RX4581_REG_SC+1] = bin2bcd(tm->tm_sec);
|
| 185 | buf[RX4581_REG_MN+1] = bin2bcd(tm->tm_min);
|
| 186 | buf[RX4581_REG_HR+1] = bin2bcd(tm->tm_hour);
|
| 187 |
|
| 188 | buf[RX4581_REG_DM+1] = bin2bcd(tm->tm_mday);
|
| 189 |
|
| 190 |
|
| 191 | buf[RX4581_REG_MO+1] = bin2bcd(tm->tm_mon + 1);
|
| 192 |
|
| 193 |
|
| 194 | buf[RX4581_REG_YR+1] = bin2bcd(tm->tm_year % 100);
|
| 195 | buf[RX4581_REG_DW+1] = (0x1 << tm->tm_wday);
|
| 196 |
|
| 197 |
|
| 198 | err = rx4581_get_reg(dev, RX4581_REG_CTRL, &data);
|
| 199 | if (err != 0) {
|
| 200 | dev_err(dev, "Unable to read control register\n");
|
| 201 | return -EIO;
|
| 202 | }
|
| 203 |
|
| 204 | err = rx4581_set_reg(dev, RX4581_REG_CTRL,
|
| 205 | (data | RX4581_CTRL_STOP));
|
| 206 | if (err != 0) {
|
| 207 | dev_err(dev, "Unable to write control register\n");
|
| 208 | return -EIO;
|
| 209 | }
|
| 210 |
|
| 211 |
|
| 212 | err = spi_write_then_read(spi, buf, 8, NULL, 0);
|
| 213 | if (err != 0) {
|
| 214 | dev_err(dev, "Unable to write to date registers\n");
|
| 215 | return -EIO;
|
| 216 | }
|
| 217 |
|
| 218 |
|
| 219 | err = rx4581_get_reg(dev, RX4581_REG_FLAG, &data);
|
| 220 | if (err != 0) {
|
| 221 | dev_err(dev, "Unable to read flag register\n");
|
| 222 | return -EIO;
|
| 223 | }
|
| 224 |
|
| 225 | err = rx4581_set_reg(dev, RX4581_REG_FLAG,
|
| 226 | (data & ~(RX4581_FLAG_VLF)));
|
| 227 | if (err != 0) {
|
| 228 | dev_err(dev, "Unable to write flag register\n");
|
| 229 | return -EIO;
|
| 230 | }
|
| 231 |
|
| 232 |
|
| 233 | err = rx4581_get_reg(dev, RX4581_REG_CTRL, &data);
|
| 234 | if (err != 0) {
|
| 235 | dev_err(dev, "Unable to read control register\n");
|
| 236 | return -EIO;
|
| 237 | }
|
| 238 |
|
| 239 | err = rx4581_set_reg(dev, RX4581_REG_CTRL,
|
| 240 | (data & ~(RX4581_CTRL_STOP)));
|
| 241 | if (err != 0) {
|
| 242 | dev_err(dev, "Unable to write control register\n");
|
| 243 | return -EIO;
|
| 244 | }
|
| 245 |
|
| 246 | return 0;
|
| 247 | }
|
| 248 |
|
| 249 | static const struct rtc_class_ops rx4581_rtc_ops = {
|
| 250 | .read_time = rx4581_get_datetime,
|
| 251 | .set_time = rx4581_set_datetime,
|
| 252 | };
|
| 253 |
|
| 254 | static int rx4581_probe(struct spi_device *spi)
|
| 255 | {
|
| 256 | struct rtc_device *rtc;
|
| 257 | unsigned char tmp;
|
| 258 | int res;
|
| 259 |
|
| 260 | res = rx4581_get_reg(&spi->dev, RX4581_REG_SC, &tmp);
|
| 261 | if (res != 0)
|
| 262 | return res;
|
| 263 |
|
| 264 | rtc = devm_rtc_device_register(&spi->dev, "rx4581",
|
| 265 | &rx4581_rtc_ops, THIS_MODULE);
|
| 266 | if (IS_ERR(rtc))
|
| 267 | return PTR_ERR(rtc);
|
| 268 |
|
| 269 | spi_set_drvdata(spi, rtc);
|
| 270 | return 0;
|
| 271 | }
|
| 272 |
|
| 273 | static const struct spi_device_id rx4581_id[] = {
|
| 274 | { "rx4581", 0 },
|
| 275 | { }
|
| 276 | };
|
| 277 | MODULE_DEVICE_TABLE(spi, rx4581_id);
|
| 278 |
|
| 279 | static struct spi_driver rx4581_driver = {
|
| 280 | .driver = {
|
| 281 | .name = "rtc-rx4581",
|
| 282 | },
|
| 283 | .probe = rx4581_probe,
|
| 284 | .id_table = rx4581_id,
|
| 285 | };
|
| 286 |
|
| 287 | module_spi_driver(rx4581_driver);
|
| 288 |
|
| 289 | MODULE_DESCRIPTION("rx4581 spi RTC driver");
|
| 290 | MODULE_AUTHOR("Torben Hohn");
|
| 291 | MODULE_LICENSE("GPL");
|
| 292 | MODULE_ALIAS("spi:rtc-rx4581");
|