All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 0/6] Add MediaTek PMIC keys support
@ 2017-09-27 10:44 ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-rtc

Changes since v4:
- rebase to Linux 4.14-rc1
- add a common keyboard binding document
- use child device tree node to define each key

Changes since v3:
- make the naming to be consistent as mtk_pmic or MTK_PMIC
- add suspend/resume functions to enable/disable irq
- change binding properties to define wakeup sources

Changes since v2:
- use standard properties for keycodes and debounce time
- change to use platform_get_irq in leaf drivers
- use better ways to define IRQ resources

Changes since v1:
- create irq mappings in mfd core driver instead of leaf drivers
- remove some unused parts in mtk-pmic-keys driver

MediaTek PMIC are multi-function devices that can handle key interrupts,
typically there are two keys attached to PMIC, which called pwrkey
and homekey. PWRKEY usually used to wake up system from sleep. Homekey
can used as volume down key due to board design. Long press keys can
shutdown PMIC, the mode can be choose to be one key only or two keys
together.
This series add support for key functions for MediaTek PMIC MT6397/MT6323.

Chen Zhong (6):
  mfd: mt6397: create irq mappings in mfd core driver
  dt-bindings: input: Add common keyboard document bindings
  dt-bindings: input: Add document bindings for mtk-pmic-keys
  dt-bindings: mfd: Add bindings for the keys as subnode of PMIC
  input: Add MediaTek PMIC keys support
  mfd: mt6397: Add PMIC keys support to MT6397 driver

 Documentation/devicetree/bindings/input/keys.txt   |   8 +
 .../devicetree/bindings/input/mtk-pmic-keys.txt    |  43 +++
 Documentation/devicetree/bindings/mfd/mt6397.txt   |   6 +
 drivers/input/keyboard/Kconfig                     |   9 +
 drivers/input/keyboard/Makefile                    |   1 +
 drivers/input/keyboard/mtk-pmic-keys.c             | 341 +++++++++++++++++++++
 drivers/mfd/mt6397-core.c                          |  26 +-
 drivers/rtc/rtc-mt6397.c                           |   7 +-
 8 files changed, 434 insertions(+), 7 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/input/keys.txt
 create mode 100644 Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
 create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c

-- 
1.9.1

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [PATCH v5 0/6] Add MediaTek PMIC keys support
@ 2017-09-27 10:44 ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-rtc

Changes since v4:
- rebase to Linux 4.14-rc1
- add a common keyboard binding document
- use child device tree node to define each key

Changes since v3:
- make the naming to be consistent as mtk_pmic or MTK_PMIC
- add suspend/resume functions to enable/disable irq
- change binding properties to define wakeup sources

Changes since v2:
- use standard properties for keycodes and debounce time
- change to use platform_get_irq in leaf drivers
- use better ways to define IRQ resources

Changes since v1:
- create irq mappings in mfd core driver instead of leaf drivers
- remove some unused parts in mtk-pmic-keys driver

MediaTek PMIC are multi-function devices that can handle key interrupts,
typically there are two keys attached to PMIC, which called pwrkey
and homekey. PWRKEY usually used to wake up system from sleep. Homekey
can used as volume down key due to board design. Long press keys can
shutdown PMIC, the mode can be choose to be one key only or two keys
together.
This series add support for key functions for MediaTek PMIC MT6397/MT6323.

Chen Zhong (6):
  mfd: mt6397: create irq mappings in mfd core driver
  dt-bindings: input: Add common keyboard document bindings
  dt-bindings: input: Add document bindings for mtk-pmic-keys
  dt-bindings: mfd: Add bindings for the keys as subnode of PMIC
  input: Add MediaTek PMIC keys support
  mfd: mt6397: Add PMIC keys support to MT6397 driver

 Documentation/devicetree/bindings/input/keys.txt   |   8 +
 .../devicetree/bindings/input/mtk-pmic-keys.txt    |  43 +++
 Documentation/devicetree/bindings/mfd/mt6397.txt   |   6 +
 drivers/input/keyboard/Kconfig                     |   9 +
 drivers/input/keyboard/Makefile                    |   1 +
 drivers/input/keyboard/mtk-pmic-keys.c             | 341 +++++++++++++++++++++
 drivers/mfd/mt6397-core.c                          |  26 +-
 drivers/rtc/rtc-mt6397.c                           |   7 +-
 8 files changed, 434 insertions(+), 7 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/input/keys.txt
 create mode 100644 Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
 create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c

-- 
1.9.1

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [PATCH v5 0/6] Add MediaTek PMIC keys support
@ 2017-09-27 10:44 ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: linux-arm-kernel

Changes since v4:
- rebase to Linux 4.14-rc1
- add a common keyboard binding document
- use child device tree node to define each key

Changes since v3:
- make the naming to be consistent as mtk_pmic or MTK_PMIC
- add suspend/resume functions to enable/disable irq
- change binding properties to define wakeup sources

Changes since v2:
- use standard properties for keycodes and debounce time
- change to use platform_get_irq in leaf drivers
- use better ways to define IRQ resources

Changes since v1:
- create irq mappings in mfd core driver instead of leaf drivers
- remove some unused parts in mtk-pmic-keys driver

MediaTek PMIC are multi-function devices that can handle key interrupts,
typically there are two keys attached to PMIC, which called pwrkey
and homekey. PWRKEY usually used to wake up system from sleep. Homekey
can used as volume down key due to board design. Long press keys can
shutdown PMIC, the mode can be choose to be one key only or two keys
together.
This series add support for key functions for MediaTek PMIC MT6397/MT6323.

Chen Zhong (6):
  mfd: mt6397: create irq mappings in mfd core driver
  dt-bindings: input: Add common keyboard document bindings
  dt-bindings: input: Add document bindings for mtk-pmic-keys
  dt-bindings: mfd: Add bindings for the keys as subnode of PMIC
  input: Add MediaTek PMIC keys support
  mfd: mt6397: Add PMIC keys support to MT6397 driver

 Documentation/devicetree/bindings/input/keys.txt   |   8 +
 .../devicetree/bindings/input/mtk-pmic-keys.txt    |  43 +++
 Documentation/devicetree/bindings/mfd/mt6397.txt   |   6 +
 drivers/input/keyboard/Kconfig                     |   9 +
 drivers/input/keyboard/Makefile                    |   1 +
 drivers/input/keyboard/mtk-pmic-keys.c             | 341 +++++++++++++++++++++
 drivers/mfd/mt6397-core.c                          |  26 +-
 drivers/rtc/rtc-mt6397.c                           |   7 +-
 8 files changed, 434 insertions(+), 7 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/input/keys.txt
 create mode 100644 Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
 create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c

-- 
1.9.1

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [PATCH v5 1/6] mfd: mt6397: create irq mappings in mfd core driver
  2017-09-27 10:44 ` Chen Zhong
  (?)
@ 2017-09-27 10:44   ` Chen Zhong
  -1 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-rtc

The core driver should create and manage irq mappings instead of
leaf drivers. This patch change to pass irq domain to
devm_mfd_add_devices() and it will create mapping for irq resources
automatically. And remove irq mapping in rtc driver since this has
been done in core driver.

Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
Acked-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 drivers/mfd/mt6397-core.c |    4 ++--
 drivers/rtc/rtc-mt6397.c  |    7 +++----
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/drivers/mfd/mt6397-core.c b/drivers/mfd/mt6397-core.c
index 04a601f..6546d7f 100644
--- a/drivers/mfd/mt6397-core.c
+++ b/drivers/mfd/mt6397-core.c
@@ -289,7 +289,7 @@ static int mt6397_probe(struct platform_device *pdev)
 
 		ret = devm_mfd_add_devices(&pdev->dev, -1, mt6323_devs,
 					   ARRAY_SIZE(mt6323_devs), NULL,
-					   0, NULL);
+					   0, pmic->irq_domain);
 		break;
 
 	case MT6397_CID_CODE:
@@ -304,7 +304,7 @@ static int mt6397_probe(struct platform_device *pdev)
 
 		ret = devm_mfd_add_devices(&pdev->dev, -1, mt6397_devs,
 					   ARRAY_SIZE(mt6397_devs), NULL,
-					   0, NULL);
+					   0, pmic->irq_domain);
 		break;
 
 	default:
diff --git a/drivers/rtc/rtc-mt6397.c b/drivers/rtc/rtc-mt6397.c
index 1a61fa5..385f830 100644
--- a/drivers/rtc/rtc-mt6397.c
+++ b/drivers/rtc/rtc-mt6397.c
@@ -322,10 +322,9 @@ static int mtk_rtc_probe(struct platform_device *pdev)
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	rtc->addr_base = res->start;
 
-	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
-	rtc->irq = irq_create_mapping(mt6397_chip->irq_domain, res->start);
-	if (rtc->irq <= 0)
-		return -EINVAL;
+	rtc->irq = platform_get_irq(pdev, 0);
+	if (rtc->irq < 0)
+		return rtc->irq;
 
 	rtc->regmap = mt6397_chip->regmap;
 	rtc->dev = &pdev->dev;
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 1/6] mfd: mt6397: create irq mappings in mfd core driver
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-rtc

The core driver should create and manage irq mappings instead of
leaf drivers. This patch change to pass irq domain to
devm_mfd_add_devices() and it will create mapping for irq resources
automatically. And remove irq mapping in rtc driver since this has
been done in core driver.

Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
Acked-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 drivers/mfd/mt6397-core.c |    4 ++--
 drivers/rtc/rtc-mt6397.c  |    7 +++----
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/drivers/mfd/mt6397-core.c b/drivers/mfd/mt6397-core.c
index 04a601f..6546d7f 100644
--- a/drivers/mfd/mt6397-core.c
+++ b/drivers/mfd/mt6397-core.c
@@ -289,7 +289,7 @@ static int mt6397_probe(struct platform_device *pdev)
 
 		ret = devm_mfd_add_devices(&pdev->dev, -1, mt6323_devs,
 					   ARRAY_SIZE(mt6323_devs), NULL,
-					   0, NULL);
+					   0, pmic->irq_domain);
 		break;
 
 	case MT6397_CID_CODE:
@@ -304,7 +304,7 @@ static int mt6397_probe(struct platform_device *pdev)
 
 		ret = devm_mfd_add_devices(&pdev->dev, -1, mt6397_devs,
 					   ARRAY_SIZE(mt6397_devs), NULL,
-					   0, NULL);
+					   0, pmic->irq_domain);
 		break;
 
 	default:
diff --git a/drivers/rtc/rtc-mt6397.c b/drivers/rtc/rtc-mt6397.c
index 1a61fa5..385f830 100644
--- a/drivers/rtc/rtc-mt6397.c
+++ b/drivers/rtc/rtc-mt6397.c
@@ -322,10 +322,9 @@ static int mtk_rtc_probe(struct platform_device *pdev)
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	rtc->addr_base = res->start;
 
-	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
-	rtc->irq = irq_create_mapping(mt6397_chip->irq_domain, res->start);
-	if (rtc->irq <= 0)
-		return -EINVAL;
+	rtc->irq = platform_get_irq(pdev, 0);
+	if (rtc->irq < 0)
+		return rtc->irq;
 
 	rtc->regmap = mt6397_chip->regmap;
 	rtc->dev = &pdev->dev;
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 1/6] mfd: mt6397: create irq mappings in mfd core driver
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: linux-arm-kernel

The core driver should create and manage irq mappings instead of
leaf drivers. This patch change to pass irq domain to
devm_mfd_add_devices() and it will create mapping for irq resources
automatically. And remove irq mapping in rtc driver since this has
been done in core driver.

Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
Acked-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 drivers/mfd/mt6397-core.c |    4 ++--
 drivers/rtc/rtc-mt6397.c  |    7 +++----
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/drivers/mfd/mt6397-core.c b/drivers/mfd/mt6397-core.c
index 04a601f..6546d7f 100644
--- a/drivers/mfd/mt6397-core.c
+++ b/drivers/mfd/mt6397-core.c
@@ -289,7 +289,7 @@ static int mt6397_probe(struct platform_device *pdev)
 
 		ret = devm_mfd_add_devices(&pdev->dev, -1, mt6323_devs,
 					   ARRAY_SIZE(mt6323_devs), NULL,
-					   0, NULL);
+					   0, pmic->irq_domain);
 		break;
 
 	case MT6397_CID_CODE:
@@ -304,7 +304,7 @@ static int mt6397_probe(struct platform_device *pdev)
 
 		ret = devm_mfd_add_devices(&pdev->dev, -1, mt6397_devs,
 					   ARRAY_SIZE(mt6397_devs), NULL,
-					   0, NULL);
+					   0, pmic->irq_domain);
 		break;
 
 	default:
diff --git a/drivers/rtc/rtc-mt6397.c b/drivers/rtc/rtc-mt6397.c
index 1a61fa5..385f830 100644
--- a/drivers/rtc/rtc-mt6397.c
+++ b/drivers/rtc/rtc-mt6397.c
@@ -322,10 +322,9 @@ static int mtk_rtc_probe(struct platform_device *pdev)
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	rtc->addr_base = res->start;
 
-	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
-	rtc->irq = irq_create_mapping(mt6397_chip->irq_domain, res->start);
-	if (rtc->irq <= 0)
-		return -EINVAL;
+	rtc->irq = platform_get_irq(pdev, 0);
+	if (rtc->irq < 0)
+		return rtc->irq;
 
 	rtc->regmap = mt6397_chip->regmap;
 	rtc->dev = &pdev->dev;
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 2/6] dt-bindings: input: Add common keyboard document bindings
  2017-09-27 10:44 ` Chen Zhong
  (?)
@ 2017-09-27 10:44   ` Chen Zhong
  -1 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-rtc

This patch adds the device tree binding documentation for common
keyboard.

Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 Documentation/devicetree/bindings/input/keys.txt |    8 ++++++++
 1 file changed, 8 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/keys.txt

diff --git a/Documentation/devicetree/bindings/input/keys.txt b/Documentation/devicetree/bindings/input/keys.txt
new file mode 100644
index 0000000..a0ea7eb
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/keys.txt
@@ -0,0 +1,8 @@
+General Keys Properties:
+
+Optional properties for Keys:
+- power-off-time-sec: Duration in seconds which the key should be kept
+ 	pressed for device to power off automatically. Device with key pressed
+ 	shutdown feature can specify this property.
+- linux,keycodes: Specifies the numeric keycode values to be used for
+	reporting key presses.
\ No newline at end of file
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 2/6] dt-bindings: input: Add common keyboard document bindings
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Alessandro Zummo, devicetree, Linus Walleij,
	Jaechul Lee, linux-kernel, Andi Shyti, Chanwoo Choi,
	linux-mediatek, linux-input, Matthias Brugger, Eddie Huang,
	Chen Zhong, linux-arm-kernel, linux-rtc

This patch adds the device tree binding documentation for common
keyboard.

Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 Documentation/devicetree/bindings/input/keys.txt |    8 ++++++++
 1 file changed, 8 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/keys.txt

diff --git a/Documentation/devicetree/bindings/input/keys.txt b/Documentation/devicetree/bindings/input/keys.txt
new file mode 100644
index 0000000..a0ea7eb
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/keys.txt
@@ -0,0 +1,8 @@
+General Keys Properties:
+
+Optional properties for Keys:
+- power-off-time-sec: Duration in seconds which the key should be kept
+ 	pressed for device to power off automatically. Device with key pressed
+ 	shutdown feature can specify this property.
+- linux,keycodes: Specifies the numeric keycode values to be used for
+	reporting key presses.
\ No newline at end of file
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 2/6] dt-bindings: input: Add common keyboard document bindings
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: linux-arm-kernel

This patch adds the device tree binding documentation for common
keyboard.

Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 Documentation/devicetree/bindings/input/keys.txt |    8 ++++++++
 1 file changed, 8 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/keys.txt

diff --git a/Documentation/devicetree/bindings/input/keys.txt b/Documentation/devicetree/bindings/input/keys.txt
new file mode 100644
index 0000000..a0ea7eb
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/keys.txt
@@ -0,0 +1,8 @@
+General Keys Properties:
+
+Optional properties for Keys:
+- power-off-time-sec: Duration in seconds which the key should be kept
+ 	pressed for device to power off automatically. Device with key pressed
+ 	shutdown feature can specify this property.
+- linux,keycodes: Specifies the numeric keycode values to be used for
+	reporting key presses.
\ No newline at end of file
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 3/6] dt-bindings: input: Add document bindings for mtk-pmic-keys
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-rtc

This patch adds the device tree binding documentation for the MediaTek
pmic keys found on PMIC MT6397/MT6323.

Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 .../devicetree/bindings/input/mtk-pmic-keys.txt    |   43 ++++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/mtk-pmic-keys.txt

diff --git a/Documentation/devicetree/bindings/input/mtk-pmic-keys.txt b/Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
new file mode 100644
index 0000000..2888d07
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
@@ -0,0 +1,43 @@
+MediaTek MT6397/MT6323 PMIC Keys Device Driver
+
+There are two key functions provided by MT6397/MT6323 PMIC, pwrkey
+and homekey. The key functions are defined as the subnode of the function
+node provided by MT6397/MT6323 PMIC that is being defined as one kind
+of Muti-Function Device (MFD)
+
+For MT6397/MT6323 MFD bindings see:
+Documentation/devicetree/bindings/mfd/mt6397.txt
+
+Required properties:
+- compatible: "mediatek,mt6397-keys" or "mediatek,mt6323-keys"
+- linux,keycodes: See Documentation/devicetree/bindings/input/keys.txt
+
+Optional Properties:
+- wakeup-source: See Documentation/devicetree/bindings/power/wakeup-source.txt
+- mediatek,long-press-mode: Long press key shutdown setting, 1 for
+	pwrkey only, 2 for pwrkey/homekey together, others for disabled.
+- power-off-time-sec: See Documentation/devicetree/bindings/input/keys.txt
+
+Example:
+
+	pmic: mt6397 {
+		compatible = "mediatek,mt6397";
+
+		...
+
+		mt6397keys: mt6397keys {
+			compatible = "mediatek,mt6397-keys";
+			mediatek,long-press-mode = <1>;
+			power-off-time-sec = <0>;
+
+			power {
+				linux,keycodes = <116>;
+				wakeup-source;
+			};
+
+			home {
+				linux,keycodes = <114>;
+			};
+		};
+
+	};
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 3/6] dt-bindings: input: Add document bindings for mtk-pmic-keys
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input-u79uwXL29TY76Z2rM5mHXA,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-rtc-u79uwXL29TY76Z2rM5mHXA

This patch adds the device tree binding documentation for the MediaTek
pmic keys found on PMIC MT6397/MT6323.

Signed-off-by: Chen Zhong <chen.zhong-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
---
 .../devicetree/bindings/input/mtk-pmic-keys.txt    |   43 ++++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/mtk-pmic-keys.txt

diff --git a/Documentation/devicetree/bindings/input/mtk-pmic-keys.txt b/Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
new file mode 100644
index 0000000..2888d07
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
@@ -0,0 +1,43 @@
+MediaTek MT6397/MT6323 PMIC Keys Device Driver
+
+There are two key functions provided by MT6397/MT6323 PMIC, pwrkey
+and homekey. The key functions are defined as the subnode of the function
+node provided by MT6397/MT6323 PMIC that is being defined as one kind
+of Muti-Function Device (MFD)
+
+For MT6397/MT6323 MFD bindings see:
+Documentation/devicetree/bindings/mfd/mt6397.txt
+
+Required properties:
+- compatible: "mediatek,mt6397-keys" or "mediatek,mt6323-keys"
+- linux,keycodes: See Documentation/devicetree/bindings/input/keys.txt
+
+Optional Properties:
+- wakeup-source: See Documentation/devicetree/bindings/power/wakeup-source.txt
+- mediatek,long-press-mode: Long press key shutdown setting, 1 for
+	pwrkey only, 2 for pwrkey/homekey together, others for disabled.
+- power-off-time-sec: See Documentation/devicetree/bindings/input/keys.txt
+
+Example:
+
+	pmic: mt6397 {
+		compatible = "mediatek,mt6397";
+
+		...
+
+		mt6397keys: mt6397keys {
+			compatible = "mediatek,mt6397-keys";
+			mediatek,long-press-mode = <1>;
+			power-off-time-sec = <0>;
+
+			power {
+				linux,keycodes = <116>;
+				wakeup-source;
+			};
+
+			home {
+				linux,keycodes = <114>;
+			};
+		};
+
+	};
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 3/6] dt-bindings: input: Add document bindings for mtk-pmic-keys
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: linux-arm-kernel

This patch adds the device tree binding documentation for the MediaTek
pmic keys found on PMIC MT6397/MT6323.

Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 .../devicetree/bindings/input/mtk-pmic-keys.txt    |   43 ++++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/mtk-pmic-keys.txt

diff --git a/Documentation/devicetree/bindings/input/mtk-pmic-keys.txt b/Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
new file mode 100644
index 0000000..2888d07
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
@@ -0,0 +1,43 @@
+MediaTek MT6397/MT6323 PMIC Keys Device Driver
+
+There are two key functions provided by MT6397/MT6323 PMIC, pwrkey
+and homekey. The key functions are defined as the subnode of the function
+node provided by MT6397/MT6323 PMIC that is being defined as one kind
+of Muti-Function Device (MFD)
+
+For MT6397/MT6323 MFD bindings see:
+Documentation/devicetree/bindings/mfd/mt6397.txt
+
+Required properties:
+- compatible: "mediatek,mt6397-keys" or "mediatek,mt6323-keys"
+- linux,keycodes: See Documentation/devicetree/bindings/input/keys.txt
+
+Optional Properties:
+- wakeup-source: See Documentation/devicetree/bindings/power/wakeup-source.txt
+- mediatek,long-press-mode: Long press key shutdown setting, 1 for
+	pwrkey only, 2 for pwrkey/homekey together, others for disabled.
+- power-off-time-sec: See Documentation/devicetree/bindings/input/keys.txt
+
+Example:
+
+	pmic: mt6397 {
+		compatible = "mediatek,mt6397";
+
+		...
+
+		mt6397keys: mt6397keys {
+			compatible = "mediatek,mt6397-keys";
+			mediatek,long-press-mode = <1>;
+			power-off-time-sec = <0>;
+
+			power {
+				linux,keycodes = <116>;
+				wakeup-source;
+			};
+
+			home {
+				linux,keycodes = <114>;
+			};
+		};
+
+	};
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 4/6] dt-bindings: mfd: Add bindings for the keys as subnode of PMIC
  2017-09-27 10:44 ` Chen Zhong
  (?)
@ 2017-09-27 10:44   ` Chen Zhong
  -1 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-rtc

This patch adds documentation for device tree bindings for keys support
as the subnode of MT6397/MT6323 PMIC.

Acked-by: Rob Herring <robh@kernel.org>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 Documentation/devicetree/bindings/mfd/mt6397.txt |    6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/Documentation/devicetree/bindings/mfd/mt6397.txt b/Documentation/devicetree/bindings/mfd/mt6397.txt
index 522a3bb..d1df77f 100644
--- a/Documentation/devicetree/bindings/mfd/mt6397.txt
+++ b/Documentation/devicetree/bindings/mfd/mt6397.txt
@@ -7,6 +7,7 @@ MT6397/MT6323 is a multifunction device with the following sub modules:
 - GPIO
 - Clock
 - LED
+- Keys
 
 It is interfaced to host controller using SPI interface by a proprietary hardware
 called PMIC wrapper or pwrap. MT6397/MT6323 MFD is a child device of pwrap.
@@ -40,6 +41,11 @@ Optional subnodes:
 		- compatible: "mediatek,mt6323-led"
 	see Documentation/devicetree/bindings/leds/leds-mt6323.txt
 
+- keys
+	Required properties:
+		- compatible: "mediatek,mt6397-keys" or "mediatek,mt6323-keys"
+	see Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
+
 Example:
 	pwrap: pwrap@1000f000 {
 		compatible = "mediatek,mt8135-pwrap";
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 4/6] dt-bindings: mfd: Add bindings for the keys as subnode of PMIC
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-rtc

This patch adds documentation for device tree bindings for keys support
as the subnode of MT6397/MT6323 PMIC.

