xref: /openbmc/linux/drivers/iio/dac/max5821.c (revision ae213c44)
1  /*
2   * iio/dac/max5821.c
3   * Copyright (C) 2014 Philippe Reynes
4   *
5   * This program is free software; you can redistribute it and/or modify
6   * it under the terms of the GNU General Public License version 2 as
7   * published by the Free Software Foundation.
8   */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/iio/iio.h>
14 #include <linux/regulator/consumer.h>
15 
16 #define MAX5821_MAX_DAC_CHANNELS		2
17 
18 /* command bytes */
19 #define MAX5821_LOAD_DAC_A_IN_REG_B		0x00
20 #define MAX5821_LOAD_DAC_B_IN_REG_A		0x10
21 #define MAX5821_EXTENDED_COMMAND_MODE		0xf0
22 #define MAX5821_READ_DAC_A_COMMAND		0xf1
23 #define MAX5821_READ_DAC_B_COMMAND		0xf2
24 
25 #define MAX5821_EXTENDED_POWER_UP		0x00
26 #define MAX5821_EXTENDED_POWER_DOWN_MODE0	0x01
27 #define MAX5821_EXTENDED_POWER_DOWN_MODE1	0x02
28 #define MAX5821_EXTENDED_POWER_DOWN_MODE2	0x03
29 #define MAX5821_EXTENDED_DAC_A			0x04
30 #define MAX5821_EXTENDED_DAC_B			0x08
31 
32 enum max5821_device_ids {
33 	ID_MAX5821,
34 };
35 
36 struct max5821_data {
37 	struct i2c_client	*client;
38 	struct regulator	*vref_reg;
39 	unsigned short		vref_mv;
40 	bool			powerdown[MAX5821_MAX_DAC_CHANNELS];
41 	u8			powerdown_mode[MAX5821_MAX_DAC_CHANNELS];
42 	struct mutex		lock;
43 };
44 
45 static const char * const max5821_powerdown_modes[] = {
46 	"three_state",
47 	"1kohm_to_gnd",
48 	"100kohm_to_gnd",
49 };
50 
51 enum {
52 	MAX5821_THREE_STATE,
53 	MAX5821_1KOHM_TO_GND,
54 	MAX5821_100KOHM_TO_GND
55 };
56 
57 static int max5821_get_powerdown_mode(struct iio_dev *indio_dev,
58 				      const struct iio_chan_spec *chan)
59 {
60 	struct max5821_data *st = iio_priv(indio_dev);
61 
62 	return st->powerdown_mode[chan->channel];
63 }
64 
65 static int max5821_set_powerdown_mode(struct iio_dev *indio_dev,
66 				      const struct iio_chan_spec *chan,
67 				      unsigned int mode)
68 {
69 	struct max5821_data *st = iio_priv(indio_dev);
70 
71 	st->powerdown_mode[chan->channel] = mode;
72 
73 	return 0;
74 }
75 
76 static const struct iio_enum max5821_powerdown_mode_enum = {
77 	.items = max5821_powerdown_modes,
78 	.num_items = ARRAY_SIZE(max5821_powerdown_modes),
79 	.get = max5821_get_powerdown_mode,
80 	.set = max5821_set_powerdown_mode,
81 };
82 
83 static ssize_t max5821_read_dac_powerdown(struct iio_dev *indio_dev,
84 					  uintptr_t private,
85 					  const struct iio_chan_spec *chan,
86 					  char *buf)
87 {
88 	struct max5821_data *st = iio_priv(indio_dev);
89 
90 	return sprintf(buf, "%d\n", st->powerdown[chan->channel]);
91 }
92 
93 static int max5821_sync_powerdown_mode(struct max5821_data *data,
94 				       const struct iio_chan_spec *chan)
95 {
96 	u8 outbuf[2];
97 
98 	outbuf[0] = MAX5821_EXTENDED_COMMAND_MODE;
99 
100 	if (chan->channel == 0)
101 		outbuf[1] = MAX5821_EXTENDED_DAC_A;
102 	else
103 		outbuf[1] = MAX5821_EXTENDED_DAC_B;
104 
105 	if (data->powerdown[chan->channel])
106 		outbuf[1] |= data->powerdown_mode[chan->channel] + 1;
107 	else
108 		outbuf[1] |= MAX5821_EXTENDED_POWER_UP;
109 
110 	return i2c_master_send(data->client, outbuf, 2);
111 }
112 
113 static ssize_t max5821_write_dac_powerdown(struct iio_dev *indio_dev,
114 					   uintptr_t private,
115 					   const struct iio_chan_spec *chan,
116 					   const char *buf, size_t len)
117 {
118 	struct max5821_data *data = iio_priv(indio_dev);
119 	bool powerdown;
120 	int ret;
121 
122 	ret = strtobool(buf, &powerdown);
123 	if (ret)
124 		return ret;
125 
126 	data->powerdown[chan->channel] = powerdown;
127 
128 	ret = max5821_sync_powerdown_mode(data, chan);
129 	if (ret < 0)
130 		return ret;
131 
132 	return len;
133 }
134 
135 static const struct iio_chan_spec_ext_info max5821_ext_info[] = {
136 	{
137 		.name = "powerdown",
138 		.read = max5821_read_dac_powerdown,
139 		.write = max5821_write_dac_powerdown,
140 		.shared = IIO_SEPARATE,
141 	},
142 	IIO_ENUM("powerdown_mode", IIO_SEPARATE, &max5821_powerdown_mode_enum),
143 	IIO_ENUM_AVAILABLE("powerdown_mode", &max5821_powerdown_mode_enum),
144 	{ },
145 };
146 
147 #define MAX5821_CHANNEL(chan) {					\
148 	.type = IIO_VOLTAGE,					\
149 	.indexed = 1,						\
150 	.output = 1,						\
151 	.channel = (chan),					\
152 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
153 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE),	\
154 	.ext_info = max5821_ext_info,				\
155 }
156 
157 static const struct iio_chan_spec max5821_channels[] = {
158 	MAX5821_CHANNEL(0),
159 	MAX5821_CHANNEL(1)
160 };
161 
162 static const u8 max5821_read_dac_command[] = {
163 	MAX5821_READ_DAC_A_COMMAND,
164 	MAX5821_READ_DAC_B_COMMAND
165 };
166 
167 static const u8 max5821_load_dac_command[] = {
168 	MAX5821_LOAD_DAC_A_IN_REG_B,
169 	MAX5821_LOAD_DAC_B_IN_REG_A
170 };
171 
172 static int max5821_get_value(struct iio_dev *indio_dev,
173 			     int *val, int channel)
174 {
175 	struct max5821_data *data = iio_priv(indio_dev);
176 	struct i2c_client *client = data->client;
177 	u8 outbuf[1];
178 	u8 inbuf[2];
179 	int ret;
180 
181 	if ((channel != 0) && (channel != 1))
182 		return -EINVAL;
183 
184 	outbuf[0] = max5821_read_dac_command[channel];
185 
186 	mutex_lock(&data->lock);
187 
188 	ret = i2c_master_send(client, outbuf, 1);
189 	if (ret < 0) {
190 		mutex_unlock(&data->lock);
191 		return ret;
192 	} else if (ret != 1) {
193 		mutex_unlock(&data->lock);
194 		return -EIO;
195 	}
196 
197 	ret = i2c_master_recv(client, inbuf, 2);
198 	if (ret < 0) {
199 		mutex_unlock(&data->lock);
200 		return ret;
201 	} else if (ret != 2) {
202 		mutex_unlock(&data->lock);
203 		return -EIO;
204 	}
205 
206 	mutex_unlock(&data->lock);
207 
208 	*val = ((inbuf[0] & 0x0f) << 6) | (inbuf[1] >> 2);
209 
210 	return IIO_VAL_INT;
211 }
212 
213 static int max5821_set_value(struct iio_dev *indio_dev,
214 			     int val, int channel)
215 {
216 	struct max5821_data *data = iio_priv(indio_dev);
217 	struct i2c_client *client = data->client;
218 	u8 outbuf[2];
219 	int ret;
220 
221 	if ((val < 0) || (val > 1023))
222 		return -EINVAL;
223 
224 	if ((channel != 0) && (channel != 1))
225 		return -EINVAL;
226 
227 	outbuf[0] = max5821_load_dac_command[channel];
228 	outbuf[0] |= val >> 6;
229 	outbuf[1] = (val & 0x3f) << 2;
230 
231 	ret = i2c_master_send(client, outbuf, 2);
232 	if (ret < 0)
233 		return ret;
234 	else if (ret != 2)
235 		return -EIO;
236 	else
237 		return 0;
238 }
239 
240 static int max5821_read_raw(struct iio_dev *indio_dev,
241 			   struct iio_chan_spec const *chan,
242 			   int *val, int *val2, long mask)
243 {
244 	struct max5821_data *data = iio_priv(indio_dev);
245 
246 	switch (mask) {
247 	case IIO_CHAN_INFO_RAW:
248 		return max5821_get_value(indio_dev, val, chan->channel);
249 	case IIO_CHAN_INFO_SCALE:
250 		*val = data->vref_mv;
251 		*val2 = 10;
252 		return IIO_VAL_FRACTIONAL_LOG2;
253 	default:
254 		return -EINVAL;
255 	}
256 }
257 
258 static int max5821_write_raw(struct iio_dev *indio_dev,
259 			     struct iio_chan_spec const *chan,
260 			     int val, int val2, long mask)
261 {
262 	if (val2 != 0)
263 		return -EINVAL;
264 
265 	switch (mask) {
266 	case IIO_CHAN_INFO_RAW:
267 		return max5821_set_value(indio_dev, val, chan->channel);
268 	default:
269 		return -EINVAL;
270 	}
271 }
272 
273 static int __maybe_unused max5821_suspend(struct device *dev)
274 {
275 	u8 outbuf[2] = { MAX5821_EXTENDED_COMMAND_MODE,
276 			 MAX5821_EXTENDED_DAC_A |
277 			 MAX5821_EXTENDED_DAC_B |
278 			 MAX5821_EXTENDED_POWER_DOWN_MODE2 };
279 
280 	return i2c_master_send(to_i2c_client(dev), outbuf, 2);
281 }
282 
283 static int __maybe_unused max5821_resume(struct device *dev)
284 {
285 	u8 outbuf[2] = { MAX5821_EXTENDED_COMMAND_MODE,
286 			 MAX5821_EXTENDED_DAC_A |
287 			 MAX5821_EXTENDED_DAC_B |
288 			 MAX5821_EXTENDED_POWER_UP };
289 
290 	return i2c_master_send(to_i2c_client(dev), outbuf, 2);
291 }
292 
293 static SIMPLE_DEV_PM_OPS(max5821_pm_ops, max5821_suspend, max5821_resume);
294 
295 static const struct iio_info max5821_info = {
296 	.read_raw = max5821_read_raw,
297 	.write_raw = max5821_write_raw,
298 };
299 
300 static int max5821_probe(struct i2c_client *client,
301 			const struct i2c_device_id *id)
302 {
303 	struct max5821_data *data;
304 	struct iio_dev *indio_dev;
305 	u32 tmp;
306 	int ret;
307 
308 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
309 	if (!indio_dev)
310 		return -ENOMEM;
311 	data = iio_priv(indio_dev);
312 	i2c_set_clientdata(client, indio_dev);
313 	data->client = client;
314 	mutex_init(&data->lock);
315 
316 	/* max5821 start in powerdown mode 100Kohm to ground */
317 	for (tmp = 0; tmp < MAX5821_MAX_DAC_CHANNELS; tmp++) {
318 		data->powerdown[tmp] = true;
319 		data->powerdown_mode[tmp] = MAX5821_100KOHM_TO_GND;
320 	}
321 
322 	data->vref_reg = devm_regulator_get(&client->dev, "vref");
323 	if (IS_ERR(data->vref_reg)) {
324 		ret = PTR_ERR(data->vref_reg);
325 		dev_err(&client->dev,
326 			"Failed to get vref regulator: %d\n", ret);
327 		goto error_free_reg;
328 	}
329 
330 	ret = regulator_enable(data->vref_reg);
331 	if (ret) {
332 		dev_err(&client->dev,
333 			"Failed to enable vref regulator: %d\n", ret);
334 		goto error_free_reg;
335 	}
336 
337 	ret = regulator_get_voltage(data->vref_reg);
338 	if (ret < 0) {
339 		dev_err(&client->dev,
340 			"Failed to get voltage on regulator: %d\n", ret);
341 		goto error_disable_reg;
342 	}
343 
344 	data->vref_mv = ret / 1000;
345 
346 	indio_dev->name = id->name;
347 	indio_dev->dev.parent = &client->dev;
348 	indio_dev->num_channels = ARRAY_SIZE(max5821_channels);
349 	indio_dev->channels = max5821_channels;
350 	indio_dev->modes = INDIO_DIRECT_MODE;
351 	indio_dev->info = &max5821_info;
352 
353 	return iio_device_register(indio_dev);
354 
355 error_disable_reg:
356 	regulator_disable(data->vref_reg);
357 
358 error_free_reg:
359 
360 	return ret;
361 }
362 
363 static int max5821_remove(struct i2c_client *client)
364 {
365 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
366 	struct max5821_data *data = iio_priv(indio_dev);
367 
368 	iio_device_unregister(indio_dev);
369 	regulator_disable(data->vref_reg);
370 
371 	return 0;
372 }
373 
374 static const struct i2c_device_id max5821_id[] = {
375 	{ "max5821", ID_MAX5821 },
376 	{ }
377 };
378 MODULE_DEVICE_TABLE(i2c, max5821_id);
379 
380 static const struct of_device_id max5821_of_match[] = {
381 	{ .compatible = "maxim,max5821" },
382 	{ }
383 };
384 MODULE_DEVICE_TABLE(of, max5821_of_match);
385 
386 static struct i2c_driver max5821_driver = {
387 	.driver = {
388 		.name	= "max5821",
389 		.of_match_table = max5821_of_match,
390 		.pm     = &max5821_pm_ops,
391 	},
392 	.probe		= max5821_probe,
393 	.remove		= max5821_remove,
394 	.id_table	= max5821_id,
395 };
396 module_i2c_driver(max5821_driver);
397 
398 MODULE_AUTHOR("Philippe Reynes <tremyfr@yahoo.fr>");
399 MODULE_DESCRIPTION("MAX5821 DAC");
400 MODULE_LICENSE("GPL v2");
401