panel-taal.c 25.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Taal DSI command mode panel
 *
 * Copyright (C) 2009 Nokia Corporation
 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.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/>.
 */

/*#define DEBUG*/

#include <linux/module.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/jiffies.h>
#include <linux/sched.h>
#include <linux/backlight.h>
#include <linux/fb.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/completion.h>
#include <linux/workqueue.h>
33
#include <linux/slab.h>
34
#include <linux/mutex.h>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

#include <plat/display.h>

/* DSI Virtual channel. Hardcoded for now. */
#define TCH 0

#define DCS_READ_NUM_ERRORS	0x05
#define DCS_READ_POWER_MODE	0x0a
#define DCS_READ_MADCTL		0x0b
#define DCS_READ_PIXEL_FORMAT	0x0c
#define DCS_RDDSDR		0x0f
#define DCS_SLEEP_IN		0x10
#define DCS_SLEEP_OUT		0x11
#define DCS_DISPLAY_OFF		0x28
#define DCS_DISPLAY_ON		0x29
#define DCS_COLUMN_ADDR		0x2a
#define DCS_PAGE_ADDR		0x2b
#define DCS_MEMORY_WRITE	0x2c
#define DCS_TEAR_OFF		0x34
#define DCS_TEAR_ON		0x35
#define DCS_MEM_ACC_CTRL	0x36
#define DCS_PIXEL_FORMAT	0x3a
#define DCS_BRIGHTNESS		0x51
#define DCS_CTRL_DISPLAY	0x53
#define DCS_WRITE_CABC		0x55
#define DCS_READ_CABC		0x56
#define DCS_GET_ID1		0xda
#define DCS_GET_ID2		0xdb
#define DCS_GET_ID3		0xdc

/* #define TAAL_USE_ESD_CHECK */
#define TAAL_ESD_CHECK_PERIOD	msecs_to_jiffies(5000)

68 69
static int _taal_enable_te(struct omap_dss_device *dssdev, bool enable);

70
struct taal_data {
71 72
	struct mutex lock;

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 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
	struct backlight_device *bldev;

	unsigned long	hw_guard_end;	/* next value of jiffies when we can
					 * issue the next sleep in/out command
					 */
	unsigned long	hw_guard_wait;	/* max guard time in jiffies */

	struct omap_dss_device *dssdev;

	bool enabled;
	u8 rotate;
	bool mirror;

	bool te_enabled;
	bool use_ext_te;
	struct completion te_completion;

	bool use_dsi_bl;

	bool cabc_broken;
	unsigned cabc_mode;

	bool intro_printed;

	struct workqueue_struct *esd_wq;
	struct delayed_work esd_work;
};

static void taal_esd_work(struct work_struct *work);

static void hw_guard_start(struct taal_data *td, int guard_msec)
{
	td->hw_guard_wait = msecs_to_jiffies(guard_msec);
	td->hw_guard_end = jiffies + td->hw_guard_wait;
}

static void hw_guard_wait(struct taal_data *td)
{
	unsigned long wait = td->hw_guard_end - jiffies;

	if ((long)wait > 0 && wait <= td->hw_guard_wait) {
		set_current_state(TASK_UNINTERRUPTIBLE);
		schedule_timeout(wait);
	}
}

static int taal_dcs_read_1(u8 dcs_cmd, u8 *data)
{
	int r;
	u8 buf[1];

	r = dsi_vc_dcs_read(TCH, dcs_cmd, buf, 1);

	if (r < 0)
		return r;

	*data = buf[0];

	return 0;
}

static int taal_dcs_write_0(u8 dcs_cmd)
{
	return dsi_vc_dcs_write(TCH, &dcs_cmd, 1);
}

static int taal_dcs_write_1(u8 dcs_cmd, u8 param)
{
	u8 buf[2];
	buf[0] = dcs_cmd;
	buf[1] = param;
	return dsi_vc_dcs_write(TCH, buf, 2);
}

static int taal_sleep_in(struct taal_data *td)

{
	u8 cmd;
	int r;

	hw_guard_wait(td);

	cmd = DCS_SLEEP_IN;
	r = dsi_vc_dcs_write_nosync(TCH, &cmd, 1);
	if (r)
		return r;

	hw_guard_start(td, 120);

	msleep(5);

	return 0;
}

static int taal_sleep_out(struct taal_data *td)
{
	int r;

	hw_guard_wait(td);

	r = taal_dcs_write_0(DCS_SLEEP_OUT);
	if (r)
		return r;

	hw_guard_start(td, 120);

	msleep(5);

	return 0;
}

static int taal_get_id(u8 *id1, u8 *id2, u8 *id3)
{
	int r;

	r = taal_dcs_read_1(DCS_GET_ID1, id1);
	if (r)
		return r;
	r = taal_dcs_read_1(DCS_GET_ID2, id2);
	if (r)
		return r;
	r = taal_dcs_read_1(DCS_GET_ID3, id3);
	if (r)
		return r;

	return 0;
}

