Re: [PATCH v1 3/3] thermal: zx2967: add thermal driver for ZTE's zx2967 family

2017-01-09 Thread Shawn Guo
On Mon, Jan 09, 2017 at 11:00:38AM +0800, Jun Nie wrote:
> >+static int zx2967_thermal_resume(struct device *dev)
> >+{
> >+struct platform_device *pdev = to_platform_device(dev);
> >+struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
> >+int error;
> >+
> >+error = clk_prepare_enable(priv->clk_gate);
> >+if (error)
> Use IS_ERR(ret) to check error.

No.  IS_ERR() checks on pointer, while clk_prepare_enable() returns
integer.

Shawn

> >+return error;
> >+
> >+error = clk_prepare_enable(priv->pclk);
> >+if (error)
> Ditto.
> >+return error;
> >+
> >+dev_info(dev, "resumed\n");
> >+
> >+return 0;
> >+}


Re: [PATCH v1 3/3] thermal: zx2967: add thermal driver for ZTE's zx2967 family

2017-01-09 Thread Shawn Guo
On Mon, Jan 09, 2017 at 11:00:38AM +0800, Jun Nie wrote:
> >+static int zx2967_thermal_resume(struct device *dev)
> >+{
> >+struct platform_device *pdev = to_platform_device(dev);
> >+struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
> >+int error;
> >+
> >+error = clk_prepare_enable(priv->clk_gate);
> >+if (error)
> Use IS_ERR(ret) to check error.

No.  IS_ERR() checks on pointer, while clk_prepare_enable() returns
integer.

Shawn

> >+return error;
> >+
> >+error = clk_prepare_enable(priv->pclk);
> >+if (error)
> Ditto.
> >+return error;
> >+
> >+dev_info(dev, "resumed\n");
> >+
> >+return 0;
> >+}


Re: [PATCH v1 3/3] thermal: zx2967: add thermal driver for ZTE's zx2967 family