Acked-by: Rob Herring <robh@kernel.org>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 Documentation/devicetree/bindings/mfd/mt6397.txt |    6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/Documentation/devicetree/bindings/mfd/mt6397.txt b/Documentation/devicetree/bindings/mfd/mt6397.txt
index 522a3bb..d1df77f 100644
--- a/Documentation/devicetree/bindings/mfd/mt6397.txt
+++ b/Documentation/devicetree/bindings/mfd/mt6397.txt
@@ -7,6 +7,7 @@ MT6397/MT6323 is a multifunction device with the following sub modules:
 - GPIO
 - Clock
 - LED
+- Keys
 
 It is interfaced to host controller using SPI interface by a proprietary hardware
 called PMIC wrapper or pwrap. MT6397/MT6323 MFD is a child device of pwrap.
@@ -40,6 +41,11 @@ Optional subnodes:
 		- compatible: "mediatek,mt6323-led"
 	see Documentation/devicetree/bindings/leds/leds-mt6323.txt
 
+- keys
+	Required properties:
+		- compatible: "mediatek,mt6397-keys" or "mediatek,mt6323-keys"
+	see Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
+
 Example:
 	pwrap: pwrap@1000f000 {
 		compatible = "mediatek,mt8135-pwrap";
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 4/6] dt-bindings: mfd: Add bindings for the keys as subnode of PMIC
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: linux-arm-kernel

This patch adds documentation for device tree bindings for keys support
as the subnode of MT6397/MT6323 PMIC.

Acked-by: Rob Herring <robh@kernel.org>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 Documentation/devicetree/bindings/mfd/mt6397.txt |    6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/Documentation/devicetree/bindings/mfd/mt6397.txt b/Documentation/devicetree/bindings/mfd/mt6397.txt
index 522a3bb..d1df77f 100644
--- a/Documentation/devicetree/bindings/mfd/mt6397.txt
+++ b/Documentation/devicetree/bindings/mfd/mt6397.txt
@@ -7,6 +7,7 @@ MT6397/MT6323 is a multifunction device with the following sub modules:
 - GPIO
 - Clock
 - LED
+- Keys
 
 It is interfaced to host controller using SPI interface by a proprietary hardware
 called PMIC wrapper or pwrap. MT6397/MT6323 MFD is a child device of pwrap.
@@ -40,6 +41,11 @@ Optional subnodes:
 		- compatible: "mediatek,mt6323-led"
 	see Documentation/devicetree/bindings/leds/leds-mt6323.txt
 
+- keys
+	Required properties:
+		- compatible: "mediatek,mt6397-keys" or "mediatek,mt6323-keys"
+	see Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
+
 Example:
 	pwrap: pwrap at 1000f000 {
 		compatible = "mediatek,mt8135-pwrap";
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 5/6] input: Add MediaTek PMIC keys support
  2017-09-27 10:44 ` Chen Zhong
  (?)
@ 2017-09-27 10:44   ` Chen Zhong
  -1 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-rtc

This patch add support to handle MediaTek PMIC MT6397/MT6323 key
interrupts including pwrkey and homekey, also add setting for
long press key shutdown behavior.

Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 drivers/input/keyboard/Kconfig         |    9 +
 drivers/input/keyboard/Makefile        |    1 +
 drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
 3 files changed, 351 insertions(+)
 create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c

diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index 4c4ab1c..bd4e20a 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -756,4 +756,13 @@ config KEYBOARD_BCM
 	  To compile this driver as a module, choose M here: the
 	  module will be called bcm-keypad.
 
+config KEYBOARD_MTK_PMIC
+	tristate "MediaTek PMIC keys support"
+	depends on MFD_MT6397
+	help
+	  Say Y here if you want to use the pmic keys (powerkey/homekey).
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called pmic-keys.
+
 endif
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index d2338ba..20c0b98 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
 obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
 obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
 obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
+obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
 obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
 obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
 obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
new file mode 100644
index 0000000..529fd95
--- /dev/null
+++ b/drivers/input/keyboard/mtk-pmic-keys.c
@@ -0,0 +1,341 @@
+/*
+ * Copyright (C) 2017 MediaTek, Inc.
+ *
+ * Author: Chen Zhong <chen.zhong@mediatek.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <linux/kernel.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/regmap.h>
+#include <linux/mfd/mt6323/registers.h>
+#include <linux/mfd/mt6397/registers.h>
+#include <linux/mfd/mt6397/core.h>
+
+#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
+#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
+#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
+#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
+#define MTK_PMIC_RST_DU_MASK		0x3
+#define MTK_PMIC_RST_DU_SHIFT		8
+
+#define MTK_PMIC_PWRKEY_RST		\
+	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
+#define MTK_PMIC_HOMEKEY_RST		\
+	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
+
+#define MTK_PMIC_PWRKEY_INDEX	0
+#define MTK_PMIC_HOMEKEY_INDEX	1
+#define MTK_PMIC_MAX_KEY_COUNT	2
+
+struct mtk_pmic_keys_regs {
+	u32 deb_reg;
+	u32 deb_mask;
+	u32 intsel_reg;
+	u32 intsel_mask;
+};
+
+#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
+	_intsel_reg, _intsel_mask)			\
+{							\
+	.deb_reg		= _deb_reg,		\
+	.deb_mask		= _deb_mask,		\
+	.intsel_reg		= _intsel_reg,		\
+	.intsel_mask		= _intsel_mask,		\
+}
+
+struct mtk_pmic_regs {
+	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
+	u32 pmic_rst_reg;
+};
+
+static const struct mtk_pmic_regs mt6397_regs = {
+	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
+		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
+		0x8, MT6397_INT_RSV, 0x10),
+	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
+		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
+		0x10, MT6397_INT_RSV, 0x8),
+	.pmic_rst_reg = MT6397_TOP_RST_MISC,
+};
+
+static const struct mtk_pmic_regs mt6323_regs = {
+	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
+		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
+		0x2, MT6323_INT_MISC_CON, 0x10),
+	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
+		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
+		0x4, MT6323_INT_MISC_CON, 0x8),
+	.pmic_rst_reg = MT6323_TOP_RST_MISC,
+};
+
+struct mtk_pmic_keys_info {
+	struct mtk_pmic_keys *keys;
+	const struct mtk_pmic_keys_regs *regs;
+	unsigned int keycode;
+	int irq;
+	bool wakeup:1;
+};
+
+struct mtk_pmic_keys {
+	struct input_dev *input_dev;
+	struct device *dev;
+	struct regmap *regmap;
+	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
+};
+
+enum mtk_pmic_keys_lp_mode {
+	LP_DISABLE,
+	LP_ONEKEY,
+	LP_TWOKEY,
+};
+
+static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
+		u32 pmic_rst_reg)
+{
+	int ret;
+	u32 long_press_mode, long_press_debounce;
+
+	ret = of_property_read_u32(keys->dev->of_node,
+		"power-off-time-sec", &long_press_debounce);
+	if (ret)
+		long_press_debounce = 0;
+
+	regmap_update_bits(keys->regmap, pmic_rst_reg,
+			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
+			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
+
+	ret = of_property_read_u32(keys->dev->of_node,
+		"mediatek,long-press-mode", &long_press_mode);
+	if (ret)
+		long_press_mode = LP_DISABLE;
+
+	switch (long_press_mode) {
+	case LP_ONEKEY:
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_PWRKEY_RST,
+				   MTK_PMIC_PWRKEY_RST);
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_HOMEKEY_RST,
+				   0);
+		break;
+	case LP_TWOKEY:
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_PWRKEY_RST,
+				   MTK_PMIC_PWRKEY_RST);
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_HOMEKEY_RST,
+				   MTK_PMIC_HOMEKEY_RST);
+		break;
+	case LP_DISABLE:
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_PWRKEY_RST,
+				   0);
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_HOMEKEY_RST,
+				   0);
+		break;
+	default:
+		break;
+	}
+}
+
+static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
+{
+	struct mtk_pmic_keys_info *info = data;
+	u32 key_deb, pressed;
+
+	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
+
+	key_deb &= info->regs->deb_mask;
+
+	pressed = !key_deb;
+
+	input_report_key(info->keys->input_dev, info->keycode, pressed);
+	input_sync(info->keys->input_dev);
+
+	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
+		 pressed ? "pressed" : "released", info->keycode);
+
+	return IRQ_HANDLED;
+}
+
+static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
+		struct mtk_pmic_keys_info *info)
+{
+	int ret;
+
+	info->keys = keys;
+
+	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
+				 info->regs->intsel_mask,
+				 info->regs->intsel_mask);
+	if (ret < 0)
+		return ret;
+
+	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
+					mtk_pmic_keys_irq_handler_thread,
+					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
+					"mtk-pmic-keys", info);
+	if (ret) {
+		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
+			info->irq, ret);
+		return ret;
+	}
+
+	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int mtk_pmic_keys_suspend(struct device *dev)
+{
+	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
+	int index;
+
+	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
+		if (keys->keys[index].wakeup)
+			enable_irq_wake(keys->keys[index].irq);
+	}
+
+	return 0;
+}
+
+static int mtk_pmic_keys_resume(struct device *dev)
+{
+	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
+	int index;
+
+	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
+		if (keys->keys[index].wakeup)
+			disable_irq_wake(keys->keys[index].irq);
+	}
+
+	return 0;
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
+			mtk_pmic_keys_resume);
+
+static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
+	{
+		.compatible = "mediatek,mt6397-keys",
+		.data = &mt6397_regs,
+	}, {
+		.compatible = "mediatek,mt6323-keys",
+		.data = &mt6323_regs,
+	}, {
+		/* sentinel */
+	}
+};
+MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
+
+static int mtk_pmic_keys_probe(struct platform_device *pdev)
+{
+	int error, index = 0;
+	unsigned int keycount;
+	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
+	struct device_node *node = pdev->dev.of_node, *child;
+	struct mtk_pmic_keys *keys;
+	const struct mtk_pmic_regs *mtk_pmic_regs;
+	struct input_dev *input_dev;
+	const struct of_device_id *of_id =
+		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
+
+	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
+	if (!keys)
+		return -ENOMEM;
+
+	keys->dev = &pdev->dev;
+	keys->regmap = pmic_chip->regmap;
+	mtk_pmic_regs = of_id->data;
+
+	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
+	if (!input_dev) {
+		dev_err(keys->dev, "input allocate device fail.\n");
+		return -ENOMEM;
+	}
+
+	input_dev->name = "mtk-pmic-keys";
+	input_dev->id.bustype = BUS_HOST;
+	input_dev->id.vendor = 0x0001;
+	input_dev->id.product = 0x0001;
+	input_dev->id.version = 0x0001;
+
+	keycount = device_get_child_node_count(keys->dev);
+	if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
+		dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
+		return -EINVAL;
+	}
+
+	for_each_child_of_node(node, child) {
+		keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
+
+		keys->keys[index].irq = platform_get_irq(pdev, index);
+		if (keys->keys[index].irq < 0)
+			return keys->keys[index].irq;
+
+		error = of_property_read_u32(child,
+			"linux,keycodes", &keys->keys[index].keycode);
+		if (error) {
+			dev_err(keys->dev,
+				"failed to read key:%d linux,keycode property: %d\n",
+				index, error);
+			return error;
+		}
+
+		if (of_property_read_bool(child, "wakeup-source"))
+			keys->keys[index].wakeup = true;
+
+		error = mtk_pmic_key_setup(keys, &keys->keys[index]);
+		if (error)
+			return error;
+
+		index++;
+	}
+
+	error = input_register_device(input_dev);
+	if (error) {
+		dev_err(&pdev->dev,
+			"register input device failed (%d)\n", error);
+		return error;
+	}
+
+	mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
+
+	platform_set_drvdata(pdev, keys);
+
+	return 0;
+}
+
+static struct platform_driver pmic_keys_pdrv = {
+	.probe = mtk_pmic_keys_probe,
+	.driver = {
+		   .name = "mtk-pmic-keys",
+		   .of_match_table = of_mtk_pmic_keys_match_tbl,
+		   .pm = &mtk_pmic_keys_pm_ops,
+	},
+};
+
+module_platform_driver(pmic_keys_pdrv);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
+MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-rtc

This patch add support to handle MediaTek PMIC MT6397/MT6323 key
interrupts including pwrkey and homekey, also add setting for
long press key shutdown behavior.

Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 drivers/input/keyboard/Kconfig         |    9 +
 drivers/input/keyboard/Makefile        |    1 +
 drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
 3 files changed, 351 insertions(+)
 create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c

diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index 4c4ab1c..bd4e20a 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -756,4 +756,13 @@ config KEYBOARD_BCM
 	  To compile this driver as a module, choose M here: the
 	  module will be called bcm-keypad.
 
+config KEYBOARD_MTK_PMIC
+	tristate "MediaTek PMIC keys support"
+	depends on MFD_MT6397
+	help
+	  Say Y here if you want to use the pmic keys (powerkey/homekey).
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called pmic-keys.
+
 endif
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index d2338ba..20c0b98 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
 obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
 obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
 obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
+obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
 obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
 obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
 obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
new file mode 100644
index 0000000..529fd95
--- /dev/null
+++ b/drivers/input/keyboard/mtk-pmic-keys.c
@@ -0,0 +1,341 @@
+/*
+ * Copyright (C) 2017 MediaTek, Inc.
+ *
+ * Author: Chen Zhong <chen.zhong@mediatek.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <linux/kernel.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/regmap.h>
+#include <linux/mfd/mt6323/registers.h>
+#include <linux/mfd/mt6397/registers.h>
+#include <linux/mfd/mt6397/core.h>
+
+#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
+#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
+#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
+#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
+#define MTK_PMIC_RST_DU_MASK		0x3
+#define MTK_PMIC_RST_DU_SHIFT		8
+
+#define MTK_PMIC_PWRKEY_RST		\
+	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
+#define MTK_PMIC_HOMEKEY_RST		\
+	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
+
+#define MTK_PMIC_PWRKEY_INDEX	0
+#define MTK_PMIC_HOMEKEY_INDEX	1
+#define MTK_PMIC_MAX_KEY_COUNT	2
+
+struct mtk_pmic_keys_regs {
+	u32 deb_reg;
+	u32 deb_mask;
+	u32 intsel_reg;
+	u32 intsel_mask;
+};
+
+#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
+	_intsel_reg, _intsel_mask)			\
+{							\
+	.deb_reg		= _deb_reg,		\
+	.deb_mask		= _deb_mask,		\
+	.intsel_reg		= _intsel_reg,		\
+	.intsel_mask		= _intsel_mask,		\
+}
+
+struct mtk_pmic_regs {
+	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
+	u32 pmic_rst_reg;
+};
+
+static const struct mtk_pmic_regs mt6397_regs = {
+	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
+		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
+		0x8, MT6397_INT_RSV, 0x10),
+	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
+		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
+		0x10, MT6397_INT_RSV, 0x8),
+	.pmic_rst_reg = MT6397_TOP_RST_MISC,
+};
+
+static const struct mtk_pmic_regs mt6323_regs = {
+	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
+		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
+		0x2, MT6323_INT_MISC_CON, 0x10),
+	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
+		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
+		0x4, MT6323_INT_MISC_CON, 0x8),
+	.pmic_rst_reg = MT6323_TOP_RST_MISC,
+};
+
+struct mtk_pmic_keys_info {
+	struct mtk_pmic_keys *keys;
+	const struct mtk_pmic_keys_regs *regs;
+	unsigned int keycode;
+	int irq;
+	bool wakeup:1;
+};
+
+struct mtk_pmic_keys {
+	struct input_dev *input_dev;
+	struct device *dev;
+	struct regmap *regmap;
+	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
+};
+
+enum mtk_pmic_keys_lp_mode {
+	LP_DISABLE,
+	LP_ONEKEY,
+	LP_TWOKEY,
+};
+
+static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
+		u32 pmic_rst_reg)
+{
+	int ret;
+	u32 long_press_mode, long_press_debounce;
+
+	ret = of_property_read_u32(keys->dev->of_node,
+		"power-off-time-sec", &long_press_debounce);
+	if (ret)
+		long_press_debounce = 0;
+
+	regmap_update_bits(keys->regmap, pmic_rst_reg,
+			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
+			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
+
+	ret = of_property_read_u32(keys->dev->of_node,
+		"mediatek,long-press-mode", &long_press_mode);
+	if (ret)
+		long_press_mode = LP_DISABLE;
+
+	switch (long_press_mode) {
+	case LP_ONEKEY:
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_PWRKEY_RST,
+				   MTK_PMIC_PWRKEY_RST);
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_HOMEKEY_RST,
+				   0);
+		break;
+	case LP_TWOKEY:
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_PWRKEY_RST,
+				   MTK_PMIC_PWRKEY_RST);
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_HOMEKEY_RST,
+				   MTK_PMIC_HOMEKEY_RST);
+		break;
+	case LP_DISABLE:
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_PWRKEY_RST,
+				   0);
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_HOMEKEY_RST,
+				   0);
+		break;
+	default:
+		break;
+	}
+}
+
+static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
+{
+	struct mtk_pmic_keys_info *info = data;
+	u32 key_deb, pressed;
+
+	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
+
+	key_deb &= info->regs->deb_mask;
+
+	pressed = !key_deb;
+
+	input_report_key(info->keys->input_dev, info->keycode, pressed);
+	input_sync(info->keys->input_dev);
+
+	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
+		 pressed ? "pressed" : "released", info->keycode);
+
+	return IRQ_HANDLED;
+}
+
+static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
+		struct mtk_pmic_keys_info *info)
+{
+	int ret;
+
+	info->keys = keys;
+
+	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
+				 info->regs->intsel_mask,
+				 info->regs->intsel_mask);
+	if (ret < 0)
+		return ret;
+
+	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
+					mtk_pmic_keys_irq_handler_thread,
+					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
+					"mtk-pmic-keys", info);
+	if (ret) {
+		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
+			info->irq, ret);
+		return ret;
+	}
+
+	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int mtk_pmic_keys_suspend(struct device *dev)
+{
+	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
+	int index;
+
+	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
+		if (keys->keys[index].wakeup)
+			enable_irq_wake(keys->keys[index].irq);
+	}
+
+	return 0;
+}
+
+static int mtk_pmic_keys_resume(struct device *dev)
+{
+	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
+	int index;
+
+	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
+		if (keys->keys[index].wakeup)
+			disable_irq_wake(keys->keys[index].irq);
+	}
+
+	return 0;
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
+			mtk_pmic_keys_resume);
+
+static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
+	{
+		.compatible = "mediatek,mt6397-keys",
+		.data = &mt6397_regs,
+	}, {
+		.compatible = "mediatek,mt6323-keys",
+		.data = &mt6323_regs,
+	}, {
+		/* sentinel */
+	}
+};
+MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
+
+static int mtk_pmic_keys_probe(struct platform_device *pdev)
+{
+	int error, index = 0;
+	unsigned int keycount;
+	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
+	struct device_node *node = pdev->dev.of_node, *child;
+	struct mtk_pmic_keys *keys;
+	const struct mtk_pmic_regs *mtk_pmic_regs;
+	struct input_dev *input_dev;
+	const struct of_device_id *of_id =
+		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
+
+	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
+	if (!keys)
+		return -ENOMEM;
+
+	keys->dev = &pdev->dev;
+	keys->regmap = pmic_chip->regmap;
+	mtk_pmic_regs = of_id->data;
+
+	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
+	if (!input_dev) {
+		dev_err(keys->dev, "input allocate device fail.\n");
+		return -ENOMEM;
+	}
+
+	input_dev->name = "mtk-pmic-keys";
+	input_dev->id.bustype = BUS_HOST;
+	input_dev->id.vendor = 0x0001;
+	input_dev->id.product = 0x0001;
+	input_dev->id.version = 0x0001;
+
+	keycount = device_get_child_node_count(keys->dev);
+	if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
+		dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
+		return -EINVAL;
+	}
+
+	for_each_child_of_node(node, child) {
+		keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
+
+		keys->keys[index].irq = platform_get_irq(pdev, index);
+		if (keys->keys[index].irq < 0)
+			return keys->keys[index].irq;
+
+		error = of_property_read_u32(child,
+			"linux,keycodes", &keys->keys[index].keycode);
+		if (error) {
+			dev_err(keys->dev,
+				"failed to read key:%d linux,keycode property: %d\n",
+				index, error);
+			return error;
+		}
+
+		if (of_property_read_bool(child, "wakeup-source"))
+			keys->keys[index].wakeup = true;
+
+		error = mtk_pmic_key_setup(keys, &keys->keys[index]);
+		if (error)
+			return error;
+
+		index++;
+	}
+
+	error = input_register_device(input_dev);
+	if (error) {
+		dev_err(&pdev->dev,
+			"register input device failed (%d)\n", error);
+		return error;
+	}
+
+	mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
+
+	platform_set_drvdata(pdev, keys);
+
+	return 0;
+}
+
+static struct platform_driver pmic_keys_pdrv = {
+	.probe = mtk_pmic_keys_probe,
+	.driver = {
+		   .name = "mtk-pmic-keys",
+		   .of_match_table = of_mtk_pmic_keys_match_tbl,
+		   .pm = &mtk_pmic_keys_pm_ops,
+	},
+};
+
+module_platform_driver(pmic_keys_pdrv);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
+MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: linux-arm-kernel

This patch add support to handle MediaTek PMIC MT6397/MT6323 key
interrupts including pwrkey and homekey, also add setting for
long press key shutdown behavior.

Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 drivers/input/keyboard/Kconfig         |    9 +
 drivers/input/keyboard/Makefile        |    1 +
 drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
 3 files changed, 351 insertions(+)
 create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c

diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index 4c4ab1c..bd4e20a 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -756,4 +756,13 @@ config KEYBOARD_BCM
 	  To compile this driver as a module, choose M here: the
 	  module will be called bcm-keypad.
 
+config KEYBOARD_MTK_PMIC
+	tristate "MediaTek PMIC keys support"
+	depends on MFD_MT6397
+	help
+	  Say Y here if you want to use the pmic keys (powerkey/homekey).
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called pmic-keys.
+
 endif
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index d2338ba..20c0b98 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
 obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
 obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
 obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
+obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
 obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
 obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
 obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
