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 29 #include <l4/devices/spi-client.h> 30 31 #include <ipc/server.h> 32 #include "backlight_object_server.h" 33 34 /* SPI access abstractions. */ 35 36 static L4::Cap<Spi_device_interface> spi_device; 37 38 39 40 /* Backlight device. */ 41 42 class server_BacklightObject : public BacklightObject 43 { 44 int _min = 55, _max = 90, _start = 70; 45 46 void set_duty(int level) 47 { 48 level = level < _min ? _min : (level > _max ? _max : level); 49 50 /* PWM_DUTY = R05h<5:3> = 5% increments from min to max */ 51 52 int duty = ((level - _min) / 5) << 3; 53 54 spi_device->send(16, 0x0516 | duty); /* R05h: GRB=0 (reset); PWM_DUTY=duty; SHDB2=1, SHDB1=1 (power-related); STB=0 (standby) */ 55 spi_device->send(16, 0x0546 | duty); /* R05h: GRB=1 (normal operation); ... */ 56 spi_device->send(16, 0x078d); /* R07h: HBLK=141 (horizontal blanking period from start of hsync pulse to data start) */ 57 spi_device->send(16, 0x1301); /* R13h: IN_SEL=1 (alignment mode) */ 58 spi_device->send(16, 0x0547 | duty); /* R05h: ...; STB=1 (not standby) */ 59 } 60 61 public: 62 /* Disable the backlight. */ 63 64 long disable() 65 { 66 spi_device->send(16, 0x0546); /* R05h: GRB=1 (normal operation); SHDB2=1, SHDB1=1 (power-related); STB=0 (standby) */ 67 return L4_EOK; 68 } 69 70 /* Enable the backlight. */ 71 72 long enable() 73 { 74 set_duty(_start); 75 return L4_EOK; 76 } 77 78 /* Use the SPI device to update the brightness level. */ 79 80 long set_brightness(int level) 81 { 82 level = level < _min ? _min : (level > _max ? _max : level); 83 spi_device->send(16, 0x0300 | level); /* R03h: brightness */ 84 return L4_EOK; 85 } 86 }; 87 88 89 90 int main(void) 91 { 92 /* Obtain a reference to the SPI device. */ 93 94 spi_device = L4Re::Env::env()->get_cap<Spi_device_interface>("spi"); 95 if (!spi_device.is_valid()) return 1; 96 97 /* Initialise and register a new server object. */ 98 99 server_BacklightObject obj; 100 l4_cap_idx_t server; 101 102 if (ipc_server_bind("backlight", (l4_umword_t) &obj, &server)) return 1; 103 104 /* Enter the IPC server loop. */ 105 106 ipc_server_loop(BacklightObject_expected_items, &obj, 107 (ipc_server_handler_type) handle_BacklightObject); 108 return 0; 109 }