max517.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * max517.c - Support for Maxim MAX517, MAX518 and MAX519
  3. *
  4. * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/i2c.h>
  24. #include <linux/err.h>
  25. #include <linux/iio/iio.h>
  26. #include <linux/iio/sysfs.h>
  27. #include <linux/iio/dac/max517.h>
  28. #define MAX517_DRV_NAME "max517"
  29. /* Commands */
  30. #define COMMAND_CHANNEL0 0x00
  31. #define COMMAND_CHANNEL1 0x01 /* for MAX518 and MAX519 */
  32. #define COMMAND_PD 0x08 /* Power Down */
  33. enum max517_device_ids {
  34. ID_MAX517,
  35. ID_MAX518,
  36. ID_MAX519,
  37. ID_MAX520,
  38. ID_MAX521,
  39. };
  40. struct max517_data {
  41. struct i2c_client *client;
  42. unsigned short vref_mv[8];
  43. };
  44. /*
  45. * channel: bit 0: channel 1
  46. * bit 1: channel 2
  47. * (this way, it's possible to set both channels at once)
  48. */
  49. static int max517_set_value(struct iio_dev *indio_dev,
  50. long val, int channel)
  51. {
  52. struct max517_data *data = iio_priv(indio_dev);
  53. struct i2c_client *client = data->client;
  54. u8 outbuf[2];
  55. int res;
  56. if (val < 0 || val > 255)
  57. return -EINVAL;
  58. outbuf[0] = channel;
  59. outbuf[1] = val;
  60. res = i2c_master_send(client, outbuf, 2);
  61. if (res < 0)
  62. return res;
  63. else if (res != 2)
  64. return -EIO;
  65. else
  66. return 0;
  67. }
  68. static int max517_read_raw(struct iio_dev *indio_dev,
  69. struct iio_chan_spec const *chan,
  70. int *val,
  71. int *val2,
  72. long m)
  73. {
  74. struct max517_data *data = iio_priv(indio_dev);
  75. switch (m) {
  76. case IIO_CHAN_INFO_SCALE:
  77. /* Corresponds to Vref / 2^(bits) */
  78. *val = data->vref_mv[chan->channel];
  79. *val2 = 8;
  80. return IIO_VAL_FRACTIONAL_LOG2;
  81. default:
  82. break;
  83. }
  84. return -EINVAL;
  85. }
  86. static int max517_write_raw(struct iio_dev *indio_dev,
  87. struct iio_chan_spec const *chan, int val, int val2, long mask)
  88. {
  89. int ret;
  90. switch (mask) {
  91. case IIO_CHAN_INFO_RAW:
  92. ret = max517_set_value(indio_dev, val, chan->channel);
  93. break;
  94. default:
  95. ret = -EINVAL;
  96. break;
  97. }
  98. return ret;
  99. }
  100. #ifdef CONFIG_PM_SLEEP
  101. static int max517_suspend(struct device *dev)
  102. {
  103. u8 outbuf = COMMAND_PD;
  104. return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
  105. }
  106. static int max517_resume(struct device *dev)
  107. {
  108. u8 outbuf = 0;
  109. return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
  110. }
  111. static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
  112. #define MAX517_PM_OPS (&max517_pm_ops)
  113. #else
  114. #define MAX517_PM_OPS NULL
  115. #endif
  116. static const struct iio_info max517_info = {
  117. .read_raw = max517_read_raw,
  118. .write_raw = max517_write_raw,
  119. .driver_module = THIS_MODULE,
  120. };
  121. #define MAX517_CHANNEL(chan) { \
  122. .type = IIO_VOLTAGE, \
  123. .indexed = 1, \
  124. .output = 1, \
  125. .channel = (chan), \
  126. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  127. BIT(IIO_CHAN_INFO_SCALE), \
  128. }
  129. static const struct iio_chan_spec max517_channels[] = {
  130. MAX517_CHANNEL(0),
  131. MAX517_CHANNEL(1),
  132. MAX517_CHANNEL(2),
  133. MAX517_CHANNEL(3),
  134. MAX517_CHANNEL(4),
  135. MAX517_CHANNEL(5),
  136. MAX517_CHANNEL(6),
  137. MAX517_CHANNEL(7),
  138. };
  139. static int max517_probe(struct i2c_client *client,
  140. const struct i2c_device_id *id)
  141. {
  142. struct max517_data *data;
  143. struct iio_dev *indio_dev;
  144. struct max517_platform_data *platform_data = client->dev.platform_data;
  145. int chan;
  146. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  147. if (!indio_dev)
  148. return -ENOMEM;
  149. data = iio_priv(indio_dev);
  150. i2c_set_clientdata(client, indio_dev);
  151. data->client = client;
  152. /* establish that the iio_dev is a child of the i2c device */
  153. indio_dev->dev.parent = &client->dev;
  154. switch (id->driver_data) {
  155. case ID_MAX521:
  156. indio_dev->num_channels = 8;
  157. break;
  158. case ID_MAX520:
  159. indio_dev->num_channels = 4;
  160. break;
  161. case ID_MAX519:
  162. case ID_MAX518:
  163. indio_dev->num_channels = 2;
  164. break;
  165. default: /* single channel for MAX517 */
  166. indio_dev->num_channels = 1;
  167. break;
  168. }
  169. indio_dev->channels = max517_channels;
  170. indio_dev->modes = INDIO_DIRECT_MODE;
  171. indio_dev->info = &max517_info;
  172. /*
  173. * Reference voltage on MAX518 and default is 5V, else take vref_mv
  174. * from platform_data
  175. */
  176. for (chan = 0; chan < indio_dev->num_channels; chan++) {
  177. if (id->driver_data == ID_MAX518 || !platform_data)
  178. data->vref_mv[chan] = 5000; /* mV */
  179. else
  180. data->vref_mv[chan] = platform_data->vref_mv[chan];
  181. }
  182. return iio_device_register(indio_dev);
  183. }
  184. static int max517_remove(struct i2c_client *client)
  185. {
  186. iio_device_unregister(i2c_get_clientdata(client));
  187. return 0;
  188. }
  189. static const struct i2c_device_id max517_id[] = {
  190. { "max517", ID_MAX517 },
  191. { "max518", ID_MAX518 },
  192. { "max519", ID_MAX519 },
  193. { "max520", ID_MAX520 },
  194. { "max521", ID_MAX521 },
  195. { }
  196. };
  197. MODULE_DEVICE_TABLE(i2c, max517_id);
  198. static struct i2c_driver max517_driver = {
  199. .driver = {
  200. .name = MAX517_DRV_NAME,
  201. .pm = MAX517_PM_OPS,
  202. },
  203. .probe = max517_probe,
  204. .remove = max517_remove,
  205. .id_table = max517_id,
  206. };
  207. module_i2c_driver(max517_driver);
  208. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  209. MODULE_DESCRIPTION("MAX517/518/519/520/521 8-bit DAC");
  210. MODULE_LICENSE("GPL");