2018-02-13 05:35:20 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Google LLC.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-26 01:01:50 +08:00
|
|
|
#define DT_DRV_COMPAT apa_apa102
|
|
|
|
|
2018-02-13 05:35:20 +08:00
|
|
|
#include <errno.h>
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/led_strip.h>
|
|
|
|
#include <zephyr/drivers/spi.h>
|
|
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
#include <zephyr/sys/util.h>
|
2020-05-14 10:24:10 +08:00
|
|
|
|
2021-08-07 11:01:52 +08:00
|
|
|
struct apa102_config {
|
|
|
|
struct spi_dt_spec bus;
|
2018-02-13 05:35:20 +08:00
|
|
|
};
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int apa102_update(const struct device *dev, void *buf, size_t size)
|
2018-02-13 05:35:20 +08:00
|
|
|
{
|
2021-08-07 11:01:52 +08:00
|
|
|
const struct apa102_config *config = dev->config;
|
|
|
|
static const uint8_t zeros[] = { 0, 0, 0, 0 };
|
|
|
|
static const uint8_t ones[] = { 0xFF, 0xFF, 0xFF, 0xFF };
|
2018-01-30 20:49:01 +08:00
|
|
|
const struct spi_buf tx_bufs[] = {
|
2018-02-13 05:35:20 +08:00
|
|
|
{
|
|
|
|
/* Start frame: at least 32 zeros */
|
2020-05-28 00:26:57 +08:00
|
|
|
.buf = (uint8_t *)zeros,
|
2018-02-13 05:35:20 +08:00
|
|
|
.len = sizeof(zeros),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
/* LED data itself */
|
|
|
|
.buf = buf,
|
|
|
|
.len = size,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
/* End frame: at least 32 ones to clock the
|
|
|
|
* remaining bits to the LEDs at the end of
|
|
|
|
* the strip.
|
|
|
|
*/
|
2020-05-28 00:26:57 +08:00
|
|
|
.buf = (uint8_t *)ones,
|
2018-02-13 05:35:20 +08:00
|
|
|
.len = sizeof(ones),
|
|
|
|
},
|
|
|
|
};
|
2018-01-30 20:49:01 +08:00
|
|
|
const struct spi_buf_set tx = {
|
|
|
|
.buffers = tx_bufs,
|
2018-12-08 23:39:39 +08:00
|
|
|
.count = ARRAY_SIZE(tx_bufs)
|
2018-01-30 20:49:01 +08:00
|
|
|
};
|
2018-02-13 05:35:20 +08:00
|
|
|
|
2021-08-07 11:01:52 +08:00
|
|
|
return spi_write_dt(&config->bus, &tx);
|
2018-02-13 05:35:20 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int apa102_update_rgb(const struct device *dev, struct led_rgb *pixels,
|
2018-02-13 05:35:20 +08:00
|
|
|
size_t count)
|
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t *p = (uint8_t *)pixels;
|
2018-02-13 05:35:20 +08:00
|
|
|
size_t i;
|
|
|
|
/* SOF (3 bits) followed by the 0 to 31 global dimming level */
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t prefix = 0xE0 | 31;
|
2018-02-13 05:35:20 +08:00
|
|
|
|
|
|
|
/* Rewrite to the on-wire format */
|
|
|
|
for (i = 0; i < count; i++) {
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t r = pixels[i].r;
|
|
|
|
uint8_t g = pixels[i].g;
|
|
|
|
uint8_t b = pixels[i].b;
|
2018-02-13 05:35:20 +08:00
|
|
|
|
|
|
|
*p++ = prefix;
|
|
|
|
*p++ = b;
|
|
|
|
*p++ = g;
|
|
|
|
*p++ = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
BUILD_ASSERT(sizeof(struct led_rgb) == 4);
|
|
|
|
return apa102_update(dev, pixels, sizeof(struct led_rgb) * count);
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int apa102_update_channels(const struct device *dev, uint8_t *channels,
|
2018-02-13 05:35:20 +08:00
|
|
|
size_t num_channels)
|
|
|
|
{
|
|
|
|
/* Not implemented */
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int apa102_init(const struct device *dev)
|
2018-02-13 05:35:20 +08:00
|
|
|
{
|
2021-08-07 11:01:52 +08:00
|
|
|
const struct apa102_config *config = dev->config;
|
2018-02-13 05:35:20 +08:00
|
|
|
|
2021-08-07 11:01:52 +08:00
|
|
|
if (!spi_is_ready(&config->bus)) {
|
2018-02-13 05:35:20 +08:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-07 11:01:52 +08:00
|
|
|
static const struct apa102_config apa102_config = {
|
|
|
|
.bus = SPI_DT_SPEC_INST_GET(
|
|
|
|
0, SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8), 0)
|
|
|
|
};
|
2018-02-13 05:35:20 +08:00
|
|
|
|
|
|
|
static const struct led_strip_driver_api apa102_api = {
|
|
|
|
.update_rgb = apa102_update_rgb,
|
|
|
|
.update_channels = apa102_update_channels,
|
|
|
|
};
|
|
|
|
|
2021-04-28 17:14:49 +08:00
|
|
|
DEVICE_DT_INST_DEFINE(0, apa102_init, NULL,
|
2021-08-07 11:01:52 +08:00
|
|
|
NULL, &apa102_config, POST_KERNEL,
|
|
|
|
CONFIG_LED_STRIP_INIT_PRIORITY, &apa102_api);
|