panel-taal.c 36.9 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
/*
 * 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/workqueue.h>
32
#include <linux/slab.h>
33
#include <linux/mutex.h>
34

35
#include <video/omapdss.h>
36
#include <video/omap-panel-nokia-dsi.h>
37
#include <video/mipi_display.h>
38 39 40 41 42 43 44 45 46 47 48 49 50

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

#define DCS_READ_NUM_ERRORS	0x05
#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

51 52
static irqreturn_t taal_te_isr(int irq, void *data);
static void taal_te_timeout_work_callback(struct work_struct *work);
53 54
static int _taal_enable_te(struct omap_dss_device *dssdev, bool enable);

55 56
static int taal_panel_reset(struct omap_dss_device *dssdev);

57 58 59 60 61 62 63
/**
 * struct panel_config - panel configuration
 * @name: panel name
 * @type: panel type
 * @timings: panel resolution
 * @sleep: various panel specific delays, passed to msleep() if non-zero
 * @reset_sequence: reset sequence timings, passed to udelay() if non-zero
64 65
 * @regulators: array of panel regulators
 * @num_regulators: number of regulators in the array
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
 */
struct panel_config {
	const char *name;
	int type;

	struct omap_video_timings timings;

	struct {
		unsigned int sleep_in;
		unsigned int sleep_out;
		unsigned int hw_reset;
		unsigned int enable_te;
	} sleep;

	struct {
		unsigned int high;
		unsigned int low;
	} reset_sequence;
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
};

enum {
	PANEL_TAAL,
};

static struct panel_config panel_configs[] = {
	{
		.name		= "taal",
		.type		= PANEL_TAAL,
		.timings	= {
			.x_res		= 864,
			.y_res		= 480,
		},
		.sleep		= {
			.sleep_in	= 5,
			.sleep_out	= 5,
			.hw_reset	= 5,
			.enable_te	= 100, /* possible panel bug */
		},
		.reset_sequence	= {
			.high		= 10,
			.low		= 10,
		},
	},
};

112
struct taal_data {
113 114
	struct mutex lock;

115 116 117 118 119 120 121 122 123 124 125 126 127 128
	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;
129 130

	atomic_t do_update;
131 132
	int channel;

133
	struct delayed_work te_timeout_work;
134 135 136 137 138 139

	bool cabc_broken;
	unsigned cabc_mode;

	bool intro_printed;

140 141
	struct workqueue_struct *workqueue;

142
	struct delayed_work esd_work;
143
	unsigned esd_interval;
144

145 146 147 148
	bool ulps_enabled;
	unsigned ulps_timeout;
	struct delayed_work ulps_work;

149
	struct panel_config *panel_config;
150 151
};

152 153 154 155 156 157
static inline struct nokia_dsi_panel_data
*get_panel_data(const struct omap_dss_device *dssdev)
{
	return (struct nokia_dsi_panel_data *) dssdev->data;
}

158
static void taal_esd_work(struct work_struct *work);
159
static void taal_ulps_work(struct work_struct *work);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

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);
	}
}

177
static int taal_dcs_read_1(struct taal_data *td, u8 dcs_cmd, u8 *data)
178 179 180 181
{
	int r;
	u8 buf[1];

182
	r = dsi_vc_dcs_read(td->dssdev, td->channel, dcs_cmd, buf, 1);
183 184 185 186 187 188 189 190 191

	if (r < 0)
		return r;

	*data = buf[0];

	return 0;
}

192
static int taal_dcs_write_0(struct taal_data *td, u8 dcs_cmd)
193
{
194
	return dsi_vc_dcs_write(td->dssdev, td->channel, &dcs_cmd, 1);
195 196
}

197
static int taal_dcs_write_1(struct taal_data *td, u8 dcs_cmd, u8 param)
198 199 200 201
{
	u8 buf[2];
	buf[0] = dcs_cmd;
	buf[1] = param;
202
	return dsi_vc_dcs_write(td->dssdev, td->channel, buf, 2);
203 204 205 206 207 208 209 210 211 212
}

static int taal_sleep_in(struct taal_data *td)

{
	u8 cmd;
	int r;

	hw_guard_wait(td);

213
	cmd = MIPI_DCS_ENTER_SLEEP_MODE;
214
	r = dsi_vc_dcs_write_nosync(td->dssdev, td->channel, &cmd, 1);
215 216 217 218 219
	if (r)
		return r;

	hw_guard_start(td, 120);

220 221
	if (td->panel_config->sleep.sleep_in)
		msleep(td->panel_config->sleep.sleep_in);
222 223 224 225 226 227 228 229 230 231

	return 0;
}

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

	hw_guard_wait(td);

232
	r = taal_dcs_write_0(td, MIPI_DCS_EXIT_SLEEP_MODE);
233 234 235 236 237
	if (r)
		return r;

	hw_guard_start(td, 120);

238 239
	if (td->panel_config->sleep.sleep_out)
		msleep(td->panel_config->sleep.sleep_out);
240 241 242 243

	return 0;
}

244
static int taal_get_id(struct taal_data *td, u8 *id1, u8 *id2, u8 *id3)
245 246 247
{
	int r;

248
	r = taal_dcs_read_1(td, DCS_GET_ID1, id1);
249 250
	if (r)
		return r;
251
	r = taal_dcs_read_1(td, DCS_GET_ID2, id2);
252 253
	if (r)
		return r;
254
	r = taal_dcs_read_1(td, DCS_GET_ID3, id3);
255 256 257 258 259 260
	if (r)
		return r;

	return 0;
}

