| 1 | |
| 2 | |
| 3 | * An I2C driver for the Epson RX8581 RTC
|
| 4 | *
|
| 5 | * Author: Martyn Welch <[email protected]>
|
| 6 | * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
|
| 7 | *
|
| 8 | * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC)
|
| 9 | * Copyright 2005-06 Tower Technologies
|
| 10 |
|
| 11 |
|
| 12 | #include <linux/module.h>
|
| 13 | #include <linux/i2c.h>
|
| 14 | #include <linux/bcd.h>
|
| 15 | #include <linux/of.h>
|
| 16 | #include <linux/regmap.h>
|
| 17 | #include <linux/rtc.h>
|
| 18 | #include <linux/log2.h>
|
| 19 |
|
| 20 | #define RX8581_REG_SC 0x00
|
| 21 | #define RX8581_REG_MN 0x01
|
| 22 | #define RX8581_REG_HR 0x02
|
| 23 | #define RX8581_REG_DW 0x03
|
| 24 | #define RX8581_REG_DM 0x04
|
| 25 | #define RX8581_REG_MO 0x05
|
| 26 | #define RX8581_REG_YR 0x06
|
| 27 | #define RX8581_REG_RAM 0x07
|
| 28 | #define RX8581_REG_AMN 0x08
|
| 29 | #define RX8581_REG_AHR 0x09
|
| 30 | #define RX8581_REG_ADM 0x0A
|
| 31 | #define RX8581_REG_ADW 0x0A
|
| 32 | #define RX8581_REG_TMR0 0x0B
|
| 33 | #define RX8581_REG_TMR1 0x0C
|
| 34 | #define RX8581_REG_EXT 0x0D
|
| 35 | #define RX8581_REG_FLAG 0x0E
|
| 36 | #define RX8581_REG_CTRL 0x0F
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 | #define RX8581_FLAG_UF 0x20
|
| 41 | #define RX8581_FLAG_TF 0x10
|
| 42 | #define RX8581_FLAG_AF 0x08
|
| 43 | #define RX8581_FLAG_VLF 0x02
|
| 44 |
|
| 45 |
|
| 46 | #define RX8581_CTRL_UIE 0x20
|
| 47 | #define RX8581_CTRL_TIE 0x10
|
| 48 | #define RX8581_CTRL_AIE 0x08
|
| 49 | #define RX8581_CTRL_STOP 0x02
|
| 50 | #define RX8581_CTRL_RESET 0x01
|
| 51 |
|
| 52 | #define RX8571_USER_RAM 0x10
|
| 53 | #define RX8571_NVRAM_SIZE 0x10
|
| 54 |
|
| 55 | struct rx85x1_config {
|
| 56 | struct regmap_config regmap;
|
| 57 | unsigned int num_nvram;
|
| 58 | };
|
| 59 |
|
| 60 | |
| 61 | * In the routines that deal directly with the rx8581 hardware, we use
|
| 62 | * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
|
| 63 |
|
| 64 | static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
|
| 65 | {
|
| 66 | struct i2c_client *client = to_i2c_client(dev);
|
| 67 | unsigned char date[7];
|
| 68 | unsigned int data;
|
| 69 | int err;
|
| 70 | struct regmap *regmap = i2c_get_clientdata(client);
|
| 71 |
|
| 72 | |
| 73 | * time and date then re-read the "update flag". If the update flag
|
| 74 | * has been set, we know that the time has changed during the read so
|
| 75 | * we repeat the whole process again.
|
| 76 |
|
| 77 | err = regmap_read(regmap, RX8581_REG_FLAG, &data);
|
| 78 | if (err < 0)
|
| 79 | return err;
|
| 80 |
|
| 81 | if (data & RX8581_FLAG_VLF) {
|
| 82 | dev_warn(dev,
|
| 83 | "low voltage detected, date/time is not reliable.\n");
|
| 84 | return -EINVAL;
|
| 85 | }
|
| 86 |
|
| 87 | do {
|
| 88 |
|
| 89 | if (data & RX8581_FLAG_UF) {
|
| 90 | err = regmap_write(regmap, RX8581_REG_FLAG,
|
| 91 | data & ~RX8581_FLAG_UF);
|
| 92 | if (err < 0)
|
| 93 | return err;
|
| 94 | }
|
| 95 |
|
| 96 |
|
| 97 | err = regmap_bulk_read(regmap, RX8581_REG_SC, date,
|
| 98 | sizeof(date));
|
| 99 | if (err < 0)
|
| 100 | return err;
|
| 101 |
|
| 102 |
|
| 103 | err = regmap_read(regmap, RX8581_REG_FLAG, &data);
|
| 104 | if (err < 0)
|
| 105 | return err;
|
| 106 | } while (data & RX8581_FLAG_UF);
|
| 107 |
|
| 108 | dev_dbg(dev, "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
|
| 109 | "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
|
| 110 | __func__,
|
| 111 | date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
|
| 112 |
|
| 113 | tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
|
| 114 | tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
|
| 115 | tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F);
|
| 116 | tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
|
| 117 | tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
|
| 118 | tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1;
|
| 119 | tm->tm_year = bcd2bin(date[RX8581_REG_YR]) + 100;
|
| 120 |
|
| 121 | dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
|
| 122 | "mday=%d, mon=%d, year=%d, wday=%d\n",
|
| 123 | __func__,
|
| 124 | tm->tm_sec, tm->tm_min, tm->tm_hour,
|
| 125 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
|
| 126 |
|
| 127 | return 0;
|
| 128 | }
|
| 129 |
|
| 130 | static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
|
| 131 | {
|
| 132 | struct i2c_client *client = to_i2c_client(dev);
|
| 133 | int err;
|
| 134 | unsigned char buf[7];
|
| 135 | struct regmap *regmap = i2c_get_clientdata(client);
|
| 136 |
|
| 137 | dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
|
| 138 | "mday=%d, mon=%d, year=%d, wday=%d\n",
|
| 139 | __func__,
|
| 140 | tm->tm_sec, tm->tm_min, tm->tm_hour,
|
| 141 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
|
| 142 |
|
| 143 |
|
| 144 | buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
|
| 145 | buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
|
| 146 | buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
|
| 147 |
|
| 148 | buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
|
| 149 |
|
| 150 |
|
| 151 | buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
|
| 152 |
|
| 153 |
|
| 154 | buf[RX8581_REG_YR] = bin2bcd(tm->tm_year - 100);
|
| 155 | buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
|
| 156 |
|
| 157 |
|
| 158 | err = regmap_update_bits(regmap, RX8581_REG_CTRL,
|
| 159 | RX8581_CTRL_STOP, RX8581_CTRL_STOP);
|
| 160 | if (err < 0)
|
| 161 | return err;
|
| 162 |
|
| 163 |
|
| 164 | err = regmap_bulk_write(regmap, RX8581_REG_SC, buf, sizeof(buf));
|
| 165 | if (err < 0)
|
| 166 | return err;
|
| 167 |
|
| 168 |
|
| 169 | err = regmap_update_bits(regmap, RX8581_REG_FLAG, RX8581_FLAG_VLF, 0);
|
| 170 | if (err < 0)
|
| 171 | return err;
|
| 172 |
|
| 173 |
|
| 174 | return regmap_update_bits(regmap, RX8581_REG_CTRL,
|
| 175 | RX8581_CTRL_STOP, 0);
|
| 176 | }
|
| 177 |
|
| 178 | static const struct rtc_class_ops rx8581_rtc_ops = {
|
| 179 | .read_time = rx8581_rtc_read_time,
|
| 180 | .set_time = rx8581_rtc_set_time,
|
| 181 | };
|
| 182 |
|
| 183 | static int rx8571_nvram_read(void *priv, unsigned int offset, void *val,
|
| 184 | size_t bytes)
|
| 185 | {
|
| 186 | struct regmap *regmap = priv;
|
| 187 |
|
| 188 | return regmap_bulk_read(regmap, RX8571_USER_RAM + offset, val, bytes);
|
| 189 | }
|
| 190 |
|
| 191 | static int rx8571_nvram_write(void *priv, unsigned int offset, void *val,
|
| 192 | size_t bytes)
|
| 193 | {
|
| 194 | struct regmap *regmap = priv;
|
| 195 |
|
| 196 | return regmap_bulk_write(regmap, RX8571_USER_RAM + offset, val, bytes);
|
| 197 | }
|
| 198 |
|
| 199 | static int rx85x1_nvram_read(void *priv, unsigned int offset, void *val,
|
| 200 | size_t bytes)
|
| 201 | {
|
| 202 | struct regmap *regmap = priv;
|
| 203 | unsigned int tmp_val;
|
| 204 | int ret;
|
| 205 |
|
| 206 | ret = regmap_read(regmap, RX8581_REG_RAM, &tmp_val);
|
| 207 | (*(unsigned char *)val) = (unsigned char) tmp_val;
|
| 208 |
|
| 209 | return ret;
|
| 210 | }
|
| 211 |
|
| 212 | static int rx85x1_nvram_write(void *priv, unsigned int offset, void *val,
|
| 213 | size_t bytes)
|
| 214 | {
|
| 215 | struct regmap *regmap = priv;
|
| 216 | unsigned char tmp_val;
|
| 217 |
|
| 218 | tmp_val = *((unsigned char *)val);
|
| 219 | return regmap_write(regmap, RX8581_REG_RAM, (unsigned int)tmp_val);
|
| 220 | }
|
| 221 |
|
| 222 | static const struct rx85x1_config rx8581_config = {
|
| 223 | .regmap = {
|
| 224 | .reg_bits = 8,
|
| 225 | .val_bits = 8,
|
| 226 | .max_register = 0xf,
|
| 227 | },
|
| 228 | .num_nvram = 1
|
| 229 | };
|
| 230 |
|
| 231 | static const struct rx85x1_config rx8571_config = {
|
| 232 | .regmap = {
|
| 233 | .reg_bits = 8,
|
| 234 | .val_bits = 8,
|
| 235 | .max_register = 0x1f,
|
| 236 | },
|
| 237 | .num_nvram = 2
|
| 238 | };
|
| 239 |
|
| 240 | static int rx8581_probe(struct i2c_client *client)
|
| 241 | {
|
| 242 | struct regmap *regmap;
|
| 243 | const struct rx85x1_config *config = &rx8581_config;
|
| 244 | const void *data = of_device_get_match_data(&client->dev);
|
| 245 | struct rtc_device *rtc;
|
| 246 | static struct nvmem_config nvmem_cfg[] = {
|
| 247 | {
|
| 248 | .name = "rx85x1-",
|
| 249 | .word_size = 1,
|
| 250 | .stride = 1,
|
| 251 | .size = 1,
|
| 252 | .reg_read = rx85x1_nvram_read,
|
| 253 | .reg_write = rx85x1_nvram_write,
|
| 254 | }, {
|
| 255 | .name = "rx8571-",
|
| 256 | .word_size = 1,
|
| 257 | .stride = 1,
|
| 258 | .size = RX8571_NVRAM_SIZE,
|
| 259 | .reg_read = rx8571_nvram_read,
|
| 260 | .reg_write = rx8571_nvram_write,
|
| 261 | },
|
| 262 | };
|
| 263 | int ret, i;
|
| 264 |
|
| 265 | dev_dbg(&client->dev, "%s\n", __func__);
|
| 266 |
|
| 267 | if (data)
|
| 268 | config = data;
|
| 269 |
|
| 270 | regmap = devm_regmap_init_i2c(client, &config->regmap);
|
| 271 | if (IS_ERR(regmap))
|
| 272 | return PTR_ERR(regmap);
|
| 273 |
|
| 274 | i2c_set_clientdata(client, regmap);
|
| 275 |
|
| 276 | rtc = devm_rtc_allocate_device(&client->dev);
|
| 277 | if (IS_ERR(rtc))
|
| 278 | return PTR_ERR(rtc);
|
| 279 |
|
| 280 | rtc->ops = &rx8581_rtc_ops;
|
| 281 | rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
|
| 282 | rtc->range_max = RTC_TIMESTAMP_END_2099;
|
| 283 | rtc->start_secs = 0;
|
| 284 | rtc->set_start_time = true;
|
| 285 |
|
| 286 | ret = devm_rtc_register_device(rtc);
|
| 287 |
|
| 288 | for (i = 0; i < config->num_nvram; i++) {
|
| 289 | nvmem_cfg[i].priv = regmap;
|
| 290 | devm_rtc_nvmem_register(rtc, &nvmem_cfg[i]);
|
| 291 | }
|
| 292 |
|
| 293 | return ret;
|
| 294 | }
|
| 295 |
|
| 296 | static const struct i2c_device_id rx8581_id[] = {
|
| 297 | { .name = "rx8581" },
|
| 298 | { }
|
| 299 | };
|
| 300 | MODULE_DEVICE_TABLE(i2c, rx8581_id);
|
| 301 |
|
| 302 | static const __maybe_unused struct of_device_id rx8581_of_match[] = {
|
| 303 | { .compatible = "epson,rx8571", .data = &rx8571_config },
|
| 304 | { .compatible = "epson,rx8581", .data = &rx8581_config },
|
| 305 | { }
|
| 306 | };
|
| 307 | MODULE_DEVICE_TABLE(of, rx8581_of_match);
|
| 308 |
|
| 309 | static struct i2c_driver rx8581_driver = {
|
| 310 | .driver = {
|
| 311 | .name = "rtc-rx8581",
|
| 312 | .of_match_table = of_match_ptr(rx8581_of_match),
|
| 313 | },
|
| 314 | .probe = rx8581_probe,
|
| 315 | .id_table = rx8581_id,
|
| 316 | };
|
| 317 |
|
| 318 | module_i2c_driver(rx8581_driver);
|
| 319 |
|
| 320 | MODULE_AUTHOR("Martyn Welch <[email protected]>");
|
| 321 | MODULE_DESCRIPTION("Epson RX-8571/RX-8581 RTC driver");
|
| 322 | MODULE_LICENSE("GPL");
|