new file mode 100644
index 0000000..529fd95
--- /dev/null
+++ b/drivers/input/keyboard/mtk-pmic-keys.c
@@ -0,0 +1,341 @@
+/*
+ * Copyright (C) 2017 MediaTek, Inc.
+ *
+ * Author: Chen Zhong <chen.zhong@mediatek.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <linux/kernel.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/regmap.h>
+#include <linux/mfd/mt6323/registers.h>
+#include <linux/mfd/mt6397/registers.h>
+#include <linux/mfd/mt6397/core.h>
+
+#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
+#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
+#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
+#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
+#define MTK_PMIC_RST_DU_MASK		0x3
+#define MTK_PMIC_RST_DU_SHIFT		8
+
+#define MTK_PMIC_PWRKEY_RST		\
+	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
+#define MTK_PMIC_HOMEKEY_RST		\
+	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
+
+#define MTK_PMIC_PWRKEY_INDEX	0
+#define MTK_PMIC_HOMEKEY_INDEX	1
+#define MTK_PMIC_MAX_KEY_COUNT	2
+
+struct mtk_pmic_keys_regs {
+	u32 deb_reg;
+	u32 deb_mask;
+	u32 intsel_reg;
+	u32 intsel_mask;
+};
+
+#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
+	_intsel_reg, _intsel_mask)			\
+{							\
+	.deb_reg		= _deb_reg,		\
+	.deb_mask		= _deb_mask,		\
+	.intsel_reg		= _intsel_reg,		\
+	.intsel_mask		= _intsel_mask,		\
+}
+
+struct mtk_pmic_regs {
+	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
+	u32 pmic_rst_reg;
+};
+
+static const struct mtk_pmic_regs mt6397_regs = {
+	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
+		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
+		0x8, MT6397_INT_RSV, 0x10),
+	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
+		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
+		0x10, MT6397_INT_RSV, 0x8),
+	.pmic_rst_reg = MT6397_TOP_RST_MISC,
+};
+
+static const struct mtk_pmic_regs mt6323_regs = {
+	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
+		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
+		0x2, MT6323_INT_MISC_CON, 0x10),
+	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
+		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
+		0x4, MT6323_INT_MISC_CON, 0x8),
+	.pmic_rst_reg = MT6323_TOP_RST_MISC,
+};
+
+struct mtk_pmic_keys_info {
+	struct mtk_pmic_keys *keys;
+	const struct mtk_pmic_keys_regs *regs;
+	unsigned int keycode;
+	int irq;
+	bool wakeup:1;
+};
+
+struct mtk_pmic_keys {
+	struct input_dev *input_dev;
+	struct device *dev;
+	struct regmap *regmap;
+	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
+};
+
+enum mtk_pmic_keys_lp_mode {
+	LP_DISABLE,
+	LP_ONEKEY,
+	LP_TWOKEY,
+};
+
+static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
+		u32 pmic_rst_reg)
+{
+	int ret;
+	u32 long_press_mode, long_press_debounce;
+
+	ret = of_property_read_u32(keys->dev->of_node,
+		"power-off-time-sec", &long_press_debounce);
+	if (ret)
+		long_press_debounce = 0;
+
+	regmap_update_bits(keys->regmap, pmic_rst_reg,
+			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
+			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
+
+	ret = of_property_read_u32(keys->dev->of_node,
+		"mediatek,long-press-mode", &long_press_mode);
+	if (ret)
+		long_press_mode = LP_DISABLE;
+
+	switch (long_press_mode) {
+	case LP_ONEKEY:
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_PWRKEY_RST,
+				   MTK_PMIC_PWRKEY_RST);
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_HOMEKEY_RST,
+				   0);
+		break;
+	case LP_TWOKEY:
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_PWRKEY_RST,
+				   MTK_PMIC_PWRKEY_RST);
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_HOMEKEY_RST,
+				   MTK_PMIC_HOMEKEY_RST);
+		break;
+	case LP_DISABLE:
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_PWRKEY_RST,
+				   0);
+		regmap_update_bits(keys->regmap, pmic_rst_reg,
+				   MTK_PMIC_HOMEKEY_RST,
+				   0);
+		break;
+	default:
+		break;
+	}
+}
+
+static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
+{
+	struct mtk_pmic_keys_info *info = data;
+	u32 key_deb, pressed;
+
+	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
+
+	key_deb &= info->regs->deb_mask;
+
+	pressed = !key_deb;
+
+	input_report_key(info->keys->input_dev, info->keycode, pressed);
+	input_sync(info->keys->input_dev);
+
+	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
+		 pressed ? "pressed" : "released", info->keycode);
+
+	return IRQ_HANDLED;
+}
+
+static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
+		struct mtk_pmic_keys_info *info)
+{
+	int ret;
+
+	info->keys = keys;
+
+	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
+				 info->regs->intsel_mask,
+				 info->regs->intsel_mask);
+	if (ret < 0)
+		return ret;
+
+	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
+					mtk_pmic_keys_irq_handler_thread,
+					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
+					"mtk-pmic-keys", info);
+	if (ret) {
+		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
+			info->irq, ret);
+		return ret;
+	}
+
+	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int mtk_pmic_keys_suspend(struct device *dev)
+{
+	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
+	int index;
+
+	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
+		if (keys->keys[index].wakeup)
+			enable_irq_wake(keys->keys[index].irq);
+	}
+
+	return 0;
+}
+
+static int mtk_pmic_keys_resume(struct device *dev)
+{
+	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
+	int index;
+
+	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
+		if (keys->keys[index].wakeup)
+			disable_irq_wake(keys->keys[index].irq);
+	}
+
+	return 0;
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
+			mtk_pmic_keys_resume);
+
+static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
+	{
+		.compatible = "mediatek,mt6397-keys",
+		.data = &mt6397_regs,
+	}, {
+		.compatible = "mediatek,mt6323-keys",
+		.data = &mt6323_regs,
+	}, {
+		/* sentinel */
+	}
+};
+MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
+
+static int mtk_pmic_keys_probe(struct platform_device *pdev)
+{
+	int error, index = 0;
+	unsigned int keycount;
+	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
+	struct device_node *node = pdev->dev.of_node, *child;
+	struct mtk_pmic_keys *keys;
+	const struct mtk_pmic_regs *mtk_pmic_regs;
+	struct input_dev *input_dev;
+	const struct of_device_id *of_id =
+		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
+
+	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
+	if (!keys)
+		return -ENOMEM;
+
+	keys->dev = &pdev->dev;
+	keys->regmap = pmic_chip->regmap;
+	mtk_pmic_regs = of_id->data;
+
+	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
+	if (!input_dev) {
+		dev_err(keys->dev, "input allocate device fail.\n");
+		return -ENOMEM;
+	}
+
+	input_dev->name = "mtk-pmic-keys";
+	input_dev->id.bustype = BUS_HOST;
+	input_dev->id.vendor = 0x0001;
+	input_dev->id.product = 0x0001;
+	input_dev->id.version = 0x0001;
+
+	keycount = device_get_child_node_count(keys->dev);
+	if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
+		dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
+		return -EINVAL;
+	}
+
+	for_each_child_of_node(node, child) {
+		keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
+
+		keys->keys[index].irq = platform_get_irq(pdev, index);
+		if (keys->keys[index].irq < 0)
+			return keys->keys[index].irq;
+
+		error = of_property_read_u32(child,
+			"linux,keycodes", &keys->keys[index].keycode);
+		if (error) {
+			dev_err(keys->dev,
+				"failed to read key:%d linux,keycode property: %d\n",
+				index, error);
+			return error;
+		}
+
+		if (of_property_read_bool(child, "wakeup-source"))
+			keys->keys[index].wakeup = true;
+
+		error = mtk_pmic_key_setup(keys, &keys->keys[index]);
+		if (error)
+			return error;
+
+		index++;
+	}
+
+	error = input_register_device(input_dev);
+	if (error) {
+		dev_err(&pdev->dev,
+			"register input device failed (%d)\n", error);
+		return error;
+	}
+
+	mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
+
+	platform_set_drvdata(pdev, keys);
+
+	return 0;
+}
+
+static struct platform_driver pmic_keys_pdrv = {
+	.probe = mtk_pmic_keys_probe,
+	.driver = {
+		   .name = "mtk-pmic-keys",
+		   .of_match_table = of_mtk_pmic_keys_match_tbl,
+		   .pm = &mtk_pmic_keys_pm_ops,
+	},
+};
+
+module_platform_driver(pmic_keys_pdrv);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
+MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 6/6] mfd: mt6397: Add PMIC keys support to MT6397 driver
  2017-09-27 10:44 ` Chen Zhong
  (?)
@ 2017-09-27 10:44   ` Chen Zhong
  -1 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-rtc

This patch adds compatible strings and interrupts for pmic keys
which serves as child device of MFD.

Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 drivers/mfd/mt6397-core.c |   22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/drivers/mfd/mt6397-core.c b/drivers/mfd/mt6397-core.c
index 6546d7f..77b64bd 100644
--- a/drivers/mfd/mt6397-core.c
+++ b/drivers/mfd/mt6397-core.c
@@ -43,6 +43,16 @@
 	},
 };
 
+static const struct resource mt6323_keys_resources[] = {
+	DEFINE_RES_IRQ(MT6323_IRQ_STATUS_PWRKEY),
+	DEFINE_RES_IRQ(MT6323_IRQ_STATUS_FCHRKEY),
+};
+
+static const struct resource mt6397_keys_resources[] = {
+	DEFINE_RES_IRQ(MT6397_IRQ_PWRKEY),
+	DEFINE_RES_IRQ(MT6397_IRQ_HOMEKEY),
+};
+
 static const struct mfd_cell mt6323_devs[] = {
 	{
 		.name = "mt6323-regulator",
@@ -50,6 +60,11 @@
 	}, {
 		.name = "mt6323-led",
 		.of_compatible = "mediatek,mt6323-led"
+	}, {
+		.name = "mtk-pmic-keys",
+		.num_resources = ARRAY_SIZE(mt6323_keys_resources),
+		.resources = mt6323_keys_resources,
+		.of_compatible = "mediatek,mt6323-keys"
 	},
 };
 
@@ -71,7 +86,12 @@
 	}, {
 		.name = "mt6397-pinctrl",
 		.of_compatible = "mediatek,mt6397-pinctrl",
-	},
+	}, {
+		.name = "mtk-pmic-keys",
+		.num_resources = ARRAY_SIZE(mt6397_keys_resources),
+		.resources = mt6397_keys_resources,
+		.of_compatible = "mediatek,mt6397-keys"
+	}
 };
 
 static void mt6397_irq_lock(struct irq_data *data)
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 6/6] mfd: mt6397: Add PMIC keys support to MT6397 driver
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
  Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Chen Zhong, Jaechul Lee, Andi Shyti,
	linux-input, devicetree, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-rtc

This patch adds compatible strings and interrupts for pmic keys
which serves as child device of MFD.

Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 drivers/mfd/mt6397-core.c |   22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/drivers/mfd/mt6397-core.c b/drivers/mfd/mt6397-core.c
index 6546d7f..77b64bd 100644
--- a/drivers/mfd/mt6397-core.c
+++ b/drivers/mfd/mt6397-core.c
@@ -43,6 +43,16 @@
 	},
 };
 
+static const struct resource mt6323_keys_resources[] = {
+	DEFINE_RES_IRQ(MT6323_IRQ_STATUS_PWRKEY),
+	DEFINE_RES_IRQ(MT6323_IRQ_STATUS_FCHRKEY),
+};
+
+static const struct resource mt6397_keys_resources[] = {
+	DEFINE_RES_IRQ(MT6397_IRQ_PWRKEY),
+	DEFINE_RES_IRQ(MT6397_IRQ_HOMEKEY),
+};
+
 static const struct mfd_cell mt6323_devs[] = {
 	{
 		.name = "mt6323-regulator",
@@ -50,6 +60,11 @@
 	}, {
 		.name = "mt6323-led",
 		.of_compatible = "mediatek,mt6323-led"
+	}, {
+		.name = "mtk-pmic-keys",
+		.num_resources = ARRAY_SIZE(mt6323_keys_resources),
+		.resources = mt6323_keys_resources,
+		.of_compatible = "mediatek,mt6323-keys"
 	},
 };
 
@@ -71,7 +86,12 @@
 	}, {
 		.name = "mt6397-pinctrl",
 		.of_compatible = "mediatek,mt6397-pinctrl",
-	},
+	}, {
+		.name = "mtk-pmic-keys",
+		.num_resources = ARRAY_SIZE(mt6397_keys_resources),
+		.resources = mt6397_keys_resources,
+		.of_compatible = "mediatek,mt6397-keys"
+	}
 };
 
 static void mt6397_irq_lock(struct irq_data *data)
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v5 6/6] mfd: mt6397: Add PMIC keys support to MT6397 driver
@ 2017-09-27 10:44   ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-09-27 10:44 UTC (permalink / raw)
  To: linux-arm-kernel

This patch adds compatible strings and interrupts for pmic keys
which serves as child device of MFD.

Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
 drivers/mfd/mt6397-core.c |   22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/drivers/mfd/mt6397-core.c b/drivers/mfd/mt6397-core.c
index 6546d7f..77b64bd 100644
--- a/drivers/mfd/mt6397-core.c
+++ b/drivers/mfd/mt6397-core.c
@@ -43,6 +43,16 @@
 	},
 };
 
+static const struct resource mt6323_keys_resources[] = {
+	DEFINE_RES_IRQ(MT6323_IRQ_STATUS_PWRKEY),
+	DEFINE_RES_IRQ(MT6323_IRQ_STATUS_FCHRKEY),
+};
+
+static const struct resource mt6397_keys_resources[] = {
+	DEFINE_RES_IRQ(MT6397_IRQ_PWRKEY),
+	DEFINE_RES_IRQ(MT6397_IRQ_HOMEKEY),
+};
+
 static const struct mfd_cell mt6323_devs[] = {
 	{
 		.name = "mt6323-regulator",
@@ -50,6 +60,11 @@
 	}, {
 		.name = "mt6323-led",
 		.of_compatible = "mediatek,mt6323-led"
+	}, {
+		.name = "mtk-pmic-keys",
+		.num_resources = ARRAY_SIZE(mt6323_keys_resources),
+		.resources = mt6323_keys_resources,
+		.of_compatible = "mediatek,mt6323-keys"
 	},
 };
 
@@ -71,7 +86,12 @@
 	}, {
 		.name = "mt6397-pinctrl",
 		.of_compatible = "mediatek,mt6397-pinctrl",
-	},
+	}, {
+		.name = "mtk-pmic-keys",
+		.num_resources = ARRAY_SIZE(mt6397_keys_resources),
+		.resources = mt6397_keys_resources,
+		.of_compatible = "mediatek,mt6397-keys"
+	}
 };
 
 static void mt6397_irq_lock(struct irq_data *data)
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 39+ messages in thread

* Re: [PATCH v5 2/6] dt-bindings: input: Add common keyboard document bindings
  2017-09-27 10:44   ` Chen Zhong
@ 2017-10-05 22:22     ` Rob Herring
  -1 siblings, 0 replies; 39+ messages in thread
From: Rob Herring @ 2017-10-05 22:22 UTC (permalink / raw)
  To: Chen Zhong
  Cc: Dmitry Torokhov, Lee Jones, Alexandre Belloni, Mark Rutland,
	Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
	Chanwoo Choi, Jaechul Lee, Andi Shyti, linux-input, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc

On Wed, Sep 27, 2017 at 06:44:04PM +0800, Chen Zhong wrote:
> This patch adds the device tree binding documentation for common
> keyboard.
> 
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> ---
>  Documentation/devicetree/bindings/input/keys.txt |    8 ++++++++
>  1 file changed, 8 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/input/keys.txt

Acked-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [PATCH v5 2/6] dt-bindings: input: Add common keyboard document bindings
@ 2017-10-05 22:22     ` Rob Herring
  0 siblings, 0 replies; 39+ messages in thread
From: Rob Herring @ 2017-10-05 22:22 UTC (permalink / raw)
  To: linux-arm-kernel

On Wed, Sep 27, 2017 at 06:44:04PM +0800, Chen Zhong wrote:
> This patch adds the device tree binding documentation for common
> keyboard.
> 
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> ---
>  Documentation/devicetree/bindings/input/keys.txt |    8 ++++++++
>  1 file changed, 8 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/input/keys.txt

Acked-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v5 3/6] dt-bindings: input: Add document bindings for mtk-pmic-keys
  2017-09-27 10:44   ` Chen Zhong
@ 2017-10-05 22:23     ` Rob Herring
  -1 siblings, 0 replies; 39+ messages in thread
From: Rob Herring @ 2017-10-05 22:23 UTC (permalink / raw)
  To: Chen Zhong
  Cc: Dmitry Torokhov, Lee Jones, Alexandre Belloni, Mark Rutland,
	Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
	Chanwoo Choi, Jaechul Lee, Andi Shyti, linux-input, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc

On Wed, Sep 27, 2017 at 06:44:05PM +0800, Chen Zhong wrote:
> This patch adds the device tree binding documentation for the MediaTek
> pmic keys found on PMIC MT6397/MT6323.
> 
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> ---
>  .../devicetree/bindings/input/mtk-pmic-keys.txt    |   43 ++++++++++++++++++++
>  1 file changed, 43 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/input/mtk-pmic-keys.txt

Acked-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [PATCH v5 3/6] dt-bindings: input: Add document bindings for mtk-pmic-keys
@ 2017-10-05 22:23     ` Rob Herring
  0 siblings, 0 replies; 39+ messages in thread
From: Rob Herring @ 2017-10-05 22:23 UTC (permalink / raw)
  To: linux-arm-kernel

On Wed, Sep 27, 2017 at 06:44:05PM +0800, Chen Zhong wrote:
> This patch adds the device tree binding documentation for the MediaTek
> pmic keys found on PMIC MT6397/MT6323.
> 
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> ---
>  .../devicetree/bindings/input/mtk-pmic-keys.txt    |   43 ++++++++++++++++++++
>  1 file changed, 43 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/input/mtk-pmic-keys.txt

Acked-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-10-24  1:27     ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-10-24  1:27 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: Rob Herring, Lee Jones, Alexandre Belloni, Mark Rutland,
	Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
	Chanwoo Choi, Jaechul Lee, Andi Shyti, linux-input, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc

Just gentle ping.

On Wed, 2017-09-27 at 18:44 +0800, Chen Zhong wrote:
> This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> interrupts including pwrkey and homekey, also add setting for
> long press key shutdown behavior.
> 
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> ---
>  drivers/input/keyboard/Kconfig         |    9 +
>  drivers/input/keyboard/Makefile        |    1 +
>  drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
>  3 files changed, 351 insertions(+)
>  create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> 
> diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> index 4c4ab1c..bd4e20a 100644
> --- a/drivers/input/keyboard/Kconfig
> +++ b/drivers/input/keyboard/Kconfig
> @@ -756,4 +756,13 @@ config KEYBOARD_BCM
>  	  To compile this driver as a module, choose M here: the
>  	  module will be called bcm-keypad.
>  
> +config KEYBOARD_MTK_PMIC
> +	tristate "MediaTek PMIC keys support"
> +	depends on MFD_MT6397
> +	help
> +	  Say Y here if you want to use the pmic keys (powerkey/homekey).
> +
> +	  To compile this driver as a module, choose M here: the
> +	  module will be called pmic-keys.
> +
>  endif
> diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> index d2338ba..20c0b98 100644
> --- a/drivers/input/keyboard/Makefile
> +++ b/drivers/input/keyboard/Makefile
> @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
>  obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
>  obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
>  obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
> +obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
>  obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
>  obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
>  obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
> diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> new file mode 100644
> index 0000000..529fd95
> --- /dev/null
> +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> @@ -0,0 +1,341 @@
> +/*
> + * Copyright (C) 2017 MediaTek, Inc.
> + *
> + * Author: Chen Zhong <chen.zhong@mediatek.com>
> + *
> + * This software is licensed under the terms of the GNU General Public
> + * License version 2, as published by the Free Software Foundation, and
> + * may be copied, distributed, and modified under those terms.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + */
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/input.h>
> +#include <linux/interrupt.h>
> +#include <linux/platform_device.h>
> +#include <linux/kernel.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/regmap.h>
> +#include <linux/mfd/mt6323/registers.h>
> +#include <linux/mfd/mt6397/registers.h>
> +#include <linux/mfd/mt6397/core.h>
> +
> +#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
> +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
> +#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
> +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
> +#define MTK_PMIC_RST_DU_MASK		0x3
> +#define MTK_PMIC_RST_DU_SHIFT		8
> +
> +#define MTK_PMIC_PWRKEY_RST		\
> +	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> +#define MTK_PMIC_HOMEKEY_RST		\
> +	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> +
> +#define MTK_PMIC_PWRKEY_INDEX	0
> +#define MTK_PMIC_HOMEKEY_INDEX	1
> +#define MTK_PMIC_MAX_KEY_COUNT	2
> +
> +struct mtk_pmic_keys_regs {
> +	u32 deb_reg;
> +	u32 deb_mask;
> +	u32 intsel_reg;
> +	u32 intsel_mask;
> +};
> +
> +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
> +	_intsel_reg, _intsel_mask)			\
> +{							\
> +	.deb_reg		= _deb_reg,		\
> +	.deb_mask		= _deb_mask,		\
> +	.intsel_reg		= _intsel_reg,		\
> +	.intsel_mask		= _intsel_mask,		\
> +}
> +
> +struct mtk_pmic_regs {
> +	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> +	u32 pmic_rst_reg;
> +};
> +
> +static const struct mtk_pmic_regs mt6397_regs = {
> +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> +		0x8, MT6397_INT_RSV, 0x10),
> +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> +		0x10, MT6397_INT_RSV, 0x8),
> +	.pmic_rst_reg = MT6397_TOP_RST_MISC,
> +};
> +
> +static const struct mtk_pmic_regs mt6323_regs = {
> +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> +		0x2, MT6323_INT_MISC_CON, 0x10),
> +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> +		0x4, MT6323_INT_MISC_CON, 0x8),
> +	.pmic_rst_reg = MT6323_TOP_RST_MISC,
> +};
> +
> +struct mtk_pmic_keys_info {
> +	struct mtk_pmic_keys *keys;
> +	const struct mtk_pmic_keys_regs *regs;
> +	unsigned int keycode;
> +	int irq;
> +	bool wakeup:1;
> +};
> +
> +struct mtk_pmic_keys {
> +	struct input_dev *input_dev;
> +	struct device *dev;
> +	struct regmap *regmap;
> +	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> +};
> +
> +enum mtk_pmic_keys_lp_mode {
> +	LP_DISABLE,
> +	LP_ONEKEY,
> +	LP_TWOKEY,
> +};
> +
> +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> +		u32 pmic_rst_reg)
> +{
> +	int ret;
> +	u32 long_press_mode, long_press_debounce;
> +
> +	ret = of_property_read_u32(keys->dev->of_node,
> +		"power-off-time-sec", &long_press_debounce);
> +	if (ret)
> +		long_press_debounce = 0;
> +
> +	regmap_update_bits(keys->regmap, pmic_rst_reg,
> +			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> +			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> +
> +	ret = of_property_read_u32(keys->dev->of_node,
> +		"mediatek,long-press-mode", &long_press_mode);
> +	if (ret)
> +		long_press_mode = LP_DISABLE;
> +
> +	switch (long_press_mode) {
> +	case LP_ONEKEY:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   MTK_PMIC_PWRKEY_RST);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   0);
> +		break;
> +	case LP_TWOKEY:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   MTK_PMIC_PWRKEY_RST);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   MTK_PMIC_HOMEKEY_RST);
> +		break;
> +	case LP_DISABLE:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   0);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   0);
> +		break;
> +	default:
> +		break;
> +	}
> +}
> +
> +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> +{
> +	struct mtk_pmic_keys_info *info = data;
> +	u32 key_deb, pressed;
> +
> +	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> +
> +	key_deb &= info->regs->deb_mask;
> +
> +	pressed = !key_deb;
> +
> +	input_report_key(info->keys->input_dev, info->keycode, pressed);
> +	input_sync(info->keys->input_dev);
> +
> +	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> +		 pressed ? "pressed" : "released", info->keycode);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> +		struct mtk_pmic_keys_info *info)
> +{
> +	int ret;
> +
> +	info->keys = keys;
> +
> +	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> +				 info->regs->intsel_mask,
> +				 info->regs->intsel_mask);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> +					mtk_pmic_keys_irq_handler_thread,
> +					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> +					"mtk-pmic-keys", info);
> +	if (ret) {
> +		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> +			info->irq, ret);
> +		return ret;
> +	}
> +
> +	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> +
> +	return 0;
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int mtk_pmic_keys_suspend(struct device *dev)
> +{
> +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> +	int index;
> +
> +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> +		if (keys->keys[index].wakeup)
> +			enable_irq_wake(keys->keys[index].irq);
> +	}
> +
> +	return 0;
> +}
> +
> +static int mtk_pmic_keys_resume(struct device *dev)
> +{
> +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> +	int index;
> +
> +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> +		if (keys->keys[index].wakeup)
> +			disable_irq_wake(keys->keys[index].irq);
> +	}
> +
> +	return 0;
> +}
> +#endif
> +
> +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> +			mtk_pmic_keys_resume);
> +
> +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> +	{
> +		.compatible = "mediatek,mt6397-keys",
> +		.data = &mt6397_regs,
> +	}, {
> +		.compatible = "mediatek,mt6323-keys",
> +		.data = &mt6323_regs,
> +	}, {
> +		/* sentinel */
> +	}
> +};
> +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> +
> +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> +{
> +	int error, index = 0;
> +	unsigned int keycount;
> +	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> +	struct device_node *node = pdev->dev.of_node, *child;
> +	struct mtk_pmic_keys *keys;
> +	const struct mtk_pmic_regs *mtk_pmic_regs;
> +	struct input_dev *input_dev;
> +	const struct of_device_id *of_id =
> +		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> +
> +	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> +	if (!keys)
> +		return -ENOMEM;
> +
> +	keys->dev = &pdev->dev;
> +	keys->regmap = pmic_chip->regmap;
> +	mtk_pmic_regs = of_id->data;
> +
> +	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> +	if (!input_dev) {
> +		dev_err(keys->dev, "input allocate device fail.\n");
> +		return -ENOMEM;
> +	}
> +
> +	input_dev->name = "mtk-pmic-keys";
> +	input_dev->id.bustype = BUS_HOST;
> +	input_dev->id.vendor = 0x0001;
> +	input_dev->id.product = 0x0001;
> +	input_dev->id.version = 0x0001;
> +
> +	keycount = device_get_child_node_count(keys->dev);
> +	if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
> +		dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
> +		return -EINVAL;
> +	}
> +
> +	for_each_child_of_node(node, child) {
> +		keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
> +
> +		keys->keys[index].irq = platform_get_irq(pdev, index);
> +		if (keys->keys[index].irq < 0)
> +			return keys->keys[index].irq;
> +
> +		error = of_property_read_u32(child,
> +			"linux,keycodes", &keys->keys[index].keycode);
> +		if (error) {
> +			dev_err(keys->dev,
> +				"failed to read key:%d linux,keycode property: %d\n",
> +				index, error);
> +			return error;
> +		}
> +
> +		if (of_property_read_bool(child, "wakeup-source"))
> +			keys->keys[index].wakeup = true;
> +
> +		error = mtk_pmic_key_setup(keys, &keys->keys[index]);
> +		if (error)
> +			return error;
> +
> +		index++;
> +	}
> +
> +	error = input_register_device(input_dev);
> +	if (error) {
> +		dev_err(&pdev->dev,
> +			"register input device failed (%d)\n", error);
> +		return error;
> +	}
> +
> +	mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
> +
> +	platform_set_drvdata(pdev, keys);
> +
> +	return 0;
> +}
> +
> +static struct platform_driver pmic_keys_pdrv = {
> +	.probe = mtk_pmic_keys_probe,
> +	.driver = {
> +		   .name = "mtk-pmic-keys",
> +		   .of_match_table = of_mtk_pmic_keys_match_tbl,
> +		   .pm = &mtk_pmic_keys_pm_ops,
> +	},
> +};
> +
> +module_platform_driver(pmic_keys_pdrv);
> +
> +MODULE_LICENSE("GPL v2");
> +MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
> +MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");