static int taal_set_addr_mode(u8 rotate, bool mirror)
{
	int r;
	u8 mode;
	int b5, b6, b7;

	r = taal_dcs_read_1(DCS_READ_MADCTL, &mode);
	if (r)
		return r;

	switch (rotate) {
	default:
	case 0:
		b7 = 0;
		b6 = 0;
		b5 = 0;
		break;
	case 1:
		b7 = 0;
		b6 = 1;
		b5 = 1;
		break;
	case 2:
		b7 = 1;
		b6 = 1;
		b5 = 0;
		break;
	case 3:
		b7 = 1;
		b6 = 0;
		b5 = 1;
		break;
	}

	if (mirror)
		b6 = !b6;

	mode &= ~((1<<7) | (1<<6) | (1<<5));
	mode |= (b7 << 7) | (b6 << 6) | (b5 << 5);

	return taal_dcs_write_1(DCS_MEM_ACC_CTRL, mode);
}

static int taal_set_update_window(u16 x, u16 y, u16 w, u16 h)
{
	int r;
	u16 x1 = x;
	u16 x2 = x + w - 1;
	u16 y1 = y;
	u16 y2 = y + h - 1;

	u8 buf[5];
	buf[0] = DCS_COLUMN_ADDR;
	buf[1] = (x1 >> 8) & 0xff;
	buf[2] = (x1 >> 0) & 0xff;
	buf[3] = (x2 >> 8) & 0xff;
	buf[4] = (x2 >> 0) & 0xff;

	r = dsi_vc_dcs_write_nosync(TCH, buf, sizeof(buf));
	if (r)
		return r;

	buf[0] = DCS_PAGE_ADDR;
	buf[1] = (y1 >> 8) & 0xff;
	buf[2] = (y1 >> 0) & 0xff;
	buf[3] = (y2 >> 8) & 0xff;
	buf[4] = (y2 >> 0) & 0xff;

	r = dsi_vc_dcs_write_nosync(TCH, buf, sizeof(buf));
	if (r)
		return r;

	dsi_vc_send_bta_sync(TCH);

	return r;
}

static int taal_bl_update_status(struct backlight_device *dev)
{
	struct omap_dss_device *dssdev = dev_get_drvdata(&dev->dev);
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	int r;
	int level;

	if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
			dev->props.power == FB_BLANK_UNBLANK)
		level = dev->props.brightness;
	else
		level = 0;

	dev_dbg(&dssdev->dev, "update brightness to %d\n", level);

293 294
	mutex_lock(&td->lock);

295 296 297 298 299
	if (td->use_dsi_bl) {
		if (td->enabled) {
			dsi_bus_lock();
			r = taal_dcs_write_1(DCS_BRIGHTNESS, level);
			dsi_bus_unlock();
300 301
		} else {
			r = 0;
302 303 304
		}
	} else {
		if (!dssdev->set_backlight)
305 306 307
			r = -EINVAL;
		else
			r = dssdev->set_backlight(dssdev, level);
308 309
	}

310 311 312
	mutex_unlock(&td->lock);

	return r;
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
}

static int taal_bl_get_intensity(struct backlight_device *dev)
{
	if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
			dev->props.power == FB_BLANK_UNBLANK)
		return dev->props.brightness;

	return 0;
}

static struct backlight_ops taal_bl_ops = {
	.get_brightness = taal_bl_get_intensity,
	.update_status  = taal_bl_update_status,
};

static void taal_get_timings(struct omap_dss_device *dssdev,
			struct omap_video_timings *timings)
{
	*timings = dssdev->panel.timings;
}

static void taal_get_resolution(struct omap_dss_device *dssdev,
		u16 *xres, u16 *yres)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);

	if (td->rotate == 0 || td->rotate == 2) {
		*xres = dssdev->panel.timings.x_res;
		*yres = dssdev->panel.timings.y_res;
	} else {
		*yres = dssdev->panel.timings.x_res;
		*xres = dssdev->panel.timings.y_res;
	}
}

static irqreturn_t taal_te_isr(int irq, void *data)
{
	struct omap_dss_device *dssdev = data;
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);

	complete_all(&td->te_completion);

	return IRQ_HANDLED;
}

static ssize_t taal_num_errors_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct omap_dss_device *dssdev = to_dss_device(dev);
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	u8 errors;
	int r;

367 368
	mutex_lock(&td->lock);

369 370 371 372 373 374 375 376
	if (td->enabled) {
		dsi_bus_lock();
		r = taal_dcs_read_1(DCS_READ_NUM_ERRORS, &errors);
		dsi_bus_unlock();
	} else {
		r = -ENODEV;
	}

