blob: 81e20653700d353e9be7543a82adbec037e3604a [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
197 /* read the RTC date and time registers all at once */
198 ret = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
199 sizeof(regs));
200 if (ret) {
201 dev_err(dev, "%s error %d\n", "read", ret);
202 return ret;
203 }
204
205 dev_dbg(dev, "%s: %7ph\n", "read", regs);
206
207 /* if oscillator fail bit is set, no data can be trusted */
208 if (ds1307->type == m41t0 &&
209 regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
210 dev_warn_once(dev, "oscillator failed, set time!\n");
211 return -EINVAL;
212 }
213
214 t->tm_sec = bcd2bin(regs[DS1307_REG_SECS] & 0x7f);
215 t->tm_min = bcd2bin(regs[DS1307_REG_MIN] & 0x7f);
216 tmp = regs[DS1307_REG_HOUR] & 0x3f;
217 t->tm_hour = bcd2bin(tmp);
218 t->tm_wday = bcd2bin(regs[DS1307_REG_WDAY] & 0x07) - 1;
219 t->tm_mday = bcd2bin(regs[DS1307_REG_MDAY] & 0x3f);
220 tmp = regs[DS1307_REG_MONTH] & 0x1f;
221 t->tm_mon = bcd2bin(tmp) - 1;
222 t->tm_year = bcd2bin(regs[DS1307_REG_YEAR]) + 100;
223
224 if (regs[chip->century_reg] & chip->century_bit &&
225 IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY))
226 t->tm_year += 100;
227
228 dev_dbg(dev, "%s secs=%d, mins=%d, "
229 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
230 "read", t->tm_sec, t->tm_min,
231 t->tm_hour, t->tm_mday,
232 t->tm_mon, t->tm_year, t->tm_wday);
233
234 return 0;
235}
236
237static int ds1307_set_time(struct device *dev, struct rtc_time *t)
238{
239 struct ds1307 *ds1307 = dev_get_drvdata(dev);
240 const struct chip_desc *chip = &chips[ds1307->type];
241 int result;
242 int tmp;
243 u8 regs[7];
244
245 dev_dbg(dev, "%s secs=%d, mins=%d, "
246 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
247 "write", t->tm_sec, t->tm_min,
248 t->tm_hour, t->tm_mday,
249 t->tm_mon, t->tm_year, t->tm_wday);
250
251 if (t->tm_year < 100)
252 return -EINVAL;
253
254#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
255 if (t->tm_year > (chip->century_bit ? 299 : 199))
256 return -EINVAL;
257#else
258 if (t->tm_year > 199)
259 return -EINVAL;
260#endif
261
262 regs[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
263 regs[DS1307_REG_MIN] = bin2bcd(t->tm_min);
264 regs[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
265 regs[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
266 regs[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
267 regs[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
268
269 /* assume 20YY not 19YY */
270 tmp = t->tm_year - 100;
271 regs[DS1307_REG_YEAR] = bin2bcd(tmp);
272
273 if (chip->century_enable_bit)
274 regs[chip->century_reg] |= chip->century_enable_bit;
275 if (t->tm_year > 199 && chip->century_bit)
276 regs[chip->century_reg] |= chip->century_bit;
277
278 if (ds1307->type == mcp794xx) {
279 /*
280 * these bits were cleared when preparing the date/time
281 * values and need to be set again before writing the
282 * regsfer out to the device.
283 */
284 regs[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
285 regs[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
286 }
287
288 dev_dbg(dev, "%s: %7ph\n", "write", regs);
289
290 result = regmap_bulk_write(ds1307->regmap, chip->offset, regs,
291 sizeof(regs));
292 if (result) {
293 dev_err(dev, "%s error %d\n", "write", result);
294 return result;
295 }
296 return 0;
297}
298
299static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
300{
301 struct ds1307 *ds1307 = dev_get_drvdata(dev);
302 int ret;
303 u8 regs[9];
304
305 if (!test_bit(HAS_ALARM, &ds1307->flags))
306 return -EINVAL;
307
308 /* read all ALARM1, ALARM2, and status registers at once */
309 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
310 regs, sizeof(regs));
311 if (ret) {
312 dev_err(dev, "%s error %d\n", "alarm read", ret);
313 return ret;
314 }
315
316 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
317 &regs[0], &regs[4], &regs[7]);
318
319 /*
320 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
321 * and that all four fields are checked matches
322 */
323 t->time.tm_sec = bcd2bin(regs[0] & 0x7f);
324 t->time.tm_min = bcd2bin(regs[1] & 0x7f);
325 t->time.tm_hour = bcd2bin(regs[2] & 0x3f);
326 t->time.tm_mday = bcd2bin(regs[3] & 0x3f);
327
328 /* ... and status */
329 t->enabled = !!(regs[7] & DS1337_BIT_A1IE);
330 t->pending = !!(regs[8] & DS1337_BIT_A1I);
331
332 dev_dbg(dev, "%s secs=%d, mins=%d, "
333 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
334 "alarm read", t->time.tm_sec, t->time.tm_min,
335 t->time.tm_hour, t->time.tm_mday,
336 t->enabled, t->pending);
337
338 return 0;
339}
340
341static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
342{
343 struct ds1307 *ds1307 = dev_get_drvdata(dev);
344 unsigned char regs[9];
345 u8 control, status;
346 int ret;
347
348 if (!test_bit(HAS_ALARM, &ds1307->flags))
349 return -EINVAL;
350
351 dev_dbg(dev, "%s secs=%d, mins=%d, "
352 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
353 "alarm set", t->time.tm_sec, t->time.tm_min,
354 t->time.tm_hour, t->time.tm_mday,
355 t->enabled, t->pending);
356
357 /* read current status of both alarms and the chip */
358 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
359 sizeof(regs));
360 if (ret) {
361 dev_err(dev, "%s error %d\n", "alarm write", ret);
362 return ret;
363 }
364 control = regs[7];
365 status = regs[8];
366
367 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
368 &regs[0], &regs[4], control, status);
369
370 /* set ALARM1, using 24 hour and day-of-month modes */
371 regs[0] = bin2bcd(t->time.tm_sec);
372 regs[1] = bin2bcd(t->time.tm_min);
373 regs[2] = bin2bcd(t->time.tm_hour);
374 regs[3] = bin2bcd(t->time.tm_mday);
375
376 /* set ALARM2 to non-garbage */
377 regs[4] = 0;
378 regs[5] = 0;
379 regs[6] = 0;
380
381 /* disable alarms */
382 regs[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
383 regs[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
384
385 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
386 sizeof(regs));
387 if (ret) {
388 dev_err(dev, "can't set alarm time\n");
389 return ret;
390 }
391
392 /* optionally enable ALARM1 */
393 if (t->enabled) {
394 dev_dbg(dev, "alarm IRQ armed\n");
395 regs[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
396 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, regs[7]);
397 }
398
399 return 0;
400}
401
402static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
403{
404 struct ds1307 *ds1307 = dev_get_drvdata(dev);
405
406 if (!test_bit(HAS_ALARM, &ds1307->flags))
407 return -ENOTTY;
408
409 return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
410 DS1337_BIT_A1IE,
411 enabled ? DS1337_BIT_A1IE : 0);
412}
413
414static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307, u32 ohms, bool diode)
415{
416 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
417 DS1307_TRICKLE_CHARGER_NO_DIODE;
418
419 switch (ohms) {
420 case 250:
421 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
422 break;
423 case 2000:
424 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
425 break;
426 case 4000:
427 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
428 break;
429 default:
430 dev_warn(ds1307->dev,
431 "Unsupported ohm value %u in dt\n", ohms);
432 return 0;
433 }
434 return setup;
435}
436
437static irqreturn_t rx8130_irq(int irq, void *dev_id)
438{
439 struct ds1307 *ds1307 = dev_id;
440 struct mutex *lock = &ds1307->rtc->ops_lock;
441 u8 ctl[3];
442 int ret;
443
444 mutex_lock(lock);
445
446 /* Read control registers. */
447 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
448 sizeof(ctl));
449 if (ret < 0)
450 goto out;
451 if (!(ctl[1] & RX8130_REG_FLAG_AF))
452 goto out;
453 ctl[1] &= ~RX8130_REG_FLAG_AF;
454 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
455
456 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
457 sizeof(ctl));
458 if (ret < 0)
459 goto out;
460
461 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
462
463out:
464 mutex_unlock(lock);
465
466 return IRQ_HANDLED;
467}
468
469static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
470{
471 struct ds1307 *ds1307 = dev_get_drvdata(dev);
472 u8 ald[3], ctl[3];
473 int ret;
474
475 if (!test_bit(HAS_ALARM, &ds1307->flags))
476 return -EINVAL;
477
478 /* Read alarm registers. */
479 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
480 sizeof(ald));
481 if (ret < 0)
482 return ret;
483
484 /* Read control registers. */
485 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
486 sizeof(ctl));
487 if (ret < 0)
488 return ret;
489
490 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
491 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
492
493 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
494 t->time.tm_sec = -1;
495 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
496 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
497 t->time.tm_wday = -1;
498 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
499 t->time.tm_mon = -1;
500 t->time.tm_year = -1;
501 t->time.tm_yday = -1;
502 t->time.tm_isdst = -1;
503
504 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
505 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
506 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
507
508 return 0;
509}
510
511static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
512{
513 struct ds1307 *ds1307 = dev_get_drvdata(dev);
514 u8 ald[3], ctl[3];
515 int ret;
516
517 if (!test_bit(HAS_ALARM, &ds1307->flags))
518 return -EINVAL;
519
520 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
521 "enabled=%d pending=%d\n", __func__,
522 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
523 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
524 t->enabled, t->pending);
525
526 /* Read control registers. */
527 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
528 sizeof(ctl));
529 if (ret < 0)
530 return ret;
531
532 ctl[0] &= ~RX8130_REG_EXTENSION_WADA;
533 ctl[1] |= RX8130_REG_FLAG_AF;
534 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
535
536 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
537 sizeof(ctl));
538 if (ret < 0)
539 return ret;
540
541 /* Hardware alarm precision is 1 minute! */
542 ald[0] = bin2bcd(t->time.tm_min);
543 ald[1] = bin2bcd(t->time.tm_hour);
544 ald[2] = bin2bcd(t->time.tm_mday);
545
546 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
547 sizeof(ald));
548 if (ret < 0)
549 return ret;
550
551 if (!t->enabled)
552 return 0;
553
554 ctl[2] |= RX8130_REG_CONTROL0_AIE;
555
556 return regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
557 sizeof(ctl));
558}
559
560static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
561{
562 struct ds1307 *ds1307 = dev_get_drvdata(dev);
563 int ret, reg;
564
565 if (!test_bit(HAS_ALARM, &ds1307->flags))
566 return -EINVAL;
567
568 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
569 if (ret < 0)
570 return ret;
571
572 if (enabled)
573 reg |= RX8130_REG_CONTROL0_AIE;
574 else
575 reg &= ~RX8130_REG_CONTROL0_AIE;
576
577 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
578}
579
580static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
581{
582 struct ds1307 *ds1307 = dev_id;
583 struct mutex *lock = &ds1307->rtc->ops_lock;
584 int reg, ret;
585
586 mutex_lock(lock);
587
588 /* Check and clear alarm 0 interrupt flag. */
589 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
590 if (ret)
591 goto out;
592 if (!(reg & MCP794XX_BIT_ALMX_IF))
593 goto out;
594 reg &= ~MCP794XX_BIT_ALMX_IF;
595 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
596 if (ret)
597 goto out;
598
599 /* Disable alarm 0. */
600 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
601 MCP794XX_BIT_ALM0_EN, 0);
602 if (ret)
603 goto out;
604
605 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
606
607out:
608 mutex_unlock(lock);
609
610 return IRQ_HANDLED;
611}
612
613static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
614{
615 struct ds1307 *ds1307 = dev_get_drvdata(dev);
616 u8 regs[10];
617 int ret;
618
619 if (!test_bit(HAS_ALARM, &ds1307->flags))
620 return -EINVAL;
621
622 /* Read control and alarm 0 registers. */
623 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
624 sizeof(regs));
625 if (ret)
626 return ret;
627
628 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
629
630 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
631 t->time.tm_sec = bcd2bin(regs[3] & 0x7f);
632 t->time.tm_min = bcd2bin(regs[4] & 0x7f);
633 t->time.tm_hour = bcd2bin(regs[5] & 0x3f);
634 t->time.tm_wday = bcd2bin(regs[6] & 0x7) - 1;
635 t->time.tm_mday = bcd2bin(regs[7] & 0x3f);
636 t->time.tm_mon = bcd2bin(regs[8] & 0x1f) - 1;
637 t->time.tm_year = -1;
638 t->time.tm_yday = -1;
639 t->time.tm_isdst = -1;
640
641 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
642 "enabled=%d polarity=%d irq=%d match=%lu\n", __func__,
643 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
644 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
645 !!(regs[6] & MCP794XX_BIT_ALMX_POL),
646 !!(regs[6] & MCP794XX_BIT_ALMX_IF),
647 (regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
648
649 return 0;
650}
651
652/*
653 * We may have a random RTC weekday, therefore calculate alarm weekday based
654 * on current weekday we read from the RTC timekeeping regs
655 */
656static int mcp794xx_alm_weekday(struct device *dev, struct rtc_time *tm_alarm)
657{
658 struct rtc_time tm_now;
659 int days_now, days_alarm, ret;
660
661 ret = ds1307_get_time(dev, &tm_now);
662 if (ret)
663 return ret;
664
665 days_now = div_s64(rtc_tm_to_time64(&tm_now), 24 * 60 * 60);
666 days_alarm = div_s64(rtc_tm_to_time64(tm_alarm), 24 * 60 * 60);
667
668 return (tm_now.tm_wday + days_alarm - days_now) % 7 + 1;
669}
670
671static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
672{
673 struct ds1307 *ds1307 = dev_get_drvdata(dev);
674 unsigned char regs[10];
675 int wday, ret;
676
677 if (!test_bit(HAS_ALARM, &ds1307->flags))
678 return -EINVAL;
679
680 wday = mcp794xx_alm_weekday(dev, &t->time);
681 if (wday < 0)
682 return wday;
683
684 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
685 "enabled=%d pending=%d\n", __func__,
686 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
687 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
688 t->enabled, t->pending);
689
690 /* Read control and alarm 0 registers. */
691 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
692 sizeof(regs));
693 if (ret)
694 return ret;
695
696 /* Set alarm 0, using 24-hour and day-of-month modes. */
697 regs[3] = bin2bcd(t->time.tm_sec);
698 regs[4] = bin2bcd(t->time.tm_min);
699 regs[5] = bin2bcd(t->time.tm_hour);
700 regs[6] = wday;
701 regs[7] = bin2bcd(t->time.tm_mday);
702 regs[8] = bin2bcd(t->time.tm_mon + 1);
703
704 /* Clear the alarm 0 interrupt flag. */
705 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
706 /* Set alarm match: second, minute, hour, day, date, month. */
707 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
708 /* Disable interrupt. We will not enable until completely programmed */
709 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
710
711 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
712 sizeof(regs));
713 if (ret)
714 return ret;
715
716 if (!t->enabled)
717 return 0;
718 regs[0] |= MCP794XX_BIT_ALM0_EN;
719 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
720}
721
722static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
723{
724 struct ds1307 *ds1307 = dev_get_drvdata(dev);
725
726 if (!test_bit(HAS_ALARM, &ds1307->flags))
727 return -EINVAL;
728
729 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
730 MCP794XX_BIT_ALM0_EN,
731 enabled ? MCP794XX_BIT_ALM0_EN : 0);
732}
733
734static int m41txx_rtc_read_offset(struct device *dev, long *offset)
735{
736 struct ds1307 *ds1307 = dev_get_drvdata(dev);
737 unsigned int ctrl_reg;
738 u8 val;
739
740 regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
741
742 val = ctrl_reg & M41TXX_M_CALIBRATION;
743
744 /* check if positive */
745 if (ctrl_reg & M41TXX_BIT_CALIB_SIGN)
746 *offset = (val * M41TXX_POS_OFFSET_STEP_PPB);
747 else
748 *offset = -(val * M41TXX_NEG_OFFSET_STEP_PPB);
749
750 return 0;
751}
752
753static int m41txx_rtc_set_offset(struct device *dev, long offset)
754{
755 struct ds1307 *ds1307 = dev_get_drvdata(dev);
756 unsigned int ctrl_reg;
757
758 if ((offset < M41TXX_MIN_OFFSET) || (offset > M41TXX_MAX_OFFSET))
759 return -ERANGE;
760
761 if (offset >= 0) {
762 ctrl_reg = DIV_ROUND_CLOSEST(offset,
763 M41TXX_POS_OFFSET_STEP_PPB);
764 ctrl_reg |= M41TXX_BIT_CALIB_SIGN;
765 } else {
766 ctrl_reg = DIV_ROUND_CLOSEST(abs(offset),
767 M41TXX_NEG_OFFSET_STEP_PPB);
768 }
769
770 return regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL,
771 M41TXX_M_CALIBRATION | M41TXX_BIT_CALIB_SIGN,
772 ctrl_reg);
773}
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700774
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200775static const struct rtc_class_ops rx8130_rtc_ops = {
776 .read_time = ds1307_get_time,
777 .set_time = ds1307_set_time,
778 .read_alarm = rx8130_read_alarm,
779 .set_alarm = rx8130_set_alarm,
780 .alarm_irq_enable = rx8130_alarm_irq_enable,
781};
782
783static const struct rtc_class_ops mcp794xx_rtc_ops = {
784 .read_time = ds1307_get_time,
785 .set_time = ds1307_set_time,
786 .read_alarm = mcp794xx_read_alarm,
787 .set_alarm = mcp794xx_set_alarm,
788 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
789};
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700790
Giulio Benetti79230ff2018-07-25 19:26:04 +0200791static const struct rtc_class_ops m41txx_rtc_ops = {
792 .read_time = ds1307_get_time,
793 .set_time = ds1307_set_time,
794 .read_alarm = ds1337_read_alarm,
795 .set_alarm = ds1337_set_alarm,
796 .alarm_irq_enable = ds1307_alarm_irq_enable,
797 .read_offset = m41txx_rtc_read_offset,
798 .set_offset = m41txx_rtc_set_offset,
799};
800
Heiner Kallweit7624df42017-07-12 07:49:33 +0200801static const struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700802 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700803 .nvram_offset = 8,
804 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700805 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200806 [ds_1308] = {
807 .nvram_offset = 8,
808 .nvram_size = 56,
809 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700810 [ds_1337] = {
811 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200812 .century_reg = DS1307_REG_MONTH,
813 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700814 },
815 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700816 .nvram_offset = 8,
817 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700818 },
819 [ds_1339] = {
820 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200821 .century_reg = DS1307_REG_MONTH,
822 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200823 .bbsqi_bit = DS1339_BIT_BBSQI,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700824 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700825 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700826 },
827 [ds_1340] = {
Heiner Kallweite48585d2017-06-05 17:57:33 +0200828 .century_reg = DS1307_REG_HOUR,
829 .century_enable_bit = DS1340_BIT_CENTURY_EN,
830 .century_bit = DS1340_BIT_CENTURY,
Andrea Greco51ed73eb2018-04-20 11:34:02 +0200831 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700832 .trickle_charger_reg = 0x08,
833 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300834 [ds_1341] = {
835 .century_reg = DS1307_REG_MONTH,
836 .century_bit = DS1337_BIT_CENTURY,
837 },
Wolfram Sangeb86c302012-05-29 15:07:38 -0700838 [ds_1388] = {
Heiner Kallweite5531702017-07-12 07:49:47 +0200839 .offset = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700840 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700841 },
842 [ds_3231] = {
843 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200844 .century_reg = DS1307_REG_MONTH,
845 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200846 .bbsqi_bit = DS3231_BIT_BBSQW,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700847 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200848 [rx_8130] = {
849 .alarm = 1,
850 /* this is battery backed SRAM */
851 .nvram_offset = 0x20,
852 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
Heiner Kallweite5531702017-07-12 07:49:47 +0200853 .offset = 0x10,
Heiner Kallweit45947122017-07-12 07:49:41 +0200854 .irq_handler = rx8130_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200855 .rtc_ops = &rx8130_rtc_ops,
Marek Vasutee0981b2017-06-18 22:55:28 +0200856 },
Giulio Benetti79230ff2018-07-25 19:26:04 +0200857 [m41t0] = {
858 .rtc_ops = &m41txx_rtc_ops,
859 },
860 [m41t00] = {
861 .rtc_ops = &m41txx_rtc_ops,
862 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200863 [m41t11] = {
864 /* this is battery backed SRAM */
865 .nvram_offset = 8,
866 .nvram_size = 56,
Giulio Benetti79230ff2018-07-25 19:26:04 +0200867 .rtc_ops = &m41txx_rtc_ops,
Giulio Benetti7e580762018-05-16 23:08:40 +0200868 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800869 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700870 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700871 /* this is battery backed SRAM */
872 .nvram_offset = 0x20,
873 .nvram_size = 0x40,
Heiner Kallweit45947122017-07-12 07:49:41 +0200874 .irq_handler = mcp794xx_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200875 .rtc_ops = &mcp794xx_rtc_ops,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700876 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700877};
David Brownell045e0e82007-07-17 04:04:55 -0700878
Jean Delvare3760f732008-04-29 23:11:40 +0200879static const struct i2c_device_id ds1307_id[] = {
880 { "ds1307", ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200881 { "ds1308", ds_1308 },
Jean Delvare3760f732008-04-29 23:11:40 +0200882 { "ds1337", ds_1337 },
883 { "ds1338", ds_1338 },
884 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700885 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200886 { "ds1340", ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300887 { "ds1341", ds_1341 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700888 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700889 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200890 { "m41t00", m41t00 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200891 { "m41t11", m41t11 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800892 { "mcp7940x", mcp794xx },
893 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700894 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700895 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200896 { "isl12057", ds_1337 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200897 { "rx8130", rx_8130 },
Jean Delvare3760f732008-04-29 23:11:40 +0200898 { }
899};
900MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700901
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300902#ifdef CONFIG_OF
903static const struct of_device_id ds1307_of_match[] = {
904 {
905 .compatible = "dallas,ds1307",
906 .data = (void *)ds_1307
907 },
908 {
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200909 .compatible = "dallas,ds1308",
910 .data = (void *)ds_1308
911 },
912 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300913 .compatible = "dallas,ds1337",
914 .data = (void *)ds_1337
915 },
916 {
917 .compatible = "dallas,ds1338",
918 .data = (void *)ds_1338
919 },
920 {
921 .compatible = "dallas,ds1339",
922 .data = (void *)ds_1339
923 },
924 {
925 .compatible = "dallas,ds1388",
926 .data = (void *)ds_1388
927 },
928 {
929 .compatible = "dallas,ds1340",
930 .data = (void *)ds_1340
931 },
932 {
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300933 .compatible = "dallas,ds1341",
934 .data = (void *)ds_1341
935 },
936 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300937 .compatible = "maxim,ds3231",
938 .data = (void *)ds_3231
939 },
940 {
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200941 .compatible = "st,m41t0",
Giulio Benetti146a5522018-05-16 23:08:39 +0200942 .data = (void *)m41t0
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200943 },
944 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300945 .compatible = "st,m41t00",
946 .data = (void *)m41t00
947 },
948 {
Giulio Benetti7e580762018-05-16 23:08:40 +0200949 .compatible = "st,m41t11",
950 .data = (void *)m41t11
951 },
952 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300953 .compatible = "microchip,mcp7940x",
954 .data = (void *)mcp794xx
955 },
956 {
957 .compatible = "microchip,mcp7941x",
958 .data = (void *)mcp794xx
959 },
960 {
961 .compatible = "pericom,pt7c4338",
962 .data = (void *)ds_1307
963 },
964 {
965 .compatible = "epson,rx8025",
966 .data = (void *)rx_8025
967 },
968 {
969 .compatible = "isil,isl12057",
970 .data = (void *)ds_1337
971 },
Bastian Stender47dd4722017-10-17 14:46:07 +0200972 {
973 .compatible = "epson,rx8130",
974 .data = (void *)rx_8130
975 },
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300976 { }
977};
978MODULE_DEVICE_TABLE(of, ds1307_of_match);
979#endif
980
Tin Huynh9c19b892016-11-30 09:57:31 +0700981#ifdef CONFIG_ACPI
982static const struct acpi_device_id ds1307_acpi_ids[] = {
983 { .id = "DS1307", .driver_data = ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200984 { .id = "DS1308", .driver_data = ds_1308 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700985 { .id = "DS1337", .driver_data = ds_1337 },
986 { .id = "DS1338", .driver_data = ds_1338 },
987 { .id = "DS1339", .driver_data = ds_1339 },
988 { .id = "DS1388", .driver_data = ds_1388 },
989 { .id = "DS1340", .driver_data = ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300990 { .id = "DS1341", .driver_data = ds_1341 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700991 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700992 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700993 { .id = "M41T00", .driver_data = m41t00 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200994 { .id = "M41T11", .driver_data = m41t11 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700995 { .id = "MCP7940X", .driver_data = mcp794xx },
996 { .id = "MCP7941X", .driver_data = mcp794xx },
997 { .id = "PT7C4338", .driver_data = ds_1307 },
998 { .id = "RX8025", .driver_data = rx_8025 },
999 { .id = "ISL12057", .driver_data = ds_1337 },
Bastian Stender47dd4722017-10-17 14:46:07 +02001000 { .id = "RX8130", .driver_data = rx_8130 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001001 { }
1002};
1003MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
1004#endif
1005
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001006/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001007 * The ds1337 and ds1339 both have two alarms, but we only use the first
1008 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
1009 * signal; ds1339 chips have only one alarm signal.
1010 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001011static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001012{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001013 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001014 struct mutex *lock = &ds1307->rtc->ops_lock;
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001015 int stat, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001016
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001017 mutex_lock(lock);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001018 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
1019 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001020 goto out;
1021
1022 if (stat & DS1337_BIT_A1I) {
1023 stat &= ~DS1337_BIT_A1I;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001024 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001025
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001026 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1027 DS1337_BIT_A1IE, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001028 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001029 goto out;
1030
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001031 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001032 }
1033
1034out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001035 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001036
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001037 return IRQ_HANDLED;
1038}
1039
1040/*----------------------------------------------------------------------*/
1041
David Brownellff8371a2006-09-30 23:28:17 -07001042static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -07001043 .read_time = ds1307_get_time,
1044 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -08001045 .read_alarm = ds1337_read_alarm,
1046 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -08001047 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -07001048};
1049
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001050static ssize_t frequency_test_store(struct device *dev,
1051 struct device_attribute *attr,
1052 const char *buf, size_t count)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001053{
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001054 struct ds1307 *ds1307 = dev_get_drvdata(dev->parent);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001055 bool freq_test_en;
1056 int ret;
1057
1058 ret = kstrtobool(buf, &freq_test_en);
1059 if (ret) {
1060 dev_err(dev, "Failed to store RTC Frequency Test attribute\n");
1061 return ret;
1062 }
1063
1064 regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL, M41TXX_BIT_FT,
1065 freq_test_en ? M41TXX_BIT_FT : 0);
1066
1067 return count;
1068}
1069
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001070static ssize_t frequency_test_show(struct device *dev,
1071 struct device_attribute *attr,
1072 char *buf)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001073{
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001074 struct ds1307 *ds1307 = dev_get_drvdata(dev->parent);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001075 unsigned int ctrl_reg;
1076
1077 regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
1078
1079 return scnprintf(buf, PAGE_SIZE, (ctrl_reg & M41TXX_BIT_FT) ? "on\n" :
1080 "off\n");
1081}
1082
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001083static DEVICE_ATTR_RW(frequency_test);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001084
1085static struct attribute *rtc_freq_test_attrs[] = {
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001086 &dev_attr_frequency_test.attr,
Giulio Benettib41c23e2018-07-25 19:26:05 +02001087 NULL,
1088};
1089
1090static const struct attribute_group rtc_freq_test_attr_group = {
1091 .attrs = rtc_freq_test_attrs,
1092};
1093
Giulio Benettib41c23e2018-07-25 19:26:05 +02001094static int ds1307_add_frequency_test(struct ds1307 *ds1307)
1095{
1096 int err;
1097
1098 switch (ds1307->type) {
1099 case m41t0:
1100 case m41t00:
1101 case m41t11:
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001102 err = rtc_add_group(ds1307->rtc, &rtc_freq_test_attr_group);
1103 if (err)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001104 return err;
Giulio Benettib41c23e2018-07-25 19:26:05 +02001105 break;
1106 default:
1107 break;
1108 }
1109
1110 return 0;
1111}
1112
Simon Guinot1d1945d2014-04-03 14:49:55 -07001113/*----------------------------------------------------------------------*/
1114
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001115static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
1116 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -08001117{
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001118 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +02001119 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -08001120
Heiner Kallweit969fa072017-07-12 07:49:54 +02001121 return regmap_bulk_read(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001122 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -08001123}
1124
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001125static int ds1307_nvram_write(void *priv, unsigned int offset, void *val,
1126 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -08001127{
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001128 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +02001129 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -08001130
Heiner Kallweit969fa072017-07-12 07:49:54 +02001131 return regmap_bulk_write(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001132 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -08001133}
1134
David Brownell682d73f2007-11-14 16:58:32 -08001135/*----------------------------------------------------------------------*/
1136
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001137static u8 ds1307_trickle_init(struct ds1307 *ds1307,
Heiner Kallweit7624df42017-07-12 07:49:33 +02001138 const struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001139{
Alexandre Belloni57ec2d92017-09-04 22:46:04 +02001140 u32 ohms;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001141 bool diode = true;
1142
1143 if (!chip->do_trickle_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001144 return 0;
1145
Heiner Kallweit11e58902017-03-10 18:52:34 +01001146 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
1147 &ohms))
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001148 return 0;
1149
Heiner Kallweit11e58902017-03-10 18:52:34 +01001150 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001151 diode = false;
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001152
1153 return chip->do_trickle_setup(ds1307, ohms, diode);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001154}
1155
Akinobu Mita445c0202016-01-25 00:22:16 +09001156/*----------------------------------------------------------------------*/
1157
Heiner Kallweit6b583a62017-09-27 22:41:26 +02001158#if IS_REACHABLE(CONFIG_HWMON)
Akinobu Mita445c0202016-01-25 00:22:16 +09001159
1160/*
1161 * Temperature sensor support for ds3231 devices.
1162 */
1163
1164#define DS3231_REG_TEMPERATURE 0x11
1165
1166/*
1167 * A user-initiated temperature conversion is not started by this function,
1168 * so the temperature is updated once every 64 seconds.
1169 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001170static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +09001171{
1172 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1173 u8 temp_buf[2];
1174 s16 temp;
1175 int ret;
1176
Heiner Kallweit11e58902017-03-10 18:52:34 +01001177 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
1178 temp_buf, sizeof(temp_buf));
1179 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +09001180 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +09001181 /*
1182 * Temperature is represented as a 10-bit code with a resolution of
1183 * 0.25 degree celsius and encoded in two's complement format.
1184 */
1185 temp = (temp_buf[0] << 8) | temp_buf[1];
1186 temp >>= 6;
1187 *mC = temp * 250;
1188
1189 return 0;
1190}
1191
1192static ssize_t ds3231_hwmon_show_temp(struct device *dev,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001193 struct device_attribute *attr, char *buf)
Akinobu Mita445c0202016-01-25 00:22:16 +09001194{
1195 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001196 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001197
1198 ret = ds3231_hwmon_read_temp(dev, &temp);
1199 if (ret)
1200 return ret;
1201
1202 return sprintf(buf, "%d\n", temp);
1203}
Alexandre Bellonib4be2712017-09-04 22:46:08 +02001204static SENSOR_DEVICE_ATTR(temp1_input, 0444, ds3231_hwmon_show_temp,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001205 NULL, 0);
Akinobu Mita445c0202016-01-25 00:22:16 +09001206
1207static struct attribute *ds3231_hwmon_attrs[] = {
1208 &sensor_dev_attr_temp1_input.dev_attr.attr,
1209 NULL,
1210};
1211ATTRIBUTE_GROUPS(ds3231_hwmon);
1212
1213static void ds1307_hwmon_register(struct ds1307 *ds1307)
1214{
1215 struct device *dev;
1216
1217 if (ds1307->type != ds_3231)
1218 return;
1219
Heiner Kallweit11e58902017-03-10 18:52:34 +01001220 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001221 ds1307,
1222 ds3231_hwmon_groups);
Akinobu Mita445c0202016-01-25 00:22:16 +09001223 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001224 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1225 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001226 }
1227}
1228
1229#else
1230
1231static void ds1307_hwmon_register(struct ds1307 *ds1307)
1232{
1233}
1234
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001235#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1236
1237/*----------------------------------------------------------------------*/
1238
1239/*
1240 * Square-wave output support for DS3231
1241 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1242 */
1243#ifdef CONFIG_COMMON_CLK
1244
1245enum {
1246 DS3231_CLK_SQW = 0,
1247 DS3231_CLK_32KHZ,
1248};
1249
1250#define clk_sqw_to_ds1307(clk) \
1251 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1252#define clk_32khz_to_ds1307(clk) \
1253 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1254
1255static int ds3231_clk_sqw_rates[] = {
1256 1,
1257 1024,
1258 4096,
1259 8192,
1260};
1261
1262static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1263{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001264 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001265 int ret;
1266
1267 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001268 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1269 mask, value);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001270 mutex_unlock(lock);
1271
1272 return ret;
1273}
1274
1275static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1276 unsigned long parent_rate)
1277{
1278 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001279 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001280 int rate_sel = 0;
1281
Heiner Kallweit11e58902017-03-10 18:52:34 +01001282 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1283 if (ret)
1284 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001285 if (control & DS1337_BIT_RS1)
1286 rate_sel += 1;
1287 if (control & DS1337_BIT_RS2)
1288 rate_sel += 2;
1289
1290 return ds3231_clk_sqw_rates[rate_sel];
1291}
1292
1293static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001294 unsigned long *prate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001295{
1296 int i;
1297
1298 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1299 if (ds3231_clk_sqw_rates[i] <= rate)
1300 return ds3231_clk_sqw_rates[i];
1301 }
1302
1303 return 0;
1304}
1305
1306static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001307 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001308{
1309 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1310 int control = 0;
1311 int rate_sel;
1312
1313 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1314 rate_sel++) {
1315 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1316 break;
1317 }
1318
1319 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1320 return -EINVAL;
1321
1322 if (rate_sel & 1)
1323 control |= DS1337_BIT_RS1;
1324 if (rate_sel & 2)
1325 control |= DS1337_BIT_RS2;
1326
1327 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1328 control);
1329}
1330
1331static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1332{
1333 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1334
1335 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1336}
1337
1338static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1339{
1340 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1341
1342 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1343}
1344
1345static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1346{
1347 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001348 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001349
Heiner Kallweit11e58902017-03-10 18:52:34 +01001350 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1351 if (ret)
1352 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001353
1354 return !(control & DS1337_BIT_INTCN);
1355}
1356
1357static const struct clk_ops ds3231_clk_sqw_ops = {
1358 .prepare = ds3231_clk_sqw_prepare,
1359 .unprepare = ds3231_clk_sqw_unprepare,
1360 .is_prepared = ds3231_clk_sqw_is_prepared,
1361 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1362 .round_rate = ds3231_clk_sqw_round_rate,
1363 .set_rate = ds3231_clk_sqw_set_rate,
1364};
1365
1366static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001367 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001368{
1369 return 32768;
1370}
1371
1372static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1373{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001374 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001375 int ret;
1376
1377 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001378 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1379 DS3231_BIT_EN32KHZ,
1380 enable ? DS3231_BIT_EN32KHZ : 0);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001381 mutex_unlock(lock);
1382
1383 return ret;
1384}
1385
1386static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1387{
1388 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1389
1390 return ds3231_clk_32khz_control(ds1307, true);
1391}
1392
1393static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1394{
1395 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1396
1397 ds3231_clk_32khz_control(ds1307, false);
1398}
1399
1400static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1401{
1402 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001403 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001404
Heiner Kallweit11e58902017-03-10 18:52:34 +01001405 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1406 if (ret)
1407 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001408
1409 return !!(status & DS3231_BIT_EN32KHZ);
1410}
1411
1412static const struct clk_ops ds3231_clk_32khz_ops = {
1413 .prepare = ds3231_clk_32khz_prepare,
1414 .unprepare = ds3231_clk_32khz_unprepare,
1415 .is_prepared = ds3231_clk_32khz_is_prepared,
1416 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1417};
1418
1419static struct clk_init_data ds3231_clks_init[] = {
1420 [DS3231_CLK_SQW] = {
1421 .name = "ds3231_clk_sqw",
1422 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001423 },
1424 [DS3231_CLK_32KHZ] = {
1425 .name = "ds3231_clk_32khz",
1426 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001427 },
1428};
1429
1430static int ds3231_clks_register(struct ds1307 *ds1307)
1431{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001432 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001433 struct clk_onecell_data *onecell;
1434 int i;
1435
Heiner Kallweit11e58902017-03-10 18:52:34 +01001436 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001437 if (!onecell)
1438 return -ENOMEM;
1439
1440 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001441 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1442 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001443 if (!onecell->clks)
1444 return -ENOMEM;
1445
1446 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1447 struct clk_init_data init = ds3231_clks_init[i];
1448
1449 /*
1450 * Interrupt signal due to alarm conditions and square-wave
1451 * output share same pin, so don't initialize both.
1452 */
1453 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1454 continue;
1455
1456 /* optional override of the clockname */
1457 of_property_read_string_index(node, "clock-output-names", i,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001458 &init.name);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001459 ds1307->clks[i].init = &init;
1460
Heiner Kallweit11e58902017-03-10 18:52:34 +01001461 onecell->clks[i] = devm_clk_register(ds1307->dev,
1462 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001463 if (IS_ERR(onecell->clks[i]))
1464 return PTR_ERR(onecell->clks[i]);
1465 }
1466
1467 if (!node)
1468 return 0;
1469
1470 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1471
1472 return 0;
1473}
1474
1475static void ds1307_clks_register(struct ds1307 *ds1307)
1476{
1477 int ret;
1478
1479 if (ds1307->type != ds_3231)
1480 return;
1481
1482 ret = ds3231_clks_register(ds1307);
1483 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001484 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1485 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001486 }
1487}
1488
1489#else
1490
1491static void ds1307_clks_register(struct ds1307 *ds1307)
1492{
1493}
1494
1495#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001496
Heiner Kallweit11e58902017-03-10 18:52:34 +01001497static const struct regmap_config regmap_config = {
1498 .reg_bits = 8,
1499 .val_bits = 8,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001500};
1501
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001502static int ds1307_probe(struct i2c_client *client,
1503 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001504{
1505 struct ds1307 *ds1307;
1506 int err = -ENODEV;
Heiner Kallweit584ce302017-08-29 21:52:56 +02001507 int tmp;
Heiner Kallweit7624df42017-07-12 07:49:33 +02001508 const struct chip_desc *chip;
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001509 bool want_irq;
Michael Lange8bc2a402016-01-21 18:10:16 +01001510 bool ds1307_can_wakeup_device = false;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001511 unsigned char regs[8];
Jingoo Han01ce8932013-11-12 15:10:41 -08001512 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001513 u8 trickle_charger_setup = 0;
David Brownell1abb0dc2006-06-25 05:48:17 -07001514
Jingoo Hanedca66d2013-07-03 15:07:05 -07001515 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001516 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001517 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001518
Heiner Kallweit11e58902017-03-10 18:52:34 +01001519 dev_set_drvdata(&client->dev, ds1307);
1520 ds1307->dev = &client->dev;
1521 ds1307->name = client->name;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001522
Heiner Kallweit11e58902017-03-10 18:52:34 +01001523 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1524 if (IS_ERR(ds1307->regmap)) {
1525 dev_err(ds1307->dev, "regmap allocation failed\n");
1526 return PTR_ERR(ds1307->regmap);
1527 }
1528
1529 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001530
1531 if (client->dev.of_node) {
1532 ds1307->type = (enum ds_type)
1533 of_device_get_match_data(&client->dev);
1534 chip = &chips[ds1307->type];
1535 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001536 chip = &chips[id->driver_data];
1537 ds1307->type = id->driver_data;
1538 } else {
1539 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001540
Tin Huynh9c19b892016-11-30 09:57:31 +07001541 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001542 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001543 if (!acpi_id)
1544 return -ENODEV;
1545 chip = &chips[acpi_id->driver_data];
1546 ds1307->type = acpi_id->driver_data;
1547 }
1548
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001549 want_irq = client->irq > 0 && chip->alarm;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001550
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001551 if (!pdata)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001552 trickle_charger_setup = ds1307_trickle_init(ds1307, chip);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001553 else if (pdata->trickle_charger_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001554 trickle_charger_setup = pdata->trickle_charger_setup;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001555
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001556 if (trickle_charger_setup && chip->trickle_charger_reg) {
1557 trickle_charger_setup |= DS13XX_TRICKLE_CHARGER_MAGIC;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001558 dev_dbg(ds1307->dev,
1559 "writing trickle charger info 0x%x to 0x%x\n",
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001560 trickle_charger_setup, chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001561 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001562 trickle_charger_setup);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001563 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001564
Michael Lange8bc2a402016-01-21 18:10:16 +01001565#ifdef CONFIG_OF
1566/*
1567 * For devices with no IRQ directly connected to the SoC, the RTC chip
1568 * can be forced as a wakeup source by stating that explicitly in
1569 * the device's .dts file using the "wakeup-source" boolean property.
1570 * If the "wakeup-source" property is set, don't request an IRQ.
1571 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1572 * if supported by the RTC.
1573 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001574 if (chip->alarm && of_property_read_bool(client->dev.of_node,
1575 "wakeup-source"))
Michael Lange8bc2a402016-01-21 18:10:16 +01001576 ds1307_can_wakeup_device = true;
Michael Lange8bc2a402016-01-21 18:10:16 +01001577#endif
1578
David Brownell045e0e82007-07-17 04:04:55 -07001579 switch (ds1307->type) {
1580 case ds_1337:
1581 case ds_1339:
Nikita Yushchenko0759c882017-08-24 09:32:11 +03001582 case ds_1341:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001583 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001584 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001585 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001586 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001587 if (err) {
1588 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001589 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001590 }
1591
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001592 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001593 if (regs[0] & DS1337_BIT_nEOSC)
1594 regs[0] &= ~DS1337_BIT_nEOSC;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001595
David Anders40ce9722012-03-23 15:02:37 -07001596 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001597 * Using IRQ or defined as wakeup-source?
1598 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001599 * For some variants, be sure alarms can trigger when we're
1600 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001601 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001602 if (want_irq || ds1307_can_wakeup_device) {
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001603 regs[0] |= DS1337_BIT_INTCN | chip->bbsqi_bit;
1604 regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001605 }
1606
Heiner Kallweit11e58902017-03-10 18:52:34 +01001607 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001608 regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001609
1610 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001611 if (regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001612 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001613 regs[1] & ~DS1337_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001614 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001615 }
David Brownell045e0e82007-07-17 04:04:55 -07001616 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001617
1618 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001619 err = regmap_bulk_read(ds1307->regmap,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001620 RX8025_REG_CTRL1 << 4 | 0x08, regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001621 if (err) {
1622 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001623 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001624 }
1625
1626 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001627 if (!(regs[1] & RX8025_BIT_XST)) {
1628 regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001629 regmap_write(ds1307->regmap,
1630 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001631 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001632 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001633 "oscillator stop detected - SET TIME!\n");
1634 }
1635
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001636 if (regs[1] & RX8025_BIT_PON) {
1637 regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001638 regmap_write(ds1307->regmap,
1639 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001640 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001641 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001642 }
1643
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001644 if (regs[1] & RX8025_BIT_VDET) {
1645 regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001646 regmap_write(ds1307->regmap,
1647 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001648 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001649 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001650 }
1651
1652 /* make sure we are running in 24hour mode */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001653 if (!(regs[0] & RX8025_BIT_2412)) {
Matthias Fuchsa2166852009-03-31 15:24:58 -07001654 u8 hour;
1655
1656 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001657 regmap_write(ds1307->regmap,
1658 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001659 regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001660
Heiner Kallweit11e58902017-03-10 18:52:34 +01001661 err = regmap_bulk_read(ds1307->regmap,
1662 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001663 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001664 if (err) {
1665 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001666 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001667 }
1668
1669 /* correct hour */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001670 hour = bcd2bin(regs[DS1307_REG_HOUR]);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001671 if (hour == 12)
1672 hour = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001673 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
Matthias Fuchsa2166852009-03-31 15:24:58 -07001674 hour += 12;
1675
Heiner Kallweit11e58902017-03-10 18:52:34 +01001676 regmap_write(ds1307->regmap,
1677 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001678 }
1679 break;
David Brownell045e0e82007-07-17 04:04:55 -07001680 default:
1681 break;
1682 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001683
1684read_rtc:
1685 /* read RTC registers */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001686 err = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
1687 sizeof(regs));
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;
David Brownell1abb0dc2006-06-25 05:48:17 -07001691 }
1692
David Anders40ce9722012-03-23 15:02:37 -07001693 /*
1694 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001695 * specify the extra bits as must-be-zero, but there are
1696 * still a few values that are clearly out-of-range.
1697 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001698 tmp = regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001699 switch (ds1307->type) {
1700 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001701 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001702 case m41t00:
Giulio Benetti7e580762018-05-16 23:08:40 +02001703 case m41t11:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001704 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001705 if (tmp & DS1307_BIT_CH) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001706 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1707 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001708 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001709 }
David Brownell045e0e82007-07-17 04:04:55 -07001710 break;
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001711 case ds_1308:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001712 case ds_1338:
1713 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001714 if (tmp & DS1307_BIT_CH)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001715 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001716
1717 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001718 if (regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001719 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001720 regs[DS1307_REG_CONTROL] &
1721 ~DS1338_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001722 dev_warn(ds1307->dev, "SET TIME!\n");
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001723 goto read_rtc;
1724 }
David Brownell045e0e82007-07-17 04:04:55 -07001725 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001726 case ds_1340:
1727 /* clock halted? turn it on, so clock can tick. */
1728 if (tmp & DS1340_BIT_nEOSC)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001729 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
frederic Rodofcd8db02008-02-06 01:38:55 -08001730
Heiner Kallweit11e58902017-03-10 18:52:34 +01001731 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1732 if (err) {
1733 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001734 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001735 }
1736
1737 /* oscillator fault? clear flag, and warn */
1738 if (tmp & DS1340_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001739 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1740 dev_warn(ds1307->dev, "SET TIME!\n");
frederic Rodofcd8db02008-02-06 01:38:55 -08001741 }
1742 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001743 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001744 /* make sure that the backup battery is enabled */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001745 if (!(regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001746 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001747 regs[DS1307_REG_WDAY] |
Heiner Kallweit11e58902017-03-10 18:52:34 +01001748 MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001749 }
1750
1751 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001752 if (!(tmp & MCP794XX_BIT_ST)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001753 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1754 MCP794XX_BIT_ST);
1755 dev_warn(ds1307->dev, "SET TIME!\n");
David Anders43fcb812011-11-02 13:37:53 -07001756 goto read_rtc;
1757 }
1758
1759 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001760 default:
David Brownell045e0e82007-07-17 04:04:55 -07001761 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001762 }
David Brownell045e0e82007-07-17 04:04:55 -07001763
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001764 tmp = regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001765 switch (ds1307->type) {
1766 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001767 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001768 case m41t00:
Giulio Benetti7e580762018-05-16 23:08:40 +02001769 case m41t11:
David Anders40ce9722012-03-23 15:02:37 -07001770 /*
1771 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001772 * systems that will run through year 2100.
1773 */
1774 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001775 case rx_8025:
1776 break;
David Brownellc065f352007-07-17 04:05:10 -07001777 default:
1778 if (!(tmp & DS1307_BIT_12HR))
1779 break;
1780
David Anders40ce9722012-03-23 15:02:37 -07001781 /*
1782 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001783 * take note...
1784 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001785 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001786 if (tmp == 12)
1787 tmp = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001788 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
David Brownellc065f352007-07-17 04:05:10 -07001789 tmp += 12;
Heiner Kallweite5531702017-07-12 07:49:47 +02001790 regmap_write(ds1307->regmap, chip->offset + DS1307_REG_HOUR,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001791 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001792 }
1793
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001794 if (want_irq || ds1307_can_wakeup_device) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001795 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001796 set_bit(HAS_ALARM, &ds1307->flags);
1797 }
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001798
1799 ds1307->rtc = devm_rtc_allocate_device(ds1307->dev);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001800 if (IS_ERR(ds1307->rtc))
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001801 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001802
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001803 if (ds1307_can_wakeup_device && !want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001804 dev_info(ds1307->dev,
1805 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001806 /* We cannot support UIE mode if we do not have an IRQ line */
1807 ds1307->rtc->uie_unsupported = 1;
1808 }
1809
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001810 if (want_irq) {
Heiner Kallweit45947122017-07-12 07:49:41 +02001811 err = devm_request_threaded_irq(ds1307->dev, client->irq, NULL,
1812 chip->irq_handler ?: ds1307_irq,
Nishanth Menonc5983192015-06-23 11:15:11 -05001813 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001814 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001815 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001816 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001817 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001818 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001819 dev_err(ds1307->dev, "unable to request IRQ!\n");
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001820 } else {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001821 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001822 }
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001823 }
1824
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001825 ds1307->rtc->ops = chip->rtc_ops ?: &ds13xx_rtc_ops;
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001826 err = ds1307_add_frequency_test(ds1307);
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001827 if (err)
1828 return err;
1829
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001830 err = rtc_register_device(ds1307->rtc);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001831 if (err)
1832 return err;
1833
Austin Boyle9eab0a72012-03-23 15:02:38 -07001834 if (chip->nvram_size) {
Alexandre Belloni409baf12018-02-12 23:47:23 +01001835 struct nvmem_config nvmem_cfg = {
1836 .name = "ds1307_nvram",
1837 .word_size = 1,
1838 .stride = 1,
1839 .size = chip->nvram_size,
1840 .reg_read = ds1307_nvram_read,
1841 .reg_write = ds1307_nvram_write,
1842 .priv = ds1307,
1843 };
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001844
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001845 ds1307->rtc->nvram_old_abi = true;
Alexandre Belloni409baf12018-02-12 23:47:23 +01001846 rtc_nvmem_register(ds1307->rtc, &nvmem_cfg);
David Brownell682d73f2007-11-14 16:58:32 -08001847 }
1848
Akinobu Mita445c0202016-01-25 00:22:16 +09001849 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001850 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001851
David Brownell1abb0dc2006-06-25 05:48:17 -07001852 return 0;
1853
Jingoo Hanedca66d2013-07-03 15:07:05 -07001854exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001855 return err;
1856}
1857
David Brownell1abb0dc2006-06-25 05:48:17 -07001858static struct i2c_driver ds1307_driver = {
1859 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001860 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001861 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001862 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001863 },
David Brownellc065f352007-07-17 04:05:10 -07001864 .probe = ds1307_probe,
Jean Delvare3760f732008-04-29 23:11:40 +02001865 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001866};
1867
Axel Lin0abc9202012-03-23 15:02:31 -07001868module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001869
1870MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1871MODULE_LICENSE("GPL");