blob: 4e31036ee2596dec93accd26f627c5b95591ae9f [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
David Brownell1abb0dc2006-06-25 05:48:17 -070014#include <linux/bcd.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050015#include <linux/i2c.h>
16#include <linux/init.h>
17#include <linux/module.h>
Wolfram Sangeb86c302012-05-29 15:07:38 -070018#include <linux/rtc/ds1307.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050019#include <linux/rtc.h>
20#include <linux/slab.h>
21#include <linux/string.h>
Akinobu Mita445c0202016-01-25 00:22:16 +090022#include <linux/hwmon.h>
23#include <linux/hwmon-sysfs.h>
Akinobu Mita6c6ff142016-01-31 23:10:10 +090024#include <linux/clk-provider.h>
David Brownell1abb0dc2006-06-25 05:48:17 -070025
David Anders40ce9722012-03-23 15:02:37 -070026/*
27 * We can't determine type by probing, but if we expect pre-Linux code
David Brownell1abb0dc2006-06-25 05:48:17 -070028 * to have set the chip up as a clock (turning on the oscillator and
29 * setting the date and time), Linux can ignore the non-clock features.
30 * That's a natural job for a factory or repair bench.
David Brownell1abb0dc2006-06-25 05:48:17 -070031 */
32enum ds_type {
David Brownell045e0e82007-07-17 04:04:55 -070033 ds_1307,
34 ds_1337,
35 ds_1338,
36 ds_1339,
37 ds_1340,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -070038 ds_1388,
Wolfram Sang97f902b2009-06-17 16:26:10 -070039 ds_3231,
David Brownell045e0e82007-07-17 04:04:55 -070040 m41t00,
Tomas Novotnyf4199f82014-12-10 15:53:57 -080041 mcp794xx,
Matthias Fuchsa2166852009-03-31 15:24:58 -070042 rx_8025,
Wolfram Sang32d322b2012-03-23 15:02:36 -070043 last_ds_type /* always last */
David Anders40ce9722012-03-23 15:02:37 -070044 /* rs5c372 too? different address... */
David Brownell1abb0dc2006-06-25 05:48:17 -070045};
46
David Brownell1abb0dc2006-06-25 05:48:17 -070047
48/* RTC registers don't differ much, except for the century flag */
49#define DS1307_REG_SECS 0x00 /* 00-59 */
50# define DS1307_BIT_CH 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070051# define DS1340_BIT_nEOSC 0x80
Tomas Novotnyf4199f82014-12-10 15:53:57 -080052# define MCP794XX_BIT_ST 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070053#define DS1307_REG_MIN 0x01 /* 00-59 */
54#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
David Brownellc065f352007-07-17 04:05:10 -070055# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
56# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
David Brownell1abb0dc2006-06-25 05:48:17 -070057# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
58# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
59#define DS1307_REG_WDAY 0x03 /* 01-07 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -080060# define MCP794XX_BIT_VBATEN 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -070061#define DS1307_REG_MDAY 0x04 /* 01-31 */
62#define DS1307_REG_MONTH 0x05 /* 01-12 */
63# define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
64#define DS1307_REG_YEAR 0x06 /* 00-99 */
65
David Anders40ce9722012-03-23 15:02:37 -070066/*
67 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
David Brownell045e0e82007-07-17 04:04:55 -070068 * start at 7, and they differ a LOT. Only control and status matter for
69 * basic RTC date and time functionality; be careful using them.
David Brownell1abb0dc2006-06-25 05:48:17 -070070 */
David Brownell045e0e82007-07-17 04:04:55 -070071#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
David Brownell1abb0dc2006-06-25 05:48:17 -070072# define DS1307_BIT_OUT 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070073# define DS1338_BIT_OSF 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070074# define DS1307_BIT_SQWE 0x10
75# define DS1307_BIT_RS1 0x02
76# define DS1307_BIT_RS0 0x01
77#define DS1337_REG_CONTROL 0x0e
78# define DS1337_BIT_nEOSC 0x80
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070079# define DS1339_BIT_BBSQI 0x20
Wolfram Sang97f902b2009-06-17 16:26:10 -070080# define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
David Brownell1abb0dc2006-06-25 05:48:17 -070081# define DS1337_BIT_RS2 0x10
82# define DS1337_BIT_RS1 0x08
83# define DS1337_BIT_INTCN 0x04
84# define DS1337_BIT_A2IE 0x02
85# define DS1337_BIT_A1IE 0x01
David Brownell045e0e82007-07-17 04:04:55 -070086#define DS1340_REG_CONTROL 0x07
87# define DS1340_BIT_OUT 0x80
88# define DS1340_BIT_FT 0x40
89# define DS1340_BIT_CALIB_SIGN 0x20
90# define DS1340_M_CALIBRATION 0x1f
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070091#define DS1340_REG_FLAG 0x09
92# define DS1340_BIT_OSF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070093#define DS1337_REG_STATUS 0x0f
94# define DS1337_BIT_OSF 0x80
Akinobu Mita6c6ff142016-01-31 23:10:10 +090095# define DS3231_BIT_EN32KHZ 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -070096# define DS1337_BIT_A2I 0x02
97# define DS1337_BIT_A1I 0x01
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070098#define DS1339_REG_ALARM1_SECS 0x07
Wolfram Sangeb86c302012-05-29 15:07:38 -070099
100#define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
David Brownell1abb0dc2006-06-25 05:48:17 -0700101
Matthias Fuchsa2166852009-03-31 15:24:58 -0700102#define RX8025_REG_CTRL1 0x0e
103# define RX8025_BIT_2412 0x20
104#define RX8025_REG_CTRL2 0x0f
105# define RX8025_BIT_PON 0x10
106# define RX8025_BIT_VDET 0x40
107# define RX8025_BIT_XST 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -0700108
109
110struct ds1307 {
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700111 u8 offset; /* register's offset */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700112 u8 regs[11];
Austin Boyle9eab0a72012-03-23 15:02:38 -0700113 u16 nvram_offset;
114 struct bin_attribute *nvram;
David Brownell1abb0dc2006-06-25 05:48:17 -0700115 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700116 unsigned long flags;
117#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
118#define HAS_ALARM 1 /* bit 1 == irq claimed */
David Brownell045e0e82007-07-17 04:04:55 -0700119 struct i2c_client *client;
David Brownell1abb0dc2006-06-25 05:48:17 -0700120 struct rtc_device *rtc;
Jean Delvare0cc43a12011-01-10 22:11:23 +0100121 s32 (*read_block_data)(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700122 u8 length, u8 *values);
Jean Delvare0cc43a12011-01-10 22:11:23 +0100123 s32 (*write_block_data)(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700124 u8 length, const u8 *values);
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900125#ifdef CONFIG_COMMON_CLK
126 struct clk_hw clks[2];
127#endif
David Brownell1abb0dc2006-06-25 05:48:17 -0700128};
129
David Brownell045e0e82007-07-17 04:04:55 -0700130struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700131 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700132 u16 nvram_offset;
133 u16 nvram_size;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700134 u16 trickle_charger_reg;
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700135 u8 trickle_charger_setup;
136 u8 (*do_trickle_setup)(struct i2c_client *, uint32_t, bool);
David Brownell045e0e82007-07-17 04:04:55 -0700137};
138
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700139static u8 do_trickle_setup_ds1339(struct i2c_client *,
140 uint32_t ohms, bool diode);
141
142static struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700143 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700144 .nvram_offset = 8,
145 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700146 },
147 [ds_1337] = {
148 .alarm = 1,
149 },
150 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700151 .nvram_offset = 8,
152 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700153 },
154 [ds_1339] = {
155 .alarm = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700156 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700157 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700158 },
159 [ds_1340] = {
160 .trickle_charger_reg = 0x08,
161 },
162 [ds_1388] = {
163 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700164 },
165 [ds_3231] = {
166 .alarm = 1,
167 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800168 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700169 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700170 /* this is battery backed SRAM */
171 .nvram_offset = 0x20,
172 .nvram_size = 0x40,
173 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700174};
David Brownell045e0e82007-07-17 04:04:55 -0700175
Jean Delvare3760f732008-04-29 23:11:40 +0200176static const struct i2c_device_id ds1307_id[] = {
177 { "ds1307", ds_1307 },
178 { "ds1337", ds_1337 },
179 { "ds1338", ds_1338 },
180 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700181 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200182 { "ds1340", ds_1340 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700183 { "ds3231", ds_3231 },
Jean Delvare3760f732008-04-29 23:11:40 +0200184 { "m41t00", m41t00 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800185 { "mcp7940x", mcp794xx },
186 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700187 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700188 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200189 { "isl12057", ds_1337 },
Jean Delvare3760f732008-04-29 23:11:40 +0200190 { }
191};
192MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700193
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700194/*----------------------------------------------------------------------*/
195
Ed Swierk30e7b032009-03-31 15:24:56 -0700196#define BLOCK_DATA_MAX_TRIES 10
197
Jean Delvare0cc43a12011-01-10 22:11:23 +0100198static s32 ds1307_read_block_data_once(const struct i2c_client *client,
199 u8 command, u8 length, u8 *values)
Ed Swierk30e7b032009-03-31 15:24:56 -0700200{
201 s32 i, data;
202
203 for (i = 0; i < length; i++) {
204 data = i2c_smbus_read_byte_data(client, command + i);
205 if (data < 0)
206 return data;
207 values[i] = data;
208 }
209 return i;
210}
211
Jean Delvare0cc43a12011-01-10 22:11:23 +0100212static s32 ds1307_read_block_data(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700213 u8 length, u8 *values)
214{
Bertrand Achardbc48b902013-04-29 16:19:26 -0700215 u8 oldvalues[255];
Ed Swierk30e7b032009-03-31 15:24:56 -0700216 s32 ret;
217 int tries = 0;
218
219 dev_dbg(&client->dev, "ds1307_read_block_data (length=%d)\n", length);
220 ret = ds1307_read_block_data_once(client, command, length, values);
221 if (ret < 0)
222 return ret;
223 do {
224 if (++tries > BLOCK_DATA_MAX_TRIES) {
225 dev_err(&client->dev,
226 "ds1307_read_block_data failed\n");
227 return -EIO;
228 }
229 memcpy(oldvalues, values, length);
230 ret = ds1307_read_block_data_once(client, command, length,
231 values);
232 if (ret < 0)
233 return ret;
234 } while (memcmp(oldvalues, values, length));
235 return length;
236}
237
Jean Delvare0cc43a12011-01-10 22:11:23 +0100238static s32 ds1307_write_block_data(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700239 u8 length, const u8 *values)
240{
Bertrand Achardbc48b902013-04-29 16:19:26 -0700241 u8 currvalues[255];
Ed Swierk30e7b032009-03-31 15:24:56 -0700242 int tries = 0;
243
244 dev_dbg(&client->dev, "ds1307_write_block_data (length=%d)\n", length);
245 do {
246 s32 i, ret;
247
248 if (++tries > BLOCK_DATA_MAX_TRIES) {
249 dev_err(&client->dev,
250 "ds1307_write_block_data failed\n");
251 return -EIO;
252 }
253 for (i = 0; i < length; i++) {
254 ret = i2c_smbus_write_byte_data(client, command + i,
255 values[i]);
256 if (ret < 0)
257 return ret;
258 }
259 ret = ds1307_read_block_data_once(client, command, length,
260 currvalues);
261 if (ret < 0)
262 return ret;
263 } while (memcmp(currvalues, values, length));
264 return length;
265}
266
267/*----------------------------------------------------------------------*/
268
Bertrand Achardbc48b902013-04-29 16:19:26 -0700269/* These RTC devices are not designed to be connected to a SMbus adapter.
270 SMbus limits block operations length to 32 bytes, whereas it's not
271 limited on I2C buses. As a result, accesses may exceed 32 bytes;
272 in that case, split them into smaller blocks */
273
274static s32 ds1307_native_smbus_write_block_data(const struct i2c_client *client,
275 u8 command, u8 length, const u8 *values)
276{
277 u8 suboffset = 0;
278
Nicolas Boullis1d879512016-04-03 00:10:37 +0200279 if (length <= I2C_SMBUS_BLOCK_MAX) {
280 s32 retval = i2c_smbus_write_i2c_block_data(client,
Bertrand Achardbc48b902013-04-29 16:19:26 -0700281 command, length, values);
Nicolas Boullis1d879512016-04-03 00:10:37 +0200282 if (retval < 0)
283 return retval;
284 return length;
285 }
Bertrand Achardbc48b902013-04-29 16:19:26 -0700286
287 while (suboffset < length) {
288 s32 retval = i2c_smbus_write_i2c_block_data(client,
289 command + suboffset,
290 min(I2C_SMBUS_BLOCK_MAX, length - suboffset),
291 values + suboffset);
292 if (retval < 0)
293 return retval;
294
295 suboffset += I2C_SMBUS_BLOCK_MAX;
296 }
297 return length;
298}
299
300static s32 ds1307_native_smbus_read_block_data(const struct i2c_client *client,
301 u8 command, u8 length, u8 *values)
302{
303 u8 suboffset = 0;
304
305 if (length <= I2C_SMBUS_BLOCK_MAX)
306 return i2c_smbus_read_i2c_block_data(client,
307 command, length, values);
308
309 while (suboffset < length) {
310 s32 retval = i2c_smbus_read_i2c_block_data(client,
311 command + suboffset,
312 min(I2C_SMBUS_BLOCK_MAX, length - suboffset),
313 values + suboffset);
314 if (retval < 0)
315 return retval;
316
317 suboffset += I2C_SMBUS_BLOCK_MAX;
318 }
319 return length;
320}
321
322/*----------------------------------------------------------------------*/
323
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700324/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700325 * The ds1337 and ds1339 both have two alarms, but we only use the first
326 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
327 * signal; ds1339 chips have only one alarm signal.
328 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500329static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700330{
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500331 struct i2c_client *client = dev_id;
332 struct ds1307 *ds1307 = i2c_get_clientdata(client);
333 struct mutex *lock = &ds1307->rtc->ops_lock;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700334 int stat, control;
335
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700336 mutex_lock(lock);
337 stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
338 if (stat < 0)
339 goto out;
340
341 if (stat & DS1337_BIT_A1I) {
342 stat &= ~DS1337_BIT_A1I;
343 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, stat);
344
345 control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
346 if (control < 0)
347 goto out;
348
349 control &= ~DS1337_BIT_A1IE;
350 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
351
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700352 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700353 }
354
355out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700356 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700357
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700358 return IRQ_HANDLED;
359}
360
361/*----------------------------------------------------------------------*/
362
David Brownell1abb0dc2006-06-25 05:48:17 -0700363static int ds1307_get_time(struct device *dev, struct rtc_time *t)
364{
365 struct ds1307 *ds1307 = dev_get_drvdata(dev);
366 int tmp;
367
David Brownell045e0e82007-07-17 04:04:55 -0700368 /* read the RTC date and time registers all at once */
Ed Swierk30e7b032009-03-31 15:24:56 -0700369 tmp = ds1307->read_block_data(ds1307->client,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700370 ds1307->offset, 7, ds1307->regs);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800371 if (tmp != 7) {
David Brownell1abb0dc2006-06-25 05:48:17 -0700372 dev_err(dev, "%s error %d\n", "read", tmp);
373 return -EIO;
374 }
375
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800376 dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700377
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700378 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
379 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700380 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700381 t->tm_hour = bcd2bin(tmp);
382 t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
383 t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700384 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700385 t->tm_mon = bcd2bin(tmp) - 1;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700386 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700387
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200388#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
389 switch (ds1307->type) {
390 case ds_1337:
391 case ds_1339:
392 case ds_3231:
393 if (ds1307->regs[DS1307_REG_MONTH] & DS1337_BIT_CENTURY)
394 t->tm_year += 100;
395 break;
396 case ds_1340:
397 if (ds1307->regs[DS1307_REG_HOUR] & DS1340_BIT_CENTURY)
398 t->tm_year += 100;
399 break;
400 default:
401 break;
402 }
403#endif
404
David Brownell1abb0dc2006-06-25 05:48:17 -0700405 dev_dbg(dev, "%s secs=%d, mins=%d, "
406 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
407 "read", t->tm_sec, t->tm_min,
408 t->tm_hour, t->tm_mday,
409 t->tm_mon, t->tm_year, t->tm_wday);
410
David Brownell045e0e82007-07-17 04:04:55 -0700411 /* initial clock setting can be undefined */
412 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700413}
414
415static int ds1307_set_time(struct device *dev, struct rtc_time *t)
416{
417 struct ds1307 *ds1307 = dev_get_drvdata(dev);
418 int result;
419 int tmp;
420 u8 *buf = ds1307->regs;
421
422 dev_dbg(dev, "%s secs=%d, mins=%d, "
423 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400424 "write", t->tm_sec, t->tm_min,
425 t->tm_hour, t->tm_mday,
426 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700427
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200428#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
429 if (t->tm_year < 100)
430 return -EINVAL;
431
432 switch (ds1307->type) {
433 case ds_1337:
434 case ds_1339:
435 case ds_3231:
436 case ds_1340:
437 if (t->tm_year > 299)
438 return -EINVAL;
439 default:
440 if (t->tm_year > 199)
441 return -EINVAL;
442 break;
443 }
444#else
445 if (t->tm_year < 100 || t->tm_year > 199)
446 return -EINVAL;
447#endif
448
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700449 buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
450 buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
451 buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
452 buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
453 buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
454 buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700455
456 /* assume 20YY not 19YY */
457 tmp = t->tm_year - 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700458 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700459
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700460 switch (ds1307->type) {
461 case ds_1337:
462 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -0700463 case ds_3231:
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200464 if (t->tm_year > 199)
465 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700466 break;
467 case ds_1340:
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200468 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN;
469 if (t->tm_year > 199)
470 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700471 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800472 case mcp794xx:
David Anders40ce9722012-03-23 15:02:37 -0700473 /*
474 * these bits were cleared when preparing the date/time
475 * values and need to be set again before writing the
476 * buffer out to the device.
477 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800478 buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
479 buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
David Anders43fcb812011-11-02 13:37:53 -0700480 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700481 default:
482 break;
483 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700484
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800485 dev_dbg(dev, "%s: %7ph\n", "write", buf);
David Brownell1abb0dc2006-06-25 05:48:17 -0700486
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700487 result = ds1307->write_block_data(ds1307->client,
488 ds1307->offset, 7, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800489 if (result < 0) {
490 dev_err(dev, "%s error %d\n", "write", result);
491 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700492 }
493 return 0;
494}
495
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800496static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700497{
498 struct i2c_client *client = to_i2c_client(dev);
499 struct ds1307 *ds1307 = i2c_get_clientdata(client);
500 int ret;
501
502 if (!test_bit(HAS_ALARM, &ds1307->flags))
503 return -EINVAL;
504
505 /* read all ALARM1, ALARM2, and status registers at once */
Ed Swierk30e7b032009-03-31 15:24:56 -0700506 ret = ds1307->read_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800507 DS1339_REG_ALARM1_SECS, 9, ds1307->regs);
508 if (ret != 9) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700509 dev_err(dev, "%s error %d\n", "alarm read", ret);
510 return -EIO;
511 }
512
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100513 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
514 &ds1307->regs[0], &ds1307->regs[4], &ds1307->regs[7]);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700515
David Anders40ce9722012-03-23 15:02:37 -0700516 /*
517 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700518 * and that all four fields are checked matches
519 */
520 t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
521 t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
522 t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
523 t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700524
525 /* ... and status */
526 t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
527 t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
528
529 dev_dbg(dev, "%s secs=%d, mins=%d, "
530 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
531 "alarm read", t->time.tm_sec, t->time.tm_min,
532 t->time.tm_hour, t->time.tm_mday,
533 t->enabled, t->pending);
534
535 return 0;
536}
537
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800538static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700539{
David Anders40ce9722012-03-23 15:02:37 -0700540 struct i2c_client *client = to_i2c_client(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700541 struct ds1307 *ds1307 = i2c_get_clientdata(client);
542 unsigned char *buf = ds1307->regs;
543 u8 control, status;
544 int ret;
545
546 if (!test_bit(HAS_ALARM, &ds1307->flags))
547 return -EINVAL;
548
549 dev_dbg(dev, "%s secs=%d, mins=%d, "
550 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
551 "alarm set", t->time.tm_sec, t->time.tm_min,
552 t->time.tm_hour, t->time.tm_mday,
553 t->enabled, t->pending);
554
555 /* read current status of both alarms and the chip */
Ed Swierk30e7b032009-03-31 15:24:56 -0700556 ret = ds1307->read_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800557 DS1339_REG_ALARM1_SECS, 9, buf);
558 if (ret != 9) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700559 dev_err(dev, "%s error %d\n", "alarm write", ret);
560 return -EIO;
561 }
562 control = ds1307->regs[7];
563 status = ds1307->regs[8];
564
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100565 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
566 &ds1307->regs[0], &ds1307->regs[4], control, status);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700567
568 /* set ALARM1, using 24 hour and day-of-month modes */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700569 buf[0] = bin2bcd(t->time.tm_sec);
570 buf[1] = bin2bcd(t->time.tm_min);
571 buf[2] = bin2bcd(t->time.tm_hour);
572 buf[3] = bin2bcd(t->time.tm_mday);
573
574 /* set ALARM2 to non-garbage */
575 buf[4] = 0;
576 buf[5] = 0;
577 buf[6] = 0;
578
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200579 /* disable alarms */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700580 buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700581 buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
582
Ed Swierk30e7b032009-03-31 15:24:56 -0700583 ret = ds1307->write_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800584 DS1339_REG_ALARM1_SECS, 9, buf);
585 if (ret < 0) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700586 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800587 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700588 }
589
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200590 /* optionally enable ALARM1 */
591 if (t->enabled) {
592 dev_dbg(dev, "alarm IRQ armed\n");
593 buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
594 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, buf[7]);
595 }
596
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700597 return 0;
598}
599
John Stultz16380c12011-02-02 17:02:41 -0800600static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700601{
602 struct i2c_client *client = to_i2c_client(dev);
603 struct ds1307 *ds1307 = i2c_get_clientdata(client);
604 int ret;
605
John Stultz16380c12011-02-02 17:02:41 -0800606 if (!test_bit(HAS_ALARM, &ds1307->flags))
607 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700608
John Stultz16380c12011-02-02 17:02:41 -0800609 ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
610 if (ret < 0)
611 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700612
John Stultz16380c12011-02-02 17:02:41 -0800613 if (enabled)
614 ret |= DS1337_BIT_A1IE;
615 else
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700616 ret &= ~DS1337_BIT_A1IE;
617
John Stultz16380c12011-02-02 17:02:41 -0800618 ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, ret);
619 if (ret < 0)
620 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700621
622 return 0;
623}
624
David Brownellff8371a2006-09-30 23:28:17 -0700625static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700626 .read_time = ds1307_get_time,
627 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800628 .read_alarm = ds1337_read_alarm,
629 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800630 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700631};
632
David Brownell682d73f2007-11-14 16:58:32 -0800633/*----------------------------------------------------------------------*/
634
Simon Guinot1d1945d2014-04-03 14:49:55 -0700635/*
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800636 * Alarm support for mcp794xx devices.
Simon Guinot1d1945d2014-04-03 14:49:55 -0700637 */
638
Keerthye29385f2016-06-01 16:19:07 +0530639#define MCP794XX_REG_WEEKDAY 0x3
640#define MCP794XX_REG_WEEKDAY_WDAY_MASK 0x7
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800641#define MCP794XX_REG_CONTROL 0x07
642# define MCP794XX_BIT_ALM0_EN 0x10
643# define MCP794XX_BIT_ALM1_EN 0x20
644#define MCP794XX_REG_ALARM0_BASE 0x0a
645#define MCP794XX_REG_ALARM0_CTRL 0x0d
646#define MCP794XX_REG_ALARM1_BASE 0x11
647#define MCP794XX_REG_ALARM1_CTRL 0x14
648# define MCP794XX_BIT_ALMX_IF (1 << 3)
649# define MCP794XX_BIT_ALMX_C0 (1 << 4)
650# define MCP794XX_BIT_ALMX_C1 (1 << 5)
651# define MCP794XX_BIT_ALMX_C2 (1 << 6)
652# define MCP794XX_BIT_ALMX_POL (1 << 7)
653# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
654 MCP794XX_BIT_ALMX_C1 | \
655 MCP794XX_BIT_ALMX_C2)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700656
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500657static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700658{
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500659 struct i2c_client *client = dev_id;
660 struct ds1307 *ds1307 = i2c_get_clientdata(client);
661 struct mutex *lock = &ds1307->rtc->ops_lock;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700662 int reg, ret;
663
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500664 mutex_lock(lock);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700665
666 /* Check and clear alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800667 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_ALARM0_CTRL);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700668 if (reg < 0)
669 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800670 if (!(reg & MCP794XX_BIT_ALMX_IF))
Simon Guinot1d1945d2014-04-03 14:49:55 -0700671 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800672 reg &= ~MCP794XX_BIT_ALMX_IF;
673 ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_ALARM0_CTRL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700674 if (ret < 0)
675 goto out;
676
677 /* Disable alarm 0. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800678 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700679 if (reg < 0)
680 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800681 reg &= ~MCP794XX_BIT_ALM0_EN;
682 ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700683 if (ret < 0)
684 goto out;
685
686 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
687
688out:
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500689 mutex_unlock(lock);
690
691 return IRQ_HANDLED;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700692}
693
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800694static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700695{
696 struct i2c_client *client = to_i2c_client(dev);
697 struct ds1307 *ds1307 = i2c_get_clientdata(client);
698 u8 *regs = ds1307->regs;
699 int ret;
700
701 if (!test_bit(HAS_ALARM, &ds1307->flags))
702 return -EINVAL;
703
704 /* Read control and alarm 0 registers. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800705 ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700706 if (ret < 0)
707 return ret;
708
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800709 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700710
711 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
712 t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f);
713 t->time.tm_min = bcd2bin(ds1307->regs[4] & 0x7f);
714 t->time.tm_hour = bcd2bin(ds1307->regs[5] & 0x3f);
715 t->time.tm_wday = bcd2bin(ds1307->regs[6] & 0x7) - 1;
716 t->time.tm_mday = bcd2bin(ds1307->regs[7] & 0x3f);
717 t->time.tm_mon = bcd2bin(ds1307->regs[8] & 0x1f) - 1;
718 t->time.tm_year = -1;
719 t->time.tm_yday = -1;
720 t->time.tm_isdst = -1;
721
722 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
723 "enabled=%d polarity=%d irq=%d match=%d\n", __func__,
724 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
725 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800726 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL),
727 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF),
728 (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700729
730 return 0;
731}
732
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800733static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700734{
735 struct i2c_client *client = to_i2c_client(dev);
736 struct ds1307 *ds1307 = i2c_get_clientdata(client);
737 unsigned char *regs = ds1307->regs;
738 int ret;
739
740 if (!test_bit(HAS_ALARM, &ds1307->flags))
741 return -EINVAL;
742
743 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
744 "enabled=%d pending=%d\n", __func__,
745 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
746 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
747 t->enabled, t->pending);
748
749 /* Read control and alarm 0 registers. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800750 ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700751 if (ret < 0)
752 return ret;
753
754 /* Set alarm 0, using 24-hour and day-of-month modes. */
755 regs[3] = bin2bcd(t->time.tm_sec);
756 regs[4] = bin2bcd(t->time.tm_min);
757 regs[5] = bin2bcd(t->time.tm_hour);
Tero Kristo62c8c202015-10-23 09:29:57 +0300758 regs[6] = bin2bcd(t->time.tm_wday + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700759 regs[7] = bin2bcd(t->time.tm_mday);
Tero Kristo62c8c202015-10-23 09:29:57 +0300760 regs[8] = bin2bcd(t->time.tm_mon + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700761
762 /* Clear the alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800763 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700764 /* Set alarm match: second, minute, hour, day, date, month. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800765 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
Nishanth Menone3edd672015-04-20 19:51:34 -0500766 /* Disable interrupt. We will not enable until completely programmed */
767 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700768
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800769 ret = ds1307->write_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700770 if (ret < 0)
771 return ret;
772
Nishanth Menone3edd672015-04-20 19:51:34 -0500773 if (!t->enabled)
774 return 0;
775 regs[0] |= MCP794XX_BIT_ALM0_EN;
776 return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, regs[0]);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700777}
778
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800779static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700780{
781 struct i2c_client *client = to_i2c_client(dev);
782 struct ds1307 *ds1307 = i2c_get_clientdata(client);
783 int reg;
784
785 if (!test_bit(HAS_ALARM, &ds1307->flags))
786 return -EINVAL;
787
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800788 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700789 if (reg < 0)
790 return reg;
791
792 if (enabled)
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800793 reg |= MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700794 else
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800795 reg &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700796
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800797 return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700798}
799
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800800static const struct rtc_class_ops mcp794xx_rtc_ops = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700801 .read_time = ds1307_get_time,
802 .set_time = ds1307_set_time,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800803 .read_alarm = mcp794xx_read_alarm,
804 .set_alarm = mcp794xx_set_alarm,
805 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
Simon Guinot1d1945d2014-04-03 14:49:55 -0700806};
807
808/*----------------------------------------------------------------------*/
809
David Brownell682d73f2007-11-14 16:58:32 -0800810static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700811ds1307_nvram_read(struct file *filp, struct kobject *kobj,
812 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800813 char *buf, loff_t off, size_t count)
814{
815 struct i2c_client *client;
816 struct ds1307 *ds1307;
David Brownell682d73f2007-11-14 16:58:32 -0800817 int result;
818
frederic Rodofcd8db02008-02-06 01:38:55 -0800819 client = kobj_to_i2c_client(kobj);
David Brownell682d73f2007-11-14 16:58:32 -0800820 ds1307 = i2c_get_clientdata(client);
821
Austin Boyle9eab0a72012-03-23 15:02:38 -0700822 result = ds1307->read_block_data(client, ds1307->nvram_offset + off,
823 count, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800824 if (result < 0)
David Brownell682d73f2007-11-14 16:58:32 -0800825 dev_err(&client->dev, "%s error %d\n", "nvram read", result);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800826 return result;
David Brownell682d73f2007-11-14 16:58:32 -0800827}
828
829static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700830ds1307_nvram_write(struct file *filp, struct kobject *kobj,
831 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800832 char *buf, loff_t off, size_t count)
833{
834 struct i2c_client *client;
Ed Swierk30e7b032009-03-31 15:24:56 -0700835 struct ds1307 *ds1307;
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800836 int result;
David Brownell682d73f2007-11-14 16:58:32 -0800837
frederic Rodofcd8db02008-02-06 01:38:55 -0800838 client = kobj_to_i2c_client(kobj);
Ed Swierk30e7b032009-03-31 15:24:56 -0700839 ds1307 = i2c_get_clientdata(client);
David Brownell682d73f2007-11-14 16:58:32 -0800840
Austin Boyle9eab0a72012-03-23 15:02:38 -0700841 result = ds1307->write_block_data(client, ds1307->nvram_offset + off,
842 count, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800843 if (result < 0) {
844 dev_err(&client->dev, "%s error %d\n", "nvram write", result);
845 return result;
846 }
847 return count;
David Brownell682d73f2007-11-14 16:58:32 -0800848}
849
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700850
David Brownell682d73f2007-11-14 16:58:32 -0800851/*----------------------------------------------------------------------*/
852
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700853static u8 do_trickle_setup_ds1339(struct i2c_client *client,
854 uint32_t ohms, bool diode)
855{
856 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
857 DS1307_TRICKLE_CHARGER_NO_DIODE;
858
859 switch (ohms) {
860 case 250:
861 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
862 break;
863 case 2000:
864 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
865 break;
866 case 4000:
867 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
868 break;
869 default:
870 dev_warn(&client->dev,
871 "Unsupported ohm value %u in dt\n", ohms);
872 return 0;
873 }
874 return setup;
875}
876
877static void ds1307_trickle_of_init(struct i2c_client *client,
878 struct chip_desc *chip)
879{
880 uint32_t ohms = 0;
881 bool diode = true;
882
883 if (!chip->do_trickle_setup)
884 goto out;
885 if (of_property_read_u32(client->dev.of_node, "trickle-resistor-ohms" , &ohms))
886 goto out;
887 if (of_property_read_bool(client->dev.of_node, "trickle-diode-disable"))
888 diode = false;
889 chip->trickle_charger_setup = chip->do_trickle_setup(client,
890 ohms, diode);
891out:
892 return;
893}
894
Akinobu Mita445c0202016-01-25 00:22:16 +0900895/*----------------------------------------------------------------------*/
896
897#ifdef CONFIG_RTC_DRV_DS1307_HWMON
898
899/*
900 * Temperature sensor support for ds3231 devices.
901 */
902
903#define DS3231_REG_TEMPERATURE 0x11
904
905/*
906 * A user-initiated temperature conversion is not started by this function,
907 * so the temperature is updated once every 64 seconds.
908 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +0900909static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +0900910{
911 struct ds1307 *ds1307 = dev_get_drvdata(dev);
912 u8 temp_buf[2];
913 s16 temp;
914 int ret;
915
916 ret = ds1307->read_block_data(ds1307->client, DS3231_REG_TEMPERATURE,
917 sizeof(temp_buf), temp_buf);
918 if (ret < 0)
919 return ret;
920 if (ret != sizeof(temp_buf))
921 return -EIO;
922
923 /*
924 * Temperature is represented as a 10-bit code with a resolution of
925 * 0.25 degree celsius and encoded in two's complement format.
926 */
927 temp = (temp_buf[0] << 8) | temp_buf[1];
928 temp >>= 6;
929 *mC = temp * 250;
930
931 return 0;
932}
933
934static ssize_t ds3231_hwmon_show_temp(struct device *dev,
935 struct device_attribute *attr, char *buf)
936{
937 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +0900938 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +0900939
940 ret = ds3231_hwmon_read_temp(dev, &temp);
941 if (ret)
942 return ret;
943
944 return sprintf(buf, "%d\n", temp);
945}
946static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ds3231_hwmon_show_temp,
947 NULL, 0);
948
949static struct attribute *ds3231_hwmon_attrs[] = {
950 &sensor_dev_attr_temp1_input.dev_attr.attr,
951 NULL,
952};
953ATTRIBUTE_GROUPS(ds3231_hwmon);
954
955static void ds1307_hwmon_register(struct ds1307 *ds1307)
956{
957 struct device *dev;
958
959 if (ds1307->type != ds_3231)
960 return;
961
962 dev = devm_hwmon_device_register_with_groups(&ds1307->client->dev,
963 ds1307->client->name,
964 ds1307, ds3231_hwmon_groups);
965 if (IS_ERR(dev)) {
966 dev_warn(&ds1307->client->dev,
967 "unable to register hwmon device %ld\n", PTR_ERR(dev));
968 }
969}
970
971#else
972
973static void ds1307_hwmon_register(struct ds1307 *ds1307)
974{
975}
976
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900977#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
978
979/*----------------------------------------------------------------------*/
980
981/*
982 * Square-wave output support for DS3231
983 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
984 */
985#ifdef CONFIG_COMMON_CLK
986
987enum {
988 DS3231_CLK_SQW = 0,
989 DS3231_CLK_32KHZ,
990};
991
992#define clk_sqw_to_ds1307(clk) \
993 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
994#define clk_32khz_to_ds1307(clk) \
995 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
996
997static int ds3231_clk_sqw_rates[] = {
998 1,
999 1024,
1000 4096,
1001 8192,
1002};
1003
1004static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1005{
1006 struct i2c_client *client = ds1307->client;
1007 struct mutex *lock = &ds1307->rtc->ops_lock;
1008 int control;
1009 int ret;
1010
1011 mutex_lock(lock);
1012
1013 control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
1014 if (control < 0) {
1015 ret = control;
1016 goto out;
1017 }
1018
1019 control &= ~mask;
1020 control |= value;
1021
1022 ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
1023out:
1024 mutex_unlock(lock);
1025
1026 return ret;
1027}
1028
1029static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1030 unsigned long parent_rate)
1031{
1032 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1033 int control;
1034 int rate_sel = 0;
1035
1036 control = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_CONTROL);
1037 if (control < 0)
1038 return control;
1039 if (control & DS1337_BIT_RS1)
1040 rate_sel += 1;
1041 if (control & DS1337_BIT_RS2)
1042 rate_sel += 2;
1043
1044 return ds3231_clk_sqw_rates[rate_sel];
1045}
1046
1047static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
1048 unsigned long *prate)
1049{
1050 int i;
1051
1052 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1053 if (ds3231_clk_sqw_rates[i] <= rate)
1054 return ds3231_clk_sqw_rates[i];
1055 }
1056
1057 return 0;
1058}
1059
1060static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
1061 unsigned long parent_rate)
1062{
1063 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1064 int control = 0;
1065 int rate_sel;
1066
1067 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1068 rate_sel++) {
1069 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1070 break;
1071 }
1072
1073 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1074 return -EINVAL;
1075
1076 if (rate_sel & 1)
1077 control |= DS1337_BIT_RS1;
1078 if (rate_sel & 2)
1079 control |= DS1337_BIT_RS2;
1080
1081 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1082 control);
1083}
1084
1085static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1086{
1087 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1088
1089 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1090}
1091
1092static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1093{
1094 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1095
1096 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1097}
1098
1099static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1100{
1101 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1102 int control;
1103
1104 control = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_CONTROL);
1105 if (control < 0)
1106 return control;
1107
1108 return !(control & DS1337_BIT_INTCN);
1109}
1110
1111static const struct clk_ops ds3231_clk_sqw_ops = {
1112 .prepare = ds3231_clk_sqw_prepare,
1113 .unprepare = ds3231_clk_sqw_unprepare,
1114 .is_prepared = ds3231_clk_sqw_is_prepared,
1115 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1116 .round_rate = ds3231_clk_sqw_round_rate,
1117 .set_rate = ds3231_clk_sqw_set_rate,
1118};
1119
1120static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
1121 unsigned long parent_rate)
1122{
1123 return 32768;
1124}
1125
1126static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1127{
1128 struct i2c_client *client = ds1307->client;
1129 struct mutex *lock = &ds1307->rtc->ops_lock;
1130 int status;
1131 int ret;
1132
1133 mutex_lock(lock);
1134
1135 status = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
1136 if (status < 0) {
1137 ret = status;
1138 goto out;
1139 }
1140
1141 if (enable)
1142 status |= DS3231_BIT_EN32KHZ;
1143 else
1144 status &= ~DS3231_BIT_EN32KHZ;
1145
1146 ret = i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, status);
1147out:
1148 mutex_unlock(lock);
1149
1150 return ret;
1151}
1152
1153static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1154{
1155 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1156
1157 return ds3231_clk_32khz_control(ds1307, true);
1158}
1159
1160static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1161{
1162 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1163
1164 ds3231_clk_32khz_control(ds1307, false);
1165}
1166
1167static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1168{
1169 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1170 int status;
1171
1172 status = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_STATUS);
1173 if (status < 0)
1174 return status;
1175
1176 return !!(status & DS3231_BIT_EN32KHZ);
1177}
1178
1179static const struct clk_ops ds3231_clk_32khz_ops = {
1180 .prepare = ds3231_clk_32khz_prepare,
1181 .unprepare = ds3231_clk_32khz_unprepare,
1182 .is_prepared = ds3231_clk_32khz_is_prepared,
1183 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1184};
1185
1186static struct clk_init_data ds3231_clks_init[] = {
1187 [DS3231_CLK_SQW] = {
1188 .name = "ds3231_clk_sqw",
1189 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001190 },
1191 [DS3231_CLK_32KHZ] = {
1192 .name = "ds3231_clk_32khz",
1193 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001194 },
1195};
1196
1197static int ds3231_clks_register(struct ds1307 *ds1307)
1198{
1199 struct i2c_client *client = ds1307->client;
1200 struct device_node *node = client->dev.of_node;
1201 struct clk_onecell_data *onecell;
1202 int i;
1203
1204 onecell = devm_kzalloc(&client->dev, sizeof(*onecell), GFP_KERNEL);
1205 if (!onecell)
1206 return -ENOMEM;
1207
1208 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
1209 onecell->clks = devm_kcalloc(&client->dev, onecell->clk_num,
1210 sizeof(onecell->clks[0]), GFP_KERNEL);
1211 if (!onecell->clks)
1212 return -ENOMEM;
1213
1214 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1215 struct clk_init_data init = ds3231_clks_init[i];
1216
1217 /*
1218 * Interrupt signal due to alarm conditions and square-wave
1219 * output share same pin, so don't initialize both.
1220 */
1221 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1222 continue;
1223
1224 /* optional override of the clockname */
1225 of_property_read_string_index(node, "clock-output-names", i,
1226 &init.name);
1227 ds1307->clks[i].init = &init;
1228
1229 onecell->clks[i] = devm_clk_register(&client->dev,
1230 &ds1307->clks[i]);
1231 if (IS_ERR(onecell->clks[i]))
1232 return PTR_ERR(onecell->clks[i]);
1233 }
1234
1235 if (!node)
1236 return 0;
1237
1238 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1239
1240 return 0;
1241}
1242
1243static void ds1307_clks_register(struct ds1307 *ds1307)
1244{
1245 int ret;
1246
1247 if (ds1307->type != ds_3231)
1248 return;
1249
1250 ret = ds3231_clks_register(ds1307);
1251 if (ret) {
1252 dev_warn(&ds1307->client->dev,
1253 "unable to register clock device %d\n", ret);
1254 }
1255}
1256
1257#else
1258
1259static void ds1307_clks_register(struct ds1307 *ds1307)
1260{
1261}
1262
1263#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001264
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001265static int ds1307_probe(struct i2c_client *client,
1266 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001267{
1268 struct ds1307 *ds1307;
1269 int err = -ENODEV;
Keerthye29385f2016-06-01 16:19:07 +05301270 int tmp, wday;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001271 struct chip_desc *chip = &chips[id->driver_data];
David Brownellc065f352007-07-17 04:05:10 -07001272 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Peter Senna Tschudinc8b18da2013-11-12 15:10:59 -08001273 bool want_irq = false;
Michael Lange8bc2a402016-01-21 18:10:16 +01001274 bool ds1307_can_wakeup_device = false;
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001275 unsigned char *buf;
Jingoo Han01ce8932013-11-12 15:10:41 -08001276 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Keerthye29385f2016-06-01 16:19:07 +05301277 struct rtc_time tm;
1278 unsigned long timestamp;
1279
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001280 irq_handler_t irq_handler = ds1307_irq;
1281
Wolfram Sang97f902b2009-06-17 16:26:10 -07001282 static const int bbsqi_bitpos[] = {
1283 [ds_1337] = 0,
1284 [ds_1339] = DS1339_BIT_BBSQI,
1285 [ds_3231] = DS3231_BIT_BBSQW,
1286 };
Simon Guinot1d1945d2014-04-03 14:49:55 -07001287 const struct rtc_class_ops *rtc_ops = &ds13xx_rtc_ops;
David Brownell1abb0dc2006-06-25 05:48:17 -07001288
Ed Swierk30e7b032009-03-31 15:24:56 -07001289 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)
1290 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
David Brownellc065f352007-07-17 04:05:10 -07001291 return -EIO;
David Brownell1abb0dc2006-06-25 05:48:17 -07001292
Jingoo Hanedca66d2013-07-03 15:07:05 -07001293 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001294 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001295 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001296
David Brownell1abb0dc2006-06-25 05:48:17 -07001297 i2c_set_clientdata(client, ds1307);
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001298
1299 ds1307->client = client;
1300 ds1307->type = id->driver_data;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001301
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001302 if (!pdata && client->dev.of_node)
1303 ds1307_trickle_of_init(client, chip);
1304 else if (pdata && pdata->trickle_charger_setup)
1305 chip->trickle_charger_setup = pdata->trickle_charger_setup;
1306
1307 if (chip->trickle_charger_setup && chip->trickle_charger_reg) {
1308 dev_dbg(&client->dev, "writing trickle charger info 0x%x to 0x%x\n",
1309 DS13XX_TRICKLE_CHARGER_MAGIC | chip->trickle_charger_setup,
1310 chip->trickle_charger_reg);
Wolfram Sangeb86c302012-05-29 15:07:38 -07001311 i2c_smbus_write_byte_data(client, chip->trickle_charger_reg,
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001312 DS13XX_TRICKLE_CHARGER_MAGIC |
1313 chip->trickle_charger_setup);
1314 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001315
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001316 buf = ds1307->regs;
Ed Swierk30e7b032009-03-31 15:24:56 -07001317 if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
Bertrand Achardbc48b902013-04-29 16:19:26 -07001318 ds1307->read_block_data = ds1307_native_smbus_read_block_data;
1319 ds1307->write_block_data = ds1307_native_smbus_write_block_data;
Ed Swierk30e7b032009-03-31 15:24:56 -07001320 } else {
1321 ds1307->read_block_data = ds1307_read_block_data;
1322 ds1307->write_block_data = ds1307_write_block_data;
1323 }
David Brownell045e0e82007-07-17 04:04:55 -07001324
Michael Lange8bc2a402016-01-21 18:10:16 +01001325#ifdef CONFIG_OF
1326/*
1327 * For devices with no IRQ directly connected to the SoC, the RTC chip
1328 * can be forced as a wakeup source by stating that explicitly in
1329 * the device's .dts file using the "wakeup-source" boolean property.
1330 * If the "wakeup-source" property is set, don't request an IRQ.
1331 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1332 * if supported by the RTC.
1333 */
1334 if (of_property_read_bool(client->dev.of_node, "wakeup-source")) {
1335 ds1307_can_wakeup_device = true;
1336 }
Alexandre Belloni78aaa062016-07-13 02:36:41 +02001337 /* Intersil ISL12057 DT backward compatibility */
1338 if (of_property_read_bool(client->dev.of_node,
1339 "isil,irq2-can-wakeup-machine")) {
1340 ds1307_can_wakeup_device = true;
1341 }
Michael Lange8bc2a402016-01-21 18:10:16 +01001342#endif
1343
David Brownell045e0e82007-07-17 04:04:55 -07001344 switch (ds1307->type) {
1345 case ds_1337:
1346 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001347 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001348 /* get registers that the "rtc" read below won't read... */
Ed Swierk30e7b032009-03-31 15:24:56 -07001349 tmp = ds1307->read_block_data(ds1307->client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001350 DS1337_REG_CONTROL, 2, buf);
David Brownell1abb0dc2006-06-25 05:48:17 -07001351 if (tmp != 2) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001352 dev_dbg(&client->dev, "read error %d\n", tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -07001353 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001354 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001355 }
1356
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001357 /* oscillator off? turn it on, so clock can tick. */
1358 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001359 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
1360
David Anders40ce9722012-03-23 15:02:37 -07001361 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001362 * Using IRQ or defined as wakeup-source?
1363 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001364 * For some variants, be sure alarms can trigger when we're
1365 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001366 */
Michael Lange8bc2a402016-01-21 18:10:16 +01001367 if (chip->alarm && (ds1307->client->irq > 0 ||
1368 ds1307_can_wakeup_device)) {
Wolfram Sang97f902b2009-06-17 16:26:10 -07001369 ds1307->regs[0] |= DS1337_BIT_INTCN
1370 | bbsqi_bitpos[ds1307->type];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001371 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Wolfram Sangb24a7262012-03-23 15:02:37 -07001372
1373 want_irq = true;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001374 }
1375
1376 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
1377 ds1307->regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001378
1379 /* oscillator fault? clear flag, and warn */
1380 if (ds1307->regs[1] & DS1337_BIT_OSF) {
1381 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
1382 ds1307->regs[1] & ~DS1337_BIT_OSF);
1383 dev_warn(&client->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001384 }
David Brownell045e0e82007-07-17 04:04:55 -07001385 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001386
1387 case rx_8025:
1388 tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
1389 RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
1390 if (tmp != 2) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001391 dev_dbg(&client->dev, "read error %d\n", tmp);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001392 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001393 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001394 }
1395
1396 /* oscillator off? turn it on, so clock can tick. */
1397 if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
1398 ds1307->regs[1] |= RX8025_BIT_XST;
1399 i2c_smbus_write_byte_data(client,
1400 RX8025_REG_CTRL2 << 4 | 0x08,
1401 ds1307->regs[1]);
1402 dev_warn(&client->dev,
1403 "oscillator stop detected - SET TIME!\n");
1404 }
1405
1406 if (ds1307->regs[1] & RX8025_BIT_PON) {
1407 ds1307->regs[1] &= ~RX8025_BIT_PON;
1408 i2c_smbus_write_byte_data(client,
1409 RX8025_REG_CTRL2 << 4 | 0x08,
1410 ds1307->regs[1]);
1411 dev_warn(&client->dev, "power-on detected\n");
1412 }
1413
1414 if (ds1307->regs[1] & RX8025_BIT_VDET) {
1415 ds1307->regs[1] &= ~RX8025_BIT_VDET;
1416 i2c_smbus_write_byte_data(client,
1417 RX8025_REG_CTRL2 << 4 | 0x08,
1418 ds1307->regs[1]);
1419 dev_warn(&client->dev, "voltage drop detected\n");
1420 }
1421
1422 /* make sure we are running in 24hour mode */
1423 if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
1424 u8 hour;
1425
1426 /* switch to 24 hour mode */
1427 i2c_smbus_write_byte_data(client,
1428 RX8025_REG_CTRL1 << 4 | 0x08,
1429 ds1307->regs[0] |
1430 RX8025_BIT_2412);
1431
1432 tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
1433 RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
1434 if (tmp != 2) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001435 dev_dbg(&client->dev, "read error %d\n", tmp);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001436 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001437 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001438 }
1439
1440 /* correct hour */
1441 hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
1442 if (hour == 12)
1443 hour = 0;
1444 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1445 hour += 12;
1446
1447 i2c_smbus_write_byte_data(client,
1448 DS1307_REG_HOUR << 4 | 0x08,
1449 hour);
1450 }
1451 break;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001452 case ds_1388:
1453 ds1307->offset = 1; /* Seconds starts at 1 */
1454 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001455 case mcp794xx:
1456 rtc_ops = &mcp794xx_rtc_ops;
Simon Guinot1d1945d2014-04-03 14:49:55 -07001457 if (ds1307->client->irq > 0 && chip->alarm) {
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001458 irq_handler = mcp794xx_irq;
Simon Guinot1d1945d2014-04-03 14:49:55 -07001459 want_irq = true;
1460 }
1461 break;
David Brownell045e0e82007-07-17 04:04:55 -07001462 default:
1463 break;
1464 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001465
1466read_rtc:
1467 /* read RTC registers */
Joakim Tjernlund96fc3a42010-06-29 15:05:34 -07001468 tmp = ds1307->read_block_data(ds1307->client, ds1307->offset, 8, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001469 if (tmp != 8) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001470 dev_dbg(&client->dev, "read error %d\n", tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -07001471 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001472 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001473 }
1474
David Anders40ce9722012-03-23 15:02:37 -07001475 /*
1476 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001477 * specify the extra bits as must-be-zero, but there are
1478 * still a few values that are clearly out-of-range.
1479 */
1480 tmp = ds1307->regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001481 switch (ds1307->type) {
1482 case ds_1307:
David Brownell045e0e82007-07-17 04:04:55 -07001483 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001484 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001485 if (tmp & DS1307_BIT_CH) {
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001486 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
1487 dev_warn(&client->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001488 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001489 }
David Brownell045e0e82007-07-17 04:04:55 -07001490 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001491 case ds_1338:
1492 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001493 if (tmp & DS1307_BIT_CH)
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001494 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
1495
1496 /* oscillator fault? clear flag, and warn */
1497 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
1498 i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
David Brownellbd16f9e2007-07-26 10:41:00 -07001499 ds1307->regs[DS1307_REG_CONTROL]
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001500 & ~DS1338_BIT_OSF);
1501 dev_warn(&client->dev, "SET TIME!\n");
1502 goto read_rtc;
1503 }
David Brownell045e0e82007-07-17 04:04:55 -07001504 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001505 case ds_1340:
1506 /* clock halted? turn it on, so clock can tick. */
1507 if (tmp & DS1340_BIT_nEOSC)
1508 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
1509
1510 tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG);
1511 if (tmp < 0) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001512 dev_dbg(&client->dev, "read error %d\n", tmp);
frederic Rodofcd8db02008-02-06 01:38:55 -08001513 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001514 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001515 }
1516
1517 /* oscillator fault? clear flag, and warn */
1518 if (tmp & DS1340_BIT_OSF) {
1519 i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0);
1520 dev_warn(&client->dev, "SET TIME!\n");
1521 }
1522 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001523 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001524 /* make sure that the backup battery is enabled */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001525 if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
David Anders43fcb812011-11-02 13:37:53 -07001526 i2c_smbus_write_byte_data(client, DS1307_REG_WDAY,
1527 ds1307->regs[DS1307_REG_WDAY]
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001528 | MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001529 }
1530
1531 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001532 if (!(tmp & MCP794XX_BIT_ST)) {
David Anders43fcb812011-11-02 13:37:53 -07001533 i2c_smbus_write_byte_data(client, DS1307_REG_SECS,
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001534 MCP794XX_BIT_ST);
David Anders43fcb812011-11-02 13:37:53 -07001535 dev_warn(&client->dev, "SET TIME!\n");
1536 goto read_rtc;
1537 }
1538
1539 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001540 default:
David Brownell045e0e82007-07-17 04:04:55 -07001541 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001542 }
David Brownell045e0e82007-07-17 04:04:55 -07001543
David Brownell1abb0dc2006-06-25 05:48:17 -07001544 tmp = ds1307->regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001545 switch (ds1307->type) {
1546 case ds_1340:
1547 case m41t00:
David Anders40ce9722012-03-23 15:02:37 -07001548 /*
1549 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001550 * systems that will run through year 2100.
1551 */
1552 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001553 case rx_8025:
1554 break;
David Brownellc065f352007-07-17 04:05:10 -07001555 default:
1556 if (!(tmp & DS1307_BIT_12HR))
1557 break;
1558
David Anders40ce9722012-03-23 15:02:37 -07001559 /*
1560 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001561 * take note...
1562 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001563 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001564 if (tmp == 12)
1565 tmp = 0;
1566 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1567 tmp += 12;
David Brownell1abb0dc2006-06-25 05:48:17 -07001568 i2c_smbus_write_byte_data(client,
Joakim Tjernlund96fc3a42010-06-29 15:05:34 -07001569 ds1307->offset + DS1307_REG_HOUR,
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001570 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001571 }
1572
Keerthye29385f2016-06-01 16:19:07 +05301573 /*
1574 * Some IPs have weekday reset value = 0x1 which might not correct
1575 * hence compute the wday using the current date/month/year values
1576 */
1577 ds1307_get_time(&client->dev, &tm);
1578 wday = tm.tm_wday;
1579 timestamp = rtc_tm_to_time64(&tm);
1580 rtc_time64_to_tm(timestamp, &tm);
1581
1582 /*
1583 * Check if reset wday is different from the computed wday
1584 * If different then set the wday which we computed using
1585 * timestamp
1586 */
1587 if (wday != tm.tm_wday) {
1588 wday = i2c_smbus_read_byte_data(client, MCP794XX_REG_WEEKDAY);
1589 wday = wday & ~MCP794XX_REG_WEEKDAY_WDAY_MASK;
1590 wday = wday | (tm.tm_wday + 1);
1591 i2c_smbus_write_byte_data(client, MCP794XX_REG_WEEKDAY, wday);
1592 }
1593
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001594 if (want_irq) {
1595 device_set_wakeup_capable(&client->dev, true);
1596 set_bit(HAS_ALARM, &ds1307->flags);
1597 }
Jingoo Hanedca66d2013-07-03 15:07:05 -07001598 ds1307->rtc = devm_rtc_device_register(&client->dev, client->name,
Simon Guinot1d1945d2014-04-03 14:49:55 -07001599 rtc_ops, THIS_MODULE);
David Brownell1abb0dc2006-06-25 05:48:17 -07001600 if (IS_ERR(ds1307->rtc)) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001601 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001602 }
1603
Nishanth Menon38a7a732016-04-19 11:23:54 -05001604 if (ds1307_can_wakeup_device && ds1307->client->irq <= 0) {
Michael Lange8bc2a402016-01-21 18:10:16 +01001605 /* Disable request for an IRQ */
1606 want_irq = false;
1607 dev_info(&client->dev, "'wakeup-source' is set, request for an IRQ is disabled!\n");
1608 /* We cannot support UIE mode if we do not have an IRQ line */
1609 ds1307->rtc->uie_unsupported = 1;
1610 }
1611
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001612 if (want_irq) {
Nishanth Menonc5983192015-06-23 11:15:11 -05001613 err = devm_request_threaded_irq(&client->dev,
1614 client->irq, NULL, irq_handler,
1615 IRQF_SHARED | IRQF_ONESHOT,
1616 ds1307->rtc->name, client);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001617 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001618 client->irq = 0;
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001619 device_set_wakeup_capable(&client->dev, false);
1620 clear_bit(HAS_ALARM, &ds1307->flags);
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001621 dev_err(&client->dev, "unable to request IRQ!\n");
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001622 } else
Felipe Balbi51c4cfe2015-11-11 10:11:01 -06001623 dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001624 }
1625
Austin Boyle9eab0a72012-03-23 15:02:38 -07001626 if (chip->nvram_size) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001627
Jingoo Hanedca66d2013-07-03 15:07:05 -07001628 ds1307->nvram = devm_kzalloc(&client->dev,
1629 sizeof(struct bin_attribute),
1630 GFP_KERNEL);
Austin Boyle9eab0a72012-03-23 15:02:38 -07001631 if (!ds1307->nvram) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001632 dev_err(&client->dev, "cannot allocate memory for nvram sysfs\n");
1633 } else {
1634
1635 ds1307->nvram->attr.name = "nvram";
1636 ds1307->nvram->attr.mode = S_IRUGO | S_IWUSR;
1637
1638 sysfs_bin_attr_init(ds1307->nvram);
1639
1640 ds1307->nvram->read = ds1307_nvram_read;
1641 ds1307->nvram->write = ds1307_nvram_write;
1642 ds1307->nvram->size = chip->nvram_size;
1643 ds1307->nvram_offset = chip->nvram_offset;
1644
1645 err = sysfs_create_bin_file(&client->dev.kobj,
1646 ds1307->nvram);
1647 if (err) {
1648 dev_err(&client->dev,
1649 "unable to create sysfs file: %s\n",
1650 ds1307->nvram->attr.name);
1651 } else {
1652 set_bit(HAS_NVRAM, &ds1307->flags);
1653 dev_info(&client->dev, "%zu bytes nvram\n",
1654 ds1307->nvram->size);
1655 }
David Brownell682d73f2007-11-14 16:58:32 -08001656 }
1657 }
1658
Akinobu Mita445c0202016-01-25 00:22:16 +09001659 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001660 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001661
David Brownell1abb0dc2006-06-25 05:48:17 -07001662 return 0;
1663
Jingoo Hanedca66d2013-07-03 15:07:05 -07001664exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001665 return err;
1666}
1667
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001668static int ds1307_remove(struct i2c_client *client)
David Brownell1abb0dc2006-06-25 05:48:17 -07001669{
David Anders40ce9722012-03-23 15:02:37 -07001670 struct ds1307 *ds1307 = i2c_get_clientdata(client);
David Brownell1abb0dc2006-06-25 05:48:17 -07001671
Jingoo Hanedca66d2013-07-03 15:07:05 -07001672 if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
Austin Boyle9eab0a72012-03-23 15:02:38 -07001673 sysfs_remove_bin_file(&client->dev.kobj, ds1307->nvram);
David Brownell682d73f2007-11-14 16:58:32 -08001674
David Brownell1abb0dc2006-06-25 05:48:17 -07001675 return 0;
1676}
1677
1678static struct i2c_driver ds1307_driver = {
1679 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001680 .name = "rtc-ds1307",
David Brownell1abb0dc2006-06-25 05:48:17 -07001681 },
David Brownellc065f352007-07-17 04:05:10 -07001682 .probe = ds1307_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001683 .remove = ds1307_remove,
Jean Delvare3760f732008-04-29 23:11:40 +02001684 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001685};
1686
Axel Lin0abc9202012-03-23 15:02:31 -07001687module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001688
1689MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1690MODULE_LICENSE("GPL");