377 378
	mutex_unlock(&td->lock);

379 380 381 382 383 384 385 386 387 388 389 390 391 392
	if (r)
		return r;

	return snprintf(buf, PAGE_SIZE, "%d\n", errors);
}

static ssize_t taal_hw_revision_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct omap_dss_device *dssdev = to_dss_device(dev);
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	u8 id1, id2, id3;
	int r;

393 394
	mutex_lock(&td->lock);

395 396 397 398 399 400 401 402
	if (td->enabled) {
		dsi_bus_lock();
		r = taal_get_id(&id1, &id2, &id3);
		dsi_bus_unlock();
	} else {
		r = -ENODEV;
	}

403 404
	mutex_unlock(&td->lock);

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
	if (r)
		return r;

	return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
}

static const char *cabc_modes[] = {
	"off",		/* used also always when CABC is not supported */
	"ui",
	"still-image",
	"moving-image",
};

static ssize_t show_cabc_mode(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	struct omap_dss_device *dssdev = to_dss_device(dev);
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	const char *mode_str;
	int mode;
	int len;

	mode = td->cabc_mode;

	mode_str = "unknown";
	if (mode >= 0 && mode < ARRAY_SIZE(cabc_modes))
		mode_str = cabc_modes[mode];
	len = snprintf(buf, PAGE_SIZE, "%s\n", mode_str);

	return len < PAGE_SIZE - 1 ? len : PAGE_SIZE - 1;
}

static ssize_t store_cabc_mode(struct device *dev,
		struct device_attribute *attr,
		const char *buf, size_t count)
{
	struct omap_dss_device *dssdev = to_dss_device(dev);
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	int i;

	for (i = 0; i < ARRAY_SIZE(cabc_modes); i++) {
		if (sysfs_streq(cabc_modes[i], buf))
			break;
	}

	if (i == ARRAY_SIZE(cabc_modes))
		return -EINVAL;

454 455
	mutex_lock(&td->lock);

456 457 458 459 460 461 462 463 464
	if (td->enabled) {
		dsi_bus_lock();
		if (!td->cabc_broken)
			taal_dcs_write_1(DCS_WRITE_CABC, i);
		dsi_bus_unlock();
	}

	td->cabc_mode = i;

465 466
	mutex_unlock(&td->lock);

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
	return count;
}

static ssize_t show_cabc_available_modes(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	int len;
	int i;

	for (i = 0, len = 0;
	     len < PAGE_SIZE && i < ARRAY_SIZE(cabc_modes); i++)
		len += snprintf(&buf[len], PAGE_SIZE - len, "%s%s%s",
			i ? " " : "", cabc_modes[i],
			i == ARRAY_SIZE(cabc_modes) - 1 ? "\n" : "");

	return len < PAGE_SIZE ? len : PAGE_SIZE - 1;
}

static DEVICE_ATTR(num_dsi_errors, S_IRUGO, taal_num_errors_show, NULL);
static DEVICE_ATTR(hw_revision, S_IRUGO, taal_hw_revision_show, NULL);
static DEVICE_ATTR(cabc_mode, S_IRUGO | S_IWUSR,
		show_cabc_mode, store_cabc_mode);
static DEVICE_ATTR(cabc_available_modes, S_IRUGO,
		show_cabc_available_modes, NULL);

static struct attribute *taal_attrs[] = {
	&dev_attr_num_dsi_errors.attr,
	&dev_attr_hw_revision.attr,
	&dev_attr_cabc_mode.attr,
	&dev_attr_cabc_available_modes.attr,
	NULL,
};

static struct attribute_group taal_attr_group = {
	.attrs = taal_attrs,
};

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
static void taal_hw_reset(struct omap_dss_device *dssdev)
{
	if (dssdev->reset_gpio == -1)
		return;

	gpio_set_value(dssdev->reset_gpio, 1);
	udelay(10);
	/* reset the panel */
	gpio_set_value(dssdev->reset_gpio, 0);
	/* assert reset for at least 10us */
	udelay(10);
	gpio_set_value(dssdev->reset_gpio, 1);
	/* wait 5ms after releasing reset */
	msleep(5);
}

