From patchwork Fri Mar 1 11:11:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dimitri Fedrau X-Patchwork-Id: 208787 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp1004685dyb; Fri, 1 Mar 2024 03:15:14 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUuu1TjrVRL/IBHFJncNUp2KWWg+XvJnkH75ech/1S/3+CSeFusJYQI2HrYM7X/GTNKY80f9CEMuPPubXHXEC0cTF4rQg== X-Google-Smtp-Source: AGHT+IEo4vPFY48Oh2ZUiq+EU6v/EnFXv9ZATaH/LMTlGXya72dXLrzV4yz0YGQepfgZYn+p9kFu X-Received: by 2002:a17:90b:889:b0:299:87ba:8a2 with SMTP id bj9-20020a17090b088900b0029987ba08a2mr1283388pjb.41.1709291714451; Fri, 01 Mar 2024 03:15:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709291714; cv=pass; d=google.com; s=arc-20160816; b=xtFRgHFfC3nXy2I1/PIok/nyc74J+0/xJfyeTE8NtieC3ipR0DYTZr1481xTGPO0L8 IOfdhUOCqDDGxILpaAyCZrlof7xz6AicCIqr9TO2KSwc82eNSi81g7dMgihJup7z3hAI z+sLrHYCaUizJ9NGnrgS5acBL7JB/lX7urWeLmBQgQFnmKBP8PkL/7HBL6fa15HTolkD TCeVRJmN6dfEzJVaj4dItqIU3eFPBU3/ldsI94vsyAxnA14xDGAdl0rFJ0yTZHEjtQh7 9Kmp7lAvhLVvweGmBV4ctgAhnuxzpd+XBJ33fKCWy8klWK/o1KNQjy980XFEVQ58etAn xrWg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=GPr5OT0hjAf7wDQpFiXt+umkADr7mP/nXjReYVXPj5M=; fh=C6KNRX8KGlM1kP/th5O1m+bTp3eGi7923Iyh9t2RYqg=; b=yT6TVBH/h1e3KiMklkcoXn2/f016AuAbIwgS9jHkFoB9VpP5Qmde/paHToM9x8Gq/l cea/BxXX6IC3jJSn5RA2sz4ahKl+dAC8W2h1c+ZXAQ6WvJZG8l+tm2ksdZkrmlfvt+5G mOgZF6QMtpzoLnYbgkQOjVd05p1JNtv3J1b2Hd/PQsxWzp1sBVTMvFI1zw/E6oU1wTD1 JSgcsDsP32Zcj2Y890aBWcjkU+IrZMGxiwTN4+lAP0hM9HrGwMXzwQBJ5h3X3QWg1IKp qQ2HHE1EX2ddoS5Jfrrxmlgrm8kotgMVtyv5wFC1WkDdAcbkBKxcy4VSGId1jw1JDzEN M1hA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=IuJ7dY3n; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-88319-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88319-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id a23-20020a17090a8c1700b002931b3873dasi5368524pjo.22.2024.03.01.03.15.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 03:15:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88319-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=IuJ7dY3n; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-88319-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88319-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 42A952849C4 for ; Fri, 1 Mar 2024 11:15:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 17EF270CA0; Fri, 1 Mar 2024 11:12:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IuJ7dY3n" Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) (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 6B23D6CDC3; Fri, 1 Mar 2024 11:12:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709291565; cv=none; b=rJly8cPylIZqhlm5ZNmr7baMDfZ7ZCx7ghHhd7qwyoKiSeqipkbqqQ5NDtsMaeZ5Dbs/m6wy1juheKTV6ZQyi9KmIiQBRD+5+pNIyErbAfKx5E93NCi9N4Kxk8sYmT5L/loBqtPk3Ix5+S6sY6S8gaxHnCYr/eYGRvACHB6qw3c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709291565; c=relaxed/simple; bh=7IIXC1qsD1MFpjVOqYYi+BOf0Ko89zjnAhsRvckXXLA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JZECmuiTBEy4nqfQfDTlDKTjSBsAH8jU4hlsi9S254ADwZFqejfnpfqMGZsk75CO+RRStzCuf/qiOiv/BAoGGoUxIx2bjzUH7eq0xslxjB18wK+6VWxYsOhjGfDE7HRl/sx0kpP07j2KyoSaAb3ftzw3R72OmU39fd1qdS15BM0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IuJ7dY3n; arc=none smtp.client-ip=209.85.218.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-a441d7c6125so253510966b.2; Fri, 01 Mar 2024 03:12:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709291562; x=1709896362; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GPr5OT0hjAf7wDQpFiXt+umkADr7mP/nXjReYVXPj5M=; b=IuJ7dY3nK6Qgu/hBg3XdZzffbClVFdoSGQfWE1vXYhRyaNB7r2TPmPXWvkmuVPg8H7 mT31J9He6NVPNV4n7gh4o19UHsg63Z9KsSYHY6cxZvWt0pUDgXwD3P7VbFYaPt4CO7Tm OrQ/EchUsm4C94HpfDOcoofs2Hmz+9qouMN7nyy2aZGwwawEWl3NCEu4j2JdPS2wsmPm OQ34JII8VAf+QqUtDjVr/pGTFcy+nehhlVUCAXbjHQMzhAw73iFbw1eXGoZuL0GJvICT wRPjsK+wxrR6biXn9BnLrtK5Nx4NIxu21296exygR5ybmW430rk6I3MIavy6ZrXyFb0x 8xBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709291562; x=1709896362; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GPr5OT0hjAf7wDQpFiXt+umkADr7mP/nXjReYVXPj5M=; b=NAiXzG/SBrwHhK7Oo0qMxQ/KCbRsURMRuJeqXpyocLF/Nx7tjxhVshlz5DeqWTy7SZ HCFQ75ESt9ZXzv7kXuBHEZtJk7wKxgziNs7dsBZ/Uzeo+9WT1YDA6jqd5yQ9gXXKDh4a 0+9kPlmMUtV8QTPBbNEtx5RcwPZchIfDf6+yN/BVHWaEjR0zYufgiYuGQeJMuJi9lFud KSLBLeTmT9y9gS5YGhs+/WrXRItceF+T5ibFYZC57En3UWo0klM1ksIatq7DymcSyYc5 /sDphp+46grZAJmhnmTDQpttEG4DcwSmaHhHGvoYqvNZRZh9yAZMc0PET0uwgLZRQpKg ZYlg== X-Forwarded-Encrypted: i=1; AJvYcCU12yxnDCai8VpuckolvX18dfkZvJc2usfCLct/o5bRE9aHBJIljMBUOYnBJhtY7XxgOG00oYlIY2yejTJvmIuGRo4LcnoV7+wHqhXKwrfUZninPmANmppEwFS7NUzdGMsTDKt2bVVFaBwyja4swWoBAHJzz3Qiigx4aXMHKRdy9X2Fgw== X-Gm-Message-State: AOJu0Yx5reZteLfSyqwZhCe+eUMSHbUo621Han5fnFJqPPUN96il3Lod 3S9iyCPKTnrXhmJYMZHczZVsGYaKibt820VNtAMefFz+6svjJG0R X-Received: by 2002:a17:906:6d4e:b0:a44:731c:bacc with SMTP id a14-20020a1709066d4e00b00a44731cbaccmr1146651ejt.10.1709291561812; Fri, 01 Mar 2024 03:12:41 -0800 (PST) Received: from debian.fritz.box ([93.184.186.109]) by smtp.gmail.com with ESMTPSA id n18-20020a170906089200b00a43a478e4f0sm1583254eje.180.2024.03.01.03.12.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 03:12:41 -0800 (PST) From: Dimitri Fedrau To: Cc: Dimitri Fedrau , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/3] pwm: mc33xs2410: add support for direct inputs Date: Fri, 1 Mar 2024 12:11:24 +0100 Message-Id: <20240301111124.29283-4-dima.fedrau@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240301111124.29283-1-dima.fedrau@gmail.com> References: <20240301111124.29283-1-dima.fedrau@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792322268781071788 X-GMAIL-MSGID: 1792322268781071788 Add support for direct inputs, which are used to directly turn-on or turn-off the outputs. Direct inputs have the advantage over the SPI controlled outputs that they aren't limited to the frequency steps. Frequency resolution depends on the input signal, range is still from 0.5Hz to 2.048kHz. Signed-off-by: Dimitri Fedrau --- drivers/pwm/pwm-mc33xs2410.c | 116 +++++++++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 11 deletions(-) diff --git a/drivers/pwm/pwm-mc33xs2410.c b/drivers/pwm/pwm-mc33xs2410.c index 35753039da6b..828a67227185 100644 --- a/drivers/pwm/pwm-mc33xs2410.c +++ b/drivers/pwm/pwm-mc33xs2410.c @@ -18,7 +18,10 @@ #define MC33XS2410_GLB_CTRL_MODE_MASK GENMASK(7, 6) #define MC33XS2410_GLB_CTRL_NORMAL_MODE BIT(6) #define MC33XS2410_GLB_CTRL_SAFE_MODE BIT(7) +#define MC33XS2410_GLB_CTRL_CMOS_LEVEL BIT(0) #define MC33XS2410_OUT1_4_CTRL 0x02 +#define MC33XS2410_IN_CTRL1 0x03 +#define MC33XS2410_IN_CTRL1_IN_EN(x) BIT(x) #define MC33XS2410_PWM_CTRL1 0x05 #define MC33XS2410_PWM_CTRL1_POL_INV(x) BIT(x) #define MC33XS2410_PWM_CTRL3 0x07 @@ -45,6 +48,7 @@ struct mc33xs2410_pwm { struct pwm_chip chip; struct spi_device *spi; + struct pwm_device *di[4]; struct mutex lock; }; @@ -154,20 +158,15 @@ static u8 mc33xs2410_pwm_get_freq(const struct pwm_state *state) return (ret | FIELD_PREP(MC33XS2410_PWM_FREQ_STEP_MASK, step)); } -static int mc33xs2410_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, - const struct pwm_state *state) +static int mc33xs2410_pwm_apply_spi(struct pwm_chip *chip, + struct pwm_device *pwm, + const struct pwm_state *state) { struct mc33xs2410_pwm *mc33xs2410 = mc33xs2410_pwm_from_chip(chip); struct spi_device *spi = mc33xs2410->spi; u8 mask, val; int ret; - if (state->period > mc33xs2410_period[STEP_05HZ][MC33XS2410_PERIOD_MAX]) - return -EINVAL; - - if (state->period < mc33xs2410_period[STEP_32HZ][MC33XS2410_PERIOD_MIN]) - return -EINVAL; - guard(mutex)(&mc33xs2410->lock); mask = MC33XS2410_PWM_CTRL1_POL_INV(pwm->hwpwm); val = (state->polarity == PWM_POLARITY_INVERSED) ? mask : 0; @@ -190,9 +189,38 @@ static int mc33xs2410_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, return mc33xs2410_modify_reg(spi, MC33XS2410_PWM_CTRL3, mask, val); } -static int mc33xs2410_pwm_get_state(struct pwm_chip *chip, - struct pwm_device *pwm, - struct pwm_state *state) +static int mc33xs2410_pwm_apply_direct_inputs(struct pwm_chip *chip, + struct pwm_device *pwm, + const struct pwm_state *state) +{ + struct mc33xs2410_pwm *mc33xs2410 = mc33xs2410_pwm_from_chip(chip); + struct pwm_device *di = mc33xs2410->di[pwm->hwpwm]; + + guard(mutex)(&mc33xs2410->lock); + + return pwm_apply_state(di, state); +} + +static int mc33xs2410_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, + const struct pwm_state *state) +{ + struct mc33xs2410_pwm *mc33xs2410 = mc33xs2410_pwm_from_chip(chip); + + if (state->period > mc33xs2410_period[STEP_05HZ][MC33XS2410_PERIOD_MAX]) + return -EINVAL; + + if (state->period < mc33xs2410_period[STEP_32HZ][MC33XS2410_PERIOD_MIN]) + return -EINVAL; + + if (mc33xs2410->di[pwm->hwpwm]) + return mc33xs2410_pwm_apply_direct_inputs(chip, pwm, state); + else + return mc33xs2410_pwm_apply_spi(chip, pwm, state); +} + +static int mc33xs2410_pwm_get_state_spi(struct pwm_chip *chip, + struct pwm_device *pwm, + struct pwm_state *state) { struct mc33xs2410_pwm *mc33xs2410 = mc33xs2410_pwm_from_chip(chip); struct spi_device *spi = mc33xs2410->spi; @@ -236,6 +264,28 @@ static int mc33xs2410_pwm_get_state(struct pwm_chip *chip, return 0; } +static int mc33xs2410_pwm_get_state_direct_inputs(struct pwm_chip *chip, + struct pwm_device *pwm, + struct pwm_state *state) +{ + struct mc33xs2410_pwm *mc33xs2410 = mc33xs2410_pwm_from_chip(chip); + + pwm_get_state(mc33xs2410->di[pwm->hwpwm], state); + return 0; +} + +static int mc33xs2410_pwm_get_state(struct pwm_chip *chip, + struct pwm_device *pwm, + struct pwm_state *state) +{ + struct mc33xs2410_pwm *mc33xs2410 = mc33xs2410_pwm_from_chip(chip); + + if (mc33xs2410->di[pwm->hwpwm]) + return mc33xs2410_pwm_get_state_direct_inputs(chip, pwm, state); + else + return mc33xs2410_pwm_get_state_spi(chip, pwm, state); +} + static const struct pwm_ops mc33xs2410_pwm_ops = { .apply = mc33xs2410_pwm_apply, .get_state = mc33xs2410_pwm_get_state, @@ -257,6 +307,45 @@ static int mc33xs2410_reset(struct device *dev) return 0; } +static int mc33xs2410_direct_inputs_probe(struct mc33xs2410_pwm *mc33xs2410) +{ + struct device *dev = &mc33xs2410->spi->dev; + u16 di_en = 0; + char buf[4]; + int ret, ch; + + for (ch = 0; ch < 4; ch++) { + sprintf(buf, "di%d", ch); + mc33xs2410->di[ch] = devm_pwm_get(dev, buf); + ret = PTR_ERR_OR_ZERO(mc33xs2410->di[ch]); + switch (ret) { + case 0: + di_en |= MC33XS2410_IN_CTRL1_IN_EN(ch); + break; + case -ENODATA: + mc33xs2410->di[ch] = NULL; + break; + case -EPROBE_DEFER: + return ret; + default: + dev_err(dev, "Failed to request %s: %d\n", buf, ret); + return ret; + } + } + + if (!di_en) + return 0; + + /* CMOS input logic level */ + ret = mc33xs2410_modify_reg(mc33xs2410->spi, MC33XS2410_GLB_CTRL, + MC33XS2410_GLB_CTRL_CMOS_LEVEL, + MC33XS2410_GLB_CTRL_CMOS_LEVEL); + if (ret < 0) + return ret; + + return mc33xs2410_write_reg(mc33xs2410->spi, MC33XS2410_IN_CTRL1, di_en); +} + static int mc33xs2410_probe(struct spi_device *spi) { struct mc33xs2410_pwm *mc33xs2410; @@ -290,6 +379,11 @@ static int mc33xs2410_probe(struct spi_device *spi) return dev_err_probe(dev, ret, "Failed to transition to normal mode\n"); + /* Enable direct inputs */ + ret = mc33xs2410_direct_inputs_probe(mc33xs2410); + if (ret) + return ret; + ret = devm_pwmchip_add(dev, &mc33xs2410->chip); if (ret < 0) return dev_err_probe(dev, ret, "Failed to add pwm chip\n");