blob: 6941e0518290230b758d104d773d2093ba694467 [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
Mathew McBridef120e2e2021-07-09 04:45:17 +000063enum rx_model {
64 model_rx_unknown,
65 model_rx_8025,
66 model_rx_8035,
67 model_last
68};
69
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070070static const struct i2c_device_id rx8025_id[] = {
Mathew McBridef120e2e2021-07-09 04:45:17 +000071 { "rx8025", model_rx_8025 },
72 { "rx8035", model_rx_8035 },
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070073 { }
74};
75MODULE_DEVICE_TABLE(i2c, rx8025_id);
76
77struct rx8025_data {
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070078 struct rtc_device *rtc;
Mathew McBridef120e2e2021-07-09 04:45:17 +000079 enum rx_model model;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070080 u8 ctrl1;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070081};
82
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020083static s32 rx8025_read_reg(const struct i2c_client *client, u8 number)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070084{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020085 return i2c_smbus_read_byte_data(client, number << 4);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070086}
87
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020088static int rx8025_read_regs(const struct i2c_client *client,
89 u8 number, u8 length, u8 *values)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070090{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020091 int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length,
92 values);
93 if (ret != length)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070094 return ret < 0 ? ret : -EIO;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070095
96 return 0;
97}
98
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020099static s32 rx8025_write_reg(const struct i2c_client *client, u8 number,
100 u8 value)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700101{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200102 return i2c_smbus_write_byte_data(client, number << 4, value);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700103}
104
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200105static s32 rx8025_write_regs(const struct i2c_client *client,
106 u8 number, u8 length, const u8 *values)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700107{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200108 return i2c_smbus_write_i2c_block_data(client, number << 4,
109 length, values);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700110}
111
Mathew McBridef120e2e2021-07-09 04:45:17 +0000112static int rx8025_is_osc_stopped(enum rx_model model, int ctrl2)
113{
114 int xstp = ctrl2 & RX8025_BIT_CTRL2_XST;
115 /* XSTP bit has different polarity on RX-8025 vs RX-8035.
116 * RX-8025: 0 == oscillator stopped
117 * RX-8035: 1 == oscillator stopped
118 */
119
120 if (model == model_rx_8025)
121 xstp = !xstp;
122
123 return xstp;
124}
125
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200126static int rx8025_check_validity(struct device *dev)
127{
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900128 struct i2c_client *client = to_i2c_client(dev);
Mathew McBridef120e2e2021-07-09 04:45:17 +0000129 struct rx8025_data *drvdata = dev_get_drvdata(dev);
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200130 int ctrl2;
Mathew McBridef120e2e2021-07-09 04:45:17 +0000131 int xstp;
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200132
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900133 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200134 if (ctrl2 < 0)
135 return ctrl2;
136
137 if (ctrl2 & RX8025_BIT_CTRL2_VDET)
138 dev_warn(dev, "power voltage drop detected\n");
139
140 if (ctrl2 & RX8025_BIT_CTRL2_PON) {
141 dev_warn(dev, "power-on reset detected, date is invalid\n");
142 return -EINVAL;
143 }
144
Mathew McBridef120e2e2021-07-09 04:45:17 +0000145 xstp = rx8025_is_osc_stopped(drvdata->model, ctrl2);
146 if (xstp) {
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200147 dev_warn(dev, "crystal stopped, date is invalid\n");
148 return -EINVAL;
149 }
150
151 return 0;
152}
153
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200154static int rx8025_reset_validity(struct i2c_client *client)
155{
Mathew McBridef120e2e2021-07-09 04:45:17 +0000156 struct rx8025_data *drvdata = i2c_get_clientdata(client);
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200157 int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
158
159 if (ctrl2 < 0)
160 return ctrl2;
161
162 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
163
Mathew McBridef120e2e2021-07-09 04:45:17 +0000164 if (drvdata->model == model_rx_8025)
165 ctrl2 |= RX8025_BIT_CTRL2_XST;
166 else
167 ctrl2 &= ~(RX8025_BIT_CTRL2_XST);
168
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200169 return rx8025_write_reg(client, RX8025_REG_CTRL2,
Mathew McBridef120e2e2021-07-09 04:45:17 +0000170 ctrl2);
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200171}
172
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200173static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700174{
175 struct i2c_client *client = dev_id;
176 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
Mathew McBridef120e2e2021-07-09 04:45:17 +0000177 int status, xstp;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700178
Alexandre Belloni31247542021-01-19 23:06:51 +0100179 rtc_lock(rx8025->rtc);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200180 status = rx8025_read_reg(client, RX8025_REG_CTRL2);
181 if (status < 0)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700182 goto out;
183
Mathew McBridef120e2e2021-07-09 04:45:17 +0000184 xstp = rx8025_is_osc_stopped(rx8025->model, status);
185 if (xstp)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700186 dev_warn(&client->dev, "Oscillation stop was detected,"
187 "you may have to readjust the clock\n");
188
189 if (status & RX8025_BIT_CTRL2_CTFG) {
190 /* periodic */
191 status &= ~RX8025_BIT_CTRL2_CTFG;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700192 rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700193 }
194
195 if (status & RX8025_BIT_CTRL2_DAFG) {
196 /* alarm */
197 status &= RX8025_BIT_CTRL2_DAFG;
198 if (rx8025_write_reg(client, RX8025_REG_CTRL1,
199 rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
200 goto out;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700201 rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700202 }
203
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700204out:
Alexandre Belloni31247542021-01-19 23:06:51 +0100205 rtc_unlock(rx8025->rtc);
Akinobu Mita9dbe3852016-02-15 23:49:07 +0900206
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200207 return IRQ_HANDLED;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700208}
209
210static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
211{
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900212 struct i2c_client *client = to_i2c_client(dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700213 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200214 u8 date[7];
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200215 int err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700216
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200217 err = rx8025_check_validity(dev);
218 if (err)
219 return err;
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200220
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900221 err = rx8025_read_regs(client, RX8025_REG_SEC, 7, date);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700222 if (err)
223 return err;
224
Andy Shevchenko1921cab2018-12-04 23:23:24 +0200225 dev_dbg(dev, "%s: read %7ph\n", __func__, date);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700226
227 dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
228 dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
229 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
230 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
231 else
232 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
233 + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
234
235 dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
236 dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
Alexandre Belloni32672c52015-07-25 12:07:37 +0200237 dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700238
Andy Shevchenko1921cab2018-12-04 23:23:24 +0200239 dev_dbg(dev, "%s: date %ptRr\n", __func__, dt);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700240
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100241 return 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700242}
243
244static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
245{
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900246 struct i2c_client *client = to_i2c_client(dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700247 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
248 u8 date[7];
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200249 int ret;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700250
Alexandre Belloni32672c52015-07-25 12:07:37 +0200251 if ((dt->tm_year < 100) || (dt->tm_year > 199))
252 return -EINVAL;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700253
254 /*
255 * Here the read-only bits are written as "0". I'm not sure if that
256 * is sound.
257 */
258 date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
259 date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
260 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
261 date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
262 else
263 date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
264 | bin2bcd((dt->tm_hour + 11) % 12 + 1);
265
266 date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
267 date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
268 date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
Alexandre Belloni32672c52015-07-25 12:07:37 +0200269 date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700270
Andy Shevchenko1921cab2018-12-04 23:23:24 +0200271 dev_dbg(dev, "%s: write %7ph\n", __func__, date);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700272
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900273 ret = rx8025_write_regs(client, RX8025_REG_SEC, 7, date);
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200274 if (ret < 0)
275 return ret;
276
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900277 return rx8025_reset_validity(client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700278}
279
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200280static int rx8025_init_client(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700281{
282 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
283 u8 ctrl[2], ctrl2;
284 int need_clear = 0;
285 int err;
286
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900287 err = rx8025_read_regs(client, RX8025_REG_CTRL1, 2, ctrl);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700288 if (err)
289 goto out;
290
291 /* Keep test bit zero ! */
292 rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
293
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700294 if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
295 dev_warn(&client->dev, "Alarm was detected\n");
296 need_clear = 1;
297 }
298
Alexandre Belloni5c66e1e2015-08-04 11:24:33 +0200299 if (ctrl[1] & RX8025_BIT_CTRL2_CTFG)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700300 need_clear = 1;
301
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200302 if (need_clear) {
Alexandre Bellonia27c7bf2015-08-04 10:46:22 +0200303 ctrl2 = ctrl[1];
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200304 ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700305 RX8025_BIT_CTRL2_DAFG);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700306
307 err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
308 }
309out:
310 return err;
311}
312
313/* Alarm support */
314static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
315{
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900316 struct i2c_client *client = to_i2c_client(dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700317 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200318 u8 ald[2];
319 int ctrl2, err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700320
321 if (client->irq <= 0)
322 return -EINVAL;
323
324 err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
325 if (err)
326 return err;
327
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200328 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
329 if (ctrl2 < 0)
330 return ctrl2;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700331
332 dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
333 __func__, ald[0], ald[1], ctrl2);
334
335 /* Hardware alarms precision is 1 minute! */
336 t->time.tm_sec = 0;
337 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
338 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
339 t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
340 else
341 t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
342 + (ald[1] & 0x20 ? 12 : 0);
343
Andy Shevchenko862cac12019-04-10 17:05:59 +0300344 dev_dbg(dev, "%s: date: %ptRr\n", __func__, &t->time);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700345 t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
346 t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
347
348 return err;
349}
350
351static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
352{
353 struct i2c_client *client = to_i2c_client(dev);
354 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
355 u8 ald[2];
356 int err;
357
358 if (client->irq <= 0)
359 return -EINVAL;
360
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700361 ald[0] = bin2bcd(t->time.tm_min);
362 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
363 ald[1] = bin2bcd(t->time.tm_hour);
364 else
365 ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
366 | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
367
368 dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
369
370 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
371 rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900372 err = rx8025_write_reg(client, RX8025_REG_CTRL1,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700373 rx8025->ctrl1);
374 if (err)
375 return err;
376 }
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900377 err = rx8025_write_regs(client, RX8025_REG_ALDMIN, 2, ald);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700378 if (err)
379 return err;
380
381 if (t->enabled) {
382 rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900383 err = rx8025_write_reg(client, RX8025_REG_CTRL1,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700384 rx8025->ctrl1);
385 if (err)
386 return err;
387 }
388
389 return 0;
390}
391
392static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
393{
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900394 struct i2c_client *client = to_i2c_client(dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700395 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
396 u8 ctrl1;
397 int err;
398
399 ctrl1 = rx8025->ctrl1;
400 if (enabled)
401 ctrl1 |= RX8025_BIT_CTRL1_DALE;
402 else
403 ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
404
405 if (ctrl1 != rx8025->ctrl1) {
406 rx8025->ctrl1 = ctrl1;
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900407 err = rx8025_write_reg(client, RX8025_REG_CTRL1,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700408 rx8025->ctrl1);
409 if (err)
410 return err;
411 }
412 return 0;
413}
414
Julia Lawall34c7b3a2016-08-31 10:05:25 +0200415static const struct rtc_class_ops rx8025_rtc_ops = {
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700416 .read_time = rx8025_get_time,
417 .set_time = rx8025_set_time,
418 .read_alarm = rx8025_read_alarm,
419 .set_alarm = rx8025_set_alarm,
420 .alarm_irq_enable = rx8025_alarm_irq_enable,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700421};
422
423/*
424 * Clock precision adjustment support
425 *
426 * According to the RX8025 SA/NB application manual the frequency and
Thomas Weberb770ffd2010-07-19 08:22:43 +0200427 * temperature characteristics can be approximated using the following
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700428 * equation:
429 *
430 * df = a * (ut - t)**2
431 *
432 * df: Frequency deviation in any temperature
433 * a : Coefficient = (-35 +-5) * 10**-9
434 * ut: Ultimate temperature in degree = +25 +-5 degree
435 * t : Any temperature in degree
436 *
437 * Note that the clock adjustment in ppb must be entered (which is
438 * the negative value of the deviation).
439 */
440static int rx8025_get_clock_adjust(struct device *dev, int *adj)
441{
442 struct i2c_client *client = to_i2c_client(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200443 int digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700444
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200445 digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF);
446 if (digoff < 0)
447 return digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700448
449 *adj = digoff >= 64 ? digoff - 128 : digoff;
450 if (*adj > 0)
451 (*adj)--;
452 *adj *= -RX8025_ADJ_RESOLUTION;
453
454 return 0;
455}
456
457static int rx8025_set_clock_adjust(struct device *dev, int adj)
458{
459 struct i2c_client *client = to_i2c_client(dev);
460 u8 digoff;
461 int err;
462
463 adj /= -RX8025_ADJ_RESOLUTION;
464 if (adj > RX8025_ADJ_DATA_MAX)
465 adj = RX8025_ADJ_DATA_MAX;
466 else if (adj < RX8025_ADJ_DATA_MIN)
467 adj = RX8025_ADJ_DATA_MIN;
468 else if (adj > 0)
469 adj++;
470 else if (adj < 0)
471 adj += 128;
472 digoff = adj;
473
474 err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
475 if (err)
476 return err;
477
478 dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff);
479
480 return 0;
481}
482
483static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
484 struct device_attribute *attr,
485 char *buf)
486{
487 int err, adj;
488
489 err = rx8025_get_clock_adjust(dev, &adj);
490 if (err)
491 return err;
492
493 return sprintf(buf, "%d\n", adj);
494}
495
496static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
497 struct device_attribute *attr,
498 const char *buf, size_t count)
499{
500 int adj, err;
501
502 if (sscanf(buf, "%i", &adj) != 1)
503 return -EINVAL;
504
505 err = rx8025_set_clock_adjust(dev, adj);
506
507 return err ? err : count;
508}
509
510static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
511 rx8025_sysfs_show_clock_adjust,
512 rx8025_sysfs_store_clock_adjust);
513
514static int rx8025_sysfs_register(struct device *dev)
515{
516 return device_create_file(dev, &dev_attr_clock_adjust_ppb);
517}
518
519static void rx8025_sysfs_unregister(struct device *dev)
520{
521 device_remove_file(dev, &dev_attr_clock_adjust_ppb);
522}
523
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800524static int rx8025_probe(struct i2c_client *client,
525 const struct i2c_device_id *id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700526{
Wolfram Sang110036b2019-06-08 12:56:09 +0200527 struct i2c_adapter *adapter = client->adapter;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700528 struct rx8025_data *rx8025;
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200529 int err = 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700530
531 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
532 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
533 dev_err(&adapter->dev,
534 "doesn't support required functionality\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200535 return -EIO;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700536 }
537
Jingoo Hanfac42b42013-07-03 15:07:10 -0700538 rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
Alexandre Bellonib1f9d792015-09-26 16:25:28 +0200539 if (!rx8025)
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200540 return -ENOMEM;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700541
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700542 i2c_set_clientdata(client, rx8025);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700543
Mathew McBridef120e2e2021-07-09 04:45:17 +0000544 if (id)
545 rx8025->model = id->driver_data;
546
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200547 err = rx8025_init_client(client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700548 if (err)
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200549 return err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700550
Alexandre Belloni5e7f6352021-11-07 23:54:52 +0100551 rx8025->rtc = devm_rtc_allocate_device(&client->dev);
552 if (IS_ERR(rx8025->rtc))
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200553 return PTR_ERR(rx8025->rtc);
Alexandre Belloni5e7f6352021-11-07 23:54:52 +0100554
555 rx8025->rtc->ops = &rx8025_rtc_ops;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700556
557 if (client->irq > 0) {
558 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
Alexandre Bellonif0b63a12015-07-24 16:07:30 +0200559 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
Akinobu Mita0a966c02016-02-15 23:49:06 +0900560 rx8025_handle_irq,
561 IRQF_ONESHOT,
562 "rx8025", client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700563 if (err) {
Alexandre Belloni8a065132015-08-04 00:45:37 +0200564 dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
565 client->irq = 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700566 }
567 }
568
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700569 rx8025->rtc->max_user_freq = 1;
570
Alexandre Belloni8670558f2021-11-07 23:54:53 +0100571 set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rx8025->rtc->features);
572 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rx8025->rtc->features);
Akinobu Mitaef5f4a92016-02-15 23:49:09 +0900573
Alexandre Belloni5e7f6352021-11-07 23:54:52 +0100574 err = devm_rtc_register_device(rx8025->rtc);
575 if (err)
576 return err;
577
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700578 err = rx8025_sysfs_register(&client->dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700579 return err;
580}
581
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800582static int rx8025_remove(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700583{
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700584 rx8025_sysfs_unregister(&client->dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700585 return 0;
586}
587
588static struct i2c_driver rx8025_driver = {
589 .driver = {
590 .name = "rtc-rx8025",
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700591 },
592 .probe = rx8025_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800593 .remove = rx8025_remove,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700594 .id_table = rx8025_id,
595};
596
Axel Lin0abc9202012-03-23 15:02:31 -0700597module_i2c_driver(rx8025_driver);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700598
599MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
600MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
601MODULE_LICENSE("GPL");