【TINY4412】LINUX移植笔记:(20)设备树LCD背光驱动

环境

宿主机 : 虚拟机 Ubuntu 16.04 LTS / X64
目标板[底板]: Tiny4412SDK - 1506
目标板[核心板]: Tiny4412 - 1412
LINUX内核: 4.12.0
交叉编译器: arm-none-linux-gnueabi-gcc(gcc version 4.8.3 20140320)
日期: 2017-9-9 20:30:09
作者: SY

简介

开发板的 LCD 背光通过 CPLD(EPM240T100) 控制,不能直接通过操作 GPIO 电平,控制背光亮度。根据友善之臂的说法,支持 128 级背光调节

1

2

参考友善之臂的驱动 linux-3.5\drivers\input\touchscreen\tiny4412_1wire_host.c

1
#define GPIO_1WIRE EXYNOS4_GPX1(2)

可以推出背光相关的引脚:GPX1_2 ,这个引脚在使用时,作为一个双向 IO ,既需要输入,也需要输出。

而且触摸屏的输入也使用该引脚,暂时我们先实现 backlight 功能。

设备树

开发板和 CPLD 通讯使用了特殊的协议,设置一次背光值,需要读写 IO 多次,因此,需要使用定时器功能,我们使用定时器 3 ,也就是 PWM3

参考:PWM 设备树

1
2
3
4
5
6
7
8
9
10
11
12
13
pwm: pwm@139D0000 {
compatible = "samsung,exynos4210-pwm";
reg = <0x139D0000 0x1000>;
interrupts = <GIC_SPI 37 IRQ_TYPE_LEVEL_HIGH>,
<GIC_SPI 38 IRQ_TYPE_LEVEL_HIGH>,
<GIC_SPI 39 IRQ_TYPE_LEVEL_HIGH>,
<GIC_SPI 40 IRQ_TYPE_LEVEL_HIGH>,
<GIC_SPI 41 IRQ_TYPE_LEVEL_HIGH>;
clocks = <&clock CLK_PWM>;
clock-names = "timers";
#pwm-cells = <3>;
status = "disabled";
};

实现我们的 dts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
backlight {
compatible = "tiny4412, onewire_backlight";
reg = <0x139D0000 0x1000>;
pinctrl-names = "backlight_in", "backlight_out";
pinctrl-0 = <&backlight_in>;
pinctrl-1 = <&backlight_out>;
gpios = <&gpx1 2 GPIO_ACTIVE_HIGH>;
clock-names = "timers";
clocks = <&clock CLK_PWM>;
interrupts = <GIC_SPI 40 IRQ_TYPE_LEVEL_HIGH>;
pwms = <&pwm 0 1000000000 0>;
};
&pinctrl_1 {
backlight_out: backlight_out {
samsung,pins = "gpx1-2";
samsung,pin-function = <EXYNOS_PIN_FUNC_OUTPUT>;
samsung,pin-pud = <EXYNOS_PIN_PULL_NONE>;
samsung,pin-drv = <EXYNOS4_PIN_DRV_LV1>;
};
backlight_in: backlight_in {
samsung,pins = "gpx1-2";
samsung,pin-function = <EXYNOS_PIN_FUNC_INPUT>;
samsung,pin-pud = <EXYNOS_PIN_PULL_NONE>;
samsung,pin-drv = <EXYNOS4_PIN_DRV_LV1>;
};
};

使用 pinctrl-0 pinctrl-1 可以将同一个引脚设置为 2 种配置。

Makefile

1
2
3
root@ubuntu:/opt/linux-4.12# cat drivers/input/touchscreen/Makefile
obj-$(CONFIG_TOUCHSCREEN_1WIRE) += tiny4412_1wire.o

Kconfig