521 522
static int taal_probe(struct omap_dss_device *dssdev)
{
523
	struct backlight_properties props;
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	struct taal_data *td;
	struct backlight_device *bldev;
	int r;

	const struct omap_video_timings taal_panel_timings = {
		.x_res		= 864,
		.y_res		= 480,
	};

	dev_dbg(&dssdev->dev, "probe\n");

	dssdev->panel.config = OMAP_DSS_LCD_TFT;
	dssdev->panel.timings = taal_panel_timings;
	dssdev->ctrl.pixel_size = 24;

	td = kzalloc(sizeof(*td), GFP_KERNEL);
	if (!td) {
		r = -ENOMEM;
542
		goto err;
543 544 545
	}
	td->dssdev = dssdev;

546 547
	mutex_init(&td->lock);

548 549 550 551
	td->esd_wq = create_singlethread_workqueue("taal_esd");
	if (td->esd_wq == NULL) {
		dev_err(&dssdev->dev, "can't create ESD workqueue\n");
		r = -ENOMEM;
552
		goto err_wq;
553 554 555 556 557
	}
	INIT_DELAYED_WORK_DEFERRABLE(&td->esd_work, taal_esd_work);

	dev_set_drvdata(&dssdev->dev, td);

558 559
	taal_hw_reset(dssdev);

560 561
	/* if no platform set_backlight() defined, presume DSI backlight
	 * control */
562
	memset(&props, 0, sizeof(struct backlight_properties));
563 564 565
	if (!dssdev->set_backlight)
		td->use_dsi_bl = true;

566 567 568 569
	if (td->use_dsi_bl)
		props.max_brightness = 255;
	else
		props.max_brightness = 127;
570
	bldev = backlight_device_register("taal", &dssdev->dev, dssdev,
571
					  &taal_bl_ops, &props);
572 573
	if (IS_ERR(bldev)) {
		r = PTR_ERR(bldev);
574
		goto err_bl;
575 576 577 578 579 580
	}

	td->bldev = bldev;

	bldev->props.fb_blank = FB_BLANK_UNBLANK;
	bldev->props.power = FB_BLANK_UNBLANK;
581
	if (td->use_dsi_bl)
582
		bldev->props.brightness = 255;
583
	else
584 585 586 587 588 589 590 591 592 593
		bldev->props.brightness = 127;

	taal_bl_update_status(bldev);

	if (dssdev->phy.dsi.ext_te) {
		int gpio = dssdev->phy.dsi.ext_te_gpio;

		r = gpio_request(gpio, "taal irq");
		if (r) {
			dev_err(&dssdev->dev, "GPIO request failed\n");
594
			goto err_gpio;
595 596 597 598 599 600 601 602 603 604 605
		}

		gpio_direction_input(gpio);

		r = request_irq(gpio_to_irq(gpio), taal_te_isr,
				IRQF_DISABLED | IRQF_TRIGGER_RISING,
				"taal vsync", dssdev);

		if (r) {
			dev_err(&dssdev->dev, "IRQ request failed\n");
			gpio_free(gpio);
606
			goto err_irq;
607 608 609 610 611 612 613 614 615 616
		}

		init_completion(&td->te_completion);

		td->use_ext_te = true;
	}

	r = sysfs_create_group(&dssdev->dev.kobj, &taal_attr_group);
	if (r) {
		dev_err(&dssdev->dev, "failed to create sysfs files\n");
617
		goto err_sysfs;
618 619 620
	}

	return 0;
621
err_sysfs:
622 623
	if (td->use_ext_te)
		free_irq(gpio_to_irq(dssdev->phy.dsi.ext_te_gpio), dssdev);
624
err_irq:
625 626
	if (td->use_ext_te)
		gpio_free(dssdev->phy.dsi.ext_te_gpio);
627
err_gpio:
628
	backlight_device_unregister(bldev);
629
err_bl:
630
	destroy_workqueue(td->esd_wq);
631
err_wq:
632
	kfree(td);
633
err:
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
	return r;
}

static void taal_remove(struct omap_dss_device *dssdev)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	struct backlight_device *bldev;

	dev_dbg(&dssdev->dev, "remove\n");

	sysfs_remove_group(&dssdev->dev.kobj, &taal_attr_group);

	if (td->use_ext_te) {
		int gpio = dssdev->phy.dsi.ext_te_gpio;
		free_irq(gpio_to_irq(gpio), dssdev);
		gpio_free(gpio);
	}

	bldev = td->bldev;
	bldev->props.power = FB_BLANK_POWERDOWN;
	taal_bl_update_status(bldev);
	backlight_device_unregister(bldev);

657
	cancel_delayed_work(&td->esd_work);
658 659
	destroy_workqueue(td->esd_wq);

660 661 662
	/* reset, to be sure that the panel is in a valid state */
	taal_hw_reset(dssdev);

663 664 665
	kfree(td);
}

666
static int taal_power_on(struct omap_dss_device *dssdev)
667 668 669 670 671 672 673 674
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	u8 id1, id2, id3;
	int r;

	/* it seems we have to wait a bit until taal is ready */
	msleep(5);

675 676 677 678 679 680
	r = omapdss_dsi_display_enable(dssdev);
	if (r) {
		dev_err(&dssdev->dev, "failed to enable DSI\n");
		goto err0;
	}

681 682
	taal_hw_reset(dssdev);

683 684
	omapdss_dsi_vc_enable_hs(TCH, false);

