summaryrefslogtreecommitdiff
path: root/drivers/iio/dac/ti-dac5571.c
blob: 3210e3098f9ae277fd69d734fd5e324a22efcfdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// SPDX-License-Identifier: GPL-2.0-only
/*
 * ti-dac5571.c - Texas Instruments 8/10/12-bit 1/4-channel DAC driver
 *
 * Copyright (C) 2018 Prevas A/S
 *
 * https://www.ti.com/lit/ds/symlink/dac5571.pdf
 * https://www.ti.com/lit/ds/symlink/dac6571.pdf
 * https://www.ti.com/lit/ds/symlink/dac7571.pdf
 * https://www.ti.com/lit/ds/symlink/dac5574.pdf
 * https://www.ti.com/lit/ds/symlink/dac6574.pdf
 * https://www.ti.com/lit/ds/symlink/dac7574.pdf
 * https://www.ti.com/lit/ds/symlink/dac5573.pdf
 * https://www.ti.com/lit/ds/symlink/dac6573.pdf
 * https://www.ti.com/lit/ds/symlink/dac7573.pdf
 * https://www.ti.com/lit/ds/symlink/dac121c081.pdf
 */

#include <linux/iio/iio.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/property.h>
#include <linux/regulator/consumer.h>

enum chip_id {
	single_8bit, single_10bit, single_12bit,
	quad_8bit, quad_10bit, quad_12bit
};

struct dac5571_spec {
	u8 num_channels;
	u8 resolution;
};

static const struct dac5571_spec dac5571_spec[] = {
	[single_8bit]  = {.num_channels = 1, .resolution =  8},
	[single_10bit] = {.num_channels = 1, .resolution = 10},
	[single_12bit] = {.num_channels = 1, .resolution = 12},
	[quad_8bit]    = {.num_channels = 4, .resolution =  8},
	[quad_10bit]   = {.num_channels = 4, .resolution = 10},
	[quad_12bit]   = {.num_channels = 4, .resolution = 12},
};

struct dac5571_data {
	struct i2c_client *client;
	int id;
	struct mutex lock;
	struct regulator *vref;
	u16 val[4];
	bool powerdown[4];
	u8 powerdown_mode[4];
	struct dac5571_spec const *spec;
	int (*dac5571_cmd)(struct dac5571_data *data, int channel, u16 val);
	int (*dac5571_pwrdwn)(struct dac5571_data *data, int channel, u8 pwrdwn);
	u8 buf[3] __aligned(IIO_DMA_MINALIGN);
};

#define DAC5571_POWERDOWN(mode)		((mode) + 1)
#define DAC5571_POWERDOWN_FLAG		BIT(0)
#define DAC5571_CHANNEL_SELECT		1
#define DAC5571_LOADMODE_DIRECT		BIT(4)
#define DAC5571_SINGLE_PWRDWN_BITS	4
#define DAC5571_QUAD_PWRDWN_BITS	6

static int dac5571_cmd_single(struct dac5571_data *data, int channel, u16 val)
{
	unsigned int shift;

	shift = 12 - data->spec->resolution;
	data->buf[1] = val << shift;
	data->buf[0] = val >> (8 - shift);

	if (i2c_master_send(data->client, data->buf, 2) != 2)
		return -EIO;

	return 0;
}

static int dac5571_cmd_quad(struct dac5571_data *data, int channel, u16 val)
{
	unsigned int shift;

	shift = 16 - data->spec->resolution;
	data->buf[2] = val << shift;
	data->buf[1] = (val >> (8 - shift));
	data->buf[0] = (channel << DAC5571_CHANNEL_SELECT) |
		       DAC5571_LOADMODE_DIRECT;

	if (i2c_master_send(data->client, data->buf, 3) != 3)
		return -EIO;

	return 0;
}

static int dac5571_pwrdwn_single(struct dac5571_data *data, int channel, u8 pwrdwn)
{
	data->buf[1] = 0;
	data->buf[0] = pwrdwn << DAC5571_SINGLE_PWRDWN_BITS;

	if (i2c_master_send(data->client, data->buf, 2) != 2)
		return -EIO;

	return 0;
}

static int dac5571_pwrdwn_quad(struct dac5571_data *data, int channel, u8 pwrdwn)
{
	data->buf[2] = 0;
	data->buf[1] = pwrdwn << DAC5571_QUAD_PWRDWN_BITS;
	data->buf[0] = (channel << DAC5571_CHANNEL_SELECT) |
		       DAC5571_LOADMODE_DIRECT | DAC5571_POWERDOWN_FLAG;

	if (i2c_master_send(data->client, data->buf, 3) != 3)
		return -EIO;

	return 0;
}

