/* Slope duration in terms of number of samples */ #define BMC150_ACCEL_DEF_SLOPE_DURATION 1 /* in terms of multiples of g's/LSB, based on range */ #define BMC150_ACCEL_DEF_SLOPE_THRESHOLD 1
ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_LPW, lpw_bits); if (ret < 0) {
dev_err(dev, "Error writing reg_pmu_lpw\n"); return ret;
}
return 0;
}
staticint bmc150_accel_set_bw(struct bmc150_accel_data *data, int val, int val2)
{ int i; int ret;
for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) { if (bmc150_accel_samp_freq_table[i].val == val &&
bmc150_accel_samp_freq_table[i].val2 == val2) {
ret = regmap_write(data->regmap,
BMC150_ACCEL_REG_PMU_BW,
bmc150_accel_samp_freq_table[i].bw_bits); if (ret < 0) return ret;
staticint bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val, int *val2)
{ int i;
for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) { if (bmc150_accel_samp_freq_table[i].bw_bits == data->bw_bits) {
*val = bmc150_accel_samp_freq_table[i].val;
*val2 = bmc150_accel_samp_freq_table[i].val2; return IIO_VAL_INT_PLUS_MICRO;
}
}
return -EINVAL;
}
#ifdef CONFIG_PM staticint bmc150_accel_get_startup_times(struct bmc150_accel_data *data)
{ int i;
for (i = 0; i < ARRAY_SIZE(bmc150_accel_sample_upd_time); ++i) { if (bmc150_accel_sample_upd_time[i].bw_bits == data->bw_bits) return bmc150_accel_sample_upd_time[i].msec;
}
#ifdef CONFIG_ACPI /* * Support for getting accelerometer information from BOSC0200 ACPI nodes. * * There are 2 variants of the BOSC0200 ACPI node. Some 2-in-1s with 360 degree * hinges declare 2 I2C ACPI-resources for 2 accelerometers, 1 in the display * and 1 in the base of the 2-in-1. On these 2-in-1s the ROMS ACPI object * contains the mount-matrix for the sensor in the display and ROMK contains * the mount-matrix for the sensor in the base. On devices using a single * sensor there is a ROTM ACPI object which contains the mount-matrix. * * Here is an incomplete list of devices known to use 1 of these setups: * * Yoga devices with 2 accelerometers using ROMS + ROMK for the mount-matrices: * Lenovo Thinkpad Yoga 11e 3th gen * Lenovo Thinkpad Yoga 11e 4th gen * * Tablets using a single accelerometer using ROTM for the mount-matrix: * Chuwi Hi8 Pro (CWI513) * Chuwi Vi8 Plus (CWI519) * Chuwi Hi13 * Irbis TW90 * Jumper EZpad mini 3 * Onda V80 plus * Predia Basic Tablet
*/ staticbool bmc150_apply_bosc0200_acpi_orientation(struct device *dev, struct iio_mount_matrix *orientation)
{ struct iio_dev *indio_dev = dev_get_drvdata(dev);
acpi_handle handle = ACPI_HANDLE(dev); char *name, *alt_name, *label;
/* * For now we map all interrupts to the same output pin. * However, some boards may have just INT2 (and not INT1) connected, * so we try to detect which IRQ it is based on the interrupt-names. * Without interrupt-names, we assume the irq belongs to INT1.
*/
irq_info = bmc150_accel_interrupts_int1; if (data->type == BOSCH_BMC156 ||
irq == fwnode_irq_get_byname(dev_fwnode(dev), "INT2"))
irq_info = bmc150_accel_interrupts_int2;
for (i = 0; i < BMC150_ACCEL_INTERRUPTS; i++)
data->interrupts[i].info = &irq_info[i];
}
staticint bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i, bool state)
{ struct device *dev = regmap_get_device(data->regmap); struct bmc150_accel_interrupt *intr = &data->interrupts[i]; conststruct bmc150_accel_interrupt_info *info = intr->info; int ret;
if (state) { if (atomic_inc_return(&intr->users) > 1) return 0;
} else { if (atomic_dec_return(&intr->users) > 0) return 0;
}
/* * We will expect the enable and disable to do operation in reverse * order. This will happen here anyway, as our resume operation uses * sync mode runtime pm calls. The suspend operation will be delayed * by autosuspend delay. * So the disable operation will still happen in reverse order of * enable operation. When runtime pm is disabled the mode is always on, * so sequence doesn't matter.
*/
ret = bmc150_accel_set_power_state(data, state); if (ret < 0) return ret;
/* map the interrupt to the appropriate pins */
ret = regmap_update_bits(data->regmap, info->map_reg, info->map_bitmask,
(state ? info->map_bitmask : 0)); if (ret < 0) {
dev_err(dev, "Error updating reg_int_map\n"); goto out_fix_power_state;
}
/* enable/disable the interrupt */
ret = regmap_update_bits(data->regmap, info->en_reg, info->en_bitmask,
(state ? info->en_bitmask : 0)); if (ret < 0) {
dev_err(dev, "Error updating reg_int_en\n"); goto out_fix_power_state;
}
staticint bmc150_accel_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask)
{ struct bmc150_accel_data *data = iio_priv(indio_dev); int ret;
switch (mask) { case IIO_CHAN_INFO_RAW: switch (chan->type) { case IIO_TEMP: return bmc150_accel_get_temp(data, val); case IIO_ACCEL: if (iio_buffer_enabled(indio_dev)) return -EBUSY; else return bmc150_accel_get_axis(data, chan, val); default: return -EINVAL;
} case IIO_CHAN_INFO_OFFSET: if (chan->type == IIO_TEMP) {
*val = BMC150_ACCEL_TEMP_CENTER_VAL; return IIO_VAL_INT;
} else { return -EINVAL;
} case IIO_CHAN_INFO_SCALE:
*val = 0; switch (chan->type) { case IIO_TEMP:
*val2 = 500000; return IIO_VAL_INT_PLUS_MICRO; case IIO_ACCEL:
{ int i; conststruct bmc150_scale_info *si; int st_size = ARRAY_SIZE(data->chip_info->scale_table);
for (i = 0; i < st_size; ++i) {
si = &data->chip_info->scale_table[i]; if (si->reg_range == data->range) {
*val2 = si->scale; return IIO_VAL_INT_PLUS_MICRO;
}
} return -EINVAL;
} default: return -EINVAL;
} case IIO_CHAN_INFO_SAMP_FREQ:
mutex_lock(&data->mutex);
ret = bmc150_accel_get_bw(data, val, val2);
mutex_unlock(&data->mutex); return ret; default: return -EINVAL;
}
}
staticint bmc150_accel_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask)
{ struct bmc150_accel_data *data = iio_priv(indio_dev); int ret;
switch (mask) { case IIO_CHAN_INFO_SAMP_FREQ:
mutex_lock(&data->mutex);
ret = bmc150_accel_set_bw(data, val, val2);
mutex_unlock(&data->mutex); break; case IIO_CHAN_INFO_SCALE: if (val) return -EINVAL;
mutex_lock(&data->mutex);
ret = bmc150_accel_set_scale(data, val2);
mutex_unlock(&data->mutex); return ret; default:
ret = -EINVAL;
}
/* * We must read at least one full frame in one burst, otherwise the rest of the * frame data is discarded.
*/ staticint bmc150_accel_fifo_transfer(struct bmc150_accel_data *data, char *buffer, int samples)
{ struct device *dev = regmap_get_device(data->regmap); int sample_length = 3 * 2; int ret; int total_length = samples * sample_length;
ret = regmap_raw_read(data->regmap, BMC150_ACCEL_REG_FIFO_DATA,
buffer, total_length); if (ret)
dev_err(dev, "Error transferring data from fifo: %d\n", ret);
ret = regmap_read(data->regmap, BMC150_ACCEL_REG_FIFO_STATUS, &val); if (ret < 0) {
dev_err(dev, "Error reading reg_fifo_status\n"); return ret;
}
count = val & 0x7F;
if (!count) return 0;
/* * If we getting called from IRQ handler we know the stored timestamp is * fairly accurate for the last stored sample. Otherwise, if we are * called as a result of a read operation from userspace and hence * before the watermark interrupt was triggered, take a timestamp * now. We can fall anywhere in between two samples so the error in this * case is at most one sample period.
*/ if (!irq) {
data->old_timestamp = data->timestamp;
data->timestamp = iio_get_time_ns(indio_dev);
}
/* * Approximate timestamps for each of the sample based on the sampling * frequency, timestamp for last sample and number of samples. * * Note that we can't use the current bandwidth settings to compute the * sample period because the sample rate varies with the device * (e.g. between 31.70ms to 32.20ms for a bandwidth of 15.63HZ). That * small variation adds when we store a large number of samples and * creates significant jitter between the last and first samples in * different batches (e.g. 32ms vs 21ms). * * To avoid this issue we compute the actual sample period ourselves * based on the timestamp delta between the last two flush operations.
*/
sample_period = (data->timestamp - data->old_timestamp);
do_div(sample_period, count);
tstamp = data->timestamp - (count - 1) * sample_period;
if (samples && count > samples)
count = samples;
ret = bmc150_accel_fifo_transfer(data, (u8 *)buffer, count); if (ret) return ret;
/* * Ideally we want the IIO core to handle the demux when running in fifo * mode but not when running in triggered buffer mode. Unfortunately * this does not seem to be possible, so stick with driver demux for * now.
*/ for (i = 0; i < count; i++) { int j, bit;
staticvoid bmc150_accel_unregister_triggers(struct bmc150_accel_data *data, int from)
{ int i;
for (i = from; i >= 0; i--) { if (data->triggers[i].indio_trig) {
iio_trigger_unregister(data->triggers[i].indio_trig);
data->triggers[i].indio_trig = NULL;
}
}
}
staticint bmc150_accel_triggers_setup(struct iio_dev *indio_dev, struct bmc150_accel_data *data)
{ struct device *dev = regmap_get_device(data->regmap); int i, ret;
for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) { struct bmc150_accel_trigger *t = &data->triggers[i];
t->indio_trig = devm_iio_trigger_alloc(dev,
bmc150_accel_triggers[i].name,
indio_dev->name,
iio_device_id(indio_dev)); if (!t->indio_trig) {
ret = -ENOMEM; break;
}
/* * Reset chip to get it in a known good state. A delay of 1.8ms after * reset is required according to the data sheets of supported chips.
*/
regmap_write(data->regmap, BMC150_ACCEL_REG_RESET,
BMC150_ACCEL_RESET_VAL);
usleep_range(1800, 2500);
ret = regmap_read(data->regmap, BMC150_ACCEL_REG_CHIP_ID, &val); if (ret < 0) {
dev_err(dev, "Error: Reading chip id\n"); return ret;
}
dev_dbg(dev, "Chip Id %x\n", val); for (i = 0; i < ARRAY_SIZE(bmc150_accel_chip_info_tbl); i++) { if (bmc150_accel_chip_info_tbl[i].chip_id == val) {
data->chip_info = &bmc150_accel_chip_info_tbl[i]; break;
}
}
ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); if (ret < 0) return ret;
/* Set Bandwidth */
ret = bmc150_accel_set_bw(data, BMC150_ACCEL_DEF_BW, 0); if (ret < 0) return ret;
/* Set Default Range */
ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_RANGE,
BMC150_ACCEL_DEF_RANGE_4G); if (ret < 0) {
dev_err(dev, "Error writing reg_pmu_range\n"); return ret;
}
data->range = BMC150_ACCEL_DEF_RANGE_4G;
/* Set default slope duration and thresholds */
data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD;
data->slope_dur = BMC150_ACCEL_DEF_SLOPE_DURATION;
ret = bmc150_accel_update_slope(data); if (ret < 0) return ret;
/* Set default as latched interrupts */
ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
BMC150_ACCEL_INT_MODE_LATCH_INT |
BMC150_ACCEL_INT_MODE_LATCH_RESET); if (ret < 0) {
dev_err(dev, "Error writing reg_int_rst_latch\n"); return ret;
}
return 0;
}
int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq, enum bmc150_type type, constchar *name, bool block_supported)
{ conststruct iio_dev_attr **fifo_attrs; struct bmc150_accel_data *data; struct iio_dev *indio_dev; int ret;
indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); if (!indio_dev) return -ENOMEM;
data = iio_priv(indio_dev);
dev_set_drvdata(dev, indio_dev);
data->regmap = regmap;
data->type = type;
if (!bmc150_apply_acpi_orientation(dev, &data->orientation)) {
ret = iio_read_mount_matrix(dev, &data->orientation); if (ret) return ret;
}
/* * VDD is the analog and digital domain voltage supply * VDDIO is the digital I/O voltage supply
*/
data->regulators[0].supply = "vdd";
data->regulators[1].supply = "vddio";
ret = devm_regulator_bulk_get(dev,
ARRAY_SIZE(data->regulators),
data->regulators); if (ret) return dev_err_probe(dev, ret, "failed to get regulators\n");
ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
data->regulators); if (ret) {
dev_err(dev, "failed to enable regulators: %d\n", ret); return ret;
} /* * 2ms or 3ms power-on time according to datasheets, let's better * be safe than sorry and set this delay to 5ms.
*/
msleep(5);
ret = bmc150_accel_chip_init(data); if (ret < 0) goto err_disable_regulators;
if (irq > 0) {
ret = devm_request_threaded_irq(dev, irq,
bmc150_accel_irq_handler,
bmc150_accel_irq_thread_handler,
IRQF_TRIGGER_RISING, "bmc150_accel_event",
indio_dev); if (ret) goto err_buffer_cleanup;
/* * Set latched mode interrupt. While certain interrupts are * non-latched regardless of this settings (e.g. new data) we * want to use latch mode when we can to prevent interrupt * flooding.
*/
ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
BMC150_ACCEL_INT_MODE_LATCH_RESET); if (ret < 0) {
dev_err(dev, "Error writing reg_int_rst_latch\n"); goto err_buffer_cleanup;
}
ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0); if (ret < 0) return -EAGAIN;
return 0;
}
staticint bmc150_accel_runtime_resume(struct device *dev)
{ struct iio_dev *indio_dev = dev_get_drvdata(dev); struct bmc150_accel_data *data = iio_priv(indio_dev); int ret; int sleep_val;
ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); if (ret < 0) return ret;
ret = bmc150_accel_fifo_set_mode(data); if (ret < 0) 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.