blob: 9a0397f3dee9a4fe564de3a4917ad5aca676acb4 [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
Mylène Josserande6e38082016-05-03 11:54:33 +0200140static int rv3029_get_sr(struct device *dev, u8 *buf)
Heiko Schocher52365232011-05-26 16:25:05 -0700141{
Mylène Josserande6e38082016-05-03 11:54:33 +0200142 int ret = rv3029_read_regs(dev, RV3029_STATUS, buf, 1);
Heiko Schocher52365232011-05-26 16:25:05 -0700143
144 if (ret < 0)
145 return -EIO;
Mylène Josserande6e38082016-05-03 11:54:33 +0200146 dev_dbg(dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
Heiko Schocher52365232011-05-26 16:25:05 -0700147 return 0;
148}
149
Mylène Josserande6e38082016-05-03 11:54:33 +0200150static int rv3029_set_sr(struct device *dev, u8 val)
Heiko Schocher52365232011-05-26 16:25:05 -0700151{
152 u8 buf[1];
153 int sr;
154
155 buf[0] = val;
Mylène Josserande6e38082016-05-03 11:54:33 +0200156 sr = rv3029_write_regs(dev, RV3029_STATUS, buf, 1);
157 dev_dbg(dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
Heiko Schocher52365232011-05-26 16:25:05 -0700158 if (sr < 0)
159 return -EIO;
160 return 0;
161}
162
Mylène Josserande6e38082016-05-03 11:54:33 +0200163static int rv3029_eeprom_busywait(struct device *dev)
Michael Büscha7f6e282016-03-04 22:40:55 +0100164{
165 int i, ret;
166 u8 sr;
167
168 for (i = 100; i > 0; i--) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200169 ret = rv3029_get_sr(dev, &sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100170 if (ret < 0)
171 break;
172 if (!(sr & RV3029_STATUS_EEBUSY))
173 break;
174 usleep_range(1000, 10000);
175 }
176 if (i <= 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200177 dev_err(dev, "EEPROM busy wait timeout.\n");
Michael Büscha7f6e282016-03-04 22:40:55 +0100178 return -ETIMEDOUT;
179 }
180
181 return ret;
182}
183
Mylène Josserande6e38082016-05-03 11:54:33 +0200184static int rv3029_eeprom_exit(struct device *dev)
Michael Büscha7f6e282016-03-04 22:40:55 +0100185{
Alexandre Belloni609e97f2019-12-14 23:10:09 +0100186 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
187
Michael Büscha7f6e282016-03-04 22:40:55 +0100188 /* Re-enable eeprom refresh */
Alexandre Belloni609e97f2019-12-14 23:10:09 +0100189 return regmap_update_bits(rv3029->regmap, RV3029_ONOFF_CTRL,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200190 RV3029_ONOFF_CTRL_EERE,
191 RV3029_ONOFF_CTRL_EERE);
Michael Büscha7f6e282016-03-04 22:40:55 +0100192}
193
Mylène Josserande6e38082016-05-03 11:54:33 +0200194static int rv3029_eeprom_enter(struct device *dev)
Michael Büscha7f6e282016-03-04 22:40:55 +0100195{
Alexandre Belloni609e97f2019-12-14 23:10:09 +0100196 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Michael Büscha7f6e282016-03-04 22:40:55 +0100197 int ret;
198 u8 sr;
199
200 /* Check whether we are in the allowed voltage range. */
Mylène Josserande6e38082016-05-03 11:54:33 +0200201 ret = rv3029_get_sr(dev, &sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100202 if (ret < 0)
203 return ret;
204 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
205 /* We clear the bits and retry once just in case
206 * we had a brown out in early startup.
207 */
208 sr &= ~RV3029_STATUS_VLOW1;
209 sr &= ~RV3029_STATUS_VLOW2;
Mylène Josserande6e38082016-05-03 11:54:33 +0200210 ret = rv3029_set_sr(dev, sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100211 if (ret < 0)
212 return ret;
213 usleep_range(1000, 10000);
Mylène Josserande6e38082016-05-03 11:54:33 +0200214 ret = rv3029_get_sr(dev, &sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100215 if (ret < 0)
216 return ret;
217 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200218 dev_err(dev,
Michael Büscha7f6e282016-03-04 22:40:55 +0100219 "Supply voltage is too low to safely access the EEPROM.\n");
220 return -ENODEV;
221 }
222 }
223
224 /* Disable eeprom refresh. */
Alexandre Belloni609e97f2019-12-14 23:10:09 +0100225 ret = regmap_update_bits(rv3029->regmap, RV3029_ONOFF_CTRL,
226 RV3029_ONOFF_CTRL_EERE, 0);
Michael Büscha7f6e282016-03-04 22:40:55 +0100227 if (ret < 0)
228 return ret;
229
230 /* Wait for any previous eeprom accesses to finish. */
Mylène Josserande6e38082016-05-03 11:54:33 +0200231 ret = rv3029_eeprom_busywait(dev);
Michael Büscha7f6e282016-03-04 22:40:55 +0100232 if (ret < 0)
Mylène Josserande6e38082016-05-03 11:54:33 +0200233 rv3029_eeprom_exit(dev);
Michael Büscha7f6e282016-03-04 22:40:55 +0100234
235 return ret;
236}
237
Mylène Josserande6e38082016-05-03 11:54:33 +0200238static int rv3029_eeprom_read(struct device *dev, u8 reg,
Michael Büscha7f6e282016-03-04 22:40:55 +0100239 u8 buf[], size_t len)
240{
241 int ret, err;
242
Mylène Josserande6e38082016-05-03 11:54:33 +0200243 err = rv3029_eeprom_enter(dev);
Michael Büscha7f6e282016-03-04 22:40:55 +0100244 if (err < 0)
245 return err;
246
Mylène Josserande6e38082016-05-03 11:54:33 +0200247 ret = rv3029_read_regs(dev, reg, buf, len);
Michael Büscha7f6e282016-03-04 22:40:55 +0100248
Mylène Josserande6e38082016-05-03 11:54:33 +0200249 err = rv3029_eeprom_exit(dev);
Michael Büscha7f6e282016-03-04 22:40:55 +0100250 if (err < 0)
251 return err;
252
253 return ret;
254}
255
Mylène Josserande6e38082016-05-03 11:54:33 +0200256static int rv3029_eeprom_write(struct device *dev, u8 reg,
Michael Büscha7f6e282016-03-04 22:40:55 +0100257 u8 const buf[], size_t len)
258{
Dan Carpentera6f26602019-08-17 09:56:04 +0300259 int ret, err;
Michael Büscha7f6e282016-03-04 22:40:55 +0100260 size_t i;
261 u8 tmp;
262
Dan Carpentera6f26602019-08-17 09:56:04 +0300263 err = rv3029_eeprom_enter(dev);
264 if (err < 0)
265 return err;
Michael Büscha7f6e282016-03-04 22:40:55 +0100266
267 for (i = 0; i < len; i++, reg++) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200268 ret = rv3029_read_regs(dev, reg, &tmp, 1);
Michael Büscha7f6e282016-03-04 22:40:55 +0100269 if (ret < 0)
270 break;
271 if (tmp != buf[i]) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200272 ret = rv3029_write_regs(dev, reg, &buf[i], 1);
Michael Büscha7f6e282016-03-04 22:40:55 +0100273 if (ret < 0)
274 break;
275 }
Mylène Josserande6e38082016-05-03 11:54:33 +0200276 ret = rv3029_eeprom_busywait(dev);
Michael Büscha7f6e282016-03-04 22:40:55 +0100277 if (ret < 0)
278 break;
279 }
280
Dan Carpentera6f26602019-08-17 09:56:04 +0300281 err = rv3029_eeprom_exit(dev);
282 if (err < 0)
283 return err;
Michael Büscha7f6e282016-03-04 22:40:55 +0100284
Dan Carpentera6f26602019-08-17 09:56:04 +0300285 return ret;
Michael Büscha7f6e282016-03-04 22:40:55 +0100286}
287
Mylène Josserande6e38082016-05-03 11:54:33 +0200288static int rv3029_eeprom_update_bits(struct device *dev,
Michael Büsch39387dc22016-03-10 18:34:23 +0100289 u8 reg, u8 mask, u8 set)
290{
291 u8 buf;
292 int ret;
293
Mylène Josserande6e38082016-05-03 11:54:33 +0200294 ret = rv3029_eeprom_read(dev, reg, &buf, 1);
Michael Büsch39387dc22016-03-10 18:34:23 +0100295 if (ret < 0)
296 return ret;
297 buf &= ~mask;
298 buf |= set & mask;
Mylène Josserande6e38082016-05-03 11:54:33 +0200299 ret = rv3029_eeprom_write(dev, reg, &buf, 1);
Michael Büsch39387dc22016-03-10 18:34:23 +0100300 if (ret < 0)
301 return ret;
302
303 return 0;
304}
305
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200306static irqreturn_t rv3029_handle_irq(int irq, void *dev_id)
307{
308 struct device *dev = dev_id;
309 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
310 struct mutex *lock = &rv3029->rtc->ops_lock;
311 unsigned long events = 0;
312 u8 flags, controls;
313 int ret;
314
315 mutex_lock(lock);
316
317 ret = rv3029_read_regs(dev, RV3029_IRQ_CTRL, &controls, 1);
318 if (ret) {
319 dev_warn(dev, "Read IRQ Control Register error %d\n", ret);
320 mutex_unlock(lock);
321 return IRQ_NONE;
322 }
323
324 ret = rv3029_read_regs(dev, RV3029_IRQ_FLAGS, &flags, 1);
325 if (ret) {
326 dev_warn(dev, "Read IRQ Flags Register error %d\n", ret);
327 mutex_unlock(lock);
328 return IRQ_NONE;
329 }
330
331 if (flags & RV3029_IRQ_FLAGS_AF) {
332 flags &= ~RV3029_IRQ_FLAGS_AF;
333 controls &= ~RV3029_IRQ_CTRL_AIE;
334 events |= RTC_AF;
335 }
336
337 if (events) {
338 rtc_update_irq(rv3029->rtc, 1, events);
339 rv3029_write_regs(dev, RV3029_IRQ_FLAGS, &flags, 1);
340 rv3029_write_regs(dev, RV3029_IRQ_CTRL, &controls, 1);
341 }
342 mutex_unlock(lock);
343
344 return IRQ_HANDLED;
345}
346
Mylène Josserande6e38082016-05-03 11:54:33 +0200347static int rv3029_read_time(struct device *dev, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700348{
349 u8 buf[1];
350 int ret;
Michael Büschaba39d22016-03-04 22:38:45 +0100351 u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, };
Heiko Schocher52365232011-05-26 16:25:05 -0700352
Mylène Josserande6e38082016-05-03 11:54:33 +0200353 ret = rv3029_get_sr(dev, buf);
Heiko Schocher52365232011-05-26 16:25:05 -0700354 if (ret < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200355 dev_err(dev, "%s: reading SR failed\n", __func__);
Heiko Schocher52365232011-05-26 16:25:05 -0700356 return -EIO;
357 }
358
Mylène Josserande6e38082016-05-03 11:54:33 +0200359 ret = rv3029_read_regs(dev, RV3029_W_SEC, regs,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200360 RV3029_WATCH_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700361 if (ret < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200362 dev_err(dev, "%s: reading RTC section failed\n", __func__);
Heiko Schocher52365232011-05-26 16:25:05 -0700363 return ret;
364 }
365
Mylène Josserandabe2f552016-05-03 11:54:35 +0200366 tm->tm_sec = bcd2bin(regs[RV3029_W_SEC - RV3029_W_SEC]);
367 tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES - RV3029_W_SEC]);
Heiko Schocher52365232011-05-26 16:25:05 -0700368
369 /* HR field has a more complex interpretation */
370 {
Mylène Josserandabe2f552016-05-03 11:54:35 +0200371 const u8 _hr = regs[RV3029_W_HOURS - RV3029_W_SEC];
Michael Büschaba39d22016-03-04 22:38:45 +0100372
373 if (_hr & RV3029_REG_HR_12_24) {
Heiko Schocher52365232011-05-26 16:25:05 -0700374 /* 12h format */
375 tm->tm_hour = bcd2bin(_hr & 0x1f);
Michael Büschaba39d22016-03-04 22:38:45 +0100376 if (_hr & RV3029_REG_HR_PM) /* PM flag set */
Heiko Schocher52365232011-05-26 16:25:05 -0700377 tm->tm_hour += 12;
378 } else /* 24h format */
379 tm->tm_hour = bcd2bin(_hr & 0x3f);
380 }
381
Mylène Josserandabe2f552016-05-03 11:54:35 +0200382 tm->tm_mday = bcd2bin(regs[RV3029_W_DATE - RV3029_W_SEC]);
383 tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS - RV3029_W_SEC]) - 1;
384 tm->tm_year = bcd2bin(regs[RV3029_W_YEARS - RV3029_W_SEC]) + 100;
385 tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS - RV3029_W_SEC]) - 1;
Heiko Schocher52365232011-05-26 16:25:05 -0700386
387 return 0;
388}
389
Mylène Josserande6e38082016-05-03 11:54:33 +0200390static int rv3029_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700391{
Heiko Schocher52365232011-05-26 16:25:05 -0700392 struct rtc_time *const tm = &alarm->time;
393 int ret;
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200394 u8 regs[8], controls, flags;
Heiko Schocher52365232011-05-26 16:25:05 -0700395
Mylène Josserande6e38082016-05-03 11:54:33 +0200396 ret = rv3029_get_sr(dev, regs);
Heiko Schocher52365232011-05-26 16:25:05 -0700397 if (ret < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200398 dev_err(dev, "%s: reading SR failed\n", __func__);
Heiko Schocher52365232011-05-26 16:25:05 -0700399 return -EIO;
400 }
401
Mylène Josserande6e38082016-05-03 11:54:33 +0200402 ret = rv3029_read_regs(dev, RV3029_A_SC, regs,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200403 RV3029_ALARM_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700404
405 if (ret < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200406 dev_err(dev, "%s: reading alarm section failed\n", __func__);
Heiko Schocher52365232011-05-26 16:25:05 -0700407 return ret;
408 }
409
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200410 ret = rv3029_read_regs(dev, RV3029_IRQ_CTRL, &controls, 1);
411 if (ret) {
412 dev_err(dev, "Read IRQ Control Register error %d\n", ret);
413 return ret;
414 }
415 ret = rv3029_read_regs(dev, RV3029_IRQ_FLAGS, &flags, 1);
416 if (ret < 0) {
417 dev_err(dev, "Read IRQ Flags Register error %d\n", ret);
418 return ret;
419 }
420
Mylène Josserandabe2f552016-05-03 11:54:35 +0200421 tm->tm_sec = bcd2bin(regs[RV3029_A_SC - RV3029_A_SC] & 0x7f);
422 tm->tm_min = bcd2bin(regs[RV3029_A_MN - RV3029_A_SC] & 0x7f);
423 tm->tm_hour = bcd2bin(regs[RV3029_A_HR - RV3029_A_SC] & 0x3f);
424 tm->tm_mday = bcd2bin(regs[RV3029_A_DT - RV3029_A_SC] & 0x3f);
425 tm->tm_mon = bcd2bin(regs[RV3029_A_MO - RV3029_A_SC] & 0x1f) - 1;
426 tm->tm_year = bcd2bin(regs[RV3029_A_YR - RV3029_A_SC] & 0x7f) + 100;
427 tm->tm_wday = bcd2bin(regs[RV3029_A_DW - RV3029_A_SC] & 0x07) - 1;
Heiko Schocher52365232011-05-26 16:25:05 -0700428
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200429 alarm->enabled = !!(controls & RV3029_IRQ_CTRL_AIE);
430 alarm->pending = (flags & RV3029_IRQ_FLAGS_AF) && alarm->enabled;
431
Heiko Schocher52365232011-05-26 16:25:05 -0700432 return 0;
433}
434
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200435static int rv3029_alarm_irq_enable(struct device *dev, unsigned int enable)
Heiko Schocher52365232011-05-26 16:25:05 -0700436{
437 int ret;
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200438 u8 controls;
439
440 ret = rv3029_read_regs(dev, RV3029_IRQ_CTRL, &controls, 1);
441 if (ret < 0) {
442 dev_warn(dev, "Read IRQ Control Register error %d\n", ret);
443 return ret;
444 }
Heiko Schocher52365232011-05-26 16:25:05 -0700445
Michael Büsch2dca3d92016-03-04 22:40:30 +0100446 /* enable/disable AIE irq */
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200447 if (enable)
448 controls |= RV3029_IRQ_CTRL_AIE;
449 else
450 controls &= ~RV3029_IRQ_CTRL_AIE;
451
452 ret = rv3029_write_regs(dev, RV3029_IRQ_CTRL, &controls, 1);
Heiko Schocher52365232011-05-26 16:25:05 -0700453 if (ret < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200454 dev_err(dev, "can't update INT reg\n");
Heiko Schocher52365232011-05-26 16:25:05 -0700455 return ret;
456 }
457
458 return 0;
459}
460
Mylène Josserande6e38082016-05-03 11:54:33 +0200461static int rv3029_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700462{
463 struct rtc_time *const tm = &alarm->time;
464 int ret;
465 u8 regs[8];
466
467 /*
468 * The clock has an 8 bit wide bcd-coded register (they never learn)
469 * for the year. tm_year is an offset from 1900 and we are interested
470 * in the 2000-2099 range, so any value less than 100 is invalid.
471 */
472 if (tm->tm_year < 100)
473 return -EINVAL;
474
Mylène Josserande6e38082016-05-03 11:54:33 +0200475 ret = rv3029_get_sr(dev, regs);
Heiko Schocher52365232011-05-26 16:25:05 -0700476 if (ret < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200477 dev_err(dev, "%s: reading SR failed\n", __func__);
Heiko Schocher52365232011-05-26 16:25:05 -0700478 return -EIO;
479 }
Heiko Schocher52365232011-05-26 16:25:05 -0700480
Mylène Josseranddc492e82016-05-03 11:54:36 +0200481 /* Activate all the alarms with AE_x bit */
482 regs[RV3029_A_SC - RV3029_A_SC] = bin2bcd(tm->tm_sec) | RV3029_A_AE_X;
483 regs[RV3029_A_MN - RV3029_A_SC] = bin2bcd(tm->tm_min) | RV3029_A_AE_X;
484 regs[RV3029_A_HR - RV3029_A_SC] = (bin2bcd(tm->tm_hour) & 0x3f)
485 | RV3029_A_AE_X;
486 regs[RV3029_A_DT - RV3029_A_SC] = (bin2bcd(tm->tm_mday) & 0x3f)
487 | RV3029_A_AE_X;
488 regs[RV3029_A_MO - RV3029_A_SC] = (bin2bcd(tm->tm_mon + 1) & 0x1f)
489 | RV3029_A_AE_X;
490 regs[RV3029_A_DW - RV3029_A_SC] = (bin2bcd(tm->tm_wday + 1) & 0x7)
491 | RV3029_A_AE_X;
492 regs[RV3029_A_YR - RV3029_A_SC] = (bin2bcd(tm->tm_year - 100))
493 | RV3029_A_AE_X;
494
495 /* Write the alarm */
Mylène Josserande6e38082016-05-03 11:54:33 +0200496 ret = rv3029_write_regs(dev, RV3029_A_SC, regs,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200497 RV3029_ALARM_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700498 if (ret < 0)
499 return ret;
500
501 if (alarm->enabled) {
Heiko Schocher52365232011-05-26 16:25:05 -0700502 /* enable AIE irq */
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200503 ret = rv3029_alarm_irq_enable(dev, 1);
Heiko Schocher52365232011-05-26 16:25:05 -0700504 if (ret)
505 return ret;
Heiko Schocher52365232011-05-26 16:25:05 -0700506 } else {
507 /* disable AIE irq */
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200508 ret = rv3029_alarm_irq_enable(dev, 0);
Heiko Schocher52365232011-05-26 16:25:05 -0700509 if (ret)
510 return ret;
Heiko Schocher52365232011-05-26 16:25:05 -0700511 }
512
513 return 0;
514}
515
Mylène Josserande6e38082016-05-03 11:54:33 +0200516static int rv3029_set_time(struct device *dev, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700517{
518 u8 regs[8];
519 int ret;
520
521 /*
522 * The clock has an 8 bit wide bcd-coded register (they never learn)
523 * for the year. tm_year is an offset from 1900 and we are interested
524 * in the 2000-2099 range, so any value less than 100 is invalid.
525 */
526 if (tm->tm_year < 100)
527 return -EINVAL;
528
Mylène Josserandabe2f552016-05-03 11:54:35 +0200529 regs[RV3029_W_SEC - RV3029_W_SEC] = bin2bcd(tm->tm_sec);
530 regs[RV3029_W_MINUTES - RV3029_W_SEC] = bin2bcd(tm->tm_min);
531 regs[RV3029_W_HOURS - RV3029_W_SEC] = bin2bcd(tm->tm_hour);
532 regs[RV3029_W_DATE - RV3029_W_SEC] = bin2bcd(tm->tm_mday);
533 regs[RV3029_W_MONTHS - RV3029_W_SEC] = bin2bcd(tm->tm_mon + 1);
Mylène Josserand38201ca2016-05-03 11:54:37 +0200534 regs[RV3029_W_DAYS - RV3029_W_SEC] = bin2bcd(tm->tm_wday + 1) & 0x7;
Mylène Josserandabe2f552016-05-03 11:54:35 +0200535 regs[RV3029_W_YEARS - RV3029_W_SEC] = bin2bcd(tm->tm_year - 100);
Heiko Schocher52365232011-05-26 16:25:05 -0700536
Mylène Josserande6e38082016-05-03 11:54:33 +0200537 ret = rv3029_write_regs(dev, RV3029_W_SEC, regs,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200538 RV3029_WATCH_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700539 if (ret < 0)
540 return ret;
541
Mylène Josserande6e38082016-05-03 11:54:33 +0200542 ret = rv3029_get_sr(dev, regs);
Heiko Schocher52365232011-05-26 16:25:05 -0700543 if (ret < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200544 dev_err(dev, "%s: reading SR failed\n", __func__);
Heiko Schocher52365232011-05-26 16:25:05 -0700545 return ret;
546 }
547 /* clear PON bit */
Mylène Josserande6e38082016-05-03 11:54:33 +0200548 ret = rv3029_set_sr(dev, (regs[0] & ~RV3029_STATUS_PON));
Heiko Schocher52365232011-05-26 16:25:05 -0700549 if (ret < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200550 dev_err(dev, "%s: reading SR failed\n", __func__);
Heiko Schocher52365232011-05-26 16:25:05 -0700551 return ret;
552 }
553
554 return 0;
555}
Mylène Josserandabe2f552016-05-03 11:54:35 +0200556
Michael Büsche27e2162016-03-04 22:41:19 +0100557static const struct rv3029_trickle_tab_elem {
558 u32 r; /* resistance in ohms */
559 u8 conf; /* trickle config bits */
560} rv3029_trickle_tab[] = {
561 {
562 .r = 1076,
563 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
564 RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
565 }, {
566 .r = 1091,
567 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
568 RV3029_TRICKLE_20K,
569 }, {
570 .r = 1137,
571 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
572 RV3029_TRICKLE_80K,
573 }, {
574 .r = 1154,
575 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
576 }, {
577 .r = 1371,
578 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
579 RV3029_TRICKLE_80K,
580 }, {
581 .r = 1395,
582 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
583 }, {
584 .r = 1472,
585 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
586 }, {
587 .r = 1500,
588 .conf = RV3029_TRICKLE_1K,
589 }, {
590 .r = 3810,
591 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
592 RV3029_TRICKLE_80K,
593 }, {
594 .r = 4000,
595 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
596 }, {
597 .r = 4706,
598 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
599 }, {
600 .r = 5000,
601 .conf = RV3029_TRICKLE_5K,
602 }, {
603 .r = 16000,
604 .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
605 }, {
606 .r = 20000,
607 .conf = RV3029_TRICKLE_20K,
608 }, {
609 .r = 80000,
610 .conf = RV3029_TRICKLE_80K,
611 },
612};
613
Mylène Josserande6e38082016-05-03 11:54:33 +0200614static void rv3029_trickle_config(struct device *dev)
Michael Büsche27e2162016-03-04 22:41:19 +0100615{
Mylène Josserande6e38082016-05-03 11:54:33 +0200616 struct device_node *of_node = dev->of_node;
Michael Büsche27e2162016-03-04 22:41:19 +0100617 const struct rv3029_trickle_tab_elem *elem;
618 int i, err;
619 u32 ohms;
Michael Büsch39387dc22016-03-10 18:34:23 +0100620 u8 trickle_set_bits;
Michael Büsche27e2162016-03-04 22:41:19 +0100621
622 if (!of_node)
623 return;
624
625 /* Configure the trickle charger. */
Michael Büsche27e2162016-03-04 22:41:19 +0100626 err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms);
627 if (err) {
628 /* Disable trickle charger. */
Michael Büsch39387dc22016-03-10 18:34:23 +0100629 trickle_set_bits = 0;
Michael Büsche27e2162016-03-04 22:41:19 +0100630 } else {
631 /* Enable trickle charger. */
632 for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) {
633 elem = &rv3029_trickle_tab[i];
634 if (elem->r >= ohms)
635 break;
636 }
Michael Büsch39387dc22016-03-10 18:34:23 +0100637 trickle_set_bits = elem->conf;
Mylène Josserande6e38082016-05-03 11:54:33 +0200638 dev_info(dev,
Michael Büsche27e2162016-03-04 22:41:19 +0100639 "Trickle charger enabled at %d ohms resistance.\n",
640 elem->r);
641 }
Mylène Josserande6e38082016-05-03 11:54:33 +0200642 err = rv3029_eeprom_update_bits(dev, RV3029_CONTROL_E2P_EECTRL,
Michael Büsch39387dc22016-03-10 18:34:23 +0100643 RV3029_TRICKLE_MASK,
644 trickle_set_bits);
Mylène Josserandabe2f552016-05-03 11:54:35 +0200645 if (err < 0)
Mylène Josserande6e38082016-05-03 11:54:33 +0200646 dev_err(dev, "Failed to update trickle charger config\n");
Michael Büsche27e2162016-03-04 22:41:19 +0100647}
648
Michael Büscha696b312016-03-10 18:34:46 +0100649#ifdef CONFIG_RTC_DRV_RV3029_HWMON
650
Mylène Josserande6e38082016-05-03 11:54:33 +0200651static int rv3029_read_temp(struct device *dev, int *temp_mC)
Michael Büscha696b312016-03-10 18:34:46 +0100652{
653 int ret;
654 u8 temp;
655
Mylène Josserande6e38082016-05-03 11:54:33 +0200656 ret = rv3029_read_regs(dev, RV3029_TEMP_PAGE, &temp, 1);
Michael Büscha696b312016-03-10 18:34:46 +0100657 if (ret < 0)
658 return ret;
659
660 *temp_mC = ((int)temp - 60) * 1000;
661
662 return 0;
663}
664
665static ssize_t rv3029_hwmon_show_temp(struct device *dev,
666 struct device_attribute *attr,
667 char *buf)
668{
Michael Büscha696b312016-03-10 18:34:46 +0100669 int ret, temp_mC;
670
Mylène Josserande6e38082016-05-03 11:54:33 +0200671 ret = rv3029_read_temp(dev, &temp_mC);
Michael Büscha696b312016-03-10 18:34:46 +0100672 if (ret < 0)
673 return ret;
674
675 return sprintf(buf, "%d\n", temp_mC);
676}
677
678static ssize_t rv3029_hwmon_set_update_interval(struct device *dev,
679 struct device_attribute *attr,
680 const char *buf,
681 size_t count)
682{
Michael Büscha696b312016-03-10 18:34:46 +0100683 unsigned long interval_ms;
684 int ret;
685 u8 th_set_bits = 0;
686
687 ret = kstrtoul(buf, 10, &interval_ms);
688 if (ret < 0)
689 return ret;
690
691 if (interval_ms != 0) {
692 th_set_bits |= RV3029_EECTRL_THE;
693 if (interval_ms >= 16000)
694 th_set_bits |= RV3029_EECTRL_THP;
695 }
Mylène Josserande6e38082016-05-03 11:54:33 +0200696 ret = rv3029_eeprom_update_bits(dev, RV3029_CONTROL_E2P_EECTRL,
Michael Büscha696b312016-03-10 18:34:46 +0100697 RV3029_EECTRL_THE | RV3029_EECTRL_THP,
698 th_set_bits);
699 if (ret < 0)
700 return ret;
701
702 return count;
703}
704
705static ssize_t rv3029_hwmon_show_update_interval(struct device *dev,
706 struct device_attribute *attr,
707 char *buf)
708{
Michael Büscha696b312016-03-10 18:34:46 +0100709 int ret, interval_ms;
710 u8 eectrl;
711
Mylène Josserande6e38082016-05-03 11:54:33 +0200712 ret = rv3029_eeprom_read(dev, RV3029_CONTROL_E2P_EECTRL,
Michael Büscha696b312016-03-10 18:34:46 +0100713 &eectrl, 1);
714 if (ret < 0)
715 return ret;
716
717 if (eectrl & RV3029_EECTRL_THE) {
718 if (eectrl & RV3029_EECTRL_THP)
719 interval_ms = 16000;
720 else
721 interval_ms = 1000;
722 } else {
723 interval_ms = 0;
724 }
725
726 return sprintf(buf, "%d\n", interval_ms);
727}
728
729static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, rv3029_hwmon_show_temp,
730 NULL, 0);
731static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,
732 rv3029_hwmon_show_update_interval,
733 rv3029_hwmon_set_update_interval, 0);
734
735static struct attribute *rv3029_hwmon_attrs[] = {
736 &sensor_dev_attr_temp1_input.dev_attr.attr,
737 &sensor_dev_attr_update_interval.dev_attr.attr,
738 NULL,
739};
740ATTRIBUTE_GROUPS(rv3029_hwmon);
741
Mylène Josserande6e38082016-05-03 11:54:33 +0200742static void rv3029_hwmon_register(struct device *dev, const char *name)
Michael Büscha696b312016-03-10 18:34:46 +0100743{
Mylène Josserande6e38082016-05-03 11:54:33 +0200744 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
Michael Büscha696b312016-03-10 18:34:46 +0100745 struct device *hwmon_dev;
746
Mylène Josserande6e38082016-05-03 11:54:33 +0200747 hwmon_dev = devm_hwmon_device_register_with_groups(dev, name, rv3029,
748 rv3029_hwmon_groups);
Michael Büscha696b312016-03-10 18:34:46 +0100749 if (IS_ERR(hwmon_dev)) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200750 dev_warn(dev, "unable to register hwmon device %ld\n",
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200751 PTR_ERR(hwmon_dev));
Michael Büscha696b312016-03-10 18:34:46 +0100752 }
753}
754
755#else /* CONFIG_RTC_DRV_RV3029_HWMON */
756
Mylène Josserande6e38082016-05-03 11:54:33 +0200757static void rv3029_hwmon_register(struct device *dev, const char *name)
Michael Büscha696b312016-03-10 18:34:46 +0100758{
759}
760
761#endif /* CONFIG_RTC_DRV_RV3029_HWMON */
762
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200763static struct rtc_class_ops rv3029_rtc_ops = {
Mylène Josserande6e38082016-05-03 11:54:33 +0200764 .read_time = rv3029_read_time,
765 .set_time = rv3029_set_time,
Heiko Schocher52365232011-05-26 16:25:05 -0700766};
767
Mylène Josserande6e38082016-05-03 11:54:33 +0200768static int rv3029_probe(struct device *dev, struct regmap *regmap, int irq,
769 const char *name)
Heiko Schocher52365232011-05-26 16:25:05 -0700770{
Mylène Josserande6e38082016-05-03 11:54:33 +0200771 struct rv3029_data *rv3029;
Heiko Schocher52365232011-05-26 16:25:05 -0700772 int rc = 0;
773 u8 buf[1];
774
Mylène Josserande6e38082016-05-03 11:54:33 +0200775 rv3029 = devm_kzalloc(dev, sizeof(*rv3029), GFP_KERNEL);
776 if (!rv3029)
777 return -ENOMEM;
Heiko Schocher52365232011-05-26 16:25:05 -0700778
Mylène Josserande6e38082016-05-03 11:54:33 +0200779 rv3029->regmap = regmap;
780 rv3029->irq = irq;
781 rv3029->dev = dev;
782 dev_set_drvdata(dev, rv3029);
783
784 rc = rv3029_get_sr(dev, buf);
Gregory Hermant67ab2442014-04-03 14:50:17 -0700785 if (rc < 0) {
Mylène Josserande6e38082016-05-03 11:54:33 +0200786 dev_err(dev, "reading status failed\n");
Gregory Hermant67ab2442014-04-03 14:50:17 -0700787 return rc;
788 }
789
Mylène Josserande6e38082016-05-03 11:54:33 +0200790 rv3029_trickle_config(dev);
791 rv3029_hwmon_register(dev, name);
Michael Büsche27e2162016-03-04 22:41:19 +0100792
Mylène Josserande6e38082016-05-03 11:54:33 +0200793 rv3029->rtc = devm_rtc_device_register(dev, name, &rv3029_rtc_ops,
794 THIS_MODULE);
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200795 if (IS_ERR(rv3029->rtc)) {
796 dev_err(dev, "unable to register the class device\n");
797 return PTR_ERR(rv3029->rtc);
798 }
Heiko Schocher52365232011-05-26 16:25:05 -0700799
Mylène Josserand0ddc5b82016-05-03 11:54:38 +0200800 if (rv3029->irq > 0) {
801 rc = devm_request_threaded_irq(dev, rv3029->irq,
802 NULL, rv3029_handle_irq,
803 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
804 "rv3029", dev);
805 if (rc) {
806 dev_warn(dev, "unable to request IRQ, alarms disabled\n");
807 rv3029->irq = 0;
808 } else {
809 rv3029_rtc_ops.read_alarm = rv3029_read_alarm;
810 rv3029_rtc_ops.set_alarm = rv3029_set_alarm;
811 rv3029_rtc_ops.alarm_irq_enable = rv3029_alarm_irq_enable;
812 }
813 }
814
815 return 0;
Mylène Josserande6e38082016-05-03 11:54:33 +0200816}
Heiko Schocher52365232011-05-26 16:25:05 -0700817
Alexandre Bellonic509e432019-12-14 23:10:08 +0100818static const struct regmap_range rv3029_holes_range[] = {
819 regmap_reg_range(0x05, 0x07),
820 regmap_reg_range(0x0f, 0x0f),
821 regmap_reg_range(0x17, 0x17),
822 regmap_reg_range(0x1a, 0x1f),
823 regmap_reg_range(0x21, 0x27),
824 regmap_reg_range(0x34, 0x37),
825};
826
827static const struct regmap_access_table rv3029_regs = {
828 .no_ranges = rv3029_holes_range,
829 .n_no_ranges = ARRAY_SIZE(rv3029_holes_range),
830};
831
832static const struct regmap_config config = {
833 .reg_bits = 8,
834 .val_bits = 8,
835 .rd_table = &rv3029_regs,
836 .wr_table = &rv3029_regs,
837 .max_register = 0x3f,
838};
839
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200840#if IS_ENABLED(CONFIG_I2C)
841
Mylène Josserande6e38082016-05-03 11:54:33 +0200842static int rv3029_i2c_probe(struct i2c_client *client,
843 const struct i2c_device_id *id)
844{
845 struct regmap *regmap;
Mylène Josserande6e38082016-05-03 11:54:33 +0200846 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK |
847 I2C_FUNC_SMBUS_BYTE)) {
848 dev_err(&client->dev, "Adapter does not support SMBUS_I2C_BLOCK or SMBUS_I2C_BYTE\n");
849 return -ENODEV;
850 }
851
852 regmap = devm_regmap_init_i2c(client, &config);
853 if (IS_ERR(regmap)) {
854 dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
855 __func__, PTR_ERR(regmap));
856 return PTR_ERR(regmap);
857 }
858
859 return rv3029_probe(&client->dev, regmap, client->irq, client->name);
Heiko Schocher52365232011-05-26 16:25:05 -0700860}
861
Arvind Yadav45a63512017-08-20 00:37:55 +0530862static const struct i2c_device_id rv3029_id[] = {
Arnd Bergmann814db2b2016-05-04 11:50:02 +0200863 { "rv3029", 0 },
864 { "rv3029c2", 0 },
865 { }
866};
867MODULE_DEVICE_TABLE(i2c, rv3029_id);
868
Javier Martinez Canillase696a1d2017-03-03 11:29:13 -0300869static const struct of_device_id rv3029_of_match[] = {
Alexandre Belloni45b611c2017-09-15 04:00:02 +0200870 { .compatible = "microcrystal,rv3029" },
871 /* Backward compatibility only, do not use compatibles below: */
Javier Martinez Canillase696a1d2017-03-03 11:29:13 -0300872 { .compatible = "rv3029" },
873 { .compatible = "rv3029c2" },
874 { .compatible = "mc,rv3029c2" },
875 { }
876};
877MODULE_DEVICE_TABLE(of, rv3029_of_match);
878
Michael Büschaba39d22016-03-04 22:38:45 +0100879static struct i2c_driver rv3029_driver = {
Heiko Schocher52365232011-05-26 16:25:05 -0700880 .driver = {
Alexandre Belloni9b45ef92019-12-14 23:10:07 +0100881 .name = "rv3029",
Javier Martinez Canillase696a1d2017-03-03 11:29:13 -0300882 .of_match_table = of_match_ptr(rv3029_of_match),
Heiko Schocher52365232011-05-26 16:25:05 -0700883 },
Mylène Josserande6e38082016-05-03 11:54:33 +0200884 .probe = rv3029_i2c_probe,
Michael Büschaba39d22016-03-04 22:38:45 +0100885 .id_table = rv3029_id,
Heiko Schocher52365232011-05-26 16:25:05 -0700886};
887
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200888static int rv3029_register_driver(void)
889{
890 return i2c_add_driver(&rv3029_driver);
891}
892
893static void rv3029_unregister_driver(void)
894{
895 i2c_del_driver(&rv3029_driver);
896}
897
898#else
899
900static int rv3029_register_driver(void)
901{
902 return 0;
903}
904
905static void rv3029_unregister_driver(void)
906{
907}
908
909#endif
910
911#if IS_ENABLED(CONFIG_SPI_MASTER)
912
913static int rv3049_probe(struct spi_device *spi)
914{
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200915 struct regmap *regmap;
916
917 regmap = devm_regmap_init_spi(spi, &config);
918 if (IS_ERR(regmap)) {
919 dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
920 __func__, PTR_ERR(regmap));
921 return PTR_ERR(regmap);
922 }
923
924 return rv3029_probe(&spi->dev, regmap, spi->irq, "rv3049");
925}
926
927static struct spi_driver rv3049_driver = {
928 .driver = {
929 .name = "rv3049",
930 },
931 .probe = rv3049_probe,
932};
933
934static int rv3049_register_driver(void)
935{
936 return spi_register_driver(&rv3049_driver);
937}
938
939static void rv3049_unregister_driver(void)
940{
941 spi_unregister_driver(&rv3049_driver);
942}
943
944#else
945
946static int rv3049_register_driver(void)
947{
948 return 0;
949}
950
951static void rv3049_unregister_driver(void)
952{
953}
954
955#endif
956
957static int __init rv30x9_init(void)
958{
959 int ret;
960
961 ret = rv3029_register_driver();
962 if (ret) {
963 pr_err("Failed to register rv3029 driver: %d\n", ret);
964 return ret;
965 }
966
967 ret = rv3049_register_driver();
968 if (ret) {
969 pr_err("Failed to register rv3049 driver: %d\n", ret);
970 rv3029_unregister_driver();
971 }
972
973 return ret;
974}
975module_init(rv30x9_init)
976
977static void __exit rv30x9_exit(void)
978{
979 rv3049_unregister_driver();
980 rv3029_unregister_driver();
981}
982module_exit(rv30x9_exit)
Heiko Schocher52365232011-05-26 16:25:05 -0700983
984MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
Michael Büsch2dca3d92016-03-04 22:40:30 +0100985MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200986MODULE_DESCRIPTION("Micro Crystal RV3029/RV3049 RTC driver");
Heiko Schocher52365232011-05-26 16:25:05 -0700987MODULE_LICENSE("GPL");
Mylène Josserandc2a1c142016-05-03 11:54:34 +0200988MODULE_ALIAS("spi:rv3049");