1 // SPDX-License-Identifier: GPL-2.0-only
3 * mcp4725.c - Support for Microchip MCP4725/6
5 * Copyright (C) 2012 Peter Meerwald <pmeerw@pmeerw.net>
7 * Based on max517 by Roland Stigge <stigge@antcom.de>
9 * driver for the Microchip I2C 12-bit digital-to-analog converter (DAC)
10 * (7-bit I2C slave address 0x60, the three LSBs can be configured in
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/err.h>
17 #include <linux/delay.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/property.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"
29 #define MCP472X_REF_VDD 0x00
30 #define MCP472X_REF_VREF_UNBUFFERED 0x02
31 #define MCP472X_REF_VREF_BUFFERED 0x03
34 struct i2c_client *client;
40 unsigned powerdown_mode;
41 struct regulator *vdd_reg;
42 struct regulator *vref_reg;
45 static int __maybe_unused mcp4725_suspend(struct device *dev)
47 struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
52 outbuf[0] = (data->powerdown_mode + 1) << 4;
54 data->powerdown = true;
56 ret = i2c_master_send(data->client, outbuf, 2);
64 static int __maybe_unused mcp4725_resume(struct device *dev)
66 struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
71 /* restore previous DAC value */
72 outbuf[0] = (data->dac_value >> 8) & 0xf;
73 outbuf[1] = data->dac_value & 0xff;
74 data->powerdown = false;
76 ret = i2c_master_send(data->client, outbuf, 2);
83 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume);
85 static ssize_t mcp4725_store_eeprom(struct device *dev,
86 struct device_attribute *attr, const char *buf, size_t len)
88 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
89 struct mcp4725_data *data = iio_priv(indio_dev);
95 ret = strtobool(buf, &state);
102 inoutbuf[0] = 0x60; /* write EEPROM */
103 inoutbuf[0] |= data->ref_mode << 3;
104 inoutbuf[0] |= data->powerdown ? ((data->powerdown_mode + 1) << 1) : 0;
105 inoutbuf[1] = data->dac_value >> 4;
106 inoutbuf[2] = (data->dac_value & 0xf) << 4;
108 ret = i2c_master_send(data->client, inoutbuf, 3);
114 /* wait for write complete, takes up to 50ms */
117 ret = i2c_master_recv(data->client, inoutbuf, 3);
123 if (inoutbuf[0] & 0x80)
128 dev_err(&data->client->dev,
129 "mcp4725_store_eeprom() failed, incomplete\n");
136 static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
138 static struct attribute *mcp4725_attributes[] = {
139 &iio_dev_attr_store_eeprom.dev_attr.attr,
143 static const struct attribute_group mcp4725_attribute_group = {
144 .attrs = mcp4725_attributes,
147 static const char * const mcp4725_powerdown_modes[] = {
153 static const char * const mcp4726_powerdown_modes[] = {
159 static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev,
160 const struct iio_chan_spec *chan)
162 struct mcp4725_data *data = iio_priv(indio_dev);
164 return data->powerdown_mode;
167 static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev,
168 const struct iio_chan_spec *chan, unsigned mode)
170 struct mcp4725_data *data = iio_priv(indio_dev);
172 data->powerdown_mode = mode;
177 static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev,
178 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
180 struct mcp4725_data *data = iio_priv(indio_dev);
182 return sprintf(buf, "%d\n", data->powerdown);
185 static ssize_t mcp4725_write_powerdown(struct iio_dev *indio_dev,
186 uintptr_t private, const struct iio_chan_spec *chan,
187 const char *buf, size_t len)
189 struct mcp4725_data *data = iio_priv(indio_dev);
193 ret = strtobool(buf, &state);
198 ret = mcp4725_suspend(&data->client->dev);
200 ret = mcp4725_resume(&data->client->dev);
212 static const struct iio_enum mcp472x_powerdown_mode_enum[] = {
214 .items = mcp4725_powerdown_modes,
215 .num_items = ARRAY_SIZE(mcp4725_powerdown_modes),
216 .get = mcp4725_get_powerdown_mode,
217 .set = mcp4725_set_powerdown_mode,
220 .items = mcp4726_powerdown_modes,
221 .num_items = ARRAY_SIZE(mcp4726_powerdown_modes),
222 .get = mcp4725_get_powerdown_mode,
223 .set = mcp4725_set_powerdown_mode,
227 static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = {
230 .read = mcp4725_read_powerdown,
231 .write = mcp4725_write_powerdown,
232 .shared = IIO_SEPARATE,
234 IIO_ENUM("powerdown_mode", IIO_SEPARATE,
235 &mcp472x_powerdown_mode_enum[MCP4725]),
236 IIO_ENUM_AVAILABLE("powerdown_mode",
237 &mcp472x_powerdown_mode_enum[MCP4725]),
241 static const struct iio_chan_spec_ext_info mcp4726_ext_info[] = {
244 .read = mcp4725_read_powerdown,
245 .write = mcp4725_write_powerdown,
246 .shared = IIO_SEPARATE,
248 IIO_ENUM("powerdown_mode", IIO_SEPARATE,
249 &mcp472x_powerdown_mode_enum[MCP4726]),
250 IIO_ENUM_AVAILABLE("powerdown_mode",
251 &mcp472x_powerdown_mode_enum[MCP4726]),
255 static const struct iio_chan_spec mcp472x_channel[] = {
261 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
262 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
263 .ext_info = mcp4725_ext_info,
270 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
271 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
272 .ext_info = mcp4726_ext_info,
276 static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
278 struct mcp4725_data *data = iio_priv(indio_dev);
282 if (val >= (1 << 12) || val < 0)
285 outbuf[0] = (val >> 8) & 0xf;
286 outbuf[1] = val & 0xff;
288 ret = i2c_master_send(data->client, outbuf, 2);
297 static int mcp4726_set_cfg(struct iio_dev *indio_dev)
299 struct mcp4725_data *data = iio_priv(indio_dev);
304 outbuf[0] |= data->ref_mode << 3;
306 outbuf[0] |= data->powerdown << 1;
307 outbuf[1] = data->dac_value >> 4;
308 outbuf[2] = (data->dac_value & 0xf) << 4;
310 ret = i2c_master_send(data->client, outbuf, 3);
319 static int mcp4725_read_raw(struct iio_dev *indio_dev,
320 struct iio_chan_spec const *chan,
321 int *val, int *val2, long mask)
323 struct mcp4725_data *data = iio_priv(indio_dev);
327 case IIO_CHAN_INFO_RAW:
328 *val = data->dac_value;
330 case IIO_CHAN_INFO_SCALE:
331 if (data->ref_mode == MCP472X_REF_VDD)
332 ret = regulator_get_voltage(data->vdd_reg);
334 ret = regulator_get_voltage(data->vref_reg);
341 return IIO_VAL_FRACTIONAL_LOG2;
346 static int mcp4725_write_raw(struct iio_dev *indio_dev,
347 struct iio_chan_spec const *chan,
348 int val, int val2, long mask)
350 struct mcp4725_data *data = iio_priv(indio_dev);
354 case IIO_CHAN_INFO_RAW:
355 ret = mcp4725_set_value(indio_dev, val);
356 data->dac_value = val;
366 static const struct iio_info mcp4725_info = {
367 .read_raw = mcp4725_read_raw,
368 .write_raw = mcp4725_write_raw,
369 .attrs = &mcp4725_attribute_group,
372 static int mcp4725_probe_dt(struct device *dev,
373 struct mcp4725_platform_data *pdata)
375 /* check if is the vref-supply defined */
376 pdata->use_vref = device_property_read_bool(dev, "vref-supply");
377 pdata->vref_buffered =
378 device_property_read_bool(dev, "microchip,vref-buffered");
383 static int mcp4725_probe(struct i2c_client *client,
384 const struct i2c_device_id *id)
386 struct mcp4725_data *data;
387 struct iio_dev *indio_dev;
388 struct mcp4725_platform_data *pdata, pdata_dt;
394 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
395 if (indio_dev == NULL)
397 data = iio_priv(indio_dev);
398 i2c_set_clientdata(client, indio_dev);
399 data->client = client;
400 if (dev_fwnode(&client->dev))
401 data->id = (enum chip_id)device_get_match_data(&client->dev);
403 data->id = id->driver_data;
404 pdata = dev_get_platdata(&client->dev);
407 err = mcp4725_probe_dt(&client->dev, &pdata_dt);
409 dev_err(&client->dev,
410 "invalid platform or devicetree data");
416 if (data->id == MCP4725 && pdata->use_vref) {
417 dev_err(&client->dev,
418 "external reference is unavailable on MCP4725");
422 if (!pdata->use_vref && pdata->vref_buffered) {
423 dev_err(&client->dev,
424 "buffering is unavailable on the internal reference");
428 if (!pdata->use_vref)
429 data->ref_mode = MCP472X_REF_VDD;
431 data->ref_mode = pdata->vref_buffered ?
432 MCP472X_REF_VREF_BUFFERED :
433 MCP472X_REF_VREF_UNBUFFERED;
435 data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
436 if (IS_ERR(data->vdd_reg))
437 return PTR_ERR(data->vdd_reg);
439 err = regulator_enable(data->vdd_reg);
443 if (pdata->use_vref) {
444 data->vref_reg = devm_regulator_get(&client->dev, "vref");
445 if (IS_ERR(data->vref_reg)) {
446 err = PTR_ERR(data->vref_reg);
447 goto err_disable_vdd_reg;
450 err = regulator_enable(data->vref_reg);
452 goto err_disable_vdd_reg;
455 indio_dev->name = id->name;
456 indio_dev->info = &mcp4725_info;
457 indio_dev->channels = &mcp472x_channel[id->driver_data];
458 indio_dev->num_channels = 1;
459 indio_dev->modes = INDIO_DIRECT_MODE;
461 /* read current DAC value and settings */
462 err = i2c_master_recv(client, inbuf, data->id == MCP4725 ? 3 : 4);
465 dev_err(&client->dev, "failed to read DAC value");
466 goto err_disable_vref_reg;
468 pd = (inbuf[0] >> 1) & 0x3;
469 data->powerdown = pd > 0;
470 data->powerdown_mode = pd ? pd - 1 : 2; /* largest resistor to gnd */
471 data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
472 if (data->id == MCP4726)
473 ref = (inbuf[3] >> 3) & 0x3;
475 if (data->id == MCP4726 && ref != data->ref_mode) {
476 dev_info(&client->dev,
477 "voltage reference mode differs (conf: %u, eeprom: %u), setting %u",
478 data->ref_mode, ref, data->ref_mode);
479 err = mcp4726_set_cfg(indio_dev);
481 goto err_disable_vref_reg;
484 err = iio_device_register(indio_dev);
486 goto err_disable_vref_reg;
490 err_disable_vref_reg:
492 regulator_disable(data->vref_reg);
495 regulator_disable(data->vdd_reg);
500 static int mcp4725_remove(struct i2c_client *client)
502 struct iio_dev *indio_dev = i2c_get_clientdata(client);
503 struct mcp4725_data *data = iio_priv(indio_dev);
505 iio_device_unregister(indio_dev);
508 regulator_disable(data->vref_reg);
509 regulator_disable(data->vdd_reg);
514 static const struct i2c_device_id mcp4725_id[] = {
515 { "mcp4725", MCP4725 },
516 { "mcp4726", MCP4726 },
519 MODULE_DEVICE_TABLE(i2c, mcp4725_id);
521 static const struct of_device_id mcp4725_of_match[] = {
523 .compatible = "microchip,mcp4725",
524 .data = (void *)MCP4725
527 .compatible = "microchip,mcp4726",
528 .data = (void *)MCP4726
532 MODULE_DEVICE_TABLE(of, mcp4725_of_match);
534 static struct i2c_driver mcp4725_driver = {
536 .name = MCP4725_DRV_NAME,
537 .of_match_table = mcp4725_of_match,
538 .pm = &mcp4725_pm_ops,
540 .probe = mcp4725_probe,
541 .remove = mcp4725_remove,
542 .id_table = mcp4725_id,
544 module_i2c_driver(mcp4725_driver);
546 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
547 MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
548 MODULE_LICENSE("GPL");