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
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/i2c.h>
20 #include <linux/err.h>
21 #include <linux/delay.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
26 #include <linux/iio/dac/mcp4725.h>
28 #define MCP4725_DRV_NAME "mcp4725"
31 struct i2c_client
*client
;
35 unsigned powerdown_mode
;
38 static int mcp4725_suspend(struct device
*dev
)
40 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
41 struct mcp4725_data
*data
= iio_priv(indio_dev
);
44 outbuf
[0] = (data
->powerdown_mode
+ 1) << 4;
46 data
->powerdown
= true;
48 return i2c_master_send(to_i2c_client(dev
), outbuf
, 2);
51 static int mcp4725_resume(struct device
*dev
)
53 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
54 struct mcp4725_data
*data
= iio_priv(indio_dev
);
57 /* restore previous DAC value */
58 outbuf
[0] = (data
->dac_value
>> 8) & 0xf;
59 outbuf
[1] = data
->dac_value
& 0xff;
60 data
->powerdown
= false;
62 return i2c_master_send(to_i2c_client(dev
), outbuf
, 2);
65 #ifdef CONFIG_PM_SLEEP
66 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops
, mcp4725_suspend
, mcp4725_resume
);
67 #define MCP4725_PM_OPS (&mcp4725_pm_ops)
69 #define MCP4725_PM_OPS NULL
72 static ssize_t
mcp4725_store_eeprom(struct device
*dev
,
73 struct device_attribute
*attr
, const char *buf
, size_t len
)
75 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
76 struct mcp4725_data
*data
= iio_priv(indio_dev
);
82 ret
= strtobool(buf
, &state
);
89 inoutbuf
[0] = 0x60; /* write EEPROM */
90 inoutbuf
[1] = data
->dac_value
>> 4;
91 inoutbuf
[2] = (data
->dac_value
& 0xf) << 4;
93 ret
= i2c_master_send(data
->client
, inoutbuf
, 3);
99 /* wait for write complete, takes up to 50ms */
102 ret
= i2c_master_recv(data
->client
, inoutbuf
, 3);
108 if (inoutbuf
[0] & 0x80)
113 dev_err(&data
->client
->dev
,
114 "mcp4725_store_eeprom() failed, incomplete\n");
121 static IIO_DEVICE_ATTR(store_eeprom
, S_IWUSR
, NULL
, mcp4725_store_eeprom
, 0);
123 static struct attribute
*mcp4725_attributes
[] = {
124 &iio_dev_attr_store_eeprom
.dev_attr
.attr
,
128 static const struct attribute_group mcp4725_attribute_group
= {
129 .attrs
= mcp4725_attributes
,
132 static const char * const mcp4725_powerdown_modes
[] = {
138 static int mcp4725_get_powerdown_mode(struct iio_dev
*indio_dev
,
139 const struct iio_chan_spec
*chan
)
141 struct mcp4725_data
*data
= iio_priv(indio_dev
);
143 return data
->powerdown_mode
;
146 static int mcp4725_set_powerdown_mode(struct iio_dev
*indio_dev
,
147 const struct iio_chan_spec
*chan
, unsigned mode
)
149 struct mcp4725_data
*data
= iio_priv(indio_dev
);
151 data
->powerdown_mode
= mode
;
156 static ssize_t
mcp4725_read_powerdown(struct iio_dev
*indio_dev
,
157 uintptr_t private, const struct iio_chan_spec
*chan
, char *buf
)
159 struct mcp4725_data
*data
= iio_priv(indio_dev
);
161 return sprintf(buf
, "%d\n", data
->powerdown
);
164 static ssize_t
mcp4725_write_powerdown(struct iio_dev
*indio_dev
,
165 uintptr_t private, const struct iio_chan_spec
*chan
,
166 const char *buf
, size_t len
)
168 struct mcp4725_data
*data
= iio_priv(indio_dev
);
172 ret
= strtobool(buf
, &state
);
177 ret
= mcp4725_suspend(&data
->client
->dev
);
179 ret
= mcp4725_resume(&data
->client
->dev
);
186 static const struct iio_enum mcp4725_powerdown_mode_enum
= {
187 .items
= mcp4725_powerdown_modes
,
188 .num_items
= ARRAY_SIZE(mcp4725_powerdown_modes
),
189 .get
= mcp4725_get_powerdown_mode
,
190 .set
= mcp4725_set_powerdown_mode
,
193 static const struct iio_chan_spec_ext_info mcp4725_ext_info
[] = {
196 .read
= mcp4725_read_powerdown
,
197 .write
= mcp4725_write_powerdown
,
199 IIO_ENUM("powerdown_mode", false, &mcp4725_powerdown_mode_enum
),
200 IIO_ENUM_AVAILABLE("powerdown_mode", &mcp4725_powerdown_mode_enum
),
204 static const struct iio_chan_spec mcp4725_channel
= {
209 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
210 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
211 .scan_type
= IIO_ST('u', 12, 16, 0),
212 .ext_info
= mcp4725_ext_info
,
215 static int mcp4725_set_value(struct iio_dev
*indio_dev
, int val
)
217 struct mcp4725_data
*data
= iio_priv(indio_dev
);
221 if (val
>= (1 << 12) || val
< 0)
224 outbuf
[0] = (val
>> 8) & 0xf;
225 outbuf
[1] = val
& 0xff;
227 ret
= i2c_master_send(data
->client
, outbuf
, 2);
236 static int mcp4725_read_raw(struct iio_dev
*indio_dev
,
237 struct iio_chan_spec
const *chan
,
238 int *val
, int *val2
, long mask
)
240 struct mcp4725_data
*data
= iio_priv(indio_dev
);
241 unsigned long scale_uv
;
244 case IIO_CHAN_INFO_RAW
:
245 *val
= data
->dac_value
;
247 case IIO_CHAN_INFO_SCALE
:
248 scale_uv
= (data
->vref_mv
* 1000) >> 12;
249 *val
= scale_uv
/ 1000000;
250 *val2
= scale_uv
% 1000000;
251 return IIO_VAL_INT_PLUS_MICRO
;
256 static int mcp4725_write_raw(struct iio_dev
*indio_dev
,
257 struct iio_chan_spec
const *chan
,
258 int val
, int val2
, long mask
)
260 struct mcp4725_data
*data
= iio_priv(indio_dev
);
264 case IIO_CHAN_INFO_RAW
:
265 ret
= mcp4725_set_value(indio_dev
, val
);
266 data
->dac_value
= val
;
276 static const struct iio_info mcp4725_info
= {
277 .read_raw
= mcp4725_read_raw
,
278 .write_raw
= mcp4725_write_raw
,
279 .attrs
= &mcp4725_attribute_group
,
280 .driver_module
= THIS_MODULE
,
283 static int mcp4725_probe(struct i2c_client
*client
,
284 const struct i2c_device_id
*id
)
286 struct mcp4725_data
*data
;
287 struct iio_dev
*indio_dev
;
288 struct mcp4725_platform_data
*platform_data
= client
->dev
.platform_data
;
293 if (!platform_data
|| !platform_data
->vref_mv
) {
294 dev_err(&client
->dev
, "invalid platform data");
298 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
299 if (indio_dev
== NULL
)
301 data
= iio_priv(indio_dev
);
302 i2c_set_clientdata(client
, indio_dev
);
303 data
->client
= client
;
305 indio_dev
->dev
.parent
= &client
->dev
;
306 indio_dev
->info
= &mcp4725_info
;
307 indio_dev
->channels
= &mcp4725_channel
;
308 indio_dev
->num_channels
= 1;
309 indio_dev
->modes
= INDIO_DIRECT_MODE
;
311 data
->vref_mv
= platform_data
->vref_mv
;
313 /* read current DAC value */
314 err
= i2c_master_recv(client
, inbuf
, 3);
316 dev_err(&client
->dev
, "failed to read DAC value");
319 pd
= (inbuf
[0] >> 1) & 0x3;
320 data
->powerdown
= pd
> 0 ? true : false;
321 data
->powerdown_mode
= pd
? pd
-1 : 2; /* 500kohm_to_gnd */
322 data
->dac_value
= (inbuf
[1] << 4) | (inbuf
[2] >> 4);
324 err
= iio_device_register(indio_dev
);
328 dev_info(&client
->dev
, "MCP4725 DAC registered\n");
333 static int mcp4725_remove(struct i2c_client
*client
)
335 iio_device_unregister(i2c_get_clientdata(client
));
339 static const struct i2c_device_id mcp4725_id
[] = {
343 MODULE_DEVICE_TABLE(i2c
, mcp4725_id
);
345 static struct i2c_driver mcp4725_driver
= {
347 .name
= MCP4725_DRV_NAME
,
348 .pm
= MCP4725_PM_OPS
,
350 .probe
= mcp4725_probe
,
351 .remove
= mcp4725_remove
,
352 .id_table
= mcp4725_id
,
354 module_i2c_driver(mcp4725_driver
);
356 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
357 MODULE_DESCRIPTION("MCP4725 12-bit DAC");
358 MODULE_LICENSE("GPL");