ad525x_dpot-spi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Driver for the Analog Devices digital potentiometers (SPI bus)
  3. *
  4. * Copyright (C) 2010-2011 Michael Hennerich, Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/spi/spi.h>
  9. #include <linux/module.h>
  10. #include "ad525x_dpot.h"
  11. /* SPI bus functions */
  12. static int write8(void *client, u8 val)
  13. {
  14. u8 data = val;
  15. return spi_write(client, &data, 1);
  16. }
  17. static int write16(void *client, u8 reg, u8 val)
  18. {
  19. u8 data[2] = {reg, val};
  20. return spi_write(client, data, 2);
  21. }
  22. static int write24(void *client, u8 reg, u16 val)
  23. {
  24. u8 data[3] = {reg, val >> 8, val};
  25. return spi_write(client, data, 3);
  26. }
  27. static int read8(void *client)
  28. {
  29. int ret;
  30. u8 data;
  31. ret = spi_read(client, &data, 1);
  32. if (ret < 0)
  33. return ret;
  34. return data;
  35. }
  36. static int read16(void *client, u8 reg)
  37. {
  38. int ret;
  39. u8 buf_rx[2];
  40. write16(client, reg, 0);
  41. ret = spi_read(client, buf_rx, 2);
  42. if (ret < 0)
  43. return ret;
  44. return (buf_rx[0] << 8) | buf_rx[1];
  45. }
  46. static int read24(void *client, u8 reg)
  47. {
  48. int ret;
  49. u8 buf_rx[3];
  50. write24(client, reg, 0);
  51. ret = spi_read(client, buf_rx, 3);
  52. if (ret < 0)
  53. return ret;
  54. return (buf_rx[1] << 8) | buf_rx[2];
  55. }
  56. static const struct ad_dpot_bus_ops bops = {
  57. .read_d8 = read8,
  58. .read_r8d8 = read16,
  59. .read_r8d16 = read24,
  60. .write_d8 = write8,
  61. .write_r8d8 = write16,
  62. .write_r8d16 = write24,
  63. };
  64. static int ad_dpot_spi_probe(struct spi_device *spi)
  65. {
  66. struct ad_dpot_bus_data bdata = {
  67. .client = spi,
  68. .bops = &bops,
  69. };
  70. return ad_dpot_probe(&spi->dev, &bdata,
  71. spi_get_device_id(spi)->driver_data,
  72. spi_get_device_id(spi)->name);
  73. }
  74. static int ad_dpot_spi_remove(struct spi_device *spi)
  75. {
  76. return ad_dpot_remove(&spi->dev);
  77. }
  78. static const struct spi_device_id ad_dpot_spi_id[] = {
  79. {"ad5160", AD5160_ID},
  80. {"ad5161", AD5161_ID},
  81. {"ad5162", AD5162_ID},
  82. {"ad5165", AD5165_ID},
  83. {"ad5200", AD5200_ID},
  84. {"ad5201", AD5201_ID},
  85. {"ad5203", AD5203_ID},
  86. {"ad5204", AD5204_ID},
  87. {"ad5206", AD5206_ID},
  88. {"ad5207", AD5207_ID},
  89. {"ad5231", AD5231_ID},
  90. {"ad5232", AD5232_ID},
  91. {"ad5233", AD5233_ID},
  92. {"ad5235", AD5235_ID},
  93. {"ad5260", AD5260_ID},
  94. {"ad5262", AD5262_ID},
  95. {"ad5263", AD5263_ID},
  96. {"ad5290", AD5290_ID},
  97. {"ad5291", AD5291_ID},
  98. {"ad5292", AD5292_ID},
  99. {"ad5293", AD5293_ID},
  100. {"ad7376", AD7376_ID},
  101. {"ad8400", AD8400_ID},
  102. {"ad8402", AD8402_ID},
  103. {"ad8403", AD8403_ID},
  104. {"adn2850", ADN2850_ID},
  105. {"ad5270", AD5270_ID},
  106. {"ad5271", AD5271_ID},
  107. {}
  108. };
  109. MODULE_DEVICE_TABLE(spi, ad_dpot_spi_id);
  110. static struct spi_driver ad_dpot_spi_driver = {
  111. .driver = {
  112. .name = "ad_dpot",
  113. },
  114. .probe = ad_dpot_spi_probe,
  115. .remove = ad_dpot_spi_remove,
  116. .id_table = ad_dpot_spi_id,
  117. };
  118. module_spi_driver(ad_dpot_spi_driver);
  119. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  120. MODULE_DESCRIPTION("digital potentiometer SPI bus driver");
  121. MODULE_LICENSE("GPL");
  122. MODULE_ALIAS("spi:ad_dpot");