blob: fddc996cb38d99a44b25a6d0b06518aa24d580ff [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
Leslie Laud00cd812016-01-28 21:17:22 -080010 * <lm-sensors@lm-sensors.org>
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070011 * 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 Belloniefbbb4f2015-08-04 11:33:59 +0200107static int rx8025_check_validity(struct device *dev)
108{
109 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
110 int ctrl2;
111
112 ctrl2 = rx8025_read_reg(rx8025->client, RX8025_REG_CTRL2);
113 if (ctrl2 < 0)
114 return ctrl2;
115
116 if (ctrl2 & RX8025_BIT_CTRL2_VDET)
117 dev_warn(dev, "power voltage drop detected\n");
118
119 if (ctrl2 & RX8025_BIT_CTRL2_PON) {
120 dev_warn(dev, "power-on reset detected, date is invalid\n");
121 return -EINVAL;
122 }
123
124 if (!(ctrl2 & RX8025_BIT_CTRL2_XST)) {
125 dev_warn(dev, "crystal stopped, date is invalid\n");
126 return -EINVAL;
127 }
128
129 return 0;
130}
131
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200132static int rx8025_reset_validity(struct i2c_client *client)
133{
134 int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
135
136 if (ctrl2 < 0)
137 return ctrl2;
138
139 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
140
141 return rx8025_write_reg(client, RX8025_REG_CTRL2,
142 ctrl2 | RX8025_BIT_CTRL2_XST);
143}
144
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200145static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700146{
147 struct i2c_client *client = dev_id;
148 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
Akinobu Mita9dbe3852016-02-15 23:49:07 +0900149 struct mutex *lock = &rx8025->rtc->ops_lock;
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200150 int status;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700151
Akinobu Mita9dbe3852016-02-15 23:49:07 +0900152 mutex_lock(lock);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200153 status = rx8025_read_reg(client, RX8025_REG_CTRL2);
154 if (status < 0)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700155 goto out;
156
157 if (!(status & RX8025_BIT_CTRL2_XST))
158 dev_warn(&client->dev, "Oscillation stop was detected,"
159 "you may have to readjust the clock\n");
160
161 if (status & RX8025_BIT_CTRL2_CTFG) {
162 /* periodic */
163 status &= ~RX8025_BIT_CTRL2_CTFG;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700164 rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700165 }
166
167 if (status & RX8025_BIT_CTRL2_DAFG) {
168 /* alarm */
169 status &= RX8025_BIT_CTRL2_DAFG;
170 if (rx8025_write_reg(client, RX8025_REG_CTRL1,
171 rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
172 goto out;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700173 rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700174 }
175
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700176out:
Akinobu Mita9dbe3852016-02-15 23:49:07 +0900177 mutex_unlock(lock);
178
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200179 return IRQ_HANDLED;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700180}
181
182static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
183{
184 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200185 u8 date[7];
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200186 int err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700187
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200188 err = rx8025_check_validity(dev);
189 if (err)
190 return err;
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200191
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700192 err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date);
193 if (err)
194 return err;
195
Andy Shevchenko1921cab2018-12-04 23:23:24 +0200196 dev_dbg(dev, "%s: read %7ph\n", __func__, date);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700197
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
Andy Shevchenko1921cab2018-12-04 23:23:24 +0200210 dev_dbg(dev, "%s: date %ptRr\n", __func__, dt);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700211
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100212 return 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700213}
214
215static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
216{
217 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
218 u8 date[7];
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200219 int ret;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700220
Alexandre Belloni32672c52015-07-25 12:07:37 +0200221 if ((dt->tm_year < 100) || (dt->tm_year > 199))
222 return -EINVAL;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700223
224 /*
225 * Here the read-only bits are written as "0". I'm not sure if that
226 * is sound.
227 */
228 date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
229 date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
230 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
231 date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
232 else
233 date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
234 | bin2bcd((dt->tm_hour + 11) % 12 + 1);
235
236 date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
237 date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
238 date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
Alexandre Belloni32672c52015-07-25 12:07:37 +0200239 date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700240
Andy Shevchenko1921cab2018-12-04 23:23:24 +0200241 dev_dbg(dev, "%s: write %7ph\n", __func__, date);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700242
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200243 ret = rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date);
244 if (ret < 0)
245 return ret;
246
247 return rx8025_reset_validity(rx8025->client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700248}
249
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200250static int rx8025_init_client(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700251{
252 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
253 u8 ctrl[2], ctrl2;
254 int need_clear = 0;
255 int err;
256
257 err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl);
258 if (err)
259 goto out;
260
261 /* Keep test bit zero ! */
262 rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
263
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700264 if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
265 dev_warn(&client->dev, "Alarm was detected\n");
266 need_clear = 1;
267 }
268
Alexandre Belloni5c66e1e2015-08-04 11:24:33 +0200269 if (ctrl[1] & RX8025_BIT_CTRL2_CTFG)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700270 need_clear = 1;
271
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200272 if (need_clear) {
Alexandre Bellonia27c7bf2015-08-04 10:46:22 +0200273 ctrl2 = ctrl[1];
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200274 ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700275 RX8025_BIT_CTRL2_DAFG);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700276
277 err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
278 }
279out:
280 return err;
281}
282
283/* Alarm support */
284static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
285{
286 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
287 struct i2c_client *client = rx8025->client;
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200288 u8 ald[2];
289 int ctrl2, err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700290
291 if (client->irq <= 0)
292 return -EINVAL;
293
294 err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
295 if (err)
296 return err;
297
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200298 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
299 if (ctrl2 < 0)
300 return ctrl2;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700301
302 dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
303 __func__, ald[0], ald[1], ctrl2);
304
305 /* Hardware alarms precision is 1 minute! */
306 t->time.tm_sec = 0;
307 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
308 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
309 t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
310 else
311 t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
312 + (ald[1] & 0x20 ? 12 : 0);
313
Andy Shevchenko862cac12019-04-10 17:05:59 +0300314 dev_dbg(dev, "%s: date: %ptRr\n", __func__, &t->time);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700315 t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
316 t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
317
318 return err;
319}
320
321static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
322{
323 struct i2c_client *client = to_i2c_client(dev);
324 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
325 u8 ald[2];
326 int err;
327
328 if (client->irq <= 0)
329 return -EINVAL;
330
Akinobu Mita302c5602016-02-15 23:49:08 +0900331 /*
332 * Hardware alarm precision is 1 minute!
333 * round up to nearest minute
334 */
335 if (t->time.tm_sec) {
336 time64_t alarm_time = rtc_tm_to_time64(&t->time);
337
338 alarm_time += 60 - t->time.tm_sec;
339 rtc_time64_to_tm(alarm_time, &t->time);
340 }
341
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700342 ald[0] = bin2bcd(t->time.tm_min);
343 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
344 ald[1] = bin2bcd(t->time.tm_hour);
345 else
346 ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
347 | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
348
349 dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
350
351 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
352 rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
353 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
354 rx8025->ctrl1);
355 if (err)
356 return err;
357 }
358 err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald);
359 if (err)
360 return err;
361
362 if (t->enabled) {
363 rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
364 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
365 rx8025->ctrl1);
366 if (err)
367 return err;
368 }
369
370 return 0;
371}
372
373static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
374{
375 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
376 u8 ctrl1;
377 int err;
378
379 ctrl1 = rx8025->ctrl1;
380 if (enabled)
381 ctrl1 |= RX8025_BIT_CTRL1_DALE;
382 else
383 ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
384
385 if (ctrl1 != rx8025->ctrl1) {
386 rx8025->ctrl1 = ctrl1;
387 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
388 rx8025->ctrl1);
389 if (err)
390 return err;
391 }
392 return 0;
393}
394
Julia Lawall34c7b3a2016-08-31 10:05:25 +0200395static const struct rtc_class_ops rx8025_rtc_ops = {
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700396 .read_time = rx8025_get_time,
397 .set_time = rx8025_set_time,
398 .read_alarm = rx8025_read_alarm,
399 .set_alarm = rx8025_set_alarm,
400 .alarm_irq_enable = rx8025_alarm_irq_enable,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700401};
402
403/*
404 * Clock precision adjustment support
405 *
406 * According to the RX8025 SA/NB application manual the frequency and
Thomas Weberb770ffd2010-07-19 08:22:43 +0200407 * temperature characteristics can be approximated using the following
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700408 * equation:
409 *
410 * df = a * (ut - t)**2
411 *
412 * df: Frequency deviation in any temperature
413 * a : Coefficient = (-35 +-5) * 10**-9
414 * ut: Ultimate temperature in degree = +25 +-5 degree
415 * t : Any temperature in degree
416 *
417 * Note that the clock adjustment in ppb must be entered (which is
418 * the negative value of the deviation).
419 */
420static int rx8025_get_clock_adjust(struct device *dev, int *adj)
421{
422 struct i2c_client *client = to_i2c_client(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200423 int digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700424
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200425 digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF);
426 if (digoff < 0)
427 return digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700428
429 *adj = digoff >= 64 ? digoff - 128 : digoff;
430 if (*adj > 0)
431 (*adj)--;
432 *adj *= -RX8025_ADJ_RESOLUTION;
433
434 return 0;
435}
436
437static int rx8025_set_clock_adjust(struct device *dev, int adj)
438{
439 struct i2c_client *client = to_i2c_client(dev);
440 u8 digoff;
441 int err;
442
443 adj /= -RX8025_ADJ_RESOLUTION;
444 if (adj > RX8025_ADJ_DATA_MAX)
445 adj = RX8025_ADJ_DATA_MAX;
446 else if (adj < RX8025_ADJ_DATA_MIN)
447 adj = RX8025_ADJ_DATA_MIN;
448 else if (adj > 0)
449 adj++;
450 else if (adj < 0)
451 adj += 128;
452 digoff = adj;
453
454 err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
455 if (err)
456 return err;
457
458 dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff);
459
460 return 0;
461}
462
463static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
464 struct device_attribute *attr,
465 char *buf)
466{
467 int err, adj;
468
469 err = rx8025_get_clock_adjust(dev, &adj);
470 if (err)
471 return err;
472
473 return sprintf(buf, "%d\n", adj);
474}
475
476static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
479{
480 int adj, err;
481
482 if (sscanf(buf, "%i", &adj) != 1)
483 return -EINVAL;
484
485 err = rx8025_set_clock_adjust(dev, adj);
486
487 return err ? err : count;
488}
489
490static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
491 rx8025_sysfs_show_clock_adjust,
492 rx8025_sysfs_store_clock_adjust);
493
494static int rx8025_sysfs_register(struct device *dev)
495{
496 return device_create_file(dev, &dev_attr_clock_adjust_ppb);
497}
498
499static void rx8025_sysfs_unregister(struct device *dev)
500{
501 device_remove_file(dev, &dev_attr_clock_adjust_ppb);
502}
503
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800504static int rx8025_probe(struct i2c_client *client,
505 const struct i2c_device_id *id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700506{
507 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
508 struct rx8025_data *rx8025;
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200509 int err = 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700510
511 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
512 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
513 dev_err(&adapter->dev,
514 "doesn't support required functionality\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200515 return -EIO;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700516 }
517
Jingoo Hanfac42b42013-07-03 15:07:10 -0700518 rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
Alexandre Bellonib1f9d792015-09-26 16:25:28 +0200519 if (!rx8025)
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200520 return -ENOMEM;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700521
522 rx8025->client = client;
523 i2c_set_clientdata(client, rx8025);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700524
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200525 err = rx8025_init_client(client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700526 if (err)
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200527 return err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700528
Jingoo Hanfac42b42013-07-03 15:07:10 -0700529 rx8025->rtc = devm_rtc_device_register(&client->dev, client->name,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700530 &rx8025_rtc_ops, THIS_MODULE);
531 if (IS_ERR(rx8025->rtc)) {
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700532 dev_err(&client->dev, "unable to register the class device\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200533 return PTR_ERR(rx8025->rtc);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700534 }
535
536 if (client->irq > 0) {
537 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
Alexandre Bellonif0b63a12015-07-24 16:07:30 +0200538 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
Akinobu Mita0a966c02016-02-15 23:49:06 +0900539 rx8025_handle_irq,
540 IRQF_ONESHOT,
541 "rx8025", client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700542 if (err) {
Alexandre Belloni8a065132015-08-04 00:45:37 +0200543 dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
544 client->irq = 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700545 }
546 }
547
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700548 rx8025->rtc->max_user_freq = 1;
549
Akinobu Mitaef5f4a92016-02-15 23:49:09 +0900550 /* the rx8025 alarm only supports a minute accuracy */
551 rx8025->rtc->uie_unsupported = 1;
552
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700553 err = rx8025_sysfs_register(&client->dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700554 return err;
555}
556
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800557static int rx8025_remove(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700558{
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700559 rx8025_sysfs_unregister(&client->dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700560 return 0;
561}
562
563static struct i2c_driver rx8025_driver = {
564 .driver = {
565 .name = "rtc-rx8025",
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700566 },
567 .probe = rx8025_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800568 .remove = rx8025_remove,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700569 .id_table = rx8025_id,
570};
571
Axel Lin0abc9202012-03-23 15:02:31 -0700572module_i2c_driver(rx8025_driver);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700573
574MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
575MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
576MODULE_LICENSE("GPL");