和手机一样,开发板中也带有调整背光亮度的功能。

调整背光亮度依赖于PWM,它通过调节脉冲宽度来控制背光亮度,此方式需要使用PWM驱动。本章将对其进行讲解。

一、用户空间调整背光亮度

一般应用程序可以通过/sys/class/目录下的节点间接调整各个外设的参数。如下图,可通过命令行来控制背光亮度。设备节点不同开发板的目录不一定相同,读者需自行测试。

十二、使用PWM调整LCD背光亮度-LMLPHP

如果读者确定自己的开发板有PWM控制背光的功能,但是在LCD、背光和PWM等相关目录没有找到调整亮度操作,可能的原因有PWM没有被编译进内核。

我们可以在配置中执行搜索操作,如:

$ make menuconfig  进入内核配置页面

按 / 键搜索,确认背光PWM是否被编译进内核。

如下图,我的背光PWM被编译进内核中了。需要注意,不同内核可能选项位置不同。

十二、使用PWM调整LCD背光亮度-LMLPHP

二、PWM子系统

在Linux3.5版本中并没有引入PWM子系统,而是使用总线设备驱动模型。我在此以Linux-4.4为例谈论PWM子系统。

PWM也分为设备属性和行为。属性使用struct pwm_device表示,行为使用struct pwm_ops表示,两结构体定义如下:

struct pwm_device {
const char *label;
unsigned long flags;
unsigned int hwpwm;
unsigned int pwm;
struct pwm_chip *chip;
void *chip_data;
struct mutex lock; unsigned int period; /* PWM周期 */
unsigned int duty_cycle; /* 占空比 */
enum pwm_polarity polarity; /* 极性反转 */
};
enum pwm_polarity {
PWM_POLARITY_NORMAL,
PWM_POLARITY_INVERSED,
}; struct pwm_ops {
int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
int duty_ns, int period_ns);
int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
enum pwm_polarity polarity);
int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
#ifdef CONFIG_DEBUG_FS
void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
#endif
struct module *owner;
};

pwm_ops的上层函数有:

/* 下列函数与pwm_ops位置一一对应 */
struct pwm_device *pwm_request(int pwm, const char *label);
void pwm_free(struct pwm_device *pwm);
int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
int pwm_enable(struct pwm_device *pwm);
void pwm_disable(struct pwm_device *pwm);

PWM虽然分层和LCD一致,但这两个结构体并不像LCD中可以互相访问。如LCD中fb_info定义有:

struct fb_info {
...
struct fb_ops *fbops; /* 可通过fb_info访问fb操作函数 */
...
};

既然struct pwm_device和struct pwm_ops不能互相访问,那么一定会有其他结构体连接两者。它就是struct pwm_chip:

struct pwm_chip {
struct device *dev;
struct list_head list;
const struct pwm_ops *ops; /* PWM操作函数 */
int base;
unsigned int npwm; /* 这个chip中的PWM个数 */ struct pwm_device *pwms; /* PWM属性 */ struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
const struct of_phandle_args *args);
unsigned int of_pwm_n_cells;
bool can_sleep;
};

如果我们需要注册或注销PWM,使用的结构体应该也是struct pwm_chip。也就是PWM以chip注册。

注册函数pwmchip_add()实现如下:

 /* chip的属性 */
static struct attribute *pwm_chip_attrs[] = {
&dev_attr_export.attr, /* 注册pwm,创建设备节点 */
&dev_attr_unexport.attr, /* 注销pwm,销毁设备节点 */
&dev_attr_npwm.attr, /* 当前chip中pwm个数 */
NULL,
}; int pwmchip_add(struct pwm_chip *chip)
{
return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL);
}
EXPORT_SYMBOL_GPL(pwmchip_add); int pwmchip_add_with_polarity(struct pwm_chip *chip, enum pwm_polarity polarity)
{
struct pwm_device *pwm;
unsigned int i;
int ret;
...
/* pwm子系统和input子系统类似,按顺序填充的,但pwm子系统没有设备号 */
ret = alloc_pwms(chip->base, chip->npwm);
...
/* 申请空间 */
chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
...
chip->base = ret;
/* 设置pwm_device */
for (i = ; i < chip->npwm; i++) {
pwm = &chip->pwms[i];
pwm->chip = chip;
pwm->pwm = chip->base + i;
pwm->hwpwm = i;
pwm->polarity = polarity;
...
}
...
/* 创建pwmchip%d设备 */
pwmchip_sysfs_export(chip);
...
} void pwmchip_sysfs_export(struct pwm_chip *chip)
{
...
parent = device_create(&pwm_class, chip->dev, MKDEV(, ), chip, "pwmchip%d", chip->base);
...
}

