MIPS: perf: fix deadlock
[linux-stable.git] / drivers / rtc / rtc-pcf85063.c
bloba06dff994c8316c0062b39d3704055a1305584a8
1 /*
2 * An I2C driver for the PCF85063 RTC
3 * Copyright 2014 Rose Technology
5 * Author: Søren Andersen <san@rosetechnology.dk>
6 * Maintainers: http://www.nslu2-linux.org/
8 * based on the other drivers in this same directory.
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.
14 #include <linux/i2c.h>
15 #include <linux/bcd.h>
16 #include <linux/rtc.h>
17 #include <linux/module.h>
20 * Information for this driver was pulled from the following datasheets.
22 * http://www.nxp.com/documents/data_sheet/PCF85063A.pdf
23 * http://www.nxp.com/documents/data_sheet/PCF85063TP.pdf
25 * PCF85063A -- Rev. 6 — 18 November 2015
26 * PCF85063TP -- Rev. 4 — 6 May 2015
29 #define PCF85063_REG_CTRL1 0x00 /* status */
30 #define PCF85063_REG_CTRL1_STOP BIT(5)
31 #define PCF85063_REG_CTRL2 0x01
33 #define PCF85063_REG_SC 0x04 /* datetime */
34 #define PCF85063_REG_SC_OS 0x80
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
42 static struct i2c_driver pcf85063_driver;
44 static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
46 s32 ret;
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;
54 /* stop the clock */
55 ret |= PCF85063_REG_CTRL1_STOP;
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;
63 *ctrl1 = ret;
65 return 0;
68 static int pcf85063_start_clock(struct i2c_client *client, u8 ctrl1)
70 s32 ret;
72 /* start the clock */
73 ctrl1 &= PCF85063_REG_CTRL1_STOP;
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;
81 return 0;
84 static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
86 int rc;
87 u8 regs[7];
90 * while reading, the time/date registers are blocked and not updated
91 * anymore until the access is finished. To not lose a second
92 * event, the access must be finished within one second. So, read all
93 * time/date registers in one turn.
95 rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
96 sizeof(regs), regs);
97 if (rc != sizeof(regs)) {
98 dev_err(&client->dev, "date/time register read error\n");
99 return -EIO;
102 /* if the clock has lost its power it makes no sense to use its time */
103 if (regs[0] & PCF85063_REG_SC_OS) {
104 dev_warn(&client->dev, "Power loss detected, invalid time\n");
105 return -EINVAL;
108 tm->tm_sec = bcd2bin(regs[0] & 0x7F);
109 tm->tm_min = bcd2bin(regs[1] & 0x7F);
110 tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
111 tm->tm_mday = bcd2bin(regs[3] & 0x3F);
112 tm->tm_wday = regs[4] & 0x07;
113 tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
114 tm->tm_year = bcd2bin(regs[6]);
115 tm->tm_year += 100;
117 return rtc_valid_tm(tm);
120 static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
122 int rc;
123 u8 regs[7];
124 u8 ctrl1;
126 if ((tm->tm_year < 100) || (tm->tm_year > 199))
127 return -EINVAL;
130 * to accurately set the time, reset the divider chain and keep it in
131 * reset state until all time/date registers are written
133 rc = pcf85063_stop_clock(client, &ctrl1);
134 if (rc != 0)
135 return rc;
137 /* hours, minutes and seconds */
138 regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
140 regs[1] = bin2bcd(tm->tm_min);
141 regs[2] = bin2bcd(tm->tm_hour);
143 /* Day of month, 1 - 31 */
144 regs[3] = bin2bcd(tm->tm_mday);
146 /* Day, 0 - 6 */
147 regs[4] = tm->tm_wday & 0x07;
149 /* month, 1 - 12 */
150 regs[5] = bin2bcd(tm->tm_mon + 1);
152 /* year and century */
153 regs[6] = bin2bcd(tm->tm_year - 100);
155 /* write all registers at once */
156 rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
157 sizeof(regs), regs);
158 if (rc < 0) {
159 dev_err(&client->dev, "date/time register write error\n");
160 return rc;
164 * Write the control register as a separate action since the size of
165 * the register space is different between the PCF85063TP and
166 * PCF85063A devices. The rollover point can not be used.
168 rc = pcf85063_start_clock(client, ctrl1);
169 if (rc != 0)
170 return rc;
172 return 0;
175 static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
177 return pcf85063_get_datetime(to_i2c_client(dev), tm);
180 static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
182 return pcf85063_set_datetime(to_i2c_client(dev), tm);
185 static const struct rtc_class_ops pcf85063_rtc_ops = {
186 .read_time = pcf85063_rtc_read_time,
187 .set_time = pcf85063_rtc_set_time
190 static int pcf85063_probe(struct i2c_client *client,
191 const struct i2c_device_id *id)
193 struct rtc_device *rtc;
194 int err;
196 dev_dbg(&client->dev, "%s\n", __func__);
198 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
199 return -ENODEV;
201 err = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
202 if (err < 0) {
203 dev_err(&client->dev, "RTC chip is not present\n");
204 return err;
207 rtc = devm_rtc_device_register(&client->dev,
208 pcf85063_driver.driver.name,
209 &pcf85063_rtc_ops, THIS_MODULE);
211 return PTR_ERR_OR_ZERO(rtc);
214 static const struct i2c_device_id pcf85063_id[] = {
215 { "pcf85063", 0 },
218 MODULE_DEVICE_TABLE(i2c, pcf85063_id);
220 #ifdef CONFIG_OF
221 static const struct of_device_id pcf85063_of_match[] = {
222 { .compatible = "nxp,pcf85063" },
225 MODULE_DEVICE_TABLE(of, pcf85063_of_match);
226 #endif
228 static struct i2c_driver pcf85063_driver = {
229 .driver = {
230 .name = "rtc-pcf85063",
231 .of_match_table = of_match_ptr(pcf85063_of_match),
233 .probe = pcf85063_probe,
234 .id_table = pcf85063_id,
237 module_i2c_driver(pcf85063_driver);
239 MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
240 MODULE_DESCRIPTION("PCF85063 RTC driver");
241 MODULE_LICENSE("GPL");