aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio/dac/max5821.c
blob: 6e914495b346450c6ebdb2bf75851de278a98863 (plain) (blame)
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
 /*
  * iio/dac/max5821.c
  * Copyright (C) 2014 Philippe Reynes
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  */

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

#define MAX5821_MAX_DAC_CHANNELS		2

/* command bytes */
#define MAX5821_LOAD_DAC_A_IN_REG_B		0x00
#define MAX5821_LOAD_DAC_B_IN_REG_A		0x10
#define MAX5821_EXTENDED_COMMAND_MODE		0xf0
#define MAX5821_READ_DAC_A_COMMAND		0xf1
#define MAX5821_READ_DAC_B_COMMAND		0xf2

#define MAX5821_EXTENDED_POWER_UP		0x00
#define MAX5821_EXTENDED_POWER_DOWN_MODE0	0x01
#define MAX5821_EXTENDED_POWER_DOWN_MODE1	0x02
#define MAX5821_EXTENDED_POWER_DOWN_MODE2	0x03
#define MAX5821_EXTENDED_DAC_A			0x04
#define MAX5821_EXTENDED_DAC_B			0x08

enum max5821_device_ids {
	ID_MAX5821,
};

struct max5821_data {
	struct i2c_client	*client;
	struct regulator	*vref_reg;
	unsigned short		vref_mv;
	bool			powerdown[MAX5821_MAX_DAC_CHANNELS];
	u8			powerdown_mode[MAX5821_MAX_DAC_CHANNELS];
	struct mutex		lock;
};

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

enum {
	MAX5821_THREE_STATE,
	MAX5821_1KOHM_TO_GND,
	MAX5821_100KOHM_TO_GND
};

static int max5821_get_powerdown_mode(struct iio_dev *indio_dev,
				      const struct iio_chan_spec *chan)
{
	struct max5821_data *st = iio_priv(indio_dev);

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

static int max5821_set_powerdown_mode(struct iio_dev *indio_dev,
				      const struct iio_chan_spec *chan,
				      unsigned int mode)
{
	struct max5821_data *st = iio_priv(indio_dev);

	st->powerdown_mode[chan->channel] = mode;

	return 0;
}

static const struct iio_enum max5821_powerdown_mode_enum = {
	.items = max5821_powerdown_modes,
	.num_items = ARRAY_SIZE(max5821_powerdown_modes),
	.get = max5821_get_powerdown_mode,
	.set = max5821_set_powerdown_mode,
};

static ssize_t max5821_read_dac_powerdown(struct iio_dev *indio_dev,
					  uintptr_t private,
					  const struct iio_chan_spec *chan,
					  char *buf)
{
	struct max5821_data *st = iio_priv(indio_dev);

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

static int max5821_sync_powerdown_mode(struct max5821_data *data,
				       const struct iio_chan_spec *chan)
{
	u8 outbuf[2];

	outbuf[0] = MAX5821_EXTENDED_COMMAND_MODE;

	if (chan->channel == 0)
		outbuf[1] = MAX5821_EXTENDED_DAC_A;
	else
		outbuf[1] = MAX5821_EXTENDED_DAC_B;

	if (data->powerdown[chan->channel])
		outbuf[1] |= data->powerdown_mode[chan->channel] + 1;
	else
		outbuf[1] |= MAX5821_EXTENDED_POWER_UP;

	return i2c_master_send(data->client, outbuf, 2);
}

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

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

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

	ret = max5821_sync_powerdown_mode(data, chan);
	if (ret < 0)
		return ret;

	return len;
}

static const struct iio_chan_spec_ext_info max5821_ext_info[] = {
	{
		.name = "powerdown",
		.read = max5821_read_dac_powerdown,
		.write = max5821_write_dac_powerdown,
		.shared = IIO_SEPARATE,
	},
	IIO_ENUM("powerdown_mode", IIO_SEPARATE, &max5821_powerdown_mode_enum),
	IIO_ENUM_AVAILABLE("powerdown_mode", &max5821_powerdown_mode_enum),
	{ },
};

#define MAX5821_CHANNEL(chan) {					\
	.type = IIO_VOLTAGE,					\
	.indexed = 1,						\
	.output = 1,						\
	.channel = (chan),					\
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE),	\
	.ext_info = max5821_ext_info,				\
}

static const struct iio_chan_spec max5821_channels[] = {
	MAX5821_CHANNEL(0),
	MAX5821_CHANNEL(1)
};

static const u8 max5821_read_dac_command[] = {
	MAX5821_READ_DAC_A_COMMAND,
	MAX5821_READ_DAC_B_COMMAND
};

static const u8 max5821_load_dac_command[] = {
	MAX5821_LOAD_DAC_A_IN_REG_B,
	MAX5821_LOAD_DAC_B_IN_REG_A
};

static int max5821_get_value(struct iio_dev *indio_dev,
			     int *val, int channel)
{
	struct max5821_data *data = iio_priv(indio_dev);
	struct i2c_client *client = data->client;
	u8 outbuf[1];
	u8 inbuf[2];
	int ret;

	if ((channel != 0) && (channel != 1))
		return -EINVAL;

	outbuf[0] = max5821_read_dac_command[channel];

	mutex_lock(&data->lock);

	ret = i2c_master_send(client, outbuf, 1);
	if (ret < 0) {
		mutex_unlock(&data->lock);
		return ret;
	} else if (ret != 1) {
		mutex_unlock(&data->lock);
		return -EIO;
	}

	ret = i2c_master_recv(client, inbuf, 2);
	if (ret < 0) {
		mutex_unlock(&data->lock);
		return ret;
	} else if (ret != 2) {
		mutex_unlock(&data->lock);
		return -EIO;
	}

	mutex_unlock(&data->lock);

	*val = ((inbuf[0] & 0x0f) << 6) | (inbuf[1] >> 2);

	return IIO_VAL_INT;
}

