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