diff mbox series

[1/2] thermal: tsens: Add support for SDM845 platform

Message ID 7a0f33cff94f41110bdbff14d28d36074fae2a7d.1527937396.git.amit.kucheria@linaro.org
State Superseded, archived
Headers show
Series thermal: tsens: SDM845 support | expand

Commit Message

Amit Kucheria June 2, 2018, 11:11 a.m. UTC
There are two tsens blocks on the SDM845. These will be configured through
the devicetree.

Signed-off-by: Amit Kucheria <amit.kucheria@linaro.org>
---
 .../devicetree/bindings/thermal/qcom-tsens.txt     |  1 +
 drivers/thermal/qcom/Makefile                      |  2 +-
 drivers/thermal/qcom/tsens-sdm845.c                | 98 ++++++++++++++++++++++
 drivers/thermal/qcom/tsens.c                       |  3 +
 drivers/thermal/qcom/tsens.h                       |  1 +
 5 files changed, 104 insertions(+), 1 deletion(-)
 create mode 100644 drivers/thermal/qcom/tsens-sdm845.c

Comments

Rajendra Nayak June 4, 2018, 4:25 a.m. UTC | #1
On 06/02/2018 04:41 PM, Amit Kucheria wrote:
> There are two tsens blocks on the SDM845. These will be configured through
> the devicetree.
> 
> Signed-off-by: Amit Kucheria <amit.kucheria@linaro.org>
> ---
>  .../devicetree/bindings/thermal/qcom-tsens.txt     |  1 +
>  drivers/thermal/qcom/Makefile                      |  2 +-
>  drivers/thermal/qcom/tsens-sdm845.c                | 98 ++++++++++++++++++++++
>  drivers/thermal/qcom/tsens.c                       |  3 +
>  drivers/thermal/qcom/tsens.h                       |  1 +
>  5 files changed, 104 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/thermal/qcom/tsens-sdm845.c
> 
> diff --git a/Documentation/devicetree/bindings/thermal/qcom-tsens.txt b/Documentation/devicetree/bindings/thermal/qcom-tsens.txt
> index 292ed89..8652499 100644
> --- a/Documentation/devicetree/bindings/thermal/qcom-tsens.txt
> +++ b/Documentation/devicetree/bindings/thermal/qcom-tsens.txt
> @@ -5,6 +5,7 @@ Required properties:
>   - "qcom,msm8916-tsens" : For 8916 Family of SoCs
>   - "qcom,msm8974-tsens" : For 8974 Family of SoCs
>   - "qcom,msm8996-tsens" : For 8996 Family of SoCs
> + - "qcom,sdm845-tsens"  : For SDM845 Family of SoCs
>  
>  - reg: Address range of the thermal registers
>  - #thermal-sensor-cells : Should be 1. See ./thermal.txt for a description.
> diff --git a/drivers/thermal/qcom/Makefile b/drivers/thermal/qcom/Makefile
> index 2cc2193..dc9f169 100644
> --- a/drivers/thermal/qcom/Makefile
> +++ b/drivers/thermal/qcom/Makefile
> @@ -1,2 +1,2 @@
>  obj-$(CONFIG_QCOM_TSENS)	+= qcom_tsens.o
> -qcom_tsens-y			+= tsens.o tsens-common.o tsens-8916.o tsens-8974.o tsens-8960.o tsens-8996.o
> +qcom_tsens-y			+= tsens.o tsens-common.o tsens-8916.o tsens-8974.o tsens-8960.o tsens-8996.o tsens-sdm845.o
> diff --git a/drivers/thermal/qcom/tsens-sdm845.c b/drivers/thermal/qcom/tsens-sdm845.c
> new file mode 100644
> index 0000000..5d78f0b
> --- /dev/null
> +++ b/drivers/thermal/qcom/tsens-sdm845.c
> @@ -0,0 +1,98 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (c) 2018, Linaro Limited
> + */
> +
> +#include <linux/of_address.h>
> +#include <linux/platform_device.h>
> +#include <linux/regmap.h>
> +#include "tsens.h"
> +
> +#define CNTL_OFFSET	4
> +/* CNTL_OFFSET bitmasks */
> +#define EN		BIT(0)
> +#define SW_RST		BIT(1)
> +
> +#define SENSOR0_SHIFT	3
> +
> +#define TRDY_OFFSET     0xe4
> +#define TRDY_READY_BIT  BIT(1)
> +
> +#define STATUS_OFFSET	0xa0
> +#define LAST_TEMP_MASK	0xfff
> +#define STATUS_VALID_BIT	BIT(21)
> +#define CODE_SIGN_BIT		BIT(11)
> +
> +static int get_temp_sdm845(struct tsens_device *tmdev, int id, int *temp)
> +{
> +	struct tsens_sensor *s = &tmdev->sensor[id];
> +	u32 code;
> +	unsigned int sensor_addr;
> +	int last_temp = 0, last_temp2 = 0, last_temp3 = 0, ret;
> +
> +	ret = regmap_read(tmdev->map, TRDY_OFFSET, &code);
> +	if (ret)
> +		return ret;
> +	if (code & TRDY_READY_BIT)
> +		return -ENODATA;
> +
> +	sensor_addr = STATUS_OFFSET + s->hw_id * 4;
> +	ret = regmap_read(tmdev->map, sensor_addr, &code);
> +	if (ret)
> +		return ret;
> +	last_temp = code & LAST_TEMP_MASK;
> +	if (code & STATUS_VALID_BIT)
> +		goto done;
> +
> +	/* Try a second time */
> +	ret = regmap_read(tmdev->map, sensor_addr, &code);
> +	if (ret)
> +		return ret;
> +	if (code & STATUS_VALID_BIT) {
> +		last_temp = code & LAST_TEMP_MASK;
> +		goto done;
> +	} else {
> +		last_temp2 = code & LAST_TEMP_MASK;
> +	}
> +
> +	/* Try a third/last time */
> +	ret = regmap_read(tmdev->map, sensor_addr, &code);
> +	if (ret)
> +		return ret;
> +	if (code & STATUS_VALID_BIT) {
> +		last_temp = code & LAST_TEMP_MASK;
> +		goto done;
> +	} else {
> +		last_temp3 = code & LAST_TEMP_MASK;
> +	}
> +
> +	if (last_temp == last_temp2)
> +		last_temp = last_temp2;
> +	else if (last_temp2 == last_temp3)
> +		last_temp = last_temp3;
> +done:
> +	/* Code sign bit is the sign extension for a negative value */
> +	if (last_temp & CODE_SIGN_BIT)
> +		last_temp |= ~CODE_SIGN_BIT;
> +
> +	/* Temperatures are in deciCelicius */
> +	*temp = last_temp * 100;

This looks the same as what we do for 8996. Does it make sense to move this
to tsens-common and reuse in both 8996 and 845?

> +
> +	return 0;
> +}
> +
> +static const struct regmap_config tsens_config = {
> +	.reg_bits	= 32,
> +	.val_bits	= 32,
> +	.reg_stride	= 4,
> +};
> +
> +
> +static const struct tsens_ops ops_sdm845 = {
> +  	.init		= init_common,
> +	.get_temp	= get_temp_sdm845,
> +};
> +
> +const struct tsens_data data_sdm845 = {
> +	.ops		= &ops_sdm845,
> +};
> diff --git a/drivers/thermal/qcom/tsens.c b/drivers/thermal/qcom/tsens.c
> index 3f9fe6a..314a20f 100644
> --- a/drivers/thermal/qcom/tsens.c
> +++ b/drivers/thermal/qcom/tsens.c
> @@ -72,6 +72,9 @@ static const struct of_device_id tsens_table[] = {
>  	}, {
>  		.compatible = "qcom,msm8996-tsens",
>  		.data = &data_8996,
> +	}, {
> +		.compatible = "qcom,sdm845-tsens",
> +		.data = &data_sdm845,
>  	},
>  	{}
>  };
> diff --git a/drivers/thermal/qcom/tsens.h b/drivers/thermal/qcom/tsens.h
> index 911c197..f15660d 100644
> --- a/drivers/thermal/qcom/tsens.h
> +++ b/drivers/thermal/qcom/tsens.h
> @@ -90,5 +90,6 @@ int init_common(struct tsens_device *);
>  int get_temp_common(struct tsens_device *, int, int *);
>  
>  extern const struct tsens_data data_8916, data_8974, data_8960, data_8996;
> +extern const struct tsens_data data_sdm845;
>  
>  #endif /* __QCOM_TSENS_H__ */
>
Bjorn Andersson June 4, 2018, 3:03 p.m. UTC | #2
On Sat 02 Jun 04:11 PDT 2018, Amit Kucheria wrote:
> diff --git a/drivers/thermal/qcom/tsens-sdm845.c b/drivers/thermal/qcom/tsens-sdm845.c
[..]
> +#define TRDY_OFFSET     0xe4
> +#define TRDY_READY_BIT  BIT(1)

