2 * mcp4725.c - Support for Microchip MCP4725/6
4 * Copyright (C) 2012 Peter Meerwald <pmeerw@pmeerw.net>
6 * Based on max517 by Roland Stigge <stigge@antcom.de>
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.
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
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/err.h>
20 #include <linux/delay.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
25 #include <linux/iio/dac/mcp4725.h>
27 #define MCP4725_DRV_NAME "mcp4725"
30 struct i2c_client *client;
34 unsigned powerdown_mode;
37 static int mcp4725_suspend(struct device *dev)
39 struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
43 outbuf[0] = (data->powerdown_mode + 1) << 4;
45 data->powerdown = true;
47 return i2c_master_send(data->client, outbuf, 2);
50 static int mcp4725_resume(struct device *dev)
52 struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
56 /* restore previous DAC value */
57 outbuf[0] = (data->dac_value >> 8) & 0xf;
58 outbuf[1] = data->dac_value & 0xff;
59 data->powerdown = false;
61 return i2c_master_send(data->client, outbuf, 2);
64 #ifdef CONFIG_PM_SLEEP
65 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume);
66 #define MCP4725_PM_OPS (&mcp4725_pm_ops)
68 #define MCP4725_PM_OPS NULL
71 static ssize_t mcp4725_store_eeprom(struct device *dev,
72 struct device_attribute *attr, const char *buf, size_t len)
74 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
75 struct mcp4725_data *data = iio_priv(indio_dev);
81 ret = strtobool(buf, &state);
88 inoutbuf[0] = 0x60; /* write EEPROM */
89 inoutbuf[1] = data->dac_value >> 4;
90 inoutbuf[2] = (data->dac_value & 0xf) << 4;
92 ret = i2c_master_send(data->client, inoutbuf, 3);
98 /* wait for write complete, takes up to 50ms */
101 ret = i2c_master_recv(data->client, inoutbuf, 3);
107 if (inoutbuf[0] & 0x80)
112 dev_err(&data->client->dev,
113 "mcp4725_store_eeprom() failed, incomplete\n");
120 static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
122 static struct attribute *mcp4725_attributes[] = {
123 &iio_dev_attr_store_eeprom.dev_attr.attr,
127 static const struct attribute_group mcp4725_attribute_group = {
128 .attrs = mcp4725_attributes,
131 static const char * const mcp4725_powerdown_modes[] = {
137 static const char * const mcp4726_powerdown_modes[] = {
143 static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev,
144 const struct iio_chan_spec *chan)
146 struct mcp4725_data *data = iio_priv(indio_dev);
148 return data->powerdown_mode;
151 static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev,
152 const struct iio_chan_spec *chan, unsigned mode)
154 struct mcp4725_data *data = iio_priv(indio_dev);
156 data->powerdown_mode = mode;
161 static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev,
162 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
164 struct mcp4725_data *data = iio_priv(indio_dev);
166 return sprintf(buf, "%d\n", data->powerdown);
169 static ssize_t mcp4725_write_powerdown(struct iio_dev *indio_dev,
170 uintptr_t private, const struct iio_chan_spec *chan,
171 const char *buf, size_t len)
173 struct mcp4725_data *data = iio_priv(indio_dev);
177 ret = strtobool(buf, &state);
182 ret = mcp4725_suspend(&data->client->dev);
184 ret = mcp4725_resume(&data->client->dev);
196 static const struct iio_enum mcp472x_powerdown_mode_enum[] = {
198 .items = mcp4725_powerdown_modes,
199 .num_items = ARRAY_SIZE(mcp4725_powerdown_modes),
200 .get = mcp4725_get_powerdown_mode,
201 .set = mcp4725_set_powerdown_mode,
204 .items = mcp4726_powerdown_modes,
205 .num_items = ARRAY_SIZE(mcp4726_powerdown_modes),
206 .get = mcp4725_get_powerdown_mode,
207 .set = mcp4725_set_powerdown_mode,
211 static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = {
214 .read = mcp4725_read_powerdown,
215 .write = mcp4725_write_powerdown,
216 .shared = IIO_SEPARATE,
218 IIO_ENUM("powerdown_mode", IIO_SEPARATE,
219 &mcp472x_powerdown_mode_enum[MCP4725]),
220 IIO_ENUM_AVAILABLE("powerdown_mode",
221 &mcp472x_powerdown_mode_enum[MCP4725]),
225 static const struct iio_chan_spec_ext_info mcp4726_ext_info[] = {
228 .read = mcp4725_read_powerdown,
229 .write = mcp4725_write_powerdown,
230 .shared = IIO_SEPARATE,
232 IIO_ENUM("powerdown_mode", IIO_SEPARATE,
233 &mcp472x_powerdown_mode_enum[MCP4726]),
234 IIO_ENUM_AVAILABLE("powerdown_mode",
235 &mcp472x_powerdown_mode_enum[MCP4726]),
239 static const struct iio_chan_spec mcp472x_channel[] = {
245 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
246 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
247 .ext_info = mcp4725_ext_info,
254 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
255 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
256 .ext_info = mcp4726_ext_info,
260 static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
262 struct mcp4725_data *data = iio_priv(indio_dev);
266 if (val >= (1 << 12) || val < 0)
269 outbuf[0] = (val >> 8) & 0xf;
270 outbuf[1] = val & 0xff;
272 ret = i2c_master_send(data->client, outbuf, 2);
281 static int mcp4725_read_raw(struct iio_dev *indio_dev,
282 struct iio_chan_spec const *chan,
283 int *val, int *val2, long mask)
285 struct mcp4725_data *data = iio_priv(indio_dev);
288 case IIO_CHAN_INFO_RAW:
289 *val = data->dac_value;
291 case IIO_CHAN_INFO_SCALE:
292 *val = data->vref_mv;
294 return IIO_VAL_FRACTIONAL_LOG2;
299 static int mcp4725_write_raw(struct iio_dev *indio_dev,
300 struct iio_chan_spec const *chan,
301 int val, int val2, long mask)
303 struct mcp4725_data *data = iio_priv(indio_dev);
307 case IIO_CHAN_INFO_RAW:
308 ret = mcp4725_set_value(indio_dev, val);
309 data->dac_value = val;
319 static const struct iio_info mcp4725_info = {
320 .read_raw = mcp4725_read_raw,
321 .write_raw = mcp4725_write_raw,
322 .attrs = &mcp4725_attribute_group,
323 .driver_module = THIS_MODULE,
326 static int mcp4725_probe(struct i2c_client *client,
327 const struct i2c_device_id *id)
329 struct mcp4725_data *data;
330 struct iio_dev *indio_dev;
331 struct mcp4725_platform_data *platform_data = client->dev.platform_data;
336 if (!platform_data || !platform_data->vref_mv) {
337 dev_err(&client->dev, "invalid platform data");
341 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
342 if (indio_dev == NULL)
344 data = iio_priv(indio_dev);
345 i2c_set_clientdata(client, indio_dev);
346 data->client = client;
348 indio_dev->dev.parent = &client->dev;
349 indio_dev->name = id->name;
350 indio_dev->info = &mcp4725_info;
351 indio_dev->channels = &mcp472x_channel[id->driver_data];
352 indio_dev->num_channels = 1;
353 indio_dev->modes = INDIO_DIRECT_MODE;
355 data->vref_mv = platform_data->vref_mv;
357 /* read current DAC value */
358 err = i2c_master_recv(client, inbuf, 3);
360 dev_err(&client->dev, "failed to read DAC value");
363 pd = (inbuf[0] >> 1) & 0x3;
364 data->powerdown = pd > 0 ? true : false;
365 data->powerdown_mode = pd ? pd - 1 : 2; /* largest register to gnd */
366 data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
368 return iio_device_register(indio_dev);
371 static int mcp4725_remove(struct i2c_client *client)
373 iio_device_unregister(i2c_get_clientdata(client));
377 static const struct i2c_device_id mcp4725_id[] = {
378 { "mcp4725", MCP4725 },
379 { "mcp4726", MCP4726 },
382 MODULE_DEVICE_TABLE(i2c, mcp4725_id);
384 static struct i2c_driver mcp4725_driver = {
386 .name = MCP4725_DRV_NAME,
387 .pm = MCP4725_PM_OPS,
389 .probe = mcp4725_probe,
390 .remove = mcp4725_remove,
391 .id_table = mcp4725_id,
393 module_i2c_driver(mcp4725_driver);
395 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
396 MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
397 MODULE_LICENSE("GPL");