1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DSI interface to the Samsung S6E63M0 panel.
4  * (C) 2019 Linus Walleij
5  */
6 
7 #include <linux/module.h>
8 #include <linux/delay.h>
9 #include <linux/of_device.h>
10 
11 #include <drm/drm_mipi_dsi.h>
12 #include <drm/drm_print.h>
13 
14 #include "panel-samsung-s6e63m0.h"
15 
16 #define MCS_GLOBAL_PARAM	0xb0
17 #define S6E63M0_DSI_MAX_CHUNK	15 /* CMD + 15 bytes max */
18 
19 static int s6e63m0_dsi_dcs_read(struct device *dev, const u8 cmd, u8 *data)
20 {
21 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
22 	int ret;
23 
24 	ret = mipi_dsi_dcs_read(dsi, cmd, data, 1);
25 	if (ret < 0) {
26 		dev_err(dev, "could not read DCS CMD %02x\n", cmd);
27 		return ret;
28 	}
29 
30 	dev_info(dev, "DSI read CMD %02x = %02x\n", cmd, *data);
31 
32 	return 0;
33 }
34 
35 static int s6e63m0_dsi_dcs_write(struct device *dev, const u8 *data, size_t len)
36 {
37 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
38 	const u8 *seqp = data;
39 	u8 cmd;
40 	u8 cmdwritten;
41 	int remain;
42 	int chunk;
43 	int ret;
44 
45 	dev_info(dev, "DSI writing dcs seq: %*ph\n", (int)len, data);
46 
47 	/* Pick out and skip past the DCS command */
48 	cmd = *seqp;
49 	seqp++;
50 	cmdwritten = 0;
51 	remain = len - 1;
52 	chunk = remain;
53 
54 	/* Send max S6E63M0_DSI_MAX_CHUNK bytes at a time */
55 	if (chunk > S6E63M0_DSI_MAX_CHUNK)
56 		chunk = S6E63M0_DSI_MAX_CHUNK;
57 	ret = mipi_dsi_dcs_write(dsi, cmd, seqp, chunk);
58 	if (ret < 0) {
59 		dev_err(dev, "error sending DCS command seq cmd %02x\n", cmd);
60 		return ret;
61 	}
62 	cmdwritten += chunk;
63 	seqp += chunk;
64 
65 	while (cmdwritten < remain) {
66 		chunk = remain - cmdwritten;
67 		if (chunk > S6E63M0_DSI_MAX_CHUNK)
68 			chunk = S6E63M0_DSI_MAX_CHUNK;
69 		ret = mipi_dsi_dcs_write(dsi, MCS_GLOBAL_PARAM, &cmdwritten, 1);
70 		if (ret < 0) {
71 			dev_err(dev, "error sending CMD %02x global param %02x\n",
72 				cmd, cmdwritten);
73 			return ret;
74 		}
75 		ret = mipi_dsi_dcs_write(dsi, cmd, seqp, chunk);
76 		if (ret < 0) {
77 			dev_err(dev, "error sending CMD %02x chunk\n", cmd);
78 			return ret;
79 		}
80 		cmdwritten += chunk;
81 		seqp += chunk;
82 	}
83 	dev_info(dev, "sent command %02x %02x bytes\n", cmd, cmdwritten);
84 
85 	usleep_range(8000, 9000);
86 
87 	return 0;
88 }
89 
90 static int s6e63m0_dsi_probe(struct mipi_dsi_device *dsi)
91 {
92 	struct device *dev = &dsi->dev;
93 	int ret;
94 
95 	dsi->lanes = 2;
96 	dsi->format = MIPI_DSI_FMT_RGB888;
97 	dsi->hs_rate = 349440000;
98 	dsi->lp_rate = 9600000;
99 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
100 		MIPI_DSI_MODE_EOT_PACKET |
101 		MIPI_DSI_MODE_VIDEO_BURST;
102 
103 	ret = s6e63m0_probe(dev, s6e63m0_dsi_dcs_read, s6e63m0_dsi_dcs_write,
104 			    true);
105 	if (ret)
106 		return ret;
107 
108 	ret = mipi_dsi_attach(dsi);
109 	if (ret < 0)
110 		s6e63m0_remove(dev);
111 
112 	return ret;
113 }
114 
115 static int s6e63m0_dsi_remove(struct mipi_dsi_device *dsi)
116 {
117 	mipi_dsi_detach(dsi);
118 	return s6e63m0_remove(&dsi->dev);
119 }
120 
121 static const struct of_device_id s6e63m0_dsi_of_match[] = {
122 	{ .compatible = "samsung,s6e63m0" },
123 	{ /* sentinel */ }
124 };
125 MODULE_DEVICE_TABLE(of, s6e63m0_dsi_of_match);
126 
127 static struct mipi_dsi_driver s6e63m0_dsi_driver = {
128 	.probe			= s6e63m0_dsi_probe,
129 	.remove			= s6e63m0_dsi_remove,
130 	.driver			= {
131 		.name		= "panel-samsung-s6e63m0",
132 		.of_match_table = s6e63m0_dsi_of_match,
133 	},
134 };
135 module_mipi_dsi_driver(s6e63m0_dsi_driver);
136 
137 MODULE_AUTHOR("Linus Walleij <linusw@kernel.org>");
138 MODULE_DESCRIPTION("s6e63m0 LCD DSI Driver");
139 MODULE_LICENSE("GPL v2");
140