blob: 9b4106a7e8e4e5eade27ee7b1166452689970e6b [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 Kallweit078f3f62017-06-05 17:57:29 +0200300 int stat, 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 Kallweit078f3f62017-06-05 17:57:29 +0200311 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
312 DS1337_BIT_A1IE, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100313 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700314 goto out;
315
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700316 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700317 }
318
319out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700320 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700321
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700322 return IRQ_HANDLED;
323}
324
325/*----------------------------------------------------------------------*/
326
David Brownell1abb0dc2006-06-25 05:48:17 -0700327static int ds1307_get_time(struct device *dev, struct rtc_time *t)
328{
329 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100330 int tmp, ret;
David Brownell1abb0dc2006-06-25 05:48:17 -0700331
David Brownell045e0e82007-07-17 04:04:55 -0700332 /* read the RTC date and time registers all at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100333 ret = regmap_bulk_read(ds1307->regmap, ds1307->offset, ds1307->regs, 7);
334 if (ret) {
335 dev_err(dev, "%s error %d\n", "read", ret);
336 return ret;
David Brownell1abb0dc2006-06-25 05:48:17 -0700337 }
338
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800339 dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700340
Stefan Agner8566f702017-03-23 16:54:57 -0700341 /* if oscillator fail bit is set, no data can be trusted */
342 if (ds1307->type == m41t0 &&
343 ds1307->regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
344 dev_warn_once(dev, "oscillator failed, set time!\n");
345 return -EINVAL;
346 }
347
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700348 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
349 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700350 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700351 t->tm_hour = bcd2bin(tmp);
352 t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
353 t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700354 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700355 t->tm_mon = bcd2bin(tmp) - 1;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700356 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700357
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200358#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
359 switch (ds1307->type) {
360 case ds_1337:
361 case ds_1339:
362 case ds_3231:
363 if (ds1307->regs[DS1307_REG_MONTH] & DS1337_BIT_CENTURY)
364 t->tm_year += 100;
365 break;
366 case ds_1340:
367 if (ds1307->regs[DS1307_REG_HOUR] & DS1340_BIT_CENTURY)
368 t->tm_year += 100;
369 break;
370 default:
371 break;
372 }
373#endif
374
David Brownell1abb0dc2006-06-25 05:48:17 -0700375 dev_dbg(dev, "%s secs=%d, mins=%d, "
376 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
377 "read", t->tm_sec, t->tm_min,
378 t->tm_hour, t->tm_mday,
379 t->tm_mon, t->tm_year, t->tm_wday);
380
David Brownell045e0e82007-07-17 04:04:55 -0700381 /* initial clock setting can be undefined */
382 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700383}
384
385static int ds1307_set_time(struct device *dev, struct rtc_time *t)
386{
387 struct ds1307 *ds1307 = dev_get_drvdata(dev);
388 int result;
389 int tmp;
390 u8 *buf = ds1307->regs;
391
392 dev_dbg(dev, "%s secs=%d, mins=%d, "
393 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400394 "write", t->tm_sec, t->tm_min,
395 t->tm_hour, t->tm_mday,
396 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700397
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200398#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
399 if (t->tm_year < 100)
400 return -EINVAL;
401
402 switch (ds1307->type) {
403 case ds_1337:
404 case ds_1339:
405 case ds_3231:
406 case ds_1340:
407 if (t->tm_year > 299)
408 return -EINVAL;
409 default:
410 if (t->tm_year > 199)
411 return -EINVAL;
412 break;
413 }
414#else
415 if (t->tm_year < 100 || t->tm_year > 199)
416 return -EINVAL;
417#endif
418
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700419 buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
420 buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
421 buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
422 buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
423 buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
424 buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700425
426 /* assume 20YY not 19YY */
427 tmp = t->tm_year - 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700428 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700429
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700430 switch (ds1307->type) {
431 case ds_1337:
432 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -0700433 case ds_3231:
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200434 if (t->tm_year > 199)
435 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700436 break;
437 case ds_1340:
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200438 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN;
439 if (t->tm_year > 199)
440 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700441 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800442 case mcp794xx:
David Anders40ce9722012-03-23 15:02:37 -0700443 /*
444 * these bits were cleared when preparing the date/time
445 * values and need to be set again before writing the
446 * buffer out to the device.
447 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800448 buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
449 buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
David Anders43fcb812011-11-02 13:37:53 -0700450 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700451 default:
452 break;
453 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700454
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800455 dev_dbg(dev, "%s: %7ph\n", "write", buf);
David Brownell1abb0dc2006-06-25 05:48:17 -0700456
Heiner Kallweit11e58902017-03-10 18:52:34 +0100457 result = regmap_bulk_write(ds1307->regmap, ds1307->offset, buf, 7);
458 if (result) {
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800459 dev_err(dev, "%s error %d\n", "write", result);
460 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700461 }
462 return 0;
463}
464
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800465static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700466{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100467 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700468 int ret;
469
470 if (!test_bit(HAS_ALARM, &ds1307->flags))
471 return -EINVAL;
472
473 /* read all ALARM1, ALARM2, and status registers at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100474 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
475 ds1307->regs, 9);
476 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700477 dev_err(dev, "%s error %d\n", "alarm read", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100478 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700479 }
480
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100481 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
482 &ds1307->regs[0], &ds1307->regs[4], &ds1307->regs[7]);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700483
David Anders40ce9722012-03-23 15:02:37 -0700484 /*
485 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700486 * and that all four fields are checked matches
487 */
488 t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
489 t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
490 t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
491 t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700492
493 /* ... and status */
494 t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
495 t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
496
497 dev_dbg(dev, "%s secs=%d, mins=%d, "
498 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
499 "alarm read", t->time.tm_sec, t->time.tm_min,
500 t->time.tm_hour, t->time.tm_mday,
501 t->enabled, t->pending);
502
503 return 0;
504}
505
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800506static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700507{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100508 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700509 unsigned char *buf = ds1307->regs;
510 u8 control, status;
511 int ret;
512
513 if (!test_bit(HAS_ALARM, &ds1307->flags))
514 return -EINVAL;
515
516 dev_dbg(dev, "%s secs=%d, mins=%d, "
517 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
518 "alarm set", t->time.tm_sec, t->time.tm_min,
519 t->time.tm_hour, t->time.tm_mday,
520 t->enabled, t->pending);
521
522 /* read current status of both alarms and the chip */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100523 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, buf, 9);
524 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700525 dev_err(dev, "%s error %d\n", "alarm write", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100526 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700527 }
528 control = ds1307->regs[7];
529 status = ds1307->regs[8];
530
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100531 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
532 &ds1307->regs[0], &ds1307->regs[4], control, status);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700533
534 /* set ALARM1, using 24 hour and day-of-month modes */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700535 buf[0] = bin2bcd(t->time.tm_sec);
536 buf[1] = bin2bcd(t->time.tm_min);
537 buf[2] = bin2bcd(t->time.tm_hour);
538 buf[3] = bin2bcd(t->time.tm_mday);
539
540 /* set ALARM2 to non-garbage */
541 buf[4] = 0;
542 buf[5] = 0;
543 buf[6] = 0;
544
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200545 /* disable alarms */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700546 buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700547 buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
548
Heiner Kallweit11e58902017-03-10 18:52:34 +0100549 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, buf, 9);
550 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700551 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800552 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700553 }
554
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200555 /* optionally enable ALARM1 */
556 if (t->enabled) {
557 dev_dbg(dev, "alarm IRQ armed\n");
558 buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100559 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, buf[7]);
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200560 }
561
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700562 return 0;
563}
564
John Stultz16380c12011-02-02 17:02:41 -0800565static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700566{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100567 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700568
John Stultz16380c12011-02-02 17:02:41 -0800569 if (!test_bit(HAS_ALARM, &ds1307->flags))
570 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700571
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200572 return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
573 DS1337_BIT_A1IE,
574 enabled ? DS1337_BIT_A1IE : 0);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700575}
576
David Brownellff8371a2006-09-30 23:28:17 -0700577static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700578 .read_time = ds1307_get_time,
579 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800580 .read_alarm = ds1337_read_alarm,
581 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800582 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700583};
584
David Brownell682d73f2007-11-14 16:58:32 -0800585/*----------------------------------------------------------------------*/
586
Simon Guinot1d1945d2014-04-03 14:49:55 -0700587/*
Marek Vasutee0981b2017-06-18 22:55:28 +0200588 * Alarm support for rx8130 devices.
589 */
590
591#define RX8130_REG_ALARM_MIN 0x07
592#define RX8130_REG_ALARM_HOUR 0x08
593#define RX8130_REG_ALARM_WEEK_OR_DAY 0x09
594#define RX8130_REG_EXTENSION 0x0c
595#define RX8130_REG_EXTENSION_WADA (1 << 3)
596#define RX8130_REG_FLAG 0x0d
597#define RX8130_REG_FLAG_AF (1 << 3)
598#define RX8130_REG_CONTROL0 0x0e
599#define RX8130_REG_CONTROL0_AIE (1 << 3)
600
601static irqreturn_t rx8130_irq(int irq, void *dev_id)
602{
603 struct ds1307 *ds1307 = dev_id;
604 struct mutex *lock = &ds1307->rtc->ops_lock;
605 u8 ctl[3];
606 int ret;
607
608 mutex_lock(lock);
609
610 /* Read control registers. */
611 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
612 if (ret < 0)
613 goto out;
614 if (!(ctl[1] & RX8130_REG_FLAG_AF))
615 goto out;
616 ctl[1] &= ~RX8130_REG_FLAG_AF;
617 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
618
619 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
620 if (ret < 0)
621 goto out;
622
623 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
624
625out:
626 mutex_unlock(lock);
627
628 return IRQ_HANDLED;
629}
630
631static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
632{
633 struct ds1307 *ds1307 = dev_get_drvdata(dev);
634 u8 ald[3], ctl[3];
635 int ret;
636
637 if (!test_bit(HAS_ALARM, &ds1307->flags))
638 return -EINVAL;
639
640 /* Read alarm registers. */
641 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, 3);
642 if (ret < 0)
643 return ret;
644
645 /* Read control registers. */
646 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
647 if (ret < 0)
648 return ret;
649
650 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
651 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
652
653 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
654 t->time.tm_sec = -1;
655 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
656 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
657 t->time.tm_wday = -1;
658 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
659 t->time.tm_mon = -1;
660 t->time.tm_year = -1;
661 t->time.tm_yday = -1;
662 t->time.tm_isdst = -1;
663
664 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
665 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
666 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
667
668 return 0;
669}
670
671static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
672{
673 struct ds1307 *ds1307 = dev_get_drvdata(dev);
674 u8 ald[3], ctl[3];
675 int ret;
676
677 if (!test_bit(HAS_ALARM, &ds1307->flags))
678 return -EINVAL;
679
680 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
681 "enabled=%d pending=%d\n", __func__,
682 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
683 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
684 t->enabled, t->pending);
685
686 /* Read control registers. */
687 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
688 if (ret < 0)
689 return ret;
690
691 ctl[0] &= ~RX8130_REG_EXTENSION_WADA;
692 ctl[1] |= RX8130_REG_FLAG_AF;
693 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
694
695 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
696 if (ret < 0)
697 return ret;
698
699 /* Hardware alarm precision is 1 minute! */
700 ald[0] = bin2bcd(t->time.tm_min);
701 ald[1] = bin2bcd(t->time.tm_hour);
702 ald[2] = bin2bcd(t->time.tm_mday);
703
704 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, 3);
705 if (ret < 0)
706 return ret;
707
708 if (!t->enabled)
709 return 0;
710
711 ctl[2] |= RX8130_REG_CONTROL0_AIE;
712
713 return regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
714}
715
716static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
717{
718 struct ds1307 *ds1307 = dev_get_drvdata(dev);
719 int ret, reg;
720
721 if (!test_bit(HAS_ALARM, &ds1307->flags))
722 return -EINVAL;
723
724 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
725 if (ret < 0)
726 return ret;
727
728 if (enabled)
729 reg |= RX8130_REG_CONTROL0_AIE;
730 else
731 reg &= ~RX8130_REG_CONTROL0_AIE;
732
733 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
734}
735
736static const struct rtc_class_ops rx8130_rtc_ops = {
737 .read_time = ds1307_get_time,
738 .set_time = ds1307_set_time,
739 .read_alarm = rx8130_read_alarm,
740 .set_alarm = rx8130_set_alarm,
741 .alarm_irq_enable = rx8130_alarm_irq_enable,
742};
743
744/*----------------------------------------------------------------------*/
745
746/*
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800747 * Alarm support for mcp794xx devices.
Simon Guinot1d1945d2014-04-03 14:49:55 -0700748 */
749
Keerthye29385f2016-06-01 16:19:07 +0530750#define MCP794XX_REG_WEEKDAY 0x3
751#define MCP794XX_REG_WEEKDAY_WDAY_MASK 0x7
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800752#define MCP794XX_REG_CONTROL 0x07
753# define MCP794XX_BIT_ALM0_EN 0x10
754# define MCP794XX_BIT_ALM1_EN 0x20
755#define MCP794XX_REG_ALARM0_BASE 0x0a
756#define MCP794XX_REG_ALARM0_CTRL 0x0d
757#define MCP794XX_REG_ALARM1_BASE 0x11
758#define MCP794XX_REG_ALARM1_CTRL 0x14
759# define MCP794XX_BIT_ALMX_IF (1 << 3)
760# define MCP794XX_BIT_ALMX_C0 (1 << 4)
761# define MCP794XX_BIT_ALMX_C1 (1 << 5)
762# define MCP794XX_BIT_ALMX_C2 (1 << 6)
763# define MCP794XX_BIT_ALMX_POL (1 << 7)
764# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
765 MCP794XX_BIT_ALMX_C1 | \
766 MCP794XX_BIT_ALMX_C2)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700767
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500768static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700769{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100770 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500771 struct mutex *lock = &ds1307->rtc->ops_lock;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700772 int reg, ret;
773
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500774 mutex_lock(lock);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700775
776 /* Check and clear alarm 0 interrupt flag. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100777 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
778 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700779 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800780 if (!(reg & MCP794XX_BIT_ALMX_IF))
Simon Guinot1d1945d2014-04-03 14:49:55 -0700781 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800782 reg &= ~MCP794XX_BIT_ALMX_IF;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100783 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
784 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700785 goto out;
786
787 /* Disable alarm 0. */
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200788 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
789 MCP794XX_BIT_ALM0_EN, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100790 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700791 goto out;
792
793 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
794
795out:
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500796 mutex_unlock(lock);
797
798 return IRQ_HANDLED;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700799}
800
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800801static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700802{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100803 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700804 u8 *regs = ds1307->regs;
805 int ret;
806
807 if (!test_bit(HAS_ALARM, &ds1307->flags))
808 return -EINVAL;
809
810 /* Read control and alarm 0 registers. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100811 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
812 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700813 return ret;
814
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800815 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700816
817 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
818 t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f);
819 t->time.tm_min = bcd2bin(ds1307->regs[4] & 0x7f);
820 t->time.tm_hour = bcd2bin(ds1307->regs[5] & 0x3f);
821 t->time.tm_wday = bcd2bin(ds1307->regs[6] & 0x7) - 1;
822 t->time.tm_mday = bcd2bin(ds1307->regs[7] & 0x3f);
823 t->time.tm_mon = bcd2bin(ds1307->regs[8] & 0x1f) - 1;
824 t->time.tm_year = -1;
825 t->time.tm_yday = -1;
826 t->time.tm_isdst = -1;
827
828 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
829 "enabled=%d polarity=%d irq=%d match=%d\n", __func__,
830 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
831 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800832 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL),
833 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF),
834 (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700835
836 return 0;
837}
838
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800839static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700840{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100841 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700842 unsigned char *regs = ds1307->regs;
843 int ret;
844
845 if (!test_bit(HAS_ALARM, &ds1307->flags))
846 return -EINVAL;
847
848 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
849 "enabled=%d pending=%d\n", __func__,
850 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
851 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
852 t->enabled, t->pending);
853
854 /* Read control and alarm 0 registers. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100855 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
856 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700857 return ret;
858
859 /* Set alarm 0, using 24-hour and day-of-month modes. */
860 regs[3] = bin2bcd(t->time.tm_sec);
861 regs[4] = bin2bcd(t->time.tm_min);
862 regs[5] = bin2bcd(t->time.tm_hour);
Tero Kristo62c8c202015-10-23 09:29:57 +0300863 regs[6] = bin2bcd(t->time.tm_wday + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700864 regs[7] = bin2bcd(t->time.tm_mday);
Tero Kristo62c8c202015-10-23 09:29:57 +0300865 regs[8] = bin2bcd(t->time.tm_mon + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700866
867 /* Clear the alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800868 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700869 /* Set alarm match: second, minute, hour, day, date, month. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800870 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
Nishanth Menone3edd672015-04-20 19:51:34 -0500871 /* Disable interrupt. We will not enable until completely programmed */
872 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700873
Heiner Kallweit11e58902017-03-10 18:52:34 +0100874 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
875 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700876 return ret;
877
Nishanth Menone3edd672015-04-20 19:51:34 -0500878 if (!t->enabled)
879 return 0;
880 regs[0] |= MCP794XX_BIT_ALM0_EN;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100881 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700882}
883
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800884static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700885{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100886 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700887
888 if (!test_bit(HAS_ALARM, &ds1307->flags))
889 return -EINVAL;
890
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200891 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
892 MCP794XX_BIT_ALM0_EN,
893 enabled ? MCP794XX_BIT_ALM0_EN : 0);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700894}
895
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800896static const struct rtc_class_ops mcp794xx_rtc_ops = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700897 .read_time = ds1307_get_time,
898 .set_time = ds1307_set_time,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800899 .read_alarm = mcp794xx_read_alarm,
900 .set_alarm = mcp794xx_set_alarm,
901 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
Simon Guinot1d1945d2014-04-03 14:49:55 -0700902};
903
904/*----------------------------------------------------------------------*/
905
David Brownell682d73f2007-11-14 16:58:32 -0800906static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700907ds1307_nvram_read(struct file *filp, struct kobject *kobj,
908 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800909 char *buf, loff_t off, size_t count)
910{
David Brownell682d73f2007-11-14 16:58:32 -0800911 struct ds1307 *ds1307;
David Brownell682d73f2007-11-14 16:58:32 -0800912 int result;
913
Heiner Kallweit11e58902017-03-10 18:52:34 +0100914 ds1307 = dev_get_drvdata(kobj_to_dev(kobj));
David Brownell682d73f2007-11-14 16:58:32 -0800915
Heiner Kallweit11e58902017-03-10 18:52:34 +0100916 result = regmap_bulk_read(ds1307->regmap, ds1307->nvram_offset + off,
917 buf, count);
918 if (result)
919 dev_err(ds1307->dev, "%s error %d\n", "nvram read", result);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800920 return result;
David Brownell682d73f2007-11-14 16:58:32 -0800921}
922
923static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700924ds1307_nvram_write(struct file *filp, struct kobject *kobj,
925 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800926 char *buf, loff_t off, size_t count)
927{
Ed Swierk30e7b032009-03-31 15:24:56 -0700928 struct ds1307 *ds1307;
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800929 int result;
David Brownell682d73f2007-11-14 16:58:32 -0800930
Heiner Kallweit11e58902017-03-10 18:52:34 +0100931 ds1307 = dev_get_drvdata(kobj_to_dev(kobj));
David Brownell682d73f2007-11-14 16:58:32 -0800932
Heiner Kallweit11e58902017-03-10 18:52:34 +0100933 result = regmap_bulk_write(ds1307->regmap, ds1307->nvram_offset + off,
934 buf, count);
935 if (result) {
936 dev_err(ds1307->dev, "%s error %d\n", "nvram write", result);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800937 return result;
938 }
939 return count;
David Brownell682d73f2007-11-14 16:58:32 -0800940}
941
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700942
David Brownell682d73f2007-11-14 16:58:32 -0800943/*----------------------------------------------------------------------*/
944
Heiner Kallweit11e58902017-03-10 18:52:34 +0100945static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700946 uint32_t ohms, bool diode)
947{
948 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
949 DS1307_TRICKLE_CHARGER_NO_DIODE;
950
951 switch (ohms) {
952 case 250:
953 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
954 break;
955 case 2000:
956 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
957 break;
958 case 4000:
959 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
960 break;
961 default:
Heiner Kallweit11e58902017-03-10 18:52:34 +0100962 dev_warn(ds1307->dev,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700963 "Unsupported ohm value %u in dt\n", ohms);
964 return 0;
965 }
966 return setup;
967}
968
Heiner Kallweit11e58902017-03-10 18:52:34 +0100969static void ds1307_trickle_init(struct ds1307 *ds1307,
Tin Huynh9c19b892016-11-30 09:57:31 +0700970 struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700971{
972 uint32_t ohms = 0;
973 bool diode = true;
974
975 if (!chip->do_trickle_setup)
976 goto out;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100977 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
978 &ohms))
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700979 goto out;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100980 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700981 diode = false;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100982 chip->trickle_charger_setup = chip->do_trickle_setup(ds1307,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700983 ohms, diode);
984out:
985 return;
986}
987
Akinobu Mita445c0202016-01-25 00:22:16 +0900988/*----------------------------------------------------------------------*/
989
990#ifdef CONFIG_RTC_DRV_DS1307_HWMON
991
992/*
993 * Temperature sensor support for ds3231 devices.
994 */
995
996#define DS3231_REG_TEMPERATURE 0x11
997
998/*
999 * A user-initiated temperature conversion is not started by this function,
1000 * so the temperature is updated once every 64 seconds.
1001 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001002static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +09001003{
1004 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1005 u8 temp_buf[2];
1006 s16 temp;
1007 int ret;
1008
Heiner Kallweit11e58902017-03-10 18:52:34 +01001009 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
1010 temp_buf, sizeof(temp_buf));
1011 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +09001012 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +09001013 /*
1014 * Temperature is represented as a 10-bit code with a resolution of
1015 * 0.25 degree celsius and encoded in two's complement format.
1016 */
1017 temp = (temp_buf[0] << 8) | temp_buf[1];
1018 temp >>= 6;
1019 *mC = temp * 250;
1020
1021 return 0;
1022}
1023
1024static ssize_t ds3231_hwmon_show_temp(struct device *dev,
1025 struct device_attribute *attr, char *buf)
1026{
1027 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001028 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001029
1030 ret = ds3231_hwmon_read_temp(dev, &temp);
1031 if (ret)
1032 return ret;
1033
1034 return sprintf(buf, "%d\n", temp);
1035}
1036static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ds3231_hwmon_show_temp,
1037 NULL, 0);
1038
1039static struct attribute *ds3231_hwmon_attrs[] = {
1040 &sensor_dev_attr_temp1_input.dev_attr.attr,
1041 NULL,
1042};
1043ATTRIBUTE_GROUPS(ds3231_hwmon);
1044
1045static void ds1307_hwmon_register(struct ds1307 *ds1307)
1046{
1047 struct device *dev;
1048
1049 if (ds1307->type != ds_3231)
1050 return;
1051
Heiner Kallweit11e58902017-03-10 18:52:34 +01001052 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Akinobu Mita445c0202016-01-25 00:22:16 +09001053 ds1307, ds3231_hwmon_groups);
1054 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001055 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1056 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001057 }
1058}
1059
1060#else
1061
1062static void ds1307_hwmon_register(struct ds1307 *ds1307)
1063{
1064}
1065
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001066#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1067
1068/*----------------------------------------------------------------------*/
1069
1070/*
1071 * Square-wave output support for DS3231
1072 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1073 */
1074#ifdef CONFIG_COMMON_CLK
1075
1076enum {
1077 DS3231_CLK_SQW = 0,
1078 DS3231_CLK_32KHZ,
1079};
1080
1081#define clk_sqw_to_ds1307(clk) \
1082 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1083#define clk_32khz_to_ds1307(clk) \
1084 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1085
1086static int ds3231_clk_sqw_rates[] = {
1087 1,
1088 1024,
1089 4096,
1090 8192,
1091};
1092
1093static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1094{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001095 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001096 int ret;
1097
1098 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001099 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1100 mask, value);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001101 mutex_unlock(lock);
1102
1103 return ret;
1104}
1105
1106static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1107 unsigned long parent_rate)
1108{
1109 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001110 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001111 int rate_sel = 0;
1112
Heiner Kallweit11e58902017-03-10 18:52:34 +01001113 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1114 if (ret)
1115 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001116 if (control & DS1337_BIT_RS1)
1117 rate_sel += 1;
1118 if (control & DS1337_BIT_RS2)
1119 rate_sel += 2;
1120
1121 return ds3231_clk_sqw_rates[rate_sel];
1122}
1123
1124static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
1125 unsigned long *prate)
1126{
1127 int i;
1128
1129 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1130 if (ds3231_clk_sqw_rates[i] <= rate)
1131 return ds3231_clk_sqw_rates[i];
1132 }
1133
1134 return 0;
1135}
1136
1137static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
1138 unsigned long parent_rate)
1139{
1140 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1141 int control = 0;
1142 int rate_sel;
1143
1144 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1145 rate_sel++) {
1146 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1147 break;
1148 }
1149
1150 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1151 return -EINVAL;
1152
1153 if (rate_sel & 1)
1154 control |= DS1337_BIT_RS1;
1155 if (rate_sel & 2)
1156 control |= DS1337_BIT_RS2;
1157
1158 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1159 control);
1160}
1161
1162static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1163{
1164 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1165
1166 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1167}
1168
1169static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1170{
1171 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1172
1173 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1174}
1175
1176static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1177{
1178 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001179 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001180
Heiner Kallweit11e58902017-03-10 18:52:34 +01001181 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1182 if (ret)
1183 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001184
1185 return !(control & DS1337_BIT_INTCN);
1186}
1187
1188static const struct clk_ops ds3231_clk_sqw_ops = {
1189 .prepare = ds3231_clk_sqw_prepare,
1190 .unprepare = ds3231_clk_sqw_unprepare,
1191 .is_prepared = ds3231_clk_sqw_is_prepared,
1192 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1193 .round_rate = ds3231_clk_sqw_round_rate,
1194 .set_rate = ds3231_clk_sqw_set_rate,
1195};
1196
1197static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
1198 unsigned long parent_rate)
1199{
1200 return 32768;
1201}
1202
1203static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1204{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001205 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001206 int ret;
1207
1208 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001209 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1210 DS3231_BIT_EN32KHZ,
1211 enable ? DS3231_BIT_EN32KHZ : 0);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001212 mutex_unlock(lock);
1213
1214 return ret;
1215}
1216
1217static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1218{
1219 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1220
1221 return ds3231_clk_32khz_control(ds1307, true);
1222}
1223
1224static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1225{
1226 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1227
1228 ds3231_clk_32khz_control(ds1307, false);
1229}
1230
1231static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1232{
1233 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001234 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001235
Heiner Kallweit11e58902017-03-10 18:52:34 +01001236 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1237 if (ret)
1238 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001239
1240 return !!(status & DS3231_BIT_EN32KHZ);
1241}
1242
1243static const struct clk_ops ds3231_clk_32khz_ops = {
1244 .prepare = ds3231_clk_32khz_prepare,
1245 .unprepare = ds3231_clk_32khz_unprepare,
1246 .is_prepared = ds3231_clk_32khz_is_prepared,
1247 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1248};
1249
1250static struct clk_init_data ds3231_clks_init[] = {
1251 [DS3231_CLK_SQW] = {
1252 .name = "ds3231_clk_sqw",
1253 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001254 },
1255 [DS3231_CLK_32KHZ] = {
1256 .name = "ds3231_clk_32khz",
1257 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001258 },
1259};
1260
1261static int ds3231_clks_register(struct ds1307 *ds1307)
1262{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001263 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001264 struct clk_onecell_data *onecell;
1265 int i;
1266
Heiner Kallweit11e58902017-03-10 18:52:34 +01001267 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001268 if (!onecell)
1269 return -ENOMEM;
1270
1271 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001272 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1273 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001274 if (!onecell->clks)
1275 return -ENOMEM;
1276
1277 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1278 struct clk_init_data init = ds3231_clks_init[i];
1279
1280 /*
1281 * Interrupt signal due to alarm conditions and square-wave
1282 * output share same pin, so don't initialize both.
1283 */
1284 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1285 continue;
1286
1287 /* optional override of the clockname */
1288 of_property_read_string_index(node, "clock-output-names", i,
1289 &init.name);
1290 ds1307->clks[i].init = &init;
1291
Heiner Kallweit11e58902017-03-10 18:52:34 +01001292 onecell->clks[i] = devm_clk_register(ds1307->dev,
1293 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001294 if (IS_ERR(onecell->clks[i]))
1295 return PTR_ERR(onecell->clks[i]);
1296 }
1297
1298 if (!node)
1299 return 0;
1300
1301 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1302
1303 return 0;
1304}
1305
1306static void ds1307_clks_register(struct ds1307 *ds1307)
1307{
1308 int ret;
1309
1310 if (ds1307->type != ds_3231)
1311 return;
1312
1313 ret = ds3231_clks_register(ds1307);
1314 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001315 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1316 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001317 }
1318}
1319
1320#else
1321
1322static void ds1307_clks_register(struct ds1307 *ds1307)
1323{
1324}
1325
1326#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001327
Heiner Kallweit11e58902017-03-10 18:52:34 +01001328static const struct regmap_config regmap_config = {
1329 .reg_bits = 8,
1330 .val_bits = 8,
1331 .max_register = 0x12,
1332};
1333
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001334static int ds1307_probe(struct i2c_client *client,
1335 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001336{
1337 struct ds1307 *ds1307;
1338 int err = -ENODEV;
Keerthye29385f2016-06-01 16:19:07 +05301339 int tmp, wday;
Tin Huynh9c19b892016-11-30 09:57:31 +07001340 struct chip_desc *chip;
Peter Senna Tschudinc8b18da2013-11-12 15:10:59 -08001341 bool want_irq = false;
Michael Lange8bc2a402016-01-21 18:10:16 +01001342 bool ds1307_can_wakeup_device = false;
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001343 unsigned char *buf;
Jingoo Han01ce8932013-11-12 15:10:41 -08001344 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Keerthye29385f2016-06-01 16:19:07 +05301345 struct rtc_time tm;
1346 unsigned long timestamp;
1347
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001348 irq_handler_t irq_handler = ds1307_irq;
1349
Wolfram Sang97f902b2009-06-17 16:26:10 -07001350 static const int bbsqi_bitpos[] = {
1351 [ds_1337] = 0,
1352 [ds_1339] = DS1339_BIT_BBSQI,
1353 [ds_3231] = DS3231_BIT_BBSQW,
1354 };
Simon Guinot1d1945d2014-04-03 14:49:55 -07001355 const struct rtc_class_ops *rtc_ops = &ds13xx_rtc_ops;
David Brownell1abb0dc2006-06-25 05:48:17 -07001356
Jingoo Hanedca66d2013-07-03 15:07:05 -07001357 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001358 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001359 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001360
Heiner Kallweit11e58902017-03-10 18:52:34 +01001361 dev_set_drvdata(&client->dev, ds1307);
1362 ds1307->dev = &client->dev;
1363 ds1307->name = client->name;
1364 ds1307->irq = client->irq;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001365
Heiner Kallweit11e58902017-03-10 18:52:34 +01001366 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1367 if (IS_ERR(ds1307->regmap)) {
1368 dev_err(ds1307->dev, "regmap allocation failed\n");
1369 return PTR_ERR(ds1307->regmap);
1370 }
1371
1372 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001373
1374 if (client->dev.of_node) {
1375 ds1307->type = (enum ds_type)
1376 of_device_get_match_data(&client->dev);
1377 chip = &chips[ds1307->type];
1378 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001379 chip = &chips[id->driver_data];
1380 ds1307->type = id->driver_data;
1381 } else {
1382 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001383
Tin Huynh9c19b892016-11-30 09:57:31 +07001384 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001385 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001386 if (!acpi_id)
1387 return -ENODEV;
1388 chip = &chips[acpi_id->driver_data];
1389 ds1307->type = acpi_id->driver_data;
1390 }
1391
1392 if (!pdata)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001393 ds1307_trickle_init(ds1307, chip);
Tin Huynh9c19b892016-11-30 09:57:31 +07001394 else if (pdata->trickle_charger_setup)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001395 chip->trickle_charger_setup = pdata->trickle_charger_setup;
1396
1397 if (chip->trickle_charger_setup && chip->trickle_charger_reg) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001398 dev_dbg(ds1307->dev,
1399 "writing trickle charger info 0x%x to 0x%x\n",
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001400 DS13XX_TRICKLE_CHARGER_MAGIC | chip->trickle_charger_setup,
1401 chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001402 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001403 DS13XX_TRICKLE_CHARGER_MAGIC |
1404 chip->trickle_charger_setup);
1405 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001406
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001407 buf = ds1307->regs;
David Brownell045e0e82007-07-17 04:04:55 -07001408
Michael Lange8bc2a402016-01-21 18:10:16 +01001409#ifdef CONFIG_OF
1410/*
1411 * For devices with no IRQ directly connected to the SoC, the RTC chip
1412 * can be forced as a wakeup source by stating that explicitly in
1413 * the device's .dts file using the "wakeup-source" boolean property.
1414 * If the "wakeup-source" property is set, don't request an IRQ.
1415 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1416 * if supported by the RTC.
1417 */
1418 if (of_property_read_bool(client->dev.of_node, "wakeup-source")) {
1419 ds1307_can_wakeup_device = true;
1420 }
Alexandre Belloni78aaa062016-07-13 02:36:41 +02001421 /* Intersil ISL12057 DT backward compatibility */
1422 if (of_property_read_bool(client->dev.of_node,
1423 "isil,irq2-can-wakeup-machine")) {
1424 ds1307_can_wakeup_device = true;
1425 }
Michael Lange8bc2a402016-01-21 18:10:16 +01001426#endif
1427
David Brownell045e0e82007-07-17 04:04:55 -07001428 switch (ds1307->type) {
1429 case ds_1337:
1430 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001431 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001432 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001433 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
1434 buf, 2);
1435 if (err) {
1436 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001437 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001438 }
1439
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001440 /* oscillator off? turn it on, so clock can tick. */
1441 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001442 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
1443
David Anders40ce9722012-03-23 15:02:37 -07001444 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001445 * Using IRQ or defined as wakeup-source?
1446 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001447 * For some variants, be sure alarms can trigger when we're
1448 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001449 */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001450 if (chip->alarm && (ds1307->irq > 0 ||
1451 ds1307_can_wakeup_device)) {
Wolfram Sang97f902b2009-06-17 16:26:10 -07001452 ds1307->regs[0] |= DS1337_BIT_INTCN
1453 | bbsqi_bitpos[ds1307->type];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001454 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Wolfram Sangb24a7262012-03-23 15:02:37 -07001455
1456 want_irq = true;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001457 }
1458
Heiner Kallweit11e58902017-03-10 18:52:34 +01001459 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
1460 ds1307->regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001461
1462 /* oscillator fault? clear flag, and warn */
1463 if (ds1307->regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001464 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
1465 ds1307->regs[1] & ~DS1337_BIT_OSF);
1466 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001467 }
David Brownell045e0e82007-07-17 04:04:55 -07001468 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001469
1470 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001471 err = regmap_bulk_read(ds1307->regmap,
1472 RX8025_REG_CTRL1 << 4 | 0x08, buf, 2);
1473 if (err) {
1474 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001475 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001476 }
1477
1478 /* oscillator off? turn it on, so clock can tick. */
1479 if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
1480 ds1307->regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001481 regmap_write(ds1307->regmap,
1482 RX8025_REG_CTRL2 << 4 | 0x08,
1483 ds1307->regs[1]);
1484 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001485 "oscillator stop detected - SET TIME!\n");
1486 }
1487
1488 if (ds1307->regs[1] & RX8025_BIT_PON) {
1489 ds1307->regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001490 regmap_write(ds1307->regmap,
1491 RX8025_REG_CTRL2 << 4 | 0x08,
1492 ds1307->regs[1]);
1493 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001494 }
1495
1496 if (ds1307->regs[1] & RX8025_BIT_VDET) {
1497 ds1307->regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001498 regmap_write(ds1307->regmap,
1499 RX8025_REG_CTRL2 << 4 | 0x08,
1500 ds1307->regs[1]);
1501 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001502 }
1503
1504 /* make sure we are running in 24hour mode */
1505 if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
1506 u8 hour;
1507
1508 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001509 regmap_write(ds1307->regmap,
1510 RX8025_REG_CTRL1 << 4 | 0x08,
1511 ds1307->regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001512
Heiner Kallweit11e58902017-03-10 18:52:34 +01001513 err = regmap_bulk_read(ds1307->regmap,
1514 RX8025_REG_CTRL1 << 4 | 0x08,
1515 buf, 2);
1516 if (err) {
1517 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001518 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001519 }
1520
1521 /* correct hour */
1522 hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
1523 if (hour == 12)
1524 hour = 0;
1525 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1526 hour += 12;
1527
Heiner Kallweit11e58902017-03-10 18:52:34 +01001528 regmap_write(ds1307->regmap,
1529 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001530 }
1531 break;
Marek Vasutee0981b2017-06-18 22:55:28 +02001532 case rx_8130:
1533 ds1307->offset = 0x10; /* Seconds starts at 0x10 */
1534 rtc_ops = &rx8130_rtc_ops;
1535 if (chip->alarm && ds1307->irq > 0) {
1536 irq_handler = rx8130_irq;
1537 want_irq = true;
1538 }
1539 break;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001540 case ds_1388:
1541 ds1307->offset = 1; /* Seconds starts at 1 */
1542 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001543 case mcp794xx:
1544 rtc_ops = &mcp794xx_rtc_ops;
David Lowe80663602017-04-22 18:28:00 +01001545 if (chip->alarm && (ds1307->irq > 0 ||
1546 ds1307_can_wakeup_device)) {
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001547 irq_handler = mcp794xx_irq;
Simon Guinot1d1945d2014-04-03 14:49:55 -07001548 want_irq = true;
1549 }
1550 break;
David Brownell045e0e82007-07-17 04:04:55 -07001551 default:
1552 break;
1553 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001554
1555read_rtc:
1556 /* read RTC registers */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001557 err = regmap_bulk_read(ds1307->regmap, ds1307->offset, buf, 8);
1558 if (err) {
1559 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001560 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001561 }
1562
David Anders40ce9722012-03-23 15:02:37 -07001563 /*
1564 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001565 * specify the extra bits as must-be-zero, but there are
1566 * still a few values that are clearly out-of-range.
1567 */
1568 tmp = ds1307->regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001569 switch (ds1307->type) {
1570 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001571 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001572 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001573 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001574 if (tmp & DS1307_BIT_CH) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001575 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1576 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001577 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001578 }
David Brownell045e0e82007-07-17 04:04:55 -07001579 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001580 case ds_1338:
1581 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001582 if (tmp & DS1307_BIT_CH)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001583 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001584
1585 /* oscillator fault? clear flag, and warn */
1586 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001587 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
1588 ds1307->regs[DS1307_REG_CONTROL] &
1589 ~DS1338_BIT_OSF);
1590 dev_warn(ds1307->dev, "SET TIME!\n");
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001591 goto read_rtc;
1592 }
David Brownell045e0e82007-07-17 04:04:55 -07001593 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001594 case ds_1340:
1595 /* clock halted? turn it on, so clock can tick. */
1596 if (tmp & DS1340_BIT_nEOSC)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001597 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
frederic Rodofcd8db02008-02-06 01:38:55 -08001598
Heiner Kallweit11e58902017-03-10 18:52:34 +01001599 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1600 if (err) {
1601 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001602 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001603 }
1604
1605 /* oscillator fault? clear flag, and warn */
1606 if (tmp & DS1340_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001607 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1608 dev_warn(ds1307->dev, "SET TIME!\n");
frederic Rodofcd8db02008-02-06 01:38:55 -08001609 }
1610 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001611 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001612 /* make sure that the backup battery is enabled */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001613 if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001614 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
1615 ds1307->regs[DS1307_REG_WDAY] |
1616 MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001617 }
1618
1619 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001620 if (!(tmp & MCP794XX_BIT_ST)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001621 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1622 MCP794XX_BIT_ST);
1623 dev_warn(ds1307->dev, "SET TIME!\n");
David Anders43fcb812011-11-02 13:37:53 -07001624 goto read_rtc;
1625 }
1626
1627 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001628 default:
David Brownell045e0e82007-07-17 04:04:55 -07001629 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001630 }
David Brownell045e0e82007-07-17 04:04:55 -07001631
David Brownell1abb0dc2006-06-25 05:48:17 -07001632 tmp = ds1307->regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001633 switch (ds1307->type) {
1634 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001635 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001636 case m41t00:
David Anders40ce9722012-03-23 15:02:37 -07001637 /*
1638 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001639 * systems that will run through year 2100.
1640 */
1641 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001642 case rx_8025:
1643 break;
David Brownellc065f352007-07-17 04:05:10 -07001644 default:
1645 if (!(tmp & DS1307_BIT_12HR))
1646 break;
1647
David Anders40ce9722012-03-23 15:02:37 -07001648 /*
1649 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001650 * take note...
1651 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001652 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001653 if (tmp == 12)
1654 tmp = 0;
1655 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1656 tmp += 12;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001657 regmap_write(ds1307->regmap, ds1307->offset + DS1307_REG_HOUR,
1658 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001659 }
1660
Keerthye29385f2016-06-01 16:19:07 +05301661 /*
1662 * Some IPs have weekday reset value = 0x1 which might not correct
1663 * hence compute the wday using the current date/month/year values
1664 */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001665 ds1307_get_time(ds1307->dev, &tm);
Keerthye29385f2016-06-01 16:19:07 +05301666 wday = tm.tm_wday;
1667 timestamp = rtc_tm_to_time64(&tm);
1668 rtc_time64_to_tm(timestamp, &tm);
1669
1670 /*
1671 * Check if reset wday is different from the computed wday
1672 * If different then set the wday which we computed using
1673 * timestamp
1674 */
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001675 if (wday != tm.tm_wday)
1676 regmap_update_bits(ds1307->regmap, MCP794XX_REG_WEEKDAY,
1677 MCP794XX_REG_WEEKDAY_WDAY_MASK,
1678 tm.tm_wday + 1);
Keerthye29385f2016-06-01 16:19:07 +05301679
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001680 if (want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001681 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001682 set_bit(HAS_ALARM, &ds1307->flags);
1683 }
Heiner Kallweit11e58902017-03-10 18:52:34 +01001684 ds1307->rtc = devm_rtc_device_register(ds1307->dev, ds1307->name,
Simon Guinot1d1945d2014-04-03 14:49:55 -07001685 rtc_ops, THIS_MODULE);
David Brownell1abb0dc2006-06-25 05:48:17 -07001686 if (IS_ERR(ds1307->rtc)) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001687 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001688 }
1689
Heiner Kallweit11e58902017-03-10 18:52:34 +01001690 if (ds1307_can_wakeup_device && ds1307->irq <= 0) {
Michael Lange8bc2a402016-01-21 18:10:16 +01001691 /* Disable request for an IRQ */
1692 want_irq = false;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001693 dev_info(ds1307->dev,
1694 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001695 /* We cannot support UIE mode if we do not have an IRQ line */
1696 ds1307->rtc->uie_unsupported = 1;
1697 }
1698
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001699 if (want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001700 err = devm_request_threaded_irq(ds1307->dev,
1701 ds1307->irq, NULL, irq_handler,
Nishanth Menonc5983192015-06-23 11:15:11 -05001702 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001703 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001704 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001705 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001706 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001707 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001708 dev_err(ds1307->dev, "unable to request IRQ!\n");
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001709 } else
Heiner Kallweit11e58902017-03-10 18:52:34 +01001710 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001711 }
1712
Austin Boyle9eab0a72012-03-23 15:02:38 -07001713 if (chip->nvram_size) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001714
Heiner Kallweit11e58902017-03-10 18:52:34 +01001715 ds1307->nvram = devm_kzalloc(ds1307->dev,
Jingoo Hanedca66d2013-07-03 15:07:05 -07001716 sizeof(struct bin_attribute),
1717 GFP_KERNEL);
Austin Boyle9eab0a72012-03-23 15:02:38 -07001718 if (!ds1307->nvram) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001719 dev_err(ds1307->dev,
1720 "cannot allocate memory for nvram sysfs\n");
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001721 } else {
1722
1723 ds1307->nvram->attr.name = "nvram";
1724 ds1307->nvram->attr.mode = S_IRUGO | S_IWUSR;
1725
1726 sysfs_bin_attr_init(ds1307->nvram);
1727
1728 ds1307->nvram->read = ds1307_nvram_read;
1729 ds1307->nvram->write = ds1307_nvram_write;
1730 ds1307->nvram->size = chip->nvram_size;
1731 ds1307->nvram_offset = chip->nvram_offset;
1732
Heiner Kallweit11e58902017-03-10 18:52:34 +01001733 err = sysfs_create_bin_file(&ds1307->dev->kobj,
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001734 ds1307->nvram);
1735 if (err) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001736 dev_err(ds1307->dev,
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001737 "unable to create sysfs file: %s\n",
1738 ds1307->nvram->attr.name);
1739 } else {
1740 set_bit(HAS_NVRAM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001741 dev_info(ds1307->dev, "%zu bytes nvram\n",
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001742 ds1307->nvram->size);
1743 }
David Brownell682d73f2007-11-14 16:58:32 -08001744 }
1745 }
1746
Akinobu Mita445c0202016-01-25 00:22:16 +09001747 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001748 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001749
David Brownell1abb0dc2006-06-25 05:48:17 -07001750 return 0;
1751
Jingoo Hanedca66d2013-07-03 15:07:05 -07001752exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001753 return err;
1754}
1755
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001756static int ds1307_remove(struct i2c_client *client)
David Brownell1abb0dc2006-06-25 05:48:17 -07001757{
David Anders40ce9722012-03-23 15:02:37 -07001758 struct ds1307 *ds1307 = i2c_get_clientdata(client);
David Brownell1abb0dc2006-06-25 05:48:17 -07001759
Jingoo Hanedca66d2013-07-03 15:07:05 -07001760 if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
Heiner Kallweit11e58902017-03-10 18:52:34 +01001761 sysfs_remove_bin_file(&ds1307->dev->kobj, ds1307->nvram);
David Brownell682d73f2007-11-14 16:58:32 -08001762
David Brownell1abb0dc2006-06-25 05:48:17 -07001763 return 0;
1764}
1765
1766static struct i2c_driver ds1307_driver = {
1767 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001768 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001769 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001770 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001771 },
David Brownellc065f352007-07-17 04:05:10 -07001772 .probe = ds1307_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001773 .remove = ds1307_remove,
Jean Delvare3760f732008-04-29 23:11:40 +02001774 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001775};
1776
Axel Lin0abc9202012-03-23 15:02:31 -07001777module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001778
1779MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1780MODULE_LICENSE("GPL");