mxc_w1.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/io.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include "../w1.h"
  21. #include "../w1_int.h"
  22. /*
  23. * MXC W1 Register offsets
  24. */
  25. #define MXC_W1_CONTROL 0x00
  26. # define MXC_W1_CONTROL_RDST BIT(3)
  27. # define MXC_W1_CONTROL_WR(x) BIT(5 - (x))
  28. # define MXC_W1_CONTROL_PST BIT(6)
  29. # define MXC_W1_CONTROL_RPP BIT(7)
  30. #define MXC_W1_TIME_DIVIDER 0x02
  31. #define MXC_W1_RESET 0x04
  32. # define MXC_W1_RESET_RST BIT(0)
  33. struct mxc_w1_device {
  34. void __iomem *regs;
  35. struct clk *clk;
  36. struct w1_bus_master bus_master;
  37. };
  38. /*
  39. * this is the low level routine to
  40. * reset the device on the One Wire interface
  41. * on the hardware
  42. */
  43. static u8 mxc_w1_ds2_reset_bus(void *data)
  44. {
  45. struct mxc_w1_device *dev = data;
  46. unsigned long timeout;
  47. writeb(MXC_W1_CONTROL_RPP, dev->regs + MXC_W1_CONTROL);
  48. /* Wait for reset sequence 511+512us, use 1500us for sure */
  49. timeout = jiffies + usecs_to_jiffies(1500);
  50. udelay(511 + 512);
  51. do {
  52. u8 ctrl = readb(dev->regs + MXC_W1_CONTROL);
  53. /* PST bit is valid after the RPP bit is self-cleared */
  54. if (!(ctrl & MXC_W1_CONTROL_RPP))
  55. return !(ctrl & MXC_W1_CONTROL_PST);
  56. } while (time_is_after_jiffies(timeout));
  57. return 1;
  58. }
  59. /*
  60. * this is the low level routine to read/write a bit on the One Wire
  61. * interface on the hardware. It does write 0 if parameter bit is set
  62. * to 0, otherwise a write 1/read.
  63. */
  64. static u8 mxc_w1_ds2_touch_bit(void *data, u8 bit)
  65. {
  66. struct mxc_w1_device *dev = data;
  67. unsigned long timeout;
  68. writeb(MXC_W1_CONTROL_WR(bit), dev->regs + MXC_W1_CONTROL);
  69. /* Wait for read/write bit (60us, Max 120us), use 200us for sure */
  70. timeout = jiffies + usecs_to_jiffies(200);
  71. udelay(60);
  72. do {
  73. u8 ctrl = readb(dev->regs + MXC_W1_CONTROL);
  74. /* RDST bit is valid after the WR1/RD bit is self-cleared */
  75. if (!(ctrl & MXC_W1_CONTROL_WR(bit)))
  76. return !!(ctrl & MXC_W1_CONTROL_RDST);
  77. } while (time_is_after_jiffies(timeout));
  78. return 0;
  79. }
  80. static int mxc_w1_probe(struct platform_device *pdev)
  81. {
  82. struct mxc_w1_device *mdev;
  83. unsigned long clkrate;
  84. struct resource *res;
  85. unsigned int clkdiv;
  86. int err;
  87. mdev = devm_kzalloc(&pdev->dev, sizeof(struct mxc_w1_device),
  88. GFP_KERNEL);
  89. if (!mdev)
  90. return -ENOMEM;
  91. mdev->clk = devm_clk_get(&pdev->dev, NULL);
  92. if (IS_ERR(mdev->clk))
  93. return PTR_ERR(mdev->clk);
  94. err = clk_prepare_enable(mdev->clk);
  95. if (err)
  96. return err;
  97. clkrate = clk_get_rate(mdev->clk);
  98. if (clkrate < 10000000)
  99. dev_warn(&pdev->dev,
  100. "Low clock frequency causes improper function\n");
  101. clkdiv = DIV_ROUND_CLOSEST(clkrate, 1000000);
  102. clkrate /= clkdiv;
  103. if ((clkrate < 980000) || (clkrate > 1020000))
  104. dev_warn(&pdev->dev,
  105. "Incorrect time base frequency %lu Hz\n", clkrate);
  106. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  107. mdev->regs = devm_ioremap_resource(&pdev->dev, res);
  108. if (IS_ERR(mdev->regs)) {
  109. err = PTR_ERR(mdev->regs);
  110. goto out_disable_clk;
  111. }
  112. /* Software reset 1-Wire module */
  113. writeb(MXC_W1_RESET_RST, mdev->regs + MXC_W1_RESET);
  114. writeb(0, mdev->regs + MXC_W1_RESET);
  115. writeb(clkdiv - 1, mdev->regs + MXC_W1_TIME_DIVIDER);
  116. mdev->bus_master.data = mdev;
  117. mdev->bus_master.reset_bus = mxc_w1_ds2_reset_bus;
  118. mdev->bus_master.touch_bit = mxc_w1_ds2_touch_bit;
  119. platform_set_drvdata(pdev, mdev);
  120. err = w1_add_master_device(&mdev->bus_master);
  121. if (err)
  122. goto out_disable_clk;
  123. return 0;
  124. out_disable_clk:
  125. clk_disable_unprepare(mdev->clk);
  126. return err;
  127. }
  128. /*
  129. * disassociate the w1 device from the driver
  130. */
  131. static int mxc_w1_remove(struct platform_device *pdev)
  132. {
  133. struct mxc_w1_device *mdev = platform_get_drvdata(pdev);
  134. w1_remove_master_device(&mdev->bus_master);
  135. clk_disable_unprepare(mdev->clk);
  136. return 0;
  137. }
  138. static const struct of_device_id mxc_w1_dt_ids[] = {
  139. { .compatible = "fsl,imx21-owire" },
  140. { /* sentinel */ }
  141. };
  142. MODULE_DEVICE_TABLE(of, mxc_w1_dt_ids);
  143. static struct platform_driver mxc_w1_driver = {
  144. .driver = {
  145. .name = "mxc_w1",
  146. .of_match_table = mxc_w1_dt_ids,
  147. },
  148. .probe = mxc_w1_probe,
  149. .remove = mxc_w1_remove,
  150. };
  151. module_platform_driver(mxc_w1_driver);
  152. MODULE_LICENSE("GPL");
  153. MODULE_AUTHOR("Freescale Semiconductors Inc");
  154. MODULE_DESCRIPTION("Driver for One-Wire on MXC");