// File: mrlordkaj[P]i2cdevlib-pi/Arduino/wiringPi/wiringPi.c
// Function: pwmSetClock
void pwmSetClock (int divisor)
{
  uint32_t pwm_control ;

  if (piGpioBase == GPIO_PERI_BASE_2711)
  {
    divisor = 540*divisor/192;
  }
  divisor &= 4095 ;

  if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  {
    if (wiringPiDebug)
      printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;

    pwm_control = *(pwm + PWM_CONTROL) ;		// preserve PWM_CONTROL

// We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
// stays high.

    *(pwm + PWM_CONTROL) = 0 ;				// Stop PWM

// Stop PWM clock before changing divisor. The delay after this does need to
// this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
// flag is not working properly in balanced mode. Without the delay when DIV is
// adjusted the clock sometimes switches to very slow, once slow further DIV
// adjustments do nothing and it's difficult to get out of this mode.

    *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ;	// Stop PWM Clock
      delayMicroseconds (110) ;			// prevents clock going sloooow

    while ((*(clk + PWMCLK_CNTL) & 0x80) != 0)	// Wait for clock to be !BUSY
      delayMicroseconds (1) ;

    *(clk + PWMCLK_DIV)  = BCM_PASSWORD | (divisor << 12) ;

    *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ;	// Start PWM clock
    *(pwm + PWM_CONTROL) = pwm_control ;		// restore PWM_CONTROL

    if (wiringPiDebug)
      printf ("Set     to: %d. Now    : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  }
}

// Assembly:
pushq %rax
movl %edi, 0x4(%rsp)
movl 0x324e5(%rip), %eax     # 0x5dcc0
cmpl $0xfe000000, %eax       # imm = 0xFE000000
jne 0x2b7f6
imull $0x21c, 0x4(%rsp), %eax # imm = 0x21C
movl $0xc0, %ecx
cltd
idivl %ecx
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
andl $0xfff, %eax            # imm = 0xFFF
movl %eax, 0x4(%rsp)
cmpl $0x0, 0x314ca(%rip)     # 0x5ccd4
je 0x2b822
cmpl $0x3, 0x314c1(%rip)     # 0x5ccd4
je 0x2b822
cmpl $0x1, 0x314b8(%rip)     # 0x5ccd4
jne 0x2b908
movq 0x30717(%rip), %rax     # 0x5bf40
cmpl $0x0, (%rax)
je 0x2b84d
movl 0x4(%rsp), %esi
movq 0x3248f(%rip), %rax     # 0x5dcc8
movl 0xa4(%rax), %edx
leaq 0x1fdf6(%rip), %rdi     # 0x4b63c
movb $0x0, %al
callq 0x22060
movq 0x32464(%rip), %rax     # 0x5dcb8
movl (%rax), %eax
movl %eax, (%rsp)
movq 0x32458(%rip), %rax     # 0x5dcb8
movl $0x0, (%rax)
movq 0x3245b(%rip), %rax     # 0x5dcc8
movl $0x5a000001, 0xa0(%rax) # imm = 0x5A000001
movl $0x6e, %edi
callq 0x22650
movq 0x32440(%rip), %rax     # 0x5dcc8
movl 0xa0(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x2b8a4
movl $0x1, %edi
callq 0x22650
jmp 0x2b881
movl 0x4(%rsp), %ecx
shll $0xc, %ecx
orl $0x5a000000, %ecx       # imm = 0x5A000000
movq 0x32410(%rip), %rax     # 0x5dcc8
movl %ecx, 0xa4(%rax)
movq 0x32403(%rip), %rax     # 0x5dcc8
movl $0x5a000011, 0xa0(%rax) # imm = 0x5A000011
movl (%rsp), %ecx
movq 0x323df(%rip), %rax     # 0x5dcb8
movl %ecx, (%rax)
movq 0x3065e(%rip), %rax     # 0x5bf40
cmpl $0x0, (%rax)
je 0x2b906
movl 0x4(%rsp), %esi
movq 0x323d6(%rip), %rax     # 0x5dcc8
movl 0xa4(%rax), %edx
leaq 0x1fd5e(%rip), %rdi     # 0x4b65d
movb $0x0, %al
callq 0x22060
jmp 0x2b908
popq %rax
retq
nopw (%rax,%rax)