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 */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 30 | #define PCF85063_REG_CTRL1_STOP BIT(5) |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 31 | #define PCF85063_REG_CTRL2 0x01 |
| 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 | #define PCF85063_REG_MN 0x05 |
| 36 | #define PCF85063_REG_HR 0x06 |
| 37 | #define PCF85063_REG_DM 0x07 |
| 38 | #define PCF85063_REG_DW 0x08 |
| 39 | #define PCF85063_REG_MO 0x09 |
| 40 | #define PCF85063_REG_YR 0x0A |
| 41 | |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 42 | static struct i2c_driver pcf85063_driver; |
| 43 | |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 44 | static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1) |
| 45 | { |
| 46 | s32 ret; |
| 47 | |
| 48 | ret = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1); |
| 49 | if (ret < 0) { |
| 50 | dev_err(&client->dev, "Failing to stop the clock\n"); |
| 51 | return -EIO; |
| 52 | } |
| 53 | |
| 54 | /* stop the clock */ |
| 55 | ret |= PCF85063_REG_CTRL1_STOP; |
| 56 | |
| 57 | ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ret); |
| 58 | if (ret < 0) { |
| 59 | dev_err(&client->dev, "Failing to stop the clock\n"); |
| 60 | return -EIO; |
| 61 | } |
| 62 | |
| 63 | *ctrl1 = ret; |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 68 | static int pcf85063_start_clock(struct i2c_client *client, u8 ctrl1) |
| 69 | { |
| 70 | s32 ret; |
| 71 | |
| 72 | /* start the clock */ |
Michael McCormick | 051abf5 | 2018-02-22 11:29:24 +1300 | [diff] [blame] | 73 | ctrl1 &= ~PCF85063_REG_CTRL1_STOP; |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 74 | |
| 75 | ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ctrl1); |
| 76 | if (ret < 0) { |
| 77 | dev_err(&client->dev, "Failing to start the clock\n"); |
| 78 | return -EIO; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Alexandre Belloni | 965271d | 2018-02-21 16:09:27 +0100 | [diff] [blame] | 84 | 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] | 85 | { |
Alexandre Belloni | 965271d | 2018-02-21 16:09:27 +0100 | [diff] [blame] | 86 | struct i2c_client *client = to_i2c_client(dev); |
Juergen Borleis | 7b57684 | 2016-02-09 11:57:25 +0100 | [diff] [blame] | 87 | int rc; |
Juergen Borleis | 7b57684 | 2016-02-09 11:57:25 +0100 | [diff] [blame] | 88 | u8 regs[7]; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 89 | |
Juergen Borleis | 7b57684 | 2016-02-09 11:57:25 +0100 | [diff] [blame] | 90 | /* |
| 91 | * while reading, the time/date registers are blocked and not updated |
| 92 | * anymore until the access is finished. To not lose a second |
| 93 | * event, the access must be finished within one second. So, read all |
| 94 | * time/date registers in one turn. |
| 95 | */ |
| 96 | rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC, |
| 97 | sizeof(regs), regs); |
| 98 | if (rc != sizeof(regs)) { |
| 99 | dev_err(&client->dev, "date/time register read error\n"); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 100 | return -EIO; |
| 101 | } |
| 102 | |
Juergen Borleis | 6cc4c8b | 2016-02-09 11:57:26 +0100 | [diff] [blame] | 103 | /* if the clock has lost its power it makes no sense to use its time */ |
| 104 | if (regs[0] & PCF85063_REG_SC_OS) { |
| 105 | dev_warn(&client->dev, "Power loss detected, invalid time\n"); |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | |
Juergen Borleis | 7b57684 | 2016-02-09 11:57:25 +0100 | [diff] [blame] | 109 | tm->tm_sec = bcd2bin(regs[0] & 0x7F); |
| 110 | tm->tm_min = bcd2bin(regs[1] & 0x7F); |
| 111 | tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */ |
| 112 | tm->tm_mday = bcd2bin(regs[3] & 0x3F); |
| 113 | tm->tm_wday = regs[4] & 0x07; |
| 114 | tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */ |
| 115 | tm->tm_year = bcd2bin(regs[6]); |
Alexandre Belloni | c421ce7 | 2016-07-18 11:08:59 +0200 | [diff] [blame] | 116 | tm->tm_year += 100; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 117 | |
Alexandre Belloni | 0a6b888 | 2018-02-21 16:07:34 +0100 | [diff] [blame] | 118 | return 0; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Alexandre Belloni | 965271d | 2018-02-21 16:09:27 +0100 | [diff] [blame] | 121 | 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] | 122 | { |
Alexandre Belloni | 965271d | 2018-02-21 16:09:27 +0100 | [diff] [blame] | 123 | struct i2c_client *client = to_i2c_client(dev); |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 124 | int rc; |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 125 | u8 regs[7]; |
| 126 | u8 ctrl1; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 127 | |
Alexandre Belloni | c421ce7 | 2016-07-18 11:08:59 +0200 | [diff] [blame] | 128 | if ((tm->tm_year < 100) || (tm->tm_year > 199)) |
| 129 | return -EINVAL; |
| 130 | |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 131 | /* |
| 132 | * to accurately set the time, reset the divider chain and keep it in |
| 133 | * reset state until all time/date registers are written |
| 134 | */ |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 135 | rc = pcf85063_stop_clock(client, &ctrl1); |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 136 | if (rc != 0) |
| 137 | return rc; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 138 | |
| 139 | /* hours, minutes and seconds */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 140 | regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */ |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 141 | |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 142 | regs[1] = bin2bcd(tm->tm_min); |
| 143 | regs[2] = bin2bcd(tm->tm_hour); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 144 | |
| 145 | /* Day of month, 1 - 31 */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 146 | regs[3] = bin2bcd(tm->tm_mday); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 147 | |
| 148 | /* Day, 0 - 6 */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 149 | regs[4] = tm->tm_wday & 0x07; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 150 | |
| 151 | /* month, 1 - 12 */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 152 | regs[5] = bin2bcd(tm->tm_mon + 1); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 153 | |
| 154 | /* year and century */ |
Alexandre Belloni | c421ce7 | 2016-07-18 11:08:59 +0200 | [diff] [blame] | 155 | regs[6] = bin2bcd(tm->tm_year - 100); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 156 | |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 157 | /* write all registers at once */ |
| 158 | rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC, |
| 159 | sizeof(regs), regs); |
| 160 | if (rc < 0) { |
| 161 | dev_err(&client->dev, "date/time register write error\n"); |
| 162 | return rc; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 165 | /* |
| 166 | * Write the control register as a separate action since the size of |
| 167 | * the register space is different between the PCF85063TP and |
| 168 | * PCF85063A devices. The rollover point can not be used. |
| 169 | */ |
| 170 | rc = pcf85063_start_clock(client, ctrl1); |
| 171 | if (rc != 0) |
| 172 | return rc; |
| 173 | |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 177 | static const struct rtc_class_ops pcf85063_rtc_ops = { |
| 178 | .read_time = pcf85063_rtc_read_time, |
| 179 | .set_time = pcf85063_rtc_set_time |
| 180 | }; |
| 181 | |
| 182 | static int pcf85063_probe(struct i2c_client *client, |
| 183 | const struct i2c_device_id *id) |
| 184 | { |
Alexandre Belloni | 2da424a | 2016-03-02 11:25:43 +0100 | [diff] [blame] | 185 | struct rtc_device *rtc; |
Mirza Krak | c18b4c5 | 2016-10-17 15:53:31 +0200 | [diff] [blame] | 186 | int err; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 187 | |
| 188 | dev_dbg(&client->dev, "%s\n", __func__); |
| 189 | |
| 190 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 191 | return -ENODEV; |
| 192 | |
Mirza Krak | c18b4c5 | 2016-10-17 15:53:31 +0200 | [diff] [blame] | 193 | err = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1); |
| 194 | if (err < 0) { |
| 195 | dev_err(&client->dev, "RTC chip is not present\n"); |
| 196 | return err; |
| 197 | } |
| 198 | |
Alexandre Belloni | 2da424a | 2016-03-02 11:25:43 +0100 | [diff] [blame] | 199 | rtc = devm_rtc_device_register(&client->dev, |
| 200 | pcf85063_driver.driver.name, |
| 201 | &pcf85063_rtc_ops, THIS_MODULE); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 202 | |
Alexandre Belloni | 2da424a | 2016-03-02 11:25:43 +0100 | [diff] [blame] | 203 | return PTR_ERR_OR_ZERO(rtc); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static const struct i2c_device_id pcf85063_id[] = { |
| 207 | { "pcf85063", 0 }, |
| 208 | { } |
| 209 | }; |
| 210 | MODULE_DEVICE_TABLE(i2c, pcf85063_id); |
| 211 | |
| 212 | #ifdef CONFIG_OF |
| 213 | static const struct of_device_id pcf85063_of_match[] = { |
| 214 | { .compatible = "nxp,pcf85063" }, |
| 215 | {} |
| 216 | }; |
| 217 | MODULE_DEVICE_TABLE(of, pcf85063_of_match); |
| 218 | #endif |
| 219 | |
| 220 | static struct i2c_driver pcf85063_driver = { |
| 221 | .driver = { |
| 222 | .name = "rtc-pcf85063", |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 223 | .of_match_table = of_match_ptr(pcf85063_of_match), |
| 224 | }, |
| 225 | .probe = pcf85063_probe, |
| 226 | .id_table = pcf85063_id, |
| 227 | }; |
| 228 | |
| 229 | module_i2c_driver(pcf85063_driver); |
| 230 | |
| 231 | MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>"); |
| 232 | MODULE_DESCRIPTION("PCF85063 RTC driver"); |
| 233 | MODULE_LICENSE("GPL"); |