261
static int taal_set_addr_mode(struct taal_data *td, u8 rotate, bool mirror)
262 263 264 265 266
{
	int r;
	u8 mode;
	int b5, b6, b7;

267
	r = taal_dcs_read_1(td, MIPI_DCS_GET_ADDRESS_MODE, &mode);
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 297 298 299 300
	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);

301
	return taal_dcs_write_1(td, MIPI_DCS_SET_ADDRESS_MODE, mode);
302 303
}

304 305
static int taal_set_update_window(struct taal_data *td,
		u16 x, u16 y, u16 w, u16 h)
306 307 308 309 310 311 312 313
{
	int r;
	u16 x1 = x;
	u16 x2 = x + w - 1;
	u16 y1 = y;
	u16 y2 = y + h - 1;

	u8 buf[5];
314
	buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS;
315 316 317 318 319
	buf[1] = (x1 >> 8) & 0xff;
	buf[2] = (x1 >> 0) & 0xff;
	buf[3] = (x2 >> 8) & 0xff;
	buf[4] = (x2 >> 0) & 0xff;

320
	r = dsi_vc_dcs_write_nosync(td->dssdev, td->channel, buf, sizeof(buf));
321 322 323
	if (r)
		return r;

324
	buf[0] = MIPI_DCS_SET_PAGE_ADDRESS;
325 326 327 328 329
	buf[1] = (y1 >> 8) & 0xff;
	buf[2] = (y1 >> 0) & 0xff;
	buf[3] = (y2 >> 8) & 0xff;
	buf[4] = (y2 >> 0) & 0xff;

330
	r = dsi_vc_dcs_write_nosync(td->dssdev, td->channel, buf, sizeof(buf));
331 332 333
	if (r)
		return r;

334
	dsi_vc_send_bta_sync(td->dssdev, td->channel);
335 336 337 338

	return r;
}

339 340 341 342 343
static void taal_queue_esd_work(struct omap_dss_device *dssdev)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);

	if (td->esd_interval > 0)
344
		queue_delayed_work(td->workqueue, &td->esd_work,
345 346 347 348 349 350 351 352 353 354
				msecs_to_jiffies(td->esd_interval));
}

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

	cancel_delayed_work(&td->esd_work);
}

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
static void taal_queue_ulps_work(struct omap_dss_device *dssdev)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);

	if (td->ulps_timeout > 0)
		queue_delayed_work(td->workqueue, &td->ulps_work,
				msecs_to_jiffies(td->ulps_timeout));
}

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

	cancel_delayed_work(&td->ulps_work);
}

static int taal_enter_ulps(struct omap_dss_device *dssdev)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
	int r;

	if (td->ulps_enabled)
		return 0;

	taal_cancel_ulps_work(dssdev);

	r = _taal_enable_te(dssdev, false);
	if (r)
		goto err;

	disable_irq(gpio_to_irq(panel_data->ext_te_gpio));

	omapdss_dsi_display_disable(dssdev, false, true);

	td->ulps_enabled = true;

	return 0;

err:
	dev_err(&dssdev->dev, "enter ULPS failed");
	taal_panel_reset(dssdev);

	td->ulps_enabled = false;

	taal_queue_ulps_work(dssdev);

	return r;
}

static int taal_exit_ulps(struct omap_dss_device *dssdev)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
	int r;

	if (!td->ulps_enabled)
		return 0;

	r = omapdss_dsi_display_enable(dssdev);
415 416 417 418
	if (r) {
		dev_err(&dssdev->dev, "failed to enable DSI\n");
		goto err1;
	}
419

420
	omapdss_dsi_vc_enable_hs(dssdev, td->channel, true);
421 422

	r = _taal_enable_te(dssdev, true);
423 424 425 426
	if (r) {
		dev_err(&dssdev->dev, "failed to re-enable TE");
		goto err2;
	}
427 428 429 430 431 432 433 434 435

	enable_irq(gpio_to_irq(panel_data->ext_te_gpio));

	taal_queue_ulps_work(dssdev);

	td->ulps_enabled = false;

	return 0;

436 437
err2:
	dev_err(&dssdev->dev, "failed to exit ULPS");
438

439 440 441 442 443 444
	r = taal_panel_reset(dssdev);
	if (!r) {
		enable_irq(gpio_to_irq(panel_data->ext_te_gpio));
		td->ulps_enabled = false;
	}
err1:
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	taal_queue_ulps_work(dssdev);

	return r;
}

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

	if (td->ulps_enabled)
		return taal_exit_ulps(dssdev);

	taal_cancel_ulps_work(dssdev);
	taal_queue_ulps_work(dssdev);
	return 0;
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
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);

477 478
	mutex_lock(&td->lock);

479 480
	if (td->enabled) {
		dsi_bus_lock(dssdev);
481

482 483 484
		r = taal_wake_up(dssdev);
		if (!r)
			r = taal_dcs_write_1(td, DCS_BRIGHTNESS, level);
485

486
		dsi_bus_unlock(dssdev);
487
	} else {
488
		r = 0;
489 490
	}

491 492 493
	mutex_unlock(&td->lock);

	return r;
494 495 496 497 498 499 500 501 502 503 504
}

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;
}

L
Lionel Debroux 已提交
505
static const struct backlight_ops taal_bl_ops = {
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
	.get_brightness = taal_bl_get_intensity,
	.update_status  = taal_bl_update_status,
};

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 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);
529
	u8 errors = 0;
530 531
	int r;

