diff options
Diffstat (limited to 'drivers/led/aw20216.c')
| -rw-r--r-- | drivers/led/aw20216.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/drivers/led/aw20216.c b/drivers/led/aw20216.c new file mode 100644 index 000000000..c608c0ab4 --- /dev/null +++ b/drivers/led/aw20216.c | |||
| @@ -0,0 +1,141 @@ | |||
| 1 | /* Copyright 2021 Jasper Chan | ||
| 2 | * | ||
| 3 | * This program is free software: you can redistribute it and/or modify | ||
| 4 | * it under the terms of the GNU General Public License as published by | ||
| 5 | * the Free Software Foundation, either version 2 of the License, or | ||
| 6 | * (at your option) any later version. | ||
| 7 | * | ||
| 8 | * This program is distributed in the hope that it will be useful, | ||
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | * GNU General Public License for more details. | ||
| 12 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "aw20216.h" | ||
| 18 | #include "spi_master.h" | ||
| 19 | |||
| 20 | /* The AW20216 appears to be somewhat similar to the IS31FL743, although quite | ||
| 21 | * a few things are different, such as the command byte format and page ordering. | ||
| 22 | * The LED addresses start from 0x00 instead of 0x01. | ||
| 23 | */ | ||
| 24 | #define AWINIC_ID 0b1010 << 4 | ||
| 25 | |||
| 26 | #define AW_PAGE_FUNCTION 0x00 << 1 // PG0, Function registers | ||
| 27 | #define AW_PAGE_PWM 0x01 << 1 // PG1, LED PWM control | ||
| 28 | #define AW_PAGE_SCALING 0x02 << 1 // PG2, LED current scaling control | ||
| 29 | #define AW_PAGE_PATCHOICE 0x03 << 1 // PG3, Pattern choice? | ||
| 30 | #define AW_PAGE_PWMSCALING 0x04 << 1 // PG4, LED PWM + Scaling control? | ||
| 31 | |||
| 32 | #define AW_WRITE 0 | ||
| 33 | #define AW_READ 1 | ||
| 34 | |||
| 35 | #define AW_REG_CONFIGURATION 0x00 // PG0 | ||
| 36 | #define AW_REG_GLOBALCURRENT 0x01 // PG0 | ||
| 37 | |||
| 38 | // Default value of AW_REG_CONFIGURATION | ||
| 39 | // D7:D4 = 1011, SWSEL (SW1~SW12 active) | ||
| 40 | // D3 = 0?, reserved (apparently this should be 1 but it doesn't seem to matter) | ||
| 41 | // D2:D1 = 00, OSDE (open/short detection enable) | ||
| 42 | // D0 = 0, CHIPEN (write 1 to enable LEDs when hardware enable pulled high) | ||
| 43 | #define AW_CONFIG_DEFAULT 0b10110000 | ||
| 44 | #define AW_CHIPEN 1 | ||
| 45 | |||
| 46 | #define AW_PWM_REGISTER_COUNT 216 | ||
| 47 | |||
| 48 | #ifndef AW_SCALING_MAX | ||
| 49 | # define AW_SCALING_MAX 150 | ||
| 50 | #endif | ||
| 51 | |||
| 52 | #ifndef AW_GLOBAL_CURRENT_MAX | ||
| 53 | # define AW_GLOBAL_CURRENT_MAX 150 | ||
| 54 | #endif | ||
| 55 | |||
| 56 | #ifndef AW_SPI_DIVISOR | ||
| 57 | # define AW_SPI_DIVISOR 4 | ||
| 58 | #endif | ||
| 59 | |||
| 60 | uint8_t g_pwm_buffer[DRIVER_COUNT][AW_PWM_REGISTER_COUNT]; | ||
| 61 | bool g_pwm_buffer_update_required[DRIVER_COUNT] = {false}; | ||
| 62 | |||
| 63 | bool AW20216_write(pin_t cs_pin, uint8_t page, uint8_t reg, uint8_t* data, uint8_t len) { | ||
| 64 | static uint8_t s_spi_transfer_buffer[2] = {0}; | ||
| 65 | |||
| 66 | if (!spi_start(cs_pin, false, 0, AW_SPI_DIVISOR)) { | ||
| 67 | spi_stop(); | ||
| 68 | return false; | ||
| 69 | } | ||
| 70 | |||
| 71 | s_spi_transfer_buffer[0] = (AWINIC_ID | page | AW_WRITE); | ||
| 72 | s_spi_transfer_buffer[1] = reg; | ||
| 73 | |||
| 74 | if (spi_transmit(s_spi_transfer_buffer, 2) != SPI_STATUS_SUCCESS) { | ||
| 75 | spi_stop(); | ||
| 76 | return false; | ||
| 77 | } | ||
| 78 | |||
| 79 | if (spi_transmit(data, len) != SPI_STATUS_SUCCESS) { | ||
| 80 | spi_stop(); | ||
| 81 | return false; | ||
| 82 | } | ||
| 83 | |||
| 84 | spi_stop(); | ||
| 85 | return true; | ||
| 86 | } | ||
| 87 | |||
| 88 | static inline bool AW20216_write_register(pin_t cs_pin, uint8_t page, uint8_t reg, uint8_t value) { | ||
| 89 | // Little wrapper so callers need not care about sending a buffer | ||
| 90 | return AW20216_write(cs_pin, page, reg, &value, 1); | ||
| 91 | } | ||
| 92 | |||
| 93 | static void AW20216_init_scaling(pin_t cs_pin) { | ||
| 94 | // Set constant current to the max, control brightness with PWM | ||
| 95 | for (uint8_t i = 0; i < AW_PWM_REGISTER_COUNT; i++) { | ||
| 96 | AW20216_write_register(cs_pin, AW_PAGE_SCALING, i, AW_SCALING_MAX); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | static inline void AW20216_init_current_limit(pin_t cs_pin) { | ||
| 101 | // Push config | ||
| 102 | AW20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_GLOBALCURRENT, AW_GLOBAL_CURRENT_MAX); | ||
| 103 | } | ||
| 104 | |||
| 105 | static inline void AW20216_soft_enable(pin_t cs_pin) { | ||
| 106 | // Push config | ||
| 107 | AW20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_CONFIGURATION, AW_CONFIG_DEFAULT | AW_CHIPEN); | ||
| 108 | } | ||
| 109 | |||
| 110 | void AW20216_init(pin_t cs_pin, pin_t en_pin) { | ||
| 111 | setPinOutput(en_pin); | ||
| 112 | writePinHigh(en_pin); | ||
| 113 | |||
| 114 | // Drivers should start with all scaling and PWM registers as off | ||
| 115 | AW20216_init_current_limit(cs_pin); | ||
| 116 | AW20216_init_scaling(cs_pin); | ||
| 117 | |||
| 118 | AW20216_soft_enable(cs_pin); | ||
| 119 | } | ||
| 120 | |||
| 121 | void AW20216_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { | ||
| 122 | aw_led led = g_aw_leds[index]; | ||
| 123 | |||
| 124 | g_pwm_buffer[led.driver][led.r] = red; | ||
| 125 | g_pwm_buffer[led.driver][led.g] = green; | ||
| 126 | g_pwm_buffer[led.driver][led.b] = blue; | ||
| 127 | g_pwm_buffer_update_required[led.driver] = true; | ||
| 128 | } | ||
| 129 | |||
| 130 | void AW20216_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { | ||
| 131 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
| 132 | AW20216_set_color(i, red, green, blue); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | void AW20216_update_pwm_buffers(pin_t cs_pin, uint8_t index) { | ||
| 137 | if (g_pwm_buffer_update_required[index]) { | ||
| 138 | AW20216_write(cs_pin, AW_PAGE_PWM, 0, g_pwm_buffer[index], AW_PWM_REGISTER_COUNT); | ||
| 139 | } | ||
| 140 | g_pwm_buffer_update_required[index] = false; | ||
| 141 | } | ||
