All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/3]  Add TDM audio on StarFive JH7110
@ 2023-05-06  9:01 ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-06  9:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, Walker Chen
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

This patchset adds TDM audio driver for the StarFive JH7110 SoC. The
first patch adds device tree binding for TDM module. The second patch
adds tdm driver support for JH7110 SoC. The last patch adds device node
of tdm and sound card to JH7110 dts.

The series has been tested on the VisionFive 2 board by plugging an
audio expansion board. 

For more information of audio expansion board, you can take a look
at the following webpage:
https://wiki.seeedstudio.com/ReSpeaker_2_Mics_Pi_HAT/

Changes since v2:
- Use dt-overlay to describe sound card because need to plug the audio
  expansion board into the VisionFive2 board.
- Modified the coding style for driver.
- Moved assignment of stop_dma_first to startup function of dai_driver.
- Dropped some useless macro definition.
- Use loops to get/enable/disable clocks. 

Changes since v1:
- Rebased on Linux 6.3-rc4.
- Added the dts file dedicated to describe audio card.
- Added the item for JH7110 audio board to the dt-binding of StarFive
  SoC-based boards.

---
v2: https://lore.kernel.org/all/20230420024118.22677-1-walker.chen@starfivetech.com/
v1: https://lore.kernel.org/all/20230329153320.31390-1-walker.chen@starfivetech.com/

Walker Chen (3):
  dt-bindings: sound: Add TDM for StarFive JH7110
  ASoC: starfive: Add JH7110 TDM driver
  riscv: dts: starfive: add tdm node and sound card

 .../bindings/sound/starfive,jh7110-tdm.yaml   |  98 +++
 MAINTAINERS                                   |   6 +
 arch/riscv/boot/dts/starfive/Makefile         |   3 +
 .../jh7110-starfive-visionfive-2-wm8960.dtso  |  70 +++
 .../jh7110-starfive-visionfive-2.dtsi         |  40 ++
 arch/riscv/boot/dts/starfive/jh7110.dtsi      |  21 +
 sound/soc/Kconfig                             |   1 +
 sound/soc/Makefile                            |   1 +
 sound/soc/starfive/Kconfig                    |  15 +
 sound/soc/starfive/Makefile                   |   2 +
 sound/soc/starfive/jh7110_tdm.c               | 573 ++++++++++++++++++
 sound/soc/starfive/jh7110_tdm.h               | 147 +++++
 12 files changed, 977 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
 create mode 100644 arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
 create mode 100644 sound/soc/starfive/Kconfig
 create mode 100644 sound/soc/starfive/Makefile
 create mode 100644 sound/soc/starfive/jh7110_tdm.c
 create mode 100644 sound/soc/starfive/jh7110_tdm.h


base-commit: 197b6b60ae7bc51dd0814953c562833143b292aa
prerequisite-patch-id: 30bec4dba6f250a6edd0c2cbab2ce09442e50e8a
prerequisite-patch-id: bb939c0c7c26b08addfccd890f9d3974b6eaec53
prerequisite-patch-id: 8a6f135bcabdad4a4bfb21f0c6a0ffd2bb57efe7
prerequisite-patch-id: c2366f993a9d85e28c06d8d09f064dd5e8b29a61
prerequisite-patch-id: 50d53a21f91f4087fc80b6f1f72864adfb0002b9
prerequisite-patch-id: 0df3703af91c30f1ca2c47f5609012f2d7200028
prerequisite-patch-id: 89f049f951e5acf75aab92541992f816fd0acc0d
prerequisite-patch-id: 551fae54377090044c3612fca9740a9b359abdd2
prerequisite-patch-id: c7fdf904f398d478f0ed6d57eb878982bc73329d
prerequisite-patch-id: 1b2d0982b18da060c82134f05bf3ce16425bac8d
prerequisite-patch-id: 090ba4b78d47bc19204916e76fdbc70021785388
prerequisite-patch-id: a5d9e0f7d4f8163f566678894cf693015119f2d9
prerequisite-patch-id: 4637a8fa2334a45fa6b64351f4e9e28d3e2d60d3
prerequisite-patch-id: 32647ec60a3b614e1c59ec8e54cb511ae832c22f
prerequisite-patch-id: aa06658ecf89c92d0dfdd6a4ba6d9e6e67532971
prerequisite-patch-id: 1387a7e87b446329dfc21f3e575ceae7ebcf954c
prerequisite-patch-id: 258ea5f9b8bf41b6981345dcc81795f25865d38f
prerequisite-patch-id: 8b6f2c9660c0ac0ee4e73e4c21aca8e6b75e81b9
prerequisite-patch-id: dbb0c0151b8bdf093e6ce79fd2fe3f60791a6e0b
prerequisite-patch-id: 9007c8610fdcd387592475949864edde874c20a2
prerequisite-patch-id: d57e95d31686772abc4c4d5aa1cadc344dc293cd
prerequisite-patch-id: 9f911969d0a550648493952c99096d26e05d4d83
prerequisite-patch-id: 2ddada18ab6ea5cd1da14212aaf59632f5203d40
prerequisite-patch-id: 80042661ff6156ce577a72e9eb8c0b218b624829
prerequisite-patch-id: 398744c61913c76a35754de867c4f820ca7a8d99
prerequisite-patch-id: f59269382164b5d642a5e10443ca447f5caa595c
prerequisite-patch-id: 1babe83d6bf999bad17584dc595480f9070a5369
prerequisite-patch-id: 77be3d122d66df813f13088141ce27b21107a341
prerequisite-patch-id: 9fbb7ad1dd258bb8ff5946c4a0e59de4bfd82a04
prerequisite-patch-id: 6f6984916dffd0cc66aa733c9b6bd3a55495a50c
prerequisite-patch-id: 39e1be2a3d1593577ab997f55f59367cba665aa7
prerequisite-patch-id: 584c256c9acb52ee2773d0c81c3f4977fc18155a
prerequisite-patch-id: b37ac15032973e1fcd918f157c82a0606775c9e9
prerequisite-patch-id: 32deea16304859842af5c2151bc41d91cf6dfc9b
prerequisite-patch-id: 20ac2450fb93b3f69f83fc720fd4800a95e618a6
prerequisite-patch-id: 6abf359fa445f4104432ddee27044dfbfb128417
-- 
2.17.1


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

* [PATCH v3 0/3]  Add TDM audio on StarFive JH7110
@ 2023-05-06  9:01 ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-06  9:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, Walker Chen
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

This patchset adds TDM audio driver for the StarFive JH7110 SoC. The
first patch adds device tree binding for TDM module. The second patch
adds tdm driver support for JH7110 SoC. The last patch adds device node
of tdm and sound card to JH7110 dts.

The series has been tested on the VisionFive 2 board by plugging an
audio expansion board. 

For more information of audio expansion board, you can take a look
at the following webpage:
https://wiki.seeedstudio.com/ReSpeaker_2_Mics_Pi_HAT/

Changes since v2:
- Use dt-overlay to describe sound card because need to plug the audio
  expansion board into the VisionFive2 board.
- Modified the coding style for driver.
- Moved assignment of stop_dma_first to startup function of dai_driver.
- Dropped some useless macro definition.
- Use loops to get/enable/disable clocks. 

Changes since v1:
- Rebased on Linux 6.3-rc4.
- Added the dts file dedicated to describe audio card.
- Added the item for JH7110 audio board to the dt-binding of StarFive
  SoC-based boards.

---
v2: https://lore.kernel.org/all/20230420024118.22677-1-walker.chen@starfivetech.com/
v1: https://lore.kernel.org/all/20230329153320.31390-1-walker.chen@starfivetech.com/

Walker Chen (3):
  dt-bindings: sound: Add TDM for StarFive JH7110
  ASoC: starfive: Add JH7110 TDM driver
  riscv: dts: starfive: add tdm node and sound card

 .../bindings/sound/starfive,jh7110-tdm.yaml   |  98 +++
 MAINTAINERS                                   |   6 +
 arch/riscv/boot/dts/starfive/Makefile         |   3 +
 .../jh7110-starfive-visionfive-2-wm8960.dtso  |  70 +++
 .../jh7110-starfive-visionfive-2.dtsi         |  40 ++
 arch/riscv/boot/dts/starfive/jh7110.dtsi      |  21 +
 sound/soc/Kconfig                             |   1 +
 sound/soc/Makefile                            |   1 +
 sound/soc/starfive/Kconfig                    |  15 +
 sound/soc/starfive/Makefile                   |   2 +
 sound/soc/starfive/jh7110_tdm.c               | 573 ++++++++++++++++++
 sound/soc/starfive/jh7110_tdm.h               | 147 +++++
 12 files changed, 977 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
 create mode 100644 arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
 create mode 100644 sound/soc/starfive/Kconfig
 create mode 100644 sound/soc/starfive/Makefile
 create mode 100644 sound/soc/starfive/jh7110_tdm.c
 create mode 100644 sound/soc/starfive/jh7110_tdm.h


base-commit: 197b6b60ae7bc51dd0814953c562833143b292aa
prerequisite-patch-id: 30bec4dba6f250a6edd0c2cbab2ce09442e50e8a
prerequisite-patch-id: bb939c0c7c26b08addfccd890f9d3974b6eaec53
prerequisite-patch-id: 8a6f135bcabdad4a4bfb21f0c6a0ffd2bb57efe7
prerequisite-patch-id: c2366f993a9d85e28c06d8d09f064dd5e8b29a61
prerequisite-patch-id: 50d53a21f91f4087fc80b6f1f72864adfb0002b9
prerequisite-patch-id: 0df3703af91c30f1ca2c47f5609012f2d7200028
prerequisite-patch-id: 89f049f951e5acf75aab92541992f816fd0acc0d
prerequisite-patch-id: 551fae54377090044c3612fca9740a9b359abdd2
prerequisite-patch-id: c7fdf904f398d478f0ed6d57eb878982bc73329d
prerequisite-patch-id: 1b2d0982b18da060c82134f05bf3ce16425bac8d
prerequisite-patch-id: 090ba4b78d47bc19204916e76fdbc70021785388
prerequisite-patch-id: a5d9e0f7d4f8163f566678894cf693015119f2d9
prerequisite-patch-id: 4637a8fa2334a45fa6b64351f4e9e28d3e2d60d3
prerequisite-patch-id: 32647ec60a3b614e1c59ec8e54cb511ae832c22f
prerequisite-patch-id: aa06658ecf89c92d0dfdd6a4ba6d9e6e67532971
prerequisite-patch-id: 1387a7e87b446329dfc21f3e575ceae7ebcf954c
prerequisite-patch-id: 258ea5f9b8bf41b6981345dcc81795f25865d38f
prerequisite-patch-id: 8b6f2c9660c0ac0ee4e73e4c21aca8e6b75e81b9
prerequisite-patch-id: dbb0c0151b8bdf093e6ce79fd2fe3f60791a6e0b
prerequisite-patch-id: 9007c8610fdcd387592475949864edde874c20a2
prerequisite-patch-id: d57e95d31686772abc4c4d5aa1cadc344dc293cd
prerequisite-patch-id: 9f911969d0a550648493952c99096d26e05d4d83
prerequisite-patch-id: 2ddada18ab6ea5cd1da14212aaf59632f5203d40
prerequisite-patch-id: 80042661ff6156ce577a72e9eb8c0b218b624829
prerequisite-patch-id: 398744c61913c76a35754de867c4f820ca7a8d99
prerequisite-patch-id: f59269382164b5d642a5e10443ca447f5caa595c
prerequisite-patch-id: 1babe83d6bf999bad17584dc595480f9070a5369
prerequisite-patch-id: 77be3d122d66df813f13088141ce27b21107a341
prerequisite-patch-id: 9fbb7ad1dd258bb8ff5946c4a0e59de4bfd82a04
prerequisite-patch-id: 6f6984916dffd0cc66aa733c9b6bd3a55495a50c
prerequisite-patch-id: 39e1be2a3d1593577ab997f55f59367cba665aa7
prerequisite-patch-id: 584c256c9acb52ee2773d0c81c3f4977fc18155a
prerequisite-patch-id: b37ac15032973e1fcd918f157c82a0606775c9e9
prerequisite-patch-id: 32deea16304859842af5c2151bc41d91cf6dfc9b
prerequisite-patch-id: 20ac2450fb93b3f69f83fc720fd4800a95e618a6
prerequisite-patch-id: 6abf359fa445f4104432ddee27044dfbfb128417
-- 
2.17.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* [PATCH v3 0/3]  Add TDM audio on StarFive JH7110
@ 2023-05-06  9:01 ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-06  9:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, Walker Chen
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

This patchset adds TDM audio driver for the StarFive JH7110 SoC. The
first patch adds device tree binding for TDM module. The second patch
adds tdm driver support for JH7110 SoC. The last patch adds device node
of tdm and sound card to JH7110 dts.

The series has been tested on the VisionFive 2 board by plugging an
audio expansion board. 

For more information of audio expansion board, you can take a look
at the following webpage:
https://wiki.seeedstudio.com/ReSpeaker_2_Mics_Pi_HAT/

Changes since v2:
- Use dt-overlay to describe sound card because need to plug the audio
  expansion board into the VisionFive2 board.
- Modified the coding style for driver.
- Moved assignment of stop_dma_first to startup function of dai_driver.
- Dropped some useless macro definition.
- Use loops to get/enable/disable clocks. 

Changes since v1:
- Rebased on Linux 6.3-rc4.
- Added the dts file dedicated to describe audio card.
- Added the item for JH7110 audio board to the dt-binding of StarFive
  SoC-based boards.

---
v2: https://lore.kernel.org/all/20230420024118.22677-1-walker.chen@starfivetech.com/
v1: https://lore.kernel.org/all/20230329153320.31390-1-walker.chen@starfivetech.com/

Walker Chen (3):
  dt-bindings: sound: Add TDM for StarFive JH7110
  ASoC: starfive: Add JH7110 TDM driver
  riscv: dts: starfive: add tdm node and sound card

 .../bindings/sound/starfive,jh7110-tdm.yaml   |  98 +++
 MAINTAINERS                                   |   6 +
 arch/riscv/boot/dts/starfive/Makefile         |   3 +
 .../jh7110-starfive-visionfive-2-wm8960.dtso  |  70 +++
 .../jh7110-starfive-visionfive-2.dtsi         |  40 ++
 arch/riscv/boot/dts/starfive/jh7110.dtsi      |  21 +
 sound/soc/Kconfig                             |   1 +
 sound/soc/Makefile                            |   1 +
 sound/soc/starfive/Kconfig                    |  15 +
 sound/soc/starfive/Makefile                   |   2 +
 sound/soc/starfive/jh7110_tdm.c               | 573 ++++++++++++++++++
 sound/soc/starfive/jh7110_tdm.h               | 147 +++++
 12 files changed, 977 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
 create mode 100644 arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
 create mode 100644 sound/soc/starfive/Kconfig
 create mode 100644 sound/soc/starfive/Makefile
 create mode 100644 sound/soc/starfive/jh7110_tdm.c
 create mode 100644 sound/soc/starfive/jh7110_tdm.h


base-commit: 197b6b60ae7bc51dd0814953c562833143b292aa
prerequisite-patch-id: 30bec4dba6f250a6edd0c2cbab2ce09442e50e8a
prerequisite-patch-id: bb939c0c7c26b08addfccd890f9d3974b6eaec53
prerequisite-patch-id: 8a6f135bcabdad4a4bfb21f0c6a0ffd2bb57efe7
prerequisite-patch-id: c2366f993a9d85e28c06d8d09f064dd5e8b29a61
prerequisite-patch-id: 50d53a21f91f4087fc80b6f1f72864adfb0002b9
prerequisite-patch-id: 0df3703af91c30f1ca2c47f5609012f2d7200028
prerequisite-patch-id: 89f049f951e5acf75aab92541992f816fd0acc0d
prerequisite-patch-id: 551fae54377090044c3612fca9740a9b359abdd2
prerequisite-patch-id: c7fdf904f398d478f0ed6d57eb878982bc73329d
prerequisite-patch-id: 1b2d0982b18da060c82134f05bf3ce16425bac8d
prerequisite-patch-id: 090ba4b78d47bc19204916e76fdbc70021785388
prerequisite-patch-id: a5d9e0f7d4f8163f566678894cf693015119f2d9
prerequisite-patch-id: 4637a8fa2334a45fa6b64351f4e9e28d3e2d60d3
prerequisite-patch-id: 32647ec60a3b614e1c59ec8e54cb511ae832c22f
prerequisite-patch-id: aa06658ecf89c92d0dfdd6a4ba6d9e6e67532971
prerequisite-patch-id: 1387a7e87b446329dfc21f3e575ceae7ebcf954c
prerequisite-patch-id: 258ea5f9b8bf41b6981345dcc81795f25865d38f
prerequisite-patch-id: 8b6f2c9660c0ac0ee4e73e4c21aca8e6b75e81b9
prerequisite-patch-id: dbb0c0151b8bdf093e6ce79fd2fe3f60791a6e0b
prerequisite-patch-id: 9007c8610fdcd387592475949864edde874c20a2
prerequisite-patch-id: d57e95d31686772abc4c4d5aa1cadc344dc293cd
prerequisite-patch-id: 9f911969d0a550648493952c99096d26e05d4d83
prerequisite-patch-id: 2ddada18ab6ea5cd1da14212aaf59632f5203d40
prerequisite-patch-id: 80042661ff6156ce577a72e9eb8c0b218b624829
prerequisite-patch-id: 398744c61913c76a35754de867c4f820ca7a8d99
prerequisite-patch-id: f59269382164b5d642a5e10443ca447f5caa595c
prerequisite-patch-id: 1babe83d6bf999bad17584dc595480f9070a5369
prerequisite-patch-id: 77be3d122d66df813f13088141ce27b21107a341
prerequisite-patch-id: 9fbb7ad1dd258bb8ff5946c4a0e59de4bfd82a04
prerequisite-patch-id: 6f6984916dffd0cc66aa733c9b6bd3a55495a50c
prerequisite-patch-id: 39e1be2a3d1593577ab997f55f59367cba665aa7
prerequisite-patch-id: 584c256c9acb52ee2773d0c81c3f4977fc18155a
prerequisite-patch-id: b37ac15032973e1fcd918f157c82a0606775c9e9
prerequisite-patch-id: 32deea16304859842af5c2151bc41d91cf6dfc9b
prerequisite-patch-id: 20ac2450fb93b3f69f83fc720fd4800a95e618a6
prerequisite-patch-id: 6abf359fa445f4104432ddee27044dfbfb128417
-- 
2.17.1


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

* [PATCH v3 1/3] dt-bindings: sound: Add TDM for StarFive JH7110
  2023-05-06  9:01 ` Walker Chen
  (?)
@ 2023-05-06  9:01   ` Walker Chen
  -1 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-06  9:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, Walker Chen
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

Add bindings to describe the TDM driver for the StarFive JH7110 SoC.

Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
---
 .../bindings/sound/starfive,jh7110-tdm.yaml   | 98 +++++++++++++++++++
 1 file changed, 98 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml

diff --git a/Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml b/Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
new file mode 100644
index 000000000000..abb373fbfa26
--- /dev/null
+++ b/Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
@@ -0,0 +1,98 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/sound/starfive,jh7110-tdm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: StarFive JH7110 TDM Controller
+
+description: |
+  The TDM Controller is a Time Division Multiplexed audio interface
+  integrated in StarFive JH7110 SoC, allowing up to 8 channels of
+  audio over a serial interface. The TDM controller can operate both
+  in master and slave mode.
+
+maintainers:
+  - Walker Chen <walker.chen@starfivetech.com>
+
+allOf:
+  - $ref: dai-common.yaml#
+
+properties:
+  compatible:
+    enum:
+      - starfive,jh7110-tdm
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: TDM AHB Clock
+      - description: TDM APB Clock
+      - description: TDM Internal Clock
+      - description: TDM Clock
+      - description: Inner MCLK
+      - description: TDM External Clock
+
+  clock-names:
+    items:
+      - const: tdm_ahb
+      - const: tdm_apb
+      - const: tdm_internal
+      - const: tdm
+      - const: mclk_inner
+      - const: tdm_ext
+
+  resets:
+    items:
+      - description: tdm ahb reset line
+      - description: tdm apb reset line
+      - description: tdm core reset line
+
+  dmas:
+    items:
+      - description: RX DMA Channel
+      - description: TX DMA Channel
+
+  dma-names:
+    items:
+      - const: rx
+      - const: tx
+
+  "#sound-dai-cells":
+    const: 0
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - resets
+  - dmas
+  - dma-names
+  - "#sound-dai-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    tdm@10090000 {
+        compatible = "starfive,jh7110-tdm";
+        reg = <0x10090000 0x1000>;
+        clocks = <&syscrg 184>,
+                 <&syscrg 185>,
+                 <&syscrg 186>,
+                 <&syscrg 187>,
+                 <&syscrg 17>,
+                 <&tdm_ext>;
+        clock-names = "tdm_ahb", "tdm_apb",
+                      "tdm_internal", "tdm",
+                      "mclk_inner", "tdm_ext";
+        resets = <&syscrg 105>,
+                 <&syscrg 107>,
+                 <&syscrg 106>;
+        dmas = <&dma 20>, <&dma 21>;
+        dma-names = "rx","tx";
+        #sound-dai-cells = <0>;
+    };
-- 
2.17.1


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

* [PATCH v3 1/3] dt-bindings: sound: Add TDM for StarFive JH7110
@ 2023-05-06  9:01   ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-06  9:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, Walker Chen
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

Add bindings to describe the TDM driver for the StarFive JH7110 SoC.

Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
---
 .../bindings/sound/starfive,jh7110-tdm.yaml   | 98 +++++++++++++++++++
 1 file changed, 98 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml

diff --git a/Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml b/Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
new file mode 100644
index 000000000000..abb373fbfa26
--- /dev/null
+++ b/Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
@@ -0,0 +1,98 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/sound/starfive,jh7110-tdm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: StarFive JH7110 TDM Controller
+
+description: |
+  The TDM Controller is a Time Division Multiplexed audio interface
+  integrated in StarFive JH7110 SoC, allowing up to 8 channels of
+  audio over a serial interface. The TDM controller can operate both
+  in master and slave mode.
+
+maintainers:
+  - Walker Chen <walker.chen@starfivetech.com>
+
+allOf:
+  - $ref: dai-common.yaml#
+
+properties:
+  compatible:
+    enum:
+      - starfive,jh7110-tdm
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: TDM AHB Clock
+      - description: TDM APB Clock
+      - description: TDM Internal Clock
+      - description: TDM Clock
+      - description: Inner MCLK
+      - description: TDM External Clock
+
+  clock-names:
+    items:
+      - const: tdm_ahb
+      - const: tdm_apb
+      - const: tdm_internal
+      - const: tdm
+      - const: mclk_inner
+      - const: tdm_ext
+
+  resets:
+    items:
+      - description: tdm ahb reset line
+      - description: tdm apb reset line
+      - description: tdm core reset line
+
+  dmas:
+    items:
+      - description: RX DMA Channel
+      - description: TX DMA Channel
+
+  dma-names:
+    items:
+      - const: rx
+      - const: tx
+
+  "#sound-dai-cells":
+    const: 0
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - resets
+  - dmas
+  - dma-names
+  - "#sound-dai-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    tdm@10090000 {
+        compatible = "starfive,jh7110-tdm";
+        reg = <0x10090000 0x1000>;
+        clocks = <&syscrg 184>,
+                 <&syscrg 185>,
+                 <&syscrg 186>,
+                 <&syscrg 187>,
+                 <&syscrg 17>,
+                 <&tdm_ext>;
+        clock-names = "tdm_ahb", "tdm_apb",
+                      "tdm_internal", "tdm",
+                      "mclk_inner", "tdm_ext";
+        resets = <&syscrg 105>,
+                 <&syscrg 107>,
+                 <&syscrg 106>;
+        dmas = <&dma 20>, <&dma 21>;
+        dma-names = "rx","tx";
+        #sound-dai-cells = <0>;
+    };
-- 
2.17.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* [PATCH v3 1/3] dt-bindings: sound: Add TDM for StarFive JH7110
@ 2023-05-06  9:01   ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-06  9:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, Walker Chen
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

Add bindings to describe the TDM driver for the StarFive JH7110 SoC.

Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
---
 .../bindings/sound/starfive,jh7110-tdm.yaml   | 98 +++++++++++++++++++
 1 file changed, 98 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml

diff --git a/Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml b/Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
new file mode 100644
index 000000000000..abb373fbfa26
--- /dev/null
+++ b/Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
@@ -0,0 +1,98 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/sound/starfive,jh7110-tdm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: StarFive JH7110 TDM Controller
+
+description: |
+  The TDM Controller is a Time Division Multiplexed audio interface
+  integrated in StarFive JH7110 SoC, allowing up to 8 channels of
+  audio over a serial interface. The TDM controller can operate both
+  in master and slave mode.
+
+maintainers:
+  - Walker Chen <walker.chen@starfivetech.com>
+
+allOf:
+  - $ref: dai-common.yaml#
+
+properties:
+  compatible:
+    enum:
+      - starfive,jh7110-tdm
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: TDM AHB Clock
+      - description: TDM APB Clock
+      - description: TDM Internal Clock
+      - description: TDM Clock
+      - description: Inner MCLK
+      - description: TDM External Clock
+
+  clock-names:
+    items:
+      - const: tdm_ahb
+      - const: tdm_apb
+      - const: tdm_internal
+      - const: tdm
+      - const: mclk_inner
+      - const: tdm_ext
+
+  resets:
+    items:
+      - description: tdm ahb reset line
+      - description: tdm apb reset line
+      - description: tdm core reset line
+
+  dmas:
+    items:
+      - description: RX DMA Channel
+      - description: TX DMA Channel
+
+  dma-names:
+    items:
+      - const: rx
+      - const: tx
+
+  "#sound-dai-cells":
+    const: 0
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - resets
+  - dmas
+  - dma-names
+  - "#sound-dai-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    tdm@10090000 {
+        compatible = "starfive,jh7110-tdm";
+        reg = <0x10090000 0x1000>;
+        clocks = <&syscrg 184>,
+                 <&syscrg 185>,
+                 <&syscrg 186>,
+                 <&syscrg 187>,
+                 <&syscrg 17>,
+                 <&tdm_ext>;
+        clock-names = "tdm_ahb", "tdm_apb",
+                      "tdm_internal", "tdm",
+                      "mclk_inner", "tdm_ext";
+        resets = <&syscrg 105>,
+                 <&syscrg 107>,
+                 <&syscrg 106>;
+        dmas = <&dma 20>, <&dma 21>;
+        dma-names = "rx","tx";
+        #sound-dai-cells = <0>;
+    };
-- 
2.17.1


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

* [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
  2023-05-06  9:01 ` Walker Chen
  (?)
@ 2023-05-06  9:01   ` Walker Chen
  -1 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-06  9:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, Walker Chen
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

Add tdm driver support for the StarFive JH7110 SoC.

Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
---
 MAINTAINERS                     |   6 +
 sound/soc/Kconfig               |   1 +
 sound/soc/Makefile              |   1 +
 sound/soc/starfive/Kconfig      |  15 +
 sound/soc/starfive/Makefile     |   2 +
 sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
 sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
 7 files changed, 745 insertions(+)
 create mode 100644 sound/soc/starfive/Kconfig
 create mode 100644 sound/soc/starfive/Makefile
 create mode 100644 sound/soc/starfive/jh7110_tdm.c
 create mode 100644 sound/soc/starfive/jh7110_tdm.h

diff --git a/MAINTAINERS b/MAINTAINERS
index 5f9c544bc189..add89615d327 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -19945,6 +19945,12 @@ F:	Documentation/devicetree/bindings/power/starfive*
 F:	drivers/soc/starfive/jh71xx_pmu.c
 F:	include/dt-bindings/power/starfive,jh7110-pmu.h
 
+STARFIVE JH7110 TDM DRIVERS
+M:	Walker Chen <walker.chen@starfivetech.com>
+S:	Maintained
+F:	Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
+F:	sound/soc/starfive/jh7110-tdm.*
+
 STARFIVE SOC DRIVERS
 M:	Conor Dooley <conor@kernel.org>
 S:	Maintained
diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
index 848fbae26c3b..8d1d9401ecf2 100644
--- a/sound/soc/Kconfig
+++ b/sound/soc/Kconfig
@@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
 source "sound/soc/sof/Kconfig"
 source "sound/soc/spear/Kconfig"
 source "sound/soc/sprd/Kconfig"
+source "sound/soc/starfive/Kconfig"
 source "sound/soc/sti/Kconfig"
 source "sound/soc/stm/Kconfig"
 source "sound/soc/sunxi/Kconfig"
diff --git a/sound/soc/Makefile b/sound/soc/Makefile
index 507eaed1d6a1..65aeb4ef4068 100644
--- a/sound/soc/Makefile
+++ b/sound/soc/Makefile
@@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC)	+= sh/
 obj-$(CONFIG_SND_SOC)	+= sof/
 obj-$(CONFIG_SND_SOC)	+= spear/
 obj-$(CONFIG_SND_SOC)	+= sprd/
+obj-$(CONFIG_SND_SOC)	+= starfive/
 obj-$(CONFIG_SND_SOC)	+= sti/
 obj-$(CONFIG_SND_SOC)	+= stm/
 obj-$(CONFIG_SND_SOC)	+= sunxi/
diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
new file mode 100644
index 000000000000..737c956f7b93
--- /dev/null
+++ b/sound/soc/starfive/Kconfig
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config SND_SOC_STARFIVE
+	tristate "Audio support for StarFive SoC"
+	depends on COMPILE_TEST || SOC_STARFIVE
+	help
+	  Say Y or M if you want to add support for codecs attached to
+	  the Starfive SoCs' Audio interfaces. You will also need to
+	  select the audio interfaces to support below.
+
+config SND_SOC_JH7110_TDM
+	tristate "JH7110 TDM device driver"
+	depends on HAVE_CLK && SND_SOC_STARFIVE
+	select SND_SOC_GENERIC_DMAENGINE_PCM
+	help
+	  Say Y or M if you want to add support for StarFive TDM driver.
diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
new file mode 100644
index 000000000000..f7d960211d72
--- /dev/null
+++ b/sound/soc/starfive/Makefile
@@ -0,0 +1,2 @@
+# StarFive Platform Support
+obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
new file mode 100644
index 000000000000..33f7cf43e4bd
--- /dev/null
+++ b/sound/soc/starfive/jh7110_tdm.c
@@ -0,0 +1,573 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * jh7110_tdm.c -- StarFive JH7110 TDM driver
+ *
+ * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
+ *
+ * Author: Walker Chen <walker.chen@starfivetech.com>
+ */
+
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/of_irq.h>
+#include <linux/of_platform.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/reset.h>
+#include <sound/initval.h>
+#include <sound/pcm_params.h>
+#include <sound/soc.h>
+#include <sound/soc-dai.h>
+#include "jh7110_tdm.h"
+
+static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
+{
+	return readl_relaxed(tdm->tdm_base + reg);
+}
+
+static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
+{
+	writel_relaxed(val, tdm->tdm_base + reg);
+}
+
+static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
+				    struct snd_pcm_substream *substream)
+{
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
+	else
+		tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
+}
+
+static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
+			     struct snd_pcm_substream *substream)
+{
+	u32 data;
+
+	data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
+	jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
+
+	/* restore context */
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
+	else
+		jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
+}
+
+static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
+			    struct snd_pcm_substream *substream)
+{
+	unsigned int val;
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
+		val &= ~PCMTXCR_TXEN;
+		jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
+	} else {
+		val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
+		val &= ~PCMRXCR_RXEN;
+		jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
+	}
+}
+
+static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
+{
+	u32 sl, sscale, syncdiv;
+
+	if (tdm->rx.sl >= tdm->tx.sl)
+		sl = tdm->rx.sl;
+	else
+		sl = tdm->tx.sl;
+
+	if (tdm->rx.sscale >= tdm->tx.sscale)
+		sscale = tdm->rx.sscale;
+	else
+		sscale = tdm->tx.sscale;
+
+	syncdiv = tdm->pcmclk / tdm->samplerate - 1;
+
+	if ((syncdiv + 1) < (sl * sscale)) {
+		dev_err(tdm->dev, "Failed to set syncdiv!\n");
+		return -EINVAL;
+	}
+
+	if (tdm->syncm == TDM_SYNCM_LONG &&
+	    (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
+		if ((syncdiv + 1) <= sl) {
+			dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
+			return -EINVAL;
+		}
+	}
+
+	jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
+	return 0;
+}
+
+static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
+			     struct snd_pcm_substream *substream)
+{
+	u32 datarx, datatx;
+	int ret;
+
+	ret = jh7110_tdm_syncdiv(tdm);
+	if (ret)
+		return ret;
+
+	datarx = (tdm->rx.ifl << IFL_BIT) |
+		  (tdm->rx.wl << WL_BIT) |
+		  (tdm->rx.sscale << SSCALE_BIT) |
+		  (tdm->rx.sl << SL_BIT) |
+		  (tdm->rx.lrj << LRJ_BIT);
+
+	datatx = (tdm->tx.ifl << IFL_BIT) |
+		  (tdm->tx.wl << WL_BIT) |
+		  (tdm->tx.sscale << SSCALE_BIT) |
+		  (tdm->tx.sl << SL_BIT) |
+		  (tdm->tx.lrj << LRJ_BIT);
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
+	else
+		jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
+
+	return 0;
+}
+
+static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
+{
+	int i;
+
+	for (i = tdm->num_clks - 1; i >= 0; i--)
+		clk_disable_unprepare(tdm->clks[i]);
+}
+
+static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
+{
+	int i, ret;
+
+	for (i = 0; i < tdm->num_clks; i++) {
+		ret = clk_prepare_enable(tdm->clks[i]);
+		if (ret) {
+			while (i-- > 0)
+				clk_disable_unprepare(tdm->clks[i]);
+			return ret;
+		}
+	}
+
+	ret = reset_control_deassert(tdm->resets);
+	if (ret) {
+		dev_err(tdm->dev, "Failed to deassert tdm resets\n");
+		goto dis_tdm_clk;
+	}
+
+	/* select tdm_ext clock as the clock source for tdm */
+	ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
+	if (ret) {
+		dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
+		goto dis_tdm_clk;
+	}
+	return 0;
+
+dis_tdm_clk:
+	for (i = tdm->num_clks - 1; i >= 0; i--)
+		clk_disable_unprepare(tdm->clks[i]);
+
+	return ret;
+}
+
+#ifdef CONFIG_PM
+static int jh7110_tdm_runtime_suspend(struct device *dev)
+{
+	struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
+
+	jh7110_tdm_clk_disable(tdm);
+	return 0;
+}
+
+static int jh7110_tdm_runtime_resume(struct device *dev)
+{
+	struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
+
+	return jh7110_tdm_clk_enable(tdm);
+}
+#endif
+
+#ifdef CONFIG_PM_SLEEP
+static int jh7110_tdm_suspend(struct snd_soc_component *component)
+{
+	/* save context */
+	tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
+	tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);
+
+	return pm_runtime_force_suspend(component->dev);
+}
+
+static int jh7110_tdm_resume(struct snd_soc_component *component)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_component_get_drvdata(component);
+
+	/* restore context */
+	jh7110_tdm_writel(tdm, TDM_PCMGBCR, tdm->saved_pcmgbcr);
+	jh7110_tdm_writel(tdm, TDM_PCMDIV, tdm->saved_pcmdiv);
+
+	return pm_runtime_force_resume(component->dev);
+}
+
+#else
+#define jh7110_tdm_suspend	NULL
+#define jh7110_tdm_resume	NULL
+#endif
+
+static const struct snd_soc_component_driver jh7110_tdm_component = {
+	.name = "jh7110-tdm",
+	.suspend = jh7110_tdm_suspend,
+	.resume	= jh7110_tdm_resume,
+};
+
+static int jh7110_tdm_startup(struct snd_pcm_substream *substream,
+			      struct snd_soc_dai *cpu_dai)
+{
+	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
+	struct snd_soc_dai_link *dai_link = rtd->dai_link;
+
+	dai_link->stop_dma_first = 1;
+
+	return 0;
+}
+
+static int jh7110_tdm_hw_params(struct snd_pcm_substream *substream,
+				struct snd_pcm_hw_params *params,
+				struct snd_soc_dai *dai)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
+	int chan_wl, chan_sl, chan_nr;
+	unsigned int data_width;
+	unsigned int dma_bus_width;
+	struct snd_dmaengine_dai_dma_data *dma_data = NULL;
+	int ret = 0;
+
+	data_width = params_width(params);
+
+	tdm->samplerate = params_rate(params);
+	tdm->pcmclk = params_channels(params) * tdm->samplerate * data_width;
+
+	switch (params_format(params)) {
+	case SNDRV_PCM_FORMAT_S16_LE:
+		chan_wl = TDM_16BIT_WORD_LEN;
+		chan_sl = TDM_16BIT_SLOT_LEN;
+		dma_bus_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
+		break;
+
+	case SNDRV_PCM_FORMAT_S32_LE:
+		chan_wl = TDM_32BIT_WORD_LEN;
+		chan_sl = TDM_32BIT_SLOT_LEN;
+		dma_bus_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
+		break;
+
+	default:
+		dev_err(tdm->dev, "tdm: unsupported PCM fmt");
+		return -EINVAL;
+	}
+
+	chan_nr = params_channels(params);
+	switch (chan_nr) {
+	case 1:
+	case 2:
+	case 4:
+	case 6:
+	case 8:
+		break;
+	default:
+		dev_err(tdm->dev, "channel not supported\n");
+		return -EINVAL;
+	}
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		tdm->tx.wl = chan_wl;
+		tdm->tx.sl = chan_sl;
+		tdm->tx.sscale = chan_nr;
+		tdm->play_dma_data.addr_width = dma_bus_width;
+		dma_data = &tdm->play_dma_data;
+	} else {
+		tdm->rx.wl = chan_wl;
+		tdm->rx.sl = chan_sl;
+		tdm->rx.sscale = chan_nr;
+		tdm->capture_dma_data.addr_width = dma_bus_width;
+		dma_data = &tdm->capture_dma_data;
+	}
+
+	snd_soc_dai_set_dma_data(dai, substream, dma_data);
+
+	ret = jh7110_tdm_config(tdm, substream);
+	if (ret)
+		return ret;
+
+	jh7110_tdm_save_context(tdm, substream);
+	return 0;
+}
+
+static int jh7110_tdm_trigger(struct snd_pcm_substream *substream,
+			      int cmd, struct snd_soc_dai *dai)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
+	int ret = 0;
+
+	switch (cmd) {
+	case SNDRV_PCM_TRIGGER_START:
+	case SNDRV_PCM_TRIGGER_RESUME:
+	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+		jh7110_tdm_start(tdm, substream);
+		break;
+
+	case SNDRV_PCM_TRIGGER_STOP:
+	case SNDRV_PCM_TRIGGER_SUSPEND:
+	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
+		jh7110_tdm_stop(tdm, substream);
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+	return ret;
+}
+
+static int jh7110_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
+				  unsigned int fmt)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(cpu_dai);
+	unsigned int gbcr;
+	int ret = 0;
+
+	/* set master/slave audio interface */
+	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
+	case SND_SOC_DAIFMT_BP_FP:
+		/* cpu is master */
+		tdm->ms_mode = TDM_AS_MASTER;
+		break;
+	case SND_SOC_DAIFMT_BC_FC:
+		/* codec is master */
+		tdm->ms_mode = TDM_AS_SLAVE;
+		break;
+	case SND_SOC_DAIFMT_BC_FP:
+	case SND_SOC_DAIFMT_BP_FC:
+		ret = -EINVAL;
+		break;
+	default:
+		dev_dbg(tdm->dev, "dwc : Invalid clock provider format\n");
+		ret = -EINVAL;
+		break;
+	}
+
+	gbcr = (tdm->clkpolity << CLKPOL_BIT) |
+		(tdm->elm << ELM_BIT) |
+		(tdm->syncm << SYNCM_BIT) |
+		(tdm->ms_mode << MS_BIT);
+	jh7110_tdm_writel(tdm, TDM_PCMGBCR, gbcr);
+
+	return ret;
+}
+
+static const struct snd_soc_dai_ops jh7110_tdm_dai_ops = {
+	.startup	= jh7110_tdm_startup,
+	.hw_params	= jh7110_tdm_hw_params,
+	.trigger	= jh7110_tdm_trigger,
+	.set_fmt	= jh7110_tdm_set_dai_fmt,
+};
+
+static int jh7110_tdm_dai_probe(struct snd_soc_dai *dai)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
+
+	snd_soc_dai_init_dma_data(dai, &tdm->play_dma_data, &tdm->capture_dma_data);
+	snd_soc_dai_set_drvdata(dai, tdm);
+	return 0;
+}
+
+#define JH7110_TDM_RATES	SNDRV_PCM_RATE_8000_48000
+
+#define JH7110_TDM_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | \
+				 SNDRV_PCM_FMTBIT_S32_LE)
+
+static struct snd_soc_dai_driver jh7110_tdm_dai = {
+	.name = "sf_tdm",
+	.id = 0,
+	.playback = {
+		.stream_name    = "Playback",
+		.channels_min   = 1,
+		.channels_max   = 8,
+		.rates          = JH7110_TDM_RATES,
+		.formats        = JH7110_TDM_FORMATS,
+	},
+	.capture = {
+		.stream_name    = "Capture",
+		.channels_min   = 1,
+		.channels_max   = 8,
+		.rates          = JH7110_TDM_RATES,
+		.formats        = JH7110_TDM_FORMATS,
+	},
+	.ops = &jh7110_tdm_dai_ops,
+	.probe = jh7110_tdm_dai_probe,
+	.symmetric_rate = 1,
+};
+
+static const struct snd_pcm_hardware jh7110_pcm_hardware = {
+	.info			= (SNDRV_PCM_INFO_MMAP		|
+				   SNDRV_PCM_INFO_MMAP_VALID	|
+				   SNDRV_PCM_INFO_PAUSE		|
+				   SNDRV_PCM_INFO_RESUME	|
+				   SNDRV_PCM_INFO_INTERLEAVED	|
+				   SNDRV_PCM_INFO_BLOCK_TRANSFER),
+	.buffer_bytes_max	= 192512,
+	.period_bytes_min	= 4096,
+	.period_bytes_max	= 32768,
+	.periods_min		= 1,
+	.periods_max		= 48,
+	.fifo_size		= 16,
+};
+
+static const struct snd_dmaengine_pcm_config jh7110_dmaengine_pcm_config = {
+	.pcm_hardware = &jh7110_pcm_hardware,
+	.prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
+	.prealloc_buffer_size = 192512,
+};
+
+static void jh7110_tdm_init_params(struct jh7110_tdm_dev *tdm)
+{
+	tdm->clkpolity = TDM_TX_RASING_RX_FALLING;
+	tdm->elm = TDM_ELM_LATE;
+	tdm->syncm = TDM_SYNCM_SHORT;
+
+	tdm->rx.ifl = TDM_FIFO_HALF;
+	tdm->tx.ifl = TDM_FIFO_HALF;
+	tdm->rx.wl = TDM_16BIT_WORD_LEN;
+	tdm->tx.wl = TDM_16BIT_WORD_LEN;
+	tdm->rx.sscale = 2;
+	tdm->tx.sscale = 2;
+	tdm->rx.lrj = TDM_LEFT_JUSTIFT;
+	tdm->tx.lrj = TDM_LEFT_JUSTIFT;
+
+	tdm->play_dma_data.addr = JH7110_TDM_FIFO;
+	tdm->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
+	tdm->play_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
+	tdm->play_dma_data.maxburst = 16;
+
+	tdm->capture_dma_data.addr = JH7110_TDM_FIFO;
+	tdm->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
+	tdm->capture_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
+	tdm->capture_dma_data.maxburst = 8;
+}
+
+static int jh7110_tdm_clk_reset_init(struct platform_device *pdev,
+				     struct jh7110_tdm_dev *tdm)
+{
+	int i, ret;
+
+	for (i = 0; i < tdm->num_clks; i++) {
+		tdm->clks[i] = devm_clk_get(&pdev->dev, tdm->clk_names[i]);
+		if (IS_ERR(tdm->clks[i])) {
+			dev_err(&pdev->dev, "Failed to get clock: %s\n",
+				tdm->clk_names[i]);
+			return PTR_ERR(tdm->clks[i]);
+		}
+	}
+
+	tdm->resets = devm_reset_control_array_get_exclusive(&pdev->dev);
+	if (IS_ERR(tdm->resets)) {
+		ret = PTR_ERR(tdm->resets);
+		dev_err(&pdev->dev, "Failed to get tdm resets");
+		return ret;
+	}
+
+	return jh7110_tdm_clk_enable(tdm);
+}
+
+static int jh7110_tdm_probe(struct platform_device *pdev)
+{
+	struct jh7110_tdm_dev *tdm;
+	const struct starfive_tdm_driverdata *driver_data;
+	int ret;
+
+	tdm = devm_kzalloc(&pdev->dev, sizeof(*tdm), GFP_KERNEL);
+	if (!tdm)
+		return -ENOMEM;
+
+	driver_data = of_device_get_match_data(&pdev->dev);
+	tdm->clk_names = (const char **)driver_data->clk_names;
+	tdm->num_clks = driver_data->num_clks;
+
+	tdm->tdm_base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(tdm->tdm_base))
+		return PTR_ERR(tdm->tdm_base);
+
+	tdm->dev = &pdev->dev;
+
+	ret = jh7110_tdm_clk_reset_init(pdev, tdm);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to enable audio-tdm clock\n");
+		return ret;
+	}
+
+	jh7110_tdm_init_params(tdm);
+
+	dev_set_drvdata(&pdev->dev, tdm);
+	ret = devm_snd_soc_register_component(&pdev->dev, &jh7110_tdm_component,
+					      &jh7110_tdm_dai, 1);
+	if (ret != 0) {
+		dev_err(&pdev->dev, "Failed to register dai\n");
+		return ret;
+	}
+
+	ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
+					      &jh7110_dmaengine_pcm_config,
+					      SND_DMAENGINE_PCM_FLAG_COMPAT);
+	if (ret) {
+		dev_err(&pdev->dev, "Could not register pcm: %d\n", ret);
+		return ret;
+	}
+
+	pm_runtime_enable(&pdev->dev);
+#ifdef CONFIG_PM
+	jh7110_tdm_clk_disable(tdm);
+#endif
+
+	return 0;
+}
+
+static int jh7110_tdm_dev_remove(struct platform_device *pdev)
+{
+	pm_runtime_disable(&pdev->dev);
+	return 0;
+}
+
+static const struct starfive_tdm_driverdata jh7110_drvdata = {
+	.clk_names = {"mclk_inner", "tdm_ahb", "tdm_apb", "tdm_internal", "tdm_ext", "tdm"},
+	.num_clks = 6,
+};
+
+static const struct of_device_id jh7110_tdm_of_match[] = {
+	{ .compatible = "starfive,jh7110-tdm", .data = &jh7110_drvdata },
+	{}
+};
+
+MODULE_DEVICE_TABLE(of, jh7110_tdm_of_match);
+
+static const struct dev_pm_ops jh7110_tdm_pm_ops = {
+	SET_RUNTIME_PM_OPS(jh7110_tdm_runtime_suspend,
+			   jh7110_tdm_runtime_resume, NULL)
+};
+
+static struct platform_driver jh7110_tdm_driver = {
+	.driver = {
+		.name = "jh7110-tdm",
+		.of_match_table = jh7110_tdm_of_match,
+		.pm = &jh7110_tdm_pm_ops,
+	},
+	.probe = jh7110_tdm_probe,
+	.remove = jh7110_tdm_dev_remove,
+};
+module_platform_driver(jh7110_tdm_driver);
+
+MODULE_DESCRIPTION("StarFive JH7110 TDM ASoC Driver");
+MODULE_AUTHOR("Walker Chen <walker.chen@starfivetech.com>");
+MODULE_LICENSE("GPL");
diff --git a/sound/soc/starfive/jh7110_tdm.h b/sound/soc/starfive/jh7110_tdm.h
new file mode 100644
index 000000000000..80f17946a45d
--- /dev/null
+++ b/sound/soc/starfive/jh7110_tdm.h
@@ -0,0 +1,147 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * TDM driver for the StarFive JH7110 SoC
+ *
+ * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
+ *
+ * Author: Walker Chen <walker.chen@starfivetech.com>
+ */
+
+#ifndef __SND_SOC_STARFIVE_TDM_H
+#define __SND_SOC_STARFIVE_TDM_H
+
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/types.h>
+#include <sound/dmaengine_pcm.h>
+#include <sound/pcm.h>
+#include <linux/dmaengine.h>
+#include <linux/types.h>
+
+#define TDM_PCMGBCR			0x00
+	#define PCMGBCR_MASK		0x1e
+	#define PCMGBCR_ENABLE		BIT(0)
+	#define PCMGBCR_TRITXEN		BIT(4)
+	#define CLKPOL_BIT		5
+	#define TRITXEN_BIT		4
+	#define ELM_BIT			3
+	#define SYNCM_BIT		2
+	#define MS_BIT			1
+#define TDM_PCMTXCR			0x04
+	#define PCMTXCR_TXEN		BIT(0)
+	#define IFL_BIT			11
+	#define WL_BIT			8
+	#define SSCALE_BIT		4
+	#define SL_BIT			2
+	#define LRJ_BIT			1
+#define TDM_PCMRXCR			0x08
+	#define PCMRXCR_RXEN		BIT(0)
+	#define PCMRXCR_RXSL_MASK	0xc
+	#define PCMRXCR_RXSL_16BIT	0x4
+	#define PCMRXCR_RXSL_32BIT	0x8
+	#define PCMRXCR_SCALE_MASK	0xf0
+	#define PCMRXCR_SCALE_1CH	0x10
+#define TDM_PCMDIV			0x0c
+
+/*  DMA registers */
+#define JH7110_TDM_FIFO			0x170c0000
+#define JH7110_TDM_FIFO_DEPTH		32
+#define JH7110_TDM_MAX_CLOCKS		6
+
+enum TDM_MASTER_SLAVE_MODE {
+	TDM_AS_MASTER = 0,
+	TDM_AS_SLAVE,
+};
+
+enum TDM_CLKPOL {
+	/* tx raising and rx falling */
+	TDM_TX_RASING_RX_FALLING = 0,
+	/* tx falling and rx raising */
+	TDM_TX_FALLING_RX_RASING,
+};
+
+enum TDM_ELM {
+	/* only work while SYNCM=0 */
+	TDM_ELM_LATE = 0,
+	TDM_ELM_EARLY,
+};
+
+enum TDM_SYNCM {
+	/* short frame sync */
+	TDM_SYNCM_SHORT = 0,
+	/* long frame sync */
+	TDM_SYNCM_LONG,
+};
+
+enum TDM_IFL {
+	/* FIFO to send or received : half-1/2, Quarter-1/4 */
+	TDM_FIFO_HALF = 0,
+	TDM_FIFO_QUARTER,
+};
+
+enum TDM_WL {
+	/* send or received word length */
+	TDM_8BIT_WORD_LEN = 0,
+	TDM_16BIT_WORD_LEN,
+	TDM_20BIT_WORD_LEN,
+	TDM_24BIT_WORD_LEN,
+	TDM_32BIT_WORD_LEN,
+};
+
+enum TDM_SL {
+	/* send or received slot length */
+	TDM_8BIT_SLOT_LEN = 0,
+	TDM_16BIT_SLOT_LEN,
+	TDM_32BIT_SLOT_LEN,
+};
+
+enum TDM_LRJ {
+	/* left-justify or right-justify */
+	TDM_RIGHT_JUSTIFY = 0,
+	TDM_LEFT_JUSTIFT,
+};
+
+struct tdm_chan_cfg {
+	enum TDM_IFL ifl;
+	enum TDM_WL  wl;
+	unsigned char sscale;
+	enum TDM_SL  sl;
+	enum TDM_LRJ lrj;
+	unsigned char enable;
+};
+
+struct starfive_tdm_driverdata {
+	const char *clk_names[JH7110_TDM_MAX_CLOCKS];
+	int num_clks;
+};
+
+struct jh7110_tdm_dev {
+	void __iomem *tdm_base;
+	struct device *dev;
+	const char **clk_names;
+	struct clk *clks[JH7110_TDM_MAX_CLOCKS];
+	int num_clks;
+	struct reset_control *resets;
+
+	enum TDM_CLKPOL clkpolity;
+	enum TDM_ELM	elm;
+	enum TDM_SYNCM	syncm;
+	enum TDM_MASTER_SLAVE_MODE ms_mode;
+
+	struct tdm_chan_cfg tx;
+	struct tdm_chan_cfg rx;
+
+	u16 syncdiv;
+	u32 samplerate;
+	u32 pcmclk;
+
+	/* data related to DMA transfers b/w tdm and DMAC */
+	struct snd_dmaengine_dai_dma_data play_dma_data;
+	struct snd_dmaengine_dai_dma_data capture_dma_data;
+	u32 saved_pcmgbcr;
+	u32 saved_pcmtxcr;
+	u32 saved_pcmrxcr;
+	u32 saved_pcmdiv;
+};
+
+#endif	/* __SND_SOC_STARFIVE_TDM_H */
-- 
2.17.1


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

* [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
@ 2023-05-06  9:01   ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-06  9:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, Walker Chen
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

Add tdm driver support for the StarFive JH7110 SoC.

Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
---
 MAINTAINERS                     |   6 +
 sound/soc/Kconfig               |   1 +
 sound/soc/Makefile              |   1 +
 sound/soc/starfive/Kconfig      |  15 +
 sound/soc/starfive/Makefile     |   2 +
 sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
 sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
 7 files changed, 745 insertions(+)
 create mode 100644 sound/soc/starfive/Kconfig
 create mode 100644 sound/soc/starfive/Makefile
 create mode 100644 sound/soc/starfive/jh7110_tdm.c
 create mode 100644 sound/soc/starfive/jh7110_tdm.h

diff --git a/MAINTAINERS b/MAINTAINERS
index 5f9c544bc189..add89615d327 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -19945,6 +19945,12 @@ F:	Documentation/devicetree/bindings/power/starfive*
 F:	drivers/soc/starfive/jh71xx_pmu.c
 F:	include/dt-bindings/power/starfive,jh7110-pmu.h
 
+STARFIVE JH7110 TDM DRIVERS
+M:	Walker Chen <walker.chen@starfivetech.com>
+S:	Maintained
+F:	Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
+F:	sound/soc/starfive/jh7110-tdm.*
+
 STARFIVE SOC DRIVERS
 M:	Conor Dooley <conor@kernel.org>
 S:	Maintained
diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
index 848fbae26c3b..8d1d9401ecf2 100644
--- a/sound/soc/Kconfig
+++ b/sound/soc/Kconfig
@@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
 source "sound/soc/sof/Kconfig"
 source "sound/soc/spear/Kconfig"
 source "sound/soc/sprd/Kconfig"
+source "sound/soc/starfive/Kconfig"
 source "sound/soc/sti/Kconfig"
 source "sound/soc/stm/Kconfig"
 source "sound/soc/sunxi/Kconfig"
diff --git a/sound/soc/Makefile b/sound/soc/Makefile
index 507eaed1d6a1..65aeb4ef4068 100644
--- a/sound/soc/Makefile
+++ b/sound/soc/Makefile
@@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC)	+= sh/
 obj-$(CONFIG_SND_SOC)	+= sof/
 obj-$(CONFIG_SND_SOC)	+= spear/
 obj-$(CONFIG_SND_SOC)	+= sprd/
+obj-$(CONFIG_SND_SOC)	+= starfive/
 obj-$(CONFIG_SND_SOC)	+= sti/
 obj-$(CONFIG_SND_SOC)	+= stm/
 obj-$(CONFIG_SND_SOC)	+= sunxi/
diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
new file mode 100644
index 000000000000..737c956f7b93
--- /dev/null
+++ b/sound/soc/starfive/Kconfig
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config SND_SOC_STARFIVE
+	tristate "Audio support for StarFive SoC"
+	depends on COMPILE_TEST || SOC_STARFIVE
+	help
+	  Say Y or M if you want to add support for codecs attached to
+	  the Starfive SoCs' Audio interfaces. You will also need to
+	  select the audio interfaces to support below.
+
+config SND_SOC_JH7110_TDM
+	tristate "JH7110 TDM device driver"
+	depends on HAVE_CLK && SND_SOC_STARFIVE
+	select SND_SOC_GENERIC_DMAENGINE_PCM
+	help
+	  Say Y or M if you want to add support for StarFive TDM driver.
diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
new file mode 100644
index 000000000000..f7d960211d72
--- /dev/null
+++ b/sound/soc/starfive/Makefile
@@ -0,0 +1,2 @@
+# StarFive Platform Support
+obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
new file mode 100644
index 000000000000..33f7cf43e4bd
--- /dev/null
+++ b/sound/soc/starfive/jh7110_tdm.c
@@ -0,0 +1,573 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * jh7110_tdm.c -- StarFive JH7110 TDM driver
+ *
+ * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
+ *
+ * Author: Walker Chen <walker.chen@starfivetech.com>
+ */
+
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/of_irq.h>
+#include <linux/of_platform.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/reset.h>
+#include <sound/initval.h>
+#include <sound/pcm_params.h>
+#include <sound/soc.h>
+#include <sound/soc-dai.h>
+#include "jh7110_tdm.h"
+
+static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
+{
+	return readl_relaxed(tdm->tdm_base + reg);
+}
+
+static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
+{
+	writel_relaxed(val, tdm->tdm_base + reg);
+}
+
+static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
+				    struct snd_pcm_substream *substream)
+{
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
+	else
+		tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
+}
+
+static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
+			     struct snd_pcm_substream *substream)
+{
+	u32 data;
+
+	data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
+	jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
+
+	/* restore context */
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
+	else
+		jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
+}
+
+static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
+			    struct snd_pcm_substream *substream)
+{
+	unsigned int val;
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
+		val &= ~PCMTXCR_TXEN;
+		jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
+	} else {
+		val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
+		val &= ~PCMRXCR_RXEN;
+		jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
+	}
+}
+
+static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
+{
+	u32 sl, sscale, syncdiv;
+
+	if (tdm->rx.sl >= tdm->tx.sl)
+		sl = tdm->rx.sl;
+	else
+		sl = tdm->tx.sl;
+
+	if (tdm->rx.sscale >= tdm->tx.sscale)
+		sscale = tdm->rx.sscale;
+	else
+		sscale = tdm->tx.sscale;
+
+	syncdiv = tdm->pcmclk / tdm->samplerate - 1;
+
+	if ((syncdiv + 1) < (sl * sscale)) {
+		dev_err(tdm->dev, "Failed to set syncdiv!\n");
+		return -EINVAL;
+	}
+
+	if (tdm->syncm == TDM_SYNCM_LONG &&
+	    (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
+		if ((syncdiv + 1) <= sl) {
+			dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
+			return -EINVAL;
+		}
+	}
+
+	jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
+	return 0;
+}
+
+static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
+			     struct snd_pcm_substream *substream)
+{
+	u32 datarx, datatx;
+	int ret;
+
+	ret = jh7110_tdm_syncdiv(tdm);
+	if (ret)
+		return ret;
+
+	datarx = (tdm->rx.ifl << IFL_BIT) |
+		  (tdm->rx.wl << WL_BIT) |
+		  (tdm->rx.sscale << SSCALE_BIT) |
+		  (tdm->rx.sl << SL_BIT) |
+		  (tdm->rx.lrj << LRJ_BIT);
+
+	datatx = (tdm->tx.ifl << IFL_BIT) |
+		  (tdm->tx.wl << WL_BIT) |
+		  (tdm->tx.sscale << SSCALE_BIT) |
+		  (tdm->tx.sl << SL_BIT) |
+		  (tdm->tx.lrj << LRJ_BIT);
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
+	else
+		jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
+
+	return 0;
+}
+
+static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
+{
+	int i;
+
+	for (i = tdm->num_clks - 1; i >= 0; i--)
+		clk_disable_unprepare(tdm->clks[i]);
+}
+
+static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
+{
+	int i, ret;
+
+	for (i = 0; i < tdm->num_clks; i++) {
+		ret = clk_prepare_enable(tdm->clks[i]);
+		if (ret) {
+			while (i-- > 0)
+				clk_disable_unprepare(tdm->clks[i]);
+			return ret;
+		}
+	}
+
+	ret = reset_control_deassert(tdm->resets);
+	if (ret) {
+		dev_err(tdm->dev, "Failed to deassert tdm resets\n");
+		goto dis_tdm_clk;
+	}
+
+	/* select tdm_ext clock as the clock source for tdm */
+	ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
+	if (ret) {
+		dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
+		goto dis_tdm_clk;
+	}
+	return 0;
+
+dis_tdm_clk:
+	for (i = tdm->num_clks - 1; i >= 0; i--)
+		clk_disable_unprepare(tdm->clks[i]);
+
+	return ret;
+}
+
+#ifdef CONFIG_PM
+static int jh7110_tdm_runtime_suspend(struct device *dev)
+{
+	struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
+
+	jh7110_tdm_clk_disable(tdm);
+	return 0;
+}
+
+static int jh7110_tdm_runtime_resume(struct device *dev)
+{
+	struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
+
+	return jh7110_tdm_clk_enable(tdm);
+}
+#endif
+
+#ifdef CONFIG_PM_SLEEP
+static int jh7110_tdm_suspend(struct snd_soc_component *component)
+{
+	/* save context */
+	tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
+	tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);
+
+	return pm_runtime_force_suspend(component->dev);
+}
+
+static int jh7110_tdm_resume(struct snd_soc_component *component)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_component_get_drvdata(component);
+
+	/* restore context */
+	jh7110_tdm_writel(tdm, TDM_PCMGBCR, tdm->saved_pcmgbcr);
+	jh7110_tdm_writel(tdm, TDM_PCMDIV, tdm->saved_pcmdiv);
+
+	return pm_runtime_force_resume(component->dev);
+}
+
+#else
+#define jh7110_tdm_suspend	NULL
+#define jh7110_tdm_resume	NULL
+#endif
+
+static const struct snd_soc_component_driver jh7110_tdm_component = {
+	.name = "jh7110-tdm",
+	.suspend = jh7110_tdm_suspend,
+	.resume	= jh7110_tdm_resume,
+};
+
+static int jh7110_tdm_startup(struct snd_pcm_substream *substream,
+			      struct snd_soc_dai *cpu_dai)
+{
+	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
+	struct snd_soc_dai_link *dai_link = rtd->dai_link;
+
+	dai_link->stop_dma_first = 1;
+
+	return 0;
+}
+
+static int jh7110_tdm_hw_params(struct snd_pcm_substream *substream,
+				struct snd_pcm_hw_params *params,
+				struct snd_soc_dai *dai)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
+	int chan_wl, chan_sl, chan_nr;
+	unsigned int data_width;
+	unsigned int dma_bus_width;
+	struct snd_dmaengine_dai_dma_data *dma_data = NULL;
+	int ret = 0;
+
+	data_width = params_width(params);
+
+	tdm->samplerate = params_rate(params);
+	tdm->pcmclk = params_channels(params) * tdm->samplerate * data_width;
+
+	switch (params_format(params)) {
+	case SNDRV_PCM_FORMAT_S16_LE:
+		chan_wl = TDM_16BIT_WORD_LEN;
+		chan_sl = TDM_16BIT_SLOT_LEN;
+		dma_bus_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
+		break;
+
+	case SNDRV_PCM_FORMAT_S32_LE:
+		chan_wl = TDM_32BIT_WORD_LEN;
+		chan_sl = TDM_32BIT_SLOT_LEN;
+		dma_bus_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
+		break;
+
+	default:
+		dev_err(tdm->dev, "tdm: unsupported PCM fmt");
+		return -EINVAL;
+	}
+
+	chan_nr = params_channels(params);
+	switch (chan_nr) {
+	case 1:
+	case 2:
+	case 4:
+	case 6:
+	case 8:
+		break;
+	default:
+		dev_err(tdm->dev, "channel not supported\n");
+		return -EINVAL;
+	}
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		tdm->tx.wl = chan_wl;
+		tdm->tx.sl = chan_sl;
+		tdm->tx.sscale = chan_nr;
+		tdm->play_dma_data.addr_width = dma_bus_width;
+		dma_data = &tdm->play_dma_data;
+	} else {
+		tdm->rx.wl = chan_wl;
+		tdm->rx.sl = chan_sl;
+		tdm->rx.sscale = chan_nr;
+		tdm->capture_dma_data.addr_width = dma_bus_width;
+		dma_data = &tdm->capture_dma_data;
+	}
+
+	snd_soc_dai_set_dma_data(dai, substream, dma_data);
+
+	ret = jh7110_tdm_config(tdm, substream);
+	if (ret)
+		return ret;
+
+	jh7110_tdm_save_context(tdm, substream);
+	return 0;
+}
+
+static int jh7110_tdm_trigger(struct snd_pcm_substream *substream,
+			      int cmd, struct snd_soc_dai *dai)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
+	int ret = 0;
+
+	switch (cmd) {
+	case SNDRV_PCM_TRIGGER_START:
+	case SNDRV_PCM_TRIGGER_RESUME:
+	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+		jh7110_tdm_start(tdm, substream);
+		break;
+
+	case SNDRV_PCM_TRIGGER_STOP:
+	case SNDRV_PCM_TRIGGER_SUSPEND:
+	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
+		jh7110_tdm_stop(tdm, substream);
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+	return ret;
+}
+
+static int jh7110_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
+				  unsigned int fmt)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(cpu_dai);
+	unsigned int gbcr;
+	int ret = 0;
+
+	/* set master/slave audio interface */
+	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
+	case SND_SOC_DAIFMT_BP_FP:
+		/* cpu is master */
+		tdm->ms_mode = TDM_AS_MASTER;
+		break;
+	case SND_SOC_DAIFMT_BC_FC:
+		/* codec is master */
+		tdm->ms_mode = TDM_AS_SLAVE;
+		break;
+	case SND_SOC_DAIFMT_BC_FP:
+	case SND_SOC_DAIFMT_BP_FC:
+		ret = -EINVAL;
+		break;
+	default:
+		dev_dbg(tdm->dev, "dwc : Invalid clock provider format\n");
+		ret = -EINVAL;
+		break;
+	}
+
+	gbcr = (tdm->clkpolity << CLKPOL_BIT) |
+		(tdm->elm << ELM_BIT) |
+		(tdm->syncm << SYNCM_BIT) |
+		(tdm->ms_mode << MS_BIT);
+	jh7110_tdm_writel(tdm, TDM_PCMGBCR, gbcr);
+
+	return ret;
+}
+
+static const struct snd_soc_dai_ops jh7110_tdm_dai_ops = {
+	.startup	= jh7110_tdm_startup,
+	.hw_params	= jh7110_tdm_hw_params,
+	.trigger	= jh7110_tdm_trigger,
+	.set_fmt	= jh7110_tdm_set_dai_fmt,
+};
+
+static int jh7110_tdm_dai_probe(struct snd_soc_dai *dai)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
+
+	snd_soc_dai_init_dma_data(dai, &tdm->play_dma_data, &tdm->capture_dma_data);
+	snd_soc_dai_set_drvdata(dai, tdm);
+	return 0;
+}
+
+#define JH7110_TDM_RATES	SNDRV_PCM_RATE_8000_48000
+
+#define JH7110_TDM_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | \
+				 SNDRV_PCM_FMTBIT_S32_LE)
+
+static struct snd_soc_dai_driver jh7110_tdm_dai = {
+	.name = "sf_tdm",
+	.id = 0,
+	.playback = {
+		.stream_name    = "Playback",
+		.channels_min   = 1,
+		.channels_max   = 8,
+		.rates          = JH7110_TDM_RATES,
+		.formats        = JH7110_TDM_FORMATS,
+	},
+	.capture = {
+		.stream_name    = "Capture",
+		.channels_min   = 1,
+		.channels_max   = 8,
+		.rates          = JH7110_TDM_RATES,
+		.formats        = JH7110_TDM_FORMATS,
+	},
+	.ops = &jh7110_tdm_dai_ops,
+	.probe = jh7110_tdm_dai_probe,
+	.symmetric_rate = 1,
+};
+
+static const struct snd_pcm_hardware jh7110_pcm_hardware = {
+	.info			= (SNDRV_PCM_INFO_MMAP		|
+				   SNDRV_PCM_INFO_MMAP_VALID	|
+				   SNDRV_PCM_INFO_PAUSE		|
+				   SNDRV_PCM_INFO_RESUME	|
+				   SNDRV_PCM_INFO_INTERLEAVED	|
+				   SNDRV_PCM_INFO_BLOCK_TRANSFER),
+	.buffer_bytes_max	= 192512,
+	.period_bytes_min	= 4096,
+	.period_bytes_max	= 32768,
+	.periods_min		= 1,
+	.periods_max		= 48,
+	.fifo_size		= 16,
+};
+
+static const struct snd_dmaengine_pcm_config jh7110_dmaengine_pcm_config = {
+	.pcm_hardware = &jh7110_pcm_hardware,
+	.prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
+	.prealloc_buffer_size = 192512,
+};
+
+static void jh7110_tdm_init_params(struct jh7110_tdm_dev *tdm)
+{
+	tdm->clkpolity = TDM_TX_RASING_RX_FALLING;
+	tdm->elm = TDM_ELM_LATE;
+	tdm->syncm = TDM_SYNCM_SHORT;
+
+	tdm->rx.ifl = TDM_FIFO_HALF;
+	tdm->tx.ifl = TDM_FIFO_HALF;
+	tdm->rx.wl = TDM_16BIT_WORD_LEN;
+	tdm->tx.wl = TDM_16BIT_WORD_LEN;
+	tdm->rx.sscale = 2;
+	tdm->tx.sscale = 2;
+	tdm->rx.lrj = TDM_LEFT_JUSTIFT;
+	tdm->tx.lrj = TDM_LEFT_JUSTIFT;
+
+	tdm->play_dma_data.addr = JH7110_TDM_FIFO;
+	tdm->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
+	tdm->play_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
+	tdm->play_dma_data.maxburst = 16;
+
+	tdm->capture_dma_data.addr = JH7110_TDM_FIFO;
+	tdm->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
+	tdm->capture_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
+	tdm->capture_dma_data.maxburst = 8;
+}
+
+static int jh7110_tdm_clk_reset_init(struct platform_device *pdev,
+				     struct jh7110_tdm_dev *tdm)
+{
+	int i, ret;
+
+	for (i = 0; i < tdm->num_clks; i++) {
+		tdm->clks[i] = devm_clk_get(&pdev->dev, tdm->clk_names[i]);
+		if (IS_ERR(tdm->clks[i])) {
+			dev_err(&pdev->dev, "Failed to get clock: %s\n",
+				tdm->clk_names[i]);
+			return PTR_ERR(tdm->clks[i]);
+		}
+	}
+
+	tdm->resets = devm_reset_control_array_get_exclusive(&pdev->dev);
+	if (IS_ERR(tdm->resets)) {
+		ret = PTR_ERR(tdm->resets);
+		dev_err(&pdev->dev, "Failed to get tdm resets");
+		return ret;
+	}
+
+	return jh7110_tdm_clk_enable(tdm);
+}
+
+static int jh7110_tdm_probe(struct platform_device *pdev)
+{
+	struct jh7110_tdm_dev *tdm;
+	const struct starfive_tdm_driverdata *driver_data;
+	int ret;
+
+	tdm = devm_kzalloc(&pdev->dev, sizeof(*tdm), GFP_KERNEL);
+	if (!tdm)
+		return -ENOMEM;
+
+	driver_data = of_device_get_match_data(&pdev->dev);
+	tdm->clk_names = (const char **)driver_data->clk_names;
+	tdm->num_clks = driver_data->num_clks;
+
+	tdm->tdm_base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(tdm->tdm_base))
+		return PTR_ERR(tdm->tdm_base);
+
+	tdm->dev = &pdev->dev;
+
+	ret = jh7110_tdm_clk_reset_init(pdev, tdm);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to enable audio-tdm clock\n");
+		return ret;
+	}
+
+	jh7110_tdm_init_params(tdm);
+
+	dev_set_drvdata(&pdev->dev, tdm);
+	ret = devm_snd_soc_register_component(&pdev->dev, &jh7110_tdm_component,
+					      &jh7110_tdm_dai, 1);
+	if (ret != 0) {
+		dev_err(&pdev->dev, "Failed to register dai\n");
+		return ret;
+	}
+
+	ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
+					      &jh7110_dmaengine_pcm_config,
+					      SND_DMAENGINE_PCM_FLAG_COMPAT);
+	if (ret) {
+		dev_err(&pdev->dev, "Could not register pcm: %d\n", ret);
+		return ret;
+	}
+
+	pm_runtime_enable(&pdev->dev);
+#ifdef CONFIG_PM
+	jh7110_tdm_clk_disable(tdm);
+#endif
+
+	return 0;
+}
+
+static int jh7110_tdm_dev_remove(struct platform_device *pdev)
+{
+	pm_runtime_disable(&pdev->dev);
+	return 0;
+}
+
+static const struct starfive_tdm_driverdata jh7110_drvdata = {
+	.clk_names = {"mclk_inner", "tdm_ahb", "tdm_apb", "tdm_internal", "tdm_ext", "tdm"},
+	.num_clks = 6,
+};
+
+static const struct of_device_id jh7110_tdm_of_match[] = {
+	{ .compatible = "starfive,jh7110-tdm", .data = &jh7110_drvdata },
+	{}
+};
+
+MODULE_DEVICE_TABLE(of, jh7110_tdm_of_match);
+
+static const struct dev_pm_ops jh7110_tdm_pm_ops = {
+	SET_RUNTIME_PM_OPS(jh7110_tdm_runtime_suspend,
+			   jh7110_tdm_runtime_resume, NULL)
+};
+
+static struct platform_driver jh7110_tdm_driver = {
+	.driver = {
+		.name = "jh7110-tdm",
+		.of_match_table = jh7110_tdm_of_match,
+		.pm = &jh7110_tdm_pm_ops,
+	},
+	.probe = jh7110_tdm_probe,
+	.remove = jh7110_tdm_dev_remove,
+};
+module_platform_driver(jh7110_tdm_driver);
+
+MODULE_DESCRIPTION("StarFive JH7110 TDM ASoC Driver");
+MODULE_AUTHOR("Walker Chen <walker.chen@starfivetech.com>");
+MODULE_LICENSE("GPL");
diff --git a/sound/soc/starfive/jh7110_tdm.h b/sound/soc/starfive/jh7110_tdm.h
new file mode 100644
index 000000000000..80f17946a45d
--- /dev/null
+++ b/sound/soc/starfive/jh7110_tdm.h
@@ -0,0 +1,147 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * TDM driver for the StarFive JH7110 SoC
+ *
+ * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
+ *
+ * Author: Walker Chen <walker.chen@starfivetech.com>
+ */
+
+#ifndef __SND_SOC_STARFIVE_TDM_H
+#define __SND_SOC_STARFIVE_TDM_H
+
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/types.h>
+#include <sound/dmaengine_pcm.h>
+#include <sound/pcm.h>
+#include <linux/dmaengine.h>
+#include <linux/types.h>
+
+#define TDM_PCMGBCR			0x00
+	#define PCMGBCR_MASK		0x1e
+	#define PCMGBCR_ENABLE		BIT(0)
+	#define PCMGBCR_TRITXEN		BIT(4)
+	#define CLKPOL_BIT		5
+	#define TRITXEN_BIT		4
+	#define ELM_BIT			3
+	#define SYNCM_BIT		2
+	#define MS_BIT			1
+#define TDM_PCMTXCR			0x04
+	#define PCMTXCR_TXEN		BIT(0)
+	#define IFL_BIT			11
+	#define WL_BIT			8
+	#define SSCALE_BIT		4
+	#define SL_BIT			2
+	#define LRJ_BIT			1
+#define TDM_PCMRXCR			0x08
+	#define PCMRXCR_RXEN		BIT(0)
+	#define PCMRXCR_RXSL_MASK	0xc
+	#define PCMRXCR_RXSL_16BIT	0x4
+	#define PCMRXCR_RXSL_32BIT	0x8
+	#define PCMRXCR_SCALE_MASK	0xf0
+	#define PCMRXCR_SCALE_1CH	0x10
+#define TDM_PCMDIV			0x0c
+
+/*  DMA registers */
+#define JH7110_TDM_FIFO			0x170c0000
+#define JH7110_TDM_FIFO_DEPTH		32
+#define JH7110_TDM_MAX_CLOCKS		6
+
+enum TDM_MASTER_SLAVE_MODE {
+	TDM_AS_MASTER = 0,
+	TDM_AS_SLAVE,
+};
+
+enum TDM_CLKPOL {
+	/* tx raising and rx falling */
+	TDM_TX_RASING_RX_FALLING = 0,
+	/* tx falling and rx raising */
+	TDM_TX_FALLING_RX_RASING,
+};
+
+enum TDM_ELM {
+	/* only work while SYNCM=0 */
+	TDM_ELM_LATE = 0,
+	TDM_ELM_EARLY,
+};
+
+enum TDM_SYNCM {
+	/* short frame sync */
+	TDM_SYNCM_SHORT = 0,
+	/* long frame sync */
+	TDM_SYNCM_LONG,
+};
+
+enum TDM_IFL {
+	/* FIFO to send or received : half-1/2, Quarter-1/4 */
+	TDM_FIFO_HALF = 0,
+	TDM_FIFO_QUARTER,
+};
+
+enum TDM_WL {
+	/* send or received word length */
+	TDM_8BIT_WORD_LEN = 0,
+	TDM_16BIT_WORD_LEN,
+	TDM_20BIT_WORD_LEN,
+	TDM_24BIT_WORD_LEN,
+	TDM_32BIT_WORD_LEN,
+};
+
+enum TDM_SL {
+	/* send or received slot length */
+	TDM_8BIT_SLOT_LEN = 0,
+	TDM_16BIT_SLOT_LEN,
+	TDM_32BIT_SLOT_LEN,
+};
+
+enum TDM_LRJ {
+	/* left-justify or right-justify */
+	TDM_RIGHT_JUSTIFY = 0,
+	TDM_LEFT_JUSTIFT,
+};
+
+struct tdm_chan_cfg {
+	enum TDM_IFL ifl;
+	enum TDM_WL  wl;
+	unsigned char sscale;
+	enum TDM_SL  sl;
+	enum TDM_LRJ lrj;
+	unsigned char enable;
+};
+
+struct starfive_tdm_driverdata {
+	const char *clk_names[JH7110_TDM_MAX_CLOCKS];
+	int num_clks;
+};
+
+struct jh7110_tdm_dev {
+	void __iomem *tdm_base;
+	struct device *dev;
+	const char **clk_names;
+	struct clk *clks[JH7110_TDM_MAX_CLOCKS];
+	int num_clks;
+	struct reset_control *resets;
+
+	enum TDM_CLKPOL clkpolity;
+	enum TDM_ELM	elm;
+	enum TDM_SYNCM	syncm;
+	enum TDM_MASTER_SLAVE_MODE ms_mode;
+
+	struct tdm_chan_cfg tx;
+	struct tdm_chan_cfg rx;
+
+	u16 syncdiv;
+	u32 samplerate;
+	u32 pcmclk;
+
+	/* data related to DMA transfers b/w tdm and DMAC */
+	struct snd_dmaengine_dai_dma_data play_dma_data;
+	struct snd_dmaengine_dai_dma_data capture_dma_data;
+	u32 saved_pcmgbcr;
+	u32 saved_pcmtxcr;
+	u32 saved_pcmrxcr;
+	u32 saved_pcmdiv;
+};
+
+#endif	/* __SND_SOC_STARFIVE_TDM_H */
-- 
2.17.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
@ 2023-05-06  9:01   ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-06  9:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, Walker Chen
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

Add tdm driver support for the StarFive JH7110 SoC.

Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
---
 MAINTAINERS                     |   6 +
 sound/soc/Kconfig               |   1 +
 sound/soc/Makefile              |   1 +
 sound/soc/starfive/Kconfig      |  15 +
 sound/soc/starfive/Makefile     |   2 +
 sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
 sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
 7 files changed, 745 insertions(+)
 create mode 100644 sound/soc/starfive/Kconfig
 create mode 100644 sound/soc/starfive/Makefile
 create mode 100644 sound/soc/starfive/jh7110_tdm.c
 create mode 100644 sound/soc/starfive/jh7110_tdm.h

diff --git a/MAINTAINERS b/MAINTAINERS
index 5f9c544bc189..add89615d327 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -19945,6 +19945,12 @@ F:	Documentation/devicetree/bindings/power/starfive*
 F:	drivers/soc/starfive/jh71xx_pmu.c
 F:	include/dt-bindings/power/starfive,jh7110-pmu.h
 
+STARFIVE JH7110 TDM DRIVERS
+M:	Walker Chen <walker.chen@starfivetech.com>
+S:	Maintained
+F:	Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
+F:	sound/soc/starfive/jh7110-tdm.*
+
 STARFIVE SOC DRIVERS
 M:	Conor Dooley <conor@kernel.org>
 S:	Maintained
diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
index 848fbae26c3b..8d1d9401ecf2 100644
--- a/sound/soc/Kconfig
+++ b/sound/soc/Kconfig
@@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
 source "sound/soc/sof/Kconfig"
 source "sound/soc/spear/Kconfig"
 source "sound/soc/sprd/Kconfig"
+source "sound/soc/starfive/Kconfig"
 source "sound/soc/sti/Kconfig"
 source "sound/soc/stm/Kconfig"
 source "sound/soc/sunxi/Kconfig"
diff --git a/sound/soc/Makefile b/sound/soc/Makefile
index 507eaed1d6a1..65aeb4ef4068 100644
--- a/sound/soc/Makefile
+++ b/sound/soc/Makefile
@@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC)	+= sh/
 obj-$(CONFIG_SND_SOC)	+= sof/
 obj-$(CONFIG_SND_SOC)	+= spear/
 obj-$(CONFIG_SND_SOC)	+= sprd/
+obj-$(CONFIG_SND_SOC)	+= starfive/
 obj-$(CONFIG_SND_SOC)	+= sti/
 obj-$(CONFIG_SND_SOC)	+= stm/
 obj-$(CONFIG_SND_SOC)	+= sunxi/
diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
new file mode 100644
index 000000000000..737c956f7b93
--- /dev/null
+++ b/sound/soc/starfive/Kconfig
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config SND_SOC_STARFIVE
+	tristate "Audio support for StarFive SoC"
+	depends on COMPILE_TEST || SOC_STARFIVE
+	help
+	  Say Y or M if you want to add support for codecs attached to
+	  the Starfive SoCs' Audio interfaces. You will also need to
+	  select the audio interfaces to support below.
+
+config SND_SOC_JH7110_TDM
+	tristate "JH7110 TDM device driver"
+	depends on HAVE_CLK && SND_SOC_STARFIVE
+	select SND_SOC_GENERIC_DMAENGINE_PCM
+	help
+	  Say Y or M if you want to add support for StarFive TDM driver.
diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
new file mode 100644
index 000000000000..f7d960211d72
--- /dev/null
+++ b/sound/soc/starfive/Makefile
@@ -0,0 +1,2 @@
+# StarFive Platform Support
+obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
new file mode 100644
index 000000000000..33f7cf43e4bd
--- /dev/null
+++ b/sound/soc/starfive/jh7110_tdm.c
@@ -0,0 +1,573 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * jh7110_tdm.c -- StarFive JH7110 TDM driver
+ *
+ * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
+ *
+ * Author: Walker Chen <walker.chen@starfivetech.com>
+ */
+
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/of_irq.h>
+#include <linux/of_platform.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/reset.h>
+#include <sound/initval.h>
+#include <sound/pcm_params.h>
+#include <sound/soc.h>
+#include <sound/soc-dai.h>
+#include "jh7110_tdm.h"
+
+static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
+{
+	return readl_relaxed(tdm->tdm_base + reg);
+}
+
+static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
+{
+	writel_relaxed(val, tdm->tdm_base + reg);
+}
+
+static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
+				    struct snd_pcm_substream *substream)
+{
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
+	else
+		tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
+}
+
+static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
+			     struct snd_pcm_substream *substream)
+{
+	u32 data;
+
+	data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
+	jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
+
+	/* restore context */
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
+	else
+		jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
+}
+
+static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
+			    struct snd_pcm_substream *substream)
+{
+	unsigned int val;
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
+		val &= ~PCMTXCR_TXEN;
+		jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
+	} else {
+		val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
+		val &= ~PCMRXCR_RXEN;
+		jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
+	}
+}
+
+static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
+{
+	u32 sl, sscale, syncdiv;
+
+	if (tdm->rx.sl >= tdm->tx.sl)
+		sl = tdm->rx.sl;
+	else
+		sl = tdm->tx.sl;
+
+	if (tdm->rx.sscale >= tdm->tx.sscale)
+		sscale = tdm->rx.sscale;
+	else
+		sscale = tdm->tx.sscale;
+
+	syncdiv = tdm->pcmclk / tdm->samplerate - 1;
+
+	if ((syncdiv + 1) < (sl * sscale)) {
+		dev_err(tdm->dev, "Failed to set syncdiv!\n");
+		return -EINVAL;
+	}
+
+	if (tdm->syncm == TDM_SYNCM_LONG &&
+	    (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
+		if ((syncdiv + 1) <= sl) {
+			dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
+			return -EINVAL;
+		}
+	}
+
+	jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
+	return 0;
+}
+
+static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
+			     struct snd_pcm_substream *substream)
+{
+	u32 datarx, datatx;
+	int ret;
+
+	ret = jh7110_tdm_syncdiv(tdm);
+	if (ret)
+		return ret;
+
+	datarx = (tdm->rx.ifl << IFL_BIT) |
+		  (tdm->rx.wl << WL_BIT) |
+		  (tdm->rx.sscale << SSCALE_BIT) |
+		  (tdm->rx.sl << SL_BIT) |
+		  (tdm->rx.lrj << LRJ_BIT);
+
+	datatx = (tdm->tx.ifl << IFL_BIT) |
+		  (tdm->tx.wl << WL_BIT) |
+		  (tdm->tx.sscale << SSCALE_BIT) |
+		  (tdm->tx.sl << SL_BIT) |
+		  (tdm->tx.lrj << LRJ_BIT);
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
+	else
+		jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
+
+	return 0;
+}
+
+static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
+{
+	int i;
+
+	for (i = tdm->num_clks - 1; i >= 0; i--)
+		clk_disable_unprepare(tdm->clks[i]);
+}
+
+static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
+{
+	int i, ret;
+
+	for (i = 0; i < tdm->num_clks; i++) {
+		ret = clk_prepare_enable(tdm->clks[i]);
+		if (ret) {
+			while (i-- > 0)
+				clk_disable_unprepare(tdm->clks[i]);
+			return ret;
+		}
+	}
+
+	ret = reset_control_deassert(tdm->resets);
+	if (ret) {
+		dev_err(tdm->dev, "Failed to deassert tdm resets\n");
+		goto dis_tdm_clk;
+	}
+
+	/* select tdm_ext clock as the clock source for tdm */
+	ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
+	if (ret) {
+		dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
+		goto dis_tdm_clk;
+	}
+	return 0;
+
+dis_tdm_clk:
+	for (i = tdm->num_clks - 1; i >= 0; i--)
+		clk_disable_unprepare(tdm->clks[i]);
+
+	return ret;
+}
+
+#ifdef CONFIG_PM
+static int jh7110_tdm_runtime_suspend(struct device *dev)
+{
+	struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
+
+	jh7110_tdm_clk_disable(tdm);
+	return 0;
+}
+
+static int jh7110_tdm_runtime_resume(struct device *dev)
+{
+	struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
+
+	return jh7110_tdm_clk_enable(tdm);
+}
+#endif
+
+#ifdef CONFIG_PM_SLEEP
+static int jh7110_tdm_suspend(struct snd_soc_component *component)
+{
+	/* save context */
+	tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
+	tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);
+
+	return pm_runtime_force_suspend(component->dev);
+}
+
+static int jh7110_tdm_resume(struct snd_soc_component *component)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_component_get_drvdata(component);
+
+	/* restore context */
+	jh7110_tdm_writel(tdm, TDM_PCMGBCR, tdm->saved_pcmgbcr);
+	jh7110_tdm_writel(tdm, TDM_PCMDIV, tdm->saved_pcmdiv);
+
+	return pm_runtime_force_resume(component->dev);
+}
+
+#else
+#define jh7110_tdm_suspend	NULL
+#define jh7110_tdm_resume	NULL
+#endif
+
+static const struct snd_soc_component_driver jh7110_tdm_component = {
+	.name = "jh7110-tdm",
+	.suspend = jh7110_tdm_suspend,
+	.resume	= jh7110_tdm_resume,
+};
+
+static int jh7110_tdm_startup(struct snd_pcm_substream *substream,
+			      struct snd_soc_dai *cpu_dai)
+{
+	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
+	struct snd_soc_dai_link *dai_link = rtd->dai_link;
+
+	dai_link->stop_dma_first = 1;
+
+	return 0;
+}
+
+static int jh7110_tdm_hw_params(struct snd_pcm_substream *substream,
+				struct snd_pcm_hw_params *params,
+				struct snd_soc_dai *dai)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
+	int chan_wl, chan_sl, chan_nr;
+	unsigned int data_width;
+	unsigned int dma_bus_width;
+	struct snd_dmaengine_dai_dma_data *dma_data = NULL;
+	int ret = 0;
+
+	data_width = params_width(params);
+
+	tdm->samplerate = params_rate(params);
+	tdm->pcmclk = params_channels(params) * tdm->samplerate * data_width;
+
+	switch (params_format(params)) {
+	case SNDRV_PCM_FORMAT_S16_LE:
+		chan_wl = TDM_16BIT_WORD_LEN;
+		chan_sl = TDM_16BIT_SLOT_LEN;
+		dma_bus_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
+		break;
+
+	case SNDRV_PCM_FORMAT_S32_LE:
+		chan_wl = TDM_32BIT_WORD_LEN;
+		chan_sl = TDM_32BIT_SLOT_LEN;
+		dma_bus_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
+		break;
+
+	default:
+		dev_err(tdm->dev, "tdm: unsupported PCM fmt");
+		return -EINVAL;
+	}
+
+	chan_nr = params_channels(params);
+	switch (chan_nr) {
+	case 1:
+	case 2:
+	case 4:
+	case 6:
+	case 8:
+		break;
+	default:
+		dev_err(tdm->dev, "channel not supported\n");
+		return -EINVAL;
+	}
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		tdm->tx.wl = chan_wl;
+		tdm->tx.sl = chan_sl;
+		tdm->tx.sscale = chan_nr;
+		tdm->play_dma_data.addr_width = dma_bus_width;
+		dma_data = &tdm->play_dma_data;
+	} else {
+		tdm->rx.wl = chan_wl;
+		tdm->rx.sl = chan_sl;
+		tdm->rx.sscale = chan_nr;
+		tdm->capture_dma_data.addr_width = dma_bus_width;
+		dma_data = &tdm->capture_dma_data;
+	}
+
+	snd_soc_dai_set_dma_data(dai, substream, dma_data);
+
+	ret = jh7110_tdm_config(tdm, substream);
+	if (ret)
+		return ret;
+
+	jh7110_tdm_save_context(tdm, substream);
+	return 0;
+}
+
+static int jh7110_tdm_trigger(struct snd_pcm_substream *substream,
+			      int cmd, struct snd_soc_dai *dai)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
+	int ret = 0;
+
+	switch (cmd) {
+	case SNDRV_PCM_TRIGGER_START:
+	case SNDRV_PCM_TRIGGER_RESUME:
+	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+		jh7110_tdm_start(tdm, substream);
+		break;
+
+	case SNDRV_PCM_TRIGGER_STOP:
+	case SNDRV_PCM_TRIGGER_SUSPEND:
+	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
+		jh7110_tdm_stop(tdm, substream);
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+	return ret;
+}
+
+static int jh7110_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
+				  unsigned int fmt)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(cpu_dai);
+	unsigned int gbcr;
+	int ret = 0;
+
+	/* set master/slave audio interface */
+	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
+	case SND_SOC_DAIFMT_BP_FP:
+		/* cpu is master */
+		tdm->ms_mode = TDM_AS_MASTER;
+		break;
+	case SND_SOC_DAIFMT_BC_FC:
+		/* codec is master */
+		tdm->ms_mode = TDM_AS_SLAVE;
+		break;
+	case SND_SOC_DAIFMT_BC_FP:
+	case SND_SOC_DAIFMT_BP_FC:
+		ret = -EINVAL;
+		break;
+	default:
+		dev_dbg(tdm->dev, "dwc : Invalid clock provider format\n");
+		ret = -EINVAL;
+		break;
+	}
+
+	gbcr = (tdm->clkpolity << CLKPOL_BIT) |
+		(tdm->elm << ELM_BIT) |
+		(tdm->syncm << SYNCM_BIT) |
+		(tdm->ms_mode << MS_BIT);
+	jh7110_tdm_writel(tdm, TDM_PCMGBCR, gbcr);
+
+	return ret;
+}
+
+static const struct snd_soc_dai_ops jh7110_tdm_dai_ops = {
+	.startup	= jh7110_tdm_startup,
+	.hw_params	= jh7110_tdm_hw_params,
+	.trigger	= jh7110_tdm_trigger,
+	.set_fmt	= jh7110_tdm_set_dai_fmt,
+};
+
+static int jh7110_tdm_dai_probe(struct snd_soc_dai *dai)
+{
+	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
+
+	snd_soc_dai_init_dma_data(dai, &tdm->play_dma_data, &tdm->capture_dma_data);
+	snd_soc_dai_set_drvdata(dai, tdm);
+	return 0;
+}
+
+#define JH7110_TDM_RATES	SNDRV_PCM_RATE_8000_48000
+
+#define JH7110_TDM_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | \
+				 SNDRV_PCM_FMTBIT_S32_LE)
+
+static struct snd_soc_dai_driver jh7110_tdm_dai = {
+	.name = "sf_tdm",
+	.id = 0,
+	.playback = {
+		.stream_name    = "Playback",
+		.channels_min   = 1,
+		.channels_max   = 8,
+		.rates          = JH7110_TDM_RATES,
+		.formats        = JH7110_TDM_FORMATS,
+	},
+	.capture = {
+		.stream_name    = "Capture",
+		.channels_min   = 1,
+		.channels_max   = 8,
+		.rates          = JH7110_TDM_RATES,
+		.formats        = JH7110_TDM_FORMATS,
+	},
+	.ops = &jh7110_tdm_dai_ops,
+	.probe = jh7110_tdm_dai_probe,
+	.symmetric_rate = 1,
+};
+
+static const struct snd_pcm_hardware jh7110_pcm_hardware = {
+	.info			= (SNDRV_PCM_INFO_MMAP		|
+				   SNDRV_PCM_INFO_MMAP_VALID	|
+				   SNDRV_PCM_INFO_PAUSE		|
+				   SNDRV_PCM_INFO_RESUME	|
+				   SNDRV_PCM_INFO_INTERLEAVED	|
+				   SNDRV_PCM_INFO_BLOCK_TRANSFER),
+	.buffer_bytes_max	= 192512,
+	.period_bytes_min	= 4096,
+	.period_bytes_max	= 32768,
+	.periods_min		= 1,
+	.periods_max		= 48,
+	.fifo_size		= 16,
+};
+
+static const struct snd_dmaengine_pcm_config jh7110_dmaengine_pcm_config = {
+	.pcm_hardware = &jh7110_pcm_hardware,
+	.prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
+	.prealloc_buffer_size = 192512,
+};
+
+static void jh7110_tdm_init_params(struct jh7110_tdm_dev *tdm)
+{
+	tdm->clkpolity = TDM_TX_RASING_RX_FALLING;
+	tdm->elm = TDM_ELM_LATE;
+	tdm->syncm = TDM_SYNCM_SHORT;
+
+	tdm->rx.ifl = TDM_FIFO_HALF;
+	tdm->tx.ifl = TDM_FIFO_HALF;
+	tdm->rx.wl = TDM_16BIT_WORD_LEN;
+	tdm->tx.wl = TDM_16BIT_WORD_LEN;
+	tdm->rx.sscale = 2;
+	tdm->tx.sscale = 2;
+	tdm->rx.lrj = TDM_LEFT_JUSTIFT;
+	tdm->tx.lrj = TDM_LEFT_JUSTIFT;
+
+	tdm->play_dma_data.addr = JH7110_TDM_FIFO;
+	tdm->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
+	tdm->play_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
+	tdm->play_dma_data.maxburst = 16;
+
+	tdm->capture_dma_data.addr = JH7110_TDM_FIFO;
+	tdm->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
+	tdm->capture_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
+	tdm->capture_dma_data.maxburst = 8;
+}
+
+static int jh7110_tdm_clk_reset_init(struct platform_device *pdev,
+				     struct jh7110_tdm_dev *tdm)
+{
+	int i, ret;
+
+	for (i = 0; i < tdm->num_clks; i++) {
+		tdm->clks[i] = devm_clk_get(&pdev->dev, tdm->clk_names[i]);
+		if (IS_ERR(tdm->clks[i])) {
+			dev_err(&pdev->dev, "Failed to get clock: %s\n",
+				tdm->clk_names[i]);
+			return PTR_ERR(tdm->clks[i]);
+		}
+	}
+
+	tdm->resets = devm_reset_control_array_get_exclusive(&pdev->dev);
+	if (IS_ERR(tdm->resets)) {
+		ret = PTR_ERR(tdm->resets);
+		dev_err(&pdev->dev, "Failed to get tdm resets");
+		return ret;
+	}
+
+	return jh7110_tdm_clk_enable(tdm);
+}
+
+static int jh7110_tdm_probe(struct platform_device *pdev)
+{
+	struct jh7110_tdm_dev *tdm;
+	const struct starfive_tdm_driverdata *driver_data;
+	int ret;
+
+	tdm = devm_kzalloc(&pdev->dev, sizeof(*tdm), GFP_KERNEL);
+	if (!tdm)
+		return -ENOMEM;
+
+	driver_data = of_device_get_match_data(&pdev->dev);
+	tdm->clk_names = (const char **)driver_data->clk_names;
+	tdm->num_clks = driver_data->num_clks;
+
+	tdm->tdm_base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(tdm->tdm_base))
+		return PTR_ERR(tdm->tdm_base);
+
+	tdm->dev = &pdev->dev;
+
+	ret = jh7110_tdm_clk_reset_init(pdev, tdm);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to enable audio-tdm clock\n");
+		return ret;
+	}
+
+	jh7110_tdm_init_params(tdm);
+
+	dev_set_drvdata(&pdev->dev, tdm);
+	ret = devm_snd_soc_register_component(&pdev->dev, &jh7110_tdm_component,
+					      &jh7110_tdm_dai, 1);
+	if (ret != 0) {
+		dev_err(&pdev->dev, "Failed to register dai\n");
+		return ret;
+	}
+
+	ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
+					      &jh7110_dmaengine_pcm_config,
+					      SND_DMAENGINE_PCM_FLAG_COMPAT);
+	if (ret) {
+		dev_err(&pdev->dev, "Could not register pcm: %d\n", ret);
+		return ret;
+	}
+
+	pm_runtime_enable(&pdev->dev);
+#ifdef CONFIG_PM
+	jh7110_tdm_clk_disable(tdm);
+#endif
+
+	return 0;
+}
+
+static int jh7110_tdm_dev_remove(struct platform_device *pdev)
+{
+	pm_runtime_disable(&pdev->dev);
+	return 0;
+}
+
+static const struct starfive_tdm_driverdata jh7110_drvdata = {
+	.clk_names = {"mclk_inner", "tdm_ahb", "tdm_apb", "tdm_internal", "tdm_ext", "tdm"},
+	.num_clks = 6,
+};
+
+static const struct of_device_id jh7110_tdm_of_match[] = {
+	{ .compatible = "starfive,jh7110-tdm", .data = &jh7110_drvdata },
+	{}
+};
+
+MODULE_DEVICE_TABLE(of, jh7110_tdm_of_match);
+
+static const struct dev_pm_ops jh7110_tdm_pm_ops = {
+	SET_RUNTIME_PM_OPS(jh7110_tdm_runtime_suspend,
+			   jh7110_tdm_runtime_resume, NULL)
+};
+
+static struct platform_driver jh7110_tdm_driver = {
+	.driver = {
+		.name = "jh7110-tdm",
+		.of_match_table = jh7110_tdm_of_match,
+		.pm = &jh7110_tdm_pm_ops,
+	},
+	.probe = jh7110_tdm_probe,
+	.remove = jh7110_tdm_dev_remove,
+};
+module_platform_driver(jh7110_tdm_driver);
+
+MODULE_DESCRIPTION("StarFive JH7110 TDM ASoC Driver");
+MODULE_AUTHOR("Walker Chen <walker.chen@starfivetech.com>");
+MODULE_LICENSE("GPL");
diff --git a/sound/soc/starfive/jh7110_tdm.h b/sound/soc/starfive/jh7110_tdm.h
new file mode 100644
index 000000000000..80f17946a45d
--- /dev/null
+++ b/sound/soc/starfive/jh7110_tdm.h
@@ -0,0 +1,147 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * TDM driver for the StarFive JH7110 SoC
+ *
+ * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
+ *
+ * Author: Walker Chen <walker.chen@starfivetech.com>
+ */
+
+#ifndef __SND_SOC_STARFIVE_TDM_H
+#define __SND_SOC_STARFIVE_TDM_H
+
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/types.h>
+#include <sound/dmaengine_pcm.h>
+#include <sound/pcm.h>
+#include <linux/dmaengine.h>
+#include <linux/types.h>
+
+#define TDM_PCMGBCR			0x00
+	#define PCMGBCR_MASK		0x1e
+	#define PCMGBCR_ENABLE		BIT(0)
+	#define PCMGBCR_TRITXEN		BIT(4)
+	#define CLKPOL_BIT		5
+	#define TRITXEN_BIT		4
+	#define ELM_BIT			3
+	#define SYNCM_BIT		2
+	#define MS_BIT			1
+#define TDM_PCMTXCR			0x04
+	#define PCMTXCR_TXEN		BIT(0)
+	#define IFL_BIT			11
+	#define WL_BIT			8
+	#define SSCALE_BIT		4
+	#define SL_BIT			2
+	#define LRJ_BIT			1
+#define TDM_PCMRXCR			0x08
+	#define PCMRXCR_RXEN		BIT(0)
+	#define PCMRXCR_RXSL_MASK	0xc
+	#define PCMRXCR_RXSL_16BIT	0x4
+	#define PCMRXCR_RXSL_32BIT	0x8
+	#define PCMRXCR_SCALE_MASK	0xf0
+	#define PCMRXCR_SCALE_1CH	0x10
+#define TDM_PCMDIV			0x0c
+
+/*  DMA registers */
+#define JH7110_TDM_FIFO			0x170c0000
+#define JH7110_TDM_FIFO_DEPTH		32
+#define JH7110_TDM_MAX_CLOCKS		6
+
+enum TDM_MASTER_SLAVE_MODE {
+	TDM_AS_MASTER = 0,
+	TDM_AS_SLAVE,
+};
+
+enum TDM_CLKPOL {
+	/* tx raising and rx falling */
+	TDM_TX_RASING_RX_FALLING = 0,
+	/* tx falling and rx raising */
+	TDM_TX_FALLING_RX_RASING,
+};
+
+enum TDM_ELM {
+	/* only work while SYNCM=0 */
+	TDM_ELM_LATE = 0,
+	TDM_ELM_EARLY,
+};
+
+enum TDM_SYNCM {
+	/* short frame sync */
+	TDM_SYNCM_SHORT = 0,
+	/* long frame sync */
+	TDM_SYNCM_LONG,
+};
+
+enum TDM_IFL {
+	/* FIFO to send or received : half-1/2, Quarter-1/4 */
+	TDM_FIFO_HALF = 0,
+	TDM_FIFO_QUARTER,
+};
+
+enum TDM_WL {
+	/* send or received word length */
+	TDM_8BIT_WORD_LEN = 0,
+	TDM_16BIT_WORD_LEN,
+	TDM_20BIT_WORD_LEN,
+	TDM_24BIT_WORD_LEN,
+	TDM_32BIT_WORD_LEN,
+};
+
+enum TDM_SL {
+	/* send or received slot length */
+	TDM_8BIT_SLOT_LEN = 0,
+	TDM_16BIT_SLOT_LEN,
+	TDM_32BIT_SLOT_LEN,
+};
+
+enum TDM_LRJ {
+	/* left-justify or right-justify */
+	TDM_RIGHT_JUSTIFY = 0,
+	TDM_LEFT_JUSTIFT,
+};
+
+struct tdm_chan_cfg {
+	enum TDM_IFL ifl;
+	enum TDM_WL  wl;
+	unsigned char sscale;
+	enum TDM_SL  sl;
+	enum TDM_LRJ lrj;
+	unsigned char enable;
+};
+
+struct starfive_tdm_driverdata {
+	const char *clk_names[JH7110_TDM_MAX_CLOCKS];
+	int num_clks;
+};
+
+struct jh7110_tdm_dev {
+	void __iomem *tdm_base;
+	struct device *dev;
+	const char **clk_names;
+	struct clk *clks[JH7110_TDM_MAX_CLOCKS];
+	int num_clks;
+	struct reset_control *resets;
+
+	enum TDM_CLKPOL clkpolity;
+	enum TDM_ELM	elm;
+	enum TDM_SYNCM	syncm;
+	enum TDM_MASTER_SLAVE_MODE ms_mode;
+
+	struct tdm_chan_cfg tx;
+	struct tdm_chan_cfg rx;
+
+	u16 syncdiv;
+	u32 samplerate;
+	u32 pcmclk;
+
+	/* data related to DMA transfers b/w tdm and DMAC */
+	struct snd_dmaengine_dai_dma_data play_dma_data;
+	struct snd_dmaengine_dai_dma_data capture_dma_data;
+	u32 saved_pcmgbcr;
+	u32 saved_pcmtxcr;
+	u32 saved_pcmrxcr;
+	u32 saved_pcmdiv;
+};
+
+#endif	/* __SND_SOC_STARFIVE_TDM_H */
-- 
2.17.1


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

* [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
  2023-05-06  9:01 ` Walker Chen
  (?)
@ 2023-05-06  9:01   ` Walker Chen
  -1 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-06  9:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, Walker Chen
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

Add the tdm controller node and sound card for the StarFive JH7110 SoC.

Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
---
 arch/riscv/boot/dts/starfive/Makefile         |  3 +
 .../jh7110-starfive-visionfive-2-wm8960.dtso  | 70 +++++++++++++++++++
 .../jh7110-starfive-visionfive-2.dtsi         | 40 +++++++++++
 arch/riscv/boot/dts/starfive/jh7110.dtsi      | 21 ++++++
 4 files changed, 134 insertions(+)
 create mode 100644 arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso

diff --git a/arch/riscv/boot/dts/starfive/Makefile b/arch/riscv/boot/dts/starfive/Makefile
index 170956846d49..644cc29b5be3 100644
--- a/arch/riscv/boot/dts/starfive/Makefile
+++ b/arch/riscv/boot/dts/starfive/Makefile
@@ -4,3 +4,6 @@ dtb-$(CONFIG_ARCH_STARFIVE) += jh7100-starfive-visionfive-v1.dtb
 
 dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-v1.2a.dtb
 dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-v1.3b.dtb
+
+jh7110-starfive-visionfive-2-wm8960-dtbs := jh7110-starfive-visionfive-2-v1.3b.dtb jh7110-starfive-visionfive-2-wm8960.dtbo
+dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-wm8960.dtb
diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
new file mode 100644
index 000000000000..67897f000883
--- /dev/null
+++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+/*
+ * Copyright (C) 2023 StarFive Technology Co., Ltd.
+ * Author: Walker Chen <walker.chen@starfivetech.com>
+ *
+ * Requires ReSpeaker 2-Mics Pi HAT plugged in 40-pin GPIO header.
+ */
+
+/dts-v1/;
+/plugin/;
+
+&{/} {
+	compatible = "starfive,visionfive-2-v1.3b", "starfive,jh7110";
+
+	wm8960_mclk: wm8960-mclk {
+		compatible = "fixed-clock";
+		clock-output-names = "wm8960_mclk";
+		#clock-cells = <0>;
+		clock-frequency = <24576000>;
+	};
+
+	sound {
+		compatible = "simple-audio-card";
+
+		#address-cells = <1>;
+		#size-cells = <0>;
+
+		simple-audio-card,name = "Starfive-TDM-Sound-Card";
+		simple-audio-card,widgets = "Microphone", "Mic Jack",
+					    "Line", "Line In",
+					    "Line", "Line Out",
+					    "Speaker", "Speaker",
+					    "Headphone", "Headphone Jack";
+		simple-audio-card,routing = "Headphone Jack", "HP_L",
+					    "Headphone Jack", "HP_R",
+					    "Speaker", "SPK_LP",
+					    "Speaker", "SPK_LN",
+					    "LINPUT1", "Mic Jack",
+					    "LINPUT3", "Mic Jack",
+					    "RINPUT1", "Mic Jack",
+					    "RINPUT2", "Mic Jack";
+
+		simple-audio-card,dai-link@0 {
+			reg = <0>;
+			format = "dsp_a";
+			bitclock-master = <&dailink_master>;
+			frame-master = <&dailink_master>;
+
+			cpu {
+				sound-dai = <&tdm>;
+			};
+			dailink_master: codec {
+				sound-dai = <&wm8960>;
+				clocks = <&wm8960_mclk>;
+			};
+		};
+	};
+};
+
+&i2c0 {
+	#address-cells = <1>;
+	#size-cells = <0>;
+
+	wm8960: codec@1a {
+		compatible = "wlf,wm8960";
+		reg = <0x1a>;
+		wlf,shared-lrclk;
+		#sound-dai-cells = <0>;
+	};
+};
diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
index 1155b97b593d..19b5954ee72d 100644
--- a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
+++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
@@ -214,6 +214,40 @@
 			slew-rate = <0>;
 		};
 	};
+
+	tdm0_pins: tdm0-pins {
+		tdm0-pins-tx {
+			pinmux = <GPIOMUX(44, GPOUT_SYS_TDM_TXD,
+					      GPOEN_ENABLE,
+					      GPI_NONE)>;
+			bias-pull-up;
+			drive-strength = <2>;
+			input-disable;
+			input-schmitt-disable;
+			slew-rate = <0>;
+		};
+
+		tdm0-pins-rx {
+			pinmux = <GPIOMUX(61, GPOUT_HIGH,
+					      GPOEN_DISABLE,
+					      GPI_SYS_TDM_RXD)>;
+			input-enable;
+		};
+
+		tdm0-pins-sync {
+			pinmux = <GPIOMUX(63, GPOUT_HIGH,
+					      GPOEN_DISABLE,
+					      GPI_SYS_TDM_SYNC)>;
+			input-enable;
+		};
+
+		tdm0-pins-pcmclk {
+			pinmux = <GPIOMUX(38, GPOUT_HIGH,
+					      GPOEN_DISABLE,
+					      GPI_SYS_TDM_CLK)>;
+			input-enable;
+		};
+	};
 };
 
 &uart0 {
@@ -221,3 +255,9 @@
 	pinctrl-0 = <&uart0_pins>;
 	status = "okay";
 };
+
+&tdm {
+	pinctrl-names = "default";
+	pinctrl-0 = <&tdm0_pins>;
+	status = "okay";
+};
diff --git a/arch/riscv/boot/dts/starfive/jh7110.dtsi b/arch/riscv/boot/dts/starfive/jh7110.dtsi
index 866313570a7e..cfda6fb0d91b 100644
--- a/arch/riscv/boot/dts/starfive/jh7110.dtsi
+++ b/arch/riscv/boot/dts/starfive/jh7110.dtsi
@@ -366,6 +366,27 @@
 			status = "disabled";
 		};
 
+		tdm: tdm@10090000 {
+			compatible = "starfive,jh7110-tdm";
+			reg = <0x0 0x10090000 0x0 0x1000>;
+			clocks = <&syscrg JH7110_SYSCLK_TDM_AHB>,
+				 <&syscrg JH7110_SYSCLK_TDM_APB>,
+				 <&syscrg JH7110_SYSCLK_TDM_INTERNAL>,
+				 <&syscrg JH7110_SYSCLK_TDM_TDM>,
+				 <&syscrg JH7110_SYSCLK_MCLK_INNER>,
+				 <&tdm_ext>;
+			clock-names = "tdm_ahb", "tdm_apb",
+				      "tdm_internal", "tdm",
+				      "mclk_inner", "tdm_ext";
+			resets = <&syscrg JH7110_SYSRST_TDM_AHB>,
+				 <&syscrg JH7110_SYSRST_TDM_APB>,
+				 <&syscrg JH7110_SYSRST_TDM_CORE>;
+			dmas = <&dma 20>, <&dma 21>;
+			dma-names = "rx","tx";
+			#sound-dai-cells = <0>;
+			status = "disabled";
+		};
+
 		stgcrg: clock-controller@10230000 {
 			compatible = "starfive,jh7110-stgcrg";
 			reg = <0x0 0x10230000 0x0 0x10000>;
-- 
2.17.1


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

* [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
@ 2023-05-06  9:01   ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-06  9:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, Walker Chen
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

Add the tdm controller node and sound card for the StarFive JH7110 SoC.

Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
---
 arch/riscv/boot/dts/starfive/Makefile         |  3 +
 .../jh7110-starfive-visionfive-2-wm8960.dtso  | 70 +++++++++++++++++++
 .../jh7110-starfive-visionfive-2.dtsi         | 40 +++++++++++
 arch/riscv/boot/dts/starfive/jh7110.dtsi      | 21 ++++++
 4 files changed, 134 insertions(+)
 create mode 100644 arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso

diff --git a/arch/riscv/boot/dts/starfive/Makefile b/arch/riscv/boot/dts/starfive/Makefile
index 170956846d49..644cc29b5be3 100644
--- a/arch/riscv/boot/dts/starfive/Makefile
+++ b/arch/riscv/boot/dts/starfive/Makefile
@@ -4,3 +4,6 @@ dtb-$(CONFIG_ARCH_STARFIVE) += jh7100-starfive-visionfive-v1.dtb
 
 dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-v1.2a.dtb
 dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-v1.3b.dtb
+
+jh7110-starfive-visionfive-2-wm8960-dtbs := jh7110-starfive-visionfive-2-v1.3b.dtb jh7110-starfive-visionfive-2-wm8960.dtbo
+dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-wm8960.dtb
diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
new file mode 100644
index 000000000000..67897f000883
--- /dev/null
+++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+/*
+ * Copyright (C) 2023 StarFive Technology Co., Ltd.
+ * Author: Walker Chen <walker.chen@starfivetech.com>
+ *
+ * Requires ReSpeaker 2-Mics Pi HAT plugged in 40-pin GPIO header.
+ */
+
+/dts-v1/;
+/plugin/;
+
+&{/} {
+	compatible = "starfive,visionfive-2-v1.3b", "starfive,jh7110";
+
+	wm8960_mclk: wm8960-mclk {
+		compatible = "fixed-clock";
+		clock-output-names = "wm8960_mclk";
+		#clock-cells = <0>;
+		clock-frequency = <24576000>;
+	};
+
+	sound {
+		compatible = "simple-audio-card";
+
+		#address-cells = <1>;
+		#size-cells = <0>;
+
+		simple-audio-card,name = "Starfive-TDM-Sound-Card";
+		simple-audio-card,widgets = "Microphone", "Mic Jack",
+					    "Line", "Line In",
+					    "Line", "Line Out",
+					    "Speaker", "Speaker",
+					    "Headphone", "Headphone Jack";
+		simple-audio-card,routing = "Headphone Jack", "HP_L",
+					    "Headphone Jack", "HP_R",
+					    "Speaker", "SPK_LP",
+					    "Speaker", "SPK_LN",
+					    "LINPUT1", "Mic Jack",
+					    "LINPUT3", "Mic Jack",
+					    "RINPUT1", "Mic Jack",
+					    "RINPUT2", "Mic Jack";
+
+		simple-audio-card,dai-link@0 {
+			reg = <0>;
+			format = "dsp_a";
+			bitclock-master = <&dailink_master>;
+			frame-master = <&dailink_master>;
+
+			cpu {
+				sound-dai = <&tdm>;
+			};
+			dailink_master: codec {
+				sound-dai = <&wm8960>;
+				clocks = <&wm8960_mclk>;
+			};
+		};
+	};
+};
+
+&i2c0 {
+	#address-cells = <1>;
+	#size-cells = <0>;
+
+	wm8960: codec@1a {
+		compatible = "wlf,wm8960";
+		reg = <0x1a>;
+		wlf,shared-lrclk;
+		#sound-dai-cells = <0>;
+	};
+};
diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
index 1155b97b593d..19b5954ee72d 100644
--- a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
+++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
@@ -214,6 +214,40 @@
 			slew-rate = <0>;
 		};
 	};
+
+	tdm0_pins: tdm0-pins {
+		tdm0-pins-tx {
+			pinmux = <GPIOMUX(44, GPOUT_SYS_TDM_TXD,
+					      GPOEN_ENABLE,
+					      GPI_NONE)>;
+			bias-pull-up;
+			drive-strength = <2>;
+			input-disable;
+			input-schmitt-disable;
+			slew-rate = <0>;
+		};
+
+		tdm0-pins-rx {
+			pinmux = <GPIOMUX(61, GPOUT_HIGH,
+					      GPOEN_DISABLE,
+					      GPI_SYS_TDM_RXD)>;
+			input-enable;
+		};
+
+		tdm0-pins-sync {
+			pinmux = <GPIOMUX(63, GPOUT_HIGH,
+					      GPOEN_DISABLE,
+					      GPI_SYS_TDM_SYNC)>;
+			input-enable;
+		};
+
+		tdm0-pins-pcmclk {
+			pinmux = <GPIOMUX(38, GPOUT_HIGH,
+					      GPOEN_DISABLE,
+					      GPI_SYS_TDM_CLK)>;
+			input-enable;
+		};
+	};
 };
 
 &uart0 {
@@ -221,3 +255,9 @@
 	pinctrl-0 = <&uart0_pins>;
 	status = "okay";
 };
+
+&tdm {
+	pinctrl-names = "default";
+	pinctrl-0 = <&tdm0_pins>;
+	status = "okay";
+};
diff --git a/arch/riscv/boot/dts/starfive/jh7110.dtsi b/arch/riscv/boot/dts/starfive/jh7110.dtsi
index 866313570a7e..cfda6fb0d91b 100644
--- a/arch/riscv/boot/dts/starfive/jh7110.dtsi
+++ b/arch/riscv/boot/dts/starfive/jh7110.dtsi
@@ -366,6 +366,27 @@
 			status = "disabled";
 		};
 
+		tdm: tdm@10090000 {
+			compatible = "starfive,jh7110-tdm";
+			reg = <0x0 0x10090000 0x0 0x1000>;
+			clocks = <&syscrg JH7110_SYSCLK_TDM_AHB>,
+				 <&syscrg JH7110_SYSCLK_TDM_APB>,
+				 <&syscrg JH7110_SYSCLK_TDM_INTERNAL>,
+				 <&syscrg JH7110_SYSCLK_TDM_TDM>,
+				 <&syscrg JH7110_SYSCLK_MCLK_INNER>,
+				 <&tdm_ext>;
+			clock-names = "tdm_ahb", "tdm_apb",
+				      "tdm_internal", "tdm",
+				      "mclk_inner", "tdm_ext";
+			resets = <&syscrg JH7110_SYSRST_TDM_AHB>,
+				 <&syscrg JH7110_SYSRST_TDM_APB>,
+				 <&syscrg JH7110_SYSRST_TDM_CORE>;
+			dmas = <&dma 20>, <&dma 21>;
+			dma-names = "rx","tx";
+			#sound-dai-cells = <0>;
+			status = "disabled";
+		};
+
 		stgcrg: clock-controller@10230000 {
 			compatible = "starfive,jh7110-stgcrg";
 			reg = <0x0 0x10230000 0x0 0x10000>;
-- 
2.17.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
@ 2023-05-06  9:01   ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-06  9:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, Walker Chen
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

Add the tdm controller node and sound card for the StarFive JH7110 SoC.

Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
---
 arch/riscv/boot/dts/starfive/Makefile         |  3 +
 .../jh7110-starfive-visionfive-2-wm8960.dtso  | 70 +++++++++++++++++++
 .../jh7110-starfive-visionfive-2.dtsi         | 40 +++++++++++
 arch/riscv/boot/dts/starfive/jh7110.dtsi      | 21 ++++++
 4 files changed, 134 insertions(+)
 create mode 100644 arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso

diff --git a/arch/riscv/boot/dts/starfive/Makefile b/arch/riscv/boot/dts/starfive/Makefile
index 170956846d49..644cc29b5be3 100644
--- a/arch/riscv/boot/dts/starfive/Makefile
+++ b/arch/riscv/boot/dts/starfive/Makefile
@@ -4,3 +4,6 @@ dtb-$(CONFIG_ARCH_STARFIVE) += jh7100-starfive-visionfive-v1.dtb
 
 dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-v1.2a.dtb
 dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-v1.3b.dtb
+
+jh7110-starfive-visionfive-2-wm8960-dtbs := jh7110-starfive-visionfive-2-v1.3b.dtb jh7110-starfive-visionfive-2-wm8960.dtbo
+dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-wm8960.dtb
diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
new file mode 100644
index 000000000000..67897f000883
--- /dev/null
+++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+/*
+ * Copyright (C) 2023 StarFive Technology Co., Ltd.
+ * Author: Walker Chen <walker.chen@starfivetech.com>
+ *
+ * Requires ReSpeaker 2-Mics Pi HAT plugged in 40-pin GPIO header.
+ */
+
+/dts-v1/;
+/plugin/;
+
+&{/} {
+	compatible = "starfive,visionfive-2-v1.3b", "starfive,jh7110";
+
+	wm8960_mclk: wm8960-mclk {
+		compatible = "fixed-clock";
+		clock-output-names = "wm8960_mclk";
+		#clock-cells = <0>;
+		clock-frequency = <24576000>;
+	};
+
+	sound {
+		compatible = "simple-audio-card";
+
+		#address-cells = <1>;
+		#size-cells = <0>;
+
+		simple-audio-card,name = "Starfive-TDM-Sound-Card";
+		simple-audio-card,widgets = "Microphone", "Mic Jack",
+					    "Line", "Line In",
+					    "Line", "Line Out",
+					    "Speaker", "Speaker",
+					    "Headphone", "Headphone Jack";
+		simple-audio-card,routing = "Headphone Jack", "HP_L",
+					    "Headphone Jack", "HP_R",
+					    "Speaker", "SPK_LP",
+					    "Speaker", "SPK_LN",
+					    "LINPUT1", "Mic Jack",
+					    "LINPUT3", "Mic Jack",
+					    "RINPUT1", "Mic Jack",
+					    "RINPUT2", "Mic Jack";
+
+		simple-audio-card,dai-link@0 {
+			reg = <0>;
+			format = "dsp_a";
+			bitclock-master = <&dailink_master>;
+			frame-master = <&dailink_master>;
+
+			cpu {
+				sound-dai = <&tdm>;
+			};
+			dailink_master: codec {
+				sound-dai = <&wm8960>;
+				clocks = <&wm8960_mclk>;
+			};
+		};
+	};
+};
+
+&i2c0 {
+	#address-cells = <1>;
+	#size-cells = <0>;
+
+	wm8960: codec@1a {
+		compatible = "wlf,wm8960";
+		reg = <0x1a>;
+		wlf,shared-lrclk;
+		#sound-dai-cells = <0>;
+	};
+};
diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
index 1155b97b593d..19b5954ee72d 100644
--- a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
+++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
@@ -214,6 +214,40 @@
 			slew-rate = <0>;
 		};
 	};
+
+	tdm0_pins: tdm0-pins {
+		tdm0-pins-tx {
+			pinmux = <GPIOMUX(44, GPOUT_SYS_TDM_TXD,
+					      GPOEN_ENABLE,
+					      GPI_NONE)>;
+			bias-pull-up;
+			drive-strength = <2>;
+			input-disable;
+			input-schmitt-disable;
+			slew-rate = <0>;
+		};
+
+		tdm0-pins-rx {
+			pinmux = <GPIOMUX(61, GPOUT_HIGH,
+					      GPOEN_DISABLE,
+					      GPI_SYS_TDM_RXD)>;
+			input-enable;
+		};
+
+		tdm0-pins-sync {
+			pinmux = <GPIOMUX(63, GPOUT_HIGH,
+					      GPOEN_DISABLE,
+					      GPI_SYS_TDM_SYNC)>;
+			input-enable;
+		};
+
+		tdm0-pins-pcmclk {
+			pinmux = <GPIOMUX(38, GPOUT_HIGH,
+					      GPOEN_DISABLE,
+					      GPI_SYS_TDM_CLK)>;
+			input-enable;
+		};
+	};
 };
 
 &uart0 {
@@ -221,3 +255,9 @@
 	pinctrl-0 = <&uart0_pins>;
 	status = "okay";
 };
+
+&tdm {
+	pinctrl-names = "default";
+	pinctrl-0 = <&tdm0_pins>;
+	status = "okay";
+};
diff --git a/arch/riscv/boot/dts/starfive/jh7110.dtsi b/arch/riscv/boot/dts/starfive/jh7110.dtsi
index 866313570a7e..cfda6fb0d91b 100644
--- a/arch/riscv/boot/dts/starfive/jh7110.dtsi
+++ b/arch/riscv/boot/dts/starfive/jh7110.dtsi
@@ -366,6 +366,27 @@
 			status = "disabled";
 		};
 
+		tdm: tdm@10090000 {
+			compatible = "starfive,jh7110-tdm";
+			reg = <0x0 0x10090000 0x0 0x1000>;
+			clocks = <&syscrg JH7110_SYSCLK_TDM_AHB>,
+				 <&syscrg JH7110_SYSCLK_TDM_APB>,
+				 <&syscrg JH7110_SYSCLK_TDM_INTERNAL>,
+				 <&syscrg JH7110_SYSCLK_TDM_TDM>,
+				 <&syscrg JH7110_SYSCLK_MCLK_INNER>,
+				 <&tdm_ext>;
+			clock-names = "tdm_ahb", "tdm_apb",
+				      "tdm_internal", "tdm",
+				      "mclk_inner", "tdm_ext";
+			resets = <&syscrg JH7110_SYSRST_TDM_AHB>,
+				 <&syscrg JH7110_SYSRST_TDM_APB>,
+				 <&syscrg JH7110_SYSRST_TDM_CORE>;
+			dmas = <&dma 20>, <&dma 21>;
+			dma-names = "rx","tx";
+			#sound-dai-cells = <0>;
+			status = "disabled";
+		};
+
 		stgcrg: clock-controller@10230000 {
 			compatible = "starfive,jh7110-stgcrg";
 			reg = <0x0 0x10230000 0x0 0x10000>;
-- 
2.17.1


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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
  2023-05-06  9:01   ` Walker Chen
@ 2023-05-06 13:47     ` Shengyu Qu
  -1 siblings, 0 replies; 48+ messages in thread
From: Shengyu Qu @ 2023-05-06 13:47 UTC (permalink / raw)
  To: Walker Chen, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing
  Cc: wiagn233, alsa-devel, devicetree, linux-kernel, linux-riscv


[-- Attachment #1.1.1: Type: text/plain, Size: 24015 bytes --]

Hi,

> Add tdm driver support for the StarFive JH7110 SoC.
>
> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
> ---
>   MAINTAINERS                     |   6 +
>   sound/soc/Kconfig               |   1 +
>   sound/soc/Makefile              |   1 +
>   sound/soc/starfive/Kconfig      |  15 +
>   sound/soc/starfive/Makefile     |   2 +
>   sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
>   sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
>   7 files changed, 745 insertions(+)
>   create mode 100644 sound/soc/starfive/Kconfig
>   create mode 100644 sound/soc/starfive/Makefile
>   create mode 100644 sound/soc/starfive/jh7110_tdm.c
>   create mode 100644 sound/soc/starfive/jh7110_tdm.h
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 5f9c544bc189..add89615d327 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -19945,6 +19945,12 @@ F:	Documentation/devicetree/bindings/power/starfive*
>   F:	drivers/soc/starfive/jh71xx_pmu.c
>   F:	include/dt-bindings/power/starfive,jh7110-pmu.h
>   
> +STARFIVE JH7110 TDM DRIVERS
> +M:	Walker Chen <walker.chen@starfivetech.com>
> +S:	Maintained
> +F:	Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
> +F:	sound/soc/starfive/jh7110-tdm.*
> +
>   STARFIVE SOC DRIVERS
>   M:	Conor Dooley <conor@kernel.org>
>   S:	Maintained
> diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
> index 848fbae26c3b..8d1d9401ecf2 100644
> --- a/sound/soc/Kconfig
> +++ b/sound/soc/Kconfig
> @@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
>   source "sound/soc/sof/Kconfig"
>   source "sound/soc/spear/Kconfig"
>   source "sound/soc/sprd/Kconfig"
> +source "sound/soc/starfive/Kconfig"
>   source "sound/soc/sti/Kconfig"
>   source "sound/soc/stm/Kconfig"
>   source "sound/soc/sunxi/Kconfig"
> diff --git a/sound/soc/Makefile b/sound/soc/Makefile
> index 507eaed1d6a1..65aeb4ef4068 100644
> --- a/sound/soc/Makefile
> +++ b/sound/soc/Makefile
> @@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC)	+= sh/
>   obj-$(CONFIG_SND_SOC)	+= sof/
>   obj-$(CONFIG_SND_SOC)	+= spear/
>   obj-$(CONFIG_SND_SOC)	+= sprd/
> +obj-$(CONFIG_SND_SOC)	+= starfive/
>   obj-$(CONFIG_SND_SOC)	+= sti/
>   obj-$(CONFIG_SND_SOC)	+= stm/
>   obj-$(CONFIG_SND_SOC)	+= sunxi/
> diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
> new file mode 100644
> index 000000000000..737c956f7b93
> --- /dev/null
> +++ b/sound/soc/starfive/Kconfig
> @@ -0,0 +1,15 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +config SND_SOC_STARFIVE
> +	tristate "Audio support for StarFive SoC"
> +	depends on COMPILE_TEST || SOC_STARFIVE
> +	help
> +	  Say Y or M if you want to add support for codecs attached to
> +	  the Starfive SoCs' Audio interfaces. You will also need to
> +	  select the audio interfaces to support below.
> +
> +config SND_SOC_JH7110_TDM
> +	tristate "JH7110 TDM device driver"
> +	depends on HAVE_CLK && SND_SOC_STARFIVE
> +	select SND_SOC_GENERIC_DMAENGINE_PCM
> +	help
> +	  Say Y or M if you want to add support for StarFive TDM driver.
> diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
> new file mode 100644
> index 000000000000..f7d960211d72
> --- /dev/null
> +++ b/sound/soc/starfive/Makefile
> @@ -0,0 +1,2 @@
> +# StarFive Platform Support
> +obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
> diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
> new file mode 100644
> index 000000000000..33f7cf43e4bd
> --- /dev/null
> +++ b/sound/soc/starfive/jh7110_tdm.c
> @@ -0,0 +1,573 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * jh7110_tdm.c -- StarFive JH7110 TDM driver
> + *
> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
> + *
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/device.h>
> +#include <linux/module.h>
> +#include <linux/of_irq.h>
> +#include <linux/of_platform.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/regmap.h>
> +#include <linux/reset.h>
> +#include <sound/initval.h>
> +#include <sound/pcm_params.h>
> +#include <sound/soc.h>
> +#include <sound/soc-dai.h>
> +#include "jh7110_tdm.h"
> +
> +static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
> +{
> +	return readl_relaxed(tdm->tdm_base + reg);
> +}
> +
> +static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
> +{
> +	writel_relaxed(val, tdm->tdm_base + reg);
> +}
> +
> +static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
> +				    struct snd_pcm_substream *substream)
> +{
> +	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +		tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
> +	else
> +		tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
> +}
> +
> +static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
> +			     struct snd_pcm_substream *substream)
> +{
> +	u32 data;
> +
> +	data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
> +	jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
> +
> +	/* restore context */
> +	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +		jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
> +	else
> +		jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
> +}
> +
> +static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
> +			    struct snd_pcm_substream *substream)
> +{
> +	unsigned int val;
> +
> +	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> +		val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
> +		val &= ~PCMTXCR_TXEN;
> +		jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
> +	} else {
> +		val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
> +		val &= ~PCMRXCR_RXEN;
> +		jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
> +	}
> +}
> +
> +static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
> +{
> +	u32 sl, sscale, syncdiv;
> +
> +	if (tdm->rx.sl >= tdm->tx.sl)
> +		sl = tdm->rx.sl;
> +	else
> +		sl = tdm->tx.sl;
> +
> +	if (tdm->rx.sscale >= tdm->tx.sscale)
> +		sscale = tdm->rx.sscale;
> +	else
> +		sscale = tdm->tx.sscale;
> +
> +	syncdiv = tdm->pcmclk / tdm->samplerate - 1;
> +
> +	if ((syncdiv + 1) < (sl * sscale)) {
> +		dev_err(tdm->dev, "Failed to set syncdiv!\n");
> +		return -EINVAL;
> +	}
> +
> +	if (tdm->syncm == TDM_SYNCM_LONG &&
> +	    (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
> +		if ((syncdiv + 1) <= sl) {
> +			dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
> +			return -EINVAL;
> +		}
> +	}
> +
> +	jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
> +	return 0;
> +}
> +
> +static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
> +			     struct snd_pcm_substream *substream)
> +{
> +	u32 datarx, datatx;
> +	int ret;
> +
> +	ret = jh7110_tdm_syncdiv(tdm);
> +	if (ret)
> +		return ret;
> +
> +	datarx = (tdm->rx.ifl << IFL_BIT) |
> +		  (tdm->rx.wl << WL_BIT) |
> +		  (tdm->rx.sscale << SSCALE_BIT) |
> +		  (tdm->rx.sl << SL_BIT) |
> +		  (tdm->rx.lrj << LRJ_BIT);
> +
> +	datatx = (tdm->tx.ifl << IFL_BIT) |
> +		  (tdm->tx.wl << WL_BIT) |
> +		  (tdm->tx.sscale << SSCALE_BIT) |
> +		  (tdm->tx.sl << SL_BIT) |
> +		  (tdm->tx.lrj << LRJ_BIT);
> +
> +	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +		jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
> +	else
> +		jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
> +
> +	return 0;
> +}
> +
> +static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
> +{
> +	int i;
> +
> +	for (i = tdm->num_clks - 1; i >= 0; i--)
> +		clk_disable_unprepare(tdm->clks[i]);
> +}
> +
> +static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
> +{
> +	int i, ret;
> +
> +	for (i = 0; i < tdm->num_clks; i++) {
> +		ret = clk_prepare_enable(tdm->clks[i]);
> +		if (ret) {
> +			while (i-- > 0)
> +				clk_disable_unprepare(tdm->clks[i]);
> +			return ret;
> +		}
> +	}
> +
> +	ret = reset_control_deassert(tdm->resets);
> +	if (ret) {
> +		dev_err(tdm->dev, "Failed to deassert tdm resets\n");
> +		goto dis_tdm_clk;
> +	}
> +
> +	/* select tdm_ext clock as the clock source for tdm */
> +	ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
> +	if (ret) {
> +		dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
> +		goto dis_tdm_clk;
> +	}
> +	return 0;
> +
> +dis_tdm_clk:
> +	for (i = tdm->num_clks - 1; i >= 0; i--)
> +		clk_disable_unprepare(tdm->clks[i]);
> +
> +	return ret;
> +}
> +
> +#ifdef CONFIG_PM
> +static int jh7110_tdm_runtime_suspend(struct device *dev)
> +{
> +	struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
> +
> +	jh7110_tdm_clk_disable(tdm);
> +	return 0;
> +}
> +
> +static int jh7110_tdm_runtime_resume(struct device *dev)
> +{
> +	struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
> +
> +	return jh7110_tdm_clk_enable(tdm);
> +}
> +#endif
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int jh7110_tdm_suspend(struct snd_soc_component *component)
> +{
> +	/* save context */
> +	tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
> +	tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);

tdm isn't declared here. Is that same with resume function?

Best regards,

Shengyu

> +
> +	return pm_runtime_force_suspend(component->dev);
> +}
> +
> +static int jh7110_tdm_resume(struct snd_soc_component *component)
> +{
> +	struct jh7110_tdm_dev *tdm = snd_soc_component_get_drvdata(component);
> +
> +	/* restore context */
> +	jh7110_tdm_writel(tdm, TDM_PCMGBCR, tdm->saved_pcmgbcr);
> +	jh7110_tdm_writel(tdm, TDM_PCMDIV, tdm->saved_pcmdiv);
> +
> +	return pm_runtime_force_resume(component->dev);
> +}
> +
> +#else
> +#define jh7110_tdm_suspend	NULL
> +#define jh7110_tdm_resume	NULL
> +#endif
> +
> +static const struct snd_soc_component_driver jh7110_tdm_component = {
> +	.name = "jh7110-tdm",
> +	.suspend = jh7110_tdm_suspend,
> +	.resume	= jh7110_tdm_resume,
> +};
> +
> +static int jh7110_tdm_startup(struct snd_pcm_substream *substream,
> +			      struct snd_soc_dai *cpu_dai)
> +{
> +	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
> +	struct snd_soc_dai_link *dai_link = rtd->dai_link;
> +
> +	dai_link->stop_dma_first = 1;
> +
> +	return 0;
> +}
> +
> +static int jh7110_tdm_hw_params(struct snd_pcm_substream *substream,
> +				struct snd_pcm_hw_params *params,
> +				struct snd_soc_dai *dai)
> +{
> +	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +	int chan_wl, chan_sl, chan_nr;
> +	unsigned int data_width;
> +	unsigned int dma_bus_width;
> +	struct snd_dmaengine_dai_dma_data *dma_data = NULL;
> +	int ret = 0;
> +
> +	data_width = params_width(params);
> +
> +	tdm->samplerate = params_rate(params);
> +	tdm->pcmclk = params_channels(params) * tdm->samplerate * data_width;
> +
> +	switch (params_format(params)) {
> +	case SNDRV_PCM_FORMAT_S16_LE:
> +		chan_wl = TDM_16BIT_WORD_LEN;
> +		chan_sl = TDM_16BIT_SLOT_LEN;
> +		dma_bus_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +		break;
> +
> +	case SNDRV_PCM_FORMAT_S32_LE:
> +		chan_wl = TDM_32BIT_WORD_LEN;
> +		chan_sl = TDM_32BIT_SLOT_LEN;
> +		dma_bus_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
> +		break;
> +
> +	default:
> +		dev_err(tdm->dev, "tdm: unsupported PCM fmt");
> +		return -EINVAL;
> +	}
> +
> +	chan_nr = params_channels(params);
> +	switch (chan_nr) {
> +	case 1:
> +	case 2:
> +	case 4:
> +	case 6:
> +	case 8:
> +		break;
> +	default:
> +		dev_err(tdm->dev, "channel not supported\n");
> +		return -EINVAL;
> +	}
> +
> +	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> +		tdm->tx.wl = chan_wl;
> +		tdm->tx.sl = chan_sl;
> +		tdm->tx.sscale = chan_nr;
> +		tdm->play_dma_data.addr_width = dma_bus_width;
> +		dma_data = &tdm->play_dma_data;
> +	} else {
> +		tdm->rx.wl = chan_wl;
> +		tdm->rx.sl = chan_sl;
> +		tdm->rx.sscale = chan_nr;
> +		tdm->capture_dma_data.addr_width = dma_bus_width;
> +		dma_data = &tdm->capture_dma_data;
> +	}
> +
> +	snd_soc_dai_set_dma_data(dai, substream, dma_data);
> +
> +	ret = jh7110_tdm_config(tdm, substream);
> +	if (ret)
> +		return ret;
> +
> +	jh7110_tdm_save_context(tdm, substream);
> +	return 0;
> +}
> +
> +static int jh7110_tdm_trigger(struct snd_pcm_substream *substream,
> +			      int cmd, struct snd_soc_dai *dai)
> +{
> +	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +	int ret = 0;
> +
> +	switch (cmd) {
> +	case SNDRV_PCM_TRIGGER_START:
> +	case SNDRV_PCM_TRIGGER_RESUME:
> +	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
> +		jh7110_tdm_start(tdm, substream);
> +		break;
> +
> +	case SNDRV_PCM_TRIGGER_STOP:
> +	case SNDRV_PCM_TRIGGER_SUSPEND:
> +	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
> +		jh7110_tdm_stop(tdm, substream);
> +		break;
> +	default:
> +		ret = -EINVAL;
> +		break;
> +	}
> +	return ret;
> +}
> +
> +static int jh7110_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
> +				  unsigned int fmt)
> +{
> +	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(cpu_dai);
> +	unsigned int gbcr;
> +	int ret = 0;
> +
> +	/* set master/slave audio interface */
> +	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
> +	case SND_SOC_DAIFMT_BP_FP:
> +		/* cpu is master */
> +		tdm->ms_mode = TDM_AS_MASTER;
> +		break;
> +	case SND_SOC_DAIFMT_BC_FC:
> +		/* codec is master */
> +		tdm->ms_mode = TDM_AS_SLAVE;
> +		break;
> +	case SND_SOC_DAIFMT_BC_FP:
> +	case SND_SOC_DAIFMT_BP_FC:
> +		ret = -EINVAL;
> +		break;
> +	default:
> +		dev_dbg(tdm->dev, "dwc : Invalid clock provider format\n");
> +		ret = -EINVAL;
> +		break;
> +	}
> +
> +	gbcr = (tdm->clkpolity << CLKPOL_BIT) |
> +		(tdm->elm << ELM_BIT) |
> +		(tdm->syncm << SYNCM_BIT) |
> +		(tdm->ms_mode << MS_BIT);
> +	jh7110_tdm_writel(tdm, TDM_PCMGBCR, gbcr);
> +
> +	return ret;
> +}
> +
> +static const struct snd_soc_dai_ops jh7110_tdm_dai_ops = {
> +	.startup	= jh7110_tdm_startup,
> +	.hw_params	= jh7110_tdm_hw_params,
> +	.trigger	= jh7110_tdm_trigger,
> +	.set_fmt	= jh7110_tdm_set_dai_fmt,
> +};
> +
> +static int jh7110_tdm_dai_probe(struct snd_soc_dai *dai)
> +{
> +	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +
> +	snd_soc_dai_init_dma_data(dai, &tdm->play_dma_data, &tdm->capture_dma_data);
> +	snd_soc_dai_set_drvdata(dai, tdm);
> +	return 0;
> +}
> +
> +#define JH7110_TDM_RATES	SNDRV_PCM_RATE_8000_48000
> +
> +#define JH7110_TDM_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | \
> +				 SNDRV_PCM_FMTBIT_S32_LE)
> +
> +static struct snd_soc_dai_driver jh7110_tdm_dai = {
> +	.name = "sf_tdm",
> +	.id = 0,
> +	.playback = {
> +		.stream_name    = "Playback",
> +		.channels_min   = 1,
> +		.channels_max   = 8,
> +		.rates          = JH7110_TDM_RATES,
> +		.formats        = JH7110_TDM_FORMATS,
> +	},
> +	.capture = {
> +		.stream_name    = "Capture",
> +		.channels_min   = 1,
> +		.channels_max   = 8,
> +		.rates          = JH7110_TDM_RATES,
> +		.formats        = JH7110_TDM_FORMATS,
> +	},
> +	.ops = &jh7110_tdm_dai_ops,
> +	.probe = jh7110_tdm_dai_probe,
> +	.symmetric_rate = 1,
> +};
> +
> +static const struct snd_pcm_hardware jh7110_pcm_hardware = {
> +	.info			= (SNDRV_PCM_INFO_MMAP		|
> +				   SNDRV_PCM_INFO_MMAP_VALID	|
> +				   SNDRV_PCM_INFO_PAUSE		|
> +				   SNDRV_PCM_INFO_RESUME	|
> +				   SNDRV_PCM_INFO_INTERLEAVED	|
> +				   SNDRV_PCM_INFO_BLOCK_TRANSFER),
> +	.buffer_bytes_max	= 192512,
> +	.period_bytes_min	= 4096,
> +	.period_bytes_max	= 32768,
> +	.periods_min		= 1,
> +	.periods_max		= 48,
> +	.fifo_size		= 16,
> +};
> +
> +static const struct snd_dmaengine_pcm_config jh7110_dmaengine_pcm_config = {
> +	.pcm_hardware = &jh7110_pcm_hardware,
> +	.prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
> +	.prealloc_buffer_size = 192512,
> +};
> +
> +static void jh7110_tdm_init_params(struct jh7110_tdm_dev *tdm)
> +{
> +	tdm->clkpolity = TDM_TX_RASING_RX_FALLING;
> +	tdm->elm = TDM_ELM_LATE;
> +	tdm->syncm = TDM_SYNCM_SHORT;
> +
> +	tdm->rx.ifl = TDM_FIFO_HALF;
> +	tdm->tx.ifl = TDM_FIFO_HALF;
> +	tdm->rx.wl = TDM_16BIT_WORD_LEN;
> +	tdm->tx.wl = TDM_16BIT_WORD_LEN;
> +	tdm->rx.sscale = 2;
> +	tdm->tx.sscale = 2;
> +	tdm->rx.lrj = TDM_LEFT_JUSTIFT;
> +	tdm->tx.lrj = TDM_LEFT_JUSTIFT;
> +
> +	tdm->play_dma_data.addr = JH7110_TDM_FIFO;
> +	tdm->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +	tdm->play_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
> +	tdm->play_dma_data.maxburst = 16;
> +
> +	tdm->capture_dma_data.addr = JH7110_TDM_FIFO;
> +	tdm->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +	tdm->capture_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
> +	tdm->capture_dma_data.maxburst = 8;
> +}
> +
> +static int jh7110_tdm_clk_reset_init(struct platform_device *pdev,
> +				     struct jh7110_tdm_dev *tdm)
> +{
> +	int i, ret;
> +
> +	for (i = 0; i < tdm->num_clks; i++) {
> +		tdm->clks[i] = devm_clk_get(&pdev->dev, tdm->clk_names[i]);
> +		if (IS_ERR(tdm->clks[i])) {
> +			dev_err(&pdev->dev, "Failed to get clock: %s\n",
> +				tdm->clk_names[i]);
> +			return PTR_ERR(tdm->clks[i]);
> +		}
> +	}
> +
> +	tdm->resets = devm_reset_control_array_get_exclusive(&pdev->dev);
> +	if (IS_ERR(tdm->resets)) {
> +		ret = PTR_ERR(tdm->resets);
> +		dev_err(&pdev->dev, "Failed to get tdm resets");
> +		return ret;
> +	}
> +
> +	return jh7110_tdm_clk_enable(tdm);
> +}
> +
> +static int jh7110_tdm_probe(struct platform_device *pdev)
> +{
> +	struct jh7110_tdm_dev *tdm;
> +	const struct starfive_tdm_driverdata *driver_data;
> +	int ret;
> +
> +	tdm = devm_kzalloc(&pdev->dev, sizeof(*tdm), GFP_KERNEL);
> +	if (!tdm)
> +		return -ENOMEM;
> +
> +	driver_data = of_device_get_match_data(&pdev->dev);
> +	tdm->clk_names = (const char **)driver_data->clk_names;
> +	tdm->num_clks = driver_data->num_clks;
> +
> +	tdm->tdm_base = devm_platform_ioremap_resource(pdev, 0);
> +	if (IS_ERR(tdm->tdm_base))
> +		return PTR_ERR(tdm->tdm_base);
> +
> +	tdm->dev = &pdev->dev;
> +
> +	ret = jh7110_tdm_clk_reset_init(pdev, tdm);
> +	if (ret) {
> +		dev_err(&pdev->dev, "Failed to enable audio-tdm clock\n");
> +		return ret;
> +	}
> +
> +	jh7110_tdm_init_params(tdm);
> +
> +	dev_set_drvdata(&pdev->dev, tdm);
> +	ret = devm_snd_soc_register_component(&pdev->dev, &jh7110_tdm_component,
> +					      &jh7110_tdm_dai, 1);
> +	if (ret != 0) {
> +		dev_err(&pdev->dev, "Failed to register dai\n");
> +		return ret;
> +	}
> +
> +	ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
> +					      &jh7110_dmaengine_pcm_config,
> +					      SND_DMAENGINE_PCM_FLAG_COMPAT);
> +	if (ret) {
> +		dev_err(&pdev->dev, "Could not register pcm: %d\n", ret);
> +		return ret;
> +	}
> +
> +	pm_runtime_enable(&pdev->dev);
> +#ifdef CONFIG_PM
> +	jh7110_tdm_clk_disable(tdm);
> +#endif
> +
> +	return 0;
> +}
> +
> +static int jh7110_tdm_dev_remove(struct platform_device *pdev)
> +{
> +	pm_runtime_disable(&pdev->dev);
> +	return 0;
> +}
> +
> +static const struct starfive_tdm_driverdata jh7110_drvdata = {
> +	.clk_names = {"mclk_inner", "tdm_ahb", "tdm_apb", "tdm_internal", "tdm_ext", "tdm"},
> +	.num_clks = 6,
> +};
> +
> +static const struct of_device_id jh7110_tdm_of_match[] = {
> +	{ .compatible = "starfive,jh7110-tdm", .data = &jh7110_drvdata },
> +	{}
> +};
> +
> +MODULE_DEVICE_TABLE(of, jh7110_tdm_of_match);
> +
> +static const struct dev_pm_ops jh7110_tdm_pm_ops = {
> +	SET_RUNTIME_PM_OPS(jh7110_tdm_runtime_suspend,
> +			   jh7110_tdm_runtime_resume, NULL)
> +};
> +
> +static struct platform_driver jh7110_tdm_driver = {
> +	.driver = {
> +		.name = "jh7110-tdm",
> +		.of_match_table = jh7110_tdm_of_match,
> +		.pm = &jh7110_tdm_pm_ops,
> +	},
> +	.probe = jh7110_tdm_probe,
> +	.remove = jh7110_tdm_dev_remove,
> +};
> +module_platform_driver(jh7110_tdm_driver);
> +
> +MODULE_DESCRIPTION("StarFive JH7110 TDM ASoC Driver");
> +MODULE_AUTHOR("Walker Chen <walker.chen@starfivetech.com>");
> +MODULE_LICENSE("GPL");
> diff --git a/sound/soc/starfive/jh7110_tdm.h b/sound/soc/starfive/jh7110_tdm.h
> new file mode 100644
> index 000000000000..80f17946a45d
> --- /dev/null
> +++ b/sound/soc/starfive/jh7110_tdm.h
> @@ -0,0 +1,147 @@
> +/* SPDX-License-Identifier: GPL-2.0
> + *
> + * TDM driver for the StarFive JH7110 SoC
> + *
> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
> + *
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + */
> +
> +#ifndef __SND_SOC_STARFIVE_TDM_H
> +#define __SND_SOC_STARFIVE_TDM_H
> +
> +#include <linux/clk.h>
> +#include <linux/device.h>
> +#include <linux/types.h>
> +#include <sound/dmaengine_pcm.h>
> +#include <sound/pcm.h>
> +#include <linux/dmaengine.h>
> +#include <linux/types.h>
> +
> +#define TDM_PCMGBCR			0x00
> +	#define PCMGBCR_MASK		0x1e
> +	#define PCMGBCR_ENABLE		BIT(0)
> +	#define PCMGBCR_TRITXEN		BIT(4)
> +	#define CLKPOL_BIT		5
> +	#define TRITXEN_BIT		4
> +	#define ELM_BIT			3
> +	#define SYNCM_BIT		2
> +	#define MS_BIT			1
> +#define TDM_PCMTXCR			0x04
> +	#define PCMTXCR_TXEN		BIT(0)
> +	#define IFL_BIT			11
> +	#define WL_BIT			8
> +	#define SSCALE_BIT		4
> +	#define SL_BIT			2
> +	#define LRJ_BIT			1
> +#define TDM_PCMRXCR			0x08
> +	#define PCMRXCR_RXEN		BIT(0)
> +	#define PCMRXCR_RXSL_MASK	0xc
> +	#define PCMRXCR_RXSL_16BIT	0x4
> +	#define PCMRXCR_RXSL_32BIT	0x8
> +	#define PCMRXCR_SCALE_MASK	0xf0
> +	#define PCMRXCR_SCALE_1CH	0x10
> +#define TDM_PCMDIV			0x0c
> +
> +/*  DMA registers */
> +#define JH7110_TDM_FIFO			0x170c0000
> +#define JH7110_TDM_FIFO_DEPTH		32
> +#define JH7110_TDM_MAX_CLOCKS		6
> +
> +enum TDM_MASTER_SLAVE_MODE {
> +	TDM_AS_MASTER = 0,
> +	TDM_AS_SLAVE,
> +};
> +
> +enum TDM_CLKPOL {
> +	/* tx raising and rx falling */
> +	TDM_TX_RASING_RX_FALLING = 0,
> +	/* tx falling and rx raising */
> +	TDM_TX_FALLING_RX_RASING,
> +};
> +
> +enum TDM_ELM {
> +	/* only work while SYNCM=0 */
> +	TDM_ELM_LATE = 0,
> +	TDM_ELM_EARLY,
> +};
> +
> +enum TDM_SYNCM {
> +	/* short frame sync */
> +	TDM_SYNCM_SHORT = 0,
> +	/* long frame sync */
> +	TDM_SYNCM_LONG,
> +};
> +
> +enum TDM_IFL {
> +	/* FIFO to send or received : half-1/2, Quarter-1/4 */
> +	TDM_FIFO_HALF = 0,
> +	TDM_FIFO_QUARTER,
> +};
> +
> +enum TDM_WL {
> +	/* send or received word length */
> +	TDM_8BIT_WORD_LEN = 0,
> +	TDM_16BIT_WORD_LEN,
> +	TDM_20BIT_WORD_LEN,
> +	TDM_24BIT_WORD_LEN,
> +	TDM_32BIT_WORD_LEN,
> +};
> +
> +enum TDM_SL {
> +	/* send or received slot length */
> +	TDM_8BIT_SLOT_LEN = 0,
> +	TDM_16BIT_SLOT_LEN,
> +	TDM_32BIT_SLOT_LEN,
> +};
> +
> +enum TDM_LRJ {
> +	/* left-justify or right-justify */
> +	TDM_RIGHT_JUSTIFY = 0,
> +	TDM_LEFT_JUSTIFT,
> +};
> +
> +struct tdm_chan_cfg {
> +	enum TDM_IFL ifl;
> +	enum TDM_WL  wl;
> +	unsigned char sscale;
> +	enum TDM_SL  sl;
> +	enum TDM_LRJ lrj;
> +	unsigned char enable;
> +};
> +
> +struct starfive_tdm_driverdata {
> +	const char *clk_names[JH7110_TDM_MAX_CLOCKS];
> +	int num_clks;
> +};
> +
> +struct jh7110_tdm_dev {
> +	void __iomem *tdm_base;
> +	struct device *dev;
> +	const char **clk_names;
> +	struct clk *clks[JH7110_TDM_MAX_CLOCKS];
> +	int num_clks;
> +	struct reset_control *resets;
> +
> +	enum TDM_CLKPOL clkpolity;
> +	enum TDM_ELM	elm;
> +	enum TDM_SYNCM	syncm;
> +	enum TDM_MASTER_SLAVE_MODE ms_mode;
> +
> +	struct tdm_chan_cfg tx;
> +	struct tdm_chan_cfg rx;
> +
> +	u16 syncdiv;
> +	u32 samplerate;
> +	u32 pcmclk;
> +
> +	/* data related to DMA transfers b/w tdm and DMAC */
> +	struct snd_dmaengine_dai_dma_data play_dma_data;
> +	struct snd_dmaengine_dai_dma_data capture_dma_data;
> +	u32 saved_pcmgbcr;
> +	u32 saved_pcmtxcr;
> +	u32 saved_pcmrxcr;
> +	u32 saved_pcmdiv;
> +};
> +
> +#endif	/* __SND_SOC_STARFIVE_TDM_H */

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 6977 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
@ 2023-05-06 13:47     ` Shengyu Qu
  0 siblings, 0 replies; 48+ messages in thread
From: Shengyu Qu @ 2023-05-06 13:47 UTC (permalink / raw)
  To: Walker Chen, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing
  Cc: wiagn233, alsa-devel, devicetree, linux-kernel, linux-riscv


[-- Attachment #1.1.1.1: Type: text/plain, Size: 24015 bytes --]

Hi,

> Add tdm driver support for the StarFive JH7110 SoC.
>
> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
> ---
>   MAINTAINERS                     |   6 +
>   sound/soc/Kconfig               |   1 +
>   sound/soc/Makefile              |   1 +
>   sound/soc/starfive/Kconfig      |  15 +
>   sound/soc/starfive/Makefile     |   2 +
>   sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
>   sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
>   7 files changed, 745 insertions(+)
>   create mode 100644 sound/soc/starfive/Kconfig
>   create mode 100644 sound/soc/starfive/Makefile
>   create mode 100644 sound/soc/starfive/jh7110_tdm.c
>   create mode 100644 sound/soc/starfive/jh7110_tdm.h
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 5f9c544bc189..add89615d327 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -19945,6 +19945,12 @@ F:	Documentation/devicetree/bindings/power/starfive*
>   F:	drivers/soc/starfive/jh71xx_pmu.c
>   F:	include/dt-bindings/power/starfive,jh7110-pmu.h
>   
> +STARFIVE JH7110 TDM DRIVERS
> +M:	Walker Chen <walker.chen@starfivetech.com>
> +S:	Maintained
> +F:	Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
> +F:	sound/soc/starfive/jh7110-tdm.*
> +
>   STARFIVE SOC DRIVERS
>   M:	Conor Dooley <conor@kernel.org>
>   S:	Maintained
> diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
> index 848fbae26c3b..8d1d9401ecf2 100644
> --- a/sound/soc/Kconfig
> +++ b/sound/soc/Kconfig
> @@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
>   source "sound/soc/sof/Kconfig"
>   source "sound/soc/spear/Kconfig"
>   source "sound/soc/sprd/Kconfig"
> +source "sound/soc/starfive/Kconfig"
>   source "sound/soc/sti/Kconfig"
>   source "sound/soc/stm/Kconfig"
>   source "sound/soc/sunxi/Kconfig"
> diff --git a/sound/soc/Makefile b/sound/soc/Makefile
> index 507eaed1d6a1..65aeb4ef4068 100644
> --- a/sound/soc/Makefile
> +++ b/sound/soc/Makefile
> @@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC)	+= sh/
>   obj-$(CONFIG_SND_SOC)	+= sof/
>   obj-$(CONFIG_SND_SOC)	+= spear/
>   obj-$(CONFIG_SND_SOC)	+= sprd/
> +obj-$(CONFIG_SND_SOC)	+= starfive/
>   obj-$(CONFIG_SND_SOC)	+= sti/
>   obj-$(CONFIG_SND_SOC)	+= stm/
>   obj-$(CONFIG_SND_SOC)	+= sunxi/
> diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
> new file mode 100644
> index 000000000000..737c956f7b93
> --- /dev/null
> +++ b/sound/soc/starfive/Kconfig
> @@ -0,0 +1,15 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +config SND_SOC_STARFIVE
> +	tristate "Audio support for StarFive SoC"
> +	depends on COMPILE_TEST || SOC_STARFIVE
> +	help
> +	  Say Y or M if you want to add support for codecs attached to
> +	  the Starfive SoCs' Audio interfaces. You will also need to
> +	  select the audio interfaces to support below.
> +
> +config SND_SOC_JH7110_TDM
> +	tristate "JH7110 TDM device driver"
> +	depends on HAVE_CLK && SND_SOC_STARFIVE
> +	select SND_SOC_GENERIC_DMAENGINE_PCM
> +	help
> +	  Say Y or M if you want to add support for StarFive TDM driver.
> diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
> new file mode 100644
> index 000000000000..f7d960211d72
> --- /dev/null
> +++ b/sound/soc/starfive/Makefile
> @@ -0,0 +1,2 @@
> +# StarFive Platform Support
> +obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
> diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
> new file mode 100644
> index 000000000000..33f7cf43e4bd
> --- /dev/null
> +++ b/sound/soc/starfive/jh7110_tdm.c
> @@ -0,0 +1,573 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * jh7110_tdm.c -- StarFive JH7110 TDM driver
> + *
> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
> + *
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/device.h>
> +#include <linux/module.h>
> +#include <linux/of_irq.h>
> +#include <linux/of_platform.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/regmap.h>
> +#include <linux/reset.h>
> +#include <sound/initval.h>
> +#include <sound/pcm_params.h>
> +#include <sound/soc.h>
> +#include <sound/soc-dai.h>
> +#include "jh7110_tdm.h"
> +
> +static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
> +{
> +	return readl_relaxed(tdm->tdm_base + reg);
> +}
> +
> +static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
> +{
> +	writel_relaxed(val, tdm->tdm_base + reg);
> +}
> +
> +static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
> +				    struct snd_pcm_substream *substream)
> +{
> +	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +		tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
> +	else
> +		tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
> +}
> +
> +static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
> +			     struct snd_pcm_substream *substream)
> +{
> +	u32 data;
> +
> +	data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
> +	jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
> +
> +	/* restore context */
> +	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +		jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
> +	else
> +		jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
> +}
> +
> +static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
> +			    struct snd_pcm_substream *substream)
> +{
> +	unsigned int val;
> +
> +	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> +		val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
> +		val &= ~PCMTXCR_TXEN;
> +		jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
> +	} else {
> +		val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
> +		val &= ~PCMRXCR_RXEN;
> +		jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
> +	}
> +}
> +
> +static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
> +{
> +	u32 sl, sscale, syncdiv;
> +
> +	if (tdm->rx.sl >= tdm->tx.sl)
> +		sl = tdm->rx.sl;
> +	else
> +		sl = tdm->tx.sl;
> +
> +	if (tdm->rx.sscale >= tdm->tx.sscale)
> +		sscale = tdm->rx.sscale;
> +	else
> +		sscale = tdm->tx.sscale;
> +
> +	syncdiv = tdm->pcmclk / tdm->samplerate - 1;
> +
> +	if ((syncdiv + 1) < (sl * sscale)) {
> +		dev_err(tdm->dev, "Failed to set syncdiv!\n");
> +		return -EINVAL;
> +	}
> +
> +	if (tdm->syncm == TDM_SYNCM_LONG &&
> +	    (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
> +		if ((syncdiv + 1) <= sl) {
> +			dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
> +			return -EINVAL;
> +		}
> +	}
> +
> +	jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
> +	return 0;
> +}
> +
> +static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
> +			     struct snd_pcm_substream *substream)
> +{
> +	u32 datarx, datatx;
> +	int ret;
> +
> +	ret = jh7110_tdm_syncdiv(tdm);
> +	if (ret)
> +		return ret;
> +
> +	datarx = (tdm->rx.ifl << IFL_BIT) |
> +		  (tdm->rx.wl << WL_BIT) |
> +		  (tdm->rx.sscale << SSCALE_BIT) |
> +		  (tdm->rx.sl << SL_BIT) |
> +		  (tdm->rx.lrj << LRJ_BIT);
> +
> +	datatx = (tdm->tx.ifl << IFL_BIT) |
> +		  (tdm->tx.wl << WL_BIT) |
> +		  (tdm->tx.sscale << SSCALE_BIT) |
> +		  (tdm->tx.sl << SL_BIT) |
> +		  (tdm->tx.lrj << LRJ_BIT);
> +
> +	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +		jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
> +	else
> +		jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
> +
> +	return 0;
> +}
> +
> +static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
> +{
> +	int i;
> +
> +	for (i = tdm->num_clks - 1; i >= 0; i--)
> +		clk_disable_unprepare(tdm->clks[i]);
> +}
> +
> +static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
> +{
> +	int i, ret;
> +
> +	for (i = 0; i < tdm->num_clks; i++) {
> +		ret = clk_prepare_enable(tdm->clks[i]);
> +		if (ret) {
> +			while (i-- > 0)
> +				clk_disable_unprepare(tdm->clks[i]);
> +			return ret;
> +		}
> +	}
> +
> +	ret = reset_control_deassert(tdm->resets);
> +	if (ret) {
> +		dev_err(tdm->dev, "Failed to deassert tdm resets\n");
> +		goto dis_tdm_clk;
> +	}
> +
> +	/* select tdm_ext clock as the clock source for tdm */
> +	ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
> +	if (ret) {
> +		dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
> +		goto dis_tdm_clk;
> +	}
> +	return 0;
> +
> +dis_tdm_clk:
> +	for (i = tdm->num_clks - 1; i >= 0; i--)
> +		clk_disable_unprepare(tdm->clks[i]);
> +
> +	return ret;
> +}
> +
> +#ifdef CONFIG_PM
> +static int jh7110_tdm_runtime_suspend(struct device *dev)
> +{
> +	struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
> +
> +	jh7110_tdm_clk_disable(tdm);
> +	return 0;
> +}
> +
> +static int jh7110_tdm_runtime_resume(struct device *dev)
> +{
> +	struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
> +
> +	return jh7110_tdm_clk_enable(tdm);
> +}
> +#endif
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int jh7110_tdm_suspend(struct snd_soc_component *component)
> +{
> +	/* save context */
> +	tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
> +	tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);

tdm isn't declared here. Is that same with resume function?

Best regards,

Shengyu

> +
> +	return pm_runtime_force_suspend(component->dev);
> +}
> +
> +static int jh7110_tdm_resume(struct snd_soc_component *component)
> +{
> +	struct jh7110_tdm_dev *tdm = snd_soc_component_get_drvdata(component);
> +
> +	/* restore context */
> +	jh7110_tdm_writel(tdm, TDM_PCMGBCR, tdm->saved_pcmgbcr);
> +	jh7110_tdm_writel(tdm, TDM_PCMDIV, tdm->saved_pcmdiv);
> +
> +	return pm_runtime_force_resume(component->dev);
> +}
> +
> +#else
> +#define jh7110_tdm_suspend	NULL
> +#define jh7110_tdm_resume	NULL
> +#endif
> +
> +static const struct snd_soc_component_driver jh7110_tdm_component = {
> +	.name = "jh7110-tdm",
> +	.suspend = jh7110_tdm_suspend,
> +	.resume	= jh7110_tdm_resume,
> +};
> +
> +static int jh7110_tdm_startup(struct snd_pcm_substream *substream,
> +			      struct snd_soc_dai *cpu_dai)
> +{
> +	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
> +	struct snd_soc_dai_link *dai_link = rtd->dai_link;
> +
> +	dai_link->stop_dma_first = 1;
> +
> +	return 0;
> +}
> +
> +static int jh7110_tdm_hw_params(struct snd_pcm_substream *substream,
> +				struct snd_pcm_hw_params *params,
> +				struct snd_soc_dai *dai)
> +{
> +	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +	int chan_wl, chan_sl, chan_nr;
> +	unsigned int data_width;
> +	unsigned int dma_bus_width;
> +	struct snd_dmaengine_dai_dma_data *dma_data = NULL;
> +	int ret = 0;
> +
> +	data_width = params_width(params);
> +
> +	tdm->samplerate = params_rate(params);
> +	tdm->pcmclk = params_channels(params) * tdm->samplerate * data_width;
> +
> +	switch (params_format(params)) {
> +	case SNDRV_PCM_FORMAT_S16_LE:
> +		chan_wl = TDM_16BIT_WORD_LEN;
> +		chan_sl = TDM_16BIT_SLOT_LEN;
> +		dma_bus_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +		break;
> +
> +	case SNDRV_PCM_FORMAT_S32_LE:
> +		chan_wl = TDM_32BIT_WORD_LEN;
> +		chan_sl = TDM_32BIT_SLOT_LEN;
> +		dma_bus_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
> +		break;
> +
> +	default:
> +		dev_err(tdm->dev, "tdm: unsupported PCM fmt");
> +		return -EINVAL;
> +	}
> +
> +	chan_nr = params_channels(params);
> +	switch (chan_nr) {
> +	case 1:
> +	case 2:
> +	case 4:
> +	case 6:
> +	case 8:
> +		break;
> +	default:
> +		dev_err(tdm->dev, "channel not supported\n");
> +		return -EINVAL;
> +	}
> +
> +	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> +		tdm->tx.wl = chan_wl;
> +		tdm->tx.sl = chan_sl;
> +		tdm->tx.sscale = chan_nr;
> +		tdm->play_dma_data.addr_width = dma_bus_width;
> +		dma_data = &tdm->play_dma_data;
> +	} else {
> +		tdm->rx.wl = chan_wl;
> +		tdm->rx.sl = chan_sl;
> +		tdm->rx.sscale = chan_nr;
> +		tdm->capture_dma_data.addr_width = dma_bus_width;
> +		dma_data = &tdm->capture_dma_data;
> +	}
> +
> +	snd_soc_dai_set_dma_data(dai, substream, dma_data);
> +
> +	ret = jh7110_tdm_config(tdm, substream);
> +	if (ret)
> +		return ret;
> +
> +	jh7110_tdm_save_context(tdm, substream);
> +	return 0;
> +}
> +
> +static int jh7110_tdm_trigger(struct snd_pcm_substream *substream,
> +			      int cmd, struct snd_soc_dai *dai)
> +{
> +	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +	int ret = 0;
> +
> +	switch (cmd) {
> +	case SNDRV_PCM_TRIGGER_START:
> +	case SNDRV_PCM_TRIGGER_RESUME:
> +	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
> +		jh7110_tdm_start(tdm, substream);
> +		break;
> +
> +	case SNDRV_PCM_TRIGGER_STOP:
> +	case SNDRV_PCM_TRIGGER_SUSPEND:
> +	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
> +		jh7110_tdm_stop(tdm, substream);
> +		break;
> +	default:
> +		ret = -EINVAL;
> +		break;
> +	}
> +	return ret;
> +}
> +
> +static int jh7110_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
> +				  unsigned int fmt)
> +{
> +	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(cpu_dai);
> +	unsigned int gbcr;
> +	int ret = 0;
> +
> +	/* set master/slave audio interface */
> +	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
> +	case SND_SOC_DAIFMT_BP_FP:
> +		/* cpu is master */
> +		tdm->ms_mode = TDM_AS_MASTER;
> +		break;
> +	case SND_SOC_DAIFMT_BC_FC:
> +		/* codec is master */
> +		tdm->ms_mode = TDM_AS_SLAVE;
> +		break;
> +	case SND_SOC_DAIFMT_BC_FP:
> +	case SND_SOC_DAIFMT_BP_FC:
> +		ret = -EINVAL;
> +		break;
> +	default:
> +		dev_dbg(tdm->dev, "dwc : Invalid clock provider format\n");
> +		ret = -EINVAL;
> +		break;
> +	}
> +
> +	gbcr = (tdm->clkpolity << CLKPOL_BIT) |
> +		(tdm->elm << ELM_BIT) |
> +		(tdm->syncm << SYNCM_BIT) |
> +		(tdm->ms_mode << MS_BIT);
> +	jh7110_tdm_writel(tdm, TDM_PCMGBCR, gbcr);
> +
> +	return ret;
> +}
> +
> +static const struct snd_soc_dai_ops jh7110_tdm_dai_ops = {
> +	.startup	= jh7110_tdm_startup,
> +	.hw_params	= jh7110_tdm_hw_params,
> +	.trigger	= jh7110_tdm_trigger,
> +	.set_fmt	= jh7110_tdm_set_dai_fmt,
> +};
> +
> +static int jh7110_tdm_dai_probe(struct snd_soc_dai *dai)
> +{
> +	struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +
> +	snd_soc_dai_init_dma_data(dai, &tdm->play_dma_data, &tdm->capture_dma_data);
> +	snd_soc_dai_set_drvdata(dai, tdm);
> +	return 0;
> +}
> +
> +#define JH7110_TDM_RATES	SNDRV_PCM_RATE_8000_48000
> +
> +#define JH7110_TDM_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | \
> +				 SNDRV_PCM_FMTBIT_S32_LE)
> +
> +static struct snd_soc_dai_driver jh7110_tdm_dai = {
> +	.name = "sf_tdm",
> +	.id = 0,
> +	.playback = {
> +		.stream_name    = "Playback",
> +		.channels_min   = 1,
> +		.channels_max   = 8,
> +		.rates          = JH7110_TDM_RATES,
> +		.formats        = JH7110_TDM_FORMATS,
> +	},
> +	.capture = {
> +		.stream_name    = "Capture",
> +		.channels_min   = 1,
> +		.channels_max   = 8,
> +		.rates          = JH7110_TDM_RATES,
> +		.formats        = JH7110_TDM_FORMATS,
> +	},
> +	.ops = &jh7110_tdm_dai_ops,
> +	.probe = jh7110_tdm_dai_probe,
> +	.symmetric_rate = 1,
> +};
> +
> +static const struct snd_pcm_hardware jh7110_pcm_hardware = {
> +	.info			= (SNDRV_PCM_INFO_MMAP		|
> +				   SNDRV_PCM_INFO_MMAP_VALID	|
> +				   SNDRV_PCM_INFO_PAUSE		|
> +				   SNDRV_PCM_INFO_RESUME	|
> +				   SNDRV_PCM_INFO_INTERLEAVED	|
> +				   SNDRV_PCM_INFO_BLOCK_TRANSFER),
> +	.buffer_bytes_max	= 192512,
> +	.period_bytes_min	= 4096,
> +	.period_bytes_max	= 32768,
> +	.periods_min		= 1,
> +	.periods_max		= 48,
> +	.fifo_size		= 16,
> +};
> +
> +static const struct snd_dmaengine_pcm_config jh7110_dmaengine_pcm_config = {
> +	.pcm_hardware = &jh7110_pcm_hardware,
> +	.prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
> +	.prealloc_buffer_size = 192512,
> +};
> +
> +static void jh7110_tdm_init_params(struct jh7110_tdm_dev *tdm)
> +{
> +	tdm->clkpolity = TDM_TX_RASING_RX_FALLING;
> +	tdm->elm = TDM_ELM_LATE;
> +	tdm->syncm = TDM_SYNCM_SHORT;
> +
> +	tdm->rx.ifl = TDM_FIFO_HALF;
> +	tdm->tx.ifl = TDM_FIFO_HALF;
> +	tdm->rx.wl = TDM_16BIT_WORD_LEN;
> +	tdm->tx.wl = TDM_16BIT_WORD_LEN;
> +	tdm->rx.sscale = 2;
> +	tdm->tx.sscale = 2;
> +	tdm->rx.lrj = TDM_LEFT_JUSTIFT;
> +	tdm->tx.lrj = TDM_LEFT_JUSTIFT;
> +
> +	tdm->play_dma_data.addr = JH7110_TDM_FIFO;
> +	tdm->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +	tdm->play_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
> +	tdm->play_dma_data.maxburst = 16;
> +
> +	tdm->capture_dma_data.addr = JH7110_TDM_FIFO;
> +	tdm->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +	tdm->capture_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
> +	tdm->capture_dma_data.maxburst = 8;
> +}
> +
> +static int jh7110_tdm_clk_reset_init(struct platform_device *pdev,
> +				     struct jh7110_tdm_dev *tdm)
> +{
> +	int i, ret;
> +
> +	for (i = 0; i < tdm->num_clks; i++) {
> +		tdm->clks[i] = devm_clk_get(&pdev->dev, tdm->clk_names[i]);
> +		if (IS_ERR(tdm->clks[i])) {
> +			dev_err(&pdev->dev, "Failed to get clock: %s\n",
> +				tdm->clk_names[i]);
> +			return PTR_ERR(tdm->clks[i]);
> +		}
> +	}
> +
> +	tdm->resets = devm_reset_control_array_get_exclusive(&pdev->dev);
> +	if (IS_ERR(tdm->resets)) {
> +		ret = PTR_ERR(tdm->resets);
> +		dev_err(&pdev->dev, "Failed to get tdm resets");
> +		return ret;
> +	}
> +
> +	return jh7110_tdm_clk_enable(tdm);
> +}
> +
> +static int jh7110_tdm_probe(struct platform_device *pdev)
> +{
> +	struct jh7110_tdm_dev *tdm;
> +	const struct starfive_tdm_driverdata *driver_data;
> +	int ret;
> +
> +	tdm = devm_kzalloc(&pdev->dev, sizeof(*tdm), GFP_KERNEL);
> +	if (!tdm)
> +		return -ENOMEM;
> +
> +	driver_data = of_device_get_match_data(&pdev->dev);
> +	tdm->clk_names = (const char **)driver_data->clk_names;
> +	tdm->num_clks = driver_data->num_clks;
> +
> +	tdm->tdm_base = devm_platform_ioremap_resource(pdev, 0);
> +	if (IS_ERR(tdm->tdm_base))
> +		return PTR_ERR(tdm->tdm_base);
> +
> +	tdm->dev = &pdev->dev;
> +
> +	ret = jh7110_tdm_clk_reset_init(pdev, tdm);
> +	if (ret) {
> +		dev_err(&pdev->dev, "Failed to enable audio-tdm clock\n");
> +		return ret;
> +	}
> +
> +	jh7110_tdm_init_params(tdm);
> +
> +	dev_set_drvdata(&pdev->dev, tdm);
> +	ret = devm_snd_soc_register_component(&pdev->dev, &jh7110_tdm_component,
> +					      &jh7110_tdm_dai, 1);
> +	if (ret != 0) {
> +		dev_err(&pdev->dev, "Failed to register dai\n");
> +		return ret;
> +	}
> +
> +	ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
> +					      &jh7110_dmaengine_pcm_config,
> +					      SND_DMAENGINE_PCM_FLAG_COMPAT);
> +	if (ret) {
> +		dev_err(&pdev->dev, "Could not register pcm: %d\n", ret);
> +		return ret;
> +	}
> +
> +	pm_runtime_enable(&pdev->dev);
> +#ifdef CONFIG_PM
> +	jh7110_tdm_clk_disable(tdm);
> +#endif
> +
> +	return 0;
> +}
> +
> +static int jh7110_tdm_dev_remove(struct platform_device *pdev)
> +{
> +	pm_runtime_disable(&pdev->dev);
> +	return 0;
> +}
> +
> +static const struct starfive_tdm_driverdata jh7110_drvdata = {
> +	.clk_names = {"mclk_inner", "tdm_ahb", "tdm_apb", "tdm_internal", "tdm_ext", "tdm"},
> +	.num_clks = 6,
> +};
> +
> +static const struct of_device_id jh7110_tdm_of_match[] = {
> +	{ .compatible = "starfive,jh7110-tdm", .data = &jh7110_drvdata },
> +	{}
> +};
> +
> +MODULE_DEVICE_TABLE(of, jh7110_tdm_of_match);
> +
> +static const struct dev_pm_ops jh7110_tdm_pm_ops = {
> +	SET_RUNTIME_PM_OPS(jh7110_tdm_runtime_suspend,
> +			   jh7110_tdm_runtime_resume, NULL)
> +};
> +
> +static struct platform_driver jh7110_tdm_driver = {
> +	.driver = {
> +		.name = "jh7110-tdm",
> +		.of_match_table = jh7110_tdm_of_match,
> +		.pm = &jh7110_tdm_pm_ops,
> +	},
> +	.probe = jh7110_tdm_probe,
> +	.remove = jh7110_tdm_dev_remove,
> +};
> +module_platform_driver(jh7110_tdm_driver);
> +
> +MODULE_DESCRIPTION("StarFive JH7110 TDM ASoC Driver");
> +MODULE_AUTHOR("Walker Chen <walker.chen@starfivetech.com>");
> +MODULE_LICENSE("GPL");
> diff --git a/sound/soc/starfive/jh7110_tdm.h b/sound/soc/starfive/jh7110_tdm.h
> new file mode 100644
> index 000000000000..80f17946a45d
> --- /dev/null
> +++ b/sound/soc/starfive/jh7110_tdm.h
> @@ -0,0 +1,147 @@
> +/* SPDX-License-Identifier: GPL-2.0
> + *
> + * TDM driver for the StarFive JH7110 SoC
> + *
> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
> + *
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + */
> +
> +#ifndef __SND_SOC_STARFIVE_TDM_H
> +#define __SND_SOC_STARFIVE_TDM_H
> +
> +#include <linux/clk.h>
> +#include <linux/device.h>
> +#include <linux/types.h>
> +#include <sound/dmaengine_pcm.h>
> +#include <sound/pcm.h>
> +#include <linux/dmaengine.h>
> +#include <linux/types.h>
> +
> +#define TDM_PCMGBCR			0x00
> +	#define PCMGBCR_MASK		0x1e
> +	#define PCMGBCR_ENABLE		BIT(0)
> +	#define PCMGBCR_TRITXEN		BIT(4)
> +	#define CLKPOL_BIT		5
> +	#define TRITXEN_BIT		4
> +	#define ELM_BIT			3
> +	#define SYNCM_BIT		2
> +	#define MS_BIT			1
> +#define TDM_PCMTXCR			0x04
> +	#define PCMTXCR_TXEN		BIT(0)
> +	#define IFL_BIT			11
> +	#define WL_BIT			8
> +	#define SSCALE_BIT		4
> +	#define SL_BIT			2
> +	#define LRJ_BIT			1
> +#define TDM_PCMRXCR			0x08
> +	#define PCMRXCR_RXEN		BIT(0)
> +	#define PCMRXCR_RXSL_MASK	0xc
> +	#define PCMRXCR_RXSL_16BIT	0x4
> +	#define PCMRXCR_RXSL_32BIT	0x8
> +	#define PCMRXCR_SCALE_MASK	0xf0
> +	#define PCMRXCR_SCALE_1CH	0x10
> +#define TDM_PCMDIV			0x0c
> +
> +/*  DMA registers */
> +#define JH7110_TDM_FIFO			0x170c0000
> +#define JH7110_TDM_FIFO_DEPTH		32
> +#define JH7110_TDM_MAX_CLOCKS		6
> +
> +enum TDM_MASTER_SLAVE_MODE {
> +	TDM_AS_MASTER = 0,
> +	TDM_AS_SLAVE,
> +};
> +
> +enum TDM_CLKPOL {
> +	/* tx raising and rx falling */
> +	TDM_TX_RASING_RX_FALLING = 0,
> +	/* tx falling and rx raising */
> +	TDM_TX_FALLING_RX_RASING,
> +};
> +
> +enum TDM_ELM {
> +	/* only work while SYNCM=0 */
> +	TDM_ELM_LATE = 0,
> +	TDM_ELM_EARLY,
> +};
> +
> +enum TDM_SYNCM {
> +	/* short frame sync */
> +	TDM_SYNCM_SHORT = 0,
> +	/* long frame sync */
> +	TDM_SYNCM_LONG,
> +};
> +
> +enum TDM_IFL {
> +	/* FIFO to send or received : half-1/2, Quarter-1/4 */
> +	TDM_FIFO_HALF = 0,
> +	TDM_FIFO_QUARTER,
> +};
> +
> +enum TDM_WL {
> +	/* send or received word length */
> +	TDM_8BIT_WORD_LEN = 0,
> +	TDM_16BIT_WORD_LEN,
> +	TDM_20BIT_WORD_LEN,
> +	TDM_24BIT_WORD_LEN,
> +	TDM_32BIT_WORD_LEN,
> +};
> +
> +enum TDM_SL {
> +	/* send or received slot length */
> +	TDM_8BIT_SLOT_LEN = 0,
> +	TDM_16BIT_SLOT_LEN,
> +	TDM_32BIT_SLOT_LEN,
> +};
> +
> +enum TDM_LRJ {
> +	/* left-justify or right-justify */
> +	TDM_RIGHT_JUSTIFY = 0,
> +	TDM_LEFT_JUSTIFT,
> +};
> +
> +struct tdm_chan_cfg {
> +	enum TDM_IFL ifl;
> +	enum TDM_WL  wl;
> +	unsigned char sscale;
> +	enum TDM_SL  sl;
> +	enum TDM_LRJ lrj;
> +	unsigned char enable;
> +};
> +
> +struct starfive_tdm_driverdata {
> +	const char *clk_names[JH7110_TDM_MAX_CLOCKS];
> +	int num_clks;
> +};
> +
> +struct jh7110_tdm_dev {
> +	void __iomem *tdm_base;
> +	struct device *dev;
> +	const char **clk_names;
> +	struct clk *clks[JH7110_TDM_MAX_CLOCKS];
> +	int num_clks;
> +	struct reset_control *resets;
> +
> +	enum TDM_CLKPOL clkpolity;
> +	enum TDM_ELM	elm;
> +	enum TDM_SYNCM	syncm;
> +	enum TDM_MASTER_SLAVE_MODE ms_mode;
> +
> +	struct tdm_chan_cfg tx;
> +	struct tdm_chan_cfg rx;
> +
> +	u16 syncdiv;
> +	u32 samplerate;
> +	u32 pcmclk;
> +
> +	/* data related to DMA transfers b/w tdm and DMAC */
> +	struct snd_dmaengine_dai_dma_data play_dma_data;
> +	struct snd_dmaengine_dai_dma_data capture_dma_data;
> +	u32 saved_pcmgbcr;
> +	u32 saved_pcmtxcr;
> +	u32 saved_pcmrxcr;
> +	u32 saved_pcmdiv;
> +};
> +
> +#endif	/* __SND_SOC_STARFIVE_TDM_H */

[-- Attachment #1.1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 6977 bytes --]

[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

[-- Attachment #2: Type: text/plain, Size: 161 bytes --]

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
  2023-05-06 13:47     ` Shengyu Qu
  (?)
@ 2023-05-08  0:09       ` Mark Brown
  -1 siblings, 0 replies; 48+ messages in thread
From: Mark Brown @ 2023-05-08  0:09 UTC (permalink / raw)
  To: Shengyu Qu
  Cc: Walker Chen, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, alsa-devel, devicetree, linux-kernel,
	linux-riscv

[-- Attachment #1: Type: text/plain, Size: 573 bytes --]

On Sat, May 06, 2023 at 09:47:55PM +0800, Shengyu Qu wrote:
> Hi,
> 
> > Add tdm driver support for the StarFive JH7110 SoC.
> > 
> > Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
> > ---
> >   MAINTAINERS                     |   6 +
> >   sound/soc/Kconfig               |   1 +
> >   sound/soc/Makefile              |   1 +

Please delete unneeded context from mails when replying.  Doing this
makes it much easier to find your reply in the message, helping ensure
it won't be missed by people scrolling through the irrelevant quoted
material.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
@ 2023-05-08  0:09       ` Mark Brown
  0 siblings, 0 replies; 48+ messages in thread
From: Mark Brown @ 2023-05-08  0:09 UTC (permalink / raw)
  To: Shengyu Qu
  Cc: Walker Chen, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, alsa-devel, devicetree, linux-kernel,
	linux-riscv


[-- Attachment #1.1: Type: text/plain, Size: 573 bytes --]

On Sat, May 06, 2023 at 09:47:55PM +0800, Shengyu Qu wrote:
> Hi,
> 
> > Add tdm driver support for the StarFive JH7110 SoC.
> > 
> > Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
> > ---
> >   MAINTAINERS                     |   6 +
> >   sound/soc/Kconfig               |   1 +
> >   sound/soc/Makefile              |   1 +

Please delete unneeded context from mails when replying.  Doing this
makes it much easier to find your reply in the message, helping ensure
it won't be missed by people scrolling through the irrelevant quoted
material.

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 161 bytes --]

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
@ 2023-05-08  0:09       ` Mark Brown
  0 siblings, 0 replies; 48+ messages in thread
From: Mark Brown @ 2023-05-08  0:09 UTC (permalink / raw)
  To: Shengyu Qu
  Cc: Walker Chen, Liam Girdwood, Takashi Iwai, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Emil Renner Berthing,
	alsa-devel, devicetree, linux-kernel, linux-riscv

[-- Attachment #1: Type: text/plain, Size: 573 bytes --]

On Sat, May 06, 2023 at 09:47:55PM +0800, Shengyu Qu wrote:
> Hi,
> 
> > Add tdm driver support for the StarFive JH7110 SoC.
> > 
> > Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
> > ---
> >   MAINTAINERS                     |   6 +
> >   sound/soc/Kconfig               |   1 +
> >   sound/soc/Makefile              |   1 +

Please delete unneeded context from mails when replying.  Doing this
makes it much easier to find your reply in the message, helping ensure
it won't be missed by people scrolling through the irrelevant quoted
material.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH v3 1/3] dt-bindings: sound: Add TDM for StarFive JH7110
  2023-05-06  9:01   ` Walker Chen
  (?)
@ 2023-05-08  1:30     ` Mark Brown
  -1 siblings, 0 replies; 48+ messages in thread
From: Mark Brown @ 2023-05-08  1:30 UTC (permalink / raw)
  To: Walker Chen
  Cc: Liam Girdwood, Jaroslav Kysela, Takashi Iwai, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Emil Renner Berthing,
	alsa-devel, devicetree, linux-kernel, linux-riscv

[-- Attachment #1: Type: text/plain, Size: 463 bytes --]

On Sat, May 06, 2023 at 05:01:14PM +0800, Walker Chen wrote:
> Add bindings to describe the TDM driver for the StarFive JH7110 SoC.

Please submit patches using subject lines reflecting the style for the
subsystem, this makes it easier for people to identify relevant patches.
Look at what existing commits in the area you're changing are doing and
make sure your subject lines visually resemble what they're doing.
There's no need to resubmit to fix this alone.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH v3 1/3] dt-bindings: sound: Add TDM for StarFive JH7110
@ 2023-05-08  1:30     ` Mark Brown
  0 siblings, 0 replies; 48+ messages in thread
From: Mark Brown @ 2023-05-08  1:30 UTC (permalink / raw)
  To: Walker Chen
  Cc: Liam Girdwood, Jaroslav Kysela, Takashi Iwai, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Emil Renner Berthing,
	alsa-devel, devicetree, linux-kernel, linux-riscv


[-- Attachment #1.1: Type: text/plain, Size: 463 bytes --]

On Sat, May 06, 2023 at 05:01:14PM +0800, Walker Chen wrote:
> Add bindings to describe the TDM driver for the StarFive JH7110 SoC.

Please submit patches using subject lines reflecting the style for the
subsystem, this makes it easier for people to identify relevant patches.
Look at what existing commits in the area you're changing are doing and
make sure your subject lines visually resemble what they're doing.
There's no need to resubmit to fix this alone.

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 161 bytes --]

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 1/3] dt-bindings: sound: Add TDM for StarFive JH7110
@ 2023-05-08  1:30     ` Mark Brown
  0 siblings, 0 replies; 48+ messages in thread
From: Mark Brown @ 2023-05-08  1:30 UTC (permalink / raw)
  To: Walker Chen
  Cc: Liam Girdwood, Takashi Iwai, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Emil Renner Berthing, alsa-devel, devicetree,
	linux-kernel, linux-riscv

[-- Attachment #1: Type: text/plain, Size: 463 bytes --]

On Sat, May 06, 2023 at 05:01:14PM +0800, Walker Chen wrote:
> Add bindings to describe the TDM driver for the StarFive JH7110 SoC.

Please submit patches using subject lines reflecting the style for the
subsystem, this makes it easier for people to identify relevant patches.
Look at what existing commits in the area you're changing are doing and
make sure your subject lines visually resemble what they're doing.
There's no need to resubmit to fix this alone.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
  2023-05-06 13:47     ` Shengyu Qu
  (?)
@ 2023-05-08  1:41       ` Walker Chen
  -1 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-08  1:41 UTC (permalink / raw)
  To: Shengyu Qu, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv



On 2023/5/6 21:47, Shengyu Qu wrote:
> Hi,
> 
>> Add tdm driver support for the StarFive JH7110 SoC.
>>
>> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
>> ---
>>   MAINTAINERS                     |   6 +
>>   sound/soc/Kconfig               |   1 +
>>   sound/soc/Makefile              |   1 +
>>   sound/soc/starfive/Kconfig      |  15 +
>>   sound/soc/starfive/Makefile     |   2 +
>>   sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
>>   sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
>>   7 files changed, 745 insertions(+)
>>   create mode 100644 sound/soc/starfive/Kconfig
>>   create mode 100644 sound/soc/starfive/Makefile
>>   create mode 100644 sound/soc/starfive/jh7110_tdm.c
>>   create mode 100644 sound/soc/starfive/jh7110_tdm.h
>>
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index 5f9c544bc189..add89615d327 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -19945,6 +19945,12 @@ F:    Documentation/devicetree/bindings/power/starfive*
>>   F:    drivers/soc/starfive/jh71xx_pmu.c
>>   F:    include/dt-bindings/power/starfive,jh7110-pmu.h
>>   +STARFIVE JH7110 TDM DRIVERS
>> +M:    Walker Chen <walker.chen@starfivetech.com>
>> +S:    Maintained
>> +F:    Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
>> +F:    sound/soc/starfive/jh7110-tdm.*
>> +
>>   STARFIVE SOC DRIVERS
>>   M:    Conor Dooley <conor@kernel.org>
>>   S:    Maintained
>> diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
>> index 848fbae26c3b..8d1d9401ecf2 100644
>> --- a/sound/soc/Kconfig
>> +++ b/sound/soc/Kconfig
>> @@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
>>   source "sound/soc/sof/Kconfig"
>>   source "sound/soc/spear/Kconfig"
>>   source "sound/soc/sprd/Kconfig"
>> +source "sound/soc/starfive/Kconfig"
>>   source "sound/soc/sti/Kconfig"
>>   source "sound/soc/stm/Kconfig"
>>   source "sound/soc/sunxi/Kconfig"
>> diff --git a/sound/soc/Makefile b/sound/soc/Makefile
>> index 507eaed1d6a1..65aeb4ef4068 100644
>> --- a/sound/soc/Makefile
>> +++ b/sound/soc/Makefile
>> @@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC)    += sh/
>>   obj-$(CONFIG_SND_SOC)    += sof/
>>   obj-$(CONFIG_SND_SOC)    += spear/
>>   obj-$(CONFIG_SND_SOC)    += sprd/
>> +obj-$(CONFIG_SND_SOC)    += starfive/
>>   obj-$(CONFIG_SND_SOC)    += sti/
>>   obj-$(CONFIG_SND_SOC)    += stm/
>>   obj-$(CONFIG_SND_SOC)    += sunxi/
>> diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
>> new file mode 100644
>> index 000000000000..737c956f7b93
>> --- /dev/null
>> +++ b/sound/soc/starfive/Kconfig
>> @@ -0,0 +1,15 @@
>> +# SPDX-License-Identifier: GPL-2.0-only
>> +config SND_SOC_STARFIVE
>> +    tristate "Audio support for StarFive SoC"
>> +    depends on COMPILE_TEST || SOC_STARFIVE
>> +    help
>> +      Say Y or M if you want to add support for codecs attached to
>> +      the Starfive SoCs' Audio interfaces. You will also need to
>> +      select the audio interfaces to support below.
>> +
>> +config SND_SOC_JH7110_TDM
>> +    tristate "JH7110 TDM device driver"
>> +    depends on HAVE_CLK && SND_SOC_STARFIVE
>> +    select SND_SOC_GENERIC_DMAENGINE_PCM
>> +    help
>> +      Say Y or M if you want to add support for StarFive TDM driver.
>> diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
>> new file mode 100644
>> index 000000000000..f7d960211d72
>> --- /dev/null
>> +++ b/sound/soc/starfive/Makefile
>> @@ -0,0 +1,2 @@
>> +# StarFive Platform Support
>> +obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
>> diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
>> new file mode 100644
>> index 000000000000..33f7cf43e4bd
>> --- /dev/null
>> +++ b/sound/soc/starfive/jh7110_tdm.c
>> @@ -0,0 +1,573 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * jh7110_tdm.c -- StarFive JH7110 TDM driver
>> + *
>> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
>> + *
>> + * Author: Walker Chen <walker.chen@starfivetech.com>
>> + */
>> +
>> +#include <linux/clk.h>
>> +#include <linux/device.h>
>> +#include <linux/module.h>
>> +#include <linux/of_irq.h>
>> +#include <linux/of_platform.h>
>> +#include <linux/pm_runtime.h>
>> +#include <linux/regmap.h>
>> +#include <linux/reset.h>
>> +#include <sound/initval.h>
>> +#include <sound/pcm_params.h>
>> +#include <sound/soc.h>
>> +#include <sound/soc-dai.h>
>> +#include "jh7110_tdm.h"
>> +
>> +static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
>> +{
>> +    return readl_relaxed(tdm->tdm_base + reg);
>> +}
>> +
>> +static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
>> +{
>> +    writel_relaxed(val, tdm->tdm_base + reg);
>> +}
>> +
>> +static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
>> +                    struct snd_pcm_substream *substream)
>> +{
>> +    if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +        tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
>> +    else
>> +        tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
>> +}
>> +
>> +static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
>> +                 struct snd_pcm_substream *substream)
>> +{
>> +    u32 data;
>> +
>> +    data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
>> +    jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
>> +
>> +    /* restore context */
>> +    if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +        jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
>> +    else
>> +        jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
>> +}
>> +
>> +static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
>> +                struct snd_pcm_substream *substream)
>> +{
>> +    unsigned int val;
>> +
>> +    if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
>> +        val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
>> +        val &= ~PCMTXCR_TXEN;
>> +        jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
>> +    } else {
>> +        val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
>> +        val &= ~PCMRXCR_RXEN;
>> +        jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
>> +    }
>> +}
>> +
>> +static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
>> +{
>> +    u32 sl, sscale, syncdiv;
>> +
>> +    if (tdm->rx.sl >= tdm->tx.sl)
>> +        sl = tdm->rx.sl;
>> +    else
>> +        sl = tdm->tx.sl;
>> +
>> +    if (tdm->rx.sscale >= tdm->tx.sscale)
>> +        sscale = tdm->rx.sscale;
>> +    else
>> +        sscale = tdm->tx.sscale;
>> +
>> +    syncdiv = tdm->pcmclk / tdm->samplerate - 1;
>> +
>> +    if ((syncdiv + 1) < (sl * sscale)) {
>> +        dev_err(tdm->dev, "Failed to set syncdiv!\n");
>> +        return -EINVAL;
>> +    }
>> +
>> +    if (tdm->syncm == TDM_SYNCM_LONG &&
>> +        (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
>> +        if ((syncdiv + 1) <= sl) {
>> +            dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
>> +            return -EINVAL;
>> +        }
>> +    }
>> +
>> +    jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
>> +    return 0;
>> +}
>> +
>> +static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
>> +                 struct snd_pcm_substream *substream)
>> +{
>> +    u32 datarx, datatx;
>> +    int ret;
>> +
>> +    ret = jh7110_tdm_syncdiv(tdm);
>> +    if (ret)
>> +        return ret;
>> +
>> +    datarx = (tdm->rx.ifl << IFL_BIT) |
>> +          (tdm->rx.wl << WL_BIT) |
>> +          (tdm->rx.sscale << SSCALE_BIT) |
>> +          (tdm->rx.sl << SL_BIT) |
>> +          (tdm->rx.lrj << LRJ_BIT);
>> +
>> +    datatx = (tdm->tx.ifl << IFL_BIT) |
>> +          (tdm->tx.wl << WL_BIT) |
>> +          (tdm->tx.sscale << SSCALE_BIT) |
>> +          (tdm->tx.sl << SL_BIT) |
>> +          (tdm->tx.lrj << LRJ_BIT);
>> +
>> +    if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +        jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
>> +    else
>> +        jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
>> +
>> +    return 0;
>> +}
>> +
>> +static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
>> +{
>> +    int i;
>> +
>> +    for (i = tdm->num_clks - 1; i >= 0; i--)
>> +        clk_disable_unprepare(tdm->clks[i]);
>> +}
>> +
>> +static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
>> +{
>> +    int i, ret;
>> +
>> +    for (i = 0; i < tdm->num_clks; i++) {
>> +        ret = clk_prepare_enable(tdm->clks[i]);
>> +        if (ret) {
>> +            while (i-- > 0)
>> +                clk_disable_unprepare(tdm->clks[i]);
>> +            return ret;
>> +        }
>> +    }
>> +
>> +    ret = reset_control_deassert(tdm->resets);
>> +    if (ret) {
>> +        dev_err(tdm->dev, "Failed to deassert tdm resets\n");
>> +        goto dis_tdm_clk;
>> +    }
>> +
>> +    /* select tdm_ext clock as the clock source for tdm */
>> +    ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
>> +    if (ret) {
>> +        dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
>> +        goto dis_tdm_clk;
>> +    }
>> +    return 0;
>> +
>> +dis_tdm_clk:
>> +    for (i = tdm->num_clks - 1; i >= 0; i--)
>> +        clk_disable_unprepare(tdm->clks[i]);
>> +
>> +    return ret;
>> +}
>> +
>> +#ifdef CONFIG_PM
>> +static int jh7110_tdm_runtime_suspend(struct device *dev)
>> +{
>> +    struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
>> +
>> +    jh7110_tdm_clk_disable(tdm);
>> +    return 0;
>> +}
>> +
>> +static int jh7110_tdm_runtime_resume(struct device *dev)
>> +{
>> +    struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
>> +
>> +    return jh7110_tdm_clk_enable(tdm);
>> +}
>> +#endif
>> +
>> +#ifdef CONFIG_PM_SLEEP
>> +static int jh7110_tdm_suspend(struct snd_soc_component *component)
>> +{
>> +    /* save context */
>> +    tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
>> +    tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);
> 
> tdm isn't declared here. Is that same with resume function?

OMG! Maybe I accidentally deleted the declaration of tdm while deleting some debugging statement.
Yes, it is same with resume function.

Best regards,
Walker

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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
@ 2023-05-08  1:41       ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-08  1:41 UTC (permalink / raw)
  To: Shengyu Qu, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv



On 2023/5/6 21:47, Shengyu Qu wrote:
> Hi,
> 
>> Add tdm driver support for the StarFive JH7110 SoC.
>>
>> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
>> ---
>>   MAINTAINERS                     |   6 +
>>   sound/soc/Kconfig               |   1 +
>>   sound/soc/Makefile              |   1 +
>>   sound/soc/starfive/Kconfig      |  15 +
>>   sound/soc/starfive/Makefile     |   2 +
>>   sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
>>   sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
>>   7 files changed, 745 insertions(+)
>>   create mode 100644 sound/soc/starfive/Kconfig
>>   create mode 100644 sound/soc/starfive/Makefile
>>   create mode 100644 sound/soc/starfive/jh7110_tdm.c
>>   create mode 100644 sound/soc/starfive/jh7110_tdm.h
>>
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index 5f9c544bc189..add89615d327 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -19945,6 +19945,12 @@ F:    Documentation/devicetree/bindings/power/starfive*
>>   F:    drivers/soc/starfive/jh71xx_pmu.c
>>   F:    include/dt-bindings/power/starfive,jh7110-pmu.h
>>   +STARFIVE JH7110 TDM DRIVERS
>> +M:    Walker Chen <walker.chen@starfivetech.com>
>> +S:    Maintained
>> +F:    Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
>> +F:    sound/soc/starfive/jh7110-tdm.*
>> +
>>   STARFIVE SOC DRIVERS
>>   M:    Conor Dooley <conor@kernel.org>
>>   S:    Maintained
>> diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
>> index 848fbae26c3b..8d1d9401ecf2 100644
>> --- a/sound/soc/Kconfig
>> +++ b/sound/soc/Kconfig
>> @@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
>>   source "sound/soc/sof/Kconfig"
>>   source "sound/soc/spear/Kconfig"
>>   source "sound/soc/sprd/Kconfig"
>> +source "sound/soc/starfive/Kconfig"
>>   source "sound/soc/sti/Kconfig"
>>   source "sound/soc/stm/Kconfig"
>>   source "sound/soc/sunxi/Kconfig"
>> diff --git a/sound/soc/Makefile b/sound/soc/Makefile
>> index 507eaed1d6a1..65aeb4ef4068 100644
>> --- a/sound/soc/Makefile
>> +++ b/sound/soc/Makefile
>> @@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC)    += sh/
>>   obj-$(CONFIG_SND_SOC)    += sof/
>>   obj-$(CONFIG_SND_SOC)    += spear/
>>   obj-$(CONFIG_SND_SOC)    += sprd/
>> +obj-$(CONFIG_SND_SOC)    += starfive/
>>   obj-$(CONFIG_SND_SOC)    += sti/
>>   obj-$(CONFIG_SND_SOC)    += stm/
>>   obj-$(CONFIG_SND_SOC)    += sunxi/
>> diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
>> new file mode 100644
>> index 000000000000..737c956f7b93
>> --- /dev/null
>> +++ b/sound/soc/starfive/Kconfig
>> @@ -0,0 +1,15 @@
>> +# SPDX-License-Identifier: GPL-2.0-only
>> +config SND_SOC_STARFIVE
>> +    tristate "Audio support for StarFive SoC"
>> +    depends on COMPILE_TEST || SOC_STARFIVE
>> +    help
>> +      Say Y or M if you want to add support for codecs attached to
>> +      the Starfive SoCs' Audio interfaces. You will also need to
>> +      select the audio interfaces to support below.
>> +
>> +config SND_SOC_JH7110_TDM
>> +    tristate "JH7110 TDM device driver"
>> +    depends on HAVE_CLK && SND_SOC_STARFIVE
>> +    select SND_SOC_GENERIC_DMAENGINE_PCM
>> +    help
>> +      Say Y or M if you want to add support for StarFive TDM driver.
>> diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
>> new file mode 100644
>> index 000000000000..f7d960211d72
>> --- /dev/null
>> +++ b/sound/soc/starfive/Makefile
>> @@ -0,0 +1,2 @@
>> +# StarFive Platform Support
>> +obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
>> diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
>> new file mode 100644
>> index 000000000000..33f7cf43e4bd
>> --- /dev/null
>> +++ b/sound/soc/starfive/jh7110_tdm.c
>> @@ -0,0 +1,573 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * jh7110_tdm.c -- StarFive JH7110 TDM driver
>> + *
>> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
>> + *
>> + * Author: Walker Chen <walker.chen@starfivetech.com>
>> + */
>> +
>> +#include <linux/clk.h>
>> +#include <linux/device.h>
>> +#include <linux/module.h>
>> +#include <linux/of_irq.h>
>> +#include <linux/of_platform.h>
>> +#include <linux/pm_runtime.h>
>> +#include <linux/regmap.h>
>> +#include <linux/reset.h>
>> +#include <sound/initval.h>
>> +#include <sound/pcm_params.h>
>> +#include <sound/soc.h>
>> +#include <sound/soc-dai.h>
>> +#include "jh7110_tdm.h"
>> +
>> +static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
>> +{
>> +    return readl_relaxed(tdm->tdm_base + reg);
>> +}
>> +
>> +static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
>> +{
>> +    writel_relaxed(val, tdm->tdm_base + reg);
>> +}
>> +
>> +static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
>> +                    struct snd_pcm_substream *substream)
>> +{
>> +    if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +        tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
>> +    else
>> +        tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
>> +}
>> +
>> +static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
>> +                 struct snd_pcm_substream *substream)
>> +{
>> +    u32 data;
>> +
>> +    data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
>> +    jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
>> +
>> +    /* restore context */
>> +    if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +        jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
>> +    else
>> +        jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
>> +}
>> +
>> +static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
>> +                struct snd_pcm_substream *substream)
>> +{
>> +    unsigned int val;
>> +
>> +    if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
>> +        val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
>> +        val &= ~PCMTXCR_TXEN;
>> +        jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
>> +    } else {
>> +        val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
>> +        val &= ~PCMRXCR_RXEN;
>> +        jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
>> +    }
>> +}
>> +
>> +static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
>> +{
>> +    u32 sl, sscale, syncdiv;
>> +
>> +    if (tdm->rx.sl >= tdm->tx.sl)
>> +        sl = tdm->rx.sl;
>> +    else
>> +        sl = tdm->tx.sl;
>> +
>> +    if (tdm->rx.sscale >= tdm->tx.sscale)
>> +        sscale = tdm->rx.sscale;
>> +    else
>> +        sscale = tdm->tx.sscale;
>> +
>> +    syncdiv = tdm->pcmclk / tdm->samplerate - 1;
>> +
>> +    if ((syncdiv + 1) < (sl * sscale)) {
>> +        dev_err(tdm->dev, "Failed to set syncdiv!\n");
>> +        return -EINVAL;
>> +    }
>> +
>> +    if (tdm->syncm == TDM_SYNCM_LONG &&
>> +        (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
>> +        if ((syncdiv + 1) <= sl) {
>> +            dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
>> +            return -EINVAL;
>> +        }
>> +    }
>> +
>> +    jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
>> +    return 0;
>> +}
>> +
>> +static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
>> +                 struct snd_pcm_substream *substream)
>> +{
>> +    u32 datarx, datatx;
>> +    int ret;
>> +
>> +    ret = jh7110_tdm_syncdiv(tdm);
>> +    if (ret)
>> +        return ret;
>> +
>> +    datarx = (tdm->rx.ifl << IFL_BIT) |
>> +          (tdm->rx.wl << WL_BIT) |
>> +          (tdm->rx.sscale << SSCALE_BIT) |
>> +          (tdm->rx.sl << SL_BIT) |
>> +          (tdm->rx.lrj << LRJ_BIT);
>> +
>> +    datatx = (tdm->tx.ifl << IFL_BIT) |
>> +          (tdm->tx.wl << WL_BIT) |
>> +          (tdm->tx.sscale << SSCALE_BIT) |
>> +          (tdm->tx.sl << SL_BIT) |
>> +          (tdm->tx.lrj << LRJ_BIT);
>> +
>> +    if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +        jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
>> +    else
>> +        jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
>> +
>> +    return 0;
>> +}
>> +
>> +static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
>> +{
>> +    int i;
>> +
>> +    for (i = tdm->num_clks - 1; i >= 0; i--)
>> +        clk_disable_unprepare(tdm->clks[i]);
>> +}
>> +
>> +static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
>> +{
>> +    int i, ret;
>> +
>> +    for (i = 0; i < tdm->num_clks; i++) {
>> +        ret = clk_prepare_enable(tdm->clks[i]);
>> +        if (ret) {
>> +            while (i-- > 0)
>> +                clk_disable_unprepare(tdm->clks[i]);
>> +            return ret;
>> +        }
>> +    }
>> +
>> +    ret = reset_control_deassert(tdm->resets);
>> +    if (ret) {
>> +        dev_err(tdm->dev, "Failed to deassert tdm resets\n");
>> +        goto dis_tdm_clk;
>> +    }
>> +
>> +    /* select tdm_ext clock as the clock source for tdm */
>> +    ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
>> +    if (ret) {
>> +        dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
>> +        goto dis_tdm_clk;
>> +    }
>> +    return 0;
>> +
>> +dis_tdm_clk:
>> +    for (i = tdm->num_clks - 1; i >= 0; i--)
>> +        clk_disable_unprepare(tdm->clks[i]);
>> +
>> +    return ret;
>> +}
>> +
>> +#ifdef CONFIG_PM
>> +static int jh7110_tdm_runtime_suspend(struct device *dev)
>> +{
>> +    struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
>> +
>> +    jh7110_tdm_clk_disable(tdm);
>> +    return 0;
>> +}
>> +
>> +static int jh7110_tdm_runtime_resume(struct device *dev)
>> +{
>> +    struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
>> +
>> +    return jh7110_tdm_clk_enable(tdm);
>> +}
>> +#endif
>> +
>> +#ifdef CONFIG_PM_SLEEP
>> +static int jh7110_tdm_suspend(struct snd_soc_component *component)
>> +{
>> +    /* save context */
>> +    tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
>> +    tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);
> 
> tdm isn't declared here. Is that same with resume function?

OMG! Maybe I accidentally deleted the declaration of tdm while deleting some debugging statement.
Yes, it is same with resume function.

Best regards,
Walker

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
@ 2023-05-08  1:41       ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-08  1:41 UTC (permalink / raw)
  To: Shengyu Qu, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv



On 2023/5/6 21:47, Shengyu Qu wrote:
> Hi,
> 
>> Add tdm driver support for the StarFive JH7110 SoC.
>>
>> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
>> ---
>>   MAINTAINERS                     |   6 +
>>   sound/soc/Kconfig               |   1 +
>>   sound/soc/Makefile              |   1 +
>>   sound/soc/starfive/Kconfig      |  15 +
>>   sound/soc/starfive/Makefile     |   2 +
>>   sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
>>   sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
>>   7 files changed, 745 insertions(+)
>>   create mode 100644 sound/soc/starfive/Kconfig
>>   create mode 100644 sound/soc/starfive/Makefile
>>   create mode 100644 sound/soc/starfive/jh7110_tdm.c
>>   create mode 100644 sound/soc/starfive/jh7110_tdm.h
>>
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index 5f9c544bc189..add89615d327 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -19945,6 +19945,12 @@ F:    Documentation/devicetree/bindings/power/starfive*
>>   F:    drivers/soc/starfive/jh71xx_pmu.c
>>   F:    include/dt-bindings/power/starfive,jh7110-pmu.h
>>   +STARFIVE JH7110 TDM DRIVERS
>> +M:    Walker Chen <walker.chen@starfivetech.com>
>> +S:    Maintained
>> +F:    Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
>> +F:    sound/soc/starfive/jh7110-tdm.*
>> +
>>   STARFIVE SOC DRIVERS
>>   M:    Conor Dooley <conor@kernel.org>
>>   S:    Maintained
>> diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
>> index 848fbae26c3b..8d1d9401ecf2 100644
>> --- a/sound/soc/Kconfig
>> +++ b/sound/soc/Kconfig
>> @@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
>>   source "sound/soc/sof/Kconfig"
>>   source "sound/soc/spear/Kconfig"
>>   source "sound/soc/sprd/Kconfig"
>> +source "sound/soc/starfive/Kconfig"
>>   source "sound/soc/sti/Kconfig"
>>   source "sound/soc/stm/Kconfig"
>>   source "sound/soc/sunxi/Kconfig"
>> diff --git a/sound/soc/Makefile b/sound/soc/Makefile
>> index 507eaed1d6a1..65aeb4ef4068 100644
>> --- a/sound/soc/Makefile
>> +++ b/sound/soc/Makefile
>> @@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC)    += sh/
>>   obj-$(CONFIG_SND_SOC)    += sof/
>>   obj-$(CONFIG_SND_SOC)    += spear/
>>   obj-$(CONFIG_SND_SOC)    += sprd/
>> +obj-$(CONFIG_SND_SOC)    += starfive/
>>   obj-$(CONFIG_SND_SOC)    += sti/
>>   obj-$(CONFIG_SND_SOC)    += stm/
>>   obj-$(CONFIG_SND_SOC)    += sunxi/
>> diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
>> new file mode 100644
>> index 000000000000..737c956f7b93
>> --- /dev/null
>> +++ b/sound/soc/starfive/Kconfig
>> @@ -0,0 +1,15 @@
>> +# SPDX-License-Identifier: GPL-2.0-only
>> +config SND_SOC_STARFIVE
>> +    tristate "Audio support for StarFive SoC"
>> +    depends on COMPILE_TEST || SOC_STARFIVE
>> +    help
>> +      Say Y or M if you want to add support for codecs attached to
>> +      the Starfive SoCs' Audio interfaces. You will also need to
>> +      select the audio interfaces to support below.
>> +
>> +config SND_SOC_JH7110_TDM
>> +    tristate "JH7110 TDM device driver"
>> +    depends on HAVE_CLK && SND_SOC_STARFIVE
>> +    select SND_SOC_GENERIC_DMAENGINE_PCM
>> +    help
>> +      Say Y or M if you want to add support for StarFive TDM driver.
>> diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
>> new file mode 100644
>> index 000000000000..f7d960211d72
>> --- /dev/null
>> +++ b/sound/soc/starfive/Makefile
>> @@ -0,0 +1,2 @@
>> +# StarFive Platform Support
>> +obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
>> diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
>> new file mode 100644
>> index 000000000000..33f7cf43e4bd
>> --- /dev/null
>> +++ b/sound/soc/starfive/jh7110_tdm.c
>> @@ -0,0 +1,573 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * jh7110_tdm.c -- StarFive JH7110 TDM driver
>> + *
>> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
>> + *
>> + * Author: Walker Chen <walker.chen@starfivetech.com>
>> + */
>> +
>> +#include <linux/clk.h>
>> +#include <linux/device.h>
>> +#include <linux/module.h>
>> +#include <linux/of_irq.h>
>> +#include <linux/of_platform.h>
>> +#include <linux/pm_runtime.h>
>> +#include <linux/regmap.h>
>> +#include <linux/reset.h>
>> +#include <sound/initval.h>
>> +#include <sound/pcm_params.h>
>> +#include <sound/soc.h>
>> +#include <sound/soc-dai.h>
>> +#include "jh7110_tdm.h"
>> +
>> +static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
>> +{
>> +    return readl_relaxed(tdm->tdm_base + reg);
>> +}
>> +
>> +static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
>> +{
>> +    writel_relaxed(val, tdm->tdm_base + reg);
>> +}
>> +
>> +static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
>> +                    struct snd_pcm_substream *substream)
>> +{
>> +    if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +        tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
>> +    else
>> +        tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
>> +}
>> +
>> +static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
>> +                 struct snd_pcm_substream *substream)
>> +{
>> +    u32 data;
>> +
>> +    data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
>> +    jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
>> +
>> +    /* restore context */
>> +    if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +        jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
>> +    else
>> +        jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
>> +}
>> +
>> +static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
>> +                struct snd_pcm_substream *substream)
>> +{
>> +    unsigned int val;
>> +
>> +    if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
>> +        val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
>> +        val &= ~PCMTXCR_TXEN;
>> +        jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
>> +    } else {
>> +        val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
>> +        val &= ~PCMRXCR_RXEN;
>> +        jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
>> +    }
>> +}
>> +
>> +static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
>> +{
>> +    u32 sl, sscale, syncdiv;
>> +
>> +    if (tdm->rx.sl >= tdm->tx.sl)
>> +        sl = tdm->rx.sl;
>> +    else
>> +        sl = tdm->tx.sl;
>> +
>> +    if (tdm->rx.sscale >= tdm->tx.sscale)
>> +        sscale = tdm->rx.sscale;
>> +    else
>> +        sscale = tdm->tx.sscale;
>> +
>> +    syncdiv = tdm->pcmclk / tdm->samplerate - 1;
>> +
>> +    if ((syncdiv + 1) < (sl * sscale)) {
>> +        dev_err(tdm->dev, "Failed to set syncdiv!\n");
>> +        return -EINVAL;
>> +    }
>> +
>> +    if (tdm->syncm == TDM_SYNCM_LONG &&
>> +        (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
>> +        if ((syncdiv + 1) <= sl) {
>> +            dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
>> +            return -EINVAL;
>> +        }
>> +    }
>> +
>> +    jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
>> +    return 0;
>> +}
>> +
>> +static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
>> +                 struct snd_pcm_substream *substream)
>> +{
>> +    u32 datarx, datatx;
>> +    int ret;
>> +
>> +    ret = jh7110_tdm_syncdiv(tdm);
>> +    if (ret)
>> +        return ret;
>> +
>> +    datarx = (tdm->rx.ifl << IFL_BIT) |
>> +          (tdm->rx.wl << WL_BIT) |
>> +          (tdm->rx.sscale << SSCALE_BIT) |
>> +          (tdm->rx.sl << SL_BIT) |
>> +          (tdm->rx.lrj << LRJ_BIT);
>> +
>> +    datatx = (tdm->tx.ifl << IFL_BIT) |
>> +          (tdm->tx.wl << WL_BIT) |
>> +          (tdm->tx.sscale << SSCALE_BIT) |
>> +          (tdm->tx.sl << SL_BIT) |
>> +          (tdm->tx.lrj << LRJ_BIT);
>> +
>> +    if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +        jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
>> +    else
>> +        jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
>> +
>> +    return 0;
>> +}
>> +
>> +static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
>> +{
>> +    int i;
>> +
>> +    for (i = tdm->num_clks - 1; i >= 0; i--)
>> +        clk_disable_unprepare(tdm->clks[i]);
>> +}
>> +
>> +static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
>> +{
>> +    int i, ret;
>> +
>> +    for (i = 0; i < tdm->num_clks; i++) {
>> +        ret = clk_prepare_enable(tdm->clks[i]);
>> +        if (ret) {
>> +            while (i-- > 0)
>> +                clk_disable_unprepare(tdm->clks[i]);
>> +            return ret;
>> +        }
>> +    }
>> +
>> +    ret = reset_control_deassert(tdm->resets);
>> +    if (ret) {
>> +        dev_err(tdm->dev, "Failed to deassert tdm resets\n");
>> +        goto dis_tdm_clk;
>> +    }
>> +
>> +    /* select tdm_ext clock as the clock source for tdm */
>> +    ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
>> +    if (ret) {
>> +        dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
>> +        goto dis_tdm_clk;
>> +    }
>> +    return 0;
>> +
>> +dis_tdm_clk:
>> +    for (i = tdm->num_clks - 1; i >= 0; i--)
>> +        clk_disable_unprepare(tdm->clks[i]);
>> +
>> +    return ret;
>> +}
>> +
>> +#ifdef CONFIG_PM
>> +static int jh7110_tdm_runtime_suspend(struct device *dev)
>> +{
>> +    struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
>> +
>> +    jh7110_tdm_clk_disable(tdm);
>> +    return 0;
>> +}
>> +
>> +static int jh7110_tdm_runtime_resume(struct device *dev)
>> +{
>> +    struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
>> +
>> +    return jh7110_tdm_clk_enable(tdm);
>> +}
>> +#endif
>> +
>> +#ifdef CONFIG_PM_SLEEP
>> +static int jh7110_tdm_suspend(struct snd_soc_component *component)
>> +{
>> +    /* save context */
>> +    tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
>> +    tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);
> 
> tdm isn't declared here. Is that same with resume function?

OMG! Maybe I accidentally deleted the declaration of tdm while deleting some debugging statement.
Yes, it is same with resume function.

Best regards,
Walker

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

* Re: [PATCH v3 1/3] dt-bindings: sound: Add TDM for StarFive JH7110
  2023-05-08  1:30     ` Mark Brown
  (?)
@ 2023-05-08  2:44       ` Walker Chen
  -1 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-08  2:44 UTC (permalink / raw)
  To: Mark Brown
  Cc: Liam Girdwood, Jaroslav Kysela, Takashi Iwai, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Emil Renner Berthing,
	alsa-devel, devicetree, linux-kernel, linux-riscv



On 2023/5/8 9:30, Mark Brown wrote:
> On Sat, May 06, 2023 at 05:01:14PM +0800, Walker Chen wrote:
>> Add bindings to describe the TDM driver for the StarFive JH7110 SoC.
> 
> Please submit patches using subject lines reflecting the style for the
> subsystem, this makes it easier for people to identify relevant patches.
> Look at what existing commits in the area you're changing are doing and
> make sure your subject lines visually resemble what they're doing.
> There's no need to resubmit to fix this alone.

Thanks for your reminder, I will pay attention to this when submitting
 patches in the future.

Best regards,
Walker

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

* Re: [PATCH v3 1/3] dt-bindings: sound: Add TDM for StarFive JH7110
@ 2023-05-08  2:44       ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-08  2:44 UTC (permalink / raw)
  To: Mark Brown
  Cc: Liam Girdwood, Jaroslav Kysela, Takashi Iwai, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Emil Renner Berthing,
	alsa-devel, devicetree, linux-kernel, linux-riscv



On 2023/5/8 9:30, Mark Brown wrote:
> On Sat, May 06, 2023 at 05:01:14PM +0800, Walker Chen wrote:
>> Add bindings to describe the TDM driver for the StarFive JH7110 SoC.
> 
> Please submit patches using subject lines reflecting the style for the
> subsystem, this makes it easier for people to identify relevant patches.
> Look at what existing commits in the area you're changing are doing and
> make sure your subject lines visually resemble what they're doing.
> There's no need to resubmit to fix this alone.

Thanks for your reminder, I will pay attention to this when submitting
 patches in the future.

Best regards,
Walker

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 1/3] dt-bindings: sound: Add TDM for StarFive JH7110
@ 2023-05-08  2:44       ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-08  2:44 UTC (permalink / raw)
  To: Mark Brown
  Cc: Liam Girdwood, Takashi Iwai, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Emil Renner Berthing, alsa-devel, devicetree,
	linux-kernel, linux-riscv



On 2023/5/8 9:30, Mark Brown wrote:
> On Sat, May 06, 2023 at 05:01:14PM +0800, Walker Chen wrote:
>> Add bindings to describe the TDM driver for the StarFive JH7110 SoC.
> 
> Please submit patches using subject lines reflecting the style for the
> subsystem, this makes it easier for people to identify relevant patches.
> Look at what existing commits in the area you're changing are doing and
> make sure your subject lines visually resemble what they're doing.
> There's no need to resubmit to fix this alone.

Thanks for your reminder, I will pay attention to this when submitting
 patches in the future.

Best regards,
Walker

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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
  2023-05-06  9:01   ` Walker Chen
@ 2023-05-09  4:33     ` Claudiu.Beznea
  -1 siblings, 0 replies; 48+ messages in thread
From: Claudiu.Beznea @ 2023-05-09  4:33 UTC (permalink / raw)
  To: walker.chen, broonie, lgirdwood, perex, tiwai, robh+dt,
	krzysztof.kozlowski+dt, Conor.Dooley, emil.renner.berthing
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

On 06.05.2023 12:01, Walker Chen wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> 
> Add tdm driver support for the StarFive JH7110 SoC.
> 
> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
> ---
>  MAINTAINERS                     |   6 +
>  sound/soc/Kconfig               |   1 +
>  sound/soc/Makefile              |   1 +
>  sound/soc/starfive/Kconfig      |  15 +
>  sound/soc/starfive/Makefile     |   2 +
>  sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
>  sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
>  7 files changed, 745 insertions(+)
>  create mode 100644 sound/soc/starfive/Kconfig
>  create mode 100644 sound/soc/starfive/Makefile
>  create mode 100644 sound/soc/starfive/jh7110_tdm.c
>  create mode 100644 sound/soc/starfive/jh7110_tdm.h
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 5f9c544bc189..add89615d327 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -19945,6 +19945,12 @@ F:     Documentation/devicetree/bindings/power/starfive*
>  F:     drivers/soc/starfive/jh71xx_pmu.c
>  F:     include/dt-bindings/power/starfive,jh7110-pmu.h
> 
> +STARFIVE JH7110 TDM DRIVERS
> +M:     Walker Chen <walker.chen@starfivetech.com>
> +S:     Maintained
> +F:     Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
> +F:     sound/soc/starfive/jh7110-tdm.*
> +
>  STARFIVE SOC DRIVERS
>  M:     Conor Dooley <conor@kernel.org>
>  S:     Maintained
> diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
> index 848fbae26c3b..8d1d9401ecf2 100644
> --- a/sound/soc/Kconfig
> +++ b/sound/soc/Kconfig
> @@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
>  source "sound/soc/sof/Kconfig"
>  source "sound/soc/spear/Kconfig"
>  source "sound/soc/sprd/Kconfig"
> +source "sound/soc/starfive/Kconfig"
>  source "sound/soc/sti/Kconfig"
>  source "sound/soc/stm/Kconfig"
>  source "sound/soc/sunxi/Kconfig"
> diff --git a/sound/soc/Makefile b/sound/soc/Makefile
> index 507eaed1d6a1..65aeb4ef4068 100644
> --- a/sound/soc/Makefile
> +++ b/sound/soc/Makefile
> @@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC) += sh/
>  obj-$(CONFIG_SND_SOC)  += sof/
>  obj-$(CONFIG_SND_SOC)  += spear/
>  obj-$(CONFIG_SND_SOC)  += sprd/
> +obj-$(CONFIG_SND_SOC)  += starfive/
>  obj-$(CONFIG_SND_SOC)  += sti/
>  obj-$(CONFIG_SND_SOC)  += stm/
>  obj-$(CONFIG_SND_SOC)  += sunxi/
> diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
> new file mode 100644
> index 000000000000..737c956f7b93
> --- /dev/null
> +++ b/sound/soc/starfive/Kconfig
> @@ -0,0 +1,15 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +config SND_SOC_STARFIVE
> +       tristate "Audio support for StarFive SoC"
> +       depends on COMPILE_TEST || SOC_STARFIVE
> +       help
> +         Say Y or M if you want to add support for codecs attached to
> +         the Starfive SoCs' Audio interfaces. You will also need to
> +         select the audio interfaces to support below.
> +
> +config SND_SOC_JH7110_TDM
> +       tristate "JH7110 TDM device driver"
> +       depends on HAVE_CLK && SND_SOC_STARFIVE
> +       select SND_SOC_GENERIC_DMAENGINE_PCM
> +       help
> +         Say Y or M if you want to add support for StarFive TDM driver.
> diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
> new file mode 100644
> index 000000000000..f7d960211d72
> --- /dev/null
> +++ b/sound/soc/starfive/Makefile
> @@ -0,0 +1,2 @@
> +# StarFive Platform Support
> +obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
> diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
> new file mode 100644
> index 000000000000..33f7cf43e4bd
> --- /dev/null
> +++ b/sound/soc/starfive/jh7110_tdm.c
> @@ -0,0 +1,573 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * jh7110_tdm.c -- StarFive JH7110 TDM driver
> + *
> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
> + *
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/device.h>
> +#include <linux/module.h>
> +#include <linux/of_irq.h>
> +#include <linux/of_platform.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/regmap.h>
> +#include <linux/reset.h>
> +#include <sound/initval.h>
> +#include <sound/pcm_params.h>
> +#include <sound/soc.h>
> +#include <sound/soc-dai.h>
> +#include "jh7110_tdm.h"
> +
> +static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
> +{
> +       return readl_relaxed(tdm->tdm_base + reg);
> +}
> +
> +static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
> +{
> +       writel_relaxed(val, tdm->tdm_base + reg);
> +}
> +
> +static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
> +                                   struct snd_pcm_substream *substream)
> +{
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +               tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
> +       else
> +               tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
> +}
> +
> +static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
> +                            struct snd_pcm_substream *substream)
> +{
> +       u32 data;
> +
> +       data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
> +
> +       /* restore context */
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
> +       else
> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
> +}
> +
> +static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
> +                           struct snd_pcm_substream *substream)
> +{
> +       unsigned int val;
> +
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> +               val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
> +               val &= ~PCMTXCR_TXEN;
> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
> +       } else {
> +               val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
> +               val &= ~PCMRXCR_RXEN;
> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
> +       }
> +}
> +
> +static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
> +{
> +       u32 sl, sscale, syncdiv;
> +
> +       if (tdm->rx.sl >= tdm->tx.sl)
> +               sl = tdm->rx.sl;
> +       else
> +               sl = tdm->tx.sl;
> +
> +       if (tdm->rx.sscale >= tdm->tx.sscale)
> +               sscale = tdm->rx.sscale;
> +       else
> +               sscale = tdm->tx.sscale;
> +
> +       syncdiv = tdm->pcmclk / tdm->samplerate - 1;
> +
> +       if ((syncdiv + 1) < (sl * sscale)) {
> +               dev_err(tdm->dev, "Failed to set syncdiv!\n");
> +               return -EINVAL;
> +       }
> +
> +       if (tdm->syncm == TDM_SYNCM_LONG &&
> +           (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
> +               if ((syncdiv + 1) <= sl) {
> +                       dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
> +                       return -EINVAL;
> +               }
> +       }
> +
> +       jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
> +       return 0;
> +}
> +
> +static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
> +                            struct snd_pcm_substream *substream)
> +{
> +       u32 datarx, datatx;
> +       int ret;
> +
> +       ret = jh7110_tdm_syncdiv(tdm);
> +       if (ret)
> +               return ret;
> +
> +       datarx = (tdm->rx.ifl << IFL_BIT) |
> +                 (tdm->rx.wl << WL_BIT) |
> +                 (tdm->rx.sscale << SSCALE_BIT) |
> +                 (tdm->rx.sl << SL_BIT) |
> +                 (tdm->rx.lrj << LRJ_BIT);
> +
> +       datatx = (tdm->tx.ifl << IFL_BIT) |
> +                 (tdm->tx.wl << WL_BIT) |
> +                 (tdm->tx.sscale << SSCALE_BIT) |
> +                 (tdm->tx.sl << SL_BIT) |
> +                 (tdm->tx.lrj << LRJ_BIT);
> +
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
> +       else
> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
> +
> +       return 0;
> +}
> +
> +static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
> +{
> +       int i;
> +
> +       for (i = tdm->num_clks - 1; i >= 0; i--)
> +               clk_disable_unprepare(tdm->clks[i]);
> +}

This could be replaced by clk_bulk_disable_unprepare().

> +
> +static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
> +{
> +       int i, ret;
> +
> +       for (i = 0; i < tdm->num_clks; i++) {
> +               ret = clk_prepare_enable(tdm->clks[i]);
> +               if (ret) {
> +                       while (i-- > 0)
> +                               clk_disable_unprepare(tdm->clks[i]);
> +                       return ret;
> +               }
> +       }


And this could be replaced by clk_bulk_prepare_enable().

> +
> +       ret = reset_control_deassert(tdm->resets);
> +       if (ret) {
> +               dev_err(tdm->dev, "Failed to deassert tdm resets\n");
> +               goto dis_tdm_clk;
> +       }
> +
> +       /* select tdm_ext clock as the clock source for tdm */
> +       ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
> +       if (ret) {
> +               dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
> +               goto dis_tdm_clk;
> +       }
> +       return 0;
> +
> +dis_tdm_clk:
> +       for (i = tdm->num_clks - 1; i >= 0; i--)
> +               clk_disable_unprepare(tdm->clks[i]);

clk_bulk_disable_unprepare()

> +
> +       return ret;
> +}
> +
> +#ifdef CONFIG_PM
> +static int jh7110_tdm_runtime_suspend(struct device *dev)
> +{
> +       struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
> +
> +       jh7110_tdm_clk_disable(tdm);
> +       return 0;
> +}
> +
> +static int jh7110_tdm_runtime_resume(struct device *dev)
> +{
> +       struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
> +
> +       return jh7110_tdm_clk_enable(tdm);
> +}
> +#endif
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int jh7110_tdm_suspend(struct snd_soc_component *component)
> +{
> +       /* save context */
> +       tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
> +       tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);
> +
> +       return pm_runtime_force_suspend(component->dev);
> +}
> +
> +static int jh7110_tdm_resume(struct snd_soc_component *component)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_component_get_drvdata(component);
> +
> +       /* restore context */
> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, tdm->saved_pcmgbcr);
> +       jh7110_tdm_writel(tdm, TDM_PCMDIV, tdm->saved_pcmdiv);
> +
> +       return pm_runtime_force_resume(component->dev);
> +}
> +
> +#else
> +#define jh7110_tdm_suspend     NULL
> +#define jh7110_tdm_resume      NULL
> +#endif

you may use pm_sleep_ptr() to avoid these.

> +
> +static const struct snd_soc_component_driver jh7110_tdm_component = {
> +       .name = "jh7110-tdm",
> +       .suspend = jh7110_tdm_suspend,
> +       .resume = jh7110_tdm_resume,
> +};
> +
> +static int jh7110_tdm_startup(struct snd_pcm_substream *substream,
> +                             struct snd_soc_dai *cpu_dai)
> +{
> +       struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
> +       struct snd_soc_dai_link *dai_link = rtd->dai_link;
> +
> +       dai_link->stop_dma_first = 1;
> +
> +       return 0;
> +}
> +
> +static int jh7110_tdm_hw_params(struct snd_pcm_substream *substream,
> +                               struct snd_pcm_hw_params *params,
> +                               struct snd_soc_dai *dai)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +       int chan_wl, chan_sl, chan_nr;
> +       unsigned int data_width;
> +       unsigned int dma_bus_width;
> +       struct snd_dmaengine_dai_dma_data *dma_data = NULL;
> +       int ret = 0;
> +
> +       data_width = params_width(params);
> +
> +       tdm->samplerate = params_rate(params);
> +       tdm->pcmclk = params_channels(params) * tdm->samplerate * data_width;
> +
> +       switch (params_format(params)) {
> +       case SNDRV_PCM_FORMAT_S16_LE:
> +               chan_wl = TDM_16BIT_WORD_LEN;
> +               chan_sl = TDM_16BIT_SLOT_LEN;
> +               dma_bus_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +               break;
> +
> +       case SNDRV_PCM_FORMAT_S32_LE:
> +               chan_wl = TDM_32BIT_WORD_LEN;
> +               chan_sl = TDM_32BIT_SLOT_LEN;
> +               dma_bus_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
> +               break;
> +
> +       default:
> +               dev_err(tdm->dev, "tdm: unsupported PCM fmt");
> +               return -EINVAL;
> +       }
> +
> +       chan_nr = params_channels(params);
> +       switch (chan_nr) {
> +       case 1:
> +       case 2:
> +       case 4:
> +       case 6:
> +       case 8:
> +               break;
> +       default:
> +               dev_err(tdm->dev, "channel not supported\n");
> +               return -EINVAL;
> +       }
> +
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> +               tdm->tx.wl = chan_wl;
> +               tdm->tx.sl = chan_sl;
> +               tdm->tx.sscale = chan_nr;
> +               tdm->play_dma_data.addr_width = dma_bus_width;
> +               dma_data = &tdm->play_dma_data;
> +       } else {
> +               tdm->rx.wl = chan_wl;
> +               tdm->rx.sl = chan_sl;
> +               tdm->rx.sscale = chan_nr;
> +               tdm->capture_dma_data.addr_width = dma_bus_width;
> +               dma_data = &tdm->capture_dma_data;
> +       }
> +
> +       snd_soc_dai_set_dma_data(dai, substream, dma_data);
> +
> +       ret = jh7110_tdm_config(tdm, substream);
> +       if (ret)
> +               return ret;
> +
> +       jh7110_tdm_save_context(tdm, substream);
> +       return 0;
> +}
> +
> +static int jh7110_tdm_trigger(struct snd_pcm_substream *substream,
> +                             int cmd, struct snd_soc_dai *dai)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +       int ret = 0;
> +
> +       switch (cmd) {
> +       case SNDRV_PCM_TRIGGER_START:
> +       case SNDRV_PCM_TRIGGER_RESUME:
> +       case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
> +               jh7110_tdm_start(tdm, substream);
> +               break;
> +
> +       case SNDRV_PCM_TRIGGER_STOP:
> +       case SNDRV_PCM_TRIGGER_SUSPEND:
> +       case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
> +               jh7110_tdm_stop(tdm, substream);
> +               break;
> +       default:
> +               ret = -EINVAL;
> +               break;
> +       }
> +       return ret;
> +}
> +
> +static int jh7110_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
> +                                 unsigned int fmt)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(cpu_dai);
> +       unsigned int gbcr;
> +       int ret = 0;
> +
> +       /* set master/slave audio interface */
> +       switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
> +       case SND_SOC_DAIFMT_BP_FP:
> +               /* cpu is master */
> +               tdm->ms_mode = TDM_AS_MASTER;
> +               break;
> +       case SND_SOC_DAIFMT_BC_FC:
> +               /* codec is master */
> +               tdm->ms_mode = TDM_AS_SLAVE;
> +               break;
> +       case SND_SOC_DAIFMT_BC_FP:
> +       case SND_SOC_DAIFMT_BP_FC:
> +               ret = -EINVAL;
> +               break;
> +       default:
> +               dev_dbg(tdm->dev, "dwc : Invalid clock provider format\n");
> +               ret = -EINVAL;
> +               break;
> +       }
> +
> +       gbcr = (tdm->clkpolity << CLKPOL_BIT) |
> +               (tdm->elm << ELM_BIT) |
> +               (tdm->syncm << SYNCM_BIT) |
> +               (tdm->ms_mode << MS_BIT);
> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, gbcr);
> +
> +       return ret;
> +}
> +
> +static const struct snd_soc_dai_ops jh7110_tdm_dai_ops = {
> +       .startup        = jh7110_tdm_startup,
> +       .hw_params      = jh7110_tdm_hw_params,
> +       .trigger        = jh7110_tdm_trigger,
> +       .set_fmt        = jh7110_tdm_set_dai_fmt,
> +};
> +
> +static int jh7110_tdm_dai_probe(struct snd_soc_dai *dai)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +
> +       snd_soc_dai_init_dma_data(dai, &tdm->play_dma_data, &tdm->capture_dma_data);
> +       snd_soc_dai_set_drvdata(dai, tdm);
> +       return 0;
> +}
> +
> +#define JH7110_TDM_RATES       SNDRV_PCM_RATE_8000_48000
> +
> +#define JH7110_TDM_FORMATS     (SNDRV_PCM_FMTBIT_S16_LE | \
> +                                SNDRV_PCM_FMTBIT_S32_LE)
> +
> +static struct snd_soc_dai_driver jh7110_tdm_dai = {
> +       .name = "sf_tdm",
> +       .id = 0,
> +       .playback = {
> +               .stream_name    = "Playback",
> +               .channels_min   = 1,
> +               .channels_max   = 8,
> +               .rates          = JH7110_TDM_RATES,
> +               .formats        = JH7110_TDM_FORMATS,
> +       },
> +       .capture = {
> +               .stream_name    = "Capture",
> +               .channels_min   = 1,
> +               .channels_max   = 8,
> +               .rates          = JH7110_TDM_RATES,
> +               .formats        = JH7110_TDM_FORMATS,
> +       },
> +       .ops = &jh7110_tdm_dai_ops,
> +       .probe = jh7110_tdm_dai_probe,
> +       .symmetric_rate = 1,
> +};
> +
> +static const struct snd_pcm_hardware jh7110_pcm_hardware = {
> +       .info                   = (SNDRV_PCM_INFO_MMAP          |
> +                                  SNDRV_PCM_INFO_MMAP_VALID    |
> +                                  SNDRV_PCM_INFO_PAUSE         |
> +                                  SNDRV_PCM_INFO_RESUME        |
> +                                  SNDRV_PCM_INFO_INTERLEAVED   |
> +                                  SNDRV_PCM_INFO_BLOCK_TRANSFER),
> +       .buffer_bytes_max       = 192512,
> +       .period_bytes_min       = 4096,
> +       .period_bytes_max       = 32768,
> +       .periods_min            = 1,
> +       .periods_max            = 48,
> +       .fifo_size              = 16,
> +};
> +
> +static const struct snd_dmaengine_pcm_config jh7110_dmaengine_pcm_config = {
> +       .pcm_hardware = &jh7110_pcm_hardware,
> +       .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
> +       .prealloc_buffer_size = 192512,
> +};
> +
> +static void jh7110_tdm_init_params(struct jh7110_tdm_dev *tdm)
> +{
> +       tdm->clkpolity = TDM_TX_RASING_RX_FALLING;
> +       tdm->elm = TDM_ELM_LATE;
> +       tdm->syncm = TDM_SYNCM_SHORT;
> +
> +       tdm->rx.ifl = TDM_FIFO_HALF;
> +       tdm->tx.ifl = TDM_FIFO_HALF;
> +       tdm->rx.wl = TDM_16BIT_WORD_LEN;
> +       tdm->tx.wl = TDM_16BIT_WORD_LEN;
> +       tdm->rx.sscale = 2;
> +       tdm->tx.sscale = 2;
> +       tdm->rx.lrj = TDM_LEFT_JUSTIFT;
> +       tdm->tx.lrj = TDM_LEFT_JUSTIFT;
> +
> +       tdm->play_dma_data.addr = JH7110_TDM_FIFO;
> +       tdm->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +       tdm->play_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
> +       tdm->play_dma_data.maxburst = 16;
> +
> +       tdm->capture_dma_data.addr = JH7110_TDM_FIFO;
> +       tdm->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +       tdm->capture_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
> +       tdm->capture_dma_data.maxburst = 8;
> +}
> +
> +static int jh7110_tdm_clk_reset_init(struct platform_device *pdev,
> +                                    struct jh7110_tdm_dev *tdm)
> +{
> +       int i, ret;
> +
> +       for (i = 0; i < tdm->num_clks; i++) {
> +               tdm->clks[i] = devm_clk_get(&pdev->dev, tdm->clk_names[i]);
> +               if (IS_ERR(tdm->clks[i])) {
> +                       dev_err(&pdev->dev, "Failed to get clock: %s\n",
> +                               tdm->clk_names[i]);
> +                       return PTR_ERR(tdm->clks[i]);
> +               }
> +       }

devm_clk_bulk_get() ?

> +
> +       tdm->resets = devm_reset_control_array_get_exclusive(&pdev->dev);
> +       if (IS_ERR(tdm->resets)) {
> +               ret = PTR_ERR(tdm->resets);
> +               dev_err(&pdev->dev, "Failed to get tdm resets");
> +               return ret;
> +       }
> +
> +       return jh7110_tdm_clk_enable(tdm);
> +}
> +
> +static int jh7110_tdm_probe(struct platform_device *pdev)
> +{
> +       struct jh7110_tdm_dev *tdm;
> +       const struct starfive_tdm_driverdata *driver_data;
> +       int ret;
> +
> +       tdm = devm_kzalloc(&pdev->dev, sizeof(*tdm), GFP_KERNEL);
> +       if (!tdm)
> +               return -ENOMEM;
> +
> +       driver_data = of_device_get_match_data(&pdev->dev);
> +       tdm->clk_names = (const char **)driver_data->clk_names;
> +       tdm->num_clks = driver_data->num_clks;
> +
> +       tdm->tdm_base = devm_platform_ioremap_resource(pdev, 0);
> +       if (IS_ERR(tdm->tdm_base))
> +               return PTR_ERR(tdm->tdm_base);
> +
> +       tdm->dev = &pdev->dev;
> +
> +       ret = jh7110_tdm_clk_reset_init(pdev, tdm);
> +       if (ret) {
> +               dev_err(&pdev->dev, "Failed to enable audio-tdm clock\n");
> +               return ret;
> +       }
> +
> +       jh7110_tdm_init_params(tdm);
> +
> +       dev_set_drvdata(&pdev->dev, tdm);
> +       ret = devm_snd_soc_register_component(&pdev->dev, &jh7110_tdm_component,
> +                                             &jh7110_tdm_dai, 1);
> +       if (ret != 0) {

if (ret)

> +               dev_err(&pdev->dev, "Failed to register dai\n");
> +               return ret;
> +       }
> +
> +       ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
> +                                             &jh7110_dmaengine_pcm_config,
> +                                             SND_DMAENGINE_PCM_FLAG_COMPAT);
> +       if (ret) {
> +               dev_err(&pdev->dev, "Could not register pcm: %d\n", ret);
> +               return ret;
> +       }
> +
> +       pm_runtime_enable(&pdev->dev);
> +#ifdef CONFIG_PM
> +       jh7110_tdm_clk_disable(tdm);
> +#endif
> +
> +       return 0;
> +}
> +
> +static int jh7110_tdm_dev_remove(struct platform_device *pdev)
> +{
> +       pm_runtime_disable(&pdev->dev);
> +       return 0;
> +}
> +
> +static const struct starfive_tdm_driverdata jh7110_drvdata = {
> +       .clk_names = {"mclk_inner", "tdm_ahb", "tdm_apb", "tdm_internal", "tdm_ext", "tdm"},
> +       .num_clks = 6,
> +};
> +
> +static const struct of_device_id jh7110_tdm_of_match[] = {
> +       { .compatible = "starfive,jh7110-tdm", .data = &jh7110_drvdata },
> +       {}
> +};
> +
> +MODULE_DEVICE_TABLE(of, jh7110_tdm_of_match);
> +
> +static const struct dev_pm_ops jh7110_tdm_pm_ops = {
> +       SET_RUNTIME_PM_OPS(jh7110_tdm_runtime_suspend,
> +                          jh7110_tdm_runtime_resume, NULL)

You can use RUNTIME_PM_OPS() and avoid having #ifdef CONFIG_PM around
jh7110_tdm_runtime_suspend and jh7110_tdm_runtime_resume definitions.

> +};
> +
> +static struct platform_driver jh7110_tdm_driver = {
> +       .driver = {
> +               .name = "jh7110-tdm",
> +               .of_match_table = jh7110_tdm_of_match,
> +               .pm = &jh7110_tdm_pm_ops,
> +       },
> +       .probe = jh7110_tdm_probe,
> +       .remove = jh7110_tdm_dev_remove,
> +};
> +module_platform_driver(jh7110_tdm_driver);
> +
> +MODULE_DESCRIPTION("StarFive JH7110 TDM ASoC Driver");
> +MODULE_AUTHOR("Walker Chen <walker.chen@starfivetech.com>");
> +MODULE_LICENSE("GPL");
> diff --git a/sound/soc/starfive/jh7110_tdm.h b/sound/soc/starfive/jh7110_tdm.h
> new file mode 100644
> index 000000000000..80f17946a45d
> --- /dev/null
> +++ b/sound/soc/starfive/jh7110_tdm.h
> @@ -0,0 +1,147 @@
> +/* SPDX-License-Identifier: GPL-2.0
> + *
> + * TDM driver for the StarFive JH7110 SoC
> + *
> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
> + *
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + */
> +
> +#ifndef __SND_SOC_STARFIVE_TDM_H
> +#define __SND_SOC_STARFIVE_TDM_H
> +
> +#include <linux/clk.h>
> +#include <linux/device.h>
> +#include <linux/types.h>
> +#include <sound/dmaengine_pcm.h>
> +#include <sound/pcm.h>
> +#include <linux/dmaengine.h>
> +#include <linux/types.h>
> +
> +#define TDM_PCMGBCR                    0x00
> +       #define PCMGBCR_MASK            0x1e
> +       #define PCMGBCR_ENABLE          BIT(0)
> +       #define PCMGBCR_TRITXEN         BIT(4)
> +       #define CLKPOL_BIT              5
> +       #define TRITXEN_BIT             4
> +       #define ELM_BIT                 3
> +       #define SYNCM_BIT               2
> +       #define MS_BIT                  1
> +#define TDM_PCMTXCR                    0x04
> +       #define PCMTXCR_TXEN            BIT(0)
> +       #define IFL_BIT                 11
> +       #define WL_BIT                  8
> +       #define SSCALE_BIT              4
> +       #define SL_BIT                  2
> +       #define LRJ_BIT                 1
> +#define TDM_PCMRXCR                    0x08
> +       #define PCMRXCR_RXEN            BIT(0)
> +       #define PCMRXCR_RXSL_MASK       0xc
> +       #define PCMRXCR_RXSL_16BIT      0x4
> +       #define PCMRXCR_RXSL_32BIT      0x8
> +       #define PCMRXCR_SCALE_MASK      0xf0
> +       #define PCMRXCR_SCALE_1CH       0x10
> +#define TDM_PCMDIV                     0x0c
> +
> +/*  DMA registers */
> +#define JH7110_TDM_FIFO                        0x170c0000
> +#define JH7110_TDM_FIFO_DEPTH          32
> +#define JH7110_TDM_MAX_CLOCKS          6
> +
> +enum TDM_MASTER_SLAVE_MODE {
> +       TDM_AS_MASTER = 0,
> +       TDM_AS_SLAVE,
> +};
> +
> +enum TDM_CLKPOL {
> +       /* tx raising and rx falling */
> +       TDM_TX_RASING_RX_FALLING = 0,
> +       /* tx falling and rx raising */
> +       TDM_TX_FALLING_RX_RASING,
> +};
> +
> +enum TDM_ELM {
> +       /* only work while SYNCM=0 */
> +       TDM_ELM_LATE = 0,
> +       TDM_ELM_EARLY,
> +};
> +
> +enum TDM_SYNCM {
> +       /* short frame sync */
> +       TDM_SYNCM_SHORT = 0,
> +       /* long frame sync */
> +       TDM_SYNCM_LONG,
> +};
> +
> +enum TDM_IFL {
> +       /* FIFO to send or received : half-1/2, Quarter-1/4 */
> +       TDM_FIFO_HALF = 0,
> +       TDM_FIFO_QUARTER,
> +};
> +
> +enum TDM_WL {
> +       /* send or received word length */
> +       TDM_8BIT_WORD_LEN = 0,
> +       TDM_16BIT_WORD_LEN,
> +       TDM_20BIT_WORD_LEN,
> +       TDM_24BIT_WORD_LEN,
> +       TDM_32BIT_WORD_LEN,
> +};
> +
> +enum TDM_SL {
> +       /* send or received slot length */
> +       TDM_8BIT_SLOT_LEN = 0,
> +       TDM_16BIT_SLOT_LEN,
> +       TDM_32BIT_SLOT_LEN,
> +};
> +
> +enum TDM_LRJ {
> +       /* left-justify or right-justify */
> +       TDM_RIGHT_JUSTIFY = 0,
> +       TDM_LEFT_JUSTIFT,
> +};
> +
> +struct tdm_chan_cfg {
> +       enum TDM_IFL ifl;
> +       enum TDM_WL  wl;
> +       unsigned char sscale;
> +       enum TDM_SL  sl;
> +       enum TDM_LRJ lrj;
> +       unsigned char enable;
> +};
> +
> +struct starfive_tdm_driverdata {
> +       const char *clk_names[JH7110_TDM_MAX_CLOCKS];
> +       int num_clks;
> +};
> +
> +struct jh7110_tdm_dev {
> +       void __iomem *tdm_base;
> +       struct device *dev;
> +       const char **clk_names;
> +       struct clk *clks[JH7110_TDM_MAX_CLOCKS];
> +       int num_clks;
> +       struct reset_control *resets;
> +
> +       enum TDM_CLKPOL clkpolity;
> +       enum TDM_ELM    elm;
> +       enum TDM_SYNCM  syncm;
> +       enum TDM_MASTER_SLAVE_MODE ms_mode;
> +
> +       struct tdm_chan_cfg tx;
> +       struct tdm_chan_cfg rx;
> +
> +       u16 syncdiv;
> +       u32 samplerate;
> +       u32 pcmclk;
> +
> +       /* data related to DMA transfers b/w tdm and DMAC */
> +       struct snd_dmaengine_dai_dma_data play_dma_data;
> +       struct snd_dmaengine_dai_dma_data capture_dma_data;
> +       u32 saved_pcmgbcr;
> +       u32 saved_pcmtxcr;
> +       u32 saved_pcmrxcr;
> +       u32 saved_pcmdiv;
> +};
> +
> +#endif /* __SND_SOC_STARFIVE_TDM_H */
> --
> 2.17.1
> 


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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
@ 2023-05-09  4:33     ` Claudiu.Beznea
  0 siblings, 0 replies; 48+ messages in thread
From: Claudiu.Beznea @ 2023-05-09  4:33 UTC (permalink / raw)
  To: walker.chen, broonie, lgirdwood, perex, tiwai, robh+dt,
	krzysztof.kozlowski+dt, Conor.Dooley, emil.renner.berthing
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

On 06.05.2023 12:01, Walker Chen wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> 
> Add tdm driver support for the StarFive JH7110 SoC.
> 
> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
> ---
>  MAINTAINERS                     |   6 +
>  sound/soc/Kconfig               |   1 +
>  sound/soc/Makefile              |   1 +
>  sound/soc/starfive/Kconfig      |  15 +
>  sound/soc/starfive/Makefile     |   2 +
>  sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
>  sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
>  7 files changed, 745 insertions(+)
>  create mode 100644 sound/soc/starfive/Kconfig
>  create mode 100644 sound/soc/starfive/Makefile
>  create mode 100644 sound/soc/starfive/jh7110_tdm.c
>  create mode 100644 sound/soc/starfive/jh7110_tdm.h
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 5f9c544bc189..add89615d327 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -19945,6 +19945,12 @@ F:     Documentation/devicetree/bindings/power/starfive*
>  F:     drivers/soc/starfive/jh71xx_pmu.c
>  F:     include/dt-bindings/power/starfive,jh7110-pmu.h
> 
> +STARFIVE JH7110 TDM DRIVERS
> +M:     Walker Chen <walker.chen@starfivetech.com>
> +S:     Maintained
> +F:     Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
> +F:     sound/soc/starfive/jh7110-tdm.*
> +
>  STARFIVE SOC DRIVERS
>  M:     Conor Dooley <conor@kernel.org>
>  S:     Maintained
> diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
> index 848fbae26c3b..8d1d9401ecf2 100644
> --- a/sound/soc/Kconfig
> +++ b/sound/soc/Kconfig
> @@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
>  source "sound/soc/sof/Kconfig"
>  source "sound/soc/spear/Kconfig"
>  source "sound/soc/sprd/Kconfig"
> +source "sound/soc/starfive/Kconfig"
>  source "sound/soc/sti/Kconfig"
>  source "sound/soc/stm/Kconfig"
>  source "sound/soc/sunxi/Kconfig"
> diff --git a/sound/soc/Makefile b/sound/soc/Makefile
> index 507eaed1d6a1..65aeb4ef4068 100644
> --- a/sound/soc/Makefile
> +++ b/sound/soc/Makefile
> @@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC) += sh/
>  obj-$(CONFIG_SND_SOC)  += sof/
>  obj-$(CONFIG_SND_SOC)  += spear/
>  obj-$(CONFIG_SND_SOC)  += sprd/
> +obj-$(CONFIG_SND_SOC)  += starfive/
>  obj-$(CONFIG_SND_SOC)  += sti/
>  obj-$(CONFIG_SND_SOC)  += stm/
>  obj-$(CONFIG_SND_SOC)  += sunxi/
> diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
> new file mode 100644
> index 000000000000..737c956f7b93
> --- /dev/null
> +++ b/sound/soc/starfive/Kconfig
> @@ -0,0 +1,15 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +config SND_SOC_STARFIVE
> +       tristate "Audio support for StarFive SoC"
> +       depends on COMPILE_TEST || SOC_STARFIVE
> +       help
> +         Say Y or M if you want to add support for codecs attached to
> +         the Starfive SoCs' Audio interfaces. You will also need to
> +         select the audio interfaces to support below.
> +
> +config SND_SOC_JH7110_TDM
> +       tristate "JH7110 TDM device driver"
> +       depends on HAVE_CLK && SND_SOC_STARFIVE
> +       select SND_SOC_GENERIC_DMAENGINE_PCM
> +       help
> +         Say Y or M if you want to add support for StarFive TDM driver.
> diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
> new file mode 100644
> index 000000000000..f7d960211d72
> --- /dev/null
> +++ b/sound/soc/starfive/Makefile
> @@ -0,0 +1,2 @@
> +# StarFive Platform Support
> +obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
> diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
> new file mode 100644
> index 000000000000..33f7cf43e4bd
> --- /dev/null
> +++ b/sound/soc/starfive/jh7110_tdm.c
> @@ -0,0 +1,573 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * jh7110_tdm.c -- StarFive JH7110 TDM driver
> + *
> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
> + *
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/device.h>
> +#include <linux/module.h>
> +#include <linux/of_irq.h>
> +#include <linux/of_platform.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/regmap.h>
> +#include <linux/reset.h>
> +#include <sound/initval.h>
> +#include <sound/pcm_params.h>
> +#include <sound/soc.h>
> +#include <sound/soc-dai.h>
> +#include "jh7110_tdm.h"
> +
> +static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
> +{
> +       return readl_relaxed(tdm->tdm_base + reg);
> +}
> +
> +static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
> +{
> +       writel_relaxed(val, tdm->tdm_base + reg);
> +}
> +
> +static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
> +                                   struct snd_pcm_substream *substream)
> +{
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +               tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
> +       else
> +               tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
> +}
> +
> +static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
> +                            struct snd_pcm_substream *substream)
> +{
> +       u32 data;
> +
> +       data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
> +
> +       /* restore context */
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
> +       else
> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
> +}
> +
> +static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
> +                           struct snd_pcm_substream *substream)
> +{
> +       unsigned int val;
> +
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> +               val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
> +               val &= ~PCMTXCR_TXEN;
> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
> +       } else {
> +               val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
> +               val &= ~PCMRXCR_RXEN;
> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
> +       }
> +}
> +
> +static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
> +{
> +       u32 sl, sscale, syncdiv;
> +
> +       if (tdm->rx.sl >= tdm->tx.sl)
> +               sl = tdm->rx.sl;
> +       else
> +               sl = tdm->tx.sl;
> +
> +       if (tdm->rx.sscale >= tdm->tx.sscale)
> +               sscale = tdm->rx.sscale;
> +       else
> +               sscale = tdm->tx.sscale;
> +
> +       syncdiv = tdm->pcmclk / tdm->samplerate - 1;
> +
> +       if ((syncdiv + 1) < (sl * sscale)) {
> +               dev_err(tdm->dev, "Failed to set syncdiv!\n");
> +               return -EINVAL;
> +       }
> +
> +       if (tdm->syncm == TDM_SYNCM_LONG &&
> +           (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
> +               if ((syncdiv + 1) <= sl) {
> +                       dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
> +                       return -EINVAL;
> +               }
> +       }
> +
> +       jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
> +       return 0;
> +}
> +
> +static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
> +                            struct snd_pcm_substream *substream)
> +{
> +       u32 datarx, datatx;
> +       int ret;
> +
> +       ret = jh7110_tdm_syncdiv(tdm);
> +       if (ret)
> +               return ret;
> +
> +       datarx = (tdm->rx.ifl << IFL_BIT) |
> +                 (tdm->rx.wl << WL_BIT) |
> +                 (tdm->rx.sscale << SSCALE_BIT) |
> +                 (tdm->rx.sl << SL_BIT) |
> +                 (tdm->rx.lrj << LRJ_BIT);
> +
> +       datatx = (tdm->tx.ifl << IFL_BIT) |
> +                 (tdm->tx.wl << WL_BIT) |
> +                 (tdm->tx.sscale << SSCALE_BIT) |
> +                 (tdm->tx.sl << SL_BIT) |
> +                 (tdm->tx.lrj << LRJ_BIT);
> +
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
> +       else
> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
> +
> +       return 0;
> +}
> +
> +static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
> +{
> +       int i;
> +
> +       for (i = tdm->num_clks - 1; i >= 0; i--)
> +               clk_disable_unprepare(tdm->clks[i]);
> +}

This could be replaced by clk_bulk_disable_unprepare().

> +
> +static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
> +{
> +       int i, ret;
> +
> +       for (i = 0; i < tdm->num_clks; i++) {
> +               ret = clk_prepare_enable(tdm->clks[i]);
> +               if (ret) {
> +                       while (i-- > 0)
> +                               clk_disable_unprepare(tdm->clks[i]);
> +                       return ret;
> +               }
> +       }


And this could be replaced by clk_bulk_prepare_enable().

> +
> +       ret = reset_control_deassert(tdm->resets);
> +       if (ret) {
> +               dev_err(tdm->dev, "Failed to deassert tdm resets\n");
> +               goto dis_tdm_clk;
> +       }
> +
> +       /* select tdm_ext clock as the clock source for tdm */
> +       ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
> +       if (ret) {
> +               dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
> +               goto dis_tdm_clk;
> +       }
> +       return 0;
> +
> +dis_tdm_clk:
> +       for (i = tdm->num_clks - 1; i >= 0; i--)
> +               clk_disable_unprepare(tdm->clks[i]);

clk_bulk_disable_unprepare()

> +
> +       return ret;
> +}
> +
> +#ifdef CONFIG_PM
> +static int jh7110_tdm_runtime_suspend(struct device *dev)
> +{
> +       struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
> +
> +       jh7110_tdm_clk_disable(tdm);
> +       return 0;
> +}
> +
> +static int jh7110_tdm_runtime_resume(struct device *dev)
> +{
> +       struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
> +
> +       return jh7110_tdm_clk_enable(tdm);
> +}
> +#endif
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int jh7110_tdm_suspend(struct snd_soc_component *component)
> +{
> +       /* save context */
> +       tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
> +       tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);
> +
> +       return pm_runtime_force_suspend(component->dev);
> +}
> +
> +static int jh7110_tdm_resume(struct snd_soc_component *component)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_component_get_drvdata(component);
> +
> +       /* restore context */
> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, tdm->saved_pcmgbcr);
> +       jh7110_tdm_writel(tdm, TDM_PCMDIV, tdm->saved_pcmdiv);
> +
> +       return pm_runtime_force_resume(component->dev);
> +}
> +
> +#else
> +#define jh7110_tdm_suspend     NULL
> +#define jh7110_tdm_resume      NULL
> +#endif

you may use pm_sleep_ptr() to avoid these.

> +
> +static const struct snd_soc_component_driver jh7110_tdm_component = {
> +       .name = "jh7110-tdm",
> +       .suspend = jh7110_tdm_suspend,
> +       .resume = jh7110_tdm_resume,
> +};
> +
> +static int jh7110_tdm_startup(struct snd_pcm_substream *substream,
> +                             struct snd_soc_dai *cpu_dai)
> +{
> +       struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
> +       struct snd_soc_dai_link *dai_link = rtd->dai_link;
> +
> +       dai_link->stop_dma_first = 1;
> +
> +       return 0;
> +}
> +
> +static int jh7110_tdm_hw_params(struct snd_pcm_substream *substream,
> +                               struct snd_pcm_hw_params *params,
> +                               struct snd_soc_dai *dai)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +       int chan_wl, chan_sl, chan_nr;
> +       unsigned int data_width;
> +       unsigned int dma_bus_width;
> +       struct snd_dmaengine_dai_dma_data *dma_data = NULL;
> +       int ret = 0;
> +
> +       data_width = params_width(params);
> +
> +       tdm->samplerate = params_rate(params);
> +       tdm->pcmclk = params_channels(params) * tdm->samplerate * data_width;
> +
> +       switch (params_format(params)) {
> +       case SNDRV_PCM_FORMAT_S16_LE:
> +               chan_wl = TDM_16BIT_WORD_LEN;
> +               chan_sl = TDM_16BIT_SLOT_LEN;
> +               dma_bus_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +               break;
> +
> +       case SNDRV_PCM_FORMAT_S32_LE:
> +               chan_wl = TDM_32BIT_WORD_LEN;
> +               chan_sl = TDM_32BIT_SLOT_LEN;
> +               dma_bus_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
> +               break;
> +
> +       default:
> +               dev_err(tdm->dev, "tdm: unsupported PCM fmt");
> +               return -EINVAL;
> +       }
> +
> +       chan_nr = params_channels(params);
> +       switch (chan_nr) {
> +       case 1:
> +       case 2:
> +       case 4:
> +       case 6:
> +       case 8:
> +               break;
> +       default:
> +               dev_err(tdm->dev, "channel not supported\n");
> +               return -EINVAL;
> +       }
> +
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> +               tdm->tx.wl = chan_wl;
> +               tdm->tx.sl = chan_sl;
> +               tdm->tx.sscale = chan_nr;
> +               tdm->play_dma_data.addr_width = dma_bus_width;
> +               dma_data = &tdm->play_dma_data;
> +       } else {
> +               tdm->rx.wl = chan_wl;
> +               tdm->rx.sl = chan_sl;
> +               tdm->rx.sscale = chan_nr;
> +               tdm->capture_dma_data.addr_width = dma_bus_width;
> +               dma_data = &tdm->capture_dma_data;
> +       }
> +
> +       snd_soc_dai_set_dma_data(dai, substream, dma_data);
> +
> +       ret = jh7110_tdm_config(tdm, substream);
> +       if (ret)
> +               return ret;
> +
> +       jh7110_tdm_save_context(tdm, substream);
> +       return 0;
> +}
> +
> +static int jh7110_tdm_trigger(struct snd_pcm_substream *substream,
> +                             int cmd, struct snd_soc_dai *dai)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +       int ret = 0;
> +
> +       switch (cmd) {
> +       case SNDRV_PCM_TRIGGER_START:
> +       case SNDRV_PCM_TRIGGER_RESUME:
> +       case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
> +               jh7110_tdm_start(tdm, substream);
> +               break;
> +
> +       case SNDRV_PCM_TRIGGER_STOP:
> +       case SNDRV_PCM_TRIGGER_SUSPEND:
> +       case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
> +               jh7110_tdm_stop(tdm, substream);
> +               break;
> +       default:
> +               ret = -EINVAL;
> +               break;
> +       }
> +       return ret;
> +}
> +
> +static int jh7110_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
> +                                 unsigned int fmt)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(cpu_dai);
> +       unsigned int gbcr;
> +       int ret = 0;
> +
> +       /* set master/slave audio interface */
> +       switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
> +       case SND_SOC_DAIFMT_BP_FP:
> +               /* cpu is master */
> +               tdm->ms_mode = TDM_AS_MASTER;
> +               break;
> +       case SND_SOC_DAIFMT_BC_FC:
> +               /* codec is master */
> +               tdm->ms_mode = TDM_AS_SLAVE;
> +               break;
> +       case SND_SOC_DAIFMT_BC_FP:
> +       case SND_SOC_DAIFMT_BP_FC:
> +               ret = -EINVAL;
> +               break;
> +       default:
> +               dev_dbg(tdm->dev, "dwc : Invalid clock provider format\n");
> +               ret = -EINVAL;
> +               break;
> +       }
> +
> +       gbcr = (tdm->clkpolity << CLKPOL_BIT) |
> +               (tdm->elm << ELM_BIT) |
> +               (tdm->syncm << SYNCM_BIT) |
> +               (tdm->ms_mode << MS_BIT);
> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, gbcr);
> +
> +       return ret;
> +}
> +
> +static const struct snd_soc_dai_ops jh7110_tdm_dai_ops = {
> +       .startup        = jh7110_tdm_startup,
> +       .hw_params      = jh7110_tdm_hw_params,
> +       .trigger        = jh7110_tdm_trigger,
> +       .set_fmt        = jh7110_tdm_set_dai_fmt,
> +};
> +
> +static int jh7110_tdm_dai_probe(struct snd_soc_dai *dai)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +
> +       snd_soc_dai_init_dma_data(dai, &tdm->play_dma_data, &tdm->capture_dma_data);
> +       snd_soc_dai_set_drvdata(dai, tdm);
> +       return 0;
> +}
> +
> +#define JH7110_TDM_RATES       SNDRV_PCM_RATE_8000_48000
> +
> +#define JH7110_TDM_FORMATS     (SNDRV_PCM_FMTBIT_S16_LE | \
> +                                SNDRV_PCM_FMTBIT_S32_LE)
> +
> +static struct snd_soc_dai_driver jh7110_tdm_dai = {
> +       .name = "sf_tdm",
> +       .id = 0,
> +       .playback = {
> +               .stream_name    = "Playback",
> +               .channels_min   = 1,
> +               .channels_max   = 8,
> +               .rates          = JH7110_TDM_RATES,
> +               .formats        = JH7110_TDM_FORMATS,
> +       },
> +       .capture = {
> +               .stream_name    = "Capture",
> +               .channels_min   = 1,
> +               .channels_max   = 8,
> +               .rates          = JH7110_TDM_RATES,
> +               .formats        = JH7110_TDM_FORMATS,
> +       },
> +       .ops = &jh7110_tdm_dai_ops,
> +       .probe = jh7110_tdm_dai_probe,
> +       .symmetric_rate = 1,
> +};
> +
> +static const struct snd_pcm_hardware jh7110_pcm_hardware = {
> +       .info                   = (SNDRV_PCM_INFO_MMAP          |
> +                                  SNDRV_PCM_INFO_MMAP_VALID    |
> +                                  SNDRV_PCM_INFO_PAUSE         |
> +                                  SNDRV_PCM_INFO_RESUME        |
> +                                  SNDRV_PCM_INFO_INTERLEAVED   |
> +                                  SNDRV_PCM_INFO_BLOCK_TRANSFER),
> +       .buffer_bytes_max       = 192512,
> +       .period_bytes_min       = 4096,
> +       .period_bytes_max       = 32768,
> +       .periods_min            = 1,
> +       .periods_max            = 48,
> +       .fifo_size              = 16,
> +};
> +
> +static const struct snd_dmaengine_pcm_config jh7110_dmaengine_pcm_config = {
> +       .pcm_hardware = &jh7110_pcm_hardware,
> +       .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
> +       .prealloc_buffer_size = 192512,
> +};
> +
> +static void jh7110_tdm_init_params(struct jh7110_tdm_dev *tdm)
> +{
> +       tdm->clkpolity = TDM_TX_RASING_RX_FALLING;
> +       tdm->elm = TDM_ELM_LATE;
> +       tdm->syncm = TDM_SYNCM_SHORT;
> +
> +       tdm->rx.ifl = TDM_FIFO_HALF;
> +       tdm->tx.ifl = TDM_FIFO_HALF;
> +       tdm->rx.wl = TDM_16BIT_WORD_LEN;
> +       tdm->tx.wl = TDM_16BIT_WORD_LEN;
> +       tdm->rx.sscale = 2;
> +       tdm->tx.sscale = 2;
> +       tdm->rx.lrj = TDM_LEFT_JUSTIFT;
> +       tdm->tx.lrj = TDM_LEFT_JUSTIFT;
> +
> +       tdm->play_dma_data.addr = JH7110_TDM_FIFO;
> +       tdm->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +       tdm->play_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
> +       tdm->play_dma_data.maxburst = 16;
> +
> +       tdm->capture_dma_data.addr = JH7110_TDM_FIFO;
> +       tdm->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +       tdm->capture_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
> +       tdm->capture_dma_data.maxburst = 8;
> +}
> +
> +static int jh7110_tdm_clk_reset_init(struct platform_device *pdev,
> +                                    struct jh7110_tdm_dev *tdm)
> +{
> +       int i, ret;
> +
> +       for (i = 0; i < tdm->num_clks; i++) {
> +               tdm->clks[i] = devm_clk_get(&pdev->dev, tdm->clk_names[i]);
> +               if (IS_ERR(tdm->clks[i])) {
> +                       dev_err(&pdev->dev, "Failed to get clock: %s\n",
> +                               tdm->clk_names[i]);
> +                       return PTR_ERR(tdm->clks[i]);
> +               }
> +       }

devm_clk_bulk_get() ?

> +
> +       tdm->resets = devm_reset_control_array_get_exclusive(&pdev->dev);
> +       if (IS_ERR(tdm->resets)) {
> +               ret = PTR_ERR(tdm->resets);
> +               dev_err(&pdev->dev, "Failed to get tdm resets");
> +               return ret;
> +       }
> +
> +       return jh7110_tdm_clk_enable(tdm);
> +}
> +
> +static int jh7110_tdm_probe(struct platform_device *pdev)
> +{
> +       struct jh7110_tdm_dev *tdm;
> +       const struct starfive_tdm_driverdata *driver_data;
> +       int ret;
> +
> +       tdm = devm_kzalloc(&pdev->dev, sizeof(*tdm), GFP_KERNEL);
> +       if (!tdm)
> +               return -ENOMEM;
> +
> +       driver_data = of_device_get_match_data(&pdev->dev);
> +       tdm->clk_names = (const char **)driver_data->clk_names;
> +       tdm->num_clks = driver_data->num_clks;
> +
> +       tdm->tdm_base = devm_platform_ioremap_resource(pdev, 0);
> +       if (IS_ERR(tdm->tdm_base))
> +               return PTR_ERR(tdm->tdm_base);
> +
> +       tdm->dev = &pdev->dev;
> +
> +       ret = jh7110_tdm_clk_reset_init(pdev, tdm);
> +       if (ret) {
> +               dev_err(&pdev->dev, "Failed to enable audio-tdm clock\n");
> +               return ret;
> +       }
> +
> +       jh7110_tdm_init_params(tdm);
> +
> +       dev_set_drvdata(&pdev->dev, tdm);
> +       ret = devm_snd_soc_register_component(&pdev->dev, &jh7110_tdm_component,
> +                                             &jh7110_tdm_dai, 1);
> +       if (ret != 0) {

if (ret)

> +               dev_err(&pdev->dev, "Failed to register dai\n");
> +               return ret;
> +       }
> +
> +       ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
> +                                             &jh7110_dmaengine_pcm_config,
> +                                             SND_DMAENGINE_PCM_FLAG_COMPAT);
> +       if (ret) {
> +               dev_err(&pdev->dev, "Could not register pcm: %d\n", ret);
> +               return ret;
> +       }
> +
> +       pm_runtime_enable(&pdev->dev);
> +#ifdef CONFIG_PM
> +       jh7110_tdm_clk_disable(tdm);
> +#endif
> +
> +       return 0;
> +}
> +
> +static int jh7110_tdm_dev_remove(struct platform_device *pdev)
> +{
> +       pm_runtime_disable(&pdev->dev);
> +       return 0;
> +}
> +
> +static const struct starfive_tdm_driverdata jh7110_drvdata = {
> +       .clk_names = {"mclk_inner", "tdm_ahb", "tdm_apb", "tdm_internal", "tdm_ext", "tdm"},
> +       .num_clks = 6,
> +};
> +
> +static const struct of_device_id jh7110_tdm_of_match[] = {
> +       { .compatible = "starfive,jh7110-tdm", .data = &jh7110_drvdata },
> +       {}
> +};
> +
> +MODULE_DEVICE_TABLE(of, jh7110_tdm_of_match);
> +
> +static const struct dev_pm_ops jh7110_tdm_pm_ops = {
> +       SET_RUNTIME_PM_OPS(jh7110_tdm_runtime_suspend,
> +                          jh7110_tdm_runtime_resume, NULL)

You can use RUNTIME_PM_OPS() and avoid having #ifdef CONFIG_PM around
jh7110_tdm_runtime_suspend and jh7110_tdm_runtime_resume definitions.

> +};
> +
> +static struct platform_driver jh7110_tdm_driver = {
> +       .driver = {
> +               .name = "jh7110-tdm",
> +               .of_match_table = jh7110_tdm_of_match,
> +               .pm = &jh7110_tdm_pm_ops,
> +       },
> +       .probe = jh7110_tdm_probe,
> +       .remove = jh7110_tdm_dev_remove,
> +};
> +module_platform_driver(jh7110_tdm_driver);
> +
> +MODULE_DESCRIPTION("StarFive JH7110 TDM ASoC Driver");
> +MODULE_AUTHOR("Walker Chen <walker.chen@starfivetech.com>");
> +MODULE_LICENSE("GPL");
> diff --git a/sound/soc/starfive/jh7110_tdm.h b/sound/soc/starfive/jh7110_tdm.h
> new file mode 100644
> index 000000000000..80f17946a45d
> --- /dev/null
> +++ b/sound/soc/starfive/jh7110_tdm.h
> @@ -0,0 +1,147 @@
> +/* SPDX-License-Identifier: GPL-2.0
> + *
> + * TDM driver for the StarFive JH7110 SoC
> + *
> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
> + *
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + */
> +
> +#ifndef __SND_SOC_STARFIVE_TDM_H
> +#define __SND_SOC_STARFIVE_TDM_H
> +
> +#include <linux/clk.h>
> +#include <linux/device.h>
> +#include <linux/types.h>
> +#include <sound/dmaengine_pcm.h>
> +#include <sound/pcm.h>
> +#include <linux/dmaengine.h>
> +#include <linux/types.h>
> +
> +#define TDM_PCMGBCR                    0x00
> +       #define PCMGBCR_MASK            0x1e
> +       #define PCMGBCR_ENABLE          BIT(0)
> +       #define PCMGBCR_TRITXEN         BIT(4)
> +       #define CLKPOL_BIT              5
> +       #define TRITXEN_BIT             4
> +       #define ELM_BIT                 3
> +       #define SYNCM_BIT               2
> +       #define MS_BIT                  1
> +#define TDM_PCMTXCR                    0x04
> +       #define PCMTXCR_TXEN            BIT(0)
> +       #define IFL_BIT                 11
> +       #define WL_BIT                  8
> +       #define SSCALE_BIT              4
> +       #define SL_BIT                  2
> +       #define LRJ_BIT                 1
> +#define TDM_PCMRXCR                    0x08
> +       #define PCMRXCR_RXEN            BIT(0)
> +       #define PCMRXCR_RXSL_MASK       0xc
> +       #define PCMRXCR_RXSL_16BIT      0x4
> +       #define PCMRXCR_RXSL_32BIT      0x8
> +       #define PCMRXCR_SCALE_MASK      0xf0
> +       #define PCMRXCR_SCALE_1CH       0x10
> +#define TDM_PCMDIV                     0x0c
> +
> +/*  DMA registers */
> +#define JH7110_TDM_FIFO                        0x170c0000
> +#define JH7110_TDM_FIFO_DEPTH          32
> +#define JH7110_TDM_MAX_CLOCKS          6
> +
> +enum TDM_MASTER_SLAVE_MODE {
> +       TDM_AS_MASTER = 0,
> +       TDM_AS_SLAVE,
> +};
> +
> +enum TDM_CLKPOL {
> +       /* tx raising and rx falling */
> +       TDM_TX_RASING_RX_FALLING = 0,
> +       /* tx falling and rx raising */
> +       TDM_TX_FALLING_RX_RASING,
> +};
> +
> +enum TDM_ELM {
> +       /* only work while SYNCM=0 */
> +       TDM_ELM_LATE = 0,
> +       TDM_ELM_EARLY,
> +};
> +
> +enum TDM_SYNCM {
> +       /* short frame sync */
> +       TDM_SYNCM_SHORT = 0,
> +       /* long frame sync */
> +       TDM_SYNCM_LONG,
> +};
> +
> +enum TDM_IFL {
> +       /* FIFO to send or received : half-1/2, Quarter-1/4 */
> +       TDM_FIFO_HALF = 0,
> +       TDM_FIFO_QUARTER,
> +};
> +
> +enum TDM_WL {
> +       /* send or received word length */
> +       TDM_8BIT_WORD_LEN = 0,
> +       TDM_16BIT_WORD_LEN,
> +       TDM_20BIT_WORD_LEN,
> +       TDM_24BIT_WORD_LEN,
> +       TDM_32BIT_WORD_LEN,
> +};
> +
> +enum TDM_SL {
> +       /* send or received slot length */
> +       TDM_8BIT_SLOT_LEN = 0,
> +       TDM_16BIT_SLOT_LEN,
> +       TDM_32BIT_SLOT_LEN,
> +};
> +
> +enum TDM_LRJ {
> +       /* left-justify or right-justify */
> +       TDM_RIGHT_JUSTIFY = 0,
> +       TDM_LEFT_JUSTIFT,
> +};
> +
> +struct tdm_chan_cfg {
> +       enum TDM_IFL ifl;
> +       enum TDM_WL  wl;
> +       unsigned char sscale;
> +       enum TDM_SL  sl;
> +       enum TDM_LRJ lrj;
> +       unsigned char enable;
> +};
> +
> +struct starfive_tdm_driverdata {
> +       const char *clk_names[JH7110_TDM_MAX_CLOCKS];
> +       int num_clks;
> +};
> +
> +struct jh7110_tdm_dev {
> +       void __iomem *tdm_base;
> +       struct device *dev;
> +       const char **clk_names;
> +       struct clk *clks[JH7110_TDM_MAX_CLOCKS];
> +       int num_clks;
> +       struct reset_control *resets;
> +
> +       enum TDM_CLKPOL clkpolity;
> +       enum TDM_ELM    elm;
> +       enum TDM_SYNCM  syncm;
> +       enum TDM_MASTER_SLAVE_MODE ms_mode;
> +
> +       struct tdm_chan_cfg tx;
> +       struct tdm_chan_cfg rx;
> +
> +       u16 syncdiv;
> +       u32 samplerate;
> +       u32 pcmclk;
> +
> +       /* data related to DMA transfers b/w tdm and DMAC */
> +       struct snd_dmaengine_dai_dma_data play_dma_data;
> +       struct snd_dmaengine_dai_dma_data capture_dma_data;
> +       u32 saved_pcmgbcr;
> +       u32 saved_pcmtxcr;
> +       u32 saved_pcmrxcr;
> +       u32 saved_pcmdiv;
> +};
> +
> +#endif /* __SND_SOC_STARFIVE_TDM_H */
> --
> 2.17.1
> 

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
  2023-05-06  9:01   ` Walker Chen
                     ` (2 preceding siblings ...)
  (?)
@ 2023-05-09  4:33   ` Claudiu.Beznea--- via Alsa-devel
  -1 siblings, 0 replies; 48+ messages in thread
From: Claudiu.Beznea--- via Alsa-devel @ 2023-05-09  4:33 UTC (permalink / raw)
  To: walker.chen, broonie, lgirdwood, perex, tiwai, robh+dt,
	krzysztof.kozlowski+dt, Conor.Dooley, emil.renner.berthing
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv


[-- Attachment #0: Type: message/rfc822, Size: 51643 bytes --]

From: <Claudiu.Beznea@microchip.com>
To: <walker.chen@starfivetech.com>, <broonie@kernel.org>, <lgirdwood@gmail.com>, <perex@perex.cz>, <tiwai@suse.com>, <robh+dt@kernel.org>, <krzysztof.kozlowski+dt@linaro.org>, <Conor.Dooley@microchip.com>, <emil.renner.berthing@canonical.com>
Cc: alsa-devel@alsa-project.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org
Subject: Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
Date: Tue, 9 May 2023 04:33:42 +0000
Message-ID: <bf12f28f-c458-9279-82a0-a673c075e440@microchip.com>

On 06.05.2023 12:01, Walker Chen wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> 
> Add tdm driver support for the StarFive JH7110 SoC.
> 
> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
> ---
>  MAINTAINERS                     |   6 +
>  sound/soc/Kconfig               |   1 +
>  sound/soc/Makefile              |   1 +
>  sound/soc/starfive/Kconfig      |  15 +
>  sound/soc/starfive/Makefile     |   2 +
>  sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
>  sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
>  7 files changed, 745 insertions(+)
>  create mode 100644 sound/soc/starfive/Kconfig
>  create mode 100644 sound/soc/starfive/Makefile
>  create mode 100644 sound/soc/starfive/jh7110_tdm.c
>  create mode 100644 sound/soc/starfive/jh7110_tdm.h
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 5f9c544bc189..add89615d327 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -19945,6 +19945,12 @@ F:     Documentation/devicetree/bindings/power/starfive*
>  F:     drivers/soc/starfive/jh71xx_pmu.c
>  F:     include/dt-bindings/power/starfive,jh7110-pmu.h
> 
> +STARFIVE JH7110 TDM DRIVERS
> +M:     Walker Chen <walker.chen@starfivetech.com>
> +S:     Maintained
> +F:     Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
> +F:     sound/soc/starfive/jh7110-tdm.*
> +
>  STARFIVE SOC DRIVERS
>  M:     Conor Dooley <conor@kernel.org>
>  S:     Maintained
> diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
> index 848fbae26c3b..8d1d9401ecf2 100644
> --- a/sound/soc/Kconfig
> +++ b/sound/soc/Kconfig
> @@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
>  source "sound/soc/sof/Kconfig"
>  source "sound/soc/spear/Kconfig"
>  source "sound/soc/sprd/Kconfig"
> +source "sound/soc/starfive/Kconfig"
>  source "sound/soc/sti/Kconfig"
>  source "sound/soc/stm/Kconfig"
>  source "sound/soc/sunxi/Kconfig"
> diff --git a/sound/soc/Makefile b/sound/soc/Makefile
> index 507eaed1d6a1..65aeb4ef4068 100644
> --- a/sound/soc/Makefile
> +++ b/sound/soc/Makefile
> @@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC) += sh/
>  obj-$(CONFIG_SND_SOC)  += sof/
>  obj-$(CONFIG_SND_SOC)  += spear/
>  obj-$(CONFIG_SND_SOC)  += sprd/
> +obj-$(CONFIG_SND_SOC)  += starfive/
>  obj-$(CONFIG_SND_SOC)  += sti/
>  obj-$(CONFIG_SND_SOC)  += stm/
>  obj-$(CONFIG_SND_SOC)  += sunxi/
> diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
> new file mode 100644
> index 000000000000..737c956f7b93
> --- /dev/null
> +++ b/sound/soc/starfive/Kconfig
> @@ -0,0 +1,15 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +config SND_SOC_STARFIVE
> +       tristate "Audio support for StarFive SoC"
> +       depends on COMPILE_TEST || SOC_STARFIVE
> +       help
> +         Say Y or M if you want to add support for codecs attached to
> +         the Starfive SoCs' Audio interfaces. You will also need to
> +         select the audio interfaces to support below.
> +
> +config SND_SOC_JH7110_TDM
> +       tristate "JH7110 TDM device driver"
> +       depends on HAVE_CLK && SND_SOC_STARFIVE
> +       select SND_SOC_GENERIC_DMAENGINE_PCM
> +       help
> +         Say Y or M if you want to add support for StarFive TDM driver.
> diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
> new file mode 100644
> index 000000000000..f7d960211d72
> --- /dev/null
> +++ b/sound/soc/starfive/Makefile
> @@ -0,0 +1,2 @@
> +# StarFive Platform Support
> +obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
> diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
> new file mode 100644
> index 000000000000..33f7cf43e4bd
> --- /dev/null
> +++ b/sound/soc/starfive/jh7110_tdm.c
> @@ -0,0 +1,573 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * jh7110_tdm.c -- StarFive JH7110 TDM driver
> + *
> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
> + *
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/device.h>
> +#include <linux/module.h>
> +#include <linux/of_irq.h>
> +#include <linux/of_platform.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/regmap.h>
> +#include <linux/reset.h>
> +#include <sound/initval.h>
> +#include <sound/pcm_params.h>
> +#include <sound/soc.h>
> +#include <sound/soc-dai.h>
> +#include "jh7110_tdm.h"
> +
> +static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
> +{
> +       return readl_relaxed(tdm->tdm_base + reg);
> +}
> +
> +static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
> +{
> +       writel_relaxed(val, tdm->tdm_base + reg);
> +}
> +
> +static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
> +                                   struct snd_pcm_substream *substream)
> +{
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +               tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
> +       else
> +               tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
> +}
> +
> +static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
> +                            struct snd_pcm_substream *substream)
> +{
> +       u32 data;
> +
> +       data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
> +
> +       /* restore context */
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
> +       else
> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
> +}
> +
> +static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
> +                           struct snd_pcm_substream *substream)
> +{
> +       unsigned int val;
> +
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> +               val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
> +               val &= ~PCMTXCR_TXEN;
> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
> +       } else {
> +               val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
> +               val &= ~PCMRXCR_RXEN;
> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
> +       }
> +}
> +
> +static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
> +{
> +       u32 sl, sscale, syncdiv;
> +
> +       if (tdm->rx.sl >= tdm->tx.sl)
> +               sl = tdm->rx.sl;
> +       else
> +               sl = tdm->tx.sl;
> +
> +       if (tdm->rx.sscale >= tdm->tx.sscale)
> +               sscale = tdm->rx.sscale;
> +       else
> +               sscale = tdm->tx.sscale;
> +
> +       syncdiv = tdm->pcmclk / tdm->samplerate - 1;
> +
> +       if ((syncdiv + 1) < (sl * sscale)) {
> +               dev_err(tdm->dev, "Failed to set syncdiv!\n");
> +               return -EINVAL;
> +       }
> +
> +       if (tdm->syncm == TDM_SYNCM_LONG &&
> +           (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
> +               if ((syncdiv + 1) <= sl) {
> +                       dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
> +                       return -EINVAL;
> +               }
> +       }
> +
> +       jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
> +       return 0;
> +}
> +
> +static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
> +                            struct snd_pcm_substream *substream)
> +{
> +       u32 datarx, datatx;
> +       int ret;
> +
> +       ret = jh7110_tdm_syncdiv(tdm);
> +       if (ret)
> +               return ret;
> +
> +       datarx = (tdm->rx.ifl << IFL_BIT) |
> +                 (tdm->rx.wl << WL_BIT) |
> +                 (tdm->rx.sscale << SSCALE_BIT) |
> +                 (tdm->rx.sl << SL_BIT) |
> +                 (tdm->rx.lrj << LRJ_BIT);
> +
> +       datatx = (tdm->tx.ifl << IFL_BIT) |
> +                 (tdm->tx.wl << WL_BIT) |
> +                 (tdm->tx.sscale << SSCALE_BIT) |
> +                 (tdm->tx.sl << SL_BIT) |
> +                 (tdm->tx.lrj << LRJ_BIT);
> +
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
> +       else
> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
> +
> +       return 0;
> +}
> +
> +static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
> +{
> +       int i;
> +
> +       for (i = tdm->num_clks - 1; i >= 0; i--)
> +               clk_disable_unprepare(tdm->clks[i]);
> +}

This could be replaced by clk_bulk_disable_unprepare().

> +
> +static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
> +{
> +       int i, ret;
> +
> +       for (i = 0; i < tdm->num_clks; i++) {
> +               ret = clk_prepare_enable(tdm->clks[i]);
> +               if (ret) {
> +                       while (i-- > 0)
> +                               clk_disable_unprepare(tdm->clks[i]);
> +                       return ret;
> +               }
> +       }


And this could be replaced by clk_bulk_prepare_enable().

> +
> +       ret = reset_control_deassert(tdm->resets);
> +       if (ret) {
> +               dev_err(tdm->dev, "Failed to deassert tdm resets\n");
> +               goto dis_tdm_clk;
> +       }
> +
> +       /* select tdm_ext clock as the clock source for tdm */
> +       ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
> +       if (ret) {
> +               dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
> +               goto dis_tdm_clk;
> +       }
> +       return 0;
> +
> +dis_tdm_clk:
> +       for (i = tdm->num_clks - 1; i >= 0; i--)
> +               clk_disable_unprepare(tdm->clks[i]);

clk_bulk_disable_unprepare()

> +
> +       return ret;
> +}
> +
> +#ifdef CONFIG_PM
> +static int jh7110_tdm_runtime_suspend(struct device *dev)
> +{
> +       struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
> +
> +       jh7110_tdm_clk_disable(tdm);
> +       return 0;
> +}
> +
> +static int jh7110_tdm_runtime_resume(struct device *dev)
> +{
> +       struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
> +
> +       return jh7110_tdm_clk_enable(tdm);
> +}
> +#endif
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int jh7110_tdm_suspend(struct snd_soc_component *component)
> +{
> +       /* save context */
> +       tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
> +       tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);
> +
> +       return pm_runtime_force_suspend(component->dev);
> +}
> +
> +static int jh7110_tdm_resume(struct snd_soc_component *component)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_component_get_drvdata(component);
> +
> +       /* restore context */
> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, tdm->saved_pcmgbcr);
> +       jh7110_tdm_writel(tdm, TDM_PCMDIV, tdm->saved_pcmdiv);
> +
> +       return pm_runtime_force_resume(component->dev);
> +}
> +
> +#else
> +#define jh7110_tdm_suspend     NULL
> +#define jh7110_tdm_resume      NULL
> +#endif

you may use pm_sleep_ptr() to avoid these.

> +
> +static const struct snd_soc_component_driver jh7110_tdm_component = {
> +       .name = "jh7110-tdm",
> +       .suspend = jh7110_tdm_suspend,
> +       .resume = jh7110_tdm_resume,
> +};
> +
> +static int jh7110_tdm_startup(struct snd_pcm_substream *substream,
> +                             struct snd_soc_dai *cpu_dai)
> +{
> +       struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
> +       struct snd_soc_dai_link *dai_link = rtd->dai_link;
> +
> +       dai_link->stop_dma_first = 1;
> +
> +       return 0;
> +}
> +
> +static int jh7110_tdm_hw_params(struct snd_pcm_substream *substream,
> +                               struct snd_pcm_hw_params *params,
> +                               struct snd_soc_dai *dai)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +       int chan_wl, chan_sl, chan_nr;
> +       unsigned int data_width;
> +       unsigned int dma_bus_width;
> +       struct snd_dmaengine_dai_dma_data *dma_data = NULL;
> +       int ret = 0;
> +
> +       data_width = params_width(params);
> +
> +       tdm->samplerate = params_rate(params);
> +       tdm->pcmclk = params_channels(params) * tdm->samplerate * data_width;
> +
> +       switch (params_format(params)) {
> +       case SNDRV_PCM_FORMAT_S16_LE:
> +               chan_wl = TDM_16BIT_WORD_LEN;
> +               chan_sl = TDM_16BIT_SLOT_LEN;
> +               dma_bus_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +               break;
> +
> +       case SNDRV_PCM_FORMAT_S32_LE:
> +               chan_wl = TDM_32BIT_WORD_LEN;
> +               chan_sl = TDM_32BIT_SLOT_LEN;
> +               dma_bus_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
> +               break;
> +
> +       default:
> +               dev_err(tdm->dev, "tdm: unsupported PCM fmt");
> +               return -EINVAL;
> +       }
> +
> +       chan_nr = params_channels(params);
> +       switch (chan_nr) {
> +       case 1:
> +       case 2:
> +       case 4:
> +       case 6:
> +       case 8:
> +               break;
> +       default:
> +               dev_err(tdm->dev, "channel not supported\n");
> +               return -EINVAL;
> +       }
> +
> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> +               tdm->tx.wl = chan_wl;
> +               tdm->tx.sl = chan_sl;
> +               tdm->tx.sscale = chan_nr;
> +               tdm->play_dma_data.addr_width = dma_bus_width;
> +               dma_data = &tdm->play_dma_data;
> +       } else {
> +               tdm->rx.wl = chan_wl;
> +               tdm->rx.sl = chan_sl;
> +               tdm->rx.sscale = chan_nr;
> +               tdm->capture_dma_data.addr_width = dma_bus_width;
> +               dma_data = &tdm->capture_dma_data;
> +       }
> +
> +       snd_soc_dai_set_dma_data(dai, substream, dma_data);
> +
> +       ret = jh7110_tdm_config(tdm, substream);
> +       if (ret)
> +               return ret;
> +
> +       jh7110_tdm_save_context(tdm, substream);
> +       return 0;
> +}
> +
> +static int jh7110_tdm_trigger(struct snd_pcm_substream *substream,
> +                             int cmd, struct snd_soc_dai *dai)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +       int ret = 0;
> +
> +       switch (cmd) {
> +       case SNDRV_PCM_TRIGGER_START:
> +       case SNDRV_PCM_TRIGGER_RESUME:
> +       case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
> +               jh7110_tdm_start(tdm, substream);
> +               break;
> +
> +       case SNDRV_PCM_TRIGGER_STOP:
> +       case SNDRV_PCM_TRIGGER_SUSPEND:
> +       case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
> +               jh7110_tdm_stop(tdm, substream);
> +               break;
> +       default:
> +               ret = -EINVAL;
> +               break;
> +       }
> +       return ret;
> +}
> +
> +static int jh7110_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
> +                                 unsigned int fmt)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(cpu_dai);
> +       unsigned int gbcr;
> +       int ret = 0;
> +
> +       /* set master/slave audio interface */
> +       switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
> +       case SND_SOC_DAIFMT_BP_FP:
> +               /* cpu is master */
> +               tdm->ms_mode = TDM_AS_MASTER;
> +               break;
> +       case SND_SOC_DAIFMT_BC_FC:
> +               /* codec is master */
> +               tdm->ms_mode = TDM_AS_SLAVE;
> +               break;
> +       case SND_SOC_DAIFMT_BC_FP:
> +       case SND_SOC_DAIFMT_BP_FC:
> +               ret = -EINVAL;
> +               break;
> +       default:
> +               dev_dbg(tdm->dev, "dwc : Invalid clock provider format\n");
> +               ret = -EINVAL;
> +               break;
> +       }
> +
> +       gbcr = (tdm->clkpolity << CLKPOL_BIT) |
> +               (tdm->elm << ELM_BIT) |
> +               (tdm->syncm << SYNCM_BIT) |
> +               (tdm->ms_mode << MS_BIT);
> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, gbcr);
> +
> +       return ret;
> +}
> +
> +static const struct snd_soc_dai_ops jh7110_tdm_dai_ops = {
> +       .startup        = jh7110_tdm_startup,
> +       .hw_params      = jh7110_tdm_hw_params,
> +       .trigger        = jh7110_tdm_trigger,
> +       .set_fmt        = jh7110_tdm_set_dai_fmt,
> +};
> +
> +static int jh7110_tdm_dai_probe(struct snd_soc_dai *dai)
> +{
> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
> +
> +       snd_soc_dai_init_dma_data(dai, &tdm->play_dma_data, &tdm->capture_dma_data);
> +       snd_soc_dai_set_drvdata(dai, tdm);
> +       return 0;
> +}
> +
> +#define JH7110_TDM_RATES       SNDRV_PCM_RATE_8000_48000
> +
> +#define JH7110_TDM_FORMATS     (SNDRV_PCM_FMTBIT_S16_LE | \
> +                                SNDRV_PCM_FMTBIT_S32_LE)
> +
> +static struct snd_soc_dai_driver jh7110_tdm_dai = {
> +       .name = "sf_tdm",
> +       .id = 0,
> +       .playback = {
> +               .stream_name    = "Playback",
> +               .channels_min   = 1,
> +               .channels_max   = 8,
> +               .rates          = JH7110_TDM_RATES,
> +               .formats        = JH7110_TDM_FORMATS,
> +       },
> +       .capture = {
> +               .stream_name    = "Capture",
> +               .channels_min   = 1,
> +               .channels_max   = 8,
> +               .rates          = JH7110_TDM_RATES,
> +               .formats        = JH7110_TDM_FORMATS,
> +       },
> +       .ops = &jh7110_tdm_dai_ops,
> +       .probe = jh7110_tdm_dai_probe,
> +       .symmetric_rate = 1,
> +};
> +
> +static const struct snd_pcm_hardware jh7110_pcm_hardware = {
> +       .info                   = (SNDRV_PCM_INFO_MMAP          |
> +                                  SNDRV_PCM_INFO_MMAP_VALID    |
> +                                  SNDRV_PCM_INFO_PAUSE         |
> +                                  SNDRV_PCM_INFO_RESUME        |
> +                                  SNDRV_PCM_INFO_INTERLEAVED   |
> +                                  SNDRV_PCM_INFO_BLOCK_TRANSFER),
> +       .buffer_bytes_max       = 192512,
> +       .period_bytes_min       = 4096,
> +       .period_bytes_max       = 32768,
> +       .periods_min            = 1,
> +       .periods_max            = 48,
> +       .fifo_size              = 16,
> +};
> +
> +static const struct snd_dmaengine_pcm_config jh7110_dmaengine_pcm_config = {
> +       .pcm_hardware = &jh7110_pcm_hardware,
> +       .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
> +       .prealloc_buffer_size = 192512,
> +};
> +
> +static void jh7110_tdm_init_params(struct jh7110_tdm_dev *tdm)
> +{
> +       tdm->clkpolity = TDM_TX_RASING_RX_FALLING;
> +       tdm->elm = TDM_ELM_LATE;
> +       tdm->syncm = TDM_SYNCM_SHORT;
> +
> +       tdm->rx.ifl = TDM_FIFO_HALF;
> +       tdm->tx.ifl = TDM_FIFO_HALF;
> +       tdm->rx.wl = TDM_16BIT_WORD_LEN;
> +       tdm->tx.wl = TDM_16BIT_WORD_LEN;
> +       tdm->rx.sscale = 2;
> +       tdm->tx.sscale = 2;
> +       tdm->rx.lrj = TDM_LEFT_JUSTIFT;
> +       tdm->tx.lrj = TDM_LEFT_JUSTIFT;
> +
> +       tdm->play_dma_data.addr = JH7110_TDM_FIFO;
> +       tdm->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +       tdm->play_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
> +       tdm->play_dma_data.maxburst = 16;
> +
> +       tdm->capture_dma_data.addr = JH7110_TDM_FIFO;
> +       tdm->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
> +       tdm->capture_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
> +       tdm->capture_dma_data.maxburst = 8;
> +}
> +
> +static int jh7110_tdm_clk_reset_init(struct platform_device *pdev,
> +                                    struct jh7110_tdm_dev *tdm)
> +{
> +       int i, ret;
> +
> +       for (i = 0; i < tdm->num_clks; i++) {
> +               tdm->clks[i] = devm_clk_get(&pdev->dev, tdm->clk_names[i]);
> +               if (IS_ERR(tdm->clks[i])) {
> +                       dev_err(&pdev->dev, "Failed to get clock: %s\n",
> +                               tdm->clk_names[i]);
> +                       return PTR_ERR(tdm->clks[i]);
> +               }
> +       }

devm_clk_bulk_get() ?

> +
> +       tdm->resets = devm_reset_control_array_get_exclusive(&pdev->dev);
> +       if (IS_ERR(tdm->resets)) {
> +               ret = PTR_ERR(tdm->resets);
> +               dev_err(&pdev->dev, "Failed to get tdm resets");
> +               return ret;
> +       }
> +
> +       return jh7110_tdm_clk_enable(tdm);
> +}
> +
> +static int jh7110_tdm_probe(struct platform_device *pdev)
> +{
> +       struct jh7110_tdm_dev *tdm;
> +       const struct starfive_tdm_driverdata *driver_data;
> +       int ret;
> +
> +       tdm = devm_kzalloc(&pdev->dev, sizeof(*tdm), GFP_KERNEL);
> +       if (!tdm)
> +               return -ENOMEM;
> +
> +       driver_data = of_device_get_match_data(&pdev->dev);
> +       tdm->clk_names = (const char **)driver_data->clk_names;
> +       tdm->num_clks = driver_data->num_clks;
> +
> +       tdm->tdm_base = devm_platform_ioremap_resource(pdev, 0);
> +       if (IS_ERR(tdm->tdm_base))
> +               return PTR_ERR(tdm->tdm_base);
> +
> +       tdm->dev = &pdev->dev;
> +
> +       ret = jh7110_tdm_clk_reset_init(pdev, tdm);
> +       if (ret) {
> +               dev_err(&pdev->dev, "Failed to enable audio-tdm clock\n");
> +               return ret;
> +       }
> +
> +       jh7110_tdm_init_params(tdm);
> +
> +       dev_set_drvdata(&pdev->dev, tdm);
> +       ret = devm_snd_soc_register_component(&pdev->dev, &jh7110_tdm_component,
> +                                             &jh7110_tdm_dai, 1);
> +       if (ret != 0) {

if (ret)

> +               dev_err(&pdev->dev, "Failed to register dai\n");
> +               return ret;
> +       }
> +
> +       ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
> +                                             &jh7110_dmaengine_pcm_config,
> +                                             SND_DMAENGINE_PCM_FLAG_COMPAT);
> +       if (ret) {
> +               dev_err(&pdev->dev, "Could not register pcm: %d\n", ret);
> +               return ret;
> +       }
> +
> +       pm_runtime_enable(&pdev->dev);
> +#ifdef CONFIG_PM
> +       jh7110_tdm_clk_disable(tdm);
> +#endif
> +
> +       return 0;
> +}
> +
> +static int jh7110_tdm_dev_remove(struct platform_device *pdev)
> +{
> +       pm_runtime_disable(&pdev->dev);
> +       return 0;
> +}
> +
> +static const struct starfive_tdm_driverdata jh7110_drvdata = {
> +       .clk_names = {"mclk_inner", "tdm_ahb", "tdm_apb", "tdm_internal", "tdm_ext", "tdm"},
> +       .num_clks = 6,
> +};
> +
> +static const struct of_device_id jh7110_tdm_of_match[] = {
> +       { .compatible = "starfive,jh7110-tdm", .data = &jh7110_drvdata },
> +       {}
> +};
> +
> +MODULE_DEVICE_TABLE(of, jh7110_tdm_of_match);
> +
> +static const struct dev_pm_ops jh7110_tdm_pm_ops = {
> +       SET_RUNTIME_PM_OPS(jh7110_tdm_runtime_suspend,
> +                          jh7110_tdm_runtime_resume, NULL)

You can use RUNTIME_PM_OPS() and avoid having #ifdef CONFIG_PM around
jh7110_tdm_runtime_suspend and jh7110_tdm_runtime_resume definitions.

> +};
> +
> +static struct platform_driver jh7110_tdm_driver = {
> +       .driver = {
> +               .name = "jh7110-tdm",
> +               .of_match_table = jh7110_tdm_of_match,
> +               .pm = &jh7110_tdm_pm_ops,
> +       },
> +       .probe = jh7110_tdm_probe,
> +       .remove = jh7110_tdm_dev_remove,
> +};
> +module_platform_driver(jh7110_tdm_driver);
> +
> +MODULE_DESCRIPTION("StarFive JH7110 TDM ASoC Driver");
> +MODULE_AUTHOR("Walker Chen <walker.chen@starfivetech.com>");
> +MODULE_LICENSE("GPL");
> diff --git a/sound/soc/starfive/jh7110_tdm.h b/sound/soc/starfive/jh7110_tdm.h
> new file mode 100644
> index 000000000000..80f17946a45d
> --- /dev/null
> +++ b/sound/soc/starfive/jh7110_tdm.h
> @@ -0,0 +1,147 @@
> +/* SPDX-License-Identifier: GPL-2.0
> + *
> + * TDM driver for the StarFive JH7110 SoC
> + *
> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
> + *
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + */
> +
> +#ifndef __SND_SOC_STARFIVE_TDM_H
> +#define __SND_SOC_STARFIVE_TDM_H
> +
> +#include <linux/clk.h>
> +#include <linux/device.h>
> +#include <linux/types.h>
> +#include <sound/dmaengine_pcm.h>
> +#include <sound/pcm.h>
> +#include <linux/dmaengine.h>
> +#include <linux/types.h>
> +
> +#define TDM_PCMGBCR                    0x00
> +       #define PCMGBCR_MASK            0x1e
> +       #define PCMGBCR_ENABLE          BIT(0)
> +       #define PCMGBCR_TRITXEN         BIT(4)
> +       #define CLKPOL_BIT              5
> +       #define TRITXEN_BIT             4
> +       #define ELM_BIT                 3
> +       #define SYNCM_BIT               2
> +       #define MS_BIT                  1
> +#define TDM_PCMTXCR                    0x04
> +       #define PCMTXCR_TXEN            BIT(0)
> +       #define IFL_BIT                 11
> +       #define WL_BIT                  8
> +       #define SSCALE_BIT              4
> +       #define SL_BIT                  2
> +       #define LRJ_BIT                 1
> +#define TDM_PCMRXCR                    0x08
> +       #define PCMRXCR_RXEN            BIT(0)
> +       #define PCMRXCR_RXSL_MASK       0xc
> +       #define PCMRXCR_RXSL_16BIT      0x4
> +       #define PCMRXCR_RXSL_32BIT      0x8
> +       #define PCMRXCR_SCALE_MASK      0xf0
> +       #define PCMRXCR_SCALE_1CH       0x10
> +#define TDM_PCMDIV                     0x0c
> +
> +/*  DMA registers */
> +#define JH7110_TDM_FIFO                        0x170c0000
> +#define JH7110_TDM_FIFO_DEPTH          32
> +#define JH7110_TDM_MAX_CLOCKS          6
> +
> +enum TDM_MASTER_SLAVE_MODE {
> +       TDM_AS_MASTER = 0,
> +       TDM_AS_SLAVE,
> +};
> +
> +enum TDM_CLKPOL {
> +       /* tx raising and rx falling */
> +       TDM_TX_RASING_RX_FALLING = 0,
> +       /* tx falling and rx raising */
> +       TDM_TX_FALLING_RX_RASING,
> +};
> +
> +enum TDM_ELM {
> +       /* only work while SYNCM=0 */
> +       TDM_ELM_LATE = 0,
> +       TDM_ELM_EARLY,
> +};
> +
> +enum TDM_SYNCM {
> +       /* short frame sync */
> +       TDM_SYNCM_SHORT = 0,
> +       /* long frame sync */
> +       TDM_SYNCM_LONG,
> +};
> +
> +enum TDM_IFL {
> +       /* FIFO to send or received : half-1/2, Quarter-1/4 */
> +       TDM_FIFO_HALF = 0,
> +       TDM_FIFO_QUARTER,
> +};
> +
> +enum TDM_WL {
> +       /* send or received word length */
> +       TDM_8BIT_WORD_LEN = 0,
> +       TDM_16BIT_WORD_LEN,
> +       TDM_20BIT_WORD_LEN,
> +       TDM_24BIT_WORD_LEN,
> +       TDM_32BIT_WORD_LEN,
> +};
> +
> +enum TDM_SL {
> +       /* send or received slot length */
> +       TDM_8BIT_SLOT_LEN = 0,
> +       TDM_16BIT_SLOT_LEN,
> +       TDM_32BIT_SLOT_LEN,
> +};
> +
> +enum TDM_LRJ {
> +       /* left-justify or right-justify */
> +       TDM_RIGHT_JUSTIFY = 0,
> +       TDM_LEFT_JUSTIFT,
> +};
> +
> +struct tdm_chan_cfg {
> +       enum TDM_IFL ifl;
> +       enum TDM_WL  wl;
> +       unsigned char sscale;
> +       enum TDM_SL  sl;
> +       enum TDM_LRJ lrj;
> +       unsigned char enable;
> +};
> +
> +struct starfive_tdm_driverdata {
> +       const char *clk_names[JH7110_TDM_MAX_CLOCKS];
> +       int num_clks;
> +};
> +
> +struct jh7110_tdm_dev {
> +       void __iomem *tdm_base;
> +       struct device *dev;
> +       const char **clk_names;
> +       struct clk *clks[JH7110_TDM_MAX_CLOCKS];
> +       int num_clks;
> +       struct reset_control *resets;
> +
> +       enum TDM_CLKPOL clkpolity;
> +       enum TDM_ELM    elm;
> +       enum TDM_SYNCM  syncm;
> +       enum TDM_MASTER_SLAVE_MODE ms_mode;
> +
> +       struct tdm_chan_cfg tx;
> +       struct tdm_chan_cfg rx;
> +
> +       u16 syncdiv;
> +       u32 samplerate;
> +       u32 pcmclk;
> +
> +       /* data related to DMA transfers b/w tdm and DMAC */
> +       struct snd_dmaengine_dai_dma_data play_dma_data;
> +       struct snd_dmaengine_dai_dma_data capture_dma_data;
> +       u32 saved_pcmgbcr;
> +       u32 saved_pcmtxcr;
> +       u32 saved_pcmrxcr;
> +       u32 saved_pcmdiv;
> +};
> +
> +#endif /* __SND_SOC_STARFIVE_TDM_H */
> --
> 2.17.1
> 


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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
  2023-05-09  4:33     ` Claudiu.Beznea
  (?)
@ 2023-05-09  7:07       ` Walker Chen
  -1 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-09  7:07 UTC (permalink / raw)
  To: Claudiu.Beznea, broonie, lgirdwood, perex, tiwai, robh+dt,
	krzysztof.kozlowski+dt, Conor.Dooley, emil.renner.berthing
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv


On 2023/5/9 12:33, Claudiu.Beznea@microchip.com wrote:
> On 06.05.2023 12:01, Walker Chen wrote:
>> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
>> 
>> Add tdm driver support for the StarFive JH7110 SoC.
>> 
>> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
>> ---
>>  MAINTAINERS                     |   6 +
>>  sound/soc/Kconfig               |   1 +
>>  sound/soc/Makefile              |   1 +
>>  sound/soc/starfive/Kconfig      |  15 +
>>  sound/soc/starfive/Makefile     |   2 +
>>  sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
>>  sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
>>  7 files changed, 745 insertions(+)
>>  create mode 100644 sound/soc/starfive/Kconfig
>>  create mode 100644 sound/soc/starfive/Makefile
>>  create mode 100644 sound/soc/starfive/jh7110_tdm.c
>>  create mode 100644 sound/soc/starfive/jh7110_tdm.h
>> 
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index 5f9c544bc189..add89615d327 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -19945,6 +19945,12 @@ F:     Documentation/devicetree/bindings/power/starfive*
>>  F:     drivers/soc/starfive/jh71xx_pmu.c
>>  F:     include/dt-bindings/power/starfive,jh7110-pmu.h
>> 
>> +STARFIVE JH7110 TDM DRIVERS
>> +M:     Walker Chen <walker.chen@starfivetech.com>
>> +S:     Maintained
>> +F:     Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
>> +F:     sound/soc/starfive/jh7110-tdm.*
>> +
>>  STARFIVE SOC DRIVERS
>>  M:     Conor Dooley <conor@kernel.org>
>>  S:     Maintained
>> diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
>> index 848fbae26c3b..8d1d9401ecf2 100644
>> --- a/sound/soc/Kconfig
>> +++ b/sound/soc/Kconfig
>> @@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
>>  source "sound/soc/sof/Kconfig"
>>  source "sound/soc/spear/Kconfig"
>>  source "sound/soc/sprd/Kconfig"
>> +source "sound/soc/starfive/Kconfig"
>>  source "sound/soc/sti/Kconfig"
>>  source "sound/soc/stm/Kconfig"
>>  source "sound/soc/sunxi/Kconfig"
>> diff --git a/sound/soc/Makefile b/sound/soc/Makefile
>> index 507eaed1d6a1..65aeb4ef4068 100644
>> --- a/sound/soc/Makefile
>> +++ b/sound/soc/Makefile
>> @@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC) += sh/
>>  obj-$(CONFIG_SND_SOC)  += sof/
>>  obj-$(CONFIG_SND_SOC)  += spear/
>>  obj-$(CONFIG_SND_SOC)  += sprd/
>> +obj-$(CONFIG_SND_SOC)  += starfive/
>>  obj-$(CONFIG_SND_SOC)  += sti/
>>  obj-$(CONFIG_SND_SOC)  += stm/
>>  obj-$(CONFIG_SND_SOC)  += sunxi/
>> diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
>> new file mode 100644
>> index 000000000000..737c956f7b93
>> --- /dev/null
>> +++ b/sound/soc/starfive/Kconfig
>> @@ -0,0 +1,15 @@
>> +# SPDX-License-Identifier: GPL-2.0-only
>> +config SND_SOC_STARFIVE
>> +       tristate "Audio support for StarFive SoC"
>> +       depends on COMPILE_TEST || SOC_STARFIVE
>> +       help
>> +         Say Y or M if you want to add support for codecs attached to
>> +         the Starfive SoCs' Audio interfaces. You will also need to
>> +         select the audio interfaces to support below.
>> +
>> +config SND_SOC_JH7110_TDM
>> +       tristate "JH7110 TDM device driver"
>> +       depends on HAVE_CLK && SND_SOC_STARFIVE
>> +       select SND_SOC_GENERIC_DMAENGINE_PCM
>> +       help
>> +         Say Y or M if you want to add support for StarFive TDM driver.
>> diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
>> new file mode 100644
>> index 000000000000..f7d960211d72
>> --- /dev/null
>> +++ b/sound/soc/starfive/Makefile
>> @@ -0,0 +1,2 @@
>> +# StarFive Platform Support
>> +obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
>> diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
>> new file mode 100644
>> index 000000000000..33f7cf43e4bd
>> --- /dev/null
>> +++ b/sound/soc/starfive/jh7110_tdm.c
>> @@ -0,0 +1,573 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * jh7110_tdm.c -- StarFive JH7110 TDM driver
>> + *
>> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
>> + *
>> + * Author: Walker Chen <walker.chen@starfivetech.com>
>> + */
>> +
>> +#include <linux/clk.h>
>> +#include <linux/device.h>
>> +#include <linux/module.h>
>> +#include <linux/of_irq.h>
>> +#include <linux/of_platform.h>
>> +#include <linux/pm_runtime.h>
>> +#include <linux/regmap.h>
>> +#include <linux/reset.h>
>> +#include <sound/initval.h>
>> +#include <sound/pcm_params.h>
>> +#include <sound/soc.h>
>> +#include <sound/soc-dai.h>
>> +#include "jh7110_tdm.h"
>> +
>> +static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
>> +{
>> +       return readl_relaxed(tdm->tdm_base + reg);
>> +}
>> +
>> +static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
>> +{
>> +       writel_relaxed(val, tdm->tdm_base + reg);
>> +}
>> +
>> +static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
>> +                                   struct snd_pcm_substream *substream)
>> +{
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +               tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
>> +       else
>> +               tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
>> +}
>> +
>> +static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
>> +                            struct snd_pcm_substream *substream)
>> +{
>> +       u32 data;
>> +
>> +       data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
>> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
>> +
>> +       /* restore context */
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
>> +       else
>> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
>> +}
>> +
>> +static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
>> +                           struct snd_pcm_substream *substream)
>> +{
>> +       unsigned int val;
>> +
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
>> +               val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
>> +               val &= ~PCMTXCR_TXEN;
>> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
>> +       } else {
>> +               val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
>> +               val &= ~PCMRXCR_RXEN;
>> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
>> +       }
>> +}
>> +
>> +static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
>> +{
>> +       u32 sl, sscale, syncdiv;
>> +
>> +       if (tdm->rx.sl >= tdm->tx.sl)
>> +               sl = tdm->rx.sl;
>> +       else
>> +               sl = tdm->tx.sl;
>> +
>> +       if (tdm->rx.sscale >= tdm->tx.sscale)
>> +               sscale = tdm->rx.sscale;
>> +       else
>> +               sscale = tdm->tx.sscale;
>> +
>> +       syncdiv = tdm->pcmclk / tdm->samplerate - 1;
>> +
>> +       if ((syncdiv + 1) < (sl * sscale)) {
>> +               dev_err(tdm->dev, "Failed to set syncdiv!\n");
>> +               return -EINVAL;
>> +       }
>> +
>> +       if (tdm->syncm == TDM_SYNCM_LONG &&
>> +           (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
>> +               if ((syncdiv + 1) <= sl) {
>> +                       dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
>> +                       return -EINVAL;
>> +               }
>> +       }
>> +
>> +       jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
>> +                            struct snd_pcm_substream *substream)
>> +{
>> +       u32 datarx, datatx;
>> +       int ret;
>> +
>> +       ret = jh7110_tdm_syncdiv(tdm);
>> +       if (ret)
>> +               return ret;
>> +
>> +       datarx = (tdm->rx.ifl << IFL_BIT) |
>> +                 (tdm->rx.wl << WL_BIT) |
>> +                 (tdm->rx.sscale << SSCALE_BIT) |
>> +                 (tdm->rx.sl << SL_BIT) |
>> +                 (tdm->rx.lrj << LRJ_BIT);
>> +
>> +       datatx = (tdm->tx.ifl << IFL_BIT) |
>> +                 (tdm->tx.wl << WL_BIT) |
>> +                 (tdm->tx.sscale << SSCALE_BIT) |
>> +                 (tdm->tx.sl << SL_BIT) |
>> +                 (tdm->tx.lrj << LRJ_BIT);
>> +
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
>> +       else
>> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
>> +
>> +       return 0;
>> +}
>> +
>> +static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
>> +{
>> +       int i;
>> +
>> +       for (i = tdm->num_clks - 1; i >= 0; i--)
>> +               clk_disable_unprepare(tdm->clks[i]);
>> +}
> 
> This could be replaced by clk_bulk_disable_unprepare().

Thanks for your reminder, it will be changed.

> 
>> +
>> +static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
>> +{
>> +       int i, ret;
>> +
>> +       for (i = 0; i < tdm->num_clks; i++) {
>> +               ret = clk_prepare_enable(tdm->clks[i]);
>> +               if (ret) {
>> +                       while (i-- > 0)
>> +                               clk_disable_unprepare(tdm->clks[i]);
>> +                       return ret;
>> +               }
>> +       }
> 
> 
> And this could be replaced by clk_bulk_prepare_enable().

Will be changed.

> 
>> +
>> +       ret = reset_control_deassert(tdm->resets);
>> +       if (ret) {
>> +               dev_err(tdm->dev, "Failed to deassert tdm resets\n");
>> +               goto dis_tdm_clk;
>> +       }
>> +
>> +       /* select tdm_ext clock as the clock source for tdm */
>> +       ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
>> +       if (ret) {
>> +               dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
>> +               goto dis_tdm_clk;
>> +       }
>> +       return 0;
>> +
>> +dis_tdm_clk:
>> +       for (i = tdm->num_clks - 1; i >= 0; i--)
>> +               clk_disable_unprepare(tdm->clks[i]);
> 
> clk_bulk_disable_unprepare()

Will be changed.

> 
>> +
>> +       return ret;
>> +}
>> +
>> +#ifdef CONFIG_PM
>> +static int jh7110_tdm_runtime_suspend(struct device *dev)
>> +{
>> +       struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
>> +
>> +       jh7110_tdm_clk_disable(tdm);
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_runtime_resume(struct device *dev)
>> +{
>> +       struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
>> +
>> +       return jh7110_tdm_clk_enable(tdm);
>> +}
>> +#endif
>> +
>> +#ifdef CONFIG_PM_SLEEP
>> +static int jh7110_tdm_suspend(struct snd_soc_component *component)
>> +{
>> +       /* save context */
>> +       tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
>> +       tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);
>> +
>> +       return pm_runtime_force_suspend(component->dev);
>> +}
>> +
>> +static int jh7110_tdm_resume(struct snd_soc_component *component)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_component_get_drvdata(component);
>> +
>> +       /* restore context */
>> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, tdm->saved_pcmgbcr);
>> +       jh7110_tdm_writel(tdm, TDM_PCMDIV, tdm->saved_pcmdiv);
>> +
>> +       return pm_runtime_force_resume(component->dev);
>> +}
>> +
>> +#else
>> +#define jh7110_tdm_suspend     NULL
>> +#define jh7110_tdm_resume      NULL
>> +#endif
> 
> you may use pm_sleep_ptr() to avoid these.

OK, thanks for your prompt.

> 
>> +
>> +static const struct snd_soc_component_driver jh7110_tdm_component = {
>> +       .name = "jh7110-tdm",
>> +       .suspend = jh7110_tdm_suspend,
>> +       .resume = jh7110_tdm_resume,
>> +};
>> +
>> +static int jh7110_tdm_startup(struct snd_pcm_substream *substream,
>> +                             struct snd_soc_dai *cpu_dai)
>> +{
>> +       struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
>> +       struct snd_soc_dai_link *dai_link = rtd->dai_link;
>> +
>> +       dai_link->stop_dma_first = 1;
>> +
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_hw_params(struct snd_pcm_substream *substream,
>> +                               struct snd_pcm_hw_params *params,
>> +                               struct snd_soc_dai *dai)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
>> +       int chan_wl, chan_sl, chan_nr;
>> +       unsigned int data_width;
>> +       unsigned int dma_bus_width;
>> +       struct snd_dmaengine_dai_dma_data *dma_data = NULL;
>> +       int ret = 0;
>> +
>> +       data_width = params_width(params);
>> +
>> +       tdm->samplerate = params_rate(params);
>> +       tdm->pcmclk = params_channels(params) * tdm->samplerate * data_width;
>> +
>> +       switch (params_format(params)) {
>> +       case SNDRV_PCM_FORMAT_S16_LE:
>> +               chan_wl = TDM_16BIT_WORD_LEN;
>> +               chan_sl = TDM_16BIT_SLOT_LEN;
>> +               dma_bus_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
>> +               break;
>> +
>> +       case SNDRV_PCM_FORMAT_S32_LE:
>> +               chan_wl = TDM_32BIT_WORD_LEN;
>> +               chan_sl = TDM_32BIT_SLOT_LEN;
>> +               dma_bus_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
>> +               break;
>> +
>> +       default:
>> +               dev_err(tdm->dev, "tdm: unsupported PCM fmt");
>> +               return -EINVAL;
>> +       }
>> +
>> +       chan_nr = params_channels(params);
>> +       switch (chan_nr) {
>> +       case 1:
>> +       case 2:
>> +       case 4:
>> +       case 6:
>> +       case 8:
>> +               break;
>> +       default:
>> +               dev_err(tdm->dev, "channel not supported\n");
>> +               return -EINVAL;
>> +       }
>> +
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
>> +               tdm->tx.wl = chan_wl;
>> +               tdm->tx.sl = chan_sl;
>> +               tdm->tx.sscale = chan_nr;
>> +               tdm->play_dma_data.addr_width = dma_bus_width;
>> +               dma_data = &tdm->play_dma_data;
>> +       } else {
>> +               tdm->rx.wl = chan_wl;
>> +               tdm->rx.sl = chan_sl;
>> +               tdm->rx.sscale = chan_nr;
>> +               tdm->capture_dma_data.addr_width = dma_bus_width;
>> +               dma_data = &tdm->capture_dma_data;
>> +       }
>> +
>> +       snd_soc_dai_set_dma_data(dai, substream, dma_data);
>> +
>> +       ret = jh7110_tdm_config(tdm, substream);
>> +       if (ret)
>> +               return ret;
>> +
>> +       jh7110_tdm_save_context(tdm, substream);
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_trigger(struct snd_pcm_substream *substream,
>> +                             int cmd, struct snd_soc_dai *dai)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
>> +       int ret = 0;
>> +
>> +       switch (cmd) {
>> +       case SNDRV_PCM_TRIGGER_START:
>> +       case SNDRV_PCM_TRIGGER_RESUME:
>> +       case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
>> +               jh7110_tdm_start(tdm, substream);
>> +               break;
>> +
>> +       case SNDRV_PCM_TRIGGER_STOP:
>> +       case SNDRV_PCM_TRIGGER_SUSPEND:
>> +       case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
>> +               jh7110_tdm_stop(tdm, substream);
>> +               break;
>> +       default:
>> +               ret = -EINVAL;
>> +               break;
>> +       }
>> +       return ret;
>> +}
>> +
>> +static int jh7110_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
>> +                                 unsigned int fmt)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(cpu_dai);
>> +       unsigned int gbcr;
>> +       int ret = 0;
>> +
>> +       /* set master/slave audio interface */
>> +       switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
>> +       case SND_SOC_DAIFMT_BP_FP:
>> +               /* cpu is master */
>> +               tdm->ms_mode = TDM_AS_MASTER;
>> +               break;
>> +       case SND_SOC_DAIFMT_BC_FC:
>> +               /* codec is master */
>> +               tdm->ms_mode = TDM_AS_SLAVE;
>> +               break;
>> +       case SND_SOC_DAIFMT_BC_FP:
>> +       case SND_SOC_DAIFMT_BP_FC:
>> +               ret = -EINVAL;
>> +               break;
>> +       default:
>> +               dev_dbg(tdm->dev, "dwc : Invalid clock provider format\n");
>> +               ret = -EINVAL;
>> +               break;
>> +       }
>> +
>> +       gbcr = (tdm->clkpolity << CLKPOL_BIT) |
>> +               (tdm->elm << ELM_BIT) |
>> +               (tdm->syncm << SYNCM_BIT) |
>> +               (tdm->ms_mode << MS_BIT);
>> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, gbcr);
>> +
>> +       return ret;
>> +}
>> +
>> +static const struct snd_soc_dai_ops jh7110_tdm_dai_ops = {
>> +       .startup        = jh7110_tdm_startup,
>> +       .hw_params      = jh7110_tdm_hw_params,
>> +       .trigger        = jh7110_tdm_trigger,
>> +       .set_fmt        = jh7110_tdm_set_dai_fmt,
>> +};
>> +
>> +static int jh7110_tdm_dai_probe(struct snd_soc_dai *dai)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
>> +
>> +       snd_soc_dai_init_dma_data(dai, &tdm->play_dma_data, &tdm->capture_dma_data);
>> +       snd_soc_dai_set_drvdata(dai, tdm);
>> +       return 0;
>> +}
>> +
>> +#define JH7110_TDM_RATES       SNDRV_PCM_RATE_8000_48000
>> +
>> +#define JH7110_TDM_FORMATS     (SNDRV_PCM_FMTBIT_S16_LE | \
>> +                                SNDRV_PCM_FMTBIT_S32_LE)
>> +
>> +static struct snd_soc_dai_driver jh7110_tdm_dai = {
>> +       .name = "sf_tdm",
>> +       .id = 0,
>> +       .playback = {
>> +               .stream_name    = "Playback",
>> +               .channels_min   = 1,
>> +               .channels_max   = 8,
>> +               .rates          = JH7110_TDM_RATES,
>> +               .formats        = JH7110_TDM_FORMATS,
>> +       },
>> +       .capture = {
>> +               .stream_name    = "Capture",
>> +               .channels_min   = 1,
>> +               .channels_max   = 8,
>> +               .rates          = JH7110_TDM_RATES,
>> +               .formats        = JH7110_TDM_FORMATS,
>> +       },
>> +       .ops = &jh7110_tdm_dai_ops,
>> +       .probe = jh7110_tdm_dai_probe,
>> +       .symmetric_rate = 1,
>> +};
>> +
>> +static const struct snd_pcm_hardware jh7110_pcm_hardware = {
>> +       .info                   = (SNDRV_PCM_INFO_MMAP          |
>> +                                  SNDRV_PCM_INFO_MMAP_VALID    |
>> +                                  SNDRV_PCM_INFO_PAUSE         |
>> +                                  SNDRV_PCM_INFO_RESUME        |
>> +                                  SNDRV_PCM_INFO_INTERLEAVED   |
>> +                                  SNDRV_PCM_INFO_BLOCK_TRANSFER),
>> +       .buffer_bytes_max       = 192512,
>> +       .period_bytes_min       = 4096,
>> +       .period_bytes_max       = 32768,
>> +       .periods_min            = 1,
>> +       .periods_max            = 48,
>> +       .fifo_size              = 16,
>> +};
>> +
>> +static const struct snd_dmaengine_pcm_config jh7110_dmaengine_pcm_config = {
>> +       .pcm_hardware = &jh7110_pcm_hardware,
>> +       .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
>> +       .prealloc_buffer_size = 192512,
>> +};
>> +
>> +static void jh7110_tdm_init_params(struct jh7110_tdm_dev *tdm)
>> +{
>> +       tdm->clkpolity = TDM_TX_RASING_RX_FALLING;
>> +       tdm->elm = TDM_ELM_LATE;
>> +       tdm->syncm = TDM_SYNCM_SHORT;
>> +
>> +       tdm->rx.ifl = TDM_FIFO_HALF;
>> +       tdm->tx.ifl = TDM_FIFO_HALF;
>> +       tdm->rx.wl = TDM_16BIT_WORD_LEN;
>> +       tdm->tx.wl = TDM_16BIT_WORD_LEN;
>> +       tdm->rx.sscale = 2;
>> +       tdm->tx.sscale = 2;
>> +       tdm->rx.lrj = TDM_LEFT_JUSTIFT;
>> +       tdm->tx.lrj = TDM_LEFT_JUSTIFT;
>> +
>> +       tdm->play_dma_data.addr = JH7110_TDM_FIFO;
>> +       tdm->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
>> +       tdm->play_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
>> +       tdm->play_dma_data.maxburst = 16;
>> +
>> +       tdm->capture_dma_data.addr = JH7110_TDM_FIFO;
>> +       tdm->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
>> +       tdm->capture_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
>> +       tdm->capture_dma_data.maxburst = 8;
>> +}
>> +
>> +static int jh7110_tdm_clk_reset_init(struct platform_device *pdev,
>> +                                    struct jh7110_tdm_dev *tdm)
>> +{
>> +       int i, ret;
>> +
>> +       for (i = 0; i < tdm->num_clks; i++) {
>> +               tdm->clks[i] = devm_clk_get(&pdev->dev, tdm->clk_names[i]);
>> +               if (IS_ERR(tdm->clks[i])) {
>> +                       dev_err(&pdev->dev, "Failed to get clock: %s\n",
>> +                               tdm->clk_names[i]);
>> +                       return PTR_ERR(tdm->clks[i]);
>> +               }
>> +       }
> 
> devm_clk_bulk_get() ?

get/enable/disable clocks with 'devm_clk_bulk_' interface

> 
>> +
>> +       tdm->resets = devm_reset_control_array_get_exclusive(&pdev->dev);
>> +       if (IS_ERR(tdm->resets)) {
>> +               ret = PTR_ERR(tdm->resets);
>> +               dev_err(&pdev->dev, "Failed to get tdm resets");
>> +               return ret;
>> +       }
>> +
>> +       return jh7110_tdm_clk_enable(tdm);
>> +}
>> +
>> +static int jh7110_tdm_probe(struct platform_device *pdev)
>> +{
>> +       struct jh7110_tdm_dev *tdm;
>> +       const struct starfive_tdm_driverdata *driver_data;
>> +       int ret;
>> +
>> +       tdm = devm_kzalloc(&pdev->dev, sizeof(*tdm), GFP_KERNEL);
>> +       if (!tdm)
>> +               return -ENOMEM;
>> +
>> +       driver_data = of_device_get_match_data(&pdev->dev);
>> +       tdm->clk_names = (const char **)driver_data->clk_names;
>> +       tdm->num_clks = driver_data->num_clks;
>> +
>> +       tdm->tdm_base = devm_platform_ioremap_resource(pdev, 0);
>> +       if (IS_ERR(tdm->tdm_base))
>> +               return PTR_ERR(tdm->tdm_base);
>> +
>> +       tdm->dev = &pdev->dev;
>> +
>> +       ret = jh7110_tdm_clk_reset_init(pdev, tdm);
>> +       if (ret) {
>> +               dev_err(&pdev->dev, "Failed to enable audio-tdm clock\n");
>> +               return ret;
>> +       }
>> +
>> +       jh7110_tdm_init_params(tdm);
>> +
>> +       dev_set_drvdata(&pdev->dev, tdm);
>> +       ret = devm_snd_soc_register_component(&pdev->dev, &jh7110_tdm_component,
>> +                                             &jh7110_tdm_dai, 1);
>> +       if (ret != 0) {
> 
> if (ret)

Will be fixed.

> 
>> +               dev_err(&pdev->dev, "Failed to register dai\n");
>> +               return ret;
>> +       }
>> +
>> +       ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
>> +                                             &jh7110_dmaengine_pcm_config,
>> +                                             SND_DMAENGINE_PCM_FLAG_COMPAT);
>> +       if (ret) {
>> +               dev_err(&pdev->dev, "Could not register pcm: %d\n", ret);
>> +               return ret;
>> +       }
>> +
>> +       pm_runtime_enable(&pdev->dev);
>> +#ifdef CONFIG_PM
>> +       jh7110_tdm_clk_disable(tdm);
>> +#endif
>> +
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_dev_remove(struct platform_device *pdev)
>> +{
>> +       pm_runtime_disable(&pdev->dev);
>> +       return 0;
>> +}
>> +
>> +static const struct starfive_tdm_driverdata jh7110_drvdata = {
>> +       .clk_names = {"mclk_inner", "tdm_ahb", "tdm_apb", "tdm_internal", "tdm_ext", "tdm"},
>> +       .num_clks = 6,
>> +};
>> +
>> +static const struct of_device_id jh7110_tdm_of_match[] = {
>> +       { .compatible = "starfive,jh7110-tdm", .data = &jh7110_drvdata },
>> +       {}
>> +};
>> +
>> +MODULE_DEVICE_TABLE(of, jh7110_tdm_of_match);
>> +
>> +static const struct dev_pm_ops jh7110_tdm_pm_ops = {
>> +       SET_RUNTIME_PM_OPS(jh7110_tdm_runtime_suspend,
>> +                          jh7110_tdm_runtime_resume, NULL)
> 
> You can use RUNTIME_PM_OPS() and avoid having #ifdef CONFIG_PM around
> jh7110_tdm_runtime_suspend and jh7110_tdm_runtime_resume definitions.

I found that the definition of SET_RUNTIME_PM_OPS is based on RUNTIME_PM_OPS,
so CONFIG_PM is not needed.

Thank you very much for your detailed review and comments !

Best regards,
Walker

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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
@ 2023-05-09  7:07       ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-09  7:07 UTC (permalink / raw)
  To: Claudiu.Beznea, broonie, lgirdwood, perex, tiwai, robh+dt,
	krzysztof.kozlowski+dt, Conor.Dooley, emil.renner.berthing
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv


On 2023/5/9 12:33, Claudiu.Beznea@microchip.com wrote:
> On 06.05.2023 12:01, Walker Chen wrote:
>> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
>> 
>> Add tdm driver support for the StarFive JH7110 SoC.
>> 
>> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
>> ---
>>  MAINTAINERS                     |   6 +
>>  sound/soc/Kconfig               |   1 +
>>  sound/soc/Makefile              |   1 +
>>  sound/soc/starfive/Kconfig      |  15 +
>>  sound/soc/starfive/Makefile     |   2 +
>>  sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
>>  sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
>>  7 files changed, 745 insertions(+)
>>  create mode 100644 sound/soc/starfive/Kconfig
>>  create mode 100644 sound/soc/starfive/Makefile
>>  create mode 100644 sound/soc/starfive/jh7110_tdm.c
>>  create mode 100644 sound/soc/starfive/jh7110_tdm.h
>> 
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index 5f9c544bc189..add89615d327 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -19945,6 +19945,12 @@ F:     Documentation/devicetree/bindings/power/starfive*
>>  F:     drivers/soc/starfive/jh71xx_pmu.c
>>  F:     include/dt-bindings/power/starfive,jh7110-pmu.h
>> 
>> +STARFIVE JH7110 TDM DRIVERS
>> +M:     Walker Chen <walker.chen@starfivetech.com>
>> +S:     Maintained
>> +F:     Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
>> +F:     sound/soc/starfive/jh7110-tdm.*
>> +
>>  STARFIVE SOC DRIVERS
>>  M:     Conor Dooley <conor@kernel.org>
>>  S:     Maintained
>> diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
>> index 848fbae26c3b..8d1d9401ecf2 100644
>> --- a/sound/soc/Kconfig
>> +++ b/sound/soc/Kconfig
>> @@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
>>  source "sound/soc/sof/Kconfig"
>>  source "sound/soc/spear/Kconfig"
>>  source "sound/soc/sprd/Kconfig"
>> +source "sound/soc/starfive/Kconfig"
>>  source "sound/soc/sti/Kconfig"
>>  source "sound/soc/stm/Kconfig"
>>  source "sound/soc/sunxi/Kconfig"
>> diff --git a/sound/soc/Makefile b/sound/soc/Makefile
>> index 507eaed1d6a1..65aeb4ef4068 100644
>> --- a/sound/soc/Makefile
>> +++ b/sound/soc/Makefile
>> @@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC) += sh/
>>  obj-$(CONFIG_SND_SOC)  += sof/
>>  obj-$(CONFIG_SND_SOC)  += spear/
>>  obj-$(CONFIG_SND_SOC)  += sprd/
>> +obj-$(CONFIG_SND_SOC)  += starfive/
>>  obj-$(CONFIG_SND_SOC)  += sti/
>>  obj-$(CONFIG_SND_SOC)  += stm/
>>  obj-$(CONFIG_SND_SOC)  += sunxi/
>> diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
>> new file mode 100644
>> index 000000000000..737c956f7b93
>> --- /dev/null
>> +++ b/sound/soc/starfive/Kconfig
>> @@ -0,0 +1,15 @@
>> +# SPDX-License-Identifier: GPL-2.0-only
>> +config SND_SOC_STARFIVE
>> +       tristate "Audio support for StarFive SoC"
>> +       depends on COMPILE_TEST || SOC_STARFIVE
>> +       help
>> +         Say Y or M if you want to add support for codecs attached to
>> +         the Starfive SoCs' Audio interfaces. You will also need to
>> +         select the audio interfaces to support below.
>> +
>> +config SND_SOC_JH7110_TDM
>> +       tristate "JH7110 TDM device driver"
>> +       depends on HAVE_CLK && SND_SOC_STARFIVE
>> +       select SND_SOC_GENERIC_DMAENGINE_PCM
>> +       help
>> +         Say Y or M if you want to add support for StarFive TDM driver.
>> diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
>> new file mode 100644
>> index 000000000000..f7d960211d72
>> --- /dev/null
>> +++ b/sound/soc/starfive/Makefile
>> @@ -0,0 +1,2 @@
>> +# StarFive Platform Support
>> +obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
>> diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
>> new file mode 100644
>> index 000000000000..33f7cf43e4bd
>> --- /dev/null
>> +++ b/sound/soc/starfive/jh7110_tdm.c
>> @@ -0,0 +1,573 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * jh7110_tdm.c -- StarFive JH7110 TDM driver
>> + *
>> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
>> + *
>> + * Author: Walker Chen <walker.chen@starfivetech.com>
>> + */
>> +
>> +#include <linux/clk.h>
>> +#include <linux/device.h>
>> +#include <linux/module.h>
>> +#include <linux/of_irq.h>
>> +#include <linux/of_platform.h>
>> +#include <linux/pm_runtime.h>
>> +#include <linux/regmap.h>
>> +#include <linux/reset.h>
>> +#include <sound/initval.h>
>> +#include <sound/pcm_params.h>
>> +#include <sound/soc.h>
>> +#include <sound/soc-dai.h>
>> +#include "jh7110_tdm.h"
>> +
>> +static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
>> +{
>> +       return readl_relaxed(tdm->tdm_base + reg);
>> +}
>> +
>> +static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
>> +{
>> +       writel_relaxed(val, tdm->tdm_base + reg);
>> +}
>> +
>> +static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
>> +                                   struct snd_pcm_substream *substream)
>> +{
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +               tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
>> +       else
>> +               tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
>> +}
>> +
>> +static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
>> +                            struct snd_pcm_substream *substream)
>> +{
>> +       u32 data;
>> +
>> +       data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
>> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
>> +
>> +       /* restore context */
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
>> +       else
>> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
>> +}
>> +
>> +static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
>> +                           struct snd_pcm_substream *substream)
>> +{
>> +       unsigned int val;
>> +
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
>> +               val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
>> +               val &= ~PCMTXCR_TXEN;
>> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
>> +       } else {
>> +               val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
>> +               val &= ~PCMRXCR_RXEN;
>> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
>> +       }
>> +}
>> +
>> +static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
>> +{
>> +       u32 sl, sscale, syncdiv;
>> +
>> +       if (tdm->rx.sl >= tdm->tx.sl)
>> +               sl = tdm->rx.sl;
>> +       else
>> +               sl = tdm->tx.sl;
>> +
>> +       if (tdm->rx.sscale >= tdm->tx.sscale)
>> +               sscale = tdm->rx.sscale;
>> +       else
>> +               sscale = tdm->tx.sscale;
>> +
>> +       syncdiv = tdm->pcmclk / tdm->samplerate - 1;
>> +
>> +       if ((syncdiv + 1) < (sl * sscale)) {
>> +               dev_err(tdm->dev, "Failed to set syncdiv!\n");
>> +               return -EINVAL;
>> +       }
>> +
>> +       if (tdm->syncm == TDM_SYNCM_LONG &&
>> +           (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
>> +               if ((syncdiv + 1) <= sl) {
>> +                       dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
>> +                       return -EINVAL;
>> +               }
>> +       }
>> +
>> +       jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
>> +                            struct snd_pcm_substream *substream)
>> +{
>> +       u32 datarx, datatx;
>> +       int ret;
>> +
>> +       ret = jh7110_tdm_syncdiv(tdm);
>> +       if (ret)
>> +               return ret;
>> +
>> +       datarx = (tdm->rx.ifl << IFL_BIT) |
>> +                 (tdm->rx.wl << WL_BIT) |
>> +                 (tdm->rx.sscale << SSCALE_BIT) |
>> +                 (tdm->rx.sl << SL_BIT) |
>> +                 (tdm->rx.lrj << LRJ_BIT);
>> +
>> +       datatx = (tdm->tx.ifl << IFL_BIT) |
>> +                 (tdm->tx.wl << WL_BIT) |
>> +                 (tdm->tx.sscale << SSCALE_BIT) |
>> +                 (tdm->tx.sl << SL_BIT) |
>> +                 (tdm->tx.lrj << LRJ_BIT);
>> +
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
>> +       else
>> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
>> +
>> +       return 0;
>> +}
>> +
>> +static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
>> +{
>> +       int i;
>> +
>> +       for (i = tdm->num_clks - 1; i >= 0; i--)
>> +               clk_disable_unprepare(tdm->clks[i]);
>> +}
> 
> This could be replaced by clk_bulk_disable_unprepare().

Thanks for your reminder, it will be changed.

> 
>> +
>> +static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
>> +{
>> +       int i, ret;
>> +
>> +       for (i = 0; i < tdm->num_clks; i++) {
>> +               ret = clk_prepare_enable(tdm->clks[i]);
>> +               if (ret) {
>> +                       while (i-- > 0)
>> +                               clk_disable_unprepare(tdm->clks[i]);
>> +                       return ret;
>> +               }
>> +       }
> 
> 
> And this could be replaced by clk_bulk_prepare_enable().

Will be changed.

> 
>> +
>> +       ret = reset_control_deassert(tdm->resets);
>> +       if (ret) {
>> +               dev_err(tdm->dev, "Failed to deassert tdm resets\n");
>> +               goto dis_tdm_clk;
>> +       }
>> +
>> +       /* select tdm_ext clock as the clock source for tdm */
>> +       ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
>> +       if (ret) {
>> +               dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
>> +               goto dis_tdm_clk;
>> +       }
>> +       return 0;
>> +
>> +dis_tdm_clk:
>> +       for (i = tdm->num_clks - 1; i >= 0; i--)
>> +               clk_disable_unprepare(tdm->clks[i]);
> 
> clk_bulk_disable_unprepare()

Will be changed.

> 
>> +
>> +       return ret;
>> +}
>> +
>> +#ifdef CONFIG_PM
>> +static int jh7110_tdm_runtime_suspend(struct device *dev)
>> +{
>> +       struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
>> +
>> +       jh7110_tdm_clk_disable(tdm);
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_runtime_resume(struct device *dev)
>> +{
>> +       struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
>> +
>> +       return jh7110_tdm_clk_enable(tdm);
>> +}
>> +#endif
>> +
>> +#ifdef CONFIG_PM_SLEEP
>> +static int jh7110_tdm_suspend(struct snd_soc_component *component)
>> +{
>> +       /* save context */
>> +       tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
>> +       tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);
>> +
>> +       return pm_runtime_force_suspend(component->dev);
>> +}
>> +
>> +static int jh7110_tdm_resume(struct snd_soc_component *component)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_component_get_drvdata(component);
>> +
>> +       /* restore context */
>> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, tdm->saved_pcmgbcr);
>> +       jh7110_tdm_writel(tdm, TDM_PCMDIV, tdm->saved_pcmdiv);
>> +
>> +       return pm_runtime_force_resume(component->dev);
>> +}
>> +
>> +#else
>> +#define jh7110_tdm_suspend     NULL
>> +#define jh7110_tdm_resume      NULL
>> +#endif
> 
> you may use pm_sleep_ptr() to avoid these.

OK, thanks for your prompt.

> 
>> +
>> +static const struct snd_soc_component_driver jh7110_tdm_component = {
>> +       .name = "jh7110-tdm",
>> +       .suspend = jh7110_tdm_suspend,
>> +       .resume = jh7110_tdm_resume,
>> +};
>> +
>> +static int jh7110_tdm_startup(struct snd_pcm_substream *substream,
>> +                             struct snd_soc_dai *cpu_dai)
>> +{
>> +       struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
>> +       struct snd_soc_dai_link *dai_link = rtd->dai_link;
>> +
>> +       dai_link->stop_dma_first = 1;
>> +
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_hw_params(struct snd_pcm_substream *substream,
>> +                               struct snd_pcm_hw_params *params,
>> +                               struct snd_soc_dai *dai)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
>> +       int chan_wl, chan_sl, chan_nr;
>> +       unsigned int data_width;
>> +       unsigned int dma_bus_width;
>> +       struct snd_dmaengine_dai_dma_data *dma_data = NULL;
>> +       int ret = 0;
>> +
>> +       data_width = params_width(params);
>> +
>> +       tdm->samplerate = params_rate(params);
>> +       tdm->pcmclk = params_channels(params) * tdm->samplerate * data_width;
>> +
>> +       switch (params_format(params)) {
>> +       case SNDRV_PCM_FORMAT_S16_LE:
>> +               chan_wl = TDM_16BIT_WORD_LEN;
>> +               chan_sl = TDM_16BIT_SLOT_LEN;
>> +               dma_bus_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
>> +               break;
>> +
>> +       case SNDRV_PCM_FORMAT_S32_LE:
>> +               chan_wl = TDM_32BIT_WORD_LEN;
>> +               chan_sl = TDM_32BIT_SLOT_LEN;
>> +               dma_bus_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
>> +               break;
>> +
>> +       default:
>> +               dev_err(tdm->dev, "tdm: unsupported PCM fmt");
>> +               return -EINVAL;
>> +       }
>> +
>> +       chan_nr = params_channels(params);
>> +       switch (chan_nr) {
>> +       case 1:
>> +       case 2:
>> +       case 4:
>> +       case 6:
>> +       case 8:
>> +               break;
>> +       default:
>> +               dev_err(tdm->dev, "channel not supported\n");
>> +               return -EINVAL;
>> +       }
>> +
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
>> +               tdm->tx.wl = chan_wl;
>> +               tdm->tx.sl = chan_sl;
>> +               tdm->tx.sscale = chan_nr;
>> +               tdm->play_dma_data.addr_width = dma_bus_width;
>> +               dma_data = &tdm->play_dma_data;
>> +       } else {
>> +               tdm->rx.wl = chan_wl;
>> +               tdm->rx.sl = chan_sl;
>> +               tdm->rx.sscale = chan_nr;
>> +               tdm->capture_dma_data.addr_width = dma_bus_width;
>> +               dma_data = &tdm->capture_dma_data;
>> +       }
>> +
>> +       snd_soc_dai_set_dma_data(dai, substream, dma_data);
>> +
>> +       ret = jh7110_tdm_config(tdm, substream);
>> +       if (ret)
>> +               return ret;
>> +
>> +       jh7110_tdm_save_context(tdm, substream);
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_trigger(struct snd_pcm_substream *substream,
>> +                             int cmd, struct snd_soc_dai *dai)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
>> +       int ret = 0;
>> +
>> +       switch (cmd) {
>> +       case SNDRV_PCM_TRIGGER_START:
>> +       case SNDRV_PCM_TRIGGER_RESUME:
>> +       case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
>> +               jh7110_tdm_start(tdm, substream);
>> +               break;
>> +
>> +       case SNDRV_PCM_TRIGGER_STOP:
>> +       case SNDRV_PCM_TRIGGER_SUSPEND:
>> +       case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
>> +               jh7110_tdm_stop(tdm, substream);
>> +               break;
>> +       default:
>> +               ret = -EINVAL;
>> +               break;
>> +       }
>> +       return ret;
>> +}
>> +
>> +static int jh7110_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
>> +                                 unsigned int fmt)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(cpu_dai);
>> +       unsigned int gbcr;
>> +       int ret = 0;
>> +
>> +       /* set master/slave audio interface */
>> +       switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
>> +       case SND_SOC_DAIFMT_BP_FP:
>> +               /* cpu is master */
>> +               tdm->ms_mode = TDM_AS_MASTER;
>> +               break;
>> +       case SND_SOC_DAIFMT_BC_FC:
>> +               /* codec is master */
>> +               tdm->ms_mode = TDM_AS_SLAVE;
>> +               break;
>> +       case SND_SOC_DAIFMT_BC_FP:
>> +       case SND_SOC_DAIFMT_BP_FC:
>> +               ret = -EINVAL;
>> +               break;
>> +       default:
>> +               dev_dbg(tdm->dev, "dwc : Invalid clock provider format\n");
>> +               ret = -EINVAL;
>> +               break;
>> +       }
>> +
>> +       gbcr = (tdm->clkpolity << CLKPOL_BIT) |
>> +               (tdm->elm << ELM_BIT) |
>> +               (tdm->syncm << SYNCM_BIT) |
>> +               (tdm->ms_mode << MS_BIT);
>> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, gbcr);
>> +
>> +       return ret;
>> +}
>> +
>> +static const struct snd_soc_dai_ops jh7110_tdm_dai_ops = {
>> +       .startup        = jh7110_tdm_startup,
>> +       .hw_params      = jh7110_tdm_hw_params,
>> +       .trigger        = jh7110_tdm_trigger,
>> +       .set_fmt        = jh7110_tdm_set_dai_fmt,
>> +};
>> +
>> +static int jh7110_tdm_dai_probe(struct snd_soc_dai *dai)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
>> +
>> +       snd_soc_dai_init_dma_data(dai, &tdm->play_dma_data, &tdm->capture_dma_data);
>> +       snd_soc_dai_set_drvdata(dai, tdm);
>> +       return 0;
>> +}
>> +
>> +#define JH7110_TDM_RATES       SNDRV_PCM_RATE_8000_48000
>> +
>> +#define JH7110_TDM_FORMATS     (SNDRV_PCM_FMTBIT_S16_LE | \
>> +                                SNDRV_PCM_FMTBIT_S32_LE)
>> +
>> +static struct snd_soc_dai_driver jh7110_tdm_dai = {
>> +       .name = "sf_tdm",
>> +       .id = 0,
>> +       .playback = {
>> +               .stream_name    = "Playback",
>> +               .channels_min   = 1,
>> +               .channels_max   = 8,
>> +               .rates          = JH7110_TDM_RATES,
>> +               .formats        = JH7110_TDM_FORMATS,
>> +       },
>> +       .capture = {
>> +               .stream_name    = "Capture",
>> +               .channels_min   = 1,
>> +               .channels_max   = 8,
>> +               .rates          = JH7110_TDM_RATES,
>> +               .formats        = JH7110_TDM_FORMATS,
>> +       },
>> +       .ops = &jh7110_tdm_dai_ops,
>> +       .probe = jh7110_tdm_dai_probe,
>> +       .symmetric_rate = 1,
>> +};
>> +
>> +static const struct snd_pcm_hardware jh7110_pcm_hardware = {
>> +       .info                   = (SNDRV_PCM_INFO_MMAP          |
>> +                                  SNDRV_PCM_INFO_MMAP_VALID    |
>> +                                  SNDRV_PCM_INFO_PAUSE         |
>> +                                  SNDRV_PCM_INFO_RESUME        |
>> +                                  SNDRV_PCM_INFO_INTERLEAVED   |
>> +                                  SNDRV_PCM_INFO_BLOCK_TRANSFER),
>> +       .buffer_bytes_max       = 192512,
>> +       .period_bytes_min       = 4096,
>> +       .period_bytes_max       = 32768,
>> +       .periods_min            = 1,
>> +       .periods_max            = 48,
>> +       .fifo_size              = 16,
>> +};
>> +
>> +static const struct snd_dmaengine_pcm_config jh7110_dmaengine_pcm_config = {
>> +       .pcm_hardware = &jh7110_pcm_hardware,
>> +       .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
>> +       .prealloc_buffer_size = 192512,
>> +};
>> +
>> +static void jh7110_tdm_init_params(struct jh7110_tdm_dev *tdm)
>> +{
>> +       tdm->clkpolity = TDM_TX_RASING_RX_FALLING;
>> +       tdm->elm = TDM_ELM_LATE;
>> +       tdm->syncm = TDM_SYNCM_SHORT;
>> +
>> +       tdm->rx.ifl = TDM_FIFO_HALF;
>> +       tdm->tx.ifl = TDM_FIFO_HALF;
>> +       tdm->rx.wl = TDM_16BIT_WORD_LEN;
>> +       tdm->tx.wl = TDM_16BIT_WORD_LEN;
>> +       tdm->rx.sscale = 2;
>> +       tdm->tx.sscale = 2;
>> +       tdm->rx.lrj = TDM_LEFT_JUSTIFT;
>> +       tdm->tx.lrj = TDM_LEFT_JUSTIFT;
>> +
>> +       tdm->play_dma_data.addr = JH7110_TDM_FIFO;
>> +       tdm->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
>> +       tdm->play_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
>> +       tdm->play_dma_data.maxburst = 16;
>> +
>> +       tdm->capture_dma_data.addr = JH7110_TDM_FIFO;
>> +       tdm->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
>> +       tdm->capture_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
>> +       tdm->capture_dma_data.maxburst = 8;
>> +}
>> +
>> +static int jh7110_tdm_clk_reset_init(struct platform_device *pdev,
>> +                                    struct jh7110_tdm_dev *tdm)
>> +{
>> +       int i, ret;
>> +
>> +       for (i = 0; i < tdm->num_clks; i++) {
>> +               tdm->clks[i] = devm_clk_get(&pdev->dev, tdm->clk_names[i]);
>> +               if (IS_ERR(tdm->clks[i])) {
>> +                       dev_err(&pdev->dev, "Failed to get clock: %s\n",
>> +                               tdm->clk_names[i]);
>> +                       return PTR_ERR(tdm->clks[i]);
>> +               }
>> +       }
> 
> devm_clk_bulk_get() ?

get/enable/disable clocks with 'devm_clk_bulk_' interface

> 
>> +
>> +       tdm->resets = devm_reset_control_array_get_exclusive(&pdev->dev);
>> +       if (IS_ERR(tdm->resets)) {
>> +               ret = PTR_ERR(tdm->resets);
>> +               dev_err(&pdev->dev, "Failed to get tdm resets");
>> +               return ret;
>> +       }
>> +
>> +       return jh7110_tdm_clk_enable(tdm);
>> +}
>> +
>> +static int jh7110_tdm_probe(struct platform_device *pdev)
>> +{
>> +       struct jh7110_tdm_dev *tdm;
>> +       const struct starfive_tdm_driverdata *driver_data;
>> +       int ret;
>> +
>> +       tdm = devm_kzalloc(&pdev->dev, sizeof(*tdm), GFP_KERNEL);
>> +       if (!tdm)
>> +               return -ENOMEM;
>> +
>> +       driver_data = of_device_get_match_data(&pdev->dev);
>> +       tdm->clk_names = (const char **)driver_data->clk_names;
>> +       tdm->num_clks = driver_data->num_clks;
>> +
>> +       tdm->tdm_base = devm_platform_ioremap_resource(pdev, 0);
>> +       if (IS_ERR(tdm->tdm_base))
>> +               return PTR_ERR(tdm->tdm_base);
>> +
>> +       tdm->dev = &pdev->dev;
>> +
>> +       ret = jh7110_tdm_clk_reset_init(pdev, tdm);
>> +       if (ret) {
>> +               dev_err(&pdev->dev, "Failed to enable audio-tdm clock\n");
>> +               return ret;
>> +       }
>> +
>> +       jh7110_tdm_init_params(tdm);
>> +
>> +       dev_set_drvdata(&pdev->dev, tdm);
>> +       ret = devm_snd_soc_register_component(&pdev->dev, &jh7110_tdm_component,
>> +                                             &jh7110_tdm_dai, 1);
>> +       if (ret != 0) {
> 
> if (ret)

Will be fixed.

> 
>> +               dev_err(&pdev->dev, "Failed to register dai\n");
>> +               return ret;
>> +       }
>> +
>> +       ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
>> +                                             &jh7110_dmaengine_pcm_config,
>> +                                             SND_DMAENGINE_PCM_FLAG_COMPAT);
>> +       if (ret) {
>> +               dev_err(&pdev->dev, "Could not register pcm: %d\n", ret);
>> +               return ret;
>> +       }
>> +
>> +       pm_runtime_enable(&pdev->dev);
>> +#ifdef CONFIG_PM
>> +       jh7110_tdm_clk_disable(tdm);
>> +#endif
>> +
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_dev_remove(struct platform_device *pdev)
>> +{
>> +       pm_runtime_disable(&pdev->dev);
>> +       return 0;
>> +}
>> +
>> +static const struct starfive_tdm_driverdata jh7110_drvdata = {
>> +       .clk_names = {"mclk_inner", "tdm_ahb", "tdm_apb", "tdm_internal", "tdm_ext", "tdm"},
>> +       .num_clks = 6,
>> +};
>> +
>> +static const struct of_device_id jh7110_tdm_of_match[] = {
>> +       { .compatible = "starfive,jh7110-tdm", .data = &jh7110_drvdata },
>> +       {}
>> +};
>> +
>> +MODULE_DEVICE_TABLE(of, jh7110_tdm_of_match);
>> +
>> +static const struct dev_pm_ops jh7110_tdm_pm_ops = {
>> +       SET_RUNTIME_PM_OPS(jh7110_tdm_runtime_suspend,
>> +                          jh7110_tdm_runtime_resume, NULL)
> 
> You can use RUNTIME_PM_OPS() and avoid having #ifdef CONFIG_PM around
> jh7110_tdm_runtime_suspend and jh7110_tdm_runtime_resume definitions.

I found that the definition of SET_RUNTIME_PM_OPS is based on RUNTIME_PM_OPS,
so CONFIG_PM is not needed.

Thank you very much for your detailed review and comments !

Best regards,
Walker

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver
@ 2023-05-09  7:07       ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-09  7:07 UTC (permalink / raw)
  To: Claudiu.Beznea, broonie, lgirdwood, perex, tiwai, robh+dt,
	krzysztof.kozlowski+dt, Conor.Dooley, emil.renner.berthing
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv


On 2023/5/9 12:33, Claudiu.Beznea@microchip.com wrote:
> On 06.05.2023 12:01, Walker Chen wrote:
>> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
>> 
>> Add tdm driver support for the StarFive JH7110 SoC.
>> 
>> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
>> ---
>>  MAINTAINERS                     |   6 +
>>  sound/soc/Kconfig               |   1 +
>>  sound/soc/Makefile              |   1 +
>>  sound/soc/starfive/Kconfig      |  15 +
>>  sound/soc/starfive/Makefile     |   2 +
>>  sound/soc/starfive/jh7110_tdm.c | 573 ++++++++++++++++++++++++++++++++
>>  sound/soc/starfive/jh7110_tdm.h | 147 ++++++++
>>  7 files changed, 745 insertions(+)
>>  create mode 100644 sound/soc/starfive/Kconfig
>>  create mode 100644 sound/soc/starfive/Makefile
>>  create mode 100644 sound/soc/starfive/jh7110_tdm.c
>>  create mode 100644 sound/soc/starfive/jh7110_tdm.h
>> 
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index 5f9c544bc189..add89615d327 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -19945,6 +19945,12 @@ F:     Documentation/devicetree/bindings/power/starfive*
>>  F:     drivers/soc/starfive/jh71xx_pmu.c
>>  F:     include/dt-bindings/power/starfive,jh7110-pmu.h
>> 
>> +STARFIVE JH7110 TDM DRIVERS
>> +M:     Walker Chen <walker.chen@starfivetech.com>
>> +S:     Maintained
>> +F:     Documentation/devicetree/bindings/sound/starfive,jh7110-tdm.yaml
>> +F:     sound/soc/starfive/jh7110-tdm.*
>> +
>>  STARFIVE SOC DRIVERS
>>  M:     Conor Dooley <conor@kernel.org>
>>  S:     Maintained
>> diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
>> index 848fbae26c3b..8d1d9401ecf2 100644
>> --- a/sound/soc/Kconfig
>> +++ b/sound/soc/Kconfig
>> @@ -91,6 +91,7 @@ source "sound/soc/sh/Kconfig"
>>  source "sound/soc/sof/Kconfig"
>>  source "sound/soc/spear/Kconfig"
>>  source "sound/soc/sprd/Kconfig"
>> +source "sound/soc/starfive/Kconfig"
>>  source "sound/soc/sti/Kconfig"
>>  source "sound/soc/stm/Kconfig"
>>  source "sound/soc/sunxi/Kconfig"
>> diff --git a/sound/soc/Makefile b/sound/soc/Makefile
>> index 507eaed1d6a1..65aeb4ef4068 100644
>> --- a/sound/soc/Makefile
>> +++ b/sound/soc/Makefile
>> @@ -59,6 +59,7 @@ obj-$(CONFIG_SND_SOC) += sh/
>>  obj-$(CONFIG_SND_SOC)  += sof/
>>  obj-$(CONFIG_SND_SOC)  += spear/
>>  obj-$(CONFIG_SND_SOC)  += sprd/
>> +obj-$(CONFIG_SND_SOC)  += starfive/
>>  obj-$(CONFIG_SND_SOC)  += sti/
>>  obj-$(CONFIG_SND_SOC)  += stm/
>>  obj-$(CONFIG_SND_SOC)  += sunxi/
>> diff --git a/sound/soc/starfive/Kconfig b/sound/soc/starfive/Kconfig
>> new file mode 100644
>> index 000000000000..737c956f7b93
>> --- /dev/null
>> +++ b/sound/soc/starfive/Kconfig
>> @@ -0,0 +1,15 @@
>> +# SPDX-License-Identifier: GPL-2.0-only
>> +config SND_SOC_STARFIVE
>> +       tristate "Audio support for StarFive SoC"
>> +       depends on COMPILE_TEST || SOC_STARFIVE
>> +       help
>> +         Say Y or M if you want to add support for codecs attached to
>> +         the Starfive SoCs' Audio interfaces. You will also need to
>> +         select the audio interfaces to support below.
>> +
>> +config SND_SOC_JH7110_TDM
>> +       tristate "JH7110 TDM device driver"
>> +       depends on HAVE_CLK && SND_SOC_STARFIVE
>> +       select SND_SOC_GENERIC_DMAENGINE_PCM
>> +       help
>> +         Say Y or M if you want to add support for StarFive TDM driver.
>> diff --git a/sound/soc/starfive/Makefile b/sound/soc/starfive/Makefile
>> new file mode 100644
>> index 000000000000..f7d960211d72
>> --- /dev/null
>> +++ b/sound/soc/starfive/Makefile
>> @@ -0,0 +1,2 @@
>> +# StarFive Platform Support
>> +obj-$(CONFIG_SND_SOC_JH7110_TDM) += jh7110_tdm.o
>> diff --git a/sound/soc/starfive/jh7110_tdm.c b/sound/soc/starfive/jh7110_tdm.c
>> new file mode 100644
>> index 000000000000..33f7cf43e4bd
>> --- /dev/null
>> +++ b/sound/soc/starfive/jh7110_tdm.c
>> @@ -0,0 +1,573 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * jh7110_tdm.c -- StarFive JH7110 TDM driver
>> + *
>> + * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
>> + *
>> + * Author: Walker Chen <walker.chen@starfivetech.com>
>> + */
>> +
>> +#include <linux/clk.h>
>> +#include <linux/device.h>
>> +#include <linux/module.h>
>> +#include <linux/of_irq.h>
>> +#include <linux/of_platform.h>
>> +#include <linux/pm_runtime.h>
>> +#include <linux/regmap.h>
>> +#include <linux/reset.h>
>> +#include <sound/initval.h>
>> +#include <sound/pcm_params.h>
>> +#include <sound/soc.h>
>> +#include <sound/soc-dai.h>
>> +#include "jh7110_tdm.h"
>> +
>> +static inline u32 jh7110_tdm_readl(struct jh7110_tdm_dev *tdm, u16 reg)
>> +{
>> +       return readl_relaxed(tdm->tdm_base + reg);
>> +}
>> +
>> +static inline void jh7110_tdm_writel(struct jh7110_tdm_dev *tdm, u16 reg, u32 val)
>> +{
>> +       writel_relaxed(val, tdm->tdm_base + reg);
>> +}
>> +
>> +static void jh7110_tdm_save_context(struct jh7110_tdm_dev *tdm,
>> +                                   struct snd_pcm_substream *substream)
>> +{
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +               tdm->saved_pcmtxcr = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
>> +       else
>> +               tdm->saved_pcmrxcr = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
>> +}
>> +
>> +static void jh7110_tdm_start(struct jh7110_tdm_dev *tdm,
>> +                            struct snd_pcm_substream *substream)
>> +{
>> +       u32 data;
>> +
>> +       data = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
>> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, data | PCMGBCR_ENABLE);
>> +
>> +       /* restore context */
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, tdm->saved_pcmtxcr | PCMTXCR_TXEN);
>> +       else
>> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, tdm->saved_pcmrxcr | PCMRXCR_RXEN);
>> +}
>> +
>> +static void jh7110_tdm_stop(struct jh7110_tdm_dev *tdm,
>> +                           struct snd_pcm_substream *substream)
>> +{
>> +       unsigned int val;
>> +
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
>> +               val = jh7110_tdm_readl(tdm, TDM_PCMTXCR);
>> +               val &= ~PCMTXCR_TXEN;
>> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, val);
>> +       } else {
>> +               val = jh7110_tdm_readl(tdm, TDM_PCMRXCR);
>> +               val &= ~PCMRXCR_RXEN;
>> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, val);
>> +       }
>> +}
>> +
>> +static int jh7110_tdm_syncdiv(struct jh7110_tdm_dev *tdm)
>> +{
>> +       u32 sl, sscale, syncdiv;
>> +
>> +       if (tdm->rx.sl >= tdm->tx.sl)
>> +               sl = tdm->rx.sl;
>> +       else
>> +               sl = tdm->tx.sl;
>> +
>> +       if (tdm->rx.sscale >= tdm->tx.sscale)
>> +               sscale = tdm->rx.sscale;
>> +       else
>> +               sscale = tdm->tx.sscale;
>> +
>> +       syncdiv = tdm->pcmclk / tdm->samplerate - 1;
>> +
>> +       if ((syncdiv + 1) < (sl * sscale)) {
>> +               dev_err(tdm->dev, "Failed to set syncdiv!\n");
>> +               return -EINVAL;
>> +       }
>> +
>> +       if (tdm->syncm == TDM_SYNCM_LONG &&
>> +           (tdm->rx.sscale <= 1 || tdm->tx.sscale <= 1)) {
>> +               if ((syncdiv + 1) <= sl) {
>> +                       dev_err(tdm->dev, "Wrong syncdiv! It must be (syncdiv+1) > max[tx.sl, rx.sl]\n");
>> +                       return -EINVAL;
>> +               }
>> +       }
>> +
>> +       jh7110_tdm_writel(tdm, TDM_PCMDIV, syncdiv);
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_config(struct jh7110_tdm_dev *tdm,
>> +                            struct snd_pcm_substream *substream)
>> +{
>> +       u32 datarx, datatx;
>> +       int ret;
>> +
>> +       ret = jh7110_tdm_syncdiv(tdm);
>> +       if (ret)
>> +               return ret;
>> +
>> +       datarx = (tdm->rx.ifl << IFL_BIT) |
>> +                 (tdm->rx.wl << WL_BIT) |
>> +                 (tdm->rx.sscale << SSCALE_BIT) |
>> +                 (tdm->rx.sl << SL_BIT) |
>> +                 (tdm->rx.lrj << LRJ_BIT);
>> +
>> +       datatx = (tdm->tx.ifl << IFL_BIT) |
>> +                 (tdm->tx.wl << WL_BIT) |
>> +                 (tdm->tx.sscale << SSCALE_BIT) |
>> +                 (tdm->tx.sl << SL_BIT) |
>> +                 (tdm->tx.lrj << LRJ_BIT);
>> +
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>> +               jh7110_tdm_writel(tdm, TDM_PCMTXCR, datatx);
>> +       else
>> +               jh7110_tdm_writel(tdm, TDM_PCMRXCR, datarx);
>> +
>> +       return 0;
>> +}
>> +
>> +static void jh7110_tdm_clk_disable(struct jh7110_tdm_dev *tdm)
>> +{
>> +       int i;
>> +
>> +       for (i = tdm->num_clks - 1; i >= 0; i--)
>> +               clk_disable_unprepare(tdm->clks[i]);
>> +}
> 
> This could be replaced by clk_bulk_disable_unprepare().

Thanks for your reminder, it will be changed.

> 
>> +
>> +static int jh7110_tdm_clk_enable(struct jh7110_tdm_dev *tdm)
>> +{
>> +       int i, ret;
>> +
>> +       for (i = 0; i < tdm->num_clks; i++) {
>> +               ret = clk_prepare_enable(tdm->clks[i]);
>> +               if (ret) {
>> +                       while (i-- > 0)
>> +                               clk_disable_unprepare(tdm->clks[i]);
>> +                       return ret;
>> +               }
>> +       }
> 
> 
> And this could be replaced by clk_bulk_prepare_enable().

Will be changed.

> 
>> +
>> +       ret = reset_control_deassert(tdm->resets);
>> +       if (ret) {
>> +               dev_err(tdm->dev, "Failed to deassert tdm resets\n");
>> +               goto dis_tdm_clk;
>> +       }
>> +
>> +       /* select tdm_ext clock as the clock source for tdm */
>> +       ret = clk_set_parent(tdm->clks[5], tdm->clks[4]);
>> +       if (ret) {
>> +               dev_err(tdm->dev, "Can't set clock source for clk_tdm: %d\n", ret);
>> +               goto dis_tdm_clk;
>> +       }
>> +       return 0;
>> +
>> +dis_tdm_clk:
>> +       for (i = tdm->num_clks - 1; i >= 0; i--)
>> +               clk_disable_unprepare(tdm->clks[i]);
> 
> clk_bulk_disable_unprepare()

Will be changed.

> 
>> +
>> +       return ret;
>> +}
>> +
>> +#ifdef CONFIG_PM
>> +static int jh7110_tdm_runtime_suspend(struct device *dev)
>> +{
>> +       struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
>> +
>> +       jh7110_tdm_clk_disable(tdm);
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_runtime_resume(struct device *dev)
>> +{
>> +       struct jh7110_tdm_dev *tdm = dev_get_drvdata(dev);
>> +
>> +       return jh7110_tdm_clk_enable(tdm);
>> +}
>> +#endif
>> +
>> +#ifdef CONFIG_PM_SLEEP
>> +static int jh7110_tdm_suspend(struct snd_soc_component *component)
>> +{
>> +       /* save context */
>> +       tdm->saved_pcmgbcr = jh7110_tdm_readl(tdm, TDM_PCMGBCR);
>> +       tdm->saved_pcmdiv = jh7110_tdm_readl(tdm, TDM_PCMDIV);
>> +
>> +       return pm_runtime_force_suspend(component->dev);
>> +}
>> +
>> +static int jh7110_tdm_resume(struct snd_soc_component *component)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_component_get_drvdata(component);
>> +
>> +       /* restore context */
>> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, tdm->saved_pcmgbcr);
>> +       jh7110_tdm_writel(tdm, TDM_PCMDIV, tdm->saved_pcmdiv);
>> +
>> +       return pm_runtime_force_resume(component->dev);
>> +}
>> +
>> +#else
>> +#define jh7110_tdm_suspend     NULL
>> +#define jh7110_tdm_resume      NULL
>> +#endif
> 
> you may use pm_sleep_ptr() to avoid these.

OK, thanks for your prompt.

> 
>> +
>> +static const struct snd_soc_component_driver jh7110_tdm_component = {
>> +       .name = "jh7110-tdm",
>> +       .suspend = jh7110_tdm_suspend,
>> +       .resume = jh7110_tdm_resume,
>> +};
>> +
>> +static int jh7110_tdm_startup(struct snd_pcm_substream *substream,
>> +                             struct snd_soc_dai *cpu_dai)
>> +{
>> +       struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
>> +       struct snd_soc_dai_link *dai_link = rtd->dai_link;
>> +
>> +       dai_link->stop_dma_first = 1;
>> +
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_hw_params(struct snd_pcm_substream *substream,
>> +                               struct snd_pcm_hw_params *params,
>> +                               struct snd_soc_dai *dai)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
>> +       int chan_wl, chan_sl, chan_nr;
>> +       unsigned int data_width;
>> +       unsigned int dma_bus_width;
>> +       struct snd_dmaengine_dai_dma_data *dma_data = NULL;
>> +       int ret = 0;
>> +
>> +       data_width = params_width(params);
>> +
>> +       tdm->samplerate = params_rate(params);
>> +       tdm->pcmclk = params_channels(params) * tdm->samplerate * data_width;
>> +
>> +       switch (params_format(params)) {
>> +       case SNDRV_PCM_FORMAT_S16_LE:
>> +               chan_wl = TDM_16BIT_WORD_LEN;
>> +               chan_sl = TDM_16BIT_SLOT_LEN;
>> +               dma_bus_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
>> +               break;
>> +
>> +       case SNDRV_PCM_FORMAT_S32_LE:
>> +               chan_wl = TDM_32BIT_WORD_LEN;
>> +               chan_sl = TDM_32BIT_SLOT_LEN;
>> +               dma_bus_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
>> +               break;
>> +
>> +       default:
>> +               dev_err(tdm->dev, "tdm: unsupported PCM fmt");
>> +               return -EINVAL;
>> +       }
>> +
>> +       chan_nr = params_channels(params);
>> +       switch (chan_nr) {
>> +       case 1:
>> +       case 2:
>> +       case 4:
>> +       case 6:
>> +       case 8:
>> +               break;
>> +       default:
>> +               dev_err(tdm->dev, "channel not supported\n");
>> +               return -EINVAL;
>> +       }
>> +
>> +       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
>> +               tdm->tx.wl = chan_wl;
>> +               tdm->tx.sl = chan_sl;
>> +               tdm->tx.sscale = chan_nr;
>> +               tdm->play_dma_data.addr_width = dma_bus_width;
>> +               dma_data = &tdm->play_dma_data;
>> +       } else {
>> +               tdm->rx.wl = chan_wl;
>> +               tdm->rx.sl = chan_sl;
>> +               tdm->rx.sscale = chan_nr;
>> +               tdm->capture_dma_data.addr_width = dma_bus_width;
>> +               dma_data = &tdm->capture_dma_data;
>> +       }
>> +
>> +       snd_soc_dai_set_dma_data(dai, substream, dma_data);
>> +
>> +       ret = jh7110_tdm_config(tdm, substream);
>> +       if (ret)
>> +               return ret;
>> +
>> +       jh7110_tdm_save_context(tdm, substream);
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_trigger(struct snd_pcm_substream *substream,
>> +                             int cmd, struct snd_soc_dai *dai)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
>> +       int ret = 0;
>> +
>> +       switch (cmd) {
>> +       case SNDRV_PCM_TRIGGER_START:
>> +       case SNDRV_PCM_TRIGGER_RESUME:
>> +       case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
>> +               jh7110_tdm_start(tdm, substream);
>> +               break;
>> +
>> +       case SNDRV_PCM_TRIGGER_STOP:
>> +       case SNDRV_PCM_TRIGGER_SUSPEND:
>> +       case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
>> +               jh7110_tdm_stop(tdm, substream);
>> +               break;
>> +       default:
>> +               ret = -EINVAL;
>> +               break;
>> +       }
>> +       return ret;
>> +}
>> +
>> +static int jh7110_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
>> +                                 unsigned int fmt)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(cpu_dai);
>> +       unsigned int gbcr;
>> +       int ret = 0;
>> +
>> +       /* set master/slave audio interface */
>> +       switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
>> +       case SND_SOC_DAIFMT_BP_FP:
>> +               /* cpu is master */
>> +               tdm->ms_mode = TDM_AS_MASTER;
>> +               break;
>> +       case SND_SOC_DAIFMT_BC_FC:
>> +               /* codec is master */
>> +               tdm->ms_mode = TDM_AS_SLAVE;
>> +               break;
>> +       case SND_SOC_DAIFMT_BC_FP:
>> +       case SND_SOC_DAIFMT_BP_FC:
>> +               ret = -EINVAL;
>> +               break;
>> +       default:
>> +               dev_dbg(tdm->dev, "dwc : Invalid clock provider format\n");
>> +               ret = -EINVAL;
>> +               break;
>> +       }
>> +
>> +       gbcr = (tdm->clkpolity << CLKPOL_BIT) |
>> +               (tdm->elm << ELM_BIT) |
>> +               (tdm->syncm << SYNCM_BIT) |
>> +               (tdm->ms_mode << MS_BIT);
>> +       jh7110_tdm_writel(tdm, TDM_PCMGBCR, gbcr);
>> +
>> +       return ret;
>> +}
>> +
>> +static const struct snd_soc_dai_ops jh7110_tdm_dai_ops = {
>> +       .startup        = jh7110_tdm_startup,
>> +       .hw_params      = jh7110_tdm_hw_params,
>> +       .trigger        = jh7110_tdm_trigger,
>> +       .set_fmt        = jh7110_tdm_set_dai_fmt,
>> +};
>> +
>> +static int jh7110_tdm_dai_probe(struct snd_soc_dai *dai)
>> +{
>> +       struct jh7110_tdm_dev *tdm = snd_soc_dai_get_drvdata(dai);
>> +
>> +       snd_soc_dai_init_dma_data(dai, &tdm->play_dma_data, &tdm->capture_dma_data);
>> +       snd_soc_dai_set_drvdata(dai, tdm);
>> +       return 0;
>> +}
>> +
>> +#define JH7110_TDM_RATES       SNDRV_PCM_RATE_8000_48000
>> +
>> +#define JH7110_TDM_FORMATS     (SNDRV_PCM_FMTBIT_S16_LE | \
>> +                                SNDRV_PCM_FMTBIT_S32_LE)
>> +
>> +static struct snd_soc_dai_driver jh7110_tdm_dai = {
>> +       .name = "sf_tdm",
>> +       .id = 0,
>> +       .playback = {
>> +               .stream_name    = "Playback",
>> +               .channels_min   = 1,
>> +               .channels_max   = 8,
>> +               .rates          = JH7110_TDM_RATES,
>> +               .formats        = JH7110_TDM_FORMATS,
>> +       },
>> +       .capture = {
>> +               .stream_name    = "Capture",
>> +               .channels_min   = 1,
>> +               .channels_max   = 8,
>> +               .rates          = JH7110_TDM_RATES,
>> +               .formats        = JH7110_TDM_FORMATS,
>> +       },
>> +       .ops = &jh7110_tdm_dai_ops,
>> +       .probe = jh7110_tdm_dai_probe,
>> +       .symmetric_rate = 1,
>> +};
>> +
>> +static const struct snd_pcm_hardware jh7110_pcm_hardware = {
>> +       .info                   = (SNDRV_PCM_INFO_MMAP          |
>> +                                  SNDRV_PCM_INFO_MMAP_VALID    |
>> +                                  SNDRV_PCM_INFO_PAUSE         |
>> +                                  SNDRV_PCM_INFO_RESUME        |
>> +                                  SNDRV_PCM_INFO_INTERLEAVED   |
>> +                                  SNDRV_PCM_INFO_BLOCK_TRANSFER),
>> +       .buffer_bytes_max       = 192512,
>> +       .period_bytes_min       = 4096,
>> +       .period_bytes_max       = 32768,
>> +       .periods_min            = 1,
>> +       .periods_max            = 48,
>> +       .fifo_size              = 16,
>> +};
>> +
>> +static const struct snd_dmaengine_pcm_config jh7110_dmaengine_pcm_config = {
>> +       .pcm_hardware = &jh7110_pcm_hardware,
>> +       .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
>> +       .prealloc_buffer_size = 192512,
>> +};
>> +
>> +static void jh7110_tdm_init_params(struct jh7110_tdm_dev *tdm)
>> +{
>> +       tdm->clkpolity = TDM_TX_RASING_RX_FALLING;
>> +       tdm->elm = TDM_ELM_LATE;
>> +       tdm->syncm = TDM_SYNCM_SHORT;
>> +
>> +       tdm->rx.ifl = TDM_FIFO_HALF;
>> +       tdm->tx.ifl = TDM_FIFO_HALF;
>> +       tdm->rx.wl = TDM_16BIT_WORD_LEN;
>> +       tdm->tx.wl = TDM_16BIT_WORD_LEN;
>> +       tdm->rx.sscale = 2;
>> +       tdm->tx.sscale = 2;
>> +       tdm->rx.lrj = TDM_LEFT_JUSTIFT;
>> +       tdm->tx.lrj = TDM_LEFT_JUSTIFT;
>> +
>> +       tdm->play_dma_data.addr = JH7110_TDM_FIFO;
>> +       tdm->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
>> +       tdm->play_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
>> +       tdm->play_dma_data.maxburst = 16;
>> +
>> +       tdm->capture_dma_data.addr = JH7110_TDM_FIFO;
>> +       tdm->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
>> +       tdm->capture_dma_data.fifo_size = JH7110_TDM_FIFO_DEPTH / 2;
>> +       tdm->capture_dma_data.maxburst = 8;
>> +}
>> +
>> +static int jh7110_tdm_clk_reset_init(struct platform_device *pdev,
>> +                                    struct jh7110_tdm_dev *tdm)
>> +{
>> +       int i, ret;
>> +
>> +       for (i = 0; i < tdm->num_clks; i++) {
>> +               tdm->clks[i] = devm_clk_get(&pdev->dev, tdm->clk_names[i]);
>> +               if (IS_ERR(tdm->clks[i])) {
>> +                       dev_err(&pdev->dev, "Failed to get clock: %s\n",
>> +                               tdm->clk_names[i]);
>> +                       return PTR_ERR(tdm->clks[i]);
>> +               }
>> +       }
> 
> devm_clk_bulk_get() ?

get/enable/disable clocks with 'devm_clk_bulk_' interface

> 
>> +
>> +       tdm->resets = devm_reset_control_array_get_exclusive(&pdev->dev);
>> +       if (IS_ERR(tdm->resets)) {
>> +               ret = PTR_ERR(tdm->resets);
>> +               dev_err(&pdev->dev, "Failed to get tdm resets");
>> +               return ret;
>> +       }
>> +
>> +       return jh7110_tdm_clk_enable(tdm);
>> +}
>> +
>> +static int jh7110_tdm_probe(struct platform_device *pdev)
>> +{
>> +       struct jh7110_tdm_dev *tdm;
>> +       const struct starfive_tdm_driverdata *driver_data;
>> +       int ret;
>> +
>> +       tdm = devm_kzalloc(&pdev->dev, sizeof(*tdm), GFP_KERNEL);
>> +       if (!tdm)
>> +               return -ENOMEM;
>> +
>> +       driver_data = of_device_get_match_data(&pdev->dev);
>> +       tdm->clk_names = (const char **)driver_data->clk_names;
>> +       tdm->num_clks = driver_data->num_clks;
>> +
>> +       tdm->tdm_base = devm_platform_ioremap_resource(pdev, 0);
>> +       if (IS_ERR(tdm->tdm_base))
>> +               return PTR_ERR(tdm->tdm_base);
>> +
>> +       tdm->dev = &pdev->dev;
>> +
>> +       ret = jh7110_tdm_clk_reset_init(pdev, tdm);
>> +       if (ret) {
>> +               dev_err(&pdev->dev, "Failed to enable audio-tdm clock\n");
>> +               return ret;
>> +       }
>> +
>> +       jh7110_tdm_init_params(tdm);
>> +
>> +       dev_set_drvdata(&pdev->dev, tdm);
>> +       ret = devm_snd_soc_register_component(&pdev->dev, &jh7110_tdm_component,
>> +                                             &jh7110_tdm_dai, 1);
>> +       if (ret != 0) {
> 
> if (ret)

Will be fixed.

> 
>> +               dev_err(&pdev->dev, "Failed to register dai\n");
>> +               return ret;
>> +       }
>> +
>> +       ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
>> +                                             &jh7110_dmaengine_pcm_config,
>> +                                             SND_DMAENGINE_PCM_FLAG_COMPAT);
>> +       if (ret) {
>> +               dev_err(&pdev->dev, "Could not register pcm: %d\n", ret);
>> +               return ret;
>> +       }
>> +
>> +       pm_runtime_enable(&pdev->dev);
>> +#ifdef CONFIG_PM
>> +       jh7110_tdm_clk_disable(tdm);
>> +#endif
>> +
>> +       return 0;
>> +}
>> +
>> +static int jh7110_tdm_dev_remove(struct platform_device *pdev)
>> +{
>> +       pm_runtime_disable(&pdev->dev);
>> +       return 0;
>> +}
>> +
>> +static const struct starfive_tdm_driverdata jh7110_drvdata = {
>> +       .clk_names = {"mclk_inner", "tdm_ahb", "tdm_apb", "tdm_internal", "tdm_ext", "tdm"},
>> +       .num_clks = 6,
>> +};
>> +
>> +static const struct of_device_id jh7110_tdm_of_match[] = {
>> +       { .compatible = "starfive,jh7110-tdm", .data = &jh7110_drvdata },
>> +       {}
>> +};
>> +
>> +MODULE_DEVICE_TABLE(of, jh7110_tdm_of_match);
>> +
>> +static const struct dev_pm_ops jh7110_tdm_pm_ops = {
>> +       SET_RUNTIME_PM_OPS(jh7110_tdm_runtime_suspend,
>> +                          jh7110_tdm_runtime_resume, NULL)
> 
> You can use RUNTIME_PM_OPS() and avoid having #ifdef CONFIG_PM around
> jh7110_tdm_runtime_suspend and jh7110_tdm_runtime_resume definitions.

I found that the definition of SET_RUNTIME_PM_OPS is based on RUNTIME_PM_OPS,
so CONFIG_PM is not needed.

Thank you very much for your detailed review and comments !

Best regards,
Walker

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
  2023-05-06  9:01   ` Walker Chen
  (?)
@ 2023-05-09 12:52     ` Walker Chen
  -1 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-09 12:52 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

Hi Conor/Emil,

DT overlay is used to describe combinations of VF2 and hat. Do you have any comments on this patch ?
Thanks!

Best regards,
Walker

On 2023/5/6 17:01, Walker Chen wrote:
> Add the tdm controller node and sound card for the StarFive JH7110 SoC.
> 
> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
> ---
>  arch/riscv/boot/dts/starfive/Makefile         |  3 +
>  .../jh7110-starfive-visionfive-2-wm8960.dtso  | 70 +++++++++++++++++++
>  .../jh7110-starfive-visionfive-2.dtsi         | 40 +++++++++++
>  arch/riscv/boot/dts/starfive/jh7110.dtsi      | 21 ++++++
>  4 files changed, 134 insertions(+)
>  create mode 100644 arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
> 
> diff --git a/arch/riscv/boot/dts/starfive/Makefile b/arch/riscv/boot/dts/starfive/Makefile
> index 170956846d49..644cc29b5be3 100644
> --- a/arch/riscv/boot/dts/starfive/Makefile
> +++ b/arch/riscv/boot/dts/starfive/Makefile
> @@ -4,3 +4,6 @@ dtb-$(CONFIG_ARCH_STARFIVE) += jh7100-starfive-visionfive-v1.dtb
>  
>  dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-v1.2a.dtb
>  dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-v1.3b.dtb
> +
> +jh7110-starfive-visionfive-2-wm8960-dtbs := jh7110-starfive-visionfive-2-v1.3b.dtb jh7110-starfive-visionfive-2-wm8960.dtbo
> +dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-wm8960.dtb
> diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
> new file mode 100644
> index 000000000000..67897f000883
> --- /dev/null
> +++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
> @@ -0,0 +1,70 @@
> +// SPDX-License-Identifier: GPL-2.0 OR MIT
> +/*
> + * Copyright (C) 2023 StarFive Technology Co., Ltd.
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + *
> + * Requires ReSpeaker 2-Mics Pi HAT plugged in 40-pin GPIO header.
> + */
> +
> +/dts-v1/;
> +/plugin/;
> +
> +&{/} {
> +	compatible = "starfive,visionfive-2-v1.3b", "starfive,jh7110";
> +
> +	wm8960_mclk: wm8960-mclk {
> +		compatible = "fixed-clock";
> +		clock-output-names = "wm8960_mclk";
> +		#clock-cells = <0>;
> +		clock-frequency = <24576000>;
> +	};
> +
> +	sound {
> +		compatible = "simple-audio-card";
> +
> +		#address-cells = <1>;
> +		#size-cells = <0>;
> +
> +		simple-audio-card,name = "Starfive-TDM-Sound-Card";
> +		simple-audio-card,widgets = "Microphone", "Mic Jack",
> +					    "Line", "Line In",
> +					    "Line", "Line Out",
> +					    "Speaker", "Speaker",
> +					    "Headphone", "Headphone Jack";
> +		simple-audio-card,routing = "Headphone Jack", "HP_L",
> +					    "Headphone Jack", "HP_R",
> +					    "Speaker", "SPK_LP",
> +					    "Speaker", "SPK_LN",
> +					    "LINPUT1", "Mic Jack",
> +					    "LINPUT3", "Mic Jack",
> +					    "RINPUT1", "Mic Jack",
> +					    "RINPUT2", "Mic Jack";
> +
> +		simple-audio-card,dai-link@0 {
> +			reg = <0>;
> +			format = "dsp_a";
> +			bitclock-master = <&dailink_master>;
> +			frame-master = <&dailink_master>;
> +
> +			cpu {
> +				sound-dai = <&tdm>;
> +			};
> +			dailink_master: codec {
> +				sound-dai = <&wm8960>;
> +				clocks = <&wm8960_mclk>;
> +			};
> +		};
> +	};
> +};
> +
> +&i2c0 {
> +	#address-cells = <1>;
> +	#size-cells = <0>;
> +
> +	wm8960: codec@1a {
> +		compatible = "wlf,wm8960";
> +		reg = <0x1a>;
> +		wlf,shared-lrclk;
> +		#sound-dai-cells = <0>;
> +	};
> +};
> diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
> index 1155b97b593d..19b5954ee72d 100644
> --- a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
> +++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
> @@ -214,6 +214,40 @@
>  			slew-rate = <0>;
>  		};
>  	};
> +
> +	tdm0_pins: tdm0-pins {
> +		tdm0-pins-tx {
> +			pinmux = <GPIOMUX(44, GPOUT_SYS_TDM_TXD,
> +					      GPOEN_ENABLE,
> +					      GPI_NONE)>;
> +			bias-pull-up;
> +			drive-strength = <2>;
> +			input-disable;
> +			input-schmitt-disable;
> +			slew-rate = <0>;
> +		};
> +
> +		tdm0-pins-rx {
> +			pinmux = <GPIOMUX(61, GPOUT_HIGH,
> +					      GPOEN_DISABLE,
> +					      GPI_SYS_TDM_RXD)>;
> +			input-enable;
> +		};
> +
> +		tdm0-pins-sync {
> +			pinmux = <GPIOMUX(63, GPOUT_HIGH,
> +					      GPOEN_DISABLE,
> +					      GPI_SYS_TDM_SYNC)>;
> +			input-enable;
> +		};
> +
> +		tdm0-pins-pcmclk {
> +			pinmux = <GPIOMUX(38, GPOUT_HIGH,
> +					      GPOEN_DISABLE,
> +					      GPI_SYS_TDM_CLK)>;
> +			input-enable;
> +		};
> +	};
>  };
>  
>  &uart0 {
> @@ -221,3 +255,9 @@
>  	pinctrl-0 = <&uart0_pins>;
>  	status = "okay";
>  };
> +
> +&tdm {
> +	pinctrl-names = "default";
> +	pinctrl-0 = <&tdm0_pins>;
> +	status = "okay";
> +};
> diff --git a/arch/riscv/boot/dts/starfive/jh7110.dtsi b/arch/riscv/boot/dts/starfive/jh7110.dtsi
> index 866313570a7e..cfda6fb0d91b 100644
> --- a/arch/riscv/boot/dts/starfive/jh7110.dtsi
> +++ b/arch/riscv/boot/dts/starfive/jh7110.dtsi
> @@ -366,6 +366,27 @@
>  			status = "disabled";
>  		};
>  
> +		tdm: tdm@10090000 {
> +			compatible = "starfive,jh7110-tdm";
> +			reg = <0x0 0x10090000 0x0 0x1000>;
> +			clocks = <&syscrg JH7110_SYSCLK_TDM_AHB>,
> +				 <&syscrg JH7110_SYSCLK_TDM_APB>,
> +				 <&syscrg JH7110_SYSCLK_TDM_INTERNAL>,
> +				 <&syscrg JH7110_SYSCLK_TDM_TDM>,
> +				 <&syscrg JH7110_SYSCLK_MCLK_INNER>,
> +				 <&tdm_ext>;
> +			clock-names = "tdm_ahb", "tdm_apb",
> +				      "tdm_internal", "tdm",
> +				      "mclk_inner", "tdm_ext";
> +			resets = <&syscrg JH7110_SYSRST_TDM_AHB>,
> +				 <&syscrg JH7110_SYSRST_TDM_APB>,
> +				 <&syscrg JH7110_SYSRST_TDM_CORE>;
> +			dmas = <&dma 20>, <&dma 21>;
> +			dma-names = "rx","tx";
> +			#sound-dai-cells = <0>;
> +			status = "disabled";
> +		};
> +
>  		stgcrg: clock-controller@10230000 {
>  			compatible = "starfive,jh7110-stgcrg";
>  			reg = <0x0 0x10230000 0x0 0x10000>;

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
@ 2023-05-09 12:52     ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-09 12:52 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

Hi Conor/Emil,

DT overlay is used to describe combinations of VF2 and hat. Do you have any comments on this patch ?
Thanks!

Best regards,
Walker

On 2023/5/6 17:01, Walker Chen wrote:
> Add the tdm controller node and sound card for the StarFive JH7110 SoC.
> 
> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
> ---
>  arch/riscv/boot/dts/starfive/Makefile         |  3 +
>  .../jh7110-starfive-visionfive-2-wm8960.dtso  | 70 +++++++++++++++++++
>  .../jh7110-starfive-visionfive-2.dtsi         | 40 +++++++++++
>  arch/riscv/boot/dts/starfive/jh7110.dtsi      | 21 ++++++
>  4 files changed, 134 insertions(+)
>  create mode 100644 arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
> 
> diff --git a/arch/riscv/boot/dts/starfive/Makefile b/arch/riscv/boot/dts/starfive/Makefile
> index 170956846d49..644cc29b5be3 100644
> --- a/arch/riscv/boot/dts/starfive/Makefile
> +++ b/arch/riscv/boot/dts/starfive/Makefile
> @@ -4,3 +4,6 @@ dtb-$(CONFIG_ARCH_STARFIVE) += jh7100-starfive-visionfive-v1.dtb
>  
>  dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-v1.2a.dtb
>  dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-v1.3b.dtb
> +
> +jh7110-starfive-visionfive-2-wm8960-dtbs := jh7110-starfive-visionfive-2-v1.3b.dtb jh7110-starfive-visionfive-2-wm8960.dtbo
> +dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-wm8960.dtb
> diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
> new file mode 100644
> index 000000000000..67897f000883
> --- /dev/null
> +++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
> @@ -0,0 +1,70 @@
> +// SPDX-License-Identifier: GPL-2.0 OR MIT
> +/*
> + * Copyright (C) 2023 StarFive Technology Co., Ltd.
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + *
> + * Requires ReSpeaker 2-Mics Pi HAT plugged in 40-pin GPIO header.
> + */
> +
> +/dts-v1/;
> +/plugin/;
> +
> +&{/} {
> +	compatible = "starfive,visionfive-2-v1.3b", "starfive,jh7110";
> +
> +	wm8960_mclk: wm8960-mclk {
> +		compatible = "fixed-clock";
> +		clock-output-names = "wm8960_mclk";
> +		#clock-cells = <0>;
> +		clock-frequency = <24576000>;
> +	};
> +
> +	sound {
> +		compatible = "simple-audio-card";
> +
> +		#address-cells = <1>;
> +		#size-cells = <0>;
> +
> +		simple-audio-card,name = "Starfive-TDM-Sound-Card";
> +		simple-audio-card,widgets = "Microphone", "Mic Jack",
> +					    "Line", "Line In",
> +					    "Line", "Line Out",
> +					    "Speaker", "Speaker",
> +					    "Headphone", "Headphone Jack";
> +		simple-audio-card,routing = "Headphone Jack", "HP_L",
> +					    "Headphone Jack", "HP_R",
> +					    "Speaker", "SPK_LP",
> +					    "Speaker", "SPK_LN",
> +					    "LINPUT1", "Mic Jack",
> +					    "LINPUT3", "Mic Jack",
> +					    "RINPUT1", "Mic Jack",
> +					    "RINPUT2", "Mic Jack";
> +
> +		simple-audio-card,dai-link@0 {
> +			reg = <0>;
> +			format = "dsp_a";
> +			bitclock-master = <&dailink_master>;
> +			frame-master = <&dailink_master>;
> +
> +			cpu {
> +				sound-dai = <&tdm>;
> +			};
> +			dailink_master: codec {
> +				sound-dai = <&wm8960>;
> +				clocks = <&wm8960_mclk>;
> +			};
> +		};
> +	};
> +};
> +
> +&i2c0 {
> +	#address-cells = <1>;
> +	#size-cells = <0>;
> +
> +	wm8960: codec@1a {
> +		compatible = "wlf,wm8960";
> +		reg = <0x1a>;
> +		wlf,shared-lrclk;
> +		#sound-dai-cells = <0>;
> +	};
> +};
> diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
> index 1155b97b593d..19b5954ee72d 100644
> --- a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
> +++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
> @@ -214,6 +214,40 @@
>  			slew-rate = <0>;
>  		};
>  	};
> +
> +	tdm0_pins: tdm0-pins {
> +		tdm0-pins-tx {
> +			pinmux = <GPIOMUX(44, GPOUT_SYS_TDM_TXD,
> +					      GPOEN_ENABLE,
> +					      GPI_NONE)>;
> +			bias-pull-up;
> +			drive-strength = <2>;
> +			input-disable;
> +			input-schmitt-disable;
> +			slew-rate = <0>;
> +		};
> +
> +		tdm0-pins-rx {
> +			pinmux = <GPIOMUX(61, GPOUT_HIGH,
> +					      GPOEN_DISABLE,
> +					      GPI_SYS_TDM_RXD)>;
> +			input-enable;
> +		};
> +
> +		tdm0-pins-sync {
> +			pinmux = <GPIOMUX(63, GPOUT_HIGH,
> +					      GPOEN_DISABLE,
> +					      GPI_SYS_TDM_SYNC)>;
> +			input-enable;
> +		};
> +
> +		tdm0-pins-pcmclk {
> +			pinmux = <GPIOMUX(38, GPOUT_HIGH,
> +					      GPOEN_DISABLE,
> +					      GPI_SYS_TDM_CLK)>;
> +			input-enable;
> +		};
> +	};
>  };
>  
>  &uart0 {
> @@ -221,3 +255,9 @@
>  	pinctrl-0 = <&uart0_pins>;
>  	status = "okay";
>  };
> +
> +&tdm {
> +	pinctrl-names = "default";
> +	pinctrl-0 = <&tdm0_pins>;
> +	status = "okay";
> +};
> diff --git a/arch/riscv/boot/dts/starfive/jh7110.dtsi b/arch/riscv/boot/dts/starfive/jh7110.dtsi
> index 866313570a7e..cfda6fb0d91b 100644
> --- a/arch/riscv/boot/dts/starfive/jh7110.dtsi
> +++ b/arch/riscv/boot/dts/starfive/jh7110.dtsi
> @@ -366,6 +366,27 @@
>  			status = "disabled";
>  		};
>  
> +		tdm: tdm@10090000 {
> +			compatible = "starfive,jh7110-tdm";
> +			reg = <0x0 0x10090000 0x0 0x1000>;
> +			clocks = <&syscrg JH7110_SYSCLK_TDM_AHB>,
> +				 <&syscrg JH7110_SYSCLK_TDM_APB>,
> +				 <&syscrg JH7110_SYSCLK_TDM_INTERNAL>,
> +				 <&syscrg JH7110_SYSCLK_TDM_TDM>,
> +				 <&syscrg JH7110_SYSCLK_MCLK_INNER>,
> +				 <&tdm_ext>;
> +			clock-names = "tdm_ahb", "tdm_apb",
> +				      "tdm_internal", "tdm",
> +				      "mclk_inner", "tdm_ext";
> +			resets = <&syscrg JH7110_SYSRST_TDM_AHB>,
> +				 <&syscrg JH7110_SYSRST_TDM_APB>,
> +				 <&syscrg JH7110_SYSRST_TDM_CORE>;
> +			dmas = <&dma 20>, <&dma 21>;
> +			dma-names = "rx","tx";
> +			#sound-dai-cells = <0>;
> +			status = "disabled";
> +		};
> +
>  		stgcrg: clock-controller@10230000 {
>  			compatible = "starfive,jh7110-stgcrg";
>  			reg = <0x0 0x10230000 0x0 0x10000>;

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
@ 2023-05-09 12:52     ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-09 12:52 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing
  Cc: alsa-devel, devicetree, linux-kernel, linux-riscv

Hi Conor/Emil,

DT overlay is used to describe combinations of VF2 and hat. Do you have any comments on this patch ?
Thanks!

Best regards,
Walker

On 2023/5/6 17:01, Walker Chen wrote:
> Add the tdm controller node and sound card for the StarFive JH7110 SoC.
> 
> Signed-off-by: Walker Chen <walker.chen@starfivetech.com>
> ---
>  arch/riscv/boot/dts/starfive/Makefile         |  3 +
>  .../jh7110-starfive-visionfive-2-wm8960.dtso  | 70 +++++++++++++++++++
>  .../jh7110-starfive-visionfive-2.dtsi         | 40 +++++++++++
>  arch/riscv/boot/dts/starfive/jh7110.dtsi      | 21 ++++++
>  4 files changed, 134 insertions(+)
>  create mode 100644 arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
> 
> diff --git a/arch/riscv/boot/dts/starfive/Makefile b/arch/riscv/boot/dts/starfive/Makefile
> index 170956846d49..644cc29b5be3 100644
> --- a/arch/riscv/boot/dts/starfive/Makefile
> +++ b/arch/riscv/boot/dts/starfive/Makefile
> @@ -4,3 +4,6 @@ dtb-$(CONFIG_ARCH_STARFIVE) += jh7100-starfive-visionfive-v1.dtb
>  
>  dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-v1.2a.dtb
>  dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-v1.3b.dtb
> +
> +jh7110-starfive-visionfive-2-wm8960-dtbs := jh7110-starfive-visionfive-2-v1.3b.dtb jh7110-starfive-visionfive-2-wm8960.dtbo
> +dtb-$(CONFIG_ARCH_STARFIVE) += jh7110-starfive-visionfive-2-wm8960.dtb
> diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
> new file mode 100644
> index 000000000000..67897f000883
> --- /dev/null
> +++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-wm8960.dtso
> @@ -0,0 +1,70 @@
> +// SPDX-License-Identifier: GPL-2.0 OR MIT
> +/*
> + * Copyright (C) 2023 StarFive Technology Co., Ltd.
> + * Author: Walker Chen <walker.chen@starfivetech.com>
> + *
> + * Requires ReSpeaker 2-Mics Pi HAT plugged in 40-pin GPIO header.
> + */
> +
> +/dts-v1/;
> +/plugin/;
> +
> +&{/} {
> +	compatible = "starfive,visionfive-2-v1.3b", "starfive,jh7110";
> +
> +	wm8960_mclk: wm8960-mclk {
> +		compatible = "fixed-clock";
> +		clock-output-names = "wm8960_mclk";
> +		#clock-cells = <0>;
> +		clock-frequency = <24576000>;
> +	};
> +
> +	sound {
> +		compatible = "simple-audio-card";
> +
> +		#address-cells = <1>;
> +		#size-cells = <0>;
> +
> +		simple-audio-card,name = "Starfive-TDM-Sound-Card";
> +		simple-audio-card,widgets = "Microphone", "Mic Jack",
> +					    "Line", "Line In",
> +					    "Line", "Line Out",
> +					    "Speaker", "Speaker",
> +					    "Headphone", "Headphone Jack";
> +		simple-audio-card,routing = "Headphone Jack", "HP_L",
> +					    "Headphone Jack", "HP_R",
> +					    "Speaker", "SPK_LP",
> +					    "Speaker", "SPK_LN",
> +					    "LINPUT1", "Mic Jack",
> +					    "LINPUT3", "Mic Jack",
> +					    "RINPUT1", "Mic Jack",
> +					    "RINPUT2", "Mic Jack";
> +
> +		simple-audio-card,dai-link@0 {
> +			reg = <0>;
> +			format = "dsp_a";
> +			bitclock-master = <&dailink_master>;
> +			frame-master = <&dailink_master>;
> +
> +			cpu {
> +				sound-dai = <&tdm>;
> +			};
> +			dailink_master: codec {
> +				sound-dai = <&wm8960>;
> +				clocks = <&wm8960_mclk>;
> +			};
> +		};
> +	};
> +};
> +
> +&i2c0 {
> +	#address-cells = <1>;
> +	#size-cells = <0>;
> +
> +	wm8960: codec@1a {
> +		compatible = "wlf,wm8960";
> +		reg = <0x1a>;
> +		wlf,shared-lrclk;
> +		#sound-dai-cells = <0>;
> +	};
> +};
> diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
> index 1155b97b593d..19b5954ee72d 100644
> --- a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
> +++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi
> @@ -214,6 +214,40 @@
>  			slew-rate = <0>;
>  		};
>  	};
> +
> +	tdm0_pins: tdm0-pins {
> +		tdm0-pins-tx {
> +			pinmux = <GPIOMUX(44, GPOUT_SYS_TDM_TXD,
> +					      GPOEN_ENABLE,
> +					      GPI_NONE)>;
> +			bias-pull-up;
> +			drive-strength = <2>;
> +			input-disable;
> +			input-schmitt-disable;
> +			slew-rate = <0>;
> +		};
> +
> +		tdm0-pins-rx {
> +			pinmux = <GPIOMUX(61, GPOUT_HIGH,
> +					      GPOEN_DISABLE,
> +					      GPI_SYS_TDM_RXD)>;
> +			input-enable;
> +		};
> +
> +		tdm0-pins-sync {
> +			pinmux = <GPIOMUX(63, GPOUT_HIGH,
> +					      GPOEN_DISABLE,
> +					      GPI_SYS_TDM_SYNC)>;
> +			input-enable;
> +		};
> +
> +		tdm0-pins-pcmclk {
> +			pinmux = <GPIOMUX(38, GPOUT_HIGH,
> +					      GPOEN_DISABLE,
> +					      GPI_SYS_TDM_CLK)>;
> +			input-enable;
> +		};
> +	};
>  };
>  
>  &uart0 {
> @@ -221,3 +255,9 @@
>  	pinctrl-0 = <&uart0_pins>;
>  	status = "okay";
>  };
> +
> +&tdm {
> +	pinctrl-names = "default";
> +	pinctrl-0 = <&tdm0_pins>;
> +	status = "okay";
> +};
> diff --git a/arch/riscv/boot/dts/starfive/jh7110.dtsi b/arch/riscv/boot/dts/starfive/jh7110.dtsi
> index 866313570a7e..cfda6fb0d91b 100644
> --- a/arch/riscv/boot/dts/starfive/jh7110.dtsi
> +++ b/arch/riscv/boot/dts/starfive/jh7110.dtsi
> @@ -366,6 +366,27 @@
>  			status = "disabled";
>  		};
>  
> +		tdm: tdm@10090000 {
> +			compatible = "starfive,jh7110-tdm";
> +			reg = <0x0 0x10090000 0x0 0x1000>;
> +			clocks = <&syscrg JH7110_SYSCLK_TDM_AHB>,
> +				 <&syscrg JH7110_SYSCLK_TDM_APB>,
> +				 <&syscrg JH7110_SYSCLK_TDM_INTERNAL>,
> +				 <&syscrg JH7110_SYSCLK_TDM_TDM>,
> +				 <&syscrg JH7110_SYSCLK_MCLK_INNER>,
> +				 <&tdm_ext>;
> +			clock-names = "tdm_ahb", "tdm_apb",
> +				      "tdm_internal", "tdm",
> +				      "mclk_inner", "tdm_ext";
> +			resets = <&syscrg JH7110_SYSRST_TDM_AHB>,
> +				 <&syscrg JH7110_SYSRST_TDM_APB>,
> +				 <&syscrg JH7110_SYSRST_TDM_CORE>;
> +			dmas = <&dma 20>, <&dma 21>;
> +			dma-names = "rx","tx";
> +			#sound-dai-cells = <0>;
> +			status = "disabled";
> +		};
> +
>  		stgcrg: clock-controller@10230000 {
>  			compatible = "starfive,jh7110-stgcrg";
>  			reg = <0x0 0x10230000 0x0 0x10000>;

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
  2023-05-09 12:52     ` Walker Chen
  (?)
@ 2023-05-09 18:05       ` Conor Dooley
  -1 siblings, 0 replies; 48+ messages in thread
From: Conor Dooley @ 2023-05-09 18:05 UTC (permalink / raw)
  To: Walker Chen
  Cc: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, alsa-devel, devicetree, linux-kernel,
	linux-riscv

[-- Attachment #1: Type: text/plain, Size: 570 bytes --]

On Tue, May 09, 2023 at 08:52:48PM +0800, Walker Chen wrote:
> Hi Conor/Emil,
> 
> DT overlay is used to describe combinations of VF2 and hat.
> Do you have any comments on this patch ?

Up to Emil :)

I seem to recall that he said at the linux-riscv sync-up call that we
have* that he was not in favour of overlays for hats like this.
I'll let him confirm that though, I might very well be misinterpreting or
misremembering what he said.

Cheers,
Conor.

* https://lore.kernel.org/linux-riscv/mhng-775d4068-6c1e-48a4-a1dc-b4a76ff26bb3@palmer-ri-x1c9a/

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
@ 2023-05-09 18:05       ` Conor Dooley
  0 siblings, 0 replies; 48+ messages in thread
From: Conor Dooley @ 2023-05-09 18:05 UTC (permalink / raw)
  To: Walker Chen
  Cc: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Emil Renner Berthing, alsa-devel, devicetree, linux-kernel,
	linux-riscv


[-- Attachment #1.1: Type: text/plain, Size: 570 bytes --]

On Tue, May 09, 2023 at 08:52:48PM +0800, Walker Chen wrote:
> Hi Conor/Emil,
> 
> DT overlay is used to describe combinations of VF2 and hat.
> Do you have any comments on this patch ?

Up to Emil :)

I seem to recall that he said at the linux-riscv sync-up call that we
have* that he was not in favour of overlays for hats like this.
I'll let him confirm that though, I might very well be misinterpreting or
misremembering what he said.

Cheers,
Conor.

* https://lore.kernel.org/linux-riscv/mhng-775d4068-6c1e-48a4-a1dc-b4a76ff26bb3@palmer-ri-x1c9a/

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

[-- Attachment #2: Type: text/plain, Size: 161 bytes --]

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
@ 2023-05-09 18:05       ` Conor Dooley
  0 siblings, 0 replies; 48+ messages in thread
From: Conor Dooley @ 2023-05-09 18:05 UTC (permalink / raw)
  To: Walker Chen
  Cc: Mark Brown, Liam Girdwood, Takashi Iwai, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Emil Renner Berthing,
	alsa-devel, devicetree, linux-kernel, linux-riscv

[-- Attachment #1: Type: text/plain, Size: 570 bytes --]

On Tue, May 09, 2023 at 08:52:48PM +0800, Walker Chen wrote:
> Hi Conor/Emil,
> 
> DT overlay is used to describe combinations of VF2 and hat.
> Do you have any comments on this patch ?

Up to Emil :)

I seem to recall that he said at the linux-riscv sync-up call that we
have* that he was not in favour of overlays for hats like this.
I'll let him confirm that though, I might very well be misinterpreting or
misremembering what he said.

Cheers,
Conor.

* https://lore.kernel.org/linux-riscv/mhng-775d4068-6c1e-48a4-a1dc-b4a76ff26bb3@palmer-ri-x1c9a/

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
  2023-05-09 18:05       ` Conor Dooley
@ 2023-05-10  8:33         ` Emil Renner Berthing
  -1 siblings, 0 replies; 48+ messages in thread
From: Emil Renner Berthing @ 2023-05-10  8:33 UTC (permalink / raw)
  To: Conor Dooley
  Cc: Walker Chen, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	alsa-devel, devicetree, linux-kernel, linux-riscv

On Tue, 9 May 2023 at 20:05, Conor Dooley <conor@kernel.org> wrote:
>
> On Tue, May 09, 2023 at 08:52:48PM +0800, Walker Chen wrote:
> > Hi Conor/Emil,
> >
> > DT overlay is used to describe combinations of VF2 and hat.
> > Do you have any comments on this patch ?
>
> Up to Emil :)
>
> I seem to recall that he said at the linux-riscv sync-up call that we
> have* that he was not in favour of overlays for hats like this.
> I'll let him confirm that though, I might very well be misinterpreting or
> misremembering what he said.

What probably meant was that I didn't want a bunch of different device
trees for each combination board * hat. An overlay makes a lot more
sense. However, looking through the kernel tree there is a surprising
lack of overlays for hats committed already, so I suspect there is
some sort of policy around overlays already in place.

> Cheers,
> Conor.
>
> * https://lore.kernel.org/linux-riscv/mhng-775d4068-6c1e-48a4-a1dc-b4a76ff26bb3@palmer-ri-x1c9a/

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
@ 2023-05-10  8:33         ` Emil Renner Berthing
  0 siblings, 0 replies; 48+ messages in thread
From: Emil Renner Berthing @ 2023-05-10  8:33 UTC (permalink / raw)
  To: Conor Dooley
  Cc: Walker Chen, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	alsa-devel, devicetree, linux-kernel, linux-riscv

On Tue, 9 May 2023 at 20:05, Conor Dooley <conor@kernel.org> wrote:
>
> On Tue, May 09, 2023 at 08:52:48PM +0800, Walker Chen wrote:
> > Hi Conor/Emil,
> >
> > DT overlay is used to describe combinations of VF2 and hat.
> > Do you have any comments on this patch ?
>
> Up to Emil :)
>
> I seem to recall that he said at the linux-riscv sync-up call that we
> have* that he was not in favour of overlays for hats like this.
> I'll let him confirm that though, I might very well be misinterpreting or
> misremembering what he said.

What probably meant was that I didn't want a bunch of different device
trees for each combination board * hat. An overlay makes a lot more
sense. However, looking through the kernel tree there is a surprising
lack of overlays for hats committed already, so I suspect there is
some sort of policy around overlays already in place.

> Cheers,
> Conor.
>
> * https://lore.kernel.org/linux-riscv/mhng-775d4068-6c1e-48a4-a1dc-b4a76ff26bb3@palmer-ri-x1c9a/

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
  2023-05-10  8:33         ` Emil Renner Berthing
@ 2023-05-10  9:21           ` Walker Chen
  -1 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-10  9:21 UTC (permalink / raw)
  To: Emil Renner Berthing, Conor Dooley
  Cc: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley, alsa-devel,
	devicetree, linux-kernel, linux-riscv



On 2023/5/10 16:33, Emil Renner Berthing wrote:
> On Tue, 9 May 2023 at 20:05, Conor Dooley <conor@kernel.org> wrote:
>>
>> On Tue, May 09, 2023 at 08:52:48PM +0800, Walker Chen wrote:
>> > Hi Conor/Emil,
>> >
>> > DT overlay is used to describe combinations of VF2 and hat.
>> > Do you have any comments on this patch ?
>>
>> Up to Emil :)
>>
>> I seem to recall that he said at the linux-riscv sync-up call that we
>> have* that he was not in favour of overlays for hats like this.
>> I'll let him confirm that though, I might very well be misinterpreting or
>> misremembering what he said.
> 
> What probably meant was that I didn't want a bunch of different device
> trees for each combination board * hat. An overlay makes a lot more
> sense. However, looking through the kernel tree there is a surprising
> lack of overlays for hats committed already, so I suspect there is
> some sort of policy around overlays already in place.
Hi Emil,

About the specific usage of overlay for this board + hat, referenced to the following example in kernel:
linux/arch/arm64/boot/dts/freescale/imx8mm-venice-gw73xx-0x-imx219.dtso
That board is connected with imx219 sensor via mipi_csi interface. That patch was accepted in 2022.

So do you think this way is feasible ?
Thanks!

Best regards,
Walker
 
> 
>> Cheers,
>> Conor.
>>
>> * https://lore.kernel.org/linux-riscv/mhng-775d4068-6c1e-48a4-a1dc-b4a76ff26bb3@palmer-ri-x1c9a/

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
@ 2023-05-10  9:21           ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-10  9:21 UTC (permalink / raw)
  To: Emil Renner Berthing, Conor Dooley
  Cc: Mark Brown, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	Rob Herring, Krzysztof Kozlowski, Conor Dooley, alsa-devel,
	devicetree, linux-kernel, linux-riscv



On 2023/5/10 16:33, Emil Renner Berthing wrote:
> On Tue, 9 May 2023 at 20:05, Conor Dooley <conor@kernel.org> wrote:
>>
>> On Tue, May 09, 2023 at 08:52:48PM +0800, Walker Chen wrote:
>> > Hi Conor/Emil,
>> >
>> > DT overlay is used to describe combinations of VF2 and hat.
>> > Do you have any comments on this patch ?
>>
>> Up to Emil :)
>>
>> I seem to recall that he said at the linux-riscv sync-up call that we
>> have* that he was not in favour of overlays for hats like this.
>> I'll let him confirm that though, I might very well be misinterpreting or
>> misremembering what he said.
> 
> What probably meant was that I didn't want a bunch of different device
> trees for each combination board * hat. An overlay makes a lot more
> sense. However, looking through the kernel tree there is a surprising
> lack of overlays for hats committed already, so I suspect there is
> some sort of policy around overlays already in place.
Hi Emil,

About the specific usage of overlay for this board + hat, referenced to the following example in kernel:
linux/arch/arm64/boot/dts/freescale/imx8mm-venice-gw73xx-0x-imx219.dtso
That board is connected with imx219 sensor via mipi_csi interface. That patch was accepted in 2022.

So do you think this way is feasible ?
Thanks!

Best regards,
Walker
 
> 
>> Cheers,
>> Conor.
>>
>> * https://lore.kernel.org/linux-riscv/mhng-775d4068-6c1e-48a4-a1dc-b4a76ff26bb3@palmer-ri-x1c9a/

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
  2023-05-10  9:21           ` Walker Chen
@ 2023-05-10 20:22             ` Conor Dooley
  -1 siblings, 0 replies; 48+ messages in thread
From: Conor Dooley @ 2023-05-10 20:22 UTC (permalink / raw)
  To: Walker Chen
  Cc: Emil Renner Berthing, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	alsa-devel, devicetree, linux-kernel, linux-riscv

[-- Attachment #1: Type: text/plain, Size: 2244 bytes --]

On Wed, May 10, 2023 at 05:21:21PM +0800, Walker Chen wrote:
> On 2023/5/10 16:33, Emil Renner Berthing wrote:
> > On Tue, 9 May 2023 at 20:05, Conor Dooley <conor@kernel.org> wrote:
> >>
> >> On Tue, May 09, 2023 at 08:52:48PM +0800, Walker Chen wrote:
> >> > Hi Conor/Emil,
> >> >
> >> > DT overlay is used to describe combinations of VF2 and hat.
> >> > Do you have any comments on this patch ?
> >>
> >> I seem to recall that he said at the linux-riscv sync-up call that we
> >> have* that he was not in favour of overlays for hats like this.
> >> I'll let him confirm that though, I might very well be misinterpreting or
> >> misremembering what he said.
> > 
> > What probably meant was that I didn't want a bunch of different device
> > trees for each combination board * hat. An overlay makes a lot more
> > sense. However, looking through the kernel tree there is a surprising
> > lack of overlays for hats committed already, so I suspect there is
> > some sort of policy around overlays already in place.

> About the specific usage of overlay for this board + hat, referenced to the following example in kernel:
> linux/arch/arm64/boot/dts/freescale/imx8mm-venice-gw73xx-0x-imx219.dtso
> That board is connected with imx219 sensor via mipi_csi interface. That patch was accepted in 2022.

Hmm, so spoke to Emil again today about it. In the interest of being
fair, I did go looking at that particular board & looked through their
documentation for more information on why there are overlays.
They do actually sell the bits required to use the overlays, based on
what I saw in their datasheet for the board & wiki. That said, what is
done for one arm64 platform does not necessarily apply elsewhere ;)

I'm not against allowing in-tree overlays for hats/capes/daughter-boards
that come bundled with a board, but accepting ones for a hat that
someone decided to use theoretically has no limit! The "someone" in this
case might be a StarFive developer, but it could be any random one of
your customers next!
We've got to draw a line somewhere, so my answer to the overlay *in this
case* is a no. Sorry.
When you submit your next version, please drop the overlay from this
patch.
Thanks,
Conor.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
@ 2023-05-10 20:22             ` Conor Dooley
  0 siblings, 0 replies; 48+ messages in thread
From: Conor Dooley @ 2023-05-10 20:22 UTC (permalink / raw)
  To: Walker Chen
  Cc: Emil Renner Berthing, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	alsa-devel, devicetree, linux-kernel, linux-riscv


[-- Attachment #1.1: Type: text/plain, Size: 2244 bytes --]

On Wed, May 10, 2023 at 05:21:21PM +0800, Walker Chen wrote:
> On 2023/5/10 16:33, Emil Renner Berthing wrote:
> > On Tue, 9 May 2023 at 20:05, Conor Dooley <conor@kernel.org> wrote:
> >>
> >> On Tue, May 09, 2023 at 08:52:48PM +0800, Walker Chen wrote:
> >> > Hi Conor/Emil,
> >> >
> >> > DT overlay is used to describe combinations of VF2 and hat.
> >> > Do you have any comments on this patch ?
> >>
> >> I seem to recall that he said at the linux-riscv sync-up call that we
> >> have* that he was not in favour of overlays for hats like this.
> >> I'll let him confirm that though, I might very well be misinterpreting or
> >> misremembering what he said.
> > 
> > What probably meant was that I didn't want a bunch of different device
> > trees for each combination board * hat. An overlay makes a lot more
> > sense. However, looking through the kernel tree there is a surprising
> > lack of overlays for hats committed already, so I suspect there is
> > some sort of policy around overlays already in place.

> About the specific usage of overlay for this board + hat, referenced to the following example in kernel:
> linux/arch/arm64/boot/dts/freescale/imx8mm-venice-gw73xx-0x-imx219.dtso
> That board is connected with imx219 sensor via mipi_csi interface. That patch was accepted in 2022.

Hmm, so spoke to Emil again today about it. In the interest of being
fair, I did go looking at that particular board & looked through their
documentation for more information on why there are overlays.
They do actually sell the bits required to use the overlays, based on
what I saw in their datasheet for the board & wiki. That said, what is
done for one arm64 platform does not necessarily apply elsewhere ;)

I'm not against allowing in-tree overlays for hats/capes/daughter-boards
that come bundled with a board, but accepting ones for a hat that
someone decided to use theoretically has no limit! The "someone" in this
case might be a StarFive developer, but it could be any random one of
your customers next!
We've got to draw a line somewhere, so my answer to the overlay *in this
case* is a no. Sorry.
When you submit your next version, please drop the overlay from this
patch.
Thanks,
Conor.

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

[-- Attachment #2: Type: text/plain, Size: 161 bytes --]

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
  2023-05-10 20:22             ` Conor Dooley
@ 2023-05-11  3:21               ` Walker Chen
  -1 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-11  3:21 UTC (permalink / raw)
  To: Conor Dooley
  Cc: Emil Renner Berthing, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	alsa-devel, devicetree, linux-kernel, linux-riscv


On 2023/5/11 4:22, Conor Dooley wrote:
> On Wed, May 10, 2023 at 05:21:21PM +0800, Walker Chen wrote:
>> On 2023/5/10 16:33, Emil Renner Berthing wrote:
>> > On Tue, 9 May 2023 at 20:05, Conor Dooley <conor@kernel.org> wrote:
>> >>
>> >> On Tue, May 09, 2023 at 08:52:48PM +0800, Walker Chen wrote:
>> >> > Hi Conor/Emil,
>> >> >
>> >> > DT overlay is used to describe combinations of VF2 and hat.
>> >> > Do you have any comments on this patch ?
>> >>
>> >> I seem to recall that he said at the linux-riscv sync-up call that we
>> >> have* that he was not in favour of overlays for hats like this.
>> >> I'll let him confirm that though, I might very well be misinterpreting or
>> >> misremembering what he said.
>> > 
>> > What probably meant was that I didn't want a bunch of different device
>> > trees for each combination board * hat. An overlay makes a lot more
>> > sense. However, looking through the kernel tree there is a surprising
>> > lack of overlays for hats committed already, so I suspect there is
>> > some sort of policy around overlays already in place.
> 
>> About the specific usage of overlay for this board + hat, referenced to the following example in kernel:
>> linux/arch/arm64/boot/dts/freescale/imx8mm-venice-gw73xx-0x-imx219.dtso
>> That board is connected with imx219 sensor via mipi_csi interface. That patch was accepted in 2022.
> 
> Hmm, so spoke to Emil again today about it. In the interest of being
> fair, I did go looking at that particular board & looked through their
> documentation for more information on why there are overlays.
> They do actually sell the bits required to use the overlays, based on
> what I saw in their datasheet for the board & wiki. That said, what is
> done for one arm64 platform does not necessarily apply elsewhere ;)
> 
> I'm not against allowing in-tree overlays for hats/capes/daughter-boards
> that come bundled with a board, but accepting ones for a hat that
> someone decided to use theoretically has no limit! The "someone" in this
> case might be a StarFive developer, but it could be any random one of
> your customers next!
> We've got to draw a line somewhere, so my answer to the overlay *in this
> case* is a no. Sorry.
> When you submit your next version, please drop the overlay from this
> patch.

I'm trying to understand what you mean. so the conclusion is that I need to drop the file
 'jh7110-starfive-visionfive-2-wm8960.dtso' from this patch.
When I submit the next version, just keep the TDM node and the pins configuration for TDM in patch [3/3],
need to drop the wm8960 stuff.
Right ?

Best regards,
Walker

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
@ 2023-05-11  3:21               ` Walker Chen
  0 siblings, 0 replies; 48+ messages in thread
From: Walker Chen @ 2023-05-11  3:21 UTC (permalink / raw)
  To: Conor Dooley
  Cc: Emil Renner Berthing, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	alsa-devel, devicetree, linux-kernel, linux-riscv


On 2023/5/11 4:22, Conor Dooley wrote:
> On Wed, May 10, 2023 at 05:21:21PM +0800, Walker Chen wrote:
>> On 2023/5/10 16:33, Emil Renner Berthing wrote:
>> > On Tue, 9 May 2023 at 20:05, Conor Dooley <conor@kernel.org> wrote:
>> >>
>> >> On Tue, May 09, 2023 at 08:52:48PM +0800, Walker Chen wrote:
>> >> > Hi Conor/Emil,
>> >> >
>> >> > DT overlay is used to describe combinations of VF2 and hat.
>> >> > Do you have any comments on this patch ?
>> >>
>> >> I seem to recall that he said at the linux-riscv sync-up call that we
>> >> have* that he was not in favour of overlays for hats like this.
>> >> I'll let him confirm that though, I might very well be misinterpreting or
>> >> misremembering what he said.
>> > 
>> > What probably meant was that I didn't want a bunch of different device
>> > trees for each combination board * hat. An overlay makes a lot more
>> > sense. However, looking through the kernel tree there is a surprising
>> > lack of overlays for hats committed already, so I suspect there is
>> > some sort of policy around overlays already in place.
> 
>> About the specific usage of overlay for this board + hat, referenced to the following example in kernel:
>> linux/arch/arm64/boot/dts/freescale/imx8mm-venice-gw73xx-0x-imx219.dtso
>> That board is connected with imx219 sensor via mipi_csi interface. That patch was accepted in 2022.
> 
> Hmm, so spoke to Emil again today about it. In the interest of being
> fair, I did go looking at that particular board & looked through their
> documentation for more information on why there are overlays.
> They do actually sell the bits required to use the overlays, based on
> what I saw in their datasheet for the board & wiki. That said, what is
> done for one arm64 platform does not necessarily apply elsewhere ;)
> 
> I'm not against allowing in-tree overlays for hats/capes/daughter-boards
> that come bundled with a board, but accepting ones for a hat that
> someone decided to use theoretically has no limit! The "someone" in this
> case might be a StarFive developer, but it could be any random one of
> your customers next!
> We've got to draw a line somewhere, so my answer to the overlay *in this
> case* is a no. Sorry.
> When you submit your next version, please drop the overlay from this
> patch.

I'm trying to understand what you mean. so the conclusion is that I need to drop the file
 'jh7110-starfive-visionfive-2-wm8960.dtso' from this patch.
When I submit the next version, just keep the TDM node and the pins configuration for TDM in patch [3/3],
need to drop the wm8960 stuff.
Right ?

Best regards,
Walker

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
  2023-05-11  3:21               ` Walker Chen
@ 2023-05-11  5:10                 ` Conor Dooley
  -1 siblings, 0 replies; 48+ messages in thread
From: Conor Dooley @ 2023-05-11  5:10 UTC (permalink / raw)
  To: Walker Chen
  Cc: Emil Renner Berthing, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	alsa-devel, devicetree, linux-kernel, linux-riscv




>I'm trying to understand what you mean. so the conclusion is that I need to drop the file
> 'jh7110-starfive-visionfive-2-wm8960.dtso' from this patch.
>When I submit the next version, just keep the TDM node and the pins configuration for TDM in patch [3/3],
>need to drop the wm8960 stuff.
>Right ?

Yes.

>
>Best regards,
>Walker

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

* Re: [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card
@ 2023-05-11  5:10                 ` Conor Dooley
  0 siblings, 0 replies; 48+ messages in thread
From: Conor Dooley @ 2023-05-11  5:10 UTC (permalink / raw)
  To: Walker Chen
  Cc: Emil Renner Berthing, Mark Brown, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	alsa-devel, devicetree, linux-kernel, linux-riscv




>I'm trying to understand what you mean. so the conclusion is that I need to drop the file
> 'jh7110-starfive-visionfive-2-wm8960.dtso' from this patch.
>When I submit the next version, just keep the TDM node and the pins configuration for TDM in patch [3/3],
>need to drop the wm8960 stuff.
>Right ?

Yes.

>
>Best regards,
>Walker

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

end of thread, other threads:[~2023-05-11  5:10 UTC | newest]

Thread overview: 48+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-06  9:01 [PATCH v3 0/3] Add TDM audio on StarFive JH7110 Walker Chen
2023-05-06  9:01 ` Walker Chen
2023-05-06  9:01 ` Walker Chen
2023-05-06  9:01 ` [PATCH v3 1/3] dt-bindings: sound: Add TDM for " Walker Chen
2023-05-06  9:01   ` Walker Chen
2023-05-06  9:01   ` Walker Chen
2023-05-08  1:30   ` Mark Brown
2023-05-08  1:30     ` Mark Brown
2023-05-08  1:30     ` Mark Brown
2023-05-08  2:44     ` Walker Chen
2023-05-08  2:44       ` Walker Chen
2023-05-08  2:44       ` Walker Chen
2023-05-06  9:01 ` [PATCH v3 2/3] ASoC: starfive: Add JH7110 TDM driver Walker Chen
2023-05-06  9:01   ` Walker Chen
2023-05-06  9:01   ` Walker Chen
2023-05-06 13:47   ` Shengyu Qu
2023-05-06 13:47     ` Shengyu Qu
2023-05-08  0:09     ` Mark Brown
2023-05-08  0:09       ` Mark Brown
2023-05-08  0:09       ` Mark Brown
2023-05-08  1:41     ` Walker Chen
2023-05-08  1:41       ` Walker Chen
2023-05-08  1:41       ` Walker Chen
2023-05-09  4:33   ` Claudiu.Beznea--- via Alsa-devel
2023-05-09  4:33   ` Claudiu.Beznea
2023-05-09  4:33     ` Claudiu.Beznea
2023-05-09  7:07     ` Walker Chen
2023-05-09  7:07       ` Walker Chen
2023-05-09  7:07       ` Walker Chen
2023-05-06  9:01 ` [PATCH v3 3/3] riscv: dts: starfive: add tdm node and sound card Walker Chen
2023-05-06  9:01   ` Walker Chen
2023-05-06  9:01   ` Walker Chen
2023-05-09 12:52   ` Walker Chen
2023-05-09 12:52     ` Walker Chen
2023-05-09 12:52     ` Walker Chen
2023-05-09 18:05     ` Conor Dooley
2023-05-09 18:05       ` Conor Dooley
2023-05-09 18:05       ` Conor Dooley
2023-05-10  8:33       ` Emil Renner Berthing
2023-05-10  8:33         ` Emil Renner Berthing
2023-05-10  9:21         ` Walker Chen
2023-05-10  9:21           ` Walker Chen
2023-05-10 20:22           ` Conor Dooley
2023-05-10 20:22             ` Conor Dooley
2023-05-11  3:21             ` Walker Chen
2023-05-11  3:21               ` Walker Chen
2023-05-11  5:10               ` Conor Dooley
2023-05-11  5:10                 ` Conor Dooley

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.