blob: e29481391496f6f738e4c4ab0cf432ab6817fd71 [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,
Stefan Agner8566f702017-03-23 16:54:57 -070042 m41t0,
David Brownell045e0e82007-07-17 04:04:55 -070043 m41t00,
Tomas Novotnyf4199f82014-12-10 15:53:57 -080044 mcp794xx,
Matthias Fuchsa2166852009-03-31 15:24:58 -070045 rx_8025,
Wolfram Sang32d322b2012-03-23 15:02:36 -070046 last_ds_type /* always last */
David Anders40ce9722012-03-23 15:02:37 -070047 /* rs5c372 too? different address... */
David Brownell1abb0dc2006-06-25 05:48:17 -070048};
49
David Brownell1abb0dc2006-06-25 05:48:17 -070050
51/* RTC registers don't differ much, except for the century flag */
52#define DS1307_REG_SECS 0x00 /* 00-59 */
53# define DS1307_BIT_CH 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070054# define DS1340_BIT_nEOSC 0x80
Tomas Novotnyf4199f82014-12-10 15:53:57 -080055# define MCP794XX_BIT_ST 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070056#define DS1307_REG_MIN 0x01 /* 00-59 */
Stefan Agner8566f702017-03-23 16:54:57 -070057# define M41T0_BIT_OF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070058#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
David Brownellc065f352007-07-17 04:05:10 -070059# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
60# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
David Brownell1abb0dc2006-06-25 05:48:17 -070061# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
62# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
63#define DS1307_REG_WDAY 0x03 /* 01-07 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -080064# define MCP794XX_BIT_VBATEN 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -070065#define DS1307_REG_MDAY 0x04 /* 01-31 */
66#define DS1307_REG_MONTH 0x05 /* 01-12 */
67# define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
68#define DS1307_REG_YEAR 0x06 /* 00-99 */
69
David Anders40ce9722012-03-23 15:02:37 -070070/*
71 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
David Brownell045e0e82007-07-17 04:04:55 -070072 * start at 7, and they differ a LOT. Only control and status matter for
73 * basic RTC date and time functionality; be careful using them.
David Brownell1abb0dc2006-06-25 05:48:17 -070074 */
David Brownell045e0e82007-07-17 04:04:55 -070075#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
David Brownell1abb0dc2006-06-25 05:48:17 -070076# define DS1307_BIT_OUT 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070077# define DS1338_BIT_OSF 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070078# define DS1307_BIT_SQWE 0x10
79# define DS1307_BIT_RS1 0x02
80# define DS1307_BIT_RS0 0x01
81#define DS1337_REG_CONTROL 0x0e
82# define DS1337_BIT_nEOSC 0x80
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070083# define DS1339_BIT_BBSQI 0x20
Wolfram Sang97f902b2009-06-17 16:26:10 -070084# define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
David Brownell1abb0dc2006-06-25 05:48:17 -070085# define DS1337_BIT_RS2 0x10
86# define DS1337_BIT_RS1 0x08
87# define DS1337_BIT_INTCN 0x04
88# define DS1337_BIT_A2IE 0x02
89# define DS1337_BIT_A1IE 0x01
David Brownell045e0e82007-07-17 04:04:55 -070090#define DS1340_REG_CONTROL 0x07
91# define DS1340_BIT_OUT 0x80
92# define DS1340_BIT_FT 0x40
93# define DS1340_BIT_CALIB_SIGN 0x20
94# define DS1340_M_CALIBRATION 0x1f
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070095#define DS1340_REG_FLAG 0x09
96# define DS1340_BIT_OSF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070097#define DS1337_REG_STATUS 0x0f
98# define DS1337_BIT_OSF 0x80
Akinobu Mita6c6ff142016-01-31 23:10:10 +090099# define DS3231_BIT_EN32KHZ 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -0700100# define DS1337_BIT_A2I 0x02
101# define DS1337_BIT_A1I 0x01
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700102#define DS1339_REG_ALARM1_SECS 0x07
Wolfram Sangeb86c302012-05-29 15:07:38 -0700103
104#define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
David Brownell1abb0dc2006-06-25 05:48:17 -0700105
Matthias Fuchsa2166852009-03-31 15:24:58 -0700106#define RX8025_REG_CTRL1 0x0e
107# define RX8025_BIT_2412 0x20
108#define RX8025_REG_CTRL2 0x0f
109# define RX8025_BIT_PON 0x10
110# define RX8025_BIT_VDET 0x40
111# define RX8025_BIT_XST 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -0700112
113
114struct ds1307 {
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700115 u8 offset; /* register's offset */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700116 u8 regs[11];
Austin Boyle9eab0a72012-03-23 15:02:38 -0700117 u16 nvram_offset;
118 struct bin_attribute *nvram;
David Brownell1abb0dc2006-06-25 05:48:17 -0700119 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700120 unsigned long flags;
121#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
122#define HAS_ALARM 1 /* bit 1 == irq claimed */
David Brownell045e0e82007-07-17 04:04:55 -0700123 struct i2c_client *client;
David Brownell1abb0dc2006-06-25 05:48:17 -0700124 struct rtc_device *rtc;
Jean Delvare0cc43a12011-01-10 22:11:23 +0100125 s32 (*read_block_data)(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700126 u8 length, u8 *values);
Jean Delvare0cc43a12011-01-10 22:11:23 +0100127 s32 (*write_block_data)(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700128 u8 length, const u8 *values);
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900129#ifdef CONFIG_COMMON_CLK
130 struct clk_hw clks[2];
131#endif
David Brownell1abb0dc2006-06-25 05:48:17 -0700132};
133
David Brownell045e0e82007-07-17 04:04:55 -0700134struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700135 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700136 u16 nvram_offset;
137 u16 nvram_size;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700138 u16 trickle_charger_reg;
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700139 u8 trickle_charger_setup;
140 u8 (*do_trickle_setup)(struct i2c_client *, uint32_t, bool);
David Brownell045e0e82007-07-17 04:04:55 -0700141};
142
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700143static u8 do_trickle_setup_ds1339(struct i2c_client *,
144 uint32_t ohms, bool diode);
145
146static struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700147 [ds_1307] = {
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_1337] = {
152 .alarm = 1,
153 },
154 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700155 .nvram_offset = 8,
156 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700157 },
158 [ds_1339] = {
159 .alarm = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700160 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700161 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700162 },
163 [ds_1340] = {
164 .trickle_charger_reg = 0x08,
165 },
166 [ds_1388] = {
167 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700168 },
169 [ds_3231] = {
170 .alarm = 1,
171 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800172 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700173 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700174 /* this is battery backed SRAM */
175 .nvram_offset = 0x20,
176 .nvram_size = 0x40,
177 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700178};
David Brownell045e0e82007-07-17 04:04:55 -0700179
Jean Delvare3760f732008-04-29 23:11:40 +0200180static const struct i2c_device_id ds1307_id[] = {
181 { "ds1307", ds_1307 },
182 { "ds1337", ds_1337 },
183 { "ds1338", ds_1338 },
184 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700185 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200186 { "ds1340", ds_1340 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700187 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700188 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200189 { "m41t00", m41t00 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800190 { "mcp7940x", mcp794xx },
191 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700192 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700193 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200194 { "isl12057", ds_1337 },
Jean Delvare3760f732008-04-29 23:11:40 +0200195 { }
196};
197MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700198
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300199#ifdef CONFIG_OF
200static const struct of_device_id ds1307_of_match[] = {
201 {
202 .compatible = "dallas,ds1307",
203 .data = (void *)ds_1307
204 },
205 {
206 .compatible = "dallas,ds1337",
207 .data = (void *)ds_1337
208 },
209 {
210 .compatible = "dallas,ds1338",
211 .data = (void *)ds_1338
212 },
213 {
214 .compatible = "dallas,ds1339",
215 .data = (void *)ds_1339
216 },
217 {
218 .compatible = "dallas,ds1388",
219 .data = (void *)ds_1388
220 },
221 {
222 .compatible = "dallas,ds1340",
223 .data = (void *)ds_1340
224 },
225 {
226 .compatible = "maxim,ds3231",
227 .data = (void *)ds_3231
228 },
229 {
230 .compatible = "st,m41t00",
231 .data = (void *)m41t00
232 },
233 {
234 .compatible = "microchip,mcp7940x",
235 .data = (void *)mcp794xx
236 },
237 {
238 .compatible = "microchip,mcp7941x",
239 .data = (void *)mcp794xx
240 },
241 {
242 .compatible = "pericom,pt7c4338",
243 .data = (void *)ds_1307
244 },
245 {
246 .compatible = "epson,rx8025",
247 .data = (void *)rx_8025
248 },
249 {
250 .compatible = "isil,isl12057",
251 .data = (void *)ds_1337
252 },
253 { }
254};
255MODULE_DEVICE_TABLE(of, ds1307_of_match);
256#endif
257
Tin Huynh9c19b892016-11-30 09:57:31 +0700258#ifdef CONFIG_ACPI
259static const struct acpi_device_id ds1307_acpi_ids[] = {
260 { .id = "DS1307", .driver_data = ds_1307 },
261 { .id = "DS1337", .driver_data = ds_1337 },
262 { .id = "DS1338", .driver_data = ds_1338 },
263 { .id = "DS1339", .driver_data = ds_1339 },
264 { .id = "DS1388", .driver_data = ds_1388 },
265 { .id = "DS1340", .driver_data = ds_1340 },
266 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700267 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700268 { .id = "M41T00", .driver_data = m41t00 },
269 { .id = "MCP7940X", .driver_data = mcp794xx },
270 { .id = "MCP7941X", .driver_data = mcp794xx },
271 { .id = "PT7C4338", .driver_data = ds_1307 },
272 { .id = "RX8025", .driver_data = rx_8025 },
273 { .id = "ISL12057", .driver_data = ds_1337 },
274 { }
275};
276MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
277#endif
278
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700279/*----------------------------------------------------------------------*/
280
Ed Swierk30e7b032009-03-31 15:24:56 -0700281#define BLOCK_DATA_MAX_TRIES 10
282
Jean Delvare0cc43a12011-01-10 22:11:23 +0100283static s32 ds1307_read_block_data_once(const struct i2c_client *client,
284 u8 command, u8 length, u8 *values)
Ed Swierk30e7b032009-03-31 15:24:56 -0700285{
286 s32 i, data;
287
288 for (i = 0; i < length; i++) {
289 data = i2c_smbus_read_byte_data(client, command + i);
290 if (data < 0)
291 return data;
292 values[i] = data;
293 }
294 return i;
295}
296
Jean Delvare0cc43a12011-01-10 22:11:23 +0100297static s32 ds1307_read_block_data(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700298 u8 length, u8 *values)
299{
Bertrand Achardbc48b902013-04-29 16:19:26 -0700300 u8 oldvalues[255];
Ed Swierk30e7b032009-03-31 15:24:56 -0700301 s32 ret;
302 int tries = 0;
303
304 dev_dbg(&client->dev, "ds1307_read_block_data (length=%d)\n", length);
305 ret = ds1307_read_block_data_once(client, command, length, values);
306 if (ret < 0)
307 return ret;
308 do {
309 if (++tries > BLOCK_DATA_MAX_TRIES) {
310 dev_err(&client->dev,
311 "ds1307_read_block_data failed\n");
312 return -EIO;
313 }
314 memcpy(oldvalues, values, length);
315 ret = ds1307_read_block_data_once(client, command, length,
316 values);
317 if (ret < 0)
318 return ret;
319 } while (memcmp(oldvalues, values, length));
320 return length;
321}
322
Jean Delvare0cc43a12011-01-10 22:11:23 +0100323static s32 ds1307_write_block_data(const struct i2c_client *client, u8 command,
Ed Swierk30e7b032009-03-31 15:24:56 -0700324 u8 length, const u8 *values)
325{
Bertrand Achardbc48b902013-04-29 16:19:26 -0700326 u8 currvalues[255];
Ed Swierk30e7b032009-03-31 15:24:56 -0700327 int tries = 0;
328
329 dev_dbg(&client->dev, "ds1307_write_block_data (length=%d)\n", length);
330 do {
331 s32 i, ret;
332
333 if (++tries > BLOCK_DATA_MAX_TRIES) {
334 dev_err(&client->dev,
335 "ds1307_write_block_data failed\n");
336 return -EIO;
337 }
338 for (i = 0; i < length; i++) {
339 ret = i2c_smbus_write_byte_data(client, command + i,
340 values[i]);
341 if (ret < 0)
342 return ret;
343 }
344 ret = ds1307_read_block_data_once(client, command, length,
345 currvalues);
346 if (ret < 0)
347 return ret;
348 } while (memcmp(currvalues, values, length));
349 return length;
350}
351
352/*----------------------------------------------------------------------*/
353
Bertrand Achardbc48b902013-04-29 16:19:26 -0700354/* These RTC devices are not designed to be connected to a SMbus adapter.
355 SMbus limits block operations length to 32 bytes, whereas it's not
356 limited on I2C buses. As a result, accesses may exceed 32 bytes;
357 in that case, split them into smaller blocks */
358
359static s32 ds1307_native_smbus_write_block_data(const struct i2c_client *client,
360 u8 command, u8 length, const u8 *values)
361{
362 u8 suboffset = 0;
363
Nicolas Boullis1d879512016-04-03 00:10:37 +0200364 if (length <= I2C_SMBUS_BLOCK_MAX) {
365 s32 retval = i2c_smbus_write_i2c_block_data(client,
Bertrand Achardbc48b902013-04-29 16:19:26 -0700366 command, length, values);
Nicolas Boullis1d879512016-04-03 00:10:37 +0200367 if (retval < 0)
368 return retval;
369 return length;
370 }
Bertrand Achardbc48b902013-04-29 16:19:26 -0700371
372 while (suboffset < length) {
373 s32 retval = i2c_smbus_write_i2c_block_data(client,
374 command + suboffset,
375 min(I2C_SMBUS_BLOCK_MAX, length - suboffset),
376 values + suboffset);
377 if (retval < 0)
378 return retval;
379
380 suboffset += I2C_SMBUS_BLOCK_MAX;
381 }
382 return length;
383}
384
385static s32 ds1307_native_smbus_read_block_data(const struct i2c_client *client,
386 u8 command, u8 length, u8 *values)
387{
388 u8 suboffset = 0;
389
390 if (length <= I2C_SMBUS_BLOCK_MAX)
391 return i2c_smbus_read_i2c_block_data(client,
392 command, length, values);
393
394 while (suboffset < length) {
395 s32 retval = i2c_smbus_read_i2c_block_data(client,
396 command + suboffset,
397 min(I2C_SMBUS_BLOCK_MAX, length - suboffset),
398 values + suboffset);
399 if (retval < 0)
400 return retval;
401
402 suboffset += I2C_SMBUS_BLOCK_MAX;
403 }
404 return length;
405}
406
407/*----------------------------------------------------------------------*/
408
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700409/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700410 * The ds1337 and ds1339 both have two alarms, but we only use the first
411 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
412 * signal; ds1339 chips have only one alarm signal.
413 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500414static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700415{
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500416 struct i2c_client *client = dev_id;
417 struct ds1307 *ds1307 = i2c_get_clientdata(client);
418 struct mutex *lock = &ds1307->rtc->ops_lock;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700419 int stat, control;
420
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700421 mutex_lock(lock);
422 stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
423 if (stat < 0)
424 goto out;
425
426 if (stat & DS1337_BIT_A1I) {
427 stat &= ~DS1337_BIT_A1I;
428 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, stat);
429
430 control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
431 if (control < 0)
432 goto out;
433
434 control &= ~DS1337_BIT_A1IE;
435 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
436
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700437 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700438 }
439
440out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700441 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700442
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700443 return IRQ_HANDLED;
444}
445
446/*----------------------------------------------------------------------*/
447
David Brownell1abb0dc2006-06-25 05:48:17 -0700448static int ds1307_get_time(struct device *dev, struct rtc_time *t)
449{
450 struct ds1307 *ds1307 = dev_get_drvdata(dev);
451 int tmp;
452
David Brownell045e0e82007-07-17 04:04:55 -0700453 /* read the RTC date and time registers all at once */
Ed Swierk30e7b032009-03-31 15:24:56 -0700454 tmp = ds1307->read_block_data(ds1307->client,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700455 ds1307->offset, 7, ds1307->regs);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800456 if (tmp != 7) {
David Brownell1abb0dc2006-06-25 05:48:17 -0700457 dev_err(dev, "%s error %d\n", "read", tmp);
458 return -EIO;
459 }
460
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800461 dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700462
Stefan Agner8566f702017-03-23 16:54:57 -0700463 /* if oscillator fail bit is set, no data can be trusted */
464 if (ds1307->type == m41t0 &&
465 ds1307->regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
466 dev_warn_once(dev, "oscillator failed, set time!\n");
467 return -EINVAL;
468 }
469
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700470 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
471 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700472 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700473 t->tm_hour = bcd2bin(tmp);
474 t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
475 t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700476 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700477 t->tm_mon = bcd2bin(tmp) - 1;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700478 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700479
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200480#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
481 switch (ds1307->type) {
482 case ds_1337:
483 case ds_1339:
484 case ds_3231:
485 if (ds1307->regs[DS1307_REG_MONTH] & DS1337_BIT_CENTURY)
486 t->tm_year += 100;
487 break;
488 case ds_1340:
489 if (ds1307->regs[DS1307_REG_HOUR] & DS1340_BIT_CENTURY)
490 t->tm_year += 100;
491 break;
492 default:
493 break;
494 }
495#endif
496
David Brownell1abb0dc2006-06-25 05:48:17 -0700497 dev_dbg(dev, "%s secs=%d, mins=%d, "
498 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
499 "read", t->tm_sec, t->tm_min,
500 t->tm_hour, t->tm_mday,
501 t->tm_mon, t->tm_year, t->tm_wday);
502
David Brownell045e0e82007-07-17 04:04:55 -0700503 /* initial clock setting can be undefined */
504 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700505}
506
507static int ds1307_set_time(struct device *dev, struct rtc_time *t)
508{
509 struct ds1307 *ds1307 = dev_get_drvdata(dev);
510 int result;
511 int tmp;
512 u8 *buf = ds1307->regs;
513
514 dev_dbg(dev, "%s secs=%d, mins=%d, "
515 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400516 "write", t->tm_sec, t->tm_min,
517 t->tm_hour, t->tm_mday,
518 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700519
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200520#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
521 if (t->tm_year < 100)
522 return -EINVAL;
523
524 switch (ds1307->type) {
525 case ds_1337:
526 case ds_1339:
527 case ds_3231:
528 case ds_1340:
529 if (t->tm_year > 299)
530 return -EINVAL;
531 default:
532 if (t->tm_year > 199)
533 return -EINVAL;
534 break;
535 }
536#else
537 if (t->tm_year < 100 || t->tm_year > 199)
538 return -EINVAL;
539#endif
540
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700541 buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
542 buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
543 buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
544 buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
545 buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
546 buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700547
548 /* assume 20YY not 19YY */
549 tmp = t->tm_year - 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700550 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700551
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700552 switch (ds1307->type) {
553 case ds_1337:
554 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -0700555 case ds_3231:
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200556 if (t->tm_year > 199)
557 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700558 break;
559 case ds_1340:
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200560 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN;
561 if (t->tm_year > 199)
562 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700563 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800564 case mcp794xx:
David Anders40ce9722012-03-23 15:02:37 -0700565 /*
566 * these bits were cleared when preparing the date/time
567 * values and need to be set again before writing the
568 * buffer out to the device.
569 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800570 buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
571 buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
David Anders43fcb812011-11-02 13:37:53 -0700572 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700573 default:
574 break;
575 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700576
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800577 dev_dbg(dev, "%s: %7ph\n", "write", buf);
David Brownell1abb0dc2006-06-25 05:48:17 -0700578
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700579 result = ds1307->write_block_data(ds1307->client,
580 ds1307->offset, 7, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800581 if (result < 0) {
582 dev_err(dev, "%s error %d\n", "write", result);
583 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700584 }
585 return 0;
586}
587
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800588static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700589{
590 struct i2c_client *client = to_i2c_client(dev);
591 struct ds1307 *ds1307 = i2c_get_clientdata(client);
592 int ret;
593
594 if (!test_bit(HAS_ALARM, &ds1307->flags))
595 return -EINVAL;
596
597 /* read all ALARM1, ALARM2, and status registers at once */
Ed Swierk30e7b032009-03-31 15:24:56 -0700598 ret = ds1307->read_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800599 DS1339_REG_ALARM1_SECS, 9, ds1307->regs);
600 if (ret != 9) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700601 dev_err(dev, "%s error %d\n", "alarm read", ret);
602 return -EIO;
603 }
604
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100605 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
606 &ds1307->regs[0], &ds1307->regs[4], &ds1307->regs[7]);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700607
David Anders40ce9722012-03-23 15:02:37 -0700608 /*
609 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700610 * and that all four fields are checked matches
611 */
612 t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
613 t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
614 t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
615 t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700616
617 /* ... and status */
618 t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
619 t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
620
621 dev_dbg(dev, "%s secs=%d, mins=%d, "
622 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
623 "alarm read", t->time.tm_sec, t->time.tm_min,
624 t->time.tm_hour, t->time.tm_mday,
625 t->enabled, t->pending);
626
627 return 0;
628}
629
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800630static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700631{
David Anders40ce9722012-03-23 15:02:37 -0700632 struct i2c_client *client = to_i2c_client(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700633 struct ds1307 *ds1307 = i2c_get_clientdata(client);
634 unsigned char *buf = ds1307->regs;
635 u8 control, status;
636 int ret;
637
638 if (!test_bit(HAS_ALARM, &ds1307->flags))
639 return -EINVAL;
640
641 dev_dbg(dev, "%s secs=%d, mins=%d, "
642 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
643 "alarm set", t->time.tm_sec, t->time.tm_min,
644 t->time.tm_hour, t->time.tm_mday,
645 t->enabled, t->pending);
646
647 /* read current status of both alarms and the chip */
Ed Swierk30e7b032009-03-31 15:24:56 -0700648 ret = ds1307->read_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800649 DS1339_REG_ALARM1_SECS, 9, buf);
650 if (ret != 9) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700651 dev_err(dev, "%s error %d\n", "alarm write", ret);
652 return -EIO;
653 }
654 control = ds1307->regs[7];
655 status = ds1307->regs[8];
656
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100657 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
658 &ds1307->regs[0], &ds1307->regs[4], control, status);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700659
660 /* set ALARM1, using 24 hour and day-of-month modes */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700661 buf[0] = bin2bcd(t->time.tm_sec);
662 buf[1] = bin2bcd(t->time.tm_min);
663 buf[2] = bin2bcd(t->time.tm_hour);
664 buf[3] = bin2bcd(t->time.tm_mday);
665
666 /* set ALARM2 to non-garbage */
667 buf[4] = 0;
668 buf[5] = 0;
669 buf[6] = 0;
670
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200671 /* disable alarms */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700672 buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700673 buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
674
Ed Swierk30e7b032009-03-31 15:24:56 -0700675 ret = ds1307->write_block_data(client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800676 DS1339_REG_ALARM1_SECS, 9, buf);
677 if (ret < 0) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700678 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800679 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700680 }
681
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200682 /* optionally enable ALARM1 */
683 if (t->enabled) {
684 dev_dbg(dev, "alarm IRQ armed\n");
685 buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
686 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, buf[7]);
687 }
688
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700689 return 0;
690}
691
John Stultz16380c12011-02-02 17:02:41 -0800692static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700693{
694 struct i2c_client *client = to_i2c_client(dev);
695 struct ds1307 *ds1307 = i2c_get_clientdata(client);
696 int ret;
697
John Stultz16380c12011-02-02 17:02:41 -0800698 if (!test_bit(HAS_ALARM, &ds1307->flags))
699 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700700
John Stultz16380c12011-02-02 17:02:41 -0800701 ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
702 if (ret < 0)
703 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700704
John Stultz16380c12011-02-02 17:02:41 -0800705 if (enabled)
706 ret |= DS1337_BIT_A1IE;
707 else
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700708 ret &= ~DS1337_BIT_A1IE;
709
John Stultz16380c12011-02-02 17:02:41 -0800710 ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, ret);
711 if (ret < 0)
712 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700713
714 return 0;
715}
716
David Brownellff8371a2006-09-30 23:28:17 -0700717static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700718 .read_time = ds1307_get_time,
719 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800720 .read_alarm = ds1337_read_alarm,
721 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800722 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700723};
724
David Brownell682d73f2007-11-14 16:58:32 -0800725/*----------------------------------------------------------------------*/
726
Simon Guinot1d1945d2014-04-03 14:49:55 -0700727/*
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800728 * Alarm support for mcp794xx devices.
Simon Guinot1d1945d2014-04-03 14:49:55 -0700729 */
730
Keerthye29385f2016-06-01 16:19:07 +0530731#define MCP794XX_REG_WEEKDAY 0x3
732#define MCP794XX_REG_WEEKDAY_WDAY_MASK 0x7
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800733#define MCP794XX_REG_CONTROL 0x07
734# define MCP794XX_BIT_ALM0_EN 0x10
735# define MCP794XX_BIT_ALM1_EN 0x20
736#define MCP794XX_REG_ALARM0_BASE 0x0a
737#define MCP794XX_REG_ALARM0_CTRL 0x0d
738#define MCP794XX_REG_ALARM1_BASE 0x11
739#define MCP794XX_REG_ALARM1_CTRL 0x14
740# define MCP794XX_BIT_ALMX_IF (1 << 3)
741# define MCP794XX_BIT_ALMX_C0 (1 << 4)
742# define MCP794XX_BIT_ALMX_C1 (1 << 5)
743# define MCP794XX_BIT_ALMX_C2 (1 << 6)
744# define MCP794XX_BIT_ALMX_POL (1 << 7)
745# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
746 MCP794XX_BIT_ALMX_C1 | \
747 MCP794XX_BIT_ALMX_C2)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700748
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500749static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700750{
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500751 struct i2c_client *client = dev_id;
752 struct ds1307 *ds1307 = i2c_get_clientdata(client);
753 struct mutex *lock = &ds1307->rtc->ops_lock;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700754 int reg, ret;
755
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500756 mutex_lock(lock);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700757
758 /* Check and clear alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800759 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_ALARM0_CTRL);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700760 if (reg < 0)
761 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800762 if (!(reg & MCP794XX_BIT_ALMX_IF))
Simon Guinot1d1945d2014-04-03 14:49:55 -0700763 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800764 reg &= ~MCP794XX_BIT_ALMX_IF;
765 ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_ALARM0_CTRL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700766 if (ret < 0)
767 goto out;
768
769 /* Disable alarm 0. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800770 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700771 if (reg < 0)
772 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800773 reg &= ~MCP794XX_BIT_ALM0_EN;
774 ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700775 if (ret < 0)
776 goto out;
777
778 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
779
780out:
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500781 mutex_unlock(lock);
782
783 return IRQ_HANDLED;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700784}
785
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800786static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700787{
788 struct i2c_client *client = to_i2c_client(dev);
789 struct ds1307 *ds1307 = i2c_get_clientdata(client);
790 u8 *regs = ds1307->regs;
791 int ret;
792
793 if (!test_bit(HAS_ALARM, &ds1307->flags))
794 return -EINVAL;
795
796 /* Read control and alarm 0 registers. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800797 ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700798 if (ret < 0)
799 return ret;
800
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800801 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700802
803 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
804 t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f);
805 t->time.tm_min = bcd2bin(ds1307->regs[4] & 0x7f);
806 t->time.tm_hour = bcd2bin(ds1307->regs[5] & 0x3f);
807 t->time.tm_wday = bcd2bin(ds1307->regs[6] & 0x7) - 1;
808 t->time.tm_mday = bcd2bin(ds1307->regs[7] & 0x3f);
809 t->time.tm_mon = bcd2bin(ds1307->regs[8] & 0x1f) - 1;
810 t->time.tm_year = -1;
811 t->time.tm_yday = -1;
812 t->time.tm_isdst = -1;
813
814 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
815 "enabled=%d polarity=%d irq=%d match=%d\n", __func__,
816 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
817 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800818 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL),
819 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF),
820 (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700821
822 return 0;
823}
824
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800825static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700826{
827 struct i2c_client *client = to_i2c_client(dev);
828 struct ds1307 *ds1307 = i2c_get_clientdata(client);
829 unsigned char *regs = ds1307->regs;
830 int ret;
831
832 if (!test_bit(HAS_ALARM, &ds1307->flags))
833 return -EINVAL;
834
835 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
836 "enabled=%d pending=%d\n", __func__,
837 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
838 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
839 t->enabled, t->pending);
840
841 /* Read control and alarm 0 registers. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800842 ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700843 if (ret < 0)
844 return ret;
845
846 /* Set alarm 0, using 24-hour and day-of-month modes. */
847 regs[3] = bin2bcd(t->time.tm_sec);
848 regs[4] = bin2bcd(t->time.tm_min);
849 regs[5] = bin2bcd(t->time.tm_hour);
Tero Kristo62c8c202015-10-23 09:29:57 +0300850 regs[6] = bin2bcd(t->time.tm_wday + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700851 regs[7] = bin2bcd(t->time.tm_mday);
Tero Kristo62c8c202015-10-23 09:29:57 +0300852 regs[8] = bin2bcd(t->time.tm_mon + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700853
854 /* Clear the alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800855 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700856 /* Set alarm match: second, minute, hour, day, date, month. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800857 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
Nishanth Menone3edd672015-04-20 19:51:34 -0500858 /* Disable interrupt. We will not enable until completely programmed */
859 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700860
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800861 ret = ds1307->write_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700862 if (ret < 0)
863 return ret;
864
Nishanth Menone3edd672015-04-20 19:51:34 -0500865 if (!t->enabled)
866 return 0;
867 regs[0] |= MCP794XX_BIT_ALM0_EN;
868 return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, regs[0]);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700869}
870
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800871static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700872{
873 struct i2c_client *client = to_i2c_client(dev);
874 struct ds1307 *ds1307 = i2c_get_clientdata(client);
875 int reg;
876
877 if (!test_bit(HAS_ALARM, &ds1307->flags))
878 return -EINVAL;
879
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800880 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700881 if (reg < 0)
882 return reg;
883
884 if (enabled)
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800885 reg |= MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700886 else
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800887 reg &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700888
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800889 return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700890}
891
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800892static const struct rtc_class_ops mcp794xx_rtc_ops = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700893 .read_time = ds1307_get_time,
894 .set_time = ds1307_set_time,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800895 .read_alarm = mcp794xx_read_alarm,
896 .set_alarm = mcp794xx_set_alarm,
897 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
Simon Guinot1d1945d2014-04-03 14:49:55 -0700898};
899
900/*----------------------------------------------------------------------*/
901
David Brownell682d73f2007-11-14 16:58:32 -0800902static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700903ds1307_nvram_read(struct file *filp, struct kobject *kobj,
904 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800905 char *buf, loff_t off, size_t count)
906{
907 struct i2c_client *client;
908 struct ds1307 *ds1307;
David Brownell682d73f2007-11-14 16:58:32 -0800909 int result;
910
frederic Rodofcd8db02008-02-06 01:38:55 -0800911 client = kobj_to_i2c_client(kobj);
David Brownell682d73f2007-11-14 16:58:32 -0800912 ds1307 = i2c_get_clientdata(client);
913
Austin Boyle9eab0a72012-03-23 15:02:38 -0700914 result = ds1307->read_block_data(client, ds1307->nvram_offset + off,
915 count, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800916 if (result < 0)
David Brownell682d73f2007-11-14 16:58:32 -0800917 dev_err(&client->dev, "%s error %d\n", "nvram read", result);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800918 return result;
David Brownell682d73f2007-11-14 16:58:32 -0800919}
920
921static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700922ds1307_nvram_write(struct file *filp, struct kobject *kobj,
923 struct bin_attribute *attr,
David Brownell682d73f2007-11-14 16:58:32 -0800924 char *buf, loff_t off, size_t count)
925{
926 struct i2c_client *client;
Ed Swierk30e7b032009-03-31 15:24:56 -0700927 struct ds1307 *ds1307;
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800928 int result;
David Brownell682d73f2007-11-14 16:58:32 -0800929
frederic Rodofcd8db02008-02-06 01:38:55 -0800930 client = kobj_to_i2c_client(kobj);
Ed Swierk30e7b032009-03-31 15:24:56 -0700931 ds1307 = i2c_get_clientdata(client);
David Brownell682d73f2007-11-14 16:58:32 -0800932
Austin Boyle9eab0a72012-03-23 15:02:38 -0700933 result = ds1307->write_block_data(client, ds1307->nvram_offset + off,
934 count, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800935 if (result < 0) {
936 dev_err(&client->dev, "%s error %d\n", "nvram write", result);
937 return result;
938 }
939 return count;
David Brownell682d73f2007-11-14 16:58:32 -0800940}
941
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700942
David Brownell682d73f2007-11-14 16:58:32 -0800943/*----------------------------------------------------------------------*/
944
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700945static u8 do_trickle_setup_ds1339(struct i2c_client *client,
946 uint32_t ohms, bool diode)
947{
948 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
949 DS1307_TRICKLE_CHARGER_NO_DIODE;
950
951 switch (ohms) {
952 case 250:
953 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
954 break;
955 case 2000:
956 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
957 break;
958 case 4000:
959 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
960 break;
961 default:
962 dev_warn(&client->dev,
963 "Unsupported ohm value %u in dt\n", ohms);
964 return 0;
965 }
966 return setup;
967}
968
Tin Huynh9c19b892016-11-30 09:57:31 +0700969static void ds1307_trickle_init(struct i2c_client *client,
970 struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700971{
972 uint32_t ohms = 0;
973 bool diode = true;
974
975 if (!chip->do_trickle_setup)
976 goto out;
Tin Huynh9c19b892016-11-30 09:57:31 +0700977 if (device_property_read_u32(&client->dev, "trickle-resistor-ohms", &ohms))
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700978 goto out;
Tin Huynh9c19b892016-11-30 09:57:31 +0700979 if (device_property_read_bool(&client->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700980 diode = false;
981 chip->trickle_charger_setup = chip->do_trickle_setup(client,
982 ohms, diode);
983out:
984 return;
985}
986
Akinobu Mita445c0202016-01-25 00:22:16 +0900987/*----------------------------------------------------------------------*/
988
989#ifdef CONFIG_RTC_DRV_DS1307_HWMON
990
991/*
992 * Temperature sensor support for ds3231 devices.
993 */
994
995#define DS3231_REG_TEMPERATURE 0x11
996
997/*
998 * A user-initiated temperature conversion is not started by this function,
999 * so the temperature is updated once every 64 seconds.
1000 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001001static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +09001002{
1003 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1004 u8 temp_buf[2];
1005 s16 temp;
1006 int ret;
1007
1008 ret = ds1307->read_block_data(ds1307->client, DS3231_REG_TEMPERATURE,
1009 sizeof(temp_buf), temp_buf);
1010 if (ret < 0)
1011 return ret;
1012 if (ret != sizeof(temp_buf))
1013 return -EIO;
1014
1015 /*
1016 * Temperature is represented as a 10-bit code with a resolution of
1017 * 0.25 degree celsius and encoded in two's complement format.
1018 */
1019 temp = (temp_buf[0] << 8) | temp_buf[1];
1020 temp >>= 6;
1021 *mC = temp * 250;
1022
1023 return 0;
1024}
1025
1026static ssize_t ds3231_hwmon_show_temp(struct device *dev,
1027 struct device_attribute *attr, char *buf)
1028{
1029 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001030 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001031
1032 ret = ds3231_hwmon_read_temp(dev, &temp);
1033 if (ret)
1034 return ret;
1035
1036 return sprintf(buf, "%d\n", temp);
1037}
1038static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ds3231_hwmon_show_temp,
1039 NULL, 0);
1040
1041static struct attribute *ds3231_hwmon_attrs[] = {
1042 &sensor_dev_attr_temp1_input.dev_attr.attr,
1043 NULL,
1044};
1045ATTRIBUTE_GROUPS(ds3231_hwmon);
1046
1047static void ds1307_hwmon_register(struct ds1307 *ds1307)
1048{
1049 struct device *dev;
1050
1051 if (ds1307->type != ds_3231)
1052 return;
1053
1054 dev = devm_hwmon_device_register_with_groups(&ds1307->client->dev,
1055 ds1307->client->name,
1056 ds1307, ds3231_hwmon_groups);
1057 if (IS_ERR(dev)) {
1058 dev_warn(&ds1307->client->dev,
1059 "unable to register hwmon device %ld\n", PTR_ERR(dev));
1060 }
1061}
1062
1063#else
1064
1065static void ds1307_hwmon_register(struct ds1307 *ds1307)
1066{
1067}
1068
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001069#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1070
1071/*----------------------------------------------------------------------*/
1072
1073/*
1074 * Square-wave output support for DS3231
1075 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1076 */
1077#ifdef CONFIG_COMMON_CLK
1078
1079enum {
1080 DS3231_CLK_SQW = 0,
1081 DS3231_CLK_32KHZ,
1082};
1083
1084#define clk_sqw_to_ds1307(clk) \
1085 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1086#define clk_32khz_to_ds1307(clk) \
1087 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1088
1089static int ds3231_clk_sqw_rates[] = {
1090 1,
1091 1024,
1092 4096,
1093 8192,
1094};
1095
1096static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1097{
1098 struct i2c_client *client = ds1307->client;
1099 struct mutex *lock = &ds1307->rtc->ops_lock;
1100 int control;
1101 int ret;
1102
1103 mutex_lock(lock);
1104
1105 control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
1106 if (control < 0) {
1107 ret = control;
1108 goto out;
1109 }
1110
1111 control &= ~mask;
1112 control |= value;
1113
1114 ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
1115out:
1116 mutex_unlock(lock);
1117
1118 return ret;
1119}
1120
1121static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1122 unsigned long parent_rate)
1123{
1124 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1125 int control;
1126 int rate_sel = 0;
1127
1128 control = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_CONTROL);
1129 if (control < 0)
1130 return control;
1131 if (control & DS1337_BIT_RS1)
1132 rate_sel += 1;
1133 if (control & DS1337_BIT_RS2)
1134 rate_sel += 2;
1135
1136 return ds3231_clk_sqw_rates[rate_sel];
1137}
1138
1139static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
1140 unsigned long *prate)
1141{
1142 int i;
1143
1144 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1145 if (ds3231_clk_sqw_rates[i] <= rate)
1146 return ds3231_clk_sqw_rates[i];
1147 }
1148
1149 return 0;
1150}
1151
1152static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
1153 unsigned long parent_rate)
1154{
1155 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1156 int control = 0;
1157 int rate_sel;
1158
1159 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1160 rate_sel++) {
1161 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1162 break;
1163 }
1164
1165 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1166 return -EINVAL;
1167
1168 if (rate_sel & 1)
1169 control |= DS1337_BIT_RS1;
1170 if (rate_sel & 2)
1171 control |= DS1337_BIT_RS2;
1172
1173 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1174 control);
1175}
1176
1177static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1178{
1179 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1180
1181 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1182}
1183
1184static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1185{
1186 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1187
1188 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1189}
1190
1191static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1192{
1193 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1194 int control;
1195
1196 control = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_CONTROL);
1197 if (control < 0)
1198 return control;
1199
1200 return !(control & DS1337_BIT_INTCN);
1201}
1202
1203static const struct clk_ops ds3231_clk_sqw_ops = {
1204 .prepare = ds3231_clk_sqw_prepare,
1205 .unprepare = ds3231_clk_sqw_unprepare,
1206 .is_prepared = ds3231_clk_sqw_is_prepared,
1207 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1208 .round_rate = ds3231_clk_sqw_round_rate,
1209 .set_rate = ds3231_clk_sqw_set_rate,
1210};
1211
1212static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
1213 unsigned long parent_rate)
1214{
1215 return 32768;
1216}
1217
1218static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1219{
1220 struct i2c_client *client = ds1307->client;
1221 struct mutex *lock = &ds1307->rtc->ops_lock;
1222 int status;
1223 int ret;
1224
1225 mutex_lock(lock);
1226
1227 status = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
1228 if (status < 0) {
1229 ret = status;
1230 goto out;
1231 }
1232
1233 if (enable)
1234 status |= DS3231_BIT_EN32KHZ;
1235 else
1236 status &= ~DS3231_BIT_EN32KHZ;
1237
1238 ret = i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, status);
1239out:
1240 mutex_unlock(lock);
1241
1242 return ret;
1243}
1244
1245static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1246{
1247 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1248
1249 return ds3231_clk_32khz_control(ds1307, true);
1250}
1251
1252static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1253{
1254 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1255
1256 ds3231_clk_32khz_control(ds1307, false);
1257}
1258
1259static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1260{
1261 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1262 int status;
1263
1264 status = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_STATUS);
1265 if (status < 0)
1266 return status;
1267
1268 return !!(status & DS3231_BIT_EN32KHZ);
1269}
1270
1271static const struct clk_ops ds3231_clk_32khz_ops = {
1272 .prepare = ds3231_clk_32khz_prepare,
1273 .unprepare = ds3231_clk_32khz_unprepare,
1274 .is_prepared = ds3231_clk_32khz_is_prepared,
1275 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1276};
1277
1278static struct clk_init_data ds3231_clks_init[] = {
1279 [DS3231_CLK_SQW] = {
1280 .name = "ds3231_clk_sqw",
1281 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001282 },
1283 [DS3231_CLK_32KHZ] = {
1284 .name = "ds3231_clk_32khz",
1285 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001286 },
1287};
1288
1289static int ds3231_clks_register(struct ds1307 *ds1307)
1290{
1291 struct i2c_client *client = ds1307->client;
1292 struct device_node *node = client->dev.of_node;
1293 struct clk_onecell_data *onecell;
1294 int i;
1295
1296 onecell = devm_kzalloc(&client->dev, sizeof(*onecell), GFP_KERNEL);
1297 if (!onecell)
1298 return -ENOMEM;
1299
1300 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
1301 onecell->clks = devm_kcalloc(&client->dev, onecell->clk_num,
1302 sizeof(onecell->clks[0]), GFP_KERNEL);
1303 if (!onecell->clks)
1304 return -ENOMEM;
1305
1306 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1307 struct clk_init_data init = ds3231_clks_init[i];
1308
1309 /*
1310 * Interrupt signal due to alarm conditions and square-wave
1311 * output share same pin, so don't initialize both.
1312 */
1313 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1314 continue;
1315
1316 /* optional override of the clockname */
1317 of_property_read_string_index(node, "clock-output-names", i,
1318 &init.name);
1319 ds1307->clks[i].init = &init;
1320
1321 onecell->clks[i] = devm_clk_register(&client->dev,
1322 &ds1307->clks[i]);
1323 if (IS_ERR(onecell->clks[i]))
1324 return PTR_ERR(onecell->clks[i]);
1325 }
1326
1327 if (!node)
1328 return 0;
1329
1330 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1331
1332 return 0;
1333}
1334
1335static void ds1307_clks_register(struct ds1307 *ds1307)
1336{
1337 int ret;
1338
1339 if (ds1307->type != ds_3231)
1340 return;
1341
1342 ret = ds3231_clks_register(ds1307);
1343 if (ret) {
1344 dev_warn(&ds1307->client->dev,
1345 "unable to register clock device %d\n", ret);
1346 }
1347}
1348
1349#else
1350
1351static void ds1307_clks_register(struct ds1307 *ds1307)
1352{
1353}
1354
1355#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001356
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001357static int ds1307_probe(struct i2c_client *client,
1358 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001359{
1360 struct ds1307 *ds1307;
1361 int err = -ENODEV;
Keerthye29385f2016-06-01 16:19:07 +05301362 int tmp, wday;
Tin Huynh9c19b892016-11-30 09:57:31 +07001363 struct chip_desc *chip;
David Brownellc065f352007-07-17 04:05:10 -07001364 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Peter Senna Tschudinc8b18da2013-11-12 15:10:59 -08001365 bool want_irq = false;
Michael Lange8bc2a402016-01-21 18:10:16 +01001366 bool ds1307_can_wakeup_device = false;
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001367 unsigned char *buf;
Jingoo Han01ce8932013-11-12 15:10:41 -08001368 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Keerthye29385f2016-06-01 16:19:07 +05301369 struct rtc_time tm;
1370 unsigned long timestamp;
1371
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001372 irq_handler_t irq_handler = ds1307_irq;
1373
Wolfram Sang97f902b2009-06-17 16:26:10 -07001374 static const int bbsqi_bitpos[] = {
1375 [ds_1337] = 0,
1376 [ds_1339] = DS1339_BIT_BBSQI,
1377 [ds_3231] = DS3231_BIT_BBSQW,
1378 };
Simon Guinot1d1945d2014-04-03 14:49:55 -07001379 const struct rtc_class_ops *rtc_ops = &ds13xx_rtc_ops;
David Brownell1abb0dc2006-06-25 05:48:17 -07001380
Ed Swierk30e7b032009-03-31 15:24:56 -07001381 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)
1382 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
David Brownellc065f352007-07-17 04:05:10 -07001383 return -EIO;
David Brownell1abb0dc2006-06-25 05:48:17 -07001384
Jingoo Hanedca66d2013-07-03 15:07:05 -07001385 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001386 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001387 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001388
David Brownell1abb0dc2006-06-25 05:48:17 -07001389 i2c_set_clientdata(client, ds1307);
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001390
1391 ds1307->client = client;
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001392
1393 if (client->dev.of_node) {
1394 ds1307->type = (enum ds_type)
1395 of_device_get_match_data(&client->dev);
1396 chip = &chips[ds1307->type];
1397 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001398 chip = &chips[id->driver_data];
1399 ds1307->type = id->driver_data;
1400 } else {
1401 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001402
Tin Huynh9c19b892016-11-30 09:57:31 +07001403 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
1404 &client->dev);
1405 if (!acpi_id)
1406 return -ENODEV;
1407 chip = &chips[acpi_id->driver_data];
1408 ds1307->type = acpi_id->driver_data;
1409 }
1410
1411 if (!pdata)
1412 ds1307_trickle_init(client, chip);
1413 else if (pdata->trickle_charger_setup)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001414 chip->trickle_charger_setup = pdata->trickle_charger_setup;
1415
1416 if (chip->trickle_charger_setup && chip->trickle_charger_reg) {
1417 dev_dbg(&client->dev, "writing trickle charger info 0x%x to 0x%x\n",
1418 DS13XX_TRICKLE_CHARGER_MAGIC | chip->trickle_charger_setup,
1419 chip->trickle_charger_reg);
Wolfram Sangeb86c302012-05-29 15:07:38 -07001420 i2c_smbus_write_byte_data(client, chip->trickle_charger_reg,
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001421 DS13XX_TRICKLE_CHARGER_MAGIC |
1422 chip->trickle_charger_setup);
1423 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001424
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001425 buf = ds1307->regs;
Ed Swierk30e7b032009-03-31 15:24:56 -07001426 if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
Bertrand Achardbc48b902013-04-29 16:19:26 -07001427 ds1307->read_block_data = ds1307_native_smbus_read_block_data;
1428 ds1307->write_block_data = ds1307_native_smbus_write_block_data;
Ed Swierk30e7b032009-03-31 15:24:56 -07001429 } else {
1430 ds1307->read_block_data = ds1307_read_block_data;
1431 ds1307->write_block_data = ds1307_write_block_data;
1432 }
David Brownell045e0e82007-07-17 04:04:55 -07001433
Michael Lange8bc2a402016-01-21 18:10:16 +01001434#ifdef CONFIG_OF
1435/*
1436 * For devices with no IRQ directly connected to the SoC, the RTC chip
1437 * can be forced as a wakeup source by stating that explicitly in
1438 * the device's .dts file using the "wakeup-source" boolean property.
1439 * If the "wakeup-source" property is set, don't request an IRQ.
1440 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1441 * if supported by the RTC.
1442 */
1443 if (of_property_read_bool(client->dev.of_node, "wakeup-source")) {
1444 ds1307_can_wakeup_device = true;
1445 }
Alexandre Belloni78aaa062016-07-13 02:36:41 +02001446 /* Intersil ISL12057 DT backward compatibility */
1447 if (of_property_read_bool(client->dev.of_node,
1448 "isil,irq2-can-wakeup-machine")) {
1449 ds1307_can_wakeup_device = true;
1450 }
Michael Lange8bc2a402016-01-21 18:10:16 +01001451#endif
1452
David Brownell045e0e82007-07-17 04:04:55 -07001453 switch (ds1307->type) {
1454 case ds_1337:
1455 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001456 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001457 /* get registers that the "rtc" read below won't read... */
Ed Swierk30e7b032009-03-31 15:24:56 -07001458 tmp = ds1307->read_block_data(ds1307->client,
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001459 DS1337_REG_CONTROL, 2, buf);
David Brownell1abb0dc2006-06-25 05:48:17 -07001460 if (tmp != 2) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001461 dev_dbg(&client->dev, "read error %d\n", tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -07001462 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001463 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001464 }
1465
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001466 /* oscillator off? turn it on, so clock can tick. */
1467 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001468 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
1469
David Anders40ce9722012-03-23 15:02:37 -07001470 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001471 * Using IRQ or defined as wakeup-source?
1472 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001473 * For some variants, be sure alarms can trigger when we're
1474 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001475 */
Michael Lange8bc2a402016-01-21 18:10:16 +01001476 if (chip->alarm && (ds1307->client->irq > 0 ||
1477 ds1307_can_wakeup_device)) {
Wolfram Sang97f902b2009-06-17 16:26:10 -07001478 ds1307->regs[0] |= DS1337_BIT_INTCN
1479 | bbsqi_bitpos[ds1307->type];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001480 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Wolfram Sangb24a7262012-03-23 15:02:37 -07001481
1482 want_irq = true;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001483 }
1484
1485 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
1486 ds1307->regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001487
1488 /* oscillator fault? clear flag, and warn */
1489 if (ds1307->regs[1] & DS1337_BIT_OSF) {
1490 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
1491 ds1307->regs[1] & ~DS1337_BIT_OSF);
1492 dev_warn(&client->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001493 }
David Brownell045e0e82007-07-17 04:04:55 -07001494 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001495
1496 case rx_8025:
1497 tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
1498 RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
1499 if (tmp != 2) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001500 dev_dbg(&client->dev, "read error %d\n", tmp);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001501 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001502 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001503 }
1504
1505 /* oscillator off? turn it on, so clock can tick. */
1506 if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
1507 ds1307->regs[1] |= RX8025_BIT_XST;
1508 i2c_smbus_write_byte_data(client,
1509 RX8025_REG_CTRL2 << 4 | 0x08,
1510 ds1307->regs[1]);
1511 dev_warn(&client->dev,
1512 "oscillator stop detected - SET TIME!\n");
1513 }
1514
1515 if (ds1307->regs[1] & RX8025_BIT_PON) {
1516 ds1307->regs[1] &= ~RX8025_BIT_PON;
1517 i2c_smbus_write_byte_data(client,
1518 RX8025_REG_CTRL2 << 4 | 0x08,
1519 ds1307->regs[1]);
1520 dev_warn(&client->dev, "power-on detected\n");
1521 }
1522
1523 if (ds1307->regs[1] & RX8025_BIT_VDET) {
1524 ds1307->regs[1] &= ~RX8025_BIT_VDET;
1525 i2c_smbus_write_byte_data(client,
1526 RX8025_REG_CTRL2 << 4 | 0x08,
1527 ds1307->regs[1]);
1528 dev_warn(&client->dev, "voltage drop detected\n");
1529 }
1530
1531 /* make sure we are running in 24hour mode */
1532 if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
1533 u8 hour;
1534
1535 /* switch to 24 hour mode */
1536 i2c_smbus_write_byte_data(client,
1537 RX8025_REG_CTRL1 << 4 | 0x08,
1538 ds1307->regs[0] |
1539 RX8025_BIT_2412);
1540
1541 tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
1542 RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
1543 if (tmp != 2) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001544 dev_dbg(&client->dev, "read error %d\n", tmp);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001545 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001546 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001547 }
1548
1549 /* correct hour */
1550 hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
1551 if (hour == 12)
1552 hour = 0;
1553 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1554 hour += 12;
1555
1556 i2c_smbus_write_byte_data(client,
1557 DS1307_REG_HOUR << 4 | 0x08,
1558 hour);
1559 }
1560 break;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001561 case ds_1388:
1562 ds1307->offset = 1; /* Seconds starts at 1 */
1563 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001564 case mcp794xx:
1565 rtc_ops = &mcp794xx_rtc_ops;
Simon Guinot1d1945d2014-04-03 14:49:55 -07001566 if (ds1307->client->irq > 0 && chip->alarm) {
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001567 irq_handler = mcp794xx_irq;
Simon Guinot1d1945d2014-04-03 14:49:55 -07001568 want_irq = true;
1569 }
1570 break;
David Brownell045e0e82007-07-17 04:04:55 -07001571 default:
1572 break;
1573 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001574
1575read_rtc:
1576 /* read RTC registers */
Joakim Tjernlund96fc3a42010-06-29 15:05:34 -07001577 tmp = ds1307->read_block_data(ds1307->client, ds1307->offset, 8, buf);
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001578 if (tmp != 8) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001579 dev_dbg(&client->dev, "read error %d\n", tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -07001580 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001581 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001582 }
1583
David Anders40ce9722012-03-23 15:02:37 -07001584 /*
1585 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001586 * specify the extra bits as must-be-zero, but there are
1587 * still a few values that are clearly out-of-range.
1588 */
1589 tmp = ds1307->regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001590 switch (ds1307->type) {
1591 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001592 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001593 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001594 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001595 if (tmp & DS1307_BIT_CH) {
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001596 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
1597 dev_warn(&client->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001598 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001599 }
David Brownell045e0e82007-07-17 04:04:55 -07001600 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001601 case ds_1338:
1602 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001603 if (tmp & DS1307_BIT_CH)
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001604 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
1605
1606 /* oscillator fault? clear flag, and warn */
1607 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
1608 i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
David Brownellbd16f9e2007-07-26 10:41:00 -07001609 ds1307->regs[DS1307_REG_CONTROL]
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001610 & ~DS1338_BIT_OSF);
1611 dev_warn(&client->dev, "SET TIME!\n");
1612 goto read_rtc;
1613 }
David Brownell045e0e82007-07-17 04:04:55 -07001614 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001615 case ds_1340:
1616 /* clock halted? turn it on, so clock can tick. */
1617 if (tmp & DS1340_BIT_nEOSC)
1618 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
1619
1620 tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG);
1621 if (tmp < 0) {
Jingoo Han6df80e22013-04-29 16:19:28 -07001622 dev_dbg(&client->dev, "read error %d\n", tmp);
frederic Rodofcd8db02008-02-06 01:38:55 -08001623 err = -EIO;
Jingoo Hanedca66d2013-07-03 15:07:05 -07001624 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001625 }
1626
1627 /* oscillator fault? clear flag, and warn */
1628 if (tmp & DS1340_BIT_OSF) {
1629 i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0);
1630 dev_warn(&client->dev, "SET TIME!\n");
1631 }
1632 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001633 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001634 /* make sure that the backup battery is enabled */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001635 if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
David Anders43fcb812011-11-02 13:37:53 -07001636 i2c_smbus_write_byte_data(client, DS1307_REG_WDAY,
1637 ds1307->regs[DS1307_REG_WDAY]
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001638 | MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001639 }
1640
1641 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001642 if (!(tmp & MCP794XX_BIT_ST)) {
David Anders43fcb812011-11-02 13:37:53 -07001643 i2c_smbus_write_byte_data(client, DS1307_REG_SECS,
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001644 MCP794XX_BIT_ST);
David Anders43fcb812011-11-02 13:37:53 -07001645 dev_warn(&client->dev, "SET TIME!\n");
1646 goto read_rtc;
1647 }
1648
1649 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001650 default:
David Brownell045e0e82007-07-17 04:04:55 -07001651 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001652 }
David Brownell045e0e82007-07-17 04:04:55 -07001653
David Brownell1abb0dc2006-06-25 05:48:17 -07001654 tmp = ds1307->regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001655 switch (ds1307->type) {
1656 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001657 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001658 case m41t00:
David Anders40ce9722012-03-23 15:02:37 -07001659 /*
1660 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001661 * systems that will run through year 2100.
1662 */
1663 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001664 case rx_8025:
1665 break;
David Brownellc065f352007-07-17 04:05:10 -07001666 default:
1667 if (!(tmp & DS1307_BIT_12HR))
1668 break;
1669
David Anders40ce9722012-03-23 15:02:37 -07001670 /*
1671 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001672 * take note...
1673 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001674 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001675 if (tmp == 12)
1676 tmp = 0;
1677 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1678 tmp += 12;
David Brownell1abb0dc2006-06-25 05:48:17 -07001679 i2c_smbus_write_byte_data(client,
Joakim Tjernlund96fc3a42010-06-29 15:05:34 -07001680 ds1307->offset + DS1307_REG_HOUR,
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001681 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001682 }
1683
Keerthye29385f2016-06-01 16:19:07 +05301684 /*
1685 * Some IPs have weekday reset value = 0x1 which might not correct
1686 * hence compute the wday using the current date/month/year values
1687 */
1688 ds1307_get_time(&client->dev, &tm);
1689 wday = tm.tm_wday;
1690 timestamp = rtc_tm_to_time64(&tm);
1691 rtc_time64_to_tm(timestamp, &tm);
1692
1693 /*
1694 * Check if reset wday is different from the computed wday
1695 * If different then set the wday which we computed using
1696 * timestamp
1697 */
1698 if (wday != tm.tm_wday) {
1699 wday = i2c_smbus_read_byte_data(client, MCP794XX_REG_WEEKDAY);
1700 wday = wday & ~MCP794XX_REG_WEEKDAY_WDAY_MASK;
1701 wday = wday | (tm.tm_wday + 1);
1702 i2c_smbus_write_byte_data(client, MCP794XX_REG_WEEKDAY, wday);
1703 }
1704
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001705 if (want_irq) {
1706 device_set_wakeup_capable(&client->dev, true);
1707 set_bit(HAS_ALARM, &ds1307->flags);
1708 }
Jingoo Hanedca66d2013-07-03 15:07:05 -07001709 ds1307->rtc = devm_rtc_device_register(&client->dev, client->name,
Simon Guinot1d1945d2014-04-03 14:49:55 -07001710 rtc_ops, THIS_MODULE);
David Brownell1abb0dc2006-06-25 05:48:17 -07001711 if (IS_ERR(ds1307->rtc)) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001712 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001713 }
1714
Nishanth Menon38a7a732016-04-19 11:23:54 -05001715 if (ds1307_can_wakeup_device && ds1307->client->irq <= 0) {
Michael Lange8bc2a402016-01-21 18:10:16 +01001716 /* Disable request for an IRQ */
1717 want_irq = false;
1718 dev_info(&client->dev, "'wakeup-source' is set, request for an IRQ is disabled!\n");
1719 /* We cannot support UIE mode if we do not have an IRQ line */
1720 ds1307->rtc->uie_unsupported = 1;
1721 }
1722
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001723 if (want_irq) {
Nishanth Menonc5983192015-06-23 11:15:11 -05001724 err = devm_request_threaded_irq(&client->dev,
1725 client->irq, NULL, irq_handler,
1726 IRQF_SHARED | IRQF_ONESHOT,
1727 ds1307->rtc->name, client);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001728 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001729 client->irq = 0;
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001730 device_set_wakeup_capable(&client->dev, false);
1731 clear_bit(HAS_ALARM, &ds1307->flags);
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001732 dev_err(&client->dev, "unable to request IRQ!\n");
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001733 } else
Felipe Balbi51c4cfe2015-11-11 10:11:01 -06001734 dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001735 }
1736
Austin Boyle9eab0a72012-03-23 15:02:38 -07001737 if (chip->nvram_size) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001738
Jingoo Hanedca66d2013-07-03 15:07:05 -07001739 ds1307->nvram = devm_kzalloc(&client->dev,
1740 sizeof(struct bin_attribute),
1741 GFP_KERNEL);
Austin Boyle9eab0a72012-03-23 15:02:38 -07001742 if (!ds1307->nvram) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001743 dev_err(&client->dev, "cannot allocate memory for nvram sysfs\n");
1744 } else {
1745
1746 ds1307->nvram->attr.name = "nvram";
1747 ds1307->nvram->attr.mode = S_IRUGO | S_IWUSR;
1748
1749 sysfs_bin_attr_init(ds1307->nvram);
1750
1751 ds1307->nvram->read = ds1307_nvram_read;
1752 ds1307->nvram->write = ds1307_nvram_write;
1753 ds1307->nvram->size = chip->nvram_size;
1754 ds1307->nvram_offset = chip->nvram_offset;
1755
1756 err = sysfs_create_bin_file(&client->dev.kobj,
1757 ds1307->nvram);
1758 if (err) {
1759 dev_err(&client->dev,
1760 "unable to create sysfs file: %s\n",
1761 ds1307->nvram->attr.name);
1762 } else {
1763 set_bit(HAS_NVRAM, &ds1307->flags);
1764 dev_info(&client->dev, "%zu bytes nvram\n",
1765 ds1307->nvram->size);
1766 }
David Brownell682d73f2007-11-14 16:58:32 -08001767 }
1768 }
1769
Akinobu Mita445c0202016-01-25 00:22:16 +09001770 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001771 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001772
David Brownell1abb0dc2006-06-25 05:48:17 -07001773 return 0;
1774
Jingoo Hanedca66d2013-07-03 15:07:05 -07001775exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001776 return err;
1777}
1778
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001779static int ds1307_remove(struct i2c_client *client)
David Brownell1abb0dc2006-06-25 05:48:17 -07001780{
David Anders40ce9722012-03-23 15:02:37 -07001781 struct ds1307 *ds1307 = i2c_get_clientdata(client);
David Brownell1abb0dc2006-06-25 05:48:17 -07001782
Jingoo Hanedca66d2013-07-03 15:07:05 -07001783 if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
Austin Boyle9eab0a72012-03-23 15:02:38 -07001784 sysfs_remove_bin_file(&client->dev.kobj, ds1307->nvram);
David Brownell682d73f2007-11-14 16:58:32 -08001785
David Brownell1abb0dc2006-06-25 05:48:17 -07001786 return 0;
1787}
1788
1789static struct i2c_driver ds1307_driver = {
1790 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001791 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001792 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001793 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001794 },
David Brownellc065f352007-07-17 04:05:10 -07001795 .probe = ds1307_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001796 .remove = ds1307_remove,
Jean Delvare3760f732008-04-29 23:11:40 +02001797 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001798};
1799
Axel Lin0abc9202012-03-23 15:02:31 -07001800module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001801
1802MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1803MODULE_LICENSE("GPL");