blob: e7d9215c9201b14fe66aaefb8c7379aeb8ae6e00 [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,
Nikita Yushchenko0759c882017-08-24 09:32:11 +030042 ds_1341,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -070043 ds_1388,
Wolfram Sang97f902b2009-06-17 16:26:10 -070044 ds_3231,
Stefan Agner8566f702017-03-23 16:54:57 -070045 m41t0,
David Brownell045e0e82007-07-17 04:04:55 -070046 m41t00,
Tomas Novotnyf4199f82014-12-10 15:53:57 -080047 mcp794xx,
Matthias Fuchsa2166852009-03-31 15:24:58 -070048 rx_8025,
Marek Vasutee0981b2017-06-18 22:55:28 +020049 rx_8130,
Wolfram Sang32d322b2012-03-23 15:02:36 -070050 last_ds_type /* always last */
David Anders40ce9722012-03-23 15:02:37 -070051 /* rs5c372 too? different address... */
David Brownell1abb0dc2006-06-25 05:48:17 -070052};
53
David Brownell1abb0dc2006-06-25 05:48:17 -070054/* 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
David Brownell1abb0dc2006-06-25 05:48:17 -0700116struct ds1307 {
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200117 struct nvmem_config nvmem_cfg;
David Brownell1abb0dc2006-06-25 05:48:17 -0700118 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700119 unsigned long flags;
120#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
121#define HAS_ALARM 1 /* bit 1 == irq claimed */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100122 struct device *dev;
123 struct regmap *regmap;
124 const char *name;
David Brownell1abb0dc2006-06-25 05:48:17 -0700125 struct rtc_device *rtc;
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900126#ifdef CONFIG_COMMON_CLK
127 struct clk_hw clks[2];
128#endif
David Brownell1abb0dc2006-06-25 05:48:17 -0700129};
130
David Brownell045e0e82007-07-17 04:04:55 -0700131struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700132 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700133 u16 nvram_offset;
134 u16 nvram_size;
Heiner Kallweite5531702017-07-12 07:49:47 +0200135 u8 offset; /* register's offset */
Heiner Kallweite48585d2017-06-05 17:57:33 +0200136 u8 century_reg;
137 u8 century_enable_bit;
138 u8 century_bit;
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200139 u8 bbsqi_bit;
Heiner Kallweit45947122017-07-12 07:49:41 +0200140 irq_handler_t irq_handler;
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200141 const struct rtc_class_ops *rtc_ops;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700142 u16 trickle_charger_reg;
Alexandre Belloni57ec2d92017-09-04 22:46:04 +0200143 u8 (*do_trickle_setup)(struct ds1307 *, u32,
Heiner Kallweit11e58902017-03-10 18:52:34 +0100144 bool);
David Brownell045e0e82007-07-17 04:04:55 -0700145};
146
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200147static int ds1307_get_time(struct device *dev, struct rtc_time *t);
148static int ds1307_set_time(struct device *dev, struct rtc_time *t);
Alexandre Belloni57ec2d92017-09-04 22:46:04 +0200149static u8 do_trickle_setup_ds1339(struct ds1307 *, u32 ohms, bool diode);
Heiner Kallweit45947122017-07-12 07:49:41 +0200150static irqreturn_t rx8130_irq(int irq, void *dev_id);
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200151static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t);
152static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t);
153static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled);
Heiner Kallweit45947122017-07-12 07:49:41 +0200154static irqreturn_t mcp794xx_irq(int irq, void *dev_id);
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200155static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t);
156static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t);
157static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled);
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700158
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200159static const struct rtc_class_ops rx8130_rtc_ops = {
160 .read_time = ds1307_get_time,
161 .set_time = ds1307_set_time,
162 .read_alarm = rx8130_read_alarm,
163 .set_alarm = rx8130_set_alarm,
164 .alarm_irq_enable = rx8130_alarm_irq_enable,
165};
166
167static const struct rtc_class_ops mcp794xx_rtc_ops = {
168 .read_time = ds1307_get_time,
169 .set_time = ds1307_set_time,
170 .read_alarm = mcp794xx_read_alarm,
171 .set_alarm = mcp794xx_set_alarm,
172 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
173};
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700174
Heiner Kallweit7624df42017-07-12 07:49:33 +0200175static const struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700176 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700177 .nvram_offset = 8,
178 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700179 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200180 [ds_1308] = {
181 .nvram_offset = 8,
182 .nvram_size = 56,
183 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700184 [ds_1337] = {
185 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200186 .century_reg = DS1307_REG_MONTH,
187 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700188 },
189 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700190 .nvram_offset = 8,
191 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700192 },
193 [ds_1339] = {
194 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200195 .century_reg = DS1307_REG_MONTH,
196 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200197 .bbsqi_bit = DS1339_BIT_BBSQI,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700198 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700199 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700200 },
201 [ds_1340] = {
Heiner Kallweite48585d2017-06-05 17:57:33 +0200202 .century_reg = DS1307_REG_HOUR,
203 .century_enable_bit = DS1340_BIT_CENTURY_EN,
204 .century_bit = DS1340_BIT_CENTURY,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700205 .trickle_charger_reg = 0x08,
206 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300207 [ds_1341] = {
208 .century_reg = DS1307_REG_MONTH,
209 .century_bit = DS1337_BIT_CENTURY,
210 },
Wolfram Sangeb86c302012-05-29 15:07:38 -0700211 [ds_1388] = {
Heiner Kallweite5531702017-07-12 07:49:47 +0200212 .offset = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700213 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700214 },
215 [ds_3231] = {
216 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200217 .century_reg = DS1307_REG_MONTH,
218 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200219 .bbsqi_bit = DS3231_BIT_BBSQW,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700220 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200221 [rx_8130] = {
222 .alarm = 1,
223 /* this is battery backed SRAM */
224 .nvram_offset = 0x20,
225 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
Heiner Kallweite5531702017-07-12 07:49:47 +0200226 .offset = 0x10,
Heiner Kallweit45947122017-07-12 07:49:41 +0200227 .irq_handler = rx8130_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200228 .rtc_ops = &rx8130_rtc_ops,
Marek Vasutee0981b2017-06-18 22:55:28 +0200229 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800230 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700231 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700232 /* this is battery backed SRAM */
233 .nvram_offset = 0x20,
234 .nvram_size = 0x40,
Heiner Kallweit45947122017-07-12 07:49:41 +0200235 .irq_handler = mcp794xx_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200236 .rtc_ops = &mcp794xx_rtc_ops,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700237 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700238};
David Brownell045e0e82007-07-17 04:04:55 -0700239
Jean Delvare3760f732008-04-29 23:11:40 +0200240static const struct i2c_device_id ds1307_id[] = {
241 { "ds1307", ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200242 { "ds1308", ds_1308 },
Jean Delvare3760f732008-04-29 23:11:40 +0200243 { "ds1337", ds_1337 },
244 { "ds1338", ds_1338 },
245 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700246 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200247 { "ds1340", ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300248 { "ds1341", ds_1341 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700249 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700250 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200251 { "m41t00", m41t00 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800252 { "mcp7940x", mcp794xx },
253 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700254 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700255 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200256 { "isl12057", ds_1337 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200257 { "rx8130", rx_8130 },
Jean Delvare3760f732008-04-29 23:11:40 +0200258 { }
259};
260MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700261
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300262#ifdef CONFIG_OF
263static const struct of_device_id ds1307_of_match[] = {
264 {
265 .compatible = "dallas,ds1307",
266 .data = (void *)ds_1307
267 },
268 {
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200269 .compatible = "dallas,ds1308",
270 .data = (void *)ds_1308
271 },
272 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300273 .compatible = "dallas,ds1337",
274 .data = (void *)ds_1337
275 },
276 {
277 .compatible = "dallas,ds1338",
278 .data = (void *)ds_1338
279 },
280 {
281 .compatible = "dallas,ds1339",
282 .data = (void *)ds_1339
283 },
284 {
285 .compatible = "dallas,ds1388",
286 .data = (void *)ds_1388
287 },
288 {
289 .compatible = "dallas,ds1340",
290 .data = (void *)ds_1340
291 },
292 {
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300293 .compatible = "dallas,ds1341",
294 .data = (void *)ds_1341
295 },
296 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300297 .compatible = "maxim,ds3231",
298 .data = (void *)ds_3231
299 },
300 {
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200301 .compatible = "st,m41t0",
302 .data = (void *)m41t00
303 },
304 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300305 .compatible = "st,m41t00",
306 .data = (void *)m41t00
307 },
308 {
309 .compatible = "microchip,mcp7940x",
310 .data = (void *)mcp794xx
311 },
312 {
313 .compatible = "microchip,mcp7941x",
314 .data = (void *)mcp794xx
315 },
316 {
317 .compatible = "pericom,pt7c4338",
318 .data = (void *)ds_1307
319 },
320 {
321 .compatible = "epson,rx8025",
322 .data = (void *)rx_8025
323 },
324 {
325 .compatible = "isil,isl12057",
326 .data = (void *)ds_1337
327 },
328 { }
329};
330MODULE_DEVICE_TABLE(of, ds1307_of_match);
331#endif
332
Tin Huynh9c19b892016-11-30 09:57:31 +0700333#ifdef CONFIG_ACPI
334static const struct acpi_device_id ds1307_acpi_ids[] = {
335 { .id = "DS1307", .driver_data = ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200336 { .id = "DS1308", .driver_data = ds_1308 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700337 { .id = "DS1337", .driver_data = ds_1337 },
338 { .id = "DS1338", .driver_data = ds_1338 },
339 { .id = "DS1339", .driver_data = ds_1339 },
340 { .id = "DS1388", .driver_data = ds_1388 },
341 { .id = "DS1340", .driver_data = ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300342 { .id = "DS1341", .driver_data = ds_1341 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700343 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700344 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700345 { .id = "M41T00", .driver_data = m41t00 },
346 { .id = "MCP7940X", .driver_data = mcp794xx },
347 { .id = "MCP7941X", .driver_data = mcp794xx },
348 { .id = "PT7C4338", .driver_data = ds_1307 },
349 { .id = "RX8025", .driver_data = rx_8025 },
350 { .id = "ISL12057", .driver_data = ds_1337 },
351 { }
352};
353MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
354#endif
355
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700356/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700357 * The ds1337 and ds1339 both have two alarms, but we only use the first
358 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
359 * signal; ds1339 chips have only one alarm signal.
360 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500361static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700362{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100363 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500364 struct mutex *lock = &ds1307->rtc->ops_lock;
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200365 int stat, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700366
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700367 mutex_lock(lock);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100368 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
369 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700370 goto out;
371
372 if (stat & DS1337_BIT_A1I) {
373 stat &= ~DS1337_BIT_A1I;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100374 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700375
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200376 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
377 DS1337_BIT_A1IE, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100378 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700379 goto out;
380
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700381 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700382 }
383
384out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700385 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700386
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700387 return IRQ_HANDLED;
388}
389
390/*----------------------------------------------------------------------*/
391
David Brownell1abb0dc2006-06-25 05:48:17 -0700392static int ds1307_get_time(struct device *dev, struct rtc_time *t)
393{
394 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100395 int tmp, ret;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200396 const struct chip_desc *chip = &chips[ds1307->type];
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200397 u8 regs[7];
David Brownell1abb0dc2006-06-25 05:48:17 -0700398
David Brownell045e0e82007-07-17 04:04:55 -0700399 /* read the RTC date and time registers all at once */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200400 ret = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
401 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100402 if (ret) {
403 dev_err(dev, "%s error %d\n", "read", ret);
404 return ret;
David Brownell1abb0dc2006-06-25 05:48:17 -0700405 }
406
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200407 dev_dbg(dev, "%s: %7ph\n", "read", regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700408
Stefan Agner8566f702017-03-23 16:54:57 -0700409 /* if oscillator fail bit is set, no data can be trusted */
410 if (ds1307->type == m41t0 &&
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200411 regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
Stefan Agner8566f702017-03-23 16:54:57 -0700412 dev_warn_once(dev, "oscillator failed, set time!\n");
413 return -EINVAL;
414 }
415
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200416 t->tm_sec = bcd2bin(regs[DS1307_REG_SECS] & 0x7f);
417 t->tm_min = bcd2bin(regs[DS1307_REG_MIN] & 0x7f);
418 tmp = regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700419 t->tm_hour = bcd2bin(tmp);
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200420 t->tm_wday = bcd2bin(regs[DS1307_REG_WDAY] & 0x07) - 1;
421 t->tm_mday = bcd2bin(regs[DS1307_REG_MDAY] & 0x3f);
422 tmp = regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700423 t->tm_mon = bcd2bin(tmp) - 1;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200424 t->tm_year = bcd2bin(regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700425
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200426 if (regs[chip->century_reg] & chip->century_bit &&
Heiner Kallweite48585d2017-06-05 17:57:33 +0200427 IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY))
428 t->tm_year += 100;
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200429
David Brownell1abb0dc2006-06-25 05:48:17 -0700430 dev_dbg(dev, "%s secs=%d, mins=%d, "
431 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
432 "read", t->tm_sec, t->tm_min,
433 t->tm_hour, t->tm_mday,
434 t->tm_mon, t->tm_year, t->tm_wday);
435
David Brownell045e0e82007-07-17 04:04:55 -0700436 /* initial clock setting can be undefined */
437 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700438}
439
440static int ds1307_set_time(struct device *dev, struct rtc_time *t)
441{
442 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweite48585d2017-06-05 17:57:33 +0200443 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell1abb0dc2006-06-25 05:48:17 -0700444 int result;
445 int tmp;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200446 u8 regs[7];
David Brownell1abb0dc2006-06-25 05:48:17 -0700447
448 dev_dbg(dev, "%s secs=%d, mins=%d, "
449 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400450 "write", t->tm_sec, t->tm_min,
451 t->tm_hour, t->tm_mday,
452 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700453
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200454 if (t->tm_year < 100)
455 return -EINVAL;
456
Heiner Kallweite48585d2017-06-05 17:57:33 +0200457#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
458 if (t->tm_year > (chip->century_bit ? 299 : 199))
459 return -EINVAL;
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200460#else
Heiner Kallweite48585d2017-06-05 17:57:33 +0200461 if (t->tm_year > 199)
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200462 return -EINVAL;
463#endif
464
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200465 regs[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
466 regs[DS1307_REG_MIN] = bin2bcd(t->tm_min);
467 regs[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
468 regs[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
469 regs[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
470 regs[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700471
472 /* assume 20YY not 19YY */
473 tmp = t->tm_year - 100;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200474 regs[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700475
Heiner Kallweite48585d2017-06-05 17:57:33 +0200476 if (chip->century_enable_bit)
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200477 regs[chip->century_reg] |= chip->century_enable_bit;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200478 if (t->tm_year > 199 && chip->century_bit)
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200479 regs[chip->century_reg] |= chip->century_bit;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200480
481 if (ds1307->type == mcp794xx) {
David Anders40ce9722012-03-23 15:02:37 -0700482 /*
483 * these bits were cleared when preparing the date/time
484 * values and need to be set again before writing the
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200485 * regsfer out to the device.
David Anders40ce9722012-03-23 15:02:37 -0700486 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200487 regs[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
488 regs[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700489 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700490
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200491 dev_dbg(dev, "%s: %7ph\n", "write", regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700492
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200493 result = regmap_bulk_write(ds1307->regmap, chip->offset, regs,
494 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100495 if (result) {
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800496 dev_err(dev, "%s error %d\n", "write", result);
497 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700498 }
499 return 0;
500}
501
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800502static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700503{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100504 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700505 int ret;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200506 u8 regs[9];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700507
508 if (!test_bit(HAS_ALARM, &ds1307->flags))
509 return -EINVAL;
510
511 /* read all ALARM1, ALARM2, and status registers at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100512 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200513 regs, sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100514 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700515 dev_err(dev, "%s error %d\n", "alarm read", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100516 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700517 }
518
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100519 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200520 &regs[0], &regs[4], &regs[7]);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700521
David Anders40ce9722012-03-23 15:02:37 -0700522 /*
523 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700524 * and that all four fields are checked matches
525 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200526 t->time.tm_sec = bcd2bin(regs[0] & 0x7f);
527 t->time.tm_min = bcd2bin(regs[1] & 0x7f);
528 t->time.tm_hour = bcd2bin(regs[2] & 0x3f);
529 t->time.tm_mday = bcd2bin(regs[3] & 0x3f);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700530
531 /* ... and status */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200532 t->enabled = !!(regs[7] & DS1337_BIT_A1IE);
533 t->pending = !!(regs[8] & DS1337_BIT_A1I);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700534
535 dev_dbg(dev, "%s secs=%d, mins=%d, "
536 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
537 "alarm read", t->time.tm_sec, t->time.tm_min,
538 t->time.tm_hour, t->time.tm_mday,
539 t->enabled, t->pending);
540
541 return 0;
542}
543
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800544static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700545{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100546 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200547 unsigned char regs[9];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700548 u8 control, status;
549 int ret;
550
551 if (!test_bit(HAS_ALARM, &ds1307->flags))
552 return -EINVAL;
553
554 dev_dbg(dev, "%s secs=%d, mins=%d, "
555 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
556 "alarm set", t->time.tm_sec, t->time.tm_min,
557 t->time.tm_hour, t->time.tm_mday,
558 t->enabled, t->pending);
559
560 /* read current status of both alarms and the chip */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200561 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
562 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100563 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700564 dev_err(dev, "%s error %d\n", "alarm write", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100565 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700566 }
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200567 control = regs[7];
568 status = regs[8];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700569
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100570 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200571 &regs[0], &regs[4], control, status);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700572
573 /* set ALARM1, using 24 hour and day-of-month modes */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200574 regs[0] = bin2bcd(t->time.tm_sec);
575 regs[1] = bin2bcd(t->time.tm_min);
576 regs[2] = bin2bcd(t->time.tm_hour);
577 regs[3] = bin2bcd(t->time.tm_mday);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700578
579 /* set ALARM2 to non-garbage */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200580 regs[4] = 0;
581 regs[5] = 0;
582 regs[6] = 0;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700583
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200584 /* disable alarms */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200585 regs[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
586 regs[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700587
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200588 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
589 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100590 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700591 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800592 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700593 }
594
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200595 /* optionally enable ALARM1 */
596 if (t->enabled) {
597 dev_dbg(dev, "alarm IRQ armed\n");
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200598 regs[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
599 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, regs[7]);
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200600 }
601
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700602 return 0;
603}
604
John Stultz16380c12011-02-02 17:02:41 -0800605static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700606{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100607 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700608
John Stultz16380c12011-02-02 17:02:41 -0800609 if (!test_bit(HAS_ALARM, &ds1307->flags))
610 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700611
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200612 return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
613 DS1337_BIT_A1IE,
614 enabled ? DS1337_BIT_A1IE : 0);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700615}
616
David Brownellff8371a2006-09-30 23:28:17 -0700617static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700618 .read_time = ds1307_get_time,
619 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800620 .read_alarm = ds1337_read_alarm,
621 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800622 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700623};
624
David Brownell682d73f2007-11-14 16:58:32 -0800625/*----------------------------------------------------------------------*/
626
Simon Guinot1d1945d2014-04-03 14:49:55 -0700627/*
Marek Vasutee0981b2017-06-18 22:55:28 +0200628 * Alarm support for rx8130 devices.
629 */
630
631#define RX8130_REG_ALARM_MIN 0x07
632#define RX8130_REG_ALARM_HOUR 0x08
633#define RX8130_REG_ALARM_WEEK_OR_DAY 0x09
634#define RX8130_REG_EXTENSION 0x0c
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200635#define RX8130_REG_EXTENSION_WADA BIT(3)
Marek Vasutee0981b2017-06-18 22:55:28 +0200636#define RX8130_REG_FLAG 0x0d
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200637#define RX8130_REG_FLAG_AF BIT(3)
Marek Vasutee0981b2017-06-18 22:55:28 +0200638#define RX8130_REG_CONTROL0 0x0e
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200639#define RX8130_REG_CONTROL0_AIE BIT(3)
Marek Vasutee0981b2017-06-18 22:55:28 +0200640
641static irqreturn_t rx8130_irq(int irq, void *dev_id)
642{
643 struct ds1307 *ds1307 = dev_id;
644 struct mutex *lock = &ds1307->rtc->ops_lock;
645 u8 ctl[3];
646 int ret;
647
648 mutex_lock(lock);
649
650 /* Read control registers. */
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200651 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
652 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200653 if (ret < 0)
654 goto out;
655 if (!(ctl[1] & RX8130_REG_FLAG_AF))
656 goto out;
657 ctl[1] &= ~RX8130_REG_FLAG_AF;
658 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
659
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200660 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
661 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200662 if (ret < 0)
663 goto out;
664
665 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
666
667out:
668 mutex_unlock(lock);
669
670 return IRQ_HANDLED;
671}
672
673static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
674{
675 struct ds1307 *ds1307 = dev_get_drvdata(dev);
676 u8 ald[3], ctl[3];
677 int ret;
678
679 if (!test_bit(HAS_ALARM, &ds1307->flags))
680 return -EINVAL;
681
682 /* Read alarm registers. */
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200683 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
684 sizeof(ald));
Marek Vasutee0981b2017-06-18 22:55:28 +0200685 if (ret < 0)
686 return ret;
687
688 /* Read control registers. */
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200689 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
690 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200691 if (ret < 0)
692 return ret;
693
694 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
695 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
696
697 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
698 t->time.tm_sec = -1;
699 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
700 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
701 t->time.tm_wday = -1;
702 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
703 t->time.tm_mon = -1;
704 t->time.tm_year = -1;
705 t->time.tm_yday = -1;
706 t->time.tm_isdst = -1;
707
708 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
709 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
710 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
711
712 return 0;
713}
714
715static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
716{
717 struct ds1307 *ds1307 = dev_get_drvdata(dev);
718 u8 ald[3], ctl[3];
719 int ret;
720
721 if (!test_bit(HAS_ALARM, &ds1307->flags))
722 return -EINVAL;
723
724 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
725 "enabled=%d pending=%d\n", __func__,
726 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
727 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
728 t->enabled, t->pending);
729
730 /* Read control registers. */
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200731 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
732 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200733 if (ret < 0)
734 return ret;
735
736 ctl[0] &= ~RX8130_REG_EXTENSION_WADA;
737 ctl[1] |= RX8130_REG_FLAG_AF;
738 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
739
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200740 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
741 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200742 if (ret < 0)
743 return ret;
744
745 /* Hardware alarm precision is 1 minute! */
746 ald[0] = bin2bcd(t->time.tm_min);
747 ald[1] = bin2bcd(t->time.tm_hour);
748 ald[2] = bin2bcd(t->time.tm_mday);
749
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200750 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
751 sizeof(ald));
Marek Vasutee0981b2017-06-18 22:55:28 +0200752 if (ret < 0)
753 return ret;
754
755 if (!t->enabled)
756 return 0;
757
758 ctl[2] |= RX8130_REG_CONTROL0_AIE;
759
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200760 return regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
761 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200762}
763
764static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
765{
766 struct ds1307 *ds1307 = dev_get_drvdata(dev);
767 int ret, reg;
768
769 if (!test_bit(HAS_ALARM, &ds1307->flags))
770 return -EINVAL;
771
772 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
773 if (ret < 0)
774 return ret;
775
776 if (enabled)
777 reg |= RX8130_REG_CONTROL0_AIE;
778 else
779 reg &= ~RX8130_REG_CONTROL0_AIE;
780
781 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
782}
783
Marek Vasutee0981b2017-06-18 22:55:28 +0200784/*----------------------------------------------------------------------*/
785
786/*
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800787 * Alarm support for mcp794xx devices.
Simon Guinot1d1945d2014-04-03 14:49:55 -0700788 */
789
Keerthye29385f2016-06-01 16:19:07 +0530790#define MCP794XX_REG_WEEKDAY 0x3
791#define MCP794XX_REG_WEEKDAY_WDAY_MASK 0x7
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800792#define MCP794XX_REG_CONTROL 0x07
793# define MCP794XX_BIT_ALM0_EN 0x10
794# define MCP794XX_BIT_ALM1_EN 0x20
795#define MCP794XX_REG_ALARM0_BASE 0x0a
796#define MCP794XX_REG_ALARM0_CTRL 0x0d
797#define MCP794XX_REG_ALARM1_BASE 0x11
798#define MCP794XX_REG_ALARM1_CTRL 0x14
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200799# define MCP794XX_BIT_ALMX_IF BIT(3)
800# define MCP794XX_BIT_ALMX_C0 BIT(4)
801# define MCP794XX_BIT_ALMX_C1 BIT(5)
802# define MCP794XX_BIT_ALMX_C2 BIT(6)
803# define MCP794XX_BIT_ALMX_POL BIT(7)
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800804# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
805 MCP794XX_BIT_ALMX_C1 | \
806 MCP794XX_BIT_ALMX_C2)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700807
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500808static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700809{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100810 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500811 struct mutex *lock = &ds1307->rtc->ops_lock;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700812 int reg, ret;
813
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500814 mutex_lock(lock);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700815
816 /* Check and clear alarm 0 interrupt flag. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100817 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
818 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700819 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800820 if (!(reg & MCP794XX_BIT_ALMX_IF))
Simon Guinot1d1945d2014-04-03 14:49:55 -0700821 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800822 reg &= ~MCP794XX_BIT_ALMX_IF;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100823 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
824 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700825 goto out;
826
827 /* Disable alarm 0. */
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200828 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
829 MCP794XX_BIT_ALM0_EN, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100830 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700831 goto out;
832
833 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
834
835out:
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500836 mutex_unlock(lock);
837
838 return IRQ_HANDLED;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700839}
840
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800841static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700842{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100843 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200844 u8 regs[10];
Simon Guinot1d1945d2014-04-03 14:49:55 -0700845 int ret;
846
847 if (!test_bit(HAS_ALARM, &ds1307->flags))
848 return -EINVAL;
849
850 /* Read control and alarm 0 registers. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200851 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
852 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100853 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700854 return ret;
855
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800856 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700857
858 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200859 t->time.tm_sec = bcd2bin(regs[3] & 0x7f);
860 t->time.tm_min = bcd2bin(regs[4] & 0x7f);
861 t->time.tm_hour = bcd2bin(regs[5] & 0x3f);
862 t->time.tm_wday = bcd2bin(regs[6] & 0x7) - 1;
863 t->time.tm_mday = bcd2bin(regs[7] & 0x3f);
864 t->time.tm_mon = bcd2bin(regs[8] & 0x1f) - 1;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700865 t->time.tm_year = -1;
866 t->time.tm_yday = -1;
867 t->time.tm_isdst = -1;
868
869 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200870 "enabled=%d polarity=%d irq=%d match=%lu\n", __func__,
Simon Guinot1d1945d2014-04-03 14:49:55 -0700871 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
872 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200873 !!(regs[6] & MCP794XX_BIT_ALMX_POL),
874 !!(regs[6] & MCP794XX_BIT_ALMX_IF),
875 (regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700876
877 return 0;
878}
879
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800880static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700881{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100882 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200883 unsigned char regs[10];
Simon Guinot1d1945d2014-04-03 14:49:55 -0700884 int ret;
885
886 if (!test_bit(HAS_ALARM, &ds1307->flags))
887 return -EINVAL;
888
889 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
890 "enabled=%d pending=%d\n", __func__,
891 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
892 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
893 t->enabled, t->pending);
894
895 /* Read control and alarm 0 registers. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200896 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
897 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100898 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700899 return ret;
900
901 /* Set alarm 0, using 24-hour and day-of-month modes. */
902 regs[3] = bin2bcd(t->time.tm_sec);
903 regs[4] = bin2bcd(t->time.tm_min);
904 regs[5] = bin2bcd(t->time.tm_hour);
Tero Kristo62c8c202015-10-23 09:29:57 +0300905 regs[6] = bin2bcd(t->time.tm_wday + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700906 regs[7] = bin2bcd(t->time.tm_mday);
Tero Kristo62c8c202015-10-23 09:29:57 +0300907 regs[8] = bin2bcd(t->time.tm_mon + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700908
909 /* Clear the alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800910 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700911 /* Set alarm match: second, minute, hour, day, date, month. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800912 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
Nishanth Menone3edd672015-04-20 19:51:34 -0500913 /* Disable interrupt. We will not enable until completely programmed */
914 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700915
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200916 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
917 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100918 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700919 return ret;
920
Nishanth Menone3edd672015-04-20 19:51:34 -0500921 if (!t->enabled)
922 return 0;
923 regs[0] |= MCP794XX_BIT_ALM0_EN;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100924 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700925}
926
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800927static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700928{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100929 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700930
931 if (!test_bit(HAS_ALARM, &ds1307->flags))
932 return -EINVAL;
933
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200934 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
935 MCP794XX_BIT_ALM0_EN,
936 enabled ? MCP794XX_BIT_ALM0_EN : 0);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700937}
938
Simon Guinot1d1945d2014-04-03 14:49:55 -0700939/*----------------------------------------------------------------------*/
940
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200941static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
942 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -0800943{
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200944 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +0200945 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -0800946
Heiner Kallweit969fa072017-07-12 07:49:54 +0200947 return regmap_bulk_read(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200948 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -0800949}
950
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200951static int ds1307_nvram_write(void *priv, unsigned int offset, void *val,
952 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -0800953{
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200954 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +0200955 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -0800956
Heiner Kallweit969fa072017-07-12 07:49:54 +0200957 return regmap_bulk_write(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200958 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -0800959}
960
David Brownell682d73f2007-11-14 16:58:32 -0800961/*----------------------------------------------------------------------*/
962
Heiner Kallweit11e58902017-03-10 18:52:34 +0100963static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307,
Alexandre Belloni57ec2d92017-09-04 22:46:04 +0200964 u32 ohms, bool diode)
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700965{
966 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
967 DS1307_TRICKLE_CHARGER_NO_DIODE;
968
969 switch (ohms) {
970 case 250:
971 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
972 break;
973 case 2000:
974 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
975 break;
976 case 4000:
977 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
978 break;
979 default:
Heiner Kallweit11e58902017-03-10 18:52:34 +0100980 dev_warn(ds1307->dev,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700981 "Unsupported ohm value %u in dt\n", ohms);
982 return 0;
983 }
984 return setup;
985}
986
Heiner Kallweitd8490fd2017-07-12 07:49:28 +0200987static u8 ds1307_trickle_init(struct ds1307 *ds1307,
Heiner Kallweit7624df42017-07-12 07:49:33 +0200988 const struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700989{
Alexandre Belloni57ec2d92017-09-04 22:46:04 +0200990 u32 ohms;
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700991 bool diode = true;
992
993 if (!chip->do_trickle_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +0200994 return 0;
995
Heiner Kallweit11e58902017-03-10 18:52:34 +0100996 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
997 &ohms))
Heiner Kallweitd8490fd2017-07-12 07:49:28 +0200998 return 0;
999
Heiner Kallweit11e58902017-03-10 18:52:34 +01001000 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001001 diode = false;
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001002
1003 return chip->do_trickle_setup(ds1307, ohms, diode);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001004}
1005
Akinobu Mita445c0202016-01-25 00:22:16 +09001006/*----------------------------------------------------------------------*/
1007
1008#ifdef CONFIG_RTC_DRV_DS1307_HWMON
1009
1010/*
1011 * Temperature sensor support for ds3231 devices.
1012 */
1013
1014#define DS3231_REG_TEMPERATURE 0x11
1015
1016/*
1017 * A user-initiated temperature conversion is not started by this function,
1018 * so the temperature is updated once every 64 seconds.
1019 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001020static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +09001021{
1022 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1023 u8 temp_buf[2];
1024 s16 temp;
1025 int ret;
1026
Heiner Kallweit11e58902017-03-10 18:52:34 +01001027 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
1028 temp_buf, sizeof(temp_buf));
1029 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +09001030 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +09001031 /*
1032 * Temperature is represented as a 10-bit code with a resolution of
1033 * 0.25 degree celsius and encoded in two's complement format.
1034 */
1035 temp = (temp_buf[0] << 8) | temp_buf[1];
1036 temp >>= 6;
1037 *mC = temp * 250;
1038
1039 return 0;
1040}
1041
1042static ssize_t ds3231_hwmon_show_temp(struct device *dev,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001043 struct device_attribute *attr, char *buf)
Akinobu Mita445c0202016-01-25 00:22:16 +09001044{
1045 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001046 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001047
1048 ret = ds3231_hwmon_read_temp(dev, &temp);
1049 if (ret)
1050 return ret;
1051
1052 return sprintf(buf, "%d\n", temp);
1053}
Alexandre Bellonib4be2712017-09-04 22:46:08 +02001054static SENSOR_DEVICE_ATTR(temp1_input, 0444, ds3231_hwmon_show_temp,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001055 NULL, 0);
Akinobu Mita445c0202016-01-25 00:22:16 +09001056
1057static struct attribute *ds3231_hwmon_attrs[] = {
1058 &sensor_dev_attr_temp1_input.dev_attr.attr,
1059 NULL,
1060};
1061ATTRIBUTE_GROUPS(ds3231_hwmon);
1062
1063static void ds1307_hwmon_register(struct ds1307 *ds1307)
1064{
1065 struct device *dev;
1066
1067 if (ds1307->type != ds_3231)
1068 return;
1069
Heiner Kallweit11e58902017-03-10 18:52:34 +01001070 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001071 ds1307,
1072 ds3231_hwmon_groups);
Akinobu Mita445c0202016-01-25 00:22:16 +09001073 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001074 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1075 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001076 }
1077}
1078
1079#else
1080
1081static void ds1307_hwmon_register(struct ds1307 *ds1307)
1082{
1083}
1084
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001085#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1086
1087/*----------------------------------------------------------------------*/
1088
1089/*
1090 * Square-wave output support for DS3231
1091 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1092 */
1093#ifdef CONFIG_COMMON_CLK
1094
1095enum {
1096 DS3231_CLK_SQW = 0,
1097 DS3231_CLK_32KHZ,
1098};
1099
1100#define clk_sqw_to_ds1307(clk) \
1101 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1102#define clk_32khz_to_ds1307(clk) \
1103 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1104
1105static int ds3231_clk_sqw_rates[] = {
1106 1,
1107 1024,
1108 4096,
1109 8192,
1110};
1111
1112static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1113{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001114 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001115 int ret;
1116
1117 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001118 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1119 mask, value);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001120 mutex_unlock(lock);
1121
1122 return ret;
1123}
1124
1125static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1126 unsigned long parent_rate)
1127{
1128 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001129 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001130 int rate_sel = 0;
1131
Heiner Kallweit11e58902017-03-10 18:52:34 +01001132 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1133 if (ret)
1134 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001135 if (control & DS1337_BIT_RS1)
1136 rate_sel += 1;
1137 if (control & DS1337_BIT_RS2)
1138 rate_sel += 2;
1139
1140 return ds3231_clk_sqw_rates[rate_sel];
1141}
1142
1143static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001144 unsigned long *prate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001145{
1146 int i;
1147
1148 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1149 if (ds3231_clk_sqw_rates[i] <= rate)
1150 return ds3231_clk_sqw_rates[i];
1151 }
1152
1153 return 0;
1154}
1155
1156static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001157 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001158{
1159 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1160 int control = 0;
1161 int rate_sel;
1162
1163 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1164 rate_sel++) {
1165 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1166 break;
1167 }
1168
1169 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1170 return -EINVAL;
1171
1172 if (rate_sel & 1)
1173 control |= DS1337_BIT_RS1;
1174 if (rate_sel & 2)
1175 control |= DS1337_BIT_RS2;
1176
1177 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1178 control);
1179}
1180
1181static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1182{
1183 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1184
1185 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1186}
1187
1188static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1189{
1190 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1191
1192 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1193}
1194
1195static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1196{
1197 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001198 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001199
Heiner Kallweit11e58902017-03-10 18:52:34 +01001200 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1201 if (ret)
1202 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001203
1204 return !(control & DS1337_BIT_INTCN);
1205}
1206
1207static const struct clk_ops ds3231_clk_sqw_ops = {
1208 .prepare = ds3231_clk_sqw_prepare,
1209 .unprepare = ds3231_clk_sqw_unprepare,
1210 .is_prepared = ds3231_clk_sqw_is_prepared,
1211 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1212 .round_rate = ds3231_clk_sqw_round_rate,
1213 .set_rate = ds3231_clk_sqw_set_rate,
1214};
1215
1216static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001217 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001218{
1219 return 32768;
1220}
1221
1222static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1223{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001224 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001225 int ret;
1226
1227 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001228 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1229 DS3231_BIT_EN32KHZ,
1230 enable ? DS3231_BIT_EN32KHZ : 0);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001231 mutex_unlock(lock);
1232
1233 return ret;
1234}
1235
1236static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1237{
1238 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1239
1240 return ds3231_clk_32khz_control(ds1307, true);
1241}
1242
1243static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1244{
1245 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1246
1247 ds3231_clk_32khz_control(ds1307, false);
1248}
1249
1250static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1251{
1252 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001253 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001254
Heiner Kallweit11e58902017-03-10 18:52:34 +01001255 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1256 if (ret)
1257 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001258
1259 return !!(status & DS3231_BIT_EN32KHZ);
1260}
1261
1262static const struct clk_ops ds3231_clk_32khz_ops = {
1263 .prepare = ds3231_clk_32khz_prepare,
1264 .unprepare = ds3231_clk_32khz_unprepare,
1265 .is_prepared = ds3231_clk_32khz_is_prepared,
1266 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1267};
1268
1269static struct clk_init_data ds3231_clks_init[] = {
1270 [DS3231_CLK_SQW] = {
1271 .name = "ds3231_clk_sqw",
1272 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001273 },
1274 [DS3231_CLK_32KHZ] = {
1275 .name = "ds3231_clk_32khz",
1276 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001277 },
1278};
1279
1280static int ds3231_clks_register(struct ds1307 *ds1307)
1281{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001282 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001283 struct clk_onecell_data *onecell;
1284 int i;
1285
Heiner Kallweit11e58902017-03-10 18:52:34 +01001286 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001287 if (!onecell)
1288 return -ENOMEM;
1289
1290 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001291 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1292 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001293 if (!onecell->clks)
1294 return -ENOMEM;
1295
1296 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1297 struct clk_init_data init = ds3231_clks_init[i];
1298
1299 /*
1300 * Interrupt signal due to alarm conditions and square-wave
1301 * output share same pin, so don't initialize both.
1302 */
1303 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1304 continue;
1305
1306 /* optional override of the clockname */
1307 of_property_read_string_index(node, "clock-output-names", i,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001308 &init.name);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001309 ds1307->clks[i].init = &init;
1310
Heiner Kallweit11e58902017-03-10 18:52:34 +01001311 onecell->clks[i] = devm_clk_register(ds1307->dev,
1312 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001313 if (IS_ERR(onecell->clks[i]))
1314 return PTR_ERR(onecell->clks[i]);
1315 }
1316
1317 if (!node)
1318 return 0;
1319
1320 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1321
1322 return 0;
1323}
1324
1325static void ds1307_clks_register(struct ds1307 *ds1307)
1326{
1327 int ret;
1328
1329 if (ds1307->type != ds_3231)
1330 return;
1331
1332 ret = ds3231_clks_register(ds1307);
1333 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001334 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1335 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001336 }
1337}
1338
1339#else
1340
1341static void ds1307_clks_register(struct ds1307 *ds1307)
1342{
1343}
1344
1345#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001346
Heiner Kallweit11e58902017-03-10 18:52:34 +01001347static const struct regmap_config regmap_config = {
1348 .reg_bits = 8,
1349 .val_bits = 8,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001350};
1351
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001352static int ds1307_probe(struct i2c_client *client,
1353 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001354{
1355 struct ds1307 *ds1307;
1356 int err = -ENODEV;
Keerthye29385f2016-06-01 16:19:07 +05301357 int tmp, wday;
Heiner Kallweit7624df42017-07-12 07:49:33 +02001358 const struct chip_desc *chip;
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001359 bool want_irq;
Michael Lange8bc2a402016-01-21 18:10:16 +01001360 bool ds1307_can_wakeup_device = false;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001361 unsigned char regs[8];
Jingoo Han01ce8932013-11-12 15:10:41 -08001362 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Keerthye29385f2016-06-01 16:19:07 +05301363 struct rtc_time tm;
1364 unsigned long timestamp;
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001365 u8 trickle_charger_setup = 0;
David Brownell1abb0dc2006-06-25 05:48:17 -07001366
Jingoo Hanedca66d2013-07-03 15:07:05 -07001367 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001368 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001369 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001370
Heiner Kallweit11e58902017-03-10 18:52:34 +01001371 dev_set_drvdata(&client->dev, ds1307);
1372 ds1307->dev = &client->dev;
1373 ds1307->name = client->name;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001374
Heiner Kallweit11e58902017-03-10 18:52:34 +01001375 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1376 if (IS_ERR(ds1307->regmap)) {
1377 dev_err(ds1307->dev, "regmap allocation failed\n");
1378 return PTR_ERR(ds1307->regmap);
1379 }
1380
1381 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001382
1383 if (client->dev.of_node) {
1384 ds1307->type = (enum ds_type)
1385 of_device_get_match_data(&client->dev);
1386 chip = &chips[ds1307->type];
1387 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001388 chip = &chips[id->driver_data];
1389 ds1307->type = id->driver_data;
1390 } else {
1391 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001392
Tin Huynh9c19b892016-11-30 09:57:31 +07001393 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001394 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001395 if (!acpi_id)
1396 return -ENODEV;
1397 chip = &chips[acpi_id->driver_data];
1398 ds1307->type = acpi_id->driver_data;
1399 }
1400
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001401 want_irq = client->irq > 0 && chip->alarm;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001402
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001403 if (!pdata)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001404 trickle_charger_setup = ds1307_trickle_init(ds1307, chip);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001405 else if (pdata->trickle_charger_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001406 trickle_charger_setup = pdata->trickle_charger_setup;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001407
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001408 if (trickle_charger_setup && chip->trickle_charger_reg) {
1409 trickle_charger_setup |= DS13XX_TRICKLE_CHARGER_MAGIC;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001410 dev_dbg(ds1307->dev,
1411 "writing trickle charger info 0x%x to 0x%x\n",
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001412 trickle_charger_setup, chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001413 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001414 trickle_charger_setup);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001415 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001416
Michael Lange8bc2a402016-01-21 18:10:16 +01001417#ifdef CONFIG_OF
1418/*
1419 * For devices with no IRQ directly connected to the SoC, the RTC chip
1420 * can be forced as a wakeup source by stating that explicitly in
1421 * the device's .dts file using the "wakeup-source" boolean property.
1422 * If the "wakeup-source" property is set, don't request an IRQ.
1423 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1424 * if supported by the RTC.
1425 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001426 if (chip->alarm && of_property_read_bool(client->dev.of_node,
1427 "wakeup-source"))
Michael Lange8bc2a402016-01-21 18:10:16 +01001428 ds1307_can_wakeup_device = true;
Michael Lange8bc2a402016-01-21 18:10:16 +01001429#endif
1430
David Brownell045e0e82007-07-17 04:04:55 -07001431 switch (ds1307->type) {
1432 case ds_1337:
1433 case ds_1339:
Nikita Yushchenko0759c882017-08-24 09:32:11 +03001434 case ds_1341:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001435 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001436 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001437 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001438 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001439 if (err) {
1440 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001441 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001442 }
1443
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001444 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001445 if (regs[0] & DS1337_BIT_nEOSC)
1446 regs[0] &= ~DS1337_BIT_nEOSC;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001447
David Anders40ce9722012-03-23 15:02:37 -07001448 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001449 * Using IRQ or defined as wakeup-source?
1450 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001451 * For some variants, be sure alarms can trigger when we're
1452 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001453 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001454 if (want_irq || ds1307_can_wakeup_device) {
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001455 regs[0] |= DS1337_BIT_INTCN | chip->bbsqi_bit;
1456 regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001457 }
1458
Heiner Kallweit11e58902017-03-10 18:52:34 +01001459 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001460 regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001461
1462 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001463 if (regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001464 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001465 regs[1] & ~DS1337_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001466 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001467 }
David Brownell045e0e82007-07-17 04:04:55 -07001468 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001469
1470 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001471 err = regmap_bulk_read(ds1307->regmap,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001472 RX8025_REG_CTRL1 << 4 | 0x08, regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001473 if (err) {
1474 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001475 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001476 }
1477
1478 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001479 if (!(regs[1] & RX8025_BIT_XST)) {
1480 regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001481 regmap_write(ds1307->regmap,
1482 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001483 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001484 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001485 "oscillator stop detected - SET TIME!\n");
1486 }
1487
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001488 if (regs[1] & RX8025_BIT_PON) {
1489 regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001490 regmap_write(ds1307->regmap,
1491 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001492 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001493 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001494 }
1495
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001496 if (regs[1] & RX8025_BIT_VDET) {
1497 regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001498 regmap_write(ds1307->regmap,
1499 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001500 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001501 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001502 }
1503
1504 /* make sure we are running in 24hour mode */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001505 if (!(regs[0] & RX8025_BIT_2412)) {
Matthias Fuchsa2166852009-03-31 15:24:58 -07001506 u8 hour;
1507
1508 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001509 regmap_write(ds1307->regmap,
1510 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001511 regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001512
Heiner Kallweit11e58902017-03-10 18:52:34 +01001513 err = regmap_bulk_read(ds1307->regmap,
1514 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001515 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001516 if (err) {
1517 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001518 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001519 }
1520
1521 /* correct hour */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001522 hour = bcd2bin(regs[DS1307_REG_HOUR]);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001523 if (hour == 12)
1524 hour = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001525 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
Matthias Fuchsa2166852009-03-31 15:24:58 -07001526 hour += 12;
1527
Heiner Kallweit11e58902017-03-10 18:52:34 +01001528 regmap_write(ds1307->regmap,
1529 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001530 }
1531 break;
David Brownell045e0e82007-07-17 04:04:55 -07001532 default:
1533 break;
1534 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001535
1536read_rtc:
1537 /* read RTC registers */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001538 err = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
1539 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +01001540 if (err) {
1541 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001542 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001543 }
1544
David Anders40ce9722012-03-23 15:02:37 -07001545 /*
1546 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001547 * specify the extra bits as must-be-zero, but there are
1548 * still a few values that are clearly out-of-range.
1549 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001550 tmp = regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001551 switch (ds1307->type) {
1552 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001553 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001554 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001555 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001556 if (tmp & DS1307_BIT_CH) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001557 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1558 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001559 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001560 }
David Brownell045e0e82007-07-17 04:04:55 -07001561 break;
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001562 case ds_1308:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001563 case ds_1338:
1564 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001565 if (tmp & DS1307_BIT_CH)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001566 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001567
1568 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001569 if (regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001570 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001571 regs[DS1307_REG_CONTROL] &
1572 ~DS1338_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001573 dev_warn(ds1307->dev, "SET TIME!\n");
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001574 goto read_rtc;
1575 }
David Brownell045e0e82007-07-17 04:04:55 -07001576 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001577 case ds_1340:
1578 /* clock halted? turn it on, so clock can tick. */
1579 if (tmp & DS1340_BIT_nEOSC)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001580 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
frederic Rodofcd8db02008-02-06 01:38:55 -08001581
Heiner Kallweit11e58902017-03-10 18:52:34 +01001582 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1583 if (err) {
1584 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001585 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001586 }
1587
1588 /* oscillator fault? clear flag, and warn */
1589 if (tmp & DS1340_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001590 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1591 dev_warn(ds1307->dev, "SET TIME!\n");
frederic Rodofcd8db02008-02-06 01:38:55 -08001592 }
1593 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001594 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001595 /* make sure that the backup battery is enabled */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001596 if (!(regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001597 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001598 regs[DS1307_REG_WDAY] |
Heiner Kallweit11e58902017-03-10 18:52:34 +01001599 MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001600 }
1601
1602 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001603 if (!(tmp & MCP794XX_BIT_ST)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001604 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1605 MCP794XX_BIT_ST);
1606 dev_warn(ds1307->dev, "SET TIME!\n");
David Anders43fcb812011-11-02 13:37:53 -07001607 goto read_rtc;
1608 }
1609
1610 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001611 default:
David Brownell045e0e82007-07-17 04:04:55 -07001612 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001613 }
David Brownell045e0e82007-07-17 04:04:55 -07001614
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001615 tmp = regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001616 switch (ds1307->type) {
1617 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001618 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001619 case m41t00:
David Anders40ce9722012-03-23 15:02:37 -07001620 /*
1621 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001622 * systems that will run through year 2100.
1623 */
1624 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001625 case rx_8025:
1626 break;
David Brownellc065f352007-07-17 04:05:10 -07001627 default:
1628 if (!(tmp & DS1307_BIT_12HR))
1629 break;
1630
David Anders40ce9722012-03-23 15:02:37 -07001631 /*
1632 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001633 * take note...
1634 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001635 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001636 if (tmp == 12)
1637 tmp = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001638 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
David Brownellc065f352007-07-17 04:05:10 -07001639 tmp += 12;
Heiner Kallweite5531702017-07-12 07:49:47 +02001640 regmap_write(ds1307->regmap, chip->offset + DS1307_REG_HOUR,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001641 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001642 }
1643
Keerthye29385f2016-06-01 16:19:07 +05301644 /*
1645 * Some IPs have weekday reset value = 0x1 which might not correct
1646 * hence compute the wday using the current date/month/year values
1647 */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001648 ds1307_get_time(ds1307->dev, &tm);
Keerthye29385f2016-06-01 16:19:07 +05301649 wday = tm.tm_wday;
1650 timestamp = rtc_tm_to_time64(&tm);
1651 rtc_time64_to_tm(timestamp, &tm);
1652
1653 /*
1654 * Check if reset wday is different from the computed wday
1655 * If different then set the wday which we computed using
1656 * timestamp
1657 */
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001658 if (wday != tm.tm_wday)
1659 regmap_update_bits(ds1307->regmap, MCP794XX_REG_WEEKDAY,
1660 MCP794XX_REG_WEEKDAY_WDAY_MASK,
1661 tm.tm_wday + 1);
Keerthye29385f2016-06-01 16:19:07 +05301662
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001663 if (want_irq || ds1307_can_wakeup_device) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001664 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001665 set_bit(HAS_ALARM, &ds1307->flags);
1666 }
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001667
1668 ds1307->rtc = devm_rtc_allocate_device(ds1307->dev);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001669 if (IS_ERR(ds1307->rtc))
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001670 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001671
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001672 if (ds1307_can_wakeup_device && !want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001673 dev_info(ds1307->dev,
1674 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001675 /* We cannot support UIE mode if we do not have an IRQ line */
1676 ds1307->rtc->uie_unsupported = 1;
1677 }
1678
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001679 if (want_irq) {
Heiner Kallweit45947122017-07-12 07:49:41 +02001680 err = devm_request_threaded_irq(ds1307->dev, client->irq, NULL,
1681 chip->irq_handler ?: ds1307_irq,
Nishanth Menonc5983192015-06-23 11:15:11 -05001682 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001683 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001684 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001685 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001686 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001687 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001688 dev_err(ds1307->dev, "unable to request IRQ!\n");
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001689 } else {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001690 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001691 }
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001692 }
1693
Austin Boyle9eab0a72012-03-23 15:02:38 -07001694 if (chip->nvram_size) {
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001695 ds1307->nvmem_cfg.name = "ds1307_nvram";
1696 ds1307->nvmem_cfg.word_size = 1;
1697 ds1307->nvmem_cfg.stride = 1;
1698 ds1307->nvmem_cfg.size = chip->nvram_size;
1699 ds1307->nvmem_cfg.reg_read = ds1307_nvram_read;
1700 ds1307->nvmem_cfg.reg_write = ds1307_nvram_write;
1701 ds1307->nvmem_cfg.priv = ds1307;
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001702
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001703 ds1307->rtc->nvmem_config = &ds1307->nvmem_cfg;
1704 ds1307->rtc->nvram_old_abi = true;
David Brownell682d73f2007-11-14 16:58:32 -08001705 }
1706
Heiner Kallweit1efb98b2017-07-12 07:49:44 +02001707 ds1307->rtc->ops = chip->rtc_ops ?: &ds13xx_rtc_ops;
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001708 err = rtc_register_device(ds1307->rtc);
1709 if (err)
1710 return err;
1711
Akinobu Mita445c0202016-01-25 00:22:16 +09001712 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001713 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001714
David Brownell1abb0dc2006-06-25 05:48:17 -07001715 return 0;
1716
Jingoo Hanedca66d2013-07-03 15:07:05 -07001717exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001718 return err;
1719}
1720
David Brownell1abb0dc2006-06-25 05:48:17 -07001721static struct i2c_driver ds1307_driver = {
1722 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001723 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001724 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001725 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001726 },
David Brownellc065f352007-07-17 04:05:10 -07001727 .probe = ds1307_probe,
Jean Delvare3760f732008-04-29 23:11:40 +02001728 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001729};
1730
Axel Lin0abc9202012-03-23 15:02:31 -07001731module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001732
1733MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1734MODULE_LICENSE("GPL");