max517.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 iio_dev *indio_dev;
  41. struct i2c_client *client;
  42. unsigned short vref_mv[2];
  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. unsigned int scale_uv;
  76. switch (m) {
  77. case IIO_CHAN_INFO_SCALE:
  78. /* Corresponds to Vref / 2^(bits) */
  79. scale_uv = (data->vref_mv[chan->channel] * 1000) >> 8;
  80. *val = scale_uv / 1000000;
  81. *val2 = scale_uv % 1000000;
  82. return IIO_VAL_INT_PLUS_MICRO;
  83. default:
  84. break;
  85. }
  86. return -EINVAL;
  87. }
  88. static int max517_write_raw(struct iio_dev *indio_dev,
  89. struct iio_chan_spec const *chan, int val, int val2, long mask)
  90. {
  91. int ret;
  92. switch (mask) {
  93. case IIO_CHAN_INFO_RAW:
  94. ret = max517_set_value(indio_dev, val, chan->channel);
  95. break;
  96. default:
  97. ret = -EINVAL;
  98. break;
  99. }
  100. return ret;
  101. }
  102. #ifdef CONFIG_PM_SLEEP
  103. static int max517_suspend(struct device *dev)
  104. {
  105. u8 outbuf = COMMAND_PD;
  106. return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
  107. }
  108. static int max517_resume(struct device *dev)
  109. {
  110. u8 outbuf = 0;
  111. return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
  112. }
  113. static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
  114. #define MAX517_PM_OPS (&max517_pm_ops)
  115. #else
  116. #define MAX517_PM_OPS NULL
  117. #endif
  118. static const struct iio_info max517_info = {
  119. .read_raw = max517_read_raw,
  120. .write_raw = max517_write_raw,
  121. .driver_module = THIS_MODULE,
  122. };
  123. #define MAX517_CHANNEL(chan) { \
  124. .type = IIO_VOLTAGE, \
  125. .indexed = 1, \
  126. .output = 1, \
  127. .channel = (chan), \
  128. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
  129. IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
  130. .scan_type = IIO_ST('u', 8, 8, 0), \
  131. }
  132. static const struct iio_chan_spec max517_channels[] = {
  133. MAX517_CHANNEL(0),
  134. MAX517_CHANNEL(1)
  135. };
  136. static int max517_probe(struct i2c_client *client,
  137. const struct i2c_device_id *id)
  138. {
  139. struct max517_data *data;
  140. struct iio_dev *indio_dev;
  141. struct max517_platform_data *platform_data = client->dev.platform_data;
  142. int err;
  143. indio_dev = iio_device_alloc(sizeof(*data));
  144. if (indio_dev == NULL) {
  145. err = -ENOMEM;
  146. goto exit;
  147. }
  148. data = iio_priv(indio_dev);
  149. i2c_set_clientdata(client, indio_dev);
  150. data->client = client;
  151. /* establish that the iio_dev is a child of the i2c device */
  152. indio_dev->dev.parent = &client->dev;
  153. /* reduced channel set for MAX517 */
  154. if (id->driver_data == ID_MAX517)
  155. indio_dev->num_channels = 1;
  156. else
  157. indio_dev->num_channels = 2;
  158. indio_dev->channels = max517_channels;
  159. indio_dev->modes = INDIO_DIRECT_MODE;
  160. indio_dev->info = &max517_info;
  161. /*
  162. * Reference voltage on MAX518 and default is 5V, else take vref_mv
  163. * from platform_data
  164. */
  165. if (id->driver_data == ID_MAX518 || !platform_data) {
  166. data->vref_mv[0] = data->vref_mv[1] = 5000; /* mV */
  167. } else {
  168. data->vref_mv[0] = platform_data->vref_mv[0];
  169. data->vref_mv[1] = platform_data->vref_mv[1];
  170. }
  171. err = iio_device_register(indio_dev);
  172. if (err)
  173. goto exit_free_device;
  174. dev_info(&client->dev, "DAC registered\n");
  175. return 0;
  176. exit_free_device:
  177. iio_device_free(indio_dev);
  178. exit:
  179. return err;
  180. }
  181. static int max517_remove(struct i2c_client *client)
  182. {
  183. iio_device_unregister(i2c_get_clientdata(client));
  184. iio_device_free(i2c_get_clientdata(client));
  185. return 0;
  186. }
  187. static const struct i2c_device_id max517_id[] = {
  188. { "max517", ID_MAX517 },
  189. { "max518", ID_MAX518 },
  190. { "max519", ID_MAX519 },
  191. { }
  192. };
  193. MODULE_DEVICE_TABLE(i2c, max517_id);
  194. static struct i2c_driver max517_driver = {
  195. .driver = {
  196. .name = MAX517_DRV_NAME,
  197. .pm = MAX517_PM_OPS,
  198. },
  199. .probe = max517_probe,
  200. .remove = max517_remove,
  201. .id_table = max517_id,
  202. };
  203. module_i2c_driver(max517_driver);
  204. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  205. MODULE_DESCRIPTION("MAX517/MAX518/MAX519 8-bit DAC");
  206. MODULE_LICENSE("GPL");