max517.c 5.3 KB

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