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