532 533
	mutex_lock(&td->lock);

534
	if (td->enabled) {
535
		dsi_bus_lock(dssdev);
536 537 538 539 540

		r = taal_wake_up(dssdev);
		if (!r)
			r = taal_dcs_read_1(td, DCS_READ_NUM_ERRORS, &errors);

541
		dsi_bus_unlock(dssdev);
542 543 544 545
	} else {
		r = -ENODEV;
	}

546 547
	mutex_unlock(&td->lock);

548 549 550 551 552 553 554 555 556 557 558 559 560 561
	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;

562 563
	mutex_lock(&td->lock);

564
	if (td->enabled) {
565
		dsi_bus_lock(dssdev);
566 567 568 569 570

		r = taal_wake_up(dssdev);
		if (!r)
			r = taal_get_id(td, &id1, &id2, &id3);

571
		dsi_bus_unlock(dssdev);
572 573 574 575
	} else {
		r = -ENODEV;
	}

576 577
	mutex_unlock(&td->lock);

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
	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;
618
	int r;
619 620 621 622 623 624 625 626 627

	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;

628 629
	mutex_lock(&td->lock);

630
	if (td->enabled) {
631
		dsi_bus_lock(dssdev);
632 633 634 635 636 637 638 639 640 641 642

		if (!td->cabc_broken) {
			r = taal_wake_up(dssdev);
			if (r)
				goto err;

			r = taal_dcs_write_1(td, DCS_WRITE_CABC, i);
			if (r)
				goto err;
		}

643
		dsi_bus_unlock(dssdev);
644 645 646 647
	}

	td->cabc_mode = i;

648 649
	mutex_unlock(&td->lock);

650
	return count;
651
err:
652
	dsi_bus_unlock(dssdev);
653 654
	mutex_unlock(&td->lock);
	return r;
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
}

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;
}

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
static ssize_t taal_store_esd_interval(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);

	unsigned long t;
	int r;

	r = strict_strtoul(buf, 10, &t);
	if (r)
		return r;

	mutex_lock(&td->lock);
	taal_cancel_esd_work(dssdev);
	td->esd_interval = t;
	if (td->enabled)
		taal_queue_esd_work(dssdev);
	mutex_unlock(&td->lock);

	return count;
}

static ssize_t taal_show_esd_interval(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);
	unsigned t;

	mutex_lock(&td->lock);
	t = td->esd_interval;
	mutex_unlock(&td->lock);

	return snprintf(buf, PAGE_SIZE, "%u\n", t);
}

712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
static ssize_t taal_store_ulps(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);
	unsigned long t;
	int r;

	r = strict_strtoul(buf, 10, &t);
	if (r)
		return r;

	mutex_lock(&td->lock);

	if (td->enabled) {
728
		dsi_bus_lock(dssdev);
729 730 731 732 733 734

		if (t)
			r = taal_enter_ulps(dssdev);
		else
			r = taal_wake_up(dssdev);

735
		dsi_bus_unlock(dssdev);
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
	}

	mutex_unlock(&td->lock);

	if (r)
		return r;

	return count;
}

static ssize_t taal_show_ulps(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);
	unsigned t;

	mutex_lock(&td->lock);
	t = td->ulps_enabled;
	mutex_unlock(&td->lock);

	return snprintf(buf, PAGE_SIZE, "%u\n", t);
}

static ssize_t taal_store_ulps_timeout(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);
	unsigned long t;
	int r;

	r = strict_strtoul(buf, 10, &t);
	if (r)
		return r;

	mutex_lock(&td->lock);
	td->ulps_timeout = t;

	if (td->enabled) {
		/* taal_wake_up will restart the timer */
779
		dsi_bus_lock(dssdev);
780
		r = taal_wake_up(dssdev);
781
		dsi_bus_unlock(dssdev);
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
	}

	mutex_unlock(&td->lock);

	if (r)
		return r;

	return count;
}

static ssize_t taal_show_ulps_timeout(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);
	unsigned t;

	mutex_lock(&td->lock);
	t = td->ulps_timeout;
	mutex_unlock(&td->lock);

	return snprintf(buf, PAGE_SIZE, "%u\n", t);
}

807 808 809 810 811 812
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);
813 814
static DEVICE_ATTR(esd_interval, S_IRUGO | S_IWUSR,
		taal_show_esd_interval, taal_store_esd_interval);
815 816 817 818
static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
		taal_show_ulps, taal_store_ulps);
static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
		taal_show_ulps_timeout, taal_store_ulps_timeout);
819 820 821 822 823 824

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,
825
	&dev_attr_esd_interval.attr,
826 827
	&dev_attr_ulps.attr,
	&dev_attr_ulps_timeout.attr,
828 829 830 831 832 833 834
	NULL,
};

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

835 836
static void taal_hw_reset(struct omap_dss_device *dssdev)
{
837
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
838 839 840
	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);

	if (panel_data->reset_gpio == -1)
841 842
		return;

843
	gpio_set_value(panel_data->reset_gpio, 1);
844 845
	if (td->panel_config->reset_sequence.high)
		udelay(td->panel_config->reset_sequence.high);
846
	/* reset the panel */
847
	gpio_set_value(panel_data->reset_gpio, 0);
848 849 850
	/* assert reset */
	if (td->panel_config->reset_sequence.low)
		udelay(td->panel_config->reset_sequence.low);
851
	gpio_set_value(panel_data->reset_gpio, 1);
852 853 854
	/* wait after releasing reset */
	if (td->panel_config->sleep.hw_reset)
		msleep(td->panel_config->sleep.hw_reset);
