Hardware pwm in user space

Hi there :call:
http://wiki.t-firefly.com/index.php/Firefly-RK3288/PWM/en
That guide describes only kernel space.
How to control hardware pwm in user space?
/sys/class/pwm doesn’t exist
Is there a way to enable sysfs pwm interface?
Thanks

there is no ready-made pwm in user space, need to write interface by youself, just like the backlight’s.

ok. i did it. I wrote a linux kernel module. when I tried insmode i got error in function pwm_config: Unable to handle kernel paging request at virtual address fffffe0b
dmesg

[ 1715.682095] rock-pwm [I]: Start module
[ 1715.682185] rock-pwm [I]: Pwm request
[ 1715.682222] rock-pwm [I]: Pwm request: success!
[ 1715.682252] rock-pwm [I]: Pwm config
[ 1715.682341] Unable to handle kernel paging request at virtual address fffffe0b
[ 1715.689537] pgd = ed784000
[ 1715.692594] [fffffe0b] *pgd=2e7f6821, *pte=00000000, *ppte=00000000
[ 1715.698963] Internal error: Oops: 17 [#1] PREEMPT SMP ARM
[ 1715.704320] Modules linked in: sysfs(+) mali_kbase
[ 1715.709186] CPU: 3 PID: 1712 Comm: insmod Not tainted 3.10.0 #46
[ 1715.715147] task: ec42ee00 ti: ec858000 task.ti: ec858000
[ 1715.720569] PC is at pwm_config+0x40/0x60
[ 1715.724586] LR is at x_init+0x50/0xc0 [sysfs]
[ 1715.728938] pc : [<c028a3f4>]    lr : [<bf036050>]    psr: 60070013
[ 1715.728938] sp : ec859f10  ip : 00000000  fp : 00000000
[ 1715.740269] r10: 00000000  r9 : ec858000  r8 : c0d80000
[ 1715.745442] r7 : 00000000  r6 : ec858000  r5 : bf036000  r4 : 0007a120
[ 1715.751898] r3 : 000f4240  r2 : 00000000  r1 : fffffdfb  r0 : fffffdfb
[ 1715.758376] Flags: nZCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment user
[ 1715.765425] Control: 10c5387d  Table: 2d78406a  DAC: 00000015
[ 1715.771110]
[ 1715.771110] PC: 0xc028a374:
[ 1715.775350] a374  e3530000 03a03002 05803054 059f3004 05803058 eaffff3e c028a2e0 eafffd29
[ 1715.783621] a394  e3500000 15801014 03e00015 13a00000 e12fff1e e3500000 15900014 e12fff1e
[ 1715.791872] a3b4  e1a03002 e2702001 e92d4010 33a02000 e1a04001 e1a01000 e1922fa4 13e00015
[ 1715.800121] a3d4  18bd8010 e3530000 c3a02000 d3a02001 e1540003 c3822001 e3520000 1a000005
[ 1715.808370] a3f4  e5910010 e590200c e592c008 e1a02004 e12fff3c e8bd8010 e3e00015 e8bd8010
[ 1715.816617] a414  e1a02001 e2501000 e92d4008 0a00000c e5910010 e590300c e3530000 0a000008
[ 1715.824864] a434  e593300c e3530000 03e00025 08bd8008 e591c004 e31c0004 1a000003 e12fff33
[ 1715.833113] a454  e8bd8008 e3e00015 e8bd8008 e3e0000f e8bd8008 e5903010 e5d30024 e12fff1e
[ 1715.841366]
[ 1715.841366] SP: 0xec859e90:
[ 1715.845603] 9e90  00000000 60070013 00000000 c002f8fc 00000000 00000000 00000000 00000000
[ 1715.853849] 9eb0  c0d9c8d0 c028a3f4 60070013 ffffffff ec859efc c000d598 fffffdfb fffffdfb
[ 1715.862096] 9ed0  00000000 000f4240 0007a120 bf036000 ec858000 00000000 c0d80000 ec858000
[ 1715.870341] 9ef0  00000000 00000000 00000000 ec859f10 bf036050 c028a3f4 60070013 ffffffff
[ 1715.878586] 9f10  bf0342d8 bf036050 bf034170 c00086a4 bf036000 00000000 bf034170 00000003
[ 1715.886827] 9f30  b6f1c398 0000017b c000db84 c0080d34 bf034170 00000000 00000000 00000003
[ 1715.895072] 9f50  b6f1c398 c00819bc f1a21000 00000fa5 f1a21664 f1a21551 f1a21e5c 000002e8
[ 1715.903319] 9f70  000003d8 00000000 00000000 00000000 00000017 00000018 0000000f 00000000
[ 1715.911576]
[ 1715.911576] R0: 0xfffffd7b:
[ 1715.915813] fd78  ******** ******** ******** ******** ******** ******** ******** ********
[ 1715.924113] fd98  ******** ******** ******** ******** ******** ******** ******** ********
[ 1715.932363] fdb8  ******** ******** ******** ******** ******** ******** ******** ********
[ 1715.940619] fdd8  ******** ******** ******** ******** ******** ******** ******** ********
[ 1715.948871] fdf8  ******** ******** ******** ******** ******** ******** ******** ********
[ 1715.957116] fe18  ******** ******** ******** ******** ******** ******** ******** ********
[ 1715.965368] fe38  ******** ******** ******** ******** ******** ******** ******** ********
[ 1715.973620] fe58  ******** ******** ******** ******** ******** ******** ******** ********
[ 1715.981867] fe78  ******** ******** ******** ******** ******** ******** ******** ********
[ 1715.990123]
[ 1715.990123] R1: 0xfffffd7b:
[ 1715.994360] fd78  ******** ******** ******** ******** ******** ******** ******** ********
[ 1716.002609] fd98  ******** ******** ******** ******** ******** ******** ******** ********
[ 1716.010859] fdb8  ******** ******** ******** ******** ******** ******** ******** ********
[ 1716.019114] fdd8  ******** ******** ******** ******** ******** ******** ******** ********
[ 1716.027364] fdf8  ******** ******** ******** ******** ******** ******** ******** ********
[ 1716.035609] fe18  ******** ******** ******** ******** ******** ******** ******** ********
[ 1716.043858] fe38  ******** ******** ******** ******** ******** ******** ******** ********
[ 1716.052112] fe58  ******** ******** ******** ******** ******** ******** ******** ********
[ 1716.060364] fe78  ******** ******** ******** ******** ******** ******** ******** ********
[ 1716.068617]
[ 1716.068617] R6: 0xec857f80:
[ 1716.072853] 7f80  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[ 1716.081097] 7fa0  00000000 00000000 00000000 00000000 2e7f3841 2e7f3c41 00000000 00000000
[ 1716.089340] 7fc0  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[ 1716.097587] 7fe0  00000000 00000000 00000000 00000000 00000000 00000000 2e7f6821 2e7f6c21
[ 1716.105833] 8000  00000002 00000001 00000000 ec42ee00 c0d03af8 00000003 00000015 ec42ee00
[ 1716.114079] 8020  c1ef1f40 ec858000 ed417180 00000000 0000018f ee19d500 ec859cb4 ec859c30
[ 1716.122321] 8040  c079ce74 00000000 00000000 00000000 00000000 00000000 01000000 00000000
[ 1716.130566] 8060  b6f00850 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[ 1716.138807]
[ 1716.138807] R8: 0xc0d7ff80:
[ 1716.143044] ff80  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[ 1716.151283] ffa0  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[ 1716.159526] ffc0  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[ 1716.167769] ffe0  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[ 1716.176011] 0000  00000000 c1ed1cb2 c0957b4f eda5c800 c1ed1c00 00000000 00000000 00000000
[ 1716.184262] 0020  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[ 1716.192507] 0040  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[ 1716.200757] 0060  00000000 00000000 00000000 00000000 00000000 00000000 00275b8f 00000000
[ 1716.209002]
[ 1716.209002] R9: 0xec857f80:
[ 1716.213238] 7f80  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[ 1716.221481] 7fa0  00000000 00000000 00000000 00000000 2e7f3841 2e7f3c41 00000000 00000000
[ 1716.229728] 7fc0  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[ 1716.237974] 7fe0  00000000 00000000 00000000 00000000 00000000 00000000 2e7f6821 2e7f6c21
[ 1716.246219] 8000  00000002 00000001 00000000 ec42ee00 c0d03af8 00000003 00000015 ec42ee00
[ 1716.254465] 8020  c1ef1f40 ec858000 ed417180 00000000 0000018f ee19d500 ec859cb4 ec859c30
[ 1716.262717] 8040  c079ce74 00000000 00000000 00000000 00000000 00000000 01000000 00000000
[ 1716.270964] 8060  b6f00850 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[ 1716.279226] Process insmod (pid: 1712, stack limit = 0xec858238)
[ 1716.285169] Stack: (0xec859f10 to 0xec85a000)
[ 1716.289494] 9f00:                                     bf0342d8 bf036050 bf034170 c00086a4
[ 1716.297579] 9f20: bf036000 00000000 bf034170 00000003 b6f1c398 0000017b c000db84 c0080d34
[ 1716.305661] 9f40: bf034170 00000000 00000000 00000003 b6f1c398 c00819bc f1a21000 00000fa5
[ 1716.313743] 9f60: f1a21664 f1a21551 f1a21e5c 000002e8 000003d8 00000000 00000000 00000000
[ 1716.321825] 9f80: 00000017 00000018 0000000f 00000000 0000000c 00000000 bee8c6b4 bee8c834
[ 1716.329907] 9fa0: 8dec7500 c000da00 bee8c6b4 bee8c834 00000003 b6f1c398 00000000 00000000
[ 1716.337989] 9fc0: bee8c6b4 bee8c834 8dec7500 0000017b b6f28ee0 00000000 b6f18b60 00000000
[ 1716.346071] 9fe0: bee8c660 bee8c650 b6f13177 b6e81982 80070030 00000003 00000000 00000000
[ 1716.354256] [<c028a3f4>] (pwm_config+0x40/0x60) from [<bf036050>] (x_init+0x50/0xc0 [sysfs])
[ 1716.362695] [<bf036050>] (x_init+0x50/0xc0 [sysfs]) from [<c00086a4>] (do_one_initcall+0x34/0xc8)
[ 1716.371523] [<c00086a4>] (do_one_initcall+0x34/0xc8) from [<c0080d34>] (do_init_module+0x44/0x168)
[ 1716.380401] [<c0080d34>] (do_init_module+0x44/0x168) from [<c00819bc>] (SyS_finit_module+0x6c/0x94)
[ 1716.389374] [<c00819bc>] (SyS_finit_module+0x6c/0x94) from [<c000da00>] (ret_fast_syscall+0x0/0x30)
[ 1716.398323] Code: e1540003 c3822001 e3520000 1a000005 (e5910010)
[ 1716.405119] ---[ end trace 6daea3937796a980 ]---

in this module i use code from example http://wiki.t-firefly.com/index.php/Firefly-RK3288/PWM/en :
struct pwm_device *pwm0=NULL;
pwm0= pwm_request(0, “backlight-pwm”);
pwm_config(pwm0, 500000, 1000000);

What is wrong?

[ 1716.354256] [] (pwm_config+0x40/0x60) from [] (x_init+0x50/0xc0 [sysfs])
[ 1716.362695] [] (x_init+0x50/0xc0 [sysfs]) from [] (do_one_initcall+0x34/0xc8)

make sure your pwm0 is not NULL.

pwm_request() used to return bad structure because device name was incorrect .
Thank you for your answers , but i have some more questions)

  1. In dts file I found, that pwm1 used by pwm_regulator. Is it safe to disable this device?
  2. Is it possible to use pwm2 and pwm3? How to assign them with the pins?

