1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/pkg/devices/backlight/src/pwm/backlight-pwm.cc Sun May 13 01:34:16 2018 +0200
1.3 @@ -0,0 +1,128 @@
1.4 +/*
1.5 + * Access a PWM server to update a display panel backlight.
1.6 + *
1.7 + * (c) 2018 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <l4/cxx/ipc_server>
1.26 +#include <l4/re/env>
1.27 +#include <l4/re/util/object_registry>
1.28 +#include <l4/sys/capability>
1.29 +
1.30 +#include <stdlib.h>
1.31 +
1.32 +#include <l4/devices/pwm-client.h>
1.33 +#include "backlight-ops.h"
1.34 +
1.35 +/* PWM access abstractions. */
1.36 +
1.37 +static L4::Cap<Pwm_device_interface> pwm_device;
1.38 +
1.39 +
1.40 +
1.41 +/* Backlight device. */
1.42 +
1.43 +class Backlight_device_server : public L4::Server_object_t<L4::Kobject>
1.44 +{
1.45 + int _min, _max;
1.46 +
1.47 +public:
1.48 + explicit Backlight_device_server(int min, int max)
1.49 + : _min(min), _max(max)
1.50 + {
1.51 + }
1.52 +
1.53 + /* Dispatch incoming requests. */
1.54 +
1.55 + int dispatch(l4_umword_t obj, L4::Ipc::Iostream &ios)
1.56 + {
1.57 + l4_msgtag_t tag;
1.58 + int arg;
1.59 +
1.60 + (void) obj;
1.61 + ios >> tag;
1.62 +
1.63 + switch (tag.label())
1.64 + {
1.65 + case Backlight_op_disable:
1.66 + disable();
1.67 + return L4_EOK;
1.68 +
1.69 + case Backlight_op_enable:
1.70 + enable();
1.71 + return L4_EOK;
1.72 +
1.73 + case Backlight_op_set_brightness:
1.74 + ios >> arg;
1.75 + set_brightness(arg);
1.76 + return L4_EOK;
1.77 +
1.78 + default:
1.79 + return -L4_EBADPROTO;
1.80 + }
1.81 + }
1.82 +
1.83 + void disable()
1.84 + {
1.85 + pwm_device->disable();
1.86 + }
1.87 +
1.88 + void enable()
1.89 + {
1.90 + pwm_device->enable();
1.91 + }
1.92 +
1.93 + /* Use the PWM device to update the backlight level. */
1.94 +
1.95 + void set_brightness(int level)
1.96 + {
1.97 + pwm_device->set_duty(level);
1.98 + }
1.99 +};
1.100 +
1.101 +static L4Re::Util::Registry_server<> server;
1.102 +
1.103 +
1.104 +
1.105 +/* Arguments: <minimum level> <maximum level> */
1.106 +
1.107 +int main(int argc, char *argv[])
1.108 +{
1.109 + int min, max;
1.110 +
1.111 + if (argc < 3)
1.112 + return 1;
1.113 +
1.114 + /* Obtain a reference to the PWM device. */
1.115 +
1.116 + pwm_device = L4Re::Env::env()->get_cap<Pwm_device_interface>("pwm");
1.117 + if (!pwm_device.is_valid()) return 1;
1.118 +
1.119 + /* Initialise and register a new server object. */
1.120 +
1.121 + min = atoi(argv[1]);
1.122 + max = atoi(argv[2]);
1.123 +
1.124 + Backlight_device_server server_obj(min, max);
1.125 + server.registry()->register_obj(&server_obj, "backlight");
1.126 +
1.127 + /* Enter the IPC server loop. */
1.128 +
1.129 + server.loop();
1.130 + return 0;
1.131 +}