^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-10-24  1:27     ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-10-24  1:27 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: Rob Herring, Lee Jones, Alexandre Belloni, Mark Rutland,
	Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
	Chanwoo Choi, Jaechul Lee, Andi Shyti,
	linux-input-u79uwXL29TY76Z2rM5mHXA,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-rtc-u79uwXL29TY76Z2rM5mHXA

Just gentle ping.

On Wed, 2017-09-27 at 18:44 +0800, Chen Zhong wrote:
> This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> interrupts including pwrkey and homekey, also add setting for
> long press key shutdown behavior.
> 
> Signed-off-by: Chen Zhong <chen.zhong-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> ---
>  drivers/input/keyboard/Kconfig         |    9 +
>  drivers/input/keyboard/Makefile        |    1 +
>  drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
>  3 files changed, 351 insertions(+)
>  create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> 
> diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> index 4c4ab1c..bd4e20a 100644
> --- a/drivers/input/keyboard/Kconfig
> +++ b/drivers/input/keyboard/Kconfig
> @@ -756,4 +756,13 @@ config KEYBOARD_BCM
>  	  To compile this driver as a module, choose M here: the
>  	  module will be called bcm-keypad.
>  
> +config KEYBOARD_MTK_PMIC
> +	tristate "MediaTek PMIC keys support"
> +	depends on MFD_MT6397
> +	help
> +	  Say Y here if you want to use the pmic keys (powerkey/homekey).
> +
> +	  To compile this driver as a module, choose M here: the
> +	  module will be called pmic-keys.
> +
>  endif
> diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> index d2338ba..20c0b98 100644
> --- a/drivers/input/keyboard/Makefile
> +++ b/drivers/input/keyboard/Makefile
> @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
>  obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
>  obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
>  obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
> +obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
>  obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
>  obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
>  obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
> diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> new file mode 100644
> index 0000000..529fd95
> --- /dev/null
> +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> @@ -0,0 +1,341 @@
> +/*
> + * Copyright (C) 2017 MediaTek, Inc.
> + *
> + * Author: Chen Zhong <chen.zhong-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> + *
> + * This software is licensed under the terms of the GNU General Public
> + * License version 2, as published by the Free Software Foundation, and
> + * may be copied, distributed, and modified under those terms.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + */
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/input.h>
> +#include <linux/interrupt.h>
> +#include <linux/platform_device.h>
> +#include <linux/kernel.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/regmap.h>
> +#include <linux/mfd/mt6323/registers.h>
> +#include <linux/mfd/mt6397/registers.h>
> +#include <linux/mfd/mt6397/core.h>
> +
> +#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
> +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
> +#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
> +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
> +#define MTK_PMIC_RST_DU_MASK		0x3
> +#define MTK_PMIC_RST_DU_SHIFT		8
> +
> +#define MTK_PMIC_PWRKEY_RST		\
> +	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> +#define MTK_PMIC_HOMEKEY_RST		\
> +	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> +
> +#define MTK_PMIC_PWRKEY_INDEX	0
> +#define MTK_PMIC_HOMEKEY_INDEX	1
> +#define MTK_PMIC_MAX_KEY_COUNT	2
> +
> +struct mtk_pmic_keys_regs {
> +	u32 deb_reg;
> +	u32 deb_mask;
> +	u32 intsel_reg;
> +	u32 intsel_mask;
> +};
> +
> +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
> +	_intsel_reg, _intsel_mask)			\
> +{							\
> +	.deb_reg		= _deb_reg,		\
> +	.deb_mask		= _deb_mask,		\
> +	.intsel_reg		= _intsel_reg,		\
> +	.intsel_mask		= _intsel_mask,		\
> +}
> +
> +struct mtk_pmic_regs {
> +	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> +	u32 pmic_rst_reg;
> +};
> +
> +static const struct mtk_pmic_regs mt6397_regs = {
> +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> +		0x8, MT6397_INT_RSV, 0x10),
> +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> +		0x10, MT6397_INT_RSV, 0x8),
> +	.pmic_rst_reg = MT6397_TOP_RST_MISC,
> +};
> +
> +static const struct mtk_pmic_regs mt6323_regs = {
> +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> +		0x2, MT6323_INT_MISC_CON, 0x10),
> +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> +		0x4, MT6323_INT_MISC_CON, 0x8),
> +	.pmic_rst_reg = MT6323_TOP_RST_MISC,
> +};
> +
> +struct mtk_pmic_keys_info {
> +	struct mtk_pmic_keys *keys;
> +	const struct mtk_pmic_keys_regs *regs;
> +	unsigned int keycode;
> +	int irq;
> +	bool wakeup:1;
> +};
> +
> +struct mtk_pmic_keys {
> +	struct input_dev *input_dev;
> +	struct device *dev;
> +	struct regmap *regmap;
> +	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> +};
> +
> +enum mtk_pmic_keys_lp_mode {
> +	LP_DISABLE,
> +	LP_ONEKEY,
> +	LP_TWOKEY,
> +};
> +
> +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> +		u32 pmic_rst_reg)
> +{
> +	int ret;
> +	u32 long_press_mode, long_press_debounce;
> +
> +	ret = of_property_read_u32(keys->dev->of_node,
> +		"power-off-time-sec", &long_press_debounce);
> +	if (ret)
> +		long_press_debounce = 0;
> +
> +	regmap_update_bits(keys->regmap, pmic_rst_reg,
> +			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> +			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> +
> +	ret = of_property_read_u32(keys->dev->of_node,
> +		"mediatek,long-press-mode", &long_press_mode);
> +	if (ret)
> +		long_press_mode = LP_DISABLE;
> +
> +	switch (long_press_mode) {
> +	case LP_ONEKEY:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   MTK_PMIC_PWRKEY_RST);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   0);
> +		break;
> +	case LP_TWOKEY:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   MTK_PMIC_PWRKEY_RST);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   MTK_PMIC_HOMEKEY_RST);
> +		break;
> +	case LP_DISABLE:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   0);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   0);
> +		break;
> +	default:
> +		break;
> +	}
> +}
> +
> +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> +{
> +	struct mtk_pmic_keys_info *info = data;
> +	u32 key_deb, pressed;
> +
> +	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> +
> +	key_deb &= info->regs->deb_mask;
> +
> +	pressed = !key_deb;
> +
> +	input_report_key(info->keys->input_dev, info->keycode, pressed);
> +	input_sync(info->keys->input_dev);
> +
> +	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> +		 pressed ? "pressed" : "released", info->keycode);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> +		struct mtk_pmic_keys_info *info)
> +{
> +	int ret;
> +
> +	info->keys = keys;
> +
> +	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> +				 info->regs->intsel_mask,
> +				 info->regs->intsel_mask);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> +					mtk_pmic_keys_irq_handler_thread,
> +					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> +					"mtk-pmic-keys", info);
> +	if (ret) {
> +		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> +			info->irq, ret);
> +		return ret;
> +	}
> +
> +	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> +
> +	return 0;
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int mtk_pmic_keys_suspend(struct device *dev)
> +{
> +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> +	int index;
> +
> +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> +		if (keys->keys[index].wakeup)
> +			enable_irq_wake(keys->keys[index].irq);
> +	}
> +
> +	return 0;
> +}
> +
> +static int mtk_pmic_keys_resume(struct device *dev)
> +{
> +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> +	int index;
> +
> +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> +		if (keys->keys[index].wakeup)
> +			disable_irq_wake(keys->keys[index].irq);
> +	}
> +
> +	return 0;
> +}
> +#endif
> +
> +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> +			mtk_pmic_keys_resume);
> +
> +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> +	{
> +		.compatible = "mediatek,mt6397-keys",
> +		.data = &mt6397_regs,
> +	}, {
> +		.compatible = "mediatek,mt6323-keys",
> +		.data = &mt6323_regs,
> +	}, {
> +		/* sentinel */
> +	}
> +};
> +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> +
> +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> +{
> +	int error, index = 0;
> +	unsigned int keycount;
> +	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> +	struct device_node *node = pdev->dev.of_node, *child;
> +	struct mtk_pmic_keys *keys;
> +	const struct mtk_pmic_regs *mtk_pmic_regs;
> +	struct input_dev *input_dev;
> +	const struct of_device_id *of_id =
> +		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> +
> +	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> +	if (!keys)
> +		return -ENOMEM;
> +
> +	keys->dev = &pdev->dev;
> +	keys->regmap = pmic_chip->regmap;
> +	mtk_pmic_regs = of_id->data;
> +
> +	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> +	if (!input_dev) {
> +		dev_err(keys->dev, "input allocate device fail.\n");
> +		return -ENOMEM;
> +	}
> +
> +	input_dev->name = "mtk-pmic-keys";
> +	input_dev->id.bustype = BUS_HOST;
> +	input_dev->id.vendor = 0x0001;
> +	input_dev->id.product = 0x0001;
> +	input_dev->id.version = 0x0001;
> +
> +	keycount = device_get_child_node_count(keys->dev);
> +	if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
> +		dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
> +		return -EINVAL;
> +	}
> +
> +	for_each_child_of_node(node, child) {
> +		keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
> +
> +		keys->keys[index].irq = platform_get_irq(pdev, index);
> +		if (keys->keys[index].irq < 0)
> +			return keys->keys[index].irq;
> +
> +		error = of_property_read_u32(child,
> +			"linux,keycodes", &keys->keys[index].keycode);
> +		if (error) {
> +			dev_err(keys->dev,
> +				"failed to read key:%d linux,keycode property: %d\n",
> +				index, error);
> +			return error;
> +		}
> +
> +		if (of_property_read_bool(child, "wakeup-source"))
> +			keys->keys[index].wakeup = true;
> +
> +		error = mtk_pmic_key_setup(keys, &keys->keys[index]);
> +		if (error)
> +			return error;
> +
> +		index++;
> +	}
> +
> +	error = input_register_device(input_dev);
> +	if (error) {
> +		dev_err(&pdev->dev,
> +			"register input device failed (%d)\n", error);
> +		return error;
> +	}
> +
> +	mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
> +
> +	platform_set_drvdata(pdev, keys);
> +
> +	return 0;
> +}
> +
> +static struct platform_driver pmic_keys_pdrv = {
> +	.probe = mtk_pmic_keys_probe,
> +	.driver = {
> +		   .name = "mtk-pmic-keys",
> +		   .of_match_table = of_mtk_pmic_keys_match_tbl,
> +		   .pm = &mtk_pmic_keys_pm_ops,
> +	},
> +};
> +
> +module_platform_driver(pmic_keys_pdrv);
> +
> +MODULE_LICENSE("GPL v2");
> +MODULE_AUTHOR("Chen Zhong <chen.zhong-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>");
> +MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");


--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-10-24  1:27     ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-10-24  1:27 UTC (permalink / raw)
  To: linux-arm-kernel

Just gentle ping.

On Wed, 2017-09-27 at 18:44 +0800, Chen Zhong wrote:
> This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> interrupts including pwrkey and homekey, also add setting for
> long press key shutdown behavior.
> 
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> ---
>  drivers/input/keyboard/Kconfig         |    9 +
>  drivers/input/keyboard/Makefile        |    1 +
>  drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
>  3 files changed, 351 insertions(+)
>  create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> 
> diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> index 4c4ab1c..bd4e20a 100644
> --- a/drivers/input/keyboard/Kconfig
> +++ b/drivers/input/keyboard/Kconfig
> @@ -756,4 +756,13 @@ config KEYBOARD_BCM
>  	  To compile this driver as a module, choose M here: the
>  	  module will be called bcm-keypad.
>  
> +config KEYBOARD_MTK_PMIC
> +	tristate "MediaTek PMIC keys support"
> +	depends on MFD_MT6397
> +	help
> +	  Say Y here if you want to use the pmic keys (powerkey/homekey).
> +
> +	  To compile this driver as a module, choose M here: the
> +	  module will be called pmic-keys.
> +
>  endif
> diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> index d2338ba..20c0b98 100644
> --- a/drivers/input/keyboard/Makefile
> +++ b/drivers/input/keyboard/Makefile
> @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
>  obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
>  obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
>  obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
> +obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
>  obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
>  obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
>  obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
> diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> new file mode 100644
> index 0000000..529fd95
> --- /dev/null
> +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> @@ -0,0 +1,341 @@
> +/*
> + * Copyright (C) 2017 MediaTek, Inc.
> + *
> + * Author: Chen Zhong <chen.zhong@mediatek.com>
> + *
> + * This software is licensed under the terms of the GNU General Public
> + * License version 2, as published by the Free Software Foundation, and
> + * may be copied, distributed, and modified under those terms.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + */
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/input.h>
> +#include <linux/interrupt.h>
> +#include <linux/platform_device.h>
> +#include <linux/kernel.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/regmap.h>
> +#include <linux/mfd/mt6323/registers.h>
> +#include <linux/mfd/mt6397/registers.h>
> +#include <linux/mfd/mt6397/core.h>
> +
> +#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
> +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
> +#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
> +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
> +#define MTK_PMIC_RST_DU_MASK		0x3
> +#define MTK_PMIC_RST_DU_SHIFT		8
> +
> +#define MTK_PMIC_PWRKEY_RST		\
> +	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> +#define MTK_PMIC_HOMEKEY_RST		\
> +	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> +
> +#define MTK_PMIC_PWRKEY_INDEX	0
> +#define MTK_PMIC_HOMEKEY_INDEX	1
> +#define MTK_PMIC_MAX_KEY_COUNT	2
> +
> +struct mtk_pmic_keys_regs {
> +	u32 deb_reg;
> +	u32 deb_mask;
> +	u32 intsel_reg;
> +	u32 intsel_mask;
> +};
> +
> +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
> +	_intsel_reg, _intsel_mask)			\
> +{							\
> +	.deb_reg		= _deb_reg,		\
> +	.deb_mask		= _deb_mask,		\
> +	.intsel_reg		= _intsel_reg,		\
> +	.intsel_mask		= _intsel_mask,		\
> +}
> +
> +struct mtk_pmic_regs {
> +	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> +	u32 pmic_rst_reg;
> +};
> +
> +static const struct mtk_pmic_regs mt6397_regs = {
> +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> +		0x8, MT6397_INT_RSV, 0x10),
> +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> +		0x10, MT6397_INT_RSV, 0x8),
> +	.pmic_rst_reg = MT6397_TOP_RST_MISC,
> +};
> +
> +static const struct mtk_pmic_regs mt6323_regs = {
> +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> +		0x2, MT6323_INT_MISC_CON, 0x10),
> +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> +		0x4, MT6323_INT_MISC_CON, 0x8),
> +	.pmic_rst_reg = MT6323_TOP_RST_MISC,
> +};
> +
> +struct mtk_pmic_keys_info {
> +	struct mtk_pmic_keys *keys;
> +	const struct mtk_pmic_keys_regs *regs;
> +	unsigned int keycode;
> +	int irq;
> +	bool wakeup:1;
> +};
> +
> +struct mtk_pmic_keys {
> +	struct input_dev *input_dev;
> +	struct device *dev;
> +	struct regmap *regmap;
> +	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> +};
> +
> +enum mtk_pmic_keys_lp_mode {
> +	LP_DISABLE,
> +	LP_ONEKEY,
> +	LP_TWOKEY,
> +};
> +
> +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> +		u32 pmic_rst_reg)
> +{
> +	int ret;
> +	u32 long_press_mode, long_press_debounce;
> +
> +	ret = of_property_read_u32(keys->dev->of_node,
> +		"power-off-time-sec", &long_press_debounce);
> +	if (ret)
> +		long_press_debounce = 0;
> +
> +	regmap_update_bits(keys->regmap, pmic_rst_reg,
> +			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> +			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> +
> +	ret = of_property_read_u32(keys->dev->of_node,
> +		"mediatek,long-press-mode", &long_press_mode);
> +	if (ret)
> +		long_press_mode = LP_DISABLE;
> +
> +	switch (long_press_mode) {
> +	case LP_ONEKEY:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   MTK_PMIC_PWRKEY_RST);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   0);
> +		break;
> +	case LP_TWOKEY:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   MTK_PMIC_PWRKEY_RST);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   MTK_PMIC_HOMEKEY_RST);
> +		break;
> +	case LP_DISABLE:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   0);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   0);
> +		break;
> +	default:
> +		break;
> +	}
> +}
> +
> +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> +{
> +	struct mtk_pmic_keys_info *info = data;
> +	u32 key_deb, pressed;
> +
> +	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> +
> +	key_deb &= info->regs->deb_mask;
> +
> +	pressed = !key_deb;
> +
> +	input_report_key(info->keys->input_dev, info->keycode, pressed);
> +	input_sync(info->keys->input_dev);
> +
> +	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> +		 pressed ? "pressed" : "released", info->keycode);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> +		struct mtk_pmic_keys_info *info)
> +{
> +	int ret;
> +
> +	info->keys = keys;
> +
> +	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> +				 info->regs->intsel_mask,
> +				 info->regs->intsel_mask);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> +					mtk_pmic_keys_irq_handler_thread,
> +					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> +					"mtk-pmic-keys", info);
> +	if (ret) {
> +		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> +			info->irq, ret);
> +		return ret;
> +	}
> +
> +	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> +
> +	return 0;
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int mtk_pmic_keys_suspend(struct device *dev)
> +{
> +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> +	int index;
> +
> +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> +		if (keys->keys[index].wakeup)
> +			enable_irq_wake(keys->keys[index].irq);
> +	}
> +
> +	return 0;
> +}
> +
> +static int mtk_pmic_keys_resume(struct device *dev)
> +{
> +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> +	int index;
> +
> +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> +		if (keys->keys[index].wakeup)
> +			disable_irq_wake(keys->keys[index].irq);
> +	}
> +
> +	return 0;
> +}
> +#endif
> +
> +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> +			mtk_pmic_keys_resume);
> +
> +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> +	{
> +		.compatible = "mediatek,mt6397-keys",
> +		.data = &mt6397_regs,
> +	}, {
> +		.compatible = "mediatek,mt6323-keys",
> +		.data = &mt6323_regs,
> +	}, {
> +		/* sentinel */
> +	}
> +};
> +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> +
> +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> +{
> +	int error, index = 0;
> +	unsigned int keycount;
> +	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> +	struct device_node *node = pdev->dev.of_node, *child;
> +	struct mtk_pmic_keys *keys;
> +	const struct mtk_pmic_regs *mtk_pmic_regs;
> +	struct input_dev *input_dev;
> +	const struct of_device_id *of_id =
> +		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> +
> +	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> +	if (!keys)
> +		return -ENOMEM;
> +
> +	keys->dev = &pdev->dev;
> +	keys->regmap = pmic_chip->regmap;
> +	mtk_pmic_regs = of_id->data;
> +
> +	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> +	if (!input_dev) {
> +		dev_err(keys->dev, "input allocate device fail.\n");
> +		return -ENOMEM;
> +	}
> +
> +	input_dev->name = "mtk-pmic-keys";
> +	input_dev->id.bustype = BUS_HOST;
> +	input_dev->id.vendor = 0x0001;
> +	input_dev->id.product = 0x0001;
> +	input_dev->id.version = 0x0001;
> +
> +	keycount = device_get_child_node_count(keys->dev);
> +	if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
> +		dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
> +		return -EINVAL;
> +	}
> +
> +	for_each_child_of_node(node, child) {
> +		keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
> +
> +		keys->keys[index].irq = platform_get_irq(pdev, index);
> +		if (keys->keys[index].irq < 0)
> +			return keys->keys[index].irq;
> +
> +		error = of_property_read_u32(child,
> +			"linux,keycodes", &keys->keys[index].keycode);
> +		if (error) {
> +			dev_err(keys->dev,
> +				"failed to read key:%d linux,keycode property: %d\n",
> +				index, error);
> +			return error;
> +		}
> +
> +		if (of_property_read_bool(child, "wakeup-source"))
> +			keys->keys[index].wakeup = true;
> +
> +		error = mtk_pmic_key_setup(keys, &keys->keys[index]);
> +		if (error)
> +			return error;
> +
> +		index++;
> +	}
> +
> +	error = input_register_device(input_dev);
> +	if (error) {
> +		dev_err(&pdev->dev,
> +			"register input device failed (%d)\n", error);
> +		return error;
> +	}
> +
> +	mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
> +
> +	platform_set_drvdata(pdev, keys);
> +
> +	return 0;
> +}
> +
> +static struct platform_driver pmic_keys_pdrv = {
> +	.probe = mtk_pmic_keys_probe,
> +	.driver = {
> +		   .name = "mtk-pmic-keys",
> +		   .of_match_table = of_mtk_pmic_keys_match_tbl,
> +		   .pm = &mtk_pmic_keys_pm_ops,
> +	},
> +};
> +
> +module_platform_driver(pmic_keys_pdrv);
> +
> +MODULE_LICENSE("GPL v2");
> +MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
> +MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");