685 686 687 688 689 690 691 692 693 694 695 696
	r = taal_sleep_out(td);
	if (r)
		goto err;

	r = taal_get_id(&id1, &id2, &id3);
	if (r)
		goto err;

	/* on early revisions CABC is broken */
	if (id2 == 0x00 || id2 == 0xff || id2 == 0x81)
		td->cabc_broken = true;

697 698 699
	r = taal_dcs_write_1(DCS_BRIGHTNESS, 0xff);
	if (r)
		goto err;
700

701 702 703 704
	r = taal_dcs_write_1(DCS_CTRL_DISPLAY,
			(1<<2) | (1<<5));	/* BL | BCTRL */
	if (r)
		goto err;
705

706 707 708
	r = taal_dcs_write_1(DCS_PIXEL_FORMAT, 0x7); /* 24bit/pixel */
	if (r)
		goto err;
709

710 711 712 713 714 715 716 717 718 719 720 721 722
	r = taal_set_addr_mode(td->rotate, td->mirror);
	if (r)
		goto err;

	if (!td->cabc_broken) {
		r = taal_dcs_write_1(DCS_WRITE_CABC, td->cabc_mode);
		if (r)
			goto err;
	}

	r = taal_dcs_write_0(DCS_DISPLAY_ON);
	if (r)
		goto err;
723

724 725 726 727
	r = _taal_enable_te(dssdev, td->te_enabled);
	if (r)
		goto err;

728 729 730 731 732 733 734 735 736 737 738
	td->enabled = 1;

	if (!td->intro_printed) {
		dev_info(&dssdev->dev, "revision %02x.%02x.%02x\n",
				id1, id2, id3);
		if (td->cabc_broken)
			dev_info(&dssdev->dev,
					"old Taal version, CABC disabled\n");
		td->intro_printed = true;
	}

739 740
	omapdss_dsi_vc_enable_hs(TCH, true);

741 742
	return 0;
err:
743 744 745 746
	dev_err(&dssdev->dev, "error while enabling panel, issuing HW reset\n");

	taal_hw_reset(dssdev);

747 748
	omapdss_dsi_display_disable(dssdev);
err0:
749 750 751
	return r;
}

752
static void taal_power_off(struct omap_dss_device *dssdev)
753 754
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
755
	int r;
756

757 758 759 760 761 762
	r = taal_dcs_write_0(DCS_DISPLAY_OFF);
	if (!r) {
		r = taal_sleep_in(td);
		/* wait a bit so that the message goes through */
		msleep(10);
	}
763

764 765 766 767 768
	if (r) {
		dev_err(&dssdev->dev,
				"error disabling panel, issuing HW reset\n");
		taal_hw_reset(dssdev);
	}
769

770 771
	omapdss_dsi_display_disable(dssdev);

772
	td->enabled = 0;
773 774 775 776
}

static int taal_enable(struct omap_dss_device *dssdev)
{
777
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
778
	int r;
779

780 781
	dev_dbg(&dssdev->dev, "enable\n");

782 783 784 785 786 787
	mutex_lock(&td->lock);

	if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED) {
		r = -EINVAL;
		goto err;
	}
788

789 790
	dsi_bus_lock();

791
	r = taal_power_on(dssdev);
792 793 794

	dsi_bus_unlock();

795
	if (r)
796
		goto err;
797

798 799 800 801
#ifdef TAAL_USE_ESD_CHECK
	queue_delayed_work(td->esd_wq, &td->esd_work, TAAL_ESD_CHECK_PERIOD);
#endif

802 803
	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;

804 805 806 807 808 809
	mutex_unlock(&td->lock);

	return 0;
err:
	dev_dbg(&dssdev->dev, "enable failed\n");
	mutex_unlock(&td->lock);
810 811 812 813 814
	return r;
}

static void taal_disable(struct omap_dss_device *dssdev)
{
815 816
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);

817 818
	dev_dbg(&dssdev->dev, "disable\n");

819 820
	mutex_lock(&td->lock);

821 822
	cancel_delayed_work(&td->esd_work);

823 824
	dsi_bus_lock();

825 826 827
	if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
		taal_power_off(dssdev);

828 829
	dsi_bus_unlock();

830
	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
831 832

	mutex_unlock(&td->lock);
833 834 835 836
}

static int taal_suspend(struct omap_dss_device *dssdev)
{
837 838 839
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	int r;

840
	dev_dbg(&dssdev->dev, "suspend\n");
841

842 843 844 845 846 847
	mutex_lock(&td->lock);

	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE) {
		r = -EINVAL;
		goto err;
	}
848

849 850
	cancel_delayed_work(&td->esd_work);

851 852
	dsi_bus_lock();

853
	taal_power_off(dssdev);
854 855 856

	dsi_bus_unlock();

857
	dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
858

859 860
	mutex_unlock(&td->lock);

861
	return 0;
