From nobody Thu Nov 28 00:49:20 2024 Received: from mail-ed1-f67.google.com (mail-ed1-f67.google.com [209.85.208.67]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C82A1D45E5 for ; Mon, 7 Oct 2024 12:39:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.67 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728304801; cv=none; b=OxoMIP8paZu+5AX+I0Bjvyf5cCe2ydPNMl/JHyQnTrhAWbaD6Ks6ELmT5PjIeTn+SIO8aV2uUuU9hkMGP92x682UqW8+ODlr2VjjLQA4DtNMGpILIsOUklNcVOiYcGjibd91Q0TRnBIcKVa1ZaTeC2D8aII2CsBTxl5QaRw2ki0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728304801; c=relaxed/simple; bh=EU7VxUFgTc1NKrucGmiHa4GEg065pGLHoPYlSdRdS/o=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U+L12QnE/iiBOKW0dl3rnNGVL5Aj4sHqKjerjBoofdBRuf1HFLpfH4Dl6GcaK+4wFsbRaW9Y7PlicWbhTFbMlGcP4+A7Xav5TrzfX4ryHNTlHTHdeDAdRwwTyDeCKrmR+sqZezJ0Xjzn5FIEC5RCtX46uZ34Txn4xtTb4sK2IkE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=XeJP404j; arc=none smtp.client-ip=209.85.208.67 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="XeJP404j" Received: by mail-ed1-f67.google.com with SMTP id 4fb4d7f45d1cf-5c42e7adbddso5966607a12.2 for ; Mon, 07 Oct 2024 05:39:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1728304795; x=1728909595; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=LP+jcJyNTYlTW59stbD95i+ZI2uUnNB70V+379xHCiU=; b=XeJP404j7F5/JHvEadQ31XrF+4pJpWGe1+idUsbg80QjB/aJrQjsvLt7XxLByE3sjw 7bI0iRipyb3sauhC/yJJ148ckIjztww6IwL3QElOgqfqYTpzniic/o0XgBItGjbLmc3v fh221U1BJXlNhKl6J+rfxxy/pVOoKj6Idv7JkYOwmlZLt8KdBYF3T6LEXcDh2oMGybOh LptSO1auggZD2fXGEibutsINksiD0ANLuhJdqRDZi6qKEJVfeyXNWsuZijgFKa6ptWP8 OWRg2gX2UtQvX6OCqWhUYrkpy4WHFK26YzuqbHVd8Lpg2lN97n6dJgAv2WMWnCNzSM23 1BzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728304795; x=1728909595; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LP+jcJyNTYlTW59stbD95i+ZI2uUnNB70V+379xHCiU=; b=pydqjiwEZKXtszDV2wFjfv9wf3lElFIb6wgvZdxfE+70dR4ZndDYaBMZ2ozbxYtpfY v3t7zivM8KYmnbVeByMKEtPfos6ird54L5KuvrOwznHtVvSZuBlzGj2Pn4CyLzKWIhG1 o9rjQR9FeuhDW6TXoT5XMH9zUHr5GG0pMNQB+9hQHpBgPaJyILudgpRdwv+c9M4tWewI 4xv6QJJ5LNUb1PoHMIWJVcTXEQPq2CaWcuIurVLHRYpSm9oS74ic33qiwijOkmDTORMy bgu0tNrYXh1uDxlfVjhoxwEm5OdBcb4fR2Rk/KY/SlCZInpiQVYvA0z2bcz0Poe+qITd 6DBA== X-Forwarded-Encrypted: i=1; AJvYcCWDcad0CuCBNmfDcP0RZ59NX+P8hEfBYk2Jf+0FC4Jmyd9C0WhENSqwgYxKnjc/4MDo+kJKcoo7Fhm742w=@vger.kernel.org X-Gm-Message-State: AOJu0YxfZjm6Otdilq11y0hgGdj5w1TuIV9rewWcujNUrjVGyaZuCQ28 lLh5scpkC5vJuoUKEyjP2N8imTmbqzrQIVrq6kdEhIVPOdXKOK5njXs6mMegHqs= X-Google-Smtp-Source: AGHT+IEFJomjeB234ZfocSW28Z4TtFBQS95U+4Veu+L1rVWtkeJIRIug2ptaCKE3IMFL7W2pXsc8BQ== X-Received: by 2002:a05:6402:448b:b0:5be:caf6:9dc7 with SMTP id 4fb4d7f45d1cf-5c8d2ebcc2dmr10999603a12.25.1728304794743; Mon, 07 Oct 2024 05:39:54 -0700 (PDT) Received: from localhost (host-87-21-212-62.retail.telecomitalia.it. [87.21.212.62]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c8e05bc56asm3132474a12.45.2024.10.07.05.39.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 05:39:54 -0700 (PDT) From: Andrea della Porta To: Andrea della Porta , Michael Turquette , Stephen Boyd , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Florian Fainelli , Broadcom internal kernel review list , Lorenzo Pieralisi , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Manivannan Sadhasivam , Bjorn Helgaas , Linus Walleij , Catalin Marinas , Will Deacon , Bartosz Golaszewski , Derek Kiernan , Dragan Cvetic , Arnd Bergmann , Greg Kroah-Hartman , Saravana Kannan , linux-clk@vger.kernel.org, devicetree@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, linux-gpio@vger.kernel.org, Masahiro Yamada , Stefan Wahren , Herve Codina , Luca Ceresoli , Thomas Petazzoni , Andrew Lunn Subject: [PATCH v2 08/14] clk: rp1: Add support for clocks provided by RP1 Date: Mon, 7 Oct 2024 14:39:51 +0200 Message-ID: <022cf4920f8147cc720eaf02fd52c0fa56f565c5.1728300189.git.andrea.porta@suse.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" RaspberryPi RP1 is an MFD providing, among other peripherals, several clock generators and PLLs that drives the sub-peripherals. Add the driver to support the clock providers. Signed-off-by: Andrea della Porta --- MAINTAINERS | 1 + drivers/clk/Kconfig | 9 + drivers/clk/Makefile | 1 + drivers/clk/clk-rp1.c | 1658 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1669 insertions(+) create mode 100644 drivers/clk/clk-rp1.c diff --git a/MAINTAINERS b/MAINTAINERS index 2aea5a6166bd..dc064cd4b6b5 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -19387,6 +19387,7 @@ F: Documentation/devicetree/bindings/clock/raspberr= ypi,rp1-clocks.yaml F: Documentation/devicetree/bindings/misc/pci1de4,1.yaml F: Documentation/devicetree/bindings/pci/pci-ep-bus.yaml F: Documentation/devicetree/bindings/pinctrl/raspberrypi,rp1-gpio.yaml +F: drivers/clk/clk-rp1.c F: include/dt-bindings/clock/rp1.h F: include/dt-bindings/misc/rp1.h =20 diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig index 299bc678ed1b..537019987f0c 100644 --- a/drivers/clk/Kconfig +++ b/drivers/clk/Kconfig @@ -88,6 +88,15 @@ config COMMON_CLK_RK808 These multi-function devices have two fixed-rate oscillators, clocked a= t 32KHz each. Clkout1 is always on, Clkout2 can off by control register. =20 +config COMMON_CLK_RP1 + tristate "Raspberry Pi RP1-based clock support" + depends on PCI || COMPILE_TEST + depends on COMMON_CLK + help + Enable common clock framework support for Raspberry Pi RP1. + This multi-function device has 3 main PLLs and several clock + generators to drive the internal sub-peripherals. + config COMMON_CLK_HI655X tristate "Clock driver for Hi655x" if EXPERT depends on (MFD_HI655X_PMIC || COMPILE_TEST) diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile index fb8878a5d7d9..2ab97f36c800 100644 --- a/drivers/clk/Makefile +++ b/drivers/clk/Makefile @@ -68,6 +68,7 @@ obj-$(CONFIG_CLK_LS1028A_PLLDIG) +=3D clk-plldig.o obj-$(CONFIG_COMMON_CLK_PWM) +=3D clk-pwm.o obj-$(CONFIG_CLK_QORIQ) +=3D clk-qoriq.o obj-$(CONFIG_COMMON_CLK_RK808) +=3D clk-rk808.o +obj-$(CONFIG_COMMON_CLK_RP1) +=3D clk-rp1.o obj-$(CONFIG_COMMON_CLK_HI655X) +=3D clk-hi655x.o obj-$(CONFIG_COMMON_CLK_S2MPS11) +=3D clk-s2mps11.o obj-$(CONFIG_COMMON_CLK_SCMI) +=3D clk-scmi.o diff --git a/drivers/clk/clk-rp1.c b/drivers/clk/clk-rp1.c new file mode 100644 index 000000000000..9016666fb27d --- /dev/null +++ b/drivers/clk/clk-rp1.c @@ -0,0 +1,1658 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2023 Raspberry Pi Ltd. + * + * Clock driver for RP1 PCIe multifunction chip. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#define PLL_SYS_OFFSET 0x08000 +#define PLL_SYS_CS (PLL_SYS_OFFSET + 0x00) +#define PLL_SYS_PWR (PLL_SYS_OFFSET + 0x04) +#define PLL_SYS_FBDIV_INT (PLL_SYS_OFFSET + 0x08) +#define PLL_SYS_FBDIV_FRAC (PLL_SYS_OFFSET + 0x0c) +#define PLL_SYS_PRIM (PLL_SYS_OFFSET + 0x10) +#define PLL_SYS_SEC (PLL_SYS_OFFSET + 0x14) + +#define PLL_AUDIO_OFFSET 0x0c000 +#define PLL_AUDIO_CS (PLL_AUDIO_OFFSET + 0x00) +#define PLL_AUDIO_PWR (PLL_AUDIO_OFFSET + 0x04) +#define PLL_AUDIO_FBDIV_INT (PLL_AUDIO_OFFSET + 0x08) +#define PLL_AUDIO_FBDIV_FRAC (PLL_AUDIO_OFFSET + 0x0c) +#define PLL_AUDIO_PRIM (PLL_AUDIO_OFFSET + 0x10) +#define PLL_AUDIO_SEC (PLL_AUDIO_OFFSET + 0x14) +#define PLL_AUDIO_TERN (PLL_AUDIO_OFFSET + 0x18) + +#define PLL_VIDEO_OFFSET 0x10000 +#define PLL_VIDEO_CS (PLL_VIDEO_OFFSET + 0x00) +#define PLL_VIDEO_PWR (PLL_VIDEO_OFFSET + 0x04) +#define PLL_VIDEO_FBDIV_INT (PLL_VIDEO_OFFSET + 0x08) +#define PLL_VIDEO_FBDIV_FRAC (PLL_VIDEO_OFFSET + 0x0c) +#define PLL_VIDEO_PRIM (PLL_VIDEO_OFFSET + 0x10) +#define PLL_VIDEO_SEC (PLL_VIDEO_OFFSET + 0x14) + +#define GPCLK_OE_CTRL 0x00000 + +#define CLK_SYS_OFFSET 0x00014 +#define CLK_SYS_CTRL (CLK_SYS_OFFSET + 0x00) +#define CLK_SYS_DIV_INT (CLK_SYS_OFFSET + 0x04) +#define CLK_SYS_SEL (CLK_SYS_OFFSET + 0x0c) + +#define CLK_SLOW_OFFSET 0x00024 +#define CLK_SLOW_SYS_CTRL (CLK_SLOW_OFFSET + 0x00) +#define CLK_SLOW_SYS_DIV_INT (CLK_SLOW_OFFSET + 0x04) +#define CLK_SLOW_SYS_SEL (CLK_SLOW_OFFSET + 0x0c) + +#define CLK_DMA_OFFSET 0x00044 +#define CLK_DMA_CTRL (CLK_DMA_OFFSET + 0x00) +#define CLK_DMA_DIV_INT (CLK_DMA_OFFSET + 0x04) +#define CLK_DMA_SEL (CLK_DMA_OFFSET + 0x0c) + +#define CLK_UART_OFFSET 0x00054 +#define CLK_UART_CTRL (CLK_UART_OFFSET + 0x00) +#define CLK_UART_DIV_INT (CLK_UART_OFFSET + 0x04) +#define CLK_UART_SEL (CLK_UART_OFFSET + 0x0c) + +#define CLK_ETH_OFFSET 0x00064 +#define CLK_ETH_CTRL (CLK_ETH_OFFSET + 0x00) +#define CLK_ETH_DIV_INT (CLK_ETH_OFFSET + 0x04) +#define CLK_ETH_SEL (CLK_ETH_OFFSET + 0x0c) + +#define CLK_PWM0_OFFSET 0x00074 +#define CLK_PWM0_CTRL (CLK_PWM0_OFFSET + 0x00) +#define CLK_PWM0_DIV_INT (CLK_PWM0_OFFSET + 0x04) +#define CLK_PWM0_DIV_FRAC (CLK_PWM0_OFFSET + 0x08) +#define CLK_PWM0_SEL (CLK_PWM0_OFFSET + 0x0c) + +#define CLK_PWM1_OFFSET 0x00084 +#define CLK_PWM1_CTRL (CLK_PWM1_OFFSET + 0x00) +#define CLK_PWM1_DIV_INT (CLK_PWM1_OFFSET + 0x04) +#define CLK_PWM1_DIV_FRAC (CLK_PWM1_OFFSET + 0x08) +#define CLK_PWM1_SEL (CLK_PWM1_OFFSET + 0x0c) + +#define CLK_AUDIO_IN_OFFSET 0x00094 +#define CLK_AUDIO_IN_CTRL (CLK_AUDIO_IN_OFFSET + 0x00) +#define CLK_AUDIO_IN_DIV_INT (CLK_AUDIO_IN_OFFSET + 0x04) +#define CLK_AUDIO_IN_SEL (CLK_AUDIO_IN_OFFSET + 0x0c) + +#define CLK_AUDIO_OUT_OFFSET 0x000a4 +#define CLK_AUDIO_OUT_CTRL (CLK_AUDIO_OUT_OFFSET + 0x00) +#define CLK_AUDIO_OUT_DIV_INT (CLK_AUDIO_OUT_OFFSET + 0x04) +#define CLK_AUDIO_OUT_SEL (CLK_AUDIO_OUT_OFFSET + 0x0c) + +#define CLK_I2S_OFFSET 0x000b4 +#define CLK_I2S_CTRL (CLK_I2S_OFFSET + 0x00) +#define CLK_I2S_DIV_INT (CLK_I2S_OFFSET + 0x04) +#define CLK_I2S_SEL (CLK_I2S_OFFSET + 0x0c) + +#define CLK_MIPI0_CFG_OFFSET 0x000c4 +#define CLK_MIPI0_CFG_CTRL (CLK_MIPI0_CFG_OFFSET + 0x00) +#define CLK_MIPI0_CFG_DIV_INT (CLK_MIPI0_CFG_OFFSET + 0x04) +#define CLK_MIPI0_CFG_SEL (CLK_MIPI0_CFG_OFFSET + 0x0c) + +#define CLK_MIPI1_CFG_OFFSET 0x000d4 +#define CLK_MIPI1_CFG_CTRL (CLK_MIPI1_CFG_OFFSET + 0x00) +#define CLK_MIPI1_CFG_DIV_INT (CLK_MIPI1_CFG_OFFSET + 0x04) +#define CLK_MIPI1_CFG_SEL (CLK_MIPI1_CFG_OFFSET + 0x0c) + +#define CLK_PCIE_AUX_OFFSET 0x000e4 +#define CLK_PCIE_AUX_CTRL (CLK_PCIE_AUX_OFFSET + 0x00) +#define CLK_PCIE_AUX_DIV_INT (CLK_PCIE_AUX_OFFSET + 0x04) +#define CLK_PCIE_AUX_SEL (CLK_PCIE_AUX_OFFSET + 0x0c) + +#define CLK_USBH0_MICROFRAME_OFFSET 0x000f4 +#define CLK_USBH0_MICROFRAME_CTRL (CLK_USBH0_MICROFRAME_OFFSET + 0x00) +#define CLK_USBH0_MICROFRAME_DIV_INT (CLK_USBH0_MICROFRAME_OFFSET + 0x04) +#define CLK_USBH0_MICROFRAME_SEL (CLK_USBH0_MICROFRAME_OFFSET + 0x0c) + +#define CLK_USBH1_MICROFRAME_OFFSET 0x00104 +#define CLK_USBH1_MICROFRAME_CTRL (CLK_USBH1_MICROFRAME_OFFSET + 0x00) +#define CLK_USBH1_MICROFRAME_DIV_INT (CLK_USBH1_MICROFRAME_OFFSET + 0x04) +#define CLK_USBH1_MICROFRAME_SEL (CLK_USBH1_MICROFRAME_OFFSET + 0x0c) + +#define CLK_USBH0_SUSPEND_OFFSET 0x00114 +#define CLK_USBH0_SUSPEND_CTRL (CLK_USBH0_SUSPEND_OFFSET + 0x00) +#define CLK_USBH0_SUSPEND_DIV_INT (CLK_USBH0_SUSPEND_OFFSET + 0x04) +#define CLK_USBH0_SUSPEND_SEL (CLK_USBH0_SUSPEND_OFFSET + 0x0c) + +#define CLK_USBH1_SUSPEND_OFFSET 0x00124 +#define CLK_USBH1_SUSPEND_CTRL (CLK_USBH1_SUSPEND_OFFSET + 0x00) +#define CLK_USBH1_SUSPEND_DIV_INT (CLK_USBH1_SUSPEND_OFFSET + 0x04) +#define CLK_USBH1_SUSPEND_SEL (CLK_USBH1_SUSPEND_OFFSET + 0x0c) + +#define CLK_ETH_TSU_OFFSET 0x00134 +#define CLK_ETH_TSU_CTRL (CLK_ETH_TSU_OFFSET + 0x00) +#define CLK_ETH_TSU_DIV_INT (CLK_ETH_TSU_OFFSET + 0x04) +#define CLK_ETH_TSU_SEL (CLK_ETH_TSU_OFFSET + 0x0c) + +#define CLK_ADC_OFFSET 0x00144 +#define CLK_ADC_CTRL (CLK_ADC_OFFSET + 0x00) +#define CLK_ADC_DIV_INT (CLK_ADC_OFFSET + 0x04) +#define CLK_ADC_SEL (CLK_ADC_OFFSET + 0x0c) + +#define CLK_SDIO_TIMER_OFFSET 0x00154 +#define CLK_SDIO_TIMER_CTRL (CLK_SDIO_TIMER_OFFSET + 0x00) +#define CLK_SDIO_TIMER_DIV_INT (CLK_SDIO_TIMER_OFFSET + 0x04) +#define CLK_SDIO_TIMER_SEL (CLK_SDIO_TIMER_OFFSET + 0x0c) + +#define CLK_SDIO_ALT_SRC_OFFSET 0x00164 +#define CLK_SDIO_ALT_SRC_CTRL (CLK_SDIO_ALT_SRC_OFFSET + 0x00) +#define CLK_SDIO_ALT_SRC_DIV_INT (CLK_SDIO_ALT_SRC_OFFSET + 0x04) +#define CLK_SDIO_ALT_SRC_SEL (CLK_SDIO_ALT_SRC_OFFSET + 0x0c) + +#define CLK_GP0_OFFSET 0x00174 +#define CLK_GP0_CTRL (CLK_GP0_OFFSET + 0x00) +#define CLK_GP0_DIV_INT (CLK_GP0_OFFSET + 0x04) +#define CLK_GP0_DIV_FRAC (CLK_GP0_OFFSET + 0x08) +#define CLK_GP0_SEL (CLK_GP0_OFFSET + 0x0c) + +#define CLK_GP1_OFFSET 0x00184 +#define CLK_GP1_CTRL (CLK_GP1_OFFSET + 0x00) +#define CLK_GP1_DIV_INT (CLK_GP1_OFFSET + 0x04) +#define CLK_GP1_DIV_FRAC (CLK_GP1_OFFSET + 0x08) +#define CLK_GP1_SEL (CLK_GP1_OFFSET + 0x0c) + +#define CLK_GP2_OFFSET 0x00194 +#define CLK_GP2_CTRL (CLK_GP2_OFFSET + 0x00) +#define CLK_GP2_DIV_INT (CLK_GP2_OFFSET + 0x04) +#define CLK_GP2_DIV_FRAC (CLK_GP2_OFFSET + 0x08) +#define CLK_GP2_SEL (CLK_GP2_OFFSET + 0x0c) + +#define CLK_GP3_OFFSET 0x001a4 +#define CLK_GP3_CTRL (CLK_GP3_OFFSET + 0x00) +#define CLK_GP3_DIV_INT (CLK_GP3_OFFSET + 0x04) +#define CLK_GP3_DIV_FRAC (CLK_GP3_OFFSET + 0x08) +#define CLK_GP3_SEL (CLK_GP3_OFFSET + 0x0c) + +#define CLK_GP4_OFFSET 0x001b4 +#define CLK_GP4_CTRL (CLK_GP4_OFFSET + 0x00) +#define CLK_GP4_DIV_INT (CLK_GP4_OFFSET + 0x04) +#define CLK_GP4_DIV_FRAC (CLK_GP4_OFFSET + 0x08) +#define CLK_GP4_SEL (CLK_GP4_OFFSET + 0x0c) + +#define CLK_GP5_OFFSET 0x001c4 +#define CLK_GP5_CTRL (CLK_GP5_OFFSET + 0x00) +#define CLK_GP5_DIV_INT (CLK_GP5_OFFSET + 0x04) +#define CLK_GP5_DIV_FRAC (CLK_GP5_OFFSET + 0x08) +#define CLK_GP5_SEL (CLK_GP5_OFFSET + 0x0c) + +#define CLK_SYS_RESUS_CTRL 0x0020c + +#define CLK_SLOW_SYS_RESUS_CTRL 0x00214 + +#define FC0_OFFSET 0x0021c +#define FC0_REF_KHZ (FC0_OFFSET + 0x00) +#define FC0_MIN_KHZ (FC0_OFFSET + 0x04) +#define FC0_MAX_KHZ (FC0_OFFSET + 0x08) +#define FC0_DELAY (FC0_OFFSET + 0x0c) +#define FC0_INTERVAL (FC0_OFFSET + 0x10) +#define FC0_SRC (FC0_OFFSET + 0x14) +#define FC0_STATUS (FC0_OFFSET + 0x18) +#define FC0_RESULT (FC0_OFFSET + 0x1c) +#define FC_SIZE 0x20 +#define FC_COUNT 8 +#define FC_NUM(idx, off) ((idx) * 32 + (off)) + +#define AUX_SEL 1 + +#define VIDEO_CLOCKS_OFFSET 0x4000 +#define VIDEO_CLK_VEC_CTRL (VIDEO_CLOCKS_OFFSET + 0x0000) +#define VIDEO_CLK_VEC_DIV_INT (VIDEO_CLOCKS_OFFSET + 0x0004) +#define VIDEO_CLK_VEC_SEL (VIDEO_CLOCKS_OFFSET + 0x000c) +#define VIDEO_CLK_DPI_CTRL (VIDEO_CLOCKS_OFFSET + 0x0010) +#define VIDEO_CLK_DPI_DIV_INT (VIDEO_CLOCKS_OFFSET + 0x0014) +#define VIDEO_CLK_DPI_SEL (VIDEO_CLOCKS_OFFSET + 0x001c) +#define VIDEO_CLK_MIPI0_DPI_CTRL (VIDEO_CLOCKS_OFFSET + 0x0020) +#define VIDEO_CLK_MIPI0_DPI_DIV_INT (VIDEO_CLOCKS_OFFSET + 0x0024) +#define VIDEO_CLK_MIPI0_DPI_DIV_FRAC (VIDEO_CLOCKS_OFFSET + 0x0028) +#define VIDEO_CLK_MIPI0_DPI_SEL (VIDEO_CLOCKS_OFFSET + 0x002c) +#define VIDEO_CLK_MIPI1_DPI_CTRL (VIDEO_CLOCKS_OFFSET + 0x0030) +#define VIDEO_CLK_MIPI1_DPI_DIV_INT (VIDEO_CLOCKS_OFFSET + 0x0034) +#define VIDEO_CLK_MIPI1_DPI_DIV_FRAC (VIDEO_CLOCKS_OFFSET + 0x0038) +#define VIDEO_CLK_MIPI1_DPI_SEL (VIDEO_CLOCKS_OFFSET + 0x003c) + +#define DIV_INT_8BIT_MAX GENMASK(7, 0) /* max divide for most clocks */ +#define DIV_INT_16BIT_MAX GENMASK(15, 0) /* max divide for GPx, PWM */ +#define DIV_INT_24BIT_MAX GENMASK(23, 0) /* max divide for C= LK_SYS */ + +#define FC0_STATUS_DONE BIT(4) +#define FC0_STATUS_RUNNING BIT(8) +#define FC0_RESULT_FRAC_SHIFT 5 + +#define PLL_PRIM_DIV1_SHIFT 16 +#define PLL_PRIM_DIV1_WIDTH 3 +#define PLL_PRIM_DIV1_MASK GENMASK(PLL_PRIM_DIV1_SHIFT + \ + PLL_PRIM_DIV1_WIDTH - 1, \ + PLL_PRIM_DIV1_SHIFT) +#define PLL_PRIM_DIV2_SHIFT 12 +#define PLL_PRIM_DIV2_WIDTH 3 +#define PLL_PRIM_DIV2_MASK GENMASK(PLL_PRIM_DIV2_SHIFT + \ + PLL_PRIM_DIV2_WIDTH - 1, \ + PLL_PRIM_DIV2_SHIFT) + +#define PLL_SEC_DIV_SHIFT 8 +#define PLL_SEC_DIV_WIDTH 5 +#define PLL_SEC_DIV_MASK GENMASK(PLL_SEC_DIV_SHIFT + \ + PLL_SEC_DIV_WIDTH - 1, \ + PLL_SEC_DIV_SHIFT) + +#define PLL_CS_LOCK BIT(31) +#define PLL_CS_REFDIV_SHIFT 0 + +#define PLL_PWR_PD BIT(0) +#define PLL_PWR_DACPD BIT(1) +#define PLL_PWR_DSMPD BIT(2) +#define PLL_PWR_POSTDIVPD BIT(3) +#define PLL_PWR_4PHASEPD BIT(4) +#define PLL_PWR_VCOPD BIT(5) +#define PLL_PWR_MASK GENMASK(5, 0) + +#define PLL_SEC_RST BIT(16) +#define PLL_SEC_IMPL BIT(31) + +/* PLL phase output for both PRI and SEC */ +#define PLL_PH_EN BIT(4) +#define PLL_PH_PHASE_SHIFT 0 + +#define RP1_PLL_PHASE_0 0 +#define RP1_PLL_PHASE_90 1 +#define RP1_PLL_PHASE_180 2 +#define RP1_PLL_PHASE_270 3 + +/* Clock fields for all clocks */ +#define CLK_CTRL_ENABLE BIT(11) +#define CLK_CTRL_AUXSRC_SHIFT 5 +#define CLK_CTRL_AUXSRC_WIDTH 5 +#define CLK_CTRL_AUXSRC_MASK GENMASK(CLK_CTRL_AUXSRC_SHIFT + \ + CLK_CTRL_AUXSRC_WIDTH - 1, \ + CLK_CTRL_AUXSRC_SHIFT) +#define CLK_CTRL_SRC_SHIFT 0 +#define CLK_DIV_FRAC_BITS 16 + +#define KHz 1000 +#define MHz (KHz * KHz) +#define LOCK_TIMEOUT_NS 100000000 +#define FC_TIMEOUT_NS 100000000 + +#define MAX_CLK_PARENTS 16 + +/* + * Secondary PLL channel output divider table. + * Divider values range from 8 to 19. + * Invalid values default to 19 + */ +static const struct clk_div_table pll_sec_div_table[] =3D { + { 0x00, 19 }, + { 0x01, 19 }, + { 0x02, 19 }, + { 0x03, 19 }, + { 0x04, 19 }, + { 0x05, 19 }, + { 0x06, 19 }, + { 0x07, 19 }, + { 0x08, 8 }, + { 0x09, 9 }, + { 0x0a, 10 }, + { 0x0b, 11 }, + { 0x0c, 12 }, + { 0x0d, 13 }, + { 0x0e, 14 }, + { 0x0f, 15 }, + { 0x10, 16 }, + { 0x11, 17 }, + { 0x12, 18 }, + { 0x13, 19 }, + { 0x14, 19 }, + { 0x15, 19 }, + { 0x16, 19 }, + { 0x17, 19 }, + { 0x18, 19 }, + { 0x19, 19 }, + { 0x1a, 19 }, + { 0x1b, 19 }, + { 0x1c, 19 }, + { 0x1d, 19 }, + { 0x1e, 19 }, + { 0x1f, 19 }, + { 0 } +}; + +struct rp1_clockman { + struct device *dev; + void __iomem *regs; + spinlock_t regs_lock; /* spinlock for all clocks */ + struct clk_hw *hw_xosc; /* reference clock */ + + /* Must be last */ + struct clk_hw_onecell_data onecell; +}; + +struct rp1_pll_core_data { + const char *name; + u32 cs_reg; + u32 pwr_reg; + u32 fbdiv_int_reg; + u32 fbdiv_frac_reg; + unsigned long flags; + u32 fc0_src; +}; + +struct rp1_pll_data { + const char *name; + const char *source_pll; + u32 ctrl_reg; + unsigned long flags; + u32 fc0_src; +}; + +struct rp1_pll_ph_data { + const char *name; + const char *source_pll; + unsigned int phase; + unsigned int fixed_divider; + u32 ph_reg; + unsigned long flags; + u32 fc0_src; +}; + +struct rp1_pll_divider_data { + const char *name; + const char *source_pll; + u32 sec_reg; + unsigned long flags; + u32 fc0_src; +}; + +struct rp1_clock_data { + const char *name; + const char *const parents[MAX_CLK_PARENTS]; + int num_std_parents; + int num_aux_parents; + unsigned long flags; + u32 oe_mask; + u32 clk_src_mask; + u32 ctrl_reg; + u32 div_int_reg; + u32 div_frac_reg; + u32 sel_reg; + u32 div_int_max; + unsigned long max_freq; + u32 fc0_src; +}; + +struct rp1_pll_core { + struct clk_hw hw; + struct rp1_clockman *clockman; + unsigned long cached_rate; + const struct rp1_pll_core_data *data; +}; + +struct rp1_pll { + struct clk_hw hw; + struct rp1_clockman *clockman; + struct clk_divider div; + unsigned long cached_rate; + const struct rp1_pll_data *data; +}; + +struct rp1_pll_ph { + struct clk_hw hw; + struct rp1_clockman *clockman; + const struct rp1_pll_ph_data *data; +}; + +struct rp1_clock { + struct clk_hw hw; + struct rp1_clockman *clockman; + unsigned long cached_rate; + const struct rp1_clock_data *data; +}; + +struct rp1_clk_change { + struct clk_hw *hw; + unsigned long new_rate; +}; + +struct rp1_clk_desc { + struct clk_hw *(*clk_register)(struct rp1_clockman *clockman, + const void *data); + const void *data; +}; + +static void rp1_debugfs_regset(struct rp1_clockman *clockman, u32 base, + const struct debugfs_reg32 *regs, + size_t nregs, struct dentry *dentry) +{ + struct debugfs_regset32 *regset; + + regset =3D devm_kzalloc(clockman->dev, sizeof(*regset), GFP_KERNEL); + if (!regset) + return; + + regset->regs =3D regs; + regset->nregs =3D nregs; + regset->base =3D clockman->regs + base; + + debugfs_create_regset32("regdump", 0444, dentry, regset); +} + +static inline u32 set_register_field(u32 reg, u32 val, u32 mask, u32 shift) +{ + reg &=3D ~mask; + reg |=3D (val << shift) & mask; + return reg; +} + +static inline +void clockman_write(struct rp1_clockman *clockman, u32 reg, u32 val) +{ + writel(val, clockman->regs + reg); +} + +static inline u32 clockman_read(struct rp1_clockman *clockman, u32 reg) +{ + return readl(clockman->regs + reg); +} + +static int rp1_pll_core_is_on(struct clk_hw *hw) +{ + struct rp1_pll_core *pll_core =3D container_of(hw, struct rp1_pll_core, h= w); + struct rp1_clockman *clockman =3D pll_core->clockman; + const struct rp1_pll_core_data *data =3D pll_core->data; + u32 pwr =3D clockman_read(clockman, data->pwr_reg); + + return (pwr & PLL_PWR_PD) || (pwr & PLL_PWR_POSTDIVPD); +} + +static int rp1_pll_core_on(struct clk_hw *hw) +{ + struct rp1_pll_core *pll_core =3D container_of(hw, struct rp1_pll_core, h= w); + struct rp1_clockman *clockman =3D pll_core->clockman; + const struct rp1_pll_core_data *data =3D pll_core->data; + u32 fbdiv_frac; + ktime_t timeout; + + spin_lock(&clockman->regs_lock); + + if (!(clockman_read(clockman, data->cs_reg) & PLL_CS_LOCK)) { + /* Reset to a known state. */ + clockman_write(clockman, data->pwr_reg, PLL_PWR_MASK); + clockman_write(clockman, data->fbdiv_int_reg, 20); + clockman_write(clockman, data->fbdiv_frac_reg, 0); + clockman_write(clockman, data->cs_reg, 1 << PLL_CS_REFDIV_SHIFT); + } + + /* Come out of reset. */ + fbdiv_frac =3D clockman_read(clockman, data->fbdiv_frac_reg); + clockman_write(clockman, data->pwr_reg, fbdiv_frac ? 0 : PLL_PWR_DSMPD); + spin_unlock(&clockman->regs_lock); + + /* Wait for the PLL to lock. */ + timeout =3D ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS); + while (!(clockman_read(clockman, data->cs_reg) & PLL_CS_LOCK)) { + if (ktime_after(ktime_get(), timeout)) { + dev_err(clockman->dev, "%s: can't lock PLL\n", + clk_hw_get_name(hw)); + return -ETIMEDOUT; + } + cpu_relax(); + } + + return 0; +} + +static void rp1_pll_core_off(struct clk_hw *hw) +{ + struct rp1_pll_core *pll_core =3D container_of(hw, struct rp1_pll_core, h= w); + struct rp1_clockman *clockman =3D pll_core->clockman; + const struct rp1_pll_core_data *data =3D pll_core->data; + + spin_lock(&clockman->regs_lock); + clockman_write(clockman, data->pwr_reg, 0); + spin_unlock(&clockman->regs_lock); +} + +static inline unsigned long get_pll_core_divider(struct clk_hw *hw, + unsigned long rate, + unsigned long parent_rate, + u32 *div_int, u32 *div_frac) +{ + unsigned long calc_rate; + u32 fbdiv_int, fbdiv_frac; + u64 div_fp64; /* 32.32 fixed point fraction. */ + + /* Factor of reference clock to VCO frequency. */ + div_fp64 =3D (u64)(rate) << 32; + div_fp64 =3D DIV_ROUND_CLOSEST_ULL(div_fp64, parent_rate); + + /* Round the fractional component at 24 bits. */ + div_fp64 +=3D 1 << (32 - 24 - 1); + + fbdiv_int =3D div_fp64 >> 32; + fbdiv_frac =3D (div_fp64 >> (32 - 24)) & 0xffffff; + + calc_rate =3D + ((u64)parent_rate * (((u64)fbdiv_int << 24) + fbdiv_frac) + (1 << 23)) >= > 24; + + *div_int =3D fbdiv_int; + *div_frac =3D fbdiv_frac; + + return calc_rate; +} + +static int rp1_pll_core_set_rate(struct clk_hw *hw, + unsigned long rate, unsigned long parent_rate) +{ + struct rp1_pll_core *pll_core =3D container_of(hw, struct rp1_pll_core, h= w); + struct rp1_clockman *clockman =3D pll_core->clockman; + const struct rp1_pll_core_data *data =3D pll_core->data; + unsigned long calc_rate; + u32 fbdiv_int, fbdiv_frac; + + /* Disable dividers to start with. */ + spin_lock(&clockman->regs_lock); + clockman_write(clockman, data->fbdiv_int_reg, 0); + clockman_write(clockman, data->fbdiv_frac_reg, 0); + spin_unlock(&clockman->regs_lock); + + calc_rate =3D get_pll_core_divider(hw, rate, parent_rate, + &fbdiv_int, &fbdiv_frac); + + spin_lock(&clockman->regs_lock); + clockman_write(clockman, data->pwr_reg, fbdiv_frac ? 0 : PLL_PWR_DSMPD); + clockman_write(clockman, data->fbdiv_int_reg, fbdiv_int); + clockman_write(clockman, data->fbdiv_frac_reg, fbdiv_frac); + spin_unlock(&clockman->regs_lock); + + /* Check that reference frequency is no greater than VCO / 16. */ + if (WARN_ON_ONCE(parent_rate > (rate / 16))) + return -ERANGE; + + pll_core->cached_rate =3D calc_rate; + + spin_lock(&clockman->regs_lock); + /* Don't need to divide ref unless parent_rate > (output freq / 16) */ + clockman_write(clockman, data->cs_reg, + clockman_read(clockman, data->cs_reg) | + (1 << PLL_CS_REFDIV_SHIFT)); + spin_unlock(&clockman->regs_lock); + + return 0; +} + +static unsigned long rp1_pll_core_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct rp1_pll_core *pll_core =3D container_of(hw, struct rp1_pll_core, h= w); + struct rp1_clockman *clockman =3D pll_core->clockman; + const struct rp1_pll_core_data *data =3D pll_core->data; + u32 fbdiv_int, fbdiv_frac; + unsigned long calc_rate; + + fbdiv_int =3D clockman_read(clockman, data->fbdiv_int_reg); + fbdiv_frac =3D clockman_read(clockman, data->fbdiv_frac_reg); + calc_rate =3D + ((u64)parent_rate * (((u64)fbdiv_int << 24) + fbdiv_frac) + (1 << 23)) >= > 24; + + return calc_rate; +} + +static long rp1_pll_core_round_rate(struct clk_hw *hw, unsigned long rate, + unsigned long *parent_rate) +{ + u32 fbdiv_int, fbdiv_frac; + long calc_rate; + + calc_rate =3D get_pll_core_divider(hw, rate, *parent_rate, + &fbdiv_int, &fbdiv_frac); + return calc_rate; +} + +static void rp1_pll_core_debug_init(struct clk_hw *hw, struct dentry *dent= ry) +{ + struct rp1_pll_core *pll_core =3D container_of(hw, struct rp1_pll_core, h= w); + struct rp1_clockman *clockman =3D pll_core->clockman; + const struct rp1_pll_core_data *data =3D pll_core->data; + struct debugfs_reg32 *regs; + + regs =3D devm_kcalloc(clockman->dev, 4, sizeof(*regs), GFP_KERNEL); + if (!regs) + return; + + regs[0].name =3D "cs"; + regs[0].offset =3D data->cs_reg; + regs[1].name =3D "pwr"; + regs[1].offset =3D data->pwr_reg; + regs[2].name =3D "fbdiv_int"; + regs[2].offset =3D data->fbdiv_int_reg; + regs[3].name =3D "fbdiv_frac"; + regs[3].offset =3D data->fbdiv_frac_reg; + + rp1_debugfs_regset(clockman, 0, regs, 4, dentry); +} + +static void get_pll_prim_dividers(unsigned long rate, unsigned long parent= _rate, + u32 *divider1, u32 *divider2) +{ + unsigned int div1, div2; + unsigned int best_div1 =3D 7, best_div2 =3D 7; + unsigned long best_rate_diff =3D + abs_diff(DIV_ROUND_CLOSEST(parent_rate, best_div1 * best_div2), rate); + unsigned long rate_diff, calc_rate; + + for (div1 =3D 1; div1 <=3D 7; div1++) { + for (div2 =3D 1; div2 <=3D div1; div2++) { + calc_rate =3D DIV_ROUND_CLOSEST(parent_rate, div1 * div2); + rate_diff =3D abs_diff(calc_rate, rate); + + if (calc_rate =3D=3D rate) { + best_div1 =3D div1; + best_div2 =3D div2; + goto done; + } else if (rate_diff < best_rate_diff) { + best_div1 =3D div1; + best_div2 =3D div2; + best_rate_diff =3D rate_diff; + } + } + } + +done: + *divider1 =3D best_div1; + *divider2 =3D best_div2; +} + +static int rp1_pll_set_rate(struct clk_hw *hw, + unsigned long rate, unsigned long parent_rate) +{ + struct rp1_pll *pll =3D container_of(hw, struct rp1_pll, hw); + struct rp1_clockman *clockman =3D pll->clockman; + const struct rp1_pll_data *data =3D pll->data; + u32 prim, prim_div1, prim_div2; + + get_pll_prim_dividers(rate, parent_rate, &prim_div1, &prim_div2); + + spin_lock(&clockman->regs_lock); + prim =3D clockman_read(clockman, data->ctrl_reg); + prim =3D set_register_field(prim, prim_div1, PLL_PRIM_DIV1_MASK, + PLL_PRIM_DIV1_SHIFT); + prim =3D set_register_field(prim, prim_div2, PLL_PRIM_DIV2_MASK, + PLL_PRIM_DIV2_SHIFT); + clockman_write(clockman, data->ctrl_reg, prim); + spin_unlock(&clockman->regs_lock); + + return 0; +} + +static unsigned long rp1_pll_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct rp1_pll *pll =3D container_of(hw, struct rp1_pll, hw); + struct rp1_clockman *clockman =3D pll->clockman; + const struct rp1_pll_data *data =3D pll->data; + u32 prim, prim_div1, prim_div2; + + prim =3D clockman_read(clockman, data->ctrl_reg); + prim_div1 =3D (prim & PLL_PRIM_DIV1_MASK) >> PLL_PRIM_DIV1_SHIFT; + prim_div2 =3D (prim & PLL_PRIM_DIV2_MASK) >> PLL_PRIM_DIV2_SHIFT; + + if (!prim_div1 || !prim_div2) { + dev_err(clockman->dev, "%s: (%s) zero divider value\n", + __func__, data->name); + return 0; + } + + return DIV_ROUND_CLOSEST(parent_rate, prim_div1 * prim_div2); +} + +static long rp1_pll_round_rate(struct clk_hw *hw, unsigned long rate, + unsigned long *parent_rate) +{ + u32 div1, div2; + + get_pll_prim_dividers(rate, *parent_rate, &div1, &div2); + + return DIV_ROUND_CLOSEST(*parent_rate, div1 * div2); +} + +static void rp1_pll_debug_init(struct clk_hw *hw, + struct dentry *dentry) +{ + struct rp1_pll *pll =3D container_of(hw, struct rp1_pll, hw); + struct rp1_clockman *clockman =3D pll->clockman; + const struct rp1_pll_data *data =3D pll->data; + struct debugfs_reg32 *regs; + + regs =3D devm_kcalloc(clockman->dev, 1, sizeof(*regs), GFP_KERNEL); + if (!regs) + return; + + regs[0].name =3D "prim"; + regs[0].offset =3D data->ctrl_reg; + + rp1_debugfs_regset(clockman, 0, regs, 1, dentry); +} + +static int rp1_pll_ph_is_on(struct clk_hw *hw) +{ + struct rp1_pll_ph *pll =3D container_of(hw, struct rp1_pll_ph, hw); + struct rp1_clockman *clockman =3D pll->clockman; + const struct rp1_pll_ph_data *data =3D pll->data; + + return !!(clockman_read(clockman, data->ph_reg) & PLL_PH_EN); +} + +static int rp1_pll_ph_on(struct clk_hw *hw) +{ + struct rp1_pll_ph *pll_ph =3D container_of(hw, struct rp1_pll_ph, hw); + struct rp1_clockman *clockman =3D pll_ph->clockman; + const struct rp1_pll_ph_data *data =3D pll_ph->data; + u32 ph_reg; + + /* todo: ensure pri/sec is enabled! */ + spin_lock(&clockman->regs_lock); + ph_reg =3D clockman_read(clockman, data->ph_reg); + ph_reg |=3D data->phase << PLL_PH_PHASE_SHIFT; + ph_reg |=3D PLL_PH_EN; + clockman_write(clockman, data->ph_reg, ph_reg); + spin_unlock(&clockman->regs_lock); + + return 0; +} + +static void rp1_pll_ph_off(struct clk_hw *hw) +{ + struct rp1_pll_ph *pll_ph =3D container_of(hw, struct rp1_pll_ph, hw); + struct rp1_clockman *clockman =3D pll_ph->clockman; + const struct rp1_pll_ph_data *data =3D pll_ph->data; + + spin_lock(&clockman->regs_lock); + clockman_write(clockman, data->ph_reg, + clockman_read(clockman, data->ph_reg) & ~PLL_PH_EN); + spin_unlock(&clockman->regs_lock); +} + +static int rp1_pll_ph_set_rate(struct clk_hw *hw, + unsigned long rate, unsigned long parent_rate) +{ + struct rp1_pll_ph *pll_ph =3D container_of(hw, struct rp1_pll_ph, hw); + const struct rp1_pll_ph_data *data =3D pll_ph->data; + + /* Nothing really to do here! */ + WARN_ON(data->fixed_divider !=3D 1 && data->fixed_divider !=3D 2); + WARN_ON(rate !=3D parent_rate / data->fixed_divider); + + return 0; +} + +static unsigned long rp1_pll_ph_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct rp1_pll_ph *pll_ph =3D container_of(hw, struct rp1_pll_ph, hw); + const struct rp1_pll_ph_data *data =3D pll_ph->data; + + return parent_rate / data->fixed_divider; +} + +static long rp1_pll_ph_round_rate(struct clk_hw *hw, unsigned long rate, + unsigned long *parent_rate) +{ + struct rp1_pll_ph *pll_ph =3D container_of(hw, struct rp1_pll_ph, hw); + const struct rp1_pll_ph_data *data =3D pll_ph->data; + + return *parent_rate / data->fixed_divider; +} + +static void rp1_pll_ph_debug_init(struct clk_hw *hw, + struct dentry *dentry) +{ + struct rp1_pll_ph *pll_ph =3D container_of(hw, struct rp1_pll_ph, hw); + const struct rp1_pll_ph_data *data =3D pll_ph->data; + struct rp1_clockman *clockman =3D pll_ph->clockman; + struct debugfs_reg32 *regs; + + regs =3D devm_kcalloc(clockman->dev, 1, sizeof(*regs), GFP_KERNEL); + if (!regs) + return; + + regs[0].name =3D "ph_reg"; + regs[0].offset =3D data->ph_reg; + + rp1_debugfs_regset(clockman, 0, regs, 1, dentry); +} + +static int rp1_pll_divider_is_on(struct clk_hw *hw) +{ + struct rp1_pll *divider =3D container_of(hw, struct rp1_pll, div.hw); + struct rp1_clockman *clockman =3D divider->clockman; + const struct rp1_pll_data *data =3D divider->data; + + return !(clockman_read(clockman, data->ctrl_reg) & PLL_SEC_RST); +} + +static int rp1_pll_divider_on(struct clk_hw *hw) +{ + struct rp1_pll *divider =3D container_of(hw, struct rp1_pll, div.hw); + struct rp1_clockman *clockman =3D divider->clockman; + const struct rp1_pll_data *data =3D divider->data; + + spin_lock(&clockman->regs_lock); + /* Check the implementation bit is set! */ + WARN_ON(!(clockman_read(clockman, data->ctrl_reg) & PLL_SEC_IMPL)); + clockman_write(clockman, data->ctrl_reg, + clockman_read(clockman, data->ctrl_reg) & ~PLL_SEC_RST); + spin_unlock(&clockman->regs_lock); + + return 0; +} + +static void rp1_pll_divider_off(struct clk_hw *hw) +{ + struct rp1_pll *divider =3D container_of(hw, struct rp1_pll, div.hw); + struct rp1_clockman *clockman =3D divider->clockman; + const struct rp1_pll_data *data =3D divider->data; + + spin_lock(&clockman->regs_lock); + clockman_write(clockman, data->ctrl_reg, PLL_SEC_RST); + spin_unlock(&clockman->regs_lock); +} + +static int rp1_pll_divider_set_rate(struct clk_hw *hw, + unsigned long rate, + unsigned long parent_rate) +{ + struct rp1_pll *divider =3D container_of(hw, struct rp1_pll, div.hw); + struct rp1_clockman *clockman =3D divider->clockman; + const struct rp1_pll_data *data =3D divider->data; + u32 div, sec; + + div =3D DIV_ROUND_UP_ULL(parent_rate, rate); + div =3D clamp(div, 8u, 19u); + + spin_lock(&clockman->regs_lock); + sec =3D clockman_read(clockman, data->ctrl_reg); + sec =3D set_register_field(sec, div, PLL_SEC_DIV_MASK, PLL_SEC_DIV_SHIFT); + + /* Must keep the divider in reset to change the value. */ + sec |=3D PLL_SEC_RST; + clockman_write(clockman, data->ctrl_reg, sec); + + // todo: must sleep 10 pll vco cycles + sec &=3D ~PLL_SEC_RST; + clockman_write(clockman, data->ctrl_reg, sec); + spin_unlock(&clockman->regs_lock); + + return 0; +} + +static unsigned long rp1_pll_divider_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + return clk_divider_ops.recalc_rate(hw, parent_rate); +} + +static long rp1_pll_divider_round_rate(struct clk_hw *hw, + unsigned long rate, + unsigned long *parent_rate) +{ + return clk_divider_ops.round_rate(hw, rate, parent_rate); +} + +static void rp1_pll_divider_debug_init(struct clk_hw *hw, struct dentry *d= entry) +{ + struct rp1_pll *divider =3D container_of(hw, struct rp1_pll, div.hw); + struct rp1_clockman *clockman =3D divider->clockman; + const struct rp1_pll_data *data =3D divider->data; + struct debugfs_reg32 *regs; + + regs =3D devm_kcalloc(clockman->dev, 1, sizeof(*regs), GFP_KERNEL); + if (!regs) + return; + + regs[0].name =3D "sec"; + regs[0].offset =3D data->ctrl_reg; + + rp1_debugfs_regset(clockman, 0, regs, 1, dentry); +} + +static int rp1_clock_is_on(struct clk_hw *hw) +{ + struct rp1_clock *clock =3D container_of(hw, struct rp1_clock, hw); + struct rp1_clockman *clockman =3D clock->clockman; + const struct rp1_clock_data *data =3D clock->data; + + return !!(clockman_read(clockman, data->ctrl_reg) & CLK_CTRL_ENABLE); +} + +static unsigned long rp1_clock_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct rp1_clock *clock =3D container_of(hw, struct rp1_clock, hw); + struct rp1_clockman *clockman =3D clock->clockman; + const struct rp1_clock_data *data =3D clock->data; + u64 calc_rate; + u64 div; + + u32 frac; + + div =3D clockman_read(clockman, data->div_int_reg); + frac =3D (data->div_frac_reg !=3D 0) ? + clockman_read(clockman, data->div_frac_reg) : 0; + + /* If the integer portion of the divider is 0, treat it as 2^16 */ + if (!div) + div =3D 1 << 16; + + div =3D (div << CLK_DIV_FRAC_BITS) | (frac >> (32 - CLK_DIV_FRAC_BITS)); + + calc_rate =3D (u64)parent_rate << CLK_DIV_FRAC_BITS; + calc_rate =3D div64_u64(calc_rate, div); + + return calc_rate; +} + +static int rp1_clock_on(struct clk_hw *hw) +{ + struct rp1_clock *clock =3D container_of(hw, struct rp1_clock, hw); + struct rp1_clockman *clockman =3D clock->clockman; + const struct rp1_clock_data *data =3D clock->data; + + spin_lock(&clockman->regs_lock); + clockman_write(clockman, data->ctrl_reg, + clockman_read(clockman, data->ctrl_reg) | CLK_CTRL_ENABLE); + /* If this is a GPCLK, turn on the output-enable */ + if (data->oe_mask) + clockman_write(clockman, GPCLK_OE_CTRL, + clockman_read(clockman, GPCLK_OE_CTRL) | data->oe_mask); + spin_unlock(&clockman->regs_lock); + + return 0; +} + +static void rp1_clock_off(struct clk_hw *hw) +{ + struct rp1_clock *clock =3D container_of(hw, struct rp1_clock, hw); + struct rp1_clockman *clockman =3D clock->clockman; + const struct rp1_clock_data *data =3D clock->data; + + spin_lock(&clockman->regs_lock); + clockman_write(clockman, data->ctrl_reg, + clockman_read(clockman, data->ctrl_reg) & ~CLK_CTRL_ENABLE); + /* If this is a GPCLK, turn off the output-enable */ + if (data->oe_mask) + clockman_write(clockman, GPCLK_OE_CTRL, + clockman_read(clockman, GPCLK_OE_CTRL) & ~data->oe_mask); + spin_unlock(&clockman->regs_lock); +} + +static u32 rp1_clock_choose_div(unsigned long rate, unsigned long parent_r= ate, + const struct rp1_clock_data *data) +{ + u64 div; + + /* + * Due to earlier rounding, calculated parent_rate may differ from + * expected value. Don't fail on a small discrepancy near unity divide. + */ + if (!rate || rate > parent_rate + (parent_rate >> CLK_DIV_FRAC_BITS)) + return 0; + + /* + * Always express div in fixed-point format for fractional division; + * If no fractional divider is present, the fraction part will be zero. + */ + if (data->div_frac_reg) { + div =3D (u64)parent_rate << CLK_DIV_FRAC_BITS; + div =3D DIV_ROUND_CLOSEST_ULL(div, rate); + } else { + div =3D DIV_ROUND_CLOSEST_ULL(parent_rate, rate); + div <<=3D CLK_DIV_FRAC_BITS; + } + + div =3D clamp(div, + 1ull << CLK_DIV_FRAC_BITS, + (u64)data->div_int_max << CLK_DIV_FRAC_BITS); + + return div; +} + +static u8 rp1_clock_get_parent(struct clk_hw *hw) +{ + struct rp1_clock *clock =3D container_of(hw, struct rp1_clock, hw); + struct rp1_clockman *clockman =3D clock->clockman; + const struct rp1_clock_data *data =3D clock->data; + u32 sel, ctrl; + u8 parent; + + /* Sel is one-hot, so find the first bit set */ + sel =3D clockman_read(clockman, data->sel_reg); + parent =3D ffs(sel) - 1; + + /* sel =3D=3D 0 implies the parent clock is not enabled yet. */ + if (!sel) { + /* Read the clock src from the CTRL register instead */ + ctrl =3D clockman_read(clockman, data->ctrl_reg); + parent =3D (ctrl & data->clk_src_mask) >> CLK_CTRL_SRC_SHIFT; + } + + if (parent >=3D data->num_std_parents) + parent =3D AUX_SEL; + + if (parent =3D=3D AUX_SEL) { + /* + * Clock parent is an auxiliary source, so get the parent from + * the AUXSRC register field. + */ + ctrl =3D clockman_read(clockman, data->ctrl_reg); + parent =3D (ctrl & CLK_CTRL_AUXSRC_MASK) >> CLK_CTRL_AUXSRC_SHIFT; + parent +=3D data->num_std_parents; + } + + return parent; +} + +static int rp1_clock_set_parent(struct clk_hw *hw, u8 index) +{ + struct rp1_clock *clock =3D container_of(hw, struct rp1_clock, hw); + struct rp1_clockman *clockman =3D clock->clockman; + const struct rp1_clock_data *data =3D clock->data; + u32 ctrl, sel; + + spin_lock(&clockman->regs_lock); + ctrl =3D clockman_read(clockman, data->ctrl_reg); + + if (index >=3D data->num_std_parents) { + /* This is an aux source request */ + if (index >=3D data->num_std_parents + data->num_aux_parents) { + spin_unlock(&clockman->regs_lock); + return -EINVAL; + } + + /* Select parent from aux list */ + ctrl =3D set_register_field(ctrl, index - data->num_std_parents, + CLK_CTRL_AUXSRC_MASK, + CLK_CTRL_AUXSRC_SHIFT); + /* Set src to aux list */ + ctrl =3D set_register_field(ctrl, AUX_SEL, data->clk_src_mask, + CLK_CTRL_SRC_SHIFT); + } else { + ctrl =3D set_register_field(ctrl, index, data->clk_src_mask, + CLK_CTRL_SRC_SHIFT); + } + + clockman_write(clockman, data->ctrl_reg, ctrl); + spin_unlock(&clockman->regs_lock); + + sel =3D rp1_clock_get_parent(hw); + WARN(sel !=3D index, "(%s): Parent index req %u returned back %u\n", + data->name, index, sel); + + return 0; +} + +static int rp1_clock_set_rate_and_parent(struct clk_hw *hw, + unsigned long rate, + unsigned long parent_rate, + u8 parent) +{ + struct rp1_clock *clock =3D container_of(hw, struct rp1_clock, hw); + struct rp1_clockman *clockman =3D clock->clockman; + const struct rp1_clock_data *data =3D clock->data; + u32 div =3D rp1_clock_choose_div(rate, parent_rate, data); + + WARN(rate > 4000000000ll, "rate is -ve (%d)\n", (int)rate); + + if (WARN(!div, + "clk divider calculated as 0! (%s, rate %ld, parent rate %ld)\n", + data->name, rate, parent_rate)) + div =3D 1 << CLK_DIV_FRAC_BITS; + + spin_lock(&clockman->regs_lock); + + clockman_write(clockman, data->div_int_reg, div >> CLK_DIV_FRAC_BITS); + if (data->div_frac_reg) + clockman_write(clockman, data->div_frac_reg, div << (32 - CLK_DIV_FRAC_B= ITS)); + + spin_unlock(&clockman->regs_lock); + + if (parent !=3D 0xff) + rp1_clock_set_parent(hw, parent); + + return 0; +} + +static int rp1_clock_set_rate(struct clk_hw *hw, unsigned long rate, + unsigned long parent_rate) +{ + return rp1_clock_set_rate_and_parent(hw, rate, parent_rate, 0xff); +} + +static void rp1_clock_choose_div_and_prate(struct clk_hw *hw, + int parent_idx, + unsigned long rate, + unsigned long *prate, + unsigned long *calc_rate) +{ + struct rp1_clock *clock =3D container_of(hw, struct rp1_clock, hw); + const struct rp1_clock_data *data =3D clock->data; + struct clk_hw *parent; + u32 div; + u64 tmp; + + parent =3D clk_hw_get_parent_by_index(hw, parent_idx); + + *prate =3D clk_hw_get_rate(parent); + div =3D rp1_clock_choose_div(rate, *prate, data); + + if (!div) { + *calc_rate =3D 0; + return; + } + + /* Recalculate to account for rounding errors */ + tmp =3D (u64)*prate << CLK_DIV_FRAC_BITS; + tmp =3D div_u64(tmp, div); + + /* + * Prevent overclocks - if all parent choices result in + * a downstream clock in excess of the maximum, then the + * call to set the clock will fail. + */ + if (tmp > clock->data->max_freq) + *calc_rate =3D 0; + else + *calc_rate =3D tmp; +} + +static int rp1_clock_determine_rate(struct clk_hw *hw, + struct clk_rate_request *req) +{ + struct clk_hw *parent, *best_parent =3D NULL; + unsigned long best_rate =3D 0; + unsigned long best_prate =3D 0; + unsigned long best_rate_diff =3D ULONG_MAX; + unsigned long prate, calc_rate; + size_t i; + + /* + * If the NO_REPARENT flag is set, try to use existing parent. + */ + if ((clk_hw_get_flags(hw) & CLK_SET_RATE_NO_REPARENT)) { + i =3D rp1_clock_get_parent(hw); + parent =3D clk_hw_get_parent_by_index(hw, i); + if (parent) { + rp1_clock_choose_div_and_prate(hw, i, req->rate, &prate, + &calc_rate); + if (calc_rate > 0) { + req->best_parent_hw =3D parent; + req->best_parent_rate =3D prate; + req->rate =3D calc_rate; + return 0; + } + } + } + + /* + * Select parent clock that results in the closest rate (lower or + * higher) + */ + for (i =3D 0; i < clk_hw_get_num_parents(hw); i++) { + parent =3D clk_hw_get_parent_by_index(hw, i); + if (!parent) + continue; + + rp1_clock_choose_div_and_prate(hw, i, req->rate, &prate, + &calc_rate); + + if (abs_diff(calc_rate, req->rate) < best_rate_diff) { + best_parent =3D parent; + best_prate =3D prate; + best_rate =3D calc_rate; + best_rate_diff =3D abs_diff(calc_rate, req->rate); + + if (best_rate_diff =3D=3D 0) + break; + } + } + + if (best_rate =3D=3D 0) + return -EINVAL; + + req->best_parent_hw =3D best_parent; + req->best_parent_rate =3D best_prate; + req->rate =3D best_rate; + + return 0; +} + +static void rp1_clk_debug_init(struct clk_hw *hw, struct dentry *dentry) +{ + struct rp1_clock *clock =3D container_of(hw, struct rp1_clock, hw); + struct rp1_clockman *clockman =3D clock->clockman; + const struct rp1_clock_data *data =3D clock->data; + struct debugfs_reg32 *regs; + int i; + + regs =3D devm_kcalloc(clockman->dev, 4, sizeof(*regs), GFP_KERNEL); + if (!regs) + return; + + i =3D 0; + regs[i].name =3D "ctrl"; + regs[i++].offset =3D data->ctrl_reg; + regs[i].name =3D "div_int"; + regs[i++].offset =3D data->div_int_reg; + regs[i].name =3D "div_frac"; + regs[i++].offset =3D data->div_frac_reg; + regs[i].name =3D "sel"; + regs[i++].offset =3D data->sel_reg; + + rp1_debugfs_regset(clockman, 0, regs, i, dentry); +} + +static const struct clk_ops rp1_pll_core_ops =3D { + .is_prepared =3D rp1_pll_core_is_on, + .prepare =3D rp1_pll_core_on, + .unprepare =3D rp1_pll_core_off, + .set_rate =3D rp1_pll_core_set_rate, + .recalc_rate =3D rp1_pll_core_recalc_rate, + .round_rate =3D rp1_pll_core_round_rate, + .debug_init =3D rp1_pll_core_debug_init, +}; + +static const struct clk_ops rp1_pll_ops =3D { + .set_rate =3D rp1_pll_set_rate, + .recalc_rate =3D rp1_pll_recalc_rate, + .round_rate =3D rp1_pll_round_rate, + .debug_init =3D rp1_pll_debug_init, +}; + +static const struct clk_ops rp1_pll_ph_ops =3D { + .is_prepared =3D rp1_pll_ph_is_on, + .prepare =3D rp1_pll_ph_on, + .unprepare =3D rp1_pll_ph_off, + .set_rate =3D rp1_pll_ph_set_rate, + .recalc_rate =3D rp1_pll_ph_recalc_rate, + .round_rate =3D rp1_pll_ph_round_rate, + .debug_init =3D rp1_pll_ph_debug_init, +}; + +static const struct clk_ops rp1_pll_divider_ops =3D { + .is_prepared =3D rp1_pll_divider_is_on, + .prepare =3D rp1_pll_divider_on, + .unprepare =3D rp1_pll_divider_off, + .set_rate =3D rp1_pll_divider_set_rate, + .recalc_rate =3D rp1_pll_divider_recalc_rate, + .round_rate =3D rp1_pll_divider_round_rate, + .debug_init =3D rp1_pll_divider_debug_init, +}; + +static const struct clk_ops rp1_clk_ops =3D { + .is_prepared =3D rp1_clock_is_on, + .prepare =3D rp1_clock_on, + .unprepare =3D rp1_clock_off, + .recalc_rate =3D rp1_clock_recalc_rate, + .get_parent =3D rp1_clock_get_parent, + .set_parent =3D rp1_clock_set_parent, + .set_rate_and_parent =3D rp1_clock_set_rate_and_parent, + .set_rate =3D rp1_clock_set_rate, + .determine_rate =3D rp1_clock_determine_rate, + .debug_init =3D rp1_clk_debug_init, +}; + +static struct clk_hw *rp1_register_pll_core(struct rp1_clockman *clockman, + const void *data) +{ + const char *ref_clk_name =3D clk_hw_get_name(clockman->hw_xosc); + const struct rp1_pll_core_data *pll_core_data =3D data; + struct rp1_pll_core *pll_core; + struct clk_init_data init; + int ret; + + memset(&init, 0, sizeof(init)); + + /* All of the PLL cores derive from the external oscillator. */ + init.parent_names =3D &ref_clk_name; + init.num_parents =3D 1; + init.name =3D pll_core_data->name; + init.ops =3D &rp1_pll_core_ops; + init.flags =3D pll_core_data->flags | CLK_IGNORE_UNUSED | CLK_IS_CRITICAL; + + pll_core =3D devm_kzalloc(clockman->dev, sizeof(*pll_core), GFP_KERNEL); + if (!pll_core) + return NULL; + + pll_core->clockman =3D clockman; + pll_core->data =3D pll_core_data; + pll_core->hw.init =3D &init; + + ret =3D devm_clk_hw_register(clockman->dev, &pll_core->hw); + if (ret) + return ERR_PTR(ret); + + return &pll_core->hw; +} + +static struct clk_hw *rp1_register_pll(struct rp1_clockman *clockman, + const void *data) +{ + const struct rp1_pll_data *pll_data =3D data; + struct rp1_pll *pll; + struct clk_init_data init; + int ret; + + memset(&init, 0, sizeof(init)); + + init.parent_names =3D &pll_data->source_pll; + init.num_parents =3D 1; + init.name =3D pll_data->name; + init.ops =3D &rp1_pll_ops; + init.flags =3D pll_data->flags | CLK_IGNORE_UNUSED | CLK_IS_CRITICAL; + + pll =3D devm_kzalloc(clockman->dev, sizeof(*pll), GFP_KERNEL); + if (!pll) + return NULL; + + pll->clockman =3D clockman; + pll->data =3D pll_data; + pll->hw.init =3D &init; + + ret =3D devm_clk_hw_register(clockman->dev, &pll->hw); + if (ret) + return ERR_PTR(ret); + + return &pll->hw; +} + +static struct clk_hw *rp1_register_pll_ph(struct rp1_clockman *clockman, + const void *data) +{ + const struct rp1_pll_ph_data *ph_data =3D data; + struct rp1_pll_ph *ph; + struct clk_init_data init; + int ret; + + memset(&init, 0, sizeof(init)); + + init.parent_names =3D &ph_data->source_pll; + init.num_parents =3D 1; + init.name =3D ph_data->name; + init.ops =3D &rp1_pll_ph_ops; + init.flags =3D ph_data->flags | CLK_IGNORE_UNUSED; + + ph =3D devm_kzalloc(clockman->dev, sizeof(*ph), GFP_KERNEL); + if (!ph) + return NULL; + + ph->clockman =3D clockman; + ph->data =3D ph_data; + ph->hw.init =3D &init; + + ret =3D devm_clk_hw_register(clockman->dev, &ph->hw); + if (ret) + return ERR_PTR(ret); + + return &ph->hw; +} + +static struct clk_hw *rp1_register_pll_divider(struct rp1_clockman *clockm= an, + const void *data) +{ + const struct rp1_pll_data *divider_data =3D data; + struct rp1_pll *divider; + struct clk_init_data init; + int ret; + + memset(&init, 0, sizeof(init)); + + init.parent_names =3D ÷r_data->source_pll; + init.num_parents =3D 1; + init.name =3D divider_data->name; + init.ops =3D &rp1_pll_divider_ops; + init.flags =3D divider_data->flags | CLK_IGNORE_UNUSED | CLK_IS_CRITICAL; + + divider =3D devm_kzalloc(clockman->dev, sizeof(*divider), GFP_KERNEL); + if (!divider) + return NULL; + + divider->div.reg =3D clockman->regs + divider_data->ctrl_reg; + divider->div.shift =3D PLL_SEC_DIV_SHIFT; + divider->div.width =3D PLL_SEC_DIV_WIDTH; + divider->div.flags =3D CLK_DIVIDER_ROUND_CLOSEST; + divider->div.flags |=3D CLK_IS_CRITICAL; + divider->div.lock =3D &clockman->regs_lock; + divider->div.hw.init =3D &init; + divider->div.table =3D pll_sec_div_table; + + divider->clockman =3D clockman; + divider->data =3D divider_data; + + ret =3D devm_clk_hw_register(clockman->dev, ÷r->div.hw); + if (ret) + return ERR_PTR(ret); + + return ÷r->div.hw; +} + +static struct clk_hw *rp1_register_clock(struct rp1_clockman *clockman, + const void *data) +{ + const struct rp1_clock_data *clock_data =3D data; + struct rp1_clock *clock; + struct clk_init_data init; + int ret; + + if (WARN_ON_ONCE(MAX_CLK_PARENTS < + clock_data->num_std_parents + clock_data->num_aux_parents)) + return NULL; + + /* There must be a gap for the AUX selector */ + if (WARN_ON_ONCE(clock_data->num_std_parents > AUX_SEL && + strcmp("-", clock_data->parents[AUX_SEL]))) + return NULL; + + memset(&init, 0, sizeof(init)); + init.parent_names =3D clock_data->parents; + init.num_parents =3D clock_data->num_std_parents + + clock_data->num_aux_parents; + init.name =3D clock_data->name; + init.flags =3D clock_data->flags | CLK_IGNORE_UNUSED; + init.ops =3D &rp1_clk_ops; + + clock =3D devm_kzalloc(clockman->dev, sizeof(*clock), GFP_KERNEL); + if (!clock) + return NULL; + + clock->clockman =3D clockman; + clock->data =3D clock_data; + clock->hw.init =3D &init; + + ret =3D devm_clk_hw_register(clockman->dev, &clock->hw); + if (ret) + return ERR_PTR(ret); + + return &clock->hw; +} + +/* Assignment helper macros for different clock types. */ +#define _REGISTER(f, ...) { .clk_register =3D f, .data =3D __VA_ARGS__ } + +#define REGISTER_PLL_CORE(...) _REGISTER(&rp1_register_pll_core, \ + &(struct rp1_pll_core_data) \ + {__VA_ARGS__}) + +#define REGISTER_PLL(...) _REGISTER(&rp1_register_pll, \ + &(struct rp1_pll_data) \ + {__VA_ARGS__}) + +#define REGISTER_PLL_PH(...) _REGISTER(&rp1_register_pll_ph, \ + &(struct rp1_pll_ph_data) \ + {__VA_ARGS__}) + +#define REGISTER_PLL_DIV(...) _REGISTER(&rp1_register_pll_divider, \ + &(struct rp1_pll_data) \ + {__VA_ARGS__}) + +#define REGISTER_CLK(...) _REGISTER(&rp1_register_clock, \ + &(struct rp1_clock_data) \ + {__VA_ARGS__}) + +static const struct rp1_clk_desc clk_desc_array[] =3D { + [RP1_PLL_SYS_CORE] =3D REGISTER_PLL_CORE(.name =3D "pll_sys_core", + .cs_reg =3D PLL_SYS_CS, + .pwr_reg =3D PLL_SYS_PWR, + .fbdiv_int_reg =3D PLL_SYS_FBDIV_INT, + .fbdiv_frac_reg =3D PLL_SYS_FBDIV_FRAC, + ), + + [RP1_PLL_AUDIO_CORE] =3D REGISTER_PLL_CORE(.name =3D "pll_audio_core", + .cs_reg =3D PLL_AUDIO_CS, + .pwr_reg =3D PLL_AUDIO_PWR, + .fbdiv_int_reg =3D PLL_AUDIO_FBDIV_INT, + .fbdiv_frac_reg =3D PLL_AUDIO_FBDIV_FRAC, + ), + + [RP1_PLL_VIDEO_CORE] =3D REGISTER_PLL_CORE(.name =3D "pll_video_core", + .cs_reg =3D PLL_VIDEO_CS, + .pwr_reg =3D PLL_VIDEO_PWR, + .fbdiv_int_reg =3D PLL_VIDEO_FBDIV_INT, + .fbdiv_frac_reg =3D PLL_VIDEO_FBDIV_FRAC, + ), + + [RP1_PLL_SYS] =3D REGISTER_PLL(.name =3D "pll_sys", + .source_pll =3D "pll_sys_core", + .ctrl_reg =3D PLL_SYS_PRIM, + .fc0_src =3D FC_NUM(0, 2), + ), + + [RP1_CLK_ETH_TSU] =3D REGISTER_CLK(.name =3D "clk_eth_tsu", + .parents =3D {"rp1-xosc"}, + .num_std_parents =3D 0, + .num_aux_parents =3D 1, + .ctrl_reg =3D CLK_ETH_TSU_CTRL, + .div_int_reg =3D CLK_ETH_TSU_DIV_INT, + .sel_reg =3D CLK_ETH_TSU_SEL, + .div_int_max =3D DIV_INT_8BIT_MAX, + .max_freq =3D 50 * MHz, + .fc0_src =3D FC_NUM(5, 7), + ), + + [RP1_CLK_SYS] =3D REGISTER_CLK(.name =3D "clk_sys", + .parents =3D {"rp1-xosc", "-", "pll_sys"}, + .num_std_parents =3D 3, + .num_aux_parents =3D 0, + .ctrl_reg =3D CLK_SYS_CTRL, + .div_int_reg =3D CLK_SYS_DIV_INT, + .sel_reg =3D CLK_SYS_SEL, + .div_int_max =3D DIV_INT_24BIT_MAX, + .max_freq =3D 200 * MHz, + .fc0_src =3D FC_NUM(0, 4), + .clk_src_mask =3D 0x3, + ), + + [RP1_PLL_SYS_PRI_PH] =3D REGISTER_PLL_PH(.name =3D "pll_sys_pri_ph", + .source_pll =3D "pll_sys", + .ph_reg =3D PLL_SYS_PRIM, + .fixed_divider =3D 2, + .phase =3D RP1_PLL_PHASE_0, + .fc0_src =3D FC_NUM(1, 2), + ), + + [RP1_PLL_SYS_SEC] =3D REGISTER_PLL_DIV(.name =3D "pll_sys_sec", + .source_pll =3D "pll_sys_core", + .ctrl_reg =3D PLL_SYS_SEC, + .fc0_src =3D FC_NUM(2, 2), + ), +}; + +static int rp1_clk_probe(struct platform_device *pdev) +{ + const size_t asize =3D ARRAY_SIZE(clk_desc_array); + const struct rp1_clk_desc *desc; + struct device *dev =3D &pdev->dev; + struct rp1_clockman *clockman; + struct clk *clk_xosc; + struct clk_hw **hws; + unsigned int i; + + clockman =3D devm_kzalloc(dev, struct_size(clockman, onecell.hws, asize), + GFP_KERNEL); + if (!clockman) + return -ENOMEM; + + spin_lock_init(&clockman->regs_lock); + clockman->dev =3D dev; + + clockman->regs =3D devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(clockman->regs)) + return PTR_ERR(clockman->regs); + + clk_xosc =3D devm_clk_get_enabled(dev, NULL); + if (IS_ERR(clk_xosc)) + return PTR_ERR(clk_xosc); + + clockman->hw_xosc =3D __clk_get_hw(clk_xosc); + clockman->onecell.num =3D asize; + hws =3D clockman->onecell.hws; + + for (i =3D 0; i < asize; i++) { + desc =3D &clk_desc_array[i]; + if (desc->clk_register && desc->data) { + hws[i] =3D desc->clk_register(clockman, desc->data); + if (IS_ERR_OR_NULL(hws[i])) + dev_err(dev, "Unable to register clock: %s\n", + clk_hw_get_name(hws[i])); + } + } + + platform_set_drvdata(pdev, clockman); + + return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, + &clockman->onecell); +} + +static const struct of_device_id rp1_clk_of_match[] =3D { + { .compatible =3D "raspberrypi,rp1-clocks" }, + {} +}; +MODULE_DEVICE_TABLE(of, rp1_clk_of_match); + +static struct platform_driver rp1_clk_driver =3D { + .driver =3D { + .name =3D "rp1-clk", + .of_match_table =3D rp1_clk_of_match, + }, + .probe =3D rp1_clk_probe, +}; + +static int __init rp1_clk_driver_init(void) +{ + return platform_driver_register(&rp1_clk_driver); +} +postcore_initcall(rp1_clk_driver_init); + +static void __exit rp1_clk_driver_exit(void) +{ + platform_driver_unregister(&rp1_clk_driver); +} +module_exit(rp1_clk_driver_exit); + +MODULE_AUTHOR("Naushir Patuck "); +MODULE_AUTHOR("Andrea della Porta "); +MODULE_DESCRIPTION("RP1 clock driver"); +MODULE_LICENSE("GPL"); --=20 2.35.3