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