diff options
author | Adriana Reus <adriana.reus@intel.com> | 2015-12-14 07:24:47 -0500 |
---|---|---|
committer | Jonathan Cameron <jic23@kernel.org> | 2015-12-22 13:07:08 -0500 |
commit | 9b1de75b5deea3e56bfb76d19d71ec599fcc803b (patch) | |
tree | c81bc44cc4f6f7b8533ba5bb199da5cd3347c97b | |
parent | b6695254f800698faee4f30a8f0b199459ebeafe (diff) |
iio: light: us5182d: Refactor read_raw function
A bit of refactoring for better readability.
Moved and slightly reorganized all the activity necessary for reading als
and proximity into a different function. This way the switch in read raw
becomes clearer and more compact.
Signed-off-by: Adriana Reus <adriana.reus@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
-rw-r--r-- | drivers/iio/light/us5182d.c | 155 |
1 files changed, 78 insertions, 77 deletions
diff --git a/drivers/iio/light/us5182d.c b/drivers/iio/light/us5182d.c index 213f7785095f..45bc2f742f46 100644 --- a/drivers/iio/light/us5182d.c +++ b/drivers/iio/light/us5182d.c | |||
@@ -203,23 +203,6 @@ static const struct iio_chan_spec us5182d_channels[] = { | |||
203 | } | 203 | } |
204 | }; | 204 | }; |
205 | 205 | ||
206 | static int us5182d_get_als(struct us5182d_data *data) | ||
207 | { | ||
208 | int ret; | ||
209 | unsigned long result; | ||
210 | |||
211 | ret = i2c_smbus_read_word_data(data->client, | ||
212 | US5182D_REG_ADL); | ||
213 | if (ret < 0) | ||
214 | return ret; | ||
215 | |||
216 | result = ret * data->ga / US5182D_GA_RESOLUTION; | ||
217 | if (result > 0xffff) | ||
218 | result = 0xffff; | ||
219 | |||
220 | return result; | ||
221 | } | ||
222 | |||
223 | static int us5182d_oneshot_en(struct us5182d_data *data) | 206 | static int us5182d_oneshot_en(struct us5182d_data *data) |
224 | { | 207 | { |
225 | int ret; | 208 | int ret; |
@@ -324,6 +307,39 @@ static int us5182d_px_enable(struct us5182d_data *data) | |||
324 | return 0; | 307 | return 0; |
325 | } | 308 | } |
326 | 309 | ||
310 | static int us5182d_get_als(struct us5182d_data *data) | ||
311 | { | ||
312 | int ret; | ||
313 | unsigned long result; | ||
314 | |||
315 | ret = us5182d_als_enable(data); | ||
316 | if (ret < 0) | ||
317 | return ret; | ||
318 | |||
319 | ret = i2c_smbus_read_word_data(data->client, | ||
320 | US5182D_REG_ADL); | ||
321 | if (ret < 0) | ||
322 | return ret; | ||
323 | |||
324 | result = ret * data->ga / US5182D_GA_RESOLUTION; | ||
325 | if (result > 0xffff) | ||
326 | result = 0xffff; | ||
327 | |||
328 | return result; | ||
329 | } | ||
330 | |||
331 | static int us5182d_get_px(struct us5182d_data *data) | ||
332 | { | ||
333 | int ret; | ||
334 | |||
335 | ret = us5182d_px_enable(data); | ||
336 | if (ret < 0) | ||
337 | return ret; | ||
338 | |||
339 | return i2c_smbus_read_word_data(data->client, | ||
340 | US5182D_REG_PDL); | ||
341 | } | ||
342 | |||
327 | static int us5182d_shutdown_en(struct us5182d_data *data, u8 state) | 343 | static int us5182d_shutdown_en(struct us5182d_data *data, u8 state) |
328 | { | 344 | { |
329 | int ret; | 345 | int ret; |
@@ -370,6 +386,46 @@ static int us5182d_set_power_state(struct us5182d_data *data, bool on) | |||
370 | return ret; | 386 | return ret; |
371 | } | 387 | } |
372 | 388 | ||
389 | static int us5182d_read_value(struct us5182d_data *data, | ||
390 | struct iio_chan_spec const *chan) | ||
391 | { | ||
392 | int ret, value; | ||
393 | |||
394 | mutex_lock(&data->lock); | ||
395 | |||
396 | if (data->power_mode == US5182D_ONESHOT) { | ||
397 | ret = us5182d_oneshot_en(data); | ||
398 | if (ret < 0) | ||
399 | goto out_err; | ||
400 | } | ||
401 | |||
402 | ret = us5182d_set_power_state(data, true); | ||
403 | if (ret < 0) | ||
404 | goto out_err; | ||
405 | |||
406 | if (chan->type == IIO_LIGHT) | ||
407 | ret = us5182d_get_als(data); | ||
408 | else | ||
409 | ret = us5182d_get_px(data); | ||
410 | if (ret < 0) | ||
411 | goto out_poweroff; | ||
412 | |||
413 | value = ret; | ||
414 | |||
415 | ret = us5182d_set_power_state(data, false); | ||
416 | if (ret < 0) | ||
417 | goto out_err; | ||
418 | |||
419 | mutex_unlock(&data->lock); | ||
420 | return value; | ||
421 | |||
422 | out_poweroff: | ||
423 | us5182d_set_power_state(data, false); | ||
424 | out_err: | ||
425 | mutex_unlock(&data->lock); | ||
426 | return ret; | ||
427 | } | ||
428 | |||
373 | static int us5182d_read_raw(struct iio_dev *indio_dev, | 429 | static int us5182d_read_raw(struct iio_dev *indio_dev, |
374 | struct iio_chan_spec const *chan, int *val, | 430 | struct iio_chan_spec const *chan, int *val, |
375 | int *val2, long mask) | 431 | int *val2, long mask) |
@@ -379,76 +435,21 @@ static int us5182d_read_raw(struct iio_dev *indio_dev, | |||
379 | 435 | ||
380 | switch (mask) { | 436 | switch (mask) { |
381 | case IIO_CHAN_INFO_RAW: | 437 | case IIO_CHAN_INFO_RAW: |
382 | switch (chan->type) { | 438 | ret = us5182d_read_value(data, chan); |
383 | case IIO_LIGHT: | 439 | if (ret < 0) |
384 | mutex_lock(&data->lock); | 440 | return ret; |
385 | if (data->power_mode == US5182D_ONESHOT) { | 441 | *val = ret; |
386 | ret = us5182d_oneshot_en(data); | 442 | return IIO_VAL_INT; |
387 | if (ret < 0) | ||
388 | goto out_err; | ||
389 | } | ||
390 | ret = us5182d_set_power_state(data, true); | ||
391 | if (ret < 0) | ||
392 | goto out_err; | ||
393 | ret = us5182d_als_enable(data); | ||
394 | if (ret < 0) | ||
395 | goto out_poweroff; | ||
396 | ret = us5182d_get_als(data); | ||
397 | if (ret < 0) | ||
398 | goto out_poweroff; | ||
399 | *val = ret; | ||
400 | ret = us5182d_set_power_state(data, false); | ||
401 | if (ret < 0) | ||
402 | goto out_err; | ||
403 | mutex_unlock(&data->lock); | ||
404 | return IIO_VAL_INT; | ||
405 | case IIO_PROXIMITY: | ||
406 | mutex_lock(&data->lock); | ||
407 | if (data->power_mode == US5182D_ONESHOT) { | ||
408 | ret = us5182d_oneshot_en(data); | ||
409 | if (ret < 0) | ||
410 | goto out_err; | ||
411 | } | ||
412 | ret = us5182d_set_power_state(data, true); | ||
413 | if (ret < 0) | ||
414 | goto out_err; | ||
415 | ret = us5182d_px_enable(data); | ||
416 | if (ret < 0) | ||
417 | goto out_poweroff; | ||
418 | ret = i2c_smbus_read_word_data(data->client, | ||
419 | US5182D_REG_PDL); | ||
420 | if (ret < 0) | ||
421 | goto out_poweroff; | ||
422 | *val = ret; | ||
423 | ret = us5182d_set_power_state(data, false); | ||
424 | if (ret < 0) | ||
425 | goto out_err; | ||
426 | mutex_unlock(&data->lock); | ||
427 | return IIO_VAL_INT; | ||
428 | default: | ||
429 | return -EINVAL; | ||
430 | } | ||
431 | |||
432 | case IIO_CHAN_INFO_SCALE: | 443 | case IIO_CHAN_INFO_SCALE: |
433 | ret = i2c_smbus_read_byte_data(data->client, US5182D_REG_CFG1); | 444 | ret = i2c_smbus_read_byte_data(data->client, US5182D_REG_CFG1); |
434 | if (ret < 0) | 445 | if (ret < 0) |
435 | return ret; | 446 | return ret; |
436 | |||
437 | *val = 0; | 447 | *val = 0; |
438 | *val2 = us5182d_scales[ret & US5182D_AGAIN_MASK]; | 448 | *val2 = us5182d_scales[ret & US5182D_AGAIN_MASK]; |
439 | |||
440 | return IIO_VAL_INT_PLUS_MICRO; | 449 | return IIO_VAL_INT_PLUS_MICRO; |
441 | default: | 450 | default: |
442 | return -EINVAL; | 451 | return -EINVAL; |
443 | } | 452 | } |
444 | |||
445 | return -EINVAL; | ||
446 | |||
447 | out_poweroff: | ||
448 | us5182d_set_power_state(data, false); | ||
449 | out_err: | ||
450 | mutex_unlock(&data->lock); | ||
451 | return ret; | ||
452 | } | 453 | } |
453 | 454 | ||
454 | /** | 455 | /** |