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