drivers: display: st7735r: use gpio_dt_spec

Simplify the driver implementation by using gpio_dt_spec. Note that most
internal functions have been changed to accept a device instance instead
of data/config references to make transition easier and to align with
most other drivers.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-01-31 12:16:45 +01:00 committed by Anas Nashif
commit cd1fb2972e

View file

@ -28,16 +28,10 @@ LOG_MODULE_REGISTER(display_st7735r, CONFIG_DISPLAY_LOG_LEVEL);
#define ST7735R_PIXEL_SIZE 2u #define ST7735R_PIXEL_SIZE 2u
struct st7735r_gpio_data {
const char *const name;
gpio_dt_flags_t flags;
gpio_pin_t pin;
};
struct st7735r_config { struct st7735r_config {
struct spi_dt_spec bus; struct spi_dt_spec bus;
struct st7735r_gpio_data cmd_data; struct gpio_dt_spec cmd_data;
struct st7735r_gpio_data reset; struct gpio_dt_spec reset;
uint16_t height; uint16_t height;
uint16_t width; uint16_t width;
uint8_t madctl; uint8_t madctl;
@ -59,9 +53,6 @@ struct st7735r_config {
}; };
struct st7735r_data { struct st7735r_data {
const struct st7735r_config *config;
const struct device *cmd_data_dev;
const struct device *reset_dev;
uint16_t x_offset; uint16_t x_offset;
uint16_t y_offset; uint16_t y_offset;
}; };
@ -73,20 +64,23 @@ static void st7735r_set_lcd_margins(struct st7735r_data *data,
data->y_offset = y_offset; data->y_offset = y_offset;
} }
static void st7735r_set_cmd(struct st7735r_data *data, int is_cmd) static void st7735r_set_cmd(const struct device *dev, int is_cmd)
{ {
gpio_pin_set(data->cmd_data_dev, data->config->cmd_data.pin, is_cmd); const struct st7735r_config *config = dev->config;
gpio_pin_set_dt(&config->cmd_data, is_cmd);
} }
static int st7735r_transmit(struct st7735r_data *data, uint8_t cmd, static int st7735r_transmit(const struct device *dev, uint8_t cmd,
const uint8_t *tx_data, size_t tx_count) const uint8_t *tx_data, size_t tx_count)
{ {
const struct st7735r_config *config = dev->config;
struct spi_buf tx_buf = { .buf = &cmd, .len = 1 }; struct spi_buf tx_buf = { .buf = &cmd, .len = 1 };
struct spi_buf_set tx_bufs = { .buffers = &tx_buf, .count = 1 }; struct spi_buf_set tx_bufs = { .buffers = &tx_buf, .count = 1 };
int ret; int ret;
st7735r_set_cmd(data, 1); st7735r_set_cmd(dev, 1);
ret = spi_write_dt(&data->config->bus, &tx_bufs); ret = spi_write_dt(&config->bus, &tx_bufs);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
@ -94,8 +88,8 @@ static int st7735r_transmit(struct st7735r_data *data, uint8_t cmd,
if (tx_data != NULL) { if (tx_data != NULL) {
tx_buf.buf = (void *)tx_data; tx_buf.buf = (void *)tx_data;
tx_buf.len = tx_count; tx_buf.len = tx_count;
st7735r_set_cmd(data, 0); st7735r_set_cmd(dev, 0);
ret = spi_write_dt(&data->config->bus, &tx_bufs); ret = spi_write_dt(&config->bus, &tx_bufs);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
@ -104,11 +98,11 @@ static int st7735r_transmit(struct st7735r_data *data, uint8_t cmd,
return 0; return 0;
} }
static int st7735r_exit_sleep(struct st7735r_data *data) static int st7735r_exit_sleep(const struct device *dev)
{ {
int ret; int ret;
ret = st7735r_transmit(data, ST7735R_CMD_SLEEP_OUT, NULL, 0); ret = st7735r_transmit(dev, ST7735R_CMD_SLEEP_OUT, NULL, 0);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
@ -118,17 +112,18 @@ static int st7735r_exit_sleep(struct st7735r_data *data)
return 0; return 0;
} }
static int st7735r_reset_display(struct st7735r_data *data) static int st7735r_reset_display(const struct device *dev)
{ {
const struct st7735r_config *config = dev->config;
int ret; int ret;
LOG_DBG("Resetting display"); LOG_DBG("Resetting display");
if (data->config->reset.name) { if (config->reset.port != NULL) {
gpio_pin_set(data->reset_dev, data->config->reset.pin, 1); gpio_pin_set_dt(&config->reset, 1);
k_sleep(ST7735R_RESET_TIME); k_sleep(ST7735R_RESET_TIME);
gpio_pin_set(data->reset_dev, data->config->reset.pin, 0); gpio_pin_set_dt(&config->reset, 0);
} else { } else {
ret = st7735r_transmit(data, ST7735R_CMD_SW_RESET, NULL, 0); ret = st7735r_transmit(dev, ST7735R_CMD_SW_RESET, NULL, 0);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
@ -141,16 +136,12 @@ static int st7735r_reset_display(struct st7735r_data *data)
static int st7735r_blanking_on(const struct device *dev) static int st7735r_blanking_on(const struct device *dev)
{ {
struct st7735r_data *data = dev->data; return st7735r_transmit(dev, ST7735R_CMD_DISP_OFF, NULL, 0);
return st7735r_transmit(data, ST7735R_CMD_DISP_OFF, NULL, 0);
} }
static int st7735r_blanking_off(const struct device *dev) static int st7735r_blanking_off(const struct device *dev)
{ {
struct st7735r_data *data = dev->data; return st7735r_transmit(dev, ST7735R_CMD_DISP_ON, NULL, 0);
return st7735r_transmit(data, ST7735R_CMD_DISP_ON, NULL, 0);
} }
static int st7735r_read(const struct device *dev, static int st7735r_read(const struct device *dev,
@ -162,10 +153,11 @@ static int st7735r_read(const struct device *dev,
return -ENOTSUP; return -ENOTSUP;
} }
static int st7735r_set_mem_area(struct st7735r_data *data, static int st7735r_set_mem_area(const struct device *dev,
const uint16_t x, const uint16_t y, const uint16_t x, const uint16_t y,
const uint16_t w, const uint16_t h) const uint16_t w, const uint16_t h)
{ {
struct st7735r_data *data = dev->data;
uint16_t spi_data[2]; uint16_t spi_data[2];
int ret; int ret;
@ -175,14 +167,14 @@ static int st7735r_set_mem_area(struct st7735r_data *data,
spi_data[0] = sys_cpu_to_be16(ram_x); spi_data[0] = sys_cpu_to_be16(ram_x);
spi_data[1] = sys_cpu_to_be16(ram_x + w - 1); spi_data[1] = sys_cpu_to_be16(ram_x + w - 1);
ret = st7735r_transmit(data, ST7735R_CMD_CASET, (uint8_t *)&spi_data[0], 4); ret = st7735r_transmit(dev, ST7735R_CMD_CASET, (uint8_t *)&spi_data[0], 4);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
spi_data[0] = sys_cpu_to_be16(ram_y); spi_data[0] = sys_cpu_to_be16(ram_y);
spi_data[1] = sys_cpu_to_be16(ram_y + h - 1); spi_data[1] = sys_cpu_to_be16(ram_y + h - 1);
ret = st7735r_transmit(data, ST7735R_CMD_RASET, (uint8_t *)&spi_data[0], 4); ret = st7735r_transmit(dev, ST7735R_CMD_RASET, (uint8_t *)&spi_data[0], 4);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
@ -196,7 +188,7 @@ static int st7735r_write(const struct device *dev,
const struct display_buffer_descriptor *desc, const struct display_buffer_descriptor *desc,
const void *buf) const void *buf)
{ {
struct st7735r_data *data = dev->data; struct st7735r_config *config = dev->config;
const uint8_t *write_data_start = (uint8_t *) buf; const uint8_t *write_data_start = (uint8_t *) buf;
struct spi_buf tx_buf; struct spi_buf tx_buf;
struct spi_buf_set tx_bufs; struct spi_buf_set tx_bufs;
@ -211,7 +203,7 @@ static int st7735r_write(const struct device *dev,
LOG_DBG("Writing %dx%d (w,h) @ %dx%d (x,y)", LOG_DBG("Writing %dx%d (w,h) @ %dx%d (x,y)",
desc->width, desc->height, x, y); desc->width, desc->height, x, y);
ret = st7735r_set_mem_area(data, x, y, desc->width, desc->height); ret = st7735r_set_mem_area(dev, x, y, desc->width, desc->height);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
@ -224,7 +216,7 @@ static int st7735r_write(const struct device *dev,
nbr_of_writes = 1U; nbr_of_writes = 1U;
} }
ret = st7735r_transmit(data, ST7735R_CMD_RAMWR, ret = st7735r_transmit(dev, ST7735R_CMD_RAMWR,
(void *) write_data_start, (void *) write_data_start,
desc->width * ST7735R_PIXEL_SIZE * write_h); desc->width * ST7735R_PIXEL_SIZE * write_h);
if (ret < 0) { if (ret < 0) {
@ -238,7 +230,7 @@ static int st7735r_write(const struct device *dev,
for (write_cnt = 1U; write_cnt < nbr_of_writes; ++write_cnt) { for (write_cnt = 1U; write_cnt < nbr_of_writes; ++write_cnt) {
tx_buf.buf = (void *)write_data_start; tx_buf.buf = (void *)write_data_start;
tx_buf.len = desc->width * ST7735R_PIXEL_SIZE * write_h; tx_buf.len = desc->width * ST7735R_PIXEL_SIZE * write_h;
ret = spi_write_dt(&data->config->bus, &tx_bufs); ret = spi_write_dt(&config->bus, &tx_bufs);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
@ -317,116 +309,117 @@ static int st7735r_set_orientation(const struct device *dev,
return -ENOTSUP; return -ENOTSUP;
} }
static int st7735r_lcd_init(struct st7735r_data *data) static int st7735r_lcd_init(const struct device *dev)
{ {
const struct st7735r_config *config = data->config; const struct st7735r_config *config = dev->config;
struct st7735r_data *data = dev->data;
int ret; int ret;
st7735r_set_lcd_margins(data, data->x_offset, data->y_offset); st7735r_set_lcd_margins(dev, data->x_offset, data->y_offset);
ret = st7735r_transmit(data, ST7735R_CMD_FRMCTR1, config->frmctr1, ret = st7735r_transmit(dev, ST7735R_CMD_FRMCTR1, config->frmctr1,
sizeof(config->frmctr1)); sizeof(config->frmctr1));
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_FRMCTR2, config->frmctr2, ret = st7735r_transmit(dev, ST7735R_CMD_FRMCTR2, config->frmctr2,
sizeof(config->frmctr2)); sizeof(config->frmctr2));
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_FRMCTR3, config->frmctr3, ret = st7735r_transmit(dev, ST7735R_CMD_FRMCTR3, config->frmctr3,
sizeof(config->frmctr3)); sizeof(config->frmctr3));
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_INVCTR, &config->invctr, 1); ret = st7735r_transmit(dev, ST7735R_CMD_INVCTR, &config->invctr, 1);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_PWCTR1, config->pwctr1, ret = st7735r_transmit(dev, ST7735R_CMD_PWCTR1, config->pwctr1,
sizeof(config->pwctr1)); sizeof(config->pwctr1));
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_PWCTR2, config->pwctr2, ret = st7735r_transmit(dev, ST7735R_CMD_PWCTR2, config->pwctr2,
sizeof(config->pwctr2)); sizeof(config->pwctr2));
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_PWCTR3, config->pwctr3, ret = st7735r_transmit(dev, ST7735R_CMD_PWCTR3, config->pwctr3,
sizeof(config->pwctr3)); sizeof(config->pwctr3));
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_PWCTR4, config->pwctr4, ret = st7735r_transmit(dev, ST7735R_CMD_PWCTR4, config->pwctr4,
sizeof(config->pwctr4)); sizeof(config->pwctr4));
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_PWCTR5, config->pwctr5, ret = st7735r_transmit(dev, ST7735R_CMD_PWCTR5, config->pwctr5,
sizeof(config->pwctr5)); sizeof(config->pwctr5));
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_VMCTR1, &config->vmctr1, 1); ret = st7735r_transmit(dev, ST7735R_CMD_VMCTR1, &config->vmctr1, 1);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_INV_OFF, NULL, 0); ret = st7735r_transmit(dev, ST7735R_CMD_INV_OFF, NULL, 0);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_MADCTL, &config->madctl, 1); ret = st7735r_transmit(dev, ST7735R_CMD_MADCTL, &config->madctl, 1);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_COLMOD, &config->colmod, 1); ret = st7735r_transmit(dev, ST7735R_CMD_COLMOD, &config->colmod, 1);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_CASET, config->caset, ret = st7735r_transmit(dev, ST7735R_CMD_CASET, config->caset,
sizeof(config->caset)); sizeof(config->caset));
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_RASET, config->raset, ret = st7735r_transmit(dev, ST7735R_CMD_RASET, config->raset,
sizeof(config->raset)); sizeof(config->raset));
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_GAMCTRP1, config->gamctrp1, ret = st7735r_transmit(dev, ST7735R_CMD_GAMCTRP1, config->gamctrp1,
sizeof(config->gamctrp1)); sizeof(config->gamctrp1));
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_GAMCTRN1, config->gamctrn1, ret = st7735r_transmit(dev, ST7735R_CMD_GAMCTRN1, config->gamctrn1,
sizeof(config->gamctrn1)); sizeof(config->gamctrn1));
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_NORON, NULL, 0); ret = st7735r_transmit(dev, ST7735R_CMD_NORON, NULL, 0);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = st7735r_transmit(data, ST7735R_CMD_DISP_ON, NULL, 0); ret = st7735r_transmit(dev, ST7735R_CMD_DISP_ON, NULL, 0);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
@ -436,7 +429,6 @@ static int st7735r_lcd_init(struct st7735r_data *data)
static int st7735r_init(const struct device *dev) static int st7735r_init(const struct device *dev)
{ {
struct st7735r_data *data = dev->data;
const struct st7735r_config *config = dev->config; const struct st7735r_config *config = dev->config;
int ret; int ret;
@ -445,47 +437,44 @@ static int st7735r_init(const struct device *dev)
return -ENODEV; return -ENODEV;
} }
if (config->reset.name) { if (config->reset.port != NULL) {
data->reset_dev = device_get_binding(config->reset.name); if (!device_is_ready(&config->reset)) {
if (data->reset_dev == NULL) { LOG_ERR("Reset GPIO port for display not ready");
LOG_ERR("Could not get GPIO port for display reset");
return -ENODEV; return -ENODEV;
} }
ret = gpio_pin_configure(data->reset_dev, config->reset.pin, ret = gpio_pin_configure_dt(&config->reset,
GPIO_OUTPUT_INACTIVE | config->reset.flags); GPIO_OUTPUT_INACTIVE);
if (ret) { if (ret) {
LOG_ERR("Couldn't configure reset pin"); LOG_ERR("Couldn't configure reset pin");
return ret; return ret;
} }
} }
data->cmd_data_dev = device_get_binding(config->cmd_data.name); if (!device_is_ready(&config->cmd_data)) {
if (data->cmd_data_dev == NULL) { LOG_ERR("cmd/DATA GPIO port not ready");
LOG_ERR("Could not get GPIO port for cmd/DATA port");
return -ENODEV; return -ENODEV;
} }
ret = gpio_pin_configure(data->cmd_data_dev, config->cmd_data.pin, ret = gpio_pin_configure_dt(&config->cmd_data, GPIO_OUTPUT);
GPIO_OUTPUT | config->cmd_data.flags);
if (ret) { if (ret) {
LOG_ERR("Couldn't configure cmd/DATA pin"); LOG_ERR("Couldn't configure cmd/DATA pin");
return ret; return ret;
} }
ret = st7735r_reset_display(data); ret = st7735r_reset_display(dev);
if (ret < 0) { if (ret < 0) {
LOG_ERR("Couldn't reset display"); LOG_ERR("Couldn't reset display");
return ret; return ret;
} }
ret = st7735r_exit_sleep(data); ret = st7735r_exit_sleep(dev);
if (ret < 0) { if (ret < 0) {
LOG_ERR("Couldn't exit sleep"); LOG_ERR("Couldn't exit sleep");
return ret; return ret;
} }
ret = st7735r_lcd_init(data); ret = st7735r_lcd_init(dev);
if (ret < 0) { if (ret < 0) {
LOG_ERR("Couldn't init LCD"); LOG_ERR("Couldn't init LCD");
return ret; return ret;
@ -499,14 +488,13 @@ static int st7735r_pm_action(const struct device *dev,
enum pm_device_action action) enum pm_device_action action)
{ {
int ret = 0; int ret = 0;
struct st7735r_data *data = dev->data;
switch (action) { switch (action) {
case PM_DEVICE_ACTION_RESUME: case PM_DEVICE_ACTION_RESUME:
ret = st7735r_exit_sleep(data); ret = st7735r_exit_sleep(dev);
break; break;
case PM_DEVICE_ACTION_SUSPEND: case PM_DEVICE_ACTION_SUSPEND:
ret = st7735r_transmit(data, ST7735R_CMD_SLEEP_IN, NULL, 0); ret = st7735r_transmit(dev, ST7735R_CMD_SLEEP_IN, NULL, 0);
break; break;
default: default:
ret = -ENOTSUP; ret = -ENOTSUP;
@ -532,23 +520,11 @@ static const struct display_driver_api st7735r_api = {
#define ST7735R_INIT(inst) \ #define ST7735R_INIT(inst) \
static struct st7735r_data st7735r_data_ ## inst; \
\
const static struct st7735r_config st7735r_config_ ## inst = { \ const static struct st7735r_config st7735r_config_ ## inst = { \
.bus = SPI_DT_SPEC_INST_GET( \ .bus = SPI_DT_SPEC_INST_GET( \
inst, SPI_OP_MODE_MASTER | SPI_WORD_SET(8), 0), \ inst, SPI_OP_MODE_MASTER | SPI_WORD_SET(8), 0), \
.cmd_data.name = DT_INST_GPIO_LABEL(inst, cmd_data_gpios), \ .cmd_data = GPIO_DT_SPEC_INST_GET(inst, cmd_data_gpios), \
.cmd_data.pin = DT_INST_GPIO_PIN(inst, cmd_data_gpios), \ .reset = GPIO_DT_SPEC_INST_GET_OR(inst, reset_gpios, {}), \
.cmd_data.flags = DT_INST_GPIO_FLAGS(inst, cmd_data_gpios), \
.reset.name = UTIL_AND( \
DT_INST_NODE_HAS_PROP(inst, reset_gpios), \
DT_INST_GPIO_LABEL(inst, reset_gpios)), \
.reset.pin = UTIL_AND( \
DT_INST_NODE_HAS_PROP(inst, reset_gpios), \
DT_INST_GPIO_PIN(inst, reset_gpios)), \
.reset.flags = UTIL_AND( \
DT_INST_NODE_HAS_PROP(inst, reset_gpios), \
DT_INST_GPIO_FLAGS(inst, reset_gpios)), \
.width = DT_INST_PROP(inst, width), \ .width = DT_INST_PROP(inst, width), \
.height = DT_INST_PROP(inst, height), \ .height = DT_INST_PROP(inst, height), \
.madctl = DT_INST_PROP(inst, madctl), \ .madctl = DT_INST_PROP(inst, madctl), \
@ -570,7 +546,6 @@ static const struct display_driver_api st7735r_api = {
}; \ }; \
\ \
static struct st7735r_data st7735r_data_ ## inst = { \ static struct st7735r_data st7735r_data_ ## inst = { \
.config = &st7735r_config_ ## inst, \
.x_offset = DT_INST_PROP(inst, x_offset), \ .x_offset = DT_INST_PROP(inst, x_offset), \
.y_offset = DT_INST_PROP(inst, y_offset), \ .y_offset = DT_INST_PROP(inst, y_offset), \
}; \ }; \