1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/pkg/devices/backlight/src/spi-ili8960/backlight-spi-ili8960.cc Fri May 25 22:14:36 2018 +0200
1.3 @@ -0,0 +1,133 @@
1.4 +/*
1.5 + * Access a SPI server to update a display panel backlight.
1.6 + *
1.7 + * This server is specific to the Ilitek ili8960 backlight controller, although
1.8 + * the Giantplus GPM940B0 panel datasheet also describes this controller
1.9 + * interface.
1.10 + *
1.11 + * (c) 2018 Paul Boddie <paul@boddie.org.uk>
1.12 + *
1.13 + * This program is free software; you can redistribute it and/or
1.14 + * modify it under the terms of the GNU General Public License as
1.15 + * published by the Free Software Foundation; either version 2 of
1.16 + * the License, or (at your option) any later version.
1.17 + *
1.18 + * This program is distributed in the hope that it will be useful,
1.19 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.20 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.21 + * GNU General Public License for more details.
1.22 + *
1.23 + * You should have received a copy of the GNU General Public License
1.24 + * along with this program; if not, write to the Free Software
1.25 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.26 + * Boston, MA 02110-1301, USA
1.27 + */
1.28 +
1.29 +#include <l4/cxx/ipc_server>
1.30 +#include <l4/re/env>
1.31 +#include <l4/re/util/object_registry>
1.32 +#include <l4/sys/capability>
1.33 +
1.34 +#include <stdlib.h>
1.35 +
1.36 +#include <l4/devices/spi-client.h>
1.37 +#include "backlight-ops.h"
1.38 +
1.39 +/* SPI access abstractions. */
1.40 +
1.41 +static L4::Cap<Spi_device_interface> spi_device;
1.42 +
1.43 +
1.44 +
1.45 +/* Backlight device. */
1.46 +
1.47 +class Backlight_device_server : public L4::Server_object_t<L4::Kobject>
1.48 +{
1.49 + int _min = 55, _max = 90, _start = 70;
1.50 +
1.51 + void set_duty(int level)
1.52 + {
1.53 + level = level < _min ? _min : (level > _max ? _max : level);
1.54 +
1.55 + /* PWM_DUTY = R05h<5:3> = 5% increments from min to max */
1.56 +
1.57 + int duty = ((level - _min) / 5) << 3;
1.58 +
1.59 + spi_device->send(16, 0x0516 | duty); /* R05h: GRB=0 (reset); PWM_DUTY=duty; SHDB2=1, SHDB1=1 (power-related); STB=0 (standby) */
1.60 + spi_device->send(16, 0x0546 | duty); /* R05h: GRB=1 (normal operation); ... */
1.61 + spi_device->send(16, 0x078d); /* R07h: HBLK=141 (horizontal blanking period from start of hsync pulse to data start) */
1.62 + spi_device->send(16, 0x1301); /* R13h: IN_SEL=1 (alignment mode) */
1.63 + spi_device->send(16, 0x0547 | duty); /* R05h: ...; STB=1 (not standby) */
1.64 + }
1.65 +
1.66 +public:
1.67 + /* Dispatch incoming requests. */
1.68 +
1.69 + int dispatch(l4_umword_t obj, L4::Ipc::Iostream &ios)
1.70 + {
1.71 + l4_msgtag_t tag;
1.72 + int arg;
1.73 +
1.74 + (void) obj;
1.75 + ios >> tag;
1.76 +
1.77 + switch (tag.label())
1.78 + {
1.79 + case Backlight_op_disable:
1.80 + disable();
1.81 + return L4_EOK;
1.82 +
1.83 + case Backlight_op_enable:
1.84 + enable();
1.85 + return L4_EOK;
1.86 +
1.87 + case Backlight_op_set_brightness:
1.88 + ios >> arg;
1.89 + set_brightness(arg);
1.90 + return L4_EOK;
1.91 +
1.92 + default:
1.93 + return -L4_EBADPROTO;
1.94 + }
1.95 + }
1.96 +
1.97 + void disable()
1.98 + {
1.99 + spi_device->send(16, 0x0546); /* R05h: GRB=1 (normal operation); SHDB2=1, SHDB1=1 (power-related); STB=0 (standby) */
1.100 + }
1.101 +
1.102 + void enable()
1.103 + {
1.104 + set_duty(_start);
1.105 + }
1.106 +
1.107 + /* Use the SPI device to update the brightness level. */
1.108 +
1.109 + void set_brightness(int level)
1.110 + {
1.111 + level = level < _min ? _min : (level > _max ? _max : level);
1.112 + spi_device->send(16, 0x0300 | level); /* R03h: brightness */
1.113 + }
1.114 +};
1.115 +
1.116 +static L4Re::Util::Registry_server<> server;
1.117 +
1.118 +
1.119 +
1.120 +int main(void)
1.121 +{
1.122 + /* Obtain a reference to the SPI device. */
1.123 +
1.124 + spi_device = L4Re::Env::env()->get_cap<Spi_device_interface>("spi");
1.125 + if (!spi_device.is_valid()) return 1;
1.126 +
1.127 + /* Initialise and register a new server object. */
1.128 +
1.129 + Backlight_device_server server_obj;
1.130 + server.registry()->register_obj(&server_obj, "backlight");
1.131 +
1.132 + /* Enter the IPC server loop. */
1.133 +
1.134 + server.loop();
1.135 + return 0;
1.136 +}