blob: d158a640299e8ed88f7c66772c542000148c3ea5 [file] [log] [blame]
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -07001/*
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 Grandegger3c2b9072009-06-17 16:26:11 -070021#include <linux/bcd.h>
Alexandre Belloni2ddd1862015-07-25 11:50:22 +020022#include <linux/bitops.h>
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070023#include <linux/i2c.h>
Alexandre Belloni15d3bdc2015-07-24 15:50:23 +020024#include <linux/kernel.h>
25#include <linux/module.h>
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070026#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 Belloni2ddd1862015-07-25 11:50:22 +020049#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 Grandegger3c2b9072009-06-17 16:26:11 -070053
Alexandre Belloni2ddd1862015-07-25 11:50:22 +020054#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 Grandegger3c2b9072009-06-17 16:26:11 -070060
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
66static const struct i2c_device_id rx8025_id[] = {
67 { "rx8025", 0 },
68 { }
69};
70MODULE_DEVICE_TABLE(i2c, rx8025_id);
71
72struct rx8025_data {
73 struct i2c_client *client;
74 struct rtc_device *rtc;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070075 u8 ctrl1;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070076};
77
78static int rx8025_read_reg(struct i2c_client *client, int number, u8 *value)
79{
Alexandre Belloni2e10e742015-07-25 15:54:59 +020080 int ret = i2c_smbus_read_byte_data(client, number << 4);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070081
82 if (ret < 0) {
83 dev_err(&client->dev, "Unable to read register #%d\n", number);
84 return ret;
85 }
86
87 *value = ret;
88 return 0;
89}
90
91static int rx8025_read_regs(struct i2c_client *client,
92 int number, u8 length, u8 *values)
93{
Alexandre Belloni2e10e742015-07-25 15:54:59 +020094 int ret = i2c_smbus_read_i2c_block_data(client, number << 4,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070095 length, values);
96
97 if (ret != length) {
98 dev_err(&client->dev, "Unable to read registers #%d..#%d\n",
99 number, number + length - 1);
100 return ret < 0 ? ret : -EIO;
101 }
102
103 return 0;
104}
105
106static int rx8025_write_reg(struct i2c_client *client, int number, u8 value)
107{
108 int ret = i2c_smbus_write_byte_data(client, number << 4, value);
109
110 if (ret)
111 dev_err(&client->dev, "Unable to write register #%d\n",
112 number);
113
114 return ret;
115}
116
117static int rx8025_write_regs(struct i2c_client *client,
118 int number, u8 length, u8 *values)
119{
Alexandre Belloni2e10e742015-07-25 15:54:59 +0200120 int ret = i2c_smbus_write_i2c_block_data(client, number << 4,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700121 length, values);
122
123 if (ret)
124 dev_err(&client->dev, "Unable to write registers #%d..#%d\n",
125 number, number + length - 1);
126
127 return ret;
128}
129
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200130static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700131{
132 struct i2c_client *client = dev_id;
133 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700134 u8 status;
135
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700136 if (rx8025_read_reg(client, RX8025_REG_CTRL2, &status))
137 goto out;
138
139 if (!(status & RX8025_BIT_CTRL2_XST))
140 dev_warn(&client->dev, "Oscillation stop was detected,"
141 "you may have to readjust the clock\n");
142
143 if (status & RX8025_BIT_CTRL2_CTFG) {
144 /* periodic */
145 status &= ~RX8025_BIT_CTRL2_CTFG;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700146 rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700147 }
148
149 if (status & RX8025_BIT_CTRL2_DAFG) {
150 /* alarm */
151 status &= RX8025_BIT_CTRL2_DAFG;
152 if (rx8025_write_reg(client, RX8025_REG_CTRL1,
153 rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
154 goto out;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700155 rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700156 }
157
158 /* acknowledge IRQ */
159 rx8025_write_reg(client, RX8025_REG_CTRL2,
160 status | RX8025_BIT_CTRL2_XST);
161
162out:
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200163 return IRQ_HANDLED;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700164}
165
166static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
167{
168 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200169 u8 date[7], ctrl;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700170 int err;
171
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200172 err = rx8025_read_reg(rx8025->client, RX8025_REG_CTRL2, &ctrl);
173 if (err)
174 return err;
175
176 if (ctrl & RX8025_BIT_CTRL2_PON) {
177 dev_warn(dev, "power-on reset detected, date is invalid\n");
178 return -EINVAL;
179 }
180
181 if (!(ctrl & RX8025_BIT_CTRL2_XST)) {
182 dev_warn(dev, "crystal stopped, date is invalid\n");
183 return -EINVAL;
184 }
185
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700186 err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date);
187 if (err)
188 return err;
189
190 dev_dbg(dev, "%s: read 0x%02x 0x%02x "
191 "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__,
192 date[0], date[1], date[2], date[3], date[4],
193 date[5], date[6]);
194
195 dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
196 dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
197 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
198 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
199 else
200 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
201 + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
202
203 dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
204 dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
Alexandre Belloni32672c52015-07-25 12:07:37 +0200205 dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700206
207 dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__,
208 dt->tm_sec, dt->tm_min, dt->tm_hour,
209 dt->tm_mday, dt->tm_mon, dt->tm_year);
210
211 return rtc_valid_tm(dt);
212}
213
214static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
215{
216 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
217 u8 date[7];
218
Alexandre Belloni32672c52015-07-25 12:07:37 +0200219 if ((dt->tm_year < 100) || (dt->tm_year > 199))
220 return -EINVAL;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700221
222 /*
223 * Here the read-only bits are written as "0". I'm not sure if that
224 * is sound.
225 */
226 date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
227 date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
228 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
229 date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
230 else
231 date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
232 | bin2bcd((dt->tm_hour + 11) % 12 + 1);
233
234 date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
235 date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
236 date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
Alexandre Belloni32672c52015-07-25 12:07:37 +0200237 date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700238
239 dev_dbg(dev,
240 "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
241 __func__,
242 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
243
244 return rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date);
245}
246
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200247static int rx8025_init_client(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700248{
249 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
250 u8 ctrl[2], ctrl2;
251 int need_clear = 0;
252 int err;
253
254 err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl);
255 if (err)
256 goto out;
257
258 /* Keep test bit zero ! */
259 rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
260
261 if (ctrl[1] & RX8025_BIT_CTRL2_PON) {
262 dev_warn(&client->dev, "power-on reset was detected, "
263 "you may have to readjust the clock\n");
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200264 need_clear = 1;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700265 }
266
267 if (ctrl[1] & RX8025_BIT_CTRL2_VDET) {
268 dev_warn(&client->dev, "a power voltage drop was detected, "
269 "you may have to readjust the clock\n");
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200270 need_clear = 1;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700271 }
272
273 if (!(ctrl[1] & RX8025_BIT_CTRL2_XST)) {
274 dev_warn(&client->dev, "Oscillation stop was detected,"
275 "you may have to readjust the clock\n");
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200276 need_clear = 1;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700277 }
278
279 if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
280 dev_warn(&client->dev, "Alarm was detected\n");
281 need_clear = 1;
282 }
283
284 if (!(ctrl[1] & RX8025_BIT_CTRL2_CTFG))
285 need_clear = 1;
286
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200287 if (need_clear) {
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700288 ctrl2 = ctrl[0];
289 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET |
290 RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
291 RX8025_BIT_CTRL2_DAFG);
292 ctrl2 |= RX8025_BIT_CTRL2_XST;
293
294 err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
295 }
296out:
297 return err;
298}
299
300/* Alarm support */
301static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
302{
303 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
304 struct i2c_client *client = rx8025->client;
305 u8 ctrl2, ald[2];
306 int err;
307
308 if (client->irq <= 0)
309 return -EINVAL;
310
311 err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
312 if (err)
313 return err;
314
315 err = rx8025_read_reg(client, RX8025_REG_CTRL2, &ctrl2);
316 if (err)
317 return err;
318
319 dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
320 __func__, ald[0], ald[1], ctrl2);
321
322 /* Hardware alarms precision is 1 minute! */
323 t->time.tm_sec = 0;
324 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
325 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
326 t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
327 else
328 t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
329 + (ald[1] & 0x20 ? 12 : 0);
330
331 t->time.tm_wday = -1;
332 t->time.tm_mday = -1;
333 t->time.tm_mon = -1;
334 t->time.tm_year = -1;
335
336 dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n",
337 __func__,
338 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
339 t->time.tm_mday, t->time.tm_mon, t->time.tm_year);
340 t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
341 t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
342
343 return err;
344}
345
346static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
347{
348 struct i2c_client *client = to_i2c_client(dev);
349 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
350 u8 ald[2];
351 int err;
352
353 if (client->irq <= 0)
354 return -EINVAL;
355
356 /* Hardware alarm precision is 1 minute! */
357 ald[0] = bin2bcd(t->time.tm_min);
358 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
359 ald[1] = bin2bcd(t->time.tm_hour);
360 else
361 ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
362 | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
363
364 dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
365
366 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
367 rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
368 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
369 rx8025->ctrl1);
370 if (err)
371 return err;
372 }
373 err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald);
374 if (err)
375 return err;
376
377 if (t->enabled) {
378 rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
379 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
380 rx8025->ctrl1);
381 if (err)
382 return err;
383 }
384
385 return 0;
386}
387
388static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
389{
390 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
391 u8 ctrl1;
392 int err;
393
394 ctrl1 = rx8025->ctrl1;
395 if (enabled)
396 ctrl1 |= RX8025_BIT_CTRL1_DALE;
397 else
398 ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
399
400 if (ctrl1 != rx8025->ctrl1) {
401 rx8025->ctrl1 = ctrl1;
402 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
403 rx8025->ctrl1);
404 if (err)
405 return err;
406 }
407 return 0;
408}
409
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700410static struct rtc_class_ops rx8025_rtc_ops = {
411 .read_time = rx8025_get_time,
412 .set_time = rx8025_set_time,
413 .read_alarm = rx8025_read_alarm,
414 .set_alarm = rx8025_set_alarm,
415 .alarm_irq_enable = rx8025_alarm_irq_enable,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700416};
417
418/*
419 * Clock precision adjustment support
420 *
421 * According to the RX8025 SA/NB application manual the frequency and
Thomas Weberb770ffd2010-07-19 08:22:43 +0200422 * temperature characteristics can be approximated using the following
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700423 * equation:
424 *
425 * df = a * (ut - t)**2
426 *
427 * df: Frequency deviation in any temperature
428 * a : Coefficient = (-35 +-5) * 10**-9
429 * ut: Ultimate temperature in degree = +25 +-5 degree
430 * t : Any temperature in degree
431 *
432 * Note that the clock adjustment in ppb must be entered (which is
433 * the negative value of the deviation).
434 */
435static int rx8025_get_clock_adjust(struct device *dev, int *adj)
436{
437 struct i2c_client *client = to_i2c_client(dev);
438 u8 digoff;
439 int err;
440
441 err = rx8025_read_reg(client, RX8025_REG_DIGOFF, &digoff);
442 if (err)
443 return err;
444
445 *adj = digoff >= 64 ? digoff - 128 : digoff;
446 if (*adj > 0)
447 (*adj)--;
448 *adj *= -RX8025_ADJ_RESOLUTION;
449
450 return 0;
451}
452
453static int rx8025_set_clock_adjust(struct device *dev, int adj)
454{
455 struct i2c_client *client = to_i2c_client(dev);
456 u8 digoff;
457 int err;
458
459 adj /= -RX8025_ADJ_RESOLUTION;
460 if (adj > RX8025_ADJ_DATA_MAX)
461 adj = RX8025_ADJ_DATA_MAX;
462 else if (adj < RX8025_ADJ_DATA_MIN)
463 adj = RX8025_ADJ_DATA_MIN;
464 else if (adj > 0)
465 adj++;
466 else if (adj < 0)
467 adj += 128;
468 digoff = adj;
469
470 err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
471 if (err)
472 return err;
473
474 dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff);
475
476 return 0;
477}
478
479static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
480 struct device_attribute *attr,
481 char *buf)
482{
483 int err, adj;
484
485 err = rx8025_get_clock_adjust(dev, &adj);
486 if (err)
487 return err;
488
489 return sprintf(buf, "%d\n", adj);
490}
491
492static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
493 struct device_attribute *attr,
494 const char *buf, size_t count)
495{
496 int adj, err;
497
498 if (sscanf(buf, "%i", &adj) != 1)
499 return -EINVAL;
500
501 err = rx8025_set_clock_adjust(dev, adj);
502
503 return err ? err : count;
504}
505
506static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
507 rx8025_sysfs_show_clock_adjust,
508 rx8025_sysfs_store_clock_adjust);
509
510static int rx8025_sysfs_register(struct device *dev)
511{
512 return device_create_file(dev, &dev_attr_clock_adjust_ppb);
513}
514
515static void rx8025_sysfs_unregister(struct device *dev)
516{
517 device_remove_file(dev, &dev_attr_clock_adjust_ppb);
518}
519
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800520static int rx8025_probe(struct i2c_client *client,
521 const struct i2c_device_id *id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700522{
523 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
524 struct rx8025_data *rx8025;
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200525 int err = 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700526
527 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
528 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
529 dev_err(&adapter->dev,
530 "doesn't support required functionality\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200531 return -EIO;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700532 }
533
Jingoo Hanfac42b42013-07-03 15:07:10 -0700534 rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700535 if (!rx8025) {
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200536 return -ENOMEM;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700537 }
538
539 rx8025->client = client;
540 i2c_set_clientdata(client, rx8025);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700541
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200542 err = rx8025_init_client(client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700543 if (err)
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200544 return err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700545
Jingoo Hanfac42b42013-07-03 15:07:10 -0700546 rx8025->rtc = devm_rtc_device_register(&client->dev, client->name,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700547 &rx8025_rtc_ops, THIS_MODULE);
548 if (IS_ERR(rx8025->rtc)) {
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700549 dev_err(&client->dev, "unable to register the class device\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200550 return PTR_ERR(rx8025->rtc);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700551 }
552
553 if (client->irq > 0) {
554 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
Alexandre Bellonif0b63a12015-07-24 16:07:30 +0200555 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
556 rx8025_handle_irq, 0, "rx8025",
557 client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700558 if (err) {
559 dev_err(&client->dev, "unable to request IRQ\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200560 return err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700561 }
562 }
563
564 rx8025->rtc->irq_freq = 1;
565 rx8025->rtc->max_user_freq = 1;
566
567 err = rx8025_sysfs_register(&client->dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700568 return err;
569}
570
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800571static int rx8025_remove(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700572{
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700573 rx8025_sysfs_unregister(&client->dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700574 return 0;
575}
576
577static struct i2c_driver rx8025_driver = {
578 .driver = {
579 .name = "rtc-rx8025",
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700580 },
581 .probe = rx8025_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800582 .remove = rx8025_remove,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700583 .id_table = rx8025_id,
584};
585
Axel Lin0abc9202012-03-23 15:02:31 -0700586module_i2c_driver(rx8025_driver);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700587
588MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
589MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
590MODULE_LICENSE("GPL");