ptn3460.c 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * NXP PTN3460 DP/LVDS bridge driver
 *
 * Copyright (C) 2013 Google, Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

16 17 18
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
19 20 21 22
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>

23 24
#include "bridge/ptn3460.h"

25 26
#include "drm_crtc.h"
#include "drm_crtc_helper.h"
27 28
#include "drm_edid.h"
#include "drmP.h"
29 30 31 32 33 34 35 36 37 38 39

#define PTN3460_EDID_ADDR			0x0
#define PTN3460_EDID_EMULATION_ADDR		0x84
#define PTN3460_EDID_ENABLE_EMULATION		0
#define PTN3460_EDID_EMULATION_SELECTION	1
#define PTN3460_EDID_SRAM_LOAD_ADDR		0x85

struct ptn3460_bridge {
	struct drm_connector connector;
	struct i2c_client *client;
	struct drm_encoder *encoder;
40
	struct drm_bridge bridge;
41 42 43 44 45 46 47
	struct edid *edid;
	int gpio_pd_n;
	int gpio_rst_n;
	u32 edid_emulation;
	bool enabled;
};

48 49 50 51 52 53 54 55 56 57 58 59
static inline struct ptn3460_bridge *
		bridge_to_ptn3460(struct drm_bridge *bridge)
{
	return container_of(bridge, struct ptn3460_bridge, bridge);
}

static inline struct ptn3460_bridge *
		connector_to_ptn3460(struct drm_connector *connector)
{
	return container_of(connector, struct ptn3460_bridge, connector);
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
static int ptn3460_read_bytes(struct ptn3460_bridge *ptn_bridge, char addr,
		u8 *buf, int len)
{
	int ret;

	ret = i2c_master_send(ptn_bridge->client, &addr, 1);
	if (ret <= 0) {
		DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
		return ret;
	}

	ret = i2c_master_recv(ptn_bridge->client, buf, len);
	if (ret <= 0) {
		DRM_ERROR("Failed to recv i2c data, ret=%d\n", ret);
		return ret;
	}

	return 0;
}

static int ptn3460_write_byte(struct ptn3460_bridge *ptn_bridge, char addr,
		char val)
{
	int ret;
	char buf[2];

	buf[0] = addr;
	buf[1] = val;

	ret = i2c_master_send(ptn_bridge->client, buf, ARRAY_SIZE(buf));
	if (ret <= 0) {
		DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
		return ret;
	}

	return 0;
}

static int ptn3460_select_edid(struct ptn3460_bridge *ptn_bridge)
{
	int ret;
	char val;

	/* Load the selected edid into SRAM (accessed at PTN3460_EDID_ADDR) */
	ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_SRAM_LOAD_ADDR,
			ptn_bridge->edid_emulation);
	if (ret) {
107
		DRM_ERROR("Failed to transfer EDID to sram, ret=%d\n", ret);
108 109 110 111 112 113 114 115 116
		return ret;
	}

	/* Enable EDID emulation and select the desired EDID */
	val = 1 << PTN3460_EDID_ENABLE_EMULATION |
		ptn_bridge->edid_emulation << PTN3460_EDID_EMULATION_SELECTION;

	ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_EMULATION_ADDR, val);
	if (ret) {
117
		DRM_ERROR("Failed to write EDID value, ret=%d\n", ret);
118 119 120 121 122 123 124 125
		return ret;
	}

	return 0;
}

static void ptn3460_pre_enable(struct drm_bridge *bridge)
{
126
	struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
127 128 129 130 131 132 133 134 135 136
	int ret;

	if (ptn_bridge->enabled)
		return;

	if (gpio_is_valid(ptn_bridge->gpio_pd_n))
		gpio_set_value(ptn_bridge->gpio_pd_n, 1);

	if (gpio_is_valid(ptn_bridge->gpio_rst_n)) {
		gpio_set_value(ptn_bridge->gpio_rst_n, 0);
137
		usleep_range(10, 20);
138 139 140 141 142 143 144 145 146 147 148 149
		gpio_set_value(ptn_bridge->gpio_rst_n, 1);
	}

	/*
	 * There's a bug in the PTN chip where it falsely asserts hotplug before
	 * it is fully functional. We're forced to wait for the maximum start up
	 * time specified in the chip's datasheet to make sure we're really up.
	 */
	msleep(90);

	ret = ptn3460_select_edid(ptn_bridge);
	if (ret)
150
		DRM_ERROR("Select EDID failed ret=%d\n", ret);
151 152 153 154 155 156 157 158 159 160

	ptn_bridge->enabled = true;
}

static void ptn3460_enable(struct drm_bridge *bridge)
{
}

static void ptn3460_disable(struct drm_bridge *bridge)
{
161
	struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

	if (!ptn_bridge->enabled)
		return;

	ptn_bridge->enabled = false;

	if (gpio_is_valid(ptn_bridge->gpio_rst_n))
		gpio_set_value(ptn_bridge->gpio_rst_n, 1);

	if (gpio_is_valid(ptn_bridge->gpio_pd_n))
		gpio_set_value(ptn_bridge->gpio_pd_n, 0);
}

static void ptn3460_post_disable(struct drm_bridge *bridge)
{
}

179
static void ptn3460_bridge_destroy(struct drm_bridge *bridge)
180
{
181
	struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
182 183 184 185 186 187 188 189 190

	drm_bridge_cleanup(bridge);
	if (gpio_is_valid(ptn_bridge->gpio_pd_n))
		gpio_free(ptn_bridge->gpio_pd_n);
	if (gpio_is_valid(ptn_bridge->gpio_rst_n))
		gpio_free(ptn_bridge->gpio_rst_n);
	/* Nothing else to free, we've got devm allocated memory */
}

