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