4 * Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <linux/device.h>
29 #include <linux/module.h>
30 #include <linux/of_device.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/slab.h>
34 #include <drm/display/drm_dsc.h>
35 #include <drm/drm_mipi_dsi.h>
36 #include <drm/drm_print.h>
38 #include <video/mipi_display.h>
43 * These functions contain some common logic and helpers to deal with MIPI DSI
46 * Helpers are provided for a number of standard MIPI DSI command as well as a
47 * subset of the MIPI DCS command set.
50 static int mipi_dsi_device_match(struct device *dev, struct device_driver *drv)
52 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
54 /* attempt OF style match */
55 if (of_driver_match_device(dev, drv))
58 /* compare DSI device and driver names */
59 if (!strcmp(dsi->name, drv->name))
65 static int mipi_dsi_uevent(struct device *dev, struct kobj_uevent_env *env)
67 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
70 err = of_device_uevent_modalias(dev, env);
74 add_uevent_var(env, "MODALIAS=%s%s", MIPI_DSI_MODULE_PREFIX,
80 static const struct dev_pm_ops mipi_dsi_device_pm_ops = {
81 .runtime_suspend = pm_generic_runtime_suspend,
82 .runtime_resume = pm_generic_runtime_resume,
83 .suspend = pm_generic_suspend,
84 .resume = pm_generic_resume,
85 .freeze = pm_generic_freeze,
86 .thaw = pm_generic_thaw,
87 .poweroff = pm_generic_poweroff,
88 .restore = pm_generic_restore,
91 static struct bus_type mipi_dsi_bus_type = {
93 .match = mipi_dsi_device_match,
94 .uevent = mipi_dsi_uevent,
95 .pm = &mipi_dsi_device_pm_ops,
99 * of_find_mipi_dsi_device_by_node() - find the MIPI DSI device matching a
101 * @np: device tree node
103 * Return: A pointer to the MIPI DSI device corresponding to @np or NULL if no
104 * such device exists (or has not been registered yet).
106 struct mipi_dsi_device *of_find_mipi_dsi_device_by_node(struct device_node *np)
110 dev = bus_find_device_by_of_node(&mipi_dsi_bus_type, np);
112 return dev ? to_mipi_dsi_device(dev) : NULL;
114 EXPORT_SYMBOL(of_find_mipi_dsi_device_by_node);
116 static void mipi_dsi_dev_release(struct device *dev)
118 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
120 of_node_put(dev->of_node);
124 static const struct device_type mipi_dsi_device_type = {
125 .release = mipi_dsi_dev_release,
128 static struct mipi_dsi_device *mipi_dsi_device_alloc(struct mipi_dsi_host *host)
130 struct mipi_dsi_device *dsi;
132 dsi = kzalloc(sizeof(*dsi), GFP_KERNEL);
134 return ERR_PTR(-ENOMEM);
137 dsi->dev.bus = &mipi_dsi_bus_type;
138 dsi->dev.parent = host->dev;
139 dsi->dev.type = &mipi_dsi_device_type;
141 device_initialize(&dsi->dev);
146 static int mipi_dsi_device_add(struct mipi_dsi_device *dsi)
148 struct mipi_dsi_host *host = dsi->host;
150 dev_set_name(&dsi->dev, "%s.%d", dev_name(host->dev), dsi->channel);
152 return device_add(&dsi->dev);
155 #if IS_ENABLED(CONFIG_OF)
156 static struct mipi_dsi_device *
157 of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
159 struct mipi_dsi_device_info info = { };
163 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
164 drm_err(host, "modalias failure on %pOF\n", node);
165 return ERR_PTR(-EINVAL);
168 ret = of_property_read_u32(node, "reg", ®);
170 drm_err(host, "device node %pOF has no valid reg property: %d\n",
172 return ERR_PTR(-EINVAL);
176 info.node = of_node_get(node);
178 return mipi_dsi_device_register_full(host, &info);
181 static struct mipi_dsi_device *
182 of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
184 return ERR_PTR(-ENODEV);
189 * mipi_dsi_device_register_full - create a MIPI DSI device
190 * @host: DSI host to which this device is connected
191 * @info: pointer to template containing DSI device information
193 * Create a MIPI DSI device by using the device information provided by
194 * mipi_dsi_device_info template
197 * A pointer to the newly created MIPI DSI device, or, a pointer encoded
200 struct mipi_dsi_device *
201 mipi_dsi_device_register_full(struct mipi_dsi_host *host,
202 const struct mipi_dsi_device_info *info)
204 struct mipi_dsi_device *dsi;
208 drm_err(host, "invalid mipi_dsi_device_info pointer\n");
209 return ERR_PTR(-EINVAL);
212 if (info->channel > 3) {
213 drm_err(host, "invalid virtual channel: %u\n", info->channel);
214 return ERR_PTR(-EINVAL);
217 dsi = mipi_dsi_device_alloc(host);
219 drm_err(host, "failed to allocate DSI device %ld\n",
224 dsi->dev.of_node = info->node;
225 dsi->channel = info->channel;
226 strlcpy(dsi->name, info->type, sizeof(dsi->name));
228 ret = mipi_dsi_device_add(dsi);
230 drm_err(host, "failed to add DSI device %d\n", ret);
237 EXPORT_SYMBOL(mipi_dsi_device_register_full);
240 * mipi_dsi_device_unregister - unregister MIPI DSI device
241 * @dsi: DSI peripheral device
243 void mipi_dsi_device_unregister(struct mipi_dsi_device *dsi)
245 device_unregister(&dsi->dev);
247 EXPORT_SYMBOL(mipi_dsi_device_unregister);
249 static void devm_mipi_dsi_device_unregister(void *arg)
251 struct mipi_dsi_device *dsi = arg;
253 mipi_dsi_device_unregister(dsi);
257 * devm_mipi_dsi_device_register_full - create a managed MIPI DSI device
258 * @dev: device to tie the MIPI-DSI device lifetime to
259 * @host: DSI host to which this device is connected
260 * @info: pointer to template containing DSI device information
262 * Create a MIPI DSI device by using the device information provided by
263 * mipi_dsi_device_info template
265 * This is the managed version of mipi_dsi_device_register_full() which
266 * automatically calls mipi_dsi_device_unregister() when @dev is
270 * A pointer to the newly created MIPI DSI device, or, a pointer encoded
273 struct mipi_dsi_device *
274 devm_mipi_dsi_device_register_full(struct device *dev,
275 struct mipi_dsi_host *host,
276 const struct mipi_dsi_device_info *info)
278 struct mipi_dsi_device *dsi;
281 dsi = mipi_dsi_device_register_full(host, info);
285 ret = devm_add_action_or_reset(dev,
286 devm_mipi_dsi_device_unregister,
293 EXPORT_SYMBOL_GPL(devm_mipi_dsi_device_register_full);
295 static DEFINE_MUTEX(host_lock);
296 static LIST_HEAD(host_list);
299 * of_find_mipi_dsi_host_by_node() - find the MIPI DSI host matching a
301 * @node: device tree node
304 * A pointer to the MIPI DSI host corresponding to @node or NULL if no
305 * such device exists (or has not been registered yet).
307 struct mipi_dsi_host *of_find_mipi_dsi_host_by_node(struct device_node *node)
309 struct mipi_dsi_host *host;
311 mutex_lock(&host_lock);
313 list_for_each_entry(host, &host_list, list) {
314 if (host->dev->of_node == node) {
315 mutex_unlock(&host_lock);
320 mutex_unlock(&host_lock);
324 EXPORT_SYMBOL(of_find_mipi_dsi_host_by_node);
326 int mipi_dsi_host_register(struct mipi_dsi_host *host)
328 struct device_node *node;
330 for_each_available_child_of_node(host->dev->of_node, node) {
331 /* skip nodes without reg property */
332 if (!of_find_property(node, "reg", NULL))
334 of_mipi_dsi_device_add(host, node);
337 mutex_lock(&host_lock);
338 list_add_tail(&host->list, &host_list);
339 mutex_unlock(&host_lock);
343 EXPORT_SYMBOL(mipi_dsi_host_register);
345 static int mipi_dsi_remove_device_fn(struct device *dev, void *priv)
347 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
349 mipi_dsi_device_unregister(dsi);
354 void mipi_dsi_host_unregister(struct mipi_dsi_host *host)
356 device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn);
358 mutex_lock(&host_lock);
359 list_del_init(&host->list);
360 mutex_unlock(&host_lock);
362 EXPORT_SYMBOL(mipi_dsi_host_unregister);
365 * mipi_dsi_attach - attach a DSI device to its DSI host
366 * @dsi: DSI peripheral
368 int mipi_dsi_attach(struct mipi_dsi_device *dsi)
370 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
372 if (!ops || !ops->attach)
375 return ops->attach(dsi->host, dsi);
377 EXPORT_SYMBOL(mipi_dsi_attach);
380 * mipi_dsi_detach - detach a DSI device from its DSI host
381 * @dsi: DSI peripheral
383 int mipi_dsi_detach(struct mipi_dsi_device *dsi)
385 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
387 if (!ops || !ops->detach)
390 return ops->detach(dsi->host, dsi);
392 EXPORT_SYMBOL(mipi_dsi_detach);
394 static void devm_mipi_dsi_detach(void *arg)
396 struct mipi_dsi_device *dsi = arg;
398 mipi_dsi_detach(dsi);
402 * devm_mipi_dsi_attach - Attach a MIPI-DSI device to its DSI Host
403 * @dev: device to tie the MIPI-DSI device attachment lifetime to
404 * @dsi: DSI peripheral
406 * This is the managed version of mipi_dsi_attach() which automatically
407 * calls mipi_dsi_detach() when @dev is unbound.
410 * 0 on success, a negative error code on failure.
412 int devm_mipi_dsi_attach(struct device *dev,
413 struct mipi_dsi_device *dsi)
417 ret = mipi_dsi_attach(dsi);
421 ret = devm_add_action_or_reset(dev, devm_mipi_dsi_detach, dsi);
427 EXPORT_SYMBOL_GPL(devm_mipi_dsi_attach);
429 static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
430 struct mipi_dsi_msg *msg)
432 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
434 if (!ops || !ops->transfer)
437 if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
438 msg->flags |= MIPI_DSI_MSG_USE_LPM;
440 return ops->transfer(dsi->host, msg);
444 * mipi_dsi_packet_format_is_short - check if a packet is of the short format
445 * @type: MIPI DSI data type of the packet
447 * Return: true if the packet for the given data type is a short packet, false
450 bool mipi_dsi_packet_format_is_short(u8 type)
453 case MIPI_DSI_V_SYNC_START:
454 case MIPI_DSI_V_SYNC_END:
455 case MIPI_DSI_H_SYNC_START:
456 case MIPI_DSI_H_SYNC_END:
457 case MIPI_DSI_COMPRESSION_MODE:
458 case MIPI_DSI_END_OF_TRANSMISSION:
459 case MIPI_DSI_COLOR_MODE_OFF:
460 case MIPI_DSI_COLOR_MODE_ON:
461 case MIPI_DSI_SHUTDOWN_PERIPHERAL:
462 case MIPI_DSI_TURN_ON_PERIPHERAL:
463 case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
464 case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
465 case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
466 case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
467 case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
468 case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
469 case MIPI_DSI_DCS_SHORT_WRITE:
470 case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
471 case MIPI_DSI_DCS_READ:
472 case MIPI_DSI_EXECUTE_QUEUE:
473 case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
479 EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
482 * mipi_dsi_packet_format_is_long - check if a packet is of the long format
483 * @type: MIPI DSI data type of the packet
485 * Return: true if the packet for the given data type is a long packet, false
488 bool mipi_dsi_packet_format_is_long(u8 type)
491 case MIPI_DSI_NULL_PACKET:
492 case MIPI_DSI_BLANKING_PACKET:
493 case MIPI_DSI_GENERIC_LONG_WRITE:
494 case MIPI_DSI_DCS_LONG_WRITE:
495 case MIPI_DSI_PICTURE_PARAMETER_SET:
496 case MIPI_DSI_COMPRESSED_PIXEL_STREAM:
497 case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
498 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
499 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
500 case MIPI_DSI_PACKED_PIXEL_STREAM_30:
501 case MIPI_DSI_PACKED_PIXEL_STREAM_36:
502 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
503 case MIPI_DSI_PACKED_PIXEL_STREAM_16:
504 case MIPI_DSI_PACKED_PIXEL_STREAM_18:
505 case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
506 case MIPI_DSI_PACKED_PIXEL_STREAM_24:
512 EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
515 * mipi_dsi_create_packet - create a packet from a message according to the
517 * @packet: pointer to a DSI packet structure
518 * @msg: message to translate into a packet
520 * Return: 0 on success or a negative error code on failure.
522 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
523 const struct mipi_dsi_msg *msg)
528 /* do some minimum sanity checking */
529 if (!mipi_dsi_packet_format_is_short(msg->type) &&
530 !mipi_dsi_packet_format_is_long(msg->type))
533 if (msg->channel > 3)
536 memset(packet, 0, sizeof(*packet));
537 packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
539 /* TODO: compute ECC if hardware support is not available */
542 * Long write packets contain the word count in header bytes 1 and 2.
543 * The payload follows the header and is word count bytes long.
545 * Short write packets encode up to two parameters in header bytes 1
548 if (mipi_dsi_packet_format_is_long(msg->type)) {
549 packet->header[1] = (msg->tx_len >> 0) & 0xff;
550 packet->header[2] = (msg->tx_len >> 8) & 0xff;
552 packet->payload_length = msg->tx_len;
553 packet->payload = msg->tx_buf;
555 const u8 *tx = msg->tx_buf;
557 packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
558 packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
561 packet->size = sizeof(packet->header) + packet->payload_length;
565 EXPORT_SYMBOL(mipi_dsi_create_packet);
568 * mipi_dsi_shutdown_peripheral() - sends a Shutdown Peripheral command
569 * @dsi: DSI peripheral device
571 * Return: 0 on success or a negative error code on failure.
573 int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
575 struct mipi_dsi_msg msg = {
576 .channel = dsi->channel,
577 .type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
578 .tx_buf = (u8 [2]) { 0, 0 },
581 int ret = mipi_dsi_device_transfer(dsi, &msg);
583 return (ret < 0) ? ret : 0;
585 EXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
588 * mipi_dsi_turn_on_peripheral() - sends a Turn On Peripheral command
589 * @dsi: DSI peripheral device
591 * Return: 0 on success or a negative error code on failure.
593 int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
595 struct mipi_dsi_msg msg = {
596 .channel = dsi->channel,
597 .type = MIPI_DSI_TURN_ON_PERIPHERAL,
598 .tx_buf = (u8 [2]) { 0, 0 },
601 int ret = mipi_dsi_device_transfer(dsi, &msg);
603 return (ret < 0) ? ret : 0;
605 EXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
608 * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the
609 * the payload in a long packet transmitted from the peripheral back to the
611 * @dsi: DSI peripheral device
612 * @value: the maximum size of the payload
614 * Return: 0 on success or a negative error code on failure.
616 int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
619 u8 tx[2] = { value & 0xff, value >> 8 };
620 struct mipi_dsi_msg msg = {
621 .channel = dsi->channel,
622 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
623 .tx_len = sizeof(tx),
626 int ret = mipi_dsi_device_transfer(dsi, &msg);
628 return (ret < 0) ? ret : 0;
630 EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
633 * mipi_dsi_compression_mode() - enable/disable DSC on the peripheral
634 * @dsi: DSI peripheral device
635 * @enable: Whether to enable or disable the DSC
637 * Enable or disable Display Stream Compression on the peripheral using the
638 * default Picture Parameter Set and VESA DSC 1.1 algorithm.
640 * Return: 0 on success or a negative error code on failure.
642 ssize_t mipi_dsi_compression_mode(struct mipi_dsi_device *dsi, bool enable)
644 /* Note: Needs updating for non-default PPS or algorithm */
645 u8 tx[2] = { enable << 0, 0 };
646 struct mipi_dsi_msg msg = {
647 .channel = dsi->channel,
648 .type = MIPI_DSI_COMPRESSION_MODE,
649 .tx_len = sizeof(tx),
652 int ret = mipi_dsi_device_transfer(dsi, &msg);
654 return (ret < 0) ? ret : 0;
656 EXPORT_SYMBOL(mipi_dsi_compression_mode);
659 * mipi_dsi_picture_parameter_set() - transmit the DSC PPS to the peripheral
660 * @dsi: DSI peripheral device
661 * @pps: VESA DSC 1.1 Picture Parameter Set
663 * Transmit the VESA DSC 1.1 Picture Parameter Set to the peripheral.
665 * Return: 0 on success or a negative error code on failure.
667 ssize_t mipi_dsi_picture_parameter_set(struct mipi_dsi_device *dsi,
668 const struct drm_dsc_picture_parameter_set *pps)
670 struct mipi_dsi_msg msg = {
671 .channel = dsi->channel,
672 .type = MIPI_DSI_PICTURE_PARAMETER_SET,
673 .tx_len = sizeof(*pps),
676 int ret = mipi_dsi_device_transfer(dsi, &msg);
678 return (ret < 0) ? ret : 0;
680 EXPORT_SYMBOL(mipi_dsi_picture_parameter_set);
683 * mipi_dsi_generic_write() - transmit data using a generic write packet
684 * @dsi: DSI peripheral device
685 * @payload: buffer containing the payload
686 * @size: size of payload buffer
688 * This function will automatically choose the right data type depending on
689 * the payload length.
691 * Return: The number of bytes transmitted on success or a negative error code
694 ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
697 struct mipi_dsi_msg msg = {
698 .channel = dsi->channel,
705 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
709 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
713 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
717 msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
721 return mipi_dsi_device_transfer(dsi, &msg);
723 EXPORT_SYMBOL(mipi_dsi_generic_write);
726 * mipi_dsi_generic_read() - receive data using a generic read packet
727 * @dsi: DSI peripheral device
728 * @params: buffer containing the request parameters
729 * @num_params: number of request parameters
730 * @data: buffer in which to return the received data
731 * @size: size of receive buffer
733 * This function will automatically choose the right data type depending on
734 * the number of parameters passed in.
736 * Return: The number of bytes successfully read or a negative error code on
739 ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
740 size_t num_params, void *data, size_t size)
742 struct mipi_dsi_msg msg = {
743 .channel = dsi->channel,
744 .tx_len = num_params,
750 switch (num_params) {
752 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
756 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
760 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
767 return mipi_dsi_device_transfer(dsi, &msg);
769 EXPORT_SYMBOL(mipi_dsi_generic_read);
772 * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
773 * @dsi: DSI peripheral device
774 * @data: buffer containing data to be transmitted
775 * @len: size of transmission buffer
777 * This function will automatically choose the right data type depending on
778 * the command payload length.
780 * Return: The number of bytes successfully transmitted or a negative error
783 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
784 const void *data, size_t len)
786 struct mipi_dsi_msg msg = {
787 .channel = dsi->channel,
797 msg.type = MIPI_DSI_DCS_SHORT_WRITE;
801 msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
805 msg.type = MIPI_DSI_DCS_LONG_WRITE;
809 return mipi_dsi_device_transfer(dsi, &msg);
811 EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
814 * mipi_dsi_dcs_write() - send DCS write command
815 * @dsi: DSI peripheral device
817 * @data: buffer containing the command payload
818 * @len: command payload length
820 * This function will automatically choose the right data type depending on
821 * the command payload length.
823 * Return: The number of bytes successfully transmitted or a negative error
826 ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
827 const void *data, size_t len)
835 if (len > ARRAY_SIZE(stack_tx) - 1) {
836 tx = kmalloc(size, GFP_KERNEL);
843 /* concatenate the DCS command byte and the payload */
846 memcpy(&tx[1], data, len);
848 err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
855 EXPORT_SYMBOL(mipi_dsi_dcs_write);
858 * mipi_dsi_dcs_read() - send DCS read request command
859 * @dsi: DSI peripheral device
861 * @data: buffer in which to receive data
862 * @len: size of receive buffer
864 * Return: The number of bytes read or a negative error code on failure.
866 ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
869 struct mipi_dsi_msg msg = {
870 .channel = dsi->channel,
871 .type = MIPI_DSI_DCS_READ,
878 return mipi_dsi_device_transfer(dsi, &msg);
880 EXPORT_SYMBOL(mipi_dsi_dcs_read);
883 * mipi_dsi_dcs_nop() - send DCS nop packet
884 * @dsi: DSI peripheral device
886 * Return: 0 on success or a negative error code on failure.
888 int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
892 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
898 EXPORT_SYMBOL(mipi_dsi_dcs_nop);
901 * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
902 * @dsi: DSI peripheral device
904 * Return: 0 on success or a negative error code on failure.
906 int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
910 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
916 EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
919 * mipi_dsi_dcs_get_power_mode() - query the display module's current power
921 * @dsi: DSI peripheral device
922 * @mode: return location for the current power mode
924 * Return: 0 on success or a negative error code on failure.
926 int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
930 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
941 EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
944 * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
945 * data used by the interface
946 * @dsi: DSI peripheral device
947 * @format: return location for the pixel format
949 * Return: 0 on success or a negative error code on failure.
951 int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
955 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
966 EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
969 * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
970 * display module except interface communication
971 * @dsi: DSI peripheral device
973 * Return: 0 on success or a negative error code on failure.
975 int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
979 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
985 EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
988 * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
990 * @dsi: DSI peripheral device
992 * Return: 0 on success or a negative error code on failure.
994 int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
998 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
1004 EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
1007 * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
1009 * @dsi: DSI peripheral device
1011 * Return: 0 on success or a negative error code on failure.
1013 int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
1017 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
1023 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
1026 * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
1028 * @dsi: DSI peripheral device
1030 * Return: 0 on success or a negative error code on failure
1032 int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
1036 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
1042 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
1045 * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
1046 * memory accessed by the host processor
1047 * @dsi: DSI peripheral device
1048 * @start: first column of frame memory
1049 * @end: last column of frame memory
1051 * Return: 0 on success or a negative error code on failure.
1053 int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
1056 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
1059 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
1066 EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
1069 * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
1070 * memory accessed by the host processor
1071 * @dsi: DSI peripheral device
1072 * @start: first page of frame memory
1073 * @end: last page of frame memory
1075 * Return: 0 on success or a negative error code on failure.
1077 int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
1080 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
1083 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
1090 EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
1093 * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
1094 * output signal on the TE signal line
1095 * @dsi: DSI peripheral device
1097 * Return: 0 on success or a negative error code on failure
1099 int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
1103 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
1109 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
1112 * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
1113 * output signal on the TE signal line.
1114 * @dsi: DSI peripheral device
1115 * @mode: the Tearing Effect Output Line mode
1117 * Return: 0 on success or a negative error code on failure
1119 int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
1120 enum mipi_dsi_dcs_tear_mode mode)
1125 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
1132 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
1135 * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
1136 * data used by the interface
1137 * @dsi: DSI peripheral device
1138 * @format: pixel format
1140 * Return: 0 on success or a negative error code on failure.
1142 int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
1146 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
1153 EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
1156 * mipi_dsi_dcs_set_tear_scanline() - set the scanline to use as trigger for
1157 * the Tearing Effect output signal of the display module
1158 * @dsi: DSI peripheral device
1159 * @scanline: scanline to use as trigger
1161 * Return: 0 on success or a negative error code on failure
1163 int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline)
1165 u8 payload[2] = { scanline >> 8, scanline & 0xff };
1168 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_SCANLINE, payload,
1175 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_scanline);
1178 * mipi_dsi_dcs_set_display_brightness() - sets the brightness value of the
1180 * @dsi: DSI peripheral device
1181 * @brightness: brightness value
1183 * Return: 0 on success or a negative error code on failure.
1185 int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
1188 u8 payload[2] = { brightness & 0xff, brightness >> 8 };
1191 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
1192 payload, sizeof(payload));
1198 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_brightness);
1201 * mipi_dsi_dcs_get_display_brightness() - gets the current brightness value
1203 * @dsi: DSI peripheral device
1204 * @brightness: brightness value
1206 * Return: 0 on success or a negative error code on failure.
1208 int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
1213 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
1214 brightness, sizeof(*brightness));
1224 EXPORT_SYMBOL(mipi_dsi_dcs_get_display_brightness);
1226 static int mipi_dsi_drv_probe(struct device *dev)
1228 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
1229 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1231 return drv->probe(dsi);
1234 static int mipi_dsi_drv_remove(struct device *dev)
1236 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
1237 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1239 return drv->remove(dsi);
1242 static void mipi_dsi_drv_shutdown(struct device *dev)
1244 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
1245 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1251 * mipi_dsi_driver_register_full() - register a driver for DSI devices
1252 * @drv: DSI driver structure
1253 * @owner: owner module
1255 * Return: 0 on success or a negative error code on failure.
1257 int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
1258 struct module *owner)
1260 drv->driver.bus = &mipi_dsi_bus_type;
1261 drv->driver.owner = owner;
1264 drv->driver.probe = mipi_dsi_drv_probe;
1266 drv->driver.remove = mipi_dsi_drv_remove;
1268 drv->driver.shutdown = mipi_dsi_drv_shutdown;
1270 return driver_register(&drv->driver);
1272 EXPORT_SYMBOL(mipi_dsi_driver_register_full);
1275 * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
1276 * @drv: DSI driver structure
1278 * Return: 0 on success or a negative error code on failure.
1280 void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
1282 driver_unregister(&drv->driver);
1284 EXPORT_SYMBOL(mipi_dsi_driver_unregister);
1286 static int __init mipi_dsi_bus_init(void)
1288 return bus_register(&mipi_dsi_bus_type);
1290 postcore_initcall(mipi_dsi_bus_init);
1293 MODULE_DESCRIPTION("MIPI DSI Bus");
1294 MODULE_LICENSE("GPL and additional rights");