blob: d44c8f5fadeea8e1ebafb123c9232c3e8d244a4b [file] [log] [blame]
Michael Hennerich59c23ea2011-04-04 15:39:15 +02001/*
2 * AD5504, AD5501 High Voltage Digital to Analog Converter
3 *
4 * Copyright 2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2.
7 */
8
9#include <linux/interrupt.h>
Michael Hennerich59c23ea2011-04-04 15:39:15 +020010#include <linux/fs.h>
11#include <linux/device.h>
12#include <linux/kernel.h>
13#include <linux/spi/spi.h>
14#include <linux/slab.h>
15#include <linux/sysfs.h>
16#include <linux/regulator/consumer.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040017#include <linux/module.h>
Michael Hennerich59c23ea2011-04-04 15:39:15 +020018
Jonathan Cameron06458e22012-04-25 15:54:58 +010019#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
21#include <linux/iio/events.h>
Lars-Peter Clausendbdc0252012-06-04 11:36:28 +020022#include <linux/iio/dac/ad5504.h>
Michael Hennerich59c23ea2011-04-04 15:39:15 +020023
Lars-Peter Clausena98348b2012-06-04 11:36:26 +020024#define AD5505_BITS 12
25#define AD5504_RES_MASK ((1 << (AD5505_BITS)) - 1)
26
27#define AD5504_CMD_READ (1 << 15)
28#define AD5504_CMD_WRITE (0 << 15)
29#define AD5504_ADDR(addr) ((addr) << 12)
30
31/* Registers */
32#define AD5504_ADDR_NOOP 0
33#define AD5504_ADDR_DAC(x) ((x) + 1)
34#define AD5504_ADDR_ALL_DAC 5
35#define AD5504_ADDR_CTRL 7
36
37/* Control Register */
38#define AD5504_DAC_PWR(ch) ((ch) << 2)
39#define AD5504_DAC_PWRDWN_MODE(mode) ((mode) << 6)
40#define AD5504_DAC_PWRDN_20K 0
41#define AD5504_DAC_PWRDN_3STATE 1
42
43/**
44 * struct ad5446_state - driver instance specific data
45 * @us: spi_device
46 * @reg: supply regulator
47 * @vref_mv: actual reference voltage used
48 * @pwr_down_mask power down mask
49 * @pwr_down_mode current power down mode
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +000050 * @data: transfer buffer
Lars-Peter Clausena98348b2012-06-04 11:36:26 +020051 */
Lars-Peter Clausena98348b2012-06-04 11:36:26 +020052struct ad5504_state {
53 struct spi_device *spi;
54 struct regulator *reg;
55 unsigned short vref_mv;
56 unsigned pwr_down_mask;
57 unsigned pwr_down_mode;
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +000058
59 __be16 data[2] ____cacheline_aligned;
Lars-Peter Clausena98348b2012-06-04 11:36:26 +020060};
61
62/**
63 * ad5504_supported_device_ids:
64 */
65
66enum ad5504_supported_device_ids {
67 ID_AD5504,
68 ID_AD5501,
69};
70
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +000071static int ad5504_spi_write(struct ad5504_state *st, u8 addr, u16 val)
Michael Hennerich59c23ea2011-04-04 15:39:15 +020072{
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +000073 st->data[0] = cpu_to_be16(AD5504_CMD_WRITE | AD5504_ADDR(addr) |
Michael Hennerich59c23ea2011-04-04 15:39:15 +020074 (val & AD5504_RES_MASK));
75
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +000076 return spi_write(st->spi, &st->data[0], 2);
Michael Hennerich59c23ea2011-04-04 15:39:15 +020077}
78
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +000079static int ad5504_spi_read(struct ad5504_state *st, u8 addr)
Michael Hennerich59c23ea2011-04-04 15:39:15 +020080{
Michael Hennerich59c23ea2011-04-04 15:39:15 +020081 int ret;
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +000082 struct spi_transfer t = {
83 .tx_buf = &st->data[0],
84 .rx_buf = &st->data[1],
85 .len = 2,
86 };
Michael Hennerich59c23ea2011-04-04 15:39:15 +020087
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +000088 st->data[0] = cpu_to_be16(AD5504_CMD_READ | AD5504_ADDR(addr));
89 ret = spi_sync_transfer(st->spi, &t, 1);
Lars-Peter Clausena7b15282011-11-15 16:31:23 +010090 if (ret < 0)
Michael Hennerich59c23ea2011-04-04 15:39:15 +020091 return ret;
92
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +000093 return be16_to_cpu(st->data[1]) & AD5504_RES_MASK;
Michael Hennerich59c23ea2011-04-04 15:39:15 +020094}
95
Lars-Peter Clausena7b15282011-11-15 16:31:23 +010096static int ad5504_read_raw(struct iio_dev *indio_dev,
97 struct iio_chan_spec const *chan,
98 int *val,
99 int *val2,
100 long m)
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200101{
Jonathan Camerona3684de2011-06-27 13:07:32 +0100102 struct ad5504_state *st = iio_priv(indio_dev);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200103 int ret;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200104
Lars-Peter Clausena7b15282011-11-15 16:31:23 +0100105 switch (m) {
Jonathan Cameron09f4eb42012-04-15 17:41:19 +0100106 case IIO_CHAN_INFO_RAW:
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +0000107 ret = ad5504_spi_read(st, chan->address);
Lars-Peter Clausena7b15282011-11-15 16:31:23 +0100108 if (ret < 0)
109 return ret;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200110
Lars-Peter Clausena7b15282011-11-15 16:31:23 +0100111 *val = ret;
112
113 return IIO_VAL_INT;
114 case IIO_CHAN_INFO_SCALE:
Lars-Peter Clausen54ea1432013-09-28 10:31:00 +0100115 *val = st->vref_mv;
116 *val2 = chan->scan_type.realbits;
117 return IIO_VAL_FRACTIONAL_LOG2;
Lars-Peter Clausena7b15282011-11-15 16:31:23 +0100118 }
119 return -EINVAL;
120}
121
122static int ad5504_write_raw(struct iio_dev *indio_dev,
123 struct iio_chan_spec const *chan,
124 int val,
125 int val2,
126 long mask)
127{
128 struct ad5504_state *st = iio_priv(indio_dev);
129 int ret;
130
131 switch (mask) {
Jonathan Cameron09f4eb42012-04-15 17:41:19 +0100132 case IIO_CHAN_INFO_RAW:
Lars-Peter Clausena7b15282011-11-15 16:31:23 +0100133 if (val >= (1 << chan->scan_type.realbits) || val < 0)
134 return -EINVAL;
135
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +0000136 return ad5504_spi_write(st, chan->address, val);
Lars-Peter Clausena7b15282011-11-15 16:31:23 +0100137 default:
138 ret = -EINVAL;
139 }
140
141 return -EINVAL;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200142}
143
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200144static const char * const ad5504_powerdown_modes[] = {
145 "20kohm_to_gnd",
146 "three_state",
147};
148
149static int ad5504_get_powerdown_mode(struct iio_dev *indio_dev,
150 const struct iio_chan_spec *chan)
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200151{
Jonathan Camerona3684de2011-06-27 13:07:32 +0100152 struct ad5504_state *st = iio_priv(indio_dev);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200153
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200154 return st->pwr_down_mode;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200155}
156
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200157static int ad5504_set_powerdown_mode(struct iio_dev *indio_dev,
158 const struct iio_chan_spec *chan, unsigned int mode)
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200159{
Jonathan Camerona3684de2011-06-27 13:07:32 +0100160 struct ad5504_state *st = iio_priv(indio_dev);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200161
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200162 st->pwr_down_mode = mode;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200163
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200164 return 0;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200165}
166
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200167static const struct iio_enum ad5504_powerdown_mode_enum = {
168 .items = ad5504_powerdown_modes,
169 .num_items = ARRAY_SIZE(ad5504_powerdown_modes),
170 .get = ad5504_get_powerdown_mode,
171 .set = ad5504_set_powerdown_mode,
172};
173
174static ssize_t ad5504_read_dac_powerdown(struct iio_dev *indio_dev,
175 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200176{
Jonathan Camerona3684de2011-06-27 13:07:32 +0100177 struct ad5504_state *st = iio_priv(indio_dev);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200178
179 return sprintf(buf, "%d\n",
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200180 !(st->pwr_down_mask & (1 << chan->channel)));
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200181}
182
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200183static ssize_t ad5504_write_dac_powerdown(struct iio_dev *indio_dev,
184 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
185 size_t len)
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200186{
Lars-Peter Clausen3bbbf152012-06-04 11:36:22 +0200187 bool pwr_down;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200188 int ret;
Jonathan Camerona3684de2011-06-27 13:07:32 +0100189 struct ad5504_state *st = iio_priv(indio_dev);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200190
Lars-Peter Clausen3bbbf152012-06-04 11:36:22 +0200191 ret = strtobool(buf, &pwr_down);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200192 if (ret)
193 return ret;
194
Lars-Peter Clausen3bbbf152012-06-04 11:36:22 +0200195 if (pwr_down)
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200196 st->pwr_down_mask |= (1 << chan->channel);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200197 else
Lars-Peter Clausen3bbbf152012-06-04 11:36:22 +0200198 st->pwr_down_mask &= ~(1 << chan->channel);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200199
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +0000200 ret = ad5504_spi_write(st, AD5504_ADDR_CTRL,
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200201 AD5504_DAC_PWRDWN_MODE(st->pwr_down_mode) |
202 AD5504_DAC_PWR(st->pwr_down_mask));
203
204 /* writes to the CTRL register must be followed by a NOOP */
Lars-Peter Clausen0dbe59c2013-11-25 12:41:00 +0000205 ad5504_spi_write(st, AD5504_ADDR_NOOP, 0);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200206
207 return ret ? ret : len;
208}
209
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200210static IIO_CONST_ATTR(temp0_thresh_rising_value, "110000");
211static IIO_CONST_ATTR(temp0_thresh_rising_en, "1");
212
213static struct attribute *ad5504_ev_attributes[] = {
214 &iio_const_attr_temp0_thresh_rising_value.dev_attr.attr,
215 &iio_const_attr_temp0_thresh_rising_en.dev_attr.attr,
216 NULL,
217};
218
219static struct attribute_group ad5504_ev_attribute_group = {
220 .attrs = ad5504_ev_attributes,
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100221 .name = "events",
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200222};
223
Jonathan Cameronce298d42011-05-18 14:41:04 +0100224static irqreturn_t ad5504_event_handler(int irq, void *private)
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200225{
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100226 iio_push_event(private,
Jonathan Cameronc4b14d92011-08-12 17:56:04 +0100227 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
Jonathan Cameronce298d42011-05-18 14:41:04 +0100228 0,
229 IIO_EV_TYPE_THRESH,
230 IIO_EV_DIR_RISING),
231 iio_get_time_ns());
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200232
Jonathan Cameronce298d42011-05-18 14:41:04 +0100233 return IRQ_HANDLED;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200234}
235
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100236static const struct iio_info ad5504_info = {
Lars-Peter Clausena7b15282011-11-15 16:31:23 +0100237 .write_raw = ad5504_write_raw,
238 .read_raw = ad5504_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100239 .event_attrs = &ad5504_ev_attribute_group,
240 .driver_module = THIS_MODULE,
241};
242
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200243static const struct iio_chan_spec_ext_info ad5504_ext_info[] = {
244 {
245 .name = "powerdown",
246 .read = ad5504_read_dac_powerdown,
247 .write = ad5504_write_dac_powerdown,
Jonathan Cameron37044322013-09-08 14:57:00 +0100248 .shared = IIO_SEPARATE,
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200249 },
Jonathan Cameron37044322013-09-08 14:57:00 +0100250 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
251 &ad5504_powerdown_mode_enum),
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200252 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5504_powerdown_mode_enum),
253 { },
254};
255
256#define AD5504_CHANNEL(_chan) { \
257 .type = IIO_VOLTAGE, \
258 .indexed = 1, \
259 .output = 1, \
260 .channel = (_chan), \
Jonathan Cameronf3ec5a22013-02-27 19:11:04 +0000261 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
262 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200263 .address = AD5504_ADDR_DAC(_chan), \
264 .scan_type = IIO_ST('u', 12, 16, 0), \
265 .ext_info = ad5504_ext_info, \
266}
267
268static const struct iio_chan_spec ad5504_channels[] = {
269 AD5504_CHANNEL(0),
270 AD5504_CHANNEL(1),
271 AD5504_CHANNEL(2),
272 AD5504_CHANNEL(3),
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100273};
274
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800275static int ad5504_probe(struct spi_device *spi)
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200276{
277 struct ad5504_platform_data *pdata = spi->dev.platform_data;
Jonathan Camerona3684de2011-06-27 13:07:32 +0100278 struct iio_dev *indio_dev;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200279 struct ad5504_state *st;
Jonathan Camerona3684de2011-06-27 13:07:32 +0100280 struct regulator *reg;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200281 int ret, voltage_uv = 0;
282
Sachin Kamat8571ebf2013-08-19 12:38:00 +0100283 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
284 if (!indio_dev)
285 return -ENOMEM;
286 reg = devm_regulator_get(&spi->dev, "vcc");
Jonathan Camerona3684de2011-06-27 13:07:32 +0100287 if (!IS_ERR(reg)) {
288 ret = regulator_enable(reg);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200289 if (ret)
Sachin Kamat8571ebf2013-08-19 12:38:00 +0100290 return ret;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200291
Axel Lin0ce2fda2012-12-14 12:06:00 +0000292 ret = regulator_get_voltage(reg);
293 if (ret < 0)
294 goto error_disable_reg;
295
296 voltage_uv = ret;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200297 }
298
Jonathan Camerona3684de2011-06-27 13:07:32 +0100299 spi_set_drvdata(spi, indio_dev);
300 st = iio_priv(indio_dev);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200301 if (voltage_uv)
302 st->vref_mv = voltage_uv / 1000;
303 else if (pdata)
304 st->vref_mv = pdata->vref_mv;
305 else
306 dev_warn(&spi->dev, "reference voltage unspecified\n");
307
Jonathan Camerona3684de2011-06-27 13:07:32 +0100308 st->reg = reg;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200309 st->spi = spi;
Jonathan Camerona3684de2011-06-27 13:07:32 +0100310 indio_dev->dev.parent = &spi->dev;
311 indio_dev->name = spi_get_device_id(st->spi)->name;
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200312 indio_dev->info = &ad5504_info;
313 if (spi_get_device_id(st->spi)->driver_data == ID_AD5501)
Lars-Peter Clausena7b15282011-11-15 16:31:23 +0100314 indio_dev->num_channels = 1;
Lars-Peter Clausen8d05d772012-06-04 11:36:15 +0200315 else
Lars-Peter Clausena7b15282011-11-15 16:31:23 +0100316 indio_dev->num_channels = 4;
Lars-Peter Clausena7b15282011-11-15 16:31:23 +0100317 indio_dev->channels = ad5504_channels;
Jonathan Camerona3684de2011-06-27 13:07:32 +0100318 indio_dev->modes = INDIO_DIRECT_MODE;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200319
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200320 if (spi->irq) {
Sachin Kamat8571ebf2013-08-19 12:38:00 +0100321 ret = devm_request_threaded_irq(&spi->dev, spi->irq,
Jonathan Cameronce298d42011-05-18 14:41:04 +0100322 NULL,
323 &ad5504_event_handler,
324 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
325 spi_get_device_id(st->spi)->name,
Jonathan Camerona3684de2011-06-27 13:07:32 +0100326 indio_dev);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200327 if (ret)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100328 goto error_disable_reg;
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200329 }
330
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100331 ret = iio_device_register(indio_dev);
332 if (ret)
Sachin Kamat8571ebf2013-08-19 12:38:00 +0100333 goto error_disable_reg;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100334
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200335 return 0;
336
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200337error_disable_reg:
Jonathan Camerona3684de2011-06-27 13:07:32 +0100338 if (!IS_ERR(reg))
Jonathan Cameron0cbb2b52011-08-12 16:55:33 +0100339 regulator_disable(reg);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200340
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200341 return ret;
342}
343
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800344static int ad5504_remove(struct spi_device *spi)
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200345{
Jonathan Camerona3684de2011-06-27 13:07:32 +0100346 struct iio_dev *indio_dev = spi_get_drvdata(spi);
347 struct ad5504_state *st = iio_priv(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100348
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100349 iio_device_unregister(indio_dev);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200350
Sachin Kamat8571ebf2013-08-19 12:38:00 +0100351 if (!IS_ERR(st->reg))
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100352 regulator_disable(st->reg);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100353
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200354 return 0;
355}
356
357static const struct spi_device_id ad5504_id[] = {
358 {"ad5504", ID_AD5504},
359 {"ad5501", ID_AD5501},
360 {}
361};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100362MODULE_DEVICE_TABLE(spi, ad5504_id);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200363
364static struct spi_driver ad5504_driver = {
365 .driver = {
366 .name = "ad5504",
367 .owner = THIS_MODULE,
368 },
369 .probe = ad5504_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800370 .remove = ad5504_remove,
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200371 .id_table = ad5504_id,
372};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100373module_spi_driver(ad5504_driver);
Michael Hennerich59c23ea2011-04-04 15:39:15 +0200374
375MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
376MODULE_DESCRIPTION("Analog Devices AD5501/AD5501 DAC");
377MODULE_LICENSE("GPL v2");