855 856
}

857 858
static int taal_probe(struct omap_dss_device *dssdev)
{
859
	struct backlight_properties props;
860
	struct taal_data *td;
861
	struct backlight_device *bldev = NULL;
862
	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
863 864
	struct panel_config *panel_config = NULL;
	int r, i;
865 866 867

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

868 869 870 871 872
	if (!panel_data || !panel_data->name) {
		r = -EINVAL;
		goto err;
	}

873 874 875 876 877 878 879 880 881 882 883 884 885
	for (i = 0; i < ARRAY_SIZE(panel_configs); i++) {
		if (strcmp(panel_data->name, panel_configs[i].name) == 0) {
			panel_config = &panel_configs[i];
			break;
		}
	}

	if (!panel_config) {
		r = -EINVAL;
		goto err;
	}

	dssdev->panel.timings = panel_config->timings;
886
	dssdev->panel.dsi_pix_fmt = OMAP_DSS_DSI_FMT_RGB888;
887 888 889 890

	td = kzalloc(sizeof(*td), GFP_KERNEL);
	if (!td) {
		r = -ENOMEM;
891
		goto err;
892 893
	}
	td->dssdev = dssdev;
894
	td->panel_config = panel_config;
895
	td->esd_interval = panel_data->esd_interval;
896 897
	td->ulps_enabled = false;
	td->ulps_timeout = panel_data->ulps_timeout;
898

899 900
	mutex_init(&td->lock);

901 902
	atomic_set(&td->do_update, 0);

903 904
	td->workqueue = create_singlethread_workqueue("taal_esd");
	if (td->workqueue == NULL) {
905 906
		dev_err(&dssdev->dev, "can't create ESD workqueue\n");
		r = -ENOMEM;
907
		goto err_wq;
908 909
	}
	INIT_DELAYED_WORK_DEFERRABLE(&td->esd_work, taal_esd_work);
910
	INIT_DELAYED_WORK(&td->ulps_work, taal_ulps_work);
911 912 913

	dev_set_drvdata(&dssdev->dev, td);

914 915 916 917 918 919 920 921 922
	if (gpio_is_valid(panel_data->reset_gpio)) {
		r = gpio_request_one(panel_data->reset_gpio, GPIOF_OUT_INIT_LOW,
				"taal rst");
		if (r) {
			dev_err(&dssdev->dev, "failed to request reset gpio\n");
			goto err_rst_gpio;
		}
	}

923 924
	taal_hw_reset(dssdev);

925 926
	if (panel_data->use_dsi_backlight) {
		memset(&props, 0, sizeof(struct backlight_properties));
927
		props.max_brightness = 255;
928

929 930 931 932 933 934 935
		props.type = BACKLIGHT_RAW;
		bldev = backlight_device_register(dev_name(&dssdev->dev),
				&dssdev->dev, dssdev, &taal_bl_ops, &props);
		if (IS_ERR(bldev)) {
			r = PTR_ERR(bldev);
			goto err_bl;
		}
936

937
		td->bldev = bldev;
938

939 940
		bldev->props.fb_blank = FB_BLANK_UNBLANK;
		bldev->props.power = FB_BLANK_UNBLANK;
941 942
		bldev->props.brightness = 255;

943 944
		taal_bl_update_status(bldev);
	}
945

946 947
	if (panel_data->use_ext_te) {
		int gpio = panel_data->ext_te_gpio;
948

J
Jingoo Han 已提交
949
		r = gpio_request_one(gpio, GPIOF_IN, "taal irq");
950 951
		if (r) {
			dev_err(&dssdev->dev, "GPIO request failed\n");
952
			goto err_gpio;
953 954 955
		}

		r = request_irq(gpio_to_irq(gpio), taal_te_isr,
Y
Yong Zhang 已提交
956
				IRQF_TRIGGER_RISING,
957 958 959 960 961
				"taal vsync", dssdev);

		if (r) {
			dev_err(&dssdev->dev, "IRQ request failed\n");
			gpio_free(gpio);
962
			goto err_irq;
963 964
		}

965 966
		INIT_DELAYED_WORK_DEFERRABLE(&td->te_timeout_work,
					taal_te_timeout_work_callback);
967

968
		dev_dbg(&dssdev->dev, "Using GPIO TE\n");
969 970
	}

971 972 973 974 975 976 977 978 979 980 981 982
	r = omap_dsi_request_vc(dssdev, &td->channel);
	if (r) {
		dev_err(&dssdev->dev, "failed to get virtual channel\n");
		goto err_req_vc;
	}

	r = omap_dsi_set_vc_id(dssdev, td->channel, TCH);
	if (r) {
		dev_err(&dssdev->dev, "failed to set VC_ID\n");
		goto err_vc_id;
	}

983 984 985
	r = sysfs_create_group(&dssdev->dev.kobj, &taal_attr_group);
	if (r) {
		dev_err(&dssdev->dev, "failed to create sysfs files\n");
986
		goto err_vc_id;
987 988 989
	}

	return 0;
990 991 992 993

err_vc_id:
	omap_dsi_release_vc(dssdev, td->channel);
err_req_vc:
994 995
	if (panel_data->use_ext_te)
		free_irq(gpio_to_irq(panel_data->ext_te_gpio), dssdev);
996
err_irq:
997 998
	if (panel_data->use_ext_te)
		gpio_free(panel_data->ext_te_gpio);
999
err_gpio:
1000 1001
	if (bldev != NULL)
		backlight_device_unregister(bldev);
1002
err_bl:
1003 1004 1005
	if (gpio_is_valid(panel_data->reset_gpio))
		gpio_free(panel_data->reset_gpio);
err_rst_gpio:
1006
	destroy_workqueue(td->workqueue);
1007
err_wq:
1008
	kfree(td);
1009
err:
1010 1011 1012
	return r;
}