static const char *const dac5571_powerdown_modes[] = {
	"1kohm_to_gnd", "100kohm_to_gnd", "three_state",
};

static int dac5571_get_powerdown_mode(struct iio_dev *indio_dev,
				      const struct iio_chan_spec *chan)
{
	struct dac5571_data *data = iio_priv(indio_dev);

	return data->powerdown_mode[chan->channel];
}

static int dac5571_set_powerdown_mode(struct iio_dev *indio_dev,
				      const struct iio_chan_spec *chan,
				      unsigned int mode)
{
	struct dac5571_data *data = iio_priv(indio_dev);
	int ret = 0;

	if (data->powerdown_mode[chan->channel] == mode)
		return 0;

	mutex_lock(&data->lock);
	if (data->powerdown[chan->channel]) {
		ret = data->dac5571_pwrdwn(data, chan->channel,
					   DAC5571_POWERDOWN(mode));
		if (ret)
			goto out;
	}
	data->powerdown_mode[chan->channel] = mode;

 out:
	mutex_unlock(&data->lock);

	return ret;
}

static const struct iio_enum dac5571_powerdown_mode = {
	.items = dac5571_powerdown_modes,
	.num_items = ARRAY_SIZE(dac5571_powerdown_modes),
	.get = dac5571_get_powerdown_mode,
	.set = dac5571_set_powerdown_mode,
};

static ssize_t dac5571_read_powerdown(struct iio_dev *indio_dev,
				      uintptr_t private,
				      const struct iio_chan_spec *chan,
				      char *buf)
{
	struct dac5571_data *data = iio_priv(indio_dev);

	return sysfs_emit(buf, "%d\n", data->powerdown[chan->channel]);
}

static ssize_t dac5571_write_powerdown(struct iio_dev *indio_dev,
				       uintptr_t private,
				       const struct iio_chan_spec *chan,
				       const char *buf, size_t len)
{
	struct dac5571_data *data = iio_priv(indio_dev);
	bool powerdown;
	int ret;

	ret = kstrtobool(buf, &powerdown);
	if (ret)
		return ret;

	if (data->powerdown[chan->channel] == powerdown)
		return len;

	mutex_lock(&data->lock);
	if (powerdown)
		ret = data->dac5571_pwrdwn(data, chan->channel,
			    DAC5571_POWERDOWN(data->powerdown_mode[chan->channel]));
	else
		ret = data->dac5571_cmd(data, chan->channel,
				data->val[chan->channel]);
	if (ret)
		goto out;

	data->powerdown[chan->channel] = powerdown;

 out:
	mutex_unlock(&data->lock);

	return ret ? ret : len;
}


static const struct iio_chan_spec_ext_info dac5571_ext_info[] = {
	{
		.name	   = "powerdown",
		.read	   = dac5571_read_powerdown,
		.write	   = dac5571_write_powerdown,
		.shared	   = IIO_SEPARATE,
	},
	IIO_ENUM("powerdown_mode", IIO_SEPARATE, &dac5571_powerdown_mode),
	IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE, &dac5571_powerdown_mode),
	{},
};

#define dac5571_CHANNEL(chan, name) {				\
	.type = IIO_VOLTAGE,					\
	.channel = (chan),					\
	.address = (chan),					\
	.indexed = true,					\
	.output = true,						\
	.datasheet_name = name,					\
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
	.ext_info = dac5571_ext_info,				\
}

static const struct iio_chan_spec dac5571_channels[] = {
	dac5571_CHANNEL(0, "A"),
	dac5571_CHANNEL(1, "B"),
	dac5571_CHANNEL(2, "C"),
	dac5571_CHANNEL(3, "D"),
};

static int dac5571_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan,
			    int *val, int *val2, long mask)
{
	struct dac5571_data *data = iio_priv(indio_dev);
	int ret;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		*val = data->val[chan->channel];
		return IIO_VAL_INT;

	case IIO_CHAN_INFO_SCALE:
		ret = regulator_get_voltage(data->vref);
		if (ret < 0)
			return ret;

		*val = ret / 1000;
		*val2 = data->spec->resolution;
		return IIO_VAL_FRACTIONAL_LOG2;

	default:
		return -EINVAL;
	}
}

static int dac5571_write_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan,
			     int val, int val2, long mask)
{
	struct dac5571_data *data = iio_priv(indio_dev);
	int ret;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		if (data->val[chan->channel] == val)
			return 0;

		if (val >= (1 << data->spec->resolution) || val < 0)
			return -EINVAL;

		if (data->powerdown[chan->channel])
			return -EBUSY;

		mutex_lock(&data->lock);
		ret = data->dac5571_cmd(data, chan->channel, val);
		if (ret == 0)
			data->val[chan->channel] = val;
		mutex_unlock(&data->lock);
		return ret;

	default:
		return -EINVAL;
	}
}

