x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / drivers / iio / dac / mcp4725.c
blobfe3e42defb33e881fad4bc430bcf230984d874b1
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_device.h>
23 #include <linux/of.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
28 #include <linux/iio/dac/mcp4725.h>
30 #define MCP4725_DRV_NAME "mcp4725"
32 #define MCP472X_REF_VDD 0x00
33 #define MCP472X_REF_VREF_UNBUFFERED 0x02
34 #define MCP472X_REF_VREF_BUFFERED 0x03
36 struct mcp4725_data {
37 struct i2c_client *client;
38 int id;
39 unsigned ref_mode;
40 bool vref_buffered;
41 u16 dac_value;
42 bool powerdown;
43 unsigned powerdown_mode;
44 struct regulator *vdd_reg;
45 struct regulator *vref_reg;
48 static int mcp4725_suspend(struct device *dev)
50 struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
51 to_i2c_client(dev)));
52 u8 outbuf[2];
54 outbuf[0] = (data->powerdown_mode + 1) << 4;
55 outbuf[1] = 0;
56 data->powerdown = true;
58 return i2c_master_send(data->client, outbuf, 2);
61 static int mcp4725_resume(struct device *dev)
63 struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
64 to_i2c_client(dev)));
65 u8 outbuf[2];
67 /* restore previous DAC value */
68 outbuf[0] = (data->dac_value >> 8) & 0xf;
69 outbuf[1] = data->dac_value & 0xff;
70 data->powerdown = false;
72 return i2c_master_send(data->client, outbuf, 2);
75 #ifdef CONFIG_PM_SLEEP
76 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume);
77 #define MCP4725_PM_OPS (&mcp4725_pm_ops)
78 #else
79 #define MCP4725_PM_OPS NULL
80 #endif
82 static ssize_t mcp4725_store_eeprom(struct device *dev,
83 struct device_attribute *attr, const char *buf, size_t len)
85 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
86 struct mcp4725_data *data = iio_priv(indio_dev);
87 int tries = 20;
88 u8 inoutbuf[3];
89 bool state;
90 int ret;
92 ret = strtobool(buf, &state);
93 if (ret < 0)
94 return ret;
96 if (!state)
97 return 0;
99 inoutbuf[0] = 0x60; /* write EEPROM */
100 inoutbuf[0] |= data->ref_mode << 3;
101 inoutbuf[0] |= data->powerdown ? ((data->powerdown_mode + 1) << 1) : 0;
102 inoutbuf[1] = data->dac_value >> 4;
103 inoutbuf[2] = (data->dac_value & 0xf) << 4;
105 ret = i2c_master_send(data->client, inoutbuf, 3);
106 if (ret < 0)
107 return ret;
108 else if (ret != 3)
109 return -EIO;
111 /* wait for write complete, takes up to 50ms */
112 while (tries--) {
113 msleep(20);
114 ret = i2c_master_recv(data->client, inoutbuf, 3);
115 if (ret < 0)
116 return ret;
117 else if (ret != 3)
118 return -EIO;
120 if (inoutbuf[0] & 0x80)
121 break;
124 if (tries < 0) {
125 dev_err(&data->client->dev,
126 "mcp4725_store_eeprom() failed, incomplete\n");
127 return -EIO;
130 return len;
133 static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
135 static struct attribute *mcp4725_attributes[] = {
136 &iio_dev_attr_store_eeprom.dev_attr.attr,
137 NULL,
140 static const struct attribute_group mcp4725_attribute_group = {
141 .attrs = mcp4725_attributes,
144 static const char * const mcp4725_powerdown_modes[] = {
145 "1kohm_to_gnd",
146 "100kohm_to_gnd",
147 "500kohm_to_gnd"
150 static const char * const mcp4726_powerdown_modes[] = {
151 "1kohm_to_gnd",
152 "125kohm_to_gnd",
153 "640kohm_to_gnd"
156 static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev,
157 const struct iio_chan_spec *chan)
159 struct mcp4725_data *data = iio_priv(indio_dev);
161 return data->powerdown_mode;
164 static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev,
165 const struct iio_chan_spec *chan, unsigned mode)
167 struct mcp4725_data *data = iio_priv(indio_dev);
169 data->powerdown_mode = mode;
171 return 0;
174 static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev,
175 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
177 struct mcp4725_data *data = iio_priv(indio_dev);
179 return sprintf(buf, "%d\n", data->powerdown);
182 static ssize_t mcp4725_write_powerdown(struct iio_dev *indio_dev,
183 uintptr_t private, const struct iio_chan_spec *chan,
184 const char *buf, size_t len)
186 struct mcp4725_data *data = iio_priv(indio_dev);
187 bool state;
188 int ret;
190 ret = strtobool(buf, &state);
191 if (ret)
192 return ret;
194 if (state)
195 ret = mcp4725_suspend(&data->client->dev);
196 else
197 ret = mcp4725_resume(&data->client->dev);
198 if (ret < 0)
199 return ret;
201 return len;
204 enum chip_id {
205 MCP4725,
206 MCP4726,
209 static const struct iio_enum mcp472x_powerdown_mode_enum[] = {
210 [MCP4725] = {
211 .items = mcp4725_powerdown_modes,
212 .num_items = ARRAY_SIZE(mcp4725_powerdown_modes),
213 .get = mcp4725_get_powerdown_mode,
214 .set = mcp4725_set_powerdown_mode,
216 [MCP4726] = {
217 .items = mcp4726_powerdown_modes,
218 .num_items = ARRAY_SIZE(mcp4726_powerdown_modes),
219 .get = mcp4725_get_powerdown_mode,
220 .set = mcp4725_set_powerdown_mode,
224 static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = {
226 .name = "powerdown",
227 .read = mcp4725_read_powerdown,
228 .write = mcp4725_write_powerdown,
229 .shared = IIO_SEPARATE,
231 IIO_ENUM("powerdown_mode", IIO_SEPARATE,
232 &mcp472x_powerdown_mode_enum[MCP4725]),
233 IIO_ENUM_AVAILABLE("powerdown_mode",
234 &mcp472x_powerdown_mode_enum[MCP4725]),
235 { },
238 static const struct iio_chan_spec_ext_info mcp4726_ext_info[] = {
240 .name = "powerdown",
241 .read = mcp4725_read_powerdown,
242 .write = mcp4725_write_powerdown,
243 .shared = IIO_SEPARATE,
245 IIO_ENUM("powerdown_mode", IIO_SEPARATE,
246 &mcp472x_powerdown_mode_enum[MCP4726]),
247 IIO_ENUM_AVAILABLE("powerdown_mode",
248 &mcp472x_powerdown_mode_enum[MCP4726]),
249 { },
252 static const struct iio_chan_spec mcp472x_channel[] = {
253 [MCP4725] = {
254 .type = IIO_VOLTAGE,
255 .indexed = 1,
256 .output = 1,
257 .channel = 0,
258 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
259 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
260 .ext_info = mcp4725_ext_info,
262 [MCP4726] = {
263 .type = IIO_VOLTAGE,
264 .indexed = 1,
265 .output = 1,
266 .channel = 0,
267 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
268 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
269 .ext_info = mcp4726_ext_info,
273 static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
275 struct mcp4725_data *data = iio_priv(indio_dev);
276 u8 outbuf[2];
277 int ret;
279 if (val >= (1 << 12) || val < 0)
280 return -EINVAL;
282 outbuf[0] = (val >> 8) & 0xf;
283 outbuf[1] = val & 0xff;
285 ret = i2c_master_send(data->client, outbuf, 2);
286 if (ret < 0)
287 return ret;
288 else if (ret != 2)
289 return -EIO;
290 else
291 return 0;
294 static int mcp4726_set_cfg(struct iio_dev *indio_dev)
296 struct mcp4725_data *data = iio_priv(indio_dev);
297 u8 outbuf[3];
298 int ret;
300 outbuf[0] = 0x40;
301 outbuf[0] |= data->ref_mode << 3;
302 if (data->powerdown)
303 outbuf[0] |= data->powerdown << 1;
304 outbuf[1] = data->dac_value >> 4;
305 outbuf[2] = (data->dac_value & 0xf) << 4;
307 ret = i2c_master_send(data->client, outbuf, 3);
308 if (ret < 0)
309 return ret;
310 else if (ret != 3)
311 return -EIO;
312 else
313 return 0;
316 static int mcp4725_read_raw(struct iio_dev *indio_dev,
317 struct iio_chan_spec const *chan,
318 int *val, int *val2, long mask)
320 struct mcp4725_data *data = iio_priv(indio_dev);
321 int ret;
323 switch (mask) {
324 case IIO_CHAN_INFO_RAW:
325 *val = data->dac_value;
326 return IIO_VAL_INT;
327 case IIO_CHAN_INFO_SCALE:
328 if (data->ref_mode == MCP472X_REF_VDD)
329 ret = regulator_get_voltage(data->vdd_reg);
330 else
331 ret = regulator_get_voltage(data->vref_reg);
333 if (ret < 0)
334 return ret;
336 *val = ret / 1000;
337 *val2 = 12;
338 return IIO_VAL_FRACTIONAL_LOG2;
340 return -EINVAL;
343 static int mcp4725_write_raw(struct iio_dev *indio_dev,
344 struct iio_chan_spec const *chan,
345 int val, int val2, long mask)
347 struct mcp4725_data *data = iio_priv(indio_dev);
348 int ret;
350 switch (mask) {
351 case IIO_CHAN_INFO_RAW:
352 ret = mcp4725_set_value(indio_dev, val);
353 data->dac_value = val;
354 break;
355 default:
356 ret = -EINVAL;
357 break;
360 return ret;
363 static const struct iio_info mcp4725_info = {
364 .read_raw = mcp4725_read_raw,
365 .write_raw = mcp4725_write_raw,
366 .attrs = &mcp4725_attribute_group,
367 .driver_module = THIS_MODULE,
370 #ifdef CONFIG_OF
371 static int mcp4725_probe_dt(struct device *dev,
372 struct mcp4725_platform_data *pdata)
374 struct device_node *np = dev->of_node;
376 if (!np)
377 return -ENODEV;
379 /* check if is the vref-supply defined */
380 pdata->use_vref = of_property_read_bool(np, "vref-supply");
381 pdata->vref_buffered =
382 of_property_read_bool(np, "microchip,vref-buffered");
384 return 0;
386 #else
387 static int mcp4725_probe_dt(struct device *dev,
388 struct mcp4725_platform_data *platform_data)
390 return -ENODEV;
392 #endif
394 static int mcp4725_probe(struct i2c_client *client,
395 const struct i2c_device_id *id)
397 struct mcp4725_data *data;
398 struct iio_dev *indio_dev;
399 struct mcp4725_platform_data *pdata, pdata_dt;
400 u8 inbuf[4];
401 u8 pd;
402 u8 ref;
403 int err;
405 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
406 if (indio_dev == NULL)
407 return -ENOMEM;
408 data = iio_priv(indio_dev);
409 i2c_set_clientdata(client, indio_dev);
410 data->client = client;
411 if (client->dev.of_node)
412 data->id = (enum chip_id)of_device_get_match_data(&client->dev);
413 else
414 data->id = id->driver_data;
415 pdata = dev_get_platdata(&client->dev);
417 if (!pdata) {
418 err = mcp4725_probe_dt(&client->dev, &pdata_dt);
419 if (err) {
420 dev_err(&client->dev,
421 "invalid platform or devicetree data");
422 return err;
424 pdata = &pdata_dt;
427 if (data->id == MCP4725 && pdata->use_vref) {
428 dev_err(&client->dev,
429 "external reference is unavailable on MCP4725");
430 return -EINVAL;
433 if (!pdata->use_vref && pdata->vref_buffered) {
434 dev_err(&client->dev,
435 "buffering is unavailable on the internal reference");
436 return -EINVAL;
439 if (!pdata->use_vref)
440 data->ref_mode = MCP472X_REF_VDD;
441 else
442 data->ref_mode = pdata->vref_buffered ?
443 MCP472X_REF_VREF_BUFFERED :
444 MCP472X_REF_VREF_UNBUFFERED;
446 data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
447 if (IS_ERR(data->vdd_reg))
448 return PTR_ERR(data->vdd_reg);
450 err = regulator_enable(data->vdd_reg);
451 if (err)
452 return err;
454 if (pdata->use_vref) {
455 data->vref_reg = devm_regulator_get(&client->dev, "vref");
456 if (IS_ERR(data->vref_reg)) {
457 err = PTR_ERR(data->vref_reg);
458 goto err_disable_vdd_reg;
461 err = regulator_enable(data->vref_reg);
462 if (err)
463 goto err_disable_vdd_reg;
466 indio_dev->dev.parent = &client->dev;
467 indio_dev->name = id->name;
468 indio_dev->info = &mcp4725_info;
469 indio_dev->channels = &mcp472x_channel[id->driver_data];
470 indio_dev->num_channels = 1;
471 indio_dev->modes = INDIO_DIRECT_MODE;
473 /* read current DAC value and settings */
474 err = i2c_master_recv(client, inbuf, data->id == MCP4725 ? 3 : 4);
476 if (err < 0) {
477 dev_err(&client->dev, "failed to read DAC value");
478 goto err_disable_vref_reg;
480 pd = (inbuf[0] >> 1) & 0x3;
481 data->powerdown = pd > 0 ? true : false;
482 data->powerdown_mode = pd ? pd - 1 : 2; /* largest resistor to gnd */
483 data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
484 if (data->id == MCP4726)
485 ref = (inbuf[3] >> 3) & 0x3;
487 if (data->id == MCP4726 && ref != data->ref_mode) {
488 dev_info(&client->dev,
489 "voltage reference mode differs (conf: %u, eeprom: %u), setting %u",
490 data->ref_mode, ref, data->ref_mode);
491 err = mcp4726_set_cfg(indio_dev);
492 if (err < 0)
493 goto err_disable_vref_reg;
496 err = iio_device_register(indio_dev);
497 if (err)
498 goto err_disable_vref_reg;
500 return 0;
502 err_disable_vref_reg:
503 if (data->vref_reg)
504 regulator_disable(data->vref_reg);
506 err_disable_vdd_reg:
507 regulator_disable(data->vdd_reg);
509 return err;
512 static int mcp4725_remove(struct i2c_client *client)
514 struct iio_dev *indio_dev = i2c_get_clientdata(client);
515 struct mcp4725_data *data = iio_priv(indio_dev);
517 iio_device_unregister(indio_dev);
519 if (data->vref_reg)
520 regulator_disable(data->vref_reg);
521 regulator_disable(data->vdd_reg);
523 return 0;
526 static const struct i2c_device_id mcp4725_id[] = {
527 { "mcp4725", MCP4725 },
528 { "mcp4726", MCP4726 },
531 MODULE_DEVICE_TABLE(i2c, mcp4725_id);
533 #ifdef CONFIG_OF
534 static const struct of_device_id mcp4725_of_match[] = {
536 .compatible = "microchip,mcp4725",
537 .data = (void *)MCP4725
540 .compatible = "microchip,mcp4726",
541 .data = (void *)MCP4726
545 MODULE_DEVICE_TABLE(of, mcp4725_of_match);
546 #endif
548 static struct i2c_driver mcp4725_driver = {
549 .driver = {
550 .name = MCP4725_DRV_NAME,
551 .of_match_table = of_match_ptr(mcp4725_of_match),
552 .pm = MCP4725_PM_OPS,
554 .probe = mcp4725_probe,
555 .remove = mcp4725_remove,
556 .id_table = mcp4725_id,
558 module_i2c_driver(mcp4725_driver);
560 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
561 MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
562 MODULE_LICENSE("GPL");