1 /* 2 * Access a PWM server to update a display panel backlight. 3 * 4 * (c) 2018 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/cxx/ipc_server> 23 #include <l4/re/env> 24 #include <l4/re/util/object_registry> 25 #include <l4/sys/capability> 26 27 #include <stdlib.h> 28 29 #include <l4/devices/pwm-client.h> 30 #include "backlight-ops.h" 31 32 /* PWM access abstractions. */ 33 34 static L4::Cap<Pwm_device_interface> pwm_device; 35 36 37 38 /* Backlight device. */ 39 40 class Backlight_device_server : public L4::Server_object_t<L4::Kobject> 41 { 42 int _min, _max; 43 44 public: 45 explicit Backlight_device_server(int min, int max) 46 : _min(min), _max(max) 47 { 48 } 49 50 /* Dispatch incoming requests. */ 51 52 int dispatch(l4_umword_t obj, L4::Ipc::Iostream &ios) 53 { 54 l4_msgtag_t tag; 55 int arg; 56 57 (void) obj; 58 ios >> tag; 59 60 switch (tag.label()) 61 { 62 case Backlight_op_disable: 63 disable(); 64 return L4_EOK; 65 66 case Backlight_op_enable: 67 enable(); 68 return L4_EOK; 69 70 case Backlight_op_set_brightness: 71 ios >> arg; 72 set_brightness(arg); 73 return L4_EOK; 74 75 default: 76 return -L4_EBADPROTO; 77 } 78 } 79 80 void disable() 81 { 82 pwm_device->disable(); 83 } 84 85 void enable() 86 { 87 pwm_device->enable(); 88 } 89 90 /* Use the PWM device to update the backlight level. */ 91 92 void set_brightness(int level) 93 { 94 pwm_device->set_duty(level); 95 } 96 }; 97 98 static L4Re::Util::Registry_server<> server; 99 100 101 102 /* Arguments: <minimum level> <maximum level> */ 103 104 int main(int argc, char *argv[]) 105 { 106 int min, max; 107 108 if (argc < 3) 109 return 1; 110 111 /* Obtain a reference to the PWM device. */ 112 113 pwm_device = L4Re::Env::env()->get_cap<Pwm_device_interface>("pwm"); 114 if (!pwm_device.is_valid()) return 1; 115 116 /* Initialise and register a new server object. */ 117 118 min = atoi(argv[1]); 119 max = atoi(argv[2]); 120 121 Backlight_device_server server_obj(min, max); 122 server.registry()->register_obj(&server_obj, "backlight"); 123 124 /* Enter the IPC server loop. */ 125 126 server.loop(); 127 return 0; 128 }