rtc-v3020.c 8.9 KB

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