m62332.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * m62332.c - Support for Mitsubishi m62332 DAC
  4. *
  5. * Copyright (c) 2014 Dmitry Eremin-Solenikov
  6. *
  7. * Based on max517 driver:
  8. * Copyright (C) 2010, 2011 Roland Stigge <[email protected]>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/i2c.h>
  13. #include <linux/err.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/driver.h>
  16. #include <linux/regulator/consumer.h>
  17. #define M62332_CHANNELS 2
  18. struct m62332_data {
  19. struct i2c_client *client;
  20. struct regulator *vcc;
  21. struct mutex mutex;
  22. u8 raw[M62332_CHANNELS];
  23. u8 save[M62332_CHANNELS];
  24. };
  25. static int m62332_set_value(struct iio_dev *indio_dev, u8 val, int channel)
  26. {
  27. struct m62332_data *data = iio_priv(indio_dev);
  28. struct i2c_client *client = data->client;
  29. u8 outbuf[2];
  30. int res;
  31. if (val == data->raw[channel])
  32. return 0;
  33. outbuf[0] = channel;
  34. outbuf[1] = val;
  35. mutex_lock(&data->mutex);
  36. if (val) {
  37. res = regulator_enable(data->vcc);
  38. if (res)
  39. goto out;
  40. }
  41. res = i2c_master_send(client, outbuf, ARRAY_SIZE(outbuf));
  42. if (res >= 0 && res != ARRAY_SIZE(outbuf))
  43. res = -EIO;
  44. if (res < 0)
  45. goto out;
  46. data->raw[channel] = val;
  47. if (!val)
  48. regulator_disable(data->vcc);
  49. mutex_unlock(&data->mutex);
  50. return 0;
  51. out:
  52. mutex_unlock(&data->mutex);
  53. return res;
  54. }
  55. static int m62332_read_raw(struct iio_dev *indio_dev,
  56. struct iio_chan_spec const *chan,
  57. int *val,
  58. int *val2,
  59. long mask)
  60. {
  61. struct m62332_data *data = iio_priv(indio_dev);
  62. int ret;
  63. switch (mask) {
  64. case IIO_CHAN_INFO_SCALE:
  65. /* Corresponds to Vref / 2^(bits) */
  66. ret = regulator_get_voltage(data->vcc);
  67. if (ret < 0)
  68. return ret;
  69. *val = ret / 1000; /* mV */
  70. *val2 = 8;
  71. return IIO_VAL_FRACTIONAL_LOG2;
  72. case IIO_CHAN_INFO_RAW:
  73. *val = data->raw[chan->channel];
  74. return IIO_VAL_INT;
  75. case IIO_CHAN_INFO_OFFSET:
  76. *val = 1;
  77. return IIO_VAL_INT;
  78. default:
  79. break;
  80. }
  81. return -EINVAL;
  82. }
  83. static int m62332_write_raw(struct iio_dev *indio_dev,
  84. struct iio_chan_spec const *chan, int val, int val2,
  85. long mask)
  86. {
  87. switch (mask) {
  88. case IIO_CHAN_INFO_RAW:
  89. if (val < 0 || val > 255)
  90. return -EINVAL;
  91. return m62332_set_value(indio_dev, val, chan->channel);
  92. default:
  93. break;
  94. }
  95. return -EINVAL;
  96. }
  97. static int m62332_suspend(struct device *dev)
  98. {
  99. struct i2c_client *client = to_i2c_client(dev);
  100. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  101. struct m62332_data *data = iio_priv(indio_dev);
  102. int ret;
  103. data->save[0] = data->raw[0];
  104. data->save[1] = data->raw[1];
  105. ret = m62332_set_value(indio_dev, 0, 0);
  106. if (ret < 0)
  107. return ret;
  108. return m62332_set_value(indio_dev, 0, 1);
  109. }
  110. static int m62332_resume(struct device *dev)
  111. {
  112. struct i2c_client *client = to_i2c_client(dev);
  113. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  114. struct m62332_data *data = iio_priv(indio_dev);
  115. int ret;
  116. ret = m62332_set_value(indio_dev, data->save[0], 0);
  117. if (ret < 0)
  118. return ret;
  119. return m62332_set_value(indio_dev, data->save[1], 1);
  120. }
  121. static DEFINE_SIMPLE_DEV_PM_OPS(m62332_pm_ops, m62332_suspend, m62332_resume);
  122. static const struct iio_info m62332_info = {
  123. .read_raw = m62332_read_raw,
  124. .write_raw = m62332_write_raw,
  125. };
  126. #define M62332_CHANNEL(chan) { \
  127. .type = IIO_VOLTAGE, \
  128. .indexed = 1, \
  129. .output = 1, \
  130. .channel = (chan), \
  131. .datasheet_name = "CH" #chan, \
  132. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  133. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  134. BIT(IIO_CHAN_INFO_OFFSET), \
  135. }
  136. static const struct iio_chan_spec m62332_channels[M62332_CHANNELS] = {
  137. M62332_CHANNEL(0),
  138. M62332_CHANNEL(1)
  139. };
  140. static int m62332_probe(struct i2c_client *client,
  141. const struct i2c_device_id *id)
  142. {
  143. struct m62332_data *data;
  144. struct iio_dev *indio_dev;
  145. int ret;
  146. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  147. if (!indio_dev)
  148. return -ENOMEM;
  149. data = iio_priv(indio_dev);
  150. i2c_set_clientdata(client, indio_dev);
  151. data->client = client;
  152. mutex_init(&data->mutex);
  153. data->vcc = devm_regulator_get(&client->dev, "VCC");
  154. if (IS_ERR(data->vcc))
  155. return PTR_ERR(data->vcc);
  156. indio_dev->num_channels = ARRAY_SIZE(m62332_channels);
  157. indio_dev->channels = m62332_channels;
  158. indio_dev->modes = INDIO_DIRECT_MODE;
  159. indio_dev->info = &m62332_info;
  160. ret = iio_map_array_register(indio_dev, client->dev.platform_data);
  161. if (ret < 0)
  162. return ret;
  163. ret = iio_device_register(indio_dev);
  164. if (ret < 0)
  165. goto err;
  166. return 0;
  167. err:
  168. iio_map_array_unregister(indio_dev);
  169. return ret;
  170. }
  171. static void m62332_remove(struct i2c_client *client)
  172. {
  173. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  174. iio_device_unregister(indio_dev);
  175. iio_map_array_unregister(indio_dev);
  176. m62332_set_value(indio_dev, 0, 0);
  177. m62332_set_value(indio_dev, 0, 1);
  178. }
  179. static const struct i2c_device_id m62332_id[] = {
  180. { "m62332", },
  181. { }
  182. };
  183. MODULE_DEVICE_TABLE(i2c, m62332_id);
  184. static struct i2c_driver m62332_driver = {
  185. .driver = {
  186. .name = "m62332",
  187. .pm = pm_sleep_ptr(&m62332_pm_ops),
  188. },
  189. .probe = m62332_probe,
  190. .remove = m62332_remove,
  191. .id_table = m62332_id,
  192. };
  193. module_i2c_driver(m62332_driver);
  194. MODULE_AUTHOR("Dmitry Eremin-Solenikov");
  195. MODULE_DESCRIPTION("M62332 8-bit DAC");
  196. MODULE_LICENSE("GPL v2");