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(
54 outbuf
[0] = (data
->powerdown_mode
+ 1) << 4;
56 data
->powerdown
= true;
58 return i2c_master_send(data
->client
, outbuf
, 2);
61 static int mcp4725_resume(struct device
*dev
)
63 struct mcp4725_data
*data
= iio_priv(i2c_get_clientdata(
67 /* restore previous DAC value */
68 outbuf
[0] = (data
->dac_value
>> 8) & 0xf;
69 outbuf
[1] = data
->dac_value
& 0xff;
70 data
->powerdown
= false;
72 return i2c_master_send(data
->client
, outbuf
, 2);
75 #ifdef CONFIG_PM_SLEEP
76 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops
, mcp4725_suspend
, mcp4725_resume
);
77 #define MCP4725_PM_OPS (&mcp4725_pm_ops)
79 #define MCP4725_PM_OPS NULL
82 static ssize_t
mcp4725_store_eeprom(struct device
*dev
,
83 struct device_attribute
*attr
, const char *buf
, size_t len
)
85 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
86 struct mcp4725_data
*data
= iio_priv(indio_dev
);
92 ret
= strtobool(buf
, &state
);
99 inoutbuf
[0] = 0x60; /* write EEPROM */
100 inoutbuf
[0] |= data
->ref_mode
<< 3;
101 inoutbuf
[1] = data
->dac_value
>> 4;
102 inoutbuf
[2] = (data
->dac_value
& 0xf) << 4;
104 ret
= i2c_master_send(data
->client
, inoutbuf
, 3);
110 /* wait for write complete, takes up to 50ms */
113 ret
= i2c_master_recv(data
->client
, inoutbuf
, 3);
119 if (inoutbuf
[0] & 0x80)
124 dev_err(&data
->client
->dev
,
125 "mcp4725_store_eeprom() failed, incomplete\n");
132 static IIO_DEVICE_ATTR(store_eeprom
, S_IWUSR
, NULL
, mcp4725_store_eeprom
, 0);
134 static struct attribute
*mcp4725_attributes
[] = {
135 &iio_dev_attr_store_eeprom
.dev_attr
.attr
,
139 static const struct attribute_group mcp4725_attribute_group
= {
140 .attrs
= mcp4725_attributes
,
143 static const char * const mcp4725_powerdown_modes
[] = {
149 static const char * const mcp4726_powerdown_modes
[] = {
155 static int mcp4725_get_powerdown_mode(struct iio_dev
*indio_dev
,
156 const struct iio_chan_spec
*chan
)
158 struct mcp4725_data
*data
= iio_priv(indio_dev
);
160 return data
->powerdown_mode
;
163 static int mcp4725_set_powerdown_mode(struct iio_dev
*indio_dev
,
164 const struct iio_chan_spec
*chan
, unsigned mode
)
166 struct mcp4725_data
*data
= iio_priv(indio_dev
);
168 data
->powerdown_mode
= mode
;
173 static ssize_t
mcp4725_read_powerdown(struct iio_dev
*indio_dev
,
174 uintptr_t private, const struct iio_chan_spec
*chan
, char *buf
)
176 struct mcp4725_data
*data
= iio_priv(indio_dev
);
178 return sprintf(buf
, "%d\n", data
->powerdown
);
181 static ssize_t
mcp4725_write_powerdown(struct iio_dev
*indio_dev
,
182 uintptr_t private, const struct iio_chan_spec
*chan
,
183 const char *buf
, size_t len
)
185 struct mcp4725_data
*data
= iio_priv(indio_dev
);
189 ret
= strtobool(buf
, &state
);
194 ret
= mcp4725_suspend(&data
->client
->dev
);
196 ret
= mcp4725_resume(&data
->client
->dev
);
208 static const struct iio_enum mcp472x_powerdown_mode_enum
[] = {
210 .items
= mcp4725_powerdown_modes
,
211 .num_items
= ARRAY_SIZE(mcp4725_powerdown_modes
),
212 .get
= mcp4725_get_powerdown_mode
,
213 .set
= mcp4725_set_powerdown_mode
,
216 .items
= mcp4726_powerdown_modes
,
217 .num_items
= ARRAY_SIZE(mcp4726_powerdown_modes
),
218 .get
= mcp4725_get_powerdown_mode
,
219 .set
= mcp4725_set_powerdown_mode
,
223 static const struct iio_chan_spec_ext_info mcp4725_ext_info
[] = {
226 .read
= mcp4725_read_powerdown
,
227 .write
= mcp4725_write_powerdown
,
228 .shared
= IIO_SEPARATE
,
230 IIO_ENUM("powerdown_mode", IIO_SEPARATE
,
231 &mcp472x_powerdown_mode_enum
[MCP4725
]),
232 IIO_ENUM_AVAILABLE("powerdown_mode",
233 &mcp472x_powerdown_mode_enum
[MCP4725
]),
237 static const struct iio_chan_spec_ext_info mcp4726_ext_info
[] = {
240 .read
= mcp4725_read_powerdown
,
241 .write
= mcp4725_write_powerdown
,
242 .shared
= IIO_SEPARATE
,
244 IIO_ENUM("powerdown_mode", IIO_SEPARATE
,
245 &mcp472x_powerdown_mode_enum
[MCP4726
]),
246 IIO_ENUM_AVAILABLE("powerdown_mode",
247 &mcp472x_powerdown_mode_enum
[MCP4726
]),
251 static const struct iio_chan_spec mcp472x_channel
[] = {
257 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
258 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
259 .ext_info
= mcp4725_ext_info
,
266 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
267 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
268 .ext_info
= mcp4726_ext_info
,
272 static int mcp4725_set_value(struct iio_dev
*indio_dev
, int val
)
274 struct mcp4725_data
*data
= iio_priv(indio_dev
);
278 if (val
>= (1 << 12) || val
< 0)
281 outbuf
[0] = (val
>> 8) & 0xf;
282 outbuf
[1] = val
& 0xff;
284 ret
= i2c_master_send(data
->client
, outbuf
, 2);
293 static int mcp4726_set_cfg(struct iio_dev
*indio_dev
)
295 struct mcp4725_data
*data
= iio_priv(indio_dev
);
300 outbuf
[0] |= data
->ref_mode
<< 3;
302 outbuf
[0] |= data
->powerdown
<< 1;
303 outbuf
[1] = data
->dac_value
>> 4;
304 outbuf
[2] = (data
->dac_value
& 0xf) << 4;
306 ret
= i2c_master_send(data
->client
, outbuf
, 3);
315 static int mcp4725_read_raw(struct iio_dev
*indio_dev
,
316 struct iio_chan_spec
const *chan
,
317 int *val
, int *val2
, long mask
)
319 struct mcp4725_data
*data
= iio_priv(indio_dev
);
323 case IIO_CHAN_INFO_RAW
:
324 *val
= data
->dac_value
;
326 case IIO_CHAN_INFO_SCALE
:
327 if (data
->ref_mode
== MCP472X_REF_VDD
)
328 ret
= regulator_get_voltage(data
->vdd_reg
);
330 ret
= regulator_get_voltage(data
->vref_reg
);
337 return IIO_VAL_FRACTIONAL_LOG2
;
342 static int mcp4725_write_raw(struct iio_dev
*indio_dev
,
343 struct iio_chan_spec
const *chan
,
344 int val
, int val2
, long mask
)
346 struct mcp4725_data
*data
= iio_priv(indio_dev
);
350 case IIO_CHAN_INFO_RAW
:
351 ret
= mcp4725_set_value(indio_dev
, val
);
352 data
->dac_value
= val
;
362 static const struct iio_info mcp4725_info
= {
363 .read_raw
= mcp4725_read_raw
,
364 .write_raw
= mcp4725_write_raw
,
365 .attrs
= &mcp4725_attribute_group
,
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 if (client
->dev
.of_node
)
410 data
->id
= (enum chip_id
)of_device_get_match_data(&client
->dev
);
412 data
->id
= id
->driver_data
;
413 pdata
= dev_get_platdata(&client
->dev
);
416 err
= mcp4725_probe_dt(&client
->dev
, &pdata_dt
);
418 dev_err(&client
->dev
,
419 "invalid platform or devicetree data");
425 if (data
->id
== MCP4725
&& pdata
->use_vref
) {
426 dev_err(&client
->dev
,
427 "external reference is unavailable on MCP4725");
431 if (!pdata
->use_vref
&& pdata
->vref_buffered
) {
432 dev_err(&client
->dev
,
433 "buffering is unavailable on the internal reference");
437 if (!pdata
->use_vref
)
438 data
->ref_mode
= MCP472X_REF_VDD
;
440 data
->ref_mode
= pdata
->vref_buffered
?
441 MCP472X_REF_VREF_BUFFERED
:
442 MCP472X_REF_VREF_UNBUFFERED
;
444 data
->vdd_reg
= devm_regulator_get(&client
->dev
, "vdd");
445 if (IS_ERR(data
->vdd_reg
))
446 return PTR_ERR(data
->vdd_reg
);
448 err
= regulator_enable(data
->vdd_reg
);
452 if (pdata
->use_vref
) {
453 data
->vref_reg
= devm_regulator_get(&client
->dev
, "vref");
454 if (IS_ERR(data
->vref_reg
)) {
455 err
= PTR_ERR(data
->vref_reg
);
456 goto err_disable_vdd_reg
;
459 err
= regulator_enable(data
->vref_reg
);
461 goto err_disable_vdd_reg
;
464 indio_dev
->dev
.parent
= &client
->dev
;
465 indio_dev
->name
= id
->name
;
466 indio_dev
->info
= &mcp4725_info
;
467 indio_dev
->channels
= &mcp472x_channel
[id
->driver_data
];
468 indio_dev
->num_channels
= 1;
469 indio_dev
->modes
= INDIO_DIRECT_MODE
;
471 /* read current DAC value and settings */
472 err
= i2c_master_recv(client
, inbuf
, data
->id
== MCP4725
? 3 : 4);
475 dev_err(&client
->dev
, "failed to read DAC value");
476 goto err_disable_vref_reg
;
478 pd
= (inbuf
[0] >> 1) & 0x3;
479 data
->powerdown
= pd
> 0;
480 data
->powerdown_mode
= pd
? pd
- 1 : 2; /* largest resistor to gnd */
481 data
->dac_value
= (inbuf
[1] << 4) | (inbuf
[2] >> 4);
482 if (data
->id
== MCP4726
)
483 ref
= (inbuf
[3] >> 3) & 0x3;
485 if (data
->id
== MCP4726
&& ref
!= data
->ref_mode
) {
486 dev_info(&client
->dev
,
487 "voltage reference mode differs (conf: %u, eeprom: %u), setting %u",
488 data
->ref_mode
, ref
, data
->ref_mode
);
489 err
= mcp4726_set_cfg(indio_dev
);
491 goto err_disable_vref_reg
;
494 err
= iio_device_register(indio_dev
);
496 goto err_disable_vref_reg
;
500 err_disable_vref_reg
:
502 regulator_disable(data
->vref_reg
);
505 regulator_disable(data
->vdd_reg
);
510 static int mcp4725_remove(struct i2c_client
*client
)
512 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
513 struct mcp4725_data
*data
= iio_priv(indio_dev
);
515 iio_device_unregister(indio_dev
);
518 regulator_disable(data
->vref_reg
);
519 regulator_disable(data
->vdd_reg
);
524 static const struct i2c_device_id mcp4725_id
[] = {
525 { "mcp4725", MCP4725
},
526 { "mcp4726", MCP4726
},
529 MODULE_DEVICE_TABLE(i2c
, mcp4725_id
);
532 static const struct of_device_id mcp4725_of_match
[] = {
534 .compatible
= "microchip,mcp4725",
535 .data
= (void *)MCP4725
538 .compatible
= "microchip,mcp4726",
539 .data
= (void *)MCP4726
543 MODULE_DEVICE_TABLE(of
, mcp4725_of_match
);
546 static struct i2c_driver mcp4725_driver
= {
548 .name
= MCP4725_DRV_NAME
,
549 .of_match_table
= of_match_ptr(mcp4725_of_match
),
550 .pm
= MCP4725_PM_OPS
,
552 .probe
= mcp4725_probe
,
553 .remove
= mcp4725_remove
,
554 .id_table
= mcp4725_id
,
556 module_i2c_driver(mcp4725_driver
);
558 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
559 MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
560 MODULE_LICENSE("GPL");