iio: refactor info mask and ext_info attribute creation.
[linux-2.6-block.git] / drivers / iio / dac / ad5624r_spi.c
CommitLineData
6790e29f
BS
1/*
2 * AD5624R, AD5644R, AD5664R Digital to analog convertors spi driver
3 *
14f88f1b 4 * Copyright 2010-2011 Analog Devices Inc.
6790e29f 5 *
14f88f1b 6 * Licensed under the GPL-2.
6790e29f
BS
7 */
8
9#include <linux/interrupt.h>
6790e29f
BS
10#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>
14f88f1b 16#include <linux/regulator/consumer.h>
99c97852 17#include <linux/module.h>
6790e29f 18
06458e27
JC
19#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
cadb6951 21
6790e29f
BS
22#include "ad5624r.h"
23
df9cd105
MH
24static int ad5624r_spi_write(struct spi_device *spi,
25 u8 cmd, u8 addr, u16 val, u8 len)
6790e29f 26{
6790e29f
BS
27 u32 data;
28 u8 msg[3];
29
30 /*
14f88f1b
MH
31 * 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.
6790e29f
BS
37 */
38 data = (0 << 22) | (cmd << 19) | (addr << 16) | (val << (16 - len));
39 msg[0] = data >> 16;
40 msg[1] = data >> 8;
41 msg[2] = data;
42
df9cd105 43 return spi_write(spi, msg, 3);
6790e29f
BS
44}
45
275de9f7
LPC
46static 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)
6790e29f 51{
3ff24205 52 struct ad5624r_state *st = iio_priv(indio_dev);
275de9f7 53 unsigned long scale_uv;
6790e29f 54
275de9f7
LPC
55 switch (m) {
56 case IIO_CHAN_INFO_SCALE:
57 scale_uv = (st->vref_mv * 1000) >> chan->scan_type.realbits;
58 *val = scale_uv / 1000;
59 *val2 = (scale_uv % 1000) * 1000;
60 return IIO_VAL_INT_PLUS_MICRO;
6790e29f 61
275de9f7
LPC
62 }
63 return -EINVAL;
64}
65
66static int ad5624r_write_raw(struct iio_dev *indio_dev,
67 struct iio_chan_spec const *chan,
68 int val,
69 int val2,
70 long mask)
71{
72 struct ad5624r_state *st = iio_priv(indio_dev);
73 int ret;
74
75 switch (mask) {
09f4eb40 76 case IIO_CHAN_INFO_RAW:
275de9f7
LPC
77 if (val >= (1 << chan->scan_type.realbits) || val < 0)
78 return -EINVAL;
79
80 return ad5624r_spi_write(st->us,
81 AD5624R_CMD_WRITE_INPUT_N_UPDATE_N,
82 chan->address, val,
83 chan->scan_type.shift);
84 default:
85 ret = -EINVAL;
86 }
87
88 return -EINVAL;
6790e29f
BS
89}
90
66eb9050
LPC
91static const char * const ad5624r_powerdown_modes[] = {
92 "1kohm_to_gnd",
93 "100kohm_to_gnd",
94 "three_state"
95};
96
97static int ad5624r_get_powerdown_mode(struct iio_dev *indio_dev,
98 const struct iio_chan_spec *chan)
6790e29f 99{
3ff24205 100 struct ad5624r_state *st = iio_priv(indio_dev);
6790e29f 101
66eb9050 102 return st->pwr_down_mode;
6790e29f
BS
103}
104
66eb9050
LPC
105static int ad5624r_set_powerdown_mode(struct iio_dev *indio_dev,
106 const struct iio_chan_spec *chan, unsigned int mode)
6790e29f 107{
3ff24205 108 struct ad5624r_state *st = iio_priv(indio_dev);
6790e29f 109
66eb9050 110 st->pwr_down_mode = mode;
6790e29f 111
66eb9050 112 return 0;
6790e29f
BS
113}
114
66eb9050
LPC
115static const struct iio_enum ad5624r_powerdown_mode_enum = {
116 .items = ad5624r_powerdown_modes,
117 .num_items = ARRAY_SIZE(ad5624r_powerdown_modes),
118 .get = ad5624r_get_powerdown_mode,
119 .set = ad5624r_set_powerdown_mode,
120};
121
122static ssize_t ad5624r_read_dac_powerdown(struct iio_dev *indio_dev,
123 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
6790e29f 124{
3ff24205 125 struct ad5624r_state *st = iio_priv(indio_dev);
6790e29f 126
14f88f1b 127 return sprintf(buf, "%d\n",
66eb9050 128 !!(st->pwr_down_mask & (1 << chan->channel)));
6790e29f
BS
129}
130
66eb9050
LPC
131static ssize_t ad5624r_write_dac_powerdown(struct iio_dev *indio_dev,
132 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
133 size_t len)
6790e29f 134{
2490594e 135 bool pwr_down;
6790e29f 136 int ret;
3ff24205 137 struct ad5624r_state *st = iio_priv(indio_dev);
6790e29f 138
2490594e 139 ret = strtobool(buf, &pwr_down);
6790e29f
BS
140 if (ret)
141 return ret;
142
2490594e 143 if (pwr_down)
66eb9050 144 st->pwr_down_mask |= (1 << chan->channel);
14f88f1b 145 else
2490594e 146 st->pwr_down_mask &= ~(1 << chan->channel);
6790e29f 147
14f88f1b
MH
148 ret = ad5624r_spi_write(st->us, AD5624R_CMD_POWERDOWN_DAC, 0,
149 (st->pwr_down_mode << 4) |
150 st->pwr_down_mask, 16);
6790e29f
BS
151
152 return ret ? ret : len;
153}
154
6fe8135f 155static const struct iio_info ad5624r_info = {
275de9f7
LPC
156 .write_raw = ad5624r_write_raw,
157 .read_raw = ad5624r_read_raw,
6fe8135f
JC
158 .driver_module = THIS_MODULE,
159};
160
66eb9050
LPC
161static const struct iio_chan_spec_ext_info ad5624r_ext_info[] = {
162 {
163 .name = "powerdown",
164 .read = ad5624r_read_dac_powerdown,
165 .write = ad5624r_write_dac_powerdown,
3704432f 166 .shared = IIO_SEPARATE,
66eb9050 167 },
3704432f
JC
168 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
169 &ad5624r_powerdown_mode_enum),
66eb9050
LPC
170 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5624r_powerdown_mode_enum),
171 { },
172};
173
174#define AD5624R_CHANNEL(_chan, _bits) { \
175 .type = IIO_VOLTAGE, \
176 .indexed = 1, \
177 .output = 1, \
178 .channel = (_chan), \
8b686348
JC
179 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
180 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
66eb9050
LPC
181 .address = (_chan), \
182 .scan_type = IIO_ST('u', (_bits), 16, 16 - (_bits)), \
183 .ext_info = ad5624r_ext_info, \
184}
185
186#define DECLARE_AD5624R_CHANNELS(_name, _bits) \
187 const struct iio_chan_spec _name##_channels[] = { \
188 AD5624R_CHANNEL(0, _bits), \
189 AD5624R_CHANNEL(1, _bits), \
190 AD5624R_CHANNEL(2, _bits), \
191 AD5624R_CHANNEL(3, _bits), \
192}
193
194static DECLARE_AD5624R_CHANNELS(ad5624r, 12);
195static DECLARE_AD5624R_CHANNELS(ad5644r, 14);
196static DECLARE_AD5624R_CHANNELS(ad5664r, 16);
197
198static const struct ad5624r_chip_info ad5624r_chip_info_tbl[] = {
199 [ID_AD5624R3] = {
200 .channels = ad5624r_channels,
201 .int_vref_mv = 1250,
202 },
203 [ID_AD5624R5] = {
204 .channels = ad5624r_channels,
205 .int_vref_mv = 2500,
206 },
207 [ID_AD5644R3] = {
208 .channels = ad5644r_channels,
209 .int_vref_mv = 1250,
210 },
211 [ID_AD5644R5] = {
212 .channels = ad5644r_channels,
213 .int_vref_mv = 2500,
214 },
215 [ID_AD5664R3] = {
216 .channels = ad5664r_channels,
217 .int_vref_mv = 1250,
218 },
219 [ID_AD5664R5] = {
220 .channels = ad5664r_channels,
221 .int_vref_mv = 2500,
222 },
223};
224
fc52692c 225static int ad5624r_probe(struct spi_device *spi)
6790e29f 226{
6790e29f 227 struct ad5624r_state *st;
3ff24205 228 struct iio_dev *indio_dev;
14f88f1b 229 int ret, voltage_uv = 0;
6790e29f 230
75238230
SK
231 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
232 if (!indio_dev)
233 return -ENOMEM;
3ff24205 234 st = iio_priv(indio_dev);
75238230 235 st->reg = devm_regulator_get(&spi->dev, "vcc");
26a54797
JC
236 if (!IS_ERR(st->reg)) {
237 ret = regulator_enable(st->reg);
238 if (ret)
75238230 239 return ret;
26a54797 240
8434e785
AL
241 ret = regulator_get_voltage(st->reg);
242 if (ret < 0)
243 goto error_disable_reg;
244
245 voltage_uv = ret;
26a54797
JC
246 }
247
3ff24205 248 spi_set_drvdata(spi, indio_dev);
14f88f1b
MH
249 st->chip_info =
250 &ad5624r_chip_info_tbl[spi_get_device_id(spi)->driver_data];
251
252 if (voltage_uv)
253 st->vref_mv = voltage_uv / 1000;
254 else
255 st->vref_mv = st->chip_info->int_vref_mv;
6790e29f
BS
256
257 st->us = spi;
6790e29f 258
3ff24205
JC
259 indio_dev->dev.parent = &spi->dev;
260 indio_dev->name = spi_get_device_id(spi)->name;
261 indio_dev->info = &ad5624r_info;
262 indio_dev->modes = INDIO_DIRECT_MODE;
275de9f7
LPC
263 indio_dev->channels = st->chip_info->channels;
264 indio_dev->num_channels = AD5624R_DAC_CHANNELS;
3ff24205 265
3e394407
JC
266 ret = ad5624r_spi_write(spi, AD5624R_CMD_INTERNAL_REFER_SETUP, 0,
267 !!voltage_uv, 16);
6790e29f 268 if (ret)
26a54797 269 goto error_disable_reg;
6790e29f 270
3e394407 271 ret = iio_device_register(indio_dev);
14f88f1b 272 if (ret)
26a54797 273 goto error_disable_reg;
6790e29f
BS
274
275 return 0;
276
14f88f1b 277error_disable_reg:
26a54797
JC
278 if (!IS_ERR(st->reg))
279 regulator_disable(st->reg);
14f88f1b 280
6790e29f
BS
281 return ret;
282}
283
fc52692c 284static int ad5624r_remove(struct spi_device *spi)
6790e29f 285{
3ff24205
JC
286 struct iio_dev *indio_dev = spi_get_drvdata(spi);
287 struct ad5624r_state *st = iio_priv(indio_dev);
3ff24205 288
d2fffd6c 289 iio_device_unregister(indio_dev);
75238230 290 if (!IS_ERR(st->reg))
26a54797 291 regulator_disable(st->reg);
14f88f1b 292
6790e29f
BS
293 return 0;
294}
295
ece30c15 296static const struct spi_device_id ad5624r_id[] = {
14f88f1b
MH
297 {"ad5624r3", ID_AD5624R3},
298 {"ad5644r3", ID_AD5644R3},
299 {"ad5664r3", ID_AD5664R3},
300 {"ad5624r5", ID_AD5624R5},
301 {"ad5644r5", ID_AD5644R5},
302 {"ad5664r5", ID_AD5664R5},
ece30c15
MH
303 {}
304};
55e4390c 305MODULE_DEVICE_TABLE(spi, ad5624r_id);
ece30c15 306
6790e29f
BS
307static struct spi_driver ad5624r_driver = {
308 .driver = {
24d6050b
MH
309 .name = "ad5624r",
310 .owner = THIS_MODULE,
311 },
6790e29f 312 .probe = ad5624r_probe,
fc52692c 313 .remove = ad5624r_remove,
ece30c15 314 .id_table = ad5624r_id,
6790e29f 315};
ae6ae6fe 316module_spi_driver(ad5624r_driver);
6790e29f
BS
317
318MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
319MODULE_DESCRIPTION("Analog Devices AD5624/44/64R DAC spi driver");
320MODULE_LICENSE("GPL v2");