Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * An I2C driver for the Epson RX8581 RTC |
| 3 | * |
Martyn Welch | 9756c4d | 2010-04-14 09:24:16 +0100 | [diff] [blame] | 4 | * Author: Martyn Welch <martyn.welch@ge.com> |
| 5 | * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc. |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC) |
| 12 | * Copyright 2005-06 Tower Technologies |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/bcd.h> |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 18 | #include <linux/regmap.h> |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 19 | #include <linux/rtc.h> |
| 20 | #include <linux/log2.h> |
| 21 | |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 22 | #define RX8581_REG_SC 0x00 /* Second in BCD */ |
| 23 | #define RX8581_REG_MN 0x01 /* Minute in BCD */ |
| 24 | #define RX8581_REG_HR 0x02 /* Hour in BCD */ |
| 25 | #define RX8581_REG_DW 0x03 /* Day of Week */ |
| 26 | #define RX8581_REG_DM 0x04 /* Day of Month in BCD */ |
| 27 | #define RX8581_REG_MO 0x05 /* Month in BCD */ |
| 28 | #define RX8581_REG_YR 0x06 /* Year in BCD */ |
| 29 | #define RX8581_REG_RAM 0x07 /* RAM */ |
| 30 | #define RX8581_REG_AMN 0x08 /* Alarm Min in BCD*/ |
| 31 | #define RX8581_REG_AHR 0x09 /* Alarm Hour in BCD */ |
| 32 | #define RX8581_REG_ADM 0x0A |
| 33 | #define RX8581_REG_ADW 0x0A |
| 34 | #define RX8581_REG_TMR0 0x0B |
| 35 | #define RX8581_REG_TMR1 0x0C |
| 36 | #define RX8581_REG_EXT 0x0D /* Extension Register */ |
| 37 | #define RX8581_REG_FLAG 0x0E /* Flag Register */ |
| 38 | #define RX8581_REG_CTRL 0x0F /* Control Register */ |
| 39 | |
| 40 | |
| 41 | /* Flag Register bit definitions */ |
| 42 | #define RX8581_FLAG_UF 0x20 /* Update */ |
| 43 | #define RX8581_FLAG_TF 0x10 /* Timer */ |
| 44 | #define RX8581_FLAG_AF 0x08 /* Alarm */ |
| 45 | #define RX8581_FLAG_VLF 0x02 /* Voltage Low */ |
| 46 | |
| 47 | /* Control Register bit definitions */ |
| 48 | #define RX8581_CTRL_UIE 0x20 /* Update Interrupt Enable */ |
| 49 | #define RX8581_CTRL_TIE 0x10 /* Timer Interrupt Enable */ |
| 50 | #define RX8581_CTRL_AIE 0x08 /* Alarm Interrupt Enable */ |
| 51 | #define RX8581_CTRL_STOP 0x02 /* STOP bit */ |
| 52 | #define RX8581_CTRL_RESET 0x01 /* RESET bit */ |
| 53 | |
Andreas Werner | d643a49 | 2014-01-23 15:55:20 -0800 | [diff] [blame] | 54 | struct rx8581 { |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 55 | struct regmap *regmap; |
Andreas Werner | d643a49 | 2014-01-23 15:55:20 -0800 | [diff] [blame] | 56 | struct rtc_device *rtc; |
Andreas Werner | d643a49 | 2014-01-23 15:55:20 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 59 | /* |
| 60 | * In the routines that deal directly with the rx8581 hardware, we use |
| 61 | * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. |
| 62 | */ |
Alexandre Belloni | 2d2b300 | 2018-05-17 22:33:28 +0200 | [diff] [blame] | 63 | static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm) |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 64 | { |
Alexandre Belloni | 2d2b300 | 2018-05-17 22:33:28 +0200 | [diff] [blame] | 65 | struct i2c_client *client = to_i2c_client(dev); |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 66 | unsigned char date[7]; |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 67 | unsigned int data; |
| 68 | int err; |
Andreas Werner | d643a49 | 2014-01-23 15:55:20 -0800 | [diff] [blame] | 69 | struct rx8581 *rx8581 = i2c_get_clientdata(client); |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 70 | |
| 71 | /* First we ensure that the "update flag" is not set, we read the |
| 72 | * time and date then re-read the "update flag". If the update flag |
| 73 | * has been set, we know that the time has changed during the read so |
| 74 | * we repeat the whole process again. |
| 75 | */ |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 76 | err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data); |
| 77 | if (err < 0) |
| 78 | return err; |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 79 | |
Alexandre Belloni | 6e6111f | 2018-05-17 22:33:30 +0200 | [diff] [blame] | 80 | if (data & RX8581_FLAG_VLF) { |
| 81 | dev_warn(dev, |
| 82 | "low voltage detected, date/time is not reliable.\n"); |
| 83 | return -EINVAL; |
| 84 | } |
| 85 | |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 86 | do { |
| 87 | /* If update flag set, clear it */ |
| 88 | if (data & RX8581_FLAG_UF) { |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 89 | err = regmap_write(rx8581->regmap, RX8581_REG_FLAG, |
| 90 | data & ~RX8581_FLAG_UF); |
| 91 | if (err < 0) |
| 92 | return err; |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* Now read time and date */ |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 96 | err = regmap_bulk_read(rx8581->regmap, RX8581_REG_SC, date, |
| 97 | sizeof(date)); |
| 98 | if (err < 0) |
| 99 | return err; |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 100 | |
| 101 | /* Check flag register */ |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 102 | err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data); |
| 103 | if (err < 0) |
| 104 | return err; |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 105 | } while (data & RX8581_FLAG_UF); |
| 106 | |
Alexandre Belloni | ed87c6d | 2018-05-17 22:33:29 +0200 | [diff] [blame] | 107 | dev_dbg(dev, "%s: raw data is sec=%02x, min=%02x, hr=%02x, " |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 108 | "wday=%02x, mday=%02x, mon=%02x, year=%02x\n", |
| 109 | __func__, |
| 110 | date[0], date[1], date[2], date[3], date[4], date[5], date[6]); |
| 111 | |
| 112 | tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F); |
| 113 | tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F); |
| 114 | tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F); /* rtc hr 0-23 */ |
| 115 | tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F); |
| 116 | tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F); |
| 117 | tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ |
Alexandre Belloni | c6e3c29 | 2018-05-17 22:33:27 +0200 | [diff] [blame] | 118 | tm->tm_year = bcd2bin(date[RX8581_REG_YR]) + 100; |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 119 | |
Alexandre Belloni | ed87c6d | 2018-05-17 22:33:29 +0200 | [diff] [blame] | 120 | dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 121 | "mday=%d, mon=%d, year=%d, wday=%d\n", |
| 122 | __func__, |
| 123 | tm->tm_sec, tm->tm_min, tm->tm_hour, |
| 124 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); |
| 125 | |
Alexandre Belloni | 24f421b | 2018-02-21 00:25:18 +0100 | [diff] [blame] | 126 | return 0; |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Alexandre Belloni | 2d2b300 | 2018-05-17 22:33:28 +0200 | [diff] [blame] | 129 | static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm) |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 130 | { |
Alexandre Belloni | 2d2b300 | 2018-05-17 22:33:28 +0200 | [diff] [blame] | 131 | struct i2c_client *client = to_i2c_client(dev); |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 132 | int err; |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 133 | unsigned char buf[7]; |
Andreas Werner | d643a49 | 2014-01-23 15:55:20 -0800 | [diff] [blame] | 134 | struct rx8581 *rx8581 = i2c_get_clientdata(client); |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 135 | |
Alexandre Belloni | ed87c6d | 2018-05-17 22:33:29 +0200 | [diff] [blame] | 136 | dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, " |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 137 | "mday=%d, mon=%d, year=%d, wday=%d\n", |
| 138 | __func__, |
| 139 | tm->tm_sec, tm->tm_min, tm->tm_hour, |
| 140 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); |
| 141 | |
| 142 | /* hours, minutes and seconds */ |
| 143 | buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec); |
| 144 | buf[RX8581_REG_MN] = bin2bcd(tm->tm_min); |
| 145 | buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour); |
| 146 | |
| 147 | buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday); |
| 148 | |
| 149 | /* month, 1 - 12 */ |
| 150 | buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1); |
| 151 | |
| 152 | /* year and century */ |
Alexandre Belloni | c6e3c29 | 2018-05-17 22:33:27 +0200 | [diff] [blame] | 153 | buf[RX8581_REG_YR] = bin2bcd(tm->tm_year - 100); |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 154 | buf[RX8581_REG_DW] = (0x1 << tm->tm_wday); |
| 155 | |
| 156 | /* Stop the clock */ |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 157 | err = regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL, |
| 158 | RX8581_CTRL_STOP, RX8581_CTRL_STOP); |
| 159 | if (err < 0) |
| 160 | return err; |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 161 | |
| 162 | /* write register's data */ |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 163 | err = regmap_bulk_write(rx8581->regmap, RX8581_REG_SC, |
| 164 | buf, sizeof(buf)); |
| 165 | if (err < 0) |
| 166 | return err; |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 167 | |
Rudolf Marek | 2884fce | 2010-07-27 13:18:02 -0700 | [diff] [blame] | 168 | /* get VLF and clear it */ |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 169 | err = regmap_update_bits(rx8581->regmap, RX8581_REG_FLAG, |
| 170 | RX8581_FLAG_VLF, 0); |
| 171 | if (err < 0) |
| 172 | return err; |
Rudolf Marek | 2884fce | 2010-07-27 13:18:02 -0700 | [diff] [blame] | 173 | |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 174 | /* Restart the clock */ |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 175 | return regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL, |
| 176 | RX8581_CTRL_STOP, 0); |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 179 | static const struct rtc_class_ops rx8581_rtc_ops = { |
| 180 | .read_time = rx8581_rtc_read_time, |
| 181 | .set_time = rx8581_rtc_set_time, |
| 182 | }; |
| 183 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 184 | static int rx8581_probe(struct i2c_client *client, |
| 185 | const struct i2c_device_id *id) |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 186 | { |
Andreas Werner | d643a49 | 2014-01-23 15:55:20 -0800 | [diff] [blame] | 187 | struct rx8581 *rx8581; |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 188 | static const struct regmap_config config = { |
| 189 | .reg_bits = 8, |
| 190 | .val_bits = 8, |
| 191 | .max_register = 0xf, |
| 192 | }; |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 193 | |
| 194 | dev_dbg(&client->dev, "%s\n", __func__); |
| 195 | |
Andreas Werner | d643a49 | 2014-01-23 15:55:20 -0800 | [diff] [blame] | 196 | rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL); |
| 197 | if (!rx8581) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | i2c_set_clientdata(client, rx8581); |
Andreas Werner | d643a49 | 2014-01-23 15:55:20 -0800 | [diff] [blame] | 201 | |
Alexandre Belloni | 94389b2 | 2018-05-17 22:33:31 +0200 | [diff] [blame^] | 202 | rx8581->regmap = devm_regmap_init_i2c(client, &config); |
| 203 | if (IS_ERR(rx8581->regmap)) |
| 204 | return PTR_ERR(rx8581->regmap); |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 205 | |
Alexandre Belloni | b815716 | 2018-05-17 22:33:25 +0200 | [diff] [blame] | 206 | rx8581->rtc = devm_rtc_allocate_device(&client->dev); |
| 207 | if (IS_ERR(rx8581->rtc)) |
Andreas Werner | d643a49 | 2014-01-23 15:55:20 -0800 | [diff] [blame] | 208 | return PTR_ERR(rx8581->rtc); |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 209 | |
Alexandre Belloni | b815716 | 2018-05-17 22:33:25 +0200 | [diff] [blame] | 210 | rx8581->rtc->ops = &rx8581_rtc_ops; |
Alexandre Belloni | 86c54ef | 2018-05-17 22:33:26 +0200 | [diff] [blame] | 211 | rx8581->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; |
| 212 | rx8581->rtc->range_max = RTC_TIMESTAMP_END_2099; |
Alexandre Belloni | c6e3c29 | 2018-05-17 22:33:27 +0200 | [diff] [blame] | 213 | rx8581->rtc->start_secs = 0; |
| 214 | rx8581->rtc->set_start_time = true; |
Alexandre Belloni | b815716 | 2018-05-17 22:33:25 +0200 | [diff] [blame] | 215 | |
| 216 | return rtc_register_device(rx8581->rtc); |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 219 | static const struct i2c_device_id rx8581_id[] = { |
| 220 | { "rx8581", 0 }, |
| 221 | { } |
| 222 | }; |
| 223 | MODULE_DEVICE_TABLE(i2c, rx8581_id); |
| 224 | |
Javier Martinez Canillas | ffbecfb | 2017-03-03 11:29:22 -0300 | [diff] [blame] | 225 | static const struct of_device_id rx8581_of_match[] = { |
| 226 | { .compatible = "epson,rx8581" }, |
| 227 | { } |
| 228 | }; |
| 229 | MODULE_DEVICE_TABLE(of, rx8581_of_match); |
| 230 | |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 231 | static struct i2c_driver rx8581_driver = { |
| 232 | .driver = { |
| 233 | .name = "rtc-rx8581", |
Javier Martinez Canillas | ffbecfb | 2017-03-03 11:29:22 -0300 | [diff] [blame] | 234 | .of_match_table = of_match_ptr(rx8581_of_match), |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 235 | }, |
| 236 | .probe = rx8581_probe, |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 237 | .id_table = rx8581_id, |
| 238 | }; |
| 239 | |
Axel Lin | 0abc920 | 2012-03-23 15:02:31 -0700 | [diff] [blame] | 240 | module_i2c_driver(rx8581_driver); |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 241 | |
Martyn Welch | 9756c4d | 2010-04-14 09:24:16 +0100 | [diff] [blame] | 242 | MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>"); |
Martyn Welch | a7fa985 | 2008-11-12 13:27:06 -0800 | [diff] [blame] | 243 | MODULE_DESCRIPTION("Epson RX-8581 RTC driver"); |
| 244 | MODULE_LICENSE("GPL"); |