rtc: pcf2127: fix reading uninitialized value on RTC_READ_VL ioctl
[firefly-linux-kernel-4.4.55.git] / drivers / rtc / rtc-pcf2127.c
1 /*
2  * An I2C driver for the NXP PCF2127 RTC
3  * Copyright 2013 Til-Technologies
4  *
5  * Author: Renaud Cerrato <r.cerrato@til-technologies.fr>
6  *
7  * based on the other drivers in this same directory.
8  *
9  * http://www.nxp.com/documents/data_sheet/PCF2127AT.pdf
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
15
16 #include <linux/i2c.h>
17 #include <linux/bcd.h>
18 #include <linux/rtc.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22
23 #define DRV_VERSION "0.0.1"
24
25 #define PCF2127_REG_CTRL1       (0x00)  /* Control Register 1 */
26 #define PCF2127_REG_CTRL2       (0x01)  /* Control Register 2 */
27
28 #define PCF2127_REG_CTRL3       (0x02)  /* Control Register 3 */
29 #define PCF2127_REG_CTRL3_BLF           BIT(2)
30
31 #define PCF2127_REG_SC          (0x03)  /* datetime */
32 #define PCF2127_REG_MN          (0x04)
33 #define PCF2127_REG_HR          (0x05)
34 #define PCF2127_REG_DM          (0x06)
35 #define PCF2127_REG_DW          (0x07)
36 #define PCF2127_REG_MO          (0x08)
37 #define PCF2127_REG_YR          (0x09)
38
39 #define PCF2127_OSF             BIT(7)  /* Oscillator Fail flag */
40
41 static struct i2c_driver pcf2127_driver;
42
43 struct pcf2127 {
44         struct rtc_device *rtc;
45 };
46
47 /*
48  * In the routines that deal directly with the pcf2127 hardware, we use
49  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
50  */
51 static int pcf2127_get_datetime(struct i2c_client *client, struct rtc_time *tm)
52 {
53         unsigned char buf[10] = { PCF2127_REG_CTRL1 };
54
55         /* read registers */
56         if (i2c_master_send(client, buf, 1) != 1 ||
57                 i2c_master_recv(client, buf, sizeof(buf)) != sizeof(buf)) {
58                 dev_err(&client->dev, "%s: read error\n", __func__);
59                 return -EIO;
60         }
61
62         if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF)
63                 dev_info(&client->dev,
64                         "low voltage detected, check/replace RTC battery.\n");
65
66         if (buf[PCF2127_REG_SC] & PCF2127_OSF) {
67                 /*
68                  * no need clear the flag here,
69                  * it will be cleared once the new date is saved
70                  */
71                 dev_warn(&client->dev,
72                          "oscillator stop detected, date/time is not reliable\n");
73                 return -EINVAL;
74         }
75
76         dev_dbg(&client->dev,
77                 "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, "
78                 "sec=%02x, min=%02x, hr=%02x, "
79                 "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
80                 __func__,
81                 buf[0], buf[1], buf[2],
82                 buf[3], buf[4], buf[5],
83                 buf[6], buf[7], buf[8], buf[9]);
84
85
86         tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
87         tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
88         tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */
89         tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
90         tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
91         tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
92         tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
93         if (tm->tm_year < 70)
94                 tm->tm_year += 100;     /* assume we are in 1970...2069 */
95
96         dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
97                 "mday=%d, mon=%d, year=%d, wday=%d\n",
98                 __func__,
99                 tm->tm_sec, tm->tm_min, tm->tm_hour,
100                 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
101
102         return rtc_valid_tm(tm);
103 }
104
105 static int pcf2127_set_datetime(struct i2c_client *client, struct rtc_time *tm)
106 {
107         unsigned char buf[8];
108         int i = 0, err;
109
110         dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
111                 "mday=%d, mon=%d, year=%d, wday=%d\n",
112                 __func__,
113                 tm->tm_sec, tm->tm_min, tm->tm_hour,
114                 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
115
116         /* start register address */
117         buf[i++] = PCF2127_REG_SC;
118
119         /* hours, minutes and seconds */
120         buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */
121         buf[i++] = bin2bcd(tm->tm_min);
122         buf[i++] = bin2bcd(tm->tm_hour);
123         buf[i++] = bin2bcd(tm->tm_mday);
124         buf[i++] = tm->tm_wday & 0x07;
125
126         /* month, 1 - 12 */
127         buf[i++] = bin2bcd(tm->tm_mon + 1);
128
129         /* year */
130         buf[i++] = bin2bcd(tm->tm_year % 100);
131
132         /* write register's data */
133         err = i2c_master_send(client, buf, i);
134         if (err != i) {
135                 dev_err(&client->dev,
136                         "%s: err=%d", __func__, err);
137                 return -EIO;
138         }
139
140         return 0;
141 }
142
143 #ifdef CONFIG_RTC_INTF_DEV
144 static int pcf2127_rtc_ioctl(struct device *dev,
145                                 unsigned int cmd, unsigned long arg)
146 {
147         struct i2c_client *client = to_i2c_client(dev);
148         unsigned char buf = PCF2127_REG_CTRL3;
149         int touser;
150         int ret;
151
152         switch (cmd) {
153         case RTC_VL_READ:
154                 ret = i2c_master_send(client, &buf, 1);
155                 if (!ret)
156                         ret = -EIO;
157                 if (ret < 0)
158                         return ret;
159
160                 ret = i2c_master_recv(client, &buf, 1);
161                 if (!ret)
162                         ret = -EIO;
163                 if (ret < 0)
164                         return ret;
165
166                 touser = buf & PCF2127_REG_CTRL3_BLF ? 1 : 0;
167
168                 if (copy_to_user((void __user *)arg, &touser, sizeof(int)))
169                         return -EFAULT;
170                 return 0;
171         default:
172                 return -ENOIOCTLCMD;
173         }
174 }
175 #else
176 #define pcf2127_rtc_ioctl NULL
177 #endif
178
179 static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
180 {
181         return pcf2127_get_datetime(to_i2c_client(dev), tm);
182 }
183
184 static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
185 {
186         return pcf2127_set_datetime(to_i2c_client(dev), tm);
187 }
188
189 static const struct rtc_class_ops pcf2127_rtc_ops = {
190         .ioctl          = pcf2127_rtc_ioctl,
191         .read_time      = pcf2127_rtc_read_time,
192         .set_time       = pcf2127_rtc_set_time,
193 };
194
195 static int pcf2127_probe(struct i2c_client *client,
196                                 const struct i2c_device_id *id)
197 {
198         struct pcf2127 *pcf2127;
199
200         dev_dbg(&client->dev, "%s\n", __func__);
201
202         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
203                 return -ENODEV;
204
205         pcf2127 = devm_kzalloc(&client->dev, sizeof(struct pcf2127),
206                                 GFP_KERNEL);
207         if (!pcf2127)
208                 return -ENOMEM;
209
210         dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
211
212         i2c_set_clientdata(client, pcf2127);
213
214         pcf2127->rtc = devm_rtc_device_register(&client->dev,
215                                 pcf2127_driver.driver.name,
216                                 &pcf2127_rtc_ops, THIS_MODULE);
217
218         return PTR_ERR_OR_ZERO(pcf2127->rtc);
219 }
220
221 static const struct i2c_device_id pcf2127_id[] = {
222         { "pcf2127", 0 },
223         { }
224 };
225 MODULE_DEVICE_TABLE(i2c, pcf2127_id);
226
227 #ifdef CONFIG_OF
228 static const struct of_device_id pcf2127_of_match[] = {
229         { .compatible = "nxp,pcf2127" },
230         {}
231 };
232 MODULE_DEVICE_TABLE(of, pcf2127_of_match);
233 #endif
234
235 static struct i2c_driver pcf2127_driver = {
236         .driver         = {
237                 .name   = "rtc-pcf2127",
238                 .of_match_table = of_match_ptr(pcf2127_of_match),
239         },
240         .probe          = pcf2127_probe,
241         .id_table       = pcf2127_id,
242 };
243
244 module_i2c_driver(pcf2127_driver);
245
246 MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
247 MODULE_DESCRIPTION("NXP PCF2127 RTC driver");
248 MODULE_LICENSE("GPL v2");
249 MODULE_VERSION(DRV_VERSION);