2 * mcp4725.c - Support for Microchip MCP4725
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
16 * writing the DAC value to EEPROM is not supported
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/i2c.h>
22 #include <linux/err.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"
32 struct i2c_client
*client
;
37 #ifdef CONFIG_PM_SLEEP
38 static int mcp4725_suspend(struct device
*dev
)
42 outbuf
[0] = 0x3 << 4; /* power-down bits, 500 kOhm resistor */
45 return i2c_master_send(to_i2c_client(dev
), outbuf
, 2);
48 static int mcp4725_resume(struct device
*dev
)
50 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
51 struct mcp4725_data
*data
= iio_priv(indio_dev
);
54 /* restore previous DAC value */
55 outbuf
[0] = (data
->dac_value
>> 8) & 0xf;
56 outbuf
[1] = data
->dac_value
& 0xff;
58 return i2c_master_send(to_i2c_client(dev
), outbuf
, 2);
61 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops
, mcp4725_suspend
, mcp4725_resume
);
62 #define MCP4725_PM_OPS (&mcp4725_pm_ops)
64 #define MCP4725_PM_OPS NULL
67 static const struct iio_chan_spec mcp4725_channel
= {
72 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
73 IIO_CHAN_INFO_SCALE_SHARED_BIT
,
74 .scan_type
= IIO_ST('u', 12, 16, 0),
77 static int mcp4725_set_value(struct iio_dev
*indio_dev
, int val
)
79 struct mcp4725_data
*data
= iio_priv(indio_dev
);
83 if (val
>= (1 << 12) || val
< 0)
86 outbuf
[0] = (val
>> 8) & 0xf;
87 outbuf
[1] = val
& 0xff;
89 ret
= i2c_master_send(data
->client
, outbuf
, 2);
98 static int mcp4725_read_raw(struct iio_dev
*indio_dev
,
99 struct iio_chan_spec
const *chan
,
100 int *val
, int *val2
, long mask
)
102 struct mcp4725_data
*data
= iio_priv(indio_dev
);
103 unsigned long scale_uv
;
106 case IIO_CHAN_INFO_RAW
:
107 *val
= data
->dac_value
;
109 case IIO_CHAN_INFO_SCALE
:
110 scale_uv
= (data
->vref_mv
* 1000) >> 12;
111 *val
= scale_uv
/ 1000000;
112 *val2
= scale_uv
% 1000000;
113 return IIO_VAL_INT_PLUS_MICRO
;
118 static int mcp4725_write_raw(struct iio_dev
*indio_dev
,
119 struct iio_chan_spec
const *chan
,
120 int val
, int val2
, long mask
)
122 struct mcp4725_data
*data
= iio_priv(indio_dev
);
126 case IIO_CHAN_INFO_RAW
:
127 ret
= mcp4725_set_value(indio_dev
, val
);
128 data
->dac_value
= val
;
138 static const struct iio_info mcp4725_info
= {
139 .read_raw
= mcp4725_read_raw
,
140 .write_raw
= mcp4725_write_raw
,
141 .driver_module
= THIS_MODULE
,
144 static int mcp4725_probe(struct i2c_client
*client
,
145 const struct i2c_device_id
*id
)
147 struct mcp4725_data
*data
;
148 struct iio_dev
*indio_dev
;
149 struct mcp4725_platform_data
*platform_data
= client
->dev
.platform_data
;
153 if (!platform_data
|| !platform_data
->vref_mv
) {
154 dev_err(&client
->dev
, "invalid platform data");
159 indio_dev
= iio_device_alloc(sizeof(*data
));
160 if (indio_dev
== NULL
) {
164 data
= iio_priv(indio_dev
);
165 i2c_set_clientdata(client
, indio_dev
);
166 data
->client
= client
;
168 indio_dev
->dev
.parent
= &client
->dev
;
169 indio_dev
->info
= &mcp4725_info
;
170 indio_dev
->channels
= &mcp4725_channel
;
171 indio_dev
->num_channels
= 1;
172 indio_dev
->modes
= INDIO_DIRECT_MODE
;
174 data
->vref_mv
= platform_data
->vref_mv
;
176 /* read current DAC value */
177 err
= i2c_master_recv(client
, inbuf
, 3);
179 dev_err(&client
->dev
, "failed to read DAC value");
180 goto exit_free_device
;
182 data
->dac_value
= (inbuf
[1] << 4) | (inbuf
[2] >> 4);
184 err
= iio_device_register(indio_dev
);
186 goto exit_free_device
;
188 dev_info(&client
->dev
, "MCP4725 DAC registered\n");
193 iio_device_free(indio_dev
);
198 static int mcp4725_remove(struct i2c_client
*client
)
200 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
202 iio_device_unregister(indio_dev
);
203 iio_device_free(indio_dev
);
208 static const struct i2c_device_id mcp4725_id
[] = {
212 MODULE_DEVICE_TABLE(i2c
, mcp4725_id
);
214 static struct i2c_driver mcp4725_driver
= {
216 .name
= MCP4725_DRV_NAME
,
217 .pm
= MCP4725_PM_OPS
,
219 .probe
= mcp4725_probe
,
220 .remove
= mcp4725_remove
,
221 .id_table
= mcp4725_id
,
223 module_i2c_driver(mcp4725_driver
);
225 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
226 MODULE_DESCRIPTION("MCP4725 12-bit DAC");
227 MODULE_LICENSE("GPL");