1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2012 Samsung Electronics
4  *
5  * Author: InKi Dae <inki.dae@samsung.com>
6  * Author: Donghwa Lee <dh09.lee@samsung.com>
7  */
8 
9 #include <common.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <fdtdec.h>
13 #include <asm/global_data.h>
14 #include <dm/devres.h>
15 #include <linux/libfdt.h>
16 #include <linux/compat.h>
17 #include <linux/err.h>
18 #include <asm/arch/dsim.h>
19 #include <asm/arch/mipi_dsim.h>
20 #include <asm/arch/power.h>
21 #include <asm/arch/cpu.h>
22 #include <asm/arch/clk.h>
23 
24 #include "exynos_mipi_dsi_lowlevel.h"
25 #include "exynos_mipi_dsi_common.h"
26 
27 #define master_to_driver(a)	(a->dsim_lcd_drv)
28 #define master_to_device(a)	(a->dsim_lcd_dev)
29 
30 DECLARE_GLOBAL_DATA_PTR;
31 
32 struct mipi_dsim_ddi {
33 	int				bus_id;
34 	struct list_head		list;
35 	struct mipi_dsim_lcd_device	*dsim_lcd_dev;
36 	struct mipi_dsim_lcd_driver	*dsim_lcd_drv;
37 };
38 
39 static LIST_HEAD(dsim_ddi_list);
40 static LIST_HEAD(dsim_lcd_dev_list);
41 
exynos_mipi_dsi_register_lcd_device(struct mipi_dsim_lcd_device * lcd_dev)42 int exynos_mipi_dsi_register_lcd_device(struct mipi_dsim_lcd_device *lcd_dev)
43 {
44 	struct mipi_dsim_ddi *dsim_ddi;
45 
46 	if (!lcd_dev) {
47 		debug("mipi_dsim_lcd_device is NULL.\n");
48 		return -EFAULT;
49 	}
50 
51 	if (!lcd_dev->name) {
52 		debug("dsim_lcd_device name is NULL.\n");
53 		return -EFAULT;
54 	}
55 
56 	dsim_ddi = kzalloc(sizeof(struct mipi_dsim_ddi), GFP_KERNEL);
57 	if (!dsim_ddi) {
58 		debug("failed to allocate dsim_ddi object.\n");
59 		return -EFAULT;
60 	}
61 
62 	dsim_ddi->dsim_lcd_dev = lcd_dev;
63 
64 	list_add_tail(&dsim_ddi->list, &dsim_ddi_list);
65 
66 	return 0;
67 }
68 
69 struct mipi_dsim_ddi
exynos_mipi_dsi_find_lcd_device(struct mipi_dsim_lcd_driver * lcd_drv)70 	*exynos_mipi_dsi_find_lcd_device(struct mipi_dsim_lcd_driver *lcd_drv)
71 {
72 	struct mipi_dsim_ddi *dsim_ddi;
73 	struct mipi_dsim_lcd_device *lcd_dev;
74 
75 	list_for_each_entry(dsim_ddi, &dsim_ddi_list, list) {
76 		lcd_dev = dsim_ddi->dsim_lcd_dev;
77 		if (!lcd_dev)
78 			continue;
79 
80 		if (lcd_drv->id >= 0) {
81 			if ((strcmp(lcd_drv->name, lcd_dev->name)) == 0 &&
82 					lcd_drv->id == lcd_dev->id) {
83 				/**
84 				 * bus_id would be used to identify
85 				 * connected bus.
86 				 */
87 				dsim_ddi->bus_id = lcd_dev->bus_id;
88 
89 				return dsim_ddi;
90 			}
91 		} else {
92 			if ((strcmp(lcd_drv->name, lcd_dev->name)) == 0) {
93 				/**
94 				 * bus_id would be used to identify
95 				 * connected bus.
96 				 */
97 				dsim_ddi->bus_id = lcd_dev->bus_id;
98 
99 				return dsim_ddi;
100 			}
101 		}
102 
103 		kfree(dsim_ddi);
104 		list_del(&dsim_ddi_list);
105 	}
106 
107 	return NULL;
108 }
109 
exynos_mipi_dsi_register_lcd_driver(struct mipi_dsim_lcd_driver * lcd_drv)110 int exynos_mipi_dsi_register_lcd_driver(struct mipi_dsim_lcd_driver *lcd_drv)
111 {
112 	struct mipi_dsim_ddi *dsim_ddi;
113 
114 	if (!lcd_drv) {
115 		debug("mipi_dsim_lcd_driver is NULL.\n");
116 		return -EFAULT;
117 	}
118 
119 	if (!lcd_drv->name) {
120 		debug("dsim_lcd_driver name is NULL.\n");
121 		return -EFAULT;
122 	}
123 
124 	dsim_ddi = exynos_mipi_dsi_find_lcd_device(lcd_drv);
125 	if (!dsim_ddi) {
126 		debug("mipi_dsim_ddi object not found.\n");
127 		return -EFAULT;
128 	}
129 
130 	dsim_ddi->dsim_lcd_drv = lcd_drv;
131 
132 	debug("registered panel driver(%s) to mipi-dsi driver.\n",
133 		lcd_drv->name);
134 
135 	return 0;
136 
137 }
138 
139 struct mipi_dsim_ddi
exynos_mipi_dsi_bind_lcd_ddi(struct mipi_dsim_device * dsim,const char * name)140 	*exynos_mipi_dsi_bind_lcd_ddi(struct mipi_dsim_device *dsim,
141 			const char *name)
142 {
143 	struct mipi_dsim_ddi *dsim_ddi;
144 	struct mipi_dsim_lcd_driver *lcd_drv;
145 	struct mipi_dsim_lcd_device *lcd_dev;
146 
147 	list_for_each_entry(dsim_ddi, &dsim_ddi_list, list) {
148 		lcd_drv = dsim_ddi->dsim_lcd_drv;
149 		lcd_dev = dsim_ddi->dsim_lcd_dev;
150 		if (!lcd_drv || !lcd_dev)
151 			continue;
152 
153 		debug("lcd_drv->id = %d, lcd_dev->id = %d\n",
154 					lcd_drv->id, lcd_dev->id);
155 
156 		if ((strcmp(lcd_drv->name, name) == 0)) {
157 			lcd_dev->master = dsim;
158 
159 			dsim->dsim_lcd_dev = lcd_dev;
160 			dsim->dsim_lcd_drv = lcd_drv;
161 
162 			return dsim_ddi;
163 		}
164 	}
165 
166 	return NULL;
167 }
168 
169 /* define MIPI-DSI Master operations. */
170 static struct mipi_dsim_master_ops master_ops = {
171 	.cmd_write			= exynos_mipi_dsi_wr_data,
172 	.get_dsim_frame_done		= exynos_mipi_dsi_get_frame_done_status,
173 	.clear_dsim_frame_done		= exynos_mipi_dsi_clear_frame_done,
174 };
175 
exynos_mipi_dsi_init(struct exynos_platform_mipi_dsim * dsim_pd)176 int exynos_mipi_dsi_init(struct exynos_platform_mipi_dsim *dsim_pd)
177 {
178 	struct mipi_dsim_device *dsim;
179 	struct mipi_dsim_config *dsim_config;
180 	struct mipi_dsim_ddi *dsim_ddi;
181 
182 	dsim = kzalloc(sizeof(struct mipi_dsim_device), GFP_KERNEL);
183 	if (!dsim) {
184 		debug("failed to allocate dsim object.\n");
185 		return -EFAULT;
186 	}
187 
188 	/* get mipi_dsim_config. */
189 	dsim_config = dsim_pd->dsim_config;
190 	if (dsim_config == NULL) {
191 		debug("failed to get dsim config data.\n");
192 		return -EFAULT;
193 	}
194 
195 	dsim->pd = dsim_pd;
196 	dsim->dsim_config = dsim_config;
197 	dsim->master_ops = &master_ops;
198 
199 	/* bind lcd ddi matched with panel name. */
200 	dsim_ddi = exynos_mipi_dsi_bind_lcd_ddi(dsim, dsim_pd->lcd_panel_name);
201 	if (!dsim_ddi) {
202 		debug("mipi_dsim_ddi object not found.\n");
203 		return -ENOSYS;
204 	}
205 	if (dsim_pd->lcd_power)
206 		dsim_pd->lcd_power();
207 
208 	if (dsim_pd->mipi_power)
209 		dsim_pd->mipi_power();
210 
211 	/* phy_enable(unsigned int dev_index, unsigned int enable) */
212 	if (dsim_pd->phy_enable)
213 		dsim_pd->phy_enable(0, 1);
214 
215 	set_mipi_clk();
216 
217 	exynos_mipi_dsi_init_dsim(dsim);
218 	exynos_mipi_dsi_init_link(dsim);
219 	exynos_mipi_dsi_set_hs_enable(dsim);
220 
221 	/* set display timing. */
222 	exynos_mipi_dsi_set_display_mode(dsim, dsim->dsim_config);
223 
224 	/* initialize mipi-dsi client(lcd panel). */
225 	if (dsim_ddi->dsim_lcd_drv && dsim_ddi->dsim_lcd_drv->mipi_panel_init) {
226 		dsim_ddi->dsim_lcd_drv->mipi_panel_init(dsim);
227 		dsim_ddi->dsim_lcd_drv->mipi_display_on(dsim);
228 	}
229 
230 	debug("mipi-dsi driver(%s mode) has been probed.\n",
231 		(dsim_config->e_interface == DSIM_COMMAND) ?
232 			"CPU" : "RGB");
233 
234 	return 0;
235 }
236 
exynos_dsim_config_parse_dt(const void * blob,struct mipi_dsim_config * dt,struct mipi_dsim_lcd_device * lcd_dt)237 int exynos_dsim_config_parse_dt(const void *blob, struct mipi_dsim_config *dt,
238 				struct mipi_dsim_lcd_device *lcd_dt)
239 {
240 	int node;
241 
242 	node = fdtdec_next_compatible(blob, 0, COMPAT_SAMSUNG_EXYNOS_MIPI_DSI);
243 	if (node <= 0) {
244 		printf("exynos_mipi_dsi: Can't get device node for mipi dsi\n");
245 		return -ENODEV;
246 	}
247 
248 	dt->e_interface = fdtdec_get_int(blob, node,
249 				"samsung,dsim-config-e-interface", 0);
250 
251 	dt->e_virtual_ch = fdtdec_get_int(blob, node,
252 				"samsung,dsim-config-e-virtual-ch", 0);
253 
254 	dt->e_pixel_format = fdtdec_get_int(blob, node,
255 				"samsung,dsim-config-e-pixel-format", 0);
256 
257 	dt->e_burst_mode = fdtdec_get_int(blob, node,
258 				"samsung,dsim-config-e-burst-mode", 0);
259 
260 	dt->e_no_data_lane = fdtdec_get_int(blob, node,
261 				"samsung,dsim-config-e-no-data-lane", 0);
262 
263 	dt->e_byte_clk = fdtdec_get_int(blob, node,
264 				"samsung,dsim-config-e-byte-clk", 0);
265 
266 	dt->hfp = fdtdec_get_int(blob, node,
267 				"samsung,dsim-config-hfp", 0);
268 
269 	dt->p = fdtdec_get_int(blob, node,
270 					  "samsung,dsim-config-p", 0);
271 	dt->m = fdtdec_get_int(blob, node,
272 					  "samsung,dsim-config-m", 0);
273 	dt->s = fdtdec_get_int(blob, node,
274 					  "samsung,dsim-config-s", 0);
275 
276 	dt->pll_stable_time = fdtdec_get_int(blob, node,
277 				"samsung,dsim-config-pll-stable-time", 0);
278 
279 	dt->esc_clk = fdtdec_get_int(blob, node,
280 				"samsung,dsim-config-esc-clk", 0);
281 
282 	dt->stop_holding_cnt = fdtdec_get_int(blob, node,
283 				"samsung,dsim-config-stop-holding-cnt", 0);
284 
285 	dt->bta_timeout = fdtdec_get_int(blob, node,
286 				"samsung,dsim-config-bta-timeout", 0);
287 
288 	dt->rx_timeout = fdtdec_get_int(blob, node,
289 				"samsung,dsim-config-rx-timeout", 0);
290 
291 	lcd_dt->name = fdtdec_get_config_string(blob,
292 				"samsung,dsim-device-name");
293 
294 	lcd_dt->id = fdtdec_get_int(blob, node,
295 				"samsung,dsim-device-id", 0);
296 
297 	lcd_dt->bus_id = fdtdec_get_int(blob, node,
298 				"samsung,dsim-device-bus_id", 0);
299 
300 	lcd_dt->reverse_panel = fdtdec_get_int(blob, node,
301 				"samsung,dsim-device-reverse-panel", 0);
302 
303 	return 0;
304 }
305 
exynos_init_dsim_platform_data(vidinfo_t * vid)306 void exynos_init_dsim_platform_data(vidinfo_t *vid)
307 {
308 	static struct mipi_dsim_config dsim_config_dt;
309 	static struct exynos_platform_mipi_dsim dsim_platform_data_dt;
310 	static struct mipi_dsim_lcd_device mipi_lcd_device_dt;
311 
312 	if (exynos_dsim_config_parse_dt(gd->fdt_blob, &dsim_config_dt,
313 					&mipi_lcd_device_dt))
314 		debug("Can't get proper dsim config.\n");
315 
316 	strcpy(dsim_platform_data_dt.lcd_panel_name, mipi_lcd_device_dt.name);
317 	dsim_platform_data_dt.dsim_config = &dsim_config_dt;
318 	dsim_platform_data_dt.mipi_power = mipi_power;
319 	dsim_platform_data_dt.phy_enable = set_mipi_phy_ctrl;
320 	dsim_platform_data_dt.lcd_panel_info = (void *)vid;
321 
322 	mipi_lcd_device_dt.platform_data = (void *)&dsim_platform_data_dt;
323 	exynos_mipi_dsi_register_lcd_device(&mipi_lcd_device_dt);
324 
325 	vid->dsim_platform_data_dt = &dsim_platform_data_dt;
326 }
327