blob: bf96f40fd200f3e8324de5f0127c2c6bd738d286 [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
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020078static s32 rx8025_read_reg(const struct i2c_client *client, u8 number)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070079{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020080 return i2c_smbus_read_byte_data(client, number << 4);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070081}
82
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020083static int rx8025_read_regs(const struct i2c_client *client,
84 u8 number, u8 length, u8 *values)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070085{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020086 int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length,
87 values);
88 if (ret != length)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070089 return ret < 0 ? ret : -EIO;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070090
91 return 0;
92}
93
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020094static s32 rx8025_write_reg(const struct i2c_client *client, u8 number,
95 u8 value)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070096{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020097 return i2c_smbus_write_byte_data(client, number << 4, value);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070098}
99
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200100static s32 rx8025_write_regs(const struct i2c_client *client,
101 u8 number, u8 length, const u8 *values)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700102{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200103 return i2c_smbus_write_i2c_block_data(client, number << 4,
104 length, values);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700105}
106
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200107static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700108{
109 struct i2c_client *client = dev_id;
110 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200111 int status;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700112
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200113 status = rx8025_read_reg(client, RX8025_REG_CTRL2);
114 if (status < 0)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700115 goto out;
116
117 if (!(status & RX8025_BIT_CTRL2_XST))
118 dev_warn(&client->dev, "Oscillation stop was detected,"
119 "you may have to readjust the clock\n");
120
121 if (status & RX8025_BIT_CTRL2_CTFG) {
122 /* periodic */
123 status &= ~RX8025_BIT_CTRL2_CTFG;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700124 rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700125 }
126
127 if (status & RX8025_BIT_CTRL2_DAFG) {
128 /* alarm */
129 status &= RX8025_BIT_CTRL2_DAFG;
130 if (rx8025_write_reg(client, RX8025_REG_CTRL1,
131 rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
132 goto out;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700133 rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700134 }
135
136 /* acknowledge IRQ */
137 rx8025_write_reg(client, RX8025_REG_CTRL2,
138 status | RX8025_BIT_CTRL2_XST);
139
140out:
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200141 return IRQ_HANDLED;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700142}
143
144static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
145{
146 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200147 u8 date[7];
148 int ctrl, err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700149
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200150 ctrl = rx8025_read_reg(rx8025->client, RX8025_REG_CTRL2);
151 if (ctrl < 0)
152 return ctrl;
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200153
154 if (ctrl & RX8025_BIT_CTRL2_PON) {
155 dev_warn(dev, "power-on reset detected, date is invalid\n");
156 return -EINVAL;
157 }
158
159 if (!(ctrl & RX8025_BIT_CTRL2_XST)) {
160 dev_warn(dev, "crystal stopped, date is invalid\n");
161 return -EINVAL;
162 }
163
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700164 err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date);
165 if (err)
166 return err;
167
168 dev_dbg(dev, "%s: read 0x%02x 0x%02x "
169 "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__,
170 date[0], date[1], date[2], date[3], date[4],
171 date[5], date[6]);
172
173 dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
174 dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
175 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
176 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
177 else
178 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
179 + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
180
181 dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
182 dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
Alexandre Belloni32672c52015-07-25 12:07:37 +0200183 dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700184
185 dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__,
186 dt->tm_sec, dt->tm_min, dt->tm_hour,
187 dt->tm_mday, dt->tm_mon, dt->tm_year);
188
189 return rtc_valid_tm(dt);
190}
191
192static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
193{
194 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
195 u8 date[7];
196
Alexandre Belloni32672c52015-07-25 12:07:37 +0200197 if ((dt->tm_year < 100) || (dt->tm_year > 199))
198 return -EINVAL;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700199
200 /*
201 * Here the read-only bits are written as "0". I'm not sure if that
202 * is sound.
203 */
204 date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
205 date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
206 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
207 date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
208 else
209 date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
210 | bin2bcd((dt->tm_hour + 11) % 12 + 1);
211
212 date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
213 date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
214 date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
Alexandre Belloni32672c52015-07-25 12:07:37 +0200215 date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700216
217 dev_dbg(dev,
218 "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
219 __func__,
220 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
221
222 return rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date);
223}
224
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200225static int rx8025_init_client(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700226{
227 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
228 u8 ctrl[2], ctrl2;
229 int need_clear = 0;
230 int err;
231
232 err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl);
233 if (err)
234 goto out;
235
236 /* Keep test bit zero ! */
237 rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
238
239 if (ctrl[1] & RX8025_BIT_CTRL2_PON) {
240 dev_warn(&client->dev, "power-on reset was detected, "
241 "you may have to readjust the clock\n");
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200242 need_clear = 1;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700243 }
244
245 if (ctrl[1] & RX8025_BIT_CTRL2_VDET) {
246 dev_warn(&client->dev, "a power voltage drop was detected, "
247 "you may have to readjust the clock\n");
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200248 need_clear = 1;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700249 }
250
251 if (!(ctrl[1] & RX8025_BIT_CTRL2_XST)) {
252 dev_warn(&client->dev, "Oscillation stop was detected,"
253 "you may have to readjust the clock\n");
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200254 need_clear = 1;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700255 }
256
257 if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
258 dev_warn(&client->dev, "Alarm was detected\n");
259 need_clear = 1;
260 }
261
262 if (!(ctrl[1] & RX8025_BIT_CTRL2_CTFG))
263 need_clear = 1;
264
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200265 if (need_clear) {
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700266 ctrl2 = ctrl[0];
267 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET |
268 RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
269 RX8025_BIT_CTRL2_DAFG);
270 ctrl2 |= RX8025_BIT_CTRL2_XST;
271
272 err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
273 }
274out:
275 return err;
276}
277
278/* Alarm support */
279static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
280{
281 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
282 struct i2c_client *client = rx8025->client;
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200283 u8 ald[2];
284 int ctrl2, err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700285
286 if (client->irq <= 0)
287 return -EINVAL;
288
289 err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
290 if (err)
291 return err;
292
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200293 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
294 if (ctrl2 < 0)
295 return ctrl2;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700296
297 dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
298 __func__, ald[0], ald[1], ctrl2);
299
300 /* Hardware alarms precision is 1 minute! */
301 t->time.tm_sec = 0;
302 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
303 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
304 t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
305 else
306 t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
307 + (ald[1] & 0x20 ? 12 : 0);
308
309 t->time.tm_wday = -1;
310 t->time.tm_mday = -1;
311 t->time.tm_mon = -1;
312 t->time.tm_year = -1;
313
314 dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n",
315 __func__,
316 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
317 t->time.tm_mday, t->time.tm_mon, t->time.tm_year);
318 t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
319 t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
320
321 return err;
322}
323
324static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
325{
326 struct i2c_client *client = to_i2c_client(dev);
327 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
328 u8 ald[2];
329 int err;
330
331 if (client->irq <= 0)
332 return -EINVAL;
333
334 /* Hardware alarm precision is 1 minute! */
335 ald[0] = bin2bcd(t->time.tm_min);
336 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
337 ald[1] = bin2bcd(t->time.tm_hour);
338 else
339 ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
340 | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
341
342 dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
343
344 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
345 rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
346 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
347 rx8025->ctrl1);
348 if (err)
349 return err;
350 }
351 err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald);
352 if (err)
353 return err;
354
355 if (t->enabled) {
356 rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
357 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
358 rx8025->ctrl1);
359 if (err)
360 return err;
361 }
362
363 return 0;
364}
365
366static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
367{
368 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
369 u8 ctrl1;
370 int err;
371
372 ctrl1 = rx8025->ctrl1;
373 if (enabled)
374 ctrl1 |= RX8025_BIT_CTRL1_DALE;
375 else
376 ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
377
378 if (ctrl1 != rx8025->ctrl1) {
379 rx8025->ctrl1 = ctrl1;
380 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
381 rx8025->ctrl1);
382 if (err)
383 return err;
384 }
385 return 0;
386}
387
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700388static struct rtc_class_ops rx8025_rtc_ops = {
389 .read_time = rx8025_get_time,
390 .set_time = rx8025_set_time,
391 .read_alarm = rx8025_read_alarm,
392 .set_alarm = rx8025_set_alarm,
393 .alarm_irq_enable = rx8025_alarm_irq_enable,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700394};
395
396/*
397 * Clock precision adjustment support
398 *
399 * According to the RX8025 SA/NB application manual the frequency and
Thomas Weberb770ffd2010-07-19 08:22:43 +0200400 * temperature characteristics can be approximated using the following
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700401 * equation:
402 *
403 * df = a * (ut - t)**2
404 *
405 * df: Frequency deviation in any temperature
406 * a : Coefficient = (-35 +-5) * 10**-9
407 * ut: Ultimate temperature in degree = +25 +-5 degree
408 * t : Any temperature in degree
409 *
410 * Note that the clock adjustment in ppb must be entered (which is
411 * the negative value of the deviation).
412 */
413static int rx8025_get_clock_adjust(struct device *dev, int *adj)
414{
415 struct i2c_client *client = to_i2c_client(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200416 int digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700417
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200418 digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF);
419 if (digoff < 0)
420 return digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700421
422 *adj = digoff >= 64 ? digoff - 128 : digoff;
423 if (*adj > 0)
424 (*adj)--;
425 *adj *= -RX8025_ADJ_RESOLUTION;
426
427 return 0;
428}
429
430static int rx8025_set_clock_adjust(struct device *dev, int adj)
431{
432 struct i2c_client *client = to_i2c_client(dev);
433 u8 digoff;
434 int err;
435
436 adj /= -RX8025_ADJ_RESOLUTION;
437 if (adj > RX8025_ADJ_DATA_MAX)
438 adj = RX8025_ADJ_DATA_MAX;
439 else if (adj < RX8025_ADJ_DATA_MIN)
440 adj = RX8025_ADJ_DATA_MIN;
441 else if (adj > 0)
442 adj++;
443 else if (adj < 0)
444 adj += 128;
445 digoff = adj;
446
447 err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
448 if (err)
449 return err;
450
451 dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff);
452
453 return 0;
454}
455
456static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
457 struct device_attribute *attr,
458 char *buf)
459{
460 int err, adj;
461
462 err = rx8025_get_clock_adjust(dev, &adj);
463 if (err)
464 return err;
465
466 return sprintf(buf, "%d\n", adj);
467}
468
469static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
470 struct device_attribute *attr,
471 const char *buf, size_t count)
472{
473 int adj, err;
474
475 if (sscanf(buf, "%i", &adj) != 1)
476 return -EINVAL;
477
478 err = rx8025_set_clock_adjust(dev, adj);
479
480 return err ? err : count;
481}
482
483static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
484 rx8025_sysfs_show_clock_adjust,
485 rx8025_sysfs_store_clock_adjust);
486
487static int rx8025_sysfs_register(struct device *dev)
488{
489 return device_create_file(dev, &dev_attr_clock_adjust_ppb);
490}
491
492static void rx8025_sysfs_unregister(struct device *dev)
493{
494 device_remove_file(dev, &dev_attr_clock_adjust_ppb);
495}
496
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800497static int rx8025_probe(struct i2c_client *client,
498 const struct i2c_device_id *id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700499{
500 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
501 struct rx8025_data *rx8025;
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200502 int err = 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700503
504 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
505 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
506 dev_err(&adapter->dev,
507 "doesn't support required functionality\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200508 return -EIO;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700509 }
510
Jingoo Hanfac42b42013-07-03 15:07:10 -0700511 rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700512 if (!rx8025) {
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200513 return -ENOMEM;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700514 }
515
516 rx8025->client = client;
517 i2c_set_clientdata(client, rx8025);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700518
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200519 err = rx8025_init_client(client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700520 if (err)
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200521 return err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700522
Jingoo Hanfac42b42013-07-03 15:07:10 -0700523 rx8025->rtc = devm_rtc_device_register(&client->dev, client->name,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700524 &rx8025_rtc_ops, THIS_MODULE);
525 if (IS_ERR(rx8025->rtc)) {
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700526 dev_err(&client->dev, "unable to register the class device\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200527 return PTR_ERR(rx8025->rtc);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700528 }
529
530 if (client->irq > 0) {
531 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
Alexandre Bellonif0b63a12015-07-24 16:07:30 +0200532 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
533 rx8025_handle_irq, 0, "rx8025",
534 client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700535 if (err) {
536 dev_err(&client->dev, "unable to request IRQ\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200537 return err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700538 }
539 }
540
541 rx8025->rtc->irq_freq = 1;
542 rx8025->rtc->max_user_freq = 1;
543
544 err = rx8025_sysfs_register(&client->dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700545 return err;
546}
547
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800548static int rx8025_remove(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700549{
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700550 rx8025_sysfs_unregister(&client->dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700551 return 0;
552}
553
554static struct i2c_driver rx8025_driver = {
555 .driver = {
556 .name = "rtc-rx8025",
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700557 },
558 .probe = rx8025_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800559 .remove = rx8025_remove,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700560 .id_table = rx8025_id,
561};
562
Axel Lin0abc9202012-03-23 15:02:31 -0700563module_i2c_driver(rx8025_driver);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700564
565MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
566MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
567MODULE_LICENSE("GPL");