Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * An I2C driver for the PCF85063 RTC |
| 3 | * Copyright 2014 Rose Technology |
| 4 | * |
| 5 | * Author: Søren Andersen <san@rosetechnology.dk> |
| 6 | * Maintainers: http://www.nslu2-linux.org/ |
| 7 | * |
| 8 | * based on the other drivers in this same directory. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | #include <linux/i2c.h> |
| 15 | #include <linux/bcd.h> |
| 16 | #include <linux/rtc.h> |
| 17 | #include <linux/module.h> |
| 18 | |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 19 | /* |
| 20 | * Information for this driver was pulled from the following datasheets. |
| 21 | * |
| 22 | * http://www.nxp.com/documents/data_sheet/PCF85063A.pdf |
| 23 | * http://www.nxp.com/documents/data_sheet/PCF85063TP.pdf |
| 24 | * |
| 25 | * PCF85063A -- Rev. 6 — 18 November 2015 |
| 26 | * PCF85063TP -- Rev. 4 — 6 May 2015 |
| 27 | */ |
| 28 | |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 29 | #define PCF85063_REG_CTRL1 0x00 /* status */ |
Sam Ravnborg | bbb4383 | 2019-01-19 10:00:31 +0100 | [diff] [blame] | 30 | #define PCF85063_REG_CTRL1_CAP_SEL BIT(0) |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 31 | #define PCF85063_REG_CTRL1_STOP BIT(5) |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 32 | |
| 33 | #define PCF85063_REG_SC 0x04 /* datetime */ |
Juergen Borleis | 6cc4c8b | 2016-02-09 11:57:26 +0100 | [diff] [blame] | 34 | #define PCF85063_REG_SC_OS 0x80 |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 35 | |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 36 | static struct i2c_driver pcf85063_driver; |
| 37 | |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 38 | static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1) |
| 39 | { |
Alvin Šipraga | ec9cf1b | 2018-07-15 23:31:53 +0200 | [diff] [blame] | 40 | int rc; |
| 41 | u8 reg; |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 42 | |
Alvin Šipraga | ec9cf1b | 2018-07-15 23:31:53 +0200 | [diff] [blame] | 43 | rc = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1); |
| 44 | if (rc < 0) { |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 45 | dev_err(&client->dev, "Failing to stop the clock\n"); |
| 46 | return -EIO; |
| 47 | } |
| 48 | |
| 49 | /* stop the clock */ |
Alvin Šipraga | ec9cf1b | 2018-07-15 23:31:53 +0200 | [diff] [blame] | 50 | reg = rc | PCF85063_REG_CTRL1_STOP; |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 51 | |
Alvin Šipraga | ec9cf1b | 2018-07-15 23:31:53 +0200 | [diff] [blame] | 52 | rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, reg); |
| 53 | if (rc < 0) { |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 54 | dev_err(&client->dev, "Failing to stop the clock\n"); |
| 55 | return -EIO; |
| 56 | } |
| 57 | |
Alvin Šipraga | ec9cf1b | 2018-07-15 23:31:53 +0200 | [diff] [blame] | 58 | *ctrl1 = reg; |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 63 | static int pcf85063_start_clock(struct i2c_client *client, u8 ctrl1) |
| 64 | { |
Alvin Šipraga | ec9cf1b | 2018-07-15 23:31:53 +0200 | [diff] [blame] | 65 | int rc; |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 66 | |
| 67 | /* start the clock */ |
Michael McCormick | 051abf5 | 2018-02-22 11:29:24 +1300 | [diff] [blame] | 68 | ctrl1 &= ~PCF85063_REG_CTRL1_STOP; |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 69 | |
Alvin Šipraga | ec9cf1b | 2018-07-15 23:31:53 +0200 | [diff] [blame] | 70 | rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ctrl1); |
| 71 | if (rc < 0) { |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 72 | dev_err(&client->dev, "Failing to start the clock\n"); |
| 73 | return -EIO; |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
Alexandre Belloni | 965271d | 2018-02-21 16:09:27 +0100 | [diff] [blame] | 79 | static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm) |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 80 | { |
Alexandre Belloni | 965271d | 2018-02-21 16:09:27 +0100 | [diff] [blame] | 81 | struct i2c_client *client = to_i2c_client(dev); |
Juergen Borleis | 7b57684 | 2016-02-09 11:57:25 +0100 | [diff] [blame] | 82 | int rc; |
Juergen Borleis | 7b57684 | 2016-02-09 11:57:25 +0100 | [diff] [blame] | 83 | u8 regs[7]; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 84 | |
Juergen Borleis | 7b57684 | 2016-02-09 11:57:25 +0100 | [diff] [blame] | 85 | /* |
| 86 | * while reading, the time/date registers are blocked and not updated |
| 87 | * anymore until the access is finished. To not lose a second |
| 88 | * event, the access must be finished within one second. So, read all |
| 89 | * time/date registers in one turn. |
| 90 | */ |
| 91 | rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC, |
| 92 | sizeof(regs), regs); |
| 93 | if (rc != sizeof(regs)) { |
| 94 | dev_err(&client->dev, "date/time register read error\n"); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 95 | return -EIO; |
| 96 | } |
| 97 | |
Juergen Borleis | 6cc4c8b | 2016-02-09 11:57:26 +0100 | [diff] [blame] | 98 | /* if the clock has lost its power it makes no sense to use its time */ |
| 99 | if (regs[0] & PCF85063_REG_SC_OS) { |
| 100 | dev_warn(&client->dev, "Power loss detected, invalid time\n"); |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
Juergen Borleis | 7b57684 | 2016-02-09 11:57:25 +0100 | [diff] [blame] | 104 | tm->tm_sec = bcd2bin(regs[0] & 0x7F); |
| 105 | tm->tm_min = bcd2bin(regs[1] & 0x7F); |
| 106 | tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */ |
| 107 | tm->tm_mday = bcd2bin(regs[3] & 0x3F); |
| 108 | tm->tm_wday = regs[4] & 0x07; |
| 109 | tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */ |
| 110 | tm->tm_year = bcd2bin(regs[6]); |
Alexandre Belloni | c421ce7 | 2016-07-18 11:08:59 +0200 | [diff] [blame] | 111 | tm->tm_year += 100; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 112 | |
Alexandre Belloni | 0a6b888 | 2018-02-21 16:07:34 +0100 | [diff] [blame] | 113 | return 0; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Alexandre Belloni | 965271d | 2018-02-21 16:09:27 +0100 | [diff] [blame] | 116 | static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm) |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 117 | { |
Alexandre Belloni | 965271d | 2018-02-21 16:09:27 +0100 | [diff] [blame] | 118 | struct i2c_client *client = to_i2c_client(dev); |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 119 | int rc; |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 120 | u8 regs[7]; |
| 121 | u8 ctrl1; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 122 | |
Alexandre Belloni | c421ce7 | 2016-07-18 11:08:59 +0200 | [diff] [blame] | 123 | if ((tm->tm_year < 100) || (tm->tm_year > 199)) |
| 124 | return -EINVAL; |
| 125 | |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 126 | /* |
| 127 | * to accurately set the time, reset the divider chain and keep it in |
| 128 | * reset state until all time/date registers are written |
| 129 | */ |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 130 | rc = pcf85063_stop_clock(client, &ctrl1); |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 131 | if (rc != 0) |
| 132 | return rc; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 133 | |
| 134 | /* hours, minutes and seconds */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 135 | regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */ |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 136 | |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 137 | regs[1] = bin2bcd(tm->tm_min); |
| 138 | regs[2] = bin2bcd(tm->tm_hour); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 139 | |
| 140 | /* Day of month, 1 - 31 */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 141 | regs[3] = bin2bcd(tm->tm_mday); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 142 | |
| 143 | /* Day, 0 - 6 */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 144 | regs[4] = tm->tm_wday & 0x07; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 145 | |
| 146 | /* month, 1 - 12 */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 147 | regs[5] = bin2bcd(tm->tm_mon + 1); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 148 | |
| 149 | /* year and century */ |
Alexandre Belloni | c421ce7 | 2016-07-18 11:08:59 +0200 | [diff] [blame] | 150 | regs[6] = bin2bcd(tm->tm_year - 100); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 151 | |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 152 | /* write all registers at once */ |
| 153 | rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC, |
| 154 | sizeof(regs), regs); |
| 155 | if (rc < 0) { |
| 156 | dev_err(&client->dev, "date/time register write error\n"); |
| 157 | return rc; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 160 | /* |
| 161 | * Write the control register as a separate action since the size of |
| 162 | * the register space is different between the PCF85063TP and |
| 163 | * PCF85063A devices. The rollover point can not be used. |
| 164 | */ |
| 165 | rc = pcf85063_start_clock(client, ctrl1); |
| 166 | if (rc != 0) |
| 167 | return rc; |
| 168 | |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 172 | static const struct rtc_class_ops pcf85063_rtc_ops = { |
| 173 | .read_time = pcf85063_rtc_read_time, |
| 174 | .set_time = pcf85063_rtc_set_time |
| 175 | }; |
| 176 | |
Sam Ravnborg | bbb4383 | 2019-01-19 10:00:31 +0100 | [diff] [blame] | 177 | static int pcf85063_load_capacitance(struct i2c_client *client) |
| 178 | { |
| 179 | u32 load; |
| 180 | int rc; |
| 181 | u8 reg; |
| 182 | |
| 183 | rc = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1); |
| 184 | if (rc < 0) |
| 185 | return rc; |
| 186 | |
| 187 | reg = rc; |
| 188 | load = 7000; |
| 189 | of_property_read_u32(client->dev.of_node, "quartz-load-femtofarads", |
| 190 | &load); |
| 191 | |
| 192 | switch (load) { |
| 193 | default: |
| 194 | dev_warn(&client->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 7000", |
| 195 | load); |
| 196 | /* fall through */ |
| 197 | case 7000: |
| 198 | reg &= ~PCF85063_REG_CTRL1_CAP_SEL; |
| 199 | break; |
| 200 | case 12500: |
| 201 | reg |= PCF85063_REG_CTRL1_CAP_SEL; |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, reg); |
| 206 | |
| 207 | return rc; |
| 208 | } |
| 209 | |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 210 | static int pcf85063_probe(struct i2c_client *client, |
| 211 | const struct i2c_device_id *id) |
| 212 | { |
Alexandre Belloni | 2da424a | 2016-03-02 11:25:43 +0100 | [diff] [blame] | 213 | struct rtc_device *rtc; |
Mirza Krak | c18b4c5 | 2016-10-17 15:53:31 +0200 | [diff] [blame] | 214 | int err; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 215 | |
| 216 | dev_dbg(&client->dev, "%s\n", __func__); |
| 217 | |
| 218 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 219 | return -ENODEV; |
| 220 | |
Mirza Krak | c18b4c5 | 2016-10-17 15:53:31 +0200 | [diff] [blame] | 221 | err = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1); |
| 222 | if (err < 0) { |
| 223 | dev_err(&client->dev, "RTC chip is not present\n"); |
| 224 | return err; |
| 225 | } |
| 226 | |
Sam Ravnborg | bbb4383 | 2019-01-19 10:00:31 +0100 | [diff] [blame] | 227 | err = pcf85063_load_capacitance(client); |
| 228 | if (err < 0) |
| 229 | dev_warn(&client->dev, "failed to set xtal load capacitance: %d", |
| 230 | err); |
| 231 | |
Alexandre Belloni | 2da424a | 2016-03-02 11:25:43 +0100 | [diff] [blame] | 232 | rtc = devm_rtc_device_register(&client->dev, |
| 233 | pcf85063_driver.driver.name, |
| 234 | &pcf85063_rtc_ops, THIS_MODULE); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 235 | |
Alexandre Belloni | 2da424a | 2016-03-02 11:25:43 +0100 | [diff] [blame] | 236 | return PTR_ERR_OR_ZERO(rtc); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static const struct i2c_device_id pcf85063_id[] = { |
| 240 | { "pcf85063", 0 }, |
| 241 | { } |
| 242 | }; |
| 243 | MODULE_DEVICE_TABLE(i2c, pcf85063_id); |
| 244 | |
| 245 | #ifdef CONFIG_OF |
| 246 | static const struct of_device_id pcf85063_of_match[] = { |
| 247 | { .compatible = "nxp,pcf85063" }, |
| 248 | {} |
| 249 | }; |
| 250 | MODULE_DEVICE_TABLE(of, pcf85063_of_match); |
| 251 | #endif |
| 252 | |
| 253 | static struct i2c_driver pcf85063_driver = { |
| 254 | .driver = { |
| 255 | .name = "rtc-pcf85063", |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 256 | .of_match_table = of_match_ptr(pcf85063_of_match), |
| 257 | }, |
| 258 | .probe = pcf85063_probe, |
| 259 | .id_table = pcf85063_id, |
| 260 | }; |
| 261 | |
| 262 | module_i2c_driver(pcf85063_driver); |
| 263 | |
| 264 | MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>"); |
| 265 | MODULE_DESCRIPTION("PCF85063 RTC driver"); |
| 266 | MODULE_LICENSE("GPL"); |