sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / iio / dac / mcp4725.c
blobdb109f0cdd8c7e733cfe380157ba214c55c301b2
1 /*
2 * mcp4725.c - Support for Microchip MCP4725/6
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
14 * hardware)
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/err.h>
20 #include <linux/delay.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/of.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
27 #include <linux/iio/dac/mcp4725.h>
29 #define MCP4725_DRV_NAME "mcp4725"
31 #define MCP472X_REF_VDD 0x00
32 #define MCP472X_REF_VREF_UNBUFFERED 0x02
33 #define MCP472X_REF_VREF_BUFFERED 0x03
35 struct mcp4725_data {
36 struct i2c_client *client;
37 int id;
38 unsigned ref_mode;
39 bool vref_buffered;
40 u16 dac_value;
41 bool powerdown;
42 unsigned powerdown_mode;
43 struct regulator *vdd_reg;
44 struct regulator *vref_reg;
47 static int mcp4725_suspend(struct device *dev)
49 struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
50 to_i2c_client(dev)));
51 u8 outbuf[2];
53 outbuf[0] = (data->powerdown_mode + 1) << 4;
54 outbuf[1] = 0;
55 data->powerdown = true;
57 return i2c_master_send(data->client, outbuf, 2);
60 static int mcp4725_resume(struct device *dev)
62 struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
63 to_i2c_client(dev)));
64 u8 outbuf[2];
66 /* restore previous DAC value */
67 outbuf[0] = (data->dac_value >> 8) & 0xf;
68 outbuf[1] = data->dac_value & 0xff;
69 data->powerdown = false;
71 return i2c_master_send(data->client, outbuf, 2);
74 #ifdef CONFIG_PM_SLEEP
75 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume);
76 #define MCP4725_PM_OPS (&mcp4725_pm_ops)
77 #else
78 #define MCP4725_PM_OPS NULL
79 #endif
81 static ssize_t mcp4725_store_eeprom(struct device *dev,
82 struct device_attribute *attr, const char *buf, size_t len)
84 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
85 struct mcp4725_data *data = iio_priv(indio_dev);
86 int tries = 20;
87 u8 inoutbuf[3];
88 bool state;
89 int ret;
91 ret = strtobool(buf, &state);
92 if (ret < 0)
93 return ret;
95 if (!state)
96 return 0;
98 inoutbuf[0] = 0x60; /* write EEPROM */
99 inoutbuf[0] |= data->ref_mode << 3;
100 inoutbuf[1] = data->dac_value >> 4;
101 inoutbuf[2] = (data->dac_value & 0xf) << 4;
103 ret = i2c_master_send(data->client, inoutbuf, 3);
104 if (ret < 0)
105 return ret;
106 else if (ret != 3)
107 return -EIO;
109 /* wait for write complete, takes up to 50ms */
110 while (tries--) {
111 msleep(20);
112 ret = i2c_master_recv(data->client, inoutbuf, 3);
113 if (ret < 0)
114 return ret;
115 else if (ret != 3)
116 return -EIO;
118 if (inoutbuf[0] & 0x80)
119 break;
122 if (tries < 0) {
123 dev_err(&data->client->dev,
124 "mcp4725_store_eeprom() failed, incomplete\n");
125 return -EIO;
128 return len;
131 static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
133 static struct attribute *mcp4725_attributes[] = {
134 &iio_dev_attr_store_eeprom.dev_attr.attr,
135 NULL,
138 static const struct attribute_group mcp4725_attribute_group = {
139 .attrs = mcp4725_attributes,
142 static const char * const mcp4725_powerdown_modes[] = {
143 "1kohm_to_gnd",
144 "100kohm_to_gnd",
145 "500kohm_to_gnd"
148 static const char * const mcp4726_powerdown_modes[] = {
149 "1kohm_to_gnd",
150 "125kohm_to_gnd",
151 "640kohm_to_gnd"
154 static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev,
155 const struct iio_chan_spec *chan)
157 struct mcp4725_data *data = iio_priv(indio_dev);
159 return data->powerdown_mode;
162 static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev,
163 const struct iio_chan_spec *chan, unsigned mode)
165 struct mcp4725_data *data = iio_priv(indio_dev);
167 data->powerdown_mode = mode;
169 return 0;
172 static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev,
173 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
175 struct mcp4725_data *data = iio_priv(indio_dev);
177 return sprintf(buf, "%d\n", data->powerdown);
180 static ssize_t mcp4725_write_powerdown(struct iio_dev *indio_dev,
181 uintptr_t private, const struct iio_chan_spec *chan,
182 const char *buf, size_t len)
184 struct mcp4725_data *data = iio_priv(indio_dev);
185 bool state;
186 int ret;
188 ret = strtobool(buf, &state);
189 if (ret)
190 return ret;
192 if (state)
193 ret = mcp4725_suspend(&data->client->dev);
194 else
195 ret = mcp4725_resume(&data->client->dev);
196 if (ret < 0)
197 return ret;
199 return len;
202 enum {
203 MCP4725,
204 MCP4726,
207 static const struct iio_enum mcp472x_powerdown_mode_enum[] = {
208 [MCP4725] = {
209 .items = mcp4725_powerdown_modes,
210 .num_items = ARRAY_SIZE(mcp4725_powerdown_modes),
211 .get = mcp4725_get_powerdown_mode,
212 .set = mcp4725_set_powerdown_mode,
214 [MCP4726] = {
215 .items = mcp4726_powerdown_modes,
216 .num_items = ARRAY_SIZE(mcp4726_powerdown_modes),
217 .get = mcp4725_get_powerdown_mode,
218 .set = mcp4725_set_powerdown_mode,
222 static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = {
224 .name = "powerdown",
225 .read = mcp4725_read_powerdown,
226 .write = mcp4725_write_powerdown,
227 .shared = IIO_SEPARATE,
229 IIO_ENUM("powerdown_mode", IIO_SEPARATE,
230 &mcp472x_powerdown_mode_enum[MCP4725]),
231 IIO_ENUM_AVAILABLE("powerdown_mode",
232 &mcp472x_powerdown_mode_enum[MCP4725]),
233 { },
236 static const struct iio_chan_spec_ext_info mcp4726_ext_info[] = {
238 .name = "powerdown",
239 .read = mcp4725_read_powerdown,
240 .write = mcp4725_write_powerdown,
241 .shared = IIO_SEPARATE,
243 IIO_ENUM("powerdown_mode", IIO_SEPARATE,
244 &mcp472x_powerdown_mode_enum[MCP4726]),
245 IIO_ENUM_AVAILABLE("powerdown_mode",
246 &mcp472x_powerdown_mode_enum[MCP4726]),
247 { },
250 static const struct iio_chan_spec mcp472x_channel[] = {
251 [MCP4725] = {
252 .type = IIO_VOLTAGE,
253 .indexed = 1,
254 .output = 1,
255 .channel = 0,
256 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
257 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
258 .ext_info = mcp4725_ext_info,
260 [MCP4726] = {
261 .type = IIO_VOLTAGE,
262 .indexed = 1,
263 .output = 1,
264 .channel = 0,
265 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
266 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
267 .ext_info = mcp4726_ext_info,
271 static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
273 struct mcp4725_data *data = iio_priv(indio_dev);
274 u8 outbuf[2];
275 int ret;
277 if (val >= (1 << 12) || val < 0)
278 return -EINVAL;
280 outbuf[0] = (val >> 8) & 0xf;
281 outbuf[1] = val & 0xff;
283 ret = i2c_master_send(data->client, outbuf, 2);
284 if (ret < 0)
285 return ret;
286 else if (ret != 2)
287 return -EIO;
288 else
289 return 0;
292 static int mcp4726_set_cfg(struct iio_dev *indio_dev)
294 struct mcp4725_data *data = iio_priv(indio_dev);
295 u8 outbuf[3];
296 int ret;
298 outbuf[0] = 0x40;
299 outbuf[0] |= data->ref_mode << 3;
300 if (data->powerdown)
301 outbuf[0] |= data->powerdown << 1;
302 outbuf[1] = data->dac_value >> 4;
303 outbuf[2] = (data->dac_value & 0xf) << 4;
305 ret = i2c_master_send(data->client, outbuf, 3);
306 if (ret < 0)
307 return ret;
308 else if (ret != 3)
309 return -EIO;
310 else
311 return 0;
314 static int mcp4725_read_raw(struct iio_dev *indio_dev,
315 struct iio_chan_spec const *chan,
316 int *val, int *val2, long mask)
318 struct mcp4725_data *data = iio_priv(indio_dev);
319 int ret;
321 switch (mask) {
322 case IIO_CHAN_INFO_RAW:
323 *val = data->dac_value;
324 return IIO_VAL_INT;
325 case IIO_CHAN_INFO_SCALE:
326 if (data->ref_mode == MCP472X_REF_VDD)
327 ret = regulator_get_voltage(data->vdd_reg);
328 else
329 ret = regulator_get_voltage(data->vref_reg);
331 if (ret < 0)
332 return ret;
334 *val = ret / 1000;
335 *val2 = 12;
336 return IIO_VAL_FRACTIONAL_LOG2;
338 return -EINVAL;
341 static int mcp4725_write_raw(struct iio_dev *indio_dev,
342 struct iio_chan_spec const *chan,
343 int val, int val2, long mask)
345 struct mcp4725_data *data = iio_priv(indio_dev);
346 int ret;
348 switch (mask) {
349 case IIO_CHAN_INFO_RAW:
350 ret = mcp4725_set_value(indio_dev, val);
351 data->dac_value = val;
352 break;
353 default:
354 ret = -EINVAL;
355 break;
358 return ret;
361 static const struct iio_info mcp4725_info = {
362 .read_raw = mcp4725_read_raw,
363 .write_raw = mcp4725_write_raw,
364 .attrs = &mcp4725_attribute_group,
365 .driver_module = THIS_MODULE,
368 #ifdef CONFIG_OF
369 static int mcp4725_probe_dt(struct device *dev,
370 struct mcp4725_platform_data *pdata)
372 struct device_node *np = dev->of_node;
374 if (!np)
375 return -ENODEV;
377 /* check if is the vref-supply defined */
378 pdata->use_vref = of_property_read_bool(np, "vref-supply");
379 pdata->vref_buffered =
380 of_property_read_bool(np, "microchip,vref-buffered");
382 return 0;
384 #else
385 static int mcp4725_probe_dt(struct device *dev,
386 struct mcp4725_platform_data *platform_data)
388 return -ENODEV;
390 #endif
392 static int mcp4725_probe(struct i2c_client *client,
393 const struct i2c_device_id *id)
395 struct mcp4725_data *data;
396 struct iio_dev *indio_dev;
397 struct mcp4725_platform_data *pdata, pdata_dt;
398 u8 inbuf[4];
399 u8 pd;
400 u8 ref;
401 int err;
403 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
404 if (indio_dev == NULL)
405 return -ENOMEM;
406 data = iio_priv(indio_dev);
407 i2c_set_clientdata(client, indio_dev);
408 data->client = client;
409 data->id = id->driver_data;
410 pdata = dev_get_platdata(&client->dev);
412 if (!pdata) {
413 err = mcp4725_probe_dt(&client->dev, &pdata_dt);
414 if (err) {
415 dev_err(&client->dev,
416 "invalid platform or devicetree data");
417 return err;
419 pdata = &pdata_dt;
422 if (data->id == MCP4725 && pdata->use_vref) {
423 dev_err(&client->dev,
424 "external reference is unavailable on MCP4725");
425 return -EINVAL;
428 if (!pdata->use_vref && pdata->vref_buffered) {
429 dev_err(&client->dev,
430 "buffering is unavailable on the internal reference");
431 return -EINVAL;
434 if (!pdata->use_vref)
435 data->ref_mode = MCP472X_REF_VDD;
436 else
437 data->ref_mode = pdata->vref_buffered ?
438 MCP472X_REF_VREF_BUFFERED :
439 MCP472X_REF_VREF_UNBUFFERED;
441 data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
442 if (IS_ERR(data->vdd_reg))
443 return PTR_ERR(data->vdd_reg);
445 err = regulator_enable(data->vdd_reg);
446 if (err)
447 return err;
449 if (pdata->use_vref) {
450 data->vref_reg = devm_regulator_get(&client->dev, "vref");
451 if (IS_ERR(data->vref_reg)) {
452 err = PTR_ERR(data->vref_reg);
453 goto err_disable_vdd_reg;
456 err = regulator_enable(data->vref_reg);
457 if (err)
458 goto err_disable_vdd_reg;
461 indio_dev->dev.parent = &client->dev;
462 indio_dev->name = id->name;
463 indio_dev->info = &mcp4725_info;
464 indio_dev->channels = &mcp472x_channel[id->driver_data];
465 indio_dev->num_channels = 1;
466 indio_dev->modes = INDIO_DIRECT_MODE;
468 /* read current DAC value and settings */
469 err = i2c_master_recv(client, inbuf, data->id == MCP4725 ? 3 : 4);
471 if (err < 0) {
472 dev_err(&client->dev, "failed to read DAC value");
473 goto err_disable_vref_reg;
475 pd = (inbuf[0] >> 1) & 0x3;
476 data->powerdown = pd > 0 ? true : false;
477 data->powerdown_mode = pd ? pd - 1 : 2; /* largest resistor to gnd */
478 data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
479 if (data->id == MCP4726)
480 ref = (inbuf[3] >> 3) & 0x3;
482 if (data->id == MCP4726 && ref != data->ref_mode) {
483 dev_info(&client->dev,
484 "voltage reference mode differs (conf: %u, eeprom: %u), setting %u",
485 data->ref_mode, ref, data->ref_mode);
486 err = mcp4726_set_cfg(indio_dev);
487 if (err < 0)
488 goto err_disable_vref_reg;
491 err = iio_device_register(indio_dev);
492 if (err)
493 goto err_disable_vref_reg;
495 return 0;
497 err_disable_vref_reg:
498 if (data->vref_reg)
499 regulator_disable(data->vref_reg);
501 err_disable_vdd_reg:
502 regulator_disable(data->vdd_reg);
504 return err;
507 static int mcp4725_remove(struct i2c_client *client)
509 struct iio_dev *indio_dev = i2c_get_clientdata(client);
510 struct mcp4725_data *data = iio_priv(indio_dev);
512 iio_device_unregister(indio_dev);
514 if (data->vref_reg)
515 regulator_disable(data->vref_reg);
516 regulator_disable(data->vdd_reg);
518 return 0;
521 static const struct i2c_device_id mcp4725_id[] = {
522 { "mcp4725", MCP4725 },
523 { "mcp4726", MCP4726 },
526 MODULE_DEVICE_TABLE(i2c, mcp4725_id);
528 static struct i2c_driver mcp4725_driver = {
529 .driver = {
530 .name = MCP4725_DRV_NAME,
531 .pm = MCP4725_PM_OPS,
533 .probe = mcp4725_probe,
534 .remove = mcp4725_remove,
535 .id_table = mcp4725_id,
537 module_i2c_driver(mcp4725_driver);
539 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
540 MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
541 MODULE_LICENSE("GPL");