hyperv: Remove recv_pkt_list and lock
[linux/fpc-iii.git] / drivers / gpu / drm / bridge / ptn3460.c
blobb171901a35538d7a958e8289df8631f29ae53a47
1 /*
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>
17 #include <linux/of.h>
18 #include <linux/of_gpio.h>
19 #include <linux/i2c.h>
20 #include <linux/gpio.h>
21 #include <linux/delay.h>
23 #include "drmP.h"
24 #include "drm_edid.h"
25 #include "drm_crtc.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;
41 struct edid *edid;
42 int gpio_pd_n;
43 int gpio_rst_n;
44 u32 edid_emulation;
45 bool enabled;
48 static int ptn3460_read_bytes(struct ptn3460_bridge *ptn_bridge, char addr,
49 u8 *buf, int len)
51 int ret;
53 ret = i2c_master_send(ptn_bridge->client, &addr, 1);
54 if (ret <= 0) {
55 DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
56 return ret;
59 ret = i2c_master_recv(ptn_bridge->client, buf, len);
60 if (ret <= 0) {
61 DRM_ERROR("Failed to recv i2c data, ret=%d\n", ret);
62 return ret;
65 return 0;
68 static int ptn3460_write_byte(struct ptn3460_bridge *ptn_bridge, char addr,
69 char val)
71 int ret;
72 char buf[2];
74 buf[0] = addr;
75 buf[1] = val;
77 ret = i2c_master_send(ptn_bridge->client, buf, ARRAY_SIZE(buf));
78 if (ret <= 0) {
79 DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
80 return ret;
83 return 0;
86 static int ptn3460_select_edid(struct ptn3460_bridge *ptn_bridge)
88 int ret;
89 char val;
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);
94 if (ret) {
95 DRM_ERROR("Failed to transfer edid to sram, ret=%d\n", ret);
96 return 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);
104 if (ret) {
105 DRM_ERROR("Failed to write edid value, ret=%d\n", ret);
106 return ret;
109 return 0;
112 static void ptn3460_pre_enable(struct drm_bridge *bridge)
114 struct ptn3460_bridge *ptn_bridge = bridge->driver_private;
115 int ret;
117 if (ptn_bridge->enabled)
118 return;
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);
125 udelay(10);
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.
134 msleep(90);
136 ret = ptn3460_select_edid(ptn_bridge);
137 if (ret)
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)
152 return;
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;
190 u8 *edid;
191 int ret, num_modes;
192 bool power_off;
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);
203 if (!edid) {
204 DRM_ERROR("Failed to allocate edid\n");
205 return 0;
208 ret = ptn3460_read_bytes(ptn_bridge, PTN3460_EDID_ADDR, edid,
209 EDID_LENGTH);
210 if (ret) {
211 kfree(edid);
212 num_modes = 0;
213 goto out;
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);
221 out:
222 if (power_off)
223 ptn3460_disable(ptn_bridge->bridge);
225 return num_modes;
228 static int ptn3460_mode_valid(struct drm_connector *connector,
229 struct drm_display_mode *mode)
231 return MODE_OK;
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,
250 bool force)
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)
270 int ret;
271 struct drm_bridge *bridge;
272 struct ptn3460_bridge *ptn_bridge;
274 bridge = devm_kzalloc(dev->dev, sizeof(*bridge), GFP_KERNEL);
275 if (!bridge) {
276 DRM_ERROR("Failed to allocate drm bridge\n");
277 return -ENOMEM;
280 ptn_bridge = devm_kzalloc(dev->dev, sizeof(*ptn_bridge), GFP_KERNEL);
281 if (!ptn_bridge) {
282 DRM_ERROR("Failed to allocate ptn bridge\n");
283 return -ENOMEM;
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");
293 if (ret) {
294 DRM_ERROR("Request powerdown-gpio failed (%d)\n", ret);
295 return 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");
307 if (ret) {
308 DRM_ERROR("Request reset-gpio failed (%d)\n", ret);
309 gpio_free(ptn_bridge->gpio_pd_n);
310 return ret;
314 ret = of_property_read_u32(node, "edid-emulation",
315 &ptn_bridge->edid_emulation);
316 if (ret) {
317 DRM_ERROR("Can't read edid emulation value\n");
318 goto err;
321 ret = drm_bridge_init(dev, bridge, &ptn3460_bridge_funcs);
322 if (ret) {
323 DRM_ERROR("Failed to initialize bridge with drm\n");
324 goto err;
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);
332 if (ret) {
333 DRM_ERROR("Failed to initialize connector with drm\n");
334 goto err;
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);
341 return 0;
343 err:
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);
348 return ret;
350 EXPORT_SYMBOL(ptn3460_init);