1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
6 * Copyright (C) 2019 STMicroelectronics - All Rights Reserved
7 * Andrzej Hajda <a.hajda@samsung.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sub license, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
24 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
25 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
26 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
27 * USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Mipi_dsi.c contains a set of dsi helpers.
30 * This file is inspired from the drm helper file drivers/gpu/drm/drm_mipi_dsi.c
39 #include <mipi_display.h>
41 #include <dm/devres.h>
46 * These functions contain some common logic and helpers to deal with MIPI DSI
49 * Helpers are provided for a number of standard MIPI DSI command as well as a
50 * subset of the MIPI DCS command set.
54 * mipi_dsi_attach - attach a DSI device to its DSI host
55 * @dsi: DSI peripheral
57 int mipi_dsi_attach(struct mipi_dsi_device *dsi)
59 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
61 if (!ops || !ops->attach)
64 return ops->attach(dsi->host, dsi);
66 EXPORT_SYMBOL(mipi_dsi_attach);
69 * mipi_dsi_detach - detach a DSI device from its DSI host
70 * @dsi: DSI peripheral
72 int mipi_dsi_detach(struct mipi_dsi_device *dsi)
74 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
76 if (!ops || !ops->detach)
79 return ops->detach(dsi->host, dsi);
81 EXPORT_SYMBOL(mipi_dsi_detach);
84 * mipi_dsi_device_transfer - transfer message to a DSI device
85 * @dsi: DSI peripheral
88 static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
89 struct mipi_dsi_msg *msg)
91 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
93 if (!ops || !ops->transfer)
96 if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
97 msg->flags |= MIPI_DSI_MSG_USE_LPM;
99 return ops->transfer(dsi->host, msg);
103 * mipi_dsi_packet_format_is_short - check if a packet is of the short format
104 * @type: MIPI DSI data type of the packet
106 * Return: true if the packet for the given data type is a short packet, false
109 bool mipi_dsi_packet_format_is_short(u8 type)
112 case MIPI_DSI_V_SYNC_START:
113 case MIPI_DSI_V_SYNC_END:
114 case MIPI_DSI_H_SYNC_START:
115 case MIPI_DSI_H_SYNC_END:
116 case MIPI_DSI_END_OF_TRANSMISSION:
117 case MIPI_DSI_COLOR_MODE_OFF:
118 case MIPI_DSI_COLOR_MODE_ON:
119 case MIPI_DSI_SHUTDOWN_PERIPHERAL:
120 case MIPI_DSI_TURN_ON_PERIPHERAL:
121 case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
122 case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
123 case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
124 case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
125 case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
126 case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
127 case MIPI_DSI_DCS_SHORT_WRITE:
128 case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
129 case MIPI_DSI_DCS_READ:
130 case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
136 EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
139 * mipi_dsi_packet_format_is_long - check if a packet is of the long format
140 * @type: MIPI DSI data type of the packet
142 * Return: true if the packet for the given data type is a long packet, false
145 bool mipi_dsi_packet_format_is_long(u8 type)
148 case MIPI_DSI_NULL_PACKET:
149 case MIPI_DSI_BLANKING_PACKET:
150 case MIPI_DSI_GENERIC_LONG_WRITE:
151 case MIPI_DSI_DCS_LONG_WRITE:
152 case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
153 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
154 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
155 case MIPI_DSI_PACKED_PIXEL_STREAM_30:
156 case MIPI_DSI_PACKED_PIXEL_STREAM_36:
157 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
158 case MIPI_DSI_PACKED_PIXEL_STREAM_16:
159 case MIPI_DSI_PACKED_PIXEL_STREAM_18:
160 case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
161 case MIPI_DSI_PACKED_PIXEL_STREAM_24:
167 EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
170 * mipi_dsi_create_packet - create a packet from a message according to the
172 * @packet: pointer to a DSI packet structure
173 * @msg: message to translate into a packet
175 * Return: 0 on success or a negative error code on failure.
177 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
178 const struct mipi_dsi_msg *msg)
183 /* do some minimum sanity checking */
184 if (!mipi_dsi_packet_format_is_short(msg->type) &&
185 !mipi_dsi_packet_format_is_long(msg->type))
188 if (msg->channel > 3)
191 memset(packet, 0, sizeof(*packet));
192 packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
194 /* TODO: compute ECC if hardware support is not available */
197 * Long write packets contain the word count in header bytes 1 and 2.
198 * The payload follows the header and is word count bytes long.
200 * Short write packets encode up to two parameters in header bytes 1
203 if (mipi_dsi_packet_format_is_long(msg->type)) {
204 packet->header[1] = (msg->tx_len >> 0) & 0xff;
205 packet->header[2] = (msg->tx_len >> 8) & 0xff;
207 packet->payload_length = msg->tx_len;
208 packet->payload = msg->tx_buf;
210 const u8 *tx = msg->tx_buf;
212 packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
213 packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
216 packet->size = sizeof(packet->header) + packet->payload_length;
220 EXPORT_SYMBOL(mipi_dsi_create_packet);
223 * mipi_dsi_shutdown_peripheral() - sends a Shutdown Peripheral command
224 * @dsi: DSI peripheral device
226 * Return: 0 on success or a negative error code on failure.
228 int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
230 struct mipi_dsi_msg msg = {
231 .channel = dsi->channel,
232 .type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
233 .tx_buf = (u8 [2]) { 0, 0 },
236 int ret = mipi_dsi_device_transfer(dsi, &msg);
238 return (ret < 0) ? ret : 0;
240 EXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
243 * mipi_dsi_turn_on_peripheral() - sends a Turn On Peripheral command
244 * @dsi: DSI peripheral device
246 * Return: 0 on success or a negative error code on failure.
248 int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
250 struct mipi_dsi_msg msg = {
251 .channel = dsi->channel,
252 .type = MIPI_DSI_TURN_ON_PERIPHERAL,
253 .tx_buf = (u8 [2]) { 0, 0 },
256 int ret = mipi_dsi_device_transfer(dsi, &msg);
258 return (ret < 0) ? ret : 0;
260 EXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
263 * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the
264 * the payload in a long packet transmitted from the peripheral back to the
266 * @dsi: DSI peripheral device
267 * @value: the maximum size of the payload
269 * Return: 0 on success or a negative error code on failure.
271 int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
274 u8 tx[2] = { value & 0xff, value >> 8 };
275 struct mipi_dsi_msg msg = {
276 .channel = dsi->channel,
277 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
278 .tx_len = sizeof(tx),
281 int ret = mipi_dsi_device_transfer(dsi, &msg);
283 return (ret < 0) ? ret : 0;
285 EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
288 * mipi_dsi_generic_write() - transmit data using a generic write packet
289 * @dsi: DSI peripheral device
290 * @payload: buffer containing the payload
291 * @size: size of payload buffer
293 * This function will automatically choose the right data type depending on
294 * the payload length.
296 * Return: The number of bytes transmitted on success or a negative error code
299 ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
302 struct mipi_dsi_msg msg = {
303 .channel = dsi->channel,
310 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
314 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
318 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
322 msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
326 return mipi_dsi_device_transfer(dsi, &msg);
328 EXPORT_SYMBOL(mipi_dsi_generic_write);
331 * mipi_dsi_generic_read() - receive data using a generic read packet
332 * @dsi: DSI peripheral device
333 * @params: buffer containing the request parameters
334 * @num_params: number of request parameters
335 * @data: buffer in which to return the received data
336 * @size: size of receive buffer
338 * This function will automatically choose the right data type depending on
339 * the number of parameters passed in.
341 * Return: The number of bytes successfully read or a negative error code on
344 ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
345 size_t num_params, void *data, size_t size)
347 struct mipi_dsi_msg msg = {
348 .channel = dsi->channel,
349 .tx_len = num_params,
355 switch (num_params) {
357 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
361 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
365 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
372 return mipi_dsi_device_transfer(dsi, &msg);
374 EXPORT_SYMBOL(mipi_dsi_generic_read);
377 * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
378 * @dsi: DSI peripheral device
379 * @data: buffer containing data to be transmitted
380 * @len: size of transmission buffer
382 * This function will automatically choose the right data type depending on
383 * the command payload length.
385 * Return: The number of bytes successfully transmitted or a negative error
388 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
389 const void *data, size_t len)
391 struct mipi_dsi_msg msg = {
392 .channel = dsi->channel,
402 msg.type = MIPI_DSI_DCS_SHORT_WRITE;
406 msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
410 msg.type = MIPI_DSI_DCS_LONG_WRITE;
414 return mipi_dsi_device_transfer(dsi, &msg);
416 EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
419 * mipi_dsi_dcs_write() - send DCS write command
420 * @dsi: DSI peripheral device
422 * @data: buffer containing the command payload
423 * @len: command payload length
425 * This function will automatically choose the right data type depending on
426 * the command payload length.
428 * Return: The number of bytes successfully transmitted or a negative error
431 ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
432 const void *data, size_t len)
441 tx = kmalloc(size, GFP_KERNEL);
445 /* concatenate the DCS command byte and the payload */
447 memcpy(&tx[1], data, len);
453 err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
460 EXPORT_SYMBOL(mipi_dsi_dcs_write);
463 * mipi_dsi_dcs_read() - send DCS read request command
464 * @dsi: DSI peripheral device
466 * @data: buffer in which to receive data
467 * @len: size of receive buffer
469 * Return: The number of bytes read or a negative error code on failure.
471 ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
474 struct mipi_dsi_msg msg = {
475 .channel = dsi->channel,
476 .type = MIPI_DSI_DCS_READ,
483 return mipi_dsi_device_transfer(dsi, &msg);
485 EXPORT_SYMBOL(mipi_dsi_dcs_read);
488 * mipi_dsi_dcs_nop() - send DCS nop packet
489 * @dsi: DSI peripheral device
491 * Return: 0 on success or a negative error code on failure.
493 int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
497 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
503 EXPORT_SYMBOL(mipi_dsi_dcs_nop);
506 * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
507 * @dsi: DSI peripheral device
509 * Return: 0 on success or a negative error code on failure.
511 int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
515 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
521 EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
524 * mipi_dsi_dcs_get_power_mode() - query the display module's current power
526 * @dsi: DSI peripheral device
527 * @mode: return location for the current power mode
529 * Return: 0 on success or a negative error code on failure.
531 int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
535 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
546 EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
549 * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
550 * data used by the interface
551 * @dsi: DSI peripheral device
552 * @format: return location for the pixel format
554 * Return: 0 on success or a negative error code on failure.
556 int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
560 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
571 EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
574 * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
575 * display module except interface communication
576 * @dsi: DSI peripheral device
578 * Return: 0 on success or a negative error code on failure.
580 int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
584 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
590 EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
593 * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
595 * @dsi: DSI peripheral device
597 * Return: 0 on success or a negative error code on failure.
599 int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
603 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
609 EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
612 * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
614 * @dsi: DSI peripheral device
616 * Return: 0 on success or a negative error code on failure.
618 int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
622 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
628 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
631 * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
633 * @dsi: DSI peripheral device
635 * Return: 0 on success or a negative error code on failure
637 int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
641 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
647 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
650 * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
651 * memory accessed by the host processor
652 * @dsi: DSI peripheral device
653 * @start: first column of frame memory
654 * @end: last column of frame memory
656 * Return: 0 on success or a negative error code on failure.
658 int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
661 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
664 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
671 EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
674 * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
675 * memory accessed by the host processor
676 * @dsi: DSI peripheral device
677 * @start: first page of frame memory
678 * @end: last page of frame memory
680 * Return: 0 on success or a negative error code on failure.
682 int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
685 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
688 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
695 EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
698 * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
699 * output signal on the TE signal line
700 * @dsi: DSI peripheral device
702 * Return: 0 on success or a negative error code on failure
704 int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
708 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
714 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
717 * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
718 * output signal on the TE signal line.
719 * @dsi: DSI peripheral device
720 * @mode: the Tearing Effect Output Line mode
722 * Return: 0 on success or a negative error code on failure
724 int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
725 enum mipi_dsi_dcs_tear_mode mode)
730 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
737 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
740 * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
741 * data used by the interface
742 * @dsi: DSI peripheral device
743 * @format: pixel format
745 * Return: 0 on success or a negative error code on failure.
747 int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
751 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
758 EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
761 * mipi_dsi_dcs_set_tear_scanline() - set the scanline to use as trigger for
762 * the Tearing Effect output signal of the display module
763 * @dsi: DSI peripheral device
764 * @scanline: scanline to use as trigger
766 * Return: 0 on success or a negative error code on failure
768 int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline)
770 u8 payload[3] = { MIPI_DCS_SET_TEAR_SCANLINE, scanline >> 8,
774 err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
780 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_scanline);
783 * mipi_dsi_dcs_set_display_brightness() - sets the brightness value of the
785 * @dsi: DSI peripheral device
786 * @brightness: brightness value
788 * Return: 0 on success or a negative error code on failure.
790 int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
793 u8 payload[2] = { brightness & 0xff, brightness >> 8 };
796 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
797 payload, sizeof(payload));
803 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_brightness);
806 * mipi_dsi_dcs_get_display_brightness() - gets the current brightness value
808 * @dsi: DSI peripheral device
809 * @brightness: brightness value
811 * Return: 0 on success or a negative error code on failure.
813 int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
818 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
819 brightness, sizeof(*brightness));
829 EXPORT_SYMBOL(mipi_dsi_dcs_get_display_brightness);