ad5593r.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * AD5593R Digital <-> Analog converters driver
  3. *
  4. * Copyright 2015-2016 Analog Devices Inc.
  5. * Author: Paul Cercueil <paul.cercueil@analog.com>
  6. *
  7. * Licensed under the GPL-2.
  8. */
  9. #include "ad5592r-base.h"
  10. #include <linux/bitops.h>
  11. #include <linux/i2c.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #define AD5593R_MODE_CONF (0 << 4)
  15. #define AD5593R_MODE_DAC_WRITE (1 << 4)
  16. #define AD5593R_MODE_ADC_READBACK (4 << 4)
  17. #define AD5593R_MODE_DAC_READBACK (5 << 4)
  18. #define AD5593R_MODE_GPIO_READBACK (6 << 4)
  19. #define AD5593R_MODE_REG_READBACK (7 << 4)
  20. static int ad5593r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value)
  21. {
  22. struct i2c_client *i2c = to_i2c_client(st->dev);
  23. return i2c_smbus_write_word_swapped(i2c,
  24. AD5593R_MODE_DAC_WRITE | chan, value);
  25. }
  26. static int ad5593r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value)
  27. {
  28. struct i2c_client *i2c = to_i2c_client(st->dev);
  29. s32 val;
  30. val = i2c_smbus_write_word_swapped(i2c,
  31. AD5593R_MODE_CONF | AD5592R_REG_ADC_SEQ, BIT(chan));
  32. if (val < 0)
  33. return (int) val;
  34. val = i2c_smbus_read_word_swapped(i2c, AD5593R_MODE_ADC_READBACK);
  35. if (val < 0)
  36. return (int) val;
  37. *value = (u16) val;
  38. return 0;
  39. }
  40. static int ad5593r_reg_write(struct ad5592r_state *st, u8 reg, u16 value)
  41. {
  42. struct i2c_client *i2c = to_i2c_client(st->dev);
  43. return i2c_smbus_write_word_swapped(i2c,
  44. AD5593R_MODE_CONF | reg, value);
  45. }
  46. static int ad5593r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value)
  47. {
  48. struct i2c_client *i2c = to_i2c_client(st->dev);
  49. s32 val;
  50. val = i2c_smbus_read_word_swapped(i2c, AD5593R_MODE_REG_READBACK | reg);
  51. if (val < 0)
  52. return (int) val;
  53. *value = (u16) val;
  54. return 0;
  55. }
  56. static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value)
  57. {
  58. struct i2c_client *i2c = to_i2c_client(st->dev);
  59. s32 val;
  60. val = i2c_smbus_read_word_swapped(i2c, AD5593R_MODE_GPIO_READBACK);
  61. if (val < 0)
  62. return (int) val;
  63. *value = (u8) val;
  64. return 0;
  65. }
  66. static const struct ad5592r_rw_ops ad5593r_rw_ops = {
  67. .write_dac = ad5593r_write_dac,
  68. .read_adc = ad5593r_read_adc,
  69. .reg_write = ad5593r_reg_write,
  70. .reg_read = ad5593r_reg_read,
  71. .gpio_read = ad5593r_gpio_read,
  72. };
  73. static int ad5593r_i2c_probe(struct i2c_client *i2c,
  74. const struct i2c_device_id *id)
  75. {
  76. return ad5592r_probe(&i2c->dev, id->name, &ad5593r_rw_ops);
  77. }
  78. static int ad5593r_i2c_remove(struct i2c_client *i2c)
  79. {
  80. return ad5592r_remove(&i2c->dev);
  81. }
  82. static const struct i2c_device_id ad5593r_i2c_ids[] = {
  83. { .name = "ad5593r", },
  84. {},
  85. };
  86. MODULE_DEVICE_TABLE(i2c, ad5593r_i2c_ids);
  87. static const struct of_device_id ad5593r_of_match[] = {
  88. { .compatible = "adi,ad5593r", },
  89. {},
  90. };
  91. MODULE_DEVICE_TABLE(of, ad5593r_of_match);
  92. static struct i2c_driver ad5593r_driver = {
  93. .driver = {
  94. .name = "ad5593r",
  95. .of_match_table = of_match_ptr(ad5593r_of_match),
  96. },
  97. .probe = ad5593r_i2c_probe,
  98. .remove = ad5593r_i2c_remove,
  99. .id_table = ad5593r_i2c_ids,
  100. };
  101. module_i2c_driver(ad5593r_driver);
  102. MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>");
  103. MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters");
  104. MODULE_LICENSE("GPL v2");