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