/* * Emits the raw flux value for the x, y, or z axis. */ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val) { struct ak8975_data *data = iio_priv(indio_dev); struct i2c_client *client = data->client; u16 meas_reg; s16 raw; int ret; mutex_lock(&data->lock); /* Set up the device for taking a sample. */ ret = ak8975_write_data(client, AK8975_REG_CNTL, AK8975_REG_CNTL_MODE_ONCE, AK8975_REG_CNTL_MODE_MASK, AK8975_REG_CNTL_MODE_SHIFT); if (ret < 0) { dev_err(&client->dev, "Error in setting operating mode\n"); goto exit; } /* Wait for the conversion to complete. */ if (gpio_is_valid(data->eoc_gpio)) ret = wait_conversion_complete_gpio(data); else ret = wait_conversion_complete_polled(data); if (ret < 0) goto exit; if (ret & AK8975_REG_ST1_DRDY_MASK) { ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2); if (ret < 0) { dev_err(&client->dev, "Error in reading ST2\n"); goto exit; } if (ret & (AK8975_REG_ST2_DERR_MASK | AK8975_REG_ST2_HOFL_MASK)) { dev_err(&client->dev, "ST2 status error 0x%x\n", ret); ret = -EINVAL; goto exit; } } /* Read the flux value from the appropriate register (the register is specified in the iio device attributes). */ ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]); if (ret < 0) { dev_err(&client->dev, "Read axis data fails\n"); goto exit; } meas_reg = ret; mutex_unlock(&data->lock); /* Endian conversion of the measured values. */ raw = (s16) (le16_to_cpu(meas_reg)); /* Clamp to valid range. */ raw = clamp_t(s16, raw, -4096, 4095); *val = raw; return IIO_VAL_INT; exit: mutex_unlock(&data->lock); return ret; }
/* * Emits the raw flux value for the x, y, or z axis. */ static ssize_t show_raw(struct device *dev, struct device_attribute *devattr, char *buf) { struct iio_dev *indio_dev = dev_get_drvdata(dev); struct ak8975_data *data = indio_dev->dev_data; struct i2c_client *client = data->client; struct iio_dev_attr *this_attr = to_iio_dev_attr(devattr); u16 meas_reg; s16 raw; u8 read_status; int ret; mutex_lock(&data->lock); if (data->mode == 0) { dev_err(&client->dev, "Operating mode is in power down mode\n"); ret = -EBUSY; goto exit; } /* Set up the device for taking a sample. */ ret = ak8975_write_data(client, AK8975_REG_CNTL, AK8975_REG_CNTL_MODE_ONCE, AK8975_REG_CNTL_MODE_MASK, AK8975_REG_CNTL_MODE_SHIFT); if (ret < 0) { dev_err(&client->dev, "Error in setting operating mode\n"); goto exit; } /* Wait for the conversion to complete. */ if (data->eoc_gpio) ret = wait_conversion_complete_gpio(data); else ret = wait_conversion_complete_polled(data); if (ret < 0) goto exit; read_status = ret; if (read_status & AK8975_REG_ST1_DRDY_MASK) { ret = ak8975_read_data(client, AK8975_REG_ST2, 1, &read_status); if (ret < 0) { dev_err(&client->dev, "Error in reading ST2\n"); goto exit; } if (read_status & (AK8975_REG_ST2_DERR_MASK | AK8975_REG_ST2_HOFL_MASK)) { dev_err(&client->dev, "ST2 status error 0x%x\n", read_status); ret = -EINVAL; goto exit; } } /* Read the flux value from the appropriate register (the register is specified in the iio device attributes). */ ret = ak8975_read_data(client, this_attr->address, 2, (u8 *)&meas_reg); if (ret < 0) { dev_err(&client->dev, "Read axis data fails\n"); goto exit; } mutex_unlock(&data->lock); /* Endian conversion of the measured values. */ raw = (s16) (le16_to_cpu(meas_reg)); /* Clamp to valid range. */ raw = clamp_t(s16, raw, -4096, 4095); return sprintf(buf, "%d\n", raw); exit: mutex_unlock(&data->lock); return ret; }