1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <common.h>
36#include <clk.h>
37#include <display.h>
38#include <dm.h>
39#include <mipi_display.h>
40#include <mipi_dsi.h>
41#include <dm/devres.h>
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57int mipi_dsi_attach(struct mipi_dsi_device *dsi)
58{
59 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
60
61 if (!ops || !ops->attach)
62 return -ENOSYS;
63
64 return ops->attach(dsi->host, dsi);
65}
66EXPORT_SYMBOL(mipi_dsi_attach);
67
68
69
70
71
72int mipi_dsi_detach(struct mipi_dsi_device *dsi)
73{
74 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
75
76 if (!ops || !ops->detach)
77 return -ENOSYS;
78
79 return ops->detach(dsi->host, dsi);
80}
81EXPORT_SYMBOL(mipi_dsi_detach);
82
83
84
85
86
87
88static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
89 struct mipi_dsi_msg *msg)
90{
91 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
92
93 if (!ops || !ops->transfer)
94 return -ENOSYS;
95
96 if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
97 msg->flags |= MIPI_DSI_MSG_USE_LPM;
98
99 return ops->transfer(dsi->host, msg);
100}
101
102
103
104
105
106
107
108
109bool mipi_dsi_packet_format_is_short(u8 type)
110{
111 switch (type) {
112 case MIPI_DSI_V_SYNC_START:
113 case MIPI_DSI_V_SYNC_END:
114 case MIPI_DSI_H_SYNC_START:
115 case MIPI_DSI_H_SYNC_END:
116 case MIPI_DSI_END_OF_TRANSMISSION:
117 case MIPI_DSI_COLOR_MODE_OFF:
118 case MIPI_DSI_COLOR_MODE_ON:
119 case MIPI_DSI_SHUTDOWN_PERIPHERAL:
120 case MIPI_DSI_TURN_ON_PERIPHERAL:
121 case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
122 case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
123 case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
124 case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
125 case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
126 case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
127 case MIPI_DSI_DCS_SHORT_WRITE:
128 case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
129 case MIPI_DSI_DCS_READ:
130 case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
131 return true;
132 }
133
134 return false;
135}
136EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
137
138
139
140
141
142
143
144
145bool mipi_dsi_packet_format_is_long(u8 type)
146{
147 switch (type) {
148 case MIPI_DSI_NULL_PACKET:
149 case MIPI_DSI_BLANKING_PACKET:
150 case MIPI_DSI_GENERIC_LONG_WRITE:
151 case MIPI_DSI_DCS_LONG_WRITE:
152 case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
153 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
154 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
155 case MIPI_DSI_PACKED_PIXEL_STREAM_30:
156 case MIPI_DSI_PACKED_PIXEL_STREAM_36:
157 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
158 case MIPI_DSI_PACKED_PIXEL_STREAM_16:
159 case MIPI_DSI_PACKED_PIXEL_STREAM_18:
160 case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
161 case MIPI_DSI_PACKED_PIXEL_STREAM_24:
162 return true;
163 }
164
165 return false;
166}
167EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
168
169
170
171
172
173
174
175
176
177int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
178 const struct mipi_dsi_msg *msg)
179{
180 if (!packet || !msg)
181 return -EINVAL;
182
183
184 if (!mipi_dsi_packet_format_is_short(msg->type) &&
185 !mipi_dsi_packet_format_is_long(msg->type))
186 return -EINVAL;
187
188 if (msg->channel > 3)
189 return -EINVAL;
190
191 memset(packet, 0, sizeof(*packet));
192 packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
193
194
195
196
197
198
199
200
201
202
203 if (mipi_dsi_packet_format_is_long(msg->type)) {
204 packet->header[1] = (msg->tx_len >> 0) & 0xff;
205 packet->header[2] = (msg->tx_len >> 8) & 0xff;
206
207 packet->payload_length = msg->tx_len;
208 packet->payload = msg->tx_buf;
209 } else {
210 const u8 *tx = msg->tx_buf;
211
212 packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
213 packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
214 }
215
216 packet->size = sizeof(packet->header) + packet->payload_length;
217
218 return 0;
219}
220EXPORT_SYMBOL(mipi_dsi_create_packet);
221
222
223
224
225
226
227
228int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
229{
230 struct mipi_dsi_msg msg = {
231 .channel = dsi->channel,
232 .type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
233 .tx_buf = (u8 [2]) { 0, 0 },
234 .tx_len = 2,
235 };
236 int ret = mipi_dsi_device_transfer(dsi, &msg);
237
238 return (ret < 0) ? ret : 0;
239}
240EXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
241
242
243
244
245
246
247
248int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
249{
250 struct mipi_dsi_msg msg = {
251 .channel = dsi->channel,
252 .type = MIPI_DSI_TURN_ON_PERIPHERAL,
253 .tx_buf = (u8 [2]) { 0, 0 },
254 .tx_len = 2,
255 };
256 int ret = mipi_dsi_device_transfer(dsi, &msg);
257
258 return (ret < 0) ? ret : 0;
259}
260EXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
261
262
263
264
265
266
267
268
269
270
271int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
272 u16 value)
273{
274 u8 tx[2] = { value & 0xff, value >> 8 };
275 struct mipi_dsi_msg msg = {
276 .channel = dsi->channel,
277 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
278 .tx_len = sizeof(tx),
279 .tx_buf = tx,
280 };
281 int ret = mipi_dsi_device_transfer(dsi, &msg);
282
283 return (ret < 0) ? ret : 0;
284}
285EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
286
287
288
289
290
291
292
293
294
295
296
297
298
299ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
300 size_t size)
301{
302 struct mipi_dsi_msg msg = {
303 .channel = dsi->channel,
304 .tx_buf = payload,
305 .tx_len = size
306 };
307
308 switch (size) {
309 case 0:
310 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
311 break;
312
313 case 1:
314 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
315 break;
316
317 case 2:
318 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
319 break;
320
321 default:
322 msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
323 break;
324 }
325
326 return mipi_dsi_device_transfer(dsi, &msg);
327}
328EXPORT_SYMBOL(mipi_dsi_generic_write);
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
345 size_t num_params, void *data, size_t size)
346{
347 struct mipi_dsi_msg msg = {
348 .channel = dsi->channel,
349 .tx_len = num_params,
350 .tx_buf = params,
351 .rx_len = size,
352 .rx_buf = data
353 };
354
355 switch (num_params) {
356 case 0:
357 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
358 break;
359
360 case 1:
361 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
362 break;
363
364 case 2:
365 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
366 break;
367
368 default:
369 return -EINVAL;
370 }
371
372 return mipi_dsi_device_transfer(dsi, &msg);
373}
374EXPORT_SYMBOL(mipi_dsi_generic_read);
375
376
377
378
379
380
381
382
383
384
385
386
387
388ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
389 const void *data, size_t len)
390{
391 struct mipi_dsi_msg msg = {
392 .channel = dsi->channel,
393 .tx_buf = data,
394 .tx_len = len
395 };
396
397 switch (len) {
398 case 0:
399 return -EINVAL;
400
401 case 1:
402 msg.type = MIPI_DSI_DCS_SHORT_WRITE;
403 break;
404
405 case 2:
406 msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
407 break;
408
409 default:
410 msg.type = MIPI_DSI_DCS_LONG_WRITE;
411 break;
412 }
413
414 return mipi_dsi_device_transfer(dsi, &msg);
415}
416EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
432 const void *data, size_t len)
433{
434 ssize_t err;
435 size_t size;
436 u8 *tx;
437
438 if (len > 0) {
439 size = 1 + len;
440
441 tx = kmalloc(size, GFP_KERNEL);
442 if (!tx)
443 return -ENOMEM;
444
445
446 tx[0] = cmd;
447 memcpy(&tx[1], data, len);
448 } else {
449 tx = &cmd;
450 size = 1;
451 }
452
453 err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
454
455 if (len > 0)
456 kfree(tx);
457
458 return err;
459}
460EXPORT_SYMBOL(mipi_dsi_dcs_write);
461
462
463
464
465
466
467
468
469
470
471ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
472 size_t len)
473{
474 struct mipi_dsi_msg msg = {
475 .channel = dsi->channel,
476 .type = MIPI_DSI_DCS_READ,
477 .tx_buf = &cmd,
478 .tx_len = 1,
479 .rx_buf = data,
480 .rx_len = len
481 };
482
483 return mipi_dsi_device_transfer(dsi, &msg);
484}
485EXPORT_SYMBOL(mipi_dsi_dcs_read);
486
487
488
489
490
491
492
493int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
494{
495 ssize_t err;
496
497 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
498 if (err < 0)
499 return err;
500
501 return 0;
502}
503EXPORT_SYMBOL(mipi_dsi_dcs_nop);
504
505
506
507
508
509
510
511int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
512{
513 ssize_t err;
514
515 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
516 if (err < 0)
517 return err;
518
519 return 0;
520}
521EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
522
523
524
525
526
527
528
529
530
531int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
532{
533 ssize_t err;
534
535 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
536 sizeof(*mode));
537 if (err <= 0) {
538 if (err == 0)
539 err = -ENODATA;
540
541 return err;
542 }
543
544 return 0;
545}
546EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
547
548
549
550
551
552
553
554
555
556int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
557{
558 ssize_t err;
559
560 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
561 sizeof(*format));
562 if (err <= 0) {
563 if (err == 0)
564 err = -ENODATA;
565
566 return err;
567 }
568
569 return 0;
570}
571EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
572
573
574
575
576
577
578
579
580int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
581{
582 ssize_t err;
583
584 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
585 if (err < 0)
586 return err;
587
588 return 0;
589}
590EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
591
592
593
594
595
596
597
598
599int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
600{
601 ssize_t err;
602
603 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
604 if (err < 0)
605 return err;
606
607 return 0;
608}
609EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
610
611
612
613
614
615
616
617
618int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
619{
620 ssize_t err;
621
622 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
623 if (err < 0)
624 return err;
625
626 return 0;
627}
628EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
629
630
631
632
633
634
635
636
637int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
638{
639 ssize_t err;
640
641 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
642 if (err < 0)
643 return err;
644
645 return 0;
646}
647EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
648
649
650
651
652
653
654
655
656
657
658int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
659 u16 end)
660{
661 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
662 ssize_t err;
663
664 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
665 sizeof(payload));
666 if (err < 0)
667 return err;
668
669 return 0;
670}
671EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
672
673
674
675
676
677
678
679
680
681
682int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
683 u16 end)
684{
685 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
686 ssize_t err;
687
688 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
689 sizeof(payload));
690 if (err < 0)
691 return err;
692
693 return 0;
694}
695EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
696
697
698
699
700
701
702
703
704int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
705{
706 ssize_t err;
707
708 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
709 if (err < 0)
710 return err;
711
712 return 0;
713}
714EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
715
716
717
718
719
720
721
722
723
724int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
725 enum mipi_dsi_dcs_tear_mode mode)
726{
727 u8 value = mode;
728 ssize_t err;
729
730 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
731 sizeof(value));
732 if (err < 0)
733 return err;
734
735 return 0;
736}
737EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
738
739
740
741
742
743
744
745
746
747int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
748{
749 ssize_t err;
750
751 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
752 sizeof(format));
753 if (err < 0)
754 return err;
755
756 return 0;
757}
758EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
759
760
761
762
763
764
765
766
767
768int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline)
769{
770 u8 payload[3] = { MIPI_DCS_SET_TEAR_SCANLINE, scanline >> 8,
771 scanline & 0xff };
772 ssize_t err;
773
774 err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
775 if (err < 0)
776 return err;
777
778 return 0;
779}
780EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_scanline);
781
782
783
784
785
786
787
788
789
790int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
791 u16 brightness)
792{
793 u8 payload[2] = { brightness & 0xff, brightness >> 8 };
794 ssize_t err;
795
796 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
797 payload, sizeof(payload));
798 if (err < 0)
799 return err;
800
801 return 0;
802}
803EXPORT_SYMBOL(mipi_dsi_dcs_set_display_brightness);
804
805
806
807
808
809
810
811
812
813int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
814 u16 *brightness)
815{
816 ssize_t err;
817
818 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
819 brightness, sizeof(*brightness));
820 if (err <= 0) {
821 if (err == 0)
822 err = -ENODATA;
823
824 return err;
825 }
826
827 return 0;
828}
829EXPORT_SYMBOL(mipi_dsi_dcs_get_display_brightness);
830