iio: accel: mma9553: refactor mma9553_read_raw
Refactor code for simplicity and clarity. Signed-off-by: Irina Tirdea <irina.tirdea@intel.com> Suggested-by: Hartmut Knaack <knaack.h@gmx.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
This commit is contained in:
parent
996ba51459
commit
334efd076d
1 changed files with 33 additions and 68 deletions
|
@ -441,6 +441,32 @@ static int mma9553_init(struct mma9553_data *data)
|
||||||
return mma9551_set_device_state(data->client, true);
|
return mma9551_set_device_state(data->client, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int mma9553_read_status_word(struct mma9553_data *data, u16 reg,
|
||||||
|
u16 *tmp)
|
||||||
|
{
|
||||||
|
bool powered_on;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The HW only counts steps and other dependent
|
||||||
|
* parameters (speed, distance, calories, activity)
|
||||||
|
* if power is on (from enabling an event or the
|
||||||
|
* step counter).
|
||||||
|
*/
|
||||||
|
powered_on = mma9553_is_any_event_enabled(data, false, 0) ||
|
||||||
|
data->stepcnt_enabled;
|
||||||
|
if (!powered_on) {
|
||||||
|
dev_err(&data->client->dev, "No channels enabled\n");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
mutex_lock(&data->mutex);
|
||||||
|
ret = mma9551_read_status_word(data->client, MMA9551_APPID_PEDOMETER,
|
||||||
|
reg, tmp);
|
||||||
|
mutex_unlock(&data->mutex);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
static int mma9553_read_raw(struct iio_dev *indio_dev,
|
static int mma9553_read_raw(struct iio_dev *indio_dev,
|
||||||
struct iio_chan_spec const *chan,
|
struct iio_chan_spec const *chan,
|
||||||
int *val, int *val2, long mask)
|
int *val, int *val2, long mask)
|
||||||
|
@ -449,70 +475,30 @@ static int mma9553_read_raw(struct iio_dev *indio_dev,
|
||||||
int ret;
|
int ret;
|
||||||
u16 tmp;
|
u16 tmp;
|
||||||
u8 activity;
|
u8 activity;
|
||||||
bool powered_on;
|
|
||||||
|
|
||||||
switch (mask) {
|
switch (mask) {
|
||||||
case IIO_CHAN_INFO_PROCESSED:
|
case IIO_CHAN_INFO_PROCESSED:
|
||||||
switch (chan->type) {
|
switch (chan->type) {
|
||||||
case IIO_STEPS:
|
case IIO_STEPS:
|
||||||
/*
|
ret = mma9553_read_status_word(data,
|
||||||
* The HW only counts steps and other dependent
|
|
||||||
* parameters (speed, distance, calories, activity)
|
|
||||||
* if power is on (from enabling an event or the
|
|
||||||
* step counter).
|
|
||||||
*/
|
|
||||||
powered_on =
|
|
||||||
mma9553_is_any_event_enabled(data, false, 0) ||
|
|
||||||
data->stepcnt_enabled;
|
|
||||||
if (!powered_on) {
|
|
||||||
dev_err(&data->client->dev,
|
|
||||||
"No channels enabled\n");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
mutex_lock(&data->mutex);
|
|
||||||
ret = mma9551_read_status_word(data->client,
|
|
||||||
MMA9551_APPID_PEDOMETER,
|
|
||||||
MMA9553_REG_STEPCNT,
|
MMA9553_REG_STEPCNT,
|
||||||
&tmp);
|
&tmp);
|
||||||
mutex_unlock(&data->mutex);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
*val = tmp;
|
*val = tmp;
|
||||||
return IIO_VAL_INT;
|
return IIO_VAL_INT;
|
||||||
case IIO_DISTANCE:
|
case IIO_DISTANCE:
|
||||||
powered_on =
|
ret = mma9553_read_status_word(data,
|
||||||
mma9553_is_any_event_enabled(data, false, 0) ||
|
|
||||||
data->stepcnt_enabled;
|
|
||||||
if (!powered_on) {
|
|
||||||
dev_err(&data->client->dev,
|
|
||||||
"No channels enabled\n");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
mutex_lock(&data->mutex);
|
|
||||||
ret = mma9551_read_status_word(data->client,
|
|
||||||
MMA9551_APPID_PEDOMETER,
|
|
||||||
MMA9553_REG_DISTANCE,
|
MMA9553_REG_DISTANCE,
|
||||||
&tmp);
|
&tmp);
|
||||||
mutex_unlock(&data->mutex);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
*val = tmp;
|
*val = tmp;
|
||||||
return IIO_VAL_INT;
|
return IIO_VAL_INT;
|
||||||
case IIO_ACTIVITY:
|
case IIO_ACTIVITY:
|
||||||
powered_on =
|
ret = mma9553_read_status_word(data,
|
||||||
mma9553_is_any_event_enabled(data, false, 0) ||
|
|
||||||
data->stepcnt_enabled;
|
|
||||||
if (!powered_on) {
|
|
||||||
dev_err(&data->client->dev,
|
|
||||||
"No channels enabled\n");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
mutex_lock(&data->mutex);
|
|
||||||
ret = mma9551_read_status_word(data->client,
|
|
||||||
MMA9551_APPID_PEDOMETER,
|
|
||||||
MMA9553_REG_STATUS,
|
MMA9553_REG_STATUS,
|
||||||
&tmp);
|
&tmp);
|
||||||
mutex_unlock(&data->mutex);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
@ -537,38 +523,17 @@ static int mma9553_read_raw(struct iio_dev *indio_dev,
|
||||||
case IIO_VELOCITY: /* m/h */
|
case IIO_VELOCITY: /* m/h */
|
||||||
if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
|
if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
powered_on =
|
ret = mma9553_read_status_word(data,
|
||||||
mma9553_is_any_event_enabled(data, false, 0) ||
|
MMA9553_REG_SPEED,
|
||||||
data->stepcnt_enabled;
|
&tmp);
|
||||||
if (!powered_on) {
|
|
||||||
dev_err(&data->client->dev,
|
|
||||||
"No channels enabled\n");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
mutex_lock(&data->mutex);
|
|
||||||
ret = mma9551_read_status_word(data->client,
|
|
||||||
MMA9551_APPID_PEDOMETER,
|
|
||||||
MMA9553_REG_SPEED, &tmp);
|
|
||||||
mutex_unlock(&data->mutex);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
*val = tmp;
|
*val = tmp;
|
||||||
return IIO_VAL_INT;
|
return IIO_VAL_INT;
|
||||||
case IIO_ENERGY: /* Cal or kcal */
|
case IIO_ENERGY: /* Cal or kcal */
|
||||||
powered_on =
|
ret = mma9553_read_status_word(data,
|
||||||
mma9553_is_any_event_enabled(data, false, 0) ||
|
|
||||||
data->stepcnt_enabled;
|
|
||||||
if (!powered_on) {
|
|
||||||
dev_err(&data->client->dev,
|
|
||||||
"No channels enabled\n");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
mutex_lock(&data->mutex);
|
|
||||||
ret = mma9551_read_status_word(data->client,
|
|
||||||
MMA9551_APPID_PEDOMETER,
|
|
||||||
MMA9553_REG_CALORIES,
|
MMA9553_REG_CALORIES,
|
||||||
&tmp);
|
&tmp);
|
||||||
mutex_unlock(&data->mutex);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
*val = tmp;
|
*val = tmp;
|
||||||
|
|
Loading…
Add table
Reference in a new issue