From nobody Sat Nov 23 23:08:43 2024 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (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 77D4913BC35; Sat, 9 Nov 2024 17:41:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731174106; cv=none; b=U2oO3u1mhSM4PHB/rNyypHz6B5AJY4w/y7JhDw75bsPGWEKal/GHvUgD1PEe9nanxslgcpulwBOCHfWqRAOFSZb/GrJY12RnZNZbxQPvWkdn0nE6vauGn9DBg2/K7C826DEXtdptCWTVEImVAOdYH+YYMC0CvemAQKOdber4R0M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731174106; c=relaxed/simple; bh=gx2AN6g8CBHOQhQWVvBpRtBLyzQubEgkJDNlqL08IzM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=L6OTO0nbWTRvORHVE1qW3wCJ/39kP79OtrFXXNQE5yhn51ZyFhialan06XgWshF0zaKeR8mwqcH2Of8T7J9LzSIKNH2kNgpmNfz7Qr+rJFur7tiqORvSE/kNmg65o9FGMM7HJNAP9d6H3ZCS5HPYUoTYK9r0KOLv24+9I9QX2lo= 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=iLi/DUS6; arc=none smtp.client-ip=209.85.221.47 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iLi/DUS6" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-37d50fad249so2373985f8f.1; Sat, 09 Nov 2024 09:41:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731174103; x=1731778903; 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=GkinVYGPyGwJ32i1kYAOsHhIvbGugGjjdqxMHgIEwAU=; b=iLi/DUS6GCJwNo0pF/iphnk2/uSdDUtW6AFJ+DopmwD28IxUQIzzzOK1klH2BJPbY4 XW6LBhGJxgH4uhp4naxMAJBrC+NdIODO/TFLbfBMEr6FWV/9yTL1bnuAh1/83hcqEbLZ rO6isV5Ydp02Wk6me9uQzSTW+OpdT0J1vUPIGidFxqgn6oNaQ2F+fQWntuCzu5C6n0Ib HlMegCkjMrjrCqQUIcqhrgmr3Zqyp3IborULlSo7PuIbiKmri42x56CKAO3Wi6QimgRU oNmtOfBD7RmR27Y3yK0UjrowzODWiqXV+n1rfMLBeCBD+RMr4NZkxSodM5xISSb2A+Oe Vk3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731174103; x=1731778903; 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=GkinVYGPyGwJ32i1kYAOsHhIvbGugGjjdqxMHgIEwAU=; b=vbaXw0isyxpmsK1uf0lQCI3yGkZNstX6q9Ll8fb1SRDLUQA6gLnPEeAeUR7WrQEKcR yYEpSpCO2XD7hwjJj/8rn0vciEDNCCvY61nKKet65NVOCrCStoW2SDx0Kqdorzm2womF ls4EVNmyza++uUko8oVWbYu8IwioWughBIUwCPyLS6YRNBKCMfz2SFfF+1NOONkSdzKe jvhbGhlCpL3wKdZlH8ig8DzMS3mo7L2uL3DeHq3T/AJ8eAUp0Vu4YBeCnS6iSgxOKxnD 1j9xYmvzPi5LvGzT6py71DVAptbwi4yQpt0eR6aPuyjer2rDGL5RxPwR4k5cPCL4Vh+T U5ag== X-Forwarded-Encrypted: i=1; AJvYcCUmkHzpuIfPJc+MyNjjW53ZuPsQKZM+MF63PdtvoK3oQNE/4LC5iWLSYyVRnNIcyI7P8MifZQilrId6o8xC@vger.kernel.org, AJvYcCWj7IdzIGVx5WB9YklS0xSjYt/qq+9Vq4XLMccmnPAdnaLOWyoPa3dB+k9+tt8U3+3AObT59qdd2DGC@vger.kernel.org, AJvYcCWnryzgvBhCoq2GqR9lt0a+nbKswbgK2GMlZPLpWvyVlougxaI8iR8Ni7N6aihE6+6WWWJtU/Z7Qk3P@vger.kernel.org X-Gm-Message-State: AOJu0Yw6EpST/7GIF3fYiuXz7p3Kh0GFVQebc62Ag0/vVQ3+SbDfibC6 en5jN45PuQI2vetVk3K7Au+37GUMsA/sNIQmGrJXgnzFtstWJ5xF X-Google-Smtp-Source: AGHT+IEh4J97FEYorYsztkOFKAsNtA+wdT1GmST8Y5PJSkDl/MlMVznGOlav5qUxAOIj7LrJEClF1g== X-Received: by 2002:a5d:59a6:0:b0:37d:4d3f:51e6 with SMTP id ffacd0b85a97d-381f186bf72mr6098216f8f.14.1731174102612; Sat, 09 Nov 2024 09:41:42 -0800 (PST) Received: from debian.fritz.box ([2a00:79c0:605:8200:303:6c5b:4b07:6715]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432b05c202asm117582285e9.30.2024.11.09.09.41.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Nov 2024 09:41:42 -0800 (PST) From: Dimitri Fedrau To: Cc: Dimitri Fedrau , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Krzysztof Kozlowski Subject: [PATCH v7 1/2] dt-bindings: pwm: add support for MC33XS2410 Date: Sat, 9 Nov 2024 18:41:34 +0100 Message-Id: <20241109174135.26292-2-dima.fedrau@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241109174135.26292-1-dima.fedrau@gmail.com> References: <20241109174135.26292-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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adding documentation for NXPs MC33XS2410 high side switch. Reviewed-by: Krzysztof Kozlowski Signed-off-by: Dimitri Fedrau --- .../bindings/pwm/nxp,mc33xs2410.yaml | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 Documentation/devicetree/bindings/pwm/nxp,mc33xs2410.ya= ml diff --git a/Documentation/devicetree/bindings/pwm/nxp,mc33xs2410.yaml b/Do= cumentation/devicetree/bindings/pwm/nxp,mc33xs2410.yaml new file mode 100644 index 000000000000..1729fe5c3dfb --- /dev/null +++ b/Documentation/devicetree/bindings/pwm/nxp,mc33xs2410.yaml @@ -0,0 +1,118 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/pwm/nxp,mc33xs2410.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: High-side switch MC33XS2410 + +maintainers: + - Dimitri Fedrau + +allOf: + - $ref: pwm.yaml# + - $ref: /schemas/spi/spi-peripheral-props.yaml# + +properties: + compatible: + const: nxp,mc33xs2410 + + reg: + maxItems: 1 + + spi-max-frequency: + maximum: 10000000 + + spi-cpha: true + + spi-cs-setup-delay-ns: + minimum: 100 + default: 100 + + spi-cs-hold-delay-ns: + minimum: 10 + default: 10 + + spi-cs-inactive-delay-ns: + minimum: 300 + default: 300 + + reset-gpios: + description: + GPIO connected to the active low reset pin. + maxItems: 1 + + "#pwm-cells": + const: 3 + + pwm-names: + items: + - const: di0 + - const: di1 + - const: di2 + - const: di3 + + pwms: + description: + Direct inputs(di0-3) are used to directly turn-on or turn-off the + outputs. + maxItems: 4 + + interrupts: + maxItems: 1 + + clocks: + description: + The external clock can be used if the internal clock doesn't meet + timing requirements over temperature and voltage operating range. + maxItems: 1 + + vdd-supply: + description: + Logic supply voltage + + vspi-supply: + description: + Supply voltage for SPI + + vpwr-supply: + description: + Power switch supply + +required: + - compatible + - reg + +unevaluatedProperties: false + +examples: + - | + #include + #include + spi { + #address-cells =3D <1>; + #size-cells =3D <0>; + + pwm@0 { + compatible =3D "nxp,mc33xs2410"; + reg =3D <0x0>; + spi-max-frequency =3D <4000000>; + spi-cpha; + spi-cs-setup-delay-ns =3D <100>; + spi-cs-hold-delay-ns =3D <10>; + spi-cs-inactive-delay-ns =3D <300>; + reset-gpios =3D <&gpio3 22 GPIO_ACTIVE_LOW>; + #pwm-cells =3D <3>; + pwm-names =3D "di0", "di1", "di2", "di3"; + pwms =3D <&pwm0 0 1000000>, + <&pwm1 0 1000000>, + <&pwm2 0 1000000>, + <&pwm3 0 1000000>; + interrupt-parent =3D <&gpio0>; + interrupts =3D <31 IRQ_TYPE_LEVEL_LOW>; + clocks =3D <&clk_ext_fixed>; + vdd-supply =3D <®_3v3>; + vspi-supply =3D <®_3v3>; + vpwr-supply =3D <®_24v0>; + }; + }; --=20 2.39.5 From nobody Sat Nov 23 23:08:43 2024 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 B0E8219DFA5; Sat, 9 Nov 2024 17:41:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731174107; cv=none; b=DHGFsJ7eaQai7fOmETBhz7TkmF23f14xGqBgsP+GKZGTq5M9n293HpDOyh1hr6IBmJDR1Fd/ediiYtMr/IuNv7qR9j6VOwxzSyGCAWlwVxpODMkjwFkERo/QEWjcmrt7VeiOzI7o0LtX1Btj1aFZAl5asWyaVidRTOjm5rmN2mk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731174107; c=relaxed/simple; bh=U2wnVl8pDpqYK/SSLtC+00Xk9FtRH+TTGkidqBfsoL4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uzGNvGmKXFQbaF+Cy6GYQoGrce2Wb7gS8tCXoWTHYnBN07LAWUQoepkeR06XT6a2LrdJhXWhcTuHXkCHEy4+ALrOTmCsYUON6rmWqXrSdeqiYXGYvnnT7sOTuKpZz+ap6PKrG83jQg+5SPKBo632nPxdOnFSuZWolUg8pL0UPbE= 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=fOpmMi5d; arc=none smtp.client-ip=209.85.128.42 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fOpmMi5d" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-431695fa98bso25040775e9.3; Sat, 09 Nov 2024 09:41:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731174104; x=1731778904; 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=gIZ3Pod6uFsC5aQPwLz1gT8xGdaob9qbUQFVgHnwKdc=; b=fOpmMi5dHiAvUhrqzzef0tSGmS+oSEDE3rrM87HUrSpo1ua/V/pwtoyJcMeWNIcm2f 0pVZZmT/SBR0QTcznPQX1Z16wrE0NgT+l/ZmVRELcAinSIXjp1fc/t0f7C39vf+1cG6d 7FgoF9sXsLdGbr4L/lWK2JtbVnRA1cWWzxTbWV+3vE/hXBWchv0TaX3dzeAGyNgMurzN v6+rnBzP7fK8FA7YjQIoXPslbZrmZfWmS2LdsV3w1s+/V52Jf7JHYGoS2mRJQO39VDy9 gCvBoCCmM0JfSXQFfAlzPA48R6o8o0uVkimNeC+fep7ATe7YS084meJ0myanjUyUpQuZ t1uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731174104; x=1731778904; 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=gIZ3Pod6uFsC5aQPwLz1gT8xGdaob9qbUQFVgHnwKdc=; b=tEmq8cUGglXb+2ODj2AXizomUC2XO7MGuySr2+TKaKM/qTJmNsvbGntmMkd+9yvUyL A1Tm6rWsaBkwLYZE7M3UF9LzT4ZasfsYALOUNcJQ+u9UzIG5kDWZ0K31HCJx1Eqo5rQl xI0V/F+ACcGVQC4beU6Or9vUEuexMF31FrQ5zQOG96+SCkBPC/a/UOWRpIfdhT2cfdFB e1KQfa10wUJ+XtaBcl/RHXv87JtbEUpqMA98Y3c13A3CrmX1Ooi05yU4wpe/dqF3E5yn dnXU9xEXL+4RZYT1umKT7SbIekRLUGnQD9zOoAUB/rq06cwKcUejiM1cZBXz9NJucwsw tKhw== X-Forwarded-Encrypted: i=1; AJvYcCUEhxk0cepdpk0SYEa7WfBSXlYHpRpL4GIu9xehCTsFw7/qeAdLv795D1XVwb8Tn5fOL/GjAKYulbiK@vger.kernel.org, AJvYcCVwrFR20Ly8rnFaSsz26d9go2qWF4nvvDPSpLAiE8mjGIxHB8gmJzWDroKvQmmSw3RIWzKEK3mguZqN@vger.kernel.org, AJvYcCW8XF4ntiDimYC4gIkoM9PLxbjmlGnmBvMNm/pqqiHiUrLUTKe71ttiKkiwbqtIIrhwpz2BQHEwJF82NT9d@vger.kernel.org X-Gm-Message-State: AOJu0YyKHAvw4BScv59BwR2+/ePuO2KuD/y1xHX/whRn3hGSsLxprBrx 2eDsQfEuo0rCl3ZM2IhmalIaxareybetZVZimW6xwbJwgdJJ1OugVN6MyjEy X-Google-Smtp-Source: AGHT+IEMOOyIS55ppOvL6DvcS0XGFUmYzvw5oO2Kv4ll57MkqvnrbNWQflKFLgTc4kK0Uo/519aKnA== X-Received: by 2002:a05:600c:3ca4:b0:42c:a6da:a149 with SMTP id 5b1f17b1804b1-432b751839dmr63047175e9.25.1731174103710; Sat, 09 Nov 2024 09:41:43 -0800 (PST) Received: from debian.fritz.box ([2a00:79c0:605:8200:303:6c5b:4b07:6715]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432b05c202asm117582285e9.30.2024.11.09.09.41.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Nov 2024 09:41:43 -0800 (PST) From: Dimitri Fedrau To: Cc: Dimitri Fedrau , =?UTF-8?q?Uwe=20Kleine-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 v7 2/2] pwm: add support for NXPs high-side switch MC33XS2410 Date: Sat, 9 Nov 2024 18:41:35 +0100 Message-Id: <20241109174135.26292-3-dima.fedrau@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241109174135.26292-1-dima.fedrau@gmail.com> References: <20241109174135.26292-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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The MC33XS2410 is a four channel high-side switch. Featuring advanced monitoring and control function, the device is operational from 3.0 V to 60 V. The device is controlled by SPI port for configuration. Signed-off-by: Dimitri Fedrau --- drivers/pwm/Kconfig | 12 ++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-mc33xs2410.c | 388 +++++++++++++++++++++++++++++++++++ 3 files changed, 401 insertions(+) create mode 100644 drivers/pwm/pwm-mc33xs2410.c diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig index 0915c1e7df16..f513513f9b2f 100644 --- a/drivers/pwm/Kconfig +++ b/drivers/pwm/Kconfig @@ -411,6 +411,18 @@ config PWM_LPSS_PLATFORM To compile this driver as a module, choose M here: the module will be called pwm-lpss-platform. =20 +config PWM_MC33XS2410 + tristate "MC33XS2410 PWM support" + depends on OF + depends on SPI + help + NXP MC33XS2410 high-side switch driver. The MC33XS2410 is a four + channel high-side switch. The device is operational from 3.0 V + to 60 V. The device is controlled by SPI port for configuration. + + To compile this driver as a module, choose M here: the module + will be called pwm-mc33xs2410. + config PWM_MESON tristate "Amlogic Meson PWM driver" depends on ARCH_MESON || COMPILE_TEST diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile index 9081e0c0e9e0..c75deeeace40 100644 --- a/drivers/pwm/Makefile +++ b/drivers/pwm/Makefile @@ -36,6 +36,7 @@ obj-$(CONFIG_PWM_LPC32XX) +=3D pwm-lpc32xx.o obj-$(CONFIG_PWM_LPSS) +=3D pwm-lpss.o obj-$(CONFIG_PWM_LPSS_PCI) +=3D pwm-lpss-pci.o obj-$(CONFIG_PWM_LPSS_PLATFORM) +=3D pwm-lpss-platform.o +obj-$(CONFIG_PWM_MC33XS2410) +=3D pwm-mc33xs2410.o obj-$(CONFIG_PWM_MESON) +=3D pwm-meson.o obj-$(CONFIG_PWM_MEDIATEK) +=3D pwm-mediatek.o obj-$(CONFIG_PWM_MICROCHIP_CORE) +=3D pwm-microchip-core.o diff --git a/drivers/pwm/pwm-mc33xs2410.c b/drivers/pwm/pwm-mc33xs2410.c new file mode 100644 index 000000000000..97cf31ef608a --- /dev/null +++ b/drivers/pwm/pwm-mc33xs2410.c @@ -0,0 +1,388 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Liebherr-Electronics and Drives GmbH + * + * Reference Manual : https://www.nxp.com/docs/en/data-sheet/MC33XS2410.pdf + * + * Limitations: + * - Supports frequencies between 0.5Hz and 2048Hz with following steps: + * - 0.5 Hz steps from 0.5 Hz to 32 Hz + * - 2 Hz steps from 2 Hz to 128 Hz + * - 8 Hz steps from 8 Hz to 512 Hz + * - 32 Hz steps from 32 Hz to 2048 Hz + * - Cannot generate a 0 % duty cycle. + * - Always produces low output if disabled. + * - Configuration isn't atomic. When changing polarity, duty cycle or per= iod + * the data is taken immediately, counters not being affected, resulting= in a + * behavior of the output pin that is neither the old nor the new state, + * rather something in between. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define MC33XS2410_GLB_CTRL 0x00 +#define MC33XS2410_GLB_CTRL_MODE GENMASK(7, 6) +#define MC33XS2410_GLB_CTRL_MODE_NORMAL FIELD_PREP(MC33XS2410_GLB_CTRL_MOD= E, 1) + +#define MC33XS2410_PWM_CTRL1 0x05 +/* x in { 1 ... 4 } */ +#define MC33XS2410_PWM_CTRL1_POL_INV(x) BIT((x - 1)) + +#define MC33XS2410_PWM_CTRL3 0x07 +/* x in { 1 ... 4 } */ +#define MC33XS2410_PWM_CTRL3_EN(x) BIT(4 + (x - 1)) + +/* x in { 1 ... 4 } */ +#define MC33XS2410_PWM_FREQ(x) (0x08 + (x - 1)) +#define MC33XS2410_PWM_FREQ_STEP GENMASK(7, 6) +#define MC33XS2410_PWM_FREQ_COUNT GENMASK(5, 0) + +/* x in { 1 ... 4 } */ +#define MC33XS2410_PWM_DC(x) (0x0c + (x - 1)) + +#define MC33XS2410_WDT 0x14 + +#define MC33XS2410_PWM_MIN_PERIOD 488282 +/* x in { 0 ... 3 } */ +#define MC33XS2410_PWM_MAX_PERIOD(x) (2000000000 >> (2 * x)) + +#define MC33XS2410_FRAME_IN_ADDR GENMASK(15, 8) +#define MC33XS2410_FRAME_IN_DATA GENMASK(7, 0) +#define MC33XS2410_FRAME_IN_ADDR_WR BIT(7) +#define MC33XS2410_FRAME_IN_DATA_RD BIT(7) +#define MC33XS2410_FRAME_OUT_DATA GENMASK(13, 0) + +#define MC33XS2410_MAX_TRANSFERS 5 + +struct mc33xs2410_pwm { + struct spi_device *spi; +}; + +static inline struct mc33xs2410_pwm *mc33xs2410_from_chip(struct pwm_chip = *chip) +{ + return pwmchip_get_drvdata(chip); +} + +static int mc33xs2410_write_regs(struct spi_device *spi, u8 *reg, u8 *val, + unsigned int len) +{ + u16 tx[MC33XS2410_MAX_TRANSFERS]; + int i; + + if (len > MC33XS2410_MAX_TRANSFERS) + return -EINVAL; + + for (i =3D 0; i < len; i++) + tx[i] =3D FIELD_PREP(MC33XS2410_FRAME_IN_DATA, val[i]) | + FIELD_PREP(MC33XS2410_FRAME_IN_ADDR, + MC33XS2410_FRAME_IN_ADDR_WR | reg[i]); + + return spi_write(spi, tx, len * 2); +} + +static int mc33xs2410_read_regs(struct spi_device *spi, u8 *reg, u8 flag, + u16 *val, unsigned int len) +{ + u16 tx[MC33XS2410_MAX_TRANSFERS]; + u16 rx[MC33XS2410_MAX_TRANSFERS]; + struct spi_transfer t =3D { + .tx_buf =3D tx, + .rx_buf =3D rx, + }; + int i, ret; + + len++; + if (len > MC33XS2410_MAX_TRANSFERS) + return -EINVAL; + + t.len =3D len * 2; + for (i =3D 0; i < len - 1; i++) + tx[i] =3D FIELD_PREP(MC33XS2410_FRAME_IN_DATA, flag) | + FIELD_PREP(MC33XS2410_FRAME_IN_ADDR, reg[i]); + + ret =3D spi_sync_transfer(spi, &t, 1); + if (ret < 0) + return ret; + + for (i =3D 1; i < len; i++) + val[i - 1] =3D FIELD_GET(MC33XS2410_FRAME_OUT_DATA, rx[i]); + + return 0; +} + + +static int mc33xs2410_write_reg(struct spi_device *spi, u8 reg, u8 val) +{ + return mc33xs2410_write_regs(spi, ®, &val, 1); +} + +static int mc33xs2410_read_reg(struct spi_device *spi, u8 reg, u16 *val, u= 8 flag) +{ + return mc33xs2410_read_regs(spi, ®, flag, val, 1); +} + +static int mc33xs2410_read_reg_ctrl(struct spi_device *spi, u8 reg, u16 *v= al) +{ + return mc33xs2410_read_reg(spi, reg, val, MC33XS2410_FRAME_IN_DATA_RD); +} + +static int mc33xs2410_modify_reg(struct spi_device *spi, u8 reg, u8 mask, = u8 val) +{ + u16 tmp; + int ret; + + ret =3D mc33xs2410_read_reg_ctrl(spi, reg, &tmp); + if (ret < 0) + return ret; + + tmp &=3D ~mask; + tmp |=3D val & mask; + + return mc33xs2410_write_reg(spi, reg, tmp); +} + +static u8 mc33xs2410_pwm_get_freq(u64 period) +{ + u8 step, count; + + /* + * Check which step is appropriate for the given period, starting with + * the highest frequency(lowest period). Higher frequencies are + * represented with better resolution by the device. Therefore favor + * frequency range with the better resolution to minimize error + * introduced by the frequency steps. + */ + + switch (period) { + case MC33XS2410_PWM_MIN_PERIOD ... MC33XS2410_PWM_MAX_PERIOD(3): + step =3D 3; + break; + case MC33XS2410_PWM_MAX_PERIOD(3) + 1 ... MC33XS2410_PWM_MAX_PERIOD(2): + step =3D 2; + break; + case MC33XS2410_PWM_MAX_PERIOD(2) + 1 ... MC33XS2410_PWM_MAX_PERIOD(1): + step =3D 1; + break; + case MC33XS2410_PWM_MAX_PERIOD(1) + 1 ... MC33XS2410_PWM_MAX_PERIOD(0): + step =3D 0; + break; + } + + /* + * Round up here because a higher count results in a higher frequency + * and so a smaller period. + */ + count =3D DIV_ROUND_UP((u32)MC33XS2410_PWM_MAX_PERIOD(step), (u32)period); + return FIELD_PREP(MC33XS2410_PWM_FREQ_STEP, step) | + FIELD_PREP(MC33XS2410_PWM_FREQ_COUNT, count - 1); +} + +static u64 mc33xs2410_pwm_get_period(u8 reg) +{ + u32 freq, code, doubled_steps; + + /* + * steps: + * - 0 =3D 0.5Hz + * - 1 =3D 2Hz + * - 2 =3D 8Hz + * - 3 =3D 32Hz + * frequency =3D (code + 1) x steps. + * + * To avoid losing precision in case steps value is zero, scale the + * steps value for now by two and keep it in mind when calculating the + * period that the frequency had been doubled. + */ + doubled_steps =3D 1 << (FIELD_GET(MC33XS2410_PWM_FREQ_STEP, reg) * 2); + code =3D FIELD_GET(MC33XS2410_PWM_FREQ_COUNT, reg); + freq =3D (code + 1) * doubled_steps; + + /* Convert frequency to period, considering the doubled frequency. */ + return DIV_ROUND_UP(2 * NSEC_PER_SEC, freq); +} + +static int mc33xs2410_pwm_apply(struct pwm_chip *chip, struct pwm_device *= pwm, + const struct pwm_state *state) +{ + struct mc33xs2410_pwm *mc33xs2410 =3D mc33xs2410_from_chip(chip); + struct spi_device *spi =3D mc33xs2410->spi; + u8 reg[4] =3D { + MC33XS2410_PWM_FREQ(pwm->hwpwm + 1), + MC33XS2410_PWM_DC(pwm->hwpwm + 1), + MC33XS2410_PWM_CTRL1, + MC33XS2410_PWM_CTRL3 + }; + u64 period, duty_cycle; + int ret, rel_dc; + u16 rd_val[2]; + u8 wr_val[4]; + u8 mask; + + period =3D min(state->period, MC33XS2410_PWM_MAX_PERIOD(0)); + if (period < MC33XS2410_PWM_MIN_PERIOD) + return -EINVAL; + + ret =3D mc33xs2410_read_regs(spi, ®[2], MC33XS2410_FRAME_IN_DATA_RD, r= d_val, 2); + if (ret < 0) + return ret; + + /* frequency */ + wr_val[0] =3D mc33xs2410_pwm_get_freq(period); + /* Continue calculations with the possibly truncated period */ + period =3D mc33xs2410_pwm_get_period(wr_val[0]); + + /* duty cycle */ + duty_cycle =3D min(period, state->duty_cycle); + rel_dc =3D div64_u64(duty_cycle * 256, period) - 1; + if (rel_dc < 0) + wr_val[1] =3D 0; + else + wr_val[1] =3D rel_dc; + + /* polarity */ + mask =3D MC33XS2410_PWM_CTRL1_POL_INV(pwm->hwpwm + 1); + wr_val[2] =3D (state->polarity =3D=3D PWM_POLARITY_INVERSED) ? + (rd_val[0] | mask) : (rd_val[0] & ~mask); + + /* enable output */ + mask =3D MC33XS2410_PWM_CTRL3_EN(pwm->hwpwm + 1); + wr_val[3] =3D (state->enabled && rel_dc >=3D 0) ? (rd_val[1] | mask) : + (rd_val[1] & ~mask); + + return mc33xs2410_write_regs(spi, reg, wr_val, 4); +} + +static int mc33xs2410_pwm_get_state(struct pwm_chip *chip, + struct pwm_device *pwm, + struct pwm_state *state) +{ + struct mc33xs2410_pwm *mc33xs2410 =3D mc33xs2410_from_chip(chip); + struct spi_device *spi =3D mc33xs2410->spi; + u8 reg[4] =3D { + MC33XS2410_PWM_FREQ(pwm->hwpwm + 1), + MC33XS2410_PWM_DC(pwm->hwpwm + 1), + MC33XS2410_PWM_CTRL1, + MC33XS2410_PWM_CTRL3, + }; + u16 val[4]; + int ret; + + ret =3D mc33xs2410_read_regs(spi, reg, MC33XS2410_FRAME_IN_DATA_RD, val, = 4); + if (ret < 0) + return ret; + + state->period =3D mc33xs2410_pwm_get_period(val[0]); + state->polarity =3D (val[2] & MC33XS2410_PWM_CTRL1_POL_INV(pwm->hwpwm + 1= )) ? + PWM_POLARITY_INVERSED : PWM_POLARITY_NORMAL; + state->enabled =3D !!(val[3] & MC33XS2410_PWM_CTRL3_EN(pwm->hwpwm + 1)); + state->duty_cycle =3D DIV_ROUND_UP_ULL((val[1] + 1) * state->period, 256); + + return 0; +} + +static const struct pwm_ops mc33xs2410_pwm_ops =3D { + .apply =3D mc33xs2410_pwm_apply, + .get_state =3D mc33xs2410_pwm_get_state, +}; + +static int mc33xs2410_reset(struct device *dev) +{ + struct gpio_desc *reset_gpio; + + reset_gpio =3D devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); + if (IS_ERR_OR_NULL(reset_gpio)) + return PTR_ERR_OR_ZERO(reset_gpio); + + fsleep(1000); + gpiod_set_value_cansleep(reset_gpio, 0); + /* Wake-up time */ + fsleep(10000); + + return 0; +} + +static int mc33xs2410_probe(struct spi_device *spi) +{ + struct mc33xs2410_pwm *mc33xs2410; + struct device *dev =3D &spi->dev; + struct pwm_chip *chip; + int ret; + + chip =3D devm_pwmchip_alloc(dev, 4, sizeof(*mc33xs2410)); + if (IS_ERR(chip)) + return PTR_ERR(chip); + + spi->bits_per_word =3D 16; + spi->mode |=3D SPI_CS_WORD; + ret =3D spi_setup(spi); + if (ret < 0) + return ret; + + mc33xs2410 =3D mc33xs2410_from_chip(chip); + mc33xs2410->spi =3D spi; + chip->ops =3D &mc33xs2410_pwm_ops; + + ret =3D mc33xs2410_reset(dev); + if (ret) + return ret; + + /* + * Disable watchdog and keep in mind that the watchdog won't trigger a + * reset of the machine when running into an timeout, instead the + * control over the outputs is handed over to the INx input logic + * signals of the device. Disabling it here just deactivates this + * feature until a proper solution is found. + */ + ret =3D mc33xs2410_write_reg(spi, MC33XS2410_WDT, 0x0); + if (ret < 0) + return dev_err_probe(dev, ret, "Failed to disable watchdog\n"); + + /* Transition to normal mode */ + ret =3D mc33xs2410_modify_reg(spi, MC33XS2410_GLB_CTRL, + MC33XS2410_GLB_CTRL_MODE, + MC33XS2410_GLB_CTRL_MODE_NORMAL); + if (ret < 0) + return dev_err_probe(dev, ret, + "Failed to transition to normal mode\n"); + + ret =3D devm_pwmchip_add(dev, chip); + if (ret < 0) + return dev_err_probe(dev, ret, "Failed to add pwm chip\n"); + + return 0; +} + +static const struct spi_device_id mc33xs2410_spi_id[] =3D { + { "mc33xs2410" }, + { } +}; +MODULE_DEVICE_TABLE(spi, mc33xs2410_spi_id); + +static const struct of_device_id mc33xs2410_of_match[] =3D { + { .compatible =3D "nxp,mc33xs2410" }, + { } +}; +MODULE_DEVICE_TABLE(of, mc33xs2410_of_match); + +static struct spi_driver mc33xs2410_driver =3D { + .driver =3D { + .name =3D "mc33xs2410-pwm", + .of_match_table =3D mc33xs2410_of_match, + }, + .probe =3D mc33xs2410_probe, + .id_table =3D mc33xs2410_spi_id, +}; +module_spi_driver(mc33xs2410_driver); + +MODULE_DESCRIPTION("NXP MC33XS2410 high-side switch driver"); +MODULE_AUTHOR("Dimitri Fedrau "); +MODULE_LICENSE("GPL"); --=20 2.39.5