blob: 4b43aa62fbc789b049ecf05b3be7ec393dfa868d [file] [log] [blame]
David Brownell1abb0dc2006-06-25 05:48:17 -07001/*
2 * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips.
3 *
4 * Copyright (C) 2005 James Chapman (ds1337 core)
5 * Copyright (C) 2006 David Brownell
Matthias Fuchsa2166852009-03-31 15:24:58 -07006 * Copyright (C) 2009 Matthias Fuchs (rx8025 support)
Bertrand Achardbc48b902013-04-29 16:19:26 -07007 * Copyright (C) 2012 Bertrand Achard (nvram access fixes)
David Brownell1abb0dc2006-06-25 05:48:17 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Tin Huynh9c19b892016-11-30 09:57:31 +070014#include <linux/acpi.h>
David Brownell1abb0dc2006-06-25 05:48:17 -070015#include <linux/bcd.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050016#include <linux/i2c.h>
17#include <linux/init.h>
18#include <linux/module.h>
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -030019#include <linux/of_device.h>
Wolfram Sangeb86c302012-05-29 15:07:38 -070020#include <linux/rtc/ds1307.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050021#include <linux/rtc.h>
22#include <linux/slab.h>
23#include <linux/string.h>
Akinobu Mita445c0202016-01-25 00:22:16 +090024#include <linux/hwmon.h>
25#include <linux/hwmon-sysfs.h>
Akinobu Mita6c6ff142016-01-31 23:10:10 +090026#include <linux/clk-provider.h>
Heiner Kallweit11e58902017-03-10 18:52:34 +010027#include <linux/regmap.h>
David Brownell1abb0dc2006-06-25 05:48:17 -070028
David Anders40ce9722012-03-23 15:02:37 -070029/*
30 * We can't determine type by probing, but if we expect pre-Linux code
David Brownell1abb0dc2006-06-25 05:48:17 -070031 * to have set the chip up as a clock (turning on the oscillator and
32 * setting the date and time), Linux can ignore the non-clock features.
33 * That's a natural job for a factory or repair bench.
David Brownell1abb0dc2006-06-25 05:48:17 -070034 */
35enum ds_type {
David Brownell045e0e82007-07-17 04:04:55 -070036 ds_1307,
Sean Nyekjaer300a7732017-06-08 12:36:54 +020037 ds_1308,
David Brownell045e0e82007-07-17 04:04:55 -070038 ds_1337,
39 ds_1338,
40 ds_1339,
41 ds_1340,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -070042 ds_1388,
Wolfram Sang97f902b2009-06-17 16:26:10 -070043 ds_3231,
Stefan Agner8566f702017-03-23 16:54:57 -070044 m41t0,
David Brownell045e0e82007-07-17 04:04:55 -070045 m41t00,
Tomas Novotnyf4199f82014-12-10 15:53:57 -080046 mcp794xx,
Matthias Fuchsa2166852009-03-31 15:24:58 -070047 rx_8025,
Marek Vasutee0981b2017-06-18 22:55:28 +020048 rx_8130,
Wolfram Sang32d322b2012-03-23 15:02:36 -070049 last_ds_type /* always last */
David Anders40ce9722012-03-23 15:02:37 -070050 /* rs5c372 too? different address... */
David Brownell1abb0dc2006-06-25 05:48:17 -070051};
52
David Brownell1abb0dc2006-06-25 05:48:17 -070053
54/* RTC registers don't differ much, except for the century flag */
55#define DS1307_REG_SECS 0x00 /* 00-59 */
56# define DS1307_BIT_CH 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070057# define DS1340_BIT_nEOSC 0x80
Tomas Novotnyf4199f82014-12-10 15:53:57 -080058# define MCP794XX_BIT_ST 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070059#define DS1307_REG_MIN 0x01 /* 00-59 */
Stefan Agner8566f702017-03-23 16:54:57 -070060# define M41T0_BIT_OF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070061#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
David Brownellc065f352007-07-17 04:05:10 -070062# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
63# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
David Brownell1abb0dc2006-06-25 05:48:17 -070064# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
65# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
66#define DS1307_REG_WDAY 0x03 /* 01-07 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -080067# define MCP794XX_BIT_VBATEN 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -070068#define DS1307_REG_MDAY 0x04 /* 01-31 */
69#define DS1307_REG_MONTH 0x05 /* 01-12 */
70# define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
71#define DS1307_REG_YEAR 0x06 /* 00-99 */
72
David Anders40ce9722012-03-23 15:02:37 -070073/*
74 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
David Brownell045e0e82007-07-17 04:04:55 -070075 * start at 7, and they differ a LOT. Only control and status matter for
76 * basic RTC date and time functionality; be careful using them.
David Brownell1abb0dc2006-06-25 05:48:17 -070077 */
David Brownell045e0e82007-07-17 04:04:55 -070078#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
David Brownell1abb0dc2006-06-25 05:48:17 -070079# define DS1307_BIT_OUT 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070080# define DS1338_BIT_OSF 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070081# define DS1307_BIT_SQWE 0x10
82# define DS1307_BIT_RS1 0x02
83# define DS1307_BIT_RS0 0x01
84#define DS1337_REG_CONTROL 0x0e
85# define DS1337_BIT_nEOSC 0x80
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070086# define DS1339_BIT_BBSQI 0x20
Wolfram Sang97f902b2009-06-17 16:26:10 -070087# define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
David Brownell1abb0dc2006-06-25 05:48:17 -070088# define DS1337_BIT_RS2 0x10
89# define DS1337_BIT_RS1 0x08
90# define DS1337_BIT_INTCN 0x04
91# define DS1337_BIT_A2IE 0x02
92# define DS1337_BIT_A1IE 0x01
David Brownell045e0e82007-07-17 04:04:55 -070093#define DS1340_REG_CONTROL 0x07
94# define DS1340_BIT_OUT 0x80
95# define DS1340_BIT_FT 0x40
96# define DS1340_BIT_CALIB_SIGN 0x20
97# define DS1340_M_CALIBRATION 0x1f
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070098#define DS1340_REG_FLAG 0x09
99# define DS1340_BIT_OSF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -0700100#define DS1337_REG_STATUS 0x0f
101# define DS1337_BIT_OSF 0x80
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900102# define DS3231_BIT_EN32KHZ 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -0700103# define DS1337_BIT_A2I 0x02
104# define DS1337_BIT_A1I 0x01
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700105#define DS1339_REG_ALARM1_SECS 0x07
Wolfram Sangeb86c302012-05-29 15:07:38 -0700106
107#define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
David Brownell1abb0dc2006-06-25 05:48:17 -0700108
Matthias Fuchsa2166852009-03-31 15:24:58 -0700109#define RX8025_REG_CTRL1 0x0e
110# define RX8025_BIT_2412 0x20
111#define RX8025_REG_CTRL2 0x0f
112# define RX8025_BIT_PON 0x10
113# define RX8025_BIT_VDET 0x40
114# define RX8025_BIT_XST 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -0700115
116
117struct ds1307 {
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700118 u8 offset; /* register's offset */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700119 u8 regs[11];
Austin Boyle9eab0a72012-03-23 15:02:38 -0700120 u16 nvram_offset;
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200121 struct nvmem_config nvmem_cfg;
David Brownell1abb0dc2006-06-25 05:48:17 -0700122 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700123 unsigned long flags;
124#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
125#define HAS_ALARM 1 /* bit 1 == irq claimed */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100126 struct device *dev;
127 struct regmap *regmap;
128 const char *name;
129 int irq;
David Brownell1abb0dc2006-06-25 05:48:17 -0700130 struct rtc_device *rtc;
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900131#ifdef CONFIG_COMMON_CLK
132 struct clk_hw clks[2];
133#endif
David Brownell1abb0dc2006-06-25 05:48:17 -0700134};
135
David Brownell045e0e82007-07-17 04:04:55 -0700136struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700137 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700138 u16 nvram_offset;
139 u16 nvram_size;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200140 u8 century_reg;
141 u8 century_enable_bit;
142 u8 century_bit;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700143 u16 trickle_charger_reg;
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700144 u8 trickle_charger_setup;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100145 u8 (*do_trickle_setup)(struct ds1307 *, uint32_t,
146 bool);
David Brownell045e0e82007-07-17 04:04:55 -0700147};
148
Heiner Kallweit11e58902017-03-10 18:52:34 +0100149static u8 do_trickle_setup_ds1339(struct ds1307 *, uint32_t ohms, bool diode);
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700150
151static struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700152 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700153 .nvram_offset = 8,
154 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700155 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200156 [ds_1308] = {
157 .nvram_offset = 8,
158 .nvram_size = 56,
159 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700160 [ds_1337] = {
161 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200162 .century_reg = DS1307_REG_MONTH,
163 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700164 },
165 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700166 .nvram_offset = 8,
167 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700168 },
169 [ds_1339] = {
170 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200171 .century_reg = DS1307_REG_MONTH,
172 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700173 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700174 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700175 },
176 [ds_1340] = {
Heiner Kallweite48585d2017-06-05 17:57:33 +0200177 .century_reg = DS1307_REG_HOUR,
178 .century_enable_bit = DS1340_BIT_CENTURY_EN,
179 .century_bit = DS1340_BIT_CENTURY,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700180 .trickle_charger_reg = 0x08,
181 },
182 [ds_1388] = {
183 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700184 },
185 [ds_3231] = {
186 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200187 .century_reg = DS1307_REG_MONTH,
188 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700189 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200190 [rx_8130] = {
191 .alarm = 1,
192 /* this is battery backed SRAM */
193 .nvram_offset = 0x20,
194 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
195 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800196 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700197 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700198 /* this is battery backed SRAM */
199 .nvram_offset = 0x20,
200 .nvram_size = 0x40,
201 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700202};
David Brownell045e0e82007-07-17 04:04:55 -0700203
Jean Delvare3760f732008-04-29 23:11:40 +0200204static const struct i2c_device_id ds1307_id[] = {
205 { "ds1307", ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200206 { "ds1308", ds_1308 },
Jean Delvare3760f732008-04-29 23:11:40 +0200207 { "ds1337", ds_1337 },
208 { "ds1338", ds_1338 },
209 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700210 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200211 { "ds1340", ds_1340 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700212 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700213 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200214 { "m41t00", m41t00 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800215 { "mcp7940x", mcp794xx },
216 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700217 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700218 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200219 { "isl12057", ds_1337 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200220 { "rx8130", rx_8130 },
Jean Delvare3760f732008-04-29 23:11:40 +0200221 { }
222};
223MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700224
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300225#ifdef CONFIG_OF
226static const struct of_device_id ds1307_of_match[] = {
227 {
228 .compatible = "dallas,ds1307",
229 .data = (void *)ds_1307
230 },
231 {
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200232 .compatible = "dallas,ds1308",
233 .data = (void *)ds_1308
234 },
235 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300236 .compatible = "dallas,ds1337",
237 .data = (void *)ds_1337
238 },
239 {
240 .compatible = "dallas,ds1338",
241 .data = (void *)ds_1338
242 },
243 {
244 .compatible = "dallas,ds1339",
245 .data = (void *)ds_1339
246 },
247 {
248 .compatible = "dallas,ds1388",
249 .data = (void *)ds_1388
250 },
251 {
252 .compatible = "dallas,ds1340",
253 .data = (void *)ds_1340
254 },
255 {
256 .compatible = "maxim,ds3231",
257 .data = (void *)ds_3231
258 },
259 {
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200260 .compatible = "st,m41t0",
261 .data = (void *)m41t00
262 },
263 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300264 .compatible = "st,m41t00",
265 .data = (void *)m41t00
266 },
267 {
268 .compatible = "microchip,mcp7940x",
269 .data = (void *)mcp794xx
270 },
271 {
272 .compatible = "microchip,mcp7941x",
273 .data = (void *)mcp794xx
274 },
275 {
276 .compatible = "pericom,pt7c4338",
277 .data = (void *)ds_1307
278 },
279 {
280 .compatible = "epson,rx8025",
281 .data = (void *)rx_8025
282 },
283 {
284 .compatible = "isil,isl12057",
285 .data = (void *)ds_1337
286 },
287 { }
288};
289MODULE_DEVICE_TABLE(of, ds1307_of_match);
290#endif
291
Tin Huynh9c19b892016-11-30 09:57:31 +0700292#ifdef CONFIG_ACPI
293static const struct acpi_device_id ds1307_acpi_ids[] = {
294 { .id = "DS1307", .driver_data = ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200295 { .id = "DS1308", .driver_data = ds_1308 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700296 { .id = "DS1337", .driver_data = ds_1337 },
297 { .id = "DS1338", .driver_data = ds_1338 },
298 { .id = "DS1339", .driver_data = ds_1339 },
299 { .id = "DS1388", .driver_data = ds_1388 },
300 { .id = "DS1340", .driver_data = ds_1340 },
301 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700302 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700303 { .id = "M41T00", .driver_data = m41t00 },
304 { .id = "MCP7940X", .driver_data = mcp794xx },
305 { .id = "MCP7941X", .driver_data = mcp794xx },
306 { .id = "PT7C4338", .driver_data = ds_1307 },
307 { .id = "RX8025", .driver_data = rx_8025 },
308 { .id = "ISL12057", .driver_data = ds_1337 },
309 { }
310};
311MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
312#endif
313
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700314/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700315 * The ds1337 and ds1339 both have two alarms, but we only use the first
316 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
317 * signal; ds1339 chips have only one alarm signal.
318 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500319static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700320{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100321 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500322 struct mutex *lock = &ds1307->rtc->ops_lock;
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200323 int stat, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700324
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700325 mutex_lock(lock);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100326 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
327 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700328 goto out;
329
330 if (stat & DS1337_BIT_A1I) {
331 stat &= ~DS1337_BIT_A1I;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100332 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700333
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200334 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
335 DS1337_BIT_A1IE, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100336 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700337 goto out;
338
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700339 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700340 }
341
342out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700343 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700344
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700345 return IRQ_HANDLED;
346}
347
348/*----------------------------------------------------------------------*/
349
David Brownell1abb0dc2006-06-25 05:48:17 -0700350static int ds1307_get_time(struct device *dev, struct rtc_time *t)
351{
352 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100353 int tmp, ret;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200354 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell1abb0dc2006-06-25 05:48:17 -0700355
David Brownell045e0e82007-07-17 04:04:55 -0700356 /* read the RTC date and time registers all at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100357 ret = regmap_bulk_read(ds1307->regmap, ds1307->offset, ds1307->regs, 7);
358 if (ret) {
359 dev_err(dev, "%s error %d\n", "read", ret);
360 return ret;
David Brownell1abb0dc2006-06-25 05:48:17 -0700361 }
362
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800363 dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700364
Stefan Agner8566f702017-03-23 16:54:57 -0700365 /* if oscillator fail bit is set, no data can be trusted */
366 if (ds1307->type == m41t0 &&
367 ds1307->regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
368 dev_warn_once(dev, "oscillator failed, set time!\n");
369 return -EINVAL;
370 }
371
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700372 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
373 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700374 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700375 t->tm_hour = bcd2bin(tmp);
376 t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
377 t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700378 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700379 t->tm_mon = bcd2bin(tmp) - 1;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700380 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700381
Heiner Kallweite48585d2017-06-05 17:57:33 +0200382 if (ds1307->regs[chip->century_reg] & chip->century_bit &&
383 IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY))
384 t->tm_year += 100;
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200385
David Brownell1abb0dc2006-06-25 05:48:17 -0700386 dev_dbg(dev, "%s secs=%d, mins=%d, "
387 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
388 "read", t->tm_sec, t->tm_min,
389 t->tm_hour, t->tm_mday,
390 t->tm_mon, t->tm_year, t->tm_wday);
391
David Brownell045e0e82007-07-17 04:04:55 -0700392 /* initial clock setting can be undefined */
393 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700394}
395
396static int ds1307_set_time(struct device *dev, struct rtc_time *t)
397{
398 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweite48585d2017-06-05 17:57:33 +0200399 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell1abb0dc2006-06-25 05:48:17 -0700400 int result;
401 int tmp;
402 u8 *buf = ds1307->regs;
403
404 dev_dbg(dev, "%s secs=%d, mins=%d, "
405 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400406 "write", t->tm_sec, t->tm_min,
407 t->tm_hour, t->tm_mday,
408 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700409
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200410 if (t->tm_year < 100)
411 return -EINVAL;
412
Heiner Kallweite48585d2017-06-05 17:57:33 +0200413#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
414 if (t->tm_year > (chip->century_bit ? 299 : 199))
415 return -EINVAL;
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200416#else
Heiner Kallweite48585d2017-06-05 17:57:33 +0200417 if (t->tm_year > 199)
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200418 return -EINVAL;
419#endif
420
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700421 buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
422 buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
423 buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
424 buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
425 buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
426 buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700427
428 /* assume 20YY not 19YY */
429 tmp = t->tm_year - 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700430 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700431
Heiner Kallweite48585d2017-06-05 17:57:33 +0200432 if (chip->century_enable_bit)
433 buf[chip->century_reg] |= chip->century_enable_bit;
434 if (t->tm_year > 199 && chip->century_bit)
435 buf[chip->century_reg] |= chip->century_bit;
436
437 if (ds1307->type == mcp794xx) {
David Anders40ce9722012-03-23 15:02:37 -0700438 /*
439 * these bits were cleared when preparing the date/time
440 * values and need to be set again before writing the
441 * buffer out to the device.
442 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800443 buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
444 buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700445 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700446
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800447 dev_dbg(dev, "%s: %7ph\n", "write", buf);
David Brownell1abb0dc2006-06-25 05:48:17 -0700448
Heiner Kallweit11e58902017-03-10 18:52:34 +0100449 result = regmap_bulk_write(ds1307->regmap, ds1307->offset, buf, 7);
450 if (result) {
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800451 dev_err(dev, "%s error %d\n", "write", result);
452 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700453 }
454 return 0;
455}
456
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800457static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700458{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100459 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700460 int ret;
461
462 if (!test_bit(HAS_ALARM, &ds1307->flags))
463 return -EINVAL;
464
465 /* read all ALARM1, ALARM2, and status registers at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100466 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
467 ds1307->regs, 9);
468 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700469 dev_err(dev, "%s error %d\n", "alarm read", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100470 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700471 }
472
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100473 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
474 &ds1307->regs[0], &ds1307->regs[4], &ds1307->regs[7]);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700475
David Anders40ce9722012-03-23 15:02:37 -0700476 /*
477 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700478 * and that all four fields are checked matches
479 */
480 t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
481 t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
482 t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
483 t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700484
485 /* ... and status */
486 t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
487 t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
488
489 dev_dbg(dev, "%s secs=%d, mins=%d, "
490 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
491 "alarm read", t->time.tm_sec, t->time.tm_min,
492 t->time.tm_hour, t->time.tm_mday,
493 t->enabled, t->pending);
494
495 return 0;
496}
497
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800498static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700499{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100500 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700501 unsigned char *buf = ds1307->regs;
502 u8 control, status;
503 int ret;
504
505 if (!test_bit(HAS_ALARM, &ds1307->flags))
506 return -EINVAL;
507
508 dev_dbg(dev, "%s secs=%d, mins=%d, "
509 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
510 "alarm set", t->time.tm_sec, t->time.tm_min,
511 t->time.tm_hour, t->time.tm_mday,
512 t->enabled, t->pending);
513
514 /* read current status of both alarms and the chip */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100515 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, buf, 9);
516 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700517 dev_err(dev, "%s error %d\n", "alarm write", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100518 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700519 }
520 control = ds1307->regs[7];
521 status = ds1307->regs[8];
522
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100523 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
524 &ds1307->regs[0], &ds1307->regs[4], control, status);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700525
526 /* set ALARM1, using 24 hour and day-of-month modes */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700527 buf[0] = bin2bcd(t->time.tm_sec);
528 buf[1] = bin2bcd(t->time.tm_min);
529 buf[2] = bin2bcd(t->time.tm_hour);
530 buf[3] = bin2bcd(t->time.tm_mday);
531
532 /* set ALARM2 to non-garbage */
533 buf[4] = 0;
534 buf[5] = 0;
535 buf[6] = 0;
536
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200537 /* disable alarms */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700538 buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700539 buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
540
Heiner Kallweit11e58902017-03-10 18:52:34 +0100541 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, buf, 9);
542 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700543 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800544 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700545 }
546
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200547 /* optionally enable ALARM1 */
548 if (t->enabled) {
549 dev_dbg(dev, "alarm IRQ armed\n");
550 buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100551 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, buf[7]);
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200552 }
553
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700554 return 0;
555}
556
John Stultz16380c12011-02-02 17:02:41 -0800557static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700558{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100559 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700560
John Stultz16380c12011-02-02 17:02:41 -0800561 if (!test_bit(HAS_ALARM, &ds1307->flags))
562 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700563
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200564 return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
565 DS1337_BIT_A1IE,
566 enabled ? DS1337_BIT_A1IE : 0);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700567}
568
David Brownellff8371a2006-09-30 23:28:17 -0700569static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700570 .read_time = ds1307_get_time,
571 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800572 .read_alarm = ds1337_read_alarm,
573 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800574 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700575};
576
David Brownell682d73f2007-11-14 16:58:32 -0800577/*----------------------------------------------------------------------*/
578
Simon Guinot1d1945d2014-04-03 14:49:55 -0700579/*
Marek Vasutee0981b2017-06-18 22:55:28 +0200580 * Alarm support for rx8130 devices.
581 */
582
583#define RX8130_REG_ALARM_MIN 0x07
584#define RX8130_REG_ALARM_HOUR 0x08
585#define RX8130_REG_ALARM_WEEK_OR_DAY 0x09
586#define RX8130_REG_EXTENSION 0x0c
587#define RX8130_REG_EXTENSION_WADA (1 << 3)
588#define RX8130_REG_FLAG 0x0d
589#define RX8130_REG_FLAG_AF (1 << 3)
590#define RX8130_REG_CONTROL0 0x0e
591#define RX8130_REG_CONTROL0_AIE (1 << 3)
592
593static irqreturn_t rx8130_irq(int irq, void *dev_id)
594{
595 struct ds1307 *ds1307 = dev_id;
596 struct mutex *lock = &ds1307->rtc->ops_lock;
597 u8 ctl[3];
598 int ret;
599
600 mutex_lock(lock);
601
602 /* Read control registers. */
603 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
604 if (ret < 0)
605 goto out;
606 if (!(ctl[1] & RX8130_REG_FLAG_AF))
607 goto out;
608 ctl[1] &= ~RX8130_REG_FLAG_AF;
609 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
610
611 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
612 if (ret < 0)
613 goto out;
614
615 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
616
617out:
618 mutex_unlock(lock);
619
620 return IRQ_HANDLED;
621}
622
623static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
624{
625 struct ds1307 *ds1307 = dev_get_drvdata(dev);
626 u8 ald[3], ctl[3];
627 int ret;
628
629 if (!test_bit(HAS_ALARM, &ds1307->flags))
630 return -EINVAL;
631
632 /* Read alarm registers. */
633 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, 3);
634 if (ret < 0)
635 return ret;
636
637 /* Read control registers. */
638 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
639 if (ret < 0)
640 return ret;
641
642 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
643 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
644
645 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
646 t->time.tm_sec = -1;
647 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
648 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
649 t->time.tm_wday = -1;
650 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
651 t->time.tm_mon = -1;
652 t->time.tm_year = -1;
653 t->time.tm_yday = -1;
654 t->time.tm_isdst = -1;
655
656 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
657 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
658 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
659
660 return 0;
661}
662
663static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
664{
665 struct ds1307 *ds1307 = dev_get_drvdata(dev);
666 u8 ald[3], ctl[3];
667 int ret;
668
669 if (!test_bit(HAS_ALARM, &ds1307->flags))
670 return -EINVAL;
671
672 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
673 "enabled=%d pending=%d\n", __func__,
674 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
675 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
676 t->enabled, t->pending);
677
678 /* Read control registers. */
679 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
680 if (ret < 0)
681 return ret;
682
683 ctl[0] &= ~RX8130_REG_EXTENSION_WADA;
684 ctl[1] |= RX8130_REG_FLAG_AF;
685 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
686
687 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
688 if (ret < 0)
689 return ret;
690
691 /* Hardware alarm precision is 1 minute! */
692 ald[0] = bin2bcd(t->time.tm_min);
693 ald[1] = bin2bcd(t->time.tm_hour);
694 ald[2] = bin2bcd(t->time.tm_mday);
695
696 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, 3);
697 if (ret < 0)
698 return ret;
699
700 if (!t->enabled)
701 return 0;
702
703 ctl[2] |= RX8130_REG_CONTROL0_AIE;
704
705 return regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
706}
707
708static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
709{
710 struct ds1307 *ds1307 = dev_get_drvdata(dev);
711 int ret, reg;
712
713 if (!test_bit(HAS_ALARM, &ds1307->flags))
714 return -EINVAL;
715
716 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
717 if (ret < 0)
718 return ret;
719
720 if (enabled)
721 reg |= RX8130_REG_CONTROL0_AIE;
722 else
723 reg &= ~RX8130_REG_CONTROL0_AIE;
724
725 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
726}
727
728static const struct rtc_class_ops rx8130_rtc_ops = {
729 .read_time = ds1307_get_time,
730 .set_time = ds1307_set_time,
731 .read_alarm = rx8130_read_alarm,
732 .set_alarm = rx8130_set_alarm,
733 .alarm_irq_enable = rx8130_alarm_irq_enable,
734};
735
736/*----------------------------------------------------------------------*/
737
738/*
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800739 * Alarm support for mcp794xx devices.
Simon Guinot1d1945d2014-04-03 14:49:55 -0700740 */
741
Keerthye29385f2016-06-01 16:19:07 +0530742#define MCP794XX_REG_WEEKDAY 0x3
743#define MCP794XX_REG_WEEKDAY_WDAY_MASK 0x7
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800744#define MCP794XX_REG_CONTROL 0x07
745# define MCP794XX_BIT_ALM0_EN 0x10
746# define MCP794XX_BIT_ALM1_EN 0x20
747#define MCP794XX_REG_ALARM0_BASE 0x0a
748#define MCP794XX_REG_ALARM0_CTRL 0x0d
749#define MCP794XX_REG_ALARM1_BASE 0x11
750#define MCP794XX_REG_ALARM1_CTRL 0x14
751# define MCP794XX_BIT_ALMX_IF (1 << 3)
752# define MCP794XX_BIT_ALMX_C0 (1 << 4)
753# define MCP794XX_BIT_ALMX_C1 (1 << 5)
754# define MCP794XX_BIT_ALMX_C2 (1 << 6)
755# define MCP794XX_BIT_ALMX_POL (1 << 7)
756# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
757 MCP794XX_BIT_ALMX_C1 | \
758 MCP794XX_BIT_ALMX_C2)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700759
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500760static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700761{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100762 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500763 struct mutex *lock = &ds1307->rtc->ops_lock;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700764 int reg, ret;
765
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500766 mutex_lock(lock);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700767
768 /* Check and clear alarm 0 interrupt flag. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100769 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
770 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700771 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800772 if (!(reg & MCP794XX_BIT_ALMX_IF))
Simon Guinot1d1945d2014-04-03 14:49:55 -0700773 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800774 reg &= ~MCP794XX_BIT_ALMX_IF;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100775 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
776 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700777 goto out;
778
779 /* Disable alarm 0. */
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200780 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
781 MCP794XX_BIT_ALM0_EN, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100782 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700783 goto out;
784
785 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
786
787out:
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500788 mutex_unlock(lock);
789
790 return IRQ_HANDLED;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700791}
792
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800793static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700794{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100795 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700796 u8 *regs = ds1307->regs;
797 int ret;
798
799 if (!test_bit(HAS_ALARM, &ds1307->flags))
800 return -EINVAL;
801
802 /* Read control and alarm 0 registers. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100803 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
804 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700805 return ret;
806
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800807 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700808
809 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
810 t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f);
811 t->time.tm_min = bcd2bin(ds1307->regs[4] & 0x7f);
812 t->time.tm_hour = bcd2bin(ds1307->regs[5] & 0x3f);
813 t->time.tm_wday = bcd2bin(ds1307->regs[6] & 0x7) - 1;
814 t->time.tm_mday = bcd2bin(ds1307->regs[7] & 0x3f);
815 t->time.tm_mon = bcd2bin(ds1307->regs[8] & 0x1f) - 1;
816 t->time.tm_year = -1;
817 t->time.tm_yday = -1;
818 t->time.tm_isdst = -1;
819
820 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
821 "enabled=%d polarity=%d irq=%d match=%d\n", __func__,
822 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
823 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800824 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL),
825 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF),
826 (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700827
828 return 0;
829}
830
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800831static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700832{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100833 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700834 unsigned char *regs = ds1307->regs;
835 int ret;
836
837 if (!test_bit(HAS_ALARM, &ds1307->flags))
838 return -EINVAL;
839
840 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
841 "enabled=%d pending=%d\n", __func__,
842 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
843 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
844 t->enabled, t->pending);
845
846 /* Read control and alarm 0 registers. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100847 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
848 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700849 return ret;
850
851 /* Set alarm 0, using 24-hour and day-of-month modes. */
852 regs[3] = bin2bcd(t->time.tm_sec);
853 regs[4] = bin2bcd(t->time.tm_min);
854 regs[5] = bin2bcd(t->time.tm_hour);
Tero Kristo62c8c202015-10-23 09:29:57 +0300855 regs[6] = bin2bcd(t->time.tm_wday + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700856 regs[7] = bin2bcd(t->time.tm_mday);
Tero Kristo62c8c202015-10-23 09:29:57 +0300857 regs[8] = bin2bcd(t->time.tm_mon + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700858
859 /* Clear the alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800860 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700861 /* Set alarm match: second, minute, hour, day, date, month. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800862 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
Nishanth Menone3edd672015-04-20 19:51:34 -0500863 /* Disable interrupt. We will not enable until completely programmed */
864 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700865
Heiner Kallweit11e58902017-03-10 18:52:34 +0100866 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
867 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700868 return ret;
869
Nishanth Menone3edd672015-04-20 19:51:34 -0500870 if (!t->enabled)
871 return 0;
872 regs[0] |= MCP794XX_BIT_ALM0_EN;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100873 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700874}
875
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800876static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700877{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100878 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700879
880 if (!test_bit(HAS_ALARM, &ds1307->flags))
881 return -EINVAL;
882
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200883 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
884 MCP794XX_BIT_ALM0_EN,
885 enabled ? MCP794XX_BIT_ALM0_EN : 0);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700886}
887
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800888static const struct rtc_class_ops mcp794xx_rtc_ops = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700889 .read_time = ds1307_get_time,
890 .set_time = ds1307_set_time,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800891 .read_alarm = mcp794xx_read_alarm,
892 .set_alarm = mcp794xx_set_alarm,
893 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
Simon Guinot1d1945d2014-04-03 14:49:55 -0700894};
895
896/*----------------------------------------------------------------------*/
897
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200898static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
899 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -0800900{
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200901 struct ds1307 *ds1307 = priv;
David Brownell682d73f2007-11-14 16:58:32 -0800902
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200903 return regmap_bulk_read(ds1307->regmap, ds1307->nvram_offset + offset,
904 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -0800905}
906
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200907static int ds1307_nvram_write(void *priv, unsigned int offset, void *val,
908 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -0800909{
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200910 struct ds1307 *ds1307 = priv;
David Brownell682d73f2007-11-14 16:58:32 -0800911
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200912 return regmap_bulk_write(ds1307->regmap, ds1307->nvram_offset + offset,
913 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -0800914}
915
David Brownell682d73f2007-11-14 16:58:32 -0800916/*----------------------------------------------------------------------*/
917
Heiner Kallweit11e58902017-03-10 18:52:34 +0100918static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700919 uint32_t ohms, bool diode)
920{
921 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
922 DS1307_TRICKLE_CHARGER_NO_DIODE;
923
924 switch (ohms) {
925 case 250:
926 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
927 break;
928 case 2000:
929 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
930 break;
931 case 4000:
932 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
933 break;
934 default:
Heiner Kallweit11e58902017-03-10 18:52:34 +0100935 dev_warn(ds1307->dev,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700936 "Unsupported ohm value %u in dt\n", ohms);
937 return 0;
938 }
939 return setup;
940}
941
Heiner Kallweit11e58902017-03-10 18:52:34 +0100942static void ds1307_trickle_init(struct ds1307 *ds1307,
Tin Huynh9c19b892016-11-30 09:57:31 +0700943 struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700944{
945 uint32_t ohms = 0;
946 bool diode = true;
947
948 if (!chip->do_trickle_setup)
949 goto out;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100950 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
951 &ohms))
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700952 goto out;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100953 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700954 diode = false;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100955 chip->trickle_charger_setup = chip->do_trickle_setup(ds1307,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700956 ohms, diode);
957out:
958 return;
959}
960
Akinobu Mita445c0202016-01-25 00:22:16 +0900961/*----------------------------------------------------------------------*/
962
963#ifdef CONFIG_RTC_DRV_DS1307_HWMON
964
965/*
966 * Temperature sensor support for ds3231 devices.
967 */
968
969#define DS3231_REG_TEMPERATURE 0x11
970
971/*
972 * A user-initiated temperature conversion is not started by this function,
973 * so the temperature is updated once every 64 seconds.
974 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +0900975static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +0900976{
977 struct ds1307 *ds1307 = dev_get_drvdata(dev);
978 u8 temp_buf[2];
979 s16 temp;
980 int ret;
981
Heiner Kallweit11e58902017-03-10 18:52:34 +0100982 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
983 temp_buf, sizeof(temp_buf));
984 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +0900985 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +0900986 /*
987 * Temperature is represented as a 10-bit code with a resolution of
988 * 0.25 degree celsius and encoded in two's complement format.
989 */
990 temp = (temp_buf[0] << 8) | temp_buf[1];
991 temp >>= 6;
992 *mC = temp * 250;
993
994 return 0;
995}
996
997static ssize_t ds3231_hwmon_show_temp(struct device *dev,
998 struct device_attribute *attr, char *buf)
999{
1000 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001001 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001002
1003 ret = ds3231_hwmon_read_temp(dev, &temp);
1004 if (ret)
1005 return ret;
1006
1007 return sprintf(buf, "%d\n", temp);
1008}
1009static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ds3231_hwmon_show_temp,
1010 NULL, 0);
1011
1012static struct attribute *ds3231_hwmon_attrs[] = {
1013 &sensor_dev_attr_temp1_input.dev_attr.attr,
1014 NULL,
1015};
1016ATTRIBUTE_GROUPS(ds3231_hwmon);
1017
1018static void ds1307_hwmon_register(struct ds1307 *ds1307)
1019{
1020 struct device *dev;
1021
1022 if (ds1307->type != ds_3231)
1023 return;
1024
Heiner Kallweit11e58902017-03-10 18:52:34 +01001025 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Akinobu Mita445c0202016-01-25 00:22:16 +09001026 ds1307, ds3231_hwmon_groups);
1027 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001028 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1029 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001030 }
1031}
1032
1033#else
1034
1035static void ds1307_hwmon_register(struct ds1307 *ds1307)
1036{
1037}
1038
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001039#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1040
1041/*----------------------------------------------------------------------*/
1042
1043/*
1044 * Square-wave output support for DS3231
1045 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1046 */
1047#ifdef CONFIG_COMMON_CLK
1048
1049enum {
1050 DS3231_CLK_SQW = 0,
1051 DS3231_CLK_32KHZ,
1052};
1053
1054#define clk_sqw_to_ds1307(clk) \
1055 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1056#define clk_32khz_to_ds1307(clk) \
1057 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1058
1059static int ds3231_clk_sqw_rates[] = {
1060 1,
1061 1024,
1062 4096,
1063 8192,
1064};
1065
1066static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1067{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001068 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001069 int ret;
1070
1071 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001072 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1073 mask, value);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001074 mutex_unlock(lock);
1075
1076 return ret;
1077}
1078
1079static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1080 unsigned long parent_rate)
1081{
1082 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001083 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001084 int rate_sel = 0;
1085
Heiner Kallweit11e58902017-03-10 18:52:34 +01001086 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1087 if (ret)
1088 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001089 if (control & DS1337_BIT_RS1)
1090 rate_sel += 1;
1091 if (control & DS1337_BIT_RS2)
1092 rate_sel += 2;
1093
1094 return ds3231_clk_sqw_rates[rate_sel];
1095}
1096
1097static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
1098 unsigned long *prate)
1099{
1100 int i;
1101
1102 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1103 if (ds3231_clk_sqw_rates[i] <= rate)
1104 return ds3231_clk_sqw_rates[i];
1105 }
1106
1107 return 0;
1108}
1109
1110static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
1111 unsigned long parent_rate)
1112{
1113 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1114 int control = 0;
1115 int rate_sel;
1116
1117 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1118 rate_sel++) {
1119 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1120 break;
1121 }
1122
1123 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1124 return -EINVAL;
1125
1126 if (rate_sel & 1)
1127 control |= DS1337_BIT_RS1;
1128 if (rate_sel & 2)
1129 control |= DS1337_BIT_RS2;
1130
1131 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1132 control);
1133}
1134
1135static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1136{
1137 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1138
1139 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1140}
1141
1142static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1143{
1144 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1145
1146 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1147}
1148
1149static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1150{
1151 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001152 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001153
Heiner Kallweit11e58902017-03-10 18:52:34 +01001154 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1155 if (ret)
1156 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001157
1158 return !(control & DS1337_BIT_INTCN);
1159}
1160
1161static const struct clk_ops ds3231_clk_sqw_ops = {
1162 .prepare = ds3231_clk_sqw_prepare,
1163 .unprepare = ds3231_clk_sqw_unprepare,
1164 .is_prepared = ds3231_clk_sqw_is_prepared,
1165 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1166 .round_rate = ds3231_clk_sqw_round_rate,
1167 .set_rate = ds3231_clk_sqw_set_rate,
1168};
1169
1170static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
1171 unsigned long parent_rate)
1172{
1173 return 32768;
1174}
1175
1176static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1177{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001178 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001179 int ret;
1180
1181 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001182 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1183 DS3231_BIT_EN32KHZ,
1184 enable ? DS3231_BIT_EN32KHZ : 0);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001185 mutex_unlock(lock);
1186
1187 return ret;
1188}
1189
1190static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1191{
1192 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1193
1194 return ds3231_clk_32khz_control(ds1307, true);
1195}
1196
1197static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1198{
1199 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1200
1201 ds3231_clk_32khz_control(ds1307, false);
1202}
1203
1204static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1205{
1206 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001207 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001208
Heiner Kallweit11e58902017-03-10 18:52:34 +01001209 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1210 if (ret)
1211 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001212
1213 return !!(status & DS3231_BIT_EN32KHZ);
1214}
1215
1216static const struct clk_ops ds3231_clk_32khz_ops = {
1217 .prepare = ds3231_clk_32khz_prepare,
1218 .unprepare = ds3231_clk_32khz_unprepare,
1219 .is_prepared = ds3231_clk_32khz_is_prepared,
1220 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1221};
1222
1223static struct clk_init_data ds3231_clks_init[] = {
1224 [DS3231_CLK_SQW] = {
1225 .name = "ds3231_clk_sqw",
1226 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001227 },
1228 [DS3231_CLK_32KHZ] = {
1229 .name = "ds3231_clk_32khz",
1230 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001231 },
1232};
1233
1234static int ds3231_clks_register(struct ds1307 *ds1307)
1235{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001236 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001237 struct clk_onecell_data *onecell;
1238 int i;
1239
Heiner Kallweit11e58902017-03-10 18:52:34 +01001240 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001241 if (!onecell)
1242 return -ENOMEM;
1243
1244 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001245 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1246 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001247 if (!onecell->clks)
1248 return -ENOMEM;
1249
1250 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1251 struct clk_init_data init = ds3231_clks_init[i];
1252
1253 /*
1254 * Interrupt signal due to alarm conditions and square-wave
1255 * output share same pin, so don't initialize both.
1256 */
1257 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1258 continue;
1259
1260 /* optional override of the clockname */
1261 of_property_read_string_index(node, "clock-output-names", i,
1262 &init.name);
1263 ds1307->clks[i].init = &init;
1264
Heiner Kallweit11e58902017-03-10 18:52:34 +01001265 onecell->clks[i] = devm_clk_register(ds1307->dev,
1266 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001267 if (IS_ERR(onecell->clks[i]))
1268 return PTR_ERR(onecell->clks[i]);
1269 }
1270
1271 if (!node)
1272 return 0;
1273
1274 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1275
1276 return 0;
1277}
1278
1279static void ds1307_clks_register(struct ds1307 *ds1307)
1280{
1281 int ret;
1282
1283 if (ds1307->type != ds_3231)
1284 return;
1285
1286 ret = ds3231_clks_register(ds1307);
1287 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001288 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1289 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001290 }
1291}
1292
1293#else
1294
1295static void ds1307_clks_register(struct ds1307 *ds1307)
1296{
1297}
1298
1299#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001300
Heiner Kallweit11e58902017-03-10 18:52:34 +01001301static const struct regmap_config regmap_config = {
1302 .reg_bits = 8,
1303 .val_bits = 8,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001304};
1305
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001306static int ds1307_probe(struct i2c_client *client,
1307 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001308{
1309 struct ds1307 *ds1307;
1310 int err = -ENODEV;
Keerthye29385f2016-06-01 16:19:07 +05301311 int tmp, wday;
Tin Huynh9c19b892016-11-30 09:57:31 +07001312 struct chip_desc *chip;
Peter Senna Tschudinc8b18da2013-11-12 15:10:59 -08001313 bool want_irq = false;
Michael Lange8bc2a402016-01-21 18:10:16 +01001314 bool ds1307_can_wakeup_device = false;
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001315 unsigned char *buf;
Jingoo Han01ce8932013-11-12 15:10:41 -08001316 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Keerthye29385f2016-06-01 16:19:07 +05301317 struct rtc_time tm;
1318 unsigned long timestamp;
1319
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001320 irq_handler_t irq_handler = ds1307_irq;
1321
Wolfram Sang97f902b2009-06-17 16:26:10 -07001322 static const int bbsqi_bitpos[] = {
1323 [ds_1337] = 0,
1324 [ds_1339] = DS1339_BIT_BBSQI,
1325 [ds_3231] = DS3231_BIT_BBSQW,
1326 };
Simon Guinot1d1945d2014-04-03 14:49:55 -07001327 const struct rtc_class_ops *rtc_ops = &ds13xx_rtc_ops;
David Brownell1abb0dc2006-06-25 05:48:17 -07001328
Jingoo Hanedca66d2013-07-03 15:07:05 -07001329 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001330 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001331 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001332
Heiner Kallweit11e58902017-03-10 18:52:34 +01001333 dev_set_drvdata(&client->dev, ds1307);
1334 ds1307->dev = &client->dev;
1335 ds1307->name = client->name;
1336 ds1307->irq = client->irq;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001337
Heiner Kallweit11e58902017-03-10 18:52:34 +01001338 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1339 if (IS_ERR(ds1307->regmap)) {
1340 dev_err(ds1307->dev, "regmap allocation failed\n");
1341 return PTR_ERR(ds1307->regmap);
1342 }
1343
1344 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001345
1346 if (client->dev.of_node) {
1347 ds1307->type = (enum ds_type)
1348 of_device_get_match_data(&client->dev);
1349 chip = &chips[ds1307->type];
1350 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001351 chip = &chips[id->driver_data];
1352 ds1307->type = id->driver_data;
1353 } else {
1354 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001355
Tin Huynh9c19b892016-11-30 09:57:31 +07001356 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001357 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001358 if (!acpi_id)
1359 return -ENODEV;
1360 chip = &chips[acpi_id->driver_data];
1361 ds1307->type = acpi_id->driver_data;
1362 }
1363
1364 if (!pdata)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001365 ds1307_trickle_init(ds1307, chip);
Tin Huynh9c19b892016-11-30 09:57:31 +07001366 else if (pdata->trickle_charger_setup)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001367 chip->trickle_charger_setup = pdata->trickle_charger_setup;
1368
1369 if (chip->trickle_charger_setup && chip->trickle_charger_reg) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001370 dev_dbg(ds1307->dev,
1371 "writing trickle charger info 0x%x to 0x%x\n",
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001372 DS13XX_TRICKLE_CHARGER_MAGIC | chip->trickle_charger_setup,
1373 chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001374 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001375 DS13XX_TRICKLE_CHARGER_MAGIC |
1376 chip->trickle_charger_setup);
1377 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001378
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001379 buf = ds1307->regs;
David Brownell045e0e82007-07-17 04:04:55 -07001380
Michael Lange8bc2a402016-01-21 18:10:16 +01001381#ifdef CONFIG_OF
1382/*
1383 * For devices with no IRQ directly connected to the SoC, the RTC chip
1384 * can be forced as a wakeup source by stating that explicitly in
1385 * the device's .dts file using the "wakeup-source" boolean property.
1386 * If the "wakeup-source" property is set, don't request an IRQ.
1387 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1388 * if supported by the RTC.
1389 */
1390 if (of_property_read_bool(client->dev.of_node, "wakeup-source")) {
1391 ds1307_can_wakeup_device = true;
1392 }
Alexandre Belloni78aaa062016-07-13 02:36:41 +02001393 /* Intersil ISL12057 DT backward compatibility */
1394 if (of_property_read_bool(client->dev.of_node,
1395 "isil,irq2-can-wakeup-machine")) {
1396 ds1307_can_wakeup_device = true;
1397 }
Michael Lange8bc2a402016-01-21 18:10:16 +01001398#endif
1399
David Brownell045e0e82007-07-17 04:04:55 -07001400 switch (ds1307->type) {
1401 case ds_1337:
1402 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001403 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001404 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001405 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
1406 buf, 2);
1407 if (err) {
1408 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001409 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001410 }
1411
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001412 /* oscillator off? turn it on, so clock can tick. */
1413 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001414 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
1415
David Anders40ce9722012-03-23 15:02:37 -07001416 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001417 * Using IRQ or defined as wakeup-source?
1418 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001419 * For some variants, be sure alarms can trigger when we're
1420 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001421 */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001422 if (chip->alarm && (ds1307->irq > 0 ||
1423 ds1307_can_wakeup_device)) {
Wolfram Sang97f902b2009-06-17 16:26:10 -07001424 ds1307->regs[0] |= DS1337_BIT_INTCN
1425 | bbsqi_bitpos[ds1307->type];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001426 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Wolfram Sangb24a7262012-03-23 15:02:37 -07001427
1428 want_irq = true;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001429 }
1430
Heiner Kallweit11e58902017-03-10 18:52:34 +01001431 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
1432 ds1307->regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001433
1434 /* oscillator fault? clear flag, and warn */
1435 if (ds1307->regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001436 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
1437 ds1307->regs[1] & ~DS1337_BIT_OSF);
1438 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001439 }
David Brownell045e0e82007-07-17 04:04:55 -07001440 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001441
1442 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001443 err = regmap_bulk_read(ds1307->regmap,
1444 RX8025_REG_CTRL1 << 4 | 0x08, buf, 2);
1445 if (err) {
1446 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001447 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001448 }
1449
1450 /* oscillator off? turn it on, so clock can tick. */
1451 if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
1452 ds1307->regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001453 regmap_write(ds1307->regmap,
1454 RX8025_REG_CTRL2 << 4 | 0x08,
1455 ds1307->regs[1]);
1456 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001457 "oscillator stop detected - SET TIME!\n");
1458 }
1459
1460 if (ds1307->regs[1] & RX8025_BIT_PON) {
1461 ds1307->regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001462 regmap_write(ds1307->regmap,
1463 RX8025_REG_CTRL2 << 4 | 0x08,
1464 ds1307->regs[1]);
1465 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001466 }
1467
1468 if (ds1307->regs[1] & RX8025_BIT_VDET) {
1469 ds1307->regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001470 regmap_write(ds1307->regmap,
1471 RX8025_REG_CTRL2 << 4 | 0x08,
1472 ds1307->regs[1]);
1473 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001474 }
1475
1476 /* make sure we are running in 24hour mode */
1477 if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
1478 u8 hour;
1479
1480 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001481 regmap_write(ds1307->regmap,
1482 RX8025_REG_CTRL1 << 4 | 0x08,
1483 ds1307->regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001484
Heiner Kallweit11e58902017-03-10 18:52:34 +01001485 err = regmap_bulk_read(ds1307->regmap,
1486 RX8025_REG_CTRL1 << 4 | 0x08,
1487 buf, 2);
1488 if (err) {
1489 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001490 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001491 }
1492
1493 /* correct hour */
1494 hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
1495 if (hour == 12)
1496 hour = 0;
1497 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1498 hour += 12;
1499
Heiner Kallweit11e58902017-03-10 18:52:34 +01001500 regmap_write(ds1307->regmap,
1501 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001502 }
1503 break;
Marek Vasutee0981b2017-06-18 22:55:28 +02001504 case rx_8130:
1505 ds1307->offset = 0x10; /* Seconds starts at 0x10 */
1506 rtc_ops = &rx8130_rtc_ops;
1507 if (chip->alarm && ds1307->irq > 0) {
1508 irq_handler = rx8130_irq;
1509 want_irq = true;
1510 }
1511 break;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001512 case ds_1388:
1513 ds1307->offset = 1; /* Seconds starts at 1 */
1514 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001515 case mcp794xx:
1516 rtc_ops = &mcp794xx_rtc_ops;
David Lowe80663602017-04-22 18:28:00 +01001517 if (chip->alarm && (ds1307->irq > 0 ||
1518 ds1307_can_wakeup_device)) {
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001519 irq_handler = mcp794xx_irq;
Simon Guinot1d1945d2014-04-03 14:49:55 -07001520 want_irq = true;
1521 }
1522 break;
David Brownell045e0e82007-07-17 04:04:55 -07001523 default:
1524 break;
1525 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001526
1527read_rtc:
1528 /* read RTC registers */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001529 err = regmap_bulk_read(ds1307->regmap, ds1307->offset, buf, 8);
1530 if (err) {
1531 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001532 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001533 }
1534
David Anders40ce9722012-03-23 15:02:37 -07001535 /*
1536 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001537 * specify the extra bits as must-be-zero, but there are
1538 * still a few values that are clearly out-of-range.
1539 */
1540 tmp = ds1307->regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001541 switch (ds1307->type) {
1542 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001543 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001544 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001545 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001546 if (tmp & DS1307_BIT_CH) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001547 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1548 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001549 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001550 }
David Brownell045e0e82007-07-17 04:04:55 -07001551 break;
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001552 case ds_1308:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001553 case ds_1338:
1554 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001555 if (tmp & DS1307_BIT_CH)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001556 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001557
1558 /* oscillator fault? clear flag, and warn */
1559 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001560 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
1561 ds1307->regs[DS1307_REG_CONTROL] &
1562 ~DS1338_BIT_OSF);
1563 dev_warn(ds1307->dev, "SET TIME!\n");
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001564 goto read_rtc;
1565 }
David Brownell045e0e82007-07-17 04:04:55 -07001566 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001567 case ds_1340:
1568 /* clock halted? turn it on, so clock can tick. */
1569 if (tmp & DS1340_BIT_nEOSC)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001570 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
frederic Rodofcd8db02008-02-06 01:38:55 -08001571
Heiner Kallweit11e58902017-03-10 18:52:34 +01001572 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1573 if (err) {
1574 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001575 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001576 }
1577
1578 /* oscillator fault? clear flag, and warn */
1579 if (tmp & DS1340_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001580 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1581 dev_warn(ds1307->dev, "SET TIME!\n");
frederic Rodofcd8db02008-02-06 01:38:55 -08001582 }
1583 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001584 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001585 /* make sure that the backup battery is enabled */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001586 if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001587 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
1588 ds1307->regs[DS1307_REG_WDAY] |
1589 MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001590 }
1591
1592 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001593 if (!(tmp & MCP794XX_BIT_ST)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001594 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1595 MCP794XX_BIT_ST);
1596 dev_warn(ds1307->dev, "SET TIME!\n");
David Anders43fcb812011-11-02 13:37:53 -07001597 goto read_rtc;
1598 }
1599
1600 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001601 default:
David Brownell045e0e82007-07-17 04:04:55 -07001602 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001603 }
David Brownell045e0e82007-07-17 04:04:55 -07001604
David Brownell1abb0dc2006-06-25 05:48:17 -07001605 tmp = ds1307->regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001606 switch (ds1307->type) {
1607 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001608 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001609 case m41t00:
David Anders40ce9722012-03-23 15:02:37 -07001610 /*
1611 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001612 * systems that will run through year 2100.
1613 */
1614 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001615 case rx_8025:
1616 break;
David Brownellc065f352007-07-17 04:05:10 -07001617 default:
1618 if (!(tmp & DS1307_BIT_12HR))
1619 break;
1620
David Anders40ce9722012-03-23 15:02:37 -07001621 /*
1622 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001623 * take note...
1624 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001625 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001626 if (tmp == 12)
1627 tmp = 0;
1628 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1629 tmp += 12;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001630 regmap_write(ds1307->regmap, ds1307->offset + DS1307_REG_HOUR,
1631 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001632 }
1633
Keerthye29385f2016-06-01 16:19:07 +05301634 /*
1635 * Some IPs have weekday reset value = 0x1 which might not correct
1636 * hence compute the wday using the current date/month/year values
1637 */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001638 ds1307_get_time(ds1307->dev, &tm);
Keerthye29385f2016-06-01 16:19:07 +05301639 wday = tm.tm_wday;
1640 timestamp = rtc_tm_to_time64(&tm);
1641 rtc_time64_to_tm(timestamp, &tm);
1642
1643 /*
1644 * Check if reset wday is different from the computed wday
1645 * If different then set the wday which we computed using
1646 * timestamp
1647 */
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001648 if (wday != tm.tm_wday)
1649 regmap_update_bits(ds1307->regmap, MCP794XX_REG_WEEKDAY,
1650 MCP794XX_REG_WEEKDAY_WDAY_MASK,
1651 tm.tm_wday + 1);
Keerthye29385f2016-06-01 16:19:07 +05301652
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001653 if (want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001654 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001655 set_bit(HAS_ALARM, &ds1307->flags);
1656 }
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001657
1658 ds1307->rtc = devm_rtc_allocate_device(ds1307->dev);
David Brownell1abb0dc2006-06-25 05:48:17 -07001659 if (IS_ERR(ds1307->rtc)) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001660 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001661 }
1662
Heiner Kallweit11e58902017-03-10 18:52:34 +01001663 if (ds1307_can_wakeup_device && ds1307->irq <= 0) {
Michael Lange8bc2a402016-01-21 18:10:16 +01001664 /* Disable request for an IRQ */
1665 want_irq = false;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001666 dev_info(ds1307->dev,
1667 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001668 /* We cannot support UIE mode if we do not have an IRQ line */
1669 ds1307->rtc->uie_unsupported = 1;
1670 }
1671
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001672 if (want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001673 err = devm_request_threaded_irq(ds1307->dev,
1674 ds1307->irq, NULL, irq_handler,
Nishanth Menonc5983192015-06-23 11:15:11 -05001675 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001676 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001677 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001678 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001679 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001680 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001681 dev_err(ds1307->dev, "unable to request IRQ!\n");
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001682 } else
Heiner Kallweit11e58902017-03-10 18:52:34 +01001683 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001684 }
1685
Austin Boyle9eab0a72012-03-23 15:02:38 -07001686 if (chip->nvram_size) {
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001687 ds1307->nvmem_cfg.name = "ds1307_nvram";
1688 ds1307->nvmem_cfg.word_size = 1;
1689 ds1307->nvmem_cfg.stride = 1;
1690 ds1307->nvmem_cfg.size = chip->nvram_size;
1691 ds1307->nvmem_cfg.reg_read = ds1307_nvram_read;
1692 ds1307->nvmem_cfg.reg_write = ds1307_nvram_write;
1693 ds1307->nvmem_cfg.priv = ds1307;
1694 ds1307->nvram_offset = chip->nvram_offset;
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001695
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001696 ds1307->rtc->nvmem_config = &ds1307->nvmem_cfg;
1697 ds1307->rtc->nvram_old_abi = true;
David Brownell682d73f2007-11-14 16:58:32 -08001698 }
1699
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001700 ds1307->rtc->ops = rtc_ops;
1701 err = rtc_register_device(ds1307->rtc);
1702 if (err)
1703 return err;
1704
Akinobu Mita445c0202016-01-25 00:22:16 +09001705 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001706 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001707
David Brownell1abb0dc2006-06-25 05:48:17 -07001708 return 0;
1709
Jingoo Hanedca66d2013-07-03 15:07:05 -07001710exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001711 return err;
1712}
1713
David Brownell1abb0dc2006-06-25 05:48:17 -07001714static struct i2c_driver ds1307_driver = {
1715 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001716 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001717 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001718 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001719 },
David Brownellc065f352007-07-17 04:05:10 -07001720 .probe = ds1307_probe,
Jean Delvare3760f732008-04-29 23:11:40 +02001721 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001722};
1723
Axel Lin0abc9202012-03-23 15:02:31 -07001724module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001725
1726MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1727MODULE_LICENSE("GPL");