^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-10-24  5:44     ` Dmitry Torokhov
  0 siblings, 0 replies; 39+ messages in thread
From: Dmitry Torokhov @ 2017-10-24  5:44 UTC (permalink / raw)
  To: Chen Zhong
  Cc: Rob Herring, Lee Jones, Alexandre Belloni, Mark Rutland,
	Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
	Chanwoo Choi, Jaechul Lee, Andi Shyti, linux-input, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc

On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> interrupts including pwrkey and homekey, also add setting for
> long press key shutdown behavior.
> 
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> ---
>  drivers/input/keyboard/Kconfig         |    9 +
>  drivers/input/keyboard/Makefile        |    1 +
>  drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
>  3 files changed, 351 insertions(+)
>  create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> 
> diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> index 4c4ab1c..bd4e20a 100644
> --- a/drivers/input/keyboard/Kconfig
> +++ b/drivers/input/keyboard/Kconfig
> @@ -756,4 +756,13 @@ config KEYBOARD_BCM
>  	  To compile this driver as a module, choose M here: the
>  	  module will be called bcm-keypad.
>  
> +config KEYBOARD_MTK_PMIC
> +	tristate "MediaTek PMIC keys support"
> +	depends on MFD_MT6397
> +	help
> +	  Say Y here if you want to use the pmic keys (powerkey/homekey).
> +
> +	  To compile this driver as a module, choose M here: the
> +	  module will be called pmic-keys.
> +
>  endif
> diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> index d2338ba..20c0b98 100644
> --- a/drivers/input/keyboard/Makefile
> +++ b/drivers/input/keyboard/Makefile
> @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
>  obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
>  obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
>  obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
> +obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
>  obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
>  obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
>  obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
> diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> new file mode 100644
> index 0000000..529fd95
> --- /dev/null
> +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> @@ -0,0 +1,341 @@
> +/*
> + * Copyright (C) 2017 MediaTek, Inc.
> + *
> + * Author: Chen Zhong <chen.zhong@mediatek.com>
> + *
> + * This software is licensed under the terms of the GNU General Public
> + * License version 2, as published by the Free Software Foundation, and
> + * may be copied, distributed, and modified under those terms.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + */
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/input.h>
> +#include <linux/interrupt.h>
> +#include <linux/platform_device.h>
> +#include <linux/kernel.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/regmap.h>
> +#include <linux/mfd/mt6323/registers.h>
> +#include <linux/mfd/mt6397/registers.h>
> +#include <linux/mfd/mt6397/core.h>
> +
> +#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
> +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
> +#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
> +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
> +#define MTK_PMIC_RST_DU_MASK		0x3
> +#define MTK_PMIC_RST_DU_SHIFT		8
> +
> +#define MTK_PMIC_PWRKEY_RST		\
> +	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> +#define MTK_PMIC_HOMEKEY_RST		\
> +	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> +
> +#define MTK_PMIC_PWRKEY_INDEX	0
> +#define MTK_PMIC_HOMEKEY_INDEX	1
> +#define MTK_PMIC_MAX_KEY_COUNT	2
> +
> +struct mtk_pmic_keys_regs {
> +	u32 deb_reg;
> +	u32 deb_mask;
> +	u32 intsel_reg;
> +	u32 intsel_mask;
> +};
> +
> +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
> +	_intsel_reg, _intsel_mask)			\
> +{							\
> +	.deb_reg		= _deb_reg,		\
> +	.deb_mask		= _deb_mask,		\
> +	.intsel_reg		= _intsel_reg,		\
> +	.intsel_mask		= _intsel_mask,		\
> +}
> +
> +struct mtk_pmic_regs {
> +	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> +	u32 pmic_rst_reg;
> +};
> +
> +static const struct mtk_pmic_regs mt6397_regs = {
> +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> +		0x8, MT6397_INT_RSV, 0x10),
> +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> +		0x10, MT6397_INT_RSV, 0x8),
> +	.pmic_rst_reg = MT6397_TOP_RST_MISC,
> +};
> +
> +static const struct mtk_pmic_regs mt6323_regs = {
> +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> +		0x2, MT6323_INT_MISC_CON, 0x10),
> +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> +		0x4, MT6323_INT_MISC_CON, 0x8),
> +	.pmic_rst_reg = MT6323_TOP_RST_MISC,
> +};
> +
> +struct mtk_pmic_keys_info {
> +	struct mtk_pmic_keys *keys;
> +	const struct mtk_pmic_keys_regs *regs;
> +	unsigned int keycode;
> +	int irq;
> +	bool wakeup:1;
> +};
> +
> +struct mtk_pmic_keys {
> +	struct input_dev *input_dev;
> +	struct device *dev;
> +	struct regmap *regmap;
> +	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> +};
> +
> +enum mtk_pmic_keys_lp_mode {
> +	LP_DISABLE,
> +	LP_ONEKEY,
> +	LP_TWOKEY,
> +};
> +
> +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> +		u32 pmic_rst_reg)
> +{
> +	int ret;
> +	u32 long_press_mode, long_press_debounce;
> +
> +	ret = of_property_read_u32(keys->dev->of_node,
> +		"power-off-time-sec", &long_press_debounce);
> +	if (ret)
> +		long_press_debounce = 0;
> +
> +	regmap_update_bits(keys->regmap, pmic_rst_reg,
> +			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> +			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> +
> +	ret = of_property_read_u32(keys->dev->of_node,
> +		"mediatek,long-press-mode", &long_press_mode);
> +	if (ret)
> +		long_press_mode = LP_DISABLE;
> +
> +	switch (long_press_mode) {
> +	case LP_ONEKEY:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   MTK_PMIC_PWRKEY_RST);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   0);
> +		break;
> +	case LP_TWOKEY:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   MTK_PMIC_PWRKEY_RST);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   MTK_PMIC_HOMEKEY_RST);
> +		break;
> +	case LP_DISABLE:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   0);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   0);
> +		break;
> +	default:
> +		break;
> +	}
> +}
> +
> +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> +{
> +	struct mtk_pmic_keys_info *info = data;
> +	u32 key_deb, pressed;
> +
> +	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> +
> +	key_deb &= info->regs->deb_mask;
> +
> +	pressed = !key_deb;
> +
> +	input_report_key(info->keys->input_dev, info->keycode, pressed);
> +	input_sync(info->keys->input_dev);
> +
> +	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> +		 pressed ? "pressed" : "released", info->keycode);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> +		struct mtk_pmic_keys_info *info)
> +{
> +	int ret;
> +
> +	info->keys = keys;
> +
> +	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> +				 info->regs->intsel_mask,
> +				 info->regs->intsel_mask);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> +					mtk_pmic_keys_irq_handler_thread,
> +					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> +					"mtk-pmic-keys", info);
> +	if (ret) {
> +		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> +			info->irq, ret);
> +		return ret;
> +	}
> +
> +	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> +
> +	return 0;
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int mtk_pmic_keys_suspend(struct device *dev)

Please use __maybe_unused annotation instead of #ifdef guard.

> +{
> +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> +	int index;
> +
> +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> +		if (keys->keys[index].wakeup)
> +			enable_irq_wake(keys->keys[index].irq);
> +	}
> +
> +	return 0;
> +}
> +
> +static int mtk_pmic_keys_resume(struct device *dev)

__maybe_unused here as well.

> +{
> +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> +	int index;
> +
> +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> +		if (keys->keys[index].wakeup)
> +			disable_irq_wake(keys->keys[index].irq);
> +	}
> +
> +	return 0;
> +}
> +#endif
> +
> +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> +			mtk_pmic_keys_resume);
> +
> +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> +	{
> +		.compatible = "mediatek,mt6397-keys",
> +		.data = &mt6397_regs,
> +	}, {
> +		.compatible = "mediatek,mt6323-keys",
> +		.data = &mt6323_regs,
> +	}, {
> +		/* sentinel */
> +	}
> +};
> +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> +
> +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> +{
> +	int error, index = 0;
> +	unsigned int keycount;
> +	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> +	struct device_node *node = pdev->dev.of_node, *child;
> +	struct mtk_pmic_keys *keys;
> +	const struct mtk_pmic_regs *mtk_pmic_regs;
> +	struct input_dev *input_dev;
> +	const struct of_device_id *of_id =
> +		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> +
> +	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> +	if (!keys)
> +		return -ENOMEM;
> +
> +	keys->dev = &pdev->dev;
> +	keys->regmap = pmic_chip->regmap;
> +	mtk_pmic_regs = of_id->data;
> +
> +	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> +	if (!input_dev) {
> +		dev_err(keys->dev, "input allocate device fail.\n");
> +		return -ENOMEM;
> +	}
> +
> +	input_dev->name = "mtk-pmic-keys";
> +	input_dev->id.bustype = BUS_HOST;
> +	input_dev->id.vendor = 0x0001;
> +	input_dev->id.product = 0x0001;
> +	input_dev->id.version = 0x0001;
> +
> +	keycount = device_get_child_node_count(keys->dev);

Since you are using of_* API everywhere else I'd rather we used
of_get_available_child_count() here.


Once these 2 issues are fixed please feel free to add 

Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

I assume it will all go though MFD tree, right?

> +	if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
> +		dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
> +		return -EINVAL;
> +	}
> +
> +	for_each_child_of_node(node, child) {
> +		keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
> +
> +		keys->keys[index].irq = platform_get_irq(pdev, index);
> +		if (keys->keys[index].irq < 0)
> +			return keys->keys[index].irq;
> +
> +		error = of_property_read_u32(child,
> +			"linux,keycodes", &keys->keys[index].keycode);
> +		if (error) {
> +			dev_err(keys->dev,
> +				"failed to read key:%d linux,keycode property: %d\n",
> +				index, error);
> +			return error;
> +		}
> +
> +		if (of_property_read_bool(child, "wakeup-source"))
> +			keys->keys[index].wakeup = true;
> +
> +		error = mtk_pmic_key_setup(keys, &keys->keys[index]);
> +		if (error)
> +			return error;
> +
> +		index++;
> +	}
> +
> +	error = input_register_device(input_dev);
> +	if (error) {
> +		dev_err(&pdev->dev,
> +			"register input device failed (%d)\n", error);
> +		return error;
> +	}
> +
> +	mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
> +
> +	platform_set_drvdata(pdev, keys);
> +
> +	return 0;
> +}
> +
> +static struct platform_driver pmic_keys_pdrv = {
> +	.probe = mtk_pmic_keys_probe,
> +	.driver = {
> +		   .name = "mtk-pmic-keys",
> +		   .of_match_table = of_mtk_pmic_keys_match_tbl,
> +		   .pm = &mtk_pmic_keys_pm_ops,
> +	},
> +};
> +
> +module_platform_driver(pmic_keys_pdrv);
> +
> +MODULE_LICENSE("GPL v2");
> +MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
> +MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
> -- 
> 1.7.9.5
> 

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-10-24  5:44     ` Dmitry Torokhov
  0 siblings, 0 replies; 39+ messages in thread
From: Dmitry Torokhov @ 2017-10-24  5:44 UTC (permalink / raw)
  To: Chen Zhong
  Cc: Rob Herring, Lee Jones, Alexandre Belloni, Mark Rutland,
	Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
	Chanwoo Choi, Jaechul Lee, Andi Shyti,
	linux-input-u79uwXL29TY76Z2rM5mHXA,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	linux-rtc-u79uwXL29TY76Z2rM5mHXA

On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> interrupts including pwrkey and homekey, also add setting for
> long press key shutdown behavior.
> 
> Signed-off-by: Chen Zhong <chen.zhong-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> ---
>  drivers/input/keyboard/Kconfig         |    9 +
>  drivers/input/keyboard/Makefile        |    1 +
>  drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
>  3 files changed, 351 insertions(+)
>  create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> 
> diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> index 4c4ab1c..bd4e20a 100644
> --- a/drivers/input/keyboard/Kconfig
> +++ b/drivers/input/keyboard/Kconfig
> @@ -756,4 +756,13 @@ config KEYBOARD_BCM
>  	  To compile this driver as a module, choose M here: the
>  	  module will be called bcm-keypad.
>  
> +config KEYBOARD_MTK_PMIC
> +	tristate "MediaTek PMIC keys support"
> +	depends on MFD_MT6397
> +	help
> +	  Say Y here if you want to use the pmic keys (powerkey/homekey).
> +
> +	  To compile this driver as a module, choose M here: the
> +	  module will be called pmic-keys.
> +
>  endif
> diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> index d2338ba..20c0b98 100644
> --- a/drivers/input/keyboard/Makefile
> +++ b/drivers/input/keyboard/Makefile
> @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
>  obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
>  obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
>  obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
> +obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
>  obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
>  obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
>  obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
> diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> new file mode 100644
> index 0000000..529fd95
> --- /dev/null
> +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> @@ -0,0 +1,341 @@
> +/*
> + * Copyright (C) 2017 MediaTek, Inc.
> + *
> + * Author: Chen Zhong <chen.zhong-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> + *
> + * This software is licensed under the terms of the GNU General Public
> + * License version 2, as published by the Free Software Foundation, and
> + * may be copied, distributed, and modified under those terms.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + */
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/input.h>
> +#include <linux/interrupt.h>
> +#include <linux/platform_device.h>
> +#include <linux/kernel.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/regmap.h>
> +#include <linux/mfd/mt6323/registers.h>
> +#include <linux/mfd/mt6397/registers.h>
> +#include <linux/mfd/mt6397/core.h>
> +
> +#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
> +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
> +#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
> +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
> +#define MTK_PMIC_RST_DU_MASK		0x3
> +#define MTK_PMIC_RST_DU_SHIFT		8
> +
> +#define MTK_PMIC_PWRKEY_RST		\
> +	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> +#define MTK_PMIC_HOMEKEY_RST		\
> +	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> +
> +#define MTK_PMIC_PWRKEY_INDEX	0
> +#define MTK_PMIC_HOMEKEY_INDEX	1
> +#define MTK_PMIC_MAX_KEY_COUNT	2
> +
> +struct mtk_pmic_keys_regs {
> +	u32 deb_reg;
> +	u32 deb_mask;
> +	u32 intsel_reg;
> +	u32 intsel_mask;
> +};
> +
> +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
> +	_intsel_reg, _intsel_mask)			\
> +{							\
> +	.deb_reg		= _deb_reg,		\
> +	.deb_mask		= _deb_mask,		\
> +	.intsel_reg		= _intsel_reg,		\
> +	.intsel_mask		= _intsel_mask,		\
> +}
> +
> +struct mtk_pmic_regs {
> +	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> +	u32 pmic_rst_reg;
> +};
> +
> +static const struct mtk_pmic_regs mt6397_regs = {
> +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> +		0x8, MT6397_INT_RSV, 0x10),
> +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> +		0x10, MT6397_INT_RSV, 0x8),
> +	.pmic_rst_reg = MT6397_TOP_RST_MISC,
> +};
> +
> +static const struct mtk_pmic_regs mt6323_regs = {
> +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> +		0x2, MT6323_INT_MISC_CON, 0x10),
> +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> +		0x4, MT6323_INT_MISC_CON, 0x8),
> +	.pmic_rst_reg = MT6323_TOP_RST_MISC,
> +};
> +
> +struct mtk_pmic_keys_info {
> +	struct mtk_pmic_keys *keys;
> +	const struct mtk_pmic_keys_regs *regs;
> +	unsigned int keycode;
> +	int irq;
> +	bool wakeup:1;
> +};
> +
> +struct mtk_pmic_keys {
> +	struct input_dev *input_dev;
> +	struct device *dev;
> +	struct regmap *regmap;
> +	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> +};
> +
> +enum mtk_pmic_keys_lp_mode {
> +	LP_DISABLE,
> +	LP_ONEKEY,
> +	LP_TWOKEY,
> +};
> +
> +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> +		u32 pmic_rst_reg)
> +{
> +	int ret;
> +	u32 long_press_mode, long_press_debounce;
> +
> +	ret = of_property_read_u32(keys->dev->of_node,
> +		"power-off-time-sec", &long_press_debounce);
> +	if (ret)
> +		long_press_debounce = 0;
> +
> +	regmap_update_bits(keys->regmap, pmic_rst_reg,
> +			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> +			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> +
> +	ret = of_property_read_u32(keys->dev->of_node,
> +		"mediatek,long-press-mode", &long_press_mode);
> +	if (ret)
> +		long_press_mode = LP_DISABLE;
> +
> +	switch (long_press_mode) {
> +	case LP_ONEKEY:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   MTK_PMIC_PWRKEY_RST);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   0);
> +		break;
> +	case LP_TWOKEY:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   MTK_PMIC_PWRKEY_RST);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   MTK_PMIC_HOMEKEY_RST);
> +		break;
> +	case LP_DISABLE:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   0);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   0);
> +		break;
> +	default:
> +		break;
> +	}
> +}
> +
> +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> +{
> +	struct mtk_pmic_keys_info *info = data;
> +	u32 key_deb, pressed;
> +
> +	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> +
> +	key_deb &= info->regs->deb_mask;
> +
> +	pressed = !key_deb;
> +
> +	input_report_key(info->keys->input_dev, info->keycode, pressed);
> +	input_sync(info->keys->input_dev);
> +
> +	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> +		 pressed ? "pressed" : "released", info->keycode);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> +		struct mtk_pmic_keys_info *info)
> +{
> +	int ret;
> +
> +	info->keys = keys;
> +
> +	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> +				 info->regs->intsel_mask,
> +				 info->regs->intsel_mask);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> +					mtk_pmic_keys_irq_handler_thread,
> +					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> +					"mtk-pmic-keys", info);
> +	if (ret) {
> +		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> +			info->irq, ret);
> +		return ret;
> +	}
> +
> +	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> +
> +	return 0;
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int mtk_pmic_keys_suspend(struct device *dev)

Please use __maybe_unused annotation instead of #ifdef guard.

> +{
> +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> +	int index;
> +
> +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> +		if (keys->keys[index].wakeup)
> +			enable_irq_wake(keys->keys[index].irq);
> +	}
> +
> +	return 0;
> +}
> +
> +static int mtk_pmic_keys_resume(struct device *dev)

__maybe_unused here as well.

> +{
> +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> +	int index;
> +
> +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> +		if (keys->keys[index].wakeup)
> +			disable_irq_wake(keys->keys[index].irq);
> +	}
> +
> +	return 0;
> +}
> +#endif
> +
> +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> +			mtk_pmic_keys_resume);
> +
> +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> +	{
> +		.compatible = "mediatek,mt6397-keys",
> +		.data = &mt6397_regs,
> +	}, {
> +		.compatible = "mediatek,mt6323-keys",
> +		.data = &mt6323_regs,
> +	}, {
> +		/* sentinel */
> +	}
> +};
> +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> +
> +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> +{
> +	int error, index = 0;
> +	unsigned int keycount;
> +	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> +	struct device_node *node = pdev->dev.of_node, *child;
> +	struct mtk_pmic_keys *keys;
> +	const struct mtk_pmic_regs *mtk_pmic_regs;
> +	struct input_dev *input_dev;
> +	const struct of_device_id *of_id =
> +		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> +
> +	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> +	if (!keys)
> +		return -ENOMEM;
> +
> +	keys->dev = &pdev->dev;
> +	keys->regmap = pmic_chip->regmap;
> +	mtk_pmic_regs = of_id->data;
> +
> +	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> +	if (!input_dev) {
> +		dev_err(keys->dev, "input allocate device fail.\n");
> +		return -ENOMEM;
> +	}
> +
> +	input_dev->name = "mtk-pmic-keys";
> +	input_dev->id.bustype = BUS_HOST;
> +	input_dev->id.vendor = 0x0001;
> +	input_dev->id.product = 0x0001;
> +	input_dev->id.version = 0x0001;
> +
> +	keycount = device_get_child_node_count(keys->dev);

Since you are using of_* API everywhere else I'd rather we used
of_get_available_child_count() here.


Once these 2 issues are fixed please feel free to add 

Acked-by: Dmitry Torokhov <dmitry.torokhov-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>

I assume it will all go though MFD tree, right?

> +	if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
> +		dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
> +		return -EINVAL;
> +	}
> +
> +	for_each_child_of_node(node, child) {
> +		keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
> +
> +		keys->keys[index].irq = platform_get_irq(pdev, index);
> +		if (keys->keys[index].irq < 0)
> +			return keys->keys[index].irq;
> +
> +		error = of_property_read_u32(child,
> +			"linux,keycodes", &keys->keys[index].keycode);
> +		if (error) {
> +			dev_err(keys->dev,
> +				"failed to read key:%d linux,keycode property: %d\n",
> +				index, error);
> +			return error;
> +		}
> +
> +		if (of_property_read_bool(child, "wakeup-source"))
> +			keys->keys[index].wakeup = true;
> +
> +		error = mtk_pmic_key_setup(keys, &keys->keys[index]);
> +		if (error)
> +			return error;
> +
> +		index++;
> +	}
> +
> +	error = input_register_device(input_dev);
> +	if (error) {
> +		dev_err(&pdev->dev,
> +			"register input device failed (%d)\n", error);
> +		return error;
> +	}
> +
> +	mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
> +
> +	platform_set_drvdata(pdev, keys);
> +
> +	return 0;
> +}
> +
> +static struct platform_driver pmic_keys_pdrv = {
> +	.probe = mtk_pmic_keys_probe,
> +	.driver = {
> +		   .name = "mtk-pmic-keys",
> +		   .of_match_table = of_mtk_pmic_keys_match_tbl,
> +		   .pm = &mtk_pmic_keys_pm_ops,
> +	},
> +};
> +
> +module_platform_driver(pmic_keys_pdrv);
> +
> +MODULE_LICENSE("GPL v2");
> +MODULE_AUTHOR("Chen Zhong <chen.zhong-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>");
> +MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
> -- 
> 1.7.9.5
> 

Thanks.

