diff options
Diffstat (limited to 'drivers/awinic/aw20216.c')
| -rw-r--r-- | drivers/awinic/aw20216.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/drivers/awinic/aw20216.c b/drivers/awinic/aw20216.c new file mode 100644 index 000000000..236c42a3c --- /dev/null +++ b/drivers/awinic/aw20216.c | |||
| @@ -0,0 +1,166 @@ | |||
| 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 | #ifndef AW_SCALING_MAX | ||
| 47 | # define AW_SCALING_MAX 150 | ||
| 48 | #endif | ||
| 49 | |||
| 50 | #ifndef AW_GLOBAL_CURRENT_MAX | ||
| 51 | # define AW_GLOBAL_CURRENT_MAX 150 | ||
| 52 | #endif | ||
| 53 | |||
| 54 | #ifndef DRIVER_1_CS | ||
| 55 | # define DRIVER_1_CS B13 | ||
| 56 | #endif | ||
| 57 | |||
| 58 | #ifndef DRIVER_1_EN | ||
| 59 | # define DRIVER_1_EN C13 | ||
| 60 | #endif | ||
| 61 | |||
| 62 | uint8_t g_spi_transfer_buffer[20] = {0}; | ||
| 63 | aw_led g_pwm_buffer[DRIVER_LED_TOTAL]; | ||
| 64 | bool g_pwm_buffer_update_required[DRIVER_LED_TOTAL]; | ||
| 65 | |||
| 66 | bool AW20216_write_register(pin_t slave_pin, uint8_t page, uint8_t reg, uint8_t data) { | ||
| 67 | // Do we need to call spi_stop() if this fails? | ||
| 68 | if (!spi_start(slave_pin, false, 0, 16)) { | ||
| 69 | return false; | ||
| 70 | } | ||
| 71 | |||
| 72 | g_spi_transfer_buffer[0] = (AWINIC_ID | page | AW_WRITE); | ||
| 73 | g_spi_transfer_buffer[1] = reg; | ||
| 74 | g_spi_transfer_buffer[2] = data; | ||
| 75 | |||
| 76 | if (spi_transmit(g_spi_transfer_buffer, 3) != SPI_STATUS_SUCCESS) { | ||
| 77 | spi_stop(); | ||
| 78 | return false; | ||
| 79 | } | ||
| 80 | spi_stop(); | ||
| 81 | return true; | ||
| 82 | } | ||
| 83 | |||
| 84 | bool AW20216_init_scaling(void) { | ||
| 85 | // Set constant current to the max, control brightness with PWM | ||
| 86 | aw_led led; | ||
| 87 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
| 88 | led = g_aw_leds[i]; | ||
| 89 | if (led.driver == 0) { | ||
| 90 | AW20216_write_register(DRIVER_1_CS, AW_PAGE_SCALING, led.r, AW_SCALING_MAX); | ||
| 91 | AW20216_write_register(DRIVER_1_CS, AW_PAGE_SCALING, led.g, AW_SCALING_MAX); | ||
| 92 | AW20216_write_register(DRIVER_1_CS, AW_PAGE_SCALING, led.b, AW_SCALING_MAX); | ||
| 93 | } | ||
| 94 | #ifdef DRIVER_2_CS | ||
| 95 | else if (led.driver == 1) { | ||
| 96 | AW20216_write_register(DRIVER_2_CS, AW_PAGE_SCALING, led.r, AW_SCALING_MAX); | ||
| 97 | AW20216_write_register(DRIVER_2_CS, AW_PAGE_SCALING, led.g, AW_SCALING_MAX); | ||
| 98 | AW20216_write_register(DRIVER_2_CS, AW_PAGE_SCALING, led.b, AW_SCALING_MAX); | ||
| 99 | } | ||
| 100 | #endif | ||
| 101 | } | ||
| 102 | return true; | ||
| 103 | } | ||
| 104 | |||
| 105 | bool AW20216_soft_enable(void) { | ||
| 106 | AW20216_write_register(DRIVER_1_CS, AW_PAGE_FUNCTION, AW_REG_CONFIGURATION, AW_CONFIG_DEFAULT | AW_CHIPEN); | ||
| 107 | #ifdef DRIVER_2_CS | ||
| 108 | AW20216_write_register(DRIVER_2_CS, AW_PAGE_FUNCTION, AW_REG_CONFIGURATION, AW_CONFIG_DEFAULT | AW_CHIPEN); | ||
| 109 | #endif | ||
| 110 | return true; | ||
| 111 | } | ||
| 112 | |||
| 113 | void AW20216_update_pwm(int index, uint8_t red, uint8_t green, uint8_t blue) { | ||
| 114 | aw_led led = g_aw_leds[index]; | ||
| 115 | if (led.driver == 0) { | ||
| 116 | AW20216_write_register(DRIVER_1_CS, AW_PAGE_PWM, led.r, red); | ||
| 117 | AW20216_write_register(DRIVER_1_CS, AW_PAGE_PWM, led.g, green); | ||
| 118 | AW20216_write_register(DRIVER_1_CS, AW_PAGE_PWM, led.b, blue); | ||
| 119 | } | ||
| 120 | #ifdef DRIVER_2_CS | ||
| 121 | else if (led.driver == 1) { | ||
| 122 | AW20216_write_register(DRIVER_2_CS, AW_PAGE_PWM, led.r, red); | ||
| 123 | AW20216_write_register(DRIVER_2_CS, AW_PAGE_PWM, led.g, green); | ||
| 124 | AW20216_write_register(DRIVER_2_CS, AW_PAGE_PWM, led.b, blue); | ||
| 125 | } | ||
| 126 | #endif | ||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | void AW20216_init(void) { | ||
| 131 | // All LEDs should start with all scaling and PWM registers as off | ||
| 132 | setPinOutput(DRIVER_1_EN); | ||
| 133 | writePinHigh(DRIVER_1_EN); | ||
| 134 | AW20216_write_register(DRIVER_1_CS, AW_PAGE_FUNCTION, AW_REG_GLOBALCURRENT, AW_GLOBAL_CURRENT_MAX); | ||
| 135 | #ifdef DRIVER_2_EN | ||
| 136 | setPinOutput(DRIVER_2_EN); | ||
| 137 | writePinHigh(DRIVER_2_EN); | ||
| 138 | AW20216_write_register(DRIVER_2_CS, AW_PAGE_FUNCTION, AW_REG_GLOBALCURRENT, AW_GLOBAL_CURRENT_MAX); | ||
| 139 | #endif | ||
| 140 | AW20216_init_scaling(); | ||
| 141 | AW20216_soft_enable(); | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | void AW20216_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { | ||
| 146 | g_pwm_buffer[index].r = red; | ||
| 147 | g_pwm_buffer[index].g = green; | ||
| 148 | g_pwm_buffer[index].b = blue; | ||
| 149 | g_pwm_buffer_update_required[index] = true; | ||
| 150 | return; | ||
| 151 | } | ||
| 152 | void AW20216_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { | ||
| 153 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
| 154 | AW20216_set_color(i, red, green, blue); | ||
| 155 | } | ||
| 156 | return; | ||
| 157 | } | ||
| 158 | void AW20216_update_pwm_buffers(void) { | ||
| 159 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
| 160 | if (g_pwm_buffer_update_required[i]) { | ||
| 161 | AW20216_update_pwm(i, g_pwm_buffer[i].r, g_pwm_buffer[i].g, g_pwm_buffer[i].b); | ||
| 162 | g_pwm_buffer_update_required[i] = false; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | return; | ||
| 166 | } | ||
