blob: 2d12d13ff747d5c2d1974ae38fca2e33a97a0981 [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
124static int rv3029_read_regs(struct device *dev, u8 reg, u8 *buf,
Mylène Josserandabe2f552016-05-03 11:54:35 +0200125 unsigned int len)
Heiko Schocher52365232011-05-26 16:25:05 -0700126{
Mylène Josserande6e38082016-05-03 11:54:33 +0200127 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Heiko Schocher52365232011-05-26 16:25:05 -0700128
Mylène Josserande6e38082016-05-03 11:54:33 +0200129 return regmap_bulk_read(rv3029->regmap, reg, buf, len);
Heiko Schocher52365232011-05-26 16:25:05 -0700130}
131
Mylène Josserande6e38082016-05-03 11:54:33 +0200132static int rv3029_write_regs(struct device *dev, u8 reg, u8 const buf[],
Mylène Josserandabe2f552016-05-03 11:54:35 +0200133 unsigned int len)
Heiko Schocher52365232011-05-26 16:25:05 -0700134{
Mylène Josserande6e38082016-05-03 11:54:33 +0200135 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
136
Mylène Josserande6e38082016-05-03 11:54:33 +0200137 return regmap_bulk_write(rv3029->regmap, reg, buf, len);
Heiko Schocher52365232011-05-26 16:25:05 -0700138}
139
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100140static int rv3029_eeprom_busywait(struct rv3029_data *rv3029)
Heiko Schocher52365232011-05-26 16:25:05 -0700141{
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100142 unsigned int sr;
Michael Büscha7f6e282016-03-04 22:40:55 +0100143 int i, ret;
Michael Büscha7f6e282016-03-04 22:40:55 +0100144
145 for (i = 100; i > 0; i--) {
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100146 ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100147 if (ret < 0)
148 break;
149 if (!(sr & RV3029_STATUS_EEBUSY))
150 break;
151 usleep_range(1000, 10000);
152 }
153 if (i <= 0) {
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100154 dev_err(rv3029->dev, "EEPROM busy wait timeout.\n");
Michael Büscha7f6e282016-03-04 22:40:55 +0100155 return -ETIMEDOUT;
156 }
157
158 return ret;
159}
160
Mylène Josserande6e38082016-05-03 11:54:33 +0200161static int rv3029_eeprom_exit(struct device *dev)
Michael Büscha7f6e282016-03-04 22:40:55 +0100162{
Alexandre Belloni609e97f2019-12-14 23:10:09 +0100163 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
164
Michael Büscha7f6e282016-03-04 22:40:55 +0100165 /* Re-enable eeprom refresh */
Alexandre Belloni609e97f2019-12-14 23:10:09 +0100166 return regmap_update_bits(rv3029->regmap, RV3029_ONOFF_CTRL,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200167 RV3029_ONOFF_CTRL_EERE,
168 RV3029_ONOFF_CTRL_EERE);
Michael Büscha7f6e282016-03-04 22:40:55 +0100169}
170
Mylène Josserande6e38082016-05-03 11:54:33 +0200171static int rv3029_eeprom_enter(struct device *dev)
Michael Büscha7f6e282016-03-04 22:40:55 +0100172{
Alexandre Belloni609e97f2019-12-14 23:10:09 +0100173 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100174 unsigned int sr;
Michael Büscha7f6e282016-03-04 22:40:55 +0100175 int ret;
Michael Büscha7f6e282016-03-04 22:40:55 +0100176
177 /* Check whether we are in the allowed voltage range. */
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100178 ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100179 if (ret < 0)
180 return ret;
181 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
182 /* We clear the bits and retry once just in case
183 * we had a brown out in early startup.
184 */
Alexandre Belloni54c59702019-12-14 23:10:10 +0100185 ret = regmap_update_bits(rv3029->regmap, RV3029_STATUS,
186 RV3029_STATUS_VLOW1 |
187 RV3029_STATUS_VLOW2, 0);
Michael Büscha7f6e282016-03-04 22:40:55 +0100188 if (ret < 0)
189 return ret;
190 usleep_range(1000, 10000);
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100191 ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100192 if (ret < 0)
193 return ret;
194 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200195 dev_err(dev,
Michael Büscha7f6e282016-03-04 22:40:55 +0100196 "Supply voltage is too low to safely access the EEPROM.\n");
197 return -ENODEV;
198 }
199 }
200
201 /* Disable eeprom refresh. */
Alexandre Belloni609e97f2019-12-14 23:10:09 +0100202 ret = regmap_update_bits(rv3029->regmap, RV3029_ONOFF_CTRL,
203 RV3029_ONOFF_CTRL_EERE, 0);
Michael Büscha7f6e282016-03-04 22:40:55 +0100204 if (ret < 0)
205 return ret;
206
207 /* Wait for any previous eeprom accesses to finish. */
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100208 ret = rv3029_eeprom_busywait(rv3029);
Michael Büscha7f6e282016-03-04 22:40:55 +0100209 if (ret < 0)
Mylène Josserande6e38082016-05-03 11:54:33 +0200210 rv3029_eeprom_exit(dev);
Michael Büscha7f6e282016-03-04 22:40:55 +0100211
212 return ret;
213}
214
Mylène Josserande6e38082016-05-03 11:54:33 +0200215static int rv3029_eeprom_read(struct device *dev, u8 reg,
Michael Büscha7f6e282016-03-04 22:40:55 +0100216 u8 buf[], size_t len)
217{
218 int ret, err;
219
Mylène Josserande6e38082016-05-03 11:54:33 +0200220 err = rv3029_eeprom_enter(dev);
Michael Büscha7f6e282016-03-04 22:40:55 +0100221 if (err < 0)
222 return err;
223
Mylène Josserande6e38082016-05-03 11:54:33 +0200224 ret = rv3029_read_regs(dev, reg, buf, len);
Michael Büscha7f6e282016-03-04 22:40:55 +0100225
Mylène Josserande6e38082016-05-03 11:54:33 +0200226 err = rv3029_eeprom_exit(dev);
Michael Büscha7f6e282016-03-04 22:40:55 +0100227 if (err < 0)
228 return err;
229
230 return ret;
231}
232
Mylène Josserande6e38082016-05-03 11:54:33 +0200233static int rv3029_eeprom_write(struct device *dev, u8 reg,
Michael Büscha7f6e282016-03-04 22:40:55 +0100234 u8 const buf[], size_t len)
235{
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100236 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Dan Carpentera6f26602019-08-17 09:56:04 +0300237 int ret, err;
Michael Büscha7f6e282016-03-04 22:40:55 +0100238 size_t i;
239 u8 tmp;
240
Dan Carpentera6f26602019-08-17 09:56:04 +0300241 err = rv3029_eeprom_enter(dev);
242 if (err < 0)
243 return err;
Michael Büscha7f6e282016-03-04 22:40:55 +0100244
245 for (i = 0; i < len; i++, reg++) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200246 ret = rv3029_read_regs(dev, reg, &tmp, 1);
Michael Büscha7f6e282016-03-04 22:40:55 +0100247 if (ret < 0)
248 break;
249 if (tmp != buf[i]) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200250 ret = rv3029_write_regs(dev, reg, &buf[i], 1);
Michael Büscha7f6e282016-03-04 22:40:55 +0100251 if (ret < 0)
252 break;
253 }
Alexandre Bellonibb72dbb2019-12-14 23:10:12 +0100254 ret = rv3029_eeprom_busywait(rv3029);
Michael Büscha7f6e282016-03-04 22:40:55 +0100255 if (ret < 0)
256 break;
257 }
258
Dan Carpentera6f26602019-08-17 09:56:04 +0300259 err = rv3029_eeprom_exit(dev);
260 if (err < 0)
261 return err;
Michael Büscha7f6e282016-03-04 22:40:55 +0100262
Dan Carpentera6f26602019-08-17 09:56:04 +0300263 return ret;
Michael Büscha7f6e282016-03-04 22:40:55 +0100264}
265
Mylène Josserande6e38082016-05-03 11:54:33 +0200266static int rv3029_eeprom_update_bits(struct device *dev,
Michael Büsch39387dc22016-03-10 18:34:23 +0100267 u8 reg, u8 mask, u8 set)
268{
269 u8 buf;
270 int ret;
271
Mylène Josserande6e38082016-05-03 11:54:33 +0200272 ret = rv3029_eeprom_read(dev, reg, &buf, 1);
Michael Büsch39387dc22016-03-10 18:34:23 +0100273 if (ret < 0)
274 return ret;
275 buf &= ~mask;
276 buf |= set & mask;
Mylène Josserande6e38082016-05-03 11:54:33 +0200277 ret = rv3029_eeprom_write(dev, reg, &buf, 1);
Michael Büsch39387dc22016-03-10 18:34:23 +0100278 if (ret < 0)
279 return ret;
280
281 return 0;
282}
283
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200284static irqreturn_t rv3029_handle_irq(int irq, void *dev_id)
285{
286 struct device *dev = dev_id;
287 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
288 struct mutex *lock = &rv3029->rtc->ops_lock;
289 unsigned long events = 0;
290 u8 flags, controls;
291 int ret;
292
293 mutex_lock(lock);
294
295 ret = rv3029_read_regs(dev, RV3029_IRQ_CTRL, &controls, 1);
296 if (ret) {
297 dev_warn(dev, "Read IRQ Control Register error %d\n", ret);
298 mutex_unlock(lock);
299 return IRQ_NONE;
300 }
301
302 ret = rv3029_read_regs(dev, RV3029_IRQ_FLAGS, &flags, 1);
303 if (ret) {
304 dev_warn(dev, "Read IRQ Flags Register error %d\n", ret);
305 mutex_unlock(lock);
306 return IRQ_NONE;
307 }
308
309 if (flags & RV3029_IRQ_FLAGS_AF) {
310 flags &= ~RV3029_IRQ_FLAGS_AF;
311 controls &= ~RV3029_IRQ_CTRL_AIE;
312 events |= RTC_AF;
313 }
314
315 if (events) {
316 rtc_update_irq(rv3029->rtc, 1, events);
317 rv3029_write_regs(dev, RV3029_IRQ_FLAGS, &flags, 1);
318 rv3029_write_regs(dev, RV3029_IRQ_CTRL, &controls, 1);
319 }
320 mutex_unlock(lock);
321
322 return IRQ_HANDLED;
323}
324
Mylène Josserande6e38082016-05-03 11:54:33 +0200325static int rv3029_read_time(struct device *dev, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700326{
Heiko Schocher52365232011-05-26 16:25:05 -0700327 int ret;
Michael Büschaba39d22016-03-04 22:38:45 +0100328 u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, };
Heiko Schocher52365232011-05-26 16:25:05 -0700329
Mylène Josserande6e38082016-05-03 11:54:33 +0200330 ret = rv3029_read_regs(dev, RV3029_W_SEC, regs,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200331 RV3029_WATCH_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700332 if (ret < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200333 dev_err(dev, "%s: reading RTC section failed\n", __func__);
Heiko Schocher52365232011-05-26 16:25:05 -0700334 return ret;
335 }
336
Mylène Josserandabe2f552016-05-03 11:54:35 +0200337 tm->tm_sec = bcd2bin(regs[RV3029_W_SEC - RV3029_W_SEC]);
338 tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES - RV3029_W_SEC]);
Heiko Schocher52365232011-05-26 16:25:05 -0700339
340 /* HR field has a more complex interpretation */
341 {
Mylène Josserandabe2f552016-05-03 11:54:35 +0200342 const u8 _hr = regs[RV3029_W_HOURS - RV3029_W_SEC];
Michael Büschaba39d22016-03-04 22:38:45 +0100343
344 if (_hr & RV3029_REG_HR_12_24) {
Heiko Schocher52365232011-05-26 16:25:05 -0700345 /* 12h format */
346 tm->tm_hour = bcd2bin(_hr & 0x1f);
Michael Büschaba39d22016-03-04 22:38:45 +0100347 if (_hr & RV3029_REG_HR_PM) /* PM flag set */
Heiko Schocher52365232011-05-26 16:25:05 -0700348 tm->tm_hour += 12;
349 } else /* 24h format */
350 tm->tm_hour = bcd2bin(_hr & 0x3f);
351 }
352
Mylène Josserandabe2f552016-05-03 11:54:35 +0200353 tm->tm_mday = bcd2bin(regs[RV3029_W_DATE - RV3029_W_SEC]);
354 tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS - RV3029_W_SEC]) - 1;
355 tm->tm_year = bcd2bin(regs[RV3029_W_YEARS - RV3029_W_SEC]) + 100;
356 tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS - RV3029_W_SEC]) - 1;
Heiko Schocher52365232011-05-26 16:25:05 -0700357
358 return 0;
359}
360
Mylène Josserande6e38082016-05-03 11:54:33 +0200361static int rv3029_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700362{
Heiko Schocher52365232011-05-26 16:25:05 -0700363 struct rtc_time *const tm = &alarm->time;
364 int ret;
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200365 u8 regs[8], controls, flags;
Heiko Schocher52365232011-05-26 16:25:05 -0700366
Mylène Josserande6e38082016-05-03 11:54:33 +0200367 ret = rv3029_read_regs(dev, RV3029_A_SC, regs,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200368 RV3029_ALARM_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700369
370 if (ret < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200371 dev_err(dev, "%s: reading alarm section failed\n", __func__);
Heiko Schocher52365232011-05-26 16:25:05 -0700372 return ret;
373 }
374
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200375 ret = rv3029_read_regs(dev, RV3029_IRQ_CTRL, &controls, 1);
376 if (ret) {
377 dev_err(dev, "Read IRQ Control Register error %d\n", ret);
378 return ret;
379 }
380 ret = rv3029_read_regs(dev, RV3029_IRQ_FLAGS, &flags, 1);
381 if (ret < 0) {
382 dev_err(dev, "Read IRQ Flags Register error %d\n", ret);
383 return ret;
384 }
385
Mylène Josserandabe2f552016-05-03 11:54:35 +0200386 tm->tm_sec = bcd2bin(regs[RV3029_A_SC - RV3029_A_SC] & 0x7f);
387 tm->tm_min = bcd2bin(regs[RV3029_A_MN - RV3029_A_SC] & 0x7f);
388 tm->tm_hour = bcd2bin(regs[RV3029_A_HR - RV3029_A_SC] & 0x3f);
389 tm->tm_mday = bcd2bin(regs[RV3029_A_DT - RV3029_A_SC] & 0x3f);
390 tm->tm_mon = bcd2bin(regs[RV3029_A_MO - RV3029_A_SC] & 0x1f) - 1;
391 tm->tm_year = bcd2bin(regs[RV3029_A_YR - RV3029_A_SC] & 0x7f) + 100;
392 tm->tm_wday = bcd2bin(regs[RV3029_A_DW - RV3029_A_SC] & 0x07) - 1;
Heiko Schocher52365232011-05-26 16:25:05 -0700393
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200394 alarm->enabled = !!(controls & RV3029_IRQ_CTRL_AIE);
395 alarm->pending = (flags & RV3029_IRQ_FLAGS_AF) && alarm->enabled;
396
Heiko Schocher52365232011-05-26 16:25:05 -0700397 return 0;
398}
399
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200400static int rv3029_alarm_irq_enable(struct device *dev, unsigned int enable)
Heiko Schocher52365232011-05-26 16:25:05 -0700401{
Alexandre Belloni38ce8e32019-12-14 23:10:13 +0100402 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200403
Alexandre Belloni38ce8e32019-12-14 23:10:13 +0100404 return regmap_update_bits(rv3029->regmap, RV3029_IRQ_CTRL,
405 RV3029_IRQ_CTRL_AIE,
406 enable ? RV3029_IRQ_CTRL_AIE : 0);
Heiko Schocher52365232011-05-26 16:25:05 -0700407}
408
Mylène Josserande6e38082016-05-03 11:54:33 +0200409static int rv3029_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700410{
411 struct rtc_time *const tm = &alarm->time;
412 int ret;
413 u8 regs[8];
414
415 /*
416 * The clock has an 8 bit wide bcd-coded register (they never learn)
417 * for the year. tm_year is an offset from 1900 and we are interested
418 * in the 2000-2099 range, so any value less than 100 is invalid.
419 */
420 if (tm->tm_year < 100)
421 return -EINVAL;
422
Mylène Josseranddc492e82016-05-03 11:54:36 +0200423 /* Activate all the alarms with AE_x bit */
424 regs[RV3029_A_SC - RV3029_A_SC] = bin2bcd(tm->tm_sec) | RV3029_A_AE_X;
425 regs[RV3029_A_MN - RV3029_A_SC] = bin2bcd(tm->tm_min) | RV3029_A_AE_X;
426 regs[RV3029_A_HR - RV3029_A_SC] = (bin2bcd(tm->tm_hour) & 0x3f)
427 | RV3029_A_AE_X;
428 regs[RV3029_A_DT - RV3029_A_SC] = (bin2bcd(tm->tm_mday) & 0x3f)
429 | RV3029_A_AE_X;
430 regs[RV3029_A_MO - RV3029_A_SC] = (bin2bcd(tm->tm_mon + 1) & 0x1f)
431 | RV3029_A_AE_X;
432 regs[RV3029_A_DW - RV3029_A_SC] = (bin2bcd(tm->tm_wday + 1) & 0x7)
433 | RV3029_A_AE_X;
434 regs[RV3029_A_YR - RV3029_A_SC] = (bin2bcd(tm->tm_year - 100))
435 | RV3029_A_AE_X;
436
437 /* Write the alarm */
Mylène Josserande6e38082016-05-03 11:54:33 +0200438 ret = rv3029_write_regs(dev, RV3029_A_SC, regs,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200439 RV3029_ALARM_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700440 if (ret < 0)
441 return ret;
442
Alexandre Belloni8fd3d602019-12-14 23:10:14 +0100443 return rv3029_alarm_irq_enable(dev, alarm->enabled);
Heiko Schocher52365232011-05-26 16:25:05 -0700444}
445
Mylène Josserande6e38082016-05-03 11:54:33 +0200446static int rv3029_set_time(struct device *dev, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700447{
Alexandre Belloni54c59702019-12-14 23:10:10 +0100448 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Heiko Schocher52365232011-05-26 16:25:05 -0700449 u8 regs[8];
450 int ret;
451
452 /*
453 * The clock has an 8 bit wide bcd-coded register (they never learn)
454 * for the year. tm_year is an offset from 1900 and we are interested
455 * in the 2000-2099 range, so any value less than 100 is invalid.
456 */
457 if (tm->tm_year < 100)
458 return -EINVAL;
459
Mylène Josserandabe2f552016-05-03 11:54:35 +0200460 regs[RV3029_W_SEC - RV3029_W_SEC] = bin2bcd(tm->tm_sec);
461 regs[RV3029_W_MINUTES - RV3029_W_SEC] = bin2bcd(tm->tm_min);
462 regs[RV3029_W_HOURS - RV3029_W_SEC] = bin2bcd(tm->tm_hour);
463 regs[RV3029_W_DATE - RV3029_W_SEC] = bin2bcd(tm->tm_mday);
464 regs[RV3029_W_MONTHS - RV3029_W_SEC] = bin2bcd(tm->tm_mon + 1);
Mylène Josserand38201ca2016-05-03 11:54:37 +0200465 regs[RV3029_W_DAYS - RV3029_W_SEC] = bin2bcd(tm->tm_wday + 1) & 0x7;
Mylène Josserandabe2f552016-05-03 11:54:35 +0200466 regs[RV3029_W_YEARS - RV3029_W_SEC] = bin2bcd(tm->tm_year - 100);
Heiko Schocher52365232011-05-26 16:25:05 -0700467
Mylène Josserande6e38082016-05-03 11:54:33 +0200468 ret = rv3029_write_regs(dev, RV3029_W_SEC, regs,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200469 RV3029_WATCH_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700470 if (ret < 0)
471 return ret;
472
Heiko Schocher52365232011-05-26 16:25:05 -0700473 /* clear PON bit */
Alexandre Belloni54c59702019-12-14 23:10:10 +0100474 return regmap_update_bits(rv3029->regmap, RV3029_STATUS,
475 RV3029_STATUS_PON, 0);
Heiko Schocher52365232011-05-26 16:25:05 -0700476}
Mylène Josserandabe2f552016-05-03 11:54:35 +0200477
Michael Büsche27e2162016-03-04 22:41:19 +0100478static const struct rv3029_trickle_tab_elem {
479 u32 r; /* resistance in ohms */
480 u8 conf; /* trickle config bits */
481} rv3029_trickle_tab[] = {
482 {
483 .r = 1076,
484 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
485 RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
486 }, {
487 .r = 1091,
488 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
489 RV3029_TRICKLE_20K,
490 }, {
491 .r = 1137,
492 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
493 RV3029_TRICKLE_80K,
494 }, {
495 .r = 1154,
496 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
497 }, {
498 .r = 1371,
499 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
500 RV3029_TRICKLE_80K,
501 }, {
502 .r = 1395,
503 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
504 }, {
505 .r = 1472,
506 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
507 }, {
508 .r = 1500,
509 .conf = RV3029_TRICKLE_1K,
510 }, {
511 .r = 3810,
512 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
513 RV3029_TRICKLE_80K,
514 }, {
515 .r = 4000,
516 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
517 }, {
518 .r = 4706,
519 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
520 }, {
521 .r = 5000,
522 .conf = RV3029_TRICKLE_5K,
523 }, {
524 .r = 16000,
525 .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
526 }, {
527 .r = 20000,
528 .conf = RV3029_TRICKLE_20K,
529 }, {
530 .r = 80000,
531 .conf = RV3029_TRICKLE_80K,
532 },
533};
534
Mylène Josserande6e38082016-05-03 11:54:33 +0200535static void rv3029_trickle_config(struct device *dev)
Michael Büsche27e2162016-03-04 22:41:19 +0100536{
Mylène Josserande6e38082016-05-03 11:54:33 +0200537 struct device_node *of_node = dev->of_node;
Michael Büsche27e2162016-03-04 22:41:19 +0100538 const struct rv3029_trickle_tab_elem *elem;
539 int i, err;
540 u32 ohms;
Michael Büsch39387dc22016-03-10 18:34:23 +0100541 u8 trickle_set_bits;
Michael Büsche27e2162016-03-04 22:41:19 +0100542
543 if (!of_node)
544 return;
545
546 /* Configure the trickle charger. */
Michael Büsche27e2162016-03-04 22:41:19 +0100547 err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms);
548 if (err) {
549 /* Disable trickle charger. */
Michael Büsch39387dc22016-03-10 18:34:23 +0100550 trickle_set_bits = 0;
Michael Büsche27e2162016-03-04 22:41:19 +0100551 } else {
552 /* Enable trickle charger. */
553 for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) {
554 elem = &rv3029_trickle_tab[i];
555 if (elem->r >= ohms)
556 break;
557 }
Michael Büsch39387dc22016-03-10 18:34:23 +0100558 trickle_set_bits = elem->conf;
Mylène Josserande6e38082016-05-03 11:54:33 +0200559 dev_info(dev,
Michael Büsche27e2162016-03-04 22:41:19 +0100560 "Trickle charger enabled at %d ohms resistance.\n",
561 elem->r);
562 }
Mylène Josserande6e38082016-05-03 11:54:33 +0200563 err = rv3029_eeprom_update_bits(dev, RV3029_CONTROL_E2P_EECTRL,
Michael Büsch39387dc22016-03-10 18:34:23 +0100564 RV3029_TRICKLE_MASK,
565 trickle_set_bits);
Mylène Josserandabe2f552016-05-03 11:54:35 +0200566 if (err < 0)
Mylène Josserande6e38082016-05-03 11:54:33 +0200567 dev_err(dev, "Failed to update trickle charger config\n");
Michael Büsche27e2162016-03-04 22:41:19 +0100568}
569
Michael Büscha696b312016-03-10 18:34:46 +0100570#ifdef CONFIG_RTC_DRV_RV3029_HWMON
571
Mylène Josserande6e38082016-05-03 11:54:33 +0200572static int rv3029_read_temp(struct device *dev, int *temp_mC)
Michael Büscha696b312016-03-10 18:34:46 +0100573{
574 int ret;
575 u8 temp;
576
Mylène Josserande6e38082016-05-03 11:54:33 +0200577 ret = rv3029_read_regs(dev, RV3029_TEMP_PAGE, &temp, 1);
Michael Büscha696b312016-03-10 18:34:46 +0100578 if (ret < 0)
579 return ret;
580
581 *temp_mC = ((int)temp - 60) * 1000;
582
583 return 0;
584}
585
586static ssize_t rv3029_hwmon_show_temp(struct device *dev,
587 struct device_attribute *attr,
588 char *buf)
589{
Michael Büscha696b312016-03-10 18:34:46 +0100590 int ret, temp_mC;
591
Mylène Josserande6e38082016-05-03 11:54:33 +0200592 ret = rv3029_read_temp(dev, &temp_mC);
Michael Büscha696b312016-03-10 18:34:46 +0100593 if (ret < 0)
594 return ret;
595
596 return sprintf(buf, "%d\n", temp_mC);
597}
598
599static ssize_t rv3029_hwmon_set_update_interval(struct device *dev,
600 struct device_attribute *attr,
601 const char *buf,
602 size_t count)
603{
Michael Büscha696b312016-03-10 18:34:46 +0100604 unsigned long interval_ms;
605 int ret;
606 u8 th_set_bits = 0;
607
608 ret = kstrtoul(buf, 10, &interval_ms);
609 if (ret < 0)
610 return ret;
611
612 if (interval_ms != 0) {
613 th_set_bits |= RV3029_EECTRL_THE;
614 if (interval_ms >= 16000)
615 th_set_bits |= RV3029_EECTRL_THP;
616 }
Mylène Josserande6e38082016-05-03 11:54:33 +0200617 ret = rv3029_eeprom_update_bits(dev, RV3029_CONTROL_E2P_EECTRL,
Michael Büscha696b312016-03-10 18:34:46 +0100618 RV3029_EECTRL_THE | RV3029_EECTRL_THP,
619 th_set_bits);
620 if (ret < 0)
621 return ret;
622
623 return count;
624}
625
626static ssize_t rv3029_hwmon_show_update_interval(struct device *dev,
627 struct device_attribute *attr,
628 char *buf)
629{
Michael Büscha696b312016-03-10 18:34:46 +0100630 int ret, interval_ms;
631 u8 eectrl;
632
Mylène Josserande6e38082016-05-03 11:54:33 +0200633 ret = rv3029_eeprom_read(dev, RV3029_CONTROL_E2P_EECTRL,
Michael Büscha696b312016-03-10 18:34:46 +0100634 &eectrl, 1);
635 if (ret < 0)
636 return ret;
637
638 if (eectrl & RV3029_EECTRL_THE) {
639 if (eectrl & RV3029_EECTRL_THP)
640 interval_ms = 16000;
641 else
642 interval_ms = 1000;
643 } else {
644 interval_ms = 0;
645 }
646
647 return sprintf(buf, "%d\n", interval_ms);
648}
649
650static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, rv3029_hwmon_show_temp,
651 NULL, 0);
652static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,
653 rv3029_hwmon_show_update_interval,
654 rv3029_hwmon_set_update_interval, 0);
655
656static struct attribute *rv3029_hwmon_attrs[] = {
657 &sensor_dev_attr_temp1_input.dev_attr.attr,
658 &sensor_dev_attr_update_interval.dev_attr.attr,
659 NULL,
660};
661ATTRIBUTE_GROUPS(rv3029_hwmon);
662
Mylène Josserande6e38082016-05-03 11:54:33 +0200663static void rv3029_hwmon_register(struct device *dev, const char *name)
Michael Büscha696b312016-03-10 18:34:46 +0100664{
Mylène Josserande6e38082016-05-03 11:54:33 +0200665 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Michael Büscha696b312016-03-10 18:34:46 +0100666 struct device *hwmon_dev;
667
Mylène Josserande6e38082016-05-03 11:54:33 +0200668 hwmon_dev = devm_hwmon_device_register_with_groups(dev, name, rv3029,
669 rv3029_hwmon_groups);
Michael Büscha696b312016-03-10 18:34:46 +0100670 if (IS_ERR(hwmon_dev)) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200671 dev_warn(dev, "unable to register hwmon device %ld\n",
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200672 PTR_ERR(hwmon_dev));
Michael Büscha696b312016-03-10 18:34:46 +0100673 }
674}
675
676#else /* CONFIG_RTC_DRV_RV3029_HWMON */
677
Mylène Josserande6e38082016-05-03 11:54:33 +0200678static void rv3029_hwmon_register(struct device *dev, const char *name)
Michael Büscha696b312016-03-10 18:34:46 +0100679{
680}
681
682#endif /* CONFIG_RTC_DRV_RV3029_HWMON */
683
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200684static struct rtc_class_ops rv3029_rtc_ops = {
Mylène Josserande6e38082016-05-03 11:54:33 +0200685 .read_time = rv3029_read_time,
686 .set_time = rv3029_set_time,
Heiko Schocher52365232011-05-26 16:25:05 -0700687};
688
Mylène Josserande6e38082016-05-03 11:54:33 +0200689static int rv3029_probe(struct device *dev, struct regmap *regmap, int irq,
690 const char *name)
Heiko Schocher52365232011-05-26 16:25:05 -0700691{
Mylène Josserande6e38082016-05-03 11:54:33 +0200692 struct rv3029_data *rv3029;
Heiko Schocher52365232011-05-26 16:25:05 -0700693 int rc = 0;
Heiko Schocher52365232011-05-26 16:25:05 -0700694
Mylène Josserande6e38082016-05-03 11:54:33 +0200695 rv3029 = devm_kzalloc(dev, sizeof(*rv3029), GFP_KERNEL);
696 if (!rv3029)
697 return -ENOMEM;
Heiko Schocher52365232011-05-26 16:25:05 -0700698
Mylène Josserande6e38082016-05-03 11:54:33 +0200699 rv3029->regmap = regmap;
700 rv3029->irq = irq;
701 rv3029->dev = dev;
702 dev_set_drvdata(dev, rv3029);
703
Mylène Josserande6e38082016-05-03 11:54:33 +0200704 rv3029_trickle_config(dev);
705 rv3029_hwmon_register(dev, name);
Michael Büsche27e2162016-03-04 22:41:19 +0100706
Mylène Josserande6e38082016-05-03 11:54:33 +0200707 rv3029->rtc = devm_rtc_device_register(dev, name, &rv3029_rtc_ops,
708 THIS_MODULE);
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200709 if (IS_ERR(rv3029->rtc)) {
710 dev_err(dev, "unable to register the class device\n");
711 return PTR_ERR(rv3029->rtc);
712 }
Heiko Schocher52365232011-05-26 16:25:05 -0700713
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200714 if (rv3029->irq > 0) {
715 rc = devm_request_threaded_irq(dev, rv3029->irq,
716 NULL, rv3029_handle_irq,
717 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
718 "rv3029", dev);
719 if (rc) {
720 dev_warn(dev, "unable to request IRQ, alarms disabled\n");
721 rv3029->irq = 0;
722 } else {
723 rv3029_rtc_ops.read_alarm = rv3029_read_alarm;
724 rv3029_rtc_ops.set_alarm = rv3029_set_alarm;
725 rv3029_rtc_ops.alarm_irq_enable = rv3029_alarm_irq_enable;
726 }
727 }
728
729 return 0;
Mylène Josserande6e38082016-05-03 11:54:33 +0200730}
Heiko Schocher52365232011-05-26 16:25:05 -0700731
Alexandre Bellonic509e432019-12-14 23:10:08 +0100732static const struct regmap_range rv3029_holes_range[] = {
733 regmap_reg_range(0x05, 0x07),
734 regmap_reg_range(0x0f, 0x0f),
735 regmap_reg_range(0x17, 0x17),
736 regmap_reg_range(0x1a, 0x1f),
737 regmap_reg_range(0x21, 0x27),
738 regmap_reg_range(0x34, 0x37),
739};
740
741static const struct regmap_access_table rv3029_regs = {
742 .no_ranges = rv3029_holes_range,
743 .n_no_ranges = ARRAY_SIZE(rv3029_holes_range),
744};
745
746static const struct regmap_config config = {
747 .reg_bits = 8,
748 .val_bits = 8,
749 .rd_table = &rv3029_regs,
750 .wr_table = &rv3029_regs,
751 .max_register = 0x3f,
752};
753
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200754#if IS_ENABLED(CONFIG_I2C)
755
Mylène Josserande6e38082016-05-03 11:54:33 +0200756static int rv3029_i2c_probe(struct i2c_client *client,
757 const struct i2c_device_id *id)
758{
759 struct regmap *regmap;
Mylène Josserande6e38082016-05-03 11:54:33 +0200760 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK |
761 I2C_FUNC_SMBUS_BYTE)) {
762 dev_err(&client->dev, "Adapter does not support SMBUS_I2C_BLOCK or SMBUS_I2C_BYTE\n");
763 return -ENODEV;
764 }
765
766 regmap = devm_regmap_init_i2c(client, &config);
767 if (IS_ERR(regmap)) {
768 dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
769 __func__, PTR_ERR(regmap));
770 return PTR_ERR(regmap);
771 }
772
773 return rv3029_probe(&client->dev, regmap, client->irq, client->name);
Heiko Schocher52365232011-05-26 16:25:05 -0700774}
775
Arvind Yadav45a63512017-08-20 00:37:55 +0530776static const struct i2c_device_id rv3029_id[] = {
Arnd Bergmann814db2b2016-05-04 11:50:02 +0200777 { "rv3029", 0 },
778 { "rv3029c2", 0 },
779 { }
780};
781MODULE_DEVICE_TABLE(i2c, rv3029_id);
782
Javier Martinez Canillase696a1d2017-03-03 11:29:13 -0300783static const struct of_device_id rv3029_of_match[] = {
Alexandre Belloni45b611c2017-09-15 04:00:02 +0200784 { .compatible = "microcrystal,rv3029" },
785 /* Backward compatibility only, do not use compatibles below: */
Javier Martinez Canillase696a1d2017-03-03 11:29:13 -0300786 { .compatible = "rv3029" },
787 { .compatible = "rv3029c2" },
788 { .compatible = "mc,rv3029c2" },
789 { }
790};
791MODULE_DEVICE_TABLE(of, rv3029_of_match);
792
Michael Büschaba39d22016-03-04 22:38:45 +0100793static struct i2c_driver rv3029_driver = {
Heiko Schocher52365232011-05-26 16:25:05 -0700794 .driver = {
Alexandre Belloni9b45ef92019-12-14 23:10:07 +0100795 .name = "rv3029",
Javier Martinez Canillase696a1d2017-03-03 11:29:13 -0300796 .of_match_table = of_match_ptr(rv3029_of_match),
Heiko Schocher52365232011-05-26 16:25:05 -0700797 },
Mylène Josserande6e38082016-05-03 11:54:33 +0200798 .probe = rv3029_i2c_probe,
Michael Büschaba39d22016-03-04 22:38:45 +0100799 .id_table = rv3029_id,
Heiko Schocher52365232011-05-26 16:25:05 -0700800};
801
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200802static int rv3029_register_driver(void)
803{
804 return i2c_add_driver(&rv3029_driver);
805}
806
807static void rv3029_unregister_driver(void)
808{
809 i2c_del_driver(&rv3029_driver);
810}
811
812#else
813
814static int rv3029_register_driver(void)
815{
816 return 0;
817}
818
819static void rv3029_unregister_driver(void)
820{
821}
822
823#endif
824
825#if IS_ENABLED(CONFIG_SPI_MASTER)
826
827static int rv3049_probe(struct spi_device *spi)
828{
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200829 struct regmap *regmap;
830
831 regmap = devm_regmap_init_spi(spi, &config);
832 if (IS_ERR(regmap)) {
833 dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
834 __func__, PTR_ERR(regmap));
835 return PTR_ERR(regmap);
836 }
837
838 return rv3029_probe(&spi->dev, regmap, spi->irq, "rv3049");
839}
840
841static struct spi_driver rv3049_driver = {
842 .driver = {
843 .name = "rv3049",
844 },
845 .probe = rv3049_probe,
846};
847
848static int rv3049_register_driver(void)
849{
850 return spi_register_driver(&rv3049_driver);
851}
852
853static void rv3049_unregister_driver(void)
854{
855 spi_unregister_driver(&rv3049_driver);
856}
857
858#else
859
860static int rv3049_register_driver(void)
861{
862 return 0;
863}
864
865static void rv3049_unregister_driver(void)
866{
867}
868
869#endif
870
871static int __init rv30x9_init(void)
872{
873 int ret;
874
875 ret = rv3029_register_driver();
876 if (ret) {
877 pr_err("Failed to register rv3029 driver: %d\n", ret);
878 return ret;
879 }
880
881 ret = rv3049_register_driver();
882 if (ret) {
883 pr_err("Failed to register rv3049 driver: %d\n", ret);
884 rv3029_unregister_driver();
885 }
886
887 return ret;
888}
889module_init(rv30x9_init)
890
891static void __exit rv30x9_exit(void)
892{
893 rv3049_unregister_driver();
894 rv3029_unregister_driver();
895}
896module_exit(rv30x9_exit)
Heiko Schocher52365232011-05-26 16:25:05 -0700897
898MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
Michael Büsch2dca3d92016-03-04 22:40:30 +0100899MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200900MODULE_DESCRIPTION("Micro Crystal RV3029/RV3049 RTC driver");
Heiko Schocher52365232011-05-26 16:25:05 -0700901MODULE_LICENSE("GPL");
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200902MODULE_ALIAS("spi:rv3049");