1
2
3
4
5
6
7
8
root@ubuntu:/opt/linux-4.12# cat drivers/input/touchscreen/Kconfig
config TOUCHSCREEN_1WIRE
tristate "Tiny4412 1wire touchscreen"
help
Say Y here to support touchscreen for tiny4412.
If unsure, say N.

Memenuconfig

1
2
3
4
Device Drivers --->
Input device support --->
[*] Touchscreens --->
<*> Tiny4412 1wire touchscreen

源文件

新建一个tiny4412_1wire.c源文件,参考:学习设备树之(十一)Backlight

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
* tiny4412_1wire_host.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* LCD-CPU one wire communication for Mini6410 from
* FriendlyARM Guangzhou CO., LTD.
*
* Copyright (c) 2010 FriendlyARM Guangzhou CO., LTD. <http://www.arm9.net>
*
* ChangeLog
*
*
* 2010-10-14: Russell Guo <russell.grey@gmail.com>
* - Initial version
* -- request touch-screen data
* -- request LCD type, Firmware version
* - Backlight control
*
* 2017-8-19:transplant by SY <1530454315@qq.com>
* - Using DTS Style
*
* the CRC-8 functions is based on web page from http://lfh1986.blogspot.com
*/
#define DEBUG
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/serio.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
#include <linux/timer.h>
#include <linux/param.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/gpio.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <linux/cdev.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/err.h>
#include <linux/io.h>
#include <asm/mach-types.h>
#include <asm/irq.h>
#include <asm/mach/time.h>
#include <linux/of_gpio.h>
#include <linux/pwm.h>
#define SAMPLE_BPS 9600
#define SLOW_LOOP_FEQ 25
#define FAST_LOOP_FEQ 60
#define REQ_KEY 0x30U
#define REQ_TS 0x40U
#define REQ_INFO 0x60U
struct pwm_base
{
unsigned int TCFG0;
unsigned int TCFG1;
unsigned int TCON;
unsigned int TCNTB0;
unsigned int TCMPB0;
unsigned int TCNTO0;
unsigned int TCNTB1;
unsigned int TCMPB1;
unsigned int TCNTO1;
unsigned int TCNTB2;
unsigned int TCMPB2;
unsigned int TCNTO2;
unsigned int TCNTB3;
unsigned int TCMPB3;
unsigned int TCNTO3;
unsigned int TCNTB4;
unsigned int TCBTO4;
unsigned int TINT_CSTAT;
};
enum rw_status
{
IDLE = 0,
START,
REQUEST,
WAITING,
RESPONSE,
STOPING,
};
static const unsigned char crc8_tab[] =
{
0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15,
0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65,
0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5,
0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85,
0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2,
0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2,
0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32,
0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42,
0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C,
0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC,
0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C,
0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C,
0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B,
0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B,
0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB,
0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB,
0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3,
};
#define crc8_init(crc) ((crc) = 0XACU)
#define crc8(crc, v) ( (crc) = crc8_tab[(crc) ^(v)])
struct tiny4412_1wire_data {
struct timer_list timer;
struct pinctrl *pctrl;
struct pinctrl_state *pstate_in;
struct pinctrl_state *pstate_out;
int gpio_write;
struct resource *res;
struct resource *irq;
volatile struct pwm_base *pwm;
volatile unsigned int io_bit_count;
volatile enum rw_status one_wire_status;
volatile unsigned int io_data;
int major;
struct cdev one_wire_cdev;
struct class *one_wire_class;
struct clk *timer_clk;
};
static struct tiny4412_1wire_data *this = NULL;
static void start_one_wire_session(unsigned char req)
{
unsigned int tcon;
unsigned long prescale;
if (!this) {
return;
}
this->one_wire_status = START;
gpio_set_value(this->gpio_write, 1);
pinctrl_select_state(this->pctrl, this->pstate_out);
// IDLE to START
{
unsigned char crc;
crc8_init(crc);
crc8(crc, req);
this->io_data = (req << 8) + crc;
this->io_data <<= 16;
}
this->io_bit_count = 1;
pinctrl_select_state(this->pctrl, this->pstate_out);
prescale = (this->pwm->TCFG0 >> 8) & 0xFF;
this->pwm->TCNTB3 = clk_get_rate(this->timer_clk) / (prescale + 1) / SAMPLE_BPS - 1;
//init tranfer and start timer
tcon = this->pwm->TCON;
tcon &= ~(0xF << 16);
tcon |= (1 << 17);
this->pwm->TCON = tcon;
tcon |= (1 << 16);
tcon |= (1 << 19);
tcon &= ~(1 << 17);
this->pwm->TCON = tcon;
this->pwm->TINT_CSTAT |= 0x08;
gpio_set_value(this->gpio_write, 0);
}
static int backlight_open(struct inode *inode, struct file *file)
{
printk("backlight_open\n");
return 0;
}
static int backlight_release(struct inode *inode, struct file *file)
{
printk("backlight_exit\n");
return 0;
}
static ssize_t backlight_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
{
unsigned char reg, ret;
ret = copy_from_user(&reg, buf, 1);
if (ret < 0)
{
printk("%s copy_from_user error\n", __func__);
}
if (reg > 127) {
reg = 127;
}
start_one_wire_session(reg + 0x80);
return 1;
}
static struct file_operations backlight_fops =
{
.owner = THIS_MODULE,
.open = backlight_open,
.release = backlight_release,
.write = backlight_write,
};
static inline void stop_timer_for_1wire(void)
{
this->pwm->TCON &= ~(1 << 16);
}
static irqreturn_t timer_for_1wire_interrupt(int irq, void *dev_id)
{
if (!this) {
return IRQ_HANDLED;
}
/* Clear interrupt status bit */
this->pwm->TINT_CSTAT |= (1 << 8);
this->io_bit_count--;
switch(this->one_wire_status) {
case START:
if (this->io_bit_count == 0) {
this->io_bit_count = 16;
this->one_wire_status = REQUEST;
}
break;
case REQUEST:
// Send a bit
gpio_set_value(this->gpio_write, this->io_data & (1U << 31));
this->io_data <<= 1;
if (this->io_bit_count == 0) {
this->io_bit_count = 2;
this->one_wire_status = WAITING;
}
break;
case WAITING:
if (this->io_bit_count == 0) {
this->io_bit_count = 32;
this->one_wire_status = RESPONSE;
}
if (this->io_bit_count == 1) {
pinctrl_select_state(this->pctrl, this->pstate_in);
gpio_set_value(this->gpio_write, 1);
}
break;
case RESPONSE:
// Get a bit
this->io_data = (this->io_data << 1) | gpio_get_value(this->gpio_write);
if (this->io_bit_count == 0) {
this->io_bit_count = 2;
this->one_wire_status = STOPING;
gpio_set_value(this->gpio_write, 1);
pinctrl_select_state(this->pctrl, this->pstate_out);
//one_wire_session_complete(one_wire_request, io_data);
}
break;
case STOPING:
if (this->io_bit_count == 0) {
this->one_wire_status = IDLE;
stop_timer_for_1wire();
}
break;
default:
stop_timer_for_1wire();
}
return IRQ_HANDLED;
}
struct samsung_pwm_variant {
u8 bits;
u8 div_base;
u8 tclk_mask;
u8 output_mask;
bool has_tint_cstat;
};
struct samsung_pwm_chip {
struct pwm_chip chip;
struct samsung_pwm_variant variant;
u8 inverter_mask;
void __iomem *base;
struct clk *base_clk;
struct clk *tclk0;
struct clk *tclk1;
};
static int ts_1wire_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct tiny4412_1wire_data *priv;
int ret;
dev_t devid;
dev_dbg(dev, "probe.");
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv) {
dev_err(dev, "calloc mem error!\n");
return -ENOMEM;
}
this = priv;
/* pinctrl */
priv->pctrl = devm_pinctrl_get(dev);
if (IS_ERR(priv->pctrl)) {
dev_err(dev, "get pinctrl error!\n");
return -EINVAL;
}
priv->pstate_in = pinctrl_lookup_state(priv->pctrl, "backlight_in");
if (IS_ERR(priv->pstate_in)) {
dev_err(dev, "backlight_in not found!\n");
return -EINVAL;
}
priv->pstate_out = pinctrl_lookup_state(priv->pctrl, "backlight_out");
if (IS_ERR(priv->pstate_out)) {
dev_err(dev, "backlight_out not found!\n");
return -EINVAL;
}
/* Get clk */
priv->timer_clk = devm_clk_get(dev, "timers");
if (IS_ERR(priv->timer_clk)) {
dev_err(dev, "devm_clk_get error!\n");
return -EINVAL;
}
dev_dbg(dev, "clk = %ld Hz\n", clk_get_rate(priv->timer_clk));
/* Enable clk */
ret = clk_prepare_enable(priv->timer_clk);
if (ret) {
dev_err(dev, "clk_prepare_enable error!\n");
return -EINVAL;
}
/* Get gpios */
priv->gpio_write = of_get_named_gpio(dev->of_node, "gpios", 0);
if (!gpio_is_valid(priv->gpio_write)) {
dev_err(dev, "gpios not found!\n");
return -EINVAL;
}
/* Request gpios */
ret = devm_gpio_request_one(dev, priv->gpio_write, GPIOF_OUT_INIT_HIGH, "one_wire");
if (ret) {
dev_err(dev, "request gpio error!\n");
return -EINVAL;
}
{
/* io资源在samsung pwm中已申请 */
#if 0
/* Get res */
priv->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!priv->res) {
dev_err(dev, "get res error!\n");
return -EINVAL;
}
dev_err(dev, "start=%x, end=%x name=%s\n", priv->res->start, priv->res->end, priv->res->name);
/* Get ioremap */
priv->pwm = (volatile struct pwm_base *)devm_ioremap_resource(dev, priv->res);
if (IS_ERR((struct pwm_base *)priv->pwm)) {
dev_err(dev, "devm_ioremap_resource error!\n");
return -EINVAL;
}
#endif
struct samsung_pwm_chip *chip;
struct pwm_device *pwm;
pwm = devm_pwm_get(dev, NULL);
if (IS_ERR(pwm)) {
return -EINVAL;
}
devm_pwm_put(dev, pwm);
chip = (struct samsung_pwm_chip *)pwm->chip;
priv->pwm = (volatile struct pwm_base *)chip->base;
if (IS_ERR((struct pwm_base *)priv->pwm)) {
return -EINVAL;
}
}
/* Timer 3 */
priv->pwm->TCFG0 |= (0xf << 8);
priv->pwm->TCFG1 &= ~(0xf << 12);
dev_dbg(dev, "TCFG0: %08x", priv->pwm->TCFG0);
dev_dbg(dev, "TCFG1: %08x", priv->pwm->TCFG1);
/* irq */
priv->irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
if (!priv->irq) {
dev_err(dev, "get irq error!\n");
return -EINVAL;
}
ret = devm_request_irq(dev, priv->irq->start, timer_for_1wire_interrupt , IRQF_TIMER, "backlight", NULL);
if (ret) {
dev_err(dev, "devm_request_irq error!\n");
return -EINVAL;
}
start_one_wire_session(0x60);
/* char device */
ret = alloc_chrdev_region(&devid, 0, 1, "backlight");
if (ret) {
dev_err(dev, "alloc_chrdev_region error!\n");
return -EINVAL;
}
priv->major = MAJOR(devid);
cdev_init(&priv->one_wire_cdev, &backlight_fops);
cdev_add(&priv->one_wire_cdev, devid, 1);
/* class */
priv->one_wire_class = class_create(THIS_MODULE, "one_wire_bl");
device_create(priv->one_wire_class, NULL, MKDEV(priv->major, 0), NULL, "backlight_1wire");
platform_set_drvdata(pdev, priv);
return 0;
}
static int ts_1wire_remove(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct tiny4412_1wire_data *priv = dev_get_drvdata(dev);
device_destroy(priv->one_wire_class, MKDEV(priv->major, 0));
class_destroy(priv->one_wire_class);
cdev_del(&priv->one_wire_cdev);
devm_pinctrl_put(priv->pctrl);
devm_gpio_free(dev, priv->gpio_write);
this = NULL;
return 0;
}
static const struct of_device_id ts_1wire_of_match[] = {
{ .compatible = "tiny4412, onewire_touchscreen" },
{ .compatible = "tiny4412, onewire_backlight" },
{ },
};
static struct platform_driver ts_1wire_device_driver = {
.probe = ts_1wire_probe,
.remove = ts_1wire_remove,
.driver = {
.name = "tiny4412_1wire",
.of_match_table = of_match_ptr(ts_1wire_of_match),
},
};
module_platform_driver(ts_1wire_device_driver);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("SY <1530454315@qq.com>");
MODULE_DESCRIPTION("Tiny4412 one-wire host and Touch Screen Driver");

