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