i2c.c 7.9 KB

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