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 | */ |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/bcd.h> |
| 25 | #include <linux/i2c.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/rtc.h> |
| 28 | |
| 29 | /* Register definitions */ |
| 30 | #define RX8025_REG_SEC 0x00 |
| 31 | #define RX8025_REG_MIN 0x01 |
| 32 | #define RX8025_REG_HOUR 0x02 |
| 33 | #define RX8025_REG_WDAY 0x03 |
| 34 | #define RX8025_REG_MDAY 0x04 |
| 35 | #define RX8025_REG_MONTH 0x05 |
| 36 | #define RX8025_REG_YEAR 0x06 |
| 37 | #define RX8025_REG_DIGOFF 0x07 |
| 38 | #define RX8025_REG_ALWMIN 0x08 |
| 39 | #define RX8025_REG_ALWHOUR 0x09 |
| 40 | #define RX8025_REG_ALWWDAY 0x0a |
| 41 | #define RX8025_REG_ALDMIN 0x0b |
| 42 | #define RX8025_REG_ALDHOUR 0x0c |
| 43 | /* 0x0d is reserved */ |
| 44 | #define RX8025_REG_CTRL1 0x0e |
| 45 | #define RX8025_REG_CTRL2 0x0f |
| 46 | |
| 47 | #define RX8025_BIT_CTRL1_CT (7 << 0) |
| 48 | /* 1 Hz periodic level irq */ |
| 49 | #define RX8025_BIT_CTRL1_CT_1HZ 4 |
| 50 | #define RX8025_BIT_CTRL1_TEST (1 << 3) |
| 51 | #define RX8025_BIT_CTRL1_1224 (1 << 5) |
| 52 | #define RX8025_BIT_CTRL1_DALE (1 << 6) |
| 53 | #define RX8025_BIT_CTRL1_WALE (1 << 7) |
| 54 | |
| 55 | #define RX8025_BIT_CTRL2_DAFG (1 << 0) |
| 56 | #define RX8025_BIT_CTRL2_WAFG (1 << 1) |
| 57 | #define RX8025_BIT_CTRL2_CTFG (1 << 2) |
| 58 | #define RX8025_BIT_CTRL2_PON (1 << 4) |
| 59 | #define RX8025_BIT_CTRL2_XST (1 << 5) |
| 60 | #define RX8025_BIT_CTRL2_VDET (1 << 6) |
| 61 | |
| 62 | /* Clock precision adjustment */ |
| 63 | #define RX8025_ADJ_RESOLUTION 3050 /* in ppb */ |
| 64 | #define RX8025_ADJ_DATA_MAX 62 |
| 65 | #define RX8025_ADJ_DATA_MIN -62 |
| 66 | |
| 67 | static const struct i2c_device_id rx8025_id[] = { |
| 68 | { "rx8025", 0 }, |
| 69 | { } |
| 70 | }; |
| 71 | MODULE_DEVICE_TABLE(i2c, rx8025_id); |
| 72 | |
| 73 | struct rx8025_data { |
| 74 | struct i2c_client *client; |
| 75 | struct rtc_device *rtc; |
| 76 | struct work_struct work; |
| 77 | u8 ctrl1; |
| 78 | unsigned exiting:1; |
| 79 | }; |
| 80 | |
| 81 | static int rx8025_read_reg(struct i2c_client *client, int number, u8 *value) |
| 82 | { |
| 83 | int ret = i2c_smbus_read_byte_data(client, (number << 4) | 0x08); |
| 84 | |
| 85 | if (ret < 0) { |
| 86 | dev_err(&client->dev, "Unable to read register #%d\n", number); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | *value = ret; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int rx8025_read_regs(struct i2c_client *client, |
| 95 | int number, u8 length, u8 *values) |
| 96 | { |
| 97 | int ret = i2c_smbus_read_i2c_block_data(client, (number << 4) | 0x08, |
| 98 | length, values); |
| 99 | |
| 100 | if (ret != length) { |
| 101 | dev_err(&client->dev, "Unable to read registers #%d..#%d\n", |
| 102 | number, number + length - 1); |
| 103 | return ret < 0 ? ret : -EIO; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int rx8025_write_reg(struct i2c_client *client, int number, u8 value) |
| 110 | { |
| 111 | int ret = i2c_smbus_write_byte_data(client, number << 4, value); |
| 112 | |
| 113 | if (ret) |
| 114 | dev_err(&client->dev, "Unable to write register #%d\n", |
| 115 | number); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static int rx8025_write_regs(struct i2c_client *client, |
| 121 | int number, u8 length, u8 *values) |
| 122 | { |
| 123 | int ret = i2c_smbus_write_i2c_block_data(client, (number << 4) | 0x08, |
| 124 | length, values); |
| 125 | |
| 126 | if (ret) |
| 127 | dev_err(&client->dev, "Unable to write registers #%d..#%d\n", |
| 128 | number, number + length - 1); |
| 129 | |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | static irqreturn_t rx8025_irq(int irq, void *dev_id) |
| 134 | { |
| 135 | struct i2c_client *client = dev_id; |
| 136 | struct rx8025_data *rx8025 = i2c_get_clientdata(client); |
| 137 | |
| 138 | disable_irq_nosync(irq); |
| 139 | schedule_work(&rx8025->work); |
| 140 | return IRQ_HANDLED; |
| 141 | } |
| 142 | |
| 143 | static void rx8025_work(struct work_struct *work) |
| 144 | { |
| 145 | struct rx8025_data *rx8025 = container_of(work, struct rx8025_data, |
| 146 | work); |
| 147 | struct i2c_client *client = rx8025->client; |
| 148 | struct mutex *lock = &rx8025->rtc->ops_lock; |
| 149 | u8 status; |
| 150 | |
| 151 | mutex_lock(lock); |
| 152 | |
| 153 | if (rx8025_read_reg(client, RX8025_REG_CTRL2, &status)) |
| 154 | goto out; |
| 155 | |
| 156 | if (!(status & RX8025_BIT_CTRL2_XST)) |
| 157 | dev_warn(&client->dev, "Oscillation stop was detected," |
| 158 | "you may have to readjust the clock\n"); |
| 159 | |
| 160 | if (status & RX8025_BIT_CTRL2_CTFG) { |
| 161 | /* periodic */ |
| 162 | status &= ~RX8025_BIT_CTRL2_CTFG; |
| 163 | local_irq_disable(); |
| 164 | rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF); |
| 165 | local_irq_enable(); |
| 166 | } |
| 167 | |
| 168 | if (status & RX8025_BIT_CTRL2_DAFG) { |
| 169 | /* alarm */ |
| 170 | status &= RX8025_BIT_CTRL2_DAFG; |
| 171 | if (rx8025_write_reg(client, RX8025_REG_CTRL1, |
| 172 | rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE)) |
| 173 | goto out; |
| 174 | local_irq_disable(); |
| 175 | rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF); |
| 176 | local_irq_enable(); |
| 177 | } |
| 178 | |
| 179 | /* acknowledge IRQ */ |
| 180 | rx8025_write_reg(client, RX8025_REG_CTRL2, |
| 181 | status | RX8025_BIT_CTRL2_XST); |
| 182 | |
| 183 | out: |
| 184 | if (!rx8025->exiting) |
| 185 | enable_irq(client->irq); |
| 186 | |
| 187 | mutex_unlock(lock); |
| 188 | } |
| 189 | |
| 190 | static int rx8025_get_time(struct device *dev, struct rtc_time *dt) |
| 191 | { |
| 192 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
| 193 | u8 date[7]; |
| 194 | int err; |
| 195 | |
| 196 | err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date); |
| 197 | if (err) |
| 198 | return err; |
| 199 | |
| 200 | dev_dbg(dev, "%s: read 0x%02x 0x%02x " |
| 201 | "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__, |
| 202 | date[0], date[1], date[2], date[3], date[4], |
| 203 | date[5], date[6]); |
| 204 | |
| 205 | dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f); |
| 206 | dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f); |
| 207 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) |
| 208 | dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f); |
| 209 | else |
| 210 | dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12 |
| 211 | + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0); |
| 212 | |
| 213 | dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f); |
| 214 | dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1; |
| 215 | dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]); |
| 216 | |
| 217 | if (dt->tm_year < 70) |
| 218 | dt->tm_year += 100; |
| 219 | |
| 220 | dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__, |
| 221 | dt->tm_sec, dt->tm_min, dt->tm_hour, |
| 222 | dt->tm_mday, dt->tm_mon, dt->tm_year); |
| 223 | |
| 224 | return rtc_valid_tm(dt); |
| 225 | } |
| 226 | |
| 227 | static int rx8025_set_time(struct device *dev, struct rtc_time *dt) |
| 228 | { |
| 229 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
| 230 | u8 date[7]; |
| 231 | |
| 232 | /* |
| 233 | * BUG: The HW assumes every year that is a multiple of 4 to be a leap |
| 234 | * year. Next time this is wrong is 2100, which will not be a leap |
| 235 | * year. |
| 236 | */ |
| 237 | |
| 238 | /* |
| 239 | * Here the read-only bits are written as "0". I'm not sure if that |
| 240 | * is sound. |
| 241 | */ |
| 242 | date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec); |
| 243 | date[RX8025_REG_MIN] = bin2bcd(dt->tm_min); |
| 244 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) |
| 245 | date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour); |
| 246 | else |
| 247 | date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0) |
| 248 | | bin2bcd((dt->tm_hour + 11) % 12 + 1); |
| 249 | |
| 250 | date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday); |
| 251 | date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday); |
| 252 | date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1); |
| 253 | date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year % 100); |
| 254 | |
| 255 | dev_dbg(dev, |
| 256 | "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", |
| 257 | __func__, |
| 258 | date[0], date[1], date[2], date[3], date[4], date[5], date[6]); |
| 259 | |
| 260 | return rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date); |
| 261 | } |
| 262 | |
| 263 | static int rx8025_init_client(struct i2c_client *client, int *need_reset) |
| 264 | { |
| 265 | struct rx8025_data *rx8025 = i2c_get_clientdata(client); |
| 266 | u8 ctrl[2], ctrl2; |
| 267 | int need_clear = 0; |
| 268 | int err; |
| 269 | |
| 270 | err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl); |
| 271 | if (err) |
| 272 | goto out; |
| 273 | |
| 274 | /* Keep test bit zero ! */ |
| 275 | rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST; |
| 276 | |
| 277 | if (ctrl[1] & RX8025_BIT_CTRL2_PON) { |
| 278 | dev_warn(&client->dev, "power-on reset was detected, " |
| 279 | "you may have to readjust the clock\n"); |
| 280 | *need_reset = 1; |
| 281 | } |
| 282 | |
| 283 | if (ctrl[1] & RX8025_BIT_CTRL2_VDET) { |
| 284 | dev_warn(&client->dev, "a power voltage drop was detected, " |
| 285 | "you may have to readjust the clock\n"); |
| 286 | *need_reset = 1; |
| 287 | } |
| 288 | |
| 289 | if (!(ctrl[1] & RX8025_BIT_CTRL2_XST)) { |
| 290 | dev_warn(&client->dev, "Oscillation stop was detected," |
| 291 | "you may have to readjust the clock\n"); |
| 292 | *need_reset = 1; |
| 293 | } |
| 294 | |
| 295 | if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) { |
| 296 | dev_warn(&client->dev, "Alarm was detected\n"); |
| 297 | need_clear = 1; |
| 298 | } |
| 299 | |
| 300 | if (!(ctrl[1] & RX8025_BIT_CTRL2_CTFG)) |
| 301 | need_clear = 1; |
| 302 | |
| 303 | if (*need_reset || need_clear) { |
| 304 | ctrl2 = ctrl[0]; |
| 305 | ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET | |
| 306 | RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG | |
| 307 | RX8025_BIT_CTRL2_DAFG); |
| 308 | ctrl2 |= RX8025_BIT_CTRL2_XST; |
| 309 | |
| 310 | err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2); |
| 311 | } |
| 312 | out: |
| 313 | return err; |
| 314 | } |
| 315 | |
| 316 | /* Alarm support */ |
| 317 | static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 318 | { |
| 319 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
| 320 | struct i2c_client *client = rx8025->client; |
| 321 | u8 ctrl2, ald[2]; |
| 322 | int err; |
| 323 | |
| 324 | if (client->irq <= 0) |
| 325 | return -EINVAL; |
| 326 | |
| 327 | err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald); |
| 328 | if (err) |
| 329 | return err; |
| 330 | |
| 331 | err = rx8025_read_reg(client, RX8025_REG_CTRL2, &ctrl2); |
| 332 | if (err) |
| 333 | return err; |
| 334 | |
| 335 | dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n", |
| 336 | __func__, ald[0], ald[1], ctrl2); |
| 337 | |
| 338 | /* Hardware alarms precision is 1 minute! */ |
| 339 | t->time.tm_sec = 0; |
| 340 | t->time.tm_min = bcd2bin(ald[0] & 0x7f); |
| 341 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) |
| 342 | t->time.tm_hour = bcd2bin(ald[1] & 0x3f); |
| 343 | else |
| 344 | t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12 |
| 345 | + (ald[1] & 0x20 ? 12 : 0); |
| 346 | |
| 347 | t->time.tm_wday = -1; |
| 348 | t->time.tm_mday = -1; |
| 349 | t->time.tm_mon = -1; |
| 350 | t->time.tm_year = -1; |
| 351 | |
| 352 | dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n", |
| 353 | __func__, |
| 354 | t->time.tm_sec, t->time.tm_min, t->time.tm_hour, |
| 355 | t->time.tm_mday, t->time.tm_mon, t->time.tm_year); |
| 356 | t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE); |
| 357 | t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled; |
| 358 | |
| 359 | return err; |
| 360 | } |
| 361 | |
| 362 | static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 363 | { |
| 364 | struct i2c_client *client = to_i2c_client(dev); |
| 365 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
| 366 | u8 ald[2]; |
| 367 | int err; |
| 368 | |
| 369 | if (client->irq <= 0) |
| 370 | return -EINVAL; |
| 371 | |
| 372 | /* Hardware alarm precision is 1 minute! */ |
| 373 | ald[0] = bin2bcd(t->time.tm_min); |
| 374 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) |
| 375 | ald[1] = bin2bcd(t->time.tm_hour); |
| 376 | else |
| 377 | ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0) |
| 378 | | bin2bcd((t->time.tm_hour + 11) % 12 + 1); |
| 379 | |
| 380 | dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]); |
| 381 | |
| 382 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) { |
| 383 | rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE; |
| 384 | err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, |
| 385 | rx8025->ctrl1); |
| 386 | if (err) |
| 387 | return err; |
| 388 | } |
| 389 | err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald); |
| 390 | if (err) |
| 391 | return err; |
| 392 | |
| 393 | if (t->enabled) { |
| 394 | rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE; |
| 395 | err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, |
| 396 | rx8025->ctrl1); |
| 397 | if (err) |
| 398 | return err; |
| 399 | } |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 405 | { |
| 406 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
| 407 | u8 ctrl1; |
| 408 | int err; |
| 409 | |
| 410 | ctrl1 = rx8025->ctrl1; |
| 411 | if (enabled) |
| 412 | ctrl1 |= RX8025_BIT_CTRL1_DALE; |
| 413 | else |
| 414 | ctrl1 &= ~RX8025_BIT_CTRL1_DALE; |
| 415 | |
| 416 | if (ctrl1 != rx8025->ctrl1) { |
| 417 | rx8025->ctrl1 = ctrl1; |
| 418 | err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, |
| 419 | rx8025->ctrl1); |
| 420 | if (err) |
| 421 | return err; |
| 422 | } |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | static int rx8025_irq_set_state(struct device *dev, int enabled) |
| 427 | { |
| 428 | struct i2c_client *client = to_i2c_client(dev); |
| 429 | struct rx8025_data *rx8025 = i2c_get_clientdata(client); |
| 430 | int ctrl1; |
| 431 | int err; |
| 432 | |
| 433 | if (client->irq <= 0) |
| 434 | return -ENXIO; |
| 435 | |
| 436 | ctrl1 = rx8025->ctrl1 & ~RX8025_BIT_CTRL1_CT; |
| 437 | if (enabled) |
| 438 | ctrl1 |= RX8025_BIT_CTRL1_CT_1HZ; |
| 439 | if (ctrl1 != rx8025->ctrl1) { |
| 440 | rx8025->ctrl1 = ctrl1; |
| 441 | err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, |
| 442 | rx8025->ctrl1); |
| 443 | if (err) |
| 444 | return err; |
| 445 | } |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static struct rtc_class_ops rx8025_rtc_ops = { |
| 451 | .read_time = rx8025_get_time, |
| 452 | .set_time = rx8025_set_time, |
| 453 | .read_alarm = rx8025_read_alarm, |
| 454 | .set_alarm = rx8025_set_alarm, |
| 455 | .alarm_irq_enable = rx8025_alarm_irq_enable, |
| 456 | .irq_set_state = rx8025_irq_set_state, |
| 457 | }; |
| 458 | |
| 459 | /* |
| 460 | * Clock precision adjustment support |
| 461 | * |
| 462 | * According to the RX8025 SA/NB application manual the frequency and |
| 463 | * temperature charateristics can be approximated using the following |
| 464 | * equation: |
| 465 | * |
| 466 | * df = a * (ut - t)**2 |
| 467 | * |
| 468 | * df: Frequency deviation in any temperature |
| 469 | * a : Coefficient = (-35 +-5) * 10**-9 |
| 470 | * ut: Ultimate temperature in degree = +25 +-5 degree |
| 471 | * t : Any temperature in degree |
| 472 | * |
| 473 | * Note that the clock adjustment in ppb must be entered (which is |
| 474 | * the negative value of the deviation). |
| 475 | */ |
| 476 | static int rx8025_get_clock_adjust(struct device *dev, int *adj) |
| 477 | { |
| 478 | struct i2c_client *client = to_i2c_client(dev); |
| 479 | u8 digoff; |
| 480 | int err; |
| 481 | |
| 482 | err = rx8025_read_reg(client, RX8025_REG_DIGOFF, &digoff); |
| 483 | if (err) |
| 484 | return err; |
| 485 | |
| 486 | *adj = digoff >= 64 ? digoff - 128 : digoff; |
| 487 | if (*adj > 0) |
| 488 | (*adj)--; |
| 489 | *adj *= -RX8025_ADJ_RESOLUTION; |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | static int rx8025_set_clock_adjust(struct device *dev, int adj) |
| 495 | { |
| 496 | struct i2c_client *client = to_i2c_client(dev); |
| 497 | u8 digoff; |
| 498 | int err; |
| 499 | |
| 500 | adj /= -RX8025_ADJ_RESOLUTION; |
| 501 | if (adj > RX8025_ADJ_DATA_MAX) |
| 502 | adj = RX8025_ADJ_DATA_MAX; |
| 503 | else if (adj < RX8025_ADJ_DATA_MIN) |
| 504 | adj = RX8025_ADJ_DATA_MIN; |
| 505 | else if (adj > 0) |
| 506 | adj++; |
| 507 | else if (adj < 0) |
| 508 | adj += 128; |
| 509 | digoff = adj; |
| 510 | |
| 511 | err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff); |
| 512 | if (err) |
| 513 | return err; |
| 514 | |
| 515 | dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev, |
| 521 | struct device_attribute *attr, |
| 522 | char *buf) |
| 523 | { |
| 524 | int err, adj; |
| 525 | |
| 526 | err = rx8025_get_clock_adjust(dev, &adj); |
| 527 | if (err) |
| 528 | return err; |
| 529 | |
| 530 | return sprintf(buf, "%d\n", adj); |
| 531 | } |
| 532 | |
| 533 | static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev, |
| 534 | struct device_attribute *attr, |
| 535 | const char *buf, size_t count) |
| 536 | { |
| 537 | int adj, err; |
| 538 | |
| 539 | if (sscanf(buf, "%i", &adj) != 1) |
| 540 | return -EINVAL; |
| 541 | |
| 542 | err = rx8025_set_clock_adjust(dev, adj); |
| 543 | |
| 544 | return err ? err : count; |
| 545 | } |
| 546 | |
| 547 | static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR, |
| 548 | rx8025_sysfs_show_clock_adjust, |
| 549 | rx8025_sysfs_store_clock_adjust); |
| 550 | |
| 551 | static int rx8025_sysfs_register(struct device *dev) |
| 552 | { |
| 553 | return device_create_file(dev, &dev_attr_clock_adjust_ppb); |
| 554 | } |
| 555 | |
| 556 | static void rx8025_sysfs_unregister(struct device *dev) |
| 557 | { |
| 558 | device_remove_file(dev, &dev_attr_clock_adjust_ppb); |
| 559 | } |
| 560 | |
| 561 | static int __devinit rx8025_probe(struct i2c_client *client, |
| 562 | const struct i2c_device_id *id) |
| 563 | { |
| 564 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 565 | struct rx8025_data *rx8025; |
| 566 | int err, need_reset = 0; |
| 567 | |
| 568 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
| 569 | | I2C_FUNC_SMBUS_I2C_BLOCK)) { |
| 570 | dev_err(&adapter->dev, |
| 571 | "doesn't support required functionality\n"); |
| 572 | err = -EIO; |
| 573 | goto errout; |
| 574 | } |
| 575 | |
| 576 | rx8025 = kzalloc(sizeof(*rx8025), GFP_KERNEL); |
| 577 | if (!rx8025) { |
| 578 | dev_err(&adapter->dev, "failed to alloc memory\n"); |
| 579 | err = -ENOMEM; |
| 580 | goto errout; |
| 581 | } |
| 582 | |
| 583 | rx8025->client = client; |
| 584 | i2c_set_clientdata(client, rx8025); |
| 585 | INIT_WORK(&rx8025->work, rx8025_work); |
| 586 | |
| 587 | err = rx8025_init_client(client, &need_reset); |
| 588 | if (err) |
| 589 | goto errout_free; |
| 590 | |
| 591 | if (need_reset) { |
| 592 | struct rtc_time tm; |
| 593 | dev_info(&client->dev, |
| 594 | "bad conditions detected, resetting date\n"); |
| 595 | rtc_time_to_tm(0, &tm); /* 1970/1/1 */ |
| 596 | rx8025_set_time(&client->dev, &tm); |
| 597 | } |
| 598 | |
| 599 | rx8025->rtc = rtc_device_register(client->name, &client->dev, |
| 600 | &rx8025_rtc_ops, THIS_MODULE); |
| 601 | if (IS_ERR(rx8025->rtc)) { |
| 602 | err = PTR_ERR(rx8025->rtc); |
| 603 | dev_err(&client->dev, "unable to register the class device\n"); |
| 604 | goto errout_free; |
| 605 | } |
| 606 | |
| 607 | if (client->irq > 0) { |
| 608 | dev_info(&client->dev, "IRQ %d supplied\n", client->irq); |
| 609 | err = request_irq(client->irq, rx8025_irq, |
| 610 | 0, "rx8025", client); |
| 611 | if (err) { |
| 612 | dev_err(&client->dev, "unable to request IRQ\n"); |
| 613 | goto errout_reg; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | rx8025->rtc->irq_freq = 1; |
| 618 | rx8025->rtc->max_user_freq = 1; |
| 619 | |
| 620 | err = rx8025_sysfs_register(&client->dev); |
| 621 | if (err) |
| 622 | goto errout_irq; |
| 623 | |
| 624 | return 0; |
| 625 | |
| 626 | errout_irq: |
| 627 | if (client->irq > 0) |
| 628 | free_irq(client->irq, client); |
| 629 | |
| 630 | errout_reg: |
| 631 | rtc_device_unregister(rx8025->rtc); |
| 632 | |
| 633 | errout_free: |
| 634 | i2c_set_clientdata(client, NULL); |
| 635 | kfree(rx8025); |
| 636 | |
| 637 | errout: |
| 638 | dev_err(&adapter->dev, "probing for rx8025 failed\n"); |
| 639 | return err; |
| 640 | } |
| 641 | |
| 642 | static int __devexit rx8025_remove(struct i2c_client *client) |
| 643 | { |
| 644 | struct rx8025_data *rx8025 = i2c_get_clientdata(client); |
| 645 | struct mutex *lock = &rx8025->rtc->ops_lock; |
| 646 | |
| 647 | if (client->irq > 0) { |
| 648 | mutex_lock(lock); |
| 649 | rx8025->exiting = 1; |
| 650 | mutex_unlock(lock); |
| 651 | |
| 652 | free_irq(client->irq, client); |
| 653 | flush_scheduled_work(); |
| 654 | } |
| 655 | |
| 656 | rx8025_sysfs_unregister(&client->dev); |
| 657 | rtc_device_unregister(rx8025->rtc); |
| 658 | i2c_set_clientdata(client, NULL); |
| 659 | kfree(rx8025); |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | static struct i2c_driver rx8025_driver = { |
| 664 | .driver = { |
| 665 | .name = "rtc-rx8025", |
| 666 | .owner = THIS_MODULE, |
| 667 | }, |
| 668 | .probe = rx8025_probe, |
| 669 | .remove = __devexit_p(rx8025_remove), |
| 670 | .id_table = rx8025_id, |
| 671 | }; |
| 672 | |
| 673 | static int __init rx8025_init(void) |
| 674 | { |
| 675 | return i2c_add_driver(&rx8025_driver); |
| 676 | } |
| 677 | |
| 678 | static void __exit rx8025_exit(void) |
| 679 | { |
| 680 | i2c_del_driver(&rx8025_driver); |
| 681 | } |
| 682 | |
| 683 | MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); |
| 684 | MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver"); |
| 685 | MODULE_LICENSE("GPL"); |
| 686 | |
| 687 | module_init(rx8025_init); |
| 688 | module_exit(rx8025_exit); |