blob: adc90f188daef65c1e11aef34fab384a28610ce1 [file] [log] [blame]
David Brownell1abb0dc2006-06-25 05:48:17 -07001/*
2 * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips.
3 *
4 * Copyright (C) 2005 James Chapman (ds1337 core)
5 * Copyright (C) 2006 David Brownell
Matthias Fuchsa2166852009-03-31 15:24:58 -07006 * Copyright (C) 2009 Matthias Fuchs (rx8025 support)
Bertrand Achardbc48b902013-04-29 16:19:26 -07007 * Copyright (C) 2012 Bertrand Achard (nvram access fixes)
David Brownell1abb0dc2006-06-25 05:48:17 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Tin Huynh9c19b892016-11-30 09:57:31 +070014#include <linux/acpi.h>
David Brownell1abb0dc2006-06-25 05:48:17 -070015#include <linux/bcd.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050016#include <linux/i2c.h>
17#include <linux/init.h>
18#include <linux/module.h>
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -030019#include <linux/of_device.h>
Wolfram Sangeb86c302012-05-29 15:07:38 -070020#include <linux/rtc/ds1307.h>
Nishanth Menoneac72372015-06-23 11:15:12 -050021#include <linux/rtc.h>
22#include <linux/slab.h>
23#include <linux/string.h>
Akinobu Mita445c0202016-01-25 00:22:16 +090024#include <linux/hwmon.h>
25#include <linux/hwmon-sysfs.h>
Akinobu Mita6c6ff142016-01-31 23:10:10 +090026#include <linux/clk-provider.h>
Heiner Kallweit11e58902017-03-10 18:52:34 +010027#include <linux/regmap.h>
David Brownell1abb0dc2006-06-25 05:48:17 -070028
David Anders40ce9722012-03-23 15:02:37 -070029/*
30 * We can't determine type by probing, but if we expect pre-Linux code
David Brownell1abb0dc2006-06-25 05:48:17 -070031 * to have set the chip up as a clock (turning on the oscillator and
32 * setting the date and time), Linux can ignore the non-clock features.
33 * That's a natural job for a factory or repair bench.
David Brownell1abb0dc2006-06-25 05:48:17 -070034 */
35enum ds_type {
David Brownell045e0e82007-07-17 04:04:55 -070036 ds_1307,
Sean Nyekjaer300a7732017-06-08 12:36:54 +020037 ds_1308,
David Brownell045e0e82007-07-17 04:04:55 -070038 ds_1337,
39 ds_1338,
40 ds_1339,
41 ds_1340,
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -070042 ds_1388,
Wolfram Sang97f902b2009-06-17 16:26:10 -070043 ds_3231,
Stefan Agner8566f702017-03-23 16:54:57 -070044 m41t0,
David Brownell045e0e82007-07-17 04:04:55 -070045 m41t00,
Tomas Novotnyf4199f82014-12-10 15:53:57 -080046 mcp794xx,
Matthias Fuchsa2166852009-03-31 15:24:58 -070047 rx_8025,
Marek Vasutee0981b2017-06-18 22:55:28 +020048 rx_8130,
Wolfram Sang32d322b2012-03-23 15:02:36 -070049 last_ds_type /* always last */
David Anders40ce9722012-03-23 15:02:37 -070050 /* rs5c372 too? different address... */
David Brownell1abb0dc2006-06-25 05:48:17 -070051};
52
David Brownell1abb0dc2006-06-25 05:48:17 -070053
54/* RTC registers don't differ much, except for the century flag */
55#define DS1307_REG_SECS 0x00 /* 00-59 */
56# define DS1307_BIT_CH 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070057# define DS1340_BIT_nEOSC 0x80
Tomas Novotnyf4199f82014-12-10 15:53:57 -080058# define MCP794XX_BIT_ST 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070059#define DS1307_REG_MIN 0x01 /* 00-59 */
Stefan Agner8566f702017-03-23 16:54:57 -070060# define M41T0_BIT_OF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070061#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
David Brownellc065f352007-07-17 04:05:10 -070062# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
63# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
David Brownell1abb0dc2006-06-25 05:48:17 -070064# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
65# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
66#define DS1307_REG_WDAY 0x03 /* 01-07 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -080067# define MCP794XX_BIT_VBATEN 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -070068#define DS1307_REG_MDAY 0x04 /* 01-31 */
69#define DS1307_REG_MONTH 0x05 /* 01-12 */
70# define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
71#define DS1307_REG_YEAR 0x06 /* 00-99 */
72
David Anders40ce9722012-03-23 15:02:37 -070073/*
74 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
David Brownell045e0e82007-07-17 04:04:55 -070075 * start at 7, and they differ a LOT. Only control and status matter for
76 * basic RTC date and time functionality; be careful using them.
David Brownell1abb0dc2006-06-25 05:48:17 -070077 */
David Brownell045e0e82007-07-17 04:04:55 -070078#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
David Brownell1abb0dc2006-06-25 05:48:17 -070079# define DS1307_BIT_OUT 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070080# define DS1338_BIT_OSF 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070081# define DS1307_BIT_SQWE 0x10
82# define DS1307_BIT_RS1 0x02
83# define DS1307_BIT_RS0 0x01
84#define DS1337_REG_CONTROL 0x0e
85# define DS1337_BIT_nEOSC 0x80
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -070086# define DS1339_BIT_BBSQI 0x20
Wolfram Sang97f902b2009-06-17 16:26:10 -070087# define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
David Brownell1abb0dc2006-06-25 05:48:17 -070088# define DS1337_BIT_RS2 0x10
89# define DS1337_BIT_RS1 0x08
90# define DS1337_BIT_INTCN 0x04
91# define DS1337_BIT_A2IE 0x02
92# define DS1337_BIT_A1IE 0x01
David Brownell045e0e82007-07-17 04:04:55 -070093#define DS1340_REG_CONTROL 0x07
94# define DS1340_BIT_OUT 0x80
95# define DS1340_BIT_FT 0x40
96# define DS1340_BIT_CALIB_SIGN 0x20
97# define DS1340_M_CALIBRATION 0x1f
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070098#define DS1340_REG_FLAG 0x09
99# define DS1340_BIT_OSF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -0700100#define DS1337_REG_STATUS 0x0f
101# define DS1337_BIT_OSF 0x80
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900102# define DS3231_BIT_EN32KHZ 0x08
David Brownell1abb0dc2006-06-25 05:48:17 -0700103# define DS1337_BIT_A2I 0x02
104# define DS1337_BIT_A1I 0x01
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700105#define DS1339_REG_ALARM1_SECS 0x07
Wolfram Sangeb86c302012-05-29 15:07:38 -0700106
107#define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
David Brownell1abb0dc2006-06-25 05:48:17 -0700108
Matthias Fuchsa2166852009-03-31 15:24:58 -0700109#define RX8025_REG_CTRL1 0x0e
110# define RX8025_BIT_2412 0x20
111#define RX8025_REG_CTRL2 0x0f
112# define RX8025_BIT_PON 0x10
113# define RX8025_BIT_VDET 0x40
114# define RX8025_BIT_XST 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -0700115
116
117struct ds1307 {
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700118 u8 offset; /* register's offset */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700119 u8 regs[11];
Austin Boyle9eab0a72012-03-23 15:02:38 -0700120 u16 nvram_offset;
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200121 struct nvmem_config nvmem_cfg;
David Brownell1abb0dc2006-06-25 05:48:17 -0700122 enum ds_type type;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700123 unsigned long flags;
124#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
125#define HAS_ALARM 1 /* bit 1 == irq claimed */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100126 struct device *dev;
127 struct regmap *regmap;
128 const char *name;
David Brownell1abb0dc2006-06-25 05:48:17 -0700129 struct rtc_device *rtc;
Akinobu Mita6c6ff142016-01-31 23:10:10 +0900130#ifdef CONFIG_COMMON_CLK
131 struct clk_hw clks[2];
132#endif
David Brownell1abb0dc2006-06-25 05:48:17 -0700133};
134
David Brownell045e0e82007-07-17 04:04:55 -0700135struct chip_desc {
David Brownell045e0e82007-07-17 04:04:55 -0700136 unsigned alarm:1;
Austin Boyle9eab0a72012-03-23 15:02:38 -0700137 u16 nvram_offset;
138 u16 nvram_size;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200139 u8 century_reg;
140 u8 century_enable_bit;
141 u8 century_bit;
Wolfram Sangeb86c302012-05-29 15:07:38 -0700142 u16 trickle_charger_reg;
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700143 u8 trickle_charger_setup;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100144 u8 (*do_trickle_setup)(struct ds1307 *, uint32_t,
145 bool);
David Brownell045e0e82007-07-17 04:04:55 -0700146};
147
Heiner Kallweit11e58902017-03-10 18:52:34 +0100148static u8 do_trickle_setup_ds1339(struct ds1307 *, uint32_t ohms, bool diode);
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700149
150static struct chip_desc chips[last_ds_type] = {
Wolfram Sang32d322b2012-03-23 15:02:36 -0700151 [ds_1307] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700152 .nvram_offset = 8,
153 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700154 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200155 [ds_1308] = {
156 .nvram_offset = 8,
157 .nvram_size = 56,
158 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700159 [ds_1337] = {
160 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200161 .century_reg = DS1307_REG_MONTH,
162 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700163 },
164 [ds_1338] = {
Austin Boyle9eab0a72012-03-23 15:02:38 -0700165 .nvram_offset = 8,
166 .nvram_size = 56,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700167 },
168 [ds_1339] = {
169 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200170 .century_reg = DS1307_REG_MONTH,
171 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700172 .trickle_charger_reg = 0x10,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700173 .do_trickle_setup = &do_trickle_setup_ds1339,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700174 },
175 [ds_1340] = {
Heiner Kallweite48585d2017-06-05 17:57:33 +0200176 .century_reg = DS1307_REG_HOUR,
177 .century_enable_bit = DS1340_BIT_CENTURY_EN,
178 .century_bit = DS1340_BIT_CENTURY,
Wolfram Sangeb86c302012-05-29 15:07:38 -0700179 .trickle_charger_reg = 0x08,
180 },
181 [ds_1388] = {
182 .trickle_charger_reg = 0x0a,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700183 },
184 [ds_3231] = {
185 .alarm = 1,
Heiner Kallweite48585d2017-06-05 17:57:33 +0200186 .century_reg = DS1307_REG_MONTH,
187 .century_bit = DS1337_BIT_CENTURY,
Wolfram Sang32d322b2012-03-23 15:02:36 -0700188 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200189 [rx_8130] = {
190 .alarm = 1,
191 /* this is battery backed SRAM */
192 .nvram_offset = 0x20,
193 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
194 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800195 [mcp794xx] = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700196 .alarm = 1,
Austin Boyle9eab0a72012-03-23 15:02:38 -0700197 /* this is battery backed SRAM */
198 .nvram_offset = 0x20,
199 .nvram_size = 0x40,
200 },
Wolfram Sang32d322b2012-03-23 15:02:36 -0700201};
David Brownell045e0e82007-07-17 04:04:55 -0700202
Jean Delvare3760f732008-04-29 23:11:40 +0200203static const struct i2c_device_id ds1307_id[] = {
204 { "ds1307", ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200205 { "ds1308", ds_1308 },
Jean Delvare3760f732008-04-29 23:11:40 +0200206 { "ds1337", ds_1337 },
207 { "ds1338", ds_1338 },
208 { "ds1339", ds_1339 },
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -0700209 { "ds1388", ds_1388 },
Jean Delvare3760f732008-04-29 23:11:40 +0200210 { "ds1340", ds_1340 },
Wolfram Sang97f902b2009-06-17 16:26:10 -0700211 { "ds3231", ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700212 { "m41t0", m41t0 },
Jean Delvare3760f732008-04-29 23:11:40 +0200213 { "m41t00", m41t00 },
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800214 { "mcp7940x", mcp794xx },
215 { "mcp7941x", mcp794xx },
Priyanka Jain31c17712011-06-27 16:18:04 -0700216 { "pt7c4338", ds_1307 },
Matthias Fuchsa2166852009-03-31 15:24:58 -0700217 { "rx8025", rx_8025 },
Alexandre Belloni78aaa062016-07-13 02:36:41 +0200218 { "isl12057", ds_1337 },
Marek Vasutee0981b2017-06-18 22:55:28 +0200219 { "rx8130", rx_8130 },
Jean Delvare3760f732008-04-29 23:11:40 +0200220 { }
221};
222MODULE_DEVICE_TABLE(i2c, ds1307_id);
David Brownell1abb0dc2006-06-25 05:48:17 -0700223
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300224#ifdef CONFIG_OF
225static const struct of_device_id ds1307_of_match[] = {
226 {
227 .compatible = "dallas,ds1307",
228 .data = (void *)ds_1307
229 },
230 {
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200231 .compatible = "dallas,ds1308",
232 .data = (void *)ds_1308
233 },
234 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300235 .compatible = "dallas,ds1337",
236 .data = (void *)ds_1337
237 },
238 {
239 .compatible = "dallas,ds1338",
240 .data = (void *)ds_1338
241 },
242 {
243 .compatible = "dallas,ds1339",
244 .data = (void *)ds_1339
245 },
246 {
247 .compatible = "dallas,ds1388",
248 .data = (void *)ds_1388
249 },
250 {
251 .compatible = "dallas,ds1340",
252 .data = (void *)ds_1340
253 },
254 {
255 .compatible = "maxim,ds3231",
256 .data = (void *)ds_3231
257 },
258 {
Alexandre Bellonidb2f8142017-04-08 17:22:02 +0200259 .compatible = "st,m41t0",
260 .data = (void *)m41t00
261 },
262 {
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -0300263 .compatible = "st,m41t00",
264 .data = (void *)m41t00
265 },
266 {
267 .compatible = "microchip,mcp7940x",
268 .data = (void *)mcp794xx
269 },
270 {
271 .compatible = "microchip,mcp7941x",
272 .data = (void *)mcp794xx
273 },
274 {
275 .compatible = "pericom,pt7c4338",
276 .data = (void *)ds_1307
277 },
278 {
279 .compatible = "epson,rx8025",
280 .data = (void *)rx_8025
281 },
282 {
283 .compatible = "isil,isl12057",
284 .data = (void *)ds_1337
285 },
286 { }
287};
288MODULE_DEVICE_TABLE(of, ds1307_of_match);
289#endif
290
Tin Huynh9c19b892016-11-30 09:57:31 +0700291#ifdef CONFIG_ACPI
292static const struct acpi_device_id ds1307_acpi_ids[] = {
293 { .id = "DS1307", .driver_data = ds_1307 },
Sean Nyekjaer300a7732017-06-08 12:36:54 +0200294 { .id = "DS1308", .driver_data = ds_1308 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700295 { .id = "DS1337", .driver_data = ds_1337 },
296 { .id = "DS1338", .driver_data = ds_1338 },
297 { .id = "DS1339", .driver_data = ds_1339 },
298 { .id = "DS1388", .driver_data = ds_1388 },
299 { .id = "DS1340", .driver_data = ds_1340 },
300 { .id = "DS3231", .driver_data = ds_3231 },
Stefan Agner8566f702017-03-23 16:54:57 -0700301 { .id = "M41T0", .driver_data = m41t0 },
Tin Huynh9c19b892016-11-30 09:57:31 +0700302 { .id = "M41T00", .driver_data = m41t00 },
303 { .id = "MCP7940X", .driver_data = mcp794xx },
304 { .id = "MCP7941X", .driver_data = mcp794xx },
305 { .id = "PT7C4338", .driver_data = ds_1307 },
306 { .id = "RX8025", .driver_data = rx_8025 },
307 { .id = "ISL12057", .driver_data = ds_1337 },
308 { }
309};
310MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
311#endif
312
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700313/*
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700314 * The ds1337 and ds1339 both have two alarms, but we only use the first
315 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
316 * signal; ds1339 chips have only one alarm signal.
317 */
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500318static irqreturn_t ds1307_irq(int irq, void *dev_id)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700319{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100320 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500321 struct mutex *lock = &ds1307->rtc->ops_lock;
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200322 int stat, ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700323
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700324 mutex_lock(lock);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100325 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
326 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700327 goto out;
328
329 if (stat & DS1337_BIT_A1I) {
330 stat &= ~DS1337_BIT_A1I;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100331 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700332
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200333 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
334 DS1337_BIT_A1IE, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100335 if (ret)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700336 goto out;
337
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700338 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700339 }
340
341out:
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700342 mutex_unlock(lock);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700343
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700344 return IRQ_HANDLED;
345}
346
347/*----------------------------------------------------------------------*/
348
David Brownell1abb0dc2006-06-25 05:48:17 -0700349static int ds1307_get_time(struct device *dev, struct rtc_time *t)
350{
351 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100352 int tmp, ret;
Heiner Kallweite48585d2017-06-05 17:57:33 +0200353 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell1abb0dc2006-06-25 05:48:17 -0700354
David Brownell045e0e82007-07-17 04:04:55 -0700355 /* read the RTC date and time registers all at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100356 ret = regmap_bulk_read(ds1307->regmap, ds1307->offset, ds1307->regs, 7);
357 if (ret) {
358 dev_err(dev, "%s error %d\n", "read", ret);
359 return ret;
David Brownell1abb0dc2006-06-25 05:48:17 -0700360 }
361
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800362 dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
David Brownell1abb0dc2006-06-25 05:48:17 -0700363
Stefan Agner8566f702017-03-23 16:54:57 -0700364 /* if oscillator fail bit is set, no data can be trusted */
365 if (ds1307->type == m41t0 &&
366 ds1307->regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
367 dev_warn_once(dev, "oscillator failed, set time!\n");
368 return -EINVAL;
369 }
370
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700371 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
372 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700373 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700374 t->tm_hour = bcd2bin(tmp);
375 t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
376 t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
David Brownell1abb0dc2006-06-25 05:48:17 -0700377 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700378 t->tm_mon = bcd2bin(tmp) - 1;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700379 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
David Brownell1abb0dc2006-06-25 05:48:17 -0700380
Heiner Kallweite48585d2017-06-05 17:57:33 +0200381 if (ds1307->regs[chip->century_reg] & chip->century_bit &&
382 IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY))
383 t->tm_year += 100;
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200384
David Brownell1abb0dc2006-06-25 05:48:17 -0700385 dev_dbg(dev, "%s secs=%d, mins=%d, "
386 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
387 "read", t->tm_sec, t->tm_min,
388 t->tm_hour, t->tm_mday,
389 t->tm_mon, t->tm_year, t->tm_wday);
390
David Brownell045e0e82007-07-17 04:04:55 -0700391 /* initial clock setting can be undefined */
392 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700393}
394
395static int ds1307_set_time(struct device *dev, struct rtc_time *t)
396{
397 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Heiner Kallweite48585d2017-06-05 17:57:33 +0200398 const struct chip_desc *chip = &chips[ds1307->type];
David Brownell1abb0dc2006-06-25 05:48:17 -0700399 int result;
400 int tmp;
401 u8 *buf = ds1307->regs;
402
403 dev_dbg(dev, "%s secs=%d, mins=%d, "
404 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400405 "write", t->tm_sec, t->tm_min,
406 t->tm_hour, t->tm_mday,
407 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700408
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200409 if (t->tm_year < 100)
410 return -EINVAL;
411
Heiner Kallweite48585d2017-06-05 17:57:33 +0200412#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
413 if (t->tm_year > (chip->century_bit ? 299 : 199))
414 return -EINVAL;
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200415#else
Heiner Kallweite48585d2017-06-05 17:57:33 +0200416 if (t->tm_year > 199)
Alexandre Belloni50d6c0e2016-07-13 02:26:08 +0200417 return -EINVAL;
418#endif
419
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700420 buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
421 buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
422 buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
423 buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
424 buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
425 buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700426
427 /* assume 20YY not 19YY */
428 tmp = t->tm_year - 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700429 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
David Brownell1abb0dc2006-06-25 05:48:17 -0700430
Heiner Kallweite48585d2017-06-05 17:57:33 +0200431 if (chip->century_enable_bit)
432 buf[chip->century_reg] |= chip->century_enable_bit;
433 if (t->tm_year > 199 && chip->century_bit)
434 buf[chip->century_reg] |= chip->century_bit;
435
436 if (ds1307->type == mcp794xx) {
David Anders40ce9722012-03-23 15:02:37 -0700437 /*
438 * these bits were cleared when preparing the date/time
439 * values and need to be set again before writing the
440 * buffer out to the device.
441 */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800442 buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
443 buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700444 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700445
Andy Shevchenko01a4ca12013-02-21 16:44:22 -0800446 dev_dbg(dev, "%s: %7ph\n", "write", buf);
David Brownell1abb0dc2006-06-25 05:48:17 -0700447
Heiner Kallweit11e58902017-03-10 18:52:34 +0100448 result = regmap_bulk_write(ds1307->regmap, ds1307->offset, buf, 7);
449 if (result) {
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800450 dev_err(dev, "%s error %d\n", "write", result);
451 return result;
David Brownell1abb0dc2006-06-25 05:48:17 -0700452 }
453 return 0;
454}
455
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800456static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700457{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100458 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700459 int ret;
460
461 if (!test_bit(HAS_ALARM, &ds1307->flags))
462 return -EINVAL;
463
464 /* read all ALARM1, ALARM2, and status registers at once */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100465 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
466 ds1307->regs, 9);
467 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700468 dev_err(dev, "%s error %d\n", "alarm read", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100469 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700470 }
471
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100472 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
473 &ds1307->regs[0], &ds1307->regs[4], &ds1307->regs[7]);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700474
David Anders40ce9722012-03-23 15:02:37 -0700475 /*
476 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700477 * and that all four fields are checked matches
478 */
479 t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
480 t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
481 t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
482 t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700483
484 /* ... and status */
485 t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
486 t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
487
488 dev_dbg(dev, "%s secs=%d, mins=%d, "
489 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
490 "alarm read", t->time.tm_sec, t->time.tm_min,
491 t->time.tm_hour, t->time.tm_mday,
492 t->enabled, t->pending);
493
494 return 0;
495}
496
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800497static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700498{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100499 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700500 unsigned char *buf = ds1307->regs;
501 u8 control, status;
502 int ret;
503
504 if (!test_bit(HAS_ALARM, &ds1307->flags))
505 return -EINVAL;
506
507 dev_dbg(dev, "%s secs=%d, mins=%d, "
508 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
509 "alarm set", t->time.tm_sec, t->time.tm_min,
510 t->time.tm_hour, t->time.tm_mday,
511 t->enabled, t->pending);
512
513 /* read current status of both alarms and the chip */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100514 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, buf, 9);
515 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700516 dev_err(dev, "%s error %d\n", "alarm write", ret);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100517 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700518 }
519 control = ds1307->regs[7];
520 status = ds1307->regs[8];
521
Rasmus Villemoesff67abd2015-11-24 14:51:23 +0100522 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
523 &ds1307->regs[0], &ds1307->regs[4], control, status);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700524
525 /* set ALARM1, using 24 hour and day-of-month modes */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700526 buf[0] = bin2bcd(t->time.tm_sec);
527 buf[1] = bin2bcd(t->time.tm_min);
528 buf[2] = bin2bcd(t->time.tm_hour);
529 buf[3] = bin2bcd(t->time.tm_mday);
530
531 /* set ALARM2 to non-garbage */
532 buf[4] = 0;
533 buf[5] = 0;
534 buf[6] = 0;
535
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200536 /* disable alarms */
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700537 buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700538 buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
539
Heiner Kallweit11e58902017-03-10 18:52:34 +0100540 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, buf, 9);
541 if (ret) {
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700542 dev_err(dev, "can't set alarm time\n");
BARRE Sebastienfed40b72009-01-07 18:07:13 -0800543 return ret;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700544 }
545
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200546 /* optionally enable ALARM1 */
547 if (t->enabled) {
548 dev_dbg(dev, "alarm IRQ armed\n");
549 buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100550 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, buf[7]);
Nicolas Boullis5919fb92016-04-10 13:23:05 +0200551 }
552
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700553 return 0;
554}
555
John Stultz16380c12011-02-02 17:02:41 -0800556static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700557{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100558 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700559
John Stultz16380c12011-02-02 17:02:41 -0800560 if (!test_bit(HAS_ALARM, &ds1307->flags))
561 return -ENOTTY;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700562
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200563 return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
564 DS1337_BIT_A1IE,
565 enabled ? DS1337_BIT_A1IE : 0);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -0700566}
567
David Brownellff8371a2006-09-30 23:28:17 -0700568static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700569 .read_time = ds1307_get_time,
570 .set_time = ds1307_set_time,
Jüri Reitel74d88eb2009-01-07 18:07:16 -0800571 .read_alarm = ds1337_read_alarm,
572 .set_alarm = ds1337_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800573 .alarm_irq_enable = ds1307_alarm_irq_enable,
David Brownell1abb0dc2006-06-25 05:48:17 -0700574};
575
David Brownell682d73f2007-11-14 16:58:32 -0800576/*----------------------------------------------------------------------*/
577
Simon Guinot1d1945d2014-04-03 14:49:55 -0700578/*
Marek Vasutee0981b2017-06-18 22:55:28 +0200579 * Alarm support for rx8130 devices.
580 */
581
582#define RX8130_REG_ALARM_MIN 0x07
583#define RX8130_REG_ALARM_HOUR 0x08
584#define RX8130_REG_ALARM_WEEK_OR_DAY 0x09
585#define RX8130_REG_EXTENSION 0x0c
586#define RX8130_REG_EXTENSION_WADA (1 << 3)
587#define RX8130_REG_FLAG 0x0d
588#define RX8130_REG_FLAG_AF (1 << 3)
589#define RX8130_REG_CONTROL0 0x0e
590#define RX8130_REG_CONTROL0_AIE (1 << 3)
591
592static irqreturn_t rx8130_irq(int irq, void *dev_id)
593{
594 struct ds1307 *ds1307 = dev_id;
595 struct mutex *lock = &ds1307->rtc->ops_lock;
596 u8 ctl[3];
597 int ret;
598
599 mutex_lock(lock);
600
601 /* Read control registers. */
602 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
603 if (ret < 0)
604 goto out;
605 if (!(ctl[1] & RX8130_REG_FLAG_AF))
606 goto out;
607 ctl[1] &= ~RX8130_REG_FLAG_AF;
608 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
609
610 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
611 if (ret < 0)
612 goto out;
613
614 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
615
616out:
617 mutex_unlock(lock);
618
619 return IRQ_HANDLED;
620}
621
622static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
623{
624 struct ds1307 *ds1307 = dev_get_drvdata(dev);
625 u8 ald[3], ctl[3];
626 int ret;
627
628 if (!test_bit(HAS_ALARM, &ds1307->flags))
629 return -EINVAL;
630
631 /* Read alarm registers. */
632 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, 3);
633 if (ret < 0)
634 return ret;
635
636 /* Read control registers. */
637 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
638 if (ret < 0)
639 return ret;
640
641 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
642 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
643
644 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
645 t->time.tm_sec = -1;
646 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
647 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
648 t->time.tm_wday = -1;
649 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
650 t->time.tm_mon = -1;
651 t->time.tm_year = -1;
652 t->time.tm_yday = -1;
653 t->time.tm_isdst = -1;
654
655 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
656 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
657 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
658
659 return 0;
660}
661
662static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
663{
664 struct ds1307 *ds1307 = dev_get_drvdata(dev);
665 u8 ald[3], ctl[3];
666 int ret;
667
668 if (!test_bit(HAS_ALARM, &ds1307->flags))
669 return -EINVAL;
670
671 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
672 "enabled=%d pending=%d\n", __func__,
673 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
674 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
675 t->enabled, t->pending);
676
677 /* Read control registers. */
678 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
679 if (ret < 0)
680 return ret;
681
682 ctl[0] &= ~RX8130_REG_EXTENSION_WADA;
683 ctl[1] |= RX8130_REG_FLAG_AF;
684 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
685
686 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
687 if (ret < 0)
688 return ret;
689
690 /* Hardware alarm precision is 1 minute! */
691 ald[0] = bin2bcd(t->time.tm_min);
692 ald[1] = bin2bcd(t->time.tm_hour);
693 ald[2] = bin2bcd(t->time.tm_mday);
694
695 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, 3);
696 if (ret < 0)
697 return ret;
698
699 if (!t->enabled)
700 return 0;
701
702 ctl[2] |= RX8130_REG_CONTROL0_AIE;
703
704 return regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, 3);
705}
706
707static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
708{
709 struct ds1307 *ds1307 = dev_get_drvdata(dev);
710 int ret, reg;
711
712 if (!test_bit(HAS_ALARM, &ds1307->flags))
713 return -EINVAL;
714
715 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, &reg);
716 if (ret < 0)
717 return ret;
718
719 if (enabled)
720 reg |= RX8130_REG_CONTROL0_AIE;
721 else
722 reg &= ~RX8130_REG_CONTROL0_AIE;
723
724 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
725}
726
727static const struct rtc_class_ops rx8130_rtc_ops = {
728 .read_time = ds1307_get_time,
729 .set_time = ds1307_set_time,
730 .read_alarm = rx8130_read_alarm,
731 .set_alarm = rx8130_set_alarm,
732 .alarm_irq_enable = rx8130_alarm_irq_enable,
733};
734
735/*----------------------------------------------------------------------*/
736
737/*
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800738 * Alarm support for mcp794xx devices.
Simon Guinot1d1945d2014-04-03 14:49:55 -0700739 */
740
Keerthye29385f2016-06-01 16:19:07 +0530741#define MCP794XX_REG_WEEKDAY 0x3
742#define MCP794XX_REG_WEEKDAY_WDAY_MASK 0x7
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800743#define MCP794XX_REG_CONTROL 0x07
744# define MCP794XX_BIT_ALM0_EN 0x10
745# define MCP794XX_BIT_ALM1_EN 0x20
746#define MCP794XX_REG_ALARM0_BASE 0x0a
747#define MCP794XX_REG_ALARM0_CTRL 0x0d
748#define MCP794XX_REG_ALARM1_BASE 0x11
749#define MCP794XX_REG_ALARM1_CTRL 0x14
750# define MCP794XX_BIT_ALMX_IF (1 << 3)
751# define MCP794XX_BIT_ALMX_C0 (1 << 4)
752# define MCP794XX_BIT_ALMX_C1 (1 << 5)
753# define MCP794XX_BIT_ALMX_C2 (1 << 6)
754# define MCP794XX_BIT_ALMX_POL (1 << 7)
755# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
756 MCP794XX_BIT_ALMX_C1 | \
757 MCP794XX_BIT_ALMX_C2)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700758
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500759static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700760{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100761 struct ds1307 *ds1307 = dev_id;
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500762 struct mutex *lock = &ds1307->rtc->ops_lock;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700763 int reg, ret;
764
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500765 mutex_lock(lock);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700766
767 /* Check and clear alarm 0 interrupt flag. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100768 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, &reg);
769 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700770 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800771 if (!(reg & MCP794XX_BIT_ALMX_IF))
Simon Guinot1d1945d2014-04-03 14:49:55 -0700772 goto out;
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800773 reg &= ~MCP794XX_BIT_ALMX_IF;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100774 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
775 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700776 goto out;
777
778 /* Disable alarm 0. */
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200779 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
780 MCP794XX_BIT_ALM0_EN, 0);
Heiner Kallweit11e58902017-03-10 18:52:34 +0100781 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700782 goto out;
783
784 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
785
786out:
Felipe Balbi2fb07a12015-06-23 11:15:10 -0500787 mutex_unlock(lock);
788
789 return IRQ_HANDLED;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700790}
791
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800792static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700793{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100794 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700795 u8 *regs = ds1307->regs;
796 int ret;
797
798 if (!test_bit(HAS_ALARM, &ds1307->flags))
799 return -EINVAL;
800
801 /* Read control and alarm 0 registers. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100802 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
803 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700804 return ret;
805
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800806 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700807
808 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
809 t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f);
810 t->time.tm_min = bcd2bin(ds1307->regs[4] & 0x7f);
811 t->time.tm_hour = bcd2bin(ds1307->regs[5] & 0x3f);
812 t->time.tm_wday = bcd2bin(ds1307->regs[6] & 0x7) - 1;
813 t->time.tm_mday = bcd2bin(ds1307->regs[7] & 0x3f);
814 t->time.tm_mon = bcd2bin(ds1307->regs[8] & 0x1f) - 1;
815 t->time.tm_year = -1;
816 t->time.tm_yday = -1;
817 t->time.tm_isdst = -1;
818
819 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
820 "enabled=%d polarity=%d irq=%d match=%d\n", __func__,
821 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
822 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800823 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL),
824 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF),
825 (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700826
827 return 0;
828}
829
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800830static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700831{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100832 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700833 unsigned char *regs = ds1307->regs;
834 int ret;
835
836 if (!test_bit(HAS_ALARM, &ds1307->flags))
837 return -EINVAL;
838
839 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
840 "enabled=%d pending=%d\n", __func__,
841 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
842 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
843 t->enabled, t->pending);
844
845 /* Read control and alarm 0 registers. */
Heiner Kallweit11e58902017-03-10 18:52:34 +0100846 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
847 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700848 return ret;
849
850 /* Set alarm 0, using 24-hour and day-of-month modes. */
851 regs[3] = bin2bcd(t->time.tm_sec);
852 regs[4] = bin2bcd(t->time.tm_min);
853 regs[5] = bin2bcd(t->time.tm_hour);
Tero Kristo62c8c202015-10-23 09:29:57 +0300854 regs[6] = bin2bcd(t->time.tm_wday + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700855 regs[7] = bin2bcd(t->time.tm_mday);
Tero Kristo62c8c202015-10-23 09:29:57 +0300856 regs[8] = bin2bcd(t->time.tm_mon + 1);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700857
858 /* Clear the alarm 0 interrupt flag. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800859 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700860 /* Set alarm match: second, minute, hour, day, date, month. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800861 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
Nishanth Menone3edd672015-04-20 19:51:34 -0500862 /* Disable interrupt. We will not enable until completely programmed */
863 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
Simon Guinot1d1945d2014-04-03 14:49:55 -0700864
Heiner Kallweit11e58902017-03-10 18:52:34 +0100865 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs, 10);
866 if (ret)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700867 return ret;
868
Nishanth Menone3edd672015-04-20 19:51:34 -0500869 if (!t->enabled)
870 return 0;
871 regs[0] |= MCP794XX_BIT_ALM0_EN;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100872 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700873}
874
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800875static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
Simon Guinot1d1945d2014-04-03 14:49:55 -0700876{
Heiner Kallweit11e58902017-03-10 18:52:34 +0100877 struct ds1307 *ds1307 = dev_get_drvdata(dev);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700878
879 if (!test_bit(HAS_ALARM, &ds1307->flags))
880 return -EINVAL;
881
Heiner Kallweit078f3f62017-06-05 17:57:29 +0200882 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
883 MCP794XX_BIT_ALM0_EN,
884 enabled ? MCP794XX_BIT_ALM0_EN : 0);
Simon Guinot1d1945d2014-04-03 14:49:55 -0700885}
886
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800887static const struct rtc_class_ops mcp794xx_rtc_ops = {
Simon Guinot1d1945d2014-04-03 14:49:55 -0700888 .read_time = ds1307_get_time,
889 .set_time = ds1307_set_time,
Tomas Novotnyf4199f82014-12-10 15:53:57 -0800890 .read_alarm = mcp794xx_read_alarm,
891 .set_alarm = mcp794xx_set_alarm,
892 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
Simon Guinot1d1945d2014-04-03 14:49:55 -0700893};
894
895/*----------------------------------------------------------------------*/
896
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200897static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
898 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -0800899{
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200900 struct ds1307 *ds1307 = priv;
David Brownell682d73f2007-11-14 16:58:32 -0800901
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200902 return regmap_bulk_read(ds1307->regmap, ds1307->nvram_offset + offset,
903 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -0800904}
905
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200906static int ds1307_nvram_write(void *priv, unsigned int offset, void *val,
907 size_t bytes)
David Brownell682d73f2007-11-14 16:58:32 -0800908{
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200909 struct ds1307 *ds1307 = priv;
David Brownell682d73f2007-11-14 16:58:32 -0800910
Alexandre Belloniabc925f2017-07-06 11:42:07 +0200911 return regmap_bulk_write(ds1307->regmap, ds1307->nvram_offset + offset,
912 val, bytes);
David Brownell682d73f2007-11-14 16:58:32 -0800913}
914
David Brownell682d73f2007-11-14 16:58:32 -0800915/*----------------------------------------------------------------------*/
916
Heiner Kallweit11e58902017-03-10 18:52:34 +0100917static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700918 uint32_t ohms, bool diode)
919{
920 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
921 DS1307_TRICKLE_CHARGER_NO_DIODE;
922
923 switch (ohms) {
924 case 250:
925 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
926 break;
927 case 2000:
928 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
929 break;
930 case 4000:
931 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
932 break;
933 default:
Heiner Kallweit11e58902017-03-10 18:52:34 +0100934 dev_warn(ds1307->dev,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700935 "Unsupported ohm value %u in dt\n", ohms);
936 return 0;
937 }
938 return setup;
939}
940
Heiner Kallweit11e58902017-03-10 18:52:34 +0100941static void ds1307_trickle_init(struct ds1307 *ds1307,
Tin Huynh9c19b892016-11-30 09:57:31 +0700942 struct chip_desc *chip)
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700943{
944 uint32_t ohms = 0;
945 bool diode = true;
946
947 if (!chip->do_trickle_setup)
948 goto out;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100949 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
950 &ohms))
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700951 goto out;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100952 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700953 diode = false;
Heiner Kallweit11e58902017-03-10 18:52:34 +0100954 chip->trickle_charger_setup = chip->do_trickle_setup(ds1307,
Matti Vaittinen33b04b72014-10-13 15:52:48 -0700955 ohms, diode);
956out:
957 return;
958}
959
Akinobu Mita445c0202016-01-25 00:22:16 +0900960/*----------------------------------------------------------------------*/
961
962#ifdef CONFIG_RTC_DRV_DS1307_HWMON
963
964/*
965 * Temperature sensor support for ds3231 devices.
966 */
967
968#define DS3231_REG_TEMPERATURE 0x11
969
970/*
971 * A user-initiated temperature conversion is not started by this function,
972 * so the temperature is updated once every 64 seconds.
973 */
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +0900974static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
Akinobu Mita445c0202016-01-25 00:22:16 +0900975{
976 struct ds1307 *ds1307 = dev_get_drvdata(dev);
977 u8 temp_buf[2];
978 s16 temp;
979 int ret;
980
Heiner Kallweit11e58902017-03-10 18:52:34 +0100981 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
982 temp_buf, sizeof(temp_buf));
983 if (ret)
Akinobu Mita445c0202016-01-25 00:22:16 +0900984 return ret;
Akinobu Mita445c0202016-01-25 00:22:16 +0900985 /*
986 * Temperature is represented as a 10-bit code with a resolution of
987 * 0.25 degree celsius and encoded in two's complement format.
988 */
989 temp = (temp_buf[0] << 8) | temp_buf[1];
990 temp >>= 6;
991 *mC = temp * 250;
992
993 return 0;
994}
995
996static ssize_t ds3231_hwmon_show_temp(struct device *dev,
997 struct device_attribute *attr, char *buf)
998{
999 int ret;
Zhuang Yuyao9a3dce62016-04-18 09:21:42 +09001000 s32 temp;
Akinobu Mita445c0202016-01-25 00:22:16 +09001001
1002 ret = ds3231_hwmon_read_temp(dev, &temp);
1003 if (ret)
1004 return ret;
1005
1006 return sprintf(buf, "%d\n", temp);
1007}
1008static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ds3231_hwmon_show_temp,
1009 NULL, 0);
1010
1011static struct attribute *ds3231_hwmon_attrs[] = {
1012 &sensor_dev_attr_temp1_input.dev_attr.attr,
1013 NULL,
1014};
1015ATTRIBUTE_GROUPS(ds3231_hwmon);
1016
1017static void ds1307_hwmon_register(struct ds1307 *ds1307)
1018{
1019 struct device *dev;
1020
1021 if (ds1307->type != ds_3231)
1022 return;
1023
Heiner Kallweit11e58902017-03-10 18:52:34 +01001024 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
Akinobu Mita445c0202016-01-25 00:22:16 +09001025 ds1307, ds3231_hwmon_groups);
1026 if (IS_ERR(dev)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001027 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1028 PTR_ERR(dev));
Akinobu Mita445c0202016-01-25 00:22:16 +09001029 }
1030}
1031
1032#else
1033
1034static void ds1307_hwmon_register(struct ds1307 *ds1307)
1035{
1036}
1037
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001038#endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1039
1040/*----------------------------------------------------------------------*/
1041
1042/*
1043 * Square-wave output support for DS3231
1044 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1045 */
1046#ifdef CONFIG_COMMON_CLK
1047
1048enum {
1049 DS3231_CLK_SQW = 0,
1050 DS3231_CLK_32KHZ,
1051};
1052
1053#define clk_sqw_to_ds1307(clk) \
1054 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1055#define clk_32khz_to_ds1307(clk) \
1056 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1057
1058static int ds3231_clk_sqw_rates[] = {
1059 1,
1060 1024,
1061 4096,
1062 8192,
1063};
1064
1065static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1066{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001067 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001068 int ret;
1069
1070 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001071 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1072 mask, value);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001073 mutex_unlock(lock);
1074
1075 return ret;
1076}
1077
1078static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1079 unsigned long parent_rate)
1080{
1081 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001082 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001083 int rate_sel = 0;
1084
Heiner Kallweit11e58902017-03-10 18:52:34 +01001085 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1086 if (ret)
1087 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001088 if (control & DS1337_BIT_RS1)
1089 rate_sel += 1;
1090 if (control & DS1337_BIT_RS2)
1091 rate_sel += 2;
1092
1093 return ds3231_clk_sqw_rates[rate_sel];
1094}
1095
1096static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
1097 unsigned long *prate)
1098{
1099 int i;
1100
1101 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1102 if (ds3231_clk_sqw_rates[i] <= rate)
1103 return ds3231_clk_sqw_rates[i];
1104 }
1105
1106 return 0;
1107}
1108
1109static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
1110 unsigned long parent_rate)
1111{
1112 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1113 int control = 0;
1114 int rate_sel;
1115
1116 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1117 rate_sel++) {
1118 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1119 break;
1120 }
1121
1122 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1123 return -EINVAL;
1124
1125 if (rate_sel & 1)
1126 control |= DS1337_BIT_RS1;
1127 if (rate_sel & 2)
1128 control |= DS1337_BIT_RS2;
1129
1130 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1131 control);
1132}
1133
1134static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1135{
1136 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1137
1138 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1139}
1140
1141static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1142{
1143 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1144
1145 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1146}
1147
1148static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1149{
1150 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001151 int control, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001152
Heiner Kallweit11e58902017-03-10 18:52:34 +01001153 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1154 if (ret)
1155 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001156
1157 return !(control & DS1337_BIT_INTCN);
1158}
1159
1160static const struct clk_ops ds3231_clk_sqw_ops = {
1161 .prepare = ds3231_clk_sqw_prepare,
1162 .unprepare = ds3231_clk_sqw_unprepare,
1163 .is_prepared = ds3231_clk_sqw_is_prepared,
1164 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1165 .round_rate = ds3231_clk_sqw_round_rate,
1166 .set_rate = ds3231_clk_sqw_set_rate,
1167};
1168
1169static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
1170 unsigned long parent_rate)
1171{
1172 return 32768;
1173}
1174
1175static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1176{
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001177 struct mutex *lock = &ds1307->rtc->ops_lock;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001178 int ret;
1179
1180 mutex_lock(lock);
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001181 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1182 DS3231_BIT_EN32KHZ,
1183 enable ? DS3231_BIT_EN32KHZ : 0);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001184 mutex_unlock(lock);
1185
1186 return ret;
1187}
1188
1189static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1190{
1191 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1192
1193 return ds3231_clk_32khz_control(ds1307, true);
1194}
1195
1196static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1197{
1198 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1199
1200 ds3231_clk_32khz_control(ds1307, false);
1201}
1202
1203static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1204{
1205 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001206 int status, ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001207
Heiner Kallweit11e58902017-03-10 18:52:34 +01001208 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1209 if (ret)
1210 return ret;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001211
1212 return !!(status & DS3231_BIT_EN32KHZ);
1213}
1214
1215static const struct clk_ops ds3231_clk_32khz_ops = {
1216 .prepare = ds3231_clk_32khz_prepare,
1217 .unprepare = ds3231_clk_32khz_unprepare,
1218 .is_prepared = ds3231_clk_32khz_is_prepared,
1219 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1220};
1221
1222static struct clk_init_data ds3231_clks_init[] = {
1223 [DS3231_CLK_SQW] = {
1224 .name = "ds3231_clk_sqw",
1225 .ops = &ds3231_clk_sqw_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001226 },
1227 [DS3231_CLK_32KHZ] = {
1228 .name = "ds3231_clk_32khz",
1229 .ops = &ds3231_clk_32khz_ops,
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001230 },
1231};
1232
1233static int ds3231_clks_register(struct ds1307 *ds1307)
1234{
Heiner Kallweit11e58902017-03-10 18:52:34 +01001235 struct device_node *node = ds1307->dev->of_node;
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001236 struct clk_onecell_data *onecell;
1237 int i;
1238
Heiner Kallweit11e58902017-03-10 18:52:34 +01001239 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001240 if (!onecell)
1241 return -ENOMEM;
1242
1243 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001244 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1245 sizeof(onecell->clks[0]), GFP_KERNEL);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001246 if (!onecell->clks)
1247 return -ENOMEM;
1248
1249 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1250 struct clk_init_data init = ds3231_clks_init[i];
1251
1252 /*
1253 * Interrupt signal due to alarm conditions and square-wave
1254 * output share same pin, so don't initialize both.
1255 */
1256 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1257 continue;
1258
1259 /* optional override of the clockname */
1260 of_property_read_string_index(node, "clock-output-names", i,
1261 &init.name);
1262 ds1307->clks[i].init = &init;
1263
Heiner Kallweit11e58902017-03-10 18:52:34 +01001264 onecell->clks[i] = devm_clk_register(ds1307->dev,
1265 &ds1307->clks[i]);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001266 if (IS_ERR(onecell->clks[i]))
1267 return PTR_ERR(onecell->clks[i]);
1268 }
1269
1270 if (!node)
1271 return 0;
1272
1273 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1274
1275 return 0;
1276}
1277
1278static void ds1307_clks_register(struct ds1307 *ds1307)
1279{
1280 int ret;
1281
1282 if (ds1307->type != ds_3231)
1283 return;
1284
1285 ret = ds3231_clks_register(ds1307);
1286 if (ret) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001287 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1288 ret);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001289 }
1290}
1291
1292#else
1293
1294static void ds1307_clks_register(struct ds1307 *ds1307)
1295{
1296}
1297
1298#endif /* CONFIG_COMMON_CLK */
Akinobu Mita445c0202016-01-25 00:22:16 +09001299
Heiner Kallweit11e58902017-03-10 18:52:34 +01001300static const struct regmap_config regmap_config = {
1301 .reg_bits = 8,
1302 .val_bits = 8,
1303 .max_register = 0x12,
1304};
1305
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001306static int ds1307_probe(struct i2c_client *client,
1307 const struct i2c_device_id *id)
David Brownell1abb0dc2006-06-25 05:48:17 -07001308{
1309 struct ds1307 *ds1307;
1310 int err = -ENODEV;
Keerthye29385f2016-06-01 16:19:07 +05301311 int tmp, wday;
Tin Huynh9c19b892016-11-30 09:57:31 +07001312 struct chip_desc *chip;
Peter Senna Tschudinc8b18da2013-11-12 15:10:59 -08001313 bool want_irq = false;
Michael Lange8bc2a402016-01-21 18:10:16 +01001314 bool ds1307_can_wakeup_device = false;
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001315 unsigned char *buf;
Jingoo Han01ce8932013-11-12 15:10:41 -08001316 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
Keerthye29385f2016-06-01 16:19:07 +05301317 struct rtc_time tm;
1318 unsigned long timestamp;
1319
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001320 irq_handler_t irq_handler = ds1307_irq;
1321
Wolfram Sang97f902b2009-06-17 16:26:10 -07001322 static const int bbsqi_bitpos[] = {
1323 [ds_1337] = 0,
1324 [ds_1339] = DS1339_BIT_BBSQI,
1325 [ds_3231] = DS3231_BIT_BBSQW,
1326 };
Simon Guinot1d1945d2014-04-03 14:49:55 -07001327 const struct rtc_class_ops *rtc_ops = &ds13xx_rtc_ops;
David Brownell1abb0dc2006-06-25 05:48:17 -07001328
Jingoo Hanedca66d2013-07-03 15:07:05 -07001329 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
David Anders40ce9722012-03-23 15:02:37 -07001330 if (!ds1307)
David Brownellc065f352007-07-17 04:05:10 -07001331 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -07001332
Heiner Kallweit11e58902017-03-10 18:52:34 +01001333 dev_set_drvdata(&client->dev, ds1307);
1334 ds1307->dev = &client->dev;
1335 ds1307->name = client->name;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001336
Heiner Kallweit11e58902017-03-10 18:52:34 +01001337 ds1307->regmap = devm_regmap_init_i2c(client, &regmap_config);
1338 if (IS_ERR(ds1307->regmap)) {
1339 dev_err(ds1307->dev, "regmap allocation failed\n");
1340 return PTR_ERR(ds1307->regmap);
1341 }
1342
1343 i2c_set_clientdata(client, ds1307);
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001344
1345 if (client->dev.of_node) {
1346 ds1307->type = (enum ds_type)
1347 of_device_get_match_data(&client->dev);
1348 chip = &chips[ds1307->type];
1349 } else if (id) {
Tin Huynh9c19b892016-11-30 09:57:31 +07001350 chip = &chips[id->driver_data];
1351 ds1307->type = id->driver_data;
1352 } else {
1353 const struct acpi_device_id *acpi_id;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001354
Tin Huynh9c19b892016-11-30 09:57:31 +07001355 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
Heiner Kallweit11e58902017-03-10 18:52:34 +01001356 ds1307->dev);
Tin Huynh9c19b892016-11-30 09:57:31 +07001357 if (!acpi_id)
1358 return -ENODEV;
1359 chip = &chips[acpi_id->driver_data];
1360 ds1307->type = acpi_id->driver_data;
1361 }
1362
1363 if (!pdata)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001364 ds1307_trickle_init(ds1307, chip);
Tin Huynh9c19b892016-11-30 09:57:31 +07001365 else if (pdata->trickle_charger_setup)
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001366 chip->trickle_charger_setup = pdata->trickle_charger_setup;
1367
1368 if (chip->trickle_charger_setup && chip->trickle_charger_reg) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001369 dev_dbg(ds1307->dev,
1370 "writing trickle charger info 0x%x to 0x%x\n",
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001371 DS13XX_TRICKLE_CHARGER_MAGIC | chip->trickle_charger_setup,
1372 chip->trickle_charger_reg);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001373 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
Matti Vaittinen33b04b72014-10-13 15:52:48 -07001374 DS13XX_TRICKLE_CHARGER_MAGIC |
1375 chip->trickle_charger_setup);
1376 }
Wolfram Sangeb86c302012-05-29 15:07:38 -07001377
BARRE Sebastienfed40b72009-01-07 18:07:13 -08001378 buf = ds1307->regs;
David Brownell045e0e82007-07-17 04:04:55 -07001379
Michael Lange8bc2a402016-01-21 18:10:16 +01001380#ifdef CONFIG_OF
1381/*
1382 * For devices with no IRQ directly connected to the SoC, the RTC chip
1383 * can be forced as a wakeup source by stating that explicitly in
1384 * the device's .dts file using the "wakeup-source" boolean property.
1385 * If the "wakeup-source" property is set, don't request an IRQ.
1386 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1387 * if supported by the RTC.
1388 */
Heiner Kallweit11909f02017-07-06 22:40:03 +02001389 if (of_property_read_bool(client->dev.of_node, "wakeup-source"))
Michael Lange8bc2a402016-01-21 18:10:16 +01001390 ds1307_can_wakeup_device = true;
Michael Lange8bc2a402016-01-21 18:10:16 +01001391#endif
1392
David Brownell045e0e82007-07-17 04:04:55 -07001393 switch (ds1307->type) {
1394 case ds_1337:
1395 case ds_1339:
Wolfram Sang97f902b2009-06-17 16:26:10 -07001396 case ds_3231:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001397 /* get registers that the "rtc" read below won't read... */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001398 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
1399 buf, 2);
1400 if (err) {
1401 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001402 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001403 }
1404
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001405 /* oscillator off? turn it on, so clock can tick. */
1406 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001407 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
1408
David Anders40ce9722012-03-23 15:02:37 -07001409 /*
Michael Lange8bc2a402016-01-21 18:10:16 +01001410 * Using IRQ or defined as wakeup-source?
1411 * Disable the square wave and both alarms.
Wolfram Sang97f902b2009-06-17 16:26:10 -07001412 * For some variants, be sure alarms can trigger when we're
1413 * running on Vbackup (BBSQI/BBSQW)
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001414 */
Heiner Kallweit340fd7b2017-07-12 07:49:14 +02001415 if (chip->alarm && (client->irq > 0 ||
Heiner Kallweit11e58902017-03-10 18:52:34 +01001416 ds1307_can_wakeup_device)) {
Wolfram Sang97f902b2009-06-17 16:26:10 -07001417 ds1307->regs[0] |= DS1337_BIT_INTCN
1418 | bbsqi_bitpos[ds1307->type];
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001419 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
Wolfram Sangb24a7262012-03-23 15:02:37 -07001420
1421 want_irq = true;
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001422 }
1423
Heiner Kallweit11e58902017-03-10 18:52:34 +01001424 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
1425 ds1307->regs[0]);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001426
1427 /* oscillator fault? clear flag, and warn */
1428 if (ds1307->regs[1] & DS1337_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001429 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
1430 ds1307->regs[1] & ~DS1337_BIT_OSF);
1431 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -07001432 }
David Brownell045e0e82007-07-17 04:04:55 -07001433 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001434
1435 case rx_8025:
Heiner Kallweit11e58902017-03-10 18:52:34 +01001436 err = regmap_bulk_read(ds1307->regmap,
1437 RX8025_REG_CTRL1 << 4 | 0x08, buf, 2);
1438 if (err) {
1439 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001440 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001441 }
1442
1443 /* oscillator off? turn it on, so clock can tick. */
1444 if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
1445 ds1307->regs[1] |= RX8025_BIT_XST;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001446 regmap_write(ds1307->regmap,
1447 RX8025_REG_CTRL2 << 4 | 0x08,
1448 ds1307->regs[1]);
1449 dev_warn(ds1307->dev,
Matthias Fuchsa2166852009-03-31 15:24:58 -07001450 "oscillator stop detected - SET TIME!\n");
1451 }
1452
1453 if (ds1307->regs[1] & RX8025_BIT_PON) {
1454 ds1307->regs[1] &= ~RX8025_BIT_PON;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001455 regmap_write(ds1307->regmap,
1456 RX8025_REG_CTRL2 << 4 | 0x08,
1457 ds1307->regs[1]);
1458 dev_warn(ds1307->dev, "power-on detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001459 }
1460
1461 if (ds1307->regs[1] & RX8025_BIT_VDET) {
1462 ds1307->regs[1] &= ~RX8025_BIT_VDET;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001463 regmap_write(ds1307->regmap,
1464 RX8025_REG_CTRL2 << 4 | 0x08,
1465 ds1307->regs[1]);
1466 dev_warn(ds1307->dev, "voltage drop detected\n");
Matthias Fuchsa2166852009-03-31 15:24:58 -07001467 }
1468
1469 /* make sure we are running in 24hour mode */
1470 if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
1471 u8 hour;
1472
1473 /* switch to 24 hour mode */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001474 regmap_write(ds1307->regmap,
1475 RX8025_REG_CTRL1 << 4 | 0x08,
1476 ds1307->regs[0] | RX8025_BIT_2412);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001477
Heiner Kallweit11e58902017-03-10 18:52:34 +01001478 err = regmap_bulk_read(ds1307->regmap,
1479 RX8025_REG_CTRL1 << 4 | 0x08,
1480 buf, 2);
1481 if (err) {
1482 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001483 goto exit;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001484 }
1485
1486 /* correct hour */
1487 hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
1488 if (hour == 12)
1489 hour = 0;
1490 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1491 hour += 12;
1492
Heiner Kallweit11e58902017-03-10 18:52:34 +01001493 regmap_write(ds1307->regmap,
1494 DS1307_REG_HOUR << 4 | 0x08, hour);
Matthias Fuchsa2166852009-03-31 15:24:58 -07001495 }
1496 break;
Marek Vasutee0981b2017-06-18 22:55:28 +02001497 case rx_8130:
1498 ds1307->offset = 0x10; /* Seconds starts at 0x10 */
1499 rtc_ops = &rx8130_rtc_ops;
Heiner Kallweit340fd7b2017-07-12 07:49:14 +02001500 if (chip->alarm && client->irq > 0) {
Marek Vasutee0981b2017-06-18 22:55:28 +02001501 irq_handler = rx8130_irq;
1502 want_irq = true;
1503 }
1504 break;
Joakim Tjernlund33df2ee2009-06-17 16:26:08 -07001505 case ds_1388:
1506 ds1307->offset = 1; /* Seconds starts at 1 */
1507 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001508 case mcp794xx:
1509 rtc_ops = &mcp794xx_rtc_ops;
Heiner Kallweit340fd7b2017-07-12 07:49:14 +02001510 if (chip->alarm && (client->irq > 0 ||
David Lowe80663602017-04-22 18:28:00 +01001511 ds1307_can_wakeup_device)) {
Felipe Balbi2fb07a12015-06-23 11:15:10 -05001512 irq_handler = mcp794xx_irq;
Simon Guinot1d1945d2014-04-03 14:49:55 -07001513 want_irq = true;
1514 }
1515 break;
David Brownell045e0e82007-07-17 04:04:55 -07001516 default:
1517 break;
1518 }
David Brownell1abb0dc2006-06-25 05:48:17 -07001519
1520read_rtc:
1521 /* read RTC registers */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001522 err = regmap_bulk_read(ds1307->regmap, ds1307->offset, buf, 8);
1523 if (err) {
1524 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001525 goto exit;
David Brownell1abb0dc2006-06-25 05:48:17 -07001526 }
1527
David Anders40ce9722012-03-23 15:02:37 -07001528 /*
1529 * minimal sanity checking; some chips (like DS1340) don't
David Brownell1abb0dc2006-06-25 05:48:17 -07001530 * specify the extra bits as must-be-zero, but there are
1531 * still a few values that are clearly out-of-range.
1532 */
1533 tmp = ds1307->regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -07001534 switch (ds1307->type) {
1535 case ds_1307:
Stefan Agner8566f702017-03-23 16:54:57 -07001536 case m41t0:
David Brownell045e0e82007-07-17 04:04:55 -07001537 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001538 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001539 if (tmp & DS1307_BIT_CH) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001540 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1541 dev_warn(ds1307->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -07001542 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -07001543 }
David Brownell045e0e82007-07-17 04:04:55 -07001544 break;
Sean Nyekjaer300a7732017-06-08 12:36:54 +02001545 case ds_1308:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001546 case ds_1338:
1547 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -07001548 if (tmp & DS1307_BIT_CH)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001549 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001550
1551 /* oscillator fault? clear flag, and warn */
1552 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001553 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
1554 ds1307->regs[DS1307_REG_CONTROL] &
1555 ~DS1338_BIT_OSF);
1556 dev_warn(ds1307->dev, "SET TIME!\n");
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -07001557 goto read_rtc;
1558 }
David Brownell045e0e82007-07-17 04:04:55 -07001559 break;
frederic Rodofcd8db02008-02-06 01:38:55 -08001560 case ds_1340:
1561 /* clock halted? turn it on, so clock can tick. */
1562 if (tmp & DS1340_BIT_nEOSC)
Heiner Kallweit11e58902017-03-10 18:52:34 +01001563 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
frederic Rodofcd8db02008-02-06 01:38:55 -08001564
Heiner Kallweit11e58902017-03-10 18:52:34 +01001565 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1566 if (err) {
1567 dev_dbg(ds1307->dev, "read error %d\n", err);
Jingoo Hanedca66d2013-07-03 15:07:05 -07001568 goto exit;
frederic Rodofcd8db02008-02-06 01:38:55 -08001569 }
1570
1571 /* oscillator fault? clear flag, and warn */
1572 if (tmp & DS1340_BIT_OSF) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001573 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1574 dev_warn(ds1307->dev, "SET TIME!\n");
frederic Rodofcd8db02008-02-06 01:38:55 -08001575 }
1576 break;
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001577 case mcp794xx:
David Anders43fcb812011-11-02 13:37:53 -07001578 /* make sure that the backup battery is enabled */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001579 if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001580 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
1581 ds1307->regs[DS1307_REG_WDAY] |
1582 MCP794XX_BIT_VBATEN);
David Anders43fcb812011-11-02 13:37:53 -07001583 }
1584
1585 /* clock halted? turn it on, so clock can tick. */
Tomas Novotnyf4199f82014-12-10 15:53:57 -08001586 if (!(tmp & MCP794XX_BIT_ST)) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001587 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1588 MCP794XX_BIT_ST);
1589 dev_warn(ds1307->dev, "SET TIME!\n");
David Anders43fcb812011-11-02 13:37:53 -07001590 goto read_rtc;
1591 }
1592
1593 break;
Wolfram Sang32d322b2012-03-23 15:02:36 -07001594 default:
David Brownell045e0e82007-07-17 04:04:55 -07001595 break;
David Brownell1abb0dc2006-06-25 05:48:17 -07001596 }
David Brownell045e0e82007-07-17 04:04:55 -07001597
David Brownell1abb0dc2006-06-25 05:48:17 -07001598 tmp = ds1307->regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -07001599 switch (ds1307->type) {
1600 case ds_1340:
Stefan Agner8566f702017-03-23 16:54:57 -07001601 case m41t0:
David Brownellc065f352007-07-17 04:05:10 -07001602 case m41t00:
David Anders40ce9722012-03-23 15:02:37 -07001603 /*
1604 * NOTE: ignores century bits; fix before deploying
David Brownellc065f352007-07-17 04:05:10 -07001605 * systems that will run through year 2100.
1606 */
1607 break;
Matthias Fuchsa2166852009-03-31 15:24:58 -07001608 case rx_8025:
1609 break;
David Brownellc065f352007-07-17 04:05:10 -07001610 default:
1611 if (!(tmp & DS1307_BIT_12HR))
1612 break;
1613
David Anders40ce9722012-03-23 15:02:37 -07001614 /*
1615 * Be sure we're in 24 hour mode. Multi-master systems
David Brownellc065f352007-07-17 04:05:10 -07001616 * take note...
1617 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -07001618 tmp = bcd2bin(tmp & 0x1f);
David Brownellc065f352007-07-17 04:05:10 -07001619 if (tmp == 12)
1620 tmp = 0;
1621 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1622 tmp += 12;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001623 regmap_write(ds1307->regmap, ds1307->offset + DS1307_REG_HOUR,
1624 bin2bcd(tmp));
David Brownell1abb0dc2006-06-25 05:48:17 -07001625 }
1626
Keerthye29385f2016-06-01 16:19:07 +05301627 /*
1628 * Some IPs have weekday reset value = 0x1 which might not correct
1629 * hence compute the wday using the current date/month/year values
1630 */
Heiner Kallweit11e58902017-03-10 18:52:34 +01001631 ds1307_get_time(ds1307->dev, &tm);
Keerthye29385f2016-06-01 16:19:07 +05301632 wday = tm.tm_wday;
1633 timestamp = rtc_tm_to_time64(&tm);
1634 rtc_time64_to_tm(timestamp, &tm);
1635
1636 /*
1637 * Check if reset wday is different from the computed wday
1638 * If different then set the wday which we computed using
1639 * timestamp
1640 */
Heiner Kallweit078f3f62017-06-05 17:57:29 +02001641 if (wday != tm.tm_wday)
1642 regmap_update_bits(ds1307->regmap, MCP794XX_REG_WEEKDAY,
1643 MCP794XX_REG_WEEKDAY_WDAY_MASK,
1644 tm.tm_wday + 1);
Keerthye29385f2016-06-01 16:19:07 +05301645
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001646 if (want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001647 device_set_wakeup_capable(ds1307->dev, true);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001648 set_bit(HAS_ALARM, &ds1307->flags);
1649 }
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001650
1651 ds1307->rtc = devm_rtc_allocate_device(ds1307->dev);
David Brownell1abb0dc2006-06-25 05:48:17 -07001652 if (IS_ERR(ds1307->rtc)) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001653 return PTR_ERR(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -07001654 }
1655
Heiner Kallweit340fd7b2017-07-12 07:49:14 +02001656 if (ds1307_can_wakeup_device && client->irq <= 0) {
Michael Lange8bc2a402016-01-21 18:10:16 +01001657 /* Disable request for an IRQ */
1658 want_irq = false;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001659 dev_info(ds1307->dev,
1660 "'wakeup-source' is set, request for an IRQ is disabled!\n");
Michael Lange8bc2a402016-01-21 18:10:16 +01001661 /* We cannot support UIE mode if we do not have an IRQ line */
1662 ds1307->rtc->uie_unsupported = 1;
1663 }
1664
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001665 if (want_irq) {
Heiner Kallweit11e58902017-03-10 18:52:34 +01001666 err = devm_request_threaded_irq(ds1307->dev,
Heiner Kallweit340fd7b2017-07-12 07:49:14 +02001667 client->irq, NULL, irq_handler,
Nishanth Menonc5983192015-06-23 11:15:11 -05001668 IRQF_SHARED | IRQF_ONESHOT,
Alexandre Belloni4b9e2a02017-06-02 14:13:21 +02001669 ds1307->name, ds1307);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001670 if (err) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001671 client->irq = 0;
Heiner Kallweit11e58902017-03-10 18:52:34 +01001672 device_set_wakeup_capable(ds1307->dev, false);
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001673 clear_bit(HAS_ALARM, &ds1307->flags);
Heiner Kallweit11e58902017-03-10 18:52:34 +01001674 dev_err(ds1307->dev, "unable to request IRQ!\n");
Simon Guinot3abb1ad2015-11-26 15:37:13 +01001675 } else
Heiner Kallweit11e58902017-03-10 18:52:34 +01001676 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
Rodolfo Giometticb49a5e2008-10-15 22:02:58 -07001677 }
1678
Austin Boyle9eab0a72012-03-23 15:02:38 -07001679 if (chip->nvram_size) {
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001680 ds1307->nvmem_cfg.name = "ds1307_nvram";
1681 ds1307->nvmem_cfg.word_size = 1;
1682 ds1307->nvmem_cfg.stride = 1;
1683 ds1307->nvmem_cfg.size = chip->nvram_size;
1684 ds1307->nvmem_cfg.reg_read = ds1307_nvram_read;
1685 ds1307->nvmem_cfg.reg_write = ds1307_nvram_write;
1686 ds1307->nvmem_cfg.priv = ds1307;
1687 ds1307->nvram_offset = chip->nvram_offset;
Alessandro Zummo4071ea22014-04-03 14:49:36 -07001688
Alexandre Belloniabc925f2017-07-06 11:42:07 +02001689 ds1307->rtc->nvmem_config = &ds1307->nvmem_cfg;
1690 ds1307->rtc->nvram_old_abi = true;
David Brownell682d73f2007-11-14 16:58:32 -08001691 }
1692
Alexandre Belloni69b119a2017-07-06 11:42:06 +02001693 ds1307->rtc->ops = rtc_ops;
1694 err = rtc_register_device(ds1307->rtc);
1695 if (err)
1696 return err;
1697
Akinobu Mita445c0202016-01-25 00:22:16 +09001698 ds1307_hwmon_register(ds1307);
Akinobu Mita6c6ff142016-01-31 23:10:10 +09001699 ds1307_clks_register(ds1307);
Akinobu Mita445c0202016-01-25 00:22:16 +09001700
David Brownell1abb0dc2006-06-25 05:48:17 -07001701 return 0;
1702
Jingoo Hanedca66d2013-07-03 15:07:05 -07001703exit:
David Brownell1abb0dc2006-06-25 05:48:17 -07001704 return err;
1705}
1706
David Brownell1abb0dc2006-06-25 05:48:17 -07001707static struct i2c_driver ds1307_driver = {
1708 .driver = {
David Brownellc065f352007-07-17 04:05:10 -07001709 .name = "rtc-ds1307",
Javier Martinez Canillas7ef6d2c2017-03-03 11:29:15 -03001710 .of_match_table = of_match_ptr(ds1307_of_match),
Tin Huynh9c19b892016-11-30 09:57:31 +07001711 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
David Brownell1abb0dc2006-06-25 05:48:17 -07001712 },
David Brownellc065f352007-07-17 04:05:10 -07001713 .probe = ds1307_probe,
Jean Delvare3760f732008-04-29 23:11:40 +02001714 .id_table = ds1307_id,
David Brownell1abb0dc2006-06-25 05:48:17 -07001715};
1716
Axel Lin0abc9202012-03-23 15:02:31 -07001717module_i2c_driver(ds1307_driver);
David Brownell1abb0dc2006-06-25 05:48:17 -07001718
1719MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1720MODULE_LICENSE("GPL");