2
0

841-rtc_pt7c4338.patch 6.9 KB

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