1013
static void __exit taal_remove(struct omap_dss_device *dssdev)
1014 1015
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1016
	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1017 1018 1019 1020 1021
	struct backlight_device *bldev;

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

	sysfs_remove_group(&dssdev->dev.kobj, &taal_attr_group);
1022
	omap_dsi_release_vc(dssdev, td->channel);
1023

1024 1025
	if (panel_data->use_ext_te) {
		int gpio = panel_data->ext_te_gpio;
1026 1027 1028 1029 1030
		free_irq(gpio_to_irq(gpio), dssdev);
		gpio_free(gpio);
	}

	bldev = td->bldev;
1031 1032 1033 1034 1035
	if (bldev != NULL) {
		bldev->props.power = FB_BLANK_POWERDOWN;
		taal_bl_update_status(bldev);
		backlight_device_unregister(bldev);
	}
1036

1037
	taal_cancel_ulps_work(dssdev);
1038
	taal_cancel_esd_work(dssdev);
1039
	destroy_workqueue(td->workqueue);
1040

1041 1042 1043
	/* reset, to be sure that the panel is in a valid state */
	taal_hw_reset(dssdev);

1044 1045 1046
	if (gpio_is_valid(panel_data->reset_gpio))
		gpio_free(panel_data->reset_gpio);

1047 1048 1049
	kfree(td);
}

1050
static int taal_power_on(struct omap_dss_device *dssdev)
1051 1052
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1053
	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1054 1055 1056
	u8 id1, id2, id3;
	int r;

1057 1058 1059 1060 1061 1062
	r = omapdss_dsi_configure_pins(dssdev, &panel_data->pin_config);
	if (r) {
		dev_err(&dssdev->dev, "failed to configure DSI pins\n");
		goto err0;
	};

1063 1064 1065 1066 1067 1068
	r = omapdss_dsi_display_enable(dssdev);
	if (r) {
		dev_err(&dssdev->dev, "failed to enable DSI\n");
		goto err0;
	}

1069 1070
	taal_hw_reset(dssdev);

1071
	omapdss_dsi_vc_enable_hs(dssdev, td->channel, false);
1072

1073 1074 1075 1076
	r = taal_sleep_out(td);
	if (r)
		goto err;

1077
	r = taal_get_id(td, &id1, &id2, &id3);
1078 1079 1080
	if (r)
		goto err;

1081 1082 1083
	/* on early Taal revisions CABC is broken */
	if (td->panel_config->type == PANEL_TAAL &&
		(id2 == 0x00 || id2 == 0xff || id2 == 0x81))
1084 1085
		td->cabc_broken = true;

1086
	r = taal_dcs_write_1(td, DCS_BRIGHTNESS, 0xff);
1087 1088
	if (r)
		goto err;
1089

1090
	r = taal_dcs_write_1(td, DCS_CTRL_DISPLAY,
1091 1092 1093
			(1<<2) | (1<<5));	/* BL | BCTRL */
	if (r)
		goto err;
1094

1095 1096
	r = taal_dcs_write_1(td, MIPI_DCS_SET_PIXEL_FORMAT,
		MIPI_DCS_PIXEL_FMT_24BIT);
1097 1098
	if (r)
		goto err;
1099

1100
	r = taal_set_addr_mode(td, td->rotate, td->mirror);
1101 1102 1103 1104
	if (r)
		goto err;

	if (!td->cabc_broken) {
1105
		r = taal_dcs_write_1(td, DCS_WRITE_CABC, td->cabc_mode);
1106 1107 1108 1109
		if (r)
			goto err;
	}

1110
	r = taal_dcs_write_0(td, MIPI_DCS_SET_DISPLAY_ON);
1111 1112
	if (r)
		goto err;
1113

1114 1115 1116 1117
	r = _taal_enable_te(dssdev, td->te_enabled);
	if (r)
		goto err;

1118 1119 1120 1121
	r = dsi_enable_video_output(dssdev, td->channel);
	if (r)
		goto err;

1122 1123 1124
	td->enabled = 1;

	if (!td->intro_printed) {
1125 1126
		dev_info(&dssdev->dev, "%s panel revision %02x.%02x.%02x\n",
			td->panel_config->name, id1, id2, id3);
1127 1128 1129 1130 1131 1132
		if (td->cabc_broken)
			dev_info(&dssdev->dev,
					"old Taal version, CABC disabled\n");
		td->intro_printed = true;
	}

1133
	omapdss_dsi_vc_enable_hs(dssdev, td->channel, true);
1134

1135 1136
	return 0;
err:
1137 1138 1139 1140
	dev_err(&dssdev->dev, "error while enabling panel, issuing HW reset\n");

	taal_hw_reset(dssdev);

1141
	omapdss_dsi_display_disable(dssdev, true, false);
1142
err0:
1143 1144 1145
	return r;
}

1146
static void taal_power_off(struct omap_dss_device *dssdev)
1147 1148
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1149
	int r;
1150

1151 1152
	dsi_disable_video_output(dssdev, td->channel);

1153
	r = taal_dcs_write_0(td, MIPI_DCS_SET_DISPLAY_OFF);
