Blame view
drivers/rtc/rtc-m41t80.c
22.6 KB
caaff562e rtc: add rtc-m41t... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/* * I2C client/driver for the ST M41T80 family of i2c rtc chips. * * Author: Alexander Bigga <ab@mycable.de> * * Based on m41t00.c by Mark A. Greer <mgreer@mvista.com> * * 2006 (c) mycable GmbH * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * */ |
35aa64f3a rtc: m41t80: sort... |
15 16 |
#include <linux/bcd.h> #include <linux/i2c.h> |
caaff562e rtc: add rtc-m41t... |
17 |
#include <linux/init.h> |
9fb1f68d4 rtc: m41t80: incl... |
18 |
#include <linux/kernel.h> |
35aa64f3a rtc: m41t80: sort... |
19 20 |
#include <linux/module.h> #include <linux/rtc.h> |
caaff562e rtc: add rtc-m41t... |
21 |
#include <linux/slab.h> |
613655fa3 drivers: autoconv... |
22 |
#include <linux/mutex.h> |
caaff562e rtc: add rtc-m41t... |
23 |
#include <linux/string.h> |
617780d29 rtc: watchdog sup... |
24 |
#ifdef CONFIG_RTC_DRV_M41T80_WDT |
617780d29 rtc: watchdog sup... |
25 26 |
#include <linux/fs.h> #include <linux/ioctl.h> |
35aa64f3a rtc: m41t80: sort... |
27 28 29 |
#include <linux/miscdevice.h> #include <linux/reboot.h> #include <linux/watchdog.h> |
617780d29 rtc: watchdog sup... |
30 |
#endif |
caaff562e rtc: add rtc-m41t... |
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#define M41T80_REG_SSEC 0 #define M41T80_REG_SEC 1 #define M41T80_REG_MIN 2 #define M41T80_REG_HOUR 3 #define M41T80_REG_WDAY 4 #define M41T80_REG_DAY 5 #define M41T80_REG_MON 6 #define M41T80_REG_YEAR 7 #define M41T80_REG_ALARM_MON 0xa #define M41T80_REG_ALARM_DAY 0xb #define M41T80_REG_ALARM_HOUR 0xc #define M41T80_REG_ALARM_MIN 0xd #define M41T80_REG_ALARM_SEC 0xe #define M41T80_REG_FLAGS 0xf #define M41T80_REG_SQW 0x13 #define M41T80_DATETIME_REG_SIZE (M41T80_REG_YEAR + 1) #define M41T80_ALARM_REG_SIZE \ (M41T80_REG_ALARM_SEC + 1 - M41T80_REG_ALARM_MON) #define M41T80_SEC_ST (1 << 7) /* ST: Stop Bit */ #define M41T80_ALMON_AFE (1 << 7) /* AFE: AF Enable Bit */ #define M41T80_ALMON_SQWE (1 << 6) /* SQWE: SQW Enable Bit */ #define M41T80_ALHOUR_HT (1 << 6) /* HT: Halt Update Bit */ #define M41T80_FLAGS_AF (1 << 6) /* AF: Alarm Flag Bit */ #define M41T80_FLAGS_BATT_LOW (1 << 4) /* BL: Battery Low Bit */ |
d3a126fcf rtc: rtc-m41t80.c... |
58 59 60 |
#define M41T80_WATCHDOG_RB2 (1 << 7) /* RB: Watchdog resolution */ #define M41T80_WATCHDOG_RB1 (1 << 1) /* RB: Watchdog resolution */ #define M41T80_WATCHDOG_RB0 (1 << 0) /* RB: Watchdog resolution */ |
caaff562e rtc: add rtc-m41t... |
61 |
|
d3a126fcf rtc: rtc-m41t80.c... |
62 63 64 65 |
#define M41T80_FEATURE_HT (1 << 0) /* Halt feature */ #define M41T80_FEATURE_BL (1 << 1) /* Battery low indicator */ #define M41T80_FEATURE_SQ (1 << 2) /* Squarewave feature */ #define M41T80_FEATURE_WD (1 << 3) /* Extra watchdog resolution */ |
f30281f4f rtc: add m41t62 s... |
66 |
#define M41T80_FEATURE_SQ_ALT (1 << 4) /* RSx bits are in reg 4 */ |
caaff562e rtc: add rtc-m41t... |
67 68 |
#define DRV_VERSION "0.05" |
613655fa3 drivers: autoconv... |
69 |
static DEFINE_MUTEX(m41t80_rtc_mutex); |
3760f7367 i2c: Convert most... |
70 |
static const struct i2c_device_id m41t80_id[] = { |
f30281f4f rtc: add m41t62 s... |
71 |
{ "m41t62", M41T80_FEATURE_SQ | M41T80_FEATURE_SQ_ALT }, |
d3a126fcf rtc: rtc-m41t80.c... |
72 73 74 75 76 77 78 79 80 |
{ "m41t65", M41T80_FEATURE_HT | M41T80_FEATURE_WD }, { "m41t80", M41T80_FEATURE_SQ }, { "m41t81", M41T80_FEATURE_HT | M41T80_FEATURE_SQ}, { "m41t81s", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ }, { "m41t82", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ }, { "m41t83", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ }, { "m41st84", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ }, { "m41st85", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ }, { "m41st87", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ }, |
3760f7367 i2c: Convert most... |
81 |
{ } |
caaff562e rtc: add rtc-m41t... |
82 |
}; |
3760f7367 i2c: Convert most... |
83 |
MODULE_DEVICE_TABLE(i2c, m41t80_id); |
caaff562e rtc: add rtc-m41t... |
84 85 |
struct m41t80_data { |
3760f7367 i2c: Convert most... |
86 |
u8 features; |
caaff562e rtc: add rtc-m41t... |
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
struct rtc_device *rtc; }; static int m41t80_get_datetime(struct i2c_client *client, struct rtc_time *tm) { u8 buf[M41T80_DATETIME_REG_SIZE], dt_addr[1] = { M41T80_REG_SEC }; struct i2c_msg msgs[] = { { .addr = client->addr, .flags = 0, .len = 1, .buf = dt_addr, }, { .addr = client->addr, .flags = I2C_M_RD, .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC, .buf = buf + M41T80_REG_SEC, }, }; if (i2c_transfer(client->adapter, msgs, 2) < 0) { dev_err(&client->dev, "read error "); return -EIO; } |
fe20ba70a drivers/rtc/: use... |
114 115 116 117 |
tm->tm_sec = bcd2bin(buf[M41T80_REG_SEC] & 0x7f); tm->tm_min = bcd2bin(buf[M41T80_REG_MIN] & 0x7f); tm->tm_hour = bcd2bin(buf[M41T80_REG_HOUR] & 0x3f); tm->tm_mday = bcd2bin(buf[M41T80_REG_DAY] & 0x3f); |
caaff562e rtc: add rtc-m41t... |
118 |
tm->tm_wday = buf[M41T80_REG_WDAY] & 0x07; |
fe20ba70a drivers/rtc/: use... |
119 |
tm->tm_mon = bcd2bin(buf[M41T80_REG_MON] & 0x1f) - 1; |
caaff562e rtc: add rtc-m41t... |
120 121 |
/* assume 20YY not 19YY, and ignore the Century Bit */ |
fe20ba70a drivers/rtc/: use... |
122 |
tm->tm_year = bcd2bin(buf[M41T80_REG_YEAR]) + 100; |
b485fe5ea rtc/m41t80: use r... |
123 |
return rtc_valid_tm(tm); |
caaff562e rtc: add rtc-m41t... |
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
} /* Sets the given date and time to the real time clock. */ static int m41t80_set_datetime(struct i2c_client *client, struct rtc_time *tm) { u8 wbuf[1 + M41T80_DATETIME_REG_SIZE]; u8 *buf = &wbuf[1]; u8 dt_addr[1] = { M41T80_REG_SEC }; struct i2c_msg msgs_in[] = { { .addr = client->addr, .flags = 0, .len = 1, .buf = dt_addr, }, { .addr = client->addr, .flags = I2C_M_RD, .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC, .buf = buf + M41T80_REG_SEC, }, }; struct i2c_msg msgs[] = { { .addr = client->addr, .flags = 0, .len = 1 + M41T80_DATETIME_REG_SIZE, .buf = wbuf, }, }; /* Read current reg values into buf[1..7] */ if (i2c_transfer(client->adapter, msgs_in, 2) < 0) { dev_err(&client->dev, "read error "); return -EIO; } wbuf[0] = 0; /* offset into rtc's regs */ /* Merge time-data and register flags into buf[0..7] */ buf[M41T80_REG_SSEC] = 0; buf[M41T80_REG_SEC] = |
fe20ba70a drivers/rtc/: use... |
166 |
bin2bcd(tm->tm_sec) | (buf[M41T80_REG_SEC] & ~0x7f); |
caaff562e rtc: add rtc-m41t... |
167 |
buf[M41T80_REG_MIN] = |
fe20ba70a drivers/rtc/: use... |
168 |
bin2bcd(tm->tm_min) | (buf[M41T80_REG_MIN] & ~0x7f); |
caaff562e rtc: add rtc-m41t... |
169 |
buf[M41T80_REG_HOUR] = |
fe20ba70a drivers/rtc/: use... |
170 |
bin2bcd(tm->tm_hour) | (buf[M41T80_REG_HOUR] & ~0x3f) ; |
caaff562e rtc: add rtc-m41t... |
171 172 173 |
buf[M41T80_REG_WDAY] = (tm->tm_wday & 0x07) | (buf[M41T80_REG_WDAY] & ~0x07); buf[M41T80_REG_DAY] = |
fe20ba70a drivers/rtc/: use... |
174 |
bin2bcd(tm->tm_mday) | (buf[M41T80_REG_DAY] & ~0x3f); |
caaff562e rtc: add rtc-m41t... |
175 |
buf[M41T80_REG_MON] = |
fe20ba70a drivers/rtc/: use... |
176 |
bin2bcd(tm->tm_mon + 1) | (buf[M41T80_REG_MON] & ~0x1f); |
caaff562e rtc: add rtc-m41t... |
177 |
/* assume 20YY not 19YY */ |
fe20ba70a drivers/rtc/: use... |
178 |
buf[M41T80_REG_YEAR] = bin2bcd(tm->tm_year % 100); |
caaff562e rtc: add rtc-m41t... |
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
if (i2c_transfer(client->adapter, msgs, 1) != 1) { dev_err(&client->dev, "write error "); return -EIO; } return 0; } #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE) static int m41t80_rtc_proc(struct device *dev, struct seq_file *seq) { struct i2c_client *client = to_i2c_client(dev); struct m41t80_data *clientdata = i2c_get_clientdata(client); u8 reg; |
3760f7367 i2c: Convert most... |
194 |
if (clientdata->features & M41T80_FEATURE_BL) { |
caaff562e rtc: add rtc-m41t... |
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
reg = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS); seq_printf(seq, "battery\t\t: %s ", (reg & M41T80_FLAGS_BATT_LOW) ? "exhausted" : "ok"); } return 0; } #else #define m41t80_rtc_proc NULL #endif static int m41t80_rtc_read_time(struct device *dev, struct rtc_time *tm) { return m41t80_get_datetime(to_i2c_client(dev), tm); } static int m41t80_rtc_set_time(struct device *dev, struct rtc_time *tm) { return m41t80_set_datetime(to_i2c_client(dev), tm); } |
16380c153 RTC: Convert rtc ... |
215 |
static int m41t80_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
caaff562e rtc: add rtc-m41t... |
216 217 218 |
{ struct i2c_client *client = to_i2c_client(dev); int rc; |
caaff562e rtc: add rtc-m41t... |
219 220 221 |
rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON); if (rc < 0) goto err; |
16380c153 RTC: Convert rtc ... |
222 223 |
if (enabled) |
caaff562e rtc: add rtc-m41t... |
224 |
rc |= M41T80_ALMON_AFE; |
16380c153 RTC: Convert rtc ... |
225 226 |
else rc &= ~M41T80_ALMON_AFE; |
caaff562e rtc: add rtc-m41t... |
227 228 |
if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON, rc) < 0) goto err; |
16380c153 RTC: Convert rtc ... |
229 |
|
caaff562e rtc: add rtc-m41t... |
230 231 232 233 |
return 0; err: return -EIO; } |
caaff562e rtc: add rtc-m41t... |
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
static int m41t80_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t) { struct i2c_client *client = to_i2c_client(dev); u8 wbuf[1 + M41T80_ALARM_REG_SIZE]; u8 *buf = &wbuf[1]; u8 *reg = buf - M41T80_REG_ALARM_MON; u8 dt_addr[1] = { M41T80_REG_ALARM_MON }; struct i2c_msg msgs_in[] = { { .addr = client->addr, .flags = 0, .len = 1, .buf = dt_addr, }, { .addr = client->addr, .flags = I2C_M_RD, .len = M41T80_ALARM_REG_SIZE, .buf = buf, }, }; struct i2c_msg msgs[] = { { .addr = client->addr, .flags = 0, .len = 1 + M41T80_ALARM_REG_SIZE, .buf = wbuf, }, }; if (i2c_transfer(client->adapter, msgs_in, 2) < 0) { dev_err(&client->dev, "read error "); return -EIO; } reg[M41T80_REG_ALARM_MON] &= ~(0x1f | M41T80_ALMON_AFE); reg[M41T80_REG_ALARM_DAY] = 0; reg[M41T80_REG_ALARM_HOUR] &= ~(0x3f | 0x80); reg[M41T80_REG_ALARM_MIN] = 0; reg[M41T80_REG_ALARM_SEC] = 0; wbuf[0] = M41T80_REG_ALARM_MON; /* offset into rtc's regs */ reg[M41T80_REG_ALARM_SEC] |= t->time.tm_sec >= 0 ? |
fe20ba70a drivers/rtc/: use... |
278 |
bin2bcd(t->time.tm_sec) : 0x80; |
caaff562e rtc: add rtc-m41t... |
279 |
reg[M41T80_REG_ALARM_MIN] |= t->time.tm_min >= 0 ? |
fe20ba70a drivers/rtc/: use... |
280 |
bin2bcd(t->time.tm_min) : 0x80; |
caaff562e rtc: add rtc-m41t... |
281 |
reg[M41T80_REG_ALARM_HOUR] |= t->time.tm_hour >= 0 ? |
fe20ba70a drivers/rtc/: use... |
282 |
bin2bcd(t->time.tm_hour) : 0x80; |
caaff562e rtc: add rtc-m41t... |
283 |
reg[M41T80_REG_ALARM_DAY] |= t->time.tm_mday >= 0 ? |
fe20ba70a drivers/rtc/: use... |
284 |
bin2bcd(t->time.tm_mday) : 0x80; |
caaff562e rtc: add rtc-m41t... |
285 |
if (t->time.tm_mon >= 0) |
fe20ba70a drivers/rtc/: use... |
286 |
reg[M41T80_REG_ALARM_MON] |= bin2bcd(t->time.tm_mon + 1); |
caaff562e rtc: add rtc-m41t... |
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
else reg[M41T80_REG_ALARM_DAY] |= 0x40; if (i2c_transfer(client->adapter, msgs, 1) != 1) { dev_err(&client->dev, "write error "); return -EIO; } if (t->enabled) { reg[M41T80_REG_ALARM_MON] |= M41T80_ALMON_AFE; if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON, reg[M41T80_REG_ALARM_MON]) < 0) { dev_err(&client->dev, "write error "); return -EIO; } } return 0; } static int m41t80_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *t) { struct i2c_client *client = to_i2c_client(dev); u8 buf[M41T80_ALARM_REG_SIZE + 1]; /* all alarm regs and flags */ u8 dt_addr[1] = { M41T80_REG_ALARM_MON }; u8 *reg = buf - M41T80_REG_ALARM_MON; struct i2c_msg msgs[] = { { .addr = client->addr, .flags = 0, .len = 1, .buf = dt_addr, }, { .addr = client->addr, .flags = I2C_M_RD, .len = M41T80_ALARM_REG_SIZE + 1, .buf = buf, }, }; if (i2c_transfer(client->adapter, msgs, 2) < 0) { dev_err(&client->dev, "read error "); return -EIO; } t->time.tm_sec = -1; t->time.tm_min = -1; t->time.tm_hour = -1; t->time.tm_mday = -1; t->time.tm_mon = -1; if (!(reg[M41T80_REG_ALARM_SEC] & 0x80)) |
fe20ba70a drivers/rtc/: use... |
340 |
t->time.tm_sec = bcd2bin(reg[M41T80_REG_ALARM_SEC] & 0x7f); |
caaff562e rtc: add rtc-m41t... |
341 |
if (!(reg[M41T80_REG_ALARM_MIN] & 0x80)) |
fe20ba70a drivers/rtc/: use... |
342 |
t->time.tm_min = bcd2bin(reg[M41T80_REG_ALARM_MIN] & 0x7f); |
caaff562e rtc: add rtc-m41t... |
343 |
if (!(reg[M41T80_REG_ALARM_HOUR] & 0x80)) |
fe20ba70a drivers/rtc/: use... |
344 |
t->time.tm_hour = bcd2bin(reg[M41T80_REG_ALARM_HOUR] & 0x3f); |
caaff562e rtc: add rtc-m41t... |
345 |
if (!(reg[M41T80_REG_ALARM_DAY] & 0x80)) |
fe20ba70a drivers/rtc/: use... |
346 |
t->time.tm_mday = bcd2bin(reg[M41T80_REG_ALARM_DAY] & 0x3f); |
caaff562e rtc: add rtc-m41t... |
347 |
if (!(reg[M41T80_REG_ALARM_DAY] & 0x40)) |
fe20ba70a drivers/rtc/: use... |
348 |
t->time.tm_mon = bcd2bin(reg[M41T80_REG_ALARM_MON] & 0x1f) - 1; |
caaff562e rtc: add rtc-m41t... |
349 350 351 352 353 354 |
t->time.tm_year = -1; t->time.tm_wday = -1; t->time.tm_yday = -1; t->time.tm_isdst = -1; t->enabled = !!(reg[M41T80_REG_ALARM_MON] & M41T80_ALMON_AFE); t->pending = !!(reg[M41T80_REG_FLAGS] & M41T80_FLAGS_AF); |
408929bed rtc: m41t80: do n... |
355 |
return 0; |
caaff562e rtc: add rtc-m41t... |
356 357 358 359 360 |
} static struct rtc_class_ops m41t80_rtc_ops = { .read_time = m41t80_rtc_read_time, .set_time = m41t80_rtc_set_time, |
c3b79770e rtc: m41t80: Work... |
361 362 363 364 |
/* * XXX - m41t80 alarm functionality is reported broken. * until it is fixed, don't register alarm functions. * |
caaff562e rtc: add rtc-m41t... |
365 366 |
.read_alarm = m41t80_rtc_read_alarm, .set_alarm = m41t80_rtc_set_alarm, |
c3b79770e rtc: m41t80: Work... |
367 |
*/ |
caaff562e rtc: add rtc-m41t... |
368 |
.proc = m41t80_rtc_proc, |
c3b79770e rtc: m41t80: Work... |
369 370 371 |
/* * See above comment on broken alarm * |
16380c153 RTC: Convert rtc ... |
372 |
.alarm_irq_enable = m41t80_rtc_alarm_irq_enable, |
c3b79770e rtc: m41t80: Work... |
373 |
*/ |
caaff562e rtc: add rtc-m41t... |
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
}; #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE) static ssize_t m41t80_sysfs_show_flags(struct device *dev, struct device_attribute *attr, char *buf) { struct i2c_client *client = to_i2c_client(dev); int val; val = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS); if (val < 0) return -EIO; return sprintf(buf, "%#x ", val); } static DEVICE_ATTR(flags, S_IRUGO, m41t80_sysfs_show_flags, NULL); static ssize_t m41t80_sysfs_show_sqwfreq(struct device *dev, struct device_attribute *attr, char *buf) { struct i2c_client *client = to_i2c_client(dev); |
d3a126fcf rtc: rtc-m41t80.c... |
395 |
struct m41t80_data *clientdata = i2c_get_clientdata(client); |
f30281f4f rtc: add m41t62 s... |
396 |
int val, reg_sqw; |
caaff562e rtc: add rtc-m41t... |
397 |
|
d3a126fcf rtc: rtc-m41t80.c... |
398 399 |
if (!(clientdata->features & M41T80_FEATURE_SQ)) return -EINVAL; |
f30281f4f rtc: add m41t62 s... |
400 401 402 403 |
reg_sqw = M41T80_REG_SQW; if (clientdata->features & M41T80_FEATURE_SQ_ALT) reg_sqw = M41T80_REG_WDAY; val = i2c_smbus_read_byte_data(client, reg_sqw); |
caaff562e rtc: add rtc-m41t... |
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
if (val < 0) return -EIO; val = (val >> 4) & 0xf; switch (val) { case 0: break; case 1: val = 32768; break; default: val = 32768 >> val; } return sprintf(buf, "%d ", val); } static ssize_t m41t80_sysfs_set_sqwfreq(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct i2c_client *client = to_i2c_client(dev); |
d3a126fcf rtc: rtc-m41t80.c... |
424 |
struct m41t80_data *clientdata = i2c_get_clientdata(client); |
f30281f4f rtc: add m41t62 s... |
425 |
int almon, sqw, reg_sqw; |
caaff562e rtc: add rtc-m41t... |
426 |
int val = simple_strtoul(buf, NULL, 0); |
d3a126fcf rtc: rtc-m41t80.c... |
427 428 |
if (!(clientdata->features & M41T80_FEATURE_SQ)) return -EINVAL; |
caaff562e rtc: add rtc-m41t... |
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
if (val) { if (!is_power_of_2(val)) return -EINVAL; val = ilog2(val); if (val == 15) val = 1; else if (val < 14) val = 15 - val; else return -EINVAL; } /* disable SQW, set SQW frequency & re-enable */ almon = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON); if (almon < 0) return -EIO; |
f30281f4f rtc: add m41t62 s... |
444 445 446 447 |
reg_sqw = M41T80_REG_SQW; if (clientdata->features & M41T80_FEATURE_SQ_ALT) reg_sqw = M41T80_REG_WDAY; sqw = i2c_smbus_read_byte_data(client, reg_sqw); |
caaff562e rtc: add rtc-m41t... |
448 449 450 451 452 |
if (sqw < 0) return -EIO; sqw = (sqw & 0x0f) | (val << 4); if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON, almon & ~M41T80_ALMON_SQWE) < 0 || |
f30281f4f rtc: add m41t62 s... |
453 |
i2c_smbus_write_byte_data(client, reg_sqw, sqw) < 0) |
caaff562e rtc: add rtc-m41t... |
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
return -EIO; if (val && i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON, almon | M41T80_ALMON_SQWE) < 0) return -EIO; return count; } static DEVICE_ATTR(sqwfreq, S_IRUGO | S_IWUSR, m41t80_sysfs_show_sqwfreq, m41t80_sysfs_set_sqwfreq); static struct attribute *attrs[] = { &dev_attr_flags.attr, &dev_attr_sqwfreq.attr, NULL, }; static struct attribute_group attr_group = { .attrs = attrs, }; static int m41t80_sysfs_register(struct device *dev) { return sysfs_create_group(&dev->kobj, &attr_group); } #else static int m41t80_sysfs_register(struct device *dev) { return 0; } #endif |
617780d29 rtc: watchdog sup... |
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
#ifdef CONFIG_RTC_DRV_M41T80_WDT /* ***************************************************************************** * * Watchdog Driver * ***************************************************************************** */ static struct i2c_client *save_client; /* Default margin */ #define WD_TIMO 60 /* 1..31 seconds */ static int wdt_margin = WD_TIMO; module_param(wdt_margin, int, 0); MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 60s)"); static unsigned long wdt_is_open; static int boot_flag; /** * wdt_ping: * * Reload counter one with the watchdog timeout. We don't bother reloading * the cascade counter. */ static void wdt_ping(void) { unsigned char i2c_data[2]; struct i2c_msg msgs1[1] = { { .addr = save_client->addr, .flags = 0, .len = 2, .buf = i2c_data, }, }; |
d3a126fcf rtc: rtc-m41t80.c... |
519 |
struct m41t80_data *clientdata = i2c_get_clientdata(save_client); |
617780d29 rtc: watchdog sup... |
520 521 522 523 524 525 526 527 528 |
i2c_data[0] = 0x09; /* watchdog register */ if (wdt_margin > 31) i2c_data[1] = (wdt_margin & 0xFC) | 0x83; /* resolution = 4s */ else /* * WDS = 1 (0x80), mulitplier = WD_TIMO, resolution = 1s (0x02) */ i2c_data[1] = wdt_margin<<2 | 0x82; |
d3a126fcf rtc: rtc-m41t80.c... |
529 530 531 532 533 534 |
/* * M41T65 has three bits for watchdog resolution. Don't set bit 7, as * that would be an invalid resolution. */ if (clientdata->features & M41T80_FEATURE_WD) i2c_data[1] &= ~M41T80_WATCHDOG_RB2; |
617780d29 rtc: watchdog sup... |
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
i2c_transfer(save_client->adapter, msgs1, 1); } /** * wdt_disable: * * disables watchdog. */ static void wdt_disable(void) { unsigned char i2c_data[2], i2c_buf[0x10]; struct i2c_msg msgs0[2] = { { .addr = save_client->addr, .flags = 0, .len = 1, .buf = i2c_data, }, { .addr = save_client->addr, .flags = I2C_M_RD, .len = 1, .buf = i2c_buf, }, }; struct i2c_msg msgs1[1] = { { .addr = save_client->addr, .flags = 0, .len = 2, .buf = i2c_data, }, }; i2c_data[0] = 0x09; i2c_transfer(save_client->adapter, msgs0, 2); i2c_data[0] = 0x09; i2c_data[1] = 0x00; i2c_transfer(save_client->adapter, msgs1, 1); } /** * wdt_write: * @file: file handle to the watchdog * @buf: buffer to write (unused as data does not matter here * @count: count of bytes * @ppos: pointer to the position to write. No seeks allowed * * A write to a watchdog device is defined as a keepalive signal. Any * write of data will do, as we we don't define content meaning. */ static ssize_t wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { |
617780d29 rtc: watchdog sup... |
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
if (count) { wdt_ping(); return 1; } return 0; } static ssize_t wdt_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { return 0; } /** * wdt_ioctl: * @inode: inode of the device * @file: file handle to the device * @cmd: watchdog command * @arg: argument pointer * * The watchdog API defines a common set of functions for all watchdogs * according to their available features. We only actually usefully support * querying capabilities and current status. */ |
55929332c drivers: Push dow... |
614 |
static int wdt_ioctl(struct file *file, unsigned int cmd, |
617780d29 rtc: watchdog sup... |
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 |
unsigned long arg) { int new_margin, rv; static struct watchdog_info ident = { .options = WDIOF_POWERUNDER | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT, .firmware_version = 1, .identity = "M41T80 WTD" }; switch (cmd) { case WDIOC_GETSUPPORT: return copy_to_user((struct watchdog_info __user *)arg, &ident, sizeof(ident)) ? -EFAULT : 0; case WDIOC_GETSTATUS: case WDIOC_GETBOOTSTATUS: return put_user(boot_flag, (int __user *)arg); case WDIOC_KEEPALIVE: wdt_ping(); return 0; case WDIOC_SETTIMEOUT: if (get_user(new_margin, (int __user *)arg)) return -EFAULT; /* Arbitrary, can't find the card's limits */ if (new_margin < 1 || new_margin > 124) return -EINVAL; wdt_margin = new_margin; wdt_ping(); /* Fall */ case WDIOC_GETTIMEOUT: return put_user(wdt_margin, (int __user *)arg); case WDIOC_SETOPTIONS: if (copy_from_user(&rv, (int __user *)arg, sizeof(int))) return -EFAULT; if (rv & WDIOS_DISABLECARD) { |
4c228db0b rtc: m41t80: use ... |
653 654 |
pr_info("rtc-m41t80: disable watchdog "); |
617780d29 rtc: watchdog sup... |
655 656 657 658 |
wdt_disable(); } if (rv & WDIOS_ENABLECARD) { |
4c228db0b rtc: m41t80: use ... |
659 660 |
pr_info("rtc-m41t80: enable watchdog "); |
617780d29 rtc: watchdog sup... |
661 662 663 664 665 666 667 |
wdt_ping(); } return -EINVAL; } return -ENOTTY; } |
55929332c drivers: Push dow... |
668 669 670 671 |
static long wdt_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { int ret; |
613655fa3 drivers: autoconv... |
672 |
mutex_lock(&m41t80_rtc_mutex); |
55929332c drivers: Push dow... |
673 |
ret = wdt_ioctl(file, cmd, arg); |
613655fa3 drivers: autoconv... |
674 |
mutex_unlock(&m41t80_rtc_mutex); |
55929332c drivers: Push dow... |
675 676 677 |
return ret; } |
617780d29 rtc: watchdog sup... |
678 679 680 681 682 683 684 685 686 |
/** * wdt_open: * @inode: inode of device * @file: file handle to device * */ static int wdt_open(struct inode *inode, struct file *file) { if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) { |
613655fa3 drivers: autoconv... |
687 |
mutex_lock(&m41t80_rtc_mutex); |
410127353 rtc-rtc-m41t80: B... |
688 |
if (test_and_set_bit(0, &wdt_is_open)) { |
613655fa3 drivers: autoconv... |
689 |
mutex_unlock(&m41t80_rtc_mutex); |
617780d29 rtc: watchdog sup... |
690 |
return -EBUSY; |
410127353 rtc-rtc-m41t80: B... |
691 |
} |
617780d29 rtc: watchdog sup... |
692 693 694 695 |
/* * Activate */ wdt_is_open = 1; |
613655fa3 drivers: autoconv... |
696 |
mutex_unlock(&m41t80_rtc_mutex); |
09eeb1f5f rtc-m41t80: use n... |
697 |
return nonseekable_open(inode, file); |
617780d29 rtc: watchdog sup... |
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
} return -ENODEV; } /** * wdt_close: * @inode: inode to board * @file: file handle to board * */ static int wdt_release(struct inode *inode, struct file *file) { if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) clear_bit(0, &wdt_is_open); return 0; } /** * notify_sys: * @this: our notifier block * @code: the event being reported * @unused: unused * * Our notifier is called on system shutdowns. We want to turn the card * off at reboot otherwise the machine will reboot again during memory * test or worse yet during the following fsck. This would suck, in fact * trust me - if it happens it does suck. */ static int wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused) { if (code == SYS_DOWN || code == SYS_HALT) /* Disable Watchdog */ wdt_disable(); return NOTIFY_DONE; } static const struct file_operations wdt_fops = { .owner = THIS_MODULE, .read = wdt_read, |
55929332c drivers: Push dow... |
738 |
.unlocked_ioctl = wdt_unlocked_ioctl, |
617780d29 rtc: watchdog sup... |
739 740 741 |
.write = wdt_write, .open = wdt_open, .release = wdt_release, |
6038f373a llseek: automatic... |
742 |
.llseek = no_llseek, |
617780d29 rtc: watchdog sup... |
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 |
}; static struct miscdevice wdt_dev = { .minor = WATCHDOG_MINOR, .name = "watchdog", .fops = &wdt_fops, }; /* * The WDT card needs to learn about soft shutdowns in order to * turn the timebomb registers off. */ static struct notifier_block wdt_notifier = { .notifier_call = wdt_notify_sys, }; #endif /* CONFIG_RTC_DRV_M41T80_WDT */ |
caaff562e rtc: add rtc-m41t... |
759 760 761 762 763 764 765 |
/* ***************************************************************************** * * Driver Interface * ***************************************************************************** */ |
d2653e927 i2c: Add support ... |
766 767 |
static int m41t80_probe(struct i2c_client *client, const struct i2c_device_id *id) |
caaff562e rtc: add rtc-m41t... |
768 |
{ |
3760f7367 i2c: Convert most... |
769 |
int rc = 0; |
caaff562e rtc: add rtc-m41t... |
770 771 |
struct rtc_device *rtc = NULL; struct rtc_time tm; |
caaff562e rtc: add rtc-m41t... |
772 773 774 775 776 777 778 779 780 781 782 |
struct m41t80_data *clientdata = NULL; if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE_DATA)) { rc = -ENODEV; goto exit; } dev_info(&client->dev, "chip found, driver version " DRV_VERSION " "); |
caaff562e rtc: add rtc-m41t... |
783 784 785 786 787 |
clientdata = kzalloc(sizeof(*clientdata), GFP_KERNEL); if (!clientdata) { rc = -ENOMEM; goto exit; } |
a015dbc11 rtc: m41t80: Init... |
788 789 |
clientdata->features = id->driver_data; i2c_set_clientdata(client, clientdata); |
caaff562e rtc: add rtc-m41t... |
790 791 792 793 794 795 796 797 798 |
rtc = rtc_device_register(client->name, &client->dev, &m41t80_rtc_ops, THIS_MODULE); if (IS_ERR(rtc)) { rc = PTR_ERR(rtc); rtc = NULL; goto exit; } clientdata->rtc = rtc; |
caaff562e rtc: add rtc-m41t... |
799 800 801 802 803 804 805 |
/* Make sure HT (Halt Update) bit is cleared */ rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_HOUR); if (rc < 0) goto ht_err; if (rc & M41T80_ALHOUR_HT) { |
3760f7367 i2c: Convert most... |
806 |
if (clientdata->features & M41T80_FEATURE_HT) { |
caaff562e rtc: add rtc-m41t... |
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 |
m41t80_get_datetime(client, &tm); dev_info(&client->dev, "HT bit was set! "); dev_info(&client->dev, "Power Down at " "%04i-%02i-%02i %02i:%02i:%02i ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); } if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_HOUR, rc & ~M41T80_ALHOUR_HT) < 0) goto ht_err; } /* Make sure ST (stop) bit is cleared */ rc = i2c_smbus_read_byte_data(client, M41T80_REG_SEC); if (rc < 0) goto st_err; if (rc & M41T80_SEC_ST) { if (i2c_smbus_write_byte_data(client, M41T80_REG_SEC, rc & ~M41T80_SEC_ST) < 0) goto st_err; } rc = m41t80_sysfs_register(&client->dev); if (rc) goto exit; |
617780d29 rtc: watchdog sup... |
838 |
#ifdef CONFIG_RTC_DRV_M41T80_WDT |
3760f7367 i2c: Convert most... |
839 |
if (clientdata->features & M41T80_FEATURE_HT) { |
417607d05 RTC/watchdog: M41... |
840 |
save_client = client; |
617780d29 rtc: watchdog sup... |
841 842 843 844 845 846 847 848 |
rc = misc_register(&wdt_dev); if (rc) goto exit; rc = register_reboot_notifier(&wdt_notifier); if (rc) { misc_deregister(&wdt_dev); goto exit; } |
617780d29 rtc: watchdog sup... |
849 850 |
} #endif |
caaff562e rtc: add rtc-m41t... |
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 |
return 0; st_err: rc = -EIO; dev_err(&client->dev, "Can't clear ST bit "); goto exit; ht_err: rc = -EIO; dev_err(&client->dev, "Can't clear HT bit "); goto exit; exit: if (rtc) rtc_device_unregister(rtc); kfree(clientdata); return rc; } static int m41t80_remove(struct i2c_client *client) { struct m41t80_data *clientdata = i2c_get_clientdata(client); struct rtc_device *rtc = clientdata->rtc; |
617780d29 rtc: watchdog sup... |
875 |
#ifdef CONFIG_RTC_DRV_M41T80_WDT |
3760f7367 i2c: Convert most... |
876 |
if (clientdata->features & M41T80_FEATURE_HT) { |
617780d29 rtc: watchdog sup... |
877 878 879 880 |
misc_deregister(&wdt_dev); unregister_reboot_notifier(&wdt_notifier); } #endif |
caaff562e rtc: add rtc-m41t... |
881 882 883 884 885 886 887 888 889 |
if (rtc) rtc_device_unregister(rtc); kfree(clientdata); return 0; } static struct i2c_driver m41t80_driver = { .driver = { |
afe1ab4d5 correct name for ... |
890 |
.name = "rtc-m41t80", |
caaff562e rtc: add rtc-m41t... |
891 892 893 |
}, .probe = m41t80_probe, .remove = m41t80_remove, |
3760f7367 i2c: Convert most... |
894 |
.id_table = m41t80_id, |
caaff562e rtc: add rtc-m41t... |
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 |
}; static int __init m41t80_rtc_init(void) { return i2c_add_driver(&m41t80_driver); } static void __exit m41t80_rtc_exit(void) { i2c_del_driver(&m41t80_driver); } MODULE_AUTHOR("Alexander Bigga <ab@mycable.de>"); MODULE_DESCRIPTION("ST Microelectronics M41T80 series RTC I2C Client Driver"); MODULE_LICENSE("GPL"); MODULE_VERSION(DRV_VERSION); module_init(m41t80_rtc_init); module_exit(m41t80_rtc_exit); |