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