From nobody Mon Sep 16 19:44:33 2024 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 83149762EB; Thu, 25 Jul 2024 19:46:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.11.138.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936775; cv=none; b=kD2B6+xaCw0KLSn/TIb6tNSws2SUCrWQL6t5hUoTRV6+HnbBGkuzCWewgSHf6Y+iD23sl6vXMIIU5/gkvTDLBDg5ccrJfvc6QHEYp3GeQzroTGC5vOI8dmxaZ1Xzm2WcxM6P0XpPUF5O7eVOpYjCauR9NtSEJwFYMaBpqrNwyyc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936775; c=relaxed/simple; bh=IP68vUdNzVSNudRdfpyMCb7gGfeDSlK8BmqYyXksQzg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BEXDSbcArf+94MSjPZYtWnfs8S+rX2Zk5EfXiX42aoOI4X6/u6KwTzcTwIvt7dBczUQVjc74up+4/bIlnJ32OpaX/z0IEqVo/udxy+bq3tiwx0styeep1Os0vrkC5WL3jHvapY3GfsH/UXJp4p+IH05wGui6S0vf3HOhTM0iJBg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de; spf=pass smtp.mailfrom=sntech.de; arc=none smtp.client-ip=185.11.138.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sntech.de Received: from i5e860cdd.versanet.de ([94.134.12.221] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sX4PO-0001rD-6Z; Thu, 25 Jul 2024 21:45:50 +0200 From: Heiko Stuebner To: lee@kernel.org, jdelvare@suse.com, linux@roeck-us.net, dmitry.torokhov@gmail.com, pavel@ucw.cz Cc: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, heiko@sntech.de, ukleinek@debian.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-hwmon@vger.kernel.org, linux-input@vger.kernel.org, linux-leds@vger.kernel.org Subject: [PATCH 1/7] dt-bindings: mfd: add binding for qnap,mcu devices Date: Thu, 25 Jul 2024 21:45:33 +0200 Message-Id: <20240725194539.1780790-2-heiko@sntech.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240725194539.1780790-1-heiko@sntech.de> References: <20240725194539.1780790-1-heiko@sntech.de> 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" These MCUs can be found in network attached storage devices made by QNAP. They are connected to a serial port of the host device and provide functionality like LEDs, power-control and temperature monitoring. Signed-off-by: Heiko Stuebner Reviewed-by: Krzysztof Kozlowski --- .../devicetree/bindings/mfd/qnap,mcu.yaml | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Documentation/devicetree/bindings/mfd/qnap,mcu.yaml diff --git a/Documentation/devicetree/bindings/mfd/qnap,mcu.yaml b/Document= ation/devicetree/bindings/mfd/qnap,mcu.yaml new file mode 100644 index 0000000000000..3b1ad8cb54da3 --- /dev/null +++ b/Documentation/devicetree/bindings/mfd/qnap,mcu.yaml @@ -0,0 +1,43 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/mfd/qnap,mcu.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: QNAP NAS on-board Microcontroller + +maintainers: + - Heiko Stuebner + +description: | + QNAP embeds a microcontroller on their NAS devices adding system feature + as PWM Fan control, additional LEDs, power button status and more. + +properties: + compatible: + enum: + - qnap,ts433-mcu + + "#cooling-cells": + const: 2 + + cooling-levels: + description: PWM duty cycle values corresponding to thermal cooling st= ates. + $ref: /schemas/types.yaml#/definitions/uint32-array + items: + maximum: 255 + +required: + - compatible + +additionalProperties: false + +examples: + - | + uart { + mcu { + compatible =3D "qnap,ts433-mcu"; + #cooling-cells =3D <2>; + cooling-levels =3D <0 64 89 128 166 204 221 238>; + }; + }; --=20 2.39.2 From nobody Mon Sep 16 19:44:33 2024 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B422770E2; Thu, 25 Jul 2024 19:46:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.11.138.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936777; cv=none; b=sLYLSDJ0F8dMExekcrfmVQtMPR0uRRIhnDWT41Ti7jzdpQSjH2PipB6Gfcg+pXMr5tQgjppQLrdI280L5ab/7sLWYXbdPlY8QQ2Lhqc+kUqVlx+3MoYLhxarGR13MsOGg4rvfiirQZl4OuIXn0X8sqjzmGOWJtpDgfrzUFwuLhQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936777; c=relaxed/simple; bh=ZpyhApMQb22/GhpSoYCzLcrMtx4K1hIsSejEpr8rl3E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oH3CA7zx+BCOX6LcT8Vbci3G98TRiKsnBo7CkzwDy3BE/NIMSeyNfiBUv81xJayxI6iskrBeKG7rPUW2yCngGOg6NH8kkGoHJJ2RVkyij7yz7a4epJ2Zq9MUqNDfUvQ3N1pqEwP4ILEIyP7s/JKwa2l/y5ir5cUo/uUMZyPjqUU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de; spf=pass smtp.mailfrom=sntech.de; arc=none smtp.client-ip=185.11.138.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sntech.de Received: from i5e860cdd.versanet.de ([94.134.12.221] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sX4PO-0001rD-Sf; Thu, 25 Jul 2024 21:45:50 +0200 From: Heiko Stuebner To: lee@kernel.org, jdelvare@suse.com, linux@roeck-us.net, dmitry.torokhov@gmail.com, pavel@ucw.cz Cc: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, heiko@sntech.de, ukleinek@debian.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-hwmon@vger.kernel.org, linux-input@vger.kernel.org, linux-leds@vger.kernel.org Subject: [PATCH 2/7] mfd: add base driver for qnap-mcu devices Date: Thu, 25 Jul 2024 21:45:34 +0200 Message-Id: <20240725194539.1780790-3-heiko@sntech.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240725194539.1780790-1-heiko@sntech.de> References: <20240725194539.1780790-1-heiko@sntech.de> 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" These MCUs are used in network-attached-storage devices made by QNAP and provide additional functionality to the system. This adds the base driver that implements the serial protocol via serdev and additionally hooks into the poweroff handlers to turn off the parts of the system not supplied by the general PMIC. Turning off (at least the TSx33 devices using Rockchip SoCs) is twofold. Turning off the MCU does not turn off the SoC and turning off the SoC does not turn off the hard-drives. And if the MCU is not turned off, the system also won't start again until it is unplugged from power. So on shutdown the MCU needs to be turned off before the general PMIC. The protocol spoken by the MCU is sadly not documented, but was obtained by listening to the chatter on the serial port, as thankfully the "hal_app" program from QNAPs firmware allows triggering all/most MCU actions from the command line. The implementation of how to talk to the serial device got some inspiration from the rave-sp servdev mfd. Signed-off-by: Heiko Stuebner --- drivers/mfd/Kconfig | 10 + drivers/mfd/Makefile | 2 + drivers/mfd/qnap-mcu.c | 356 +++++++++++++++++++++++++++++++++++ include/linux/mfd/qnap-mcu.h | 28 +++ 4 files changed, 396 insertions(+) create mode 100644 drivers/mfd/qnap-mcu.c create mode 100644 include/linux/mfd/qnap-mcu.h diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig index 266b4f54af603..bdd2d94d167cb 100644 --- a/drivers/mfd/Kconfig +++ b/drivers/mfd/Kconfig @@ -2306,6 +2306,16 @@ config MFD_INTEL_M10_BMC_PMCI additional drivers must be enabled in order to use the functionality of the device. =20 +config MFD_QNAP_MCU + tristate "QNAP MCU core driver" + depends on SERIAL_DEV_BUS + help + Select this to get support for the QNAP MCU device found in + several devices of QNAP network attached storages. + + It implements the base serial protocol to talk to the device + and provides functions for the other parts to hook into. + config MFD_RSMU_I2C tristate "Renesas Synchronization Management Unit with I2C" depends on I2C && OF diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile index c66f07edcd0e6..5c54ddd6b2f94 100644 --- a/drivers/mfd/Makefile +++ b/drivers/mfd/Makefile @@ -280,6 +280,8 @@ obj-$(CONFIG_MFD_INTEL_M10_BMC_PMCI) +=3D intel-m10-b= mc-pmci.o obj-$(CONFIG_MFD_ATC260X) +=3D atc260x-core.o obj-$(CONFIG_MFD_ATC260X_I2C) +=3D atc260x-i2c.o =20 +obj-$(CONFIG_MFD_QNAP_MCU) +=3D qnap-mcu.o + rsmu-i2c-objs :=3D rsmu_core.o rsmu_i2c.o rsmu-spi-objs :=3D rsmu_core.o rsmu_spi.o obj-$(CONFIG_MFD_RSMU_I2C) +=3D rsmu-i2c.o diff --git a/drivers/mfd/qnap-mcu.c b/drivers/mfd/qnap-mcu.c new file mode 100644 index 0000000000000..3a0f55ea41331 --- /dev/null +++ b/drivers/mfd/qnap-mcu.c @@ -0,0 +1,356 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * MFD core driver for the MCU in Qnap NAS devices that is connected + * via a dedicated UART port + * + * Copyright (C) 2024 Heiko Stuebner + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* The longest command found so far is 5 bytes long */ +#define QNAP_MCU_MAX_CMD_SIZE 5 +#define QNAP_MCU_MAX_DATA_SIZE 36 +#define QNAP_MCU_CHECKSUM_SIZE 1 + +#define QNAP_MCU_RX_BUFFER_SIZE \ + (QNAP_MCU_MAX_DATA_SIZE + QNAP_MCU_CHECKSUM_SIZE) + +#define QNAP_MCU_TX_BUFFER_SIZE \ + (QNAP_MCU_MAX_CMD_SIZE + QNAP_MCU_CHECKSUM_SIZE) + +/** + * struct qnap_mcu_reply - Reply to a command + * + * @data: Buffer to store reply payload in + * @length: Expected reply length, including the checksum + * @received: So far received number of bytes + * @done: Reply received completely + */ +struct qnap_mcu_reply { + u8 *data; + size_t length; + size_t received; + struct completion done; +}; + +/** + * struct qnap_mcu - QNAP NAS embedded controller + * + * @serdev: Pointer to underlying serdev + * @bus_lock: Lock to serialize access to the device + * @reply_lock: Lock protecting @reply + * @reply: Pointer to memory to store reply payload + * @variant: Device variant specific information + * @version: MCU firmware version + */ +struct qnap_mcu { + struct serdev_device *serdev; + struct mutex bus_lock; + struct mutex reply_lock; + struct qnap_mcu_reply *reply; + const struct qnap_mcu_variant *variant; + u8 version[4]; +}; + +/* + * The QNAP-MCU uses a basic XOR checksum. + * It is always the last byte and XORs the whole previous message. + */ +static u8 qnap_mcu_csum(const u8 *buf, size_t size) +{ + u8 csum =3D 0; + + while (size--) + csum ^=3D *buf++; + + return csum; +} + +static int qnap_mcu_write(struct qnap_mcu *sp, const u8 *data, u8 data_siz= e) +{ + unsigned char tx[QNAP_MCU_TX_BUFFER_SIZE]; + size_t length =3D data_size + QNAP_MCU_CHECKSUM_SIZE; + + if (WARN_ON(length > sizeof(tx))) + return -ENOMEM; + + memcpy(tx, data, data_size); + tx[data_size] =3D qnap_mcu_csum(data, data_size); + + print_hex_dump_debug("qnap-mcu tx: ", DUMP_PREFIX_NONE, + 16, 1, tx, length, false); + + return serdev_device_write(sp->serdev, tx, length, HZ); +} + +static size_t qnap_mcu_receive_buf(struct serdev_device *serdev, + const u8 *buf, size_t size) +{ + struct device *dev =3D &serdev->dev; + struct qnap_mcu *mcu =3D dev_get_drvdata(dev); + struct qnap_mcu_reply *reply =3D mcu->reply; + const u8 *src =3D buf; + const u8 *end =3D buf + size; + + mutex_lock(&mcu->reply_lock); + if (!reply) { + dev_warn(dev, "received %lu bytes, we were not waiting for\n", + size); + mutex_unlock(&mcu->reply_lock); + return size; + } + + while (src < end) { + reply->data[reply->received] =3D *src++; + reply->received++; + + if (reply->received =3D=3D reply->length) { + complete(&reply->done); + mutex_unlock(&mcu->reply_lock); + + /* + * We report the consumed number of bytes. If there + * are still bytes remaining (though there shouldn't) + * the serdev layer will re-execute this handler with + * the remainder of the Rx bytes. + */ + return src - buf; + } + } + + /* + * The only way to get out of the above loop and end up here + * is through consuming all of the supplied data, so here we + * report that we processed it all. + */ + mutex_unlock(&mcu->reply_lock); + return size; +} + +static const struct serdev_device_ops qnap_mcu_serdev_device_ops =3D { + .receive_buf =3D qnap_mcu_receive_buf, + .write_wakeup =3D serdev_device_write_wakeup, +}; + +int qnap_mcu_exec(struct qnap_mcu *mcu, + u8 *cmd_data, size_t cmd_data_size, + u8 *reply_data, size_t reply_data_size) +{ + unsigned char rx[QNAP_MCU_RX_BUFFER_SIZE]; + size_t length =3D reply_data_size + QNAP_MCU_CHECKSUM_SIZE; + struct qnap_mcu_reply reply =3D { + .data =3D rx, + .length =3D length, + .received =3D 0, + .done =3D COMPLETION_INITIALIZER_ONSTACK(reply.done), + }; + int ret; + + if (WARN_ON(length > sizeof(rx))) + return -ENOMEM; + + mutex_lock(&mcu->bus_lock); + + mutex_lock(&mcu->reply_lock); + mcu->reply =3D &reply; + mutex_unlock(&mcu->reply_lock); + + qnap_mcu_write(mcu, cmd_data, cmd_data_size); + + if (!wait_for_completion_timeout(&reply.done, + msecs_to_jiffies(500))) { + dev_err(&mcu->serdev->dev, "Command timeout\n"); + ret =3D -ETIMEDOUT; + } else { + u8 crc =3D qnap_mcu_csum(rx, reply_data_size); + + print_hex_dump_debug("qnap-mcu rx: ", DUMP_PREFIX_NONE, + 16, 1, rx, length, false); + + if (crc !=3D rx[reply_data_size]) { + dev_err(&mcu->serdev->dev, + "Checksum 0x%02x wrong for data\n", crc); + ret =3D -EIO; + } else { + memcpy(reply_data, rx, reply_data_size); + ret =3D 0; + } + } + + mutex_lock(&mcu->reply_lock); + mcu->reply =3D NULL; + mutex_unlock(&mcu->reply_lock); + + mutex_unlock(&mcu->bus_lock); + return ret; +} +EXPORT_SYMBOL_GPL(qnap_mcu_exec); + +int qnap_mcu_exec_with_ack(struct qnap_mcu *mcu, + u8 *cmd_data, size_t cmd_data_size) +{ + u8 ack[2]; + int ret; + + ret =3D qnap_mcu_exec(mcu, cmd_data, cmd_data_size, ack, sizeof(ack)); + if (ret) + return ret; + + /* Should return @0 */ + if (ack[0] !=3D 0x40 || ack[1] !=3D 0x30) { + dev_err(&mcu->serdev->dev, "Did not receive ack\n"); + return -EIO; + } + + return 0; +} +EXPORT_SYMBOL_GPL(qnap_mcu_exec_with_ack); + +const struct qnap_mcu_variant *qnap_mcu_get_variant_data(struct qnap_mcu *= mcu) +{ + return mcu->variant; +} +EXPORT_SYMBOL_GPL(qnap_mcu_get_variant_data); + +static int qnap_mcu_get_version(struct qnap_mcu *mcu) +{ + u8 cmd[] =3D { + [0] =3D 0x25, /* % */ + [1] =3D 0x56 /* V */ + }; + u8 rx[14]; + int ret; + + ret =3D qnap_mcu_exec(mcu, cmd, sizeof(cmd), rx, 6); + if (ret) + return ret; + + memcpy(mcu->version, &rx[2], 4); + + return 0; +} + +/* + * The MCU controls power to the peripherals but not the CPU. + * + * So using the pmic to power off the system keeps the MCU and hard-drives + * running. This also then prevents the system from turning back on until + * the MCU is turned off by unplugging the power-cable. + * Turning off the MCU alone on the other hand turns off the hard-drives, + * LEDs, etc while the main SoC stays running - including its network port= s. + */ +static int qnap_mcu_power_off(struct sys_off_data *data) +{ + struct qnap_mcu *mcu =3D data->cb_data; + int ret; + u8 cmd[] =3D { + [0] =3D 0x40, /* @ */ + [1] =3D 0x43, /* C */ + [2] =3D 0x30 /* 0 */ + }; + + dev_dbg(&mcu->serdev->dev, "running MCU poweroff\n"); + ret =3D qnap_mcu_exec_with_ack(mcu, cmd, sizeof(cmd)); + if (ret) { + dev_err(&mcu->serdev->dev, "MCU poweroff failed %d\n", ret); + return NOTIFY_STOP; + } + + return NOTIFY_DONE; +} + +static const struct qnap_mcu_variant qnap_ts433_mcu =3D { + .baud_rate =3D 115200, + .num_drives =3D 4, + .fan_pwm_min =3D 51, /* specified in original model.conf */ + .fan_pwm_max =3D 255, + .usb_led =3D true, +}; + +static const struct of_device_id qnap_mcu_dt_ids[] =3D { + { .compatible =3D "qnap,ts433-mcu", .data =3D &qnap_ts433_mcu }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, qnap_mcu_dt_ids); + +static const struct mfd_cell qnap_mcu_subdevs[] =3D { + { .name =3D "qnap-mcu-input", }, + { .name =3D "qnap-mcu-leds", }, + { .name =3D "qnap-mcu-hwmon", } +}; + +static int qnap_mcu_probe(struct serdev_device *serdev) +{ + struct device *dev =3D &serdev->dev; + struct qnap_mcu *mcu; + int ret; + + mcu =3D devm_kzalloc(dev, sizeof(*mcu), GFP_KERNEL); + if (!mcu) + return -ENOMEM; + + mcu->serdev =3D serdev; + dev_set_drvdata(dev, mcu); + + mcu->variant =3D of_device_get_match_data(dev); + if (!mcu->variant) + return -ENODEV; + + mutex_init(&mcu->bus_lock); + mutex_init(&mcu->reply_lock); + + serdev_device_set_client_ops(serdev, &qnap_mcu_serdev_device_ops); + ret =3D devm_serdev_device_open(dev, serdev); + if (ret) + return ret; + + serdev_device_set_baudrate(serdev, mcu->variant->baud_rate); + serdev_device_set_flow_control(serdev, false); + + ret =3D serdev_device_set_parity(serdev, SERDEV_PARITY_NONE); + if (ret) { + dev_err(dev, "Failed to set parity\n"); + return ret; + } + + ret =3D qnap_mcu_get_version(mcu); + if (ret) + return ret; + + ret =3D devm_register_sys_off_handler(dev, + SYS_OFF_MODE_POWER_OFF_PREPARE, + SYS_OFF_PRIO_FIRMWARE, + &qnap_mcu_power_off, mcu); + if (ret) + return dev_err_probe(dev, ret, + "failed to register poweroff handler\n"); + + ret =3D devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, qnap_mcu_subdevs, + ARRAY_SIZE(qnap_mcu_subdevs), NULL, 0, NULL); + if (ret) + return dev_err_probe(dev, ret, "adding qnap mfd devices failed\n"); + + return 0; +} + +static struct serdev_device_driver qnap_mcu_drv =3D { + .probe =3D qnap_mcu_probe, + .driver =3D { + .name =3D "qnap-mcu", + .of_match_table =3D qnap_mcu_dt_ids, + }, +}; +module_serdev_device_driver(qnap_mcu_drv); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Heiko Stuebner "); +MODULE_DESCRIPTION("QNAP MCU core driver"); diff --git a/include/linux/mfd/qnap-mcu.h b/include/linux/mfd/qnap-mcu.h new file mode 100644 index 0000000000000..a71d753705546 --- /dev/null +++ b/include/linux/mfd/qnap-mcu.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ + +/* + * Core definitions for QNAP MCU MFD driver. + * Copyright (C) 2024 Heiko Stuebner + */ + +#ifndef _LINUX_QNAP_MCU_H_ +#define _LINUX_QNAP_MCU_H_ + +struct qnap_mcu; + +struct qnap_mcu_variant { + u32 baud_rate; + int num_drives; + int fan_pwm_min; + int fan_pwm_max; + bool usb_led; +}; + +int qnap_mcu_exec(struct qnap_mcu *mcu, + u8 *cmd_data, size_t cmd_data_size, + u8 *reply_data, size_t reply_data_size); +int qnap_mcu_exec_with_ack(struct qnap_mcu *mcu, + u8 *cmd_data, size_t cmd_data_size); +const struct qnap_mcu_variant *qnap_mcu_get_variant_data(struct qnap_mcu *= mcu); + +#endif /* _LINUX_QNAP_MCU_H_ */ --=20 2.39.2 From nobody Mon Sep 16 19:44:33 2024 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B45D770E9; Thu, 25 Jul 2024 19:46:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.11.138.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936776; cv=none; b=dGiVAhCslh41KpeISN2xjUzOvXBh7UHqzn2Z5hUu6VlkAOI2tCBottY5GHy+WsNMgZRW+1vI6uoJ5cvfXY7c9X1RLKhrlJBM2GGURGB7UtlhOHtzQQtT4kZBVP0LiZWI4hetzs++djEDk9lelIW4nOrNE/+kHWoGJ+5GGwZP5XI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936776; c=relaxed/simple; bh=74eT7WXELLC1eLlDhUZ75V9CtrrFrenF4GK9UTOAuMI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XPEfhrDEqZ2vcI5G8s8XO2m25UEyYTY1HWojkvIey1RWAEQGBPVWmnJvD74+GnLg3DclaXsBTWVEokZrmiM+CuaBHWoXSOV/kDAIwrhhsJuwxl6cejsjplpKvD2EGU2/fr7ZLWiFPNDJC+jFG59kpazN+eprw0ZziarpmD9Bhg4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de; spf=pass smtp.mailfrom=sntech.de; arc=none smtp.client-ip=185.11.138.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sntech.de Received: from i5e860cdd.versanet.de ([94.134.12.221] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sX4PP-0001rD-Iv; Thu, 25 Jul 2024 21:45:51 +0200 From: Heiko Stuebner To: lee@kernel.org, jdelvare@suse.com, linux@roeck-us.net, dmitry.torokhov@gmail.com, pavel@ucw.cz Cc: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, heiko@sntech.de, ukleinek@debian.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-hwmon@vger.kernel.org, linux-input@vger.kernel.org, linux-leds@vger.kernel.org Subject: [PATCH 3/7] leds: add driver for LEDs from qnap-mcu devices Date: Thu, 25 Jul 2024 21:45:35 +0200 Message-Id: <20240725194539.1780790-4-heiko@sntech.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240725194539.1780790-1-heiko@sntech.de> References: <20240725194539.1780790-1-heiko@sntech.de> 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" This adds a driver that connects to the qnap-mcu mfd driver and provides access to the LEDs on it. Signed-off-by: Heiko Stuebner --- drivers/leds/Kconfig | 11 ++ drivers/leds/Makefile | 1 + drivers/leds/leds-qnap-mcu.c | 247 +++++++++++++++++++++++++++++++++++ 3 files changed, 259 insertions(+) create mode 100644 drivers/leds/leds-qnap-mcu.c diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index 05e6af88b88cd..e128a8016b84c 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -555,6 +555,17 @@ config LEDS_PCA995X LED driver chips accessed via the I2C bus. Supported devices include PCA9955BTW, PCA9952TW and PCA9955TW. =20 +config LEDS_QNAP_MCU + tristate "LED Support for QNAP MCU controllers" + depends on LEDS_CLASS + depends on MFD_QNAP_MCU + help + This option enables support for LEDs available on embedded + controllers used in QNAP NAS devices. + + This driver can also be built as a module. If so, the module + will be called qnap-mcu-leds. + config LEDS_WM831X_STATUS tristate "LED support for status LEDs on WM831x PMICs" depends on LEDS_CLASS diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index effdfc6f1e951..27eb6cd827610 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -77,6 +77,7 @@ obj-$(CONFIG_LEDS_PCA995X) +=3D leds-pca995x.o obj-$(CONFIG_LEDS_PM8058) +=3D leds-pm8058.o obj-$(CONFIG_LEDS_POWERNV) +=3D leds-powernv.o obj-$(CONFIG_LEDS_PWM) +=3D leds-pwm.o +obj-$(CONFIG_LEDS_QNAP_MCU) +=3D leds-qnap-mcu.o obj-$(CONFIG_LEDS_REGULATOR) +=3D leds-regulator.o obj-$(CONFIG_LEDS_SC27XX_BLTC) +=3D leds-sc27xx-bltc.o obj-$(CONFIG_LEDS_SUN50I_A100) +=3D leds-sun50i-a100.o diff --git a/drivers/leds/leds-qnap-mcu.c b/drivers/leds/leds-qnap-mcu.c new file mode 100644 index 0000000000000..e3244923759d2 --- /dev/null +++ b/drivers/leds/leds-qnap-mcu.c @@ -0,0 +1,247 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Driver for LEDs found on QNAP MCU devices + * + * Copyright (C) 2024 Heiko Stuebner + */ + +#include +#include +#include +#include +#include +#include + +enum qnap_mcu_err_led_mode { + QNAP_MCU_ERR_LED_ON =3D 0, + QNAP_MCU_ERR_LED_OFF =3D 1, + QNAP_MCU_ERR_LED_BLINK_FAST =3D 2, + QNAP_MCU_ERR_LED_BLINK_SLOW =3D 3, +}; + +struct qnap_mcu_err_led { + struct qnap_mcu *mcu; + struct led_classdev cdev; + u8 num; + u8 mode; +}; + +static inline struct qnap_mcu_err_led * + cdev_to_qnap_mcu_err_led(struct led_classdev *led_cdev) +{ + return container_of(led_cdev, struct qnap_mcu_err_led, cdev); +} + +static int qnap_mcu_err_led_set(struct led_classdev *led_cdev, + enum led_brightness value) +{ + struct qnap_mcu_err_led *err_led =3D cdev_to_qnap_mcu_err_led(led_cdev); + u8 cmd[] =3D { + [0] =3D 0x40, + [1] =3D 0x52, + [2] =3D 0x30 + err_led->num, + [3] =3D 0x30 + }; + + /* + * If the led is off, turn it on. Otherwise don't disturb + * a possible set blink-mode. + */ + if (value =3D=3D 0) + err_led->mode =3D QNAP_MCU_ERR_LED_OFF; + else if (err_led->mode =3D=3D QNAP_MCU_ERR_LED_OFF) + err_led->mode =3D QNAP_MCU_ERR_LED_ON; + + cmd[3] =3D 0x30 + err_led->mode; + + return qnap_mcu_exec_with_ack(err_led->mcu, cmd, sizeof(cmd)); +} + +static int qnap_mcu_err_led_blink_set(struct led_classdev *led_cdev, + unsigned long *delay_on, + unsigned long *delay_off) +{ + struct qnap_mcu_err_led *err_led =3D cdev_to_qnap_mcu_err_led(led_cdev); + u8 cmd[] =3D { + [0] =3D 0x40, + [1] =3D 0x52, + [2] =3D 0x30 + err_led->num, + [3] =3D 0x30 + }; + + /* LED is off, nothing to do */ + if (err_led->mode =3D=3D QNAP_MCU_ERR_LED_OFF) + return 0; + + if (*delay_on < 500) { + *delay_on =3D 100; + *delay_off =3D 100; + err_led->mode =3D QNAP_MCU_ERR_LED_BLINK_FAST; + } else { + *delay_on =3D 500; + *delay_off =3D 500; + err_led->mode =3D QNAP_MCU_ERR_LED_BLINK_SLOW; + } + + cmd[3] =3D 0x30 + err_led->mode; + + return qnap_mcu_exec_with_ack(err_led->mcu, cmd, sizeof(cmd)); +} + +static int qnap_mcu_register_err_led(struct device *dev, struct qnap_mcu *= mcu, int num) +{ + struct qnap_mcu_err_led *err_led; + char tmp_buf[LED_MAX_NAME_SIZE]; + int ret; + + err_led =3D devm_kzalloc(dev, sizeof(*err_led), GFP_KERNEL); + if (!err_led) + return -ENOMEM; + + err_led->mcu =3D mcu; + err_led->num =3D num; + err_led->mode =3D QNAP_MCU_ERR_LED_OFF; + + snprintf(tmp_buf, LED_MAX_NAME_SIZE, "hdd%d:red:status", num + 1); + err_led->cdev.name =3D tmp_buf; + + err_led->cdev.brightness_set_blocking =3D qnap_mcu_err_led_set; + err_led->cdev.blink_set =3D qnap_mcu_err_led_blink_set; + err_led->cdev.brightness =3D 0; + err_led->cdev.max_brightness =3D 1; + + ret =3D devm_led_classdev_register(dev, &err_led->cdev); + if (ret) + return dev_err_probe(dev, ret, "failed to register hdd led %d", num); + + return qnap_mcu_err_led_set(&err_led->cdev, 0); +} + +enum qnap_mcu_usb_led_mode { + QNAP_MCU_USB_LED_ON =3D 1, + QNAP_MCU_USB_LED_OFF =3D 3, + QNAP_MCU_USB_LED_BLINK =3D 2, +}; + +struct qnap_mcu_usb_led { + struct qnap_mcu *mcu; + struct led_classdev cdev; + u8 mode; +}; + +static inline struct qnap_mcu_usb_led * + cdev_to_qnap_mcu_usb_led(struct led_classdev *led_cdev) +{ + return container_of(led_cdev, struct qnap_mcu_usb_led, cdev); +} + +static int qnap_mcu_usb_led_set(struct led_classdev *led_cdev, + enum led_brightness value) +{ + struct qnap_mcu_usb_led *usb_led =3D cdev_to_qnap_mcu_usb_led(led_cdev); + u8 cmd[] =3D { + [0] =3D 0x40, + [1] =3D 0x43, + [2] =3D 0 + }; + + /* + * If the led is off, turn it on. Otherwise don't disturb + * a possible set blink-mode. + */ + if (value =3D=3D 0) + usb_led->mode =3D QNAP_MCU_USB_LED_OFF; + else if (usb_led->mode =3D=3D QNAP_MCU_USB_LED_OFF) + usb_led->mode =3D QNAP_MCU_USB_LED_ON; + + /* byte 3 is shared between the usb led target and setting the mode */ + cmd[2] =3D 0x44 | usb_led->mode; + + return qnap_mcu_exec_with_ack(usb_led->mcu, cmd, sizeof(cmd)); +} + +static int qnap_mcu_usb_led_blink_set(struct led_classdev *led_cdev, + unsigned long *delay_on, + unsigned long *delay_off) +{ + struct qnap_mcu_usb_led *usb_led =3D cdev_to_qnap_mcu_usb_led(led_cdev); + u8 cmd[] =3D { + [0] =3D 0x40, + [1] =3D 0x43, + [2] =3D 0 + }; + + /* LED is off, nothing to do */ + if (usb_led->mode =3D=3D QNAP_MCU_USB_LED_OFF) + return 0; + + *delay_on =3D 250; + *delay_off =3D 250; + usb_led->mode =3D QNAP_MCU_USB_LED_BLINK; + + /* byte 3 is shared between the usb led target and setting the mode */ + cmd[2] =3D 0x44 | usb_led->mode; + + return qnap_mcu_exec_with_ack(usb_led->mcu, cmd, sizeof(cmd)); +} + +static int qnap_mcu_register_usb_led(struct device *dev, struct qnap_mcu *= mcu) +{ + struct qnap_mcu_usb_led *usb_led; + int ret; + + usb_led =3D devm_kzalloc(dev, sizeof(*usb_led), GFP_KERNEL); + if (!usb_led) + return -ENOMEM; + + usb_led->mcu =3D mcu; + usb_led->mode =3D QNAP_MCU_USB_LED_OFF; + usb_led->cdev.name =3D "usb:blue:disk"; + usb_led->cdev.brightness_set_blocking =3D qnap_mcu_usb_led_set; + usb_led->cdev.blink_set =3D qnap_mcu_usb_led_blink_set; + usb_led->cdev.brightness =3D 0; + usb_led->cdev.max_brightness =3D 1; + + ret =3D devm_led_classdev_register(dev, &usb_led->cdev); + if (ret) + return dev_err_probe(dev, ret, "failed to register usb led"); + + return qnap_mcu_usb_led_set(&usb_led->cdev, 0); +} + +static int qnap_mcu_leds_probe(struct platform_device *pdev) +{ + struct qnap_mcu *mcu =3D dev_get_drvdata(pdev->dev.parent); + const struct qnap_mcu_variant *variant =3D qnap_mcu_get_variant_data(mcu); + int ret, i; + + for (i =3D 0; i < variant->num_drives; i++) { + ret =3D qnap_mcu_register_err_led(&pdev->dev, mcu, i); + if (ret) + return dev_err_probe(&pdev->dev, ret, + "failed to register error led %d\n", i); + } + + if (variant->usb_led) { + ret =3D qnap_mcu_register_usb_led(&pdev->dev, mcu); + if (ret) + return dev_err_probe(&pdev->dev, ret, + "failed to register usb led %d\n", i); + } + + return 0; +} + +static struct platform_driver qnap_mcu_leds_driver =3D { + .probe =3D qnap_mcu_leds_probe, + .driver =3D { + .name =3D "qnap-mcu-leds", + }, +}; +module_platform_driver(qnap_mcu_leds_driver); + +MODULE_ALIAS("platform:qnap-mcu-leds"); +MODULE_AUTHOR("Heiko Stuebner "); +MODULE_DESCRIPTION("QNAP MCU LEDs driver"); +MODULE_LICENSE("GPL"); --=20 2.39.2 From nobody Mon Sep 16 19:44:33 2024 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3643A13AD23; Thu, 25 Jul 2024 19:46:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.11.138.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936777; cv=none; b=l4WHsCvHRuoOVOWnS+UEpgBKlXyzIWFQCWjUNRiSu9X4XjH+uNluSqkX+SWD9PVIFEcz/nPLza0Mdz3ttkq0B8prlHz4BsmFt1mse5bMK1mOEb9qwtOm9kippd9kipDWRTK3xrGAE7FiDVSb3W5CO9+69Ufl7/qzqnFeoaLPhoA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936777; c=relaxed/simple; bh=+tAV/jdwNu/c42NsW1/DDe58gft+TQdAXYr918JtTps=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lfUgjtUtiTV3LqMSHTIdPSl+/sHZVlS+18k2Sbyy9FZ0xO8baD5sgi2WoSUEl/mfoCmfKIpM32As60ffCJlOgBtAyzNYd071v+aX8dbYbD0naUwGEHa3mD7b1448JlRper25KVk+ZEUUQcDDOQELjXtdCsotLNlvIWoGCqyQ/LQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de; spf=pass smtp.mailfrom=sntech.de; arc=none smtp.client-ip=185.11.138.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sntech.de Received: from i5e860cdd.versanet.de ([94.134.12.221] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sX4PQ-0001rD-7y; Thu, 25 Jul 2024 21:45:52 +0200 From: Heiko Stuebner To: lee@kernel.org, jdelvare@suse.com, linux@roeck-us.net, dmitry.torokhov@gmail.com, pavel@ucw.cz Cc: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, heiko@sntech.de, ukleinek@debian.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-hwmon@vger.kernel.org, linux-input@vger.kernel.org, linux-leds@vger.kernel.org Subject: [PATCH 4/7] Input: add driver for the input part of qnap-mcu devices Date: Thu, 25 Jul 2024 21:45:36 +0200 Message-Id: <20240725194539.1780790-5-heiko@sntech.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240725194539.1780790-1-heiko@sntech.de> References: <20240725194539.1780790-1-heiko@sntech.de> 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 MCU controls the power-button and beeper, so expose them as input device. There is of course no interrupt line, so the status of the power-button needs to be polled. To generate an event the power-button also needs to be held for 1-2 seconds, so the polling interval does not need to be overly fast. Signed-off-by: Heiko Stuebner --- drivers/input/misc/Kconfig | 12 +++ drivers/input/misc/Makefile | 1 + drivers/input/misc/qnap-mcu-input.c | 156 ++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 drivers/input/misc/qnap-mcu-input.c diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig index 6ba984d7f0b18..4ab8fe8301635 100644 --- a/drivers/input/misc/Kconfig +++ b/drivers/input/misc/Kconfig @@ -900,6 +900,18 @@ config INPUT_HISI_POWERKEY To compile this driver as a module, choose M here: the module will be called hisi_powerkey. =20 +config INPUT_QNAP_MCU + tristate "Input Support for QNAP MCU controllers" + depends on MFD_QNAP_MCU + help + This option enables support for input elements available on + embedded controllers used in QNAP NAS devices. + + This includes a polled power-button as well as a beeper. + + To compile this driver as a module, choose M here: the + module will be called qnap-mcu-input. + config INPUT_RAVE_SP_PWRBUTTON tristate "RAVE SP Power button Driver" depends on RAVE_SP_CORE diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile index 04296a4abe8e8..05f5d0072b08f 100644 --- a/drivers/input/misc/Makefile +++ b/drivers/input/misc/Makefile @@ -66,6 +66,7 @@ obj-$(CONFIG_INPUT_PMIC8XXX_PWRKEY) +=3D pmic8xxx-pwrkey.o obj-$(CONFIG_INPUT_POWERMATE) +=3D powermate.o obj-$(CONFIG_INPUT_PWM_BEEPER) +=3D pwm-beeper.o obj-$(CONFIG_INPUT_PWM_VIBRA) +=3D pwm-vibra.o +obj-$(CONFIG_INPUT_QNAP_MCU) +=3D qnap-mcu-input.o obj-$(CONFIG_INPUT_RAVE_SP_PWRBUTTON) +=3D rave-sp-pwrbutton.o obj-$(CONFIG_INPUT_RB532_BUTTON) +=3D rb532_button.o obj-$(CONFIG_INPUT_REGULATOR_HAPTIC) +=3D regulator-haptic.o diff --git a/drivers/input/misc/qnap-mcu-input.c b/drivers/input/misc/qnap-= mcu-input.c new file mode 100644 index 0000000000000..9bac7ea2c6b80 --- /dev/null +++ b/drivers/input/misc/qnap-mcu-input.c @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Driver for input events on QNAP-MCUs + * + * Copyright (C) 2024 Heiko Stuebner + */ + +#include +#include +#include +#include +#include +#include + +/* + * The power-key needs to be pressed for a while to create an event, + * so there is no use for overly frequent polling. + */ +#define POLL_INTERVAL 500 + +struct qnap_mcu_input_dev { + struct input_dev *input; + struct qnap_mcu *mcu; + struct device *dev; + + struct work_struct beep_work; + int beep_type; +}; + +static void qnap_mcu_input_poll(struct input_dev *input) +{ + struct qnap_mcu_input_dev *idev =3D input_get_drvdata(input); + u8 cmd[] =3D { + [0] =3D 0x40, /* @ */ + [1] =3D 0x43, /* C */ + [2] =3D 0x56 /* V */ + }; + u8 reply[4]; + int state, ret; + + /* poll the power button */ + ret =3D qnap_mcu_exec(idev->mcu, cmd, sizeof(cmd), reply, sizeof(reply)); + if (ret) + return; + + /* First bytes must mirror the sent command */ + if (memcmp(cmd, reply, sizeof(cmd))) { + dev_err(idev->dev, "malformed data received\n"); + return; + } + + state =3D reply[3] - 0x30; + input_event(input, EV_KEY, KEY_POWER, state); + input_sync(input); +} + +static void qnap_mcu_input_beeper_work(struct work_struct *work) +{ + struct qnap_mcu_input_dev *idev =3D + container_of(work, struct qnap_mcu_input_dev, beep_work); + u8 cmd[] =3D { + [0] =3D 0x40, /* @ */ + [1] =3D 0x43, /* C */ + [2] =3D (idev->beep_type =3D=3D SND_TONE) ? 0x33 : 0x32 + }; + + qnap_mcu_exec_with_ack(idev->mcu, cmd, sizeof(cmd)); +} + +static int qnap_mcu_input_event(struct input_dev *input, unsigned int type, + unsigned int code, int value) +{ + struct qnap_mcu_input_dev *idev =3D input_get_drvdata(input); + + if (type !=3D EV_SND || (code !=3D SND_BELL && code !=3D SND_TONE)) + return -EOPNOTSUPP; + + if (value < 0) + return -EINVAL; + + /* beep runtime is determined by the MCU */ + if (value =3D=3D 0) + return 0; + + /* Schedule work to actually turn the beeper on */ + idev->beep_type =3D code; + schedule_work(&idev->beep_work); + + return 0; +} + +static int qnap_mcu_input_probe(struct platform_device *pdev) +{ + struct qnap_mcu *mcu =3D dev_get_drvdata(pdev->dev.parent); + struct qnap_mcu_input_dev *idev; + struct device *dev =3D &pdev->dev; + struct input_dev *input; + int ret; + + idev =3D devm_kzalloc(dev, sizeof(*idev), GFP_KERNEL); + if (!idev) + return -ENOMEM; + + input =3D devm_input_allocate_device(dev); + if (!input) + return dev_err_probe(dev, -ENOMEM, "no memory for input device\n"); + + idev->input =3D input; + idev->dev =3D dev; + idev->mcu =3D mcu; + + input_set_drvdata(input, idev); + + input->name =3D "qnap-mcu"; + input->phys =3D "qnap-mcu-input/input0"; + input->id.bustype =3D BUS_HOST; + input->id.vendor =3D 0x0001; + input->id.product =3D 0x0001; + input->id.version =3D 0x0100; + input->event =3D qnap_mcu_input_event; + + input_set_capability(input, EV_KEY, KEY_POWER); + input_set_capability(input, EV_SND, SND_BELL); + input_set_capability(input, EV_SND, SND_TONE); + + INIT_WORK(&idev->beep_work, qnap_mcu_input_beeper_work); + + ret =3D input_setup_polling(input, qnap_mcu_input_poll); + if (ret) + return dev_err_probe(dev, ret, "unable to set up polling\n"); + + input_set_poll_interval(input, POLL_INTERVAL); + + ret =3D input_register_device(input); + if (ret) + return dev_err_probe(dev, ret, "unable to register input device\n"); + + qnap_mcu_input_poll(input); + input_sync(input); + + return 0; +} + +static struct platform_driver qnap_mcu_input_driver =3D { + .probe =3D qnap_mcu_input_probe, + .driver =3D { + .name =3D "qnap-mcu-input", + }, +}; +module_platform_driver(qnap_mcu_input_driver); + +MODULE_ALIAS("platform:qnap-mcu-input"); +MODULE_AUTHOR("Heiko Stuebner "); +MODULE_DESCRIPTION("QNAP MCU input driver"); +MODULE_LICENSE("GPL"); --=20 2.39.2 From nobody Mon Sep 16 19:44:33 2024 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B4B9770F5; Thu, 25 Jul 2024 19:46:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.11.138.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936776; cv=none; b=deyW7DDtA6Zf3QemVXyIV6LqKeIHKlnnQJ9J4KrYn4ZCVeS24gpFqh2RVcKJrkL5kNQTHSpgADYt8tvuCHw+gLcdRJpR2f+OffXTHIOhUZS3If7JIA5U8FsLH1ykx96UyQ1Yo9GxU10ljmlB40BnRTltzAIDYury/w5aXYOnX+M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936776; c=relaxed/simple; bh=zfvUzOXtPa+5vynVkr2zPv4EPX+LZXeREtTWR71I2Pc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XwJO07vRLyMGWB0hV/qB9eIZ6h99YWxgLcWTU6WjIawdDioQp2ngWWiYyK6MMLRTFPxK6eavQYxQKDeuiOFhIjhuyz6TLLPV1o92H3LAZXEf3wzNu7YGIYDLllbae1WNat1JTYSNd9YxEGoRHRHxHhD3r8XPOWRYym9b5J8W4wg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de; spf=pass smtp.mailfrom=sntech.de; arc=none smtp.client-ip=185.11.138.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sntech.de Received: from i5e860cdd.versanet.de ([94.134.12.221] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sX4PQ-0001rD-Td; Thu, 25 Jul 2024 21:45:52 +0200 From: Heiko Stuebner To: lee@kernel.org, jdelvare@suse.com, linux@roeck-us.net, dmitry.torokhov@gmail.com, pavel@ucw.cz Cc: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, heiko@sntech.de, ukleinek@debian.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-hwmon@vger.kernel.org, linux-input@vger.kernel.org, linux-leds@vger.kernel.org Subject: [PATCH 5/7] hwmon: add driver for the hwmon parts of qnap-mcu devices Date: Thu, 25 Jul 2024 21:45:37 +0200 Message-Id: <20240725194539.1780790-6-heiko@sntech.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240725194539.1780790-1-heiko@sntech.de> References: <20240725194539.1780790-1-heiko@sntech.de> 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 MCU can be found on network-attached-storage devices made by QNAP and provides access to fan control including reading back its RPM as well as reading the temperature of the NAS case. Signed-off-by: Heiko Stuebner --- drivers/hwmon/Kconfig | 12 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/qnap-mcu-hwmon.c | 352 +++++++++++++++++++++++++++++++++ 3 files changed, 365 insertions(+) create mode 100644 drivers/hwmon/qnap-mcu-hwmon.c diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index e14ae18a973b3..87b6ee463a967 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1806,6 +1806,18 @@ config SENSORS_PWM_FAN This driver can also be built as a module. If so, the module will be called pwm-fan. =20 +config SENSORS_QNAP_MCU_HWMON + tristate "QNAP MCU hardware monitoring" + depends on MFD_QNAP_MCU + depends on THERMAL || THERMAL=3Dn + help + Say yes here to enable support for fans and temperatures sensors + connected to a QNAP MCU, as found in a number of QNAP network + attached storage devices. + + This driver can also be built as a module. If so, the module + will be called qnap-mcu-hwmon. + config SENSORS_RASPBERRYPI_HWMON tristate "Raspberry Pi voltage monitor" depends on RASPBERRYPI_FIRMWARE || (COMPILE_TEST && !RASPBERRYPI_FIRMWARE) diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index e3f25475d1f04..b72c6a059e4af 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile @@ -188,6 +188,7 @@ obj-$(CONFIG_SENSORS_POWERZ) +=3D powerz.o obj-$(CONFIG_SENSORS_POWR1220) +=3D powr1220.o obj-$(CONFIG_SENSORS_PT5161L) +=3D pt5161l.o obj-$(CONFIG_SENSORS_PWM_FAN) +=3D pwm-fan.o +obj-$(CONFIG_SENSORS_QNAP_MCU_HWMON) +=3D qnap-mcu-hwmon.o obj-$(CONFIG_SENSORS_RASPBERRYPI_HWMON) +=3D raspberrypi-hwmon.o obj-$(CONFIG_SENSORS_SBTSI) +=3D sbtsi_temp.o obj-$(CONFIG_SENSORS_SBRMI) +=3D sbrmi.o diff --git a/drivers/hwmon/qnap-mcu-hwmon.c b/drivers/hwmon/qnap-mcu-hwmon.c new file mode 100644 index 0000000000000..c4925ece165e2 --- /dev/null +++ b/drivers/hwmon/qnap-mcu-hwmon.c @@ -0,0 +1,352 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Driver for hwmon elements found on QNAP-MCU devices + * + * Copyright (C) 2024 Heiko Stuebner + */ + +#include +#include +#include +#include +#include +#include + +struct qnap_mcu_hwmon { + struct qnap_mcu *mcu; + struct device *dev; + + unsigned int pwm_min; + unsigned int pwm_max; + + unsigned int pwm_value; + bool pwm_enabled; + + unsigned int fan_state; + unsigned int fan_max_state; + unsigned int *fan_cooling_levels; + + struct thermal_cooling_device *cdev; + struct hwmon_chip_info info; +}; + +static int qnap_mcu_hwmon_get_rpm(struct qnap_mcu_hwmon *hwm) +{ + u8 cmd[] =3D { + [0] =3D 0x40, /* @ */ + [1] =3D 0x46, /* F */ + [2] =3D 0x41 /* A */ + }; + u8 reply[6]; + int ret; + + /* poll the fan rpm */ + ret =3D qnap_mcu_exec(hwm->mcu, cmd, sizeof(cmd), reply, sizeof(reply)); + if (ret) + return ret; + + /* First 2 bytes must mirror the sent command */ + if (memcmp(cmd, reply, 2)) { + dev_err(hwm->dev, "malformed data received\n"); + return -EIO; + } + + return reply[4] * 450; +} + +static int qnap_mcu_hwmon_update_fan(struct qnap_mcu_hwmon *hwm) +{ + u8 cmd[] =3D { + [0] =3D 0x40, /* @ */ + [1] =3D 0x46, /* F */ + [2] =3D 0x57, /* W */ + [3] =3D 0x30, /* 0 ... fan-id? */ + [4] =3D hwm->pwm_enabled ? hwm->pwm_value : 0 + }; + + /* poll the fan rpm */ + return qnap_mcu_exec_with_ack(hwm->mcu, cmd, sizeof(cmd)); +} + +static int qnap_mcu_hwmon_get_temp(struct qnap_mcu_hwmon *hwm) +{ + u8 cmd[] =3D { + [0] =3D 0x40, /* @ */ + [1] =3D 0x54, /* T */ + [2] =3D 0x33 /* 3 */ + }; + u8 reply[4]; + int ret; + + /* poll the fan rpm */ + ret =3D qnap_mcu_exec(hwm->mcu, cmd, sizeof(cmd), reply, sizeof(reply)); + if (ret) + return ret; + + /* First bytes must mirror the sent command */ + if (memcmp(cmd, reply, sizeof(cmd))) { + dev_err(hwm->dev, "malformed data received\n"); + return -EIO; + } + + /* + * There is an unknown bit set in bit7. + * Bits [6:0] report the actual temperature as returned by the + * original qnap firmware-tools, so just drop bit7 for now. + */ + return reply[3] & 0x7f; +} + +static int qnap_mcu_hwmon_write(struct device *dev, enum hwmon_sensor_type= s type, + u32 attr, int channel, long val) +{ + struct qnap_mcu_hwmon *hwm =3D dev_get_drvdata(dev); + + switch (attr) { + case hwmon_pwm_input: + if (val < hwm->pwm_min || val > hwm->pwm_max) + return -EINVAL; + + hwm->pwm_value =3D val; + return qnap_mcu_hwmon_update_fan(hwm); + case hwmon_pwm_enable: + if (val < 0 || val > 1) + return -EINVAL; + + hwm->pwm_enabled =3D val; + return qnap_mcu_hwmon_update_fan(hwm); + default: + return -EOPNOTSUPP; + } + + return 0; +} + +static int qnap_mcu_hwmon_read(struct device *dev, enum hwmon_sensor_types= type, + u32 attr, int channel, long *val) +{ + struct qnap_mcu_hwmon *hwm =3D dev_get_drvdata(dev); + + switch (type) { + case hwmon_pwm: + switch (attr) { + case hwmon_pwm_input: + *val =3D hwm->pwm_value; + return 0; + case hwmon_pwm_enable: + *val =3D hwm->pwm_enabled; + return 0; + } + return -EOPNOTSUPP; + case hwmon_fan: + *val =3D qnap_mcu_hwmon_get_rpm(hwm); + return 0; + case hwmon_temp: + *val =3D qnap_mcu_hwmon_get_temp(hwm); + return 0; + default: + return -EOPNOTSUPP; + } +} + +static umode_t qnap_mcu_hwmon_is_visible(const void *data, + enum hwmon_sensor_types type, + u32 attr, int channel) +{ + switch (type) { + case hwmon_temp: + return 0444; + + case hwmon_pwm: + return 0644; + + case hwmon_fan: + return 0444; + + default: + return 0; + } +} + +static const struct hwmon_ops qnap_mcu_hwmon_hwmon_ops =3D { + .is_visible =3D qnap_mcu_hwmon_is_visible, + .read =3D qnap_mcu_hwmon_read, + .write =3D qnap_mcu_hwmon_write, +}; + +/* thermal cooling device callbacks */ +static int qnap_mcu_hwmon_get_max_state(struct thermal_cooling_device *cde= v, + unsigned long *state) +{ + struct qnap_mcu_hwmon *hwm =3D cdev->devdata; + + if (!hwm) + return -EINVAL; + + *state =3D hwm->fan_max_state; + + return 0; +} + +static int qnap_mcu_hwmon_get_cur_state(struct thermal_cooling_device *cde= v, + unsigned long *state) +{ + struct qnap_mcu_hwmon *hwm =3D cdev->devdata; + + if (!hwm) + return -EINVAL; + + *state =3D hwm->fan_state; + + return 0; +} + +static int qnap_mcu_hwmon_set_cur_state(struct thermal_cooling_device *cde= v, + unsigned long state) +{ + struct qnap_mcu_hwmon *hwm =3D cdev->devdata; + int ret; + + if (!hwm || (state > hwm->fan_max_state)) + return -EINVAL; + + if (state =3D=3D hwm->fan_state) + return 0; + + if (hwm->fan_cooling_levels[state]) { + hwm->pwm_enabled =3D 1; + hwm->pwm_value =3D hwm->fan_cooling_levels[state]; + } else { + hwm->pwm_enabled =3D 0; + } + + ret =3D qnap_mcu_hwmon_update_fan(hwm); + if (ret) + return ret; + + hwm->fan_state =3D state; + + return ret; +} + +static const struct thermal_cooling_device_ops qnap_mcu_hwmon_cooling_ops = =3D { + .get_max_state =3D qnap_mcu_hwmon_get_max_state, + .get_cur_state =3D qnap_mcu_hwmon_get_cur_state, + .set_cur_state =3D qnap_mcu_hwmon_set_cur_state, +}; + +static int qnap_mcu_hwmon_get_cooling_data(struct device *dev, struct qnap= _mcu_hwmon *hwm) +{ + int num, i, ret; + + if (!device_property_present(dev->parent, "cooling-levels")) + return 0; + + ret =3D device_property_count_u32(dev->parent, "cooling-levels"); + if (ret <=3D 0) { + dev_err(dev, "Wrong data!\n"); + return ret ? : -EINVAL; + } + + num =3D ret; + hwm->fan_cooling_levels =3D devm_kcalloc(dev, num, sizeof(u32), + GFP_KERNEL); + if (!hwm->fan_cooling_levels) + return -ENOMEM; + + ret =3D device_property_read_u32_array(dev->parent, "cooling-levels", + hwm->fan_cooling_levels, num); + if (ret) { + dev_err(dev, "Property 'cooling-levels' cannot be read!\n"); + return ret; + } + + for (i =3D 0; i < num; i++) { + if (hwm->fan_cooling_levels[i] > hwm->pwm_max) { + dev_err(dev, "fan state[%d]:%d > %d\n", i, + hwm->fan_cooling_levels[i], hwm->pwm_max); + return -EINVAL; + } + } + + hwm->fan_max_state =3D num - 1; + + return 0; +} + +static const struct hwmon_channel_info * const qnap_mcu_hwmon_channels[] = =3D { + HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_ENABLE), + HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT), + HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT), + NULL +}; + +static int qnap_mcu_hwmon_probe(struct platform_device *pdev) +{ + struct qnap_mcu *mcu =3D dev_get_drvdata(pdev->dev.parent); + const struct qnap_mcu_variant *variant =3D qnap_mcu_get_variant_data(mcu); + struct qnap_mcu_hwmon *hwm; + struct thermal_cooling_device *cdev; + struct device *dev =3D &pdev->dev; + struct device *hwmon; + int ret; + + hwm =3D devm_kzalloc(dev, sizeof(*hwm), GFP_KERNEL); + if (!hwm) + return -ENOMEM; + + hwm->mcu =3D mcu; + hwm->dev =3D &pdev->dev; + hwm->pwm_min =3D variant->fan_pwm_min; + hwm->pwm_max =3D variant->fan_pwm_max; + + platform_set_drvdata(pdev, hwm); + + /* + * Set duty cycle to maximum allowed and enable PWM output as well as + * the regulator. In case of error nothing is changed + */ + hwm->pwm_value =3D hwm->pwm_max; + hwm->pwm_enabled =3D 1; + qnap_mcu_hwmon_update_fan(hwm); + + hwm->info.ops =3D &qnap_mcu_hwmon_hwmon_ops; + hwm->info.info =3D qnap_mcu_hwmon_channels; + + hwmon =3D devm_hwmon_device_register_with_info(dev, "qnapmcu", + hwm, &hwm->info, NULL); + if (IS_ERR(hwmon)) + return dev_err_probe(dev, PTR_ERR(hwmon), "Failed to register hwmon devi= ce\n"); + + ret =3D qnap_mcu_hwmon_get_cooling_data(dev, hwm); + if (ret) + return ret; + + hwm->fan_state =3D hwm->fan_max_state; + + if (IS_ENABLED(CONFIG_THERMAL)) { + cdev =3D devm_thermal_of_cooling_device_register(dev, + dev->parent->of_node, "qnap-mcu-hwmon", + hwm, &qnap_mcu_hwmon_cooling_ops); + if (IS_ERR(cdev)) + return dev_err_probe(dev, PTR_ERR(cdev), + "Failed to register qnap-mcu-hwmon as cooling device\n"); + hwm->cdev =3D cdev; + } + + return 0; +} + +static struct platform_driver qnap_mcu_hwmon_driver =3D { + .probe =3D qnap_mcu_hwmon_probe, + .driver =3D { + .name =3D "qnap-mcu-hwmon", + }, +}; +module_platform_driver(qnap_mcu_hwmon_driver); + +MODULE_ALIAS("platform:qnap-mcu-hwmon"); +MODULE_AUTHOR("Heiko Stuebner "); +MODULE_DESCRIPTION("QNAP MCU hwmon driver"); +MODULE_LICENSE("GPL"); --=20 2.39.2 From nobody Mon Sep 16 19:44:33 2024 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5019A60B96; Thu, 25 Jul 2024 19:46:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.11.138.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936774; cv=none; b=kWQyN/4+Pu+0Rg6U4xEHhBAD/PMdqHWj6cViQ4ZbSOylIYFCH1v6I5JjOo/kLPkQDN3diR+UDSGaNC81NtJBHI0dGdL4xnw0M9zHgDmhiYo0rBA+txh1X0x2VqmdAEZdiGQ9YEI1gpak2O9WAdyP/iwR7GtmTX0/0nRb6n2aV/Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936774; c=relaxed/simple; bh=09KivGoGc4Pv0IQGH9j0x6dXZZIgfM7SGRm7MhgvkAI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BHayLzaljaNaev2CeYGcHlLP4R6I7n9YMOm3jzfcqDjnYwg8uzFG4u99EenPxDRzbgxYuq+hJ2ZNAy+EPX3+bts3l/Ol69nqIUPnEMmDjIj5cguvgXdpEIKaXGW8lazdCmvQxHmYsRLR5YuAX4Jd0zqTQy/M+/AdC+zs6BRT9Ak= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de; spf=pass smtp.mailfrom=sntech.de; arc=none smtp.client-ip=185.11.138.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sntech.de Received: from i5e860cdd.versanet.de ([94.134.12.221] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sX4PR-0001rD-Ie; Thu, 25 Jul 2024 21:45:53 +0200 From: Heiko Stuebner To: lee@kernel.org, jdelvare@suse.com, linux@roeck-us.net, dmitry.torokhov@gmail.com, pavel@ucw.cz Cc: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, heiko@sntech.de, ukleinek@debian.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-hwmon@vger.kernel.org, linux-input@vger.kernel.org, linux-leds@vger.kernel.org Subject: [PATCH 6/7] arm64: dts: rockchip: hook up the MCU on the QNAP TS433 Date: Thu, 25 Jul 2024 21:45:38 +0200 Message-Id: <20240725194539.1780790-7-heiko@sntech.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240725194539.1780790-1-heiko@sntech.de> References: <20240725194539.1780790-1-heiko@sntech.de> 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 MCU is an important part of the device functionality. It provides functionality like fan-control, more leds, etc and even more important without it, the NAS-device cannot even fully turned off. Hook up the serial device to its uart and hook into the thermal management to control the fan according to the cpu temperature. While the MCU also provides a temperature sensor for the case, this one is just polled and does not provide functionality for handling trip points in hardware, so a lot of polling would be involved. As the cpu is only cooled passively in these devices, it's temperature rising will indicate the temperature level of the system just earlier. Signed-off-by: Heiko Stuebner --- .../boot/dts/rockchip/rk3568-qnap-ts433.dts | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/arch/arm64/boot/dts/rockchip/rk3568-qnap-ts433.dts b/arch/arm6= 4/boot/dts/rockchip/rk3568-qnap-ts433.dts index 6c4269b3d9597..bff21684a3970 100644 --- a/arch/arm64/boot/dts/rockchip/rk3568-qnap-ts433.dts +++ b/arch/arm64/boot/dts/rockchip/rk3568-qnap-ts433.dts @@ -465,6 +465,54 @@ rgmii_phy0: ethernet-phy@0 { }; }; =20 +/* + * The MCU can provide system temperature too, but only by polling and of + * course also cannot set trip points. So attach to the cpu thermal-zone + * instead to control the fan. + */ +&cpu_thermal { + trips { + case_fan0: case-fan0 { + hysteresis =3D <2000>; + temperature =3D <35000>; + type =3D "active"; + }; + + case_fan1: case-fan1 { + hysteresis =3D <2000>; + temperature =3D <45000>; + type =3D "active"; + }; + + case_fan2: case-fan2 { + hysteresis =3D <2000>; + temperature =3D <65000>; + type =3D "active"; + }; + }; + + cooling-maps { + /* + * Always provide some air movement, due to small case + * full of harddrives. + */ + map1 { + cooling-device =3D <&fan THERMAL_NO_LIMIT 1>; + trip =3D <&case_fan0>; + }; + + map2 { + cooling-device =3D <&fan 2 3>; + trip =3D <&case_fan1>; + }; + + map3 { + cooling-device =3D <&fan 4 THERMAL_NO_LIMIT>; + trip =3D <&case_fan2>; + }; + }; +}; + &pcie30phy { data-lanes =3D <1 2>; status =3D "okay"; @@ -563,6 +611,12 @@ &tsadc { */ &uart0 { status =3D "okay"; + + fan: mcu { + compatible =3D "qnap,ts433-mcu"; + #cooling-cells =3D <2>; + cooling-levels =3D <0 64 89 128 166 204 221 238>; + }; }; =20 /* --=20 2.39.2 From nobody Mon Sep 16 19:44:33 2024 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67FDB6F2EA; Thu, 25 Jul 2024 19:46:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.11.138.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936774; cv=none; b=p+tb7wcCxO2oXnwmuBiSsOyTzLtgbkJSuuOHDSEkyoAqHG8L9Dn4yLwbkFh5s+XUXshkvsO09p6h7b7JES2WsDczmwfog9Lx1Sjd/pyruqfhQDz2Rypi8xOlXjQjBHRVnFo0cBBlZjBKMw49AN6YO1gRMuLnRCVz54UCYH4H2eE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721936774; c=relaxed/simple; bh=fkrMzGswzCO7CPU3pzXIY4sgrQ/Z3eYGrtwNukVb2dM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Zv3Hnbk37+dENHaiuVSUo7xlnkCe71Jr4VxusRJ1tzS5ysBwO1QGDGUQExlooudQovXK9jZ50X4jVUjKvrc5BCRgwJd1FePXnEBiipxhja0CawQtgqAI4pHlCOemd9JJck4oG7FMUIg3r9mYFYe8g1zVnZJKCKDE3lYkOGb+kb0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de; spf=pass smtp.mailfrom=sntech.de; arc=none smtp.client-ip=185.11.138.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sntech.de Received: from i5e860cdd.versanet.de ([94.134.12.221] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sX4PS-0001rD-Bx; Thu, 25 Jul 2024 21:45:54 +0200 From: Heiko Stuebner To: lee@kernel.org, jdelvare@suse.com, linux@roeck-us.net, dmitry.torokhov@gmail.com, pavel@ucw.cz Cc: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, heiko@sntech.de, ukleinek@debian.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-hwmon@vger.kernel.org, linux-input@vger.kernel.org, linux-leds@vger.kernel.org Subject: [PATCH 7/7] arm64: dts: rockchip: set hdd led labels on qnap-ts433 Date: Thu, 25 Jul 2024 21:45:39 +0200 Message-Id: <20240725194539.1780790-8-heiko@sntech.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240725194539.1780790-1-heiko@sntech.de> References: <20240725194539.1780790-1-heiko@sntech.de> 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 automatically generated names for the LEDs from color and function do not match nicely for the 4 hdds, so set them manually per the label property to also match the LEDs generated from the MCU. Signed-off-by: Heiko Stuebner --- arch/arm64/boot/dts/rockchip/rk3568-qnap-ts433.dts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/arm64/boot/dts/rockchip/rk3568-qnap-ts433.dts b/arch/arm6= 4/boot/dts/rockchip/rk3568-qnap-ts433.dts index bff21684a3970..c35c11203e903 100644 --- a/arch/arm64/boot/dts/rockchip/rk3568-qnap-ts433.dts +++ b/arch/arm64/boot/dts/rockchip/rk3568-qnap-ts433.dts @@ -50,6 +50,7 @@ led-0 { color =3D ; function =3D LED_FUNCTION_DISK; gpios =3D <&gpio1 RK_PD5 GPIO_ACTIVE_LOW>; + label =3D "hdd1:green:disk"; linux,default-trigger =3D "disk-activity"; pinctrl-names =3D "default"; pinctrl-0 =3D <&hdd1_led_pin>; @@ -59,6 +60,7 @@ led-1 { color =3D ; function =3D LED_FUNCTION_DISK; gpios =3D <&gpio1 RK_PD6 GPIO_ACTIVE_LOW>; + label =3D "hdd2:green:disk"; linux,default-trigger =3D "disk-activity"; pinctrl-names =3D "default"; pinctrl-0 =3D <&hdd2_led_pin>; @@ -68,6 +70,7 @@ led-2 { color =3D ; function =3D LED_FUNCTION_DISK; gpios =3D <&gpio1 RK_PD7 GPIO_ACTIVE_LOW>; + label =3D "hdd3:green:disk"; linux,default-trigger =3D "disk-activity"; pinctrl-names =3D "default"; pinctrl-0 =3D <&hdd3_led_pin>; @@ -77,6 +80,7 @@ led-3 { color =3D ; function =3D LED_FUNCTION_DISK; gpios =3D <&gpio2 RK_PA0 GPIO_ACTIVE_LOW>; + label =3D "hdd4:green:disk"; linux,default-trigger =3D "disk-activity"; pinctrl-names =3D "default"; pinctrl-0 =3D <&hdd4_led_pin>; --=20 2.39.2