blob: cca935c06f2b6fbac5fd85763fc85c1bc99e37ed [file] [log] [blame]
Peter Meerwaldcf35ad62012-06-08 18:06:45 +02001/*
Akinobu Mita35f73962015-12-26 21:17:22 +09002 * mcp4725.c - Support for Microchip MCP4725/6
Peter Meerwaldcf35ad62012-06-08 18:06:45 +02003 *
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)
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020015 */
16
17#include <linux/module.h>
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020018#include <linux/i2c.h>
19#include <linux/err.h>
Peter Meerwaldf17b77d2013-06-23 22:30:00 +010020#include <linux/delay.h>
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020021
22#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24
25#include <linux/iio/dac/mcp4725.h>
26
27#define MCP4725_DRV_NAME "mcp4725"
28
29struct mcp4725_data {
30 struct i2c_client *client;
31 u16 vref_mv;
32 u16 dac_value;
Peter Meerwald155f1b42013-06-23 22:30:00 +010033 bool powerdown;
34 unsigned powerdown_mode;
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020035};
36
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020037static int mcp4725_suspend(struct device *dev)
38{
Peter Meerwalda97dd062013-09-18 22:47:00 +010039 struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
40 to_i2c_client(dev)));
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020041 u8 outbuf[2];
42
Peter Meerwald155f1b42013-06-23 22:30:00 +010043 outbuf[0] = (data->powerdown_mode + 1) << 4;
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020044 outbuf[1] = 0;
Peter Meerwald155f1b42013-06-23 22:30:00 +010045 data->powerdown = true;
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020046
Peter Meerwalda97dd062013-09-18 22:47:00 +010047 return i2c_master_send(data->client, outbuf, 2);
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020048}
49
50static int mcp4725_resume(struct device *dev)
51{
Peter Meerwalda97dd062013-09-18 22:47:00 +010052 struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
53 to_i2c_client(dev)));
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020054 u8 outbuf[2];
55
56 /* restore previous DAC value */
57 outbuf[0] = (data->dac_value >> 8) & 0xf;
58 outbuf[1] = data->dac_value & 0xff;
Peter Meerwald155f1b42013-06-23 22:30:00 +010059 data->powerdown = false;
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020060
Peter Meerwalda97dd062013-09-18 22:47:00 +010061 return i2c_master_send(data->client, outbuf, 2);
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020062}
63
Peter Meerwald155f1b42013-06-23 22:30:00 +010064#ifdef CONFIG_PM_SLEEP
Peter Meerwaldcf35ad62012-06-08 18:06:45 +020065static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume);
66#define MCP4725_PM_OPS (&mcp4725_pm_ops)
67#else
68#define MCP4725_PM_OPS NULL
69#endif
70
Jonathan Cameron4fd313d2013-07-12 20:54:00 +010071static ssize_t mcp4725_store_eeprom(struct device *dev,
Peter Meerwaldf17b77d2013-06-23 22:30:00 +010072 struct device_attribute *attr, const char *buf, size_t len)
73{
74 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
75 struct mcp4725_data *data = iio_priv(indio_dev);
76 int tries = 20;
77 u8 inoutbuf[3];
78 bool state;
79 int ret;
80
81 ret = strtobool(buf, &state);
82 if (ret < 0)
83 return ret;
84
85 if (!state)
86 return 0;
87
88 inoutbuf[0] = 0x60; /* write EEPROM */
89 inoutbuf[1] = data->dac_value >> 4;
90 inoutbuf[2] = (data->dac_value & 0xf) << 4;
91
92 ret = i2c_master_send(data->client, inoutbuf, 3);
93 if (ret < 0)
94 return ret;
95 else if (ret != 3)
96 return -EIO;
97
98 /* wait for write complete, takes up to 50ms */
99 while (tries--) {
100 msleep(20);
101 ret = i2c_master_recv(data->client, inoutbuf, 3);
102 if (ret < 0)
103 return ret;
104 else if (ret != 3)
105 return -EIO;
106
107 if (inoutbuf[0] & 0x80)
108 break;
109 }
110
111 if (tries < 0) {
112 dev_err(&data->client->dev,
113 "mcp4725_store_eeprom() failed, incomplete\n");
114 return -EIO;
115 }
116
117 return len;
118}
119
120static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
121
122static struct attribute *mcp4725_attributes[] = {
123 &iio_dev_attr_store_eeprom.dev_attr.attr,
124 NULL,
125};
126
127static const struct attribute_group mcp4725_attribute_group = {
128 .attrs = mcp4725_attributes,
129};
130
Peter Meerwald155f1b42013-06-23 22:30:00 +0100131static const char * const mcp4725_powerdown_modes[] = {
132 "1kohm_to_gnd",
133 "100kohm_to_gnd",
134 "500kohm_to_gnd"
135};
136
Akinobu Mita35f73962015-12-26 21:17:22 +0900137static const char * const mcp4726_powerdown_modes[] = {
138 "1kohm_to_gnd",
139 "125kohm_to_gnd",
140 "640kohm_to_gnd"
141};
142
Peter Meerwald155f1b42013-06-23 22:30:00 +0100143static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev,
144 const struct iio_chan_spec *chan)
145{
146 struct mcp4725_data *data = iio_priv(indio_dev);
147
148 return data->powerdown_mode;
149}
150
151static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev,
152 const struct iio_chan_spec *chan, unsigned mode)
153{
154 struct mcp4725_data *data = iio_priv(indio_dev);
155
156 data->powerdown_mode = mode;
157
158 return 0;
159}
160
161static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev,
162 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
163{
164 struct mcp4725_data *data = iio_priv(indio_dev);
165
166 return sprintf(buf, "%d\n", data->powerdown);
167}
168
169static ssize_t mcp4725_write_powerdown(struct iio_dev *indio_dev,
170 uintptr_t private, const struct iio_chan_spec *chan,
171 const char *buf, size_t len)
172{
173 struct mcp4725_data *data = iio_priv(indio_dev);
174 bool state;
175 int ret;
176
177 ret = strtobool(buf, &state);
178 if (ret)
179 return ret;
180
181 if (state)
182 ret = mcp4725_suspend(&data->client->dev);
183 else
184 ret = mcp4725_resume(&data->client->dev);
185 if (ret < 0)
186 return ret;
187
188 return len;
189}
190
Akinobu Mita35f73962015-12-26 21:17:22 +0900191enum {
192 MCP4725,
193 MCP4726,
194};
195
196static const struct iio_enum mcp472x_powerdown_mode_enum[] = {
197 [MCP4725] = {
198 .items = mcp4725_powerdown_modes,
199 .num_items = ARRAY_SIZE(mcp4725_powerdown_modes),
200 .get = mcp4725_get_powerdown_mode,
201 .set = mcp4725_set_powerdown_mode,
202 },
203 [MCP4726] = {
204 .items = mcp4726_powerdown_modes,
205 .num_items = ARRAY_SIZE(mcp4726_powerdown_modes),
206 .get = mcp4725_get_powerdown_mode,
207 .set = mcp4725_set_powerdown_mode,
208 },
Peter Meerwald155f1b42013-06-23 22:30:00 +0100209};
210
211static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = {
212 {
213 .name = "powerdown",
214 .read = mcp4725_read_powerdown,
215 .write = mcp4725_write_powerdown,
Jonathan Cameron37044322013-09-08 14:57:00 +0100216 .shared = IIO_SEPARATE,
Peter Meerwald155f1b42013-06-23 22:30:00 +0100217 },
Akinobu Mita35f73962015-12-26 21:17:22 +0900218 IIO_ENUM("powerdown_mode", IIO_SEPARATE,
219 &mcp472x_powerdown_mode_enum[MCP4725]),
220 IIO_ENUM_AVAILABLE("powerdown_mode",
221 &mcp472x_powerdown_mode_enum[MCP4725]),
Peter Meerwald155f1b42013-06-23 22:30:00 +0100222 { },
223};
224
Akinobu Mita35f73962015-12-26 21:17:22 +0900225static const struct iio_chan_spec_ext_info mcp4726_ext_info[] = {
226 {
227 .name = "powerdown",
228 .read = mcp4725_read_powerdown,
229 .write = mcp4725_write_powerdown,
230 .shared = IIO_SEPARATE,
231 },
232 IIO_ENUM("powerdown_mode", IIO_SEPARATE,
233 &mcp472x_powerdown_mode_enum[MCP4726]),
234 IIO_ENUM_AVAILABLE("powerdown_mode",
235 &mcp472x_powerdown_mode_enum[MCP4726]),
236 { },
237};
238
239static const struct iio_chan_spec mcp472x_channel[] = {
240 [MCP4725] = {
241 .type = IIO_VOLTAGE,
242 .indexed = 1,
243 .output = 1,
244 .channel = 0,
245 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
246 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
247 .ext_info = mcp4725_ext_info,
248 },
249 [MCP4726] = {
250 .type = IIO_VOLTAGE,
251 .indexed = 1,
252 .output = 1,
253 .channel = 0,
254 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
255 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
256 .ext_info = mcp4726_ext_info,
257 },
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200258};
259
260static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
261{
262 struct mcp4725_data *data = iio_priv(indio_dev);
263 u8 outbuf[2];
264 int ret;
265
266 if (val >= (1 << 12) || val < 0)
267 return -EINVAL;
268
269 outbuf[0] = (val >> 8) & 0xf;
270 outbuf[1] = val & 0xff;
271
272 ret = i2c_master_send(data->client, outbuf, 2);
273 if (ret < 0)
274 return ret;
275 else if (ret != 2)
276 return -EIO;
277 else
278 return 0;
279}
280
281static int mcp4725_read_raw(struct iio_dev *indio_dev,
282 struct iio_chan_spec const *chan,
283 int *val, int *val2, long mask)
284{
285 struct mcp4725_data *data = iio_priv(indio_dev);
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200286
287 switch (mask) {
288 case IIO_CHAN_INFO_RAW:
289 *val = data->dac_value;
290 return IIO_VAL_INT;
291 case IIO_CHAN_INFO_SCALE:
Lars-Peter Clausenb117f962013-09-28 10:31:00 +0100292 *val = data->vref_mv;
293 *val2 = 12;
294 return IIO_VAL_FRACTIONAL_LOG2;
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200295 }
296 return -EINVAL;
297}
298
299static int mcp4725_write_raw(struct iio_dev *indio_dev,
300 struct iio_chan_spec const *chan,
301 int val, int val2, long mask)
302{
303 struct mcp4725_data *data = iio_priv(indio_dev);
304 int ret;
305
306 switch (mask) {
307 case IIO_CHAN_INFO_RAW:
308 ret = mcp4725_set_value(indio_dev, val);
309 data->dac_value = val;
310 break;
311 default:
312 ret = -EINVAL;
313 break;
314 }
315
316 return ret;
317}
318
319static const struct iio_info mcp4725_info = {
320 .read_raw = mcp4725_read_raw,
321 .write_raw = mcp4725_write_raw,
Peter Meerwaldf17b77d2013-06-23 22:30:00 +0100322 .attrs = &mcp4725_attribute_group,
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200323 .driver_module = THIS_MODULE,
324};
325
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800326static int mcp4725_probe(struct i2c_client *client,
327 const struct i2c_device_id *id)
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200328{
329 struct mcp4725_data *data;
330 struct iio_dev *indio_dev;
331 struct mcp4725_platform_data *platform_data = client->dev.platform_data;
332 u8 inbuf[3];
Peter Meerwald155f1b42013-06-23 22:30:00 +0100333 u8 pd;
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200334 int err;
335
336 if (!platform_data || !platform_data->vref_mv) {
337 dev_err(&client->dev, "invalid platform data");
Peter Meerwaldb6ff86f2013-07-29 23:18:00 +0100338 return -EINVAL;
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200339 }
340
Peter Meerwaldb6ff86f2013-07-29 23:18:00 +0100341 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
342 if (indio_dev == NULL)
343 return -ENOMEM;
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200344 data = iio_priv(indio_dev);
345 i2c_set_clientdata(client, indio_dev);
346 data->client = client;
347
348 indio_dev->dev.parent = &client->dev;
Yong Li97a249e2016-01-06 09:09:43 +0800349 indio_dev->name = id->name;
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200350 indio_dev->info = &mcp4725_info;
Akinobu Mita35f73962015-12-26 21:17:22 +0900351 indio_dev->channels = &mcp472x_channel[id->driver_data];
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200352 indio_dev->num_channels = 1;
353 indio_dev->modes = INDIO_DIRECT_MODE;
354
355 data->vref_mv = platform_data->vref_mv;
356
357 /* read current DAC value */
358 err = i2c_master_recv(client, inbuf, 3);
359 if (err < 0) {
360 dev_err(&client->dev, "failed to read DAC value");
Peter Meerwaldb6ff86f2013-07-29 23:18:00 +0100361 return err;
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200362 }
Peter Meerwald155f1b42013-06-23 22:30:00 +0100363 pd = (inbuf[0] >> 1) & 0x3;
364 data->powerdown = pd > 0 ? true : false;
Akinobu Mita35f73962015-12-26 21:17:22 +0900365 data->powerdown_mode = pd ? pd - 1 : 2; /* largest register to gnd */
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200366 data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
367
Sachin Kamata9b68512013-10-24 12:53:00 +0100368 return iio_device_register(indio_dev);
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200369}
370
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800371static int mcp4725_remove(struct i2c_client *client)
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200372{
Peter Meerwaldb6ff86f2013-07-29 23:18:00 +0100373 iio_device_unregister(i2c_get_clientdata(client));
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200374 return 0;
375}
376
377static const struct i2c_device_id mcp4725_id[] = {
Akinobu Mita35f73962015-12-26 21:17:22 +0900378 { "mcp4725", MCP4725 },
379 { "mcp4726", MCP4726 },
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200380 { }
381};
382MODULE_DEVICE_TABLE(i2c, mcp4725_id);
383
384static struct i2c_driver mcp4725_driver = {
385 .driver = {
386 .name = MCP4725_DRV_NAME,
387 .pm = MCP4725_PM_OPS,
388 },
389 .probe = mcp4725_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800390 .remove = mcp4725_remove,
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200391 .id_table = mcp4725_id,
392};
393module_i2c_driver(mcp4725_driver);
394
395MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
Akinobu Mita35f73962015-12-26 21:17:22 +0900396MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
Peter Meerwaldcf35ad62012-06-08 18:06:45 +0200397MODULE_LICENSE("GPL");