From nobody Sat Jun 20 06:07:13 2026 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (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 27E1B13957E for ; Sun, 19 Apr 2026 17:47:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776620836; cv=none; b=QXFki2aKrZch5LHdYskx29oDbvOWFC1LETPX0fnf8UySdCd+9UuB4VeOeo/rbVHB+MCkJ0by42LIkaW+y/gOsF8mnqGWFZG9KVHx1m3hnsfxLvdHyc0S5VxjpXipqx8OBlDcUeaAVyTGAAqfqhlIEqShFBE5e5927pOqmzm5am8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776620836; c=relaxed/simple; bh=+a4Fr0IjQQ/CYoI5Eys19WCJe3OBe+8XTQTbWlUDiu4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ackiiT0EBkpbRdDBcwnEEHhWeN2IaAOpMMt1vUDWdYZRyv0xRRUBxL+l757YwoPiLTO76UXMCOvosUaeNjzrX/MZrZOfT4CowRegmrCtFXzGQTsQ3IFjGftHwpuKsYU5IX+b6RwrfKfgFUYxYZZp6uudP+ZiMH1VpkfXP5BHTsM= 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=kt9jtqba; arc=none smtp.client-ip=209.85.216.48 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="kt9jtqba" Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-354a18c48b5so2229272a91.1 for ; Sun, 19 Apr 2026 10:47:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776620834; x=1777225634; 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=QYVJP+gR72z2Eu7RTUx+WXRO3NGvC74i7dV5FetC1js=; b=kt9jtqba0YDK8eQJZ644LeKNGQGM+CoNASYy/IQb7n84SxQQliZzn+wxCQrqkUe/D0 0xVol4t4971oNIxGgDTDH67/ly6UgyLCpDOrSwgaojJXl8nc4qLCubU62yrFIX12I5dM +v7ONGElU8jP6xjxd9Mv+CPcRGF9vapfQDdXxTC8EZthApVB0pslPLSZRU7oDIeKEtje QsWF+hLG+p9/PVEBWmxKQNr3+UPPc0vKUORMBOrbeTPmLLXCABAjHm6XgAh7e7tF+4x3 OswvK+X6A3roGXEjmEfY+vEUfZ89kD9eLZX0wn9CkS2SSnuGxAoY/lKnJELjjkpQtG2O Kfdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776620834; x=1777225634; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=QYVJP+gR72z2Eu7RTUx+WXRO3NGvC74i7dV5FetC1js=; b=I6QzEin6VWFt9AX2ilFqyRwuzLIP3bjkRbsg4qiGgt2gikT3oWU0OY8C6RqJJO3h9b MviIPAGRe3w3c7w7CM1rlXoLNwklFOtaBqOG5wnDip2leNI7cYINWbVCs/uWXiVJ9Tu6 oGr+nPFkWTQgP1MdSodA3pZf0iuACh7YHQExNlViQV1QSq1CZmbM1y2D1+fPHp0ckpE7 B/coXUGxyulVcnntvus+x9THpM6kstzp+zJa4Yb80vLGOOQl9QBQfraF2PCORvj7psQU z2+lWi6S8eALxU96SkqGfWvVQ+6UhcUohy/uvsB0YkMkp4e14wkZU0LzWOQ8fksK0UqV WB7A== X-Forwarded-Encrypted: i=1; AFNElJ9pGsC1JCRixxrsjzSTmeWmGsyo30E4Zse3DqWu+EPtwZUU4+xjQF1uwqSNRk85EJiLDsPvmhQLAhk76bo=@vger.kernel.org X-Gm-Message-State: AOJu0Yxy9XhSKGzRVeyvPcJyDHyP3WE2b73QaCSl2JmVY4+nQXkX00T8 5m/lB6j0K8aqBIFXBcmt2BNhr6YvPmN/KqkGqKDilqPPlWSFiJBceRf9 X-Gm-Gg: AeBDiev+zAZhX4Te3X2pbU+h6ENuPR9yKqZsBaZF8FHqn3/CLibyQzcVOm6rhYI/rK1 d34IOBSMOXiLi/g1GEHP1LkSBrnzQGOgpCIU/IjQODbeJXOqJBiWmeqCH9UmOsb56ryTGdYVdU6 Js8LvZSqSPiZHIjO0GFo8kOFmf2KP9KlFzMuYf0oCLIlxIh3mRxjbAAgYDv8y9F8CUvxEsdjj2E kMeP9BE0ve9vacuqqgYVMldwvc8Bqa+nNO0Zs6jx5sNWSFL+StziMCEN6c+kGSBocXuZLpIUqH5 e1ptQCr1Ep4Kf+8dR/dd0z7x4uvoxcmkCzuf4wg5rvf/OBpv9coCjmU1FHGFSrOlaDWJuwi+eeG RN6DAmDKfzdcYQdMkmL8a0UfW1DWPL0J5iLgQQF+xq2xORarakHiemR9yOjAILsZfuMVw1qccF9 7+TUqQzTYvqvBT6RK1RI6syYqI9pxYrhlFwi4GU1pjsgDYCRfGMsFj6nWgTkL4R1iI+7oq9sC31 sVNPpiOyyX84pOBUTRtr95zxG8A9rkE5cNrV9Q3IAeXzo4otTqjZpKgCXnFnw== X-Received: by 2002:a17:90b:5543:b0:35f:b227:db86 with SMTP id 98e67ed59e1d1-361403af6a5mr11018606a91.2.1776620834377; Sun, 19 Apr 2026 10:47:14 -0700 (PDT) Received: from lord-daniel-VivoBook-ASUSLaptop-K3502ZA-S3502ZA.. ([2405:201:31:d01f:1c1e:6c93:8067:7dee]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36140fe07c7sm7794875a91.5.2026.04.19.10.47.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Apr 2026 10:47:13 -0700 (PDT) From: Piyush Patle To: jic23@kernel.org, ak@it-klinger.de Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] dt-bindings: iio: adc: avia-hx711: add avia,hx710b compatible Date: Sun, 19 Apr 2026 23:16:39 +0530 Message-ID: <20260419174654.683692-2-piyushpatle228@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260419174654.683692-1-piyushpatle228@gmail.com> References: <20260419174654.683692-1-piyushpatle228@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 HX710B shares the same two-wire interface as the HX711 but differs in its channel and gain model: gain is fixed at 128 and the number of trailing PD_SCK pulses selects the input channel rather than the gain. Add avia,hx710b to the compatible enum. Document the chip differences in the description and add chip-specific supply properties (dvdd-supply, vsup-supply) for the HX711, a rate-gpios property for the HX711 RATE pin, and an allOf constraint that forbids HX711-only properties on HX710B nodes. Clarify the clock-frequency description to reflect its actual purpose: controlling the SCK bit-bang timing. Note that AVDD serves as the voltage reference on both chips so no separate vref-supply is needed. Signed-off-by: Piyush Patle --- Changes in v2: - Remove driver implementation details (bit-banging, channel-selection mechanics) from the description; describe hardware behaviour only - Drop unrelated punctuation cleanup (trailing period on PD_SCK description) - Add dvdd-supply and vsup-supply optional properties for HX711 - Add rate-gpios optional property for the HX711 RATE pin; forbid it on HX710B via the allOf if/then block - Add note to avdd-supply clarifying it serves as voltage reference on both chips (no separate vref-supply needed) - Add allOf if/then block forbidding HX711-only properties on HX710B - Fix clock-frequency description to reflect its true purpose (SCK bit-bang timing control, not a crystal/external clock frequency) - Sort compatible enum alphabetically (hx710b before hx711) - Remove redundant example comments (compatible already identifies part) - Update HX711 example to exercise the new rate-gpios property .../bindings/iio/adc/avia-hx711.yaml | 61 ++++++++++++++++--- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml b/Do= cumentation/devicetree/bindings/iio/adc/avia-hx711.yaml index 9c57eb13f892..35ac74a490b1 100644 --- a/Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +++ b/Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml @@ -4,24 +4,24 @@ $id: http://devicetree.org/schemas/iio/adc/avia-hx711.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# =20 -title: AVIA HX711 ADC chip for weight cells +title: AVIA HX711 and HX710B ADCs =20 maintainers: - Andreas Klinger =20 description: | - Bit-banging driver using two GPIOs: - - sck-gpio gives a clock to the sensor with 24 cycles for data retrieval - and up to 3 cycles for selection of the input channel and gain for the - next measurement - - dout-gpio is the sensor data the sensor responds to the clock + The HX711 is a 24-bit ADC with selectable gain (32/64/128) and two + differential input channels. Channel A supports gain 64 and 128; + channel B supports gain 32. =20 - Specifications about the driver can be found at: - http://www.aviaic.com/ENProducts.aspx + The HX710B is a 24-bit ADC with fixed gain of 128. Channel 0 is the + differential input and channel 1 measures the DVDD-AVDD supply + voltage difference. =20 properties: compatible: enum: + - avia,hx710b - avia,hx711 =20 sck-gpios: @@ -40,13 +40,45 @@ properties: =20 avdd-supply: description: - Definition of the regulator used as analog supply + Analog supply voltage (AVDD). Also serves as the voltage reference on + both chips; no separate vref-supply is required. + + dvdd-supply: + description: + Digital supply voltage (DVDD). HX711 only. + + vsup-supply: + description: + Supply voltage for the on-chip regulator (VSUP). HX711 only. + + rate-gpios: + description: + GPIO connected to the RATE pin (HX711 only). When driven low the + output data rate is 10 SPS; when driven high it is 80 SPS. If + omitted the RATE pin state is determined by the board wiring. + maxItems: 1 =20 clock-frequency: + description: + Controls the SCK bit-bang timing. The value is used to derive the + delay between SCK edges; keep the SCK high time below 60 us to + avoid triggering chip power-down mode. Defaults to 400 kHz if not + specified. minimum: 20000 maximum: 2500000 default: 400000 =20 +allOf: + - if: + properties: + compatible: + const: avia,hx710b + then: + properties: + vsup-supply: false + dvdd-supply: false + rate-gpios: false + required: - compatible - sck-gpios @@ -58,10 +90,19 @@ additionalProperties: false examples: - | #include - weight { + weight0 { compatible =3D "avia,hx711"; sck-gpios =3D <&gpio3 10 GPIO_ACTIVE_HIGH>; dout-gpios =3D <&gpio0 7 GPIO_ACTIVE_HIGH>; + rate-gpios =3D <&gpio0 9 GPIO_ACTIVE_HIGH>; avdd-supply =3D <&avdd>; clock-frequency =3D <100000>; }; + - | + #include + weight1 { + compatible =3D "avia,hx710b"; + sck-gpios =3D <&gpio3 11 GPIO_ACTIVE_HIGH>; + dout-gpios =3D <&gpio0 8 GPIO_ACTIVE_HIGH>; + avdd-supply =3D <&avdd>; + }; --=20 2.43.0 From nobody Sat Jun 20 06:07:13 2026 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 1F45728DC4 for ; Sun, 19 Apr 2026 17:47:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776620843; cv=none; b=fhu1mhpXNBM5e4XOdzVVHGAa+BTgNjAyoLYlFVdn1xYARtA43q3s1ZE3gyXU7n36bEt/wVUNGFXO9YuRQnfVg5cnZWWUVVa70i5nxRyh7Zx0C+6GOgHRy/8CZbv50FLwkUPQh5IgBEgNAkKV3G3H9F4JuU19Jdw9cbCSIH/XNbQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776620843; c=relaxed/simple; bh=IiZoDIW/qSPAQ62PeGLJSQDCBqPv8LFFgyiTz22xvWU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r1HsSsxScwM0XLwR7HNdH5M68/GiW7XKOG/mR3seU2bd4xon841Sulw96UQq6oWoyQNVvvYONvX7EexRsxTlXveEm/asnks7c/fDLOdp5vSsD+p9kbct+qcvKgkU+iTfsR6sp7q7hbXjNNU7h8J1MFIAWBZifQU4oeAerW1CG/8= 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=JdlPGdUz; arc=none smtp.client-ip=209.85.216.52 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="JdlPGdUz" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-356337f058aso1409085a91.2 for ; Sun, 19 Apr 2026 10:47:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776620840; x=1777225640; 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=WcvRYGZ1sJJEZCkKcoqV7JaYB5TeC/s9HV7XVT0QOXA=; b=JdlPGdUzgatsM4D+RuwfeFTmVjHdaJ654AycGUHTlJe2d+SgmfduLQGwjPfmH0dwxB sTrExBvHQBi6dckfk67GRt68qI/NQGalSl3sUpCVQ1XtagNQLDk816j48No4icsQCwiR 1boATx4cvmRcLKjsh1CpENHaYrZGQNW/YCGXXsRXMq5b4L00YogD9uh3ebF34ypMRcAv 9DVqzIDKqKZczTlGWBsmBRp4OYO4pCdZVntElodYJTRKkKtgz4Eic6jiISWdoGubRmm6 f/eqxt3+fmTgWSGhzF0MyM/AUhuYfrPn04dxGFNnUYajAdPozuiYoFAkpNxHzTv4t/oH YMDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776620840; x=1777225640; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=WcvRYGZ1sJJEZCkKcoqV7JaYB5TeC/s9HV7XVT0QOXA=; b=rSaxjuRgPx1Lt9OS4QO5EggAPm+8pMRV+GGIMnzsrz7gVMev/2JWOMSKfS9+6znV04 vXZWwEJVaL3+SA4R3OzQiaaQ2A8aYQrTSpunsdMKuAcGB4NubuZw7xXG4QFnPDqDJzlJ 2wJe8NboGMInpuZeptVJKNcQQLA1GdiEkionOFui64ikm883vLCtLbIE58gsQmarQ2QS mrjAoes14ru/LgxtdtfqFptVWJVhGAMZo8jo+v/lkyc9KiZAHCfa7q3ijyPtOmWxz7L+ DFuhZzuRsxuLXK5/NAACZnVolSVzZJCapbcDBPApoft15CRSYuC26xrUUxymmycC2OkV EToA== X-Forwarded-Encrypted: i=1; AFNElJ8nxOX3+DyfPYlcC3u+FG9n4TPXlTz5fAAz18j4GbIPYCRefzfGiB8d1DQ84F+aOA7uyY8PgjPHikfhp+o=@vger.kernel.org X-Gm-Message-State: AOJu0YwccAaEGGDwK+xROGQdZLehl69CI0jnHuVP75n4kDHPHfyC2V6N Td9cj8cY4BLH9nSpZ3mV4i/Fim6gSPWlvuyf/jimcU7xMbj+7c+2Ftek X-Gm-Gg: AeBDiet8tYlidXVUXllewL1mBpzISTkQU9N29/OGna5jKGSqI+huFPrJVC9Z1rUIunC G+L5TjZZbQuIvX3cE1Dn1WT1UhlhFtSCRmtfXJTjMpno+dV1X3M3rekpXrZ/EFsf/ktAFm76j/h OCGI+hJwGCzVME9j8zuh+AYWb0rQgUEIabvH/4MOnF470X92zM2PGYjCHatKvGIsLjcDDvYaiJg c7nD4Vqd+xqn5pio5s+RlvEVCbScKcQyG89SM0GB+lsF8Nss8hy5gKYiWJjoM25IRUGl99JXi4I GIxHNbsRtHmkveiatg9TANNxrea6Ss6jBrIzCheNEO9rtt6ipHwLw70c3x0wif7Qo/cf79OflQW 4/rf2GLPVDgws12JfOWlk6Tylx3Y/S682DR8Lt2/hzO4sS/D4bn/mRcoLy6CRRhTaznD0xDkWTV Td83PLRtdzODjB/6VvKKPjCEsmu6ohXhbEBmXe9zYpYEAsd1zBQsMLSLRLDGFOiKrS2BYSCUsyU 1yeOIAVwJxkHzIVyNTeD2kUD1MJnNIib0UCXR6FR78Qlc0m1h0= X-Received: by 2002:a17:90b:4c52:b0:35f:b647:d98a with SMTP id 98e67ed59e1d1-361403c3412mr11556760a91.5.1776620840354; Sun, 19 Apr 2026 10:47:20 -0700 (PDT) Received: from lord-daniel-VivoBook-ASUSLaptop-K3502ZA-S3502ZA.. ([2405:201:31:d01f:1c1e:6c93:8067:7dee]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36140fe07c7sm7794875a91.5.2026.04.19.10.47.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Apr 2026 10:47:19 -0700 (PDT) From: Piyush Patle To: jic23@kernel.org, ak@it-klinger.de Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] iio: adc: hx711: add support for HX710B Date: Sun, 19 Apr 2026 23:16:40 +0530 Message-ID: <20260419174654.683692-3-piyushpatle228@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260419174654.683692-1-piyushpatle228@gmail.com> References: <20260419174654.683692-1-piyushpatle228@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 HX711 uses trailing SCK pulses after each 24-bit conversion to select the channel and gain for the next measurement: 1 pulse gives channel A at gain 128, 2 pulses give channel B at gain 32, and 3 pulses give channel A at gain 64. The HX710B works differently: gain is fixed at 128 and the trailing pulses select only the channel. One trailing pulse selects the differential input (channel 0, 10 SPS) and two trailing pulses select the DVDD-AVDD supply monitor (channel 1, 40 SPS). Refactor the driver around a per-chip hx711_chip_info structure so both variants can share the same core. Each chip provides its own iio_chan_spec array and iio_info pointer. The HX710B stores per-channel trailing pulse counts in chan->address (1 for channel 0, 2 for channel 1) instead of a separate array. A bool fixed_gain flag and fixed_gain_val field in hx711_chip_info distinguish the fixed-gain path from the HX711's user-selectable gain path without conflating unrelated properties. The HX710B differential input channel is described with .differential=3D1 and .channel2=3D1 as required by the IIO ABI. Signed-off-by: Piyush Patle --- Changes in v2: - Fix pulse count bug: hx710b values were {25, 26} (total SCK cycles); corrected to {1, 2} (trailing-only, hx711_read() clocks 24 data bits) - Add .differential =3D 1 and .channel2 =3D 1 to HX710B channel 0 - Move trailing pulse counts from a separate array to chan->address (1 for ch0, 2 for ch1); remove chan_pulse_count / num_chan_pulses - Replace chan_pulse_count !=3D NULL tests with dedicated bool fixed_gain flag; add fixed_gain_val field to hx711_chip_info - Add const struct iio_info *iio_info to hx711_chip_info; probe sets indio_dev->info =3D chip_info->iio_info directly - Remove NULL check after device_get_match_data() - Remove reset_channel from hx711_chip_info (always 0; use literal) - Change hx711_reset_read() and hx710b_set_channel() to take const struct iio_chan_spec * instead of int chan - Revert hx711_data struct member alignment noise - Restore trailing blank line at end of file (unrelated removal reverted) - Sort of_device_id entries alphabetically (hx710b before hx711) - Expand commit message to explain HX711 vs HX710B trailing-pulse model - Restore file header to mention weight sensor modules drivers/iio/adc/Kconfig | 9 +- drivers/iio/adc/hx711.c | 206 ++++++++++++++++++++++++++++++++-------- 2 files changed, 174 insertions(+), 41 deletions(-) diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index 60038ae8dfc4..ddf981fa72a2 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -784,18 +784,21 @@ config HI8435 called hi8435. =20 config HX711 - tristate "AVIA HX711 ADC for weight cells" + tristate "AVIA HX711 and HX710B ADC" depends on GPIOLIB select IIO_BUFFER select IIO_TRIGGERED_BUFFER help - If you say yes here you get support for AVIA HX711 ADC which is used - for weigh cells + If you say yes here you get support for AVIA HX711 and HX710B ADCs + which are used for bridge sensors such as weigh cells. =20 This driver uses two GPIOs, one acts as the clock and controls the channel selection and gain, the other one is used for the measurement data =20 + The HX710B is a variant with fixed gain and a different channel + selection scheme. + Currently the raw value is read from the chip and delivered. To get an actual weight one needs to subtract the zero offset and multiply by a scale factor. diff --git a/drivers/iio/adc/hx711.c b/drivers/iio/adc/hx711.c index 1db8b68a8f64..b024fb341b2e 100644 --- a/drivers/iio/adc/hx711.c +++ b/drivers/iio/adc/hx711.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-or-later /* - * HX711: analog to digital converter for weight sensor module + * HX711/HX710B: ADC driver for weight sensor modules * * Copyright (c) 2016 Andreas Klinger */ @@ -76,12 +76,33 @@ static int hx711_get_scale_to_gain(int scale) return -EINVAL; } =20 +/** + * struct hx711_chip_info - per-variant static configuration + * @name: IIO device name + * @channels: channel specification + * @num_channels: number of channels + * @iio_info: IIO info ops for this variant + * @fixed_gain: true if the variant has a fixed ADC gain (e.g. HX710B) + * @fixed_gain_val: the fixed gain value used to compute scale (when fixed= _gain) + */ +struct hx711_chip_info { + const char *name; + const struct iio_chan_spec *channels; + int num_channels; + const struct iio_info *iio_info; + bool fixed_gain; + int fixed_gain_val; +}; + struct hx711_data { struct device *dev; struct gpio_desc *gpiod_pd_sck; struct gpio_desc *gpiod_dout; int gain_set; /* gain set on device */ int gain_chan_a; /* gain for channel A */ + int channel_set; /* HX710B current channel */ + int scale; /* HX710B fixed scale */ + const struct hx711_chip_info *chip_info; struct mutex lock; /* * triggered buffer @@ -92,10 +113,7 @@ struct hx711_data { aligned_s64 timestamp; } buffer; /* - * delay after a rising edge on SCK until the data is ready DOUT - * this is dependent on the hx711 where the datasheet tells a - * maximum value of 100 ns - * but also on potential parasitic capacities on the wiring + * Delay after SCK rising edge before sampling DOUT. */ u32 data_ready_delay_ns; u32 clock_frequency; @@ -139,7 +157,11 @@ static int hx711_cycle(struct hx711_data *hx711_data) return gpiod_get_value(hx711_data->gpiod_dout); } =20 -static int hx711_read(struct hx711_data *hx711_data) +/* + * Clock out 24 data bits and then send trailing pulses to select the + * next channel/gain state. + */ +static int hx711_read(struct hx711_data *hx711_data, int trailing_pulses) { int i, ret; int value =3D 0; @@ -158,7 +180,7 @@ static int hx711_read(struct hx711_data *hx711_data) =20 value ^=3D 0x800000; =20 - for (i =3D 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++) + for (i =3D 0; i < trailing_pulses; i++) hx711_cycle(hx711_data); =20 return value; @@ -188,6 +210,7 @@ static int hx711_wait_for_ready(struct hx711_data *hx71= 1_data) =20 static int hx711_reset(struct hx711_data *hx711_data) { + const struct hx711_chip_info *info =3D hx711_data->chip_info; int val =3D hx711_wait_for_ready(hx711_data); =20 if (val) { @@ -206,13 +229,17 @@ static int hx711_reset(struct hx711_data *hx711_data) =20 val =3D hx711_wait_for_ready(hx711_data); =20 - /* after a reset the gain is 128 */ - hx711_data->gain_set =3D HX711_RESET_GAIN; + /* Restore variant default after reset. */ + if (info->fixed_gain) + hx711_data->channel_set =3D 0; + else + hx711_data->gain_set =3D HX711_RESET_GAIN; } =20 return val; } =20 +/* Select HX711 channel/gain for the next conversion. */ static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int c= han) { int ret; @@ -221,7 +248,8 @@ static int hx711_set_gain_for_channel(struct hx711_data= *hx711_data, int chan) if (hx711_data->gain_set =3D=3D 32) { hx711_data->gain_set =3D hx711_data->gain_chan_a; =20 - ret =3D hx711_read(hx711_data); + ret =3D hx711_read(hx711_data, + hx711_get_gain_to_pulse(hx711_data->gain_set)); if (ret < 0) return ret; =20 @@ -233,7 +261,8 @@ static int hx711_set_gain_for_channel(struct hx711_data= *hx711_data, int chan) if (hx711_data->gain_set !=3D 32) { hx711_data->gain_set =3D 32; =20 - ret =3D hx711_read(hx711_data); + ret =3D hx711_read(hx711_data, + hx711_get_gain_to_pulse(hx711_data->gain_set)); if (ret < 0) return ret; =20 @@ -246,27 +275,50 @@ static int hx711_set_gain_for_channel(struct hx711_da= ta *hx711_data, int chan) return 0; } =20 -static int hx711_reset_read(struct hx711_data *hx711_data, int chan) +/* Select HX710B channel for the next conversion. */ +static int hx710b_set_channel(struct hx711_data *hx711_data, + const struct iio_chan_spec *chan) { int ret; - int val; =20 - /* - * hx711_reset() must be called from here - * because it could be calling hx711_read() by itself - */ + if (hx711_data->channel_set =3D=3D chan->channel) + return 0; + + hx711_data->channel_set =3D chan->channel; + + ret =3D hx711_read(hx711_data, chan->address); + if (ret < 0) + return ret; + + return hx711_wait_for_ready(hx711_data); +} + +static int hx711_reset_read(struct hx711_data *hx711_data, + const struct iio_chan_spec *chan) +{ + const struct hx711_chip_info *info =3D hx711_data->chip_info; + int trailing_pulses; + int ret; + + /* Reset first so the read starts from a known chip state. */ if (hx711_reset(hx711_data)) { dev_err(hx711_data->dev, "reset failed!"); return -EIO; } =20 - ret =3D hx711_set_gain_for_channel(hx711_data, chan); - if (ret < 0) - return ret; - - val =3D hx711_read(hx711_data); + if (info->fixed_gain) { + ret =3D hx710b_set_channel(hx711_data, chan); + if (ret < 0) + return ret; + trailing_pulses =3D chan->address; + } else { + ret =3D hx711_set_gain_for_channel(hx711_data, chan->channel); + if (ret < 0) + return ret; + trailing_pulses =3D hx711_get_gain_to_pulse(hx711_data->gain_set); + } =20 - return val; + return hx711_read(hx711_data, trailing_pulses); } =20 static int hx711_read_raw(struct iio_dev *indio_dev, @@ -274,12 +326,13 @@ static int hx711_read_raw(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct hx711_data *hx711_data =3D iio_priv(indio_dev); + const struct hx711_chip_info *info =3D hx711_data->chip_info; =20 switch (mask) { case IIO_CHAN_INFO_RAW: mutex_lock(&hx711_data->lock); =20 - *val =3D hx711_reset_read(hx711_data, chan->channel); + *val =3D hx711_reset_read(hx711_data, chan); =20 mutex_unlock(&hx711_data->lock); =20 @@ -290,7 +343,10 @@ static int hx711_read_raw(struct iio_dev *indio_dev, *val =3D 0; mutex_lock(&hx711_data->lock); =20 - *val2 =3D hx711_get_gain_to_scale(hx711_data->gain_set); + if (info->fixed_gain) + *val2 =3D hx711_data->scale; + else + *val2 =3D hx711_get_gain_to_scale(hx711_data->gain_set); =20 mutex_unlock(&hx711_data->lock); =20 @@ -332,7 +388,8 @@ static int hx711_write_raw(struct iio_dev *indio_dev, if (gain !=3D 32) hx711_data->gain_chan_a =3D gain; =20 - ret =3D hx711_read(hx711_data); + ret =3D hx711_read(hx711_data, + hx711_get_gain_to_pulse(hx711_data->gain_set)); if (ret < 0) { mutex_unlock(&hx711_data->lock); return ret; @@ -368,7 +425,7 @@ static irqreturn_t hx711_trigger(int irq, void *p) =20 iio_for_each_active_channel(indio_dev, i) { hx711_data->buffer.channel[j] =3D hx711_reset_read(hx711_data, - indio_dev->channels[i].channel); + &indio_dev->channels[i]); j++; } =20 @@ -423,6 +480,10 @@ static const struct iio_info hx711_iio_info =3D { .attrs =3D &hx711_attribute_group, }; =20 +static const struct iio_info hx710b_iio_info =3D { + .read_raw =3D hx711_read_raw, +}; + static const struct iio_chan_spec hx711_chan_spec[] =3D { { .type =3D IIO_VOLTAGE, @@ -455,10 +516,69 @@ static const struct iio_chan_spec hx711_chan_spec[] = =3D { IIO_CHAN_SOFT_TIMESTAMP(2), }; =20 +/* + * HX710B channels. + * Channel 0: differential input (IN+ vs IN-), 10 SPS, 1 trailing pulse. + * Channel 1: DVDD-AVDD supply monitor, 40 SPS, 2 trailing pulses. + * .address holds the trailing pulse count used by hx710b_set_channel(). + */ +static const struct iio_chan_spec hx710b_chan_spec[] =3D { + { + .type =3D IIO_VOLTAGE, + .differential =3D 1, + .channel =3D 0, + .channel2 =3D 1, + .indexed =3D 1, + .address =3D 1, + .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE), + .scan_index =3D 0, + .scan_type =3D { + .sign =3D 'u', + .realbits =3D 24, + .storagebits =3D 32, + .endianness =3D IIO_CPU, + }, + }, + { + .type =3D IIO_VOLTAGE, + .channel =3D 1, + .indexed =3D 1, + .address =3D 2, + .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE), + .scan_index =3D 1, + .scan_type =3D { + .sign =3D 'u', + .realbits =3D 24, + .storagebits =3D 32, + .endianness =3D IIO_CPU, + }, + }, + IIO_CHAN_SOFT_TIMESTAMP(2), +}; + +static const struct hx711_chip_info hx711_chip =3D { + .name =3D "hx711", + .channels =3D hx711_chan_spec, + .num_channels =3D ARRAY_SIZE(hx711_chan_spec), + .iio_info =3D &hx711_iio_info, +}; + +static const struct hx711_chip_info hx710b_chip =3D { + .name =3D "hx710b", + .channels =3D hx710b_chan_spec, + .num_channels =3D ARRAY_SIZE(hx710b_chan_spec), + .iio_info =3D &hx710b_iio_info, + .fixed_gain =3D true, + .fixed_gain_val =3D 128, +}; + static int hx711_probe(struct platform_device *pdev) { struct device *dev =3D &pdev->dev; struct hx711_data *hx711_data; + const struct hx711_chip_info *chip_info; struct iio_dev *indio_dev; int ret; int i; @@ -472,6 +592,9 @@ static int hx711_probe(struct platform_device *pdev) =20 mutex_init(&hx711_data->lock); =20 + chip_info =3D device_get_match_data(dev); + hx711_data->chip_info =3D chip_info; + /* * PD_SCK stands for power down and serial clock input of HX711 * in the driver it is an output @@ -510,12 +633,19 @@ static int hx711_probe(struct platform_device *pdev) /* we need 10^-9 mV */ ret *=3D 100; =20 - for (i =3D 0; i < HX711_GAIN_MAX; i++) - hx711_gain_to_scale[i].scale =3D - ret / hx711_gain_to_scale[i].gain / 1678; + if (chip_info->fixed_gain) { + /* Fixed-gain variant: compute scale once at probe. */ + hx711_data->scale =3D ret / chip_info->fixed_gain_val / 1678; + } else { + for (i =3D 0; i < HX711_GAIN_MAX; i++) + hx711_gain_to_scale[i].scale =3D + ret / hx711_gain_to_scale[i].gain / 1678; + + hx711_data->gain_set =3D 128; + hx711_data->gain_chan_a =3D 128; + } =20 - hx711_data->gain_set =3D 128; - hx711_data->gain_chan_a =3D 128; + indio_dev->info =3D chip_info->iio_info; =20 hx711_data->clock_frequency =3D 400000; ret =3D device_property_read_u32(&pdev->dev, "clock-frequency", @@ -533,11 +663,10 @@ static int hx711_probe(struct platform_device *pdev) hx711_data->data_ready_delay_ns =3D 1000000000 / hx711_data->clock_frequency; =20 - indio_dev->name =3D "hx711"; - indio_dev->info =3D &hx711_iio_info; + indio_dev->name =3D chip_info->name; indio_dev->modes =3D INDIO_DIRECT_MODE; - indio_dev->channels =3D hx711_chan_spec; - indio_dev->num_channels =3D ARRAY_SIZE(hx711_chan_spec); + indio_dev->channels =3D chip_info->channels; + indio_dev->num_channels =3D chip_info->num_channels; =20 ret =3D devm_iio_triggered_buffer_setup(dev, indio_dev, iio_pollfunc_store_time, @@ -554,7 +683,8 @@ static int hx711_probe(struct platform_device *pdev) } =20 static const struct of_device_id of_hx711_match[] =3D { - { .compatible =3D "avia,hx711", }, + { .compatible =3D "avia,hx710b", .data =3D &hx710b_chip }, + { .compatible =3D "avia,hx711", .data =3D &hx711_chip }, { } }; =20 @@ -571,7 +701,7 @@ static struct platform_driver hx711_driver =3D { module_platform_driver(hx711_driver); =20 MODULE_AUTHOR("Andreas Klinger "); -MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells"); +MODULE_DESCRIPTION("HX711/HX710B GPIO ADC driver"); MODULE_LICENSE("GPL"); MODULE_ALIAS("platform:hx711-gpio"); =20 --=20 2.43.0