max517.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. };
  38. struct max517_data {
  39. struct i2c_client *client;
  40. unsigned short vref_mv[2];
  41. };
  42. /*
  43. * channel: bit 0: channel 1
  44. * bit 1: channel 2
  45. * (this way, it's possible to set both channels at once)
  46. */
  47. static int max517_set_value(struct iio_dev *indio_dev,
  48. long val, int channel)
  49. {
  50. struct max517_data *data = iio_priv(indio_dev);
  51. struct i2c_client *client = data->client;
  52. u8 outbuf[2];
  53. int res;
  54. if (val < 0 || val > 255)
  55. return -EINVAL;
  56. outbuf[0] = channel;
  57. outbuf[1] = val;
  58. res = i2c_master_send(client, outbuf, 2);
  59. if (res < 0)
  60. return res;
  61. else if (res != 2)
  62. return -EIO;
  63. else
  64. return 0;
  65. }
  66. static int max517_read_raw(struct iio_dev *indio_dev,
  67. struct iio_chan_spec const *chan,
  68. int *val,
  69. int *val2,
  70. long m)
  71. {
  72. struct max517_data *data = iio_priv(indio_dev);
  73. switch (m) {
  74. case IIO_CHAN_INFO_SCALE:
  75. /* Corresponds to Vref / 2^(bits) */
  76. *val = data->vref_mv[chan->channel];
  77. *val2 = 8;
  78. return IIO_VAL_FRACTIONAL_LOG2;
  79. default:
  80. break;
  81. }
  82. return -EINVAL;
  83. }
  84. static int max517_write_raw(struct iio_dev *indio_dev,
  85. struct iio_chan_spec const *chan, int val, int val2, long mask)
  86. {
  87. int ret;
  88. switch (mask) {
  89. case IIO_CHAN_INFO_RAW:
  90. ret = max517_set_value(indio_dev, val, chan->channel);
  91. break;
  92. default:
  93. ret = -EINVAL;
  94. break;
  95. }
  96. return ret;
  97. }
  98. #ifdef CONFIG_PM_SLEEP
  99. static int max517_suspend(struct device *dev)
  100. {
  101. u8 outbuf = COMMAND_PD;
  102. return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
  103. }
  104. static int max517_resume(struct device *dev)
  105. {
  106. u8 outbuf = 0;
  107. return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
  108. }
  109. static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
  110. #define MAX517_PM_OPS (&max517_pm_ops)
  111. #else
  112. #define MAX517_PM_OPS NULL
  113. #endif
  114. static const struct iio_info max517_info = {
  115. .read_raw = max517_read_raw,
  116. .write_raw = max517_write_raw,
  117. .driver_module = THIS_MODULE,
  118. };
  119. #define MAX517_CHANNEL(chan) { \
  120. .type = IIO_VOLTAGE, \
  121. .indexed = 1, \
  122. .output = 1, \
  123. .channel = (chan), \
  124. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  125. BIT(IIO_CHAN_INFO_SCALE), \
  126. }
  127. static const struct iio_chan_spec max517_channels[] = {
  128. MAX517_CHANNEL(0),
  129. MAX517_CHANNEL(1)
  130. };
  131. static int max517_probe(struct i2c_client *client,
  132. const struct i2c_device_id *id)
  133. {
  134. struct max517_data *data;
  135. struct iio_dev *indio_dev;
  136. struct max517_platform_data *platform_data = client->dev.platform_data;
  137. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  138. if (!indio_dev)
  139. return -ENOMEM;
  140. data = iio_priv(indio_dev);
  141. i2c_set_clientdata(client, indio_dev);
  142. data->client = client;
  143. /* establish that the iio_dev is a child of the i2c device */
  144. indio_dev->dev.parent = &client->dev;
  145. /* reduced channel set for MAX517 */
  146. if (id->driver_data == ID_MAX517)
  147. indio_dev->num_channels = 1;
  148. else
  149. indio_dev->num_channels = 2;
  150. indio_dev->channels = max517_channels;
  151. indio_dev->modes = INDIO_DIRECT_MODE;
  152. indio_dev->info = &max517_info;
  153. /*
  154. * Reference voltage on MAX518 and default is 5V, else take vref_mv
  155. * from platform_data
  156. */
  157. if (id->driver_data == ID_MAX518 || !platform_data) {
  158. data->vref_mv[0] = data->vref_mv[1] = 5000; /* mV */
  159. } else {
  160. data->vref_mv[0] = platform_data->vref_mv[0];
  161. data->vref_mv[1] = platform_data->vref_mv[1];
  162. }
  163. return iio_device_register(indio_dev);
  164. }
  165. static int max517_remove(struct i2c_client *client)
  166. {
  167. iio_device_unregister(i2c_get_clientdata(client));
  168. return 0;
  169. }
  170. static const struct i2c_device_id max517_id[] = {
  171. { "max517", ID_MAX517 },
  172. { "max518", ID_MAX518 },
  173. { "max519", ID_MAX519 },
  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/MAX518/MAX519 8-bit DAC");
  189. MODULE_LICENSE("GPL");