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