Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=46.4.11.11 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796413150; b=gc+2omJh5/6CoJO6QqzGSfBtGLE9Z5FcYquSataImcNbmw3jUlG+14ZSL27ikxNvFsPO oktde82AaG0lSzaQO+01i+cQKH5mZzBEFcrRCSb4b5XYE/h72uH+5bAi+Za0pGqXgH9ZC ZAUjf5a81sygfAa4c3WJ+OadPN6/ZDMeGE09l/+zelc+OfakMRI+TPGbLABVuYs1ZfxYZ W9ALKCqo32rS4CtEijHdTQxu9vF9gCwxJNExOOSLQpfvl3RCt/E8gF0Rzi9NPsv6BEqgu RPa64ahmsbojORIUKfcIUKaQMwKaSnNmt185wJyea1nM5Q9Qosky2eTwvntdPVCvWBQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796413150; h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-Id:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=9ddDCdUiBUfiz3cHnpmvMxQEUDJIDFYkEYcuKnphp1U=; b=Is+PJ9n+Qe/hIGheMwPYhRvvvxnA9WhrVFZFZae6DaaC9MZeQuVeLvcvaT+IrQm42TIE 5rkpS/+MmI5b5u4Ns1mO2GkQvHFLa34m6XUXikeEixVe5Qxtl1NT2v27b5P4TtBoJO+fr Rb0OL6FKXGDn3tXcSbC9JDJ5AAm/gYorXVuDcVyt74l2Ql0EUbAJCSpmZUXXED1URLOt8 r5Ct8JGdNqLvmOJvHQqE0MleeaFJpunzJmj7MkL5NiG9G/jbQZj7jteWZV44TLdTD9o9C pSsl+3fFPTtbjoW2VJ3mfQ8LRViCcmTs1i7kTIOHCjcuL6WbxPP1+Wl8mdWH85jCUFQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=phrozen.org; spf=pass smtp.mailfrom=phrozen.org; arc=none smtp.remote-ip=46.4.11.11 Received: from nbd.name (nbd.name [46.4.11.11]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 47320606A337; Fri, 4 Dec 2026 19:39:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 47320606A337 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=phrozen.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=phrozen.org Received: from [2a04:4540:1400:b6fc:7b90:6b35:724f:c910] (helo=bertha10..) by ds12 with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1xZZ7Z-00Cjpq-0s; Fri, 04 Dec 2026 20:39:05 +0100 From: John Crispin To: Lorenzo Bianconi , Lee Jones , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= Cc: Christian Marangi , devicetree@vger.kernel.org, lei.dong@airoha.com, Linqi.zhang@airoha.com, linux-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org, linux-pwm@vger.kernel.org, mfd@lists.linux.dev, Rajeev.kumar@airoha.com, upstream@airoha.com, Weifeng.xu@airoha.com, zhengping.zhang@airoha.com Subject: [PATCH v2 3/3] pwm: airoha: add support for AN7589 Date: Fri, 4 Dec 2026 20:38:57 +0100 Message-Id: <20261204193857.3801624-4-john@phrozen.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261204193857.3801624-1-john@phrozen.org> References: <20261204193857.3801624-1-john@phrozen.org> X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The AN7589 flash map has a 4 bit set-id per channel instead of 3. On the EN7581 bit 3 of each channel is the enable bit; on the AN7589 the enable bits are in a register of their own at 0xe0, one bit per channel. Describe both layouts in match data: the set-id mask, and for the enable bits the first register, the bit of channel 0 and the distance in bits between two channels. The AN7589 has the 16 GPIO channels of the EN7581 and no SIPO channels: only the low 16 bits of the enable register and the two GPIO flash map registers hold a value. So the channel count moves into the match data as well: 16 for the AN7589, 33 for the EN7581 as before. The device tree binding of the AN7589 GPIO system controller has no child node for the PWM controller; the system controller node itself carries #pwm-cells. The MFD driver of the system controller registers the PWM controller as a cell. Match the cell by name and take over the device tree node of the parent, so that consumers find the PWM chip. With an id_table, the platform bus no longer matches a device by the driver name "pwm-airoha". Nothing creates a device with that name: the EN7581 PWM controller binds by its compatible. Signed-off-by: John Crispin --- Changes in v2: - Describe the enable bits of both SoCs with a register, the bit of channel 0 and the distance between two channels, so the lookup has no condition (Uwe). - Add EN7581_PWM_NUM_CHANNELS (Uwe). - Say in the commit message why the PWM chip uses the node of its parent and what the id_table changes for the name match (Uwe). drivers/pwm/pwm-airoha.c | 116++++++++++++++++++++++++++++++++++----- 1 file changed, 102 insertions(+), 14 deletions(-) diff --git a/drivers/pwm/pwm-airoha.c b/drivers/pwm/pwm-airoha.c index 0cf6b011520a..85e17a9d69b6 100644 --- a/drivers/pwm/pwm-airoha.c +++ b/drivers/pwm/pwm-airoha.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +54,7 @@ #define AIROHA_PWM_REG_GPIO_FLASH_MAP_SHIFT(_n) (4 * (_n)) #define AIROHA_PWM_GPIO_FLASH_EN BIT(3) #define AIROHA_PWM_GPIO_FLASH_SET_ID GENMASK(2, 0) +#define AIROHA_PWM_GPIO_FLASH_SET_ID_WIDE GENMASK(3, 0) /* Register map is equal to GPIO flash map */ #define AIROHA_PWM_REG_SIPO_FLASH_MAP(_n) (0x0054 + (4 * (_n))) @@ -61,6 +63,8 @@ #define AIROHA_PWM_REG_CYCLE_CFG_SHIFT(_n) (8 * (_n)) #define AIROHA_PWM_WAVE_GEN_CYCLE GENMASK(7, 0) +#define AIROHA_PWM_REG_GPIO_FLASH_EN 0x00e0 + /* GPIO/SIPO flash map handles 8 pins in one register */ #define AIROHA_PWM_PINS_PER_FLASH_MAP 8 /* Cycle(Period) registers handles 4 generators in one 32-bit register */ @@ -78,6 +82,16 @@ #define AIROHA_PWM_NUM_GPIO 16 #define AIROHA_PWM_NUM_SIPO 17 #define AIROHA_PWM_MAX_CHANNELS (AIROHA_PWM_NUM_GPIO + AIROHA_PWM_NUM_SIPO) +#define EN7581_PWM_NUM_CHANNELS AIROHA_PWM_MAX_CHANNELS +#define AN7589_PWM_NUM_CHANNELS AIROHA_PWM_NUM_GPIO + +struct airoha_pwm_soc_data { + unsigned int npwm; + u32 flash_set_id_mask; + u32 flash_en_reg; + u32 flash_en_mask; + u8 flash_en_stride; +}; struct airoha_pwm_bucket { /* Concurrent access protected by PWM core */ @@ -88,6 +102,7 @@ struct airoha_pwm_bucket { struct airoha_pwm { struct regmap *regmap; + const struct airoha_pwm_soc_data *soc; DECLARE_BITMAP(initialized, AIROHA_PWM_MAX_CHANNELS); @@ -417,9 +432,41 @@ static int airoha_pwm_sipo_init(struct airoha_pwm *pc) AIROHA_PWM_SERIAL_GPIO_FLASH_MODE); } +static void airoha_pwm_get_flash_en_addr_and_mask(struct airoha_pwm *pc, + unsigned int hwpwm, + u32 *addr, u32 *mask) +{ + const struct airoha_pwm_soc_data *soc = pc->soc; + unsigned int bit = __ffs(soc->flash_en_mask) + + hwpwm * soc->flash_en_stride; + + *addr = soc->flash_en_reg + sizeof(u32) * (bit / BITS_PER_TYPE(u32)); + *mask = BIT(bit % BITS_PER_TYPE(u32)); +} + +static int airoha_pwm_flash_enable(struct airoha_pwm *pc, unsigned int hwpwm, + bool enable) +{ + u32 addr, mask; + + airoha_pwm_get_flash_en_addr_and_mask(pc, hwpwm, &addr, &mask); + + return regmap_assign_bits(pc->regmap, addr, mask, enable); +} + +static int airoha_pwm_flash_enabled(struct airoha_pwm *pc, unsigned int hwpwm) +{ + u32 addr, mask; + + airoha_pwm_get_flash_en_addr_and_mask(pc, hwpwm, &addr, &mask); + + return regmap_test_bits(pc->regmap, addr, mask); +} + static int airoha_pwm_config_flash_map(struct airoha_pwm *pc, unsigned int hwpwm, int index) { + u32 set_id_mask = pc->soc->flash_set_id_mask; unsigned int addr; u32 shift; int ret; @@ -432,17 +479,15 @@ static int airoha_pwm_config_flash_map(struct airoha_pwm *pc, * If we need to disable the PWM, we just put low the * GPIO. No need to setup buckets. */ - return regmap_clear_bits(pc->regmap, addr, - AIROHA_PWM_GPIO_FLASH_EN << shift); + return airoha_pwm_flash_enable(pc, hwpwm, false); } - ret = regmap_update_bits(pc->regmap, addr, - AIROHA_PWM_GPIO_FLASH_SET_ID << shift, - FIELD_PREP(AIROHA_PWM_GPIO_FLASH_SET_ID, index) << shift); + ret = regmap_update_bits(pc->regmap, addr, set_id_mask << shift, + field_prep(set_id_mask, index) << shift); if (ret) return ret; - return regmap_set_bits(pc->regmap, addr, AIROHA_PWM_GPIO_FLASH_EN << shift); + return airoha_pwm_flash_enable(pc, hwpwm, true); } static int airoha_pwm_config(struct airoha_pwm *pc, struct pwm_device *pwm, @@ -550,19 +595,23 @@ static int airoha_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, u32 addr, shift, val; u8 bucket; + ret = airoha_pwm_flash_enabled(pc, hwpwm); + if (ret < 0) + return ret; + + state->enabled = ret; + if (!state->enabled) + return 0; + airoha_pwm_get_flash_map_addr_and_shift(hwpwm, &addr, &shift); ret = regmap_read(pc->regmap, addr, &val); if (ret) return ret; - state->enabled = FIELD_GET(AIROHA_PWM_GPIO_FLASH_EN, val >> shift); - if (!state->enabled) - return 0; - state->polarity = PWM_POLARITY_NORMAL; - bucket = FIELD_GET(AIROHA_PWM_GPIO_FLASH_SET_ID, val >> shift); + bucket = field_get(pc->soc->flash_set_id_mask, val >> shift); return airoha_pwm_get_bucket(pc, bucket, &state->period, &state->duty_cycle); } @@ -574,17 +623,30 @@ static const struct pwm_ops airoha_pwm_ops = { static int airoha_pwm_probe(struct platform_device *pdev) { + const struct airoha_pwm_soc_data *soc; + const struct platform_device_id *id; struct device *dev = &pdev->dev; struct airoha_pwm *pc; struct pwm_chip *chip; int ret; - chip = devm_pwmchip_alloc(dev, AIROHA_PWM_MAX_CHANNELS, sizeof(*pc)); + id = platform_get_device_id(pdev); + if (id) { + soc = (const struct airoha_pwm_soc_data *)id->driver_data; + device_set_of_node_from_dev(dev, dev->parent); + } else { + soc = device_get_match_data(dev); + } + if (!soc) + return -EINVAL; + + chip = devm_pwmchip_alloc(dev, soc->npwm, sizeof(*pc)); if (IS_ERR(chip)) return PTR_ERR(chip); chip->ops = &airoha_pwm_ops; pc = pwmchip_get_drvdata(chip); + pc->soc = soc; pc->regmap = device_node_to_regmap(dev_of_node(dev->parent)); if (IS_ERR(pc->regmap)) @@ -597,12 +659,37 @@ static int airoha_pwm_probe(struct platform_device *pdev) return 0; } +static const struct airoha_pwm_soc_data en7581_pwm_soc_data = { + .npwm = EN7581_PWM_NUM_CHANNELS, + .flash_set_id_mask = AIROHA_PWM_GPIO_FLASH_SET_ID, + .flash_en_reg = AIROHA_PWM_REG_GPIO_FLASH_MAP(0), + .flash_en_mask = AIROHA_PWM_GPIO_FLASH_EN, + .flash_en_stride = AIROHA_PWM_REG_GPIO_FLASH_MAP_SHIFT(1), +}; + +static const struct airoha_pwm_soc_data an7589_pwm_soc_data = { + .npwm = AN7589_PWM_NUM_CHANNELS, + .flash_set_id_mask = AIROHA_PWM_GPIO_FLASH_SET_ID_WIDE, + .flash_en_reg = AIROHA_PWM_REG_GPIO_FLASH_EN, + .flash_en_mask = BIT(0), + .flash_en_stride = 1, +}; + static const struct of_device_id airoha_pwm_of_match[] = { - { .compatible = "airoha,en7581-pwm" }, + { .compatible = "airoha,en7581-pwm", .data = &en7581_pwm_soc_data }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, airoha_pwm_of_match); +static const struct platform_device_id airoha_pwm_id_table[] = { + { + .name = "airoha-an7589-pwm", + .driver_data = (kernel_ulong_t)&an7589_pwm_soc_data, + }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(platform, airoha_pwm_id_table); + static struct platform_driver airoha_pwm_driver = { .driver = { .name = "pwm-airoha", @@ -610,6 +697,7 @@ static struct platform_driver airoha_pwm_driver = { .of_match_table = airoha_pwm_of_match, }, .probe = airoha_pwm_probe, + .id_table = airoha_pwm_id_table, }; module_platform_driver(airoha_pwm_driver); @@ -617,5 +705,5 @@ MODULE_AUTHOR("Lorenzo Bianconi "); MODULE_AUTHOR("Markus Gothe "); MODULE_AUTHOR("Benjamin Larsson "); MODULE_AUTHOR("Christian Marangi "); -MODULE_DESCRIPTION("Airoha EN7581 PWM driver"); +MODULE_DESCRIPTION("Airoha PWM driver"); MODULE_LICENSE("GPL"); -- 2.34.1