1 /* 2 * Export a JZ4730 PWM peripheral as a server. 3 * 4 * Copyright (C) 2018, 2020, 2023, 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/devices/gpio-jz4730.h> 23 #include <l4/devices/pwm-jz4730.h> 24 #include <l4/devices/memory.h> 25 26 #include <l4/re/env.h> 27 28 #include <stdint.h> 29 #include <stdlib.h> 30 31 #include <ipc/server.h> 32 33 #include "pwm_object_server.h" 34 35 36 37 /* Virtual addresses for the GPIO and PWM register blocks. */ 38 39 static l4_addr_t gpio_virt_base = 0, gpio_virt_base_end = 0; 40 static l4_addr_t pwm_virt_base = 0, pwm_virt_base_end = 0; 41 42 43 44 static int setup_memory(void) 45 { 46 if (get_memory("jz4730-gpio", &gpio_virt_base, &gpio_virt_base_end)) 47 return 1; 48 49 if (get_memory("jz4730-pwm", &pwm_virt_base, &pwm_virt_base_end)) 50 return 1; 51 52 return 0; 53 } 54 55 56 57 /* PWM peripheral device. */ 58 59 class PWMObject_server : public PWMObject 60 { 61 Pwm_jz4730_chip *_device = 0; 62 int _duty, _period, _prescale; 63 64 public: 65 /* Associate the device with a particular memory region. */ 66 67 explicit PWMObject_server(Pwm_jz4730_chip *device, int duty, int period, 68 int prescale) 69 : _device(device), _duty(duty), _period(period), _prescale(prescale) 70 { 71 } 72 73 /* Disable the device. */ 74 75 long disable() 76 { 77 _device->disable(); 78 return L4_EOK; 79 } 80 81 /* Enable the device. */ 82 83 long enable() 84 { 85 set_duty(_duty); 86 set_period(_period); 87 set_control(0x80 | _prescale); /* enable | prescale */ 88 return L4_EOK; 89 } 90 91 /* Set the control register. */ 92 93 long set_control(uint8_t control) 94 { 95 _device->set_control(control); 96 return L4_EOK; 97 } 98 99 /* Set the PWM duty cycle. */ 100 101 long set_duty(uint16_t duty) 102 { 103 _duty = duty; 104 _device->set_duty(duty); 105 return L4_EOK; 106 } 107 108 /* Set the PWM period. */ 109 110 long set_period(uint16_t period) 111 { 112 _period = period; 113 _device->set_period(period); 114 return L4_EOK; 115 } 116 }; 117 118 119 120 /* Initialise devices and start the server. */ 121 122 static int run(int number, int duty, int period, int prescale) 123 { 124 Pwm_jz4730_chip pwm_device(pwm_virt_base + number * 0x1000, 125 pwm_virt_base + (number + 1) * 0x1000); 126 127 Gpio_jz4730_chip gpio_port_c(gpio_virt_base, 2); 128 129 /* Enable the PWM output for PC30 or PC31. */ 130 131 gpio_port_c.config_pad(30 + number, Hw::Gpio_chip::Function_alt, 1); 132 133 /* Initialise and register a new server object. */ 134 135 PWMObject_server obj(&pwm_device, duty, period, prescale); 136 137 /* Bind and start the IPC server loop. */ 138 139 if (ipc_server_loop_for(PWMObject, &obj, "pwm")) 140 return 1; 141 142 return 0; 143 } 144 145 146 147 /* Arguments: <PWM device number> <duty> <period> <prescale> */ 148 149 int main(int argc, char *argv[]) 150 { 151 int number, duty, period, prescale; 152 153 if (argc < 5) 154 return 1; 155 156 /* Interpret and restrict the device number. */ 157 158 number = atoi(argv[1]); 159 160 if ((number < 0) || (number > 1)) 161 return 1; 162 163 /* Interpret and restrict the initial parameters. */ 164 165 duty = atoi(argv[2]); 166 period = atoi(argv[3]); 167 prescale = atoi(argv[4]); 168 169 if ((duty < 0) || (period < 0) || (prescale < 0)) 170 return 1; 171 172 /* Obtain access to peripheral memory. */ 173 174 if (setup_memory()) 175 return 1; 176 177 return run(number, duty, period, prescale); 178 }