blob: a705e6490808f277e21102bf208a5e77c1d35bb1 [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>
Nishanth Menon7abea612015-06-24 11:26:54 -050018#include <linux/of_device.h>
19#include <linux/of_irq.h>
20#include <linux/pm_wakeirq.h>
Wolfram Sangeb86c302012-05-29 15:07:38 -070021#include <linux/rtc/ds1307.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050022#include <linux/rtc.h>
23#include <linux/slab.h>
24#include <linux/string.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
95# define DS1337_BIT_A2I 0x02
96# define DS1337_BIT_A1I 0x01
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070097#define DS1339_REG_ALARM1_SECS 0x07
Wolfram Sangeb86c302012-05-29 15:07:38 -070098
99#define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
David Brownell1abb0dc2006-06-25 05:48:17 -0700100
Matthias Fuchsa2166852009-03-31 15:24:58 -0700101#define RX8025_REG_CTRL1 0x0e
102# define RX8025_BIT_2412 0x20
103#define RX8025_REG_CTRL2 0x0f
104# define RX8025_BIT_PON 0x10
105# define RX8025_BIT_VDET 0x40
106# define RX8025_BIT_XST 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -0700107
108
109struct ds1307 {
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700110 u8 offset; /* register's offset */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700111 u8 regs[11];
Austin Boyle9eab0a72012-03-23 15:02:38 -0700112 u16 nvram_offset;
113 struct bin_attribute *nvram;
David Brownell1abb0dc2006-06-25 05:48:17 -0700114 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700115 unsigned long flags;
116#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
117#define HAS_ALARM 1 /* bit 1 == irq claimed */
David Brownell045e0e82007-07-17 04:04:55 -0700118 struct i2c_client *client;
David Brownell1abb0dc2006-06-25 05:48:17 -0700119 struct rtc_device *rtc;
Nishanth Menon7abea612015-06-24 11:26:54 -0500120 int wakeirq;
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);
David Brownell1abb0dc2006-06-25 05:48:17 -0700125};
126
David Brownell045e0e82007-07-17 04:04:55 -0700127struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700128 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700129 u16 nvram_offset;
130 u16 nvram_size;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700131 u16 trickle_charger_reg;
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700132 u8 trickle_charger_setup;
133 u8 (*do_trickle_setup)(struct i2c_client *, uint32_t, bool);
David Brownell045e0e82007-07-17 04:04:55 -0700134};
135
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700136static u8 do_trickle_setup_ds1339(struct i2c_client *,
137 uint32_t ohms, bool diode);
138
139static struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700140 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700141 .nvram_offset = 8,
142 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700143 },
144 [ds_1337] = {
145 .alarm = 1,
146 },
147 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700148 .nvram_offset = 8,
149 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700150 },
151 [ds_1339] = {
152 .alarm = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700153 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700154 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700155 },
156 [ds_1340] = {
157 .trickle_charger_reg = 0x08,
158 },
159 [ds_1388] = {
160 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700161 },
162 [ds_3231] = {
163 .alarm = 1,
164 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800165 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700166 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700167 /* this is battery backed SRAM */
168 .nvram_offset = 0x20,
169 .nvram_size = 0x40,
170 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700171};
David Brownell045e0e82007-07-17 04:04:55 -0700172
Jean Delvare3760f732008-04-29 23:11:40 +0200173static const struct i2c_device_id ds1307_id[] = {
174 { "ds1307", ds_1307 },
175 { "ds1337", ds_1337 },
176 { "ds1338", ds_1338 },
177 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700178 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200179 { "ds1340", ds_1340 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700180 { "ds3231", ds_3231 },
Jean Delvare3760f732008-04-29 23:11:40 +0200181 { "m41t00", m41t00 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800182 { "mcp7940x", mcp794xx },
183 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700184 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700185 { "rx8025", rx_8025 },
Jean Delvare3760f732008-04-29 23:11:40 +0200186 { }
187};
188MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700189
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700190/*----------------------------------------------------------------------*/
191
Ed Swierk30e7b032009-03-31 15:24:56 -0700192#define BLOCK_DATA_MAX_TRIES 10
193
Jean Delvare0cc43a12011-01-10 22:11:23 +0100194static s32 ds1307_read_block_data_once(const struct i2c_client *client,
195 u8 command, u8 length, u8 *values)
Ed Swierk30e7b032009-03-31 15:24:56 -0700196{
197 s32 i, data;
198
199 for (i = 0; i < length; i++) {
200 data = i2c_smbus_read_byte_data(client, command + i);
201 if (data < 0)
202 return data;
203 values[i] = data;
204 }
205 return i;
206}
207
Jean Delvare0cc43a12011-01-10 22:11:23 +0100208static s32 ds1307_read_block_data(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700209 u8 length, u8 *values)
210{
Bertrand Achardbc48b902013-04-29 16:19:26 -0700211 u8 oldvalues[255];
Ed Swierk30e7b032009-03-31 15:24:56 -0700212 s32 ret;
213 int tries = 0;
214
215 dev_dbg(&client->dev, "ds1307_read_block_data (length=%d)\n", length);
216 ret = ds1307_read_block_data_once(client, command, length, values);
217 if (ret < 0)
218 return ret;
219 do {
220 if (++tries > BLOCK_DATA_MAX_TRIES) {
221 dev_err(&client->dev,
222 "ds1307_read_block_data failed\n");
223 return -EIO;
224 }
225 memcpy(oldvalues, values, length);
226 ret = ds1307_read_block_data_once(client, command, length,
227 values);
228 if (ret < 0)
229 return ret;
230 } while (memcmp(oldvalues, values, length));
231 return length;
232}
233
Jean Delvare0cc43a12011-01-10 22:11:23 +0100234static s32 ds1307_write_block_data(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700235 u8 length, const u8 *values)
236{
Bertrand Achardbc48b902013-04-29 16:19:26 -0700237 u8 currvalues[255];
Ed Swierk30e7b032009-03-31 15:24:56 -0700238 int tries = 0;
239
240 dev_dbg(&client->dev, "ds1307_write_block_data (length=%d)\n", length);
241 do {
242 s32 i, ret;
243
244 if (++tries > BLOCK_DATA_MAX_TRIES) {
245 dev_err(&client->dev,
246 "ds1307_write_block_data failed\n");
247 return -EIO;
248 }
249 for (i = 0; i < length; i++) {
250 ret = i2c_smbus_write_byte_data(client, command + i,
251 values[i]);
252 if (ret < 0)
253 return ret;
254 }
255 ret = ds1307_read_block_data_once(client, command, length,
256 currvalues);
257 if (ret < 0)
258 return ret;
259 } while (memcmp(currvalues, values, length));
260 return length;
261}
262
263/*----------------------------------------------------------------------*/
264
Bertrand Achardbc48b902013-04-29 16:19:26 -0700265/* These RTC devices are not designed to be connected to a SMbus adapter.
266 SMbus limits block operations length to 32 bytes, whereas it's not
267 limited on I2C buses. As a result, accesses may exceed 32 bytes;
268 in that case, split them into smaller blocks */
269
270static s32 ds1307_native_smbus_write_block_data(const struct i2c_client *client,
271 u8 command, u8 length, const u8 *values)
272{
273 u8 suboffset = 0;
274
275 if (length <= I2C_SMBUS_BLOCK_MAX)
276 return i2c_smbus_write_i2c_block_data(client,
277 command, length, values);
278
279 while (suboffset < length) {
280 s32 retval = i2c_smbus_write_i2c_block_data(client,
281 command + suboffset,
282 min(I2C_SMBUS_BLOCK_MAX, length - suboffset),
283 values + suboffset);
284 if (retval < 0)
285 return retval;
286
287 suboffset += I2C_SMBUS_BLOCK_MAX;
288 }
289 return length;
290}
291
292static s32 ds1307_native_smbus_read_block_data(const struct i2c_client *client,
293 u8 command, u8 length, u8 *values)
294{
295 u8 suboffset = 0;
296
297 if (length <= I2C_SMBUS_BLOCK_MAX)
298 return i2c_smbus_read_i2c_block_data(client,
299 command, length, values);
300
301 while (suboffset < length) {
302 s32 retval = i2c_smbus_read_i2c_block_data(client,
303 command + suboffset,
304 min(I2C_SMBUS_BLOCK_MAX, length - suboffset),
305 values + suboffset);
306 if (retval < 0)
307 return retval;
308
309 suboffset += I2C_SMBUS_BLOCK_MAX;
310 }
311 return length;
312}
313
314/*----------------------------------------------------------------------*/
315
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700316/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700317 * The ds1337 and ds1339 both have two alarms, but we only use the first
318 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
319 * signal; ds1339 chips have only one alarm signal.
320 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500321static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700322{
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500323 struct i2c_client *client = dev_id;
324 struct ds1307 *ds1307 = i2c_get_clientdata(client);
325 struct mutex *lock = &ds1307->rtc->ops_lock;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700326 int stat, control;
327
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700328 mutex_lock(lock);
329 stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
330 if (stat < 0)
331 goto out;
332
333 if (stat & DS1337_BIT_A1I) {
334 stat &= ~DS1337_BIT_A1I;
335 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, stat);
336
337 control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
338 if (control < 0)
339 goto out;
340
341 control &= ~DS1337_BIT_A1IE;
342 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
343
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700344 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700345 }
346
347out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700348 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700349
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700350 return IRQ_HANDLED;
351}
352
353/*----------------------------------------------------------------------*/
354
David Brownell1abb0dc2006-06-25 05:48:17 -0700355static int ds1307_get_time(struct device *dev, struct rtc_time *t)
356{
357 struct ds1307 *ds1307 = dev_get_drvdata(dev);
358 int tmp;
359
David Brownell045e0e82007-07-17 04:04:55 -0700360 /* read the RTC date and time registers all at once */
Ed Swierk30e7b032009-03-31 15:24:56 -0700361 tmp = ds1307->read_block_data(ds1307->client,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700362 ds1307->offset, 7, ds1307->regs);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800363 if (tmp != 7) {
David Brownell1abb0dc2006-06-25 05:48:17 -0700364 dev_err(dev, "%s error %d\n", "read", tmp);
365 return -EIO;
366 }
367
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800368 dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700369
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700370 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
371 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700372 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700373 t->tm_hour = bcd2bin(tmp);
374 t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
375 t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700376 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700377 t->tm_mon = bcd2bin(tmp) - 1;
David Brownell1abb0dc2006-06-25 05:48:17 -0700378
379 /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700380 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700381
382 dev_dbg(dev, "%s secs=%d, mins=%d, "
383 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
384 "read", t->tm_sec, t->tm_min,
385 t->tm_hour, t->tm_mday,
386 t->tm_mon, t->tm_year, t->tm_wday);
387
David Brownell045e0e82007-07-17 04:04:55 -0700388 /* initial clock setting can be undefined */
389 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700390}
391
392static int ds1307_set_time(struct device *dev, struct rtc_time *t)
393{
394 struct ds1307 *ds1307 = dev_get_drvdata(dev);
395 int result;
396 int tmp;
397 u8 *buf = ds1307->regs;
398
399 dev_dbg(dev, "%s secs=%d, mins=%d, "
400 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400401 "write", t->tm_sec, t->tm_min,
402 t->tm_hour, t->tm_mday,
403 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700404
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700405 buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
406 buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
407 buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
408 buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
409 buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
410 buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700411
412 /* assume 20YY not 19YY */
413 tmp = t->tm_year - 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700414 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700415
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700416 switch (ds1307->type) {
417 case ds_1337:
418 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -0700419 case ds_3231:
David Brownell1abb0dc2006-06-25 05:48:17 -0700420 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700421 break;
422 case ds_1340:
David Brownell1abb0dc2006-06-25 05:48:17 -0700423 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
424 | DS1340_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700425 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800426 case mcp794xx:
David Anders40ce9722012-03-23 15:02:37 -0700427 /*
428 * these bits were cleared when preparing the date/time
429 * values and need to be set again before writing the
430 * buffer out to the device.
431 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800432 buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
433 buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
David Anders43fcb812011-11-02 13:37:53 -0700434 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700435 default:
436 break;
437 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700438
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800439 dev_dbg(dev, "%s: %7ph\n", "write", buf);
David Brownell1abb0dc2006-06-25 05:48:17 -0700440
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700441 result = ds1307->write_block_data(ds1307->client,
442 ds1307->offset, 7, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800443 if (result < 0) {
444 dev_err(dev, "%s error %d\n", "write", result);
445 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700446 }
447 return 0;
448}
449
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800450static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700451{
452 struct i2c_client *client = to_i2c_client(dev);
453 struct ds1307 *ds1307 = i2c_get_clientdata(client);
454 int ret;
455
456 if (!test_bit(HAS_ALARM, &ds1307->flags))
457 return -EINVAL;
458
459 /* read all ALARM1, ALARM2, and status registers at once */
Ed Swierk30e7b032009-03-31 15:24:56 -0700460 ret = ds1307->read_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800461 DS1339_REG_ALARM1_SECS, 9, ds1307->regs);
462 if (ret != 9) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700463 dev_err(dev, "%s error %d\n", "alarm read", ret);
464 return -EIO;
465 }
466
467 dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
468 "alarm read",
469 ds1307->regs[0], ds1307->regs[1],
470 ds1307->regs[2], ds1307->regs[3],
471 ds1307->regs[4], ds1307->regs[5],
472 ds1307->regs[6], ds1307->regs[7],
473 ds1307->regs[8]);
474
David Anders40ce9722012-03-23 15:02:37 -0700475 /*
476 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700477 * and that all four fields are checked matches
478 */
479 t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
480 t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
481 t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
482 t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
483 t->time.tm_mon = -1;
484 t->time.tm_year = -1;
485 t->time.tm_wday = -1;
486 t->time.tm_yday = -1;
487 t->time.tm_isdst = -1;
488
489 /* ... and status */
490 t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
491 t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
492
493 dev_dbg(dev, "%s secs=%d, mins=%d, "
494 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
495 "alarm read", t->time.tm_sec, t->time.tm_min,
496 t->time.tm_hour, t->time.tm_mday,
497 t->enabled, t->pending);
498
499 return 0;
500}
501
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800502static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700503{
David Anders40ce9722012-03-23 15:02:37 -0700504 struct i2c_client *client = to_i2c_client(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700505 struct ds1307 *ds1307 = i2c_get_clientdata(client);
506 unsigned char *buf = ds1307->regs;
507 u8 control, status;
508 int ret;
509
510 if (!test_bit(HAS_ALARM, &ds1307->flags))
511 return -EINVAL;
512
513 dev_dbg(dev, "%s secs=%d, mins=%d, "
514 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
515 "alarm set", t->time.tm_sec, t->time.tm_min,
516 t->time.tm_hour, t->time.tm_mday,
517 t->enabled, t->pending);
518
519 /* read current status of both alarms and the chip */
Ed Swierk30e7b032009-03-31 15:24:56 -0700520 ret = ds1307->read_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800521 DS1339_REG_ALARM1_SECS, 9, buf);
522 if (ret != 9) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700523 dev_err(dev, "%s error %d\n", "alarm write", ret);
524 return -EIO;
525 }
526 control = ds1307->regs[7];
527 status = ds1307->regs[8];
528
529 dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
530 "alarm set (old status)",
531 ds1307->regs[0], ds1307->regs[1],
532 ds1307->regs[2], ds1307->regs[3],
533 ds1307->regs[4], ds1307->regs[5],
534 ds1307->regs[6], control, status);
535
536 /* set ALARM1, using 24 hour and day-of-month modes */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700537 buf[0] = bin2bcd(t->time.tm_sec);
538 buf[1] = bin2bcd(t->time.tm_min);
539 buf[2] = bin2bcd(t->time.tm_hour);
540 buf[3] = bin2bcd(t->time.tm_mday);
541
542 /* set ALARM2 to non-garbage */
543 buf[4] = 0;
544 buf[5] = 0;
545 buf[6] = 0;
546
547 /* optionally enable ALARM1 */
548 buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
549 if (t->enabled) {
550 dev_dbg(dev, "alarm IRQ armed\n");
551 buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
552 }
553 buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
554
Ed Swierk30e7b032009-03-31 15:24:56 -0700555 ret = ds1307->write_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800556 DS1339_REG_ALARM1_SECS, 9, buf);
557 if (ret < 0) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700558 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800559 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700560 }
561
562 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{
567 struct i2c_client *client = to_i2c_client(dev);
568 struct ds1307 *ds1307 = i2c_get_clientdata(client);
569 int ret;
570
John Stultz16380c12011-02-02 17:02:41 -0800571 if (!test_bit(HAS_ALARM, &ds1307->flags))
572 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700573
John Stultz16380c12011-02-02 17:02:41 -0800574 ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
575 if (ret < 0)
576 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700577
John Stultz16380c12011-02-02 17:02:41 -0800578 if (enabled)
579 ret |= DS1337_BIT_A1IE;
580 else
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700581 ret &= ~DS1337_BIT_A1IE;
582
John Stultz16380c12011-02-02 17:02:41 -0800583 ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, ret);
584 if (ret < 0)
585 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700586
587 return 0;
588}
589
David Brownellff8371a2006-09-30 23:28:17 -0700590static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700591 .read_time = ds1307_get_time,
592 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800593 .read_alarm = ds1337_read_alarm,
594 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800595 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700596};
597
David Brownell682d73f2007-11-14 16:58:32 -0800598/*----------------------------------------------------------------------*/
599
Simon Guinot1d1945d2014-04-03 14:49:55 -0700600/*
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800601 * Alarm support for mcp794xx devices.
Simon Guinot1d1945d2014-04-03 14:49:55 -0700602 */
603
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800604#define MCP794XX_REG_CONTROL 0x07
605# define MCP794XX_BIT_ALM0_EN 0x10
606# define MCP794XX_BIT_ALM1_EN 0x20
607#define MCP794XX_REG_ALARM0_BASE 0x0a
608#define MCP794XX_REG_ALARM0_CTRL 0x0d
609#define MCP794XX_REG_ALARM1_BASE 0x11
610#define MCP794XX_REG_ALARM1_CTRL 0x14
611# define MCP794XX_BIT_ALMX_IF (1 << 3)
612# define MCP794XX_BIT_ALMX_C0 (1 << 4)
613# define MCP794XX_BIT_ALMX_C1 (1 << 5)
614# define MCP794XX_BIT_ALMX_C2 (1 << 6)
615# define MCP794XX_BIT_ALMX_POL (1 << 7)
616# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
617 MCP794XX_BIT_ALMX_C1 | \
618 MCP794XX_BIT_ALMX_C2)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700619
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500620static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700621{
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500622 struct i2c_client *client = dev_id;
623 struct ds1307 *ds1307 = i2c_get_clientdata(client);
624 struct mutex *lock = &ds1307->rtc->ops_lock;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700625 int reg, ret;
626
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500627 mutex_lock(lock);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700628
629 /* Check and clear alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800630 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_ALARM0_CTRL);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700631 if (reg < 0)
632 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800633 if (!(reg & MCP794XX_BIT_ALMX_IF))
Simon Guinot1d1945d2014-04-03 14:49:55 -0700634 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800635 reg &= ~MCP794XX_BIT_ALMX_IF;
636 ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_ALARM0_CTRL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700637 if (ret < 0)
638 goto out;
639
640 /* Disable alarm 0. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800641 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700642 if (reg < 0)
643 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800644 reg &= ~MCP794XX_BIT_ALM0_EN;
645 ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700646 if (ret < 0)
647 goto out;
648
649 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
650
651out:
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500652 mutex_unlock(lock);
653
654 return IRQ_HANDLED;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700655}
656
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800657static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700658{
659 struct i2c_client *client = to_i2c_client(dev);
660 struct ds1307 *ds1307 = i2c_get_clientdata(client);
661 u8 *regs = ds1307->regs;
662 int ret;
663
664 if (!test_bit(HAS_ALARM, &ds1307->flags))
665 return -EINVAL;
666
667 /* Read control and alarm 0 registers. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800668 ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700669 if (ret < 0)
670 return ret;
671
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800672 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700673
674 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
675 t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f);
676 t->time.tm_min = bcd2bin(ds1307->regs[4] & 0x7f);
677 t->time.tm_hour = bcd2bin(ds1307->regs[5] & 0x3f);
678 t->time.tm_wday = bcd2bin(ds1307->regs[6] & 0x7) - 1;
679 t->time.tm_mday = bcd2bin(ds1307->regs[7] & 0x3f);
680 t->time.tm_mon = bcd2bin(ds1307->regs[8] & 0x1f) - 1;
681 t->time.tm_year = -1;
682 t->time.tm_yday = -1;
683 t->time.tm_isdst = -1;
684
685 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
686 "enabled=%d polarity=%d irq=%d match=%d\n", __func__,
687 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
688 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800689 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL),
690 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF),
691 (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700692
693 return 0;
694}
695
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800696static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700697{
698 struct i2c_client *client = to_i2c_client(dev);
699 struct ds1307 *ds1307 = i2c_get_clientdata(client);
700 unsigned char *regs = ds1307->regs;
701 int ret;
702
703 if (!test_bit(HAS_ALARM, &ds1307->flags))
704 return -EINVAL;
705
706 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
707 "enabled=%d pending=%d\n", __func__,
708 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
709 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
710 t->enabled, t->pending);
711
712 /* Read control and alarm 0 registers. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800713 ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700714 if (ret < 0)
715 return ret;
716
717 /* Set alarm 0, using 24-hour and day-of-month modes. */
718 regs[3] = bin2bcd(t->time.tm_sec);
719 regs[4] = bin2bcd(t->time.tm_min);
720 regs[5] = bin2bcd(t->time.tm_hour);
721 regs[6] = bin2bcd(t->time.tm_wday) + 1;
722 regs[7] = bin2bcd(t->time.tm_mday);
723 regs[8] = bin2bcd(t->time.tm_mon) + 1;
724
725 /* Clear the alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800726 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700727 /* Set alarm match: second, minute, hour, day, date, month. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800728 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
Nishanth Menone3edd672015-04-20 19:51:34 -0500729 /* Disable interrupt. We will not enable until completely programmed */
730 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700731
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800732 ret = ds1307->write_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700733 if (ret < 0)
734 return ret;
735
Nishanth Menone3edd672015-04-20 19:51:34 -0500736 if (!t->enabled)
737 return 0;
738 regs[0] |= MCP794XX_BIT_ALM0_EN;
739 return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, regs[0]);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700740}
741
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800742static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700743{
744 struct i2c_client *client = to_i2c_client(dev);
745 struct ds1307 *ds1307 = i2c_get_clientdata(client);
746 int reg;
747
748 if (!test_bit(HAS_ALARM, &ds1307->flags))
749 return -EINVAL;
750
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800751 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700752 if (reg < 0)
753 return reg;
754
755 if (enabled)
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800756 reg |= MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700757 else
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800758 reg &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700759
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800760 return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700761}
762
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800763static const struct rtc_class_ops mcp794xx_rtc_ops = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700764 .read_time = ds1307_get_time,
765 .set_time = ds1307_set_time,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800766 .read_alarm = mcp794xx_read_alarm,
767 .set_alarm = mcp794xx_set_alarm,
768 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
Simon Guinot1d1945d2014-04-03 14:49:55 -0700769};
770
771/*----------------------------------------------------------------------*/
772
David Brownell682d73f2007-11-14 16:58:32 -0800773static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700774ds1307_nvram_read(struct file *filp, struct kobject *kobj,
775 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800776 char *buf, loff_t off, size_t count)
777{
778 struct i2c_client *client;
779 struct ds1307 *ds1307;
David Brownell682d73f2007-11-14 16:58:32 -0800780 int result;
781
frederic Rodofcd8db02008-02-06 01:38:55 -0800782 client = kobj_to_i2c_client(kobj);
David Brownell682d73f2007-11-14 16:58:32 -0800783 ds1307 = i2c_get_clientdata(client);
784
Austin Boyle9eab0a72012-03-23 15:02:38 -0700785 result = ds1307->read_block_data(client, ds1307->nvram_offset + off,
786 count, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800787 if (result < 0)
David Brownell682d73f2007-11-14 16:58:32 -0800788 dev_err(&client->dev, "%s error %d\n", "nvram read", result);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800789 return result;
David Brownell682d73f2007-11-14 16:58:32 -0800790}
791
792static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700793ds1307_nvram_write(struct file *filp, struct kobject *kobj,
794 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800795 char *buf, loff_t off, size_t count)
796{
797 struct i2c_client *client;
Ed Swierk30e7b032009-03-31 15:24:56 -0700798 struct ds1307 *ds1307;
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800799 int result;
David Brownell682d73f2007-11-14 16:58:32 -0800800
frederic Rodofcd8db02008-02-06 01:38:55 -0800801 client = kobj_to_i2c_client(kobj);
Ed Swierk30e7b032009-03-31 15:24:56 -0700802 ds1307 = i2c_get_clientdata(client);
David Brownell682d73f2007-11-14 16:58:32 -0800803
Austin Boyle9eab0a72012-03-23 15:02:38 -0700804 result = ds1307->write_block_data(client, ds1307->nvram_offset + off,
805 count, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800806 if (result < 0) {
807 dev_err(&client->dev, "%s error %d\n", "nvram write", result);
808 return result;
809 }
810 return count;
David Brownell682d73f2007-11-14 16:58:32 -0800811}
812
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700813
David Brownell682d73f2007-11-14 16:58:32 -0800814/*----------------------------------------------------------------------*/
815
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700816static u8 do_trickle_setup_ds1339(struct i2c_client *client,
817 uint32_t ohms, bool diode)
818{
819 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
820 DS1307_TRICKLE_CHARGER_NO_DIODE;
821
822 switch (ohms) {
823 case 250:
824 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
825 break;
826 case 2000:
827 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
828 break;
829 case 4000:
830 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
831 break;
832 default:
833 dev_warn(&client->dev,
834 "Unsupported ohm value %u in dt\n", ohms);
835 return 0;
836 }
837 return setup;
838}
839
840static void ds1307_trickle_of_init(struct i2c_client *client,
841 struct chip_desc *chip)
842{
843 uint32_t ohms = 0;
844 bool diode = true;
845
846 if (!chip->do_trickle_setup)
847 goto out;
848 if (of_property_read_u32(client->dev.of_node, "trickle-resistor-ohms" , &ohms))
849 goto out;
850 if (of_property_read_bool(client->dev.of_node, "trickle-diode-disable"))
851 diode = false;
852 chip->trickle_charger_setup = chip->do_trickle_setup(client,
853 ohms, diode);
854out:
855 return;
856}
857
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800858static int ds1307_probe(struct i2c_client *client,
859 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -0700860{
861 struct ds1307 *ds1307;
862 int err = -ENODEV;
David Brownell1abb0dc2006-06-25 05:48:17 -0700863 int tmp;
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700864 struct chip_desc *chip = &chips[id->driver_data];
David Brownellc065f352007-07-17 04:05:10 -0700865 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Peter Senna Tschudinc8b18da2013-11-12 15:10:59 -0800866 bool want_irq = false;
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800867 unsigned char *buf;
Jingoo Han01ce8932013-11-12 15:10:41 -0800868 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500869 irq_handler_t irq_handler = ds1307_irq;
870
Wolfram Sang97f902b2009-06-17 16:26:10 -0700871 static const int bbsqi_bitpos[] = {
872 [ds_1337] = 0,
873 [ds_1339] = DS1339_BIT_BBSQI,
874 [ds_3231] = DS3231_BIT_BBSQW,
875 };
Simon Guinot1d1945d2014-04-03 14:49:55 -0700876 const struct rtc_class_ops *rtc_ops = &ds13xx_rtc_ops;
David Brownell1abb0dc2006-06-25 05:48:17 -0700877
Ed Swierk30e7b032009-03-31 15:24:56 -0700878 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)
879 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
David Brownellc065f352007-07-17 04:05:10 -0700880 return -EIO;
David Brownell1abb0dc2006-06-25 05:48:17 -0700881
Jingoo Hanedca66d2013-07-03 15:07:05 -0700882 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -0700883 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -0700884 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -0700885
David Brownell1abb0dc2006-06-25 05:48:17 -0700886 i2c_set_clientdata(client, ds1307);
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700887
888 ds1307->client = client;
889 ds1307->type = id->driver_data;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700890
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700891 if (!pdata && client->dev.of_node)
892 ds1307_trickle_of_init(client, chip);
893 else if (pdata && pdata->trickle_charger_setup)
894 chip->trickle_charger_setup = pdata->trickle_charger_setup;
895
896 if (chip->trickle_charger_setup && chip->trickle_charger_reg) {
897 dev_dbg(&client->dev, "writing trickle charger info 0x%x to 0x%x\n",
898 DS13XX_TRICKLE_CHARGER_MAGIC | chip->trickle_charger_setup,
899 chip->trickle_charger_reg);
Wolfram Sangeb86c302012-05-29 15:07:38 -0700900 i2c_smbus_write_byte_data(client, chip->trickle_charger_reg,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700901 DS13XX_TRICKLE_CHARGER_MAGIC |
902 chip->trickle_charger_setup);
903 }
Wolfram Sangeb86c302012-05-29 15:07:38 -0700904
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800905 buf = ds1307->regs;
Ed Swierk30e7b032009-03-31 15:24:56 -0700906 if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
Bertrand Achardbc48b902013-04-29 16:19:26 -0700907 ds1307->read_block_data = ds1307_native_smbus_read_block_data;
908 ds1307->write_block_data = ds1307_native_smbus_write_block_data;
Ed Swierk30e7b032009-03-31 15:24:56 -0700909 } else {
910 ds1307->read_block_data = ds1307_read_block_data;
911 ds1307->write_block_data = ds1307_write_block_data;
912 }
David Brownell045e0e82007-07-17 04:04:55 -0700913
914 switch (ds1307->type) {
915 case ds_1337:
916 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -0700917 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700918 /* get registers that the "rtc" read below won't read... */
Ed Swierk30e7b032009-03-31 15:24:56 -0700919 tmp = ds1307->read_block_data(ds1307->client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800920 DS1337_REG_CONTROL, 2, buf);
David Brownell1abb0dc2006-06-25 05:48:17 -0700921 if (tmp != 2) {
Jingoo Han6df80e22013-04-29 16:19:28 -0700922 dev_dbg(&client->dev, "read error %d\n", tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700923 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -0700924 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -0700925 }
926
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700927 /* oscillator off? turn it on, so clock can tick. */
928 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700929 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
930
David Anders40ce9722012-03-23 15:02:37 -0700931 /*
932 * Using IRQ? Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -0700933 * For some variants, be sure alarms can trigger when we're
934 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700935 */
Wolfram Sangb24a7262012-03-23 15:02:37 -0700936 if (ds1307->client->irq > 0 && chip->alarm) {
Wolfram Sang97f902b2009-06-17 16:26:10 -0700937 ds1307->regs[0] |= DS1337_BIT_INTCN
938 | bbsqi_bitpos[ds1307->type];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700939 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Wolfram Sangb24a7262012-03-23 15:02:37 -0700940
941 want_irq = true;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700942 }
943
944 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
945 ds1307->regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700946
947 /* oscillator fault? clear flag, and warn */
948 if (ds1307->regs[1] & DS1337_BIT_OSF) {
949 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
950 ds1307->regs[1] & ~DS1337_BIT_OSF);
951 dev_warn(&client->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -0700952 }
David Brownell045e0e82007-07-17 04:04:55 -0700953 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -0700954
955 case rx_8025:
956 tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
957 RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
958 if (tmp != 2) {
Jingoo Han6df80e22013-04-29 16:19:28 -0700959 dev_dbg(&client->dev, "read error %d\n", tmp);
Matthias Fuchsa2166852009-03-31 15:24:58 -0700960 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -0700961 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -0700962 }
963
964 /* oscillator off? turn it on, so clock can tick. */
965 if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
966 ds1307->regs[1] |= RX8025_BIT_XST;
967 i2c_smbus_write_byte_data(client,
968 RX8025_REG_CTRL2 << 4 | 0x08,
969 ds1307->regs[1]);
970 dev_warn(&client->dev,
971 "oscillator stop detected - SET TIME!\n");
972 }
973
974 if (ds1307->regs[1] & RX8025_BIT_PON) {
975 ds1307->regs[1] &= ~RX8025_BIT_PON;
976 i2c_smbus_write_byte_data(client,
977 RX8025_REG_CTRL2 << 4 | 0x08,
978 ds1307->regs[1]);
979 dev_warn(&client->dev, "power-on detected\n");
980 }
981
982 if (ds1307->regs[1] & RX8025_BIT_VDET) {
983 ds1307->regs[1] &= ~RX8025_BIT_VDET;
984 i2c_smbus_write_byte_data(client,
985 RX8025_REG_CTRL2 << 4 | 0x08,
986 ds1307->regs[1]);
987 dev_warn(&client->dev, "voltage drop detected\n");
988 }
989
990 /* make sure we are running in 24hour mode */
991 if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
992 u8 hour;
993
994 /* switch to 24 hour mode */
995 i2c_smbus_write_byte_data(client,
996 RX8025_REG_CTRL1 << 4 | 0x08,
997 ds1307->regs[0] |
998 RX8025_BIT_2412);
999
1000 tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
1001 RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
1002 if (tmp != 2) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001003 dev_dbg(&client->dev, "read error %d\n", tmp);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001004 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001005 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001006 }
1007
1008 /* correct hour */
1009 hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
1010 if (hour == 12)
1011 hour = 0;
1012 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1013 hour += 12;
1014
1015 i2c_smbus_write_byte_data(client,
1016 DS1307_REG_HOUR << 4 | 0x08,
1017 hour);
1018 }
1019 break;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001020 case ds_1388:
1021 ds1307->offset = 1; /* Seconds starts at 1 */
1022 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001023 case mcp794xx:
1024 rtc_ops = &mcp794xx_rtc_ops;
Simon Guinot1d1945d2014-04-03 14:49:55 -07001025 if (ds1307->client->irq > 0 && chip->alarm) {
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001026 irq_handler = mcp794xx_irq;
Simon Guinot1d1945d2014-04-03 14:49:55 -07001027 want_irq = true;
1028 }
1029 break;
David Brownell045e0e82007-07-17 04:04:55 -07001030 default:
1031 break;
1032 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001033
1034read_rtc:
1035 /* read RTC registers */
Joakim Tjernlund96fc3a42010-06-29 15:05:34 -07001036 tmp = ds1307->read_block_data(ds1307->client, ds1307->offset, 8, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001037 if (tmp != 8) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001038 dev_dbg(&client->dev, "read error %d\n", tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -07001039 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001040 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001041 }
1042
David Anders40ce9722012-03-23 15:02:37 -07001043 /*
1044 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001045 * specify the extra bits as must-be-zero, but there are
1046 * still a few values that are clearly out-of-range.
1047 */
1048 tmp = ds1307->regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001049 switch (ds1307->type) {
1050 case ds_1307:
David Brownell045e0e82007-07-17 04:04:55 -07001051 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001052 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001053 if (tmp & DS1307_BIT_CH) {
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001054 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
1055 dev_warn(&client->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001056 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001057 }
David Brownell045e0e82007-07-17 04:04:55 -07001058 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001059 case ds_1338:
1060 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001061 if (tmp & DS1307_BIT_CH)
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001062 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
1063
1064 /* oscillator fault? clear flag, and warn */
1065 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
1066 i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
David Brownellbd16f9e2007-07-26 10:41:00 -07001067 ds1307->regs[DS1307_REG_CONTROL]
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001068 & ~DS1338_BIT_OSF);
1069 dev_warn(&client->dev, "SET TIME!\n");
1070 goto read_rtc;
1071 }
David Brownell045e0e82007-07-17 04:04:55 -07001072 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001073 case ds_1340:
1074 /* clock halted? turn it on, so clock can tick. */
1075 if (tmp & DS1340_BIT_nEOSC)
1076 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
1077
1078 tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG);
1079 if (tmp < 0) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001080 dev_dbg(&client->dev, "read error %d\n", tmp);
frederic Rodofcd8db02008-02-06 01:38:55 -08001081 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001082 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001083 }
1084
1085 /* oscillator fault? clear flag, and warn */
1086 if (tmp & DS1340_BIT_OSF) {
1087 i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0);
1088 dev_warn(&client->dev, "SET TIME!\n");
1089 }
1090 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001091 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001092 /* make sure that the backup battery is enabled */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001093 if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
David Anders43fcb812011-11-02 13:37:53 -07001094 i2c_smbus_write_byte_data(client, DS1307_REG_WDAY,
1095 ds1307->regs[DS1307_REG_WDAY]
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001096 | MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001097 }
1098
1099 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001100 if (!(tmp & MCP794XX_BIT_ST)) {
David Anders43fcb812011-11-02 13:37:53 -07001101 i2c_smbus_write_byte_data(client, DS1307_REG_SECS,
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001102 MCP794XX_BIT_ST);
David Anders43fcb812011-11-02 13:37:53 -07001103 dev_warn(&client->dev, "SET TIME!\n");
1104 goto read_rtc;
1105 }
1106
1107 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001108 default:
David Brownell045e0e82007-07-17 04:04:55 -07001109 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001110 }
David Brownell045e0e82007-07-17 04:04:55 -07001111
David Brownell1abb0dc2006-06-25 05:48:17 -07001112 tmp = ds1307->regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001113 switch (ds1307->type) {
1114 case ds_1340:
1115 case m41t00:
David Anders40ce9722012-03-23 15:02:37 -07001116 /*
1117 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001118 * systems that will run through year 2100.
1119 */
1120 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001121 case rx_8025:
1122 break;
David Brownellc065f352007-07-17 04:05:10 -07001123 default:
1124 if (!(tmp & DS1307_BIT_12HR))
1125 break;
1126
David Anders40ce9722012-03-23 15:02:37 -07001127 /*
1128 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001129 * take note...
1130 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001131 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001132 if (tmp == 12)
1133 tmp = 0;
1134 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1135 tmp += 12;
David Brownell1abb0dc2006-06-25 05:48:17 -07001136 i2c_smbus_write_byte_data(client,
Joakim Tjernlund96fc3a42010-06-29 15:05:34 -07001137 ds1307->offset + DS1307_REG_HOUR,
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001138 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001139 }
1140
Simon Guinot5ea73512014-04-03 14:49:54 -07001141 device_set_wakeup_capable(&client->dev, want_irq);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001142 ds1307->rtc = devm_rtc_device_register(&client->dev, client->name,
Simon Guinot1d1945d2014-04-03 14:49:55 -07001143 rtc_ops, THIS_MODULE);
David Brownell1abb0dc2006-06-25 05:48:17 -07001144 if (IS_ERR(ds1307->rtc)) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001145 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001146 }
1147
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001148 if (want_irq) {
Nishanth Menon7abea612015-06-24 11:26:54 -05001149 struct device_node *node = client->dev.of_node;
1150
Nishanth Menonc5983192015-06-23 11:15:11 -05001151 err = devm_request_threaded_irq(&client->dev,
1152 client->irq, NULL, irq_handler,
1153 IRQF_SHARED | IRQF_ONESHOT,
1154 ds1307->rtc->name, client);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001155 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001156 client->irq = 0;
1157 dev_err(&client->dev, "unable to request IRQ!\n");
Nishanth Menon7abea612015-06-24 11:26:54 -05001158 goto no_irq;
1159 }
Anton Vorontsov26b3c012009-12-17 15:27:23 -08001160
Nishanth Menon7abea612015-06-24 11:26:54 -05001161 set_bit(HAS_ALARM, &ds1307->flags);
1162 dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
1163
1164 /* Currently supported by OF code only! */
1165 if (!node)
1166 goto no_irq;
1167
1168 err = of_irq_get(node, 1);
1169 if (err <= 0) {
1170 if (err == -EPROBE_DEFER)
1171 goto exit;
1172 goto no_irq;
1173 }
1174 ds1307->wakeirq = err;
1175
1176 err = dev_pm_set_dedicated_wake_irq(&client->dev,
1177 ds1307->wakeirq);
1178 if (err) {
1179 dev_err(&client->dev, "unable to setup wakeIRQ %d!\n",
1180 err);
1181 goto exit;
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001182 }
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001183 }
1184
Nishanth Menon7abea612015-06-24 11:26:54 -05001185no_irq:
Austin Boyle9eab0a72012-03-23 15:02:38 -07001186 if (chip->nvram_size) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001187
Jingoo Hanedca66d2013-07-03 15:07:05 -07001188 ds1307->nvram = devm_kzalloc(&client->dev,
1189 sizeof(struct bin_attribute),
1190 GFP_KERNEL);
Austin Boyle9eab0a72012-03-23 15:02:38 -07001191 if (!ds1307->nvram) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001192 dev_err(&client->dev, "cannot allocate memory for nvram sysfs\n");
1193 } else {
1194
1195 ds1307->nvram->attr.name = "nvram";
1196 ds1307->nvram->attr.mode = S_IRUGO | S_IWUSR;
1197
1198 sysfs_bin_attr_init(ds1307->nvram);
1199
1200 ds1307->nvram->read = ds1307_nvram_read;
1201 ds1307->nvram->write = ds1307_nvram_write;
1202 ds1307->nvram->size = chip->nvram_size;
1203 ds1307->nvram_offset = chip->nvram_offset;
1204
1205 err = sysfs_create_bin_file(&client->dev.kobj,
1206 ds1307->nvram);
1207 if (err) {
1208 dev_err(&client->dev,
1209 "unable to create sysfs file: %s\n",
1210 ds1307->nvram->attr.name);
1211 } else {
1212 set_bit(HAS_NVRAM, &ds1307->flags);
1213 dev_info(&client->dev, "%zu bytes nvram\n",
1214 ds1307->nvram->size);
1215 }
David Brownell682d73f2007-11-14 16:58:32 -08001216 }
1217 }
1218
David Brownell1abb0dc2006-06-25 05:48:17 -07001219 return 0;
1220
Jingoo Hanedca66d2013-07-03 15:07:05 -07001221exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001222 return err;
1223}
1224
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001225static int ds1307_remove(struct i2c_client *client)
David Brownell1abb0dc2006-06-25 05:48:17 -07001226{
David Anders40ce9722012-03-23 15:02:37 -07001227 struct ds1307 *ds1307 = i2c_get_clientdata(client);
David Brownell1abb0dc2006-06-25 05:48:17 -07001228
Nishanth Menon7abea612015-06-24 11:26:54 -05001229 if (ds1307->wakeirq)
1230 dev_pm_clear_wake_irq(&client->dev);
1231
Jingoo Hanedca66d2013-07-03 15:07:05 -07001232 if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
Austin Boyle9eab0a72012-03-23 15:02:38 -07001233 sysfs_remove_bin_file(&client->dev.kobj, ds1307->nvram);
David Brownell682d73f2007-11-14 16:58:32 -08001234
David Brownell1abb0dc2006-06-25 05:48:17 -07001235 return 0;
1236}
1237
1238static struct i2c_driver ds1307_driver = {
1239 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001240 .name = "rtc-ds1307",
David Brownell1abb0dc2006-06-25 05:48:17 -07001241 },
David Brownellc065f352007-07-17 04:05:10 -07001242 .probe = ds1307_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001243 .remove = ds1307_remove,
Jean Delvare3760f732008-04-29 23:11:40 +02001244 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001245};
1246
Axel Lin0abc9202012-03-23 15:02:31 -07001247module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001248
1249MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1250MODULE_LICENSE("GPL");