From nobody Mon Nov 25 00:27:29 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 75DDC207A03; Tue, 12 Nov 2024 03:10:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731381018; cv=none; b=sgi8hL8W/paZHNlgJIq3IcD4pwD0zYiquHn5AtQmMHlnWcPn7bv/v8QBKWbLskMT+4APaj8njazFAq7duB4ZQJE6Hsxc9mXx71nlW8+BiqTspvkITJZU3Vl20BMRukufcoBuywRRg7nnLKPPeER6rUmvw14Nb1nhEOuHEwVUad8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731381018; c=relaxed/simple; bh=HJE0pA+74tCzliJNJXQ3Kg9jGHLW4xMEmX5HYgB7ZDs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HEF8MiGvOlhV2gHGGnhkyY/wdqx3G1HOePszF4wdy9VLlKajuWIxOSZotgjgDCAQxMwo2q4LlKazjvPCSfzWoAQU7k1X4qIr+r3QGhpmo1vHGDRwXROnF9CYxcRUZ1mvDfyY59d985QlJioF3oOMseeMoR4AXyfGfcgnR+MxhYc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nPr5HCxs; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nPr5HCxs" Received: by smtp.kernel.org (Postfix) with ESMTPS id 14BA4C4CED5; Tue, 12 Nov 2024 03:10:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731381018; bh=HJE0pA+74tCzliJNJXQ3Kg9jGHLW4xMEmX5HYgB7ZDs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=nPr5HCxsQ+y3O2cGZvkbzY9oCsyZx+/OhUxLcKKdd6C8bWSdgoV4Z4+CrEA+a2FV/ z6abTi6K4TJ9UEQaVa+LWxJ0zZv7jGDQL1qmYquZPTCatUvS1Q1+rVQFGV01xv/atZ UMVWu6wKpp13nPYtwdFow/92HzlfQJg3zRLo9CerKavxgRJL+JD9pbFvN7P8xDiPqI 2DtddizFxdgX055gbmpAwq/HHh/PIkUqQpshulH1wx0GZYCjoGff5HE4rzNWZasbie tFSfNV5+sBtu9A0GC1I91e2n1vTS4wx5pu3OiGrpmJukVslPH/a027W/HbegRhqJqR dOguo6uvF0iuQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04C10D41D44; Tue, 12 Nov 2024 03:10:18 +0000 (UTC) From: Xianwei Zhao via B4 Relay Date: Tue, 12 Nov 2024 11:10:14 +0800 Subject: [PATCH v6 1/3] dt-bindings: rtc: Add Amlogic A4 and A5 RTC Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241112-rtc-v6-1-a71b60d2f354@amlogic.com> References: <20241112-rtc-v6-0-a71b60d2f354@amlogic.com> In-Reply-To: <20241112-rtc-v6-0-a71b60d2f354@amlogic.com> To: Yiting Deng , Alexandre Belloni , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-amlogic@lists.infradead.org, linux-rtc@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Xianwei Zhao , Krzysztof Kozlowski X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1731381015; l=2146; i=xianwei.zhao@amlogic.com; s=20231208; h=from:subject:message-id; bh=SnfWHSIVk7X6HuExSpIoFJO0hQjcCpxtc5dtKXQKyis=; b=ARyRjGQBigrOHawyvYihlaq+QPQMdFvObHVumkJjjR07eDzlV0/z5lY9omeWLJxA3S1mMEIyz Agc312FFxZuB6Yr6GlmpxflTkX3QurKNjusSNwkwEqeGVMZ2xWP/W8R X-Developer-Key: i=xianwei.zhao@amlogic.com; a=ed25519; pk=o4fDH8ZXL6xQg5h17eNzRljf6pwZHWWjqcOSsj3dW24= X-Endpoint-Received: by B4 Relay for xianwei.zhao@amlogic.com/20231208 with auth_id=107 X-Original-From: Xianwei Zhao Reply-To: xianwei.zhao@amlogic.com From: Yiting Deng Add documentation describing the Amlogic A4(A113L2) and A5(A113X2) RTC. Signed-off-by: Yiting Deng Reviewed-by: Krzysztof Kozlowski Signed-off-by: Xianwei Zhao --- .../devicetree/bindings/rtc/amlogic,a4-rtc.yaml | 63 ++++++++++++++++++= ++++ 1 file changed, 63 insertions(+) diff --git a/Documentation/devicetree/bindings/rtc/amlogic,a4-rtc.yaml b/Do= cumentation/devicetree/bindings/rtc/amlogic,a4-rtc.yaml new file mode 100644 index 000000000000..5d3ac737abcb --- /dev/null +++ b/Documentation/devicetree/bindings/rtc/amlogic,a4-rtc.yaml @@ -0,0 +1,63 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +# Copyright (C) 2024 Amlogic, Inc. All rights reserved +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/rtc/amlogic,a4-rtc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Amlogic A4 and A5 RTC + +maintainers: + - Yiting Deng + - Xianwei Zhao + +allOf: + - $ref: rtc.yaml# + +properties: + compatible: + enum: + - amlogic,a4-rtc + - amlogic,a5-rtc + + reg: + maxItems: 1 + + clocks: + items: + - description: RTC clock source, available 24M or 32K crystal + oscillator source. when using 24M, need to divide 24M into 32K. + - description: RTC module accesses the clock of the apb bus. + + clock-names: + items: + - const: osc + - const: sys + + interrupts: + maxItems: 1 + +required: + - compatible + - reg + - clocks + - clock-names + - interrupts + +additionalProperties: false + +examples: + - | + #include + apb { + #address-cells =3D <2>; + #size-cells =3D <2>; + + rtc@8e600 { + compatible =3D "amlogic,a4-rtc"; + reg =3D <0x0 0x8e600 0x0 0x38>; + clocks =3D <&xtal_32k>, <&clkc_periphs 1>; + clock-names =3D "osc", "sys"; + interrupts =3D ; + }; + }; --=20 2.37.1 From nobody Mon Nov 25 00:27:29 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1B14207A17; Tue, 12 Nov 2024 03:10:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731381018; cv=none; b=mS8N5U74SKNCvh9FN1L0sPRgqiNC5r5+NCAJCPaDFl4Emhli0ztLbQ0sOUQQU97vxUaB54JGqUfJayeJG/YY62p//O8M9NBJltK/mzbzuqAE7IzK+FgJVdzhzjMv0pyjg0nw4BpmaQZD7Ap6FSUVSuUQ+LXnza+re9VgdGN8qyQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731381018; c=relaxed/simple; bh=+hKa5KKLkmi+z4aY9T/zGIvNRwQ8odXz1+tu9NIJoRc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VQMB4KFzgkAfgLUfBFqRtFIM/3qyE7Qhm40eCrENY7UmeocaKAzRgIthBc/y/hpTEhIghl0hXFMvkj7AU1LOmNyhQM0xvYROlkCf52ezze58Wp041ypvBaQeOry7pGVk4sEVpN7b+Iy4Hz94z6EW8MpqNXyHHx5N+KT750zfxHA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=joqF+1rk; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="joqF+1rk" Received: by smtp.kernel.org (Postfix) with ESMTPS id 26D36C4AF0B; Tue, 12 Nov 2024 03:10:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731381018; bh=+hKa5KKLkmi+z4aY9T/zGIvNRwQ8odXz1+tu9NIJoRc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=joqF+1rkKXCvNB6PBeQNFhz7PR/BjjOiI8bPN6jdmeqrjVLeO6Be2QvA9NxPDxteX HZCGK2u2ipOWXePldR6/JwcB4WBqDTdi9ftKSkSgBFJPFF09+H7K+80Tmaa3O6hadk ekN0r5OoUDSZdihPtEcG6WCjwfW/xppPEQMm6ASvQ22c3GN2n7nH/Aod69vyzfj2pg ut1Xb2fZoSAB+0+RNWSTn3AhTZzXBuyK1MHPwJIztTxZEAhMic0SVSP2cmg2j1tSMX VjvQDffT0alD0T2KD+QaGCet0ZnVCURLwmJRgIsAxlUKQQOpSzpc35KLEeppGIvAeD yyrHEPyqXavcw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13AB7D41D46; Tue, 12 Nov 2024 03:10:18 +0000 (UTC) From: Xianwei Zhao via B4 Relay Date: Tue, 12 Nov 2024 11:10:15 +0800 Subject: [PATCH v6 2/3] rtc: support for the Amlogic on-chip RTC Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241112-rtc-v6-2-a71b60d2f354@amlogic.com> References: <20241112-rtc-v6-0-a71b60d2f354@amlogic.com> In-Reply-To: <20241112-rtc-v6-0-a71b60d2f354@amlogic.com> To: Yiting Deng , Alexandre Belloni , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-amlogic@lists.infradead.org, linux-rtc@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Xianwei Zhao X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1731381015; l=15456; i=xianwei.zhao@amlogic.com; s=20231208; h=from:subject:message-id; bh=dTCOepBRzxg2g0M6eKzxPyfC8FTqTa9XLW6EyBbZhng=; b=tXQLOuDD2y8YFQ/gO84P/rcaeFDsXU5u20Hp64SFnXTxOcbG124jqc6WNYSjMlcOfzAeTL8d9 F0z1KG7bQhkDZDI46CXbnyunoiuELDVQ2KagkCvKg0kRCZ3yv0MhFtB X-Developer-Key: i=xianwei.zhao@amlogic.com; a=ed25519; pk=o4fDH8ZXL6xQg5h17eNzRljf6pwZHWWjqcOSsj3dW24= X-Endpoint-Received: by B4 Relay for xianwei.zhao@amlogic.com/20231208 with auth_id=107 X-Original-From: Xianwei Zhao Reply-To: xianwei.zhao@amlogic.com From: Yiting Deng This is the third amlogic driver. The RTC hardware of A4 SoC is different from the previous one. This RTC hardware includes a timing function and an alarm function. But the existing has only timing function, alarm function is using the system clock to implement a virtual alarm. Add the RTC driver to support it. Signed-off-by: Yiting Deng Signed-off-by: Xianwei Zhao --- drivers/rtc/Kconfig | 12 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-amlogic-a4.c | 474 +++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 487 insertions(+) diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 66eb1122248b..98bce0c42307 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -2070,4 +2070,16 @@ config RTC_DRV_SSD202D This driver can also be built as a module, if so, the module will be called "rtc-ssd20xd". =20 +config RTC_DRV_AMLOGIC_A4 + tristate "Amlogic RTC" + depends on ARCH_MESON || COMPILE_TEST + select REGMAP_MMIO + default y + help + If you say yes here you get support for the RTC block on the + Amlogic A113L2(A4) and A113X2(A5) SoCs. + + This driver can also be built as a module. If so, the module + will be called "rtc-amlogic-a4". + endif # RTC_CLASS diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index f62340ecc534..efeba9a2eaad 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -26,6 +26,7 @@ obj-$(CONFIG_RTC_DRV_ABB5ZES3) +=3D rtc-ab-b5ze-s3.o obj-$(CONFIG_RTC_DRV_ABEOZ9) +=3D rtc-ab-eoz9.o obj-$(CONFIG_RTC_DRV_ABX80X) +=3D rtc-abx80x.o obj-$(CONFIG_RTC_DRV_AC100) +=3D rtc-ac100.o +obj-$(CONFIG_RTC_DRV_AMLOGIC_A4) +=3D rtc-amlogic-a4.o obj-$(CONFIG_RTC_DRV_ARMADA38X) +=3D rtc-armada38x.o obj-$(CONFIG_RTC_DRV_AS3722) +=3D rtc-as3722.o obj-$(CONFIG_RTC_DRV_ASM9260) +=3D rtc-asm9260.o diff --git a/drivers/rtc/rtc-amlogic-a4.c b/drivers/rtc/rtc-amlogic-a4.c new file mode 100644 index 000000000000..9423dce4193d --- /dev/null +++ b/drivers/rtc/rtc-amlogic-a4.c @@ -0,0 +1,474 @@ +// SPDX-License-Identifier: (GPL-2.0-only OR MIT) +/* + * Copyright (C) 2024 Amlogic, Inc. All rights reserved + * Author: Yiting Deng + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* rtc oscillator rate */ +#define OSC_32K 32768 +#define OSC_24M 24000000 + +#define RTC_CTRL (0x0 << 2) /* Control RTC */ +#define RTC_ALRM0_EN BIT(0) +#define RTC_OSC_SEL BIT(8) +#define RTC_ENABLE BIT(12) + +#define RTC_COUNTER_REG (0x1 << 2) /* Program RTC counter initial value = */ + +#define RTC_ALARM0_REG (0x2 << 2) /* Program RTC alarm0 value */ + +#define RTC_SEC_ADJUST_REG (0x6 << 2) /* Control second-based timing adju= stment */ +#define RTC_MATCH_COUNTER GENMASK(18, 0) +#define RTC_SEC_ADJUST_CTRL GENMASK(20, 19) +#define RTC_ADJ_VALID BIT(23) + +#define RTC_INT_MASK (0x8 << 2) /* RTC interrupt mask */ +#define RTC_ALRM0_IRQ_MSK BIT(0) + +#define RTC_INT_CLR (0x9 << 2) /* Clear RTC interrupt */ +#define RTC_ALRM0_IRQ_CLR BIT(0) + +#define RTC_OSCIN_CTRL0 (0xa << 2) /* Control RTC clk from 24M */ +#define RTC_OSCIN_CTRL1 (0xb << 2) /* Control RTC clk from 24M */ +#define RTC_OSCIN_IN_EN BIT(31) +#define RTC_OSCIN_OUT_CFG GENMASK(29, 28) +#define RTC_OSCIN_OUT_N0M0 GENMASK(11, 0) +#define RTC_OSCIN_OUT_N1M1 GENMASK(23, 12) + +#define RTC_INT_STATUS (0xc << 2) /* RTC interrupt status */ +#define RTC_ALRM0_IRQ_STATUS BIT(0) + +#define RTC_REAL_TIME (0xd << 2) /* RTC time value */ + +#define RTC_OSCIN_OUT_32K_N0 0x2dc +#define RTC_OSCIN_OUT_32K_N1 0x2db +#define RTC_OSCIN_OUT_32K_M0 0x1 +#define RTC_OSCIN_OUT_32K_M1 0x2 + +#define RTC_SWALLOW_SECOND 0x2 +#define RTC_INSERT_SECOND 0x3 + +struct aml_rtc_config { + bool gray_stored; +}; + +struct aml_rtc_data { + struct regmap *map; + struct rtc_device *rtc_dev; + int irq; + struct clk *rtc_clk; + struct clk *sys_clk; + int rtc_enabled; + const struct aml_rtc_config *config; +}; + +static const struct regmap_config aml_rtc_regmap_config =3D { + .reg_bits =3D 32, + .val_bits =3D 32, + .reg_stride =3D 4, + .max_register =3D RTC_REAL_TIME, +}; + +static inline u32 gray_to_binary(u32 gray) +{ + u32 bcd =3D gray; + int size =3D sizeof(bcd) * 8; + int i; + + for (i =3D 0; (1 << i) < size; i++) + bcd ^=3D bcd >> (1 << i); + + return bcd; +} + +static inline u32 binary_to_gray(u32 bcd) +{ + return bcd ^ (bcd >> 1); +} + +static int aml_rtc_read_time(struct device *dev, struct rtc_time *tm) +{ + struct aml_rtc_data *rtc =3D dev_get_drvdata(dev); + u32 time_sec; + + /* if RTC disabled, read time failed */ + if (!rtc->rtc_enabled) { + dev_err(dev, "RTC disabled, read time failed\n"); + return -EINVAL; + } + + regmap_read(rtc->map, RTC_REAL_TIME, &time_sec); + if (rtc->config->gray_stored) + time_sec =3D gray_to_binary(time_sec); + rtc_time64_to_tm(time_sec, tm); + dev_dbg(dev, "%s: read time =3D %us\n", __func__, time_sec); + + return 0; +} + +static int aml_rtc_set_time(struct device *dev, struct rtc_time *tm) +{ + struct aml_rtc_data *rtc =3D dev_get_drvdata(dev); + u32 time_sec; + + /* if RTC disabled, first enable it */ + if (!rtc->rtc_enabled) { + regmap_write_bits(rtc->map, RTC_CTRL, RTC_ENABLE, RTC_ENABLE); + usleep_range(100, 200); + rtc->rtc_enabled =3D regmap_test_bits(rtc->map, RTC_CTRL, RTC_ENABLE); + if (!rtc->rtc_enabled) + return -EINVAL; + } + + time_sec =3D rtc_tm_to_time64(tm); + if (rtc->config->gray_stored) + time_sec =3D binary_to_gray(time_sec); + regmap_write(rtc->map, RTC_COUNTER_REG, time_sec); + dev_dbg(dev, "%s: set time =3D %us\n", __func__, time_sec); + + return 0; +} + +static int aml_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) +{ + struct aml_rtc_data *rtc =3D dev_get_drvdata(dev); + time64_t alarm_sec; + + /* if RTC disabled, set alarm failed */ + if (!rtc->rtc_enabled) { + dev_err(dev, "RTC disabled, set alarm failed\n"); + return -EINVAL; + } + + regmap_update_bits(rtc->map, RTC_CTRL, + RTC_ALRM0_EN, RTC_ALRM0_EN); + regmap_update_bits(rtc->map, RTC_INT_MASK, + RTC_ALRM0_IRQ_MSK, 0); + + alarm_sec =3D rtc_tm_to_time64(&alarm->time); + if (rtc->config->gray_stored) + alarm_sec =3D binary_to_gray(alarm_sec); + regmap_write(rtc->map, RTC_ALARM0_REG, alarm_sec); + + dev_dbg(dev, "%s: alarm->enabled=3D%d alarm_set=3D%llds\n", __func__, + alarm->enabled, alarm_sec); + + return 0; +} + +static int aml_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) +{ + struct aml_rtc_data *rtc =3D dev_get_drvdata(dev); + u32 alarm_sec; + int alarm_enable; + int alarm_mask; + + /* if RTC disabled, read alarm failed */ + if (!rtc->rtc_enabled) { + dev_err(dev, "RTC disabled, read alarm failed\n"); + return -EINVAL; + } + + regmap_read(rtc->map, RTC_ALARM0_REG, &alarm_sec); + if (rtc->config->gray_stored) + alarm_sec =3D gray_to_binary(alarm_sec); + rtc_time64_to_tm(alarm_sec, &alarm->time); + + alarm_enable =3D regmap_test_bits(rtc->map, RTC_CTRL, RTC_ALRM0_EN); + alarm_mask =3D regmap_test_bits(rtc->map, RTC_INT_MASK, RTC_ALRM0_IRQ_MSK= ); + alarm->enabled =3D (alarm_enable && !alarm_mask) ? 1 : 0; + dev_dbg(dev, "%s: alarm->enabled=3D%d alarm=3D%us\n", __func__, + alarm->enabled, alarm_sec); + + return 0; +} + +static int aml_rtc_read_offset(struct device *dev, long *offset) +{ + struct aml_rtc_data *rtc =3D dev_get_drvdata(dev); + u32 reg_val; + long val; + int sign, match_counter, enable; + + /* if RTC disabled, read offset failed */ + if (!rtc->rtc_enabled) { + dev_err(dev, "RTC disabled, read offset failed\n"); + return -EINVAL; + } + + regmap_read(rtc->map, RTC_SEC_ADJUST_REG, ®_val); + enable =3D FIELD_GET(RTC_ADJ_VALID, reg_val); + if (!enable) { + val =3D 0; + } else { + sign =3D FIELD_GET(RTC_SEC_ADJUST_CTRL, reg_val); + match_counter =3D FIELD_GET(RTC_MATCH_COUNTER, reg_val); + val =3D 1000000000 / (match_counter + 1); + if (sign =3D=3D RTC_SWALLOW_SECOND) + val =3D -val; + } + *offset =3D val; + + return 0; +} + +static int aml_rtc_set_offset(struct device *dev, long offset) +{ + struct aml_rtc_data *rtc =3D dev_get_drvdata(dev); + int sign =3D 0; + int match_counter =3D 0; + int enable =3D 0; + u32 reg_val; + + /* if RTC disabled, set offset failed */ + if (!rtc->rtc_enabled) { + dev_err(dev, "RTC disabled, set offset failed\n"); + return -EINVAL; + } + + if (offset) { + enable =3D 1; + sign =3D offset < 0 ? RTC_SWALLOW_SECOND : RTC_INSERT_SECOND; + match_counter =3D 1000000000 / abs(offset) - 1; + if (match_counter < 0 || match_counter > RTC_MATCH_COUNTER) + return -EINVAL; + } + + reg_val =3D FIELD_PREP(RTC_ADJ_VALID, enable) | + FIELD_PREP(RTC_SEC_ADJUST_CTRL, sign) | + FIELD_PREP(RTC_MATCH_COUNTER, match_counter); + regmap_write(rtc->map, RTC_SEC_ADJUST_REG, reg_val); + + return 0; +} + +static int aml_rtc_alarm_enable(struct device *dev, unsigned int enabled) +{ + struct aml_rtc_data *rtc =3D dev_get_drvdata(dev); + + if (enabled) { + regmap_update_bits(rtc->map, RTC_CTRL, + RTC_ALRM0_EN, RTC_ALRM0_EN); + regmap_update_bits(rtc->map, RTC_INT_MASK, + RTC_ALRM0_IRQ_MSK, 0); + } else { + regmap_update_bits(rtc->map, RTC_INT_MASK, + RTC_ALRM0_IRQ_MSK, RTC_ALRM0_IRQ_MSK); + regmap_update_bits(rtc->map, RTC_CTRL, + RTC_ALRM0_EN, 0); + } + + return 0; +} + +static const struct rtc_class_ops aml_rtc_ops =3D { + .read_time =3D aml_rtc_read_time, + .set_time =3D aml_rtc_set_time, + .read_alarm =3D aml_rtc_read_alarm, + .set_alarm =3D aml_rtc_set_alarm, + .alarm_irq_enable =3D aml_rtc_alarm_enable, + .read_offset =3D aml_rtc_read_offset, + .set_offset =3D aml_rtc_set_offset, +}; + +static irqreturn_t aml_rtc_handler(int irq, void *data) +{ + struct aml_rtc_data *rtc =3D (struct aml_rtc_data *)data; + + regmap_write(rtc->map, RTC_ALARM0_REG, 0); + regmap_write(rtc->map, RTC_INT_CLR, RTC_ALRM0_IRQ_STATUS); + + rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF); + + return IRQ_HANDLED; +} + +static void aml_rtc_init(struct aml_rtc_data *rtc) +{ + u32 reg_val =3D 0; + + rtc->rtc_enabled =3D regmap_test_bits(rtc->map, RTC_CTRL, RTC_ENABLE); + if (!rtc->rtc_enabled) { + if (clk_get_rate(rtc->rtc_clk) =3D=3D OSC_24M) { + /* select 24M oscillator */ + regmap_write_bits(rtc->map, RTC_CTRL, RTC_OSC_SEL, RTC_OSC_SEL); + + /* + * Set RTC oscillator to freq_out to freq_in/((N0*M0+N1*M1)/(M0+M1)) + * Enable clock_in gate of oscillator 24MHz + * Set N0 to 733, N1 to 732 + */ + reg_val =3D FIELD_PREP(RTC_OSCIN_IN_EN, 1) + | FIELD_PREP(RTC_OSCIN_OUT_CFG, 1) + | FIELD_PREP(RTC_OSCIN_OUT_N0M0, RTC_OSCIN_OUT_32K_N0) + | FIELD_PREP(RTC_OSCIN_OUT_N1M1, RTC_OSCIN_OUT_32K_N1); + regmap_write_bits(rtc->map, RTC_OSCIN_CTRL0, RTC_OSCIN_IN_EN + | RTC_OSCIN_OUT_CFG | RTC_OSCIN_OUT_N0M0 + | RTC_OSCIN_OUT_N1M1, reg_val); + + /* Set M0 to 2, M1 to 3, so freq_out =3D 32768 Hz*/ + reg_val =3D FIELD_PREP(RTC_OSCIN_OUT_N0M0, RTC_OSCIN_OUT_32K_M0) + | FIELD_PREP(RTC_OSCIN_OUT_N1M1, RTC_OSCIN_OUT_32K_M1); + regmap_write_bits(rtc->map, RTC_OSCIN_CTRL1, RTC_OSCIN_OUT_N0M0 + | RTC_OSCIN_OUT_N1M1, reg_val); + } else { + /* select 32K oscillator */ + regmap_write_bits(rtc->map, RTC_CTRL, RTC_OSC_SEL, 0); + } + } + regmap_write_bits(rtc->map, RTC_INT_MASK, + RTC_ALRM0_IRQ_MSK, RTC_ALRM0_IRQ_MSK); + regmap_write_bits(rtc->map, RTC_CTRL, RTC_ALRM0_EN, 0); +} + +static int aml_rtc_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct aml_rtc_data *rtc; + void __iomem *base; + int ret =3D 0; + + rtc =3D devm_kzalloc(dev, sizeof(*rtc), GFP_KERNEL); + if (!rtc) + return -ENOMEM; + + rtc->config =3D of_device_get_match_data(dev); + if (!rtc->config) + return -ENODEV; + + base =3D devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(base)) + return dev_err_probe(dev, PTR_ERR(base), "resource ioremap failed\n"); + + rtc->map =3D devm_regmap_init_mmio(dev, base, &aml_rtc_regmap_config); + if (IS_ERR(rtc->map)) + return dev_err_probe(dev, PTR_ERR(rtc->map), "regmap init failed\n"); + + rtc->irq =3D platform_get_irq(pdev, 0); + if (rtc->irq < 0) + return rtc->irq; + + rtc->rtc_clk =3D devm_clk_get(dev, "osc"); + if (IS_ERR(rtc->rtc_clk)) + return dev_err_probe(dev, PTR_ERR(rtc->rtc_clk), + "failed to find rtc clock\n"); + if (clk_get_rate(rtc->rtc_clk) !=3D OSC_32K && clk_get_rate(rtc->rtc_clk)= !=3D OSC_24M) + return dev_err_probe(dev, -EINVAL, "Invalid clock configuration\n"); + + rtc->sys_clk =3D devm_clk_get_enabled(dev, "sys"); + if (IS_ERR(rtc->sys_clk)) + return dev_err_probe(dev, PTR_ERR(rtc->sys_clk), + "failed to get_enable rtc sys clk\n"); + aml_rtc_init(rtc); + + device_init_wakeup(dev, 1); + platform_set_drvdata(pdev, rtc); + + rtc->rtc_dev =3D devm_rtc_allocate_device(dev); + if (IS_ERR(rtc->rtc_dev)) { + ret =3D PTR_ERR(rtc->rtc_dev); + goto err_clk; + } + + ret =3D devm_request_irq(dev, rtc->irq, aml_rtc_handler, + IRQF_ONESHOT, "aml-rtc alarm", rtc); + if (ret) { + dev_err_probe(dev, ret, "IRQ%d request failed, ret =3D %d\n", + rtc->irq, ret); + goto err_clk; + } + + rtc->rtc_dev->ops =3D &aml_rtc_ops; + rtc->rtc_dev->range_min =3D 0; + rtc->rtc_dev->range_max =3D U32_MAX; + + ret =3D devm_rtc_register_device(rtc->rtc_dev); + if (ret) { + dev_err_probe(&pdev->dev, ret, "Failed to register RTC device: %d\n", re= t); + goto err_clk; + } + + return 0; +err_clk: + clk_disable_unprepare(rtc->sys_clk); + device_init_wakeup(dev, 0); + + return ret; +} + +#ifdef CONFIG_PM_SLEEP +static int aml_rtc_suspend(struct device *dev) +{ + struct aml_rtc_data *rtc =3D dev_get_drvdata(dev); + + if (device_may_wakeup(dev)) + enable_irq_wake(rtc->irq); + + return 0; +} + +static int aml_rtc_resume(struct device *dev) +{ + struct aml_rtc_data *rtc =3D dev_get_drvdata(dev); + + if (device_may_wakeup(dev)) + disable_irq_wake(rtc->irq); + + return 0; +} +#endif + +static SIMPLE_DEV_PM_OPS(aml_rtc_pm_ops, + aml_rtc_suspend, aml_rtc_resume); + +static void aml_rtc_remove(struct platform_device *pdev) +{ + struct aml_rtc_data *rtc =3D dev_get_drvdata(&pdev->dev); + + clk_disable_unprepare(rtc->sys_clk); + device_init_wakeup(&pdev->dev, 0); +} + +static const struct aml_rtc_config a5_rtc_config =3D { +}; + +static const struct aml_rtc_config a4_rtc_config =3D { + .gray_stored =3D true, +}; + +static const struct of_device_id aml_rtc_device_id[] =3D { + { + .compatible =3D "amlogic,a4-rtc", + .data =3D &a4_rtc_config, + }, + { + .compatible =3D "amlogic,a5-rtc", + .data =3D &a5_rtc_config, + }, +}; +MODULE_DEVICE_TABLE(of, aml_rtc_device_id); + +static struct platform_driver aml_rtc_driver =3D { + .probe =3D aml_rtc_probe, + .remove =3D aml_rtc_remove, + .driver =3D { + .name =3D "aml-rtc", + .pm =3D &aml_rtc_pm_ops, + .of_match_table =3D aml_rtc_device_id, + }, +}; + +module_platform_driver(aml_rtc_driver); +MODULE_DESCRIPTION("Amlogic RTC driver"); +MODULE_AUTHOR("Yiting Deng "); +MODULE_LICENSE("GPL"); --=20 2.37.1 From nobody Mon Nov 25 00:27:29 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1A89207A14; Tue, 12 Nov 2024 03:10:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731381018; cv=none; b=ChxCXJNTFZ8lKUqri86JPDWGwQgVsPYIRPL+i/cLhv2wBSGwIpVufe78RbzRxgiSkKnppVSFuUS4NrxRZIeZ6V+MVzkUUOI/ApYTnifDJYgh1O2rEEa2/deFaXDUOqQg/nCtkEq8fwkFn7HsITZZDJ42vdMxmmWbyND8nhTEWnY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731381018; c=relaxed/simple; bh=SVIUbQzxLofeJMLFPpkqoKqNt1gn+vEU3tlnN8KXr+c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=d5LkHiyrrUBDY/zMo1dV8Oj9/GfxRwBcW2eRnNCSmiBMas4GuedcYySxDxg5MHLbNtZfN64tB3fam3Wai//LGqrqXMRa9HORB4+8RviVtU8FegWW5cBWpTpEhnIdP7+9zcOaEWZNA5P1gVKenYb3gtrvEWCQWN4yQw1YgCLwndQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=f/bIjYvF; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="f/bIjYvF" Received: by smtp.kernel.org (Postfix) with ESMTPS id 30992C4AF10; Tue, 12 Nov 2024 03:10:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731381018; bh=SVIUbQzxLofeJMLFPpkqoKqNt1gn+vEU3tlnN8KXr+c=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=f/bIjYvF/nZ6o8Fps/kS0C6UFlZGwQfhJQJFusR+oPfENGgTR4OBf3xHRqR2/tl3g 8TdiuyACyJFkOGzPFS7kZuAnisEELWIVeh93VhQ2CWE+P5uMzCkwekrs3qOLPbDLv3 pWbA/mhzb47wehubpIGLLwhpXTwM5C3VQ/fLCUNC/nYwqfeh3UWRRyDaS6MWGaegaB IKRESnm7mYDZ8kOTjZBo7r0MMPYX6zS+JjREpv2yjPj1WVyI1fwO2buFwArKPYRgT6 419mWbNNm5cCs5fq+JxlipvX5MSPlv6t6JpmflyFSDoOlJ4V7GvYuHKkVTujELmKl1 Jdl/+8uce7Brg== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 23621D41D4A; Tue, 12 Nov 2024 03:10:18 +0000 (UTC) From: Xianwei Zhao via B4 Relay Date: Tue, 12 Nov 2024 11:10:16 +0800 Subject: [PATCH v6 3/3] MAINTAINERS: Add an entry for Amlogic RTC driver Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241112-rtc-v6-3-a71b60d2f354@amlogic.com> References: <20241112-rtc-v6-0-a71b60d2f354@amlogic.com> In-Reply-To: <20241112-rtc-v6-0-a71b60d2f354@amlogic.com> To: Yiting Deng , Alexandre Belloni , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-amlogic@lists.infradead.org, linux-rtc@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Xianwei Zhao X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1731381015; l=931; i=xianwei.zhao@amlogic.com; s=20231208; h=from:subject:message-id; bh=Gn6Ya+wBxnc2VJ4I38RsdaXoWkK7Fe7p7YGOFyMiaMc=; b=+Dw8a0HzDmMQJH0sX/GRskjZ9a1Mnzshf4iUIbwZucRCh5cCAlPCMBtlUipbZEhPVHUJ3xxnW 2H0vSdoXjj+CyrfUG7XGEHEV5JRpAo+xjxFQauP3Z7uASm7Pr1sUd+i X-Developer-Key: i=xianwei.zhao@amlogic.com; a=ed25519; pk=o4fDH8ZXL6xQg5h17eNzRljf6pwZHWWjqcOSsj3dW24= X-Endpoint-Received: by B4 Relay for xianwei.zhao@amlogic.com/20231208 with auth_id=107 X-Original-From: Xianwei Zhao Reply-To: xianwei.zhao@amlogic.com From: Yiting Deng Add Amlogic RTC entry to MAINTAINERS to clarify the maintainers. Signed-off-by: Yiting Deng Signed-off-by: Xianwei Zhao --- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index c27f3190737f..86675e03888c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1209,6 +1209,14 @@ F: Documentation/devicetree/bindings/perf/amlogic,g1= 2-ddr-pmu.yaml F: drivers/perf/amlogic/ F: include/soc/amlogic/ =20 +AMLOGIC RTC DRIVER +M: Yiting Deng +M: Xianwei Zhao +L: linux-amlogic@lists.infradead.org +S: Maintained +F: Documentation/devicetree/bindings/rtc/amlogic,a4-rtc.yaml +F: drivers/rtc/rtc-amlogic-a4.c + AMPHENOL CHIPCAP 2 HUMIDITY-TEMPERATURE IIO DRIVER M: Javier Carrasco L: linux-hwmon@vger.kernel.org --=20 2.37.1