ds4424.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Maxim Integrated
  4. * 7-bit, Multi-Channel Sink/Source Current DAC Driver
  5. * Copyright (C) 2017 Maxim Integrated
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/i2c.h>
  10. #include <linux/regulator/consumer.h>
  11. #include <linux/err.h>
  12. #include <linux/delay.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/driver.h>
  15. #include <linux/iio/machine.h>
  16. #include <linux/iio/consumer.h>
  17. #define DS4422_MAX_DAC_CHANNELS 2
  18. #define DS4424_MAX_DAC_CHANNELS 4
  19. #define DS4424_DAC_ADDR(chan) ((chan) + 0xf8)
  20. #define DS4424_SOURCE_I 1
  21. #define DS4424_SINK_I 0
  22. #define DS4424_CHANNEL(chan) { \
  23. .type = IIO_CURRENT, \
  24. .indexed = 1, \
  25. .output = 1, \
  26. .channel = chan, \
  27. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  28. }
  29. /*
  30. * DS4424 DAC control register 8 bits
  31. * [7] 0: to sink; 1: to source
  32. * [6:0] steps to sink/source
  33. * bit[7] looks like a sign bit, but the value of the register is
  34. * not a two's complement code considering the bit[6:0] is a absolute
  35. * distance from the zero point.
  36. */
  37. union ds4424_raw_data {
  38. struct {
  39. u8 dx:7;
  40. u8 source_bit:1;
  41. };
  42. u8 bits;
  43. };
  44. enum ds4424_device_ids {
  45. ID_DS4422,
  46. ID_DS4424,
  47. };
  48. struct ds4424_data {
  49. struct i2c_client *client;
  50. struct mutex lock;
  51. uint8_t save[DS4424_MAX_DAC_CHANNELS];
  52. struct regulator *vcc_reg;
  53. uint8_t raw[DS4424_MAX_DAC_CHANNELS];
  54. };
  55. static const struct iio_chan_spec ds4424_channels[] = {
  56. DS4424_CHANNEL(0),
  57. DS4424_CHANNEL(1),
  58. DS4424_CHANNEL(2),
  59. DS4424_CHANNEL(3),
  60. };
  61. static int ds4424_get_value(struct iio_dev *indio_dev,
  62. int *val, int channel)
  63. {
  64. struct ds4424_data *data = iio_priv(indio_dev);
  65. int ret;
  66. mutex_lock(&data->lock);
  67. ret = i2c_smbus_read_byte_data(data->client, DS4424_DAC_ADDR(channel));
  68. if (ret < 0)
  69. goto fail;
  70. *val = ret;
  71. fail:
  72. mutex_unlock(&data->lock);
  73. return ret;
  74. }
  75. static int ds4424_set_value(struct iio_dev *indio_dev,
  76. int val, struct iio_chan_spec const *chan)
  77. {
  78. struct ds4424_data *data = iio_priv(indio_dev);
  79. int ret;
  80. mutex_lock(&data->lock);
  81. ret = i2c_smbus_write_byte_data(data->client,
  82. DS4424_DAC_ADDR(chan->channel), val);
  83. if (ret < 0)
  84. goto fail;
  85. data->raw[chan->channel] = val;
  86. fail:
  87. mutex_unlock(&data->lock);
  88. return ret;
  89. }
  90. static int ds4424_read_raw(struct iio_dev *indio_dev,
  91. struct iio_chan_spec const *chan,
  92. int *val, int *val2, long mask)
  93. {
  94. union ds4424_raw_data raw;
  95. int ret;
  96. switch (mask) {
  97. case IIO_CHAN_INFO_RAW:
  98. ret = ds4424_get_value(indio_dev, val, chan->channel);
  99. if (ret < 0) {
  100. pr_err("%s : ds4424_get_value returned %d\n",
  101. __func__, ret);
  102. return ret;
  103. }
  104. raw.bits = *val;
  105. *val = raw.dx;
  106. if (raw.source_bit == DS4424_SINK_I)
  107. *val = -*val;
  108. return IIO_VAL_INT;
  109. default:
  110. return -EINVAL;
  111. }
  112. }
  113. static int ds4424_write_raw(struct iio_dev *indio_dev,
  114. struct iio_chan_spec const *chan,
  115. int val, int val2, long mask)
  116. {
  117. union ds4424_raw_data raw;
  118. if (val2 != 0)
  119. return -EINVAL;
  120. switch (mask) {
  121. case IIO_CHAN_INFO_RAW:
  122. if (val < S8_MIN || val > S8_MAX)
  123. return -EINVAL;
  124. if (val > 0) {
  125. raw.source_bit = DS4424_SOURCE_I;
  126. raw.dx = val;
  127. } else {
  128. raw.source_bit = DS4424_SINK_I;
  129. raw.dx = -val;
  130. }
  131. return ds4424_set_value(indio_dev, raw.bits, chan);
  132. default:
  133. return -EINVAL;
  134. }
  135. }
  136. static int ds4424_verify_chip(struct iio_dev *indio_dev)
  137. {
  138. int ret, val;
  139. ret = ds4424_get_value(indio_dev, &val, 0);
  140. if (ret < 0)
  141. dev_err(&indio_dev->dev,
  142. "%s failed. ret: %d\n", __func__, ret);
  143. return ret;
  144. }
  145. static int ds4424_suspend(struct device *dev)
  146. {
  147. struct i2c_client *client = to_i2c_client(dev);
  148. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  149. struct ds4424_data *data = iio_priv(indio_dev);
  150. int ret = 0;
  151. int i;
  152. for (i = 0; i < indio_dev->num_channels; i++) {
  153. data->save[i] = data->raw[i];
  154. ret = ds4424_set_value(indio_dev, 0,
  155. &indio_dev->channels[i]);
  156. if (ret < 0)
  157. return ret;
  158. }
  159. return ret;
  160. }
  161. static int ds4424_resume(struct device *dev)
  162. {
  163. struct i2c_client *client = to_i2c_client(dev);
  164. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  165. struct ds4424_data *data = iio_priv(indio_dev);
  166. int ret = 0;
  167. int i;
  168. for (i = 0; i < indio_dev->num_channels; i++) {
  169. ret = ds4424_set_value(indio_dev, data->save[i],
  170. &indio_dev->channels[i]);
  171. if (ret < 0)
  172. return ret;
  173. }
  174. return ret;
  175. }
  176. static DEFINE_SIMPLE_DEV_PM_OPS(ds4424_pm_ops, ds4424_suspend, ds4424_resume);
  177. static const struct iio_info ds4424_info = {
  178. .read_raw = ds4424_read_raw,
  179. .write_raw = ds4424_write_raw,
  180. };
  181. static int ds4424_probe(struct i2c_client *client,
  182. const struct i2c_device_id *id)
  183. {
  184. struct ds4424_data *data;
  185. struct iio_dev *indio_dev;
  186. int ret;
  187. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  188. if (!indio_dev) {
  189. dev_err(&client->dev, "iio dev alloc failed.\n");
  190. return -ENOMEM;
  191. }
  192. data = iio_priv(indio_dev);
  193. i2c_set_clientdata(client, indio_dev);
  194. data->client = client;
  195. indio_dev->name = id->name;
  196. data->vcc_reg = devm_regulator_get(&client->dev, "vcc");
  197. if (IS_ERR(data->vcc_reg))
  198. return dev_err_probe(&client->dev, PTR_ERR(data->vcc_reg),
  199. "Failed to get vcc-supply regulator.\n");
  200. mutex_init(&data->lock);
  201. ret = regulator_enable(data->vcc_reg);
  202. if (ret < 0) {
  203. dev_err(&client->dev,
  204. "Unable to enable the regulator.\n");
  205. return ret;
  206. }
  207. usleep_range(1000, 1200);
  208. ret = ds4424_verify_chip(indio_dev);
  209. if (ret < 0)
  210. goto fail;
  211. switch (id->driver_data) {
  212. case ID_DS4422:
  213. indio_dev->num_channels = DS4422_MAX_DAC_CHANNELS;
  214. break;
  215. case ID_DS4424:
  216. indio_dev->num_channels = DS4424_MAX_DAC_CHANNELS;
  217. break;
  218. default:
  219. dev_err(&client->dev,
  220. "ds4424: Invalid chip id.\n");
  221. ret = -ENXIO;
  222. goto fail;
  223. }
  224. indio_dev->channels = ds4424_channels;
  225. indio_dev->modes = INDIO_DIRECT_MODE;
  226. indio_dev->info = &ds4424_info;
  227. ret = iio_device_register(indio_dev);
  228. if (ret < 0) {
  229. dev_err(&client->dev,
  230. "iio_device_register failed. ret: %d\n", ret);
  231. goto fail;
  232. }
  233. return ret;
  234. fail:
  235. regulator_disable(data->vcc_reg);
  236. return ret;
  237. }
  238. static void ds4424_remove(struct i2c_client *client)
  239. {
  240. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  241. struct ds4424_data *data = iio_priv(indio_dev);
  242. iio_device_unregister(indio_dev);
  243. regulator_disable(data->vcc_reg);
  244. }
  245. static const struct i2c_device_id ds4424_id[] = {
  246. { "ds4422", ID_DS4422 },
  247. { "ds4424", ID_DS4424 },
  248. { }
  249. };
  250. MODULE_DEVICE_TABLE(i2c, ds4424_id);
  251. static const struct of_device_id ds4424_of_match[] = {
  252. { .compatible = "maxim,ds4422" },
  253. { .compatible = "maxim,ds4424" },
  254. { },
  255. };
  256. MODULE_DEVICE_TABLE(of, ds4424_of_match);
  257. static struct i2c_driver ds4424_driver = {
  258. .driver = {
  259. .name = "ds4424",
  260. .of_match_table = ds4424_of_match,
  261. .pm = pm_sleep_ptr(&ds4424_pm_ops),
  262. },
  263. .probe = ds4424_probe,
  264. .remove = ds4424_remove,
  265. .id_table = ds4424_id,
  266. };
  267. module_i2c_driver(ds4424_driver);
  268. MODULE_DESCRIPTION("Maxim DS4424 DAC Driver");
  269. MODULE_AUTHOR("Ismail H. Kose <[email protected]>");
  270. MODULE_AUTHOR("Vishal Sood <[email protected]>");
  271. MODULE_AUTHOR("David Jung <[email protected]>");
  272. MODULE_LICENSE("GPL v2");