pwm_sub_system.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/of_platform.h>
#include <linux/leds.h>
#include <linux/err.h>
#include <linux/pwm.h>
#include <linux/leds_pwm.h>
#include <linux/slab.h>

#include "pwm_sub_system.h"


struct pwm_device	*red_led_pwm;  //定义pwm设备结构体
/*精简版 prob函数*/
static int led_pwm_probe_new(struct platform_device *pdev)
{
	int ret = 0;
	struct device_node *child; // 保存子节点
	struct device *dev = &pdev->dev;
	printk("match success \n");

	child = of_get_next_child(dev->of_node, NULL);
	if (child)
	{
		red_led_pwm = devm_of_pwm_get(dev, child, NULL);
		if (IS_ERR(red_led_pwm)) 
		{
			printk(KERN_ERR" red_led_pwm,get pwm  error!!\n");
			return -1;
		}
	}
	else
	{
		printk(KERN_ERR" red_led_pwm of_get_next_child  error!!\n");
		return -1;
	}

	/*配置频率100KHz 占空比80%*/
	pwm_config(red_led_pwm, 1000, 5000);
	/*反相 频率100KHz 占空比20%*/
	pwm_set_polarity(red_led_pwm, PWM_POLARITY_INVERSED);
	pwm_enable(red_led_pwm);

	return ret;
}

static int led_pwm_remove(struct platform_device *pdev)
{
	pwm_config(red_led_pwm, 0, 5000);
	return 0;
}

static const struct of_device_id of_pwm_leds_match[] = {
	{.compatible = "red_led_pwm"},
	{},
};

static struct platform_driver led_pwm_driver = {
	.probe		= led_pwm_probe_new,
	.remove		= led_pwm_remove,
	.driver		= {
		.name	= "led_pwm",
		.of_match_table = of_pwm_leds_match,
	},
};


/*
*驱动初始化函数
*/
static int __init pwm_leds_platform_driver_init(void)
{
	return platform_driver_register(&led_pwm_driver);
}

/*
*驱动注销函数
*/
static void __exit pwm_leds_platform_driver_exit(void)
{
	printk(KERN_ERR " pwm_leds_exit\n");
	/*注销平台设备*/
	platform_driver_unregister(&led_pwm_driver);
}

module_init(pwm_leds_platform_driver_init);
module_exit(pwm_leds_platform_driver_exit);

MODULE_LICENSE("GPL");


 




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值