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
33 struct mcp4725_chip_info
{
34 const struct iio_chan_spec
*chan_spec
;
36 bool use_ext_ref_voltage
;
40 struct i2c_client
*client
;
45 unsigned powerdown_mode
;
46 struct regulator
*vdd_reg
;
47 struct regulator
*vref_reg
;
50 static int mcp4725_suspend(struct device
*dev
)
52 struct mcp4725_data
*data
= iio_priv(i2c_get_clientdata(
57 outbuf
[0] = (data
->powerdown_mode
+ 1) << 4;
59 data
->powerdown
= true;
61 ret
= i2c_master_send(data
->client
, outbuf
, 2);
69 static int mcp4725_resume(struct device
*dev
)
71 struct mcp4725_data
*data
= iio_priv(i2c_get_clientdata(
76 /* restore previous DAC value */
77 outbuf
[0] = (data
->dac_value
>> 8) & 0xf;
78 outbuf
[1] = data
->dac_value
& 0xff;
79 data
->powerdown
= false;
81 ret
= i2c_master_send(data
->client
, outbuf
, 2);
88 static DEFINE_SIMPLE_DEV_PM_OPS(mcp4725_pm_ops
, mcp4725_suspend
,
91 static ssize_t
mcp4725_store_eeprom(struct device
*dev
,
92 struct device_attribute
*attr
, const char *buf
, size_t len
)
94 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
95 struct mcp4725_data
*data
= iio_priv(indio_dev
);
101 ret
= kstrtobool(buf
, &state
);
108 inoutbuf
[0] = 0x60; /* write EEPROM */
109 inoutbuf
[0] |= data
->ref_mode
<< 3;
110 inoutbuf
[0] |= data
->powerdown
? ((data
->powerdown_mode
+ 1) << 1) : 0;
111 inoutbuf
[1] = data
->dac_value
>> 4;
112 inoutbuf
[2] = (data
->dac_value
& 0xf) << 4;
114 ret
= i2c_master_send(data
->client
, inoutbuf
, 3);
120 /* wait for write complete, takes up to 50ms */
123 ret
= i2c_master_recv(data
->client
, inoutbuf
, 3);
129 if (inoutbuf
[0] & 0x80)
134 dev_err(&data
->client
->dev
,
135 "mcp4725_store_eeprom() failed, incomplete\n");
142 static IIO_DEVICE_ATTR(store_eeprom
, S_IWUSR
, NULL
, mcp4725_store_eeprom
, 0);
144 static struct attribute
*mcp4725_attributes
[] = {
145 &iio_dev_attr_store_eeprom
.dev_attr
.attr
,
149 static const struct attribute_group mcp4725_attribute_group
= {
150 .attrs
= mcp4725_attributes
,
153 static const char * const mcp4725_powerdown_modes
[] = {
159 static const char * const mcp4726_powerdown_modes
[] = {
165 static int mcp4725_get_powerdown_mode(struct iio_dev
*indio_dev
,
166 const struct iio_chan_spec
*chan
)
168 struct mcp4725_data
*data
= iio_priv(indio_dev
);
170 return data
->powerdown_mode
;
173 static int mcp4725_set_powerdown_mode(struct iio_dev
*indio_dev
,
174 const struct iio_chan_spec
*chan
, unsigned mode
)
176 struct mcp4725_data
*data
= iio_priv(indio_dev
);
178 data
->powerdown_mode
= mode
;
183 static ssize_t
mcp4725_read_powerdown(struct iio_dev
*indio_dev
,
184 uintptr_t private, const struct iio_chan_spec
*chan
, char *buf
)
186 struct mcp4725_data
*data
= iio_priv(indio_dev
);
188 return sysfs_emit(buf
, "%d\n", data
->powerdown
);
191 static ssize_t
mcp4725_write_powerdown(struct iio_dev
*indio_dev
,
192 uintptr_t private, const struct iio_chan_spec
*chan
,
193 const char *buf
, size_t len
)
195 struct mcp4725_data
*data
= iio_priv(indio_dev
);
199 ret
= kstrtobool(buf
, &state
);
204 ret
= mcp4725_suspend(&data
->client
->dev
);
206 ret
= mcp4725_resume(&data
->client
->dev
);
218 static const struct iio_enum mcp472x_powerdown_mode_enum
[] = {
220 .items
= mcp4725_powerdown_modes
,
221 .num_items
= ARRAY_SIZE(mcp4725_powerdown_modes
),
222 .get
= mcp4725_get_powerdown_mode
,
223 .set
= mcp4725_set_powerdown_mode
,
226 .items
= mcp4726_powerdown_modes
,
227 .num_items
= ARRAY_SIZE(mcp4726_powerdown_modes
),
228 .get
= mcp4725_get_powerdown_mode
,
229 .set
= mcp4725_set_powerdown_mode
,
233 static const struct iio_chan_spec_ext_info mcp4725_ext_info
[] = {
236 .read
= mcp4725_read_powerdown
,
237 .write
= mcp4725_write_powerdown
,
238 .shared
= IIO_SEPARATE
,
240 IIO_ENUM("powerdown_mode", IIO_SEPARATE
,
241 &mcp472x_powerdown_mode_enum
[MCP4725
]),
242 IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE
,
243 &mcp472x_powerdown_mode_enum
[MCP4725
]),
247 static const struct iio_chan_spec_ext_info mcp4726_ext_info
[] = {
250 .read
= mcp4725_read_powerdown
,
251 .write
= mcp4725_write_powerdown
,
252 .shared
= IIO_SEPARATE
,
254 IIO_ENUM("powerdown_mode", IIO_SEPARATE
,
255 &mcp472x_powerdown_mode_enum
[MCP4726
]),
256 IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE
,
257 &mcp472x_powerdown_mode_enum
[MCP4726
]),
261 static const struct iio_chan_spec mcp472x_channel
[] = {
267 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
268 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
269 .ext_info
= mcp4725_ext_info
,
276 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
277 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
278 .ext_info
= mcp4726_ext_info
,
282 static int mcp4725_set_value(struct iio_dev
*indio_dev
, int val
)
284 struct mcp4725_data
*data
= iio_priv(indio_dev
);
288 if (val
>= (1 << 12) || val
< 0)
291 outbuf
[0] = (val
>> 8) & 0xf;
292 outbuf
[1] = val
& 0xff;
294 ret
= i2c_master_send(data
->client
, outbuf
, 2);
303 static int mcp4726_set_cfg(struct iio_dev
*indio_dev
)
305 struct mcp4725_data
*data
= iio_priv(indio_dev
);
310 outbuf
[0] |= data
->ref_mode
<< 3;
312 outbuf
[0] |= data
->powerdown
<< 1;
313 outbuf
[1] = data
->dac_value
>> 4;
314 outbuf
[2] = (data
->dac_value
& 0xf) << 4;
316 ret
= i2c_master_send(data
->client
, outbuf
, 3);
325 static int mcp4725_read_raw(struct iio_dev
*indio_dev
,
326 struct iio_chan_spec
const *chan
,
327 int *val
, int *val2
, long mask
)
329 struct mcp4725_data
*data
= iio_priv(indio_dev
);
333 case IIO_CHAN_INFO_RAW
:
334 *val
= data
->dac_value
;
336 case IIO_CHAN_INFO_SCALE
:
337 if (data
->ref_mode
== MCP472X_REF_VDD
)
338 ret
= regulator_get_voltage(data
->vdd_reg
);
340 ret
= regulator_get_voltage(data
->vref_reg
);
347 return IIO_VAL_FRACTIONAL_LOG2
;
352 static int mcp4725_write_raw(struct iio_dev
*indio_dev
,
353 struct iio_chan_spec
const *chan
,
354 int val
, int val2
, long mask
)
356 struct mcp4725_data
*data
= iio_priv(indio_dev
);
360 case IIO_CHAN_INFO_RAW
:
361 ret
= mcp4725_set_value(indio_dev
, val
);
362 data
->dac_value
= val
;
372 static const struct iio_info mcp4725_info
= {
373 .read_raw
= mcp4725_read_raw
,
374 .write_raw
= mcp4725_write_raw
,
375 .attrs
= &mcp4725_attribute_group
,
378 static int mcp4725_probe_dt(struct device
*dev
,
379 struct mcp4725_platform_data
*pdata
)
381 /* check if is the vref-supply defined */
382 pdata
->use_vref
= device_property_read_bool(dev
, "vref-supply");
383 pdata
->vref_buffered
=
384 device_property_read_bool(dev
, "microchip,vref-buffered");
389 static int mcp4725_probe(struct i2c_client
*client
)
391 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
392 const struct mcp4725_chip_info
*info
;
393 struct mcp4725_data
*data
;
394 struct iio_dev
*indio_dev
;
395 struct mcp4725_platform_data
*pdata
, pdata_dt
;
401 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
402 if (indio_dev
== NULL
)
404 data
= iio_priv(indio_dev
);
405 i2c_set_clientdata(client
, indio_dev
);
406 data
->client
= client
;
407 info
= i2c_get_match_data(client
);
408 pdata
= dev_get_platdata(&client
->dev
);
411 err
= mcp4725_probe_dt(&client
->dev
, &pdata_dt
);
413 dev_err(&client
->dev
,
414 "invalid platform or devicetree data");
420 if (info
->use_ext_ref_voltage
&& pdata
->use_vref
) {
421 dev_err(&client
->dev
,
422 "external reference is unavailable on MCP4725");
426 if (!pdata
->use_vref
&& pdata
->vref_buffered
) {
427 dev_err(&client
->dev
,
428 "buffering is unavailable on the internal reference");
432 if (!pdata
->use_vref
)
433 data
->ref_mode
= MCP472X_REF_VDD
;
435 data
->ref_mode
= pdata
->vref_buffered
?
436 MCP472X_REF_VREF_BUFFERED
:
437 MCP472X_REF_VREF_UNBUFFERED
;
439 data
->vdd_reg
= devm_regulator_get(&client
->dev
, "vdd");
440 if (IS_ERR(data
->vdd_reg
))
441 return PTR_ERR(data
->vdd_reg
);
443 err
= regulator_enable(data
->vdd_reg
);
447 if (pdata
->use_vref
) {
448 data
->vref_reg
= devm_regulator_get(&client
->dev
, "vref");
449 if (IS_ERR(data
->vref_reg
)) {
450 err
= PTR_ERR(data
->vref_reg
);
451 goto err_disable_vdd_reg
;
454 err
= regulator_enable(data
->vref_reg
);
456 goto err_disable_vdd_reg
;
459 indio_dev
->name
= id
->name
;
460 indio_dev
->info
= &mcp4725_info
;
461 indio_dev
->channels
= info
->chan_spec
;
462 indio_dev
->num_channels
= 1;
463 indio_dev
->modes
= INDIO_DIRECT_MODE
;
465 /* read current DAC value and settings */
466 err
= i2c_master_recv(client
, inbuf
, info
->dac_reg_offset
);
469 dev_err(&client
->dev
, "failed to read DAC value");
470 goto err_disable_vref_reg
;
472 pd
= (inbuf
[0] >> 1) & 0x3;
473 data
->powerdown
= pd
> 0;
474 data
->powerdown_mode
= pd
? pd
- 1 : 2; /* largest resistor to gnd */
475 data
->dac_value
= (inbuf
[1] << 4) | (inbuf
[2] >> 4);
476 if (!info
->use_ext_ref_voltage
)
477 ref
= (inbuf
[3] >> 3) & 0x3;
479 if (!info
->use_ext_ref_voltage
&& ref
!= data
->ref_mode
) {
480 dev_info(&client
->dev
,
481 "voltage reference mode differs (conf: %u, eeprom: %u), setting %u",
482 data
->ref_mode
, ref
, data
->ref_mode
);
483 err
= mcp4726_set_cfg(indio_dev
);
485 goto err_disable_vref_reg
;
488 err
= iio_device_register(indio_dev
);
490 goto err_disable_vref_reg
;
494 err_disable_vref_reg
:
496 regulator_disable(data
->vref_reg
);
499 regulator_disable(data
->vdd_reg
);
504 static void mcp4725_remove(struct i2c_client
*client
)
506 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
507 struct mcp4725_data
*data
= iio_priv(indio_dev
);
509 iio_device_unregister(indio_dev
);
512 regulator_disable(data
->vref_reg
);
513 regulator_disable(data
->vdd_reg
);
516 static const struct mcp4725_chip_info mcp4725
= {
517 .chan_spec
= &mcp472x_channel
[MCP4725
],
519 .use_ext_ref_voltage
= true,
522 static const struct mcp4725_chip_info mcp4726
= {
523 .chan_spec
= &mcp472x_channel
[MCP4726
],
527 static const struct i2c_device_id mcp4725_id
[] = {
528 { "mcp4725", (kernel_ulong_t
)&mcp4725
},
529 { "mcp4726", (kernel_ulong_t
)&mcp4726
},
532 MODULE_DEVICE_TABLE(i2c
, mcp4725_id
);
534 static const struct of_device_id mcp4725_of_match
[] = {
536 .compatible
= "microchip,mcp4725",
540 .compatible
= "microchip,mcp4726",
545 MODULE_DEVICE_TABLE(of
, mcp4725_of_match
);
547 static struct i2c_driver mcp4725_driver
= {
549 .name
= MCP4725_DRV_NAME
,
550 .of_match_table
= mcp4725_of_match
,
551 .pm
= pm_sleep_ptr(&mcp4725_pm_ops
),
553 .probe
= mcp4725_probe
,
554 .remove
= mcp4725_remove
,
555 .id_table
= mcp4725_id
,
557 module_i2c_driver(mcp4725_driver
);
559 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
560 MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
561 MODULE_LICENSE("GPL");