From nobody Fri Nov 29 14:50:02 2024 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 A775913D8A0 for ; Fri, 20 Sep 2024 08:58:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726822735; cv=none; b=H0QUXB4WmbQeO42ZdNUYXz36vP4Yl+aOSwb7r0srRqd1iRDgFKzjyXrDUEDSZGJjeDXdsAAeIs4Jes0kONm/95gr+q/Z9CiJRuEu7r9GphN7wKqhSrVhIz4osqhQe2WvI41YvbPHpNRgfDJh5e4PznHHTzxmNBMfkWII4Iok/IQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726822735; c=relaxed/simple; bh=8JKOHwvVTOOdOu90bngn5MEjnoNO8sQIVWMjzemkCfk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cdxvbKSliUUV2xnGsczCLoeCtDwqVwh9hzkDHpXAmZEF/C1vEaAFJAk74gGIpKQOc22qRh0fsI5ukcwTESa5HbcpY3bxFuwBbSbdUjvmI4hv+u09Gq89FTOo/rOJQfTXNh+v7HC0+8zu7xjT2R85Jyku+mqEy7+LQUkxHr+nW88= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=esQcH0j8; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="esQcH0j8" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-42cb57f8b41so21706985e9.0 for ; Fri, 20 Sep 2024 01:58:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1726822732; x=1727427532; 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=yIJe8wo5LEYCDyfF2tjtE/tRYsnrKBITD4P5/2zAJmg=; b=esQcH0j8353YVSts9v+/ThZ+qlpmXlMqaoR6gXS9Fpf4rYYPWcq2feqbAlt3AMS+b9 yCU+vCLtUMj+fg1hLuOgWI6mxeasDlsKAix+rSP5quDLuGe4txRS/WYFjYi6D1MUtpft Gwc8XprrcEY1Tn3mfekrwdG+M6c6ZCSrZFmVcsvu5EEYoqaw8o9rPWELJKziVG8jiXR7 oisXxeaBZd0kqRxOBVPCqpgLk8rbdxQ7X/VfGBZlOXRpN+/1V5M47J8TpdP4CyW266fg kgmfz2B1nXf7+JQYFitg6uzuVmAqt8p9k2RL1TgB5Upbf47SucJiMscp8YNZClXkMNur QzJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726822732; x=1727427532; 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=yIJe8wo5LEYCDyfF2tjtE/tRYsnrKBITD4P5/2zAJmg=; b=daO45ExkRLeQZvq3Ekc5TVnwZ308ePJHGUnJJCuhfjzGIJ7+IB0zoD76e1LXTvP+Dy CCRBlVfkZX2FuMSjEir5qs15DYOJoC6RVvPZ8XHPz6skJy0ioflySY3VvzGzp1ZmgeGy lk1F9wN188GXHidfO5dRShj00MEz0g8G31zikJJop3jz86YHbLhkt8Np36dstriiFw/A UXGR0yRSLJEcjQ5RgpNef651sMOYlk7ukujYYiV74J06V2ydBU/YeWqvTBBUBwLFIhnw 0AKGvDlG3nibqsZHyg5XUGHsNszVEhaUrzfse4aEqQZ//7cqkC/0f+7P/Pf94nl+3H6d 9ESA== X-Gm-Message-State: AOJu0YxDG9ORkjivGEU41c6zvSQFijjpIOXQQfF1xRfuO507eO7n/HtK SCwjKtO2ZLSie1DHgYfh/s0Cru/ZelPsI+ofn10zMV2ViIx4bJpe/VI9WhjGsj+yeQUrwXOBoh4 H X-Google-Smtp-Source: AGHT+IEamuKw3j7lSmk0UG/EIStzY9+oi8Mn2s4wNSgOjO3z4k9kC7oJWxHuLTVvyk/WnqsCAQsDEw== X-Received: by 2002:a05:6000:2a8:b0:374:c160:269e with SMTP id ffacd0b85a97d-37a42279388mr1644429f8f.22.1726822731781; Fri, 20 Sep 2024 01:58:51 -0700 (PDT) Received: from localhost (p200300f65f01db004d6a46a6454a1385.dip0.t-ipconnect.de. [2003:f6:5f01:db00:4d6a:46a6:454a:1385]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-378e73e85ccsm17007726f8f.42.2024.09.20.01.58.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Sep 2024 01:58:51 -0700 (PDT) From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= To: linux-kernel@vger.kernel.org Cc: Trevor Gamblin , David Lechner , Fabrice Gasnier , Kent Gibson Subject: [PATCH v5 8/8] pwm: Add support for pwmchip devices for faster and easier userspace access Date: Fri, 20 Sep 2024 10:58:04 +0200 Message-ID: <657876d1321467a2b23060ac15924aed8c423390.1726819463.git.u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Developer-Signature: v=1; a=openpgp-sha256; l=10642; i=u.kleine-koenig@baylibre.com; h=from:subject:message-id; bh=8JKOHwvVTOOdOu90bngn5MEjnoNO8sQIVWMjzemkCfk=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBm7TkpswEwsF25gBsVxXSlIq4lmGd3FeJ9fPI8K B52WGhBz1GJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZu05KQAKCRCPgPtYfRL+ TqrAB/4jddMa8T8RqTIhHG9NbYzhXbKaaW1+lYimdszaksyxbqGy0Nk9GtxnfCjGt/LDVf9uVGV ipkyCS4uumCU7A5EABb6R9O8jqYP6sMvcKcIp9PTJBpCmqQTXHxY/0iTV0aKQ9MUB3dSZ/BTxCg Enyut5KinVcMV+h48vlxEg78Lfn3PUslIHpZ1UIc0X7MJKdcHBOBcv25TOgRYYl1fyMM09oBnH5 ucvg0Jzh/n8llAUTTCDnbe8aVviCX4s7gYzoJrIf1gItup9PgCk/oIO80uN5OP3MuTiTxnOa2bs xVociSJJgcAlKbR1zNBvO5YYqQrLpGbyNXPamhR+ZgbiUohS X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 Content-Transfer-Encoding: quoted-printable With this change each pwmchip defining the new-style waveform callbacks can be accessed from userspace via a character device. Compared to the sysfs-API this is faster (on a stm32mp157 applying a new configuration takes approx 25% only) and allows to pass the whole configuration in a single ioctl allowing atomic application. Signed-off-by: Uwe Kleine-K=C3=B6nig --- drivers/pwm/core.c | 291 +++++++++++++++++++++++++++++++++++++-- include/linux/pwm.h | 3 + include/uapi/linux/pwm.h | 32 +++++ 3 files changed, 311 insertions(+), 15 deletions(-) create mode 100644 include/uapi/linux/pwm.h diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c index ed620e35db61..3c25e0ac682c 100644 --- a/drivers/pwm/core.c +++ b/drivers/pwm/core.c @@ -23,6 +23,8 @@ =20 #include =20 +#include + #define CREATE_TRACE_POINTS #include =20 @@ -1915,20 +1917,9 @@ struct pwm_device *pwm_get(struct device *dev, const= char *con_id) } EXPORT_SYMBOL_GPL(pwm_get); =20 -/** - * pwm_put() - release a PWM device - * @pwm: PWM device - */ -void pwm_put(struct pwm_device *pwm) +static void __pwm_put(struct pwm_device *pwm) { - struct pwm_chip *chip; - - if (!pwm) - return; - - chip =3D pwm->chip; - - guard(mutex)(&pwm_lock); + struct pwm_chip *chip =3D pwm->chip; =20 /* * Trigger a warning if a consumer called pwm_put() twice. @@ -1949,6 +1940,20 @@ void pwm_put(struct pwm_device *pwm) =20 module_put(chip->owner); } + +/** + * pwm_put() - release a PWM device + * @pwm: PWM device + */ +void pwm_put(struct pwm_device *pwm) +{ + if (!pwm) + return; + + guard(mutex)(&pwm_lock); + + __pwm_put(pwm); +} EXPORT_SYMBOL_GPL(pwm_put); =20 static void devm_pwm_release(void *pwm) @@ -2018,6 +2023,249 @@ struct pwm_device *devm_fwnode_pwm_get(struct devic= e *dev, } EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get); =20 +struct pwm_cdev_data { + struct pwm_chip *chip; + struct pwm_device *pwm[]; +}; + +static int pwm_cdev_open(struct inode *inode, struct file *file) +{ + struct pwm_chip *chip =3D container_of(inode->i_cdev, struct pwm_chip, cd= ev); + struct pwm_cdev_data *cdata; + + guard(mutex)(&pwm_lock); + + if (!chip->operational) + return -ENXIO; + + cdata =3D kzalloc(struct_size(cdata, pwm, chip->npwm), GFP_KERNEL); + if (!cdata) + return -ENOMEM; + + cdata->chip =3D chip; + + file->private_data =3D cdata; + + return nonseekable_open(inode, file); +} + +static int pwm_cdev_release(struct inode *inode, struct file *file) +{ + struct pwm_cdev_data *cdata =3D file->private_data; + unsigned int i; + + for (i =3D 0; i < cdata->chip->npwm; ++i) { + if (cdata->pwm[i]) + pwm_put(cdata->pwm[i]); + } + kfree(cdata); + + return 0; +} + +static int pwm_cdev_request(struct pwm_cdev_data *cdata, unsigned int hwpw= m) +{ + struct pwm_chip *chip =3D cdata->chip; + + if (hwpwm >=3D chip->npwm) + return -EINVAL; + + if (!cdata->pwm[hwpwm]) { + struct pwm_device *pwm =3D &chip->pwms[hwpwm]; + int ret; + + ret =3D pwm_device_request(pwm, "pwm-cdev"); + if (ret < 0) + return ret; + + cdata->pwm[hwpwm] =3D pwm; + } + + return 0; +} + +static int pwm_cdev_free(struct pwm_cdev_data *cdata, unsigned int hwpwm) +{ + struct pwm_chip *chip =3D cdata->chip; + + if (hwpwm >=3D chip->npwm) + return -EINVAL; + + if (cdata->pwm[hwpwm]) { + struct pwm_device *pwm =3D cdata->pwm[hwpwm]; + + __pwm_put(pwm); + + cdata->pwm[hwpwm] =3D NULL; + } + + return 0; +} + +static struct pwm_device *pwm_cdev_get_requested_pwm(struct pwm_cdev_data = *cdata, + u32 hwpwm) +{ + struct pwm_chip *chip =3D cdata->chip; + + if (hwpwm >=3D chip->npwm) + return ERR_PTR(-EINVAL); + + if (cdata->pwm[hwpwm]) + return cdata->pwm[hwpwm]; + + return ERR_PTR(-EINVAL); +} + +static long pwm_cdev_ioctl(struct file *file, unsigned int cmd, unsigned l= ong arg) +{ + int ret =3D 0; + struct pwm_cdev_data *cdata =3D file->private_data; + struct pwm_chip *chip =3D cdata->chip; + + guard(mutex)(&pwm_lock); + + if (!chip->operational) + return -ENODEV; + + switch (cmd) { + case PWM_IOCTL_REQUEST: + { + unsigned int hwpwm =3D arg; + + return pwm_cdev_request(cdata, hwpwm); + } + break; + + case PWM_IOCTL_FREE: + { + unsigned int hwpwm =3D arg; + + return pwm_cdev_free(cdata, hwpwm); + } + break; + + case PWM_IOCTL_ROUNDWF: + { + struct pwmchip_waveform cwf; + struct pwm_waveform wf; + struct pwm_device *pwm; + + ret =3D copy_from_user(&cwf, + (struct pwmchip_waveform __user *)arg, + sizeof(cwf)); + if (ret) + return -EFAULT; + + if (cwf.__pad !=3D 0) + return -EINVAL; + + pwm =3D pwm_cdev_get_requested_pwm(cdata, cwf.hwpwm); + if (IS_ERR(pwm)) + return PTR_ERR(pwm); + + wf =3D (struct pwm_waveform) { + .period_length_ns =3D cwf.period_length_ns, + .duty_length_ns =3D cwf.duty_length_ns, + .duty_offset_ns =3D cwf.duty_offset_ns, + }; + + ret =3D pwm_round_waveform_might_sleep(pwm, &wf); + if (ret) + return ret; + + cwf =3D (struct pwmchip_waveform) { + .hwpwm =3D cwf.hwpwm, + .period_length_ns =3D wf.period_length_ns, + .duty_length_ns =3D wf.duty_length_ns, + .duty_offset_ns =3D wf.duty_offset_ns, + }; + + return copy_to_user((struct pwmchip_waveform __user *)arg, + &cwf, sizeof(cwf)); + } + break; + + case PWM_IOCTL_GETWF: + { + struct pwmchip_waveform cwf; + struct pwm_waveform wf; + struct pwm_device *pwm; + + ret =3D copy_from_user(&cwf, + (struct pwmchip_waveform __user *)arg, + sizeof(cwf)); + if (ret) + return -EFAULT; + + if (cwf.__pad !=3D 0) + return -EINVAL; + + pwm =3D pwm_cdev_get_requested_pwm(cdata, cwf.hwpwm); + if (IS_ERR(pwm)) + return PTR_ERR(pwm); + + ret =3D pwm_get_waveform_might_sleep(pwm, &wf); + if (ret) + return ret; + + cwf.period_length_ns =3D wf.period_length_ns; + cwf.duty_length_ns =3D wf.duty_length_ns; + cwf.duty_offset_ns =3D wf.duty_offset_ns; + + return copy_to_user((struct pwmchip_waveform __user *)arg, + &cwf, sizeof(cwf)); + } + break; + + case PWM_IOCTL_SETROUNDEDWF: + case PWM_IOCTL_SETEXACTWF: + { + struct pwmchip_waveform cwf; + struct pwm_waveform wf; + struct pwm_device *pwm; + + ret =3D copy_from_user(&cwf, + (struct pwmchip_waveform __user *)arg, + sizeof(cwf)); + if (ret) + return -EFAULT; + + if (cwf.__pad !=3D 0) + return -EINVAL; + + wf =3D (struct pwm_waveform){ + .period_length_ns =3D cwf.period_length_ns, + .duty_length_ns =3D cwf.duty_length_ns, + .duty_offset_ns =3D cwf.duty_offset_ns, + }; + + if (!pwm_wf_valid(&wf)) + return -EINVAL; + + pwm =3D pwm_cdev_get_requested_pwm(cdata, cwf.hwpwm); + if (IS_ERR(pwm)) + return PTR_ERR(pwm); + + return pwm_set_waveform_might_sleep(pwm, &wf, + cmd =3D=3D PWM_IOCTL_SETEXACTWF); + } + break; + + default: + return -ENOTTY; + } +} + +static const struct file_operations pwm_cdev_fileops =3D { + .open =3D pwm_cdev_open, + .release =3D pwm_cdev_release, + .owner =3D THIS_MODULE, + .llseek =3D no_llseek, + .unlocked_ioctl =3D pwm_cdev_ioctl, +}; + +static dev_t pwm_devt; + /** * __pwmchip_add() - register a new PWM chip * @chip: the PWM chip to add @@ -2070,7 +2318,13 @@ int __pwmchip_add(struct pwm_chip *chip, struct modu= le *owner) scoped_guard(pwmchip, chip) chip->operational =3D true; =20 - ret =3D device_add(&chip->dev); + if (chip->id < 256 && chip->ops->write_waveform) + chip->dev.devt =3D MKDEV(MAJOR(pwm_devt), chip->id); + + cdev_init(&chip->cdev, &pwm_cdev_fileops); + chip->cdev.owner =3D owner; + + ret =3D cdev_device_add(&chip->cdev, &chip->dev); if (ret) goto err_device_add; =20 @@ -2121,7 +2375,7 @@ void pwmchip_remove(struct pwm_chip *chip) idr_remove(&pwm_chips, chip->id); } =20 - device_del(&chip->dev); + cdev_device_del(&chip->cdev, &chip->dev); } EXPORT_SYMBOL_GPL(pwmchip_remove); =20 @@ -2262,9 +2516,16 @@ static int __init pwm_init(void) { int ret; =20 + ret =3D alloc_chrdev_region(&pwm_devt, 0, 256, "pwm"); + if (ret) { + pr_warn("Failed to initialize chrdev region for PWM usage\n"); + return ret; + } + ret =3D class_register(&pwm_class); if (ret) { pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret)); + unregister_chrdev_region(pwm_devt, 256); return ret; } =20 diff --git a/include/linux/pwm.h b/include/linux/pwm.h index c3d9ddeafa65..f43dbfaef1a1 100644 --- a/include/linux/pwm.h +++ b/include/linux/pwm.h @@ -2,6 +2,7 @@ #ifndef __LINUX_PWM_H #define __LINUX_PWM_H =20 +#include #include #include #include @@ -304,6 +305,7 @@ struct pwm_ops { /** * struct pwm_chip - abstract a PWM controller * @dev: device providing the PWMs + * @cdev: &struct cdev for this device * @ops: callbacks for this PWM controller * @owner: module providing this chip * @id: unique number of this PWM chip @@ -318,6 +320,7 @@ struct pwm_ops { */ struct pwm_chip { struct device dev; + struct cdev cdev; const struct pwm_ops *ops; struct module *owner; unsigned int id; diff --git a/include/uapi/linux/pwm.h b/include/uapi/linux/pwm.h new file mode 100644 index 000000000000..0a9cdaa0fe37 --- /dev/null +++ b/include/uapi/linux/pwm.h @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */ + +#ifndef _UAPI_PWM_H_ +#define _UAPI_PWM_H_ + +#include +#include + +/** + * struct pwmchip_waveform - Describe a PWM waveform for a pwm_chip's PWM = channel + * @hwpwm: per-chip relative index of the PWM device + * @__pad: padding, must be zero + * @period_length_ns: duration of the repeating period + * @duty_length_ns: duration of the active part in each period + * @duty_offset_ns: offset of the rising edge from a period's start + */ +struct pwmchip_waveform { + __u32 hwpwm; + __u32 __pad; + __u64 period_length_ns; + __u64 duty_length_ns; + __u64 duty_offset_ns; +}; + +#define PWM_IOCTL_REQUEST _IO(0x75, 1) +#define PWM_IOCTL_FREE _IO(0x75, 2) +#define PWM_IOCTL_ROUNDWF _IOWR(0x75, 3, struct pwmchip_waveform) +#define PWM_IOCTL_GETWF _IOWR(0x75, 4, struct pwmchip_waveform) +#define PWM_IOCTL_SETROUNDEDWF _IOW(0x75, 5, struct pwmchip_waveform) +#define PWM_IOCTL_SETEXACTWF _IOW(0x75, 6, struct pwmchip_waveform) + +#endif /* _UAPI_PWM_H_ */ --=20 2.45.2