2 * NXP PTN3460 DP/LVDS bridge driver
4 * Copyright (C) 2013 Google, Inc.
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.
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.
16 #include <linux/module.h>
18 #include <linux/of_gpio.h>
19 #include <linux/i2c.h>
20 #include <linux/gpio.h>
21 #include <linux/delay.h>
26 #include "drm_crtc_helper.h"
28 #include "bridge/ptn3460.h"
30 #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
36 struct ptn3460_bridge
{
37 struct drm_connector connector
;
38 struct i2c_client
*client
;
39 struct drm_encoder
*encoder
;
40 struct drm_bridge
*bridge
;
48 static int ptn3460_read_bytes(struct ptn3460_bridge
*ptn_bridge
, char addr
,
53 ret
= i2c_master_send(ptn_bridge
->client
, &addr
, 1);
55 DRM_ERROR("Failed to send i2c command, ret=%d\n", ret
);
59 ret
= i2c_master_recv(ptn_bridge
->client
, buf
, len
);
61 DRM_ERROR("Failed to recv i2c data, ret=%d\n", ret
);
68 static int ptn3460_write_byte(struct ptn3460_bridge
*ptn_bridge
, char addr
,
77 ret
= i2c_master_send(ptn_bridge
->client
, buf
, ARRAY_SIZE(buf
));
79 DRM_ERROR("Failed to send i2c command, ret=%d\n", ret
);
86 static int ptn3460_select_edid(struct ptn3460_bridge
*ptn_bridge
)
91 /* Load the selected edid into SRAM (accessed at PTN3460_EDID_ADDR) */
92 ret
= ptn3460_write_byte(ptn_bridge
, PTN3460_EDID_SRAM_LOAD_ADDR
,
93 ptn_bridge
->edid_emulation
);
95 DRM_ERROR("Failed to transfer edid to sram, ret=%d\n", ret
);
99 /* Enable EDID emulation and select the desired EDID */
100 val
= 1 << PTN3460_EDID_ENABLE_EMULATION
|
101 ptn_bridge
->edid_emulation
<< PTN3460_EDID_EMULATION_SELECTION
;
103 ret
= ptn3460_write_byte(ptn_bridge
, PTN3460_EDID_EMULATION_ADDR
, val
);
105 DRM_ERROR("Failed to write edid value, ret=%d\n", ret
);
112 static void ptn3460_pre_enable(struct drm_bridge
*bridge
)
114 struct ptn3460_bridge
*ptn_bridge
= bridge
->driver_private
;
117 if (ptn_bridge
->enabled
)
120 if (gpio_is_valid(ptn_bridge
->gpio_pd_n
))
121 gpio_set_value(ptn_bridge
->gpio_pd_n
, 1);
123 if (gpio_is_valid(ptn_bridge
->gpio_rst_n
)) {
124 gpio_set_value(ptn_bridge
->gpio_rst_n
, 0);
126 gpio_set_value(ptn_bridge
->gpio_rst_n
, 1);
130 * There's a bug in the PTN chip where it falsely asserts hotplug before
131 * it is fully functional. We're forced to wait for the maximum start up
132 * time specified in the chip's datasheet to make sure we're really up.
136 ret
= ptn3460_select_edid(ptn_bridge
);
138 DRM_ERROR("Select edid failed ret=%d\n", ret
);
140 ptn_bridge
->enabled
= true;
143 static void ptn3460_enable(struct drm_bridge
*bridge
)
147 static void ptn3460_disable(struct drm_bridge
*bridge
)
149 struct ptn3460_bridge
*ptn_bridge
= bridge
->driver_private
;
151 if (!ptn_bridge
->enabled
)
154 ptn_bridge
->enabled
= false;
156 if (gpio_is_valid(ptn_bridge
->gpio_rst_n
))
157 gpio_set_value(ptn_bridge
->gpio_rst_n
, 1);
159 if (gpio_is_valid(ptn_bridge
->gpio_pd_n
))
160 gpio_set_value(ptn_bridge
->gpio_pd_n
, 0);
163 static void ptn3460_post_disable(struct drm_bridge
*bridge
)
167 void ptn3460_bridge_destroy(struct drm_bridge
*bridge
)
169 struct ptn3460_bridge
*ptn_bridge
= bridge
->driver_private
;
171 drm_bridge_cleanup(bridge
);
172 if (gpio_is_valid(ptn_bridge
->gpio_pd_n
))
173 gpio_free(ptn_bridge
->gpio_pd_n
);
174 if (gpio_is_valid(ptn_bridge
->gpio_rst_n
))
175 gpio_free(ptn_bridge
->gpio_rst_n
);
176 /* Nothing else to free, we've got devm allocated memory */
179 struct drm_bridge_funcs ptn3460_bridge_funcs
= {
180 .pre_enable
= ptn3460_pre_enable
,
181 .enable
= ptn3460_enable
,
182 .disable
= ptn3460_disable
,
183 .post_disable
= ptn3460_post_disable
,
184 .destroy
= ptn3460_bridge_destroy
,
187 int ptn3460_get_modes(struct drm_connector
*connector
)
189 struct ptn3460_bridge
*ptn_bridge
;
194 ptn_bridge
= container_of(connector
, struct ptn3460_bridge
, connector
);
196 if (ptn_bridge
->edid
)
197 return drm_add_edid_modes(connector
, ptn_bridge
->edid
);
199 power_off
= !ptn_bridge
->enabled
;
200 ptn3460_pre_enable(ptn_bridge
->bridge
);
202 edid
= kmalloc(EDID_LENGTH
, GFP_KERNEL
);
204 DRM_ERROR("Failed to allocate edid\n");
208 ret
= ptn3460_read_bytes(ptn_bridge
, PTN3460_EDID_ADDR
, edid
,
216 ptn_bridge
->edid
= (struct edid
*)edid
;
217 drm_mode_connector_update_edid_property(connector
, ptn_bridge
->edid
);
219 num_modes
= drm_add_edid_modes(connector
, ptn_bridge
->edid
);
223 ptn3460_disable(ptn_bridge
->bridge
);
228 static int ptn3460_mode_valid(struct drm_connector
*connector
,
229 struct drm_display_mode
*mode
)
234 struct drm_encoder
*ptn3460_best_encoder(struct drm_connector
*connector
)
236 struct ptn3460_bridge
*ptn_bridge
;
238 ptn_bridge
= container_of(connector
, struct ptn3460_bridge
, connector
);
240 return ptn_bridge
->encoder
;
243 struct drm_connector_helper_funcs ptn3460_connector_helper_funcs
= {
244 .get_modes
= ptn3460_get_modes
,
245 .mode_valid
= ptn3460_mode_valid
,
246 .best_encoder
= ptn3460_best_encoder
,
249 enum drm_connector_status
ptn3460_detect(struct drm_connector
*connector
,
252 return connector_status_connected
;
255 void ptn3460_connector_destroy(struct drm_connector
*connector
)
257 drm_connector_cleanup(connector
);
260 struct drm_connector_funcs ptn3460_connector_funcs
= {
261 .dpms
= drm_helper_connector_dpms
,
262 .fill_modes
= drm_helper_probe_single_connector_modes
,
263 .detect
= ptn3460_detect
,
264 .destroy
= ptn3460_connector_destroy
,
267 int ptn3460_init(struct drm_device
*dev
, struct drm_encoder
*encoder
,
268 struct i2c_client
*client
, struct device_node
*node
)
271 struct drm_bridge
*bridge
;
272 struct ptn3460_bridge
*ptn_bridge
;
274 bridge
= devm_kzalloc(dev
->dev
, sizeof(*bridge
), GFP_KERNEL
);
276 DRM_ERROR("Failed to allocate drm bridge\n");
280 ptn_bridge
= devm_kzalloc(dev
->dev
, sizeof(*ptn_bridge
), GFP_KERNEL
);
282 DRM_ERROR("Failed to allocate ptn bridge\n");
286 ptn_bridge
->client
= client
;
287 ptn_bridge
->encoder
= encoder
;
288 ptn_bridge
->bridge
= bridge
;
289 ptn_bridge
->gpio_pd_n
= of_get_named_gpio(node
, "powerdown-gpio", 0);
290 if (gpio_is_valid(ptn_bridge
->gpio_pd_n
)) {
291 ret
= gpio_request_one(ptn_bridge
->gpio_pd_n
,
292 GPIOF_OUT_INIT_HIGH
, "PTN3460_PD_N");
294 DRM_ERROR("Request powerdown-gpio failed (%d)\n", ret
);
299 ptn_bridge
->gpio_rst_n
= of_get_named_gpio(node
, "reset-gpio", 0);
300 if (gpio_is_valid(ptn_bridge
->gpio_rst_n
)) {
302 * Request the reset pin low to avoid the bridge being
303 * initialized prematurely
305 ret
= gpio_request_one(ptn_bridge
->gpio_rst_n
,
306 GPIOF_OUT_INIT_LOW
, "PTN3460_RST_N");
308 DRM_ERROR("Request reset-gpio failed (%d)\n", ret
);
309 gpio_free(ptn_bridge
->gpio_pd_n
);
314 ret
= of_property_read_u32(node
, "edid-emulation",
315 &ptn_bridge
->edid_emulation
);
317 DRM_ERROR("Can't read edid emulation value\n");
321 ret
= drm_bridge_init(dev
, bridge
, &ptn3460_bridge_funcs
);
323 DRM_ERROR("Failed to initialize bridge with drm\n");
327 bridge
->driver_private
= ptn_bridge
;
328 encoder
->bridge
= bridge
;
330 ret
= drm_connector_init(dev
, &ptn_bridge
->connector
,
331 &ptn3460_connector_funcs
, DRM_MODE_CONNECTOR_LVDS
);
333 DRM_ERROR("Failed to initialize connector with drm\n");
336 drm_connector_helper_add(&ptn_bridge
->connector
,
337 &ptn3460_connector_helper_funcs
);
338 drm_sysfs_connector_add(&ptn_bridge
->connector
);
339 drm_mode_connector_attach_encoder(&ptn_bridge
->connector
, encoder
);
344 if (gpio_is_valid(ptn_bridge
->gpio_pd_n
))
345 gpio_free(ptn_bridge
->gpio_pd_n
);
346 if (gpio_is_valid(ptn_bridge
->gpio_rst_n
))
347 gpio_free(ptn_bridge
->gpio_rst_n
);
350 EXPORT_SYMBOL(ptn3460_init
);