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>
45 * These functions contain some common logic and helpers to deal with MIPI DSI
48 * Helpers are provided for a number of standard MIPI DSI command as well as a
49 * subset of the MIPI DCS command set.
53 * mipi_dsi_attach - attach a DSI device to its DSI host
54 * @dsi: DSI peripheral
56 int mipi_dsi_attach(struct mipi_dsi_device *dsi)
58 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
60 if (!ops || !ops->attach)
63 return ops->attach(dsi->host, dsi);
65 EXPORT_SYMBOL(mipi_dsi_attach);
68 * mipi_dsi_detach - detach a DSI device from its DSI host
69 * @dsi: DSI peripheral
71 int mipi_dsi_detach(struct mipi_dsi_device *dsi)
73 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
75 if (!ops || !ops->detach)
78 return ops->detach(dsi->host, dsi);
80 EXPORT_SYMBOL(mipi_dsi_detach);
83 * mipi_dsi_device_transfer - transfer message to a DSI device
84 * @dsi: DSI peripheral
87 static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
88 struct mipi_dsi_msg *msg)
90 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
92 if (!ops || !ops->transfer)
95 if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
96 msg->flags |= MIPI_DSI_MSG_USE_LPM;
98 return ops->transfer(dsi->host, msg);
102 * mipi_dsi_packet_format_is_short - check if a packet is of the short format
103 * @type: MIPI DSI data type of the packet
105 * Return: true if the packet for the given data type is a short packet, false
108 bool mipi_dsi_packet_format_is_short(u8 type)
111 case MIPI_DSI_V_SYNC_START:
112 case MIPI_DSI_V_SYNC_END:
113 case MIPI_DSI_H_SYNC_START:
114 case MIPI_DSI_H_SYNC_END:
115 case MIPI_DSI_END_OF_TRANSMISSION:
116 case MIPI_DSI_COLOR_MODE_OFF:
117 case MIPI_DSI_COLOR_MODE_ON:
118 case MIPI_DSI_SHUTDOWN_PERIPHERAL:
119 case MIPI_DSI_TURN_ON_PERIPHERAL:
120 case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
121 case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
122 case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
123 case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
124 case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
125 case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
126 case MIPI_DSI_DCS_SHORT_WRITE:
127 case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
128 case MIPI_DSI_DCS_READ:
129 case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
135 EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
138 * mipi_dsi_packet_format_is_long - check if a packet is of the long format
139 * @type: MIPI DSI data type of the packet
141 * Return: true if the packet for the given data type is a long packet, false
144 bool mipi_dsi_packet_format_is_long(u8 type)
147 case MIPI_DSI_NULL_PACKET:
148 case MIPI_DSI_BLANKING_PACKET:
149 case MIPI_DSI_GENERIC_LONG_WRITE:
150 case MIPI_DSI_DCS_LONG_WRITE:
151 case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
152 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
153 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
154 case MIPI_DSI_PACKED_PIXEL_STREAM_30:
155 case MIPI_DSI_PACKED_PIXEL_STREAM_36:
156 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
157 case MIPI_DSI_PACKED_PIXEL_STREAM_16:
158 case MIPI_DSI_PACKED_PIXEL_STREAM_18:
159 case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
160 case MIPI_DSI_PACKED_PIXEL_STREAM_24:
166 EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
169 * mipi_dsi_create_packet - create a packet from a message according to the
171 * @packet: pointer to a DSI packet structure
172 * @msg: message to translate into a packet
174 * Return: 0 on success or a negative error code on failure.
176 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
177 const struct mipi_dsi_msg *msg)
182 /* do some minimum sanity checking */
183 if (!mipi_dsi_packet_format_is_short(msg->type) &&
184 !mipi_dsi_packet_format_is_long(msg->type))
187 if (msg->channel > 3)
190 memset(packet, 0, sizeof(*packet));
191 packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
193 /* TODO: compute ECC if hardware support is not available */
196 * Long write packets contain the word count in header bytes 1 and 2.
197 * The payload follows the header and is word count bytes long.
199 * Short write packets encode up to two parameters in header bytes 1
202 if (mipi_dsi_packet_format_is_long(msg->type)) {
203 packet->header[1] = (msg->tx_len >> 0) & 0xff;
204 packet->header[2] = (msg->tx_len >> 8) & 0xff;
206 packet->payload_length = msg->tx_len;
207 packet->payload = msg->tx_buf;
209 const u8 *tx = msg->tx_buf;
211 packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
212 packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
215 packet->size = sizeof(packet->header) + packet->payload_length;
219 EXPORT_SYMBOL(mipi_dsi_create_packet);
222 * mipi_dsi_shutdown_peripheral() - sends a Shutdown Peripheral command
223 * @dsi: DSI peripheral device
225 * Return: 0 on success or a negative error code on failure.
227 int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
229 struct mipi_dsi_msg msg = {
230 .channel = dsi->channel,
231 .type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
232 .tx_buf = (u8 [2]) { 0, 0 },
235 int ret = mipi_dsi_device_transfer(dsi, &msg);
237 return (ret < 0) ? ret : 0;
239 EXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
242 * mipi_dsi_turn_on_peripheral() - sends a Turn On Peripheral command
243 * @dsi: DSI peripheral device
245 * Return: 0 on success or a negative error code on failure.
247 int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
249 struct mipi_dsi_msg msg = {
250 .channel = dsi->channel,
251 .type = MIPI_DSI_TURN_ON_PERIPHERAL,
252 .tx_buf = (u8 [2]) { 0, 0 },
255 int ret = mipi_dsi_device_transfer(dsi, &msg);
257 return (ret < 0) ? ret : 0;
259 EXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
262 * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the
263 * the payload in a long packet transmitted from the peripheral back to the
265 * @dsi: DSI peripheral device
266 * @value: the maximum size of the payload
268 * Return: 0 on success or a negative error code on failure.
270 int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
273 u8 tx[2] = { value & 0xff, value >> 8 };
274 struct mipi_dsi_msg msg = {
275 .channel = dsi->channel,
276 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
277 .tx_len = sizeof(tx),
280 int ret = mipi_dsi_device_transfer(dsi, &msg);
282 return (ret < 0) ? ret : 0;
284 EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
287 * mipi_dsi_generic_write() - transmit data using a generic write packet
288 * @dsi: DSI peripheral device
289 * @payload: buffer containing the payload
290 * @size: size of payload buffer
292 * This function will automatically choose the right data type depending on
293 * the payload length.
295 * Return: The number of bytes transmitted on success or a negative error code
298 ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
301 struct mipi_dsi_msg msg = {
302 .channel = dsi->channel,
309 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
313 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
317 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
321 msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
325 return mipi_dsi_device_transfer(dsi, &msg);
327 EXPORT_SYMBOL(mipi_dsi_generic_write);
330 * mipi_dsi_generic_read() - receive data using a generic read packet
331 * @dsi: DSI peripheral device
332 * @params: buffer containing the request parameters
333 * @num_params: number of request parameters
334 * @data: buffer in which to return the received data
335 * @size: size of receive buffer
337 * This function will automatically choose the right data type depending on
338 * the number of parameters passed in.
340 * Return: The number of bytes successfully read or a negative error code on
343 ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
344 size_t num_params, void *data, size_t size)
346 struct mipi_dsi_msg msg = {
347 .channel = dsi->channel,
348 .tx_len = num_params,
354 switch (num_params) {
356 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
360 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
364 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
371 return mipi_dsi_device_transfer(dsi, &msg);
373 EXPORT_SYMBOL(mipi_dsi_generic_read);
376 * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
377 * @dsi: DSI peripheral device
378 * @data: buffer containing data to be transmitted
379 * @len: size of transmission buffer
381 * This function will automatically choose the right data type depending on
382 * the command payload length.
384 * Return: The number of bytes successfully transmitted or a negative error
387 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
388 const void *data, size_t len)
390 struct mipi_dsi_msg msg = {
391 .channel = dsi->channel,
401 msg.type = MIPI_DSI_DCS_SHORT_WRITE;
405 msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
409 msg.type = MIPI_DSI_DCS_LONG_WRITE;
413 return mipi_dsi_device_transfer(dsi, &msg);
415 EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
418 * mipi_dsi_dcs_write() - send DCS write command
419 * @dsi: DSI peripheral device
421 * @data: buffer containing the command payload
422 * @len: command payload length
424 * This function will automatically choose the right data type depending on
425 * the command payload length.
427 * Return: The number of bytes successfully transmitted or a negative error
430 ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
431 const void *data, size_t len)
440 tx = kmalloc(size, GFP_KERNEL);
444 /* concatenate the DCS command byte and the payload */
446 memcpy(&tx[1], data, len);
452 err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
459 EXPORT_SYMBOL(mipi_dsi_dcs_write);
462 * mipi_dsi_dcs_read() - send DCS read request command
463 * @dsi: DSI peripheral device
465 * @data: buffer in which to receive data
466 * @len: size of receive buffer
468 * Return: The number of bytes read or a negative error code on failure.
470 ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
473 struct mipi_dsi_msg msg = {
474 .channel = dsi->channel,
475 .type = MIPI_DSI_DCS_READ,
482 return mipi_dsi_device_transfer(dsi, &msg);
484 EXPORT_SYMBOL(mipi_dsi_dcs_read);
487 * mipi_dsi_dcs_nop() - send DCS nop packet
488 * @dsi: DSI peripheral device
490 * Return: 0 on success or a negative error code on failure.
492 int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
496 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
502 EXPORT_SYMBOL(mipi_dsi_dcs_nop);
505 * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
506 * @dsi: DSI peripheral device
508 * Return: 0 on success or a negative error code on failure.
510 int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
514 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
520 EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
523 * mipi_dsi_dcs_get_power_mode() - query the display module's current power
525 * @dsi: DSI peripheral device
526 * @mode: return location for the current power mode
528 * Return: 0 on success or a negative error code on failure.
530 int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
534 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
545 EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
548 * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
549 * data used by the interface
550 * @dsi: DSI peripheral device
551 * @format: return location for the pixel format
553 * Return: 0 on success or a negative error code on failure.
555 int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
559 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
570 EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
573 * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
574 * display module except interface communication
575 * @dsi: DSI peripheral device
577 * Return: 0 on success or a negative error code on failure.
579 int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
583 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
589 EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
592 * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
594 * @dsi: DSI peripheral device
596 * Return: 0 on success or a negative error code on failure.
598 int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
602 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
608 EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
611 * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
613 * @dsi: DSI peripheral device
615 * Return: 0 on success or a negative error code on failure.
617 int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
621 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
627 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
630 * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
632 * @dsi: DSI peripheral device
634 * Return: 0 on success or a negative error code on failure
636 int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
640 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
646 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
649 * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
650 * memory accessed by the host processor
651 * @dsi: DSI peripheral device
652 * @start: first column of frame memory
653 * @end: last column of frame memory
655 * Return: 0 on success or a negative error code on failure.
657 int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
660 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
663 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
670 EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
673 * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
674 * memory accessed by the host processor
675 * @dsi: DSI peripheral device
676 * @start: first page of frame memory
677 * @end: last page of frame memory
679 * Return: 0 on success or a negative error code on failure.
681 int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
684 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
687 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
694 EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
697 * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
698 * output signal on the TE signal line
699 * @dsi: DSI peripheral device
701 * Return: 0 on success or a negative error code on failure
703 int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
707 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
713 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
716 * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
717 * output signal on the TE signal line.
718 * @dsi: DSI peripheral device
719 * @mode: the Tearing Effect Output Line mode
721 * Return: 0 on success or a negative error code on failure
723 int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
724 enum mipi_dsi_dcs_tear_mode mode)
729 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
736 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
739 * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
740 * data used by the interface
741 * @dsi: DSI peripheral device
742 * @format: pixel format
744 * Return: 0 on success or a negative error code on failure.
746 int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
750 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
757 EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
760 * mipi_dsi_dcs_set_tear_scanline() - set the scanline to use as trigger for
761 * the Tearing Effect output signal of the display module
762 * @dsi: DSI peripheral device
763 * @scanline: scanline to use as trigger
765 * Return: 0 on success or a negative error code on failure
767 int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline)
769 u8 payload[3] = { MIPI_DCS_SET_TEAR_SCANLINE, scanline >> 8,
773 err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
779 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_scanline);
782 * mipi_dsi_dcs_set_display_brightness() - sets the brightness value of the
784 * @dsi: DSI peripheral device
785 * @brightness: brightness value
787 * Return: 0 on success or a negative error code on failure.
789 int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
792 u8 payload[2] = { brightness & 0xff, brightness >> 8 };
795 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
796 payload, sizeof(payload));
802 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_brightness);
805 * mipi_dsi_dcs_get_display_brightness() - gets the current brightness value
807 * @dsi: DSI peripheral device
808 * @brightness: brightness value
810 * Return: 0 on success or a negative error code on failure.
812 int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
817 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
818 brightness, sizeof(*brightness));
828 EXPORT_SYMBOL(mipi_dsi_dcs_get_display_brightness);