ad525x_dpot-spi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Driver for the Analog Devices digital potentiometers (SPI bus)
  3. *
  4. * Copyright (C) 2010 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. static const struct ad_dpot_id ad_dpot_spi_devlist[] = {
  12. {.name = "ad5160", .devid = AD5160_ID},
  13. {.name = "ad5161", .devid = AD5161_ID},
  14. {.name = "ad5162", .devid = AD5162_ID},
  15. {.name = "ad5165", .devid = AD5165_ID},
  16. {.name = "ad5200", .devid = AD5200_ID},
  17. {.name = "ad5201", .devid = AD5201_ID},
  18. {.name = "ad5203", .devid = AD5203_ID},
  19. {.name = "ad5204", .devid = AD5204_ID},
  20. {.name = "ad5206", .devid = AD5206_ID},
  21. {.name = "ad5207", .devid = AD5207_ID},
  22. {.name = "ad5231", .devid = AD5231_ID},
  23. {.name = "ad5232", .devid = AD5232_ID},
  24. {.name = "ad5233", .devid = AD5233_ID},
  25. {.name = "ad5235", .devid = AD5235_ID},
  26. {.name = "ad5260", .devid = AD5260_ID},
  27. {.name = "ad5262", .devid = AD5262_ID},
  28. {.name = "ad5263", .devid = AD5263_ID},
  29. {.name = "ad5290", .devid = AD5290_ID},
  30. {.name = "ad5291", .devid = AD5291_ID},
  31. {.name = "ad5292", .devid = AD5292_ID},
  32. {.name = "ad5293", .devid = AD5293_ID},
  33. {.name = "ad7376", .devid = AD7376_ID},
  34. {.name = "ad8400", .devid = AD8400_ID},
  35. {.name = "ad8402", .devid = AD8402_ID},
  36. {.name = "ad8403", .devid = AD8403_ID},
  37. {.name = "adn2850", .devid = ADN2850_ID},
  38. {}
  39. };
  40. /* ------------------------------------------------------------------------- */
  41. /* SPI bus functions */
  42. static int write8(void *client, u8 val)
  43. {
  44. u8 data = val;
  45. return spi_write(client, &data, 1);
  46. }
  47. static int write16(void *client, u8 reg, u8 val)
  48. {
  49. u8 data[2] = {reg, val};
  50. return spi_write(client, data, 1);
  51. }
  52. static int write24(void *client, u8 reg, u16 val)
  53. {
  54. u8 data[3] = {reg, val >> 8, val};
  55. return spi_write(client, data, 1);
  56. }
  57. static int read8(void *client)
  58. {
  59. int ret;
  60. u8 data;
  61. ret = spi_read(client, &data, 1);
  62. if (ret < 0)
  63. return ret;
  64. return data;
  65. }
  66. static int read16(void *client, u8 reg)
  67. {
  68. int ret;
  69. u8 buf_rx[2];
  70. write16(client, reg, 0);
  71. ret = spi_read(client, buf_rx, 2);
  72. if (ret < 0)
  73. return ret;
  74. return (buf_rx[0] << 8) | buf_rx[1];
  75. }
  76. static int read24(void *client, u8 reg)
  77. {
  78. int ret;
  79. u8 buf_rx[3];
  80. write24(client, reg, 0);
  81. ret = spi_read(client, buf_rx, 3);
  82. if (ret < 0)
  83. return ret;
  84. return (buf_rx[1] << 8) | buf_rx[2];
  85. }
  86. static const struct ad_dpot_bus_ops bops = {
  87. .read_d8 = read8,
  88. .read_r8d8 = read16,
  89. .read_r8d16 = read24,
  90. .write_d8 = write8,
  91. .write_r8d8 = write16,
  92. .write_r8d16 = write24,
  93. };
  94. static const struct ad_dpot_id *dpot_match_id(const struct ad_dpot_id *id,
  95. char *name)
  96. {
  97. while (id->name && id->name[0]) {
  98. if (strcmp(name, id->name) == 0)
  99. return id;
  100. id++;
  101. }
  102. return NULL;
  103. }
  104. static int __devinit ad_dpot_spi_probe(struct spi_device *spi)
  105. {
  106. char *name = spi->dev.platform_data;
  107. const struct ad_dpot_id *dpot_id;
  108. struct ad_dpot_bus_data bdata = {
  109. .client = spi,
  110. .bops = &bops,
  111. };
  112. dpot_id = dpot_match_id(ad_dpot_spi_devlist, name);
  113. if (dpot_id == NULL) {
  114. dev_err(&spi->dev, "%s not in supported device list", name);
  115. return -ENODEV;
  116. }
  117. return ad_dpot_probe(&spi->dev, &bdata, dpot_id);
  118. }
  119. static int __devexit ad_dpot_spi_remove(struct spi_device *spi)
  120. {
  121. return ad_dpot_remove(&spi->dev);
  122. }
  123. static struct spi_driver ad_dpot_spi_driver = {
  124. .driver = {
  125. .name = "ad_dpot",
  126. .bus = &spi_bus_type,
  127. .owner = THIS_MODULE,
  128. },
  129. .probe = ad_dpot_spi_probe,
  130. .remove = __devexit_p(ad_dpot_spi_remove),
  131. };
  132. static int __init ad_dpot_spi_init(void)
  133. {
  134. return spi_register_driver(&ad_dpot_spi_driver);
  135. }
  136. module_init(ad_dpot_spi_init);
  137. static void __exit ad_dpot_spi_exit(void)
  138. {
  139. spi_unregister_driver(&ad_dpot_spi_driver);
  140. }
  141. module_exit(ad_dpot_spi_exit);
  142. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  143. MODULE_DESCRIPTION("digital potentiometer SPI bus driver");
  144. MODULE_LICENSE("GPL");
  145. MODULE_ALIAS("spi:ad_dpot");