m62332.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * m62332.c - Support for Mitsubishi m62332 DAC
  3. *
  4. * Copyright (c) 2014 Dmitry Eremin-Solenikov
  5. *
  6. * Based on max517 driver:
  7. * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/i2c.h>
  22. #include <linux/err.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/driver.h>
  25. #include <linux/regulator/consumer.h>
  26. #define M62332_CHANNELS 2
  27. struct m62332_data {
  28. struct i2c_client *client;
  29. struct regulator *vcc;
  30. struct mutex mutex;
  31. u8 raw[M62332_CHANNELS];
  32. #ifdef CONFIG_PM_SLEEP
  33. u8 save[M62332_CHANNELS];
  34. #endif
  35. };
  36. static int m62332_set_value(struct iio_dev *indio_dev, u8 val, int channel)
  37. {
  38. struct m62332_data *data = iio_priv(indio_dev);
  39. struct i2c_client *client = data->client;
  40. u8 outbuf[2];
  41. int res;
  42. if (val == data->raw[channel])
  43. return 0;
  44. outbuf[0] = channel;
  45. outbuf[1] = val;
  46. mutex_lock(&data->mutex);
  47. if (val) {
  48. res = regulator_enable(data->vcc);
  49. if (res)
  50. goto out;
  51. }
  52. res = i2c_master_send(client, outbuf, ARRAY_SIZE(outbuf));
  53. if (res >= 0 && res != ARRAY_SIZE(outbuf))
  54. res = -EIO;
  55. if (res < 0)
  56. goto out;
  57. data->raw[channel] = val;
  58. if (!val)
  59. regulator_disable(data->vcc);
  60. mutex_unlock(&data->mutex);
  61. return 0;
  62. out:
  63. mutex_unlock(&data->mutex);
  64. return res;
  65. }
  66. static int m62332_read_raw(struct iio_dev *indio_dev,
  67. struct iio_chan_spec const *chan,
  68. int *val,
  69. int *val2,
  70. long mask)
  71. {
  72. struct m62332_data *data = iio_priv(indio_dev);
  73. int ret;
  74. switch (mask) {
  75. case IIO_CHAN_INFO_SCALE:
  76. /* Corresponds to Vref / 2^(bits) */
  77. ret = regulator_get_voltage(data->vcc);
  78. if (ret < 0)
  79. return ret;
  80. *val = ret / 1000; /* mV */
  81. *val2 = 8;
  82. return IIO_VAL_FRACTIONAL_LOG2;
  83. case IIO_CHAN_INFO_RAW:
  84. *val = data->raw[chan->channel];
  85. return IIO_VAL_INT;
  86. case IIO_CHAN_INFO_OFFSET:
  87. *val = 1;
  88. return IIO_VAL_INT;
  89. default:
  90. break;
  91. }
  92. return -EINVAL;
  93. }
  94. static int m62332_write_raw(struct iio_dev *indio_dev,
  95. struct iio_chan_spec const *chan, int val, int val2,
  96. long mask)
  97. {
  98. switch (mask) {
  99. case IIO_CHAN_INFO_RAW:
  100. if (val < 0 || val > 255)
  101. return -EINVAL;
  102. return m62332_set_value(indio_dev, val, chan->channel);
  103. default:
  104. break;
  105. }
  106. return -EINVAL;
  107. }
  108. #ifdef CONFIG_PM_SLEEP
  109. static int m62332_suspend(struct device *dev)
  110. {
  111. struct i2c_client *client = to_i2c_client(dev);
  112. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  113. struct m62332_data *data = iio_priv(indio_dev);
  114. int ret;
  115. data->save[0] = data->raw[0];
  116. data->save[1] = data->raw[1];
  117. ret = m62332_set_value(indio_dev, 0, 0);
  118. if (ret < 0)
  119. return ret;
  120. return m62332_set_value(indio_dev, 0, 1);
  121. }
  122. static int m62332_resume(struct device *dev)
  123. {
  124. struct i2c_client *client = to_i2c_client(dev);
  125. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  126. struct m62332_data *data = iio_priv(indio_dev);
  127. int ret;
  128. ret = m62332_set_value(indio_dev, data->save[0], 0);
  129. if (ret < 0)
  130. return ret;
  131. return m62332_set_value(indio_dev, data->save[1], 1);
  132. }
  133. static SIMPLE_DEV_PM_OPS(m62332_pm_ops, m62332_suspend, m62332_resume);
  134. #define M62332_PM_OPS (&m62332_pm_ops)
  135. #else
  136. #define M62332_PM_OPS NULL
  137. #endif
  138. static const struct iio_info m62332_info = {
  139. .read_raw = m62332_read_raw,
  140. .write_raw = m62332_write_raw,
  141. .driver_module = THIS_MODULE,
  142. };
  143. #define M62332_CHANNEL(chan) { \
  144. .type = IIO_VOLTAGE, \
  145. .indexed = 1, \
  146. .output = 1, \
  147. .channel = (chan), \
  148. .datasheet_name = "CH" #chan, \
  149. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  150. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  151. BIT(IIO_CHAN_INFO_OFFSET), \
  152. }
  153. static const struct iio_chan_spec m62332_channels[M62332_CHANNELS] = {
  154. M62332_CHANNEL(0),
  155. M62332_CHANNEL(1)
  156. };
  157. static int m62332_probe(struct i2c_client *client,
  158. const struct i2c_device_id *id)
  159. {
  160. struct m62332_data *data;
  161. struct iio_dev *indio_dev;
  162. int ret;
  163. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  164. if (!indio_dev)
  165. return -ENOMEM;
  166. data = iio_priv(indio_dev);
  167. i2c_set_clientdata(client, indio_dev);
  168. data->client = client;
  169. mutex_init(&data->mutex);
  170. data->vcc = devm_regulator_get(&client->dev, "VCC");
  171. if (IS_ERR(data->vcc))
  172. return PTR_ERR(data->vcc);
  173. /* establish that the iio_dev is a child of the i2c device */
  174. indio_dev->dev.parent = &client->dev;
  175. indio_dev->num_channels = ARRAY_SIZE(m62332_channels);
  176. indio_dev->channels = m62332_channels;
  177. indio_dev->modes = INDIO_DIRECT_MODE;
  178. indio_dev->info = &m62332_info;
  179. ret = iio_map_array_register(indio_dev, client->dev.platform_data);
  180. if (ret < 0)
  181. return ret;
  182. ret = iio_device_register(indio_dev);
  183. if (ret < 0)
  184. goto err;
  185. return 0;
  186. err:
  187. iio_map_array_unregister(indio_dev);
  188. return ret;
  189. }
  190. static int m62332_remove(struct i2c_client *client)
  191. {
  192. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  193. iio_device_unregister(indio_dev);
  194. iio_map_array_unregister(indio_dev);
  195. m62332_set_value(indio_dev, 0, 0);
  196. m62332_set_value(indio_dev, 0, 1);
  197. return 0;
  198. }
  199. static const struct i2c_device_id m62332_id[] = {
  200. { "m62332", },
  201. { }
  202. };
  203. MODULE_DEVICE_TABLE(i2c, m62332_id);
  204. static struct i2c_driver m62332_driver = {
  205. .driver = {
  206. .name = "m62332",
  207. .pm = M62332_PM_OPS,
  208. },
  209. .probe = m62332_probe,
  210. .remove = m62332_remove,
  211. .id_table = m62332_id,
  212. };
  213. module_i2c_driver(m62332_driver);
  214. MODULE_AUTHOR("Dmitry Eremin-Solenikov");
  215. MODULE_DESCRIPTION("M62332 8-bit DAC");
  216. MODULE_LICENSE("GPL v2");