]> Pileus Git - ~andy/linux/blob - drivers/iio/dac/mcp4725.c
iio: add store_eeprom to mcp4725 dac driver
[~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 };
35
36 #ifdef CONFIG_PM_SLEEP
37 static int mcp4725_suspend(struct device *dev)
38 {
39         u8 outbuf[2];
40
41         outbuf[0] = 0x3 << 4; /* power-down bits, 500 kOhm resistor */
42         outbuf[1] = 0;
43
44         return i2c_master_send(to_i2c_client(dev), outbuf, 2);
45 }
46
47 static int mcp4725_resume(struct device *dev)
48 {
49         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
50         struct mcp4725_data *data = iio_priv(indio_dev);
51         u8 outbuf[2];
52
53         /* restore previous DAC value */
54         outbuf[0] = (data->dac_value >> 8) & 0xf;
55         outbuf[1] = data->dac_value & 0xff;
56
57         return i2c_master_send(to_i2c_client(dev), outbuf, 2);
58 }
59
60 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume);
61 #define MCP4725_PM_OPS (&mcp4725_pm_ops)
62 #else
63 #define MCP4725_PM_OPS NULL
64 #endif
65
66 static int mcp4725_store_eeprom(struct device *dev,
67         struct device_attribute *attr, const char *buf, size_t len)
68 {
69         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
70         struct mcp4725_data *data = iio_priv(indio_dev);
71         int tries = 20;
72         u8 inoutbuf[3];
73         bool state;
74         int ret;
75
76         ret = strtobool(buf, &state);
77         if (ret < 0)
78                 return ret;
79
80         if (!state)
81                 return 0;
82
83         inoutbuf[0] = 0x60; /* write EEPROM */
84         inoutbuf[1] = data->dac_value >> 4;
85         inoutbuf[2] = (data->dac_value & 0xf) << 4;
86
87         ret = i2c_master_send(data->client, inoutbuf, 3);
88         if (ret < 0)
89                 return ret;
90         else if (ret != 3)
91                 return -EIO;
92
93         /* wait for write complete, takes up to 50ms */
94         while (tries--) {
95                 msleep(20);
96                 ret = i2c_master_recv(data->client, inoutbuf, 3);
97                 if (ret < 0)
98                         return ret;
99                 else if (ret != 3)
100                         return -EIO;
101
102                 if (inoutbuf[0] & 0x80)
103                         break;
104         }
105
106         if (tries < 0) {
107                 dev_err(&data->client->dev,
108                         "mcp4725_store_eeprom() failed, incomplete\n");
109                 return -EIO;
110         }
111
112         return len;
113 }
114
115 static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
116
117 static struct attribute *mcp4725_attributes[] = {
118         &iio_dev_attr_store_eeprom.dev_attr.attr,
119         NULL,
120 };
121
122 static const struct attribute_group mcp4725_attribute_group = {
123         .attrs = mcp4725_attributes,
124 };
125
126 static const struct iio_chan_spec mcp4725_channel = {
127         .type           = IIO_VOLTAGE,
128         .indexed        = 1,
129         .output         = 1,
130         .channel        = 0,
131         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
132         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
133         .scan_type      = IIO_ST('u', 12, 16, 0),
134 };
135
136 static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
137 {
138         struct mcp4725_data *data = iio_priv(indio_dev);
139         u8 outbuf[2];
140         int ret;
141
142         if (val >= (1 << 12) || val < 0)
143                 return -EINVAL;
144
145         outbuf[0] = (val >> 8) & 0xf;
146         outbuf[1] = val & 0xff;
147
148         ret = i2c_master_send(data->client, outbuf, 2);
149         if (ret < 0)
150                 return ret;
151         else if (ret != 2)
152                 return -EIO;
153         else
154                 return 0;
155 }
156
157 static int mcp4725_read_raw(struct iio_dev *indio_dev,
158                            struct iio_chan_spec const *chan,
159                            int *val, int *val2, long mask)
160 {
161         struct mcp4725_data *data = iio_priv(indio_dev);
162         unsigned long scale_uv;
163
164         switch (mask) {
165         case IIO_CHAN_INFO_RAW:
166                 *val = data->dac_value;
167                 return IIO_VAL_INT;
168         case IIO_CHAN_INFO_SCALE:
169                 scale_uv = (data->vref_mv * 1000) >> 12;
170                 *val =  scale_uv / 1000000;
171                 *val2 = scale_uv % 1000000;
172                 return IIO_VAL_INT_PLUS_MICRO;
173         }
174         return -EINVAL;
175 }
176
177 static int mcp4725_write_raw(struct iio_dev *indio_dev,
178                                struct iio_chan_spec const *chan,
179                                int val, int val2, long mask)
180 {
181         struct mcp4725_data *data = iio_priv(indio_dev);
182         int ret;
183
184         switch (mask) {
185         case IIO_CHAN_INFO_RAW:
186                 ret = mcp4725_set_value(indio_dev, val);
187                 data->dac_value = val;
188                 break;
189         default:
190                 ret = -EINVAL;
191                 break;
192         }
193
194         return ret;
195 }
196
197 static const struct iio_info mcp4725_info = {
198         .read_raw = mcp4725_read_raw,
199         .write_raw = mcp4725_write_raw,
200         .attrs = &mcp4725_attribute_group,
201         .driver_module = THIS_MODULE,
202 };
203
204 static int mcp4725_probe(struct i2c_client *client,
205                          const struct i2c_device_id *id)
206 {
207         struct mcp4725_data *data;
208         struct iio_dev *indio_dev;
209         struct mcp4725_platform_data *platform_data = client->dev.platform_data;
210         u8 inbuf[3];
211         int err;
212
213         if (!platform_data || !platform_data->vref_mv) {
214                 dev_err(&client->dev, "invalid platform data");
215                 err = -EINVAL;
216                 goto exit;
217         }
218
219         indio_dev = iio_device_alloc(sizeof(*data));
220         if (indio_dev == NULL) {
221                 err = -ENOMEM;
222                 goto exit;
223         }
224         data = iio_priv(indio_dev);
225         i2c_set_clientdata(client, indio_dev);
226         data->client = client;
227
228         indio_dev->dev.parent = &client->dev;
229         indio_dev->info = &mcp4725_info;
230         indio_dev->channels = &mcp4725_channel;
231         indio_dev->num_channels = 1;
232         indio_dev->modes = INDIO_DIRECT_MODE;
233
234         data->vref_mv = platform_data->vref_mv;
235
236         /* read current DAC value */
237         err = i2c_master_recv(client, inbuf, 3);
238         if (err < 0) {
239                 dev_err(&client->dev, "failed to read DAC value");
240                 goto exit_free_device;
241         }
242         data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
243
244         err = iio_device_register(indio_dev);
245         if (err)
246                 goto exit_free_device;
247
248         dev_info(&client->dev, "MCP4725 DAC registered\n");
249
250         return 0;
251
252 exit_free_device:
253         iio_device_free(indio_dev);
254 exit:
255         return err;
256 }
257
258 static int mcp4725_remove(struct i2c_client *client)
259 {
260         struct iio_dev *indio_dev = i2c_get_clientdata(client);
261
262         iio_device_unregister(indio_dev);
263         iio_device_free(indio_dev);
264
265         return 0;
266 }
267
268 static const struct i2c_device_id mcp4725_id[] = {
269         { "mcp4725", 0 },
270         { }
271 };
272 MODULE_DEVICE_TABLE(i2c, mcp4725_id);
273
274 static struct i2c_driver mcp4725_driver = {
275         .driver = {
276                 .name   = MCP4725_DRV_NAME,
277                 .pm     = MCP4725_PM_OPS,
278         },
279         .probe          = mcp4725_probe,
280         .remove         = mcp4725_remove,
281         .id_table       = mcp4725_id,
282 };
283 module_i2c_driver(mcp4725_driver);
284
285 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
286 MODULE_DESCRIPTION("MCP4725 12-bit DAC");
287 MODULE_LICENSE("GPL");