PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / iio / dac / ad5624r_spi.c
blobe8199cce2aeaed585dd80e600a654ed3f52d03c3
1 /*
2 * AD5624R, AD5644R, AD5664R Digital to analog convertors spi driver
4 * Copyright 2010-2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
7 */
9 #include <linux/interrupt.h>
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>
16 #include <linux/regulator/consumer.h>
17 #include <linux/module.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
22 #include "ad5624r.h"
24 static int ad5624r_spi_write(struct spi_device *spi,
25 u8 cmd, u8 addr, u16 val, u8 len)
27 u32 data;
28 u8 msg[3];
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.
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;
43 return spi_write(spi, msg, 3);
46 static 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)
52 struct ad5624r_state *st = iio_priv(indio_dev);
54 switch (m) {
55 case IIO_CHAN_INFO_SCALE:
56 *val = st->vref_mv;
57 *val2 = chan->scan_type.realbits;
58 return IIO_VAL_FRACTIONAL_LOG2;
60 return -EINVAL;
63 static 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)
69 struct ad5624r_state *st = iio_priv(indio_dev);
70 int ret;
72 switch (mask) {
73 case IIO_CHAN_INFO_RAW:
74 if (val >= (1 << chan->scan_type.realbits) || val < 0)
75 return -EINVAL;
77 return ad5624r_spi_write(st->us,
78 AD5624R_CMD_WRITE_INPUT_N_UPDATE_N,
79 chan->address, val,
80 chan->scan_type.shift);
81 default:
82 ret = -EINVAL;
85 return -EINVAL;
88 static const char * const ad5624r_powerdown_modes[] = {
89 "1kohm_to_gnd",
90 "100kohm_to_gnd",
91 "three_state"
94 static int ad5624r_get_powerdown_mode(struct iio_dev *indio_dev,
95 const struct iio_chan_spec *chan)
97 struct ad5624r_state *st = iio_priv(indio_dev);
99 return st->pwr_down_mode;
102 static int ad5624r_set_powerdown_mode(struct iio_dev *indio_dev,
103 const struct iio_chan_spec *chan, unsigned int mode)
105 struct ad5624r_state *st = iio_priv(indio_dev);
107 st->pwr_down_mode = mode;
109 return 0;
112 static const struct iio_enum ad5624r_powerdown_mode_enum = {
113 .items = ad5624r_powerdown_modes,
114 .num_items = ARRAY_SIZE(ad5624r_powerdown_modes),
115 .get = ad5624r_get_powerdown_mode,
116 .set = ad5624r_set_powerdown_mode,
119 static ssize_t ad5624r_read_dac_powerdown(struct iio_dev *indio_dev,
120 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
122 struct ad5624r_state *st = iio_priv(indio_dev);
124 return sprintf(buf, "%d\n",
125 !!(st->pwr_down_mask & (1 << chan->channel)));
128 static ssize_t ad5624r_write_dac_powerdown(struct iio_dev *indio_dev,
129 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
130 size_t len)
132 bool pwr_down;
133 int ret;
134 struct ad5624r_state *st = iio_priv(indio_dev);
136 ret = strtobool(buf, &pwr_down);
137 if (ret)
138 return ret;
140 if (pwr_down)
141 st->pwr_down_mask |= (1 << chan->channel);
142 else
143 st->pwr_down_mask &= ~(1 << chan->channel);
145 ret = ad5624r_spi_write(st->us, AD5624R_CMD_POWERDOWN_DAC, 0,
146 (st->pwr_down_mode << 4) |
147 st->pwr_down_mask, 16);
149 return ret ? ret : len;
152 static const struct iio_info ad5624r_info = {
153 .write_raw = ad5624r_write_raw,
154 .read_raw = ad5624r_read_raw,
155 .driver_module = THIS_MODULE,
158 static const struct iio_chan_spec_ext_info ad5624r_ext_info[] = {
160 .name = "powerdown",
161 .read = ad5624r_read_dac_powerdown,
162 .write = ad5624r_write_dac_powerdown,
163 .shared = IIO_SEPARATE,
165 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
166 &ad5624r_powerdown_mode_enum),
167 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5624r_powerdown_mode_enum),
168 { },
171 #define AD5624R_CHANNEL(_chan, _bits) { \
172 .type = IIO_VOLTAGE, \
173 .indexed = 1, \
174 .output = 1, \
175 .channel = (_chan), \
176 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
177 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
178 .address = (_chan), \
179 .scan_type = { \
180 .sign = 'u', \
181 .realbits = (_bits), \
182 .storagebits = 16, \
183 .shift = 16 - (_bits), \
184 }, \
185 .ext_info = ad5624r_ext_info, \
188 #define DECLARE_AD5624R_CHANNELS(_name, _bits) \
189 const struct iio_chan_spec _name##_channels[] = { \
190 AD5624R_CHANNEL(0, _bits), \
191 AD5624R_CHANNEL(1, _bits), \
192 AD5624R_CHANNEL(2, _bits), \
193 AD5624R_CHANNEL(3, _bits), \
196 static DECLARE_AD5624R_CHANNELS(ad5624r, 12);
197 static DECLARE_AD5624R_CHANNELS(ad5644r, 14);
198 static DECLARE_AD5624R_CHANNELS(ad5664r, 16);
200 static const struct ad5624r_chip_info ad5624r_chip_info_tbl[] = {
201 [ID_AD5624R3] = {
202 .channels = ad5624r_channels,
203 .int_vref_mv = 1250,
205 [ID_AD5624R5] = {
206 .channels = ad5624r_channels,
207 .int_vref_mv = 2500,
209 [ID_AD5644R3] = {
210 .channels = ad5644r_channels,
211 .int_vref_mv = 1250,
213 [ID_AD5644R5] = {
214 .channels = ad5644r_channels,
215 .int_vref_mv = 2500,
217 [ID_AD5664R3] = {
218 .channels = ad5664r_channels,
219 .int_vref_mv = 1250,
221 [ID_AD5664R5] = {
222 .channels = ad5664r_channels,
223 .int_vref_mv = 2500,
227 static int ad5624r_probe(struct spi_device *spi)
229 struct ad5624r_state *st;
230 struct iio_dev *indio_dev;
231 int ret, voltage_uv = 0;
233 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
234 if (!indio_dev)
235 return -ENOMEM;
236 st = iio_priv(indio_dev);
237 st->reg = devm_regulator_get(&spi->dev, "vcc");
238 if (!IS_ERR(st->reg)) {
239 ret = regulator_enable(st->reg);
240 if (ret)
241 return ret;
243 ret = regulator_get_voltage(st->reg);
244 if (ret < 0)
245 goto error_disable_reg;
247 voltage_uv = ret;
250 spi_set_drvdata(spi, indio_dev);
251 st->chip_info =
252 &ad5624r_chip_info_tbl[spi_get_device_id(spi)->driver_data];
254 if (voltage_uv)
255 st->vref_mv = voltage_uv / 1000;
256 else
257 st->vref_mv = st->chip_info->int_vref_mv;
259 st->us = spi;
261 indio_dev->dev.parent = &spi->dev;
262 indio_dev->name = spi_get_device_id(spi)->name;
263 indio_dev->info = &ad5624r_info;
264 indio_dev->modes = INDIO_DIRECT_MODE;
265 indio_dev->channels = st->chip_info->channels;
266 indio_dev->num_channels = AD5624R_DAC_CHANNELS;
268 ret = ad5624r_spi_write(spi, AD5624R_CMD_INTERNAL_REFER_SETUP, 0,
269 !!voltage_uv, 16);
270 if (ret)
271 goto error_disable_reg;
273 ret = iio_device_register(indio_dev);
274 if (ret)
275 goto error_disable_reg;
277 return 0;
279 error_disable_reg:
280 if (!IS_ERR(st->reg))
281 regulator_disable(st->reg);
283 return ret;
286 static int ad5624r_remove(struct spi_device *spi)
288 struct iio_dev *indio_dev = spi_get_drvdata(spi);
289 struct ad5624r_state *st = iio_priv(indio_dev);
291 iio_device_unregister(indio_dev);
292 if (!IS_ERR(st->reg))
293 regulator_disable(st->reg);
295 return 0;
298 static const struct spi_device_id ad5624r_id[] = {
299 {"ad5624r3", ID_AD5624R3},
300 {"ad5644r3", ID_AD5644R3},
301 {"ad5664r3", ID_AD5664R3},
302 {"ad5624r5", ID_AD5624R5},
303 {"ad5644r5", ID_AD5644R5},
304 {"ad5664r5", ID_AD5664R5},
307 MODULE_DEVICE_TABLE(spi, ad5624r_id);
309 static struct spi_driver ad5624r_driver = {
310 .driver = {
311 .name = "ad5624r",
312 .owner = THIS_MODULE,
314 .probe = ad5624r_probe,
315 .remove = ad5624r_remove,
316 .id_table = ad5624r_id,
318 module_spi_driver(ad5624r_driver);
320 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
321 MODULE_DESCRIPTION("Analog Devices AD5624/44/64R DAC spi driver");
322 MODULE_LICENSE("GPL v2");