blob: 4021d478b9f362c023c82288f98d451a3525fb3c [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 {
David Brownell1abb0dc2006-06-25 05:48:17 -0700117 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700118 unsigned long flags;
119#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
120#define HAS_ALARM 1 /* bit 1 == irq claimed */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100121 struct device *dev;
122 struct regmap *regmap;
123 const char *name;
David Brownell1abb0dc2006-06-25 05:48:17 -0700124 struct rtc_device *rtc;
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900125#ifdef CONFIG_COMMON_CLK
126 struct clk_hw clks[2];
127#endif
David Brownell1abb0dc2006-06-25 05:48:17 -0700128};
129
David Brownell045e0e82007-07-17 04:04:55 -0700130struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700131 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700132 u16 nvram_offset;
133 u16 nvram_size;
Heiner Kallweite5531702017-07-12 07:49:47 +0200134 u8 offset; /* register's offset */
Heiner Kallweite48585d2017-06-05 17:57:33 +0200135 u8 century_reg;
136 u8 century_enable_bit;
137 u8 century_bit;
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200138 u8 bbsqi_bit;
Heiner Kallweit45947122017-07-12 07:49:41 +0200139 irq_handler_t irq_handler;
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200140 const struct rtc_class_ops *rtc_ops;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700141 u16 trickle_charger_reg;
Alexandre Belloni57ec2d92017-09-04 22:46:04 +0200142 u8 (*do_trickle_setup)(struct ds1307 *, u32,
Heiner Kallweit11e58902017-03-10 18:52:34 +0100143 bool);
David Brownell045e0e82007-07-17 04:04:55 -0700144};
145
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200146static int ds1307_get_time(struct device *dev, struct rtc_time *t);
147static int ds1307_set_time(struct device *dev, struct rtc_time *t);
Alexandre Belloni57ec2d92017-09-04 22:46:04 +0200148static u8 do_trickle_setup_ds1339(struct ds1307 *, u32 ohms, bool diode);
Heiner Kallweit45947122017-07-12 07:49:41 +0200149static irqreturn_t rx8130_irq(int irq, void *dev_id);
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200150static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t);
151static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t);
152static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled);
Heiner Kallweit45947122017-07-12 07:49:41 +0200153static irqreturn_t mcp794xx_irq(int irq, void *dev_id);
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200154static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t);
155static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t);
156static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled);
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700157
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200158static const struct rtc_class_ops rx8130_rtc_ops = {
159 .read_time = ds1307_get_time,
160 .set_time = ds1307_set_time,
161 .read_alarm = rx8130_read_alarm,
162 .set_alarm = rx8130_set_alarm,
163 .alarm_irq_enable = rx8130_alarm_irq_enable,
164};
165
166static const struct rtc_class_ops mcp794xx_rtc_ops = {
167 .read_time = ds1307_get_time,
168 .set_time = ds1307_set_time,
169 .read_alarm = mcp794xx_read_alarm,
170 .set_alarm = mcp794xx_set_alarm,
171 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
172};
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700173
Heiner Kallweit7624df42017-07-12 07:49:33 +0200174static const struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700175 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700176 .nvram_offset = 8,
177 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700178 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200179 [ds_1308] = {
180 .nvram_offset = 8,
181 .nvram_size = 56,
182 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700183 [ds_1337] = {
184 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200185 .century_reg = DS1307_REG_MONTH,
186 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700187 },
188 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700189 .nvram_offset = 8,
190 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700191 },
192 [ds_1339] = {
193 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200194 .century_reg = DS1307_REG_MONTH,
195 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200196 .bbsqi_bit = DS1339_BIT_BBSQI,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700197 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700198 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700199 },
200 [ds_1340] = {
Heiner Kallweite48585d2017-06-05 17:57:33 +0200201 .century_reg = DS1307_REG_HOUR,
202 .century_enable_bit = DS1340_BIT_CENTURY_EN,
203 .century_bit = DS1340_BIT_CENTURY,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700204 .trickle_charger_reg = 0x08,
205 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300206 [ds_1341] = {
207 .century_reg = DS1307_REG_MONTH,
208 .century_bit = DS1337_BIT_CENTURY,
209 },
Wolfram Sangeb86c302012-05-29 15:07:38 -0700210 [ds_1388] = {
Heiner Kallweite5531702017-07-12 07:49:47 +0200211 .offset = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700212 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700213 },
214 [ds_3231] = {
215 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200216 .century_reg = DS1307_REG_MONTH,
217 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200218 .bbsqi_bit = DS3231_BIT_BBSQW,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700219 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200220 [rx_8130] = {
221 .alarm = 1,
222 /* this is battery backed SRAM */
223 .nvram_offset = 0x20,
224 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
Heiner Kallweite5531702017-07-12 07:49:47 +0200225 .offset = 0x10,
Heiner Kallweit45947122017-07-12 07:49:41 +0200226 .irq_handler = rx8130_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200227 .rtc_ops = &rx8130_rtc_ops,
Marek Vasutee0981b2017-06-18 22:55:28 +0200228 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800229 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700230 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700231 /* this is battery backed SRAM */
232 .nvram_offset = 0x20,
233 .nvram_size = 0x40,
Heiner Kallweit45947122017-07-12 07:49:41 +0200234 .irq_handler = mcp794xx_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200235 .rtc_ops = &mcp794xx_rtc_ops,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700236 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700237};
David Brownell045e0e82007-07-17 04:04:55 -0700238
Jean Delvare3760f732008-04-29 23:11:40 +0200239static const struct i2c_device_id ds1307_id[] = {
240 { "ds1307", ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200241 { "ds1308", ds_1308 },
Jean Delvare3760f732008-04-29 23:11:40 +0200242 { "ds1337", ds_1337 },
243 { "ds1338", ds_1338 },
244 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700245 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200246 { "ds1340", ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300247 { "ds1341", ds_1341 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700248 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700249 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200250 { "m41t00", m41t00 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800251 { "mcp7940x", mcp794xx },
252 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700253 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700254 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200255 { "isl12057", ds_1337 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200256 { "rx8130", rx_8130 },
Jean Delvare3760f732008-04-29 23:11:40 +0200257 { }
258};
259MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700260
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300261#ifdef CONFIG_OF
262static const struct of_device_id ds1307_of_match[] = {
263 {
264 .compatible = "dallas,ds1307",
265 .data = (void *)ds_1307
266 },
267 {
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200268 .compatible = "dallas,ds1308",
269 .data = (void *)ds_1308
270 },
271 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300272 .compatible = "dallas,ds1337",
273 .data = (void *)ds_1337
274 },
275 {
276 .compatible = "dallas,ds1338",
277 .data = (void *)ds_1338
278 },
279 {
280 .compatible = "dallas,ds1339",
281 .data = (void *)ds_1339
282 },
283 {
284 .compatible = "dallas,ds1388",
285 .data = (void *)ds_1388
286 },
287 {
288 .compatible = "dallas,ds1340",
289 .data = (void *)ds_1340
290 },
291 {
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300292 .compatible = "dallas,ds1341",
293 .data = (void *)ds_1341
294 },
295 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300296 .compatible = "maxim,ds3231",
297 .data = (void *)ds_3231
298 },
299 {
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200300 .compatible = "st,m41t0",
301 .data = (void *)m41t00
302 },
303 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300304 .compatible = "st,m41t00",
305 .data = (void *)m41t00
306 },
307 {
308 .compatible = "microchip,mcp7940x",
309 .data = (void *)mcp794xx
310 },
311 {
312 .compatible = "microchip,mcp7941x",
313 .data = (void *)mcp794xx
314 },
315 {
316 .compatible = "pericom,pt7c4338",
317 .data = (void *)ds_1307
318 },
319 {
320 .compatible = "epson,rx8025",
321 .data = (void *)rx_8025
322 },
323 {
324 .compatible = "isil,isl12057",
325 .data = (void *)ds_1337
326 },
Bastian Stender47dd4722017-10-17 14:46:07 +0200327 {
328 .compatible = "epson,rx8130",
329 .data = (void *)rx_8130
330 },
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300331 { }
332};
333MODULE_DEVICE_TABLE(of, ds1307_of_match);
334#endif
335
Tin Huynh9c19b892016-11-30 09:57:31 +0700336#ifdef CONFIG_ACPI
337static const struct acpi_device_id ds1307_acpi_ids[] = {
338 { .id = "DS1307", .driver_data = ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200339 { .id = "DS1308", .driver_data = ds_1308 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700340 { .id = "DS1337", .driver_data = ds_1337 },
341 { .id = "DS1338", .driver_data = ds_1338 },
342 { .id = "DS1339", .driver_data = ds_1339 },
343 { .id = "DS1388", .driver_data = ds_1388 },
344 { .id = "DS1340", .driver_data = ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300345 { .id = "DS1341", .driver_data = ds_1341 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700346 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700347 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700348 { .id = "M41T00", .driver_data = m41t00 },
349 { .id = "MCP7940X", .driver_data = mcp794xx },
350 { .id = "MCP7941X", .driver_data = mcp794xx },
351 { .id = "PT7C4338", .driver_data = ds_1307 },
352 { .id = "RX8025", .driver_data = rx_8025 },
353 { .id = "ISL12057", .driver_data = ds_1337 },
Bastian Stender47dd4722017-10-17 14:46:07 +0200354 { .id = "RX8130", .driver_data = rx_8130 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700355 { }
356};
357MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
358#endif
359
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700360/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700361 * The ds1337 and ds1339 both have two alarms, but we only use the first
362 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
363 * signal; ds1339 chips have only one alarm signal.
364 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500365static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700366{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100367 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500368 struct mutex *lock = &ds1307->rtc->ops_lock;
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200369 int stat, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700370
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700371 mutex_lock(lock);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100372 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
373 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700374 goto out;
375
376 if (stat & DS1337_BIT_A1I) {
377 stat &= ~DS1337_BIT_A1I;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100378 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700379
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200380 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
381 DS1337_BIT_A1IE, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100382 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700383 goto out;
384
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700385 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700386 }
387
388out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700389 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700390
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700391 return IRQ_HANDLED;
392}
393
394/*----------------------------------------------------------------------*/
395
David Brownell1abb0dc2006-06-25 05:48:17 -0700396static int ds1307_get_time(struct device *dev, struct rtc_time *t)
397{
398 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100399 int tmp, ret;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200400 const struct chip_desc *chip = &chips[ds1307->type];
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200401 u8 regs[7];
David Brownell1abb0dc2006-06-25 05:48:17 -0700402
David Brownell045e0e82007-07-17 04:04:55 -0700403 /* read the RTC date and time registers all at once */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200404 ret = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
405 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100406 if (ret) {
407 dev_err(dev, "%s error %d\n", "read", ret);
408 return ret;
David Brownell1abb0dc2006-06-25 05:48:17 -0700409 }
410
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200411 dev_dbg(dev, "%s: %7ph\n", "read", regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700412
Stefan Agner8566f702017-03-23 16:54:57 -0700413 /* if oscillator fail bit is set, no data can be trusted */
414 if (ds1307->type == m41t0 &&
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200415 regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
Stefan Agner8566f702017-03-23 16:54:57 -0700416 dev_warn_once(dev, "oscillator failed, set time!\n");
417 return -EINVAL;
418 }
419
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200420 t->tm_sec = bcd2bin(regs[DS1307_REG_SECS] & 0x7f);
421 t->tm_min = bcd2bin(regs[DS1307_REG_MIN] & 0x7f);
422 tmp = regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700423 t->tm_hour = bcd2bin(tmp);
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200424 t->tm_wday = bcd2bin(regs[DS1307_REG_WDAY] & 0x07) - 1;
425 t->tm_mday = bcd2bin(regs[DS1307_REG_MDAY] & 0x3f);
426 tmp = regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700427 t->tm_mon = bcd2bin(tmp) - 1;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200428 t->tm_year = bcd2bin(regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700429
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200430 if (regs[chip->century_reg] & chip->century_bit &&
Heiner Kallweite48585d2017-06-05 17:57:33 +0200431 IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY))
432 t->tm_year += 100;
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200433
David Brownell1abb0dc2006-06-25 05:48:17 -0700434 dev_dbg(dev, "%s secs=%d, mins=%d, "
435 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
436 "read", t->tm_sec, t->tm_min,
437 t->tm_hour, t->tm_mday,
438 t->tm_mon, t->tm_year, t->tm_wday);
439
David Brownell045e0e82007-07-17 04:04:55 -0700440 /* initial clock setting can be undefined */
441 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700442}
443
444static int ds1307_set_time(struct device *dev, struct rtc_time *t)
445{
446 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweite48585d2017-06-05 17:57:33 +0200447 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell1abb0dc2006-06-25 05:48:17 -0700448 int result;
449 int tmp;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200450 u8 regs[7];
David Brownell1abb0dc2006-06-25 05:48:17 -0700451
452 dev_dbg(dev, "%s secs=%d, mins=%d, "
453 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400454 "write", t->tm_sec, t->tm_min,
455 t->tm_hour, t->tm_mday,
456 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700457
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200458 if (t->tm_year < 100)
459 return -EINVAL;
460
Heiner Kallweite48585d2017-06-05 17:57:33 +0200461#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
462 if (t->tm_year > (chip->century_bit ? 299 : 199))
463 return -EINVAL;
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200464#else
Heiner Kallweite48585d2017-06-05 17:57:33 +0200465 if (t->tm_year > 199)
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200466 return -EINVAL;
467#endif
468
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200469 regs[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
470 regs[DS1307_REG_MIN] = bin2bcd(t->tm_min);
471 regs[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
472 regs[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
473 regs[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
474 regs[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700475
476 /* assume 20YY not 19YY */
477 tmp = t->tm_year - 100;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200478 regs[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700479
Heiner Kallweite48585d2017-06-05 17:57:33 +0200480 if (chip->century_enable_bit)
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200481 regs[chip->century_reg] |= chip->century_enable_bit;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200482 if (t->tm_year > 199 && chip->century_bit)
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200483 regs[chip->century_reg] |= chip->century_bit;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200484
485 if (ds1307->type == mcp794xx) {
David Anders40ce9722012-03-23 15:02:37 -0700486 /*
487 * these bits were cleared when preparing the date/time
488 * values and need to be set again before writing the
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200489 * regsfer out to the device.
David Anders40ce9722012-03-23 15:02:37 -0700490 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200491 regs[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
492 regs[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700493 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700494
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200495 dev_dbg(dev, "%s: %7ph\n", "write", regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700496
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200497 result = regmap_bulk_write(ds1307->regmap, chip->offset, regs,
498 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100499 if (result) {
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800500 dev_err(dev, "%s error %d\n", "write", result);
501 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700502 }
503 return 0;
504}
505
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800506static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700507{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100508 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700509 int ret;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200510 u8 regs[9];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700511
512 if (!test_bit(HAS_ALARM, &ds1307->flags))
513 return -EINVAL;
514
515 /* read all ALARM1, ALARM2, and status registers at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100516 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200517 regs, sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100518 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700519 dev_err(dev, "%s error %d\n", "alarm read", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100520 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700521 }
522
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100523 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200524 &regs[0], &regs[4], &regs[7]);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700525
David Anders40ce9722012-03-23 15:02:37 -0700526 /*
527 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700528 * and that all four fields are checked matches
529 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200530 t->time.tm_sec = bcd2bin(regs[0] & 0x7f);
531 t->time.tm_min = bcd2bin(regs[1] & 0x7f);
532 t->time.tm_hour = bcd2bin(regs[2] & 0x3f);
533 t->time.tm_mday = bcd2bin(regs[3] & 0x3f);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700534
535 /* ... and status */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200536 t->enabled = !!(regs[7] & DS1337_BIT_A1IE);
537 t->pending = !!(regs[8] & DS1337_BIT_A1I);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700538
539 dev_dbg(dev, "%s secs=%d, mins=%d, "
540 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
541 "alarm read", t->time.tm_sec, t->time.tm_min,
542 t->time.tm_hour, t->time.tm_mday,
543 t->enabled, t->pending);
544
545 return 0;
546}
547
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800548static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700549{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100550 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200551 unsigned char regs[9];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700552 u8 control, status;
553 int ret;
554
555 if (!test_bit(HAS_ALARM, &ds1307->flags))
556 return -EINVAL;
557
558 dev_dbg(dev, "%s secs=%d, mins=%d, "
559 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
560 "alarm set", t->time.tm_sec, t->time.tm_min,
561 t->time.tm_hour, t->time.tm_mday,
562 t->enabled, t->pending);
563
564 /* read current status of both alarms and the chip */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200565 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
566 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100567 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700568 dev_err(dev, "%s error %d\n", "alarm write", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100569 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700570 }
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200571 control = regs[7];
572 status = regs[8];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700573
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100574 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200575 &regs[0], &regs[4], control, status);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700576
577 /* set ALARM1, using 24 hour and day-of-month modes */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200578 regs[0] = bin2bcd(t->time.tm_sec);
579 regs[1] = bin2bcd(t->time.tm_min);
580 regs[2] = bin2bcd(t->time.tm_hour);
581 regs[3] = bin2bcd(t->time.tm_mday);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700582
583 /* set ALARM2 to non-garbage */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200584 regs[4] = 0;
585 regs[5] = 0;
586 regs[6] = 0;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700587
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200588 /* disable alarms */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200589 regs[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
590 regs[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700591
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200592 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
593 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100594 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700595 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800596 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700597 }
598
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200599 /* optionally enable ALARM1 */
600 if (t->enabled) {
601 dev_dbg(dev, "alarm IRQ armed\n");
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200602 regs[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
603 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, regs[7]);
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200604 }
605
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700606 return 0;
607}
608
John Stultz16380c12011-02-02 17:02:41 -0800609static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700610{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100611 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700612
John Stultz16380c12011-02-02 17:02:41 -0800613 if (!test_bit(HAS_ALARM, &ds1307->flags))
614 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700615
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200616 return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
617 DS1337_BIT_A1IE,
618 enabled ? DS1337_BIT_A1IE : 0);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700619}
620
David Brownellff8371a2006-09-30 23:28:17 -0700621static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700622 .read_time = ds1307_get_time,
623 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800624 .read_alarm = ds1337_read_alarm,
625 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800626 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700627};
628
David Brownell682d73f2007-11-14 16:58:32 -0800629/*----------------------------------------------------------------------*/
630
Simon Guinot1d1945d2014-04-03 14:49:55 -0700631/*
Marek Vasutee0981b2017-06-18 22:55:28 +0200632 * Alarm support for rx8130 devices.
633 */
634
635#define RX8130_REG_ALARM_MIN 0x07
636#define RX8130_REG_ALARM_HOUR 0x08
637#define RX8130_REG_ALARM_WEEK_OR_DAY 0x09
638#define RX8130_REG_EXTENSION 0x0c
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200639#define RX8130_REG_EXTENSION_WADA BIT(3)
Marek Vasutee0981b2017-06-18 22:55:28 +0200640#define RX8130_REG_FLAG 0x0d
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200641#define RX8130_REG_FLAG_AF BIT(3)
Marek Vasutee0981b2017-06-18 22:55:28 +0200642#define RX8130_REG_CONTROL0 0x0e
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200643#define RX8130_REG_CONTROL0_AIE BIT(3)
Marek Vasutee0981b2017-06-18 22:55:28 +0200644
645static irqreturn_t rx8130_irq(int irq, void *dev_id)
646{
647 struct ds1307 *ds1307 = dev_id;
648 struct mutex *lock = &ds1307->rtc->ops_lock;
649 u8 ctl[3];
650 int ret;
651
652 mutex_lock(lock);
653
654 /* Read control registers. */
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200655 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
656 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200657 if (ret < 0)
658 goto out;
659 if (!(ctl[1] & RX8130_REG_FLAG_AF))
660 goto out;
661 ctl[1] &= ~RX8130_REG_FLAG_AF;
662 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
663
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200664 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
665 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200666 if (ret < 0)
667 goto out;
668
669 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
670
671out:
672 mutex_unlock(lock);
673
674 return IRQ_HANDLED;
675}
676
677static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
678{
679 struct ds1307 *ds1307 = dev_get_drvdata(dev);
680 u8 ald[3], ctl[3];
681 int ret;
682
683 if (!test_bit(HAS_ALARM, &ds1307->flags))
684 return -EINVAL;
685
686 /* Read alarm registers. */
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200687 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
688 sizeof(ald));
Marek Vasutee0981b2017-06-18 22:55:28 +0200689 if (ret < 0)
690 return ret;
691
692 /* Read control registers. */
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200693 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
694 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200695 if (ret < 0)
696 return ret;
697
698 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
699 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
700
701 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
702 t->time.tm_sec = -1;
703 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
704 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
705 t->time.tm_wday = -1;
706 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
707 t->time.tm_mon = -1;
708 t->time.tm_year = -1;
709 t->time.tm_yday = -1;
710 t->time.tm_isdst = -1;
711
712 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
713 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
714 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
715
716 return 0;
717}
718
719static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
720{
721 struct ds1307 *ds1307 = dev_get_drvdata(dev);
722 u8 ald[3], ctl[3];
723 int ret;
724
725 if (!test_bit(HAS_ALARM, &ds1307->flags))
726 return -EINVAL;
727
728 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
729 "enabled=%d pending=%d\n", __func__,
730 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
731 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
732 t->enabled, t->pending);
733
734 /* Read control registers. */
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200735 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
736 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200737 if (ret < 0)
738 return ret;
739
740 ctl[0] &= ~RX8130_REG_EXTENSION_WADA;
741 ctl[1] |= RX8130_REG_FLAG_AF;
742 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
743
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200744 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
745 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200746 if (ret < 0)
747 return ret;
748
749 /* Hardware alarm precision is 1 minute! */
750 ald[0] = bin2bcd(t->time.tm_min);
751 ald[1] = bin2bcd(t->time.tm_hour);
752 ald[2] = bin2bcd(t->time.tm_mday);
753
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200754 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
755 sizeof(ald));
Marek Vasutee0981b2017-06-18 22:55:28 +0200756 if (ret < 0)
757 return ret;
758
759 if (!t->enabled)
760 return 0;
761
762 ctl[2] |= RX8130_REG_CONTROL0_AIE;
763
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200764 return regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
765 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200766}
767
768static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
769{
770 struct ds1307 *ds1307 = dev_get_drvdata(dev);
771 int ret, reg;
772
773 if (!test_bit(HAS_ALARM, &ds1307->flags))
774 return -EINVAL;
775
776 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
777 if (ret < 0)
778 return ret;
779
780 if (enabled)
781 reg |= RX8130_REG_CONTROL0_AIE;
782 else
783 reg &= ~RX8130_REG_CONTROL0_AIE;
784
785 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
786}
787
Marek Vasutee0981b2017-06-18 22:55:28 +0200788/*----------------------------------------------------------------------*/
789
790/*
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800791 * Alarm support for mcp794xx devices.
Simon Guinot1d1945d2014-04-03 14:49:55 -0700792 */
793
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800794#define MCP794XX_REG_CONTROL 0x07
795# define MCP794XX_BIT_ALM0_EN 0x10
796# define MCP794XX_BIT_ALM1_EN 0x20
797#define MCP794XX_REG_ALARM0_BASE 0x0a
798#define MCP794XX_REG_ALARM0_CTRL 0x0d
799#define MCP794XX_REG_ALARM1_BASE 0x11
800#define MCP794XX_REG_ALARM1_CTRL 0x14
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200801# define MCP794XX_BIT_ALMX_IF BIT(3)
802# define MCP794XX_BIT_ALMX_C0 BIT(4)
803# define MCP794XX_BIT_ALMX_C1 BIT(5)
804# define MCP794XX_BIT_ALMX_C2 BIT(6)
805# define MCP794XX_BIT_ALMX_POL BIT(7)
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800806# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
807 MCP794XX_BIT_ALMX_C1 | \
808 MCP794XX_BIT_ALMX_C2)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700809
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500810static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700811{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100812 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500813 struct mutex *lock = &ds1307->rtc->ops_lock;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700814 int reg, ret;
815
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500816 mutex_lock(lock);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700817
818 /* Check and clear alarm 0 interrupt flag. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100819 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
820 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700821 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800822 if (!(reg & MCP794XX_BIT_ALMX_IF))
Simon Guinot1d1945d2014-04-03 14:49:55 -0700823 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800824 reg &= ~MCP794XX_BIT_ALMX_IF;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100825 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
826 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700827 goto out;
828
829 /* Disable alarm 0. */
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200830 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
831 MCP794XX_BIT_ALM0_EN, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100832 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700833 goto out;
834
835 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
836
837out:
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500838 mutex_unlock(lock);
839
840 return IRQ_HANDLED;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700841}
842
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800843static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700844{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100845 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200846 u8 regs[10];
Simon Guinot1d1945d2014-04-03 14:49:55 -0700847 int ret;
848
849 if (!test_bit(HAS_ALARM, &ds1307->flags))
850 return -EINVAL;
851
852 /* Read control and alarm 0 registers. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200853 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
854 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100855 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700856 return ret;
857
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800858 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700859
860 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200861 t->time.tm_sec = bcd2bin(regs[3] & 0x7f);
862 t->time.tm_min = bcd2bin(regs[4] & 0x7f);
863 t->time.tm_hour = bcd2bin(regs[5] & 0x3f);
864 t->time.tm_wday = bcd2bin(regs[6] & 0x7) - 1;
865 t->time.tm_mday = bcd2bin(regs[7] & 0x3f);
866 t->time.tm_mon = bcd2bin(regs[8] & 0x1f) - 1;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700867 t->time.tm_year = -1;
868 t->time.tm_yday = -1;
869 t->time.tm_isdst = -1;
870
871 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200872 "enabled=%d polarity=%d irq=%d match=%lu\n", __func__,
Simon Guinot1d1945d2014-04-03 14:49:55 -0700873 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
874 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200875 !!(regs[6] & MCP794XX_BIT_ALMX_POL),
876 !!(regs[6] & MCP794XX_BIT_ALMX_IF),
877 (regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700878
879 return 0;
880}
881
Heiner Kallweit584ce302017-08-29 21:52:56 +0200882/*
883 * We may have a random RTC weekday, therefore calculate alarm weekday based
884 * on current weekday we read from the RTC timekeeping regs
885 */
886static int mcp794xx_alm_weekday(struct device *dev, struct rtc_time *tm_alarm)
887{
888 struct rtc_time tm_now;
889 int days_now, days_alarm, ret;
890
891 ret = ds1307_get_time(dev, &tm_now);
892 if (ret)
893 return ret;
894
895 days_now = div_s64(rtc_tm_to_time64(&tm_now), 24 * 60 * 60);
896 days_alarm = div_s64(rtc_tm_to_time64(tm_alarm), 24 * 60 * 60);
897
898 return (tm_now.tm_wday + days_alarm - days_now) % 7 + 1;
899}
900
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800901static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700902{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100903 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200904 unsigned char regs[10];
Heiner Kallweit584ce302017-08-29 21:52:56 +0200905 int wday, ret;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700906
907 if (!test_bit(HAS_ALARM, &ds1307->flags))
908 return -EINVAL;
909
Heiner Kallweit584ce302017-08-29 21:52:56 +0200910 wday = mcp794xx_alm_weekday(dev, &t->time);
911 if (wday < 0)
912 return wday;
913
Simon Guinot1d1945d2014-04-03 14:49:55 -0700914 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
915 "enabled=%d pending=%d\n", __func__,
916 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
917 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
918 t->enabled, t->pending);
919
920 /* Read control and alarm 0 registers. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200921 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
922 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100923 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700924 return ret;
925
926 /* Set alarm 0, using 24-hour and day-of-month modes. */
927 regs[3] = bin2bcd(t->time.tm_sec);
928 regs[4] = bin2bcd(t->time.tm_min);
929 regs[5] = bin2bcd(t->time.tm_hour);
Heiner Kallweit584ce302017-08-29 21:52:56 +0200930 regs[6] = wday;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700931 regs[7] = bin2bcd(t->time.tm_mday);
Tero Kristo62c8c202015-10-23 09:29:57 +0300932 regs[8] = bin2bcd(t->time.tm_mon + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700933
934 /* Clear the alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800935 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700936 /* Set alarm match: second, minute, hour, day, date, month. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800937 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
Nishanth Menone3edd672015-04-20 19:51:34 -0500938 /* Disable interrupt. We will not enable until completely programmed */
939 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700940
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200941 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
942 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100943 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700944 return ret;
945
Nishanth Menone3edd672015-04-20 19:51:34 -0500946 if (!t->enabled)
947 return 0;
948 regs[0] |= MCP794XX_BIT_ALM0_EN;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100949 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700950}
951
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800952static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700953{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100954 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700955
956 if (!test_bit(HAS_ALARM, &ds1307->flags))
957 return -EINVAL;
958
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200959 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
960 MCP794XX_BIT_ALM0_EN,
961 enabled ? MCP794XX_BIT_ALM0_EN : 0);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700962}
963
Simon Guinot1d1945d2014-04-03 14:49:55 -0700964/*----------------------------------------------------------------------*/
965
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200966static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
967 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -0800968{
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200969 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +0200970 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -0800971
Heiner Kallweit969fa072017-07-12 07:49:54 +0200972 return regmap_bulk_read(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200973 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -0800974}
975
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200976static int ds1307_nvram_write(void *priv, unsigned int offset, void *val,
977 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -0800978{
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200979 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +0200980 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -0800981
Heiner Kallweit969fa072017-07-12 07:49:54 +0200982 return regmap_bulk_write(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200983 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -0800984}
985
David Brownell682d73f2007-11-14 16:58:32 -0800986/*----------------------------------------------------------------------*/
987
Heiner Kallweit11e58902017-03-10 18:52:34 +0100988static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307,
Alexandre Belloni57ec2d92017-09-04 22:46:04 +0200989 u32 ohms, bool diode)
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700990{
991 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
992 DS1307_TRICKLE_CHARGER_NO_DIODE;
993
994 switch (ohms) {
995 case 250:
996 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
997 break;
998 case 2000:
999 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
1000 break;
1001 case 4000:
1002 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
1003 break;
1004 default:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001005 dev_warn(ds1307->dev,
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001006 "Unsupported ohm value %u in dt\n", ohms);
1007 return 0;
1008 }
1009 return setup;
1010}
1011
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001012static u8 ds1307_trickle_init(struct ds1307 *ds1307,
Heiner Kallweit7624df42017-07-12 07:49:33 +02001013 const struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001014{
Alexandre Belloni57ec2d92017-09-04 22:46:04 +02001015 u32 ohms;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001016 bool diode = true;
1017
1018 if (!chip->do_trickle_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001019 return 0;
1020
Heiner Kallweit11e58902017-03-10 18:52:34 +01001021 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
1022 &ohms))
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001023 return 0;
1024
Heiner Kallweit11e58902017-03-10 18:52:34 +01001025 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001026 diode = false;
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001027
1028 return chip->do_trickle_setup(ds1307, ohms, diode);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001029}
1030
Akinobu Mita445c0202016-01-25 00:22:16 +09001031/*----------------------------------------------------------------------*/
1032
1033#ifdef CONFIG_RTC_DRV_DS1307_HWMON
1034
1035/*
1036 * Temperature sensor support for ds3231 devices.
1037 */
1038
1039#define DS3231_REG_TEMPERATURE 0x11
1040
1041/*
1042 * A user-initiated temperature conversion is not started by this function,
1043 * so the temperature is updated once every 64 seconds.
1044 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001045static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +09001046{
1047 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1048 u8 temp_buf[2];
1049 s16 temp;
1050 int ret;
1051
Heiner Kallweit11e58902017-03-10 18:52:34 +01001052 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
1053 temp_buf, sizeof(temp_buf));
1054 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +09001055 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +09001056 /*
1057 * Temperature is represented as a 10-bit code with a resolution of
1058 * 0.25 degree celsius and encoded in two's complement format.
1059 */
1060 temp = (temp_buf[0] << 8) | temp_buf[1];
1061 temp >>= 6;
1062 *mC = temp * 250;
1063
1064 return 0;
1065}
1066
1067static ssize_t ds3231_hwmon_show_temp(struct device *dev,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001068 struct device_attribute *attr, char *buf)
Akinobu Mita445c0202016-01-25 00:22:16 +09001069{
1070 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001071 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001072
1073 ret = ds3231_hwmon_read_temp(dev, &temp);
1074 if (ret)
1075 return ret;
1076
1077 return sprintf(buf, "%d\n", temp);
1078}
Alexandre Bellonib4be2712017-09-04 22:46:08 +02001079static SENSOR_DEVICE_ATTR(temp1_input, 0444, ds3231_hwmon_show_temp,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001080 NULL, 0);
Akinobu Mita445c0202016-01-25 00:22:16 +09001081
1082static struct attribute *ds3231_hwmon_attrs[] = {
1083 &sensor_dev_attr_temp1_input.dev_attr.attr,
1084 NULL,
1085};
1086ATTRIBUTE_GROUPS(ds3231_hwmon);
1087
1088static void ds1307_hwmon_register(struct ds1307 *ds1307)
1089{
1090 struct device *dev;
1091
1092 if (ds1307->type != ds_3231)
1093 return;
1094
Heiner Kallweit11e58902017-03-10 18:52:34 +01001095 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001096 ds1307,
1097 ds3231_hwmon_groups);
Akinobu Mita445c0202016-01-25 00:22:16 +09001098 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001099 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1100 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001101 }
1102}
1103
1104#else
1105
1106static void ds1307_hwmon_register(struct ds1307 *ds1307)
1107{
1108}
1109
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001110#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1111
1112/*----------------------------------------------------------------------*/
1113
1114/*
1115 * Square-wave output support for DS3231
1116 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1117 */
1118#ifdef CONFIG_COMMON_CLK
1119
1120enum {
1121 DS3231_CLK_SQW = 0,
1122 DS3231_CLK_32KHZ,
1123};
1124
1125#define clk_sqw_to_ds1307(clk) \
1126 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1127#define clk_32khz_to_ds1307(clk) \
1128 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1129
1130static int ds3231_clk_sqw_rates[] = {
1131 1,
1132 1024,
1133 4096,
1134 8192,
1135};
1136
1137static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1138{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001139 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001140 int ret;
1141
1142 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001143 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1144 mask, value);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001145 mutex_unlock(lock);
1146
1147 return ret;
1148}
1149
1150static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1151 unsigned long parent_rate)
1152{
1153 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001154 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001155 int rate_sel = 0;
1156
Heiner Kallweit11e58902017-03-10 18:52:34 +01001157 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1158 if (ret)
1159 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001160 if (control & DS1337_BIT_RS1)
1161 rate_sel += 1;
1162 if (control & DS1337_BIT_RS2)
1163 rate_sel += 2;
1164
1165 return ds3231_clk_sqw_rates[rate_sel];
1166}
1167
1168static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001169 unsigned long *prate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001170{
1171 int i;
1172
1173 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1174 if (ds3231_clk_sqw_rates[i] <= rate)
1175 return ds3231_clk_sqw_rates[i];
1176 }
1177
1178 return 0;
1179}
1180
1181static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001182 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001183{
1184 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1185 int control = 0;
1186 int rate_sel;
1187
1188 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1189 rate_sel++) {
1190 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1191 break;
1192 }
1193
1194 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1195 return -EINVAL;
1196
1197 if (rate_sel & 1)
1198 control |= DS1337_BIT_RS1;
1199 if (rate_sel & 2)
1200 control |= DS1337_BIT_RS2;
1201
1202 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1203 control);
1204}
1205
1206static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1207{
1208 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1209
1210 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1211}
1212
1213static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1214{
1215 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1216
1217 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1218}
1219
1220static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1221{
1222 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001223 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001224
Heiner Kallweit11e58902017-03-10 18:52:34 +01001225 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1226 if (ret)
1227 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001228
1229 return !(control & DS1337_BIT_INTCN);
1230}
1231
1232static const struct clk_ops ds3231_clk_sqw_ops = {
1233 .prepare = ds3231_clk_sqw_prepare,
1234 .unprepare = ds3231_clk_sqw_unprepare,
1235 .is_prepared = ds3231_clk_sqw_is_prepared,
1236 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1237 .round_rate = ds3231_clk_sqw_round_rate,
1238 .set_rate = ds3231_clk_sqw_set_rate,
1239};
1240
1241static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001242 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001243{
1244 return 32768;
1245}
1246
1247static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1248{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001249 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001250 int ret;
1251
1252 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001253 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1254 DS3231_BIT_EN32KHZ,
1255 enable ? DS3231_BIT_EN32KHZ : 0);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001256 mutex_unlock(lock);
1257
1258 return ret;
1259}
1260
1261static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1262{
1263 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1264
1265 return ds3231_clk_32khz_control(ds1307, true);
1266}
1267
1268static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1269{
1270 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1271
1272 ds3231_clk_32khz_control(ds1307, false);
1273}
1274
1275static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1276{
1277 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001278 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001279
Heiner Kallweit11e58902017-03-10 18:52:34 +01001280 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1281 if (ret)
1282 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001283
1284 return !!(status & DS3231_BIT_EN32KHZ);
1285}
1286
1287static const struct clk_ops ds3231_clk_32khz_ops = {
1288 .prepare = ds3231_clk_32khz_prepare,
1289 .unprepare = ds3231_clk_32khz_unprepare,
1290 .is_prepared = ds3231_clk_32khz_is_prepared,
1291 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1292};
1293
1294static struct clk_init_data ds3231_clks_init[] = {
1295 [DS3231_CLK_SQW] = {
1296 .name = "ds3231_clk_sqw",
1297 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001298 },
1299 [DS3231_CLK_32KHZ] = {
1300 .name = "ds3231_clk_32khz",
1301 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001302 },
1303};
1304
1305static int ds3231_clks_register(struct ds1307 *ds1307)
1306{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001307 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001308 struct clk_onecell_data *onecell;
1309 int i;
1310
Heiner Kallweit11e58902017-03-10 18:52:34 +01001311 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001312 if (!onecell)
1313 return -ENOMEM;
1314
1315 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001316 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1317 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001318 if (!onecell->clks)
1319 return -ENOMEM;
1320
1321 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1322 struct clk_init_data init = ds3231_clks_init[i];
1323
1324 /*
1325 * Interrupt signal due to alarm conditions and square-wave
1326 * output share same pin, so don't initialize both.
1327 */
1328 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1329 continue;
1330
1331 /* optional override of the clockname */
1332 of_property_read_string_index(node, "clock-output-names", i,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001333 &init.name);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001334 ds1307->clks[i].init = &init;
1335
Heiner Kallweit11e58902017-03-10 18:52:34 +01001336 onecell->clks[i] = devm_clk_register(ds1307->dev,
1337 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001338 if (IS_ERR(onecell->clks[i]))
1339 return PTR_ERR(onecell->clks[i]);
1340 }
1341
1342 if (!node)
1343 return 0;
1344
1345 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1346
1347 return 0;
1348}
1349
1350static void ds1307_clks_register(struct ds1307 *ds1307)
1351{
1352 int ret;
1353
1354 if (ds1307->type != ds_3231)
1355 return;
1356
1357 ret = ds3231_clks_register(ds1307);
1358 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001359 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1360 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001361 }
1362}
1363
1364#else
1365
1366static void ds1307_clks_register(struct ds1307 *ds1307)
1367{
1368}
1369
1370#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001371
Heiner Kallweit11e58902017-03-10 18:52:34 +01001372static const struct regmap_config regmap_config = {
1373 .reg_bits = 8,
1374 .val_bits = 8,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001375};
1376
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001377static int ds1307_probe(struct i2c_client *client,
1378 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001379{
1380 struct ds1307 *ds1307;
1381 int err = -ENODEV;
Heiner Kallweit584ce302017-08-29 21:52:56 +02001382 int tmp;
Heiner Kallweit7624df42017-07-12 07:49:33 +02001383 const struct chip_desc *chip;
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001384 bool want_irq;
Michael Lange8bc2a402016-01-21 18:10:16 +01001385 bool ds1307_can_wakeup_device = false;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001386 unsigned char regs[8];
Jingoo Han01ce8932013-11-12 15:10:41 -08001387 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001388 u8 trickle_charger_setup = 0;
David Brownell1abb0dc2006-06-25 05:48:17 -07001389
Jingoo Hanedca66d2013-07-03 15:07:05 -07001390 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001391 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001392 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001393
Heiner Kallweit11e58902017-03-10 18:52:34 +01001394 dev_set_drvdata(&client->dev, ds1307);
1395 ds1307->dev = &client->dev;
1396 ds1307->name = client->name;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001397
Heiner Kallweit11e58902017-03-10 18:52:34 +01001398 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1399 if (IS_ERR(ds1307->regmap)) {
1400 dev_err(ds1307->dev, "regmap allocation failed\n");
1401 return PTR_ERR(ds1307->regmap);
1402 }
1403
1404 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001405
1406 if (client->dev.of_node) {
1407 ds1307->type = (enum ds_type)
1408 of_device_get_match_data(&client->dev);
1409 chip = &chips[ds1307->type];
1410 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001411 chip = &chips[id->driver_data];
1412 ds1307->type = id->driver_data;
1413 } else {
1414 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001415
Tin Huynh9c19b892016-11-30 09:57:31 +07001416 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001417 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001418 if (!acpi_id)
1419 return -ENODEV;
1420 chip = &chips[acpi_id->driver_data];
1421 ds1307->type = acpi_id->driver_data;
1422 }
1423
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001424 want_irq = client->irq > 0 && chip->alarm;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001425
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001426 if (!pdata)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001427 trickle_charger_setup = ds1307_trickle_init(ds1307, chip);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001428 else if (pdata->trickle_charger_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001429 trickle_charger_setup = pdata->trickle_charger_setup;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001430
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001431 if (trickle_charger_setup && chip->trickle_charger_reg) {
1432 trickle_charger_setup |= DS13XX_TRICKLE_CHARGER_MAGIC;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001433 dev_dbg(ds1307->dev,
1434 "writing trickle charger info 0x%x to 0x%x\n",
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001435 trickle_charger_setup, chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001436 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001437 trickle_charger_setup);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001438 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001439
Michael Lange8bc2a402016-01-21 18:10:16 +01001440#ifdef CONFIG_OF
1441/*
1442 * For devices with no IRQ directly connected to the SoC, the RTC chip
1443 * can be forced as a wakeup source by stating that explicitly in
1444 * the device's .dts file using the "wakeup-source" boolean property.
1445 * If the "wakeup-source" property is set, don't request an IRQ.
1446 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1447 * if supported by the RTC.
1448 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001449 if (chip->alarm && of_property_read_bool(client->dev.of_node,
1450 "wakeup-source"))
Michael Lange8bc2a402016-01-21 18:10:16 +01001451 ds1307_can_wakeup_device = true;
Michael Lange8bc2a402016-01-21 18:10:16 +01001452#endif
1453
David Brownell045e0e82007-07-17 04:04:55 -07001454 switch (ds1307->type) {
1455 case ds_1337:
1456 case ds_1339:
Nikita Yushchenko0759c882017-08-24 09:32:11 +03001457 case ds_1341:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001458 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001459 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001460 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001461 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001462 if (err) {
1463 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001464 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001465 }
1466
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001467 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001468 if (regs[0] & DS1337_BIT_nEOSC)
1469 regs[0] &= ~DS1337_BIT_nEOSC;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001470
David Anders40ce9722012-03-23 15:02:37 -07001471 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001472 * Using IRQ or defined as wakeup-source?
1473 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001474 * For some variants, be sure alarms can trigger when we're
1475 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001476 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001477 if (want_irq || ds1307_can_wakeup_device) {
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001478 regs[0] |= DS1337_BIT_INTCN | chip->bbsqi_bit;
1479 regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001480 }
1481
Heiner Kallweit11e58902017-03-10 18:52:34 +01001482 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001483 regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001484
1485 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001486 if (regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001487 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001488 regs[1] & ~DS1337_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001489 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001490 }
David Brownell045e0e82007-07-17 04:04:55 -07001491 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001492
1493 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001494 err = regmap_bulk_read(ds1307->regmap,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001495 RX8025_REG_CTRL1 << 4 | 0x08, regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001496 if (err) {
1497 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001498 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001499 }
1500
1501 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001502 if (!(regs[1] & RX8025_BIT_XST)) {
1503 regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001504 regmap_write(ds1307->regmap,
1505 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001506 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001507 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001508 "oscillator stop detected - SET TIME!\n");
1509 }
1510
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001511 if (regs[1] & RX8025_BIT_PON) {
1512 regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001513 regmap_write(ds1307->regmap,
1514 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001515 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001516 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001517 }
1518
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001519 if (regs[1] & RX8025_BIT_VDET) {
1520 regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001521 regmap_write(ds1307->regmap,
1522 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001523 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001524 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001525 }
1526
1527 /* make sure we are running in 24hour mode */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001528 if (!(regs[0] & RX8025_BIT_2412)) {
Matthias Fuchsa2166852009-03-31 15:24:58 -07001529 u8 hour;
1530
1531 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001532 regmap_write(ds1307->regmap,
1533 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001534 regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001535
Heiner Kallweit11e58902017-03-10 18:52:34 +01001536 err = regmap_bulk_read(ds1307->regmap,
1537 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001538 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001539 if (err) {
1540 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001541 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001542 }
1543
1544 /* correct hour */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001545 hour = bcd2bin(regs[DS1307_REG_HOUR]);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001546 if (hour == 12)
1547 hour = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001548 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
Matthias Fuchsa2166852009-03-31 15:24:58 -07001549 hour += 12;
1550
Heiner Kallweit11e58902017-03-10 18:52:34 +01001551 regmap_write(ds1307->regmap,
1552 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001553 }
1554 break;
David Brownell045e0e82007-07-17 04:04:55 -07001555 default:
1556 break;
1557 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001558
1559read_rtc:
1560 /* read RTC registers */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001561 err = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
1562 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +01001563 if (err) {
1564 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001565 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001566 }
1567
David Anders40ce9722012-03-23 15:02:37 -07001568 /*
1569 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001570 * specify the extra bits as must-be-zero, but there are
1571 * still a few values that are clearly out-of-range.
1572 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001573 tmp = regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001574 switch (ds1307->type) {
1575 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001576 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001577 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001578 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001579 if (tmp & DS1307_BIT_CH) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001580 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1581 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001582 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001583 }
David Brownell045e0e82007-07-17 04:04:55 -07001584 break;
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001585 case ds_1308:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001586 case ds_1338:
1587 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001588 if (tmp & DS1307_BIT_CH)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001589 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001590
1591 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001592 if (regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001593 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001594 regs[DS1307_REG_CONTROL] &
1595 ~DS1338_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001596 dev_warn(ds1307->dev, "SET TIME!\n");
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001597 goto read_rtc;
1598 }
David Brownell045e0e82007-07-17 04:04:55 -07001599 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001600 case ds_1340:
1601 /* clock halted? turn it on, so clock can tick. */
1602 if (tmp & DS1340_BIT_nEOSC)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001603 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
frederic Rodofcd8db02008-02-06 01:38:55 -08001604
Heiner Kallweit11e58902017-03-10 18:52:34 +01001605 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1606 if (err) {
1607 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001608 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001609 }
1610
1611 /* oscillator fault? clear flag, and warn */
1612 if (tmp & DS1340_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001613 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1614 dev_warn(ds1307->dev, "SET TIME!\n");
frederic Rodofcd8db02008-02-06 01:38:55 -08001615 }
1616 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001617 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001618 /* make sure that the backup battery is enabled */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001619 if (!(regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001620 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001621 regs[DS1307_REG_WDAY] |
Heiner Kallweit11e58902017-03-10 18:52:34 +01001622 MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001623 }
1624
1625 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001626 if (!(tmp & MCP794XX_BIT_ST)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001627 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1628 MCP794XX_BIT_ST);
1629 dev_warn(ds1307->dev, "SET TIME!\n");
David Anders43fcb812011-11-02 13:37:53 -07001630 goto read_rtc;
1631 }
1632
1633 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001634 default:
David Brownell045e0e82007-07-17 04:04:55 -07001635 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001636 }
David Brownell045e0e82007-07-17 04:04:55 -07001637
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001638 tmp = regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001639 switch (ds1307->type) {
1640 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001641 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001642 case m41t00:
David Anders40ce9722012-03-23 15:02:37 -07001643 /*
1644 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001645 * systems that will run through year 2100.
1646 */
1647 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001648 case rx_8025:
1649 break;
David Brownellc065f352007-07-17 04:05:10 -07001650 default:
1651 if (!(tmp & DS1307_BIT_12HR))
1652 break;
1653
David Anders40ce9722012-03-23 15:02:37 -07001654 /*
1655 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001656 * take note...
1657 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001658 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001659 if (tmp == 12)
1660 tmp = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001661 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
David Brownellc065f352007-07-17 04:05:10 -07001662 tmp += 12;
Heiner Kallweite5531702017-07-12 07:49:47 +02001663 regmap_write(ds1307->regmap, chip->offset + DS1307_REG_HOUR,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001664 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001665 }
1666
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001667 if (want_irq || ds1307_can_wakeup_device) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001668 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001669 set_bit(HAS_ALARM, &ds1307->flags);
1670 }
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001671
1672 ds1307->rtc = devm_rtc_allocate_device(ds1307->dev);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001673 if (IS_ERR(ds1307->rtc))
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001674 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001675
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001676 if (ds1307_can_wakeup_device && !want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001677 dev_info(ds1307->dev,
1678 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001679 /* We cannot support UIE mode if we do not have an IRQ line */
1680 ds1307->rtc->uie_unsupported = 1;
1681 }
1682
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001683 if (want_irq) {
Heiner Kallweit45947122017-07-12 07:49:41 +02001684 err = devm_request_threaded_irq(ds1307->dev, client->irq, NULL,
1685 chip->irq_handler ?: ds1307_irq,
Nishanth Menonc5983192015-06-23 11:15:11 -05001686 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001687 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001688 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001689 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001690 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001691 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001692 dev_err(ds1307->dev, "unable to request IRQ!\n");
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001693 } else {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001694 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001695 }
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001696 }
1697
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001698 ds1307->rtc->ops = chip->rtc_ops ?: &ds13xx_rtc_ops;
1699 err = rtc_register_device(ds1307->rtc);
1700 if (err)
1701 return err;
1702
Austin Boyle9eab0a72012-03-23 15:02:38 -07001703 if (chip->nvram_size) {
Alexandre Belloni409baf12018-02-12 23:47:23 +01001704 struct nvmem_config nvmem_cfg = {
1705 .name = "ds1307_nvram",
1706 .word_size = 1,
1707 .stride = 1,
1708 .size = chip->nvram_size,
1709 .reg_read = ds1307_nvram_read,
1710 .reg_write = ds1307_nvram_write,
1711 .priv = ds1307,
1712 };
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001713
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001714 ds1307->rtc->nvram_old_abi = true;
Alexandre Belloni409baf12018-02-12 23:47:23 +01001715 rtc_nvmem_register(ds1307->rtc, &nvmem_cfg);
David Brownell682d73f2007-11-14 16:58:32 -08001716 }
1717
Akinobu Mita445c0202016-01-25 00:22:16 +09001718 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001719 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001720
David Brownell1abb0dc2006-06-25 05:48:17 -07001721 return 0;
1722
Jingoo Hanedca66d2013-07-03 15:07:05 -07001723exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001724 return err;
1725}
1726
David Brownell1abb0dc2006-06-25 05:48:17 -07001727static struct i2c_driver ds1307_driver = {
1728 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001729 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001730 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001731 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001732 },
David Brownellc065f352007-07-17 04:05:10 -07001733 .probe = ds1307_probe,
Jean Delvare3760f732008-04-29 23:11:40 +02001734 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001735};
1736
Axel Lin0abc9202012-03-23 15:02:31 -07001737module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001738
1739MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1740MODULE_LICENSE("GPL");