max517.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * max517.c - Support for Maxim MAX517, MAX518 and MAX519
  4. *
  5. * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/jiffies.h>
  10. #include <linux/i2c.h>
  11. #include <linux/err.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/iio/sysfs.h>
  14. #include <linux/iio/dac/max517.h>
  15. #define MAX517_DRV_NAME "max517"
  16. /* Commands */
  17. #define COMMAND_CHANNEL0 0x00
  18. #define COMMAND_CHANNEL1 0x01 /* for MAX518 and MAX519 */
  19. #define COMMAND_PD 0x08 /* Power Down */
  20. enum max517_device_ids {
  21. ID_MAX517,
  22. ID_MAX518,
  23. ID_MAX519,
  24. ID_MAX520,
  25. ID_MAX521,
  26. };
  27. struct max517_data {
  28. struct i2c_client *client;
  29. unsigned short vref_mv[8];
  30. };
  31. /*
  32. * channel: bit 0: channel 1
  33. * bit 1: channel 2
  34. * (this way, it's possible to set both channels at once)
  35. */
  36. static int max517_set_value(struct iio_dev *indio_dev,
  37. long val, int channel)
  38. {
  39. struct max517_data *data = iio_priv(indio_dev);
  40. struct i2c_client *client = data->client;
  41. u8 outbuf[2];
  42. int res;
  43. if (val < 0 || val > 255)
  44. return -EINVAL;
  45. outbuf[0] = channel;
  46. outbuf[1] = val;
  47. res = i2c_master_send(client, outbuf, 2);
  48. if (res < 0)
  49. return res;
  50. else if (res != 2)
  51. return -EIO;
  52. else
  53. return 0;
  54. }
  55. static int max517_read_raw(struct iio_dev *indio_dev,
  56. struct iio_chan_spec const *chan,
  57. int *val,
  58. int *val2,
  59. long m)
  60. {
  61. struct max517_data *data = iio_priv(indio_dev);
  62. switch (m) {
  63. case IIO_CHAN_INFO_SCALE:
  64. /* Corresponds to Vref / 2^(bits) */
  65. *val = data->vref_mv[chan->channel];
  66. *val2 = 8;
  67. return IIO_VAL_FRACTIONAL_LOG2;
  68. default:
  69. break;
  70. }
  71. return -EINVAL;
  72. }
  73. static int max517_write_raw(struct iio_dev *indio_dev,
  74. struct iio_chan_spec const *chan, int val, int val2, long mask)
  75. {
  76. int ret;
  77. switch (mask) {
  78. case IIO_CHAN_INFO_RAW:
  79. ret = max517_set_value(indio_dev, val, chan->channel);
  80. break;
  81. default:
  82. ret = -EINVAL;
  83. break;
  84. }
  85. return ret;
  86. }
  87. static int __maybe_unused max517_suspend(struct device *dev)
  88. {
  89. u8 outbuf = COMMAND_PD;
  90. return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
  91. }
  92. static int __maybe_unused max517_resume(struct device *dev)
  93. {
  94. u8 outbuf = 0;
  95. return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
  96. }
  97. static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
  98. static const struct iio_info max517_info = {
  99. .read_raw = max517_read_raw,
  100. .write_raw = max517_write_raw,
  101. };
  102. #define MAX517_CHANNEL(chan) { \
  103. .type = IIO_VOLTAGE, \
  104. .indexed = 1, \
  105. .output = 1, \
  106. .channel = (chan), \
  107. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  108. BIT(IIO_CHAN_INFO_SCALE), \
  109. }
  110. static const struct iio_chan_spec max517_channels[] = {
  111. MAX517_CHANNEL(0),
  112. MAX517_CHANNEL(1),
  113. MAX517_CHANNEL(2),
  114. MAX517_CHANNEL(3),
  115. MAX517_CHANNEL(4),
  116. MAX517_CHANNEL(5),
  117. MAX517_CHANNEL(6),
  118. MAX517_CHANNEL(7),
  119. };
  120. static int max517_probe(struct i2c_client *client,
  121. const struct i2c_device_id *id)
  122. {
  123. struct max517_data *data;
  124. struct iio_dev *indio_dev;
  125. struct max517_platform_data *platform_data = client->dev.platform_data;
  126. int chan;
  127. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  128. if (!indio_dev)
  129. return -ENOMEM;
  130. data = iio_priv(indio_dev);
  131. i2c_set_clientdata(client, indio_dev);
  132. data->client = client;
  133. switch (id->driver_data) {
  134. case ID_MAX521:
  135. indio_dev->num_channels = 8;
  136. break;
  137. case ID_MAX520:
  138. indio_dev->num_channels = 4;
  139. break;
  140. case ID_MAX519:
  141. case ID_MAX518:
  142. indio_dev->num_channels = 2;
  143. break;
  144. default: /* single channel for MAX517 */
  145. indio_dev->num_channels = 1;
  146. break;
  147. }
  148. indio_dev->channels = max517_channels;
  149. indio_dev->modes = INDIO_DIRECT_MODE;
  150. indio_dev->info = &max517_info;
  151. /*
  152. * Reference voltage on MAX518 and default is 5V, else take vref_mv
  153. * from platform_data
  154. */
  155. for (chan = 0; chan < indio_dev->num_channels; chan++) {
  156. if (id->driver_data == ID_MAX518 || !platform_data)
  157. data->vref_mv[chan] = 5000; /* mV */
  158. else
  159. data->vref_mv[chan] = platform_data->vref_mv[chan];
  160. }
  161. return iio_device_register(indio_dev);
  162. }
  163. static int max517_remove(struct i2c_client *client)
  164. {
  165. iio_device_unregister(i2c_get_clientdata(client));
  166. return 0;
  167. }
  168. static const struct i2c_device_id max517_id[] = {
  169. { "max517", ID_MAX517 },
  170. { "max518", ID_MAX518 },
  171. { "max519", ID_MAX519 },
  172. { "max520", ID_MAX520 },
  173. { "max521", ID_MAX521 },
  174. { }
  175. };
  176. MODULE_DEVICE_TABLE(i2c, max517_id);
  177. static struct i2c_driver max517_driver = {
  178. .driver = {
  179. .name = MAX517_DRV_NAME,
  180. .pm = &max517_pm_ops,
  181. },
  182. .probe = max517_probe,
  183. .remove = max517_remove,
  184. .id_table = max517_id,
  185. };
  186. module_i2c_driver(max517_driver);
  187. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  188. MODULE_DESCRIPTION("MAX517/518/519/520/521 8-bit DAC");
  189. MODULE_LICENSE("GPL");