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> |
Nishanth Menon | 7abea61 | 2015-06-24 11:26:54 -0500 | [diff] [blame] | 18 | #include <linux/of_device.h> |
| 19 | #include <linux/of_irq.h> |
| 20 | #include <linux/pm_wakeirq.h> |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 21 | #include <linux/rtc/ds1307.h> |
Nishanth Menon | eac7237 | 2015-06-23 11:15:12 -0500 | [diff] [blame] | 22 | #include <linux/rtc.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/string.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 |
| 95 | # define DS1337_BIT_A2I 0x02 |
| 96 | # define DS1337_BIT_A1I 0x01 |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 97 | #define DS1339_REG_ALARM1_SECS 0x07 |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 98 | |
| 99 | #define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 100 | |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 101 | #define RX8025_REG_CTRL1 0x0e |
| 102 | # define RX8025_BIT_2412 0x20 |
| 103 | #define RX8025_REG_CTRL2 0x0f |
| 104 | # define RX8025_BIT_PON 0x10 |
| 105 | # define RX8025_BIT_VDET 0x40 |
| 106 | # define RX8025_BIT_XST 0x20 |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 107 | |
| 108 | |
| 109 | struct ds1307 { |
Joakim Tjernlund | 33df2ee | 2009-06-17 16:26:08 -0700 | [diff] [blame] | 110 | u8 offset; /* register's offset */ |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 111 | u8 regs[11]; |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 112 | u16 nvram_offset; |
| 113 | struct bin_attribute *nvram; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 114 | enum ds_type type; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 115 | unsigned long flags; |
| 116 | #define HAS_NVRAM 0 /* bit 0 == sysfs file active */ |
| 117 | #define HAS_ALARM 1 /* bit 1 == irq claimed */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 118 | struct i2c_client *client; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 119 | struct rtc_device *rtc; |
Nishanth Menon | 7abea61 | 2015-06-24 11:26:54 -0500 | [diff] [blame] | 120 | int wakeirq; |
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); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 127 | struct chip_desc { |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 128 | unsigned alarm:1; |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 129 | u16 nvram_offset; |
| 130 | u16 nvram_size; |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 131 | u16 trickle_charger_reg; |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 132 | u8 trickle_charger_setup; |
| 133 | u8 (*do_trickle_setup)(struct i2c_client *, uint32_t, bool); |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 136 | static u8 do_trickle_setup_ds1339(struct i2c_client *, |
| 137 | uint32_t ohms, bool diode); |
| 138 | |
| 139 | static struct chip_desc chips[last_ds_type] = { |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 140 | [ds_1307] = { |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 141 | .nvram_offset = 8, |
| 142 | .nvram_size = 56, |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 143 | }, |
| 144 | [ds_1337] = { |
| 145 | .alarm = 1, |
| 146 | }, |
| 147 | [ds_1338] = { |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 148 | .nvram_offset = 8, |
| 149 | .nvram_size = 56, |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 150 | }, |
| 151 | [ds_1339] = { |
| 152 | .alarm = 1, |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 153 | .trickle_charger_reg = 0x10, |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 154 | .do_trickle_setup = &do_trickle_setup_ds1339, |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 155 | }, |
| 156 | [ds_1340] = { |
| 157 | .trickle_charger_reg = 0x08, |
| 158 | }, |
| 159 | [ds_1388] = { |
| 160 | .trickle_charger_reg = 0x0a, |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 161 | }, |
| 162 | [ds_3231] = { |
| 163 | .alarm = 1, |
| 164 | }, |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 165 | [mcp794xx] = { |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 166 | .alarm = 1, |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 167 | /* this is battery backed SRAM */ |
| 168 | .nvram_offset = 0x20, |
| 169 | .nvram_size = 0x40, |
| 170 | }, |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 171 | }; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 172 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 173 | static const struct i2c_device_id ds1307_id[] = { |
| 174 | { "ds1307", ds_1307 }, |
| 175 | { "ds1337", ds_1337 }, |
| 176 | { "ds1338", ds_1338 }, |
| 177 | { "ds1339", ds_1339 }, |
Joakim Tjernlund | 33df2ee | 2009-06-17 16:26:08 -0700 | [diff] [blame] | 178 | { "ds1388", ds_1388 }, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 179 | { "ds1340", ds_1340 }, |
Wolfram Sang | 97f902b | 2009-06-17 16:26:10 -0700 | [diff] [blame] | 180 | { "ds3231", ds_3231 }, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 181 | { "m41t00", m41t00 }, |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 182 | { "mcp7940x", mcp794xx }, |
| 183 | { "mcp7941x", mcp794xx }, |
Priyanka Jain | 31c1771 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 184 | { "pt7c4338", ds_1307 }, |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 185 | { "rx8025", rx_8025 }, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 186 | { } |
| 187 | }; |
| 188 | MODULE_DEVICE_TABLE(i2c, ds1307_id); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 189 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 190 | /*----------------------------------------------------------------------*/ |
| 191 | |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 192 | #define BLOCK_DATA_MAX_TRIES 10 |
| 193 | |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 194 | static s32 ds1307_read_block_data_once(const struct i2c_client *client, |
| 195 | u8 command, u8 length, u8 *values) |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 196 | { |
| 197 | s32 i, data; |
| 198 | |
| 199 | for (i = 0; i < length; i++) { |
| 200 | data = i2c_smbus_read_byte_data(client, command + i); |
| 201 | if (data < 0) |
| 202 | return data; |
| 203 | values[i] = data; |
| 204 | } |
| 205 | return i; |
| 206 | } |
| 207 | |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 208 | 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] | 209 | u8 length, u8 *values) |
| 210 | { |
Bertrand Achard | bc48b90 | 2013-04-29 16:19:26 -0700 | [diff] [blame] | 211 | u8 oldvalues[255]; |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 212 | s32 ret; |
| 213 | int tries = 0; |
| 214 | |
| 215 | dev_dbg(&client->dev, "ds1307_read_block_data (length=%d)\n", length); |
| 216 | ret = ds1307_read_block_data_once(client, command, length, values); |
| 217 | if (ret < 0) |
| 218 | return ret; |
| 219 | do { |
| 220 | if (++tries > BLOCK_DATA_MAX_TRIES) { |
| 221 | dev_err(&client->dev, |
| 222 | "ds1307_read_block_data failed\n"); |
| 223 | return -EIO; |
| 224 | } |
| 225 | memcpy(oldvalues, values, length); |
| 226 | ret = ds1307_read_block_data_once(client, command, length, |
| 227 | values); |
| 228 | if (ret < 0) |
| 229 | return ret; |
| 230 | } while (memcmp(oldvalues, values, length)); |
| 231 | return length; |
| 232 | } |
| 233 | |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 234 | 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] | 235 | u8 length, const u8 *values) |
| 236 | { |
Bertrand Achard | bc48b90 | 2013-04-29 16:19:26 -0700 | [diff] [blame] | 237 | u8 currvalues[255]; |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 238 | int tries = 0; |
| 239 | |
| 240 | dev_dbg(&client->dev, "ds1307_write_block_data (length=%d)\n", length); |
| 241 | do { |
| 242 | s32 i, ret; |
| 243 | |
| 244 | if (++tries > BLOCK_DATA_MAX_TRIES) { |
| 245 | dev_err(&client->dev, |
| 246 | "ds1307_write_block_data failed\n"); |
| 247 | return -EIO; |
| 248 | } |
| 249 | for (i = 0; i < length; i++) { |
| 250 | ret = i2c_smbus_write_byte_data(client, command + i, |
| 251 | values[i]); |
| 252 | if (ret < 0) |
| 253 | return ret; |
| 254 | } |
| 255 | ret = ds1307_read_block_data_once(client, command, length, |
| 256 | currvalues); |
| 257 | if (ret < 0) |
| 258 | return ret; |
| 259 | } while (memcmp(currvalues, values, length)); |
| 260 | return length; |
| 261 | } |
| 262 | |
| 263 | /*----------------------------------------------------------------------*/ |
| 264 | |
Bertrand Achard | bc48b90 | 2013-04-29 16:19:26 -0700 | [diff] [blame] | 265 | /* These RTC devices are not designed to be connected to a SMbus adapter. |
| 266 | SMbus limits block operations length to 32 bytes, whereas it's not |
| 267 | limited on I2C buses. As a result, accesses may exceed 32 bytes; |
| 268 | in that case, split them into smaller blocks */ |
| 269 | |
| 270 | static s32 ds1307_native_smbus_write_block_data(const struct i2c_client *client, |
| 271 | u8 command, u8 length, const u8 *values) |
| 272 | { |
| 273 | u8 suboffset = 0; |
| 274 | |
| 275 | if (length <= I2C_SMBUS_BLOCK_MAX) |
| 276 | return i2c_smbus_write_i2c_block_data(client, |
| 277 | command, length, values); |
| 278 | |
| 279 | while (suboffset < length) { |
| 280 | s32 retval = i2c_smbus_write_i2c_block_data(client, |
| 281 | command + suboffset, |
| 282 | min(I2C_SMBUS_BLOCK_MAX, length - suboffset), |
| 283 | values + suboffset); |
| 284 | if (retval < 0) |
| 285 | return retval; |
| 286 | |
| 287 | suboffset += I2C_SMBUS_BLOCK_MAX; |
| 288 | } |
| 289 | return length; |
| 290 | } |
| 291 | |
| 292 | static s32 ds1307_native_smbus_read_block_data(const struct i2c_client *client, |
| 293 | u8 command, u8 length, u8 *values) |
| 294 | { |
| 295 | u8 suboffset = 0; |
| 296 | |
| 297 | if (length <= I2C_SMBUS_BLOCK_MAX) |
| 298 | return i2c_smbus_read_i2c_block_data(client, |
| 299 | command, length, values); |
| 300 | |
| 301 | while (suboffset < length) { |
| 302 | s32 retval = i2c_smbus_read_i2c_block_data(client, |
| 303 | command + suboffset, |
| 304 | min(I2C_SMBUS_BLOCK_MAX, length - suboffset), |
| 305 | values + suboffset); |
| 306 | if (retval < 0) |
| 307 | return retval; |
| 308 | |
| 309 | suboffset += I2C_SMBUS_BLOCK_MAX; |
| 310 | } |
| 311 | return length; |
| 312 | } |
| 313 | |
| 314 | /*----------------------------------------------------------------------*/ |
| 315 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 316 | /* |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 317 | * The ds1337 and ds1339 both have two alarms, but we only use the first |
| 318 | * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm |
| 319 | * signal; ds1339 chips have only one alarm signal. |
| 320 | */ |
Felipe Balbi | 2fb07a1 | 2015-06-23 11:15:10 -0500 | [diff] [blame] | 321 | static irqreturn_t ds1307_irq(int irq, void *dev_id) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 322 | { |
Felipe Balbi | 2fb07a1 | 2015-06-23 11:15:10 -0500 | [diff] [blame] | 323 | struct i2c_client *client = dev_id; |
| 324 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 325 | struct mutex *lock = &ds1307->rtc->ops_lock; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 326 | int stat, control; |
| 327 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 328 | mutex_lock(lock); |
| 329 | stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS); |
| 330 | if (stat < 0) |
| 331 | goto out; |
| 332 | |
| 333 | if (stat & DS1337_BIT_A1I) { |
| 334 | stat &= ~DS1337_BIT_A1I; |
| 335 | i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, stat); |
| 336 | |
| 337 | control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL); |
| 338 | if (control < 0) |
| 339 | goto out; |
| 340 | |
| 341 | control &= ~DS1337_BIT_A1IE; |
| 342 | i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control); |
| 343 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 344 | rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF); |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | out: |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 348 | mutex_unlock(lock); |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 349 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 350 | return IRQ_HANDLED; |
| 351 | } |
| 352 | |
| 353 | /*----------------------------------------------------------------------*/ |
| 354 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 355 | static int ds1307_get_time(struct device *dev, struct rtc_time *t) |
| 356 | { |
| 357 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 358 | int tmp; |
| 359 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 360 | /* read the RTC date and time registers all at once */ |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 361 | tmp = ds1307->read_block_data(ds1307->client, |
Joakim Tjernlund | 33df2ee | 2009-06-17 16:26:08 -0700 | [diff] [blame] | 362 | ds1307->offset, 7, ds1307->regs); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 363 | if (tmp != 7) { |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 364 | dev_err(dev, "%s error %d\n", "read", tmp); |
| 365 | return -EIO; |
| 366 | } |
| 367 | |
Andy Shevchenko | 01a4ca1 | 2013-02-21 16:44:22 -0800 | [diff] [blame] | 368 | dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 369 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 370 | t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f); |
| 371 | t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 372 | tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 373 | t->tm_hour = bcd2bin(tmp); |
| 374 | t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1; |
| 375 | t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 376 | tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 377 | t->tm_mon = bcd2bin(tmp) - 1; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 378 | |
| 379 | /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 380 | t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 381 | |
| 382 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 383 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
| 384 | "read", t->tm_sec, t->tm_min, |
| 385 | t->tm_hour, t->tm_mday, |
| 386 | t->tm_mon, t->tm_year, t->tm_wday); |
| 387 | |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 388 | /* initial clock setting can be undefined */ |
| 389 | return rtc_valid_tm(t); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static int ds1307_set_time(struct device *dev, struct rtc_time *t) |
| 393 | { |
| 394 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
| 395 | int result; |
| 396 | int tmp; |
| 397 | u8 *buf = ds1307->regs; |
| 398 | |
| 399 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 400 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
Jeff Garzik | 11966ad | 2006-10-04 04:41:53 -0400 | [diff] [blame] | 401 | "write", t->tm_sec, t->tm_min, |
| 402 | t->tm_hour, t->tm_mday, |
| 403 | t->tm_mon, t->tm_year, t->tm_wday); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 404 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 405 | buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec); |
| 406 | buf[DS1307_REG_MIN] = bin2bcd(t->tm_min); |
| 407 | buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour); |
| 408 | buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1); |
| 409 | buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday); |
| 410 | buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 411 | |
| 412 | /* assume 20YY not 19YY */ |
| 413 | tmp = t->tm_year - 100; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 414 | buf[DS1307_REG_YEAR] = bin2bcd(tmp); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 415 | |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 416 | switch (ds1307->type) { |
| 417 | case ds_1337: |
| 418 | case ds_1339: |
Wolfram Sang | 97f902b | 2009-06-17 16:26:10 -0700 | [diff] [blame] | 419 | case ds_3231: |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 420 | buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY; |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 421 | break; |
| 422 | case ds_1340: |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 423 | buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN |
| 424 | | DS1340_BIT_CENTURY; |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 425 | break; |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 426 | case mcp794xx: |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 427 | /* |
| 428 | * these bits were cleared when preparing the date/time |
| 429 | * values and need to be set again before writing the |
| 430 | * buffer out to the device. |
| 431 | */ |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 432 | buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST; |
| 433 | buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN; |
David Anders | 43fcb81 | 2011-11-02 13:37:53 -0700 | [diff] [blame] | 434 | break; |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 435 | default: |
| 436 | break; |
| 437 | } |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 438 | |
Andy Shevchenko | 01a4ca1 | 2013-02-21 16:44:22 -0800 | [diff] [blame] | 439 | dev_dbg(dev, "%s: %7ph\n", "write", buf); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 440 | |
Joakim Tjernlund | 33df2ee | 2009-06-17 16:26:08 -0700 | [diff] [blame] | 441 | result = ds1307->write_block_data(ds1307->client, |
| 442 | ds1307->offset, 7, buf); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 443 | if (result < 0) { |
| 444 | dev_err(dev, "%s error %d\n", "write", result); |
| 445 | return result; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
Jüri Reitel | 74d88eb | 2009-01-07 18:07:16 -0800 | [diff] [blame] | 450 | static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 451 | { |
| 452 | struct i2c_client *client = to_i2c_client(dev); |
| 453 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 454 | int ret; |
| 455 | |
| 456 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 457 | return -EINVAL; |
| 458 | |
| 459 | /* read all ALARM1, ALARM2, and status registers at once */ |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 460 | ret = ds1307->read_block_data(client, |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 461 | DS1339_REG_ALARM1_SECS, 9, ds1307->regs); |
| 462 | if (ret != 9) { |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 463 | dev_err(dev, "%s error %d\n", "alarm read", ret); |
| 464 | return -EIO; |
| 465 | } |
| 466 | |
| 467 | dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n", |
| 468 | "alarm read", |
| 469 | ds1307->regs[0], ds1307->regs[1], |
| 470 | ds1307->regs[2], ds1307->regs[3], |
| 471 | ds1307->regs[4], ds1307->regs[5], |
| 472 | ds1307->regs[6], ds1307->regs[7], |
| 473 | ds1307->regs[8]); |
| 474 | |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 475 | /* |
| 476 | * report alarm time (ALARM1); assume 24 hour and day-of-month modes, |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 477 | * and that all four fields are checked matches |
| 478 | */ |
| 479 | t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f); |
| 480 | t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f); |
| 481 | t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f); |
| 482 | t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f); |
| 483 | t->time.tm_mon = -1; |
| 484 | t->time.tm_year = -1; |
| 485 | t->time.tm_wday = -1; |
| 486 | t->time.tm_yday = -1; |
| 487 | t->time.tm_isdst = -1; |
| 488 | |
| 489 | /* ... and status */ |
| 490 | t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE); |
| 491 | t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I); |
| 492 | |
| 493 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 494 | "hours=%d, mday=%d, enabled=%d, pending=%d\n", |
| 495 | "alarm read", t->time.tm_sec, t->time.tm_min, |
| 496 | t->time.tm_hour, t->time.tm_mday, |
| 497 | t->enabled, t->pending); |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
Jüri Reitel | 74d88eb | 2009-01-07 18:07:16 -0800 | [diff] [blame] | 502 | static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 503 | { |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 504 | struct i2c_client *client = to_i2c_client(dev); |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 505 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 506 | unsigned char *buf = ds1307->regs; |
| 507 | u8 control, status; |
| 508 | int ret; |
| 509 | |
| 510 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 511 | return -EINVAL; |
| 512 | |
| 513 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
| 514 | "hours=%d, mday=%d, enabled=%d, pending=%d\n", |
| 515 | "alarm set", t->time.tm_sec, t->time.tm_min, |
| 516 | t->time.tm_hour, t->time.tm_mday, |
| 517 | t->enabled, t->pending); |
| 518 | |
| 519 | /* read current status of both alarms and the chip */ |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 520 | ret = ds1307->read_block_data(client, |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 521 | DS1339_REG_ALARM1_SECS, 9, buf); |
| 522 | if (ret != 9) { |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 523 | dev_err(dev, "%s error %d\n", "alarm write", ret); |
| 524 | return -EIO; |
| 525 | } |
| 526 | control = ds1307->regs[7]; |
| 527 | status = ds1307->regs[8]; |
| 528 | |
| 529 | dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n", |
| 530 | "alarm set (old status)", |
| 531 | ds1307->regs[0], ds1307->regs[1], |
| 532 | ds1307->regs[2], ds1307->regs[3], |
| 533 | ds1307->regs[4], ds1307->regs[5], |
| 534 | ds1307->regs[6], control, status); |
| 535 | |
| 536 | /* set ALARM1, using 24 hour and day-of-month modes */ |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 537 | buf[0] = bin2bcd(t->time.tm_sec); |
| 538 | buf[1] = bin2bcd(t->time.tm_min); |
| 539 | buf[2] = bin2bcd(t->time.tm_hour); |
| 540 | buf[3] = bin2bcd(t->time.tm_mday); |
| 541 | |
| 542 | /* set ALARM2 to non-garbage */ |
| 543 | buf[4] = 0; |
| 544 | buf[5] = 0; |
| 545 | buf[6] = 0; |
| 546 | |
| 547 | /* optionally enable ALARM1 */ |
| 548 | buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE); |
| 549 | if (t->enabled) { |
| 550 | dev_dbg(dev, "alarm IRQ armed\n"); |
| 551 | buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */ |
| 552 | } |
| 553 | buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I); |
| 554 | |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 555 | ret = ds1307->write_block_data(client, |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 556 | DS1339_REG_ALARM1_SECS, 9, buf); |
| 557 | if (ret < 0) { |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 558 | dev_err(dev, "can't set alarm time\n"); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 559 | return ret; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 565 | static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 566 | { |
| 567 | struct i2c_client *client = to_i2c_client(dev); |
| 568 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 569 | int ret; |
| 570 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 571 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 572 | return -ENOTTY; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 573 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 574 | ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL); |
| 575 | if (ret < 0) |
| 576 | return ret; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 577 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 578 | if (enabled) |
| 579 | ret |= DS1337_BIT_A1IE; |
| 580 | else |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 581 | ret &= ~DS1337_BIT_A1IE; |
| 582 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 583 | ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, ret); |
| 584 | if (ret < 0) |
| 585 | return ret; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 590 | static const struct rtc_class_ops ds13xx_rtc_ops = { |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 591 | .read_time = ds1307_get_time, |
| 592 | .set_time = ds1307_set_time, |
Jüri Reitel | 74d88eb | 2009-01-07 18:07:16 -0800 | [diff] [blame] | 593 | .read_alarm = ds1337_read_alarm, |
| 594 | .set_alarm = ds1337_set_alarm, |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 595 | .alarm_irq_enable = ds1307_alarm_irq_enable, |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 596 | }; |
| 597 | |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 598 | /*----------------------------------------------------------------------*/ |
| 599 | |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 600 | /* |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 601 | * Alarm support for mcp794xx devices. |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 602 | */ |
| 603 | |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 604 | #define MCP794XX_REG_CONTROL 0x07 |
| 605 | # define MCP794XX_BIT_ALM0_EN 0x10 |
| 606 | # define MCP794XX_BIT_ALM1_EN 0x20 |
| 607 | #define MCP794XX_REG_ALARM0_BASE 0x0a |
| 608 | #define MCP794XX_REG_ALARM0_CTRL 0x0d |
| 609 | #define MCP794XX_REG_ALARM1_BASE 0x11 |
| 610 | #define MCP794XX_REG_ALARM1_CTRL 0x14 |
| 611 | # define MCP794XX_BIT_ALMX_IF (1 << 3) |
| 612 | # define MCP794XX_BIT_ALMX_C0 (1 << 4) |
| 613 | # define MCP794XX_BIT_ALMX_C1 (1 << 5) |
| 614 | # define MCP794XX_BIT_ALMX_C2 (1 << 6) |
| 615 | # define MCP794XX_BIT_ALMX_POL (1 << 7) |
| 616 | # define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \ |
| 617 | MCP794XX_BIT_ALMX_C1 | \ |
| 618 | MCP794XX_BIT_ALMX_C2) |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 619 | |
Felipe Balbi | 2fb07a1 | 2015-06-23 11:15:10 -0500 | [diff] [blame] | 620 | static irqreturn_t mcp794xx_irq(int irq, void *dev_id) |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 621 | { |
Felipe Balbi | 2fb07a1 | 2015-06-23 11:15:10 -0500 | [diff] [blame] | 622 | struct i2c_client *client = dev_id; |
| 623 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 624 | struct mutex *lock = &ds1307->rtc->ops_lock; |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 625 | int reg, ret; |
| 626 | |
Felipe Balbi | 2fb07a1 | 2015-06-23 11:15:10 -0500 | [diff] [blame] | 627 | mutex_lock(lock); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 628 | |
| 629 | /* Check and clear alarm 0 interrupt flag. */ |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 630 | reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_ALARM0_CTRL); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 631 | if (reg < 0) |
| 632 | goto out; |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 633 | if (!(reg & MCP794XX_BIT_ALMX_IF)) |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 634 | goto out; |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 635 | reg &= ~MCP794XX_BIT_ALMX_IF; |
| 636 | ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_ALARM0_CTRL, reg); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 637 | if (ret < 0) |
| 638 | goto out; |
| 639 | |
| 640 | /* Disable alarm 0. */ |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 641 | reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 642 | if (reg < 0) |
| 643 | goto out; |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 644 | reg &= ~MCP794XX_BIT_ALM0_EN; |
| 645 | ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 646 | if (ret < 0) |
| 647 | goto out; |
| 648 | |
| 649 | rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF); |
| 650 | |
| 651 | out: |
Felipe Balbi | 2fb07a1 | 2015-06-23 11:15:10 -0500 | [diff] [blame] | 652 | mutex_unlock(lock); |
| 653 | |
| 654 | return IRQ_HANDLED; |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 657 | static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 658 | { |
| 659 | struct i2c_client *client = to_i2c_client(dev); |
| 660 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 661 | u8 *regs = ds1307->regs; |
| 662 | int ret; |
| 663 | |
| 664 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 665 | return -EINVAL; |
| 666 | |
| 667 | /* Read control and alarm 0 registers. */ |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 668 | ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 669 | if (ret < 0) |
| 670 | return ret; |
| 671 | |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 672 | t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 673 | |
| 674 | /* Report alarm 0 time assuming 24-hour and day-of-month modes. */ |
| 675 | t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f); |
| 676 | t->time.tm_min = bcd2bin(ds1307->regs[4] & 0x7f); |
| 677 | t->time.tm_hour = bcd2bin(ds1307->regs[5] & 0x3f); |
| 678 | t->time.tm_wday = bcd2bin(ds1307->regs[6] & 0x7) - 1; |
| 679 | t->time.tm_mday = bcd2bin(ds1307->regs[7] & 0x3f); |
| 680 | t->time.tm_mon = bcd2bin(ds1307->regs[8] & 0x1f) - 1; |
| 681 | t->time.tm_year = -1; |
| 682 | t->time.tm_yday = -1; |
| 683 | t->time.tm_isdst = -1; |
| 684 | |
| 685 | dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d " |
| 686 | "enabled=%d polarity=%d irq=%d match=%d\n", __func__, |
| 687 | t->time.tm_sec, t->time.tm_min, t->time.tm_hour, |
| 688 | 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] | 689 | !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL), |
| 690 | !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF), |
| 691 | (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 696 | static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 697 | { |
| 698 | struct i2c_client *client = to_i2c_client(dev); |
| 699 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 700 | unsigned char *regs = ds1307->regs; |
| 701 | int ret; |
| 702 | |
| 703 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 704 | return -EINVAL; |
| 705 | |
| 706 | dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d " |
| 707 | "enabled=%d pending=%d\n", __func__, |
| 708 | t->time.tm_sec, t->time.tm_min, t->time.tm_hour, |
| 709 | t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, |
| 710 | t->enabled, t->pending); |
| 711 | |
| 712 | /* Read control and alarm 0 registers. */ |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 713 | ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 714 | if (ret < 0) |
| 715 | return ret; |
| 716 | |
| 717 | /* Set alarm 0, using 24-hour and day-of-month modes. */ |
| 718 | regs[3] = bin2bcd(t->time.tm_sec); |
| 719 | regs[4] = bin2bcd(t->time.tm_min); |
| 720 | regs[5] = bin2bcd(t->time.tm_hour); |
| 721 | regs[6] = bin2bcd(t->time.tm_wday) + 1; |
| 722 | regs[7] = bin2bcd(t->time.tm_mday); |
| 723 | regs[8] = bin2bcd(t->time.tm_mon) + 1; |
| 724 | |
| 725 | /* Clear the alarm 0 interrupt flag. */ |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 726 | regs[6] &= ~MCP794XX_BIT_ALMX_IF; |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 727 | /* Set alarm match: second, minute, hour, day, date, month. */ |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 728 | regs[6] |= MCP794XX_MSK_ALMX_MATCH; |
Nishanth Menon | e3edd67 | 2015-04-20 19:51:34 -0500 | [diff] [blame] | 729 | /* Disable interrupt. We will not enable until completely programmed */ |
| 730 | regs[0] &= ~MCP794XX_BIT_ALM0_EN; |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 731 | |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 732 | ret = ds1307->write_block_data(client, MCP794XX_REG_CONTROL, 10, regs); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 733 | if (ret < 0) |
| 734 | return ret; |
| 735 | |
Nishanth Menon | e3edd67 | 2015-04-20 19:51:34 -0500 | [diff] [blame] | 736 | if (!t->enabled) |
| 737 | return 0; |
| 738 | regs[0] |= MCP794XX_BIT_ALM0_EN; |
| 739 | return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, regs[0]); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 742 | static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled) |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 743 | { |
| 744 | struct i2c_client *client = to_i2c_client(dev); |
| 745 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
| 746 | int reg; |
| 747 | |
| 748 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
| 749 | return -EINVAL; |
| 750 | |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 751 | reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 752 | if (reg < 0) |
| 753 | return reg; |
| 754 | |
| 755 | if (enabled) |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 756 | reg |= MCP794XX_BIT_ALM0_EN; |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 757 | else |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 758 | reg &= ~MCP794XX_BIT_ALM0_EN; |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 759 | |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 760 | return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg); |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 763 | static const struct rtc_class_ops mcp794xx_rtc_ops = { |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 764 | .read_time = ds1307_get_time, |
| 765 | .set_time = ds1307_set_time, |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 766 | .read_alarm = mcp794xx_read_alarm, |
| 767 | .set_alarm = mcp794xx_set_alarm, |
| 768 | .alarm_irq_enable = mcp794xx_alarm_irq_enable, |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 769 | }; |
| 770 | |
| 771 | /*----------------------------------------------------------------------*/ |
| 772 | |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 773 | static ssize_t |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 774 | ds1307_nvram_read(struct file *filp, struct kobject *kobj, |
| 775 | struct bin_attribute *attr, |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 776 | char *buf, loff_t off, size_t count) |
| 777 | { |
| 778 | struct i2c_client *client; |
| 779 | struct ds1307 *ds1307; |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 780 | int result; |
| 781 | |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 782 | client = kobj_to_i2c_client(kobj); |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 783 | ds1307 = i2c_get_clientdata(client); |
| 784 | |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 785 | result = ds1307->read_block_data(client, ds1307->nvram_offset + off, |
| 786 | count, buf); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 787 | if (result < 0) |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 788 | dev_err(&client->dev, "%s error %d\n", "nvram read", result); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 789 | return result; |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | static ssize_t |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 793 | ds1307_nvram_write(struct file *filp, struct kobject *kobj, |
| 794 | struct bin_attribute *attr, |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 795 | char *buf, loff_t off, size_t count) |
| 796 | { |
| 797 | struct i2c_client *client; |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 798 | struct ds1307 *ds1307; |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 799 | int result; |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 800 | |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 801 | client = kobj_to_i2c_client(kobj); |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 802 | ds1307 = i2c_get_clientdata(client); |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 803 | |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 804 | result = ds1307->write_block_data(client, ds1307->nvram_offset + off, |
| 805 | count, buf); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 806 | if (result < 0) { |
| 807 | dev_err(&client->dev, "%s error %d\n", "nvram write", result); |
| 808 | return result; |
| 809 | } |
| 810 | return count; |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 813 | |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 814 | /*----------------------------------------------------------------------*/ |
| 815 | |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 816 | static u8 do_trickle_setup_ds1339(struct i2c_client *client, |
| 817 | uint32_t ohms, bool diode) |
| 818 | { |
| 819 | u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE : |
| 820 | DS1307_TRICKLE_CHARGER_NO_DIODE; |
| 821 | |
| 822 | switch (ohms) { |
| 823 | case 250: |
| 824 | setup |= DS1307_TRICKLE_CHARGER_250_OHM; |
| 825 | break; |
| 826 | case 2000: |
| 827 | setup |= DS1307_TRICKLE_CHARGER_2K_OHM; |
| 828 | break; |
| 829 | case 4000: |
| 830 | setup |= DS1307_TRICKLE_CHARGER_4K_OHM; |
| 831 | break; |
| 832 | default: |
| 833 | dev_warn(&client->dev, |
| 834 | "Unsupported ohm value %u in dt\n", ohms); |
| 835 | return 0; |
| 836 | } |
| 837 | return setup; |
| 838 | } |
| 839 | |
| 840 | static void ds1307_trickle_of_init(struct i2c_client *client, |
| 841 | struct chip_desc *chip) |
| 842 | { |
| 843 | uint32_t ohms = 0; |
| 844 | bool diode = true; |
| 845 | |
| 846 | if (!chip->do_trickle_setup) |
| 847 | goto out; |
| 848 | if (of_property_read_u32(client->dev.of_node, "trickle-resistor-ohms" , &ohms)) |
| 849 | goto out; |
| 850 | if (of_property_read_bool(client->dev.of_node, "trickle-diode-disable")) |
| 851 | diode = false; |
| 852 | chip->trickle_charger_setup = chip->do_trickle_setup(client, |
| 853 | ohms, diode); |
| 854 | out: |
| 855 | return; |
| 856 | } |
| 857 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 858 | static int ds1307_probe(struct i2c_client *client, |
| 859 | const struct i2c_device_id *id) |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 860 | { |
| 861 | struct ds1307 *ds1307; |
| 862 | int err = -ENODEV; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 863 | int tmp; |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 864 | struct chip_desc *chip = &chips[id->driver_data]; |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 865 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
Peter Senna Tschudin | c8b18da | 2013-11-12 15:10:59 -0800 | [diff] [blame] | 866 | bool want_irq = false; |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 867 | unsigned char *buf; |
Jingoo Han | 01ce893 | 2013-11-12 15:10:41 -0800 | [diff] [blame] | 868 | struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev); |
Felipe Balbi | 2fb07a1 | 2015-06-23 11:15:10 -0500 | [diff] [blame] | 869 | irq_handler_t irq_handler = ds1307_irq; |
| 870 | |
Wolfram Sang | 97f902b | 2009-06-17 16:26:10 -0700 | [diff] [blame] | 871 | static const int bbsqi_bitpos[] = { |
| 872 | [ds_1337] = 0, |
| 873 | [ds_1339] = DS1339_BIT_BBSQI, |
| 874 | [ds_3231] = DS3231_BIT_BBSQW, |
| 875 | }; |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 876 | const struct rtc_class_ops *rtc_ops = &ds13xx_rtc_ops; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 877 | |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 878 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA) |
| 879 | && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 880 | return -EIO; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 881 | |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 882 | ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL); |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 883 | if (!ds1307) |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 884 | return -ENOMEM; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 885 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 886 | i2c_set_clientdata(client, ds1307); |
Joakim Tjernlund | 33df2ee | 2009-06-17 16:26:08 -0700 | [diff] [blame] | 887 | |
| 888 | ds1307->client = client; |
| 889 | ds1307->type = id->driver_data; |
Joakim Tjernlund | 33df2ee | 2009-06-17 16:26:08 -0700 | [diff] [blame] | 890 | |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 891 | if (!pdata && client->dev.of_node) |
| 892 | ds1307_trickle_of_init(client, chip); |
| 893 | else if (pdata && pdata->trickle_charger_setup) |
| 894 | chip->trickle_charger_setup = pdata->trickle_charger_setup; |
| 895 | |
| 896 | if (chip->trickle_charger_setup && chip->trickle_charger_reg) { |
| 897 | dev_dbg(&client->dev, "writing trickle charger info 0x%x to 0x%x\n", |
| 898 | DS13XX_TRICKLE_CHARGER_MAGIC | chip->trickle_charger_setup, |
| 899 | chip->trickle_charger_reg); |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 900 | i2c_smbus_write_byte_data(client, chip->trickle_charger_reg, |
Matti Vaittinen | 33b04b7 | 2014-10-13 15:52:48 -0700 | [diff] [blame] | 901 | DS13XX_TRICKLE_CHARGER_MAGIC | |
| 902 | chip->trickle_charger_setup); |
| 903 | } |
Wolfram Sang | eb86c30 | 2012-05-29 15:07:38 -0700 | [diff] [blame] | 904 | |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 905 | buf = ds1307->regs; |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 906 | if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) { |
Bertrand Achard | bc48b90 | 2013-04-29 16:19:26 -0700 | [diff] [blame] | 907 | ds1307->read_block_data = ds1307_native_smbus_read_block_data; |
| 908 | ds1307->write_block_data = ds1307_native_smbus_write_block_data; |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 909 | } else { |
| 910 | ds1307->read_block_data = ds1307_read_block_data; |
| 911 | ds1307->write_block_data = ds1307_write_block_data; |
| 912 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 913 | |
| 914 | switch (ds1307->type) { |
| 915 | case ds_1337: |
| 916 | case ds_1339: |
Wolfram Sang | 97f902b | 2009-06-17 16:26:10 -0700 | [diff] [blame] | 917 | case ds_3231: |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 918 | /* get registers that the "rtc" read below won't read... */ |
Ed Swierk | 30e7b03 | 2009-03-31 15:24:56 -0700 | [diff] [blame] | 919 | tmp = ds1307->read_block_data(ds1307->client, |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 920 | DS1337_REG_CONTROL, 2, buf); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 921 | if (tmp != 2) { |
Jingoo Han | 6df80e2 | 2013-04-29 16:19:28 -0700 | [diff] [blame] | 922 | dev_dbg(&client->dev, "read error %d\n", tmp); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 923 | err = -EIO; |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 924 | goto exit; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 927 | /* oscillator off? turn it on, so clock can tick. */ |
| 928 | if (ds1307->regs[0] & DS1337_BIT_nEOSC) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 929 | ds1307->regs[0] &= ~DS1337_BIT_nEOSC; |
| 930 | |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 931 | /* |
| 932 | * Using IRQ? Disable the square wave and both alarms. |
Wolfram Sang | 97f902b | 2009-06-17 16:26:10 -0700 | [diff] [blame] | 933 | * For some variants, be sure alarms can trigger when we're |
| 934 | * running on Vbackup (BBSQI/BBSQW) |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 935 | */ |
Wolfram Sang | b24a726 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 936 | if (ds1307->client->irq > 0 && chip->alarm) { |
Wolfram Sang | 97f902b | 2009-06-17 16:26:10 -0700 | [diff] [blame] | 937 | ds1307->regs[0] |= DS1337_BIT_INTCN |
| 938 | | bbsqi_bitpos[ds1307->type]; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 939 | ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE); |
Wolfram Sang | b24a726 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 940 | |
| 941 | want_irq = true; |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, |
| 945 | ds1307->regs[0]); |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 946 | |
| 947 | /* oscillator fault? clear flag, and warn */ |
| 948 | if (ds1307->regs[1] & DS1337_BIT_OSF) { |
| 949 | i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, |
| 950 | ds1307->regs[1] & ~DS1337_BIT_OSF); |
| 951 | dev_warn(&client->dev, "SET TIME!\n"); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 952 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 953 | break; |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 954 | |
| 955 | case rx_8025: |
| 956 | tmp = i2c_smbus_read_i2c_block_data(ds1307->client, |
| 957 | RX8025_REG_CTRL1 << 4 | 0x08, 2, buf); |
| 958 | if (tmp != 2) { |
Jingoo Han | 6df80e2 | 2013-04-29 16:19:28 -0700 | [diff] [blame] | 959 | dev_dbg(&client->dev, "read error %d\n", tmp); |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 960 | err = -EIO; |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 961 | goto exit; |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | /* oscillator off? turn it on, so clock can tick. */ |
| 965 | if (!(ds1307->regs[1] & RX8025_BIT_XST)) { |
| 966 | ds1307->regs[1] |= RX8025_BIT_XST; |
| 967 | i2c_smbus_write_byte_data(client, |
| 968 | RX8025_REG_CTRL2 << 4 | 0x08, |
| 969 | ds1307->regs[1]); |
| 970 | dev_warn(&client->dev, |
| 971 | "oscillator stop detected - SET TIME!\n"); |
| 972 | } |
| 973 | |
| 974 | if (ds1307->regs[1] & RX8025_BIT_PON) { |
| 975 | ds1307->regs[1] &= ~RX8025_BIT_PON; |
| 976 | i2c_smbus_write_byte_data(client, |
| 977 | RX8025_REG_CTRL2 << 4 | 0x08, |
| 978 | ds1307->regs[1]); |
| 979 | dev_warn(&client->dev, "power-on detected\n"); |
| 980 | } |
| 981 | |
| 982 | if (ds1307->regs[1] & RX8025_BIT_VDET) { |
| 983 | ds1307->regs[1] &= ~RX8025_BIT_VDET; |
| 984 | i2c_smbus_write_byte_data(client, |
| 985 | RX8025_REG_CTRL2 << 4 | 0x08, |
| 986 | ds1307->regs[1]); |
| 987 | dev_warn(&client->dev, "voltage drop detected\n"); |
| 988 | } |
| 989 | |
| 990 | /* make sure we are running in 24hour mode */ |
| 991 | if (!(ds1307->regs[0] & RX8025_BIT_2412)) { |
| 992 | u8 hour; |
| 993 | |
| 994 | /* switch to 24 hour mode */ |
| 995 | i2c_smbus_write_byte_data(client, |
| 996 | RX8025_REG_CTRL1 << 4 | 0x08, |
| 997 | ds1307->regs[0] | |
| 998 | RX8025_BIT_2412); |
| 999 | |
| 1000 | tmp = i2c_smbus_read_i2c_block_data(ds1307->client, |
| 1001 | RX8025_REG_CTRL1 << 4 | 0x08, 2, buf); |
| 1002 | if (tmp != 2) { |
Jingoo Han | 6df80e2 | 2013-04-29 16:19:28 -0700 | [diff] [blame] | 1003 | dev_dbg(&client->dev, "read error %d\n", tmp); |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1004 | err = -EIO; |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1005 | goto exit; |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | /* correct hour */ |
| 1009 | hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]); |
| 1010 | if (hour == 12) |
| 1011 | hour = 0; |
| 1012 | if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM) |
| 1013 | hour += 12; |
| 1014 | |
| 1015 | i2c_smbus_write_byte_data(client, |
| 1016 | DS1307_REG_HOUR << 4 | 0x08, |
| 1017 | hour); |
| 1018 | } |
| 1019 | break; |
Joakim Tjernlund | 33df2ee | 2009-06-17 16:26:08 -0700 | [diff] [blame] | 1020 | case ds_1388: |
| 1021 | ds1307->offset = 1; /* Seconds starts at 1 */ |
| 1022 | break; |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 1023 | case mcp794xx: |
| 1024 | rtc_ops = &mcp794xx_rtc_ops; |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 1025 | if (ds1307->client->irq > 0 && chip->alarm) { |
Felipe Balbi | 2fb07a1 | 2015-06-23 11:15:10 -0500 | [diff] [blame] | 1026 | irq_handler = mcp794xx_irq; |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 1027 | want_irq = true; |
| 1028 | } |
| 1029 | break; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1030 | default: |
| 1031 | break; |
| 1032 | } |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1033 | |
| 1034 | read_rtc: |
| 1035 | /* read RTC registers */ |
Joakim Tjernlund | 96fc3a4 | 2010-06-29 15:05:34 -0700 | [diff] [blame] | 1036 | tmp = ds1307->read_block_data(ds1307->client, ds1307->offset, 8, buf); |
BARRE Sebastien | fed40b7 | 2009-01-07 18:07:13 -0800 | [diff] [blame] | 1037 | if (tmp != 8) { |
Jingoo Han | 6df80e2 | 2013-04-29 16:19:28 -0700 | [diff] [blame] | 1038 | dev_dbg(&client->dev, "read error %d\n", tmp); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1039 | err = -EIO; |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1040 | goto exit; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 1043 | /* |
| 1044 | * minimal sanity checking; some chips (like DS1340) don't |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1045 | * specify the extra bits as must-be-zero, but there are |
| 1046 | * still a few values that are clearly out-of-range. |
| 1047 | */ |
| 1048 | tmp = ds1307->regs[DS1307_REG_SECS]; |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1049 | switch (ds1307->type) { |
| 1050 | case ds_1307: |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1051 | case m41t00: |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1052 | /* clock halted? turn it on, so clock can tick. */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1053 | if (tmp & DS1307_BIT_CH) { |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1054 | i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0); |
| 1055 | dev_warn(&client->dev, "SET TIME!\n"); |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1056 | goto read_rtc; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1057 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1058 | break; |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1059 | case ds_1338: |
| 1060 | /* clock halted? turn it on, so clock can tick. */ |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1061 | if (tmp & DS1307_BIT_CH) |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1062 | i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0); |
| 1063 | |
| 1064 | /* oscillator fault? clear flag, and warn */ |
| 1065 | if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) { |
| 1066 | i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL, |
David Brownell | bd16f9e | 2007-07-26 10:41:00 -0700 | [diff] [blame] | 1067 | ds1307->regs[DS1307_REG_CONTROL] |
Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 1068 | & ~DS1338_BIT_OSF); |
| 1069 | dev_warn(&client->dev, "SET TIME!\n"); |
| 1070 | goto read_rtc; |
| 1071 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1072 | break; |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 1073 | case ds_1340: |
| 1074 | /* clock halted? turn it on, so clock can tick. */ |
| 1075 | if (tmp & DS1340_BIT_nEOSC) |
| 1076 | i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0); |
| 1077 | |
| 1078 | tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG); |
| 1079 | if (tmp < 0) { |
Jingoo Han | 6df80e2 | 2013-04-29 16:19:28 -0700 | [diff] [blame] | 1080 | dev_dbg(&client->dev, "read error %d\n", tmp); |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 1081 | err = -EIO; |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1082 | goto exit; |
frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /* oscillator fault? clear flag, and warn */ |
| 1086 | if (tmp & DS1340_BIT_OSF) { |
| 1087 | i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0); |
| 1088 | dev_warn(&client->dev, "SET TIME!\n"); |
| 1089 | } |
| 1090 | break; |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 1091 | case mcp794xx: |
David Anders | 43fcb81 | 2011-11-02 13:37:53 -0700 | [diff] [blame] | 1092 | /* make sure that the backup battery is enabled */ |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 1093 | if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) { |
David Anders | 43fcb81 | 2011-11-02 13:37:53 -0700 | [diff] [blame] | 1094 | i2c_smbus_write_byte_data(client, DS1307_REG_WDAY, |
| 1095 | ds1307->regs[DS1307_REG_WDAY] |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 1096 | | MCP794XX_BIT_VBATEN); |
David Anders | 43fcb81 | 2011-11-02 13:37:53 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* clock halted? turn it on, so clock can tick. */ |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 1100 | if (!(tmp & MCP794XX_BIT_ST)) { |
David Anders | 43fcb81 | 2011-11-02 13:37:53 -0700 | [diff] [blame] | 1101 | i2c_smbus_write_byte_data(client, DS1307_REG_SECS, |
Tomas Novotny | f4199f8 | 2014-12-10 15:53:57 -0800 | [diff] [blame] | 1102 | MCP794XX_BIT_ST); |
David Anders | 43fcb81 | 2011-11-02 13:37:53 -0700 | [diff] [blame] | 1103 | dev_warn(&client->dev, "SET TIME!\n"); |
| 1104 | goto read_rtc; |
| 1105 | } |
| 1106 | |
| 1107 | break; |
Wolfram Sang | 32d322b | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 1108 | default: |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1109 | break; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1110 | } |
David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 1111 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1112 | tmp = ds1307->regs[DS1307_REG_HOUR]; |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1113 | switch (ds1307->type) { |
| 1114 | case ds_1340: |
| 1115 | case m41t00: |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 1116 | /* |
| 1117 | * NOTE: ignores century bits; fix before deploying |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1118 | * systems that will run through year 2100. |
| 1119 | */ |
| 1120 | break; |
Matthias Fuchs | a216685 | 2009-03-31 15:24:58 -0700 | [diff] [blame] | 1121 | case rx_8025: |
| 1122 | break; |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1123 | default: |
| 1124 | if (!(tmp & DS1307_BIT_12HR)) |
| 1125 | break; |
| 1126 | |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 1127 | /* |
| 1128 | * Be sure we're in 24 hour mode. Multi-master systems |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1129 | * take note... |
| 1130 | */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 1131 | tmp = bcd2bin(tmp & 0x1f); |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1132 | if (tmp == 12) |
| 1133 | tmp = 0; |
| 1134 | if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM) |
| 1135 | tmp += 12; |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1136 | i2c_smbus_write_byte_data(client, |
Joakim Tjernlund | 96fc3a4 | 2010-06-29 15:05:34 -0700 | [diff] [blame] | 1137 | ds1307->offset + DS1307_REG_HOUR, |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 1138 | bin2bcd(tmp)); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Simon Guinot | 5ea7351 | 2014-04-03 14:49:54 -0700 | [diff] [blame] | 1141 | device_set_wakeup_capable(&client->dev, want_irq); |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1142 | ds1307->rtc = devm_rtc_device_register(&client->dev, client->name, |
Simon Guinot | 1d1945d | 2014-04-03 14:49:55 -0700 | [diff] [blame] | 1143 | rtc_ops, THIS_MODULE); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1144 | if (IS_ERR(ds1307->rtc)) { |
Alessandro Zummo | 4071ea2 | 2014-04-03 14:49:36 -0700 | [diff] [blame] | 1145 | return PTR_ERR(ds1307->rtc); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1148 | if (want_irq) { |
Nishanth Menon | 7abea61 | 2015-06-24 11:26:54 -0500 | [diff] [blame] | 1149 | struct device_node *node = client->dev.of_node; |
| 1150 | |
Nishanth Menon | c598319 | 2015-06-23 11:15:11 -0500 | [diff] [blame] | 1151 | err = devm_request_threaded_irq(&client->dev, |
| 1152 | client->irq, NULL, irq_handler, |
| 1153 | IRQF_SHARED | IRQF_ONESHOT, |
| 1154 | ds1307->rtc->name, client); |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1155 | if (err) { |
Alessandro Zummo | 4071ea2 | 2014-04-03 14:49:36 -0700 | [diff] [blame] | 1156 | client->irq = 0; |
| 1157 | dev_err(&client->dev, "unable to request IRQ!\n"); |
Nishanth Menon | 7abea61 | 2015-06-24 11:26:54 -0500 | [diff] [blame] | 1158 | goto no_irq; |
| 1159 | } |
Anton Vorontsov | 26b3c01 | 2009-12-17 15:27:23 -0800 | [diff] [blame] | 1160 | |
Nishanth Menon | 7abea61 | 2015-06-24 11:26:54 -0500 | [diff] [blame] | 1161 | set_bit(HAS_ALARM, &ds1307->flags); |
| 1162 | dev_dbg(&client->dev, "got IRQ %d\n", client->irq); |
| 1163 | |
| 1164 | /* Currently supported by OF code only! */ |
| 1165 | if (!node) |
| 1166 | goto no_irq; |
| 1167 | |
| 1168 | err = of_irq_get(node, 1); |
| 1169 | if (err <= 0) { |
| 1170 | if (err == -EPROBE_DEFER) |
| 1171 | goto exit; |
| 1172 | goto no_irq; |
| 1173 | } |
| 1174 | ds1307->wakeirq = err; |
| 1175 | |
| 1176 | err = dev_pm_set_dedicated_wake_irq(&client->dev, |
| 1177 | ds1307->wakeirq); |
| 1178 | if (err) { |
| 1179 | dev_err(&client->dev, "unable to setup wakeIRQ %d!\n", |
| 1180 | err); |
| 1181 | goto exit; |
Alessandro Zummo | 4071ea2 | 2014-04-03 14:49:36 -0700 | [diff] [blame] | 1182 | } |
Rodolfo Giometti | cb49a5e | 2008-10-15 22:02:58 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Nishanth Menon | 7abea61 | 2015-06-24 11:26:54 -0500 | [diff] [blame] | 1185 | no_irq: |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 1186 | if (chip->nvram_size) { |
Alessandro Zummo | 4071ea2 | 2014-04-03 14:49:36 -0700 | [diff] [blame] | 1187 | |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1188 | ds1307->nvram = devm_kzalloc(&client->dev, |
| 1189 | sizeof(struct bin_attribute), |
| 1190 | GFP_KERNEL); |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 1191 | if (!ds1307->nvram) { |
Alessandro Zummo | 4071ea2 | 2014-04-03 14:49:36 -0700 | [diff] [blame] | 1192 | dev_err(&client->dev, "cannot allocate memory for nvram sysfs\n"); |
| 1193 | } else { |
| 1194 | |
| 1195 | ds1307->nvram->attr.name = "nvram"; |
| 1196 | ds1307->nvram->attr.mode = S_IRUGO | S_IWUSR; |
| 1197 | |
| 1198 | sysfs_bin_attr_init(ds1307->nvram); |
| 1199 | |
| 1200 | ds1307->nvram->read = ds1307_nvram_read; |
| 1201 | ds1307->nvram->write = ds1307_nvram_write; |
| 1202 | ds1307->nvram->size = chip->nvram_size; |
| 1203 | ds1307->nvram_offset = chip->nvram_offset; |
| 1204 | |
| 1205 | err = sysfs_create_bin_file(&client->dev.kobj, |
| 1206 | ds1307->nvram); |
| 1207 | if (err) { |
| 1208 | dev_err(&client->dev, |
| 1209 | "unable to create sysfs file: %s\n", |
| 1210 | ds1307->nvram->attr.name); |
| 1211 | } else { |
| 1212 | set_bit(HAS_NVRAM, &ds1307->flags); |
| 1213 | dev_info(&client->dev, "%zu bytes nvram\n", |
| 1214 | ds1307->nvram->size); |
| 1215 | } |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1219 | return 0; |
| 1220 | |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1221 | exit: |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1222 | return err; |
| 1223 | } |
| 1224 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 1225 | static int ds1307_remove(struct i2c_client *client) |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1226 | { |
David Anders | 40ce972 | 2012-03-23 15:02:37 -0700 | [diff] [blame] | 1227 | struct ds1307 *ds1307 = i2c_get_clientdata(client); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1228 | |
Nishanth Menon | 7abea61 | 2015-06-24 11:26:54 -0500 | [diff] [blame] | 1229 | if (ds1307->wakeirq) |
| 1230 | dev_pm_clear_wake_irq(&client->dev); |
| 1231 | |
Jingoo Han | edca66d | 2013-07-03 15:07:05 -0700 | [diff] [blame] | 1232 | if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags)) |
Austin Boyle | 9eab0a7 | 2012-03-23 15:02:38 -0700 | [diff] [blame] | 1233 | sysfs_remove_bin_file(&client->dev.kobj, ds1307->nvram); |
David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 1234 | |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | static struct i2c_driver ds1307_driver = { |
| 1239 | .driver = { |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1240 | .name = "rtc-ds1307", |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1241 | }, |
David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 1242 | .probe = ds1307_probe, |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 1243 | .remove = ds1307_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 1244 | .id_table = ds1307_id, |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1245 | }; |
| 1246 | |
Axel Lin | 0abc920 | 2012-03-23 15:02:31 -0700 | [diff] [blame] | 1247 | module_i2c_driver(ds1307_driver); |
David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1248 | |
| 1249 | MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips"); |
| 1250 | MODULE_LICENSE("GPL"); |