xref: /dragonfly/sys/dev/drm/drm_mipi_dsi.c (revision e98bdfd3)
1 /*
2  * MIPI DSI Bus
3  *
4  * Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
5  * Andrzej Hajda <a.hajda@samsung.com>
6  *
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:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
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.
26  */
27 
28 #include <drm/drmP.h>
29 #include <drm/drm_mipi_dsi.h>
30 
31 #include <linux/device.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 
35 #include <video/mipi_display.h>
36 
37 /**
38  * DOC: dsi helpers
39  *
40  * These functions contain some common logic and helpers to deal with MIPI DSI
41  * peripherals.
42  *
43  * Helpers are provided for a number of standard MIPI DSI command as well as a
44  * subset of the MIPI DCS command set.
45  */
46 
47 #if 0
48 static int mipi_dsi_device_match(struct device *dev, struct device_driver *drv)
49 {
50 	return of_driver_match_device(dev, drv);
51 }
52 
53 static const struct dev_pm_ops mipi_dsi_device_pm_ops = {
54 	.runtime_suspend = pm_generic_runtime_suspend,
55 	.runtime_resume = pm_generic_runtime_resume,
56 	.suspend = pm_generic_suspend,
57 	.resume = pm_generic_resume,
58 	.freeze = pm_generic_freeze,
59 	.thaw = pm_generic_thaw,
60 	.poweroff = pm_generic_poweroff,
61 	.restore = pm_generic_restore,
62 };
63 
64 static struct bus_type mipi_dsi_bus_type = {
65 	.name = "mipi-dsi",
66 	.match = mipi_dsi_device_match,
67 	.pm = &mipi_dsi_device_pm_ops,
68 };
69 
70 static int of_device_match(struct device *dev, void *data)
71 {
72 	return dev->of_node == data;
73 }
74 
75 /**
76  * of_find_mipi_dsi_device_by_node() - find the MIPI DSI device matching a
77  *    device tree node
78  * @np: device tree node
79  *
80  * Return: A pointer to the MIPI DSI device corresponding to @np or NULL if no
81  *    such device exists (or has not been registered yet).
82  */
83 struct mipi_dsi_device *of_find_mipi_dsi_device_by_node(struct device_node *np)
84 {
85 	struct device *dev;
86 
87 	dev = bus_find_device(&mipi_dsi_bus_type, NULL, np, of_device_match);
88 
89 	return dev ? to_mipi_dsi_device(dev) : NULL;
90 }
91 EXPORT_SYMBOL(of_find_mipi_dsi_device_by_node);
92 
93 static void mipi_dsi_dev_release(struct device *dev)
94 {
95 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
96 
97 	of_node_put(dev->of_node);
98 	kfree(dsi);
99 }
100 
101 static const struct device_type mipi_dsi_device_type = {
102 	.release = mipi_dsi_dev_release,
103 };
104 
105 static struct mipi_dsi_device *mipi_dsi_device_alloc(struct mipi_dsi_host *host)
106 {
107 	struct mipi_dsi_device *dsi;
108 
109 	dsi = kzalloc(sizeof(*dsi), GFP_KERNEL);
110 	if (!dsi)
111 		return ERR_PTR(-ENOMEM);
112 
113 	dsi->host = host;
114 	dsi->dev.bus = &mipi_dsi_bus_type;
115 	dsi->dev.parent = host->dev;
116 	dsi->dev.type = &mipi_dsi_device_type;
117 
118 	device_initialize(&dsi->dev);
119 
120 	return dsi;
121 }
122 
123 static int mipi_dsi_device_add(struct mipi_dsi_device *dsi)
124 {
125 	struct mipi_dsi_host *host = dsi->host;
126 
127 	dev_set_name(&dsi->dev, "%s.%d", dev_name(host->dev),  dsi->channel);
128 
129 	return device_add(&dsi->dev);
130 }
131 
132 static struct mipi_dsi_device *
133 of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
134 {
135 	struct mipi_dsi_device *dsi;
136 	struct device *dev = host->dev;
137 	int ret;
138 	u32 reg;
139 
140 	ret = of_property_read_u32(node, "reg", &reg);
141 	if (ret) {
142 		dev_err(dev, "device node %s has no valid reg property: %d\n",
143 			node->full_name, ret);
144 		return ERR_PTR(-EINVAL);
145 	}
146 
147 	if (reg > 3) {
148 		dev_err(dev, "device node %s has invalid reg property: %u\n",
149 			node->full_name, reg);
150 		return ERR_PTR(-EINVAL);
151 	}
152 
153 	dsi = mipi_dsi_device_alloc(host);
154 	if (IS_ERR(dsi)) {
155 		dev_err(dev, "failed to allocate DSI device %s: %ld\n",
156 			node->full_name, PTR_ERR(dsi));
157 		return dsi;
158 	}
159 
160 	dsi->dev.of_node = of_node_get(node);
161 	dsi->channel = reg;
162 
163 	ret = mipi_dsi_device_add(dsi);
164 	if (ret) {
165 		dev_err(dev, "failed to add DSI device %s: %d\n",
166 			node->full_name, ret);
167 		kfree(dsi);
168 		return ERR_PTR(ret);
169 	}
170 
171 	return dsi;
172 }
173 
174 int mipi_dsi_host_register(struct mipi_dsi_host *host)
175 {
176 	struct device_node *node;
177 
178 	for_each_available_child_of_node(host->dev->of_node, node) {
179 		/* skip nodes without reg property */
180 		if (!of_find_property(node, "reg", NULL))
181 			continue;
182 		of_mipi_dsi_device_add(host, node);
183 	}
184 
185 	return 0;
186 }
187 EXPORT_SYMBOL(mipi_dsi_host_register);
188 
189 static int mipi_dsi_remove_device_fn(struct device *dev, void *priv)
190 {
191 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
192 
193 	device_unregister(&dsi->dev);
194 
195 	return 0;
196 }
197 
198 void mipi_dsi_host_unregister(struct mipi_dsi_host *host)
199 {
200 	device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn);
201 }
202 EXPORT_SYMBOL(mipi_dsi_host_unregister);
203 #endif
204 
205 /**
206  * mipi_dsi_attach - attach a DSI device to its DSI host
207  * @dsi: DSI peripheral
208  */
209 int mipi_dsi_attach(struct mipi_dsi_device *dsi)
210 {
211 	const struct mipi_dsi_host_ops *ops = dsi->host->ops;
212 
213 	if (!ops || !ops->attach)
214 		return -ENOSYS;
215 
216 	return ops->attach(dsi->host, dsi);
217 }
218 EXPORT_SYMBOL(mipi_dsi_attach);
219 
220 /**
221  * mipi_dsi_detach - detach a DSI device from its DSI host
222  * @dsi: DSI peripheral
223  */
224 int mipi_dsi_detach(struct mipi_dsi_device *dsi)
225 {
226 	const struct mipi_dsi_host_ops *ops = dsi->host->ops;
227 
228 	if (!ops || !ops->detach)
229 		return -ENOSYS;
230 
231 	return ops->detach(dsi->host, dsi);
232 }
233 EXPORT_SYMBOL(mipi_dsi_detach);
234 
235 static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
236 					struct mipi_dsi_msg *msg)
237 {
238 	const struct mipi_dsi_host_ops *ops = dsi->host->ops;
239 
240 	if (!ops || !ops->transfer)
241 		return -ENOSYS;
242 
243 	if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
244 		msg->flags |= MIPI_DSI_MSG_USE_LPM;
245 
246 	return ops->transfer(dsi->host, msg);
247 }
248 
249 /**
250  * mipi_dsi_packet_format_is_short - check if a packet is of the short format
251  * @type: MIPI DSI data type of the packet
252  *
253  * Return: true if the packet for the given data type is a short packet, false
254  * otherwise.
255  */
256 bool mipi_dsi_packet_format_is_short(u8 type)
257 {
258 	switch (type) {
259 	case MIPI_DSI_V_SYNC_START:
260 	case MIPI_DSI_V_SYNC_END:
261 	case MIPI_DSI_H_SYNC_START:
262 	case MIPI_DSI_H_SYNC_END:
263 	case MIPI_DSI_END_OF_TRANSMISSION:
264 	case MIPI_DSI_COLOR_MODE_OFF:
265 	case MIPI_DSI_COLOR_MODE_ON:
266 	case MIPI_DSI_SHUTDOWN_PERIPHERAL:
267 	case MIPI_DSI_TURN_ON_PERIPHERAL:
268 	case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
269 	case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
270 	case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
271 	case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
272 	case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
273 	case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
274 	case MIPI_DSI_DCS_SHORT_WRITE:
275 	case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
276 	case MIPI_DSI_DCS_READ:
277 	case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
278 		return true;
279 	}
280 
281 	return false;
282 }
283 EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
284 
285 /**
286  * mipi_dsi_packet_format_is_long - check if a packet is of the long format
287  * @type: MIPI DSI data type of the packet
288  *
289  * Return: true if the packet for the given data type is a long packet, false
290  * otherwise.
291  */
292 bool mipi_dsi_packet_format_is_long(u8 type)
293 {
294 	switch (type) {
295 	case MIPI_DSI_NULL_PACKET:
296 	case MIPI_DSI_BLANKING_PACKET:
297 	case MIPI_DSI_GENERIC_LONG_WRITE:
298 	case MIPI_DSI_DCS_LONG_WRITE:
299 	case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
300 	case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
301 	case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
302 	case MIPI_DSI_PACKED_PIXEL_STREAM_30:
303 	case MIPI_DSI_PACKED_PIXEL_STREAM_36:
304 	case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
305 	case MIPI_DSI_PACKED_PIXEL_STREAM_16:
306 	case MIPI_DSI_PACKED_PIXEL_STREAM_18:
307 	case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
308 	case MIPI_DSI_PACKED_PIXEL_STREAM_24:
309 		return true;
310 	}
311 
312 	return false;
313 }
314 EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
315 
316 /**
317  * mipi_dsi_create_packet - create a packet from a message according to the
318  *     DSI protocol
319  * @packet: pointer to a DSI packet structure
320  * @msg: message to translate into a packet
321  *
322  * Return: 0 on success or a negative error code on failure.
323  */
324 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
325 			   const struct mipi_dsi_msg *msg)
326 {
327 	if (!packet || !msg)
328 		return -EINVAL;
329 
330 	/* do some minimum sanity checking */
331 	if (!mipi_dsi_packet_format_is_short(msg->type) &&
332 	    !mipi_dsi_packet_format_is_long(msg->type))
333 		return -EINVAL;
334 
335 	if (msg->channel > 3)
336 		return -EINVAL;
337 
338 	memset(packet, 0, sizeof(*packet));
339 	packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
340 
341 	/* TODO: compute ECC if hardware support is not available */
342 
343 	/*
344 	 * Long write packets contain the word count in header bytes 1 and 2.
345 	 * The payload follows the header and is word count bytes long.
346 	 *
347 	 * Short write packets encode up to two parameters in header bytes 1
348 	 * and 2.
349 	 */
350 	if (mipi_dsi_packet_format_is_long(msg->type)) {
351 		packet->header[1] = (msg->tx_len >> 0) & 0xff;
352 		packet->header[2] = (msg->tx_len >> 8) & 0xff;
353 
354 		packet->payload_length = msg->tx_len;
355 		packet->payload = msg->tx_buf;
356 	} else {
357 		const u8 *tx = msg->tx_buf;
358 
359 		packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
360 		packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
361 	}
362 
363 	packet->size = sizeof(packet->header) + packet->payload_length;
364 
365 	return 0;
366 }
367 EXPORT_SYMBOL(mipi_dsi_create_packet);
368 
369 #if 0
370 /*
371  * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the
372  *    the payload in a long packet transmitted from the peripheral back to the
373  *    host processor
374  * @dsi: DSI peripheral device
375  * @value: the maximum size of the payload
376  *
377  * Return: 0 on success or a negative error code on failure.
378  */
379 int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
380 					    u16 value)
381 {
382 	u8 tx[2] = { value & 0xff, value >> 8 };
383 	struct mipi_dsi_msg msg = {
384 		.channel = dsi->channel,
385 		.type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
386 		.tx_len = sizeof(tx),
387 		.tx_buf = tx,
388 	};
389 
390 	return mipi_dsi_device_transfer(dsi, &msg);
391 }
392 EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
393 #endif
394 
395 /**
396  * mipi_dsi_generic_write() - transmit data using a generic write packet
397  * @dsi: DSI peripheral device
398  * @payload: buffer containing the payload
399  * @size: size of payload buffer
400  *
401  * This function will automatically choose the right data type depending on
402  * the payload length.
403  *
404  * Return: The number of bytes transmitted on success or a negative error code
405  * on failure.
406  */
407 ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
408 			       size_t size)
409 {
410 	struct mipi_dsi_msg msg = {
411 		.channel = dsi->channel,
412 		.tx_buf = payload,
413 		.tx_len = size
414 	};
415 
416 	switch (size) {
417 	case 0:
418 		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
419 		break;
420 
421 	case 1:
422 		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
423 		break;
424 
425 	case 2:
426 		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
427 		break;
428 
429 	default:
430 		msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
431 		break;
432 	}
433 
434 	return mipi_dsi_device_transfer(dsi, &msg);
435 }
436 EXPORT_SYMBOL(mipi_dsi_generic_write);
437 
438 /**
439  * mipi_dsi_generic_read() - receive data using a generic read packet
440  * @dsi: DSI peripheral device
441  * @params: buffer containing the request parameters
442  * @num_params: number of request parameters
443  * @data: buffer in which to return the received data
444  * @size: size of receive buffer
445  *
446  * This function will automatically choose the right data type depending on
447  * the number of parameters passed in.
448  *
449  * Return: The number of bytes successfully read or a negative error code on
450  * failure.
451  */
452 ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
453 			      size_t num_params, void *data, size_t size)
454 {
455 	struct mipi_dsi_msg msg = {
456 		.channel = dsi->channel,
457 		.tx_len = num_params,
458 		.tx_buf = params,
459 		.rx_len = size,
460 		.rx_buf = data
461 	};
462 
463 	switch (num_params) {
464 	case 0:
465 		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
466 		break;
467 
468 	case 1:
469 		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
470 		break;
471 
472 	case 2:
473 		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
474 		break;
475 
476 	default:
477 		return -EINVAL;
478 	}
479 
480 	return mipi_dsi_device_transfer(dsi, &msg);
481 }
482 EXPORT_SYMBOL(mipi_dsi_generic_read);
483 
484 /**
485  * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
486  * @dsi: DSI peripheral device
487  * @data: buffer containing data to be transmitted
488  * @len: size of transmission buffer
489  *
490  * This function will automatically choose the right data type depending on
491  * the command payload length.
492  *
493  * Return: The number of bytes successfully transmitted or a negative error
494  * code on failure.
495  */
496 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
497 				  const void *data, size_t len)
498 {
499 	struct mipi_dsi_msg msg = {
500 		.channel = dsi->channel,
501 		.tx_buf = data,
502 		.tx_len = len
503 	};
504 
505 	switch (len) {
506 	case 0:
507 		return -EINVAL;
508 
509 	case 1:
510 		msg.type = MIPI_DSI_DCS_SHORT_WRITE;
511 		break;
512 
513 	case 2:
514 		msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
515 		break;
516 
517 	default:
518 		msg.type = MIPI_DSI_DCS_LONG_WRITE;
519 		break;
520 	}
521 
522 	return mipi_dsi_device_transfer(dsi, &msg);
523 }
524 EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
525 
526 #if 0
527 /**
528  * mipi_dsi_dcs_write() - send DCS write command
529  * @dsi: DSI peripheral device
530  * @cmd: DCS command
531  * @data: buffer containing the command payload
532  * @len: command payload length
533  *
534  * This function will automatically choose the right data type depending on
535  * the command payload length.
536  *
537  * Return: The number of bytes successfully transmitted or a negative error
538  * code on failure.
539  */
540 ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
541 			   const void *data, size_t len)
542 {
543 	ssize_t err;
544 	size_t size;
545 	u8 *tx;
546 
547 	if (len > 0) {
548 		size = 1 + len;
549 
550 		tx = kmalloc(size, GFP_KERNEL);
551 		if (!tx)
552 			return -ENOMEM;
553 
554 		/* concatenate the DCS command byte and the payload */
555 		tx[0] = cmd;
556 		memcpy(&tx[1], data, len);
557 	} else {
558 		tx = &cmd;
559 		size = 1;
560 	}
561 
562 	err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
563 
564 	if (len > 0)
565 		kfree(tx);
566 
567 	return err;
568 }
569 EXPORT_SYMBOL(mipi_dsi_dcs_write);
570 #endif
571 
572 /**
573  * mipi_dsi_dcs_read() - send DCS read request command
574  * @dsi: DSI peripheral device
575  * @cmd: DCS command
576  * @data: buffer in which to receive data
577  * @len: size of receive buffer
578  *
579  * Return: The number of bytes read or a negative error code on failure.
580  */
581 ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
582 			  size_t len)
583 {
584 	struct mipi_dsi_msg msg = {
585 		.channel = dsi->channel,
586 		.type = MIPI_DSI_DCS_READ,
587 		.tx_buf = &cmd,
588 		.tx_len = 1,
589 		.rx_buf = data,
590 		.rx_len = len
591 	};
592 
593 	return mipi_dsi_device_transfer(dsi, &msg);
594 }
595 EXPORT_SYMBOL(mipi_dsi_dcs_read);
596 
597 #if 0
598 /**
599  * mipi_dsi_dcs_nop() - send DCS nop packet
600  * @dsi: DSI peripheral device
601  *
602  * Return: 0 on success or a negative error code on failure.
603  */
604 int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
605 {
606 	ssize_t err;
607 
608 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
609 	if (err < 0)
610 		return err;
611 
612 	return 0;
613 }
614 EXPORT_SYMBOL(mipi_dsi_dcs_nop);
615 
616 /**
617  * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
618  * @dsi: DSI peripheral device
619  *
620  * Return: 0 on success or a negative error code on failure.
621  */
622 int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
623 {
624 	ssize_t err;
625 
626 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
627 	if (err < 0)
628 		return err;
629 
630 	return 0;
631 }
632 EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
633 
634 /**
635  * mipi_dsi_dcs_get_power_mode() - query the display module's current power
636  *    mode
637  * @dsi: DSI peripheral device
638  * @mode: return location for the current power mode
639  *
640  * Return: 0 on success or a negative error code on failure.
641  */
642 int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
643 {
644 	ssize_t err;
645 
646 	err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
647 				sizeof(*mode));
648 	if (err <= 0) {
649 		if (err == 0)
650 			err = -ENODATA;
651 
652 		return err;
653 	}
654 
655 	return 0;
656 }
657 EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
658 
659 /**
660  * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
661  *    data used by the interface
662  * @dsi: DSI peripheral device
663  * @format: return location for the pixel format
664  *
665  * Return: 0 on success or a negative error code on failure.
666  */
667 int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
668 {
669 	ssize_t err;
670 
671 	err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
672 				sizeof(*format));
673 	if (err <= 0) {
674 		if (err == 0)
675 			err = -ENODATA;
676 
677 		return err;
678 	}
679 
680 	return 0;
681 }
682 EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
683 
684 /**
685  * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
686  *    display module except interface communication
687  * @dsi: DSI peripheral device
688  *
689  * Return: 0 on success or a negative error code on failure.
690  */
691 int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
692 {
693 	ssize_t err;
694 
695 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
696 	if (err < 0)
697 		return err;
698 
699 	return 0;
700 }
701 EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
702 
703 /**
704  * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
705  *    module
706  * @dsi: DSI peripheral device
707  *
708  * Return: 0 on success or a negative error code on failure.
709  */
710 int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
711 {
712 	ssize_t err;
713 
714 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
715 	if (err < 0)
716 		return err;
717 
718 	return 0;
719 }
720 EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
721 
722 /**
723  * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
724  *    display device
725  * @dsi: DSI peripheral device
726  *
727  * Return: 0 on success or a negative error code on failure.
728  */
729 int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
730 {
731 	ssize_t err;
732 
733 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
734 	if (err < 0)
735 		return err;
736 
737 	return 0;
738 }
739 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
740 
741 /**
742  * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
743  *    display device
744  * @dsi: DSI peripheral device
745  *
746  * Return: 0 on success or a negative error code on failure
747  */
748 int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
749 {
750 	ssize_t err;
751 
752 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
753 	if (err < 0)
754 		return err;
755 
756 	return 0;
757 }
758 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
759 
760 /**
761  * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
762  *    memory accessed by the host processor
763  * @dsi: DSI peripheral device
764  * @start: first column of frame memory
765  * @end: last column of frame memory
766  *
767  * Return: 0 on success or a negative error code on failure.
768  */
769 int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
770 				    u16 end)
771 {
772 	u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
773 	ssize_t err;
774 
775 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
776 				 sizeof(payload));
777 	if (err < 0)
778 		return err;
779 
780 	return 0;
781 }
782 EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
783 
784 /**
785  * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
786  *    memory accessed by the host processor
787  * @dsi: DSI peripheral device
788  * @start: first page of frame memory
789  * @end: last page of frame memory
790  *
791  * Return: 0 on success or a negative error code on failure.
792  */
793 int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
794 				  u16 end)
795 {
796 	u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
797 	ssize_t err;
798 
799 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
800 				 sizeof(payload));
801 	if (err < 0)
802 		return err;
803 
804 	return 0;
805 }
806 EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
807 
808 /**
809  * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
810  *    output signal on the TE signal line
811  * @dsi: DSI peripheral device
812  *
813  * Return: 0 on success or a negative error code on failure
814  */
815 int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
816 {
817 	ssize_t err;
818 
819 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
820 	if (err < 0)
821 		return err;
822 
823 	return 0;
824 }
825 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
826 
827 /**
828  * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
829  *    output signal on the TE signal line.
830  * @dsi: DSI peripheral device
831  * @mode: the Tearing Effect Output Line mode
832  *
833  * Return: 0 on success or a negative error code on failure
834  */
835 int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
836 			     enum mipi_dsi_dcs_tear_mode mode)
837 {
838 	u8 value = mode;
839 	ssize_t err;
840 
841 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
842 				 sizeof(value));
843 	if (err < 0)
844 		return err;
845 
846 	return 0;
847 }
848 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
849 
850 /**
851  * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
852  *    data used by the interface
853  * @dsi: DSI peripheral device
854  * @format: pixel format
855  *
856  * Return: 0 on success or a negative error code on failure.
857  */
858 int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
859 {
860 	ssize_t err;
861 
862 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
863 				 sizeof(format));
864 	if (err < 0)
865 		return err;
866 
867 	return 0;
868 }
869 EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
870 
871 static int mipi_dsi_drv_probe(struct device *dev)
872 {
873 	struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
874 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
875 
876 	return drv->probe(dsi);
877 }
878 
879 static int mipi_dsi_drv_remove(struct device *dev)
880 {
881 	struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
882 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
883 
884 	return drv->remove(dsi);
885 }
886 
887 static void mipi_dsi_drv_shutdown(struct device *dev)
888 {
889 	struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
890 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
891 
892 	drv->shutdown(dsi);
893 }
894 
895 /**
896  * mipi_dsi_driver_register_full() - register a driver for DSI devices
897  * @drv: DSI driver structure
898  * @owner: owner module
899  *
900  * Return: 0 on success or a negative error code on failure.
901  */
902 int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
903 				  struct module *owner)
904 {
905 	drv->driver.bus = &mipi_dsi_bus_type;
906 	drv->driver.owner = owner;
907 
908 	if (drv->probe)
909 		drv->driver.probe = mipi_dsi_drv_probe;
910 	if (drv->remove)
911 		drv->driver.remove = mipi_dsi_drv_remove;
912 	if (drv->shutdown)
913 		drv->driver.shutdown = mipi_dsi_drv_shutdown;
914 
915 	return driver_register(&drv->driver);
916 }
917 EXPORT_SYMBOL(mipi_dsi_driver_register_full);
918 
919 /**
920  * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
921  * @drv: DSI driver structure
922  *
923  * Return: 0 on success or a negative error code on failure.
924  */
925 void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
926 {
927 	driver_unregister(&drv->driver);
928 }
929 EXPORT_SYMBOL(mipi_dsi_driver_unregister);
930 
931 static int __init mipi_dsi_bus_init(void)
932 {
933 	return bus_register(&mipi_dsi_bus_type);
934 }
935 postcore_initcall(mipi_dsi_bus_init);
936 #endif
937 
938 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
939 MODULE_DESCRIPTION("MIPI DSI Bus");
940