xref: /openbmc/linux/drivers/iio/dac/mcp4725.c (revision d2574c33)
1 /*
2  * mcp4725.c - Support for Microchip MCP4725/6
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/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>
24 
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 
28 #include <linux/iio/dac/mcp4725.h>
29 
30 #define MCP4725_DRV_NAME "mcp4725"
31 
32 #define MCP472X_REF_VDD			0x00
33 #define MCP472X_REF_VREF_UNBUFFERED	0x02
34 #define MCP472X_REF_VREF_BUFFERED	0x03
35 
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;
46 };
47 
48 static int __maybe_unused mcp4725_suspend(struct device *dev)
49 {
50 	struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
51 		to_i2c_client(dev)));
52 	u8 outbuf[2];
53 
54 	outbuf[0] = (data->powerdown_mode + 1) << 4;
55 	outbuf[1] = 0;
56 	data->powerdown = true;
57 
58 	return i2c_master_send(data->client, outbuf, 2);
59 }
60 
61 static int __maybe_unused mcp4725_resume(struct device *dev)
62 {
63 	struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
64 		to_i2c_client(dev)));
65 	u8 outbuf[2];
66 
67 	/* restore previous DAC value */
68 	outbuf[0] = (data->dac_value >> 8) & 0xf;
69 	outbuf[1] = data->dac_value & 0xff;
70 	data->powerdown = false;
71 
72 	return i2c_master_send(data->client, outbuf, 2);
73 }
74 static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume);
75 
76 static ssize_t mcp4725_store_eeprom(struct device *dev,
77 	struct device_attribute *attr, const char *buf, size_t len)
78 {
79 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
80 	struct mcp4725_data *data = iio_priv(indio_dev);
81 	int tries = 20;
82 	u8 inoutbuf[3];
83 	bool state;
84 	int ret;
85 
86 	ret = strtobool(buf, &state);
87 	if (ret < 0)
88 		return ret;
89 
90 	if (!state)
91 		return 0;
92 
93 	inoutbuf[0] = 0x60; /* write EEPROM */
94 	inoutbuf[0] |= data->ref_mode << 3;
95 	inoutbuf[1] = data->dac_value >> 4;
96 	inoutbuf[2] = (data->dac_value & 0xf) << 4;
97 
98 	ret = i2c_master_send(data->client, inoutbuf, 3);
99 	if (ret < 0)
100 		return ret;
101 	else if (ret != 3)
102 		return -EIO;
103 
104 	/* wait for write complete, takes up to 50ms */
105 	while (tries--) {
106 		msleep(20);
107 		ret = i2c_master_recv(data->client, inoutbuf, 3);
108 		if (ret < 0)
109 			return ret;
110 		else if (ret != 3)
111 			return -EIO;
112 
113 		if (inoutbuf[0] & 0x80)
114 			break;
115 	}
116 
117 	if (tries < 0) {
118 		dev_err(&data->client->dev,
119 			"mcp4725_store_eeprom() failed, incomplete\n");
120 		return -EIO;
121 	}
122 
123 	return len;
124 }
125 
126 static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
127 
128 static struct attribute *mcp4725_attributes[] = {
129 	&iio_dev_attr_store_eeprom.dev_attr.attr,
130 	NULL,
131 };
132 
133 static const struct attribute_group mcp4725_attribute_group = {
134 	.attrs = mcp4725_attributes,
135 };
136 
137 static const char * const mcp4725_powerdown_modes[] = {
138 	"1kohm_to_gnd",
139 	"100kohm_to_gnd",
140 	"500kohm_to_gnd"
141 };
142 
143 static const char * const mcp4726_powerdown_modes[] = {
144 	"1kohm_to_gnd",
145 	"125kohm_to_gnd",
146 	"640kohm_to_gnd"
147 };
148 
149 static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev,
150 	const struct iio_chan_spec *chan)
151 {
152 	struct mcp4725_data *data = iio_priv(indio_dev);
153 
154 	return data->powerdown_mode;
155 }
156 
157 static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev,
158 	const struct iio_chan_spec *chan, unsigned mode)
159 {
160 	struct mcp4725_data *data = iio_priv(indio_dev);
161 
162 	data->powerdown_mode = mode;
163 
164 	return 0;
165 }
166 
167 static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev,
168 	uintptr_t private, const struct iio_chan_spec *chan, char *buf)
169 {
170 	struct mcp4725_data *data = iio_priv(indio_dev);
171 
172 	return sprintf(buf, "%d\n", data->powerdown);
173 }
174 
175 static ssize_t mcp4725_write_powerdown(struct iio_dev *indio_dev,
176 	 uintptr_t private, const struct iio_chan_spec *chan,
177 	 const char *buf, size_t len)
178 {
179 	struct mcp4725_data *data = iio_priv(indio_dev);
180 	bool state;
181 	int ret;
182 
183 	ret = strtobool(buf, &state);
184 	if (ret)
185 		return ret;
186 
187 	if (state)
188 		ret = mcp4725_suspend(&data->client->dev);
189 	else
190 		ret = mcp4725_resume(&data->client->dev);
191 	if (ret < 0)
192 		return ret;
193 
194 	return len;
195 }
196 
197 enum chip_id {
198 	MCP4725,
199 	MCP4726,
200 };
201 
202 static const struct iio_enum mcp472x_powerdown_mode_enum[] = {
203 	[MCP4725] = {
204 		.items = mcp4725_powerdown_modes,
205 		.num_items = ARRAY_SIZE(mcp4725_powerdown_modes),
206 		.get = mcp4725_get_powerdown_mode,
207 		.set = mcp4725_set_powerdown_mode,
208 	},
209 	[MCP4726] = {
210 		.items = mcp4726_powerdown_modes,
211 		.num_items = ARRAY_SIZE(mcp4726_powerdown_modes),
212 		.get = mcp4725_get_powerdown_mode,
213 		.set = mcp4725_set_powerdown_mode,
214 	},
215 };
216 
217 static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = {
218 	{
219 		.name = "powerdown",
220 		.read = mcp4725_read_powerdown,
221 		.write = mcp4725_write_powerdown,
222 		.shared = IIO_SEPARATE,
223 	},
224 	IIO_ENUM("powerdown_mode", IIO_SEPARATE,
225 			&mcp472x_powerdown_mode_enum[MCP4725]),
226 	IIO_ENUM_AVAILABLE("powerdown_mode",
227 			&mcp472x_powerdown_mode_enum[MCP4725]),
228 	{ },
229 };
230 
231 static const struct iio_chan_spec_ext_info mcp4726_ext_info[] = {
232 	{
233 		.name = "powerdown",
234 		.read = mcp4725_read_powerdown,
235 		.write = mcp4725_write_powerdown,
236 		.shared = IIO_SEPARATE,
237 	},
238 	IIO_ENUM("powerdown_mode", IIO_SEPARATE,
239 			&mcp472x_powerdown_mode_enum[MCP4726]),
240 	IIO_ENUM_AVAILABLE("powerdown_mode",
241 			&mcp472x_powerdown_mode_enum[MCP4726]),
242 	{ },
243 };
244 
245 static const struct iio_chan_spec mcp472x_channel[] = {
246 	[MCP4725] = {
247 		.type		= IIO_VOLTAGE,
248 		.indexed	= 1,
249 		.output		= 1,
250 		.channel	= 0,
251 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
252 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
253 		.ext_info	= mcp4725_ext_info,
254 	},
255 	[MCP4726] = {
256 		.type		= IIO_VOLTAGE,
257 		.indexed	= 1,
258 		.output		= 1,
259 		.channel	= 0,
260 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
261 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
262 		.ext_info	= mcp4726_ext_info,
263 	},
264 };
265 
266 static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
267 {
268 	struct mcp4725_data *data = iio_priv(indio_dev);
269 	u8 outbuf[2];
270 	int ret;
271 
272 	if (val >= (1 << 12) || val < 0)
273 		return -EINVAL;
274 
275 	outbuf[0] = (val >> 8) & 0xf;
276 	outbuf[1] = val & 0xff;
277 
278 	ret = i2c_master_send(data->client, outbuf, 2);
279 	if (ret < 0)
280 		return ret;
281 	else if (ret != 2)
282 		return -EIO;
283 	else
284 		return 0;
285 }
286 
287 static int mcp4726_set_cfg(struct iio_dev *indio_dev)
288 {
289 	struct mcp4725_data *data = iio_priv(indio_dev);
290 	u8 outbuf[3];
291 	int ret;
292 
293 	outbuf[0] = 0x40;
294 	outbuf[0] |= data->ref_mode << 3;
295 	if (data->powerdown)
296 		outbuf[0] |= data->powerdown << 1;
297 	outbuf[1] = data->dac_value >> 4;
298 	outbuf[2] = (data->dac_value & 0xf) << 4;
299 
300 	ret = i2c_master_send(data->client, outbuf, 3);
301 	if (ret < 0)
302 		return ret;
303 	else if (ret != 3)
304 		return -EIO;
305 	else
306 		return 0;
307 }
308 
309 static int mcp4725_read_raw(struct iio_dev *indio_dev,
310 			   struct iio_chan_spec const *chan,
311 			   int *val, int *val2, long mask)
312 {
313 	struct mcp4725_data *data = iio_priv(indio_dev);
314 	int ret;
315 
316 	switch (mask) {
317 	case IIO_CHAN_INFO_RAW:
318 		*val = data->dac_value;
319 		return IIO_VAL_INT;
320 	case IIO_CHAN_INFO_SCALE:
321 		if (data->ref_mode == MCP472X_REF_VDD)
322 			ret = regulator_get_voltage(data->vdd_reg);
323 		else
324 			ret = regulator_get_voltage(data->vref_reg);
325 
326 		if (ret < 0)
327 			return ret;
328 
329 		*val = ret / 1000;
330 		*val2 = 12;
331 		return IIO_VAL_FRACTIONAL_LOG2;
332 	}
333 	return -EINVAL;
334 }
335 
336 static int mcp4725_write_raw(struct iio_dev *indio_dev,
337 			       struct iio_chan_spec const *chan,
338 			       int val, int val2, long mask)
339 {
340 	struct mcp4725_data *data = iio_priv(indio_dev);
341 	int ret;
342 
343 	switch (mask) {
344 	case IIO_CHAN_INFO_RAW:
345 		ret = mcp4725_set_value(indio_dev, val);
346 		data->dac_value = val;
347 		break;
348 	default:
349 		ret = -EINVAL;
350 		break;
351 	}
352 
353 	return ret;
354 }
355 
356 static const struct iio_info mcp4725_info = {
357 	.read_raw = mcp4725_read_raw,
358 	.write_raw = mcp4725_write_raw,
359 	.attrs = &mcp4725_attribute_group,
360 };
361 
362 #ifdef CONFIG_OF
363 static int mcp4725_probe_dt(struct device *dev,
364 			    struct mcp4725_platform_data *pdata)
365 {
366 	struct device_node *np = dev->of_node;
367 
368 	if (!np)
369 		return -ENODEV;
370 
371 	/* check if is the vref-supply defined */
372 	pdata->use_vref = of_property_read_bool(np, "vref-supply");
373 	pdata->vref_buffered =
374 		of_property_read_bool(np, "microchip,vref-buffered");
375 
376 	return 0;
377 }
378 #else
379 static int mcp4725_probe_dt(struct device *dev,
380 			    struct mcp4725_platform_data *platform_data)
381 {
382 	return -ENODEV;
383 }
384 #endif
385 
386 static int mcp4725_probe(struct i2c_client *client,
387 			 const struct i2c_device_id *id)
388 {
389 	struct mcp4725_data *data;
390 	struct iio_dev *indio_dev;
391 	struct mcp4725_platform_data *pdata, pdata_dt;
392 	u8 inbuf[4];
393 	u8 pd;
394 	u8 ref;
395 	int err;
396 
397 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
398 	if (indio_dev == NULL)
399 		return -ENOMEM;
400 	data = iio_priv(indio_dev);
401 	i2c_set_clientdata(client, indio_dev);
402 	data->client = client;
403 	if (client->dev.of_node)
404 		data->id = (enum chip_id)of_device_get_match_data(&client->dev);
405 	else
406 		data->id = id->driver_data;
407 	pdata = dev_get_platdata(&client->dev);
408 
409 	if (!pdata) {
410 		err = mcp4725_probe_dt(&client->dev, &pdata_dt);
411 		if (err) {
412 			dev_err(&client->dev,
413 				"invalid platform or devicetree data");
414 			return err;
415 		}
416 		pdata = &pdata_dt;
417 	}
418 
419 	if (data->id == MCP4725 && pdata->use_vref) {
420 		dev_err(&client->dev,
421 			"external reference is unavailable on MCP4725");
422 		return -EINVAL;
423 	}
424 
425 	if (!pdata->use_vref && pdata->vref_buffered) {
426 		dev_err(&client->dev,
427 			"buffering is unavailable on the internal reference");
428 		return -EINVAL;
429 	}
430 
431 	if (!pdata->use_vref)
432 		data->ref_mode = MCP472X_REF_VDD;
433 	else
434 		data->ref_mode = pdata->vref_buffered ?
435 			MCP472X_REF_VREF_BUFFERED :
436 			MCP472X_REF_VREF_UNBUFFERED;
437 
438 	data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
439 	if (IS_ERR(data->vdd_reg))
440 		return PTR_ERR(data->vdd_reg);
441 
442 	err = regulator_enable(data->vdd_reg);
443 	if (err)
444 		return err;
445 
446 	if (pdata->use_vref) {
447 		data->vref_reg = devm_regulator_get(&client->dev, "vref");
448 		if (IS_ERR(data->vref_reg)) {
449 			err = PTR_ERR(data->vref_reg);
450 			goto err_disable_vdd_reg;
451 		}
452 
453 		err = regulator_enable(data->vref_reg);
454 		if (err)
455 			goto err_disable_vdd_reg;
456 	}
457 
458 	indio_dev->dev.parent = &client->dev;
459 	indio_dev->name = id->name;
460 	indio_dev->info = &mcp4725_info;
461 	indio_dev->channels = &mcp472x_channel[id->driver_data];
462 	indio_dev->num_channels = 1;
463 	indio_dev->modes = INDIO_DIRECT_MODE;
464 
465 	/* read current DAC value and settings */
466 	err = i2c_master_recv(client, inbuf, data->id == MCP4725 ? 3 : 4);
467 
468 	if (err < 0) {
469 		dev_err(&client->dev, "failed to read DAC value");
470 		goto err_disable_vref_reg;
471 	}
472 	pd = (inbuf[0] >> 1) & 0x3;
473 	data->powerdown = pd > 0;
474 	data->powerdown_mode = pd ? pd - 1 : 2; /* largest resistor to gnd */
475 	data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
476 	if (data->id == MCP4726)
477 		ref = (inbuf[3] >> 3) & 0x3;
478 
479 	if (data->id == MCP4726 && ref != data->ref_mode) {
480 		dev_info(&client->dev,
481 			"voltage reference mode differs (conf: %u, eeprom: %u), setting %u",
482 			data->ref_mode, ref, data->ref_mode);
483 		err = mcp4726_set_cfg(indio_dev);
484 		if (err < 0)
485 			goto err_disable_vref_reg;
486 	}
487 
488 	err = iio_device_register(indio_dev);
489 	if (err)
490 		goto err_disable_vref_reg;
491 
492 	return 0;
493 
494 err_disable_vref_reg:
495 	if (data->vref_reg)
496 		regulator_disable(data->vref_reg);
497 
498 err_disable_vdd_reg:
499 	regulator_disable(data->vdd_reg);
500 
501 	return err;
502 }
503 
504 static int mcp4725_remove(struct i2c_client *client)
505 {
506 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
507 	struct mcp4725_data *data = iio_priv(indio_dev);
508 
509 	iio_device_unregister(indio_dev);
510 
511 	if (data->vref_reg)
512 		regulator_disable(data->vref_reg);
513 	regulator_disable(data->vdd_reg);
514 
515 	return 0;
516 }
517 
518 static const struct i2c_device_id mcp4725_id[] = {
519 	{ "mcp4725", MCP4725 },
520 	{ "mcp4726", MCP4726 },
521 	{ }
522 };
523 MODULE_DEVICE_TABLE(i2c, mcp4725_id);
524 
525 #ifdef CONFIG_OF
526 static const struct of_device_id mcp4725_of_match[] = {
527 	{
528 		.compatible = "microchip,mcp4725",
529 		.data = (void *)MCP4725
530 	},
531 	{
532 		.compatible = "microchip,mcp4726",
533 		.data = (void *)MCP4726
534 	},
535 	{ }
536 };
537 MODULE_DEVICE_TABLE(of, mcp4725_of_match);
538 #endif
539 
540 static struct i2c_driver mcp4725_driver = {
541 	.driver = {
542 		.name	= MCP4725_DRV_NAME,
543 		.of_match_table = of_match_ptr(mcp4725_of_match),
544 		.pm	= &mcp4725_pm_ops,
545 	},
546 	.probe		= mcp4725_probe,
547 	.remove		= mcp4725_remove,
548 	.id_table	= mcp4725_id,
549 };
550 module_i2c_driver(mcp4725_driver);
551 
552 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
553 MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
554 MODULE_LICENSE("GPL");
555