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