xref: /openbmc/linux/drivers/gpu/drm/drm_mipi_dsi.c (revision 8c749ce9)
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/drm_mipi_dsi.h>
29 
30 #include <linux/device.h>
31 #include <linux/module.h>
32 #include <linux/of_device.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/slab.h>
35 
36 #include <video/mipi_display.h>
37 
38 /**
39  * DOC: dsi helpers
40  *
41  * These functions contain some common logic and helpers to deal with MIPI DSI
42  * peripherals.
43  *
44  * Helpers are provided for a number of standard MIPI DSI command as well as a
45  * subset of the MIPI DCS command set.
46  */
47 
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 
204 /**
205  * mipi_dsi_attach - attach a DSI device to its DSI host
206  * @dsi: DSI peripheral
207  */
208 int mipi_dsi_attach(struct mipi_dsi_device *dsi)
209 {
210 	const struct mipi_dsi_host_ops *ops = dsi->host->ops;
211 
212 	if (!ops || !ops->attach)
213 		return -ENOSYS;
214 
215 	return ops->attach(dsi->host, dsi);
216 }
217 EXPORT_SYMBOL(mipi_dsi_attach);
218 
219 /**
220  * mipi_dsi_detach - detach a DSI device from its DSI host
221  * @dsi: DSI peripheral
222  */
223 int mipi_dsi_detach(struct mipi_dsi_device *dsi)
224 {
225 	const struct mipi_dsi_host_ops *ops = dsi->host->ops;
226 
227 	if (!ops || !ops->detach)
228 		return -ENOSYS;
229 
230 	return ops->detach(dsi->host, dsi);
231 }
232 EXPORT_SYMBOL(mipi_dsi_detach);
233 
234 static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
235 					struct mipi_dsi_msg *msg)
236 {
237 	const struct mipi_dsi_host_ops *ops = dsi->host->ops;
238 
239 	if (!ops || !ops->transfer)
240 		return -ENOSYS;
241 
242 	if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
243 		msg->flags |= MIPI_DSI_MSG_USE_LPM;
244 
245 	return ops->transfer(dsi->host, msg);
246 }
247 
248 /**
249  * mipi_dsi_packet_format_is_short - check if a packet is of the short format
250  * @type: MIPI DSI data type of the packet
251  *
252  * Return: true if the packet for the given data type is a short packet, false
253  * otherwise.
254  */
255 bool mipi_dsi_packet_format_is_short(u8 type)
256 {
257 	switch (type) {
258 	case MIPI_DSI_V_SYNC_START:
259 	case MIPI_DSI_V_SYNC_END:
260 	case MIPI_DSI_H_SYNC_START:
261 	case MIPI_DSI_H_SYNC_END:
262 	case MIPI_DSI_END_OF_TRANSMISSION:
263 	case MIPI_DSI_COLOR_MODE_OFF:
264 	case MIPI_DSI_COLOR_MODE_ON:
265 	case MIPI_DSI_SHUTDOWN_PERIPHERAL:
266 	case MIPI_DSI_TURN_ON_PERIPHERAL:
267 	case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
268 	case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
269 	case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
270 	case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
271 	case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
272 	case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
273 	case MIPI_DSI_DCS_SHORT_WRITE:
274 	case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
275 	case MIPI_DSI_DCS_READ:
276 	case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
277 		return true;
278 	}
279 
280 	return false;
281 }
282 EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
283 
284 /**
285  * mipi_dsi_packet_format_is_long - check if a packet is of the long format
286  * @type: MIPI DSI data type of the packet
287  *
288  * Return: true if the packet for the given data type is a long packet, false
289  * otherwise.
290  */
291 bool mipi_dsi_packet_format_is_long(u8 type)
292 {
293 	switch (type) {
294 	case MIPI_DSI_NULL_PACKET:
295 	case MIPI_DSI_BLANKING_PACKET:
296 	case MIPI_DSI_GENERIC_LONG_WRITE:
297 	case MIPI_DSI_DCS_LONG_WRITE:
298 	case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
299 	case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
300 	case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
301 	case MIPI_DSI_PACKED_PIXEL_STREAM_30:
302 	case MIPI_DSI_PACKED_PIXEL_STREAM_36:
303 	case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
304 	case MIPI_DSI_PACKED_PIXEL_STREAM_16:
305 	case MIPI_DSI_PACKED_PIXEL_STREAM_18:
306 	case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
307 	case MIPI_DSI_PACKED_PIXEL_STREAM_24:
308 		return true;
309 	}
310 
311 	return false;
312 }
313 EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
314 
315 /**
316  * mipi_dsi_create_packet - create a packet from a message according to the
317  *     DSI protocol
318  * @packet: pointer to a DSI packet structure
319  * @msg: message to translate into a packet
320  *
321  * Return: 0 on success or a negative error code on failure.
322  */
323 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
324 			   const struct mipi_dsi_msg *msg)
325 {
326 	if (!packet || !msg)
327 		return -EINVAL;
328 
329 	/* do some minimum sanity checking */
330 	if (!mipi_dsi_packet_format_is_short(msg->type) &&
331 	    !mipi_dsi_packet_format_is_long(msg->type))
332 		return -EINVAL;
333 
334 	if (msg->channel > 3)
335 		return -EINVAL;
336 
337 	memset(packet, 0, sizeof(*packet));
338 	packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
339 
340 	/* TODO: compute ECC if hardware support is not available */
341 
342 	/*
343 	 * Long write packets contain the word count in header bytes 1 and 2.
344 	 * The payload follows the header and is word count bytes long.
345 	 *
346 	 * Short write packets encode up to two parameters in header bytes 1
347 	 * and 2.
348 	 */
349 	if (mipi_dsi_packet_format_is_long(msg->type)) {
350 		packet->header[1] = (msg->tx_len >> 0) & 0xff;
351 		packet->header[2] = (msg->tx_len >> 8) & 0xff;
352 
353 		packet->payload_length = msg->tx_len;
354 		packet->payload = msg->tx_buf;
355 	} else {
356 		const u8 *tx = msg->tx_buf;
357 
358 		packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
359 		packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
360 	}
361 
362 	packet->size = sizeof(packet->header) + packet->payload_length;
363 
364 	return 0;
365 }
366 EXPORT_SYMBOL(mipi_dsi_create_packet);
367 
368 /**
369  * mipi_dsi_shutdown_peripheral() - sends a Shutdown Peripheral command
370  * @dsi: DSI peripheral device
371  *
372  * Return: 0 on success or a negative error code on failure.
373  */
374 int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
375 {
376 	struct mipi_dsi_msg msg = {
377 		.channel = dsi->channel,
378 		.type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
379 		.tx_buf = (u8 [2]) { 0, 0 },
380 		.tx_len = 2,
381 	};
382 
383 	return mipi_dsi_device_transfer(dsi, &msg);
384 }
385 EXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
386 
387 /**
388  * mipi_dsi_turn_on_peripheral() - sends a Turn On Peripheral command
389  * @dsi: DSI peripheral device
390  *
391  * Return: 0 on success or a negative error code on failure.
392  */
393 int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
394 {
395 	struct mipi_dsi_msg msg = {
396 		.channel = dsi->channel,
397 		.type = MIPI_DSI_TURN_ON_PERIPHERAL,
398 		.tx_buf = (u8 [2]) { 0, 0 },
399 		.tx_len = 2,
400 	};
401 
402 	return mipi_dsi_device_transfer(dsi, &msg);
403 }
404 EXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
405 
406 /*
407  * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the
408  *    the payload in a long packet transmitted from the peripheral back to the
409  *    host processor
410  * @dsi: DSI peripheral device
411  * @value: the maximum size of the payload
412  *
413  * Return: 0 on success or a negative error code on failure.
414  */
415 int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
416 					    u16 value)
417 {
418 	u8 tx[2] = { value & 0xff, value >> 8 };
419 	struct mipi_dsi_msg msg = {
420 		.channel = dsi->channel,
421 		.type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
422 		.tx_len = sizeof(tx),
423 		.tx_buf = tx,
424 	};
425 
426 	return mipi_dsi_device_transfer(dsi, &msg);
427 }
428 EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
429 
430 /**
431  * mipi_dsi_generic_write() - transmit data using a generic write packet
432  * @dsi: DSI peripheral device
433  * @payload: buffer containing the payload
434  * @size: size of payload buffer
435  *
436  * This function will automatically choose the right data type depending on
437  * the payload length.
438  *
439  * Return: The number of bytes transmitted on success or a negative error code
440  * on failure.
441  */
442 ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
443 			       size_t size)
444 {
445 	struct mipi_dsi_msg msg = {
446 		.channel = dsi->channel,
447 		.tx_buf = payload,
448 		.tx_len = size
449 	};
450 
451 	switch (size) {
452 	case 0:
453 		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
454 		break;
455 
456 	case 1:
457 		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
458 		break;
459 
460 	case 2:
461 		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
462 		break;
463 
464 	default:
465 		msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
466 		break;
467 	}
468 
469 	return mipi_dsi_device_transfer(dsi, &msg);
470 }
471 EXPORT_SYMBOL(mipi_dsi_generic_write);
472 
473 /**
474  * mipi_dsi_generic_read() - receive data using a generic read packet
475  * @dsi: DSI peripheral device
476  * @params: buffer containing the request parameters
477  * @num_params: number of request parameters
478  * @data: buffer in which to return the received data
479  * @size: size of receive buffer
480  *
481  * This function will automatically choose the right data type depending on
482  * the number of parameters passed in.
483  *
484  * Return: The number of bytes successfully read or a negative error code on
485  * failure.
486  */
487 ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
488 			      size_t num_params, void *data, size_t size)
489 {
490 	struct mipi_dsi_msg msg = {
491 		.channel = dsi->channel,
492 		.tx_len = num_params,
493 		.tx_buf = params,
494 		.rx_len = size,
495 		.rx_buf = data
496 	};
497 
498 	switch (num_params) {
499 	case 0:
500 		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
501 		break;
502 
503 	case 1:
504 		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
505 		break;
506 
507 	case 2:
508 		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
509 		break;
510 
511 	default:
512 		return -EINVAL;
513 	}
514 
515 	return mipi_dsi_device_transfer(dsi, &msg);
516 }
517 EXPORT_SYMBOL(mipi_dsi_generic_read);
518 
519 /**
520  * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
521  * @dsi: DSI peripheral device
522  * @data: buffer containing data to be transmitted
523  * @len: size of transmission buffer
524  *
525  * This function will automatically choose the right data type depending on
526  * the command payload length.
527  *
528  * Return: The number of bytes successfully transmitted or a negative error
529  * code on failure.
530  */
531 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
532 				  const void *data, size_t len)
533 {
534 	struct mipi_dsi_msg msg = {
535 		.channel = dsi->channel,
536 		.tx_buf = data,
537 		.tx_len = len
538 	};
539 
540 	switch (len) {
541 	case 0:
542 		return -EINVAL;
543 
544 	case 1:
545 		msg.type = MIPI_DSI_DCS_SHORT_WRITE;
546 		break;
547 
548 	case 2:
549 		msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
550 		break;
551 
552 	default:
553 		msg.type = MIPI_DSI_DCS_LONG_WRITE;
554 		break;
555 	}
556 
557 	return mipi_dsi_device_transfer(dsi, &msg);
558 }
559 EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
560 
561 /**
562  * mipi_dsi_dcs_write() - send DCS write command
563  * @dsi: DSI peripheral device
564  * @cmd: DCS command
565  * @data: buffer containing the command payload
566  * @len: command payload length
567  *
568  * This function will automatically choose the right data type depending on
569  * the command payload length.
570  *
571  * Return: The number of bytes successfully transmitted or a negative error
572  * code on failure.
573  */
574 ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
575 			   const void *data, size_t len)
576 {
577 	ssize_t err;
578 	size_t size;
579 	u8 *tx;
580 
581 	if (len > 0) {
582 		size = 1 + len;
583 
584 		tx = kmalloc(size, GFP_KERNEL);
585 		if (!tx)
586 			return -ENOMEM;
587 
588 		/* concatenate the DCS command byte and the payload */
589 		tx[0] = cmd;
590 		memcpy(&tx[1], data, len);
591 	} else {
592 		tx = &cmd;
593 		size = 1;
594 	}
595 
596 	err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
597 
598 	if (len > 0)
599 		kfree(tx);
600 
601 	return err;
602 }
603 EXPORT_SYMBOL(mipi_dsi_dcs_write);
604 
605 /**
606  * mipi_dsi_dcs_read() - send DCS read request command
607  * @dsi: DSI peripheral device
608  * @cmd: DCS command
609  * @data: buffer in which to receive data
610  * @len: size of receive buffer
611  *
612  * Return: The number of bytes read or a negative error code on failure.
613  */
614 ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
615 			  size_t len)
616 {
617 	struct mipi_dsi_msg msg = {
618 		.channel = dsi->channel,
619 		.type = MIPI_DSI_DCS_READ,
620 		.tx_buf = &cmd,
621 		.tx_len = 1,
622 		.rx_buf = data,
623 		.rx_len = len
624 	};
625 
626 	return mipi_dsi_device_transfer(dsi, &msg);
627 }
628 EXPORT_SYMBOL(mipi_dsi_dcs_read);
629 
630 /**
631  * mipi_dsi_dcs_nop() - send DCS nop packet
632  * @dsi: DSI peripheral device
633  *
634  * Return: 0 on success or a negative error code on failure.
635  */
636 int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
637 {
638 	ssize_t err;
639 
640 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
641 	if (err < 0)
642 		return err;
643 
644 	return 0;
645 }
646 EXPORT_SYMBOL(mipi_dsi_dcs_nop);
647 
648 /**
649  * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
650  * @dsi: DSI peripheral device
651  *
652  * Return: 0 on success or a negative error code on failure.
653  */
654 int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
655 {
656 	ssize_t err;
657 
658 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
659 	if (err < 0)
660 		return err;
661 
662 	return 0;
663 }
664 EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
665 
666 /**
667  * mipi_dsi_dcs_get_power_mode() - query the display module's current power
668  *    mode
669  * @dsi: DSI peripheral device
670  * @mode: return location for the current power mode
671  *
672  * Return: 0 on success or a negative error code on failure.
673  */
674 int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
675 {
676 	ssize_t err;
677 
678 	err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
679 				sizeof(*mode));
680 	if (err <= 0) {
681 		if (err == 0)
682 			err = -ENODATA;
683 
684 		return err;
685 	}
686 
687 	return 0;
688 }
689 EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
690 
691 /**
692  * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
693  *    data used by the interface
694  * @dsi: DSI peripheral device
695  * @format: return location for the pixel format
696  *
697  * Return: 0 on success or a negative error code on failure.
698  */
699 int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
700 {
701 	ssize_t err;
702 
703 	err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
704 				sizeof(*format));
705 	if (err <= 0) {
706 		if (err == 0)
707 			err = -ENODATA;
708 
709 		return err;
710 	}
711 
712 	return 0;
713 }
714 EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
715 
716 /**
717  * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
718  *    display module except interface communication
719  * @dsi: DSI peripheral device
720  *
721  * Return: 0 on success or a negative error code on failure.
722  */
723 int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
724 {
725 	ssize_t err;
726 
727 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
728 	if (err < 0)
729 		return err;
730 
731 	return 0;
732 }
733 EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
734 
735 /**
736  * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
737  *    module
738  * @dsi: DSI peripheral device
739  *
740  * Return: 0 on success or a negative error code on failure.
741  */
742 int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
743 {
744 	ssize_t err;
745 
746 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
747 	if (err < 0)
748 		return err;
749 
750 	return 0;
751 }
752 EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
753 
754 /**
755  * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
756  *    display device
757  * @dsi: DSI peripheral device
758  *
759  * Return: 0 on success or a negative error code on failure.
760  */
761 int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
762 {
763 	ssize_t err;
764 
765 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
766 	if (err < 0)
767 		return err;
768 
769 	return 0;
770 }
771 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
772 
773 /**
774  * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
775  *    display device
776  * @dsi: DSI peripheral device
777  *
778  * Return: 0 on success or a negative error code on failure
779  */
780 int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
781 {
782 	ssize_t err;
783 
784 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
785 	if (err < 0)
786 		return err;
787 
788 	return 0;
789 }
790 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
791 
792 /**
793  * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
794  *    memory accessed by the host processor
795  * @dsi: DSI peripheral device
796  * @start: first column of frame memory
797  * @end: last column of frame memory
798  *
799  * Return: 0 on success or a negative error code on failure.
800  */
801 int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
802 				    u16 end)
803 {
804 	u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
805 	ssize_t err;
806 
807 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
808 				 sizeof(payload));
809 	if (err < 0)
810 		return err;
811 
812 	return 0;
813 }
814 EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
815 
816 /**
817  * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
818  *    memory accessed by the host processor
819  * @dsi: DSI peripheral device
820  * @start: first page of frame memory
821  * @end: last page of frame memory
822  *
823  * Return: 0 on success or a negative error code on failure.
824  */
825 int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
826 				  u16 end)
827 {
828 	u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
829 	ssize_t err;
830 
831 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
832 				 sizeof(payload));
833 	if (err < 0)
834 		return err;
835 
836 	return 0;
837 }
838 EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
839 
840 /**
841  * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
842  *    output signal on the TE signal line
843  * @dsi: DSI peripheral device
844  *
845  * Return: 0 on success or a negative error code on failure
846  */
847 int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
848 {
849 	ssize_t err;
850 
851 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
852 	if (err < 0)
853 		return err;
854 
855 	return 0;
856 }
857 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
858 
859 /**
860  * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
861  *    output signal on the TE signal line.
862  * @dsi: DSI peripheral device
863  * @mode: the Tearing Effect Output Line mode
864  *
865  * Return: 0 on success or a negative error code on failure
866  */
867 int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
868 			     enum mipi_dsi_dcs_tear_mode mode)
869 {
870 	u8 value = mode;
871 	ssize_t err;
872 
873 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
874 				 sizeof(value));
875 	if (err < 0)
876 		return err;
877 
878 	return 0;
879 }
880 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
881 
882 /**
883  * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
884  *    data used by the interface
885  * @dsi: DSI peripheral device
886  * @format: pixel format
887  *
888  * Return: 0 on success or a negative error code on failure.
889  */
890 int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
891 {
892 	ssize_t err;
893 
894 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
895 				 sizeof(format));
896 	if (err < 0)
897 		return err;
898 
899 	return 0;
900 }
901 EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
902 
903 static int mipi_dsi_drv_probe(struct device *dev)
904 {
905 	struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
906 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
907 
908 	return drv->probe(dsi);
909 }
910 
911 static int mipi_dsi_drv_remove(struct device *dev)
912 {
913 	struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
914 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
915 
916 	return drv->remove(dsi);
917 }
918 
919 static void mipi_dsi_drv_shutdown(struct device *dev)
920 {
921 	struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
922 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
923 
924 	drv->shutdown(dsi);
925 }
926 
927 /**
928  * mipi_dsi_driver_register_full() - register a driver for DSI devices
929  * @drv: DSI driver structure
930  * @owner: owner module
931  *
932  * Return: 0 on success or a negative error code on failure.
933  */
934 int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
935 				  struct module *owner)
936 {
937 	drv->driver.bus = &mipi_dsi_bus_type;
938 	drv->driver.owner = owner;
939 
940 	if (drv->probe)
941 		drv->driver.probe = mipi_dsi_drv_probe;
942 	if (drv->remove)
943 		drv->driver.remove = mipi_dsi_drv_remove;
944 	if (drv->shutdown)
945 		drv->driver.shutdown = mipi_dsi_drv_shutdown;
946 
947 	return driver_register(&drv->driver);
948 }
949 EXPORT_SYMBOL(mipi_dsi_driver_register_full);
950 
951 /**
952  * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
953  * @drv: DSI driver structure
954  *
955  * Return: 0 on success or a negative error code on failure.
956  */
957 void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
958 {
959 	driver_unregister(&drv->driver);
960 }
961 EXPORT_SYMBOL(mipi_dsi_driver_unregister);
962 
963 static int __init mipi_dsi_bus_init(void)
964 {
965 	return bus_register(&mipi_dsi_bus_type);
966 }
967 postcore_initcall(mipi_dsi_bus_init);
968 
969 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
970 MODULE_DESCRIPTION("MIPI DSI Bus");
971 MODULE_LICENSE("GPL and additional rights");
972