Linux PWM driver
1 Overview
This article will provide an in-depth introduction to the PWM (Pulse-Width Modulation) driver framework of Linux, including its implementation principles, driver addition methods, and debugging methods to help developers better understand and use this function.
The example Linux kernel version shown in this article is 6.2.8.
2. Principle
PWM technology, as a commonly used modulation technology, can change the duty cycle of the effective signal by adjusting the high-level time width of the periodic waveform, thereby achieving power supply control for the device. Common PWM application areas include screen backlight brightness adjustment, motor speed control, and fan speed control. The Linux kernel supports the PWM driver framework and provides the PWM driver framework core code and sysfs interface code by abstracting the structure data types of the PWM controller and PWM signals. Developers can use this driver framework to easily add their own specific PWM controller drivers and use the sysfs interface for functional debugging.

Driver developers can design a dedicated PWM controller structure as needed, using the struct pwm_chip structure as a member variable. The following is an example, in the ./drivers/pwm/pwm-ab8500.c driver definition.

(2) struct pwm_ops structure, including the operation function of the PWM controller. The two basic functions that should be implemented by driver developers are the apply function and the get_state function; the apply function is used to configure the PWM controller, including configuring the enable state, period, duty cycle, polarity, etc. of the PWM signal; the get_state function is used Obtain the initial status of the specified PWM channel signal when registering the PWM controller, including enable status, period, duty cycle, polarity, etc.

(3) struct pwm_device structure, representing the PWM signal output by the PWM controller.

3.3 Driver Framework Core Function
(1) pwmchip_add function. Used to register a new PWM controller device. Defined in ./drivers/pwm/core.c.

(2) pwmchip_remove function. Used to remove a PWM controller device. Defined in ./drivers/pwm/core.c.

3.4 Driver typical implementation method
(1) Summary
The PWM driver source file is located in the ./drivers/pwm path. The driver developer needs to add the corresponding source file. The source file design can refer to the driver code of other manufacturers. Referring to the naming style of the original code, you can name the newly added driver source file pwm-xx.c, name the probe and remove functions as xx_pwm_probe and xx_pwm_remove respectively, and define the PWM controller structure as struct xx_pwm_chip. The files in the ./drivers/pwm path are as follows.

(2) apply function and get_state function definition
According to the PWM controller manual, the apply function and get_state function are defined by the driver developer.
(3)xx_pwm_probe function definition
The implementation method of the xx_pwm_probe function is: first initialize the PWM controller structure struct xx_pwm_chip variable, and then call the PWM driver framework core function pwmchip_add to register a new PWM controller device.
(4)xx_pwm_remove function definition
xx_pwm_remove function is implemented by calling the PWM driver framework core function pwmchip_remove to remove a PWM controller device.
4. Driver addition method
4.1 Add driver source file
Follow the method described in Section 3.4, write the driver source file pwm-xx.c, and add it to the ./drivers/pwm path.
4.2 Add compilation support
(1) Modify the Kconfig file under the ./drivers/pwm path and add the added PWM driver configuration options.
(2) Modify the Makefile file in the ./drivers/pwm path and add the compilation option of pwm-xx.c.
(3) In the memuconfig interface, enable the added PWM driver. The configuration interface is as follows.

4.3 Add PWM controller device tree node
Add the PWM controller device tree node in the device tree. The basic properties of this node include compatibility, registers and number of #pwm-cells. Add interrupts, clocks, resets and other properties as needed.
PWM controller device tree design method can refer to ./Documentation/devicetree/bindings/pwm.
The following is an example of a PWM controller device tree node. The reference documents are as follows:
./Documentation/devicetree/bindings/pwm/nvidia,tegra20-pwm.yaml.

5.Debugging method
Use the sysfs interface to perform functional debugging of the PWM driver. Examples of the main debugging commands are as follows.
(1) View PWM controller node
ls /sys/class/pwm/pwmchip0
(2) Open the specified PWM channel signal
echo n > /sys/class/pwm/pwmchip0/export
//n is the channel number
(3)Set the PWM signal period
echo pvalue > /sys/class/pwm/pwmchip0/pwm0/period
//pvalue is the period value
(4)Set the PWM signal duty cycle
echo dvalue > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
//dvalue is the effective level width value
(5) Enable a certain PWM channel signal
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
(6) Disable a certain PWM channel signal
echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable
The above is the detailed content of Linux PWM driver. For more information, please follow other related articles on the PHP Chinese website!

Linux service management is a must-have skill for Linux system administrators and users. A service is a process running in the background, providing various functions, such as a web server, a database, or a network service. This article will guide you on how to start, stop, and restart Linux services. Why start, stop or restart the service? Starting the Service: Services may be required to start after software is installed or when certain services are not automatically started when the system starts. Stop Service: Stop Service can free up system resources or prevent unwanted programs from running. Restart the service: If the service fails or after a configuration change is made, restarting is usually the fastest way to resolve the problem. Key commands for managing services In Linux, management services are the most common

This article explores hashdeep, a command-line utility for verifying file integrity and cryptographic hashes in Linux. It's a powerful tool for system administrators, security professionals, and digital forensic investigators. Understanding hashdeep

Linuxusesdecentralized,distribution-specificpackagemanagersforpatchmanagement,whileWindowsemploysacentralizedWindowsUpdatesystem.Linux'sapproachoffersflexibilitybutcanbecomplexacrossdistributions,whereasWindowsprovidesastreamlinedbutlessflexibleupdat

Virtual Data Rooms (VDRs) offer secure document storage and sharing, ideal for sensitive business information. This article explores three open-source VDR solutions for on-premises deployment on Linux, eliminating the need for cloud-based services a

Upscayl: Your Free and Open-Source Solution for High-Resolution Images on Linux Linux users who frequently work with images know the frustration of low-resolution pictures. Luckily, Upscayl offers a powerful, free, and open-source solution. This des

The terminal emulator landscape is evolving rapidly, with developers leveraging modern hardware, GPU acceleration, containerization, and even AI/LLMs to enhance console experiences. Enter Ghostty, a new open-source, cross-platform terminal emulator

Innotop: Powerful MySQL monitoring command line tool Innotop is an excellent command line program, similar to the top command, used to monitor local and remote MySQL servers running under the InnoDB engine. It provides a comprehensive set of features and options to help database administrators (DBAs) track various aspects of MySQL performance, troubleshoot issues and optimize server configuration. Innotop allows you to monitor critical MySQL metrics, such as: MySQL replication status User statistics Query list InnoDB buffer pool InnoDB I/O Statistics Open table Locked table etc… The tool regularly refreshes its data to provide server status

Restic: Your Comprehensive Guide to Secure Linux Backups Data loss can cripple a Linux system. Accidental deletions, hardware failures, or system corruption necessitate a robust backup strategy. Restic is a leading solution, providing speed, securi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
