panel-tfp410.c 7.9 KB
Newer Older
1
/*
2
 * TFP410 DPI-to-DVI chip
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Copyright (C) 2011 Texas Instruments Inc
 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/module.h>
#include <linux/slab.h>
#include <video/omapdss.h>
#include <linux/i2c.h>
24
#include <linux/gpio.h>
25 26
#include <drm/drm_edid.h>

27
#include <video/omap-panel-tfp410.h>
28

29
static const struct omap_video_timings tfp410_default_timings = {
30 31 32 33 34 35 36 37 38 39 40 41
	.x_res		= 640,
	.y_res		= 480,

	.pixel_clock	= 23500,

	.hfp		= 48,
	.hsw		= 32,
	.hbp		= 80,

	.vfp		= 3,
	.vsw		= 4,
	.vbp		= 7,
42 43 44 45 46 47

	.vsync_level	= OMAPDSS_SIG_ACTIVE_HIGH,
	.hsync_level	= OMAPDSS_SIG_ACTIVE_HIGH,
	.data_pclk_edge	= OMAPDSS_DRIVE_SIG_RISING_EDGE,
	.de_level	= OMAPDSS_SIG_ACTIVE_HIGH,
	.sync_pclk_edge	= OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES,
48 49 50 51 52 53
};

struct panel_drv_data {
	struct omap_dss_device *dssdev;

	struct mutex lock;
54 55

	int pd_gpio;
56

T
Tomi Valkeinen 已提交
57 58
	struct i2c_adapter *i2c_adapter;
};
59

60
static int tfp410_power_on(struct omap_dss_device *dssdev)
61
{
62
	struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
63 64 65 66 67
	int r;

	if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
		return 0;

68
	omapdss_dpi_set_timings(dssdev, &dssdev->panel.timings);
69
	omapdss_dpi_set_data_lines(dssdev, dssdev->phy.dpi.data_lines);
70

71 72 73 74
	r = omapdss_dpi_display_enable(dssdev);
	if (r)
		goto err0;

75
	if (gpio_is_valid(ddata->pd_gpio))
76
		gpio_set_value_cansleep(ddata->pd_gpio, 1);
77

78 79 80 81 82
	return 0;
err0:
	return r;
}

83
static void tfp410_power_off(struct omap_dss_device *dssdev)
84
{
85
	struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
86 87 88 89

	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
		return;

90
	if (gpio_is_valid(ddata->pd_gpio))
91
		gpio_set_value_cansleep(ddata->pd_gpio, 0);
92

93 94 95
	omapdss_dpi_display_disable(dssdev);
}

96
static int tfp410_probe(struct omap_dss_device *dssdev)
97 98
{
	struct panel_drv_data *ddata;
99
	int r;
T
Tomi Valkeinen 已提交
100
	int i2c_bus_num;
101

T
Tomi Valkeinen 已提交
102
	ddata = devm_kzalloc(&dssdev->dev, sizeof(*ddata), GFP_KERNEL);
103 104 105
	if (!ddata)
		return -ENOMEM;

106
	dssdev->panel.timings = tfp410_default_timings;
107 108 109 110

	ddata->dssdev = dssdev;
	mutex_init(&ddata->lock);

T
Tomi Valkeinen 已提交
111 112 113
	if (dssdev->data) {
		struct tfp410_platform_data *pdata = dssdev->data;

114
		ddata->pd_gpio = pdata->power_down_gpio;
T
Tomi Valkeinen 已提交
115 116
		i2c_bus_num = pdata->i2c_bus_num;
	} else {
117
		ddata->pd_gpio = -1;
T
Tomi Valkeinen 已提交
118 119
		i2c_bus_num = -1;
	}
120 121 122 123 124 125 126

	if (gpio_is_valid(ddata->pd_gpio)) {
		r = gpio_request_one(ddata->pd_gpio, GPIOF_OUT_INIT_LOW,
				"tfp410 pd");
		if (r) {
			dev_err(&dssdev->dev, "Failed to request PD GPIO %d\n",
					ddata->pd_gpio);
T
Tomi Valkeinen 已提交
127
			return r;
128 129 130
		}
	}

T
Tomi Valkeinen 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144
	if (i2c_bus_num != -1) {
		struct i2c_adapter *adapter;

		adapter = i2c_get_adapter(i2c_bus_num);
		if (!adapter) {
			dev_err(&dssdev->dev, "Failed to get I2C adapter, bus %d\n",
					i2c_bus_num);
			r = -EINVAL;
			goto err_i2c;
		}

		ddata->i2c_adapter = adapter;
	}

145 146 147
	dev_set_drvdata(&dssdev->dev, ddata);

	return 0;
T
Tomi Valkeinen 已提交
148 149 150 151
err_i2c:
	if (gpio_is_valid(ddata->pd_gpio))
		gpio_free(ddata->pd_gpio);
	return r;
152 153
}

154
static void __exit tfp410_remove(struct omap_dss_device *dssdev)
155 156 157 158 159
{
	struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);

	mutex_lock(&ddata->lock);

T
Tomi Valkeinen 已提交
160 161 162
	if (ddata->i2c_adapter)
		i2c_put_adapter(ddata->i2c_adapter);

163 164 165
	if (gpio_is_valid(ddata->pd_gpio))
		gpio_free(ddata->pd_gpio);

166 167 168 169 170
	dev_set_drvdata(&dssdev->dev, NULL);

	mutex_unlock(&ddata->lock);
}

171
static int tfp410_enable(struct omap_dss_device *dssdev)
172 173 174 175 176 177
{
	struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
	int r;

	mutex_lock(&ddata->lock);

178
	r = tfp410_power_on(dssdev);
179 180 181 182 183 184 185 186
	if (r == 0)
		dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;

	mutex_unlock(&ddata->lock);

	return r;
}

187
static void tfp410_disable(struct omap_dss_device *dssdev)
188 189 190 191 192
{
	struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);

	mutex_lock(&ddata->lock);

193
	tfp410_power_off(dssdev);
194 195 196 197 198 199

	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;

	mutex_unlock(&ddata->lock);
}

200
static int tfp410_suspend(struct omap_dss_device *dssdev)
201 202 203 204 205
{
	struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);

	mutex_lock(&ddata->lock);

206
	tfp410_power_off(dssdev);
207 208 209 210 211 212 213 214

	dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;

	mutex_unlock(&ddata->lock);

	return 0;
}

215
static int tfp410_resume(struct omap_dss_device *dssdev)
216 217 218 219 220 221
{
	struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
	int r;

	mutex_lock(&ddata->lock);

222
	r = tfp410_power_on(dssdev);
223 224 225 226 227 228 229 230
	if (r == 0)
		dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;

	mutex_unlock(&ddata->lock);

	return r;
}

231
static void tfp410_set_timings(struct omap_dss_device *dssdev,
232 233 234 235 236
		struct omap_video_timings *timings)
{
	struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);

	mutex_lock(&ddata->lock);
237
	omapdss_dpi_set_timings(dssdev, timings);
238
	dssdev->panel.timings = *timings;
239 240 241
	mutex_unlock(&ddata->lock);
}

242
static void tfp410_get_timings(struct omap_dss_device *dssdev,
243 244 245 246 247 248 249 250 251
		struct omap_video_timings *timings)
{
	struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);

	mutex_lock(&ddata->lock);
	*timings = dssdev->panel.timings;
	mutex_unlock(&ddata->lock);
}

252
static int tfp410_check_timings(struct omap_dss_device *dssdev,
253 254 255 256 257 258 259 260 261 262 263 264 265
		struct omap_video_timings *timings)
{
	struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
	int r;

	mutex_lock(&ddata->lock);
	r = dpi_check_timings(dssdev, timings);
	mutex_unlock(&ddata->lock);

	return r;
}


266
static int tfp410_ddc_read(struct i2c_adapter *adapter,
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
		unsigned char *buf, u16 count, u8 offset)
{
	int r, retries;

	for (retries = 3; retries > 0; retries--) {
		struct i2c_msg msgs[] = {
			{
				.addr   = DDC_ADDR,
				.flags  = 0,
				.len    = 1,
				.buf    = &offset,
			}, {
				.addr   = DDC_ADDR,
				.flags  = I2C_M_RD,
				.len    = count,
				.buf    = buf,
			}
		};

		r = i2c_transfer(adapter, msgs, 2);
		if (r == 2)
			return 0;

		if (r != -EAGAIN)
			break;
	}

	return r < 0 ? r : -EIO;
}

297
static int tfp410_read_edid(struct omap_dss_device *dssdev,
298 299 300 301 302 303 304
		u8 *edid, int len)
{
	struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
	int r, l, bytes_read;

	mutex_lock(&ddata->lock);

T
Tomi Valkeinen 已提交
305
	if (!ddata->i2c_adapter) {
306 307 308 309 310
		r = -ENODEV;
		goto err;
	}

	l = min(EDID_LENGTH, len);
T
Tomi Valkeinen 已提交
311
	r = tfp410_ddc_read(ddata->i2c_adapter, edid, l, 0);
312 313 314 315 316 317 318 319 320
	if (r)
		goto err;

	bytes_read = l;

	/* if there are extensions, read second block */
	if (len > EDID_LENGTH && edid[0x7e] > 0) {
		l = min(EDID_LENGTH, len - EDID_LENGTH);

T
Tomi Valkeinen 已提交
321
		r = tfp410_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
				l, EDID_LENGTH);
		if (r)
			goto err;

		bytes_read += l;
	}

	mutex_unlock(&ddata->lock);

	return bytes_read;

