blob: f085b487bbe7298c4b6aac8d34c70fe3568abe82 [file] [log] [blame]
David Brownell1abb0dc2006-06-25 05:48:17 -07001/*
2 * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips.
3 *
4 * Copyright (C) 2005 James Chapman (ds1337 core)
5 * Copyright (C) 2006 David Brownell
Matthias Fuchsa2166852009-03-31 15:24:58 -07006 * Copyright (C) 2009 Matthias Fuchs (rx8025 support)
Bertrand Achardbc48b902013-04-29 16:19:26 -07007 * Copyright (C) 2012 Bertrand Achard (nvram access fixes)
David Brownell1abb0dc2006-06-25 05:48:17 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Tin Huynh9c19b892016-11-30 09:57:31 +070014#include <linux/acpi.h>
David Brownell1abb0dc2006-06-25 05:48:17 -070015#include <linux/bcd.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050016#include <linux/i2c.h>
17#include <linux/init.h>
18#include <linux/module.h>
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -030019#include <linux/of_device.h>
Wolfram Sangeb86c302012-05-29 15:07:38 -070020#include <linux/rtc/ds1307.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050021#include <linux/rtc.h>
22#include <linux/slab.h>
23#include <linux/string.h>
Akinobu Mita445c0202016-01-25 00:22:16 +090024#include <linux/hwmon.h>
25#include <linux/hwmon-sysfs.h>
Akinobu Mita6c6ff142016-01-31 23:10:10 +090026#include <linux/clk-provider.h>
David Brownell1abb0dc2006-06-25 05:48:17 -070027
David Anders40ce9722012-03-23 15:02:37 -070028/*
29 * We can't determine type by probing, but if we expect pre-Linux code
David Brownell1abb0dc2006-06-25 05:48:17 -070030 * to have set the chip up as a clock (turning on the oscillator and
31 * setting the date and time), Linux can ignore the non-clock features.
32 * That's a natural job for a factory or repair bench.
David Brownell1abb0dc2006-06-25 05:48:17 -070033 */
34enum ds_type {
David Brownell045e0e82007-07-17 04:04:55 -070035 ds_1307,
36 ds_1337,
37 ds_1338,
38 ds_1339,
39 ds_1340,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -070040 ds_1388,
Wolfram Sang97f902b2009-06-17 16:26:10 -070041 ds_3231,
David Brownell045e0e82007-07-17 04:04:55 -070042 m41t00,
Tomas Novotnyf4199f82014-12-10 15:53:57 -080043 mcp794xx,
Matthias Fuchsa2166852009-03-31 15:24:58 -070044 rx_8025,
Wolfram Sang32d322b2012-03-23 15:02:36 -070045 last_ds_type /* always last */
David Anders40ce9722012-03-23 15:02:37 -070046 /* rs5c372 too? different address... */
David Brownell1abb0dc2006-06-25 05:48:17 -070047};
48
David Brownell1abb0dc2006-06-25 05:48:17 -070049
50/* RTC registers don't differ much, except for the century flag */
51#define DS1307_REG_SECS 0x00 /* 00-59 */
52# define DS1307_BIT_CH 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070053# define DS1340_BIT_nEOSC 0x80
Tomas Novotnyf4199f82014-12-10 15:53:57 -080054# define MCP794XX_BIT_ST 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070055#define DS1307_REG_MIN 0x01 /* 00-59 */
56#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
David Brownellc065f352007-07-17 04:05:10 -070057# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
58# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
David Brownell1abb0dc2006-06-25 05:48:17 -070059# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
60# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
61#define DS1307_REG_WDAY 0x03 /* 01-07 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -080062# define MCP794XX_BIT_VBATEN 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -070063#define DS1307_REG_MDAY 0x04 /* 01-31 */
64#define DS1307_REG_MONTH 0x05 /* 01-12 */
65# define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
66#define DS1307_REG_YEAR 0x06 /* 00-99 */
67
David Anders40ce9722012-03-23 15:02:37 -070068/*
69 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
David Brownell045e0e82007-07-17 04:04:55 -070070 * start at 7, and they differ a LOT. Only control and status matter for
71 * basic RTC date and time functionality; be careful using them.
David Brownell1abb0dc2006-06-25 05:48:17 -070072 */
David Brownell045e0e82007-07-17 04:04:55 -070073#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
David Brownell1abb0dc2006-06-25 05:48:17 -070074# define DS1307_BIT_OUT 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070075# define DS1338_BIT_OSF 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070076# define DS1307_BIT_SQWE 0x10
77# define DS1307_BIT_RS1 0x02
78# define DS1307_BIT_RS0 0x01
79#define DS1337_REG_CONTROL 0x0e
80# define DS1337_BIT_nEOSC 0x80
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070081# define DS1339_BIT_BBSQI 0x20
Wolfram Sang97f902b2009-06-17 16:26:10 -070082# define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
David Brownell1abb0dc2006-06-25 05:48:17 -070083# define DS1337_BIT_RS2 0x10
84# define DS1337_BIT_RS1 0x08
85# define DS1337_BIT_INTCN 0x04
86# define DS1337_BIT_A2IE 0x02
87# define DS1337_BIT_A1IE 0x01
David Brownell045e0e82007-07-17 04:04:55 -070088#define DS1340_REG_CONTROL 0x07
89# define DS1340_BIT_OUT 0x80
90# define DS1340_BIT_FT 0x40
91# define DS1340_BIT_CALIB_SIGN 0x20
92# define DS1340_M_CALIBRATION 0x1f
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070093#define DS1340_REG_FLAG 0x09
94# define DS1340_BIT_OSF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070095#define DS1337_REG_STATUS 0x0f
96# define DS1337_BIT_OSF 0x80
Akinobu Mita6c6ff142016-01-31 23:10:10 +090097# define DS3231_BIT_EN32KHZ 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -070098# define DS1337_BIT_A2I 0x02
99# define DS1337_BIT_A1I 0x01
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700100#define DS1339_REG_ALARM1_SECS 0x07
Wolfram Sangeb86c302012-05-29 15:07:38 -0700101
102#define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
David Brownell1abb0dc2006-06-25 05:48:17 -0700103
Matthias Fuchsa2166852009-03-31 15:24:58 -0700104#define RX8025_REG_CTRL1 0x0e
105# define RX8025_BIT_2412 0x20
106#define RX8025_REG_CTRL2 0x0f
107# define RX8025_BIT_PON 0x10
108# define RX8025_BIT_VDET 0x40
109# define RX8025_BIT_XST 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -0700110
111
112struct ds1307 {
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700113 u8 offset; /* register's offset */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700114 u8 regs[11];
Austin Boyle9eab0a72012-03-23 15:02:38 -0700115 u16 nvram_offset;
116 struct bin_attribute *nvram;
David Brownell1abb0dc2006-06-25 05:48:17 -0700117 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700118 unsigned long flags;
119#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
120#define HAS_ALARM 1 /* bit 1 == irq claimed */
David Brownell045e0e82007-07-17 04:04:55 -0700121 struct i2c_client *client;
David Brownell1abb0dc2006-06-25 05:48:17 -0700122 struct rtc_device *rtc;
Jean Delvare0cc43a12011-01-10 22:11:23 +0100123 s32 (*read_block_data)(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700124 u8 length, u8 *values);
Jean Delvare0cc43a12011-01-10 22:11:23 +0100125 s32 (*write_block_data)(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700126 u8 length, const u8 *values);
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900127#ifdef CONFIG_COMMON_CLK
128 struct clk_hw clks[2];
129#endif
David Brownell1abb0dc2006-06-25 05:48:17 -0700130};
131
David Brownell045e0e82007-07-17 04:04:55 -0700132struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700133 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700134 u16 nvram_offset;
135 u16 nvram_size;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700136 u16 trickle_charger_reg;
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700137 u8 trickle_charger_setup;
138 u8 (*do_trickle_setup)(struct i2c_client *, uint32_t, bool);
David Brownell045e0e82007-07-17 04:04:55 -0700139};
140
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700141static u8 do_trickle_setup_ds1339(struct i2c_client *,
142 uint32_t ohms, bool diode);
143
144static struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700145 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700146 .nvram_offset = 8,
147 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700148 },
149 [ds_1337] = {
150 .alarm = 1,
151 },
152 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700153 .nvram_offset = 8,
154 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700155 },
156 [ds_1339] = {
157 .alarm = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700158 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700159 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700160 },
161 [ds_1340] = {
162 .trickle_charger_reg = 0x08,
163 },
164 [ds_1388] = {
165 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700166 },
167 [ds_3231] = {
168 .alarm = 1,
169 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800170 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700171 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700172 /* this is battery backed SRAM */
173 .nvram_offset = 0x20,
174 .nvram_size = 0x40,
175 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700176};
David Brownell045e0e82007-07-17 04:04:55 -0700177
Jean Delvare3760f732008-04-29 23:11:40 +0200178static const struct i2c_device_id ds1307_id[] = {
179 { "ds1307", ds_1307 },
180 { "ds1337", ds_1337 },
181 { "ds1338", ds_1338 },
182 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700183 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200184 { "ds1340", ds_1340 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700185 { "ds3231", ds_3231 },
Jean Delvare3760f732008-04-29 23:11:40 +0200186 { "m41t00", m41t00 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800187 { "mcp7940x", mcp794xx },
188 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700189 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700190 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200191 { "isl12057", ds_1337 },
Jean Delvare3760f732008-04-29 23:11:40 +0200192 { }
193};
194MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700195
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300196#ifdef CONFIG_OF
197static const struct of_device_id ds1307_of_match[] = {
198 {
199 .compatible = "dallas,ds1307",
200 .data = (void *)ds_1307
201 },
202 {
203 .compatible = "dallas,ds1337",
204 .data = (void *)ds_1337
205 },
206 {
207 .compatible = "dallas,ds1338",
208 .data = (void *)ds_1338
209 },
210 {
211 .compatible = "dallas,ds1339",
212 .data = (void *)ds_1339
213 },
214 {
215 .compatible = "dallas,ds1388",
216 .data = (void *)ds_1388
217 },
218 {
219 .compatible = "dallas,ds1340",
220 .data = (void *)ds_1340
221 },
222 {
223 .compatible = "maxim,ds3231",
224 .data = (void *)ds_3231
225 },
226 {
227 .compatible = "st,m41t00",
228 .data = (void *)m41t00
229 },
230 {
231 .compatible = "microchip,mcp7940x",
232 .data = (void *)mcp794xx
233 },
234 {
235 .compatible = "microchip,mcp7941x",
236 .data = (void *)mcp794xx
237 },
238 {
239 .compatible = "pericom,pt7c4338",
240 .data = (void *)ds_1307
241 },
242 {
243 .compatible = "epson,rx8025",
244 .data = (void *)rx_8025
245 },
246 {
247 .compatible = "isil,isl12057",
248 .data = (void *)ds_1337
249 },
250 { }
251};
252MODULE_DEVICE_TABLE(of, ds1307_of_match);
253#endif
254
Tin Huynh9c19b892016-11-30 09:57:31 +0700255#ifdef CONFIG_ACPI
256static const struct acpi_device_id ds1307_acpi_ids[] = {
257 { .id = "DS1307", .driver_data = ds_1307 },
258 { .id = "DS1337", .driver_data = ds_1337 },
259 { .id = "DS1338", .driver_data = ds_1338 },
260 { .id = "DS1339", .driver_data = ds_1339 },
261 { .id = "DS1388", .driver_data = ds_1388 },
262 { .id = "DS1340", .driver_data = ds_1340 },
263 { .id = "DS3231", .driver_data = ds_3231 },
264 { .id = "M41T00", .driver_data = m41t00 },
265 { .id = "MCP7940X", .driver_data = mcp794xx },
266 { .id = "MCP7941X", .driver_data = mcp794xx },
267 { .id = "PT7C4338", .driver_data = ds_1307 },
268 { .id = "RX8025", .driver_data = rx_8025 },
269 { .id = "ISL12057", .driver_data = ds_1337 },
270 { }
271};
272MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
273#endif
274
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700275/*----------------------------------------------------------------------*/
276
Ed Swierk30e7b032009-03-31 15:24:56 -0700277#define BLOCK_DATA_MAX_TRIES 10
278
Jean Delvare0cc43a12011-01-10 22:11:23 +0100279static s32 ds1307_read_block_data_once(const struct i2c_client *client,
280 u8 command, u8 length, u8 *values)
Ed Swierk30e7b032009-03-31 15:24:56 -0700281{
282 s32 i, data;
283
284 for (i = 0; i < length; i++) {
285 data = i2c_smbus_read_byte_data(client, command + i);
286 if (data < 0)
287 return data;
288 values[i] = data;
289 }
290 return i;
291}
292
Jean Delvare0cc43a12011-01-10 22:11:23 +0100293static s32 ds1307_read_block_data(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700294 u8 length, u8 *values)
295{
Bertrand Achardbc48b902013-04-29 16:19:26 -0700296 u8 oldvalues[255];
Ed Swierk30e7b032009-03-31 15:24:56 -0700297 s32 ret;
298 int tries = 0;
299
300 dev_dbg(&client->dev, "ds1307_read_block_data (length=%d)\n", length);
301 ret = ds1307_read_block_data_once(client, command, length, values);
302 if (ret < 0)
303 return ret;
304 do {
305 if (++tries > BLOCK_DATA_MAX_TRIES) {
306 dev_err(&client->dev,
307 "ds1307_read_block_data failed\n");
308 return -EIO;
309 }
310 memcpy(oldvalues, values, length);
311 ret = ds1307_read_block_data_once(client, command, length,
312 values);
313 if (ret < 0)
314 return ret;
315 } while (memcmp(oldvalues, values, length));
316 return length;
317}
318
Jean Delvare0cc43a12011-01-10 22:11:23 +0100319static s32 ds1307_write_block_data(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700320 u8 length, const u8 *values)
321{
Bertrand Achardbc48b902013-04-29 16:19:26 -0700322 u8 currvalues[255];
Ed Swierk30e7b032009-03-31 15:24:56 -0700323 int tries = 0;
324
325 dev_dbg(&client->dev, "ds1307_write_block_data (length=%d)\n", length);
326 do {
327 s32 i, ret;
328
329 if (++tries > BLOCK_DATA_MAX_TRIES) {
330 dev_err(&client->dev,
331 "ds1307_write_block_data failed\n");
332 return -EIO;
333 }
334 for (i = 0; i < length; i++) {
335 ret = i2c_smbus_write_byte_data(client, command + i,
336 values[i]);
337 if (ret < 0)
338 return ret;
339 }
340 ret = ds1307_read_block_data_once(client, command, length,
341 currvalues);
342 if (ret < 0)
343 return ret;
344 } while (memcmp(currvalues, values, length));
345 return length;
346}
347
348/*----------------------------------------------------------------------*/
349
Bertrand Achardbc48b902013-04-29 16:19:26 -0700350/* These RTC devices are not designed to be connected to a SMbus adapter.
351 SMbus limits block operations length to 32 bytes, whereas it's not
352 limited on I2C buses. As a result, accesses may exceed 32 bytes;
353 in that case, split them into smaller blocks */
354
355static s32 ds1307_native_smbus_write_block_data(const struct i2c_client *client,
356 u8 command, u8 length, const u8 *values)
357{
358 u8 suboffset = 0;
359
Nicolas Boullis1d879512016-04-03 00:10:37 +0200360 if (length <= I2C_SMBUS_BLOCK_MAX) {
361 s32 retval = i2c_smbus_write_i2c_block_data(client,
Bertrand Achardbc48b902013-04-29 16:19:26 -0700362 command, length, values);
Nicolas Boullis1d879512016-04-03 00:10:37 +0200363 if (retval < 0)
364 return retval;
365 return length;
366 }
Bertrand Achardbc48b902013-04-29 16:19:26 -0700367
368 while (suboffset < length) {
369 s32 retval = i2c_smbus_write_i2c_block_data(client,
370 command + suboffset,
371 min(I2C_SMBUS_BLOCK_MAX, length - suboffset),
372 values + suboffset);
373 if (retval < 0)
374 return retval;
375
376 suboffset += I2C_SMBUS_BLOCK_MAX;
377 }
378 return length;
379}
380
381static s32 ds1307_native_smbus_read_block_data(const struct i2c_client *client,
382 u8 command, u8 length, u8 *values)
383{
384 u8 suboffset = 0;
385
386 if (length <= I2C_SMBUS_BLOCK_MAX)
387 return i2c_smbus_read_i2c_block_data(client,
388 command, length, values);
389
390 while (suboffset < length) {
391 s32 retval = i2c_smbus_read_i2c_block_data(client,
392 command + suboffset,
393 min(I2C_SMBUS_BLOCK_MAX, length - suboffset),
394 values + suboffset);
395 if (retval < 0)
396 return retval;
397
398 suboffset += I2C_SMBUS_BLOCK_MAX;
399 }
400 return length;
401}
402
403/*----------------------------------------------------------------------*/
404
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700405/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700406 * The ds1337 and ds1339 both have two alarms, but we only use the first
407 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
408 * signal; ds1339 chips have only one alarm signal.
409 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500410static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700411{
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500412 struct i2c_client *client = dev_id;
413 struct ds1307 *ds1307 = i2c_get_clientdata(client);
414 struct mutex *lock = &ds1307->rtc->ops_lock;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700415 int stat, control;
416
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700417 mutex_lock(lock);
418 stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
419 if (stat < 0)
420 goto out;
421
422 if (stat & DS1337_BIT_A1I) {
423 stat &= ~DS1337_BIT_A1I;
424 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, stat);
425
426 control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
427 if (control < 0)
428 goto out;
429
430 control &= ~DS1337_BIT_A1IE;
431 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
432
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700433 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700434 }
435
436out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700437 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700438
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700439 return IRQ_HANDLED;
440}
441
442/*----------------------------------------------------------------------*/
443
David Brownell1abb0dc2006-06-25 05:48:17 -0700444static int ds1307_get_time(struct device *dev, struct rtc_time *t)
445{
446 struct ds1307 *ds1307 = dev_get_drvdata(dev);
447 int tmp;
448
David Brownell045e0e82007-07-17 04:04:55 -0700449 /* read the RTC date and time registers all at once */
Ed Swierk30e7b032009-03-31 15:24:56 -0700450 tmp = ds1307->read_block_data(ds1307->client,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700451 ds1307->offset, 7, ds1307->regs);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800452 if (tmp != 7) {
David Brownell1abb0dc2006-06-25 05:48:17 -0700453 dev_err(dev, "%s error %d\n", "read", tmp);
454 return -EIO;
455 }
456
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800457 dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700458
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700459 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
460 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700461 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700462 t->tm_hour = bcd2bin(tmp);
463 t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
464 t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700465 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700466 t->tm_mon = bcd2bin(tmp) - 1;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700467 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700468
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200469#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
470 switch (ds1307->type) {
471 case ds_1337:
472 case ds_1339:
473 case ds_3231:
474 if (ds1307->regs[DS1307_REG_MONTH] & DS1337_BIT_CENTURY)
475 t->tm_year += 100;
476 break;
477 case ds_1340:
478 if (ds1307->regs[DS1307_REG_HOUR] & DS1340_BIT_CENTURY)
479 t->tm_year += 100;
480 break;
481 default:
482 break;
483 }
484#endif
485
David Brownell1abb0dc2006-06-25 05:48:17 -0700486 dev_dbg(dev, "%s secs=%d, mins=%d, "
487 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
488 "read", t->tm_sec, t->tm_min,
489 t->tm_hour, t->tm_mday,
490 t->tm_mon, t->tm_year, t->tm_wday);
491
David Brownell045e0e82007-07-17 04:04:55 -0700492 /* initial clock setting can be undefined */
493 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700494}
495
496static int ds1307_set_time(struct device *dev, struct rtc_time *t)
497{
498 struct ds1307 *ds1307 = dev_get_drvdata(dev);
499 int result;
500 int tmp;
501 u8 *buf = ds1307->regs;
502
503 dev_dbg(dev, "%s secs=%d, mins=%d, "
504 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400505 "write", t->tm_sec, t->tm_min,
506 t->tm_hour, t->tm_mday,
507 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700508
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200509#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
510 if (t->tm_year < 100)
511 return -EINVAL;
512
513 switch (ds1307->type) {
514 case ds_1337:
515 case ds_1339:
516 case ds_3231:
517 case ds_1340:
518 if (t->tm_year > 299)
519 return -EINVAL;
520 default:
521 if (t->tm_year > 199)
522 return -EINVAL;
523 break;
524 }
525#else
526 if (t->tm_year < 100 || t->tm_year > 199)
527 return -EINVAL;
528#endif
529
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700530 buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
531 buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
532 buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
533 buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
534 buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
535 buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700536
537 /* assume 20YY not 19YY */
538 tmp = t->tm_year - 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700539 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700540
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700541 switch (ds1307->type) {
542 case ds_1337:
543 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -0700544 case ds_3231:
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200545 if (t->tm_year > 199)
546 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700547 break;
548 case ds_1340:
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200549 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN;
550 if (t->tm_year > 199)
551 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700552 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800553 case mcp794xx:
David Anders40ce9722012-03-23 15:02:37 -0700554 /*
555 * these bits were cleared when preparing the date/time
556 * values and need to be set again before writing the
557 * buffer out to the device.
558 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800559 buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
560 buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
David Anders43fcb812011-11-02 13:37:53 -0700561 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700562 default:
563 break;
564 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700565
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800566 dev_dbg(dev, "%s: %7ph\n", "write", buf);
David Brownell1abb0dc2006-06-25 05:48:17 -0700567
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700568 result = ds1307->write_block_data(ds1307->client,
569 ds1307->offset, 7, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800570 if (result < 0) {
571 dev_err(dev, "%s error %d\n", "write", result);
572 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700573 }
574 return 0;
575}
576
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800577static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700578{
579 struct i2c_client *client = to_i2c_client(dev);
580 struct ds1307 *ds1307 = i2c_get_clientdata(client);
581 int ret;
582
583 if (!test_bit(HAS_ALARM, &ds1307->flags))
584 return -EINVAL;
585
586 /* read all ALARM1, ALARM2, and status registers at once */
Ed Swierk30e7b032009-03-31 15:24:56 -0700587 ret = ds1307->read_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800588 DS1339_REG_ALARM1_SECS, 9, ds1307->regs);
589 if (ret != 9) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700590 dev_err(dev, "%s error %d\n", "alarm read", ret);
591 return -EIO;
592 }
593
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100594 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
595 &ds1307->regs[0], &ds1307->regs[4], &ds1307->regs[7]);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700596
David Anders40ce9722012-03-23 15:02:37 -0700597 /*
598 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700599 * and that all four fields are checked matches
600 */
601 t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
602 t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
603 t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
604 t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700605
606 /* ... and status */
607 t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
608 t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
609
610 dev_dbg(dev, "%s secs=%d, mins=%d, "
611 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
612 "alarm read", t->time.tm_sec, t->time.tm_min,
613 t->time.tm_hour, t->time.tm_mday,
614 t->enabled, t->pending);
615
616 return 0;
617}
618
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800619static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700620{
David Anders40ce9722012-03-23 15:02:37 -0700621 struct i2c_client *client = to_i2c_client(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700622 struct ds1307 *ds1307 = i2c_get_clientdata(client);
623 unsigned char *buf = ds1307->regs;
624 u8 control, status;
625 int ret;
626
627 if (!test_bit(HAS_ALARM, &ds1307->flags))
628 return -EINVAL;
629
630 dev_dbg(dev, "%s secs=%d, mins=%d, "
631 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
632 "alarm set", t->time.tm_sec, t->time.tm_min,
633 t->time.tm_hour, t->time.tm_mday,
634 t->enabled, t->pending);
635
636 /* read current status of both alarms and the chip */
Ed Swierk30e7b032009-03-31 15:24:56 -0700637 ret = ds1307->read_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800638 DS1339_REG_ALARM1_SECS, 9, buf);
639 if (ret != 9) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700640 dev_err(dev, "%s error %d\n", "alarm write", ret);
641 return -EIO;
642 }
643 control = ds1307->regs[7];
644 status = ds1307->regs[8];
645
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100646 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
647 &ds1307->regs[0], &ds1307->regs[4], control, status);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700648
649 /* set ALARM1, using 24 hour and day-of-month modes */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700650 buf[0] = bin2bcd(t->time.tm_sec);
651 buf[1] = bin2bcd(t->time.tm_min);
652 buf[2] = bin2bcd(t->time.tm_hour);
653 buf[3] = bin2bcd(t->time.tm_mday);
654
655 /* set ALARM2 to non-garbage */
656 buf[4] = 0;
657 buf[5] = 0;
658 buf[6] = 0;
659
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200660 /* disable alarms */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700661 buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700662 buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
663
Ed Swierk30e7b032009-03-31 15:24:56 -0700664 ret = ds1307->write_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800665 DS1339_REG_ALARM1_SECS, 9, buf);
666 if (ret < 0) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700667 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800668 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700669 }
670
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200671 /* optionally enable ALARM1 */
672 if (t->enabled) {
673 dev_dbg(dev, "alarm IRQ armed\n");
674 buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
675 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, buf[7]);
676 }
677
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700678 return 0;
679}
680
John Stultz16380c12011-02-02 17:02:41 -0800681static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700682{
683 struct i2c_client *client = to_i2c_client(dev);
684 struct ds1307 *ds1307 = i2c_get_clientdata(client);
685 int ret;
686
John Stultz16380c12011-02-02 17:02:41 -0800687 if (!test_bit(HAS_ALARM, &ds1307->flags))
688 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700689
John Stultz16380c12011-02-02 17:02:41 -0800690 ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
691 if (ret < 0)
692 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700693
John Stultz16380c12011-02-02 17:02:41 -0800694 if (enabled)
695 ret |= DS1337_BIT_A1IE;
696 else
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700697 ret &= ~DS1337_BIT_A1IE;
698
John Stultz16380c12011-02-02 17:02:41 -0800699 ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, ret);
700 if (ret < 0)
701 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700702
703 return 0;
704}
705
David Brownellff8371a2006-09-30 23:28:17 -0700706static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700707 .read_time = ds1307_get_time,
708 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800709 .read_alarm = ds1337_read_alarm,
710 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800711 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700712};
713
David Brownell682d73f2007-11-14 16:58:32 -0800714/*----------------------------------------------------------------------*/
715
Simon Guinot1d1945d2014-04-03 14:49:55 -0700716/*
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800717 * Alarm support for mcp794xx devices.
Simon Guinot1d1945d2014-04-03 14:49:55 -0700718 */
719
Keerthye29385f2016-06-01 16:19:07 +0530720#define MCP794XX_REG_WEEKDAY 0x3
721#define MCP794XX_REG_WEEKDAY_WDAY_MASK 0x7
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800722#define MCP794XX_REG_CONTROL 0x07
723# define MCP794XX_BIT_ALM0_EN 0x10
724# define MCP794XX_BIT_ALM1_EN 0x20
725#define MCP794XX_REG_ALARM0_BASE 0x0a
726#define MCP794XX_REG_ALARM0_CTRL 0x0d
727#define MCP794XX_REG_ALARM1_BASE 0x11
728#define MCP794XX_REG_ALARM1_CTRL 0x14
729# define MCP794XX_BIT_ALMX_IF (1 << 3)
730# define MCP794XX_BIT_ALMX_C0 (1 << 4)
731# define MCP794XX_BIT_ALMX_C1 (1 << 5)
732# define MCP794XX_BIT_ALMX_C2 (1 << 6)
733# define MCP794XX_BIT_ALMX_POL (1 << 7)
734# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
735 MCP794XX_BIT_ALMX_C1 | \
736 MCP794XX_BIT_ALMX_C2)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700737
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500738static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700739{
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500740 struct i2c_client *client = dev_id;
741 struct ds1307 *ds1307 = i2c_get_clientdata(client);
742 struct mutex *lock = &ds1307->rtc->ops_lock;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700743 int reg, ret;
744
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500745 mutex_lock(lock);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700746
747 /* Check and clear alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800748 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_ALARM0_CTRL);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700749 if (reg < 0)
750 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800751 if (!(reg & MCP794XX_BIT_ALMX_IF))
Simon Guinot1d1945d2014-04-03 14:49:55 -0700752 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800753 reg &= ~MCP794XX_BIT_ALMX_IF;
754 ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_ALARM0_CTRL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700755 if (ret < 0)
756 goto out;
757
758 /* Disable alarm 0. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800759 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700760 if (reg < 0)
761 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800762 reg &= ~MCP794XX_BIT_ALM0_EN;
763 ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700764 if (ret < 0)
765 goto out;
766
767 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
768
769out:
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500770 mutex_unlock(lock);
771
772 return IRQ_HANDLED;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700773}
774
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800775static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700776{
777 struct i2c_client *client = to_i2c_client(dev);
778 struct ds1307 *ds1307 = i2c_get_clientdata(client);
779 u8 *regs = ds1307->regs;
780 int ret;
781
782 if (!test_bit(HAS_ALARM, &ds1307->flags))
783 return -EINVAL;
784
785 /* Read control and alarm 0 registers. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800786 ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700787 if (ret < 0)
788 return ret;
789
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800790 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700791
792 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
793 t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f);
794 t->time.tm_min = bcd2bin(ds1307->regs[4] & 0x7f);
795 t->time.tm_hour = bcd2bin(ds1307->regs[5] & 0x3f);
796 t->time.tm_wday = bcd2bin(ds1307->regs[6] & 0x7) - 1;
797 t->time.tm_mday = bcd2bin(ds1307->regs[7] & 0x3f);
798 t->time.tm_mon = bcd2bin(ds1307->regs[8] & 0x1f) - 1;
799 t->time.tm_year = -1;
800 t->time.tm_yday = -1;
801 t->time.tm_isdst = -1;
802
803 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
804 "enabled=%d polarity=%d irq=%d match=%d\n", __func__,
805 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
806 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800807 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL),
808 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF),
809 (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700810
811 return 0;
812}
813
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800814static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700815{
816 struct i2c_client *client = to_i2c_client(dev);
817 struct ds1307 *ds1307 = i2c_get_clientdata(client);
818 unsigned char *regs = ds1307->regs;
819 int ret;
820
821 if (!test_bit(HAS_ALARM, &ds1307->flags))
822 return -EINVAL;
823
824 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
825 "enabled=%d pending=%d\n", __func__,
826 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
827 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
828 t->enabled, t->pending);
829
830 /* Read control and alarm 0 registers. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800831 ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700832 if (ret < 0)
833 return ret;
834
835 /* Set alarm 0, using 24-hour and day-of-month modes. */
836 regs[3] = bin2bcd(t->time.tm_sec);
837 regs[4] = bin2bcd(t->time.tm_min);
838 regs[5] = bin2bcd(t->time.tm_hour);
Tero Kristo62c8c202015-10-23 09:29:57 +0300839 regs[6] = bin2bcd(t->time.tm_wday + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700840 regs[7] = bin2bcd(t->time.tm_mday);
Tero Kristo62c8c202015-10-23 09:29:57 +0300841 regs[8] = bin2bcd(t->time.tm_mon + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700842
843 /* Clear the alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800844 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700845 /* Set alarm match: second, minute, hour, day, date, month. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800846 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
Nishanth Menone3edd672015-04-20 19:51:34 -0500847 /* Disable interrupt. We will not enable until completely programmed */
848 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700849
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800850 ret = ds1307->write_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700851 if (ret < 0)
852 return ret;
853
Nishanth Menone3edd672015-04-20 19:51:34 -0500854 if (!t->enabled)
855 return 0;
856 regs[0] |= MCP794XX_BIT_ALM0_EN;
857 return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, regs[0]);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700858}
859
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800860static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700861{
862 struct i2c_client *client = to_i2c_client(dev);
863 struct ds1307 *ds1307 = i2c_get_clientdata(client);
864 int reg;
865
866 if (!test_bit(HAS_ALARM, &ds1307->flags))
867 return -EINVAL;
868
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800869 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700870 if (reg < 0)
871 return reg;
872
873 if (enabled)
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800874 reg |= MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700875 else
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800876 reg &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700877
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800878 return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700879}
880
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800881static const struct rtc_class_ops mcp794xx_rtc_ops = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700882 .read_time = ds1307_get_time,
883 .set_time = ds1307_set_time,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800884 .read_alarm = mcp794xx_read_alarm,
885 .set_alarm = mcp794xx_set_alarm,
886 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
Simon Guinot1d1945d2014-04-03 14:49:55 -0700887};
888
889/*----------------------------------------------------------------------*/
890
David Brownell682d73f2007-11-14 16:58:32 -0800891static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700892ds1307_nvram_read(struct file *filp, struct kobject *kobj,
893 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800894 char *buf, loff_t off, size_t count)
895{
896 struct i2c_client *client;
897 struct ds1307 *ds1307;
David Brownell682d73f2007-11-14 16:58:32 -0800898 int result;
899
frederic Rodofcd8db02008-02-06 01:38:55 -0800900 client = kobj_to_i2c_client(kobj);
David Brownell682d73f2007-11-14 16:58:32 -0800901 ds1307 = i2c_get_clientdata(client);
902
Austin Boyle9eab0a72012-03-23 15:02:38 -0700903 result = ds1307->read_block_data(client, ds1307->nvram_offset + off,
904 count, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800905 if (result < 0)
David Brownell682d73f2007-11-14 16:58:32 -0800906 dev_err(&client->dev, "%s error %d\n", "nvram read", result);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800907 return result;
David Brownell682d73f2007-11-14 16:58:32 -0800908}
909
910static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700911ds1307_nvram_write(struct file *filp, struct kobject *kobj,
912 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800913 char *buf, loff_t off, size_t count)
914{
915 struct i2c_client *client;
Ed Swierk30e7b032009-03-31 15:24:56 -0700916 struct ds1307 *ds1307;
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800917 int result;
David Brownell682d73f2007-11-14 16:58:32 -0800918
frederic Rodofcd8db02008-02-06 01:38:55 -0800919 client = kobj_to_i2c_client(kobj);
Ed Swierk30e7b032009-03-31 15:24:56 -0700920 ds1307 = i2c_get_clientdata(client);
David Brownell682d73f2007-11-14 16:58:32 -0800921
Austin Boyle9eab0a72012-03-23 15:02:38 -0700922 result = ds1307->write_block_data(client, ds1307->nvram_offset + off,
923 count, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800924 if (result < 0) {
925 dev_err(&client->dev, "%s error %d\n", "nvram write", result);
926 return result;
927 }
928 return count;
David Brownell682d73f2007-11-14 16:58:32 -0800929}
930
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700931
David Brownell682d73f2007-11-14 16:58:32 -0800932/*----------------------------------------------------------------------*/
933
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700934static u8 do_trickle_setup_ds1339(struct i2c_client *client,
935 uint32_t ohms, bool diode)
936{
937 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
938 DS1307_TRICKLE_CHARGER_NO_DIODE;
939
940 switch (ohms) {
941 case 250:
942 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
943 break;
944 case 2000:
945 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
946 break;
947 case 4000:
948 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
949 break;
950 default:
951 dev_warn(&client->dev,
952 "Unsupported ohm value %u in dt\n", ohms);
953 return 0;
954 }
955 return setup;
956}
957
Tin Huynh9c19b892016-11-30 09:57:31 +0700958static void ds1307_trickle_init(struct i2c_client *client,
959 struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700960{
961 uint32_t ohms = 0;
962 bool diode = true;
963
964 if (!chip->do_trickle_setup)
965 goto out;
Tin Huynh9c19b892016-11-30 09:57:31 +0700966 if (device_property_read_u32(&client->dev, "trickle-resistor-ohms", &ohms))
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700967 goto out;
Tin Huynh9c19b892016-11-30 09:57:31 +0700968 if (device_property_read_bool(&client->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700969 diode = false;
970 chip->trickle_charger_setup = chip->do_trickle_setup(client,
971 ohms, diode);
972out:
973 return;
974}
975
Akinobu Mita445c0202016-01-25 00:22:16 +0900976/*----------------------------------------------------------------------*/
977
978#ifdef CONFIG_RTC_DRV_DS1307_HWMON
979
980/*
981 * Temperature sensor support for ds3231 devices.
982 */
983
984#define DS3231_REG_TEMPERATURE 0x11
985
986/*
987 * A user-initiated temperature conversion is not started by this function,
988 * so the temperature is updated once every 64 seconds.
989 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +0900990static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +0900991{
992 struct ds1307 *ds1307 = dev_get_drvdata(dev);
993 u8 temp_buf[2];
994 s16 temp;
995 int ret;
996
997 ret = ds1307->read_block_data(ds1307->client, DS3231_REG_TEMPERATURE,
998 sizeof(temp_buf), temp_buf);
999 if (ret < 0)
1000 return ret;
1001 if (ret != sizeof(temp_buf))
1002 return -EIO;
1003
1004 /*
1005 * Temperature is represented as a 10-bit code with a resolution of
1006 * 0.25 degree celsius and encoded in two's complement format.
1007 */
1008 temp = (temp_buf[0] << 8) | temp_buf[1];
1009 temp >>= 6;
1010 *mC = temp * 250;
1011
1012 return 0;
1013}
1014
1015static ssize_t ds3231_hwmon_show_temp(struct device *dev,
1016 struct device_attribute *attr, char *buf)
1017{
1018 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001019 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001020
1021 ret = ds3231_hwmon_read_temp(dev, &temp);
1022 if (ret)
1023 return ret;
1024
1025 return sprintf(buf, "%d\n", temp);
1026}
1027static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ds3231_hwmon_show_temp,
1028 NULL, 0);
1029
1030static struct attribute *ds3231_hwmon_attrs[] = {
1031 &sensor_dev_attr_temp1_input.dev_attr.attr,
1032 NULL,
1033};
1034ATTRIBUTE_GROUPS(ds3231_hwmon);
1035
1036static void ds1307_hwmon_register(struct ds1307 *ds1307)
1037{
1038 struct device *dev;
1039
1040 if (ds1307->type != ds_3231)
1041 return;
1042
1043 dev = devm_hwmon_device_register_with_groups(&ds1307->client->dev,
1044 ds1307->client->name,
1045 ds1307, ds3231_hwmon_groups);
1046 if (IS_ERR(dev)) {
1047 dev_warn(&ds1307->client->dev,
1048 "unable to register hwmon device %ld\n", PTR_ERR(dev));
1049 }
1050}
1051
1052#else
1053
1054static void ds1307_hwmon_register(struct ds1307 *ds1307)
1055{
1056}
1057
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001058#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1059
1060/*----------------------------------------------------------------------*/
1061
1062/*
1063 * Square-wave output support for DS3231
1064 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1065 */
1066#ifdef CONFIG_COMMON_CLK
1067
1068enum {
1069 DS3231_CLK_SQW = 0,
1070 DS3231_CLK_32KHZ,
1071};
1072
1073#define clk_sqw_to_ds1307(clk) \
1074 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1075#define clk_32khz_to_ds1307(clk) \
1076 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1077
1078static int ds3231_clk_sqw_rates[] = {
1079 1,
1080 1024,
1081 4096,
1082 8192,
1083};
1084
1085static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1086{
1087 struct i2c_client *client = ds1307->client;
1088 struct mutex *lock = &ds1307->rtc->ops_lock;
1089 int control;
1090 int ret;
1091
1092 mutex_lock(lock);
1093
1094 control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
1095 if (control < 0) {
1096 ret = control;
1097 goto out;
1098 }
1099
1100 control &= ~mask;
1101 control |= value;
1102
1103 ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
1104out:
1105 mutex_unlock(lock);
1106
1107 return ret;
1108}
1109
1110static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1111 unsigned long parent_rate)
1112{
1113 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1114 int control;
1115 int rate_sel = 0;
1116
1117 control = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_CONTROL);
1118 if (control < 0)
1119 return control;
1120 if (control & DS1337_BIT_RS1)
1121 rate_sel += 1;
1122 if (control & DS1337_BIT_RS2)
1123 rate_sel += 2;
1124
1125 return ds3231_clk_sqw_rates[rate_sel];
1126}
1127
1128static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
1129 unsigned long *prate)
1130{
1131 int i;
1132
1133 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1134 if (ds3231_clk_sqw_rates[i] <= rate)
1135 return ds3231_clk_sqw_rates[i];
1136 }
1137
1138 return 0;
1139}
1140
1141static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
1142 unsigned long parent_rate)
1143{
1144 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1145 int control = 0;
1146 int rate_sel;
1147
1148 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1149 rate_sel++) {
1150 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1151 break;
1152 }
1153
1154 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1155 return -EINVAL;
1156
1157 if (rate_sel & 1)
1158 control |= DS1337_BIT_RS1;
1159 if (rate_sel & 2)
1160 control |= DS1337_BIT_RS2;
1161
1162 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1163 control);
1164}
1165
1166static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1167{
1168 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1169
1170 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1171}
1172
1173static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1174{
1175 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1176
1177 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1178}
1179
1180static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1181{
1182 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1183 int control;
1184
1185 control = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_CONTROL);
1186 if (control < 0)
1187 return control;
1188
1189 return !(control & DS1337_BIT_INTCN);
1190}
1191
1192static const struct clk_ops ds3231_clk_sqw_ops = {
1193 .prepare = ds3231_clk_sqw_prepare,
1194 .unprepare = ds3231_clk_sqw_unprepare,
1195 .is_prepared = ds3231_clk_sqw_is_prepared,
1196 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1197 .round_rate = ds3231_clk_sqw_round_rate,
1198 .set_rate = ds3231_clk_sqw_set_rate,
1199};
1200
1201static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
1202 unsigned long parent_rate)
1203{
1204 return 32768;
1205}
1206
1207static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1208{
1209 struct i2c_client *client = ds1307->client;
1210 struct mutex *lock = &ds1307->rtc->ops_lock;
1211 int status;
1212 int ret;
1213
1214 mutex_lock(lock);
1215
1216 status = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
1217 if (status < 0) {
1218 ret = status;
1219 goto out;
1220 }
1221
1222 if (enable)
1223 status |= DS3231_BIT_EN32KHZ;
1224 else
1225 status &= ~DS3231_BIT_EN32KHZ;
1226
1227 ret = i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, status);
1228out:
1229 mutex_unlock(lock);
1230
1231 return ret;
1232}
1233
1234static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1235{
1236 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1237
1238 return ds3231_clk_32khz_control(ds1307, true);
1239}
1240
1241static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1242{
1243 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1244
1245 ds3231_clk_32khz_control(ds1307, false);
1246}
1247
1248static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1249{
1250 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1251 int status;
1252
1253 status = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_STATUS);
1254 if (status < 0)
1255 return status;
1256
1257 return !!(status & DS3231_BIT_EN32KHZ);
1258}
1259
1260static const struct clk_ops ds3231_clk_32khz_ops = {
1261 .prepare = ds3231_clk_32khz_prepare,
1262 .unprepare = ds3231_clk_32khz_unprepare,
1263 .is_prepared = ds3231_clk_32khz_is_prepared,
1264 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1265};
1266
1267static struct clk_init_data ds3231_clks_init[] = {
1268 [DS3231_CLK_SQW] = {
1269 .name = "ds3231_clk_sqw",
1270 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001271 },
1272 [DS3231_CLK_32KHZ] = {
1273 .name = "ds3231_clk_32khz",
1274 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001275 },
1276};
1277
1278static int ds3231_clks_register(struct ds1307 *ds1307)
1279{
1280 struct i2c_client *client = ds1307->client;
1281 struct device_node *node = client->dev.of_node;
1282 struct clk_onecell_data *onecell;
1283 int i;
1284
1285 onecell = devm_kzalloc(&client->dev, sizeof(*onecell), GFP_KERNEL);
1286 if (!onecell)
1287 return -ENOMEM;
1288
1289 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
1290 onecell->clks = devm_kcalloc(&client->dev, onecell->clk_num,
1291 sizeof(onecell->clks[0]), GFP_KERNEL);
1292 if (!onecell->clks)
1293 return -ENOMEM;
1294
1295 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1296 struct clk_init_data init = ds3231_clks_init[i];
1297
1298 /*
1299 * Interrupt signal due to alarm conditions and square-wave
1300 * output share same pin, so don't initialize both.
1301 */
1302 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1303 continue;
1304
1305 /* optional override of the clockname */
1306 of_property_read_string_index(node, "clock-output-names", i,
1307 &init.name);
1308 ds1307->clks[i].init = &init;
1309
1310 onecell->clks[i] = devm_clk_register(&client->dev,
1311 &ds1307->clks[i]);
1312 if (IS_ERR(onecell->clks[i]))
1313 return PTR_ERR(onecell->clks[i]);
1314 }
1315
1316 if (!node)
1317 return 0;
1318
1319 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1320
1321 return 0;
1322}
1323
1324static void ds1307_clks_register(struct ds1307 *ds1307)
1325{
1326 int ret;
1327
1328 if (ds1307->type != ds_3231)
1329 return;
1330
1331 ret = ds3231_clks_register(ds1307);
1332 if (ret) {
1333 dev_warn(&ds1307->client->dev,
1334 "unable to register clock device %d\n", ret);
1335 }
1336}
1337
1338#else
1339
1340static void ds1307_clks_register(struct ds1307 *ds1307)
1341{
1342}
1343
1344#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001345
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001346static int ds1307_probe(struct i2c_client *client,
1347 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001348{
1349 struct ds1307 *ds1307;
1350 int err = -ENODEV;
Keerthye29385f2016-06-01 16:19:07 +05301351 int tmp, wday;
Tin Huynh9c19b892016-11-30 09:57:31 +07001352 struct chip_desc *chip;
David Brownellc065f352007-07-17 04:05:10 -07001353 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Peter Senna Tschudinc8b18da2013-11-12 15:10:59 -08001354 bool want_irq = false;
Michael Lange8bc2a402016-01-21 18:10:16 +01001355 bool ds1307_can_wakeup_device = false;
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001356 unsigned char *buf;
Jingoo Han01ce8932013-11-12 15:10:41 -08001357 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Keerthye29385f2016-06-01 16:19:07 +05301358 struct rtc_time tm;
1359 unsigned long timestamp;
1360
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001361 irq_handler_t irq_handler = ds1307_irq;
1362
Wolfram Sang97f902b2009-06-17 16:26:10 -07001363 static const int bbsqi_bitpos[] = {
1364 [ds_1337] = 0,
1365 [ds_1339] = DS1339_BIT_BBSQI,
1366 [ds_3231] = DS3231_BIT_BBSQW,
1367 };
Simon Guinot1d1945d2014-04-03 14:49:55 -07001368 const struct rtc_class_ops *rtc_ops = &ds13xx_rtc_ops;
David Brownell1abb0dc2006-06-25 05:48:17 -07001369
Ed Swierk30e7b032009-03-31 15:24:56 -07001370 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)
1371 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
David Brownellc065f352007-07-17 04:05:10 -07001372 return -EIO;
David Brownell1abb0dc2006-06-25 05:48:17 -07001373
Jingoo Hanedca66d2013-07-03 15:07:05 -07001374 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001375 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001376 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001377
David Brownell1abb0dc2006-06-25 05:48:17 -07001378 i2c_set_clientdata(client, ds1307);
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001379
1380 ds1307->client = client;
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001381
1382 if (client->dev.of_node) {
1383 ds1307->type = (enum ds_type)
1384 of_device_get_match_data(&client->dev);
1385 chip = &chips[ds1307->type];
1386 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001387 chip = &chips[id->driver_data];
1388 ds1307->type = id->driver_data;
1389 } else {
1390 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001391
Tin Huynh9c19b892016-11-30 09:57:31 +07001392 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
1393 &client->dev);
1394 if (!acpi_id)
1395 return -ENODEV;
1396 chip = &chips[acpi_id->driver_data];
1397 ds1307->type = acpi_id->driver_data;
1398 }
1399
1400 if (!pdata)
1401 ds1307_trickle_init(client, chip);
1402 else if (pdata->trickle_charger_setup)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001403 chip->trickle_charger_setup = pdata->trickle_charger_setup;
1404
1405 if (chip->trickle_charger_setup && chip->trickle_charger_reg) {
1406 dev_dbg(&client->dev, "writing trickle charger info 0x%x to 0x%x\n",
1407 DS13XX_TRICKLE_CHARGER_MAGIC | chip->trickle_charger_setup,
1408 chip->trickle_charger_reg);
Wolfram Sangeb86c302012-05-29 15:07:38 -07001409 i2c_smbus_write_byte_data(client, chip->trickle_charger_reg,
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001410 DS13XX_TRICKLE_CHARGER_MAGIC |
1411 chip->trickle_charger_setup);
1412 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001413
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001414 buf = ds1307->regs;
Ed Swierk30e7b032009-03-31 15:24:56 -07001415 if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
Bertrand Achardbc48b902013-04-29 16:19:26 -07001416 ds1307->read_block_data = ds1307_native_smbus_read_block_data;
1417 ds1307->write_block_data = ds1307_native_smbus_write_block_data;
Ed Swierk30e7b032009-03-31 15:24:56 -07001418 } else {
1419 ds1307->read_block_data = ds1307_read_block_data;
1420 ds1307->write_block_data = ds1307_write_block_data;
1421 }
David Brownell045e0e82007-07-17 04:04:55 -07001422
Michael Lange8bc2a402016-01-21 18:10:16 +01001423#ifdef CONFIG_OF
1424/*
1425 * For devices with no IRQ directly connected to the SoC, the RTC chip
1426 * can be forced as a wakeup source by stating that explicitly in
1427 * the device's .dts file using the "wakeup-source" boolean property.
1428 * If the "wakeup-source" property is set, don't request an IRQ.
1429 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1430 * if supported by the RTC.
1431 */
1432 if (of_property_read_bool(client->dev.of_node, "wakeup-source")) {
1433 ds1307_can_wakeup_device = true;
1434 }
Alexandre Belloni78aaa062016-07-13 02:36:41 +02001435 /* Intersil ISL12057 DT backward compatibility */
1436 if (of_property_read_bool(client->dev.of_node,
1437 "isil,irq2-can-wakeup-machine")) {
1438 ds1307_can_wakeup_device = true;
1439 }
Michael Lange8bc2a402016-01-21 18:10:16 +01001440#endif
1441
David Brownell045e0e82007-07-17 04:04:55 -07001442 switch (ds1307->type) {
1443 case ds_1337:
1444 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001445 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001446 /* get registers that the "rtc" read below won't read... */
Ed Swierk30e7b032009-03-31 15:24:56 -07001447 tmp = ds1307->read_block_data(ds1307->client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001448 DS1337_REG_CONTROL, 2, buf);
David Brownell1abb0dc2006-06-25 05:48:17 -07001449 if (tmp != 2) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001450 dev_dbg(&client->dev, "read error %d\n", tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -07001451 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001452 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001453 }
1454
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001455 /* oscillator off? turn it on, so clock can tick. */
1456 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001457 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
1458
David Anders40ce9722012-03-23 15:02:37 -07001459 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001460 * Using IRQ or defined as wakeup-source?
1461 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001462 * For some variants, be sure alarms can trigger when we're
1463 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001464 */
Michael Lange8bc2a402016-01-21 18:10:16 +01001465 if (chip->alarm && (ds1307->client->irq > 0 ||
1466 ds1307_can_wakeup_device)) {
Wolfram Sang97f902b2009-06-17 16:26:10 -07001467 ds1307->regs[0] |= DS1337_BIT_INTCN
1468 | bbsqi_bitpos[ds1307->type];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001469 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Wolfram Sangb24a7262012-03-23 15:02:37 -07001470
1471 want_irq = true;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001472 }
1473
1474 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
1475 ds1307->regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001476
1477 /* oscillator fault? clear flag, and warn */
1478 if (ds1307->regs[1] & DS1337_BIT_OSF) {
1479 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
1480 ds1307->regs[1] & ~DS1337_BIT_OSF);
1481 dev_warn(&client->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001482 }
David Brownell045e0e82007-07-17 04:04:55 -07001483 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001484
1485 case rx_8025:
1486 tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
1487 RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
1488 if (tmp != 2) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001489 dev_dbg(&client->dev, "read error %d\n", tmp);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001490 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001491 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001492 }
1493
1494 /* oscillator off? turn it on, so clock can tick. */
1495 if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
1496 ds1307->regs[1] |= RX8025_BIT_XST;
1497 i2c_smbus_write_byte_data(client,
1498 RX8025_REG_CTRL2 << 4 | 0x08,
1499 ds1307->regs[1]);
1500 dev_warn(&client->dev,
1501 "oscillator stop detected - SET TIME!\n");
1502 }
1503
1504 if (ds1307->regs[1] & RX8025_BIT_PON) {
1505 ds1307->regs[1] &= ~RX8025_BIT_PON;
1506 i2c_smbus_write_byte_data(client,
1507 RX8025_REG_CTRL2 << 4 | 0x08,
1508 ds1307->regs[1]);
1509 dev_warn(&client->dev, "power-on detected\n");
1510 }
1511
1512 if (ds1307->regs[1] & RX8025_BIT_VDET) {
1513 ds1307->regs[1] &= ~RX8025_BIT_VDET;
1514 i2c_smbus_write_byte_data(client,
1515 RX8025_REG_CTRL2 << 4 | 0x08,
1516 ds1307->regs[1]);
1517 dev_warn(&client->dev, "voltage drop detected\n");
1518 }
1519
1520 /* make sure we are running in 24hour mode */
1521 if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
1522 u8 hour;
1523
1524 /* switch to 24 hour mode */
1525 i2c_smbus_write_byte_data(client,
1526 RX8025_REG_CTRL1 << 4 | 0x08,
1527 ds1307->regs[0] |
1528 RX8025_BIT_2412);
1529
1530 tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
1531 RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
1532 if (tmp != 2) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001533 dev_dbg(&client->dev, "read error %d\n", tmp);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001534 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001535 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001536 }
1537
1538 /* correct hour */
1539 hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
1540 if (hour == 12)
1541 hour = 0;
1542 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1543 hour += 12;
1544
1545 i2c_smbus_write_byte_data(client,
1546 DS1307_REG_HOUR << 4 | 0x08,
1547 hour);
1548 }
1549 break;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001550 case ds_1388:
1551 ds1307->offset = 1; /* Seconds starts at 1 */
1552 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001553 case mcp794xx:
1554 rtc_ops = &mcp794xx_rtc_ops;
Simon Guinot1d1945d2014-04-03 14:49:55 -07001555 if (ds1307->client->irq > 0 && chip->alarm) {
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001556 irq_handler = mcp794xx_irq;
Simon Guinot1d1945d2014-04-03 14:49:55 -07001557 want_irq = true;
1558 }
1559 break;
David Brownell045e0e82007-07-17 04:04:55 -07001560 default:
1561 break;
1562 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001563
1564read_rtc:
1565 /* read RTC registers */
Joakim Tjernlund96fc3a42010-06-29 15:05:34 -07001566 tmp = ds1307->read_block_data(ds1307->client, ds1307->offset, 8, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001567 if (tmp != 8) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001568 dev_dbg(&client->dev, "read error %d\n", tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -07001569 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001570 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001571 }
1572
David Anders40ce9722012-03-23 15:02:37 -07001573 /*
1574 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001575 * specify the extra bits as must-be-zero, but there are
1576 * still a few values that are clearly out-of-range.
1577 */
1578 tmp = ds1307->regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001579 switch (ds1307->type) {
1580 case ds_1307:
David Brownell045e0e82007-07-17 04:04:55 -07001581 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001582 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001583 if (tmp & DS1307_BIT_CH) {
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001584 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
1585 dev_warn(&client->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001586 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001587 }
David Brownell045e0e82007-07-17 04:04:55 -07001588 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001589 case ds_1338:
1590 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001591 if (tmp & DS1307_BIT_CH)
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001592 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
1593
1594 /* oscillator fault? clear flag, and warn */
1595 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
1596 i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
David Brownellbd16f9e2007-07-26 10:41:00 -07001597 ds1307->regs[DS1307_REG_CONTROL]
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001598 & ~DS1338_BIT_OSF);
1599 dev_warn(&client->dev, "SET TIME!\n");
1600 goto read_rtc;
1601 }
David Brownell045e0e82007-07-17 04:04:55 -07001602 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001603 case ds_1340:
1604 /* clock halted? turn it on, so clock can tick. */
1605 if (tmp & DS1340_BIT_nEOSC)
1606 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
1607
1608 tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG);
1609 if (tmp < 0) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001610 dev_dbg(&client->dev, "read error %d\n", tmp);
frederic Rodofcd8db02008-02-06 01:38:55 -08001611 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001612 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001613 }
1614
1615 /* oscillator fault? clear flag, and warn */
1616 if (tmp & DS1340_BIT_OSF) {
1617 i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0);
1618 dev_warn(&client->dev, "SET TIME!\n");
1619 }
1620 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001621 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001622 /* make sure that the backup battery is enabled */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001623 if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
David Anders43fcb812011-11-02 13:37:53 -07001624 i2c_smbus_write_byte_data(client, DS1307_REG_WDAY,
1625 ds1307->regs[DS1307_REG_WDAY]
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001626 | MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001627 }
1628
1629 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001630 if (!(tmp & MCP794XX_BIT_ST)) {
David Anders43fcb812011-11-02 13:37:53 -07001631 i2c_smbus_write_byte_data(client, DS1307_REG_SECS,
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001632 MCP794XX_BIT_ST);
David Anders43fcb812011-11-02 13:37:53 -07001633 dev_warn(&client->dev, "SET TIME!\n");
1634 goto read_rtc;
1635 }
1636
1637 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001638 default:
David Brownell045e0e82007-07-17 04:04:55 -07001639 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001640 }
David Brownell045e0e82007-07-17 04:04:55 -07001641
David Brownell1abb0dc2006-06-25 05:48:17 -07001642 tmp = ds1307->regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001643 switch (ds1307->type) {
1644 case ds_1340:
1645 case m41t00:
David Anders40ce9722012-03-23 15:02:37 -07001646 /*
1647 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001648 * systems that will run through year 2100.
1649 */
1650 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001651 case rx_8025:
1652 break;
David Brownellc065f352007-07-17 04:05:10 -07001653 default:
1654 if (!(tmp & DS1307_BIT_12HR))
1655 break;
1656
David Anders40ce9722012-03-23 15:02:37 -07001657 /*
1658 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001659 * take note...
1660 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001661 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001662 if (tmp == 12)
1663 tmp = 0;
1664 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1665 tmp += 12;
David Brownell1abb0dc2006-06-25 05:48:17 -07001666 i2c_smbus_write_byte_data(client,
Joakim Tjernlund96fc3a42010-06-29 15:05:34 -07001667 ds1307->offset + DS1307_REG_HOUR,
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001668 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001669 }
1670
Keerthye29385f2016-06-01 16:19:07 +05301671 /*
1672 * Some IPs have weekday reset value = 0x1 which might not correct
1673 * hence compute the wday using the current date/month/year values
1674 */
1675 ds1307_get_time(&client->dev, &tm);
1676 wday = tm.tm_wday;
1677 timestamp = rtc_tm_to_time64(&tm);
1678 rtc_time64_to_tm(timestamp, &tm);
1679
1680 /*
1681 * Check if reset wday is different from the computed wday
1682 * If different then set the wday which we computed using
1683 * timestamp
1684 */
1685 if (wday != tm.tm_wday) {
1686 wday = i2c_smbus_read_byte_data(client, MCP794XX_REG_WEEKDAY);
1687 wday = wday & ~MCP794XX_REG_WEEKDAY_WDAY_MASK;
1688 wday = wday | (tm.tm_wday + 1);
1689 i2c_smbus_write_byte_data(client, MCP794XX_REG_WEEKDAY, wday);
1690 }
1691
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001692 if (want_irq) {
1693 device_set_wakeup_capable(&client->dev, true);
1694 set_bit(HAS_ALARM, &ds1307->flags);
1695 }
Jingoo Hanedca66d2013-07-03 15:07:05 -07001696 ds1307->rtc = devm_rtc_device_register(&client->dev, client->name,
Simon Guinot1d1945d2014-04-03 14:49:55 -07001697 rtc_ops, THIS_MODULE);
David Brownell1abb0dc2006-06-25 05:48:17 -07001698 if (IS_ERR(ds1307->rtc)) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001699 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001700 }
1701
Nishanth Menon38a7a732016-04-19 11:23:54 -05001702 if (ds1307_can_wakeup_device && ds1307->client->irq <= 0) {
Michael Lange8bc2a402016-01-21 18:10:16 +01001703 /* Disable request for an IRQ */
1704 want_irq = false;
1705 dev_info(&client->dev, "'wakeup-source' is set, request for an IRQ is disabled!\n");
1706 /* We cannot support UIE mode if we do not have an IRQ line */
1707 ds1307->rtc->uie_unsupported = 1;
1708 }
1709
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001710 if (want_irq) {
Nishanth Menonc5983192015-06-23 11:15:11 -05001711 err = devm_request_threaded_irq(&client->dev,
1712 client->irq, NULL, irq_handler,
1713 IRQF_SHARED | IRQF_ONESHOT,
1714 ds1307->rtc->name, client);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001715 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001716 client->irq = 0;
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001717 device_set_wakeup_capable(&client->dev, false);
1718 clear_bit(HAS_ALARM, &ds1307->flags);
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001719 dev_err(&client->dev, "unable to request IRQ!\n");
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001720 } else
Felipe Balbi51c4cfe2015-11-11 10:11:01 -06001721 dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001722 }
1723
Austin Boyle9eab0a72012-03-23 15:02:38 -07001724 if (chip->nvram_size) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001725
Jingoo Hanedca66d2013-07-03 15:07:05 -07001726 ds1307->nvram = devm_kzalloc(&client->dev,
1727 sizeof(struct bin_attribute),
1728 GFP_KERNEL);
Austin Boyle9eab0a72012-03-23 15:02:38 -07001729 if (!ds1307->nvram) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001730 dev_err(&client->dev, "cannot allocate memory for nvram sysfs\n");
1731 } else {
1732
1733 ds1307->nvram->attr.name = "nvram";
1734 ds1307->nvram->attr.mode = S_IRUGO | S_IWUSR;
1735
1736 sysfs_bin_attr_init(ds1307->nvram);
1737
1738 ds1307->nvram->read = ds1307_nvram_read;
1739 ds1307->nvram->write = ds1307_nvram_write;
1740 ds1307->nvram->size = chip->nvram_size;
1741 ds1307->nvram_offset = chip->nvram_offset;
1742
1743 err = sysfs_create_bin_file(&client->dev.kobj,
1744 ds1307->nvram);
1745 if (err) {
1746 dev_err(&client->dev,
1747 "unable to create sysfs file: %s\n",
1748 ds1307->nvram->attr.name);
1749 } else {
1750 set_bit(HAS_NVRAM, &ds1307->flags);
1751 dev_info(&client->dev, "%zu bytes nvram\n",
1752 ds1307->nvram->size);
1753 }
David Brownell682d73f2007-11-14 16:58:32 -08001754 }
1755 }
1756
Akinobu Mita445c0202016-01-25 00:22:16 +09001757 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001758 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001759
David Brownell1abb0dc2006-06-25 05:48:17 -07001760 return 0;
1761
Jingoo Hanedca66d2013-07-03 15:07:05 -07001762exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001763 return err;
1764}
1765
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001766static int ds1307_remove(struct i2c_client *client)
David Brownell1abb0dc2006-06-25 05:48:17 -07001767{
David Anders40ce9722012-03-23 15:02:37 -07001768 struct ds1307 *ds1307 = i2c_get_clientdata(client);
David Brownell1abb0dc2006-06-25 05:48:17 -07001769
Jingoo Hanedca66d2013-07-03 15:07:05 -07001770 if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
Austin Boyle9eab0a72012-03-23 15:02:38 -07001771 sysfs_remove_bin_file(&client->dev.kobj, ds1307->nvram);
David Brownell682d73f2007-11-14 16:58:32 -08001772
David Brownell1abb0dc2006-06-25 05:48:17 -07001773 return 0;
1774}
1775
1776static struct i2c_driver ds1307_driver = {
1777 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001778 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001779 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001780 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001781 },
David Brownellc065f352007-07-17 04:05:10 -07001782 .probe = ds1307_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001783 .remove = ds1307_remove,
Jean Delvare3760f732008-04-29 23:11:40 +02001784 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001785};
1786
Axel Lin0abc9202012-03-23 15:02:31 -07001787module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001788
1789MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1790MODULE_LICENSE("GPL");