blob: e6d5ae741a419181b99cdde117346ec935823f97 [file] [log] [blame]
Sean Paula9fe7132014-02-24 19:31:24 +09001/*
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
Ajay Kumar94d50d52015-01-20 22:08:42 +053016#include <linux/delay.h>
17#include <linux/gpio.h>
18#include <linux/i2c.h>
Sean Paula9fe7132014-02-24 19:31:24 +090019#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/of_gpio.h>
Ajay Kumar5bbb9a22015-01-20 22:08:47 +053022#include <linux/of_graph.h>
23
24#include <drm/drm_panel.h>
Sean Paula9fe7132014-02-24 19:31:24 +090025
26#include "bridge/ptn3460.h"
27
Ajay Kumar94d50d52015-01-20 22:08:42 +053028#include "drm_crtc.h"
29#include "drm_crtc_helper.h"
30#include "drm_edid.h"
31#include "drmP.h"
32
Sean Paula9fe7132014-02-24 19:31:24 +090033#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;
Ajay Kumar94d50d52015-01-20 22:08:42 +053042 struct drm_bridge bridge;
Sean Paula9fe7132014-02-24 19:31:24 +090043 struct edid *edid;
Ajay Kumar5bbb9a22015-01-20 22:08:47 +053044 struct drm_panel *panel;
Sean Paula9fe7132014-02-24 19:31:24 +090045 int gpio_pd_n;
46 int gpio_rst_n;
47 u32 edid_emulation;
48 bool enabled;
49};
50
Ajay Kumar94d50d52015-01-20 22:08:42 +053051static 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
Sean Paula9fe7132014-02-24 19:31:24 +090063static 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) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530110 DRM_ERROR("Failed to transfer EDID to sram, ret=%d\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900111 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) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530120 DRM_ERROR("Failed to write EDID value, ret=%d\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900121 return ret;
122 }
123
124 return 0;
125}
126
127static void ptn3460_pre_enable(struct drm_bridge *bridge)
128{
Ajay Kumar94d50d52015-01-20 22:08:42 +0530129 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900130 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);
Ajay Kumar94d50d52015-01-20 22:08:42 +0530140 usleep_range(10, 20);
Sean Paula9fe7132014-02-24 19:31:24 +0900141 gpio_set_value(ptn_bridge->gpio_rst_n, 1);
142 }
143
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530144 if (drm_panel_prepare(ptn_bridge->panel)) {
145 DRM_ERROR("failed to prepare panel\n");
146 return;
147 }
148
Sean Paula9fe7132014-02-24 19:31:24 +0900149 /*
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)
Ajay Kumar94d50d52015-01-20 22:08:42 +0530158 DRM_ERROR("Select EDID failed ret=%d\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900159
160 ptn_bridge->enabled = true;
161}
162
163static void ptn3460_enable(struct drm_bridge *bridge)
164{
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530165 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 }
Sean Paula9fe7132014-02-24 19:31:24 +0900171}
172
173static void ptn3460_disable(struct drm_bridge *bridge)
174{
Ajay Kumar94d50d52015-01-20 22:08:42 +0530175 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900176
177 if (!ptn_bridge->enabled)
178 return;
179
180 ptn_bridge->enabled = false;
181
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530182 if (drm_panel_disable(ptn_bridge->panel)) {
183 DRM_ERROR("failed to disable panel\n");
184 return;
185 }
186
Sean Paula9fe7132014-02-24 19:31:24 +0900187 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{
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530196 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 }
Sean Paula9fe7132014-02-24 19:31:24 +0900202}
203
Ajay Kumar94d50d52015-01-20 22:08:42 +0530204static int ptn3460_get_modes(struct drm_connector *connector)
Sean Paula9fe7132014-02-24 19:31:24 +0900205{
206 struct ptn3460_bridge *ptn_bridge;
207 u8 *edid;
Ajay Kumar94d50d52015-01-20 22:08:42 +0530208 int ret, num_modes = 0;
Sean Paula9fe7132014-02-24 19:31:24 +0900209 bool power_off;
210
Ajay Kumar94d50d52015-01-20 22:08:42 +0530211 ptn_bridge = connector_to_ptn3460(connector);
Sean Paula9fe7132014-02-24 19:31:24 +0900212
213 if (ptn_bridge->edid)
214 return drm_add_edid_modes(connector, ptn_bridge->edid);
215
216 power_off = !ptn_bridge->enabled;
Ajay Kumar94d50d52015-01-20 22:08:42 +0530217 ptn3460_pre_enable(&ptn_bridge->bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900218
219 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
220 if (!edid) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530221 DRM_ERROR("Failed to allocate EDID\n");
Sean Paula9fe7132014-02-24 19:31:24 +0900222 return 0;
223 }
224
225 ret = ptn3460_read_bytes(ptn_bridge, PTN3460_EDID_ADDR, edid,
226 EDID_LENGTH);
227 if (ret) {
228 kfree(edid);
Sean Paula9fe7132014-02-24 19:31:24 +0900229 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)
Ajay Kumar94d50d52015-01-20 22:08:42 +0530239 ptn3460_disable(&ptn_bridge->bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900240
241 return num_modes;
242}
243
Ajay Kumar94d50d52015-01-20 22:08:42 +0530244static struct drm_encoder *ptn3460_best_encoder(struct drm_connector *connector)
Sean Paula9fe7132014-02-24 19:31:24 +0900245{
Ajay Kumar94d50d52015-01-20 22:08:42 +0530246 struct ptn3460_bridge *ptn_bridge = connector_to_ptn3460(connector);
Sean Paula9fe7132014-02-24 19:31:24 +0900247
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530248 return ptn_bridge->bridge.encoder;
Sean Paula9fe7132014-02-24 19:31:24 +0900249}
250
Ajay Kumar94d50d52015-01-20 22:08:42 +0530251static struct drm_connector_helper_funcs ptn3460_connector_helper_funcs = {
Sean Paula9fe7132014-02-24 19:31:24 +0900252 .get_modes = ptn3460_get_modes,
Sean Paula9fe7132014-02-24 19:31:24 +0900253 .best_encoder = ptn3460_best_encoder,
254};
255
Ajay Kumar94d50d52015-01-20 22:08:42 +0530256static enum drm_connector_status ptn3460_detect(struct drm_connector *connector,
Sean Paula9fe7132014-02-24 19:31:24 +0900257 bool force)
258{
259 return connector_status_connected;
260}
261
Ajay Kumar94d50d52015-01-20 22:08:42 +0530262static void ptn3460_connector_destroy(struct drm_connector *connector)
Sean Paula9fe7132014-02-24 19:31:24 +0900263{
264 drm_connector_cleanup(connector);
265}
266
Ajay Kumar94d50d52015-01-20 22:08:42 +0530267static struct drm_connector_funcs ptn3460_connector_funcs = {
Sean Paula9fe7132014-02-24 19:31:24 +0900268 .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
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530274int ptn3460_bridge_attach(struct drm_bridge *bridge)
Sean Paula9fe7132014-02-24 19:31:24 +0900275{
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530276 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900277 int ret;
Sean Paula9fe7132014-02-24 19:31:24 +0900278
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530279 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
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530296 if (ptn_bridge->panel)
297 drm_panel_attach(ptn_bridge->panel, &ptn_bridge->connector);
298
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530299 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;
314 struct ptn3460_bridge *ptn_bridge;
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530315 struct device_node *endpoint, *panel_node;
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530316 int ret;
317
318 ptn_bridge = devm_kzalloc(dev, sizeof(*ptn_bridge), GFP_KERNEL);
Sean Paula9fe7132014-02-24 19:31:24 +0900319 if (!ptn_bridge) {
Sean Paula9fe7132014-02-24 19:31:24 +0900320 return -ENOMEM;
321 }
322
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530323 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
Sean Paula9fe7132014-02-24 19:31:24 +0900334 ptn_bridge->client = client;
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530335 ptn_bridge->gpio_pd_n = of_get_named_gpio(dev->of_node,
336 "powerdown-gpio", 0);
Sean Paula9fe7132014-02-24 19:31:24 +0900337 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) {
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530341 dev_err(dev, "Request powerdown-gpio failed (%d)\n",
342 ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900343 return ret;
344 }
345 }
346
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530347 ptn_bridge->gpio_rst_n = of_get_named_gpio(dev->of_node,
348 "reset-gpio", 0);
Sean Paula9fe7132014-02-24 19:31:24 +0900349 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) {
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530357 dev_err(dev, "Request reset-gpio failed (%d)\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900358 gpio_free(ptn_bridge->gpio_pd_n);
359 return ret;
360 }
361 }
362
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530363 ret = of_property_read_u32(dev->of_node, "edid-emulation",
Sean Paula9fe7132014-02-24 19:31:24 +0900364 &ptn_bridge->edid_emulation);
365 if (ret) {
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530366 dev_err(dev, "Can't read EDID emulation value\n");
Sean Paula9fe7132014-02-24 19:31:24 +0900367 goto err;
368 }
369
Ajay Kumarb07b90f2015-01-20 22:08:43 +0530370 ptn_bridge->bridge.funcs = &ptn3460_bridge_funcs;
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530371 ptn_bridge->bridge.of_node = dev->of_node;
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530372 ret = drm_bridge_add(&ptn_bridge->bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900373 if (ret) {
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530374 DRM_ERROR("Failed to add bridge\n");
Sean Paula9fe7132014-02-24 19:31:24 +0900375 goto err;
376 }
377
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530378 i2c_set_clientdata(client, ptn_bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900379
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}
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530389
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530390static int ptn3460_remove(struct i2c_client *client)
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530391{
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530392 struct ptn3460_bridge *ptn_bridge = i2c_get_clientdata(client);
393
394 drm_bridge_remove(&ptn_bridge->bridge);
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530395
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);
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530400
401 return 0;
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530402}
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530403
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");