862 863 864
err:
	mutex_unlock(&td->lock);
	return r;
865 866 867 868
}

static int taal_resume(struct omap_dss_device *dssdev)
{
869
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
870
	int r;
871

872
	dev_dbg(&dssdev->dev, "resume\n");
873

874 875 876 877 878 879
	mutex_lock(&td->lock);

	if (dssdev->state != OMAP_DSS_DISPLAY_SUSPENDED) {
		r = -EINVAL;
		goto err;
	}
880

881 882
	dsi_bus_lock();

883
	r = taal_power_on(dssdev);
884 885 886

	dsi_bus_unlock();

887
	if (r) {
888
		dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
889
	} else {
890
		dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
891 892 893 894 895
#ifdef TAAL_USE_ESD_CHECK
		queue_delayed_work(td->esd_wq, &td->esd_work,
				TAAL_ESD_CHECK_PERIOD);
#endif
	}
896 897 898 899 900 901

	mutex_unlock(&td->lock);

	return r;
err:
	mutex_unlock(&td->lock);
902
	return r;
903 904
}

905 906 907 908 909 910 911 912
static void taal_framedone_cb(int err, void *data)
{
	struct omap_dss_device *dssdev = data;
	dev_dbg(&dssdev->dev, "framedone, err %d\n", err);
	dsi_bus_unlock();
}

static int taal_update(struct omap_dss_device *dssdev,
913 914
				    u16 x, u16 y, u16 w, u16 h)
{
915 916 917 918 919
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	int r;

	dev_dbg(&dssdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);

920
	mutex_lock(&td->lock);
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
	dsi_bus_lock();

	if (!td->enabled) {
		r = 0;
		goto err;
	}

	r = omap_dsi_prepare_update(dssdev, &x, &y, &w, &h);
	if (r)
		goto err;

	r = taal_set_update_window(x, y, w, h);
	if (r)
		goto err;

	r = omap_dsi_update(dssdev, TCH, x, y, w, h,
			taal_framedone_cb, dssdev);
	if (r)
		goto err;

	/* note: no bus_unlock here. unlock is in framedone_cb */
942
	mutex_unlock(&td->lock);
943 944 945
	return 0;
err:
	dsi_bus_unlock();
946
	mutex_unlock(&td->lock);
947 948 949 950 951
	return r;
}

static int taal_sync(struct omap_dss_device *dssdev)
{
952 953
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);

954 955
	dev_dbg(&dssdev->dev, "sync\n");

956
	mutex_lock(&td->lock);
957 958
	dsi_bus_lock();
	dsi_bus_unlock();
959
	mutex_unlock(&td->lock);
960 961 962 963

	dev_dbg(&dssdev->dev, "sync done\n");

	return 0;
964 965
}

966
static int _taal_enable_te(struct omap_dss_device *dssdev, bool enable)
967 968 969 970 971 972 973 974
{
	int r;

	if (enable)
		r = taal_dcs_write_1(DCS_TEAR_ON, 0);
	else
		r = taal_dcs_write_0(DCS_TEAR_OFF);

975 976 977 978 979 980
	omapdss_dsi_enable_te(dssdev, enable);

	/* XXX for some reason, DSI TE breaks if we don't wait here.
	 * Panel bug? Needs more studying */
	msleep(100);

981 982 983 984 985
	return r;
}

static int taal_enable_te(struct omap_dss_device *dssdev, bool enable)
{
986
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
987 988
	int r;

989
	mutex_lock(&td->lock);
990 991
	dsi_bus_lock();

992 993 994 995 996 997 998
	if (td->enabled) {
		r = _taal_enable_te(dssdev, enable);
		if (r)
			goto err;
	}

	td->te_enabled = enable;
999

1000
	dsi_bus_unlock();
1001
	mutex_unlock(&td->lock);
1002

1003 1004 1005 1006 1007
	return 0;
err:
	dsi_bus_unlock();
	mutex_unlock(&td->lock);

1008 1009 1010
	return r;
}

1011 1012 1013
static int taal_get_te(struct omap_dss_device *dssdev)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1014 1015 1016 1017 1018 1019 1020
	int r;

	mutex_lock(&td->lock);
	r = td->te_enabled;
	mutex_unlock(&td->lock);

	return r;
1021 1022
}

1023 1024 1025 1026 1027 1028 1029
static int taal_rotate(struct omap_dss_device *dssdev, u8 rotate)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	int r;

	dev_dbg(&dssdev->dev, "rotate %d\n", rotate);

1030
	mutex_lock(&td->lock);
1031 1032
	dsi_bus_lock();

1033 1034 1035
	if (td->enabled) {
		r = taal_set_addr_mode(rotate, td->mirror);
		if (r)
1036
			goto err;
1037 1038 1039 1040
	}

	td->rotate = rotate;

