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>
21 #include <linux/regulator/consumer.h>
22 #include <linux/of_device.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
28 #include <linux/iio/dac/mcp4725.h>
30 #define MCP4725_DRV_NAME "mcp4725"
32 #define MCP472X_REF_VDD 0x00
33 #define MCP472X_REF_VREF_UNBUFFERED 0x02
34 #define MCP472X_REF_VREF_BUFFERED 0x03
37 struct i2c_client *client;
43 unsigned powerdown_mode;
44 struct regulator *vdd_reg;
45 struct regulator *vref_reg;
48 static int mcp4725_suspend(struct device *dev)
50 struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
55 outbuf[0] = (data->powerdown_mode + 1) << 4;
57 data->powerdown = true;
59 ret = i2c_master_send(data->client, outbuf, 2);
67 static int mcp4725_resume(struct device *dev)
69 struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
74 /* restore previous DAC value */
75 outbuf[0] = (data->dac_value >> 8) & 0xf;
76 outbuf[1] = data->dac_value & 0xff;
77 data->powerdown = false;
79 ret = i2c_master_send(data->client, outbuf, 2);
87 #ifdef CONFIG_PM_SLEEP
88 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume);
89 #define MCP4725_PM_OPS (&mcp4725_pm_ops)
91 #define MCP4725_PM_OPS NULL
94 static ssize_t mcp4725_store_eeprom(struct device *dev,
95 struct device_attribute *attr, const char *buf, size_t len)
97 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
98 struct mcp4725_data *data = iio_priv(indio_dev);
104 ret = strtobool(buf, &state);
111 inoutbuf[0] = 0x60; /* write EEPROM */
112 inoutbuf[0] |= data->ref_mode << 3;
113 inoutbuf[0] |= data->powerdown ? ((data->powerdown_mode + 1) << 1) : 0;
114 inoutbuf[1] = data->dac_value >> 4;
115 inoutbuf[2] = (data->dac_value & 0xf) << 4;
117 ret = i2c_master_send(data->client, inoutbuf, 3);
123 /* wait for write complete, takes up to 50ms */
126 ret = i2c_master_recv(data->client, inoutbuf, 3);
132 if (inoutbuf[0] & 0x80)
137 dev_err(&data->client->dev,
138 "mcp4725_store_eeprom() failed, incomplete\n");
145 static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
147 static struct attribute *mcp4725_attributes[] = {
148 &iio_dev_attr_store_eeprom.dev_attr.attr,
152 static const struct attribute_group mcp4725_attribute_group = {
153 .attrs = mcp4725_attributes,
156 static const char * const mcp4725_powerdown_modes[] = {
162 static const char * const mcp4726_powerdown_modes[] = {
168 static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev,
169 const struct iio_chan_spec *chan)
171 struct mcp4725_data *data = iio_priv(indio_dev);
173 return data->powerdown_mode;
176 static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev,
177 const struct iio_chan_spec *chan, unsigned mode)
179 struct mcp4725_data *data = iio_priv(indio_dev);
181 data->powerdown_mode = mode;
186 static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev,
187 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
189 struct mcp4725_data *data = iio_priv(indio_dev);
191 return sprintf(buf, "%d\n", data->powerdown);
194 static ssize_t mcp4725_write_powerdown(struct iio_dev *indio_dev,
195 uintptr_t private, const struct iio_chan_spec *chan,
196 const char *buf, size_t len)
198 struct mcp4725_data *data = iio_priv(indio_dev);
202 ret = strtobool(buf, &state);
207 ret = mcp4725_suspend(&data->client->dev);
209 ret = mcp4725_resume(&data->client->dev);
221 static const struct iio_enum mcp472x_powerdown_mode_enum[] = {
223 .items = mcp4725_powerdown_modes,
224 .num_items = ARRAY_SIZE(mcp4725_powerdown_modes),
225 .get = mcp4725_get_powerdown_mode,
226 .set = mcp4725_set_powerdown_mode,
229 .items = mcp4726_powerdown_modes,
230 .num_items = ARRAY_SIZE(mcp4726_powerdown_modes),
231 .get = mcp4725_get_powerdown_mode,
232 .set = mcp4725_set_powerdown_mode,
236 static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = {
239 .read = mcp4725_read_powerdown,
240 .write = mcp4725_write_powerdown,
241 .shared = IIO_SEPARATE,
243 IIO_ENUM("powerdown_mode", IIO_SEPARATE,
244 &mcp472x_powerdown_mode_enum[MCP4725]),
245 IIO_ENUM_AVAILABLE("powerdown_mode",
246 &mcp472x_powerdown_mode_enum[MCP4725]),
250 static const struct iio_chan_spec_ext_info mcp4726_ext_info[] = {
253 .read = mcp4725_read_powerdown,
254 .write = mcp4725_write_powerdown,
255 .shared = IIO_SEPARATE,
257 IIO_ENUM("powerdown_mode", IIO_SEPARATE,
258 &mcp472x_powerdown_mode_enum[MCP4726]),
259 IIO_ENUM_AVAILABLE("powerdown_mode",
260 &mcp472x_powerdown_mode_enum[MCP4726]),
264 static const struct iio_chan_spec mcp472x_channel[] = {
270 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
271 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
272 .ext_info = mcp4725_ext_info,
279 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
280 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
281 .ext_info = mcp4726_ext_info,
285 static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
287 struct mcp4725_data *data = iio_priv(indio_dev);
291 if (val >= (1 << 12) || val < 0)
294 outbuf[0] = (val >> 8) & 0xf;
295 outbuf[1] = val & 0xff;
297 ret = i2c_master_send(data->client, outbuf, 2);
306 static int mcp4726_set_cfg(struct iio_dev *indio_dev)
308 struct mcp4725_data *data = iio_priv(indio_dev);
313 outbuf[0] |= data->ref_mode << 3;
315 outbuf[0] |= data->powerdown << 1;
316 outbuf[1] = data->dac_value >> 4;
317 outbuf[2] = (data->dac_value & 0xf) << 4;
319 ret = i2c_master_send(data->client, outbuf, 3);
328 static int mcp4725_read_raw(struct iio_dev *indio_dev,
329 struct iio_chan_spec const *chan,
330 int *val, int *val2, long mask)
332 struct mcp4725_data *data = iio_priv(indio_dev);
336 case IIO_CHAN_INFO_RAW:
337 *val = data->dac_value;
339 case IIO_CHAN_INFO_SCALE:
340 if (data->ref_mode == MCP472X_REF_VDD)
341 ret = regulator_get_voltage(data->vdd_reg);
343 ret = regulator_get_voltage(data->vref_reg);
350 return IIO_VAL_FRACTIONAL_LOG2;
355 static int mcp4725_write_raw(struct iio_dev *indio_dev,
356 struct iio_chan_spec const *chan,
357 int val, int val2, long mask)
359 struct mcp4725_data *data = iio_priv(indio_dev);
363 case IIO_CHAN_INFO_RAW:
364 ret = mcp4725_set_value(indio_dev, val);
365 data->dac_value = val;
375 static const struct iio_info mcp4725_info = {
376 .read_raw = mcp4725_read_raw,
377 .write_raw = mcp4725_write_raw,
378 .attrs = &mcp4725_attribute_group,
382 static int mcp4725_probe_dt(struct device *dev,
383 struct mcp4725_platform_data *pdata)
385 struct device_node *np = dev->of_node;
390 /* check if is the vref-supply defined */
391 pdata->use_vref = of_property_read_bool(np, "vref-supply");
392 pdata->vref_buffered =
393 of_property_read_bool(np, "microchip,vref-buffered");
398 static int mcp4725_probe_dt(struct device *dev,
399 struct mcp4725_platform_data *platform_data)
405 static int mcp4725_probe(struct i2c_client *client,
406 const struct i2c_device_id *id)
408 struct mcp4725_data *data;
409 struct iio_dev *indio_dev;
410 struct mcp4725_platform_data *pdata, pdata_dt;
416 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
417 if (indio_dev == NULL)
419 data = iio_priv(indio_dev);
420 i2c_set_clientdata(client, indio_dev);
421 data->client = client;
422 if (client->dev.of_node)
423 data->id = (enum chip_id)of_device_get_match_data(&client->dev);
425 data->id = id->driver_data;
426 pdata = dev_get_platdata(&client->dev);
429 err = mcp4725_probe_dt(&client->dev, &pdata_dt);
431 dev_err(&client->dev,
432 "invalid platform or devicetree data");
438 if (data->id == MCP4725 && pdata->use_vref) {
439 dev_err(&client->dev,
440 "external reference is unavailable on MCP4725");
444 if (!pdata->use_vref && pdata->vref_buffered) {
445 dev_err(&client->dev,
446 "buffering is unavailable on the internal reference");
450 if (!pdata->use_vref)
451 data->ref_mode = MCP472X_REF_VDD;
453 data->ref_mode = pdata->vref_buffered ?
454 MCP472X_REF_VREF_BUFFERED :
455 MCP472X_REF_VREF_UNBUFFERED;
457 data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
458 if (IS_ERR(data->vdd_reg))
459 return PTR_ERR(data->vdd_reg);
461 err = regulator_enable(data->vdd_reg);
465 if (pdata->use_vref) {
466 data->vref_reg = devm_regulator_get(&client->dev, "vref");
467 if (IS_ERR(data->vref_reg)) {
468 err = PTR_ERR(data->vref_reg);
469 goto err_disable_vdd_reg;
472 err = regulator_enable(data->vref_reg);
474 goto err_disable_vdd_reg;
477 indio_dev->dev.parent = &client->dev;
478 indio_dev->name = id->name;
479 indio_dev->info = &mcp4725_info;
480 indio_dev->channels = &mcp472x_channel[id->driver_data];
481 indio_dev->num_channels = 1;
482 indio_dev->modes = INDIO_DIRECT_MODE;
484 /* read current DAC value and settings */
485 err = i2c_master_recv(client, inbuf, data->id == MCP4725 ? 3 : 4);
488 dev_err(&client->dev, "failed to read DAC value");
489 goto err_disable_vref_reg;
491 pd = (inbuf[0] >> 1) & 0x3;
492 data->powerdown = pd > 0;
493 data->powerdown_mode = pd ? pd - 1 : 2; /* largest resistor to gnd */
494 data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
495 if (data->id == MCP4726)
496 ref = (inbuf[3] >> 3) & 0x3;
498 if (data->id == MCP4726 && ref != data->ref_mode) {
499 dev_info(&client->dev,
500 "voltage reference mode differs (conf: %u, eeprom: %u), setting %u",
501 data->ref_mode, ref, data->ref_mode);
502 err = mcp4726_set_cfg(indio_dev);
504 goto err_disable_vref_reg;
507 err = iio_device_register(indio_dev);
509 goto err_disable_vref_reg;
513 err_disable_vref_reg:
515 regulator_disable(data->vref_reg);
518 regulator_disable(data->vdd_reg);
523 static int mcp4725_remove(struct i2c_client *client)
525 struct iio_dev *indio_dev = i2c_get_clientdata(client);
526 struct mcp4725_data *data = iio_priv(indio_dev);
528 iio_device_unregister(indio_dev);
531 regulator_disable(data->vref_reg);
532 regulator_disable(data->vdd_reg);
537 static const struct i2c_device_id mcp4725_id[] = {
538 { "mcp4725", MCP4725 },
539 { "mcp4726", MCP4726 },
542 MODULE_DEVICE_TABLE(i2c, mcp4725_id);
545 static const struct of_device_id mcp4725_of_match[] = {
547 .compatible = "microchip,mcp4725",
548 .data = (void *)MCP4725
551 .compatible = "microchip,mcp4726",
552 .data = (void *)MCP4726
556 MODULE_DEVICE_TABLE(of, mcp4725_of_match);
559 static struct i2c_driver mcp4725_driver = {
561 .name = MCP4725_DRV_NAME,
562 .of_match_table = of_match_ptr(mcp4725_of_match),
563 .pm = MCP4725_PM_OPS,
565 .probe = mcp4725_probe,
566 .remove = mcp4725_remove,
567 .id_table = mcp4725_id,
569 module_i2c_driver(mcp4725_driver);
571 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
572 MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
573 MODULE_LICENSE("GPL");