diff options
author | Gigahawk <jasperchan515@gmail.com> | 2021-06-09 18:40:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-10 11:40:25 +1000 |
commit | 32b2ac0a807bdb088df685e6118f4c0966b6cca4 (patch) | |
tree | c1720e551dc875bd585dc1136f4f99ea00f23c28 /drivers | |
parent | e21a03990116b439de2d4a0181f4a87d048e0366 (diff) | |
download | qmk_firmware-32b2ac0a807bdb088df685e6118f4c0966b6cca4.tar.gz qmk_firmware-32b2ac0a807bdb088df685e6118f4c0966b6cca4.zip |
GMMK Pro RGB Support (#13147)
* Enable SPI1 for GMMK pro
* Setup initial boilerplate for new LED driver
* RGB matrix minimally functional
* Map full LED matrix
* Return keymap to default
* Fix printscreen LED mapping
* Reduce max brightness
* Default values for AW20216
* Add documentation for AW20216
* Disable console and warnings
* Run cformat
* Update drivers/awinic/aw20216.h
Co-authored-by: Drashna Jaelre <drashna@live.com>
* make aw struct match issi struct
Co-authored-by: Drashna Jaelre <drashna@live.com>
* add led location defines
Co-authored-by: Drashna Jaelre <drashna@live.com>
* Use led pin definitions in keyboard.c
* Add driver indices to led map
* Fix elif typo
* Run cformat
* Update docs
* Fix typo in docs
* Document global brightness limits
Co-authored-by: Drashna Jaelre <drashna@live.com>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/awinic/aw20216.c | 166 | ||||
-rw-r--r-- | drivers/awinic/aw20216.h | 251 |
2 files changed, 417 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 | } | ||
diff --git a/drivers/awinic/aw20216.h b/drivers/awinic/aw20216.h new file mode 100644 index 000000000..9c6865cc8 --- /dev/null +++ b/drivers/awinic/aw20216.h | |||
@@ -0,0 +1,251 @@ | |||
1 | /* Copyright 2021 Jasper Chan (Gigahawk) | ||
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 | #pragma once | ||
18 | |||
19 | #include <stdint.h> | ||
20 | #include <stdbool.h> | ||
21 | |||
22 | typedef struct aw_led { | ||
23 | uint8_t driver : 2; | ||
24 | uint8_t r; | ||
25 | uint8_t g; | ||
26 | uint8_t b; | ||
27 | } aw_led; | ||
28 | |||
29 | extern const aw_led g_aw_leds[DRIVER_LED_TOTAL]; | ||
30 | |||
31 | void AW20216_init(void); | ||
32 | void AW20216_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); | ||
33 | void AW20216_set_color_all(uint8_t red, uint8_t green, uint8_t blue); | ||
34 | void AW20216_update_pwm_buffers(void); | ||
35 | |||
36 | #define CS1_SW1 0x00 | ||
37 | #define CS2_SW1 0x01 | ||
38 | #define CS3_SW1 0x02 | ||
39 | #define CS4_SW1 0x03 | ||
40 | #define CS5_SW1 0x04 | ||
41 | #define CS6_SW1 0x05 | ||
42 | #define CS7_SW1 0x06 | ||
43 | #define CS8_SW1 0x07 | ||
44 | #define CS9_SW1 0x08 | ||
45 | #define CS10_SW1 0x09 | ||
46 | #define CS11_SW1 0x0A | ||
47 | #define CS12_SW1 0x0B | ||
48 | #define CS13_SW1 0x0C | ||
49 | #define CS14_SW1 0x0D | ||
50 | #define CS15_SW1 0x0E | ||
51 | #define CS16_SW1 0x0F | ||
52 | #define CS17_SW1 0x10 | ||
53 | #define CS18_SW1 0x11 | ||
54 | #define CS1_SW2 0x12 | ||
55 | #define CS2_SW2 0x13 | ||
56 | #define CS3_SW2 0x14 | ||
57 | #define CS4_SW2 0x15 | ||
58 | #define CS5_SW2 0x16 | ||
59 | #define CS6_SW2 0x17 | ||
60 | #define CS7_SW2 0x18 | ||
61 | #define CS8_SW2 0x19 | ||
62 | #define CS9_SW2 0x1A | ||
63 | #define CS10_SW2 0x1B | ||
64 | #define CS11_SW2 0x1C | ||
65 | #define CS12_SW2 0x1D | ||
66 | #define CS13_SW2 0x1E | ||
67 | #define CS14_SW2 0x1F | ||
68 | #define CS15_SW2 0x20 | ||
69 | #define CS16_SW2 0x21 | ||
70 | #define CS17_SW2 0x22 | ||
71 | #define CS18_SW2 0x23 | ||
72 | #define CS1_SW3 0x24 | ||
73 | #define CS2_SW3 0x25 | ||
74 | #define CS3_SW3 0x26 | ||
75 | #define CS4_SW3 0x27 | ||
76 | #define CS5_SW3 0x28 | ||
77 | #define CS6_SW3 0x29 | ||
78 | #define CS7_SW3 0x2A | ||
79 | #define CS8_SW3 0x2B | ||
80 | #define CS9_SW3 0x2C | ||
81 | #define CS10_SW3 0x2D | ||
82 | #define CS11_SW3 0x2E | ||
83 | #define CS12_SW3 0x2F | ||
84 | #define CS13_SW3 0x30 | ||
85 | #define CS14_SW3 0x31 | ||
86 | #define CS15_SW3 0x32 | ||
87 | #define CS16_SW3 0x33 | ||
88 | #define CS17_SW3 0x34 | ||
89 | #define CS18_SW3 0x35 | ||
90 | #define CS1_SW4 0x36 | ||
91 | #define CS2_SW4 0x37 | ||
92 | #define CS3_SW4 0x38 | ||
93 | #define CS4_SW4 0x39 | ||
94 | #define CS5_SW4 0x3A | ||
95 | #define CS6_SW4 0x3B | ||
96 | #define CS7_SW4 0x3C | ||
97 | #define CS8_SW4 0x3D | ||
98 | #define CS9_SW4 0x3E | ||
99 | #define CS10_SW4 0x3F | ||
100 | #define CS11_SW4 0x40 | ||
101 | #define CS12_SW4 0x41 | ||
102 | #define CS13_SW4 0x42 | ||
103 | #define CS14_SW4 0x43 | ||
104 | #define CS15_SW4 0x44 | ||
105 | #define CS16_SW4 0x45 | ||
106 | #define CS17_SW4 0x46 | ||
107 | #define CS18_SW4 0x47 | ||
108 | #define CS1_SW5 0x48 | ||
109 | #define CS2_SW5 0x49 | ||
110 | #define CS3_SW5 0x4A | ||
111 | #define CS4_SW5 0x4B | ||
112 | #define CS5_SW5 0x4C | ||
113 | #define CS6_SW5 0x4D | ||
114 | #define CS7_SW5 0x4E | ||
115 | #define CS8_SW5 0x4F | ||
116 | #define CS9_SW5 0x50 | ||
117 | #define CS10_SW5 0x51 | ||
118 | #define CS11_SW5 0x52 | ||
119 | #define CS12_SW5 0x53 | ||
120 | #define CS13_SW5 0x54 | ||
121 | #define CS14_SW5 0x55 | ||
122 | #define CS15_SW5 0x56 | ||
123 | #define CS16_SW5 0x57 | ||
124 | #define CS17_SW5 0x58 | ||
125 | #define CS18_SW5 0x59 | ||
126 | #define CS1_SW6 0x5A | ||
127 | #define CS2_SW6 0x5B | ||
128 | #define CS3_SW6 0x5C | ||
129 | #define CS4_SW6 0x5D | ||
130 | #define CS5_SW6 0x5E | ||
131 | #define CS6_SW6 0x5F | ||
132 | #define CS7_SW6 0x60 | ||
133 | #define CS8_SW6 0x61 | ||
134 | #define CS9_SW6 0x62 | ||
135 | #define CS10_SW6 0x63 | ||
136 | #define CS11_SW6 0x64 | ||
137 | #define CS12_SW6 0x65 | ||
138 | #define CS13_SW6 0x66 | ||
139 | #define CS14_SW6 0x67 | ||
140 | #define CS15_SW6 0x68 | ||
141 | #define CS16_SW6 0x69 | ||
142 | #define CS17_SW6 0x6A | ||
143 | #define CS18_SW6 0x6B | ||
144 | #define CS1_SW7 0x6C | ||
145 | #define CS2_SW7 0x6D | ||
146 | #define CS3_SW7 0x6E | ||
147 | #define CS4_SW7 0x6F | ||
148 | #define CS5_SW7 0x70 | ||
149 | #define CS6_SW7 0x71 | ||
150 | #define CS7_SW7 0x72 | ||
151 | #define CS8_SW7 0x73 | ||
152 | #define CS9_SW7 0x74 | ||
153 | #define CS10_SW7 0x75 | ||
154 | #define CS11_SW7 0x76 | ||
155 | #define CS12_SW7 0x77 | ||
156 | #define CS13_SW7 0x78 | ||
157 | #define CS14_SW7 0x79 | ||
158 | #define CS15_SW7 0x7A | ||
159 | #define CS16_SW7 0x7B | ||
160 | #define CS17_SW7 0x7C | ||
161 | #define CS18_SW7 0x7D | ||
162 | #define CS1_SW8 0x7E | ||
163 | #define CS2_SW8 0x7F | ||
164 | #define CS3_SW8 0x80 | ||
165 | #define CS4_SW8 0x81 | ||
166 | #define CS5_SW8 0x82 | ||
167 | #define CS6_SW8 0x83 | ||
168 | #define CS7_SW8 0x84 | ||
169 | #define CS8_SW8 0x85 | ||
170 | #define CS9_SW8 0x86 | ||
171 | #define CS10_SW8 0x87 | ||
172 | #define CS11_SW8 0x88 | ||
173 | #define CS12_SW8 0x89 | ||
174 | #define CS13_SW8 0x8A | ||
175 | #define CS14_SW8 0x8B | ||
176 | #define CS15_SW8 0x8C | ||
177 | #define CS16_SW8 0x8D | ||
178 | #define CS17_SW8 0x8E | ||
179 | #define CS18_SW8 0x8F | ||
180 | #define CS1_SW9 0x90 | ||
181 | #define CS2_SW9 0x91 | ||
182 | #define CS3_SW9 0x92 | ||
183 | #define CS4_SW9 0x93 | ||
184 | #define CS5_SW9 0x94 | ||
185 | #define CS6_SW9 0x95 | ||
186 | #define CS7_SW9 0x96 | ||
187 | #define CS8_SW9 0x97 | ||
188 | #define CS9_SW9 0x98 | ||
189 | #define CS10_SW9 0x99 | ||
190 | #define CS11_SW9 0x9A | ||
191 | #define CS12_SW9 0x9B | ||
192 | #define CS13_SW9 0x9C | ||
193 | #define CS14_SW9 0x9D | ||
194 | #define CS15_SW9 0x9E | ||
195 | #define CS16_SW9 0x9F | ||
196 | #define CS17_SW9 0xA0 | ||
197 | #define CS18_SW9 0xA1 | ||
198 | #define CS1_SW10 0xA2 | ||
199 | #define CS2_SW10 0xA3 | ||
200 | #define CS3_SW10 0xA4 | ||
201 | #define CS4_SW10 0xA5 | ||
202 | #define CS5_SW10 0xA6 | ||
203 | #define CS6_SW10 0xA7 | ||
204 | #define CS7_SW10 0xA8 | ||
205 | #define CS8_SW10 0xA9 | ||
206 | #define CS9_SW10 0xAA | ||
207 | #define CS10_SW10 0xAB | ||
208 | #define CS11_SW10 0xAC | ||
209 | #define CS12_SW10 0xAD | ||
210 | #define CS13_SW10 0xAE | ||
211 | #define CS14_SW10 0xAF | ||
212 | #define CS15_SW10 0xB0 | ||
213 | #define CS16_SW10 0xB1 | ||
214 | #define CS17_SW10 0xB2 | ||
215 | #define CS18_SW10 0xB3 | ||
216 | #define CS1_SW11 0xB4 | ||
217 | #define CS2_SW11 0xB5 | ||
218 | #define CS3_SW11 0xB6 | ||
219 | #define CS4_SW11 0xB7 | ||
220 | #define CS5_SW11 0xB8 | ||
221 | #define CS6_SW11 0xB9 | ||
222 | #define CS7_SW11 0xBA | ||
223 | #define CS8_SW11 0xBB | ||
224 | #define CS9_SW11 0xBC | ||
225 | #define CS10_SW11 0xBD | ||
226 | #define CS11_SW11 0xBE | ||
227 | #define CS12_SW11 0xBF | ||
228 | #define CS13_SW11 0xC0 | ||
229 | #define CS14_SW11 0xC1 | ||
230 | #define CS15_SW11 0xC2 | ||
231 | #define CS16_SW11 0xC3 | ||
232 | #define CS17_SW11 0xC4 | ||
233 | #define CS18_SW11 0xC5 | ||
234 | #define CS1_SW12 0xC6 | ||
235 | #define CS2_SW12 0xC7 | ||
236 | #define CS3_SW12 0xC8 | ||
237 | #define CS4_SW12 0xC9 | ||
238 | #define CS5_SW12 0xCA | ||
239 | #define CS6_SW12 0xCB | ||
240 | #define CS7_SW12 0xCC | ||
241 | #define CS8_SW12 0xCD | ||
242 | #define CS9_SW12 0xCE | ||
243 | #define CS10_SW12 0xCF | ||
244 | #define CS11_SW12 0xD0 | ||
245 | #define CS12_SW12 0xD1 | ||
246 | #define CS13_SW12 0xD2 | ||
247 | #define CS14_SW12 0xD3 | ||
248 | #define CS15_SW12 0xD4 | ||
249 | #define CS16_SW12 0xD5 | ||
250 | #define CS17_SW12 0xD6 | ||
251 | #define CS18_SW12 0xD7 | ||