aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio/adc/ad799x.c
diff options
context:
space:
mode:
authorPeter Meerwald <pmeerw@pmeerw.net>2014-12-06 00:54:00 -0500
committerJonathan Cameron <jic23@kernel.org>2014-07-13 16:46:54 -0400
commit541544712ea9f1f4511ecc1ec7dcabf6ce042909 (patch)
treefcc1df5d258d48dfa04d80675ca75fdc25e29582 /drivers/iio/adc/ad799x.c
parentdb7c17ecbf4d6328597047c4e4d6e8914945477c (diff)
iio:adc:ad799x: Drop I2C access helper functions
Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net> Acked-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio/adc/ad799x.c')
-rw-r--r--drivers/iio/adc/ad799x.c119
1 files changed, 26 insertions, 93 deletions
diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c
index 6cf9ee18a216..34d93a5eefb1 100644
--- a/drivers/iio/adc/ad799x.c
+++ b/drivers/iio/adc/ad799x.c
@@ -175,65 +175,6 @@ out:
175 return IRQ_HANDLED; 175 return IRQ_HANDLED;
176} 176}
177 177
178/*
179 * ad799x register access by I2C
180 */
181static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
182{
183 struct i2c_client *client = st->client;
184 int ret = 0;
185
186 ret = i2c_smbus_read_word_swapped(client, reg);
187 if (ret < 0) {
188 dev_err(&client->dev, "I2C read error\n");
189 return ret;
190 }
191
192 *data = (u16)ret;
193
194 return 0;
195}
196
197static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
198{
199 struct i2c_client *client = st->client;
200 int ret = 0;
201
202 ret = i2c_smbus_read_byte_data(client, reg);
203 if (ret < 0) {
204 dev_err(&client->dev, "I2C read error\n");
205 return ret;
206 }
207
208 *data = (u8)ret;
209
210 return 0;
211}
212
213static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
214{
215 struct i2c_client *client = st->client;
216 int ret = 0;
217
218 ret = i2c_smbus_write_word_swapped(client, reg, data);
219 if (ret < 0)
220 dev_err(&client->dev, "I2C write error\n");
221
222 return ret;
223}
224
225static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
226{
227 struct i2c_client *client = st->client;
228 int ret = 0;
229
230 ret = i2c_smbus_write_byte_data(client, reg, data);
231 if (ret < 0)
232 dev_err(&client->dev, "I2C write error\n");
233
234 return ret;
235}
236
237static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev, 178static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
238 const unsigned long *scan_mask) 179 const unsigned long *scan_mask)
239{ 180{
@@ -249,7 +190,7 @@ static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
249 switch (st->id) { 190 switch (st->id) {
250 case ad7997: 191 case ad7997:
251 case ad7998: 192 case ad7998:
252 return ad799x_i2c_write16(st, AD7998_CONF_REG, 193 return i2c_smbus_write_word_swapped(st->client, AD7998_CONF_REG,
253 st->config | (*scan_mask << AD799X_CHANNEL_SHIFT)); 194 st->config | (*scan_mask << AD799X_CHANNEL_SHIFT));
254 default: 195 default:
255 break; 196 break;
@@ -260,9 +201,7 @@ static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
260 201
261static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch) 202static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
262{ 203{
263 u16 rxbuf;
264 u8 cmd; 204 u8 cmd;
265 int ret;
266 205
267 switch (st->id) { 206 switch (st->id) {
268 case ad7991: 207 case ad7991:
@@ -283,11 +222,7 @@ static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
283 return -EINVAL; 222 return -EINVAL;
284 } 223 }
285 224
286 ret = ad799x_i2c_read16(st, cmd, &rxbuf); 225 return i2c_smbus_read_word_swapped(st->client, cmd);
287 if (ret < 0)
288 return ret;
289
290 return rxbuf;
291} 226}
292 227
293static int ad799x_read_raw(struct iio_dev *indio_dev, 228static int ad799x_read_raw(struct iio_dev *indio_dev,
@@ -332,6 +267,7 @@ static const unsigned int ad7998_frequencies[] = {
332 [AD7998_CYC_TCONF_1024] = 488, 267 [AD7998_CYC_TCONF_1024] = 488,
333 [AD7998_CYC_TCONF_2048] = 244, 268 [AD7998_CYC_TCONF_2048] = 244,
334}; 269};
270
335static ssize_t ad799x_read_frequency(struct device *dev, 271static ssize_t ad799x_read_frequency(struct device *dev,
336 struct device_attribute *attr, 272 struct device_attribute *attr,
337 char *buf) 273 char *buf)
@@ -339,15 +275,11 @@ static ssize_t ad799x_read_frequency(struct device *dev,
339 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 275 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
340 struct ad799x_state *st = iio_priv(indio_dev); 276 struct ad799x_state *st = iio_priv(indio_dev);
341 277
342 int ret; 278 int ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
343 u8 val; 279 if (ret < 0)
344 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
345 if (ret)
346 return ret; 280 return ret;
347 281
348 val &= AD7998_CYC_MASK; 282 return sprintf(buf, "%u\n", ad7998_frequencies[ret & AD7998_CYC_MASK]);
349
350 return sprintf(buf, "%u\n", ad7998_frequencies[val]);
351} 283}
352 284
353static ssize_t ad799x_write_frequency(struct device *dev, 285static ssize_t ad799x_write_frequency(struct device *dev,
@@ -360,18 +292,17 @@ static ssize_t ad799x_write_frequency(struct device *dev,
360 292
361 long val; 293 long val;
362 int ret, i; 294 int ret, i;
363 u8 t;
364 295
365 ret = kstrtol(buf, 10, &val); 296 ret = kstrtol(buf, 10, &val);
366 if (ret) 297 if (ret)
367 return ret; 298 return ret;
368 299
369 mutex_lock(&indio_dev->mlock); 300 mutex_lock(&indio_dev->mlock);
370 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t); 301 ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
371 if (ret) 302 if (ret < 0)
372 goto error_ret_mutex; 303 goto error_ret_mutex;
373 /* Wipe the bits clean */ 304 /* Wipe the bits clean */
374 t &= ~AD7998_CYC_MASK; 305 ret &= ~AD7998_CYC_MASK;
375 306
376 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++) 307 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
377 if (val == ad7998_frequencies[i]) 308 if (val == ad7998_frequencies[i])
@@ -380,13 +311,17 @@ static ssize_t ad799x_write_frequency(struct device *dev,
380 ret = -EINVAL; 311 ret = -EINVAL;
381 goto error_ret_mutex; 312 goto error_ret_mutex;
382 } 313 }
383 t |= i; 314
384 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t); 315 ret = i2c_smbus_write_byte_data(st->client, AD7998_CYCLE_TMR_REG,
316 ret | i);
317 if (ret < 0)
318 goto error_ret_mutex;
319 ret = len;
385 320
386error_ret_mutex: 321error_ret_mutex:
387 mutex_unlock(&indio_dev->mlock); 322 mutex_unlock(&indio_dev->mlock);
388 323
389 return ret ? ret : len; 324 return ret;
390} 325}
391 326
392static int ad799x_read_event_config(struct iio_dev *indio_dev, 327static int ad799x_read_event_config(struct iio_dev *indio_dev,
@@ -430,7 +365,8 @@ static int ad799x_write_event_value(struct iio_dev *indio_dev,
430 return -EINVAL; 365 return -EINVAL;
431 366
432 mutex_lock(&indio_dev->mlock); 367 mutex_lock(&indio_dev->mlock);
433 ret = ad799x_i2c_write16(st, ad799x_threshold_reg(chan, dir, info), 368 ret = i2c_smbus_write_word_swapped(st->client,
369 ad799x_threshold_reg(chan, dir, info),
434 val << chan->scan_type.shift); 370 val << chan->scan_type.shift);
435 mutex_unlock(&indio_dev->mlock); 371 mutex_unlock(&indio_dev->mlock);
436 372
@@ -446,15 +382,14 @@ static int ad799x_read_event_value(struct iio_dev *indio_dev,
446{ 382{
447 int ret; 383 int ret;
448 struct ad799x_state *st = iio_priv(indio_dev); 384 struct ad799x_state *st = iio_priv(indio_dev);
449 u16 valin;
450 385
451 mutex_lock(&indio_dev->mlock); 386 mutex_lock(&indio_dev->mlock);
452 ret = ad799x_i2c_read16(st, ad799x_threshold_reg(chan, dir, info), 387 ret = i2c_smbus_read_word_swapped(st->client,
453 &valin); 388 ad799x_threshold_reg(chan, dir, info));
454 mutex_unlock(&indio_dev->mlock); 389 mutex_unlock(&indio_dev->mlock);
455 if (ret < 0) 390 if (ret < 0)
456 return ret; 391 return ret;
457 *val = (valin >> chan->scan_type.shift) & 392 *val = (ret >> chan->scan_type.shift) &
458 RES_MASK(chan->scan_type.realbits); 393 RES_MASK(chan->scan_type.realbits);
459 394
460 return IIO_VAL_INT; 395 return IIO_VAL_INT;
@@ -464,20 +399,18 @@ static irqreturn_t ad799x_event_handler(int irq, void *private)
464{ 399{
465 struct iio_dev *indio_dev = private; 400 struct iio_dev *indio_dev = private;
466 struct ad799x_state *st = iio_priv(private); 401 struct ad799x_state *st = iio_priv(private);
467 u8 status;
468 int i, ret; 402 int i, ret;
469 403
470 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status); 404 ret = i2c_smbus_read_byte_data(st->client, AD7998_ALERT_STAT_REG);
471 if (ret) 405 if (ret <= 0)
472 goto done; 406 goto done;
473 407
474 if (!status) 408 if (i2c_smbus_write_byte_data(st->client, AD7998_ALERT_STAT_REG,
409 AD7998_ALERT_STAT_CLEAR) < 0)
475 goto done; 410 goto done;
476 411
477 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
478
479 for (i = 0; i < 8; i++) { 412 for (i = 0; i < 8; i++) {
480 if (status & (1 << i)) 413 if (ret & (1 << i))
481 iio_push_event(indio_dev, 414 iio_push_event(indio_dev,
482 i & 0x1 ? 415 i & 0x1 ?
483 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 416 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,