blob: b1e0f0b078335f06f2eef4df59cd2b3359ab76dd [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>
Geert Uytterhoevendad3c352015-05-05 18:32:17 +020018#include <linux/gpio/consumer.h>
Ajay Kumar94d50d52015-01-20 22:08:42 +053019#include <linux/i2c.h>
Sean Paula9fe7132014-02-24 19:31:24 +090020#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/of_gpio.h>
Ajay Kumar5bbb9a22015-01-20 22:08:47 +053023#include <linux/of_graph.h>
24
25#include <drm/drm_panel.h>
Sean Paula9fe7132014-02-24 19:31:24 +090026
27#include "bridge/ptn3460.h"
28
Ajay Kumar94d50d52015-01-20 22:08:42 +053029#include "drm_crtc.h"
30#include "drm_crtc_helper.h"
31#include "drm_edid.h"
32#include "drmP.h"
33
Sean Paula9fe7132014-02-24 19:31:24 +090034#define PTN3460_EDID_ADDR 0x0
35#define PTN3460_EDID_EMULATION_ADDR 0x84
36#define PTN3460_EDID_ENABLE_EMULATION 0
37#define PTN3460_EDID_EMULATION_SELECTION 1
38#define PTN3460_EDID_SRAM_LOAD_ADDR 0x85
39
40struct ptn3460_bridge {
41 struct drm_connector connector;
42 struct i2c_client *client;
Ajay Kumar94d50d52015-01-20 22:08:42 +053043 struct drm_bridge bridge;
Sean Paula9fe7132014-02-24 19:31:24 +090044 struct edid *edid;
Ajay Kumar5bbb9a22015-01-20 22:08:47 +053045 struct drm_panel *panel;
Ajay Kumaraf478d82015-01-20 22:08:49 +053046 struct gpio_desc *gpio_pd_n;
47 struct gpio_desc *gpio_rst_n;
Sean Paula9fe7132014-02-24 19:31:24 +090048 u32 edid_emulation;
49 bool enabled;
50};
51
Ajay Kumar94d50d52015-01-20 22:08:42 +053052static inline struct ptn3460_bridge *
53 bridge_to_ptn3460(struct drm_bridge *bridge)
54{
55 return container_of(bridge, struct ptn3460_bridge, bridge);
56}
57
58static inline struct ptn3460_bridge *
59 connector_to_ptn3460(struct drm_connector *connector)
60{
61 return container_of(connector, struct ptn3460_bridge, connector);
62}
63
Sean Paula9fe7132014-02-24 19:31:24 +090064static int ptn3460_read_bytes(struct ptn3460_bridge *ptn_bridge, char addr,
65 u8 *buf, int len)
66{
67 int ret;
68
69 ret = i2c_master_send(ptn_bridge->client, &addr, 1);
70 if (ret <= 0) {
71 DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
72 return ret;
73 }
74
75 ret = i2c_master_recv(ptn_bridge->client, buf, len);
76 if (ret <= 0) {
77 DRM_ERROR("Failed to recv i2c data, ret=%d\n", ret);
78 return ret;
79 }
80
81 return 0;
82}
83
84static int ptn3460_write_byte(struct ptn3460_bridge *ptn_bridge, char addr,
85 char val)
86{
87 int ret;
88 char buf[2];
89
90 buf[0] = addr;
91 buf[1] = val;
92
93 ret = i2c_master_send(ptn_bridge->client, buf, ARRAY_SIZE(buf));
94 if (ret <= 0) {
95 DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
96 return ret;
97 }
98
99 return 0;
100}
101
102static int ptn3460_select_edid(struct ptn3460_bridge *ptn_bridge)
103{
104 int ret;
105 char val;
106
107 /* Load the selected edid into SRAM (accessed at PTN3460_EDID_ADDR) */
108 ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_SRAM_LOAD_ADDR,
109 ptn_bridge->edid_emulation);
110 if (ret) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530111 DRM_ERROR("Failed to transfer EDID to sram, ret=%d\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900112 return ret;
113 }
114
115 /* Enable EDID emulation and select the desired EDID */
116 val = 1 << PTN3460_EDID_ENABLE_EMULATION |
117 ptn_bridge->edid_emulation << PTN3460_EDID_EMULATION_SELECTION;
118
119 ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_EMULATION_ADDR, val);
120 if (ret) {
Ajay Kumar94d50d52015-01-20 22:08:42 +0530121 DRM_ERROR("Failed to write EDID value, ret=%d\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900122 return ret;
123 }
124
125 return 0;
126}
127
128static void ptn3460_pre_enable(struct drm_bridge *bridge)
129{
Ajay Kumar94d50d52015-01-20 22:08:42 +0530130 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900131 int ret;
132
133 if (ptn_bridge->enabled)
134 return;
135
Ajay Kumaraf478d82015-01-20 22:08:49 +0530136 gpiod_set_value(ptn_bridge->gpio_pd_n, 1);
Sean Paula9fe7132014-02-24 19:31:24 +0900137
Ajay Kumaraf478d82015-01-20 22:08:49 +0530138 gpiod_set_value(ptn_bridge->gpio_rst_n, 0);
139 usleep_range(10, 20);
140 gpiod_set_value(ptn_bridge->gpio_rst_n, 1);
Sean Paula9fe7132014-02-24 19:31:24 +0900141
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530142 if (drm_panel_prepare(ptn_bridge->panel)) {
143 DRM_ERROR("failed to prepare panel\n");
144 return;
145 }
146
Sean Paula9fe7132014-02-24 19:31:24 +0900147 /*
148 * There's a bug in the PTN chip where it falsely asserts hotplug before
149 * it is fully functional. We're forced to wait for the maximum start up
150 * time specified in the chip's datasheet to make sure we're really up.
151 */
152 msleep(90);
153
154 ret = ptn3460_select_edid(ptn_bridge);
155 if (ret)
Ajay Kumar94d50d52015-01-20 22:08:42 +0530156 DRM_ERROR("Select EDID failed ret=%d\n", ret);
Sean Paula9fe7132014-02-24 19:31:24 +0900157
158 ptn_bridge->enabled = true;
159}
160
161static void ptn3460_enable(struct drm_bridge *bridge)
162{
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530163 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
164
165 if (drm_panel_enable(ptn_bridge->panel)) {
166 DRM_ERROR("failed to enable panel\n");
167 return;
168 }
Sean Paula9fe7132014-02-24 19:31:24 +0900169}
170
171static void ptn3460_disable(struct drm_bridge *bridge)
172{
Ajay Kumar94d50d52015-01-20 22:08:42 +0530173 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900174
175 if (!ptn_bridge->enabled)
176 return;
177
178 ptn_bridge->enabled = false;
179
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530180 if (drm_panel_disable(ptn_bridge->panel)) {
181 DRM_ERROR("failed to disable panel\n");
182 return;
183 }
184
Ajay Kumaraf478d82015-01-20 22:08:49 +0530185 gpiod_set_value(ptn_bridge->gpio_rst_n, 1);
186 gpiod_set_value(ptn_bridge->gpio_pd_n, 0);
Sean Paula9fe7132014-02-24 19:31:24 +0900187}
188
189static void ptn3460_post_disable(struct drm_bridge *bridge)
190{
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530191 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
192
193 if (drm_panel_unprepare(ptn_bridge->panel)) {
194 DRM_ERROR("failed to unprepare panel\n");
195 return;
196 }
Sean Paula9fe7132014-02-24 19:31:24 +0900197}
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
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530243 return ptn_bridge->bridge.encoder;
Sean Paula9fe7132014-02-24 19:31:24 +0900244}
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
Thierry Redingdf1d3062015-01-30 12:33:58 +0100269static int ptn3460_bridge_attach(struct drm_bridge *bridge)
Sean Paula9fe7132014-02-24 19:31:24 +0900270{
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530271 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900272 int ret;
Sean Paula9fe7132014-02-24 19:31:24 +0900273
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530274 if (!bridge->encoder) {
275 DRM_ERROR("Parent encoder object not found");
276 return -ENODEV;
277 }
278
Ajay Kumara1899952015-01-20 22:08:48 +0530279 ptn_bridge->connector.polled = DRM_CONNECTOR_POLL_HPD;
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530280 ret = drm_connector_init(bridge->dev, &ptn_bridge->connector,
281 &ptn3460_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
282 if (ret) {
283 DRM_ERROR("Failed to initialize connector with drm\n");
284 return ret;
285 }
286 drm_connector_helper_add(&ptn_bridge->connector,
287 &ptn3460_connector_helper_funcs);
288 drm_connector_register(&ptn_bridge->connector);
289 drm_mode_connector_attach_encoder(&ptn_bridge->connector,
290 bridge->encoder);
291
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530292 if (ptn_bridge->panel)
293 drm_panel_attach(ptn_bridge->panel, &ptn_bridge->connector);
294
Ajay Kumara1899952015-01-20 22:08:48 +0530295 drm_helper_hpd_irq_event(ptn_bridge->connector.dev);
296
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530297 return ret;
298}
299
300static struct drm_bridge_funcs ptn3460_bridge_funcs = {
301 .pre_enable = ptn3460_pre_enable,
302 .enable = ptn3460_enable,
303 .disable = ptn3460_disable,
304 .post_disable = ptn3460_post_disable,
305 .attach = ptn3460_bridge_attach,
306};
307
308static int ptn3460_probe(struct i2c_client *client,
309 const struct i2c_device_id *id)
310{
311 struct device *dev = &client->dev;
312 struct ptn3460_bridge *ptn_bridge;
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530313 struct device_node *endpoint, *panel_node;
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530314 int ret;
315
316 ptn_bridge = devm_kzalloc(dev, sizeof(*ptn_bridge), GFP_KERNEL);
Sean Paula9fe7132014-02-24 19:31:24 +0900317 if (!ptn_bridge) {
Sean Paula9fe7132014-02-24 19:31:24 +0900318 return -ENOMEM;
319 }
320
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530321 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
322 if (endpoint) {
323 panel_node = of_graph_get_remote_port_parent(endpoint);
324 if (panel_node) {
325 ptn_bridge->panel = of_drm_find_panel(panel_node);
326 of_node_put(panel_node);
327 if (!ptn_bridge->panel)
328 return -EPROBE_DEFER;
329 }
330 }
331
Sean Paula9fe7132014-02-24 19:31:24 +0900332 ptn_bridge->client = client;
Ajay Kumaraf478d82015-01-20 22:08:49 +0530333
Uwe Kleine-Königc06ea0a2015-05-19 09:15:56 +0200334 ptn_bridge->gpio_pd_n = devm_gpiod_get(&client->dev, "powerdown",
335 GPIOD_OUT_HIGH);
Ajay Kumaraf478d82015-01-20 22:08:49 +0530336 if (IS_ERR(ptn_bridge->gpio_pd_n)) {
337 ret = PTR_ERR(ptn_bridge->gpio_pd_n);
338 dev_err(dev, "cannot get gpio_pd_n %d\n", ret);
339 return ret;
Sean Paula9fe7132014-02-24 19:31:24 +0900340 }
341
Ajay Kumaraf478d82015-01-20 22:08:49 +0530342 /*
343 * Request the reset pin low to avoid the bridge being
344 * initialized prematurely
345 */
Uwe Kleine-Königc06ea0a2015-05-19 09:15:56 +0200346 ptn_bridge->gpio_rst_n = devm_gpiod_get(&client->dev, "reset",
347 GPIOD_OUT_LOW);
348 if (IS_ERR(ptn_bridge->gpio_rst_n)) {
349 ret = PTR_ERR(ptn_bridge->gpio_rst_n);
350 DRM_ERROR("cannot get gpio_rst_n %d\n", ret);
Ajay Kumaraf478d82015-01-20 22:08:49 +0530351 return ret;
Sean Paula9fe7132014-02-24 19:31:24 +0900352 }
353
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530354 ret = of_property_read_u32(dev->of_node, "edid-emulation",
Sean Paula9fe7132014-02-24 19:31:24 +0900355 &ptn_bridge->edid_emulation);
356 if (ret) {
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530357 dev_err(dev, "Can't read EDID emulation value\n");
Ajay Kumaraf478d82015-01-20 22:08:49 +0530358 return ret;
Sean Paula9fe7132014-02-24 19:31:24 +0900359 }
360
Ajay Kumarb07b90f2015-01-20 22:08:43 +0530361 ptn_bridge->bridge.funcs = &ptn3460_bridge_funcs;
Ajay Kumar5bbb9a22015-01-20 22:08:47 +0530362 ptn_bridge->bridge.of_node = dev->of_node;
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530363 ret = drm_bridge_add(&ptn_bridge->bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900364 if (ret) {
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530365 DRM_ERROR("Failed to add bridge\n");
Ajay Kumaraf478d82015-01-20 22:08:49 +0530366 return ret;
Sean Paula9fe7132014-02-24 19:31:24 +0900367 }
368
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530369 i2c_set_clientdata(client, ptn_bridge);
Sean Paula9fe7132014-02-24 19:31:24 +0900370
371 return 0;
Sean Paula9fe7132014-02-24 19:31:24 +0900372}
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530373
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530374static int ptn3460_remove(struct i2c_client *client)
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530375{
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530376 struct ptn3460_bridge *ptn_bridge = i2c_get_clientdata(client);
377
378 drm_bridge_remove(&ptn_bridge->bridge);
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530379
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530380 return 0;
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530381}
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530382
383static const struct i2c_device_id ptn3460_i2c_table[] = {
Javier Martinez Canillas394e5b62015-05-14 16:31:29 +0200384 {"ptn3460", 0},
Ajay Kumar6a1688a2015-01-20 22:08:45 +0530385 {},
386};
387MODULE_DEVICE_TABLE(i2c, ptn3460_i2c_table);
388
389static const struct of_device_id ptn3460_match[] = {
390 { .compatible = "nxp,ptn3460" },
391 {},
392};
393MODULE_DEVICE_TABLE(of, ptn3460_match);
394
395static struct i2c_driver ptn3460_driver = {
396 .id_table = ptn3460_i2c_table,
397 .probe = ptn3460_probe,
398 .remove = ptn3460_remove,
399 .driver = {
400 .name = "nxp,ptn3460",
401 .owner = THIS_MODULE,
402 .of_match_table = ptn3460_match,
403 },
404};
405module_i2c_driver(ptn3460_driver);
406
407MODULE_AUTHOR("Sean Paul <seanpaul@chromium.org>");
408MODULE_DESCRIPTION("NXP ptn3460 eDP-LVDS converter driver");
409MODULE_LICENSE("GPL v2");