1041
	dsi_bus_unlock();
1042
	mutex_unlock(&td->lock);
1043
	return 0;
1044 1045
err:
	dsi_bus_unlock();
1046
	mutex_unlock(&td->lock);
1047
	return r;
1048 1049 1050 1051 1052
}

static u8 taal_get_rotate(struct omap_dss_device *dssdev)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1053 1054 1055 1056 1057 1058 1059
	int r;

	mutex_lock(&td->lock);
	r = td->rotate;
	mutex_unlock(&td->lock);

	return r;
1060 1061 1062 1063 1064 1065 1066 1067 1068
}

static int taal_mirror(struct omap_dss_device *dssdev, bool enable)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	int r;

	dev_dbg(&dssdev->dev, "mirror %d\n", enable);

1069
	mutex_lock(&td->lock);
1070
	dsi_bus_lock();
1071 1072 1073
	if (td->enabled) {
		r = taal_set_addr_mode(td->rotate, enable);
		if (r)
1074
			goto err;
1075 1076 1077 1078
	}

	td->mirror = enable;

1079
	dsi_bus_unlock();
1080
	mutex_unlock(&td->lock);
1081
	return 0;
1082 1083
err:
	dsi_bus_unlock();
1084
	mutex_unlock(&td->lock);
1085
	return r;
1086 1087 1088 1089 1090
}

static bool taal_get_mirror(struct omap_dss_device *dssdev)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1091 1092 1093 1094 1095 1096 1097
	int r;

	mutex_lock(&td->lock);
	r = td->mirror;
	mutex_unlock(&td->lock);

	return r;
1098 1099 1100 1101
}

static int taal_run_test(struct omap_dss_device *dssdev, int test_num)
{
1102
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1103 1104 1105
	u8 id1, id2, id3;
	int r;

1106
	mutex_lock(&td->lock);
1107 1108 1109 1110 1111 1112

	if (!td->enabled) {
		r = -ENODEV;
		goto err1;
	}

T
Tomi Valkeinen 已提交
1113 1114
	dsi_bus_lock();

1115 1116
	r = taal_dcs_read_1(DCS_GET_ID1, &id1);
	if (r)
1117
		goto err2;
1118 1119
	r = taal_dcs_read_1(DCS_GET_ID2, &id2);
	if (r)
1120
		goto err2;
1121 1122
	r = taal_dcs_read_1(DCS_GET_ID3, &id3);
	if (r)
1123
		goto err2;
1124

T
Tomi Valkeinen 已提交
1125
	dsi_bus_unlock();
1126
	mutex_unlock(&td->lock);
1127
	return 0;
1128
err2:
T
Tomi Valkeinen 已提交
1129
	dsi_bus_unlock();
1130
err1:
1131
	mutex_unlock(&td->lock);
T
Tomi Valkeinen 已提交
1132
	return r;
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
}

static int taal_memory_read(struct omap_dss_device *dssdev,
		void *buf, size_t size,
		u16 x, u16 y, u16 w, u16 h)
{
	int r;
	int first = 1;
	int plen;
	unsigned buf_used = 0;
T
Tomi Valkeinen 已提交
1143 1144
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);

1145 1146 1147
	if (size < w * h * 3)
		return -ENOMEM;

1148 1149 1150 1151 1152 1153 1154
	mutex_lock(&td->lock);

	if (!td->enabled) {
		r = -ENODEV;
		goto err1;
	}

1155 1156 1157 1158
	size = min(w * h * 3,
			dssdev->panel.timings.x_res *
			dssdev->panel.timings.y_res * 3);

T
Tomi Valkeinen 已提交
1159 1160
	dsi_bus_lock();

1161 1162 1163 1164 1165 1166 1167 1168
	/* plen 1 or 2 goes into short packet. until checksum error is fixed,
	 * use short packets. plen 32 works, but bigger packets seem to cause
	 * an error. */
	if (size % 2)
		plen = 1;
	else
		plen = 2;

T
Tomi Valkeinen 已提交
1169
	taal_set_update_window(x, y, w, h);
1170 1171 1172

	r = dsi_vc_set_max_rx_packet_size(TCH, plen);
	if (r)
1173
		goto err2;
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183

	while (buf_used < size) {
		u8 dcs_cmd = first ? 0x2e : 0x3e;
		first = 0;

		r = dsi_vc_dcs_read(TCH, dcs_cmd,
				buf + buf_used, size - buf_used);

		if (r < 0) {
			dev_err(&dssdev->dev, "read error\n");
1184
			goto err3;
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
		}

		buf_used += r;

		if (r < plen) {
			dev_err(&dssdev->dev, "short read\n");
			break;
		}

		if (signal_pending(current)) {
			dev_err(&dssdev->dev, "signal pending, "
					"aborting memory read\n");
			r = -ERESTARTSYS;
1198
			goto err3;
1199 1200 1201 1202 1203
		}
	}

	r = buf_used;