在申请 IO 资源时,由于我们的驱动中在 pwm-samsung.cpwm_samsung_probe 中已经申请过,而且不会释放资源,因此:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* Get res */
priv->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!priv->res) {
dev_err(dev, "get res error!\n");
return -EINVAL;
}
dev_err(dev, "start=%x, end=%x name=%s\n", priv->res->start, priv->res->end, priv->res->name);
/* Get ioremap */
priv->pwm = (volatile struct pwm_base *)devm_ioremap_resource(dev, priv->res);
if (IS_ERR((struct pwm_base *)priv->pwm)) {
dev_err(dev, "devm_ioremap_resource error!\n");
return -EINVAL;
}

如果再次申请就会失败。

我们采用另一种方式获取资源,devm_pwm_get

1
2
3
struct pwm_device *pwm;
pwm = devm_pwm_get(dev, NULL);

查看结构体:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct pwm_device {
const char *label;
unsigned long flags;
unsigned int hwpwm;
unsigned int pwm;
struct pwm_chip *chip;
void *chip_data;
struct pwm_args args;
struct pwm_state state;
};
struct pwm_chip {
struct device *dev;
struct list_head list;
const struct pwm_ops *ops;
int base;
unsigned int npwm;
struct pwm_device *pwms;
struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
const struct of_phandle_args *args);
unsigned int of_pwm_n_cells;
};