191
static struct drm_bridge_funcs ptn3460_bridge_funcs = {
192 193 194 195 196 197 198
	.pre_enable = ptn3460_pre_enable,
	.enable = ptn3460_enable,
	.disable = ptn3460_disable,
	.post_disable = ptn3460_post_disable,
	.destroy = ptn3460_bridge_destroy,
};

199
static int ptn3460_get_modes(struct drm_connector *connector)
200 201 202
{
	struct ptn3460_bridge *ptn_bridge;
	u8 *edid;
203
	int ret, num_modes = 0;
204 205
	bool power_off;

206
	ptn_bridge = connector_to_ptn3460(connector);
207 208 209 210 211

	if (ptn_bridge->edid)
		return drm_add_edid_modes(connector, ptn_bridge->edid);

	power_off = !ptn_bridge->enabled;
212
	ptn3460_pre_enable(&ptn_bridge->bridge);
213 214 215

	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
	if (!edid) {
216
		DRM_ERROR("Failed to allocate EDID\n");
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
		return 0;
	}

	ret = ptn3460_read_bytes(ptn_bridge, PTN3460_EDID_ADDR, edid,
			EDID_LENGTH);
	if (ret) {
		kfree(edid);
		goto out;
	}

	ptn_bridge->edid = (struct edid *)edid;
	drm_mode_connector_update_edid_property(connector, ptn_bridge->edid);

	num_modes = drm_add_edid_modes(connector, ptn_bridge->edid);

out:
	if (power_off)
234
		ptn3460_disable(&ptn_bridge->bridge);
235 236 237 238

	return num_modes;
}

239
static struct drm_encoder *ptn3460_best_encoder(struct drm_connector *connector)
240
{
241
	struct ptn3460_bridge *ptn_bridge = connector_to_ptn3460(connector);
242 243 244 245

	return ptn_bridge->encoder;
}

246
static struct drm_connector_helper_funcs ptn3460_connector_helper_funcs = {
247 248 249 250
	.get_modes = ptn3460_get_modes,
	.best_encoder = ptn3460_best_encoder,
};

251
static enum drm_connector_status ptn3460_detect(struct drm_connector *connector,
252 253 254 255 256
		bool force)
{
	return connector_status_connected;
}

257
static void ptn3460_connector_destroy(struct drm_connector *connector)
258 259 260 261
{
	drm_connector_cleanup(connector);
}

262
static struct drm_connector_funcs ptn3460_connector_funcs = {
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	.dpms = drm_helper_connector_dpms,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.detect = ptn3460_detect,
	.destroy = ptn3460_connector_destroy,
};

int ptn3460_init(struct drm_device *dev, struct drm_encoder *encoder,
		struct i2c_client *client, struct device_node *node)
{
	int ret;
	struct ptn3460_bridge *ptn_bridge;

	ptn_bridge = devm_kzalloc(dev->dev, sizeof(*ptn_bridge), GFP_KERNEL);
	if (!ptn_bridge) {
		return -ENOMEM;
	}

	ptn_bridge->client = client;
	ptn_bridge->encoder = encoder;
	ptn_bridge->gpio_pd_n = of_get_named_gpio(node, "powerdown-gpio", 0);
	if (gpio_is_valid(ptn_bridge->gpio_pd_n)) {
		ret = gpio_request_one(ptn_bridge->gpio_pd_n,
				GPIOF_OUT_INIT_HIGH, "PTN3460_PD_N");
		if (ret) {
287 288
			dev_err(&client->dev,
				"Request powerdown-gpio failed (%d)\n", ret);
289 290 291 292 293 294 295 296 297 298 299 300 301
			return ret;
		}
	}

	ptn_bridge->gpio_rst_n = of_get_named_gpio(node, "reset-gpio", 0);
	if (gpio_is_valid(ptn_bridge->gpio_rst_n)) {
		/*
		 * Request the reset pin low to avoid the bridge being
		 * initialized prematurely
		 */
		ret = gpio_request_one(ptn_bridge->gpio_rst_n,
				GPIOF_OUT_INIT_LOW, "PTN3460_RST_N");
		if (ret) {
302 303
			dev_err(&client->dev,
				"Request reset-gpio failed (%d)\n", ret);
304 305 306 307 308 309 310 311
			gpio_free(ptn_bridge->gpio_pd_n);
			return ret;
		}
	}

	ret = of_property_read_u32(node, "edid-emulation",
			&ptn_bridge->edid_emulation);
	if (ret) {
312
		dev_err(&client->dev, "Can't read EDID emulation value\n");
313 314 315
		goto err;
	}

316
	ret = drm_bridge_init(dev, &ptn_bridge->bridge, &ptn3460_bridge_funcs);
317 318 319 320 321
	if (ret) {
		DRM_ERROR("Failed to initialize bridge with drm\n");
		goto err;
	}

322
	encoder->bridge = &ptn_bridge->bridge;
323 324 325 326 327 328 329 330 331

	ret = drm_connector_init(dev, &ptn_bridge->connector,
			&ptn3460_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
	if (ret) {
		DRM_ERROR("Failed to initialize connector with drm\n");
		goto err;
	}
	drm_connector_helper_add(&ptn_bridge->connector,
			&ptn3460_connector_helper_funcs);
332
	drm_connector_register(&ptn_bridge->connector);
333 334 335 336 337 338 339 340 341 342 343
	drm_mode_connector_attach_encoder(&ptn_bridge->connector, encoder);

	return 0;

err:
	if (gpio_is_valid(ptn_bridge->gpio_pd_n))
		gpio_free(ptn_bridge->gpio_pd_n);
	if (gpio_is_valid(ptn_bridge->gpio_rst_n))
		gpio_free(ptn_bridge->gpio_rst_n);
	return ret;
}
344
EXPORT_SYMBOL(ptn3460_init);