/* * Register definitions, as well as various shifts and masks to get at the * individual fields of the registers.
*/ #define AK8975_REG_WIA 0x00 #define AK8975_DEVICE_ID 0x48
/* * Precalculate scale factor (in Gauss units) for each axis and * store in the device data. * * This scale factor is axis-dependent, and is derived from 3 calibration * factors ASA(x), ASA(y), and ASA(z). * * These ASA values are read from the sensor device at start of day, and * cached in the device context struct. * * Adjusting the flux value with the sensitivity adjustment value should be * done via the following formula: * * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 ) * where H is the raw value, ASA is the sensitivity adjustment, and Hadj * is the resultant adjusted value. * * We reduce the formula to: * * Hadj = H * (ASA + 128) / 256 * * H is in the range of -4096 to 4095. The magnetometer has a range of * +-1229uT. To go from the raw value to uT is: * * HuT = H * 1229/4096, or roughly, 3/10. * * Since 1uT = 0.01 gauss, our final scale factor becomes: * * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100 * Hadj = H * ((ASA + 128) * 0.003) / 256 * * Since ASA doesn't change, we cache the resultant scale factor into the * device context in ak8975_setup(). * * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we * multiply the stored scale value by 1e6.
*/ staticlong ak8975_raw_to_gauss(u16 data)
{ return (((long)data + 128) * 3000) / 256;
}
/* * For AK8963 and AK09911, same calculation, but the device is less sensitive: * * H is in the range of +-8190. The magnetometer has a range of * +-4912uT. To go from the raw value to uT is: * * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
*/
/* * For AK09912, same calculation, except the device is more sensitive: * * H is in the range of -32752 to 32752. The magnetometer has a range of * +-4912uT. To go from the raw value to uT is: * * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
*/ staticlong ak09912_raw_to_gauss(u16 data)
{ return (((long)data + 128) * 1500) / 256;
}
/* Enable attached power regulator if any. */ staticint ak8975_power_on(conststruct ak8975_data *data)
{ int ret;
ret = regulator_enable(data->vdd); if (ret) {
dev_warn(&data->client->dev, "Failed to enable specified Vdd supply\n"); return ret;
}
ret = regulator_enable(data->vid); if (ret) {
dev_warn(&data->client->dev, "Failed to enable specified Vid supply\n");
regulator_disable(data->vdd); return ret;
}
gpiod_set_value_cansleep(data->reset_gpiod, 0);
/* * According to the datasheet the power supply rise time is 200us * and the minimum wait time before mode setting is 100us, in * total 300us. Add some margin and say minimum 500us here.
*/
usleep_range(500, 1000); return 0;
}
/* Disable attached power regulator if any. */ staticvoid ak8975_power_off(conststruct ak8975_data *data)
{
gpiod_set_value_cansleep(data->reset_gpiod, 1);
/* * Return 0 if the i2c device is the one we expect. * return a negative error number otherwise
*/ staticint ak8975_who_i_am(struct i2c_client *client, enum asahi_compass_chipset type)
{
u8 wia_val[2]; int ret;
if (wia_val[0] != AK8975_DEVICE_ID) return -ENODEV;
switch (type) { case AK8975: case AK8963: return 0; case AK09911: if (wia_val[1] == AK09911_DEVICE_ID) return 0; break; case AK09912: if (wia_val[1] == AK09912_DEVICE_ID) return 0; break; case AK09916: if (wia_val[1] == AK09916_DEVICE_ID) return 0; break; case AK09918: if (wia_val[1] == AK09918_DEVICE_ID) return 0; break;
}
dev_info(&client->dev, "Device ID %x is unknown.\n", wia_val[1]); /* * Let driver to probe on unknown id for support more register * compatible variants.
*/ return 0;
}
/* * Helper function to write to CNTL register.
*/ staticint ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
{
u8 regval; int ret;
/* * Perform some start-of-day setup, including reading the asa calibration * values and caching them.
*/ staticint ak8975_setup(struct i2c_client *client)
{ struct iio_dev *indio_dev = i2c_get_clientdata(client); struct ak8975_data *data = iio_priv(indio_dev); int ret;
/* Write the fused rom access mode. */
ret = ak8975_set_mode(data, FUSE_ROM); if (ret < 0) {
dev_err(&client->dev, "Error in setting fuse access mode\n"); return ret;
}
/* Get asa data and store in the device data. */
ret = i2c_smbus_read_i2c_block_data_or_emulated(
client, data->def->ctrl_regs[ASA_BASE],
3, data->asa); if (ret < 0) {
dev_err(&client->dev, "Not able to read asa data\n"); return ret;
}
/* After reading fuse ROM data set power-down mode */
ret = ak8975_set_mode(data, POWER_DOWN); if (ret < 0) {
dev_err(&client->dev, "Error in setting power-down mode\n"); return ret;
}
if (data->eoc_gpiod || client->irq > 0) {
ret = ak8975_setup_irq(data); if (ret < 0) {
dev_err(&client->dev, "Error setting data ready interrupt\n"); return ret;
}
}
/* Wait for the conversion to complete. */ while (timeout_ms) {
msleep(AK8975_CONVERSION_DONE_POLL_TIME); if (gpiod_get_value(data->eoc_gpiod)) break;
timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
} if (!timeout_ms) {
dev_err(&client->dev, "Conversion timeout happened\n"); return -EINVAL;
}
ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]); if (ret < 0)
dev_err(&client->dev, "Error in reading ST1\n");
/* Wait for the conversion to complete. */ while (timeout_ms) {
msleep(AK8975_CONVERSION_DONE_POLL_TIME);
ret = i2c_smbus_read_byte_data(client,
data->def->ctrl_regs[ST1]); if (ret < 0) {
dev_err(&client->dev, "Error in reading ST1\n"); return ret;
}
read_status = ret; if (read_status) break;
timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
} if (!timeout_ms) {
dev_err(&client->dev, "Conversion timeout happened\n"); return -EINVAL;
}
return read_status;
}
/* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */ staticint wait_conversion_complete_interrupt(struct ak8975_data *data)
{ int ret;
ret = wait_event_timeout(data->data_ready_queue,
test_bit(0, &data->flags),
AK8975_DATA_READY_TIMEOUT);
clear_bit(0, &data->flags);
return ret > 0 ? 0 : -ETIME;
}
staticint ak8975_start_read_axis(struct ak8975_data *data, conststruct i2c_client *client)
{ /* Set up the device for taking a sample. */ int ret = ak8975_set_mode(data, MODE_ONCE);
if (ret < 0) {
dev_err(&client->dev, "Error in setting operating mode\n"); return ret;
}
/* Wait for the conversion to complete. */ if (data->eoc_irq)
ret = wait_conversion_complete_interrupt(data); elseif (data->eoc_gpiod)
ret = wait_conversion_complete_gpio(data); else
ret = wait_conversion_complete_polled(data); if (ret < 0) return ret;
/* Return with zero if the data is ready. */ return !data->def->ctrl_regs[ST1_DRDY];
}
/* Retrieve raw flux value for one of the x, y, or z axis. */ staticint ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
{ struct ak8975_data *data = iio_priv(indio_dev); conststruct i2c_client *client = data->client; conststruct ak_def *def = data->def;
__le16 rval;
u16 buff; int ret;
pm_runtime_get_sync(&data->client->dev);
mutex_lock(&data->lock);
ret = ak8975_start_read_axis(data, client); if (ret) gotoexit;
ret = i2c_smbus_read_i2c_block_data_or_emulated(
client, def->data_regs[index], sizeof(rval), (u8*)&rval); if (ret < 0) gotoexit;
/* Read out ST2 for release lock on measurment data. */
ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST2]); if (ret < 0) {
dev_err(&client->dev, "Error in reading ST2\n"); gotoexit;
}
if (ret & (data->def->ctrl_masks[ST2_DERR] |
data->def->ctrl_masks[ST2_HOFL])) {
dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
ret = -EINVAL; gotoexit;
}
ret = ak8975_start_read_axis(data, client); if (ret) goto unlock;
/* * For each axis, read the flux value from the appropriate register * (the register is specified in the iio device attributes).
*/
ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
def->data_regs[0],
3 * sizeof(fval[0]),
(u8 *)fval); if (ret < 0) goto unlock;
/* * Grab and set up the supplied GPIO. * We may not have a GPIO based IRQ to scan, that is fine, we will * poll if so.
*/
eoc_gpiod = devm_gpiod_get_optional(&client->dev, NULL, GPIOD_IN); if (IS_ERR(eoc_gpiod)) return PTR_ERR(eoc_gpiod); if (eoc_gpiod)
gpiod_set_consumer_name(eoc_gpiod, "ak_8975");
/* * According to AK09911 datasheet, if reset GPIO is provided then * deassert reset on ak8975_power_on() and assert reset on * ak8975_power_off().
*/
reset_gpiod = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH); if (IS_ERR(reset_gpiod)) return PTR_ERR(reset_gpiod);
/* Register with IIO */
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); if (indio_dev == NULL) return -ENOMEM;
data = iio_priv(indio_dev);
i2c_set_clientdata(client, indio_dev);
/* Enable runtime PM */
pm_runtime_get_noresume(&client->dev);
pm_runtime_set_active(&client->dev);
pm_runtime_enable(&client->dev); /* * The device comes online in 500us, so add two orders of magnitude * of delay before autosuspending: 50 ms.
*/
pm_runtime_set_autosuspend_delay(&client->dev, 50);
pm_runtime_use_autosuspend(&client->dev);
pm_runtime_put(&client->dev);
/* Set the device in power down if it wasn't already */
ret = ak8975_set_mode(data, POWER_DOWN); if (ret < 0) {
dev_err(&client->dev, "Error in setting power-down mode\n"); return ret;
} /* Next cut the regulators */
ak8975_power_off(data);
/* Take up the regulators */
ak8975_power_on(data); /* * We come up in powered down mode, the reading routines will * put us in the mode to read values later.
*/
ret = ak8975_set_mode(data, POWER_DOWN); if (ret < 0) {
dev_err(&client->dev, "Error in setting power-down mode\n"); return ret;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.