blob: 07530fe1da2a54c0cec799b27842b1b4204c8656 [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
Uwe Kleine-König3f929ca2019-01-25 15:35:58 +0100557 ctl[0] &= RX8130_REG_EXTENSION_WADA;
558 ctl[1] &= ~RX8130_REG_FLAG_AF;
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100559 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
Uwe Kleine-König3f929ca2019-01-25 15:35:58 +0100581 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, ctl[2]);
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100582}
583
584static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
585{
586 struct ds1307 *ds1307 = dev_get_drvdata(dev);
587 int ret, reg;
588
589 if (!test_bit(HAS_ALARM, &ds1307->flags))
590 return -EINVAL;
591
592 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
593 if (ret < 0)
594 return ret;
595
596 if (enabled)
597 reg |= RX8130_REG_CONTROL0_AIE;
598 else
599 reg &= ~RX8130_REG_CONTROL0_AIE;
600
601 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
602}
603
604static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
605{
606 struct ds1307 *ds1307 = dev_id;
607 struct mutex *lock = &ds1307->rtc->ops_lock;
608 int reg, ret;
609
610 mutex_lock(lock);
611
612 /* Check and clear alarm 0 interrupt flag. */
613 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
614 if (ret)
615 goto out;
616 if (!(reg & MCP794XX_BIT_ALMX_IF))
617 goto out;
618 reg &= ~MCP794XX_BIT_ALMX_IF;
619 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
620 if (ret)
621 goto out;
622
623 /* Disable alarm 0. */
624 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
625 MCP794XX_BIT_ALM0_EN, 0);
626 if (ret)
627 goto out;
628
629 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
630
631out:
632 mutex_unlock(lock);
633
634 return IRQ_HANDLED;
635}
636
637static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
638{
639 struct ds1307 *ds1307 = dev_get_drvdata(dev);
640 u8 regs[10];
641 int ret;
642
643 if (!test_bit(HAS_ALARM, &ds1307->flags))
644 return -EINVAL;
645
646 /* Read control and alarm 0 registers. */
647 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
648 sizeof(regs));
649 if (ret)
650 return ret;
651
652 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
653
654 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
655 t->time.tm_sec = bcd2bin(regs[3] & 0x7f);
656 t->time.tm_min = bcd2bin(regs[4] & 0x7f);
657 t->time.tm_hour = bcd2bin(regs[5] & 0x3f);
658 t->time.tm_wday = bcd2bin(regs[6] & 0x7) - 1;
659 t->time.tm_mday = bcd2bin(regs[7] & 0x3f);
660 t->time.tm_mon = bcd2bin(regs[8] & 0x1f) - 1;
661 t->time.tm_year = -1;
662 t->time.tm_yday = -1;
663 t->time.tm_isdst = -1;
664
665 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
666 "enabled=%d polarity=%d irq=%d match=%lu\n", __func__,
667 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
668 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
669 !!(regs[6] & MCP794XX_BIT_ALMX_POL),
670 !!(regs[6] & MCP794XX_BIT_ALMX_IF),
671 (regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
672
673 return 0;
674}
675
676/*
677 * We may have a random RTC weekday, therefore calculate alarm weekday based
678 * on current weekday we read from the RTC timekeeping regs
679 */
680static int mcp794xx_alm_weekday(struct device *dev, struct rtc_time *tm_alarm)
681{
682 struct rtc_time tm_now;
683 int days_now, days_alarm, ret;
684
685 ret = ds1307_get_time(dev, &tm_now);
686 if (ret)
687 return ret;
688
689 days_now = div_s64(rtc_tm_to_time64(&tm_now), 24 * 60 * 60);
690 days_alarm = div_s64(rtc_tm_to_time64(tm_alarm), 24 * 60 * 60);
691
692 return (tm_now.tm_wday + days_alarm - days_now) % 7 + 1;
693}
694
695static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
696{
697 struct ds1307 *ds1307 = dev_get_drvdata(dev);
698 unsigned char regs[10];
699 int wday, ret;
700
701 if (!test_bit(HAS_ALARM, &ds1307->flags))
702 return -EINVAL;
703
704 wday = mcp794xx_alm_weekday(dev, &t->time);
705 if (wday < 0)
706 return wday;
707
708 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
709 "enabled=%d pending=%d\n", __func__,
710 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
711 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
712 t->enabled, t->pending);
713
714 /* Read control and alarm 0 registers. */
715 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
716 sizeof(regs));
717 if (ret)
718 return ret;
719
720 /* Set alarm 0, using 24-hour and day-of-month modes. */
721 regs[3] = bin2bcd(t->time.tm_sec);
722 regs[4] = bin2bcd(t->time.tm_min);
723 regs[5] = bin2bcd(t->time.tm_hour);
724 regs[6] = wday;
725 regs[7] = bin2bcd(t->time.tm_mday);
726 regs[8] = bin2bcd(t->time.tm_mon + 1);
727
728 /* Clear the alarm 0 interrupt flag. */
729 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
730 /* Set alarm match: second, minute, hour, day, date, month. */
731 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
732 /* Disable interrupt. We will not enable until completely programmed */
733 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
734
735 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
736 sizeof(regs));
737 if (ret)
738 return ret;
739
740 if (!t->enabled)
741 return 0;
742 regs[0] |= MCP794XX_BIT_ALM0_EN;
743 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
744}
745
746static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
747{
748 struct ds1307 *ds1307 = dev_get_drvdata(dev);
749
750 if (!test_bit(HAS_ALARM, &ds1307->flags))
751 return -EINVAL;
752
753 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
754 MCP794XX_BIT_ALM0_EN,
755 enabled ? MCP794XX_BIT_ALM0_EN : 0);
756}
757
758static int m41txx_rtc_read_offset(struct device *dev, long *offset)
759{
760 struct ds1307 *ds1307 = dev_get_drvdata(dev);
761 unsigned int ctrl_reg;
762 u8 val;
763
764 regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
765
766 val = ctrl_reg & M41TXX_M_CALIBRATION;
767
768 /* check if positive */
769 if (ctrl_reg & M41TXX_BIT_CALIB_SIGN)
770 *offset = (val * M41TXX_POS_OFFSET_STEP_PPB);
771 else
772 *offset = -(val * M41TXX_NEG_OFFSET_STEP_PPB);
773
774 return 0;
775}
776
777static int m41txx_rtc_set_offset(struct device *dev, long offset)
778{
779 struct ds1307 *ds1307 = dev_get_drvdata(dev);
780 unsigned int ctrl_reg;
781
782 if ((offset < M41TXX_MIN_OFFSET) || (offset > M41TXX_MAX_OFFSET))
783 return -ERANGE;
784
785 if (offset >= 0) {
786 ctrl_reg = DIV_ROUND_CLOSEST(offset,
787 M41TXX_POS_OFFSET_STEP_PPB);
788 ctrl_reg |= M41TXX_BIT_CALIB_SIGN;
789 } else {
790 ctrl_reg = DIV_ROUND_CLOSEST(abs(offset),
791 M41TXX_NEG_OFFSET_STEP_PPB);
792 }
793
794 return regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL,
795 M41TXX_M_CALIBRATION | M41TXX_BIT_CALIB_SIGN,
796 ctrl_reg);
797}
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700798
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200799static const struct rtc_class_ops rx8130_rtc_ops = {
800 .read_time = ds1307_get_time,
801 .set_time = ds1307_set_time,
802 .read_alarm = rx8130_read_alarm,
803 .set_alarm = rx8130_set_alarm,
804 .alarm_irq_enable = rx8130_alarm_irq_enable,
805};
806
807static const struct rtc_class_ops mcp794xx_rtc_ops = {
808 .read_time = ds1307_get_time,
809 .set_time = ds1307_set_time,
810 .read_alarm = mcp794xx_read_alarm,
811 .set_alarm = mcp794xx_set_alarm,
812 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
813};
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700814
Giulio Benetti79230ff2018-07-25 19:26:04 +0200815static const struct rtc_class_ops m41txx_rtc_ops = {
816 .read_time = ds1307_get_time,
817 .set_time = ds1307_set_time,
818 .read_alarm = ds1337_read_alarm,
819 .set_alarm = ds1337_set_alarm,
820 .alarm_irq_enable = ds1307_alarm_irq_enable,
821 .read_offset = m41txx_rtc_read_offset,
822 .set_offset = m41txx_rtc_set_offset,
823};
824
Heiner Kallweit7624df42017-07-12 07:49:33 +0200825static const struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700826 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700827 .nvram_offset = 8,
828 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700829 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200830 [ds_1308] = {
831 .nvram_offset = 8,
832 .nvram_size = 56,
833 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700834 [ds_1337] = {
835 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200836 .century_reg = DS1307_REG_MONTH,
837 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700838 },
839 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700840 .nvram_offset = 8,
841 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700842 },
843 [ds_1339] = {
844 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200845 .century_reg = DS1307_REG_MONTH,
846 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200847 .bbsqi_bit = DS1339_BIT_BBSQI,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700848 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700849 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700850 },
851 [ds_1340] = {
Heiner Kallweite48585d2017-06-05 17:57:33 +0200852 .century_reg = DS1307_REG_HOUR,
853 .century_enable_bit = DS1340_BIT_CENTURY_EN,
854 .century_bit = DS1340_BIT_CENTURY,
Andrea Greco51ed73eb2018-04-20 11:34:02 +0200855 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700856 .trickle_charger_reg = 0x08,
857 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300858 [ds_1341] = {
859 .century_reg = DS1307_REG_MONTH,
860 .century_bit = DS1337_BIT_CENTURY,
861 },
Wolfram Sangeb86c302012-05-29 15:07:38 -0700862 [ds_1388] = {
Heiner Kallweite5531702017-07-12 07:49:47 +0200863 .offset = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700864 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700865 },
866 [ds_3231] = {
867 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200868 .century_reg = DS1307_REG_MONTH,
869 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200870 .bbsqi_bit = DS3231_BIT_BBSQW,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700871 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200872 [rx_8130] = {
873 .alarm = 1,
874 /* this is battery backed SRAM */
875 .nvram_offset = 0x20,
876 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
Heiner Kallweite5531702017-07-12 07:49:47 +0200877 .offset = 0x10,
Heiner Kallweit45947122017-07-12 07:49:41 +0200878 .irq_handler = rx8130_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200879 .rtc_ops = &rx8130_rtc_ops,
Marek Vasutee0981b2017-06-18 22:55:28 +0200880 },
Giulio Benetti79230ff2018-07-25 19:26:04 +0200881 [m41t0] = {
882 .rtc_ops = &m41txx_rtc_ops,
883 },
884 [m41t00] = {
885 .rtc_ops = &m41txx_rtc_ops,
886 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200887 [m41t11] = {
888 /* this is battery backed SRAM */
889 .nvram_offset = 8,
890 .nvram_size = 56,
Giulio Benetti79230ff2018-07-25 19:26:04 +0200891 .rtc_ops = &m41txx_rtc_ops,
Giulio Benetti7e580762018-05-16 23:08:40 +0200892 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800893 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700894 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700895 /* this is battery backed SRAM */
896 .nvram_offset = 0x20,
897 .nvram_size = 0x40,
Heiner Kallweit45947122017-07-12 07:49:41 +0200898 .irq_handler = mcp794xx_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200899 .rtc_ops = &mcp794xx_rtc_ops,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700900 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700901};
David Brownell045e0e82007-07-17 04:04:55 -0700902
Jean Delvare3760f732008-04-29 23:11:40 +0200903static const struct i2c_device_id ds1307_id[] = {
904 { "ds1307", ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200905 { "ds1308", ds_1308 },
Jean Delvare3760f732008-04-29 23:11:40 +0200906 { "ds1337", ds_1337 },
907 { "ds1338", ds_1338 },
908 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700909 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200910 { "ds1340", ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300911 { "ds1341", ds_1341 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700912 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700913 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200914 { "m41t00", m41t00 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200915 { "m41t11", m41t11 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800916 { "mcp7940x", mcp794xx },
917 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700918 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700919 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200920 { "isl12057", ds_1337 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200921 { "rx8130", rx_8130 },
Jean Delvare3760f732008-04-29 23:11:40 +0200922 { }
923};
924MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700925
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300926#ifdef CONFIG_OF
927static const struct of_device_id ds1307_of_match[] = {
928 {
929 .compatible = "dallas,ds1307",
930 .data = (void *)ds_1307
931 },
932 {
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200933 .compatible = "dallas,ds1308",
934 .data = (void *)ds_1308
935 },
936 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300937 .compatible = "dallas,ds1337",
938 .data = (void *)ds_1337
939 },
940 {
941 .compatible = "dallas,ds1338",
942 .data = (void *)ds_1338
943 },
944 {
945 .compatible = "dallas,ds1339",
946 .data = (void *)ds_1339
947 },
948 {
949 .compatible = "dallas,ds1388",
950 .data = (void *)ds_1388
951 },
952 {
953 .compatible = "dallas,ds1340",
954 .data = (void *)ds_1340
955 },
956 {
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300957 .compatible = "dallas,ds1341",
958 .data = (void *)ds_1341
959 },
960 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300961 .compatible = "maxim,ds3231",
962 .data = (void *)ds_3231
963 },
964 {
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200965 .compatible = "st,m41t0",
Giulio Benetti146a5522018-05-16 23:08:39 +0200966 .data = (void *)m41t0
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200967 },
968 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300969 .compatible = "st,m41t00",
970 .data = (void *)m41t00
971 },
972 {
Giulio Benetti7e580762018-05-16 23:08:40 +0200973 .compatible = "st,m41t11",
974 .data = (void *)m41t11
975 },
976 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300977 .compatible = "microchip,mcp7940x",
978 .data = (void *)mcp794xx
979 },
980 {
981 .compatible = "microchip,mcp7941x",
982 .data = (void *)mcp794xx
983 },
984 {
985 .compatible = "pericom,pt7c4338",
986 .data = (void *)ds_1307
987 },
988 {
989 .compatible = "epson,rx8025",
990 .data = (void *)rx_8025
991 },
992 {
993 .compatible = "isil,isl12057",
994 .data = (void *)ds_1337
995 },
Bastian Stender47dd4722017-10-17 14:46:07 +0200996 {
997 .compatible = "epson,rx8130",
998 .data = (void *)rx_8130
999 },
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001000 { }
1001};
1002MODULE_DEVICE_TABLE(of, ds1307_of_match);
1003#endif
1004
Tin Huynh9c19b892016-11-30 09:57:31 +07001005#ifdef CONFIG_ACPI
1006static const struct acpi_device_id ds1307_acpi_ids[] = {
1007 { .id = "DS1307", .driver_data = ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001008 { .id = "DS1308", .driver_data = ds_1308 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001009 { .id = "DS1337", .driver_data = ds_1337 },
1010 { .id = "DS1338", .driver_data = ds_1338 },
1011 { .id = "DS1339", .driver_data = ds_1339 },
1012 { .id = "DS1388", .driver_data = ds_1388 },
1013 { .id = "DS1340", .driver_data = ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +03001014 { .id = "DS1341", .driver_data = ds_1341 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001015 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -07001016 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001017 { .id = "M41T00", .driver_data = m41t00 },
Giulio Benetti7e580762018-05-16 23:08:40 +02001018 { .id = "M41T11", .driver_data = m41t11 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001019 { .id = "MCP7940X", .driver_data = mcp794xx },
1020 { .id = "MCP7941X", .driver_data = mcp794xx },
1021 { .id = "PT7C4338", .driver_data = ds_1307 },
1022 { .id = "RX8025", .driver_data = rx_8025 },
1023 { .id = "ISL12057", .driver_data = ds_1337 },
Bastian Stender47dd4722017-10-17 14:46:07 +02001024 { .id = "RX8130", .driver_data = rx_8130 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001025 { }
1026};
1027MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
1028#endif
1029
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001030/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001031 * The ds1337 and ds1339 both have two alarms, but we only use the first
1032 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
1033 * signal; ds1339 chips have only one alarm signal.
1034 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001035static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001036{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001037 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001038 struct mutex *lock = &ds1307->rtc->ops_lock;
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001039 int stat, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001040
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001041 mutex_lock(lock);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001042 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
1043 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001044 goto out;
1045
1046 if (stat & DS1337_BIT_A1I) {
1047 stat &= ~DS1337_BIT_A1I;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001048 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001049
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001050 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1051 DS1337_BIT_A1IE, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001052 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001053 goto out;
1054
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001055 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001056 }
1057
1058out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001059 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001060
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001061 return IRQ_HANDLED;
1062}
1063
1064/*----------------------------------------------------------------------*/
1065
David Brownellff8371a2006-09-30 23:28:17 -07001066static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -07001067 .read_time = ds1307_get_time,
1068 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -08001069 .read_alarm = ds1337_read_alarm,
1070 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -08001071 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -07001072};
1073
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001074static ssize_t frequency_test_store(struct device *dev,
1075 struct device_attribute *attr,
1076 const char *buf, size_t count)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001077{
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001078 struct ds1307 *ds1307 = dev_get_drvdata(dev->parent);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001079 bool freq_test_en;
1080 int ret;
1081
1082 ret = kstrtobool(buf, &freq_test_en);
1083 if (ret) {
1084 dev_err(dev, "Failed to store RTC Frequency Test attribute\n");
1085 return ret;
1086 }
1087
1088 regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL, M41TXX_BIT_FT,
1089 freq_test_en ? M41TXX_BIT_FT : 0);
1090
1091 return count;
1092}
1093
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001094static ssize_t frequency_test_show(struct device *dev,
1095 struct device_attribute *attr,
1096 char *buf)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001097{
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001098 struct ds1307 *ds1307 = dev_get_drvdata(dev->parent);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001099 unsigned int ctrl_reg;
1100
1101 regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
1102
1103 return scnprintf(buf, PAGE_SIZE, (ctrl_reg & M41TXX_BIT_FT) ? "on\n" :
1104 "off\n");
1105}
1106
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001107static DEVICE_ATTR_RW(frequency_test);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001108
1109static struct attribute *rtc_freq_test_attrs[] = {
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001110 &dev_attr_frequency_test.attr,
Giulio Benettib41c23e2018-07-25 19:26:05 +02001111 NULL,
1112};
1113
1114static const struct attribute_group rtc_freq_test_attr_group = {
1115 .attrs = rtc_freq_test_attrs,
1116};
1117
Giulio Benettib41c23e2018-07-25 19:26:05 +02001118static int ds1307_add_frequency_test(struct ds1307 *ds1307)
1119{
1120 int err;
1121
1122 switch (ds1307->type) {
1123 case m41t0:
1124 case m41t00:
1125 case m41t11:
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001126 err = rtc_add_group(ds1307->rtc, &rtc_freq_test_attr_group);
1127 if (err)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001128 return err;
Giulio Benettib41c23e2018-07-25 19:26:05 +02001129 break;
1130 default:
1131 break;
1132 }
1133
1134 return 0;
1135}
1136
Simon Guinot1d1945d2014-04-03 14:49:55 -07001137/*----------------------------------------------------------------------*/
1138
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001139static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
1140 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -08001141{
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001142 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +02001143 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -08001144
Heiner Kallweit969fa072017-07-12 07:49:54 +02001145 return regmap_bulk_read(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001146 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -08001147}
1148
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001149static int ds1307_nvram_write(void *priv, unsigned int offset, void *val,
1150 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -08001151{
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001152 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +02001153 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -08001154
Heiner Kallweit969fa072017-07-12 07:49:54 +02001155 return regmap_bulk_write(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001156 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -08001157}
1158
David Brownell682d73f2007-11-14 16:58:32 -08001159/*----------------------------------------------------------------------*/
1160
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001161static u8 ds1307_trickle_init(struct ds1307 *ds1307,
Heiner Kallweit7624df42017-07-12 07:49:33 +02001162 const struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001163{
Alexandre Belloni57ec2d92017-09-04 22:46:04 +02001164 u32 ohms;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001165 bool diode = true;
1166
1167 if (!chip->do_trickle_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001168 return 0;
1169
Heiner Kallweit11e58902017-03-10 18:52:34 +01001170 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
1171 &ohms))
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001172 return 0;
1173
Heiner Kallweit11e58902017-03-10 18:52:34 +01001174 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001175 diode = false;
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001176
1177 return chip->do_trickle_setup(ds1307, ohms, diode);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001178}
1179
Akinobu Mita445c0202016-01-25 00:22:16 +09001180/*----------------------------------------------------------------------*/
1181
Heiner Kallweit6b583a62017-09-27 22:41:26 +02001182#if IS_REACHABLE(CONFIG_HWMON)
Akinobu Mita445c0202016-01-25 00:22:16 +09001183
1184/*
1185 * Temperature sensor support for ds3231 devices.
1186 */
1187
1188#define DS3231_REG_TEMPERATURE 0x11
1189
1190/*
1191 * A user-initiated temperature conversion is not started by this function,
1192 * so the temperature is updated once every 64 seconds.
1193 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001194static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +09001195{
1196 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1197 u8 temp_buf[2];
1198 s16 temp;
1199 int ret;
1200
Heiner Kallweit11e58902017-03-10 18:52:34 +01001201 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
1202 temp_buf, sizeof(temp_buf));
1203 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +09001204 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +09001205 /*
1206 * Temperature is represented as a 10-bit code with a resolution of
1207 * 0.25 degree celsius and encoded in two's complement format.
1208 */
1209 temp = (temp_buf[0] << 8) | temp_buf[1];
1210 temp >>= 6;
1211 *mC = temp * 250;
1212
1213 return 0;
1214}
1215
1216static ssize_t ds3231_hwmon_show_temp(struct device *dev,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001217 struct device_attribute *attr, char *buf)
Akinobu Mita445c0202016-01-25 00:22:16 +09001218{
1219 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001220 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001221
1222 ret = ds3231_hwmon_read_temp(dev, &temp);
1223 if (ret)
1224 return ret;
1225
1226 return sprintf(buf, "%d\n", temp);
1227}
Alexandre Bellonib4be2712017-09-04 22:46:08 +02001228static SENSOR_DEVICE_ATTR(temp1_input, 0444, ds3231_hwmon_show_temp,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001229 NULL, 0);
Akinobu Mita445c0202016-01-25 00:22:16 +09001230
1231static struct attribute *ds3231_hwmon_attrs[] = {
1232 &sensor_dev_attr_temp1_input.dev_attr.attr,
1233 NULL,
1234};
1235ATTRIBUTE_GROUPS(ds3231_hwmon);
1236
1237static void ds1307_hwmon_register(struct ds1307 *ds1307)
1238{
1239 struct device *dev;
1240
1241 if (ds1307->type != ds_3231)
1242 return;
1243
Heiner Kallweit11e58902017-03-10 18:52:34 +01001244 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001245 ds1307,
1246 ds3231_hwmon_groups);
Akinobu Mita445c0202016-01-25 00:22:16 +09001247 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001248 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1249 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001250 }
1251}
1252
1253#else
1254
1255static void ds1307_hwmon_register(struct ds1307 *ds1307)
1256{
1257}
1258
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001259#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1260
1261/*----------------------------------------------------------------------*/
1262
1263/*
1264 * Square-wave output support for DS3231
1265 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1266 */
1267#ifdef CONFIG_COMMON_CLK
1268
1269enum {
1270 DS3231_CLK_SQW = 0,
1271 DS3231_CLK_32KHZ,
1272};
1273
1274#define clk_sqw_to_ds1307(clk) \
1275 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1276#define clk_32khz_to_ds1307(clk) \
1277 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1278
1279static int ds3231_clk_sqw_rates[] = {
1280 1,
1281 1024,
1282 4096,
1283 8192,
1284};
1285
1286static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1287{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001288 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001289 int ret;
1290
1291 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001292 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1293 mask, value);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001294 mutex_unlock(lock);
1295
1296 return ret;
1297}
1298
1299static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1300 unsigned long parent_rate)
1301{
1302 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001303 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001304 int rate_sel = 0;
1305
Heiner Kallweit11e58902017-03-10 18:52:34 +01001306 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1307 if (ret)
1308 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001309 if (control & DS1337_BIT_RS1)
1310 rate_sel += 1;
1311 if (control & DS1337_BIT_RS2)
1312 rate_sel += 2;
1313
1314 return ds3231_clk_sqw_rates[rate_sel];
1315}
1316
1317static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001318 unsigned long *prate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001319{
1320 int i;
1321
1322 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1323 if (ds3231_clk_sqw_rates[i] <= rate)
1324 return ds3231_clk_sqw_rates[i];
1325 }
1326
1327 return 0;
1328}
1329
1330static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001331 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001332{
1333 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1334 int control = 0;
1335 int rate_sel;
1336
1337 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1338 rate_sel++) {
1339 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1340 break;
1341 }
1342
1343 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1344 return -EINVAL;
1345
1346 if (rate_sel & 1)
1347 control |= DS1337_BIT_RS1;
1348 if (rate_sel & 2)
1349 control |= DS1337_BIT_RS2;
1350
1351 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1352 control);
1353}
1354
1355static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1356{
1357 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1358
1359 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1360}
1361
1362static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1363{
1364 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1365
1366 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1367}
1368
1369static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1370{
1371 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001372 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001373
Heiner Kallweit11e58902017-03-10 18:52:34 +01001374 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1375 if (ret)
1376 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001377
1378 return !(control & DS1337_BIT_INTCN);
1379}
1380
1381static const struct clk_ops ds3231_clk_sqw_ops = {
1382 .prepare = ds3231_clk_sqw_prepare,
1383 .unprepare = ds3231_clk_sqw_unprepare,
1384 .is_prepared = ds3231_clk_sqw_is_prepared,
1385 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1386 .round_rate = ds3231_clk_sqw_round_rate,
1387 .set_rate = ds3231_clk_sqw_set_rate,
1388};
1389
1390static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001391 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001392{
1393 return 32768;
1394}
1395
1396static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1397{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001398 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001399 int ret;
1400
1401 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001402 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1403 DS3231_BIT_EN32KHZ,
1404 enable ? DS3231_BIT_EN32KHZ : 0);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001405 mutex_unlock(lock);
1406
1407 return ret;
1408}
1409
1410static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1411{
1412 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1413
1414 return ds3231_clk_32khz_control(ds1307, true);
1415}
1416
1417static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1418{
1419 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1420
1421 ds3231_clk_32khz_control(ds1307, false);
1422}
1423
1424static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1425{
1426 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001427 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001428
Heiner Kallweit11e58902017-03-10 18:52:34 +01001429 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1430 if (ret)
1431 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001432
1433 return !!(status & DS3231_BIT_EN32KHZ);
1434}
1435
1436static const struct clk_ops ds3231_clk_32khz_ops = {
1437 .prepare = ds3231_clk_32khz_prepare,
1438 .unprepare = ds3231_clk_32khz_unprepare,
1439 .is_prepared = ds3231_clk_32khz_is_prepared,
1440 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1441};
1442
1443static struct clk_init_data ds3231_clks_init[] = {
1444 [DS3231_CLK_SQW] = {
1445 .name = "ds3231_clk_sqw",
1446 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001447 },
1448 [DS3231_CLK_32KHZ] = {
1449 .name = "ds3231_clk_32khz",
1450 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001451 },
1452};
1453
1454static int ds3231_clks_register(struct ds1307 *ds1307)
1455{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001456 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001457 struct clk_onecell_data *onecell;
1458 int i;
1459
Heiner Kallweit11e58902017-03-10 18:52:34 +01001460 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001461 if (!onecell)
1462 return -ENOMEM;
1463
1464 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001465 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1466 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001467 if (!onecell->clks)
1468 return -ENOMEM;
1469
1470 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1471 struct clk_init_data init = ds3231_clks_init[i];
1472
1473 /*
1474 * Interrupt signal due to alarm conditions and square-wave
1475 * output share same pin, so don't initialize both.
1476 */
1477 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1478 continue;
1479
1480 /* optional override of the clockname */
1481 of_property_read_string_index(node, "clock-output-names", i,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001482 &init.name);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001483 ds1307->clks[i].init = &init;
1484
Heiner Kallweit11e58902017-03-10 18:52:34 +01001485 onecell->clks[i] = devm_clk_register(ds1307->dev,
1486 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001487 if (IS_ERR(onecell->clks[i]))
1488 return PTR_ERR(onecell->clks[i]);
1489 }
1490
1491 if (!node)
1492 return 0;
1493
1494 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1495
1496 return 0;
1497}
1498
1499static void ds1307_clks_register(struct ds1307 *ds1307)
1500{
1501 int ret;
1502
1503 if (ds1307->type != ds_3231)
1504 return;
1505
1506 ret = ds3231_clks_register(ds1307);
1507 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001508 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1509 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001510 }
1511}
1512
1513#else
1514
1515static void ds1307_clks_register(struct ds1307 *ds1307)
1516{
1517}
1518
1519#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001520
Heiner Kallweit11e58902017-03-10 18:52:34 +01001521static const struct regmap_config regmap_config = {
1522 .reg_bits = 8,
1523 .val_bits = 8,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001524};
1525
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001526static int ds1307_probe(struct i2c_client *client,
1527 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001528{
1529 struct ds1307 *ds1307;
1530 int err = -ENODEV;
Heiner Kallweit584ce302017-08-29 21:52:56 +02001531 int tmp;
Heiner Kallweit7624df42017-07-12 07:49:33 +02001532 const struct chip_desc *chip;
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001533 bool want_irq;
Michael Lange8bc2a402016-01-21 18:10:16 +01001534 bool ds1307_can_wakeup_device = false;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001535 unsigned char regs[8];
Jingoo Han01ce8932013-11-12 15:10:41 -08001536 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001537 u8 trickle_charger_setup = 0;
David Brownell1abb0dc2006-06-25 05:48:17 -07001538
Jingoo Hanedca66d2013-07-03 15:07:05 -07001539 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001540 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001541 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001542
Heiner Kallweit11e58902017-03-10 18:52:34 +01001543 dev_set_drvdata(&client->dev, ds1307);
1544 ds1307->dev = &client->dev;
1545 ds1307->name = client->name;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001546
Heiner Kallweit11e58902017-03-10 18:52:34 +01001547 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1548 if (IS_ERR(ds1307->regmap)) {
1549 dev_err(ds1307->dev, "regmap allocation failed\n");
1550 return PTR_ERR(ds1307->regmap);
1551 }
1552
1553 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001554
1555 if (client->dev.of_node) {
1556 ds1307->type = (enum ds_type)
1557 of_device_get_match_data(&client->dev);
1558 chip = &chips[ds1307->type];
1559 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001560 chip = &chips[id->driver_data];
1561 ds1307->type = id->driver_data;
1562 } else {
1563 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001564
Tin Huynh9c19b892016-11-30 09:57:31 +07001565 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001566 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001567 if (!acpi_id)
1568 return -ENODEV;
1569 chip = &chips[acpi_id->driver_data];
1570 ds1307->type = acpi_id->driver_data;
1571 }
1572
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001573 want_irq = client->irq > 0 && chip->alarm;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001574
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001575 if (!pdata)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001576 trickle_charger_setup = ds1307_trickle_init(ds1307, chip);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001577 else if (pdata->trickle_charger_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001578 trickle_charger_setup = pdata->trickle_charger_setup;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001579
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001580 if (trickle_charger_setup && chip->trickle_charger_reg) {
1581 trickle_charger_setup |= DS13XX_TRICKLE_CHARGER_MAGIC;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001582 dev_dbg(ds1307->dev,
1583 "writing trickle charger info 0x%x to 0x%x\n",
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001584 trickle_charger_setup, chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001585 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001586 trickle_charger_setup);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001587 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001588
Michael Lange8bc2a402016-01-21 18:10:16 +01001589#ifdef CONFIG_OF
1590/*
1591 * For devices with no IRQ directly connected to the SoC, the RTC chip
1592 * can be forced as a wakeup source by stating that explicitly in
1593 * the device's .dts file using the "wakeup-source" boolean property.
1594 * If the "wakeup-source" property is set, don't request an IRQ.
1595 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1596 * if supported by the RTC.
1597 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001598 if (chip->alarm && of_property_read_bool(client->dev.of_node,
1599 "wakeup-source"))
Michael Lange8bc2a402016-01-21 18:10:16 +01001600 ds1307_can_wakeup_device = true;
Michael Lange8bc2a402016-01-21 18:10:16 +01001601#endif
1602
David Brownell045e0e82007-07-17 04:04:55 -07001603 switch (ds1307->type) {
1604 case ds_1337:
1605 case ds_1339:
Nikita Yushchenko0759c882017-08-24 09:32:11 +03001606 case ds_1341:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001607 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001608 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001609 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001610 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001611 if (err) {
1612 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001613 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001614 }
1615
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001616 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001617 if (regs[0] & DS1337_BIT_nEOSC)
1618 regs[0] &= ~DS1337_BIT_nEOSC;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001619
David Anders40ce9722012-03-23 15:02:37 -07001620 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001621 * Using IRQ or defined as wakeup-source?
1622 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001623 * For some variants, be sure alarms can trigger when we're
1624 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001625 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001626 if (want_irq || ds1307_can_wakeup_device) {
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001627 regs[0] |= DS1337_BIT_INTCN | chip->bbsqi_bit;
1628 regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001629 }
1630
Heiner Kallweit11e58902017-03-10 18:52:34 +01001631 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001632 regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001633
1634 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001635 if (regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001636 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001637 regs[1] & ~DS1337_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001638 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001639 }
David Brownell045e0e82007-07-17 04:04:55 -07001640 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001641
1642 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001643 err = regmap_bulk_read(ds1307->regmap,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001644 RX8025_REG_CTRL1 << 4 | 0x08, regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001645 if (err) {
1646 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001647 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001648 }
1649
1650 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001651 if (!(regs[1] & RX8025_BIT_XST)) {
1652 regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001653 regmap_write(ds1307->regmap,
1654 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001655 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001656 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001657 "oscillator stop detected - SET TIME!\n");
1658 }
1659
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001660 if (regs[1] & RX8025_BIT_PON) {
1661 regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001662 regmap_write(ds1307->regmap,
1663 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001664 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001665 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001666 }
1667
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001668 if (regs[1] & RX8025_BIT_VDET) {
1669 regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001670 regmap_write(ds1307->regmap,
1671 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001672 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001673 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001674 }
1675
1676 /* make sure we are running in 24hour mode */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001677 if (!(regs[0] & RX8025_BIT_2412)) {
Matthias Fuchsa2166852009-03-31 15:24:58 -07001678 u8 hour;
1679
1680 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001681 regmap_write(ds1307->regmap,
1682 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001683 regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001684
Heiner Kallweit11e58902017-03-10 18:52:34 +01001685 err = regmap_bulk_read(ds1307->regmap,
1686 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001687 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001688 if (err) {
1689 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001690 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001691 }
1692
1693 /* correct hour */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001694 hour = bcd2bin(regs[DS1307_REG_HOUR]);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001695 if (hour == 12)
1696 hour = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001697 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
Matthias Fuchsa2166852009-03-31 15:24:58 -07001698 hour += 12;
1699
Heiner Kallweit11e58902017-03-10 18:52:34 +01001700 regmap_write(ds1307->regmap,
1701 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001702 }
1703 break;
David Brownell045e0e82007-07-17 04:04:55 -07001704 default:
1705 break;
1706 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001707
1708read_rtc:
1709 /* read RTC registers */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001710 err = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
1711 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +01001712 if (err) {
1713 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001714 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001715 }
1716
David Anders40ce9722012-03-23 15:02:37 -07001717 /*
1718 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001719 * specify the extra bits as must-be-zero, but there are
1720 * still a few values that are clearly out-of-range.
1721 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001722 tmp = regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001723 switch (ds1307->type) {
1724 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001725 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001726 case m41t00:
Giulio Benetti7e580762018-05-16 23:08:40 +02001727 case m41t11:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001728 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001729 if (tmp & DS1307_BIT_CH) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001730 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1731 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001732 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001733 }
David Brownell045e0e82007-07-17 04:04:55 -07001734 break;
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001735 case ds_1308:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001736 case ds_1338:
1737 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001738 if (tmp & DS1307_BIT_CH)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001739 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001740
1741 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001742 if (regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001743 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001744 regs[DS1307_REG_CONTROL] &
1745 ~DS1338_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001746 dev_warn(ds1307->dev, "SET TIME!\n");
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001747 goto read_rtc;
1748 }
David Brownell045e0e82007-07-17 04:04:55 -07001749 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001750 case ds_1340:
1751 /* clock halted? turn it on, so clock can tick. */
1752 if (tmp & DS1340_BIT_nEOSC)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001753 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
frederic Rodofcd8db02008-02-06 01:38:55 -08001754
Heiner Kallweit11e58902017-03-10 18:52:34 +01001755 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1756 if (err) {
1757 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001758 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001759 }
1760
1761 /* oscillator fault? clear flag, and warn */
1762 if (tmp & DS1340_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001763 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1764 dev_warn(ds1307->dev, "SET TIME!\n");
frederic Rodofcd8db02008-02-06 01:38:55 -08001765 }
1766 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001767 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001768 /* make sure that the backup battery is enabled */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001769 if (!(regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001770 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001771 regs[DS1307_REG_WDAY] |
Heiner Kallweit11e58902017-03-10 18:52:34 +01001772 MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001773 }
1774
1775 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001776 if (!(tmp & MCP794XX_BIT_ST)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001777 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1778 MCP794XX_BIT_ST);
1779 dev_warn(ds1307->dev, "SET TIME!\n");
David Anders43fcb812011-11-02 13:37:53 -07001780 goto read_rtc;
1781 }
1782
1783 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001784 default:
David Brownell045e0e82007-07-17 04:04:55 -07001785 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001786 }
David Brownell045e0e82007-07-17 04:04:55 -07001787
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001788 tmp = regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001789 switch (ds1307->type) {
1790 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001791 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001792 case m41t00:
Giulio Benetti7e580762018-05-16 23:08:40 +02001793 case m41t11:
David Anders40ce9722012-03-23 15:02:37 -07001794 /*
1795 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001796 * systems that will run through year 2100.
1797 */
1798 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001799 case rx_8025:
1800 break;
David Brownellc065f352007-07-17 04:05:10 -07001801 default:
1802 if (!(tmp & DS1307_BIT_12HR))
1803 break;
1804
David Anders40ce9722012-03-23 15:02:37 -07001805 /*
1806 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001807 * take note...
1808 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001809 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001810 if (tmp == 12)
1811 tmp = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001812 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
David Brownellc065f352007-07-17 04:05:10 -07001813 tmp += 12;
Heiner Kallweite5531702017-07-12 07:49:47 +02001814 regmap_write(ds1307->regmap, chip->offset + DS1307_REG_HOUR,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001815 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001816 }
1817
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001818 if (want_irq || ds1307_can_wakeup_device) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001819 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001820 set_bit(HAS_ALARM, &ds1307->flags);
1821 }
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001822
1823 ds1307->rtc = devm_rtc_allocate_device(ds1307->dev);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001824 if (IS_ERR(ds1307->rtc))
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001825 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001826
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001827 if (ds1307_can_wakeup_device && !want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001828 dev_info(ds1307->dev,
1829 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001830 /* We cannot support UIE mode if we do not have an IRQ line */
1831 ds1307->rtc->uie_unsupported = 1;
1832 }
1833
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001834 if (want_irq) {
Heiner Kallweit45947122017-07-12 07:49:41 +02001835 err = devm_request_threaded_irq(ds1307->dev, client->irq, NULL,
1836 chip->irq_handler ?: ds1307_irq,
Nishanth Menonc5983192015-06-23 11:15:11 -05001837 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001838 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001839 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001840 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001841 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001842 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001843 dev_err(ds1307->dev, "unable to request IRQ!\n");
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001844 } else {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001845 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001846 }
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001847 }
1848
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001849 ds1307->rtc->ops = chip->rtc_ops ?: &ds13xx_rtc_ops;
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001850 err = ds1307_add_frequency_test(ds1307);
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001851 if (err)
1852 return err;
1853
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001854 err = rtc_register_device(ds1307->rtc);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001855 if (err)
1856 return err;
1857
Austin Boyle9eab0a72012-03-23 15:02:38 -07001858 if (chip->nvram_size) {
Alexandre Belloni409baf12018-02-12 23:47:23 +01001859 struct nvmem_config nvmem_cfg = {
1860 .name = "ds1307_nvram",
1861 .word_size = 1,
1862 .stride = 1,
1863 .size = chip->nvram_size,
1864 .reg_read = ds1307_nvram_read,
1865 .reg_write = ds1307_nvram_write,
1866 .priv = ds1307,
1867 };
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001868
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001869 ds1307->rtc->nvram_old_abi = true;
Alexandre Belloni409baf12018-02-12 23:47:23 +01001870 rtc_nvmem_register(ds1307->rtc, &nvmem_cfg);
David Brownell682d73f2007-11-14 16:58:32 -08001871 }
1872
Akinobu Mita445c0202016-01-25 00:22:16 +09001873 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001874 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001875
David Brownell1abb0dc2006-06-25 05:48:17 -07001876 return 0;
1877
Jingoo Hanedca66d2013-07-03 15:07:05 -07001878exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001879 return err;
1880}
1881
David Brownell1abb0dc2006-06-25 05:48:17 -07001882static struct i2c_driver ds1307_driver = {
1883 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001884 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001885 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001886 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001887 },
David Brownellc065f352007-07-17 04:05:10 -07001888 .probe = ds1307_probe,
Jean Delvare3760f732008-04-29 23:11:40 +02001889 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001890};
1891
Axel Lin0abc9202012-03-23 15:02:31 -07001892module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001893
1894MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1895MODULE_LICENSE("GPL");