search
HomeSystem TutorialLINUXLinux PWM driver

Linux PWM driver

Feb 05, 2024 pm 03:40 PM
linuxlinux tutoriallinux systemlinux commandshell scriptembeddedlinuxgood promiseGetting started with linuxlinux learning

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.

Linux PWM driver

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.

Linux PWM driver

(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.

Linux PWM driver

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

Linux PWM driver

3.3 Driver Framework Core Function

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

Linux PWM driver

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

Linux PWM driver

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.

Linux PWM driver

(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.

Linux PWM driver

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.

Linux PWM driver

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!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
How to Start, Stop, and Restart Services in LinuxHow to Start, Stop, and Restart Services in LinuxMay 09, 2025 am 09:22 AM

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

Hashdeep: A Tool for File Integrity and ForensicsHashdeep: A Tool for File Integrity and ForensicsMay 09, 2025 am 09:15 AM

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

How does the patch management process differ between Linux and Windows?How does the patch management process differ between Linux and Windows?May 09, 2025 am 12:01 AM

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

Top 3 Open Source Virtual Data Room (VDR) for LinuxTop 3 Open Source Virtual Data Room (VDR) for LinuxMay 08, 2025 am 11:35 AM

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: An Open-Source Image Upscaling Tool for LinuxUpscayl: An Open-Source Image Upscaling Tool for LinuxMay 08, 2025 am 11:19 AM

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

Ghostty - A Feature-Rich Terminal Emulator for LinuxGhostty - A Feature-Rich Terminal Emulator for LinuxMay 08, 2025 am 11:14 AM

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 - A CLI Based top-like Monitor Tool for MySQLInnotop - A CLI Based top-like Monitor Tool for MySQLMay 08, 2025 am 10:48 AM

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

How to Back Up Linux Data with Restic ToolHow to Back Up Linux Data with Restic ToolMay 08, 2025 am 10:34 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Safe Exam Browser

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

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

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

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

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.