关键的字段是 base ,搜索该字段的内容:

1
2
3
4
5
static int pwm_samsung_probe(struct platform_device *pdev)
{
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
chip->base = devm_ioremap_resource(&pdev->dev, res);
}

可以看出,在这个驱动中,已经获取过 IO 资源,并把 指针 存储到 base 中,这样就好办了

1
2
3
4
5
chip = (struct samsung_pwm_chip *)pwm->chip;
priv->pwm = (volatile struct pwm_base *)chip->base;
if (IS_ERR((struct pwm_base *)priv->pwm)) {
return -EINVAL;
}

这样,定时器的寄存器基地址被存储到 priv->pwm 中,后面我们就可以自由读写定时器寄存器了。

应用程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* Copyright (c) 2017
* Author: SY <1530454315@qq.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#if 0
static void help(void)
{
printf("Usage: ./key <id>\n");
}
#endif
int main(int argc, char **argv)
{
int evfd = open("/dev/backlight_1wire", O_RDWR);
if (evfd < 0) {
perror("[open]");
}
for (int i=0; i<=127; i++) {
uint8_t buff = i;
if (write(evfd, &buff, 1) < 0) {
perror("write");
}
printf("backlight: %d\n", i);
usleep(50000);
}
close(evfd);
printf("done!\n");
return 0;
}