-- 
Dmitry
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-10-24  5:44     ` Dmitry Torokhov
  0 siblings, 0 replies; 39+ messages in thread
From: Dmitry Torokhov @ 2017-10-24  5:44 UTC (permalink / raw)
  To: linux-arm-kernel

On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> interrupts including pwrkey and homekey, also add setting for
> long press key shutdown behavior.
> 
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> ---
>  drivers/input/keyboard/Kconfig         |    9 +
>  drivers/input/keyboard/Makefile        |    1 +
>  drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
>  3 files changed, 351 insertions(+)
>  create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> 
> diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> index 4c4ab1c..bd4e20a 100644
> --- a/drivers/input/keyboard/Kconfig
> +++ b/drivers/input/keyboard/Kconfig
> @@ -756,4 +756,13 @@ config KEYBOARD_BCM
>  	  To compile this driver as a module, choose M here: the
>  	  module will be called bcm-keypad.
>  
> +config KEYBOARD_MTK_PMIC
> +	tristate "MediaTek PMIC keys support"
> +	depends on MFD_MT6397
> +	help
> +	  Say Y here if you want to use the pmic keys (powerkey/homekey).
> +
> +	  To compile this driver as a module, choose M here: the
> +	  module will be called pmic-keys.
> +
>  endif
> diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> index d2338ba..20c0b98 100644
> --- a/drivers/input/keyboard/Makefile
> +++ b/drivers/input/keyboard/Makefile
> @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
>  obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
>  obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
>  obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
> +obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
>  obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
>  obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
>  obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
> diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> new file mode 100644
> index 0000000..529fd95
> --- /dev/null
> +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> @@ -0,0 +1,341 @@
> +/*
> + * Copyright (C) 2017 MediaTek, Inc.
> + *
> + * Author: Chen Zhong <chen.zhong@mediatek.com>
> + *
> + * This software is licensed under the terms of the GNU General Public
> + * License version 2, as published by the Free Software Foundation, and
> + * may be copied, distributed, and modified under those terms.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + */
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/input.h>
> +#include <linux/interrupt.h>
> +#include <linux/platform_device.h>
> +#include <linux/kernel.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/regmap.h>
> +#include <linux/mfd/mt6323/registers.h>
> +#include <linux/mfd/mt6397/registers.h>
> +#include <linux/mfd/mt6397/core.h>
> +
> +#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
> +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
> +#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
> +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
> +#define MTK_PMIC_RST_DU_MASK		0x3
> +#define MTK_PMIC_RST_DU_SHIFT		8
> +
> +#define MTK_PMIC_PWRKEY_RST		\
> +	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> +#define MTK_PMIC_HOMEKEY_RST		\
> +	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> +
> +#define MTK_PMIC_PWRKEY_INDEX	0
> +#define MTK_PMIC_HOMEKEY_INDEX	1
> +#define MTK_PMIC_MAX_KEY_COUNT	2
> +
> +struct mtk_pmic_keys_regs {
> +	u32 deb_reg;
> +	u32 deb_mask;
> +	u32 intsel_reg;
> +	u32 intsel_mask;
> +};
> +
> +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
> +	_intsel_reg, _intsel_mask)			\
> +{							\
> +	.deb_reg		= _deb_reg,		\
> +	.deb_mask		= _deb_mask,		\
> +	.intsel_reg		= _intsel_reg,		\
> +	.intsel_mask		= _intsel_mask,		\
> +}
> +
> +struct mtk_pmic_regs {
> +	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> +	u32 pmic_rst_reg;
> +};
> +
> +static const struct mtk_pmic_regs mt6397_regs = {
> +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> +		0x8, MT6397_INT_RSV, 0x10),
> +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> +		0x10, MT6397_INT_RSV, 0x8),
> +	.pmic_rst_reg = MT6397_TOP_RST_MISC,
> +};
> +
> +static const struct mtk_pmic_regs mt6323_regs = {
> +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> +		0x2, MT6323_INT_MISC_CON, 0x10),
> +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> +		0x4, MT6323_INT_MISC_CON, 0x8),
> +	.pmic_rst_reg = MT6323_TOP_RST_MISC,
> +};
> +
> +struct mtk_pmic_keys_info {
> +	struct mtk_pmic_keys *keys;
> +	const struct mtk_pmic_keys_regs *regs;
> +	unsigned int keycode;
> +	int irq;
> +	bool wakeup:1;
> +};
> +
> +struct mtk_pmic_keys {
> +	struct input_dev *input_dev;
> +	struct device *dev;
> +	struct regmap *regmap;
> +	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> +};
> +
> +enum mtk_pmic_keys_lp_mode {
> +	LP_DISABLE,
> +	LP_ONEKEY,
> +	LP_TWOKEY,
> +};
> +
> +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> +		u32 pmic_rst_reg)
> +{
> +	int ret;
> +	u32 long_press_mode, long_press_debounce;
> +
> +	ret = of_property_read_u32(keys->dev->of_node,
> +		"power-off-time-sec", &long_press_debounce);
> +	if (ret)
> +		long_press_debounce = 0;
> +
> +	regmap_update_bits(keys->regmap, pmic_rst_reg,
> +			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> +			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> +
> +	ret = of_property_read_u32(keys->dev->of_node,
> +		"mediatek,long-press-mode", &long_press_mode);
> +	if (ret)
> +		long_press_mode = LP_DISABLE;
> +
> +	switch (long_press_mode) {
> +	case LP_ONEKEY:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   MTK_PMIC_PWRKEY_RST);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   0);
> +		break;
> +	case LP_TWOKEY:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   MTK_PMIC_PWRKEY_RST);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   MTK_PMIC_HOMEKEY_RST);
> +		break;
> +	case LP_DISABLE:
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_PWRKEY_RST,
> +				   0);
> +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> +				   MTK_PMIC_HOMEKEY_RST,
> +				   0);
> +		break;
> +	default:
> +		break;
> +	}
> +}
> +
> +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> +{
> +	struct mtk_pmic_keys_info *info = data;
> +	u32 key_deb, pressed;
> +
> +	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> +
> +	key_deb &= info->regs->deb_mask;
> +
> +	pressed = !key_deb;
> +
> +	input_report_key(info->keys->input_dev, info->keycode, pressed);
> +	input_sync(info->keys->input_dev);
> +
> +	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> +		 pressed ? "pressed" : "released", info->keycode);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> +		struct mtk_pmic_keys_info *info)
> +{
> +	int ret;
> +
> +	info->keys = keys;
> +
> +	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> +				 info->regs->intsel_mask,
> +				 info->regs->intsel_mask);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> +					mtk_pmic_keys_irq_handler_thread,
> +					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> +					"mtk-pmic-keys", info);
> +	if (ret) {
> +		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> +			info->irq, ret);
> +		return ret;
> +	}
> +
> +	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> +
> +	return 0;
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int mtk_pmic_keys_suspend(struct device *dev)

Please use __maybe_unused annotation instead of #ifdef guard.

> +{
> +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> +	int index;
> +
> +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> +		if (keys->keys[index].wakeup)
> +			enable_irq_wake(keys->keys[index].irq);
> +	}
> +
> +	return 0;
> +}
> +
> +static int mtk_pmic_keys_resume(struct device *dev)

__maybe_unused here as well.

> +{
> +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> +	int index;
> +
> +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> +		if (keys->keys[index].wakeup)
> +			disable_irq_wake(keys->keys[index].irq);
> +	}
> +
> +	return 0;
> +}
> +#endif
> +
> +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> +			mtk_pmic_keys_resume);
> +
> +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> +	{
> +		.compatible = "mediatek,mt6397-keys",
> +		.data = &mt6397_regs,
> +	}, {
> +		.compatible = "mediatek,mt6323-keys",
> +		.data = &mt6323_regs,
> +	}, {
> +		/* sentinel */
> +	}
> +};
> +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> +
> +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> +{
> +	int error, index = 0;
> +	unsigned int keycount;
> +	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> +	struct device_node *node = pdev->dev.of_node, *child;
> +	struct mtk_pmic_keys *keys;
> +	const struct mtk_pmic_regs *mtk_pmic_regs;
> +	struct input_dev *input_dev;
> +	const struct of_device_id *of_id =
> +		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> +
> +	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> +	if (!keys)
> +		return -ENOMEM;
> +
> +	keys->dev = &pdev->dev;
> +	keys->regmap = pmic_chip->regmap;
> +	mtk_pmic_regs = of_id->data;
> +
> +	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> +	if (!input_dev) {
> +		dev_err(keys->dev, "input allocate device fail.\n");
> +		return -ENOMEM;
> +	}
> +
> +	input_dev->name = "mtk-pmic-keys";
> +	input_dev->id.bustype = BUS_HOST;
> +	input_dev->id.vendor = 0x0001;
> +	input_dev->id.product = 0x0001;
> +	input_dev->id.version = 0x0001;
> +
> +	keycount = device_get_child_node_count(keys->dev);

Since you are using of_* API everywhere else I'd rather we used
of_get_available_child_count() here.


Once these 2 issues are fixed please feel free to add 

Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

I assume it will all go though MFD tree, right?

> +	if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
> +		dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
> +		return -EINVAL;
> +	}
> +
> +	for_each_child_of_node(node, child) {
> +		keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
> +
> +		keys->keys[index].irq = platform_get_irq(pdev, index);
> +		if (keys->keys[index].irq < 0)
> +			return keys->keys[index].irq;
> +
> +		error = of_property_read_u32(child,
> +			"linux,keycodes", &keys->keys[index].keycode);
> +		if (error) {
> +			dev_err(keys->dev,
> +				"failed to read key:%d linux,keycode property: %d\n",
> +				index, error);
> +			return error;
> +		}
> +
> +		if (of_property_read_bool(child, "wakeup-source"))
> +			keys->keys[index].wakeup = true;
> +
> +		error = mtk_pmic_key_setup(keys, &keys->keys[index]);
> +		if (error)
> +			return error;
> +
> +		index++;
> +	}
> +
> +	error = input_register_device(input_dev);
> +	if (error) {
> +		dev_err(&pdev->dev,
> +			"register input device failed (%d)\n", error);
> +		return error;
> +	}
> +
> +	mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
> +
> +	platform_set_drvdata(pdev, keys);
> +
> +	return 0;
> +}
> +
> +static struct platform_driver pmic_keys_pdrv = {
> +	.probe = mtk_pmic_keys_probe,
> +	.driver = {
> +		   .name = "mtk-pmic-keys",
> +		   .of_match_table = of_mtk_pmic_keys_match_tbl,
> +		   .pm = &mtk_pmic_keys_pm_ops,
> +	},
> +};
> +
> +module_platform_driver(pmic_keys_pdrv);
> +
> +MODULE_LICENSE("GPL v2");
> +MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
> +MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
> -- 
> 1.7.9.5
> 

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
  2017-10-24  5:44     ` Dmitry Torokhov
  (?)
@ 2017-10-24  9:37       ` Chen Zhong
  -1 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-10-24  9:37 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: Rob Herring, Lee Jones, Alexandre Belloni, Mark Rutland,
	Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
	Chanwoo Choi, Jaechul Lee, Andi Shyti, linux-input, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc

On Mon, 2017-10-23 at 22:44 -0700, Dmitry Torokhov wrote:
> On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> > This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> > interrupts including pwrkey and homekey, also add setting for
> > long press key shutdown behavior.
> > 
> > Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> > ---
> >  drivers/input/keyboard/Kconfig         |    9 +
> >  drivers/input/keyboard/Makefile        |    1 +
> >  drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
> >  3 files changed, 351 insertions(+)
> >  create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> > 
> > diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> > index 4c4ab1c..bd4e20a 100644
> > --- a/drivers/input/keyboard/Kconfig
> > +++ b/drivers/input/keyboard/Kconfig
> > @@ -756,4 +756,13 @@ config KEYBOARD_BCM
> >  	  To compile this driver as a module, choose M here: the
> >  	  module will be called bcm-keypad.
> >  
> > +config KEYBOARD_MTK_PMIC
> > +	tristate "MediaTek PMIC keys support"
> > +	depends on MFD_MT6397
> > +	help
> > +	  Say Y here if you want to use the pmic keys (powerkey/homekey).
> > +
> > +	  To compile this driver as a module, choose M here: the
> > +	  module will be called pmic-keys.
> > +
> >  endif
> > diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> > index d2338ba..20c0b98 100644
> > --- a/drivers/input/keyboard/Makefile
> > +++ b/drivers/input/keyboard/Makefile
> > @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
> >  obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
> >  obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
> >  obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
> > +obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
> >  obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
> >  obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
> >  obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
> > diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> > new file mode 100644
> > index 0000000..529fd95
> > --- /dev/null
> > +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> > @@ -0,0 +1,341 @@
> > +/*
> > + * Copyright (C) 2017 MediaTek, Inc.
> > + *
> > + * Author: Chen Zhong <chen.zhong@mediatek.com>
> > + *
> > + * This software is licensed under the terms of the GNU General Public
> > + * License version 2, as published by the Free Software Foundation, and
> > + * may be copied, distributed, and modified under those terms.
> > + *
> > + * This program is distributed in the hope that it will be useful,
> > + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > + * GNU General Public License for more details.
> > + *
> > + */
> > +
> > +#include <linux/module.h>
> > +#include <linux/kernel.h>
> > +#include <linux/input.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/kernel.h>
> > +#include <linux/of.h>
> > +#include <linux/of_device.h>
> > +#include <linux/regmap.h>
> > +#include <linux/mfd/mt6323/registers.h>
> > +#include <linux/mfd/mt6397/registers.h>
> > +#include <linux/mfd/mt6397/core.h>
> > +
> > +#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
> > +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
> > +#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
> > +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
> > +#define MTK_PMIC_RST_DU_MASK		0x3
> > +#define MTK_PMIC_RST_DU_SHIFT		8
> > +
> > +#define MTK_PMIC_PWRKEY_RST		\
> > +	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> > +#define MTK_PMIC_HOMEKEY_RST		\
> > +	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> > +
> > +#define MTK_PMIC_PWRKEY_INDEX	0
> > +#define MTK_PMIC_HOMEKEY_INDEX	1
> > +#define MTK_PMIC_MAX_KEY_COUNT	2
> > +
> > +struct mtk_pmic_keys_regs {
> > +	u32 deb_reg;
> > +	u32 deb_mask;
> > +	u32 intsel_reg;
> > +	u32 intsel_mask;
> > +};
> > +
> > +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
> > +	_intsel_reg, _intsel_mask)			\
> > +{							\
> > +	.deb_reg		= _deb_reg,		\
> > +	.deb_mask		= _deb_mask,		\
> > +	.intsel_reg		= _intsel_reg,		\
> > +	.intsel_mask		= _intsel_mask,		\
> > +}
> > +
> > +struct mtk_pmic_regs {
> > +	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> > +	u32 pmic_rst_reg;
> > +};
> > +
> > +static const struct mtk_pmic_regs mt6397_regs = {
> > +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > +		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> > +		0x8, MT6397_INT_RSV, 0x10),
> > +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > +		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> > +		0x10, MT6397_INT_RSV, 0x8),
> > +	.pmic_rst_reg = MT6397_TOP_RST_MISC,
> > +};
> > +
> > +static const struct mtk_pmic_regs mt6323_regs = {
> > +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > +		0x2, MT6323_INT_MISC_CON, 0x10),
> > +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > +		0x4, MT6323_INT_MISC_CON, 0x8),
> > +	.pmic_rst_reg = MT6323_TOP_RST_MISC,
> > +};
> > +
> > +struct mtk_pmic_keys_info {
> > +	struct mtk_pmic_keys *keys;
> > +	const struct mtk_pmic_keys_regs *regs;
> > +	unsigned int keycode;
> > +	int irq;
> > +	bool wakeup:1;
> > +};
> > +
> > +struct mtk_pmic_keys {
> > +	struct input_dev *input_dev;
> > +	struct device *dev;
> > +	struct regmap *regmap;
> > +	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> > +};
> > +
> > +enum mtk_pmic_keys_lp_mode {
> > +	LP_DISABLE,
> > +	LP_ONEKEY,
> > +	LP_TWOKEY,
> > +};
> > +
> > +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> > +		u32 pmic_rst_reg)
> > +{
> > +	int ret;
> > +	u32 long_press_mode, long_press_debounce;
> > +
> > +	ret = of_property_read_u32(keys->dev->of_node,
> > +		"power-off-time-sec", &long_press_debounce);
> > +	if (ret)
> > +		long_press_debounce = 0;
> > +
> > +	regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> > +			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> > +
> > +	ret = of_property_read_u32(keys->dev->of_node,
> > +		"mediatek,long-press-mode", &long_press_mode);
> > +	if (ret)
> > +		long_press_mode = LP_DISABLE;
> > +
> > +	switch (long_press_mode) {
> > +	case LP_ONEKEY:
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_PWRKEY_RST,
> > +				   MTK_PMIC_PWRKEY_RST);
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_HOMEKEY_RST,
> > +				   0);
> > +		break;
> > +	case LP_TWOKEY:
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_PWRKEY_RST,
> > +				   MTK_PMIC_PWRKEY_RST);
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_HOMEKEY_RST,
> > +				   MTK_PMIC_HOMEKEY_RST);
> > +		break;
> > +	case LP_DISABLE:
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_PWRKEY_RST,
> > +				   0);
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_HOMEKEY_RST,
> > +				   0);
> > +		break;
> > +	default:
> > +		break;
> > +	}
> > +}
> > +
> > +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> > +{
> > +	struct mtk_pmic_keys_info *info = data;
> > +	u32 key_deb, pressed;
> > +
> > +	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> > +
> > +	key_deb &= info->regs->deb_mask;
> > +
> > +	pressed = !key_deb;
> > +
> > +	input_report_key(info->keys->input_dev, info->keycode, pressed);
> > +	input_sync(info->keys->input_dev);
> > +
> > +	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> > +		 pressed ? "pressed" : "released", info->keycode);
> > +
> > +	return IRQ_HANDLED;
> > +}
> > +
> > +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> > +		struct mtk_pmic_keys_info *info)
> > +{
> > +	int ret;
> > +
> > +	info->keys = keys;
> > +
> > +	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> > +				 info->regs->intsel_mask,
> > +				 info->regs->intsel_mask);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> > +					mtk_pmic_keys_irq_handler_thread,
> > +					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> > +					"mtk-pmic-keys", info);
> > +	if (ret) {
> > +		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> > +			info->irq, ret);
> > +		return ret;
> > +	}
> > +
> > +	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> > +
> > +	return 0;
> > +}
> > +
> > +#ifdef CONFIG_PM_SLEEP
> > +static int mtk_pmic_keys_suspend(struct device *dev)
> 
> Please use __maybe_unused annotation instead of #ifdef guard.
> 
> > +{
> > +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > +	int index;
> > +
> > +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > +		if (keys->keys[index].wakeup)
> > +			enable_irq_wake(keys->keys[index].irq);
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int mtk_pmic_keys_resume(struct device *dev)
> 
> __maybe_unused here as well.
> 
> > +{
> > +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > +	int index;
> > +
> > +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > +		if (keys->keys[index].wakeup)
> > +			disable_irq_wake(keys->keys[index].irq);
> > +	}
> > +
> > +	return 0;
> > +}
> > +#endif
> > +
> > +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> > +			mtk_pmic_keys_resume);
> > +
> > +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> > +	{
> > +		.compatible = "mediatek,mt6397-keys",
> > +		.data = &mt6397_regs,
> > +	}, {
> > +		.compatible = "mediatek,mt6323-keys",
> > +		.data = &mt6323_regs,
> > +	}, {
> > +		/* sentinel */
> > +	}
> > +};
> > +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> > +
> > +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> > +{
> > +	int error, index = 0;
> > +	unsigned int keycount;
> > +	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> > +	struct device_node *node = pdev->dev.of_node, *child;
> > +	struct mtk_pmic_keys *keys;
> > +	const struct mtk_pmic_regs *mtk_pmic_regs;
> > +	struct input_dev *input_dev;
> > +	const struct of_device_id *of_id =
> > +		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> > +
> > +	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> > +	if (!keys)
> > +		return -ENOMEM;
> > +
> > +	keys->dev = &pdev->dev;
> > +	keys->regmap = pmic_chip->regmap;
> > +	mtk_pmic_regs = of_id->data;
> > +
> > +	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> > +	if (!input_dev) {
> > +		dev_err(keys->dev, "input allocate device fail.\n");
> > +		return -ENOMEM;
> > +	}
> > +
> > +	input_dev->name = "mtk-pmic-keys";
> > +	input_dev->id.bustype = BUS_HOST;
> > +	input_dev->id.vendor = 0x0001;
> > +	input_dev->id.product = 0x0001;
> > +	input_dev->id.version = 0x0001;
> > +
> > +	keycount = device_get_child_node_count(keys->dev);
> 
> Since you are using of_* API everywhere else I'd rather we used
> of_get_available_child_count() here.
> 
> 
> Once these 2 issues are fixed please feel free to add 
> 
> Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> 
> I assume it will all go though MFD tree, right?

I will fix these 2 issues and send the new version.

Since patch 2,3,4,5 of this series are all for input driver, could you
help to merge them with the new version later?

Thank you.

> 
> > +	if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
> > +		dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
> > +		return -EINVAL;
> > +	}
> > +
> > +	for_each_child_of_node(node, child) {
> > +		keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
> > +
> > +		keys->keys[index].irq = platform_get_irq(pdev, index);
> > +		if (keys->keys[index].irq < 0)
> > +			return keys->keys[index].irq;
> > +
> > +		error = of_property_read_u32(child,
> > +			"linux,keycodes", &keys->keys[index].keycode);
> > +		if (error) {
> > +			dev_err(keys->dev,
> > +				"failed to read key:%d linux,keycode property: %d\n",
> > +				index, error);
> > +			return error;
> > +		}
> > +
> > +		if (of_property_read_bool(child, "wakeup-source"))
> > +			keys->keys[index].wakeup = true;
> > +
> > +		error = mtk_pmic_key_setup(keys, &keys->keys[index]);
> > +		if (error)
> > +			return error;
> > +
> > +		index++;
> > +	}
> > +
> > +	error = input_register_device(input_dev);
> > +	if (error) {
> > +		dev_err(&pdev->dev,
> > +			"register input device failed (%d)\n", error);
> > +		return error;
> > +	}
> > +
> > +	mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
> > +
> > +	platform_set_drvdata(pdev, keys);
> > +
> > +	return 0;
> > +}
> > +
> > +static struct platform_driver pmic_keys_pdrv = {
> > +	.probe = mtk_pmic_keys_probe,
> > +	.driver = {
> > +		   .name = "mtk-pmic-keys",
> > +		   .of_match_table = of_mtk_pmic_keys_match_tbl,
> > +		   .pm = &mtk_pmic_keys_pm_ops,
> > +	},
> > +};
> > +
> > +module_platform_driver(pmic_keys_pdrv);
> > +
> > +MODULE_LICENSE("GPL v2");
> > +MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
> > +MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
> > -- 
> > 1.7.9.5
> > 
> 
> Thanks.
> 

^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-10-24  9:37       ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-10-24  9:37 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: Rob Herring, Lee Jones, Alexandre Belloni, Mark Rutland,
	Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
	Chanwoo Choi, Jaechul Lee, Andi Shyti, linux-input, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc

On Mon, 2017-10-23 at 22:44 -0700, Dmitry Torokhov wrote:
> On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> > This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> > interrupts including pwrkey and homekey, also add setting for
> > long press key shutdown behavior.
> > 
> > Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> > ---
> >  drivers/input/keyboard/Kconfig         |    9 +
> >  drivers/input/keyboard/Makefile        |    1 +
> >  drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
> >  3 files changed, 351 insertions(+)
> >  create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> > 
> > diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> > index 4c4ab1c..bd4e20a 100644
> > --- a/drivers/input/keyboard/Kconfig
> > +++ b/drivers/input/keyboard/Kconfig
> > @@ -756,4 +756,13 @@ config KEYBOARD_BCM
> >  	  To compile this driver as a module, choose M here: the
> >  	  module will be called bcm-keypad.
> >  
> > +config KEYBOARD_MTK_PMIC
> > +	tristate "MediaTek PMIC keys support"
> > +	depends on MFD_MT6397
> > +	help
> > +	  Say Y here if you want to use the pmic keys (powerkey/homekey).
> > +
> > +	  To compile this driver as a module, choose M here: the
> > +	  module will be called pmic-keys.
> > +
> >  endif
> > diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> > index d2338ba..20c0b98 100644
> > --- a/drivers/input/keyboard/Makefile
> > +++ b/drivers/input/keyboard/Makefile
> > @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
> >  obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
> >  obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
> >  obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
> > +obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
> >  obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
> >  obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
> >  obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
> > diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> > new file mode 100644
> > index 0000000..529fd95
> > --- /dev/null
> > +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> > @@ -0,0 +1,341 @@
> > +/*
> > + * Copyright (C) 2017 MediaTek, Inc.
> > + *
> > + * Author: Chen Zhong <chen.zhong@mediatek.com>
> > + *
> > + * This software is licensed under the terms of the GNU General Public
> > + * License version 2, as published by the Free Software Foundation, and
> > + * may be copied, distributed, and modified under those terms.
> > + *
> > + * This program is distributed in the hope that it will be useful,
> > + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > + * GNU General Public License for more details.
> > + *
> > + */
> > +
> > +#include <linux/module.h>
> > +#include <linux/kernel.h>
> > +#include <linux/input.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/kernel.h>
> > +#include <linux/of.h>
> > +#include <linux/of_device.h>
> > +#include <linux/regmap.h>
> > +#include <linux/mfd/mt6323/registers.h>
> > +#include <linux/mfd/mt6397/registers.h>
> > +#include <linux/mfd/mt6397/core.h>
> > +
> > +#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
> > +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
> > +#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
> > +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
> > +#define MTK_PMIC_RST_DU_MASK		0x3
> > +#define MTK_PMIC_RST_DU_SHIFT		8
> > +
> > +#define MTK_PMIC_PWRKEY_RST		\
> > +	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> > +#define MTK_PMIC_HOMEKEY_RST		\
> > +	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> > +
> > +#define MTK_PMIC_PWRKEY_INDEX	0
> > +#define MTK_PMIC_HOMEKEY_INDEX	1
> > +#define MTK_PMIC_MAX_KEY_COUNT	2
> > +
> > +struct mtk_pmic_keys_regs {
> > +	u32 deb_reg;
> > +	u32 deb_mask;
> > +	u32 intsel_reg;
> > +	u32 intsel_mask;
> > +};
> > +
> > +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
> > +	_intsel_reg, _intsel_mask)			\
> > +{							\
> > +	.deb_reg		= _deb_reg,		\
> > +	.deb_mask		= _deb_mask,		\
> > +	.intsel_reg		= _intsel_reg,		\
> > +	.intsel_mask		= _intsel_mask,		\
> > +}
> > +
> > +struct mtk_pmic_regs {
> > +	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> > +	u32 pmic_rst_reg;
> > +};
> > +
> > +static const struct mtk_pmic_regs mt6397_regs = {
> > +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > +		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> > +		0x8, MT6397_INT_RSV, 0x10),
> > +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > +		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> > +		0x10, MT6397_INT_RSV, 0x8),
> > +	.pmic_rst_reg = MT6397_TOP_RST_MISC,
> > +};
> > +
> > +static const struct mtk_pmic_regs mt6323_regs = {
> > +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > +		0x2, MT6323_INT_MISC_CON, 0x10),
> > +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > +		0x4, MT6323_INT_MISC_CON, 0x8),
> > +	.pmic_rst_reg = MT6323_TOP_RST_MISC,
> > +};
> > +
> > +struct mtk_pmic_keys_info {
> > +	struct mtk_pmic_keys *keys;
> > +	const struct mtk_pmic_keys_regs *regs;
> > +	unsigned int keycode;
> > +	int irq;
> > +	bool wakeup:1;
> > +};
> > +
> > +struct mtk_pmic_keys {
> > +	struct input_dev *input_dev;
> > +	struct device *dev;
> > +	struct regmap *regmap;
> > +	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> > +};
> > +
> > +enum mtk_pmic_keys_lp_mode {
> > +	LP_DISABLE,
> > +	LP_ONEKEY,
> > +	LP_TWOKEY,
> > +};
> > +
> > +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> > +		u32 pmic_rst_reg)
> > +{
> > +	int ret;
> > +	u32 long_press_mode, long_press_debounce;
> > +
> > +	ret = of_property_read_u32(keys->dev->of_node,
> > +		"power-off-time-sec", &long_press_debounce);
> > +	if (ret)
> > +		long_press_debounce = 0;
> > +
> > +	regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> > +			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> > +
> > +	ret = of_property_read_u32(keys->dev->of_node,
> > +		"mediatek,long-press-mode", &long_press_mode);
> > +	if (ret)
> > +		long_press_mode = LP_DISABLE;
> > +
> > +	switch (long_press_mode) {
> > +	case LP_ONEKEY:
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_PWRKEY_RST,
> > +				   MTK_PMIC_PWRKEY_RST);
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_HOMEKEY_RST,
> > +				   0);
> > +		break;
> > +	case LP_TWOKEY:
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_PWRKEY_RST,
> > +				   MTK_PMIC_PWRKEY_RST);
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_HOMEKEY_RST,
> > +				   MTK_PMIC_HOMEKEY_RST);
> > +		break;
> > +	case LP_DISABLE:
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_PWRKEY_RST,
> > +				   0);
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_HOMEKEY_RST,
> > +				   0);
> > +		break;
> > +	default:
> > +		break;
> > +	}
> > +}
> > +
> > +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> > +{
> > +	struct mtk_pmic_keys_info *info = data;
> > +	u32 key_deb, pressed;
> > +
> > +	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> > +
> > +	key_deb &= info->regs->deb_mask;
> > +
> > +	pressed = !key_deb;
> > +
> > +	input_report_key(info->keys->input_dev, info->keycode, pressed);
> > +	input_sync(info->keys->input_dev);
> > +
> > +	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> > +		 pressed ? "pressed" : "released", info->keycode);
> > +
> > +	return IRQ_HANDLED;
> > +}
> > +
> > +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> > +		struct mtk_pmic_keys_info *info)
> > +{
> > +	int ret;
> > +
> > +	info->keys = keys;
> > +
> > +	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> > +				 info->regs->intsel_mask,
> > +				 info->regs->intsel_mask);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> > +					mtk_pmic_keys_irq_handler_thread,
> > +					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> > +					"mtk-pmic-keys", info);
> > +	if (ret) {
> > +		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> > +			info->irq, ret);
> > +		return ret;
> > +	}
> > +
> > +	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> > +
> > +	return 0;
> > +}
> > +
> > +#ifdef CONFIG_PM_SLEEP
> > +static int mtk_pmic_keys_suspend(struct device *dev)
> 
> Please use __maybe_unused annotation instead of #ifdef guard.
> 
> > +{
> > +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > +	int index;
> > +
> > +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > +		if (keys->keys[index].wakeup)
> > +			enable_irq_wake(keys->keys[index].irq);
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int mtk_pmic_keys_resume(struct device *dev)
> 
> __maybe_unused here as well.
> 
> > +{
> > +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > +	int index;
> > +
> > +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > +		if (keys->keys[index].wakeup)
> > +			disable_irq_wake(keys->keys[index].irq);
> > +	}
> > +
> > +	return 0;
> > +}
> > +#endif
> > +
> > +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> > +			mtk_pmic_keys_resume);
> > +
> > +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> > +	{
> > +		.compatible = "mediatek,mt6397-keys",
> > +		.data = &mt6397_regs,
> > +	}, {
> > +		.compatible = "mediatek,mt6323-keys",
> > +		.data = &mt6323_regs,
> > +	}, {
> > +		/* sentinel */
> > +	}
> > +};
> > +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> > +
> > +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> > +{
> > +	int error, index = 0;
> > +	unsigned int keycount;
> > +	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> > +	struct device_node *node = pdev->dev.of_node, *child;
> > +	struct mtk_pmic_keys *keys;
> > +	const struct mtk_pmic_regs *mtk_pmic_regs;
> > +	struct input_dev *input_dev;
> > +	const struct of_device_id *of_id =
> > +		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> > +
> > +	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> > +	if (!keys)
> > +		return -ENOMEM;
> > +
> > +	keys->dev = &pdev->dev;
> > +	keys->regmap = pmic_chip->regmap;
> > +	mtk_pmic_regs = of_id->data;
> > +
> > +	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> > +	if (!input_dev) {
> > +		dev_err(keys->dev, "input allocate device fail.\n");
> > +		return -ENOMEM;
> > +	}
> > +
> > +	input_dev->name = "mtk-pmic-keys";
> > +	input_dev->id.bustype = BUS_HOST;
> > +	input_dev->id.vendor = 0x0001;
> > +	input_dev->id.product = 0x0001;
> > +	input_dev->id.version = 0x0001;
> > +
> > +	keycount = device_get_child_node_count(keys->dev);
> 
> Since you are using of_* API everywhere else I'd rather we used
> of_get_available_child_count() here.
> 
> 
> Once these 2 issues are fixed please feel free to add 
> 
> Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> 
> I assume it will all go though MFD tree, right?

I will fix these 2 issues and send the new version.

Since patch 2,3,4,5 of this series are all for input driver, could you
help to merge them with the new version later?

Thank you.

> 
> > +	if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
> > +		dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
> > +		return -EINVAL;
> > +	}
> > +
> > +	for_each_child_of_node(node, child) {
> > +		keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
> > +
> > +		keys->keys[index].irq = platform_get_irq(pdev, index);
> > +		if (keys->keys[index].irq < 0)
> > +			return keys->keys[index].irq;
> > +
> > +		error = of_property_read_u32(child,
> > +			"linux,keycodes", &keys->keys[index].keycode);
> > +		if (error) {
> > +			dev_err(keys->dev,
> > +				"failed to read key:%d linux,keycode property: %d\n",
> > +				index, error);
> > +			return error;
> > +		}
> > +
> > +		if (of_property_read_bool(child, "wakeup-source"))
> > +			keys->keys[index].wakeup = true;
> > +
> > +		error = mtk_pmic_key_setup(keys, &keys->keys[index]);
> > +		if (error)
> > +			return error;
> > +
> > +		index++;
> > +	}
> > +
> > +	error = input_register_device(input_dev);
> > +	if (error) {
> > +		dev_err(&pdev->dev,
> > +			"register input device failed (%d)\n", error);
> > +		return error;
> > +	}
> > +
> > +	mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
> > +
> > +	platform_set_drvdata(pdev, keys);
> > +
> > +	return 0;
> > +}
> > +
> > +static struct platform_driver pmic_keys_pdrv = {
> > +	.probe = mtk_pmic_keys_probe,
> > +	.driver = {
> > +		   .name = "mtk-pmic-keys",
> > +		   .of_match_table = of_mtk_pmic_keys_match_tbl,
> > +		   .pm = &mtk_pmic_keys_pm_ops,
> > +	},
> > +};
> > +
> > +module_platform_driver(pmic_keys_pdrv);
> > +
> > +MODULE_LICENSE("GPL v2");
> > +MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
> > +MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
> > -- 
> > 1.7.9.5
> > 
> 
> Thanks.
> 



^ permalink raw reply	[flat|nested] 39+ messages in thread

* [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-10-24  9:37       ` Chen Zhong
  0 siblings, 0 replies; 39+ messages in thread
