Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Google LLC. |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
Kumar Gala | 3bc3f2a | 2020-03-25 12:01:50 -0500 | [diff] [blame] | 7 | #define DT_DRV_COMPAT apa_apa102 |
| 8 | |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 9 | #include <errno.h> |
Gerard Marull-Paretas | fb60aab | 2022-05-06 10:25:46 +0200 | [diff] [blame] | 10 | #include <zephyr/drivers/led_strip.h> |
| 11 | #include <zephyr/drivers/spi.h> |
| 12 | #include <zephyr/drivers/gpio.h> |
| 13 | #include <zephyr/sys/util.h> |
Roman Vaughan | ac6fec4 | 2020-05-14 14:24:10 +1200 | [diff] [blame] | 14 | |
Jordan Yates | 8d84626 | 2021-08-07 13:01:52 +1000 | [diff] [blame] | 15 | struct apa102_config { |
| 16 | struct spi_dt_spec bus; |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 17 | }; |
| 18 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 19 | static int apa102_update(const struct device *dev, void *buf, size_t size) |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 20 | { |
Jordan Yates | 8d84626 | 2021-08-07 13:01:52 +1000 | [diff] [blame] | 21 | const struct apa102_config *config = dev->config; |
| 22 | static const uint8_t zeros[] = { 0, 0, 0, 0 }; |
| 23 | static const uint8_t ones[] = { 0xFF, 0xFF, 0xFF, 0xFF }; |
Tomasz Bursztyka | ea2431f | 2018-01-30 13:49:01 +0100 | [diff] [blame] | 24 | const struct spi_buf tx_bufs[] = { |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 25 | { |
| 26 | /* Start frame: at least 32 zeros */ |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 27 | .buf = (uint8_t *)zeros, |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 28 | .len = sizeof(zeros), |
| 29 | }, |
| 30 | { |
| 31 | /* LED data itself */ |
| 32 | .buf = buf, |
| 33 | .len = size, |
| 34 | }, |
| 35 | { |
| 36 | /* End frame: at least 32 ones to clock the |
| 37 | * remaining bits to the LEDs at the end of |
| 38 | * the strip. |
| 39 | */ |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 40 | .buf = (uint8_t *)ones, |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 41 | .len = sizeof(ones), |
| 42 | }, |
| 43 | }; |
Tomasz Bursztyka | ea2431f | 2018-01-30 13:49:01 +0100 | [diff] [blame] | 44 | const struct spi_buf_set tx = { |
| 45 | .buffers = tx_bufs, |
Roman Vaughan | ebf5dfb | 2018-12-09 04:39:39 +1300 | [diff] [blame] | 46 | .count = ARRAY_SIZE(tx_bufs) |
Tomasz Bursztyka | ea2431f | 2018-01-30 13:49:01 +0100 | [diff] [blame] | 47 | }; |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 48 | |
Jordan Yates | 8d84626 | 2021-08-07 13:01:52 +1000 | [diff] [blame] | 49 | return spi_write_dt(&config->bus, &tx); |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 52 | static int apa102_update_rgb(const struct device *dev, struct led_rgb *pixels, |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 53 | size_t count) |
| 54 | { |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 55 | uint8_t *p = (uint8_t *)pixels; |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 56 | size_t i; |
| 57 | /* SOF (3 bits) followed by the 0 to 31 global dimming level */ |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 58 | uint8_t prefix = 0xE0 | 31; |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 59 | |
| 60 | /* Rewrite to the on-wire format */ |
| 61 | for (i = 0; i < count; i++) { |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 62 | uint8_t r = pixels[i].r; |
| 63 | uint8_t g = pixels[i].g; |
| 64 | uint8_t b = pixels[i].b; |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 65 | |
| 66 | *p++ = prefix; |
| 67 | *p++ = b; |
| 68 | *p++ = g; |
| 69 | *p++ = r; |
| 70 | } |
| 71 | |
| 72 | BUILD_ASSERT(sizeof(struct led_rgb) == 4); |
| 73 | return apa102_update(dev, pixels, sizeof(struct led_rgb) * count); |
| 74 | } |
| 75 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 76 | static int apa102_update_channels(const struct device *dev, uint8_t *channels, |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 77 | size_t num_channels) |
| 78 | { |
| 79 | /* Not implemented */ |
| 80 | return -EINVAL; |
| 81 | } |
| 82 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 83 | static int apa102_init(const struct device *dev) |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 84 | { |
Jordan Yates | 8d84626 | 2021-08-07 13:01:52 +1000 | [diff] [blame] | 85 | const struct apa102_config *config = dev->config; |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 86 | |
Bartosz Bilas | e077fb7 | 2022-12-03 14:49:05 +0100 | [diff] [blame] | 87 | if (!spi_is_ready_dt(&config->bus)) { |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 88 | return -ENODEV; |
| 89 | } |
| 90 | |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
Jordan Yates | 8d84626 | 2021-08-07 13:01:52 +1000 | [diff] [blame] | 94 | static const struct apa102_config apa102_config = { |
| 95 | .bus = SPI_DT_SPEC_INST_GET( |
| 96 | 0, SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8), 0) |
| 97 | }; |
Michael Hope | 8ee282e | 2018-02-12 22:35:20 +0100 | [diff] [blame] | 98 | |
| 99 | static const struct led_strip_driver_api apa102_api = { |
| 100 | .update_rgb = apa102_update_rgb, |
| 101 | .update_channels = apa102_update_channels, |
| 102 | }; |
| 103 | |
Gerard Marull-Paretas | 233fbf4 | 2021-04-28 11:14:49 +0200 | [diff] [blame] | 104 | DEVICE_DT_INST_DEFINE(0, apa102_init, NULL, |
Jordan Yates | 8d84626 | 2021-08-07 13:01:52 +1000 | [diff] [blame] | 105 | NULL, &apa102_config, POST_KERNEL, |
| 106 | CONFIG_LED_STRIP_INIT_PRIORITY, &apa102_api); |