应用程序 Makefile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Linux modules compile
# Author : SY
# Time : 2017-8-10 22:29:24
#############################################################################
CC = arm-none-linux-gnueabi-gcc
CFLAGS = -Wall -std=gnu99 -pthread
TARGET = backlight
OBJS = $(TARGET).o
INSTALL_PATH = /opt/fs/rootfs/rootfs/tmp/
$(TARGET) : $(OBJS)
$(CC) $(CFLAGS) -o $@ $<
install:
chmod 755 $(TARGET)
cp $(TARGET) $(INSTALL_PATH)
clean:
rm -rf *.o $(TARGET)

测试

查看内核消息

1
2
3
4
5
[ 2.653749] tiny4412_1wire 139d0000.backlight: probe.
[ 2.658219] tiny4412_1wire 139d0000.backlight: clk = 100000000 Hz
[ 2.664248] of_get_named_gpiod_flags: parsed 'gpios' property of node '/backlight[0]' - status (0)
[ 2.673202] tiny4412_1wire 139d0000.backlight: TCFG0: 00000f01
[ 2.678969] tiny4412_1wire 139d0000.backlight: TCFG1: 00020000

找到应用程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
[root@TINY4412:~]# cd tmp/
[root@TINY4412:/tmp]# ./backlight
[ 156.325821] backlight_open
backlight: 0
backlight: 1
backlight: 2
backlight: 3
backlight: 4
backlight: 5
backlight: 6
backlight: 7
backlight: 8
backlight: 9
backlight: 10
backlight: 11
backlight: 12
backlight: 13
backlight: 14
backlight: 15
backlight: 16
backlight: 17
backlight: 18
backlight: 19
backlight: 20
backlight: 21
backlight: 22
backlight: 23
backlight: 24
backlight: 25
backlight: 26
backlight: 27
backlight: 28
backlight: 29
backlight: 30
backlight: 31
backlight: 32
backlight: 33
backlight: 34
backlight: 35
backlight: 36
backlight: 37
backlight: 38
backlight: 39
backlight: 40
backlight: 41
backlight: 42
backlight: 43
backlight: 44
backlight: 45
backlight: 46
backlight: 47
backlight: 48
backlight: 49
backlight: 50
backlight: 51
backlight: 52
backlight: 53
backlight: 54
backlight: 55
backlight: 56
backlight: 57
backlight: 58
backlight: 59
backlight: 60
backlight: 61
backlight: 62
backlight: 63
backlight: 64
backlight: 65
backlight: 66
backlight: 67
backlight: 68
backlight: 69
backlight: 70
backlight: 71
backlight: 72
backlight: 73
backlight: 74
backlight: 75
backlight: 76
backlight: 77
backlight: 78
backlight: 79
backlight: 80
backlight: 81
backlight: 82
backlight: 83
backlight: 84
backlight: 85
backlight: 86
backlight: 87
backlight: 88
backlight: 89
backlight: 90
backlight: 91
backlight: 92
backlight: 93
backlight: 94
backlight: 95
backlight: 96
backlight: 97
backlight: 98
backlight: 99
backlight: 100
backlight: 101
backlight: 102
backlight: 103
backlight: 104
backlight: 105
backlight: 106
backlight: 107
backlight: 108
backlight: 109
backlight: 110
backlight: 111
backlight: 112
backlight: 113
backlight: 114
backlight: 115
backlight: 116
backlight: 117
backlight: 118
backlight: 119
backlight: 120
backlight: 121
backlight: 122
backlight: 123
backlight: 124
backlight: 125
backlight: 126
backlight: 127
[ 162.758208] backlight_exit
done!
[root@TINY4412:/tmp]#

可以看到 LCD 从暗慢慢变亮。

SY wechat
扫一扫,用手机访问本站