blob: 5d7243da23238183c6394330acf5efebeb5f38a7 [file] [log] [blame]
Andrzej Hajda068a0022013-12-04 16:35:12 +01001/*
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
Thierry Reding009081e2014-08-05 10:41:13 +020038/**
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
Andrzej Hajda068a0022013-12-04 16:35:12 +010048static int mipi_dsi_device_match(struct device *dev, struct device_driver *drv)
49{
50 return of_driver_match_device(dev, drv);
51}
52
53static 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
64static 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
Thierry Reding3ef05922014-08-06 08:53:39 +020070static 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 */
83struct 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}
91EXPORT_SYMBOL(of_find_mipi_dsi_device_by_node);
92
Andrzej Hajda068a0022013-12-04 16:35:12 +010093static 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
101static const struct device_type mipi_dsi_device_type = {
102 .release = mipi_dsi_dev_release,
103};
104
105static 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
123static 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
Archit Tanejafc903eb2016-02-12 14:48:30 +0530132#if IS_ENABLED(CONFIG_OF)
Andrzej Hajda068a0022013-12-04 16:35:12 +0100133static struct mipi_dsi_device *
134of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
135{
Andrzej Hajda068a0022013-12-04 16:35:12 +0100136 struct device *dev = host->dev;
Archit Tanejac63ae8a2016-02-12 14:48:31 +0530137 struct mipi_dsi_device_info info = { };
Andrzej Hajda068a0022013-12-04 16:35:12 +0100138 int ret;
139 u32 reg;
140
141 ret = of_property_read_u32(node, "reg", &reg);
142 if (ret) {
143 dev_err(dev, "device node %s has no valid reg property: %d\n",
144 node->full_name, ret);
145 return ERR_PTR(-EINVAL);
146 }
147
Archit Tanejac63ae8a2016-02-12 14:48:31 +0530148 info.channel = reg;
149 info.node = of_node_get(node);
Andrzej Hajda068a0022013-12-04 16:35:12 +0100150
Archit Tanejac63ae8a2016-02-12 14:48:31 +0530151 return mipi_dsi_device_register_full(host, &info);
Andrzej Hajda068a0022013-12-04 16:35:12 +0100152}
Archit Tanejafc903eb2016-02-12 14:48:30 +0530153#else
154static struct mipi_dsi_device *
155of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
156{
157 return ERR_PTR(-ENODEV);
158}
159#endif
Andrzej Hajda068a0022013-12-04 16:35:12 +0100160
Archit Tanejac63ae8a2016-02-12 14:48:31 +0530161/**
162 * mipi_dsi_device_register_full - create a MIPI DSI device
163 * @host: DSI host to which this device is connected
164 * @info: pointer to template containing DSI device information
165 *
166 * Create a MIPI DSI device by using the device information provided by
167 * mipi_dsi_device_info template
168 *
169 * Returns:
170 * A pointer to the newly created MIPI DSI device, or, a pointer encoded
171 * with an error
172 */
173struct mipi_dsi_device *
174mipi_dsi_device_register_full(struct mipi_dsi_host *host,
175 const struct mipi_dsi_device_info *info)
176{
177 struct mipi_dsi_device *dsi;
178 struct device *dev = host->dev;
179 int ret;
180
181 if (!info) {
182 dev_err(dev, "invalid mipi_dsi_device_info pointer\n");
183 return ERR_PTR(-EINVAL);
184 }
185
186 if (info->channel > 3) {
187 dev_err(dev, "invalid virtual channel: %u\n", info->channel);
188 return ERR_PTR(-EINVAL);
189 }
190
191 dsi = mipi_dsi_device_alloc(host);
192 if (IS_ERR(dsi)) {
193 dev_err(dev, "failed to allocate DSI device %ld\n",
194 PTR_ERR(dsi));
195 return dsi;
196 }
197
198 dsi->dev.of_node = info->node;
199 dsi->channel = info->channel;
200
201 ret = mipi_dsi_device_add(dsi);
202 if (ret) {
203 dev_err(dev, "failed to add DSI device %d\n", ret);
204 kfree(dsi);
205 return ERR_PTR(ret);
206 }
207
208 return dsi;
209}
210EXPORT_SYMBOL(mipi_dsi_device_register_full);
211
Andrzej Hajda068a0022013-12-04 16:35:12 +0100212int mipi_dsi_host_register(struct mipi_dsi_host *host)
213{
214 struct device_node *node;
215
Andrzej Hajdae49640d2014-03-28 12:52:37 +0100216 for_each_available_child_of_node(host->dev->of_node, node) {
217 /* skip nodes without reg property */
218 if (!of_find_property(node, "reg", NULL))
219 continue;
Andrzej Hajda068a0022013-12-04 16:35:12 +0100220 of_mipi_dsi_device_add(host, node);
Andrzej Hajdae49640d2014-03-28 12:52:37 +0100221 }
Andrzej Hajda068a0022013-12-04 16:35:12 +0100222
223 return 0;
224}
225EXPORT_SYMBOL(mipi_dsi_host_register);
226
227static int mipi_dsi_remove_device_fn(struct device *dev, void *priv)
228{
229 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
230
231 device_unregister(&dsi->dev);
232
233 return 0;
234}
235
236void mipi_dsi_host_unregister(struct mipi_dsi_host *host)
237{
238 device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn);
239}
240EXPORT_SYMBOL(mipi_dsi_host_unregister);
241
242/**
243 * mipi_dsi_attach - attach a DSI device to its DSI host
244 * @dsi: DSI peripheral
245 */
246int mipi_dsi_attach(struct mipi_dsi_device *dsi)
247{
248 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
249
250 if (!ops || !ops->attach)
251 return -ENOSYS;
252
253 return ops->attach(dsi->host, dsi);
254}
255EXPORT_SYMBOL(mipi_dsi_attach);
256
257/**
258 * mipi_dsi_detach - detach a DSI device from its DSI host
259 * @dsi: DSI peripheral
260 */
261int mipi_dsi_detach(struct mipi_dsi_device *dsi)
262{
263 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
264
265 if (!ops || !ops->detach)
266 return -ENOSYS;
267
268 return ops->detach(dsi->host, dsi);
269}
270EXPORT_SYMBOL(mipi_dsi_detach);
271
Thierry Reding9eb491f2014-10-14 11:12:32 +0200272static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
273 struct mipi_dsi_msg *msg)
274{
275 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
276
277 if (!ops || !ops->transfer)
278 return -ENOSYS;
279
280 if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
281 msg->flags |= MIPI_DSI_MSG_USE_LPM;
282
283 return ops->transfer(dsi->host, msg);
284}
285
Andrzej Hajda068a0022013-12-04 16:35:12 +0100286/**
Thierry Reding02acb762014-11-04 14:59:14 +0100287 * mipi_dsi_packet_format_is_short - check if a packet is of the short format
288 * @type: MIPI DSI data type of the packet
289 *
290 * Return: true if the packet for the given data type is a short packet, false
291 * otherwise.
292 */
293bool mipi_dsi_packet_format_is_short(u8 type)
294{
295 switch (type) {
296 case MIPI_DSI_V_SYNC_START:
297 case MIPI_DSI_V_SYNC_END:
298 case MIPI_DSI_H_SYNC_START:
299 case MIPI_DSI_H_SYNC_END:
300 case MIPI_DSI_END_OF_TRANSMISSION:
301 case MIPI_DSI_COLOR_MODE_OFF:
302 case MIPI_DSI_COLOR_MODE_ON:
303 case MIPI_DSI_SHUTDOWN_PERIPHERAL:
304 case MIPI_DSI_TURN_ON_PERIPHERAL:
305 case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
306 case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
307 case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
308 case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
309 case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
310 case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
311 case MIPI_DSI_DCS_SHORT_WRITE:
312 case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
313 case MIPI_DSI_DCS_READ:
314 case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
315 return true;
316 }
317
318 return false;
319}
320EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
321
322/**
323 * mipi_dsi_packet_format_is_long - check if a packet is of the long format
324 * @type: MIPI DSI data type of the packet
325 *
326 * Return: true if the packet for the given data type is a long packet, false
327 * otherwise.
328 */
329bool mipi_dsi_packet_format_is_long(u8 type)
330{
331 switch (type) {
332 case MIPI_DSI_NULL_PACKET:
333 case MIPI_DSI_BLANKING_PACKET:
334 case MIPI_DSI_GENERIC_LONG_WRITE:
335 case MIPI_DSI_DCS_LONG_WRITE:
336 case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
337 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
338 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
339 case MIPI_DSI_PACKED_PIXEL_STREAM_30:
340 case MIPI_DSI_PACKED_PIXEL_STREAM_36:
341 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
342 case MIPI_DSI_PACKED_PIXEL_STREAM_16:
343 case MIPI_DSI_PACKED_PIXEL_STREAM_18:
344 case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
345 case MIPI_DSI_PACKED_PIXEL_STREAM_24:
346 return true;
347 }
348
349 return false;
350}
351EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
352
353/**
Thierry Redinga52879e2014-10-16 13:44:02 +0200354 * mipi_dsi_create_packet - create a packet from a message according to the
355 * DSI protocol
356 * @packet: pointer to a DSI packet structure
357 * @msg: message to translate into a packet
358 *
359 * Return: 0 on success or a negative error code on failure.
360 */
361int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
362 const struct mipi_dsi_msg *msg)
363{
Thierry Redinga52879e2014-10-16 13:44:02 +0200364 if (!packet || !msg)
365 return -EINVAL;
366
367 /* do some minimum sanity checking */
368 if (!mipi_dsi_packet_format_is_short(msg->type) &&
369 !mipi_dsi_packet_format_is_long(msg->type))
370 return -EINVAL;
371
372 if (msg->channel > 3)
373 return -EINVAL;
374
375 memset(packet, 0, sizeof(*packet));
376 packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
377
378 /* TODO: compute ECC if hardware support is not available */
379
380 /*
381 * Long write packets contain the word count in header bytes 1 and 2.
382 * The payload follows the header and is word count bytes long.
383 *
384 * Short write packets encode up to two parameters in header bytes 1
385 * and 2.
386 */
387 if (mipi_dsi_packet_format_is_long(msg->type)) {
388 packet->header[1] = (msg->tx_len >> 0) & 0xff;
389 packet->header[2] = (msg->tx_len >> 8) & 0xff;
390
391 packet->payload_length = msg->tx_len;
Thierry Reding9b973832014-12-05 11:46:56 +0100392 packet->payload = msg->tx_buf;
Thierry Redinga52879e2014-10-16 13:44:02 +0200393 } else {
Thierry Reding9b973832014-12-05 11:46:56 +0100394 const u8 *tx = msg->tx_buf;
395
Thierry Redinga52879e2014-10-16 13:44:02 +0200396 packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
397 packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
398 }
399
400 packet->size = sizeof(packet->header) + packet->payload_length;
401
402 return 0;
403}
404EXPORT_SYMBOL(mipi_dsi_create_packet);
405
Werner Johansson6e8c9e32015-10-30 17:38:26 -0700406/**
407 * mipi_dsi_shutdown_peripheral() - sends a Shutdown Peripheral command
408 * @dsi: DSI peripheral device
409 *
410 * Return: 0 on success or a negative error code on failure.
411 */
412int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
413{
414 struct mipi_dsi_msg msg = {
415 .channel = dsi->channel,
416 .type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
417 .tx_buf = (u8 [2]) { 0, 0 },
418 .tx_len = 2,
419 };
420
421 return mipi_dsi_device_transfer(dsi, &msg);
422}
423EXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
424
425/**
426 * mipi_dsi_turn_on_peripheral() - sends a Turn On Peripheral command
427 * @dsi: DSI peripheral device
428 *
429 * Return: 0 on success or a negative error code on failure.
430 */
431int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
432{
433 struct mipi_dsi_msg msg = {
434 .channel = dsi->channel,
435 .type = MIPI_DSI_TURN_ON_PERIPHERAL,
436 .tx_buf = (u8 [2]) { 0, 0 },
437 .tx_len = 2,
438 };
439
440 return mipi_dsi_device_transfer(dsi, &msg);
441}
442EXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
443
YoungJun Chodbf30b62014-08-05 09:27:15 +0200444/*
445 * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the
446 * the payload in a long packet transmitted from the peripheral back to the
447 * host processor
448 * @dsi: DSI peripheral device
449 * @value: the maximum size of the payload
450 *
451 * Return: 0 on success or a negative error code on failure.
452 */
453int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
454 u16 value)
455{
456 u8 tx[2] = { value & 0xff, value >> 8 };
457 struct mipi_dsi_msg msg = {
458 .channel = dsi->channel,
459 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
460 .tx_len = sizeof(tx),
461 .tx_buf = tx,
462 };
463
464 return mipi_dsi_device_transfer(dsi, &msg);
465}
466EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
467
Thierry Redinga52879e2014-10-16 13:44:02 +0200468/**
Thierry Reding550ab842014-08-05 10:36:21 +0200469 * mipi_dsi_generic_write() - transmit data using a generic write packet
470 * @dsi: DSI peripheral device
471 * @payload: buffer containing the payload
472 * @size: size of payload buffer
473 *
474 * This function will automatically choose the right data type depending on
475 * the payload length.
476 *
477 * Return: The number of bytes transmitted on success or a negative error code
478 * on failure.
479 */
480ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
481 size_t size)
482{
483 struct mipi_dsi_msg msg = {
484 .channel = dsi->channel,
485 .tx_buf = payload,
486 .tx_len = size
487 };
488
489 switch (size) {
490 case 0:
491 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
492 break;
493
494 case 1:
495 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
496 break;
497
498 case 2:
499 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
500 break;
501
502 default:
503 msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
504 break;
505 }
506
507 return mipi_dsi_device_transfer(dsi, &msg);
508}
509EXPORT_SYMBOL(mipi_dsi_generic_write);
510
511/**
512 * mipi_dsi_generic_read() - receive data using a generic read packet
513 * @dsi: DSI peripheral device
514 * @params: buffer containing the request parameters
515 * @num_params: number of request parameters
516 * @data: buffer in which to return the received data
517 * @size: size of receive buffer
518 *
519 * This function will automatically choose the right data type depending on
520 * the number of parameters passed in.
521 *
522 * Return: The number of bytes successfully read or a negative error code on
523 * failure.
524 */
525ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
526 size_t num_params, void *data, size_t size)
527{
528 struct mipi_dsi_msg msg = {
529 .channel = dsi->channel,
530 .tx_len = num_params,
531 .tx_buf = params,
532 .rx_len = size,
533 .rx_buf = data
534 };
535
536 switch (num_params) {
537 case 0:
538 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
539 break;
540
541 case 1:
542 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
543 break;
544
545 case 2:
546 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
547 break;
548
549 default:
550 return -EINVAL;
551 }
552
553 return mipi_dsi_device_transfer(dsi, &msg);
554}
555EXPORT_SYMBOL(mipi_dsi_generic_read);
556
557/**
Thierry Reding960dd612014-07-21 15:47:10 +0200558 * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
559 * @dsi: DSI peripheral device
560 * @data: buffer containing data to be transmitted
561 * @len: size of transmission buffer
562 *
563 * This function will automatically choose the right data type depending on
564 * the command payload length.
565 *
566 * Return: The number of bytes successfully transmitted or a negative error
567 * code on failure.
Andrzej Hajda068a0022013-12-04 16:35:12 +0100568 */
Thierry Reding960dd612014-07-21 15:47:10 +0200569ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
570 const void *data, size_t len)
Andrzej Hajda068a0022013-12-04 16:35:12 +0100571{
Andrzej Hajda068a0022013-12-04 16:35:12 +0100572 struct mipi_dsi_msg msg = {
Thierry Reding3c523d72014-07-21 12:28:25 +0200573 .channel = dsi->channel,
Andrzej Hajda068a0022013-12-04 16:35:12 +0100574 .tx_buf = data,
575 .tx_len = len
576 };
577
Andrzej Hajda068a0022013-12-04 16:35:12 +0100578 switch (len) {
579 case 0:
580 return -EINVAL;
Thierry Reding960dd612014-07-21 15:47:10 +0200581
Andrzej Hajda068a0022013-12-04 16:35:12 +0100582 case 1:
583 msg.type = MIPI_DSI_DCS_SHORT_WRITE;
584 break;
Thierry Reding960dd612014-07-21 15:47:10 +0200585
Andrzej Hajda068a0022013-12-04 16:35:12 +0100586 case 2:
587 msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
588 break;
Thierry Reding960dd612014-07-21 15:47:10 +0200589
Andrzej Hajda068a0022013-12-04 16:35:12 +0100590 default:
591 msg.type = MIPI_DSI_DCS_LONG_WRITE;
592 break;
593 }
594
Thierry Reding9eb491f2014-10-14 11:12:32 +0200595 return mipi_dsi_device_transfer(dsi, &msg);
Andrzej Hajda068a0022013-12-04 16:35:12 +0100596}
Thierry Reding960dd612014-07-21 15:47:10 +0200597EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
598
599/**
600 * mipi_dsi_dcs_write() - send DCS write command
601 * @dsi: DSI peripheral device
602 * @cmd: DCS command
603 * @data: buffer containing the command payload
604 * @len: command payload length
605 *
606 * This function will automatically choose the right data type depending on
607 * the command payload length.
608 *
609 * Return: The number of bytes successfully transmitted or a negative error
610 * code on failure.
611 */
612ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
613 const void *data, size_t len)
614{
615 ssize_t err;
616 size_t size;
617 u8 *tx;
618
619 if (len > 0) {
620 size = 1 + len;
621
622 tx = kmalloc(size, GFP_KERNEL);
623 if (!tx)
624 return -ENOMEM;
625
626 /* concatenate the DCS command byte and the payload */
627 tx[0] = cmd;
628 memcpy(&tx[1], data, len);
629 } else {
630 tx = &cmd;
631 size = 1;
632 }
633
634 err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
635
636 if (len > 0)
637 kfree(tx);
638
639 return err;
640}
Andrzej Hajda068a0022013-12-04 16:35:12 +0100641EXPORT_SYMBOL(mipi_dsi_dcs_write);
642
643/**
Thierry Reding960dd612014-07-21 15:47:10 +0200644 * mipi_dsi_dcs_read() - send DCS read request command
645 * @dsi: DSI peripheral device
646 * @cmd: DCS command
647 * @data: buffer in which to receive data
648 * @len: size of receive buffer
Andrzej Hajda068a0022013-12-04 16:35:12 +0100649 *
Thierry Reding960dd612014-07-21 15:47:10 +0200650 * Return: The number of bytes read or a negative error code on failure.
Andrzej Hajda068a0022013-12-04 16:35:12 +0100651 */
Thierry Reding3c523d72014-07-21 12:28:25 +0200652ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
653 size_t len)
Andrzej Hajda068a0022013-12-04 16:35:12 +0100654{
Andrzej Hajda068a0022013-12-04 16:35:12 +0100655 struct mipi_dsi_msg msg = {
Thierry Reding3c523d72014-07-21 12:28:25 +0200656 .channel = dsi->channel,
Andrzej Hajda068a0022013-12-04 16:35:12 +0100657 .type = MIPI_DSI_DCS_READ,
658 .tx_buf = &cmd,
659 .tx_len = 1,
660 .rx_buf = data,
661 .rx_len = len
662 };
663
Thierry Reding9eb491f2014-10-14 11:12:32 +0200664 return mipi_dsi_device_transfer(dsi, &msg);
Andrzej Hajda068a0022013-12-04 16:35:12 +0100665}
666EXPORT_SYMBOL(mipi_dsi_dcs_read);
667
YoungJun Cho42fe1e72014-08-05 10:38:31 +0200668/**
Thierry Reding083d5732014-08-05 11:14:02 +0200669 * mipi_dsi_dcs_nop() - send DCS nop packet
670 * @dsi: DSI peripheral device
671 *
672 * Return: 0 on success or a negative error code on failure.
673 */
674int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
675{
676 ssize_t err;
677
678 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
679 if (err < 0)
680 return err;
681
682 return 0;
683}
684EXPORT_SYMBOL(mipi_dsi_dcs_nop);
685
686/**
Thierry Reding2f16b892014-08-05 11:15:15 +0200687 * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
688 * @dsi: DSI peripheral device
689 *
690 * Return: 0 on success or a negative error code on failure.
691 */
692int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
693{
694 ssize_t err;
695
696 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
697 if (err < 0)
698 return err;
699
700 return 0;
701}
702EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
703
704/**
Thierry Reding3d9a8fc2014-08-05 11:17:06 +0200705 * mipi_dsi_dcs_get_power_mode() - query the display module's current power
706 * mode
707 * @dsi: DSI peripheral device
708 * @mode: return location for the current power mode
709 *
710 * Return: 0 on success or a negative error code on failure.
711 */
712int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
713{
714 ssize_t err;
715
716 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
717 sizeof(*mode));
718 if (err <= 0) {
719 if (err == 0)
720 err = -ENODATA;
721
722 return err;
723 }
724
725 return 0;
726}
727EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
728
729/**
Thierry Reding5cc0af12014-08-05 11:18:46 +0200730 * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
731 * data used by the interface
732 * @dsi: DSI peripheral device
733 * @format: return location for the pixel format
734 *
735 * Return: 0 on success or a negative error code on failure.
736 */
737int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
738{
739 ssize_t err;
740
741 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
742 sizeof(*format));
743 if (err <= 0) {
744 if (err == 0)
745 err = -ENODATA;
746
747 return err;
748 }
749
750 return 0;
751}
752EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
753
754/**
YoungJun Cho42fe1e72014-08-05 10:38:31 +0200755 * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
756 * display module except interface communication
757 * @dsi: DSI peripheral device
758 *
759 * Return: 0 on success or a negative error code on failure.
760 */
761int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
762{
763 ssize_t err;
764
765 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
766 if (err < 0)
767 return err;
768
769 return 0;
770}
771EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
772
773/**
774 * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
775 * module
776 * @dsi: DSI peripheral device
777 *
778 * Return: 0 on success or a negative error code on failure.
779 */
780int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
781{
782 ssize_t err;
783
784 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
785 if (err < 0)
786 return err;
787
788 return 0;
789}
790EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
791
792/**
793 * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
794 * display device
795 * @dsi: DSI peripheral device
796 *
797 * Return: 0 on success or a negative error code on failure.
798 */
799int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
800{
801 ssize_t err;
802
803 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
804 if (err < 0)
805 return err;
806
807 return 0;
808}
809EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
810
811/**
812 * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
813 * display device
814 * @dsi: DSI peripheral device
815 *
816 * Return: 0 on success or a negative error code on failure
817 */
818int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
819{
820 ssize_t err;
821
822 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
823 if (err < 0)
824 return err;
825
826 return 0;
827}
828EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
829
830/**
Thierry Reding3b46d4a2014-08-05 11:20:25 +0200831 * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
832 * memory accessed by the host processor
833 * @dsi: DSI peripheral device
834 * @start: first column of frame memory
835 * @end: last column of frame memory
836 *
837 * Return: 0 on success or a negative error code on failure.
838 */
839int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
840 u16 end)
841{
842 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
843 ssize_t err;
844
845 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
846 sizeof(payload));
847 if (err < 0)
848 return err;
849
850 return 0;
851}
852EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
853
854/**
855 * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
856 * memory accessed by the host processor
857 * @dsi: DSI peripheral device
858 * @start: first page of frame memory
859 * @end: last page of frame memory
860 *
861 * Return: 0 on success or a negative error code on failure.
862 */
863int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
864 u16 end)
865{
866 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
867 ssize_t err;
868
869 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
870 sizeof(payload));
871 if (err < 0)
872 return err;
873
874 return 0;
875}
876EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
877
878/**
YoungJun Cho42fe1e72014-08-05 10:38:31 +0200879 * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
880 * output signal on the TE signal line
881 * @dsi: DSI peripheral device
882 *
883 * Return: 0 on success or a negative error code on failure
884 */
885int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
886{
887 ssize_t err;
888
889 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
890 if (err < 0)
891 return err;
892
893 return 0;
894}
895EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
896
897/**
898 * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
899 * output signal on the TE signal line.
900 * @dsi: DSI peripheral device
901 * @mode: the Tearing Effect Output Line mode
902 *
903 * Return: 0 on success or a negative error code on failure
904 */
905int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
906 enum mipi_dsi_dcs_tear_mode mode)
907{
908 u8 value = mode;
909 ssize_t err;
910
911 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
912 sizeof(value));
913 if (err < 0)
914 return err;
915
916 return 0;
917}
918EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
919
Thierry Reding5cc0af12014-08-05 11:18:46 +0200920/**
921 * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
922 * data used by the interface
923 * @dsi: DSI peripheral device
924 * @format: pixel format
925 *
926 * Return: 0 on success or a negative error code on failure.
927 */
928int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
929{
930 ssize_t err;
931
932 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
933 sizeof(format));
934 if (err < 0)
935 return err;
936
937 return 0;
938}
939EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
940
Andrzej Hajda068a0022013-12-04 16:35:12 +0100941static int mipi_dsi_drv_probe(struct device *dev)
942{
943 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
944 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
945
946 return drv->probe(dsi);
947}
948
949static int mipi_dsi_drv_remove(struct device *dev)
950{
951 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
952 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
953
954 return drv->remove(dsi);
955}
956
Thierry Redingd1621802014-04-29 17:19:57 +0200957static void mipi_dsi_drv_shutdown(struct device *dev)
958{
959 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
960 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
961
962 drv->shutdown(dsi);
963}
964
Andrzej Hajda068a0022013-12-04 16:35:12 +0100965/**
Thierry Reding99035e92014-11-04 16:09:56 +0100966 * mipi_dsi_driver_register_full() - register a driver for DSI devices
Andrzej Hajda068a0022013-12-04 16:35:12 +0100967 * @drv: DSI driver structure
Thierry Reding99035e92014-11-04 16:09:56 +0100968 * @owner: owner module
Thierry Reding009081e2014-08-05 10:41:13 +0200969 *
970 * Return: 0 on success or a negative error code on failure.
Andrzej Hajda068a0022013-12-04 16:35:12 +0100971 */
Thierry Reding99035e92014-11-04 16:09:56 +0100972int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
973 struct module *owner)
Andrzej Hajda068a0022013-12-04 16:35:12 +0100974{
975 drv->driver.bus = &mipi_dsi_bus_type;
Thierry Reding99035e92014-11-04 16:09:56 +0100976 drv->driver.owner = owner;
977
Andrzej Hajda068a0022013-12-04 16:35:12 +0100978 if (drv->probe)
979 drv->driver.probe = mipi_dsi_drv_probe;
980 if (drv->remove)
981 drv->driver.remove = mipi_dsi_drv_remove;
Thierry Redingd1621802014-04-29 17:19:57 +0200982 if (drv->shutdown)
983 drv->driver.shutdown = mipi_dsi_drv_shutdown;
Andrzej Hajda068a0022013-12-04 16:35:12 +0100984
985 return driver_register(&drv->driver);
986}
Thierry Reding99035e92014-11-04 16:09:56 +0100987EXPORT_SYMBOL(mipi_dsi_driver_register_full);
Andrzej Hajda068a0022013-12-04 16:35:12 +0100988
989/**
Thierry Reding009081e2014-08-05 10:41:13 +0200990 * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
Andrzej Hajda068a0022013-12-04 16:35:12 +0100991 * @drv: DSI driver structure
Thierry Reding009081e2014-08-05 10:41:13 +0200992 *
993 * Return: 0 on success or a negative error code on failure.
Andrzej Hajda068a0022013-12-04 16:35:12 +0100994 */
995void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
996{
997 driver_unregister(&drv->driver);
998}
999EXPORT_SYMBOL(mipi_dsi_driver_unregister);
1000
1001static int __init mipi_dsi_bus_init(void)
1002{
1003 return bus_register(&mipi_dsi_bus_type);
1004}
1005postcore_initcall(mipi_dsi_bus_init);
1006
1007MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1008MODULE_DESCRIPTION("MIPI DSI Bus");
1009MODULE_LICENSE("GPL and additional rights");