linux/3.3: add support for the Pericom PT7C4338 I2C RTC chip
[openwrt.git] / target / linux / generic / patches-3.3 / 841-rtc_pt7c4338.patch
1 Index: linux-3.3/drivers/rtc/Kconfig
2 ===================================================================
3 --- linux-3.3.orig/drivers/rtc/Kconfig  2012-03-20 07:52:18.838582040 +0100
4 +++ linux-3.3/drivers/rtc/Kconfig       2012-03-20 07:52:19.228579409 +0100
5 @@ -379,6 +379,15 @@
6           This driver can also be built as a module. If so, the module
7           will be called rtc-rv3029c2.
8  
9 +config RTC_DRV_PT7C4338
10 +       tristate "Pericom Technology Inc. PT7C4338 RTC"
11 +       help
12 +         If you say yes here you get support for the Pericom Technology
13 +         Inc. PT7C4338 RTC chip.
14 +
15 +         This driver can also be built as a module. If so, the module
16 +         will be called rtc-pt7c4338.
17 +
18  endif # I2C
19  
20  comment "SPI RTC drivers"
21 Index: linux-3.3/drivers/rtc/Makefile
22 ===================================================================
23 --- linux-3.3.orig/drivers/rtc/Makefile 2012-03-20 07:52:18.838582040 +0100
24 +++ linux-3.3/drivers/rtc/Makefile      2012-03-20 07:52:19.228579409 +0100
25 @@ -79,6 +79,7 @@
26  obj-$(CONFIG_RTC_DRV_PL031)    += rtc-pl031.o
27  obj-$(CONFIG_RTC_DRV_PM8XXX)   += rtc-pm8xxx.o
28  obj-$(CONFIG_RTC_DRV_PS3)      += rtc-ps3.o
29 +obj-$(CONFIG_RTC_DRV_PT7C4338) += rtc-pt7c4338.o
30  obj-$(CONFIG_RTC_DRV_PUV3)     += rtc-puv3.o
31  obj-$(CONFIG_RTC_DRV_PXA)      += rtc-pxa.o
32  obj-$(CONFIG_RTC_DRV_R9701)    += rtc-r9701.o
33 Index: linux-3.3/drivers/rtc/rtc-pt7c4338.c
34 ===================================================================
35 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
36 +++ linux-3.3/drivers/rtc/rtc-pt7c4338.c        2012-03-20 07:52:19.228579409 +0100
37 @@ -0,0 +1,216 @@
38 +/*
39 + * Copyright 2010 Freescale Semiconductor, Inc.
40 + *
41 + * Author:     Priyanka Jain <Priyanka.Jain@freescale.com>
42 + *
43 + * See file CREDITS for list of people who contributed to this
44 + * project.
45 + *
46 + * This program is free software; you can redistribute it and/or
47 + * modify it under the terms of the GNU General Public License as
48 + * published by the Free Software Foundation; either version 2 of
49 + * the License, or (at your option) any later version.
50 + *
51 + * This program is distributed in the hope that it will be useful,
52 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
53 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
54 + * GNU General Public License for more details.
55 + *
56 + * You should have received a copy of the GNU General Public License
57 + * along with this program; if not, write to the Free Software
58 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
59 + * MA 02111-1307 USA
60 + */
61 +
62 +/*
63 + * This file provides Date & Time support (no alarms) for PT7C4338 chip.
64 + *
65 + * This file is based on drivers/rtc/rtc-ds1307.c
66 + *
67 + * PT7C4338 chip is manufactured by Pericom Technology Inc.
68 + * It is a serial real-time clock which provides
69 + * 1)Low-power clock/calendar.
70 + * 2)Programmable square-wave output.
71 + * It has 56 bytes of nonvolatile RAM.
72 + */
73 +
74 +#include <linux/kernel.h>
75 +#include <linux/module.h>
76 +#include <linux/slab.h>
77 +#include <linux/i2c.h>
78 +#include <linux/rtc.h>
79 +#include <linux/bcd.h>
80 +
81 +/* RTC register addresses */
82 +#define PT7C4338_REG_SECONDS          0x00
83 +#define PT7C4338_REG_MINUTES          0x01
84 +#define PT7C4338_REG_HOURS            0x02
85 +#define PT7C4338_REG_AMPM             0x02
86 +#define PT7C4338_REG_DAY              0x03
87 +#define PT7C4338_REG_DATE             0x04
88 +#define PT7C4338_REG_MONTH            0x05
89 +#define PT7C4338_REG_YEAR             0x06
90 +#define PT7C4338_REG_CTRL_STAT        0x07
91 +
92 +/* RTC second register address bit */
93 +#define PT7C4338_SEC_BIT_CH           0x80     /*Clock Halt (in Register 0)*/
94 +
95 +/* RTC control and status register bits */
96 +#define PT7C4338_CTRL_STAT_BIT_RS0    0x1      /*Rate select 0*/
97 +#define PT7C4338_CTRL_STAT_BIT_RS1    0x2      /*Rate select 1*/
98 +#define PT7C4338_CTRL_STAT_BIT_SQWE   0x10     /*Square Wave Enable*/
99 +#define PT7C4338_CTRL_STAT_BIT_OSF    0x20     /*Oscillator Stop Flag*/
100 +#define PT7C4338_CTRL_STAT_BIT_OUT    0x80     /*Output Level Control*/
101 +
102 +static const struct i2c_device_id pt7c4338_id[] = {
103 +       { "pt7c4338", 0 },
104 +       { }
105 +};
106 +MODULE_DEVICE_TABLE(i2c, pt7c4338_id);
107 +
108 +struct pt7c4338{
109 +       struct i2c_client *client;
110 +       struct rtc_device *rtc;
111 +};
112 +
113 +static int pt7c4338_read_time(struct device *dev, struct rtc_time *time)
114 +{
115 +       struct i2c_client *client = to_i2c_client(dev);
116 +       int ret;
117 +       u8 buf[7];
118 +       u8 year, month, day, hour, minute, second;
119 +       u8 week, twelve_hr, am_pm;
120 +
121 +       ret = i2c_smbus_read_i2c_block_data(client,
122 +                       PT7C4338_REG_SECONDS, 7, buf);
123 +       if (ret < 0)
124 +               return ret;
125 +       if (ret < 7)
126 +               return -EIO;
127 +
128 +       second = buf[0];
129 +       minute = buf[1];
130 +       hour = buf[2];
131 +       week = buf[3];
132 +       day = buf[4];
133 +       month = buf[5];
134 +       year = buf[6];
135 +
136 +       /* Extract additional information for AM/PM */
137 +       twelve_hr = hour & 0x40;
138 +       am_pm = hour & 0x20;
139 +
140 +       /* Write to rtc_time structure */
141 +       time->tm_sec = bcd2bin(second & 0x7f);
142 +       time->tm_min = bcd2bin(minute & 0x7f);
143 +       if (twelve_hr) {
144 +               /* Convert to 24 hr */
145 +               if (am_pm)
146 +                       time->tm_hour = bcd2bin(hour & 0x10) + 12;
147 +               else
148 +                       time->tm_hour = bcd2bin(hour & 0xBF);
149 +       } else {
150 +               time->tm_hour = bcd2bin(hour);
151 +       }
152 +
153 +       time->tm_wday = bcd2bin(week & 0x07) - 1;
154 +       time->tm_mday = bcd2bin(day & 0x3f);
155 +       time->tm_mon = bcd2bin(month & 0x1F) - 1;
156 +       /* assume 20YY not 19YY */
157 +       time->tm_year = bcd2bin(year) + 100;
158 +
159 +       return 0;
160 +}
161 +
162 +static int pt7c4338_set_time(struct device *dev, struct rtc_time *time)
163 +{
164 +       struct i2c_client *client = to_i2c_client(dev);
165 +       u8 buf[7];
166 +
167 +       /* Extract time from rtc_time and load into pt7c4338*/
168 +       buf[0] = bin2bcd(time->tm_sec);
169 +       buf[1] = bin2bcd(time->tm_min);
170 +       buf[2] = bin2bcd(time->tm_hour);
171 +       buf[3] = bin2bcd(time->tm_wday + 1); /* Day of the week */
172 +       buf[4] = bin2bcd(time->tm_mday); /* Date */
173 +       buf[5] = bin2bcd(time->tm_mon + 1);
174 +
175 +       /* assume 20YY not 19YY */
176 +       if (time->tm_year >= 100)
177 +               buf[6] = bin2bcd(time->tm_year - 100);
178 +       else
179 +               buf[6] = bin2bcd(time->tm_year);
180 +
181 +       return i2c_smbus_write_i2c_block_data(client,
182 +                                       PT7C4338_REG_SECONDS, 7, buf);
183 +}
184 +
185 +static const struct rtc_class_ops pt7c4338_rtc_ops = {
186 +       .read_time = pt7c4338_read_time,
187 +       .set_time = pt7c4338_set_time,
188 +};
189 +
190 +static int pt7c4338_probe(struct i2c_client *client,
191 +               const struct i2c_device_id *id)
192 +{
193 +       struct pt7c4338 *pt7c4338;
194 +       struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
195 +       int ret;
196 +
197 +       pt7c4338 = kzalloc(sizeof(struct pt7c4338), GFP_KERNEL);
198 +       if (!pt7c4338)
199 +               return -ENOMEM;
200 +
201 +       pt7c4338->client = client;
202 +       i2c_set_clientdata(client, pt7c4338);
203 +       pt7c4338->rtc = rtc_device_register(client->name, &client->dev,
204 +                                       &pt7c4338_rtc_ops, THIS_MODULE);
205 +       if (IS_ERR(pt7c4338->rtc)) {
206 +               ret = PTR_ERR(pt7c4338->rtc);
207 +               dev_err(&client->dev, "unable to register the class device\n");
208 +               goto out_free;
209 +       }
210 +
211 +       return 0;
212 +out_free:
213 +       i2c_set_clientdata(client, NULL);
214 +       kfree(pt7c4338);
215 +       return ret;
216 +}
217 +
218 +static int __devexit pt7c4338_remove(struct i2c_client *client)
219 +{
220 +       struct pt7c4338 *pt7c4338 = i2c_get_clientdata(client);
221 +
222 +       rtc_device_unregister(pt7c4338->rtc);
223 +       i2c_set_clientdata(client, NULL);
224 +       kfree(pt7c4338);
225 +       return 0;
226 +}
227 +
228 +static struct i2c_driver pt7c4338_driver = {
229 +       .driver = {
230 +               .name = "rtc-pt7c4338",
231 +               .owner = THIS_MODULE,
232 +       },
233 +       .probe = pt7c4338_probe,
234 +       .remove = __devexit_p(pt7c4338_remove),
235 +       .id_table = pt7c4338_id,
236 +};
237 +
238 +static int __init pt7c4338_init(void)
239 +{
240 +       return i2c_add_driver(&pt7c4338_driver);
241 +}
242 +
243 +static void __exit pt7c4338_exit(void)
244 +{
245 +       i2c_del_driver(&pt7c4338_driver);
246 +}
247 +
248 +module_init(pt7c4338_init);
249 +module_exit(pt7c4338_exit);
250 +
251 +MODULE_AUTHOR("Priyanka Jain <Priyanka.Jain@freescale.com>");
252 +MODULE_DESCRIPTION("pericom Technology Inc. PT7C4338 RTC Driver");
253 +MODULE_LICENSE("GPL");