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