1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/i2c.h>
22#include <linux/err.h>
23
24#include <linux/iio/iio.h>
25#include <linux/iio/sysfs.h>
26
27#include <linux/iio/dac/mcp4725.h>
28
29#define MCP4725_DRV_NAME "mcp4725"
30
31struct mcp4725_data {
32 struct i2c_client *client;
33 u16 vref_mv;
34 u16 dac_value;
35};
36
37#ifdef CONFIG_PM_SLEEP
38static int mcp4725_suspend(struct device *dev)
39{
40 u8 outbuf[2];
41
42 outbuf[0] = 0x3 << 4;
43 outbuf[1] = 0;
44
45 return i2c_master_send(to_i2c_client(dev), outbuf, 2);
46}
47
48static int mcp4725_resume(struct device *dev)
49{
50 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
51 struct mcp4725_data *data = iio_priv(indio_dev);
52 u8 outbuf[2];
53
54
55 outbuf[0] = (data->dac_value >> 8) & 0xf;
56 outbuf[1] = data->dac_value & 0xff;
57
58 return i2c_master_send(to_i2c_client(dev), outbuf, 2);
59}
60
61static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume);
62#define MCP4725_PM_OPS (&mcp4725_pm_ops)
63#else
64#define MCP4725_PM_OPS NULL
65#endif
66
67static const struct iio_chan_spec mcp4725_channel = {
68 .type = IIO_VOLTAGE,
69 .indexed = 1,
70 .output = 1,
71 .channel = 0,
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),
75};
76
77static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
78{
79 struct mcp4725_data *data = iio_priv(indio_dev);
80 u8 outbuf[2];
81 int ret;
82
83 if (val >= (1 << 12) || val < 0)
84 return -EINVAL;
85
86 outbuf[0] = (val >> 8) & 0xf;
87 outbuf[1] = val & 0xff;
88
89 ret = i2c_master_send(data->client, outbuf, 2);
90 if (ret < 0)
91 return ret;
92 else if (ret != 2)
93 return -EIO;
94 else
95 return 0;
96}
97
98static int mcp4725_read_raw(struct iio_dev *indio_dev,
99 struct iio_chan_spec const *chan,
100 int *val, int *val2, long mask)
101{
102 struct mcp4725_data *data = iio_priv(indio_dev);
103 unsigned long scale_uv;
104
105 switch (mask) {
106 case IIO_CHAN_INFO_RAW:
107 *val = data->dac_value;
108 return IIO_VAL_INT;
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;
114 }
115 return -EINVAL;
116}
117
118static int mcp4725_write_raw(struct iio_dev *indio_dev,
119 struct iio_chan_spec const *chan,
120 int val, int val2, long mask)
121{
122 struct mcp4725_data *data = iio_priv(indio_dev);
123 int ret;
124
125 switch (mask) {
126 case IIO_CHAN_INFO_RAW:
127 ret = mcp4725_set_value(indio_dev, val);
128 data->dac_value = val;
129 break;
130 default:
131 ret = -EINVAL;
132 break;
133 }
134
135 return ret;
136}
137
138static const struct iio_info mcp4725_info = {
139 .read_raw = mcp4725_read_raw,
140 .write_raw = mcp4725_write_raw,
141 .driver_module = THIS_MODULE,
142};
143
144static int __devinit mcp4725_probe(struct i2c_client *client,
145 const struct i2c_device_id *id)
146{
147 struct mcp4725_data *data;
148 struct iio_dev *indio_dev;
149 struct mcp4725_platform_data *platform_data = client->dev.platform_data;
150 u8 inbuf[3];
151 int err;
152
153 if (!platform_data || !platform_data->vref_mv) {
154 dev_err(&client->dev, "invalid platform data");
155 err = -EINVAL;
156 goto exit;
157 }
158
159 indio_dev = iio_device_alloc(sizeof(*data));
160 if (indio_dev == NULL) {
161 err = -ENOMEM;
162 goto exit;
163 }
164 data = iio_priv(indio_dev);
165 i2c_set_clientdata(client, indio_dev);
166 data->client = client;
167
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;
173
174 data->vref_mv = platform_data->vref_mv;
175
176
177 err = i2c_master_recv(client, inbuf, 3);
178 if (err < 0) {
179 dev_err(&client->dev, "failed to read DAC value");
180 goto exit_free_device;
181 }
182 data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
183
184 err = iio_device_register(indio_dev);
185 if (err)
186 goto exit_free_device;
187
188 dev_info(&client->dev, "MCP4725 DAC registered\n");
189
190 return 0;
191
192exit_free_device:
193 iio_device_free(indio_dev);
194exit:
195 return err;
196}
197
198static int __devexit mcp4725_remove(struct i2c_client *client)
199{
200 struct iio_dev *indio_dev = i2c_get_clientdata(client);
201
202 iio_device_unregister(indio_dev);
203 iio_device_free(indio_dev);
204
205 return 0;
206}
207
208static const struct i2c_device_id mcp4725_id[] = {
209 { "mcp4725", 0 },
210 { }
211};
212MODULE_DEVICE_TABLE(i2c, mcp4725_id);
213
214static struct i2c_driver mcp4725_driver = {
215 .driver = {
216 .name = MCP4725_DRV_NAME,
217 .pm = MCP4725_PM_OPS,
218 },
219 .probe = mcp4725_probe,
220 .remove = __devexit_p(mcp4725_remove),
221 .id_table = mcp4725_id,
222};
223module_i2c_driver(mcp4725_driver);
224
225MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
226MODULE_DESCRIPTION("MCP4725 12-bit DAC");
227MODULE_LICENSE("GPL");
228