err:
	mutex_unlock(&ddata->lock);
	return r;
}

338
static bool tfp410_detect(struct omap_dss_device *dssdev)
339 340 341 342 343 344 345
{
	struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
	unsigned char out;
	int r;

	mutex_lock(&ddata->lock);

T
Tomi Valkeinen 已提交
346
	if (!ddata->i2c_adapter)
347 348
		goto out;

T
Tomi Valkeinen 已提交
349
	r = tfp410_ddc_read(ddata->i2c_adapter, &out, 1, 0);
350 351 352 353 354 355 356 357 358 359

	mutex_unlock(&ddata->lock);

	return r == 0;

out:
	mutex_unlock(&ddata->lock);
	return true;
}

360 361 362
static struct omap_dss_driver tfp410_driver = {
	.probe		= tfp410_probe,
	.remove		= __exit_p(tfp410_remove),
363

364 365 366 367
	.enable		= tfp410_enable,
	.disable	= tfp410_disable,
	.suspend	= tfp410_suspend,
	.resume		= tfp410_resume,
368

369 370 371
	.set_timings	= tfp410_set_timings,
	.get_timings	= tfp410_get_timings,
	.check_timings	= tfp410_check_timings,
372

373 374
	.read_edid	= tfp410_read_edid,
	.detect		= tfp410_detect,
375 376

	.driver         = {
377
		.name   = "tfp410",
378 379 380 381
		.owner  = THIS_MODULE,
	},
};

382
static int __init tfp410_init(void)
383
{
384
	return omap_dss_register_driver(&tfp410_driver);
385 386
}

387
static void __exit tfp410_exit(void)
388
{
389
	omap_dss_unregister_driver(&tfp410_driver);
390 391
}

392 393
module_init(tfp410_init);
module_exit(tfp410_exit);
394
MODULE_LICENSE("GPL");