xref: /openbmc/linux/drivers/gpu/drm/drm_mipi_dsi.c (revision 530e7a660fb795452357b36cce26b839a9a187a9)
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 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
51 
52 	/* attempt OF style match */
53 	if (of_driver_match_device(dev, drv))
54 		return 1;
55 
56 	/* compare DSI device and driver names */
57 	if (!strcmp(dsi->name, drv->name))
58 		return 1;
59 
60 	return 0;
61 }
62 
63 static int mipi_dsi_uevent(struct device *dev, struct kobj_uevent_env *env)
64 {
65 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
66 	int err;
67 
68 	err = of_device_uevent_modalias(dev, env);
69 	if (err != -ENODEV)
70 		return err;
71 
72 	add_uevent_var(env, "MODALIAS=%s%s", MIPI_DSI_MODULE_PREFIX,
73 		       dsi->name);
74 
75 	return 0;
76 }
77 
78 static const struct dev_pm_ops mipi_dsi_device_pm_ops = {
79 	.runtime_suspend = pm_generic_runtime_suspend,
80 	.runtime_resume = pm_generic_runtime_resume,
81 	.suspend = pm_generic_suspend,
82 	.resume = pm_generic_resume,
83 	.freeze = pm_generic_freeze,
84 	.thaw = pm_generic_thaw,
85 	.poweroff = pm_generic_poweroff,
86 	.restore = pm_generic_restore,
87 };
88 
89 static struct bus_type mipi_dsi_bus_type = {
90 	.name = "mipi-dsi",
91 	.match = mipi_dsi_device_match,
92 	.uevent = mipi_dsi_uevent,
93 	.pm = &mipi_dsi_device_pm_ops,
94 };
95 
96 static int of_device_match(struct device *dev, void *data)
97 {
98 	return dev->of_node == data;
99 }
100 
101 /**
102  * of_find_mipi_dsi_device_by_node() - find the MIPI DSI device matching a
103  *    device tree node
104  * @np: device tree node
105  *
106  * Return: A pointer to the MIPI DSI device corresponding to @np or NULL if no
107  *    such device exists (or has not been registered yet).
108  */
109 struct mipi_dsi_device *of_find_mipi_dsi_device_by_node(struct device_node *np)
110 {
111 	struct device *dev;
112 
113 	dev = bus_find_device(&mipi_dsi_bus_type, NULL, np, of_device_match);
114 
115 	return dev ? to_mipi_dsi_device(dev) : NULL;
116 }
117 EXPORT_SYMBOL(of_find_mipi_dsi_device_by_node);
118 
119 static void mipi_dsi_dev_release(struct device *dev)
120 {
121 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
122 
123 	of_node_put(dev->of_node);
124 	kfree(dsi);
125 }
126 
127 static const struct device_type mipi_dsi_device_type = {
128 	.release = mipi_dsi_dev_release,
129 };
130 
131 static struct mipi_dsi_device *mipi_dsi_device_alloc(struct mipi_dsi_host *host)
132 {
133 	struct mipi_dsi_device *dsi;
134 
135 	dsi = kzalloc(sizeof(*dsi), GFP_KERNEL);
136 	if (!dsi)
137 		return ERR_PTR(-ENOMEM);
138 
139 	dsi->host = host;
140 	dsi->dev.bus = &mipi_dsi_bus_type;
141 	dsi->dev.parent = host->dev;
142 	dsi->dev.type = &mipi_dsi_device_type;
143 
144 	device_initialize(&dsi->dev);
145 
146 	return dsi;
147 }
148 
149 static int mipi_dsi_device_add(struct mipi_dsi_device *dsi)
150 {
151 	struct mipi_dsi_host *host = dsi->host;
152 
153 	dev_set_name(&dsi->dev, "%s.%d", dev_name(host->dev),  dsi->channel);
154 
155 	return device_add(&dsi->dev);
156 }
157 
158 #if IS_ENABLED(CONFIG_OF)
159 static struct mipi_dsi_device *
160 of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
161 {
162 	struct device *dev = host->dev;
163 	struct mipi_dsi_device_info info = { };
164 	int ret;
165 	u32 reg;
166 
167 	if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
168 		dev_err(dev, "modalias failure on %pOF\n", node);
169 		return ERR_PTR(-EINVAL);
170 	}
171 
172 	ret = of_property_read_u32(node, "reg", &reg);
173 	if (ret) {
174 		dev_err(dev, "device node %pOF has no valid reg property: %d\n",
175 			node, ret);
176 		return ERR_PTR(-EINVAL);
177 	}
178 
179 	info.channel = reg;
180 	info.node = of_node_get(node);
181 
182 	return mipi_dsi_device_register_full(host, &info);
183 }
184 #else
185 static struct mipi_dsi_device *
186 of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
187 {
188 	return ERR_PTR(-ENODEV);
189 }
190 #endif
191 
192 /**
193  * mipi_dsi_device_register_full - create a MIPI DSI device
194  * @host: DSI host to which this device is connected
195  * @info: pointer to template containing DSI device information
196  *
197  * Create a MIPI DSI device by using the device information provided by
198  * mipi_dsi_device_info template
199  *
200  * Returns:
201  * A pointer to the newly created MIPI DSI device, or, a pointer encoded
202  * with an error
203  */
204 struct mipi_dsi_device *
205 mipi_dsi_device_register_full(struct mipi_dsi_host *host,
206 			      const struct mipi_dsi_device_info *info)
207 {
208 	struct mipi_dsi_device *dsi;
209 	struct device *dev = host->dev;
210 	int ret;
211 
212 	if (!info) {
213 		dev_err(dev, "invalid mipi_dsi_device_info pointer\n");
214 		return ERR_PTR(-EINVAL);
215 	}
216 
217 	if (info->channel > 3) {
218 		dev_err(dev, "invalid virtual channel: %u\n", info->channel);
219 		return ERR_PTR(-EINVAL);
220 	}
221 
222 	dsi = mipi_dsi_device_alloc(host);
223 	if (IS_ERR(dsi)) {
224 		dev_err(dev, "failed to allocate DSI device %ld\n",
225 			PTR_ERR(dsi));
226 		return dsi;
227 	}
228 
229 	dsi->dev.of_node = info->node;
230 	dsi->channel = info->channel;
231 	strlcpy(dsi->name, info->type, sizeof(dsi->name));
232 
233 	ret = mipi_dsi_device_add(dsi);
234 	if (ret) {
235 		dev_err(dev, "failed to add DSI device %d\n", ret);
236 		kfree(dsi);
237 		return ERR_PTR(ret);
238 	}
239 
240 	return dsi;
241 }
242 EXPORT_SYMBOL(mipi_dsi_device_register_full);
243 
244 /**
245  * mipi_dsi_device_unregister - unregister MIPI DSI device
246  * @dsi: DSI peripheral device
247  */
248 void mipi_dsi_device_unregister(struct mipi_dsi_device *dsi)
249 {
250 	device_unregister(&dsi->dev);
251 }
252 EXPORT_SYMBOL(mipi_dsi_device_unregister);
253 
254 static DEFINE_MUTEX(host_lock);
255 static LIST_HEAD(host_list);
256 
257 /**
258  * of_find_mipi_dsi_host_by_node() - find the MIPI DSI host matching a
259  *				     device tree node
260  * @node: device tree node
261  *
262  * Returns:
263  * A pointer to the MIPI DSI host corresponding to @node or NULL if no
264  * such device exists (or has not been registered yet).
265  */
266 struct mipi_dsi_host *of_find_mipi_dsi_host_by_node(struct device_node *node)
267 {
268 	struct mipi_dsi_host *host;
269 
270 	mutex_lock(&host_lock);
271 
272 	list_for_each_entry(host, &host_list, list) {
273 		if (host->dev->of_node == node) {
274 			mutex_unlock(&host_lock);
275 			return host;
276 		}
277 	}
278 
279 	mutex_unlock(&host_lock);
280 
281 	return NULL;
282 }
283 EXPORT_SYMBOL(of_find_mipi_dsi_host_by_node);
284 
285 int mipi_dsi_host_register(struct mipi_dsi_host *host)
286 {
287 	struct device_node *node;
288 
289 	for_each_available_child_of_node(host->dev->of_node, node) {
290 		/* skip nodes without reg property */
291 		if (!of_find_property(node, "reg", NULL))
292 			continue;
293 		of_mipi_dsi_device_add(host, node);
294 	}
295 
296 	mutex_lock(&host_lock);
297 	list_add_tail(&host->list, &host_list);
298 	mutex_unlock(&host_lock);
299 
300 	return 0;
301 }
302 EXPORT_SYMBOL(mipi_dsi_host_register);
303 
304 static int mipi_dsi_remove_device_fn(struct device *dev, void *priv)
305 {
306 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
307 
308 	mipi_dsi_device_unregister(dsi);
309 
310 	return 0;
311 }
312 
313 void mipi_dsi_host_unregister(struct mipi_dsi_host *host)
314 {
315 	device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn);
316 
317 	mutex_lock(&host_lock);
318 	list_del_init(&host->list);
319 	mutex_unlock(&host_lock);
320 }
321 EXPORT_SYMBOL(mipi_dsi_host_unregister);
322 
323 /**
324  * mipi_dsi_attach - attach a DSI device to its DSI host
325  * @dsi: DSI peripheral
326  */
327 int mipi_dsi_attach(struct mipi_dsi_device *dsi)
328 {
329 	const struct mipi_dsi_host_ops *ops = dsi->host->ops;
330 
331 	if (!ops || !ops->attach)
332 		return -ENOSYS;
333 
334 	return ops->attach(dsi->host, dsi);
335 }
336 EXPORT_SYMBOL(mipi_dsi_attach);
337 
338 /**
339  * mipi_dsi_detach - detach a DSI device from its DSI host
340  * @dsi: DSI peripheral
341  */
342 int mipi_dsi_detach(struct mipi_dsi_device *dsi)
343 {
344 	const struct mipi_dsi_host_ops *ops = dsi->host->ops;
345 
346 	if (!ops || !ops->detach)
347 		return -ENOSYS;
348 
349 	return ops->detach(dsi->host, dsi);
350 }
351 EXPORT_SYMBOL(mipi_dsi_detach);
352 
353 static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
354 					struct mipi_dsi_msg *msg)
355 {
356 	const struct mipi_dsi_host_ops *ops = dsi->host->ops;
357 
358 	if (!ops || !ops->transfer)
359 		return -ENOSYS;
360 
361 	if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
362 		msg->flags |= MIPI_DSI_MSG_USE_LPM;
363 
364 	return ops->transfer(dsi->host, msg);
365 }
366 
367 /**
368  * mipi_dsi_packet_format_is_short - check if a packet is of the short format
369  * @type: MIPI DSI data type of the packet
370  *
371  * Return: true if the packet for the given data type is a short packet, false
372  * otherwise.
373  */
374 bool mipi_dsi_packet_format_is_short(u8 type)
375 {
376 	switch (type) {
377 	case MIPI_DSI_V_SYNC_START:
378 	case MIPI_DSI_V_SYNC_END:
379 	case MIPI_DSI_H_SYNC_START:
380 	case MIPI_DSI_H_SYNC_END:
381 	case MIPI_DSI_END_OF_TRANSMISSION:
382 	case MIPI_DSI_COLOR_MODE_OFF:
383 	case MIPI_DSI_COLOR_MODE_ON:
384 	case MIPI_DSI_SHUTDOWN_PERIPHERAL:
385 	case MIPI_DSI_TURN_ON_PERIPHERAL:
386 	case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
387 	case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
388 	case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
389 	case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
390 	case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
391 	case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
392 	case MIPI_DSI_DCS_SHORT_WRITE:
393 	case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
394 	case MIPI_DSI_DCS_READ:
395 	case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
396 		return true;
397 	}
398 
399 	return false;
400 }
401 EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
402 
403 /**
404  * mipi_dsi_packet_format_is_long - check if a packet is of the long format
405  * @type: MIPI DSI data type of the packet
406  *
407  * Return: true if the packet for the given data type is a long packet, false
408  * otherwise.
409  */
410 bool mipi_dsi_packet_format_is_long(u8 type)
411 {
412 	switch (type) {
413 	case MIPI_DSI_NULL_PACKET:
414 	case MIPI_DSI_BLANKING_PACKET:
415 	case MIPI_DSI_GENERIC_LONG_WRITE:
416 	case MIPI_DSI_DCS_LONG_WRITE:
417 	case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
418 	case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
419 	case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
420 	case MIPI_DSI_PACKED_PIXEL_STREAM_30:
421 	case MIPI_DSI_PACKED_PIXEL_STREAM_36:
422 	case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
423 	case MIPI_DSI_PACKED_PIXEL_STREAM_16:
424 	case MIPI_DSI_PACKED_PIXEL_STREAM_18:
425 	case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
426 	case MIPI_DSI_PACKED_PIXEL_STREAM_24:
427 		return true;
428 	}
429 
430 	return false;
431 }
432 EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
433 
434 /**
435  * mipi_dsi_create_packet - create a packet from a message according to the
436  *     DSI protocol
437  * @packet: pointer to a DSI packet structure
438  * @msg: message to translate into a packet
439  *
440  * Return: 0 on success or a negative error code on failure.
441  */
442 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
443 			   const struct mipi_dsi_msg *msg)
444 {
445 	if (!packet || !msg)
446 		return -EINVAL;
447 
448 	/* do some minimum sanity checking */
449 	if (!mipi_dsi_packet_format_is_short(msg->type) &&
450 	    !mipi_dsi_packet_format_is_long(msg->type))
451 		return -EINVAL;
452 
453 	if (msg->channel > 3)
454 		return -EINVAL;
455 
456 	memset(packet, 0, sizeof(*packet));
457 	packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
458 
459 	/* TODO: compute ECC if hardware support is not available */
460 
461 	/*
462 	 * Long write packets contain the word count in header bytes 1 and 2.
463 	 * The payload follows the header and is word count bytes long.
464 	 *
465 	 * Short write packets encode up to two parameters in header bytes 1
466 	 * and 2.
467 	 */
468 	if (mipi_dsi_packet_format_is_long(msg->type)) {
469 		packet->header[1] = (msg->tx_len >> 0) & 0xff;
470 		packet->header[2] = (msg->tx_len >> 8) & 0xff;
471 
472 		packet->payload_length = msg->tx_len;
473 		packet->payload = msg->tx_buf;
474 	} else {
475 		const u8 *tx = msg->tx_buf;
476 
477 		packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
478 		packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
479 	}
480 
481 	packet->size = sizeof(packet->header) + packet->payload_length;
482 
483 	return 0;
484 }
485 EXPORT_SYMBOL(mipi_dsi_create_packet);
486 
487 /**
488  * mipi_dsi_shutdown_peripheral() - sends a Shutdown Peripheral command
489  * @dsi: DSI peripheral device
490  *
491  * Return: 0 on success or a negative error code on failure.
492  */
493 int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
494 {
495 	struct mipi_dsi_msg msg = {
496 		.channel = dsi->channel,
497 		.type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
498 		.tx_buf = (u8 [2]) { 0, 0 },
499 		.tx_len = 2,
500 	};
501 	int ret = mipi_dsi_device_transfer(dsi, &msg);
502 
503 	return (ret < 0) ? ret : 0;
504 }
505 EXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
506 
507 /**
508  * mipi_dsi_turn_on_peripheral() - sends a Turn On Peripheral command
509  * @dsi: DSI peripheral device
510  *
511  * Return: 0 on success or a negative error code on failure.
512  */
513 int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
514 {
515 	struct mipi_dsi_msg msg = {
516 		.channel = dsi->channel,
517 		.type = MIPI_DSI_TURN_ON_PERIPHERAL,
518 		.tx_buf = (u8 [2]) { 0, 0 },
519 		.tx_len = 2,
520 	};
521 	int ret = mipi_dsi_device_transfer(dsi, &msg);
522 
523 	return (ret < 0) ? ret : 0;
524 }
525 EXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
526 
527 /*
528  * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the
529  *    the payload in a long packet transmitted from the peripheral back to the
530  *    host processor
531  * @dsi: DSI peripheral device
532  * @value: the maximum size of the payload
533  *
534  * Return: 0 on success or a negative error code on failure.
535  */
536 int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
537 					    u16 value)
538 {
539 	u8 tx[2] = { value & 0xff, value >> 8 };
540 	struct mipi_dsi_msg msg = {
541 		.channel = dsi->channel,
542 		.type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
543 		.tx_len = sizeof(tx),
544 		.tx_buf = tx,
545 	};
546 	int ret = mipi_dsi_device_transfer(dsi, &msg);
547 
548 	return (ret < 0) ? ret : 0;
549 }
550 EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
551 
552 /**
553  * mipi_dsi_generic_write() - transmit data using a generic write packet
554  * @dsi: DSI peripheral device
555  * @payload: buffer containing the payload
556  * @size: size of payload buffer
557  *
558  * This function will automatically choose the right data type depending on
559  * the payload length.
560  *
561  * Return: The number of bytes transmitted on success or a negative error code
562  * on failure.
563  */
564 ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
565 			       size_t size)
566 {
567 	struct mipi_dsi_msg msg = {
568 		.channel = dsi->channel,
569 		.tx_buf = payload,
570 		.tx_len = size
571 	};
572 
573 	switch (size) {
574 	case 0:
575 		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
576 		break;
577 
578 	case 1:
579 		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
580 		break;
581 
582 	case 2:
583 		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
584 		break;
585 
586 	default:
587 		msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
588 		break;
589 	}
590 
591 	return mipi_dsi_device_transfer(dsi, &msg);
592 }
593 EXPORT_SYMBOL(mipi_dsi_generic_write);
594 
595 /**
596  * mipi_dsi_generic_read() - receive data using a generic read packet
597  * @dsi: DSI peripheral device
598  * @params: buffer containing the request parameters
599  * @num_params: number of request parameters
600  * @data: buffer in which to return the received data
601  * @size: size of receive buffer
602  *
603  * This function will automatically choose the right data type depending on
604  * the number of parameters passed in.
605  *
606  * Return: The number of bytes successfully read or a negative error code on
607  * failure.
608  */
609 ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
610 			      size_t num_params, void *data, size_t size)
611 {
612 	struct mipi_dsi_msg msg = {
613 		.channel = dsi->channel,
614 		.tx_len = num_params,
615 		.tx_buf = params,
616 		.rx_len = size,
617 		.rx_buf = data
618 	};
619 
620 	switch (num_params) {
621 	case 0:
622 		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
623 		break;
624 
625 	case 1:
626 		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
627 		break;
628 
629 	case 2:
630 		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
631 		break;
632 
633 	default:
634 		return -EINVAL;
635 	}
636 
637 	return mipi_dsi_device_transfer(dsi, &msg);
638 }
639 EXPORT_SYMBOL(mipi_dsi_generic_read);
640 
641 /**
642  * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
643  * @dsi: DSI peripheral device
644  * @data: buffer containing data to be transmitted
645  * @len: size of transmission buffer
646  *
647  * This function will automatically choose the right data type depending on
648  * the command payload length.
649  *
650  * Return: The number of bytes successfully transmitted or a negative error
651  * code on failure.
652  */
653 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
654 				  const void *data, size_t len)
655 {
656 	struct mipi_dsi_msg msg = {
657 		.channel = dsi->channel,
658 		.tx_buf = data,
659 		.tx_len = len
660 	};
661 
662 	switch (len) {
663 	case 0:
664 		return -EINVAL;
665 
666 	case 1:
667 		msg.type = MIPI_DSI_DCS_SHORT_WRITE;
668 		break;
669 
670 	case 2:
671 		msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
672 		break;
673 
674 	default:
675 		msg.type = MIPI_DSI_DCS_LONG_WRITE;
676 		break;
677 	}
678 
679 	return mipi_dsi_device_transfer(dsi, &msg);
680 }
681 EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
682 
683 /**
684  * mipi_dsi_dcs_write() - send DCS write command
685  * @dsi: DSI peripheral device
686  * @cmd: DCS command
687  * @data: buffer containing the command payload
688  * @len: command payload length
689  *
690  * This function will automatically choose the right data type depending on
691  * the command payload length.
692  *
693  * Return: The number of bytes successfully transmitted or a negative error
694  * code on failure.
695  */
696 ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
697 			   const void *data, size_t len)
698 {
699 	ssize_t err;
700 	size_t size;
701 	u8 *tx;
702 
703 	if (len > 0) {
704 		size = 1 + len;
705 
706 		tx = kmalloc(size, GFP_KERNEL);
707 		if (!tx)
708 			return -ENOMEM;
709 
710 		/* concatenate the DCS command byte and the payload */
711 		tx[0] = cmd;
712 		memcpy(&tx[1], data, len);
713 	} else {
714 		tx = &cmd;
715 		size = 1;
716 	}
717 
718 	err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
719 
720 	if (len > 0)
721 		kfree(tx);
722 
723 	return err;
724 }
725 EXPORT_SYMBOL(mipi_dsi_dcs_write);
726 
727 /**
728  * mipi_dsi_dcs_read() - send DCS read request command
729  * @dsi: DSI peripheral device
730  * @cmd: DCS command
731  * @data: buffer in which to receive data
732  * @len: size of receive buffer
733  *
734  * Return: The number of bytes read or a negative error code on failure.
735  */
736 ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
737 			  size_t len)
738 {
739 	struct mipi_dsi_msg msg = {
740 		.channel = dsi->channel,
741 		.type = MIPI_DSI_DCS_READ,
742 		.tx_buf = &cmd,
743 		.tx_len = 1,
744 		.rx_buf = data,
745 		.rx_len = len
746 	};
747 
748 	return mipi_dsi_device_transfer(dsi, &msg);
749 }
750 EXPORT_SYMBOL(mipi_dsi_dcs_read);
751 
752 /**
753  * mipi_dsi_dcs_nop() - send DCS nop packet
754  * @dsi: DSI peripheral device
755  *
756  * Return: 0 on success or a negative error code on failure.
757  */
758 int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
759 {
760 	ssize_t err;
761 
762 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
763 	if (err < 0)
764 		return err;
765 
766 	return 0;
767 }
768 EXPORT_SYMBOL(mipi_dsi_dcs_nop);
769 
770 /**
771  * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
772  * @dsi: DSI peripheral device
773  *
774  * Return: 0 on success or a negative error code on failure.
775  */
776 int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
777 {
778 	ssize_t err;
779 
780 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
781 	if (err < 0)
782 		return err;
783 
784 	return 0;
785 }
786 EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
787 
788 /**
789  * mipi_dsi_dcs_get_power_mode() - query the display module's current power
790  *    mode
791  * @dsi: DSI peripheral device
792  * @mode: return location for the current power mode
793  *
794  * Return: 0 on success or a negative error code on failure.
795  */
796 int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
797 {
798 	ssize_t err;
799 
800 	err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
801 				sizeof(*mode));
802 	if (err <= 0) {
803 		if (err == 0)
804 			err = -ENODATA;
805 
806 		return err;
807 	}
808 
809 	return 0;
810 }
811 EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
812 
813 /**
814  * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
815  *    data used by the interface
816  * @dsi: DSI peripheral device
817  * @format: return location for the pixel format
818  *
819  * Return: 0 on success or a negative error code on failure.
820  */
821 int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
822 {
823 	ssize_t err;
824 
825 	err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
826 				sizeof(*format));
827 	if (err <= 0) {
828 		if (err == 0)
829 			err = -ENODATA;
830 
831 		return err;
832 	}
833 
834 	return 0;
835 }
836 EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
837 
838 /**
839  * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
840  *    display module except interface communication
841  * @dsi: DSI peripheral device
842  *
843  * Return: 0 on success or a negative error code on failure.
844  */
845 int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
846 {
847 	ssize_t err;
848 
849 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
850 	if (err < 0)
851 		return err;
852 
853 	return 0;
854 }
855 EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
856 
857 /**
858  * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
859  *    module
860  * @dsi: DSI peripheral device
861  *
862  * Return: 0 on success or a negative error code on failure.
863  */
864 int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
865 {
866 	ssize_t err;
867 
868 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
869 	if (err < 0)
870 		return err;
871 
872 	return 0;
873 }
874 EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
875 
876 /**
877  * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
878  *    display device
879  * @dsi: DSI peripheral device
880  *
881  * Return: 0 on success or a negative error code on failure.
882  */
883 int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
884 {
885 	ssize_t err;
886 
887 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
888 	if (err < 0)
889 		return err;
890 
891 	return 0;
892 }
893 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
894 
895 /**
896  * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
897  *    display device
898  * @dsi: DSI peripheral device
899  *
900  * Return: 0 on success or a negative error code on failure
901  */
902 int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
903 {
904 	ssize_t err;
905 
906 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
907 	if (err < 0)
908 		return err;
909 
910 	return 0;
911 }
912 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
913 
914 /**
915  * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
916  *    memory accessed by the host processor
917  * @dsi: DSI peripheral device
918  * @start: first column of frame memory
919  * @end: last column of frame memory
920  *
921  * Return: 0 on success or a negative error code on failure.
922  */
923 int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
924 				    u16 end)
925 {
926 	u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
927 	ssize_t err;
928 
929 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
930 				 sizeof(payload));
931 	if (err < 0)
932 		return err;
933 
934 	return 0;
935 }
936 EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
937 
938 /**
939  * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
940  *    memory accessed by the host processor
941  * @dsi: DSI peripheral device
942  * @start: first page of frame memory
943  * @end: last page of frame memory
944  *
945  * Return: 0 on success or a negative error code on failure.
946  */
947 int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
948 				  u16 end)
949 {
950 	u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
951 	ssize_t err;
952 
953 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
954 				 sizeof(payload));
955 	if (err < 0)
956 		return err;
957 
958 	return 0;
959 }
960 EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
961 
962 /**
963  * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
964  *    output signal on the TE signal line
965  * @dsi: DSI peripheral device
966  *
967  * Return: 0 on success or a negative error code on failure
968  */
969 int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
970 {
971 	ssize_t err;
972 
973 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
974 	if (err < 0)
975 		return err;
976 
977 	return 0;
978 }
979 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
980 
981 /**
982  * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
983  *    output signal on the TE signal line.
984  * @dsi: DSI peripheral device
985  * @mode: the Tearing Effect Output Line mode
986  *
987  * Return: 0 on success or a negative error code on failure
988  */
989 int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
990 			     enum mipi_dsi_dcs_tear_mode mode)
991 {
992 	u8 value = mode;
993 	ssize_t err;
994 
995 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
996 				 sizeof(value));
997 	if (err < 0)
998 		return err;
999 
1000 	return 0;
1001 }
1002 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
1003 
1004 /**
1005  * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
1006  *    data used by the interface
1007  * @dsi: DSI peripheral device
1008  * @format: pixel format
1009  *
1010  * Return: 0 on success or a negative error code on failure.
1011  */
1012 int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
1013 {
1014 	ssize_t err;
1015 
1016 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
1017 				 sizeof(format));
1018 	if (err < 0)
1019 		return err;
1020 
1021 	return 0;
1022 }
1023 EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
1024 
1025 /**
1026  * mipi_dsi_dcs_set_tear_scanline() - set the scanline to use as trigger for
1027  *    the Tearing Effect output signal of the display module
1028  * @dsi: DSI peripheral device
1029  * @scanline: scanline to use as trigger
1030  *
1031  * Return: 0 on success or a negative error code on failure
1032  */
1033 int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline)
1034 {
1035 	u8 payload[3] = { MIPI_DCS_SET_TEAR_SCANLINE, scanline >> 8,
1036 			  scanline & 0xff };
1037 	ssize_t err;
1038 
1039 	err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
1040 	if (err < 0)
1041 		return err;
1042 
1043 	return 0;
1044 }
1045 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_scanline);
1046 
1047 /**
1048  * mipi_dsi_dcs_set_display_brightness() - sets the brightness value of the
1049  *    display
1050  * @dsi: DSI peripheral device
1051  * @brightness: brightness value
1052  *
1053  * Return: 0 on success or a negative error code on failure.
1054  */
1055 int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
1056 					u16 brightness)
1057 {
1058 	u8 payload[2] = { brightness & 0xff, brightness >> 8 };
1059 	ssize_t err;
1060 
1061 	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
1062 				 payload, sizeof(payload));
1063 	if (err < 0)
1064 		return err;
1065 
1066 	return 0;
1067 }
1068 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_brightness);
1069 
1070 /**
1071  * mipi_dsi_dcs_get_display_brightness() - gets the current brightness value
1072  *    of the display
1073  * @dsi: DSI peripheral device
1074  * @brightness: brightness value
1075  *
1076  * Return: 0 on success or a negative error code on failure.
1077  */
1078 int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
1079 					u16 *brightness)
1080 {
1081 	ssize_t err;
1082 
1083 	err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
1084 				brightness, sizeof(*brightness));
1085 	if (err <= 0) {
1086 		if (err == 0)
1087 			err = -ENODATA;
1088 
1089 		return err;
1090 	}
1091 
1092 	return 0;
1093 }
1094 EXPORT_SYMBOL(mipi_dsi_dcs_get_display_brightness);
1095 
1096 static int mipi_dsi_drv_probe(struct device *dev)
1097 {
1098 	struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
1099 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1100 
1101 	return drv->probe(dsi);
1102 }
1103 
1104 static int mipi_dsi_drv_remove(struct device *dev)
1105 {
1106 	struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
1107 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1108 
1109 	return drv->remove(dsi);
1110 }
1111 
1112 static void mipi_dsi_drv_shutdown(struct device *dev)
1113 {
1114 	struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
1115 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1116 
1117 	drv->shutdown(dsi);
1118 }
1119 
1120 /**
1121  * mipi_dsi_driver_register_full() - register a driver for DSI devices
1122  * @drv: DSI driver structure
1123  * @owner: owner module
1124  *
1125  * Return: 0 on success or a negative error code on failure.
1126  */
1127 int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
1128 				  struct module *owner)
1129 {
1130 	drv->driver.bus = &mipi_dsi_bus_type;
1131 	drv->driver.owner = owner;
1132 
1133 	if (drv->probe)
1134 		drv->driver.probe = mipi_dsi_drv_probe;
1135 	if (drv->remove)
1136 		drv->driver.remove = mipi_dsi_drv_remove;
1137 	if (drv->shutdown)
1138 		drv->driver.shutdown = mipi_dsi_drv_shutdown;
1139 
1140 	return driver_register(&drv->driver);
1141 }
1142 EXPORT_SYMBOL(mipi_dsi_driver_register_full);
1143 
1144 /**
1145  * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
1146  * @drv: DSI driver structure
1147  *
1148  * Return: 0 on success or a negative error code on failure.
1149  */
1150 void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
1151 {
1152 	driver_unregister(&drv->driver);
1153 }
1154 EXPORT_SYMBOL(mipi_dsi_driver_unregister);
1155 
1156 static int __init mipi_dsi_bus_init(void)
1157 {
1158 	return bus_register(&mipi_dsi_bus_type);
1159 }
1160 postcore_initcall(mipi_dsi_bus_init);
1161 
1162 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1163 MODULE_DESCRIPTION("MIPI DSI Bus");
1164 MODULE_LICENSE("GPL and additional rights");
1165