blob: bd911bafb809a2ede1c46c3f2e2877ac7507fc50 [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 },
Alexandre Bellonib1f9d792015-09-26 16:25:28 +020068 { "rv8803", 1 },
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070069 { }
70};
71MODULE_DEVICE_TABLE(i2c, rx8025_id);
72
73struct rx8025_data {
74 struct i2c_client *client;
75 struct rtc_device *rtc;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070076 u8 ctrl1;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070077};
78
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020079static s32 rx8025_read_reg(const struct i2c_client *client, u8 number)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070080{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020081 return i2c_smbus_read_byte_data(client, number << 4);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070082}
83
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020084static int rx8025_read_regs(const struct i2c_client *client,
85 u8 number, u8 length, u8 *values)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070086{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020087 int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length,
88 values);
89 if (ret != length)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070090 return ret < 0 ? ret : -EIO;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070091
92 return 0;
93}
94
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020095static s32 rx8025_write_reg(const struct i2c_client *client, u8 number,
96 u8 value)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070097{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020098 return i2c_smbus_write_byte_data(client, number << 4, value);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070099}
100
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200101static s32 rx8025_write_regs(const struct i2c_client *client,
102 u8 number, u8 length, const u8 *values)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700103{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200104 return i2c_smbus_write_i2c_block_data(client, number << 4,
105 length, values);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700106}
107
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200108static int rx8025_check_validity(struct device *dev)
109{
110 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
111 int ctrl2;
112
113 ctrl2 = rx8025_read_reg(rx8025->client, RX8025_REG_CTRL2);
114 if (ctrl2 < 0)
115 return ctrl2;
116
117 if (ctrl2 & RX8025_BIT_CTRL2_VDET)
118 dev_warn(dev, "power voltage drop detected\n");
119
120 if (ctrl2 & RX8025_BIT_CTRL2_PON) {
121 dev_warn(dev, "power-on reset detected, date is invalid\n");
122 return -EINVAL;
123 }
124
125 if (!(ctrl2 & RX8025_BIT_CTRL2_XST)) {
126 dev_warn(dev, "crystal stopped, date is invalid\n");
127 return -EINVAL;
128 }
129
130 return 0;
131}
132
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200133static int rx8025_reset_validity(struct i2c_client *client)
134{
135 int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
136
137 if (ctrl2 < 0)
138 return ctrl2;
139
140 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
141
142 return rx8025_write_reg(client, RX8025_REG_CTRL2,
143 ctrl2 | RX8025_BIT_CTRL2_XST);
144}
145
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200146static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700147{
148 struct i2c_client *client = dev_id;
149 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200150 int status;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700151
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200152 status = rx8025_read_reg(client, RX8025_REG_CTRL2);
153 if (status < 0)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700154 goto out;
155
156 if (!(status & RX8025_BIT_CTRL2_XST))
157 dev_warn(&client->dev, "Oscillation stop was detected,"
158 "you may have to readjust the clock\n");
159
160 if (status & RX8025_BIT_CTRL2_CTFG) {
161 /* periodic */
162 status &= ~RX8025_BIT_CTRL2_CTFG;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700163 rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700164 }
165
166 if (status & RX8025_BIT_CTRL2_DAFG) {
167 /* alarm */
168 status &= RX8025_BIT_CTRL2_DAFG;
169 if (rx8025_write_reg(client, RX8025_REG_CTRL1,
170 rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
171 goto out;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700172 rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700173 }
174
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700175out:
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200176 return IRQ_HANDLED;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700177}
178
179static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
180{
181 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200182 u8 date[7];
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200183 int err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700184
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200185 err = rx8025_check_validity(dev);
186 if (err)
187 return err;
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200188
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700189 err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date);
190 if (err)
191 return err;
192
193 dev_dbg(dev, "%s: read 0x%02x 0x%02x "
194 "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__,
195 date[0], date[1], date[2], date[3], date[4],
196 date[5], date[6]);
197
198 dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
199 dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
200 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
201 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
202 else
203 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
204 + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
205
206 dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
207 dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
Alexandre Belloni32672c52015-07-25 12:07:37 +0200208 dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700209
210 dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__,
211 dt->tm_sec, dt->tm_min, dt->tm_hour,
212 dt->tm_mday, dt->tm_mon, dt->tm_year);
213
214 return rtc_valid_tm(dt);
215}
216
217static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
218{
219 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
220 u8 date[7];
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200221 int ret;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700222
Alexandre Belloni32672c52015-07-25 12:07:37 +0200223 if ((dt->tm_year < 100) || (dt->tm_year > 199))
224 return -EINVAL;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700225
226 /*
227 * Here the read-only bits are written as "0". I'm not sure if that
228 * is sound.
229 */
230 date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
231 date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
232 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
233 date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
234 else
235 date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
236 | bin2bcd((dt->tm_hour + 11) % 12 + 1);
237
238 date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
239 date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
240 date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
Alexandre Belloni32672c52015-07-25 12:07:37 +0200241 date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700242
243 dev_dbg(dev,
244 "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
245 __func__,
246 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
247
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200248 ret = rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date);
249 if (ret < 0)
250 return ret;
251
252 return rx8025_reset_validity(rx8025->client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700253}
254
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200255static int rx8025_init_client(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700256{
257 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
258 u8 ctrl[2], ctrl2;
259 int need_clear = 0;
260 int err;
261
262 err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl);
263 if (err)
264 goto out;
265
266 /* Keep test bit zero ! */
267 rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
268
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700269 if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
270 dev_warn(&client->dev, "Alarm was detected\n");
271 need_clear = 1;
272 }
273
Alexandre Belloni5c66e1e2015-08-04 11:24:33 +0200274 if (ctrl[1] & RX8025_BIT_CTRL2_CTFG)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700275 need_clear = 1;
276
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200277 if (need_clear) {
Alexandre Bellonia27c7bf2015-08-04 10:46:22 +0200278 ctrl2 = ctrl[1];
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200279 ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700280 RX8025_BIT_CTRL2_DAFG);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700281
282 err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
283 }
284out:
285 return err;
286}
287
288/* Alarm support */
289static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
290{
291 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
292 struct i2c_client *client = rx8025->client;
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200293 u8 ald[2];
294 int ctrl2, err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700295
296 if (client->irq <= 0)
297 return -EINVAL;
298
299 err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
300 if (err)
301 return err;
302
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200303 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
304 if (ctrl2 < 0)
305 return ctrl2;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700306
307 dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
308 __func__, ald[0], ald[1], ctrl2);
309
310 /* Hardware alarms precision is 1 minute! */
311 t->time.tm_sec = 0;
312 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
313 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
314 t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
315 else
316 t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
317 + (ald[1] & 0x20 ? 12 : 0);
318
319 t->time.tm_wday = -1;
320 t->time.tm_mday = -1;
321 t->time.tm_mon = -1;
322 t->time.tm_year = -1;
323
324 dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n",
325 __func__,
326 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
327 t->time.tm_mday, t->time.tm_mon, t->time.tm_year);
328 t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
329 t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
330
331 return err;
332}
333
334static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
335{
336 struct i2c_client *client = to_i2c_client(dev);
337 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
338 u8 ald[2];
339 int err;
340
341 if (client->irq <= 0)
342 return -EINVAL;
343
344 /* Hardware alarm precision is 1 minute! */
345 ald[0] = bin2bcd(t->time.tm_min);
346 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
347 ald[1] = bin2bcd(t->time.tm_hour);
348 else
349 ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
350 | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
351
352 dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
353
354 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
355 rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
356 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
357 rx8025->ctrl1);
358 if (err)
359 return err;
360 }
361 err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald);
362 if (err)
363 return err;
364
365 if (t->enabled) {
366 rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
367 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
368 rx8025->ctrl1);
369 if (err)
370 return err;
371 }
372
373 return 0;
374}
375
376static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
377{
378 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
379 u8 ctrl1;
380 int err;
381
382 ctrl1 = rx8025->ctrl1;
383 if (enabled)
384 ctrl1 |= RX8025_BIT_CTRL1_DALE;
385 else
386 ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
387
388 if (ctrl1 != rx8025->ctrl1) {
389 rx8025->ctrl1 = ctrl1;
390 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
391 rx8025->ctrl1);
392 if (err)
393 return err;
394 }
395 return 0;
396}
397
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700398static struct rtc_class_ops rx8025_rtc_ops = {
399 .read_time = rx8025_get_time,
400 .set_time = rx8025_set_time,
401 .read_alarm = rx8025_read_alarm,
402 .set_alarm = rx8025_set_alarm,
403 .alarm_irq_enable = rx8025_alarm_irq_enable,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700404};
405
406/*
407 * Clock precision adjustment support
408 *
409 * According to the RX8025 SA/NB application manual the frequency and
Thomas Weberb770ffd2010-07-19 08:22:43 +0200410 * temperature characteristics can be approximated using the following
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700411 * equation:
412 *
413 * df = a * (ut - t)**2
414 *
415 * df: Frequency deviation in any temperature
416 * a : Coefficient = (-35 +-5) * 10**-9
417 * ut: Ultimate temperature in degree = +25 +-5 degree
418 * t : Any temperature in degree
419 *
420 * Note that the clock adjustment in ppb must be entered (which is
421 * the negative value of the deviation).
422 */
423static int rx8025_get_clock_adjust(struct device *dev, int *adj)
424{
425 struct i2c_client *client = to_i2c_client(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200426 int digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700427
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200428 digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF);
429 if (digoff < 0)
430 return digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700431
432 *adj = digoff >= 64 ? digoff - 128 : digoff;
433 if (*adj > 0)
434 (*adj)--;
435 *adj *= -RX8025_ADJ_RESOLUTION;
436
437 return 0;
438}
439
440static int rx8025_set_clock_adjust(struct device *dev, int adj)
441{
442 struct i2c_client *client = to_i2c_client(dev);
443 u8 digoff;
444 int err;
445
446 adj /= -RX8025_ADJ_RESOLUTION;
447 if (adj > RX8025_ADJ_DATA_MAX)
448 adj = RX8025_ADJ_DATA_MAX;
449 else if (adj < RX8025_ADJ_DATA_MIN)
450 adj = RX8025_ADJ_DATA_MIN;
451 else if (adj > 0)
452 adj++;
453 else if (adj < 0)
454 adj += 128;
455 digoff = adj;
456
457 err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
458 if (err)
459 return err;
460
461 dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff);
462
463 return 0;
464}
465
466static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
467 struct device_attribute *attr,
468 char *buf)
469{
470 int err, adj;
471
472 err = rx8025_get_clock_adjust(dev, &adj);
473 if (err)
474 return err;
475
476 return sprintf(buf, "%d\n", adj);
477}
478
479static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
480 struct device_attribute *attr,
481 const char *buf, size_t count)
482{
483 int adj, err;
484
485 if (sscanf(buf, "%i", &adj) != 1)
486 return -EINVAL;
487
488 err = rx8025_set_clock_adjust(dev, adj);
489
490 return err ? err : count;
491}
492
493static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
494 rx8025_sysfs_show_clock_adjust,
495 rx8025_sysfs_store_clock_adjust);
496
497static int rx8025_sysfs_register(struct device *dev)
498{
499 return device_create_file(dev, &dev_attr_clock_adjust_ppb);
500}
501
502static void rx8025_sysfs_unregister(struct device *dev)
503{
504 device_remove_file(dev, &dev_attr_clock_adjust_ppb);
505}
506
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800507static int rx8025_probe(struct i2c_client *client,
508 const struct i2c_device_id *id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700509{
510 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
511 struct rx8025_data *rx8025;
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200512 int err = 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700513
514 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
515 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
516 dev_err(&adapter->dev,
517 "doesn't support required functionality\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200518 return -EIO;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700519 }
520
Jingoo Hanfac42b42013-07-03 15:07:10 -0700521 rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
Alexandre Bellonib1f9d792015-09-26 16:25:28 +0200522 if (!rx8025)
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200523 return -ENOMEM;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700524
525 rx8025->client = client;
526 i2c_set_clientdata(client, rx8025);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700527
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200528 err = rx8025_init_client(client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700529 if (err)
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200530 return err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700531
Jingoo Hanfac42b42013-07-03 15:07:10 -0700532 rx8025->rtc = devm_rtc_device_register(&client->dev, client->name,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700533 &rx8025_rtc_ops, THIS_MODULE);
534 if (IS_ERR(rx8025->rtc)) {
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700535 dev_err(&client->dev, "unable to register the class device\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200536 return PTR_ERR(rx8025->rtc);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700537 }
538
539 if (client->irq > 0) {
540 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
Alexandre Bellonif0b63a12015-07-24 16:07:30 +0200541 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
542 rx8025_handle_irq, 0, "rx8025",
543 client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700544 if (err) {
Alexandre Belloni8a065132015-08-04 00:45:37 +0200545 dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
546 client->irq = 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700547 }
548 }
549
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700550 rx8025->rtc->max_user_freq = 1;
551
552 err = rx8025_sysfs_register(&client->dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700553 return err;
554}
555
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800556static int rx8025_remove(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700557{
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700558 rx8025_sysfs_unregister(&client->dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700559 return 0;
560}
561
562static struct i2c_driver rx8025_driver = {
563 .driver = {
564 .name = "rtc-rx8025",
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700565 },
566 .probe = rx8025_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800567 .remove = rx8025_remove,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700568 .id_table = rx8025_id,
569};
570
Axel Lin0abc9202012-03-23 15:02:31 -0700571module_i2c_driver(rx8025_driver);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700572
573MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
574MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
575MODULE_LICENSE("GPL");