sgi_w1.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sgi_w1.c - w1 master driver for one wire support in SGI ASICs
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/io.h>
  8. #include <linux/jiffies.h>
  9. #include <linux/module.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/platform_data/sgi-w1.h>
  13. #include <linux/w1.h>
  14. #define MCR_RD_DATA BIT(0)
  15. #define MCR_DONE BIT(1)
  16. #define MCR_PACK(pulse, sample) (((pulse) << 10) | ((sample) << 2))
  17. struct sgi_w1_device {
  18. u32 __iomem *mcr;
  19. struct w1_bus_master bus_master;
  20. char dev_id[64];
  21. };
  22. static u8 sgi_w1_wait(u32 __iomem *mcr)
  23. {
  24. u32 mcr_val;
  25. do {
  26. mcr_val = readl(mcr);
  27. } while (!(mcr_val & MCR_DONE));
  28. return (mcr_val & MCR_RD_DATA) ? 1 : 0;
  29. }
  30. /*
  31. * this is the low level routine to
  32. * reset the device on the One Wire interface
  33. * on the hardware
  34. */
  35. static u8 sgi_w1_reset_bus(void *data)
  36. {
  37. struct sgi_w1_device *dev = data;
  38. u8 ret;
  39. writel(MCR_PACK(520, 65), dev->mcr);
  40. ret = sgi_w1_wait(dev->mcr);
  41. udelay(500); /* recovery time */
  42. return ret;
  43. }
  44. /*
  45. * this is the low level routine to read/write a bit on the One Wire
  46. * interface on the hardware. It does write 0 if parameter bit is set
  47. * to 0, otherwise a write 1/read.
  48. */
  49. static u8 sgi_w1_touch_bit(void *data, u8 bit)
  50. {
  51. struct sgi_w1_device *dev = data;
  52. u8 ret;
  53. if (bit)
  54. writel(MCR_PACK(6, 13), dev->mcr);
  55. else
  56. writel(MCR_PACK(80, 30), dev->mcr);
  57. ret = sgi_w1_wait(dev->mcr);
  58. if (bit)
  59. udelay(100); /* recovery */
  60. return ret;
  61. }
  62. static int sgi_w1_probe(struct platform_device *pdev)
  63. {
  64. struct sgi_w1_device *sdev;
  65. struct sgi_w1_platform_data *pdata;
  66. sdev = devm_kzalloc(&pdev->dev, sizeof(struct sgi_w1_device),
  67. GFP_KERNEL);
  68. if (!sdev)
  69. return -ENOMEM;
  70. sdev->mcr = devm_platform_ioremap_resource(pdev, 0);
  71. if (IS_ERR(sdev->mcr))
  72. return PTR_ERR(sdev->mcr);
  73. sdev->bus_master.data = sdev;
  74. sdev->bus_master.reset_bus = sgi_w1_reset_bus;
  75. sdev->bus_master.touch_bit = sgi_w1_touch_bit;
  76. pdata = dev_get_platdata(&pdev->dev);
  77. if (pdata) {
  78. strlcpy(sdev->dev_id, pdata->dev_id, sizeof(sdev->dev_id));
  79. sdev->bus_master.dev_id = sdev->dev_id;
  80. }
  81. platform_set_drvdata(pdev, sdev);
  82. return w1_add_master_device(&sdev->bus_master);
  83. }
  84. /*
  85. * disassociate the w1 device from the driver
  86. */
  87. static int sgi_w1_remove(struct platform_device *pdev)
  88. {
  89. struct sgi_w1_device *sdev = platform_get_drvdata(pdev);
  90. w1_remove_master_device(&sdev->bus_master);
  91. return 0;
  92. }
  93. static struct platform_driver sgi_w1_driver = {
  94. .driver = {
  95. .name = "sgi_w1",
  96. },
  97. .probe = sgi_w1_probe,
  98. .remove = sgi_w1_remove,
  99. };
  100. module_platform_driver(sgi_w1_driver);
  101. MODULE_LICENSE("GPL");
  102. MODULE_AUTHOR("Thomas Bogendoerfer");
  103. MODULE_DESCRIPTION("Driver for One-Wire IP in SGI ASICs");