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