Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: magnetometer: bmc150: Add rudimentary regulator support

BMC150 needs VDD and VDDIO regulators that might need to be explicitly
enabled. Add some rudimentary support to obtain and enable these
regulators during probe() and disable them during remove()
or on the error path.

Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Stephan Gerhold <stephan@gerhold.net>
Link: https://lore.kernel.org/r/20210109152327.512538-2-stephan@gerhold.net
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Stephan Gerhold and committed by
Jonathan Cameron
cce4f160 111a10d4

+25 -1
+25 -1
drivers/iio/magnetometer/bmc150_magn.c
··· 25 25 #include <linux/iio/trigger_consumer.h> 26 26 #include <linux/iio/triggered_buffer.h> 27 27 #include <linux/regmap.h> 28 + #include <linux/regulator/consumer.h> 28 29 29 30 #include "bmc150_magn.h" 30 31 ··· 136 135 */ 137 136 struct mutex mutex; 138 137 struct regmap *regmap; 138 + struct regulator_bulk_data regulators[2]; 139 139 struct iio_mount_matrix orientation; 140 140 /* 4 x 32 bits for x, y z, 4 bytes align, 64 bits timestamp */ 141 141 s32 buffer[6]; ··· 694 692 int ret, chip_id; 695 693 struct bmc150_magn_preset preset; 696 694 695 + ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators), 696 + data->regulators); 697 + if (ret < 0) { 698 + dev_err(data->dev, "Failed to enable regulators: %d\n", ret); 699 + return ret; 700 + } 701 + /* 702 + * 3ms power-on time according to datasheet, let's better 703 + * be safe than sorry and set this delay to 5ms. 704 + */ 705 + msleep(5); 706 + 697 707 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, 698 708 false); 699 709 if (ret < 0) { 700 710 dev_err(data->dev, 701 711 "Failed to bring up device from suspend mode\n"); 702 - return ret; 712 + goto err_regulator_disable; 703 713 } 704 714 705 715 ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id); ··· 766 752 767 753 err_poweroff: 768 754 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); 755 + err_regulator_disable: 756 + regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators); 769 757 return ret; 770 758 } 771 759 ··· 882 866 data->regmap = regmap; 883 867 data->irq = irq; 884 868 data->dev = dev; 869 + 870 + data->regulators[0].supply = "vdd"; 871 + data->regulators[1].supply = "vddio"; 872 + ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators), 873 + data->regulators); 874 + if (ret) 875 + return dev_err_probe(dev, ret, "failed to get regulators\n"); 885 876 886 877 ret = iio_read_mount_matrix(dev, "mount-matrix", 887 878 &data->orientation); ··· 1007 984 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); 1008 985 mutex_unlock(&data->mutex); 1009 986 987 + regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators); 1010 988 return 0; 1011 989 } 1012 990 EXPORT_SYMBOL(bmc150_magn_remove);