]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/gpu/drm/bridge/ptn3460.c
drm/bridge: ptn3460: support drm_panel
[mirror_ubuntu-jammy-kernel.git] / drivers / gpu / drm / bridge / ptn3460.c
CommitLineData
a9fe713d
SP
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
94d50d57
AK
16#include <linux/delay.h>
17#include <linux/gpio.h>
18#include <linux/i2c.h>
a9fe713d
SP
19#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/of_gpio.h>
5bbb9a2e
AK
22#include <linux/of_graph.h>
23
24#include <drm/drm_panel.h>
a9fe713d 25
94d50d57
AK
26#include "bridge/ptn3460.h"
27
a9fe713d
SP
28#include "drm_crtc.h"
29#include "drm_crtc_helper.h"
94d50d57
AK
30#include "drm_edid.h"
31#include "drmP.h"
a9fe713d
SP
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
39struct ptn3460_bridge {
40 struct drm_connector connector;
41 struct i2c_client *client;
94d50d57 42 struct drm_bridge bridge;
a9fe713d 43 struct edid *edid;
5bbb9a2e 44 struct drm_panel *panel;
a9fe713d
SP
45 int gpio_pd_n;
46 int gpio_rst_n;
47 u32 edid_emulation;
48 bool enabled;
49};
50
94d50d57
AK
51static inline struct ptn3460_bridge *
52 bridge_to_ptn3460(struct drm_bridge *bridge)
53{
54 return container_of(bridge, struct ptn3460_bridge, bridge);
55}
56
57static inline struct ptn3460_bridge *
58 connector_to_ptn3460(struct drm_connector *connector)
59{
60 return container_of(connector, struct ptn3460_bridge, connector);
61}
62
a9fe713d
SP
63static 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
83static 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
101static 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) {
94d50d57 110 DRM_ERROR("Failed to transfer EDID to sram, ret=%d\n", ret);
a9fe713d
SP
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) {
94d50d57 120 DRM_ERROR("Failed to write EDID value, ret=%d\n", ret);
a9fe713d
SP
121 return ret;
122 }
123
124 return 0;
125}
126
127static void ptn3460_pre_enable(struct drm_bridge *bridge)
128{
94d50d57 129 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
a9fe713d
SP
130 int ret;
131
132 if (ptn_bridge->enabled)
133 return;
134
135 if (gpio_is_valid(ptn_bridge->gpio_pd_n))
136 gpio_set_value(ptn_bridge->gpio_pd_n, 1);
137
138 if (gpio_is_valid(ptn_bridge->gpio_rst_n)) {
139 gpio_set_value(ptn_bridge->gpio_rst_n, 0);
94d50d57 140 usleep_range(10, 20);
a9fe713d
SP
141 gpio_set_value(ptn_bridge->gpio_rst_n, 1);
142 }
143
5bbb9a2e
AK
144 if (drm_panel_prepare(ptn_bridge->panel)) {
145 DRM_ERROR("failed to prepare panel\n");
146 return;
147 }
148
a9fe713d
SP
149 /*
150 * There's a bug in the PTN chip where it falsely asserts hotplug before
151 * it is fully functional. We're forced to wait for the maximum start up
152 * time specified in the chip's datasheet to make sure we're really up.
153 */
154 msleep(90);
155
156 ret = ptn3460_select_edid(ptn_bridge);
157 if (ret)
94d50d57 158 DRM_ERROR("Select EDID failed ret=%d\n", ret);
a9fe713d
SP
159
160 ptn_bridge->enabled = true;
161}
162
163static void ptn3460_enable(struct drm_bridge *bridge)
164{
5bbb9a2e
AK
165 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
166
167 if (drm_panel_enable(ptn_bridge->panel)) {
168 DRM_ERROR("failed to enable panel\n");
169 return;
170 }
a9fe713d
SP
171}
172
173static void ptn3460_disable(struct drm_bridge *bridge)
174{
94d50d57 175 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
a9fe713d
SP
176
177 if (!ptn_bridge->enabled)
178 return;
179
180 ptn_bridge->enabled = false;
181
5bbb9a2e
AK
182 if (drm_panel_disable(ptn_bridge->panel)) {
183 DRM_ERROR("failed to disable panel\n");
184 return;
185 }
186
a9fe713d
SP
187 if (gpio_is_valid(ptn_bridge->gpio_rst_n))
188 gpio_set_value(ptn_bridge->gpio_rst_n, 1);
189
190 if (gpio_is_valid(ptn_bridge->gpio_pd_n))
191 gpio_set_value(ptn_bridge->gpio_pd_n, 0);
192}
193
194static void ptn3460_post_disable(struct drm_bridge *bridge)
195{
5bbb9a2e
AK
196 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
197
198 if (drm_panel_unprepare(ptn_bridge->panel)) {
199 DRM_ERROR("failed to unprepare panel\n");
200 return;
201 }
a9fe713d
SP
202}
203
94d50d57 204static int ptn3460_get_modes(struct drm_connector *connector)
a9fe713d
SP
205{
206 struct ptn3460_bridge *ptn_bridge;
207 u8 *edid;
94d50d57 208 int ret, num_modes = 0;
a9fe713d
SP
209 bool power_off;
210
94d50d57 211 ptn_bridge = connector_to_ptn3460(connector);
a9fe713d
SP
212
213 if (ptn_bridge->edid)
214 return drm_add_edid_modes(connector, ptn_bridge->edid);
215
216 power_off = !ptn_bridge->enabled;
94d50d57 217 ptn3460_pre_enable(&ptn_bridge->bridge);
a9fe713d
SP
218
219 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
220 if (!edid) {
94d50d57 221 DRM_ERROR("Failed to allocate EDID\n");
a9fe713d
SP
222 return 0;
223 }
224
225 ret = ptn3460_read_bytes(ptn_bridge, PTN3460_EDID_ADDR, edid,
226 EDID_LENGTH);
227 if (ret) {
228 kfree(edid);
a9fe713d
SP
229 goto out;
230 }
231
232 ptn_bridge->edid = (struct edid *)edid;
233 drm_mode_connector_update_edid_property(connector, ptn_bridge->edid);
234
235 num_modes = drm_add_edid_modes(connector, ptn_bridge->edid);
236
237out:
238 if (power_off)
94d50d57 239 ptn3460_disable(&ptn_bridge->bridge);
a9fe713d
SP
240
241 return num_modes;
242}
243
94d50d57 244static struct drm_encoder *ptn3460_best_encoder(struct drm_connector *connector)
a9fe713d 245{
94d50d57 246 struct ptn3460_bridge *ptn_bridge = connector_to_ptn3460(connector);
a9fe713d 247
6a1688ae 248 return ptn_bridge->bridge.encoder;
a9fe713d
SP
249}
250
94d50d57 251static struct drm_connector_helper_funcs ptn3460_connector_helper_funcs = {
a9fe713d 252 .get_modes = ptn3460_get_modes,
a9fe713d
SP
253 .best_encoder = ptn3460_best_encoder,
254};
255
94d50d57 256static enum drm_connector_status ptn3460_detect(struct drm_connector *connector,
a9fe713d
SP
257 bool force)
258{
259 return connector_status_connected;
260}
261
94d50d57 262static void ptn3460_connector_destroy(struct drm_connector *connector)
a9fe713d
SP
263{
264 drm_connector_cleanup(connector);
265}
266
94d50d57 267static struct drm_connector_funcs ptn3460_connector_funcs = {
a9fe713d
SP
268 .dpms = drm_helper_connector_dpms,
269 .fill_modes = drm_helper_probe_single_connector_modes,
270 .detect = ptn3460_detect,
271 .destroy = ptn3460_connector_destroy,
272};
273
6a1688ae 274int ptn3460_bridge_attach(struct drm_bridge *bridge)
a9fe713d 275{
6a1688ae 276 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
a9fe713d 277 int ret;
6a1688ae
AK
278
279 if (!bridge->encoder) {
280 DRM_ERROR("Parent encoder object not found");
281 return -ENODEV;
282 }
283
284 ret = drm_connector_init(bridge->dev, &ptn_bridge->connector,
285 &ptn3460_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
286 if (ret) {
287 DRM_ERROR("Failed to initialize connector with drm\n");
288 return ret;
289 }
290 drm_connector_helper_add(&ptn_bridge->connector,
291 &ptn3460_connector_helper_funcs);
292 drm_connector_register(&ptn_bridge->connector);
293 drm_mode_connector_attach_encoder(&ptn_bridge->connector,
294 bridge->encoder);
295
5bbb9a2e
AK
296 if (ptn_bridge->panel)
297 drm_panel_attach(ptn_bridge->panel, &ptn_bridge->connector);
298
6a1688ae
AK
299 return ret;
300}
301
302static struct drm_bridge_funcs ptn3460_bridge_funcs = {
303 .pre_enable = ptn3460_pre_enable,
304 .enable = ptn3460_enable,
305 .disable = ptn3460_disable,
306 .post_disable = ptn3460_post_disable,
307 .attach = ptn3460_bridge_attach,
308};
309
310static int ptn3460_probe(struct i2c_client *client,
311 const struct i2c_device_id *id)
312{
313 struct device *dev = &client->dev;
a9fe713d 314 struct ptn3460_bridge *ptn_bridge;
5bbb9a2e 315 struct device_node *endpoint, *panel_node;
6a1688ae 316 int ret;
a9fe713d 317
6a1688ae 318 ptn_bridge = devm_kzalloc(dev, sizeof(*ptn_bridge), GFP_KERNEL);
a9fe713d 319 if (!ptn_bridge) {
a9fe713d
SP
320 return -ENOMEM;
321 }
322
5bbb9a2e
AK
323 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
324 if (endpoint) {
325 panel_node = of_graph_get_remote_port_parent(endpoint);
326 if (panel_node) {
327 ptn_bridge->panel = of_drm_find_panel(panel_node);
328 of_node_put(panel_node);
329 if (!ptn_bridge->panel)
330 return -EPROBE_DEFER;
331 }
332 }
333
a9fe713d 334 ptn_bridge->client = client;
6a1688ae
AK
335 ptn_bridge->gpio_pd_n = of_get_named_gpio(dev->of_node,
336 "powerdown-gpio", 0);
a9fe713d
SP
337 if (gpio_is_valid(ptn_bridge->gpio_pd_n)) {
338 ret = gpio_request_one(ptn_bridge->gpio_pd_n,
339 GPIOF_OUT_INIT_HIGH, "PTN3460_PD_N");
340 if (ret) {
6a1688ae
AK
341 dev_err(dev, "Request powerdown-gpio failed (%d)\n",
342 ret);
a9fe713d
SP
343 return ret;
344 }
345 }
346
6a1688ae
AK
347 ptn_bridge->gpio_rst_n = of_get_named_gpio(dev->of_node,
348 "reset-gpio", 0);
a9fe713d
SP
349 if (gpio_is_valid(ptn_bridge->gpio_rst_n)) {
350 /*
351 * Request the reset pin low to avoid the bridge being
352 * initialized prematurely
353 */
354 ret = gpio_request_one(ptn_bridge->gpio_rst_n,
355 GPIOF_OUT_INIT_LOW, "PTN3460_RST_N");
356 if (ret) {
6a1688ae 357 dev_err(dev, "Request reset-gpio failed (%d)\n", ret);
a9fe713d
SP
358 gpio_free(ptn_bridge->gpio_pd_n);
359 return ret;
360 }
361 }
362
6a1688ae 363 ret = of_property_read_u32(dev->of_node, "edid-emulation",
a9fe713d
SP
364 &ptn_bridge->edid_emulation);
365 if (ret) {
6a1688ae 366 dev_err(dev, "Can't read EDID emulation value\n");
a9fe713d
SP
367 goto err;
368 }
369
b07b90fd 370 ptn_bridge->bridge.funcs = &ptn3460_bridge_funcs;
5bbb9a2e 371 ptn_bridge->bridge.of_node = dev->of_node;
6a1688ae 372 ret = drm_bridge_add(&ptn_bridge->bridge);
a9fe713d 373 if (ret) {
6a1688ae 374 DRM_ERROR("Failed to add bridge\n");
a9fe713d
SP
375 goto err;
376 }
377
6a1688ae 378 i2c_set_clientdata(client, ptn_bridge);
a9fe713d
SP
379
380 return 0;
381
382err:
383 if (gpio_is_valid(ptn_bridge->gpio_pd_n))
384 gpio_free(ptn_bridge->gpio_pd_n);
385 if (gpio_is_valid(ptn_bridge->gpio_rst_n))
386 gpio_free(ptn_bridge->gpio_rst_n);
387 return ret;
388}
3d3f8b1f 389
6a1688ae 390static int ptn3460_remove(struct i2c_client *client)
3d3f8b1f 391{
6a1688ae
AK
392 struct ptn3460_bridge *ptn_bridge = i2c_get_clientdata(client);
393
394 drm_bridge_remove(&ptn_bridge->bridge);
3d3f8b1f
AK
395
396 if (gpio_is_valid(ptn_bridge->gpio_pd_n))
397 gpio_free(ptn_bridge->gpio_pd_n);
398 if (gpio_is_valid(ptn_bridge->gpio_rst_n))
399 gpio_free(ptn_bridge->gpio_rst_n);
6a1688ae
AK
400
401 return 0;
3d3f8b1f 402}
6a1688ae
AK
403
404static const struct i2c_device_id ptn3460_i2c_table[] = {
405 {"nxp,ptn3460", 0},
406 {},
407};
408MODULE_DEVICE_TABLE(i2c, ptn3460_i2c_table);
409
410static const struct of_device_id ptn3460_match[] = {
411 { .compatible = "nxp,ptn3460" },
412 {},
413};
414MODULE_DEVICE_TABLE(of, ptn3460_match);
415
416static struct i2c_driver ptn3460_driver = {
417 .id_table = ptn3460_i2c_table,
418 .probe = ptn3460_probe,
419 .remove = ptn3460_remove,
420 .driver = {
421 .name = "nxp,ptn3460",
422 .owner = THIS_MODULE,
423 .of_match_table = ptn3460_match,
424 },
425};
426module_i2c_driver(ptn3460_driver);
427
428MODULE_AUTHOR("Sean Paul <seanpaul@chromium.org>");
429MODULE_DESCRIPTION("NXP ptn3460 eDP-LVDS converter driver");
430MODULE_LICENSE("GPL v2");