static int dac5571_write_raw_get_fmt(struct iio_dev *indio_dev,
				     struct iio_chan_spec const *chan,
				     long mask)
{
	return IIO_VAL_INT;
}

static const struct iio_info dac5571_info = {
	.read_raw = dac5571_read_raw,
	.write_raw = dac5571_write_raw,
	.write_raw_get_fmt = dac5571_write_raw_get_fmt,
};

static int dac5571_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
{
	struct device *dev = &client->dev;
	const struct dac5571_spec *spec;
	struct dac5571_data *data;
	struct iio_dev *indio_dev;
	enum chip_id chip_id;
	int ret, i;

	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
	if (!indio_dev)
		return -ENOMEM;

	data = iio_priv(indio_dev);
	i2c_set_clientdata(client, indio_dev);
	data->client = client;

	indio_dev->info = &dac5571_info;
	indio_dev->name = id->name;
	indio_dev->modes = INDIO_DIRECT_MODE;
	indio_dev->channels = dac5571_channels;

	if (dev_fwnode(dev))
		chip_id = (uintptr_t)device_get_match_data(dev);
	else
		chip_id = id->driver_data;

	spec = &dac5571_spec[chip_id];

	indio_dev->num_channels = spec->num_channels;
	data->spec = spec;

	data->vref = devm_regulator_get(dev, "vref");
	if (IS_ERR(data->vref))
		return PTR_ERR(data->vref);

	ret = regulator_enable(data->vref);
	if (ret < 0)
		return ret;

	mutex_init(&data->lock);

	switch (spec->num_channels) {
	case 1:
		data->dac5571_cmd = dac5571_cmd_single;
		data->dac5571_pwrdwn = dac5571_pwrdwn_single;
		break;
	case 4:
		data->dac5571_cmd = dac5571_cmd_quad;
		data->dac5571_pwrdwn = dac5571_pwrdwn_quad;
		break;
	default:
		ret = -EINVAL;
		goto err;
	}

	for (i = 0; i < spec->num_channels; i++) {
		ret = data->dac5571_cmd(data, i, 0);
		if (ret) {
			dev_err(dev, "failed to initialize channel %d to 0\n", i);
			goto err;
		}
	}

	ret = iio_device_register(indio_dev);
	if (ret)
		goto err;

	return 0;

 err:
	regulator_disable(data->vref);
	return ret;
}

static void dac5571_remove(struct i2c_client *i2c)
{
	struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
	struct dac5571_data *data = iio_priv(indio_dev);

	iio_device_unregister(indio_dev);
	regulator_disable(data->vref);
}

static const struct of_device_id dac5571_of_id[] = {
	{.compatible = "ti,dac5571", .data = (void *)single_8bit},
	{.compatible = "ti,dac6571", .data = (void *)single_10bit},
	{.compatible = "ti,dac7571", .data = (void *)single_12bit},
	{.compatible = "ti,dac5574", .data = (void *)quad_8bit},
	{.compatible = "ti,dac6574", .data = (void *)quad_10bit},
	{.compatible = "ti,dac7574", .data = (void *)quad_12bit},
	{.compatible = "ti,dac5573", .data = (void *)quad_8bit},
	{.compatible = "ti,dac6573", .data = (void *)quad_10bit},
	{.compatible = "ti,dac7573", .data = (void *)quad_12bit},
	{.compatible = "ti,dac121c081", .data = (void *)single_12bit},
	{}
};
MODULE_DEVICE_TABLE(of, dac5571_of_id);

static const struct i2c_device_id dac5571_id[] = {
	{"dac5571", single_8bit},
	{"dac6571", single_10bit},
	{"dac7571", single_12bit},
	{"dac5574", quad_8bit},
	{"dac6574", quad_10bit},
	{"dac7574", quad_12bit},
	{"dac5573", quad_8bit},
	{"dac6573", quad_10bit},
	{"dac7573", quad_12bit},
	{"dac121c081", single_12bit},
	{}
};
MODULE_DEVICE_TABLE(i2c, dac5571_id);

static struct i2c_driver dac5571_driver = {
	.driver = {
		   .name = "ti-dac5571",
		   .of_match_table = dac5571_of_id,
	},
	.probe	  = dac5571_probe,
	.remove   = dac5571_remove,
	.id_table = dac5571_id,
};
module_i2c_driver(dac5571_driver);

MODULE_AUTHOR("Sean Nyekjaer <sean@geanix.dk>");
MODULE_DESCRIPTION("Texas Instruments 8/10/12-bit 1/4-channel DAC driver");
MODULE_LICENSE("GPL v2");