From: Chen Zhong @ 2017-10-24  9:37 UTC (permalink / raw)
  To: linux-arm-kernel

On Mon, 2017-10-23 at 22:44 -0700, Dmitry Torokhov wrote:
> On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> > This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> > interrupts including pwrkey and homekey, also add setting for
> > long press key shutdown behavior.
> > 
> > Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> > ---
> >  drivers/input/keyboard/Kconfig         |    9 +
> >  drivers/input/keyboard/Makefile        |    1 +
> >  drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
> >  3 files changed, 351 insertions(+)
> >  create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> > 
> > diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> > index 4c4ab1c..bd4e20a 100644
> > --- a/drivers/input/keyboard/Kconfig
> > +++ b/drivers/input/keyboard/Kconfig
> > @@ -756,4 +756,13 @@ config KEYBOARD_BCM
> >  	  To compile this driver as a module, choose M here: the
> >  	  module will be called bcm-keypad.
> >  
> > +config KEYBOARD_MTK_PMIC
> > +	tristate "MediaTek PMIC keys support"
> > +	depends on MFD_MT6397
> > +	help
> > +	  Say Y here if you want to use the pmic keys (powerkey/homekey).
> > +
> > +	  To compile this driver as a module, choose M here: the
> > +	  module will be called pmic-keys.
> > +
> >  endif
> > diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> > index d2338ba..20c0b98 100644
> > --- a/drivers/input/keyboard/Makefile
> > +++ b/drivers/input/keyboard/Makefile
> > @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
> >  obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
> >  obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
> >  obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
> > +obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
> >  obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
> >  obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
> >  obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
> > diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> > new file mode 100644
> > index 0000000..529fd95
> > --- /dev/null
> > +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> > @@ -0,0 +1,341 @@
> > +/*
> > + * Copyright (C) 2017 MediaTek, Inc.
> > + *
> > + * Author: Chen Zhong <chen.zhong@mediatek.com>
> > + *
> > + * This software is licensed under the terms of the GNU General Public
> > + * License version 2, as published by the Free Software Foundation, and
> > + * may be copied, distributed, and modified under those terms.
> > + *
> > + * This program is distributed in the hope that it will be useful,
> > + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > + * GNU General Public License for more details.
> > + *
> > + */
> > +
> > +#include <linux/module.h>
> > +#include <linux/kernel.h>
> > +#include <linux/input.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/kernel.h>
> > +#include <linux/of.h>
> > +#include <linux/of_device.h>
> > +#include <linux/regmap.h>
> > +#include <linux/mfd/mt6323/registers.h>
> > +#include <linux/mfd/mt6397/registers.h>
> > +#include <linux/mfd/mt6397/core.h>
> > +
> > +#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
> > +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
> > +#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
> > +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
> > +#define MTK_PMIC_RST_DU_MASK		0x3
> > +#define MTK_PMIC_RST_DU_SHIFT		8
> > +
> > +#define MTK_PMIC_PWRKEY_RST		\
> > +	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> > +#define MTK_PMIC_HOMEKEY_RST		\
> > +	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> > +
> > +#define MTK_PMIC_PWRKEY_INDEX	0
> > +#define MTK_PMIC_HOMEKEY_INDEX	1
> > +#define MTK_PMIC_MAX_KEY_COUNT	2
> > +
> > +struct mtk_pmic_keys_regs {
> > +	u32 deb_reg;
> > +	u32 deb_mask;
> > +	u32 intsel_reg;
> > +	u32 intsel_mask;
> > +};
> > +
> > +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
> > +	_intsel_reg, _intsel_mask)			\
> > +{							\
> > +	.deb_reg		= _deb_reg,		\
> > +	.deb_mask		= _deb_mask,		\
> > +	.intsel_reg		= _intsel_reg,		\
> > +	.intsel_mask		= _intsel_mask,		\
> > +}
> > +
> > +struct mtk_pmic_regs {
> > +	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> > +	u32 pmic_rst_reg;
> > +};
> > +
> > +static const struct mtk_pmic_regs mt6397_regs = {
> > +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > +		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> > +		0x8, MT6397_INT_RSV, 0x10),
> > +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > +		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> > +		0x10, MT6397_INT_RSV, 0x8),
> > +	.pmic_rst_reg = MT6397_TOP_RST_MISC,
> > +};
> > +
> > +static const struct mtk_pmic_regs mt6323_regs = {
> > +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > +		0x2, MT6323_INT_MISC_CON, 0x10),
> > +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > +		0x4, MT6323_INT_MISC_CON, 0x8),
> > +	.pmic_rst_reg = MT6323_TOP_RST_MISC,
> > +};
> > +
> > +struct mtk_pmic_keys_info {
> > +	struct mtk_pmic_keys *keys;
> > +	const struct mtk_pmic_keys_regs *regs;
> > +	unsigned int keycode;
> > +	int irq;
> > +	bool wakeup:1;
> > +};
> > +
> > +struct mtk_pmic_keys {
> > +	struct input_dev *input_dev;
> > +	struct device *dev;
> > +	struct regmap *regmap;
> > +	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> > +};
> > +
> > +enum mtk_pmic_keys_lp_mode {
> > +	LP_DISABLE,
> > +	LP_ONEKEY,
> > +	LP_TWOKEY,
> > +};
> > +
> > +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> > +		u32 pmic_rst_reg)
> > +{
> > +	int ret;
> > +	u32 long_press_mode, long_press_debounce;
> > +
> > +	ret = of_property_read_u32(keys->dev->of_node,
> > +		"power-off-time-sec", &long_press_debounce);
> > +	if (ret)
> > +		long_press_debounce = 0;
> > +
> > +	regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> > +			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> > +
> > +	ret = of_property_read_u32(keys->dev->of_node,
> > +		"mediatek,long-press-mode", &long_press_mode);
> > +	if (ret)
> > +		long_press_mode = LP_DISABLE;
> > +
> > +	switch (long_press_mode) {
> > +	case LP_ONEKEY:
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_PWRKEY_RST,
> > +				   MTK_PMIC_PWRKEY_RST);
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_HOMEKEY_RST,
> > +				   0);
> > +		break;
> > +	case LP_TWOKEY:
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_PWRKEY_RST,
> > +				   MTK_PMIC_PWRKEY_RST);
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_HOMEKEY_RST,
> > +				   MTK_PMIC_HOMEKEY_RST);
> > +		break;
> > +	case LP_DISABLE:
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_PWRKEY_RST,
> > +				   0);
> > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > +				   MTK_PMIC_HOMEKEY_RST,
> > +				   0);
> > +		break;
> > +	default:
> > +		break;
> > +	}
> > +}
> > +
> > +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> > +{
> > +	struct mtk_pmic_keys_info *info = data;
> > +	u32 key_deb, pressed;
> > +
> > +	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> > +
> > +	key_deb &= info->regs->deb_mask;
> > +
> > +	pressed = !key_deb;
> > +
> > +	input_report_key(info->keys->input_dev, info->keycode, pressed);
> > +	input_sync(info->keys->input_dev);
> > +
> > +	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> > +		 pressed ? "pressed" : "released", info->keycode);
> > +
> > +	return IRQ_HANDLED;
> > +}
> > +
> > +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> > +		struct mtk_pmic_keys_info *info)
> > +{
> > +	int ret;
> > +
> > +	info->keys = keys;
> > +
> > +	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> > +				 info->regs->intsel_mask,
> > +				 info->regs->intsel_mask);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> > +					mtk_pmic_keys_irq_handler_thread,
> > +					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> > +					"mtk-pmic-keys", info);
> > +	if (ret) {
> > +		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> > +			info->irq, ret);
> > +		return ret;
> > +	}
> > +
> > +	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> > +
> > +	return 0;
> > +}
> > +
> > +#ifdef CONFIG_PM_SLEEP
> > +static int mtk_pmic_keys_suspend(struct device *dev)
> 
> Please use __maybe_unused annotation instead of #ifdef guard.
> 
> > +{
> > +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > +	int index;
> > +
> > +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > +		if (keys->keys[index].wakeup)
> > +			enable_irq_wake(keys->keys[index].irq);
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int mtk_pmic_keys_resume(struct device *dev)
> 
> __maybe_unused here as well.
> 
> > +{
> > +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > +	int index;
> > +
> > +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > +		if (keys->keys[index].wakeup)
> > +			disable_irq_wake(keys->keys[index].irq);
> > +	}
> > +
> > +	return 0;
> > +}
> > +#endif
> > +
> > +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> > +			mtk_pmic_keys_resume);
> > +
> > +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> > +	{
> > +		.compatible = "mediatek,mt6397-keys",
> > +		.data = &mt6397_regs,
> > +	}, {
> > +		.compatible = "mediatek,mt6323-keys",
> > +		.data = &mt6323_regs,
> > +	}, {
> > +		/* sentinel */
> > +	}
> > +};
> > +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> > +
> > +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> > +{
> > +	int error, index = 0;
> > +	unsigned int keycount;
> > +	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> > +	struct device_node *node = pdev->dev.of_node, *child;
> > +	struct mtk_pmic_keys *keys;
> > +	const struct mtk_pmic_regs *mtk_pmic_regs;
> > +	struct input_dev *input_dev;
> > +	const struct of_device_id *of_id =
> > +		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> > +
> > +	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> > +	if (!keys)
> > +		return -ENOMEM;
> > +
> > +	keys->dev = &pdev->dev;
> > +	keys->regmap = pmic_chip->regmap;
> > +	mtk_pmic_regs = of_id->data;
> > +
> > +	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> > +	if (!input_dev) {
> > +		dev_err(keys->dev, "input allocate device fail.\n");
> > +		return -ENOMEM;
> > +	}
> > +
> > +	input_dev->name = "mtk-pmic-keys";
> > +	input_dev->id.bustype = BUS_HOST;
> > +	input_dev->id.vendor = 0x0001;
> > +	input_dev->id.product = 0x0001;
> > +	input_dev->id.version = 0x0001;
> > +
> > +	keycount = device_get_child_node_count(keys->dev);
> 
> Since you are using of_* API everywhere else I'd rather we used
> of_get_available_child_count() here.
> 
> 
> Once these 2 issues are fixed please feel free to add 
> 
> Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> 
> I assume it will all go though MFD tree, right?

I will fix these 2 issues and send the new version.

Since patch 2,3,4,5 of this series are all for input driver, could you
help to merge them with the new version later?

Thank you.

> 
> > +	if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
> > +		dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
> > +		return -EINVAL;
> > +	}
> > +
> > +	for_each_child_of_node(node, child) {
> > +		keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
> > +
> > +		keys->keys[index].irq = platform_get_irq(pdev, index);
> > +		if (keys->keys[index].irq < 0)
> > +			return keys->keys[index].irq;
> > +
> > +		error = of_property_read_u32(child,
> > +			"linux,keycodes", &keys->keys[index].keycode);
> > +		if (error) {
> > +			dev_err(keys->dev,
> > +				"failed to read key:%d linux,keycode property: %d\n",
> > +				index, error);
> > +			return error;
> > +		}
> > +
> > +		if (of_property_read_bool(child, "wakeup-source"))
> > +			keys->keys[index].wakeup = true;
> > +
> > +		error = mtk_pmic_key_setup(keys, &keys->keys[index]);
> > +		if (error)
> > +			return error;
> > +
> > +		index++;
> > +	}
> > +
> > +	error = input_register_device(input_dev);
> > +	if (error) {
> > +		dev_err(&pdev->dev,
> > +			"register input device failed (%d)\n", error);
> > +		return error;
> > +	}
> > +
> > +	mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
> > +
> > +	platform_set_drvdata(pdev, keys);
> > +
> > +	return 0;
> > +}
> > +
> > +static struct platform_driver pmic_keys_pdrv = {
> > +	.probe = mtk_pmic_keys_probe,
> > +	.driver = {
> > +		   .name = "mtk-pmic-keys",
> > +		   .of_match_table = of_mtk_pmic_keys_match_tbl,
> > +		   .pm = &mtk_pmic_keys_pm_ops,
> > +	},
> > +};
> > +
> > +module_platform_driver(pmic_keys_pdrv);
> > +
> > +MODULE_LICENSE("GPL v2");
> > +MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
> > +MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
> > -- 
> > 1.7.9.5
> > 
> 
> Thanks.
> 

^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
  2017-10-24  9:37       ` Chen Zhong
@ 2017-10-24 19:16         ` Dmitry Torokhov
  -1 siblings, 0 replies; 39+ messages in thread
From: Dmitry Torokhov @ 2017-10-24 19:16 UTC (permalink / raw)
  To: Chen Zhong
  Cc: Rob Herring, Lee Jones, Alexandre Belloni, Mark Rutland,
	Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
	Chanwoo Choi, Jaechul Lee, Andi Shyti, linux-input, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc

