paul@0 | 1 | /* |
paul@0 | 2 | * Access a PWM server to update a display panel backlight. |
paul@0 | 3 | * |
paul@31 | 4 | * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 5 | * |
paul@0 | 6 | * This program is free software; you can redistribute it and/or |
paul@0 | 7 | * modify it under the terms of the GNU General Public License as |
paul@0 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 9 | * the License, or (at your option) any later version. |
paul@0 | 10 | * |
paul@0 | 11 | * This program is distributed in the hope that it will be useful, |
paul@0 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 14 | * GNU General Public License for more details. |
paul@0 | 15 | * |
paul@0 | 16 | * You should have received a copy of the GNU General Public License |
paul@0 | 17 | * along with this program; if not, write to the Free Software |
paul@0 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 19 | * Boston, MA 02110-1301, USA |
paul@0 | 20 | */ |
paul@0 | 21 | |
paul@0 | 22 | #include <l4/cxx/ipc_server> |
paul@0 | 23 | #include <l4/re/env> |
paul@0 | 24 | #include <l4/re/util/object_registry> |
paul@0 | 25 | #include <l4/sys/capability> |
paul@0 | 26 | |
paul@0 | 27 | #include <stdlib.h> |
paul@0 | 28 | |
paul@0 | 29 | #include <l4/devices/pwm-client.h> |
paul@0 | 30 | #include "backlight-ops.h" |
paul@0 | 31 | |
paul@0 | 32 | /* PWM access abstractions. */ |
paul@0 | 33 | |
paul@0 | 34 | static L4::Cap<Pwm_device_interface> pwm_device; |
paul@0 | 35 | |
paul@0 | 36 | |
paul@0 | 37 | |
paul@0 | 38 | /* Backlight device. */ |
paul@0 | 39 | |
paul@0 | 40 | class Backlight_device_server : public L4::Server_object_t<L4::Kobject> |
paul@0 | 41 | { |
paul@0 | 42 | int _min, _max; |
paul@0 | 43 | |
paul@0 | 44 | public: |
paul@0 | 45 | explicit Backlight_device_server(int min, int max) |
paul@0 | 46 | : _min(min), _max(max) |
paul@0 | 47 | { |
paul@0 | 48 | } |
paul@0 | 49 | |
paul@0 | 50 | /* Dispatch incoming requests. */ |
paul@0 | 51 | |
paul@0 | 52 | int dispatch(l4_umword_t obj, L4::Ipc::Iostream &ios) |
paul@0 | 53 | { |
paul@0 | 54 | l4_msgtag_t tag; |
paul@0 | 55 | int arg; |
paul@0 | 56 | |
paul@0 | 57 | (void) obj; |
paul@0 | 58 | ios >> tag; |
paul@0 | 59 | |
paul@0 | 60 | switch (tag.label()) |
paul@0 | 61 | { |
paul@0 | 62 | case Backlight_op_disable: |
paul@0 | 63 | disable(); |
paul@0 | 64 | return L4_EOK; |
paul@0 | 65 | |
paul@0 | 66 | case Backlight_op_enable: |
paul@0 | 67 | enable(); |
paul@0 | 68 | return L4_EOK; |
paul@0 | 69 | |
paul@0 | 70 | case Backlight_op_set_brightness: |
paul@0 | 71 | ios >> arg; |
paul@0 | 72 | set_brightness(arg); |
paul@0 | 73 | return L4_EOK; |
paul@0 | 74 | |
paul@0 | 75 | default: |
paul@0 | 76 | return -L4_EBADPROTO; |
paul@0 | 77 | } |
paul@0 | 78 | } |
paul@0 | 79 | |
paul@0 | 80 | void disable() |
paul@0 | 81 | { |
paul@0 | 82 | pwm_device->disable(); |
paul@0 | 83 | } |
paul@0 | 84 | |
paul@0 | 85 | void enable() |
paul@0 | 86 | { |
paul@0 | 87 | pwm_device->enable(); |
paul@0 | 88 | } |
paul@0 | 89 | |
paul@0 | 90 | /* Use the PWM device to update the backlight level. */ |
paul@0 | 91 | |
paul@0 | 92 | void set_brightness(int level) |
paul@0 | 93 | { |
paul@0 | 94 | pwm_device->set_duty(level); |
paul@0 | 95 | } |
paul@0 | 96 | }; |
paul@0 | 97 | |
paul@0 | 98 | static L4Re::Util::Registry_server<> server; |
paul@0 | 99 | |
paul@0 | 100 | |
paul@0 | 101 | |
paul@0 | 102 | /* Arguments: <minimum level> <maximum level> */ |
paul@0 | 103 | |
paul@0 | 104 | int main(int argc, char *argv[]) |
paul@0 | 105 | { |
paul@0 | 106 | int min, max; |
paul@0 | 107 | |
paul@0 | 108 | if (argc < 3) |
paul@0 | 109 | return 1; |
paul@0 | 110 | |
paul@0 | 111 | /* Obtain a reference to the PWM device. */ |
paul@0 | 112 | |
paul@0 | 113 | pwm_device = L4Re::Env::env()->get_cap<Pwm_device_interface>("pwm"); |
paul@0 | 114 | if (!pwm_device.is_valid()) return 1; |
paul@0 | 115 | |
paul@0 | 116 | /* Initialise and register a new server object. */ |
paul@0 | 117 | |
paul@0 | 118 | min = atoi(argv[1]); |
paul@0 | 119 | max = atoi(argv[2]); |
paul@0 | 120 | |
paul@0 | 121 | Backlight_device_server server_obj(min, max); |
paul@0 | 122 | server.registry()->register_obj(&server_obj, "backlight"); |
paul@0 | 123 | |
paul@0 | 124 | /* Enter the IPC server loop. */ |
paul@0 | 125 | |
paul@0 | 126 | server.loop(); |
paul@0 | 127 | return 0; |
paul@0 | 128 | } |