blob: 57a194eca491f836555fa3cb104967974d51f8c1 [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;
David Brownell1abb0dc2006-06-25 05:48:17 -0700129 struct rtc_device *rtc;
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900130#ifdef CONFIG_COMMON_CLK
131 struct clk_hw clks[2];
132#endif
David Brownell1abb0dc2006-06-25 05:48:17 -0700133};
134
David Brownell045e0e82007-07-17 04:04:55 -0700135struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700136 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700137 u16 nvram_offset;
138 u16 nvram_size;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200139 u8 century_reg;
140 u8 century_enable_bit;
141 u8 century_bit;
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200142 u8 bbsqi_bit;
Heiner Kallweit45947122017-07-12 07:49:41 +0200143 irq_handler_t irq_handler;
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200144 const struct rtc_class_ops *rtc_ops;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700145 u16 trickle_charger_reg;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100146 u8 (*do_trickle_setup)(struct ds1307 *, uint32_t,
147 bool);
David Brownell045e0e82007-07-17 04:04:55 -0700148};
149
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200150static int ds1307_get_time(struct device *dev, struct rtc_time *t);
151static int ds1307_set_time(struct device *dev, struct rtc_time *t);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100152static u8 do_trickle_setup_ds1339(struct ds1307 *, uint32_t ohms, bool diode);
Heiner Kallweit45947122017-07-12 07:49:41 +0200153static irqreturn_t rx8130_irq(int irq, void *dev_id);
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200154static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t);
155static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t);
156static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled);
Heiner Kallweit45947122017-07-12 07:49:41 +0200157static irqreturn_t mcp794xx_irq(int irq, void *dev_id);
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200158static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t);
159static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t);
160static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled);
161
162static const struct rtc_class_ops rx8130_rtc_ops = {
163 .read_time = ds1307_get_time,
164 .set_time = ds1307_set_time,
165 .read_alarm = rx8130_read_alarm,
166 .set_alarm = rx8130_set_alarm,
167 .alarm_irq_enable = rx8130_alarm_irq_enable,
168};
169
170static const struct rtc_class_ops mcp794xx_rtc_ops = {
171 .read_time = ds1307_get_time,
172 .set_time = ds1307_set_time,
173 .read_alarm = mcp794xx_read_alarm,
174 .set_alarm = mcp794xx_set_alarm,
175 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
176};
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700177
Heiner Kallweit7624df42017-07-12 07:49:33 +0200178static const struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700179 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700180 .nvram_offset = 8,
181 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700182 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200183 [ds_1308] = {
184 .nvram_offset = 8,
185 .nvram_size = 56,
186 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700187 [ds_1337] = {
188 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200189 .century_reg = DS1307_REG_MONTH,
190 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700191 },
192 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700193 .nvram_offset = 8,
194 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700195 },
196 [ds_1339] = {
197 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200198 .century_reg = DS1307_REG_MONTH,
199 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200200 .bbsqi_bit = DS1339_BIT_BBSQI,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700201 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700202 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700203 },
204 [ds_1340] = {
Heiner Kallweite48585d2017-06-05 17:57:33 +0200205 .century_reg = DS1307_REG_HOUR,
206 .century_enable_bit = DS1340_BIT_CENTURY_EN,
207 .century_bit = DS1340_BIT_CENTURY,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700208 .trickle_charger_reg = 0x08,
209 },
210 [ds_1388] = {
211 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700212 },
213 [ds_3231] = {
214 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200215 .century_reg = DS1307_REG_MONTH,
216 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200217 .bbsqi_bit = DS3231_BIT_BBSQW,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700218 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200219 [rx_8130] = {
220 .alarm = 1,
221 /* this is battery backed SRAM */
222 .nvram_offset = 0x20,
223 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
Heiner Kallweit45947122017-07-12 07:49:41 +0200224 .irq_handler = rx8130_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200225 .rtc_ops = &rx8130_rtc_ops,
Marek Vasutee0981b2017-06-18 22:55:28 +0200226 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800227 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700228 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700229 /* this is battery backed SRAM */
230 .nvram_offset = 0x20,
231 .nvram_size = 0x40,
Heiner Kallweit45947122017-07-12 07:49:41 +0200232 .irq_handler = mcp794xx_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200233 .rtc_ops = &mcp794xx_rtc_ops,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700234 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700235};
David Brownell045e0e82007-07-17 04:04:55 -0700236
Jean Delvare3760f732008-04-29 23:11:40 +0200237static const struct i2c_device_id ds1307_id[] = {
238 { "ds1307", ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200239 { "ds1308", ds_1308 },
Jean Delvare3760f732008-04-29 23:11:40 +0200240 { "ds1337", ds_1337 },
241 { "ds1338", ds_1338 },
242 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700243 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200244 { "ds1340", ds_1340 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700245 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700246 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200247 { "m41t00", m41t00 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800248 { "mcp7940x", mcp794xx },
249 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700250 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700251 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200252 { "isl12057", ds_1337 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200253 { "rx8130", rx_8130 },
Jean Delvare3760f732008-04-29 23:11:40 +0200254 { }
255};
256MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700257
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300258#ifdef CONFIG_OF
259static const struct of_device_id ds1307_of_match[] = {
260 {
261 .compatible = "dallas,ds1307",
262 .data = (void *)ds_1307
263 },
264 {
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200265 .compatible = "dallas,ds1308",
266 .data = (void *)ds_1308
267 },
268 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300269 .compatible = "dallas,ds1337",
270 .data = (void *)ds_1337
271 },
272 {
273 .compatible = "dallas,ds1338",
274 .data = (void *)ds_1338
275 },
276 {
277 .compatible = "dallas,ds1339",
278 .data = (void *)ds_1339
279 },
280 {
281 .compatible = "dallas,ds1388",
282 .data = (void *)ds_1388
283 },
284 {
285 .compatible = "dallas,ds1340",
286 .data = (void *)ds_1340
287 },
288 {
289 .compatible = "maxim,ds3231",
290 .data = (void *)ds_3231
291 },
292 {
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200293 .compatible = "st,m41t0",
294 .data = (void *)m41t00
295 },
296 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300297 .compatible = "st,m41t00",
298 .data = (void *)m41t00
299 },
300 {
301 .compatible = "microchip,mcp7940x",
302 .data = (void *)mcp794xx
303 },
304 {
305 .compatible = "microchip,mcp7941x",
306 .data = (void *)mcp794xx
307 },
308 {
309 .compatible = "pericom,pt7c4338",
310 .data = (void *)ds_1307
311 },
312 {
313 .compatible = "epson,rx8025",
314 .data = (void *)rx_8025
315 },
316 {
317 .compatible = "isil,isl12057",
318 .data = (void *)ds_1337
319 },
320 { }
321};
322MODULE_DEVICE_TABLE(of, ds1307_of_match);
323#endif
324
Tin Huynh9c19b892016-11-30 09:57:31 +0700325#ifdef CONFIG_ACPI
326static const struct acpi_device_id ds1307_acpi_ids[] = {
327 { .id = "DS1307", .driver_data = ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200328 { .id = "DS1308", .driver_data = ds_1308 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700329 { .id = "DS1337", .driver_data = ds_1337 },
330 { .id = "DS1338", .driver_data = ds_1338 },
331 { .id = "DS1339", .driver_data = ds_1339 },
332 { .id = "DS1388", .driver_data = ds_1388 },
333 { .id = "DS1340", .driver_data = ds_1340 },
334 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700335 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700336 { .id = "M41T00", .driver_data = m41t00 },
337 { .id = "MCP7940X", .driver_data = mcp794xx },
338 { .id = "MCP7941X", .driver_data = mcp794xx },
339 { .id = "PT7C4338", .driver_data = ds_1307 },
340 { .id = "RX8025", .driver_data = rx_8025 },
341 { .id = "ISL12057", .driver_data = ds_1337 },
342 { }
343};
344MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
345#endif
346
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700347/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700348 * The ds1337 and ds1339 both have two alarms, but we only use the first
349 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
350 * signal; ds1339 chips have only one alarm signal.
351 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500352static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700353{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100354 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500355 struct mutex *lock = &ds1307->rtc->ops_lock;
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200356 int stat, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700357
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700358 mutex_lock(lock);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100359 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
360 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700361 goto out;
362
363 if (stat & DS1337_BIT_A1I) {
364 stat &= ~DS1337_BIT_A1I;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100365 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700366
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200367 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
368 DS1337_BIT_A1IE, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100369 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700370 goto out;
371
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700372 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700373 }
374
375out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700376 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700377
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700378 return IRQ_HANDLED;
379}
380
381/*----------------------------------------------------------------------*/
382
David Brownell1abb0dc2006-06-25 05:48:17 -0700383static int ds1307_get_time(struct device *dev, struct rtc_time *t)
384{
385 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100386 int tmp, ret;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200387 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell1abb0dc2006-06-25 05:48:17 -0700388
David Brownell045e0e82007-07-17 04:04:55 -0700389 /* read the RTC date and time registers all at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100390 ret = regmap_bulk_read(ds1307->regmap, ds1307->offset, ds1307->regs, 7);
391 if (ret) {
392 dev_err(dev, "%s error %d\n", "read", ret);
393 return ret;
David Brownell1abb0dc2006-06-25 05:48:17 -0700394 }
395
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800396 dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700397
Stefan Agner8566f702017-03-23 16:54:57 -0700398 /* if oscillator fail bit is set, no data can be trusted */
399 if (ds1307->type == m41t0 &&
400 ds1307->regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
401 dev_warn_once(dev, "oscillator failed, set time!\n");
402 return -EINVAL;
403 }
404
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700405 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
406 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700407 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700408 t->tm_hour = bcd2bin(tmp);
409 t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
410 t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700411 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700412 t->tm_mon = bcd2bin(tmp) - 1;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700413 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700414
Heiner Kallweite48585d2017-06-05 17:57:33 +0200415 if (ds1307->regs[chip->century_reg] & chip->century_bit &&
416 IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY))
417 t->tm_year += 100;
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200418
David Brownell1abb0dc2006-06-25 05:48:17 -0700419 dev_dbg(dev, "%s secs=%d, mins=%d, "
420 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
421 "read", t->tm_sec, t->tm_min,
422 t->tm_hour, t->tm_mday,
423 t->tm_mon, t->tm_year, t->tm_wday);
424
David Brownell045e0e82007-07-17 04:04:55 -0700425 /* initial clock setting can be undefined */
426 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700427}
428
429static int ds1307_set_time(struct device *dev, struct rtc_time *t)
430{
431 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweite48585d2017-06-05 17:57:33 +0200432 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell1abb0dc2006-06-25 05:48:17 -0700433 int result;
434 int tmp;
435 u8 *buf = ds1307->regs;
436
437 dev_dbg(dev, "%s secs=%d, mins=%d, "
438 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400439 "write", t->tm_sec, t->tm_min,
440 t->tm_hour, t->tm_mday,
441 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700442
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200443 if (t->tm_year < 100)
444 return -EINVAL;
445
Heiner Kallweite48585d2017-06-05 17:57:33 +0200446#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
447 if (t->tm_year > (chip->century_bit ? 299 : 199))
448 return -EINVAL;
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200449#else
Heiner Kallweite48585d2017-06-05 17:57:33 +0200450 if (t->tm_year > 199)
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200451 return -EINVAL;
452#endif
453
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700454 buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
455 buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
456 buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
457 buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
458 buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
459 buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700460
461 /* assume 20YY not 19YY */
462 tmp = t->tm_year - 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700463 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700464
Heiner Kallweite48585d2017-06-05 17:57:33 +0200465 if (chip->century_enable_bit)
466 buf[chip->century_reg] |= chip->century_enable_bit;
467 if (t->tm_year > 199 && chip->century_bit)
468 buf[chip->century_reg] |= chip->century_bit;
469
470 if (ds1307->type == mcp794xx) {
David Anders40ce9722012-03-23 15:02:37 -0700471 /*
472 * these bits were cleared when preparing the date/time
473 * values and need to be set again before writing the
474 * buffer out to the device.
475 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800476 buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
477 buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700478 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700479
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800480 dev_dbg(dev, "%s: %7ph\n", "write", buf);
David Brownell1abb0dc2006-06-25 05:48:17 -0700481
Heiner Kallweit11e58902017-03-10 18:52:34 +0100482 result = regmap_bulk_write(ds1307->regmap, ds1307->offset, buf, 7);
483 if (result) {
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800484 dev_err(dev, "%s error %d\n", "write", result);
485 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700486 }
487 return 0;
488}
489
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800490static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700491{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100492 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700493 int ret;
494
495 if (!test_bit(HAS_ALARM, &ds1307->flags))
496 return -EINVAL;
497
498 /* read all ALARM1, ALARM2, and status registers at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100499 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
500 ds1307->regs, 9);
501 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700502 dev_err(dev, "%s error %d\n", "alarm read", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100503 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700504 }
505
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100506 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
507 &ds1307->regs[0], &ds1307->regs[4], &ds1307->regs[7]);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700508
David Anders40ce9722012-03-23 15:02:37 -0700509 /*
510 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700511 * and that all four fields are checked matches
512 */
513 t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
514 t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
515 t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
516 t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700517
518 /* ... and status */
519 t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
520 t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
521
522 dev_dbg(dev, "%s secs=%d, mins=%d, "
523 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
524 "alarm read", t->time.tm_sec, t->time.tm_min,
525 t->time.tm_hour, t->time.tm_mday,
526 t->enabled, t->pending);
527
528 return 0;
529}
530
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800531static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700532{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100533 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700534 unsigned char *buf = ds1307->regs;
535 u8 control, status;
536 int ret;
537
538 if (!test_bit(HAS_ALARM, &ds1307->flags))
539 return -EINVAL;
540
541 dev_dbg(dev, "%s secs=%d, mins=%d, "
542 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
543 "alarm set", t->time.tm_sec, t->time.tm_min,
544 t->time.tm_hour, t->time.tm_mday,
545 t->enabled, t->pending);
546
547 /* read current status of both alarms and the chip */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100548 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, buf, 9);
549 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700550 dev_err(dev, "%s error %d\n", "alarm write", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100551 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700552 }
553 control = ds1307->regs[7];
554 status = ds1307->regs[8];
555
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100556 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
557 &ds1307->regs[0], &ds1307->regs[4], control, status);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700558
559 /* set ALARM1, using 24 hour and day-of-month modes */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700560 buf[0] = bin2bcd(t->time.tm_sec);
561 buf[1] = bin2bcd(t->time.tm_min);
562 buf[2] = bin2bcd(t->time.tm_hour);
563 buf[3] = bin2bcd(t->time.tm_mday);
564
565 /* set ALARM2 to non-garbage */
566 buf[4] = 0;
567 buf[5] = 0;
568 buf[6] = 0;
569
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200570 /* disable alarms */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700571 buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700572 buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
573
Heiner Kallweit11e58902017-03-10 18:52:34 +0100574 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, buf, 9);
575 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700576 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800577 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700578 }
579
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200580 /* optionally enable ALARM1 */
581 if (t->enabled) {
582 dev_dbg(dev, "alarm IRQ armed\n");
583 buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100584 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, buf[7]);
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200585 }
586
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700587 return 0;
588}
589
John Stultz16380c12011-02-02 17:02:41 -0800590static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700591{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100592 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700593
John Stultz16380c12011-02-02 17:02:41 -0800594 if (!test_bit(HAS_ALARM, &ds1307->flags))
595 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700596
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200597 return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
598 DS1337_BIT_A1IE,
599 enabled ? DS1337_BIT_A1IE : 0);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700600}
601
David Brownellff8371a2006-09-30 23:28:17 -0700602static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700603 .read_time = ds1307_get_time,
604 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800605 .read_alarm = ds1337_read_alarm,
606 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800607 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700608};
609
David Brownell682d73f2007-11-14 16:58:32 -0800610/*----------------------------------------------------------------------*/
611
Simon Guinot1d1945d2014-04-03 14:49:55 -0700612/*
Marek Vasutee0981b2017-06-18 22:55:28 +0200613 * Alarm support for rx8130 devices.
614 */
615
616#define RX8130_REG_ALARM_MIN 0x07
617#define RX8130_REG_ALARM_HOUR 0x08
618#define RX8130_REG_ALARM_WEEK_OR_DAY 0x09
619#define RX8130_REG_EXTENSION 0x0c
620#define RX8130_REG_EXTENSION_WADA (1 << 3)
621#define RX8130_REG_FLAG 0x0d
622#define RX8130_REG_FLAG_AF (1 << 3)
623#define RX8130_REG_CONTROL0 0x0e
624#define RX8130_REG_CONTROL0_AIE (1 << 3)
625
626static irqreturn_t rx8130_irq(int irq, void *dev_id)
627{
628 struct ds1307 *ds1307 = dev_id;
629 struct mutex *lock = &ds1307->rtc->ops_lock;
630 u8 ctl[3];
631 int ret;
632
633 mutex_lock(lock);
634
635 /* Read control registers. */
636 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
637 if (ret < 0)
638 goto out;
639 if (!(ctl[1] & RX8130_REG_FLAG_AF))
640 goto out;
641 ctl[1] &= ~RX8130_REG_FLAG_AF;
642 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
643
644 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
645 if (ret < 0)
646 goto out;
647
648 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
649
650out:
651 mutex_unlock(lock);
652
653 return IRQ_HANDLED;
654}
655
656static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
657{
658 struct ds1307 *ds1307 = dev_get_drvdata(dev);
659 u8 ald[3], ctl[3];
660 int ret;
661
662 if (!test_bit(HAS_ALARM, &ds1307->flags))
663 return -EINVAL;
664
665 /* Read alarm registers. */
666 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, 3);
667 if (ret < 0)
668 return ret;
669
670 /* Read control registers. */
671 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
672 if (ret < 0)
673 return ret;
674
675 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
676 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
677
678 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
679 t->time.tm_sec = -1;
680 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
681 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
682 t->time.tm_wday = -1;
683 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
684 t->time.tm_mon = -1;
685 t->time.tm_year = -1;
686 t->time.tm_yday = -1;
687 t->time.tm_isdst = -1;
688
689 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
690 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
691 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
692
693 return 0;
694}
695
696static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
697{
698 struct ds1307 *ds1307 = dev_get_drvdata(dev);
699 u8 ald[3], ctl[3];
700 int ret;
701
702 if (!test_bit(HAS_ALARM, &ds1307->flags))
703 return -EINVAL;
704
705 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
706 "enabled=%d pending=%d\n", __func__,
707 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
708 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
709 t->enabled, t->pending);
710
711 /* Read control registers. */
712 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
713 if (ret < 0)
714 return ret;
715
716 ctl[0] &= ~RX8130_REG_EXTENSION_WADA;
717 ctl[1] |= RX8130_REG_FLAG_AF;
718 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
719
720 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
721 if (ret < 0)
722 return ret;
723
724 /* Hardware alarm precision is 1 minute! */
725 ald[0] = bin2bcd(t->time.tm_min);
726 ald[1] = bin2bcd(t->time.tm_hour);
727 ald[2] = bin2bcd(t->time.tm_mday);
728
729 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, 3);
730 if (ret < 0)
731 return ret;
732
733 if (!t->enabled)
734 return 0;
735
736 ctl[2] |= RX8130_REG_CONTROL0_AIE;
737
738 return regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
739}
740
741static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
742{
743 struct ds1307 *ds1307 = dev_get_drvdata(dev);
744 int ret, reg;
745
746 if (!test_bit(HAS_ALARM, &ds1307->flags))
747 return -EINVAL;
748
749 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
750 if (ret < 0)
751 return ret;
752
753 if (enabled)
754 reg |= RX8130_REG_CONTROL0_AIE;
755 else
756 reg &= ~RX8130_REG_CONTROL0_AIE;
757
758 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
759}
760
Marek Vasutee0981b2017-06-18 22:55:28 +0200761/*----------------------------------------------------------------------*/
762
763/*
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800764 * Alarm support for mcp794xx devices.
Simon Guinot1d1945d2014-04-03 14:49:55 -0700765 */
766
Keerthye29385f2016-06-01 16:19:07 +0530767#define MCP794XX_REG_WEEKDAY 0x3
768#define MCP794XX_REG_WEEKDAY_WDAY_MASK 0x7
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800769#define MCP794XX_REG_CONTROL 0x07
770# define MCP794XX_BIT_ALM0_EN 0x10
771# define MCP794XX_BIT_ALM1_EN 0x20
772#define MCP794XX_REG_ALARM0_BASE 0x0a
773#define MCP794XX_REG_ALARM0_CTRL 0x0d
774#define MCP794XX_REG_ALARM1_BASE 0x11
775#define MCP794XX_REG_ALARM1_CTRL 0x14
776# define MCP794XX_BIT_ALMX_IF (1 << 3)
777# define MCP794XX_BIT_ALMX_C0 (1 << 4)
778# define MCP794XX_BIT_ALMX_C1 (1 << 5)
779# define MCP794XX_BIT_ALMX_C2 (1 << 6)
780# define MCP794XX_BIT_ALMX_POL (1 << 7)
781# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
782 MCP794XX_BIT_ALMX_C1 | \
783 MCP794XX_BIT_ALMX_C2)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700784
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500785static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700786{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100787 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500788 struct mutex *lock = &ds1307->rtc->ops_lock;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700789 int reg, ret;
790
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500791 mutex_lock(lock);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700792
793 /* Check and clear alarm 0 interrupt flag. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100794 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
795 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700796 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800797 if (!(reg & MCP794XX_BIT_ALMX_IF))
Simon Guinot1d1945d2014-04-03 14:49:55 -0700798 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800799 reg &= ~MCP794XX_BIT_ALMX_IF;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100800 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
801 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700802 goto out;
803
804 /* Disable alarm 0. */
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200805 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
806 MCP794XX_BIT_ALM0_EN, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100807 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700808 goto out;
809
810 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
811
812out:
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500813 mutex_unlock(lock);
814
815 return IRQ_HANDLED;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700816}
817
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800818static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700819{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100820 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700821 u8 *regs = ds1307->regs;
822 int ret;
823
824 if (!test_bit(HAS_ALARM, &ds1307->flags))
825 return -EINVAL;
826
827 /* Read control and alarm 0 registers. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100828 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
829 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700830 return ret;
831
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800832 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700833
834 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
835 t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f);
836 t->time.tm_min = bcd2bin(ds1307->regs[4] & 0x7f);
837 t->time.tm_hour = bcd2bin(ds1307->regs[5] & 0x3f);
838 t->time.tm_wday = bcd2bin(ds1307->regs[6] & 0x7) - 1;
839 t->time.tm_mday = bcd2bin(ds1307->regs[7] & 0x3f);
840 t->time.tm_mon = bcd2bin(ds1307->regs[8] & 0x1f) - 1;
841 t->time.tm_year = -1;
842 t->time.tm_yday = -1;
843 t->time.tm_isdst = -1;
844
845 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
846 "enabled=%d polarity=%d irq=%d match=%d\n", __func__,
847 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
848 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800849 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL),
850 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF),
851 (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700852
853 return 0;
854}
855
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800856static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700857{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100858 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700859 unsigned char *regs = ds1307->regs;
860 int ret;
861
862 if (!test_bit(HAS_ALARM, &ds1307->flags))
863 return -EINVAL;
864
865 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
866 "enabled=%d pending=%d\n", __func__,
867 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
868 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
869 t->enabled, t->pending);
870
871 /* Read control and alarm 0 registers. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100872 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
873 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700874 return ret;
875
876 /* Set alarm 0, using 24-hour and day-of-month modes. */
877 regs[3] = bin2bcd(t->time.tm_sec);
878 regs[4] = bin2bcd(t->time.tm_min);
879 regs[5] = bin2bcd(t->time.tm_hour);
Tero Kristo62c8c202015-10-23 09:29:57 +0300880 regs[6] = bin2bcd(t->time.tm_wday + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700881 regs[7] = bin2bcd(t->time.tm_mday);
Tero Kristo62c8c202015-10-23 09:29:57 +0300882 regs[8] = bin2bcd(t->time.tm_mon + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700883
884 /* Clear the alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800885 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700886 /* Set alarm match: second, minute, hour, day, date, month. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800887 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
Nishanth Menone3edd672015-04-20 19:51:34 -0500888 /* Disable interrupt. We will not enable until completely programmed */
889 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700890
Heiner Kallweit11e58902017-03-10 18:52:34 +0100891 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
892 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700893 return ret;
894
Nishanth Menone3edd672015-04-20 19:51:34 -0500895 if (!t->enabled)
896 return 0;
897 regs[0] |= MCP794XX_BIT_ALM0_EN;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100898 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700899}
900
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800901static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700902{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100903 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700904
905 if (!test_bit(HAS_ALARM, &ds1307->flags))
906 return -EINVAL;
907
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200908 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
909 MCP794XX_BIT_ALM0_EN,
910 enabled ? MCP794XX_BIT_ALM0_EN : 0);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700911}
912
Simon Guinot1d1945d2014-04-03 14:49:55 -0700913/*----------------------------------------------------------------------*/
914
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200915static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
916 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -0800917{
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200918 struct ds1307 *ds1307 = priv;
David Brownell682d73f2007-11-14 16:58:32 -0800919
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200920 return regmap_bulk_read(ds1307->regmap, ds1307->nvram_offset + offset,
921 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -0800922}
923
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200924static int ds1307_nvram_write(void *priv, unsigned int offset, void *val,
925 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -0800926{
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200927 struct ds1307 *ds1307 = priv;
David Brownell682d73f2007-11-14 16:58:32 -0800928
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200929 return regmap_bulk_write(ds1307->regmap, ds1307->nvram_offset + offset,
930 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -0800931}
932
David Brownell682d73f2007-11-14 16:58:32 -0800933/*----------------------------------------------------------------------*/
934
Heiner Kallweit11e58902017-03-10 18:52:34 +0100935static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700936 uint32_t ohms, bool diode)
937{
938 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
939 DS1307_TRICKLE_CHARGER_NO_DIODE;
940
941 switch (ohms) {
942 case 250:
943 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
944 break;
945 case 2000:
946 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
947 break;
948 case 4000:
949 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
950 break;
951 default:
Heiner Kallweit11e58902017-03-10 18:52:34 +0100952 dev_warn(ds1307->dev,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700953 "Unsupported ohm value %u in dt\n", ohms);
954 return 0;
955 }
956 return setup;
957}
958
Heiner Kallweitd8490fd2017-07-12 07:49:28 +0200959static u8 ds1307_trickle_init(struct ds1307 *ds1307,
Heiner Kallweit7624df42017-07-12 07:49:33 +0200960 const struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700961{
Heiner Kallweitd8490fd2017-07-12 07:49:28 +0200962 uint32_t ohms;
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700963 bool diode = true;
964
965 if (!chip->do_trickle_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +0200966 return 0;
967
Heiner Kallweit11e58902017-03-10 18:52:34 +0100968 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
969 &ohms))
Heiner Kallweitd8490fd2017-07-12 07:49:28 +0200970 return 0;
971
Heiner Kallweit11e58902017-03-10 18:52:34 +0100972 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700973 diode = false;
Heiner Kallweitd8490fd2017-07-12 07:49:28 +0200974
975 return chip->do_trickle_setup(ds1307, ohms, diode);
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700976}
977
Akinobu Mita445c0202016-01-25 00:22:16 +0900978/*----------------------------------------------------------------------*/
979
980#ifdef CONFIG_RTC_DRV_DS1307_HWMON
981
982/*
983 * Temperature sensor support for ds3231 devices.
984 */
985
986#define DS3231_REG_TEMPERATURE 0x11
987
988/*
989 * A user-initiated temperature conversion is not started by this function,
990 * so the temperature is updated once every 64 seconds.
991 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +0900992static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +0900993{
994 struct ds1307 *ds1307 = dev_get_drvdata(dev);
995 u8 temp_buf[2];
996 s16 temp;
997 int ret;
998
Heiner Kallweit11e58902017-03-10 18:52:34 +0100999 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
1000 temp_buf, sizeof(temp_buf));
1001 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +09001002 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +09001003 /*
1004 * Temperature is represented as a 10-bit code with a resolution of
1005 * 0.25 degree celsius and encoded in two's complement format.
1006 */
1007 temp = (temp_buf[0] << 8) | temp_buf[1];
1008 temp >>= 6;
1009 *mC = temp * 250;
1010
1011 return 0;
1012}
1013
1014static ssize_t ds3231_hwmon_show_temp(struct device *dev,
1015 struct device_attribute *attr, char *buf)
1016{
1017 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001018 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001019
1020 ret = ds3231_hwmon_read_temp(dev, &temp);
1021 if (ret)
1022 return ret;
1023
1024 return sprintf(buf, "%d\n", temp);
1025}
1026static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ds3231_hwmon_show_temp,
1027 NULL, 0);
1028
1029static struct attribute *ds3231_hwmon_attrs[] = {
1030 &sensor_dev_attr_temp1_input.dev_attr.attr,
1031 NULL,
1032};
1033ATTRIBUTE_GROUPS(ds3231_hwmon);
1034
1035static void ds1307_hwmon_register(struct ds1307 *ds1307)
1036{
1037 struct device *dev;
1038
1039 if (ds1307->type != ds_3231)
1040 return;
1041
Heiner Kallweit11e58902017-03-10 18:52:34 +01001042 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Akinobu Mita445c0202016-01-25 00:22:16 +09001043 ds1307, ds3231_hwmon_groups);
1044 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001045 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1046 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001047 }
1048}
1049
1050#else
1051
1052static void ds1307_hwmon_register(struct ds1307 *ds1307)
1053{
1054}
1055
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001056#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1057
1058/*----------------------------------------------------------------------*/
1059
1060/*
1061 * Square-wave output support for DS3231
1062 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1063 */
1064#ifdef CONFIG_COMMON_CLK
1065
1066enum {
1067 DS3231_CLK_SQW = 0,
1068 DS3231_CLK_32KHZ,
1069};
1070
1071#define clk_sqw_to_ds1307(clk) \
1072 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1073#define clk_32khz_to_ds1307(clk) \
1074 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1075
1076static int ds3231_clk_sqw_rates[] = {
1077 1,
1078 1024,
1079 4096,
1080 8192,
1081};
1082
1083static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1084{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001085 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001086 int ret;
1087
1088 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001089 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1090 mask, value);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001091 mutex_unlock(lock);
1092
1093 return ret;
1094}
1095
1096static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1097 unsigned long parent_rate)
1098{
1099 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001100 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001101 int rate_sel = 0;
1102
Heiner Kallweit11e58902017-03-10 18:52:34 +01001103 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1104 if (ret)
1105 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001106 if (control & DS1337_BIT_RS1)
1107 rate_sel += 1;
1108 if (control & DS1337_BIT_RS2)
1109 rate_sel += 2;
1110
1111 return ds3231_clk_sqw_rates[rate_sel];
1112}
1113
1114static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
1115 unsigned long *prate)
1116{
1117 int i;
1118
1119 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1120 if (ds3231_clk_sqw_rates[i] <= rate)
1121 return ds3231_clk_sqw_rates[i];
1122 }
1123
1124 return 0;
1125}
1126
1127static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
1128 unsigned long parent_rate)
1129{
1130 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1131 int control = 0;
1132 int rate_sel;
1133
1134 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1135 rate_sel++) {
1136 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1137 break;
1138 }
1139
1140 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1141 return -EINVAL;
1142
1143 if (rate_sel & 1)
1144 control |= DS1337_BIT_RS1;
1145 if (rate_sel & 2)
1146 control |= DS1337_BIT_RS2;
1147
1148 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1149 control);
1150}
1151
1152static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1153{
1154 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1155
1156 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1157}
1158
1159static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1160{
1161 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1162
1163 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1164}
1165
1166static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1167{
1168 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001169 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001170
Heiner Kallweit11e58902017-03-10 18:52:34 +01001171 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1172 if (ret)
1173 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001174
1175 return !(control & DS1337_BIT_INTCN);
1176}
1177
1178static const struct clk_ops ds3231_clk_sqw_ops = {
1179 .prepare = ds3231_clk_sqw_prepare,
1180 .unprepare = ds3231_clk_sqw_unprepare,
1181 .is_prepared = ds3231_clk_sqw_is_prepared,
1182 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1183 .round_rate = ds3231_clk_sqw_round_rate,
1184 .set_rate = ds3231_clk_sqw_set_rate,
1185};
1186
1187static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
1188 unsigned long parent_rate)
1189{
1190 return 32768;
1191}
1192
1193static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1194{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001195 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001196 int ret;
1197
1198 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001199 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1200 DS3231_BIT_EN32KHZ,
1201 enable ? DS3231_BIT_EN32KHZ : 0);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001202 mutex_unlock(lock);
1203
1204 return ret;
1205}
1206
1207static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1208{
1209 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1210
1211 return ds3231_clk_32khz_control(ds1307, true);
1212}
1213
1214static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1215{
1216 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1217
1218 ds3231_clk_32khz_control(ds1307, false);
1219}
1220
1221static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1222{
1223 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001224 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001225
Heiner Kallweit11e58902017-03-10 18:52:34 +01001226 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1227 if (ret)
1228 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001229
1230 return !!(status & DS3231_BIT_EN32KHZ);
1231}
1232
1233static const struct clk_ops ds3231_clk_32khz_ops = {
1234 .prepare = ds3231_clk_32khz_prepare,
1235 .unprepare = ds3231_clk_32khz_unprepare,
1236 .is_prepared = ds3231_clk_32khz_is_prepared,
1237 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1238};
1239
1240static struct clk_init_data ds3231_clks_init[] = {
1241 [DS3231_CLK_SQW] = {
1242 .name = "ds3231_clk_sqw",
1243 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001244 },
1245 [DS3231_CLK_32KHZ] = {
1246 .name = "ds3231_clk_32khz",
1247 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001248 },
1249};
1250
1251static int ds3231_clks_register(struct ds1307 *ds1307)
1252{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001253 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001254 struct clk_onecell_data *onecell;
1255 int i;
1256
Heiner Kallweit11e58902017-03-10 18:52:34 +01001257 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001258 if (!onecell)
1259 return -ENOMEM;
1260
1261 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001262 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1263 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001264 if (!onecell->clks)
1265 return -ENOMEM;
1266
1267 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1268 struct clk_init_data init = ds3231_clks_init[i];
1269
1270 /*
1271 * Interrupt signal due to alarm conditions and square-wave
1272 * output share same pin, so don't initialize both.
1273 */
1274 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1275 continue;
1276
1277 /* optional override of the clockname */
1278 of_property_read_string_index(node, "clock-output-names", i,
1279 &init.name);
1280 ds1307->clks[i].init = &init;
1281
Heiner Kallweit11e58902017-03-10 18:52:34 +01001282 onecell->clks[i] = devm_clk_register(ds1307->dev,
1283 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001284 if (IS_ERR(onecell->clks[i]))
1285 return PTR_ERR(onecell->clks[i]);
1286 }
1287
1288 if (!node)
1289 return 0;
1290
1291 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1292
1293 return 0;
1294}
1295
1296static void ds1307_clks_register(struct ds1307 *ds1307)
1297{
1298 int ret;
1299
1300 if (ds1307->type != ds_3231)
1301 return;
1302
1303 ret = ds3231_clks_register(ds1307);
1304 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001305 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1306 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001307 }
1308}
1309
1310#else
1311
1312static void ds1307_clks_register(struct ds1307 *ds1307)
1313{
1314}
1315
1316#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001317
Heiner Kallweit11e58902017-03-10 18:52:34 +01001318static const struct regmap_config regmap_config = {
1319 .reg_bits = 8,
1320 .val_bits = 8,
1321 .max_register = 0x12,
1322};
1323
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001324static int ds1307_probe(struct i2c_client *client,
1325 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001326{
1327 struct ds1307 *ds1307;
1328 int err = -ENODEV;
Keerthye29385f2016-06-01 16:19:07 +05301329 int tmp, wday;
Heiner Kallweit7624df42017-07-12 07:49:33 +02001330 const struct chip_desc *chip;
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001331 bool want_irq;
Michael Lange8bc2a402016-01-21 18:10:16 +01001332 bool ds1307_can_wakeup_device = false;
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001333 unsigned char *buf;
Jingoo Han01ce8932013-11-12 15:10:41 -08001334 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Keerthye29385f2016-06-01 16:19:07 +05301335 struct rtc_time tm;
1336 unsigned long timestamp;
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001337 u8 trickle_charger_setup = 0;
Keerthye29385f2016-06-01 16:19:07 +05301338
Jingoo Hanedca66d2013-07-03 15:07:05 -07001339 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001340 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001341 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001342
Heiner Kallweit11e58902017-03-10 18:52:34 +01001343 dev_set_drvdata(&client->dev, ds1307);
1344 ds1307->dev = &client->dev;
1345 ds1307->name = client->name;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001346
Heiner Kallweit11e58902017-03-10 18:52:34 +01001347 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1348 if (IS_ERR(ds1307->regmap)) {
1349 dev_err(ds1307->dev, "regmap allocation failed\n");
1350 return PTR_ERR(ds1307->regmap);
1351 }
1352
1353 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001354
1355 if (client->dev.of_node) {
1356 ds1307->type = (enum ds_type)
1357 of_device_get_match_data(&client->dev);
1358 chip = &chips[ds1307->type];
1359 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001360 chip = &chips[id->driver_data];
1361 ds1307->type = id->driver_data;
1362 } else {
1363 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001364
Tin Huynh9c19b892016-11-30 09:57:31 +07001365 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001366 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001367 if (!acpi_id)
1368 return -ENODEV;
1369 chip = &chips[acpi_id->driver_data];
1370 ds1307->type = acpi_id->driver_data;
1371 }
1372
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001373 want_irq = client->irq > 0 && chip->alarm;
1374
Tin Huynh9c19b892016-11-30 09:57:31 +07001375 if (!pdata)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001376 trickle_charger_setup = ds1307_trickle_init(ds1307, chip);
Tin Huynh9c19b892016-11-30 09:57:31 +07001377 else if (pdata->trickle_charger_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001378 trickle_charger_setup = pdata->trickle_charger_setup;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001379
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001380 if (trickle_charger_setup && chip->trickle_charger_reg) {
1381 trickle_charger_setup |= DS13XX_TRICKLE_CHARGER_MAGIC;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001382 dev_dbg(ds1307->dev,
1383 "writing trickle charger info 0x%x to 0x%x\n",
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001384 trickle_charger_setup, chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001385 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001386 trickle_charger_setup);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001387 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001388
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001389 buf = ds1307->regs;
David Brownell045e0e82007-07-17 04:04:55 -07001390
Michael Lange8bc2a402016-01-21 18:10:16 +01001391#ifdef CONFIG_OF
1392/*
1393 * For devices with no IRQ directly connected to the SoC, the RTC chip
1394 * can be forced as a wakeup source by stating that explicitly in
1395 * the device's .dts file using the "wakeup-source" boolean property.
1396 * If the "wakeup-source" property is set, don't request an IRQ.
1397 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1398 * if supported by the RTC.
1399 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001400 if (chip->alarm && of_property_read_bool(client->dev.of_node,
1401 "wakeup-source"))
Michael Lange8bc2a402016-01-21 18:10:16 +01001402 ds1307_can_wakeup_device = true;
Michael Lange8bc2a402016-01-21 18:10:16 +01001403#endif
1404
David Brownell045e0e82007-07-17 04:04:55 -07001405 switch (ds1307->type) {
1406 case ds_1337:
1407 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001408 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001409 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001410 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
1411 buf, 2);
1412 if (err) {
1413 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001414 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001415 }
1416
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001417 /* oscillator off? turn it on, so clock can tick. */
1418 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001419 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
1420
David Anders40ce9722012-03-23 15:02:37 -07001421 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001422 * Using IRQ or defined as wakeup-source?
1423 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001424 * For some variants, be sure alarms can trigger when we're
1425 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001426 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001427 if (want_irq || ds1307_can_wakeup_device) {
Heiner Kallweit0b6ee802017-07-12 07:49:22 +02001428 ds1307->regs[0] |= DS1337_BIT_INTCN | chip->bbsqi_bit;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001429 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
1430 }
1431
Heiner Kallweit11e58902017-03-10 18:52:34 +01001432 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
1433 ds1307->regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001434
1435 /* oscillator fault? clear flag, and warn */
1436 if (ds1307->regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001437 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
1438 ds1307->regs[1] & ~DS1337_BIT_OSF);
1439 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001440 }
David Brownell045e0e82007-07-17 04:04:55 -07001441 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001442
1443 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001444 err = regmap_bulk_read(ds1307->regmap,
1445 RX8025_REG_CTRL1 << 4 | 0x08, buf, 2);
1446 if (err) {
1447 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001448 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001449 }
1450
1451 /* oscillator off? turn it on, so clock can tick. */
1452 if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
1453 ds1307->regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001454 regmap_write(ds1307->regmap,
1455 RX8025_REG_CTRL2 << 4 | 0x08,
1456 ds1307->regs[1]);
1457 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001458 "oscillator stop detected - SET TIME!\n");
1459 }
1460
1461 if (ds1307->regs[1] & RX8025_BIT_PON) {
1462 ds1307->regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001463 regmap_write(ds1307->regmap,
1464 RX8025_REG_CTRL2 << 4 | 0x08,
1465 ds1307->regs[1]);
1466 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001467 }
1468
1469 if (ds1307->regs[1] & RX8025_BIT_VDET) {
1470 ds1307->regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001471 regmap_write(ds1307->regmap,
1472 RX8025_REG_CTRL2 << 4 | 0x08,
1473 ds1307->regs[1]);
1474 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001475 }
1476
1477 /* make sure we are running in 24hour mode */
1478 if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
1479 u8 hour;
1480
1481 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001482 regmap_write(ds1307->regmap,
1483 RX8025_REG_CTRL1 << 4 | 0x08,
1484 ds1307->regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001485
Heiner Kallweit11e58902017-03-10 18:52:34 +01001486 err = regmap_bulk_read(ds1307->regmap,
1487 RX8025_REG_CTRL1 << 4 | 0x08,
1488 buf, 2);
1489 if (err) {
1490 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001491 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001492 }
1493
1494 /* correct hour */
1495 hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
1496 if (hour == 12)
1497 hour = 0;
1498 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1499 hour += 12;
1500
Heiner Kallweit11e58902017-03-10 18:52:34 +01001501 regmap_write(ds1307->regmap,
1502 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001503 }
1504 break;
Marek Vasutee0981b2017-06-18 22:55:28 +02001505 case rx_8130:
1506 ds1307->offset = 0x10; /* Seconds starts at 0x10 */
Marek Vasutee0981b2017-06-18 22:55:28 +02001507 break;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001508 case ds_1388:
1509 ds1307->offset = 1; /* Seconds starts at 1 */
1510 break;
David Brownell045e0e82007-07-17 04:04:55 -07001511 default:
1512 break;
1513 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001514
1515read_rtc:
1516 /* read RTC registers */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001517 err = regmap_bulk_read(ds1307->regmap, ds1307->offset, buf, 8);
1518 if (err) {
1519 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001520 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001521 }
1522
David Anders40ce9722012-03-23 15:02:37 -07001523 /*
1524 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001525 * specify the extra bits as must-be-zero, but there are
1526 * still a few values that are clearly out-of-range.
1527 */
1528 tmp = ds1307->regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001529 switch (ds1307->type) {
1530 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001531 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001532 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001533 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001534 if (tmp & DS1307_BIT_CH) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001535 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1536 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001537 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001538 }
David Brownell045e0e82007-07-17 04:04:55 -07001539 break;
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001540 case ds_1308:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001541 case ds_1338:
1542 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001543 if (tmp & DS1307_BIT_CH)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001544 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001545
1546 /* oscillator fault? clear flag, and warn */
1547 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001548 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
1549 ds1307->regs[DS1307_REG_CONTROL] &
1550 ~DS1338_BIT_OSF);
1551 dev_warn(ds1307->dev, "SET TIME!\n");
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001552 goto read_rtc;
1553 }
David Brownell045e0e82007-07-17 04:04:55 -07001554 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001555 case ds_1340:
1556 /* clock halted? turn it on, so clock can tick. */
1557 if (tmp & DS1340_BIT_nEOSC)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001558 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
frederic Rodofcd8db02008-02-06 01:38:55 -08001559
Heiner Kallweit11e58902017-03-10 18:52:34 +01001560 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1561 if (err) {
1562 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001563 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001564 }
1565
1566 /* oscillator fault? clear flag, and warn */
1567 if (tmp & DS1340_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001568 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1569 dev_warn(ds1307->dev, "SET TIME!\n");
frederic Rodofcd8db02008-02-06 01:38:55 -08001570 }
1571 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001572 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001573 /* make sure that the backup battery is enabled */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001574 if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001575 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
1576 ds1307->regs[DS1307_REG_WDAY] |
1577 MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001578 }
1579
1580 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001581 if (!(tmp & MCP794XX_BIT_ST)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001582 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1583 MCP794XX_BIT_ST);
1584 dev_warn(ds1307->dev, "SET TIME!\n");
David Anders43fcb812011-11-02 13:37:53 -07001585 goto read_rtc;
1586 }
1587
1588 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001589 default:
David Brownell045e0e82007-07-17 04:04:55 -07001590 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001591 }
David Brownell045e0e82007-07-17 04:04:55 -07001592
David Brownell1abb0dc2006-06-25 05:48:17 -07001593 tmp = ds1307->regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001594 switch (ds1307->type) {
1595 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001596 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001597 case m41t00:
David Anders40ce9722012-03-23 15:02:37 -07001598 /*
1599 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001600 * systems that will run through year 2100.
1601 */
1602 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001603 case rx_8025:
1604 break;
David Brownellc065f352007-07-17 04:05:10 -07001605 default:
1606 if (!(tmp & DS1307_BIT_12HR))
1607 break;
1608
David Anders40ce9722012-03-23 15:02:37 -07001609 /*
1610 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001611 * take note...
1612 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001613 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001614 if (tmp == 12)
1615 tmp = 0;
1616 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1617 tmp += 12;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001618 regmap_write(ds1307->regmap, ds1307->offset + DS1307_REG_HOUR,
1619 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001620 }
1621
Keerthye29385f2016-06-01 16:19:07 +05301622 /*
1623 * Some IPs have weekday reset value = 0x1 which might not correct
1624 * hence compute the wday using the current date/month/year values
1625 */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001626 ds1307_get_time(ds1307->dev, &tm);
Keerthye29385f2016-06-01 16:19:07 +05301627 wday = tm.tm_wday;
1628 timestamp = rtc_tm_to_time64(&tm);
1629 rtc_time64_to_tm(timestamp, &tm);
1630
1631 /*
1632 * Check if reset wday is different from the computed wday
1633 * If different then set the wday which we computed using
1634 * timestamp
1635 */
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001636 if (wday != tm.tm_wday)
1637 regmap_update_bits(ds1307->regmap, MCP794XX_REG_WEEKDAY,
1638 MCP794XX_REG_WEEKDAY_WDAY_MASK,
1639 tm.tm_wday + 1);
Keerthye29385f2016-06-01 16:19:07 +05301640
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001641 if (want_irq || ds1307_can_wakeup_device) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001642 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001643 set_bit(HAS_ALARM, &ds1307->flags);
1644 }
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001645
1646 ds1307->rtc = devm_rtc_allocate_device(ds1307->dev);
David Brownell1abb0dc2006-06-25 05:48:17 -07001647 if (IS_ERR(ds1307->rtc)) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001648 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001649 }
1650
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001651 if (ds1307_can_wakeup_device && !want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001652 dev_info(ds1307->dev,
1653 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001654 /* We cannot support UIE mode if we do not have an IRQ line */
1655 ds1307->rtc->uie_unsupported = 1;
1656 }
1657
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001658 if (want_irq) {
Heiner Kallweit45947122017-07-12 07:49:41 +02001659 err = devm_request_threaded_irq(ds1307->dev, client->irq, NULL,
1660 chip->irq_handler ?: ds1307_irq,
Nishanth Menonc5983192015-06-23 11:15:11 -05001661 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001662 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001663 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001664 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001665 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001666 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001667 dev_err(ds1307->dev, "unable to request IRQ!\n");
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001668 } else
Heiner Kallweit11e58902017-03-10 18:52:34 +01001669 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001670 }
1671
Austin Boyle9eab0a72012-03-23 15:02:38 -07001672 if (chip->nvram_size) {
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001673 ds1307->nvmem_cfg.name = "ds1307_nvram";
1674 ds1307->nvmem_cfg.word_size = 1;
1675 ds1307->nvmem_cfg.stride = 1;
1676 ds1307->nvmem_cfg.size = chip->nvram_size;
1677 ds1307->nvmem_cfg.reg_read = ds1307_nvram_read;
1678 ds1307->nvmem_cfg.reg_write = ds1307_nvram_write;
1679 ds1307->nvmem_cfg.priv = ds1307;
1680 ds1307->nvram_offset = chip->nvram_offset;
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001681
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001682 ds1307->rtc->nvmem_config = &ds1307->nvmem_cfg;
1683 ds1307->rtc->nvram_old_abi = true;
David Brownell682d73f2007-11-14 16:58:32 -08001684 }
1685
Heiner Kallweit1efb98b2017-07-12 07:49:44 +02001686 ds1307->rtc->ops = chip->rtc_ops ?: &ds13xx_rtc_ops;
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001687 err = rtc_register_device(ds1307->rtc);
1688 if (err)
1689 return err;
1690
Akinobu Mita445c0202016-01-25 00:22:16 +09001691 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001692 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001693
David Brownell1abb0dc2006-06-25 05:48:17 -07001694 return 0;
1695
Jingoo Hanedca66d2013-07-03 15:07:05 -07001696exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001697 return err;
1698}
1699
David Brownell1abb0dc2006-06-25 05:48:17 -07001700static struct i2c_driver ds1307_driver = {
1701 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001702 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001703 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001704 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001705 },
David Brownellc065f352007-07-17 04:05:10 -07001706 .probe = ds1307_probe,
Jean Delvare3760f732008-04-29 23:11:40 +02001707 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001708};
1709
Axel Lin0abc9202012-03-23 15:02:31 -07001710module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001711
1712MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1713MODULE_LICENSE("GPL");