blob: 5489ec43b95d7c3acabcc2416c742b64edf92469 [file] [log] [blame]
Barry Song6790e292010-10-27 21:44:07 -04001/*
2 * AD5624R, AD5644R, AD5664R Digital to analog convertors spi driver
3 *
Michael Hennerich14f88f12011-03-09 16:01:45 +01004 * Copyright 2010-2011 Analog Devices Inc.
Barry Song6790e292010-10-27 21:44:07 -04005 *
Michael Hennerich14f88f12011-03-09 16:01:45 +01006 * Licensed under the GPL-2.
Barry Song6790e292010-10-27 21:44:07 -04007 */
8
9#include <linux/interrupt.h>
Barry Song6790e292010-10-27 21:44:07 -040010#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>
Michael Hennerich14f88f12011-03-09 16:01:45 +010016#include <linux/regulator/consumer.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040017#include <linux/module.h>
Barry Song6790e292010-10-27 21:44:07 -040018
Jonathan Cameron06458e22012-04-25 15:54:58 +010019#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
Lars-Peter Clausencadb6952012-06-04 11:36:19 +020021
Barry Song6790e292010-10-27 21:44:07 -040022#include "ad5624r.h"
23
Michael Hennerichdf9cd102010-11-19 15:16:44 +010024static int ad5624r_spi_write(struct spi_device *spi,
JM Friedtadfa9692015-06-19 14:48:06 +020025 u8 cmd, u8 addr, u16 val, u8 shift)
Barry Song6790e292010-10-27 21:44:07 -040026{
Barry Song6790e292010-10-27 21:44:07 -040027 u32 data;
28 u8 msg[3];
29
30 /*
Michael Hennerich14f88f12011-03-09 16:01:45 +010031 * The input shift register is 24 bits wide. The first two bits are
32 * don't care bits. The next three are the command bits, C2 to C0,
33 * followed by the 3-bit DAC address, A2 to A0, and then the
34 * 16-, 14-, 12-bit data-word. The data-word comprises the 16-,
35 * 14-, 12-bit input code followed by 0, 2, or 4 don't care bits,
36 * for the AD5664R, AD5644R, and AD5624R, respectively.
Barry Song6790e292010-10-27 21:44:07 -040037 */
JM Friedtadfa9692015-06-19 14:48:06 +020038 data = (0 << 22) | (cmd << 19) | (addr << 16) | (val << shift);
Barry Song6790e292010-10-27 21:44:07 -040039 msg[0] = data >> 16;
40 msg[1] = data >> 8;
41 msg[2] = data;
42
Michael Hennerichdf9cd102010-11-19 15:16:44 +010043 return spi_write(spi, msg, 3);
Barry Song6790e292010-10-27 21:44:07 -040044}
45
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +010046static int ad5624r_read_raw(struct iio_dev *indio_dev,
47 struct iio_chan_spec const *chan,
48 int *val,
49 int *val2,
50 long m)
Barry Song6790e292010-10-27 21:44:07 -040051{
Jonathan Cameron3ff24202011-06-27 13:07:31 +010052 struct ad5624r_state *st = iio_priv(indio_dev);
Barry Song6790e292010-10-27 21:44:07 -040053
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +010054 switch (m) {
55 case IIO_CHAN_INFO_SCALE:
Lars-Peter Clausen14229e82013-09-28 10:31:00 +010056 *val = st->vref_mv;
57 *val2 = chan->scan_type.realbits;
58 return IIO_VAL_FRACTIONAL_LOG2;
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +010059 }
60 return -EINVAL;
61}
62
63static int ad5624r_write_raw(struct iio_dev *indio_dev,
64 struct iio_chan_spec const *chan,
65 int val,
66 int val2,
67 long mask)
68{
69 struct ad5624r_state *st = iio_priv(indio_dev);
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +010070
71 switch (mask) {
Jonathan Cameron09f4eb42012-04-15 17:41:19 +010072 case IIO_CHAN_INFO_RAW:
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +010073 if (val >= (1 << chan->scan_type.realbits) || val < 0)
74 return -EINVAL;
75
76 return ad5624r_spi_write(st->us,
77 AD5624R_CMD_WRITE_INPUT_N_UPDATE_N,
78 chan->address, val,
79 chan->scan_type.shift);
80 default:
Sachin Kamat15255c62014-03-07 07:38:00 +000081 return -EINVAL;
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +010082 }
Barry Song6790e292010-10-27 21:44:07 -040083}
84
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +020085static const char * const ad5624r_powerdown_modes[] = {
86 "1kohm_to_gnd",
87 "100kohm_to_gnd",
88 "three_state"
89};
90
91static int ad5624r_get_powerdown_mode(struct iio_dev *indio_dev,
92 const struct iio_chan_spec *chan)
Barry Song6790e292010-10-27 21:44:07 -040093{
Jonathan Cameron3ff24202011-06-27 13:07:31 +010094 struct ad5624r_state *st = iio_priv(indio_dev);
Barry Song6790e292010-10-27 21:44:07 -040095
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +020096 return st->pwr_down_mode;
Barry Song6790e292010-10-27 21:44:07 -040097}
98
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +020099static int ad5624r_set_powerdown_mode(struct iio_dev *indio_dev,
100 const struct iio_chan_spec *chan, unsigned int mode)
Barry Song6790e292010-10-27 21:44:07 -0400101{
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100102 struct ad5624r_state *st = iio_priv(indio_dev);
Barry Song6790e292010-10-27 21:44:07 -0400103
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200104 st->pwr_down_mode = mode;
Barry Song6790e292010-10-27 21:44:07 -0400105
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200106 return 0;
Barry Song6790e292010-10-27 21:44:07 -0400107}
108
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200109static const struct iio_enum ad5624r_powerdown_mode_enum = {
110 .items = ad5624r_powerdown_modes,
111 .num_items = ARRAY_SIZE(ad5624r_powerdown_modes),
112 .get = ad5624r_get_powerdown_mode,
113 .set = ad5624r_set_powerdown_mode,
114};
115
116static ssize_t ad5624r_read_dac_powerdown(struct iio_dev *indio_dev,
117 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
Barry Song6790e292010-10-27 21:44:07 -0400118{
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100119 struct ad5624r_state *st = iio_priv(indio_dev);
Barry Song6790e292010-10-27 21:44:07 -0400120
Michael Hennerich14f88f12011-03-09 16:01:45 +0100121 return sprintf(buf, "%d\n",
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200122 !!(st->pwr_down_mask & (1 << chan->channel)));
Barry Song6790e292010-10-27 21:44:07 -0400123}
124
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200125static ssize_t ad5624r_write_dac_powerdown(struct iio_dev *indio_dev,
126 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
127 size_t len)
Barry Song6790e292010-10-27 21:44:07 -0400128{
Lars-Peter Clausen24905942012-06-04 11:36:23 +0200129 bool pwr_down;
Barry Song6790e292010-10-27 21:44:07 -0400130 int ret;
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100131 struct ad5624r_state *st = iio_priv(indio_dev);
Barry Song6790e292010-10-27 21:44:07 -0400132
Lars-Peter Clausen24905942012-06-04 11:36:23 +0200133 ret = strtobool(buf, &pwr_down);
Barry Song6790e292010-10-27 21:44:07 -0400134 if (ret)
135 return ret;
136
Lars-Peter Clausen24905942012-06-04 11:36:23 +0200137 if (pwr_down)
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200138 st->pwr_down_mask |= (1 << chan->channel);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100139 else
Lars-Peter Clausen24905942012-06-04 11:36:23 +0200140 st->pwr_down_mask &= ~(1 << chan->channel);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100141
Michael Hennerichdf9cd102010-11-19 15:16:44 +0100142 ret = ad5624r_spi_write(st->us, AD5624R_CMD_POWERDOWN_DAC, 0,
Michael Hennerich14f88f12011-03-09 16:01:45 +0100143 (st->pwr_down_mode << 4) |
144 st->pwr_down_mask, 16);
Barry Song6790e292010-10-27 21:44:07 -0400145
146 return ret ? ret : len;
147}
148
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100149static const struct iio_info ad5624r_info = {
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +0100150 .write_raw = ad5624r_write_raw,
151 .read_raw = ad5624r_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100152 .driver_module = THIS_MODULE,
153};
154
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200155static const struct iio_chan_spec_ext_info ad5624r_ext_info[] = {
156 {
157 .name = "powerdown",
158 .read = ad5624r_read_dac_powerdown,
159 .write = ad5624r_write_dac_powerdown,
Jonathan Cameron37044322013-09-08 14:57:00 +0100160 .shared = IIO_SEPARATE,
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200161 },
Jonathan Cameron37044322013-09-08 14:57:00 +0100162 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
163 &ad5624r_powerdown_mode_enum),
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200164 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5624r_powerdown_mode_enum),
165 { },
166};
167
168#define AD5624R_CHANNEL(_chan, _bits) { \
169 .type = IIO_VOLTAGE, \
170 .indexed = 1, \
171 .output = 1, \
172 .channel = (_chan), \
Jonathan Cameron8b686342013-02-27 19:11:22 +0000173 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
174 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200175 .address = (_chan), \
Jonathan Cameron49746002013-12-11 18:45:00 +0000176 .scan_type = { \
177 .sign = 'u', \
178 .realbits = (_bits), \
179 .storagebits = 16, \
180 .shift = 16 - (_bits), \
181 }, \
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200182 .ext_info = ad5624r_ext_info, \
183}
184
185#define DECLARE_AD5624R_CHANNELS(_name, _bits) \
186 const struct iio_chan_spec _name##_channels[] = { \
187 AD5624R_CHANNEL(0, _bits), \
188 AD5624R_CHANNEL(1, _bits), \
189 AD5624R_CHANNEL(2, _bits), \
190 AD5624R_CHANNEL(3, _bits), \
191}
192
193static DECLARE_AD5624R_CHANNELS(ad5624r, 12);
194static DECLARE_AD5624R_CHANNELS(ad5644r, 14);
195static DECLARE_AD5624R_CHANNELS(ad5664r, 16);
196
197static const struct ad5624r_chip_info ad5624r_chip_info_tbl[] = {
198 [ID_AD5624R3] = {
199 .channels = ad5624r_channels,
200 .int_vref_mv = 1250,
201 },
202 [ID_AD5624R5] = {
203 .channels = ad5624r_channels,
204 .int_vref_mv = 2500,
205 },
206 [ID_AD5644R3] = {
207 .channels = ad5644r_channels,
208 .int_vref_mv = 1250,
209 },
210 [ID_AD5644R5] = {
211 .channels = ad5644r_channels,
212 .int_vref_mv = 2500,
213 },
214 [ID_AD5664R3] = {
215 .channels = ad5664r_channels,
216 .int_vref_mv = 1250,
217 },
218 [ID_AD5664R5] = {
219 .channels = ad5664r_channels,
220 .int_vref_mv = 2500,
221 },
222};
223
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800224static int ad5624r_probe(struct spi_device *spi)
Barry Song6790e292010-10-27 21:44:07 -0400225{
Barry Song6790e292010-10-27 21:44:07 -0400226 struct ad5624r_state *st;
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100227 struct iio_dev *indio_dev;
Michael Hennerich14f88f12011-03-09 16:01:45 +0100228 int ret, voltage_uv = 0;
Barry Song6790e292010-10-27 21:44:07 -0400229
Sachin Kamat75238232013-08-19 12:38:00 +0100230 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
231 if (!indio_dev)
232 return -ENOMEM;
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100233 st = iio_priv(indio_dev);
Sachin Kamat75238232013-08-19 12:38:00 +0100234 st->reg = devm_regulator_get(&spi->dev, "vcc");
Jonathan Cameron26a54792011-08-30 12:41:19 +0100235 if (!IS_ERR(st->reg)) {
236 ret = regulator_enable(st->reg);
237 if (ret)
Sachin Kamat75238232013-08-19 12:38:00 +0100238 return ret;
Jonathan Cameron26a54792011-08-30 12:41:19 +0100239
Axel Lin8434e782012-12-14 12:07:00 +0000240 ret = regulator_get_voltage(st->reg);
241 if (ret < 0)
242 goto error_disable_reg;
243
244 voltage_uv = ret;
Jonathan Cameron26a54792011-08-30 12:41:19 +0100245 }
246
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100247 spi_set_drvdata(spi, indio_dev);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100248 st->chip_info =
249 &ad5624r_chip_info_tbl[spi_get_device_id(spi)->driver_data];
250
251 if (voltage_uv)
252 st->vref_mv = voltage_uv / 1000;
253 else
254 st->vref_mv = st->chip_info->int_vref_mv;
Barry Song6790e292010-10-27 21:44:07 -0400255
256 st->us = spi;
Barry Song6790e292010-10-27 21:44:07 -0400257
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100258 indio_dev->dev.parent = &spi->dev;
259 indio_dev->name = spi_get_device_id(spi)->name;
260 indio_dev->info = &ad5624r_info;
261 indio_dev->modes = INDIO_DIRECT_MODE;
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +0100262 indio_dev->channels = st->chip_info->channels;
263 indio_dev->num_channels = AD5624R_DAC_CHANNELS;
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100264
Jonathan Cameron3e394402011-08-30 12:41:18 +0100265 ret = ad5624r_spi_write(spi, AD5624R_CMD_INTERNAL_REFER_SETUP, 0,
266 !!voltage_uv, 16);
Barry Song6790e292010-10-27 21:44:07 -0400267 if (ret)
Jonathan Cameron26a54792011-08-30 12:41:19 +0100268 goto error_disable_reg;
Barry Song6790e292010-10-27 21:44:07 -0400269
Jonathan Cameron3e394402011-08-30 12:41:18 +0100270 ret = iio_device_register(indio_dev);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100271 if (ret)
Jonathan Cameron26a54792011-08-30 12:41:19 +0100272 goto error_disable_reg;
Barry Song6790e292010-10-27 21:44:07 -0400273
274 return 0;
275
Michael Hennerich14f88f12011-03-09 16:01:45 +0100276error_disable_reg:
Jonathan Cameron26a54792011-08-30 12:41:19 +0100277 if (!IS_ERR(st->reg))
278 regulator_disable(st->reg);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100279
Barry Song6790e292010-10-27 21:44:07 -0400280 return ret;
281}
282
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800283static int ad5624r_remove(struct spi_device *spi)
Barry Song6790e292010-10-27 21:44:07 -0400284{
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100285 struct iio_dev *indio_dev = spi_get_drvdata(spi);
286 struct ad5624r_state *st = iio_priv(indio_dev);
Barry Song6790e292010-10-27 21:44:07 -0400287
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100288 iio_device_unregister(indio_dev);
Sachin Kamat75238232013-08-19 12:38:00 +0100289 if (!IS_ERR(st->reg))
Jonathan Cameron26a54792011-08-30 12:41:19 +0100290 regulator_disable(st->reg);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100291
Barry Song6790e292010-10-27 21:44:07 -0400292 return 0;
293}
294
Michael Hennerichece30c12010-11-19 15:16:45 +0100295static const struct spi_device_id ad5624r_id[] = {
Michael Hennerich14f88f12011-03-09 16:01:45 +0100296 {"ad5624r3", ID_AD5624R3},
297 {"ad5644r3", ID_AD5644R3},
298 {"ad5664r3", ID_AD5664R3},
299 {"ad5624r5", ID_AD5624R5},
300 {"ad5644r5", ID_AD5644R5},
301 {"ad5664r5", ID_AD5664R5},
Michael Hennerichece30c12010-11-19 15:16:45 +0100302 {}
303};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100304MODULE_DEVICE_TABLE(spi, ad5624r_id);
Michael Hennerichece30c12010-11-19 15:16:45 +0100305
Barry Song6790e292010-10-27 21:44:07 -0400306static struct spi_driver ad5624r_driver = {
307 .driver = {
Michael Hennerich24d60502010-11-19 15:16:46 +0100308 .name = "ad5624r",
Michael Hennerich24d60502010-11-19 15:16:46 +0100309 },
Barry Song6790e292010-10-27 21:44:07 -0400310 .probe = ad5624r_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800311 .remove = ad5624r_remove,
Michael Hennerichece30c12010-11-19 15:16:45 +0100312 .id_table = ad5624r_id,
Barry Song6790e292010-10-27 21:44:07 -0400313};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100314module_spi_driver(ad5624r_driver);
Barry Song6790e292010-10-27 21:44:07 -0400315
316MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
317MODULE_DESCRIPTION("Analog Devices AD5624/44/64R DAC spi driver");
318MODULE_LICENSE("GPL v2");