1154
	if (!r)
1155
		r = taal_sleep_in(td);
1156

1157 1158 1159 1160 1161
	if (r) {
		dev_err(&dssdev->dev,
				"error disabling panel, issuing HW reset\n");
		taal_hw_reset(dssdev);
	}
1162

1163
	omapdss_dsi_display_disable(dssdev, true, false);
1164

1165
	td->enabled = 0;
1166 1167
}

1168 1169 1170 1171 1172 1173 1174 1175 1176
static int taal_panel_reset(struct omap_dss_device *dssdev)
{
	dev_err(&dssdev->dev, "performing LCD reset\n");

	taal_power_off(dssdev);
	taal_hw_reset(dssdev);
	return taal_power_on(dssdev);
}

1177 1178
static int taal_enable(struct omap_dss_device *dssdev)
{
1179
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1180
	int r;
1181

1182 1183
	dev_dbg(&dssdev->dev, "enable\n");

1184 1185 1186 1187 1188 1189
	mutex_lock(&td->lock);

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

1191
	dsi_bus_lock(dssdev);
1192

1193
	r = taal_power_on(dssdev);
1194

1195
	dsi_bus_unlock(dssdev);
1196

1197
	if (r)
1198
		goto err;
1199

1200
	taal_queue_esd_work(dssdev);
1201

1202 1203
	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;

1204 1205 1206 1207 1208 1209
	mutex_unlock(&td->lock);

	return 0;
err:
	dev_dbg(&dssdev->dev, "enable failed\n");
	mutex_unlock(&td->lock);
1210 1211 1212 1213 1214
	return r;
}

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

1217 1218
	dev_dbg(&dssdev->dev, "disable\n");

1219 1220
	mutex_lock(&td->lock);

1221
	taal_cancel_ulps_work(dssdev);
1222
	taal_cancel_esd_work(dssdev);
1223

1224
	dsi_bus_lock(dssdev);
1225

1226
	if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
1227 1228 1229 1230 1231
		int r;

		r = taal_wake_up(dssdev);
		if (!r)
			taal_power_off(dssdev);
1232
	}
1233

1234
	dsi_bus_unlock(dssdev);
1235

1236
	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
1237 1238

	mutex_unlock(&td->lock);
1239 1240 1241 1242
}

static int taal_suspend(struct omap_dss_device *dssdev)
{
1243 1244 1245
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
	int r;

1246
	dev_dbg(&dssdev->dev, "suspend\n");
1247

1248 1249 1250 1251 1252 1253
	mutex_lock(&td->lock);

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

1255
	taal_cancel_ulps_work(dssdev);
1256
	taal_cancel_esd_work(dssdev);
1257

1258
	dsi_bus_lock(dssdev);
1259

1260 1261 1262
	r = taal_wake_up(dssdev);
	if (!r)
		taal_power_off(dssdev);
1263

1264
	dsi_bus_unlock(dssdev);
1265

1266
	dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
1267

1268 1269
	mutex_unlock(&td->lock);

1270
	return 0;
1271 1272 1273
err:
	mutex_unlock(&td->lock);
	return r;
1274 1275 1276 1277
}

static int taal_resume(struct omap_dss_device *dssdev)
{
1278
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1279
	int r;
1280

1281
	dev_dbg(&dssdev->dev, "resume\n");
1282

1283 1284 1285 1286 1287 1288
	mutex_lock(&td->lock);

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

1290
	dsi_bus_lock(dssdev);
1291

1292
	r = taal_power_on(dssdev);
1293

1294
	dsi_bus_unlock(dssdev);
1295

1296
	if (r) {
1297
		dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
1298
	} else {
1299
		dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
1300
		taal_queue_esd_work(dssdev);
1301
	}
1302 1303 1304 1305 1306 1307

	mutex_unlock(&td->lock);

	return r;
err:
	mutex_unlock(&td->lock);
1308
	return r;
1309 1310
}

1311 1312 1313 1314
static void taal_framedone_cb(int err, void *data)
{
	struct omap_dss_device *dssdev = data;
	dev_dbg(&dssdev->dev, "framedone, err %d\n", err);
1315
	dsi_bus_unlock(dssdev);
1316 1317
}

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
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);
	int old;
	int r;

	old = atomic_cmpxchg(&td->do_update, 1, 0);

	if (old) {
		cancel_delayed_work(&td->te_timeout_work);

1330 1331
		r = omap_dsi_update(dssdev, td->channel, taal_framedone_cb,
				dssdev);
1332 1333 1334 1335 1336 1337 1338
		if (r)
			goto err;
	}

	return IRQ_HANDLED;
err:
	dev_err(&dssdev->dev, "start update failed\n");
1339
	dsi_bus_unlock(dssdev);
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
	return IRQ_HANDLED;
}

static void taal_te_timeout_work_callback(struct work_struct *work)
{
	struct taal_data *td = container_of(work, struct taal_data,
					te_timeout_work.work);
	struct omap_dss_device *dssdev = td->dssdev;

	dev_err(&dssdev->dev, "TE not received for 250ms!\n");

	atomic_set(&td->do_update, 0);
1352
	dsi_bus_unlock(dssdev);
1353 1354
}

