blob: a4883d59d88def0d9d2d04c95b8d647528d1394a [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önig92cbf122019-01-25 15:35:54 +0100117#define RX8130_REG_ALARM_MIN 0x07
118#define RX8130_REG_ALARM_HOUR 0x08
119#define RX8130_REG_ALARM_WEEK_OR_DAY 0x09
120#define RX8130_REG_EXTENSION 0x0c
121#define RX8130_REG_EXTENSION_WADA BIT(3)
122#define RX8130_REG_FLAG 0x0d
123#define RX8130_REG_FLAG_AF BIT(3)
124#define RX8130_REG_CONTROL0 0x0e
125#define RX8130_REG_CONTROL0_AIE BIT(3)
126
127#define MCP794XX_REG_CONTROL 0x07
128# define MCP794XX_BIT_ALM0_EN 0x10
129# define MCP794XX_BIT_ALM1_EN 0x20
130#define MCP794XX_REG_ALARM0_BASE 0x0a
131#define MCP794XX_REG_ALARM0_CTRL 0x0d
132#define MCP794XX_REG_ALARM1_BASE 0x11
133#define MCP794XX_REG_ALARM1_CTRL 0x14
134# define MCP794XX_BIT_ALMX_IF BIT(3)
135# define MCP794XX_BIT_ALMX_C0 BIT(4)
136# define MCP794XX_BIT_ALMX_C1 BIT(5)
137# define MCP794XX_BIT_ALMX_C2 BIT(6)
138# define MCP794XX_BIT_ALMX_POL BIT(7)
139# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
140 MCP794XX_BIT_ALMX_C1 | \
141 MCP794XX_BIT_ALMX_C2)
142
Giulio Benetti79230ff2018-07-25 19:26:04 +0200143#define M41TXX_REG_CONTROL 0x07
144# define M41TXX_BIT_OUT BIT(7)
145# define M41TXX_BIT_FT BIT(6)
146# define M41TXX_BIT_CALIB_SIGN BIT(5)
147# define M41TXX_M_CALIBRATION GENMASK(4, 0)
148
149/* negative offset step is -2.034ppm */
150#define M41TXX_NEG_OFFSET_STEP_PPB 2034
151/* positive offset step is +4.068ppm */
152#define M41TXX_POS_OFFSET_STEP_PPB 4068
153/* Min and max values supported with 'offset' interface by M41TXX */
154#define M41TXX_MIN_OFFSET ((-31) * M41TXX_NEG_OFFSET_STEP_PPB)
155#define M41TXX_MAX_OFFSET ((31) * M41TXX_POS_OFFSET_STEP_PPB)
156
David Brownell1abb0dc2006-06-25 05:48:17 -0700157struct ds1307 {
David Brownell1abb0dc2006-06-25 05:48:17 -0700158 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700159 unsigned long flags;
160#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
161#define HAS_ALARM 1 /* bit 1 == irq claimed */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100162 struct device *dev;
163 struct regmap *regmap;
164 const char *name;
David Brownell1abb0dc2006-06-25 05:48:17 -0700165 struct rtc_device *rtc;
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900166#ifdef CONFIG_COMMON_CLK
167 struct clk_hw clks[2];
168#endif
David Brownell1abb0dc2006-06-25 05:48:17 -0700169};
170
David Brownell045e0e82007-07-17 04:04:55 -0700171struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700172 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700173 u16 nvram_offset;
174 u16 nvram_size;
Heiner Kallweite5531702017-07-12 07:49:47 +0200175 u8 offset; /* register's offset */
Heiner Kallweite48585d2017-06-05 17:57:33 +0200176 u8 century_reg;
177 u8 century_enable_bit;
178 u8 century_bit;
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200179 u8 bbsqi_bit;
Heiner Kallweit45947122017-07-12 07:49:41 +0200180 irq_handler_t irq_handler;
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200181 const struct rtc_class_ops *rtc_ops;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700182 u16 trickle_charger_reg;
Alexandre Belloni57ec2d92017-09-04 22:46:04 +0200183 u8 (*do_trickle_setup)(struct ds1307 *, u32,
Heiner Kallweit11e58902017-03-10 18:52:34 +0100184 bool);
David Brownell045e0e82007-07-17 04:04:55 -0700185};
186
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100187static const struct chip_desc chips[last_ds_type];
188
189static int ds1307_get_time(struct device *dev, struct rtc_time *t)
190{
191 struct ds1307 *ds1307 = dev_get_drvdata(dev);
192 int tmp, ret;
193 const struct chip_desc *chip = &chips[ds1307->type];
194 u8 regs[7];
195
196 /* read the RTC date and time registers all at once */
197 ret = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
198 sizeof(regs));
199 if (ret) {
200 dev_err(dev, "%s error %d\n", "read", ret);
201 return ret;
202 }
203
204 dev_dbg(dev, "%s: %7ph\n", "read", regs);
205
206 /* if oscillator fail bit is set, no data can be trusted */
207 if (ds1307->type == m41t0 &&
208 regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
209 dev_warn_once(dev, "oscillator failed, set time!\n");
210 return -EINVAL;
211 }
212
213 t->tm_sec = bcd2bin(regs[DS1307_REG_SECS] & 0x7f);
214 t->tm_min = bcd2bin(regs[DS1307_REG_MIN] & 0x7f);
215 tmp = regs[DS1307_REG_HOUR] & 0x3f;
216 t->tm_hour = bcd2bin(tmp);
217 t->tm_wday = bcd2bin(regs[DS1307_REG_WDAY] & 0x07) - 1;
218 t->tm_mday = bcd2bin(regs[DS1307_REG_MDAY] & 0x3f);
219 tmp = regs[DS1307_REG_MONTH] & 0x1f;
220 t->tm_mon = bcd2bin(tmp) - 1;
221 t->tm_year = bcd2bin(regs[DS1307_REG_YEAR]) + 100;
222
223 if (regs[chip->century_reg] & chip->century_bit &&
224 IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY))
225 t->tm_year += 100;
226
227 dev_dbg(dev, "%s secs=%d, mins=%d, "
228 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
229 "read", t->tm_sec, t->tm_min,
230 t->tm_hour, t->tm_mday,
231 t->tm_mon, t->tm_year, t->tm_wday);
232
233 return 0;
234}
235
236static int ds1307_set_time(struct device *dev, struct rtc_time *t)
237{
238 struct ds1307 *ds1307 = dev_get_drvdata(dev);
239 const struct chip_desc *chip = &chips[ds1307->type];
240 int result;
241 int tmp;
242 u8 regs[7];
243
244 dev_dbg(dev, "%s secs=%d, mins=%d, "
245 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
246 "write", t->tm_sec, t->tm_min,
247 t->tm_hour, t->tm_mday,
248 t->tm_mon, t->tm_year, t->tm_wday);
249
250 if (t->tm_year < 100)
251 return -EINVAL;
252
253#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
254 if (t->tm_year > (chip->century_bit ? 299 : 199))
255 return -EINVAL;
256#else
257 if (t->tm_year > 199)
258 return -EINVAL;
259#endif
260
261 regs[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
262 regs[DS1307_REG_MIN] = bin2bcd(t->tm_min);
263 regs[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
264 regs[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
265 regs[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
266 regs[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
267
268 /* assume 20YY not 19YY */
269 tmp = t->tm_year - 100;
270 regs[DS1307_REG_YEAR] = bin2bcd(tmp);
271
272 if (chip->century_enable_bit)
273 regs[chip->century_reg] |= chip->century_enable_bit;
274 if (t->tm_year > 199 && chip->century_bit)
275 regs[chip->century_reg] |= chip->century_bit;
276
277 if (ds1307->type == mcp794xx) {
278 /*
279 * these bits were cleared when preparing the date/time
280 * values and need to be set again before writing the
281 * regsfer out to the device.
282 */
283 regs[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
284 regs[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
285 }
286
287 dev_dbg(dev, "%s: %7ph\n", "write", regs);
288
289 result = regmap_bulk_write(ds1307->regmap, chip->offset, regs,
290 sizeof(regs));
291 if (result) {
292 dev_err(dev, "%s error %d\n", "write", result);
293 return result;
294 }
295 return 0;
296}
297
298static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
299{
300 struct ds1307 *ds1307 = dev_get_drvdata(dev);
301 int ret;
302 u8 regs[9];
303
304 if (!test_bit(HAS_ALARM, &ds1307->flags))
305 return -EINVAL;
306
307 /* read all ALARM1, ALARM2, and status registers at once */
308 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
309 regs, sizeof(regs));
310 if (ret) {
311 dev_err(dev, "%s error %d\n", "alarm read", ret);
312 return ret;
313 }
314
315 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
316 &regs[0], &regs[4], &regs[7]);
317
318 /*
319 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
320 * and that all four fields are checked matches
321 */
322 t->time.tm_sec = bcd2bin(regs[0] & 0x7f);
323 t->time.tm_min = bcd2bin(regs[1] & 0x7f);
324 t->time.tm_hour = bcd2bin(regs[2] & 0x3f);
325 t->time.tm_mday = bcd2bin(regs[3] & 0x3f);
326
327 /* ... and status */
328 t->enabled = !!(regs[7] & DS1337_BIT_A1IE);
329 t->pending = !!(regs[8] & DS1337_BIT_A1I);
330
331 dev_dbg(dev, "%s secs=%d, mins=%d, "
332 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
333 "alarm read", t->time.tm_sec, t->time.tm_min,
334 t->time.tm_hour, t->time.tm_mday,
335 t->enabled, t->pending);
336
337 return 0;
338}
339
340static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
341{
342 struct ds1307 *ds1307 = dev_get_drvdata(dev);
343 unsigned char regs[9];
344 u8 control, status;
345 int ret;
346
347 if (!test_bit(HAS_ALARM, &ds1307->flags))
348 return -EINVAL;
349
350 dev_dbg(dev, "%s secs=%d, mins=%d, "
351 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
352 "alarm set", t->time.tm_sec, t->time.tm_min,
353 t->time.tm_hour, t->time.tm_mday,
354 t->enabled, t->pending);
355
356 /* read current status of both alarms and the chip */
357 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
358 sizeof(regs));
359 if (ret) {
360 dev_err(dev, "%s error %d\n", "alarm write", ret);
361 return ret;
362 }
363 control = regs[7];
364 status = regs[8];
365
366 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
367 &regs[0], &regs[4], control, status);
368
369 /* set ALARM1, using 24 hour and day-of-month modes */
370 regs[0] = bin2bcd(t->time.tm_sec);
371 regs[1] = bin2bcd(t->time.tm_min);
372 regs[2] = bin2bcd(t->time.tm_hour);
373 regs[3] = bin2bcd(t->time.tm_mday);
374
375 /* set ALARM2 to non-garbage */
376 regs[4] = 0;
377 regs[5] = 0;
378 regs[6] = 0;
379
380 /* disable alarms */
381 regs[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
382 regs[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
383
384 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
385 sizeof(regs));
386 if (ret) {
387 dev_err(dev, "can't set alarm time\n");
388 return ret;
389 }
390
391 /* optionally enable ALARM1 */
392 if (t->enabled) {
393 dev_dbg(dev, "alarm IRQ armed\n");
394 regs[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
395 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, regs[7]);
396 }
397
398 return 0;
399}
400
401static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
402{
403 struct ds1307 *ds1307 = dev_get_drvdata(dev);
404
405 if (!test_bit(HAS_ALARM, &ds1307->flags))
406 return -ENOTTY;
407
408 return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
409 DS1337_BIT_A1IE,
410 enabled ? DS1337_BIT_A1IE : 0);
411}
412
413static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307, u32 ohms, bool diode)
414{
415 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
416 DS1307_TRICKLE_CHARGER_NO_DIODE;
417
418 switch (ohms) {
419 case 250:
420 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
421 break;
422 case 2000:
423 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
424 break;
425 case 4000:
426 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
427 break;
428 default:
429 dev_warn(ds1307->dev,
430 "Unsupported ohm value %u in dt\n", ohms);
431 return 0;
432 }
433 return setup;
434}
435
436static irqreturn_t rx8130_irq(int irq, void *dev_id)
437{
438 struct ds1307 *ds1307 = dev_id;
439 struct mutex *lock = &ds1307->rtc->ops_lock;
440 u8 ctl[3];
441 int ret;
442
443 mutex_lock(lock);
444
445 /* Read control registers. */
446 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
447 sizeof(ctl));
448 if (ret < 0)
449 goto out;
450 if (!(ctl[1] & RX8130_REG_FLAG_AF))
451 goto out;
452 ctl[1] &= ~RX8130_REG_FLAG_AF;
453 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
454
455 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
456 sizeof(ctl));
457 if (ret < 0)
458 goto out;
459
460 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
461
462out:
463 mutex_unlock(lock);
464
465 return IRQ_HANDLED;
466}
467
468static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
469{
470 struct ds1307 *ds1307 = dev_get_drvdata(dev);
471 u8 ald[3], ctl[3];
472 int ret;
473
474 if (!test_bit(HAS_ALARM, &ds1307->flags))
475 return -EINVAL;
476
477 /* Read alarm registers. */
478 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
479 sizeof(ald));
480 if (ret < 0)
481 return ret;
482
483 /* Read control registers. */
484 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
485 sizeof(ctl));
486 if (ret < 0)
487 return ret;
488
489 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
490 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
491
492 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
493 t->time.tm_sec = -1;
494 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
495 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
496 t->time.tm_wday = -1;
497 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
498 t->time.tm_mon = -1;
499 t->time.tm_year = -1;
500 t->time.tm_yday = -1;
501 t->time.tm_isdst = -1;
502
503 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
504 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
505 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
506
507 return 0;
508}
509
510static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
511{
512 struct ds1307 *ds1307 = dev_get_drvdata(dev);
513 u8 ald[3], ctl[3];
514 int ret;
515
516 if (!test_bit(HAS_ALARM, &ds1307->flags))
517 return -EINVAL;
518
519 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
520 "enabled=%d pending=%d\n", __func__,
521 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
522 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
523 t->enabled, t->pending);
524
525 /* Read control registers. */
526 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
527 sizeof(ctl));
528 if (ret < 0)
529 return ret;
530
531 ctl[0] &= ~RX8130_REG_EXTENSION_WADA;
532 ctl[1] |= RX8130_REG_FLAG_AF;
533 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
534
535 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
536 sizeof(ctl));
537 if (ret < 0)
538 return ret;
539
540 /* Hardware alarm precision is 1 minute! */
541 ald[0] = bin2bcd(t->time.tm_min);
542 ald[1] = bin2bcd(t->time.tm_hour);
543 ald[2] = bin2bcd(t->time.tm_mday);
544
545 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
546 sizeof(ald));
547 if (ret < 0)
548 return ret;
549
550 if (!t->enabled)
551 return 0;
552
553 ctl[2] |= RX8130_REG_CONTROL0_AIE;
554
555 return regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
556 sizeof(ctl));
557}
558
559static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
560{
561 struct ds1307 *ds1307 = dev_get_drvdata(dev);
562 int ret, reg;
563
564 if (!test_bit(HAS_ALARM, &ds1307->flags))
565 return -EINVAL;
566
567 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
568 if (ret < 0)
569 return ret;
570
571 if (enabled)
572 reg |= RX8130_REG_CONTROL0_AIE;
573 else
574 reg &= ~RX8130_REG_CONTROL0_AIE;
575
576 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
577}
578
579static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
580{
581 struct ds1307 *ds1307 = dev_id;
582 struct mutex *lock = &ds1307->rtc->ops_lock;
583 int reg, ret;
584
585 mutex_lock(lock);
586
587 /* Check and clear alarm 0 interrupt flag. */
588 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
589 if (ret)
590 goto out;
591 if (!(reg & MCP794XX_BIT_ALMX_IF))
592 goto out;
593 reg &= ~MCP794XX_BIT_ALMX_IF;
594 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
595 if (ret)
596 goto out;
597
598 /* Disable alarm 0. */
599 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
600 MCP794XX_BIT_ALM0_EN, 0);
601 if (ret)
602 goto out;
603
604 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
605
606out:
607 mutex_unlock(lock);
608
609 return IRQ_HANDLED;
610}
611
612static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
613{
614 struct ds1307 *ds1307 = dev_get_drvdata(dev);
615 u8 regs[10];
616 int ret;
617
618 if (!test_bit(HAS_ALARM, &ds1307->flags))
619 return -EINVAL;
620
621 /* Read control and alarm 0 registers. */
622 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
623 sizeof(regs));
624 if (ret)
625 return ret;
626
627 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
628
629 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
630 t->time.tm_sec = bcd2bin(regs[3] & 0x7f);
631 t->time.tm_min = bcd2bin(regs[4] & 0x7f);
632 t->time.tm_hour = bcd2bin(regs[5] & 0x3f);
633 t->time.tm_wday = bcd2bin(regs[6] & 0x7) - 1;
634 t->time.tm_mday = bcd2bin(regs[7] & 0x3f);
635 t->time.tm_mon = bcd2bin(regs[8] & 0x1f) - 1;
636 t->time.tm_year = -1;
637 t->time.tm_yday = -1;
638 t->time.tm_isdst = -1;
639
640 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
641 "enabled=%d polarity=%d irq=%d match=%lu\n", __func__,
642 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
643 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
644 !!(regs[6] & MCP794XX_BIT_ALMX_POL),
645 !!(regs[6] & MCP794XX_BIT_ALMX_IF),
646 (regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
647
648 return 0;
649}
650
651/*
652 * We may have a random RTC weekday, therefore calculate alarm weekday based
653 * on current weekday we read from the RTC timekeeping regs
654 */
655static int mcp794xx_alm_weekday(struct device *dev, struct rtc_time *tm_alarm)
656{
657 struct rtc_time tm_now;
658 int days_now, days_alarm, ret;
659
660 ret = ds1307_get_time(dev, &tm_now);
661 if (ret)
662 return ret;
663
664 days_now = div_s64(rtc_tm_to_time64(&tm_now), 24 * 60 * 60);
665 days_alarm = div_s64(rtc_tm_to_time64(tm_alarm), 24 * 60 * 60);
666
667 return (tm_now.tm_wday + days_alarm - days_now) % 7 + 1;
668}
669
670static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
671{
672 struct ds1307 *ds1307 = dev_get_drvdata(dev);
673 unsigned char regs[10];
674 int wday, ret;
675
676 if (!test_bit(HAS_ALARM, &ds1307->flags))
677 return -EINVAL;
678
679 wday = mcp794xx_alm_weekday(dev, &t->time);
680 if (wday < 0)
681 return wday;
682
683 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
684 "enabled=%d pending=%d\n", __func__,
685 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
686 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
687 t->enabled, t->pending);
688
689 /* Read control and alarm 0 registers. */
690 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
691 sizeof(regs));
692 if (ret)
693 return ret;
694
695 /* Set alarm 0, using 24-hour and day-of-month modes. */
696 regs[3] = bin2bcd(t->time.tm_sec);
697 regs[4] = bin2bcd(t->time.tm_min);
698 regs[5] = bin2bcd(t->time.tm_hour);
699 regs[6] = wday;
700 regs[7] = bin2bcd(t->time.tm_mday);
701 regs[8] = bin2bcd(t->time.tm_mon + 1);
702
703 /* Clear the alarm 0 interrupt flag. */
704 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
705 /* Set alarm match: second, minute, hour, day, date, month. */
706 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
707 /* Disable interrupt. We will not enable until completely programmed */
708 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
709
710 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
711 sizeof(regs));
712 if (ret)
713 return ret;
714
715 if (!t->enabled)
716 return 0;
717 regs[0] |= MCP794XX_BIT_ALM0_EN;
718 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
719}
720
721static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
722{
723 struct ds1307 *ds1307 = dev_get_drvdata(dev);
724
725 if (!test_bit(HAS_ALARM, &ds1307->flags))
726 return -EINVAL;
727
728 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
729 MCP794XX_BIT_ALM0_EN,
730 enabled ? MCP794XX_BIT_ALM0_EN : 0);
731}
732
733static int m41txx_rtc_read_offset(struct device *dev, long *offset)
734{
735 struct ds1307 *ds1307 = dev_get_drvdata(dev);
736 unsigned int ctrl_reg;
737 u8 val;
738
739 regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
740
741 val = ctrl_reg & M41TXX_M_CALIBRATION;
742
743 /* check if positive */
744 if (ctrl_reg & M41TXX_BIT_CALIB_SIGN)
745 *offset = (val * M41TXX_POS_OFFSET_STEP_PPB);
746 else
747 *offset = -(val * M41TXX_NEG_OFFSET_STEP_PPB);
748
749 return 0;
750}
751
752static int m41txx_rtc_set_offset(struct device *dev, long offset)
753{
754 struct ds1307 *ds1307 = dev_get_drvdata(dev);
755 unsigned int ctrl_reg;
756
757 if ((offset < M41TXX_MIN_OFFSET) || (offset > M41TXX_MAX_OFFSET))
758 return -ERANGE;
759
760 if (offset >= 0) {
761 ctrl_reg = DIV_ROUND_CLOSEST(offset,
762 M41TXX_POS_OFFSET_STEP_PPB);
763 ctrl_reg |= M41TXX_BIT_CALIB_SIGN;
764 } else {
765 ctrl_reg = DIV_ROUND_CLOSEST(abs(offset),
766 M41TXX_NEG_OFFSET_STEP_PPB);
767 }
768
769 return regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL,
770 M41TXX_M_CALIBRATION | M41TXX_BIT_CALIB_SIGN,
771 ctrl_reg);
772}
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700773
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200774static const struct rtc_class_ops rx8130_rtc_ops = {
775 .read_time = ds1307_get_time,
776 .set_time = ds1307_set_time,
777 .read_alarm = rx8130_read_alarm,
778 .set_alarm = rx8130_set_alarm,
779 .alarm_irq_enable = rx8130_alarm_irq_enable,
780};
781
782static const struct rtc_class_ops mcp794xx_rtc_ops = {
783 .read_time = ds1307_get_time,
784 .set_time = ds1307_set_time,
785 .read_alarm = mcp794xx_read_alarm,
786 .set_alarm = mcp794xx_set_alarm,
787 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
788};
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700789
Giulio Benetti79230ff2018-07-25 19:26:04 +0200790static const struct rtc_class_ops m41txx_rtc_ops = {
791 .read_time = ds1307_get_time,
792 .set_time = ds1307_set_time,
793 .read_alarm = ds1337_read_alarm,
794 .set_alarm = ds1337_set_alarm,
795 .alarm_irq_enable = ds1307_alarm_irq_enable,
796 .read_offset = m41txx_rtc_read_offset,
797 .set_offset = m41txx_rtc_set_offset,
798};
799
Heiner Kallweit7624df42017-07-12 07:49:33 +0200800static const struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700801 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700802 .nvram_offset = 8,
803 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700804 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200805 [ds_1308] = {
806 .nvram_offset = 8,
807 .nvram_size = 56,
808 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700809 [ds_1337] = {
810 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200811 .century_reg = DS1307_REG_MONTH,
812 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700813 },
814 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700815 .nvram_offset = 8,
816 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700817 },
818 [ds_1339] = {
819 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200820 .century_reg = DS1307_REG_MONTH,
821 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200822 .bbsqi_bit = DS1339_BIT_BBSQI,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700823 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700824 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700825 },
826 [ds_1340] = {
Heiner Kallweite48585d2017-06-05 17:57:33 +0200827 .century_reg = DS1307_REG_HOUR,
828 .century_enable_bit = DS1340_BIT_CENTURY_EN,
829 .century_bit = DS1340_BIT_CENTURY,
Andrea Greco51ed73eb2018-04-20 11:34:02 +0200830 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700831 .trickle_charger_reg = 0x08,
832 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300833 [ds_1341] = {
834 .century_reg = DS1307_REG_MONTH,
835 .century_bit = DS1337_BIT_CENTURY,
836 },
Wolfram Sangeb86c302012-05-29 15:07:38 -0700837 [ds_1388] = {
Heiner Kallweite5531702017-07-12 07:49:47 +0200838 .offset = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700839 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700840 },
841 [ds_3231] = {
842 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200843 .century_reg = DS1307_REG_MONTH,
844 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200845 .bbsqi_bit = DS3231_BIT_BBSQW,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700846 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200847 [rx_8130] = {
848 .alarm = 1,
849 /* this is battery backed SRAM */
850 .nvram_offset = 0x20,
851 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
Heiner Kallweite5531702017-07-12 07:49:47 +0200852 .offset = 0x10,
Heiner Kallweit45947122017-07-12 07:49:41 +0200853 .irq_handler = rx8130_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200854 .rtc_ops = &rx8130_rtc_ops,
Marek Vasutee0981b2017-06-18 22:55:28 +0200855 },
Giulio Benetti79230ff2018-07-25 19:26:04 +0200856 [m41t0] = {
857 .rtc_ops = &m41txx_rtc_ops,
858 },
859 [m41t00] = {
860 .rtc_ops = &m41txx_rtc_ops,
861 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200862 [m41t11] = {
863 /* this is battery backed SRAM */
864 .nvram_offset = 8,
865 .nvram_size = 56,
Giulio Benetti79230ff2018-07-25 19:26:04 +0200866 .rtc_ops = &m41txx_rtc_ops,
Giulio Benetti7e580762018-05-16 23:08:40 +0200867 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800868 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700869 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700870 /* this is battery backed SRAM */
871 .nvram_offset = 0x20,
872 .nvram_size = 0x40,
Heiner Kallweit45947122017-07-12 07:49:41 +0200873 .irq_handler = mcp794xx_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200874 .rtc_ops = &mcp794xx_rtc_ops,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700875 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700876};
David Brownell045e0e82007-07-17 04:04:55 -0700877
Jean Delvare3760f732008-04-29 23:11:40 +0200878static const struct i2c_device_id ds1307_id[] = {
879 { "ds1307", ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200880 { "ds1308", ds_1308 },
Jean Delvare3760f732008-04-29 23:11:40 +0200881 { "ds1337", ds_1337 },
882 { "ds1338", ds_1338 },
883 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700884 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200885 { "ds1340", ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300886 { "ds1341", ds_1341 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700887 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700888 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200889 { "m41t00", m41t00 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200890 { "m41t11", m41t11 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800891 { "mcp7940x", mcp794xx },
892 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700893 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700894 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200895 { "isl12057", ds_1337 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200896 { "rx8130", rx_8130 },
Jean Delvare3760f732008-04-29 23:11:40 +0200897 { }
898};
899MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700900
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300901#ifdef CONFIG_OF
902static const struct of_device_id ds1307_of_match[] = {
903 {
904 .compatible = "dallas,ds1307",
905 .data = (void *)ds_1307
906 },
907 {
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200908 .compatible = "dallas,ds1308",
909 .data = (void *)ds_1308
910 },
911 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300912 .compatible = "dallas,ds1337",
913 .data = (void *)ds_1337
914 },
915 {
916 .compatible = "dallas,ds1338",
917 .data = (void *)ds_1338
918 },
919 {
920 .compatible = "dallas,ds1339",
921 .data = (void *)ds_1339
922 },
923 {
924 .compatible = "dallas,ds1388",
925 .data = (void *)ds_1388
926 },
927 {
928 .compatible = "dallas,ds1340",
929 .data = (void *)ds_1340
930 },
931 {
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300932 .compatible = "dallas,ds1341",
933 .data = (void *)ds_1341
934 },
935 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300936 .compatible = "maxim,ds3231",
937 .data = (void *)ds_3231
938 },
939 {
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200940 .compatible = "st,m41t0",
Giulio Benetti146a5522018-05-16 23:08:39 +0200941 .data = (void *)m41t0
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200942 },
943 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300944 .compatible = "st,m41t00",
945 .data = (void *)m41t00
946 },
947 {
Giulio Benetti7e580762018-05-16 23:08:40 +0200948 .compatible = "st,m41t11",
949 .data = (void *)m41t11
950 },
951 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300952 .compatible = "microchip,mcp7940x",
953 .data = (void *)mcp794xx
954 },
955 {
956 .compatible = "microchip,mcp7941x",
957 .data = (void *)mcp794xx
958 },
959 {
960 .compatible = "pericom,pt7c4338",
961 .data = (void *)ds_1307
962 },
963 {
964 .compatible = "epson,rx8025",
965 .data = (void *)rx_8025
966 },
967 {
968 .compatible = "isil,isl12057",
969 .data = (void *)ds_1337
970 },
Bastian Stender47dd4722017-10-17 14:46:07 +0200971 {
972 .compatible = "epson,rx8130",
973 .data = (void *)rx_8130
974 },
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300975 { }
976};
977MODULE_DEVICE_TABLE(of, ds1307_of_match);
978#endif
979
Tin Huynh9c19b892016-11-30 09:57:31 +0700980#ifdef CONFIG_ACPI
981static const struct acpi_device_id ds1307_acpi_ids[] = {
982 { .id = "DS1307", .driver_data = ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200983 { .id = "DS1308", .driver_data = ds_1308 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700984 { .id = "DS1337", .driver_data = ds_1337 },
985 { .id = "DS1338", .driver_data = ds_1338 },
986 { .id = "DS1339", .driver_data = ds_1339 },
987 { .id = "DS1388", .driver_data = ds_1388 },
988 { .id = "DS1340", .driver_data = ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300989 { .id = "DS1341", .driver_data = ds_1341 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700990 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700991 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700992 { .id = "M41T00", .driver_data = m41t00 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200993 { .id = "M41T11", .driver_data = m41t11 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700994 { .id = "MCP7940X", .driver_data = mcp794xx },
995 { .id = "MCP7941X", .driver_data = mcp794xx },
996 { .id = "PT7C4338", .driver_data = ds_1307 },
997 { .id = "RX8025", .driver_data = rx_8025 },
998 { .id = "ISL12057", .driver_data = ds_1337 },
Bastian Stender47dd4722017-10-17 14:46:07 +0200999 { .id = "RX8130", .driver_data = rx_8130 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001000 { }
1001};
1002MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
1003#endif
1004
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001005/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001006 * The ds1337 and ds1339 both have two alarms, but we only use the first
1007 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
1008 * signal; ds1339 chips have only one alarm signal.
1009 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001010static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001011{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001012 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001013 struct mutex *lock = &ds1307->rtc->ops_lock;
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001014 int stat, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001015
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001016 mutex_lock(lock);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001017 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
1018 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001019 goto out;
1020
1021 if (stat & DS1337_BIT_A1I) {
1022 stat &= ~DS1337_BIT_A1I;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001023 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001024
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001025 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1026 DS1337_BIT_A1IE, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001027 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001028 goto out;
1029
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001030 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001031 }
1032
1033out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001034 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001035
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001036 return IRQ_HANDLED;
1037}
1038
1039/*----------------------------------------------------------------------*/
1040
David Brownellff8371a2006-09-30 23:28:17 -07001041static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -07001042 .read_time = ds1307_get_time,
1043 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -08001044 .read_alarm = ds1337_read_alarm,
1045 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -08001046 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -07001047};
1048
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001049static ssize_t frequency_test_store(struct device *dev,
1050 struct device_attribute *attr,
1051 const char *buf, size_t count)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001052{
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001053 struct ds1307 *ds1307 = dev_get_drvdata(dev->parent);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001054 bool freq_test_en;
1055 int ret;
1056
1057 ret = kstrtobool(buf, &freq_test_en);
1058 if (ret) {
1059 dev_err(dev, "Failed to store RTC Frequency Test attribute\n");
1060 return ret;
1061 }
1062
1063 regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL, M41TXX_BIT_FT,
1064 freq_test_en ? M41TXX_BIT_FT : 0);
1065
1066 return count;
1067}
1068
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001069static ssize_t frequency_test_show(struct device *dev,
1070 struct device_attribute *attr,
1071 char *buf)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001072{
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001073 struct ds1307 *ds1307 = dev_get_drvdata(dev->parent);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001074 unsigned int ctrl_reg;
1075
1076 regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
1077
1078 return scnprintf(buf, PAGE_SIZE, (ctrl_reg & M41TXX_BIT_FT) ? "on\n" :
1079 "off\n");
1080}
1081
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001082static DEVICE_ATTR_RW(frequency_test);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001083
1084static struct attribute *rtc_freq_test_attrs[] = {
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001085 &dev_attr_frequency_test.attr,
Giulio Benettib41c23e2018-07-25 19:26:05 +02001086 NULL,
1087};
1088
1089static const struct attribute_group rtc_freq_test_attr_group = {
1090 .attrs = rtc_freq_test_attrs,
1091};
1092
Giulio Benettib41c23e2018-07-25 19:26:05 +02001093static int ds1307_add_frequency_test(struct ds1307 *ds1307)
1094{
1095 int err;
1096
1097 switch (ds1307->type) {
1098 case m41t0:
1099 case m41t00:
1100 case m41t11:
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001101 err = rtc_add_group(ds1307->rtc, &rtc_freq_test_attr_group);
1102 if (err)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001103 return err;
Giulio Benettib41c23e2018-07-25 19:26:05 +02001104 break;
1105 default:
1106 break;
1107 }
1108
1109 return 0;
1110}
1111
Simon Guinot1d1945d2014-04-03 14:49:55 -07001112/*----------------------------------------------------------------------*/
1113
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001114static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
1115 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -08001116{
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001117 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +02001118 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -08001119
Heiner Kallweit969fa072017-07-12 07:49:54 +02001120 return regmap_bulk_read(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001121 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -08001122}
1123
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001124static int ds1307_nvram_write(void *priv, unsigned int offset, void *val,
1125 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -08001126{
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001127 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +02001128 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -08001129
Heiner Kallweit969fa072017-07-12 07:49:54 +02001130 return regmap_bulk_write(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001131 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -08001132}
1133
David Brownell682d73f2007-11-14 16:58:32 -08001134/*----------------------------------------------------------------------*/
1135
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001136static u8 ds1307_trickle_init(struct ds1307 *ds1307,
Heiner Kallweit7624df42017-07-12 07:49:33 +02001137 const struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001138{
Alexandre Belloni57ec2d92017-09-04 22:46:04 +02001139 u32 ohms;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001140 bool diode = true;
1141
1142 if (!chip->do_trickle_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001143 return 0;
1144
Heiner Kallweit11e58902017-03-10 18:52:34 +01001145 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
1146 &ohms))
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001147 return 0;
1148
Heiner Kallweit11e58902017-03-10 18:52:34 +01001149 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001150 diode = false;
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001151
1152 return chip->do_trickle_setup(ds1307, ohms, diode);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001153}
1154
Akinobu Mita445c0202016-01-25 00:22:16 +09001155/*----------------------------------------------------------------------*/
1156
Heiner Kallweit6b583a62017-09-27 22:41:26 +02001157#if IS_REACHABLE(CONFIG_HWMON)
Akinobu Mita445c0202016-01-25 00:22:16 +09001158
1159/*
1160 * Temperature sensor support for ds3231 devices.
1161 */
1162
1163#define DS3231_REG_TEMPERATURE 0x11
1164
1165/*
1166 * A user-initiated temperature conversion is not started by this function,
1167 * so the temperature is updated once every 64 seconds.
1168 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001169static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +09001170{
1171 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1172 u8 temp_buf[2];
1173 s16 temp;
1174 int ret;
1175
Heiner Kallweit11e58902017-03-10 18:52:34 +01001176 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
1177 temp_buf, sizeof(temp_buf));
1178 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +09001179 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +09001180 /*
1181 * Temperature is represented as a 10-bit code with a resolution of
1182 * 0.25 degree celsius and encoded in two's complement format.
1183 */
1184 temp = (temp_buf[0] << 8) | temp_buf[1];
1185 temp >>= 6;
1186 *mC = temp * 250;
1187
1188 return 0;
1189}
1190
1191static ssize_t ds3231_hwmon_show_temp(struct device *dev,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001192 struct device_attribute *attr, char *buf)
Akinobu Mita445c0202016-01-25 00:22:16 +09001193{
1194 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001195 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001196
1197 ret = ds3231_hwmon_read_temp(dev, &temp);
1198 if (ret)
1199 return ret;
1200
1201 return sprintf(buf, "%d\n", temp);
1202}
Alexandre Bellonib4be2712017-09-04 22:46:08 +02001203static SENSOR_DEVICE_ATTR(temp1_input, 0444, ds3231_hwmon_show_temp,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001204 NULL, 0);
Akinobu Mita445c0202016-01-25 00:22:16 +09001205
1206static struct attribute *ds3231_hwmon_attrs[] = {
1207 &sensor_dev_attr_temp1_input.dev_attr.attr,
1208 NULL,
1209};
1210ATTRIBUTE_GROUPS(ds3231_hwmon);
1211
1212static void ds1307_hwmon_register(struct ds1307 *ds1307)
1213{
1214 struct device *dev;
1215
1216 if (ds1307->type != ds_3231)
1217 return;
1218
Heiner Kallweit11e58902017-03-10 18:52:34 +01001219 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001220 ds1307,
1221 ds3231_hwmon_groups);
Akinobu Mita445c0202016-01-25 00:22:16 +09001222 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001223 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1224 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001225 }
1226}
1227
1228#else
1229
1230static void ds1307_hwmon_register(struct ds1307 *ds1307)
1231{
1232}
1233
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001234#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1235
1236/*----------------------------------------------------------------------*/
1237
1238/*
1239 * Square-wave output support for DS3231
1240 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1241 */
1242#ifdef CONFIG_COMMON_CLK
1243
1244enum {
1245 DS3231_CLK_SQW = 0,
1246 DS3231_CLK_32KHZ,
1247};
1248
1249#define clk_sqw_to_ds1307(clk) \
1250 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1251#define clk_32khz_to_ds1307(clk) \
1252 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1253
1254static int ds3231_clk_sqw_rates[] = {
1255 1,
1256 1024,
1257 4096,
1258 8192,
1259};
1260
1261static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1262{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001263 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001264 int ret;
1265
1266 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001267 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1268 mask, value);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001269 mutex_unlock(lock);
1270
1271 return ret;
1272}
1273
1274static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1275 unsigned long parent_rate)
1276{
1277 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001278 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001279 int rate_sel = 0;
1280
Heiner Kallweit11e58902017-03-10 18:52:34 +01001281 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1282 if (ret)
1283 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001284 if (control & DS1337_BIT_RS1)
1285 rate_sel += 1;
1286 if (control & DS1337_BIT_RS2)
1287 rate_sel += 2;
1288
1289 return ds3231_clk_sqw_rates[rate_sel];
1290}
1291
1292static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001293 unsigned long *prate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001294{
1295 int i;
1296
1297 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1298 if (ds3231_clk_sqw_rates[i] <= rate)
1299 return ds3231_clk_sqw_rates[i];
1300 }
1301
1302 return 0;
1303}
1304
1305static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001306 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001307{
1308 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1309 int control = 0;
1310 int rate_sel;
1311
1312 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1313 rate_sel++) {
1314 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1315 break;
1316 }
1317
1318 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1319 return -EINVAL;
1320
1321 if (rate_sel & 1)
1322 control |= DS1337_BIT_RS1;
1323 if (rate_sel & 2)
1324 control |= DS1337_BIT_RS2;
1325
1326 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1327 control);
1328}
1329
1330static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1331{
1332 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1333
1334 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1335}
1336
1337static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1338{
1339 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1340
1341 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1342}
1343
1344static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1345{
1346 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001347 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001348
Heiner Kallweit11e58902017-03-10 18:52:34 +01001349 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1350 if (ret)
1351 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001352
1353 return !(control & DS1337_BIT_INTCN);
1354}
1355
1356static const struct clk_ops ds3231_clk_sqw_ops = {
1357 .prepare = ds3231_clk_sqw_prepare,
1358 .unprepare = ds3231_clk_sqw_unprepare,
1359 .is_prepared = ds3231_clk_sqw_is_prepared,
1360 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1361 .round_rate = ds3231_clk_sqw_round_rate,
1362 .set_rate = ds3231_clk_sqw_set_rate,
1363};
1364
1365static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001366 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001367{
1368 return 32768;
1369}
1370
1371static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1372{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001373 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001374 int ret;
1375
1376 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001377 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1378 DS3231_BIT_EN32KHZ,
1379 enable ? DS3231_BIT_EN32KHZ : 0);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001380 mutex_unlock(lock);
1381
1382 return ret;
1383}
1384
1385static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1386{
1387 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1388
1389 return ds3231_clk_32khz_control(ds1307, true);
1390}
1391
1392static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1393{
1394 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1395
1396 ds3231_clk_32khz_control(ds1307, false);
1397}
1398
1399static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1400{
1401 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001402 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001403
Heiner Kallweit11e58902017-03-10 18:52:34 +01001404 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1405 if (ret)
1406 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001407
1408 return !!(status & DS3231_BIT_EN32KHZ);
1409}
1410
1411static const struct clk_ops ds3231_clk_32khz_ops = {
1412 .prepare = ds3231_clk_32khz_prepare,
1413 .unprepare = ds3231_clk_32khz_unprepare,
1414 .is_prepared = ds3231_clk_32khz_is_prepared,
1415 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1416};
1417
1418static struct clk_init_data ds3231_clks_init[] = {
1419 [DS3231_CLK_SQW] = {
1420 .name = "ds3231_clk_sqw",
1421 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001422 },
1423 [DS3231_CLK_32KHZ] = {
1424 .name = "ds3231_clk_32khz",
1425 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001426 },
1427};
1428
1429static int ds3231_clks_register(struct ds1307 *ds1307)
1430{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001431 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001432 struct clk_onecell_data *onecell;
1433 int i;
1434
Heiner Kallweit11e58902017-03-10 18:52:34 +01001435 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001436 if (!onecell)
1437 return -ENOMEM;
1438
1439 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001440 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1441 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001442 if (!onecell->clks)
1443 return -ENOMEM;
1444
1445 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1446 struct clk_init_data init = ds3231_clks_init[i];
1447
1448 /*
1449 * Interrupt signal due to alarm conditions and square-wave
1450 * output share same pin, so don't initialize both.
1451 */
1452 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1453 continue;
1454
1455 /* optional override of the clockname */
1456 of_property_read_string_index(node, "clock-output-names", i,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001457 &init.name);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001458 ds1307->clks[i].init = &init;
1459
Heiner Kallweit11e58902017-03-10 18:52:34 +01001460 onecell->clks[i] = devm_clk_register(ds1307->dev,
1461 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001462 if (IS_ERR(onecell->clks[i]))
1463 return PTR_ERR(onecell->clks[i]);
1464 }
1465
1466 if (!node)
1467 return 0;
1468
1469 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1470
1471 return 0;
1472}
1473
1474static void ds1307_clks_register(struct ds1307 *ds1307)
1475{
1476 int ret;
1477
1478 if (ds1307->type != ds_3231)
1479 return;
1480
1481 ret = ds3231_clks_register(ds1307);
1482 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001483 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1484 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001485 }
1486}
1487
1488#else
1489
1490static void ds1307_clks_register(struct ds1307 *ds1307)
1491{
1492}
1493
1494#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001495
Heiner Kallweit11e58902017-03-10 18:52:34 +01001496static const struct regmap_config regmap_config = {
1497 .reg_bits = 8,
1498 .val_bits = 8,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001499};
1500
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001501static int ds1307_probe(struct i2c_client *client,
1502 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001503{
1504 struct ds1307 *ds1307;
1505 int err = -ENODEV;
Heiner Kallweit584ce302017-08-29 21:52:56 +02001506 int tmp;
Heiner Kallweit7624df42017-07-12 07:49:33 +02001507 const struct chip_desc *chip;
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001508 bool want_irq;
Michael Lange8bc2a402016-01-21 18:10:16 +01001509 bool ds1307_can_wakeup_device = false;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001510 unsigned char regs[8];
Jingoo Han01ce8932013-11-12 15:10:41 -08001511 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001512 u8 trickle_charger_setup = 0;
David Brownell1abb0dc2006-06-25 05:48:17 -07001513
Jingoo Hanedca66d2013-07-03 15:07:05 -07001514 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001515 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001516 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001517
Heiner Kallweit11e58902017-03-10 18:52:34 +01001518 dev_set_drvdata(&client->dev, ds1307);
1519 ds1307->dev = &client->dev;
1520 ds1307->name = client->name;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001521
Heiner Kallweit11e58902017-03-10 18:52:34 +01001522 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1523 if (IS_ERR(ds1307->regmap)) {
1524 dev_err(ds1307->dev, "regmap allocation failed\n");
1525 return PTR_ERR(ds1307->regmap);
1526 }
1527
1528 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001529
1530 if (client->dev.of_node) {
1531 ds1307->type = (enum ds_type)
1532 of_device_get_match_data(&client->dev);
1533 chip = &chips[ds1307->type];
1534 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001535 chip = &chips[id->driver_data];
1536 ds1307->type = id->driver_data;
1537 } else {
1538 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001539
Tin Huynh9c19b892016-11-30 09:57:31 +07001540 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001541 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001542 if (!acpi_id)
1543 return -ENODEV;
1544 chip = &chips[acpi_id->driver_data];
1545 ds1307->type = acpi_id->driver_data;
1546 }
1547
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001548 want_irq = client->irq > 0 && chip->alarm;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001549
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001550 if (!pdata)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001551 trickle_charger_setup = ds1307_trickle_init(ds1307, chip);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001552 else if (pdata->trickle_charger_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001553 trickle_charger_setup = pdata->trickle_charger_setup;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001554
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001555 if (trickle_charger_setup && chip->trickle_charger_reg) {
1556 trickle_charger_setup |= DS13XX_TRICKLE_CHARGER_MAGIC;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001557 dev_dbg(ds1307->dev,
1558 "writing trickle charger info 0x%x to 0x%x\n",
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001559 trickle_charger_setup, chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001560 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001561 trickle_charger_setup);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001562 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001563
Michael Lange8bc2a402016-01-21 18:10:16 +01001564#ifdef CONFIG_OF
1565/*
1566 * For devices with no IRQ directly connected to the SoC, the RTC chip
1567 * can be forced as a wakeup source by stating that explicitly in
1568 * the device's .dts file using the "wakeup-source" boolean property.
1569 * If the "wakeup-source" property is set, don't request an IRQ.
1570 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1571 * if supported by the RTC.
1572 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001573 if (chip->alarm && of_property_read_bool(client->dev.of_node,
1574 "wakeup-source"))
Michael Lange8bc2a402016-01-21 18:10:16 +01001575 ds1307_can_wakeup_device = true;
Michael Lange8bc2a402016-01-21 18:10:16 +01001576#endif
1577
David Brownell045e0e82007-07-17 04:04:55 -07001578 switch (ds1307->type) {
1579 case ds_1337:
1580 case ds_1339:
Nikita Yushchenko0759c882017-08-24 09:32:11 +03001581 case ds_1341:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001582 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001583 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001584 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001585 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001586 if (err) {
1587 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001588 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001589 }
1590
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001591 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001592 if (regs[0] & DS1337_BIT_nEOSC)
1593 regs[0] &= ~DS1337_BIT_nEOSC;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001594
David Anders40ce9722012-03-23 15:02:37 -07001595 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001596 * Using IRQ or defined as wakeup-source?
1597 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001598 * For some variants, be sure alarms can trigger when we're
1599 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001600 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001601 if (want_irq || ds1307_can_wakeup_device) {
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001602 regs[0] |= DS1337_BIT_INTCN | chip->bbsqi_bit;
1603 regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001604 }
1605
Heiner Kallweit11e58902017-03-10 18:52:34 +01001606 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001607 regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001608
1609 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001610 if (regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001611 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001612 regs[1] & ~DS1337_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001613 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001614 }
David Brownell045e0e82007-07-17 04:04:55 -07001615 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001616
1617 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001618 err = regmap_bulk_read(ds1307->regmap,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001619 RX8025_REG_CTRL1 << 4 | 0x08, regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001620 if (err) {
1621 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001622 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001623 }
1624
1625 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001626 if (!(regs[1] & RX8025_BIT_XST)) {
1627 regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001628 regmap_write(ds1307->regmap,
1629 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001630 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001631 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001632 "oscillator stop detected - SET TIME!\n");
1633 }
1634
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001635 if (regs[1] & RX8025_BIT_PON) {
1636 regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001637 regmap_write(ds1307->regmap,
1638 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001639 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001640 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001641 }
1642
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001643 if (regs[1] & RX8025_BIT_VDET) {
1644 regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001645 regmap_write(ds1307->regmap,
1646 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001647 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001648 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001649 }
1650
1651 /* make sure we are running in 24hour mode */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001652 if (!(regs[0] & RX8025_BIT_2412)) {
Matthias Fuchsa2166852009-03-31 15:24:58 -07001653 u8 hour;
1654
1655 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001656 regmap_write(ds1307->regmap,
1657 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001658 regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001659
Heiner Kallweit11e58902017-03-10 18:52:34 +01001660 err = regmap_bulk_read(ds1307->regmap,
1661 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001662 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001663 if (err) {
1664 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001665 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001666 }
1667
1668 /* correct hour */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001669 hour = bcd2bin(regs[DS1307_REG_HOUR]);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001670 if (hour == 12)
1671 hour = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001672 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
Matthias Fuchsa2166852009-03-31 15:24:58 -07001673 hour += 12;
1674
Heiner Kallweit11e58902017-03-10 18:52:34 +01001675 regmap_write(ds1307->regmap,
1676 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001677 }
1678 break;
David Brownell045e0e82007-07-17 04:04:55 -07001679 default:
1680 break;
1681 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001682
1683read_rtc:
1684 /* read RTC registers */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001685 err = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
1686 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +01001687 if (err) {
1688 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001689 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001690 }
1691
David Anders40ce9722012-03-23 15:02:37 -07001692 /*
1693 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001694 * specify the extra bits as must-be-zero, but there are
1695 * still a few values that are clearly out-of-range.
1696 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001697 tmp = regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001698 switch (ds1307->type) {
1699 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001700 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001701 case m41t00:
Giulio Benetti7e580762018-05-16 23:08:40 +02001702 case m41t11:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001703 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001704 if (tmp & DS1307_BIT_CH) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001705 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1706 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001707 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001708 }
David Brownell045e0e82007-07-17 04:04:55 -07001709 break;
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001710 case ds_1308:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001711 case ds_1338:
1712 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001713 if (tmp & DS1307_BIT_CH)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001714 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001715
1716 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001717 if (regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001718 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001719 regs[DS1307_REG_CONTROL] &
1720 ~DS1338_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001721 dev_warn(ds1307->dev, "SET TIME!\n");
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001722 goto read_rtc;
1723 }
David Brownell045e0e82007-07-17 04:04:55 -07001724 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001725 case ds_1340:
1726 /* clock halted? turn it on, so clock can tick. */
1727 if (tmp & DS1340_BIT_nEOSC)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001728 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
frederic Rodofcd8db02008-02-06 01:38:55 -08001729
Heiner Kallweit11e58902017-03-10 18:52:34 +01001730 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1731 if (err) {
1732 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001733 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001734 }
1735
1736 /* oscillator fault? clear flag, and warn */
1737 if (tmp & DS1340_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001738 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1739 dev_warn(ds1307->dev, "SET TIME!\n");
frederic Rodofcd8db02008-02-06 01:38:55 -08001740 }
1741 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001742 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001743 /* make sure that the backup battery is enabled */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001744 if (!(regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001745 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001746 regs[DS1307_REG_WDAY] |
Heiner Kallweit11e58902017-03-10 18:52:34 +01001747 MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001748 }
1749
1750 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001751 if (!(tmp & MCP794XX_BIT_ST)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001752 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1753 MCP794XX_BIT_ST);
1754 dev_warn(ds1307->dev, "SET TIME!\n");
David Anders43fcb812011-11-02 13:37:53 -07001755 goto read_rtc;
1756 }
1757
1758 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001759 default:
David Brownell045e0e82007-07-17 04:04:55 -07001760 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001761 }
David Brownell045e0e82007-07-17 04:04:55 -07001762
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001763 tmp = regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001764 switch (ds1307->type) {
1765 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001766 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001767 case m41t00:
Giulio Benetti7e580762018-05-16 23:08:40 +02001768 case m41t11:
David Anders40ce9722012-03-23 15:02:37 -07001769 /*
1770 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001771 * systems that will run through year 2100.
1772 */
1773 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001774 case rx_8025:
1775 break;
David Brownellc065f352007-07-17 04:05:10 -07001776 default:
1777 if (!(tmp & DS1307_BIT_12HR))
1778 break;
1779
David Anders40ce9722012-03-23 15:02:37 -07001780 /*
1781 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001782 * take note...
1783 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001784 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001785 if (tmp == 12)
1786 tmp = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001787 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
David Brownellc065f352007-07-17 04:05:10 -07001788 tmp += 12;
Heiner Kallweite5531702017-07-12 07:49:47 +02001789 regmap_write(ds1307->regmap, chip->offset + DS1307_REG_HOUR,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001790 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001791 }
1792
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001793 if (want_irq || ds1307_can_wakeup_device) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001794 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001795 set_bit(HAS_ALARM, &ds1307->flags);
1796 }
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001797
1798 ds1307->rtc = devm_rtc_allocate_device(ds1307->dev);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001799 if (IS_ERR(ds1307->rtc))
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001800 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001801
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001802 if (ds1307_can_wakeup_device && !want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001803 dev_info(ds1307->dev,
1804 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001805 /* We cannot support UIE mode if we do not have an IRQ line */
1806 ds1307->rtc->uie_unsupported = 1;
1807 }
1808
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001809 if (want_irq) {
Heiner Kallweit45947122017-07-12 07:49:41 +02001810 err = devm_request_threaded_irq(ds1307->dev, client->irq, NULL,
1811 chip->irq_handler ?: ds1307_irq,
Nishanth Menonc5983192015-06-23 11:15:11 -05001812 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001813 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001814 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001815 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001816 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001817 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001818 dev_err(ds1307->dev, "unable to request IRQ!\n");
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001819 } else {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001820 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001821 }
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001822 }
1823
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001824 ds1307->rtc->ops = chip->rtc_ops ?: &ds13xx_rtc_ops;
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001825 err = ds1307_add_frequency_test(ds1307);
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001826 if (err)
1827 return err;
1828
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001829 err = rtc_register_device(ds1307->rtc);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001830 if (err)
1831 return err;
1832
Austin Boyle9eab0a72012-03-23 15:02:38 -07001833 if (chip->nvram_size) {
Alexandre Belloni409baf12018-02-12 23:47:23 +01001834 struct nvmem_config nvmem_cfg = {
1835 .name = "ds1307_nvram",
1836 .word_size = 1,
1837 .stride = 1,
1838 .size = chip->nvram_size,
1839 .reg_read = ds1307_nvram_read,
1840 .reg_write = ds1307_nvram_write,
1841 .priv = ds1307,
1842 };
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001843
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001844 ds1307->rtc->nvram_old_abi = true;
Alexandre Belloni409baf12018-02-12 23:47:23 +01001845 rtc_nvmem_register(ds1307->rtc, &nvmem_cfg);
David Brownell682d73f2007-11-14 16:58:32 -08001846 }
1847
Akinobu Mita445c0202016-01-25 00:22:16 +09001848 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001849 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001850
David Brownell1abb0dc2006-06-25 05:48:17 -07001851 return 0;
1852
Jingoo Hanedca66d2013-07-03 15:07:05 -07001853exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001854 return err;
1855}
1856
David Brownell1abb0dc2006-06-25 05:48:17 -07001857static struct i2c_driver ds1307_driver = {
1858 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001859 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001860 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001861 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001862 },
David Brownellc065f352007-07-17 04:05:10 -07001863 .probe = ds1307_probe,
Jean Delvare3760f732008-04-29 23:11:40 +02001864 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001865};
1866
Axel Lin0abc9202012-03-23 15:02:31 -07001867module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001868
1869MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1870MODULE_LICENSE("GPL");