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