4 * Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
5 * Andrzej Hajda <a.hajda@samsung.com>
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_detach(dsi);
350 mipi_dsi_device_unregister(dsi);
355 void mipi_dsi_host_unregister(struct mipi_dsi_host *host)
357 device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn);
359 mutex_lock(&host_lock);
360 list_del_init(&host->list);
361 mutex_unlock(&host_lock);
363 EXPORT_SYMBOL(mipi_dsi_host_unregister);
366 * mipi_dsi_attach - attach a DSI device to its DSI host
367 * @dsi: DSI peripheral
369 int mipi_dsi_attach(struct mipi_dsi_device *dsi)
371 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
373 if (!ops || !ops->attach)
376 return ops->attach(dsi->host, dsi);
378 EXPORT_SYMBOL(mipi_dsi_attach);
381 * mipi_dsi_detach - detach a DSI device from its DSI host
382 * @dsi: DSI peripheral
384 int mipi_dsi_detach(struct mipi_dsi_device *dsi)
386 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
388 if (!ops || !ops->detach)
391 return ops->detach(dsi->host, dsi);
393 EXPORT_SYMBOL(mipi_dsi_detach);
395 static void devm_mipi_dsi_detach(void *arg)
397 struct mipi_dsi_device *dsi = arg;
399 mipi_dsi_detach(dsi);
403 * devm_mipi_dsi_attach - Attach a MIPI-DSI device to its DSI Host
404 * @dev: device to tie the MIPI-DSI device attachment lifetime to
405 * @dsi: DSI peripheral
407 * This is the managed version of mipi_dsi_attach() which automatically
408 * calls mipi_dsi_detach() when @dev is unbound.
411 * 0 on success, a negative error code on failure.
413 int devm_mipi_dsi_attach(struct device *dev,
414 struct mipi_dsi_device *dsi)
418 ret = mipi_dsi_attach(dsi);
422 ret = devm_add_action_or_reset(dev, devm_mipi_dsi_detach, dsi);
428 EXPORT_SYMBOL_GPL(devm_mipi_dsi_attach);
430 static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
431 struct mipi_dsi_msg *msg)
433 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
435 if (!ops || !ops->transfer)
438 if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
439 msg->flags |= MIPI_DSI_MSG_USE_LPM;
441 return ops->transfer(dsi->host, msg);
445 * mipi_dsi_packet_format_is_short - check if a packet is of the short format
446 * @type: MIPI DSI data type of the packet
448 * Return: true if the packet for the given data type is a short packet, false
451 bool mipi_dsi_packet_format_is_short(u8 type)
454 case MIPI_DSI_V_SYNC_START:
455 case MIPI_DSI_V_SYNC_END:
456 case MIPI_DSI_H_SYNC_START:
457 case MIPI_DSI_H_SYNC_END:
458 case MIPI_DSI_COMPRESSION_MODE:
459 case MIPI_DSI_END_OF_TRANSMISSION:
460 case MIPI_DSI_COLOR_MODE_OFF:
461 case MIPI_DSI_COLOR_MODE_ON:
462 case MIPI_DSI_SHUTDOWN_PERIPHERAL:
463 case MIPI_DSI_TURN_ON_PERIPHERAL:
464 case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
465 case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
466 case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
467 case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
468 case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
469 case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
470 case MIPI_DSI_DCS_SHORT_WRITE:
471 case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
472 case MIPI_DSI_DCS_READ:
473 case MIPI_DSI_EXECUTE_QUEUE:
474 case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
480 EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
483 * mipi_dsi_packet_format_is_long - check if a packet is of the long format
484 * @type: MIPI DSI data type of the packet
486 * Return: true if the packet for the given data type is a long packet, false
489 bool mipi_dsi_packet_format_is_long(u8 type)
492 case MIPI_DSI_NULL_PACKET:
493 case MIPI_DSI_BLANKING_PACKET:
494 case MIPI_DSI_GENERIC_LONG_WRITE:
495 case MIPI_DSI_DCS_LONG_WRITE:
496 case MIPI_DSI_PICTURE_PARAMETER_SET:
497 case MIPI_DSI_COMPRESSED_PIXEL_STREAM:
498 case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
499 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
500 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
501 case MIPI_DSI_PACKED_PIXEL_STREAM_30:
502 case MIPI_DSI_PACKED_PIXEL_STREAM_36:
503 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
504 case MIPI_DSI_PACKED_PIXEL_STREAM_16:
505 case MIPI_DSI_PACKED_PIXEL_STREAM_18:
506 case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
507 case MIPI_DSI_PACKED_PIXEL_STREAM_24:
513 EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
516 * mipi_dsi_create_packet - create a packet from a message according to the
518 * @packet: pointer to a DSI packet structure
519 * @msg: message to translate into a packet
521 * Return: 0 on success or a negative error code on failure.
523 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
524 const struct mipi_dsi_msg *msg)
529 /* do some minimum sanity checking */
530 if (!mipi_dsi_packet_format_is_short(msg->type) &&
531 !mipi_dsi_packet_format_is_long(msg->type))
534 if (msg->channel > 3)
537 memset(packet, 0, sizeof(*packet));
538 packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
540 /* TODO: compute ECC if hardware support is not available */
543 * Long write packets contain the word count in header bytes 1 and 2.
544 * The payload follows the header and is word count bytes long.
546 * Short write packets encode up to two parameters in header bytes 1
549 if (mipi_dsi_packet_format_is_long(msg->type)) {
550 packet->header[1] = (msg->tx_len >> 0) & 0xff;
551 packet->header[2] = (msg->tx_len >> 8) & 0xff;
553 packet->payload_length = msg->tx_len;
554 packet->payload = msg->tx_buf;
556 const u8 *tx = msg->tx_buf;
558 packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
559 packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
562 packet->size = sizeof(packet->header) + packet->payload_length;
566 EXPORT_SYMBOL(mipi_dsi_create_packet);
569 * mipi_dsi_shutdown_peripheral() - sends a Shutdown Peripheral command
570 * @dsi: DSI peripheral device
572 * Return: 0 on success or a negative error code on failure.
574 int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
576 struct mipi_dsi_msg msg = {
577 .channel = dsi->channel,
578 .type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
579 .tx_buf = (u8 [2]) { 0, 0 },
582 int ret = mipi_dsi_device_transfer(dsi, &msg);
584 return (ret < 0) ? ret : 0;
586 EXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
589 * mipi_dsi_turn_on_peripheral() - sends a Turn On Peripheral command
590 * @dsi: DSI peripheral device
592 * Return: 0 on success or a negative error code on failure.
594 int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
596 struct mipi_dsi_msg msg = {
597 .channel = dsi->channel,
598 .type = MIPI_DSI_TURN_ON_PERIPHERAL,
599 .tx_buf = (u8 [2]) { 0, 0 },
602 int ret = mipi_dsi_device_transfer(dsi, &msg);
604 return (ret < 0) ? ret : 0;
606 EXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
609 * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of
610 * the payload in a long packet transmitted from the peripheral back to the
612 * @dsi: DSI peripheral device
613 * @value: the maximum size of the payload
615 * Return: 0 on success or a negative error code on failure.
617 int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
620 u8 tx[2] = { value & 0xff, value >> 8 };
621 struct mipi_dsi_msg msg = {
622 .channel = dsi->channel,
623 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
624 .tx_len = sizeof(tx),
627 int ret = mipi_dsi_device_transfer(dsi, &msg);
629 return (ret < 0) ? ret : 0;
631 EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
634 * mipi_dsi_compression_mode() - enable/disable DSC on the peripheral
635 * @dsi: DSI peripheral device
636 * @enable: Whether to enable or disable the DSC
638 * Enable or disable Display Stream Compression on the peripheral using the
639 * default Picture Parameter Set and VESA DSC 1.1 algorithm.
641 * Return: 0 on success or a negative error code on failure.
643 ssize_t mipi_dsi_compression_mode(struct mipi_dsi_device *dsi, bool enable)
645 /* Note: Needs updating for non-default PPS or algorithm */
646 u8 tx[2] = { enable << 0, 0 };
647 struct mipi_dsi_msg msg = {
648 .channel = dsi->channel,
649 .type = MIPI_DSI_COMPRESSION_MODE,
650 .tx_len = sizeof(tx),
653 int ret = mipi_dsi_device_transfer(dsi, &msg);
655 return (ret < 0) ? ret : 0;
657 EXPORT_SYMBOL(mipi_dsi_compression_mode);
660 * mipi_dsi_picture_parameter_set() - transmit the DSC PPS to the peripheral
661 * @dsi: DSI peripheral device
662 * @pps: VESA DSC 1.1 Picture Parameter Set
664 * Transmit the VESA DSC 1.1 Picture Parameter Set to the peripheral.
666 * Return: 0 on success or a negative error code on failure.
668 ssize_t mipi_dsi_picture_parameter_set(struct mipi_dsi_device *dsi,
669 const struct drm_dsc_picture_parameter_set *pps)
671 struct mipi_dsi_msg msg = {
672 .channel = dsi->channel,
673 .type = MIPI_DSI_PICTURE_PARAMETER_SET,
674 .tx_len = sizeof(*pps),
677 int ret = mipi_dsi_device_transfer(dsi, &msg);
679 return (ret < 0) ? ret : 0;
681 EXPORT_SYMBOL(mipi_dsi_picture_parameter_set);
684 * mipi_dsi_generic_write() - transmit data using a generic write packet
685 * @dsi: DSI peripheral device
686 * @payload: buffer containing the payload
687 * @size: size of payload buffer
689 * This function will automatically choose the right data type depending on
690 * the payload length.
692 * Return: The number of bytes transmitted on success or a negative error code
695 ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
698 struct mipi_dsi_msg msg = {
699 .channel = dsi->channel,
706 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
710 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
714 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
718 msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
722 return mipi_dsi_device_transfer(dsi, &msg);
724 EXPORT_SYMBOL(mipi_dsi_generic_write);
727 * mipi_dsi_generic_read() - receive data using a generic read packet
728 * @dsi: DSI peripheral device
729 * @params: buffer containing the request parameters
730 * @num_params: number of request parameters
731 * @data: buffer in which to return the received data
732 * @size: size of receive buffer
734 * This function will automatically choose the right data type depending on
735 * the number of parameters passed in.
737 * Return: The number of bytes successfully read or a negative error code on
740 ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
741 size_t num_params, void *data, size_t size)
743 struct mipi_dsi_msg msg = {
744 .channel = dsi->channel,
745 .tx_len = num_params,
751 switch (num_params) {
753 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
757 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
761 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
768 return mipi_dsi_device_transfer(dsi, &msg);
770 EXPORT_SYMBOL(mipi_dsi_generic_read);
773 * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
774 * @dsi: DSI peripheral device
775 * @data: buffer containing data to be transmitted
776 * @len: size of transmission buffer
778 * This function will automatically choose the right data type depending on
779 * the command payload length.
781 * Return: The number of bytes successfully transmitted or a negative error
784 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
785 const void *data, size_t len)
787 struct mipi_dsi_msg msg = {
788 .channel = dsi->channel,
798 msg.type = MIPI_DSI_DCS_SHORT_WRITE;
802 msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
806 msg.type = MIPI_DSI_DCS_LONG_WRITE;
810 return mipi_dsi_device_transfer(dsi, &msg);
812 EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
815 * mipi_dsi_dcs_write() - send DCS write command
816 * @dsi: DSI peripheral device
818 * @data: buffer containing the command payload
819 * @len: command payload length
821 * This function will automatically choose the right data type depending on
822 * the command payload length.
824 * Return: The number of bytes successfully transmitted or a negative error
827 ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
828 const void *data, size_t len)
836 if (len > ARRAY_SIZE(stack_tx) - 1) {
837 tx = kmalloc(size, GFP_KERNEL);
844 /* concatenate the DCS command byte and the payload */
847 memcpy(&tx[1], data, len);
849 err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
856 EXPORT_SYMBOL(mipi_dsi_dcs_write);
859 * mipi_dsi_dcs_read() - send DCS read request command
860 * @dsi: DSI peripheral device
862 * @data: buffer in which to receive data
863 * @len: size of receive buffer
865 * Return: The number of bytes read or a negative error code on failure.
867 ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
870 struct mipi_dsi_msg msg = {
871 .channel = dsi->channel,
872 .type = MIPI_DSI_DCS_READ,
879 return mipi_dsi_device_transfer(dsi, &msg);
881 EXPORT_SYMBOL(mipi_dsi_dcs_read);
884 * mipi_dsi_dcs_nop() - send DCS nop packet
885 * @dsi: DSI peripheral device
887 * Return: 0 on success or a negative error code on failure.
889 int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
893 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
899 EXPORT_SYMBOL(mipi_dsi_dcs_nop);
902 * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
903 * @dsi: DSI peripheral device
905 * Return: 0 on success or a negative error code on failure.
907 int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
911 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
917 EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
920 * mipi_dsi_dcs_get_power_mode() - query the display module's current power
922 * @dsi: DSI peripheral device
923 * @mode: return location for the current power mode
925 * Return: 0 on success or a negative error code on failure.
927 int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
931 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
942 EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
945 * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
946 * data used by the interface
947 * @dsi: DSI peripheral device
948 * @format: return location for the pixel format
950 * Return: 0 on success or a negative error code on failure.
952 int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
956 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
967 EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
970 * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
971 * display module except interface communication
972 * @dsi: DSI peripheral device
974 * Return: 0 on success or a negative error code on failure.
976 int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
980 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
986 EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
989 * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
991 * @dsi: DSI peripheral device
993 * Return: 0 on success or a negative error code on failure.
995 int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
999 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
1005 EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
1008 * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
1010 * @dsi: DSI peripheral device
1012 * Return: 0 on success or a negative error code on failure.
1014 int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
1018 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
1024 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
1027 * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
1029 * @dsi: DSI peripheral device
1031 * Return: 0 on success or a negative error code on failure
1033 int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
1037 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
1043 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
1046 * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
1047 * memory accessed by the host processor
1048 * @dsi: DSI peripheral device
1049 * @start: first column of frame memory
1050 * @end: last column of frame memory
1052 * Return: 0 on success or a negative error code on failure.
1054 int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
1057 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
1060 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
1067 EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
1070 * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
1071 * memory accessed by the host processor
1072 * @dsi: DSI peripheral device
1073 * @start: first page of frame memory
1074 * @end: last page of frame memory
1076 * Return: 0 on success or a negative error code on failure.
1078 int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
1081 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
1084 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
1091 EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
1094 * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
1095 * output signal on the TE signal line
1096 * @dsi: DSI peripheral device
1098 * Return: 0 on success or a negative error code on failure
1100 int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
1104 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
1110 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
1113 * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
1114 * output signal on the TE signal line.
1115 * @dsi: DSI peripheral device
1116 * @mode: the Tearing Effect Output Line mode
1118 * Return: 0 on success or a negative error code on failure
1120 int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
1121 enum mipi_dsi_dcs_tear_mode mode)
1126 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
1133 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
1136 * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
1137 * data used by the interface
1138 * @dsi: DSI peripheral device
1139 * @format: pixel format
1141 * Return: 0 on success or a negative error code on failure.
1143 int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
1147 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
1154 EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
1157 * mipi_dsi_dcs_set_tear_scanline() - set the scanline to use as trigger for
1158 * the Tearing Effect output signal of the display module
1159 * @dsi: DSI peripheral device
1160 * @scanline: scanline to use as trigger
1162 * Return: 0 on success or a negative error code on failure
1164 int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline)
1166 u8 payload[2] = { scanline >> 8, scanline & 0xff };
1169 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_SCANLINE, payload,
1176 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_scanline);
1179 * mipi_dsi_dcs_set_display_brightness() - sets the brightness value of the
1181 * @dsi: DSI peripheral device
1182 * @brightness: brightness value
1184 * Return: 0 on success or a negative error code on failure.
1186 int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
1189 u8 payload[2] = { brightness & 0xff, brightness >> 8 };
1192 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
1193 payload, sizeof(payload));
1199 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_brightness);
1202 * mipi_dsi_dcs_get_display_brightness() - gets the current brightness value
1204 * @dsi: DSI peripheral device
1205 * @brightness: brightness value
1207 * Return: 0 on success or a negative error code on failure.
1209 int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
1214 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
1215 brightness, sizeof(*brightness));
1225 EXPORT_SYMBOL(mipi_dsi_dcs_get_display_brightness);
1228 * mipi_dsi_dcs_set_display_brightness_large() - sets the 16-bit brightness value
1230 * @dsi: DSI peripheral device
1231 * @brightness: brightness value
1233 * Return: 0 on success or a negative error code on failure.
1235 int mipi_dsi_dcs_set_display_brightness_large(struct mipi_dsi_device *dsi,
1238 u8 payload[2] = { brightness >> 8, brightness & 0xff };
1241 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
1242 payload, sizeof(payload));
1248 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_brightness_large);
1251 * mipi_dsi_dcs_get_display_brightness_large() - gets the current 16-bit
1252 * brightness value of the display
1253 * @dsi: DSI peripheral device
1254 * @brightness: brightness value
1256 * Return: 0 on success or a negative error code on failure.
1258 int mipi_dsi_dcs_get_display_brightness_large(struct mipi_dsi_device *dsi,
1261 u8 brightness_be[2];
1264 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
1265 brightness_be, sizeof(brightness_be));
1273 *brightness = (brightness_be[0] << 8) | brightness_be[1];
1277 EXPORT_SYMBOL(mipi_dsi_dcs_get_display_brightness_large);
1279 static int mipi_dsi_drv_probe(struct device *dev)
1281 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
1282 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1284 return drv->probe(dsi);
1287 static int mipi_dsi_drv_remove(struct device *dev)
1289 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
1290 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1297 static void mipi_dsi_drv_shutdown(struct device *dev)
1299 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
1300 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1306 * mipi_dsi_driver_register_full() - register a driver for DSI devices
1307 * @drv: DSI driver structure
1308 * @owner: owner module
1310 * Return: 0 on success or a negative error code on failure.
1312 int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
1313 struct module *owner)
1315 drv->driver.bus = &mipi_dsi_bus_type;
1316 drv->driver.owner = owner;
1319 drv->driver.probe = mipi_dsi_drv_probe;
1321 drv->driver.remove = mipi_dsi_drv_remove;
1323 drv->driver.shutdown = mipi_dsi_drv_shutdown;
1325 return driver_register(&drv->driver);
1327 EXPORT_SYMBOL(mipi_dsi_driver_register_full);
1330 * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
1331 * @drv: DSI driver structure
1333 * Return: 0 on success or a negative error code on failure.
1335 void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
1337 driver_unregister(&drv->driver);
1339 EXPORT_SYMBOL(mipi_dsi_driver_unregister);
1341 static int __init mipi_dsi_bus_init(void)
1343 return bus_register(&mipi_dsi_bus_type);
1345 postcore_initcall(mipi_dsi_bus_init);
1347 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1348 MODULE_DESCRIPTION("MIPI DSI Bus");
1349 MODULE_LICENSE("GPL and additional rights");