blob: aabe62c283a150f18810545a05827f54b8ed468f [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>
Christophe JAILLETe59b3c72022-11-06 09:00:51 +010022#include <linux/kstrtox.h>
Alexandre Belloni15d3bdc2015-07-24 15:50:23 +020023#include <linux/module.h>
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070024#include <linux/rtc.h>
25
26/* Register definitions */
27#define RX8025_REG_SEC 0x00
28#define RX8025_REG_MIN 0x01
29#define RX8025_REG_HOUR 0x02
30#define RX8025_REG_WDAY 0x03
31#define RX8025_REG_MDAY 0x04
32#define RX8025_REG_MONTH 0x05
33#define RX8025_REG_YEAR 0x06
34#define RX8025_REG_DIGOFF 0x07
35#define RX8025_REG_ALWMIN 0x08
36#define RX8025_REG_ALWHOUR 0x09
37#define RX8025_REG_ALWWDAY 0x0a
38#define RX8025_REG_ALDMIN 0x0b
39#define RX8025_REG_ALDHOUR 0x0c
40/* 0x0d is reserved */
41#define RX8025_REG_CTRL1 0x0e
42#define RX8025_REG_CTRL2 0x0f
43
44#define RX8025_BIT_CTRL1_CT (7 << 0)
45/* 1 Hz periodic level irq */
46#define RX8025_BIT_CTRL1_CT_1HZ 4
Alexandre Belloni2ddd1862015-07-25 11:50:22 +020047#define RX8025_BIT_CTRL1_TEST BIT(3)
48#define RX8025_BIT_CTRL1_1224 BIT(5)
49#define RX8025_BIT_CTRL1_DALE BIT(6)
50#define RX8025_BIT_CTRL1_WALE BIT(7)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070051
Alexandre Belloni2ddd1862015-07-25 11:50:22 +020052#define RX8025_BIT_CTRL2_DAFG BIT(0)
53#define RX8025_BIT_CTRL2_WAFG BIT(1)
54#define RX8025_BIT_CTRL2_CTFG BIT(2)
55#define RX8025_BIT_CTRL2_PON BIT(4)
56#define RX8025_BIT_CTRL2_XST BIT(5)
57#define RX8025_BIT_CTRL2_VDET BIT(6)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070058
Mathew McBride71af9152022-07-06 07:42:36 +000059#define RX8035_BIT_HOUR_1224 BIT(7)
60
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070061/* 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
Mathew McBridef120e2e2021-07-09 04:45:17 +000066enum rx_model {
67 model_rx_unknown,
68 model_rx_8025,
69 model_rx_8035,
70 model_last
71};
72
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070073static const struct i2c_device_id rx8025_id[] = {
Mathew McBridef120e2e2021-07-09 04:45:17 +000074 { "rx8025", model_rx_8025 },
75 { "rx8035", model_rx_8035 },
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070076 { }
77};
78MODULE_DEVICE_TABLE(i2c, rx8025_id);
79
80struct rx8025_data {
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070081 struct rtc_device *rtc;
Mathew McBridef120e2e2021-07-09 04:45:17 +000082 enum rx_model model;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070083 u8 ctrl1;
Mathew McBride71af9152022-07-06 07:42:36 +000084 int is_24;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070085};
86
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020087static s32 rx8025_read_reg(const struct i2c_client *client, u8 number)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070088{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020089 return i2c_smbus_read_byte_data(client, number << 4);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070090}
91
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020092static int rx8025_read_regs(const struct i2c_client *client,
93 u8 number, u8 length, u8 *values)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070094{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +020095 int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length,
96 values);
97 if (ret != length)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070098 return ret < 0 ? ret : -EIO;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -070099
100 return 0;
101}
102
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200103static s32 rx8025_write_reg(const struct i2c_client *client, u8 number,
104 u8 value)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700105{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200106 return i2c_smbus_write_byte_data(client, number << 4, value);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700107}
108
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200109static s32 rx8025_write_regs(const struct i2c_client *client,
110 u8 number, u8 length, const u8 *values)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700111{
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200112 return i2c_smbus_write_i2c_block_data(client, number << 4,
113 length, values);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700114}
115
Mathew McBridef120e2e2021-07-09 04:45:17 +0000116static int rx8025_is_osc_stopped(enum rx_model model, int ctrl2)
117{
118 int xstp = ctrl2 & RX8025_BIT_CTRL2_XST;
119 /* XSTP bit has different polarity on RX-8025 vs RX-8035.
120 * RX-8025: 0 == oscillator stopped
121 * RX-8035: 1 == oscillator stopped
122 */
123
124 if (model == model_rx_8025)
125 xstp = !xstp;
126
127 return xstp;
128}
129
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200130static int rx8025_check_validity(struct device *dev)
131{
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900132 struct i2c_client *client = to_i2c_client(dev);
Mathew McBridef120e2e2021-07-09 04:45:17 +0000133 struct rx8025_data *drvdata = dev_get_drvdata(dev);
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200134 int ctrl2;
Mathew McBridef120e2e2021-07-09 04:45:17 +0000135 int xstp;
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200136
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900137 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200138 if (ctrl2 < 0)
139 return ctrl2;
140
141 if (ctrl2 & RX8025_BIT_CTRL2_VDET)
142 dev_warn(dev, "power voltage drop detected\n");
143
144 if (ctrl2 & RX8025_BIT_CTRL2_PON) {
145 dev_warn(dev, "power-on reset detected, date is invalid\n");
146 return -EINVAL;
147 }
148
Mathew McBridef120e2e2021-07-09 04:45:17 +0000149 xstp = rx8025_is_osc_stopped(drvdata->model, ctrl2);
150 if (xstp) {
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200151 dev_warn(dev, "crystal stopped, date is invalid\n");
152 return -EINVAL;
153 }
154
155 return 0;
156}
157
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200158static int rx8025_reset_validity(struct i2c_client *client)
159{
Mathew McBridef120e2e2021-07-09 04:45:17 +0000160 struct rx8025_data *drvdata = i2c_get_clientdata(client);
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200161 int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
162
163 if (ctrl2 < 0)
164 return ctrl2;
165
166 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
167
Mathew McBridef120e2e2021-07-09 04:45:17 +0000168 if (drvdata->model == model_rx_8025)
169 ctrl2 |= RX8025_BIT_CTRL2_XST;
170 else
171 ctrl2 &= ~(RX8025_BIT_CTRL2_XST);
172
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200173 return rx8025_write_reg(client, RX8025_REG_CTRL2,
Mathew McBridef120e2e2021-07-09 04:45:17 +0000174 ctrl2);
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200175}
176
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200177static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700178{
179 struct i2c_client *client = dev_id;
180 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
Mathew McBridef120e2e2021-07-09 04:45:17 +0000181 int status, xstp;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700182
Alexandre Belloni31247542021-01-19 23:06:51 +0100183 rtc_lock(rx8025->rtc);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200184 status = rx8025_read_reg(client, RX8025_REG_CTRL2);
185 if (status < 0)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700186 goto out;
187
Mathew McBridef120e2e2021-07-09 04:45:17 +0000188 xstp = rx8025_is_osc_stopped(rx8025->model, status);
189 if (xstp)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700190 dev_warn(&client->dev, "Oscillation stop was detected,"
191 "you may have to readjust the clock\n");
192
193 if (status & RX8025_BIT_CTRL2_CTFG) {
194 /* periodic */
195 status &= ~RX8025_BIT_CTRL2_CTFG;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700196 rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700197 }
198
199 if (status & RX8025_BIT_CTRL2_DAFG) {
200 /* alarm */
201 status &= RX8025_BIT_CTRL2_DAFG;
202 if (rx8025_write_reg(client, RX8025_REG_CTRL1,
203 rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
204 goto out;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700205 rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700206 }
207
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700208out:
Alexandre Belloni31247542021-01-19 23:06:51 +0100209 rtc_unlock(rx8025->rtc);
Akinobu Mita9dbe3852016-02-15 23:49:07 +0900210
Alexandre Bellonib6a57c92015-07-24 15:59:43 +0200211 return IRQ_HANDLED;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700212}
213
214static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
215{
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900216 struct i2c_client *client = to_i2c_client(dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700217 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200218 u8 date[7];
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200219 int err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700220
Alexandre Belloniefbbb4f2015-08-04 11:33:59 +0200221 err = rx8025_check_validity(dev);
222 if (err)
223 return err;
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200224
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900225 err = rx8025_read_regs(client, RX8025_REG_SEC, 7, date);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700226 if (err)
227 return err;
228
Andy Shevchenko1921cab2018-12-04 23:23:24 +0200229 dev_dbg(dev, "%s: read %7ph\n", __func__, date);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700230
231 dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
232 dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
Mathew McBride71af9152022-07-06 07:42:36 +0000233 if (rx8025->is_24)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700234 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
235 else
236 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
237 + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
238
239 dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
240 dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
Alexandre Belloni32672c52015-07-25 12:07:37 +0200241 dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700242
Andy Shevchenko1921cab2018-12-04 23:23:24 +0200243 dev_dbg(dev, "%s: date %ptRr\n", __func__, dt);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700244
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100245 return 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700246}
247
248static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
249{
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900250 struct i2c_client *client = to_i2c_client(dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700251 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
252 u8 date[7];
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200253 int ret;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700254
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700255 /*
256 * Here the read-only bits are written as "0". I'm not sure if that
257 * is sound.
258 */
259 date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
260 date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
Mathew McBride71af9152022-07-06 07:42:36 +0000261 if (rx8025->is_24)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700262 date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
263 else
264 date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
265 | bin2bcd((dt->tm_hour + 11) % 12 + 1);
266
267 date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
268 date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
269 date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
Alexandre Belloni32672c52015-07-25 12:07:37 +0200270 date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700271
Andy Shevchenko1921cab2018-12-04 23:23:24 +0200272 dev_dbg(dev, "%s: write %7ph\n", __func__, date);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700273
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900274 ret = rx8025_write_regs(client, RX8025_REG_SEC, 7, date);
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200275 if (ret < 0)
276 return ret;
277
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900278 return rx8025_reset_validity(client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700279}
280
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200281static int rx8025_init_client(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700282{
283 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
284 u8 ctrl[2], ctrl2;
285 int need_clear = 0;
Mathew McBride71af9152022-07-06 07:42:36 +0000286 int hour_reg;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700287 int err;
288
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900289 err = rx8025_read_regs(client, RX8025_REG_CTRL1, 2, ctrl);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700290 if (err)
291 goto out;
292
293 /* Keep test bit zero ! */
294 rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
295
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700296 if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
297 dev_warn(&client->dev, "Alarm was detected\n");
298 need_clear = 1;
299 }
300
Alexandre Belloni5c66e1e2015-08-04 11:24:33 +0200301 if (ctrl[1] & RX8025_BIT_CTRL2_CTFG)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700302 need_clear = 1;
303
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200304 if (need_clear) {
Alexandre Bellonia27c7bf2015-08-04 10:46:22 +0200305 ctrl2 = ctrl[1];
Alexandre Belloni8c4a4462015-08-04 10:48:20 +0200306 ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700307 RX8025_BIT_CTRL2_DAFG);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700308
309 err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
310 }
Mathew McBride71af9152022-07-06 07:42:36 +0000311
312 if (rx8025->model == model_rx_8035) {
313 /* In RX-8035, 12/24 flag is in the hour register */
314 hour_reg = rx8025_read_reg(client, RX8025_REG_HOUR);
315 if (hour_reg < 0)
316 return hour_reg;
317 rx8025->is_24 = (hour_reg & RX8035_BIT_HOUR_1224);
318 } else {
319 rx8025->is_24 = (ctrl[1] & RX8025_BIT_CTRL1_1224);
320 }
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700321out:
322 return err;
323}
324
325/* Alarm support */
326static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
327{
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900328 struct i2c_client *client = to_i2c_client(dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700329 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200330 u8 ald[2];
331 int ctrl2, err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700332
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700333 err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
334 if (err)
335 return err;
336
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200337 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
338 if (ctrl2 < 0)
339 return ctrl2;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700340
341 dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
342 __func__, ald[0], ald[1], ctrl2);
343
344 /* Hardware alarms precision is 1 minute! */
345 t->time.tm_sec = 0;
346 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
Mathew McBride71af9152022-07-06 07:42:36 +0000347 if (rx8025->is_24)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700348 t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
349 else
350 t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
351 + (ald[1] & 0x20 ? 12 : 0);
352
Andy Shevchenko862cac12019-04-10 17:05:59 +0300353 dev_dbg(dev, "%s: date: %ptRr\n", __func__, &t->time);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700354 t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
355 t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
356
357 return err;
358}
359
360static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
361{
362 struct i2c_client *client = to_i2c_client(dev);
363 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
364 u8 ald[2];
365 int err;
366
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700367 ald[0] = bin2bcd(t->time.tm_min);
Mathew McBride71af9152022-07-06 07:42:36 +0000368 if (rx8025->is_24)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700369 ald[1] = bin2bcd(t->time.tm_hour);
370 else
371 ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
372 | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
373
374 dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
375
376 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
377 rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900378 err = rx8025_write_reg(client, RX8025_REG_CTRL1,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700379 rx8025->ctrl1);
380 if (err)
381 return err;
382 }
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900383 err = rx8025_write_regs(client, RX8025_REG_ALDMIN, 2, ald);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700384 if (err)
385 return err;
386
387 if (t->enabled) {
388 rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900389 err = rx8025_write_reg(client, RX8025_REG_CTRL1,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700390 rx8025->ctrl1);
391 if (err)
392 return err;
393 }
394
395 return 0;
396}
397
398static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
399{
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900400 struct i2c_client *client = to_i2c_client(dev);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700401 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
402 u8 ctrl1;
403 int err;
404
405 ctrl1 = rx8025->ctrl1;
406 if (enabled)
407 ctrl1 |= RX8025_BIT_CTRL1_DALE;
408 else
409 ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
410
411 if (ctrl1 != rx8025->ctrl1) {
412 rx8025->ctrl1 = ctrl1;
Nobuhiro Iwamatsu47a3c042019-12-18 17:16:24 +0900413 err = rx8025_write_reg(client, RX8025_REG_CTRL1,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700414 rx8025->ctrl1);
415 if (err)
416 return err;
417 }
418 return 0;
419}
420
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700421/*
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700422 * According to the RX8025 SA/NB application manual the frequency and
Thomas Weberb770ffd2010-07-19 08:22:43 +0200423 * temperature characteristics can be approximated using the following
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700424 * equation:
425 *
426 * df = a * (ut - t)**2
427 *
428 * df: Frequency deviation in any temperature
429 * a : Coefficient = (-35 +-5) * 10**-9
430 * ut: Ultimate temperature in degree = +25 +-5 degree
431 * t : Any temperature in degree
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700432 */
Alexandre Bellonib4762662021-11-07 23:54:57 +0100433static int rx8025_read_offset(struct device *dev, long *offset)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700434{
435 struct i2c_client *client = to_i2c_client(dev);
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200436 int digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700437
Alexandre Bellonifd9061f2015-08-04 00:40:25 +0200438 digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF);
439 if (digoff < 0)
440 return digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700441
Alexandre Bellonib4762662021-11-07 23:54:57 +0100442 *offset = digoff >= 64 ? digoff - 128 : digoff;
443 if (*offset > 0)
444 (*offset)--;
445 *offset *= RX8025_ADJ_RESOLUTION;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700446
447 return 0;
448}
449
Alexandre Bellonib4762662021-11-07 23:54:57 +0100450static int rx8025_set_offset(struct device *dev, long offset)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700451{
452 struct i2c_client *client = to_i2c_client(dev);
453 u8 digoff;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700454
Alexandre Bellonib4762662021-11-07 23:54:57 +0100455 offset /= RX8025_ADJ_RESOLUTION;
456 if (offset > RX8025_ADJ_DATA_MAX)
457 offset = RX8025_ADJ_DATA_MAX;
458 else if (offset < RX8025_ADJ_DATA_MIN)
459 offset = RX8025_ADJ_DATA_MIN;
460 else if (offset > 0)
461 offset++;
462 else if (offset < 0)
463 offset += 128;
464 digoff = offset;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700465
Minghao Chibce7a012022-05-05 02:23:14 +0000466 return rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700467}
468
Alexandre Bellonib4762662021-11-07 23:54:57 +0100469static const struct rtc_class_ops rx8025_rtc_ops = {
470 .read_time = rx8025_get_time,
471 .set_time = rx8025_set_time,
472 .read_alarm = rx8025_read_alarm,
473 .set_alarm = rx8025_set_alarm,
474 .alarm_irq_enable = rx8025_alarm_irq_enable,
475 .read_offset = rx8025_read_offset,
476 .set_offset = rx8025_set_offset,
477};
478
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700479static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
480 struct device_attribute *attr,
481 char *buf)
482{
Alexandre Bellonib4762662021-11-07 23:54:57 +0100483 long adj;
484 int err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700485
Alexandre Bellonib4762662021-11-07 23:54:57 +0100486 dev_warn_once(dev, "clock_adjust_ppb is deprecated, use offset\n");
487 err = rx8025_read_offset(dev, &adj);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700488 if (err)
489 return err;
490
Alexandre Bellonib4762662021-11-07 23:54:57 +0100491 return sprintf(buf, "%ld\n", -adj);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700492}
493
494static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
495 struct device_attribute *attr,
496 const char *buf, size_t count)
497{
Alexandre Bellonib4762662021-11-07 23:54:57 +0100498 long adj;
499 int err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700500
Alexandre Bellonib4762662021-11-07 23:54:57 +0100501 dev_warn_once(dev, "clock_adjust_ppb is deprecated, use offset\n");
502 if (kstrtol(buf, 10, &adj) != 0)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700503 return -EINVAL;
504
Alexandre Bellonib4762662021-11-07 23:54:57 +0100505 err = rx8025_set_offset(dev, -adj);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700506
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
Alexandre Belloni3d358402021-11-07 23:54:56 +0100514static struct attribute *rx8025_attrs[] = {
515 &dev_attr_clock_adjust_ppb.attr,
516 NULL
517};
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700518
Alexandre Belloni3d358402021-11-07 23:54:56 +0100519static const struct attribute_group rx8025_attr_group = {
520 .attrs = rx8025_attrs,
521};
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700522
Uwe Kleine-König8ffb7732022-10-21 15:07:06 +0200523static int rx8025_probe(struct i2c_client *client)
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700524{
Uwe Kleine-König8ffb7732022-10-21 15:07:06 +0200525 const struct i2c_device_id *id = i2c_match_id(rx8025_id, client);
Wolfram Sang110036b2019-06-08 12:56:09 +0200526 struct i2c_adapter *adapter = client->adapter;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700527 struct rx8025_data *rx8025;
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200528 int err = 0;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700529
530 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
531 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
532 dev_err(&adapter->dev,
533 "doesn't support required functionality\n");
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200534 return -EIO;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700535 }
536
Jingoo Hanfac42b42013-07-03 15:07:10 -0700537 rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
Alexandre Bellonib1f9d792015-09-26 16:25:28 +0200538 if (!rx8025)
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200539 return -ENOMEM;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700540
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700541 i2c_set_clientdata(client, rx8025);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700542
Mathew McBridef120e2e2021-07-09 04:45:17 +0000543 if (id)
544 rx8025->model = id->driver_data;
545
Alexandre Belloni6f0a8cf2015-07-26 10:13:31 +0200546 err = rx8025_init_client(client);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700547 if (err)
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200548 return err;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700549
Alexandre Belloni5e7f6352021-11-07 23:54:52 +0100550 rx8025->rtc = devm_rtc_allocate_device(&client->dev);
551 if (IS_ERR(rx8025->rtc))
Alexandre Bellonidbcce7c2015-07-24 16:12:10 +0200552 return PTR_ERR(rx8025->rtc);
Alexandre Belloni5e7f6352021-11-07 23:54:52 +0100553
554 rx8025->rtc->ops = &rx8025_rtc_ops;
Alexandre Belloni1709d7e2021-11-07 23:54:54 +0100555 rx8025->rtc->range_min = RTC_TIMESTAMP_BEGIN_1900;
556 rx8025->rtc->range_max = RTC_TIMESTAMP_END_2099;
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700557
558 if (client->irq > 0) {
559 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
Alexandre Bellonif0b63a12015-07-24 16:07:30 +0200560 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
Akinobu Mita0a966c02016-02-15 23:49:06 +0900561 rx8025_handle_irq,
562 IRQF_ONESHOT,
563 "rx8025", client);
Alexandre Belloni5be39332021-11-07 23:54:55 +0100564 if (err)
565 clear_bit(RTC_FEATURE_ALARM, rx8025->rtc->features);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700566 }
567
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700568 rx8025->rtc->max_user_freq = 1;
569
Alexandre Belloni8670558f2021-11-07 23:54:53 +0100570 set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rx8025->rtc->features);
571 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rx8025->rtc->features);
Akinobu Mitaef5f4a92016-02-15 23:49:09 +0900572
Alexandre Belloni3d358402021-11-07 23:54:56 +0100573 err = rtc_add_group(rx8025->rtc, &rx8025_attr_group);
Alexandre Belloni5e7f6352021-11-07 23:54:52 +0100574 if (err)
575 return err;
576
Alexandre Belloni3d358402021-11-07 23:54:56 +0100577 return devm_rtc_register_device(rx8025->rtc);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700578}
579
580static struct i2c_driver rx8025_driver = {
581 .driver = {
582 .name = "rtc-rx8025",
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700583 },
Uwe Kleine-König31b0cec2023-05-05 14:11:36 +0200584 .probe = rx8025_probe,
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700585 .id_table = rx8025_id,
586};
587
Axel Lin0abc9202012-03-23 15:02:31 -0700588module_i2c_driver(rx8025_driver);
Wolfgang Grandegger3c2b9072009-06-17 16:26:11 -0700589
590MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
591MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
592MODULE_LICENSE("GPL");