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#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
40
41
42
43
44
45
46
47
48static 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
70static int of_device_match(struct device *dev, void *data)
71{
72 return dev->of_node == data;
73}
74
75
76
77
78
79
80
81
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
93static 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
132static struct mipi_dsi_device *
133of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
134{
135 struct mipi_dsi_device *dsi;
136 struct device *dev = host->dev;
137 int ret;
138 u32 reg;
139
140 ret = of_property_read_u32(node, "reg", ®);
141 if (ret) {
142 dev_err(dev, "device node %s has no valid reg property: %d\n",
143 node->full_name, ret);
144 return ERR_PTR(-EINVAL);
145 }
146
147 if (reg > 3) {
148 dev_err(dev, "device node %s has invalid reg property: %u\n",
149 node->full_name, reg);
150 return ERR_PTR(-EINVAL);
151 }
152
153 dsi = mipi_dsi_device_alloc(host);
154 if (IS_ERR(dsi)) {
155 dev_err(dev, "failed to allocate DSI device %s: %ld\n",
156 node->full_name, PTR_ERR(dsi));
157 return dsi;
158 }
159
160 dsi->dev.of_node = of_node_get(node);
161 dsi->channel = reg;
162
163 ret = mipi_dsi_device_add(dsi);
164 if (ret) {
165 dev_err(dev, "failed to add DSI device %s: %d\n",
166 node->full_name, ret);
167 kfree(dsi);
168 return ERR_PTR(ret);
169 }
170
171 return dsi;
172}
173
174int mipi_dsi_host_register(struct mipi_dsi_host *host)
175{
176 struct device_node *node;
177
178 for_each_available_child_of_node(host->dev->of_node, node) {
179
180 if (!of_find_property(node, "reg", NULL))
181 continue;
182 of_mipi_dsi_device_add(host, node);
183 }
184
185 return 0;
186}
187EXPORT_SYMBOL(mipi_dsi_host_register);
188
189static int mipi_dsi_remove_device_fn(struct device *dev, void *priv)
190{
191 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
192
193 device_unregister(&dsi->dev);
194
195 return 0;
196}
197
198void mipi_dsi_host_unregister(struct mipi_dsi_host *host)
199{
200 device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn);
201}
202EXPORT_SYMBOL(mipi_dsi_host_unregister);
203
204
205
206
207
208int mipi_dsi_attach(struct mipi_dsi_device *dsi)
209{
210 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
211
212 if (!ops || !ops->attach)
213 return -ENOSYS;
214
215 return ops->attach(dsi->host, dsi);
216}
217EXPORT_SYMBOL(mipi_dsi_attach);
218
219
220
221
222
223int mipi_dsi_detach(struct mipi_dsi_device *dsi)
224{
225 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
226
227 if (!ops || !ops->detach)
228 return -ENOSYS;
229
230 return ops->detach(dsi->host, dsi);
231}
232EXPORT_SYMBOL(mipi_dsi_detach);
233
234static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
235 struct mipi_dsi_msg *msg)
236{
237 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
238
239 if (!ops || !ops->transfer)
240 return -ENOSYS;
241
242 if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
243 msg->flags |= MIPI_DSI_MSG_USE_LPM;
244
245 return ops->transfer(dsi->host, msg);
246}
247
248
249
250
251
252
253
254
255bool mipi_dsi_packet_format_is_short(u8 type)
256{
257 switch (type) {
258 case MIPI_DSI_V_SYNC_START:
259 case MIPI_DSI_V_SYNC_END:
260 case MIPI_DSI_H_SYNC_START:
261 case MIPI_DSI_H_SYNC_END:
262 case MIPI_DSI_END_OF_TRANSMISSION:
263 case MIPI_DSI_COLOR_MODE_OFF:
264 case MIPI_DSI_COLOR_MODE_ON:
265 case MIPI_DSI_SHUTDOWN_PERIPHERAL:
266 case MIPI_DSI_TURN_ON_PERIPHERAL:
267 case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
268 case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
269 case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
270 case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
271 case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
272 case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
273 case MIPI_DSI_DCS_SHORT_WRITE:
274 case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
275 case MIPI_DSI_DCS_READ:
276 case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
277 return true;
278 }
279
280 return false;
281}
282EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
283
284
285
286
287
288
289
290
291bool mipi_dsi_packet_format_is_long(u8 type)
292{
293 switch (type) {
294 case MIPI_DSI_NULL_PACKET:
295 case MIPI_DSI_BLANKING_PACKET:
296 case MIPI_DSI_GENERIC_LONG_WRITE:
297 case MIPI_DSI_DCS_LONG_WRITE:
298 case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
299 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
300 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
301 case MIPI_DSI_PACKED_PIXEL_STREAM_30:
302 case MIPI_DSI_PACKED_PIXEL_STREAM_36:
303 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
304 case MIPI_DSI_PACKED_PIXEL_STREAM_16:
305 case MIPI_DSI_PACKED_PIXEL_STREAM_18:
306 case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
307 case MIPI_DSI_PACKED_PIXEL_STREAM_24:
308 return true;
309 }
310
311 return false;
312}
313EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
314
315
316
317
318
319
320
321
322
323int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
324 const struct mipi_dsi_msg *msg)
325{
326 if (!packet || !msg)
327 return -EINVAL;
328
329
330 if (!mipi_dsi_packet_format_is_short(msg->type) &&
331 !mipi_dsi_packet_format_is_long(msg->type))
332 return -EINVAL;
333
334 if (msg->channel > 3)
335 return -EINVAL;
336
337 memset(packet, 0, sizeof(*packet));
338 packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
339
340
341
342
343
344
345
346
347
348
349 if (mipi_dsi_packet_format_is_long(msg->type)) {
350 packet->header[1] = (msg->tx_len >> 0) & 0xff;
351 packet->header[2] = (msg->tx_len >> 8) & 0xff;
352
353 packet->payload_length = msg->tx_len;
354 packet->payload = msg->tx_buf;
355 } else {
356 const u8 *tx = msg->tx_buf;
357
358 packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
359 packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
360 }
361
362 packet->size = sizeof(packet->header) + packet->payload_length;
363
364 return 0;
365}
366EXPORT_SYMBOL(mipi_dsi_create_packet);
367
368
369
370
371
372
373
374
375
376
377int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
378 u16 value)
379{
380 u8 tx[2] = { value & 0xff, value >> 8 };
381 struct mipi_dsi_msg msg = {
382 .channel = dsi->channel,
383 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
384 .tx_len = sizeof(tx),
385 .tx_buf = tx,
386 };
387
388 return mipi_dsi_device_transfer(dsi, &msg);
389}
390EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
391
392
393
394
395
396
397
398
399
400
401
402
403
404ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
405 size_t size)
406{
407 struct mipi_dsi_msg msg = {
408 .channel = dsi->channel,
409 .tx_buf = payload,
410 .tx_len = size
411 };
412
413 switch (size) {
414 case 0:
415 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
416 break;
417
418 case 1:
419 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
420 break;
421
422 case 2:
423 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
424 break;
425
426 default:
427 msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
428 break;
429 }
430
431 return mipi_dsi_device_transfer(dsi, &msg);
432}
433EXPORT_SYMBOL(mipi_dsi_generic_write);
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
450 size_t num_params, void *data, size_t size)
451{
452 struct mipi_dsi_msg msg = {
453 .channel = dsi->channel,
454 .tx_len = num_params,
455 .tx_buf = params,
456 .rx_len = size,
457 .rx_buf = data
458 };
459
460 switch (num_params) {
461 case 0:
462 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
463 break;
464
465 case 1:
466 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
467 break;
468
469 case 2:
470 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
471 break;
472
473 default:
474 return -EINVAL;
475 }
476
477 return mipi_dsi_device_transfer(dsi, &msg);
478}
479EXPORT_SYMBOL(mipi_dsi_generic_read);
480
481
482
483
484
485
486
487
488
489
490
491
492
493ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
494 const void *data, size_t len)
495{
496 struct mipi_dsi_msg msg = {
497 .channel = dsi->channel,
498 .tx_buf = data,
499 .tx_len = len
500 };
501
502 switch (len) {
503 case 0:
504 return -EINVAL;
505
506 case 1:
507 msg.type = MIPI_DSI_DCS_SHORT_WRITE;
508 break;
509
510 case 2:
511 msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
512 break;
513
514 default:
515 msg.type = MIPI_DSI_DCS_LONG_WRITE;
516 break;
517 }
518
519 return mipi_dsi_device_transfer(dsi, &msg);
520}
521EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
537 const void *data, size_t len)
538{
539 ssize_t err;
540 size_t size;
541 u8 *tx;
542
543 if (len > 0) {
544 size = 1 + len;
545
546 tx = kmalloc(size, GFP_KERNEL);
547 if (!tx)
548 return -ENOMEM;
549
550
551 tx[0] = cmd;
552 memcpy(&tx[1], data, len);
553 } else {
554 tx = &cmd;
555 size = 1;
556 }
557
558 err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
559
560 if (len > 0)
561 kfree(tx);
562
563 return err;
564}
565EXPORT_SYMBOL(mipi_dsi_dcs_write);
566
567
568
569
570
571
572
573
574
575
576ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
577 size_t len)
578{
579 struct mipi_dsi_msg msg = {
580 .channel = dsi->channel,
581 .type = MIPI_DSI_DCS_READ,
582 .tx_buf = &cmd,
583 .tx_len = 1,
584 .rx_buf = data,
585 .rx_len = len
586 };
587
588 return mipi_dsi_device_transfer(dsi, &msg);
589}
590EXPORT_SYMBOL(mipi_dsi_dcs_read);
591
592
593
594
595
596
597
598int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
599{
600 ssize_t err;
601
602 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
603 if (err < 0)
604 return err;
605
606 return 0;
607}
608EXPORT_SYMBOL(mipi_dsi_dcs_nop);
609
610
611
612
613
614
615
616int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
617{
618 ssize_t err;
619
620 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
621 if (err < 0)
622 return err;
623
624 return 0;
625}
626EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
627
628
629
630
631
632
633
634
635
636int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
637{
638 ssize_t err;
639
640 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
641 sizeof(*mode));
642 if (err <= 0) {
643 if (err == 0)
644 err = -ENODATA;
645
646 return err;
647 }
648
649 return 0;
650}
651EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
652
653
654
655
656
657
658
659
660
661int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
662{
663 ssize_t err;
664
665 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
666 sizeof(*format));
667 if (err <= 0) {
668 if (err == 0)
669 err = -ENODATA;
670
671 return err;
672 }
673
674 return 0;
675}
676EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
677
678
679
680
681
682
683
684
685int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
686{
687 ssize_t err;
688
689 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
690 if (err < 0)
691 return err;
692
693 return 0;
694}
695EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
696
697
698
699
700
701
702
703
704int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
705{
706 ssize_t err;
707
708 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
709 if (err < 0)
710 return err;
711
712 return 0;
713}
714EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
715
716
717
718
719
720
721
722
723int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
724{
725 ssize_t err;
726
727 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
728 if (err < 0)
729 return err;
730
731 return 0;
732}
733EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
734
735
736
737
738
739
740
741
742int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
743{
744 ssize_t err;
745
746 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
747 if (err < 0)
748 return err;
749
750 return 0;
751}
752EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
753
754
755
756
757
758
759
760
761
762
763int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
764 u16 end)
765{
766 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
767 ssize_t err;
768
769 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
770 sizeof(payload));
771 if (err < 0)
772 return err;
773
774 return 0;
775}
776EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
777
778
779
780
781
782
783
784
785
786
787int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
788 u16 end)
789{
790 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
791 ssize_t err;
792
793 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
794 sizeof(payload));
795 if (err < 0)
796 return err;
797
798 return 0;
799}
800EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
801
802
803
804
805
806
807
808
809int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
810{
811 ssize_t err;
812
813 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
814 if (err < 0)
815 return err;
816
817 return 0;
818}
819EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
820
821
822
823
824
825
826
827
828
829int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
830 enum mipi_dsi_dcs_tear_mode mode)
831{
832 u8 value = mode;
833 ssize_t err;
834
835 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
836 sizeof(value));
837 if (err < 0)
838 return err;
839
840 return 0;
841}
842EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
843
844
845
846
847
848
849
850
851
852int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
853{
854 ssize_t err;
855
856 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
857 sizeof(format));
858 if (err < 0)
859 return err;
860
861 return 0;
862}
863EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
864
865static int mipi_dsi_drv_probe(struct device *dev)
866{
867 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
868 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
869
870 return drv->probe(dsi);
871}
872
873static int mipi_dsi_drv_remove(struct device *dev)
874{
875 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
876 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
877
878 return drv->remove(dsi);
879}
880
881static void mipi_dsi_drv_shutdown(struct device *dev)
882{
883 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
884 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
885
886 drv->shutdown(dsi);
887}
888
889
890
891
892
893
894
895
896int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
897 struct module *owner)
898{
899 drv->driver.bus = &mipi_dsi_bus_type;
900 drv->driver.owner = owner;
901
902 if (drv->probe)
903 drv->driver.probe = mipi_dsi_drv_probe;
904 if (drv->remove)
905 drv->driver.remove = mipi_dsi_drv_remove;
906 if (drv->shutdown)
907 drv->driver.shutdown = mipi_dsi_drv_shutdown;
908
909 return driver_register(&drv->driver);
910}
911EXPORT_SYMBOL(mipi_dsi_driver_register_full);
912
913
914
915
916
917
918
919void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
920{
921 driver_unregister(&drv->driver);
922}
923EXPORT_SYMBOL(mipi_dsi_driver_unregister);
924
925static int __init mipi_dsi_bus_init(void)
926{
927 return bus_register(&mipi_dsi_bus_type);
928}
929postcore_initcall(mipi_dsi_bus_init);
930
931MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
932MODULE_DESCRIPTION("MIPI DSI Bus");
933MODULE_LICENSE("GPL and additional rights");
934