Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Seiko Instruments S-35390A RTC Driver |
| 3 | * |
| 4 | * Copyright (c) 2007 Byron Bradley |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/rtc.h> |
| 14 | #include <linux/i2c.h> |
| 15 | #include <linux/bitrev.h> |
| 16 | #include <linux/bcd.h> |
| 17 | #include <linux/slab.h> |
Uwe Kleine-König | 8e6583f | 2016-07-02 17:28:09 +0200 | [diff] [blame] | 18 | #include <linux/delay.h> |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 19 | |
| 20 | #define S35390A_CMD_STATUS1 0 |
| 21 | #define S35390A_CMD_STATUS2 1 |
| 22 | #define S35390A_CMD_TIME1 2 |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 23 | #define S35390A_CMD_TIME2 3 |
| 24 | #define S35390A_CMD_INT2_REG1 5 |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 25 | |
| 26 | #define S35390A_BYTE_YEAR 0 |
| 27 | #define S35390A_BYTE_MONTH 1 |
| 28 | #define S35390A_BYTE_DAY 2 |
| 29 | #define S35390A_BYTE_WDAY 3 |
| 30 | #define S35390A_BYTE_HOURS 4 |
| 31 | #define S35390A_BYTE_MINS 5 |
| 32 | #define S35390A_BYTE_SECS 6 |
| 33 | |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 34 | #define S35390A_ALRM_BYTE_WDAY 0 |
| 35 | #define S35390A_ALRM_BYTE_HOURS 1 |
| 36 | #define S35390A_ALRM_BYTE_MINS 2 |
| 37 | |
Uwe Kleine-König | 3bd3272 | 2016-07-02 17:28:10 +0200 | [diff] [blame] | 38 | /* flags for STATUS1 */ |
Richard Leitner | 097aa24 | 2019-05-23 13:54:51 +0200 | [diff] [blame^] | 39 | #define S35390A_FLAG_POC BIT(0) |
| 40 | #define S35390A_FLAG_BLD BIT(1) |
| 41 | #define S35390A_FLAG_INT2 BIT(2) |
| 42 | #define S35390A_FLAG_24H BIT(6) |
| 43 | #define S35390A_FLAG_RESET BIT(7) |
Uwe Kleine-König | 3bd3272 | 2016-07-02 17:28:10 +0200 | [diff] [blame] | 44 | |
| 45 | /* flag for STATUS2 */ |
Richard Leitner | 097aa24 | 2019-05-23 13:54:51 +0200 | [diff] [blame^] | 46 | #define S35390A_FLAG_TEST BIT(0) |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 47 | |
Richard Leitner | a86bd90 | 2019-05-23 13:54:48 +0200 | [diff] [blame] | 48 | /* INT2 pin output mode */ |
| 49 | #define S35390A_INT2_MODE_MASK 0x0E |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 50 | #define S35390A_INT2_MODE_NOINTR 0x00 |
Richard Leitner | a86bd90 | 2019-05-23 13:54:48 +0200 | [diff] [blame] | 51 | #define S35390A_INT2_MODE_ALARM BIT(1) /* INT2AE */ |
| 52 | #define S35390A_INT2_MODE_PMIN_EDG BIT(2) /* INT2ME */ |
| 53 | #define S35390A_INT2_MODE_FREQ BIT(3) /* INT2FE */ |
| 54 | #define S35390A_INT2_MODE_PMIN (BIT(3) | BIT(2)) /* INT2FE | INT2ME */ |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 55 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 56 | static const struct i2c_device_id s35390a_id[] = { |
| 57 | { "s35390a", 0 }, |
| 58 | { } |
| 59 | }; |
| 60 | MODULE_DEVICE_TABLE(i2c, s35390a_id); |
| 61 | |
Javier Martinez Canillas | 21f2743 | 2017-03-03 11:29:21 -0300 | [diff] [blame] | 62 | static const struct of_device_id s35390a_of_match[] = { |
| 63 | { .compatible = "s35390a" }, |
| 64 | { .compatible = "sii,s35390a" }, |
| 65 | { } |
| 66 | }; |
| 67 | MODULE_DEVICE_TABLE(of, s35390a_of_match); |
| 68 | |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 69 | struct s35390a { |
| 70 | struct i2c_client *client[8]; |
| 71 | struct rtc_device *rtc; |
| 72 | int twentyfourhour; |
| 73 | }; |
| 74 | |
| 75 | static int s35390a_set_reg(struct s35390a *s35390a, int reg, char *buf, int len) |
| 76 | { |
| 77 | struct i2c_client *client = s35390a->client[reg]; |
| 78 | struct i2c_msg msg[] = { |
Shubhrajyoti D | 65659f6 | 2012-10-04 17:14:21 -0700 | [diff] [blame] | 79 | { |
| 80 | .addr = client->addr, |
| 81 | .len = len, |
| 82 | .buf = buf |
| 83 | }, |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | if ((i2c_transfer(client->adapter, msg, 1)) != 1) |
| 87 | return -EIO; |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int s35390a_get_reg(struct s35390a *s35390a, int reg, char *buf, int len) |
| 93 | { |
| 94 | struct i2c_client *client = s35390a->client[reg]; |
| 95 | struct i2c_msg msg[] = { |
Shubhrajyoti D | 65659f6 | 2012-10-04 17:14:21 -0700 | [diff] [blame] | 96 | { |
| 97 | .addr = client->addr, |
| 98 | .flags = I2C_M_RD, |
| 99 | .len = len, |
| 100 | .buf = buf |
| 101 | }, |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | if ((i2c_transfer(client->adapter, msg, 1)) != 1) |
| 105 | return -EIO; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Fabien Lahoudere | 16486d0 | 2017-07-05 10:02:29 +0200 | [diff] [blame] | 110 | static int s35390a_init(struct s35390a *s35390a) |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 111 | { |
Nathan Chancellor | ef0f02f | 2018-10-19 13:43:45 -0700 | [diff] [blame] | 112 | u8 buf; |
Uwe Kleine-König | 8e6583f | 2016-07-02 17:28:09 +0200 | [diff] [blame] | 113 | int ret; |
| 114 | unsigned initcount = 0; |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 115 | |
Uwe Kleine-König | 8e6583f | 2016-07-02 17:28:09 +0200 | [diff] [blame] | 116 | /* |
| 117 | * At least one of POC and BLD are set, so reinitialise chip. Keeping |
| 118 | * this information in the hardware to know later that the time isn't |
| 119 | * valid is unfortunately not possible because POC and BLD are cleared |
| 120 | * on read. So the reset is best done now. |
| 121 | * |
| 122 | * The 24H bit is kept over reset, so set it already here. |
| 123 | */ |
| 124 | initialize: |
Uwe Kleine-König | 8e6583f | 2016-07-02 17:28:09 +0200 | [diff] [blame] | 125 | buf = S35390A_FLAG_RESET | S35390A_FLAG_24H; |
| 126 | ret = s35390a_set_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1); |
| 127 | |
| 128 | if (ret < 0) |
| 129 | return ret; |
| 130 | |
| 131 | ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1); |
| 132 | if (ret < 0) |
| 133 | return ret; |
| 134 | |
| 135 | if (buf & (S35390A_FLAG_POC | S35390A_FLAG_BLD)) { |
| 136 | /* Try up to five times to reset the chip */ |
| 137 | if (initcount < 5) { |
| 138 | ++initcount; |
| 139 | goto initialize; |
| 140 | } else |
| 141 | return -EIO; |
| 142 | } |
| 143 | |
| 144 | return 1; |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Fabien Lahoudere | 16486d0 | 2017-07-05 10:02:29 +0200 | [diff] [blame] | 147 | /* |
| 148 | * Returns <0 on error, 0 if rtc is setup fine and 1 if the chip was reset. |
| 149 | * To keep the information if an irq is pending, pass the value read from |
| 150 | * STATUS1 to the caller. |
| 151 | */ |
| 152 | static int s35390a_read_status(struct s35390a *s35390a, char *status1) |
| 153 | { |
| 154 | int ret; |
| 155 | |
| 156 | ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, status1, 1); |
| 157 | if (ret < 0) |
| 158 | return ret; |
| 159 | |
| 160 | if (*status1 & S35390A_FLAG_POC) { |
| 161 | /* |
| 162 | * Do not communicate for 0.5 seconds since the power-on |
| 163 | * detection circuit is in operation. |
| 164 | */ |
| 165 | msleep(500); |
| 166 | return 1; |
| 167 | } else if (*status1 & S35390A_FLAG_BLD) |
| 168 | return 1; |
| 169 | /* |
| 170 | * If both POC and BLD are unset everything is fine. |
| 171 | */ |
| 172 | return 0; |
| 173 | } |
| 174 | |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 175 | static int s35390a_disable_test_mode(struct s35390a *s35390a) |
| 176 | { |
| 177 | char buf[1]; |
| 178 | |
| 179 | if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf)) < 0) |
| 180 | return -EIO; |
| 181 | |
| 182 | if (!(buf[0] & S35390A_FLAG_TEST)) |
| 183 | return 0; |
| 184 | |
| 185 | buf[0] &= ~S35390A_FLAG_TEST; |
| 186 | return s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf)); |
| 187 | } |
| 188 | |
| 189 | static char s35390a_hr2reg(struct s35390a *s35390a, int hour) |
| 190 | { |
| 191 | if (s35390a->twentyfourhour) |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 192 | return bin2bcd(hour); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 193 | |
| 194 | if (hour < 12) |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 195 | return bin2bcd(hour); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 196 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 197 | return 0x40 | bin2bcd(hour - 12); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static int s35390a_reg2hr(struct s35390a *s35390a, char reg) |
| 201 | { |
| 202 | unsigned hour; |
| 203 | |
| 204 | if (s35390a->twentyfourhour) |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 205 | return bcd2bin(reg & 0x3f); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 206 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 207 | hour = bcd2bin(reg & 0x3f); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 208 | if (reg & 0x40) |
| 209 | hour += 12; |
| 210 | |
| 211 | return hour; |
| 212 | } |
| 213 | |
Alexandre Belloni | 779e1aa | 2018-02-21 14:00:18 +0100 | [diff] [blame] | 214 | static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm) |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 215 | { |
Alexandre Belloni | 779e1aa | 2018-02-21 14:00:18 +0100 | [diff] [blame] | 216 | struct i2c_client *client = to_i2c_client(dev); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 217 | struct s35390a *s35390a = i2c_get_clientdata(client); |
| 218 | int i, err; |
Fabien Lahoudere | 16486d0 | 2017-07-05 10:02:29 +0200 | [diff] [blame] | 219 | char buf[7], status; |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 220 | |
| 221 | dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d mday=%d, " |
| 222 | "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec, |
| 223 | tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year, |
| 224 | tm->tm_wday); |
| 225 | |
Fabien Lahoudere | 16486d0 | 2017-07-05 10:02:29 +0200 | [diff] [blame] | 226 | if (s35390a_read_status(s35390a, &status) == 1) |
| 227 | s35390a_init(s35390a); |
| 228 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 229 | buf[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 100); |
| 230 | buf[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon + 1); |
| 231 | buf[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday); |
| 232 | buf[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 233 | buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour); |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 234 | buf[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min); |
| 235 | buf[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 236 | |
| 237 | /* This chip expects the bits of each byte to be in reverse order */ |
| 238 | for (i = 0; i < 7; ++i) |
| 239 | buf[i] = bitrev8(buf[i]); |
| 240 | |
| 241 | err = s35390a_set_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf)); |
| 242 | |
| 243 | return err; |
| 244 | } |
| 245 | |
Alexandre Belloni | 779e1aa | 2018-02-21 14:00:18 +0100 | [diff] [blame] | 246 | static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm) |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 247 | { |
Alexandre Belloni | 779e1aa | 2018-02-21 14:00:18 +0100 | [diff] [blame] | 248 | struct i2c_client *client = to_i2c_client(dev); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 249 | struct s35390a *s35390a = i2c_get_clientdata(client); |
Fabien Lahoudere | 16486d0 | 2017-07-05 10:02:29 +0200 | [diff] [blame] | 250 | char buf[7], status; |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 251 | int i, err; |
| 252 | |
Fabien Lahoudere | 16486d0 | 2017-07-05 10:02:29 +0200 | [diff] [blame] | 253 | if (s35390a_read_status(s35390a, &status) == 1) |
| 254 | return -EINVAL; |
| 255 | |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 256 | err = s35390a_get_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf)); |
| 257 | if (err < 0) |
| 258 | return err; |
| 259 | |
| 260 | /* This chip returns the bits of each byte in reverse order */ |
| 261 | for (i = 0; i < 7; ++i) |
| 262 | buf[i] = bitrev8(buf[i]); |
| 263 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 264 | tm->tm_sec = bcd2bin(buf[S35390A_BYTE_SECS]); |
| 265 | tm->tm_min = bcd2bin(buf[S35390A_BYTE_MINS]); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 266 | tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]); |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 267 | tm->tm_wday = bcd2bin(buf[S35390A_BYTE_WDAY]); |
| 268 | tm->tm_mday = bcd2bin(buf[S35390A_BYTE_DAY]); |
| 269 | tm->tm_mon = bcd2bin(buf[S35390A_BYTE_MONTH]) - 1; |
| 270 | tm->tm_year = bcd2bin(buf[S35390A_BYTE_YEAR]) + 100; |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 271 | |
| 272 | dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, " |
| 273 | "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec, |
| 274 | tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year, |
| 275 | tm->tm_wday); |
| 276 | |
Alexandre Belloni | bb53019 | 2018-02-21 13:54:46 +0100 | [diff] [blame] | 277 | return 0; |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Alexandre Belloni | 779e1aa | 2018-02-21 14:00:18 +0100 | [diff] [blame] | 280 | static int s35390a_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 281 | { |
Alexandre Belloni | 779e1aa | 2018-02-21 14:00:18 +0100 | [diff] [blame] | 282 | struct i2c_client *client = to_i2c_client(dev); |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 283 | struct s35390a *s35390a = i2c_get_clientdata(client); |
| 284 | char buf[3], sts = 0; |
| 285 | int err, i; |
| 286 | |
| 287 | dev_dbg(&client->dev, "%s: alm is secs=%d, mins=%d, hours=%d mday=%d, "\ |
| 288 | "mon=%d, year=%d, wday=%d\n", __func__, alm->time.tm_sec, |
| 289 | alm->time.tm_min, alm->time.tm_hour, alm->time.tm_mday, |
| 290 | alm->time.tm_mon, alm->time.tm_year, alm->time.tm_wday); |
| 291 | |
Richard Leitner | c0e1284 | 2019-05-23 13:54:49 +0200 | [diff] [blame] | 292 | if (alm->time.tm_sec != 0) |
| 293 | dev_warn(&client->dev, "Alarms are only supported on a per minute basis!\n"); |
| 294 | |
Uwe Kleine-König | 5227e8a | 2016-07-02 17:28:11 +0200 | [diff] [blame] | 295 | /* disable interrupt (which deasserts the irq line) */ |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 296 | err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts)); |
| 297 | if (err < 0) |
| 298 | return err; |
| 299 | |
Uwe Kleine-König | 5227e8a | 2016-07-02 17:28:11 +0200 | [diff] [blame] | 300 | /* clear pending interrupt (in STATUS1 only), if any */ |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 301 | err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &sts, sizeof(sts)); |
| 302 | if (err < 0) |
| 303 | return err; |
| 304 | |
| 305 | if (alm->enabled) |
| 306 | sts = S35390A_INT2_MODE_ALARM; |
| 307 | else |
| 308 | sts = S35390A_INT2_MODE_NOINTR; |
| 309 | |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 310 | /* set interupt mode*/ |
| 311 | err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts)); |
| 312 | if (err < 0) |
| 313 | return err; |
| 314 | |
| 315 | if (alm->time.tm_wday != -1) |
| 316 | buf[S35390A_ALRM_BYTE_WDAY] = bin2bcd(alm->time.tm_wday) | 0x80; |
Uwe Kleine-König | f87e904 | 2016-07-02 17:28:08 +0200 | [diff] [blame] | 317 | else |
| 318 | buf[S35390A_ALRM_BYTE_WDAY] = 0; |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 319 | |
| 320 | buf[S35390A_ALRM_BYTE_HOURS] = s35390a_hr2reg(s35390a, |
| 321 | alm->time.tm_hour) | 0x80; |
| 322 | buf[S35390A_ALRM_BYTE_MINS] = bin2bcd(alm->time.tm_min) | 0x80; |
| 323 | |
| 324 | if (alm->time.tm_hour >= 12) |
| 325 | buf[S35390A_ALRM_BYTE_HOURS] |= 0x40; |
| 326 | |
| 327 | for (i = 0; i < 3; ++i) |
| 328 | buf[i] = bitrev8(buf[i]); |
| 329 | |
| 330 | err = s35390a_set_reg(s35390a, S35390A_CMD_INT2_REG1, buf, |
| 331 | sizeof(buf)); |
| 332 | |
| 333 | return err; |
| 334 | } |
| 335 | |
Alexandre Belloni | 779e1aa | 2018-02-21 14:00:18 +0100 | [diff] [blame] | 336 | static int s35390a_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 337 | { |
Alexandre Belloni | 779e1aa | 2018-02-21 14:00:18 +0100 | [diff] [blame] | 338 | struct i2c_client *client = to_i2c_client(dev); |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 339 | struct s35390a *s35390a = i2c_get_clientdata(client); |
| 340 | char buf[3], sts; |
| 341 | int i, err; |
| 342 | |
| 343 | err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts)); |
| 344 | if (err < 0) |
| 345 | return err; |
| 346 | |
Richard Leitner | a86bd90 | 2019-05-23 13:54:48 +0200 | [diff] [blame] | 347 | if ((sts & S35390A_INT2_MODE_MASK) != S35390A_INT2_MODE_ALARM) { |
Uwe Kleine-König | f87e904 | 2016-07-02 17:28:08 +0200 | [diff] [blame] | 348 | /* |
| 349 | * When the alarm isn't enabled, the register to configure |
| 350 | * the alarm time isn't accessible. |
| 351 | */ |
| 352 | alm->enabled = 0; |
| 353 | return 0; |
| 354 | } else { |
| 355 | alm->enabled = 1; |
| 356 | } |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 357 | |
| 358 | err = s35390a_get_reg(s35390a, S35390A_CMD_INT2_REG1, buf, sizeof(buf)); |
| 359 | if (err < 0) |
| 360 | return err; |
| 361 | |
| 362 | /* This chip returns the bits of each byte in reverse order */ |
Uwe Kleine-König | f87e904 | 2016-07-02 17:28:08 +0200 | [diff] [blame] | 363 | for (i = 0; i < 3; ++i) |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 364 | buf[i] = bitrev8(buf[i]); |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 365 | |
Uwe Kleine-König | f87e904 | 2016-07-02 17:28:08 +0200 | [diff] [blame] | 366 | /* |
| 367 | * B0 of the three matching registers is an enable flag. Iff it is set |
| 368 | * the configured value is used for matching. |
| 369 | */ |
| 370 | if (buf[S35390A_ALRM_BYTE_WDAY] & 0x80) |
| 371 | alm->time.tm_wday = |
| 372 | bcd2bin(buf[S35390A_ALRM_BYTE_WDAY] & ~0x80); |
| 373 | |
| 374 | if (buf[S35390A_ALRM_BYTE_HOURS] & 0x80) |
| 375 | alm->time.tm_hour = |
| 376 | s35390a_reg2hr(s35390a, |
| 377 | buf[S35390A_ALRM_BYTE_HOURS] & ~0x80); |
| 378 | |
| 379 | if (buf[S35390A_ALRM_BYTE_MINS] & 0x80) |
| 380 | alm->time.tm_min = bcd2bin(buf[S35390A_ALRM_BYTE_MINS] & ~0x80); |
| 381 | |
| 382 | /* alarm triggers always at s=0 */ |
| 383 | alm->time.tm_sec = 0; |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 384 | |
| 385 | dev_dbg(&client->dev, "%s: alm is mins=%d, hours=%d, wday=%d\n", |
| 386 | __func__, alm->time.tm_min, alm->time.tm_hour, |
| 387 | alm->time.tm_wday); |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
Fabien Lahoudere | 7a1fe40 | 2017-07-05 10:02:30 +0200 | [diff] [blame] | 392 | static int s35390a_rtc_ioctl(struct device *dev, unsigned int cmd, |
| 393 | unsigned long arg) |
| 394 | { |
| 395 | struct i2c_client *client = to_i2c_client(dev); |
| 396 | struct s35390a *s35390a = i2c_get_clientdata(client); |
| 397 | char sts; |
| 398 | int err; |
| 399 | |
| 400 | switch (cmd) { |
| 401 | case RTC_VL_READ: |
| 402 | /* s35390a_reset set lowvoltage flag and init RTC if needed */ |
| 403 | err = s35390a_read_status(s35390a, &sts); |
| 404 | if (err < 0) |
| 405 | return err; |
| 406 | if (copy_to_user((void __user *)arg, &err, sizeof(int))) |
| 407 | return -EFAULT; |
| 408 | break; |
| 409 | case RTC_VL_CLR: |
| 410 | /* update flag and clear register */ |
| 411 | err = s35390a_init(s35390a); |
| 412 | if (err < 0) |
| 413 | return err; |
| 414 | break; |
| 415 | default: |
| 416 | return -ENOIOCTLCMD; |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 422 | static const struct rtc_class_ops s35390a_rtc_ops = { |
| 423 | .read_time = s35390a_rtc_read_time, |
| 424 | .set_time = s35390a_rtc_set_time, |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 425 | .set_alarm = s35390a_rtc_set_alarm, |
| 426 | .read_alarm = s35390a_rtc_read_alarm, |
Fabien Lahoudere | 7a1fe40 | 2017-07-05 10:02:30 +0200 | [diff] [blame] | 427 | .ioctl = s35390a_rtc_ioctl, |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 428 | }; |
| 429 | |
| 430 | static struct i2c_driver s35390a_driver; |
| 431 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 432 | static int s35390a_probe(struct i2c_client *client, |
| 433 | const struct i2c_device_id *id) |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 434 | { |
Fabien Lahoudere | 16486d0 | 2017-07-05 10:02:29 +0200 | [diff] [blame] | 435 | int err, err_read; |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 436 | unsigned int i; |
| 437 | struct s35390a *s35390a; |
Uwe Kleine-König | 3bd3272 | 2016-07-02 17:28:10 +0200 | [diff] [blame] | 438 | char buf, status1; |
Richard Leitner | 0327963 | 2019-05-23 13:54:50 +0200 | [diff] [blame] | 439 | struct device *dev = &client->dev; |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 440 | |
| 441 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 442 | err = -ENODEV; |
| 443 | goto exit; |
| 444 | } |
| 445 | |
Richard Leitner | 0327963 | 2019-05-23 13:54:50 +0200 | [diff] [blame] | 446 | s35390a = devm_kzalloc(dev, sizeof(struct s35390a), GFP_KERNEL); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 447 | if (!s35390a) { |
| 448 | err = -ENOMEM; |
| 449 | goto exit; |
| 450 | } |
| 451 | |
| 452 | s35390a->client[0] = client; |
| 453 | i2c_set_clientdata(client, s35390a); |
| 454 | |
| 455 | /* This chip uses multiple addresses, use dummy devices for them */ |
| 456 | for (i = 1; i < 8; ++i) { |
| 457 | s35390a->client[i] = i2c_new_dummy(client->adapter, |
Jean Delvare | 60b129d | 2008-05-11 20:37:06 +0200 | [diff] [blame] | 458 | client->addr + i); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 459 | if (!s35390a->client[i]) { |
Richard Leitner | 0327963 | 2019-05-23 13:54:50 +0200 | [diff] [blame] | 460 | dev_err(dev, "Address %02x unavailable\n", |
| 461 | client->addr + i); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 462 | err = -EBUSY; |
| 463 | goto exit_dummy; |
| 464 | } |
| 465 | } |
| 466 | |
Fabien Lahoudere | 16486d0 | 2017-07-05 10:02:29 +0200 | [diff] [blame] | 467 | err_read = s35390a_read_status(s35390a, &status1); |
| 468 | if (err_read < 0) { |
| 469 | err = err_read; |
Richard Leitner | 0327963 | 2019-05-23 13:54:50 +0200 | [diff] [blame] | 470 | dev_err(dev, "error resetting chip\n"); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 471 | goto exit_dummy; |
| 472 | } |
| 473 | |
Uwe Kleine-König | 3bd3272 | 2016-07-02 17:28:10 +0200 | [diff] [blame] | 474 | if (status1 & S35390A_FLAG_24H) |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 475 | s35390a->twentyfourhour = 1; |
| 476 | else |
| 477 | s35390a->twentyfourhour = 0; |
| 478 | |
Uwe Kleine-König | 3bd3272 | 2016-07-02 17:28:10 +0200 | [diff] [blame] | 479 | if (status1 & S35390A_FLAG_INT2) { |
| 480 | /* disable alarm (and maybe test mode) */ |
| 481 | buf = 0; |
| 482 | err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &buf, 1); |
| 483 | if (err < 0) { |
Richard Leitner | 0327963 | 2019-05-23 13:54:50 +0200 | [diff] [blame] | 484 | dev_err(dev, "error disabling alarm"); |
Uwe Kleine-König | 3bd3272 | 2016-07-02 17:28:10 +0200 | [diff] [blame] | 485 | goto exit_dummy; |
| 486 | } |
| 487 | } else { |
| 488 | err = s35390a_disable_test_mode(s35390a); |
| 489 | if (err < 0) { |
Richard Leitner | 0327963 | 2019-05-23 13:54:50 +0200 | [diff] [blame] | 490 | dev_err(dev, "error disabling test mode\n"); |
Uwe Kleine-König | 3bd3272 | 2016-07-02 17:28:10 +0200 | [diff] [blame] | 491 | goto exit_dummy; |
| 492 | } |
| 493 | } |
| 494 | |
Richard Leitner | 0327963 | 2019-05-23 13:54:50 +0200 | [diff] [blame] | 495 | device_set_wakeup_capable(dev, 1); |
Michael Langer | 542dd33 | 2012-10-04 17:14:37 -0700 | [diff] [blame] | 496 | |
Richard Leitner | 0327963 | 2019-05-23 13:54:50 +0200 | [diff] [blame] | 497 | s35390a->rtc = devm_rtc_device_register(dev, s35390a_driver.driver.name, |
| 498 | &s35390a_rtc_ops, THIS_MODULE); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 499 | |
| 500 | if (IS_ERR(s35390a->rtc)) { |
| 501 | err = PTR_ERR(s35390a->rtc); |
| 502 | goto exit_dummy; |
| 503 | } |
Uwe Kleine-König | 3bd3272 | 2016-07-02 17:28:10 +0200 | [diff] [blame] | 504 | |
Richard Leitner | c0e1284 | 2019-05-23 13:54:49 +0200 | [diff] [blame] | 505 | /* supports per-minute alarms only, therefore set uie_unsupported */ |
| 506 | s35390a->rtc->uie_unsupported = 1; |
| 507 | |
Uwe Kleine-König | 3bd3272 | 2016-07-02 17:28:10 +0200 | [diff] [blame] | 508 | if (status1 & S35390A_FLAG_INT2) |
| 509 | rtc_update_irq(s35390a->rtc, 1, RTC_AF); |
| 510 | |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 511 | return 0; |
| 512 | |
| 513 | exit_dummy: |
| 514 | for (i = 1; i < 8; ++i) |
| 515 | if (s35390a->client[i]) |
| 516 | i2c_unregister_device(s35390a->client[i]); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 517 | |
| 518 | exit: |
| 519 | return err; |
| 520 | } |
| 521 | |
| 522 | static int s35390a_remove(struct i2c_client *client) |
| 523 | { |
| 524 | unsigned int i; |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 525 | struct s35390a *s35390a = i2c_get_clientdata(client); |
Jingoo Han | b4cd3d6 | 2013-04-29 16:20:53 -0700 | [diff] [blame] | 526 | |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 527 | for (i = 1; i < 8; ++i) |
| 528 | if (s35390a->client[i]) |
| 529 | i2c_unregister_device(s35390a->client[i]); |
| 530 | |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static struct i2c_driver s35390a_driver = { |
| 535 | .driver = { |
| 536 | .name = "rtc-s35390a", |
Javier Martinez Canillas | 21f2743 | 2017-03-03 11:29:21 -0300 | [diff] [blame] | 537 | .of_match_table = of_match_ptr(s35390a_of_match), |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 538 | }, |
| 539 | .probe = s35390a_probe, |
| 540 | .remove = s35390a_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 541 | .id_table = s35390a_id, |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 542 | }; |
| 543 | |
Axel Lin | 0abc920 | 2012-03-23 15:02:31 -0700 | [diff] [blame] | 544 | module_i2c_driver(s35390a_driver); |
Byron Bradley | c46288b | 2008-03-04 14:28:25 -0800 | [diff] [blame] | 545 | |
| 546 | MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>"); |
| 547 | MODULE_DESCRIPTION("S35390A RTC driver"); |
| 548 | MODULE_LICENSE("GPL"); |