mxc_w1.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 2005-2008 Freescale Semiconductor, Inc. All Rights Reserved.
  3. * Copyright 2008 Luotao Fu, kernel@pengutronix.de
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/clk.h>
  23. #include <linux/slab.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include "../w1.h"
  27. #include "../w1_int.h"
  28. #include "../w1_log.h"
  29. /* According to the mx27 Datasheet the reset procedure should take up to about
  30. * 1350us. We set the timeout to 500*100us = 50ms for sure */
  31. #define MXC_W1_RESET_TIMEOUT 500
  32. /*
  33. * MXC W1 Register offsets
  34. */
  35. #define MXC_W1_CONTROL 0x00
  36. #define MXC_W1_TIME_DIVIDER 0x02
  37. #define MXC_W1_RESET 0x04
  38. #define MXC_W1_COMMAND 0x06
  39. #define MXC_W1_TXRX 0x08
  40. #define MXC_W1_INTERRUPT 0x0A
  41. #define MXC_W1_INTERRUPT_EN 0x0C
  42. struct mxc_w1_device {
  43. void __iomem *regs;
  44. unsigned int clkdiv;
  45. struct clk *clk;
  46. struct w1_bus_master bus_master;
  47. };
  48. /*
  49. * this is the low level routine to
  50. * reset the device on the One Wire interface
  51. * on the hardware
  52. */
  53. static u8 mxc_w1_ds2_reset_bus(void *data)
  54. {
  55. u8 reg_val;
  56. unsigned int timeout_cnt = 0;
  57. struct mxc_w1_device *dev = data;
  58. __raw_writeb(0x80, (dev->regs + MXC_W1_CONTROL));
  59. while (1) {
  60. reg_val = __raw_readb(dev->regs + MXC_W1_CONTROL);
  61. if (((reg_val >> 7) & 0x1) == 0 ||
  62. timeout_cnt > MXC_W1_RESET_TIMEOUT)
  63. break;
  64. else
  65. timeout_cnt++;
  66. udelay(100);
  67. }
  68. return (reg_val >> 7) & 0x1;
  69. }
  70. /*
  71. * this is the low level routine to read/write a bit on the One Wire
  72. * interface on the hardware. It does write 0 if parameter bit is set
  73. * to 0, otherwise a write 1/read.
  74. */
  75. static u8 mxc_w1_ds2_touch_bit(void *data, u8 bit)
  76. {
  77. struct mxc_w1_device *mdev = data;
  78. void __iomem *ctrl_addr = mdev->regs + MXC_W1_CONTROL;
  79. unsigned int timeout_cnt = 400; /* Takes max. 120us according to
  80. * datasheet.
  81. */
  82. __raw_writeb((1 << (5 - bit)), ctrl_addr);
  83. while (timeout_cnt--) {
  84. if (!((__raw_readb(ctrl_addr) >> (5 - bit)) & 0x1))
  85. break;
  86. udelay(1);
  87. }
  88. return ((__raw_readb(ctrl_addr)) >> 3) & 0x1;
  89. }
  90. static int __devinit mxc_w1_probe(struct platform_device *pdev)
  91. {
  92. struct mxc_w1_device *mdev;
  93. struct resource *res;
  94. int err = 0;
  95. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  96. if (!res)
  97. return -ENODEV;
  98. mdev = kzalloc(sizeof(struct mxc_w1_device), GFP_KERNEL);
  99. if (!mdev)
  100. return -ENOMEM;
  101. mdev->clk = clk_get(&pdev->dev, "owire");
  102. if (!mdev->clk) {
  103. err = -ENODEV;
  104. goto failed_clk;
  105. }
  106. mdev->clkdiv = (clk_get_rate(mdev->clk) / 1000000) - 1;
  107. res = request_mem_region(res->start, resource_size(res),
  108. "mxc_w1");
  109. if (!res) {
  110. err = -EBUSY;
  111. goto failed_req;
  112. }
  113. mdev->regs = ioremap(res->start, resource_size(res));
  114. if (!mdev->regs) {
  115. printk(KERN_ERR "Cannot map frame buffer registers\n");
  116. goto failed_ioremap;
  117. }
  118. clk_enable(mdev->clk);
  119. __raw_writeb(mdev->clkdiv, mdev->regs + MXC_W1_TIME_DIVIDER);
  120. mdev->bus_master.data = mdev;
  121. mdev->bus_master.reset_bus = mxc_w1_ds2_reset_bus;
  122. mdev->bus_master.touch_bit = mxc_w1_ds2_touch_bit;
  123. err = w1_add_master_device(&mdev->bus_master);
  124. if (err)
  125. goto failed_add;
  126. platform_set_drvdata(pdev, mdev);
  127. return 0;
  128. failed_add:
  129. iounmap(mdev->regs);
  130. failed_ioremap:
  131. release_mem_region(res->start, resource_size(res));
  132. failed_req:
  133. clk_put(mdev->clk);
  134. failed_clk:
  135. kfree(mdev);
  136. return err;
  137. }
  138. /*
  139. * disassociate the w1 device from the driver
  140. */
  141. static int __devexit mxc_w1_remove(struct platform_device *pdev)
  142. {
  143. struct mxc_w1_device *mdev = platform_get_drvdata(pdev);
  144. struct resource *res;
  145. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  146. w1_remove_master_device(&mdev->bus_master);
  147. iounmap(mdev->regs);
  148. release_mem_region(res->start, resource_size(res));
  149. clk_disable(mdev->clk);
  150. clk_put(mdev->clk);
  151. platform_set_drvdata(pdev, NULL);
  152. return 0;
  153. }
  154. static struct platform_driver mxc_w1_driver = {
  155. .driver = {
  156. .name = "mxc_w1",
  157. },
  158. .probe = mxc_w1_probe,
  159. .remove = mxc_w1_remove,
  160. };
  161. static int __init mxc_w1_init(void)
  162. {
  163. return platform_driver_register(&mxc_w1_driver);
  164. }
  165. static void mxc_w1_exit(void)
  166. {
  167. platform_driver_unregister(&mxc_w1_driver);
  168. }
  169. module_init(mxc_w1_init);
  170. module_exit(mxc_w1_exit);
  171. MODULE_LICENSE("GPL");
  172. MODULE_AUTHOR("Freescale Semiconductors Inc");
  173. MODULE_DESCRIPTION("Driver for One-Wire on MXC");