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_dbg(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_dbg(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_dbg(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_VIDEO_BURST;
101 
102 	ret = s6e63m0_probe(dev, s6e63m0_dsi_dcs_read, s6e63m0_dsi_dcs_write,
103 			    true);
104 	if (ret)
105 		return ret;
106 
107 	ret = mipi_dsi_attach(dsi);
108 	if (ret < 0)
109 		s6e63m0_remove(dev);
110 
111 	return ret;
112 }
113 
114 static int s6e63m0_dsi_remove(struct mipi_dsi_device *dsi)
115 {
116 	mipi_dsi_detach(dsi);
117 	return s6e63m0_remove(&dsi->dev);
118 }
119 
120 static const struct of_device_id s6e63m0_dsi_of_match[] = {
121 	{ .compatible = "samsung,s6e63m0" },
122 	{ /* sentinel */ }
123 };
124 MODULE_DEVICE_TABLE(of, s6e63m0_dsi_of_match);
125 
126 static struct mipi_dsi_driver s6e63m0_dsi_driver = {
127 	.probe			= s6e63m0_dsi_probe,
128 	.remove			= s6e63m0_dsi_remove,
129 	.driver			= {
130 		.name		= "panel-samsung-s6e63m0",
131 		.of_match_table = s6e63m0_dsi_of_match,
132 	},
133 };
134 module_mipi_dsi_driver(s6e63m0_dsi_driver);
135 
136 MODULE_AUTHOR("Linus Walleij <linusw@kernel.org>");
137 MODULE_DESCRIPTION("s6e63m0 LCD DSI Driver");
138 MODULE_LICENSE("GPL v2");
139