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