mcp4725.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * mcp4725.c - Support for Microchip MCP4725/6
  3. *
  4. * Copyright (C) 2012 Peter Meerwald <pmeerw@pmeerw.net>
  5. *
  6. * Based on max517 by Roland Stigge <stigge@antcom.de>
  7. *
  8. * This file is subject to the terms and conditions of version 2 of
  9. * the GNU General Public License. See the file COPYING in the main
  10. * directory of this archive for more details.
  11. *
  12. * driver for the Microchip I2C 12-bit digital-to-analog converter (DAC)
  13. * (7-bit I2C slave address 0x60, the three LSBs can be configured in
  14. * hardware)
  15. */
  16. #include <linux/module.h>
  17. #include <linux/i2c.h>
  18. #include <linux/err.h>
  19. #include <linux/delay.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/sysfs.h>
  22. #include <linux/iio/dac/mcp4725.h>
  23. #define MCP4725_DRV_NAME "mcp4725"
  24. struct mcp4725_data {
  25. struct i2c_client *client;
  26. u16 vref_mv;
  27. u16 dac_value;
  28. bool powerdown;
  29. unsigned powerdown_mode;
  30. };
  31. static int mcp4725_suspend(struct device *dev)
  32. {
  33. struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
  34. to_i2c_client(dev)));
  35. u8 outbuf[2];
  36. outbuf[0] = (data->powerdown_mode + 1) << 4;
  37. outbuf[1] = 0;
  38. data->powerdown = true;
  39. return i2c_master_send(data->client, outbuf, 2);
  40. }
  41. static int mcp4725_resume(struct device *dev)
  42. {
  43. struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
  44. to_i2c_client(dev)));
  45. u8 outbuf[2];
  46. /* restore previous DAC value */
  47. outbuf[0] = (data->dac_value >> 8) & 0xf;
  48. outbuf[1] = data->dac_value & 0xff;
  49. data->powerdown = false;
  50. return i2c_master_send(data->client, outbuf, 2);
  51. }
  52. #ifdef CONFIG_PM_SLEEP
  53. static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume);
  54. #define MCP4725_PM_OPS (&mcp4725_pm_ops)
  55. #else
  56. #define MCP4725_PM_OPS NULL
  57. #endif
  58. static ssize_t mcp4725_store_eeprom(struct device *dev,
  59. struct device_attribute *attr, const char *buf, size_t len)
  60. {
  61. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  62. struct mcp4725_data *data = iio_priv(indio_dev);
  63. int tries = 20;
  64. u8 inoutbuf[3];
  65. bool state;
  66. int ret;
  67. ret = strtobool(buf, &state);
  68. if (ret < 0)
  69. return ret;
  70. if (!state)
  71. return 0;
  72. inoutbuf[0] = 0x60; /* write EEPROM */
  73. inoutbuf[1] = data->dac_value >> 4;
  74. inoutbuf[2] = (data->dac_value & 0xf) << 4;
  75. ret = i2c_master_send(data->client, inoutbuf, 3);
  76. if (ret < 0)
  77. return ret;
  78. else if (ret != 3)
  79. return -EIO;
  80. /* wait for write complete, takes up to 50ms */
  81. while (tries--) {
  82. msleep(20);
  83. ret = i2c_master_recv(data->client, inoutbuf, 3);
  84. if (ret < 0)
  85. return ret;
  86. else if (ret != 3)
  87. return -EIO;
  88. if (inoutbuf[0] & 0x80)
  89. break;
  90. }
  91. if (tries < 0) {
  92. dev_err(&data->client->dev,
  93. "mcp4725_store_eeprom() failed, incomplete\n");
  94. return -EIO;
  95. }
  96. return len;
  97. }
  98. static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
  99. static struct attribute *mcp4725_attributes[] = {
  100. &iio_dev_attr_store_eeprom.dev_attr.attr,
  101. NULL,
  102. };
  103. static const struct attribute_group mcp4725_attribute_group = {
  104. .attrs = mcp4725_attributes,
  105. };
  106. static const char * const mcp4725_powerdown_modes[] = {
  107. "1kohm_to_gnd",
  108. "100kohm_to_gnd",
  109. "500kohm_to_gnd"
  110. };
  111. static const char * const mcp4726_powerdown_modes[] = {
  112. "1kohm_to_gnd",
  113. "125kohm_to_gnd",
  114. "640kohm_to_gnd"
  115. };
  116. static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev,
  117. const struct iio_chan_spec *chan)
  118. {
  119. struct mcp4725_data *data = iio_priv(indio_dev);
  120. return data->powerdown_mode;
  121. }
  122. static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev,
  123. const struct iio_chan_spec *chan, unsigned mode)
  124. {
  125. struct mcp4725_data *data = iio_priv(indio_dev);
  126. data->powerdown_mode = mode;
  127. return 0;
  128. }
  129. static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev,
  130. uintptr_t private, const struct iio_chan_spec *chan, char *buf)
  131. {
  132. struct mcp4725_data *data = iio_priv(indio_dev);
  133. return sprintf(buf, "%d\n", data->powerdown);
  134. }
  135. static ssize_t mcp4725_write_powerdown(struct iio_dev *indio_dev,
  136. uintptr_t private, const struct iio_chan_spec *chan,
  137. const char *buf, size_t len)
  138. {
  139. struct mcp4725_data *data = iio_priv(indio_dev);
  140. bool state;
  141. int ret;
  142. ret = strtobool(buf, &state);
  143. if (ret)
  144. return ret;
  145. if (state)
  146. ret = mcp4725_suspend(&data->client->dev);
  147. else
  148. ret = mcp4725_resume(&data->client->dev);
  149. if (ret < 0)
  150. return ret;
  151. return len;
  152. }
  153. enum {
  154. MCP4725,
  155. MCP4726,
  156. };
  157. static const struct iio_enum mcp472x_powerdown_mode_enum[] = {
  158. [MCP4725] = {
  159. .items = mcp4725_powerdown_modes,
  160. .num_items = ARRAY_SIZE(mcp4725_powerdown_modes),
  161. .get = mcp4725_get_powerdown_mode,
  162. .set = mcp4725_set_powerdown_mode,
  163. },
  164. [MCP4726] = {
  165. .items = mcp4726_powerdown_modes,
  166. .num_items = ARRAY_SIZE(mcp4726_powerdown_modes),
  167. .get = mcp4725_get_powerdown_mode,
  168. .set = mcp4725_set_powerdown_mode,
  169. },
  170. };
  171. static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = {
  172. {
  173. .name = "powerdown",
  174. .read = mcp4725_read_powerdown,
  175. .write = mcp4725_write_powerdown,
  176. .shared = IIO_SEPARATE,
  177. },
  178. IIO_ENUM("powerdown_mode", IIO_SEPARATE,
  179. &mcp472x_powerdown_mode_enum[MCP4725]),
  180. IIO_ENUM_AVAILABLE("powerdown_mode",
  181. &mcp472x_powerdown_mode_enum[MCP4725]),
  182. { },
  183. };
  184. static const struct iio_chan_spec_ext_info mcp4726_ext_info[] = {
  185. {
  186. .name = "powerdown",
  187. .read = mcp4725_read_powerdown,
  188. .write = mcp4725_write_powerdown,
  189. .shared = IIO_SEPARATE,
  190. },
  191. IIO_ENUM("powerdown_mode", IIO_SEPARATE,
  192. &mcp472x_powerdown_mode_enum[MCP4726]),
  193. IIO_ENUM_AVAILABLE("powerdown_mode",
  194. &mcp472x_powerdown_mode_enum[MCP4726]),
  195. { },
  196. };
  197. static const struct iio_chan_spec mcp472x_channel[] = {
  198. [MCP4725] = {
  199. .type = IIO_VOLTAGE,
  200. .indexed = 1,
  201. .output = 1,
  202. .channel = 0,
  203. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  204. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  205. .ext_info = mcp4725_ext_info,
  206. },
  207. [MCP4726] = {
  208. .type = IIO_VOLTAGE,
  209. .indexed = 1,
  210. .output = 1,
  211. .channel = 0,
  212. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  213. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  214. .ext_info = mcp4726_ext_info,
  215. },
  216. };
  217. static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
  218. {
  219. struct mcp4725_data *data = iio_priv(indio_dev);
  220. u8 outbuf[2];
  221. int ret;
  222. if (val >= (1 << 12) || val < 0)
  223. return -EINVAL;
  224. outbuf[0] = (val >> 8) & 0xf;
  225. outbuf[1] = val & 0xff;
  226. ret = i2c_master_send(data->client, outbuf, 2);
  227. if (ret < 0)
  228. return ret;
  229. else if (ret != 2)
  230. return -EIO;
  231. else
  232. return 0;
  233. }
  234. static int mcp4725_read_raw(struct iio_dev *indio_dev,
  235. struct iio_chan_spec const *chan,
  236. int *val, int *val2, long mask)
  237. {
  238. struct mcp4725_data *data = iio_priv(indio_dev);
  239. switch (mask) {
  240. case IIO_CHAN_INFO_RAW:
  241. *val = data->dac_value;
  242. return IIO_VAL_INT;
  243. case IIO_CHAN_INFO_SCALE:
  244. *val = data->vref_mv;
  245. *val2 = 12;
  246. return IIO_VAL_FRACTIONAL_LOG2;
  247. }
  248. return -EINVAL;
  249. }
  250. static int mcp4725_write_raw(struct iio_dev *indio_dev,
  251. struct iio_chan_spec const *chan,
  252. int val, int val2, long mask)
  253. {
  254. struct mcp4725_data *data = iio_priv(indio_dev);
  255. int ret;
  256. switch (mask) {
  257. case IIO_CHAN_INFO_RAW:
  258. ret = mcp4725_set_value(indio_dev, val);
  259. data->dac_value = val;
  260. break;
  261. default:
  262. ret = -EINVAL;
  263. break;
  264. }
  265. return ret;
  266. }
  267. static const struct iio_info mcp4725_info = {
  268. .read_raw = mcp4725_read_raw,
  269. .write_raw = mcp4725_write_raw,
  270. .attrs = &mcp4725_attribute_group,
  271. .driver_module = THIS_MODULE,
  272. };
  273. static int mcp4725_probe(struct i2c_client *client,
  274. const struct i2c_device_id *id)
  275. {
  276. struct mcp4725_data *data;
  277. struct iio_dev *indio_dev;
  278. struct mcp4725_platform_data *platform_data = client->dev.platform_data;
  279. u8 inbuf[3];
  280. u8 pd;
  281. int err;
  282. if (!platform_data || !platform_data->vref_mv) {
  283. dev_err(&client->dev, "invalid platform data");
  284. return -EINVAL;
  285. }
  286. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  287. if (indio_dev == NULL)
  288. return -ENOMEM;
  289. data = iio_priv(indio_dev);
  290. i2c_set_clientdata(client, indio_dev);
  291. data->client = client;
  292. indio_dev->dev.parent = &client->dev;
  293. indio_dev->name = id->name;
  294. indio_dev->info = &mcp4725_info;
  295. indio_dev->channels = &mcp472x_channel[id->driver_data];
  296. indio_dev->num_channels = 1;
  297. indio_dev->modes = INDIO_DIRECT_MODE;
  298. data->vref_mv = platform_data->vref_mv;
  299. /* read current DAC value */
  300. err = i2c_master_recv(client, inbuf, 3);
  301. if (err < 0) {
  302. dev_err(&client->dev, "failed to read DAC value");
  303. return err;
  304. }
  305. pd = (inbuf[0] >> 1) & 0x3;
  306. data->powerdown = pd > 0 ? true : false;
  307. data->powerdown_mode = pd ? pd - 1 : 2; /* largest register to gnd */
  308. data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
  309. return iio_device_register(indio_dev);
  310. }
  311. static int mcp4725_remove(struct i2c_client *client)
  312. {
  313. iio_device_unregister(i2c_get_clientdata(client));
  314. return 0;
  315. }
  316. static const struct i2c_device_id mcp4725_id[] = {
  317. { "mcp4725", MCP4725 },
  318. { "mcp4726", MCP4726 },
  319. { }
  320. };
  321. MODULE_DEVICE_TABLE(i2c, mcp4725_id);
  322. static struct i2c_driver mcp4725_driver = {
  323. .driver = {
  324. .name = MCP4725_DRV_NAME,
  325. .pm = MCP4725_PM_OPS,
  326. },
  327. .probe = mcp4725_probe,
  328. .remove = mcp4725_remove,
  329. .id_table = mcp4725_id,
  330. };
  331. module_i2c_driver(mcp4725_driver);
  332. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  333. MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
  334. MODULE_LICENSE("GPL");