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