Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 2 | /* |
| 3 | * rtc-fm3130.c - RTC driver for Ramtron FM3130 I2C chip. |
| 4 | * |
| 5 | * Copyright (C) 2008 Sergey Lapin |
| 6 | * Based on ds1307 driver by James Chapman and David Brownell |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/i2c.h> |
| 11 | #include <linux/rtc.h> |
| 12 | #include <linux/bcd.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 14 | |
| 15 | #define FM3130_RTC_CONTROL (0x0) |
| 16 | #define FM3130_CAL_CONTROL (0x1) |
| 17 | #define FM3130_RTC_SECONDS (0x2) |
| 18 | #define FM3130_RTC_MINUTES (0x3) |
| 19 | #define FM3130_RTC_HOURS (0x4) |
| 20 | #define FM3130_RTC_DAY (0x5) |
| 21 | #define FM3130_RTC_DATE (0x6) |
| 22 | #define FM3130_RTC_MONTHS (0x7) |
| 23 | #define FM3130_RTC_YEARS (0x8) |
| 24 | |
| 25 | #define FM3130_ALARM_SECONDS (0x9) |
| 26 | #define FM3130_ALARM_MINUTES (0xa) |
| 27 | #define FM3130_ALARM_HOURS (0xb) |
| 28 | #define FM3130_ALARM_DATE (0xc) |
| 29 | #define FM3130_ALARM_MONTHS (0xd) |
| 30 | #define FM3130_ALARM_WP_CONTROL (0xe) |
| 31 | |
| 32 | #define FM3130_CAL_CONTROL_BIT_nOSCEN (1 << 7) /* Osciallator enabled */ |
| 33 | #define FM3130_RTC_CONTROL_BIT_LB (1 << 7) /* Low battery */ |
| 34 | #define FM3130_RTC_CONTROL_BIT_AF (1 << 6) /* Alarm flag */ |
| 35 | #define FM3130_RTC_CONTROL_BIT_CF (1 << 5) /* Century overflow */ |
| 36 | #define FM3130_RTC_CONTROL_BIT_POR (1 << 4) /* Power on reset */ |
| 37 | #define FM3130_RTC_CONTROL_BIT_AEN (1 << 3) /* Alarm enable */ |
| 38 | #define FM3130_RTC_CONTROL_BIT_CAL (1 << 2) /* Calibration mode */ |
| 39 | #define FM3130_RTC_CONTROL_BIT_WRITE (1 << 1) /* W=1 -> write mode W=0 normal */ |
| 40 | #define FM3130_RTC_CONTROL_BIT_READ (1 << 0) /* R=1 -> read mode R=0 normal */ |
| 41 | |
| 42 | #define FM3130_CLOCK_REGS 7 |
| 43 | #define FM3130_ALARM_REGS 5 |
| 44 | |
| 45 | struct fm3130 { |
| 46 | u8 reg_addr_time; |
Sachin Kamat | c865e92 | 2013-07-03 15:05:51 -0700 | [diff] [blame] | 47 | u8 reg_addr_alarm; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 48 | u8 regs[15]; |
| 49 | struct i2c_msg msg[4]; |
| 50 | struct i2c_client *client; |
| 51 | struct rtc_device *rtc; |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 52 | int alarm_valid; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 53 | int data_valid; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 54 | }; |
| 55 | static const struct i2c_device_id fm3130_id[] = { |
Alessandro Zummo | 876550a | 2008-07-12 13:47:55 -0700 | [diff] [blame] | 56 | { "fm3130", 0 }, |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 57 | { } |
| 58 | }; |
| 59 | MODULE_DEVICE_TABLE(i2c, fm3130_id); |
| 60 | |
| 61 | #define FM3130_MODE_NORMAL 0 |
| 62 | #define FM3130_MODE_WRITE 1 |
| 63 | #define FM3130_MODE_READ 2 |
| 64 | |
| 65 | static void fm3130_rtc_mode(struct device *dev, int mode) |
| 66 | { |
| 67 | struct fm3130 *fm3130 = dev_get_drvdata(dev); |
| 68 | |
| 69 | fm3130->regs[FM3130_RTC_CONTROL] = |
| 70 | i2c_smbus_read_byte_data(fm3130->client, FM3130_RTC_CONTROL); |
| 71 | switch (mode) { |
| 72 | case FM3130_MODE_NORMAL: |
| 73 | fm3130->regs[FM3130_RTC_CONTROL] &= |
| 74 | ~(FM3130_RTC_CONTROL_BIT_WRITE | |
| 75 | FM3130_RTC_CONTROL_BIT_READ); |
| 76 | break; |
| 77 | case FM3130_MODE_WRITE: |
| 78 | fm3130->regs[FM3130_RTC_CONTROL] |= FM3130_RTC_CONTROL_BIT_WRITE; |
| 79 | break; |
| 80 | case FM3130_MODE_READ: |
| 81 | fm3130->regs[FM3130_RTC_CONTROL] |= FM3130_RTC_CONTROL_BIT_READ; |
| 82 | break; |
| 83 | default: |
| 84 | dev_dbg(dev, "invalid mode %d\n", mode); |
| 85 | break; |
| 86 | } |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 87 | |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 88 | i2c_smbus_write_byte_data(fm3130->client, |
| 89 | FM3130_RTC_CONTROL, fm3130->regs[FM3130_RTC_CONTROL]); |
| 90 | } |
| 91 | |
| 92 | static int fm3130_get_time(struct device *dev, struct rtc_time *t) |
| 93 | { |
| 94 | struct fm3130 *fm3130 = dev_get_drvdata(dev); |
| 95 | int tmp; |
| 96 | |
| 97 | if (!fm3130->data_valid) { |
| 98 | /* We have invalid data in RTC, probably due |
| 99 | to battery faults or other problems. Return EIO |
Uwe Kleine-König | fd0961f | 2010-06-11 12:16:56 +0200 | [diff] [blame] | 100 | for now, it will allow us to set data later instead |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 101 | of error during probing which disables device */ |
| 102 | return -EIO; |
| 103 | } |
| 104 | fm3130_rtc_mode(dev, FM3130_MODE_READ); |
| 105 | |
| 106 | /* read the RTC date and time registers all at once */ |
Wolfram Sang | 55c2431 | 2019-06-08 12:56:05 +0200 | [diff] [blame] | 107 | tmp = i2c_transfer(fm3130->client->adapter, fm3130->msg, 2); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 108 | if (tmp != 2) { |
| 109 | dev_err(dev, "%s error %d\n", "read", tmp); |
| 110 | return -EIO; |
| 111 | } |
| 112 | |
| 113 | fm3130_rtc_mode(dev, FM3130_MODE_NORMAL); |
| 114 | |
Andy Shevchenko | 01a4ca1 | 2013-02-21 16:44:22 -0800 | [diff] [blame] | 115 | dev_dbg(dev, "%s: %15ph\n", "read", fm3130->regs); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 116 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 117 | t->tm_sec = bcd2bin(fm3130->regs[FM3130_RTC_SECONDS] & 0x7f); |
| 118 | t->tm_min = bcd2bin(fm3130->regs[FM3130_RTC_MINUTES] & 0x7f); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 119 | tmp = fm3130->regs[FM3130_RTC_HOURS] & 0x3f; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 120 | t->tm_hour = bcd2bin(tmp); |
| 121 | t->tm_wday = bcd2bin(fm3130->regs[FM3130_RTC_DAY] & 0x07) - 1; |
| 122 | t->tm_mday = bcd2bin(fm3130->regs[FM3130_RTC_DATE] & 0x3f); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 123 | tmp = fm3130->regs[FM3130_RTC_MONTHS] & 0x1f; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 124 | t->tm_mon = bcd2bin(tmp) - 1; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 125 | |
| 126 | /* assume 20YY not 19YY, and ignore CF bit */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 127 | t->tm_year = bcd2bin(fm3130->regs[FM3130_RTC_YEARS]) + 100; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 128 | |
| 129 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 130 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
| 131 | "read", t->tm_sec, t->tm_min, |
| 132 | t->tm_hour, t->tm_mday, |
| 133 | t->tm_mon, t->tm_year, t->tm_wday); |
| 134 | |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 135 | return 0; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
| 139 | static int fm3130_set_time(struct device *dev, struct rtc_time *t) |
| 140 | { |
| 141 | struct fm3130 *fm3130 = dev_get_drvdata(dev); |
| 142 | int tmp, i; |
| 143 | u8 *buf = fm3130->regs; |
| 144 | |
| 145 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 146 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
| 147 | "write", t->tm_sec, t->tm_min, |
| 148 | t->tm_hour, t->tm_mday, |
| 149 | t->tm_mon, t->tm_year, t->tm_wday); |
| 150 | |
| 151 | /* first register addr */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 152 | buf[FM3130_RTC_SECONDS] = bin2bcd(t->tm_sec); |
| 153 | buf[FM3130_RTC_MINUTES] = bin2bcd(t->tm_min); |
| 154 | buf[FM3130_RTC_HOURS] = bin2bcd(t->tm_hour); |
| 155 | buf[FM3130_RTC_DAY] = bin2bcd(t->tm_wday + 1); |
| 156 | buf[FM3130_RTC_DATE] = bin2bcd(t->tm_mday); |
| 157 | buf[FM3130_RTC_MONTHS] = bin2bcd(t->tm_mon + 1); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 158 | |
| 159 | /* assume 20YY not 19YY */ |
| 160 | tmp = t->tm_year - 100; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 161 | buf[FM3130_RTC_YEARS] = bin2bcd(tmp); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 162 | |
Andy Shevchenko | 01a4ca1 | 2013-02-21 16:44:22 -0800 | [diff] [blame] | 163 | dev_dbg(dev, "%s: %15ph\n", "write", buf); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 164 | |
| 165 | fm3130_rtc_mode(dev, FM3130_MODE_WRITE); |
| 166 | |
| 167 | /* Writing time registers, we don't support multibyte transfers */ |
| 168 | for (i = 0; i < FM3130_CLOCK_REGS; i++) { |
| 169 | i2c_smbus_write_byte_data(fm3130->client, |
| 170 | FM3130_RTC_SECONDS + i, |
| 171 | fm3130->regs[FM3130_RTC_SECONDS + i]); |
| 172 | } |
| 173 | |
| 174 | fm3130_rtc_mode(dev, FM3130_MODE_NORMAL); |
| 175 | |
| 176 | /* We assume here that data are valid once written */ |
| 177 | if (!fm3130->data_valid) |
| 178 | fm3130->data_valid = 1; |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int fm3130_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 183 | { |
| 184 | struct fm3130 *fm3130 = dev_get_drvdata(dev); |
| 185 | int tmp; |
| 186 | struct rtc_time *tm = &alrm->time; |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 187 | |
| 188 | if (!fm3130->alarm_valid) { |
| 189 | /* |
| 190 | * We have invalid alarm in RTC, probably due to battery faults |
| 191 | * or other problems. Return EIO for now, it will allow us to |
| 192 | * set alarm value later instead of error during probing which |
| 193 | * disables device |
| 194 | */ |
| 195 | return -EIO; |
| 196 | } |
| 197 | |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 198 | /* read the RTC alarm registers all at once */ |
Wolfram Sang | 55c2431 | 2019-06-08 12:56:05 +0200 | [diff] [blame] | 199 | tmp = i2c_transfer(fm3130->client->adapter, &fm3130->msg[2], 2); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 200 | if (tmp != 2) { |
| 201 | dev_err(dev, "%s error %d\n", "read", tmp); |
| 202 | return -EIO; |
| 203 | } |
| 204 | dev_dbg(dev, "alarm read %02x %02x %02x %02x %02x\n", |
| 205 | fm3130->regs[FM3130_ALARM_SECONDS], |
| 206 | fm3130->regs[FM3130_ALARM_MINUTES], |
| 207 | fm3130->regs[FM3130_ALARM_HOURS], |
| 208 | fm3130->regs[FM3130_ALARM_DATE], |
| 209 | fm3130->regs[FM3130_ALARM_MONTHS]); |
| 210 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 211 | tm->tm_sec = bcd2bin(fm3130->regs[FM3130_ALARM_SECONDS] & 0x7F); |
| 212 | tm->tm_min = bcd2bin(fm3130->regs[FM3130_ALARM_MINUTES] & 0x7F); |
| 213 | tm->tm_hour = bcd2bin(fm3130->regs[FM3130_ALARM_HOURS] & 0x3F); |
| 214 | tm->tm_mday = bcd2bin(fm3130->regs[FM3130_ALARM_DATE] & 0x3F); |
| 215 | tm->tm_mon = bcd2bin(fm3130->regs[FM3130_ALARM_MONTHS] & 0x1F); |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 216 | |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 217 | if (tm->tm_mon > 0) |
| 218 | tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */ |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 219 | |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 220 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 221 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
| 222 | "read alarm", tm->tm_sec, tm->tm_min, |
| 223 | tm->tm_hour, tm->tm_mday, |
| 224 | tm->tm_mon, tm->tm_year, tm->tm_wday); |
| 225 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 226 | /* check if alarm enabled */ |
| 227 | fm3130->regs[FM3130_RTC_CONTROL] = |
| 228 | i2c_smbus_read_byte_data(fm3130->client, FM3130_RTC_CONTROL); |
| 229 | |
| 230 | if ((fm3130->regs[FM3130_RTC_CONTROL] & FM3130_RTC_CONTROL_BIT_AEN) && |
| 231 | (~fm3130->regs[FM3130_RTC_CONTROL] & |
| 232 | FM3130_RTC_CONTROL_BIT_CAL)) { |
| 233 | alrm->enabled = 1; |
| 234 | } |
| 235 | |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int fm3130_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 240 | { |
| 241 | struct fm3130 *fm3130 = dev_get_drvdata(dev); |
| 242 | struct rtc_time *tm = &alrm->time; |
| 243 | int i; |
| 244 | |
| 245 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 246 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
| 247 | "write alarm", tm->tm_sec, tm->tm_min, |
| 248 | tm->tm_hour, tm->tm_mday, |
| 249 | tm->tm_mon, tm->tm_year, tm->tm_wday); |
| 250 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 251 | fm3130->regs[FM3130_ALARM_SECONDS] = |
| 252 | (tm->tm_sec != -1) ? bin2bcd(tm->tm_sec) : 0x80; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 253 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 254 | fm3130->regs[FM3130_ALARM_MINUTES] = |
| 255 | (tm->tm_min != -1) ? bin2bcd(tm->tm_min) : 0x80; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 256 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 257 | fm3130->regs[FM3130_ALARM_HOURS] = |
| 258 | (tm->tm_hour != -1) ? bin2bcd(tm->tm_hour) : 0x80; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 259 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 260 | fm3130->regs[FM3130_ALARM_DATE] = |
| 261 | (tm->tm_mday != -1) ? bin2bcd(tm->tm_mday) : 0x80; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 262 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 263 | fm3130->regs[FM3130_ALARM_MONTHS] = |
| 264 | (tm->tm_mon != -1) ? bin2bcd(tm->tm_mon + 1) : 0x80; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 265 | |
| 266 | dev_dbg(dev, "alarm write %02x %02x %02x %02x %02x\n", |
| 267 | fm3130->regs[FM3130_ALARM_SECONDS], |
| 268 | fm3130->regs[FM3130_ALARM_MINUTES], |
| 269 | fm3130->regs[FM3130_ALARM_HOURS], |
| 270 | fm3130->regs[FM3130_ALARM_DATE], |
| 271 | fm3130->regs[FM3130_ALARM_MONTHS]); |
| 272 | /* Writing time registers, we don't support multibyte transfers */ |
| 273 | for (i = 0; i < FM3130_ALARM_REGS; i++) { |
| 274 | i2c_smbus_write_byte_data(fm3130->client, |
| 275 | FM3130_ALARM_SECONDS + i, |
| 276 | fm3130->regs[FM3130_ALARM_SECONDS + i]); |
| 277 | } |
| 278 | fm3130->regs[FM3130_RTC_CONTROL] = |
| 279 | i2c_smbus_read_byte_data(fm3130->client, FM3130_RTC_CONTROL); |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 280 | |
| 281 | /* enable or disable alarm */ |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 282 | if (alrm->enabled) { |
| 283 | i2c_smbus_write_byte_data(fm3130->client, FM3130_RTC_CONTROL, |
| 284 | (fm3130->regs[FM3130_RTC_CONTROL] & |
| 285 | ~(FM3130_RTC_CONTROL_BIT_CAL)) | |
| 286 | FM3130_RTC_CONTROL_BIT_AEN); |
| 287 | } else { |
| 288 | i2c_smbus_write_byte_data(fm3130->client, FM3130_RTC_CONTROL, |
| 289 | fm3130->regs[FM3130_RTC_CONTROL] & |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 290 | ~(FM3130_RTC_CONTROL_BIT_CAL) & |
| 291 | ~(FM3130_RTC_CONTROL_BIT_AEN)); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 292 | } |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 293 | |
| 294 | /* We assume here that data is valid once written */ |
| 295 | if (!fm3130->alarm_valid) |
| 296 | fm3130->alarm_valid = 1; |
| 297 | |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 301 | static int fm3130_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 302 | { |
| 303 | struct fm3130 *fm3130 = dev_get_drvdata(dev); |
| 304 | int ret = 0; |
| 305 | |
| 306 | fm3130->regs[FM3130_RTC_CONTROL] = |
| 307 | i2c_smbus_read_byte_data(fm3130->client, FM3130_RTC_CONTROL); |
| 308 | |
| 309 | dev_dbg(dev, "alarm_irq_enable: enable=%d, FM3130_RTC_CONTROL=%02x\n", |
| 310 | enabled, fm3130->regs[FM3130_RTC_CONTROL]); |
| 311 | |
| 312 | switch (enabled) { |
| 313 | case 0: /* alarm off */ |
| 314 | ret = i2c_smbus_write_byte_data(fm3130->client, |
| 315 | FM3130_RTC_CONTROL, fm3130->regs[FM3130_RTC_CONTROL] & |
| 316 | ~(FM3130_RTC_CONTROL_BIT_CAL) & |
| 317 | ~(FM3130_RTC_CONTROL_BIT_AEN)); |
| 318 | break; |
| 319 | case 1: /* alarm on */ |
| 320 | ret = i2c_smbus_write_byte_data(fm3130->client, |
| 321 | FM3130_RTC_CONTROL, (fm3130->regs[FM3130_RTC_CONTROL] & |
| 322 | ~(FM3130_RTC_CONTROL_BIT_CAL)) | |
| 323 | FM3130_RTC_CONTROL_BIT_AEN); |
| 324 | break; |
| 325 | default: |
| 326 | ret = -EINVAL; |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | return ret; |
| 331 | } |
| 332 | |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 333 | static const struct rtc_class_ops fm3130_rtc_ops = { |
| 334 | .read_time = fm3130_get_time, |
| 335 | .set_time = fm3130_set_time, |
| 336 | .read_alarm = fm3130_read_alarm, |
| 337 | .set_alarm = fm3130_set_alarm, |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 338 | .alarm_irq_enable = fm3130_alarm_irq_enable, |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 339 | }; |
| 340 | |
| 341 | static struct i2c_driver fm3130_driver; |
| 342 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 343 | static int fm3130_probe(struct i2c_client *client, |
| 344 | const struct i2c_device_id *id) |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 345 | { |
| 346 | struct fm3130 *fm3130; |
| 347 | int err = -ENODEV; |
| 348 | int tmp; |
Wolfram Sang | 55c2431 | 2019-06-08 12:56:05 +0200 | [diff] [blame] | 349 | struct i2c_adapter *adapter = client->adapter; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 350 | |
| 351 | if (!i2c_check_functionality(adapter, |
| 352 | I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) |
| 353 | return -EIO; |
| 354 | |
Jingoo Han | c54a52e | 2013-04-29 16:20:41 -0700 | [diff] [blame] | 355 | fm3130 = devm_kzalloc(&client->dev, sizeof(struct fm3130), GFP_KERNEL); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 356 | |
| 357 | if (!fm3130) |
| 358 | return -ENOMEM; |
| 359 | |
| 360 | fm3130->client = client; |
| 361 | i2c_set_clientdata(client, fm3130); |
| 362 | fm3130->reg_addr_time = FM3130_RTC_SECONDS; |
| 363 | fm3130->reg_addr_alarm = FM3130_ALARM_SECONDS; |
| 364 | |
| 365 | /* Messages to read time */ |
| 366 | fm3130->msg[0].addr = client->addr; |
| 367 | fm3130->msg[0].flags = 0; |
| 368 | fm3130->msg[0].len = 1; |
| 369 | fm3130->msg[0].buf = &fm3130->reg_addr_time; |
| 370 | |
| 371 | fm3130->msg[1].addr = client->addr; |
| 372 | fm3130->msg[1].flags = I2C_M_RD; |
| 373 | fm3130->msg[1].len = FM3130_CLOCK_REGS; |
| 374 | fm3130->msg[1].buf = &fm3130->regs[FM3130_RTC_SECONDS]; |
| 375 | |
| 376 | /* Messages to read alarm */ |
| 377 | fm3130->msg[2].addr = client->addr; |
| 378 | fm3130->msg[2].flags = 0; |
| 379 | fm3130->msg[2].len = 1; |
| 380 | fm3130->msg[2].buf = &fm3130->reg_addr_alarm; |
| 381 | |
| 382 | fm3130->msg[3].addr = client->addr; |
| 383 | fm3130->msg[3].flags = I2C_M_RD; |
| 384 | fm3130->msg[3].len = FM3130_ALARM_REGS; |
| 385 | fm3130->msg[3].buf = &fm3130->regs[FM3130_ALARM_SECONDS]; |
| 386 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 387 | fm3130->alarm_valid = 0; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 388 | fm3130->data_valid = 0; |
| 389 | |
| 390 | tmp = i2c_transfer(adapter, fm3130->msg, 4); |
| 391 | if (tmp != 4) { |
Jingoo Han | 198b8ec8 | 2013-04-29 16:19:29 -0700 | [diff] [blame] | 392 | dev_dbg(&client->dev, "read error %d\n", tmp); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 393 | err = -EIO; |
| 394 | goto exit_free; |
| 395 | } |
| 396 | |
| 397 | fm3130->regs[FM3130_RTC_CONTROL] = |
| 398 | i2c_smbus_read_byte_data(client, FM3130_RTC_CONTROL); |
| 399 | fm3130->regs[FM3130_CAL_CONTROL] = |
| 400 | i2c_smbus_read_byte_data(client, FM3130_CAL_CONTROL); |
| 401 | |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 402 | /* Disabling calibration mode */ |
Sergey Matyukevich | f4b5162 | 2010-02-02 13:43:59 -0800 | [diff] [blame] | 403 | if (fm3130->regs[FM3130_RTC_CONTROL] & FM3130_RTC_CONTROL_BIT_CAL) { |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 404 | i2c_smbus_write_byte_data(client, FM3130_RTC_CONTROL, |
| 405 | fm3130->regs[FM3130_RTC_CONTROL] & |
| 406 | ~(FM3130_RTC_CONTROL_BIT_CAL)); |
| 407 | dev_warn(&client->dev, "Disabling calibration mode!\n"); |
Sergey Matyukevich | f4b5162 | 2010-02-02 13:43:59 -0800 | [diff] [blame] | 408 | } |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 409 | |
| 410 | /* Disabling read and write modes */ |
| 411 | if (fm3130->regs[FM3130_RTC_CONTROL] & FM3130_RTC_CONTROL_BIT_WRITE || |
Sergey Matyukevich | f4b5162 | 2010-02-02 13:43:59 -0800 | [diff] [blame] | 412 | fm3130->regs[FM3130_RTC_CONTROL] & FM3130_RTC_CONTROL_BIT_READ) { |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 413 | i2c_smbus_write_byte_data(client, FM3130_RTC_CONTROL, |
| 414 | fm3130->regs[FM3130_RTC_CONTROL] & |
| 415 | ~(FM3130_RTC_CONTROL_BIT_READ | |
| 416 | FM3130_RTC_CONTROL_BIT_WRITE)); |
| 417 | dev_warn(&client->dev, "Disabling READ or WRITE mode!\n"); |
Sergey Matyukevich | f4b5162 | 2010-02-02 13:43:59 -0800 | [diff] [blame] | 418 | } |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 419 | |
| 420 | /* oscillator off? turn it on, so clock can tick. */ |
| 421 | if (fm3130->regs[FM3130_CAL_CONTROL] & FM3130_CAL_CONTROL_BIT_nOSCEN) |
| 422 | i2c_smbus_write_byte_data(client, FM3130_CAL_CONTROL, |
| 423 | fm3130->regs[FM3130_CAL_CONTROL] & |
| 424 | ~(FM3130_CAL_CONTROL_BIT_nOSCEN)); |
| 425 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 426 | /* low battery? clear flag, and warn */ |
| 427 | if (fm3130->regs[FM3130_RTC_CONTROL] & FM3130_RTC_CONTROL_BIT_LB) { |
| 428 | i2c_smbus_write_byte_data(client, FM3130_RTC_CONTROL, |
| 429 | fm3130->regs[FM3130_RTC_CONTROL] & |
| 430 | ~(FM3130_RTC_CONTROL_BIT_LB)); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 431 | dev_warn(&client->dev, "Low battery!\n"); |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 432 | } |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 433 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 434 | /* check if Power On Reset bit is set */ |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 435 | if (fm3130->regs[FM3130_RTC_CONTROL] & FM3130_RTC_CONTROL_BIT_POR) { |
| 436 | i2c_smbus_write_byte_data(client, FM3130_RTC_CONTROL, |
| 437 | fm3130->regs[FM3130_RTC_CONTROL] & |
| 438 | ~FM3130_RTC_CONTROL_BIT_POR); |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 439 | dev_dbg(&client->dev, "POR bit is set\n"); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 440 | } |
| 441 | /* ACS is controlled by alarm */ |
| 442 | i2c_smbus_write_byte_data(client, FM3130_ALARM_WP_CONTROL, 0x80); |
| 443 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 444 | /* alarm registers sanity check */ |
| 445 | tmp = bcd2bin(fm3130->regs[FM3130_RTC_SECONDS] & 0x7f); |
| 446 | if (tmp > 59) |
| 447 | goto bad_alarm; |
| 448 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 449 | tmp = bcd2bin(fm3130->regs[FM3130_RTC_MINUTES] & 0x7f); |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 450 | if (tmp > 59) |
| 451 | goto bad_alarm; |
| 452 | |
| 453 | tmp = bcd2bin(fm3130->regs[FM3130_RTC_HOURS] & 0x3f); |
| 454 | if (tmp > 23) |
| 455 | goto bad_alarm; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 456 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 457 | tmp = bcd2bin(fm3130->regs[FM3130_RTC_DATE] & 0x3f); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 458 | if (tmp == 0 || tmp > 31) |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 459 | goto bad_alarm; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 460 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 461 | tmp = bcd2bin(fm3130->regs[FM3130_RTC_MONTHS] & 0x1f); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 462 | if (tmp == 0 || tmp > 12) |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 463 | goto bad_alarm; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 464 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 465 | fm3130->alarm_valid = 1; |
| 466 | |
| 467 | bad_alarm: |
| 468 | |
| 469 | /* clock registers sanity chek */ |
| 470 | tmp = bcd2bin(fm3130->regs[FM3130_RTC_SECONDS] & 0x7f); |
| 471 | if (tmp > 59) |
| 472 | goto bad_clock; |
| 473 | |
| 474 | tmp = bcd2bin(fm3130->regs[FM3130_RTC_MINUTES] & 0x7f); |
| 475 | if (tmp > 59) |
| 476 | goto bad_clock; |
| 477 | |
| 478 | tmp = bcd2bin(fm3130->regs[FM3130_RTC_HOURS] & 0x3f); |
| 479 | if (tmp > 23) |
| 480 | goto bad_clock; |
| 481 | |
| 482 | tmp = bcd2bin(fm3130->regs[FM3130_RTC_DAY] & 0x7); |
| 483 | if (tmp == 0 || tmp > 7) |
| 484 | goto bad_clock; |
| 485 | |
| 486 | tmp = bcd2bin(fm3130->regs[FM3130_RTC_DATE] & 0x3f); |
| 487 | if (tmp == 0 || tmp > 31) |
| 488 | goto bad_clock; |
| 489 | |
| 490 | tmp = bcd2bin(fm3130->regs[FM3130_RTC_MONTHS] & 0x1f); |
| 491 | if (tmp == 0 || tmp > 12) |
| 492 | goto bad_clock; |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 493 | |
| 494 | fm3130->data_valid = 1; |
| 495 | |
Sergey Matyukevich | f3f99cf | 2010-08-10 18:02:10 -0700 | [diff] [blame] | 496 | bad_clock: |
| 497 | |
| 498 | if (!fm3130->data_valid || !fm3130->alarm_valid) |
Andy Shevchenko | 01a4ca1 | 2013-02-21 16:44:22 -0800 | [diff] [blame] | 499 | dev_dbg(&client->dev, "%s: %15ph\n", "bogus registers", |
| 500 | fm3130->regs); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 501 | |
| 502 | /* We won't bail out here because we just got invalid data. |
| 503 | Time setting from u-boot doesn't work anyway */ |
Jingoo Han | c54a52e | 2013-04-29 16:20:41 -0700 | [diff] [blame] | 504 | fm3130->rtc = devm_rtc_device_register(&client->dev, client->name, |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 505 | &fm3130_rtc_ops, THIS_MODULE); |
| 506 | if (IS_ERR(fm3130->rtc)) { |
| 507 | err = PTR_ERR(fm3130->rtc); |
| 508 | dev_err(&client->dev, |
| 509 | "unable to register the class device\n"); |
| 510 | goto exit_free; |
| 511 | } |
| 512 | return 0; |
| 513 | exit_free: |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 514 | return err; |
| 515 | } |
| 516 | |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 517 | static struct i2c_driver fm3130_driver = { |
| 518 | .driver = { |
| 519 | .name = "rtc-fm3130", |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 520 | }, |
| 521 | .probe = fm3130_probe, |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 522 | .id_table = fm3130_id, |
| 523 | }; |
| 524 | |
Axel Lin | 0abc920 | 2012-03-23 15:02:31 -0700 | [diff] [blame] | 525 | module_i2c_driver(fm3130_driver); |
Sergey Lapin | c6d8f40 | 2008-06-12 15:21:55 -0700 | [diff] [blame] | 526 | |
| 527 | MODULE_DESCRIPTION("RTC driver for FM3130"); |
| 528 | MODULE_AUTHOR("Sergey Lapin <slapin@ossfans.org>"); |
| 529 | MODULE_LICENSE("GPL"); |
| 530 | |