1 /* 2 * Access a SPI server to update a display panel backlight. 3 * 4 * This server is specific to the Ilitek ili8960 backlight controller, although 5 * the Giantplus GPM940B0 panel datasheet also describes this controller 6 * interface. 7 * 8 * Copyright (C) 2018, 2020 Paul Boddie <paul@boddie.org.uk> 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License as 12 * published by the Free Software Foundation; either version 2 of 13 * the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 23 * Boston, MA 02110-1301, USA 24 */ 25 26 #include <l4/re/env> 27 #include <l4/sys/capability> 28 #include <l4/sys/err.h> 29 30 #include <stdio.h> 31 32 #include <l4/devices/spi-client.h> 33 34 #include <ipc/server.h> 35 #include "backlight_object_server.h" 36 37 /* SPI access abstractions. */ 38 39 static L4::Cap<Spi_device_interface> spi_device; 40 41 42 43 /* Backlight device. */ 44 45 class server_BacklightObject : public BacklightObject 46 { 47 int _min = 55, _max = 90, _start = 70; 48 49 void set_duty(int level) 50 { 51 level = level < _min ? _min : (level > _max ? _max : level); 52 53 /* PWM_DUTY = R05h<5:3> = 5% increments from min to max */ 54 55 int duty = ((level - _min) / 5) << 3; 56 57 spi_device->send(16, 0x0516 | duty); /* R05h: GRB=0 (reset); PWM_DUTY=duty; SHDB2=1, SHDB1=1 (power-related); STB=0 (standby) */ 58 spi_device->send(16, 0x0546 | duty); /* R05h: GRB=1 (normal operation); ... */ 59 spi_device->send(16, 0x078d); /* R07h: HBLK=141 (horizontal blanking period from start of hsync pulse to data start) */ 60 spi_device->send(16, 0x1301); /* R13h: IN_SEL=1 (alignment mode) */ 61 spi_device->send(16, 0x0547 | duty); /* R05h: ...; STB=1 (not standby) */ 62 } 63 64 public: 65 /* Disable the backlight. */ 66 67 long disable() 68 { 69 spi_device->send(16, 0x0546); /* R05h: GRB=1 (normal operation); SHDB2=1, SHDB1=1 (power-related); STB=0 (standby) */ 70 return L4_EOK; 71 } 72 73 /* Enable the backlight. */ 74 75 long enable() 76 { 77 set_duty(_start); 78 return L4_EOK; 79 } 80 81 /* Use the SPI device to update the brightness level. */ 82 83 long set_brightness(int level) 84 { 85 level = level < _min ? _min : (level > _max ? _max : level); 86 spi_device->send(16, 0x0300 | level); /* R03h: brightness */ 87 return L4_EOK; 88 } 89 }; 90 91 92 93 int main(void) 94 { 95 /* Obtain a reference to the SPI device. */ 96 97 spi_device = L4Re::Env::env()->get_cap<Spi_device_interface>("spi"); 98 if (!spi_device.is_valid()) return 1; 99 100 /* Initialise and register a new server object. */ 101 102 server_BacklightObject obj; 103 l4_cap_idx_t server; 104 105 if (ipc_server_bind("backlight", (l4_umword_t) &obj, &server)) return 1; 106 107 /* Enter the IPC server loop. */ 108 109 ipc_server_loop(BacklightObject_expected_items, &obj, 110 (ipc_server_handler_type) handle_BacklightObject); 111 return 0; 112 }