]> Pileus Git - ~andy/linux/blob - drivers/iio/dac/mcp4725.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[~andy/linux] / drivers / iio / dac / mcp4725.c
1 /*
2  * mcp4725.c - Support for Microchip MCP4725
3  *
4  * Copyright (C) 2012 Peter Meerwald <pmeerw@pmeerw.net>
5  *
6  * Based on max517 by Roland Stigge <stigge@antcom.de>
7  *
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.
11  *
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
14  * hardware)
15  */
16
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>
22
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25
26 #include <linux/iio/dac/mcp4725.h>
27
28 #define MCP4725_DRV_NAME "mcp4725"
29
30 struct mcp4725_data {
31         struct i2c_client *client;
32         u16 vref_mv;
33         u16 dac_value;
34         bool powerdown;
35         unsigned powerdown_mode;
36 };
37
38 static int mcp4725_suspend(struct device *dev)
39 {
40         struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
41                 to_i2c_client(dev)));
42         u8 outbuf[2];
43
44         outbuf[0] = (data->powerdown_mode + 1) << 4;
45         outbuf[1] = 0;
46         data->powerdown = true;
47
48         return i2c_master_send(data->client, outbuf, 2);
49 }
50
51 static int mcp4725_resume(struct device *dev)
52 {
53         struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
54                 to_i2c_client(dev)));
55         u8 outbuf[2];
56
57         /* restore previous DAC value */
58         outbuf[0] = (data->dac_value >> 8) & 0xf;
59         outbuf[1] = data->dac_value & 0xff;
60         data->powerdown = false;
61
62         return i2c_master_send(data->client, outbuf, 2);
63 }
64
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)
68 #else
69 #define MCP4725_PM_OPS NULL
70 #endif
71
72 static ssize_t mcp4725_store_eeprom(struct device *dev,
73         struct device_attribute *attr, const char *buf, size_t len)
74 {
75         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
76         struct mcp4725_data *data = iio_priv(indio_dev);
77         int tries = 20;
78         u8 inoutbuf[3];
79         bool state;
80         int ret;
81
82         ret = strtobool(buf, &state);
83         if (ret < 0)
84                 return ret;
85
86         if (!state)
87                 return 0;
88
89         inoutbuf[0] = 0x60; /* write EEPROM */
90         inoutbuf[1] = data->dac_value >> 4;
91         inoutbuf[2] = (data->dac_value & 0xf) << 4;
92
93         ret = i2c_master_send(data->client, inoutbuf, 3);
94         if (ret < 0)
95                 return ret;
96         else if (ret != 3)
97                 return -EIO;
98
99         /* wait for write complete, takes up to 50ms */
100         while (tries--) {
101                 msleep(20);
102                 ret = i2c_master_recv(data->client, inoutbuf, 3);
103                 if (ret < 0)
104                         return ret;
105                 else if (ret != 3)
106                         return -EIO;
107
108                 if (inoutbuf[0] & 0x80)
109                         break;
110         }
111
112         if (tries < 0) {
113                 dev_err(&data->client->dev,
114                         "mcp4725_store_eeprom() failed, incomplete\n");
115                 return -EIO;
116         }
117
118         return len;
119 }
120
121 static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
122
123 static struct attribute *mcp4725_attributes[] = {
124         &iio_dev_attr_store_eeprom.dev_attr.attr,
125         NULL,
126 };
127
128 static const struct attribute_group mcp4725_attribute_group = {
129         .attrs = mcp4725_attributes,
130 };
131
132 static const char * const mcp4725_powerdown_modes[] = {
133         "1kohm_to_gnd",
134         "100kohm_to_gnd",
135         "500kohm_to_gnd"
136 };
137
138 static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev,
139         const struct iio_chan_spec *chan)
140 {
141         struct mcp4725_data *data = iio_priv(indio_dev);
142
143         return data->powerdown_mode;
144 }
145
146 static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev,
147         const struct iio_chan_spec *chan, unsigned mode)
148 {
149         struct mcp4725_data *data = iio_priv(indio_dev);
150
151         data->powerdown_mode = mode;
152
153         return 0;
154 }
155
156 static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev,
157         uintptr_t private, const struct iio_chan_spec *chan, char *buf)
158 {
159         struct mcp4725_data *data = iio_priv(indio_dev);
160
161         return sprintf(buf, "%d\n", data->powerdown);
162 }
163
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)
167 {
168         struct mcp4725_data *data = iio_priv(indio_dev);
169         bool state;
170         int ret;
171
172         ret = strtobool(buf, &state);
173         if (ret)
174                 return ret;
175
176         if (state)
177                 ret = mcp4725_suspend(&data->client->dev);
178         else
179                 ret = mcp4725_resume(&data->client->dev);
180         if (ret < 0)
181                 return ret;
182
183         return len;
184 }
185
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,
191 };
192
193 static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = {
194         {
195                 .name = "powerdown",
196                 .read = mcp4725_read_powerdown,
197                 .write = mcp4725_write_powerdown,
198                 .shared = IIO_SEPARATE,
199         },
200         IIO_ENUM("powerdown_mode", IIO_SEPARATE, &mcp4725_powerdown_mode_enum),
201         IIO_ENUM_AVAILABLE("powerdown_mode", &mcp4725_powerdown_mode_enum),
202         { },
203 };
204
205 static const struct iio_chan_spec mcp4725_channel = {
206         .type           = IIO_VOLTAGE,
207         .indexed        = 1,
208         .output         = 1,
209         .channel        = 0,
210         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
211         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
212         .ext_info       = mcp4725_ext_info,
213 };
214
215 static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
216 {
217         struct mcp4725_data *data = iio_priv(indio_dev);
218         u8 outbuf[2];
219         int ret;
220
221         if (val >= (1 << 12) || val < 0)
222                 return -EINVAL;
223
224         outbuf[0] = (val >> 8) & 0xf;
225         outbuf[1] = val & 0xff;
226
227         ret = i2c_master_send(data->client, outbuf, 2);
228         if (ret < 0)
229                 return ret;
230         else if (ret != 2)
231                 return -EIO;
232         else
233                 return 0;
234 }
235
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)
239 {
240         struct mcp4725_data *data = iio_priv(indio_dev);
241
242         switch (mask) {
243         case IIO_CHAN_INFO_RAW:
244                 *val = data->dac_value;
245                 return IIO_VAL_INT;
246         case IIO_CHAN_INFO_SCALE:
247                 *val = data->vref_mv;
248                 *val2 = 12;
249                 return IIO_VAL_FRACTIONAL_LOG2;
250         }
251         return -EINVAL;
252 }
253
254 static int mcp4725_write_raw(struct iio_dev *indio_dev,
255                                struct iio_chan_spec const *chan,
256                                int val, int val2, long mask)
257 {
258         struct mcp4725_data *data = iio_priv(indio_dev);
259         int ret;
260
261         switch (mask) {
262         case IIO_CHAN_INFO_RAW:
263                 ret = mcp4725_set_value(indio_dev, val);
264                 data->dac_value = val;
265                 break;
266         default:
267                 ret = -EINVAL;
268                 break;
269         }
270
271         return ret;
272 }
273
274 static const struct iio_info mcp4725_info = {
275         .read_raw = mcp4725_read_raw,
276         .write_raw = mcp4725_write_raw,
277         .attrs = &mcp4725_attribute_group,
278         .driver_module = THIS_MODULE,
279 };
280
281 static int mcp4725_probe(struct i2c_client *client,
282                          const struct i2c_device_id *id)
283 {
284         struct mcp4725_data *data;
285         struct iio_dev *indio_dev;
286         struct mcp4725_platform_data *platform_data = client->dev.platform_data;
287         u8 inbuf[3];
288         u8 pd;
289         int err;
290
291         if (!platform_data || !platform_data->vref_mv) {
292                 dev_err(&client->dev, "invalid platform data");
293                 return -EINVAL;
294         }
295
296         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
297         if (indio_dev == NULL)
298                 return -ENOMEM;
299         data = iio_priv(indio_dev);
300         i2c_set_clientdata(client, indio_dev);
301         data->client = client;
302
303         indio_dev->dev.parent = &client->dev;
304         indio_dev->info = &mcp4725_info;
305         indio_dev->channels = &mcp4725_channel;
306         indio_dev->num_channels = 1;
307         indio_dev->modes = INDIO_DIRECT_MODE;
308
309         data->vref_mv = platform_data->vref_mv;
310
311         /* read current DAC value */
312         err = i2c_master_recv(client, inbuf, 3);
313         if (err < 0) {
314                 dev_err(&client->dev, "failed to read DAC value");
315                 return err;
316         }
317         pd = (inbuf[0] >> 1) & 0x3;
318         data->powerdown = pd > 0 ? true : false;
319         data->powerdown_mode = pd ? pd-1 : 2; /* 500kohm_to_gnd */
320         data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
321
322         return iio_device_register(indio_dev);
323 }
324
325 static int mcp4725_remove(struct i2c_client *client)
326 {
327         iio_device_unregister(i2c_get_clientdata(client));
328         return 0;
329 }
330
331 static const struct i2c_device_id mcp4725_id[] = {
332         { "mcp4725", 0 },
333         { }
334 };
335 MODULE_DEVICE_TABLE(i2c, mcp4725_id);
336
337 static struct i2c_driver mcp4725_driver = {
338         .driver = {
339                 .name   = MCP4725_DRV_NAME,
340                 .pm     = MCP4725_PM_OPS,
341         },
342         .probe          = mcp4725_probe,
343         .remove         = mcp4725_remove,
344         .id_table       = mcp4725_id,
345 };
346 module_i2c_driver(mcp4725_driver);
347
348 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
349 MODULE_DESCRIPTION("MCP4725 12-bit DAC");
350 MODULE_LICENSE("GPL");