This is bit 0.

> +
> +#define STATUS_OFFSET	0xa0
> +#define LAST_TEMP_MASK	0xfff
> +#define STATUS_VALID_BIT	BIT(21)
> +#define CODE_SIGN_BIT		BIT(11)
> +
> +static int get_temp_sdm845(struct tsens_device *tmdev, int id, int *temp)
> +{
> +	struct tsens_sensor *s = &tmdev->sensor[id];
> +	u32 code;
> +	unsigned int sensor_addr;
> +	int last_temp = 0, last_temp2 = 0, last_temp3 = 0, ret;
> +
> +	ret = regmap_read(tmdev->map, TRDY_OFFSET, &code);
> +	if (ret)
> +		return ret;
> +	if (code & TRDY_READY_BIT)
> +		return -ENODATA;

This section is the only difference from 8996, but this register is
identical to 8996 and 8998. So I think you should add this to
tsens-8996.c and we can use that for 8996, 8998 and sdm845.

Perhaps we should name it tsens-v2, as that seems to be the common
denominator for these, according to the documentation.

Regards,
Bjorn
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Amit Kucheria June 12, 2018, 10:39 a.m. UTC | #3
On Mon, Jun 4, 2018 at 6:03 PM, Bjorn Andersson
<bjorn.andersson@linaro.org> wrote:
> On Sat 02 Jun 04:11 PDT 2018, Amit Kucheria wrote:
>> diff --git a/drivers/thermal/qcom/tsens-sdm845.c b/drivers/thermal/qcom/tsens-sdm845.c
> [..]
>> +#define TRDY_OFFSET     0xe4
>> +#define TRDY_READY_BIT  BIT(1)
>
> This is bit 0.

Oops, fixed.

>> +
>> +#define STATUS_OFFSET        0xa0
>> +#define LAST_TEMP_MASK       0xfff
>> +#define STATUS_VALID_BIT     BIT(21)
>> +#define CODE_SIGN_BIT                BIT(11)
>> +
>> +static int get_temp_sdm845(struct tsens_device *tmdev, int id, int *temp)
>> +{
>> +     struct tsens_sensor *s = &tmdev->sensor[id];
>> +     u32 code;
>> +     unsigned int sensor_addr;
>> +     int last_temp = 0, last_temp2 = 0, last_temp3 = 0, ret;
>> +
>> +     ret = regmap_read(tmdev->map, TRDY_OFFSET, &code);
>> +     if (ret)
>> +             return ret;
>> +     if (code & TRDY_READY_BIT)
>> +             return -ENODATA;
>
> This section is the only difference from 8996, but this register is
> identical to 8996 and 8998. So I think you should add this to
> tsens-8996.c and we can use that for 8996, 8998 and sdm845.
>
> Perhaps we should name it tsens-v2, as that seems to be the common
> denominator for these, according to the documentation.

Thanks for confirming that the block is identical^Wsimilar across
8996, 8998, sdm845 (and possible others). I suspected as much but
didn't check in a lot of detail.

I expected the 845 to gain more features over time, hence the reason
for a separate file and a custom get_temp(). Now, I'll fold all of
them into a common tsens-v2 function.

We'll also need to change how we regmap the 8998 and sdm845 tsens
registers because the offsets are not the same and hence not handled
corrently by tsens-common.c/init_common().
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox series

Patch

diff --git a/Documentation/devicetree/bindings/thermal/qcom-tsens.txt b/Documentation/devicetree/bindings/thermal/qcom-tsens.txt
index 292ed89..8652499 100644
--- a/Documentation/devicetree/bindings/thermal/qcom-tsens.txt
+++ b/Documentation/devicetree/bindings/thermal/qcom-tsens.txt
@@ -5,6 +5,7 @@  Required properties:
  - "qcom,msm8916-tsens" : For 8916 Family of SoCs
  - "qcom,msm8974-tsens" : For 8974 Family of SoCs
  - "qcom,msm8996-tsens" : For 8996 Family of SoCs
+ - "qcom,sdm845-tsens"  : For SDM845 Family of SoCs
 
 - reg: Address range of the thermal registers
 - #thermal-sensor-cells : Should be 1. See ./thermal.txt for a description.
diff --git a/drivers/thermal/qcom/Makefile b/drivers/thermal/qcom/Makefile
index 2cc2193..dc9f169 100644
--- a/drivers/thermal/qcom/Makefile
+++ b/drivers/thermal/qcom/Makefile
@@ -1,2 +1,2 @@ 
 obj-$(CONFIG_QCOM_TSENS)	+= qcom_tsens.o
-qcom_tsens-y			+= tsens.o tsens-common.o tsens-8916.o tsens-8974.o tsens-8960.o tsens-8996.o
+qcom_tsens-y			+= tsens.o tsens-common.o tsens-8916.o tsens-8974.o tsens-8960.o tsens-8996.o tsens-sdm845.o
diff --git a/drivers/thermal/qcom/tsens-sdm845.c b/drivers/thermal/qcom/tsens-sdm845.c
new file mode 100644
index 0000000..5d78f0b
--- /dev/null
+++ b/drivers/thermal/qcom/tsens-sdm845.c
@@ -0,0 +1,98 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2018, Linaro Limited
+ */
+
+#include <linux/of_address.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include "tsens.h"
+
+#define CNTL_OFFSET	4
+/* CNTL_OFFSET bitmasks */
+#define EN		BIT(0)
+#define SW_RST		BIT(1)
+
+#define SENSOR0_SHIFT	3
+
+#define TRDY_OFFSET     0xe4
+#define TRDY_READY_BIT  BIT(1)
+
+#define STATUS_OFFSET	0xa0
+#define LAST_TEMP_MASK	0xfff
+#define STATUS_VALID_BIT	BIT(21)
+#define CODE_SIGN_BIT		BIT(11)
+
+static int get_temp_sdm845(struct tsens_device *tmdev, int id, int *temp)
+{
+	struct tsens_sensor *s = &tmdev->sensor[id];
+	u32 code;
+	unsigned int sensor_addr;
+	int last_temp = 0, last_temp2 = 0, last_temp3 = 0, ret;
+
+	ret = regmap_read(tmdev->map, TRDY_OFFSET, &code);
+	if (ret)
+		return ret;
+	if (code & TRDY_READY_BIT)
+		return -ENODATA;
+
+	sensor_addr = STATUS_OFFSET + s->hw_id * 4;
+	ret = regmap_read(tmdev->map, sensor_addr, &code);
+	if (ret)
+		return ret;
+	last_temp = code & LAST_TEMP_MASK;
+	if (code & STATUS_VALID_BIT)
+		goto done;
+
+	/* Try a second time */
+	ret = regmap_read(tmdev->map, sensor_addr, &code);
+	if (ret)
+		return ret;
+	if (code & STATUS_VALID_BIT) {
+		last_temp = code & LAST_TEMP_MASK;
+		goto done;
+	} else {
+		last_temp2 = code & LAST_TEMP_MASK;
+	}
+
+	/* Try a third/last time */
+	ret = regmap_read(tmdev->map, sensor_addr, &code);
+	if (ret)
+		return ret;
+	if (code & STATUS_VALID_BIT) {
+		last_temp = code & LAST_TEMP_MASK;
+		goto done;
+	} else {
+		last_temp3 = code & LAST_TEMP_MASK;
+	}
+
+	if (last_temp == last_temp2)
+		last_temp = last_temp2;
+	else if (last_temp2 == last_temp3)
+		last_temp = last_temp3;
+done:
+	/* Code sign bit is the sign extension for a negative value */
+	if (last_temp & CODE_SIGN_BIT)
+		last_temp |= ~CODE_SIGN_BIT;
+
+	/* Temperatures are in deciCelicius */
+	*temp = last_temp * 100;
+
+	return 0;
+}
+
+static const struct regmap_config tsens_config = {
+	.reg_bits	= 32,
+	.val_bits	= 32,
+	.reg_stride	= 4,
+};
+
+
+static const struct tsens_ops ops_sdm845 = {
+  	.init		= init_common,
+	.get_temp	= get_temp_sdm845,
+};
+
+const struct tsens_data data_sdm845 = {
+	.ops		= &ops_sdm845,
+};
diff --git a/drivers/thermal/qcom/tsens.c b/drivers/thermal/qcom/tsens.c
index 3f9fe6a..314a20f 100644
--- a/drivers/thermal/qcom/tsens.c
+++ b/drivers/thermal/qcom/tsens.c
@@ -72,6 +72,9 @@  static const struct of_device_id tsens_table[] = {
 	}, {
 		.compatible = "qcom,msm8996-tsens",
 		.data = &data_8996,
+	}, {
+		.compatible = "qcom,sdm845-tsens",
+		.data = &data_sdm845,
 	},
 	{}
 };
diff --git a/drivers/thermal/qcom/tsens.h b/drivers/thermal/qcom/tsens.h
index 911c197..f15660d 100644
--- a/drivers/thermal/qcom/tsens.h
+++ b/drivers/thermal/qcom/tsens.h
@@ -90,5 +90,6 @@  int init_common(struct tsens_device *);
 int get_temp_common(struct tsens_device *, int, int *);
 
 extern const struct tsens_data data_8916, data_8974, data_8960, data_8996;
+extern const struct tsens_data data_sdm845;
 
 #endif /* __QCOM_TSENS_H__ */