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

iio: tsl2772: add support for regulator framework

This patch adds support for the regulator framework to the tsl2772
driver. Driver was tested using a LG Nexus 5 (hammerhead) phone with
the two regulators and on a Raspberry Pi 2 without any regulators
controlling the power to the sensor.

Signed-off-by: Brian Masney <masneyb@onstation.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Brian Masney and committed by
Jonathan Cameron
7c14947e 75de3b57

+94 -1
+94 -1
drivers/iio/light/tsl2772.c
··· 20 20 #include <linux/iio/iio.h> 21 21 #include <linux/iio/sysfs.h> 22 22 #include <linux/platform_data/tsl2772.h> 23 + #include <linux/regulator/consumer.h> 23 24 24 25 /* Cal defs */ 25 26 #define PROX_STAT_CAL 0 ··· 110 109 111 110 #define TSL2772_MAX_PROX_LEDS 2 112 111 112 + #define TSL2772_BOOT_MIN_SLEEP_TIME 10000 113 + #define TSL2772_BOOT_MAX_SLEEP_TIME 28000 114 + 113 115 /* Device family members */ 114 116 enum { 115 117 tsl2571, ··· 161 157 struct mutex prox_mutex; 162 158 struct mutex als_mutex; 163 159 struct i2c_client *client; 160 + struct regulator *vdd_supply; 161 + struct regulator *vddio_supply; 164 162 u16 prox_data; 165 163 struct tsl2772_als_info als_cur_info; 166 164 struct tsl2772_settings settings; ··· 691 685 chip->settings.als_gain_trim = ret; 692 686 693 687 return ret; 688 + } 689 + 690 + static void tsl2772_disable_regulators_action(void *_data) 691 + { 692 + struct tsl2772_chip *chip = _data; 693 + 694 + regulator_disable(chip->vdd_supply); 695 + regulator_disable(chip->vddio_supply); 696 + } 697 + 698 + static int tsl2772_enable_regulator(struct tsl2772_chip *chip, 699 + struct regulator *regulator) 700 + { 701 + int ret; 702 + 703 + ret = regulator_enable(regulator); 704 + if (ret < 0) { 705 + dev_err(&chip->client->dev, "Failed to enable regulator: %d\n", 706 + ret); 707 + return ret; 708 + } 709 + 710 + return 0; 711 + } 712 + 713 + static struct regulator *tsl2772_get_regulator(struct tsl2772_chip *chip, 714 + char *name) 715 + { 716 + struct regulator *regulator; 717 + int ret; 718 + 719 + regulator = devm_regulator_get(&chip->client->dev, name); 720 + if (IS_ERR(regulator)) { 721 + if (PTR_ERR(regulator) != -EPROBE_DEFER) 722 + dev_err(&chip->client->dev, 723 + "Failed to get %s regulator %d\n", 724 + name, (int)PTR_ERR(regulator)); 725 + 726 + return regulator; 727 + } 728 + 729 + ret = tsl2772_enable_regulator(chip, regulator); 730 + if (ret < 0) 731 + return ERR_PTR(ret); 732 + 733 + return regulator; 694 734 } 695 735 696 736 static int tsl2772_chip_on(struct iio_dev *indio_dev) ··· 1797 1745 chip->client = clientp; 1798 1746 i2c_set_clientdata(clientp, indio_dev); 1799 1747 1748 + chip->vddio_supply = tsl2772_get_regulator(chip, "vddio"); 1749 + if (IS_ERR(chip->vddio_supply)) 1750 + return PTR_ERR(chip->vddio_supply); 1751 + 1752 + chip->vdd_supply = tsl2772_get_regulator(chip, "vdd"); 1753 + if (IS_ERR(chip->vdd_supply)) { 1754 + regulator_disable(chip->vddio_supply); 1755 + return PTR_ERR(chip->vdd_supply); 1756 + } 1757 + 1758 + ret = devm_add_action(&clientp->dev, tsl2772_disable_regulators_action, 1759 + chip); 1760 + if (ret < 0) { 1761 + tsl2772_disable_regulators_action(chip); 1762 + dev_err(&clientp->dev, "Failed to setup regulator cleanup action %d\n", 1763 + ret); 1764 + return ret; 1765 + } 1766 + 1767 + usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME); 1768 + 1800 1769 ret = i2c_smbus_read_byte_data(chip->client, 1801 1770 TSL2772_CMD_REG | TSL2772_CHIPID); 1802 1771 if (ret < 0) ··· 1891 1818 static int tsl2772_suspend(struct device *dev) 1892 1819 { 1893 1820 struct iio_dev *indio_dev = dev_get_drvdata(dev); 1821 + struct tsl2772_chip *chip = iio_priv(indio_dev); 1822 + int ret; 1894 1823 1895 - return tsl2772_chip_off(indio_dev); 1824 + ret = tsl2772_chip_off(indio_dev); 1825 + regulator_disable(chip->vdd_supply); 1826 + regulator_disable(chip->vddio_supply); 1827 + 1828 + return ret; 1896 1829 } 1897 1830 1898 1831 static int tsl2772_resume(struct device *dev) 1899 1832 { 1900 1833 struct iio_dev *indio_dev = dev_get_drvdata(dev); 1834 + struct tsl2772_chip *chip = iio_priv(indio_dev); 1835 + int ret; 1836 + 1837 + ret = tsl2772_enable_regulator(chip, chip->vddio_supply); 1838 + if (ret < 0) 1839 + return ret; 1840 + 1841 + ret = tsl2772_enable_regulator(chip, chip->vdd_supply); 1842 + if (ret < 0) { 1843 + regulator_disable(chip->vddio_supply); 1844 + return ret; 1845 + } 1846 + 1847 + usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME); 1901 1848 1902 1849 return tsl2772_chip_on(indio_dev); 1903 1850 }