blob: 3cbb7636b405c2abd34046736934b24f7f5a12fd [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,
37 ds_1337,
38 ds_1338,
39 ds_1339,
40 ds_1340,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -070041 ds_1388,
Wolfram Sang97f902b2009-06-17 16:26:10 -070042 ds_3231,
Stefan Agner8566f702017-03-23 16:54:57 -070043 m41t0,
David Brownell045e0e82007-07-17 04:04:55 -070044 m41t00,
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
53/* RTC registers don't differ much, except for the century flag */
54#define DS1307_REG_SECS 0x00 /* 00-59 */
55# define DS1307_BIT_CH 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070056# define DS1340_BIT_nEOSC 0x80
Tomas Novotnyf4199f82014-12-10 15:53:57 -080057# define MCP794XX_BIT_ST 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070058#define DS1307_REG_MIN 0x01 /* 00-59 */
Stefan Agner8566f702017-03-23 16:54:57 -070059# define M41T0_BIT_OF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070060#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
David Brownellc065f352007-07-17 04:05:10 -070061# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
62# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
David Brownell1abb0dc2006-06-25 05:48:17 -070063# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
64# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
65#define DS1307_REG_WDAY 0x03 /* 01-07 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -080066# define MCP794XX_BIT_VBATEN 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -070067#define DS1307_REG_MDAY 0x04 /* 01-31 */
68#define DS1307_REG_MONTH 0x05 /* 01-12 */
69# define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
70#define DS1307_REG_YEAR 0x06 /* 00-99 */
71
David Anders40ce9722012-03-23 15:02:37 -070072/*
73 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
David Brownell045e0e82007-07-17 04:04:55 -070074 * start at 7, and they differ a LOT. Only control and status matter for
75 * basic RTC date and time functionality; be careful using them.
David Brownell1abb0dc2006-06-25 05:48:17 -070076 */
David Brownell045e0e82007-07-17 04:04:55 -070077#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
David Brownell1abb0dc2006-06-25 05:48:17 -070078# define DS1307_BIT_OUT 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070079# define DS1338_BIT_OSF 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070080# define DS1307_BIT_SQWE 0x10
81# define DS1307_BIT_RS1 0x02
82# define DS1307_BIT_RS0 0x01
83#define DS1337_REG_CONTROL 0x0e
84# define DS1337_BIT_nEOSC 0x80
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070085# define DS1339_BIT_BBSQI 0x20
Wolfram Sang97f902b2009-06-17 16:26:10 -070086# define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
David Brownell1abb0dc2006-06-25 05:48:17 -070087# define DS1337_BIT_RS2 0x10
88# define DS1337_BIT_RS1 0x08
89# define DS1337_BIT_INTCN 0x04
90# define DS1337_BIT_A2IE 0x02
91# define DS1337_BIT_A1IE 0x01
David Brownell045e0e82007-07-17 04:04:55 -070092#define DS1340_REG_CONTROL 0x07
93# define DS1340_BIT_OUT 0x80
94# define DS1340_BIT_FT 0x40
95# define DS1340_BIT_CALIB_SIGN 0x20
96# define DS1340_M_CALIBRATION 0x1f
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070097#define DS1340_REG_FLAG 0x09
98# define DS1340_BIT_OSF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070099#define DS1337_REG_STATUS 0x0f
100# define DS1337_BIT_OSF 0x80
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900101# define DS3231_BIT_EN32KHZ 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -0700102# define DS1337_BIT_A2I 0x02
103# define DS1337_BIT_A1I 0x01
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700104#define DS1339_REG_ALARM1_SECS 0x07
Wolfram Sangeb86c302012-05-29 15:07:38 -0700105
106#define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
David Brownell1abb0dc2006-06-25 05:48:17 -0700107
Matthias Fuchsa2166852009-03-31 15:24:58 -0700108#define RX8025_REG_CTRL1 0x0e
109# define RX8025_BIT_2412 0x20
110#define RX8025_REG_CTRL2 0x0f
111# define RX8025_BIT_PON 0x10
112# define RX8025_BIT_VDET 0x40
113# define RX8025_BIT_XST 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -0700114
115
116struct ds1307 {
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700117 u8 offset; /* register's offset */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700118 u8 regs[11];
Austin Boyle9eab0a72012-03-23 15:02:38 -0700119 u16 nvram_offset;
120 struct bin_attribute *nvram;
David Brownell1abb0dc2006-06-25 05:48:17 -0700121 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700122 unsigned long flags;
123#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
124#define HAS_ALARM 1 /* bit 1 == irq claimed */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100125 struct device *dev;
126 struct regmap *regmap;
127 const char *name;
128 int irq;
David Brownell1abb0dc2006-06-25 05:48:17 -0700129 struct rtc_device *rtc;
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900130#ifdef CONFIG_COMMON_CLK
131 struct clk_hw clks[2];
132#endif
David Brownell1abb0dc2006-06-25 05:48:17 -0700133};
134
David Brownell045e0e82007-07-17 04:04:55 -0700135struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700136 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700137 u16 nvram_offset;
138 u16 nvram_size;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700139 u16 trickle_charger_reg;
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700140 u8 trickle_charger_setup;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100141 u8 (*do_trickle_setup)(struct ds1307 *, uint32_t,
142 bool);
David Brownell045e0e82007-07-17 04:04:55 -0700143};
144
Heiner Kallweit11e58902017-03-10 18:52:34 +0100145static u8 do_trickle_setup_ds1339(struct ds1307 *, uint32_t ohms, bool diode);
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700146
147static struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700148 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700149 .nvram_offset = 8,
150 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700151 },
152 [ds_1337] = {
153 .alarm = 1,
154 },
155 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700156 .nvram_offset = 8,
157 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700158 },
159 [ds_1339] = {
160 .alarm = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700161 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700162 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700163 },
164 [ds_1340] = {
165 .trickle_charger_reg = 0x08,
166 },
167 [ds_1388] = {
168 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700169 },
170 [ds_3231] = {
171 .alarm = 1,
172 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200173 [rx_8130] = {
174 .alarm = 1,
175 /* this is battery backed SRAM */
176 .nvram_offset = 0x20,
177 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
178 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800179 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700180 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700181 /* this is battery backed SRAM */
182 .nvram_offset = 0x20,
183 .nvram_size = 0x40,
184 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700185};
David Brownell045e0e82007-07-17 04:04:55 -0700186
Jean Delvare3760f732008-04-29 23:11:40 +0200187static const struct i2c_device_id ds1307_id[] = {
188 { "ds1307", ds_1307 },
189 { "ds1337", ds_1337 },
190 { "ds1338", ds_1338 },
191 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700192 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200193 { "ds1340", ds_1340 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700194 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700195 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200196 { "m41t00", m41t00 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800197 { "mcp7940x", mcp794xx },
198 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700199 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700200 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200201 { "isl12057", ds_1337 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200202 { "rx8130", rx_8130 },
Jean Delvare3760f732008-04-29 23:11:40 +0200203 { }
204};
205MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700206
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300207#ifdef CONFIG_OF
208static const struct of_device_id ds1307_of_match[] = {
209 {
210 .compatible = "dallas,ds1307",
211 .data = (void *)ds_1307
212 },
213 {
214 .compatible = "dallas,ds1337",
215 .data = (void *)ds_1337
216 },
217 {
218 .compatible = "dallas,ds1338",
219 .data = (void *)ds_1338
220 },
221 {
222 .compatible = "dallas,ds1339",
223 .data = (void *)ds_1339
224 },
225 {
226 .compatible = "dallas,ds1388",
227 .data = (void *)ds_1388
228 },
229 {
230 .compatible = "dallas,ds1340",
231 .data = (void *)ds_1340
232 },
233 {
234 .compatible = "maxim,ds3231",
235 .data = (void *)ds_3231
236 },
237 {
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200238 .compatible = "st,m41t0",
239 .data = (void *)m41t00
240 },
241 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300242 .compatible = "st,m41t00",
243 .data = (void *)m41t00
244 },
245 {
246 .compatible = "microchip,mcp7940x",
247 .data = (void *)mcp794xx
248 },
249 {
250 .compatible = "microchip,mcp7941x",
251 .data = (void *)mcp794xx
252 },
253 {
254 .compatible = "pericom,pt7c4338",
255 .data = (void *)ds_1307
256 },
257 {
258 .compatible = "epson,rx8025",
259 .data = (void *)rx_8025
260 },
261 {
262 .compatible = "isil,isl12057",
263 .data = (void *)ds_1337
264 },
265 { }
266};
267MODULE_DEVICE_TABLE(of, ds1307_of_match);
268#endif
269
Tin Huynh9c19b892016-11-30 09:57:31 +0700270#ifdef CONFIG_ACPI
271static const struct acpi_device_id ds1307_acpi_ids[] = {
272 { .id = "DS1307", .driver_data = ds_1307 },
273 { .id = "DS1337", .driver_data = ds_1337 },
274 { .id = "DS1338", .driver_data = ds_1338 },
275 { .id = "DS1339", .driver_data = ds_1339 },
276 { .id = "DS1388", .driver_data = ds_1388 },
277 { .id = "DS1340", .driver_data = ds_1340 },
278 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700279 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700280 { .id = "M41T00", .driver_data = m41t00 },
281 { .id = "MCP7940X", .driver_data = mcp794xx },
282 { .id = "MCP7941X", .driver_data = mcp794xx },
283 { .id = "PT7C4338", .driver_data = ds_1307 },
284 { .id = "RX8025", .driver_data = rx_8025 },
285 { .id = "ISL12057", .driver_data = ds_1337 },
286 { }
287};
288MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
289#endif
290
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700291/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700292 * The ds1337 and ds1339 both have two alarms, but we only use the first
293 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
294 * signal; ds1339 chips have only one alarm signal.
295 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500296static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700297{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100298 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500299 struct mutex *lock = &ds1307->rtc->ops_lock;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100300 int stat, control, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700301
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700302 mutex_lock(lock);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100303 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
304 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700305 goto out;
306
307 if (stat & DS1337_BIT_A1I) {
308 stat &= ~DS1337_BIT_A1I;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100309 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700310
Heiner Kallweit11e58902017-03-10 18:52:34 +0100311 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
312 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700313 goto out;
314
315 control &= ~DS1337_BIT_A1IE;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100316 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, control);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700317
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700318 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700319 }
320
321out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700322 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700323
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700324 return IRQ_HANDLED;
325}
326
327/*----------------------------------------------------------------------*/
328
David Brownell1abb0dc2006-06-25 05:48:17 -0700329static int ds1307_get_time(struct device *dev, struct rtc_time *t)
330{
331 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100332 int tmp, ret;
David Brownell1abb0dc2006-06-25 05:48:17 -0700333
David Brownell045e0e82007-07-17 04:04:55 -0700334 /* read the RTC date and time registers all at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100335 ret = regmap_bulk_read(ds1307->regmap, ds1307->offset, ds1307->regs, 7);
336 if (ret) {
337 dev_err(dev, "%s error %d\n", "read", ret);
338 return ret;
David Brownell1abb0dc2006-06-25 05:48:17 -0700339 }
340
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800341 dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700342
Stefan Agner8566f702017-03-23 16:54:57 -0700343 /* if oscillator fail bit is set, no data can be trusted */
344 if (ds1307->type == m41t0 &&
345 ds1307->regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
346 dev_warn_once(dev, "oscillator failed, set time!\n");
347 return -EINVAL;
348 }
349
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700350 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
351 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700352 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700353 t->tm_hour = bcd2bin(tmp);
354 t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
355 t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700356 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700357 t->tm_mon = bcd2bin(tmp) - 1;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700358 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700359
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200360#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
361 switch (ds1307->type) {
362 case ds_1337:
363 case ds_1339:
364 case ds_3231:
365 if (ds1307->regs[DS1307_REG_MONTH] & DS1337_BIT_CENTURY)
366 t->tm_year += 100;
367 break;
368 case ds_1340:
369 if (ds1307->regs[DS1307_REG_HOUR] & DS1340_BIT_CENTURY)
370 t->tm_year += 100;
371 break;
372 default:
373 break;
374 }
375#endif
376
David Brownell1abb0dc2006-06-25 05:48:17 -0700377 dev_dbg(dev, "%s secs=%d, mins=%d, "
378 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
379 "read", t->tm_sec, t->tm_min,
380 t->tm_hour, t->tm_mday,
381 t->tm_mon, t->tm_year, t->tm_wday);
382
David Brownell045e0e82007-07-17 04:04:55 -0700383 /* initial clock setting can be undefined */
384 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700385}
386
387static int ds1307_set_time(struct device *dev, struct rtc_time *t)
388{
389 struct ds1307 *ds1307 = dev_get_drvdata(dev);
390 int result;
391 int tmp;
392 u8 *buf = ds1307->regs;
393
394 dev_dbg(dev, "%s secs=%d, mins=%d, "
395 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400396 "write", t->tm_sec, t->tm_min,
397 t->tm_hour, t->tm_mday,
398 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700399
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200400#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
401 if (t->tm_year < 100)
402 return -EINVAL;
403
404 switch (ds1307->type) {
405 case ds_1337:
406 case ds_1339:
407 case ds_3231:
408 case ds_1340:
409 if (t->tm_year > 299)
410 return -EINVAL;
411 default:
412 if (t->tm_year > 199)
413 return -EINVAL;
414 break;
415 }
416#else
417 if (t->tm_year < 100 || t->tm_year > 199)
418 return -EINVAL;
419#endif
420
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700421 buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
422 buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
423 buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
424 buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
425 buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
426 buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700427
428 /* assume 20YY not 19YY */
429 tmp = t->tm_year - 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700430 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700431
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700432 switch (ds1307->type) {
433 case ds_1337:
434 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -0700435 case ds_3231:
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200436 if (t->tm_year > 199)
437 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700438 break;
439 case ds_1340:
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200440 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN;
441 if (t->tm_year > 199)
442 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700443 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800444 case mcp794xx:
David Anders40ce9722012-03-23 15:02:37 -0700445 /*
446 * these bits were cleared when preparing the date/time
447 * values and need to be set again before writing the
448 * buffer out to the device.
449 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800450 buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
451 buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
David Anders43fcb812011-11-02 13:37:53 -0700452 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700453 default:
454 break;
455 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700456
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800457 dev_dbg(dev, "%s: %7ph\n", "write", buf);
David Brownell1abb0dc2006-06-25 05:48:17 -0700458
Heiner Kallweit11e58902017-03-10 18:52:34 +0100459 result = regmap_bulk_write(ds1307->regmap, ds1307->offset, buf, 7);
460 if (result) {
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800461 dev_err(dev, "%s error %d\n", "write", result);
462 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700463 }
464 return 0;
465}
466
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800467static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700468{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100469 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700470 int ret;
471
472 if (!test_bit(HAS_ALARM, &ds1307->flags))
473 return -EINVAL;
474
475 /* read all ALARM1, ALARM2, and status registers at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100476 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
477 ds1307->regs, 9);
478 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700479 dev_err(dev, "%s error %d\n", "alarm read", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100480 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700481 }
482
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100483 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
484 &ds1307->regs[0], &ds1307->regs[4], &ds1307->regs[7]);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700485
David Anders40ce9722012-03-23 15:02:37 -0700486 /*
487 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700488 * and that all four fields are checked matches
489 */
490 t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
491 t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
492 t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
493 t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700494
495 /* ... and status */
496 t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
497 t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
498
499 dev_dbg(dev, "%s secs=%d, mins=%d, "
500 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
501 "alarm read", t->time.tm_sec, t->time.tm_min,
502 t->time.tm_hour, t->time.tm_mday,
503 t->enabled, t->pending);
504
505 return 0;
506}
507
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800508static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700509{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100510 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700511 unsigned char *buf = ds1307->regs;
512 u8 control, status;
513 int ret;
514
515 if (!test_bit(HAS_ALARM, &ds1307->flags))
516 return -EINVAL;
517
518 dev_dbg(dev, "%s secs=%d, mins=%d, "
519 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
520 "alarm set", t->time.tm_sec, t->time.tm_min,
521 t->time.tm_hour, t->time.tm_mday,
522 t->enabled, t->pending);
523
524 /* read current status of both alarms and the chip */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100525 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, buf, 9);
526 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700527 dev_err(dev, "%s error %d\n", "alarm write", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100528 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700529 }
530 control = ds1307->regs[7];
531 status = ds1307->regs[8];
532
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100533 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
534 &ds1307->regs[0], &ds1307->regs[4], control, status);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700535
536 /* set ALARM1, using 24 hour and day-of-month modes */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700537 buf[0] = bin2bcd(t->time.tm_sec);
538 buf[1] = bin2bcd(t->time.tm_min);
539 buf[2] = bin2bcd(t->time.tm_hour);
540 buf[3] = bin2bcd(t->time.tm_mday);
541
542 /* set ALARM2 to non-garbage */
543 buf[4] = 0;
544 buf[5] = 0;
545 buf[6] = 0;
546
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200547 /* disable alarms */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700548 buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700549 buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
550
Heiner Kallweit11e58902017-03-10 18:52:34 +0100551 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, buf, 9);
552 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700553 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800554 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700555 }
556
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200557 /* optionally enable ALARM1 */
558 if (t->enabled) {
559 dev_dbg(dev, "alarm IRQ armed\n");
560 buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100561 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, buf[7]);
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200562 }
563
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700564 return 0;
565}
566
John Stultz16380c12011-02-02 17:02:41 -0800567static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700568{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100569 struct ds1307 *ds1307 = dev_get_drvdata(dev);
570 int control, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700571
John Stultz16380c12011-02-02 17:02:41 -0800572 if (!test_bit(HAS_ALARM, &ds1307->flags))
573 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700574
Heiner Kallweit11e58902017-03-10 18:52:34 +0100575 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
576 if (ret)
John Stultz16380c12011-02-02 17:02:41 -0800577 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700578
John Stultz16380c12011-02-02 17:02:41 -0800579 if (enabled)
Heiner Kallweit11e58902017-03-10 18:52:34 +0100580 control |= DS1337_BIT_A1IE;
John Stultz16380c12011-02-02 17:02:41 -0800581 else
Heiner Kallweit11e58902017-03-10 18:52:34 +0100582 control &= ~DS1337_BIT_A1IE;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700583
Heiner Kallweit11e58902017-03-10 18:52:34 +0100584 return regmap_write(ds1307->regmap, DS1337_REG_CONTROL, control);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700585}
586
David Brownellff8371a2006-09-30 23:28:17 -0700587static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700588 .read_time = ds1307_get_time,
589 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800590 .read_alarm = ds1337_read_alarm,
591 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800592 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700593};
594
David Brownell682d73f2007-11-14 16:58:32 -0800595/*----------------------------------------------------------------------*/
596
Simon Guinot1d1945d2014-04-03 14:49:55 -0700597/*
Marek Vasutee0981b2017-06-18 22:55:28 +0200598 * Alarm support for rx8130 devices.
599 */
600
601#define RX8130_REG_ALARM_MIN 0x07
602#define RX8130_REG_ALARM_HOUR 0x08
603#define RX8130_REG_ALARM_WEEK_OR_DAY 0x09
604#define RX8130_REG_EXTENSION 0x0c
605#define RX8130_REG_EXTENSION_WADA (1 << 3)
606#define RX8130_REG_FLAG 0x0d
607#define RX8130_REG_FLAG_AF (1 << 3)
608#define RX8130_REG_CONTROL0 0x0e
609#define RX8130_REG_CONTROL0_AIE (1 << 3)
610
611static irqreturn_t rx8130_irq(int irq, void *dev_id)
612{
613 struct ds1307 *ds1307 = dev_id;
614 struct mutex *lock = &ds1307->rtc->ops_lock;
615 u8 ctl[3];
616 int ret;
617
618 mutex_lock(lock);
619
620 /* Read control registers. */
621 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
622 if (ret < 0)
623 goto out;
624 if (!(ctl[1] & RX8130_REG_FLAG_AF))
625 goto out;
626 ctl[1] &= ~RX8130_REG_FLAG_AF;
627 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
628
629 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
630 if (ret < 0)
631 goto out;
632
633 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
634
635out:
636 mutex_unlock(lock);
637
638 return IRQ_HANDLED;
639}
640
641static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
642{
643 struct ds1307 *ds1307 = dev_get_drvdata(dev);
644 u8 ald[3], ctl[3];
645 int ret;
646
647 if (!test_bit(HAS_ALARM, &ds1307->flags))
648 return -EINVAL;
649
650 /* Read alarm registers. */
651 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, 3);
652 if (ret < 0)
653 return ret;
654
655 /* Read control registers. */
656 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
657 if (ret < 0)
658 return ret;
659
660 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
661 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
662
663 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
664 t->time.tm_sec = -1;
665 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
666 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
667 t->time.tm_wday = -1;
668 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
669 t->time.tm_mon = -1;
670 t->time.tm_year = -1;
671 t->time.tm_yday = -1;
672 t->time.tm_isdst = -1;
673
674 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
675 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
676 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
677
678 return 0;
679}
680
681static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
682{
683 struct ds1307 *ds1307 = dev_get_drvdata(dev);
684 u8 ald[3], ctl[3];
685 int ret;
686
687 if (!test_bit(HAS_ALARM, &ds1307->flags))
688 return -EINVAL;
689
690 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
691 "enabled=%d pending=%d\n", __func__,
692 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
693 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
694 t->enabled, t->pending);
695
696 /* Read control registers. */
697 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
698 if (ret < 0)
699 return ret;
700
701 ctl[0] &= ~RX8130_REG_EXTENSION_WADA;
702 ctl[1] |= RX8130_REG_FLAG_AF;
703 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
704
705 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
706 if (ret < 0)
707 return ret;
708
709 /* Hardware alarm precision is 1 minute! */
710 ald[0] = bin2bcd(t->time.tm_min);
711 ald[1] = bin2bcd(t->time.tm_hour);
712 ald[2] = bin2bcd(t->time.tm_mday);
713
714 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, 3);
715 if (ret < 0)
716 return ret;
717
718 if (!t->enabled)
719 return 0;
720
721 ctl[2] |= RX8130_REG_CONTROL0_AIE;
722
723 return regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
724}
725
726static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
727{
728 struct ds1307 *ds1307 = dev_get_drvdata(dev);
729 int ret, reg;
730
731 if (!test_bit(HAS_ALARM, &ds1307->flags))
732 return -EINVAL;
733
734 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
735 if (ret < 0)
736 return ret;
737
738 if (enabled)
739 reg |= RX8130_REG_CONTROL0_AIE;
740 else
741 reg &= ~RX8130_REG_CONTROL0_AIE;
742
743 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
744}
745
746static const struct rtc_class_ops rx8130_rtc_ops = {
747 .read_time = ds1307_get_time,
748 .set_time = ds1307_set_time,
749 .read_alarm = rx8130_read_alarm,
750 .set_alarm = rx8130_set_alarm,
751 .alarm_irq_enable = rx8130_alarm_irq_enable,
752};
753
754/*----------------------------------------------------------------------*/
755
756/*
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800757 * Alarm support for mcp794xx devices.
Simon Guinot1d1945d2014-04-03 14:49:55 -0700758 */
759
Keerthye29385f2016-06-01 16:19:07 +0530760#define MCP794XX_REG_WEEKDAY 0x3
761#define MCP794XX_REG_WEEKDAY_WDAY_MASK 0x7
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800762#define MCP794XX_REG_CONTROL 0x07
763# define MCP794XX_BIT_ALM0_EN 0x10
764# define MCP794XX_BIT_ALM1_EN 0x20
765#define MCP794XX_REG_ALARM0_BASE 0x0a
766#define MCP794XX_REG_ALARM0_CTRL 0x0d
767#define MCP794XX_REG_ALARM1_BASE 0x11
768#define MCP794XX_REG_ALARM1_CTRL 0x14
769# define MCP794XX_BIT_ALMX_IF (1 << 3)
770# define MCP794XX_BIT_ALMX_C0 (1 << 4)
771# define MCP794XX_BIT_ALMX_C1 (1 << 5)
772# define MCP794XX_BIT_ALMX_C2 (1 << 6)
773# define MCP794XX_BIT_ALMX_POL (1 << 7)
774# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
775 MCP794XX_BIT_ALMX_C1 | \
776 MCP794XX_BIT_ALMX_C2)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700777
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500778static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700779{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100780 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500781 struct mutex *lock = &ds1307->rtc->ops_lock;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700782 int reg, ret;
783
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500784 mutex_lock(lock);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700785
786 /* Check and clear alarm 0 interrupt flag. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100787 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
788 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700789 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800790 if (!(reg & MCP794XX_BIT_ALMX_IF))
Simon Guinot1d1945d2014-04-03 14:49:55 -0700791 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800792 reg &= ~MCP794XX_BIT_ALMX_IF;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100793 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
794 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700795 goto out;
796
797 /* Disable alarm 0. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100798 ret = regmap_read(ds1307->regmap, MCP794XX_REG_CONTROL, &reg);
799 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700800 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800801 reg &= ~MCP794XX_BIT_ALM0_EN;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100802 ret = regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, reg);
803 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700804 goto out;
805
806 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
807
808out:
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500809 mutex_unlock(lock);
810
811 return IRQ_HANDLED;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700812}
813
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800814static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700815{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100816 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700817 u8 *regs = ds1307->regs;
818 int ret;
819
820 if (!test_bit(HAS_ALARM, &ds1307->flags))
821 return -EINVAL;
822
823 /* Read control and alarm 0 registers. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100824 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
825 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700826 return ret;
827
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800828 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700829
830 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
831 t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f);
832 t->time.tm_min = bcd2bin(ds1307->regs[4] & 0x7f);
833 t->time.tm_hour = bcd2bin(ds1307->regs[5] & 0x3f);
834 t->time.tm_wday = bcd2bin(ds1307->regs[6] & 0x7) - 1;
835 t->time.tm_mday = bcd2bin(ds1307->regs[7] & 0x3f);
836 t->time.tm_mon = bcd2bin(ds1307->regs[8] & 0x1f) - 1;
837 t->time.tm_year = -1;
838 t->time.tm_yday = -1;
839 t->time.tm_isdst = -1;
840
841 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
842 "enabled=%d polarity=%d irq=%d match=%d\n", __func__,
843 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
844 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800845 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL),
846 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF),
847 (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700848
849 return 0;
850}
851
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800852static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700853{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100854 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700855 unsigned char *regs = ds1307->regs;
856 int ret;
857
858 if (!test_bit(HAS_ALARM, &ds1307->flags))
859 return -EINVAL;
860
861 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
862 "enabled=%d pending=%d\n", __func__,
863 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
864 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
865 t->enabled, t->pending);
866
867 /* Read control and alarm 0 registers. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100868 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
869 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700870 return ret;
871
872 /* Set alarm 0, using 24-hour and day-of-month modes. */
873 regs[3] = bin2bcd(t->time.tm_sec);
874 regs[4] = bin2bcd(t->time.tm_min);
875 regs[5] = bin2bcd(t->time.tm_hour);
Tero Kristo62c8c202015-10-23 09:29:57 +0300876 regs[6] = bin2bcd(t->time.tm_wday + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700877 regs[7] = bin2bcd(t->time.tm_mday);
Tero Kristo62c8c202015-10-23 09:29:57 +0300878 regs[8] = bin2bcd(t->time.tm_mon + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700879
880 /* Clear the alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800881 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700882 /* Set alarm match: second, minute, hour, day, date, month. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800883 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
Nishanth Menone3edd672015-04-20 19:51:34 -0500884 /* Disable interrupt. We will not enable until completely programmed */
885 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700886
Heiner Kallweit11e58902017-03-10 18:52:34 +0100887 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
888 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700889 return ret;
890
Nishanth Menone3edd672015-04-20 19:51:34 -0500891 if (!t->enabled)
892 return 0;
893 regs[0] |= MCP794XX_BIT_ALM0_EN;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100894 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700895}
896
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800897static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700898{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100899 struct ds1307 *ds1307 = dev_get_drvdata(dev);
900 int reg, ret;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700901
902 if (!test_bit(HAS_ALARM, &ds1307->flags))
903 return -EINVAL;
904
Heiner Kallweit11e58902017-03-10 18:52:34 +0100905 ret = regmap_read(ds1307->regmap, MCP794XX_REG_CONTROL, &reg);
906 if (ret)
907 return ret;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700908
909 if (enabled)
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800910 reg |= MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700911 else
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800912 reg &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700913
Heiner Kallweit11e58902017-03-10 18:52:34 +0100914 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700915}
916
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800917static const struct rtc_class_ops mcp794xx_rtc_ops = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700918 .read_time = ds1307_get_time,
919 .set_time = ds1307_set_time,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800920 .read_alarm = mcp794xx_read_alarm,
921 .set_alarm = mcp794xx_set_alarm,
922 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
Simon Guinot1d1945d2014-04-03 14:49:55 -0700923};
924
925/*----------------------------------------------------------------------*/
926
David Brownell682d73f2007-11-14 16:58:32 -0800927static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700928ds1307_nvram_read(struct file *filp, struct kobject *kobj,
929 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800930 char *buf, loff_t off, size_t count)
931{
David Brownell682d73f2007-11-14 16:58:32 -0800932 struct ds1307 *ds1307;
David Brownell682d73f2007-11-14 16:58:32 -0800933 int result;
934
Heiner Kallweit11e58902017-03-10 18:52:34 +0100935 ds1307 = dev_get_drvdata(kobj_to_dev(kobj));
David Brownell682d73f2007-11-14 16:58:32 -0800936
Heiner Kallweit11e58902017-03-10 18:52:34 +0100937 result = regmap_bulk_read(ds1307->regmap, ds1307->nvram_offset + off,
938 buf, count);
939 if (result)
940 dev_err(ds1307->dev, "%s error %d\n", "nvram read", result);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800941 return result;
David Brownell682d73f2007-11-14 16:58:32 -0800942}
943
944static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700945ds1307_nvram_write(struct file *filp, struct kobject *kobj,
946 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800947 char *buf, loff_t off, size_t count)
948{
Ed Swierk30e7b032009-03-31 15:24:56 -0700949 struct ds1307 *ds1307;
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800950 int result;
David Brownell682d73f2007-11-14 16:58:32 -0800951
Heiner Kallweit11e58902017-03-10 18:52:34 +0100952 ds1307 = dev_get_drvdata(kobj_to_dev(kobj));
David Brownell682d73f2007-11-14 16:58:32 -0800953
Heiner Kallweit11e58902017-03-10 18:52:34 +0100954 result = regmap_bulk_write(ds1307->regmap, ds1307->nvram_offset + off,
955 buf, count);
956 if (result) {
957 dev_err(ds1307->dev, "%s error %d\n", "nvram write", result);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800958 return result;
959 }
960 return count;
David Brownell682d73f2007-11-14 16:58:32 -0800961}
962
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700963
David Brownell682d73f2007-11-14 16:58:32 -0800964/*----------------------------------------------------------------------*/
965
Heiner Kallweit11e58902017-03-10 18:52:34 +0100966static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700967 uint32_t ohms, bool diode)
968{
969 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
970 DS1307_TRICKLE_CHARGER_NO_DIODE;
971
972 switch (ohms) {
973 case 250:
974 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
975 break;
976 case 2000:
977 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
978 break;
979 case 4000:
980 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
981 break;
982 default:
Heiner Kallweit11e58902017-03-10 18:52:34 +0100983 dev_warn(ds1307->dev,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700984 "Unsupported ohm value %u in dt\n", ohms);
985 return 0;
986 }
987 return setup;
988}
989
Heiner Kallweit11e58902017-03-10 18:52:34 +0100990static void ds1307_trickle_init(struct ds1307 *ds1307,
Tin Huynh9c19b892016-11-30 09:57:31 +0700991 struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700992{
993 uint32_t ohms = 0;
994 bool diode = true;
995
996 if (!chip->do_trickle_setup)
997 goto out;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100998 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
999 &ohms))
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001000 goto out;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001001 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001002 diode = false;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001003 chip->trickle_charger_setup = chip->do_trickle_setup(ds1307,
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001004 ohms, diode);
1005out:
1006 return;
1007}
1008
Akinobu Mita445c0202016-01-25 00:22:16 +09001009/*----------------------------------------------------------------------*/
1010
1011#ifdef CONFIG_RTC_DRV_DS1307_HWMON
1012
1013/*
1014 * Temperature sensor support for ds3231 devices.
1015 */
1016
1017#define DS3231_REG_TEMPERATURE 0x11
1018
1019/*
1020 * A user-initiated temperature conversion is not started by this function,
1021 * so the temperature is updated once every 64 seconds.
1022 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001023static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +09001024{
1025 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1026 u8 temp_buf[2];
1027 s16 temp;
1028 int ret;
1029
Heiner Kallweit11e58902017-03-10 18:52:34 +01001030 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
1031 temp_buf, sizeof(temp_buf));
1032 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +09001033 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +09001034 /*
1035 * Temperature is represented as a 10-bit code with a resolution of
1036 * 0.25 degree celsius and encoded in two's complement format.
1037 */
1038 temp = (temp_buf[0] << 8) | temp_buf[1];
1039 temp >>= 6;
1040 *mC = temp * 250;
1041
1042 return 0;
1043}
1044
1045static ssize_t ds3231_hwmon_show_temp(struct device *dev,
1046 struct device_attribute *attr, char *buf)
1047{
1048 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001049 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001050
1051 ret = ds3231_hwmon_read_temp(dev, &temp);
1052 if (ret)
1053 return ret;
1054
1055 return sprintf(buf, "%d\n", temp);
1056}
1057static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ds3231_hwmon_show_temp,
1058 NULL, 0);
1059
1060static struct attribute *ds3231_hwmon_attrs[] = {
1061 &sensor_dev_attr_temp1_input.dev_attr.attr,
1062 NULL,
1063};
1064ATTRIBUTE_GROUPS(ds3231_hwmon);
1065
1066static void ds1307_hwmon_register(struct ds1307 *ds1307)
1067{
1068 struct device *dev;
1069
1070 if (ds1307->type != ds_3231)
1071 return;
1072
Heiner Kallweit11e58902017-03-10 18:52:34 +01001073 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Akinobu Mita445c0202016-01-25 00:22:16 +09001074 ds1307, ds3231_hwmon_groups);
1075 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001076 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1077 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001078 }
1079}
1080
1081#else
1082
1083static void ds1307_hwmon_register(struct ds1307 *ds1307)
1084{
1085}
1086
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001087#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1088
1089/*----------------------------------------------------------------------*/
1090
1091/*
1092 * Square-wave output support for DS3231
1093 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1094 */
1095#ifdef CONFIG_COMMON_CLK
1096
1097enum {
1098 DS3231_CLK_SQW = 0,
1099 DS3231_CLK_32KHZ,
1100};
1101
1102#define clk_sqw_to_ds1307(clk) \
1103 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1104#define clk_32khz_to_ds1307(clk) \
1105 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1106
1107static int ds3231_clk_sqw_rates[] = {
1108 1,
1109 1024,
1110 4096,
1111 8192,
1112};
1113
1114static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1115{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001116 struct mutex *lock = &ds1307->rtc->ops_lock;
1117 int control;
1118 int ret;
1119
1120 mutex_lock(lock);
1121
Heiner Kallweit11e58902017-03-10 18:52:34 +01001122 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1123 if (ret)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001124 goto out;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001125
1126 control &= ~mask;
1127 control |= value;
1128
Heiner Kallweit11e58902017-03-10 18:52:34 +01001129 ret = regmap_write(ds1307->regmap, DS1337_REG_CONTROL, control);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001130out:
1131 mutex_unlock(lock);
1132
1133 return ret;
1134}
1135
1136static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1137 unsigned long parent_rate)
1138{
1139 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001140 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001141 int rate_sel = 0;
1142
Heiner Kallweit11e58902017-03-10 18:52:34 +01001143 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1144 if (ret)
1145 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001146 if (control & DS1337_BIT_RS1)
1147 rate_sel += 1;
1148 if (control & DS1337_BIT_RS2)
1149 rate_sel += 2;
1150
1151 return ds3231_clk_sqw_rates[rate_sel];
1152}
1153
1154static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
1155 unsigned long *prate)
1156{
1157 int i;
1158
1159 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1160 if (ds3231_clk_sqw_rates[i] <= rate)
1161 return ds3231_clk_sqw_rates[i];
1162 }
1163
1164 return 0;
1165}
1166
1167static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
1168 unsigned long parent_rate)
1169{
1170 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1171 int control = 0;
1172 int rate_sel;
1173
1174 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1175 rate_sel++) {
1176 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1177 break;
1178 }
1179
1180 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1181 return -EINVAL;
1182
1183 if (rate_sel & 1)
1184 control |= DS1337_BIT_RS1;
1185 if (rate_sel & 2)
1186 control |= DS1337_BIT_RS2;
1187
1188 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1189 control);
1190}
1191
1192static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1193{
1194 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1195
1196 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1197}
1198
1199static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1200{
1201 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1202
1203 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1204}
1205
1206static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1207{
1208 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001209 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001210
Heiner Kallweit11e58902017-03-10 18:52:34 +01001211 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1212 if (ret)
1213 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001214
1215 return !(control & DS1337_BIT_INTCN);
1216}
1217
1218static const struct clk_ops ds3231_clk_sqw_ops = {
1219 .prepare = ds3231_clk_sqw_prepare,
1220 .unprepare = ds3231_clk_sqw_unprepare,
1221 .is_prepared = ds3231_clk_sqw_is_prepared,
1222 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1223 .round_rate = ds3231_clk_sqw_round_rate,
1224 .set_rate = ds3231_clk_sqw_set_rate,
1225};
1226
1227static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
1228 unsigned long parent_rate)
1229{
1230 return 32768;
1231}
1232
1233static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1234{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001235 struct mutex *lock = &ds1307->rtc->ops_lock;
1236 int status;
1237 int ret;
1238
1239 mutex_lock(lock);
1240
Heiner Kallweit11e58902017-03-10 18:52:34 +01001241 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1242 if (ret)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001243 goto out;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001244
1245 if (enable)
1246 status |= DS3231_BIT_EN32KHZ;
1247 else
1248 status &= ~DS3231_BIT_EN32KHZ;
1249
Heiner Kallweit11e58902017-03-10 18:52:34 +01001250 ret = regmap_write(ds1307->regmap, DS1337_REG_STATUS, status);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001251out:
1252 mutex_unlock(lock);
1253
1254 return ret;
1255}
1256
1257static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1258{
1259 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1260
1261 return ds3231_clk_32khz_control(ds1307, true);
1262}
1263
1264static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1265{
1266 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1267
1268 ds3231_clk_32khz_control(ds1307, false);
1269}
1270
1271static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1272{
1273 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001274 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001275
Heiner Kallweit11e58902017-03-10 18:52:34 +01001276 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1277 if (ret)
1278 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001279
1280 return !!(status & DS3231_BIT_EN32KHZ);
1281}
1282
1283static const struct clk_ops ds3231_clk_32khz_ops = {
1284 .prepare = ds3231_clk_32khz_prepare,
1285 .unprepare = ds3231_clk_32khz_unprepare,
1286 .is_prepared = ds3231_clk_32khz_is_prepared,
1287 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1288};
1289
1290static struct clk_init_data ds3231_clks_init[] = {
1291 [DS3231_CLK_SQW] = {
1292 .name = "ds3231_clk_sqw",
1293 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001294 },
1295 [DS3231_CLK_32KHZ] = {
1296 .name = "ds3231_clk_32khz",
1297 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001298 },
1299};
1300
1301static int ds3231_clks_register(struct ds1307 *ds1307)
1302{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001303 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001304 struct clk_onecell_data *onecell;
1305 int i;
1306
Heiner Kallweit11e58902017-03-10 18:52:34 +01001307 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001308 if (!onecell)
1309 return -ENOMEM;
1310
1311 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001312 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1313 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001314 if (!onecell->clks)
1315 return -ENOMEM;
1316
1317 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1318 struct clk_init_data init = ds3231_clks_init[i];
1319
1320 /*
1321 * Interrupt signal due to alarm conditions and square-wave
1322 * output share same pin, so don't initialize both.
1323 */
1324 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1325 continue;
1326
1327 /* optional override of the clockname */
1328 of_property_read_string_index(node, "clock-output-names", i,
1329 &init.name);
1330 ds1307->clks[i].init = &init;
1331
Heiner Kallweit11e58902017-03-10 18:52:34 +01001332 onecell->clks[i] = devm_clk_register(ds1307->dev,
1333 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001334 if (IS_ERR(onecell->clks[i]))
1335 return PTR_ERR(onecell->clks[i]);
1336 }
1337
1338 if (!node)
1339 return 0;
1340
1341 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1342
1343 return 0;
1344}
1345
1346static void ds1307_clks_register(struct ds1307 *ds1307)
1347{
1348 int ret;
1349
1350 if (ds1307->type != ds_3231)
1351 return;
1352
1353 ret = ds3231_clks_register(ds1307);
1354 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001355 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1356 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001357 }
1358}
1359
1360#else
1361
1362static void ds1307_clks_register(struct ds1307 *ds1307)
1363{
1364}
1365
1366#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001367
Heiner Kallweit11e58902017-03-10 18:52:34 +01001368static const struct regmap_config regmap_config = {
1369 .reg_bits = 8,
1370 .val_bits = 8,
1371 .max_register = 0x12,
1372};
1373
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001374static int ds1307_probe(struct i2c_client *client,
1375 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001376{
1377 struct ds1307 *ds1307;
1378 int err = -ENODEV;
Keerthye29385f2016-06-01 16:19:07 +05301379 int tmp, wday;
Tin Huynh9c19b892016-11-30 09:57:31 +07001380 struct chip_desc *chip;
Peter Senna Tschudinc8b18da2013-11-12 15:10:59 -08001381 bool want_irq = false;
Michael Lange8bc2a402016-01-21 18:10:16 +01001382 bool ds1307_can_wakeup_device = false;
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001383 unsigned char *buf;
Jingoo Han01ce8932013-11-12 15:10:41 -08001384 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Keerthye29385f2016-06-01 16:19:07 +05301385 struct rtc_time tm;
1386 unsigned long timestamp;
1387
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001388 irq_handler_t irq_handler = ds1307_irq;
1389
Wolfram Sang97f902b2009-06-17 16:26:10 -07001390 static const int bbsqi_bitpos[] = {
1391 [ds_1337] = 0,
1392 [ds_1339] = DS1339_BIT_BBSQI,
1393 [ds_3231] = DS3231_BIT_BBSQW,
1394 };
Simon Guinot1d1945d2014-04-03 14:49:55 -07001395 const struct rtc_class_ops *rtc_ops = &ds13xx_rtc_ops;
David Brownell1abb0dc2006-06-25 05:48:17 -07001396
Jingoo Hanedca66d2013-07-03 15:07:05 -07001397 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001398 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001399 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001400
Heiner Kallweit11e58902017-03-10 18:52:34 +01001401 dev_set_drvdata(&client->dev, ds1307);
1402 ds1307->dev = &client->dev;
1403 ds1307->name = client->name;
1404 ds1307->irq = client->irq;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001405
Heiner Kallweit11e58902017-03-10 18:52:34 +01001406 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1407 if (IS_ERR(ds1307->regmap)) {
1408 dev_err(ds1307->dev, "regmap allocation failed\n");
1409 return PTR_ERR(ds1307->regmap);
1410 }
1411
1412 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001413
1414 if (client->dev.of_node) {
1415 ds1307->type = (enum ds_type)
1416 of_device_get_match_data(&client->dev);
1417 chip = &chips[ds1307->type];
1418 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001419 chip = &chips[id->driver_data];
1420 ds1307->type = id->driver_data;
1421 } else {
1422 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001423
Tin Huynh9c19b892016-11-30 09:57:31 +07001424 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001425 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001426 if (!acpi_id)
1427 return -ENODEV;
1428 chip = &chips[acpi_id->driver_data];
1429 ds1307->type = acpi_id->driver_data;
1430 }
1431
1432 if (!pdata)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001433 ds1307_trickle_init(ds1307, chip);
Tin Huynh9c19b892016-11-30 09:57:31 +07001434 else if (pdata->trickle_charger_setup)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001435 chip->trickle_charger_setup = pdata->trickle_charger_setup;
1436
1437 if (chip->trickle_charger_setup && chip->trickle_charger_reg) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001438 dev_dbg(ds1307->dev,
1439 "writing trickle charger info 0x%x to 0x%x\n",
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001440 DS13XX_TRICKLE_CHARGER_MAGIC | chip->trickle_charger_setup,
1441 chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001442 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001443 DS13XX_TRICKLE_CHARGER_MAGIC |
1444 chip->trickle_charger_setup);
1445 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001446
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001447 buf = ds1307->regs;
David Brownell045e0e82007-07-17 04:04:55 -07001448
Michael Lange8bc2a402016-01-21 18:10:16 +01001449#ifdef CONFIG_OF
1450/*
1451 * For devices with no IRQ directly connected to the SoC, the RTC chip
1452 * can be forced as a wakeup source by stating that explicitly in
1453 * the device's .dts file using the "wakeup-source" boolean property.
1454 * If the "wakeup-source" property is set, don't request an IRQ.
1455 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1456 * if supported by the RTC.
1457 */
1458 if (of_property_read_bool(client->dev.of_node, "wakeup-source")) {
1459 ds1307_can_wakeup_device = true;
1460 }
Alexandre Belloni78aaa062016-07-13 02:36:41 +02001461 /* Intersil ISL12057 DT backward compatibility */
1462 if (of_property_read_bool(client->dev.of_node,
1463 "isil,irq2-can-wakeup-machine")) {
1464 ds1307_can_wakeup_device = true;
1465 }
Michael Lange8bc2a402016-01-21 18:10:16 +01001466#endif
1467
David Brownell045e0e82007-07-17 04:04:55 -07001468 switch (ds1307->type) {
1469 case ds_1337:
1470 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001471 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001472 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001473 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
1474 buf, 2);
1475 if (err) {
1476 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001477 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001478 }
1479
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001480 /* oscillator off? turn it on, so clock can tick. */
1481 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001482 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
1483
David Anders40ce9722012-03-23 15:02:37 -07001484 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001485 * Using IRQ or defined as wakeup-source?
1486 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001487 * For some variants, be sure alarms can trigger when we're
1488 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001489 */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001490 if (chip->alarm && (ds1307->irq > 0 ||
1491 ds1307_can_wakeup_device)) {
Wolfram Sang97f902b2009-06-17 16:26:10 -07001492 ds1307->regs[0] |= DS1337_BIT_INTCN
1493 | bbsqi_bitpos[ds1307->type];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001494 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Wolfram Sangb24a7262012-03-23 15:02:37 -07001495
1496 want_irq = true;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001497 }
1498
Heiner Kallweit11e58902017-03-10 18:52:34 +01001499 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
1500 ds1307->regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001501
1502 /* oscillator fault? clear flag, and warn */
1503 if (ds1307->regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001504 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
1505 ds1307->regs[1] & ~DS1337_BIT_OSF);
1506 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001507 }
David Brownell045e0e82007-07-17 04:04:55 -07001508 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001509
1510 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001511 err = regmap_bulk_read(ds1307->regmap,
1512 RX8025_REG_CTRL1 << 4 | 0x08, buf, 2);
1513 if (err) {
1514 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001515 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001516 }
1517
1518 /* oscillator off? turn it on, so clock can tick. */
1519 if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
1520 ds1307->regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001521 regmap_write(ds1307->regmap,
1522 RX8025_REG_CTRL2 << 4 | 0x08,
1523 ds1307->regs[1]);
1524 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001525 "oscillator stop detected - SET TIME!\n");
1526 }
1527
1528 if (ds1307->regs[1] & RX8025_BIT_PON) {
1529 ds1307->regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001530 regmap_write(ds1307->regmap,
1531 RX8025_REG_CTRL2 << 4 | 0x08,
1532 ds1307->regs[1]);
1533 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001534 }
1535
1536 if (ds1307->regs[1] & RX8025_BIT_VDET) {
1537 ds1307->regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001538 regmap_write(ds1307->regmap,
1539 RX8025_REG_CTRL2 << 4 | 0x08,
1540 ds1307->regs[1]);
1541 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001542 }
1543
1544 /* make sure we are running in 24hour mode */
1545 if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
1546 u8 hour;
1547
1548 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001549 regmap_write(ds1307->regmap,
1550 RX8025_REG_CTRL1 << 4 | 0x08,
1551 ds1307->regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001552
Heiner Kallweit11e58902017-03-10 18:52:34 +01001553 err = regmap_bulk_read(ds1307->regmap,
1554 RX8025_REG_CTRL1 << 4 | 0x08,
1555 buf, 2);
1556 if (err) {
1557 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001558 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001559 }
1560
1561 /* correct hour */
1562 hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
1563 if (hour == 12)
1564 hour = 0;
1565 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1566 hour += 12;
1567
Heiner Kallweit11e58902017-03-10 18:52:34 +01001568 regmap_write(ds1307->regmap,
1569 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001570 }
1571 break;
Marek Vasutee0981b2017-06-18 22:55:28 +02001572 case rx_8130:
1573 ds1307->offset = 0x10; /* Seconds starts at 0x10 */
1574 rtc_ops = &rx8130_rtc_ops;
1575 if (chip->alarm && ds1307->irq > 0) {
1576 irq_handler = rx8130_irq;
1577 want_irq = true;
1578 }
1579 break;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001580 case ds_1388:
1581 ds1307->offset = 1; /* Seconds starts at 1 */
1582 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001583 case mcp794xx:
1584 rtc_ops = &mcp794xx_rtc_ops;
David Lowe80663602017-04-22 18:28:00 +01001585 if (chip->alarm && (ds1307->irq > 0 ||
1586 ds1307_can_wakeup_device)) {
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001587 irq_handler = mcp794xx_irq;
Simon Guinot1d1945d2014-04-03 14:49:55 -07001588 want_irq = true;
1589 }
1590 break;
David Brownell045e0e82007-07-17 04:04:55 -07001591 default:
1592 break;
1593 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001594
1595read_rtc:
1596 /* read RTC registers */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001597 err = regmap_bulk_read(ds1307->regmap, ds1307->offset, buf, 8);
1598 if (err) {
1599 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001600 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001601 }
1602
David Anders40ce9722012-03-23 15:02:37 -07001603 /*
1604 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001605 * specify the extra bits as must-be-zero, but there are
1606 * still a few values that are clearly out-of-range.
1607 */
1608 tmp = ds1307->regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001609 switch (ds1307->type) {
1610 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001611 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001612 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001613 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001614 if (tmp & DS1307_BIT_CH) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001615 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1616 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001617 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001618 }
David Brownell045e0e82007-07-17 04:04:55 -07001619 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001620 case ds_1338:
1621 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001622 if (tmp & DS1307_BIT_CH)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001623 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001624
1625 /* oscillator fault? clear flag, and warn */
1626 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001627 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
1628 ds1307->regs[DS1307_REG_CONTROL] &
1629 ~DS1338_BIT_OSF);
1630 dev_warn(ds1307->dev, "SET TIME!\n");
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001631 goto read_rtc;
1632 }
David Brownell045e0e82007-07-17 04:04:55 -07001633 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001634 case ds_1340:
1635 /* clock halted? turn it on, so clock can tick. */
1636 if (tmp & DS1340_BIT_nEOSC)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001637 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
frederic Rodofcd8db02008-02-06 01:38:55 -08001638
Heiner Kallweit11e58902017-03-10 18:52:34 +01001639 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1640 if (err) {
1641 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001642 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001643 }
1644
1645 /* oscillator fault? clear flag, and warn */
1646 if (tmp & DS1340_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001647 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1648 dev_warn(ds1307->dev, "SET TIME!\n");
frederic Rodofcd8db02008-02-06 01:38:55 -08001649 }
1650 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001651 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001652 /* make sure that the backup battery is enabled */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001653 if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001654 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
1655 ds1307->regs[DS1307_REG_WDAY] |
1656 MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001657 }
1658
1659 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001660 if (!(tmp & MCP794XX_BIT_ST)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001661 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1662 MCP794XX_BIT_ST);
1663 dev_warn(ds1307->dev, "SET TIME!\n");
David Anders43fcb812011-11-02 13:37:53 -07001664 goto read_rtc;
1665 }
1666
1667 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001668 default:
David Brownell045e0e82007-07-17 04:04:55 -07001669 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001670 }
David Brownell045e0e82007-07-17 04:04:55 -07001671
David Brownell1abb0dc2006-06-25 05:48:17 -07001672 tmp = ds1307->regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001673 switch (ds1307->type) {
1674 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001675 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001676 case m41t00:
David Anders40ce9722012-03-23 15:02:37 -07001677 /*
1678 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001679 * systems that will run through year 2100.
1680 */
1681 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001682 case rx_8025:
1683 break;
David Brownellc065f352007-07-17 04:05:10 -07001684 default:
1685 if (!(tmp & DS1307_BIT_12HR))
1686 break;
1687
David Anders40ce9722012-03-23 15:02:37 -07001688 /*
1689 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001690 * take note...
1691 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001692 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001693 if (tmp == 12)
1694 tmp = 0;
1695 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1696 tmp += 12;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001697 regmap_write(ds1307->regmap, ds1307->offset + DS1307_REG_HOUR,
1698 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001699 }
1700
Keerthye29385f2016-06-01 16:19:07 +05301701 /*
1702 * Some IPs have weekday reset value = 0x1 which might not correct
1703 * hence compute the wday using the current date/month/year values
1704 */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001705 ds1307_get_time(ds1307->dev, &tm);
Keerthye29385f2016-06-01 16:19:07 +05301706 wday = tm.tm_wday;
1707 timestamp = rtc_tm_to_time64(&tm);
1708 rtc_time64_to_tm(timestamp, &tm);
1709
1710 /*
1711 * Check if reset wday is different from the computed wday
1712 * If different then set the wday which we computed using
1713 * timestamp
1714 */
1715 if (wday != tm.tm_wday) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001716 regmap_read(ds1307->regmap, MCP794XX_REG_WEEKDAY, &wday);
Keerthye29385f2016-06-01 16:19:07 +05301717 wday = wday & ~MCP794XX_REG_WEEKDAY_WDAY_MASK;
1718 wday = wday | (tm.tm_wday + 1);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001719 regmap_write(ds1307->regmap, MCP794XX_REG_WEEKDAY, wday);
Keerthye29385f2016-06-01 16:19:07 +05301720 }
1721
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001722 if (want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001723 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001724 set_bit(HAS_ALARM, &ds1307->flags);
1725 }
Heiner Kallweit11e58902017-03-10 18:52:34 +01001726 ds1307->rtc = devm_rtc_device_register(ds1307->dev, ds1307->name,
Simon Guinot1d1945d2014-04-03 14:49:55 -07001727 rtc_ops, THIS_MODULE);
David Brownell1abb0dc2006-06-25 05:48:17 -07001728 if (IS_ERR(ds1307->rtc)) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001729 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001730 }
1731
Heiner Kallweit11e58902017-03-10 18:52:34 +01001732 if (ds1307_can_wakeup_device && ds1307->irq <= 0) {
Michael Lange8bc2a402016-01-21 18:10:16 +01001733 /* Disable request for an IRQ */
1734 want_irq = false;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001735 dev_info(ds1307->dev,
1736 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001737 /* We cannot support UIE mode if we do not have an IRQ line */
1738 ds1307->rtc->uie_unsupported = 1;
1739 }
1740
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001741 if (want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001742 err = devm_request_threaded_irq(ds1307->dev,
1743 ds1307->irq, NULL, irq_handler,
Nishanth Menonc5983192015-06-23 11:15:11 -05001744 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001745 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001746 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001747 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001748 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001749 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001750 dev_err(ds1307->dev, "unable to request IRQ!\n");
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001751 } else
Heiner Kallweit11e58902017-03-10 18:52:34 +01001752 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001753 }
1754
Austin Boyle9eab0a72012-03-23 15:02:38 -07001755 if (chip->nvram_size) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001756
Heiner Kallweit11e58902017-03-10 18:52:34 +01001757 ds1307->nvram = devm_kzalloc(ds1307->dev,
Jingoo Hanedca66d2013-07-03 15:07:05 -07001758 sizeof(struct bin_attribute),
1759 GFP_KERNEL);
Austin Boyle9eab0a72012-03-23 15:02:38 -07001760 if (!ds1307->nvram) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001761 dev_err(ds1307->dev,
1762 "cannot allocate memory for nvram sysfs\n");
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001763 } else {
1764
1765 ds1307->nvram->attr.name = "nvram";
1766 ds1307->nvram->attr.mode = S_IRUGO | S_IWUSR;
1767
1768 sysfs_bin_attr_init(ds1307->nvram);
1769
1770 ds1307->nvram->read = ds1307_nvram_read;
1771 ds1307->nvram->write = ds1307_nvram_write;
1772 ds1307->nvram->size = chip->nvram_size;
1773 ds1307->nvram_offset = chip->nvram_offset;
1774
Heiner Kallweit11e58902017-03-10 18:52:34 +01001775 err = sysfs_create_bin_file(&ds1307->dev->kobj,
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001776 ds1307->nvram);
1777 if (err) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001778 dev_err(ds1307->dev,
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001779 "unable to create sysfs file: %s\n",
1780 ds1307->nvram->attr.name);
1781 } else {
1782 set_bit(HAS_NVRAM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001783 dev_info(ds1307->dev, "%zu bytes nvram\n",
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001784 ds1307->nvram->size);
1785 }
David Brownell682d73f2007-11-14 16:58:32 -08001786 }
1787 }
1788
Akinobu Mita445c0202016-01-25 00:22:16 +09001789 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001790 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001791
David Brownell1abb0dc2006-06-25 05:48:17 -07001792 return 0;
1793
Jingoo Hanedca66d2013-07-03 15:07:05 -07001794exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001795 return err;
1796}
1797
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001798static int ds1307_remove(struct i2c_client *client)
David Brownell1abb0dc2006-06-25 05:48:17 -07001799{
David Anders40ce9722012-03-23 15:02:37 -07001800 struct ds1307 *ds1307 = i2c_get_clientdata(client);
David Brownell1abb0dc2006-06-25 05:48:17 -07001801
Jingoo Hanedca66d2013-07-03 15:07:05 -07001802 if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
Heiner Kallweit11e58902017-03-10 18:52:34 +01001803 sysfs_remove_bin_file(&ds1307->dev->kobj, ds1307->nvram);
David Brownell682d73f2007-11-14 16:58:32 -08001804
David Brownell1abb0dc2006-06-25 05:48:17 -07001805 return 0;
1806}
1807
1808static struct i2c_driver ds1307_driver = {
1809 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001810 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001811 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001812 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001813 },
David Brownellc065f352007-07-17 04:05:10 -07001814 .probe = ds1307_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001815 .remove = ds1307_remove,
Jean Delvare3760f732008-04-29 23:11:40 +02001816 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001817};
1818
Axel Lin0abc9202012-03-23 15:02:31 -07001819module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001820
1821MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1822MODULE_LICENSE("GPL");