max5821.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * iio/dac/max5821.c
  3. * Copyright (C) 2014 Philippe Reynes
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/i2c.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/regulator/consumer.h>
  14. #define MAX5821_MAX_DAC_CHANNELS 2
  15. /* command bytes */
  16. #define MAX5821_LOAD_DAC_A_IN_REG_B 0x00
  17. #define MAX5821_LOAD_DAC_B_IN_REG_A 0x10
  18. #define MAX5821_EXTENDED_COMMAND_MODE 0xf0
  19. #define MAX5821_READ_DAC_A_COMMAND 0xf1
  20. #define MAX5821_READ_DAC_B_COMMAND 0xf2
  21. #define MAX5821_EXTENDED_POWER_UP 0x00
  22. #define MAX5821_EXTENDED_POWER_DOWN_MODE0 0x01
  23. #define MAX5821_EXTENDED_POWER_DOWN_MODE1 0x02
  24. #define MAX5821_EXTENDED_POWER_DOWN_MODE2 0x03
  25. #define MAX5821_EXTENDED_DAC_A 0x04
  26. #define MAX5821_EXTENDED_DAC_B 0x08
  27. enum max5821_device_ids {
  28. ID_MAX5821,
  29. };
  30. struct max5821_data {
  31. struct i2c_client *client;
  32. struct regulator *vref_reg;
  33. unsigned short vref_mv;
  34. bool powerdown[MAX5821_MAX_DAC_CHANNELS];
  35. u8 powerdown_mode[MAX5821_MAX_DAC_CHANNELS];
  36. struct mutex lock;
  37. };
  38. static const char * const max5821_powerdown_modes[] = {
  39. "three_state",
  40. "1kohm_to_gnd",
  41. "100kohm_to_gnd",
  42. };
  43. enum {
  44. MAX5821_THREE_STATE,
  45. MAX5821_1KOHM_TO_GND,
  46. MAX5821_100KOHM_TO_GND
  47. };
  48. static int max5821_get_powerdown_mode(struct iio_dev *indio_dev,
  49. const struct iio_chan_spec *chan)
  50. {
  51. struct max5821_data *st = iio_priv(indio_dev);
  52. return st->powerdown_mode[chan->channel];
  53. }
  54. static int max5821_set_powerdown_mode(struct iio_dev *indio_dev,
  55. const struct iio_chan_spec *chan,
  56. unsigned int mode)
  57. {
  58. struct max5821_data *st = iio_priv(indio_dev);
  59. st->powerdown_mode[chan->channel] = mode;
  60. return 0;
  61. }
  62. static const struct iio_enum max5821_powerdown_mode_enum = {
  63. .items = max5821_powerdown_modes,
  64. .num_items = ARRAY_SIZE(max5821_powerdown_modes),
  65. .get = max5821_get_powerdown_mode,
  66. .set = max5821_set_powerdown_mode,
  67. };
  68. static ssize_t max5821_read_dac_powerdown(struct iio_dev *indio_dev,
  69. uintptr_t private,
  70. const struct iio_chan_spec *chan,
  71. char *buf)
  72. {
  73. struct max5821_data *st = iio_priv(indio_dev);
  74. return sprintf(buf, "%d\n", st->powerdown[chan->channel]);
  75. }
  76. static int max5821_sync_powerdown_mode(struct max5821_data *data,
  77. const struct iio_chan_spec *chan)
  78. {
  79. u8 outbuf[2];
  80. outbuf[0] = MAX5821_EXTENDED_COMMAND_MODE;
  81. if (chan->channel == 0)
  82. outbuf[1] = MAX5821_EXTENDED_DAC_A;
  83. else
  84. outbuf[1] = MAX5821_EXTENDED_DAC_B;
  85. if (data->powerdown[chan->channel])
  86. outbuf[1] |= data->powerdown_mode[chan->channel] + 1;
  87. else
  88. outbuf[1] |= MAX5821_EXTENDED_POWER_UP;
  89. return i2c_master_send(data->client, outbuf, 2);
  90. }
  91. static ssize_t max5821_write_dac_powerdown(struct iio_dev *indio_dev,
  92. uintptr_t private,
  93. const struct iio_chan_spec *chan,
  94. const char *buf, size_t len)
  95. {
  96. struct max5821_data *data = iio_priv(indio_dev);
  97. bool powerdown;
  98. int ret;
  99. ret = strtobool(buf, &powerdown);
  100. if (ret)
  101. return ret;
  102. data->powerdown[chan->channel] = powerdown;
  103. ret = max5821_sync_powerdown_mode(data, chan);
  104. if (ret < 0)
  105. return ret;
  106. return len;
  107. }
  108. static const struct iio_chan_spec_ext_info max5821_ext_info[] = {
  109. {
  110. .name = "powerdown",
  111. .read = max5821_read_dac_powerdown,
  112. .write = max5821_write_dac_powerdown,
  113. .shared = IIO_SEPARATE,
  114. },
  115. IIO_ENUM("powerdown_mode", IIO_SEPARATE, &max5821_powerdown_mode_enum),
  116. IIO_ENUM_AVAILABLE("powerdown_mode", &max5821_powerdown_mode_enum),
  117. { },
  118. };
  119. #define MAX5821_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. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE), \
  126. .ext_info = max5821_ext_info, \
  127. }
  128. static const struct iio_chan_spec max5821_channels[] = {
  129. MAX5821_CHANNEL(0),
  130. MAX5821_CHANNEL(1)
  131. };
  132. static const u8 max5821_read_dac_command[] = {
  133. MAX5821_READ_DAC_A_COMMAND,
  134. MAX5821_READ_DAC_B_COMMAND
  135. };
  136. static const u8 max5821_load_dac_command[] = {
  137. MAX5821_LOAD_DAC_A_IN_REG_B,
  138. MAX5821_LOAD_DAC_B_IN_REG_A
  139. };
  140. static int max5821_get_value(struct iio_dev *indio_dev,
  141. int *val, int channel)
  142. {
  143. struct max5821_data *data = iio_priv(indio_dev);
  144. struct i2c_client *client = data->client;
  145. u8 outbuf[1];
  146. u8 inbuf[2];
  147. int ret;
  148. if ((channel != 0) && (channel != 1))
  149. return -EINVAL;
  150. outbuf[0] = max5821_read_dac_command[channel];
  151. mutex_lock(&data->lock);
  152. ret = i2c_master_send(client, outbuf, 1);
  153. if (ret < 0) {
  154. mutex_unlock(&data->lock);
  155. return ret;
  156. } else if (ret != 1) {
  157. mutex_unlock(&data->lock);
  158. return -EIO;
  159. }
  160. ret = i2c_master_recv(client, inbuf, 2);
  161. if (ret < 0) {
  162. mutex_unlock(&data->lock);
  163. return ret;
  164. } else if (ret != 2) {
  165. mutex_unlock(&data->lock);
  166. return -EIO;
  167. }
  168. mutex_unlock(&data->lock);
  169. *val = ((inbuf[0] & 0x0f) << 6) | (inbuf[1] >> 2);
  170. return IIO_VAL_INT;
  171. }
  172. static int max5821_set_value(struct iio_dev *indio_dev,
  173. int val, int channel)
  174. {
  175. struct max5821_data *data = iio_priv(indio_dev);
  176. struct i2c_client *client = data->client;
  177. u8 outbuf[2];
  178. int ret;
  179. if ((val < 0) || (val > 1023))
  180. return -EINVAL;
  181. if ((channel != 0) && (channel != 1))
  182. return -EINVAL;
  183. outbuf[0] = max5821_load_dac_command[channel];
  184. outbuf[0] |= val >> 6;
  185. outbuf[1] = (val & 0x3f) << 2;
  186. ret = i2c_master_send(client, outbuf, 2);
  187. if (ret < 0)
  188. return ret;
  189. else if (ret != 2)
  190. return -EIO;
  191. else
  192. return 0;
  193. }
  194. static int max5821_read_raw(struct iio_dev *indio_dev,
  195. struct iio_chan_spec const *chan,
  196. int *val, int *val2, long mask)
  197. {
  198. struct max5821_data *data = iio_priv(indio_dev);
  199. switch (mask) {
  200. case IIO_CHAN_INFO_RAW:
  201. return max5821_get_value(indio_dev, val, chan->channel);
  202. case IIO_CHAN_INFO_SCALE:
  203. *val = data->vref_mv;
  204. *val2 = 10;
  205. return IIO_VAL_FRACTIONAL_LOG2;
  206. default:
  207. return -EINVAL;
  208. }
  209. }
  210. static int max5821_write_raw(struct iio_dev *indio_dev,
  211. struct iio_chan_spec const *chan,
  212. int val, int val2, long mask)
  213. {
  214. if (val2 != 0)
  215. return -EINVAL;
  216. switch (mask) {
  217. case IIO_CHAN_INFO_RAW:
  218. return max5821_set_value(indio_dev, val, chan->channel);
  219. default:
  220. return -EINVAL;
  221. }
  222. }
  223. #ifdef CONFIG_PM_SLEEP
  224. static int max5821_suspend(struct device *dev)
  225. {
  226. u8 outbuf[2] = { MAX5821_EXTENDED_COMMAND_MODE,
  227. MAX5821_EXTENDED_DAC_A |
  228. MAX5821_EXTENDED_DAC_B |
  229. MAX5821_EXTENDED_POWER_DOWN_MODE2 };
  230. return i2c_master_send(to_i2c_client(dev), outbuf, 2);
  231. }
  232. static int max5821_resume(struct device *dev)
  233. {
  234. u8 outbuf[2] = { MAX5821_EXTENDED_COMMAND_MODE,
  235. MAX5821_EXTENDED_DAC_A |
  236. MAX5821_EXTENDED_DAC_B |
  237. MAX5821_EXTENDED_POWER_UP };
  238. return i2c_master_send(to_i2c_client(dev), outbuf, 2);
  239. }
  240. static SIMPLE_DEV_PM_OPS(max5821_pm_ops, max5821_suspend, max5821_resume);
  241. #define MAX5821_PM_OPS (&max5821_pm_ops)
  242. #else
  243. #define MAX5821_PM_OPS NULL
  244. #endif /* CONFIG_PM_SLEEP */
  245. static const struct iio_info max5821_info = {
  246. .read_raw = max5821_read_raw,
  247. .write_raw = max5821_write_raw,
  248. .driver_module = THIS_MODULE,
  249. };
  250. static int max5821_probe(struct i2c_client *client,
  251. const struct i2c_device_id *id)
  252. {
  253. struct max5821_data *data;
  254. struct iio_dev *indio_dev;
  255. u32 tmp;
  256. int ret;
  257. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  258. if (!indio_dev)
  259. return -ENOMEM;
  260. data = iio_priv(indio_dev);
  261. i2c_set_clientdata(client, indio_dev);
  262. data->client = client;
  263. mutex_init(&data->lock);
  264. /* max5821 start in powerdown mode 100Kohm to ground */
  265. for (tmp = 0; tmp < MAX5821_MAX_DAC_CHANNELS; tmp++) {
  266. data->powerdown[tmp] = true;
  267. data->powerdown_mode[tmp] = MAX5821_100KOHM_TO_GND;
  268. }
  269. data->vref_reg = devm_regulator_get(&client->dev, "vref");
  270. if (IS_ERR(data->vref_reg)) {
  271. ret = PTR_ERR(data->vref_reg);
  272. dev_err(&client->dev,
  273. "Failed to get vref regulator: %d\n", ret);
  274. goto error_free_reg;
  275. }
  276. ret = regulator_enable(data->vref_reg);
  277. if (ret) {
  278. dev_err(&client->dev,
  279. "Failed to enable vref regulator: %d\n", ret);
  280. goto error_free_reg;
  281. }
  282. ret = regulator_get_voltage(data->vref_reg);
  283. if (ret < 0) {
  284. dev_err(&client->dev,
  285. "Failed to get voltage on regulator: %d\n", ret);
  286. goto error_disable_reg;
  287. }
  288. data->vref_mv = ret / 1000;
  289. indio_dev->name = id->name;
  290. indio_dev->dev.parent = &client->dev;
  291. indio_dev->num_channels = ARRAY_SIZE(max5821_channels);
  292. indio_dev->channels = max5821_channels;
  293. indio_dev->modes = INDIO_DIRECT_MODE;
  294. indio_dev->info = &max5821_info;
  295. return iio_device_register(indio_dev);
  296. error_disable_reg:
  297. regulator_disable(data->vref_reg);
  298. error_free_reg:
  299. return ret;
  300. }
  301. static int max5821_remove(struct i2c_client *client)
  302. {
  303. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  304. struct max5821_data *data = iio_priv(indio_dev);
  305. iio_device_unregister(indio_dev);
  306. regulator_disable(data->vref_reg);
  307. return 0;
  308. }
  309. static const struct i2c_device_id max5821_id[] = {
  310. { "max5821", ID_MAX5821 },
  311. { }
  312. };
  313. MODULE_DEVICE_TABLE(i2c, max5821_id);
  314. static const struct of_device_id max5821_of_match[] = {
  315. { .compatible = "maxim,max5821" },
  316. { }
  317. };
  318. MODULE_DEVICE_TABLE(of, max5821_of_match);
  319. static struct i2c_driver max5821_driver = {
  320. .driver = {
  321. .name = "max5821",
  322. .pm = MAX5821_PM_OPS,
  323. },
  324. .probe = max5821_probe,
  325. .remove = max5821_remove,
  326. .id_table = max5821_id,
  327. };
  328. module_i2c_driver(max5821_driver);
  329. MODULE_AUTHOR("Philippe Reynes <tremyfr@yahoo.fr>");
  330. MODULE_DESCRIPTION("MAX5821 DAC");
  331. MODULE_LICENSE("GPL v2");