aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Franchi <marco.franchi@nxp.com>2017-02-16 07:23:43 -0500
committerGuenter Roeck <linux@roeck-us.net>2017-02-16 09:49:05 -0500
commit2f1736ff0664937636f8c0a4994c4a5a23da2090 (patch)
treebdbad45ae752eb7a029be1f6f5f0ad282043e933
parent87d08b11b1616cbc70c28c9d3601bd1a3642bae5 (diff)
hwmon: (sht15) Add device tree support
Allow the driver to work with device tree support. Based on initial patch submission from Peter Fox. Tested on a imx7d-sdb board connected to a SHT15 board via Mikro Bus. Signed-off-by: Marco Franchi <marco.franchi@nxp.com> Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
-rw-r--r--Documentation/devicetree/bindings/hwmon/sht15.txt19
-rw-r--r--drivers/hwmon/sht15.c64
2 files changed, 79 insertions, 4 deletions
diff --git a/Documentation/devicetree/bindings/hwmon/sht15.txt b/Documentation/devicetree/bindings/hwmon/sht15.txt
new file mode 100644
index 000000000000..6a80277cc426
--- /dev/null
+++ b/Documentation/devicetree/bindings/hwmon/sht15.txt
@@ -0,0 +1,19 @@
1Sensirion SHT15 Humidity and Temperature Sensor
2
3Required properties:
4
5 - "compatible": must be "sensirion,sht15".
6 - "data-gpios": GPIO connected to the data line.
7 - "clk-gpios": GPIO connected to the clock line.
8 - "vcc-supply": regulator that drives the VCC pin.
9
10Example:
11
12 sensor {
13 pinctrl-names = "default";
14 pinctrl-0 = <&pinctrl_sensor>;
15 compatible = "sensirion,sht15";
16 clk-gpios = <&gpio4 12 0>;
17 data-gpios = <&gpio4 13 0>;
18 vcc-supply = <&reg_sht15>;
19 };
diff --git a/drivers/hwmon/sht15.c b/drivers/hwmon/sht15.c
index f16687c64fc8..e4d642b673c6 100644
--- a/drivers/hwmon/sht15.c
+++ b/drivers/hwmon/sht15.c
@@ -34,6 +34,7 @@
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include <linux/atomic.h> 35#include <linux/atomic.h>
36#include <linux/bitrev.h> 36#include <linux/bitrev.h>
37#include <linux/of_gpio.h>
37 38
38/* Commands */ 39/* Commands */
39#define SHT15_MEASURE_TEMP 0x03 40#define SHT15_MEASURE_TEMP 0x03
@@ -911,6 +912,54 @@ static int sht15_invalidate_voltage(struct notifier_block *nb,
911 return NOTIFY_OK; 912 return NOTIFY_OK;
912} 913}
913 914
915#ifdef CONFIG_OF
916static const struct of_device_id sht15_dt_match[] = {
917 { .compatible = "sensirion,sht15" },
918 { },
919};
920MODULE_DEVICE_TABLE(of, sht15_dt_match);
921
922/*
923 * This function returns NULL if pdev isn't a device instatiated by dt,
924 * a pointer to pdata if it could successfully get all information
925 * from dt or a negative ERR_PTR() on error.
926 */
927static struct sht15_platform_data *sht15_probe_dt(struct device *dev)
928{
929 struct device_node *np = dev->of_node;
930 struct sht15_platform_data *pdata;
931
932 /* no device tree device */
933 if (!np)
934 return NULL;
935
936 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
937 if (!pdata)
938 return ERR_PTR(-ENOMEM);
939
940 pdata->gpio_data = of_get_named_gpio(np, "data-gpios", 0);
941 if (pdata->gpio_data < 0) {
942 if (pdata->gpio_data != -EPROBE_DEFER)
943 dev_err(dev, "data-gpios not found\n");
944 return ERR_PTR(pdata->gpio_data);
945 }
946
947 pdata->gpio_sck = of_get_named_gpio(np, "clk-gpios", 0);
948 if (pdata->gpio_sck < 0) {
949 if (pdata->gpio_sck != -EPROBE_DEFER)
950 dev_err(dev, "clk-gpios not found\n");
951 return ERR_PTR(pdata->gpio_sck);
952 }
953
954 return pdata;
955}
956#else
957static inline struct sht15_platform_data *sht15_probe_dt(struct device *dev)
958{
959 return NULL;
960}
961#endif
962
914static int sht15_probe(struct platform_device *pdev) 963static int sht15_probe(struct platform_device *pdev)
915{ 964{
916 int ret; 965 int ret;
@@ -928,11 +977,17 @@ static int sht15_probe(struct platform_device *pdev)
928 data->dev = &pdev->dev; 977 data->dev = &pdev->dev;
929 init_waitqueue_head(&data->wait_queue); 978 init_waitqueue_head(&data->wait_queue);
930 979
931 if (dev_get_platdata(&pdev->dev) == NULL) { 980 data->pdata = sht15_probe_dt(&pdev->dev);
932 dev_err(&pdev->dev, "no platform data supplied\n"); 981 if (IS_ERR(data->pdata))
933 return -EINVAL; 982 return PTR_ERR(data->pdata);
983 if (data->pdata == NULL) {
984 data->pdata = dev_get_platdata(&pdev->dev);
985 if (data->pdata == NULL) {
986 dev_err(&pdev->dev, "no platform data supplied\n");
987 return -EINVAL;
988 }
934 } 989 }
935 data->pdata = dev_get_platdata(&pdev->dev); 990
936 data->supply_uv = data->pdata->supply_mv * 1000; 991 data->supply_uv = data->pdata->supply_mv * 1000;
937 if (data->pdata->checksum) 992 if (data->pdata->checksum)
938 data->checksumming = true; 993 data->checksumming = true;
@@ -1075,6 +1130,7 @@ MODULE_DEVICE_TABLE(platform, sht15_device_ids);
1075static struct platform_driver sht15_driver = { 1130static struct platform_driver sht15_driver = {
1076 .driver = { 1131 .driver = {
1077 .name = "sht15", 1132 .name = "sht15",
1133 .of_match_table = of_match_ptr(sht15_dt_match),
1078 }, 1134 },
1079 .probe = sht15_probe, 1135 .probe = sht15_probe,
1080 .remove = sht15_remove, 1136 .remove = sht15_remove,