blob: 74b31dce484feebb4288050557217289500c0006 [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,
Giulio Benetti7e580762018-05-16 23:08:40 +020047 m41t11,
Tomas Novotnyf4199f82014-12-10 15:53:57 -080048 mcp794xx,
Matthias Fuchsa2166852009-03-31 15:24:58 -070049 rx_8025,
Marek Vasutee0981b2017-06-18 22:55:28 +020050 rx_8130,
Wolfram Sang32d322b2012-03-23 15:02:36 -070051 last_ds_type /* always last */
David Anders40ce9722012-03-23 15:02:37 -070052 /* rs5c372 too? different address... */
David Brownell1abb0dc2006-06-25 05:48:17 -070053};
54
David Brownell1abb0dc2006-06-25 05:48:17 -070055/* RTC registers don't differ much, except for the century flag */
56#define DS1307_REG_SECS 0x00 /* 00-59 */
57# define DS1307_BIT_CH 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070058# define DS1340_BIT_nEOSC 0x80
Tomas Novotnyf4199f82014-12-10 15:53:57 -080059# define MCP794XX_BIT_ST 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070060#define DS1307_REG_MIN 0x01 /* 00-59 */
Stefan Agner8566f702017-03-23 16:54:57 -070061# define M41T0_BIT_OF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070062#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
David Brownellc065f352007-07-17 04:05:10 -070063# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
64# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
David Brownell1abb0dc2006-06-25 05:48:17 -070065# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
66# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
67#define DS1307_REG_WDAY 0x03 /* 01-07 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -080068# define MCP794XX_BIT_VBATEN 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -070069#define DS1307_REG_MDAY 0x04 /* 01-31 */
70#define DS1307_REG_MONTH 0x05 /* 01-12 */
71# define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
72#define DS1307_REG_YEAR 0x06 /* 00-99 */
73
David Anders40ce9722012-03-23 15:02:37 -070074/*
75 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
David Brownell045e0e82007-07-17 04:04:55 -070076 * start at 7, and they differ a LOT. Only control and status matter for
77 * basic RTC date and time functionality; be careful using them.
David Brownell1abb0dc2006-06-25 05:48:17 -070078 */
David Brownell045e0e82007-07-17 04:04:55 -070079#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
David Brownell1abb0dc2006-06-25 05:48:17 -070080# define DS1307_BIT_OUT 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070081# define DS1338_BIT_OSF 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070082# define DS1307_BIT_SQWE 0x10
83# define DS1307_BIT_RS1 0x02
84# define DS1307_BIT_RS0 0x01
85#define DS1337_REG_CONTROL 0x0e
86# define DS1337_BIT_nEOSC 0x80
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070087# define DS1339_BIT_BBSQI 0x20
Wolfram Sang97f902b2009-06-17 16:26:10 -070088# define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
David Brownell1abb0dc2006-06-25 05:48:17 -070089# define DS1337_BIT_RS2 0x10
90# define DS1337_BIT_RS1 0x08
91# define DS1337_BIT_INTCN 0x04
92# define DS1337_BIT_A2IE 0x02
93# define DS1337_BIT_A1IE 0x01
David Brownell045e0e82007-07-17 04:04:55 -070094#define DS1340_REG_CONTROL 0x07
95# define DS1340_BIT_OUT 0x80
96# define DS1340_BIT_FT 0x40
97# define DS1340_BIT_CALIB_SIGN 0x20
98# define DS1340_M_CALIBRATION 0x1f
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070099#define DS1340_REG_FLAG 0x09
100# define DS1340_BIT_OSF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -0700101#define DS1337_REG_STATUS 0x0f
102# define DS1337_BIT_OSF 0x80
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900103# define DS3231_BIT_EN32KHZ 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -0700104# define DS1337_BIT_A2I 0x02
105# define DS1337_BIT_A1I 0x01
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700106#define DS1339_REG_ALARM1_SECS 0x07
Wolfram Sangeb86c302012-05-29 15:07:38 -0700107
108#define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
David Brownell1abb0dc2006-06-25 05:48:17 -0700109
Matthias Fuchsa2166852009-03-31 15:24:58 -0700110#define RX8025_REG_CTRL1 0x0e
111# define RX8025_BIT_2412 0x20
112#define RX8025_REG_CTRL2 0x0f
113# define RX8025_BIT_PON 0x10
114# define RX8025_BIT_VDET 0x40
115# define RX8025_BIT_XST 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -0700116
Giulio Benetti79230ff2018-07-25 19:26:04 +0200117#define M41TXX_REG_CONTROL 0x07
118# define M41TXX_BIT_OUT BIT(7)
119# define M41TXX_BIT_FT BIT(6)
120# define M41TXX_BIT_CALIB_SIGN BIT(5)
121# define M41TXX_M_CALIBRATION GENMASK(4, 0)
122
123/* negative offset step is -2.034ppm */
124#define M41TXX_NEG_OFFSET_STEP_PPB 2034
125/* positive offset step is +4.068ppm */
126#define M41TXX_POS_OFFSET_STEP_PPB 4068
127/* Min and max values supported with 'offset' interface by M41TXX */
128#define M41TXX_MIN_OFFSET ((-31) * M41TXX_NEG_OFFSET_STEP_PPB)
129#define M41TXX_MAX_OFFSET ((31) * M41TXX_POS_OFFSET_STEP_PPB)
130
David Brownell1abb0dc2006-06-25 05:48:17 -0700131struct ds1307 {
David Brownell1abb0dc2006-06-25 05:48:17 -0700132 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700133 unsigned long flags;
134#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
135#define HAS_ALARM 1 /* bit 1 == irq claimed */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100136 struct device *dev;
137 struct regmap *regmap;
138 const char *name;
David Brownell1abb0dc2006-06-25 05:48:17 -0700139 struct rtc_device *rtc;
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900140#ifdef CONFIG_COMMON_CLK
141 struct clk_hw clks[2];
142#endif
David Brownell1abb0dc2006-06-25 05:48:17 -0700143};
144
David Brownell045e0e82007-07-17 04:04:55 -0700145struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700146 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700147 u16 nvram_offset;
148 u16 nvram_size;
Heiner Kallweite5531702017-07-12 07:49:47 +0200149 u8 offset; /* register's offset */
Heiner Kallweite48585d2017-06-05 17:57:33 +0200150 u8 century_reg;
151 u8 century_enable_bit;
152 u8 century_bit;
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200153 u8 bbsqi_bit;
Heiner Kallweit45947122017-07-12 07:49:41 +0200154 irq_handler_t irq_handler;
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200155 const struct rtc_class_ops *rtc_ops;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700156 u16 trickle_charger_reg;
Alexandre Belloni57ec2d92017-09-04 22:46:04 +0200157 u8 (*do_trickle_setup)(struct ds1307 *, u32,
Heiner Kallweit11e58902017-03-10 18:52:34 +0100158 bool);
David Brownell045e0e82007-07-17 04:04:55 -0700159};
160
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200161static int ds1307_get_time(struct device *dev, struct rtc_time *t);
162static int ds1307_set_time(struct device *dev, struct rtc_time *t);
Giulio Benetti79230ff2018-07-25 19:26:04 +0200163static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t);
164static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t);
165static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled);
Alexandre Belloni57ec2d92017-09-04 22:46:04 +0200166static u8 do_trickle_setup_ds1339(struct ds1307 *, u32 ohms, bool diode);
Heiner Kallweit45947122017-07-12 07:49:41 +0200167static irqreturn_t rx8130_irq(int irq, void *dev_id);
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200168static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t);
169static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t);
170static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled);
Heiner Kallweit45947122017-07-12 07:49:41 +0200171static irqreturn_t mcp794xx_irq(int irq, void *dev_id);
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200172static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t);
173static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t);
174static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled);
Giulio Benetti79230ff2018-07-25 19:26:04 +0200175static int m41txx_rtc_read_offset(struct device *dev, long *offset);
176static int m41txx_rtc_set_offset(struct device *dev, long offset);
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700177
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200178static const struct rtc_class_ops rx8130_rtc_ops = {
179 .read_time = ds1307_get_time,
180 .set_time = ds1307_set_time,
181 .read_alarm = rx8130_read_alarm,
182 .set_alarm = rx8130_set_alarm,
183 .alarm_irq_enable = rx8130_alarm_irq_enable,
184};
185
186static const struct rtc_class_ops mcp794xx_rtc_ops = {
187 .read_time = ds1307_get_time,
188 .set_time = ds1307_set_time,
189 .read_alarm = mcp794xx_read_alarm,
190 .set_alarm = mcp794xx_set_alarm,
191 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
192};
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700193
Giulio Benetti79230ff2018-07-25 19:26:04 +0200194static const struct rtc_class_ops m41txx_rtc_ops = {
195 .read_time = ds1307_get_time,
196 .set_time = ds1307_set_time,
197 .read_alarm = ds1337_read_alarm,
198 .set_alarm = ds1337_set_alarm,
199 .alarm_irq_enable = ds1307_alarm_irq_enable,
200 .read_offset = m41txx_rtc_read_offset,
201 .set_offset = m41txx_rtc_set_offset,
202};
203
Heiner Kallweit7624df42017-07-12 07:49:33 +0200204static const struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700205 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700206 .nvram_offset = 8,
207 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700208 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200209 [ds_1308] = {
210 .nvram_offset = 8,
211 .nvram_size = 56,
212 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700213 [ds_1337] = {
214 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200215 .century_reg = DS1307_REG_MONTH,
216 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700217 },
218 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700219 .nvram_offset = 8,
220 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700221 },
222 [ds_1339] = {
223 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200224 .century_reg = DS1307_REG_MONTH,
225 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200226 .bbsqi_bit = DS1339_BIT_BBSQI,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700227 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700228 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700229 },
230 [ds_1340] = {
Heiner Kallweite48585d2017-06-05 17:57:33 +0200231 .century_reg = DS1307_REG_HOUR,
232 .century_enable_bit = DS1340_BIT_CENTURY_EN,
233 .century_bit = DS1340_BIT_CENTURY,
Andrea Greco51ed73eb2018-04-20 11:34:02 +0200234 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700235 .trickle_charger_reg = 0x08,
236 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300237 [ds_1341] = {
238 .century_reg = DS1307_REG_MONTH,
239 .century_bit = DS1337_BIT_CENTURY,
240 },
Wolfram Sangeb86c302012-05-29 15:07:38 -0700241 [ds_1388] = {
Heiner Kallweite5531702017-07-12 07:49:47 +0200242 .offset = 1,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700243 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700244 },
245 [ds_3231] = {
246 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200247 .century_reg = DS1307_REG_MONTH,
248 .century_bit = DS1337_BIT_CENTURY,
Heiner Kallweit0b6ee802017-07-12 07:49:22 +0200249 .bbsqi_bit = DS3231_BIT_BBSQW,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700250 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200251 [rx_8130] = {
252 .alarm = 1,
253 /* this is battery backed SRAM */
254 .nvram_offset = 0x20,
255 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
Heiner Kallweite5531702017-07-12 07:49:47 +0200256 .offset = 0x10,
Heiner Kallweit45947122017-07-12 07:49:41 +0200257 .irq_handler = rx8130_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200258 .rtc_ops = &rx8130_rtc_ops,
Marek Vasutee0981b2017-06-18 22:55:28 +0200259 },
Giulio Benetti79230ff2018-07-25 19:26:04 +0200260 [m41t0] = {
261 .rtc_ops = &m41txx_rtc_ops,
262 },
263 [m41t00] = {
264 .rtc_ops = &m41txx_rtc_ops,
265 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200266 [m41t11] = {
267 /* this is battery backed SRAM */
268 .nvram_offset = 8,
269 .nvram_size = 56,
Giulio Benetti79230ff2018-07-25 19:26:04 +0200270 .rtc_ops = &m41txx_rtc_ops,
Giulio Benetti7e580762018-05-16 23:08:40 +0200271 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800272 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700273 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700274 /* this is battery backed SRAM */
275 .nvram_offset = 0x20,
276 .nvram_size = 0x40,
Heiner Kallweit45947122017-07-12 07:49:41 +0200277 .irq_handler = mcp794xx_irq,
Heiner Kallweit1efb98b2017-07-12 07:49:44 +0200278 .rtc_ops = &mcp794xx_rtc_ops,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700279 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700280};
David Brownell045e0e82007-07-17 04:04:55 -0700281
Jean Delvare3760f732008-04-29 23:11:40 +0200282static const struct i2c_device_id ds1307_id[] = {
283 { "ds1307", ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200284 { "ds1308", ds_1308 },
Jean Delvare3760f732008-04-29 23:11:40 +0200285 { "ds1337", ds_1337 },
286 { "ds1338", ds_1338 },
287 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700288 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200289 { "ds1340", ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300290 { "ds1341", ds_1341 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700291 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700292 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200293 { "m41t00", m41t00 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200294 { "m41t11", m41t11 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800295 { "mcp7940x", mcp794xx },
296 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700297 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700298 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200299 { "isl12057", ds_1337 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200300 { "rx8130", rx_8130 },
Jean Delvare3760f732008-04-29 23:11:40 +0200301 { }
302};
303MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700304
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300305#ifdef CONFIG_OF
306static const struct of_device_id ds1307_of_match[] = {
307 {
308 .compatible = "dallas,ds1307",
309 .data = (void *)ds_1307
310 },
311 {
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200312 .compatible = "dallas,ds1308",
313 .data = (void *)ds_1308
314 },
315 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300316 .compatible = "dallas,ds1337",
317 .data = (void *)ds_1337
318 },
319 {
320 .compatible = "dallas,ds1338",
321 .data = (void *)ds_1338
322 },
323 {
324 .compatible = "dallas,ds1339",
325 .data = (void *)ds_1339
326 },
327 {
328 .compatible = "dallas,ds1388",
329 .data = (void *)ds_1388
330 },
331 {
332 .compatible = "dallas,ds1340",
333 .data = (void *)ds_1340
334 },
335 {
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300336 .compatible = "dallas,ds1341",
337 .data = (void *)ds_1341
338 },
339 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300340 .compatible = "maxim,ds3231",
341 .data = (void *)ds_3231
342 },
343 {
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200344 .compatible = "st,m41t0",
Giulio Benetti146a5522018-05-16 23:08:39 +0200345 .data = (void *)m41t0
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200346 },
347 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300348 .compatible = "st,m41t00",
349 .data = (void *)m41t00
350 },
351 {
Giulio Benetti7e580762018-05-16 23:08:40 +0200352 .compatible = "st,m41t11",
353 .data = (void *)m41t11
354 },
355 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300356 .compatible = "microchip,mcp7940x",
357 .data = (void *)mcp794xx
358 },
359 {
360 .compatible = "microchip,mcp7941x",
361 .data = (void *)mcp794xx
362 },
363 {
364 .compatible = "pericom,pt7c4338",
365 .data = (void *)ds_1307
366 },
367 {
368 .compatible = "epson,rx8025",
369 .data = (void *)rx_8025
370 },
371 {
372 .compatible = "isil,isl12057",
373 .data = (void *)ds_1337
374 },
Bastian Stender47dd4722017-10-17 14:46:07 +0200375 {
376 .compatible = "epson,rx8130",
377 .data = (void *)rx_8130
378 },
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300379 { }
380};
381MODULE_DEVICE_TABLE(of, ds1307_of_match);
382#endif
383
Tin Huynh9c19b892016-11-30 09:57:31 +0700384#ifdef CONFIG_ACPI
385static const struct acpi_device_id ds1307_acpi_ids[] = {
386 { .id = "DS1307", .driver_data = ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200387 { .id = "DS1308", .driver_data = ds_1308 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700388 { .id = "DS1337", .driver_data = ds_1337 },
389 { .id = "DS1338", .driver_data = ds_1338 },
390 { .id = "DS1339", .driver_data = ds_1339 },
391 { .id = "DS1388", .driver_data = ds_1388 },
392 { .id = "DS1340", .driver_data = ds_1340 },
Nikita Yushchenko0759c882017-08-24 09:32:11 +0300393 { .id = "DS1341", .driver_data = ds_1341 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700394 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700395 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700396 { .id = "M41T00", .driver_data = m41t00 },
Giulio Benetti7e580762018-05-16 23:08:40 +0200397 { .id = "M41T11", .driver_data = m41t11 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700398 { .id = "MCP7940X", .driver_data = mcp794xx },
399 { .id = "MCP7941X", .driver_data = mcp794xx },
400 { .id = "PT7C4338", .driver_data = ds_1307 },
401 { .id = "RX8025", .driver_data = rx_8025 },
402 { .id = "ISL12057", .driver_data = ds_1337 },
Bastian Stender47dd4722017-10-17 14:46:07 +0200403 { .id = "RX8130", .driver_data = rx_8130 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700404 { }
405};
406MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
407#endif
408
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700409/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700410 * The ds1337 and ds1339 both have two alarms, but we only use the first
411 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
412 * signal; ds1339 chips have only one alarm signal.
413 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500414static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700415{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100416 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500417 struct mutex *lock = &ds1307->rtc->ops_lock;
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200418 int stat, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700419
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700420 mutex_lock(lock);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100421 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
422 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700423 goto out;
424
425 if (stat & DS1337_BIT_A1I) {
426 stat &= ~DS1337_BIT_A1I;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100427 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700428
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200429 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
430 DS1337_BIT_A1IE, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100431 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700432 goto out;
433
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700434 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700435 }
436
437out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700438 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700439
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700440 return IRQ_HANDLED;
441}
442
443/*----------------------------------------------------------------------*/
444
David Brownell1abb0dc2006-06-25 05:48:17 -0700445static int ds1307_get_time(struct device *dev, struct rtc_time *t)
446{
447 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100448 int tmp, ret;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200449 const struct chip_desc *chip = &chips[ds1307->type];
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200450 u8 regs[7];
David Brownell1abb0dc2006-06-25 05:48:17 -0700451
David Brownell045e0e82007-07-17 04:04:55 -0700452 /* read the RTC date and time registers all at once */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200453 ret = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
454 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100455 if (ret) {
456 dev_err(dev, "%s error %d\n", "read", ret);
457 return ret;
David Brownell1abb0dc2006-06-25 05:48:17 -0700458 }
459
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200460 dev_dbg(dev, "%s: %7ph\n", "read", regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700461
Stefan Agner8566f702017-03-23 16:54:57 -0700462 /* if oscillator fail bit is set, no data can be trusted */
463 if (ds1307->type == m41t0 &&
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200464 regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
Stefan Agner8566f702017-03-23 16:54:57 -0700465 dev_warn_once(dev, "oscillator failed, set time!\n");
466 return -EINVAL;
467 }
468
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200469 t->tm_sec = bcd2bin(regs[DS1307_REG_SECS] & 0x7f);
470 t->tm_min = bcd2bin(regs[DS1307_REG_MIN] & 0x7f);
471 tmp = regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700472 t->tm_hour = bcd2bin(tmp);
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200473 t->tm_wday = bcd2bin(regs[DS1307_REG_WDAY] & 0x07) - 1;
474 t->tm_mday = bcd2bin(regs[DS1307_REG_MDAY] & 0x3f);
475 tmp = regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700476 t->tm_mon = bcd2bin(tmp) - 1;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200477 t->tm_year = bcd2bin(regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700478
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200479 if (regs[chip->century_reg] & chip->century_bit &&
Heiner Kallweite48585d2017-06-05 17:57:33 +0200480 IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY))
481 t->tm_year += 100;
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200482
David Brownell1abb0dc2006-06-25 05:48:17 -0700483 dev_dbg(dev, "%s secs=%d, mins=%d, "
484 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
485 "read", t->tm_sec, t->tm_min,
486 t->tm_hour, t->tm_mday,
487 t->tm_mon, t->tm_year, t->tm_wday);
488
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100489 return 0;
David Brownell1abb0dc2006-06-25 05:48:17 -0700490}
491
492static int ds1307_set_time(struct device *dev, struct rtc_time *t)
493{
494 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweite48585d2017-06-05 17:57:33 +0200495 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell1abb0dc2006-06-25 05:48:17 -0700496 int result;
497 int tmp;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200498 u8 regs[7];
David Brownell1abb0dc2006-06-25 05:48:17 -0700499
500 dev_dbg(dev, "%s secs=%d, mins=%d, "
501 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400502 "write", t->tm_sec, t->tm_min,
503 t->tm_hour, t->tm_mday,
504 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700505
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200506 if (t->tm_year < 100)
507 return -EINVAL;
508
Heiner Kallweite48585d2017-06-05 17:57:33 +0200509#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
510 if (t->tm_year > (chip->century_bit ? 299 : 199))
511 return -EINVAL;
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200512#else
Heiner Kallweite48585d2017-06-05 17:57:33 +0200513 if (t->tm_year > 199)
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200514 return -EINVAL;
515#endif
516
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200517 regs[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
518 regs[DS1307_REG_MIN] = bin2bcd(t->tm_min);
519 regs[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
520 regs[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
521 regs[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
522 regs[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700523
524 /* assume 20YY not 19YY */
525 tmp = t->tm_year - 100;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200526 regs[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700527
Heiner Kallweite48585d2017-06-05 17:57:33 +0200528 if (chip->century_enable_bit)
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200529 regs[chip->century_reg] |= chip->century_enable_bit;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200530 if (t->tm_year > 199 && chip->century_bit)
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200531 regs[chip->century_reg] |= chip->century_bit;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200532
533 if (ds1307->type == mcp794xx) {
David Anders40ce9722012-03-23 15:02:37 -0700534 /*
535 * these bits were cleared when preparing the date/time
536 * values and need to be set again before writing the
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200537 * regsfer out to the device.
David Anders40ce9722012-03-23 15:02:37 -0700538 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200539 regs[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
540 regs[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700541 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700542
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200543 dev_dbg(dev, "%s: %7ph\n", "write", regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700544
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200545 result = regmap_bulk_write(ds1307->regmap, chip->offset, regs,
546 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100547 if (result) {
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800548 dev_err(dev, "%s error %d\n", "write", result);
549 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700550 }
551 return 0;
552}
553
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800554static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700555{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100556 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700557 int ret;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200558 u8 regs[9];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700559
560 if (!test_bit(HAS_ALARM, &ds1307->flags))
561 return -EINVAL;
562
563 /* read all ALARM1, ALARM2, and status registers at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100564 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200565 regs, sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100566 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700567 dev_err(dev, "%s error %d\n", "alarm read", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100568 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700569 }
570
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100571 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200572 &regs[0], &regs[4], &regs[7]);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700573
David Anders40ce9722012-03-23 15:02:37 -0700574 /*
575 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700576 * and that all four fields are checked matches
577 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200578 t->time.tm_sec = bcd2bin(regs[0] & 0x7f);
579 t->time.tm_min = bcd2bin(regs[1] & 0x7f);
580 t->time.tm_hour = bcd2bin(regs[2] & 0x3f);
581 t->time.tm_mday = bcd2bin(regs[3] & 0x3f);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700582
583 /* ... and status */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200584 t->enabled = !!(regs[7] & DS1337_BIT_A1IE);
585 t->pending = !!(regs[8] & DS1337_BIT_A1I);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700586
587 dev_dbg(dev, "%s secs=%d, mins=%d, "
588 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
589 "alarm read", t->time.tm_sec, t->time.tm_min,
590 t->time.tm_hour, t->time.tm_mday,
591 t->enabled, t->pending);
592
593 return 0;
594}
595
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800596static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700597{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100598 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200599 unsigned char regs[9];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700600 u8 control, status;
601 int ret;
602
603 if (!test_bit(HAS_ALARM, &ds1307->flags))
604 return -EINVAL;
605
606 dev_dbg(dev, "%s secs=%d, mins=%d, "
607 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
608 "alarm set", t->time.tm_sec, t->time.tm_min,
609 t->time.tm_hour, t->time.tm_mday,
610 t->enabled, t->pending);
611
612 /* read current status of both alarms and the chip */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200613 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
614 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100615 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700616 dev_err(dev, "%s error %d\n", "alarm write", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100617 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700618 }
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200619 control = regs[7];
620 status = regs[8];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700621
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100622 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200623 &regs[0], &regs[4], control, status);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700624
625 /* set ALARM1, using 24 hour and day-of-month modes */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200626 regs[0] = bin2bcd(t->time.tm_sec);
627 regs[1] = bin2bcd(t->time.tm_min);
628 regs[2] = bin2bcd(t->time.tm_hour);
629 regs[3] = bin2bcd(t->time.tm_mday);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700630
631 /* set ALARM2 to non-garbage */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200632 regs[4] = 0;
633 regs[5] = 0;
634 regs[6] = 0;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700635
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200636 /* disable alarms */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200637 regs[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
638 regs[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700639
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200640 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
641 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100642 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700643 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800644 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700645 }
646
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200647 /* optionally enable ALARM1 */
648 if (t->enabled) {
649 dev_dbg(dev, "alarm IRQ armed\n");
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200650 regs[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
651 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, regs[7]);
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200652 }
653
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700654 return 0;
655}
656
John Stultz16380c12011-02-02 17:02:41 -0800657static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700658{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100659 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700660
John Stultz16380c12011-02-02 17:02:41 -0800661 if (!test_bit(HAS_ALARM, &ds1307->flags))
662 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700663
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200664 return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
665 DS1337_BIT_A1IE,
666 enabled ? DS1337_BIT_A1IE : 0);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700667}
668
David Brownellff8371a2006-09-30 23:28:17 -0700669static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700670 .read_time = ds1307_get_time,
671 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800672 .read_alarm = ds1337_read_alarm,
673 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800674 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700675};
676
David Brownell682d73f2007-11-14 16:58:32 -0800677/*----------------------------------------------------------------------*/
678
Simon Guinot1d1945d2014-04-03 14:49:55 -0700679/*
Marek Vasutee0981b2017-06-18 22:55:28 +0200680 * Alarm support for rx8130 devices.
681 */
682
683#define RX8130_REG_ALARM_MIN 0x07
684#define RX8130_REG_ALARM_HOUR 0x08
685#define RX8130_REG_ALARM_WEEK_OR_DAY 0x09
686#define RX8130_REG_EXTENSION 0x0c
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200687#define RX8130_REG_EXTENSION_WADA BIT(3)
Marek Vasutee0981b2017-06-18 22:55:28 +0200688#define RX8130_REG_FLAG 0x0d
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200689#define RX8130_REG_FLAG_AF BIT(3)
Marek Vasutee0981b2017-06-18 22:55:28 +0200690#define RX8130_REG_CONTROL0 0x0e
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200691#define RX8130_REG_CONTROL0_AIE BIT(3)
Marek Vasutee0981b2017-06-18 22:55:28 +0200692
693static irqreturn_t rx8130_irq(int irq, void *dev_id)
694{
695 struct ds1307 *ds1307 = dev_id;
696 struct mutex *lock = &ds1307->rtc->ops_lock;
697 u8 ctl[3];
698 int ret;
699
700 mutex_lock(lock);
701
702 /* Read control registers. */
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200703 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
704 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200705 if (ret < 0)
706 goto out;
707 if (!(ctl[1] & RX8130_REG_FLAG_AF))
708 goto out;
709 ctl[1] &= ~RX8130_REG_FLAG_AF;
710 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
711
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200712 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
713 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200714 if (ret < 0)
715 goto out;
716
717 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
718
719out:
720 mutex_unlock(lock);
721
722 return IRQ_HANDLED;
723}
724
725static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
726{
727 struct ds1307 *ds1307 = dev_get_drvdata(dev);
728 u8 ald[3], ctl[3];
729 int ret;
730
731 if (!test_bit(HAS_ALARM, &ds1307->flags))
732 return -EINVAL;
733
734 /* Read alarm registers. */
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200735 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
736 sizeof(ald));
Marek Vasutee0981b2017-06-18 22:55:28 +0200737 if (ret < 0)
738 return ret;
739
740 /* Read control registers. */
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200741 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
742 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200743 if (ret < 0)
744 return ret;
745
746 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
747 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
748
749 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
750 t->time.tm_sec = -1;
751 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
752 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
753 t->time.tm_wday = -1;
754 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
755 t->time.tm_mon = -1;
756 t->time.tm_year = -1;
757 t->time.tm_yday = -1;
758 t->time.tm_isdst = -1;
759
760 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
761 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
762 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
763
764 return 0;
765}
766
767static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
768{
769 struct ds1307 *ds1307 = dev_get_drvdata(dev);
770 u8 ald[3], ctl[3];
771 int ret;
772
773 if (!test_bit(HAS_ALARM, &ds1307->flags))
774 return -EINVAL;
775
776 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
777 "enabled=%d pending=%d\n", __func__,
778 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
779 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
780 t->enabled, t->pending);
781
782 /* Read control registers. */
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200783 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
784 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200785 if (ret < 0)
786 return ret;
787
788 ctl[0] &= ~RX8130_REG_EXTENSION_WADA;
789 ctl[1] |= RX8130_REG_FLAG_AF;
790 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
791
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200792 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
793 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200794 if (ret < 0)
795 return ret;
796
797 /* Hardware alarm precision is 1 minute! */
798 ald[0] = bin2bcd(t->time.tm_min);
799 ald[1] = bin2bcd(t->time.tm_hour);
800 ald[2] = bin2bcd(t->time.tm_mday);
801
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200802 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
803 sizeof(ald));
Marek Vasutee0981b2017-06-18 22:55:28 +0200804 if (ret < 0)
805 return ret;
806
807 if (!t->enabled)
808 return 0;
809
810 ctl[2] |= RX8130_REG_CONTROL0_AIE;
811
Alexandre Bellonif2b48012017-09-04 22:46:03 +0200812 return regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
813 sizeof(ctl));
Marek Vasutee0981b2017-06-18 22:55:28 +0200814}
815
816static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
817{
818 struct ds1307 *ds1307 = dev_get_drvdata(dev);
819 int ret, reg;
820
821 if (!test_bit(HAS_ALARM, &ds1307->flags))
822 return -EINVAL;
823
824 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
825 if (ret < 0)
826 return ret;
827
828 if (enabled)
829 reg |= RX8130_REG_CONTROL0_AIE;
830 else
831 reg &= ~RX8130_REG_CONTROL0_AIE;
832
833 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
834}
835
Marek Vasutee0981b2017-06-18 22:55:28 +0200836/*----------------------------------------------------------------------*/
837
838/*
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800839 * Alarm support for mcp794xx devices.
Simon Guinot1d1945d2014-04-03 14:49:55 -0700840 */
841
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800842#define MCP794XX_REG_CONTROL 0x07
843# define MCP794XX_BIT_ALM0_EN 0x10
844# define MCP794XX_BIT_ALM1_EN 0x20
845#define MCP794XX_REG_ALARM0_BASE 0x0a
846#define MCP794XX_REG_ALARM0_CTRL 0x0d
847#define MCP794XX_REG_ALARM1_BASE 0x11
848#define MCP794XX_REG_ALARM1_CTRL 0x14
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200849# define MCP794XX_BIT_ALMX_IF BIT(3)
850# define MCP794XX_BIT_ALMX_C0 BIT(4)
851# define MCP794XX_BIT_ALMX_C1 BIT(5)
852# define MCP794XX_BIT_ALMX_C2 BIT(6)
853# define MCP794XX_BIT_ALMX_POL BIT(7)
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800854# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
855 MCP794XX_BIT_ALMX_C1 | \
856 MCP794XX_BIT_ALMX_C2)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700857
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500858static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700859{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100860 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500861 struct mutex *lock = &ds1307->rtc->ops_lock;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700862 int reg, ret;
863
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500864 mutex_lock(lock);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700865
866 /* Check and clear alarm 0 interrupt flag. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100867 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
868 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700869 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800870 if (!(reg & MCP794XX_BIT_ALMX_IF))
Simon Guinot1d1945d2014-04-03 14:49:55 -0700871 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800872 reg &= ~MCP794XX_BIT_ALMX_IF;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100873 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
874 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700875 goto out;
876
877 /* Disable alarm 0. */
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200878 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
879 MCP794XX_BIT_ALM0_EN, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100880 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700881 goto out;
882
883 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
884
885out:
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500886 mutex_unlock(lock);
887
888 return IRQ_HANDLED;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700889}
890
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800891static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700892{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100893 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200894 u8 regs[10];
Simon Guinot1d1945d2014-04-03 14:49:55 -0700895 int ret;
896
897 if (!test_bit(HAS_ALARM, &ds1307->flags))
898 return -EINVAL;
899
900 /* Read control and alarm 0 registers. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200901 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
902 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100903 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700904 return ret;
905
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800906 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700907
908 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200909 t->time.tm_sec = bcd2bin(regs[3] & 0x7f);
910 t->time.tm_min = bcd2bin(regs[4] & 0x7f);
911 t->time.tm_hour = bcd2bin(regs[5] & 0x3f);
912 t->time.tm_wday = bcd2bin(regs[6] & 0x7) - 1;
913 t->time.tm_mday = bcd2bin(regs[7] & 0x3f);
914 t->time.tm_mon = bcd2bin(regs[8] & 0x1f) - 1;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700915 t->time.tm_year = -1;
916 t->time.tm_yday = -1;
917 t->time.tm_isdst = -1;
918
919 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
Alexandre Bellonieb4fd192017-09-04 22:46:05 +0200920 "enabled=%d polarity=%d irq=%d match=%lu\n", __func__,
Simon Guinot1d1945d2014-04-03 14:49:55 -0700921 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
922 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200923 !!(regs[6] & MCP794XX_BIT_ALMX_POL),
924 !!(regs[6] & MCP794XX_BIT_ALMX_IF),
925 (regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700926
927 return 0;
928}
929
Heiner Kallweit584ce302017-08-29 21:52:56 +0200930/*
931 * We may have a random RTC weekday, therefore calculate alarm weekday based
932 * on current weekday we read from the RTC timekeeping regs
933 */
934static int mcp794xx_alm_weekday(struct device *dev, struct rtc_time *tm_alarm)
935{
936 struct rtc_time tm_now;
937 int days_now, days_alarm, ret;
938
939 ret = ds1307_get_time(dev, &tm_now);
940 if (ret)
941 return ret;
942
943 days_now = div_s64(rtc_tm_to_time64(&tm_now), 24 * 60 * 60);
944 days_alarm = div_s64(rtc_tm_to_time64(tm_alarm), 24 * 60 * 60);
945
946 return (tm_now.tm_wday + days_alarm - days_now) % 7 + 1;
947}
948
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800949static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700950{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100951 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200952 unsigned char regs[10];
Heiner Kallweit584ce302017-08-29 21:52:56 +0200953 int wday, ret;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700954
955 if (!test_bit(HAS_ALARM, &ds1307->flags))
956 return -EINVAL;
957
Heiner Kallweit584ce302017-08-29 21:52:56 +0200958 wday = mcp794xx_alm_weekday(dev, &t->time);
959 if (wday < 0)
960 return wday;
961
Simon Guinot1d1945d2014-04-03 14:49:55 -0700962 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
963 "enabled=%d pending=%d\n", __func__,
964 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
965 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
966 t->enabled, t->pending);
967
968 /* Read control and alarm 0 registers. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200969 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
970 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100971 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700972 return ret;
973
974 /* Set alarm 0, using 24-hour and day-of-month modes. */
975 regs[3] = bin2bcd(t->time.tm_sec);
976 regs[4] = bin2bcd(t->time.tm_min);
977 regs[5] = bin2bcd(t->time.tm_hour);
Heiner Kallweit584ce302017-08-29 21:52:56 +0200978 regs[6] = wday;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700979 regs[7] = bin2bcd(t->time.tm_mday);
Tero Kristo62c8c202015-10-23 09:29:57 +0300980 regs[8] = bin2bcd(t->time.tm_mon + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700981
982 /* Clear the alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800983 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700984 /* Set alarm match: second, minute, hour, day, date, month. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800985 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
Nishanth Menone3edd672015-04-20 19:51:34 -0500986 /* Disable interrupt. We will not enable until completely programmed */
987 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700988
Alexandre Belloni042fa8c2017-09-04 22:46:02 +0200989 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
990 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +0100991 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700992 return ret;
993
Nishanth Menone3edd672015-04-20 19:51:34 -0500994 if (!t->enabled)
995 return 0;
996 regs[0] |= MCP794XX_BIT_ALM0_EN;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100997 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700998}
999
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001000static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
Simon Guinot1d1945d2014-04-03 14:49:55 -07001001{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001002 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -07001003
1004 if (!test_bit(HAS_ALARM, &ds1307->flags))
1005 return -EINVAL;
1006
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001007 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
1008 MCP794XX_BIT_ALM0_EN,
1009 enabled ? MCP794XX_BIT_ALM0_EN : 0);
Simon Guinot1d1945d2014-04-03 14:49:55 -07001010}
1011
Giulio Benetti79230ff2018-07-25 19:26:04 +02001012static int m41txx_rtc_read_offset(struct device *dev, long *offset)
1013{
1014 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1015 unsigned int ctrl_reg;
1016 u8 val;
1017
1018 regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
1019
1020 val = ctrl_reg & M41TXX_M_CALIBRATION;
1021
1022 /* check if positive */
1023 if (ctrl_reg & M41TXX_BIT_CALIB_SIGN)
1024 *offset = (val * M41TXX_POS_OFFSET_STEP_PPB);
1025 else
1026 *offset = -(val * M41TXX_NEG_OFFSET_STEP_PPB);
1027
1028 return 0;
1029}
1030
1031static int m41txx_rtc_set_offset(struct device *dev, long offset)
1032{
1033 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1034 unsigned int ctrl_reg;
1035
1036 if ((offset < M41TXX_MIN_OFFSET) || (offset > M41TXX_MAX_OFFSET))
1037 return -ERANGE;
1038
1039 if (offset >= 0) {
1040 ctrl_reg = DIV_ROUND_CLOSEST(offset,
1041 M41TXX_POS_OFFSET_STEP_PPB);
1042 ctrl_reg |= M41TXX_BIT_CALIB_SIGN;
1043 } else {
1044 ctrl_reg = DIV_ROUND_CLOSEST(abs(offset),
1045 M41TXX_NEG_OFFSET_STEP_PPB);
1046 }
1047
1048 return regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL,
1049 M41TXX_M_CALIBRATION | M41TXX_BIT_CALIB_SIGN,
1050 ctrl_reg);
1051}
1052
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001053static ssize_t frequency_test_store(struct device *dev,
1054 struct device_attribute *attr,
1055 const char *buf, size_t count)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001056{
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001057 struct ds1307 *ds1307 = dev_get_drvdata(dev->parent);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001058 bool freq_test_en;
1059 int ret;
1060
1061 ret = kstrtobool(buf, &freq_test_en);
1062 if (ret) {
1063 dev_err(dev, "Failed to store RTC Frequency Test attribute\n");
1064 return ret;
1065 }
1066
1067 regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL, M41TXX_BIT_FT,
1068 freq_test_en ? M41TXX_BIT_FT : 0);
1069
1070 return count;
1071}
1072
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001073static ssize_t frequency_test_show(struct device *dev,
1074 struct device_attribute *attr,
1075 char *buf)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001076{
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001077 struct ds1307 *ds1307 = dev_get_drvdata(dev->parent);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001078 unsigned int ctrl_reg;
1079
1080 regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
1081
1082 return scnprintf(buf, PAGE_SIZE, (ctrl_reg & M41TXX_BIT_FT) ? "on\n" :
1083 "off\n");
1084}
1085
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001086static DEVICE_ATTR_RW(frequency_test);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001087
1088static struct attribute *rtc_freq_test_attrs[] = {
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001089 &dev_attr_frequency_test.attr,
Giulio Benettib41c23e2018-07-25 19:26:05 +02001090 NULL,
1091};
1092
1093static const struct attribute_group rtc_freq_test_attr_group = {
1094 .attrs = rtc_freq_test_attrs,
1095};
1096
Giulio Benettib41c23e2018-07-25 19:26:05 +02001097static int ds1307_add_frequency_test(struct ds1307 *ds1307)
1098{
1099 int err;
1100
1101 switch (ds1307->type) {
1102 case m41t0:
1103 case m41t00:
1104 case m41t11:
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001105 err = rtc_add_group(ds1307->rtc, &rtc_freq_test_attr_group);
1106 if (err)
Giulio Benettib41c23e2018-07-25 19:26:05 +02001107 return err;
Giulio Benettib41c23e2018-07-25 19:26:05 +02001108 break;
1109 default:
1110 break;
1111 }
1112
1113 return 0;
1114}
1115
Simon Guinot1d1945d2014-04-03 14:49:55 -07001116/*----------------------------------------------------------------------*/
1117
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001118static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
1119 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -08001120{
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001121 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +02001122 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -08001123
Heiner Kallweit969fa072017-07-12 07:49:54 +02001124 return regmap_bulk_read(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001125 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -08001126}
1127
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001128static int ds1307_nvram_write(void *priv, unsigned int offset, void *val,
1129 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -08001130{
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001131 struct ds1307 *ds1307 = priv;
Heiner Kallweit969fa072017-07-12 07:49:54 +02001132 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell682d73f2007-11-14 16:58:32 -08001133
Heiner Kallweit969fa072017-07-12 07:49:54 +02001134 return regmap_bulk_write(ds1307->regmap, chip->nvram_offset + offset,
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001135 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -08001136}
1137
David Brownell682d73f2007-11-14 16:58:32 -08001138/*----------------------------------------------------------------------*/
1139
Heiner Kallweit11e58902017-03-10 18:52:34 +01001140static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307,
Alexandre Belloni57ec2d92017-09-04 22:46:04 +02001141 u32 ohms, bool diode)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001142{
1143 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
1144 DS1307_TRICKLE_CHARGER_NO_DIODE;
1145
1146 switch (ohms) {
1147 case 250:
1148 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
1149 break;
1150 case 2000:
1151 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
1152 break;
1153 case 4000:
1154 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
1155 break;
1156 default:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001157 dev_warn(ds1307->dev,
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001158 "Unsupported ohm value %u in dt\n", ohms);
1159 return 0;
1160 }
1161 return setup;
1162}
1163
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001164static u8 ds1307_trickle_init(struct ds1307 *ds1307,
Heiner Kallweit7624df42017-07-12 07:49:33 +02001165 const struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001166{
Alexandre Belloni57ec2d92017-09-04 22:46:04 +02001167 u32 ohms;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001168 bool diode = true;
1169
1170 if (!chip->do_trickle_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001171 return 0;
1172
Heiner Kallweit11e58902017-03-10 18:52:34 +01001173 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
1174 &ohms))
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001175 return 0;
1176
Heiner Kallweit11e58902017-03-10 18:52:34 +01001177 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001178 diode = false;
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001179
1180 return chip->do_trickle_setup(ds1307, ohms, diode);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001181}
1182
Akinobu Mita445c0202016-01-25 00:22:16 +09001183/*----------------------------------------------------------------------*/
1184
Heiner Kallweit6b583a62017-09-27 22:41:26 +02001185#if IS_REACHABLE(CONFIG_HWMON)
Akinobu Mita445c0202016-01-25 00:22:16 +09001186
1187/*
1188 * Temperature sensor support for ds3231 devices.
1189 */
1190
1191#define DS3231_REG_TEMPERATURE 0x11
1192
1193/*
1194 * A user-initiated temperature conversion is not started by this function,
1195 * so the temperature is updated once every 64 seconds.
1196 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001197static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +09001198{
1199 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1200 u8 temp_buf[2];
1201 s16 temp;
1202 int ret;
1203
Heiner Kallweit11e58902017-03-10 18:52:34 +01001204 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
1205 temp_buf, sizeof(temp_buf));
1206 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +09001207 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +09001208 /*
1209 * Temperature is represented as a 10-bit code with a resolution of
1210 * 0.25 degree celsius and encoded in two's complement format.
1211 */
1212 temp = (temp_buf[0] << 8) | temp_buf[1];
1213 temp >>= 6;
1214 *mC = temp * 250;
1215
1216 return 0;
1217}
1218
1219static ssize_t ds3231_hwmon_show_temp(struct device *dev,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001220 struct device_attribute *attr, char *buf)
Akinobu Mita445c0202016-01-25 00:22:16 +09001221{
1222 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001223 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001224
1225 ret = ds3231_hwmon_read_temp(dev, &temp);
1226 if (ret)
1227 return ret;
1228
1229 return sprintf(buf, "%d\n", temp);
1230}
Alexandre Bellonib4be2712017-09-04 22:46:08 +02001231static SENSOR_DEVICE_ATTR(temp1_input, 0444, ds3231_hwmon_show_temp,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001232 NULL, 0);
Akinobu Mita445c0202016-01-25 00:22:16 +09001233
1234static struct attribute *ds3231_hwmon_attrs[] = {
1235 &sensor_dev_attr_temp1_input.dev_attr.attr,
1236 NULL,
1237};
1238ATTRIBUTE_GROUPS(ds3231_hwmon);
1239
1240static void ds1307_hwmon_register(struct ds1307 *ds1307)
1241{
1242 struct device *dev;
1243
1244 if (ds1307->type != ds_3231)
1245 return;
1246
Heiner Kallweit11e58902017-03-10 18:52:34 +01001247 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001248 ds1307,
1249 ds3231_hwmon_groups);
Akinobu Mita445c0202016-01-25 00:22:16 +09001250 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001251 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1252 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001253 }
1254}
1255
1256#else
1257
1258static void ds1307_hwmon_register(struct ds1307 *ds1307)
1259{
1260}
1261
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001262#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1263
1264/*----------------------------------------------------------------------*/
1265
1266/*
1267 * Square-wave output support for DS3231
1268 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1269 */
1270#ifdef CONFIG_COMMON_CLK
1271
1272enum {
1273 DS3231_CLK_SQW = 0,
1274 DS3231_CLK_32KHZ,
1275};
1276
1277#define clk_sqw_to_ds1307(clk) \
1278 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1279#define clk_32khz_to_ds1307(clk) \
1280 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1281
1282static int ds3231_clk_sqw_rates[] = {
1283 1,
1284 1024,
1285 4096,
1286 8192,
1287};
1288
1289static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1290{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001291 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001292 int ret;
1293
1294 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001295 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1296 mask, value);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001297 mutex_unlock(lock);
1298
1299 return ret;
1300}
1301
1302static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1303 unsigned long parent_rate)
1304{
1305 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001306 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001307 int rate_sel = 0;
1308
Heiner Kallweit11e58902017-03-10 18:52:34 +01001309 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1310 if (ret)
1311 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001312 if (control & DS1337_BIT_RS1)
1313 rate_sel += 1;
1314 if (control & DS1337_BIT_RS2)
1315 rate_sel += 2;
1316
1317 return ds3231_clk_sqw_rates[rate_sel];
1318}
1319
1320static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001321 unsigned long *prate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001322{
1323 int i;
1324
1325 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1326 if (ds3231_clk_sqw_rates[i] <= rate)
1327 return ds3231_clk_sqw_rates[i];
1328 }
1329
1330 return 0;
1331}
1332
1333static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001334 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001335{
1336 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1337 int control = 0;
1338 int rate_sel;
1339
1340 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1341 rate_sel++) {
1342 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1343 break;
1344 }
1345
1346 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1347 return -EINVAL;
1348
1349 if (rate_sel & 1)
1350 control |= DS1337_BIT_RS1;
1351 if (rate_sel & 2)
1352 control |= DS1337_BIT_RS2;
1353
1354 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1355 control);
1356}
1357
1358static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1359{
1360 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1361
1362 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1363}
1364
1365static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1366{
1367 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1368
1369 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1370}
1371
1372static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1373{
1374 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001375 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001376
Heiner Kallweit11e58902017-03-10 18:52:34 +01001377 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1378 if (ret)
1379 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001380
1381 return !(control & DS1337_BIT_INTCN);
1382}
1383
1384static const struct clk_ops ds3231_clk_sqw_ops = {
1385 .prepare = ds3231_clk_sqw_prepare,
1386 .unprepare = ds3231_clk_sqw_unprepare,
1387 .is_prepared = ds3231_clk_sqw_is_prepared,
1388 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1389 .round_rate = ds3231_clk_sqw_round_rate,
1390 .set_rate = ds3231_clk_sqw_set_rate,
1391};
1392
1393static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001394 unsigned long parent_rate)
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001395{
1396 return 32768;
1397}
1398
1399static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1400{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001401 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001402 int ret;
1403
1404 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001405 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1406 DS3231_BIT_EN32KHZ,
1407 enable ? DS3231_BIT_EN32KHZ : 0);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001408 mutex_unlock(lock);
1409
1410 return ret;
1411}
1412
1413static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1414{
1415 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1416
1417 return ds3231_clk_32khz_control(ds1307, true);
1418}
1419
1420static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1421{
1422 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1423
1424 ds3231_clk_32khz_control(ds1307, false);
1425}
1426
1427static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1428{
1429 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001430 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001431
Heiner Kallweit11e58902017-03-10 18:52:34 +01001432 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1433 if (ret)
1434 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001435
1436 return !!(status & DS3231_BIT_EN32KHZ);
1437}
1438
1439static const struct clk_ops ds3231_clk_32khz_ops = {
1440 .prepare = ds3231_clk_32khz_prepare,
1441 .unprepare = ds3231_clk_32khz_unprepare,
1442 .is_prepared = ds3231_clk_32khz_is_prepared,
1443 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1444};
1445
1446static struct clk_init_data ds3231_clks_init[] = {
1447 [DS3231_CLK_SQW] = {
1448 .name = "ds3231_clk_sqw",
1449 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001450 },
1451 [DS3231_CLK_32KHZ] = {
1452 .name = "ds3231_clk_32khz",
1453 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001454 },
1455};
1456
1457static int ds3231_clks_register(struct ds1307 *ds1307)
1458{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001459 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001460 struct clk_onecell_data *onecell;
1461 int i;
1462
Heiner Kallweit11e58902017-03-10 18:52:34 +01001463 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001464 if (!onecell)
1465 return -ENOMEM;
1466
1467 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001468 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1469 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001470 if (!onecell->clks)
1471 return -ENOMEM;
1472
1473 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1474 struct clk_init_data init = ds3231_clks_init[i];
1475
1476 /*
1477 * Interrupt signal due to alarm conditions and square-wave
1478 * output share same pin, so don't initialize both.
1479 */
1480 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1481 continue;
1482
1483 /* optional override of the clockname */
1484 of_property_read_string_index(node, "clock-output-names", i,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001485 &init.name);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001486 ds1307->clks[i].init = &init;
1487
Heiner Kallweit11e58902017-03-10 18:52:34 +01001488 onecell->clks[i] = devm_clk_register(ds1307->dev,
1489 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001490 if (IS_ERR(onecell->clks[i]))
1491 return PTR_ERR(onecell->clks[i]);
1492 }
1493
1494 if (!node)
1495 return 0;
1496
1497 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1498
1499 return 0;
1500}
1501
1502static void ds1307_clks_register(struct ds1307 *ds1307)
1503{
1504 int ret;
1505
1506 if (ds1307->type != ds_3231)
1507 return;
1508
1509 ret = ds3231_clks_register(ds1307);
1510 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001511 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1512 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001513 }
1514}
1515
1516#else
1517
1518static void ds1307_clks_register(struct ds1307 *ds1307)
1519{
1520}
1521
1522#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001523
Heiner Kallweit11e58902017-03-10 18:52:34 +01001524static const struct regmap_config regmap_config = {
1525 .reg_bits = 8,
1526 .val_bits = 8,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001527};
1528
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001529static int ds1307_probe(struct i2c_client *client,
1530 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001531{
1532 struct ds1307 *ds1307;
1533 int err = -ENODEV;
Heiner Kallweit584ce302017-08-29 21:52:56 +02001534 int tmp;
Heiner Kallweit7624df42017-07-12 07:49:33 +02001535 const struct chip_desc *chip;
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001536 bool want_irq;
Michael Lange8bc2a402016-01-21 18:10:16 +01001537 bool ds1307_can_wakeup_device = false;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001538 unsigned char regs[8];
Jingoo Han01ce8932013-11-12 15:10:41 -08001539 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001540 u8 trickle_charger_setup = 0;
David Brownell1abb0dc2006-06-25 05:48:17 -07001541
Jingoo Hanedca66d2013-07-03 15:07:05 -07001542 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001543 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001544 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001545
Heiner Kallweit11e58902017-03-10 18:52:34 +01001546 dev_set_drvdata(&client->dev, ds1307);
1547 ds1307->dev = &client->dev;
1548 ds1307->name = client->name;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001549
Heiner Kallweit11e58902017-03-10 18:52:34 +01001550 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1551 if (IS_ERR(ds1307->regmap)) {
1552 dev_err(ds1307->dev, "regmap allocation failed\n");
1553 return PTR_ERR(ds1307->regmap);
1554 }
1555
1556 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001557
1558 if (client->dev.of_node) {
1559 ds1307->type = (enum ds_type)
1560 of_device_get_match_data(&client->dev);
1561 chip = &chips[ds1307->type];
1562 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001563 chip = &chips[id->driver_data];
1564 ds1307->type = id->driver_data;
1565 } else {
1566 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001567
Tin Huynh9c19b892016-11-30 09:57:31 +07001568 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001569 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001570 if (!acpi_id)
1571 return -ENODEV;
1572 chip = &chips[acpi_id->driver_data];
1573 ds1307->type = acpi_id->driver_data;
1574 }
1575
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001576 want_irq = client->irq > 0 && chip->alarm;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001577
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001578 if (!pdata)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001579 trickle_charger_setup = ds1307_trickle_init(ds1307, chip);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001580 else if (pdata->trickle_charger_setup)
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001581 trickle_charger_setup = pdata->trickle_charger_setup;
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001582
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001583 if (trickle_charger_setup && chip->trickle_charger_reg) {
1584 trickle_charger_setup |= DS13XX_TRICKLE_CHARGER_MAGIC;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001585 dev_dbg(ds1307->dev,
1586 "writing trickle charger info 0x%x to 0x%x\n",
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001587 trickle_charger_setup, chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001588 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Heiner Kallweitd8490fd2017-07-12 07:49:28 +02001589 trickle_charger_setup);
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001590 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001591
Michael Lange8bc2a402016-01-21 18:10:16 +01001592#ifdef CONFIG_OF
1593/*
1594 * For devices with no IRQ directly connected to the SoC, the RTC chip
1595 * can be forced as a wakeup source by stating that explicitly in
1596 * the device's .dts file using the "wakeup-source" boolean property.
1597 * If the "wakeup-source" property is set, don't request an IRQ.
1598 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1599 * if supported by the RTC.
1600 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001601 if (chip->alarm && of_property_read_bool(client->dev.of_node,
1602 "wakeup-source"))
Michael Lange8bc2a402016-01-21 18:10:16 +01001603 ds1307_can_wakeup_device = true;
Michael Lange8bc2a402016-01-21 18:10:16 +01001604#endif
1605
David Brownell045e0e82007-07-17 04:04:55 -07001606 switch (ds1307->type) {
1607 case ds_1337:
1608 case ds_1339:
Nikita Yushchenko0759c882017-08-24 09:32:11 +03001609 case ds_1341:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001610 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001611 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001612 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001613 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001614 if (err) {
1615 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001616 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001617 }
1618
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001619 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001620 if (regs[0] & DS1337_BIT_nEOSC)
1621 regs[0] &= ~DS1337_BIT_nEOSC;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001622
David Anders40ce9722012-03-23 15:02:37 -07001623 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001624 * Using IRQ or defined as wakeup-source?
1625 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001626 * For some variants, be sure alarms can trigger when we're
1627 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001628 */
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001629 if (want_irq || ds1307_can_wakeup_device) {
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001630 regs[0] |= DS1337_BIT_INTCN | chip->bbsqi_bit;
1631 regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001632 }
1633
Heiner Kallweit11e58902017-03-10 18:52:34 +01001634 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001635 regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001636
1637 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001638 if (regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001639 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001640 regs[1] & ~DS1337_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001641 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001642 }
David Brownell045e0e82007-07-17 04:04:55 -07001643 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001644
1645 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001646 err = regmap_bulk_read(ds1307->regmap,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001647 RX8025_REG_CTRL1 << 4 | 0x08, regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001648 if (err) {
1649 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001650 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001651 }
1652
1653 /* oscillator off? turn it on, so clock can tick. */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001654 if (!(regs[1] & RX8025_BIT_XST)) {
1655 regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001656 regmap_write(ds1307->regmap,
1657 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001658 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001659 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001660 "oscillator stop detected - SET TIME!\n");
1661 }
1662
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001663 if (regs[1] & RX8025_BIT_PON) {
1664 regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001665 regmap_write(ds1307->regmap,
1666 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001667 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001668 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001669 }
1670
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001671 if (regs[1] & RX8025_BIT_VDET) {
1672 regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001673 regmap_write(ds1307->regmap,
1674 RX8025_REG_CTRL2 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001675 regs[1]);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001676 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001677 }
1678
1679 /* make sure we are running in 24hour mode */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001680 if (!(regs[0] & RX8025_BIT_2412)) {
Matthias Fuchsa2166852009-03-31 15:24:58 -07001681 u8 hour;
1682
1683 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001684 regmap_write(ds1307->regmap,
1685 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001686 regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001687
Heiner Kallweit11e58902017-03-10 18:52:34 +01001688 err = regmap_bulk_read(ds1307->regmap,
1689 RX8025_REG_CTRL1 << 4 | 0x08,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001690 regs, 2);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001691 if (err) {
1692 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001693 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001694 }
1695
1696 /* correct hour */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001697 hour = bcd2bin(regs[DS1307_REG_HOUR]);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001698 if (hour == 12)
1699 hour = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001700 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
Matthias Fuchsa2166852009-03-31 15:24:58 -07001701 hour += 12;
1702
Heiner Kallweit11e58902017-03-10 18:52:34 +01001703 regmap_write(ds1307->regmap,
1704 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001705 }
1706 break;
David Brownell045e0e82007-07-17 04:04:55 -07001707 default:
1708 break;
1709 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001710
1711read_rtc:
1712 /* read RTC registers */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001713 err = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
1714 sizeof(regs));
Heiner Kallweit11e58902017-03-10 18:52:34 +01001715 if (err) {
1716 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001717 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001718 }
1719
David Anders40ce9722012-03-23 15:02:37 -07001720 /*
1721 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001722 * specify the extra bits as must-be-zero, but there are
1723 * still a few values that are clearly out-of-range.
1724 */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001725 tmp = regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001726 switch (ds1307->type) {
1727 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001728 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001729 case m41t00:
Giulio Benetti7e580762018-05-16 23:08:40 +02001730 case m41t11:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001731 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001732 if (tmp & DS1307_BIT_CH) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001733 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1734 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001735 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001736 }
David Brownell045e0e82007-07-17 04:04:55 -07001737 break;
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001738 case ds_1308:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001739 case ds_1338:
1740 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001741 if (tmp & DS1307_BIT_CH)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001742 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001743
1744 /* oscillator fault? clear flag, and warn */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001745 if (regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001746 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
Alexandre Belloni4057a662017-09-04 22:46:06 +02001747 regs[DS1307_REG_CONTROL] &
1748 ~DS1338_BIT_OSF);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001749 dev_warn(ds1307->dev, "SET TIME!\n");
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001750 goto read_rtc;
1751 }
David Brownell045e0e82007-07-17 04:04:55 -07001752 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001753 case ds_1340:
1754 /* clock halted? turn it on, so clock can tick. */
1755 if (tmp & DS1340_BIT_nEOSC)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001756 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
frederic Rodofcd8db02008-02-06 01:38:55 -08001757
Heiner Kallweit11e58902017-03-10 18:52:34 +01001758 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1759 if (err) {
1760 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001761 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001762 }
1763
1764 /* oscillator fault? clear flag, and warn */
1765 if (tmp & DS1340_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001766 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1767 dev_warn(ds1307->dev, "SET TIME!\n");
frederic Rodofcd8db02008-02-06 01:38:55 -08001768 }
1769 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001770 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001771 /* make sure that the backup battery is enabled */
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001772 if (!(regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001773 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001774 regs[DS1307_REG_WDAY] |
Heiner Kallweit11e58902017-03-10 18:52:34 +01001775 MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001776 }
1777
1778 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001779 if (!(tmp & MCP794XX_BIT_ST)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001780 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1781 MCP794XX_BIT_ST);
1782 dev_warn(ds1307->dev, "SET TIME!\n");
David Anders43fcb812011-11-02 13:37:53 -07001783 goto read_rtc;
1784 }
1785
1786 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001787 default:
David Brownell045e0e82007-07-17 04:04:55 -07001788 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001789 }
David Brownell045e0e82007-07-17 04:04:55 -07001790
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001791 tmp = regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001792 switch (ds1307->type) {
1793 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001794 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001795 case m41t00:
Giulio Benetti7e580762018-05-16 23:08:40 +02001796 case m41t11:
David Anders40ce9722012-03-23 15:02:37 -07001797 /*
1798 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001799 * systems that will run through year 2100.
1800 */
1801 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001802 case rx_8025:
1803 break;
David Brownellc065f352007-07-17 04:05:10 -07001804 default:
1805 if (!(tmp & DS1307_BIT_12HR))
1806 break;
1807
David Anders40ce9722012-03-23 15:02:37 -07001808 /*
1809 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001810 * take note...
1811 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001812 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001813 if (tmp == 12)
1814 tmp = 0;
Alexandre Belloni042fa8c2017-09-04 22:46:02 +02001815 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
David Brownellc065f352007-07-17 04:05:10 -07001816 tmp += 12;
Heiner Kallweite5531702017-07-12 07:49:47 +02001817 regmap_write(ds1307->regmap, chip->offset + DS1307_REG_HOUR,
Heiner Kallweit11e58902017-03-10 18:52:34 +01001818 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001819 }
1820
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001821 if (want_irq || ds1307_can_wakeup_device) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001822 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001823 set_bit(HAS_ALARM, &ds1307->flags);
1824 }
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001825
1826 ds1307->rtc = devm_rtc_allocate_device(ds1307->dev);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001827 if (IS_ERR(ds1307->rtc))
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001828 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001829
Heiner Kallweit82e2d432017-07-12 07:49:37 +02001830 if (ds1307_can_wakeup_device && !want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001831 dev_info(ds1307->dev,
1832 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001833 /* We cannot support UIE mode if we do not have an IRQ line */
1834 ds1307->rtc->uie_unsupported = 1;
1835 }
1836
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001837 if (want_irq) {
Heiner Kallweit45947122017-07-12 07:49:41 +02001838 err = devm_request_threaded_irq(ds1307->dev, client->irq, NULL,
1839 chip->irq_handler ?: ds1307_irq,
Nishanth Menonc5983192015-06-23 11:15:11 -05001840 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001841 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001842 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001843 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001844 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001845 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001846 dev_err(ds1307->dev, "unable to request IRQ!\n");
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001847 } else {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001848 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Alexandre Bellonie69c0562017-09-04 22:46:07 +02001849 }
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001850 }
1851
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001852 ds1307->rtc->ops = chip->rtc_ops ?: &ds13xx_rtc_ops;
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001853 err = ds1307_add_frequency_test(ds1307);
Alexandre Bellonie9fb7682018-02-12 23:47:22 +01001854 if (err)
1855 return err;
1856
Alexandre Belloni6a5f2a1f2018-09-20 16:35:26 +02001857 err = rtc_register_device(ds1307->rtc);
Giulio Benettib41c23e2018-07-25 19:26:05 +02001858 if (err)
1859 return err;
1860
Austin Boyle9eab0a72012-03-23 15:02:38 -07001861 if (chip->nvram_size) {
Alexandre Belloni409baf12018-02-12 23:47:23 +01001862 struct nvmem_config nvmem_cfg = {
1863 .name = "ds1307_nvram",
1864 .word_size = 1,
1865 .stride = 1,
1866 .size = chip->nvram_size,
1867 .reg_read = ds1307_nvram_read,
1868 .reg_write = ds1307_nvram_write,
1869 .priv = ds1307,
1870 };
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001871
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001872 ds1307->rtc->nvram_old_abi = true;
Alexandre Belloni409baf12018-02-12 23:47:23 +01001873 rtc_nvmem_register(ds1307->rtc, &nvmem_cfg);
David Brownell682d73f2007-11-14 16:58:32 -08001874 }
1875
Akinobu Mita445c0202016-01-25 00:22:16 +09001876 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001877 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001878
David Brownell1abb0dc2006-06-25 05:48:17 -07001879 return 0;
1880
Jingoo Hanedca66d2013-07-03 15:07:05 -07001881exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001882 return err;
1883}
1884
David Brownell1abb0dc2006-06-25 05:48:17 -07001885static struct i2c_driver ds1307_driver = {
1886 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001887 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001888 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001889 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001890 },
David Brownellc065f352007-07-17 04:05:10 -07001891 .probe = ds1307_probe,
Jean Delvare3760f732008-04-29 23:11:40 +02001892 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001893};
1894
Axel Lin0abc9202012-03-23 15:02:31 -07001895module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001896
1897MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1898MODULE_LICENSE("GPL");