1355
static int taal_update(struct omap_dss_device *dssdev,
1356 1357
				    u16 x, u16 y, u16 w, u16 h)
{
1358
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1359
	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1360 1361 1362 1363
	int r;

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

1364
	mutex_lock(&td->lock);
1365
	dsi_bus_lock(dssdev);
1366

1367 1368 1369 1370
	r = taal_wake_up(dssdev);
	if (r)
		goto err;

1371 1372 1373 1374 1375
	if (!td->enabled) {
		r = 0;
		goto err;
	}

1376 1377 1378 1379
	/* XXX no need to send this every frame, but dsi break if not done */
	r = taal_set_update_window(td, 0, 0,
			td->panel_config->timings.x_res,
			td->panel_config->timings.y_res);
1380 1381 1382
	if (r)
		goto err;

1383
	if (td->te_enabled && panel_data->use_ext_te) {
1384 1385 1386 1387
		schedule_delayed_work(&td->te_timeout_work,
				msecs_to_jiffies(250));
		atomic_set(&td->do_update, 1);
	} else {
1388 1389
		r = omap_dsi_update(dssdev, td->channel, taal_framedone_cb,
				dssdev);
1390 1391 1392
		if (r)
			goto err;
	}
1393 1394

	/* note: no bus_unlock here. unlock is in framedone_cb */
1395
	mutex_unlock(&td->lock);
1396 1397
	return 0;
err:
1398
	dsi_bus_unlock(dssdev);
1399
	mutex_unlock(&td->lock);
1400 1401 1402 1403 1404
	return r;
}

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

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

1409
	mutex_lock(&td->lock);
1410 1411
	dsi_bus_lock(dssdev);
	dsi_bus_unlock(dssdev);
1412
	mutex_unlock(&td->lock);
1413 1414 1415 1416

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

	return 0;
1417 1418
}

1419
static int _taal_enable_te(struct omap_dss_device *dssdev, bool enable)
1420
{
1421
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1422
	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1423 1424 1425
	int r;

	if (enable)
1426
		r = taal_dcs_write_1(td, MIPI_DCS_SET_TEAR_ON, 0);
1427
	else
1428
		r = taal_dcs_write_0(td, MIPI_DCS_SET_TEAR_OFF);
1429

1430
	if (!panel_data->use_ext_te)
1431
		omapdss_dsi_enable_te(dssdev, enable);
1432

1433 1434
	if (td->panel_config->sleep.enable_te)
		msleep(td->panel_config->sleep.enable_te);
1435

1436 1437 1438 1439 1440
	return r;
}

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

1444
	mutex_lock(&td->lock);
1445 1446 1447 1448

	if (td->te_enabled == enable)
		goto end;

1449
	dsi_bus_lock(dssdev);
1450

1451
	if (td->enabled) {
1452 1453 1454 1455
		r = taal_wake_up(dssdev);
		if (r)
			goto err;

1456 1457 1458 1459 1460 1461
		r = _taal_enable_te(dssdev, enable);
		if (r)
			goto err;
	}

	td->te_enabled = enable;
1462

1463
	dsi_bus_unlock(dssdev);
1464
end:
1465
	mutex_unlock(&td->lock);
1466

1467 1468
	return 0;
err:
1469
	dsi_bus_unlock(dssdev);
1470 1471
	mutex_unlock(&td->lock);

1472 1473 1474
	return r;
}

1475 1476 1477
static int taal_get_te(struct omap_dss_device *dssdev)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1478 1479 1480 1481 1482 1483 1484
	int r;

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

	return r;
1485 1486
}

1487 1488 1489 1490 1491 1492 1493
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);

1494
	mutex_lock(&td->lock);
1495 1496 1497 1498

	if (td->rotate == rotate)
		goto end;

1499
	dsi_bus_lock(dssdev);
1500

1501
	if (td->enabled) {
1502 1503 1504 1505
		r = taal_wake_up(dssdev);
		if (r)
			goto err;

1506
		r = taal_set_addr_mode(td, rotate, td->mirror);
1507
		if (r)
1508
			goto err;
1509 1510 1511 1512
	}

	td->rotate = rotate;

1513
	dsi_bus_unlock(dssdev);
1514
end:
1515
	mutex_unlock(&td->lock);
1516
	return 0;
1517
err:
1518
	dsi_bus_unlock(dssdev);
1519
	mutex_unlock(&td->lock);
1520
	return r;
1521 1522 1523 1524 1525
}

static u8 taal_get_rotate(struct omap_dss_device *dssdev)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1526 1527 1528 1529 1530 1531 1532
	int r;

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

	return r;
1533 1534 1535 1536 1537 1538 1539 1540 1541
}

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);

1542
	mutex_lock(&td->lock);
1543 1544 1545 1546

	if (td->mirror == enable)
		goto end;

1547
	dsi_bus_lock(dssdev);
1548
	if (td->enabled) {
1549 1550 1551 1552
		r = taal_wake_up(dssdev);
		if (r)
			goto err;

1553
		r = taal_set_addr_mode(td, td->rotate, enable);
1554
		if (r)
1555
			goto err;
1556 1557 1558 1559
	}

	td->mirror = enable;

1560
	dsi_bus_unlock(dssdev);
1561
end:
1562
	mutex_unlock(&td->lock);
1563
	return 0;
1564
err:
1565
	dsi_bus_unlock(dssdev);
1566
	mutex_unlock(&td->lock);
1567
	return r;
1568 1569 1570 1571 1572
}

static bool taal_get_mirror(struct omap_dss_device *dssdev)
{
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1573 1574 1575 1576 1577 1578 1579
	int r;

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

	return r;
1580 1581 1582 1583
}

