2 * mcp4725.c - Support for Microchip MCP4725
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/init.h>
19 #include <linux/i2c.h>
20 #include <linux/err.h>
21 #include <linux/delay.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
26 #include <linux/iio/dac/mcp4725.h>
28 #define MCP4725_DRV_NAME "mcp4725"
31 struct i2c_client *client;
36 #ifdef CONFIG_PM_SLEEP
37 static int mcp4725_suspend(struct device *dev)
41 outbuf[0] = 0x3 << 4; /* power-down bits, 500 kOhm resistor */
44 return i2c_master_send(to_i2c_client(dev), outbuf, 2);
47 static int mcp4725_resume(struct device *dev)
49 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
50 struct mcp4725_data *data = iio_priv(indio_dev);
53 /* restore previous DAC value */
54 outbuf[0] = (data->dac_value >> 8) & 0xf;
55 outbuf[1] = data->dac_value & 0xff;
57 return i2c_master_send(to_i2c_client(dev), outbuf, 2);
60 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume);
61 #define MCP4725_PM_OPS (&mcp4725_pm_ops)
63 #define MCP4725_PM_OPS NULL
66 static int mcp4725_store_eeprom(struct device *dev,
67 struct device_attribute *attr, const char *buf, size_t len)
69 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
70 struct mcp4725_data *data = iio_priv(indio_dev);
76 ret = strtobool(buf, &state);
83 inoutbuf[0] = 0x60; /* write EEPROM */
84 inoutbuf[1] = data->dac_value >> 4;
85 inoutbuf[2] = (data->dac_value & 0xf) << 4;
87 ret = i2c_master_send(data->client, inoutbuf, 3);
93 /* wait for write complete, takes up to 50ms */
96 ret = i2c_master_recv(data->client, inoutbuf, 3);
102 if (inoutbuf[0] & 0x80)
107 dev_err(&data->client->dev,
108 "mcp4725_store_eeprom() failed, incomplete\n");
115 static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
117 static struct attribute *mcp4725_attributes[] = {
118 &iio_dev_attr_store_eeprom.dev_attr.attr,
122 static const struct attribute_group mcp4725_attribute_group = {
123 .attrs = mcp4725_attributes,
126 static const struct iio_chan_spec mcp4725_channel = {
131 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
132 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
133 .scan_type = IIO_ST('u', 12, 16, 0),
136 static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
138 struct mcp4725_data *data = iio_priv(indio_dev);
142 if (val >= (1 << 12) || val < 0)
145 outbuf[0] = (val >> 8) & 0xf;
146 outbuf[1] = val & 0xff;
148 ret = i2c_master_send(data->client, outbuf, 2);
157 static int mcp4725_read_raw(struct iio_dev *indio_dev,
158 struct iio_chan_spec const *chan,
159 int *val, int *val2, long mask)
161 struct mcp4725_data *data = iio_priv(indio_dev);
162 unsigned long scale_uv;
165 case IIO_CHAN_INFO_RAW:
166 *val = data->dac_value;
168 case IIO_CHAN_INFO_SCALE:
169 scale_uv = (data->vref_mv * 1000) >> 12;
170 *val = scale_uv / 1000000;
171 *val2 = scale_uv % 1000000;
172 return IIO_VAL_INT_PLUS_MICRO;
177 static int mcp4725_write_raw(struct iio_dev *indio_dev,
178 struct iio_chan_spec const *chan,
179 int val, int val2, long mask)
181 struct mcp4725_data *data = iio_priv(indio_dev);
185 case IIO_CHAN_INFO_RAW:
186 ret = mcp4725_set_value(indio_dev, val);
187 data->dac_value = val;
197 static const struct iio_info mcp4725_info = {
198 .read_raw = mcp4725_read_raw,
199 .write_raw = mcp4725_write_raw,
200 .attrs = &mcp4725_attribute_group,
201 .driver_module = THIS_MODULE,
204 static int mcp4725_probe(struct i2c_client *client,
205 const struct i2c_device_id *id)
207 struct mcp4725_data *data;
208 struct iio_dev *indio_dev;
209 struct mcp4725_platform_data *platform_data = client->dev.platform_data;
213 if (!platform_data || !platform_data->vref_mv) {
214 dev_err(&client->dev, "invalid platform data");
219 indio_dev = iio_device_alloc(sizeof(*data));
220 if (indio_dev == NULL) {
224 data = iio_priv(indio_dev);
225 i2c_set_clientdata(client, indio_dev);
226 data->client = client;
228 indio_dev->dev.parent = &client->dev;
229 indio_dev->info = &mcp4725_info;
230 indio_dev->channels = &mcp4725_channel;
231 indio_dev->num_channels = 1;
232 indio_dev->modes = INDIO_DIRECT_MODE;
234 data->vref_mv = platform_data->vref_mv;
236 /* read current DAC value */
237 err = i2c_master_recv(client, inbuf, 3);
239 dev_err(&client->dev, "failed to read DAC value");
240 goto exit_free_device;
242 data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
244 err = iio_device_register(indio_dev);
246 goto exit_free_device;
248 dev_info(&client->dev, "MCP4725 DAC registered\n");
253 iio_device_free(indio_dev);
258 static int mcp4725_remove(struct i2c_client *client)
260 struct iio_dev *indio_dev = i2c_get_clientdata(client);
262 iio_device_unregister(indio_dev);
263 iio_device_free(indio_dev);
268 static const struct i2c_device_id mcp4725_id[] = {
272 MODULE_DEVICE_TABLE(i2c, mcp4725_id);
274 static struct i2c_driver mcp4725_driver = {
276 .name = MCP4725_DRV_NAME,
277 .pm = MCP4725_PM_OPS,
279 .probe = mcp4725_probe,
280 .remove = mcp4725_remove,
281 .id_table = mcp4725_id,
283 module_i2c_driver(mcp4725_driver);
285 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
286 MODULE_DESCRIPTION("MCP4725 12-bit DAC");
287 MODULE_LICENSE("GPL");