Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Driver for Epson's RTC module RX-8025 SA/NB |
| 4 | * |
| 5 | * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com> |
| 6 | * |
| 7 | * Copyright (C) 2005 by Digi International Inc. |
| 8 | * All rights reserved. |
| 9 | * |
| 10 | * Modified by fengjh at rising.com.cn |
Leslie Lau | d00cd81 | 2016-01-28 21:17:22 -0800 | [diff] [blame] | 11 | * <lm-sensors@lm-sensors.org> |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 12 | * 2006.11 |
| 13 | * |
| 14 | * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com> |
| 15 | * Converted to new style by Wolfgang Grandegger <wg@grandegger.com> |
| 16 | * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com> |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 17 | */ |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 18 | #include <linux/bcd.h> |
Alexandre Belloni | 2ddd186 | 2015-07-25 11:50:22 +0200 | [diff] [blame] | 19 | #include <linux/bitops.h> |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 20 | #include <linux/i2c.h> |
Alexandre Belloni | 15d3bdc | 2015-07-24 15:50:23 +0200 | [diff] [blame] | 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 23 | #include <linux/rtc.h> |
| 24 | |
| 25 | /* Register definitions */ |
| 26 | #define RX8025_REG_SEC 0x00 |
| 27 | #define RX8025_REG_MIN 0x01 |
| 28 | #define RX8025_REG_HOUR 0x02 |
| 29 | #define RX8025_REG_WDAY 0x03 |
| 30 | #define RX8025_REG_MDAY 0x04 |
| 31 | #define RX8025_REG_MONTH 0x05 |
| 32 | #define RX8025_REG_YEAR 0x06 |
| 33 | #define RX8025_REG_DIGOFF 0x07 |
| 34 | #define RX8025_REG_ALWMIN 0x08 |
| 35 | #define RX8025_REG_ALWHOUR 0x09 |
| 36 | #define RX8025_REG_ALWWDAY 0x0a |
| 37 | #define RX8025_REG_ALDMIN 0x0b |
| 38 | #define RX8025_REG_ALDHOUR 0x0c |
| 39 | /* 0x0d is reserved */ |
| 40 | #define RX8025_REG_CTRL1 0x0e |
| 41 | #define RX8025_REG_CTRL2 0x0f |
| 42 | |
| 43 | #define RX8025_BIT_CTRL1_CT (7 << 0) |
| 44 | /* 1 Hz periodic level irq */ |
| 45 | #define RX8025_BIT_CTRL1_CT_1HZ 4 |
Alexandre Belloni | 2ddd186 | 2015-07-25 11:50:22 +0200 | [diff] [blame] | 46 | #define RX8025_BIT_CTRL1_TEST BIT(3) |
| 47 | #define RX8025_BIT_CTRL1_1224 BIT(5) |
| 48 | #define RX8025_BIT_CTRL1_DALE BIT(6) |
| 49 | #define RX8025_BIT_CTRL1_WALE BIT(7) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 50 | |
Alexandre Belloni | 2ddd186 | 2015-07-25 11:50:22 +0200 | [diff] [blame] | 51 | #define RX8025_BIT_CTRL2_DAFG BIT(0) |
| 52 | #define RX8025_BIT_CTRL2_WAFG BIT(1) |
| 53 | #define RX8025_BIT_CTRL2_CTFG BIT(2) |
| 54 | #define RX8025_BIT_CTRL2_PON BIT(4) |
| 55 | #define RX8025_BIT_CTRL2_XST BIT(5) |
| 56 | #define RX8025_BIT_CTRL2_VDET BIT(6) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 57 | |
| 58 | /* Clock precision adjustment */ |
| 59 | #define RX8025_ADJ_RESOLUTION 3050 /* in ppb */ |
| 60 | #define RX8025_ADJ_DATA_MAX 62 |
| 61 | #define RX8025_ADJ_DATA_MIN -62 |
| 62 | |
| 63 | static const struct i2c_device_id rx8025_id[] = { |
| 64 | { "rx8025", 0 }, |
| 65 | { } |
| 66 | }; |
| 67 | MODULE_DEVICE_TABLE(i2c, rx8025_id); |
| 68 | |
| 69 | struct rx8025_data { |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 70 | struct rtc_device *rtc; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 71 | u8 ctrl1; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 74 | static s32 rx8025_read_reg(const struct i2c_client *client, u8 number) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 75 | { |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 76 | return i2c_smbus_read_byte_data(client, number << 4); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 79 | static int rx8025_read_regs(const struct i2c_client *client, |
| 80 | u8 number, u8 length, u8 *values) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 81 | { |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 82 | int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length, |
| 83 | values); |
| 84 | if (ret != length) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 85 | return ret < 0 ? ret : -EIO; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 90 | static s32 rx8025_write_reg(const struct i2c_client *client, u8 number, |
| 91 | u8 value) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 92 | { |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 93 | return i2c_smbus_write_byte_data(client, number << 4, value); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 96 | static s32 rx8025_write_regs(const struct i2c_client *client, |
| 97 | u8 number, u8 length, const u8 *values) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 98 | { |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 99 | return i2c_smbus_write_i2c_block_data(client, number << 4, |
| 100 | length, values); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Alexandre Belloni | efbbb4f | 2015-08-04 11:33:59 +0200 | [diff] [blame] | 103 | static int rx8025_check_validity(struct device *dev) |
| 104 | { |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 105 | struct i2c_client *client = to_i2c_client(dev); |
Alexandre Belloni | efbbb4f | 2015-08-04 11:33:59 +0200 | [diff] [blame] | 106 | int ctrl2; |
| 107 | |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 108 | ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2); |
Alexandre Belloni | efbbb4f | 2015-08-04 11:33:59 +0200 | [diff] [blame] | 109 | if (ctrl2 < 0) |
| 110 | return ctrl2; |
| 111 | |
| 112 | if (ctrl2 & RX8025_BIT_CTRL2_VDET) |
| 113 | dev_warn(dev, "power voltage drop detected\n"); |
| 114 | |
| 115 | if (ctrl2 & RX8025_BIT_CTRL2_PON) { |
| 116 | dev_warn(dev, "power-on reset detected, date is invalid\n"); |
| 117 | return -EINVAL; |
| 118 | } |
| 119 | |
| 120 | if (!(ctrl2 & RX8025_BIT_CTRL2_XST)) { |
| 121 | dev_warn(dev, "crystal stopped, date is invalid\n"); |
| 122 | return -EINVAL; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Alexandre Belloni | 8c4a446 | 2015-08-04 10:48:20 +0200 | [diff] [blame] | 128 | static int rx8025_reset_validity(struct i2c_client *client) |
| 129 | { |
| 130 | int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2); |
| 131 | |
| 132 | if (ctrl2 < 0) |
| 133 | return ctrl2; |
| 134 | |
| 135 | ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET); |
| 136 | |
| 137 | return rx8025_write_reg(client, RX8025_REG_CTRL2, |
| 138 | ctrl2 | RX8025_BIT_CTRL2_XST); |
| 139 | } |
| 140 | |
Alexandre Belloni | b6a57c9 | 2015-07-24 15:59:43 +0200 | [diff] [blame] | 141 | static irqreturn_t rx8025_handle_irq(int irq, void *dev_id) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 142 | { |
| 143 | struct i2c_client *client = dev_id; |
| 144 | struct rx8025_data *rx8025 = i2c_get_clientdata(client); |
Akinobu Mita | 9dbe385 | 2016-02-15 23:49:07 +0900 | [diff] [blame] | 145 | struct mutex *lock = &rx8025->rtc->ops_lock; |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 146 | int status; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 147 | |
Akinobu Mita | 9dbe385 | 2016-02-15 23:49:07 +0900 | [diff] [blame] | 148 | mutex_lock(lock); |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 149 | status = rx8025_read_reg(client, RX8025_REG_CTRL2); |
| 150 | if (status < 0) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 151 | goto out; |
| 152 | |
| 153 | if (!(status & RX8025_BIT_CTRL2_XST)) |
| 154 | dev_warn(&client->dev, "Oscillation stop was detected," |
| 155 | "you may have to readjust the clock\n"); |
| 156 | |
| 157 | if (status & RX8025_BIT_CTRL2_CTFG) { |
| 158 | /* periodic */ |
| 159 | status &= ~RX8025_BIT_CTRL2_CTFG; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 160 | rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | if (status & RX8025_BIT_CTRL2_DAFG) { |
| 164 | /* alarm */ |
| 165 | status &= RX8025_BIT_CTRL2_DAFG; |
| 166 | if (rx8025_write_reg(client, RX8025_REG_CTRL1, |
| 167 | rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE)) |
| 168 | goto out; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 169 | rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 172 | out: |
Akinobu Mita | 9dbe385 | 2016-02-15 23:49:07 +0900 | [diff] [blame] | 173 | mutex_unlock(lock); |
| 174 | |
Alexandre Belloni | b6a57c9 | 2015-07-24 15:59:43 +0200 | [diff] [blame] | 175 | return IRQ_HANDLED; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static int rx8025_get_time(struct device *dev, struct rtc_time *dt) |
| 179 | { |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 180 | struct i2c_client *client = to_i2c_client(dev); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 181 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 182 | u8 date[7]; |
Alexandre Belloni | efbbb4f | 2015-08-04 11:33:59 +0200 | [diff] [blame] | 183 | int err; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 184 | |
Alexandre Belloni | efbbb4f | 2015-08-04 11:33:59 +0200 | [diff] [blame] | 185 | err = rx8025_check_validity(dev); |
| 186 | if (err) |
| 187 | return err; |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 188 | |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 189 | err = rx8025_read_regs(client, RX8025_REG_SEC, 7, date); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 190 | if (err) |
| 191 | return err; |
| 192 | |
Andy Shevchenko | 1921cab | 2018-12-04 23:23:24 +0200 | [diff] [blame] | 193 | dev_dbg(dev, "%s: read %7ph\n", __func__, date); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 194 | |
| 195 | dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f); |
| 196 | dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f); |
| 197 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) |
| 198 | dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f); |
| 199 | else |
| 200 | dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12 |
| 201 | + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0); |
| 202 | |
| 203 | dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f); |
| 204 | dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1; |
Alexandre Belloni | 32672c5 | 2015-07-25 12:07:37 +0200 | [diff] [blame] | 205 | dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 206 | |
Andy Shevchenko | 1921cab | 2018-12-04 23:23:24 +0200 | [diff] [blame] | 207 | dev_dbg(dev, "%s: date %ptRr\n", __func__, dt); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 208 | |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 209 | return 0; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static int rx8025_set_time(struct device *dev, struct rtc_time *dt) |
| 213 | { |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 214 | struct i2c_client *client = to_i2c_client(dev); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 215 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
| 216 | u8 date[7]; |
Alexandre Belloni | 8c4a446 | 2015-08-04 10:48:20 +0200 | [diff] [blame] | 217 | int ret; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 218 | |
Alexandre Belloni | 32672c5 | 2015-07-25 12:07:37 +0200 | [diff] [blame] | 219 | if ((dt->tm_year < 100) || (dt->tm_year > 199)) |
| 220 | return -EINVAL; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * Here the read-only bits are written as "0". I'm not sure if that |
| 224 | * is sound. |
| 225 | */ |
| 226 | date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec); |
| 227 | date[RX8025_REG_MIN] = bin2bcd(dt->tm_min); |
| 228 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) |
| 229 | date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour); |
| 230 | else |
| 231 | date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0) |
| 232 | | bin2bcd((dt->tm_hour + 11) % 12 + 1); |
| 233 | |
| 234 | date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday); |
| 235 | date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday); |
| 236 | date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1); |
Alexandre Belloni | 32672c5 | 2015-07-25 12:07:37 +0200 | [diff] [blame] | 237 | date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 238 | |
Andy Shevchenko | 1921cab | 2018-12-04 23:23:24 +0200 | [diff] [blame] | 239 | dev_dbg(dev, "%s: write %7ph\n", __func__, date); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 240 | |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 241 | ret = rx8025_write_regs(client, RX8025_REG_SEC, 7, date); |
Alexandre Belloni | 8c4a446 | 2015-08-04 10:48:20 +0200 | [diff] [blame] | 242 | if (ret < 0) |
| 243 | return ret; |
| 244 | |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 245 | return rx8025_reset_validity(client); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 248 | static int rx8025_init_client(struct i2c_client *client) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 249 | { |
| 250 | struct rx8025_data *rx8025 = i2c_get_clientdata(client); |
| 251 | u8 ctrl[2], ctrl2; |
| 252 | int need_clear = 0; |
| 253 | int err; |
| 254 | |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 255 | err = rx8025_read_regs(client, RX8025_REG_CTRL1, 2, ctrl); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 256 | if (err) |
| 257 | goto out; |
| 258 | |
| 259 | /* Keep test bit zero ! */ |
| 260 | rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST; |
| 261 | |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 262 | if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) { |
| 263 | dev_warn(&client->dev, "Alarm was detected\n"); |
| 264 | need_clear = 1; |
| 265 | } |
| 266 | |
Alexandre Belloni | 5c66e1e | 2015-08-04 11:24:33 +0200 | [diff] [blame] | 267 | if (ctrl[1] & RX8025_BIT_CTRL2_CTFG) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 268 | need_clear = 1; |
| 269 | |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 270 | if (need_clear) { |
Alexandre Belloni | a27c7bf | 2015-08-04 10:46:22 +0200 | [diff] [blame] | 271 | ctrl2 = ctrl[1]; |
Alexandre Belloni | 8c4a446 | 2015-08-04 10:48:20 +0200 | [diff] [blame] | 272 | ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG | |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 273 | RX8025_BIT_CTRL2_DAFG); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 274 | |
| 275 | err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2); |
| 276 | } |
| 277 | out: |
| 278 | return err; |
| 279 | } |
| 280 | |
| 281 | /* Alarm support */ |
| 282 | static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 283 | { |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 284 | struct i2c_client *client = to_i2c_client(dev); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 285 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 286 | u8 ald[2]; |
| 287 | int ctrl2, err; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 288 | |
| 289 | if (client->irq <= 0) |
| 290 | return -EINVAL; |
| 291 | |
| 292 | err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald); |
| 293 | if (err) |
| 294 | return err; |
| 295 | |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 296 | ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2); |
| 297 | if (ctrl2 < 0) |
| 298 | return ctrl2; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 299 | |
| 300 | dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n", |
| 301 | __func__, ald[0], ald[1], ctrl2); |
| 302 | |
| 303 | /* Hardware alarms precision is 1 minute! */ |
| 304 | t->time.tm_sec = 0; |
| 305 | t->time.tm_min = bcd2bin(ald[0] & 0x7f); |
| 306 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) |
| 307 | t->time.tm_hour = bcd2bin(ald[1] & 0x3f); |
| 308 | else |
| 309 | t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12 |
| 310 | + (ald[1] & 0x20 ? 12 : 0); |
| 311 | |
Andy Shevchenko | 862cac1 | 2019-04-10 17:05:59 +0300 | [diff] [blame] | 312 | dev_dbg(dev, "%s: date: %ptRr\n", __func__, &t->time); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 313 | t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE); |
| 314 | t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled; |
| 315 | |
| 316 | return err; |
| 317 | } |
| 318 | |
| 319 | static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 320 | { |
| 321 | struct i2c_client *client = to_i2c_client(dev); |
| 322 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
| 323 | u8 ald[2]; |
| 324 | int err; |
| 325 | |
| 326 | if (client->irq <= 0) |
| 327 | return -EINVAL; |
| 328 | |
Akinobu Mita | 302c560 | 2016-02-15 23:49:08 +0900 | [diff] [blame] | 329 | /* |
| 330 | * Hardware alarm precision is 1 minute! |
| 331 | * round up to nearest minute |
| 332 | */ |
| 333 | if (t->time.tm_sec) { |
| 334 | time64_t alarm_time = rtc_tm_to_time64(&t->time); |
| 335 | |
| 336 | alarm_time += 60 - t->time.tm_sec; |
| 337 | rtc_time64_to_tm(alarm_time, &t->time); |
| 338 | } |
| 339 | |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 340 | ald[0] = bin2bcd(t->time.tm_min); |
| 341 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) |
| 342 | ald[1] = bin2bcd(t->time.tm_hour); |
| 343 | else |
| 344 | ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0) |
| 345 | | bin2bcd((t->time.tm_hour + 11) % 12 + 1); |
| 346 | |
| 347 | dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]); |
| 348 | |
| 349 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) { |
| 350 | rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE; |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 351 | err = rx8025_write_reg(client, RX8025_REG_CTRL1, |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 352 | rx8025->ctrl1); |
| 353 | if (err) |
| 354 | return err; |
| 355 | } |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 356 | err = rx8025_write_regs(client, RX8025_REG_ALDMIN, 2, ald); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 357 | if (err) |
| 358 | return err; |
| 359 | |
| 360 | if (t->enabled) { |
| 361 | rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE; |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 362 | err = rx8025_write_reg(client, RX8025_REG_CTRL1, |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 363 | rx8025->ctrl1); |
| 364 | if (err) |
| 365 | return err; |
| 366 | } |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 372 | { |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 373 | struct i2c_client *client = to_i2c_client(dev); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 374 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
| 375 | u8 ctrl1; |
| 376 | int err; |
| 377 | |
| 378 | ctrl1 = rx8025->ctrl1; |
| 379 | if (enabled) |
| 380 | ctrl1 |= RX8025_BIT_CTRL1_DALE; |
| 381 | else |
| 382 | ctrl1 &= ~RX8025_BIT_CTRL1_DALE; |
| 383 | |
| 384 | if (ctrl1 != rx8025->ctrl1) { |
| 385 | rx8025->ctrl1 = ctrl1; |
Nobuhiro Iwamatsu | 47a3c04 | 2019-12-18 17:16:24 +0900 | [diff] [blame] | 386 | err = rx8025_write_reg(client, RX8025_REG_CTRL1, |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 387 | rx8025->ctrl1); |
| 388 | if (err) |
| 389 | return err; |
| 390 | } |
| 391 | return 0; |
| 392 | } |
| 393 | |
Julia Lawall | 34c7b3a | 2016-08-31 10:05:25 +0200 | [diff] [blame] | 394 | static const struct rtc_class_ops rx8025_rtc_ops = { |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 395 | .read_time = rx8025_get_time, |
| 396 | .set_time = rx8025_set_time, |
| 397 | .read_alarm = rx8025_read_alarm, |
| 398 | .set_alarm = rx8025_set_alarm, |
| 399 | .alarm_irq_enable = rx8025_alarm_irq_enable, |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | /* |
| 403 | * Clock precision adjustment support |
| 404 | * |
| 405 | * According to the RX8025 SA/NB application manual the frequency and |
Thomas Weber | b770ffd | 2010-07-19 08:22:43 +0200 | [diff] [blame] | 406 | * temperature characteristics can be approximated using the following |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 407 | * equation: |
| 408 | * |
| 409 | * df = a * (ut - t)**2 |
| 410 | * |
| 411 | * df: Frequency deviation in any temperature |
| 412 | * a : Coefficient = (-35 +-5) * 10**-9 |
| 413 | * ut: Ultimate temperature in degree = +25 +-5 degree |
| 414 | * t : Any temperature in degree |
| 415 | * |
| 416 | * Note that the clock adjustment in ppb must be entered (which is |
| 417 | * the negative value of the deviation). |
| 418 | */ |
| 419 | static int rx8025_get_clock_adjust(struct device *dev, int *adj) |
| 420 | { |
| 421 | struct i2c_client *client = to_i2c_client(dev); |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 422 | int digoff; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 423 | |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame] | 424 | digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF); |
| 425 | if (digoff < 0) |
| 426 | return digoff; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 427 | |
| 428 | *adj = digoff >= 64 ? digoff - 128 : digoff; |
| 429 | if (*adj > 0) |
| 430 | (*adj)--; |
| 431 | *adj *= -RX8025_ADJ_RESOLUTION; |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static int rx8025_set_clock_adjust(struct device *dev, int adj) |
| 437 | { |
| 438 | struct i2c_client *client = to_i2c_client(dev); |
| 439 | u8 digoff; |
| 440 | int err; |
| 441 | |
| 442 | adj /= -RX8025_ADJ_RESOLUTION; |
| 443 | if (adj > RX8025_ADJ_DATA_MAX) |
| 444 | adj = RX8025_ADJ_DATA_MAX; |
| 445 | else if (adj < RX8025_ADJ_DATA_MIN) |
| 446 | adj = RX8025_ADJ_DATA_MIN; |
| 447 | else if (adj > 0) |
| 448 | adj++; |
| 449 | else if (adj < 0) |
| 450 | adj += 128; |
| 451 | digoff = adj; |
| 452 | |
| 453 | err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff); |
| 454 | if (err) |
| 455 | return err; |
| 456 | |
| 457 | dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff); |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev, |
| 463 | struct device_attribute *attr, |
| 464 | char *buf) |
| 465 | { |
| 466 | int err, adj; |
| 467 | |
| 468 | err = rx8025_get_clock_adjust(dev, &adj); |
| 469 | if (err) |
| 470 | return err; |
| 471 | |
| 472 | return sprintf(buf, "%d\n", adj); |
| 473 | } |
| 474 | |
| 475 | static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev, |
| 476 | struct device_attribute *attr, |
| 477 | const char *buf, size_t count) |
| 478 | { |
| 479 | int adj, err; |
| 480 | |
| 481 | if (sscanf(buf, "%i", &adj) != 1) |
| 482 | return -EINVAL; |
| 483 | |
| 484 | err = rx8025_set_clock_adjust(dev, adj); |
| 485 | |
| 486 | return err ? err : count; |
| 487 | } |
| 488 | |
| 489 | static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR, |
| 490 | rx8025_sysfs_show_clock_adjust, |
| 491 | rx8025_sysfs_store_clock_adjust); |
| 492 | |
| 493 | static int rx8025_sysfs_register(struct device *dev) |
| 494 | { |
| 495 | return device_create_file(dev, &dev_attr_clock_adjust_ppb); |
| 496 | } |
| 497 | |
| 498 | static void rx8025_sysfs_unregister(struct device *dev) |
| 499 | { |
| 500 | device_remove_file(dev, &dev_attr_clock_adjust_ppb); |
| 501 | } |
| 502 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 503 | static int rx8025_probe(struct i2c_client *client, |
| 504 | const struct i2c_device_id *id) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 505 | { |
Wolfram Sang | 110036b | 2019-06-08 12:56:09 +0200 | [diff] [blame] | 506 | struct i2c_adapter *adapter = client->adapter; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 507 | struct rx8025_data *rx8025; |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 508 | int err = 0; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 509 | |
| 510 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
| 511 | | I2C_FUNC_SMBUS_I2C_BLOCK)) { |
| 512 | dev_err(&adapter->dev, |
| 513 | "doesn't support required functionality\n"); |
Alexandre Belloni | dbcce7c | 2015-07-24 16:12:10 +0200 | [diff] [blame] | 514 | return -EIO; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Jingoo Han | fac42b4 | 2013-07-03 15:07:10 -0700 | [diff] [blame] | 517 | rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL); |
Alexandre Belloni | b1f9d79 | 2015-09-26 16:25:28 +0200 | [diff] [blame] | 518 | if (!rx8025) |
Alexandre Belloni | dbcce7c | 2015-07-24 16:12:10 +0200 | [diff] [blame] | 519 | return -ENOMEM; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 520 | |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 521 | i2c_set_clientdata(client, rx8025); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 522 | |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 523 | err = rx8025_init_client(client); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 524 | if (err) |
Alexandre Belloni | dbcce7c | 2015-07-24 16:12:10 +0200 | [diff] [blame] | 525 | return err; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 526 | |
Jingoo Han | fac42b4 | 2013-07-03 15:07:10 -0700 | [diff] [blame] | 527 | rx8025->rtc = devm_rtc_device_register(&client->dev, client->name, |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 528 | &rx8025_rtc_ops, THIS_MODULE); |
| 529 | if (IS_ERR(rx8025->rtc)) { |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 530 | dev_err(&client->dev, "unable to register the class device\n"); |
Alexandre Belloni | dbcce7c | 2015-07-24 16:12:10 +0200 | [diff] [blame] | 531 | return PTR_ERR(rx8025->rtc); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | if (client->irq > 0) { |
| 535 | dev_info(&client->dev, "IRQ %d supplied\n", client->irq); |
Alexandre Belloni | f0b63a1 | 2015-07-24 16:07:30 +0200 | [diff] [blame] | 536 | err = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
Akinobu Mita | 0a966c0 | 2016-02-15 23:49:06 +0900 | [diff] [blame] | 537 | rx8025_handle_irq, |
| 538 | IRQF_ONESHOT, |
| 539 | "rx8025", client); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 540 | if (err) { |
Alexandre Belloni | 8a06513 | 2015-08-04 00:45:37 +0200 | [diff] [blame] | 541 | dev_err(&client->dev, "unable to request IRQ, alarms disabled\n"); |
| 542 | client->irq = 0; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 546 | rx8025->rtc->max_user_freq = 1; |
| 547 | |
Akinobu Mita | ef5f4a9 | 2016-02-15 23:49:09 +0900 | [diff] [blame] | 548 | /* the rx8025 alarm only supports a minute accuracy */ |
| 549 | rx8025->rtc->uie_unsupported = 1; |
| 550 | |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 551 | err = rx8025_sysfs_register(&client->dev); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 552 | return err; |
| 553 | } |
| 554 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 555 | static int rx8025_remove(struct i2c_client *client) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 556 | { |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 557 | rx8025_sysfs_unregister(&client->dev); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static struct i2c_driver rx8025_driver = { |
| 562 | .driver = { |
| 563 | .name = "rtc-rx8025", |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 564 | }, |
| 565 | .probe = rx8025_probe, |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 566 | .remove = rx8025_remove, |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 567 | .id_table = rx8025_id, |
| 568 | }; |
| 569 | |
Axel Lin | 0abc920 | 2012-03-23 15:02:31 -0700 | [diff] [blame] | 570 | module_i2c_driver(rx8025_driver); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 571 | |
| 572 | MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); |
| 573 | MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver"); |
| 574 | MODULE_LICENSE("GPL"); |