1 /*
2  * Copyright (c) 2020 Teslabs Engineering S.L.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 
9 #include <zephyr/drivers/mipi_dsi.h>
10 
mipi_dsi_generic_read(const struct device * dev,uint8_t channel,const void * params,size_t nparams,void * buf,size_t len)11 ssize_t mipi_dsi_generic_read(const struct device *dev, uint8_t channel,
12 			      const void *params, size_t nparams,
13 			      void *buf, size_t len)
14 {
15 	struct mipi_dsi_msg msg = {
16 		.tx_len = nparams,
17 		.tx_buf = params,
18 		.rx_len = len,
19 		.rx_buf = buf
20 	};
21 
22 	switch (nparams) {
23 	case 0U:
24 		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
25 		break;
26 
27 	case 1U:
28 		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
29 		break;
30 
31 	case 2U:
32 		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
33 		break;
34 
35 	default:
36 		return -EINVAL;
37 	}
38 
39 	return mipi_dsi_transfer(dev, channel, &msg);
40 }
41 
mipi_dsi_generic_write(const struct device * dev,uint8_t channel,const void * buf,size_t len)42 ssize_t mipi_dsi_generic_write(const struct device *dev, uint8_t channel,
43 			       const void *buf, size_t len)
44 {
45 	struct mipi_dsi_msg msg = {
46 		.tx_buf = buf,
47 		.tx_len = len
48 	};
49 
50 	switch (len) {
51 	case 0U:
52 		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
53 		break;
54 
55 	case 1U:
56 		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
57 		break;
58 
59 	case 2U:
60 		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
61 		break;
62 
63 	default:
64 		msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
65 		break;
66 	}
67 
68 	return mipi_dsi_transfer(dev, channel, &msg);
69 }
70 
mipi_dsi_dcs_read(const struct device * dev,uint8_t channel,uint8_t cmd,void * buf,size_t len)71 ssize_t mipi_dsi_dcs_read(const struct device *dev, uint8_t channel,
72 			  uint8_t cmd, void *buf, size_t len)
73 {
74 	struct mipi_dsi_msg msg = {
75 		.type = MIPI_DSI_DCS_READ,
76 		.cmd = cmd,
77 		.rx_buf = buf,
78 		.rx_len = len
79 	};
80 
81 	return mipi_dsi_transfer(dev, channel, &msg);
82 }
83 
mipi_dsi_dcs_write(const struct device * dev,uint8_t channel,uint8_t cmd,const void * buf,size_t len)84 ssize_t mipi_dsi_dcs_write(const struct device *dev, uint8_t channel,
85 			   uint8_t cmd, const void *buf, size_t len)
86 {
87 	struct mipi_dsi_msg msg = {
88 		.cmd = cmd,
89 		.tx_buf = buf,
90 		.tx_len = len
91 	};
92 
93 	switch (len) {
94 	case 0U:
95 		msg.type = MIPI_DSI_DCS_SHORT_WRITE;
96 		break;
97 
98 	case 1U:
99 		msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
100 		break;
101 
102 	default:
103 		msg.type = MIPI_DSI_DCS_LONG_WRITE;
104 		break;
105 	}
106 
107 	return mipi_dsi_transfer(dev, channel, &msg);
108 }
109