blob: f4c6d6e3b1657d15dd5996e2743bfb23b787e643 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Heiko Schocher52365232011-05-26 16:25:05 -07002/*
Mylène Josserandc2a1c142016-05-03 11:54:34 +02003 * Micro Crystal RV-3029 / RV-3049 rtc class driver
Heiko Schocher52365232011-05-26 16:25:05 -07004 *
5 * Author: Gregory Hermant <gregory.hermant@calao-systems.com>
Michael Büsch2dca3d92016-03-04 22:40:30 +01006 * Michael Buesch <m@bues.ch>
Heiko Schocher52365232011-05-26 16:25:05 -07007 *
8 * based on previously existing rtc class drivers
Heiko Schocher52365232011-05-26 16:25:05 -07009 */
10
11#include <linux/module.h>
12#include <linux/i2c.h>
Mylène Josserandc2a1c142016-05-03 11:54:34 +020013#include <linux/spi/spi.h>
Heiko Schocher52365232011-05-26 16:25:05 -070014#include <linux/bcd.h>
15#include <linux/rtc.h>
Michael Büscha7f6e282016-03-04 22:40:55 +010016#include <linux/delay.h>
17#include <linux/of.h>
Michael Büscha696b312016-03-10 18:34:46 +010018#include <linux/hwmon.h>
19#include <linux/hwmon-sysfs.h>
Mylène Josserande6e38082016-05-03 11:54:33 +020020#include <linux/regmap.h>
Heiko Schocher52365232011-05-26 16:25:05 -070021
22/* Register map */
23/* control section */
Michael Büschaba39d22016-03-04 22:38:45 +010024#define RV3029_ONOFF_CTRL 0x00
Michael Büsch7697de32016-03-04 22:39:49 +010025#define RV3029_ONOFF_CTRL_WE BIT(0)
26#define RV3029_ONOFF_CTRL_TE BIT(1)
27#define RV3029_ONOFF_CTRL_TAR BIT(2)
28#define RV3029_ONOFF_CTRL_EERE BIT(3)
29#define RV3029_ONOFF_CTRL_SRON BIT(4)
30#define RV3029_ONOFF_CTRL_TD0 BIT(5)
31#define RV3029_ONOFF_CTRL_TD1 BIT(6)
32#define RV3029_ONOFF_CTRL_CLKINT BIT(7)
Michael Büschaba39d22016-03-04 22:38:45 +010033#define RV3029_IRQ_CTRL 0x01
Michael Büsch7697de32016-03-04 22:39:49 +010034#define RV3029_IRQ_CTRL_AIE BIT(0)
35#define RV3029_IRQ_CTRL_TIE BIT(1)
36#define RV3029_IRQ_CTRL_V1IE BIT(2)
37#define RV3029_IRQ_CTRL_V2IE BIT(3)
38#define RV3029_IRQ_CTRL_SRIE BIT(4)
Michael Büschaba39d22016-03-04 22:38:45 +010039#define RV3029_IRQ_FLAGS 0x02
Michael Büsch7697de32016-03-04 22:39:49 +010040#define RV3029_IRQ_FLAGS_AF BIT(0)
41#define RV3029_IRQ_FLAGS_TF BIT(1)
42#define RV3029_IRQ_FLAGS_V1IF BIT(2)
43#define RV3029_IRQ_FLAGS_V2IF BIT(3)
44#define RV3029_IRQ_FLAGS_SRF BIT(4)
Michael Büschaba39d22016-03-04 22:38:45 +010045#define RV3029_STATUS 0x03
Michael Büsch7697de32016-03-04 22:39:49 +010046#define RV3029_STATUS_VLOW1 BIT(2)
47#define RV3029_STATUS_VLOW2 BIT(3)
48#define RV3029_STATUS_SR BIT(4)
49#define RV3029_STATUS_PON BIT(5)
50#define RV3029_STATUS_EEBUSY BIT(7)
Michael Büschaba39d22016-03-04 22:38:45 +010051#define RV3029_RST_CTRL 0x04
Michael Büsch7697de32016-03-04 22:39:49 +010052#define RV3029_RST_CTRL_SYSR BIT(4)
Michael Büschaba39d22016-03-04 22:38:45 +010053#define RV3029_CONTROL_SECTION_LEN 0x05
Heiko Schocher52365232011-05-26 16:25:05 -070054
55/* watch section */
Michael Büschaba39d22016-03-04 22:38:45 +010056#define RV3029_W_SEC 0x08
57#define RV3029_W_MINUTES 0x09
58#define RV3029_W_HOURS 0x0A
Michael Büsch7697de32016-03-04 22:39:49 +010059#define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */
60#define RV3029_REG_HR_PM BIT(5) /* PM/AM bit in 12h mode */
Michael Büschaba39d22016-03-04 22:38:45 +010061#define RV3029_W_DATE 0x0B
62#define RV3029_W_DAYS 0x0C
63#define RV3029_W_MONTHS 0x0D
64#define RV3029_W_YEARS 0x0E
65#define RV3029_WATCH_SECTION_LEN 0x07
Heiko Schocher52365232011-05-26 16:25:05 -070066
67/* alarm section */
Michael Büschaba39d22016-03-04 22:38:45 +010068#define RV3029_A_SC 0x10
69#define RV3029_A_MN 0x11
70#define RV3029_A_HR 0x12
71#define RV3029_A_DT 0x13
72#define RV3029_A_DW 0x14
73#define RV3029_A_MO 0x15
74#define RV3029_A_YR 0x16
Mylène Josseranddc492e82016-05-03 11:54:36 +020075#define RV3029_A_AE_X BIT(7)
Michael Büschaba39d22016-03-04 22:38:45 +010076#define RV3029_ALARM_SECTION_LEN 0x07
Heiko Schocher52365232011-05-26 16:25:05 -070077
78/* timer section */
Michael Büschaba39d22016-03-04 22:38:45 +010079#define RV3029_TIMER_LOW 0x18
80#define RV3029_TIMER_HIGH 0x19
Heiko Schocher52365232011-05-26 16:25:05 -070081
82/* temperature section */
Michael Büschaba39d22016-03-04 22:38:45 +010083#define RV3029_TEMP_PAGE 0x20
Heiko Schocher52365232011-05-26 16:25:05 -070084
85/* eeprom data section */
Michael Büschaba39d22016-03-04 22:38:45 +010086#define RV3029_E2P_EEDATA1 0x28
87#define RV3029_E2P_EEDATA2 0x29
Michael Büsch7697de32016-03-04 22:39:49 +010088#define RV3029_E2PDATA_SECTION_LEN 0x02
Heiko Schocher52365232011-05-26 16:25:05 -070089
90/* eeprom control section */
Michael Büschaba39d22016-03-04 22:38:45 +010091#define RV3029_CONTROL_E2P_EECTRL 0x30
Michael Büsch7697de32016-03-04 22:39:49 +010092#define RV3029_EECTRL_THP BIT(0) /* temp scan interval */
93#define RV3029_EECTRL_THE BIT(1) /* thermometer enable */
94#define RV3029_EECTRL_FD0 BIT(2) /* CLKOUT */
95#define RV3029_EECTRL_FD1 BIT(3) /* CLKOUT */
96#define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */
97#define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */
98#define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */
99#define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */
100#define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\
101 RV3029_TRICKLE_5K |\
102 RV3029_TRICKLE_20K |\
103 RV3029_TRICKLE_80K)
104#define RV3029_TRICKLE_SHIFT 4
105#define RV3029_CONTROL_E2P_XOFFS 0x31 /* XTAL offset */
106#define RV3029_CONTROL_E2P_XOFFS_SIGN BIT(7) /* Sign: 1->pos, 0->neg */
107#define RV3029_CONTROL_E2P_QCOEF 0x32 /* XTAL temp drift coef */
108#define RV3029_CONTROL_E2P_TURNOVER 0x33 /* XTAL turnover temp (in *C) */
109#define RV3029_CONTROL_E2P_TOV_MASK 0x3F /* XTAL turnover temp mask */
Heiko Schocher52365232011-05-26 16:25:05 -0700110
111/* user ram section */
Michael Büschaba39d22016-03-04 22:38:45 +0100112#define RV3029_USR1_RAM_PAGE 0x38
113#define RV3029_USR1_SECTION_LEN 0x04
114#define RV3029_USR2_RAM_PAGE 0x3C
115#define RV3029_USR2_SECTION_LEN 0x04
Heiko Schocher52365232011-05-26 16:25:05 -0700116
Mylène Josserande6e38082016-05-03 11:54:33 +0200117struct rv3029_data {
118 struct device *dev;
119 struct rtc_device *rtc;
120 struct regmap *regmap;
121 int irq;
122};
123
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100124static int rv3029_eeprom_busywait(struct rv3029_data *rv3029)
Heiko Schocher52365232011-05-26 16:25:05 -0700125{
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100126 unsigned int sr;
Michael Büscha7f6e282016-03-04 22:40:55 +0100127 int i, ret;
Michael Büscha7f6e282016-03-04 22:40:55 +0100128
129 for (i = 100; i > 0; i--) {
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100130 ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100131 if (ret < 0)
132 break;
133 if (!(sr & RV3029_STATUS_EEBUSY))
134 break;
135 usleep_range(1000, 10000);
136 }
137 if (i <= 0) {
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100138 dev_err(rv3029->dev, "EEPROM busy wait timeout.\n");
Michael Büscha7f6e282016-03-04 22:40:55 +0100139 return -ETIMEDOUT;
140 }
141
142 return ret;
143}
144
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100145static int rv3029_eeprom_exit(struct rv3029_data *rv3029)
Michael Büscha7f6e282016-03-04 22:40:55 +0100146{
147 /* Re-enable eeprom refresh */
Alexandre Belloni609e97f2019-12-14 23:10:09 +0100148 return regmap_update_bits(rv3029->regmap, RV3029_ONOFF_CTRL,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200149 RV3029_ONOFF_CTRL_EERE,
150 RV3029_ONOFF_CTRL_EERE);
Michael Büscha7f6e282016-03-04 22:40:55 +0100151}
152
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100153static int rv3029_eeprom_enter(struct rv3029_data *rv3029)
Michael Büscha7f6e282016-03-04 22:40:55 +0100154{
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100155 unsigned int sr;
Michael Büscha7f6e282016-03-04 22:40:55 +0100156 int ret;
Michael Büscha7f6e282016-03-04 22:40:55 +0100157
158 /* Check whether we are in the allowed voltage range. */
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100159 ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100160 if (ret < 0)
161 return ret;
162 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
163 /* We clear the bits and retry once just in case
164 * we had a brown out in early startup.
165 */
Alexandre Belloni54c59702019-12-14 23:10:10 +0100166 ret = regmap_update_bits(rv3029->regmap, RV3029_STATUS,
167 RV3029_STATUS_VLOW1 |
168 RV3029_STATUS_VLOW2, 0);
Michael Büscha7f6e282016-03-04 22:40:55 +0100169 if (ret < 0)
170 return ret;
171 usleep_range(1000, 10000);
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100172 ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100173 if (ret < 0)
174 return ret;
175 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100176 dev_err(rv3029->dev,
Michael Büscha7f6e282016-03-04 22:40:55 +0100177 "Supply voltage is too low to safely access the EEPROM.\n");
178 return -ENODEV;
179 }
180 }
181
182 /* Disable eeprom refresh. */
Alexandre Belloni609e97f2019-12-14 23:10:09 +0100183 ret = regmap_update_bits(rv3029->regmap, RV3029_ONOFF_CTRL,
184 RV3029_ONOFF_CTRL_EERE, 0);
Michael Büscha7f6e282016-03-04 22:40:55 +0100185 if (ret < 0)
186 return ret;
187
188 /* Wait for any previous eeprom accesses to finish. */
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100189 ret = rv3029_eeprom_busywait(rv3029);
Michael Büscha7f6e282016-03-04 22:40:55 +0100190 if (ret < 0)
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100191 rv3029_eeprom_exit(rv3029);
Michael Büscha7f6e282016-03-04 22:40:55 +0100192
193 return ret;
194}
195
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100196static int rv3029_eeprom_read(struct rv3029_data *rv3029, u8 reg,
Michael Büscha7f6e282016-03-04 22:40:55 +0100197 u8 buf[], size_t len)
198{
199 int ret, err;
200
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100201 err = rv3029_eeprom_enter(rv3029);
Michael Büscha7f6e282016-03-04 22:40:55 +0100202 if (err < 0)
203 return err;
204
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100205 ret = regmap_bulk_read(rv3029->regmap, reg, buf, len);
Michael Büscha7f6e282016-03-04 22:40:55 +0100206
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100207 err = rv3029_eeprom_exit(rv3029);
Michael Büscha7f6e282016-03-04 22:40:55 +0100208 if (err < 0)
209 return err;
210
211 return ret;
212}
213
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100214static int rv3029_eeprom_write(struct rv3029_data *rv3029, u8 reg,
Michael Büscha7f6e282016-03-04 22:40:55 +0100215 u8 const buf[], size_t len)
216{
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100217 unsigned int tmp;
Dan Carpentera6f26602019-08-17 09:56:04 +0300218 int ret, err;
Michael Büscha7f6e282016-03-04 22:40:55 +0100219 size_t i;
Michael Büscha7f6e282016-03-04 22:40:55 +0100220
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100221 err = rv3029_eeprom_enter(rv3029);
Dan Carpentera6f26602019-08-17 09:56:04 +0300222 if (err < 0)
223 return err;
Michael Büscha7f6e282016-03-04 22:40:55 +0100224
225 for (i = 0; i < len; i++, reg++) {
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100226 ret = regmap_read(rv3029->regmap, reg, &tmp);
Michael Büscha7f6e282016-03-04 22:40:55 +0100227 if (ret < 0)
228 break;
229 if (tmp != buf[i]) {
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100230 tmp = buf[i];
231 ret = regmap_write(rv3029->regmap, reg, tmp);
Michael Büscha7f6e282016-03-04 22:40:55 +0100232 if (ret < 0)
233 break;
234 }
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100235 ret = rv3029_eeprom_busywait(rv3029);
Michael Büscha7f6e282016-03-04 22:40:55 +0100236 if (ret < 0)
237 break;
238 }
239
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100240 err = rv3029_eeprom_exit(rv3029);
Dan Carpentera6f26602019-08-17 09:56:04 +0300241 if (err < 0)
242 return err;
Michael Büscha7f6e282016-03-04 22:40:55 +0100243
Dan Carpentera6f26602019-08-17 09:56:04 +0300244 return ret;
Michael Büscha7f6e282016-03-04 22:40:55 +0100245}
246
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100247static int rv3029_eeprom_update_bits(struct rv3029_data *rv3029,
Michael Büsch39387dc22016-03-10 18:34:23 +0100248 u8 reg, u8 mask, u8 set)
249{
250 u8 buf;
251 int ret;
252
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100253 ret = rv3029_eeprom_read(rv3029, reg, &buf, 1);
Michael Büsch39387dc22016-03-10 18:34:23 +0100254 if (ret < 0)
255 return ret;
256 buf &= ~mask;
257 buf |= set & mask;
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100258 ret = rv3029_eeprom_write(rv3029, reg, &buf, 1);
Michael Büsch39387dc22016-03-10 18:34:23 +0100259 if (ret < 0)
260 return ret;
261
262 return 0;
263}
264
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200265static irqreturn_t rv3029_handle_irq(int irq, void *dev_id)
266{
267 struct device *dev = dev_id;
268 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
269 struct mutex *lock = &rv3029->rtc->ops_lock;
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100270 unsigned int flags, controls;
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200271 unsigned long events = 0;
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200272 int ret;
273
274 mutex_lock(lock);
275
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100276 ret = regmap_read(rv3029->regmap, RV3029_IRQ_CTRL, &controls);
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200277 if (ret) {
278 dev_warn(dev, "Read IRQ Control Register error %d\n", ret);
279 mutex_unlock(lock);
280 return IRQ_NONE;
281 }
282
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100283 ret = regmap_read(rv3029->regmap, RV3029_IRQ_FLAGS, &flags);
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200284 if (ret) {
285 dev_warn(dev, "Read IRQ Flags Register error %d\n", ret);
286 mutex_unlock(lock);
287 return IRQ_NONE;
288 }
289
290 if (flags & RV3029_IRQ_FLAGS_AF) {
291 flags &= ~RV3029_IRQ_FLAGS_AF;
292 controls &= ~RV3029_IRQ_CTRL_AIE;
293 events |= RTC_AF;
294 }
295
296 if (events) {
297 rtc_update_irq(rv3029->rtc, 1, events);
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100298 regmap_write(rv3029->regmap, RV3029_IRQ_FLAGS, flags);
299 regmap_write(rv3029->regmap, RV3029_IRQ_CTRL, controls);
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200300 }
301 mutex_unlock(lock);
302
303 return IRQ_HANDLED;
304}
305
Mylène Josserande6e38082016-05-03 11:54:33 +0200306static int rv3029_read_time(struct device *dev, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700307{
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100308 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Heiko Schocher52365232011-05-26 16:25:05 -0700309 int ret;
Michael Büschaba39d22016-03-04 22:38:45 +0100310 u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, };
Heiko Schocher52365232011-05-26 16:25:05 -0700311
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100312 ret = regmap_bulk_read(rv3029->regmap, RV3029_W_SEC, regs,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200313 RV3029_WATCH_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700314 if (ret < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200315 dev_err(dev, "%s: reading RTC section failed\n", __func__);
Heiko Schocher52365232011-05-26 16:25:05 -0700316 return ret;
317 }
318
Mylène Josserandabe2f552016-05-03 11:54:35 +0200319 tm->tm_sec = bcd2bin(regs[RV3029_W_SEC - RV3029_W_SEC]);
320 tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES - RV3029_W_SEC]);
Heiko Schocher52365232011-05-26 16:25:05 -0700321
322 /* HR field has a more complex interpretation */
323 {
Mylène Josserandabe2f552016-05-03 11:54:35 +0200324 const u8 _hr = regs[RV3029_W_HOURS - RV3029_W_SEC];
Michael Büschaba39d22016-03-04 22:38:45 +0100325
326 if (_hr & RV3029_REG_HR_12_24) {
Heiko Schocher52365232011-05-26 16:25:05 -0700327 /* 12h format */
328 tm->tm_hour = bcd2bin(_hr & 0x1f);
Michael Büschaba39d22016-03-04 22:38:45 +0100329 if (_hr & RV3029_REG_HR_PM) /* PM flag set */
Heiko Schocher52365232011-05-26 16:25:05 -0700330 tm->tm_hour += 12;
331 } else /* 24h format */
332 tm->tm_hour = bcd2bin(_hr & 0x3f);
333 }
334
Mylène Josserandabe2f552016-05-03 11:54:35 +0200335 tm->tm_mday = bcd2bin(regs[RV3029_W_DATE - RV3029_W_SEC]);
336 tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS - RV3029_W_SEC]) - 1;
337 tm->tm_year = bcd2bin(regs[RV3029_W_YEARS - RV3029_W_SEC]) + 100;
338 tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS - RV3029_W_SEC]) - 1;
Heiko Schocher52365232011-05-26 16:25:05 -0700339
340 return 0;
341}
342
Mylène Josserande6e38082016-05-03 11:54:33 +0200343static int rv3029_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700344{
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100345 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Heiko Schocher52365232011-05-26 16:25:05 -0700346 struct rtc_time *const tm = &alarm->time;
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100347 unsigned int controls, flags;
Heiko Schocher52365232011-05-26 16:25:05 -0700348 int ret;
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100349 u8 regs[8];
Heiko Schocher52365232011-05-26 16:25:05 -0700350
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100351 ret = regmap_bulk_read(rv3029->regmap, RV3029_A_SC, regs,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200352 RV3029_ALARM_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700353 if (ret < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200354 dev_err(dev, "%s: reading alarm section failed\n", __func__);
Heiko Schocher52365232011-05-26 16:25:05 -0700355 return ret;
356 }
357
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100358 ret = regmap_read(rv3029->regmap, RV3029_IRQ_CTRL, &controls);
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200359 if (ret) {
360 dev_err(dev, "Read IRQ Control Register error %d\n", ret);
361 return ret;
362 }
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100363 ret = regmap_read(rv3029->regmap, RV3029_IRQ_FLAGS, &flags);
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200364 if (ret < 0) {
365 dev_err(dev, "Read IRQ Flags Register error %d\n", ret);
366 return ret;
367 }
368
Mylène Josserandabe2f552016-05-03 11:54:35 +0200369 tm->tm_sec = bcd2bin(regs[RV3029_A_SC - RV3029_A_SC] & 0x7f);
370 tm->tm_min = bcd2bin(regs[RV3029_A_MN - RV3029_A_SC] & 0x7f);
371 tm->tm_hour = bcd2bin(regs[RV3029_A_HR - RV3029_A_SC] & 0x3f);
372 tm->tm_mday = bcd2bin(regs[RV3029_A_DT - RV3029_A_SC] & 0x3f);
373 tm->tm_mon = bcd2bin(regs[RV3029_A_MO - RV3029_A_SC] & 0x1f) - 1;
374 tm->tm_year = bcd2bin(regs[RV3029_A_YR - RV3029_A_SC] & 0x7f) + 100;
375 tm->tm_wday = bcd2bin(regs[RV3029_A_DW - RV3029_A_SC] & 0x07) - 1;
Heiko Schocher52365232011-05-26 16:25:05 -0700376
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200377 alarm->enabled = !!(controls & RV3029_IRQ_CTRL_AIE);
378 alarm->pending = (flags & RV3029_IRQ_FLAGS_AF) && alarm->enabled;
379
Heiko Schocher52365232011-05-26 16:25:05 -0700380 return 0;
381}
382
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200383static int rv3029_alarm_irq_enable(struct device *dev, unsigned int enable)
Heiko Schocher52365232011-05-26 16:25:05 -0700384{
Alexandre Belloni38ce8e32019-12-14 23:10:13 +0100385 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200386
Alexandre Belloni38ce8e32019-12-14 23:10:13 +0100387 return regmap_update_bits(rv3029->regmap, RV3029_IRQ_CTRL,
388 RV3029_IRQ_CTRL_AIE,
389 enable ? RV3029_IRQ_CTRL_AIE : 0);
Heiko Schocher52365232011-05-26 16:25:05 -0700390}
391
Mylène Josserande6e38082016-05-03 11:54:33 +0200392static int rv3029_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700393{
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100394 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Heiko Schocher52365232011-05-26 16:25:05 -0700395 struct rtc_time *const tm = &alarm->time;
396 int ret;
397 u8 regs[8];
398
399 /*
400 * The clock has an 8 bit wide bcd-coded register (they never learn)
401 * for the year. tm_year is an offset from 1900 and we are interested
402 * in the 2000-2099 range, so any value less than 100 is invalid.
403 */
404 if (tm->tm_year < 100)
405 return -EINVAL;
406
Mylène Josseranddc492e82016-05-03 11:54:36 +0200407 /* Activate all the alarms with AE_x bit */
408 regs[RV3029_A_SC - RV3029_A_SC] = bin2bcd(tm->tm_sec) | RV3029_A_AE_X;
409 regs[RV3029_A_MN - RV3029_A_SC] = bin2bcd(tm->tm_min) | RV3029_A_AE_X;
410 regs[RV3029_A_HR - RV3029_A_SC] = (bin2bcd(tm->tm_hour) & 0x3f)
411 | RV3029_A_AE_X;
412 regs[RV3029_A_DT - RV3029_A_SC] = (bin2bcd(tm->tm_mday) & 0x3f)
413 | RV3029_A_AE_X;
414 regs[RV3029_A_MO - RV3029_A_SC] = (bin2bcd(tm->tm_mon + 1) & 0x1f)
415 | RV3029_A_AE_X;
416 regs[RV3029_A_DW - RV3029_A_SC] = (bin2bcd(tm->tm_wday + 1) & 0x7)
417 | RV3029_A_AE_X;
418 regs[RV3029_A_YR - RV3029_A_SC] = (bin2bcd(tm->tm_year - 100))
419 | RV3029_A_AE_X;
420
421 /* Write the alarm */
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100422 ret = regmap_bulk_write(rv3029->regmap, RV3029_A_SC, regs,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200423 RV3029_ALARM_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700424 if (ret < 0)
425 return ret;
426
Alexandre Belloni8fd3d602019-12-14 23:10:14 +0100427 return rv3029_alarm_irq_enable(dev, alarm->enabled);
Heiko Schocher52365232011-05-26 16:25:05 -0700428}
429
Mylène Josserande6e38082016-05-03 11:54:33 +0200430static int rv3029_set_time(struct device *dev, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700431{
Alexandre Belloni54c59702019-12-14 23:10:10 +0100432 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Heiko Schocher52365232011-05-26 16:25:05 -0700433 u8 regs[8];
434 int ret;
435
436 /*
437 * The clock has an 8 bit wide bcd-coded register (they never learn)
438 * for the year. tm_year is an offset from 1900 and we are interested
439 * in the 2000-2099 range, so any value less than 100 is invalid.
440 */
441 if (tm->tm_year < 100)
442 return -EINVAL;
443
Mylène Josserandabe2f552016-05-03 11:54:35 +0200444 regs[RV3029_W_SEC - RV3029_W_SEC] = bin2bcd(tm->tm_sec);
445 regs[RV3029_W_MINUTES - RV3029_W_SEC] = bin2bcd(tm->tm_min);
446 regs[RV3029_W_HOURS - RV3029_W_SEC] = bin2bcd(tm->tm_hour);
447 regs[RV3029_W_DATE - RV3029_W_SEC] = bin2bcd(tm->tm_mday);
448 regs[RV3029_W_MONTHS - RV3029_W_SEC] = bin2bcd(tm->tm_mon + 1);
Mylène Josserand38201ca2016-05-03 11:54:37 +0200449 regs[RV3029_W_DAYS - RV3029_W_SEC] = bin2bcd(tm->tm_wday + 1) & 0x7;
Mylène Josserandabe2f552016-05-03 11:54:35 +0200450 regs[RV3029_W_YEARS - RV3029_W_SEC] = bin2bcd(tm->tm_year - 100);
Heiko Schocher52365232011-05-26 16:25:05 -0700451
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100452 ret = regmap_bulk_write(rv3029->regmap, RV3029_W_SEC, regs,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200453 RV3029_WATCH_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700454 if (ret < 0)
455 return ret;
456
Heiko Schocher52365232011-05-26 16:25:05 -0700457 /* clear PON bit */
Alexandre Belloni54c59702019-12-14 23:10:10 +0100458 return regmap_update_bits(rv3029->regmap, RV3029_STATUS,
459 RV3029_STATUS_PON, 0);
Heiko Schocher52365232011-05-26 16:25:05 -0700460}
Mylène Josserandabe2f552016-05-03 11:54:35 +0200461
Alexandre Bellonif630f722019-12-14 23:10:16 +0100462static int rv3029_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
463{
464 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
465 unsigned long vl = 0;
466 int sr, ret = 0;
467
468 switch (cmd) {
469 case RTC_VL_READ:
470 ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
471 if (ret < 0)
472 return ret;
473
474 if (sr & RV3029_STATUS_VLOW1)
475 vl = RTC_VL_ACCURACY_LOW;
476
477 if (sr & (RV3029_STATUS_VLOW2 | RV3029_STATUS_PON))
478 vl |= RTC_VL_DATA_INVALID;
479
480 return put_user(vl, (unsigned int __user *)arg);
481
482 case RTC_VL_CLR:
483 return regmap_update_bits(rv3029->regmap, RV3029_STATUS,
484 RV3029_STATUS_VLOW1, 0);
485
486 default:
487 return -ENOIOCTLCMD;
488 }
489}
490
Michael Büsche27e2162016-03-04 22:41:19 +0100491static const struct rv3029_trickle_tab_elem {
492 u32 r; /* resistance in ohms */
493 u8 conf; /* trickle config bits */
494} rv3029_trickle_tab[] = {
495 {
496 .r = 1076,
497 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
498 RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
499 }, {
500 .r = 1091,
501 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
502 RV3029_TRICKLE_20K,
503 }, {
504 .r = 1137,
505 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
506 RV3029_TRICKLE_80K,
507 }, {
508 .r = 1154,
509 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
510 }, {
511 .r = 1371,
512 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
513 RV3029_TRICKLE_80K,
514 }, {
515 .r = 1395,
516 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
517 }, {
518 .r = 1472,
519 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
520 }, {
521 .r = 1500,
522 .conf = RV3029_TRICKLE_1K,
523 }, {
524 .r = 3810,
525 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
526 RV3029_TRICKLE_80K,
527 }, {
528 .r = 4000,
529 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
530 }, {
531 .r = 4706,
532 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
533 }, {
534 .r = 5000,
535 .conf = RV3029_TRICKLE_5K,
536 }, {
537 .r = 16000,
538 .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
539 }, {
540 .r = 20000,
541 .conf = RV3029_TRICKLE_20K,
542 }, {
543 .r = 80000,
544 .conf = RV3029_TRICKLE_80K,
545 },
546};
547
Mylène Josserande6e38082016-05-03 11:54:33 +0200548static void rv3029_trickle_config(struct device *dev)
Michael Büsche27e2162016-03-04 22:41:19 +0100549{
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100550 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Mylène Josserande6e38082016-05-03 11:54:33 +0200551 struct device_node *of_node = dev->of_node;
Michael Büsche27e2162016-03-04 22:41:19 +0100552 const struct rv3029_trickle_tab_elem *elem;
553 int i, err;
554 u32 ohms;
Michael Büsch39387dc22016-03-10 18:34:23 +0100555 u8 trickle_set_bits;
Michael Büsche27e2162016-03-04 22:41:19 +0100556
557 if (!of_node)
558 return;
559
560 /* Configure the trickle charger. */
Michael Büsche27e2162016-03-04 22:41:19 +0100561 err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms);
562 if (err) {
563 /* Disable trickle charger. */
Michael Büsch39387dc22016-03-10 18:34:23 +0100564 trickle_set_bits = 0;
Michael Büsche27e2162016-03-04 22:41:19 +0100565 } else {
566 /* Enable trickle charger. */
567 for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) {
568 elem = &rv3029_trickle_tab[i];
569 if (elem->r >= ohms)
570 break;
571 }
Michael Büsch39387dc22016-03-10 18:34:23 +0100572 trickle_set_bits = elem->conf;
Mylène Josserande6e38082016-05-03 11:54:33 +0200573 dev_info(dev,
Michael Büsche27e2162016-03-04 22:41:19 +0100574 "Trickle charger enabled at %d ohms resistance.\n",
575 elem->r);
576 }
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100577 err = rv3029_eeprom_update_bits(rv3029, RV3029_CONTROL_E2P_EECTRL,
Michael Büsch39387dc22016-03-10 18:34:23 +0100578 RV3029_TRICKLE_MASK,
579 trickle_set_bits);
Mylène Josserandabe2f552016-05-03 11:54:35 +0200580 if (err < 0)
Mylène Josserande6e38082016-05-03 11:54:33 +0200581 dev_err(dev, "Failed to update trickle charger config\n");
Michael Büsche27e2162016-03-04 22:41:19 +0100582}
583
Michael Büscha696b312016-03-10 18:34:46 +0100584#ifdef CONFIG_RTC_DRV_RV3029_HWMON
585
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100586static int rv3029_read_temp(struct rv3029_data *rv3029, int *temp_mC)
Michael Büscha696b312016-03-10 18:34:46 +0100587{
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100588 unsigned int temp;
Michael Büscha696b312016-03-10 18:34:46 +0100589 int ret;
Michael Büscha696b312016-03-10 18:34:46 +0100590
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100591 ret = regmap_read(rv3029->regmap, RV3029_TEMP_PAGE, &temp);
Michael Büscha696b312016-03-10 18:34:46 +0100592 if (ret < 0)
593 return ret;
594
595 *temp_mC = ((int)temp - 60) * 1000;
596
597 return 0;
598}
599
600static ssize_t rv3029_hwmon_show_temp(struct device *dev,
601 struct device_attribute *attr,
602 char *buf)
603{
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100604 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Michael Büscha696b312016-03-10 18:34:46 +0100605 int ret, temp_mC;
606
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100607 ret = rv3029_read_temp(rv3029, &temp_mC);
Michael Büscha696b312016-03-10 18:34:46 +0100608 if (ret < 0)
609 return ret;
610
611 return sprintf(buf, "%d\n", temp_mC);
612}
613
614static ssize_t rv3029_hwmon_set_update_interval(struct device *dev,
615 struct device_attribute *attr,
616 const char *buf,
617 size_t count)
618{
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100619 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
620 unsigned int th_set_bits = 0;
Michael Büscha696b312016-03-10 18:34:46 +0100621 unsigned long interval_ms;
622 int ret;
Michael Büscha696b312016-03-10 18:34:46 +0100623
624 ret = kstrtoul(buf, 10, &interval_ms);
625 if (ret < 0)
626 return ret;
627
628 if (interval_ms != 0) {
629 th_set_bits |= RV3029_EECTRL_THE;
630 if (interval_ms >= 16000)
631 th_set_bits |= RV3029_EECTRL_THP;
632 }
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100633 ret = rv3029_eeprom_update_bits(rv3029, RV3029_CONTROL_E2P_EECTRL,
Michael Büscha696b312016-03-10 18:34:46 +0100634 RV3029_EECTRL_THE | RV3029_EECTRL_THP,
635 th_set_bits);
636 if (ret < 0)
637 return ret;
638
639 return count;
640}
641
642static ssize_t rv3029_hwmon_show_update_interval(struct device *dev,
643 struct device_attribute *attr,
644 char *buf)
645{
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100646 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Michael Büscha696b312016-03-10 18:34:46 +0100647 int ret, interval_ms;
648 u8 eectrl;
649
Alexandre Belloni7518dd92019-12-14 23:10:15 +0100650 ret = rv3029_eeprom_read(rv3029, RV3029_CONTROL_E2P_EECTRL,
Michael Büscha696b312016-03-10 18:34:46 +0100651 &eectrl, 1);
652 if (ret < 0)
653 return ret;
654
655 if (eectrl & RV3029_EECTRL_THE) {
656 if (eectrl & RV3029_EECTRL_THP)
657 interval_ms = 16000;
658 else
659 interval_ms = 1000;
660 } else {
661 interval_ms = 0;
662 }
663
664 return sprintf(buf, "%d\n", interval_ms);
665}
666
667static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, rv3029_hwmon_show_temp,
668 NULL, 0);
669static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,
670 rv3029_hwmon_show_update_interval,
671 rv3029_hwmon_set_update_interval, 0);
672
673static struct attribute *rv3029_hwmon_attrs[] = {
674 &sensor_dev_attr_temp1_input.dev_attr.attr,
675 &sensor_dev_attr_update_interval.dev_attr.attr,
676 NULL,
677};
678ATTRIBUTE_GROUPS(rv3029_hwmon);
679
Mylène Josserande6e38082016-05-03 11:54:33 +0200680static void rv3029_hwmon_register(struct device *dev, const char *name)
Michael Büscha696b312016-03-10 18:34:46 +0100681{
Mylène Josserande6e38082016-05-03 11:54:33 +0200682 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Michael Büscha696b312016-03-10 18:34:46 +0100683 struct device *hwmon_dev;
684
Mylène Josserande6e38082016-05-03 11:54:33 +0200685 hwmon_dev = devm_hwmon_device_register_with_groups(dev, name, rv3029,
686 rv3029_hwmon_groups);
Michael Büscha696b312016-03-10 18:34:46 +0100687 if (IS_ERR(hwmon_dev)) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200688 dev_warn(dev, "unable to register hwmon device %ld\n",
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200689 PTR_ERR(hwmon_dev));
Michael Büscha696b312016-03-10 18:34:46 +0100690 }
691}
692
693#else /* CONFIG_RTC_DRV_RV3029_HWMON */
694
Mylène Josserande6e38082016-05-03 11:54:33 +0200695static void rv3029_hwmon_register(struct device *dev, const char *name)
Michael Büscha696b312016-03-10 18:34:46 +0100696{
697}
698
699#endif /* CONFIG_RTC_DRV_RV3029_HWMON */
700
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200701static struct rtc_class_ops rv3029_rtc_ops = {
Mylène Josserande6e38082016-05-03 11:54:33 +0200702 .read_time = rv3029_read_time,
703 .set_time = rv3029_set_time,
Alexandre Bellonif630f722019-12-14 23:10:16 +0100704 .ioctl = rv3029_ioctl,
Heiko Schocher52365232011-05-26 16:25:05 -0700705};
706
Mylène Josserande6e38082016-05-03 11:54:33 +0200707static int rv3029_probe(struct device *dev, struct regmap *regmap, int irq,
708 const char *name)
Heiko Schocher52365232011-05-26 16:25:05 -0700709{
Mylène Josserande6e38082016-05-03 11:54:33 +0200710 struct rv3029_data *rv3029;
Heiko Schocher52365232011-05-26 16:25:05 -0700711 int rc = 0;
Heiko Schocher52365232011-05-26 16:25:05 -0700712
Mylène Josserande6e38082016-05-03 11:54:33 +0200713 rv3029 = devm_kzalloc(dev, sizeof(*rv3029), GFP_KERNEL);
714 if (!rv3029)
715 return -ENOMEM;
Heiko Schocher52365232011-05-26 16:25:05 -0700716
Mylène Josserande6e38082016-05-03 11:54:33 +0200717 rv3029->regmap = regmap;
718 rv3029->irq = irq;
719 rv3029->dev = dev;
720 dev_set_drvdata(dev, rv3029);
721
Mylène Josserande6e38082016-05-03 11:54:33 +0200722 rv3029_trickle_config(dev);
723 rv3029_hwmon_register(dev, name);
Michael Büsche27e2162016-03-04 22:41:19 +0100724
Mylène Josserande6e38082016-05-03 11:54:33 +0200725 rv3029->rtc = devm_rtc_device_register(dev, name, &rv3029_rtc_ops,
726 THIS_MODULE);
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200727 if (IS_ERR(rv3029->rtc)) {
728 dev_err(dev, "unable to register the class device\n");
729 return PTR_ERR(rv3029->rtc);
730 }
Heiko Schocher52365232011-05-26 16:25:05 -0700731
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200732 if (rv3029->irq > 0) {
733 rc = devm_request_threaded_irq(dev, rv3029->irq,
734 NULL, rv3029_handle_irq,
735 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
736 "rv3029", dev);
737 if (rc) {
738 dev_warn(dev, "unable to request IRQ, alarms disabled\n");
739 rv3029->irq = 0;
740 } else {
741 rv3029_rtc_ops.read_alarm = rv3029_read_alarm;
742 rv3029_rtc_ops.set_alarm = rv3029_set_alarm;
743 rv3029_rtc_ops.alarm_irq_enable = rv3029_alarm_irq_enable;
744 }
745 }
746
747 return 0;
Mylène Josserande6e38082016-05-03 11:54:33 +0200748}
Heiko Schocher52365232011-05-26 16:25:05 -0700749
Alexandre Bellonic509e432019-12-14 23:10:08 +0100750static const struct regmap_range rv3029_holes_range[] = {
751 regmap_reg_range(0x05, 0x07),
752 regmap_reg_range(0x0f, 0x0f),
753 regmap_reg_range(0x17, 0x17),
754 regmap_reg_range(0x1a, 0x1f),
755 regmap_reg_range(0x21, 0x27),
756 regmap_reg_range(0x34, 0x37),
757};
758
759static const struct regmap_access_table rv3029_regs = {
760 .no_ranges = rv3029_holes_range,
761 .n_no_ranges = ARRAY_SIZE(rv3029_holes_range),
762};
763
764static const struct regmap_config config = {
765 .reg_bits = 8,
766 .val_bits = 8,
767 .rd_table = &rv3029_regs,
768 .wr_table = &rv3029_regs,
769 .max_register = 0x3f,
770};
771
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200772#if IS_ENABLED(CONFIG_I2C)
773
Mylène Josserande6e38082016-05-03 11:54:33 +0200774static int rv3029_i2c_probe(struct i2c_client *client,
775 const struct i2c_device_id *id)
776{
777 struct regmap *regmap;
Mylène Josserande6e38082016-05-03 11:54:33 +0200778 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK |
779 I2C_FUNC_SMBUS_BYTE)) {
780 dev_err(&client->dev, "Adapter does not support SMBUS_I2C_BLOCK or SMBUS_I2C_BYTE\n");
781 return -ENODEV;
782 }
783
784 regmap = devm_regmap_init_i2c(client, &config);
785 if (IS_ERR(regmap)) {
786 dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
787 __func__, PTR_ERR(regmap));
788 return PTR_ERR(regmap);
789 }
790
791 return rv3029_probe(&client->dev, regmap, client->irq, client->name);
Heiko Schocher52365232011-05-26 16:25:05 -0700792}
793
Arvind Yadav45a63512017-08-20 00:37:55 +0530794static const struct i2c_device_id rv3029_id[] = {
Arnd Bergmann814db2b2016-05-04 11:50:02 +0200795 { "rv3029", 0 },
796 { "rv3029c2", 0 },
797 { }
798};
799MODULE_DEVICE_TABLE(i2c, rv3029_id);
800
Javier Martinez Canillase696a1d2017-03-03 11:29:13 -0300801static const struct of_device_id rv3029_of_match[] = {
Alexandre Belloni45b611c2017-09-15 04:00:02 +0200802 { .compatible = "microcrystal,rv3029" },
803 /* Backward compatibility only, do not use compatibles below: */
Javier Martinez Canillase696a1d2017-03-03 11:29:13 -0300804 { .compatible = "rv3029" },
805 { .compatible = "rv3029c2" },
806 { .compatible = "mc,rv3029c2" },
807 { }
808};
809MODULE_DEVICE_TABLE(of, rv3029_of_match);
810
Michael Büschaba39d22016-03-04 22:38:45 +0100811static struct i2c_driver rv3029_driver = {
Heiko Schocher52365232011-05-26 16:25:05 -0700812 .driver = {
Alexandre Belloni9b45ef92019-12-14 23:10:07 +0100813 .name = "rv3029",
Javier Martinez Canillase696a1d2017-03-03 11:29:13 -0300814 .of_match_table = of_match_ptr(rv3029_of_match),
Heiko Schocher52365232011-05-26 16:25:05 -0700815 },
Mylène Josserande6e38082016-05-03 11:54:33 +0200816 .probe = rv3029_i2c_probe,
Michael Büschaba39d22016-03-04 22:38:45 +0100817 .id_table = rv3029_id,
Heiko Schocher52365232011-05-26 16:25:05 -0700818};
819
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200820static int rv3029_register_driver(void)
821{
822 return i2c_add_driver(&rv3029_driver);
823}
824
825static void rv3029_unregister_driver(void)
826{
827 i2c_del_driver(&rv3029_driver);
828}
829
830#else
831
832static int rv3029_register_driver(void)
833{
834 return 0;
835}
836
837static void rv3029_unregister_driver(void)
838{
839}
840
841#endif
842
843#if IS_ENABLED(CONFIG_SPI_MASTER)
844
845static int rv3049_probe(struct spi_device *spi)
846{
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200847 struct regmap *regmap;
848
849 regmap = devm_regmap_init_spi(spi, &config);
850 if (IS_ERR(regmap)) {
851 dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
852 __func__, PTR_ERR(regmap));
853 return PTR_ERR(regmap);
854 }
855
856 return rv3029_probe(&spi->dev, regmap, spi->irq, "rv3049");
857}
858
859static struct spi_driver rv3049_driver = {
860 .driver = {
861 .name = "rv3049",
862 },
863 .probe = rv3049_probe,
864};
865
866static int rv3049_register_driver(void)
867{
868 return spi_register_driver(&rv3049_driver);
869}
870
871static void rv3049_unregister_driver(void)
872{
873 spi_unregister_driver(&rv3049_driver);
874}
875
876#else
877
878static int rv3049_register_driver(void)
879{
880 return 0;
881}
882
883static void rv3049_unregister_driver(void)
884{
885}
886
887#endif
888
889static int __init rv30x9_init(void)
890{
891 int ret;
892
893 ret = rv3029_register_driver();
894 if (ret) {
895 pr_err("Failed to register rv3029 driver: %d\n", ret);
896 return ret;
897 }
898
899 ret = rv3049_register_driver();
900 if (ret) {
901 pr_err("Failed to register rv3049 driver: %d\n", ret);
902 rv3029_unregister_driver();
903 }
904
905 return ret;
906}
907module_init(rv30x9_init)
908
909static void __exit rv30x9_exit(void)
910{
911 rv3049_unregister_driver();
912 rv3029_unregister_driver();
913}
914module_exit(rv30x9_exit)
Heiko Schocher52365232011-05-26 16:25:05 -0700915
916MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
Michael Büsch2dca3d92016-03-04 22:40:30 +0100917MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200918MODULE_DESCRIPTION("Micro Crystal RV3029/RV3049 RTC driver");
Heiko Schocher52365232011-05-26 16:25:05 -0700919MODULE_LICENSE("GPL");
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200920MODULE_ALIAS("spi:rv3049");