On Tue, Oct 24, 2017 at 05:37:32PM +0800, Chen Zhong wrote:
> On Mon, 2017-10-23 at 22:44 -0700, Dmitry Torokhov wrote:
> > On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> > > This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> > > interrupts including pwrkey and homekey, also add setting for
> > > long press key shutdown behavior.
> > > 
> > > Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> > > ---
> > >  drivers/input/keyboard/Kconfig         |    9 +
> > >  drivers/input/keyboard/Makefile        |    1 +
> > >  drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
> > >  3 files changed, 351 insertions(+)
> > >  create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> > > 
> > > diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> > > index 4c4ab1c..bd4e20a 100644
> > > --- a/drivers/input/keyboard/Kconfig
> > > +++ b/drivers/input/keyboard/Kconfig
> > > @@ -756,4 +756,13 @@ config KEYBOARD_BCM
> > >  	  To compile this driver as a module, choose M here: the
> > >  	  module will be called bcm-keypad.
> > >  
> > > +config KEYBOARD_MTK_PMIC
> > > +	tristate "MediaTek PMIC keys support"
> > > +	depends on MFD_MT6397
> > > +	help
> > > +	  Say Y here if you want to use the pmic keys (powerkey/homekey).
> > > +
> > > +	  To compile this driver as a module, choose M here: the
> > > +	  module will be called pmic-keys.
> > > +
> > >  endif
> > > diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> > > index d2338ba..20c0b98 100644
> > > --- a/drivers/input/keyboard/Makefile
> > > +++ b/drivers/input/keyboard/Makefile
> > > @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
> > >  obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
> > >  obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
> > >  obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
> > > +obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
> > >  obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
> > >  obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
> > >  obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
> > > diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> > > new file mode 100644
> > > index 0000000..529fd95
> > > --- /dev/null
> > > +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> > > @@ -0,0 +1,341 @@
> > > +/*
> > > + * Copyright (C) 2017 MediaTek, Inc.
> > > + *
> > > + * Author: Chen Zhong <chen.zhong@mediatek.com>
> > > + *
> > > + * This software is licensed under the terms of the GNU General Public
> > > + * License version 2, as published by the Free Software Foundation, and
> > > + * may be copied, distributed, and modified under those terms.
> > > + *
> > > + * This program is distributed in the hope that it will be useful,
> > > + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> > > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > > + * GNU General Public License for more details.
> > > + *
> > > + */
> > > +
> > > +#include <linux/module.h>
> > > +#include <linux/kernel.h>
> > > +#include <linux/input.h>
> > > +#include <linux/interrupt.h>
> > > +#include <linux/platform_device.h>
> > > +#include <linux/kernel.h>
> > > +#include <linux/of.h>
> > > +#include <linux/of_device.h>
> > > +#include <linux/regmap.h>
> > > +#include <linux/mfd/mt6323/registers.h>
> > > +#include <linux/mfd/mt6397/registers.h>
> > > +#include <linux/mfd/mt6397/core.h>
> > > +
> > > +#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
> > > +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
> > > +#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
> > > +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
> > > +#define MTK_PMIC_RST_DU_MASK		0x3
> > > +#define MTK_PMIC_RST_DU_SHIFT		8
> > > +
> > > +#define MTK_PMIC_PWRKEY_RST		\
> > > +	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> > > +#define MTK_PMIC_HOMEKEY_RST		\
> > > +	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> > > +
> > > +#define MTK_PMIC_PWRKEY_INDEX	0
> > > +#define MTK_PMIC_HOMEKEY_INDEX	1
> > > +#define MTK_PMIC_MAX_KEY_COUNT	2
> > > +
> > > +struct mtk_pmic_keys_regs {
> > > +	u32 deb_reg;
> > > +	u32 deb_mask;
> > > +	u32 intsel_reg;
> > > +	u32 intsel_mask;
> > > +};
> > > +
> > > +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
> > > +	_intsel_reg, _intsel_mask)			\
> > > +{							\
> > > +	.deb_reg		= _deb_reg,		\
> > > +	.deb_mask		= _deb_mask,		\
> > > +	.intsel_reg		= _intsel_reg,		\
> > > +	.intsel_mask		= _intsel_mask,		\
> > > +}
> > > +
> > > +struct mtk_pmic_regs {
> > > +	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> > > +	u32 pmic_rst_reg;
> > > +};
> > > +
> > > +static const struct mtk_pmic_regs mt6397_regs = {
> > > +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > > +		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> > > +		0x8, MT6397_INT_RSV, 0x10),
> > > +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > > +		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> > > +		0x10, MT6397_INT_RSV, 0x8),
> > > +	.pmic_rst_reg = MT6397_TOP_RST_MISC,
> > > +};
> > > +
> > > +static const struct mtk_pmic_regs mt6323_regs = {
> > > +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > > +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > > +		0x2, MT6323_INT_MISC_CON, 0x10),
> > > +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > > +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > > +		0x4, MT6323_INT_MISC_CON, 0x8),
> > > +	.pmic_rst_reg = MT6323_TOP_RST_MISC,
> > > +};
> > > +
> > > +struct mtk_pmic_keys_info {
> > > +	struct mtk_pmic_keys *keys;
> > > +	const struct mtk_pmic_keys_regs *regs;
> > > +	unsigned int keycode;
> > > +	int irq;
> > > +	bool wakeup:1;
> > > +};
> > > +
> > > +struct mtk_pmic_keys {
> > > +	struct input_dev *input_dev;
> > > +	struct device *dev;
> > > +	struct regmap *regmap;
> > > +	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> > > +};
> > > +
> > > +enum mtk_pmic_keys_lp_mode {
> > > +	LP_DISABLE,
> > > +	LP_ONEKEY,
> > > +	LP_TWOKEY,
> > > +};
> > > +
> > > +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> > > +		u32 pmic_rst_reg)
> > > +{
> > > +	int ret;
> > > +	u32 long_press_mode, long_press_debounce;
> > > +
> > > +	ret = of_property_read_u32(keys->dev->of_node,
> > > +		"power-off-time-sec", &long_press_debounce);
> > > +	if (ret)
> > > +		long_press_debounce = 0;
> > > +
> > > +	regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> > > +			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> > > +
> > > +	ret = of_property_read_u32(keys->dev->of_node,
> > > +		"mediatek,long-press-mode", &long_press_mode);
> > > +	if (ret)
> > > +		long_press_mode = LP_DISABLE;
> > > +
> > > +	switch (long_press_mode) {
> > > +	case LP_ONEKEY:
> > > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +				   MTK_PMIC_PWRKEY_RST,
> > > +				   MTK_PMIC_PWRKEY_RST);
> > > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +				   MTK_PMIC_HOMEKEY_RST,
> > > +				   0);
> > > +		break;
> > > +	case LP_TWOKEY:
> > > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +				   MTK_PMIC_PWRKEY_RST,
> > > +				   MTK_PMIC_PWRKEY_RST);
> > > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +				   MTK_PMIC_HOMEKEY_RST,
> > > +				   MTK_PMIC_HOMEKEY_RST);
> > > +		break;
> > > +	case LP_DISABLE:
> > > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +				   MTK_PMIC_PWRKEY_RST,
> > > +				   0);
> > > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +				   MTK_PMIC_HOMEKEY_RST,
> > > +				   0);
> > > +		break;
> > > +	default:
> > > +		break;
> > > +	}
> > > +}
> > > +
> > > +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> > > +{
> > > +	struct mtk_pmic_keys_info *info = data;
> > > +	u32 key_deb, pressed;
> > > +
> > > +	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> > > +
> > > +	key_deb &= info->regs->deb_mask;
> > > +
> > > +	pressed = !key_deb;
> > > +
> > > +	input_report_key(info->keys->input_dev, info->keycode, pressed);
> > > +	input_sync(info->keys->input_dev);
> > > +
> > > +	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> > > +		 pressed ? "pressed" : "released", info->keycode);
> > > +
> > > +	return IRQ_HANDLED;
> > > +}
> > > +
> > > +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> > > +		struct mtk_pmic_keys_info *info)
> > > +{
> > > +	int ret;
> > > +
> > > +	info->keys = keys;
> > > +
> > > +	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> > > +				 info->regs->intsel_mask,
> > > +				 info->regs->intsel_mask);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> > > +					mtk_pmic_keys_irq_handler_thread,
> > > +					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> > > +					"mtk-pmic-keys", info);
> > > +	if (ret) {
> > > +		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> > > +			info->irq, ret);
> > > +		return ret;
> > > +	}
> > > +
> > > +	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> > > +
> > > +	return 0;
> > > +}
> > > +
> > > +#ifdef CONFIG_PM_SLEEP
> > > +static int mtk_pmic_keys_suspend(struct device *dev)
> > 
> > Please use __maybe_unused annotation instead of #ifdef guard.
> > 
> > > +{
> > > +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > > +	int index;
> > > +
> > > +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > > +		if (keys->keys[index].wakeup)
> > > +			enable_irq_wake(keys->keys[index].irq);
> > > +	}
> > > +
> > > +	return 0;
> > > +}
> > > +
> > > +static int mtk_pmic_keys_resume(struct device *dev)
> > 
> > __maybe_unused here as well.
> > 
> > > +{
> > > +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > > +	int index;
> > > +
> > > +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > > +		if (keys->keys[index].wakeup)
> > > +			disable_irq_wake(keys->keys[index].irq);
> > > +	}
> > > +
> > > +	return 0;
> > > +}
> > > +#endif
> > > +
> > > +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> > > +			mtk_pmic_keys_resume);
> > > +
> > > +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> > > +	{
> > > +		.compatible = "mediatek,mt6397-keys",
> > > +		.data = &mt6397_regs,
> > > +	}, {
> > > +		.compatible = "mediatek,mt6323-keys",
> > > +		.data = &mt6323_regs,
> > > +	}, {
> > > +		/* sentinel */
> > > +	}
> > > +};
> > > +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> > > +
> > > +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> > > +{
> > > +	int error, index = 0;
> > > +	unsigned int keycount;
> > > +	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> > > +	struct device_node *node = pdev->dev.of_node, *child;
> > > +	struct mtk_pmic_keys *keys;
> > > +	const struct mtk_pmic_regs *mtk_pmic_regs;
> > > +	struct input_dev *input_dev;
> > > +	const struct of_device_id *of_id =
> > > +		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> > > +
> > > +	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> > > +	if (!keys)
> > > +		return -ENOMEM;
> > > +
> > > +	keys->dev = &pdev->dev;
> > > +	keys->regmap = pmic_chip->regmap;
> > > +	mtk_pmic_regs = of_id->data;
> > > +
> > > +	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> > > +	if (!input_dev) {
> > > +		dev_err(keys->dev, "input allocate device fail.\n");
> > > +		return -ENOMEM;
> > > +	}
> > > +
> > > +	input_dev->name = "mtk-pmic-keys";
> > > +	input_dev->id.bustype = BUS_HOST;
> > > +	input_dev->id.vendor = 0x0001;
> > > +	input_dev->id.product = 0x0001;
> > > +	input_dev->id.version = 0x0001;
> > > +
> > > +	keycount = device_get_child_node_count(keys->dev);
> > 
> > Since you are using of_* API everywhere else I'd rather we used
> > of_get_available_child_count() here.
> > 
> > 
> > Once these 2 issues are fixed please feel free to add 
> > 
> > Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> > 
> > I assume it will all go though MFD tree, right?
> 
> I will fix these 2 issues and send the new version.
> 
> Since patch 2,3,4,5 of this series are all for input driver, could you
> help to merge them with the new version later?

Even though they applicable to the input component they do touch MFD
domain... Lee, how do you want to proceed here so we do not hold up the
patch series for too long?

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-10-24 19:16         ` Dmitry Torokhov
  0 siblings, 0 replies; 39+ messages in thread
From: Dmitry Torokhov @ 2017-10-24 19:16 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Oct 24, 2017 at 05:37:32PM +0800, Chen Zhong wrote:
> On Mon, 2017-10-23 at 22:44 -0700, Dmitry Torokhov wrote:
> > On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> > > This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> > > interrupts including pwrkey and homekey, also add setting for
> > > long press key shutdown behavior.
> > > 
> > > Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> > > ---
> > >  drivers/input/keyboard/Kconfig         |    9 +
> > >  drivers/input/keyboard/Makefile        |    1 +
> > >  drivers/input/keyboard/mtk-pmic-keys.c |  341 ++++++++++++++++++++++++++++++++
> > >  3 files changed, 351 insertions(+)
> > >  create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> > > 
> > > diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> > > index 4c4ab1c..bd4e20a 100644
> > > --- a/drivers/input/keyboard/Kconfig
> > > +++ b/drivers/input/keyboard/Kconfig
> > > @@ -756,4 +756,13 @@ config KEYBOARD_BCM
> > >  	  To compile this driver as a module, choose M here: the
> > >  	  module will be called bcm-keypad.
> > >  
> > > +config KEYBOARD_MTK_PMIC
> > > +	tristate "MediaTek PMIC keys support"
> > > +	depends on MFD_MT6397
> > > +	help
> > > +	  Say Y here if you want to use the pmic keys (powerkey/homekey).
> > > +
> > > +	  To compile this driver as a module, choose M here: the
> > > +	  module will be called pmic-keys.
> > > +
> > >  endif
> > > diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> > > index d2338ba..20c0b98 100644
> > > --- a/drivers/input/keyboard/Makefile
> > > +++ b/drivers/input/keyboard/Makefile
> > > @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
> > >  obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
> > >  obj-$(CONFIG_KEYBOARD_MCS)		+= mcs_touchkey.o
> > >  obj-$(CONFIG_KEYBOARD_MPR121)		+= mpr121_touchkey.o
> > > +obj-$(CONFIG_KEYBOARD_MTK_PMIC) 	+= mtk-pmic-keys.o
> > >  obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
> > >  obj-$(CONFIG_KEYBOARD_NOMADIK)		+= nomadik-ske-keypad.o
> > >  obj-$(CONFIG_KEYBOARD_NSPIRE)		+= nspire-keypad.o
> > > diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> > > new file mode 100644
> > > index 0000000..529fd95
> > > --- /dev/null
> > > +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> > > @@ -0,0 +1,341 @@
> > > +/*
> > > + * Copyright (C) 2017 MediaTek, Inc.
> > > + *
> > > + * Author: Chen Zhong <chen.zhong@mediatek.com>
> > > + *
> > > + * This software is licensed under the terms of the GNU General Public
> > > + * License version 2, as published by the Free Software Foundation, and
> > > + * may be copied, distributed, and modified under those terms.
> > > + *
> > > + * This program is distributed in the hope that it will be useful,
> > > + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> > > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > > + * GNU General Public License for more details.
> > > + *
> > > + */
> > > +
> > > +#include <linux/module.h>
> > > +#include <linux/kernel.h>
> > > +#include <linux/input.h>
> > > +#include <linux/interrupt.h>
> > > +#include <linux/platform_device.h>
> > > +#include <linux/kernel.h>
> > > +#include <linux/of.h>
> > > +#include <linux/of_device.h>
> > > +#include <linux/regmap.h>
> > > +#include <linux/mfd/mt6323/registers.h>
> > > +#include <linux/mfd/mt6397/registers.h>
> > > +#include <linux/mfd/mt6397/core.h>
> > > +
> > > +#define MTK_PMIC_PWRKEY_RST_EN_MASK	0x1
> > > +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT	6
> > > +#define MTK_PMIC_HOMEKEY_RST_EN_MASK	0x1
> > > +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT	5
> > > +#define MTK_PMIC_RST_DU_MASK		0x3
> > > +#define MTK_PMIC_RST_DU_SHIFT		8
> > > +
> > > +#define MTK_PMIC_PWRKEY_RST		\
> > > +	(MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> > > +#define MTK_PMIC_HOMEKEY_RST		\
> > > +	(MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> > > +
> > > +#define MTK_PMIC_PWRKEY_INDEX	0
> > > +#define MTK_PMIC_HOMEKEY_INDEX	1
> > > +#define MTK_PMIC_MAX_KEY_COUNT	2
> > > +
> > > +struct mtk_pmic_keys_regs {
> > > +	u32 deb_reg;
> > > +	u32 deb_mask;
> > > +	u32 intsel_reg;
> > > +	u32 intsel_mask;
> > > +};
> > > +
> > > +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask,		\
> > > +	_intsel_reg, _intsel_mask)			\
> > > +{							\
> > > +	.deb_reg		= _deb_reg,		\
> > > +	.deb_mask		= _deb_mask,		\
> > > +	.intsel_reg		= _intsel_reg,		\
> > > +	.intsel_mask		= _intsel_mask,		\
> > > +}
> > > +
> > > +struct mtk_pmic_regs {
> > > +	const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> > > +	u32 pmic_rst_reg;
> > > +};
> > > +
> > > +static const struct mtk_pmic_regs mt6397_regs = {
> > > +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > > +		MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> > > +		0x8, MT6397_INT_RSV, 0x10),
> > > +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > > +		MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> > > +		0x10, MT6397_INT_RSV, 0x8),
> > > +	.pmic_rst_reg = MT6397_TOP_RST_MISC,
> > > +};
> > > +
> > > +static const struct mtk_pmic_regs mt6323_regs = {
> > > +	.keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > > +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > > +		0x2, MT6323_INT_MISC_CON, 0x10),
> > > +	.keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > > +		MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > > +		0x4, MT6323_INT_MISC_CON, 0x8),
> > > +	.pmic_rst_reg = MT6323_TOP_RST_MISC,
> > > +};
> > > +
> > > +struct mtk_pmic_keys_info {
> > > +	struct mtk_pmic_keys *keys;
> > > +	const struct mtk_pmic_keys_regs *regs;
> > > +	unsigned int keycode;
> > > +	int irq;
> > > +	bool wakeup:1;
> > > +};
> > > +
> > > +struct mtk_pmic_keys {
> > > +	struct input_dev *input_dev;
> > > +	struct device *dev;
> > > +	struct regmap *regmap;
> > > +	struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> > > +};
> > > +
> > > +enum mtk_pmic_keys_lp_mode {
> > > +	LP_DISABLE,
> > > +	LP_ONEKEY,
> > > +	LP_TWOKEY,
> > > +};
> > > +
> > > +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> > > +		u32 pmic_rst_reg)
> > > +{
> > > +	int ret;
> > > +	u32 long_press_mode, long_press_debounce;
> > > +
> > > +	ret = of_property_read_u32(keys->dev->of_node,
> > > +		"power-off-time-sec", &long_press_debounce);
> > > +	if (ret)
> > > +		long_press_debounce = 0;
> > > +
> > > +	regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +			   MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> > > +			   long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> > > +
> > > +	ret = of_property_read_u32(keys->dev->of_node,
> > > +		"mediatek,long-press-mode", &long_press_mode);
> > > +	if (ret)
> > > +		long_press_mode = LP_DISABLE;
> > > +
> > > +	switch (long_press_mode) {
> > > +	case LP_ONEKEY:
> > > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +				   MTK_PMIC_PWRKEY_RST,
> > > +				   MTK_PMIC_PWRKEY_RST);
> > > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +				   MTK_PMIC_HOMEKEY_RST,
> > > +				   0);
> > > +		break;
> > > +	case LP_TWOKEY:
> > > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +				   MTK_PMIC_PWRKEY_RST,
> > > +				   MTK_PMIC_PWRKEY_RST);
> > > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +				   MTK_PMIC_HOMEKEY_RST,
> > > +				   MTK_PMIC_HOMEKEY_RST);
> > > +		break;
> > > +	case LP_DISABLE:
> > > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +				   MTK_PMIC_PWRKEY_RST,
> > > +				   0);
> > > +		regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > +				   MTK_PMIC_HOMEKEY_RST,
> > > +				   0);
> > > +		break;
> > > +	default:
> > > +		break;
> > > +	}
> > > +}
> > > +
> > > +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> > > +{
> > > +	struct mtk_pmic_keys_info *info = data;
> > > +	u32 key_deb, pressed;
> > > +
> > > +	regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> > > +
> > > +	key_deb &= info->regs->deb_mask;
> > > +
> > > +	pressed = !key_deb;
> > > +
> > > +	input_report_key(info->keys->input_dev, info->keycode, pressed);
> > > +	input_sync(info->keys->input_dev);
> > > +
> > > +	dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> > > +		 pressed ? "pressed" : "released", info->keycode);
> > > +
> > > +	return IRQ_HANDLED;
> > > +}
> > > +
> > > +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> > > +		struct mtk_pmic_keys_info *info)
> > > +{
> > > +	int ret;
> > > +
> > > +	info->keys = keys;
> > > +
> > > +	ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> > > +				 info->regs->intsel_mask,
> > > +				 info->regs->intsel_mask);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> > > +					mtk_pmic_keys_irq_handler_thread,
> > > +					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> > > +					"mtk-pmic-keys", info);
> > > +	if (ret) {
> > > +		dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> > > +			info->irq, ret);
> > > +		return ret;
> > > +	}
> > > +
> > > +	input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> > > +
> > > +	return 0;
> > > +}
> > > +
> > > +#ifdef CONFIG_PM_SLEEP
> > > +static int mtk_pmic_keys_suspend(struct device *dev)
> > 
> > Please use __maybe_unused annotation instead of #ifdef guard.
> > 
> > > +{
> > > +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > > +	int index;
> > > +
> > > +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > > +		if (keys->keys[index].wakeup)
> > > +			enable_irq_wake(keys->keys[index].irq);
> > > +	}
> > > +
> > > +	return 0;
> > > +}
> > > +
> > > +static int mtk_pmic_keys_resume(struct device *dev)
> > 
> > __maybe_unused here as well.
> > 
> > > +{
> > > +	struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > > +	int index;
> > > +
> > > +	for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > > +		if (keys->keys[index].wakeup)
> > > +			disable_irq_wake(keys->keys[index].irq);
> > > +	}
> > > +
> > > +	return 0;
> > > +}
> > > +#endif
> > > +
> > > +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> > > +			mtk_pmic_keys_resume);
> > > +
> > > +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> > > +	{
> > > +		.compatible = "mediatek,mt6397-keys",
> > > +		.data = &mt6397_regs,
> > > +	}, {
> > > +		.compatible = "mediatek,mt6323-keys",
> > > +		.data = &mt6323_regs,
> > > +	}, {
> > > +		/* sentinel */
> > > +	}
> > > +};
> > > +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> > > +
> > > +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> > > +{
> > > +	int error, index = 0;
> > > +	unsigned int keycount;
> > > +	struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> > > +	struct device_node *node = pdev->dev.of_node, *child;
> > > +	struct mtk_pmic_keys *keys;
> > > +	const struct mtk_pmic_regs *mtk_pmic_regs;
> > > +	struct input_dev *input_dev;
> > > +	const struct of_device_id *of_id =
> > > +		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> > > +
> > > +	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> > > +	if (!keys)
> > > +		return -ENOMEM;
> > > +
> > > +	keys->dev = &pdev->dev;
> > > +	keys->regmap = pmic_chip->regmap;
> > > +	mtk_pmic_regs = of_id->data;
> > > +
> > > +	keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> > > +	if (!input_dev) {
> > > +		dev_err(keys->dev, "input allocate device fail.\n");
> > > +		return -ENOMEM;
> > > +	}
> > > +
> > > +	input_dev->name = "mtk-pmic-keys";
> > > +	input_dev->id.bustype = BUS_HOST;
> > > +	input_dev->id.vendor = 0x0001;
> > > +	input_dev->id.product = 0x0001;
> > > +	input_dev->id.version = 0x0001;
> > > +
> > > +	keycount = device_get_child_node_count(keys->dev);
> > 
> > Since you are using of_* API everywhere else I'd rather we used
> > of_get_available_child_count() here.
> > 
> > 
> > Once these 2 issues are fixed please feel free to add 
> > 
> > Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> > 
> > I assume it will all go though MFD tree, right?
> 
> I will fix these 2 issues and send the new version.
> 
> Since patch 2,3,4,5 of this series are all for input driver, could you
> help to merge them with the new version later?

Even though they applicable to the input component they do touch MFD
domain... Lee, how do you want to proceed here so we do not hold up the
patch series for too long?

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
  2017-09-27 10:44   ` Chen Zhong
  (?)
@ 2017-10-26  8:27     ` Andi Shyti
  -1 siblings, 0 replies; 39+ messages in thread
From: Andi Shyti @ 2017-10-26  8:27 UTC (permalink / raw)
  To: Chen Zhong
  Cc: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni,
	Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
	Linus Walleij, Chanwoo Choi, Jaechul Lee, linux-input,
	devicetree, linux-kernel, linux-arm-kernel, linux-mediatek,
	linux-rtc

Hi,

On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> interrupts including pwrkey and homekey, also add setting for
> long press key shutdown behavior.
> 
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>

if you want you can add:

Reviewed-by: Andi Shyti <andi.shyti@samsung.com>

Andi

^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-10-26  8:27     ` Andi Shyti
  0 siblings, 0 replies; 39+ messages in thread
From: Andi Shyti @ 2017-10-26  8:27 UTC (permalink / raw)
  To: Chen Zhong
  Cc: Mark Rutland, Alessandro Zummo, Jaechul Lee, devicetree,
	Linus Walleij, Dmitry Torokhov, linux-kernel, Chanwoo Choi,
	Rob Herring, Alexandre Belloni, linux-input, Matthias Brugger,
	linux-mediatek, Eddie Huang, Lee Jones, linux-arm-kernel,
	linux-rtc

Hi,

On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> interrupts including pwrkey and homekey, also add setting for
> long press key shutdown behavior.
> 
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>

if you want you can add:

Reviewed-by: Andi Shyti <andi.shyti@samsung.com>

Andi

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [PATCH v5 5/6] input: Add MediaTek PMIC keys support
@ 2017-10-26  8:27     ` Andi Shyti
  0 siblings, 0 replies; 39+ messages in thread
From: Andi Shyti @ 2017-10-26  8:27 UTC (permalink / raw)
  To: linux-arm-kernel

Hi,

On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> interrupts including pwrkey and homekey, also add setting for
> long press key shutdown behavior.
> 
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>

if you want you can add:

Reviewed-by: Andi Shyti <andi.shyti@samsung.com>

Andi

^ permalink raw reply	[flat|nested] 39+ messages in thread

end of thread, other threads:[~2017-10-26  8:30 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-27 10:44 [PATCH v5 0/6] Add MediaTek PMIC keys support Chen Zhong
2017-09-27 10:44 ` Chen Zhong
2017-09-27 10:44 ` Chen Zhong
2017-09-27 10:44 ` [PATCH v5 1/6] mfd: mt6397: create irq mappings in mfd core driver Chen Zhong
2017-09-27 10:44   ` Chen Zhong
2017-09-27 10:44   ` Chen Zhong
2017-09-27 10:44 ` [PATCH v5 2/6] dt-bindings: input: Add common keyboard document bindings Chen Zhong
2017-09-27 10:44   ` Chen Zhong
2017-09-27 10:44   ` Chen Zhong
2017-10-05 22:22   ` Rob Herring
2017-10-05 22:22     ` Rob Herring
2017-09-27 10:44 ` [PATCH v5 3/6] dt-bindings: input: Add document bindings for mtk-pmic-keys Chen Zhong
2017-09-27 10:44   ` Chen Zhong
2017-09-27 10:44   ` Chen Zhong
2017-10-05 22:23   ` Rob Herring
2017-10-05 22:23     ` Rob Herring
2017-09-27 10:44 ` [PATCH v5 4/6] dt-bindings: mfd: Add bindings for the keys as subnode of PMIC Chen Zhong
2017-09-27 10:44   ` Chen Zhong
2017-09-27 10:44   ` Chen Zhong
2017-09-27 10:44 ` [PATCH v5 5/6] input: Add MediaTek PMIC keys support Chen Zhong
2017-09-27 10:44   ` Chen Zhong
2017-09-27 10:44   ` Chen Zhong
2017-10-24  1:27   ` Chen Zhong
2017-10-24  1:27     ` Chen Zhong
2017-10-24  1:27     ` Chen Zhong
2017-10-24  5:44   ` Dmitry Torokhov
2017-10-24  5:44     ` Dmitry Torokhov
2017-10-24  5:44     ` Dmitry Torokhov
2017-10-24  9:37     ` Chen Zhong
2017-10-24  9:37       ` Chen Zhong
2017-10-24  9:37       ` Chen Zhong
2017-10-24 19:16       ` Dmitry Torokhov
2017-10-24 19:16         ` Dmitry Torokhov
2017-10-26  8:27   ` Andi Shyti
2017-10-26  8:27     ` Andi Shyti
2017-10-26  8:27     ` Andi Shyti
2017-09-27 10:44 ` [PATCH v5 6/6] mfd: mt6397: Add PMIC keys support to MT6397 driver Chen Zhong
2017-09-27 10:44   ` Chen Zhong
2017-09-27 10:44   ` Chen Zhong

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.