blob: 1a769275ab9d87ca39d22d53325ebae84876b910 [file] [log] [blame]
Atsushi Nemotocaaff562007-07-17 04:05:02 -07001/*
2 * I2C client/driver for the ST M41T80 family of i2c rtc chips.
3 *
4 * Author: Alexander Bigga <ab@mycable.de>
5 *
6 * Based on m41t00.c by Mark A. Greer <mgreer@mvista.com>
7 *
8 * 2006 (c) mycable GmbH
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15
Joe Perchesa737e832015-04-16 12:46:14 -070016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Maciej W. Rozycki35aa64f2008-07-23 21:30:29 -070018#include <linux/bcd.h>
19#include <linux/i2c.h>
Atsushi Nemotocaaff562007-07-17 04:05:02 -070020#include <linux/init.h>
Maciej W. Rozycki9fb1f682008-05-12 14:02:38 -070021#include <linux/kernel.h>
Maciej W. Rozycki35aa64f2008-07-23 21:30:29 -070022#include <linux/module.h>
23#include <linux/rtc.h>
Atsushi Nemotocaaff562007-07-17 04:05:02 -070024#include <linux/slab.h>
Arnd Bergmann613655f2010-06-02 14:28:52 +020025#include <linux/mutex.h>
Atsushi Nemotocaaff562007-07-17 04:05:02 -070026#include <linux/string.h>
Atsushi Nemoto617780d2007-07-17 04:05:04 -070027#ifdef CONFIG_RTC_DRV_M41T80_WDT
Atsushi Nemoto617780d2007-07-17 04:05:04 -070028#include <linux/fs.h>
29#include <linux/ioctl.h>
Maciej W. Rozycki35aa64f2008-07-23 21:30:29 -070030#include <linux/miscdevice.h>
31#include <linux/reboot.h>
32#include <linux/watchdog.h>
Atsushi Nemoto617780d2007-07-17 04:05:04 -070033#endif
Atsushi Nemotocaaff562007-07-17 04:05:02 -070034
Mylène Josserandf2b84ee2016-03-29 08:56:00 +020035#define M41T80_REG_SSEC 0x00
36#define M41T80_REG_SEC 0x01
37#define M41T80_REG_MIN 0x02
38#define M41T80_REG_HOUR 0x03
39#define M41T80_REG_WDAY 0x04
40#define M41T80_REG_DAY 0x05
41#define M41T80_REG_MON 0x06
42#define M41T80_REG_YEAR 0x07
43#define M41T80_REG_ALARM_MON 0x0a
44#define M41T80_REG_ALARM_DAY 0x0b
45#define M41T80_REG_ALARM_HOUR 0x0c
46#define M41T80_REG_ALARM_MIN 0x0d
47#define M41T80_REG_ALARM_SEC 0x0e
48#define M41T80_REG_FLAGS 0x0f
49#define M41T80_REG_SQW 0x13
Atsushi Nemotocaaff562007-07-17 04:05:02 -070050
51#define M41T80_DATETIME_REG_SIZE (M41T80_REG_YEAR + 1)
52#define M41T80_ALARM_REG_SIZE \
53 (M41T80_REG_ALARM_SEC + 1 - M41T80_REG_ALARM_MON)
54
55#define M41T80_SEC_ST (1 << 7) /* ST: Stop Bit */
56#define M41T80_ALMON_AFE (1 << 7) /* AFE: AF Enable Bit */
57#define M41T80_ALMON_SQWE (1 << 6) /* SQWE: SQW Enable Bit */
58#define M41T80_ALHOUR_HT (1 << 6) /* HT: Halt Update Bit */
59#define M41T80_FLAGS_AF (1 << 6) /* AF: Alarm Flag Bit */
60#define M41T80_FLAGS_BATT_LOW (1 << 4) /* BL: Battery Low Bit */
Steven A. Falcod3a126f2008-10-15 22:03:07 -070061#define M41T80_WATCHDOG_RB2 (1 << 7) /* RB: Watchdog resolution */
62#define M41T80_WATCHDOG_RB1 (1 << 1) /* RB: Watchdog resolution */
63#define M41T80_WATCHDOG_RB0 (1 << 0) /* RB: Watchdog resolution */
Atsushi Nemotocaaff562007-07-17 04:05:02 -070064
Steven A. Falcod3a126f2008-10-15 22:03:07 -070065#define M41T80_FEATURE_HT (1 << 0) /* Halt feature */
66#define M41T80_FEATURE_BL (1 << 1) /* Battery low indicator */
67#define M41T80_FEATURE_SQ (1 << 2) /* Squarewave feature */
68#define M41T80_FEATURE_WD (1 << 3) /* Extra watchdog resolution */
Daniel Glocknerf30281f2009-04-02 16:57:03 -070069#define M41T80_FEATURE_SQ_ALT (1 << 4) /* RSx bits are in reg 4 */
Atsushi Nemotocaaff562007-07-17 04:05:02 -070070
Arnd Bergmann613655f2010-06-02 14:28:52 +020071static DEFINE_MUTEX(m41t80_rtc_mutex);
Jean Delvare3760f732008-04-29 23:11:40 +020072static const struct i2c_device_id m41t80_id[] = {
Daniel Glocknerf30281f2009-04-02 16:57:03 -070073 { "m41t62", M41T80_FEATURE_SQ | M41T80_FEATURE_SQ_ALT },
Steven A. Falcod3a126f2008-10-15 22:03:07 -070074 { "m41t65", M41T80_FEATURE_HT | M41T80_FEATURE_WD },
75 { "m41t80", M41T80_FEATURE_SQ },
76 { "m41t81", M41T80_FEATURE_HT | M41T80_FEATURE_SQ},
77 { "m41t81s", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
78 { "m41t82", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
79 { "m41t83", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
80 { "m41st84", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
81 { "m41st85", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
82 { "m41st87", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
Wolfram Sang6b1a5232014-06-06 14:35:47 -070083 { "rv4162", M41T80_FEATURE_SQ | M41T80_FEATURE_WD | M41T80_FEATURE_SQ_ALT },
Jean Delvare3760f732008-04-29 23:11:40 +020084 { }
Atsushi Nemotocaaff562007-07-17 04:05:02 -070085};
Jean Delvare3760f732008-04-29 23:11:40 +020086MODULE_DEVICE_TABLE(i2c, m41t80_id);
Atsushi Nemotocaaff562007-07-17 04:05:02 -070087
88struct m41t80_data {
Jean Delvare3760f732008-04-29 23:11:40 +020089 u8 features;
Atsushi Nemotocaaff562007-07-17 04:05:02 -070090 struct rtc_device *rtc;
91};
92
93static int m41t80_get_datetime(struct i2c_client *client,
94 struct rtc_time *tm)
95{
Mylène Josserandf2b84ee2016-03-29 08:56:00 +020096 unsigned char buf[8];
97 int err;
Atsushi Nemotocaaff562007-07-17 04:05:02 -070098
Mylène Josserandf2b84ee2016-03-29 08:56:00 +020099 err = i2c_smbus_read_i2c_block_data(client, M41T80_REG_SSEC,
100 sizeof(buf), buf);
101 if (err < 0) {
102 dev_err(&client->dev, "Unable to read date\n");
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700103 return -EIO;
104 }
105
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700106 tm->tm_sec = bcd2bin(buf[M41T80_REG_SEC] & 0x7f);
107 tm->tm_min = bcd2bin(buf[M41T80_REG_MIN] & 0x7f);
108 tm->tm_hour = bcd2bin(buf[M41T80_REG_HOUR] & 0x3f);
109 tm->tm_mday = bcd2bin(buf[M41T80_REG_DAY] & 0x3f);
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700110 tm->tm_wday = buf[M41T80_REG_WDAY] & 0x07;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700111 tm->tm_mon = bcd2bin(buf[M41T80_REG_MON] & 0x1f) - 1;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700112
113 /* assume 20YY not 19YY, and ignore the Century Bit */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700114 tm->tm_year = bcd2bin(buf[M41T80_REG_YEAR]) + 100;
Wan ZongShunb485fe52010-08-10 18:02:15 -0700115 return rtc_valid_tm(tm);
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700116}
117
118/* Sets the given date and time to the real time clock. */
119static int m41t80_set_datetime(struct i2c_client *client, struct rtc_time *tm)
120{
Mylène Josserandf2b84ee2016-03-29 08:56:00 +0200121 unsigned char buf[8];
122 int err;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700123
Mylène Josserandf2b84ee2016-03-29 08:56:00 +0200124 if (tm->tm_year < 100 || tm->tm_year > 199)
Stefan Christbcebd812016-03-15 14:22:26 +0100125 return -EINVAL;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700126
Mylène Josserandf2b84ee2016-03-29 08:56:00 +0200127 buf[M41T80_REG_SSEC] = 0;
128 buf[M41T80_REG_SEC] = bin2bcd(tm->tm_sec);
129 buf[M41T80_REG_MIN] = bin2bcd(tm->tm_min);
130 buf[M41T80_REG_HOUR] = bin2bcd(tm->tm_hour);
131 buf[M41T80_REG_DAY] = bin2bcd(tm->tm_mday);
132 buf[M41T80_REG_MON] = bin2bcd(tm->tm_mon + 1);
133 buf[M41T80_REG_YEAR] = bin2bcd(tm->tm_year - 100);
134 buf[M41T80_REG_WDAY] = tm->tm_wday;
135
136 err = i2c_smbus_write_i2c_block_data(client, M41T80_REG_SSEC,
137 sizeof(buf), buf);
138 if (err < 0) {
139 dev_err(&client->dev, "Unable to write to date registers\n");
140 return err;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700141 }
Mylène Josserandf2b84ee2016-03-29 08:56:00 +0200142
143 return err;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700144}
145
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700146static int m41t80_rtc_proc(struct device *dev, struct seq_file *seq)
147{
148 struct i2c_client *client = to_i2c_client(dev);
149 struct m41t80_data *clientdata = i2c_get_clientdata(client);
150 u8 reg;
151
Jean Delvare3760f732008-04-29 23:11:40 +0200152 if (clientdata->features & M41T80_FEATURE_BL) {
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700153 reg = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
154 seq_printf(seq, "battery\t\t: %s\n",
155 (reg & M41T80_FLAGS_BATT_LOW) ? "exhausted" : "ok");
156 }
157 return 0;
158}
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700159
160static int m41t80_rtc_read_time(struct device *dev, struct rtc_time *tm)
161{
162 return m41t80_get_datetime(to_i2c_client(dev), tm);
163}
164
165static int m41t80_rtc_set_time(struct device *dev, struct rtc_time *tm)
166{
167 return m41t80_set_datetime(to_i2c_client(dev), tm);
168}
169
Paul Bolle48e97662012-10-04 17:14:35 -0700170/*
171 * XXX - m41t80 alarm functionality is reported broken.
172 * until it is fixed, don't register alarm functions.
173 */
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700174static struct rtc_class_ops m41t80_rtc_ops = {
175 .read_time = m41t80_rtc_read_time,
176 .set_time = m41t80_rtc_set_time,
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700177 .proc = m41t80_rtc_proc,
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700178};
179
Mylène Josserandef6b3122016-03-29 08:55:58 +0200180static ssize_t flags_show(struct device *dev,
181 struct device_attribute *attr, char *buf)
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700182{
183 struct i2c_client *client = to_i2c_client(dev);
184 int val;
185
186 val = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
187 if (val < 0)
Wolfram Sang85d77042014-06-06 14:35:46 -0700188 return val;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700189 return sprintf(buf, "%#x\n", val);
190}
Mylène Josserandef6b3122016-03-29 08:55:58 +0200191static DEVICE_ATTR_RO(flags);
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700192
Mylène Josserandef6b3122016-03-29 08:55:58 +0200193static ssize_t sqwfreq_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700195{
196 struct i2c_client *client = to_i2c_client(dev);
Steven A. Falcod3a126f2008-10-15 22:03:07 -0700197 struct m41t80_data *clientdata = i2c_get_clientdata(client);
Daniel Glocknerf30281f2009-04-02 16:57:03 -0700198 int val, reg_sqw;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700199
Steven A. Falcod3a126f2008-10-15 22:03:07 -0700200 if (!(clientdata->features & M41T80_FEATURE_SQ))
201 return -EINVAL;
202
Daniel Glocknerf30281f2009-04-02 16:57:03 -0700203 reg_sqw = M41T80_REG_SQW;
204 if (clientdata->features & M41T80_FEATURE_SQ_ALT)
205 reg_sqw = M41T80_REG_WDAY;
206 val = i2c_smbus_read_byte_data(client, reg_sqw);
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700207 if (val < 0)
Wolfram Sang85d77042014-06-06 14:35:46 -0700208 return val;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700209 val = (val >> 4) & 0xf;
210 switch (val) {
211 case 0:
212 break;
213 case 1:
214 val = 32768;
215 break;
216 default:
217 val = 32768 >> val;
218 }
219 return sprintf(buf, "%d\n", val);
220}
Mylène Josserandef6b3122016-03-29 08:55:58 +0200221
222static ssize_t sqwfreq_store(struct device *dev,
223 struct device_attribute *attr,
224 const char *buf, size_t count)
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700225{
226 struct i2c_client *client = to_i2c_client(dev);
Steven A. Falcod3a126f2008-10-15 22:03:07 -0700227 struct m41t80_data *clientdata = i2c_get_clientdata(client);
Wolfram Sang85d77042014-06-06 14:35:46 -0700228 int almon, sqw, reg_sqw, rc;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700229 int val = simple_strtoul(buf, NULL, 0);
230
Steven A. Falcod3a126f2008-10-15 22:03:07 -0700231 if (!(clientdata->features & M41T80_FEATURE_SQ))
232 return -EINVAL;
233
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700234 if (val) {
235 if (!is_power_of_2(val))
236 return -EINVAL;
237 val = ilog2(val);
238 if (val == 15)
239 val = 1;
240 else if (val < 14)
241 val = 15 - val;
242 else
243 return -EINVAL;
244 }
245 /* disable SQW, set SQW frequency & re-enable */
246 almon = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
247 if (almon < 0)
Wolfram Sang85d77042014-06-06 14:35:46 -0700248 return almon;
Daniel Glocknerf30281f2009-04-02 16:57:03 -0700249 reg_sqw = M41T80_REG_SQW;
250 if (clientdata->features & M41T80_FEATURE_SQ_ALT)
251 reg_sqw = M41T80_REG_WDAY;
252 sqw = i2c_smbus_read_byte_data(client, reg_sqw);
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700253 if (sqw < 0)
Wolfram Sang85d77042014-06-06 14:35:46 -0700254 return sqw;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700255 sqw = (sqw & 0x0f) | (val << 4);
Wolfram Sang85d77042014-06-06 14:35:46 -0700256
257 rc = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
258 almon & ~M41T80_ALMON_SQWE);
259 if (rc < 0)
260 return rc;
261
262 if (val) {
263 rc = i2c_smbus_write_byte_data(client, reg_sqw, sqw);
264 if (rc < 0)
265 return rc;
266
267 rc = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
268 almon | M41T80_ALMON_SQWE);
269 if (rc <0)
270 return rc;
271 }
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700272 return count;
273}
Mylène Josserandef6b3122016-03-29 08:55:58 +0200274static DEVICE_ATTR_RW(sqwfreq);
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700275
276static struct attribute *attrs[] = {
277 &dev_attr_flags.attr,
278 &dev_attr_sqwfreq.attr,
279 NULL,
280};
281static struct attribute_group attr_group = {
282 .attrs = attrs,
283};
284
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700285#ifdef CONFIG_RTC_DRV_M41T80_WDT
286/*
287 *****************************************************************************
288 *
289 * Watchdog Driver
290 *
291 *****************************************************************************
292 */
293static struct i2c_client *save_client;
294
295/* Default margin */
296#define WD_TIMO 60 /* 1..31 seconds */
297
298static int wdt_margin = WD_TIMO;
299module_param(wdt_margin, int, 0);
300MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 60s)");
301
302static unsigned long wdt_is_open;
303static int boot_flag;
304
305/**
306 * wdt_ping:
307 *
308 * Reload counter one with the watchdog timeout. We don't bother reloading
309 * the cascade counter.
310 */
311static void wdt_ping(void)
312{
313 unsigned char i2c_data[2];
314 struct i2c_msg msgs1[1] = {
315 {
316 .addr = save_client->addr,
317 .flags = 0,
318 .len = 2,
319 .buf = i2c_data,
320 },
321 };
Steven A. Falcod3a126f2008-10-15 22:03:07 -0700322 struct m41t80_data *clientdata = i2c_get_clientdata(save_client);
323
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700324 i2c_data[0] = 0x09; /* watchdog register */
325
326 if (wdt_margin > 31)
327 i2c_data[1] = (wdt_margin & 0xFC) | 0x83; /* resolution = 4s */
328 else
329 /*
330 * WDS = 1 (0x80), mulitplier = WD_TIMO, resolution = 1s (0x02)
331 */
332 i2c_data[1] = wdt_margin<<2 | 0x82;
333
Steven A. Falcod3a126f2008-10-15 22:03:07 -0700334 /*
335 * M41T65 has three bits for watchdog resolution. Don't set bit 7, as
336 * that would be an invalid resolution.
337 */
338 if (clientdata->features & M41T80_FEATURE_WD)
339 i2c_data[1] &= ~M41T80_WATCHDOG_RB2;
340
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700341 i2c_transfer(save_client->adapter, msgs1, 1);
342}
343
344/**
345 * wdt_disable:
346 *
347 * disables watchdog.
348 */
349static void wdt_disable(void)
350{
351 unsigned char i2c_data[2], i2c_buf[0x10];
352 struct i2c_msg msgs0[2] = {
353 {
354 .addr = save_client->addr,
355 .flags = 0,
356 .len = 1,
357 .buf = i2c_data,
358 },
359 {
360 .addr = save_client->addr,
361 .flags = I2C_M_RD,
362 .len = 1,
363 .buf = i2c_buf,
364 },
365 };
366 struct i2c_msg msgs1[1] = {
367 {
368 .addr = save_client->addr,
369 .flags = 0,
370 .len = 2,
371 .buf = i2c_data,
372 },
373 };
374
375 i2c_data[0] = 0x09;
376 i2c_transfer(save_client->adapter, msgs0, 2);
377
378 i2c_data[0] = 0x09;
379 i2c_data[1] = 0x00;
380 i2c_transfer(save_client->adapter, msgs1, 1);
381}
382
383/**
384 * wdt_write:
385 * @file: file handle to the watchdog
386 * @buf: buffer to write (unused as data does not matter here
387 * @count: count of bytes
388 * @ppos: pointer to the position to write. No seeks allowed
389 *
390 * A write to a watchdog device is defined as a keepalive signal. Any
391 * write of data will do, as we we don't define content meaning.
392 */
393static ssize_t wdt_write(struct file *file, const char __user *buf,
394 size_t count, loff_t *ppos)
395{
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700396 if (count) {
397 wdt_ping();
398 return 1;
399 }
400 return 0;
401}
402
403static ssize_t wdt_read(struct file *file, char __user *buf,
404 size_t count, loff_t *ppos)
405{
406 return 0;
407}
408
409/**
410 * wdt_ioctl:
411 * @inode: inode of the device
412 * @file: file handle to the device
413 * @cmd: watchdog command
414 * @arg: argument pointer
415 *
416 * The watchdog API defines a common set of functions for all watchdogs
417 * according to their available features. We only actually usefully support
418 * querying capabilities and current status.
419 */
Arnd Bergmann55929332010-04-27 00:24:05 +0200420static int wdt_ioctl(struct file *file, unsigned int cmd,
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700421 unsigned long arg)
422{
423 int new_margin, rv;
424 static struct watchdog_info ident = {
425 .options = WDIOF_POWERUNDER | WDIOF_KEEPALIVEPING |
426 WDIOF_SETTIMEOUT,
427 .firmware_version = 1,
428 .identity = "M41T80 WTD"
429 };
430
431 switch (cmd) {
432 case WDIOC_GETSUPPORT:
433 return copy_to_user((struct watchdog_info __user *)arg, &ident,
434 sizeof(ident)) ? -EFAULT : 0;
435
436 case WDIOC_GETSTATUS:
437 case WDIOC_GETBOOTSTATUS:
438 return put_user(boot_flag, (int __user *)arg);
439 case WDIOC_KEEPALIVE:
440 wdt_ping();
441 return 0;
442 case WDIOC_SETTIMEOUT:
443 if (get_user(new_margin, (int __user *)arg))
444 return -EFAULT;
445 /* Arbitrary, can't find the card's limits */
446 if (new_margin < 1 || new_margin > 124)
447 return -EINVAL;
448 wdt_margin = new_margin;
449 wdt_ping();
450 /* Fall */
451 case WDIOC_GETTIMEOUT:
452 return put_user(wdt_margin, (int __user *)arg);
453
454 case WDIOC_SETOPTIONS:
455 if (copy_from_user(&rv, (int __user *)arg, sizeof(int)))
456 return -EFAULT;
457
458 if (rv & WDIOS_DISABLECARD) {
Joe Perchesa737e832015-04-16 12:46:14 -0700459 pr_info("disable watchdog\n");
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700460 wdt_disable();
461 }
462
463 if (rv & WDIOS_ENABLECARD) {
Joe Perchesa737e832015-04-16 12:46:14 -0700464 pr_info("enable watchdog\n");
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700465 wdt_ping();
466 }
467
468 return -EINVAL;
469 }
470 return -ENOTTY;
471}
472
Arnd Bergmann55929332010-04-27 00:24:05 +0200473static long wdt_unlocked_ioctl(struct file *file, unsigned int cmd,
474 unsigned long arg)
475{
476 int ret;
477
Arnd Bergmann613655f2010-06-02 14:28:52 +0200478 mutex_lock(&m41t80_rtc_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200479 ret = wdt_ioctl(file, cmd, arg);
Arnd Bergmann613655f2010-06-02 14:28:52 +0200480 mutex_unlock(&m41t80_rtc_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200481
482 return ret;
483}
484
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700485/**
486 * wdt_open:
487 * @inode: inode of device
488 * @file: file handle to device
489 *
490 */
491static int wdt_open(struct inode *inode, struct file *file)
492{
493 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
Arnd Bergmann613655f2010-06-02 14:28:52 +0200494 mutex_lock(&m41t80_rtc_mutex);
Arnd Bergmann41012732008-05-20 19:16:39 +0200495 if (test_and_set_bit(0, &wdt_is_open)) {
Arnd Bergmann613655f2010-06-02 14:28:52 +0200496 mutex_unlock(&m41t80_rtc_mutex);
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700497 return -EBUSY;
Arnd Bergmann41012732008-05-20 19:16:39 +0200498 }
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700499 /*
500 * Activate
501 */
502 wdt_is_open = 1;
Arnd Bergmann613655f2010-06-02 14:28:52 +0200503 mutex_unlock(&m41t80_rtc_mutex);
Jan Blunck09eeb1f2010-05-26 14:44:47 -0700504 return nonseekable_open(inode, file);
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700505 }
506 return -ENODEV;
507}
508
509/**
510 * wdt_close:
511 * @inode: inode to board
512 * @file: file handle to board
513 *
514 */
515static int wdt_release(struct inode *inode, struct file *file)
516{
517 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
518 clear_bit(0, &wdt_is_open);
519 return 0;
520}
521
522/**
523 * notify_sys:
524 * @this: our notifier block
525 * @code: the event being reported
526 * @unused: unused
527 *
528 * Our notifier is called on system shutdowns. We want to turn the card
529 * off at reboot otherwise the machine will reboot again during memory
530 * test or worse yet during the following fsck. This would suck, in fact
531 * trust me - if it happens it does suck.
532 */
533static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
534 void *unused)
535{
536 if (code == SYS_DOWN || code == SYS_HALT)
537 /* Disable Watchdog */
538 wdt_disable();
539 return NOTIFY_DONE;
540}
541
542static const struct file_operations wdt_fops = {
543 .owner = THIS_MODULE,
544 .read = wdt_read,
Arnd Bergmann55929332010-04-27 00:24:05 +0200545 .unlocked_ioctl = wdt_unlocked_ioctl,
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700546 .write = wdt_write,
547 .open = wdt_open,
548 .release = wdt_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200549 .llseek = no_llseek,
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700550};
551
552static struct miscdevice wdt_dev = {
553 .minor = WATCHDOG_MINOR,
554 .name = "watchdog",
555 .fops = &wdt_fops,
556};
557
558/*
559 * The WDT card needs to learn about soft shutdowns in order to
560 * turn the timebomb registers off.
561 */
562static struct notifier_block wdt_notifier = {
563 .notifier_call = wdt_notify_sys,
564};
565#endif /* CONFIG_RTC_DRV_M41T80_WDT */
566
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700567/*
568 *****************************************************************************
569 *
570 * Driver Interface
571 *
572 *****************************************************************************
573 */
Mylène Josserandef6b3122016-03-29 08:55:58 +0200574
575static void m41t80_remove_sysfs_group(void *_dev)
576{
577 struct device *dev = _dev;
578
579 sysfs_remove_group(&dev->kobj, &attr_group);
580}
581
Jean Delvared2653e92008-04-29 23:11:39 +0200582static int m41t80_probe(struct i2c_client *client,
583 const struct i2c_device_id *id)
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700584{
Mylène Josserandf2b84ee2016-03-29 08:56:00 +0200585 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Jean Delvare3760f732008-04-29 23:11:40 +0200586 int rc = 0;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700587 struct rtc_device *rtc = NULL;
588 struct rtc_time tm;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700589 struct m41t80_data *clientdata = NULL;
590
Mylène Josserandf2b84ee2016-03-29 08:56:00 +0200591 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK |
592 I2C_FUNC_SMBUS_BYTE_DATA)) {
593 dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
Wolfram Sangc67fedf2014-06-06 14:35:45 -0700594 return -ENODEV;
Mylène Josserandf2b84ee2016-03-29 08:56:00 +0200595 }
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700596
Jingoo Han4ebabb72013-04-29 16:20:42 -0700597 clientdata = devm_kzalloc(&client->dev, sizeof(*clientdata),
598 GFP_KERNEL);
Wolfram Sangc67fedf2014-06-06 14:35:45 -0700599 if (!clientdata)
600 return -ENOMEM;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700601
John Stultza015dbc2011-05-06 17:24:27 -0700602 clientdata->features = id->driver_data;
603 i2c_set_clientdata(client, clientdata);
604
Jingoo Han4ebabb72013-04-29 16:20:42 -0700605 rtc = devm_rtc_device_register(&client->dev, client->name,
606 &m41t80_rtc_ops, THIS_MODULE);
Wolfram Sangc67fedf2014-06-06 14:35:45 -0700607 if (IS_ERR(rtc))
608 return PTR_ERR(rtc);
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700609
610 clientdata->rtc = rtc;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700611
612 /* Make sure HT (Halt Update) bit is cleared */
613 rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_HOUR);
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700614
Wolfram Sangc67fedf2014-06-06 14:35:45 -0700615 if (rc >= 0 && rc & M41T80_ALHOUR_HT) {
Jean Delvare3760f732008-04-29 23:11:40 +0200616 if (clientdata->features & M41T80_FEATURE_HT) {
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700617 m41t80_get_datetime(client, &tm);
618 dev_info(&client->dev, "HT bit was set!\n");
619 dev_info(&client->dev,
620 "Power Down at "
621 "%04i-%02i-%02i %02i:%02i:%02i\n",
622 tm.tm_year + 1900,
623 tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
624 tm.tm_min, tm.tm_sec);
625 }
Wolfram Sangc67fedf2014-06-06 14:35:45 -0700626 rc = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_HOUR,
627 rc & ~M41T80_ALHOUR_HT);
628 }
629
630 if (rc < 0) {
631 dev_err(&client->dev, "Can't clear HT bit\n");
Wolfram Sang85d77042014-06-06 14:35:46 -0700632 return rc;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700633 }
634
635 /* Make sure ST (stop) bit is cleared */
636 rc = i2c_smbus_read_byte_data(client, M41T80_REG_SEC);
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700637
Wolfram Sangc67fedf2014-06-06 14:35:45 -0700638 if (rc >= 0 && rc & M41T80_SEC_ST)
639 rc = i2c_smbus_write_byte_data(client, M41T80_REG_SEC,
640 rc & ~M41T80_SEC_ST);
641 if (rc < 0) {
642 dev_err(&client->dev, "Can't clear ST bit\n");
Wolfram Sang85d77042014-06-06 14:35:46 -0700643 return rc;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700644 }
645
Mylène Josserandef6b3122016-03-29 08:55:58 +0200646 /* Export sysfs entries */
647 rc = sysfs_create_group(&(&client->dev)->kobj, &attr_group);
648 if (rc) {
649 dev_err(&client->dev, "Failed to create sysfs group: %d\n", rc);
Wolfram Sangc67fedf2014-06-06 14:35:45 -0700650 return rc;
Mylène Josserandef6b3122016-03-29 08:55:58 +0200651 }
652
653 rc = devm_add_action(&client->dev, m41t80_remove_sysfs_group,
654 &client->dev);
655 if (rc) {
656 m41t80_remove_sysfs_group(&client->dev);
657 dev_err(&client->dev,
658 "Failed to add sysfs cleanup action: %d\n", rc);
659 return rc;
660 }
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700661
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700662#ifdef CONFIG_RTC_DRV_M41T80_WDT
Jean Delvare3760f732008-04-29 23:11:40 +0200663 if (clientdata->features & M41T80_FEATURE_HT) {
Maciej W. Rozycki417607d2008-05-12 14:02:35 -0700664 save_client = client;
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700665 rc = misc_register(&wdt_dev);
666 if (rc)
Wolfram Sangc67fedf2014-06-06 14:35:45 -0700667 return rc;
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700668 rc = register_reboot_notifier(&wdt_notifier);
669 if (rc) {
670 misc_deregister(&wdt_dev);
Wolfram Sangc67fedf2014-06-06 14:35:45 -0700671 return rc;
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700672 }
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700673 }
674#endif
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700675 return 0;
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700676}
677
678static int m41t80_remove(struct i2c_client *client)
679{
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700680#ifdef CONFIG_RTC_DRV_M41T80_WDT
Jingoo Han4ebabb72013-04-29 16:20:42 -0700681 struct m41t80_data *clientdata = i2c_get_clientdata(client);
682
Jean Delvare3760f732008-04-29 23:11:40 +0200683 if (clientdata->features & M41T80_FEATURE_HT) {
Atsushi Nemoto617780d2007-07-17 04:05:04 -0700684 misc_deregister(&wdt_dev);
685 unregister_reboot_notifier(&wdt_notifier);
686 }
687#endif
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700688
689 return 0;
690}
691
692static struct i2c_driver m41t80_driver = {
693 .driver = {
David Brownellafe1ab42007-08-22 14:01:27 -0700694 .name = "rtc-m41t80",
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700695 },
696 .probe = m41t80_probe,
697 .remove = m41t80_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200698 .id_table = m41t80_id,
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700699};
700
Axel Lin0abc9202012-03-23 15:02:31 -0700701module_i2c_driver(m41t80_driver);
Atsushi Nemotocaaff562007-07-17 04:05:02 -0700702
703MODULE_AUTHOR("Alexander Bigga <ab@mycable.de>");
704MODULE_DESCRIPTION("ST Microelectronics M41T80 series RTC I2C Client Driver");
705MODULE_LICENSE("GPL");