Nobuhiro Iwamatsu | cd13635 | 2021-07-07 16:57:57 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 2 | /* |
| 3 | * rtc class driver for the Maxim MAX6900 chip |
| 4 | * |
Nobuhiro Iwamatsu | cd13635 | 2021-07-07 16:57:57 +0900 | [diff] [blame] | 5 | * Copyright (c) 2007 MontaVista, Software, Inc. |
| 6 | * |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 7 | * Author: Dale Farnsworth <dale@farnsworth.org> |
| 8 | * |
| 9 | * based on previously existing rtc class drivers |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/bcd.h> |
| 15 | #include <linux/rtc.h> |
| 16 | #include <linux/delay.h> |
| 17 | |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 18 | /* |
| 19 | * register indices |
| 20 | */ |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 21 | #define MAX6900_REG_SC 0 /* seconds 00-59 */ |
| 22 | #define MAX6900_REG_MN 1 /* minutes 00-59 */ |
| 23 | #define MAX6900_REG_HR 2 /* hours 00-23 */ |
| 24 | #define MAX6900_REG_DT 3 /* day of month 00-31 */ |
| 25 | #define MAX6900_REG_MO 4 /* month 01-12 */ |
| 26 | #define MAX6900_REG_DW 5 /* day of week 1-7 */ |
| 27 | #define MAX6900_REG_YR 6 /* year 00-99 */ |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 28 | #define MAX6900_REG_CT 7 /* control */ |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 29 | /* register 8 is undocumented */ |
| 30 | #define MAX6900_REG_CENTURY 9 /* century */ |
| 31 | #define MAX6900_REG_LEN 10 |
| 32 | |
| 33 | #define MAX6900_BURST_LEN 8 /* can burst r/w first 8 regs */ |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 34 | |
| 35 | #define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */ |
| 36 | |
| 37 | /* |
| 38 | * register read/write commands |
| 39 | */ |
| 40 | #define MAX6900_REG_CONTROL_WRITE 0x8e |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 41 | #define MAX6900_REG_CENTURY_WRITE 0x92 |
| 42 | #define MAX6900_REG_CENTURY_READ 0x93 |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 43 | #define MAX6900_REG_RESERVED_READ 0x96 |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 44 | #define MAX6900_REG_BURST_WRITE 0xbe |
| 45 | #define MAX6900_REG_BURST_READ 0xbf |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 46 | |
| 47 | #define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */ |
| 48 | |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 49 | static struct i2c_driver max6900_driver; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 50 | |
| 51 | static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf) |
| 52 | { |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 53 | u8 reg_burst_read[1] = { MAX6900_REG_BURST_READ }; |
| 54 | u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ }; |
| 55 | struct i2c_msg msgs[4] = { |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 56 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 57 | .addr = client->addr, |
| 58 | .flags = 0, /* write */ |
| 59 | .len = sizeof(reg_burst_read), |
| 60 | .buf = reg_burst_read} |
| 61 | , |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 62 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 63 | .addr = client->addr, |
| 64 | .flags = I2C_M_RD, |
| 65 | .len = MAX6900_BURST_LEN, |
| 66 | .buf = buf} |
| 67 | , |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 68 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 69 | .addr = client->addr, |
| 70 | .flags = 0, /* write */ |
| 71 | .len = sizeof(reg_century_read), |
| 72 | .buf = reg_century_read} |
| 73 | , |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 74 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 75 | .addr = client->addr, |
| 76 | .flags = I2C_M_RD, |
| 77 | .len = sizeof(buf[MAX6900_REG_CENTURY]), |
| 78 | .buf = &buf[MAX6900_REG_CENTURY] |
| 79 | } |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 80 | }; |
| 81 | int rc; |
| 82 | |
| 83 | rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
| 84 | if (rc != ARRAY_SIZE(msgs)) { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 85 | dev_err(&client->dev, "%s: register read failed\n", __func__); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 86 | return -EIO; |
| 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf) |
| 92 | { |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 93 | u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE }; |
| 94 | struct i2c_msg century_msgs[1] = { |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 95 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 96 | .addr = client->addr, |
| 97 | .flags = 0, /* write */ |
| 98 | .len = sizeof(i2c_century_buf), |
| 99 | .buf = i2c_century_buf} |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 100 | }; |
| 101 | u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE }; |
| 102 | struct i2c_msg burst_msgs[1] = { |
| 103 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 104 | .addr = client->addr, |
| 105 | .flags = 0, /* write */ |
| 106 | .len = sizeof(i2c_burst_buf), |
| 107 | .buf = i2c_burst_buf} |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 108 | }; |
| 109 | int rc; |
| 110 | |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 111 | /* |
| 112 | * We have to make separate calls to i2c_transfer because of |
| 113 | * the need to delay after each write to the chip. Also, |
| 114 | * we write the century byte first, since we set the write-protect |
| 115 | * bit as part of the burst write. |
| 116 | */ |
| 117 | i2c_century_buf[1] = buf[MAX6900_REG_CENTURY]; |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 118 | |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 119 | rc = i2c_transfer(client->adapter, century_msgs, |
| 120 | ARRAY_SIZE(century_msgs)); |
| 121 | if (rc != ARRAY_SIZE(century_msgs)) |
| 122 | goto write_failed; |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 123 | |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 124 | msleep(MAX6900_IDLE_TIME_AFTER_WRITE); |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 125 | |
| 126 | memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN); |
| 127 | |
| 128 | rc = i2c_transfer(client->adapter, burst_msgs, ARRAY_SIZE(burst_msgs)); |
| 129 | if (rc != ARRAY_SIZE(burst_msgs)) |
| 130 | goto write_failed; |
| 131 | msleep(MAX6900_IDLE_TIME_AFTER_WRITE); |
| 132 | |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 133 | return 0; |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 134 | |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 135 | write_failed: |
| 136 | dev_err(&client->dev, "%s: register write failed\n", __func__); |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 137 | return -EIO; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Alexandre Belloni | 859a6dd | 2018-02-21 16:04:27 +0100 | [diff] [blame] | 140 | static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm) |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 141 | { |
Alexandre Belloni | 859a6dd | 2018-02-21 16:04:27 +0100 | [diff] [blame] | 142 | struct i2c_client *client = to_i2c_client(dev); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 143 | int rc; |
| 144 | u8 regs[MAX6900_REG_LEN]; |
| 145 | |
| 146 | rc = max6900_i2c_read_regs(client, regs); |
| 147 | if (rc < 0) |
| 148 | return rc; |
| 149 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 150 | tm->tm_sec = bcd2bin(regs[MAX6900_REG_SC]); |
| 151 | tm->tm_min = bcd2bin(regs[MAX6900_REG_MN]); |
| 152 | tm->tm_hour = bcd2bin(regs[MAX6900_REG_HR] & 0x3f); |
| 153 | tm->tm_mday = bcd2bin(regs[MAX6900_REG_DT]); |
| 154 | tm->tm_mon = bcd2bin(regs[MAX6900_REG_MO]) - 1; |
| 155 | tm->tm_year = bcd2bin(regs[MAX6900_REG_YR]) + |
| 156 | bcd2bin(regs[MAX6900_REG_CENTURY]) * 100 - 1900; |
| 157 | tm->tm_wday = bcd2bin(regs[MAX6900_REG_DW]); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 158 | |
Alexandre Belloni | ceed43f | 2018-02-21 16:01:52 +0100 | [diff] [blame] | 159 | return 0; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static int max6900_i2c_clear_write_protect(struct i2c_client *client) |
| 163 | { |
Sachin Kamat | f16189a | 2013-11-12 15:10:27 -0800 | [diff] [blame] | 164 | return i2c_smbus_write_byte_data(client, MAX6900_REG_CONTROL_WRITE, 0); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Alexandre Belloni | 859a6dd | 2018-02-21 16:04:27 +0100 | [diff] [blame] | 167 | static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm) |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 168 | { |
Alexandre Belloni | 859a6dd | 2018-02-21 16:04:27 +0100 | [diff] [blame] | 169 | struct i2c_client *client = to_i2c_client(dev); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 170 | u8 regs[MAX6900_REG_LEN]; |
| 171 | int rc; |
| 172 | |
| 173 | rc = max6900_i2c_clear_write_protect(client); |
| 174 | if (rc < 0) |
| 175 | return rc; |
| 176 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 177 | regs[MAX6900_REG_SC] = bin2bcd(tm->tm_sec); |
| 178 | regs[MAX6900_REG_MN] = bin2bcd(tm->tm_min); |
| 179 | regs[MAX6900_REG_HR] = bin2bcd(tm->tm_hour); |
| 180 | regs[MAX6900_REG_DT] = bin2bcd(tm->tm_mday); |
| 181 | regs[MAX6900_REG_MO] = bin2bcd(tm->tm_mon + 1); |
| 182 | regs[MAX6900_REG_DW] = bin2bcd(tm->tm_wday); |
| 183 | regs[MAX6900_REG_YR] = bin2bcd(tm->tm_year % 100); |
| 184 | regs[MAX6900_REG_CENTURY] = bin2bcd((tm->tm_year + 1900) / 100); |
Dale Farnsworth | 8a2601f | 2007-07-21 04:37:57 -0700 | [diff] [blame] | 185 | /* set write protect */ |
| 186 | regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 187 | |
| 188 | rc = max6900_i2c_write_regs(client, regs); |
| 189 | if (rc < 0) |
| 190 | return rc; |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 195 | static const struct rtc_class_ops max6900_rtc_ops = { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 196 | .read_time = max6900_rtc_read_time, |
| 197 | .set_time = max6900_rtc_set_time, |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 200 | static int |
| 201 | max6900_probe(struct i2c_client *client, const struct i2c_device_id *id) |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 202 | { |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 203 | struct rtc_device *rtc; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 204 | |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 205 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 206 | return -ENODEV; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 207 | |
Jingoo Han | feb141d | 2013-04-29 16:19:43 -0700 | [diff] [blame] | 208 | rtc = devm_rtc_device_register(&client->dev, max6900_driver.driver.name, |
| 209 | &max6900_rtc_ops, THIS_MODULE); |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 210 | if (IS_ERR(rtc)) |
| 211 | return PTR_ERR(rtc); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 212 | |
| 213 | i2c_set_clientdata(client, rtc); |
| 214 | |
| 215 | return 0; |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Arvind Yadav | 45a6351 | 2017-08-20 00:37:55 +0530 | [diff] [blame] | 218 | static const struct i2c_device_id max6900_id[] = { |
Alessandro Zummo | fe102c7 | 2008-12-09 13:14:11 -0800 | [diff] [blame] | 219 | { "max6900", 0 }, |
| 220 | { } |
| 221 | }; |
Axel Lin | 8ad0f5b | 2011-04-18 20:16:57 +0800 | [diff] [blame] | 222 | MODULE_DEVICE_TABLE(i2c, max6900_id); |
Alessandro Zummo | fe102c7 | 2008-12-09 13:14:11 -0800 | [diff] [blame] | 223 | |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 224 | static struct i2c_driver max6900_driver = { |
| 225 | .driver = { |
| 226 | .name = "rtc-max6900", |
| 227 | }, |
| 228 | .probe = max6900_probe, |
Alessandro Zummo | fe102c7 | 2008-12-09 13:14:11 -0800 | [diff] [blame] | 229 | .id_table = max6900_id, |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
Axel Lin | 0abc920 | 2012-03-23 15:02:31 -0700 | [diff] [blame] | 232 | module_i2c_driver(max6900_driver); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 233 | |
| 234 | MODULE_DESCRIPTION("Maxim MAX6900 RTC driver"); |
Alessandro Zummo | 6fd5c03 | 2008-10-15 22:03:08 -0700 | [diff] [blame] | 235 | MODULE_AUTHOR("Dale Farnsworth <dale@farnsworth.org>"); |
Dale Farnsworth | aa5bd7e | 2007-05-08 00:26:39 -0700 | [diff] [blame] | 236 | MODULE_LICENSE("GPL"); |