static int taal_run_test(struct omap_dss_device *dssdev, int test_num)
{
1584
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1585 1586 1587
	u8 id1, id2, id3;
	int r;

1588
	mutex_lock(&td->lock);
1589 1590 1591 1592 1593 1594

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

1595
	dsi_bus_lock(dssdev);
T
Tomi Valkeinen 已提交
1596

1597 1598 1599 1600
	r = taal_wake_up(dssdev);
	if (r)
		goto err2;

1601
	r = taal_dcs_read_1(td, DCS_GET_ID1, &id1);
1602
	if (r)
1603
		goto err2;
1604
	r = taal_dcs_read_1(td, DCS_GET_ID2, &id2);
1605
	if (r)
1606
		goto err2;
1607
	r = taal_dcs_read_1(td, DCS_GET_ID3, &id3);
1608
	if (r)
1609
		goto err2;
1610

1611
	dsi_bus_unlock(dssdev);
1612
	mutex_unlock(&td->lock);
1613
	return 0;
1614
err2:
1615
	dsi_bus_unlock(dssdev);
1616
err1:
1617
	mutex_unlock(&td->lock);
T
Tomi Valkeinen 已提交
1618
	return r;
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
}

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 已提交
1629 1630
	struct taal_data *td = dev_get_drvdata(&dssdev->dev);

1631 1632 1633
	if (size < w * h * 3)
		return -ENOMEM;

1634 1635 1636 1637 1638 1639 1640
	mutex_lock(&td->lock);

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

1641 1642 1643 1644
	size = min(w * h * 3,
			dssdev->panel.timings.x_res *
			dssdev->panel.timings.y_res * 3);

1645
	dsi_bus_lock(dssdev);
T
Tomi Valkeinen 已提交
1646

1647 1648 1649 1650
	r = taal_wake_up(dssdev);
	if (r)
		goto err2;

1651 1652 1653 1654 1655 1656 1657 1658
	/* 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;

1659
	taal_set_update_window(td, x, y, w, h);
1660

1661
	r = dsi_vc_set_max_rx_packet_size(dssdev, td->channel, plen);
1662
	if (r)
1663
		goto err2;
1664 1665 1666 1667 1668

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

1669
		r = dsi_vc_dcs_read(dssdev, td->channel, dcs_cmd,
1670 1671 1672 1673
				buf + buf_used, size - buf_used);

		if (r < 0) {
			dev_err(&dssdev->dev, "read error\n");
1674
			goto err3;
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
		}

		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;
1688
			goto err3;
1689 1690 1691 1692 1693
		}
	}

	r = buf_used;

1694
err3:
1695
	dsi_vc_set_max_rx_packet_size(dssdev, td->channel, 1);
1696
err2:
1697
	dsi_bus_unlock(dssdev);
1698 1699
err1:
	mutex_unlock(&td->lock);
1700 1701 1702
	return r;
}

1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
static void taal_ulps_work(struct work_struct *work)
{
	struct taal_data *td = container_of(work, struct taal_data,
			ulps_work.work);
	struct omap_dss_device *dssdev = td->dssdev;

	mutex_lock(&td->lock);

	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !td->enabled) {
		mutex_unlock(&td->lock);
		return;
	}

1716
	dsi_bus_lock(dssdev);
1717 1718 1719

	taal_enter_ulps(dssdev);

1720
	dsi_bus_unlock(dssdev);
1721 1722 1723
	mutex_unlock(&td->lock);
}

1724 1725 1726 1727 1728
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;
1729
	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1730 1731 1732
	u8 state1, state2;
	int r;

1733 1734 1735 1736
	mutex_lock(&td->lock);

	if (!td->enabled) {
		mutex_unlock(&td->lock);
1737
		return;
1738
	}
1739

1740
	dsi_bus_lock(dssdev);
1741

1742 1743 1744 1745 1746 1747
	r = taal_wake_up(dssdev);
	if (r) {
		dev_err(&dssdev->dev, "failed to exit ULPS\n");
		goto err;
	}

1748
	r = taal_dcs_read_1(td, MIPI_DCS_GET_DIAGNOSTIC_RESULT, &state1);
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
	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;
	}

1761
	r = taal_dcs_read_1(td, MIPI_DCS_GET_DIAGNOSTIC_RESULT, &state2);
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
	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 */
1776
	if (td->te_enabled && panel_data->use_ext_te) {
1777
		r = taal_dcs_write_1(td, MIPI_DCS_SET_TEAR_ON, 0);
T
Tomi Valkeinen 已提交
1778 1779 1780
		if (r)
			goto err;
	}
1781

1782
	dsi_bus_unlock(dssdev);
1783

1784
	taal_queue_esd_work(dssdev);
1785

1786
	mutex_unlock(&td->lock);
1787 1788 1789 1790
	return;
err:
	dev_err(&dssdev->dev, "performing LCD reset\n");

1791
	taal_panel_reset(dssdev);
1792

1793
	dsi_bus_unlock(dssdev);
1794

1795
	taal_queue_esd_work(dssdev);
1796 1797

	mutex_unlock(&td->lock);
1798 1799 1800 1801
}

static struct omap_dss_driver taal_driver = {
	.probe		= taal_probe,
1802
	.remove		= __exit_p(taal_remove),
1803 1804 1805 1806 1807 1808

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

1809 1810 1811
	.update		= taal_update,
	.sync		= taal_sync,

1812
	.get_resolution	= taal_get_resolution,
1813 1814
	.get_recommended_bpp = omapdss_default_get_recommended_bpp,

1815
	.enable_te	= taal_enable_te,
1816 1817
	.get_te		= taal_get_te,

1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
	.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,

	.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");