xref: /linux/drivers/gpu/drm/bridge/nxp-ptn3460.c (revision a06c3fad49a50d5d5eb078f93e70f4d3eca5d5a5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * NXP PTN3460 DP/LVDS bridge driver
4  *
5  * Copyright (C) 2013 Google, Inc.
6  */
7 
8 #include <linux/delay.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_bridge.h>
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_edid.h>
17 #include <drm/drm_of.h>
18 #include <drm/drm_panel.h>
19 #include <drm/drm_print.h>
20 #include <drm/drm_probe_helper.h>
21 
22 #define PTN3460_EDID_ADDR			0x0
23 #define PTN3460_EDID_EMULATION_ADDR		0x84
24 #define PTN3460_EDID_ENABLE_EMULATION		0
25 #define PTN3460_EDID_EMULATION_SELECTION	1
26 #define PTN3460_EDID_SRAM_LOAD_ADDR		0x85
27 
28 struct ptn3460_bridge {
29 	struct drm_connector connector;
30 	struct i2c_client *client;
31 	struct drm_bridge bridge;
32 	struct drm_bridge *panel_bridge;
33 	struct gpio_desc *gpio_pd_n;
34 	struct gpio_desc *gpio_rst_n;
35 	u32 edid_emulation;
36 	bool enabled;
37 };
38 
39 static inline struct ptn3460_bridge *
40 		bridge_to_ptn3460(struct drm_bridge *bridge)
41 {
42 	return container_of(bridge, struct ptn3460_bridge, bridge);
43 }
44 
45 static inline struct ptn3460_bridge *
46 		connector_to_ptn3460(struct drm_connector *connector)
47 {
48 	return container_of(connector, struct ptn3460_bridge, connector);
49 }
50 
51 static int ptn3460_read_bytes(struct ptn3460_bridge *ptn_bridge, char addr,
52 		u8 *buf, int len)
53 {
54 	int ret;
55 
56 	ret = i2c_master_send(ptn_bridge->client, &addr, 1);
57 	if (ret < 0) {
58 		DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
59 		return ret;
60 	}
61 
62 	ret = i2c_master_recv(ptn_bridge->client, buf, len);
63 	if (ret < 0) {
64 		DRM_ERROR("Failed to recv i2c data, ret=%d\n", ret);
65 		return ret;
66 	}
67 
68 	return 0;
69 }
70 
71 static int ptn3460_write_byte(struct ptn3460_bridge *ptn_bridge, char addr,
72 		char val)
73 {
74 	int ret;
75 	char buf[2];
76 
77 	buf[0] = addr;
78 	buf[1] = val;
79 
80 	ret = i2c_master_send(ptn_bridge->client, buf, ARRAY_SIZE(buf));
81 	if (ret < 0) {
82 		DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
83 		return ret;
84 	}
85 
86 	return 0;
87 }
88 
89 static int ptn3460_select_edid(struct ptn3460_bridge *ptn_bridge)
90 {
91 	int ret;
92 	char val;
93 
94 	/* Load the selected edid into SRAM (accessed at PTN3460_EDID_ADDR) */
95 	ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_SRAM_LOAD_ADDR,
96 			ptn_bridge->edid_emulation);
97 	if (ret) {
98 		DRM_ERROR("Failed to transfer EDID to sram, ret=%d\n", ret);
99 		return ret;
100 	}
101 
102 	/* Enable EDID emulation and select the desired EDID */
103 	val = 1 << PTN3460_EDID_ENABLE_EMULATION |
104 		ptn_bridge->edid_emulation << PTN3460_EDID_EMULATION_SELECTION;
105 
106 	ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_EMULATION_ADDR, val);
107 	if (ret) {
108 		DRM_ERROR("Failed to write EDID value, ret=%d\n", ret);
109 		return ret;
110 	}
111 
112 	return 0;
113 }
114 
115 static void ptn3460_pre_enable(struct drm_bridge *bridge)
116 {
117 	struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
118 	int ret;
119 
120 	if (ptn_bridge->enabled)
121 		return;
122 
123 	gpiod_set_value(ptn_bridge->gpio_pd_n, 1);
124 
125 	gpiod_set_value(ptn_bridge->gpio_rst_n, 0);
126 	usleep_range(10, 20);
127 	gpiod_set_value(ptn_bridge->gpio_rst_n, 1);
128 
129 	/*
130 	 * There's a bug in the PTN chip where it falsely asserts hotplug before
131 	 * it is fully functional. We're forced to wait for the maximum start up
132 	 * time specified in the chip's datasheet to make sure we're really up.
133 	 */
134 	msleep(90);
135 
136 	ret = ptn3460_select_edid(ptn_bridge);
137 	if (ret)
138 		DRM_ERROR("Select EDID failed ret=%d\n", ret);
139 
140 	ptn_bridge->enabled = true;
141 }
142 
143 static void ptn3460_disable(struct drm_bridge *bridge)
144 {
145 	struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
146 
147 	if (!ptn_bridge->enabled)
148 		return;
149 
150 	ptn_bridge->enabled = false;
151 
152 	gpiod_set_value(ptn_bridge->gpio_rst_n, 1);
153 	gpiod_set_value(ptn_bridge->gpio_pd_n, 0);
154 }
155 
156 
157 static const struct drm_edid *ptn3460_edid_read(struct drm_bridge *bridge,
158 						struct drm_connector *connector)
159 {
160 	struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
161 	const struct drm_edid *drm_edid = NULL;
162 	bool power_off;
163 	u8 *edid;
164 	int ret;
165 
166 	power_off = !ptn_bridge->enabled;
167 	ptn3460_pre_enable(&ptn_bridge->bridge);
168 
169 	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
170 	if (!edid) {
171 		DRM_ERROR("Failed to allocate EDID\n");
172 		goto out;
173 	}
174 
175 	ret = ptn3460_read_bytes(ptn_bridge, PTN3460_EDID_ADDR, edid,
176 				 EDID_LENGTH);
177 	if (ret) {
178 		kfree(edid);
179 		goto out;
180 	}
181 
182 	drm_edid = drm_edid_alloc(edid, EDID_LENGTH);
183 
184 out:
185 	if (power_off)
186 		ptn3460_disable(&ptn_bridge->bridge);
187 
188 	return drm_edid;
189 }
190 
191 static int ptn3460_connector_get_modes(struct drm_connector *connector)
192 {
193 	struct ptn3460_bridge *ptn_bridge = connector_to_ptn3460(connector);
194 	const struct drm_edid *drm_edid;
195 	int num_modes;
196 
197 	drm_edid = ptn3460_edid_read(&ptn_bridge->bridge, connector);
198 	drm_edid_connector_update(connector, drm_edid);
199 	num_modes = drm_edid_connector_add_modes(connector);
200 	drm_edid_free(drm_edid);
201 
202 	return num_modes;
203 }
204 
205 static const struct drm_connector_helper_funcs ptn3460_connector_helper_funcs = {
206 	.get_modes = ptn3460_connector_get_modes,
207 };
208 
209 static const struct drm_connector_funcs ptn3460_connector_funcs = {
210 	.fill_modes = drm_helper_probe_single_connector_modes,
211 	.destroy = drm_connector_cleanup,
212 	.reset = drm_atomic_helper_connector_reset,
213 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
214 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
215 };
216 
217 static int ptn3460_bridge_attach(struct drm_bridge *bridge,
218 				 enum drm_bridge_attach_flags flags)
219 {
220 	struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
221 	int ret;
222 
223 	/* Let this driver create connector if requested */
224 	ret = drm_bridge_attach(bridge->encoder, ptn_bridge->panel_bridge,
225 				bridge, flags | DRM_BRIDGE_ATTACH_NO_CONNECTOR);
226 	if (ret < 0)
227 		return ret;
228 
229 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
230 		return 0;
231 
232 	ptn_bridge->connector.polled = DRM_CONNECTOR_POLL_HPD;
233 	ret = drm_connector_init(bridge->dev, &ptn_bridge->connector,
234 			&ptn3460_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
235 	if (ret) {
236 		DRM_ERROR("Failed to initialize connector with drm\n");
237 		return ret;
238 	}
239 	drm_connector_helper_add(&ptn_bridge->connector,
240 					&ptn3460_connector_helper_funcs);
241 	drm_connector_register(&ptn_bridge->connector);
242 	drm_connector_attach_encoder(&ptn_bridge->connector,
243 							bridge->encoder);
244 
245 	drm_helper_hpd_irq_event(ptn_bridge->connector.dev);
246 
247 	return ret;
248 }
249 
250 static const struct drm_bridge_funcs ptn3460_bridge_funcs = {
251 	.pre_enable = ptn3460_pre_enable,
252 	.disable = ptn3460_disable,
253 	.attach = ptn3460_bridge_attach,
254 	.edid_read = ptn3460_edid_read,
255 };
256 
257 static int ptn3460_probe(struct i2c_client *client)
258 {
259 	struct device *dev = &client->dev;
260 	struct ptn3460_bridge *ptn_bridge;
261 	struct drm_bridge *panel_bridge;
262 	int ret;
263 
264 	ptn_bridge = devm_kzalloc(dev, sizeof(*ptn_bridge), GFP_KERNEL);
265 	if (!ptn_bridge) {
266 		return -ENOMEM;
267 	}
268 
269 	panel_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 0, 0);
270 	if (IS_ERR(panel_bridge))
271 		return PTR_ERR(panel_bridge);
272 
273 	ptn_bridge->panel_bridge = panel_bridge;
274 	ptn_bridge->client = client;
275 
276 	ptn_bridge->gpio_pd_n = devm_gpiod_get(&client->dev, "powerdown",
277 					       GPIOD_OUT_HIGH);
278 	if (IS_ERR(ptn_bridge->gpio_pd_n)) {
279 		ret = PTR_ERR(ptn_bridge->gpio_pd_n);
280 		dev_err(dev, "cannot get gpio_pd_n %d\n", ret);
281 		return ret;
282 	}
283 
284 	/*
285 	 * Request the reset pin low to avoid the bridge being
286 	 * initialized prematurely
287 	 */
288 	ptn_bridge->gpio_rst_n = devm_gpiod_get(&client->dev, "reset",
289 						GPIOD_OUT_LOW);
290 	if (IS_ERR(ptn_bridge->gpio_rst_n)) {
291 		ret = PTR_ERR(ptn_bridge->gpio_rst_n);
292 		DRM_ERROR("cannot get gpio_rst_n %d\n", ret);
293 		return ret;
294 	}
295 
296 	ret = of_property_read_u32(dev->of_node, "edid-emulation",
297 			&ptn_bridge->edid_emulation);
298 	if (ret) {
299 		dev_err(dev, "Can't read EDID emulation value\n");
300 		return ret;
301 	}
302 
303 	ptn_bridge->bridge.funcs = &ptn3460_bridge_funcs;
304 	ptn_bridge->bridge.ops = DRM_BRIDGE_OP_EDID;
305 	ptn_bridge->bridge.type = DRM_MODE_CONNECTOR_LVDS;
306 	ptn_bridge->bridge.of_node = dev->of_node;
307 	drm_bridge_add(&ptn_bridge->bridge);
308 
309 	i2c_set_clientdata(client, ptn_bridge);
310 
311 	return 0;
312 }
313 
314 static void ptn3460_remove(struct i2c_client *client)
315 {
316 	struct ptn3460_bridge *ptn_bridge = i2c_get_clientdata(client);
317 
318 	drm_bridge_remove(&ptn_bridge->bridge);
319 }
320 
321 static const struct i2c_device_id ptn3460_i2c_table[] = {
322 	{"ptn3460", 0},
323 	{},
324 };
325 MODULE_DEVICE_TABLE(i2c, ptn3460_i2c_table);
326 
327 static const struct of_device_id ptn3460_match[] = {
328 	{ .compatible = "nxp,ptn3460" },
329 	{},
330 };
331 MODULE_DEVICE_TABLE(of, ptn3460_match);
332 
333 static struct i2c_driver ptn3460_driver = {
334 	.id_table	= ptn3460_i2c_table,
335 	.probe		= ptn3460_probe,
336 	.remove		= ptn3460_remove,
337 	.driver		= {
338 		.name	= "nxp,ptn3460",
339 		.of_match_table = ptn3460_match,
340 	},
341 };
342 module_i2c_driver(ptn3460_driver);
343 
344 MODULE_AUTHOR("Sean Paul <seanpaul@chromium.org>");
345 MODULE_DESCRIPTION("NXP ptn3460 eDP-LVDS converter driver");
346 MODULE_LICENSE("GPL v2");
347