staging:iio:dac:ad5504: Convert to extended channel attributes
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / 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>
6790e29f
BS
21#include "dac.h"
22#include "ad5624r.h"
23
275de9f7
LPC
24#define AD5624R_CHANNEL(_chan, _bits) { \
25 .type = IIO_VOLTAGE, \
26 .indexed = 1, \
27 .output = 1, \
28 .channel = (_chan), \
09f4eb40
JC
29 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
30 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
275de9f7
LPC
31 .address = (_chan), \
32 .scan_type = IIO_ST('u', (_bits), 16, 16 - (_bits)), \
33}
34
35#define DECLARE_AD5624R_CHANNELS(_name, _bits) \
36 const struct iio_chan_spec _name##_channels[] = { \
37 AD5624R_CHANNEL(0, _bits), \
38 AD5624R_CHANNEL(1, _bits), \
39 AD5624R_CHANNEL(2, _bits), \
40 AD5624R_CHANNEL(3, _bits), \
41}
42
43static DECLARE_AD5624R_CHANNELS(ad5624r, 12);
44static DECLARE_AD5624R_CHANNELS(ad5644r, 14);
45static DECLARE_AD5624R_CHANNELS(ad5664r, 16);
46
14f88f1b
MH
47static const struct ad5624r_chip_info ad5624r_chip_info_tbl[] = {
48 [ID_AD5624R3] = {
275de9f7 49 .channels = ad5624r_channels,
14f88f1b
MH
50 .int_vref_mv = 1250,
51 },
52 [ID_AD5624R5] = {
275de9f7 53 .channels = ad5624r_channels,
14f88f1b
MH
54 .int_vref_mv = 2500,
55 },
275de9f7
LPC
56 [ID_AD5644R3] = {
57 .channels = ad5644r_channels,
58 .int_vref_mv = 1250,
59 },
14f88f1b 60 [ID_AD5644R5] = {
275de9f7 61 .channels = ad5644r_channels,
14f88f1b
MH
62 .int_vref_mv = 2500,
63 },
275de9f7
LPC
64 [ID_AD5664R3] = {
65 .channels = ad5664r_channels,
66 .int_vref_mv = 1250,
67 },
14f88f1b 68 [ID_AD5664R5] = {
275de9f7 69 .channels = ad5664r_channels,
14f88f1b
MH
70 .int_vref_mv = 2500,
71 },
6790e29f
BS
72};
73
df9cd105
MH
74static int ad5624r_spi_write(struct spi_device *spi,
75 u8 cmd, u8 addr, u16 val, u8 len)
6790e29f 76{
6790e29f
BS
77 u32 data;
78 u8 msg[3];
79
80 /*
14f88f1b
MH
81 * The input shift register is 24 bits wide. The first two bits are
82 * don't care bits. The next three are the command bits, C2 to C0,
83 * followed by the 3-bit DAC address, A2 to A0, and then the
84 * 16-, 14-, 12-bit data-word. The data-word comprises the 16-,
85 * 14-, 12-bit input code followed by 0, 2, or 4 don't care bits,
86 * for the AD5664R, AD5644R, and AD5624R, respectively.
6790e29f
BS
87 */
88 data = (0 << 22) | (cmd << 19) | (addr << 16) | (val << (16 - len));
89 msg[0] = data >> 16;
90 msg[1] = data >> 8;
91 msg[2] = data;
92
df9cd105 93 return spi_write(spi, msg, 3);
6790e29f
BS
94}
95
275de9f7
LPC
96static int ad5624r_read_raw(struct iio_dev *indio_dev,
97 struct iio_chan_spec const *chan,
98 int *val,
99 int *val2,
100 long m)
6790e29f 101{
3ff24205 102 struct ad5624r_state *st = iio_priv(indio_dev);
275de9f7 103 unsigned long scale_uv;
6790e29f 104
275de9f7
LPC
105 switch (m) {
106 case IIO_CHAN_INFO_SCALE:
107 scale_uv = (st->vref_mv * 1000) >> chan->scan_type.realbits;
108 *val = scale_uv / 1000;
109 *val2 = (scale_uv % 1000) * 1000;
110 return IIO_VAL_INT_PLUS_MICRO;
6790e29f 111
275de9f7
LPC
112 }
113 return -EINVAL;
114}
115
116static int ad5624r_write_raw(struct iio_dev *indio_dev,
117 struct iio_chan_spec const *chan,
118 int val,
119 int val2,
120 long mask)
121{
122 struct ad5624r_state *st = iio_priv(indio_dev);
123 int ret;
124
125 switch (mask) {
09f4eb40 126 case IIO_CHAN_INFO_RAW:
275de9f7
LPC
127 if (val >= (1 << chan->scan_type.realbits) || val < 0)
128 return -EINVAL;
129
130 return ad5624r_spi_write(st->us,
131 AD5624R_CMD_WRITE_INPUT_N_UPDATE_N,
132 chan->address, val,
133 chan->scan_type.shift);
134 default:
135 ret = -EINVAL;
136 }
137
138 return -EINVAL;
6790e29f
BS
139}
140
14f88f1b 141static ssize_t ad5624r_read_powerdown_mode(struct device *dev,
24d6050b 142 struct device_attribute *attr, char *buf)
6790e29f 143{
ac3f851e 144 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
3ff24205 145 struct ad5624r_state *st = iio_priv(indio_dev);
6790e29f 146
14f88f1b
MH
147 char mode[][15] = {"", "1kohm_to_gnd", "100kohm_to_gnd", "three_state"};
148
149 return sprintf(buf, "%s\n", mode[st->pwr_down_mode]);
6790e29f
BS
150}
151
14f88f1b 152static ssize_t ad5624r_write_powerdown_mode(struct device *dev,
24d6050b
MH
153 struct device_attribute *attr,
154 const char *buf, size_t len)
6790e29f 155{
ac3f851e 156 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
3ff24205 157 struct ad5624r_state *st = iio_priv(indio_dev);
14f88f1b 158 int ret;
6790e29f 159
14f88f1b
MH
160 if (sysfs_streq(buf, "1kohm_to_gnd"))
161 st->pwr_down_mode = AD5624R_LDAC_PWRDN_1K;
162 else if (sysfs_streq(buf, "100kohm_to_gnd"))
163 st->pwr_down_mode = AD5624R_LDAC_PWRDN_100K;
164 else if (sysfs_streq(buf, "three_state"))
165 st->pwr_down_mode = AD5624R_LDAC_PWRDN_3STATE;
166 else
167 ret = -EINVAL;
6790e29f
BS
168
169 return ret ? ret : len;
170}
171
14f88f1b 172static ssize_t ad5624r_read_dac_powerdown(struct device *dev,
24d6050b
MH
173 struct device_attribute *attr,
174 char *buf)
6790e29f 175{
ac3f851e 176 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
3ff24205 177 struct ad5624r_state *st = iio_priv(indio_dev);
6790e29f
BS
178 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
179
14f88f1b
MH
180 return sprintf(buf, "%d\n",
181 !!(st->pwr_down_mask & (1 << this_attr->address)));
6790e29f
BS
182}
183
14f88f1b 184static ssize_t ad5624r_write_dac_powerdown(struct device *dev,
24d6050b
MH
185 struct device_attribute *attr,
186 const char *buf, size_t len)
6790e29f
BS
187{
188 long readin;
189 int ret;
ac3f851e 190 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
3ff24205 191 struct ad5624r_state *st = iio_priv(indio_dev);
6790e29f
BS
192 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
193
194 ret = strict_strtol(buf, 10, &readin);
195 if (ret)
196 return ret;
197
14f88f1b
MH
198 if (readin == 1)
199 st->pwr_down_mask |= (1 << this_attr->address);
200 else if (!readin)
201 st->pwr_down_mask &= ~(1 << this_attr->address);
202 else
203 ret = -EINVAL;
6790e29f 204
14f88f1b
MH
205 ret = ad5624r_spi_write(st->us, AD5624R_CMD_POWERDOWN_DAC, 0,
206 (st->pwr_down_mode << 4) |
207 st->pwr_down_mask, 16);
6790e29f
BS
208
209 return ret ? ret : len;
210}
211
322c9563 212static IIO_DEVICE_ATTR(out_voltage_powerdown_mode, S_IRUGO |
14f88f1b
MH
213 S_IWUSR, ad5624r_read_powerdown_mode,
214 ad5624r_write_powerdown_mode, 0);
6790e29f 215
322c9563 216static IIO_CONST_ATTR(out_voltage_powerdown_mode_available,
14f88f1b 217 "1kohm_to_gnd 100kohm_to_gnd three_state");
6790e29f 218
14f88f1b 219#define IIO_DEV_ATTR_DAC_POWERDOWN(_num, _show, _store, _addr) \
322c9563 220 IIO_DEVICE_ATTR(out_voltage##_num##_powerdown, \
14f88f1b
MH
221 S_IRUGO | S_IWUSR, _show, _store, _addr)
222
223static IIO_DEV_ATTR_DAC_POWERDOWN(0, ad5624r_read_dac_powerdown,
224 ad5624r_write_dac_powerdown, 0);
225static IIO_DEV_ATTR_DAC_POWERDOWN(1, ad5624r_read_dac_powerdown,
226 ad5624r_write_dac_powerdown, 1);
227static IIO_DEV_ATTR_DAC_POWERDOWN(2, ad5624r_read_dac_powerdown,
228 ad5624r_write_dac_powerdown, 2);
229static IIO_DEV_ATTR_DAC_POWERDOWN(3, ad5624r_read_dac_powerdown,
230 ad5624r_write_dac_powerdown, 3);
6790e29f
BS
231
232static struct attribute *ad5624r_attributes[] = {
322c9563
JC
233 &iio_dev_attr_out_voltage0_powerdown.dev_attr.attr,
234 &iio_dev_attr_out_voltage1_powerdown.dev_attr.attr,
235 &iio_dev_attr_out_voltage2_powerdown.dev_attr.attr,
236 &iio_dev_attr_out_voltage3_powerdown.dev_attr.attr,
237 &iio_dev_attr_out_voltage_powerdown_mode.dev_attr.attr,
238 &iio_const_attr_out_voltage_powerdown_mode_available.dev_attr.attr,
6790e29f
BS
239 NULL,
240};
241
242static const struct attribute_group ad5624r_attribute_group = {
243 .attrs = ad5624r_attributes,
244};
245
6fe8135f 246static const struct iio_info ad5624r_info = {
275de9f7
LPC
247 .write_raw = ad5624r_write_raw,
248 .read_raw = ad5624r_read_raw,
6fe8135f
JC
249 .attrs = &ad5624r_attribute_group,
250 .driver_module = THIS_MODULE,
251};
252
6790e29f
BS
253static int __devinit ad5624r_probe(struct spi_device *spi)
254{
6790e29f 255 struct ad5624r_state *st;
3ff24205 256 struct iio_dev *indio_dev;
14f88f1b 257 int ret, voltage_uv = 0;
6790e29f 258
7cbb7537 259 indio_dev = iio_device_alloc(sizeof(*st));
3ff24205
JC
260 if (indio_dev == NULL) {
261 ret = -ENOMEM;
26a54797 262 goto error_ret;
3ff24205
JC
263 }
264 st = iio_priv(indio_dev);
26a54797
JC
265 st->reg = regulator_get(&spi->dev, "vcc");
266 if (!IS_ERR(st->reg)) {
267 ret = regulator_enable(st->reg);
268 if (ret)
269 goto error_put_reg;
270
271 voltage_uv = regulator_get_voltage(st->reg);
272 }
273
3ff24205 274 spi_set_drvdata(spi, indio_dev);
14f88f1b
MH
275 st->chip_info =
276 &ad5624r_chip_info_tbl[spi_get_device_id(spi)->driver_data];
277
278 if (voltage_uv)
279 st->vref_mv = voltage_uv / 1000;
280 else
281 st->vref_mv = st->chip_info->int_vref_mv;
6790e29f
BS
282
283 st->us = spi;
6790e29f 284
3ff24205
JC
285 indio_dev->dev.parent = &spi->dev;
286 indio_dev->name = spi_get_device_id(spi)->name;
287 indio_dev->info = &ad5624r_info;
288 indio_dev->modes = INDIO_DIRECT_MODE;
275de9f7
LPC
289 indio_dev->channels = st->chip_info->channels;
290 indio_dev->num_channels = AD5624R_DAC_CHANNELS;
3ff24205 291
3e394407
JC
292 ret = ad5624r_spi_write(spi, AD5624R_CMD_INTERNAL_REFER_SETUP, 0,
293 !!voltage_uv, 16);
6790e29f 294 if (ret)
26a54797 295 goto error_disable_reg;
6790e29f 296
3e394407 297 ret = iio_device_register(indio_dev);
14f88f1b 298 if (ret)
26a54797 299 goto error_disable_reg;
6790e29f
BS
300
301 return 0;
302
14f88f1b 303error_disable_reg:
26a54797
JC
304 if (!IS_ERR(st->reg))
305 regulator_disable(st->reg);
14f88f1b 306error_put_reg:
26a54797
JC
307 if (!IS_ERR(st->reg))
308 regulator_put(st->reg);
7cbb7537 309 iio_device_free(indio_dev);
26a54797 310error_ret:
14f88f1b 311
6790e29f
BS
312 return ret;
313}
314
315static int __devexit ad5624r_remove(struct spi_device *spi)
316{
3ff24205
JC
317 struct iio_dev *indio_dev = spi_get_drvdata(spi);
318 struct ad5624r_state *st = iio_priv(indio_dev);
3ff24205 319
d2fffd6c 320 iio_device_unregister(indio_dev);
26a54797
JC
321 if (!IS_ERR(st->reg)) {
322 regulator_disable(st->reg);
323 regulator_put(st->reg);
14f88f1b 324 }
7cbb7537 325 iio_device_free(indio_dev);
14f88f1b 326
6790e29f
BS
327 return 0;
328}
329
ece30c15 330static const struct spi_device_id ad5624r_id[] = {
14f88f1b
MH
331 {"ad5624r3", ID_AD5624R3},
332 {"ad5644r3", ID_AD5644R3},
333 {"ad5664r3", ID_AD5664R3},
334 {"ad5624r5", ID_AD5624R5},
335 {"ad5644r5", ID_AD5644R5},
336 {"ad5664r5", ID_AD5664R5},
ece30c15
MH
337 {}
338};
55e4390c 339MODULE_DEVICE_TABLE(spi, ad5624r_id);
ece30c15 340
6790e29f
BS
341static struct spi_driver ad5624r_driver = {
342 .driver = {
24d6050b
MH
343 .name = "ad5624r",
344 .owner = THIS_MODULE,
345 },
6790e29f
BS
346 .probe = ad5624r_probe,
347 .remove = __devexit_p(ad5624r_remove),
ece30c15 348 .id_table = ad5624r_id,
6790e29f 349};
ae6ae6fe 350module_spi_driver(ad5624r_driver);
6790e29f
BS
351
352MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
353MODULE_DESCRIPTION("Analog Devices AD5624/44/64R DAC spi driver");
354MODULE_LICENSE("GPL v2");