P.S. sorry for my bad english

pwm_regulator is disabled, pwm1 can be used by yourself.
pwm2 and pwm3 can also be used, enable them in dts file as pwm1.

I need to achieve 10000 microseconds period.
pwm_config(1000000,10000000) didn’t work(
How to change timer resolution?

ok. prescale parameter is calculating in pwm-rockchip driver automatically. there is no reason to recalculate.
but…
When I set a period more than 850 microseconds, something strange is happening.
For example I set a period of 900 microseconds(pwm_config(500000,900000)). but according to oscilloscope real period is 450 microseconds. Is there a way to fix it?

Hi,
You can use this pwm-rockchip.c (see attachment) to replace the /kernel/drivers/pwm/pwm-rockchip.c, then recompile, this is can be normal use, the main reason is that # define PWMDCR_MAX_DUTY length is too small。
pwm-rockchip.zip (3.9 KB)

madman Posted at 1/4/2016 11:05
Hi,
You can use this pwm-rockchip.c (see attachment) to replace the /kernel/drivers/pwm/pwm-rockchi …

hi, madman. it’s not working(
I got 670 microsends period with pwm_config parameters: duty: 1600000, period: 2000000…

P.S. I tried to put the printk(“test_pwm\n”) code in each pwm_config function in pwm-rockchip.c, but the message didn’t appear in dmseg… wtf

hi,
I add printk in the probe,it can pritk. use pwm will use this function,

— a/kernel/drivers/pwm/pwm-rockchip.c
+++ b/kernel/drivers/pwm/pwm-rockchip.c
@@ -630,6 +630,7 @@ static int rk_pwm_probe(struct platform_device *pdev)
struct rk_pwm_chip *pc;
int ret;

  •   printk("pwm is here\n");
      if (!of_id){
    

[ 0.750802] io scheduler noop registered
[ 0.750813] io scheduler deadline registered
[ 0.750900] io scheduler cfq registered (default)
[ 0.751217] pwm is here
[ 0.752517] rockchip-hdmiv2 ff980000.hdmi: rockchip hdmiv2 probe sucess.
[ 0.753187] IEP Driver loaded succesfully

The article is very content-rich and has given me many practical insights. This is the L4M3 new study materials exam that helped me land my promotion and salary raise. It’s free today—wishing you all the best in your career progress!