max517.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/i2c.h>
  25. #include <linux/err.h>
  26. #include <linux/iio/iio.h>
  27. #include <linux/iio/sysfs.h>
  28. #include <linux/iio/dac/max517.h>
  29. #define MAX517_DRV_NAME "max517"
  30. /* Commands */
  31. #define COMMAND_CHANNEL0 0x00
  32. #define COMMAND_CHANNEL1 0x01 /* for MAX518 and MAX519 */
  33. #define COMMAND_PD 0x08 /* Power Down */
  34. enum max517_device_ids {
  35. ID_MAX517,
  36. ID_MAX518,
  37. ID_MAX519,
  38. };
  39. struct max517_data {
  40. struct i2c_client *client;
  41. unsigned short vref_mv[2];
  42. };
  43. /*
  44. * channel: bit 0: channel 1
  45. * bit 1: channel 2
  46. * (this way, it's possible to set both channels at once)
  47. */
  48. static int max517_set_value(struct iio_dev *indio_dev,
  49. long val, int channel)
  50. {
  51. struct max517_data *data = iio_priv(indio_dev);
  52. struct i2c_client *client = data->client;
  53. u8 outbuf[2];
  54. int res;
  55. if (val < 0 || val > 255)
  56. return -EINVAL;
  57. outbuf[0] = channel;
  58. outbuf[1] = val;
  59. res = i2c_master_send(client, outbuf, 2);
  60. if (res < 0)
  61. return res;
  62. else if (res != 2)
  63. return -EIO;
  64. else
  65. return 0;
  66. }
  67. static int max517_read_raw(struct iio_dev *indio_dev,
  68. struct iio_chan_spec const *chan,
  69. int *val,
  70. int *val2,
  71. long m)
  72. {
  73. struct max517_data *data = iio_priv(indio_dev);
  74. unsigned int scale_uv;
  75. switch (m) {
  76. case IIO_CHAN_INFO_SCALE:
  77. /* Corresponds to Vref / 2^(bits) */
  78. scale_uv = (data->vref_mv[chan->channel] * 1000) >> 8;
  79. *val = scale_uv / 1000000;
  80. *val2 = scale_uv % 1000000;
  81. return IIO_VAL_INT_PLUS_MICRO;
  82. default:
  83. break;
  84. }
  85. return -EINVAL;
  86. }
  87. static int max517_write_raw(struct iio_dev *indio_dev,
  88. struct iio_chan_spec const *chan, int val, int val2, long mask)
  89. {
  90. int ret;
  91. switch (mask) {
  92. case IIO_CHAN_INFO_RAW:
  93. ret = max517_set_value(indio_dev, val, chan->channel);
  94. break;
  95. default:
  96. ret = -EINVAL;
  97. break;
  98. }
  99. return ret;
  100. }
  101. #ifdef CONFIG_PM_SLEEP
  102. static int max517_suspend(struct device *dev)
  103. {
  104. u8 outbuf = COMMAND_PD;
  105. return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
  106. }
  107. static int max517_resume(struct device *dev)
  108. {
  109. u8 outbuf = 0;
  110. return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
  111. }
  112. static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
  113. #define MAX517_PM_OPS (&max517_pm_ops)
  114. #else
  115. #define MAX517_PM_OPS NULL
  116. #endif
  117. static const struct iio_info max517_info = {
  118. .read_raw = max517_read_raw,
  119. .write_raw = max517_write_raw,
  120. .driver_module = THIS_MODULE,
  121. };
  122. #define MAX517_CHANNEL(chan) { \
  123. .type = IIO_VOLTAGE, \
  124. .indexed = 1, \
  125. .output = 1, \
  126. .channel = (chan), \
  127. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
  128. IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
  129. .scan_type = IIO_ST('u', 8, 8, 0), \
  130. }
  131. static const struct iio_chan_spec max517_channels[] = {
  132. MAX517_CHANNEL(0),
  133. MAX517_CHANNEL(1)
  134. };
  135. static int max517_probe(struct i2c_client *client,
  136. const struct i2c_device_id *id)
  137. {
  138. struct max517_data *data;
  139. struct iio_dev *indio_dev;
  140. struct max517_platform_data *platform_data = client->dev.platform_data;
  141. int err;
  142. indio_dev = iio_device_alloc(sizeof(*data));
  143. if (indio_dev == NULL) {
  144. err = -ENOMEM;
  145. goto exit;
  146. }
  147. data = iio_priv(indio_dev);
  148. i2c_set_clientdata(client, indio_dev);
  149. data->client = client;
  150. /* establish that the iio_dev is a child of the i2c device */
  151. indio_dev->dev.parent = &client->dev;
  152. /* reduced channel set for MAX517 */
  153. if (id->driver_data == ID_MAX517)
  154. indio_dev->num_channels = 1;
  155. else
  156. indio_dev->num_channels = 2;
  157. indio_dev->channels = max517_channels;
  158. indio_dev->modes = INDIO_DIRECT_MODE;
  159. indio_dev->info = &max517_info;
  160. /*
  161. * Reference voltage on MAX518 and default is 5V, else take vref_mv
  162. * from platform_data
  163. */
  164. if (id->driver_data == ID_MAX518 || !platform_data) {
  165. data->vref_mv[0] = data->vref_mv[1] = 5000; /* mV */
  166. } else {
  167. data->vref_mv[0] = platform_data->vref_mv[0];
  168. data->vref_mv[1] = platform_data->vref_mv[1];
  169. }
  170. err = iio_device_register(indio_dev);
  171. if (err)
  172. goto exit_free_device;
  173. dev_info(&client->dev, "DAC registered\n");
  174. return 0;
  175. exit_free_device:
  176. iio_device_free(indio_dev);
  177. exit:
  178. return err;
  179. }
  180. static int max517_remove(struct i2c_client *client)
  181. {
  182. iio_device_unregister(i2c_get_clientdata(client));
  183. iio_device_free(i2c_get_clientdata(client));
  184. return 0;
  185. }
  186. static const struct i2c_device_id max517_id[] = {
  187. { "max517", ID_MAX517 },
  188. { "max518", ID_MAX518 },
  189. { "max519", ID_MAX519 },
  190. { }
  191. };
  192. MODULE_DEVICE_TABLE(i2c, max517_id);
  193. static struct i2c_driver max517_driver = {
  194. .driver = {
  195. .name = MAX517_DRV_NAME,
  196. .pm = MAX517_PM_OPS,
  197. },
  198. .probe = max517_probe,
  199. .remove = max517_remove,
  200. .id_table = max517_id,
  201. };
  202. module_i2c_driver(max517_driver);
  203. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  204. MODULE_DESCRIPTION("MAX517/MAX518/MAX519 8-bit DAC");
  205. MODULE_LICENSE("GPL");