注销函数pwmchip_remove()实现如下:

 int pwmchip_remove(struct pwm_chip *chip)
{
unsigned int i;
int ret = ;
/* 此函数会卸载此chip下的所有PWM */
pwmchip_sysfs_unexport_children(chip);
...
free_pwms(chip);
/* 注销设备 */
pwmchip_sysfs_unexport(chip);
...
}
EXPORT_SYMBOL_GPL(pwmchip_remove); void pwmchip_sysfs_unexport(struct pwm_chip *chip)
{
struct device *parent; parent = class_find_device(&pwm_class, NULL, chip,
pwmchip_sysfs_match);
if (parent) {
/* for class_find_device() */
put_device(parent);
device_unregister(parent);
}
}

在注册和注销pwmchip函数中,只是创建和删除和设备节点。但是在此之前,它并没有创建类,而是使用了类pwm_class。分析可知PWM子系统在初始化过程中一定创建了类pwm_class。

 static struct class pwm_class = {
.name = "pwm", /* 类名为pwm */
.owner = THIS_MODULE,
.dev_groups = pwm_chip_groups, /* 存放所有chip */
}; static int __init pwm_sysfs_init(void)
{
return class_register(&pwm_class);
}

现在,我们来整理一下整体框架。

十二、使用PWM调整LCD背光亮度-LMLPHP

二、三星平台驱动分析

1. platform_device

 static struct resource samsung_pwm_resource[] = {
DEFINE_RES_MEM(SAMSUNG_PA_TIMER, SZ_4K),
}; struct platform_device samsung_device_pwm = {
.name = "samsung-pwm",
.id = -,
.num_resources = ARRAY_SIZE(samsung_pwm_resource),
.resource = samsung_pwm_resource,
};

2. platform_driver

 static struct platform_driver pwm_samsung_driver = {
.driver = {
.name = "samsung-pwm",
.pm = &pwm_samsung_pm_ops,
.of_match_table = of_match_ptr(samsung_pwm_matches),
},
.probe = pwm_samsung_probe,
.remove = pwm_samsung_remove,
};
module_platform_driver(pwm_samsung_driver);

我们来分析probe()函数,它设置并注册了pwmchip:

 struct samsung_pwm_chip {
struct pwm_chip chip; /* pwm_chip */
struct samsung_pwm_variant variant;
u8 inverter_mask; void __iomem *base;
struct clk *base_clk;
struct clk *tclk0;
struct clk *tclk1;
}; static int pwm_samsung_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct samsung_pwm_chip *chip;
struct resource *res;
unsigned int chan;
int ret; /* 分配内存 */
chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
/* 设置chip */
chip->chip.dev = &pdev->dev;
chip->chip.ops = &pwm_samsung_ops;
chip->chip.base = -;
chip->chip.npwm = SAMSUNG_PWM_NUM;
chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - ;
...
memcpy(&chip->variant, pdev->dev.platform_data, sizeof(chip->variant)); res = platform_get_resource(pdev, IORESOURCE_MEM, );
chip->base = devm_ioremap_resource(&pdev->dev, res);
...
chip->base_clk = devm_clk_get(&pdev->dev, "timers");
...
ret = clk_prepare_enable(chip->base_clk);
...
for (chan = ; chan < SAMSUNG_PWM_NUM; ++chan)
if (chip->variant.output_mask & BIT(chan))
pwm_samsung_set_invert(chip, chan, true); /* Following clocks are optional. */
chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0");
chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1"); platform_set_drvdata(pdev, chip);
/* 注册pwm */
ret = pwmchip_add(&chip->chip);
...
return ;
}

remove()应该注销pwmchip:

 static int pwm_samsung_remove(struct platform_device *pdev)
{
struct samsung_pwm_chip *chip = platform_get_drvdata(pdev);
int ret; ret = pwmchip_remove(&chip->chip);
if (ret < )
return ret; clk_disable_unprepare(chip->base_clk); return ;
}

关于三星平台的struct pwm_ops pwm_samsung_ops在此不做分析,这些函数只是对寄存器进行读写操作。

最后总结一下:

1. PWM是通过注册chip来注册一个芯片中所有的PWM的

2. 对于具体的PWM,可以使用sysfs中的attributre属性来分辨

3. 注册完成某个PWM后,它的周期、占空比、极性和使能可以在sysfs中更改

4. 在sysfs中修改降低了驱动程序修改的次数

pwmchip在/sys/class/目录下生成,我们可以执行以下命令查看和修改pwmchip0的占空比:

# cd /sys/class/pwm/

# ls

# cd ./pwmchip0

# ls

# cd ./pwm0

# ls

# ls duty_cycle

# echo 50 > duty_cycle

# ls duty_cycle

下一章  十三、GPIO子系统

05-11 10:54