mxc_w1.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. struct clk *clk;
  45. struct w1_bus_master bus_master;
  46. };
  47. /*
  48. * this is the low level routine to
  49. * reset the device on the One Wire interface
  50. * on the hardware
  51. */
  52. static u8 mxc_w1_ds2_reset_bus(void *data)
  53. {
  54. u8 reg_val;
  55. unsigned int timeout_cnt = 0;
  56. struct mxc_w1_device *dev = data;
  57. __raw_writeb(0x80, (dev->regs + MXC_W1_CONTROL));
  58. while (1) {
  59. reg_val = __raw_readb(dev->regs + MXC_W1_CONTROL);
  60. if (((reg_val >> 7) & 0x1) == 0 ||
  61. timeout_cnt > MXC_W1_RESET_TIMEOUT)
  62. break;
  63. else
  64. timeout_cnt++;
  65. udelay(100);
  66. }
  67. return (reg_val >> 7) & 0x1;
  68. }
  69. /*
  70. * this is the low level routine to read/write a bit on the One Wire
  71. * interface on the hardware. It does write 0 if parameter bit is set
  72. * to 0, otherwise a write 1/read.
  73. */
  74. static u8 mxc_w1_ds2_touch_bit(void *data, u8 bit)
  75. {
  76. struct mxc_w1_device *mdev = data;
  77. void __iomem *ctrl_addr = mdev->regs + MXC_W1_CONTROL;
  78. unsigned int timeout_cnt = 400; /* Takes max. 120us according to
  79. * datasheet.
  80. */
  81. __raw_writeb((1 << (5 - bit)), ctrl_addr);
  82. while (timeout_cnt--) {
  83. if (!((__raw_readb(ctrl_addr) >> (5 - bit)) & 0x1))
  84. break;
  85. udelay(1);
  86. }
  87. return ((__raw_readb(ctrl_addr)) >> 3) & 0x1;
  88. }
  89. static int mxc_w1_probe(struct platform_device *pdev)
  90. {
  91. struct mxc_w1_device *mdev;
  92. unsigned long clkrate;
  93. struct resource *res;
  94. unsigned int clkdiv;
  95. int err;
  96. mdev = devm_kzalloc(&pdev->dev, sizeof(struct mxc_w1_device),
  97. GFP_KERNEL);
  98. if (!mdev)
  99. return -ENOMEM;
  100. mdev->clk = devm_clk_get(&pdev->dev, NULL);
  101. if (IS_ERR(mdev->clk))
  102. return PTR_ERR(mdev->clk);
  103. clkrate = clk_get_rate(mdev->clk);
  104. if (clkrate < 10000000)
  105. dev_warn(&pdev->dev,
  106. "Low clock frequency causes improper function\n");
  107. clkdiv = DIV_ROUND_CLOSEST(clkrate, 1000000);
  108. clkrate /= clkdiv;
  109. if ((clkrate < 980000) || (clkrate > 1020000))
  110. dev_warn(&pdev->dev,
  111. "Incorrect time base frequency %lu Hz\n", clkrate);
  112. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  113. mdev->regs = devm_ioremap_resource(&pdev->dev, res);
  114. if (IS_ERR(mdev->regs))
  115. return PTR_ERR(mdev->regs);
  116. err = clk_prepare_enable(mdev->clk);
  117. if (err)
  118. return err;
  119. __raw_writeb(clkdiv - 1, 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. platform_set_drvdata(pdev, mdev);
  124. err = w1_add_master_device(&mdev->bus_master);
  125. if (err)
  126. clk_disable_unprepare(mdev->clk);
  127. return err;
  128. }
  129. /*
  130. * disassociate the w1 device from the driver
  131. */
  132. static int mxc_w1_remove(struct platform_device *pdev)
  133. {
  134. struct mxc_w1_device *mdev = platform_get_drvdata(pdev);
  135. w1_remove_master_device(&mdev->bus_master);
  136. clk_disable_unprepare(mdev->clk);
  137. return 0;
  138. }
  139. static struct of_device_id mxc_w1_dt_ids[] = {
  140. { .compatible = "fsl,imx21-owire" },
  141. { /* sentinel */ }
  142. };
  143. MODULE_DEVICE_TABLE(of, mxc_w1_dt_ids);
  144. static struct platform_driver mxc_w1_driver = {
  145. .driver = {
  146. .name = "mxc_w1",
  147. .of_match_table = mxc_w1_dt_ids,
  148. },
  149. .probe = mxc_w1_probe,
  150. .remove = mxc_w1_remove,
  151. };
  152. module_platform_driver(mxc_w1_driver);
  153. MODULE_LICENSE("GPL");
  154. MODULE_AUTHOR("Freescale Semiconductors Inc");
  155. MODULE_DESCRIPTION("Driver for One-Wire on MXC");