aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/staging/iio/adc/ad7780.c80
1 files changed, 36 insertions, 44 deletions
diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index 14b16b5d4b1a..06d3b6aca544 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -40,7 +40,6 @@ struct ad7780_chip_info {
40}; 40};
41 41
42struct ad7780_state { 42struct ad7780_state {
43 struct iio_dev *indio_dev;
44 struct spi_device *spi; 43 struct spi_device *spi;
45 const struct ad7780_chip_info *chip_info; 44 const struct ad7780_chip_info *chip_info;
46 struct regulator *reg; 45 struct regulator *reg;
@@ -92,9 +91,10 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
92 int *val2, 91 int *val2,
93 long m) 92 long m)
94{ 93{
95 int ret, smpl; 94 struct ad7780_state *st = iio_priv(indio_dev);
96 struct ad7780_state *st = indio_dev->dev_data; 95 struct iio_chan_spec channel = st->chip_info->channel;
97 unsigned int scale_uv; 96 int ret, smpl = 0;
97 unsigned long scale_uv;
98 98
99 switch (m) { 99 switch (m) {
100 case 0: 100 case 0:
@@ -109,11 +109,9 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
109 !((smpl & AD7780_PAT0) && !(smpl & AD7780_PAT1))) 109 !((smpl & AD7780_PAT0) && !(smpl & AD7780_PAT1)))
110 return -EIO; 110 return -EIO;
111 111
112 *val = (smpl >> st->chip_info->channel.scan_type.shift) & 112 *val = (smpl >> channel.scan_type.shift) &
113 ((1 << (st->chip_info->channel.scan_type.realbits)) 113 ((1 << (channel.scan_type.realbits)) - 1);
114 - 1); 114 *val -= (1 << (channel.scan_type.realbits - 1));
115 *val -= (1 << (st->chip_info->channel.scan_type.realbits
116 - 1));
117 115
118 if (!(smpl & AD7780_GAIN)) 116 if (!(smpl & AD7780_GAIN))
119 *val *= 128; 117 *val *= 128;
@@ -121,7 +119,7 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
121 return IIO_VAL_INT; 119 return IIO_VAL_INT;
122 case (1 << IIO_CHAN_INFO_SCALE_SHARED): 120 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
123 scale_uv = (st->int_vref_mv * 100000) 121 scale_uv = (st->int_vref_mv * 100000)
124 >> (st->chip_info->channel.scan_type.realbits - 1); 122 >> (channel.scan_type.realbits - 1);
125 *val = scale_uv / 100000; 123 *val = scale_uv / 100000;
126 *val2 = (scale_uv % 100000) * 10; 124 *val2 = (scale_uv % 100000) * 10;
127 return IIO_VAL_INT_PLUS_MICRO; 125 return IIO_VAL_INT_PLUS_MICRO;
@@ -159,6 +157,7 @@ static int __devinit ad7780_probe(struct spi_device *spi)
159{ 157{
160 struct ad7780_platform_data *pdata = spi->dev.platform_data; 158 struct ad7780_platform_data *pdata = spi->dev.platform_data;
161 struct ad7780_state *st; 159 struct ad7780_state *st;
160 struct iio_dev *indio_dev;
162 int ret, voltage_uv = 0; 161 int ret, voltage_uv = 0;
163 162
164 if (!pdata) { 163 if (!pdata) {
@@ -166,11 +165,11 @@ static int __devinit ad7780_probe(struct spi_device *spi)
166 return -ENODEV; 165 return -ENODEV;
167 } 166 }
168 167
169 st = kzalloc(sizeof(*st), GFP_KERNEL); 168 indio_dev = iio_allocate_device(sizeof(*st));
170 if (st == NULL) { 169 if (indio_dev == NULL)
171 ret = -ENOMEM; 170 return -ENOMEM;
172 goto error_ret; 171
173 } 172 st = iio_priv(indio_dev);
174 173
175 st->reg = regulator_get(&spi->dev, "vcc"); 174 st->reg = regulator_get(&spi->dev, "vcc");
176 if (!IS_ERR(st->reg)) { 175 if (!IS_ERR(st->reg)) {
@@ -193,24 +192,16 @@ static int __devinit ad7780_probe(struct spi_device *spi)
193 else 192 else
194 dev_warn(&spi->dev, "reference voltage unspecified\n"); 193 dev_warn(&spi->dev, "reference voltage unspecified\n");
195 194
196 spi_set_drvdata(spi, st); 195 spi_set_drvdata(spi, indio_dev);
197 st->spi = spi; 196 st->spi = spi;
198 197
199 st->indio_dev = iio_allocate_device(0); 198 indio_dev->dev.parent = &spi->dev;
200 if (st->indio_dev == NULL) { 199 indio_dev->name = spi_get_device_id(spi)->name;
201 ret = -ENOMEM; 200 indio_dev->driver_module = THIS_MODULE;
202 goto error_disable_reg; 201 indio_dev->modes = INDIO_DIRECT_MODE;
203 } 202 indio_dev->channels = &st->chip_info->channel;
204 203 indio_dev->num_channels = 1;
205 /* Establish that the iio_dev is a child of the spi device */ 204 indio_dev->read_raw = &ad7780_read_raw;
206 st->indio_dev->dev.parent = &spi->dev;
207 st->indio_dev->name = spi_get_device_id(spi)->name;
208 st->indio_dev->dev_data = (void *)(st);
209 st->indio_dev->driver_module = THIS_MODULE;
210 st->indio_dev->modes = INDIO_DIRECT_MODE;
211 st->indio_dev->channels = &st->chip_info->channel;
212 st->indio_dev->num_channels = 1;
213 st->indio_dev->read_raw = &ad7780_read_raw;
214 205
215 init_waitqueue_head(&st->wq_data_avail); 206 init_waitqueue_head(&st->wq_data_avail);
216 207
@@ -223,20 +214,20 @@ static int __devinit ad7780_probe(struct spi_device *spi)
223 spi_message_add_tail(&st->xfer, &st->msg); 214 spi_message_add_tail(&st->xfer, &st->msg);
224 215
225 ret = gpio_request_one(st->pdata->gpio_pdrst, GPIOF_OUT_INIT_LOW, 216 ret = gpio_request_one(st->pdata->gpio_pdrst, GPIOF_OUT_INIT_LOW,
226 "AD7877 /PDRST"); 217 "AD7780 /PDRST");
227 if (ret) { 218 if (ret) {
228 dev_err(&spi->dev, "failed to request GPIO PDRST\n"); 219 dev_err(&spi->dev, "failed to request GPIO PDRST\n");
229 goto error_free_device; 220 goto error_disable_reg;
230 } 221 }
231 222
232 ret = request_irq(spi->irq, ad7780_interrupt, 223 ret = request_irq(spi->irq, ad7780_interrupt,
233 IRQF_TRIGGER_FALLING, spi_get_device_id(spi)->name, st); 224 IRQF_TRIGGER_FALLING, spi_get_device_id(spi)->name, st);
234 if (ret) 225 if (ret)
235 goto error_free_device; 226 goto error_free_gpio;
236 227
237 disable_irq(spi->irq); 228 disable_irq(spi->irq);
238 229
239 ret = iio_device_register(st->indio_dev); 230 ret = iio_device_register(indio_dev);
240 if (ret) 231 if (ret)
241 goto error_free_irq; 232 goto error_free_irq;
242 233
@@ -244,32 +235,33 @@ static int __devinit ad7780_probe(struct spi_device *spi)
244 235
245error_free_irq: 236error_free_irq:
246 free_irq(spi->irq, st); 237 free_irq(spi->irq, st);
247 238error_free_gpio:
248error_free_device: 239 gpio_free(st->pdata->gpio_pdrst);
249 iio_free_device(st->indio_dev);
250error_disable_reg: 240error_disable_reg:
251 if (!IS_ERR(st->reg)) 241 if (!IS_ERR(st->reg))
252 regulator_disable(st->reg); 242 regulator_disable(st->reg);
253error_put_reg: 243error_put_reg:
254 if (!IS_ERR(st->reg)) 244 if (!IS_ERR(st->reg))
255 regulator_put(st->reg); 245 regulator_put(st->reg);
256 kfree(st); 246
257error_ret: 247 iio_free_device(indio_dev);
248
258 return ret; 249 return ret;
259} 250}
260 251
261static int ad7780_remove(struct spi_device *spi) 252static int ad7780_remove(struct spi_device *spi)
262{ 253{
263 struct ad7780_state *st = spi_get_drvdata(spi); 254 struct iio_dev *indio_dev = spi_get_drvdata(spi);
264 struct iio_dev *indio_dev = st->indio_dev; 255 struct ad7780_state *st = iio_priv(indio_dev);
256
265 free_irq(spi->irq, st); 257 free_irq(spi->irq, st);
266 gpio_free(st->pdata->gpio_pdrst); 258 gpio_free(st->pdata->gpio_pdrst);
267 iio_device_unregister(indio_dev);
268 if (!IS_ERR(st->reg)) { 259 if (!IS_ERR(st->reg)) {
269 regulator_disable(st->reg); 260 regulator_disable(st->reg);
270 regulator_put(st->reg); 261 regulator_put(st->reg);
271 } 262 }
272 kfree(st); 263 iio_device_unregister(indio_dev);
264
273 return 0; 265 return 0;
274} 266}
275 267