hwmon: (sht15) Add device tree support
Subsystems (2)
PATCH v2 1/2 dt: Add vendor prefix for Sensirion
In reply to Marco FranchiSensirion is a sensor manufacturer, providing relative humidity sensors, temperature sensor and flow sensor solutions.
---
Changes since v1:
- none
Documentation/devicetree/bindings/vendor-prefixes.txt | 1 + 1 file changed, 1 insertion(+)
Documentation/devicetree/bindings/vendor-prefixes.txt +1 -0
@@ -263,6 +263,7 @@ sbs Smart Battery System schindler Schindler seagate Seagate Technology PLC semtech Semtech Corporation +sensirion Sensirion AG sgx SGX Sensortech sharp Sharp Corporation si-en Si-En Technology Ltd.
Signature · 1 line
2.7.4
PATCH v2 2/2 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.
---
Changes since v1:
- reworked to not require int returns in the sht15_probe_dt function;
- add a clear preference for dt or platform data
- Use 'device tree' instead of 'dts' in the Subject
Documentation/devicetree/bindings/hwmon/sht15.txt | 25 +++++++++ drivers/hwmon/sht15.c | 66 +++++++++++++++++++++-- 2 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 Documentation/devicetree/bindings/hwmon/sht15.txt
Documentation/devicetree/bindings/hwmon/sht15.txt new file +25 -0
@@ -0,0 +1,25 @@ +Sensirion SHT15 Humidity and Temperature Sensor + +Required properties: + + - "compatible": must be "sensirion,sht15". + - "data-gpios": GPIO connected to the data line. + - "clk-gpios": GPIO connected to the clock line. + - "vcc-supply": regulator that drives the VCC pin. + +Optional properties: + + - "checksum": boolean property that when present enables the CRC validation of the readings. + - "no-otp-reload": boolean property that when present indicates not to reload from OTP. + - "low-resolution": boolean property that when present indicates that the device operates in + low-resolution mode. +Example: + + temp_humid_sensor: sht15 { + pinctrl-names = "default"; + pinctrl-0 = <&pinctrl_sensor>; + compatible = "sensirion,sht15"; + clk-gpios = <&gpio4 12 0>; + data-gpios = <&gpio4 13 0>; + vcc-supply = <®_sht15>; + };
drivers/hwmon/sht15.c +62 -4
91 lines not on this page · raw message
Signature · 1 line
2.7.4
Guenter Roeck
maintainerQuoted 84 lines · Marco Franchi
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 <[email protected]>---
Changes since v1:
- reworked to not require int returns in thesht15_probe_dtfunction;
- add a clear preference for dt or platform data
- Use 'device tree' instead of 'dts' in the SubjectDocumentation/devicetree/bindings/hwmon/sht15.txt | 25 +++++++++ drivers/hwmon/sht15.c | 66 +++++++++++++++++++++-- 2 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 Documentation/devicetree/bindings/hwmon/sht15.txtDocumentation/devicetree/bindings/hwmon/sht15.txt new file +25
-0@@ -0,0 +1,25 @@ +Sensirion SHT15 Humidity and Temperature Sensor + +Required properties: + + - "compatible": must be "sensirion,sht15". + - "data-gpios": GPIO connected to the data line. + - "clk-gpios": GPIO connected to the clock line. + - "vcc-supply": regulator that drives the VCC pin. + +Optional properties: + + - "checksum": boolean property that when present enables the CRC validation of the readings. + - "no-otp-reload": boolean property that when present indicates not to reload from OTP. + - "low-resolution": boolean property that when present indicates that the device operates in + low-resolution mode. +Example: + + temp_humid_sensor: sht15 { + pinctrl-names = "default"; + pinctrl-0 = <&pinctrl_sensor>; + compatible = "sensirion,sht15"; + clk-gpios = <&gpio4 12 0>; + data-gpios = <&gpio4 13 0>; + vcc-supply = <®_sht15>; + };drivers/hwmon/sht15.c +20
-0@@ -34,6 +34,7 @@#include <linux/slab.h> #include <linux/atomic.h> #include <linux/bitrev.h> +#include <linux/of_gpio.h> /* Commands */ #define SHT15_MEASURE_TEMP 0x03@@ -911,6 +912,56 @@ static int sht15_invalidate_voltage(struct notifier_block *nb,return NOTIFY_OK ; } +#ifdef CONFIG_OF +static const struct of_device_id sht15_dt_match[] = { + { .compatible ="sensirion,sht15" }, + { }, +}; +MODULE_DEVICE_TABLE (of, sht15_dt_match); + +/* +* This function returns NULL if pdev isn't a device instatiated by dt, +* a pointer to the data if it could successfully get all information +* from dt or a negative error number. +*/ +static struct sht15_platform_data *sht15_probe_dt (struct device *dev) +{ +struct device_node *np = dev->of_node; +struct sht15_platform_data *pdata; + +/* no device tree device */ +if (!np)
drivers/hwmon/sht15.c static int sht15_invalidate_voltage(struct notifier_block *nb, · line 943
Quoted 10 lines
drivers/hwmon/sht15.c +10
-0+return NULL ; + + pdata =devm_kzalloc (dev,sizeof (*pdata),GFP_KERNEL ); +if (!pdata) +return ERR_PTR (-ENOMEM ); + + pdata->gpio_data =of_get_named_gpio (np,"data-gpios" ,0 ); +if (pdata->gpio_data <0 ) { +dev_err (dev,"data-gpios not found\n" ); +return ERR_PTR (-EINVAL );
Never replace an error return value with a different one. The called function could return -EPROBE_DEFER, which you would want to have returned to the caller. In that case, you would also not want to see an error message.
drivers/hwmon/sht15.c static int sht15_invalidate_voltage(struct notifier_block *nb, · line 949
Quoted 6 lines
drivers/hwmon/sht15.c +6
-0+ } + + pdata->gpio_sck =of_get_named_gpio (np,"clk-gpios" ,0 ); +if (pdata->gpio_sck <0 ) { +dev_err (dev,"clk-gpios not found\n" ); +return ERR_PTR (-EINVAL );
Same here.
Quoted 49 lines
drivers/hwmon/sht15.c +26
-4+ } + + pdata->checksum =of_property_read_bool (np,"checksum" ); + pdata->no_otp_reload =of_property_read_bool (np,"no-otp-reload" ); + pdata->low_resolution =of_property_read_bool (np,"low-resolution" ); + +return pdata; +} +#else +static inline struct sht15_platform_data *sht15_probe_dt (struct device *dev) +{ +return NULL ; +} +#endif +static int sht15_probe (struct platform_device *pdev) {int ret;@@ -928,11 +979,17 @@ static int sht15_probe(struct platform_device *pdev) data->dev = &pdev->dev;init_waitqueue_head (&data->wait_queue); -if (dev_get_platdata (&pdev->dev) ==NULL ) { -dev_err (&pdev->dev,"no platform data supplied\n" ); -return -EINVAL ; + data->pdata =sht15_probe_dt (&pdev->dev); +if (IS_ERR (data->pdata)) +return PTR_ERR (data->pdata); +if (data->pdata ==NULL ) { + data->pdata =dev_get_platdata (&pdev->dev); +if (data->pdata ==NULL ) { +dev_err (&pdev->dev,"no platform data supplied\n" ); +return -EINVAL ; + } } - data->pdata =dev_get_platdata (&pdev->dev); + data->supply_uv = data->pdata->supply_mv *1000 ;if (data->pdata->checksum) data->checksumming = true;@@ -1075,6 +1132,7 @@ MODULE_DEVICE_TABLE(platform, sht15_device_ids);static struct platform_driver sht15_driver = { .driver = { .name ="sht15" , + .of_match_table =of_match_ptr (sht15_dt_match), }, .probe = sht15_probe, .remove = sht15_remove,