blob: 31a38d468378ecb9ce74641c82a37af120c3fac8 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
David Brownell1abb0dc2006-06-25 05:48:17 -07002/*
3 * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips.
4 *
5 * Copyright (C) 2005 James Chapman (ds1337 core)
6 * Copyright (C) 2006 David Brownell
Matthias Fuchsa2166852009-03-31 15:24:58 -07007 * Copyright (C) 2009 Matthias Fuchs (rx8025 support)
Bertrand Achardbc48b902013-04-29 16:19:26 -07008 * Copyright (C) 2012 Bertrand Achard (nvram access fixes)
David Brownell1abb0dc2006-06-25 05:48:17 -07009 */
10
Tin Huynh9c19b892016-11-30 09:57:31 +070011#include <linux/acpi.h>
David Brownell1abb0dc2006-06-25 05:48:17 -070012#include <linux/bcd.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050013#include <linux/i2c.h>
14#include <linux/init.h>
15#include <linux/module.h>
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -030016#include <linux/of_device.h>
Wolfram Sangeb86c302012-05-29 15:07:38 -070017#include <linux/rtc/ds1307.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050018#include <linux/rtc.h>
19#include <linux/slab.h>
20#include <linux/string.h>
Akinobu Mita445c0202016-01-25 00:22:16 +090021#include <linux/hwmon.h>
22#include <linux/hwmon-sysfs.h>
Akinobu Mita6c6ff142016-01-31 23:10:10 +090023#include <linux/clk-provider.h>
Heiner Kallweit11e58902017-03-10 18:52:34 +010024#include <linux/regmap.h>
David Brownell1abb0dc2006-06-25 05:48:17 -070025
David Anders40ce9722012-03-23 15:02:37 -070026/*
27 * We can't determine type by probing, but if we expect pre-Linux code
David Brownell1abb0dc2006-06-25 05:48:17 -070028 * to have set the chip up as a clock (turning on the oscillator and
29 * setting the date and time), Linux can ignore the non-clock features.
30 * That's a natural job for a factory or repair bench.
David Brownell1abb0dc2006-06-25 05:48:17 -070031 */
32enum ds_type {
David Brownell045e0e82007-07-17 04:04:55 -070033 ds_1307,
Sean Nyekjaer300a7732017-06-08 12:36:54 +020034 ds_1308,
David Brownell045e0e82007-07-17 04:04:55 -070035 ds_1337,
36 ds_1338,
37 ds_1339,
38 ds_1340,
Nikita Yushchenko0759c882017-08-24 09:32:11 +030039 ds_1341,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -070040 ds_1388,
Wolfram Sang97f902b2009-06-17 16:26:10 -070041 ds_3231,
Stefan Agner8566f702017-03-23 16:54:57 -070042 m41t0,
David Brownell045e0e82007-07-17 04:04:55 -070043 m41t00,
Giulio Benetti7e580762018-05-16 23:08:40 +020044 m41t11,
Tomas Novotnyf4199f82014-12-10 15:53:57 -080045 mcp794xx,
Matthias Fuchsa2166852009-03-31 15:24:58 -070046 rx_8025,
Marek Vasutee0981b2017-06-18 22:55:28 +020047 rx_8130,
Wolfram Sang32d322b2012-03-23 15:02:36 -070048 last_ds_type /* always last */
David Anders40ce9722012-03-23 15:02:37 -070049 /* rs5c372 too? different address... */
David Brownell1abb0dc2006-06-25 05:48:17 -070050};
51
David Brownell1abb0dc2006-06-25 05:48:17 -070052/* RTC registers don't differ much, except for the century flag */
53#define DS1307_REG_SECS 0x00 /* 00-59 */
54# define DS1307_BIT_CH 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070055# define DS1340_BIT_nEOSC 0x80
Tomas Novotnyf4199f82014-12-10 15:53:57 -080056# define MCP794XX_BIT_ST 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070057#define DS1307_REG_MIN 0x01 /* 00-59 */
Stefan Agner8566f702017-03-23 16:54:57 -070058# define M41T0_BIT_OF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070059#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
David Brownellc065f352007-07-17 04:05:10 -070060# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
61# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
David Brownell1abb0dc2006-06-25 05:48:17 -070062# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
63# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
64#define DS1307_REG_WDAY 0x03 /* 01-07 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -080065# define MCP794XX_BIT_VBATEN 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -070066#define DS1307_REG_MDAY 0x04 /* 01-31 */
67#define DS1307_REG_MONTH 0x05 /* 01-12 */
68# define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
69#define DS1307_REG_YEAR 0x06 /* 00-99 */
70
David Anders40ce9722012-03-23 15:02:37 -070071/*
72 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
David Brownell045e0e82007-07-17 04:04:55 -070073 * start at 7, and they differ a LOT. Only control and status matter for
74 * basic RTC date and time functionality; be careful using them.
David Brownell1abb0dc2006-06-25 05:48:17 -070075 */
David Brownell045e0e82007-07-17 04:04:55 -070076#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
David Brownell1abb0dc2006-06-25 05:48:17 -070077# define DS1307_BIT_OUT 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070078# define DS1338_BIT_OSF 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070079# define DS1307_BIT_SQWE 0x10
80# define DS1307_BIT_RS1 0x02
81# define DS1307_BIT_RS0 0x01
82#define DS1337_REG_CONTROL 0x0e
83# define DS1337_BIT_nEOSC 0x80
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070084# define DS1339_BIT_BBSQI 0x20
Wolfram Sang97f902b2009-06-17 16:26:10 -070085# define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
David Brownell1abb0dc2006-06-25 05:48:17 -070086# define DS1337_BIT_RS2 0x10
87# define DS1337_BIT_RS1 0x08
88# define DS1337_BIT_INTCN 0x04
89# define DS1337_BIT_A2IE 0x02
90# define DS1337_BIT_A1IE 0x01
David Brownell045e0e82007-07-17 04:04:55 -070091#define DS1340_REG_CONTROL 0x07
92# define DS1340_BIT_OUT 0x80
93# define DS1340_BIT_FT 0x40
94# define DS1340_BIT_CALIB_SIGN 0x20
95# define DS1340_M_CALIBRATION 0x1f
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070096#define DS1340_REG_FLAG 0x09
97# define DS1340_BIT_OSF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070098#define DS1337_REG_STATUS 0x0f
99# define DS1337_BIT_OSF 0x80
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900100# define DS3231_BIT_EN32KHZ 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -0700101# define DS1337_BIT_A2I 0x02
102# define DS1337_BIT_A1I 0x01
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700103#define DS1339_REG_ALARM1_SECS 0x07
Wolfram Sangeb86c302012-05-29 15:07:38 -0700104
105#define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
David Brownell1abb0dc2006-06-25 05:48:17 -0700106
Matthias Fuchsa2166852009-03-31 15:24:58 -0700107#define RX8025_REG_CTRL1 0x0e
108# define RX8025_BIT_2412 0x20
109#define RX8025_REG_CTRL2 0x0f
110# define RX8025_BIT_PON 0x10
111# define RX8025_BIT_VDET 0x40
112# define RX8025_BIT_XST 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -0700113
Uwe Kleine-König3ffd4a22019-01-25 15:35:56 +0100114#define RX8130_REG_ALARM_MIN 0x17
115#define RX8130_REG_ALARM_HOUR 0x18
116#define RX8130_REG_ALARM_WEEK_OR_DAY 0x19
117#define RX8130_REG_EXTENSION 0x1c
Uwe Kleine-König92cbf122019-01-25 15:35:54 +0100118#define RX8130_REG_EXTENSION_WADA BIT(3)
Uwe Kleine-König3ffd4a22019-01-25 15:35:56 +0100119#define RX8130_REG_FLAG 0x1d
120#define RX8130_REG_FLAG_VLF BIT(1)
Uwe Kleine-König92cbf122019-01-25 15:35:54 +0100121#define RX8130_REG_FLAG_AF BIT(3)
Uwe Kleine-König3ffd4a22019-01-25 15:35:56 +0100122#define RX8130_REG_CONTROL0 0x1e
Uwe Kleine-König92cbf122019-01-25 15:35:54 +0100123#define RX8130_REG_CONTROL0_AIE BIT(3)
124
125#define MCP794XX_REG_CONTROL 0x07
126# define MCP794XX_BIT_ALM0_EN 0x10
127# define MCP794XX_BIT_ALM1_EN 0x20
128#define MCP794XX_REG_ALARM0_BASE 0x0a
129#define MCP794XX_REG_ALARM0_CTRL 0x0d
130#define MCP794XX_REG_ALARM1_BASE 0x11
131#define MCP794XX_REG_ALARM1_CTRL 0x14
132# define MCP794XX_BIT_ALMX_IF BIT(3)
133# define MCP794XX_BIT_ALMX_C0 BIT(4)
134# define MCP794XX_BIT_ALMX_C1 BIT(5)
135# define MCP794XX_BIT_ALMX_C2 BIT(6)
136# define MCP794XX_BIT_ALMX_POL BIT(7)
137# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
138 MCP794XX_BIT_ALMX_C1 | \
139 MCP794XX_BIT_ALMX_C2)
140
Giulio Benetti79230ff2018-07-25 19:26:04 +0200141#define M41TXX_REG_CONTROL 0x07
142# define M41TXX_BIT_OUT BIT(7)
143# define M41TXX_BIT_FT BIT(6)
144# define M41TXX_BIT_CALIB_SIGN BIT(5)
145# define M41TXX_M_CALIBRATION GENMASK(4, 0)
146
Chris Packhamdf11b322020-02-07 16:18:11 +1300147#define DS1388_REG_FLAG 0x0b
148# define DS1388_BIT_OSF BIT(7)
Giulio Benetti79230ff2018-07-25 19:26:04 +0200149/* 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
Uwe Kleine-König501f9822019-01-25 15:35:57 +0100196 if (ds1307->type == rx_8130) {
197 unsigned int regflag;
198 ret = regmap_read(ds1307->regmap, RX8130_REG_FLAG, &regflag);
199 if (ret) {
200 dev_err(dev, "%s error %d\n", "read", ret);
201 return ret;
202 }
203
204 if (regflag & RX8130_REG_FLAG_VLF) {
205 dev_warn_once(dev, "oscillator failed, set time!\n");
206 return -EINVAL;
207 }
208 }
209
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100210 /* read the RTC date and time registers all at once */
211 ret = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
212 sizeof(regs));
213 if (ret) {
214 dev_err(dev, "%s error %d\n", "read", ret);
215 return ret;
216 }
217
218 dev_dbg(dev, "%s: %7ph\n", "read", regs);
219
220 /* if oscillator fail bit is set, no data can be trusted */
221 if (ds1307->type == m41t0 &&
222 regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
223 dev_warn_once(dev, "oscillator failed, set time!\n");
224 return -EINVAL;
225 }
226
Alexandre Bellonib3a50162019-04-11 00:16:29 +0200227 tmp = regs[DS1307_REG_SECS];
228 switch (ds1307->type) {
229 case ds_1307:
230 case m41t0:
231 case m41t00:
232 case m41t11:
233 if (tmp & DS1307_BIT_CH)
234 return -EINVAL;
235 break;
236 case ds_1308:
237 case ds_1338:
238 if (tmp & DS1307_BIT_CH)
239 return -EINVAL;
240
241 ret = regmap_read(ds1307->regmap, DS1307_REG_CONTROL, &tmp);
242 if (ret)
243 return ret;
244 if (tmp & DS1338_BIT_OSF)
245 return -EINVAL;
246 break;
247 case ds_1340:
248 if (tmp & DS1340_BIT_nEOSC)
249 return -EINVAL;
250
251 ret = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
252 if (ret)
253 return ret;
254 if (tmp & DS1340_BIT_OSF)
255 return -EINVAL;
256 break;
Chris Packhamdf11b322020-02-07 16:18:11 +1300257 case ds_1388:
258 ret = regmap_read(ds1307->regmap, DS1388_REG_FLAG, &tmp);
259 if (ret)
260 return ret;
261 if (tmp & DS1388_BIT_OSF)
262 return -EINVAL;
263 break;
Alexandre Bellonib3a50162019-04-11 00:16:29 +0200264 case mcp794xx:
265 if (!(tmp & MCP794XX_BIT_ST))
266 return -EINVAL;
267
268 break;
269 default:
270 break;
271 }
272
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100273 t->tm_sec = bcd2bin(regs[DS1307_REG_SECS] & 0x7f);
274 t->tm_min = bcd2bin(regs[DS1307_REG_MIN] & 0x7f);
275 tmp = regs[DS1307_REG_HOUR] & 0x3f;
276 t->tm_hour = bcd2bin(tmp);
277 t->tm_wday = bcd2bin(regs[DS1307_REG_WDAY] & 0x07) - 1;
278 t->tm_mday = bcd2bin(regs[DS1307_REG_MDAY] & 0x3f);
279 tmp = regs[DS1307_REG_MONTH] & 0x1f;
280 t->tm_mon = bcd2bin(tmp) - 1;
281 t->tm_year = bcd2bin(regs[DS1307_REG_YEAR]) + 100;
282
283 if (regs[chip->century_reg] & chip->century_bit &&
284 IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY))
285 t->tm_year += 100;
286
287 dev_dbg(dev, "%s secs=%d, mins=%d, "
288 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
289 "read", t->tm_sec, t->tm_min,
290 t->tm_hour, t->tm_mday,
291 t->tm_mon, t->tm_year, t->tm_wday);
292
293 return 0;
294}
295
296static int ds1307_set_time(struct device *dev, struct rtc_time *t)
297{
298 struct ds1307 *ds1307 = dev_get_drvdata(dev);
299 const struct chip_desc *chip = &chips[ds1307->type];
300 int result;
301 int tmp;
302 u8 regs[7];
303
304 dev_dbg(dev, "%s secs=%d, mins=%d, "
305 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
306 "write", t->tm_sec, t->tm_min,
307 t->tm_hour, t->tm_mday,
308 t->tm_mon, t->tm_year, t->tm_wday);
309
310 if (t->tm_year < 100)
311 return -EINVAL;
312
313#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
314 if (t->tm_year > (chip->century_bit ? 299 : 199))
315 return -EINVAL;
316#else
317 if (t->tm_year > 199)
318 return -EINVAL;
319#endif
320
321 regs[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
322 regs[DS1307_REG_MIN] = bin2bcd(t->tm_min);
323 regs[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
324 regs[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
325 regs[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
326 regs[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
327
328 /* assume 20YY not 19YY */
329 tmp = t->tm_year - 100;
330 regs[DS1307_REG_YEAR] = bin2bcd(tmp);
331
332 if (chip->century_enable_bit)
333 regs[chip->century_reg] |= chip->century_enable_bit;
334 if (t->tm_year > 199 && chip->century_bit)
335 regs[chip->century_reg] |= chip->century_bit;
336
Alexandre Bellonib3a50162019-04-11 00:16:29 +0200337 switch (ds1307->type) {
338 case ds_1308:
339 case ds_1338:
340 regmap_update_bits(ds1307->regmap, DS1307_REG_CONTROL,
341 DS1338_BIT_OSF, 0);
342 break;
343 case ds_1340:
344 regmap_update_bits(ds1307->regmap, DS1340_REG_FLAG,
345 DS1340_BIT_OSF, 0);
346 break;
347 case mcp794xx:
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100348 /*
349 * these bits were cleared when preparing the date/time
350 * values and need to be set again before writing the
351 * regsfer out to the device.
352 */
353 regs[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
354 regs[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
Alexandre Bellonib3a50162019-04-11 00:16:29 +0200355 break;
356 default:
357 break;
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100358 }
359
360 dev_dbg(dev, "%s: %7ph\n", "write", regs);
361
362 result = regmap_bulk_write(ds1307->regmap, chip->offset, regs,
363 sizeof(regs));
364 if (result) {
365 dev_err(dev, "%s error %d\n", "write", result);
366 return result;
367 }
Uwe Kleine-König501f9822019-01-25 15:35:57 +0100368
369 if (ds1307->type == rx_8130) {
370 /* clear Voltage Loss Flag as data is available now */
371 result = regmap_write(ds1307->regmap, RX8130_REG_FLAG,
372 ~(u8)RX8130_REG_FLAG_VLF);
373 if (result) {
374 dev_err(dev, "%s error %d\n", "write", result);
375 return result;
376 }
377 }
378
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100379 return 0;
380}
381
382static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
383{
384 struct ds1307 *ds1307 = dev_get_drvdata(dev);
385 int ret;
386 u8 regs[9];
387
388 if (!test_bit(HAS_ALARM, &ds1307->flags))
389 return -EINVAL;
390
391 /* read all ALARM1, ALARM2, and status registers at once */
392 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
393 regs, sizeof(regs));
394 if (ret) {
395 dev_err(dev, "%s error %d\n", "alarm read", ret);
396 return ret;
397 }
398
399 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
400 &regs[0], &regs[4], &regs[7]);
401
402 /*
403 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
404 * and that all four fields are checked matches
405 */
406 t->time.tm_sec = bcd2bin(regs[0] & 0x7f);
407 t->time.tm_min = bcd2bin(regs[1] & 0x7f);
408 t->time.tm_hour = bcd2bin(regs[2] & 0x3f);
409 t->time.tm_mday = bcd2bin(regs[3] & 0x3f);
410
411 /* ... and status */
412 t->enabled = !!(regs[7] & DS1337_BIT_A1IE);
413 t->pending = !!(regs[8] & DS1337_BIT_A1I);
414
415 dev_dbg(dev, "%s secs=%d, mins=%d, "
416 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
417 "alarm read", t->time.tm_sec, t->time.tm_min,
418 t->time.tm_hour, t->time.tm_mday,
419 t->enabled, t->pending);
420
421 return 0;
422}
423
424static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
425{
426 struct ds1307 *ds1307 = dev_get_drvdata(dev);
427 unsigned char regs[9];
428 u8 control, status;
429 int ret;
430
431 if (!test_bit(HAS_ALARM, &ds1307->flags))
432 return -EINVAL;
433
434 dev_dbg(dev, "%s secs=%d, mins=%d, "
435 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
436 "alarm set", t->time.tm_sec, t->time.tm_min,
437 t->time.tm_hour, t->time.tm_mday,
438 t->enabled, t->pending);
439
440 /* read current status of both alarms and the chip */
441 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
442 sizeof(regs));
443 if (ret) {
444 dev_err(dev, "%s error %d\n", "alarm write", ret);
445 return ret;
446 }
447 control = regs[7];
448 status = regs[8];
449
450 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
451 &regs[0], &regs[4], control, status);
452
453 /* set ALARM1, using 24 hour and day-of-month modes */
454 regs[0] = bin2bcd(t->time.tm_sec);
455 regs[1] = bin2bcd(t->time.tm_min);
456 regs[2] = bin2bcd(t->time.tm_hour);
457 regs[3] = bin2bcd(t->time.tm_mday);
458
459 /* set ALARM2 to non-garbage */
460 regs[4] = 0;
461 regs[5] = 0;
462 regs[6] = 0;
463
464 /* disable alarms */
465 regs[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
466 regs[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
467
468 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
469 sizeof(regs));
470 if (ret) {
471 dev_err(dev, "can't set alarm time\n");
472 return ret;
473 }
474
475 /* optionally enable ALARM1 */
476 if (t->enabled) {
477 dev_dbg(dev, "alarm IRQ armed\n");
478 regs[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
479 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, regs[7]);
480 }
481
482 return 0;
483}
484
485static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
486{
487 struct ds1307 *ds1307 = dev_get_drvdata(dev);
488
489 if (!test_bit(HAS_ALARM, &ds1307->flags))
490 return -ENOTTY;
491
492 return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
493 DS1337_BIT_A1IE,
494 enabled ? DS1337_BIT_A1IE : 0);
495}
496
497static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307, u32 ohms, bool diode)
498{
499 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
500 DS1307_TRICKLE_CHARGER_NO_DIODE;
501
502 switch (ohms) {
503 case 250:
504 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
505 break;
506 case 2000:
507 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
508 break;
509 case 4000:
510 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
511 break;
512 default:
513 dev_warn(ds1307->dev,
514 "Unsupported ohm value %u in dt\n", ohms);
515 return 0;
516 }
517 return setup;
518}
519
520static irqreturn_t rx8130_irq(int irq, void *dev_id)
521{
522 struct ds1307 *ds1307 = dev_id;
523 struct mutex *lock = &ds1307->rtc->ops_lock;
524 u8 ctl[3];
525 int ret;
526
527 mutex_lock(lock);
528
529 /* Read control registers. */
530 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
531 sizeof(ctl));
532 if (ret < 0)
533 goto out;
534 if (!(ctl[1] & RX8130_REG_FLAG_AF))
535 goto out;
536 ctl[1] &= ~RX8130_REG_FLAG_AF;
537 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
538
539 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
540 sizeof(ctl));
541 if (ret < 0)
542 goto out;
543
544 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
545
546out:
547 mutex_unlock(lock);
548
549 return IRQ_HANDLED;
550}
551
552static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
553{
554 struct ds1307 *ds1307 = dev_get_drvdata(dev);
555 u8 ald[3], ctl[3];
556 int ret;
557
558 if (!test_bit(HAS_ALARM, &ds1307->flags))
559 return -EINVAL;
560
561 /* Read alarm registers. */
562 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
563 sizeof(ald));
564 if (ret < 0)
565 return ret;
566
567 /* Read control registers. */
568 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
569 sizeof(ctl));
570 if (ret < 0)
571 return ret;
572
573 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
574 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
575
576 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
577 t->time.tm_sec = -1;
578 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
579 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
580 t->time.tm_wday = -1;
581 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
582 t->time.tm_mon = -1;
583 t->time.tm_year = -1;
584 t->time.tm_yday = -1;
585 t->time.tm_isdst = -1;
586
587 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
588 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
589 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
590
591 return 0;
592}
593
594static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
595{
596 struct ds1307 *ds1307 = dev_get_drvdata(dev);
597 u8 ald[3], ctl[3];
598 int ret;
599
600 if (!test_bit(HAS_ALARM, &ds1307->flags))
601 return -EINVAL;
602
603 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
604 "enabled=%d pending=%d\n", __func__,
605 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
606 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
607 t->enabled, t->pending);
608
609 /* Read control registers. */
610 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
611 sizeof(ctl));
612 if (ret < 0)
613 return ret;
614
Uwe Kleine-König3f929ca2019-01-25 15:35:58 +0100615 ctl[0] &= RX8130_REG_EXTENSION_WADA;
616 ctl[1] &= ~RX8130_REG_FLAG_AF;
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100617 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
618
619 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
620 sizeof(ctl));
621 if (ret < 0)
622 return ret;
623
624 /* Hardware alarm precision is 1 minute! */
625 ald[0] = bin2bcd(t->time.tm_min);
626 ald[1] = bin2bcd(t->time.tm_hour);
627 ald[2] = bin2bcd(t->time.tm_mday);
628
629 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
630 sizeof(ald));
631 if (ret < 0)
632 return ret;
633
634 if (!t->enabled)
635 return 0;
636
637 ctl[2] |= RX8130_REG_CONTROL0_AIE;
638
Uwe Kleine-König3f929ca2019-01-25 15:35:58 +0100639 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, ctl[2]);
Uwe Kleine-Königd0e3f612019-01-25 15:35:55 +0100640}
641
642static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
643{
644 struct ds1307 *ds1307 = dev_get_drvdata(dev);
645 int ret, reg;
646
647 if (!test_bit(HAS_ALARM, &ds1307->flags))
648 return -EINVAL;
649
650 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
651 if (ret < 0)
652 return ret;
653
654 if (enabled)
655 reg |= RX8130_REG_CONTROL0_AIE;
656 else
657 reg &= ~RX8130_REG_CONTROL0_AIE;
658
659 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
660}
661
662static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
663{
664 struct ds1307 *ds1307 = dev_id;
665 struct mutex *lock = &ds1307->rtc->ops_lock;
666 int reg, ret;
667
668 mutex_lock(lock);
669
670 /* Check and clear alarm 0 interrupt flag. */
671 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
672 if (ret)
673 goto out;
674 if (!(reg & MCP794XX_BIT_ALMX_IF))
675 goto out;
676 reg &= ~MCP794XX_BIT_ALMX_IF;
677 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
678 if (ret)
679 goto out;
680
681 /* Disable alarm 0. */
682 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
683 MCP794XX_BIT_ALM0_EN, 0);
684 if (ret)
685 goto out;
686
687 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
688
689out:
690 mutex_unlock(lock);
691
692 return IRQ_HANDLED;
693}
694
695static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
696{
697 struct ds1307 *ds1307 = dev_get_drvdata(dev);
698 u8 regs[10];
699 int ret;
700
701 if (!test_bit(HAS_ALARM, &ds1307->flags))
702 return -EINVAL;
703
704 /* Read control and alarm 0 registers. */
705 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
706 sizeof(regs));
707 if (ret)
708 return ret;
709
710 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
711
712 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
713 t->time.tm_sec = bcd2bin(regs[3] & 0x7f);
714 t->time.tm_min = bcd2bin(regs[4] & 0x7f);
715 t->time.tm_hour = bcd2bin(regs[5] & 0x3f);
716 t->time.tm_wday = bcd2bin(regs[6] & 0x7) - 1;
717 t->time.tm_mday = bcd2bin(regs[7] & 0x3f);
718 t->time.tm_mon = bcd2bin(regs[8] & 0x1f) - 1;
719 t->time.tm_year = -1;
720 t->time.tm_yday = -1;
721 t->time.tm_isdst = -1;
722
723 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
724 "enabled=%d polarity=%d irq=%d match=%lu\n", __func__,
725 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
726 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
727 !!(regs[6] & MCP794XX_BIT_ALMX_POL),
728 !!(regs[6] & MCP794XX_BIT_ALMX_IF),
729 (regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
730
731 return 0;
732}
733
734/*
735 * We may have a random RTC weekday, therefore calculate alarm weekday based
736 * on current weekday we read from the RTC timekeeping regs
737 */
738static int mcp794xx_alm_weekday(struct device *dev, struct rtc_time *tm_alarm)
739{
740 struct rtc_time tm_now;
741 int days_now, days_alarm, ret;
742
743 ret = ds1307_get_time(dev, &tm_now);
744 if (ret)
745 return ret;
746
747 days_now = div_s64(rtc_tm_to_time64(&tm_now), 24 * 60 * 60);
748 days_alarm = div_s64(rtc_tm_to_time64(tm_alarm), 24 * 60 * 60);
749
750 return (tm_now.tm_wday + days_alarm - days_now) % 7 + 1;
751}
752
753static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
754{
755 struct ds1307 *ds1307 = dev_get_drvdata(dev);
756 unsigned char regs[10];
757 int wday, ret;
758
759 if (!test_bit(HAS_ALARM, &ds1307->flags))
760 return -EINVAL;
761
762 wday = mcp794xx_alm_weekday(dev, &t->time);
763 if (wday < 0)
764 return wday;
765
766 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
767 "enabled=%d pending=%d\n", __func__,
768 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
769 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
770 t->enabled, t->pending);
771
772 /* Read control and alarm 0 registers. */
773 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
774 sizeof(regs));
775 if (ret)
776 return ret;
777
778 /* Set alarm 0, using 24-hour and day-of-month modes. */
779 regs[3] = bin2bcd(t->time.tm_sec);
780 regs[4] = bin2bcd(t->time.tm_min);
781 regs[5] = bin2bcd(t->time.tm_hour);
782 regs[6] = wday;
783 regs[7] = bin2bcd(t->time.tm_mday);
784 regs[8] = bin2bcd(t->time.tm_mon + 1);
785
786 /* Clear the alarm 0 interrupt flag. */
787 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
788 /* Set alarm match: second, minute, hour, day, date, month. */
789 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
790 /* Disable interrupt. We will not enable until completely programmed */
791 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
792
793 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
794 sizeof(regs));
795 if (ret)
796 return ret;
797
798 if (!t->enabled)
799 return 0;
800 regs[0] |= MCP794XX_BIT_ALM0_EN;
801 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
802}
803
804static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
805{
806 struct ds1307 *ds1307 = dev_get_drvdata(dev);
807
808 if (!test_bit(HAS_ALARM, &ds1307->flags))
809 return -EINVAL;
810
811 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
812 MCP794XX_BIT_ALM0_EN,
813 enabled ? MCP794XX_BIT_ALM0_EN : 0);
814}
815
816static int m41txx_rtc_read_offset(struct device *dev, long *offset)
817{
818 struct ds1307 *ds1307 = dev_get_drvdata(dev);
819 unsigned int ctrl_reg;
820 u8 val;
821
822 regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
823
824 val = ctrl_reg & M41TXX_M_CALIBRATION;
825
826 /* check if positive */
827 if (ctrl_reg & M41TXX_BIT_CALIB_SIGN)
828 *offset = (val * M41TXX_POS_OFFSET_STEP_PPB);
829 else
830 *offset = -(val * M41TXX_NEG_OFFSET_STEP_PPB);
831
832 return 0;
833}
834
835static int m41txx_rtc_set_offset(struct device *dev, long offset)
836{
837 struct ds1307 *ds1307 = dev_get_drvdata(dev);
838 unsigned int ctrl_reg;
839
840 if ((offset < M41TXX_MIN_OFFSET) || (offset > M41TXX_MAX_OFFSET))
841 return -ERANGE;
842
843 if (offset >= 0) {
844 ctrl_reg = DIV_ROUND_CLOSEST(offset,
845 M41TXX_POS_OFFSET_STEP_PPB);
846 ctrl_reg |= M41TXX_BIT_CALIB_SIGN;
847 } else {
848 ctrl_reg = DIV_ROUND_CLOSEST(abs(offset),
849 M41TXX_NEG_OFFSET_STEP_PPB);
850 }
851
852 return regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL,
853 M41TXX_M_CALIBRATION | M41TXX_BIT_CALIB_SIGN,
854 ctrl_reg);
855}
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700856
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200857static const struct rtc_class_ops rx8130_rtc_ops = {
858 .read_time = ds1307_get_time,
859 .set_time = ds1307_set_time,
860 .read_alarm = rx8130_read_alarm,
861 .set_alarm = rx8130_set_alarm,
862 .alarm_irq_enable = rx8130_alarm_irq_enable,
863};
864
865static const struct rtc_class_ops mcp794xx_rtc_ops = {
866 .read_time = ds1307_get_time,
867 .set_time = ds1307_set_time,
868 .read_alarm = mcp794xx_read_alarm,
869 .set_alarm = mcp794xx_set_alarm,
870 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
871};
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700872
Giulio Benetti79230ff2018-07-25 19:26:04 +0200873static const struct rtc_class_ops m41txx_rtc_ops = {
874 .read_time = ds1307_get_time,
875 .set_time = ds1307_set_time,
876 .read_alarm = ds1337_read_alarm,
877 .set_alarm = ds1337_set_alarm,
878 .alarm_irq_enable = ds1307_alarm_irq_enable,
879 .read_offset = m41txx_rtc_read_offset,
880 .set_offset = m41txx_rtc_set_offset,
881};
882
Heiner Kallweit7624df42017-07-12 07:49:33 +0200883static const struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700884 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700885 .nvram_offset = 8,
886 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700887 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200888 [ds_1308] = {
889 .nvram_offset = 8,
890 .nvram_size = 56,
891 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700892 [ds_1337] = {
893 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200894 .century_reg = DS1307_REG_MONTH,
895 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700896 },
897 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700898 .nvram_offset = 8,
899 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700900 },
901 [ds_1339] = {
902 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200903 .century_reg = DS1307_REG_MONTH,
904 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200905 .bbsqi_bit = DS1339_BIT_BBSQI,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700906 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700907 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700908 },
909 [ds_1340] = {
Heiner Kallweite48585d2017-06-05 17:57:33 +0200910 .century_reg = DS1307_REG_HOUR,
911 .century_enable_bit = DS1340_BIT_CENTURY_EN,
912 .century_bit = DS1340_BIT_CENTURY,
Andrea Greco51ed73eb2018-04-20 11:34:02 +0200913 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700914 .trickle_charger_reg = 0x08,
915 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300916 [ds_1341] = {
917 .century_reg = DS1307_REG_MONTH,
918 .century_bit = DS1337_BIT_CENTURY,
919 },
Wolfram Sangeb86c302012-05-29 15:07:38 -0700920 [ds_1388] = {
Heiner Kallweite5531702017-07-12 07:49:47 +0200921 .offset = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700922 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700923 },
924 [ds_3231] = {
925 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200926 .century_reg = DS1307_REG_MONTH,
927 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200928 .bbsqi_bit = DS3231_BIT_BBSQW,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700929 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200930 [rx_8130] = {
931 .alarm = 1,
932 /* this is battery backed SRAM */
933 .nvram_offset = 0x20,
934 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
Heiner Kallweite5531702017-07-12 07:49:47 +0200935 .offset = 0x10,
Heiner Kallweit45947122017-07-12 07:49:41 +0200936 .irq_handler = rx8130_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200937 .rtc_ops = &rx8130_rtc_ops,
Marek Vasutee0981b2017-06-18 22:55:28 +0200938 },
Giulio Benetti79230ff2018-07-25 19:26:04 +0200939 [m41t0] = {
940 .rtc_ops = &m41txx_rtc_ops,
941 },
942 [m41t00] = {
943 .rtc_ops = &m41txx_rtc_ops,
944 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200945 [m41t11] = {
946 /* this is battery backed SRAM */
947 .nvram_offset = 8,
948 .nvram_size = 56,
Giulio Benetti79230ff2018-07-25 19:26:04 +0200949 .rtc_ops = &m41txx_rtc_ops,
Giulio Benetti7e580762018-05-16 23:08:40 +0200950 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800951 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700952 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700953 /* this is battery backed SRAM */
954 .nvram_offset = 0x20,
955 .nvram_size = 0x40,
Heiner Kallweit45947122017-07-12 07:49:41 +0200956 .irq_handler = mcp794xx_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200957 .rtc_ops = &mcp794xx_rtc_ops,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700958 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700959};
David Brownell045e0e82007-07-17 04:04:55 -0700960
Jean Delvare3760f732008-04-29 23:11:40 +0200961static const struct i2c_device_id ds1307_id[] = {
962 { "ds1307", ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200963 { "ds1308", ds_1308 },
Jean Delvare3760f732008-04-29 23:11:40 +0200964 { "ds1337", ds_1337 },
965 { "ds1338", ds_1338 },
966 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700967 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200968 { "ds1340", ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300969 { "ds1341", ds_1341 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700970 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700971 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200972 { "m41t00", m41t00 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200973 { "m41t11", m41t11 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800974 { "mcp7940x", mcp794xx },
975 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700976 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700977 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200978 { "isl12057", ds_1337 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200979 { "rx8130", rx_8130 },
Jean Delvare3760f732008-04-29 23:11:40 +0200980 { }
981};
982MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700983
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300984#ifdef CONFIG_OF
985static const struct of_device_id ds1307_of_match[] = {
986 {
987 .compatible = "dallas,ds1307",
988 .data = (void *)ds_1307
989 },
990 {
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200991 .compatible = "dallas,ds1308",
992 .data = (void *)ds_1308
993 },
994 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300995 .compatible = "dallas,ds1337",
996 .data = (void *)ds_1337
997 },
998 {
999 .compatible = "dallas,ds1338",
1000 .data = (void *)ds_1338
1001 },
1002 {
1003 .compatible = "dallas,ds1339",
1004 .data = (void *)ds_1339
1005 },
1006 {
1007 .compatible = "dallas,ds1388",
1008 .data = (void *)ds_1388
1009 },
1010 {
1011 .compatible = "dallas,ds1340",
1012 .data = (void *)ds_1340
1013 },
1014 {
Nikita Yushchenko0759c882017-08-24 09:32:11 +03001015 .compatible = "dallas,ds1341",
1016 .data = (void *)ds_1341
1017 },
1018 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001019 .compatible = "maxim,ds3231",
1020 .data = (void *)ds_3231
1021 },
1022 {
Alexandre Bellonidb2f8142017-04-08 17:22:02 +02001023 .compatible = "st,m41t0",
Giulio Benetti146a5522018-05-16 23:08:39 +02001024 .data = (void *)m41t0
Alexandre Bellonidb2f8142017-04-08 17:22:02 +02001025 },
1026 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001027 .compatible = "st,m41t00",
1028 .data = (void *)m41t00
1029 },
1030 {
Giulio Benetti7e580762018-05-16 23:08:40 +02001031 .compatible = "st,m41t11",
1032 .data = (void *)m41t11
1033 },
1034 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001035 .compatible = "microchip,mcp7940x",
1036 .data = (void *)mcp794xx
1037 },
1038 {
1039 .compatible = "microchip,mcp7941x",
1040 .data = (void *)mcp794xx
1041 },
1042 {
1043 .compatible = "pericom,pt7c4338",
1044 .data = (void *)ds_1307
1045 },
1046 {
1047 .compatible = "epson,rx8025",
1048 .data = (void *)rx_8025
1049 },
1050 {
1051 .compatible = "isil,isl12057",
1052 .data = (void *)ds_1337
1053 },
Bastian Stender47dd4722017-10-17 14:46:07 +02001054 {
1055 .compatible = "epson,rx8130",
1056 .data = (void *)rx_8130
1057 },
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001058 { }
1059};
1060MODULE_DEVICE_TABLE(of, ds1307_of_match);
1061#endif
1062
Tin Huynh9c19b892016-11-30 09:57:31 +07001063#ifdef CONFIG_ACPI
1064static const struct acpi_device_id ds1307_acpi_ids[] = {
1065 { .id = "DS1307", .driver_data = ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001066 { .id = "DS1308", .driver_data = ds_1308 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001067 { .id = "DS1337", .driver_data = ds_1337 },
1068 { .id = "DS1338", .driver_data = ds_1338 },
1069 { .id = "DS1339", .driver_data = ds_1339 },
1070 { .id = "DS1388", .driver_data = ds_1388 },
1071 { .id = "DS1340", .driver_data = ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +03001072 { .id = "DS1341", .driver_data = ds_1341 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001073 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -07001074 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001075 { .id = "M41T00", .driver_data = m41t00 },
Giulio Benetti7e580762018-05-16 23:08:40 +02001076 { .id = "M41T11", .driver_data = m41t11 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001077 { .id = "MCP7940X", .driver_data = mcp794xx },
1078 { .id = "MCP7941X", .driver_data = mcp794xx },
1079 { .id = "PT7C4338", .driver_data = ds_1307 },
1080 { .id = "RX8025", .driver_data = rx_8025 },
1081 { .id = "ISL12057", .driver_data = ds_1337 },
Bastian Stender47dd4722017-10-17 14:46:07 +02001082 { .id = "RX8130", .driver_data = rx_8130 },
Tin Huynh9c19b892016-11-30 09:57:31 +07001083 { }
1084};
1085MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
1086#endif
1087
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001088/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001089 * The ds1337 and ds1339 both have two alarms, but we only use the first
1090 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
1091 * signal; ds1339 chips have only one alarm signal.
1092 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001093static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001094{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001095 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001096 struct mutex *lock = &ds1307->rtc->ops_lock;
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001097 int stat, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001098
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001099 mutex_lock(lock);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001100 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
1101 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001102 goto out;
1103
1104 if (stat & DS1337_BIT_A1I) {
1105 stat &= ~DS1337_BIT_A1I;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001106 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001107
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001108 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1109 DS1337_BIT_A1IE, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001110 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001111 goto out;
1112
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001113 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001114 }
1115
1116out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001117 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001118
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001119 return IRQ_HANDLED;
1120}
1121
1122/*----------------------------------------------------------------------*/
1123
David Brownellff8371a2006-09-30 23:28:17 -07001124static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -07001125 .read_time = ds1307_get_time,
1126 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -08001127 .read_alarm = ds1337_read_alarm,
1128 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -08001129 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -07001130};
1131
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001132static ssize_t frequency_test_store(struct device *dev,
1133 struct device_attribute *attr,
1134 const char *buf, size_t count)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001135{
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001136 struct ds1307 *ds1307 = dev_get_drvdata(dev->parent);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001137 bool freq_test_en;
1138 int ret;
1139
1140 ret = kstrtobool(buf, &freq_test_en);
1141 if (ret) {
1142 dev_err(dev, "Failed to store RTC Frequency Test attribute\n");
1143 return ret;
1144 }
1145
1146 regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL, M41TXX_BIT_FT,
1147 freq_test_en ? M41TXX_BIT_FT : 0);
1148
1149 return count;
1150}
1151
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001152static ssize_t frequency_test_show(struct device *dev,
1153 struct device_attribute *attr,
1154 char *buf)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001155{
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001156 struct ds1307 *ds1307 = dev_get_drvdata(dev->parent);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001157 unsigned int ctrl_reg;
1158
1159 regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
1160
1161 return scnprintf(buf, PAGE_SIZE, (ctrl_reg & M41TXX_BIT_FT) ? "on\n" :
1162 "off\n");
1163}
1164
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001165static DEVICE_ATTR_RW(frequency_test);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001166
1167static struct attribute *rtc_freq_test_attrs[] = {
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001168 &dev_attr_frequency_test.attr,
Giulio Benettib41c23e2018-07-25 19:26:05 +02001169 NULL,
1170};
1171
1172static const struct attribute_group rtc_freq_test_attr_group = {
1173 .attrs = rtc_freq_test_attrs,
1174};
1175
Giulio Benettib41c23e2018-07-25 19:26:05 +02001176static int ds1307_add_frequency_test(struct ds1307 *ds1307)
1177{
1178 int err;
1179
1180 switch (ds1307->type) {
1181 case m41t0:
1182 case m41t00:
1183 case m41t11:
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001184 err = rtc_add_group(ds1307->rtc, &rtc_freq_test_attr_group);
1185 if (err)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001186 return err;
Giulio Benettib41c23e2018-07-25 19:26:05 +02001187 break;
1188 default:
1189 break;
1190 }
1191
1192 return 0;
1193}
1194
Simon Guinot1d1945d2014-04-03 14:49:55 -07001195/*----------------------------------------------------------------------*/
1196
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001197static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
1198 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -08001199{
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001200 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +02001201 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -08001202
Heiner Kallweit969fa072017-07-12 07:49:54 +02001203 return regmap_bulk_read(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001204 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -08001205}
1206
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001207static int ds1307_nvram_write(void *priv, unsigned int offset, void *val,
1208 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -08001209{
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001210 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +02001211 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -08001212
Heiner Kallweit969fa072017-07-12 07:49:54 +02001213 return regmap_bulk_write(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001214 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -08001215}
1216
David Brownell682d73f2007-11-14 16:58:32 -08001217/*----------------------------------------------------------------------*/
1218
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001219static u8 ds1307_trickle_init(struct ds1307 *ds1307,
Heiner Kallweit7624df42017-07-12 07:49:33 +02001220 const struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001221{
Alexandre Belloni57ec2d92017-09-04 22:46:04 +02001222 u32 ohms;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001223 bool diode = true;
1224
1225 if (!chip->do_trickle_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001226 return 0;
1227
Heiner Kallweit11e58902017-03-10 18:52:34 +01001228 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
1229 &ohms))
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001230 return 0;
1231
Heiner Kallweit11e58902017-03-10 18:52:34 +01001232 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001233 diode = false;
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001234
1235 return chip->do_trickle_setup(ds1307, ohms, diode);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001236}
1237
Akinobu Mita445c0202016-01-25 00:22:16 +09001238/*----------------------------------------------------------------------*/
1239
Heiner Kallweit6b583a62017-09-27 22:41:26 +02001240#if IS_REACHABLE(CONFIG_HWMON)
Akinobu Mita445c0202016-01-25 00:22:16 +09001241
1242/*
1243 * Temperature sensor support for ds3231 devices.
1244 */
1245
1246#define DS3231_REG_TEMPERATURE 0x11
1247
1248/*
1249 * A user-initiated temperature conversion is not started by this function,
1250 * so the temperature is updated once every 64 seconds.
1251 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001252static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +09001253{
1254 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1255 u8 temp_buf[2];
1256 s16 temp;
1257 int ret;
1258
Heiner Kallweit11e58902017-03-10 18:52:34 +01001259 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
1260 temp_buf, sizeof(temp_buf));
1261 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +09001262 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +09001263 /*
1264 * Temperature is represented as a 10-bit code with a resolution of
1265 * 0.25 degree celsius and encoded in two's complement format.
1266 */
1267 temp = (temp_buf[0] << 8) | temp_buf[1];
1268 temp >>= 6;
1269 *mC = temp * 250;
1270
1271 return 0;
1272}
1273
1274static ssize_t ds3231_hwmon_show_temp(struct device *dev,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001275 struct device_attribute *attr, char *buf)
Akinobu Mita445c0202016-01-25 00:22:16 +09001276{
1277 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001278 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001279
1280 ret = ds3231_hwmon_read_temp(dev, &temp);
1281 if (ret)
1282 return ret;
1283
1284 return sprintf(buf, "%d\n", temp);
1285}
Alexandre Bellonib4be2712017-09-04 22:46:08 +02001286static SENSOR_DEVICE_ATTR(temp1_input, 0444, ds3231_hwmon_show_temp,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001287 NULL, 0);
Akinobu Mita445c0202016-01-25 00:22:16 +09001288
1289static struct attribute *ds3231_hwmon_attrs[] = {
1290 &sensor_dev_attr_temp1_input.dev_attr.attr,
1291 NULL,
1292};
1293ATTRIBUTE_GROUPS(ds3231_hwmon);
1294
1295static void ds1307_hwmon_register(struct ds1307 *ds1307)
1296{
1297 struct device *dev;
1298
1299 if (ds1307->type != ds_3231)
1300 return;
1301
Heiner Kallweit11e58902017-03-10 18:52:34 +01001302 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001303 ds1307,
1304 ds3231_hwmon_groups);
Akinobu Mita445c0202016-01-25 00:22:16 +09001305 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001306 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1307 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001308 }
1309}
1310
1311#else
1312
1313static void ds1307_hwmon_register(struct ds1307 *ds1307)
1314{
1315}
1316
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001317#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1318
1319/*----------------------------------------------------------------------*/
1320
1321/*
1322 * Square-wave output support for DS3231
1323 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1324 */
1325#ifdef CONFIG_COMMON_CLK
1326
1327enum {
1328 DS3231_CLK_SQW = 0,
1329 DS3231_CLK_32KHZ,
1330};
1331
1332#define clk_sqw_to_ds1307(clk) \
1333 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1334#define clk_32khz_to_ds1307(clk) \
1335 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1336
1337static int ds3231_clk_sqw_rates[] = {
1338 1,
1339 1024,
1340 4096,
1341 8192,
1342};
1343
1344static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1345{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001346 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001347 int ret;
1348
1349 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001350 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1351 mask, value);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001352 mutex_unlock(lock);
1353
1354 return ret;
1355}
1356
1357static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1358 unsigned long parent_rate)
1359{
1360 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001361 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001362 int rate_sel = 0;
1363
Heiner Kallweit11e58902017-03-10 18:52:34 +01001364 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1365 if (ret)
1366 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001367 if (control & DS1337_BIT_RS1)
1368 rate_sel += 1;
1369 if (control & DS1337_BIT_RS2)
1370 rate_sel += 2;
1371
1372 return ds3231_clk_sqw_rates[rate_sel];
1373}
1374
1375static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001376 unsigned long *prate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001377{
1378 int i;
1379
1380 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1381 if (ds3231_clk_sqw_rates[i] <= rate)
1382 return ds3231_clk_sqw_rates[i];
1383 }
1384
1385 return 0;
1386}
1387
1388static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001389 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001390{
1391 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1392 int control = 0;
1393 int rate_sel;
1394
1395 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1396 rate_sel++) {
1397 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1398 break;
1399 }
1400
1401 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1402 return -EINVAL;
1403
1404 if (rate_sel & 1)
1405 control |= DS1337_BIT_RS1;
1406 if (rate_sel & 2)
1407 control |= DS1337_BIT_RS2;
1408
1409 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1410 control);
1411}
1412
1413static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1414{
1415 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1416
1417 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1418}
1419
1420static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1421{
1422 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1423
1424 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1425}
1426
1427static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1428{
1429 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001430 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001431
Heiner Kallweit11e58902017-03-10 18:52:34 +01001432 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1433 if (ret)
1434 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001435
1436 return !(control & DS1337_BIT_INTCN);
1437}
1438
1439static const struct clk_ops ds3231_clk_sqw_ops = {
1440 .prepare = ds3231_clk_sqw_prepare,
1441 .unprepare = ds3231_clk_sqw_unprepare,
1442 .is_prepared = ds3231_clk_sqw_is_prepared,
1443 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1444 .round_rate = ds3231_clk_sqw_round_rate,
1445 .set_rate = ds3231_clk_sqw_set_rate,
1446};
1447
1448static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001449 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001450{
1451 return 32768;
1452}
1453
1454static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1455{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001456 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001457 int ret;
1458
1459 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001460 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1461 DS3231_BIT_EN32KHZ,
1462 enable ? DS3231_BIT_EN32KHZ : 0);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001463 mutex_unlock(lock);
1464
1465 return ret;
1466}
1467
1468static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1469{
1470 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1471
1472 return ds3231_clk_32khz_control(ds1307, true);
1473}
1474
1475static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1476{
1477 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1478
1479 ds3231_clk_32khz_control(ds1307, false);
1480}
1481
1482static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1483{
1484 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001485 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001486
Heiner Kallweit11e58902017-03-10 18:52:34 +01001487 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1488 if (ret)
1489 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001490
1491 return !!(status & DS3231_BIT_EN32KHZ);
1492}
1493
1494static const struct clk_ops ds3231_clk_32khz_ops = {
1495 .prepare = ds3231_clk_32khz_prepare,
1496 .unprepare = ds3231_clk_32khz_unprepare,
1497 .is_prepared = ds3231_clk_32khz_is_prepared,
1498 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1499};
1500
1501static struct clk_init_data ds3231_clks_init[] = {
1502 [DS3231_CLK_SQW] = {
1503 .name = "ds3231_clk_sqw",
1504 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001505 },
1506 [DS3231_CLK_32KHZ] = {
1507 .name = "ds3231_clk_32khz",
1508 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001509 },
1510};
1511
1512static int ds3231_clks_register(struct ds1307 *ds1307)
1513{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001514 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001515 struct clk_onecell_data *onecell;
1516 int i;
1517
Heiner Kallweit11e58902017-03-10 18:52:34 +01001518 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001519 if (!onecell)
1520 return -ENOMEM;
1521
1522 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001523 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1524 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001525 if (!onecell->clks)
1526 return -ENOMEM;
1527
1528 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1529 struct clk_init_data init = ds3231_clks_init[i];
1530
1531 /*
1532 * Interrupt signal due to alarm conditions and square-wave
1533 * output share same pin, so don't initialize both.
1534 */
1535 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1536 continue;
1537
1538 /* optional override of the clockname */
1539 of_property_read_string_index(node, "clock-output-names", i,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001540 &init.name);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001541 ds1307->clks[i].init = &init;
1542
Heiner Kallweit11e58902017-03-10 18:52:34 +01001543 onecell->clks[i] = devm_clk_register(ds1307->dev,
1544 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001545 if (IS_ERR(onecell->clks[i]))
1546 return PTR_ERR(onecell->clks[i]);
1547 }
1548
1549 if (!node)
1550 return 0;
1551
1552 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1553
1554 return 0;
1555}
1556
1557static void ds1307_clks_register(struct ds1307 *ds1307)
1558{
1559 int ret;
1560
1561 if (ds1307->type != ds_3231)
1562 return;
1563
1564 ret = ds3231_clks_register(ds1307);
1565 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001566 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1567 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001568 }
1569}
1570
1571#else
1572
1573static void ds1307_clks_register(struct ds1307 *ds1307)
1574{
1575}
1576
1577#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001578
Heiner Kallweit11e58902017-03-10 18:52:34 +01001579static const struct regmap_config regmap_config = {
1580 .reg_bits = 8,
1581 .val_bits = 8,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001582};
1583
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001584static int ds1307_probe(struct i2c_client *client,
1585 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001586{
1587 struct ds1307 *ds1307;
1588 int err = -ENODEV;
Heiner Kallweit584ce302017-08-29 21:52:56 +02001589 int tmp;
Heiner Kallweit7624df42017-07-12 07:49:33 +02001590 const struct chip_desc *chip;
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001591 bool want_irq;
Michael Lange8bc2a402016-01-21 18:10:16 +01001592 bool ds1307_can_wakeup_device = false;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001593 unsigned char regs[8];
Jingoo Han01ce8932013-11-12 15:10:41 -08001594 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001595 u8 trickle_charger_setup = 0;
David Brownell1abb0dc2006-06-25 05:48:17 -07001596
Jingoo Hanedca66d2013-07-03 15:07:05 -07001597 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001598 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001599 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001600
Heiner Kallweit11e58902017-03-10 18:52:34 +01001601 dev_set_drvdata(&client->dev, ds1307);
1602 ds1307->dev = &client->dev;
1603 ds1307->name = client->name;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001604
Heiner Kallweit11e58902017-03-10 18:52:34 +01001605 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1606 if (IS_ERR(ds1307->regmap)) {
1607 dev_err(ds1307->dev, "regmap allocation failed\n");
1608 return PTR_ERR(ds1307->regmap);
1609 }
1610
1611 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001612
1613 if (client->dev.of_node) {
1614 ds1307->type = (enum ds_type)
1615 of_device_get_match_data(&client->dev);
1616 chip = &chips[ds1307->type];
1617 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001618 chip = &chips[id->driver_data];
1619 ds1307->type = id->driver_data;
1620 } else {
1621 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001622
Tin Huynh9c19b892016-11-30 09:57:31 +07001623 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001624 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001625 if (!acpi_id)
1626 return -ENODEV;
1627 chip = &chips[acpi_id->driver_data];
1628 ds1307->type = acpi_id->driver_data;
1629 }
1630
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001631 want_irq = client->irq > 0 && chip->alarm;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001632
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001633 if (!pdata)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001634 trickle_charger_setup = ds1307_trickle_init(ds1307, chip);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001635 else if (pdata->trickle_charger_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001636 trickle_charger_setup = pdata->trickle_charger_setup;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001637
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001638 if (trickle_charger_setup && chip->trickle_charger_reg) {
1639 trickle_charger_setup |= DS13XX_TRICKLE_CHARGER_MAGIC;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001640 dev_dbg(ds1307->dev,
1641 "writing trickle charger info 0x%x to 0x%x\n",
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001642 trickle_charger_setup, chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001643 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001644 trickle_charger_setup);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001645 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001646
Michael Lange8bc2a402016-01-21 18:10:16 +01001647#ifdef CONFIG_OF
1648/*
1649 * For devices with no IRQ directly connected to the SoC, the RTC chip
1650 * can be forced as a wakeup source by stating that explicitly in
1651 * the device's .dts file using the "wakeup-source" boolean property.
1652 * If the "wakeup-source" property is set, don't request an IRQ.
1653 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1654 * if supported by the RTC.
1655 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001656 if (chip->alarm && of_property_read_bool(client->dev.of_node,
1657 "wakeup-source"))
Michael Lange8bc2a402016-01-21 18:10:16 +01001658 ds1307_can_wakeup_device = true;
Michael Lange8bc2a402016-01-21 18:10:16 +01001659#endif
1660
David Brownell045e0e82007-07-17 04:04:55 -07001661 switch (ds1307->type) {
1662 case ds_1337:
1663 case ds_1339:
Nikita Yushchenko0759c882017-08-24 09:32:11 +03001664 case ds_1341:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001665 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001666 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001667 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001668 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001669 if (err) {
1670 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001671 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001672 }
1673
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001674 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001675 if (regs[0] & DS1337_BIT_nEOSC)
1676 regs[0] &= ~DS1337_BIT_nEOSC;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001677
David Anders40ce9722012-03-23 15:02:37 -07001678 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001679 * Using IRQ or defined as wakeup-source?
1680 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001681 * For some variants, be sure alarms can trigger when we're
1682 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001683 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001684 if (want_irq || ds1307_can_wakeup_device) {
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001685 regs[0] |= DS1337_BIT_INTCN | chip->bbsqi_bit;
1686 regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001687 }
1688
Heiner Kallweit11e58902017-03-10 18:52:34 +01001689 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001690 regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001691
1692 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001693 if (regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001694 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001695 regs[1] & ~DS1337_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001696 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001697 }
David Brownell045e0e82007-07-17 04:04:55 -07001698 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001699
1700 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001701 err = regmap_bulk_read(ds1307->regmap,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001702 RX8025_REG_CTRL1 << 4 | 0x08, regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001703 if (err) {
1704 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001705 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001706 }
1707
1708 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001709 if (!(regs[1] & RX8025_BIT_XST)) {
1710 regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001711 regmap_write(ds1307->regmap,
1712 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001713 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001714 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001715 "oscillator stop detected - SET TIME!\n");
1716 }
1717
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001718 if (regs[1] & RX8025_BIT_PON) {
1719 regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001720 regmap_write(ds1307->regmap,
1721 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001722 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001723 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001724 }
1725
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001726 if (regs[1] & RX8025_BIT_VDET) {
1727 regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001728 regmap_write(ds1307->regmap,
1729 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001730 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001731 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001732 }
1733
1734 /* make sure we are running in 24hour mode */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001735 if (!(regs[0] & RX8025_BIT_2412)) {
Matthias Fuchsa2166852009-03-31 15:24:58 -07001736 u8 hour;
1737
1738 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001739 regmap_write(ds1307->regmap,
1740 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001741 regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001742
Heiner Kallweit11e58902017-03-10 18:52:34 +01001743 err = regmap_bulk_read(ds1307->regmap,
1744 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001745 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001746 if (err) {
1747 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001748 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001749 }
1750
1751 /* correct hour */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001752 hour = bcd2bin(regs[DS1307_REG_HOUR]);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001753 if (hour == 12)
1754 hour = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001755 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
Matthias Fuchsa2166852009-03-31 15:24:58 -07001756 hour += 12;
1757
Heiner Kallweit11e58902017-03-10 18:52:34 +01001758 regmap_write(ds1307->regmap,
1759 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001760 }
1761 break;
David Brownell045e0e82007-07-17 04:04:55 -07001762 default:
1763 break;
1764 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001765
David Brownell1abb0dc2006-06-25 05:48:17 -07001766 /* read RTC registers */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001767 err = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
1768 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +01001769 if (err) {
1770 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001771 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001772 }
1773
Alexandre Bellonib3a50162019-04-11 00:16:29 +02001774 if (ds1307->type == mcp794xx &&
1775 !(regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
1776 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
1777 regs[DS1307_REG_WDAY] |
1778 MCP794XX_BIT_VBATEN);
David Brownell1abb0dc2006-06-25 05:48:17 -07001779 }
David Brownell045e0e82007-07-17 04:04:55 -07001780
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001781 tmp = regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001782 switch (ds1307->type) {
1783 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001784 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001785 case m41t00:
Giulio Benetti7e580762018-05-16 23:08:40 +02001786 case m41t11:
David Anders40ce9722012-03-23 15:02:37 -07001787 /*
1788 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001789 * systems that will run through year 2100.
1790 */
1791 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001792 case rx_8025:
1793 break;
David Brownellc065f352007-07-17 04:05:10 -07001794 default:
1795 if (!(tmp & DS1307_BIT_12HR))
1796 break;
1797
David Anders40ce9722012-03-23 15:02:37 -07001798 /*
1799 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001800 * take note...
1801 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001802 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001803 if (tmp == 12)
1804 tmp = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001805 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
David Brownellc065f352007-07-17 04:05:10 -07001806 tmp += 12;
Heiner Kallweite5531702017-07-12 07:49:47 +02001807 regmap_write(ds1307->regmap, chip->offset + DS1307_REG_HOUR,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001808 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001809 }
1810
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001811 if (want_irq || ds1307_can_wakeup_device) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001812 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001813 set_bit(HAS_ALARM, &ds1307->flags);
1814 }
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001815
1816 ds1307->rtc = devm_rtc_allocate_device(ds1307->dev);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001817 if (IS_ERR(ds1307->rtc))
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001818 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001819
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001820 if (ds1307_can_wakeup_device && !want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001821 dev_info(ds1307->dev,
1822 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001823 /* We cannot support UIE mode if we do not have an IRQ line */
1824 ds1307->rtc->uie_unsupported = 1;
1825 }
1826
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001827 if (want_irq) {
Heiner Kallweit45947122017-07-12 07:49:41 +02001828 err = devm_request_threaded_irq(ds1307->dev, client->irq, NULL,
1829 chip->irq_handler ?: ds1307_irq,
Nishanth Menonc5983192015-06-23 11:15:11 -05001830 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001831 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001832 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001833 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001834 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001835 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001836 dev_err(ds1307->dev, "unable to request IRQ!\n");
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001837 } else {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001838 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001839 }
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001840 }
1841
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001842 ds1307->rtc->ops = chip->rtc_ops ?: &ds13xx_rtc_ops;
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001843 err = ds1307_add_frequency_test(ds1307);
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001844 if (err)
1845 return err;
1846
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001847 err = rtc_register_device(ds1307->rtc);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001848 if (err)
1849 return err;
1850
Austin Boyle9eab0a72012-03-23 15:02:38 -07001851 if (chip->nvram_size) {
Alexandre Belloni409baf12018-02-12 23:47:23 +01001852 struct nvmem_config nvmem_cfg = {
1853 .name = "ds1307_nvram",
1854 .word_size = 1,
1855 .stride = 1,
1856 .size = chip->nvram_size,
1857 .reg_read = ds1307_nvram_read,
1858 .reg_write = ds1307_nvram_write,
1859 .priv = ds1307,
1860 };
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001861
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001862 ds1307->rtc->nvram_old_abi = true;
Alexandre Belloni409baf12018-02-12 23:47:23 +01001863 rtc_nvmem_register(ds1307->rtc, &nvmem_cfg);
David Brownell682d73f2007-11-14 16:58:32 -08001864 }
1865
Akinobu Mita445c0202016-01-25 00:22:16 +09001866 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001867 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001868
David Brownell1abb0dc2006-06-25 05:48:17 -07001869 return 0;
1870
Jingoo Hanedca66d2013-07-03 15:07:05 -07001871exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001872 return err;
1873}
1874
David Brownell1abb0dc2006-06-25 05:48:17 -07001875static struct i2c_driver ds1307_driver = {
1876 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001877 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001878 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001879 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001880 },
David Brownellc065f352007-07-17 04:05:10 -07001881 .probe = ds1307_probe,
Jean Delvare3760f732008-04-29 23:11:40 +02001882 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001883};
1884
Axel Lin0abc9202012-03-23 15:02:31 -07001885module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001886
1887MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1888MODULE_LICENSE("GPL");