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(
51 outbuf
[0] = (data
->powerdown_mode
+ 1) << 4;
53 data
->powerdown
= true;
55 return i2c_master_send(data
->client
, outbuf
, 2);
58 static int __maybe_unused
mcp4725_resume(struct device
*dev
)
60 struct mcp4725_data
*data
= iio_priv(i2c_get_clientdata(
64 /* restore previous DAC value */
65 outbuf
[0] = (data
->dac_value
>> 8) & 0xf;
66 outbuf
[1] = data
->dac_value
& 0xff;
67 data
->powerdown
= false;
69 return i2c_master_send(data
->client
, outbuf
, 2);
71 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops
, mcp4725_suspend
, mcp4725_resume
);
73 static ssize_t
mcp4725_store_eeprom(struct device
*dev
,
74 struct device_attribute
*attr
, const char *buf
, size_t len
)
76 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
77 struct mcp4725_data
*data
= iio_priv(indio_dev
);
83 ret
= strtobool(buf
, &state
);
90 inoutbuf
[0] = 0x60; /* write EEPROM */
91 inoutbuf
[0] |= data
->ref_mode
<< 3;
92 inoutbuf
[0] |= data
->powerdown
? ((data
->powerdown_mode
+ 1) << 1) : 0;
93 inoutbuf
[1] = data
->dac_value
>> 4;
94 inoutbuf
[2] = (data
->dac_value
& 0xf) << 4;
96 ret
= i2c_master_send(data
->client
, inoutbuf
, 3);
102 /* wait for write complete, takes up to 50ms */
105 ret
= i2c_master_recv(data
->client
, inoutbuf
, 3);
111 if (inoutbuf
[0] & 0x80)
116 dev_err(&data
->client
->dev
,
117 "mcp4725_store_eeprom() failed, incomplete\n");
124 static IIO_DEVICE_ATTR(store_eeprom
, S_IWUSR
, NULL
, mcp4725_store_eeprom
, 0);
126 static struct attribute
*mcp4725_attributes
[] = {
127 &iio_dev_attr_store_eeprom
.dev_attr
.attr
,
131 static const struct attribute_group mcp4725_attribute_group
= {
132 .attrs
= mcp4725_attributes
,
135 static const char * const mcp4725_powerdown_modes
[] = {
141 static const char * const mcp4726_powerdown_modes
[] = {
147 static int mcp4725_get_powerdown_mode(struct iio_dev
*indio_dev
,
148 const struct iio_chan_spec
*chan
)
150 struct mcp4725_data
*data
= iio_priv(indio_dev
);
152 return data
->powerdown_mode
;
155 static int mcp4725_set_powerdown_mode(struct iio_dev
*indio_dev
,
156 const struct iio_chan_spec
*chan
, unsigned mode
)
158 struct mcp4725_data
*data
= iio_priv(indio_dev
);
160 data
->powerdown_mode
= mode
;
165 static ssize_t
mcp4725_read_powerdown(struct iio_dev
*indio_dev
,
166 uintptr_t private, const struct iio_chan_spec
*chan
, char *buf
)
168 struct mcp4725_data
*data
= iio_priv(indio_dev
);
170 return sprintf(buf
, "%d\n", data
->powerdown
);
173 static ssize_t
mcp4725_write_powerdown(struct iio_dev
*indio_dev
,
174 uintptr_t private, const struct iio_chan_spec
*chan
,
175 const char *buf
, size_t len
)
177 struct mcp4725_data
*data
= iio_priv(indio_dev
);
181 ret
= strtobool(buf
, &state
);
186 ret
= mcp4725_suspend(&data
->client
->dev
);
188 ret
= mcp4725_resume(&data
->client
->dev
);
200 static const struct iio_enum mcp472x_powerdown_mode_enum
[] = {
202 .items
= mcp4725_powerdown_modes
,
203 .num_items
= ARRAY_SIZE(mcp4725_powerdown_modes
),
204 .get
= mcp4725_get_powerdown_mode
,
205 .set
= mcp4725_set_powerdown_mode
,
208 .items
= mcp4726_powerdown_modes
,
209 .num_items
= ARRAY_SIZE(mcp4726_powerdown_modes
),
210 .get
= mcp4725_get_powerdown_mode
,
211 .set
= mcp4725_set_powerdown_mode
,
215 static const struct iio_chan_spec_ext_info mcp4725_ext_info
[] = {
218 .read
= mcp4725_read_powerdown
,
219 .write
= mcp4725_write_powerdown
,
220 .shared
= IIO_SEPARATE
,
222 IIO_ENUM("powerdown_mode", IIO_SEPARATE
,
223 &mcp472x_powerdown_mode_enum
[MCP4725
]),
224 IIO_ENUM_AVAILABLE("powerdown_mode",
225 &mcp472x_powerdown_mode_enum
[MCP4725
]),
229 static const struct iio_chan_spec_ext_info mcp4726_ext_info
[] = {
232 .read
= mcp4725_read_powerdown
,
233 .write
= mcp4725_write_powerdown
,
234 .shared
= IIO_SEPARATE
,
236 IIO_ENUM("powerdown_mode", IIO_SEPARATE
,
237 &mcp472x_powerdown_mode_enum
[MCP4726
]),
238 IIO_ENUM_AVAILABLE("powerdown_mode",
239 &mcp472x_powerdown_mode_enum
[MCP4726
]),
243 static const struct iio_chan_spec mcp472x_channel
[] = {
249 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
250 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
251 .ext_info
= mcp4725_ext_info
,
258 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
259 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
260 .ext_info
= mcp4726_ext_info
,
264 static int mcp4725_set_value(struct iio_dev
*indio_dev
, int val
)
266 struct mcp4725_data
*data
= iio_priv(indio_dev
);
270 if (val
>= (1 << 12) || val
< 0)
273 outbuf
[0] = (val
>> 8) & 0xf;
274 outbuf
[1] = val
& 0xff;
276 ret
= i2c_master_send(data
->client
, outbuf
, 2);
285 static int mcp4726_set_cfg(struct iio_dev
*indio_dev
)
287 struct mcp4725_data
*data
= iio_priv(indio_dev
);
292 outbuf
[0] |= data
->ref_mode
<< 3;
294 outbuf
[0] |= data
->powerdown
<< 1;
295 outbuf
[1] = data
->dac_value
>> 4;
296 outbuf
[2] = (data
->dac_value
& 0xf) << 4;
298 ret
= i2c_master_send(data
->client
, outbuf
, 3);
307 static int mcp4725_read_raw(struct iio_dev
*indio_dev
,
308 struct iio_chan_spec
const *chan
,
309 int *val
, int *val2
, long mask
)
311 struct mcp4725_data
*data
= iio_priv(indio_dev
);
315 case IIO_CHAN_INFO_RAW
:
316 *val
= data
->dac_value
;
318 case IIO_CHAN_INFO_SCALE
:
319 if (data
->ref_mode
== MCP472X_REF_VDD
)
320 ret
= regulator_get_voltage(data
->vdd_reg
);
322 ret
= regulator_get_voltage(data
->vref_reg
);
329 return IIO_VAL_FRACTIONAL_LOG2
;
334 static int mcp4725_write_raw(struct iio_dev
*indio_dev
,
335 struct iio_chan_spec
const *chan
,
336 int val
, int val2
, long mask
)
338 struct mcp4725_data
*data
= iio_priv(indio_dev
);
342 case IIO_CHAN_INFO_RAW
:
343 ret
= mcp4725_set_value(indio_dev
, val
);
344 data
->dac_value
= val
;
354 static const struct iio_info mcp4725_info
= {
355 .read_raw
= mcp4725_read_raw
,
356 .write_raw
= mcp4725_write_raw
,
357 .attrs
= &mcp4725_attribute_group
,
360 static int mcp4725_probe_dt(struct device
*dev
,
361 struct mcp4725_platform_data
*pdata
)
363 /* check if is the vref-supply defined */
364 pdata
->use_vref
= device_property_read_bool(dev
, "vref-supply");
365 pdata
->vref_buffered
=
366 device_property_read_bool(dev
, "microchip,vref-buffered");
371 static int mcp4725_probe(struct i2c_client
*client
,
372 const struct i2c_device_id
*id
)
374 struct mcp4725_data
*data
;
375 struct iio_dev
*indio_dev
;
376 struct mcp4725_platform_data
*pdata
, pdata_dt
;
382 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
383 if (indio_dev
== NULL
)
385 data
= iio_priv(indio_dev
);
386 i2c_set_clientdata(client
, indio_dev
);
387 data
->client
= client
;
388 if (dev_fwnode(&client
->dev
))
389 data
->id
= (enum chip_id
)device_get_match_data(&client
->dev
);
391 data
->id
= id
->driver_data
;
392 pdata
= dev_get_platdata(&client
->dev
);
395 err
= mcp4725_probe_dt(&client
->dev
, &pdata_dt
);
397 dev_err(&client
->dev
,
398 "invalid platform or devicetree data");
404 if (data
->id
== MCP4725
&& pdata
->use_vref
) {
405 dev_err(&client
->dev
,
406 "external reference is unavailable on MCP4725");
410 if (!pdata
->use_vref
&& pdata
->vref_buffered
) {
411 dev_err(&client
->dev
,
412 "buffering is unavailable on the internal reference");
416 if (!pdata
->use_vref
)
417 data
->ref_mode
= MCP472X_REF_VDD
;
419 data
->ref_mode
= pdata
->vref_buffered
?
420 MCP472X_REF_VREF_BUFFERED
:
421 MCP472X_REF_VREF_UNBUFFERED
;
423 data
->vdd_reg
= devm_regulator_get(&client
->dev
, "vdd");
424 if (IS_ERR(data
->vdd_reg
))
425 return PTR_ERR(data
->vdd_reg
);
427 err
= regulator_enable(data
->vdd_reg
);
431 if (pdata
->use_vref
) {
432 data
->vref_reg
= devm_regulator_get(&client
->dev
, "vref");
433 if (IS_ERR(data
->vref_reg
)) {
434 err
= PTR_ERR(data
->vref_reg
);
435 goto err_disable_vdd_reg
;
438 err
= regulator_enable(data
->vref_reg
);
440 goto err_disable_vdd_reg
;
443 indio_dev
->name
= id
->name
;
444 indio_dev
->info
= &mcp4725_info
;
445 indio_dev
->channels
= &mcp472x_channel
[id
->driver_data
];
446 indio_dev
->num_channels
= 1;
447 indio_dev
->modes
= INDIO_DIRECT_MODE
;
449 /* read current DAC value and settings */
450 err
= i2c_master_recv(client
, inbuf
, data
->id
== MCP4725
? 3 : 4);
453 dev_err(&client
->dev
, "failed to read DAC value");
454 goto err_disable_vref_reg
;
456 pd
= (inbuf
[0] >> 1) & 0x3;
457 data
->powerdown
= pd
> 0;
458 data
->powerdown_mode
= pd
? pd
- 1 : 2; /* largest resistor to gnd */
459 data
->dac_value
= (inbuf
[1] << 4) | (inbuf
[2] >> 4);
460 if (data
->id
== MCP4726
)
461 ref
= (inbuf
[3] >> 3) & 0x3;
463 if (data
->id
== MCP4726
&& ref
!= data
->ref_mode
) {
464 dev_info(&client
->dev
,
465 "voltage reference mode differs (conf: %u, eeprom: %u), setting %u",
466 data
->ref_mode
, ref
, data
->ref_mode
);
467 err
= mcp4726_set_cfg(indio_dev
);
469 goto err_disable_vref_reg
;
472 err
= iio_device_register(indio_dev
);
474 goto err_disable_vref_reg
;
478 err_disable_vref_reg
:
480 regulator_disable(data
->vref_reg
);
483 regulator_disable(data
->vdd_reg
);
488 static int mcp4725_remove(struct i2c_client
*client
)
490 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
491 struct mcp4725_data
*data
= iio_priv(indio_dev
);
493 iio_device_unregister(indio_dev
);
496 regulator_disable(data
->vref_reg
);
497 regulator_disable(data
->vdd_reg
);
502 static const struct i2c_device_id mcp4725_id
[] = {
503 { "mcp4725", MCP4725
},
504 { "mcp4726", MCP4726
},
507 MODULE_DEVICE_TABLE(i2c
, mcp4725_id
);
509 static const struct of_device_id mcp4725_of_match
[] = {
511 .compatible
= "microchip,mcp4725",
512 .data
= (void *)MCP4725
515 .compatible
= "microchip,mcp4726",
516 .data
= (void *)MCP4726
520 MODULE_DEVICE_TABLE(of
, mcp4725_of_match
);
522 static struct i2c_driver mcp4725_driver
= {
524 .name
= MCP4725_DRV_NAME
,
525 .of_match_table
= mcp4725_of_match
,
526 .pm
= &mcp4725_pm_ops
,
528 .probe
= mcp4725_probe
,
529 .remove
= mcp4725_remove
,
530 .id_table
= mcp4725_id
,
532 module_i2c_driver(mcp4725_driver
);
534 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
535 MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
536 MODULE_LICENSE("GPL");