blob: 974eafda9de4f1ef577ad5dd158516090719d5f5 [file] [log] [blame]
David Brownell1abb0dc2006-06-25 05:48:17 -07001/*
2 * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips.
3 *
4 * Copyright (C) 2005 James Chapman (ds1337 core)
5 * Copyright (C) 2006 David Brownell
Matthias Fuchsa2166852009-03-31 15:24:58 -07006 * Copyright (C) 2009 Matthias Fuchs (rx8025 support)
Bertrand Achardbc48b902013-04-29 16:19:26 -07007 * Copyright (C) 2012 Bertrand Achard (nvram access fixes)
David Brownell1abb0dc2006-06-25 05:48:17 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Tin Huynh9c19b892016-11-30 09:57:31 +070014#include <linux/acpi.h>
David Brownell1abb0dc2006-06-25 05:48:17 -070015#include <linux/bcd.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050016#include <linux/i2c.h>
17#include <linux/init.h>
18#include <linux/module.h>
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -030019#include <linux/of_device.h>
Wolfram Sangeb86c302012-05-29 15:07:38 -070020#include <linux/rtc/ds1307.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050021#include <linux/rtc.h>
22#include <linux/slab.h>
23#include <linux/string.h>
Akinobu Mita445c0202016-01-25 00:22:16 +090024#include <linux/hwmon.h>
25#include <linux/hwmon-sysfs.h>
Akinobu Mita6c6ff142016-01-31 23:10:10 +090026#include <linux/clk-provider.h>
Heiner Kallweit11e58902017-03-10 18:52:34 +010027#include <linux/regmap.h>
David Brownell1abb0dc2006-06-25 05:48:17 -070028
David Anders40ce9722012-03-23 15:02:37 -070029/*
30 * We can't determine type by probing, but if we expect pre-Linux code
David Brownell1abb0dc2006-06-25 05:48:17 -070031 * to have set the chip up as a clock (turning on the oscillator and
32 * setting the date and time), Linux can ignore the non-clock features.
33 * That's a natural job for a factory or repair bench.
David Brownell1abb0dc2006-06-25 05:48:17 -070034 */
35enum ds_type {
David Brownell045e0e82007-07-17 04:04:55 -070036 ds_1307,
Sean Nyekjaer300a7732017-06-08 12:36:54 +020037 ds_1308,
David Brownell045e0e82007-07-17 04:04:55 -070038 ds_1337,
39 ds_1338,
40 ds_1339,
41 ds_1340,
Nikita Yushchenko0759c882017-08-24 09:32:11 +030042 ds_1341,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -070043 ds_1388,
Wolfram Sang97f902b2009-06-17 16:26:10 -070044 ds_3231,
Stefan Agner8566f702017-03-23 16:54:57 -070045 m41t0,
David Brownell045e0e82007-07-17 04:04:55 -070046 m41t00,
Giulio Benetti7e580762018-05-16 23:08:40 +020047 m41t11,
Tomas Novotnyf4199f82014-12-10 15:53:57 -080048 mcp794xx,
Matthias Fuchsa2166852009-03-31 15:24:58 -070049 rx_8025,
Marek Vasutee0981b2017-06-18 22:55:28 +020050 rx_8130,
Wolfram Sang32d322b2012-03-23 15:02:36 -070051 last_ds_type /* always last */
David Anders40ce9722012-03-23 15:02:37 -070052 /* rs5c372 too? different address... */
David Brownell1abb0dc2006-06-25 05:48:17 -070053};
54
David Brownell1abb0dc2006-06-25 05:48:17 -070055/* RTC registers don't differ much, except for the century flag */
56#define DS1307_REG_SECS 0x00 /* 00-59 */
57# define DS1307_BIT_CH 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070058# define DS1340_BIT_nEOSC 0x80
Tomas Novotnyf4199f82014-12-10 15:53:57 -080059# define MCP794XX_BIT_ST 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070060#define DS1307_REG_MIN 0x01 /* 00-59 */
Stefan Agner8566f702017-03-23 16:54:57 -070061# define M41T0_BIT_OF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070062#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
David Brownellc065f352007-07-17 04:05:10 -070063# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
64# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
David Brownell1abb0dc2006-06-25 05:48:17 -070065# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
66# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
67#define DS1307_REG_WDAY 0x03 /* 01-07 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -080068# define MCP794XX_BIT_VBATEN 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -070069#define DS1307_REG_MDAY 0x04 /* 01-31 */
70#define DS1307_REG_MONTH 0x05 /* 01-12 */
71# define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
72#define DS1307_REG_YEAR 0x06 /* 00-99 */
73
David Anders40ce9722012-03-23 15:02:37 -070074/*
75 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
David Brownell045e0e82007-07-17 04:04:55 -070076 * start at 7, and they differ a LOT. Only control and status matter for
77 * basic RTC date and time functionality; be careful using them.
David Brownell1abb0dc2006-06-25 05:48:17 -070078 */
David Brownell045e0e82007-07-17 04:04:55 -070079#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
David Brownell1abb0dc2006-06-25 05:48:17 -070080# define DS1307_BIT_OUT 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070081# define DS1338_BIT_OSF 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070082# define DS1307_BIT_SQWE 0x10
83# define DS1307_BIT_RS1 0x02
84# define DS1307_BIT_RS0 0x01
85#define DS1337_REG_CONTROL 0x0e
86# define DS1337_BIT_nEOSC 0x80
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070087# define DS1339_BIT_BBSQI 0x20
Wolfram Sang97f902b2009-06-17 16:26:10 -070088# define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
David Brownell1abb0dc2006-06-25 05:48:17 -070089# define DS1337_BIT_RS2 0x10
90# define DS1337_BIT_RS1 0x08
91# define DS1337_BIT_INTCN 0x04
92# define DS1337_BIT_A2IE 0x02
93# define DS1337_BIT_A1IE 0x01
David Brownell045e0e82007-07-17 04:04:55 -070094#define DS1340_REG_CONTROL 0x07
95# define DS1340_BIT_OUT 0x80
96# define DS1340_BIT_FT 0x40
97# define DS1340_BIT_CALIB_SIGN 0x20
98# define DS1340_M_CALIBRATION 0x1f
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070099#define DS1340_REG_FLAG 0x09
100# define DS1340_BIT_OSF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -0700101#define DS1337_REG_STATUS 0x0f
102# define DS1337_BIT_OSF 0x80
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900103# define DS3231_BIT_EN32KHZ 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -0700104# define DS1337_BIT_A2I 0x02
105# define DS1337_BIT_A1I 0x01
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700106#define DS1339_REG_ALARM1_SECS 0x07
Wolfram Sangeb86c302012-05-29 15:07:38 -0700107
108#define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
David Brownell1abb0dc2006-06-25 05:48:17 -0700109
Matthias Fuchsa2166852009-03-31 15:24:58 -0700110#define RX8025_REG_CTRL1 0x0e
111# define RX8025_BIT_2412 0x20
112#define RX8025_REG_CTRL2 0x0f
113# define RX8025_BIT_PON 0x10
114# define RX8025_BIT_VDET 0x40
115# define RX8025_BIT_XST 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -0700116
Uwe Kleine-König3ffd4a22019-01-25 15:35:56 +0100117#define RX8130_REG_ALARM_MIN 0x17
118#define RX8130_REG_ALARM_HOUR 0x18
119#define RX8130_REG_ALARM_WEEK_OR_DAY 0x19
120#define RX8130_REG_EXTENSION 0x1c
Uwe Kleine-König92cbf122019-01-25 15:35:54 +0100121#define RX8130_REG_EXTENSION_WADA BIT(3)
Uwe Kleine-König3ffd4a22019-01-25 15:35:56 +0100122#define RX8130_REG_FLAG 0x1d
123#define RX8130_REG_FLAG_VLF BIT(1)
Uwe Kleine-König92cbf122019-01-25 15:35:54 +0100124#define RX8130_REG_FLAG_AF BIT(3)
Uwe Kleine-König3ffd4a22019-01-25 15:35:56 +0100125#define RX8130_REG_CONTROL0 0x1e
Uwe Kleine-König92cbf122019-01-25 15:35:54 +0100126#define RX8130_REG_CONTROL0_AIE BIT(3)
127
128#define MCP794XX_REG_CONTROL 0x07
129# define MCP794XX_BIT_ALM0_EN 0x10
130# define MCP794XX_BIT_ALM1_EN 0x20
131#define MCP794XX_REG_ALARM0_BASE 0x0a
132#define MCP794XX_REG_ALARM0_CTRL 0x0d
133#define MCP794XX_REG_ALARM1_BASE 0x11
134#define MCP794XX_REG_ALARM1_CTRL 0x14
135# define MCP794XX_BIT_ALMX_IF BIT(3)
136# define MCP794XX_BIT_ALMX_C0 BIT(4)
137# define MCP794XX_BIT_ALMX_C1 BIT(5)
138# define MCP794XX_BIT_ALMX_C2 BIT(6)
139# define MCP794XX_BIT_ALMX_POL BIT(7)
140# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
141 MCP794XX_BIT_ALMX_C1 | \
142 MCP794XX_BIT_ALMX_C2)
143
Giulio Benetti79230ff2018-07-25 19:26:04 +0200144#define M41TXX_REG_CONTROL 0x07
145# define M41TXX_BIT_OUT BIT(7)
146# define M41TXX_BIT_FT BIT(6)
147# define M41TXX_BIT_CALIB_SIGN BIT(5)
148# define M41TXX_M_CALIBRATION GENMASK(4, 0)
149
150/* negative offset step is -2.034ppm */
151#define M41TXX_NEG_OFFSET_STEP_PPB 2034
152/* positive offset step is +4.068ppm */
153#define M41TXX_POS_OFFSET_STEP_PPB 4068
154/* Min and max values supported with 'offset' interface by M41TXX */
155#define M41TXX_MIN_OFFSET ((-31) * M41TXX_NEG_OFFSET_STEP_PPB)
156#define M41TXX_MAX_OFFSET ((31) * M41TXX_POS_OFFSET_STEP_PPB)
157
David Brownell1abb0dc2006-06-25 05:48:17 -0700158struct ds1307 {
David Brownell1abb0dc2006-06-25 05:48:17 -0700159 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700160 unsigned long flags;
161#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
162#define HAS_ALARM 1 /* bit 1 == irq claimed */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100163 struct device *dev;
164 struct regmap *regmap;
165 const char *name;
David Brownell1abb0dc2006-06-25 05:48:17 -0700166 struct rtc_device *rtc;
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900167#ifdef CONFIG_COMMON_CLK
168 struct clk_hw clks[2];
169#endif
David Brownell1abb0dc2006-06-25 05:48:17 -0700170};
171
David Brownell045e0e82007-07-17 04:04:55 -0700172struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700173 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700174 u16 nvram_offset;
175 u16 nvram_size;
Heiner Kallweite5531702017-07-12 07:49:47 +0200176 u8 offset; /* register's offset */
Heiner Kallweite48585d2017-06-05 17:57:33 +0200177 u8 century_reg;
178 u8 century_enable_bit;
179 u8 century_bit;
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200180 u8 bbsqi_bit;
Heiner Kallweit45947122017-07-12 07:49:41 +0200181 irq_handler_t irq_handler;
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200182 const struct rtc_class_ops *rtc_ops;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700183 u16 trickle_charger_reg;
Alexandre Belloni57ec2d92017-09-04 22:46:04 +0200184 u8 (*do_trickle_setup)(struct ds1307 *, u32,
Heiner Kallweit11e58902017-03-10 18:52:34 +0100185 bool);
David Brownell045e0e82007-07-17 04:04:55 -0700186};
187
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100188static const struct chip_desc chips[last_ds_type];
189
190static int ds1307_get_time(struct device *dev, struct rtc_time *t)
191{
192 struct ds1307 *ds1307 = dev_get_drvdata(dev);
193 int tmp, ret;
194 const struct chip_desc *chip = &chips[ds1307->type];
195 u8 regs[7];
196
Uwe Kleine-König501f9822019-01-25 15:35:57 +0100197 if (ds1307->type == rx_8130) {
198 unsigned int regflag;
199 ret = regmap_read(ds1307->regmap, RX8130_REG_FLAG, &regflag);
200 if (ret) {
201 dev_err(dev, "%s error %d\n", "read", ret);
202 return ret;
203 }
204
205 if (regflag & RX8130_REG_FLAG_VLF) {
206 dev_warn_once(dev, "oscillator failed, set time!\n");
207 return -EINVAL;
208 }
209 }
210
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100211 /* read the RTC date and time registers all at once */
212 ret = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
213 sizeof(regs));
214 if (ret) {
215 dev_err(dev, "%s error %d\n", "read", ret);
216 return ret;
217 }
218
219 dev_dbg(dev, "%s: %7ph\n", "read", regs);
220
221 /* if oscillator fail bit is set, no data can be trusted */
222 if (ds1307->type == m41t0 &&
223 regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
224 dev_warn_once(dev, "oscillator failed, set time!\n");
225 return -EINVAL;
226 }
227
228 t->tm_sec = bcd2bin(regs[DS1307_REG_SECS] & 0x7f);
229 t->tm_min = bcd2bin(regs[DS1307_REG_MIN] & 0x7f);
230 tmp = regs[DS1307_REG_HOUR] & 0x3f;
231 t->tm_hour = bcd2bin(tmp);
232 t->tm_wday = bcd2bin(regs[DS1307_REG_WDAY] & 0x07) - 1;
233 t->tm_mday = bcd2bin(regs[DS1307_REG_MDAY] & 0x3f);
234 tmp = regs[DS1307_REG_MONTH] & 0x1f;
235 t->tm_mon = bcd2bin(tmp) - 1;
236 t->tm_year = bcd2bin(regs[DS1307_REG_YEAR]) + 100;
237
238 if (regs[chip->century_reg] & chip->century_bit &&
239 IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY))
240 t->tm_year += 100;
241
242 dev_dbg(dev, "%s secs=%d, mins=%d, "
243 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
244 "read", t->tm_sec, t->tm_min,
245 t->tm_hour, t->tm_mday,
246 t->tm_mon, t->tm_year, t->tm_wday);
247
248 return 0;
249}
250
251static int ds1307_set_time(struct device *dev, struct rtc_time *t)
252{
253 struct ds1307 *ds1307 = dev_get_drvdata(dev);
254 const struct chip_desc *chip = &chips[ds1307->type];
255 int result;
256 int tmp;
257 u8 regs[7];
258
259 dev_dbg(dev, "%s secs=%d, mins=%d, "
260 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
261 "write", t->tm_sec, t->tm_min,
262 t->tm_hour, t->tm_mday,
263 t->tm_mon, t->tm_year, t->tm_wday);
264
265 if (t->tm_year < 100)
266 return -EINVAL;
267
268#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
269 if (t->tm_year > (chip->century_bit ? 299 : 199))
270 return -EINVAL;
271#else
272 if (t->tm_year > 199)
273 return -EINVAL;
274#endif
275
276 regs[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
277 regs[DS1307_REG_MIN] = bin2bcd(t->tm_min);
278 regs[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
279 regs[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
280 regs[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
281 regs[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
282
283 /* assume 20YY not 19YY */
284 tmp = t->tm_year - 100;
285 regs[DS1307_REG_YEAR] = bin2bcd(tmp);
286
287 if (chip->century_enable_bit)
288 regs[chip->century_reg] |= chip->century_enable_bit;
289 if (t->tm_year > 199 && chip->century_bit)
290 regs[chip->century_reg] |= chip->century_bit;
291
292 if (ds1307->type == mcp794xx) {
293 /*
294 * these bits were cleared when preparing the date/time
295 * values and need to be set again before writing the
296 * regsfer out to the device.
297 */
298 regs[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
299 regs[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
300 }
301
302 dev_dbg(dev, "%s: %7ph\n", "write", regs);
303
304 result = regmap_bulk_write(ds1307->regmap, chip->offset, regs,
305 sizeof(regs));
306 if (result) {
307 dev_err(dev, "%s error %d\n", "write", result);
308 return result;
309 }
Uwe Kleine-König501f9822019-01-25 15:35:57 +0100310
311 if (ds1307->type == rx_8130) {
312 /* clear Voltage Loss Flag as data is available now */
313 result = regmap_write(ds1307->regmap, RX8130_REG_FLAG,
314 ~(u8)RX8130_REG_FLAG_VLF);
315 if (result) {
316 dev_err(dev, "%s error %d\n", "write", result);
317 return result;
318 }
319 }
320
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100321 return 0;
322}
323
324static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
325{
326 struct ds1307 *ds1307 = dev_get_drvdata(dev);
327 int ret;
328 u8 regs[9];
329
330 if (!test_bit(HAS_ALARM, &ds1307->flags))
331 return -EINVAL;
332
333 /* read all ALARM1, ALARM2, and status registers at once */
334 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
335 regs, sizeof(regs));
336 if (ret) {
337 dev_err(dev, "%s error %d\n", "alarm read", ret);
338 return ret;
339 }
340
341 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
342 &regs[0], &regs[4], &regs[7]);
343
344 /*
345 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
346 * and that all four fields are checked matches
347 */
348 t->time.tm_sec = bcd2bin(regs[0] & 0x7f);
349 t->time.tm_min = bcd2bin(regs[1] & 0x7f);
350 t->time.tm_hour = bcd2bin(regs[2] & 0x3f);
351 t->time.tm_mday = bcd2bin(regs[3] & 0x3f);
352
353 /* ... and status */
354 t->enabled = !!(regs[7] & DS1337_BIT_A1IE);
355 t->pending = !!(regs[8] & DS1337_BIT_A1I);
356
357 dev_dbg(dev, "%s secs=%d, mins=%d, "
358 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
359 "alarm read", t->time.tm_sec, t->time.tm_min,
360 t->time.tm_hour, t->time.tm_mday,
361 t->enabled, t->pending);
362
363 return 0;
364}
365
366static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
367{
368 struct ds1307 *ds1307 = dev_get_drvdata(dev);
369 unsigned char regs[9];
370 u8 control, status;
371 int ret;
372
373 if (!test_bit(HAS_ALARM, &ds1307->flags))
374 return -EINVAL;
375
376 dev_dbg(dev, "%s secs=%d, mins=%d, "
377 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
378 "alarm set", t->time.tm_sec, t->time.tm_min,
379 t->time.tm_hour, t->time.tm_mday,
380 t->enabled, t->pending);
381
382 /* read current status of both alarms and the chip */
383 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
384 sizeof(regs));
385 if (ret) {
386 dev_err(dev, "%s error %d\n", "alarm write", ret);
387 return ret;
388 }
389 control = regs[7];
390 status = regs[8];
391
392 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
393 &regs[0], &regs[4], control, status);
394
395 /* set ALARM1, using 24 hour and day-of-month modes */
396 regs[0] = bin2bcd(t->time.tm_sec);
397 regs[1] = bin2bcd(t->time.tm_min);
398 regs[2] = bin2bcd(t->time.tm_hour);
399 regs[3] = bin2bcd(t->time.tm_mday);
400
401 /* set ALARM2 to non-garbage */
402 regs[4] = 0;
403 regs[5] = 0;
404 regs[6] = 0;
405
406 /* disable alarms */
407 regs[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
408 regs[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
409
410 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
411 sizeof(regs));
412 if (ret) {
413 dev_err(dev, "can't set alarm time\n");
414 return ret;
415 }
416
417 /* optionally enable ALARM1 */
418 if (t->enabled) {
419 dev_dbg(dev, "alarm IRQ armed\n");
420 regs[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
421 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, regs[7]);
422 }
423
424 return 0;
425}
426
427static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
428{
429 struct ds1307 *ds1307 = dev_get_drvdata(dev);
430
431 if (!test_bit(HAS_ALARM, &ds1307->flags))
432 return -ENOTTY;
433
434 return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
435 DS1337_BIT_A1IE,
436 enabled ? DS1337_BIT_A1IE : 0);
437}
438
439static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307, u32 ohms, bool diode)
440{
441 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
442 DS1307_TRICKLE_CHARGER_NO_DIODE;
443
444 switch (ohms) {
445 case 250:
446 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
447 break;
448 case 2000:
449 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
450 break;
451 case 4000:
452 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
453 break;
454 default:
455 dev_warn(ds1307->dev,
456 "Unsupported ohm value %u in dt\n", ohms);
457 return 0;
458 }
459 return setup;
460}
461
462static irqreturn_t rx8130_irq(int irq, void *dev_id)
463{
464 struct ds1307 *ds1307 = dev_id;
465 struct mutex *lock = &ds1307->rtc->ops_lock;
466 u8 ctl[3];
467 int ret;
468
469 mutex_lock(lock);
470
471 /* Read control registers. */
472 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
473 sizeof(ctl));
474 if (ret < 0)
475 goto out;
476 if (!(ctl[1] & RX8130_REG_FLAG_AF))
477 goto out;
478 ctl[1] &= ~RX8130_REG_FLAG_AF;
479 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
480
481 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
482 sizeof(ctl));
483 if (ret < 0)
484 goto out;
485
486 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
487
488out:
489 mutex_unlock(lock);
490
491 return IRQ_HANDLED;
492}
493
494static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
495{
496 struct ds1307 *ds1307 = dev_get_drvdata(dev);
497 u8 ald[3], ctl[3];
498 int ret;
499
500 if (!test_bit(HAS_ALARM, &ds1307->flags))
501 return -EINVAL;
502
503 /* Read alarm registers. */
504 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
505 sizeof(ald));
506 if (ret < 0)
507 return ret;
508
509 /* Read control registers. */
510 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
511 sizeof(ctl));
512 if (ret < 0)
513 return ret;
514
515 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
516 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
517
518 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
519 t->time.tm_sec = -1;
520 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
521 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
522 t->time.tm_wday = -1;
523 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
524 t->time.tm_mon = -1;
525 t->time.tm_year = -1;
526 t->time.tm_yday = -1;
527 t->time.tm_isdst = -1;
528
529 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
530 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
531 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
532
533 return 0;
534}
535
536static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
537{
538 struct ds1307 *ds1307 = dev_get_drvdata(dev);
539 u8 ald[3], ctl[3];
540 int ret;
541
542 if (!test_bit(HAS_ALARM, &ds1307->flags))
543 return -EINVAL;
544
545 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
546 "enabled=%d pending=%d\n", __func__,
547 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
548 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
549 t->enabled, t->pending);
550
551 /* Read control registers. */
552 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
553 sizeof(ctl));
554 if (ret < 0)
555 return ret;
556
557 ctl[0] &= ~RX8130_REG_EXTENSION_WADA;
558 ctl[1] |= RX8130_REG_FLAG_AF;
559 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
560
561 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
562 sizeof(ctl));
563 if (ret < 0)
564 return ret;
565
566 /* Hardware alarm precision is 1 minute! */
567 ald[0] = bin2bcd(t->time.tm_min);
568 ald[1] = bin2bcd(t->time.tm_hour);
569 ald[2] = bin2bcd(t->time.tm_mday);
570
571 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
572 sizeof(ald));
573 if (ret < 0)
574 return ret;
575
576 if (!t->enabled)
577 return 0;
578
579 ctl[2] |= RX8130_REG_CONTROL0_AIE;
580
581 return regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
582 sizeof(ctl));
583}
584
585static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
586{
587 struct ds1307 *ds1307 = dev_get_drvdata(dev);
588 int ret, reg;
589
590 if (!test_bit(HAS_ALARM, &ds1307->flags))
591 return -EINVAL;
592
593 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
594 if (ret < 0)
595 return ret;
596
597 if (enabled)
598 reg |= RX8130_REG_CONTROL0_AIE;
599 else
600 reg &= ~RX8130_REG_CONTROL0_AIE;
601
602 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
603}
604
605static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
606{
607 struct ds1307 *ds1307 = dev_id;
608 struct mutex *lock = &ds1307->rtc->ops_lock;
609 int reg, ret;
610
611 mutex_lock(lock);
612
613 /* Check and clear alarm 0 interrupt flag. */
614 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
615 if (ret)
616 goto out;
617 if (!(reg & MCP794XX_BIT_ALMX_IF))
618 goto out;
619 reg &= ~MCP794XX_BIT_ALMX_IF;
620 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
621 if (ret)
622 goto out;
623
624 /* Disable alarm 0. */
625 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
626 MCP794XX_BIT_ALM0_EN, 0);
627 if (ret)
628 goto out;
629
630 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
631
632out:
633 mutex_unlock(lock);
634
635 return IRQ_HANDLED;
636}
637
638static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
639{
640 struct ds1307 *ds1307 = dev_get_drvdata(dev);
641 u8 regs[10];
642 int ret;
643
644 if (!test_bit(HAS_ALARM, &ds1307->flags))
645 return -EINVAL;
646
647 /* Read control and alarm 0 registers. */
648 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
649 sizeof(regs));
650 if (ret)
651 return ret;
652
653 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
654
655 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
656 t->time.tm_sec = bcd2bin(regs[3] & 0x7f);
657 t->time.tm_min = bcd2bin(regs[4] & 0x7f);
658 t->time.tm_hour = bcd2bin(regs[5] & 0x3f);
659 t->time.tm_wday = bcd2bin(regs[6] & 0x7) - 1;
660 t->time.tm_mday = bcd2bin(regs[7] & 0x3f);
661 t->time.tm_mon = bcd2bin(regs[8] & 0x1f) - 1;
662 t->time.tm_year = -1;
663 t->time.tm_yday = -1;
664 t->time.tm_isdst = -1;
665
666 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
667 "enabled=%d polarity=%d irq=%d match=%lu\n", __func__,
668 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
669 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
670 !!(regs[6] & MCP794XX_BIT_ALMX_POL),
671 !!(regs[6] & MCP794XX_BIT_ALMX_IF),
672 (regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
673
674 return 0;
675}
676
677/*
678 * We may have a random RTC weekday, therefore calculate alarm weekday based
679 * on current weekday we read from the RTC timekeeping regs
680 */
681static int mcp794xx_alm_weekday(struct device *dev, struct rtc_time *tm_alarm)
682{
683 struct rtc_time tm_now;
684 int days_now, days_alarm, ret;
685
686 ret = ds1307_get_time(dev, &tm_now);
687 if (ret)
688 return ret;
689
690 days_now = div_s64(rtc_tm_to_time64(&tm_now), 24 * 60 * 60);
691 days_alarm = div_s64(rtc_tm_to_time64(tm_alarm), 24 * 60 * 60);
692
693 return (tm_now.tm_wday + days_alarm - days_now) % 7 + 1;
694}
695
696static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
697{
698 struct ds1307 *ds1307 = dev_get_drvdata(dev);
699 unsigned char regs[10];
700 int wday, ret;
701
702 if (!test_bit(HAS_ALARM, &ds1307->flags))
703 return -EINVAL;
704
705 wday = mcp794xx_alm_weekday(dev, &t->time);
706 if (wday < 0)
707 return wday;
708
709 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
710 "enabled=%d pending=%d\n", __func__,
711 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
712 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
713 t->enabled, t->pending);
714
715 /* Read control and alarm 0 registers. */
716 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
717 sizeof(regs));
718 if (ret)
719 return ret;
720
721 /* Set alarm 0, using 24-hour and day-of-month modes. */
722 regs[3] = bin2bcd(t->time.tm_sec);
723 regs[4] = bin2bcd(t->time.tm_min);
724 regs[5] = bin2bcd(t->time.tm_hour);
725 regs[6] = wday;
726 regs[7] = bin2bcd(t->time.tm_mday);
727 regs[8] = bin2bcd(t->time.tm_mon + 1);
728
729 /* Clear the alarm 0 interrupt flag. */
730 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
731 /* Set alarm match: second, minute, hour, day, date, month. */
732 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
733 /* Disable interrupt. We will not enable until completely programmed */
734 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
735
736 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
737 sizeof(regs));
738 if (ret)
739 return ret;
740
741 if (!t->enabled)
742 return 0;
743 regs[0] |= MCP794XX_BIT_ALM0_EN;
744 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
745}
746
747static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
748{
749 struct ds1307 *ds1307 = dev_get_drvdata(dev);
750
751 if (!test_bit(HAS_ALARM, &ds1307->flags))
752 return -EINVAL;
753
754 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
755 MCP794XX_BIT_ALM0_EN,
756 enabled ? MCP794XX_BIT_ALM0_EN : 0);
757}
758
759static int m41txx_rtc_read_offset(struct device *dev, long *offset)
760{
761 struct ds1307 *ds1307 = dev_get_drvdata(dev);
762 unsigned int ctrl_reg;
763 u8 val;
764
765 regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
766
767 val = ctrl_reg & M41TXX_M_CALIBRATION;
768
769 /* check if positive */
770 if (ctrl_reg & M41TXX_BIT_CALIB_SIGN)
771 *offset = (val * M41TXX_POS_OFFSET_STEP_PPB);
772 else
773 *offset = -(val * M41TXX_NEG_OFFSET_STEP_PPB);
774
775 return 0;
776}
777
778static int m41txx_rtc_set_offset(struct device *dev, long offset)
779{
780 struct ds1307 *ds1307 = dev_get_drvdata(dev);
781 unsigned int ctrl_reg;
782
783 if ((offset < M41TXX_MIN_OFFSET) || (offset > M41TXX_MAX_OFFSET))
784 return -ERANGE;
785
786 if (offset >= 0) {
787 ctrl_reg = DIV_ROUND_CLOSEST(offset,
788 M41TXX_POS_OFFSET_STEP_PPB);
789 ctrl_reg |= M41TXX_BIT_CALIB_SIGN;
790 } else {
791 ctrl_reg = DIV_ROUND_CLOSEST(abs(offset),
792 M41TXX_NEG_OFFSET_STEP_PPB);
793 }
794
795 return regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL,
796 M41TXX_M_CALIBRATION | M41TXX_BIT_CALIB_SIGN,
797 ctrl_reg);
798}
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700799
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200800static const struct rtc_class_ops rx8130_rtc_ops = {
801 .read_time = ds1307_get_time,
802 .set_time = ds1307_set_time,
803 .read_alarm = rx8130_read_alarm,
804 .set_alarm = rx8130_set_alarm,
805 .alarm_irq_enable = rx8130_alarm_irq_enable,
806};
807
808static const struct rtc_class_ops mcp794xx_rtc_ops = {
809 .read_time = ds1307_get_time,
810 .set_time = ds1307_set_time,
811 .read_alarm = mcp794xx_read_alarm,
812 .set_alarm = mcp794xx_set_alarm,
813 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
814};
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700815
Giulio Benetti79230ff2018-07-25 19:26:04 +0200816static const struct rtc_class_ops m41txx_rtc_ops = {
817 .read_time = ds1307_get_time,
818 .set_time = ds1307_set_time,
819 .read_alarm = ds1337_read_alarm,
820 .set_alarm = ds1337_set_alarm,
821 .alarm_irq_enable = ds1307_alarm_irq_enable,
822 .read_offset = m41txx_rtc_read_offset,
823 .set_offset = m41txx_rtc_set_offset,
824};
825
Heiner Kallweit7624df42017-07-12 07:49:33 +0200826static const struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700827 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700828 .nvram_offset = 8,
829 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700830 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200831 [ds_1308] = {
832 .nvram_offset = 8,
833 .nvram_size = 56,
834 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700835 [ds_1337] = {
836 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200837 .century_reg = DS1307_REG_MONTH,
838 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700839 },
840 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700841 .nvram_offset = 8,
842 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700843 },
844 [ds_1339] = {
845 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200846 .century_reg = DS1307_REG_MONTH,
847 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200848 .bbsqi_bit = DS1339_BIT_BBSQI,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700849 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700850 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700851 },
852 [ds_1340] = {
Heiner Kallweite48585d2017-06-05 17:57:33 +0200853 .century_reg = DS1307_REG_HOUR,
854 .century_enable_bit = DS1340_BIT_CENTURY_EN,
855 .century_bit = DS1340_BIT_CENTURY,
Andrea Greco51ed73eb2018-04-20 11:34:02 +0200856 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700857 .trickle_charger_reg = 0x08,
858 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300859 [ds_1341] = {
860 .century_reg = DS1307_REG_MONTH,
861 .century_bit = DS1337_BIT_CENTURY,
862 },
Wolfram Sangeb86c302012-05-29 15:07:38 -0700863 [ds_1388] = {
Heiner Kallweite5531702017-07-12 07:49:47 +0200864 .offset = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700865 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700866 },
867 [ds_3231] = {
868 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200869 .century_reg = DS1307_REG_MONTH,
870 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200871 .bbsqi_bit = DS3231_BIT_BBSQW,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700872 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200873 [rx_8130] = {
874 .alarm = 1,
875 /* this is battery backed SRAM */
876 .nvram_offset = 0x20,
877 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
Heiner Kallweite5531702017-07-12 07:49:47 +0200878 .offset = 0x10,
Heiner Kallweit45947122017-07-12 07:49:41 +0200879 .irq_handler = rx8130_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200880 .rtc_ops = &rx8130_rtc_ops,
Marek Vasutee0981b2017-06-18 22:55:28 +0200881 },
Giulio Benetti79230ff2018-07-25 19:26:04 +0200882 [m41t0] = {
883 .rtc_ops = &m41txx_rtc_ops,
884 },
885 [m41t00] = {
886 .rtc_ops = &m41txx_rtc_ops,
887 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200888 [m41t11] = {
889 /* this is battery backed SRAM */
890 .nvram_offset = 8,
891 .nvram_size = 56,
Giulio Benetti79230ff2018-07-25 19:26:04 +0200892 .rtc_ops = &m41txx_rtc_ops,
Giulio Benetti7e580762018-05-16 23:08:40 +0200893 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800894 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700895 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700896 /* this is battery backed SRAM */
897 .nvram_offset = 0x20,
898 .nvram_size = 0x40,
Heiner Kallweit45947122017-07-12 07:49:41 +0200899 .irq_handler = mcp794xx_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200900 .rtc_ops = &mcp794xx_rtc_ops,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700901 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700902};
David Brownell045e0e82007-07-17 04:04:55 -0700903
Jean Delvare3760f732008-04-29 23:11:40 +0200904static const struct i2c_device_id ds1307_id[] = {
905 { "ds1307", ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200906 { "ds1308", ds_1308 },
Jean Delvare3760f732008-04-29 23:11:40 +0200907 { "ds1337", ds_1337 },
908 { "ds1338", ds_1338 },
909 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700910 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200911 { "ds1340", ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300912 { "ds1341", ds_1341 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700913 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700914 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200915 { "m41t00", m41t00 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200916 { "m41t11", m41t11 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800917 { "mcp7940x", mcp794xx },
918 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700919 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700920 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200921 { "isl12057", ds_1337 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200922 { "rx8130", rx_8130 },
Jean Delvare3760f732008-04-29 23:11:40 +0200923 { }
924};
925MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700926
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300927#ifdef CONFIG_OF
928static const struct of_device_id ds1307_of_match[] = {
929 {
930 .compatible = "dallas,ds1307",
931 .data = (void *)ds_1307
932 },
933 {
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200934 .compatible = "dallas,ds1308",
935 .data = (void *)ds_1308
936 },
937 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300938 .compatible = "dallas,ds1337",
939 .data = (void *)ds_1337
940 },
941 {
942 .compatible = "dallas,ds1338",
943 .data = (void *)ds_1338
944 },
945 {
946 .compatible = "dallas,ds1339",
947 .data = (void *)ds_1339
948 },
949 {
950 .compatible = "dallas,ds1388",
951 .data = (void *)ds_1388
952 },
953 {
954 .compatible = "dallas,ds1340",
955 .data = (void *)ds_1340
956 },
957 {
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300958 .compatible = "dallas,ds1341",
959 .data = (void *)ds_1341
960 },
961 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300962 .compatible = "maxim,ds3231",
963 .data = (void *)ds_3231
964 },
965 {
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200966 .compatible = "st,m41t0",
Giulio Benetti146a5522018-05-16 23:08:39 +0200967 .data = (void *)m41t0
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200968 },
969 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300970 .compatible = "st,m41t00",
971 .data = (void *)m41t00
972 },
973 {
Giulio Benetti7e580762018-05-16 23:08:40 +0200974 .compatible = "st,m41t11",
975 .data = (void *)m41t11
976 },
977 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300978 .compatible = "microchip,mcp7940x",
979 .data = (void *)mcp794xx
980 },
981 {
982 .compatible = "microchip,mcp7941x",
983 .data = (void *)mcp794xx
984 },
985 {
986 .compatible = "pericom,pt7c4338",
987 .data = (void *)ds_1307
988 },
989 {
990 .compatible = "epson,rx8025",
991 .data = (void *)rx_8025
992 },
993 {
994 .compatible = "isil,isl12057",
995 .data = (void *)ds_1337
996 },
Bastian Stender47dd4722017-10-17 14:46:07 +0200997 {
998 .compatible = "epson,rx8130",
999 .data = (void *)rx_8130
1000 },
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001001 { }
1002};
1003MODULE_DEVICE_TABLE(of, ds1307_of_match);
1004#endif
1005
Tin Huynh9c19b892016-11-30 09:57:31 +07001006#ifdef CONFIG_ACPI
1007static const struct acpi_device_id ds1307_acpi_ids[] = {
1008 { .id = "DS1307", .driver_data = ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001009 { .id = "DS1308", .driver_data = ds_1308 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001010 { .id = "DS1337", .driver_data = ds_1337 },
1011 { .id = "DS1338", .driver_data = ds_1338 },
1012 { .id = "DS1339", .driver_data = ds_1339 },
1013 { .id = "DS1388", .driver_data = ds_1388 },
1014 { .id = "DS1340", .driver_data = ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +03001015 { .id = "DS1341", .driver_data = ds_1341 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001016 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -07001017 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001018 { .id = "M41T00", .driver_data = m41t00 },
Giulio Benetti7e580762018-05-16 23:08:40 +02001019 { .id = "M41T11", .driver_data = m41t11 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001020 { .id = "MCP7940X", .driver_data = mcp794xx },
1021 { .id = "MCP7941X", .driver_data = mcp794xx },
1022 { .id = "PT7C4338", .driver_data = ds_1307 },
1023 { .id = "RX8025", .driver_data = rx_8025 },
1024 { .id = "ISL12057", .driver_data = ds_1337 },
Bastian Stender47dd4722017-10-17 14:46:07 +02001025 { .id = "RX8130", .driver_data = rx_8130 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001026 { }
1027};
1028MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
1029#endif
1030
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001031/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001032 * The ds1337 and ds1339 both have two alarms, but we only use the first
1033 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
1034 * signal; ds1339 chips have only one alarm signal.
1035 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001036static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001037{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001038 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001039 struct mutex *lock = &ds1307->rtc->ops_lock;
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001040 int stat, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001041
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001042 mutex_lock(lock);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001043 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
1044 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001045 goto out;
1046
1047 if (stat & DS1337_BIT_A1I) {
1048 stat &= ~DS1337_BIT_A1I;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001049 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001050
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001051 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1052 DS1337_BIT_A1IE, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001053 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001054 goto out;
1055
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001056 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001057 }
1058
1059out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001060 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001061
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001062 return IRQ_HANDLED;
1063}
1064
1065/*----------------------------------------------------------------------*/
1066
David Brownellff8371a2006-09-30 23:28:17 -07001067static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -07001068 .read_time = ds1307_get_time,
1069 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -08001070 .read_alarm = ds1337_read_alarm,
1071 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -08001072 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -07001073};
1074
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001075static ssize_t frequency_test_store(struct device *dev,
1076 struct device_attribute *attr,
1077 const char *buf, size_t count)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001078{
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001079 struct ds1307 *ds1307 = dev_get_drvdata(dev->parent);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001080 bool freq_test_en;
1081 int ret;
1082
1083 ret = kstrtobool(buf, &freq_test_en);
1084 if (ret) {
1085 dev_err(dev, "Failed to store RTC Frequency Test attribute\n");
1086 return ret;
1087 }
1088
1089 regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL, M41TXX_BIT_FT,
1090 freq_test_en ? M41TXX_BIT_FT : 0);
1091
1092 return count;
1093}
1094
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001095static ssize_t frequency_test_show(struct device *dev,
1096 struct device_attribute *attr,
1097 char *buf)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001098{
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001099 struct ds1307 *ds1307 = dev_get_drvdata(dev->parent);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001100 unsigned int ctrl_reg;
1101
1102 regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
1103
1104 return scnprintf(buf, PAGE_SIZE, (ctrl_reg & M41TXX_BIT_FT) ? "on\n" :
1105 "off\n");
1106}
1107
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001108static DEVICE_ATTR_RW(frequency_test);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001109
1110static struct attribute *rtc_freq_test_attrs[] = {
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001111 &dev_attr_frequency_test.attr,
Giulio Benettib41c23e2018-07-25 19:26:05 +02001112 NULL,
1113};
1114
1115static const struct attribute_group rtc_freq_test_attr_group = {
1116 .attrs = rtc_freq_test_attrs,
1117};
1118
Giulio Benettib41c23e2018-07-25 19:26:05 +02001119static int ds1307_add_frequency_test(struct ds1307 *ds1307)
1120{
1121 int err;
1122
1123 switch (ds1307->type) {
1124 case m41t0:
1125 case m41t00:
1126 case m41t11:
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001127 err = rtc_add_group(ds1307->rtc, &rtc_freq_test_attr_group);
1128 if (err)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001129 return err;
Giulio Benettib41c23e2018-07-25 19:26:05 +02001130 break;
1131 default:
1132 break;
1133 }
1134
1135 return 0;
1136}
1137
Simon Guinot1d1945d2014-04-03 14:49:55 -07001138/*----------------------------------------------------------------------*/
1139
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001140static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
1141 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -08001142{
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001143 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +02001144 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -08001145
Heiner Kallweit969fa072017-07-12 07:49:54 +02001146 return regmap_bulk_read(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001147 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -08001148}
1149
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001150static int ds1307_nvram_write(void *priv, unsigned int offset, void *val,
1151 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -08001152{
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001153 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +02001154 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -08001155
Heiner Kallweit969fa072017-07-12 07:49:54 +02001156 return regmap_bulk_write(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001157 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -08001158}
1159
David Brownell682d73f2007-11-14 16:58:32 -08001160/*----------------------------------------------------------------------*/
1161
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001162static u8 ds1307_trickle_init(struct ds1307 *ds1307,
Heiner Kallweit7624df42017-07-12 07:49:33 +02001163 const struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001164{
Alexandre Belloni57ec2d92017-09-04 22:46:04 +02001165 u32 ohms;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001166 bool diode = true;
1167
1168 if (!chip->do_trickle_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001169 return 0;
1170
Heiner Kallweit11e58902017-03-10 18:52:34 +01001171 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
1172 &ohms))
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001173 return 0;
1174
Heiner Kallweit11e58902017-03-10 18:52:34 +01001175 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001176 diode = false;
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001177
1178 return chip->do_trickle_setup(ds1307, ohms, diode);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001179}
1180
Akinobu Mita445c0202016-01-25 00:22:16 +09001181/*----------------------------------------------------------------------*/
1182
Heiner Kallweit6b583a62017-09-27 22:41:26 +02001183#if IS_REACHABLE(CONFIG_HWMON)
Akinobu Mita445c0202016-01-25 00:22:16 +09001184
1185/*
1186 * Temperature sensor support for ds3231 devices.
1187 */
1188
1189#define DS3231_REG_TEMPERATURE 0x11
1190
1191/*
1192 * A user-initiated temperature conversion is not started by this function,
1193 * so the temperature is updated once every 64 seconds.
1194 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001195static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +09001196{
1197 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1198 u8 temp_buf[2];
1199 s16 temp;
1200 int ret;
1201
Heiner Kallweit11e58902017-03-10 18:52:34 +01001202 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
1203 temp_buf, sizeof(temp_buf));
1204 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +09001205 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +09001206 /*
1207 * Temperature is represented as a 10-bit code with a resolution of
1208 * 0.25 degree celsius and encoded in two's complement format.
1209 */
1210 temp = (temp_buf[0] << 8) | temp_buf[1];
1211 temp >>= 6;
1212 *mC = temp * 250;
1213
1214 return 0;
1215}
1216
1217static ssize_t ds3231_hwmon_show_temp(struct device *dev,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001218 struct device_attribute *attr, char *buf)
Akinobu Mita445c0202016-01-25 00:22:16 +09001219{
1220 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001221 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001222
1223 ret = ds3231_hwmon_read_temp(dev, &temp);
1224 if (ret)
1225 return ret;
1226
1227 return sprintf(buf, "%d\n", temp);
1228}
Alexandre Bellonib4be2712017-09-04 22:46:08 +02001229static SENSOR_DEVICE_ATTR(temp1_input, 0444, ds3231_hwmon_show_temp,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001230 NULL, 0);
Akinobu Mita445c0202016-01-25 00:22:16 +09001231
1232static struct attribute *ds3231_hwmon_attrs[] = {
1233 &sensor_dev_attr_temp1_input.dev_attr.attr,
1234 NULL,
1235};
1236ATTRIBUTE_GROUPS(ds3231_hwmon);
1237
1238static void ds1307_hwmon_register(struct ds1307 *ds1307)
1239{
1240 struct device *dev;
1241
1242 if (ds1307->type != ds_3231)
1243 return;
1244
Heiner Kallweit11e58902017-03-10 18:52:34 +01001245 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001246 ds1307,
1247 ds3231_hwmon_groups);
Akinobu Mita445c0202016-01-25 00:22:16 +09001248 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001249 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1250 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001251 }
1252}
1253
1254#else
1255
1256static void ds1307_hwmon_register(struct ds1307 *ds1307)
1257{
1258}
1259
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001260#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1261
1262/*----------------------------------------------------------------------*/
1263
1264/*
1265 * Square-wave output support for DS3231
1266 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1267 */
1268#ifdef CONFIG_COMMON_CLK
1269
1270enum {
1271 DS3231_CLK_SQW = 0,
1272 DS3231_CLK_32KHZ,
1273};
1274
1275#define clk_sqw_to_ds1307(clk) \
1276 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1277#define clk_32khz_to_ds1307(clk) \
1278 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1279
1280static int ds3231_clk_sqw_rates[] = {
1281 1,
1282 1024,
1283 4096,
1284 8192,
1285};
1286
1287static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1288{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001289 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001290 int ret;
1291
1292 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001293 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1294 mask, value);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001295 mutex_unlock(lock);
1296
1297 return ret;
1298}
1299
1300static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1301 unsigned long parent_rate)
1302{
1303 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001304 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001305 int rate_sel = 0;
1306
Heiner Kallweit11e58902017-03-10 18:52:34 +01001307 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1308 if (ret)
1309 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001310 if (control & DS1337_BIT_RS1)
1311 rate_sel += 1;
1312 if (control & DS1337_BIT_RS2)
1313 rate_sel += 2;
1314
1315 return ds3231_clk_sqw_rates[rate_sel];
1316}
1317
1318static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001319 unsigned long *prate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001320{
1321 int i;
1322
1323 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1324 if (ds3231_clk_sqw_rates[i] <= rate)
1325 return ds3231_clk_sqw_rates[i];
1326 }
1327
1328 return 0;
1329}
1330
1331static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001332 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001333{
1334 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1335 int control = 0;
1336 int rate_sel;
1337
1338 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1339 rate_sel++) {
1340 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1341 break;
1342 }
1343
1344 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1345 return -EINVAL;
1346
1347 if (rate_sel & 1)
1348 control |= DS1337_BIT_RS1;
1349 if (rate_sel & 2)
1350 control |= DS1337_BIT_RS2;
1351
1352 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1353 control);
1354}
1355
1356static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1357{
1358 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1359
1360 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1361}
1362
1363static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1364{
1365 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1366
1367 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1368}
1369
1370static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1371{
1372 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001373 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001374
Heiner Kallweit11e58902017-03-10 18:52:34 +01001375 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1376 if (ret)
1377 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001378
1379 return !(control & DS1337_BIT_INTCN);
1380}
1381
1382static const struct clk_ops ds3231_clk_sqw_ops = {
1383 .prepare = ds3231_clk_sqw_prepare,
1384 .unprepare = ds3231_clk_sqw_unprepare,
1385 .is_prepared = ds3231_clk_sqw_is_prepared,
1386 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1387 .round_rate = ds3231_clk_sqw_round_rate,
1388 .set_rate = ds3231_clk_sqw_set_rate,
1389};
1390
1391static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001392 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001393{
1394 return 32768;
1395}
1396
1397static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1398{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001399 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001400 int ret;
1401
1402 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001403 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1404 DS3231_BIT_EN32KHZ,
1405 enable ? DS3231_BIT_EN32KHZ : 0);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001406 mutex_unlock(lock);
1407
1408 return ret;
1409}
1410
1411static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1412{
1413 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1414
1415 return ds3231_clk_32khz_control(ds1307, true);
1416}
1417
1418static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1419{
1420 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1421
1422 ds3231_clk_32khz_control(ds1307, false);
1423}
1424
1425static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1426{
1427 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001428 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001429
Heiner Kallweit11e58902017-03-10 18:52:34 +01001430 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1431 if (ret)
1432 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001433
1434 return !!(status & DS3231_BIT_EN32KHZ);
1435}
1436
1437static const struct clk_ops ds3231_clk_32khz_ops = {
1438 .prepare = ds3231_clk_32khz_prepare,
1439 .unprepare = ds3231_clk_32khz_unprepare,
1440 .is_prepared = ds3231_clk_32khz_is_prepared,
1441 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1442};
1443
1444static struct clk_init_data ds3231_clks_init[] = {
1445 [DS3231_CLK_SQW] = {
1446 .name = "ds3231_clk_sqw",
1447 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001448 },
1449 [DS3231_CLK_32KHZ] = {
1450 .name = "ds3231_clk_32khz",
1451 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001452 },
1453};
1454
1455static int ds3231_clks_register(struct ds1307 *ds1307)
1456{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001457 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001458 struct clk_onecell_data *onecell;
1459 int i;
1460
Heiner Kallweit11e58902017-03-10 18:52:34 +01001461 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001462 if (!onecell)
1463 return -ENOMEM;
1464
1465 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001466 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1467 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001468 if (!onecell->clks)
1469 return -ENOMEM;
1470
1471 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1472 struct clk_init_data init = ds3231_clks_init[i];
1473
1474 /*
1475 * Interrupt signal due to alarm conditions and square-wave
1476 * output share same pin, so don't initialize both.
1477 */
1478 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1479 continue;
1480
1481 /* optional override of the clockname */
1482 of_property_read_string_index(node, "clock-output-names", i,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001483 &init.name);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001484 ds1307->clks[i].init = &init;
1485
Heiner Kallweit11e58902017-03-10 18:52:34 +01001486 onecell->clks[i] = devm_clk_register(ds1307->dev,
1487 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001488 if (IS_ERR(onecell->clks[i]))
1489 return PTR_ERR(onecell->clks[i]);
1490 }
1491
1492 if (!node)
1493 return 0;
1494
1495 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1496
1497 return 0;
1498}
1499
1500static void ds1307_clks_register(struct ds1307 *ds1307)
1501{
1502 int ret;
1503
1504 if (ds1307->type != ds_3231)
1505 return;
1506
1507 ret = ds3231_clks_register(ds1307);
1508 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001509 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1510 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001511 }
1512}
1513
1514#else
1515
1516static void ds1307_clks_register(struct ds1307 *ds1307)
1517{
1518}
1519
1520#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001521
Heiner Kallweit11e58902017-03-10 18:52:34 +01001522static const struct regmap_config regmap_config = {
1523 .reg_bits = 8,
1524 .val_bits = 8,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001525};
1526
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001527static int ds1307_probe(struct i2c_client *client,
1528 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001529{
1530 struct ds1307 *ds1307;
1531 int err = -ENODEV;
Heiner Kallweit584ce302017-08-29 21:52:56 +02001532 int tmp;
Heiner Kallweit7624df42017-07-12 07:49:33 +02001533 const struct chip_desc *chip;
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001534 bool want_irq;
Michael Lange8bc2a402016-01-21 18:10:16 +01001535 bool ds1307_can_wakeup_device = false;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001536 unsigned char regs[8];
Jingoo Han01ce8932013-11-12 15:10:41 -08001537 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001538 u8 trickle_charger_setup = 0;
David Brownell1abb0dc2006-06-25 05:48:17 -07001539
Jingoo Hanedca66d2013-07-03 15:07:05 -07001540 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001541 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001542 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001543
Heiner Kallweit11e58902017-03-10 18:52:34 +01001544 dev_set_drvdata(&client->dev, ds1307);
1545 ds1307->dev = &client->dev;
1546 ds1307->name = client->name;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001547
Heiner Kallweit11e58902017-03-10 18:52:34 +01001548 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1549 if (IS_ERR(ds1307->regmap)) {
1550 dev_err(ds1307->dev, "regmap allocation failed\n");
1551 return PTR_ERR(ds1307->regmap);
1552 }
1553
1554 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001555
1556 if (client->dev.of_node) {
1557 ds1307->type = (enum ds_type)
1558 of_device_get_match_data(&client->dev);
1559 chip = &chips[ds1307->type];
1560 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001561 chip = &chips[id->driver_data];
1562 ds1307->type = id->driver_data;
1563 } else {
1564 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001565
Tin Huynh9c19b892016-11-30 09:57:31 +07001566 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001567 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001568 if (!acpi_id)
1569 return -ENODEV;
1570 chip = &chips[acpi_id->driver_data];
1571 ds1307->type = acpi_id->driver_data;
1572 }
1573
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001574 want_irq = client->irq > 0 && chip->alarm;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001575
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001576 if (!pdata)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001577 trickle_charger_setup = ds1307_trickle_init(ds1307, chip);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001578 else if (pdata->trickle_charger_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001579 trickle_charger_setup = pdata->trickle_charger_setup;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001580
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001581 if (trickle_charger_setup && chip->trickle_charger_reg) {
1582 trickle_charger_setup |= DS13XX_TRICKLE_CHARGER_MAGIC;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001583 dev_dbg(ds1307->dev,
1584 "writing trickle charger info 0x%x to 0x%x\n",
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001585 trickle_charger_setup, chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001586 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001587 trickle_charger_setup);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001588 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001589
Michael Lange8bc2a402016-01-21 18:10:16 +01001590#ifdef CONFIG_OF
1591/*
1592 * For devices with no IRQ directly connected to the SoC, the RTC chip
1593 * can be forced as a wakeup source by stating that explicitly in
1594 * the device's .dts file using the "wakeup-source" boolean property.
1595 * If the "wakeup-source" property is set, don't request an IRQ.
1596 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1597 * if supported by the RTC.
1598 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001599 if (chip->alarm && of_property_read_bool(client->dev.of_node,
1600 "wakeup-source"))
Michael Lange8bc2a402016-01-21 18:10:16 +01001601 ds1307_can_wakeup_device = true;
Michael Lange8bc2a402016-01-21 18:10:16 +01001602#endif
1603
David Brownell045e0e82007-07-17 04:04:55 -07001604 switch (ds1307->type) {
1605 case ds_1337:
1606 case ds_1339:
Nikita Yushchenko0759c882017-08-24 09:32:11 +03001607 case ds_1341:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001608 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001609 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001610 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001611 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001612 if (err) {
1613 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001614 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001615 }
1616
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001617 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001618 if (regs[0] & DS1337_BIT_nEOSC)
1619 regs[0] &= ~DS1337_BIT_nEOSC;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001620
David Anders40ce9722012-03-23 15:02:37 -07001621 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001622 * Using IRQ or defined as wakeup-source?
1623 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001624 * For some variants, be sure alarms can trigger when we're
1625 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001626 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001627 if (want_irq || ds1307_can_wakeup_device) {
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001628 regs[0] |= DS1337_BIT_INTCN | chip->bbsqi_bit;
1629 regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001630 }
1631
Heiner Kallweit11e58902017-03-10 18:52:34 +01001632 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001633 regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001634
1635 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001636 if (regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001637 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001638 regs[1] & ~DS1337_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001639 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001640 }
David Brownell045e0e82007-07-17 04:04:55 -07001641 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001642
1643 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001644 err = regmap_bulk_read(ds1307->regmap,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001645 RX8025_REG_CTRL1 << 4 | 0x08, regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001646 if (err) {
1647 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001648 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001649 }
1650
1651 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001652 if (!(regs[1] & RX8025_BIT_XST)) {
1653 regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001654 regmap_write(ds1307->regmap,
1655 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001656 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001657 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001658 "oscillator stop detected - SET TIME!\n");
1659 }
1660
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001661 if (regs[1] & RX8025_BIT_PON) {
1662 regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001663 regmap_write(ds1307->regmap,
1664 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001665 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001666 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001667 }
1668
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001669 if (regs[1] & RX8025_BIT_VDET) {
1670 regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001671 regmap_write(ds1307->regmap,
1672 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001673 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001674 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001675 }
1676
1677 /* make sure we are running in 24hour mode */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001678 if (!(regs[0] & RX8025_BIT_2412)) {
Matthias Fuchsa2166852009-03-31 15:24:58 -07001679 u8 hour;
1680
1681 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001682 regmap_write(ds1307->regmap,
1683 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001684 regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001685
Heiner Kallweit11e58902017-03-10 18:52:34 +01001686 err = regmap_bulk_read(ds1307->regmap,
1687 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001688 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001689 if (err) {
1690 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001691 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001692 }
1693
1694 /* correct hour */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001695 hour = bcd2bin(regs[DS1307_REG_HOUR]);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001696 if (hour == 12)
1697 hour = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001698 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
Matthias Fuchsa2166852009-03-31 15:24:58 -07001699 hour += 12;
1700
Heiner Kallweit11e58902017-03-10 18:52:34 +01001701 regmap_write(ds1307->regmap,
1702 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001703 }
1704 break;
David Brownell045e0e82007-07-17 04:04:55 -07001705 default:
1706 break;
1707 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001708
1709read_rtc:
1710 /* read RTC registers */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001711 err = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
1712 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +01001713 if (err) {
1714 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001715 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001716 }
1717
David Anders40ce9722012-03-23 15:02:37 -07001718 /*
1719 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001720 * specify the extra bits as must-be-zero, but there are
1721 * still a few values that are clearly out-of-range.
1722 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001723 tmp = regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001724 switch (ds1307->type) {
1725 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001726 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001727 case m41t00:
Giulio Benetti7e580762018-05-16 23:08:40 +02001728 case m41t11:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001729 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001730 if (tmp & DS1307_BIT_CH) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001731 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1732 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001733 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001734 }
David Brownell045e0e82007-07-17 04:04:55 -07001735 break;
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001736 case ds_1308:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001737 case ds_1338:
1738 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001739 if (tmp & DS1307_BIT_CH)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001740 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001741
1742 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001743 if (regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001744 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001745 regs[DS1307_REG_CONTROL] &
1746 ~DS1338_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001747 dev_warn(ds1307->dev, "SET TIME!\n");
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001748 goto read_rtc;
1749 }
David Brownell045e0e82007-07-17 04:04:55 -07001750 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001751 case ds_1340:
1752 /* clock halted? turn it on, so clock can tick. */
1753 if (tmp & DS1340_BIT_nEOSC)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001754 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
frederic Rodofcd8db02008-02-06 01:38:55 -08001755
Heiner Kallweit11e58902017-03-10 18:52:34 +01001756 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1757 if (err) {
1758 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001759 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001760 }
1761
1762 /* oscillator fault? clear flag, and warn */
1763 if (tmp & DS1340_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001764 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1765 dev_warn(ds1307->dev, "SET TIME!\n");
frederic Rodofcd8db02008-02-06 01:38:55 -08001766 }
1767 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001768 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001769 /* make sure that the backup battery is enabled */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001770 if (!(regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001771 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001772 regs[DS1307_REG_WDAY] |
Heiner Kallweit11e58902017-03-10 18:52:34 +01001773 MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001774 }
1775
1776 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001777 if (!(tmp & MCP794XX_BIT_ST)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001778 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1779 MCP794XX_BIT_ST);
1780 dev_warn(ds1307->dev, "SET TIME!\n");
David Anders43fcb812011-11-02 13:37:53 -07001781 goto read_rtc;
1782 }
1783
1784 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001785 default:
David Brownell045e0e82007-07-17 04:04:55 -07001786 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001787 }
David Brownell045e0e82007-07-17 04:04:55 -07001788
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001789 tmp = regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001790 switch (ds1307->type) {
1791 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001792 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001793 case m41t00:
Giulio Benetti7e580762018-05-16 23:08:40 +02001794 case m41t11:
David Anders40ce9722012-03-23 15:02:37 -07001795 /*
1796 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001797 * systems that will run through year 2100.
1798 */
1799 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001800 case rx_8025:
1801 break;
David Brownellc065f352007-07-17 04:05:10 -07001802 default:
1803 if (!(tmp & DS1307_BIT_12HR))
1804 break;
1805
David Anders40ce9722012-03-23 15:02:37 -07001806 /*
1807 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001808 * take note...
1809 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001810 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001811 if (tmp == 12)
1812 tmp = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001813 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
David Brownellc065f352007-07-17 04:05:10 -07001814 tmp += 12;
Heiner Kallweite5531702017-07-12 07:49:47 +02001815 regmap_write(ds1307->regmap, chip->offset + DS1307_REG_HOUR,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001816 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001817 }
1818
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001819 if (want_irq || ds1307_can_wakeup_device) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001820 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001821 set_bit(HAS_ALARM, &ds1307->flags);
1822 }
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001823
1824 ds1307->rtc = devm_rtc_allocate_device(ds1307->dev);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001825 if (IS_ERR(ds1307->rtc))
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001826 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001827
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001828 if (ds1307_can_wakeup_device && !want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001829 dev_info(ds1307->dev,
1830 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001831 /* We cannot support UIE mode if we do not have an IRQ line */
1832 ds1307->rtc->uie_unsupported = 1;
1833 }
1834
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001835 if (want_irq) {
Heiner Kallweit45947122017-07-12 07:49:41 +02001836 err = devm_request_threaded_irq(ds1307->dev, client->irq, NULL,
1837 chip->irq_handler ?: ds1307_irq,
Nishanth Menonc5983192015-06-23 11:15:11 -05001838 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001839 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001840 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001841 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001842 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001843 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001844 dev_err(ds1307->dev, "unable to request IRQ!\n");
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001845 } else {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001846 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001847 }
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001848 }
1849
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001850 ds1307->rtc->ops = chip->rtc_ops ?: &ds13xx_rtc_ops;
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001851 err = ds1307_add_frequency_test(ds1307);
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001852 if (err)
1853 return err;
1854
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001855 err = rtc_register_device(ds1307->rtc);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001856 if (err)
1857 return err;
1858
Austin Boyle9eab0a72012-03-23 15:02:38 -07001859 if (chip->nvram_size) {
Alexandre Belloni409baf12018-02-12 23:47:23 +01001860 struct nvmem_config nvmem_cfg = {
1861 .name = "ds1307_nvram",
1862 .word_size = 1,
1863 .stride = 1,
1864 .size = chip->nvram_size,
1865 .reg_read = ds1307_nvram_read,
1866 .reg_write = ds1307_nvram_write,
1867 .priv = ds1307,
1868 };
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001869
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001870 ds1307->rtc->nvram_old_abi = true;
Alexandre Belloni409baf12018-02-12 23:47:23 +01001871 rtc_nvmem_register(ds1307->rtc, &nvmem_cfg);
David Brownell682d73f2007-11-14 16:58:32 -08001872 }
1873
Akinobu Mita445c0202016-01-25 00:22:16 +09001874 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001875 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001876
David Brownell1abb0dc2006-06-25 05:48:17 -07001877 return 0;
1878
Jingoo Hanedca66d2013-07-03 15:07:05 -07001879exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001880 return err;
1881}
1882
David Brownell1abb0dc2006-06-25 05:48:17 -07001883static struct i2c_driver ds1307_driver = {
1884 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001885 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001886 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001887 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001888 },
David Brownellc065f352007-07-17 04:05:10 -07001889 .probe = ds1307_probe,
Jean Delvare3760f732008-04-29 23:11:40 +02001890 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001891};
1892
Axel Lin0abc9202012-03-23 15:02:31 -07001893module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001894
1895MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1896MODULE_LICENSE("GPL");