ad525x_dpot-spi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. {.name = "ad5270", .devid = AD5270_ID},
  39. {.name = "ad5271", .devid = AD5271_ID},
  40. {}
  41. };
  42. /* ------------------------------------------------------------------------- */
  43. /* SPI bus functions */
  44. static int write8(void *client, u8 val)
  45. {
  46. u8 data = val;
  47. return spi_write(client, &data, 1);
  48. }
  49. static int write16(void *client, u8 reg, u8 val)
  50. {
  51. u8 data[2] = {reg, val};
  52. return spi_write(client, data, 2);
  53. }
  54. static int write24(void *client, u8 reg, u16 val)
  55. {
  56. u8 data[3] = {reg, val >> 8, val};
  57. return spi_write(client, data, 3);
  58. }
  59. static int read8(void *client)
  60. {
  61. int ret;
  62. u8 data;
  63. ret = spi_read(client, &data, 1);
  64. if (ret < 0)
  65. return ret;
  66. return data;
  67. }
  68. static int read16(void *client, u8 reg)
  69. {
  70. int ret;
  71. u8 buf_rx[2];
  72. write16(client, reg, 0);
  73. ret = spi_read(client, buf_rx, 2);
  74. if (ret < 0)
  75. return ret;
  76. return (buf_rx[0] << 8) | buf_rx[1];
  77. }
  78. static int read24(void *client, u8 reg)
  79. {
  80. int ret;
  81. u8 buf_rx[3];
  82. write24(client, reg, 0);
  83. ret = spi_read(client, buf_rx, 3);
  84. if (ret < 0)
  85. return ret;
  86. return (buf_rx[1] << 8) | buf_rx[2];
  87. }
  88. static const struct ad_dpot_bus_ops bops = {
  89. .read_d8 = read8,
  90. .read_r8d8 = read16,
  91. .read_r8d16 = read24,
  92. .write_d8 = write8,
  93. .write_r8d8 = write16,
  94. .write_r8d16 = write24,
  95. };
  96. static const struct ad_dpot_id *dpot_match_id(const struct ad_dpot_id *id,
  97. char *name)
  98. {
  99. while (id->name && id->name[0]) {
  100. if (strcmp(name, id->name) == 0)
  101. return id;
  102. id++;
  103. }
  104. return NULL;
  105. }
  106. static int __devinit ad_dpot_spi_probe(struct spi_device *spi)
  107. {
  108. char *name = spi->dev.platform_data;
  109. const struct ad_dpot_id *dpot_id;
  110. struct ad_dpot_bus_data bdata = {
  111. .client = spi,
  112. .bops = &bops,
  113. };
  114. dpot_id = dpot_match_id(ad_dpot_spi_devlist, name);
  115. if (dpot_id == NULL) {
  116. dev_err(&spi->dev, "%s not in supported device list", name);
  117. return -ENODEV;
  118. }
  119. return ad_dpot_probe(&spi->dev, &bdata, dpot_id);
  120. }
  121. static int __devexit ad_dpot_spi_remove(struct spi_device *spi)
  122. {
  123. return ad_dpot_remove(&spi->dev);
  124. }
  125. static struct spi_driver ad_dpot_spi_driver = {
  126. .driver = {
  127. .name = "ad_dpot",
  128. .bus = &spi_bus_type,
  129. .owner = THIS_MODULE,
  130. },
  131. .probe = ad_dpot_spi_probe,
  132. .remove = __devexit_p(ad_dpot_spi_remove),
  133. };
  134. static int __init ad_dpot_spi_init(void)
  135. {
  136. return spi_register_driver(&ad_dpot_spi_driver);
  137. }
  138. module_init(ad_dpot_spi_init);
  139. static void __exit ad_dpot_spi_exit(void)
  140. {
  141. spi_unregister_driver(&ad_dpot_spi_driver);
  142. }
  143. module_exit(ad_dpot_spi_exit);
  144. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  145. MODULE_DESCRIPTION("digital potentiometer SPI bus driver");
  146. MODULE_LICENSE("GPL");
  147. MODULE_ALIAS("spi:ad_dpot");