Lines Matching +full:bosch +full:- +full:sensortec

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Bosch BMC150 three-axis magnetic field sensor driver
7 * This code is based on bmm050_api.c authored by contact@bosch.sensortec.com:
9 * (C) Copyright 2011~2014 Bosch Sensortec GmbH All Rights Reserved
86 #define BMC150_MAGN_XY_OVERFLOW_VAL -4096
87 #define BMC150_MAGN_Z_OVERFLOW_VAL -16384
96 #define BMC150_MAGN_REPXY_TO_REGVAL(rep) (((rep) - 1) / 2)
97 #define BMC150_MAGN_REPZ_TO_REGVAL(rep) ((rep) - 1)
239 ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_POWER, in bmc150_magn_set_power_mode()
246 return regmap_update_bits(data->regmap, in bmc150_magn_set_power_mode()
252 return regmap_update_bits(data->regmap, in bmc150_magn_set_power_mode()
259 return -EINVAL; in bmc150_magn_set_power_mode()
268 ret = pm_runtime_resume_and_get(data->dev); in bmc150_magn_set_power_state()
270 pm_runtime_mark_last_busy(data->dev); in bmc150_magn_set_power_state()
271 ret = pm_runtime_put_autosuspend(data->dev); in bmc150_magn_set_power_state()
275 dev_err(data->dev, in bmc150_magn_set_power_state()
289 ret = regmap_read(data->regmap, BMC150_MAGN_REG_OPMODE_ODR, &reg_val); in bmc150_magn_get_odr()
300 return -EINVAL; in bmc150_magn_get_odr()
310 ret = regmap_update_bits(data->regmap, in bmc150_magn_set_odr()
322 return -EINVAL; in bmc150_magn_set_odr()
331 ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY, in bmc150_magn_set_max_odr()
338 ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z, in bmc150_magn_set_max_odr()
349 /* the maximum selectable read-out frequency from datasheet */ in bmc150_magn_set_max_odr()
352 dev_err(data->dev, in bmc150_magn_set_max_odr()
355 return -EINVAL; in bmc150_magn_set_max_odr()
357 data->max_odr = max_odr; in bmc150_magn_set_max_odr()
366 u16 xyz1 = le16_to_cpu(tregs->xyz1); in bmc150_magn_compensate_x()
374 val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000))); in bmc150_magn_compensate_x()
375 val = ((s16)((((s32)x) * ((((((((s32)tregs->xy2) * ((((s32)val) * in bmc150_magn_compensate_x()
377 ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) * in bmc150_magn_compensate_x()
378 ((s32)(((s16)tregs->x2) + ((s16)0xA0)))) >> 12)) >> 13)) + in bmc150_magn_compensate_x()
379 (((s16)tregs->x1) << 3); in bmc150_magn_compensate_x()
388 u16 xyz1 = le16_to_cpu(tregs->xyz1); in bmc150_magn_compensate_y()
396 val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000))); in bmc150_magn_compensate_y()
397 val = ((s16)((((s32)y) * ((((((((s32)tregs->xy2) * ((((s32)val) * in bmc150_magn_compensate_y()
399 ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) * in bmc150_magn_compensate_y()
400 ((s32)(((s16)tregs->y2) + ((s16)0xA0)))) >> 12)) >> 13)) + in bmc150_magn_compensate_y()
401 (((s16)tregs->y1) << 3); in bmc150_magn_compensate_y()
410 u16 xyz1 = le16_to_cpu(tregs->xyz1); in bmc150_magn_compensate_z()
411 u16 z1 = le16_to_cpu(tregs->z1); in bmc150_magn_compensate_z()
412 s16 z2 = le16_to_cpu(tregs->z2); in bmc150_magn_compensate_z()
413 s16 z3 = le16_to_cpu(tregs->z3); in bmc150_magn_compensate_z()
414 s16 z4 = le16_to_cpu(tregs->z4); in bmc150_magn_compensate_z()
419 val = (((((s32)(z - z4)) << 15) - ((((s32)z3) * ((s32)(((s16)rhall) - in bmc150_magn_compensate_z()
434 ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_X_L, in bmc150_magn_read_xyz()
444 ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_TRIM_START, in bmc150_magn_read_xyz()
467 return -EBUSY; in bmc150_magn_read_raw()
468 mutex_lock(&data->mutex); in bmc150_magn_read_raw()
472 mutex_unlock(&data->mutex); in bmc150_magn_read_raw()
479 mutex_unlock(&data->mutex); in bmc150_magn_read_raw()
482 *val = values[chan->scan_index]; in bmc150_magn_read_raw()
486 mutex_unlock(&data->mutex); in bmc150_magn_read_raw()
490 mutex_unlock(&data->mutex); in bmc150_magn_read_raw()
494 * The API/driver performs an off-chip temperature in bmc150_magn_read_raw()
507 switch (chan->channel2) { in bmc150_magn_read_raw()
510 ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY, in bmc150_magn_read_raw()
517 ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z, in bmc150_magn_read_raw()
524 return -EINVAL; in bmc150_magn_read_raw()
527 return -EINVAL; in bmc150_magn_read_raw()
540 if (val > data->max_odr) in bmc150_magn_write_raw()
541 return -EINVAL; in bmc150_magn_write_raw()
542 mutex_lock(&data->mutex); in bmc150_magn_write_raw()
544 mutex_unlock(&data->mutex); in bmc150_magn_write_raw()
547 switch (chan->channel2) { in bmc150_magn_write_raw()
551 return -EINVAL; in bmc150_magn_write_raw()
552 mutex_lock(&data->mutex); in bmc150_magn_write_raw()
555 mutex_unlock(&data->mutex); in bmc150_magn_write_raw()
558 ret = regmap_update_bits(data->regmap, in bmc150_magn_write_raw()
563 mutex_unlock(&data->mutex); in bmc150_magn_write_raw()
567 return -EINVAL; in bmc150_magn_write_raw()
568 mutex_lock(&data->mutex); in bmc150_magn_write_raw()
571 mutex_unlock(&data->mutex); in bmc150_magn_write_raw()
574 ret = regmap_update_bits(data->regmap, in bmc150_magn_write_raw()
579 mutex_unlock(&data->mutex); in bmc150_magn_write_raw()
582 return -EINVAL; in bmc150_magn_write_raw()
585 return -EINVAL; in bmc150_magn_write_raw()
599 if (bmc150_magn_samp_freq_table[i].freq > data->max_odr) in bmc150_magn_show_samp_freq_avail()
601 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", in bmc150_magn_show_samp_freq_avail()
605 buf[len - 1] = '\n'; in bmc150_magn_show_samp_freq_avail()
616 return &data->orientation; in bmc150_magn_get_mount_matrix()
673 struct iio_dev *indio_dev = pf->indio_dev; in bmc150_magn_trigger_handler()
677 mutex_lock(&data->mutex); in bmc150_magn_trigger_handler()
678 ret = bmc150_magn_read_xyz(data, data->scan.chans); in bmc150_magn_trigger_handler()
682 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, in bmc150_magn_trigger_handler()
683 pf->timestamp); in bmc150_magn_trigger_handler()
686 mutex_unlock(&data->mutex); in bmc150_magn_trigger_handler()
687 iio_trigger_notify_done(indio_dev->trig); in bmc150_magn_trigger_handler()
697 ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators), in bmc150_magn_init()
698 data->regulators); in bmc150_magn_init()
700 dev_err(data->dev, "Failed to enable regulators: %d\n", ret); in bmc150_magn_init()
704 * 3ms power-on time according to datasheet, let's better in bmc150_magn_init()
712 dev_err(data->dev, in bmc150_magn_init()
717 ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id); in bmc150_magn_init()
719 dev_err(data->dev, "Failed reading chip id\n"); in bmc150_magn_init()
723 dev_err(data->dev, "Invalid chip id 0x%x\n", chip_id); in bmc150_magn_init()
724 ret = -ENODEV; in bmc150_magn_init()
727 dev_dbg(data->dev, "Chip id %x\n", chip_id); in bmc150_magn_init()
732 dev_err(data->dev, "Failed to set ODR to %d\n", in bmc150_magn_init()
737 ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY, in bmc150_magn_init()
740 dev_err(data->dev, "Failed to set REP XY to %d\n", in bmc150_magn_init()
745 ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z, in bmc150_magn_init()
748 dev_err(data->dev, "Failed to set REP Z to %d\n", in bmc150_magn_init()
761 dev_err(data->dev, "Failed to power on device\n"); in bmc150_magn_init()
770 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators); in bmc150_magn_init()
782 return regmap_read(data->regmap, BMC150_MAGN_REG_X_L, &tmp); in bmc150_magn_reset_intr()
791 if (!data->dready_trigger_on) in bmc150_magn_trig_reen()
794 mutex_lock(&data->mutex); in bmc150_magn_trig_reen()
796 mutex_unlock(&data->mutex); in bmc150_magn_trig_reen()
798 dev_err(data->dev, "Failed to reset interrupt\n"); in bmc150_magn_trig_reen()
808 mutex_lock(&data->mutex); in bmc150_magn_data_rdy_trigger_set_state()
809 if (state == data->dready_trigger_on) in bmc150_magn_data_rdy_trigger_set_state()
812 ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY, in bmc150_magn_data_rdy_trigger_set_state()
818 data->dready_trigger_on = state; in bmc150_magn_data_rdy_trigger_set_state()
825 mutex_unlock(&data->mutex); in bmc150_magn_data_rdy_trigger_set_state()
830 mutex_unlock(&data->mutex); in bmc150_magn_data_rdy_trigger_set_state()
862 id = acpi_match_device(dev->driver->acpi_match_table, dev); in bmc150_magn_match_acpi_device()
878 return -ENOMEM; in bmc150_magn_probe()
882 data->regmap = regmap; in bmc150_magn_probe()
883 data->irq = irq; in bmc150_magn_probe()
884 data->dev = dev; in bmc150_magn_probe()
886 data->regulators[0].supply = "vdd"; in bmc150_magn_probe()
887 data->regulators[1].supply = "vddio"; in bmc150_magn_probe()
888 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators), in bmc150_magn_probe()
889 data->regulators); in bmc150_magn_probe()
893 ret = iio_read_mount_matrix(dev, &data->orientation); in bmc150_magn_probe()
900 mutex_init(&data->mutex); in bmc150_magn_probe()
906 indio_dev->channels = bmc150_magn_channels; in bmc150_magn_probe()
907 indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels); in bmc150_magn_probe()
908 indio_dev->available_scan_masks = bmc150_magn_scan_masks; in bmc150_magn_probe()
909 indio_dev->name = name; in bmc150_magn_probe()
910 indio_dev->modes = INDIO_DIRECT_MODE; in bmc150_magn_probe()
911 indio_dev->info = &bmc150_magn_info; in bmc150_magn_probe()
914 data->dready_trig = devm_iio_trigger_alloc(dev, in bmc150_magn_probe()
915 "%s-dev%d", in bmc150_magn_probe()
916 indio_dev->name, in bmc150_magn_probe()
918 if (!data->dready_trig) { in bmc150_magn_probe()
919 ret = -ENOMEM; in bmc150_magn_probe()
924 data->dready_trig->ops = &bmc150_magn_trigger_ops; in bmc150_magn_probe()
925 iio_trigger_set_drvdata(data->dready_trig, indio_dev); in bmc150_magn_probe()
926 ret = iio_trigger_register(data->dready_trig); in bmc150_magn_probe()
937 data->dready_trig); in bmc150_magn_probe()
978 free_irq(irq, data->dready_trig); in bmc150_magn_probe()
980 if (data->dready_trig) in bmc150_magn_probe()
981 iio_trigger_unregister(data->dready_trig); in bmc150_magn_probe()
1000 if (data->irq > 0) in bmc150_magn_remove()
1001 free_irq(data->irq, data->dready_trig); in bmc150_magn_remove()
1003 if (data->dready_trig) in bmc150_magn_remove()
1004 iio_trigger_unregister(data->dready_trig); in bmc150_magn_remove()
1006 mutex_lock(&data->mutex); in bmc150_magn_remove()
1008 mutex_unlock(&data->mutex); in bmc150_magn_remove()
1010 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators); in bmc150_magn_remove()
1021 mutex_lock(&data->mutex); in bmc150_magn_runtime_suspend()
1024 mutex_unlock(&data->mutex); in bmc150_magn_runtime_suspend()
1033 * Should be called with data->mutex held.
1052 mutex_lock(&data->mutex); in bmc150_magn_suspend()
1055 mutex_unlock(&data->mutex); in bmc150_magn_suspend()
1066 mutex_lock(&data->mutex); in bmc150_magn_resume()
1069 mutex_unlock(&data->mutex); in bmc150_magn_resume()