Message ID | 20221209111313.2701-1-fnkl.kernel@gmail.com |
---|---|
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp712071wrr; Fri, 9 Dec 2022 03:19:08 -0800 (PST) X-Google-Smtp-Source: AA0mqf7rGDt3ivGOok/gIgNeseV5QZ6uObKFYnwDjDN+erZITHnp5WR1eCeHtck7qGVlCvG2KTrm X-Received: by 2002:a05:6a20:e619:b0:9f:2d6a:dff4 with SMTP id my25-20020a056a20e61900b0009f2d6adff4mr7568553pzb.13.1670584748484; Fri, 09 Dec 2022 03:19:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670584748; cv=none; d=google.com; s=arc-20160816; b=Jg5eiB2/CEhfmdWqQHgeDSOw5uBRUUzZg9On8JGwQp77yIh6HaZeetWjAX8zhV5yEY QBqJxa3AOXcMs6xr8alyxk+cFT182mgOUkMFVptm8rvu7Op0bD6fNmVd+yPnjrrLBizv Iaa/uE24Plp4+2Mo+j2hyBkhXrLCH+KuaJVlssyuXaledALtJpIC2he/ChtgBhXEWIlN XIIkStJw4Mig6MA/8HYmVQp4YmDiH0n7L1WucgKQp2vFo2H2Iv54UoM6FbgFGtNxE/sr DR4vceu3AEZB9+xOk67LHRhvmJnP4YGAJm3lixdHwttNAnYsqJ/h5RclYYK5a0ICcvy3 r1Nw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=aptsriDL75Ig6gjid308/Jf1kFuZ73/LpGcnByUwftU=; b=gO221MxEPko3mX/lGxZ90bMlOoI+a5V3KbLFWjl7zmOJqCzSsC8dc2p8hh2OhpF5b8 h0+c1/MlNXKCk+DC9vWIv7fzv/5tiY9ADzOX3N7lBi4ljoAVRw2QJH7tZBbQrQW53M0k E/Gk0sW8PQHLQf9zpQNy3MkaQxk+VoOhW9DiZvxcABbKFVPayJneHDtfBEvEEX3luxQL UbO1r9Q6hpfHwJ03Go3UdmR8w8UHyGqOGu5m70P8Fcjpwhutrf1cvq8UiDYx2P9FYdyl MTCFjyJGHGq9CoHt8izJrQNREwn6Lo83Cja8ZZBSDVryyIHbF7TCJi6Gju3b2jShVgdJ 9tKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Tc35s68O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u36-20020a634724000000b004600f27528fsi1271635pga.175.2022.12.09.03.18.54; Fri, 09 Dec 2022 03:19:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Tc35s68O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229521AbiLILOp (ORCPT <rfc822;sophiezhao968@gmail.com> + 99 others); Fri, 9 Dec 2022 06:14:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229488AbiLILOm (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 9 Dec 2022 06:14:42 -0500 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F02B31F9DF; Fri, 9 Dec 2022 03:14:41 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id m4-20020a05600c3b0400b003d1cb516ce0so5510279wms.4; Fri, 09 Dec 2022 03:14:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=aptsriDL75Ig6gjid308/Jf1kFuZ73/LpGcnByUwftU=; b=Tc35s68OF5v/x5JicHtvMLmwSH8H8p5eYTwOaTIPhnSP8L3O4gM+ZWgXAqgbmMWT6I +LQ3awlxrnDCU1IkqCWUKc5co5vIituYd4IaOGiXt2GXLJLF4mT3OpErbIfjjbX4jwqa ukD5OwKhFtqys/G5DSP1uCXyAT6H6lwqWCEuB2qyIvbM9AMuCKuq+Y1UhNR7OPrGOYzq 4X3pS+Nns9YEJTxQAsjiXjvu4R4K6db8McQ+uzd641csHEpgzAIr030PogKu+8MuvaKC JpnehUiyhcRAA1fTEQIyg2/Krxw9Wn17It8sjZ1tvIr1Wp55h93x9w63jvqWi5usu188 CEuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=aptsriDL75Ig6gjid308/Jf1kFuZ73/LpGcnByUwftU=; b=jfKjZXgpX96UqSNvSoD/FU8KOrw14CnVAe1Eey7Z0oFykHqsG/Gm2z82cL8RPaYUui PbVp60xgSRPIM8L948Jszoq0MsargCDf7Q2NjJQx6jOFkLXo6nnsVOYj2Mh341cFpQO2 01bF/j12l589mZiSB379NghIFIPyp3xCDEjmBpdiCzVDNQMhUTEmAaq6r9mM2q9oSdBT AcP2L5oXdUD3rJ9iuLz37G6MDeTS2cBjfoLouBoyj3h7PSKoqdjbDHNJZAp/qk2wgW7M O080aPC/q6qjir58KiYrE2fcLFkzz8EpBWV2QgdC809f8ZBwZBaqNfb4Zk8mQaigWPsV gTsQ== X-Gm-Message-State: ANoB5pkTdHCUYAgcUhq7kEGtcv+asC2aEF2GbygKkHpYr6Q+qNPQO646 ox4UfQX89TpwjdVIQT7nP64= X-Received: by 2002:a05:600c:4e54:b0:3cf:51cf:91c with SMTP id e20-20020a05600c4e5400b003cf51cf091cmr4503087wmq.11.1670584480443; Fri, 09 Dec 2022 03:14:40 -0800 (PST) Received: from localhost ([217.131.81.52]) by smtp.gmail.com with UTF8SMTPSA id bg9-20020a05600c3c8900b003c6f3f6675bsm9149366wmb.26.2022.12.09.03.14.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 09 Dec 2022 03:14:39 -0800 (PST) From: Sasha Finkelstein <fnkl.kernel@gmail.com> To: thierry.reding@gmail.com, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org Cc: marcan@marcan.st, sven@svenpeter.dev, alyssa@rosenzweig.io, asahi@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Sasha Finkelstein <fnkl.kernel@gmail.com> Subject: [PATCH v4 0/4] PWM and keyboard backlight driver for ARM Macs Date: Fri, 9 Dec 2022 14:13:09 +0300 Message-Id: <20221209111313.2701-1-fnkl.kernel@gmail.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751735072838943204?= X-GMAIL-MSGID: =?utf-8?q?1751735072838943204?= |
Series |
PWM and keyboard backlight driver for ARM Macs
|
|
Message
Sasha Finkelstein
Dec. 9, 2022, 11:13 a.m. UTC
Hi, This is the v4 of the patch series to add PWM and keyboard backlight driver for ARM macs. Changes in v1: Addressing the review comments. Changes in v2: Added the reviewed-by and acked-by tags. Addressing a review comment. Changes in v3: Addressing the review comments. v1: https://www.spinics.net/lists/linux-pwm/msg19500.html v2: https://www.spinics.net/lists/linux-pwm/msg19562.html v3: https://www.spinics.net/lists/linux-pwm/msg19901.html Sasha Finkelstein (4): dt-bindings: pwm: Add Apple PWM controller pwm: Add Apple PWM controller arm64: dts: apple: t8103: Add PWM controller MAINTAINERS: Add entries for Apple PWM driver .../bindings/pwm/apple,s5l-fpwm.yaml | 51 ++++++ MAINTAINERS | 2 + arch/arm64/boot/dts/apple/t8103-j293.dts | 19 +++ arch/arm64/boot/dts/apple/t8103-j313.dts | 19 +++ arch/arm64/boot/dts/apple/t8103.dtsi | 9 ++ drivers/pwm/Kconfig | 12 ++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-apple.c | 150 ++++++++++++++++++ 8 files changed, 263 insertions(+) create mode 100644 Documentation/devicetree/bindings/pwm/apple,s5l-fpwm.yaml create mode 100644 drivers/pwm/pwm-apple.c
Comments
Hello, over all the driver looks good. Just a few smaller issues below. I wonder if it's a good idea to call this driver "apple". SoC vendors seem to reinvent their peripherals (or buy them somewhere else) for their different generations of processors. Maybe call it "apple-s5l" already today and not only when the next generation SoC appears? (I don't feel strong here, if you want to delay that renaming until there is an incompatible SoC that's fine for me.) On Fri, Dec 09, 2022 at 02:13:11PM +0300, Sasha Finkelstein wrote: > diff --git a/drivers/pwm/pwm-apple.c b/drivers/pwm/pwm-apple.c > new file mode 100644 > index 000000000000..a85fecb20105 > --- /dev/null > +++ b/drivers/pwm/pwm-apple.c > @@ -0,0 +1,150 @@ > +// SPDX-License-Identifier: GPL-2.0 OR MIT > +/* > + * Driver for the Apple SoC PWM controller > + * > + * Copyright The Asahi Linux Contributors > + * > + * Limitations: > + * - The writes to cycle registers are shadowed until a write to > + * the control register. > + */ > + > +#include <linux/module.h> > +#include <linux/of.h> > +#include <linux/of_device.h> > +#include <linux/platform_device.h> > +#include <linux/pwm.h> > +#include <linux/io.h> > +#include <linux/clk.h> > +#include <linux/pm_runtime.h> > +#include <linux/math64.h> I didn't test, but I think you don't need pm_runtime.h here. Also maybe the of headers are not needed? > +#define APPLE_PWM_CONTROL 0x00 > +#define APPLE_PWM_ON_CYCLES 0x1c > +#define APPLE_PWM_OFF_CYCLES 0x18 > + > +#define APPLE_CTRL_ENABLE BIT(0) > +#define APPLE_CTRL_MODE BIT(2) > +#define APPLE_CTRL_UPDATE BIT(5) > +#define APPLE_CTRL_TRIGGER BIT(9) > +#define APPLE_CTRL_INVERT BIT(10) > +#define APPLE_CTRL_OUTPUT_ENABLE BIT(14) Would be nice if the register prefix would match the register name. That is please either rename APPLE_PWM_CONTROL to APPLE_PWM_CTRL or use APPLE_PWM_CONTROL as prefix for the bit fields in that register. > + > +struct apple_pwm { > + struct pwm_chip chip; > + void __iomem *base; > + u64 clkrate; > +}; > + > +static inline struct apple_pwm *to_apple_pwm(struct pwm_chip *chip) > +{ > + return container_of(chip, struct apple_pwm, chip); > +} > + > +static int apple_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, > + const struct pwm_state *state) > +{ > + struct apple_pwm *fpwm; > + u64 on_cycles, off_cycles; The declaration can move into the if block below. > + > + fpwm = to_apple_pwm(chip); > + if (state->enabled) { > + on_cycles = mul_u64_u64_div_u64(fpwm->clkrate, > + state->duty_cycle, NSEC_PER_SEC); > + if (on_cycles > 0xFFFFFFFF) > + return -ERANGE; > + > + off_cycles = mul_u64_u64_div_u64(fpwm->clkrate, > + state->period, NSEC_PER_SEC) - on_cycles; > + if (off_cycles > 0xFFFFFFFF) > + return -ERANGE; > + > + writel(on_cycles, fpwm->base + APPLE_PWM_ON_CYCLES); > + writel(off_cycles, fpwm->base + APPLE_PWM_OFF_CYCLES); > + writel(APPLE_CTRL_ENABLE | APPLE_CTRL_OUTPUT_ENABLE | APPLE_CTRL_UPDATE, > + fpwm->base + APPLE_PWM_CONTROL); > + } else { > + writel(0, fpwm->base + APPLE_PWM_CONTROL); > + } > + return 0; Assuming clkrate = 24000000, I wonder what happens if (duty_cycle and) period are so small that on_cycles and off_cycles are both zero. How does the hardware behave in this case? > +} > + > +static void apple_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, > + struct pwm_state *state) > +{ > + struct apple_pwm *fpwm; > + u32 on_cycles, off_cycles, ctrl; > + > + fpwm = to_apple_pwm(chip); > + > + ctrl = readl(fpwm->base + APPLE_PWM_CONTROL); > + on_cycles = readl(fpwm->base + APPLE_PWM_ON_CYCLES); > + off_cycles = readl(fpwm->base + APPLE_PWM_OFF_CYCLES); > + > + state->enabled = (ctrl & APPLE_CTRL_ENABLE) && (ctrl & APPLE_CTRL_OUTPUT_ENABLE); > + state->polarity = PWM_POLARITY_NORMAL; > + state->duty_cycle = mul_u64_u64_div_u64(on_cycles, NSEC_PER_SEC, fpwm->clkrate); > + state->period = mul_u64_u64_div_u64(off_cycles + on_cycles, > + NSEC_PER_SEC, fpwm->clkrate); Wrong rounding direction, you need to round up. Did you test with PWM_DEBUG on? This should point out the more obvious cases. Assuming clkrate = 24000000 for example setting .duty_cycle = 3 should trigger a warning. Unfortunately there is no variant of mul_u64_u64_div_u64 that rounds up, maybe we need to introduce one. > +} Best regards Uwe
On Fri, Dec 09, 2022 at 02:13:11PM +0300, Sasha Finkelstein wrote: > Adds the Apple PWM controller driver. > > Signed-off-by: Sasha Finkelstein <fnkl.kernel@gmail.com> > Acked-by: Sven Peter <sven@svenpeter.dev> > --- > drivers/pwm/Kconfig | 12 ++++ > drivers/pwm/Makefile | 1 + > drivers/pwm/pwm-apple.c | 150 ++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 163 insertions(+) > create mode 100644 drivers/pwm/pwm-apple.c > > diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig > index 60d13a949bc5..c3be11468414 100644 > --- a/drivers/pwm/Kconfig > +++ b/drivers/pwm/Kconfig > @@ -51,6 +51,18 @@ config PWM_AB8500 > To compile this driver as a module, choose M here: the module > will be called pwm-ab8500. > > +config PWM_APPLE > + tristate "Apple SoC PWM support" > + depends on ARCH_APPLE || COMPILE_TEST > + help > + Generic PWM framework driver for PWM controller present on > + Apple SoCs > + > + Say Y here if you have an ARM Apple laptop, otherwise say N > + > + To compile this driver as a module, choose M here: the module > + will be called pwm-apple. > + > config PWM_ATMEL > tristate "Atmel PWM support" > depends on ARCH_AT91 || COMPILE_TEST > diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile > index 7bf1a29f02b8..19899b912e00 100644 > --- a/drivers/pwm/Makefile > +++ b/drivers/pwm/Makefile > @@ -2,6 +2,7 @@ > obj-$(CONFIG_PWM) += core.o > obj-$(CONFIG_PWM_SYSFS) += sysfs.o > obj-$(CONFIG_PWM_AB8500) += pwm-ab8500.o > +obj-$(CONFIG_PWM_APPLE) += pwm-apple.o > obj-$(CONFIG_PWM_ATMEL) += pwm-atmel.o > obj-$(CONFIG_PWM_ATMEL_HLCDC_PWM) += pwm-atmel-hlcdc.o > obj-$(CONFIG_PWM_ATMEL_TCB) += pwm-atmel-tcb.o > diff --git a/drivers/pwm/pwm-apple.c b/drivers/pwm/pwm-apple.c > new file mode 100644 > index 000000000000..a85fecb20105 > --- /dev/null > +++ b/drivers/pwm/pwm-apple.c > @@ -0,0 +1,150 @@ > +// SPDX-License-Identifier: GPL-2.0 OR MIT > +/* > + * Driver for the Apple SoC PWM controller > + * > + * Copyright The Asahi Linux Contributors > + * > + * Limitations: > + * - The writes to cycle registers are shadowed until a write to > + * the control register. > + */ > + > +#include <linux/module.h> > +#include <linux/of.h> > +#include <linux/of_device.h> > +#include <linux/platform_device.h> > +#include <linux/pwm.h> > +#include <linux/io.h> > +#include <linux/clk.h> > +#include <linux/pm_runtime.h> > +#include <linux/math64.h> > + > +#define APPLE_PWM_CONTROL 0x00 > +#define APPLE_PWM_ON_CYCLES 0x1c > +#define APPLE_PWM_OFF_CYCLES 0x18 > + > +#define APPLE_CTRL_ENABLE BIT(0) > +#define APPLE_CTRL_MODE BIT(2) > +#define APPLE_CTRL_UPDATE BIT(5) > +#define APPLE_CTRL_TRIGGER BIT(9) > +#define APPLE_CTRL_INVERT BIT(10) > +#define APPLE_CTRL_OUTPUT_ENABLE BIT(14) > + > +struct apple_pwm { > + struct pwm_chip chip; > + void __iomem *base; > + u64 clkrate; > +}; > + > +static inline struct apple_pwm *to_apple_pwm(struct pwm_chip *chip) > +{ > + return container_of(chip, struct apple_pwm, chip); > +} > + > +static int apple_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, > + const struct pwm_state *state) > +{ > + struct apple_pwm *fpwm; > + u64 on_cycles, off_cycles; > + polarity handling is missing here and in .get_state(). I assume you want: if (state->polarity == PWM_POLARITY_INVERSED) return -EINVAL; here and state->polarity = PWM_POLARITY_NORMAL; in .get_state(). > + fpwm = to_apple_pwm(chip); > + if (state->enabled) { > + on_cycles = mul_u64_u64_div_u64(fpwm->clkrate, > + state->duty_cycle, NSEC_PER_SEC); > + if (on_cycles > 0xFFFFFFFF) > + return -ERANGE; > + > + off_cycles = mul_u64_u64_div_u64(fpwm->clkrate, > + state->period, NSEC_PER_SEC) - on_cycles; > + if (off_cycles > 0xFFFFFFFF) > + return -ERANGE; > + > + writel(on_cycles, fpwm->base + APPLE_PWM_ON_CYCLES); > + writel(off_cycles, fpwm->base + APPLE_PWM_OFF_CYCLES); > + writel(APPLE_CTRL_ENABLE | APPLE_CTRL_OUTPUT_ENABLE | APPLE_CTRL_UPDATE, > + fpwm->base + APPLE_PWM_CONTROL); > + } else { > + writel(0, fpwm->base + APPLE_PWM_CONTROL); > + } > + return 0; > +} Best regards Uwe
On 19/12/2022 22.06, Uwe Kleine-König wrote: > Hello, > > over all the driver looks good. Just a few smaller issues below. > > I wonder if it's a good idea to call this driver "apple". SoC vendors > seem to reinvent their peripherals (or buy them somewhere else) for > their different generations of processors. Maybe call it "apple-s5l" > already today and not only when the next generation SoC appears? > (I don't feel strong here, if you want to delay that renaming until > there is an incompatible SoC that's fine for me.) Well... considering s5l refers to the s5l8920x, the SoC used in the iPhone 3GS released in 2009, and here we are with the M1 13 years later and it's still the same, I think we're doing pretty well here. (Apple doesn't reinvent their peripherals nearly as often as other vendors). - Hector