blob: 4a818c1b62e0432a2efdf0d9bd3191104d0d21e4 [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 struct drm_bridge_funcs ptn3460_bridge_funcs = {
Sean Paula9fe7132014-02-24 19:31:24 +0900180 .pre_enable = ptn3460_pre_enable,
181 .enable = ptn3460_enable,
182 .disable = ptn3460_disable,
183 .post_disable = ptn3460_post_disable,
Sean Paula9fe7132014-02-24 19:31:24 +0900184};
185
Ajay Kumar94d50d52015-01-20 22:08:42 +0530186static int ptn3460_get_modes(struct drm_connector *connector)
Sean Paula9fe7132014-02-24 19:31:24 +0900187{
188 struct ptn3460_bridge *ptn_bridge;
189 u8 *edid;
Ajay Kumar94d50d52015-01-20 22:08:42 +0530190 int ret, num_modes = 0;
Sean Paula9fe7132014-02-24 19:31:24 +0900191 bool power_off;
192
Ajay Kumar94d50d52015-01-20 22:08:42 +0530193 ptn_bridge = connector_to_ptn3460(connector);
Sean Paula9fe7132014-02-24 19:31:24 +0900194
195 if (ptn_bridge->edid)
196 return drm_add_edid_modes(connector, ptn_bridge->edid);
197
198 power_off = !ptn_bridge->enabled;
Ajay Kumar94d50d52015-01-20 22:08:42 +0530199 ptn3460_pre_enable(&ptn_bridge->bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900200
201 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
202 if (!edid) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530203 DRM_ERROR("Failed to allocate EDID\n");
Sean Paula9fe7132014-02-24 19:31:24 +0900204 return 0;
205 }
206
207 ret = ptn3460_read_bytes(ptn_bridge, PTN3460_EDID_ADDR, edid,
208 EDID_LENGTH);
209 if (ret) {
210 kfree(edid);
Sean Paula9fe7132014-02-24 19:31:24 +0900211 goto out;
212 }
213
214 ptn_bridge->edid = (struct edid *)edid;
215 drm_mode_connector_update_edid_property(connector, ptn_bridge->edid);
216
217 num_modes = drm_add_edid_modes(connector, ptn_bridge->edid);
218
219out:
220 if (power_off)
Ajay Kumar94d50d52015-01-20 22:08:42 +0530221 ptn3460_disable(&ptn_bridge->bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900222
223 return num_modes;
224}
225
Ajay Kumar94d50d52015-01-20 22:08:42 +0530226static struct drm_encoder *ptn3460_best_encoder(struct drm_connector *connector)
Sean Paula9fe7132014-02-24 19:31:24 +0900227{
Ajay Kumar94d50d52015-01-20 22:08:42 +0530228 struct ptn3460_bridge *ptn_bridge = connector_to_ptn3460(connector);
Sean Paula9fe7132014-02-24 19:31:24 +0900229
230 return ptn_bridge->encoder;
231}
232
Ajay Kumar94d50d52015-01-20 22:08:42 +0530233static struct drm_connector_helper_funcs ptn3460_connector_helper_funcs = {
Sean Paula9fe7132014-02-24 19:31:24 +0900234 .get_modes = ptn3460_get_modes,
Sean Paula9fe7132014-02-24 19:31:24 +0900235 .best_encoder = ptn3460_best_encoder,
236};
237
Ajay Kumar94d50d52015-01-20 22:08:42 +0530238static enum drm_connector_status ptn3460_detect(struct drm_connector *connector,
Sean Paula9fe7132014-02-24 19:31:24 +0900239 bool force)
240{
241 return connector_status_connected;
242}
243
Ajay Kumar94d50d52015-01-20 22:08:42 +0530244static void ptn3460_connector_destroy(struct drm_connector *connector)
Sean Paula9fe7132014-02-24 19:31:24 +0900245{
246 drm_connector_cleanup(connector);
247}
248
Ajay Kumar94d50d52015-01-20 22:08:42 +0530249static struct drm_connector_funcs ptn3460_connector_funcs = {
Sean Paula9fe7132014-02-24 19:31:24 +0900250 .dpms = drm_helper_connector_dpms,
251 .fill_modes = drm_helper_probe_single_connector_modes,
252 .detect = ptn3460_detect,
253 .destroy = ptn3460_connector_destroy,
254};
255
256int ptn3460_init(struct drm_device *dev, struct drm_encoder *encoder,
257 struct i2c_client *client, struct device_node *node)
258{
259 int ret;
Sean Paula9fe7132014-02-24 19:31:24 +0900260 struct ptn3460_bridge *ptn_bridge;
261
Sean Paula9fe7132014-02-24 19:31:24 +0900262 ptn_bridge = devm_kzalloc(dev->dev, sizeof(*ptn_bridge), GFP_KERNEL);
263 if (!ptn_bridge) {
Sean Paula9fe7132014-02-24 19:31:24 +0900264 return -ENOMEM;
265 }
266
267 ptn_bridge->client = client;
268 ptn_bridge->encoder = encoder;
Sean Paula9fe7132014-02-24 19:31:24 +0900269 ptn_bridge->gpio_pd_n = of_get_named_gpio(node, "powerdown-gpio", 0);
270 if (gpio_is_valid(ptn_bridge->gpio_pd_n)) {
271 ret = gpio_request_one(ptn_bridge->gpio_pd_n,
272 GPIOF_OUT_INIT_HIGH, "PTN3460_PD_N");
273 if (ret) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530274 dev_err(&client->dev,
275 "Request powerdown-gpio failed (%d)\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900276 return ret;
277 }
278 }
279
280 ptn_bridge->gpio_rst_n = of_get_named_gpio(node, "reset-gpio", 0);
281 if (gpio_is_valid(ptn_bridge->gpio_rst_n)) {
282 /*
283 * Request the reset pin low to avoid the bridge being
284 * initialized prematurely
285 */
286 ret = gpio_request_one(ptn_bridge->gpio_rst_n,
287 GPIOF_OUT_INIT_LOW, "PTN3460_RST_N");
288 if (ret) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530289 dev_err(&client->dev,
290 "Request reset-gpio failed (%d)\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900291 gpio_free(ptn_bridge->gpio_pd_n);
292 return ret;
293 }
294 }
295
296 ret = of_property_read_u32(node, "edid-emulation",
297 &ptn_bridge->edid_emulation);
298 if (ret) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530299 dev_err(&client->dev, "Can't read EDID emulation value\n");
Sean Paula9fe7132014-02-24 19:31:24 +0900300 goto err;
301 }
302
Ajay Kumarb07b90f2015-01-20 22:08:43 +0530303 ptn_bridge->bridge.funcs = &ptn3460_bridge_funcs;
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530304 ret = drm_bridge_attach(dev, &ptn_bridge->bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900305 if (ret) {
306 DRM_ERROR("Failed to initialize bridge with drm\n");
307 goto err;
308 }
309
Ajay Kumar94d50d52015-01-20 22:08:42 +0530310 encoder->bridge = &ptn_bridge->bridge;
Sean Paula9fe7132014-02-24 19:31:24 +0900311
312 ret = drm_connector_init(dev, &ptn_bridge->connector,
313 &ptn3460_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
314 if (ret) {
315 DRM_ERROR("Failed to initialize connector with drm\n");
316 goto err;
317 }
318 drm_connector_helper_add(&ptn_bridge->connector,
319 &ptn3460_connector_helper_funcs);
Thomas Wood34ea3d32014-05-29 16:57:41 +0100320 drm_connector_register(&ptn_bridge->connector);
Sean Paula9fe7132014-02-24 19:31:24 +0900321 drm_mode_connector_attach_encoder(&ptn_bridge->connector, encoder);
322
323 return 0;
324
325err:
326 if (gpio_is_valid(ptn_bridge->gpio_pd_n))
327 gpio_free(ptn_bridge->gpio_pd_n);
328 if (gpio_is_valid(ptn_bridge->gpio_rst_n))
329 gpio_free(ptn_bridge->gpio_rst_n);
330 return ret;
331}
Inki Dae96e112c2014-04-04 12:14:51 +0900332EXPORT_SYMBOL(ptn3460_init);
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530333
334void ptn3460_destroy(struct drm_bridge *bridge)
335{
336 struct ptn3460_bridge *ptn_bridge = bridge->driver_private;
337
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 /* Nothing else to free, we've got devm allocated memory */
343}
344EXPORT_SYMBOL(ptn3460_destroy);