rtc-v3020.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* drivers/rtc/rtc-v3020.c
  2. *
  3. * Copyright (C) 2006 8D Technologies inc.
  4. * Copyright (C) 2004 Compulab Ltd.
  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. * Driver for the V3020 RTC
  11. *
  12. * Changelog:
  13. *
  14. * 10-May-2006: Raphael Assenat <raph@8d.com>
  15. * - Converted to platform driver
  16. * - Use the generic rtc class
  17. *
  18. * ??-???-2004: Someone at Compulab
  19. * - Initial driver creation.
  20. *
  21. */
  22. #include <linux/platform_device.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/rtc.h>
  26. #include <linux/types.h>
  27. #include <linux/bcd.h>
  28. #include <linux/rtc-v3020.h>
  29. #include <linux/delay.h>
  30. #include <linux/gpio.h>
  31. #include <linux/io.h>
  32. #undef DEBUG
  33. struct v3020;
  34. struct v3020_chip_ops {
  35. int (*map_io)(struct v3020 *chip, struct platform_device *pdev,
  36. struct v3020_platform_data *pdata);
  37. void (*unmap_io)(struct v3020 *chip);
  38. unsigned char (*read_bit)(struct v3020 *chip);
  39. void (*write_bit)(struct v3020 *chip, unsigned char bit);
  40. };
  41. #define V3020_CS 0
  42. #define V3020_WR 1
  43. #define V3020_RD 2
  44. #define V3020_IO 3
  45. struct v3020_gpio {
  46. const char *name;
  47. unsigned int gpio;
  48. };
  49. struct v3020 {
  50. /* MMIO access */
  51. void __iomem *ioaddress;
  52. int leftshift;
  53. /* GPIO access */
  54. struct v3020_gpio *gpio;
  55. struct v3020_chip_ops *ops;
  56. struct rtc_device *rtc;
  57. };
  58. static int v3020_mmio_map(struct v3020 *chip, struct platform_device *pdev,
  59. struct v3020_platform_data *pdata)
  60. {
  61. if (pdev->num_resources != 1)
  62. return -EBUSY;
  63. if (pdev->resource[0].flags != IORESOURCE_MEM)
  64. return -EBUSY;
  65. chip->leftshift = pdata->leftshift;
  66. chip->ioaddress = ioremap(pdev->resource[0].start, 1);
  67. if (chip->ioaddress == NULL)
  68. return -EBUSY;
  69. return 0;
  70. }
  71. static void v3020_mmio_unmap(struct v3020 *chip)
  72. {
  73. iounmap(chip->ioaddress);
  74. }
  75. static void v3020_mmio_write_bit(struct v3020 *chip, unsigned char bit)
  76. {
  77. writel(bit << chip->leftshift, chip->ioaddress);
  78. }
  79. static unsigned char v3020_mmio_read_bit(struct v3020 *chip)
  80. {
  81. return readl(chip->ioaddress) & (1 << chip->leftshift);
  82. }
  83. static struct v3020_chip_ops v3020_mmio_ops = {
  84. .map_io = v3020_mmio_map,
  85. .unmap_io = v3020_mmio_unmap,
  86. .read_bit = v3020_mmio_read_bit,
  87. .write_bit = v3020_mmio_write_bit,
  88. };
  89. static struct v3020_gpio v3020_gpio[] = {
  90. { "RTC CS", 0 },
  91. { "RTC WR", 0 },
  92. { "RTC RD", 0 },
  93. { "RTC IO", 0 },
  94. };
  95. static int v3020_gpio_map(struct v3020 *chip, struct platform_device *pdev,
  96. struct v3020_platform_data *pdata)
  97. {
  98. int i, err;
  99. v3020_gpio[V3020_CS].gpio = pdata->gpio_cs;
  100. v3020_gpio[V3020_WR].gpio = pdata->gpio_wr;
  101. v3020_gpio[V3020_RD].gpio = pdata->gpio_rd;
  102. v3020_gpio[V3020_IO].gpio = pdata->gpio_io;
  103. for (i = 0; i < ARRAY_SIZE(v3020_gpio); i++) {
  104. err = gpio_request(v3020_gpio[i].gpio, v3020_gpio[i].name);
  105. if (err)
  106. goto err_request;
  107. gpio_direction_output(v3020_gpio[i].gpio, 1);
  108. }
  109. chip->gpio = v3020_gpio;
  110. return 0;
  111. err_request:
  112. while (--i >= 0)
  113. gpio_free(v3020_gpio[i].gpio);
  114. return err;
  115. }
  116. static void v3020_gpio_unmap(struct v3020 *chip)
  117. {
  118. int i;
  119. for (i = 0; i < ARRAY_SIZE(v3020_gpio); i++)
  120. gpio_free(v3020_gpio[i].gpio);
  121. }
  122. static void v3020_gpio_write_bit(struct v3020 *chip, unsigned char bit)
  123. {
  124. gpio_direction_output(chip->gpio[V3020_IO].gpio, bit);
  125. gpio_set_value(chip->gpio[V3020_CS].gpio, 0);
  126. gpio_set_value(chip->gpio[V3020_WR].gpio, 0);
  127. udelay(1);
  128. gpio_set_value(chip->gpio[V3020_WR].gpio, 1);
  129. gpio_set_value(chip->gpio[V3020_CS].gpio, 1);
  130. }
  131. static unsigned char v3020_gpio_read_bit(struct v3020 *chip)
  132. {
  133. int bit;
  134. gpio_direction_input(chip->gpio[V3020_IO].gpio);
  135. gpio_set_value(chip->gpio[V3020_CS].gpio, 0);
  136. gpio_set_value(chip->gpio[V3020_RD].gpio, 0);
  137. udelay(1);
  138. bit = !!gpio_get_value(chip->gpio[V3020_IO].gpio);
  139. udelay(1);
  140. gpio_set_value(chip->gpio[V3020_RD].gpio, 1);
  141. gpio_set_value(chip->gpio[V3020_CS].gpio, 1);
  142. return bit;
  143. }
  144. static struct v3020_chip_ops v3020_gpio_ops = {
  145. .map_io = v3020_gpio_map,
  146. .unmap_io = v3020_gpio_unmap,
  147. .read_bit = v3020_gpio_read_bit,
  148. .write_bit = v3020_gpio_write_bit,
  149. };
  150. static void v3020_set_reg(struct v3020 *chip, unsigned char address,
  151. unsigned char data)
  152. {
  153. int i;
  154. unsigned char tmp;
  155. tmp = address;
  156. for (i = 0; i < 4; i++) {
  157. chip->ops->write_bit(chip, (tmp & 1));
  158. tmp >>= 1;
  159. udelay(1);
  160. }
  161. /* Commands dont have data */
  162. if (!V3020_IS_COMMAND(address)) {
  163. for (i = 0; i < 8; i++) {
  164. chip->ops->write_bit(chip, (data & 1));
  165. data >>= 1;
  166. udelay(1);
  167. }
  168. }
  169. }
  170. static unsigned char v3020_get_reg(struct v3020 *chip, unsigned char address)
  171. {
  172. unsigned int data = 0;
  173. int i;
  174. for (i = 0; i < 4; i++) {
  175. chip->ops->write_bit(chip, (address & 1));
  176. address >>= 1;
  177. udelay(1);
  178. }
  179. for (i = 0; i < 8; i++) {
  180. data >>= 1;
  181. if (chip->ops->read_bit(chip))
  182. data |= 0x80;
  183. udelay(1);
  184. }
  185. return data;
  186. }
  187. static int v3020_read_time(struct device *dev, struct rtc_time *dt)
  188. {
  189. struct v3020 *chip = dev_get_drvdata(dev);
  190. int tmp;
  191. /* Copy the current time to ram... */
  192. v3020_set_reg(chip, V3020_CMD_CLOCK2RAM, 0);
  193. /* ...and then read constant values. */
  194. tmp = v3020_get_reg(chip, V3020_SECONDS);
  195. dt->tm_sec = bcd2bin(tmp);
  196. tmp = v3020_get_reg(chip, V3020_MINUTES);
  197. dt->tm_min = bcd2bin(tmp);
  198. tmp = v3020_get_reg(chip, V3020_HOURS);
  199. dt->tm_hour = bcd2bin(tmp);
  200. tmp = v3020_get_reg(chip, V3020_MONTH_DAY);
  201. dt->tm_mday = bcd2bin(tmp);
  202. tmp = v3020_get_reg(chip, V3020_MONTH);
  203. dt->tm_mon = bcd2bin(tmp) - 1;
  204. tmp = v3020_get_reg(chip, V3020_WEEK_DAY);
  205. dt->tm_wday = bcd2bin(tmp);
  206. tmp = v3020_get_reg(chip, V3020_YEAR);
  207. dt->tm_year = bcd2bin(tmp)+100;
  208. dev_dbg(dev, "\n%s : Read RTC values\n", __func__);
  209. dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
  210. dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
  211. dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
  212. dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
  213. dev_dbg(dev, "tm_mon : %i\n", dt->tm_mon);
  214. dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
  215. dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
  216. return 0;
  217. }
  218. static int v3020_set_time(struct device *dev, struct rtc_time *dt)
  219. {
  220. struct v3020 *chip = dev_get_drvdata(dev);
  221. dev_dbg(dev, "\n%s : Setting RTC values\n", __func__);
  222. dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
  223. dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
  224. dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
  225. dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
  226. dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
  227. dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
  228. /* Write all the values to ram... */
  229. v3020_set_reg(chip, V3020_SECONDS, bin2bcd(dt->tm_sec));
  230. v3020_set_reg(chip, V3020_MINUTES, bin2bcd(dt->tm_min));
  231. v3020_set_reg(chip, V3020_HOURS, bin2bcd(dt->tm_hour));
  232. v3020_set_reg(chip, V3020_MONTH_DAY, bin2bcd(dt->tm_mday));
  233. v3020_set_reg(chip, V3020_MONTH, bin2bcd(dt->tm_mon + 1));
  234. v3020_set_reg(chip, V3020_WEEK_DAY, bin2bcd(dt->tm_wday));
  235. v3020_set_reg(chip, V3020_YEAR, bin2bcd(dt->tm_year % 100));
  236. /* ...and set the clock. */
  237. v3020_set_reg(chip, V3020_CMD_RAM2CLOCK, 0);
  238. /* Compulab used this delay here. I dont know why,
  239. * the datasheet does not specify a delay. */
  240. /*mdelay(5);*/
  241. return 0;
  242. }
  243. static const struct rtc_class_ops v3020_rtc_ops = {
  244. .read_time = v3020_read_time,
  245. .set_time = v3020_set_time,
  246. };
  247. static int rtc_probe(struct platform_device *pdev)
  248. {
  249. struct v3020_platform_data *pdata = pdev->dev.platform_data;
  250. struct v3020 *chip;
  251. struct rtc_device *rtc;
  252. int retval = -EBUSY;
  253. int i;
  254. int temp;
  255. chip = kzalloc(sizeof *chip, GFP_KERNEL);
  256. if (!chip)
  257. return -ENOMEM;
  258. if (pdata->use_gpio)
  259. chip->ops = &v3020_gpio_ops;
  260. else
  261. chip->ops = &v3020_mmio_ops;
  262. retval = chip->ops->map_io(chip, pdev, pdata);
  263. if (retval)
  264. goto err_chip;
  265. /* Make sure the v3020 expects a communication cycle
  266. * by reading 8 times */
  267. for (i = 0; i < 8; i++)
  268. temp = chip->ops->read_bit(chip);
  269. /* Test chip by doing a write/read sequence
  270. * to the chip ram */
  271. v3020_set_reg(chip, V3020_SECONDS, 0x33);
  272. if (v3020_get_reg(chip, V3020_SECONDS) != 0x33) {
  273. retval = -ENODEV;
  274. goto err_io;
  275. }
  276. /* Make sure frequency measurment mode, test modes, and lock
  277. * are all disabled */
  278. v3020_set_reg(chip, V3020_STATUS_0, 0x0);
  279. if (pdata->use_gpio)
  280. dev_info(&pdev->dev, "Chip available at GPIOs "
  281. "%d, %d, %d, %d\n",
  282. chip->gpio[V3020_CS].gpio, chip->gpio[V3020_WR].gpio,
  283. chip->gpio[V3020_RD].gpio, chip->gpio[V3020_IO].gpio);
  284. else
  285. dev_info(&pdev->dev, "Chip available at "
  286. "physical address 0x%llx,"
  287. "data connected to D%d\n",
  288. (unsigned long long)pdev->resource[0].start,
  289. chip->leftshift);
  290. platform_set_drvdata(pdev, chip);
  291. rtc = rtc_device_register("v3020",
  292. &pdev->dev, &v3020_rtc_ops, THIS_MODULE);
  293. if (IS_ERR(rtc)) {
  294. retval = PTR_ERR(rtc);
  295. goto err_io;
  296. }
  297. chip->rtc = rtc;
  298. return 0;
  299. err_io:
  300. chip->ops->unmap_io(chip);
  301. err_chip:
  302. kfree(chip);
  303. return retval;
  304. }
  305. static int rtc_remove(struct platform_device *dev)
  306. {
  307. struct v3020 *chip = platform_get_drvdata(dev);
  308. struct rtc_device *rtc = chip->rtc;
  309. if (rtc)
  310. rtc_device_unregister(rtc);
  311. chip->ops->unmap_io(chip);
  312. kfree(chip);
  313. return 0;
  314. }
  315. static struct platform_driver rtc_device_driver = {
  316. .probe = rtc_probe,
  317. .remove = rtc_remove,
  318. .driver = {
  319. .name = "v3020",
  320. .owner = THIS_MODULE,
  321. },
  322. };
  323. static __init int v3020_init(void)
  324. {
  325. return platform_driver_register(&rtc_device_driver);
  326. }
  327. static __exit void v3020_exit(void)
  328. {
  329. platform_driver_unregister(&rtc_device_driver);
  330. }
  331. module_init(v3020_init);
  332. module_exit(v3020_exit);
  333. MODULE_DESCRIPTION("V3020 RTC");
  334. MODULE_AUTHOR("Raphael Assenat");
  335. MODULE_LICENSE("GPL");
  336. MODULE_ALIAS("platform:v3020");