David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1 | /* |
| 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 Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 6 | * Copyright (C) 2009 Matthias Fuchs (rx8025 support) |
Bertrand Achard | bc48b90 | 2013-04-29 16:19:26 -0700 | [diff] [blame] | 7 | * Copyright (C) 2012 Bertrand Achard (nvram access fixes) |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 8 | * |
| 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 Huynh | 9c19b89 | 2016-11-30 09:57:31 +0700 | [diff] [blame] | 14 | #include <linux/acpi.h> |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 15 | #include <linux/bcd.h> |
Nishanth Menon | eac7237 | 2015-06-23 11:15:12 -0500 | [diff] [blame] | 16 | #include <linux/i2c.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/module.h> |
Javier Martinez Canillas | 7ef6d2c | 2017-03-03 11:29:15 -0300 | [diff] [blame] | 19 | #include <linux/of_device.h> |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 20 | #include <linux/rtc/ds1307.h> |
Nishanth Menon | eac7237 | 2015-06-23 11:15:12 -0500 | [diff] [blame] | 21 | #include <linux/rtc.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/string.h> |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 24 | #include <linux/hwmon.h> |
| 25 | #include <linux/hwmon-sysfs.h> |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 26 | #include <linux/clk-provider.h> |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 27 | #include <linux/regmap.h> |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 28 | |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 29 | /* |
| 30 | * We can't determine type by probing, but if we expect pre-Linux code |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 31 | * 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 Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 34 | */ |
| 35 | enum ds_type { |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 36 | ds_1307, |
Sean Nyekjaer | 300a773 | 2017-06-08 12:36:54 +0200 | [diff] [blame] | 37 | ds_1308, |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 38 | ds_1337, |
| 39 | ds_1338, |
| 40 | ds_1339, |
| 41 | ds_1340, |
Nikita Yushchenko | 0759c88 | 2017-08-24 09:32:11 +0300 | [diff] [blame] | 42 | ds_1341, |
Joakim Tjernlund | 33df2ee | 2009-06-17 16:26:08 -0700 | [diff] [blame] | 43 | ds_1388, |
Wolfram Sang | 97f902b | 2009-06-17 16:26:10 -0700 | [diff] [blame] | 44 | ds_3231, |
Stefan Agner | 8566f70 | 2017-03-23 16:54:57 -0700 | [diff] [blame] | 45 | m41t0, |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 46 | m41t00, |
Giulio Benetti | 7e58076 | 2018-05-16 23:08:40 +0200 | [diff] [blame] | 47 | m41t11, |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 48 | mcp794xx, |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 49 | rx_8025, |
Marek Vasut | ee0981b | 2017-06-18 22:55:28 +0200 | [diff] [blame] | 50 | rx_8130, |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 51 | last_ds_type /* always last */ |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 52 | /* rs5c372 too? different address... */ |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 55 | /* 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 Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 58 | # define DS1340_BIT_nEOSC 0x80 |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 59 | # define MCP794XX_BIT_ST 0x80 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 60 | #define DS1307_REG_MIN 0x01 /* 00-59 */ |
Stefan Agner | 8566f70 | 2017-03-23 16:54:57 -0700 | [diff] [blame] | 61 | # define M41T0_BIT_OF 0x80 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 62 | #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */ |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 63 | # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */ |
| 64 | # define DS1307_BIT_PM 0x20 /* in REG_HOUR */ |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 65 | # 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 Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 68 | # define MCP794XX_BIT_VBATEN 0x08 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 69 | #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 Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 74 | /* |
| 75 | * Other registers (control, status, alarms, trickle charge, NVRAM, etc) |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 76 | * 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 Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 78 | */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 79 | #define DS1307_REG_CONTROL 0x07 /* or ds1338 */ |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 80 | # define DS1307_BIT_OUT 0x80 |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 81 | # define DS1338_BIT_OSF 0x20 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 82 | # 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 Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 87 | # define DS1339_BIT_BBSQI 0x20 |
Wolfram Sang | 97f902b | 2009-06-17 16:26:10 -0700 | [diff] [blame] | 88 | # define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */ |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 89 | # 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 Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 94 | #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 Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 99 | #define DS1340_REG_FLAG 0x09 |
| 100 | # define DS1340_BIT_OSF 0x80 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 101 | #define DS1337_REG_STATUS 0x0f |
| 102 | # define DS1337_BIT_OSF 0x80 |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 103 | # define DS3231_BIT_EN32KHZ 0x08 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 104 | # define DS1337_BIT_A2I 0x02 |
| 105 | # define DS1337_BIT_A1I 0x01 |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 106 | #define DS1339_REG_ALARM1_SECS 0x07 |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 107 | |
| 108 | #define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 109 | |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 110 | #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 Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 116 | |
Uwe Kleine-König | 3ffd4a2 | 2019-01-25 15:35:56 +0100 | [diff] [blame] | 117 | #define RX8130_REG_ALARM_MIN 0x17 |
| 118 | #define RX8130_REG_ALARM_HOUR 0x18 |
| 119 | #define RX8130_REG_ALARM_WEEK_OR_DAY 0x19 |
| 120 | #define RX8130_REG_EXTENSION 0x1c |
Uwe Kleine-König | 92cbf12 | 2019-01-25 15:35:54 +0100 | [diff] [blame] | 121 | #define RX8130_REG_EXTENSION_WADA BIT(3) |
Uwe Kleine-König | 3ffd4a2 | 2019-01-25 15:35:56 +0100 | [diff] [blame] | 122 | #define RX8130_REG_FLAG 0x1d |
| 123 | #define RX8130_REG_FLAG_VLF BIT(1) |
Uwe Kleine-König | 92cbf12 | 2019-01-25 15:35:54 +0100 | [diff] [blame] | 124 | #define RX8130_REG_FLAG_AF BIT(3) |
Uwe Kleine-König | 3ffd4a2 | 2019-01-25 15:35:56 +0100 | [diff] [blame] | 125 | #define RX8130_REG_CONTROL0 0x1e |
Uwe Kleine-König | 92cbf12 | 2019-01-25 15:35:54 +0100 | [diff] [blame] | 126 | #define RX8130_REG_CONTROL0_AIE BIT(3) |
| 127 | |
| 128 | #define MCP794XX_REG_CONTROL 0x07 |
| 129 | # define MCP794XX_BIT_ALM0_EN 0x10 |
| 130 | # define MCP794XX_BIT_ALM1_EN 0x20 |
| 131 | #define MCP794XX_REG_ALARM0_BASE 0x0a |
| 132 | #define MCP794XX_REG_ALARM0_CTRL 0x0d |
| 133 | #define MCP794XX_REG_ALARM1_BASE 0x11 |
| 134 | #define MCP794XX_REG_ALARM1_CTRL 0x14 |
| 135 | # define MCP794XX_BIT_ALMX_IF BIT(3) |
| 136 | # define MCP794XX_BIT_ALMX_C0 BIT(4) |
| 137 | # define MCP794XX_BIT_ALMX_C1 BIT(5) |
| 138 | # define MCP794XX_BIT_ALMX_C2 BIT(6) |
| 139 | # define MCP794XX_BIT_ALMX_POL BIT(7) |
| 140 | # define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \ |
| 141 | MCP794XX_BIT_ALMX_C1 | \ |
| 142 | MCP794XX_BIT_ALMX_C2) |
| 143 | |
Giulio Benetti | 79230ff | 2018-07-25 19:26:04 +0200 | [diff] [blame] | 144 | #define M41TXX_REG_CONTROL 0x07 |
| 145 | # define M41TXX_BIT_OUT BIT(7) |
| 146 | # define M41TXX_BIT_FT BIT(6) |
| 147 | # define M41TXX_BIT_CALIB_SIGN BIT(5) |
| 148 | # define M41TXX_M_CALIBRATION GENMASK(4, 0) |
| 149 | |
| 150 | /* negative offset step is -2.034ppm */ |
| 151 | #define M41TXX_NEG_OFFSET_STEP_PPB 2034 |
| 152 | /* positive offset step is +4.068ppm */ |
| 153 | #define M41TXX_POS_OFFSET_STEP_PPB 4068 |
| 154 | /* Min and max values supported with 'offset' interface by M41TXX */ |
| 155 | #define M41TXX_MIN_OFFSET ((-31) * M41TXX_NEG_OFFSET_STEP_PPB) |
| 156 | #define M41TXX_MAX_OFFSET ((31) * M41TXX_POS_OFFSET_STEP_PPB) |
| 157 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 158 | struct ds1307 { |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 159 | enum ds_type type; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 160 | unsigned long flags; |
| 161 | #define HAS_NVRAM 0 /* bit 0 == sysfs file active */ |
| 162 | #define HAS_ALARM 1 /* bit 1 == irq claimed */ |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 163 | struct device *dev; |
| 164 | struct regmap *regmap; |
| 165 | const char *name; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 166 | struct rtc_device *rtc; |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 167 | #ifdef CONFIG_COMMON_CLK |
| 168 | struct clk_hw clks[2]; |
| 169 | #endif |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 172 | struct chip_desc { |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 173 | unsigned alarm:1; |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 174 | u16 nvram_offset; |
| 175 | u16 nvram_size; |
Heiner Kallweit | e553170 | 2017-07-12 07:49:47 +0200 | [diff] [blame] | 176 | u8 offset; /* register's offset */ |
Heiner Kallweit | e48585d | 2017-06-05 17:57:33 +0200 | [diff] [blame] | 177 | u8 century_reg; |
| 178 | u8 century_enable_bit; |
| 179 | u8 century_bit; |
Heiner Kallweit | 0b6ee80 | 2017-07-12 07:49:22 +0200 | [diff] [blame] | 180 | u8 bbsqi_bit; |
Heiner Kallweit | 4594712 | 2017-07-12 07:49:41 +0200 | [diff] [blame] | 181 | irq_handler_t irq_handler; |
Heiner Kallweit | 1efb98b | 2017-07-12 07:49:44 +0200 | [diff] [blame] | 182 | const struct rtc_class_ops *rtc_ops; |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 183 | u16 trickle_charger_reg; |
Alexandre Belloni | 57ec2d9 | 2017-09-04 22:46:04 +0200 | [diff] [blame] | 184 | u8 (*do_trickle_setup)(struct ds1307 *, u32, |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 185 | bool); |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
Uwe Kleine-König | d0e3f61 | 2019-01-25 15:35:55 +0100 | [diff] [blame] | 188 | static const struct chip_desc chips[last_ds_type]; |
| 189 | |
| 190 | static int ds1307_get_time(struct device *dev, struct rtc_time *t) |
| 191 | { |
| 192 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 193 | int tmp, ret; |
| 194 | const struct chip_desc *chip = &chips[ds1307->type]; |
| 195 | u8 regs[7]; |
| 196 | |
Uwe Kleine-König | 501f982 | 2019-01-25 15:35:57 +0100 | [diff] [blame] | 197 | if (ds1307->type == rx_8130) { |
| 198 | unsigned int regflag; |
| 199 | ret = regmap_read(ds1307->regmap, RX8130_REG_FLAG, ®flag); |
| 200 | if (ret) { |
| 201 | dev_err(dev, "%s error %d\n", "read", ret); |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | if (regflag & RX8130_REG_FLAG_VLF) { |
| 206 | dev_warn_once(dev, "oscillator failed, set time!\n"); |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | } |
| 210 | |
Uwe Kleine-König | d0e3f61 | 2019-01-25 15:35:55 +0100 | [diff] [blame] | 211 | /* read the RTC date and time registers all at once */ |
| 212 | ret = regmap_bulk_read(ds1307->regmap, chip->offset, regs, |
| 213 | sizeof(regs)); |
| 214 | if (ret) { |
| 215 | dev_err(dev, "%s error %d\n", "read", ret); |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | dev_dbg(dev, "%s: %7ph\n", "read", regs); |
| 220 | |
| 221 | /* if oscillator fail bit is set, no data can be trusted */ |
| 222 | if (ds1307->type == m41t0 && |
| 223 | regs[DS1307_REG_MIN] & M41T0_BIT_OF) { |
| 224 | dev_warn_once(dev, "oscillator failed, set time!\n"); |
| 225 | return -EINVAL; |
| 226 | } |
| 227 | |
| 228 | t->tm_sec = bcd2bin(regs[DS1307_REG_SECS] & 0x7f); |
| 229 | t->tm_min = bcd2bin(regs[DS1307_REG_MIN] & 0x7f); |
| 230 | tmp = regs[DS1307_REG_HOUR] & 0x3f; |
| 231 | t->tm_hour = bcd2bin(tmp); |
| 232 | t->tm_wday = bcd2bin(regs[DS1307_REG_WDAY] & 0x07) - 1; |
| 233 | t->tm_mday = bcd2bin(regs[DS1307_REG_MDAY] & 0x3f); |
| 234 | tmp = regs[DS1307_REG_MONTH] & 0x1f; |
| 235 | t->tm_mon = bcd2bin(tmp) - 1; |
| 236 | t->tm_year = bcd2bin(regs[DS1307_REG_YEAR]) + 100; |
| 237 | |
| 238 | if (regs[chip->century_reg] & chip->century_bit && |
| 239 | IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY)) |
| 240 | t->tm_year += 100; |
| 241 | |
| 242 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 243 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
| 244 | "read", t->tm_sec, t->tm_min, |
| 245 | t->tm_hour, t->tm_mday, |
| 246 | t->tm_mon, t->tm_year, t->tm_wday); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static int ds1307_set_time(struct device *dev, struct rtc_time *t) |
| 252 | { |
| 253 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 254 | const struct chip_desc *chip = &chips[ds1307->type]; |
| 255 | int result; |
| 256 | int tmp; |
| 257 | u8 regs[7]; |
| 258 | |
| 259 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 260 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
| 261 | "write", t->tm_sec, t->tm_min, |
| 262 | t->tm_hour, t->tm_mday, |
| 263 | t->tm_mon, t->tm_year, t->tm_wday); |
| 264 | |
| 265 | if (t->tm_year < 100) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | #ifdef CONFIG_RTC_DRV_DS1307_CENTURY |
| 269 | if (t->tm_year > (chip->century_bit ? 299 : 199)) |
| 270 | return -EINVAL; |
| 271 | #else |
| 272 | if (t->tm_year > 199) |
| 273 | return -EINVAL; |
| 274 | #endif |
| 275 | |
| 276 | regs[DS1307_REG_SECS] = bin2bcd(t->tm_sec); |
| 277 | regs[DS1307_REG_MIN] = bin2bcd(t->tm_min); |
| 278 | regs[DS1307_REG_HOUR] = bin2bcd(t->tm_hour); |
| 279 | regs[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1); |
| 280 | regs[DS1307_REG_MDAY] = bin2bcd(t->tm_mday); |
| 281 | regs[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1); |
| 282 | |
| 283 | /* assume 20YY not 19YY */ |
| 284 | tmp = t->tm_year - 100; |
| 285 | regs[DS1307_REG_YEAR] = bin2bcd(tmp); |
| 286 | |
| 287 | if (chip->century_enable_bit) |
| 288 | regs[chip->century_reg] |= chip->century_enable_bit; |
| 289 | if (t->tm_year > 199 && chip->century_bit) |
| 290 | regs[chip->century_reg] |= chip->century_bit; |
| 291 | |
| 292 | if (ds1307->type == mcp794xx) { |
| 293 | /* |
| 294 | * these bits were cleared when preparing the date/time |
| 295 | * values and need to be set again before writing the |
| 296 | * regsfer out to the device. |
| 297 | */ |
| 298 | regs[DS1307_REG_SECS] |= MCP794XX_BIT_ST; |
| 299 | regs[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN; |
| 300 | } |
| 301 | |
| 302 | dev_dbg(dev, "%s: %7ph\n", "write", regs); |
| 303 | |
| 304 | result = regmap_bulk_write(ds1307->regmap, chip->offset, regs, |
| 305 | sizeof(regs)); |
| 306 | if (result) { |
| 307 | dev_err(dev, "%s error %d\n", "write", result); |
| 308 | return result; |
| 309 | } |
Uwe Kleine-König | 501f982 | 2019-01-25 15:35:57 +0100 | [diff] [blame] | 310 | |
| 311 | if (ds1307->type == rx_8130) { |
| 312 | /* clear Voltage Loss Flag as data is available now */ |
| 313 | result = regmap_write(ds1307->regmap, RX8130_REG_FLAG, |
| 314 | ~(u8)RX8130_REG_FLAG_VLF); |
| 315 | if (result) { |
| 316 | dev_err(dev, "%s error %d\n", "write", result); |
| 317 | return result; |
| 318 | } |
| 319 | } |
| 320 | |
Uwe Kleine-König | d0e3f61 | 2019-01-25 15:35:55 +0100 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 325 | { |
| 326 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 327 | int ret; |
| 328 | u8 regs[9]; |
| 329 | |
| 330 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 331 | return -EINVAL; |
| 332 | |
| 333 | /* read all ALARM1, ALARM2, and status registers at once */ |
| 334 | ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, |
| 335 | regs, sizeof(regs)); |
| 336 | if (ret) { |
| 337 | dev_err(dev, "%s error %d\n", "alarm read", ret); |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read", |
| 342 | ®s[0], ®s[4], ®s[7]); |
| 343 | |
| 344 | /* |
| 345 | * report alarm time (ALARM1); assume 24 hour and day-of-month modes, |
| 346 | * and that all four fields are checked matches |
| 347 | */ |
| 348 | t->time.tm_sec = bcd2bin(regs[0] & 0x7f); |
| 349 | t->time.tm_min = bcd2bin(regs[1] & 0x7f); |
| 350 | t->time.tm_hour = bcd2bin(regs[2] & 0x3f); |
| 351 | t->time.tm_mday = bcd2bin(regs[3] & 0x3f); |
| 352 | |
| 353 | /* ... and status */ |
| 354 | t->enabled = !!(regs[7] & DS1337_BIT_A1IE); |
| 355 | t->pending = !!(regs[8] & DS1337_BIT_A1I); |
| 356 | |
| 357 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 358 | "hours=%d, mday=%d, enabled=%d, pending=%d\n", |
| 359 | "alarm read", t->time.tm_sec, t->time.tm_min, |
| 360 | t->time.tm_hour, t->time.tm_mday, |
| 361 | t->enabled, t->pending); |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 367 | { |
| 368 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 369 | unsigned char regs[9]; |
| 370 | u8 control, status; |
| 371 | int ret; |
| 372 | |
| 373 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 374 | return -EINVAL; |
| 375 | |
| 376 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 377 | "hours=%d, mday=%d, enabled=%d, pending=%d\n", |
| 378 | "alarm set", t->time.tm_sec, t->time.tm_min, |
| 379 | t->time.tm_hour, t->time.tm_mday, |
| 380 | t->enabled, t->pending); |
| 381 | |
| 382 | /* read current status of both alarms and the chip */ |
| 383 | ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs, |
| 384 | sizeof(regs)); |
| 385 | if (ret) { |
| 386 | dev_err(dev, "%s error %d\n", "alarm write", ret); |
| 387 | return ret; |
| 388 | } |
| 389 | control = regs[7]; |
| 390 | status = regs[8]; |
| 391 | |
| 392 | dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)", |
| 393 | ®s[0], ®s[4], control, status); |
| 394 | |
| 395 | /* set ALARM1, using 24 hour and day-of-month modes */ |
| 396 | regs[0] = bin2bcd(t->time.tm_sec); |
| 397 | regs[1] = bin2bcd(t->time.tm_min); |
| 398 | regs[2] = bin2bcd(t->time.tm_hour); |
| 399 | regs[3] = bin2bcd(t->time.tm_mday); |
| 400 | |
| 401 | /* set ALARM2 to non-garbage */ |
| 402 | regs[4] = 0; |
| 403 | regs[5] = 0; |
| 404 | regs[6] = 0; |
| 405 | |
| 406 | /* disable alarms */ |
| 407 | regs[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE); |
| 408 | regs[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I); |
| 409 | |
| 410 | ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs, |
| 411 | sizeof(regs)); |
| 412 | if (ret) { |
| 413 | dev_err(dev, "can't set alarm time\n"); |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | /* optionally enable ALARM1 */ |
| 418 | if (t->enabled) { |
| 419 | dev_dbg(dev, "alarm IRQ armed\n"); |
| 420 | regs[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */ |
| 421 | regmap_write(ds1307->regmap, DS1337_REG_CONTROL, regs[7]); |
| 422 | } |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 428 | { |
| 429 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 430 | |
| 431 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 432 | return -ENOTTY; |
| 433 | |
| 434 | return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL, |
| 435 | DS1337_BIT_A1IE, |
| 436 | enabled ? DS1337_BIT_A1IE : 0); |
| 437 | } |
| 438 | |
| 439 | static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307, u32 ohms, bool diode) |
| 440 | { |
| 441 | u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE : |
| 442 | DS1307_TRICKLE_CHARGER_NO_DIODE; |
| 443 | |
| 444 | switch (ohms) { |
| 445 | case 250: |
| 446 | setup |= DS1307_TRICKLE_CHARGER_250_OHM; |
| 447 | break; |
| 448 | case 2000: |
| 449 | setup |= DS1307_TRICKLE_CHARGER_2K_OHM; |
| 450 | break; |
| 451 | case 4000: |
| 452 | setup |= DS1307_TRICKLE_CHARGER_4K_OHM; |
| 453 | break; |
| 454 | default: |
| 455 | dev_warn(ds1307->dev, |
| 456 | "Unsupported ohm value %u in dt\n", ohms); |
| 457 | return 0; |
| 458 | } |
| 459 | return setup; |
| 460 | } |
| 461 | |
| 462 | static irqreturn_t rx8130_irq(int irq, void *dev_id) |
| 463 | { |
| 464 | struct ds1307 *ds1307 = dev_id; |
| 465 | struct mutex *lock = &ds1307->rtc->ops_lock; |
| 466 | u8 ctl[3]; |
| 467 | int ret; |
| 468 | |
| 469 | mutex_lock(lock); |
| 470 | |
| 471 | /* Read control registers. */ |
| 472 | ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, |
| 473 | sizeof(ctl)); |
| 474 | if (ret < 0) |
| 475 | goto out; |
| 476 | if (!(ctl[1] & RX8130_REG_FLAG_AF)) |
| 477 | goto out; |
| 478 | ctl[1] &= ~RX8130_REG_FLAG_AF; |
| 479 | ctl[2] &= ~RX8130_REG_CONTROL0_AIE; |
| 480 | |
| 481 | ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, |
| 482 | sizeof(ctl)); |
| 483 | if (ret < 0) |
| 484 | goto out; |
| 485 | |
| 486 | rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF); |
| 487 | |
| 488 | out: |
| 489 | mutex_unlock(lock); |
| 490 | |
| 491 | return IRQ_HANDLED; |
| 492 | } |
| 493 | |
| 494 | static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 495 | { |
| 496 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 497 | u8 ald[3], ctl[3]; |
| 498 | int ret; |
| 499 | |
| 500 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 501 | return -EINVAL; |
| 502 | |
| 503 | /* Read alarm registers. */ |
| 504 | ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, |
| 505 | sizeof(ald)); |
| 506 | if (ret < 0) |
| 507 | return ret; |
| 508 | |
| 509 | /* Read control registers. */ |
| 510 | ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, |
| 511 | sizeof(ctl)); |
| 512 | if (ret < 0) |
| 513 | return ret; |
| 514 | |
| 515 | t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE); |
| 516 | t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF); |
| 517 | |
| 518 | /* Report alarm 0 time assuming 24-hour and day-of-month modes. */ |
| 519 | t->time.tm_sec = -1; |
| 520 | t->time.tm_min = bcd2bin(ald[0] & 0x7f); |
| 521 | t->time.tm_hour = bcd2bin(ald[1] & 0x7f); |
| 522 | t->time.tm_wday = -1; |
| 523 | t->time.tm_mday = bcd2bin(ald[2] & 0x7f); |
| 524 | t->time.tm_mon = -1; |
| 525 | t->time.tm_year = -1; |
| 526 | t->time.tm_yday = -1; |
| 527 | t->time.tm_isdst = -1; |
| 528 | |
| 529 | dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n", |
| 530 | __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour, |
| 531 | t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled); |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 537 | { |
| 538 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 539 | u8 ald[3], ctl[3]; |
| 540 | int ret; |
| 541 | |
| 542 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 543 | return -EINVAL; |
| 544 | |
| 545 | dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d " |
| 546 | "enabled=%d pending=%d\n", __func__, |
| 547 | t->time.tm_sec, t->time.tm_min, t->time.tm_hour, |
| 548 | t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, |
| 549 | t->enabled, t->pending); |
| 550 | |
| 551 | /* Read control registers. */ |
| 552 | ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, |
| 553 | sizeof(ctl)); |
| 554 | if (ret < 0) |
| 555 | return ret; |
| 556 | |
Uwe Kleine-König | 3f929ca | 2019-01-25 15:35:58 +0100 | [diff] [blame^] | 557 | ctl[0] &= RX8130_REG_EXTENSION_WADA; |
| 558 | ctl[1] &= ~RX8130_REG_FLAG_AF; |
Uwe Kleine-König | d0e3f61 | 2019-01-25 15:35:55 +0100 | [diff] [blame] | 559 | ctl[2] &= ~RX8130_REG_CONTROL0_AIE; |
| 560 | |
| 561 | ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, |
| 562 | sizeof(ctl)); |
| 563 | if (ret < 0) |
| 564 | return ret; |
| 565 | |
| 566 | /* Hardware alarm precision is 1 minute! */ |
| 567 | ald[0] = bin2bcd(t->time.tm_min); |
| 568 | ald[1] = bin2bcd(t->time.tm_hour); |
| 569 | ald[2] = bin2bcd(t->time.tm_mday); |
| 570 | |
| 571 | ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, |
| 572 | sizeof(ald)); |
| 573 | if (ret < 0) |
| 574 | return ret; |
| 575 | |
| 576 | if (!t->enabled) |
| 577 | return 0; |
| 578 | |
| 579 | ctl[2] |= RX8130_REG_CONTROL0_AIE; |
| 580 | |
Uwe Kleine-König | 3f929ca | 2019-01-25 15:35:58 +0100 | [diff] [blame^] | 581 | return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, ctl[2]); |
Uwe Kleine-König | d0e3f61 | 2019-01-25 15:35:55 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 585 | { |
| 586 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 587 | int ret, reg; |
| 588 | |
| 589 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 590 | return -EINVAL; |
| 591 | |
| 592 | ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, ®); |
| 593 | if (ret < 0) |
| 594 | return ret; |
| 595 | |
| 596 | if (enabled) |
| 597 | reg |= RX8130_REG_CONTROL0_AIE; |
| 598 | else |
| 599 | reg &= ~RX8130_REG_CONTROL0_AIE; |
| 600 | |
| 601 | return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg); |
| 602 | } |
| 603 | |
| 604 | static irqreturn_t mcp794xx_irq(int irq, void *dev_id) |
| 605 | { |
| 606 | struct ds1307 *ds1307 = dev_id; |
| 607 | struct mutex *lock = &ds1307->rtc->ops_lock; |
| 608 | int reg, ret; |
| 609 | |
| 610 | mutex_lock(lock); |
| 611 | |
| 612 | /* Check and clear alarm 0 interrupt flag. */ |
| 613 | ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, ®); |
| 614 | if (ret) |
| 615 | goto out; |
| 616 | if (!(reg & MCP794XX_BIT_ALMX_IF)) |
| 617 | goto out; |
| 618 | reg &= ~MCP794XX_BIT_ALMX_IF; |
| 619 | ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg); |
| 620 | if (ret) |
| 621 | goto out; |
| 622 | |
| 623 | /* Disable alarm 0. */ |
| 624 | ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL, |
| 625 | MCP794XX_BIT_ALM0_EN, 0); |
| 626 | if (ret) |
| 627 | goto out; |
| 628 | |
| 629 | rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF); |
| 630 | |
| 631 | out: |
| 632 | mutex_unlock(lock); |
| 633 | |
| 634 | return IRQ_HANDLED; |
| 635 | } |
| 636 | |
| 637 | static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 638 | { |
| 639 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 640 | u8 regs[10]; |
| 641 | int ret; |
| 642 | |
| 643 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 644 | return -EINVAL; |
| 645 | |
| 646 | /* Read control and alarm 0 registers. */ |
| 647 | ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, |
| 648 | sizeof(regs)); |
| 649 | if (ret) |
| 650 | return ret; |
| 651 | |
| 652 | t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN); |
| 653 | |
| 654 | /* Report alarm 0 time assuming 24-hour and day-of-month modes. */ |
| 655 | t->time.tm_sec = bcd2bin(regs[3] & 0x7f); |
| 656 | t->time.tm_min = bcd2bin(regs[4] & 0x7f); |
| 657 | t->time.tm_hour = bcd2bin(regs[5] & 0x3f); |
| 658 | t->time.tm_wday = bcd2bin(regs[6] & 0x7) - 1; |
| 659 | t->time.tm_mday = bcd2bin(regs[7] & 0x3f); |
| 660 | t->time.tm_mon = bcd2bin(regs[8] & 0x1f) - 1; |
| 661 | t->time.tm_year = -1; |
| 662 | t->time.tm_yday = -1; |
| 663 | t->time.tm_isdst = -1; |
| 664 | |
| 665 | dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d " |
| 666 | "enabled=%d polarity=%d irq=%d match=%lu\n", __func__, |
| 667 | t->time.tm_sec, t->time.tm_min, t->time.tm_hour, |
| 668 | t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled, |
| 669 | !!(regs[6] & MCP794XX_BIT_ALMX_POL), |
| 670 | !!(regs[6] & MCP794XX_BIT_ALMX_IF), |
| 671 | (regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4); |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * We may have a random RTC weekday, therefore calculate alarm weekday based |
| 678 | * on current weekday we read from the RTC timekeeping regs |
| 679 | */ |
| 680 | static int mcp794xx_alm_weekday(struct device *dev, struct rtc_time *tm_alarm) |
| 681 | { |
| 682 | struct rtc_time tm_now; |
| 683 | int days_now, days_alarm, ret; |
| 684 | |
| 685 | ret = ds1307_get_time(dev, &tm_now); |
| 686 | if (ret) |
| 687 | return ret; |
| 688 | |
| 689 | days_now = div_s64(rtc_tm_to_time64(&tm_now), 24 * 60 * 60); |
| 690 | days_alarm = div_s64(rtc_tm_to_time64(tm_alarm), 24 * 60 * 60); |
| 691 | |
| 692 | return (tm_now.tm_wday + days_alarm - days_now) % 7 + 1; |
| 693 | } |
| 694 | |
| 695 | static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 696 | { |
| 697 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 698 | unsigned char regs[10]; |
| 699 | int wday, ret; |
| 700 | |
| 701 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 702 | return -EINVAL; |
| 703 | |
| 704 | wday = mcp794xx_alm_weekday(dev, &t->time); |
| 705 | if (wday < 0) |
| 706 | return wday; |
| 707 | |
| 708 | dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d " |
| 709 | "enabled=%d pending=%d\n", __func__, |
| 710 | t->time.tm_sec, t->time.tm_min, t->time.tm_hour, |
| 711 | t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, |
| 712 | t->enabled, t->pending); |
| 713 | |
| 714 | /* Read control and alarm 0 registers. */ |
| 715 | ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, |
| 716 | sizeof(regs)); |
| 717 | if (ret) |
| 718 | return ret; |
| 719 | |
| 720 | /* Set alarm 0, using 24-hour and day-of-month modes. */ |
| 721 | regs[3] = bin2bcd(t->time.tm_sec); |
| 722 | regs[4] = bin2bcd(t->time.tm_min); |
| 723 | regs[5] = bin2bcd(t->time.tm_hour); |
| 724 | regs[6] = wday; |
| 725 | regs[7] = bin2bcd(t->time.tm_mday); |
| 726 | regs[8] = bin2bcd(t->time.tm_mon + 1); |
| 727 | |
| 728 | /* Clear the alarm 0 interrupt flag. */ |
| 729 | regs[6] &= ~MCP794XX_BIT_ALMX_IF; |
| 730 | /* Set alarm match: second, minute, hour, day, date, month. */ |
| 731 | regs[6] |= MCP794XX_MSK_ALMX_MATCH; |
| 732 | /* Disable interrupt. We will not enable until completely programmed */ |
| 733 | regs[0] &= ~MCP794XX_BIT_ALM0_EN; |
| 734 | |
| 735 | ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs, |
| 736 | sizeof(regs)); |
| 737 | if (ret) |
| 738 | return ret; |
| 739 | |
| 740 | if (!t->enabled) |
| 741 | return 0; |
| 742 | regs[0] |= MCP794XX_BIT_ALM0_EN; |
| 743 | return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]); |
| 744 | } |
| 745 | |
| 746 | static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 747 | { |
| 748 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 749 | |
| 750 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 751 | return -EINVAL; |
| 752 | |
| 753 | return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL, |
| 754 | MCP794XX_BIT_ALM0_EN, |
| 755 | enabled ? MCP794XX_BIT_ALM0_EN : 0); |
| 756 | } |
| 757 | |
| 758 | static int m41txx_rtc_read_offset(struct device *dev, long *offset) |
| 759 | { |
| 760 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 761 | unsigned int ctrl_reg; |
| 762 | u8 val; |
| 763 | |
| 764 | regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg); |
| 765 | |
| 766 | val = ctrl_reg & M41TXX_M_CALIBRATION; |
| 767 | |
| 768 | /* check if positive */ |
| 769 | if (ctrl_reg & M41TXX_BIT_CALIB_SIGN) |
| 770 | *offset = (val * M41TXX_POS_OFFSET_STEP_PPB); |
| 771 | else |
| 772 | *offset = -(val * M41TXX_NEG_OFFSET_STEP_PPB); |
| 773 | |
| 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | static int m41txx_rtc_set_offset(struct device *dev, long offset) |
| 778 | { |
| 779 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 780 | unsigned int ctrl_reg; |
| 781 | |
| 782 | if ((offset < M41TXX_MIN_OFFSET) || (offset > M41TXX_MAX_OFFSET)) |
| 783 | return -ERANGE; |
| 784 | |
| 785 | if (offset >= 0) { |
| 786 | ctrl_reg = DIV_ROUND_CLOSEST(offset, |
| 787 | M41TXX_POS_OFFSET_STEP_PPB); |
| 788 | ctrl_reg |= M41TXX_BIT_CALIB_SIGN; |
| 789 | } else { |
| 790 | ctrl_reg = DIV_ROUND_CLOSEST(abs(offset), |
| 791 | M41TXX_NEG_OFFSET_STEP_PPB); |
| 792 | } |
| 793 | |
| 794 | return regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL, |
| 795 | M41TXX_M_CALIBRATION | M41TXX_BIT_CALIB_SIGN, |
| 796 | ctrl_reg); |
| 797 | } |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 798 | |
Heiner Kallweit | 1efb98b | 2017-07-12 07:49:44 +0200 | [diff] [blame] | 799 | static const struct rtc_class_ops rx8130_rtc_ops = { |
| 800 | .read_time = ds1307_get_time, |
| 801 | .set_time = ds1307_set_time, |
| 802 | .read_alarm = rx8130_read_alarm, |
| 803 | .set_alarm = rx8130_set_alarm, |
| 804 | .alarm_irq_enable = rx8130_alarm_irq_enable, |
| 805 | }; |
| 806 | |
| 807 | static const struct rtc_class_ops mcp794xx_rtc_ops = { |
| 808 | .read_time = ds1307_get_time, |
| 809 | .set_time = ds1307_set_time, |
| 810 | .read_alarm = mcp794xx_read_alarm, |
| 811 | .set_alarm = mcp794xx_set_alarm, |
| 812 | .alarm_irq_enable = mcp794xx_alarm_irq_enable, |
| 813 | }; |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 814 | |
Giulio Benetti | 79230ff | 2018-07-25 19:26:04 +0200 | [diff] [blame] | 815 | static const struct rtc_class_ops m41txx_rtc_ops = { |
| 816 | .read_time = ds1307_get_time, |
| 817 | .set_time = ds1307_set_time, |
| 818 | .read_alarm = ds1337_read_alarm, |
| 819 | .set_alarm = ds1337_set_alarm, |
| 820 | .alarm_irq_enable = ds1307_alarm_irq_enable, |
| 821 | .read_offset = m41txx_rtc_read_offset, |
| 822 | .set_offset = m41txx_rtc_set_offset, |
| 823 | }; |
| 824 | |
Heiner Kallweit | 7624df4 | 2017-07-12 07:49:33 +0200 | [diff] [blame] | 825 | static const struct chip_desc chips[last_ds_type] = { |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 826 | [ds_1307] = { |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 827 | .nvram_offset = 8, |
| 828 | .nvram_size = 56, |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 829 | }, |
Sean Nyekjaer | 300a773 | 2017-06-08 12:36:54 +0200 | [diff] [blame] | 830 | [ds_1308] = { |
| 831 | .nvram_offset = 8, |
| 832 | .nvram_size = 56, |
| 833 | }, |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 834 | [ds_1337] = { |
| 835 | .alarm = 1, |
Heiner Kallweit | e48585d | 2017-06-05 17:57:33 +0200 | [diff] [blame] | 836 | .century_reg = DS1307_REG_MONTH, |
| 837 | .century_bit = DS1337_BIT_CENTURY, |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 838 | }, |
| 839 | [ds_1338] = { |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 840 | .nvram_offset = 8, |
| 841 | .nvram_size = 56, |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 842 | }, |
| 843 | [ds_1339] = { |
| 844 | .alarm = 1, |
Heiner Kallweit | e48585d | 2017-06-05 17:57:33 +0200 | [diff] [blame] | 845 | .century_reg = DS1307_REG_MONTH, |
| 846 | .century_bit = DS1337_BIT_CENTURY, |
Heiner Kallweit | 0b6ee80 | 2017-07-12 07:49:22 +0200 | [diff] [blame] | 847 | .bbsqi_bit = DS1339_BIT_BBSQI, |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 848 | .trickle_charger_reg = 0x10, |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 849 | .do_trickle_setup = &do_trickle_setup_ds1339, |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 850 | }, |
| 851 | [ds_1340] = { |
Heiner Kallweit | e48585d | 2017-06-05 17:57:33 +0200 | [diff] [blame] | 852 | .century_reg = DS1307_REG_HOUR, |
| 853 | .century_enable_bit = DS1340_BIT_CENTURY_EN, |
| 854 | .century_bit = DS1340_BIT_CENTURY, |
Andrea Greco | 51ed73eb | 2018-04-20 11:34:02 +0200 | [diff] [blame] | 855 | .do_trickle_setup = &do_trickle_setup_ds1339, |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 856 | .trickle_charger_reg = 0x08, |
| 857 | }, |
Nikita Yushchenko | 0759c88 | 2017-08-24 09:32:11 +0300 | [diff] [blame] | 858 | [ds_1341] = { |
| 859 | .century_reg = DS1307_REG_MONTH, |
| 860 | .century_bit = DS1337_BIT_CENTURY, |
| 861 | }, |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 862 | [ds_1388] = { |
Heiner Kallweit | e553170 | 2017-07-12 07:49:47 +0200 | [diff] [blame] | 863 | .offset = 1, |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 864 | .trickle_charger_reg = 0x0a, |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 865 | }, |
| 866 | [ds_3231] = { |
| 867 | .alarm = 1, |
Heiner Kallweit | e48585d | 2017-06-05 17:57:33 +0200 | [diff] [blame] | 868 | .century_reg = DS1307_REG_MONTH, |
| 869 | .century_bit = DS1337_BIT_CENTURY, |
Heiner Kallweit | 0b6ee80 | 2017-07-12 07:49:22 +0200 | [diff] [blame] | 870 | .bbsqi_bit = DS3231_BIT_BBSQW, |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 871 | }, |
Marek Vasut | ee0981b | 2017-06-18 22:55:28 +0200 | [diff] [blame] | 872 | [rx_8130] = { |
| 873 | .alarm = 1, |
| 874 | /* this is battery backed SRAM */ |
| 875 | .nvram_offset = 0x20, |
| 876 | .nvram_size = 4, /* 32bit (4 word x 8 bit) */ |
Heiner Kallweit | e553170 | 2017-07-12 07:49:47 +0200 | [diff] [blame] | 877 | .offset = 0x10, |
Heiner Kallweit | 4594712 | 2017-07-12 07:49:41 +0200 | [diff] [blame] | 878 | .irq_handler = rx8130_irq, |
Heiner Kallweit | 1efb98b | 2017-07-12 07:49:44 +0200 | [diff] [blame] | 879 | .rtc_ops = &rx8130_rtc_ops, |
Marek Vasut | ee0981b | 2017-06-18 22:55:28 +0200 | [diff] [blame] | 880 | }, |
Giulio Benetti | 79230ff | 2018-07-25 19:26:04 +0200 | [diff] [blame] | 881 | [m41t0] = { |
| 882 | .rtc_ops = &m41txx_rtc_ops, |
| 883 | }, |
| 884 | [m41t00] = { |
| 885 | .rtc_ops = &m41txx_rtc_ops, |
| 886 | }, |
Giulio Benetti | 7e58076 | 2018-05-16 23:08:40 +0200 | [diff] [blame] | 887 | [m41t11] = { |
| 888 | /* this is battery backed SRAM */ |
| 889 | .nvram_offset = 8, |
| 890 | .nvram_size = 56, |
Giulio Benetti | 79230ff | 2018-07-25 19:26:04 +0200 | [diff] [blame] | 891 | .rtc_ops = &m41txx_rtc_ops, |
Giulio Benetti | 7e58076 | 2018-05-16 23:08:40 +0200 | [diff] [blame] | 892 | }, |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 893 | [mcp794xx] = { |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 894 | .alarm = 1, |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 895 | /* this is battery backed SRAM */ |
| 896 | .nvram_offset = 0x20, |
| 897 | .nvram_size = 0x40, |
Heiner Kallweit | 4594712 | 2017-07-12 07:49:41 +0200 | [diff] [blame] | 898 | .irq_handler = mcp794xx_irq, |
Heiner Kallweit | 1efb98b | 2017-07-12 07:49:44 +0200 | [diff] [blame] | 899 | .rtc_ops = &mcp794xx_rtc_ops, |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 900 | }, |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 901 | }; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 902 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 903 | static const struct i2c_device_id ds1307_id[] = { |
| 904 | { "ds1307", ds_1307 }, |
Sean Nyekjaer | 300a773 | 2017-06-08 12:36:54 +0200 | [diff] [blame] | 905 | { "ds1308", ds_1308 }, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 906 | { "ds1337", ds_1337 }, |
| 907 | { "ds1338", ds_1338 }, |
| 908 | { "ds1339", ds_1339 }, |
Joakim Tjernlund | 33df2ee | 2009-06-17 16:26:08 -0700 | [diff] [blame] | 909 | { "ds1388", ds_1388 }, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 910 | { "ds1340", ds_1340 }, |
Nikita Yushchenko | 0759c88 | 2017-08-24 09:32:11 +0300 | [diff] [blame] | 911 | { "ds1341", ds_1341 }, |
Wolfram Sang | 97f902b | 2009-06-17 16:26:10 -0700 | [diff] [blame] | 912 | { "ds3231", ds_3231 }, |
Stefan Agner | 8566f70 | 2017-03-23 16:54:57 -0700 | [diff] [blame] | 913 | { "m41t0", m41t0 }, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 914 | { "m41t00", m41t00 }, |
Giulio Benetti | 7e58076 | 2018-05-16 23:08:40 +0200 | [diff] [blame] | 915 | { "m41t11", m41t11 }, |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 916 | { "mcp7940x", mcp794xx }, |
| 917 | { "mcp7941x", mcp794xx }, |
Priyanka Jain | 31c1771 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 918 | { "pt7c4338", ds_1307 }, |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 919 | { "rx8025", rx_8025 }, |
Alexandre Belloni | 78aaa06 | 2016-07-13 02:36:41 +0200 | [diff] [blame] | 920 | { "isl12057", ds_1337 }, |
Marek Vasut | ee0981b | 2017-06-18 22:55:28 +0200 | [diff] [blame] | 921 | { "rx8130", rx_8130 }, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 922 | { } |
| 923 | }; |
| 924 | MODULE_DEVICE_TABLE(i2c, ds1307_id); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 925 | |
Javier Martinez Canillas | 7ef6d2c | 2017-03-03 11:29:15 -0300 | [diff] [blame] | 926 | #ifdef CONFIG_OF |
| 927 | static const struct of_device_id ds1307_of_match[] = { |
| 928 | { |
| 929 | .compatible = "dallas,ds1307", |
| 930 | .data = (void *)ds_1307 |
| 931 | }, |
| 932 | { |
Sean Nyekjaer | 300a773 | 2017-06-08 12:36:54 +0200 | [diff] [blame] | 933 | .compatible = "dallas,ds1308", |
| 934 | .data = (void *)ds_1308 |
| 935 | }, |
| 936 | { |
Javier Martinez Canillas | 7ef6d2c | 2017-03-03 11:29:15 -0300 | [diff] [blame] | 937 | .compatible = "dallas,ds1337", |
| 938 | .data = (void *)ds_1337 |
| 939 | }, |
| 940 | { |
| 941 | .compatible = "dallas,ds1338", |
| 942 | .data = (void *)ds_1338 |
| 943 | }, |
| 944 | { |
| 945 | .compatible = "dallas,ds1339", |
| 946 | .data = (void *)ds_1339 |
| 947 | }, |
| 948 | { |
| 949 | .compatible = "dallas,ds1388", |
| 950 | .data = (void *)ds_1388 |
| 951 | }, |
| 952 | { |
| 953 | .compatible = "dallas,ds1340", |
| 954 | .data = (void *)ds_1340 |
| 955 | }, |
| 956 | { |
Nikita Yushchenko | 0759c88 | 2017-08-24 09:32:11 +0300 | [diff] [blame] | 957 | .compatible = "dallas,ds1341", |
| 958 | .data = (void *)ds_1341 |
| 959 | }, |
| 960 | { |
Javier Martinez Canillas | 7ef6d2c | 2017-03-03 11:29:15 -0300 | [diff] [blame] | 961 | .compatible = "maxim,ds3231", |
| 962 | .data = (void *)ds_3231 |
| 963 | }, |
| 964 | { |
Alexandre Belloni | db2f814 | 2017-04-08 17:22:02 +0200 | [diff] [blame] | 965 | .compatible = "st,m41t0", |
Giulio Benetti | 146a552 | 2018-05-16 23:08:39 +0200 | [diff] [blame] | 966 | .data = (void *)m41t0 |
Alexandre Belloni | db2f814 | 2017-04-08 17:22:02 +0200 | [diff] [blame] | 967 | }, |
| 968 | { |
Javier Martinez Canillas | 7ef6d2c | 2017-03-03 11:29:15 -0300 | [diff] [blame] | 969 | .compatible = "st,m41t00", |
| 970 | .data = (void *)m41t00 |
| 971 | }, |
| 972 | { |
Giulio Benetti | 7e58076 | 2018-05-16 23:08:40 +0200 | [diff] [blame] | 973 | .compatible = "st,m41t11", |
| 974 | .data = (void *)m41t11 |
| 975 | }, |
| 976 | { |
Javier Martinez Canillas | 7ef6d2c | 2017-03-03 11:29:15 -0300 | [diff] [blame] | 977 | .compatible = "microchip,mcp7940x", |
| 978 | .data = (void *)mcp794xx |
| 979 | }, |
| 980 | { |
| 981 | .compatible = "microchip,mcp7941x", |
| 982 | .data = (void *)mcp794xx |
| 983 | }, |
| 984 | { |
| 985 | .compatible = "pericom,pt7c4338", |
| 986 | .data = (void *)ds_1307 |
| 987 | }, |
| 988 | { |
| 989 | .compatible = "epson,rx8025", |
| 990 | .data = (void *)rx_8025 |
| 991 | }, |
| 992 | { |
| 993 | .compatible = "isil,isl12057", |
| 994 | .data = (void *)ds_1337 |
| 995 | }, |
Bastian Stender | 47dd472 | 2017-10-17 14:46:07 +0200 | [diff] [blame] | 996 | { |
| 997 | .compatible = "epson,rx8130", |
| 998 | .data = (void *)rx_8130 |
| 999 | }, |
Javier Martinez Canillas | 7ef6d2c | 2017-03-03 11:29:15 -0300 | [diff] [blame] | 1000 | { } |
| 1001 | }; |
| 1002 | MODULE_DEVICE_TABLE(of, ds1307_of_match); |
| 1003 | #endif |
| 1004 | |
Tin Huynh | 9c19b89 | 2016-11-30 09:57:31 +0700 | [diff] [blame] | 1005 | #ifdef CONFIG_ACPI |
| 1006 | static const struct acpi_device_id ds1307_acpi_ids[] = { |
| 1007 | { .id = "DS1307", .driver_data = ds_1307 }, |
Sean Nyekjaer | 300a773 | 2017-06-08 12:36:54 +0200 | [diff] [blame] | 1008 | { .id = "DS1308", .driver_data = ds_1308 }, |
Tin Huynh | 9c19b89 | 2016-11-30 09:57:31 +0700 | [diff] [blame] | 1009 | { .id = "DS1337", .driver_data = ds_1337 }, |
| 1010 | { .id = "DS1338", .driver_data = ds_1338 }, |
| 1011 | { .id = "DS1339", .driver_data = ds_1339 }, |
| 1012 | { .id = "DS1388", .driver_data = ds_1388 }, |
| 1013 | { .id = "DS1340", .driver_data = ds_1340 }, |
Nikita Yushchenko | 0759c88 | 2017-08-24 09:32:11 +0300 | [diff] [blame] | 1014 | { .id = "DS1341", .driver_data = ds_1341 }, |
Tin Huynh | 9c19b89 | 2016-11-30 09:57:31 +0700 | [diff] [blame] | 1015 | { .id = "DS3231", .driver_data = ds_3231 }, |
Stefan Agner | 8566f70 | 2017-03-23 16:54:57 -0700 | [diff] [blame] | 1016 | { .id = "M41T0", .driver_data = m41t0 }, |
Tin Huynh | 9c19b89 | 2016-11-30 09:57:31 +0700 | [diff] [blame] | 1017 | { .id = "M41T00", .driver_data = m41t00 }, |
Giulio Benetti | 7e58076 | 2018-05-16 23:08:40 +0200 | [diff] [blame] | 1018 | { .id = "M41T11", .driver_data = m41t11 }, |
Tin Huynh | 9c19b89 | 2016-11-30 09:57:31 +0700 | [diff] [blame] | 1019 | { .id = "MCP7940X", .driver_data = mcp794xx }, |
| 1020 | { .id = "MCP7941X", .driver_data = mcp794xx }, |
| 1021 | { .id = "PT7C4338", .driver_data = ds_1307 }, |
| 1022 | { .id = "RX8025", .driver_data = rx_8025 }, |
| 1023 | { .id = "ISL12057", .driver_data = ds_1337 }, |
Bastian Stender | 47dd472 | 2017-10-17 14:46:07 +0200 | [diff] [blame] | 1024 | { .id = "RX8130", .driver_data = rx_8130 }, |
Tin Huynh | 9c19b89 | 2016-11-30 09:57:31 +0700 | [diff] [blame] | 1025 | { } |
| 1026 | }; |
| 1027 | MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids); |
| 1028 | #endif |
| 1029 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1030 | /* |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1031 | * The ds1337 and ds1339 both have two alarms, but we only use the first |
| 1032 | * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm |
| 1033 | * signal; ds1339 chips have only one alarm signal. |
| 1034 | */ |
Felipe Balbi | 2fb07a1 | 2015-06-23 11:15:10 -0500 | [diff] [blame] | 1035 | static irqreturn_t ds1307_irq(int irq, void *dev_id) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1036 | { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1037 | struct ds1307 *ds1307 = dev_id; |
Felipe Balbi | 2fb07a1 | 2015-06-23 11:15:10 -0500 | [diff] [blame] | 1038 | struct mutex *lock = &ds1307->rtc->ops_lock; |
Heiner Kallweit | 078f3f6 | 2017-06-05 17:57:29 +0200 | [diff] [blame] | 1039 | int stat, ret; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1040 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1041 | mutex_lock(lock); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1042 | ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat); |
| 1043 | if (ret) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1044 | goto out; |
| 1045 | |
| 1046 | if (stat & DS1337_BIT_A1I) { |
| 1047 | stat &= ~DS1337_BIT_A1I; |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1048 | regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat); |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1049 | |
Heiner Kallweit | 078f3f6 | 2017-06-05 17:57:29 +0200 | [diff] [blame] | 1050 | ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL, |
| 1051 | DS1337_BIT_A1IE, 0); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1052 | if (ret) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1053 | goto out; |
| 1054 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1055 | rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF); |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | out: |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1059 | mutex_unlock(lock); |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1060 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1061 | return IRQ_HANDLED; |
| 1062 | } |
| 1063 | |
| 1064 | /*----------------------------------------------------------------------*/ |
| 1065 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 1066 | static const struct rtc_class_ops ds13xx_rtc_ops = { |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1067 | .read_time = ds1307_get_time, |
| 1068 | .set_time = ds1307_set_time, |
Jüri Reitel | 74d88eb | 2009-01-07 18:07:16 -0800 | [diff] [blame] | 1069 | .read_alarm = ds1337_read_alarm, |
| 1070 | .set_alarm = ds1337_set_alarm, |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 1071 | .alarm_irq_enable = ds1307_alarm_irq_enable, |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1072 | }; |
| 1073 | |
Alexandre Belloni | 6a5f2a1f | 2018-09-20 16:35:26 +0200 | [diff] [blame] | 1074 | static ssize_t frequency_test_store(struct device *dev, |
| 1075 | struct device_attribute *attr, |
| 1076 | const char *buf, size_t count) |
Giulio Benetti | b41c23e | 2018-07-25 19:26:05 +0200 | [diff] [blame] | 1077 | { |
Alexandre Belloni | 6a5f2a1f | 2018-09-20 16:35:26 +0200 | [diff] [blame] | 1078 | struct ds1307 *ds1307 = dev_get_drvdata(dev->parent); |
Giulio Benetti | b41c23e | 2018-07-25 19:26:05 +0200 | [diff] [blame] | 1079 | bool freq_test_en; |
| 1080 | int ret; |
| 1081 | |
| 1082 | ret = kstrtobool(buf, &freq_test_en); |
| 1083 | if (ret) { |
| 1084 | dev_err(dev, "Failed to store RTC Frequency Test attribute\n"); |
| 1085 | return ret; |
| 1086 | } |
| 1087 | |
| 1088 | regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL, M41TXX_BIT_FT, |
| 1089 | freq_test_en ? M41TXX_BIT_FT : 0); |
| 1090 | |
| 1091 | return count; |
| 1092 | } |
| 1093 | |
Alexandre Belloni | 6a5f2a1f | 2018-09-20 16:35:26 +0200 | [diff] [blame] | 1094 | static ssize_t frequency_test_show(struct device *dev, |
| 1095 | struct device_attribute *attr, |
| 1096 | char *buf) |
Giulio Benetti | b41c23e | 2018-07-25 19:26:05 +0200 | [diff] [blame] | 1097 | { |
Alexandre Belloni | 6a5f2a1f | 2018-09-20 16:35:26 +0200 | [diff] [blame] | 1098 | struct ds1307 *ds1307 = dev_get_drvdata(dev->parent); |
Giulio Benetti | b41c23e | 2018-07-25 19:26:05 +0200 | [diff] [blame] | 1099 | unsigned int ctrl_reg; |
| 1100 | |
| 1101 | regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg); |
| 1102 | |
| 1103 | return scnprintf(buf, PAGE_SIZE, (ctrl_reg & M41TXX_BIT_FT) ? "on\n" : |
| 1104 | "off\n"); |
| 1105 | } |
| 1106 | |
Alexandre Belloni | 6a5f2a1f | 2018-09-20 16:35:26 +0200 | [diff] [blame] | 1107 | static DEVICE_ATTR_RW(frequency_test); |
Giulio Benetti | b41c23e | 2018-07-25 19:26:05 +0200 | [diff] [blame] | 1108 | |
| 1109 | static struct attribute *rtc_freq_test_attrs[] = { |
Alexandre Belloni | 6a5f2a1f | 2018-09-20 16:35:26 +0200 | [diff] [blame] | 1110 | &dev_attr_frequency_test.attr, |
Giulio Benetti | b41c23e | 2018-07-25 19:26:05 +0200 | [diff] [blame] | 1111 | NULL, |
| 1112 | }; |
| 1113 | |
| 1114 | static const struct attribute_group rtc_freq_test_attr_group = { |
| 1115 | .attrs = rtc_freq_test_attrs, |
| 1116 | }; |
| 1117 | |
Giulio Benetti | b41c23e | 2018-07-25 19:26:05 +0200 | [diff] [blame] | 1118 | static int ds1307_add_frequency_test(struct ds1307 *ds1307) |
| 1119 | { |
| 1120 | int err; |
| 1121 | |
| 1122 | switch (ds1307->type) { |
| 1123 | case m41t0: |
| 1124 | case m41t00: |
| 1125 | case m41t11: |
Alexandre Belloni | 6a5f2a1f | 2018-09-20 16:35:26 +0200 | [diff] [blame] | 1126 | err = rtc_add_group(ds1307->rtc, &rtc_freq_test_attr_group); |
| 1127 | if (err) |
Giulio Benetti | b41c23e | 2018-07-25 19:26:05 +0200 | [diff] [blame] | 1128 | return err; |
Giulio Benetti | b41c23e | 2018-07-25 19:26:05 +0200 | [diff] [blame] | 1129 | break; |
| 1130 | default: |
| 1131 | break; |
| 1132 | } |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 1137 | /*----------------------------------------------------------------------*/ |
| 1138 | |
Alexandre Belloni | abc925f | 2017-07-06 11:42:07 +0200 | [diff] [blame] | 1139 | static int ds1307_nvram_read(void *priv, unsigned int offset, void *val, |
| 1140 | size_t bytes) |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 1141 | { |
Alexandre Belloni | abc925f | 2017-07-06 11:42:07 +0200 | [diff] [blame] | 1142 | struct ds1307 *ds1307 = priv; |
Heiner Kallweit | 969fa07 | 2017-07-12 07:49:54 +0200 | [diff] [blame] | 1143 | const struct chip_desc *chip = &chips[ds1307->type]; |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 1144 | |
Heiner Kallweit | 969fa07 | 2017-07-12 07:49:54 +0200 | [diff] [blame] | 1145 | return regmap_bulk_read(ds1307->regmap, chip->nvram_offset + offset, |
Alexandre Belloni | abc925f | 2017-07-06 11:42:07 +0200 | [diff] [blame] | 1146 | val, bytes); |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Alexandre Belloni | abc925f | 2017-07-06 11:42:07 +0200 | [diff] [blame] | 1149 | static int ds1307_nvram_write(void *priv, unsigned int offset, void *val, |
| 1150 | size_t bytes) |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 1151 | { |
Alexandre Belloni | abc925f | 2017-07-06 11:42:07 +0200 | [diff] [blame] | 1152 | struct ds1307 *ds1307 = priv; |
Heiner Kallweit | 969fa07 | 2017-07-12 07:49:54 +0200 | [diff] [blame] | 1153 | const struct chip_desc *chip = &chips[ds1307->type]; |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 1154 | |
Heiner Kallweit | 969fa07 | 2017-07-12 07:49:54 +0200 | [diff] [blame] | 1155 | return regmap_bulk_write(ds1307->regmap, chip->nvram_offset + offset, |
Alexandre Belloni | abc925f | 2017-07-06 11:42:07 +0200 | [diff] [blame] | 1156 | val, bytes); |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 1157 | } |
| 1158 | |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 1159 | /*----------------------------------------------------------------------*/ |
| 1160 | |
Heiner Kallweit | d8490fd | 2017-07-12 07:49:28 +0200 | [diff] [blame] | 1161 | static u8 ds1307_trickle_init(struct ds1307 *ds1307, |
Heiner Kallweit | 7624df4 | 2017-07-12 07:49:33 +0200 | [diff] [blame] | 1162 | const struct chip_desc *chip) |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 1163 | { |
Alexandre Belloni | 57ec2d9 | 2017-09-04 22:46:04 +0200 | [diff] [blame] | 1164 | u32 ohms; |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 1165 | bool diode = true; |
| 1166 | |
| 1167 | if (!chip->do_trickle_setup) |
Heiner Kallweit | d8490fd | 2017-07-12 07:49:28 +0200 | [diff] [blame] | 1168 | return 0; |
| 1169 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1170 | if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms", |
| 1171 | &ohms)) |
Heiner Kallweit | d8490fd | 2017-07-12 07:49:28 +0200 | [diff] [blame] | 1172 | return 0; |
| 1173 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1174 | if (device_property_read_bool(ds1307->dev, "trickle-diode-disable")) |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 1175 | diode = false; |
Heiner Kallweit | d8490fd | 2017-07-12 07:49:28 +0200 | [diff] [blame] | 1176 | |
| 1177 | return chip->do_trickle_setup(ds1307, ohms, diode); |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1180 | /*----------------------------------------------------------------------*/ |
| 1181 | |
Heiner Kallweit | 6b583a6 | 2017-09-27 22:41:26 +0200 | [diff] [blame] | 1182 | #if IS_REACHABLE(CONFIG_HWMON) |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1183 | |
| 1184 | /* |
| 1185 | * Temperature sensor support for ds3231 devices. |
| 1186 | */ |
| 1187 | |
| 1188 | #define DS3231_REG_TEMPERATURE 0x11 |
| 1189 | |
| 1190 | /* |
| 1191 | * A user-initiated temperature conversion is not started by this function, |
| 1192 | * so the temperature is updated once every 64 seconds. |
| 1193 | */ |
Zhuang Yuyao | 9a3dce6 | 2016-04-18 09:21:42 +0900 | [diff] [blame] | 1194 | static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC) |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1195 | { |
| 1196 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 1197 | u8 temp_buf[2]; |
| 1198 | s16 temp; |
| 1199 | int ret; |
| 1200 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1201 | ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE, |
| 1202 | temp_buf, sizeof(temp_buf)); |
| 1203 | if (ret) |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1204 | return ret; |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1205 | /* |
| 1206 | * Temperature is represented as a 10-bit code with a resolution of |
| 1207 | * 0.25 degree celsius and encoded in two's complement format. |
| 1208 | */ |
| 1209 | temp = (temp_buf[0] << 8) | temp_buf[1]; |
| 1210 | temp >>= 6; |
| 1211 | *mC = temp * 250; |
| 1212 | |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | static ssize_t ds3231_hwmon_show_temp(struct device *dev, |
Alexandre Belloni | 4057a66 | 2017-09-04 22:46:06 +0200 | [diff] [blame] | 1217 | struct device_attribute *attr, char *buf) |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1218 | { |
| 1219 | int ret; |
Zhuang Yuyao | 9a3dce6 | 2016-04-18 09:21:42 +0900 | [diff] [blame] | 1220 | s32 temp; |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1221 | |
| 1222 | ret = ds3231_hwmon_read_temp(dev, &temp); |
| 1223 | if (ret) |
| 1224 | return ret; |
| 1225 | |
| 1226 | return sprintf(buf, "%d\n", temp); |
| 1227 | } |
Alexandre Belloni | b4be271 | 2017-09-04 22:46:08 +0200 | [diff] [blame] | 1228 | static SENSOR_DEVICE_ATTR(temp1_input, 0444, ds3231_hwmon_show_temp, |
Alexandre Belloni | 4057a66 | 2017-09-04 22:46:06 +0200 | [diff] [blame] | 1229 | NULL, 0); |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1230 | |
| 1231 | static struct attribute *ds3231_hwmon_attrs[] = { |
| 1232 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 1233 | NULL, |
| 1234 | }; |
| 1235 | ATTRIBUTE_GROUPS(ds3231_hwmon); |
| 1236 | |
| 1237 | static void ds1307_hwmon_register(struct ds1307 *ds1307) |
| 1238 | { |
| 1239 | struct device *dev; |
| 1240 | |
| 1241 | if (ds1307->type != ds_3231) |
| 1242 | return; |
| 1243 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1244 | dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name, |
Alexandre Belloni | 4057a66 | 2017-09-04 22:46:06 +0200 | [diff] [blame] | 1245 | ds1307, |
| 1246 | ds3231_hwmon_groups); |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1247 | if (IS_ERR(dev)) { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1248 | dev_warn(ds1307->dev, "unable to register hwmon device %ld\n", |
| 1249 | PTR_ERR(dev)); |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | #else |
| 1254 | |
| 1255 | static void ds1307_hwmon_register(struct ds1307 *ds1307) |
| 1256 | { |
| 1257 | } |
| 1258 | |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1259 | #endif /* CONFIG_RTC_DRV_DS1307_HWMON */ |
| 1260 | |
| 1261 | /*----------------------------------------------------------------------*/ |
| 1262 | |
| 1263 | /* |
| 1264 | * Square-wave output support for DS3231 |
| 1265 | * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf |
| 1266 | */ |
| 1267 | #ifdef CONFIG_COMMON_CLK |
| 1268 | |
| 1269 | enum { |
| 1270 | DS3231_CLK_SQW = 0, |
| 1271 | DS3231_CLK_32KHZ, |
| 1272 | }; |
| 1273 | |
| 1274 | #define clk_sqw_to_ds1307(clk) \ |
| 1275 | container_of(clk, struct ds1307, clks[DS3231_CLK_SQW]) |
| 1276 | #define clk_32khz_to_ds1307(clk) \ |
| 1277 | container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ]) |
| 1278 | |
| 1279 | static int ds3231_clk_sqw_rates[] = { |
| 1280 | 1, |
| 1281 | 1024, |
| 1282 | 4096, |
| 1283 | 8192, |
| 1284 | }; |
| 1285 | |
| 1286 | static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value) |
| 1287 | { |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1288 | struct mutex *lock = &ds1307->rtc->ops_lock; |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1289 | int ret; |
| 1290 | |
| 1291 | mutex_lock(lock); |
Heiner Kallweit | 078f3f6 | 2017-06-05 17:57:29 +0200 | [diff] [blame] | 1292 | ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL, |
| 1293 | mask, value); |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1294 | mutex_unlock(lock); |
| 1295 | |
| 1296 | return ret; |
| 1297 | } |
| 1298 | |
| 1299 | static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw, |
| 1300 | unsigned long parent_rate) |
| 1301 | { |
| 1302 | struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1303 | int control, ret; |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1304 | int rate_sel = 0; |
| 1305 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1306 | ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control); |
| 1307 | if (ret) |
| 1308 | return ret; |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1309 | if (control & DS1337_BIT_RS1) |
| 1310 | rate_sel += 1; |
| 1311 | if (control & DS1337_BIT_RS2) |
| 1312 | rate_sel += 2; |
| 1313 | |
| 1314 | return ds3231_clk_sqw_rates[rate_sel]; |
| 1315 | } |
| 1316 | |
| 1317 | static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate, |
Alexandre Belloni | 4057a66 | 2017-09-04 22:46:06 +0200 | [diff] [blame] | 1318 | unsigned long *prate) |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1319 | { |
| 1320 | int i; |
| 1321 | |
| 1322 | for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) { |
| 1323 | if (ds3231_clk_sqw_rates[i] <= rate) |
| 1324 | return ds3231_clk_sqw_rates[i]; |
| 1325 | } |
| 1326 | |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate, |
Alexandre Belloni | 4057a66 | 2017-09-04 22:46:06 +0200 | [diff] [blame] | 1331 | unsigned long parent_rate) |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1332 | { |
| 1333 | struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); |
| 1334 | int control = 0; |
| 1335 | int rate_sel; |
| 1336 | |
| 1337 | for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates); |
| 1338 | rate_sel++) { |
| 1339 | if (ds3231_clk_sqw_rates[rate_sel] == rate) |
| 1340 | break; |
| 1341 | } |
| 1342 | |
| 1343 | if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates)) |
| 1344 | return -EINVAL; |
| 1345 | |
| 1346 | if (rate_sel & 1) |
| 1347 | control |= DS1337_BIT_RS1; |
| 1348 | if (rate_sel & 2) |
| 1349 | control |= DS1337_BIT_RS2; |
| 1350 | |
| 1351 | return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2, |
| 1352 | control); |
| 1353 | } |
| 1354 | |
| 1355 | static int ds3231_clk_sqw_prepare(struct clk_hw *hw) |
| 1356 | { |
| 1357 | struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); |
| 1358 | |
| 1359 | return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0); |
| 1360 | } |
| 1361 | |
| 1362 | static void ds3231_clk_sqw_unprepare(struct clk_hw *hw) |
| 1363 | { |
| 1364 | struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); |
| 1365 | |
| 1366 | ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN); |
| 1367 | } |
| 1368 | |
| 1369 | static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw) |
| 1370 | { |
| 1371 | struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1372 | int control, ret; |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1373 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1374 | ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control); |
| 1375 | if (ret) |
| 1376 | return ret; |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1377 | |
| 1378 | return !(control & DS1337_BIT_INTCN); |
| 1379 | } |
| 1380 | |
| 1381 | static const struct clk_ops ds3231_clk_sqw_ops = { |
| 1382 | .prepare = ds3231_clk_sqw_prepare, |
| 1383 | .unprepare = ds3231_clk_sqw_unprepare, |
| 1384 | .is_prepared = ds3231_clk_sqw_is_prepared, |
| 1385 | .recalc_rate = ds3231_clk_sqw_recalc_rate, |
| 1386 | .round_rate = ds3231_clk_sqw_round_rate, |
| 1387 | .set_rate = ds3231_clk_sqw_set_rate, |
| 1388 | }; |
| 1389 | |
| 1390 | static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw, |
Alexandre Belloni | 4057a66 | 2017-09-04 22:46:06 +0200 | [diff] [blame] | 1391 | unsigned long parent_rate) |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1392 | { |
| 1393 | return 32768; |
| 1394 | } |
| 1395 | |
| 1396 | static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable) |
| 1397 | { |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1398 | struct mutex *lock = &ds1307->rtc->ops_lock; |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1399 | int ret; |
| 1400 | |
| 1401 | mutex_lock(lock); |
Heiner Kallweit | 078f3f6 | 2017-06-05 17:57:29 +0200 | [diff] [blame] | 1402 | ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS, |
| 1403 | DS3231_BIT_EN32KHZ, |
| 1404 | enable ? DS3231_BIT_EN32KHZ : 0); |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1405 | mutex_unlock(lock); |
| 1406 | |
| 1407 | return ret; |
| 1408 | } |
| 1409 | |
| 1410 | static int ds3231_clk_32khz_prepare(struct clk_hw *hw) |
| 1411 | { |
| 1412 | struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw); |
| 1413 | |
| 1414 | return ds3231_clk_32khz_control(ds1307, true); |
| 1415 | } |
| 1416 | |
| 1417 | static void ds3231_clk_32khz_unprepare(struct clk_hw *hw) |
| 1418 | { |
| 1419 | struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw); |
| 1420 | |
| 1421 | ds3231_clk_32khz_control(ds1307, false); |
| 1422 | } |
| 1423 | |
| 1424 | static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw) |
| 1425 | { |
| 1426 | struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1427 | int status, ret; |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1428 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1429 | ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status); |
| 1430 | if (ret) |
| 1431 | return ret; |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1432 | |
| 1433 | return !!(status & DS3231_BIT_EN32KHZ); |
| 1434 | } |
| 1435 | |
| 1436 | static const struct clk_ops ds3231_clk_32khz_ops = { |
| 1437 | .prepare = ds3231_clk_32khz_prepare, |
| 1438 | .unprepare = ds3231_clk_32khz_unprepare, |
| 1439 | .is_prepared = ds3231_clk_32khz_is_prepared, |
| 1440 | .recalc_rate = ds3231_clk_32khz_recalc_rate, |
| 1441 | }; |
| 1442 | |
| 1443 | static struct clk_init_data ds3231_clks_init[] = { |
| 1444 | [DS3231_CLK_SQW] = { |
| 1445 | .name = "ds3231_clk_sqw", |
| 1446 | .ops = &ds3231_clk_sqw_ops, |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1447 | }, |
| 1448 | [DS3231_CLK_32KHZ] = { |
| 1449 | .name = "ds3231_clk_32khz", |
| 1450 | .ops = &ds3231_clk_32khz_ops, |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1451 | }, |
| 1452 | }; |
| 1453 | |
| 1454 | static int ds3231_clks_register(struct ds1307 *ds1307) |
| 1455 | { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1456 | struct device_node *node = ds1307->dev->of_node; |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1457 | struct clk_onecell_data *onecell; |
| 1458 | int i; |
| 1459 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1460 | onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL); |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1461 | if (!onecell) |
| 1462 | return -ENOMEM; |
| 1463 | |
| 1464 | onecell->clk_num = ARRAY_SIZE(ds3231_clks_init); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1465 | onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num, |
| 1466 | sizeof(onecell->clks[0]), GFP_KERNEL); |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1467 | if (!onecell->clks) |
| 1468 | return -ENOMEM; |
| 1469 | |
| 1470 | for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) { |
| 1471 | struct clk_init_data init = ds3231_clks_init[i]; |
| 1472 | |
| 1473 | /* |
| 1474 | * Interrupt signal due to alarm conditions and square-wave |
| 1475 | * output share same pin, so don't initialize both. |
| 1476 | */ |
| 1477 | if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags)) |
| 1478 | continue; |
| 1479 | |
| 1480 | /* optional override of the clockname */ |
| 1481 | of_property_read_string_index(node, "clock-output-names", i, |
Alexandre Belloni | 4057a66 | 2017-09-04 22:46:06 +0200 | [diff] [blame] | 1482 | &init.name); |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1483 | ds1307->clks[i].init = &init; |
| 1484 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1485 | onecell->clks[i] = devm_clk_register(ds1307->dev, |
| 1486 | &ds1307->clks[i]); |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1487 | if (IS_ERR(onecell->clks[i])) |
| 1488 | return PTR_ERR(onecell->clks[i]); |
| 1489 | } |
| 1490 | |
| 1491 | if (!node) |
| 1492 | return 0; |
| 1493 | |
| 1494 | of_clk_add_provider(node, of_clk_src_onecell_get, onecell); |
| 1495 | |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
| 1499 | static void ds1307_clks_register(struct ds1307 *ds1307) |
| 1500 | { |
| 1501 | int ret; |
| 1502 | |
| 1503 | if (ds1307->type != ds_3231) |
| 1504 | return; |
| 1505 | |
| 1506 | ret = ds3231_clks_register(ds1307); |
| 1507 | if (ret) { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1508 | dev_warn(ds1307->dev, "unable to register clock device %d\n", |
| 1509 | ret); |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | #else |
| 1514 | |
| 1515 | static void ds1307_clks_register(struct ds1307 *ds1307) |
| 1516 | { |
| 1517 | } |
| 1518 | |
| 1519 | #endif /* CONFIG_COMMON_CLK */ |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1520 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1521 | static const struct regmap_config regmap_config = { |
| 1522 | .reg_bits = 8, |
| 1523 | .val_bits = 8, |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1524 | }; |
| 1525 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 1526 | static int ds1307_probe(struct i2c_client *client, |
| 1527 | const struct i2c_device_id *id) |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1528 | { |
| 1529 | struct ds1307 *ds1307; |
| 1530 | int err = -ENODEV; |
Heiner Kallweit | 584ce30 | 2017-08-29 21:52:56 +0200 | [diff] [blame] | 1531 | int tmp; |
Heiner Kallweit | 7624df4 | 2017-07-12 07:49:33 +0200 | [diff] [blame] | 1532 | const struct chip_desc *chip; |
Heiner Kallweit | 82e2d43 | 2017-07-12 07:49:37 +0200 | [diff] [blame] | 1533 | bool want_irq; |
Michael Lange | 8bc2a40 | 2016-01-21 18:10:16 +0100 | [diff] [blame] | 1534 | bool ds1307_can_wakeup_device = false; |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1535 | unsigned char regs[8]; |
Jingoo Han | 01ce893 | 2013-11-12 15:10:41 -0800 | [diff] [blame] | 1536 | struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev); |
Heiner Kallweit | d8490fd | 2017-07-12 07:49:28 +0200 | [diff] [blame] | 1537 | u8 trickle_charger_setup = 0; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1538 | |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1539 | ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL); |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 1540 | if (!ds1307) |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1541 | return -ENOMEM; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1542 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1543 | dev_set_drvdata(&client->dev, ds1307); |
| 1544 | ds1307->dev = &client->dev; |
| 1545 | ds1307->name = client->name; |
Joakim Tjernlund | 33df2ee | 2009-06-17 16:26:08 -0700 | [diff] [blame] | 1546 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1547 | ds1307->regmap = devm_regmap_init_i2c(client, ®map_config); |
| 1548 | if (IS_ERR(ds1307->regmap)) { |
| 1549 | dev_err(ds1307->dev, "regmap allocation failed\n"); |
| 1550 | return PTR_ERR(ds1307->regmap); |
| 1551 | } |
| 1552 | |
| 1553 | i2c_set_clientdata(client, ds1307); |
Javier Martinez Canillas | 7ef6d2c | 2017-03-03 11:29:15 -0300 | [diff] [blame] | 1554 | |
| 1555 | if (client->dev.of_node) { |
| 1556 | ds1307->type = (enum ds_type) |
| 1557 | of_device_get_match_data(&client->dev); |
| 1558 | chip = &chips[ds1307->type]; |
| 1559 | } else if (id) { |
Tin Huynh | 9c19b89 | 2016-11-30 09:57:31 +0700 | [diff] [blame] | 1560 | chip = &chips[id->driver_data]; |
| 1561 | ds1307->type = id->driver_data; |
| 1562 | } else { |
| 1563 | const struct acpi_device_id *acpi_id; |
Joakim Tjernlund | 33df2ee | 2009-06-17 16:26:08 -0700 | [diff] [blame] | 1564 | |
Tin Huynh | 9c19b89 | 2016-11-30 09:57:31 +0700 | [diff] [blame] | 1565 | acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids), |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1566 | ds1307->dev); |
Tin Huynh | 9c19b89 | 2016-11-30 09:57:31 +0700 | [diff] [blame] | 1567 | if (!acpi_id) |
| 1568 | return -ENODEV; |
| 1569 | chip = &chips[acpi_id->driver_data]; |
| 1570 | ds1307->type = acpi_id->driver_data; |
| 1571 | } |
| 1572 | |
Heiner Kallweit | 82e2d43 | 2017-07-12 07:49:37 +0200 | [diff] [blame] | 1573 | want_irq = client->irq > 0 && chip->alarm; |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 1574 | |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 1575 | if (!pdata) |
Heiner Kallweit | d8490fd | 2017-07-12 07:49:28 +0200 | [diff] [blame] | 1576 | trickle_charger_setup = ds1307_trickle_init(ds1307, chip); |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 1577 | else if (pdata->trickle_charger_setup) |
Heiner Kallweit | d8490fd | 2017-07-12 07:49:28 +0200 | [diff] [blame] | 1578 | trickle_charger_setup = pdata->trickle_charger_setup; |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 1579 | |
Heiner Kallweit | d8490fd | 2017-07-12 07:49:28 +0200 | [diff] [blame] | 1580 | if (trickle_charger_setup && chip->trickle_charger_reg) { |
| 1581 | trickle_charger_setup |= DS13XX_TRICKLE_CHARGER_MAGIC; |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1582 | dev_dbg(ds1307->dev, |
| 1583 | "writing trickle charger info 0x%x to 0x%x\n", |
Heiner Kallweit | d8490fd | 2017-07-12 07:49:28 +0200 | [diff] [blame] | 1584 | trickle_charger_setup, chip->trickle_charger_reg); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1585 | regmap_write(ds1307->regmap, chip->trickle_charger_reg, |
Heiner Kallweit | d8490fd | 2017-07-12 07:49:28 +0200 | [diff] [blame] | 1586 | trickle_charger_setup); |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 1587 | } |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 1588 | |
Michael Lange | 8bc2a40 | 2016-01-21 18:10:16 +0100 | [diff] [blame] | 1589 | #ifdef CONFIG_OF |
| 1590 | /* |
| 1591 | * For devices with no IRQ directly connected to the SoC, the RTC chip |
| 1592 | * can be forced as a wakeup source by stating that explicitly in |
| 1593 | * the device's .dts file using the "wakeup-source" boolean property. |
| 1594 | * If the "wakeup-source" property is set, don't request an IRQ. |
| 1595 | * This will guarantee the 'wakealarm' sysfs entry is available on the device, |
| 1596 | * if supported by the RTC. |
| 1597 | */ |
Heiner Kallweit | 82e2d43 | 2017-07-12 07:49:37 +0200 | [diff] [blame] | 1598 | if (chip->alarm && of_property_read_bool(client->dev.of_node, |
| 1599 | "wakeup-source")) |
Michael Lange | 8bc2a40 | 2016-01-21 18:10:16 +0100 | [diff] [blame] | 1600 | ds1307_can_wakeup_device = true; |
Michael Lange | 8bc2a40 | 2016-01-21 18:10:16 +0100 | [diff] [blame] | 1601 | #endif |
| 1602 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1603 | switch (ds1307->type) { |
| 1604 | case ds_1337: |
| 1605 | case ds_1339: |
Nikita Yushchenko | 0759c88 | 2017-08-24 09:32:11 +0300 | [diff] [blame] | 1606 | case ds_1341: |
Wolfram Sang | 97f902b | 2009-06-17 16:26:10 -0700 | [diff] [blame] | 1607 | case ds_3231: |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1608 | /* get registers that the "rtc" read below won't read... */ |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1609 | err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL, |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1610 | regs, 2); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1611 | if (err) { |
| 1612 | dev_dbg(ds1307->dev, "read error %d\n", err); |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1613 | goto exit; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1614 | } |
| 1615 | |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1616 | /* oscillator off? turn it on, so clock can tick. */ |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1617 | if (regs[0] & DS1337_BIT_nEOSC) |
| 1618 | regs[0] &= ~DS1337_BIT_nEOSC; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1619 | |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 1620 | /* |
Michael Lange | 8bc2a40 | 2016-01-21 18:10:16 +0100 | [diff] [blame] | 1621 | * Using IRQ or defined as wakeup-source? |
| 1622 | * Disable the square wave and both alarms. |
Wolfram Sang | 97f902b | 2009-06-17 16:26:10 -0700 | [diff] [blame] | 1623 | * For some variants, be sure alarms can trigger when we're |
| 1624 | * running on Vbackup (BBSQI/BBSQW) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1625 | */ |
Heiner Kallweit | 82e2d43 | 2017-07-12 07:49:37 +0200 | [diff] [blame] | 1626 | if (want_irq || ds1307_can_wakeup_device) { |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1627 | regs[0] |= DS1337_BIT_INTCN | chip->bbsqi_bit; |
| 1628 | regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE); |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1629 | } |
| 1630 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1631 | regmap_write(ds1307->regmap, DS1337_REG_CONTROL, |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1632 | regs[0]); |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1633 | |
| 1634 | /* oscillator fault? clear flag, and warn */ |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1635 | if (regs[1] & DS1337_BIT_OSF) { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1636 | regmap_write(ds1307->regmap, DS1337_REG_STATUS, |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1637 | regs[1] & ~DS1337_BIT_OSF); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1638 | dev_warn(ds1307->dev, "SET TIME!\n"); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1639 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1640 | break; |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1641 | |
| 1642 | case rx_8025: |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1643 | err = regmap_bulk_read(ds1307->regmap, |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1644 | RX8025_REG_CTRL1 << 4 | 0x08, regs, 2); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1645 | if (err) { |
| 1646 | dev_dbg(ds1307->dev, "read error %d\n", err); |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1647 | goto exit; |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | /* oscillator off? turn it on, so clock can tick. */ |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1651 | if (!(regs[1] & RX8025_BIT_XST)) { |
| 1652 | regs[1] |= RX8025_BIT_XST; |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1653 | regmap_write(ds1307->regmap, |
| 1654 | RX8025_REG_CTRL2 << 4 | 0x08, |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1655 | regs[1]); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1656 | dev_warn(ds1307->dev, |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1657 | "oscillator stop detected - SET TIME!\n"); |
| 1658 | } |
| 1659 | |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1660 | if (regs[1] & RX8025_BIT_PON) { |
| 1661 | regs[1] &= ~RX8025_BIT_PON; |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1662 | regmap_write(ds1307->regmap, |
| 1663 | RX8025_REG_CTRL2 << 4 | 0x08, |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1664 | regs[1]); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1665 | dev_warn(ds1307->dev, "power-on detected\n"); |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1668 | if (regs[1] & RX8025_BIT_VDET) { |
| 1669 | regs[1] &= ~RX8025_BIT_VDET; |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1670 | regmap_write(ds1307->regmap, |
| 1671 | RX8025_REG_CTRL2 << 4 | 0x08, |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1672 | regs[1]); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1673 | dev_warn(ds1307->dev, "voltage drop detected\n"); |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | /* make sure we are running in 24hour mode */ |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1677 | if (!(regs[0] & RX8025_BIT_2412)) { |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1678 | u8 hour; |
| 1679 | |
| 1680 | /* switch to 24 hour mode */ |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1681 | regmap_write(ds1307->regmap, |
| 1682 | RX8025_REG_CTRL1 << 4 | 0x08, |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1683 | regs[0] | RX8025_BIT_2412); |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1684 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1685 | err = regmap_bulk_read(ds1307->regmap, |
| 1686 | RX8025_REG_CTRL1 << 4 | 0x08, |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1687 | regs, 2); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1688 | if (err) { |
| 1689 | dev_dbg(ds1307->dev, "read error %d\n", err); |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1690 | goto exit; |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | /* correct hour */ |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1694 | hour = bcd2bin(regs[DS1307_REG_HOUR]); |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1695 | if (hour == 12) |
| 1696 | hour = 0; |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1697 | if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM) |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1698 | hour += 12; |
| 1699 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1700 | regmap_write(ds1307->regmap, |
| 1701 | DS1307_REG_HOUR << 4 | 0x08, hour); |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1702 | } |
| 1703 | break; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1704 | default: |
| 1705 | break; |
| 1706 | } |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1707 | |
| 1708 | read_rtc: |
| 1709 | /* read RTC registers */ |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1710 | err = regmap_bulk_read(ds1307->regmap, chip->offset, regs, |
| 1711 | sizeof(regs)); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1712 | if (err) { |
| 1713 | dev_dbg(ds1307->dev, "read error %d\n", err); |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1714 | goto exit; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 1717 | /* |
| 1718 | * minimal sanity checking; some chips (like DS1340) don't |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1719 | * specify the extra bits as must-be-zero, but there are |
| 1720 | * still a few values that are clearly out-of-range. |
| 1721 | */ |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1722 | tmp = regs[DS1307_REG_SECS]; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1723 | switch (ds1307->type) { |
| 1724 | case ds_1307: |
Stefan Agner | 8566f70 | 2017-03-23 16:54:57 -0700 | [diff] [blame] | 1725 | case m41t0: |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1726 | case m41t00: |
Giulio Benetti | 7e58076 | 2018-05-16 23:08:40 +0200 | [diff] [blame] | 1727 | case m41t11: |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1728 | /* clock halted? turn it on, so clock can tick. */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1729 | if (tmp & DS1307_BIT_CH) { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1730 | regmap_write(ds1307->regmap, DS1307_REG_SECS, 0); |
| 1731 | dev_warn(ds1307->dev, "SET TIME!\n"); |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1732 | goto read_rtc; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1733 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1734 | break; |
Sean Nyekjaer | 300a773 | 2017-06-08 12:36:54 +0200 | [diff] [blame] | 1735 | case ds_1308: |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1736 | case ds_1338: |
| 1737 | /* clock halted? turn it on, so clock can tick. */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1738 | if (tmp & DS1307_BIT_CH) |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1739 | regmap_write(ds1307->regmap, DS1307_REG_SECS, 0); |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1740 | |
| 1741 | /* oscillator fault? clear flag, and warn */ |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1742 | if (regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1743 | regmap_write(ds1307->regmap, DS1307_REG_CONTROL, |
Alexandre Belloni | 4057a66 | 2017-09-04 22:46:06 +0200 | [diff] [blame] | 1744 | regs[DS1307_REG_CONTROL] & |
| 1745 | ~DS1338_BIT_OSF); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1746 | dev_warn(ds1307->dev, "SET TIME!\n"); |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1747 | goto read_rtc; |
| 1748 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1749 | break; |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 1750 | case ds_1340: |
| 1751 | /* clock halted? turn it on, so clock can tick. */ |
| 1752 | if (tmp & DS1340_BIT_nEOSC) |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1753 | regmap_write(ds1307->regmap, DS1307_REG_SECS, 0); |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 1754 | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1755 | err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp); |
| 1756 | if (err) { |
| 1757 | dev_dbg(ds1307->dev, "read error %d\n", err); |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1758 | goto exit; |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | /* oscillator fault? clear flag, and warn */ |
| 1762 | if (tmp & DS1340_BIT_OSF) { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1763 | regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0); |
| 1764 | dev_warn(ds1307->dev, "SET TIME!\n"); |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 1765 | } |
| 1766 | break; |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 1767 | case mcp794xx: |
David Anders | 43fcb81 | 2011-11-02 13:37:53 -0700 | [diff] [blame] | 1768 | /* make sure that the backup battery is enabled */ |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1769 | if (!(regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1770 | regmap_write(ds1307->regmap, DS1307_REG_WDAY, |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1771 | regs[DS1307_REG_WDAY] | |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1772 | MCP794XX_BIT_VBATEN); |
David Anders | 43fcb81 | 2011-11-02 13:37:53 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | /* clock halted? turn it on, so clock can tick. */ |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 1776 | if (!(tmp & MCP794XX_BIT_ST)) { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1777 | regmap_write(ds1307->regmap, DS1307_REG_SECS, |
| 1778 | MCP794XX_BIT_ST); |
| 1779 | dev_warn(ds1307->dev, "SET TIME!\n"); |
David Anders | 43fcb81 | 2011-11-02 13:37:53 -0700 | [diff] [blame] | 1780 | goto read_rtc; |
| 1781 | } |
| 1782 | |
| 1783 | break; |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 1784 | default: |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1785 | break; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1786 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1787 | |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1788 | tmp = regs[DS1307_REG_HOUR]; |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1789 | switch (ds1307->type) { |
| 1790 | case ds_1340: |
Stefan Agner | 8566f70 | 2017-03-23 16:54:57 -0700 | [diff] [blame] | 1791 | case m41t0: |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1792 | case m41t00: |
Giulio Benetti | 7e58076 | 2018-05-16 23:08:40 +0200 | [diff] [blame] | 1793 | case m41t11: |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 1794 | /* |
| 1795 | * NOTE: ignores century bits; fix before deploying |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1796 | * systems that will run through year 2100. |
| 1797 | */ |
| 1798 | break; |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1799 | case rx_8025: |
| 1800 | break; |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1801 | default: |
| 1802 | if (!(tmp & DS1307_BIT_12HR)) |
| 1803 | break; |
| 1804 | |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 1805 | /* |
| 1806 | * Be sure we're in 24 hour mode. Multi-master systems |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1807 | * take note... |
| 1808 | */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 1809 | tmp = bcd2bin(tmp & 0x1f); |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1810 | if (tmp == 12) |
| 1811 | tmp = 0; |
Alexandre Belloni | 042fa8c | 2017-09-04 22:46:02 +0200 | [diff] [blame] | 1812 | if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM) |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1813 | tmp += 12; |
Heiner Kallweit | e553170 | 2017-07-12 07:49:47 +0200 | [diff] [blame] | 1814 | regmap_write(ds1307->regmap, chip->offset + DS1307_REG_HOUR, |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1815 | bin2bcd(tmp)); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
Heiner Kallweit | 82e2d43 | 2017-07-12 07:49:37 +0200 | [diff] [blame] | 1818 | if (want_irq || ds1307_can_wakeup_device) { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1819 | device_set_wakeup_capable(ds1307->dev, true); |
Simon Guinot | 3abb1ad | 2015-11-26 15:37:13 +0100 | [diff] [blame] | 1820 | set_bit(HAS_ALARM, &ds1307->flags); |
| 1821 | } |
Alexandre Belloni | 69b119a | 2017-07-06 11:42:06 +0200 | [diff] [blame] | 1822 | |
| 1823 | ds1307->rtc = devm_rtc_allocate_device(ds1307->dev); |
Alexandre Belloni | e69c056 | 2017-09-04 22:46:07 +0200 | [diff] [blame] | 1824 | if (IS_ERR(ds1307->rtc)) |
Alessandro Zummo | 4071ea2 | 2014-04-03 14:49:36 -0700 | [diff] [blame] | 1825 | return PTR_ERR(ds1307->rtc); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1826 | |
Heiner Kallweit | 82e2d43 | 2017-07-12 07:49:37 +0200 | [diff] [blame] | 1827 | if (ds1307_can_wakeup_device && !want_irq) { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1828 | dev_info(ds1307->dev, |
| 1829 | "'wakeup-source' is set, request for an IRQ is disabled!\n"); |
Michael Lange | 8bc2a40 | 2016-01-21 18:10:16 +0100 | [diff] [blame] | 1830 | /* We cannot support UIE mode if we do not have an IRQ line */ |
| 1831 | ds1307->rtc->uie_unsupported = 1; |
| 1832 | } |
| 1833 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1834 | if (want_irq) { |
Heiner Kallweit | 4594712 | 2017-07-12 07:49:41 +0200 | [diff] [blame] | 1835 | err = devm_request_threaded_irq(ds1307->dev, client->irq, NULL, |
| 1836 | chip->irq_handler ?: ds1307_irq, |
Nishanth Menon | c598319 | 2015-06-23 11:15:11 -0500 | [diff] [blame] | 1837 | IRQF_SHARED | IRQF_ONESHOT, |
Alexandre Belloni | 4b9e2a0 | 2017-06-02 14:13:21 +0200 | [diff] [blame] | 1838 | ds1307->name, ds1307); |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1839 | if (err) { |
Alessandro Zummo | 4071ea2 | 2014-04-03 14:49:36 -0700 | [diff] [blame] | 1840 | client->irq = 0; |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1841 | device_set_wakeup_capable(ds1307->dev, false); |
Simon Guinot | 3abb1ad | 2015-11-26 15:37:13 +0100 | [diff] [blame] | 1842 | clear_bit(HAS_ALARM, &ds1307->flags); |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1843 | dev_err(ds1307->dev, "unable to request IRQ!\n"); |
Alexandre Belloni | e69c056 | 2017-09-04 22:46:07 +0200 | [diff] [blame] | 1844 | } else { |
Heiner Kallweit | 11e5890 | 2017-03-10 18:52:34 +0100 | [diff] [blame] | 1845 | dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq); |
Alexandre Belloni | e69c056 | 2017-09-04 22:46:07 +0200 | [diff] [blame] | 1846 | } |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
Alexandre Belloni | e9fb768 | 2018-02-12 23:47:22 +0100 | [diff] [blame] | 1849 | ds1307->rtc->ops = chip->rtc_ops ?: &ds13xx_rtc_ops; |
Alexandre Belloni | 6a5f2a1f | 2018-09-20 16:35:26 +0200 | [diff] [blame] | 1850 | err = ds1307_add_frequency_test(ds1307); |
Alexandre Belloni | e9fb768 | 2018-02-12 23:47:22 +0100 | [diff] [blame] | 1851 | if (err) |
| 1852 | return err; |
| 1853 | |
Alexandre Belloni | 6a5f2a1f | 2018-09-20 16:35:26 +0200 | [diff] [blame] | 1854 | err = rtc_register_device(ds1307->rtc); |
Giulio Benetti | b41c23e | 2018-07-25 19:26:05 +0200 | [diff] [blame] | 1855 | if (err) |
| 1856 | return err; |
| 1857 | |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 1858 | if (chip->nvram_size) { |
Alexandre Belloni | 409baf1 | 2018-02-12 23:47:23 +0100 | [diff] [blame] | 1859 | struct nvmem_config nvmem_cfg = { |
| 1860 | .name = "ds1307_nvram", |
| 1861 | .word_size = 1, |
| 1862 | .stride = 1, |
| 1863 | .size = chip->nvram_size, |
| 1864 | .reg_read = ds1307_nvram_read, |
| 1865 | .reg_write = ds1307_nvram_write, |
| 1866 | .priv = ds1307, |
| 1867 | }; |
Alessandro Zummo | 4071ea2 | 2014-04-03 14:49:36 -0700 | [diff] [blame] | 1868 | |
Alexandre Belloni | abc925f | 2017-07-06 11:42:07 +0200 | [diff] [blame] | 1869 | ds1307->rtc->nvram_old_abi = true; |
Alexandre Belloni | 409baf1 | 2018-02-12 23:47:23 +0100 | [diff] [blame] | 1870 | rtc_nvmem_register(ds1307->rtc, &nvmem_cfg); |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 1871 | } |
| 1872 | |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1873 | ds1307_hwmon_register(ds1307); |
Akinobu Mita | 6c6ff14 | 2016-01-31 23:10:10 +0900 | [diff] [blame] | 1874 | ds1307_clks_register(ds1307); |
Akinobu Mita | 445c020 | 2016-01-25 00:22:16 +0900 | [diff] [blame] | 1875 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1876 | return 0; |
| 1877 | |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1878 | exit: |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1879 | return err; |
| 1880 | } |
| 1881 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1882 | static struct i2c_driver ds1307_driver = { |
| 1883 | .driver = { |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1884 | .name = "rtc-ds1307", |
Javier Martinez Canillas | 7ef6d2c | 2017-03-03 11:29:15 -0300 | [diff] [blame] | 1885 | .of_match_table = of_match_ptr(ds1307_of_match), |
Tin Huynh | 9c19b89 | 2016-11-30 09:57:31 +0700 | [diff] [blame] | 1886 | .acpi_match_table = ACPI_PTR(ds1307_acpi_ids), |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1887 | }, |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1888 | .probe = ds1307_probe, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 1889 | .id_table = ds1307_id, |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1890 | }; |
| 1891 | |
Axel Lin | 0abc920 | 2012-03-23 15:02:31 -0700 | [diff] [blame] | 1892 | module_i2c_driver(ds1307_driver); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1893 | |
| 1894 | MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips"); |
| 1895 | MODULE_LICENSE("GPL"); |