blob: 4db38e1f7a1fe466263f86f6b11c6ff890d82c20 [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>
Sean Paula9fe7132014-02-24 19:31:24 +090022
23#include "bridge/ptn3460.h"
24
Ajay Kumar94d50d52015-01-20 22:08:42 +053025#include "drm_crtc.h"
26#include "drm_crtc_helper.h"
27#include "drm_edid.h"
28#include "drmP.h"
29
Sean Paula9fe7132014-02-24 19:31:24 +090030#define PTN3460_EDID_ADDR 0x0
31#define PTN3460_EDID_EMULATION_ADDR 0x84
32#define PTN3460_EDID_ENABLE_EMULATION 0
33#define PTN3460_EDID_EMULATION_SELECTION 1
34#define PTN3460_EDID_SRAM_LOAD_ADDR 0x85
35
36struct ptn3460_bridge {
37 struct drm_connector connector;
38 struct i2c_client *client;
39 struct drm_encoder *encoder;
Ajay Kumar94d50d52015-01-20 22:08:42 +053040 struct drm_bridge bridge;
Sean Paula9fe7132014-02-24 19:31:24 +090041 struct edid *edid;
42 int gpio_pd_n;
43 int gpio_rst_n;
44 u32 edid_emulation;
45 bool enabled;
46};
47
Ajay Kumar94d50d52015-01-20 22:08:42 +053048static inline struct ptn3460_bridge *
49 bridge_to_ptn3460(struct drm_bridge *bridge)
50{
51 return container_of(bridge, struct ptn3460_bridge, bridge);
52}
53
54static inline struct ptn3460_bridge *
55 connector_to_ptn3460(struct drm_connector *connector)
56{
57 return container_of(connector, struct ptn3460_bridge, connector);
58}
59
Sean Paula9fe7132014-02-24 19:31:24 +090060static int ptn3460_read_bytes(struct ptn3460_bridge *ptn_bridge, char addr,
61 u8 *buf, int len)
62{
63 int ret;
64
65 ret = i2c_master_send(ptn_bridge->client, &addr, 1);
66 if (ret <= 0) {
67 DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
68 return ret;
69 }
70
71 ret = i2c_master_recv(ptn_bridge->client, buf, len);
72 if (ret <= 0) {
73 DRM_ERROR("Failed to recv i2c data, ret=%d\n", ret);
74 return ret;
75 }
76
77 return 0;
78}
79
80static int ptn3460_write_byte(struct ptn3460_bridge *ptn_bridge, char addr,
81 char val)
82{
83 int ret;
84 char buf[2];
85
86 buf[0] = addr;
87 buf[1] = val;
88
89 ret = i2c_master_send(ptn_bridge->client, buf, ARRAY_SIZE(buf));
90 if (ret <= 0) {
91 DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
92 return ret;
93 }
94
95 return 0;
96}
97
98static int ptn3460_select_edid(struct ptn3460_bridge *ptn_bridge)
99{
100 int ret;
101 char val;
102
103 /* Load the selected edid into SRAM (accessed at PTN3460_EDID_ADDR) */
104 ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_SRAM_LOAD_ADDR,
105 ptn_bridge->edid_emulation);
106 if (ret) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530107 DRM_ERROR("Failed to transfer EDID to sram, ret=%d\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900108 return ret;
109 }
110
111 /* Enable EDID emulation and select the desired EDID */
112 val = 1 << PTN3460_EDID_ENABLE_EMULATION |
113 ptn_bridge->edid_emulation << PTN3460_EDID_EMULATION_SELECTION;
114
115 ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_EMULATION_ADDR, val);
116 if (ret) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530117 DRM_ERROR("Failed to write EDID value, ret=%d\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900118 return ret;
119 }
120
121 return 0;
122}
123
124static void ptn3460_pre_enable(struct drm_bridge *bridge)
125{
Ajay Kumar94d50d52015-01-20 22:08:42 +0530126 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900127 int ret;
128
129 if (ptn_bridge->enabled)
130 return;
131
132 if (gpio_is_valid(ptn_bridge->gpio_pd_n))
133 gpio_set_value(ptn_bridge->gpio_pd_n, 1);
134
135 if (gpio_is_valid(ptn_bridge->gpio_rst_n)) {
136 gpio_set_value(ptn_bridge->gpio_rst_n, 0);
Ajay Kumar94d50d52015-01-20 22:08:42 +0530137 usleep_range(10, 20);
Sean Paula9fe7132014-02-24 19:31:24 +0900138 gpio_set_value(ptn_bridge->gpio_rst_n, 1);
139 }
140
141 /*
142 * There's a bug in the PTN chip where it falsely asserts hotplug before
143 * it is fully functional. We're forced to wait for the maximum start up
144 * time specified in the chip's datasheet to make sure we're really up.
145 */
146 msleep(90);
147
148 ret = ptn3460_select_edid(ptn_bridge);
149 if (ret)
Ajay Kumar94d50d52015-01-20 22:08:42 +0530150 DRM_ERROR("Select EDID failed ret=%d\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900151
152 ptn_bridge->enabled = true;
153}
154
155static void ptn3460_enable(struct drm_bridge *bridge)
156{
157}
158
159static void ptn3460_disable(struct drm_bridge *bridge)
160{
Ajay Kumar94d50d52015-01-20 22:08:42 +0530161 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900162
163 if (!ptn_bridge->enabled)
164 return;
165
166 ptn_bridge->enabled = false;
167
168 if (gpio_is_valid(ptn_bridge->gpio_rst_n))
169 gpio_set_value(ptn_bridge->gpio_rst_n, 1);
170
171 if (gpio_is_valid(ptn_bridge->gpio_pd_n))
172 gpio_set_value(ptn_bridge->gpio_pd_n, 0);
173}
174
175static void ptn3460_post_disable(struct drm_bridge *bridge)
176{
177}
178
Ajay Kumar94d50d52015-01-20 22:08:42 +0530179static void ptn3460_bridge_destroy(struct drm_bridge *bridge)
Sean Paula9fe7132014-02-24 19:31:24 +0900180{
Ajay Kumar94d50d52015-01-20 22:08:42 +0530181 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900182
183 drm_bridge_cleanup(bridge);
184 if (gpio_is_valid(ptn_bridge->gpio_pd_n))
185 gpio_free(ptn_bridge->gpio_pd_n);
186 if (gpio_is_valid(ptn_bridge->gpio_rst_n))
187 gpio_free(ptn_bridge->gpio_rst_n);
188 /* Nothing else to free, we've got devm allocated memory */
189}
190
Ajay Kumar94d50d52015-01-20 22:08:42 +0530191static struct drm_bridge_funcs ptn3460_bridge_funcs = {
Sean Paula9fe7132014-02-24 19:31:24 +0900192 .pre_enable = ptn3460_pre_enable,
193 .enable = ptn3460_enable,
194 .disable = ptn3460_disable,
195 .post_disable = ptn3460_post_disable,
196 .destroy = ptn3460_bridge_destroy,
197};
198
Ajay Kumar94d50d52015-01-20 22:08:42 +0530199static int ptn3460_get_modes(struct drm_connector *connector)
Sean Paula9fe7132014-02-24 19:31:24 +0900200{
201 struct ptn3460_bridge *ptn_bridge;
202 u8 *edid;
Ajay Kumar94d50d52015-01-20 22:08:42 +0530203 int ret, num_modes = 0;
Sean Paula9fe7132014-02-24 19:31:24 +0900204 bool power_off;
205
Ajay Kumar94d50d52015-01-20 22:08:42 +0530206 ptn_bridge = connector_to_ptn3460(connector);
Sean Paula9fe7132014-02-24 19:31:24 +0900207
208 if (ptn_bridge->edid)
209 return drm_add_edid_modes(connector, ptn_bridge->edid);
210
211 power_off = !ptn_bridge->enabled;
Ajay Kumar94d50d52015-01-20 22:08:42 +0530212 ptn3460_pre_enable(&ptn_bridge->bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900213
214 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
215 if (!edid) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530216 DRM_ERROR("Failed to allocate EDID\n");
Sean Paula9fe7132014-02-24 19:31:24 +0900217 return 0;
218 }
219
220 ret = ptn3460_read_bytes(ptn_bridge, PTN3460_EDID_ADDR, edid,
221 EDID_LENGTH);
222 if (ret) {
223 kfree(edid);
Sean Paula9fe7132014-02-24 19:31:24 +0900224 goto out;
225 }
226
227 ptn_bridge->edid = (struct edid *)edid;
228 drm_mode_connector_update_edid_property(connector, ptn_bridge->edid);
229
230 num_modes = drm_add_edid_modes(connector, ptn_bridge->edid);
231
232out:
233 if (power_off)
Ajay Kumar94d50d52015-01-20 22:08:42 +0530234 ptn3460_disable(&ptn_bridge->bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900235
236 return num_modes;
237}
238
Ajay Kumar94d50d52015-01-20 22:08:42 +0530239static struct drm_encoder *ptn3460_best_encoder(struct drm_connector *connector)
Sean Paula9fe7132014-02-24 19:31:24 +0900240{
Ajay Kumar94d50d52015-01-20 22:08:42 +0530241 struct ptn3460_bridge *ptn_bridge = connector_to_ptn3460(connector);
Sean Paula9fe7132014-02-24 19:31:24 +0900242
243 return ptn_bridge->encoder;
244}
245
Ajay Kumar94d50d52015-01-20 22:08:42 +0530246static struct drm_connector_helper_funcs ptn3460_connector_helper_funcs = {
Sean Paula9fe7132014-02-24 19:31:24 +0900247 .get_modes = ptn3460_get_modes,
Sean Paula9fe7132014-02-24 19:31:24 +0900248 .best_encoder = ptn3460_best_encoder,
249};
250
Ajay Kumar94d50d52015-01-20 22:08:42 +0530251static enum drm_connector_status ptn3460_detect(struct drm_connector *connector,
Sean Paula9fe7132014-02-24 19:31:24 +0900252 bool force)
253{
254 return connector_status_connected;
255}
256
Ajay Kumar94d50d52015-01-20 22:08:42 +0530257static void ptn3460_connector_destroy(struct drm_connector *connector)
Sean Paula9fe7132014-02-24 19:31:24 +0900258{
259 drm_connector_cleanup(connector);
260}
261
Ajay Kumar94d50d52015-01-20 22:08:42 +0530262static struct drm_connector_funcs ptn3460_connector_funcs = {
Sean Paula9fe7132014-02-24 19:31:24 +0900263 .dpms = drm_helper_connector_dpms,
264 .fill_modes = drm_helper_probe_single_connector_modes,
265 .detect = ptn3460_detect,
266 .destroy = ptn3460_connector_destroy,
267};
268
269int ptn3460_init(struct drm_device *dev, struct drm_encoder *encoder,
270 struct i2c_client *client, struct device_node *node)
271{
272 int ret;
Sean Paula9fe7132014-02-24 19:31:24 +0900273 struct ptn3460_bridge *ptn_bridge;
274
Sean Paula9fe7132014-02-24 19:31:24 +0900275 ptn_bridge = devm_kzalloc(dev->dev, sizeof(*ptn_bridge), GFP_KERNEL);
276 if (!ptn_bridge) {
Sean Paula9fe7132014-02-24 19:31:24 +0900277 return -ENOMEM;
278 }
279
280 ptn_bridge->client = client;
281 ptn_bridge->encoder = encoder;
Sean Paula9fe7132014-02-24 19:31:24 +0900282 ptn_bridge->gpio_pd_n = of_get_named_gpio(node, "powerdown-gpio", 0);
283 if (gpio_is_valid(ptn_bridge->gpio_pd_n)) {
284 ret = gpio_request_one(ptn_bridge->gpio_pd_n,
285 GPIOF_OUT_INIT_HIGH, "PTN3460_PD_N");
286 if (ret) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530287 dev_err(&client->dev,
288 "Request powerdown-gpio failed (%d)\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900289 return ret;
290 }
291 }
292
293 ptn_bridge->gpio_rst_n = of_get_named_gpio(node, "reset-gpio", 0);
294 if (gpio_is_valid(ptn_bridge->gpio_rst_n)) {
295 /*
296 * Request the reset pin low to avoid the bridge being
297 * initialized prematurely
298 */
299 ret = gpio_request_one(ptn_bridge->gpio_rst_n,
300 GPIOF_OUT_INIT_LOW, "PTN3460_RST_N");
301 if (ret) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530302 dev_err(&client->dev,
303 "Request reset-gpio failed (%d)\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900304 gpio_free(ptn_bridge->gpio_pd_n);
305 return ret;
306 }
307 }
308
309 ret = of_property_read_u32(node, "edid-emulation",
310 &ptn_bridge->edid_emulation);
311 if (ret) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530312 dev_err(&client->dev, "Can't read EDID emulation value\n");
Sean Paula9fe7132014-02-24 19:31:24 +0900313 goto err;
314 }
315
Ajay Kumar94d50d52015-01-20 22:08:42 +0530316 ret = drm_bridge_init(dev, &ptn_bridge->bridge, &ptn3460_bridge_funcs);
Sean Paula9fe7132014-02-24 19:31:24 +0900317 if (ret) {
318 DRM_ERROR("Failed to initialize bridge with drm\n");
319 goto err;
320 }
321
Ajay Kumar94d50d52015-01-20 22:08:42 +0530322 encoder->bridge = &ptn_bridge->bridge;
Sean Paula9fe7132014-02-24 19:31:24 +0900323
324 ret = drm_connector_init(dev, &ptn_bridge->connector,
325 &ptn3460_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
326 if (ret) {
327 DRM_ERROR("Failed to initialize connector with drm\n");
328 goto err;
329 }
330 drm_connector_helper_add(&ptn_bridge->connector,
331 &ptn3460_connector_helper_funcs);
Thomas Wood34ea3d32014-05-29 16:57:41 +0100332 drm_connector_register(&ptn_bridge->connector);
Sean Paula9fe7132014-02-24 19:31:24 +0900333 drm_mode_connector_attach_encoder(&ptn_bridge->connector, encoder);
334
335 return 0;
336
337err:
338 if (gpio_is_valid(ptn_bridge->gpio_pd_n))
339 gpio_free(ptn_bridge->gpio_pd_n);
340 if (gpio_is_valid(ptn_bridge->gpio_rst_n))
341 gpio_free(ptn_bridge->gpio_rst_n);
342 return ret;
343}
Inki Dae96e112c2014-04-04 12:14:51 +0900344EXPORT_SYMBOL(ptn3460_init);