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