xref: /openbmc/linux/drivers/iio/dac/ad5504.c (revision b8d312aa)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AD5504, AD5501 High Voltage Digital to Analog Converter
4  *
5  * Copyright 2011 Analog Devices Inc.
6  */
7 
8 #include <linux/interrupt.h>
9 #include <linux/fs.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/spi/spi.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/module.h>
17 #include <linux/bitops.h>
18 
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/events.h>
22 #include <linux/iio/dac/ad5504.h>
23 
24 #define AD5504_RES_MASK			GENMASK(11, 0)
25 #define AD5504_CMD_READ			BIT(15)
26 #define AD5504_CMD_WRITE		0
27 #define AD5504_ADDR(addr)		((addr) << 12)
28 
29 /* Registers */
30 #define AD5504_ADDR_NOOP		0
31 #define AD5504_ADDR_DAC(x)		((x) + 1)
32 #define AD5504_ADDR_ALL_DAC		5
33 #define AD5504_ADDR_CTRL		7
34 
35 /* Control Register */
36 #define AD5504_DAC_PWR(ch)		((ch) << 2)
37 #define AD5504_DAC_PWRDWN_MODE(mode)	((mode) << 6)
38 #define AD5504_DAC_PWRDN_20K		0
39 #define AD5504_DAC_PWRDN_3STATE		1
40 
41 /**
42  * struct ad5446_state - driver instance specific data
43  * @spi:			spi_device
44  * @reg:		supply regulator
45  * @vref_mv:		actual reference voltage used
46  * @pwr_down_mask	power down mask
47  * @pwr_down_mode	current power down mode
48  * @data:		transfer buffer
49  */
50 struct ad5504_state {
51 	struct spi_device		*spi;
52 	struct regulator		*reg;
53 	unsigned short			vref_mv;
54 	unsigned			pwr_down_mask;
55 	unsigned			pwr_down_mode;
56 
57 	__be16				data[2] ____cacheline_aligned;
58 };
59 
60 /**
61  * ad5504_supported_device_ids:
62  */
63 
64 enum ad5504_supported_device_ids {
65 	ID_AD5504,
66 	ID_AD5501,
67 };
68 
69 static int ad5504_spi_write(struct ad5504_state *st, u8 addr, u16 val)
70 {
71 	st->data[0] = cpu_to_be16(AD5504_CMD_WRITE | AD5504_ADDR(addr) |
72 			      (val & AD5504_RES_MASK));
73 
74 	return spi_write(st->spi, &st->data[0], 2);
75 }
76 
77 static int ad5504_spi_read(struct ad5504_state *st, u8 addr)
78 {
79 	int ret;
80 	struct spi_transfer t = {
81 	    .tx_buf = &st->data[0],
82 	    .rx_buf = &st->data[1],
83 	    .len = 2,
84 	};
85 
86 	st->data[0] = cpu_to_be16(AD5504_CMD_READ | AD5504_ADDR(addr));
87 	ret = spi_sync_transfer(st->spi, &t, 1);
88 	if (ret < 0)
89 		return ret;
90 
91 	return be16_to_cpu(st->data[1]) & AD5504_RES_MASK;
92 }
93 
94 static int ad5504_read_raw(struct iio_dev *indio_dev,
95 			   struct iio_chan_spec const *chan,
96 			   int *val,
97 			   int *val2,
98 			   long m)
99 {
100 	struct ad5504_state *st = iio_priv(indio_dev);
101 	int ret;
102 
103 	switch (m) {
104 	case IIO_CHAN_INFO_RAW:
105 		ret = ad5504_spi_read(st, chan->address);
106 		if (ret < 0)
107 			return ret;
108 
109 		*val = ret;
110 
111 		return IIO_VAL_INT;
112 	case IIO_CHAN_INFO_SCALE:
113 		*val = st->vref_mv;
114 		*val2 = chan->scan_type.realbits;
115 		return IIO_VAL_FRACTIONAL_LOG2;
116 	}
117 	return -EINVAL;
118 }
119 
120 static int ad5504_write_raw(struct iio_dev *indio_dev,
121 			       struct iio_chan_spec const *chan,
122 			       int val,
123 			       int val2,
124 			       long mask)
125 {
126 	struct ad5504_state *st = iio_priv(indio_dev);
127 
128 	switch (mask) {
129 	case IIO_CHAN_INFO_RAW:
130 		if (val >= (1 << chan->scan_type.realbits) || val < 0)
131 			return -EINVAL;
132 
133 		return ad5504_spi_write(st, chan->address, val);
134 	default:
135 		return -EINVAL;
136 	}
137 }
138 
139 static const char * const ad5504_powerdown_modes[] = {
140 	"20kohm_to_gnd",
141 	"three_state",
142 };
143 
144 static int ad5504_get_powerdown_mode(struct iio_dev *indio_dev,
145 	const struct iio_chan_spec *chan)
146 {
147 	struct ad5504_state *st = iio_priv(indio_dev);
148 
149 	return st->pwr_down_mode;
150 }
151 
152 static int ad5504_set_powerdown_mode(struct iio_dev *indio_dev,
153 	const struct iio_chan_spec *chan, unsigned int mode)
154 {
155 	struct ad5504_state *st = iio_priv(indio_dev);
156 
157 	st->pwr_down_mode = mode;
158 
159 	return 0;
160 }
161 
162 static const struct iio_enum ad5504_powerdown_mode_enum = {
163 	.items = ad5504_powerdown_modes,
164 	.num_items = ARRAY_SIZE(ad5504_powerdown_modes),
165 	.get = ad5504_get_powerdown_mode,
166 	.set = ad5504_set_powerdown_mode,
167 };
168 
169 static ssize_t ad5504_read_dac_powerdown(struct iio_dev *indio_dev,
170 	uintptr_t private, const struct iio_chan_spec *chan, char *buf)
171 {
172 	struct ad5504_state *st = iio_priv(indio_dev);
173 
174 	return sprintf(buf, "%d\n",
175 			!(st->pwr_down_mask & (1 << chan->channel)));
176 }
177 
178 static ssize_t ad5504_write_dac_powerdown(struct iio_dev *indio_dev,
179 	uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
180 	size_t len)
181 {
182 	bool pwr_down;
183 	int ret;
184 	struct ad5504_state *st = iio_priv(indio_dev);
185 
186 	ret = strtobool(buf, &pwr_down);
187 	if (ret)
188 		return ret;
189 
190 	if (pwr_down)
191 		st->pwr_down_mask |= (1 << chan->channel);
192 	else
193 		st->pwr_down_mask &= ~(1 << chan->channel);
194 
195 	ret = ad5504_spi_write(st, AD5504_ADDR_CTRL,
196 				AD5504_DAC_PWRDWN_MODE(st->pwr_down_mode) |
197 				AD5504_DAC_PWR(st->pwr_down_mask));
198 
199 	/* writes to the CTRL register must be followed by a NOOP */
200 	ad5504_spi_write(st, AD5504_ADDR_NOOP, 0);
201 
202 	return ret ? ret : len;
203 }
204 
205 static IIO_CONST_ATTR(temp0_thresh_rising_value, "110000");
206 static IIO_CONST_ATTR(temp0_thresh_rising_en, "1");
207 
208 static struct attribute *ad5504_ev_attributes[] = {
209 	&iio_const_attr_temp0_thresh_rising_value.dev_attr.attr,
210 	&iio_const_attr_temp0_thresh_rising_en.dev_attr.attr,
211 	NULL,
212 };
213 
214 static const struct attribute_group ad5504_ev_attribute_group = {
215 	.attrs = ad5504_ev_attributes,
216 };
217 
218 static irqreturn_t ad5504_event_handler(int irq, void *private)
219 {
220 	iio_push_event(private,
221 		       IIO_UNMOD_EVENT_CODE(IIO_TEMP,
222 					    0,
223 					    IIO_EV_TYPE_THRESH,
224 					    IIO_EV_DIR_RISING),
225 		       iio_get_time_ns(private));
226 
227 	return IRQ_HANDLED;
228 }
229 
230 static const struct iio_info ad5504_info = {
231 	.write_raw = ad5504_write_raw,
232 	.read_raw = ad5504_read_raw,
233 	.event_attrs = &ad5504_ev_attribute_group,
234 };
235 
236 static const struct iio_chan_spec_ext_info ad5504_ext_info[] = {
237 	{
238 		.name = "powerdown",
239 		.read = ad5504_read_dac_powerdown,
240 		.write = ad5504_write_dac_powerdown,
241 		.shared = IIO_SEPARATE,
242 	},
243 	IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
244 		 &ad5504_powerdown_mode_enum),
245 	IIO_ENUM_AVAILABLE("powerdown_mode", &ad5504_powerdown_mode_enum),
246 	{ },
247 };
248 
249 #define AD5504_CHANNEL(_chan) { \
250 	.type = IIO_VOLTAGE, \
251 	.indexed = 1, \
252 	.output = 1, \
253 	.channel = (_chan), \
254 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
255 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
256 	.address = AD5504_ADDR_DAC(_chan), \
257 	.scan_type = { \
258 		.sign = 'u', \
259 		.realbits = 12, \
260 		.storagebits = 16, \
261 	}, \
262 	.ext_info = ad5504_ext_info, \
263 }
264 
265 static const struct iio_chan_spec ad5504_channels[] = {
266 	AD5504_CHANNEL(0),
267 	AD5504_CHANNEL(1),
268 	AD5504_CHANNEL(2),
269 	AD5504_CHANNEL(3),
270 };
271 
272 static int ad5504_probe(struct spi_device *spi)
273 {
274 	struct ad5504_platform_data *pdata = spi->dev.platform_data;
275 	struct iio_dev *indio_dev;
276 	struct ad5504_state *st;
277 	struct regulator *reg;
278 	int ret, voltage_uv = 0;
279 
280 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
281 	if (!indio_dev)
282 		return -ENOMEM;
283 	reg = devm_regulator_get(&spi->dev, "vcc");
284 	if (!IS_ERR(reg)) {
285 		ret = regulator_enable(reg);
286 		if (ret)
287 			return ret;
288 
289 		ret = regulator_get_voltage(reg);
290 		if (ret < 0)
291 			goto error_disable_reg;
292 
293 		voltage_uv = ret;
294 	}
295 
296 	spi_set_drvdata(spi, indio_dev);
297 	st = iio_priv(indio_dev);
298 	if (voltage_uv)
299 		st->vref_mv = voltage_uv / 1000;
300 	else if (pdata)
301 		st->vref_mv = pdata->vref_mv;
302 	else
303 		dev_warn(&spi->dev, "reference voltage unspecified\n");
304 
305 	st->reg = reg;
306 	st->spi = spi;
307 	indio_dev->dev.parent = &spi->dev;
308 	indio_dev->name = spi_get_device_id(st->spi)->name;
309 	indio_dev->info = &ad5504_info;
310 	if (spi_get_device_id(st->spi)->driver_data == ID_AD5501)
311 		indio_dev->num_channels = 1;
312 	else
313 		indio_dev->num_channels = 4;
314 	indio_dev->channels = ad5504_channels;
315 	indio_dev->modes = INDIO_DIRECT_MODE;
316 
317 	if (spi->irq) {
318 		ret = devm_request_threaded_irq(&spi->dev, spi->irq,
319 					   NULL,
320 					   &ad5504_event_handler,
321 					   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
322 					   spi_get_device_id(st->spi)->name,
323 					   indio_dev);
324 		if (ret)
325 			goto error_disable_reg;
326 	}
327 
328 	ret = iio_device_register(indio_dev);
329 	if (ret)
330 		goto error_disable_reg;
331 
332 	return 0;
333 
334 error_disable_reg:
335 	if (!IS_ERR(reg))
336 		regulator_disable(reg);
337 
338 	return ret;
339 }
340 
341 static int ad5504_remove(struct spi_device *spi)
342 {
343 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
344 	struct ad5504_state *st = iio_priv(indio_dev);
345 
346 	iio_device_unregister(indio_dev);
347 
348 	if (!IS_ERR(st->reg))
349 		regulator_disable(st->reg);
350 
351 	return 0;
352 }
353 
354 static const struct spi_device_id ad5504_id[] = {
355 	{"ad5504", ID_AD5504},
356 	{"ad5501", ID_AD5501},
357 	{}
358 };
359 MODULE_DEVICE_TABLE(spi, ad5504_id);
360 
361 static struct spi_driver ad5504_driver = {
362 	.driver = {
363 		   .name = "ad5504",
364 		   },
365 	.probe = ad5504_probe,
366 	.remove = ad5504_remove,
367 	.id_table = ad5504_id,
368 };
369 module_spi_driver(ad5504_driver);
370 
371 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
372 MODULE_DESCRIPTION("Analog Devices AD5501/AD5501 DAC");
373 MODULE_LICENSE("GPL v2");
374