1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23c2b9075SWolfgang Grandegger /*
33c2b9075SWolfgang Grandegger * Driver for Epson's RTC module RX-8025 SA/NB
43c2b9075SWolfgang Grandegger *
53c2b9075SWolfgang Grandegger * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com>
63c2b9075SWolfgang Grandegger *
73c2b9075SWolfgang Grandegger * Copyright (C) 2005 by Digi International Inc.
83c2b9075SWolfgang Grandegger * All rights reserved.
93c2b9075SWolfgang Grandegger *
103c2b9075SWolfgang Grandegger * Modified by fengjh at rising.com.cn
11d00cd819SLeslie Lau * <lm-sensors@lm-sensors.org>
123c2b9075SWolfgang Grandegger * 2006.11
133c2b9075SWolfgang Grandegger *
143c2b9075SWolfgang Grandegger * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com>
153c2b9075SWolfgang Grandegger * Converted to new style by Wolfgang Grandegger <wg@grandegger.com>
163c2b9075SWolfgang Grandegger * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com>
173c2b9075SWolfgang Grandegger */
183c2b9075SWolfgang Grandegger #include <linux/bcd.h>
192ddd1869SAlexandre Belloni #include <linux/bitops.h>
203c2b9075SWolfgang Grandegger #include <linux/i2c.h>
2115d3bdc2SAlexandre Belloni #include <linux/kernel.h>
22e59b3c73SChristophe JAILLET #include <linux/kstrtox.h>
2315d3bdc2SAlexandre Belloni #include <linux/module.h>
243c2b9075SWolfgang Grandegger #include <linux/rtc.h>
253c2b9075SWolfgang Grandegger
263c2b9075SWolfgang Grandegger /* Register definitions */
273c2b9075SWolfgang Grandegger #define RX8025_REG_SEC 0x00
283c2b9075SWolfgang Grandegger #define RX8025_REG_MIN 0x01
293c2b9075SWolfgang Grandegger #define RX8025_REG_HOUR 0x02
303c2b9075SWolfgang Grandegger #define RX8025_REG_WDAY 0x03
313c2b9075SWolfgang Grandegger #define RX8025_REG_MDAY 0x04
323c2b9075SWolfgang Grandegger #define RX8025_REG_MONTH 0x05
333c2b9075SWolfgang Grandegger #define RX8025_REG_YEAR 0x06
343c2b9075SWolfgang Grandegger #define RX8025_REG_DIGOFF 0x07
353c2b9075SWolfgang Grandegger #define RX8025_REG_ALWMIN 0x08
363c2b9075SWolfgang Grandegger #define RX8025_REG_ALWHOUR 0x09
373c2b9075SWolfgang Grandegger #define RX8025_REG_ALWWDAY 0x0a
383c2b9075SWolfgang Grandegger #define RX8025_REG_ALDMIN 0x0b
393c2b9075SWolfgang Grandegger #define RX8025_REG_ALDHOUR 0x0c
403c2b9075SWolfgang Grandegger /* 0x0d is reserved */
413c2b9075SWolfgang Grandegger #define RX8025_REG_CTRL1 0x0e
423c2b9075SWolfgang Grandegger #define RX8025_REG_CTRL2 0x0f
433c2b9075SWolfgang Grandegger
443c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL1_CT (7 << 0)
453c2b9075SWolfgang Grandegger /* 1 Hz periodic level irq */
463c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL1_CT_1HZ 4
472ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL1_TEST BIT(3)
482ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL1_1224 BIT(5)
492ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL1_DALE BIT(6)
502ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL1_WALE BIT(7)
513c2b9075SWolfgang Grandegger
522ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_DAFG BIT(0)
532ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_WAFG BIT(1)
542ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_CTFG BIT(2)
552ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_PON BIT(4)
562ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_XST BIT(5)
572ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_VDET BIT(6)
583c2b9075SWolfgang Grandegger
5971af9156SMathew McBride #define RX8035_BIT_HOUR_1224 BIT(7)
6071af9156SMathew McBride
613c2b9075SWolfgang Grandegger /* Clock precision adjustment */
623c2b9075SWolfgang Grandegger #define RX8025_ADJ_RESOLUTION 3050 /* in ppb */
633c2b9075SWolfgang Grandegger #define RX8025_ADJ_DATA_MAX 62
643c2b9075SWolfgang Grandegger #define RX8025_ADJ_DATA_MIN -62
653c2b9075SWolfgang Grandegger
66f120e2e3SMathew McBride enum rx_model {
67f120e2e3SMathew McBride model_rx_unknown,
68f120e2e3SMathew McBride model_rx_8025,
69f120e2e3SMathew McBride model_rx_8035,
70f120e2e3SMathew McBride model_last
71f120e2e3SMathew McBride };
72f120e2e3SMathew McBride
733c2b9075SWolfgang Grandegger static const struct i2c_device_id rx8025_id[] = {
74f120e2e3SMathew McBride { "rx8025", model_rx_8025 },
75f120e2e3SMathew McBride { "rx8035", model_rx_8035 },
763c2b9075SWolfgang Grandegger { }
773c2b9075SWolfgang Grandegger };
783c2b9075SWolfgang Grandegger MODULE_DEVICE_TABLE(i2c, rx8025_id);
793c2b9075SWolfgang Grandegger
803c2b9075SWolfgang Grandegger struct rx8025_data {
813c2b9075SWolfgang Grandegger struct rtc_device *rtc;
82f120e2e3SMathew McBride enum rx_model model;
833c2b9075SWolfgang Grandegger u8 ctrl1;
8471af9156SMathew McBride int is_24;
853c2b9075SWolfgang Grandegger };
863c2b9075SWolfgang Grandegger
rx8025_read_reg(const struct i2c_client * client,u8 number)87fd9061fbSAlexandre Belloni static s32 rx8025_read_reg(const struct i2c_client *client, u8 number)
883c2b9075SWolfgang Grandegger {
89fd9061fbSAlexandre Belloni return i2c_smbus_read_byte_data(client, number << 4);
903c2b9075SWolfgang Grandegger }
913c2b9075SWolfgang Grandegger
rx8025_read_regs(const struct i2c_client * client,u8 number,u8 length,u8 * values)92fd9061fbSAlexandre Belloni static int rx8025_read_regs(const struct i2c_client *client,
93fd9061fbSAlexandre Belloni u8 number, u8 length, u8 *values)
943c2b9075SWolfgang Grandegger {
95fd9061fbSAlexandre Belloni int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length,
96fd9061fbSAlexandre Belloni values);
97fd9061fbSAlexandre Belloni if (ret != length)
983c2b9075SWolfgang Grandegger return ret < 0 ? ret : -EIO;
993c2b9075SWolfgang Grandegger
1003c2b9075SWolfgang Grandegger return 0;
1013c2b9075SWolfgang Grandegger }
1023c2b9075SWolfgang Grandegger
rx8025_write_reg(const struct i2c_client * client,u8 number,u8 value)103fd9061fbSAlexandre Belloni static s32 rx8025_write_reg(const struct i2c_client *client, u8 number,
104fd9061fbSAlexandre Belloni u8 value)
1053c2b9075SWolfgang Grandegger {
106fd9061fbSAlexandre Belloni return i2c_smbus_write_byte_data(client, number << 4, value);
1073c2b9075SWolfgang Grandegger }
1083c2b9075SWolfgang Grandegger
rx8025_write_regs(const struct i2c_client * client,u8 number,u8 length,const u8 * values)109fd9061fbSAlexandre Belloni static s32 rx8025_write_regs(const struct i2c_client *client,
110fd9061fbSAlexandre Belloni u8 number, u8 length, const u8 *values)
1113c2b9075SWolfgang Grandegger {
112fd9061fbSAlexandre Belloni return i2c_smbus_write_i2c_block_data(client, number << 4,
1133c2b9075SWolfgang Grandegger length, values);
1143c2b9075SWolfgang Grandegger }
1153c2b9075SWolfgang Grandegger
rx8025_is_osc_stopped(enum rx_model model,int ctrl2)116f120e2e3SMathew McBride static int rx8025_is_osc_stopped(enum rx_model model, int ctrl2)
117f120e2e3SMathew McBride {
118f120e2e3SMathew McBride int xstp = ctrl2 & RX8025_BIT_CTRL2_XST;
119f120e2e3SMathew McBride /* XSTP bit has different polarity on RX-8025 vs RX-8035.
120f120e2e3SMathew McBride * RX-8025: 0 == oscillator stopped
121f120e2e3SMathew McBride * RX-8035: 1 == oscillator stopped
122f120e2e3SMathew McBride */
123f120e2e3SMathew McBride
124f120e2e3SMathew McBride if (model == model_rx_8025)
125f120e2e3SMathew McBride xstp = !xstp;
126f120e2e3SMathew McBride
127f120e2e3SMathew McBride return xstp;
128f120e2e3SMathew McBride }
129f120e2e3SMathew McBride
rx8025_check_validity(struct device * dev)130efbbb4fdSAlexandre Belloni static int rx8025_check_validity(struct device *dev)
131efbbb4fdSAlexandre Belloni {
13247a3c048SNobuhiro Iwamatsu struct i2c_client *client = to_i2c_client(dev);
133f120e2e3SMathew McBride struct rx8025_data *drvdata = dev_get_drvdata(dev);
134efbbb4fdSAlexandre Belloni int ctrl2;
135f120e2e3SMathew McBride int xstp;
136efbbb4fdSAlexandre Belloni
13747a3c048SNobuhiro Iwamatsu ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
138efbbb4fdSAlexandre Belloni if (ctrl2 < 0)
139efbbb4fdSAlexandre Belloni return ctrl2;
140efbbb4fdSAlexandre Belloni
141efbbb4fdSAlexandre Belloni if (ctrl2 & RX8025_BIT_CTRL2_VDET)
142efbbb4fdSAlexandre Belloni dev_warn(dev, "power voltage drop detected\n");
143efbbb4fdSAlexandre Belloni
144efbbb4fdSAlexandre Belloni if (ctrl2 & RX8025_BIT_CTRL2_PON) {
145efbbb4fdSAlexandre Belloni dev_warn(dev, "power-on reset detected, date is invalid\n");
146efbbb4fdSAlexandre Belloni return -EINVAL;
147efbbb4fdSAlexandre Belloni }
148efbbb4fdSAlexandre Belloni
149f120e2e3SMathew McBride xstp = rx8025_is_osc_stopped(drvdata->model, ctrl2);
150f120e2e3SMathew McBride if (xstp) {
151efbbb4fdSAlexandre Belloni dev_warn(dev, "crystal stopped, date is invalid\n");
152efbbb4fdSAlexandre Belloni return -EINVAL;
153efbbb4fdSAlexandre Belloni }
154efbbb4fdSAlexandre Belloni
155efbbb4fdSAlexandre Belloni return 0;
156efbbb4fdSAlexandre Belloni }
157efbbb4fdSAlexandre Belloni
rx8025_reset_validity(struct i2c_client * client)1588c4a4467SAlexandre Belloni static int rx8025_reset_validity(struct i2c_client *client)
1598c4a4467SAlexandre Belloni {
160f120e2e3SMathew McBride struct rx8025_data *drvdata = i2c_get_clientdata(client);
1618c4a4467SAlexandre Belloni int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
1628c4a4467SAlexandre Belloni
1638c4a4467SAlexandre Belloni if (ctrl2 < 0)
1648c4a4467SAlexandre Belloni return ctrl2;
1658c4a4467SAlexandre Belloni
1668c4a4467SAlexandre Belloni ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
1678c4a4467SAlexandre Belloni
168f120e2e3SMathew McBride if (drvdata->model == model_rx_8025)
169f120e2e3SMathew McBride ctrl2 |= RX8025_BIT_CTRL2_XST;
170f120e2e3SMathew McBride else
171f120e2e3SMathew McBride ctrl2 &= ~(RX8025_BIT_CTRL2_XST);
172f120e2e3SMathew McBride
1738c4a4467SAlexandre Belloni return rx8025_write_reg(client, RX8025_REG_CTRL2,
174f120e2e3SMathew McBride ctrl2);
1758c4a4467SAlexandre Belloni }
1768c4a4467SAlexandre Belloni
rx8025_handle_irq(int irq,void * dev_id)177b6a57c95SAlexandre Belloni static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
1783c2b9075SWolfgang Grandegger {
1793c2b9075SWolfgang Grandegger struct i2c_client *client = dev_id;
1803c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = i2c_get_clientdata(client);
181f120e2e3SMathew McBride int status, xstp;
1823c2b9075SWolfgang Grandegger
18331247546SAlexandre Belloni rtc_lock(rx8025->rtc);
184fd9061fbSAlexandre Belloni status = rx8025_read_reg(client, RX8025_REG_CTRL2);
185fd9061fbSAlexandre Belloni if (status < 0)
1863c2b9075SWolfgang Grandegger goto out;
1873c2b9075SWolfgang Grandegger
188f120e2e3SMathew McBride xstp = rx8025_is_osc_stopped(rx8025->model, status);
189f120e2e3SMathew McBride if (xstp)
1903c2b9075SWolfgang Grandegger dev_warn(&client->dev, "Oscillation stop was detected,"
1913c2b9075SWolfgang Grandegger "you may have to readjust the clock\n");
1923c2b9075SWolfgang Grandegger
1933c2b9075SWolfgang Grandegger if (status & RX8025_BIT_CTRL2_CTFG) {
1943c2b9075SWolfgang Grandegger /* periodic */
1953c2b9075SWolfgang Grandegger status &= ~RX8025_BIT_CTRL2_CTFG;
1963c2b9075SWolfgang Grandegger rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
1973c2b9075SWolfgang Grandegger }
1983c2b9075SWolfgang Grandegger
1993c2b9075SWolfgang Grandegger if (status & RX8025_BIT_CTRL2_DAFG) {
2003c2b9075SWolfgang Grandegger /* alarm */
2013c2b9075SWolfgang Grandegger status &= RX8025_BIT_CTRL2_DAFG;
2023c2b9075SWolfgang Grandegger if (rx8025_write_reg(client, RX8025_REG_CTRL1,
2033c2b9075SWolfgang Grandegger rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
2043c2b9075SWolfgang Grandegger goto out;
2053c2b9075SWolfgang Grandegger rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
2063c2b9075SWolfgang Grandegger }
2073c2b9075SWolfgang Grandegger
2083c2b9075SWolfgang Grandegger out:
20931247546SAlexandre Belloni rtc_unlock(rx8025->rtc);
2109dbe3852SAkinobu Mita
211b6a57c95SAlexandre Belloni return IRQ_HANDLED;
2123c2b9075SWolfgang Grandegger }
2133c2b9075SWolfgang Grandegger
rx8025_get_time(struct device * dev,struct rtc_time * dt)2143c2b9075SWolfgang Grandegger static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
2153c2b9075SWolfgang Grandegger {
21647a3c048SNobuhiro Iwamatsu struct i2c_client *client = to_i2c_client(dev);
2173c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev);
218fd9061fbSAlexandre Belloni u8 date[7];
219efbbb4fdSAlexandre Belloni int err;
2203c2b9075SWolfgang Grandegger
221efbbb4fdSAlexandre Belloni err = rx8025_check_validity(dev);
222efbbb4fdSAlexandre Belloni if (err)
223efbbb4fdSAlexandre Belloni return err;
2246f0a8cfeSAlexandre Belloni
22547a3c048SNobuhiro Iwamatsu err = rx8025_read_regs(client, RX8025_REG_SEC, 7, date);
2263c2b9075SWolfgang Grandegger if (err)
2273c2b9075SWolfgang Grandegger return err;
2283c2b9075SWolfgang Grandegger
2291921cab1SAndy Shevchenko dev_dbg(dev, "%s: read %7ph\n", __func__, date);
2303c2b9075SWolfgang Grandegger
2313c2b9075SWolfgang Grandegger dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
2323c2b9075SWolfgang Grandegger dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
23371af9156SMathew McBride if (rx8025->is_24)
2343c2b9075SWolfgang Grandegger dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
2353c2b9075SWolfgang Grandegger else
2363c2b9075SWolfgang Grandegger dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
2373c2b9075SWolfgang Grandegger + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
2383c2b9075SWolfgang Grandegger
2393c2b9075SWolfgang Grandegger dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
2403c2b9075SWolfgang Grandegger dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
24132672c55SAlexandre Belloni dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100;
2423c2b9075SWolfgang Grandegger
2431921cab1SAndy Shevchenko dev_dbg(dev, "%s: date %ptRr\n", __func__, dt);
2443c2b9075SWolfgang Grandegger
24522652ba7SAlexandre Belloni return 0;
2463c2b9075SWolfgang Grandegger }
2473c2b9075SWolfgang Grandegger
rx8025_set_time(struct device * dev,struct rtc_time * dt)2483c2b9075SWolfgang Grandegger static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
2493c2b9075SWolfgang Grandegger {
25047a3c048SNobuhiro Iwamatsu struct i2c_client *client = to_i2c_client(dev);
2513c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev);
2523c2b9075SWolfgang Grandegger u8 date[7];
2538c4a4467SAlexandre Belloni int ret;
2543c2b9075SWolfgang Grandegger
2553c2b9075SWolfgang Grandegger /*
2563c2b9075SWolfgang Grandegger * Here the read-only bits are written as "0". I'm not sure if that
2573c2b9075SWolfgang Grandegger * is sound.
2583c2b9075SWolfgang Grandegger */
2593c2b9075SWolfgang Grandegger date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
2603c2b9075SWolfgang Grandegger date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
26171af9156SMathew McBride if (rx8025->is_24)
2623c2b9075SWolfgang Grandegger date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
2633c2b9075SWolfgang Grandegger else
2643c2b9075SWolfgang Grandegger date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
2653c2b9075SWolfgang Grandegger | bin2bcd((dt->tm_hour + 11) % 12 + 1);
2663c2b9075SWolfgang Grandegger
2673c2b9075SWolfgang Grandegger date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
2683c2b9075SWolfgang Grandegger date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
2693c2b9075SWolfgang Grandegger date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
27032672c55SAlexandre Belloni date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
2713c2b9075SWolfgang Grandegger
2721921cab1SAndy Shevchenko dev_dbg(dev, "%s: write %7ph\n", __func__, date);
2733c2b9075SWolfgang Grandegger
27447a3c048SNobuhiro Iwamatsu ret = rx8025_write_regs(client, RX8025_REG_SEC, 7, date);
2758c4a4467SAlexandre Belloni if (ret < 0)
2768c4a4467SAlexandre Belloni return ret;
2778c4a4467SAlexandre Belloni
27847a3c048SNobuhiro Iwamatsu return rx8025_reset_validity(client);
2793c2b9075SWolfgang Grandegger }
2803c2b9075SWolfgang Grandegger
rx8025_init_client(struct i2c_client * client)2816f0a8cfeSAlexandre Belloni static int rx8025_init_client(struct i2c_client *client)
2823c2b9075SWolfgang Grandegger {
2833c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = i2c_get_clientdata(client);
2843c2b9075SWolfgang Grandegger u8 ctrl[2], ctrl2;
2853c2b9075SWolfgang Grandegger int need_clear = 0;
28671af9156SMathew McBride int hour_reg;
2873c2b9075SWolfgang Grandegger int err;
2883c2b9075SWolfgang Grandegger
28947a3c048SNobuhiro Iwamatsu err = rx8025_read_regs(client, RX8025_REG_CTRL1, 2, ctrl);
2903c2b9075SWolfgang Grandegger if (err)
2913c2b9075SWolfgang Grandegger goto out;
2923c2b9075SWolfgang Grandegger
2933c2b9075SWolfgang Grandegger /* Keep test bit zero ! */
2943c2b9075SWolfgang Grandegger rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
2953c2b9075SWolfgang Grandegger
2963c2b9075SWolfgang Grandegger if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
2973c2b9075SWolfgang Grandegger dev_warn(&client->dev, "Alarm was detected\n");
2983c2b9075SWolfgang Grandegger need_clear = 1;
2993c2b9075SWolfgang Grandegger }
3003c2b9075SWolfgang Grandegger
3015c66e1e0SAlexandre Belloni if (ctrl[1] & RX8025_BIT_CTRL2_CTFG)
3023c2b9075SWolfgang Grandegger need_clear = 1;
3033c2b9075SWolfgang Grandegger
3046f0a8cfeSAlexandre Belloni if (need_clear) {
305a27c7bf6SAlexandre Belloni ctrl2 = ctrl[1];
3068c4a4467SAlexandre Belloni ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
3073c2b9075SWolfgang Grandegger RX8025_BIT_CTRL2_DAFG);
3083c2b9075SWolfgang Grandegger
3093c2b9075SWolfgang Grandegger err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
3103c2b9075SWolfgang Grandegger }
31171af9156SMathew McBride
31271af9156SMathew McBride if (rx8025->model == model_rx_8035) {
31371af9156SMathew McBride /* In RX-8035, 12/24 flag is in the hour register */
31471af9156SMathew McBride hour_reg = rx8025_read_reg(client, RX8025_REG_HOUR);
31571af9156SMathew McBride if (hour_reg < 0)
31671af9156SMathew McBride return hour_reg;
31771af9156SMathew McBride rx8025->is_24 = (hour_reg & RX8035_BIT_HOUR_1224);
31871af9156SMathew McBride } else {
31971af9156SMathew McBride rx8025->is_24 = (ctrl[1] & RX8025_BIT_CTRL1_1224);
32071af9156SMathew McBride }
3213c2b9075SWolfgang Grandegger out:
3223c2b9075SWolfgang Grandegger return err;
3233c2b9075SWolfgang Grandegger }
3243c2b9075SWolfgang Grandegger
3253c2b9075SWolfgang Grandegger /* Alarm support */
rx8025_read_alarm(struct device * dev,struct rtc_wkalrm * t)3263c2b9075SWolfgang Grandegger static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
3273c2b9075SWolfgang Grandegger {
32847a3c048SNobuhiro Iwamatsu struct i2c_client *client = to_i2c_client(dev);
3293c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev);
330fd9061fbSAlexandre Belloni u8 ald[2];
331fd9061fbSAlexandre Belloni int ctrl2, err;
3323c2b9075SWolfgang Grandegger
3333c2b9075SWolfgang Grandegger err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
3343c2b9075SWolfgang Grandegger if (err)
3353c2b9075SWolfgang Grandegger return err;
3363c2b9075SWolfgang Grandegger
337fd9061fbSAlexandre Belloni ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
338fd9061fbSAlexandre Belloni if (ctrl2 < 0)
339fd9061fbSAlexandre Belloni return ctrl2;
3403c2b9075SWolfgang Grandegger
3413c2b9075SWolfgang Grandegger dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
3423c2b9075SWolfgang Grandegger __func__, ald[0], ald[1], ctrl2);
3433c2b9075SWolfgang Grandegger
3443c2b9075SWolfgang Grandegger /* Hardware alarms precision is 1 minute! */
3453c2b9075SWolfgang Grandegger t->time.tm_sec = 0;
3463c2b9075SWolfgang Grandegger t->time.tm_min = bcd2bin(ald[0] & 0x7f);
34771af9156SMathew McBride if (rx8025->is_24)
3483c2b9075SWolfgang Grandegger t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
3493c2b9075SWolfgang Grandegger else
3503c2b9075SWolfgang Grandegger t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
3513c2b9075SWolfgang Grandegger + (ald[1] & 0x20 ? 12 : 0);
3523c2b9075SWolfgang Grandegger
353862cac15SAndy Shevchenko dev_dbg(dev, "%s: date: %ptRr\n", __func__, &t->time);
3543c2b9075SWolfgang Grandegger t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
3553c2b9075SWolfgang Grandegger t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
3563c2b9075SWolfgang Grandegger
3573c2b9075SWolfgang Grandegger return err;
3583c2b9075SWolfgang Grandegger }
3593c2b9075SWolfgang Grandegger
rx8025_set_alarm(struct device * dev,struct rtc_wkalrm * t)3603c2b9075SWolfgang Grandegger static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
3613c2b9075SWolfgang Grandegger {
3623c2b9075SWolfgang Grandegger struct i2c_client *client = to_i2c_client(dev);
3633c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev);
3643c2b9075SWolfgang Grandegger u8 ald[2];
3653c2b9075SWolfgang Grandegger int err;
3663c2b9075SWolfgang Grandegger
3673c2b9075SWolfgang Grandegger ald[0] = bin2bcd(t->time.tm_min);
36871af9156SMathew McBride if (rx8025->is_24)
3693c2b9075SWolfgang Grandegger ald[1] = bin2bcd(t->time.tm_hour);
3703c2b9075SWolfgang Grandegger else
3713c2b9075SWolfgang Grandegger ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
3723c2b9075SWolfgang Grandegger | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
3733c2b9075SWolfgang Grandegger
3743c2b9075SWolfgang Grandegger dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
3753c2b9075SWolfgang Grandegger
3763c2b9075SWolfgang Grandegger if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
3773c2b9075SWolfgang Grandegger rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
37847a3c048SNobuhiro Iwamatsu err = rx8025_write_reg(client, RX8025_REG_CTRL1,
3793c2b9075SWolfgang Grandegger rx8025->ctrl1);
3803c2b9075SWolfgang Grandegger if (err)
3813c2b9075SWolfgang Grandegger return err;
3823c2b9075SWolfgang Grandegger }
38347a3c048SNobuhiro Iwamatsu err = rx8025_write_regs(client, RX8025_REG_ALDMIN, 2, ald);
3843c2b9075SWolfgang Grandegger if (err)
3853c2b9075SWolfgang Grandegger return err;
3863c2b9075SWolfgang Grandegger
3873c2b9075SWolfgang Grandegger if (t->enabled) {
3883c2b9075SWolfgang Grandegger rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
38947a3c048SNobuhiro Iwamatsu err = rx8025_write_reg(client, RX8025_REG_CTRL1,
3903c2b9075SWolfgang Grandegger rx8025->ctrl1);
3913c2b9075SWolfgang Grandegger if (err)
3923c2b9075SWolfgang Grandegger return err;
3933c2b9075SWolfgang Grandegger }
3943c2b9075SWolfgang Grandegger
3953c2b9075SWolfgang Grandegger return 0;
3963c2b9075SWolfgang Grandegger }
3973c2b9075SWolfgang Grandegger
rx8025_alarm_irq_enable(struct device * dev,unsigned int enabled)3983c2b9075SWolfgang Grandegger static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
3993c2b9075SWolfgang Grandegger {
40047a3c048SNobuhiro Iwamatsu struct i2c_client *client = to_i2c_client(dev);
4013c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev);
4023c2b9075SWolfgang Grandegger u8 ctrl1;
4033c2b9075SWolfgang Grandegger int err;
4043c2b9075SWolfgang Grandegger
4053c2b9075SWolfgang Grandegger ctrl1 = rx8025->ctrl1;
4063c2b9075SWolfgang Grandegger if (enabled)
4073c2b9075SWolfgang Grandegger ctrl1 |= RX8025_BIT_CTRL1_DALE;
4083c2b9075SWolfgang Grandegger else
4093c2b9075SWolfgang Grandegger ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
4103c2b9075SWolfgang Grandegger
4113c2b9075SWolfgang Grandegger if (ctrl1 != rx8025->ctrl1) {
4123c2b9075SWolfgang Grandegger rx8025->ctrl1 = ctrl1;
41347a3c048SNobuhiro Iwamatsu err = rx8025_write_reg(client, RX8025_REG_CTRL1,
4143c2b9075SWolfgang Grandegger rx8025->ctrl1);
4153c2b9075SWolfgang Grandegger if (err)
4163c2b9075SWolfgang Grandegger return err;
4173c2b9075SWolfgang Grandegger }
4183c2b9075SWolfgang Grandegger return 0;
4193c2b9075SWolfgang Grandegger }
4203c2b9075SWolfgang Grandegger
4213c2b9075SWolfgang Grandegger /*
4223c2b9075SWolfgang Grandegger * According to the RX8025 SA/NB application manual the frequency and
423b770ffd4SThomas Weber * temperature characteristics can be approximated using the following
4243c2b9075SWolfgang Grandegger * equation:
4253c2b9075SWolfgang Grandegger *
4263c2b9075SWolfgang Grandegger * df = a * (ut - t)**2
4273c2b9075SWolfgang Grandegger *
4283c2b9075SWolfgang Grandegger * df: Frequency deviation in any temperature
4293c2b9075SWolfgang Grandegger * a : Coefficient = (-35 +-5) * 10**-9
4303c2b9075SWolfgang Grandegger * ut: Ultimate temperature in degree = +25 +-5 degree
4313c2b9075SWolfgang Grandegger * t : Any temperature in degree
4323c2b9075SWolfgang Grandegger */
rx8025_read_offset(struct device * dev,long * offset)433b476266fSAlexandre Belloni static int rx8025_read_offset(struct device *dev, long *offset)
4343c2b9075SWolfgang Grandegger {
4353c2b9075SWolfgang Grandegger struct i2c_client *client = to_i2c_client(dev);
436fd9061fbSAlexandre Belloni int digoff;
4373c2b9075SWolfgang Grandegger
438fd9061fbSAlexandre Belloni digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF);
439fd9061fbSAlexandre Belloni if (digoff < 0)
440fd9061fbSAlexandre Belloni return digoff;
4413c2b9075SWolfgang Grandegger
442b476266fSAlexandre Belloni *offset = digoff >= 64 ? digoff - 128 : digoff;
443b476266fSAlexandre Belloni if (*offset > 0)
444b476266fSAlexandre Belloni (*offset)--;
445b476266fSAlexandre Belloni *offset *= RX8025_ADJ_RESOLUTION;
4463c2b9075SWolfgang Grandegger
4473c2b9075SWolfgang Grandegger return 0;
4483c2b9075SWolfgang Grandegger }
4493c2b9075SWolfgang Grandegger
rx8025_set_offset(struct device * dev,long offset)450b476266fSAlexandre Belloni static int rx8025_set_offset(struct device *dev, long offset)
4513c2b9075SWolfgang Grandegger {
4523c2b9075SWolfgang Grandegger struct i2c_client *client = to_i2c_client(dev);
4533c2b9075SWolfgang Grandegger u8 digoff;
4543c2b9075SWolfgang Grandegger
455b476266fSAlexandre Belloni offset /= RX8025_ADJ_RESOLUTION;
456b476266fSAlexandre Belloni if (offset > RX8025_ADJ_DATA_MAX)
457b476266fSAlexandre Belloni offset = RX8025_ADJ_DATA_MAX;
458b476266fSAlexandre Belloni else if (offset < RX8025_ADJ_DATA_MIN)
459b476266fSAlexandre Belloni offset = RX8025_ADJ_DATA_MIN;
460b476266fSAlexandre Belloni else if (offset > 0)
461b476266fSAlexandre Belloni offset++;
462b476266fSAlexandre Belloni else if (offset < 0)
463b476266fSAlexandre Belloni offset += 128;
464b476266fSAlexandre Belloni digoff = offset;
4653c2b9075SWolfgang Grandegger
466bce7a01aSMinghao Chi return rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
4673c2b9075SWolfgang Grandegger }
4683c2b9075SWolfgang Grandegger
469b476266fSAlexandre Belloni static const struct rtc_class_ops rx8025_rtc_ops = {
470b476266fSAlexandre Belloni .read_time = rx8025_get_time,
471b476266fSAlexandre Belloni .set_time = rx8025_set_time,
472b476266fSAlexandre Belloni .read_alarm = rx8025_read_alarm,
473b476266fSAlexandre Belloni .set_alarm = rx8025_set_alarm,
474b476266fSAlexandre Belloni .alarm_irq_enable = rx8025_alarm_irq_enable,
475b476266fSAlexandre Belloni .read_offset = rx8025_read_offset,
476b476266fSAlexandre Belloni .set_offset = rx8025_set_offset,
477b476266fSAlexandre Belloni };
478b476266fSAlexandre Belloni
rx8025_sysfs_show_clock_adjust(struct device * dev,struct device_attribute * attr,char * buf)4793c2b9075SWolfgang Grandegger static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
4803c2b9075SWolfgang Grandegger struct device_attribute *attr,
4813c2b9075SWolfgang Grandegger char *buf)
4823c2b9075SWolfgang Grandegger {
483b476266fSAlexandre Belloni long adj;
484b476266fSAlexandre Belloni int err;
4853c2b9075SWolfgang Grandegger
486b476266fSAlexandre Belloni dev_warn_once(dev, "clock_adjust_ppb is deprecated, use offset\n");
487b476266fSAlexandre Belloni err = rx8025_read_offset(dev, &adj);
4883c2b9075SWolfgang Grandegger if (err)
4893c2b9075SWolfgang Grandegger return err;
4903c2b9075SWolfgang Grandegger
491b476266fSAlexandre Belloni return sprintf(buf, "%ld\n", -adj);
4923c2b9075SWolfgang Grandegger }
4933c2b9075SWolfgang Grandegger
rx8025_sysfs_store_clock_adjust(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4943c2b9075SWolfgang Grandegger static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
4953c2b9075SWolfgang Grandegger struct device_attribute *attr,
4963c2b9075SWolfgang Grandegger const char *buf, size_t count)
4973c2b9075SWolfgang Grandegger {
498b476266fSAlexandre Belloni long adj;
499b476266fSAlexandre Belloni int err;
5003c2b9075SWolfgang Grandegger
501b476266fSAlexandre Belloni dev_warn_once(dev, "clock_adjust_ppb is deprecated, use offset\n");
502b476266fSAlexandre Belloni if (kstrtol(buf, 10, &adj) != 0)
5033c2b9075SWolfgang Grandegger return -EINVAL;
5043c2b9075SWolfgang Grandegger
505b476266fSAlexandre Belloni err = rx8025_set_offset(dev, -adj);
5063c2b9075SWolfgang Grandegger
5073c2b9075SWolfgang Grandegger return err ? err : count;
5083c2b9075SWolfgang Grandegger }
5093c2b9075SWolfgang Grandegger
5103c2b9075SWolfgang Grandegger static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
5113c2b9075SWolfgang Grandegger rx8025_sysfs_show_clock_adjust,
5123c2b9075SWolfgang Grandegger rx8025_sysfs_store_clock_adjust);
5133c2b9075SWolfgang Grandegger
5143d35840dSAlexandre Belloni static struct attribute *rx8025_attrs[] = {
5153d35840dSAlexandre Belloni &dev_attr_clock_adjust_ppb.attr,
5163d35840dSAlexandre Belloni NULL
5173d35840dSAlexandre Belloni };
5183c2b9075SWolfgang Grandegger
5193d35840dSAlexandre Belloni static const struct attribute_group rx8025_attr_group = {
5203d35840dSAlexandre Belloni .attrs = rx8025_attrs,
5213d35840dSAlexandre Belloni };
5223c2b9075SWolfgang Grandegger
rx8025_probe(struct i2c_client * client)5238ffb7733SUwe Kleine-König static int rx8025_probe(struct i2c_client *client)
5243c2b9075SWolfgang Grandegger {
5258ffb7733SUwe Kleine-König const struct i2c_device_id *id = i2c_match_id(rx8025_id, client);
526110036b4SWolfram Sang struct i2c_adapter *adapter = client->adapter;
5273c2b9075SWolfgang Grandegger struct rx8025_data *rx8025;
5286f0a8cfeSAlexandre Belloni int err = 0;
5293c2b9075SWolfgang Grandegger
5303c2b9075SWolfgang Grandegger if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
5313c2b9075SWolfgang Grandegger | I2C_FUNC_SMBUS_I2C_BLOCK)) {
5323c2b9075SWolfgang Grandegger dev_err(&adapter->dev,
5333c2b9075SWolfgang Grandegger "doesn't support required functionality\n");
534dbcce7cfSAlexandre Belloni return -EIO;
5353c2b9075SWolfgang Grandegger }
5363c2b9075SWolfgang Grandegger
537fac42b41SJingoo Han rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
538b1f9d790SAlexandre Belloni if (!rx8025)
539dbcce7cfSAlexandre Belloni return -ENOMEM;
5403c2b9075SWolfgang Grandegger
5413c2b9075SWolfgang Grandegger i2c_set_clientdata(client, rx8025);
5423c2b9075SWolfgang Grandegger
543f120e2e3SMathew McBride if (id)
544f120e2e3SMathew McBride rx8025->model = id->driver_data;
545f120e2e3SMathew McBride
5466f0a8cfeSAlexandre Belloni err = rx8025_init_client(client);
5473c2b9075SWolfgang Grandegger if (err)
548dbcce7cfSAlexandre Belloni return err;
5493c2b9075SWolfgang Grandegger
5505e7f635aSAlexandre Belloni rx8025->rtc = devm_rtc_allocate_device(&client->dev);
5515e7f635aSAlexandre Belloni if (IS_ERR(rx8025->rtc))
552dbcce7cfSAlexandre Belloni return PTR_ERR(rx8025->rtc);
5535e7f635aSAlexandre Belloni
5545e7f635aSAlexandre Belloni rx8025->rtc->ops = &rx8025_rtc_ops;
5551709d7eeSAlexandre Belloni rx8025->rtc->range_min = RTC_TIMESTAMP_BEGIN_1900;
5561709d7eeSAlexandre Belloni rx8025->rtc->range_max = RTC_TIMESTAMP_END_2099;
5573c2b9075SWolfgang Grandegger
5583c2b9075SWolfgang Grandegger if (client->irq > 0) {
5593c2b9075SWolfgang Grandegger dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
560f0b63a1dSAlexandre Belloni err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
5610a966c07SAkinobu Mita rx8025_handle_irq,
5620a966c07SAkinobu Mita IRQF_ONESHOT,
5630a966c07SAkinobu Mita "rx8025", client);
5645be3933fSAlexandre Belloni if (err)
5655be3933fSAlexandre Belloni clear_bit(RTC_FEATURE_ALARM, rx8025->rtc->features);
5663c2b9075SWolfgang Grandegger }
5673c2b9075SWolfgang Grandegger
5683c2b9075SWolfgang Grandegger rx8025->rtc->max_user_freq = 1;
5693c2b9075SWolfgang Grandegger
5708670558fSAlexandre Belloni set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rx8025->rtc->features);
5718670558fSAlexandre Belloni clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rx8025->rtc->features);
572ef5f4a9eSAkinobu Mita
5733d35840dSAlexandre Belloni err = rtc_add_group(rx8025->rtc, &rx8025_attr_group);
5745e7f635aSAlexandre Belloni if (err)
5755e7f635aSAlexandre Belloni return err;
5765e7f635aSAlexandre Belloni
5773d35840dSAlexandre Belloni return devm_rtc_register_device(rx8025->rtc);
5783c2b9075SWolfgang Grandegger }
5793c2b9075SWolfgang Grandegger
5803c2b9075SWolfgang Grandegger static struct i2c_driver rx8025_driver = {
5813c2b9075SWolfgang Grandegger .driver = {
5823c2b9075SWolfgang Grandegger .name = "rtc-rx8025",
5833c2b9075SWolfgang Grandegger },
584*31b0cecbSUwe Kleine-König .probe = rx8025_probe,
5853c2b9075SWolfgang Grandegger .id_table = rx8025_id,
5863c2b9075SWolfgang Grandegger };
5873c2b9075SWolfgang Grandegger
5880abc9201SAxel Lin module_i2c_driver(rx8025_driver);
5893c2b9075SWolfgang Grandegger
5903c2b9075SWolfgang Grandegger MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
5913c2b9075SWolfgang Grandegger MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
5923c2b9075SWolfgang Grandegger MODULE_LICENSE("GPL");
593