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 |
| 10 | * <http://lists.lm-sensors.org/mailman/listinfo/lm-sensors> |
| 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 | b6a57c9 | 2015-07-24 15:59:43 +0200 | [diff] [blame] | 107 | static irqreturn_t rx8025_handle_irq(int irq, void *dev_id) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 108 | { |
| 109 | struct i2c_client *client = dev_id; |
| 110 | struct rx8025_data *rx8025 = i2c_get_clientdata(client); |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame^] | 111 | int status; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 112 | |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame^] | 113 | status = rx8025_read_reg(client, RX8025_REG_CTRL2); |
| 114 | if (status < 0) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 115 | goto out; |
| 116 | |
| 117 | if (!(status & RX8025_BIT_CTRL2_XST)) |
| 118 | dev_warn(&client->dev, "Oscillation stop was detected," |
| 119 | "you may have to readjust the clock\n"); |
| 120 | |
| 121 | if (status & RX8025_BIT_CTRL2_CTFG) { |
| 122 | /* periodic */ |
| 123 | status &= ~RX8025_BIT_CTRL2_CTFG; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 124 | rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | if (status & RX8025_BIT_CTRL2_DAFG) { |
| 128 | /* alarm */ |
| 129 | status &= RX8025_BIT_CTRL2_DAFG; |
| 130 | if (rx8025_write_reg(client, RX8025_REG_CTRL1, |
| 131 | rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE)) |
| 132 | goto out; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 133 | rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* acknowledge IRQ */ |
| 137 | rx8025_write_reg(client, RX8025_REG_CTRL2, |
| 138 | status | RX8025_BIT_CTRL2_XST); |
| 139 | |
| 140 | out: |
Alexandre Belloni | b6a57c9 | 2015-07-24 15:59:43 +0200 | [diff] [blame] | 141 | return IRQ_HANDLED; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int rx8025_get_time(struct device *dev, struct rtc_time *dt) |
| 145 | { |
| 146 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame^] | 147 | u8 date[7]; |
| 148 | int ctrl, err; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 149 | |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame^] | 150 | ctrl = rx8025_read_reg(rx8025->client, RX8025_REG_CTRL2); |
| 151 | if (ctrl < 0) |
| 152 | return ctrl; |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 153 | |
| 154 | if (ctrl & RX8025_BIT_CTRL2_PON) { |
| 155 | dev_warn(dev, "power-on reset detected, date is invalid\n"); |
| 156 | return -EINVAL; |
| 157 | } |
| 158 | |
| 159 | if (!(ctrl & RX8025_BIT_CTRL2_XST)) { |
| 160 | dev_warn(dev, "crystal stopped, date is invalid\n"); |
| 161 | return -EINVAL; |
| 162 | } |
| 163 | |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 164 | err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date); |
| 165 | if (err) |
| 166 | return err; |
| 167 | |
| 168 | dev_dbg(dev, "%s: read 0x%02x 0x%02x " |
| 169 | "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__, |
| 170 | date[0], date[1], date[2], date[3], date[4], |
| 171 | date[5], date[6]); |
| 172 | |
| 173 | dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f); |
| 174 | dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f); |
| 175 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) |
| 176 | dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f); |
| 177 | else |
| 178 | dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12 |
| 179 | + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0); |
| 180 | |
| 181 | dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f); |
| 182 | dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1; |
Alexandre Belloni | 32672c5 | 2015-07-25 12:07:37 +0200 | [diff] [blame] | 183 | dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 184 | |
| 185 | dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__, |
| 186 | dt->tm_sec, dt->tm_min, dt->tm_hour, |
| 187 | dt->tm_mday, dt->tm_mon, dt->tm_year); |
| 188 | |
| 189 | return rtc_valid_tm(dt); |
| 190 | } |
| 191 | |
| 192 | static int rx8025_set_time(struct device *dev, struct rtc_time *dt) |
| 193 | { |
| 194 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
| 195 | u8 date[7]; |
| 196 | |
Alexandre Belloni | 32672c5 | 2015-07-25 12:07:37 +0200 | [diff] [blame] | 197 | if ((dt->tm_year < 100) || (dt->tm_year > 199)) |
| 198 | return -EINVAL; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * Here the read-only bits are written as "0". I'm not sure if that |
| 202 | * is sound. |
| 203 | */ |
| 204 | date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec); |
| 205 | date[RX8025_REG_MIN] = bin2bcd(dt->tm_min); |
| 206 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) |
| 207 | date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour); |
| 208 | else |
| 209 | date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0) |
| 210 | | bin2bcd((dt->tm_hour + 11) % 12 + 1); |
| 211 | |
| 212 | date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday); |
| 213 | date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday); |
| 214 | date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1); |
Alexandre Belloni | 32672c5 | 2015-07-25 12:07:37 +0200 | [diff] [blame] | 215 | date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 216 | |
| 217 | dev_dbg(dev, |
| 218 | "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", |
| 219 | __func__, |
| 220 | date[0], date[1], date[2], date[3], date[4], date[5], date[6]); |
| 221 | |
| 222 | return rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date); |
| 223 | } |
| 224 | |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 225 | static int rx8025_init_client(struct i2c_client *client) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 226 | { |
| 227 | struct rx8025_data *rx8025 = i2c_get_clientdata(client); |
| 228 | u8 ctrl[2], ctrl2; |
| 229 | int need_clear = 0; |
| 230 | int err; |
| 231 | |
| 232 | err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl); |
| 233 | if (err) |
| 234 | goto out; |
| 235 | |
| 236 | /* Keep test bit zero ! */ |
| 237 | rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST; |
| 238 | |
| 239 | if (ctrl[1] & RX8025_BIT_CTRL2_PON) { |
| 240 | dev_warn(&client->dev, "power-on reset was detected, " |
| 241 | "you may have to readjust the clock\n"); |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 242 | need_clear = 1; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | if (ctrl[1] & RX8025_BIT_CTRL2_VDET) { |
| 246 | dev_warn(&client->dev, "a power voltage drop was detected, " |
| 247 | "you may have to readjust the clock\n"); |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 248 | need_clear = 1; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | if (!(ctrl[1] & RX8025_BIT_CTRL2_XST)) { |
| 252 | dev_warn(&client->dev, "Oscillation stop was detected," |
| 253 | "you may have to readjust the clock\n"); |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 254 | need_clear = 1; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) { |
| 258 | dev_warn(&client->dev, "Alarm was detected\n"); |
| 259 | need_clear = 1; |
| 260 | } |
| 261 | |
| 262 | if (!(ctrl[1] & RX8025_BIT_CTRL2_CTFG)) |
| 263 | need_clear = 1; |
| 264 | |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 265 | if (need_clear) { |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 266 | ctrl2 = ctrl[0]; |
| 267 | ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET | |
| 268 | RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG | |
| 269 | RX8025_BIT_CTRL2_DAFG); |
| 270 | ctrl2 |= RX8025_BIT_CTRL2_XST; |
| 271 | |
| 272 | err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2); |
| 273 | } |
| 274 | out: |
| 275 | return err; |
| 276 | } |
| 277 | |
| 278 | /* Alarm support */ |
| 279 | static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 280 | { |
| 281 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
| 282 | struct i2c_client *client = rx8025->client; |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame^] | 283 | u8 ald[2]; |
| 284 | int ctrl2, err; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 285 | |
| 286 | if (client->irq <= 0) |
| 287 | return -EINVAL; |
| 288 | |
| 289 | err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald); |
| 290 | if (err) |
| 291 | return err; |
| 292 | |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame^] | 293 | ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2); |
| 294 | if (ctrl2 < 0) |
| 295 | return ctrl2; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 296 | |
| 297 | dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n", |
| 298 | __func__, ald[0], ald[1], ctrl2); |
| 299 | |
| 300 | /* Hardware alarms precision is 1 minute! */ |
| 301 | t->time.tm_sec = 0; |
| 302 | t->time.tm_min = bcd2bin(ald[0] & 0x7f); |
| 303 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) |
| 304 | t->time.tm_hour = bcd2bin(ald[1] & 0x3f); |
| 305 | else |
| 306 | t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12 |
| 307 | + (ald[1] & 0x20 ? 12 : 0); |
| 308 | |
| 309 | t->time.tm_wday = -1; |
| 310 | t->time.tm_mday = -1; |
| 311 | t->time.tm_mon = -1; |
| 312 | t->time.tm_year = -1; |
| 313 | |
| 314 | dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n", |
| 315 | __func__, |
| 316 | t->time.tm_sec, t->time.tm_min, t->time.tm_hour, |
| 317 | t->time.tm_mday, t->time.tm_mon, t->time.tm_year); |
| 318 | t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE); |
| 319 | t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled; |
| 320 | |
| 321 | return err; |
| 322 | } |
| 323 | |
| 324 | static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 325 | { |
| 326 | struct i2c_client *client = to_i2c_client(dev); |
| 327 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
| 328 | u8 ald[2]; |
| 329 | int err; |
| 330 | |
| 331 | if (client->irq <= 0) |
| 332 | return -EINVAL; |
| 333 | |
| 334 | /* Hardware alarm precision is 1 minute! */ |
| 335 | ald[0] = bin2bcd(t->time.tm_min); |
| 336 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) |
| 337 | ald[1] = bin2bcd(t->time.tm_hour); |
| 338 | else |
| 339 | ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0) |
| 340 | | bin2bcd((t->time.tm_hour + 11) % 12 + 1); |
| 341 | |
| 342 | dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]); |
| 343 | |
| 344 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) { |
| 345 | rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE; |
| 346 | err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, |
| 347 | rx8025->ctrl1); |
| 348 | if (err) |
| 349 | return err; |
| 350 | } |
| 351 | err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald); |
| 352 | if (err) |
| 353 | return err; |
| 354 | |
| 355 | if (t->enabled) { |
| 356 | rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE; |
| 357 | err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, |
| 358 | rx8025->ctrl1); |
| 359 | if (err) |
| 360 | return err; |
| 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 367 | { |
| 368 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
| 369 | u8 ctrl1; |
| 370 | int err; |
| 371 | |
| 372 | ctrl1 = rx8025->ctrl1; |
| 373 | if (enabled) |
| 374 | ctrl1 |= RX8025_BIT_CTRL1_DALE; |
| 375 | else |
| 376 | ctrl1 &= ~RX8025_BIT_CTRL1_DALE; |
| 377 | |
| 378 | if (ctrl1 != rx8025->ctrl1) { |
| 379 | rx8025->ctrl1 = ctrl1; |
| 380 | err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, |
| 381 | rx8025->ctrl1); |
| 382 | if (err) |
| 383 | return err; |
| 384 | } |
| 385 | return 0; |
| 386 | } |
| 387 | |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 388 | static struct rtc_class_ops rx8025_rtc_ops = { |
| 389 | .read_time = rx8025_get_time, |
| 390 | .set_time = rx8025_set_time, |
| 391 | .read_alarm = rx8025_read_alarm, |
| 392 | .set_alarm = rx8025_set_alarm, |
| 393 | .alarm_irq_enable = rx8025_alarm_irq_enable, |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | /* |
| 397 | * Clock precision adjustment support |
| 398 | * |
| 399 | * According to the RX8025 SA/NB application manual the frequency and |
Thomas Weber | b770ffd | 2010-07-19 08:22:43 +0200 | [diff] [blame] | 400 | * temperature characteristics can be approximated using the following |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 401 | * equation: |
| 402 | * |
| 403 | * df = a * (ut - t)**2 |
| 404 | * |
| 405 | * df: Frequency deviation in any temperature |
| 406 | * a : Coefficient = (-35 +-5) * 10**-9 |
| 407 | * ut: Ultimate temperature in degree = +25 +-5 degree |
| 408 | * t : Any temperature in degree |
| 409 | * |
| 410 | * Note that the clock adjustment in ppb must be entered (which is |
| 411 | * the negative value of the deviation). |
| 412 | */ |
| 413 | static int rx8025_get_clock_adjust(struct device *dev, int *adj) |
| 414 | { |
| 415 | struct i2c_client *client = to_i2c_client(dev); |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame^] | 416 | int digoff; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 417 | |
Alexandre Belloni | fd9061f | 2015-08-04 00:40:25 +0200 | [diff] [blame^] | 418 | digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF); |
| 419 | if (digoff < 0) |
| 420 | return digoff; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 421 | |
| 422 | *adj = digoff >= 64 ? digoff - 128 : digoff; |
| 423 | if (*adj > 0) |
| 424 | (*adj)--; |
| 425 | *adj *= -RX8025_ADJ_RESOLUTION; |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static int rx8025_set_clock_adjust(struct device *dev, int adj) |
| 431 | { |
| 432 | struct i2c_client *client = to_i2c_client(dev); |
| 433 | u8 digoff; |
| 434 | int err; |
| 435 | |
| 436 | adj /= -RX8025_ADJ_RESOLUTION; |
| 437 | if (adj > RX8025_ADJ_DATA_MAX) |
| 438 | adj = RX8025_ADJ_DATA_MAX; |
| 439 | else if (adj < RX8025_ADJ_DATA_MIN) |
| 440 | adj = RX8025_ADJ_DATA_MIN; |
| 441 | else if (adj > 0) |
| 442 | adj++; |
| 443 | else if (adj < 0) |
| 444 | adj += 128; |
| 445 | digoff = adj; |
| 446 | |
| 447 | err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff); |
| 448 | if (err) |
| 449 | return err; |
| 450 | |
| 451 | dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev, |
| 457 | struct device_attribute *attr, |
| 458 | char *buf) |
| 459 | { |
| 460 | int err, adj; |
| 461 | |
| 462 | err = rx8025_get_clock_adjust(dev, &adj); |
| 463 | if (err) |
| 464 | return err; |
| 465 | |
| 466 | return sprintf(buf, "%d\n", adj); |
| 467 | } |
| 468 | |
| 469 | static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev, |
| 470 | struct device_attribute *attr, |
| 471 | const char *buf, size_t count) |
| 472 | { |
| 473 | int adj, err; |
| 474 | |
| 475 | if (sscanf(buf, "%i", &adj) != 1) |
| 476 | return -EINVAL; |
| 477 | |
| 478 | err = rx8025_set_clock_adjust(dev, adj); |
| 479 | |
| 480 | return err ? err : count; |
| 481 | } |
| 482 | |
| 483 | static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR, |
| 484 | rx8025_sysfs_show_clock_adjust, |
| 485 | rx8025_sysfs_store_clock_adjust); |
| 486 | |
| 487 | static int rx8025_sysfs_register(struct device *dev) |
| 488 | { |
| 489 | return device_create_file(dev, &dev_attr_clock_adjust_ppb); |
| 490 | } |
| 491 | |
| 492 | static void rx8025_sysfs_unregister(struct device *dev) |
| 493 | { |
| 494 | device_remove_file(dev, &dev_attr_clock_adjust_ppb); |
| 495 | } |
| 496 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 497 | static int rx8025_probe(struct i2c_client *client, |
| 498 | const struct i2c_device_id *id) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 499 | { |
| 500 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 501 | struct rx8025_data *rx8025; |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 502 | int err = 0; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 503 | |
| 504 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
| 505 | | I2C_FUNC_SMBUS_I2C_BLOCK)) { |
| 506 | dev_err(&adapter->dev, |
| 507 | "doesn't support required functionality\n"); |
Alexandre Belloni | dbcce7c | 2015-07-24 16:12:10 +0200 | [diff] [blame] | 508 | return -EIO; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Jingoo Han | fac42b4 | 2013-07-03 15:07:10 -0700 | [diff] [blame] | 511 | rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 512 | if (!rx8025) { |
Alexandre Belloni | dbcce7c | 2015-07-24 16:12:10 +0200 | [diff] [blame] | 513 | return -ENOMEM; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | rx8025->client = client; |
| 517 | i2c_set_clientdata(client, rx8025); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 518 | |
Alexandre Belloni | 6f0a8cf | 2015-07-26 10:13:31 +0200 | [diff] [blame] | 519 | err = rx8025_init_client(client); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 520 | if (err) |
Alexandre Belloni | dbcce7c | 2015-07-24 16:12:10 +0200 | [diff] [blame] | 521 | return err; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 522 | |
Jingoo Han | fac42b4 | 2013-07-03 15:07:10 -0700 | [diff] [blame] | 523 | rx8025->rtc = devm_rtc_device_register(&client->dev, client->name, |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 524 | &rx8025_rtc_ops, THIS_MODULE); |
| 525 | if (IS_ERR(rx8025->rtc)) { |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 526 | dev_err(&client->dev, "unable to register the class device\n"); |
Alexandre Belloni | dbcce7c | 2015-07-24 16:12:10 +0200 | [diff] [blame] | 527 | return PTR_ERR(rx8025->rtc); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | if (client->irq > 0) { |
| 531 | dev_info(&client->dev, "IRQ %d supplied\n", client->irq); |
Alexandre Belloni | f0b63a1 | 2015-07-24 16:07:30 +0200 | [diff] [blame] | 532 | err = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
| 533 | rx8025_handle_irq, 0, "rx8025", |
| 534 | client); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 535 | if (err) { |
| 536 | dev_err(&client->dev, "unable to request IRQ\n"); |
Alexandre Belloni | dbcce7c | 2015-07-24 16:12:10 +0200 | [diff] [blame] | 537 | return err; |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
| 541 | rx8025->rtc->irq_freq = 1; |
| 542 | rx8025->rtc->max_user_freq = 1; |
| 543 | |
| 544 | err = rx8025_sysfs_register(&client->dev); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 545 | return err; |
| 546 | } |
| 547 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 548 | static int rx8025_remove(struct i2c_client *client) |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 549 | { |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 550 | rx8025_sysfs_unregister(&client->dev); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static struct i2c_driver rx8025_driver = { |
| 555 | .driver = { |
| 556 | .name = "rtc-rx8025", |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 557 | }, |
| 558 | .probe = rx8025_probe, |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 559 | .remove = rx8025_remove, |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 560 | .id_table = rx8025_id, |
| 561 | }; |
| 562 | |
Axel Lin | 0abc920 | 2012-03-23 15:02:31 -0700 | [diff] [blame] | 563 | module_i2c_driver(rx8025_driver); |
Wolfgang Grandegger | 3c2b907 | 2009-06-17 16:26:11 -0700 | [diff] [blame] | 564 | |
| 565 | MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); |
| 566 | MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver"); |
| 567 | MODULE_LICENSE("GPL"); |