xref: /linux/drivers/gpu/drm/bridge/nxp-ptn3460.c (revision 98838d95075a5295f3478ceba18bcccf472e30f4)
1 /*
2  * NXP PTN3460 DP/LVDS bridge driver
3  *
4  * Copyright (C) 2013 Google, Inc.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #include <linux/delay.h>
17 #include <linux/gpio.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/of_gpio.h>
23 #include <linux/of_graph.h>
24 
25 #include <drm/drm_panel.h>
26 
27 #include "drm_crtc.h"
28 #include "drm_crtc_helper.h"
29 #include "drm_atomic_helper.h"
30 #include "drm_edid.h"
31 #include "drmP.h"
32 
33 #define PTN3460_EDID_ADDR			0x0
34 #define PTN3460_EDID_EMULATION_ADDR		0x84
35 #define PTN3460_EDID_ENABLE_EMULATION		0
36 #define PTN3460_EDID_EMULATION_SELECTION	1
37 #define PTN3460_EDID_SRAM_LOAD_ADDR		0x85
38 
39 struct ptn3460_bridge {
40 	struct drm_connector connector;
41 	struct i2c_client *client;
42 	struct drm_bridge bridge;
43 	struct edid *edid;
44 	struct drm_panel *panel;
45 	struct gpio_desc *gpio_pd_n;
46 	struct gpio_desc *gpio_rst_n;
47 	u32 edid_emulation;
48 	bool enabled;
49 };
50 
51 static inline struct ptn3460_bridge *
52 		bridge_to_ptn3460(struct drm_bridge *bridge)
53 {
54 	return container_of(bridge, struct ptn3460_bridge, bridge);
55 }
56 
57 static inline struct ptn3460_bridge *
58 		connector_to_ptn3460(struct drm_connector *connector)
59 {
60 	return container_of(connector, struct ptn3460_bridge, connector);
61 }
62 
63 static int ptn3460_read_bytes(struct ptn3460_bridge *ptn_bridge, char addr,
64 		u8 *buf, int len)
65 {
66 	int ret;
67 
68 	ret = i2c_master_send(ptn_bridge->client, &addr, 1);
69 	if (ret <= 0) {
70 		DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
71 		return ret;
72 	}
73 
74 	ret = i2c_master_recv(ptn_bridge->client, buf, len);
75 	if (ret <= 0) {
76 		DRM_ERROR("Failed to recv i2c data, ret=%d\n", ret);
77 		return ret;
78 	}
79 
80 	return 0;
81 }
82 
83 static int ptn3460_write_byte(struct ptn3460_bridge *ptn_bridge, char addr,
84 		char val)
85 {
86 	int ret;
87 	char buf[2];
88 
89 	buf[0] = addr;
90 	buf[1] = val;
91 
92 	ret = i2c_master_send(ptn_bridge->client, buf, ARRAY_SIZE(buf));
93 	if (ret <= 0) {
94 		DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
95 		return ret;
96 	}
97 
98 	return 0;
99 }
100 
101 static int ptn3460_select_edid(struct ptn3460_bridge *ptn_bridge)
102 {
103 	int ret;
104 	char val;
105 
106 	/* Load the selected edid into SRAM (accessed at PTN3460_EDID_ADDR) */
107 	ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_SRAM_LOAD_ADDR,
108 			ptn_bridge->edid_emulation);
109 	if (ret) {
110 		DRM_ERROR("Failed to transfer EDID to sram, ret=%d\n", ret);
111 		return ret;
112 	}
113 
114 	/* Enable EDID emulation and select the desired EDID */
115 	val = 1 << PTN3460_EDID_ENABLE_EMULATION |
116 		ptn_bridge->edid_emulation << PTN3460_EDID_EMULATION_SELECTION;
117 
118 	ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_EMULATION_ADDR, val);
119 	if (ret) {
120 		DRM_ERROR("Failed to write EDID value, ret=%d\n", ret);
121 		return ret;
122 	}
123 
124 	return 0;
125 }
126 
127 static void ptn3460_pre_enable(struct drm_bridge *bridge)
128 {
129 	struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
130 	int ret;
131 
132 	if (ptn_bridge->enabled)
133 		return;
134 
135 	gpiod_set_value(ptn_bridge->gpio_pd_n, 1);
136 
137 	gpiod_set_value(ptn_bridge->gpio_rst_n, 0);
138 	usleep_range(10, 20);
139 	gpiod_set_value(ptn_bridge->gpio_rst_n, 1);
140 
141 	if (drm_panel_prepare(ptn_bridge->panel)) {
142 		DRM_ERROR("failed to prepare panel\n");
143 		return;
144 	}
145 
146 	/*
147 	 * There's a bug in the PTN chip where it falsely asserts hotplug before
148 	 * it is fully functional. We're forced to wait for the maximum start up
149 	 * time specified in the chip's datasheet to make sure we're really up.
150 	 */
151 	msleep(90);
152 
153 	ret = ptn3460_select_edid(ptn_bridge);
154 	if (ret)
155 		DRM_ERROR("Select EDID failed ret=%d\n", ret);
156 
157 	ptn_bridge->enabled = true;
158 }
159 
160 static void ptn3460_enable(struct drm_bridge *bridge)
161 {
162 	struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
163 
164 	if (drm_panel_enable(ptn_bridge->panel)) {
165 		DRM_ERROR("failed to enable panel\n");
166 		return;
167 	}
168 }
169 
170 static void ptn3460_disable(struct drm_bridge *bridge)
171 {
172 	struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
173 
174 	if (!ptn_bridge->enabled)
175 		return;
176 
177 	ptn_bridge->enabled = false;
178 
179 	if (drm_panel_disable(ptn_bridge->panel)) {
180 		DRM_ERROR("failed to disable panel\n");
181 		return;
182 	}
183 
184 	gpiod_set_value(ptn_bridge->gpio_rst_n, 1);
185 	gpiod_set_value(ptn_bridge->gpio_pd_n, 0);
186 }
187 
188 static void ptn3460_post_disable(struct drm_bridge *bridge)
189 {
190 	struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
191 
192 	if (drm_panel_unprepare(ptn_bridge->panel)) {
193 		DRM_ERROR("failed to unprepare panel\n");
194 		return;
195 	}
196 }
197 
198 static int ptn3460_get_modes(struct drm_connector *connector)
199 {
200 	struct ptn3460_bridge *ptn_bridge;
201 	u8 *edid;
202 	int ret, num_modes = 0;
203 	bool power_off;
204 
205 	ptn_bridge = connector_to_ptn3460(connector);
206 
207 	if (ptn_bridge->edid)
208 		return drm_add_edid_modes(connector, ptn_bridge->edid);
209 
210 	power_off = !ptn_bridge->enabled;
211 	ptn3460_pre_enable(&ptn_bridge->bridge);
212 
213 	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
214 	if (!edid) {
215 		DRM_ERROR("Failed to allocate EDID\n");
216 		return 0;
217 	}
218 
219 	ret = ptn3460_read_bytes(ptn_bridge, PTN3460_EDID_ADDR, edid,
220 			EDID_LENGTH);
221 	if (ret) {
222 		kfree(edid);
223 		goto out;
224 	}
225 
226 	ptn_bridge->edid = (struct edid *)edid;
227 	drm_mode_connector_update_edid_property(connector, ptn_bridge->edid);
228 
229 	num_modes = drm_add_edid_modes(connector, ptn_bridge->edid);
230 
231 out:
232 	if (power_off)
233 		ptn3460_disable(&ptn_bridge->bridge);
234 
235 	return num_modes;
236 }
237 
238 static const struct drm_connector_helper_funcs ptn3460_connector_helper_funcs = {
239 	.get_modes = ptn3460_get_modes,
240 };
241 
242 static enum drm_connector_status ptn3460_detect(struct drm_connector *connector,
243 		bool force)
244 {
245 	return connector_status_connected;
246 }
247 
248 static const struct drm_connector_funcs ptn3460_connector_funcs = {
249 	.dpms = drm_atomic_helper_connector_dpms,
250 	.fill_modes = drm_helper_probe_single_connector_modes,
251 	.detect = ptn3460_detect,
252 	.destroy = drm_connector_cleanup,
253 	.reset = drm_atomic_helper_connector_reset,
254 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
255 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
256 };
257 
258 static int ptn3460_bridge_attach(struct drm_bridge *bridge)
259 {
260 	struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
261 	int ret;
262 
263 	if (!bridge->encoder) {
264 		DRM_ERROR("Parent encoder object not found");
265 		return -ENODEV;
266 	}
267 
268 	ptn_bridge->connector.polled = DRM_CONNECTOR_POLL_HPD;
269 	ret = drm_connector_init(bridge->dev, &ptn_bridge->connector,
270 			&ptn3460_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
271 	if (ret) {
272 		DRM_ERROR("Failed to initialize connector with drm\n");
273 		return ret;
274 	}
275 	drm_connector_helper_add(&ptn_bridge->connector,
276 					&ptn3460_connector_helper_funcs);
277 	drm_connector_register(&ptn_bridge->connector);
278 	drm_mode_connector_attach_encoder(&ptn_bridge->connector,
279 							bridge->encoder);
280 
281 	if (ptn_bridge->panel)
282 		drm_panel_attach(ptn_bridge->panel, &ptn_bridge->connector);
283 
284 	drm_helper_hpd_irq_event(ptn_bridge->connector.dev);
285 
286 	return ret;
287 }
288 
289 static const struct drm_bridge_funcs ptn3460_bridge_funcs = {
290 	.pre_enable = ptn3460_pre_enable,
291 	.enable = ptn3460_enable,
292 	.disable = ptn3460_disable,
293 	.post_disable = ptn3460_post_disable,
294 	.attach = ptn3460_bridge_attach,
295 };
296 
297 static int ptn3460_probe(struct i2c_client *client,
298 				const struct i2c_device_id *id)
299 {
300 	struct device *dev = &client->dev;
301 	struct ptn3460_bridge *ptn_bridge;
302 	struct device_node *endpoint, *panel_node;
303 	int ret;
304 
305 	ptn_bridge = devm_kzalloc(dev, sizeof(*ptn_bridge), GFP_KERNEL);
306 	if (!ptn_bridge) {
307 		return -ENOMEM;
308 	}
309 
310 	endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
311 	if (endpoint) {
312 		panel_node = of_graph_get_remote_port_parent(endpoint);
313 		if (panel_node) {
314 			ptn_bridge->panel = of_drm_find_panel(panel_node);
315 			of_node_put(panel_node);
316 			if (!ptn_bridge->panel)
317 				return -EPROBE_DEFER;
318 		}
319 	}
320 
321 	ptn_bridge->client = client;
322 
323 	ptn_bridge->gpio_pd_n = devm_gpiod_get(&client->dev, "powerdown",
324 					       GPIOD_OUT_HIGH);
325 	if (IS_ERR(ptn_bridge->gpio_pd_n)) {
326 		ret = PTR_ERR(ptn_bridge->gpio_pd_n);
327 		dev_err(dev, "cannot get gpio_pd_n %d\n", ret);
328 		return ret;
329 	}
330 
331 	/*
332 	 * Request the reset pin low to avoid the bridge being
333 	 * initialized prematurely
334 	 */
335 	ptn_bridge->gpio_rst_n = devm_gpiod_get(&client->dev, "reset",
336 						GPIOD_OUT_LOW);
337 	if (IS_ERR(ptn_bridge->gpio_rst_n)) {
338 		ret = PTR_ERR(ptn_bridge->gpio_rst_n);
339 		DRM_ERROR("cannot get gpio_rst_n %d\n", ret);
340 		return ret;
341 	}
342 
343 	ret = of_property_read_u32(dev->of_node, "edid-emulation",
344 			&ptn_bridge->edid_emulation);
345 	if (ret) {
346 		dev_err(dev, "Can't read EDID emulation value\n");
347 		return ret;
348 	}
349 
350 	ptn_bridge->bridge.funcs = &ptn3460_bridge_funcs;
351 	ptn_bridge->bridge.of_node = dev->of_node;
352 	ret = drm_bridge_add(&ptn_bridge->bridge);
353 	if (ret) {
354 		DRM_ERROR("Failed to add bridge\n");
355 		return ret;
356 	}
357 
358 	i2c_set_clientdata(client, ptn_bridge);
359 
360 	return 0;
361 }
362 
363 static int ptn3460_remove(struct i2c_client *client)
364 {
365 	struct ptn3460_bridge *ptn_bridge = i2c_get_clientdata(client);
366 
367 	drm_bridge_remove(&ptn_bridge->bridge);
368 
369 	return 0;
370 }
371 
372 static const struct i2c_device_id ptn3460_i2c_table[] = {
373 	{"ptn3460", 0},
374 	{},
375 };
376 MODULE_DEVICE_TABLE(i2c, ptn3460_i2c_table);
377 
378 static const struct of_device_id ptn3460_match[] = {
379 	{ .compatible = "nxp,ptn3460" },
380 	{},
381 };
382 MODULE_DEVICE_TABLE(of, ptn3460_match);
383 
384 static struct i2c_driver ptn3460_driver = {
385 	.id_table	= ptn3460_i2c_table,
386 	.probe		= ptn3460_probe,
387 	.remove		= ptn3460_remove,
388 	.driver		= {
389 		.name	= "nxp,ptn3460",
390 		.of_match_table = ptn3460_match,
391 	},
392 };
393 module_i2c_driver(ptn3460_driver);
394 
395 MODULE_AUTHOR("Sean Paul <seanpaul@chromium.org>");
396 MODULE_DESCRIPTION("NXP ptn3460 eDP-LVDS converter driver");
397 MODULE_LICENSE("GPL v2");
398