blob: b9bda10589e0ddfbb6de363dbfcf782d0972964e [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -07002/*
3 * Driver for Epson's RTC module RX-8025 SA/NB
4 *
5 * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com>
6 *
7 * Copyright (C) 2005 by Digi International Inc.
8 * All rights reserved.
9 *
10 * Modified by fengjh at rising.com.cn
Leslie Laud00cd812016-01-28 21:17:22 -080011 * <lm-sensors@lm-sensors.org>
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070012 * 2006.11
13 *
14 * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com>
15 * Converted to new style by Wolfgang Grandegger <wg@grandegger.com>
16 * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com>
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070017 */
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070018#include <linux/bcd.h>
Alexandre Belloni2ddd1862015-07-25 11:50:22 +020019#include <linux/bitops.h>
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070020#include <linux/i2c.h>
Alexandre Belloni15d3bdc2015-07-24 15:50:23 +020021#include <linux/kernel.h>
22#include <linux/module.h>
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070023#include <linux/rtc.h>
24
25/* Register definitions */
26#define RX8025_REG_SEC 0x00
27#define RX8025_REG_MIN 0x01
28#define RX8025_REG_HOUR 0x02
29#define RX8025_REG_WDAY 0x03
30#define RX8025_REG_MDAY 0x04
31#define RX8025_REG_MONTH 0x05
32#define RX8025_REG_YEAR 0x06
33#define RX8025_REG_DIGOFF 0x07
34#define RX8025_REG_ALWMIN 0x08
35#define RX8025_REG_ALWHOUR 0x09
36#define RX8025_REG_ALWWDAY 0x0a
37#define RX8025_REG_ALDMIN 0x0b
38#define RX8025_REG_ALDHOUR 0x0c
39/* 0x0d is reserved */
40#define RX8025_REG_CTRL1 0x0e
41#define RX8025_REG_CTRL2 0x0f
42
43#define RX8025_BIT_CTRL1_CT (7 << 0)
44/* 1 Hz periodic level irq */
45#define RX8025_BIT_CTRL1_CT_1HZ 4
Alexandre Belloni2ddd1862015-07-25 11:50:22 +020046#define RX8025_BIT_CTRL1_TEST BIT(3)
47#define RX8025_BIT_CTRL1_1224 BIT(5)
48#define RX8025_BIT_CTRL1_DALE BIT(6)
49#define RX8025_BIT_CTRL1_WALE BIT(7)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070050
Alexandre Belloni2ddd1862015-07-25 11:50:22 +020051#define RX8025_BIT_CTRL2_DAFG BIT(0)
52#define RX8025_BIT_CTRL2_WAFG BIT(1)
53#define RX8025_BIT_CTRL2_CTFG BIT(2)
54#define RX8025_BIT_CTRL2_PON BIT(4)
55#define RX8025_BIT_CTRL2_XST BIT(5)
56#define RX8025_BIT_CTRL2_VDET BIT(6)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070057
58/* Clock precision adjustment */
59#define RX8025_ADJ_RESOLUTION 3050 /* in ppb */
60#define RX8025_ADJ_DATA_MAX 62
61#define RX8025_ADJ_DATA_MIN -62
62
63static const struct i2c_device_id rx8025_id[] = {
64 { "rx8025", 0 },
65 { }
66};
67MODULE_DEVICE_TABLE(i2c, rx8025_id);
68
69struct rx8025_data {
70 struct i2c_client *client;
71 struct rtc_device *rtc;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070072 u8 ctrl1;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070073};
74
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020075static s32 rx8025_read_reg(const struct i2c_client *client, u8 number)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070076{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020077 return i2c_smbus_read_byte_data(client, number << 4);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070078}
79
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020080static int rx8025_read_regs(const struct i2c_client *client,
81 u8 number, u8 length, u8 *values)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070082{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020083 int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length,
84 values);
85 if (ret != length)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070086 return ret < 0 ? ret : -EIO;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070087
88 return 0;
89}
90
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020091static s32 rx8025_write_reg(const struct i2c_client *client, u8 number,
92 u8 value)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070093{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020094 return i2c_smbus_write_byte_data(client, number << 4, value);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070095}
96
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020097static s32 rx8025_write_regs(const struct i2c_client *client,
98 u8 number, u8 length, const u8 *values)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070099{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200100 return i2c_smbus_write_i2c_block_data(client, number << 4,
101 length, values);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700102}
103
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200104static int rx8025_check_validity(struct device *dev)
105{
106 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
107 int ctrl2;
108
109 ctrl2 = rx8025_read_reg(rx8025->client, RX8025_REG_CTRL2);
110 if (ctrl2 < 0)
111 return ctrl2;
112
113 if (ctrl2 & RX8025_BIT_CTRL2_VDET)
114 dev_warn(dev, "power voltage drop detected\n");
115
116 if (ctrl2 & RX8025_BIT_CTRL2_PON) {
117 dev_warn(dev, "power-on reset detected, date is invalid\n");
118 return -EINVAL;
119 }
120
121 if (!(ctrl2 & RX8025_BIT_CTRL2_XST)) {
122 dev_warn(dev, "crystal stopped, date is invalid\n");
123 return -EINVAL;
124 }
125
126 return 0;
127}
128
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200129static int rx8025_reset_validity(struct i2c_client *client)
130{
131 int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
132
133 if (ctrl2 < 0)
134 return ctrl2;
135
136 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
137
138 return rx8025_write_reg(client, RX8025_REG_CTRL2,
139 ctrl2 | RX8025_BIT_CTRL2_XST);
140}
141
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200142static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700143{
144 struct i2c_client *client = dev_id;
145 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
Akinobu Mita9dbe3852016-02-15 23:49:07 +0900146 struct mutex *lock = &rx8025->rtc->ops_lock;
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200147 int status;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700148
Akinobu Mita9dbe3852016-02-15 23:49:07 +0900149 mutex_lock(lock);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200150 status = rx8025_read_reg(client, RX8025_REG_CTRL2);
151 if (status < 0)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700152 goto out;
153
154 if (!(status & RX8025_BIT_CTRL2_XST))
155 dev_warn(&client->dev, "Oscillation stop was detected,"
156 "you may have to readjust the clock\n");
157
158 if (status & RX8025_BIT_CTRL2_CTFG) {
159 /* periodic */
160 status &= ~RX8025_BIT_CTRL2_CTFG;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700161 rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700162 }
163
164 if (status & RX8025_BIT_CTRL2_DAFG) {
165 /* alarm */
166 status &= RX8025_BIT_CTRL2_DAFG;
167 if (rx8025_write_reg(client, RX8025_REG_CTRL1,
168 rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
169 goto out;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700170 rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700171 }
172
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700173out:
Akinobu Mita9dbe3852016-02-15 23:49:07 +0900174 mutex_unlock(lock);
175
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
Andy Shevchenko1921cab2018-12-04 23:23:24 +0200193 dev_dbg(dev, "%s: read %7ph\n", __func__, date);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700194
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
Andy Shevchenko1921cab2018-12-04 23:23:24 +0200207 dev_dbg(dev, "%s: date %ptRr\n", __func__, dt);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700208
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100209 return 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700210}
211
212static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
213{
214 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
215 u8 date[7];
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200216 int ret;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700217
Alexandre Belloni32672c52015-07-25 12:07:37 +0200218 if ((dt->tm_year < 100) || (dt->tm_year > 199))
219 return -EINVAL;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700220
221 /*
222 * Here the read-only bits are written as "0". I'm not sure if that
223 * is sound.
224 */
225 date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
226 date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
227 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
228 date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
229 else
230 date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
231 | bin2bcd((dt->tm_hour + 11) % 12 + 1);
232
233 date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
234 date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
235 date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
Alexandre Belloni32672c52015-07-25 12:07:37 +0200236 date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700237
Andy Shevchenko1921cab2018-12-04 23:23:24 +0200238 dev_dbg(dev, "%s: write %7ph\n", __func__, date);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700239
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200240 ret = rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date);
241 if (ret < 0)
242 return ret;
243
244 return rx8025_reset_validity(rx8025->client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700245}
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
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700261 if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
262 dev_warn(&client->dev, "Alarm was detected\n");
263 need_clear = 1;
264 }
265
Alexandre Belloni5c66e1e2015-08-04 11:24:33 +0200266 if (ctrl[1] & RX8025_BIT_CTRL2_CTFG)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700267 need_clear = 1;
268
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200269 if (need_clear) {
Alexandre Bellonia27c7bf2015-08-04 10:46:22 +0200270 ctrl2 = ctrl[1];
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200271 ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700272 RX8025_BIT_CTRL2_DAFG);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700273
274 err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
275 }
276out:
277 return err;
278}
279
280/* Alarm support */
281static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
282{
283 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
284 struct i2c_client *client = rx8025->client;
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200285 u8 ald[2];
286 int ctrl2, err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700287
288 if (client->irq <= 0)
289 return -EINVAL;
290
291 err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
292 if (err)
293 return err;
294
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200295 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
296 if (ctrl2 < 0)
297 return ctrl2;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700298
299 dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
300 __func__, ald[0], ald[1], ctrl2);
301
302 /* Hardware alarms precision is 1 minute! */
303 t->time.tm_sec = 0;
304 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
305 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
306 t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
307 else
308 t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
309 + (ald[1] & 0x20 ? 12 : 0);
310
Andy Shevchenko862cac12019-04-10 17:05:59 +0300311 dev_dbg(dev, "%s: date: %ptRr\n", __func__, &t->time);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700312 t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
313 t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
314
315 return err;
316}
317
318static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
319{
320 struct i2c_client *client = to_i2c_client(dev);
321 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
322 u8 ald[2];
323 int err;
324
325 if (client->irq <= 0)
326 return -EINVAL;
327
Akinobu Mita302c5602016-02-15 23:49:08 +0900328 /*
329 * Hardware alarm precision is 1 minute!
330 * round up to nearest minute
331 */
332 if (t->time.tm_sec) {
333 time64_t alarm_time = rtc_tm_to_time64(&t->time);
334
335 alarm_time += 60 - t->time.tm_sec;
336 rtc_time64_to_tm(alarm_time, &t->time);
337 }
338
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700339 ald[0] = bin2bcd(t->time.tm_min);
340 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
341 ald[1] = bin2bcd(t->time.tm_hour);
342 else
343 ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
344 | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
345
346 dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
347
348 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
349 rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
350 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
351 rx8025->ctrl1);
352 if (err)
353 return err;
354 }
355 err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald);
356 if (err)
357 return err;
358
359 if (t->enabled) {
360 rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
361 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
362 rx8025->ctrl1);
363 if (err)
364 return err;
365 }
366
367 return 0;
368}
369
370static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
371{
372 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
373 u8 ctrl1;
374 int err;
375
376 ctrl1 = rx8025->ctrl1;
377 if (enabled)
378 ctrl1 |= RX8025_BIT_CTRL1_DALE;
379 else
380 ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
381
382 if (ctrl1 != rx8025->ctrl1) {
383 rx8025->ctrl1 = ctrl1;
384 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
385 rx8025->ctrl1);
386 if (err)
387 return err;
388 }
389 return 0;
390}
391
Julia Lawall34c7b3a2016-08-31 10:05:25 +0200392static const struct rtc_class_ops rx8025_rtc_ops = {
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700393 .read_time = rx8025_get_time,
394 .set_time = rx8025_set_time,
395 .read_alarm = rx8025_read_alarm,
396 .set_alarm = rx8025_set_alarm,
397 .alarm_irq_enable = rx8025_alarm_irq_enable,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700398};
399
400/*
401 * Clock precision adjustment support
402 *
403 * According to the RX8025 SA/NB application manual the frequency and
Thomas Weberb770ffd2010-07-19 08:22:43 +0200404 * temperature characteristics can be approximated using the following
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700405 * equation:
406 *
407 * df = a * (ut - t)**2
408 *
409 * df: Frequency deviation in any temperature
410 * a : Coefficient = (-35 +-5) * 10**-9
411 * ut: Ultimate temperature in degree = +25 +-5 degree
412 * t : Any temperature in degree
413 *
414 * Note that the clock adjustment in ppb must be entered (which is
415 * the negative value of the deviation).
416 */
417static int rx8025_get_clock_adjust(struct device *dev, int *adj)
418{
419 struct i2c_client *client = to_i2c_client(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200420 int digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700421
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200422 digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF);
423 if (digoff < 0)
424 return digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700425
426 *adj = digoff >= 64 ? digoff - 128 : digoff;
427 if (*adj > 0)
428 (*adj)--;
429 *adj *= -RX8025_ADJ_RESOLUTION;
430
431 return 0;
432}
433
434static int rx8025_set_clock_adjust(struct device *dev, int adj)
435{
436 struct i2c_client *client = to_i2c_client(dev);
437 u8 digoff;
438 int err;
439
440 adj /= -RX8025_ADJ_RESOLUTION;
441 if (adj > RX8025_ADJ_DATA_MAX)
442 adj = RX8025_ADJ_DATA_MAX;
443 else if (adj < RX8025_ADJ_DATA_MIN)
444 adj = RX8025_ADJ_DATA_MIN;
445 else if (adj > 0)
446 adj++;
447 else if (adj < 0)
448 adj += 128;
449 digoff = adj;
450
451 err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
452 if (err)
453 return err;
454
455 dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff);
456
457 return 0;
458}
459
460static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
461 struct device_attribute *attr,
462 char *buf)
463{
464 int err, adj;
465
466 err = rx8025_get_clock_adjust(dev, &adj);
467 if (err)
468 return err;
469
470 return sprintf(buf, "%d\n", adj);
471}
472
473static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
474 struct device_attribute *attr,
475 const char *buf, size_t count)
476{
477 int adj, err;
478
479 if (sscanf(buf, "%i", &adj) != 1)
480 return -EINVAL;
481
482 err = rx8025_set_clock_adjust(dev, adj);
483
484 return err ? err : count;
485}
486
487static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
488 rx8025_sysfs_show_clock_adjust,
489 rx8025_sysfs_store_clock_adjust);
490
491static int rx8025_sysfs_register(struct device *dev)
492{
493 return device_create_file(dev, &dev_attr_clock_adjust_ppb);
494}
495
496static void rx8025_sysfs_unregister(struct device *dev)
497{
498 device_remove_file(dev, &dev_attr_clock_adjust_ppb);
499}
500
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800501static int rx8025_probe(struct i2c_client *client,
502 const struct i2c_device_id *id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700503{
Wolfram Sang110036b2019-06-08 12:56:09 +0200504 struct i2c_adapter *adapter = client->adapter;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700505 struct rx8025_data *rx8025;
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200506 int err = 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700507
508 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
509 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
510 dev_err(&adapter->dev,
511 "doesn't support required functionality\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200512 return -EIO;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700513 }
514
Jingoo Hanfac42b42013-07-03 15:07:10 -0700515 rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
Alexandre Bellonib1f9d792015-09-26 16:25:28 +0200516 if (!rx8025)
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200517 return -ENOMEM;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700518
519 rx8025->client = client;
520 i2c_set_clientdata(client, rx8025);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700521
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200522 err = rx8025_init_client(client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700523 if (err)
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200524 return err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700525
Jingoo Hanfac42b42013-07-03 15:07:10 -0700526 rx8025->rtc = devm_rtc_device_register(&client->dev, client->name,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700527 &rx8025_rtc_ops, THIS_MODULE);
528 if (IS_ERR(rx8025->rtc)) {
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700529 dev_err(&client->dev, "unable to register the class device\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200530 return PTR_ERR(rx8025->rtc);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700531 }
532
533 if (client->irq > 0) {
534 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
Alexandre Bellonif0b63a12015-07-24 16:07:30 +0200535 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
Akinobu Mita0a966c02016-02-15 23:49:06 +0900536 rx8025_handle_irq,
537 IRQF_ONESHOT,
538 "rx8025", client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700539 if (err) {
Alexandre Belloni8a065132015-08-04 00:45:37 +0200540 dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
541 client->irq = 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700542 }
543 }
544
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700545 rx8025->rtc->max_user_freq = 1;
546
Akinobu Mitaef5f4a92016-02-15 23:49:09 +0900547 /* the rx8025 alarm only supports a minute accuracy */
548 rx8025->rtc->uie_unsupported = 1;
549
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700550 err = rx8025_sysfs_register(&client->dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700551 return err;
552}
553
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800554static int rx8025_remove(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700555{
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700556 rx8025_sysfs_unregister(&client->dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700557 return 0;
558}
559
560static struct i2c_driver rx8025_driver = {
561 .driver = {
562 .name = "rtc-rx8025",
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700563 },
564 .probe = rx8025_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800565 .remove = rx8025_remove,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700566 .id_table = rx8025_id,
567};
568
Axel Lin0abc9202012-03-23 15:02:31 -0700569module_i2c_driver(rx8025_driver);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700570
571MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
572MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
573MODULE_LICENSE("GPL");