static int max5821_set_value(struct iio_dev *indio_dev,
			     int val, int channel)
{
	struct max5821_data *data = iio_priv(indio_dev);
	struct i2c_client *client = data->client;
	u8 outbuf[2];
	int ret;

	if ((val < 0) || (val > 1023))
		return -EINVAL;

	if ((channel != 0) && (channel != 1))
		return -EINVAL;

	outbuf[0] = max5821_load_dac_command[channel];
	outbuf[0] |= val >> 6;
	outbuf[1] = (val & 0x3f) << 2;

	ret = i2c_master_send(client, outbuf, 2);
	if (ret < 0)
		return ret;
	else if (ret != 2)
		return -EIO;
	else
		return 0;
}

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

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		return max5821_get_value(indio_dev, val, chan->channel);
	case IIO_CHAN_INFO_SCALE:
		*val = data->vref_mv;
		*val2 = 10;
		return IIO_VAL_FRACTIONAL_LOG2;
	default:
		return -EINVAL;
	}
}

static int max5821_write_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan,
			     int val, int val2, long mask)
{
	if (val2 != 0)
		return -EINVAL;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		return max5821_set_value(indio_dev, val, chan->channel);
	default:
		return -EINVAL;
	}
}

#ifdef CONFIG_PM_SLEEP
static int max5821_suspend(struct device *dev)
{
	u8 outbuf[2] = { MAX5821_EXTENDED_COMMAND_MODE,
			 MAX5821_EXTENDED_DAC_A |
			 MAX5821_EXTENDED_DAC_B |
			 MAX5821_EXTENDED_POWER_DOWN_MODE2 };

	return i2c_master_send(to_i2c_client(dev), outbuf, 2);
}

static int max5821_resume(struct device *dev)
{
	u8 outbuf[2] = { MAX5821_EXTENDED_COMMAND_MODE,
			 MAX5821_EXTENDED_DAC_A |
			 MAX5821_EXTENDED_DAC_B |
			 MAX5821_EXTENDED_POWER_UP };

	return i2c_master_send(to_i2c_client(dev), outbuf, 2);
}

static SIMPLE_DEV_PM_OPS(max5821_pm_ops, max5821_suspend, max5821_resume);
#define MAX5821_PM_OPS (&max5821_pm_ops)
#else
#define MAX5821_PM_OPS NULL
#endif /* CONFIG_PM_SLEEP */

static const struct iio_info max5821_info = {
	.read_raw = max5821_read_raw,
	.write_raw = max5821_write_raw,
	.driver_module = THIS_MODULE,
};

static int max5821_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	struct max5821_data *data;
	struct iio_dev *indio_dev;
	u32 tmp;
	int ret;

	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
	if (!indio_dev)
		return -ENOMEM;
	data = iio_priv(indio_dev);
	i2c_set_clientdata(client, indio_dev);
	data->client = client;
	mutex_init(&data->lock);

	/* max5821 start in powerdown mode 100Kohm to ground */
	for (tmp = 0; tmp < MAX5821_MAX_DAC_CHANNELS; tmp++) {
		data->powerdown[tmp] = true;
		data->powerdown_mode[tmp] = MAX5821_100KOHM_TO_GND;
	}

	data->vref_reg = devm_regulator_get(&client->dev, "vref");
	if (IS_ERR(data->vref_reg)) {
		ret = PTR_ERR(data->vref_reg);
		dev_err(&client->dev,
			"Failed to get vref regulator: %d\n", ret);
		goto error_free_reg;
	}

	ret = regulator_enable(data->vref_reg);
	if (ret) {
		dev_err(&client->dev,
			"Failed to enable vref regulator: %d\n", ret);
		goto error_free_reg;
	}

	ret = regulator_get_voltage(data->vref_reg);
	if (ret < 0) {
		dev_err(&client->dev,
			"Failed to get voltage on regulator: %d\n", ret);
		goto error_disable_reg;
	}

	data->vref_mv = ret / 1000;

	indio_dev->name = id->name;
	indio_dev->dev.parent = &client->dev;
	indio_dev->num_channels = ARRAY_SIZE(max5821_channels);
	indio_dev->channels = max5821_channels;
	indio_dev->modes = INDIO_DIRECT_MODE;
	indio_dev->info = &max5821_info;

	return iio_device_register(indio_dev);

error_disable_reg:
	regulator_disable(data->vref_reg);

error_free_reg:

	return ret;
}

static int max5821_remove(struct i2c_client *client)
{
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct max5821_data *data = iio_priv(indio_dev);

	iio_device_unregister(indio_dev);
	regulator_disable(data->vref_reg);

	return 0;
}

static const struct i2c_device_id max5821_id[] = {
	{ "max5821", ID_MAX5821 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, max5821_id);

static const struct of_device_id max5821_of_match[] = {
	{ .compatible = "maxim,max5821" },
	{ }
};

static struct i2c_driver max5821_driver = {
	.driver = {
		.name	= "max5821",
		.pm     = MAX5821_PM_OPS,
		.owner	= THIS_MODULE,
	},
	.probe		= max5821_probe,
	.remove		= max5821_remove,
	.id_table	= max5821_id,
};
module_i2c_driver(max5821_driver);

MODULE_AUTHOR("Philippe Reynes <tremyfr@yahoo.fr>");
MODULE_DESCRIPTION("MAX5821 DAC");
MODULE_LICENSE("GPL v2");