i2c.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * linux/drivers/acorn/char/i2c.c
  3. *
  4. * Copyright (C) 2000 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * ARM IOC/IOMD i2c driver.
  11. *
  12. * On Acorn machines, the following i2c devices are on the bus:
  13. * - PCF8583 real time clock & static RAM
  14. */
  15. #include <linux/capability.h>
  16. #include <linux/init.h>
  17. #include <linux/sched.h>
  18. #include <linux/time.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/rtc.h>
  21. #include <linux/i2c.h>
  22. #include <linux/i2c-algo-bit.h>
  23. #include <linux/fs.h>
  24. #include <asm/hardware.h>
  25. #include <asm/io.h>
  26. #include <asm/hardware/ioc.h>
  27. #include <asm/system.h>
  28. #include <asm/uaccess.h>
  29. #include "pcf8583.h"
  30. extern int (*set_rtc)(void);
  31. static struct i2c_client *rtc_client;
  32. static const unsigned char days_in_mon[] =
  33. { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  34. #define CMOS_CHECKSUM (63)
  35. /*
  36. * Acorn machines store the year in the static RAM at
  37. * location 128.
  38. */
  39. #define CMOS_YEAR (64 + 128)
  40. static inline int rtc_command(int cmd, void *data)
  41. {
  42. int ret = -EIO;
  43. if (rtc_client)
  44. ret = rtc_client->driver->command(rtc_client, cmd, data);
  45. return ret;
  46. }
  47. /*
  48. * Update the century + year bytes in the CMOS RAM, ensuring
  49. * that the check byte is correctly adjusted for the change.
  50. */
  51. static int rtc_update_year(unsigned int new_year)
  52. {
  53. unsigned char yr[2], chk;
  54. struct mem cmos_year = { CMOS_YEAR, sizeof(yr), yr };
  55. struct mem cmos_check = { CMOS_CHECKSUM, 1, &chk };
  56. int ret;
  57. ret = rtc_command(MEM_READ, &cmos_check);
  58. if (ret)
  59. goto out;
  60. ret = rtc_command(MEM_READ, &cmos_year);
  61. if (ret)
  62. goto out;
  63. chk -= yr[1] + yr[0];
  64. yr[1] = new_year / 100;
  65. yr[0] = new_year % 100;
  66. chk += yr[1] + yr[0];
  67. ret = rtc_command(MEM_WRITE, &cmos_year);
  68. if (ret == 0)
  69. ret = rtc_command(MEM_WRITE, &cmos_check);
  70. out:
  71. return ret;
  72. }
  73. /*
  74. * Read the current RTC time and date, and update xtime.
  75. */
  76. static void get_rtc_time(struct rtc_tm *rtctm, unsigned int *year)
  77. {
  78. unsigned char ctrl, yr[2];
  79. struct mem rtcmem = { CMOS_YEAR, sizeof(yr), yr };
  80. int real_year, year_offset;
  81. /*
  82. * Ensure that the RTC is running.
  83. */
  84. rtc_command(RTC_GETCTRL, &ctrl);
  85. if (ctrl & 0xc0) {
  86. unsigned char new_ctrl = ctrl & ~0xc0;
  87. printk(KERN_WARNING "RTC: resetting control %02x -> %02x\n",
  88. ctrl, new_ctrl);
  89. rtc_command(RTC_SETCTRL, &new_ctrl);
  90. }
  91. if (rtc_command(RTC_GETDATETIME, rtctm) ||
  92. rtc_command(MEM_READ, &rtcmem))
  93. return;
  94. real_year = yr[0];
  95. /*
  96. * The RTC year holds the LSB two bits of the current
  97. * year, which should reflect the LSB two bits of the
  98. * CMOS copy of the year. Any difference indicates
  99. * that we have to correct the CMOS version.
  100. */
  101. year_offset = rtctm->year_off - (real_year & 3);
  102. if (year_offset < 0)
  103. /*
  104. * RTC year wrapped. Adjust it appropriately.
  105. */
  106. year_offset += 4;
  107. *year = real_year + year_offset + yr[1] * 100;
  108. }
  109. static int set_rtc_time(struct rtc_tm *rtctm, unsigned int year)
  110. {
  111. unsigned char leap;
  112. int ret;
  113. leap = (!(year % 4) && (year % 100)) || !(year % 400);
  114. if (rtctm->mon > 12 || rtctm->mon == 0 || rtctm->mday == 0)
  115. return -EINVAL;
  116. if (rtctm->mday > (days_in_mon[rtctm->mon] + (rtctm->mon == 2 && leap)))
  117. return -EINVAL;
  118. if (rtctm->hours >= 24 || rtctm->mins >= 60 || rtctm->secs >= 60)
  119. return -EINVAL;
  120. /*
  121. * The RTC's own 2-bit year must reflect the least
  122. * significant two bits of the CMOS year.
  123. */
  124. rtctm->year_off = (year % 100) & 3;
  125. ret = rtc_command(RTC_SETDATETIME, rtctm);
  126. if (ret == 0)
  127. ret = rtc_update_year(year);
  128. return ret;
  129. }
  130. /*
  131. * Set the RTC time only. Note that
  132. * we do not touch the date.
  133. */
  134. static int k_set_rtc_time(void)
  135. {
  136. struct rtc_tm new_rtctm, old_rtctm;
  137. unsigned long nowtime = xtime.tv_sec;
  138. if (rtc_command(RTC_GETDATETIME, &old_rtctm))
  139. return 0;
  140. new_rtctm.cs = xtime.tv_nsec / 10000000;
  141. new_rtctm.secs = nowtime % 60; nowtime /= 60;
  142. new_rtctm.mins = nowtime % 60; nowtime /= 60;
  143. new_rtctm.hours = nowtime % 24;
  144. /*
  145. * avoid writing when we're going to change the day
  146. * of the month. We will retry in the next minute.
  147. * This basically means that if the RTC must not drift
  148. * by more than 1 minute in 11 minutes.
  149. *
  150. * [ rtc: 1/1/2000 23:58:00, real 2/1/2000 00:01:00,
  151. * rtc gets set to 1/1/2000 00:01:00 ]
  152. */
  153. if ((old_rtctm.hours == 23 && old_rtctm.mins == 59) ||
  154. (new_rtctm.hours == 23 && new_rtctm.mins == 59))
  155. return 1;
  156. return rtc_command(RTC_SETTIME, &new_rtctm);
  157. }
  158. static int rtc_ioctl(struct inode *inode, struct file *file,
  159. unsigned int cmd, unsigned long arg)
  160. {
  161. unsigned int year;
  162. struct rtc_time rtctm;
  163. struct rtc_tm rtc_raw;
  164. switch (cmd) {
  165. case RTC_ALM_READ:
  166. case RTC_ALM_SET:
  167. break;
  168. case RTC_RD_TIME:
  169. memset(&rtctm, 0, sizeof(struct rtc_time));
  170. get_rtc_time(&rtc_raw, &year);
  171. rtctm.tm_sec = rtc_raw.secs;
  172. rtctm.tm_min = rtc_raw.mins;
  173. rtctm.tm_hour = rtc_raw.hours;
  174. rtctm.tm_mday = rtc_raw.mday;
  175. rtctm.tm_mon = rtc_raw.mon - 1; /* month starts at 0 */
  176. rtctm.tm_year = year - 1900; /* starts at 1900 */
  177. return copy_to_user((void *)arg, &rtctm, sizeof(rtctm))
  178. ? -EFAULT : 0;
  179. case RTC_SET_TIME:
  180. if (!capable(CAP_SYS_TIME))
  181. return -EACCES;
  182. if (copy_from_user(&rtctm, (void *)arg, sizeof(rtctm)))
  183. return -EFAULT;
  184. rtc_raw.secs = rtctm.tm_sec;
  185. rtc_raw.mins = rtctm.tm_min;
  186. rtc_raw.hours = rtctm.tm_hour;
  187. rtc_raw.mday = rtctm.tm_mday;
  188. rtc_raw.mon = rtctm.tm_mon + 1;
  189. year = rtctm.tm_year + 1900;
  190. return set_rtc_time(&rtc_raw, year);
  191. break;
  192. case RTC_EPOCH_READ:
  193. return put_user(1900, (unsigned long *)arg);
  194. }
  195. return -EINVAL;
  196. }
  197. static struct file_operations rtc_fops = {
  198. .ioctl = rtc_ioctl,
  199. };
  200. static struct miscdevice rtc_dev = {
  201. .minor = RTC_MINOR,
  202. .name = "rtc",
  203. .fops = &rtc_fops,
  204. };
  205. /* IOC / IOMD i2c driver */
  206. #define FORCE_ONES 0xdc
  207. #define SCL 0x02
  208. #define SDA 0x01
  209. /*
  210. * We must preserve all non-i2c output bits in IOC_CONTROL.
  211. * Note also that we need to preserve the value of SCL and
  212. * SDA outputs as well (which may be different from the
  213. * values read back from IOC_CONTROL).
  214. */
  215. static u_int force_ones;
  216. static void ioc_setscl(void *data, int state)
  217. {
  218. u_int ioc_control = ioc_readb(IOC_CONTROL) & ~(SCL | SDA);
  219. u_int ones = force_ones;
  220. if (state)
  221. ones |= SCL;
  222. else
  223. ones &= ~SCL;
  224. force_ones = ones;
  225. ioc_writeb(ioc_control | ones, IOC_CONTROL);
  226. }
  227. static void ioc_setsda(void *data, int state)
  228. {
  229. u_int ioc_control = ioc_readb(IOC_CONTROL) & ~(SCL | SDA);
  230. u_int ones = force_ones;
  231. if (state)
  232. ones |= SDA;
  233. else
  234. ones &= ~SDA;
  235. force_ones = ones;
  236. ioc_writeb(ioc_control | ones, IOC_CONTROL);
  237. }
  238. static int ioc_getscl(void *data)
  239. {
  240. return (ioc_readb(IOC_CONTROL) & SCL) != 0;
  241. }
  242. static int ioc_getsda(void *data)
  243. {
  244. return (ioc_readb(IOC_CONTROL) & SDA) != 0;
  245. }
  246. static struct i2c_algo_bit_data ioc_data = {
  247. .setsda = ioc_setsda,
  248. .setscl = ioc_setscl,
  249. .getsda = ioc_getsda,
  250. .getscl = ioc_getscl,
  251. .udelay = 80,
  252. .mdelay = 80,
  253. .timeout = 100
  254. };
  255. static int ioc_client_reg(struct i2c_client *client)
  256. {
  257. if (client->driver->id == I2C_DRIVERID_PCF8583 &&
  258. client->addr == 0x50) {
  259. struct rtc_tm rtctm;
  260. unsigned int year;
  261. struct timespec tv;
  262. rtc_client = client;
  263. get_rtc_time(&rtctm, &year);
  264. tv.tv_nsec = rtctm.cs * 10000000;
  265. tv.tv_sec = mktime(year, rtctm.mon, rtctm.mday,
  266. rtctm.hours, rtctm.mins, rtctm.secs);
  267. do_settimeofday(&tv);
  268. set_rtc = k_set_rtc_time;
  269. }
  270. return 0;
  271. }
  272. static int ioc_client_unreg(struct i2c_client *client)
  273. {
  274. if (client == rtc_client) {
  275. set_rtc = NULL;
  276. rtc_client = NULL;
  277. }
  278. return 0;
  279. }
  280. static struct i2c_adapter ioc_ops = {
  281. .id = I2C_HW_B_IOC,
  282. .algo_data = &ioc_data,
  283. .client_register = ioc_client_reg,
  284. .client_unregister = ioc_client_unreg,
  285. };
  286. static int __init i2c_ioc_init(void)
  287. {
  288. int ret;
  289. force_ones = FORCE_ONES | SCL | SDA;
  290. ret = i2c_bit_add_bus(&ioc_ops);
  291. if (ret >= 0){
  292. ret = misc_register(&rtc_dev);
  293. if(ret < 0)
  294. i2c_bit_del_bus(&ioc_ops);
  295. }
  296. return ret;
  297. }
  298. __initcall(i2c_ioc_init);