1204
err3:
1205
	dsi_vc_set_max_rx_packet_size(TCH, 1);
1206
err2:
T
Tomi Valkeinen 已提交
1207
	dsi_bus_unlock();
1208 1209
err1:
	mutex_unlock(&td->lock);
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
	return r;
}

static void taal_esd_work(struct work_struct *work)
{
	struct taal_data *td = container_of(work, struct taal_data,
			esd_work.work);
	struct omap_dss_device *dssdev = td->dssdev;
	u8 state1, state2;
	int r;

1221 1222 1223 1224
	mutex_lock(&td->lock);

	if (!td->enabled) {
		mutex_unlock(&td->lock);
1225
		return;
1226
	}
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

	dsi_bus_lock();

	r = taal_dcs_read_1(DCS_RDDSDR, &state1);
	if (r) {
		dev_err(&dssdev->dev, "failed to read Taal status\n");
		goto err;
	}

	/* Run self diagnostics */
	r = taal_sleep_out(td);
	if (r) {
		dev_err(&dssdev->dev, "failed to run Taal self-diagnostics\n");
		goto err;
	}

	r = taal_dcs_read_1(DCS_RDDSDR, &state2);
	if (r) {
		dev_err(&dssdev->dev, "failed to read Taal status\n");
		goto err;
	}

	/* Each sleep out command will trigger a self diagnostic and flip
	 * Bit6 if the test passes.
	 */
	if (!((state1 ^ state2) & (1 << 6))) {
		dev_err(&dssdev->dev, "LCD self diagnostics failed\n");
		goto err;
	}
	/* Self-diagnostics result is also shown on TE GPIO line. We need
	 * to re-enable TE after self diagnostics */
T
Tomi Valkeinen 已提交
1258 1259 1260 1261 1262
	if (td->use_ext_te && td->te_enabled) {
		r = taal_dcs_write_1(DCS_TEAR_ON, 0);
		if (r)
			goto err;
	}
1263 1264 1265 1266 1267

	dsi_bus_unlock();

	queue_delayed_work(td->esd_wq, &td->esd_work, TAAL_ESD_CHECK_PERIOD);

1268
	mutex_unlock(&td->lock);
1269 1270 1271 1272
	return;
err:
	dev_err(&dssdev->dev, "performing LCD reset\n");

1273
	taal_power_off(dssdev);
1274
	taal_hw_reset(dssdev);
1275
	taal_power_on(dssdev);
1276 1277 1278 1279

	dsi_bus_unlock();

	queue_delayed_work(td->esd_wq, &td->esd_work, TAAL_ESD_CHECK_PERIOD);
1280 1281

	mutex_unlock(&td->lock);
1282 1283
}

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
static int taal_set_update_mode(struct omap_dss_device *dssdev,
		enum omap_dss_update_mode mode)
{
	if (mode != OMAP_DSS_UPDATE_MANUAL)
		return -EINVAL;
	return 0;
}

static enum omap_dss_update_mode taal_get_update_mode(
		struct omap_dss_device *dssdev)
{
	return OMAP_DSS_UPDATE_MANUAL;
}

1298 1299 1300 1301 1302 1303 1304 1305 1306
static struct omap_dss_driver taal_driver = {
	.probe		= taal_probe,
	.remove		= taal_remove,

	.enable		= taal_enable,
	.disable	= taal_disable,
	.suspend	= taal_suspend,
	.resume		= taal_resume,

1307 1308
	.set_update_mode = taal_set_update_mode,
	.get_update_mode = taal_get_update_mode,
1309 1310 1311 1312

	.update		= taal_update,
	.sync		= taal_sync,

1313
	.get_resolution	= taal_get_resolution,
1314 1315
	.get_recommended_bpp = omapdss_default_get_recommended_bpp,

1316
	.enable_te	= taal_enable_te,
1317 1318
	.get_te		= taal_get_te,

1319 1320 1321 1322 1323 1324 1325
	.set_rotate	= taal_rotate,
	.get_rotate	= taal_get_rotate,
	.set_mirror	= taal_mirror,
	.get_mirror	= taal_get_mirror,
	.run_test	= taal_run_test,
	.memory_read	= taal_memory_read,

1326 1327
	.get_timings	= taal_get_timings,

1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
	.driver         = {
		.name   = "taal",
		.owner  = THIS_MODULE,
	},
};

static int __init taal_init(void)
{
	omap_dss_register_driver(&taal_driver);

	return 0;
}

static void __exit taal_exit(void)
{
	omap_dss_unregister_driver(&taal_driver);
}

module_init(taal_init);
module_exit(taal_exit);

MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
MODULE_DESCRIPTION("Taal Driver");
MODULE_LICENSE("GPL");