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>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
27 #include <linux/iio/dac/mcp4725.h>
29 #define MCP4725_DRV_NAME "mcp4725"
31 #define MCP472X_REF_VDD 0x00
32 #define MCP472X_REF_VREF_UNBUFFERED 0x02
33 #define MCP472X_REF_VREF_BUFFERED 0x03
36 struct i2c_client
*client
;
42 unsigned powerdown_mode
;
43 struct regulator
*vdd_reg
;
44 struct regulator
*vref_reg
;
47 static int mcp4725_suspend(struct device
*dev
)
49 struct mcp4725_data
*data
= iio_priv(i2c_get_clientdata(
53 outbuf
[0] = (data
->powerdown_mode
+ 1) << 4;
55 data
->powerdown
= true;
57 return i2c_master_send(data
->client
, outbuf
, 2);
60 static int mcp4725_resume(struct device
*dev
)
62 struct mcp4725_data
*data
= iio_priv(i2c_get_clientdata(
66 /* restore previous DAC value */
67 outbuf
[0] = (data
->dac_value
>> 8) & 0xf;
68 outbuf
[1] = data
->dac_value
& 0xff;
69 data
->powerdown
= false;
71 return i2c_master_send(data
->client
, outbuf
, 2);
74 #ifdef CONFIG_PM_SLEEP
75 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops
, mcp4725_suspend
, mcp4725_resume
);
76 #define MCP4725_PM_OPS (&mcp4725_pm_ops)
78 #define MCP4725_PM_OPS NULL
81 static ssize_t
mcp4725_store_eeprom(struct device
*dev
,
82 struct device_attribute
*attr
, const char *buf
, size_t len
)
84 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
85 struct mcp4725_data
*data
= iio_priv(indio_dev
);
91 ret
= strtobool(buf
, &state
);
98 inoutbuf
[0] = 0x60; /* write EEPROM */
99 inoutbuf
[0] |= data
->ref_mode
<< 3;
100 inoutbuf
[1] = data
->dac_value
>> 4;
101 inoutbuf
[2] = (data
->dac_value
& 0xf) << 4;
103 ret
= i2c_master_send(data
->client
, inoutbuf
, 3);
109 /* wait for write complete, takes up to 50ms */
112 ret
= i2c_master_recv(data
->client
, inoutbuf
, 3);
118 if (inoutbuf
[0] & 0x80)
123 dev_err(&data
->client
->dev
,
124 "mcp4725_store_eeprom() failed, incomplete\n");
131 static IIO_DEVICE_ATTR(store_eeprom
, S_IWUSR
, NULL
, mcp4725_store_eeprom
, 0);
133 static struct attribute
*mcp4725_attributes
[] = {
134 &iio_dev_attr_store_eeprom
.dev_attr
.attr
,
138 static const struct attribute_group mcp4725_attribute_group
= {
139 .attrs
= mcp4725_attributes
,
142 static const char * const mcp4725_powerdown_modes
[] = {
148 static const char * const mcp4726_powerdown_modes
[] = {
154 static int mcp4725_get_powerdown_mode(struct iio_dev
*indio_dev
,
155 const struct iio_chan_spec
*chan
)
157 struct mcp4725_data
*data
= iio_priv(indio_dev
);
159 return data
->powerdown_mode
;
162 static int mcp4725_set_powerdown_mode(struct iio_dev
*indio_dev
,
163 const struct iio_chan_spec
*chan
, unsigned mode
)
165 struct mcp4725_data
*data
= iio_priv(indio_dev
);
167 data
->powerdown_mode
= mode
;
172 static ssize_t
mcp4725_read_powerdown(struct iio_dev
*indio_dev
,
173 uintptr_t private, const struct iio_chan_spec
*chan
, char *buf
)
175 struct mcp4725_data
*data
= iio_priv(indio_dev
);
177 return sprintf(buf
, "%d\n", data
->powerdown
);
180 static ssize_t
mcp4725_write_powerdown(struct iio_dev
*indio_dev
,
181 uintptr_t private, const struct iio_chan_spec
*chan
,
182 const char *buf
, size_t len
)
184 struct mcp4725_data
*data
= iio_priv(indio_dev
);
188 ret
= strtobool(buf
, &state
);
193 ret
= mcp4725_suspend(&data
->client
->dev
);
195 ret
= mcp4725_resume(&data
->client
->dev
);
207 static const struct iio_enum mcp472x_powerdown_mode_enum
[] = {
209 .items
= mcp4725_powerdown_modes
,
210 .num_items
= ARRAY_SIZE(mcp4725_powerdown_modes
),
211 .get
= mcp4725_get_powerdown_mode
,
212 .set
= mcp4725_set_powerdown_mode
,
215 .items
= mcp4726_powerdown_modes
,
216 .num_items
= ARRAY_SIZE(mcp4726_powerdown_modes
),
217 .get
= mcp4725_get_powerdown_mode
,
218 .set
= mcp4725_set_powerdown_mode
,
222 static const struct iio_chan_spec_ext_info mcp4725_ext_info
[] = {
225 .read
= mcp4725_read_powerdown
,
226 .write
= mcp4725_write_powerdown
,
227 .shared
= IIO_SEPARATE
,
229 IIO_ENUM("powerdown_mode", IIO_SEPARATE
,
230 &mcp472x_powerdown_mode_enum
[MCP4725
]),
231 IIO_ENUM_AVAILABLE("powerdown_mode",
232 &mcp472x_powerdown_mode_enum
[MCP4725
]),
236 static const struct iio_chan_spec_ext_info mcp4726_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
[MCP4726
]),
245 IIO_ENUM_AVAILABLE("powerdown_mode",
246 &mcp472x_powerdown_mode_enum
[MCP4726
]),
250 static const struct iio_chan_spec mcp472x_channel
[] = {
256 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
257 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
258 .ext_info
= mcp4725_ext_info
,
265 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
266 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
267 .ext_info
= mcp4726_ext_info
,
271 static int mcp4725_set_value(struct iio_dev
*indio_dev
, int val
)
273 struct mcp4725_data
*data
= iio_priv(indio_dev
);
277 if (val
>= (1 << 12) || val
< 0)
280 outbuf
[0] = (val
>> 8) & 0xf;
281 outbuf
[1] = val
& 0xff;
283 ret
= i2c_master_send(data
->client
, outbuf
, 2);
292 static int mcp4726_set_cfg(struct iio_dev
*indio_dev
)
294 struct mcp4725_data
*data
= iio_priv(indio_dev
);
299 outbuf
[0] |= data
->ref_mode
<< 3;
301 outbuf
[0] |= data
->powerdown
<< 1;
302 outbuf
[1] = data
->dac_value
>> 4;
303 outbuf
[2] = (data
->dac_value
& 0xf) << 4;
305 ret
= i2c_master_send(data
->client
, outbuf
, 3);
314 static int mcp4725_read_raw(struct iio_dev
*indio_dev
,
315 struct iio_chan_spec
const *chan
,
316 int *val
, int *val2
, long mask
)
318 struct mcp4725_data
*data
= iio_priv(indio_dev
);
322 case IIO_CHAN_INFO_RAW
:
323 *val
= data
->dac_value
;
325 case IIO_CHAN_INFO_SCALE
:
326 if (data
->ref_mode
== MCP472X_REF_VDD
)
327 ret
= regulator_get_voltage(data
->vdd_reg
);
329 ret
= regulator_get_voltage(data
->vref_reg
);
336 return IIO_VAL_FRACTIONAL_LOG2
;
341 static int mcp4725_write_raw(struct iio_dev
*indio_dev
,
342 struct iio_chan_spec
const *chan
,
343 int val
, int val2
, long mask
)
345 struct mcp4725_data
*data
= iio_priv(indio_dev
);
349 case IIO_CHAN_INFO_RAW
:
350 ret
= mcp4725_set_value(indio_dev
, val
);
351 data
->dac_value
= val
;
361 static const struct iio_info mcp4725_info
= {
362 .read_raw
= mcp4725_read_raw
,
363 .write_raw
= mcp4725_write_raw
,
364 .attrs
= &mcp4725_attribute_group
,
365 .driver_module
= THIS_MODULE
,
369 static int mcp4725_probe_dt(struct device
*dev
,
370 struct mcp4725_platform_data
*pdata
)
372 struct device_node
*np
= dev
->of_node
;
377 /* check if is the vref-supply defined */
378 pdata
->use_vref
= of_property_read_bool(np
, "vref-supply");
379 pdata
->vref_buffered
=
380 of_property_read_bool(np
, "microchip,vref-buffered");
385 static int mcp4725_probe_dt(struct device
*dev
,
386 struct mcp4725_platform_data
*platform_data
)
392 static int mcp4725_probe(struct i2c_client
*client
,
393 const struct i2c_device_id
*id
)
395 struct mcp4725_data
*data
;
396 struct iio_dev
*indio_dev
;
397 struct mcp4725_platform_data
*pdata
, pdata_dt
;
403 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
404 if (indio_dev
== NULL
)
406 data
= iio_priv(indio_dev
);
407 i2c_set_clientdata(client
, indio_dev
);
408 data
->client
= client
;
409 data
->id
= id
->driver_data
;
410 pdata
= dev_get_platdata(&client
->dev
);
413 err
= mcp4725_probe_dt(&client
->dev
, &pdata_dt
);
415 dev_err(&client
->dev
,
416 "invalid platform or devicetree data");
422 if (data
->id
== MCP4725
&& pdata
->use_vref
) {
423 dev_err(&client
->dev
,
424 "external reference is unavailable on MCP4725");
428 if (!pdata
->use_vref
&& pdata
->vref_buffered
) {
429 dev_err(&client
->dev
,
430 "buffering is unavailable on the internal reference");
434 if (!pdata
->use_vref
)
435 data
->ref_mode
= MCP472X_REF_VDD
;
437 data
->ref_mode
= pdata
->vref_buffered
?
438 MCP472X_REF_VREF_BUFFERED
:
439 MCP472X_REF_VREF_UNBUFFERED
;
441 data
->vdd_reg
= devm_regulator_get(&client
->dev
, "vdd");
442 if (IS_ERR(data
->vdd_reg
))
443 return PTR_ERR(data
->vdd_reg
);
445 err
= regulator_enable(data
->vdd_reg
);
449 if (pdata
->use_vref
) {
450 data
->vref_reg
= devm_regulator_get(&client
->dev
, "vref");
451 if (IS_ERR(data
->vref_reg
)) {
452 err
= PTR_ERR(data
->vref_reg
);
453 goto err_disable_vdd_reg
;
456 err
= regulator_enable(data
->vref_reg
);
458 goto err_disable_vdd_reg
;
461 indio_dev
->dev
.parent
= &client
->dev
;
462 indio_dev
->name
= id
->name
;
463 indio_dev
->info
= &mcp4725_info
;
464 indio_dev
->channels
= &mcp472x_channel
[id
->driver_data
];
465 indio_dev
->num_channels
= 1;
466 indio_dev
->modes
= INDIO_DIRECT_MODE
;
468 /* read current DAC value and settings */
469 err
= i2c_master_recv(client
, inbuf
, data
->id
== MCP4725
? 3 : 4);
472 dev_err(&client
->dev
, "failed to read DAC value");
473 goto err_disable_vref_reg
;
475 pd
= (inbuf
[0] >> 1) & 0x3;
476 data
->powerdown
= pd
> 0 ? true : false;
477 data
->powerdown_mode
= pd
? pd
- 1 : 2; /* largest resistor to gnd */
478 data
->dac_value
= (inbuf
[1] << 4) | (inbuf
[2] >> 4);
479 if (data
->id
== MCP4726
)
480 ref
= (inbuf
[3] >> 3) & 0x3;
482 if (data
->id
== MCP4726
&& ref
!= data
->ref_mode
) {
483 dev_info(&client
->dev
,
484 "voltage reference mode differs (conf: %u, eeprom: %u), setting %u",
485 data
->ref_mode
, ref
, data
->ref_mode
);
486 err
= mcp4726_set_cfg(indio_dev
);
488 goto err_disable_vref_reg
;
491 err
= iio_device_register(indio_dev
);
493 goto err_disable_vref_reg
;
497 err_disable_vref_reg
:
499 regulator_disable(data
->vref_reg
);
502 regulator_disable(data
->vdd_reg
);
507 static int mcp4725_remove(struct i2c_client
*client
)
509 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
510 struct mcp4725_data
*data
= iio_priv(indio_dev
);
512 iio_device_unregister(indio_dev
);
515 regulator_disable(data
->vref_reg
);
516 regulator_disable(data
->vdd_reg
);
521 static const struct i2c_device_id mcp4725_id
[] = {
522 { "mcp4725", MCP4725
},
523 { "mcp4726", MCP4726
},
526 MODULE_DEVICE_TABLE(i2c
, mcp4725_id
);
528 static struct i2c_driver mcp4725_driver
= {
530 .name
= MCP4725_DRV_NAME
,
531 .pm
= MCP4725_PM_OPS
,
533 .probe
= mcp4725_probe
,
534 .remove
= mcp4725_remove
,
535 .id_table
= mcp4725_id
,
537 module_i2c_driver(mcp4725_driver
);
539 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
540 MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
541 MODULE_LICENSE("GPL");