2017-01-08 Thread Shawn Guo
On Sat, Jan 07, 2017 at 01:38:08PM +0800, Baoyou Xie wrote:
> This patch adds thermal driver for ZTE's zx2967 family.
> 
> Signed-off-by: Baoyou Xie 
> ---
>  drivers/thermal/Kconfig  |   6 +
>  drivers/thermal/Makefile |   1 +
>  drivers/thermal/zx2967_thermal.c | 241 
> +++
>  3 files changed, 248 insertions(+)
>  create mode 100644 drivers/thermal/zx2967_thermal.c
> 
> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> index 18f2de6..0dd597e 100644
> --- a/drivers/thermal/Kconfig
> +++ b/drivers/thermal/Kconfig
> @@ -445,3 +445,9 @@ config BCM2835_THERMAL
> Support for thermal sensors on Broadcom bcm2835 SoCs.
>  
>  endif
> +
> +config ZX2967_THERMAL
> + tristate "Thermal sensors on zx2967 SoC"
> + depends on ARCH_ZX
> + help
> +   Support for thermal sensors on ZTE zx2967 SoCs.
> diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
> index 677c6d9..c00c05e 100644
> --- a/drivers/thermal/Makefile
> +++ b/drivers/thermal/Makefile
> @@ -57,3 +57,4 @@ obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o
>  obj-$(CONFIG_MTK_THERMAL)+= mtk_thermal.o
>  obj-$(CONFIG_GENERIC_ADC_THERMAL)+= thermal-generic-adc.o
>  obj-$(CONFIG_BCM2835_THERMAL)+= bcm2835_thermal.o
> +obj-$(CONFIG_ZX2967_THERMAL) += zx2967_thermal.o
> diff --git a/drivers/thermal/zx2967_thermal.c 
> b/drivers/thermal/zx2967_thermal.c
> new file mode 100644
> index 000..1aef070
> --- /dev/null
> +++ b/drivers/thermal/zx2967_thermal.c
> @@ -0,0 +1,241 @@
> +/*
> + * ZTE's zx2967 family thermal sensor driver
> + *
> + * Copyright (C) 2017 ZTE Ltd.
> + *
> + * Author: Baoyou Xie 
> + *
> + * License terms: GNU General Public License (GPL) version 2
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +/* DCF Control Register */
> +#define ZX2967_THERMAL_DCF   0x4
> +
> +/* Selection Register */
> +#define ZX2967_THERMAL_SEL   0x8
> +
> +/* Control Register */
> +#define ZX2967_THERMAL_CTRL  0x10
> +
> +#define ZX2967_THERMAL_ID_MASK   (0x18)
> +
> +struct zx2967_thermal_sensor {
> + struct zx2967_thermal_priv *priv;
> + struct thermal_zone_device *tzd;
> + int id;
> +};
> +
> +#define NUM_SENSORS  1
> +
> +struct zx2967_thermal_priv {
> + struct zx2967_thermal_sensorsensors[NUM_SENSORS];

What's the point of defining an array with only one element?

> + struct mutexlock;
> + struct clk  *clk_gate;
> + struct clk  *pclk;
> + void __iomem*regs;

> + struct pinctrl  *pinmux_dvi0_d3;
> + struct pinctrl  *pinmux_dvi0_d4;
> + struct pinctrl  *pinmux_dvi0_d5;

These three pointers are not used.

> +};
> +
> +static int zx2967_thermal_suspend(struct device *dev)
> +{
> + struct platform_device *pdev = to_platform_device(dev);
> + struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
> +
> + if (priv && priv->pclk)
> + clk_disable_unprepare(priv->pclk);
> +
> + if (priv && priv->clk_gate)
> + clk_disable_unprepare(priv->clk_gate);
> + dev_info(dev, "suspended\n");

Noisy message.

> +
> + return 0;
> +}
> +
> +static int zx2967_thermal_resume(struct device *dev)
> +{
> + struct platform_device *pdev = to_platform_device(dev);
> + struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
> + int error;
> +
> + error = clk_prepare_enable(priv->clk_gate);
> + if (error)
> + return error;
> +
> + error = clk_prepare_enable(priv->pclk);
> + if (error)
> + return error;

clk_disable_unprepare() should be called for priv->clk_gate before
returning here.

> +
> + dev_info(dev, "resumed\n");
> +
> + return 0;
> +}
> +
> +static int zx2967_thermal_get_temp(void *data, int *temp)
> +{
> + void __iomem *regs;
> + struct zx2967_thermal_sensor *sensor = data;
> + struct zx2967_thermal_priv *priv = sensor->priv;
> + unsigned long timeout = jiffies + msecs_to_jiffies(100);
> + u32 val, sel_id;
> +
> + regs = priv->regs;
> + mutex_lock(>lock);
> +
> + writel_relaxed(0, regs);

I suggest we have a macro for register at offset 0 as well to improve
the readability.

> + writel_relaxed(2, regs + ZX2967_THERMAL_DCF);
> +
> + val = readl_relaxed(regs + ZX2967_THERMAL_SEL);
> + val &= ~ZX2967_THERMAL_ID_MASK;
> + sel_id = sensor->id ? 8 : 0x10;
> + val |= sel_id;
> + writel_relaxed(val, regs + ZX2967_THERMAL_SEL);
> +
> + usleep_range(100, 300);
> + while (!(readl_relaxed(regs + ZX2967_THERMAL_CTRL) & 0x1000)) {
> + if (time_after(jiffies, timeout)) {
> + pr_err("*** Thermal sensor %d data timeout\n",
> +   

Re: [PATCH v1 3/3] thermal: zx2967: add thermal driver for ZTE's zx2967 family

2017-01-08 Thread Jun Nie

On 2017年01月07日 13:38, Baoyou Xie wrote:

This patch adds thermal driver for ZTE's zx2967 family.

Signed-off-by: Baoyou Xie 
---
 drivers/thermal/Kconfig  |   6 +
 drivers/thermal/Makefile |   1 +
 drivers/thermal/zx2967_thermal.c | 241 +++
 3 files changed, 248 insertions(+)
 create mode 100644 drivers/thermal/zx2967_thermal.c

diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 18f2de6..0dd597e 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -445,3 +445,9 @@ config BCM2835_THERMAL
  Support for thermal sensors on Broadcom bcm2835 SoCs.

 endif
+
+config ZX2967_THERMAL
+   tristate "Thermal sensors on zx2967 SoC"
+   depends on ARCH_ZX
+   help
+ Support for thermal sensors on ZTE zx2967 SoCs.
diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
index 677c6d9..c00c05e 100644
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -57,3 +57,4 @@ obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o
 obj-$(CONFIG_MTK_THERMAL)  += mtk_thermal.o
 obj-$(CONFIG_GENERIC_ADC_THERMAL)  += thermal-generic-adc.o
 obj-$(CONFIG_BCM2835_THERMAL)  += bcm2835_thermal.o
+obj-$(CONFIG_ZX2967_THERMAL)   += zx2967_thermal.o
diff --git a/drivers/thermal/zx2967_thermal.c b/drivers/thermal/zx2967_thermal.c
new file mode 100644
index 000..1aef070
--- /dev/null
+++ b/drivers/thermal/zx2967_thermal.c
@@ -0,0 +1,241 @@
+/*
+ * ZTE's zx2967 family thermal sensor driver
+ *
+ * Copyright (C) 2017 ZTE Ltd.
+ *
+ * Author: Baoyou Xie 
+ *
+ * License terms: GNU General Public License (GPL) version 2
+ */
+
+#include 

Please follow alphabet sequence.

+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* DCF Control Register */
+#define ZX2967_THERMAL_DCF 0x4
+
+/* Selection Register */
+#define ZX2967_THERMAL_SEL 0x8
+
+/* Control Register */
+#define ZX2967_THERMAL_CTRL0x10
+
+#define ZX2967_THERMAL_ID_MASK (0x18)
+
+struct zx2967_thermal_sensor {
+   struct zx2967_thermal_priv *priv;
+   struct thermal_zone_device *tzd;
+   int id;
+};
+
+#define NUM_SENSORS1
+
+struct zx2967_thermal_priv {
+   struct zx2967_thermal_sensorsensors[NUM_SENSORS];
+   struct mutexlock;
+   struct clk  *clk_gate;
+   struct clk  *pclk;
+   void __iomem*regs;
+   struct pinctrl  *pinmux_dvi0_d3;
+   struct pinctrl  *pinmux_dvi0_d4;
+   struct pinctrl  *pinmux_dvi0_d5;


I do not see usage of pinmux_div0_d*, please remove it.


+};
+
+static int zx2967_thermal_suspend(struct device *dev)
+{
+   struct platform_device *pdev = to_platform_device(dev);
+   struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
+
+   if (priv && priv->pclk)
+   clk_disable_unprepare(priv->pclk);
+
+   if (priv && priv->clk_gate)
+   clk_disable_unprepare(priv->clk_gate);
+   dev_info(dev, "suspended\n");
+
+   return 0;
+}
+
+static int zx2967_thermal_resume(struct device *dev)
+{
+   struct platform_device *pdev = to_platform_device(dev);
+   struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
+   int error;
+
+   error = clk_prepare_enable(priv->clk_gate);
+   if (error)

Use IS_ERR(ret) to check error.

+   return error;
+
+   error = clk_prepare_enable(priv->pclk);
+   if (error)

Ditto.

+   return error;
+
+   dev_info(dev, "resumed\n");
+
+   return 0;
+}
+
+static int zx2967_thermal_get_temp(void *data, int *temp)
+{
+   void __iomem *regs;
+   struct zx2967_thermal_sensor *sensor = data;
+   struct zx2967_thermal_priv *priv = sensor->priv;
+   unsigned long timeout = jiffies + msecs_to_jiffies(100);
+   u32 val, sel_id;
+
+   regs = priv->regs;
+   mutex_lock(>lock);
+
+   writel_relaxed(0, regs);
+   writel_relaxed(2, regs + ZX2967_THERMAL_DCF);
+
+   val = readl_relaxed(regs + ZX2967_THERMAL_SEL);
+   val &= ~ZX2967_THERMAL_ID_MASK;
+   sel_id = sensor->id ? 8 : 0x10;


You can define a macro for 8 and 0x10. BTW: NUM_SENSORS is 1 currently, 
you can change it to 2 if hardware support it. Or you can add TODO mark 
for later work.



+   val |= sel_id;
+   writel_relaxed(val, regs + ZX2967_THERMAL_SEL);
+
+   usleep_range(100, 300);
+   while (!(readl_relaxed(regs + ZX2967_THERMAL_CTRL) & 0x1000)) {
+   if (time_after(jiffies, timeout)) {
+   pr_err("*** Thermal sensor %d data timeout\n",
+ sensor->id);
+   mutex_unlock(>lock);
+   return -EIO;
+   }
+   }
+
+   writel_relaxed(3, regs + ZX2967_THERMAL_DCF);
+   val = 

Re: [PATCH v1 3/3] thermal: zx2967: add thermal driver for ZTE's zx2967 family

2017-01-08 Thread Shawn Guo
On Sat, Jan 07, 2017 at 01:38:08PM +0800, Baoyou Xie wrote:
> This patch adds thermal driver for ZTE's zx2967 family.
> 
> Signed-off-by: Baoyou Xie 
> ---
>  drivers/thermal/Kconfig  |   6 +
>  drivers/thermal/Makefile |   1 +
>  drivers/thermal/zx2967_thermal.c | 241 
> +++
>  3 files changed, 248 insertions(+)
>  create mode 100644 drivers/thermal/zx2967_thermal.c
> 
> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> index 18f2de6..0dd597e 100644
> --- a/drivers/thermal/Kconfig
> +++ b/drivers/thermal/Kconfig
> @@ -445,3 +445,9 @@ config BCM2835_THERMAL
> Support for thermal sensors on Broadcom bcm2835 SoCs.
>  
>  endif
> +
> +config ZX2967_THERMAL
> + tristate "Thermal sensors on zx2967 SoC"
> + depends on ARCH_ZX
> + help
> +   Support for thermal sensors on ZTE zx2967 SoCs.
> diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
> index 677c6d9..c00c05e 100644
> --- a/drivers/thermal/Makefile
> +++ b/drivers/thermal/Makefile
> @@ -57,3 +57,4 @@ obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o
>  obj-$(CONFIG_MTK_THERMAL)+= mtk_thermal.o
>  obj-$(CONFIG_GENERIC_ADC_THERMAL)+= thermal-generic-adc.o
>  obj-$(CONFIG_BCM2835_THERMAL)+= bcm2835_thermal.o
> +obj-$(CONFIG_ZX2967_THERMAL) += zx2967_thermal.o
> diff --git a/drivers/thermal/zx2967_thermal.c 
> b/drivers/thermal/zx2967_thermal.c
> new file mode 100644
> index 000..1aef070
> --- /dev/null
> +++ b/drivers/thermal/zx2967_thermal.c
> @@ -0,0 +1,241 @@
> +/*
> + * ZTE's zx2967 family thermal sensor driver
> + *
> + * Copyright (C) 2017 ZTE Ltd.
> + *
> + * Author: Baoyou Xie 
> + *
> + * License terms: GNU General Public License (GPL) version 2
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +/* DCF Control Register */
> +#define ZX2967_THERMAL_DCF   0x4
> +
> +/* Selection Register */
> +#define ZX2967_THERMAL_SEL   0x8
> +
> +/* Control Register */
> +#define ZX2967_THERMAL_CTRL  0x10
> +
> +#define ZX2967_THERMAL_ID_MASK   (0x18)
> +
> +struct zx2967_thermal_sensor {
> + struct zx2967_thermal_priv *priv;
> + struct thermal_zone_device *tzd;
> + int id;
> +};
> +
> +#define NUM_SENSORS  1
> +
> +struct zx2967_thermal_priv {
> + struct zx2967_thermal_sensorsensors[NUM_SENSORS];

What's the point of defining an array with only one element?

> + struct mutexlock;
> + struct clk  *clk_gate;
> + struct clk  *pclk;
> + void __iomem*regs;

> + struct pinctrl  *pinmux_dvi0_d3;
> + struct pinctrl  *pinmux_dvi0_d4;
> + struct pinctrl  *pinmux_dvi0_d5;

These three pointers are not used.

> +};
> +
> +static int zx2967_thermal_suspend(struct device *dev)
> +{
> + struct platform_device *pdev = to_platform_device(dev);
> + struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
> +
> + if (priv && priv->pclk)
> + clk_disable_unprepare(priv->pclk);
> +
> + if (priv && priv->clk_gate)
> + clk_disable_unprepare(priv->clk_gate);
> + dev_info(dev, "suspended\n");

Noisy message.

> +
> + return 0;
> +}
> +
> +static int zx2967_thermal_resume(struct device *dev)
> +{
> + struct platform_device *pdev = to_platform_device(dev);
> + struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
> + int error;
> +
> + error = clk_prepare_enable(priv->clk_gate);
> + if (error)
> + return error;
> +
> + error = clk_prepare_enable(priv->pclk);
> + if (error)
> + return error;

clk_disable_unprepare() should be called for priv->clk_gate before
returning here.

> +
> + dev_info(dev, "resumed\n");
> +
> + return 0;
> +}
> +
> +static int zx2967_thermal_get_temp(void *data, int *temp)
> +{
> + void __iomem *regs;
> + struct zx2967_thermal_sensor *sensor = data;
> + struct zx2967_thermal_priv *priv = sensor->priv;
> + unsigned long timeout = jiffies + msecs_to_jiffies(100);
> + u32 val, sel_id;
> +
> + regs = priv->regs;
> + mutex_lock(>lock);
> +
> + writel_relaxed(0, regs);

I suggest we have a macro for register at offset 0 as well to improve
the readability.

> + writel_relaxed(2, regs + ZX2967_THERMAL_DCF);
> +
> + val = readl_relaxed(regs + ZX2967_THERMAL_SEL);
> + val &= ~ZX2967_THERMAL_ID_MASK;
> + sel_id = sensor->id ? 8 : 0x10;
> + val |= sel_id;
> + writel_relaxed(val, regs + ZX2967_THERMAL_SEL);
> +
> + usleep_range(100, 300);
> + while (!(readl_relaxed(regs + ZX2967_THERMAL_CTRL) & 0x1000)) {
> + if (time_after(jiffies, timeout)) {
> + pr_err("*** Thermal sensor %d data timeout\n",
> +   sensor->id);

dev_err?  And drop "*** ".

> + 

Re: [PATCH v1 3/3] thermal: zx2967: add thermal driver for ZTE's zx2967 family

2017-01-08 Thread Jun Nie

On 2017年01月07日 13:38, Baoyou Xie wrote:

This patch adds thermal driver for ZTE's zx2967 family.

Signed-off-by: Baoyou Xie 
---
 drivers/thermal/Kconfig  |   6 +
 drivers/thermal/Makefile |   1 +
 drivers/thermal/zx2967_thermal.c | 241 +++
 3 files changed, 248 insertions(+)
 create mode 100644 drivers/thermal/zx2967_thermal.c

diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 18f2de6..0dd597e 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -445,3 +445,9 @@ config BCM2835_THERMAL
  Support for thermal sensors on Broadcom bcm2835 SoCs.

 endif
+
+config ZX2967_THERMAL
+   tristate "Thermal sensors on zx2967 SoC"
+   depends on ARCH_ZX
+   help
+ Support for thermal sensors on ZTE zx2967 SoCs.
diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
index 677c6d9..c00c05e 100644
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -57,3 +57,4 @@ obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o
 obj-$(CONFIG_MTK_THERMAL)  += mtk_thermal.o
 obj-$(CONFIG_GENERIC_ADC_THERMAL)  += thermal-generic-adc.o
 obj-$(CONFIG_BCM2835_THERMAL)  += bcm2835_thermal.o
+obj-$(CONFIG_ZX2967_THERMAL)   += zx2967_thermal.o
diff --git a/drivers/thermal/zx2967_thermal.c b/drivers/thermal/zx2967_thermal.c
new file mode 100644
index 000..1aef070
--- /dev/null
+++ b/drivers/thermal/zx2967_thermal.c
@@ -0,0 +1,241 @@
+/*
+ * ZTE's zx2967 family thermal sensor driver
+ *
+ * Copyright (C) 2017 ZTE Ltd.
+ *
+ * Author: Baoyou Xie 
+ *
+ * License terms: GNU General Public License (GPL) version 2
+ */
+
+#include 

Please follow alphabet sequence.

+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* DCF Control Register */
+#define ZX2967_THERMAL_DCF 0x4
+
+/* Selection Register */
+#define ZX2967_THERMAL_SEL 0x8
+
+/* Control Register */
+#define ZX2967_THERMAL_CTRL0x10
+
+#define ZX2967_THERMAL_ID_MASK (0x18)
+
+struct zx2967_thermal_sensor {
+   struct zx2967_thermal_priv *priv;
+   struct thermal_zone_device *tzd;
+   int id;
+};
+
+#define NUM_SENSORS1
+
+struct zx2967_thermal_priv {
+   struct zx2967_thermal_sensorsensors[NUM_SENSORS];
+   struct mutexlock;
+   struct clk  *clk_gate;
+   struct clk  *pclk;
+   void __iomem*regs;
+   struct pinctrl  *pinmux_dvi0_d3;
+   struct pinctrl  *pinmux_dvi0_d4;
+   struct pinctrl  *pinmux_dvi0_d5;


I do not see usage of pinmux_div0_d*, please remove it.


+};
+
+static int zx2967_thermal_suspend(struct device *dev)
+{
+   struct platform_device *pdev = to_platform_device(dev);
+   struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
+
+   if (priv && priv->pclk)
+   clk_disable_unprepare(priv->pclk);
+
+   if (priv && priv->clk_gate)
+   clk_disable_unprepare(priv->clk_gate);
+   dev_info(dev, "suspended\n");
+
+   return 0;
+}
+
+static int zx2967_thermal_resume(struct device *dev)
+{
+   struct platform_device *pdev = to_platform_device(dev);
+   struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
+   int error;
+
+   error = clk_prepare_enable(priv->clk_gate);
+   if (error)

Use IS_ERR(ret) to check error.

+   return error;
+
+   error = clk_prepare_enable(priv->pclk);
+   if (error)

Ditto.

+   return error;
+
+   dev_info(dev, "resumed\n");
+
+   return 0;
+}
+
+static int zx2967_thermal_get_temp(void *data, int *temp)
+{
+   void __iomem *regs;
+   struct zx2967_thermal_sensor *sensor = data;
+   struct zx2967_thermal_priv *priv = sensor->priv;
+   unsigned long timeout = jiffies + msecs_to_jiffies(100);
+   u32 val, sel_id;
+
+   regs = priv->regs;
+   mutex_lock(>lock);
+
+   writel_relaxed(0, regs);
+   writel_relaxed(2, regs + ZX2967_THERMAL_DCF);
+
+   val = readl_relaxed(regs + ZX2967_THERMAL_SEL);
+   val &= ~ZX2967_THERMAL_ID_MASK;
+   sel_id = sensor->id ? 8 : 0x10;


You can define a macro for 8 and 0x10. BTW: NUM_SENSORS is 1 currently, 
you can change it to 2 if hardware support it. Or you can add TODO mark 
for later work.



+   val |= sel_id;
+   writel_relaxed(val, regs + ZX2967_THERMAL_SEL);
+
+   usleep_range(100, 300);
+   while (!(readl_relaxed(regs + ZX2967_THERMAL_CTRL) & 0x1000)) {
+   if (time_after(jiffies, timeout)) {
+   pr_err("*** Thermal sensor %d data timeout\n",
+ sensor->id);
+   mutex_unlock(>lock);
+   return -EIO;
+   }
+   }
+
+   writel_relaxed(3, regs + ZX2967_THERMAL_DCF);
+   val = readl_relaxed(regs + ZX2967_THERMAL_CTRL) & 

[PATCH v1 3/3] thermal: zx2967: add thermal driver for ZTE's zx2967 family

2017-01-06 Thread Baoyou Xie
This patch adds thermal driver for ZTE's zx2967 family.

Signed-off-by: Baoyou Xie 
---
 drivers/thermal/Kconfig  |   6 +
 drivers/thermal/Makefile |   1 +
 drivers/thermal/zx2967_thermal.c | 241 +++
 3 files changed, 248 insertions(+)
 create mode 100644 drivers/thermal/zx2967_thermal.c

diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 18f2de6..0dd597e 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -445,3 +445,9 @@ config BCM2835_THERMAL
  Support for thermal sensors on Broadcom bcm2835 SoCs.
 
 endif
+
+config ZX2967_THERMAL
+   tristate "Thermal sensors on zx2967 SoC"
+   depends on ARCH_ZX
+   help
+ Support for thermal sensors on ZTE zx2967 SoCs.
diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
index 677c6d9..c00c05e 100644
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -57,3 +57,4 @@ obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o
 obj-$(CONFIG_MTK_THERMAL)  += mtk_thermal.o
 obj-$(CONFIG_GENERIC_ADC_THERMAL)  += thermal-generic-adc.o
 obj-$(CONFIG_BCM2835_THERMAL)  += bcm2835_thermal.o
+obj-$(CONFIG_ZX2967_THERMAL)   += zx2967_thermal.o
diff --git a/drivers/thermal/zx2967_thermal.c b/drivers/thermal/zx2967_thermal.c
new file mode 100644
index 000..1aef070
--- /dev/null
+++ b/drivers/thermal/zx2967_thermal.c
@@ -0,0 +1,241 @@
+/*
+ * ZTE's zx2967 family thermal sensor driver
+ *
+ * Copyright (C) 2017 ZTE Ltd.
+ *
+ * Author: Baoyou Xie 
+ *
+ * License terms: GNU General Public License (GPL) version 2
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* DCF Control Register */
+#define ZX2967_THERMAL_DCF 0x4
+
+/* Selection Register */
+#define ZX2967_THERMAL_SEL 0x8
+
+/* Control Register */
+#define ZX2967_THERMAL_CTRL0x10
+
+#define ZX2967_THERMAL_ID_MASK (0x18)
+
+struct zx2967_thermal_sensor {
+   struct zx2967_thermal_priv *priv;
+   struct thermal_zone_device *tzd;
+   int id;
+};
+
+#define NUM_SENSORS1
+
+struct zx2967_thermal_priv {
+   struct zx2967_thermal_sensorsensors[NUM_SENSORS];
+   struct mutexlock;
+   struct clk  *clk_gate;
+   struct clk  *pclk;
+   void __iomem*regs;
+   struct pinctrl  *pinmux_dvi0_d3;
+   struct pinctrl  *pinmux_dvi0_d4;
+   struct pinctrl  *pinmux_dvi0_d5;
+};
+
+static int zx2967_thermal_suspend(struct device *dev)
+{
+   struct platform_device *pdev = to_platform_device(dev);
+   struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
+
+   if (priv && priv->pclk)
+   clk_disable_unprepare(priv->pclk);
+
+   if (priv && priv->clk_gate)
+   clk_disable_unprepare(priv->clk_gate);
+   dev_info(dev, "suspended\n");
+
+   return 0;
+}
+
+static int zx2967_thermal_resume(struct device *dev)
+{
+   struct platform_device *pdev = to_platform_device(dev);
+   struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
+   int error;
+
+   error = clk_prepare_enable(priv->clk_gate);
+   if (error)
+   return error;
+
+   error = clk_prepare_enable(priv->pclk);
+   if (error)
+   return error;
+
+   dev_info(dev, "resumed\n");
+
+   return 0;
+}
+
+static int zx2967_thermal_get_temp(void *data, int *temp)
+{
+   void __iomem *regs;
+   struct zx2967_thermal_sensor *sensor = data;
+   struct zx2967_thermal_priv *priv = sensor->priv;
+   unsigned long timeout = jiffies + msecs_to_jiffies(100);
+   u32 val, sel_id;
+
+   regs = priv->regs;
+   mutex_lock(>lock);
+
+   writel_relaxed(0, regs);
+   writel_relaxed(2, regs + ZX2967_THERMAL_DCF);
+
+   val = readl_relaxed(regs + ZX2967_THERMAL_SEL);
+   val &= ~ZX2967_THERMAL_ID_MASK;
+   sel_id = sensor->id ? 8 : 0x10;
+   val |= sel_id;
+   writel_relaxed(val, regs + ZX2967_THERMAL_SEL);
+
+   usleep_range(100, 300);
+   while (!(readl_relaxed(regs + ZX2967_THERMAL_CTRL) & 0x1000)) {
+   if (time_after(jiffies, timeout)) {
+   pr_err("*** Thermal sensor %d data timeout\n",
+ sensor->id);
+   mutex_unlock(>lock);
+   return -EIO;
+   }
+   }
+
+   writel_relaxed(3, regs + ZX2967_THERMAL_DCF);
+   val = readl_relaxed(regs + ZX2967_THERMAL_CTRL) & 0xfff;
+   writel_relaxed(1, regs);
+
+   /** Calculate temperature */
+   *temp = DIV_ROUND_CLOSEST((val - 922) * 1000, 1951);
+
+   mutex_unlock(>lock);
+
+   return 0;
+}
+
+static struct thermal_zone_of_device_ops zx2967_of_thermal_ops = {
+   .get_temp = 

[PATCH v1 3/3] thermal: zx2967: add thermal driver for ZTE's zx2967 family

2017-01-06 Thread Baoyou Xie
This patch adds thermal driver for ZTE's zx2967 family.

Signed-off-by: Baoyou Xie 
---
 drivers/thermal/Kconfig  |   6 +
 drivers/thermal/Makefile |   1 +
 drivers/thermal/zx2967_thermal.c | 241 +++
 3 files changed, 248 insertions(+)
 create mode 100644 drivers/thermal/zx2967_thermal.c

diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 18f2de6..0dd597e 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -445,3 +445,9 @@ config BCM2835_THERMAL
  Support for thermal sensors on Broadcom bcm2835 SoCs.
 
 endif
+
+config ZX2967_THERMAL
+   tristate "Thermal sensors on zx2967 SoC"
+   depends on ARCH_ZX
+   help
+ Support for thermal sensors on ZTE zx2967 SoCs.
diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
index 677c6d9..c00c05e 100644
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -57,3 +57,4 @@ obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o
 obj-$(CONFIG_MTK_THERMAL)  += mtk_thermal.o
 obj-$(CONFIG_GENERIC_ADC_THERMAL)  += thermal-generic-adc.o
 obj-$(CONFIG_BCM2835_THERMAL)  += bcm2835_thermal.o
+obj-$(CONFIG_ZX2967_THERMAL)   += zx2967_thermal.o
diff --git a/drivers/thermal/zx2967_thermal.c b/drivers/thermal/zx2967_thermal.c
new file mode 100644
index 000..1aef070
--- /dev/null
+++ b/drivers/thermal/zx2967_thermal.c
@@ -0,0 +1,241 @@
+/*
+ * ZTE's zx2967 family thermal sensor driver
+ *
+ * Copyright (C) 2017 ZTE Ltd.
+ *
+ * Author: Baoyou Xie 
+ *
+ * License terms: GNU General Public License (GPL) version 2
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* DCF Control Register */
+#define ZX2967_THERMAL_DCF 0x4
+
+/* Selection Register */
+#define ZX2967_THERMAL_SEL 0x8
+
+/* Control Register */
+#define ZX2967_THERMAL_CTRL0x10
+
+#define ZX2967_THERMAL_ID_MASK (0x18)
+
+struct zx2967_thermal_sensor {
+   struct zx2967_thermal_priv *priv;
+   struct thermal_zone_device *tzd;
+   int id;
+};
+
+#define NUM_SENSORS1
+
+struct zx2967_thermal_priv {
+   struct zx2967_thermal_sensorsensors[NUM_SENSORS];
+   struct mutexlock;
+   struct clk  *clk_gate;
+   struct clk  *pclk;
+   void __iomem*regs;
+   struct pinctrl  *pinmux_dvi0_d3;
+   struct pinctrl  *pinmux_dvi0_d4;
+   struct pinctrl  *pinmux_dvi0_d5;
+};
+
+static int zx2967_thermal_suspend(struct device *dev)
+{
+   struct platform_device *pdev = to_platform_device(dev);
+   struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
+
+   if (priv && priv->pclk)
+   clk_disable_unprepare(priv->pclk);
+
+   if (priv && priv->clk_gate)
+   clk_disable_unprepare(priv->clk_gate);
+   dev_info(dev, "suspended\n");
+
+   return 0;
+}
+
+static int zx2967_thermal_resume(struct device *dev)
+{
+   struct platform_device *pdev = to_platform_device(dev);
+   struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
+   int error;
+
+   error = clk_prepare_enable(priv->clk_gate);
+   if (error)
+   return error;
+
+   error = clk_prepare_enable(priv->pclk);
+   if (error)
+   return error;
+
+   dev_info(dev, "resumed\n");
+
+   return 0;
+}
+
+static int zx2967_thermal_get_temp(void *data, int *temp)
+{
+   void __iomem *regs;
+   struct zx2967_thermal_sensor *sensor = data;
+   struct zx2967_thermal_priv *priv = sensor->priv;
+   unsigned long timeout = jiffies + msecs_to_jiffies(100);
+   u32 val, sel_id;
+
+   regs = priv->regs;
+   mutex_lock(>lock);
+
+   writel_relaxed(0, regs);
+   writel_relaxed(2, regs + ZX2967_THERMAL_DCF);
+
+   val = readl_relaxed(regs + ZX2967_THERMAL_SEL);
+   val &= ~ZX2967_THERMAL_ID_MASK;
+   sel_id = sensor->id ? 8 : 0x10;
+   val |= sel_id;
+   writel_relaxed(val, regs + ZX2967_THERMAL_SEL);
+
+   usleep_range(100, 300);
+   while (!(readl_relaxed(regs + ZX2967_THERMAL_CTRL) & 0x1000)) {
+   if (time_after(jiffies, timeout)) {
+   pr_err("*** Thermal sensor %d data timeout\n",
+ sensor->id);
+   mutex_unlock(>lock);
+   return -EIO;
+   }
+   }
+
+   writel_relaxed(3, regs + ZX2967_THERMAL_DCF);
+   val = readl_relaxed(regs + ZX2967_THERMAL_CTRL) & 0xfff;
+   writel_relaxed(1, regs);
+
+   /** Calculate temperature */
+   *temp = DIV_ROUND_CLOSEST((val - 922) * 1000, 1951);
+
+   mutex_unlock(>lock);
+
+   return 0;
+}
+
+static struct thermal_zone_of_device_ops zx2967_of_thermal_ops = {
+   .get_temp = zx2967_thermal_get_temp,
+};
+
+static int