From nobody Tue Jul 20 17:30:16 2021 X-Original-To: freebsd-arm@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id CFC2F12B0A2A for ; Tue, 20 Jul 2021 17:30:41 +0000 (UTC) (envelope-from mgrooms@shrew.net) Received: from mx1.shrew.net (mx1.shrew.net [38.97.5.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4GTm2S63Hzz3Hqb for ; Tue, 20 Jul 2021 17:30:40 +0000 (UTC) (envelope-from mgrooms@shrew.net) Received: from mail.shrew.net (mail.shrew.prv [10.24.10.20]) by mx1.shrew.net (8.15.2/8.15.2) with ESMTP id 16KHUX9s011796 for ; Tue, 20 Jul 2021 12:30:33 -0500 (CDT) (envelope-from mgrooms@shrew.net) Received: from [10.16.32.30] (65-36-5-114.static.grandenetworks.net [65.36.5.114]) by mail.shrew.net (Postfix) with ESMTPSA id 9885A19AE83 for ; Tue, 20 Jul 2021 12:30:28 -0500 (CDT) Subject: Re: RPI4 Hardware PWM To: freebsd-arm@freebsd.org References: <9bb7f3d1-8256-be26-ba87-90946ce1b95f@shrew.net> <05ec75e0-8b9c-e69f-904d-6166337611e0@shrew.net> From: Matthew Grooms Message-ID: Date: Tue, 20 Jul 2021 12:30:16 -0500 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 List-Id: Porting FreeBSD to ARM processors List-Archive: https://lists.freebsd.org/archives/freebsd-arm List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-arm@freebsd.org MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------9A553A45CC0DCE886E1A4D0E" Content-Language: en-US X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.shrew.net [10.24.10.10]); Tue, 20 Jul 2021 12:30:33 -0500 (CDT) X-Rspamd-Queue-Id: 4GTm2S63Hzz3Hqb X-Spamd-Bar: --- Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of mgrooms@shrew.net designates 38.97.5.131 as permitted sender) smtp.mailfrom=mgrooms@shrew.net X-Spamd-Result: default: False [-3.30 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; RBL_DBL_DONT_QUERY_IPS(0.00)[38.97.5.131:from]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_SPF_ALLOW(-0.20)[+mx]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[freebsd-arm@freebsd.org]; RCPT_COUNT_ONE(0.00)[1]; SPAMHAUS_ZRD(0.00)[38.97.5.131:from:127.0.2.255]; RCVD_COUNT_THREE(0.00)[3]; DMARC_NA(0.00)[shrew.net]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-1.00)[-1.000]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:~]; ASN(0.00)[asn:174, ipnet:38.0.0.0/8, country:US]; RCVD_TLS_LAST(0.00)[]; MAILMAN_DEST(0.00)[freebsd-arm] X-Spam: Yes X-ThisMailContainsUnwantedMimeParts: Y This is a multi-part message in MIME format. --------------9A553A45CC0DCE886E1A4D0E Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 6/11/2021 9:21 AM, Matthew Grooms wrote: > On 6/11/2021 9:19 AM, Matthew Grooms wrote: >> On 6/9/2021 4:16 PM, Matthew Grooms wrote: >>> On 6/8/2021 4:06 PM, Matthew Grooms wrote: >>>> Hey All, >>>> >>>> I have a project I'm working on that depends on interfacing with a >>>> few sensor modules using both i2c and PWM. I've got the i2c devices >>>> to work correctly, but I'm not sure how to interface with the HW >>>> PWM support of the RPI4. I can see there are settings exposed via >>>> sysctl for Beaglebone systems ... >>>> >>>> https://zewaren.net/bbb-pwm.html >>>> >>>> I was hoping I'd be able to force GPIO 12 or 13 into ALT0 and set >>>> the duty values via sysctl, but that doesn't seem to be an option. >>>> Any help would be greatly appreciated. >>> >>> Replying to myself with a bit more info. I see that there is a >>> driver available for rpi boards authored by PHK ... >>> >>> https://cgit.freebsd.org/src/tree/sys/arm/broadcom/bcm2835/bcm2835_pwm.c >>> >>> >>> That has notes on RPi2/3 boards, but not mention of RPi4. When I >>> load that, I see the following output ... >>> >>> Jun  9 18:29:50 generic kernel: pwm0: >>> mem 0x7e20c000-0x7e20c027 on simplebus0 >>> Jun  9 18:29:50 generic kernel: pwm0: cannot find Clock Manager >>> >>> I assume I'm doing something wrong. Any feedback would be greatly >>> appreciated. >> >> Hey Everyone, >> >> I decided to take a look at the patch that introduced rpi4 support in >> Linux. I'm pretty out of my depth here but they didn't look all that >> extensive. One obvious difference that stood out was that the FreeBSD >> clock manager driver only appears to load for the bcm2835 part while >> the Linux driver loads for both 2835 and 2711. Adding the following >> line to the clkman driver allows the pwm driver to load without an >> error now ... >> >> --- bcm2835_clkman.c    2021-06-11 09:06:19.893728000 -0500 >> +++ bcm2835_clkman.c    2021-06-11 08:50:44.646221000 -0500 >> @@ -51,6 +51,7 @@ >>  #include >> >>  static struct ofw_compat_data compat_data[] = { >> +       {"brcm,bcm2711-cprman",         1}, >>         {"brcm,bcm2835-cprman",         1}, >>         {"broadcom,bcm2835-cprman",     1}, >>         {NULL,                          0} >> >> root@generic:/home/mgrooms # tail -n 1 /var/log/messages >> Jun 11 08:35:58 generic kernel: pwm0: >> mem 0x7e20c000-0x7e20c027 on simplebus0 >> >> Additionally, there appears to be valid sysctl values for the >> hardware pwm devices now. I'll try testing them out and see if I can >> determine if they're working as expected and report back. > > Woops. I was going to include these for reference: > > root@generic:/home/mgrooms # sysctl -a | grep pwm > dev.pwm.0.mode2: 0 > dev.pwm.0.ratio2: 2500 > dev.pwm.0.period2: 10000 > dev.pwm.0.pwm_freq2: 0 > dev.pwm.0.mode: 0 > dev.pwm.0.freq: 125000000 > dev.pwm.0.ratio: 2500 > dev.pwm.0.period: 10000 > dev.pwm.0.pwm_freq: 0 > dev.pwm.0.%parent: simplebus0 > dev.pwm.0.%pnpinfo: name=pwm@7e20c000 compat=brcm,bcm2835-pwm > dev.pwm.0.%location: > dev.pwm.0.%driver: pwm > dev.pwm.0.%desc: BCM2708/2835 PWM controller > dev.pwm.%parent: > Hey everyone. I just wanted to follow up on this. I can confirm that with that one line patch, hardware pwm appears to be working reliably on my rpi4b. Since I struggled to find man pages that explain what these values do ( maybe just missed a man page? ), here are some notes I took: /sbin/sysctl dev.gpio.0.pin.12.function=alt0    # set pin to hw pwm function /sbin/sysctl dev.pwm.X.mode=1                   # 0 = off | 1 = on /sbin/sysctl dev.pwm.X.freq=1000000             # clock frequency /sbin/sysctl dev.pwm.X.period=1000              # pwm_freq = freq / period /sbin/sysctl dev.pwm.X.ratio=500                # 0 - period = 0 - 100% duty cycle dev.pwm.0.mode2: 0 dev.pwm.0.ratio2: 2500 dev.pwm.0.period2: 10000 dev.pwm.0.pwm_freq2: 0 dev.pwm.0.mode: 1 dev.pwm.0.freq: 1000000 dev.pwm.0.ratio: 0 dev.pwm.0.period: 1000 dev.pwm.0.pwm_freq: 1000 dev.pwm.0.%parent: simplebus0 dev.pwm.0.%pnpinfo: name=pwm@7e20c000 compat=brcm,bcm2835-pwm dev.pwm.0.%location: dev.pwm.0.%driver: pwm dev.pwm.0.%desc: BCM2708/2835 PWM controller dev.pwm.%parent: For the above example: A ratio value of 0 would be a 0% duty cycle, 500 would be 50% and 1000 would be 100%. Hopefully this helps someone else in the future. -Matthew --------------9A553A45CC0DCE886E1A4D0E--