Linux Audio

Check our new training course

Loading...
v4.6
 
   1/*
   2 * Generic DSI Command Mode panel driver
   3 *
   4 * Copyright (C) 2013 Texas Instruments
   5 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify it
   8 * under the terms of the GNU General Public License version 2 as published by
   9 * the Free Software Foundation.
  10 */
  11
  12/* #define DEBUG */
  13
  14#include <linux/backlight.h>
  15#include <linux/delay.h>
  16#include <linux/fb.h>
  17#include <linux/gpio.h>
  18#include <linux/interrupt.h>
  19#include <linux/jiffies.h>
  20#include <linux/module.h>
  21#include <linux/platform_device.h>
  22#include <linux/sched.h>
  23#include <linux/slab.h>
  24#include <linux/workqueue.h>
  25#include <linux/of_device.h>
  26#include <linux/of_gpio.h>
 
 
  27
  28#include <video/omapdss.h>
  29#include <video/omap-panel-data.h>
  30#include <video/mipi_display.h>
 
 
 
  31
  32/* DSI Virtual channel. Hardcoded for now. */
  33#define TCH 0
  34
  35#define DCS_READ_NUM_ERRORS	0x05
  36#define DCS_BRIGHTNESS		0x51
  37#define DCS_CTRL_DISPLAY	0x53
  38#define DCS_GET_ID1		0xda
  39#define DCS_GET_ID2		0xdb
  40#define DCS_GET_ID3		0xdc
  41
  42struct panel_drv_data {
  43	struct omap_dss_device dssdev;
  44	struct omap_dss_device *in;
  45
  46	struct omap_video_timings timings;
  47
  48	struct platform_device *pdev;
  49
  50	struct mutex lock;
  51
  52	struct backlight_device *bldev;
 
  53
  54	unsigned long	hw_guard_end;	/* next value of jiffies when we can
  55					 * issue the next sleep in/out command
  56					 */
  57	unsigned long	hw_guard_wait;	/* max guard time in jiffies */
  58
  59	/* panel HW configuration from DT or platform data */
  60	int reset_gpio;
  61	int ext_te_gpio;
 
 
 
  62
  63	bool use_dsi_backlight;
  64
 
 
 
  65	struct omap_dsi_pin_config pin_config;
  66
  67	/* runtime variables */
  68	bool enabled;
  69
  70	bool te_enabled;
  71
  72	atomic_t do_update;
  73	int channel;
  74
  75	struct delayed_work te_timeout_work;
  76
  77	bool intro_printed;
  78
  79	struct workqueue_struct *workqueue;
  80
  81	bool ulps_enabled;
  82	unsigned ulps_timeout;
  83	struct delayed_work ulps_work;
  84};
  85
  86#define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
  87
  88static irqreturn_t dsicm_te_isr(int irq, void *data);
  89static void dsicm_te_timeout_work_callback(struct work_struct *work);
  90static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable);
  91
  92static int dsicm_panel_reset(struct panel_drv_data *ddata);
  93
  94static void dsicm_ulps_work(struct work_struct *work);
  95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  96static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec)
  97{
  98	ddata->hw_guard_wait = msecs_to_jiffies(guard_msec);
  99	ddata->hw_guard_end = jiffies + ddata->hw_guard_wait;
 100}
 101
 102static void hw_guard_wait(struct panel_drv_data *ddata)
 103{
 104	unsigned long wait = ddata->hw_guard_end - jiffies;
 105
 106	if ((long)wait > 0 && wait <= ddata->hw_guard_wait) {
 107		set_current_state(TASK_UNINTERRUPTIBLE);
 108		schedule_timeout(wait);
 109	}
 110}
 111
 112static int dsicm_dcs_read_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 *data)
 113{
 114	struct omap_dss_device *in = ddata->in;
 115	int r;
 116	u8 buf[1];
 117
 118	r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd, buf, 1);
 119
 120	if (r < 0)
 121		return r;
 122
 123	*data = buf[0];
 124
 125	return 0;
 126}
 127
 128static int dsicm_dcs_write_0(struct panel_drv_data *ddata, u8 dcs_cmd)
 129{
 130	struct omap_dss_device *in = ddata->in;
 131	return in->ops.dsi->dcs_write(in, ddata->channel, &dcs_cmd, 1);
 
 132}
 133
 134static int dsicm_dcs_write_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 param)
 135{
 136	struct omap_dss_device *in = ddata->in;
 137	u8 buf[2] = { dcs_cmd, param };
 138
 139	return in->ops.dsi->dcs_write(in, ddata->channel, buf, 2);
 140}
 141
 142static int dsicm_sleep_in(struct panel_drv_data *ddata)
 143
 144{
 145	struct omap_dss_device *in = ddata->in;
 146	u8 cmd;
 147	int r;
 148
 149	hw_guard_wait(ddata);
 150
 151	cmd = MIPI_DCS_ENTER_SLEEP_MODE;
 152	r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, &cmd, 1);
 153	if (r)
 154		return r;
 155
 156	hw_guard_start(ddata, 120);
 157
 158	usleep_range(5000, 10000);
 159
 160	return 0;
 161}
 162
 163static int dsicm_sleep_out(struct panel_drv_data *ddata)
 164{
 165	int r;
 166
 167	hw_guard_wait(ddata);
 168
 169	r = dsicm_dcs_write_0(ddata, MIPI_DCS_EXIT_SLEEP_MODE);
 170	if (r)
 171		return r;
 172
 173	hw_guard_start(ddata, 120);
 174
 175	usleep_range(5000, 10000);
 176
 177	return 0;
 178}
 179
 180static int dsicm_get_id(struct panel_drv_data *ddata, u8 *id1, u8 *id2, u8 *id3)
 181{
 182	int r;
 183
 184	r = dsicm_dcs_read_1(ddata, DCS_GET_ID1, id1);
 185	if (r)
 186		return r;
 187	r = dsicm_dcs_read_1(ddata, DCS_GET_ID2, id2);
 188	if (r)
 189		return r;
 190	r = dsicm_dcs_read_1(ddata, DCS_GET_ID3, id3);
 191	if (r)
 192		return r;
 193
 194	return 0;
 195}
 196
 197static int dsicm_set_update_window(struct panel_drv_data *ddata,
 198		u16 x, u16 y, u16 w, u16 h)
 199{
 200	struct omap_dss_device *in = ddata->in;
 201	int r;
 202	u16 x1 = x;
 203	u16 x2 = x + w - 1;
 204	u16 y1 = y;
 205	u16 y2 = y + h - 1;
 206
 207	u8 buf[5];
 208	buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS;
 209	buf[1] = (x1 >> 8) & 0xff;
 210	buf[2] = (x1 >> 0) & 0xff;
 211	buf[3] = (x2 >> 8) & 0xff;
 212	buf[4] = (x2 >> 0) & 0xff;
 213
 214	r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
 215	if (r)
 216		return r;
 217
 218	buf[0] = MIPI_DCS_SET_PAGE_ADDRESS;
 219	buf[1] = (y1 >> 8) & 0xff;
 220	buf[2] = (y1 >> 0) & 0xff;
 221	buf[3] = (y2 >> 8) & 0xff;
 222	buf[4] = (y2 >> 0) & 0xff;
 223
 224	r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
 225	if (r)
 226		return r;
 227
 228	in->ops.dsi->bta_sync(in, ddata->channel);
 229
 230	return r;
 231}
 232
 233static void dsicm_queue_ulps_work(struct panel_drv_data *ddata)
 234{
 235	if (ddata->ulps_timeout > 0)
 236		queue_delayed_work(ddata->workqueue, &ddata->ulps_work,
 237				msecs_to_jiffies(ddata->ulps_timeout));
 238}
 239
 240static void dsicm_cancel_ulps_work(struct panel_drv_data *ddata)
 241{
 242	cancel_delayed_work(&ddata->ulps_work);
 243}
 244
 245static int dsicm_enter_ulps(struct panel_drv_data *ddata)
 246{
 247	struct omap_dss_device *in = ddata->in;
 248	int r;
 249
 250	if (ddata->ulps_enabled)
 251		return 0;
 252
 253	dsicm_cancel_ulps_work(ddata);
 254
 255	r = _dsicm_enable_te(ddata, false);
 256	if (r)
 257		goto err;
 258
 259	if (gpio_is_valid(ddata->ext_te_gpio))
 260		disable_irq(gpio_to_irq(ddata->ext_te_gpio));
 261
 262	in->ops.dsi->disable(in, false, true);
 263
 264	ddata->ulps_enabled = true;
 265
 266	return 0;
 267
 268err:
 269	dev_err(&ddata->pdev->dev, "enter ULPS failed");
 270	dsicm_panel_reset(ddata);
 271
 272	ddata->ulps_enabled = false;
 273
 274	dsicm_queue_ulps_work(ddata);
 275
 276	return r;
 277}
 278
 279static int dsicm_exit_ulps(struct panel_drv_data *ddata)
 280{
 281	struct omap_dss_device *in = ddata->in;
 282	int r;
 283
 284	if (!ddata->ulps_enabled)
 285		return 0;
 286
 287	r = in->ops.dsi->enable(in);
 288	if (r) {
 289		dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
 290		goto err1;
 291	}
 292
 293	in->ops.dsi->enable_hs(in, ddata->channel, true);
 294
 295	r = _dsicm_enable_te(ddata, true);
 296	if (r) {
 297		dev_err(&ddata->pdev->dev, "failed to re-enable TE");
 298		goto err2;
 299	}
 300
 301	if (gpio_is_valid(ddata->ext_te_gpio))
 302		enable_irq(gpio_to_irq(ddata->ext_te_gpio));
 303
 304	dsicm_queue_ulps_work(ddata);
 305
 306	ddata->ulps_enabled = false;
 307
 308	return 0;
 309
 310err2:
 311	dev_err(&ddata->pdev->dev, "failed to exit ULPS");
 312
 313	r = dsicm_panel_reset(ddata);
 314	if (!r) {
 315		if (gpio_is_valid(ddata->ext_te_gpio))
 316			enable_irq(gpio_to_irq(ddata->ext_te_gpio));
 317		ddata->ulps_enabled = false;
 318	}
 319err1:
 320	dsicm_queue_ulps_work(ddata);
 321
 322	return r;
 323}
 324
 325static int dsicm_wake_up(struct panel_drv_data *ddata)
 326{
 327	if (ddata->ulps_enabled)
 328		return dsicm_exit_ulps(ddata);
 329
 330	dsicm_cancel_ulps_work(ddata);
 331	dsicm_queue_ulps_work(ddata);
 332	return 0;
 333}
 334
 335static int dsicm_bl_update_status(struct backlight_device *dev)
 336{
 337	struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
 338	struct omap_dss_device *in = ddata->in;
 339	int r;
 340	int level;
 341
 342	if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
 343			dev->props.power == FB_BLANK_UNBLANK)
 344		level = dev->props.brightness;
 345	else
 346		level = 0;
 347
 348	dev_dbg(&ddata->pdev->dev, "update brightness to %d\n", level);
 349
 350	mutex_lock(&ddata->lock);
 351
 352	if (ddata->enabled) {
 353		in->ops.dsi->bus_lock(in);
 354
 355		r = dsicm_wake_up(ddata);
 356		if (!r)
 357			r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, level);
 358
 359		in->ops.dsi->bus_unlock(in);
 360	} else {
 361		r = 0;
 362	}
 363
 364	mutex_unlock(&ddata->lock);
 365
 366	return r;
 367}
 368
 369static int dsicm_bl_get_intensity(struct backlight_device *dev)
 370{
 371	if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
 372			dev->props.power == FB_BLANK_UNBLANK)
 373		return dev->props.brightness;
 374
 375	return 0;
 376}
 377
 378static const struct backlight_ops dsicm_bl_ops = {
 379	.get_brightness = dsicm_bl_get_intensity,
 380	.update_status  = dsicm_bl_update_status,
 381};
 382
 383static void dsicm_get_resolution(struct omap_dss_device *dssdev,
 384		u16 *xres, u16 *yres)
 385{
 386	*xres = dssdev->panel.timings.x_res;
 387	*yres = dssdev->panel.timings.y_res;
 388}
 389
 390static ssize_t dsicm_num_errors_show(struct device *dev,
 391		struct device_attribute *attr, char *buf)
 392{
 393	struct platform_device *pdev = to_platform_device(dev);
 394	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
 395	struct omap_dss_device *in = ddata->in;
 396	u8 errors = 0;
 397	int r;
 398
 399	mutex_lock(&ddata->lock);
 400
 401	if (ddata->enabled) {
 402		in->ops.dsi->bus_lock(in);
 403
 404		r = dsicm_wake_up(ddata);
 405		if (!r)
 406			r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS,
 407					&errors);
 408
 409		in->ops.dsi->bus_unlock(in);
 410	} else {
 411		r = -ENODEV;
 412	}
 413
 414	mutex_unlock(&ddata->lock);
 415
 416	if (r)
 417		return r;
 418
 419	return snprintf(buf, PAGE_SIZE, "%d\n", errors);
 420}
 421
 422static ssize_t dsicm_hw_revision_show(struct device *dev,
 423		struct device_attribute *attr, char *buf)
 424{
 425	struct platform_device *pdev = to_platform_device(dev);
 426	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
 427	struct omap_dss_device *in = ddata->in;
 428	u8 id1, id2, id3;
 429	int r;
 430
 431	mutex_lock(&ddata->lock);
 432
 433	if (ddata->enabled) {
 434		in->ops.dsi->bus_lock(in);
 435
 436		r = dsicm_wake_up(ddata);
 437		if (!r)
 438			r = dsicm_get_id(ddata, &id1, &id2, &id3);
 439
 440		in->ops.dsi->bus_unlock(in);
 441	} else {
 442		r = -ENODEV;
 443	}
 444
 445	mutex_unlock(&ddata->lock);
 446
 447	if (r)
 448		return r;
 449
 450	return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
 451}
 452
 453static ssize_t dsicm_store_ulps(struct device *dev,
 454		struct device_attribute *attr,
 455		const char *buf, size_t count)
 456{
 457	struct platform_device *pdev = to_platform_device(dev);
 458	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
 459	struct omap_dss_device *in = ddata->in;
 460	unsigned long t;
 461	int r;
 462
 463	r = kstrtoul(buf, 0, &t);
 464	if (r)
 465		return r;
 466
 467	mutex_lock(&ddata->lock);
 468
 469	if (ddata->enabled) {
 470		in->ops.dsi->bus_lock(in);
 471
 472		if (t)
 473			r = dsicm_enter_ulps(ddata);
 474		else
 475			r = dsicm_wake_up(ddata);
 476
 477		in->ops.dsi->bus_unlock(in);
 478	}
 479
 480	mutex_unlock(&ddata->lock);
 481
 482	if (r)
 483		return r;
 484
 485	return count;
 486}
 487
 488static ssize_t dsicm_show_ulps(struct device *dev,
 489		struct device_attribute *attr,
 490		char *buf)
 491{
 492	struct platform_device *pdev = to_platform_device(dev);
 493	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
 494	unsigned t;
 495
 496	mutex_lock(&ddata->lock);
 497	t = ddata->ulps_enabled;
 498	mutex_unlock(&ddata->lock);
 499
 500	return snprintf(buf, PAGE_SIZE, "%u\n", t);
 501}
 502
 503static ssize_t dsicm_store_ulps_timeout(struct device *dev,
 504		struct device_attribute *attr,
 505		const char *buf, size_t count)
 506{
 507	struct platform_device *pdev = to_platform_device(dev);
 508	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
 509	struct omap_dss_device *in = ddata->in;
 510	unsigned long t;
 511	int r;
 512
 513	r = kstrtoul(buf, 0, &t);
 514	if (r)
 515		return r;
 516
 517	mutex_lock(&ddata->lock);
 518	ddata->ulps_timeout = t;
 519
 520	if (ddata->enabled) {
 521		/* dsicm_wake_up will restart the timer */
 522		in->ops.dsi->bus_lock(in);
 523		r = dsicm_wake_up(ddata);
 524		in->ops.dsi->bus_unlock(in);
 525	}
 526
 527	mutex_unlock(&ddata->lock);
 528
 529	if (r)
 530		return r;
 531
 532	return count;
 533}
 534
 535static ssize_t dsicm_show_ulps_timeout(struct device *dev,
 536		struct device_attribute *attr,
 537		char *buf)
 538{
 539	struct platform_device *pdev = to_platform_device(dev);
 540	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
 541	unsigned t;
 542
 543	mutex_lock(&ddata->lock);
 544	t = ddata->ulps_timeout;
 545	mutex_unlock(&ddata->lock);
 546
 547	return snprintf(buf, PAGE_SIZE, "%u\n", t);
 548}
 549
 550static DEVICE_ATTR(num_dsi_errors, S_IRUGO, dsicm_num_errors_show, NULL);
 551static DEVICE_ATTR(hw_revision, S_IRUGO, dsicm_hw_revision_show, NULL);
 552static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
 553		dsicm_show_ulps, dsicm_store_ulps);
 554static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
 555		dsicm_show_ulps_timeout, dsicm_store_ulps_timeout);
 556
 557static struct attribute *dsicm_attrs[] = {
 558	&dev_attr_num_dsi_errors.attr,
 559	&dev_attr_hw_revision.attr,
 560	&dev_attr_ulps.attr,
 561	&dev_attr_ulps_timeout.attr,
 562	NULL,
 563};
 564
 565static struct attribute_group dsicm_attr_group = {
 566	.attrs = dsicm_attrs,
 567};
 568
 569static void dsicm_hw_reset(struct panel_drv_data *ddata)
 570{
 571	if (!gpio_is_valid(ddata->reset_gpio))
 572		return;
 573
 574	gpio_set_value(ddata->reset_gpio, 1);
 575	udelay(10);
 576	/* reset the panel */
 577	gpio_set_value(ddata->reset_gpio, 0);
 578	/* assert reset */
 579	udelay(10);
 580	gpio_set_value(ddata->reset_gpio, 1);
 581	/* wait after releasing reset */
 582	usleep_range(5000, 10000);
 583}
 584
 585static int dsicm_power_on(struct panel_drv_data *ddata)
 586{
 587	struct omap_dss_device *in = ddata->in;
 588	u8 id1, id2, id3;
 589	int r;
 590	struct omap_dss_dsi_config dsi_config = {
 591		.mode = OMAP_DSS_DSI_CMD_MODE,
 592		.pixel_format = OMAP_DSS_DSI_FMT_RGB888,
 593		.timings = &ddata->timings,
 594		.hs_clk_min = 150000000,
 595		.hs_clk_max = 300000000,
 596		.lp_clk_min = 7000000,
 597		.lp_clk_max = 10000000,
 598	};
 599
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 600	if (ddata->pin_config.num_pins > 0) {
 601		r = in->ops.dsi->configure_pins(in, &ddata->pin_config);
 602		if (r) {
 603			dev_err(&ddata->pdev->dev,
 604				"failed to configure DSI pins\n");
 605			goto err0;
 606		}
 607	}
 608
 609	r = in->ops.dsi->set_config(in, &dsi_config);
 610	if (r) {
 611		dev_err(&ddata->pdev->dev, "failed to configure DSI\n");
 612		goto err0;
 613	}
 614
 615	r = in->ops.dsi->enable(in);
 616	if (r) {
 617		dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
 618		goto err0;
 619	}
 620
 621	dsicm_hw_reset(ddata);
 622
 623	in->ops.dsi->enable_hs(in, ddata->channel, false);
 624
 625	r = dsicm_sleep_out(ddata);
 626	if (r)
 627		goto err;
 628
 629	r = dsicm_get_id(ddata, &id1, &id2, &id3);
 630	if (r)
 631		goto err;
 632
 633	r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff);
 634	if (r)
 635		goto err;
 636
 637	r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY,
 638			(1<<2) | (1<<5));	/* BL | BCTRL */
 639	if (r)
 640		goto err;
 641
 642	r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT,
 643		MIPI_DCS_PIXEL_FMT_24BIT);
 644	if (r)
 645		goto err;
 646
 647	r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON);
 648	if (r)
 649		goto err;
 650
 651	r = _dsicm_enable_te(ddata, ddata->te_enabled);
 652	if (r)
 653		goto err;
 654
 655	r = in->ops.dsi->enable_video_output(in, ddata->channel);
 656	if (r)
 657		goto err;
 658
 659	ddata->enabled = 1;
 660
 661	if (!ddata->intro_printed) {
 662		dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n",
 663			id1, id2, id3);
 664		ddata->intro_printed = true;
 665	}
 666
 667	in->ops.dsi->enable_hs(in, ddata->channel, true);
 668
 669	return 0;
 670err:
 671	dev_err(&ddata->pdev->dev, "error while enabling panel, issuing HW reset\n");
 672
 673	dsicm_hw_reset(ddata);
 674
 675	in->ops.dsi->disable(in, true, false);
 676err0:
 
 
 
 
 
 
 677	return r;
 678}
 679
 680static void dsicm_power_off(struct panel_drv_data *ddata)
 681{
 682	struct omap_dss_device *in = ddata->in;
 683	int r;
 684
 685	in->ops.dsi->disable_video_output(in, ddata->channel);
 686
 687	r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF);
 688	if (!r)
 689		r = dsicm_sleep_in(ddata);
 690
 691	if (r) {
 692		dev_err(&ddata->pdev->dev,
 693				"error disabling panel, issuing HW reset\n");
 694		dsicm_hw_reset(ddata);
 695	}
 696
 697	in->ops.dsi->disable(in, true, false);
 698
 699	ddata->enabled = 0;
 
 
 
 
 
 700}
 701
 702static int dsicm_panel_reset(struct panel_drv_data *ddata)
 703{
 704	dev_err(&ddata->pdev->dev, "performing LCD reset\n");
 705
 706	dsicm_power_off(ddata);
 707	dsicm_hw_reset(ddata);
 708	return dsicm_power_on(ddata);
 709}
 710
 711static int dsicm_connect(struct omap_dss_device *dssdev)
 
 712{
 713	struct panel_drv_data *ddata = to_panel_data(dssdev);
 714	struct omap_dss_device *in = ddata->in;
 715	struct device *dev = &ddata->pdev->dev;
 716	int r;
 717
 718	if (omapdss_device_is_connected(dssdev))
 719		return 0;
 720
 721	r = in->ops.dsi->connect(in, dssdev);
 722	if (r) {
 723		dev_err(dev, "Failed to connect to video source\n");
 724		return r;
 725	}
 726
 727	r = in->ops.dsi->request_vc(ddata->in, &ddata->channel);
 728	if (r) {
 729		dev_err(dev, "failed to get virtual channel\n");
 730		goto err_req_vc;
 731	}
 732
 733	r = in->ops.dsi->set_vc_id(ddata->in, ddata->channel, TCH);
 734	if (r) {
 735		dev_err(dev, "failed to set VC_ID\n");
 736		goto err_vc_id;
 
 737	}
 738
 
 739	return 0;
 740
 741err_vc_id:
 742	in->ops.dsi->release_vc(ddata->in, ddata->channel);
 743err_req_vc:
 744	in->ops.dsi->disconnect(in, dssdev);
 745	return r;
 746}
 747
 748static void dsicm_disconnect(struct omap_dss_device *dssdev)
 
 749{
 750	struct panel_drv_data *ddata = to_panel_data(dssdev);
 751	struct omap_dss_device *in = ddata->in;
 752
 753	if (!omapdss_device_is_connected(dssdev))
 754		return;
 755
 756	in->ops.dsi->release_vc(in, ddata->channel);
 757	in->ops.dsi->disconnect(in, dssdev);
 758}
 759
 760static int dsicm_enable(struct omap_dss_device *dssdev)
 761{
 762	struct panel_drv_data *ddata = to_panel_data(dssdev);
 763	struct omap_dss_device *in = ddata->in;
 764	int r;
 765
 766	dev_dbg(&ddata->pdev->dev, "enable\n");
 767
 768	mutex_lock(&ddata->lock);
 769
 770	if (!omapdss_device_is_connected(dssdev)) {
 771		r = -ENODEV;
 772		goto err;
 773	}
 774
 775	if (omapdss_device_is_enabled(dssdev)) {
 776		r = 0;
 777		goto err;
 778	}
 779
 780	in->ops.dsi->bus_lock(in);
 781
 782	r = dsicm_power_on(ddata);
 783
 784	in->ops.dsi->bus_unlock(in);
 785
 786	if (r)
 787		goto err;
 788
 789	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
 790
 791	mutex_unlock(&ddata->lock);
 792
 793	return 0;
 
 
 794err:
 795	dev_dbg(&ddata->pdev->dev, "enable failed\n");
 796	mutex_unlock(&ddata->lock);
 797	return r;
 798}
 799
 800static void dsicm_disable(struct omap_dss_device *dssdev)
 801{
 802	struct panel_drv_data *ddata = to_panel_data(dssdev);
 803	struct omap_dss_device *in = ddata->in;
 804	int r;
 805
 806	dev_dbg(&ddata->pdev->dev, "disable\n");
 807
 808	mutex_lock(&ddata->lock);
 809
 810	dsicm_cancel_ulps_work(ddata);
 811
 812	in->ops.dsi->bus_lock(in);
 813
 814	if (omapdss_device_is_enabled(dssdev)) {
 815		r = dsicm_wake_up(ddata);
 816		if (!r)
 817			dsicm_power_off(ddata);
 818	}
 819
 820	in->ops.dsi->bus_unlock(in);
 
 
 821
 822	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
 823
 824	mutex_unlock(&ddata->lock);
 825}
 826
 827static void dsicm_framedone_cb(int err, void *data)
 828{
 829	struct panel_drv_data *ddata = data;
 830	struct omap_dss_device *in = ddata->in;
 831
 832	dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err);
 833	in->ops.dsi->bus_unlock(ddata->in);
 834}
 835
 836static irqreturn_t dsicm_te_isr(int irq, void *data)
 837{
 838	struct panel_drv_data *ddata = data;
 839	struct omap_dss_device *in = ddata->in;
 840	int old;
 841	int r;
 842
 843	old = atomic_cmpxchg(&ddata->do_update, 1, 0);
 844
 845	if (old) {
 846		cancel_delayed_work(&ddata->te_timeout_work);
 847
 848		r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
 849				ddata);
 850		if (r)
 851			goto err;
 852	}
 853
 854	return IRQ_HANDLED;
 855err:
 856	dev_err(&ddata->pdev->dev, "start update failed\n");
 857	in->ops.dsi->bus_unlock(in);
 858	return IRQ_HANDLED;
 859}
 860
 861static void dsicm_te_timeout_work_callback(struct work_struct *work)
 862{
 863	struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
 864					te_timeout_work.work);
 865	struct omap_dss_device *in = ddata->in;
 866
 867	dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n");
 868
 869	atomic_set(&ddata->do_update, 0);
 870	in->ops.dsi->bus_unlock(in);
 871}
 872
 873static int dsicm_update(struct omap_dss_device *dssdev,
 874				    u16 x, u16 y, u16 w, u16 h)
 875{
 876	struct panel_drv_data *ddata = to_panel_data(dssdev);
 877	struct omap_dss_device *in = ddata->in;
 878	int r;
 879
 880	dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
 881
 882	mutex_lock(&ddata->lock);
 883	in->ops.dsi->bus_lock(in);
 884
 885	r = dsicm_wake_up(ddata);
 886	if (r)
 887		goto err;
 888
 889	if (!ddata->enabled) {
 890		r = 0;
 891		goto err;
 892	}
 893
 894	/* XXX no need to send this every frame, but dsi break if not done */
 895	r = dsicm_set_update_window(ddata, 0, 0,
 896			dssdev->panel.timings.x_res,
 897			dssdev->panel.timings.y_res);
 898	if (r)
 899		goto err;
 900
 901	if (ddata->te_enabled && gpio_is_valid(ddata->ext_te_gpio)) {
 902		schedule_delayed_work(&ddata->te_timeout_work,
 903				msecs_to_jiffies(250));
 904		atomic_set(&ddata->do_update, 1);
 905	} else {
 906		r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
 907				ddata);
 908		if (r)
 909			goto err;
 910	}
 911
 912	/* note: no bus_unlock here. unlock is in framedone_cb */
 913	mutex_unlock(&ddata->lock);
 914	return 0;
 915err:
 916	in->ops.dsi->bus_unlock(in);
 917	mutex_unlock(&ddata->lock);
 918	return r;
 919}
 920
 921static int dsicm_sync(struct omap_dss_device *dssdev)
 922{
 923	struct panel_drv_data *ddata = to_panel_data(dssdev);
 924	struct omap_dss_device *in = ddata->in;
 925
 926	dev_dbg(&ddata->pdev->dev, "sync\n");
 927
 928	mutex_lock(&ddata->lock);
 929	in->ops.dsi->bus_lock(in);
 930	in->ops.dsi->bus_unlock(in);
 931	mutex_unlock(&ddata->lock);
 932
 933	dev_dbg(&ddata->pdev->dev, "sync done\n");
 934
 935	return 0;
 936}
 937
 938static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable)
 939{
 940	struct omap_dss_device *in = ddata->in;
 941	int r;
 942
 943	if (enable)
 944		r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0);
 945	else
 946		r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF);
 947
 948	if (!gpio_is_valid(ddata->ext_te_gpio))
 949		in->ops.dsi->enable_te(in, enable);
 950
 951	/* possible panel bug */
 952	msleep(100);
 953
 954	return r;
 955}
 956
 957static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable)
 958{
 959	struct panel_drv_data *ddata = to_panel_data(dssdev);
 960	struct omap_dss_device *in = ddata->in;
 961	int r;
 962
 963	mutex_lock(&ddata->lock);
 964
 965	if (ddata->te_enabled == enable)
 966		goto end;
 967
 968	in->ops.dsi->bus_lock(in);
 969
 970	if (ddata->enabled) {
 971		r = dsicm_wake_up(ddata);
 972		if (r)
 973			goto err;
 974
 975		r = _dsicm_enable_te(ddata, enable);
 976		if (r)
 977			goto err;
 978	}
 979
 980	ddata->te_enabled = enable;
 981
 982	in->ops.dsi->bus_unlock(in);
 983end:
 984	mutex_unlock(&ddata->lock);
 985
 986	return 0;
 987err:
 988	in->ops.dsi->bus_unlock(in);
 989	mutex_unlock(&ddata->lock);
 990
 991	return r;
 992}
 993
 994static int dsicm_get_te(struct omap_dss_device *dssdev)
 995{
 996	struct panel_drv_data *ddata = to_panel_data(dssdev);
 997	int r;
 998
 999	mutex_lock(&ddata->lock);
1000	r = ddata->te_enabled;
1001	mutex_unlock(&ddata->lock);
1002
1003	return r;
1004}
1005
1006static int dsicm_memory_read(struct omap_dss_device *dssdev,
1007		void *buf, size_t size,
1008		u16 x, u16 y, u16 w, u16 h)
1009{
1010	struct panel_drv_data *ddata = to_panel_data(dssdev);
1011	struct omap_dss_device *in = ddata->in;
1012	int r;
1013	int first = 1;
1014	int plen;
1015	unsigned buf_used = 0;
1016
1017	if (size < w * h * 3)
1018		return -ENOMEM;
1019
1020	mutex_lock(&ddata->lock);
1021
1022	if (!ddata->enabled) {
1023		r = -ENODEV;
1024		goto err1;
1025	}
1026
1027	size = min(w * h * 3,
1028			dssdev->panel.timings.x_res *
1029			dssdev->panel.timings.y_res * 3);
1030
1031	in->ops.dsi->bus_lock(in);
1032
1033	r = dsicm_wake_up(ddata);
1034	if (r)
1035		goto err2;
1036
1037	/* plen 1 or 2 goes into short packet. until checksum error is fixed,
1038	 * use short packets. plen 32 works, but bigger packets seem to cause
1039	 * an error. */
1040	if (size % 2)
1041		plen = 1;
1042	else
1043		plen = 2;
1044
1045	dsicm_set_update_window(ddata, x, y, w, h);
1046
1047	r = in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, plen);
1048	if (r)
1049		goto err2;
1050
1051	while (buf_used < size) {
1052		u8 dcs_cmd = first ? 0x2e : 0x3e;
1053		first = 0;
1054
1055		r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd,
1056				buf + buf_used, size - buf_used);
1057
1058		if (r < 0) {
1059			dev_err(dssdev->dev, "read error\n");
1060			goto err3;
1061		}
1062
1063		buf_used += r;
1064
1065		if (r < plen) {
1066			dev_err(&ddata->pdev->dev, "short read\n");
1067			break;
1068		}
1069
1070		if (signal_pending(current)) {
1071			dev_err(&ddata->pdev->dev, "signal pending, "
1072					"aborting memory read\n");
1073			r = -ERESTARTSYS;
1074			goto err3;
1075		}
1076	}
1077
1078	r = buf_used;
1079
1080err3:
1081	in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, 1);
1082err2:
1083	in->ops.dsi->bus_unlock(in);
1084err1:
1085	mutex_unlock(&ddata->lock);
1086	return r;
1087}
1088
1089static void dsicm_ulps_work(struct work_struct *work)
1090{
1091	struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
1092			ulps_work.work);
1093	struct omap_dss_device *dssdev = &ddata->dssdev;
1094	struct omap_dss_device *in = ddata->in;
1095
1096	mutex_lock(&ddata->lock);
1097
1098	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) {
1099		mutex_unlock(&ddata->lock);
1100		return;
1101	}
1102
1103	in->ops.dsi->bus_lock(in);
1104
1105	dsicm_enter_ulps(ddata);
1106
1107	in->ops.dsi->bus_unlock(in);
1108	mutex_unlock(&ddata->lock);
1109}
1110
1111static struct omap_dss_driver dsicm_ops = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1112	.connect	= dsicm_connect,
1113	.disconnect	= dsicm_disconnect,
1114
1115	.enable		= dsicm_enable,
1116	.disable	= dsicm_disable,
1117
 
 
 
 
 
1118	.update		= dsicm_update,
1119	.sync		= dsicm_sync,
1120
1121	.get_resolution	= dsicm_get_resolution,
1122	.get_recommended_bpp = omapdss_default_get_recommended_bpp,
1123
1124	.enable_te	= dsicm_enable_te,
1125	.get_te		= dsicm_get_te,
1126
1127	.memory_read	= dsicm_memory_read,
1128};
1129
1130static int dsicm_probe_of(struct platform_device *pdev)
1131{
1132	struct device_node *node = pdev->dev.of_node;
 
1133	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1134	struct omap_dss_device *in;
1135	int gpio;
1136
1137	gpio = of_get_named_gpio(node, "reset-gpios", 0);
1138	if (!gpio_is_valid(gpio)) {
1139		dev_err(&pdev->dev, "failed to parse reset gpio\n");
1140		return gpio;
1141	}
1142	ddata->reset_gpio = gpio;
1143
1144	gpio = of_get_named_gpio(node, "te-gpios", 0);
1145	if (gpio_is_valid(gpio) || gpio == -ENOENT) {
1146		ddata->ext_te_gpio = gpio;
 
 
 
 
 
 
 
 
 
 
 
1147	} else {
1148		dev_err(&pdev->dev, "failed to parse TE gpio\n");
1149		return gpio;
1150	}
1151
1152	in = omapdss_of_find_source_for_first_ep(node);
1153	if (IS_ERR(in)) {
1154		dev_err(&pdev->dev, "failed to find video source\n");
1155		return PTR_ERR(in);
 
 
 
 
 
 
 
 
1156	}
1157
1158	ddata->in = in;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1159
1160	/* TODO: ulps, backlight */
1161
1162	return 0;
1163}
1164
1165static int dsicm_probe(struct platform_device *pdev)
1166{
1167	struct backlight_properties props;
1168	struct panel_drv_data *ddata;
1169	struct backlight_device *bldev = NULL;
1170	struct device *dev = &pdev->dev;
1171	struct omap_dss_device *dssdev;
1172	int r;
1173
1174	dev_dbg(dev, "probe\n");
1175
1176	ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
1177	if (!ddata)
1178		return -ENOMEM;
1179
1180	platform_set_drvdata(pdev, ddata);
1181	ddata->pdev = pdev;
1182
1183	if (!pdev->dev.of_node)
1184		return -ENODEV;
 
1185
1186	r = dsicm_probe_of(pdev);
1187	if (r)
1188		return r;
1189
1190	ddata->timings.x_res = 864;
1191	ddata->timings.y_res = 480;
1192	ddata->timings.pixelclock = 864 * 480 * 60;
1193
1194	dssdev = &ddata->dssdev;
1195	dssdev->dev = dev;
1196	dssdev->driver = &dsicm_ops;
1197	dssdev->panel.timings = ddata->timings;
1198	dssdev->type = OMAP_DISPLAY_TYPE_DSI;
 
1199	dssdev->owner = THIS_MODULE;
 
 
1200
1201	dssdev->panel.dsi_pix_fmt = OMAP_DSS_DSI_FMT_RGB888;
1202	dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
1203		OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
1204
1205	r = omapdss_register_display(dssdev);
1206	if (r) {
1207		dev_err(dev, "Failed to register panel\n");
1208		goto err_reg;
1209	}
1210
1211	mutex_init(&ddata->lock);
1212
1213	atomic_set(&ddata->do_update, 0);
1214
1215	if (gpio_is_valid(ddata->reset_gpio)) {
1216		r = devm_gpio_request_one(dev, ddata->reset_gpio,
1217				GPIOF_OUT_INIT_LOW, "taal rst");
1218		if (r) {
1219			dev_err(dev, "failed to request reset gpio\n");
1220			return r;
1221		}
1222	}
1223
1224	if (gpio_is_valid(ddata->ext_te_gpio)) {
1225		r = devm_gpio_request_one(dev, ddata->ext_te_gpio,
1226				GPIOF_IN, "taal irq");
1227		if (r) {
1228			dev_err(dev, "GPIO request failed\n");
1229			return r;
1230		}
1231
1232		r = devm_request_irq(dev, gpio_to_irq(ddata->ext_te_gpio),
1233				dsicm_te_isr,
1234				IRQF_TRIGGER_RISING,
1235				"taal vsync", ddata);
1236
1237		if (r) {
1238			dev_err(dev, "IRQ request failed\n");
1239			return r;
1240		}
1241
1242		INIT_DEFERRABLE_WORK(&ddata->te_timeout_work,
1243					dsicm_te_timeout_work_callback);
1244
1245		dev_dbg(dev, "Using GPIO TE\n");
1246	}
1247
1248	ddata->workqueue = create_singlethread_workqueue("dsicm_wq");
1249	if (ddata->workqueue == NULL) {
1250		dev_err(dev, "can't create workqueue\n");
1251		return -ENOMEM;
1252	}
1253	INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work);
1254
1255	dsicm_hw_reset(ddata);
1256
1257	if (ddata->use_dsi_backlight) {
1258		memset(&props, 0, sizeof(struct backlight_properties));
1259		props.max_brightness = 255;
1260
1261		props.type = BACKLIGHT_RAW;
1262		bldev = backlight_device_register(dev_name(dev),
1263				dev, ddata, &dsicm_bl_ops, &props);
 
1264		if (IS_ERR(bldev)) {
1265			r = PTR_ERR(bldev);
1266			goto err_bl;
1267		}
1268
1269		ddata->bldev = bldev;
1270
1271		bldev->props.fb_blank = FB_BLANK_UNBLANK;
1272		bldev->props.power = FB_BLANK_UNBLANK;
1273		bldev->props.brightness = 255;
1274
1275		dsicm_bl_update_status(bldev);
1276	}
1277
1278	r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
1279	if (r) {
1280		dev_err(dev, "failed to create sysfs files\n");
1281		goto err_sysfs_create;
1282	}
1283
1284	return 0;
1285
1286err_sysfs_create:
1287	if (bldev != NULL)
1288		backlight_device_unregister(bldev);
1289err_bl:
1290	destroy_workqueue(ddata->workqueue);
1291err_reg:
 
 
 
1292	return r;
1293}
1294
1295static int __exit dsicm_remove(struct platform_device *pdev)
1296{
1297	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1298	struct omap_dss_device *dssdev = &ddata->dssdev;
1299	struct backlight_device *bldev;
1300
1301	dev_dbg(&pdev->dev, "remove\n");
1302
1303	omapdss_unregister_display(dssdev);
1304
1305	dsicm_disable(dssdev);
1306	dsicm_disconnect(dssdev);
 
1307
1308	sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group);
1309
1310	bldev = ddata->bldev;
1311	if (bldev != NULL) {
1312		bldev->props.power = FB_BLANK_POWERDOWN;
1313		dsicm_bl_update_status(bldev);
1314		backlight_device_unregister(bldev);
1315	}
1316
1317	omap_dss_put_device(ddata->in);
1318
1319	dsicm_cancel_ulps_work(ddata);
1320	destroy_workqueue(ddata->workqueue);
1321
1322	/* reset, to be sure that the panel is in a valid state */
1323	dsicm_hw_reset(ddata);
1324
1325	return 0;
1326}
1327
1328static const struct of_device_id dsicm_of_match[] = {
1329	{ .compatible = "omapdss,panel-dsi-cm", },
1330	{},
1331};
1332
1333MODULE_DEVICE_TABLE(of, dsicm_of_match);
1334
1335static struct platform_driver dsicm_driver = {
1336	.probe = dsicm_probe,
1337	.remove = __exit_p(dsicm_remove),
1338	.driver = {
1339		.name = "panel-dsi-cm",
1340		.of_match_table = dsicm_of_match,
1341		.suppress_bind_attrs = true,
1342	},
1343};
1344
1345module_platform_driver(dsicm_driver);
1346
1347MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
1348MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
1349MODULE_LICENSE("GPL");
v5.9
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Generic DSI Command Mode panel driver
   4 *
   5 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
   6 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
 
 
 
 
   7 */
   8
   9/* #define DEBUG */
  10
  11#include <linux/backlight.h>
  12#include <linux/delay.h>
  13#include <linux/gpio/consumer.h>
 
  14#include <linux/interrupt.h>
  15#include <linux/jiffies.h>
  16#include <linux/module.h>
  17#include <linux/platform_device.h>
  18#include <linux/sched/signal.h>
  19#include <linux/slab.h>
  20#include <linux/workqueue.h>
  21#include <linux/of_device.h>
  22#include <linux/regulator/consumer.h>
  23
  24#include <drm/drm_connector.h>
  25
 
 
  26#include <video/mipi_display.h>
  27#include <video/of_display_timing.h>
  28
  29#include "../dss/omapdss.h"
  30
  31/* DSI Virtual channel. Hardcoded for now. */
  32#define TCH 0
  33
  34#define DCS_READ_NUM_ERRORS	0x05
  35#define DCS_BRIGHTNESS		0x51
  36#define DCS_CTRL_DISPLAY	0x53
  37#define DCS_GET_ID1		0xda
  38#define DCS_GET_ID2		0xdb
  39#define DCS_GET_ID3		0xdc
  40
  41struct panel_drv_data {
  42	struct omap_dss_device dssdev;
  43	struct omap_dss_device *src;
  44
  45	struct videomode vm;
  46
  47	struct platform_device *pdev;
  48
  49	struct mutex lock;
  50
  51	struct backlight_device *bldev;
  52	struct backlight_device *extbldev;
  53
  54	unsigned long	hw_guard_end;	/* next value of jiffies when we can
  55					 * issue the next sleep in/out command
  56					 */
  57	unsigned long	hw_guard_wait;	/* max guard time in jiffies */
  58
  59	/* panel HW configuration from DT or platform data */
  60	struct gpio_desc *reset_gpio;
  61	struct gpio_desc *ext_te_gpio;
  62
  63	struct regulator *vpnl;
  64	struct regulator *vddi;
  65
  66	bool use_dsi_backlight;
  67
  68	int width_mm;
  69	int height_mm;
  70
  71	struct omap_dsi_pin_config pin_config;
  72
  73	/* runtime variables */
  74	bool enabled;
  75
  76	bool te_enabled;
  77
  78	atomic_t do_update;
  79	int channel;
  80
  81	struct delayed_work te_timeout_work;
  82
  83	bool intro_printed;
  84
  85	struct workqueue_struct *workqueue;
  86
  87	bool ulps_enabled;
  88	unsigned int ulps_timeout;
  89	struct delayed_work ulps_work;
  90};
  91
  92#define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
  93
  94static irqreturn_t dsicm_te_isr(int irq, void *data);
  95static void dsicm_te_timeout_work_callback(struct work_struct *work);
  96static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable);
  97
  98static int dsicm_panel_reset(struct panel_drv_data *ddata);
  99
 100static void dsicm_ulps_work(struct work_struct *work);
 101
 102static void dsicm_bl_power(struct panel_drv_data *ddata, bool enable)
 103{
 104	struct backlight_device *backlight;
 105
 106	if (ddata->bldev)
 107		backlight = ddata->bldev;
 108	else if (ddata->extbldev)
 109		backlight = ddata->extbldev;
 110	else
 111		return;
 112
 113	if (enable) {
 114		backlight->props.fb_blank = FB_BLANK_UNBLANK;
 115		backlight->props.state = ~(BL_CORE_FBBLANK | BL_CORE_SUSPENDED);
 116		backlight->props.power = FB_BLANK_UNBLANK;
 117	} else {
 118		backlight->props.fb_blank = FB_BLANK_NORMAL;
 119		backlight->props.power = FB_BLANK_POWERDOWN;
 120		backlight->props.state |= BL_CORE_FBBLANK | BL_CORE_SUSPENDED;
 121	}
 122
 123	backlight_update_status(backlight);
 124}
 125
 126static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec)
 127{
 128	ddata->hw_guard_wait = msecs_to_jiffies(guard_msec);
 129	ddata->hw_guard_end = jiffies + ddata->hw_guard_wait;
 130}
 131
 132static void hw_guard_wait(struct panel_drv_data *ddata)
 133{
 134	unsigned long wait = ddata->hw_guard_end - jiffies;
 135
 136	if ((long)wait > 0 && wait <= ddata->hw_guard_wait) {
 137		set_current_state(TASK_UNINTERRUPTIBLE);
 138		schedule_timeout(wait);
 139	}
 140}
 141
 142static int dsicm_dcs_read_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 *data)
 143{
 144	struct omap_dss_device *src = ddata->src;
 145	int r;
 146	u8 buf[1];
 147
 148	r = src->ops->dsi.dcs_read(src, ddata->channel, dcs_cmd, buf, 1);
 149
 150	if (r < 0)
 151		return r;
 152
 153	*data = buf[0];
 154
 155	return 0;
 156}
 157
 158static int dsicm_dcs_write_0(struct panel_drv_data *ddata, u8 dcs_cmd)
 159{
 160	struct omap_dss_device *src = ddata->src;
 161
 162	return src->ops->dsi.dcs_write(src, ddata->channel, &dcs_cmd, 1);
 163}
 164
 165static int dsicm_dcs_write_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 param)
 166{
 167	struct omap_dss_device *src = ddata->src;
 168	u8 buf[2] = { dcs_cmd, param };
 169
 170	return src->ops->dsi.dcs_write(src, ddata->channel, buf, 2);
 171}
 172
 173static int dsicm_sleep_in(struct panel_drv_data *ddata)
 174
 175{
 176	struct omap_dss_device *src = ddata->src;
 177	u8 cmd;
 178	int r;
 179
 180	hw_guard_wait(ddata);
 181
 182	cmd = MIPI_DCS_ENTER_SLEEP_MODE;
 183	r = src->ops->dsi.dcs_write_nosync(src, ddata->channel, &cmd, 1);
 184	if (r)
 185		return r;
 186
 187	hw_guard_start(ddata, 120);
 188
 189	usleep_range(5000, 10000);
 190
 191	return 0;
 192}
 193
 194static int dsicm_sleep_out(struct panel_drv_data *ddata)
 195{
 196	int r;
 197
 198	hw_guard_wait(ddata);
 199
 200	r = dsicm_dcs_write_0(ddata, MIPI_DCS_EXIT_SLEEP_MODE);
 201	if (r)
 202		return r;
 203
 204	hw_guard_start(ddata, 120);
 205
 206	usleep_range(5000, 10000);
 207
 208	return 0;
 209}
 210
 211static int dsicm_get_id(struct panel_drv_data *ddata, u8 *id1, u8 *id2, u8 *id3)
 212{
 213	int r;
 214
 215	r = dsicm_dcs_read_1(ddata, DCS_GET_ID1, id1);
 216	if (r)
 217		return r;
 218	r = dsicm_dcs_read_1(ddata, DCS_GET_ID2, id2);
 219	if (r)
 220		return r;
 221	r = dsicm_dcs_read_1(ddata, DCS_GET_ID3, id3);
 222	if (r)
 223		return r;
 224
 225	return 0;
 226}
 227
 228static int dsicm_set_update_window(struct panel_drv_data *ddata,
 229		u16 x, u16 y, u16 w, u16 h)
 230{
 231	struct omap_dss_device *src = ddata->src;
 232	int r;
 233	u16 x1 = x;
 234	u16 x2 = x + w - 1;
 235	u16 y1 = y;
 236	u16 y2 = y + h - 1;
 237
 238	u8 buf[5];
 239	buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS;
 240	buf[1] = (x1 >> 8) & 0xff;
 241	buf[2] = (x1 >> 0) & 0xff;
 242	buf[3] = (x2 >> 8) & 0xff;
 243	buf[4] = (x2 >> 0) & 0xff;
 244
 245	r = src->ops->dsi.dcs_write_nosync(src, ddata->channel, buf, sizeof(buf));
 246	if (r)
 247		return r;
 248
 249	buf[0] = MIPI_DCS_SET_PAGE_ADDRESS;
 250	buf[1] = (y1 >> 8) & 0xff;
 251	buf[2] = (y1 >> 0) & 0xff;
 252	buf[3] = (y2 >> 8) & 0xff;
 253	buf[4] = (y2 >> 0) & 0xff;
 254
 255	r = src->ops->dsi.dcs_write_nosync(src, ddata->channel, buf, sizeof(buf));
 256	if (r)
 257		return r;
 258
 259	src->ops->dsi.bta_sync(src, ddata->channel);
 260
 261	return r;
 262}
 263
 264static void dsicm_queue_ulps_work(struct panel_drv_data *ddata)
 265{
 266	if (ddata->ulps_timeout > 0)
 267		queue_delayed_work(ddata->workqueue, &ddata->ulps_work,
 268				msecs_to_jiffies(ddata->ulps_timeout));
 269}
 270
 271static void dsicm_cancel_ulps_work(struct panel_drv_data *ddata)
 272{
 273	cancel_delayed_work(&ddata->ulps_work);
 274}
 275
 276static int dsicm_enter_ulps(struct panel_drv_data *ddata)
 277{
 278	struct omap_dss_device *src = ddata->src;
 279	int r;
 280
 281	if (ddata->ulps_enabled)
 282		return 0;
 283
 284	dsicm_cancel_ulps_work(ddata);
 285
 286	r = _dsicm_enable_te(ddata, false);
 287	if (r)
 288		goto err;
 289
 290	if (ddata->ext_te_gpio)
 291		disable_irq(gpiod_to_irq(ddata->ext_te_gpio));
 292
 293	src->ops->dsi.disable(src, false, true);
 294
 295	ddata->ulps_enabled = true;
 296
 297	return 0;
 298
 299err:
 300	dev_err(&ddata->pdev->dev, "enter ULPS failed");
 301	dsicm_panel_reset(ddata);
 302
 303	ddata->ulps_enabled = false;
 304
 305	dsicm_queue_ulps_work(ddata);
 306
 307	return r;
 308}
 309
 310static int dsicm_exit_ulps(struct panel_drv_data *ddata)
 311{
 312	struct omap_dss_device *src = ddata->src;
 313	int r;
 314
 315	if (!ddata->ulps_enabled)
 316		return 0;
 317
 318	src->ops->enable(src);
 319	src->ops->dsi.enable_hs(src, ddata->channel, true);
 
 
 
 
 
 320
 321	r = _dsicm_enable_te(ddata, true);
 322	if (r) {
 323		dev_err(&ddata->pdev->dev, "failed to re-enable TE");
 324		goto err2;
 325	}
 326
 327	if (ddata->ext_te_gpio)
 328		enable_irq(gpiod_to_irq(ddata->ext_te_gpio));
 329
 330	dsicm_queue_ulps_work(ddata);
 331
 332	ddata->ulps_enabled = false;
 333
 334	return 0;
 335
 336err2:
 337	dev_err(&ddata->pdev->dev, "failed to exit ULPS");
 338
 339	r = dsicm_panel_reset(ddata);
 340	if (!r) {
 341		if (ddata->ext_te_gpio)
 342			enable_irq(gpiod_to_irq(ddata->ext_te_gpio));
 343		ddata->ulps_enabled = false;
 344	}
 345
 346	dsicm_queue_ulps_work(ddata);
 347
 348	return r;
 349}
 350
 351static int dsicm_wake_up(struct panel_drv_data *ddata)
 352{
 353	if (ddata->ulps_enabled)
 354		return dsicm_exit_ulps(ddata);
 355
 356	dsicm_cancel_ulps_work(ddata);
 357	dsicm_queue_ulps_work(ddata);
 358	return 0;
 359}
 360
 361static int dsicm_bl_update_status(struct backlight_device *dev)
 362{
 363	struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
 364	struct omap_dss_device *src = ddata->src;
 365	int r = 0;
 366	int level;
 367
 368	if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
 369			dev->props.power == FB_BLANK_UNBLANK)
 370		level = dev->props.brightness;
 371	else
 372		level = 0;
 373
 374	dev_dbg(&ddata->pdev->dev, "update brightness to %d\n", level);
 375
 376	mutex_lock(&ddata->lock);
 377
 378	if (ddata->enabled) {
 379		src->ops->dsi.bus_lock(src);
 380
 381		r = dsicm_wake_up(ddata);
 382		if (!r)
 383			r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, level);
 384
 385		src->ops->dsi.bus_unlock(src);
 
 
 386	}
 387
 388	mutex_unlock(&ddata->lock);
 389
 390	return r;
 391}
 392
 393static int dsicm_bl_get_intensity(struct backlight_device *dev)
 394{
 395	if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
 396			dev->props.power == FB_BLANK_UNBLANK)
 397		return dev->props.brightness;
 398
 399	return 0;
 400}
 401
 402static const struct backlight_ops dsicm_bl_ops = {
 403	.get_brightness = dsicm_bl_get_intensity,
 404	.update_status  = dsicm_bl_update_status,
 405};
 406
 
 
 
 
 
 
 
 407static ssize_t dsicm_num_errors_show(struct device *dev,
 408		struct device_attribute *attr, char *buf)
 409{
 410	struct panel_drv_data *ddata = dev_get_drvdata(dev);
 411	struct omap_dss_device *src = ddata->src;
 
 412	u8 errors = 0;
 413	int r;
 414
 415	mutex_lock(&ddata->lock);
 416
 417	if (ddata->enabled) {
 418		src->ops->dsi.bus_lock(src);
 419
 420		r = dsicm_wake_up(ddata);
 421		if (!r)
 422			r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS,
 423					&errors);
 424
 425		src->ops->dsi.bus_unlock(src);
 426	} else {
 427		r = -ENODEV;
 428	}
 429
 430	mutex_unlock(&ddata->lock);
 431
 432	if (r)
 433		return r;
 434
 435	return snprintf(buf, PAGE_SIZE, "%d\n", errors);
 436}
 437
 438static ssize_t dsicm_hw_revision_show(struct device *dev,
 439		struct device_attribute *attr, char *buf)
 440{
 441	struct panel_drv_data *ddata = dev_get_drvdata(dev);
 442	struct omap_dss_device *src = ddata->src;
 
 443	u8 id1, id2, id3;
 444	int r;
 445
 446	mutex_lock(&ddata->lock);
 447
 448	if (ddata->enabled) {
 449		src->ops->dsi.bus_lock(src);
 450
 451		r = dsicm_wake_up(ddata);
 452		if (!r)
 453			r = dsicm_get_id(ddata, &id1, &id2, &id3);
 454
 455		src->ops->dsi.bus_unlock(src);
 456	} else {
 457		r = -ENODEV;
 458	}
 459
 460	mutex_unlock(&ddata->lock);
 461
 462	if (r)
 463		return r;
 464
 465	return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
 466}
 467
 468static ssize_t dsicm_store_ulps(struct device *dev,
 469		struct device_attribute *attr,
 470		const char *buf, size_t count)
 471{
 472	struct panel_drv_data *ddata = dev_get_drvdata(dev);
 473	struct omap_dss_device *src = ddata->src;
 
 474	unsigned long t;
 475	int r;
 476
 477	r = kstrtoul(buf, 0, &t);
 478	if (r)
 479		return r;
 480
 481	mutex_lock(&ddata->lock);
 482
 483	if (ddata->enabled) {
 484		src->ops->dsi.bus_lock(src);
 485
 486		if (t)
 487			r = dsicm_enter_ulps(ddata);
 488		else
 489			r = dsicm_wake_up(ddata);
 490
 491		src->ops->dsi.bus_unlock(src);
 492	}
 493
 494	mutex_unlock(&ddata->lock);
 495
 496	if (r)
 497		return r;
 498
 499	return count;
 500}
 501
 502static ssize_t dsicm_show_ulps(struct device *dev,
 503		struct device_attribute *attr,
 504		char *buf)
 505{
 506	struct panel_drv_data *ddata = dev_get_drvdata(dev);
 507	unsigned int t;
 
 508
 509	mutex_lock(&ddata->lock);
 510	t = ddata->ulps_enabled;
 511	mutex_unlock(&ddata->lock);
 512
 513	return snprintf(buf, PAGE_SIZE, "%u\n", t);
 514}
 515
 516static ssize_t dsicm_store_ulps_timeout(struct device *dev,
 517		struct device_attribute *attr,
 518		const char *buf, size_t count)
 519{
 520	struct panel_drv_data *ddata = dev_get_drvdata(dev);
 521	struct omap_dss_device *src = ddata->src;
 
 522	unsigned long t;
 523	int r;
 524
 525	r = kstrtoul(buf, 0, &t);
 526	if (r)
 527		return r;
 528
 529	mutex_lock(&ddata->lock);
 530	ddata->ulps_timeout = t;
 531
 532	if (ddata->enabled) {
 533		/* dsicm_wake_up will restart the timer */
 534		src->ops->dsi.bus_lock(src);
 535		r = dsicm_wake_up(ddata);
 536		src->ops->dsi.bus_unlock(src);
 537	}
 538
 539	mutex_unlock(&ddata->lock);
 540
 541	if (r)
 542		return r;
 543
 544	return count;
 545}
 546
 547static ssize_t dsicm_show_ulps_timeout(struct device *dev,
 548		struct device_attribute *attr,
 549		char *buf)
 550{
 551	struct panel_drv_data *ddata = dev_get_drvdata(dev);
 552	unsigned int t;
 
 553
 554	mutex_lock(&ddata->lock);
 555	t = ddata->ulps_timeout;
 556	mutex_unlock(&ddata->lock);
 557
 558	return snprintf(buf, PAGE_SIZE, "%u\n", t);
 559}
 560
 561static DEVICE_ATTR(num_dsi_errors, S_IRUGO, dsicm_num_errors_show, NULL);
 562static DEVICE_ATTR(hw_revision, S_IRUGO, dsicm_hw_revision_show, NULL);
 563static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
 564		dsicm_show_ulps, dsicm_store_ulps);
 565static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
 566		dsicm_show_ulps_timeout, dsicm_store_ulps_timeout);
 567
 568static struct attribute *dsicm_attrs[] = {
 569	&dev_attr_num_dsi_errors.attr,
 570	&dev_attr_hw_revision.attr,
 571	&dev_attr_ulps.attr,
 572	&dev_attr_ulps_timeout.attr,
 573	NULL,
 574};
 575
 576static const struct attribute_group dsicm_attr_group = {
 577	.attrs = dsicm_attrs,
 578};
 579
 580static void dsicm_hw_reset(struct panel_drv_data *ddata)
 581{
 582	gpiod_set_value(ddata->reset_gpio, 1);
 
 
 
 583	udelay(10);
 584	/* reset the panel */
 585	gpiod_set_value(ddata->reset_gpio, 0);
 586	/* assert reset */
 587	udelay(10);
 588	gpiod_set_value(ddata->reset_gpio, 1);
 589	/* wait after releasing reset */
 590	usleep_range(5000, 10000);
 591}
 592
 593static int dsicm_power_on(struct panel_drv_data *ddata)
 594{
 595	struct omap_dss_device *src = ddata->src;
 596	u8 id1, id2, id3;
 597	int r;
 598	struct omap_dss_dsi_config dsi_config = {
 599		.mode = OMAP_DSS_DSI_CMD_MODE,
 600		.pixel_format = OMAP_DSS_DSI_FMT_RGB888,
 601		.vm = &ddata->vm,
 602		.hs_clk_min = 150000000,
 603		.hs_clk_max = 300000000,
 604		.lp_clk_min = 7000000,
 605		.lp_clk_max = 10000000,
 606	};
 607
 608	if (ddata->vpnl) {
 609		r = regulator_enable(ddata->vpnl);
 610		if (r) {
 611			dev_err(&ddata->pdev->dev,
 612				"failed to enable VPNL: %d\n", r);
 613			return r;
 614		}
 615	}
 616
 617	if (ddata->vddi) {
 618		r = regulator_enable(ddata->vddi);
 619		if (r) {
 620			dev_err(&ddata->pdev->dev,
 621				"failed to enable VDDI: %d\n", r);
 622			goto err_vpnl;
 623		}
 624	}
 625
 626	if (ddata->pin_config.num_pins > 0) {
 627		r = src->ops->dsi.configure_pins(src, &ddata->pin_config);
 628		if (r) {
 629			dev_err(&ddata->pdev->dev,
 630				"failed to configure DSI pins\n");
 631			goto err_vddi;
 632		}
 633	}
 634
 635	r = src->ops->dsi.set_config(src, &dsi_config);
 636	if (r) {
 637		dev_err(&ddata->pdev->dev, "failed to configure DSI\n");
 638		goto err_vddi;
 639	}
 640
 641	src->ops->enable(src);
 
 
 
 
 642
 643	dsicm_hw_reset(ddata);
 644
 645	src->ops->dsi.enable_hs(src, ddata->channel, false);
 646
 647	r = dsicm_sleep_out(ddata);
 648	if (r)
 649		goto err;
 650
 651	r = dsicm_get_id(ddata, &id1, &id2, &id3);
 652	if (r)
 653		goto err;
 654
 655	r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff);
 656	if (r)
 657		goto err;
 658
 659	r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY,
 660			(1<<2) | (1<<5));	/* BL | BCTRL */
 661	if (r)
 662		goto err;
 663
 664	r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT,
 665		MIPI_DCS_PIXEL_FMT_24BIT);
 666	if (r)
 667		goto err;
 668
 669	r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON);
 670	if (r)
 671		goto err;
 672
 673	r = _dsicm_enable_te(ddata, ddata->te_enabled);
 674	if (r)
 675		goto err;
 676
 677	r = src->ops->dsi.enable_video_output(src, ddata->channel);
 678	if (r)
 679		goto err;
 680
 681	ddata->enabled = true;
 682
 683	if (!ddata->intro_printed) {
 684		dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n",
 685			id1, id2, id3);
 686		ddata->intro_printed = true;
 687	}
 688
 689	src->ops->dsi.enable_hs(src, ddata->channel, true);
 690
 691	return 0;
 692err:
 693	dev_err(&ddata->pdev->dev, "error while enabling panel, issuing HW reset\n");
 694
 695	dsicm_hw_reset(ddata);
 696
 697	src->ops->dsi.disable(src, true, false);
 698err_vddi:
 699	if (ddata->vddi)
 700		regulator_disable(ddata->vddi);
 701err_vpnl:
 702	if (ddata->vpnl)
 703		regulator_disable(ddata->vpnl);
 704
 705	return r;
 706}
 707
 708static void dsicm_power_off(struct panel_drv_data *ddata)
 709{
 710	struct omap_dss_device *src = ddata->src;
 711	int r;
 712
 713	src->ops->dsi.disable_video_output(src, ddata->channel);
 714
 715	r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF);
 716	if (!r)
 717		r = dsicm_sleep_in(ddata);
 718
 719	if (r) {
 720		dev_err(&ddata->pdev->dev,
 721				"error disabling panel, issuing HW reset\n");
 722		dsicm_hw_reset(ddata);
 723	}
 724
 725	src->ops->dsi.disable(src, true, false);
 726
 727	if (ddata->vddi)
 728		regulator_disable(ddata->vddi);
 729	if (ddata->vpnl)
 730		regulator_disable(ddata->vpnl);
 731
 732	ddata->enabled = false;
 733}
 734
 735static int dsicm_panel_reset(struct panel_drv_data *ddata)
 736{
 737	dev_err(&ddata->pdev->dev, "performing LCD reset\n");
 738
 739	dsicm_power_off(ddata);
 740	dsicm_hw_reset(ddata);
 741	return dsicm_power_on(ddata);
 742}
 743
 744static int dsicm_connect(struct omap_dss_device *src,
 745			 struct omap_dss_device *dst)
 746{
 747	struct panel_drv_data *ddata = to_panel_data(dst);
 
 748	struct device *dev = &ddata->pdev->dev;
 749	int r;
 750
 751	r = src->ops->dsi.request_vc(src, &ddata->channel);
 
 
 
 
 
 
 
 
 
 752	if (r) {
 753		dev_err(dev, "failed to get virtual channel\n");
 754		return r;
 755	}
 756
 757	r = src->ops->dsi.set_vc_id(src, ddata->channel, TCH);
 758	if (r) {
 759		dev_err(dev, "failed to set VC_ID\n");
 760		src->ops->dsi.release_vc(src, ddata->channel);
 761		return r;
 762	}
 763
 764	ddata->src = src;
 765	return 0;
 
 
 
 
 
 
 766}
 767
 768static void dsicm_disconnect(struct omap_dss_device *src,
 769			     struct omap_dss_device *dst)
 770{
 771	struct panel_drv_data *ddata = to_panel_data(dst);
 
 
 
 
 772
 773	src->ops->dsi.release_vc(src, ddata->channel);
 774	ddata->src = NULL;
 775}
 776
 777static void dsicm_enable(struct omap_dss_device *dssdev)
 778{
 779	struct panel_drv_data *ddata = to_panel_data(dssdev);
 780	struct omap_dss_device *src = ddata->src;
 781	int r;
 782
 
 
 783	mutex_lock(&ddata->lock);
 784
 785	src->ops->dsi.bus_lock(src);
 
 
 
 
 
 
 
 
 
 
 786
 787	r = dsicm_power_on(ddata);
 788
 789	src->ops->dsi.bus_unlock(src);
 790
 791	if (r)
 792		goto err;
 793
 
 
 794	mutex_unlock(&ddata->lock);
 795
 796	dsicm_bl_power(ddata, true);
 797
 798	return;
 799err:
 800	dev_dbg(&ddata->pdev->dev, "enable failed (%d)\n", r);
 801	mutex_unlock(&ddata->lock);
 
 802}
 803
 804static void dsicm_disable(struct omap_dss_device *dssdev)
 805{
 806	struct panel_drv_data *ddata = to_panel_data(dssdev);
 807	struct omap_dss_device *src = ddata->src;
 808	int r;
 809
 810	dsicm_bl_power(ddata, false);
 811
 812	mutex_lock(&ddata->lock);
 813
 814	dsicm_cancel_ulps_work(ddata);
 815
 816	src->ops->dsi.bus_lock(src);
 
 
 
 
 
 
 817
 818	r = dsicm_wake_up(ddata);
 819	if (!r)
 820		dsicm_power_off(ddata);
 821
 822	src->ops->dsi.bus_unlock(src);
 823
 824	mutex_unlock(&ddata->lock);
 825}
 826
 827static void dsicm_framedone_cb(int err, void *data)
 828{
 829	struct panel_drv_data *ddata = data;
 830	struct omap_dss_device *src = ddata->src;
 831
 832	dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err);
 833	src->ops->dsi.bus_unlock(src);
 834}
 835
 836static irqreturn_t dsicm_te_isr(int irq, void *data)
 837{
 838	struct panel_drv_data *ddata = data;
 839	struct omap_dss_device *src = ddata->src;
 840	int old;
 841	int r;
 842
 843	old = atomic_cmpxchg(&ddata->do_update, 1, 0);
 844
 845	if (old) {
 846		cancel_delayed_work(&ddata->te_timeout_work);
 847
 848		r = src->ops->dsi.update(src, ddata->channel, dsicm_framedone_cb,
 849				ddata);
 850		if (r)
 851			goto err;
 852	}
 853
 854	return IRQ_HANDLED;
 855err:
 856	dev_err(&ddata->pdev->dev, "start update failed\n");
 857	src->ops->dsi.bus_unlock(src);
 858	return IRQ_HANDLED;
 859}
 860
 861static void dsicm_te_timeout_work_callback(struct work_struct *work)
 862{
 863	struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
 864					te_timeout_work.work);
 865	struct omap_dss_device *src = ddata->src;
 866
 867	dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n");
 868
 869	atomic_set(&ddata->do_update, 0);
 870	src->ops->dsi.bus_unlock(src);
 871}
 872
 873static int dsicm_update(struct omap_dss_device *dssdev,
 874				    u16 x, u16 y, u16 w, u16 h)
 875{
 876	struct panel_drv_data *ddata = to_panel_data(dssdev);
 877	struct omap_dss_device *src = ddata->src;
 878	int r;
 879
 880	dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
 881
 882	mutex_lock(&ddata->lock);
 883	src->ops->dsi.bus_lock(src);
 884
 885	r = dsicm_wake_up(ddata);
 886	if (r)
 887		goto err;
 888
 889	if (!ddata->enabled) {
 890		r = 0;
 891		goto err;
 892	}
 893
 894	/* XXX no need to send this every frame, but dsi break if not done */
 895	r = dsicm_set_update_window(ddata, 0, 0, ddata->vm.hactive,
 896				    ddata->vm.vactive);
 
 897	if (r)
 898		goto err;
 899
 900	if (ddata->te_enabled && ddata->ext_te_gpio) {
 901		schedule_delayed_work(&ddata->te_timeout_work,
 902				msecs_to_jiffies(250));
 903		atomic_set(&ddata->do_update, 1);
 904	} else {
 905		r = src->ops->dsi.update(src, ddata->channel, dsicm_framedone_cb,
 906				ddata);
 907		if (r)
 908			goto err;
 909	}
 910
 911	/* note: no bus_unlock here. unlock is src framedone_cb */
 912	mutex_unlock(&ddata->lock);
 913	return 0;
 914err:
 915	src->ops->dsi.bus_unlock(src);
 916	mutex_unlock(&ddata->lock);
 917	return r;
 918}
 919
 920static int dsicm_sync(struct omap_dss_device *dssdev)
 921{
 922	struct panel_drv_data *ddata = to_panel_data(dssdev);
 923	struct omap_dss_device *src = ddata->src;
 924
 925	dev_dbg(&ddata->pdev->dev, "sync\n");
 926
 927	mutex_lock(&ddata->lock);
 928	src->ops->dsi.bus_lock(src);
 929	src->ops->dsi.bus_unlock(src);
 930	mutex_unlock(&ddata->lock);
 931
 932	dev_dbg(&ddata->pdev->dev, "sync done\n");
 933
 934	return 0;
 935}
 936
 937static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable)
 938{
 939	struct omap_dss_device *src = ddata->src;
 940	int r;
 941
 942	if (enable)
 943		r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0);
 944	else
 945		r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF);
 946
 947	if (!ddata->ext_te_gpio)
 948		src->ops->dsi.enable_te(src, enable);
 949
 950	/* possible panel bug */
 951	msleep(100);
 952
 953	return r;
 954}
 955
 956static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable)
 957{
 958	struct panel_drv_data *ddata = to_panel_data(dssdev);
 959	struct omap_dss_device *src = ddata->src;
 960	int r;
 961
 962	mutex_lock(&ddata->lock);
 963
 964	if (ddata->te_enabled == enable)
 965		goto end;
 966
 967	src->ops->dsi.bus_lock(src);
 968
 969	if (ddata->enabled) {
 970		r = dsicm_wake_up(ddata);
 971		if (r)
 972			goto err;
 973
 974		r = _dsicm_enable_te(ddata, enable);
 975		if (r)
 976			goto err;
 977	}
 978
 979	ddata->te_enabled = enable;
 980
 981	src->ops->dsi.bus_unlock(src);
 982end:
 983	mutex_unlock(&ddata->lock);
 984
 985	return 0;
 986err:
 987	src->ops->dsi.bus_unlock(src);
 988	mutex_unlock(&ddata->lock);
 989
 990	return r;
 991}
 992
 993static int dsicm_get_te(struct omap_dss_device *dssdev)
 994{
 995	struct panel_drv_data *ddata = to_panel_data(dssdev);
 996	int r;
 997
 998	mutex_lock(&ddata->lock);
 999	r = ddata->te_enabled;
1000	mutex_unlock(&ddata->lock);
1001
1002	return r;
1003}
1004
1005static int dsicm_memory_read(struct omap_dss_device *dssdev,
1006		void *buf, size_t size,
1007		u16 x, u16 y, u16 w, u16 h)
1008{
1009	struct panel_drv_data *ddata = to_panel_data(dssdev);
1010	struct omap_dss_device *src = ddata->src;
1011	int r;
1012	int first = 1;
1013	int plen;
1014	unsigned int buf_used = 0;
1015
1016	if (size < w * h * 3)
1017		return -ENOMEM;
1018
1019	mutex_lock(&ddata->lock);
1020
1021	if (!ddata->enabled) {
1022		r = -ENODEV;
1023		goto err1;
1024	}
1025
1026	size = min((u32)w * h * 3,
1027		   ddata->vm.hactive * ddata->vm.vactive * 3);
 
1028
1029	src->ops->dsi.bus_lock(src);
1030
1031	r = dsicm_wake_up(ddata);
1032	if (r)
1033		goto err2;
1034
1035	/* plen 1 or 2 goes into short packet. until checksum error is fixed,
1036	 * use short packets. plen 32 works, but bigger packets seem to cause
1037	 * an error. */
1038	if (size % 2)
1039		plen = 1;
1040	else
1041		plen = 2;
1042
1043	dsicm_set_update_window(ddata, x, y, w, h);
1044
1045	r = src->ops->dsi.set_max_rx_packet_size(src, ddata->channel, plen);
1046	if (r)
1047		goto err2;
1048
1049	while (buf_used < size) {
1050		u8 dcs_cmd = first ? 0x2e : 0x3e;
1051		first = 0;
1052
1053		r = src->ops->dsi.dcs_read(src, ddata->channel, dcs_cmd,
1054				buf + buf_used, size - buf_used);
1055
1056		if (r < 0) {
1057			dev_err(dssdev->dev, "read error\n");
1058			goto err3;
1059		}
1060
1061		buf_used += r;
1062
1063		if (r < plen) {
1064			dev_err(&ddata->pdev->dev, "short read\n");
1065			break;
1066		}
1067
1068		if (signal_pending(current)) {
1069			dev_err(&ddata->pdev->dev, "signal pending, "
1070					"aborting memory read\n");
1071			r = -ERESTARTSYS;
1072			goto err3;
1073		}
1074	}
1075
1076	r = buf_used;
1077
1078err3:
1079	src->ops->dsi.set_max_rx_packet_size(src, ddata->channel, 1);
1080err2:
1081	src->ops->dsi.bus_unlock(src);
1082err1:
1083	mutex_unlock(&ddata->lock);
1084	return r;
1085}
1086
1087static void dsicm_ulps_work(struct work_struct *work)
1088{
1089	struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
1090			ulps_work.work);
1091	struct omap_dss_device *dssdev = &ddata->dssdev;
1092	struct omap_dss_device *src = ddata->src;
1093
1094	mutex_lock(&ddata->lock);
1095
1096	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) {
1097		mutex_unlock(&ddata->lock);
1098		return;
1099	}
1100
1101	src->ops->dsi.bus_lock(src);
1102
1103	dsicm_enter_ulps(ddata);
1104
1105	src->ops->dsi.bus_unlock(src);
1106	mutex_unlock(&ddata->lock);
1107}
1108
1109static int dsicm_get_modes(struct omap_dss_device *dssdev,
1110			   struct drm_connector *connector)
1111{
1112	struct panel_drv_data *ddata = to_panel_data(dssdev);
1113
1114	connector->display_info.width_mm = ddata->width_mm;
1115	connector->display_info.height_mm = ddata->height_mm;
1116
1117	return omapdss_display_get_modes(connector, &ddata->vm);
1118}
1119
1120static int dsicm_check_timings(struct omap_dss_device *dssdev,
1121			       struct drm_display_mode *mode)
1122{
1123	struct panel_drv_data *ddata = to_panel_data(dssdev);
1124	int ret = 0;
1125
1126	if (mode->hdisplay != ddata->vm.hactive)
1127		ret = -EINVAL;
1128
1129	if (mode->vdisplay != ddata->vm.vactive)
1130		ret = -EINVAL;
1131
1132	if (ret) {
1133		dev_warn(dssdev->dev, "wrong resolution: %d x %d",
1134			 mode->hdisplay, mode->vdisplay);
1135		dev_warn(dssdev->dev, "panel resolution: %d x %d",
1136			 ddata->vm.hactive, ddata->vm.vactive);
1137	}
1138
1139	return ret;
1140}
1141
1142static const struct omap_dss_device_ops dsicm_ops = {
1143	.connect	= dsicm_connect,
1144	.disconnect	= dsicm_disconnect,
1145
1146	.enable		= dsicm_enable,
1147	.disable	= dsicm_disable,
1148
1149	.get_modes	= dsicm_get_modes,
1150	.check_timings	= dsicm_check_timings,
1151};
1152
1153static const struct omap_dss_driver dsicm_dss_driver = {
1154	.update		= dsicm_update,
1155	.sync		= dsicm_sync,
1156
 
 
 
1157	.enable_te	= dsicm_enable_te,
1158	.get_te		= dsicm_get_te,
1159
1160	.memory_read	= dsicm_memory_read,
1161};
1162
1163static int dsicm_probe_of(struct platform_device *pdev)
1164{
1165	struct device_node *node = pdev->dev.of_node;
1166	struct backlight_device *backlight;
1167	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1168	struct display_timing timing;
1169	int err;
1170
1171	ddata->reset_gpio = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
1172	if (IS_ERR(ddata->reset_gpio)) {
1173		err = PTR_ERR(ddata->reset_gpio);
1174		dev_err(&pdev->dev, "reset gpio request failed: %d", err);
1175		return err;
1176	}
1177
1178	ddata->ext_te_gpio = devm_gpiod_get_optional(&pdev->dev, "te",
1179						     GPIOD_IN);
1180	if (IS_ERR(ddata->ext_te_gpio)) {
1181		err = PTR_ERR(ddata->ext_te_gpio);
1182		dev_err(&pdev->dev, "TE gpio request failed: %d", err);
1183		return err;
1184	}
1185
1186	err = of_get_display_timing(node, "panel-timing", &timing);
1187	if (!err) {
1188		videomode_from_timing(&timing, &ddata->vm);
1189		if (!ddata->vm.pixelclock)
1190			ddata->vm.pixelclock =
1191				ddata->vm.hactive * ddata->vm.vactive * 60;
1192	} else {
1193		dev_warn(&pdev->dev,
1194			 "failed to get video timing, using defaults\n");
1195	}
1196
1197	ddata->width_mm = 0;
1198	of_property_read_u32(node, "width-mm", &ddata->width_mm);
1199
1200	ddata->height_mm = 0;
1201	of_property_read_u32(node, "height-mm", &ddata->height_mm);
1202
1203	ddata->vpnl = devm_regulator_get_optional(&pdev->dev, "vpnl");
1204	if (IS_ERR(ddata->vpnl)) {
1205		err = PTR_ERR(ddata->vpnl);
1206		if (err == -EPROBE_DEFER)
1207			return err;
1208		ddata->vpnl = NULL;
1209	}
1210
1211	ddata->vddi = devm_regulator_get_optional(&pdev->dev, "vddi");
1212	if (IS_ERR(ddata->vddi)) {
1213		err = PTR_ERR(ddata->vddi);
1214		if (err == -EPROBE_DEFER)
1215			return err;
1216		ddata->vddi = NULL;
1217	}
1218
1219	backlight = devm_of_find_backlight(&pdev->dev);
1220	if (IS_ERR(backlight))
1221		return PTR_ERR(backlight);
1222
1223	/* If no backlight device is found assume native backlight support */
1224	if (backlight)
1225		ddata->extbldev = backlight;
1226	else
1227		ddata->use_dsi_backlight = true;
1228
1229	/* TODO: ulps */
1230
1231	return 0;
1232}
1233
1234static int dsicm_probe(struct platform_device *pdev)
1235{
 
1236	struct panel_drv_data *ddata;
1237	struct backlight_device *bldev = NULL;
1238	struct device *dev = &pdev->dev;
1239	struct omap_dss_device *dssdev;
1240	int r;
1241
1242	dev_dbg(dev, "probe\n");
1243
1244	ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
1245	if (!ddata)
1246		return -ENOMEM;
1247
1248	platform_set_drvdata(pdev, ddata);
1249	ddata->pdev = pdev;
1250
1251	ddata->vm.hactive = 864;
1252	ddata->vm.vactive = 480;
1253	ddata->vm.pixelclock = 864 * 480 * 60;
1254
1255	r = dsicm_probe_of(pdev);
1256	if (r)
1257		return r;
1258
 
 
 
 
1259	dssdev = &ddata->dssdev;
1260	dssdev->dev = dev;
1261	dssdev->ops = &dsicm_ops;
1262	dssdev->driver = &dsicm_dss_driver;
1263	dssdev->type = OMAP_DISPLAY_TYPE_DSI;
1264	dssdev->display = true;
1265	dssdev->owner = THIS_MODULE;
1266	dssdev->of_port = 0;
1267	dssdev->ops_flags = OMAP_DSS_DEVICE_OP_MODES;
1268
 
1269	dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
1270		OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
1271
1272	omapdss_display_init(dssdev);
1273	omapdss_device_register(dssdev);
 
 
 
1274
1275	mutex_init(&ddata->lock);
1276
1277	atomic_set(&ddata->do_update, 0);
1278
1279	if (ddata->ext_te_gpio) {
1280		r = devm_request_irq(dev, gpiod_to_irq(ddata->ext_te_gpio),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1281				dsicm_te_isr,
1282				IRQF_TRIGGER_RISING,
1283				"taal vsync", ddata);
1284
1285		if (r) {
1286			dev_err(dev, "IRQ request failed\n");
1287			goto err_reg;
1288		}
1289
1290		INIT_DEFERRABLE_WORK(&ddata->te_timeout_work,
1291					dsicm_te_timeout_work_callback);
1292
1293		dev_dbg(dev, "Using GPIO TE\n");
1294	}
1295
1296	ddata->workqueue = create_singlethread_workqueue("dsicm_wq");
1297	if (!ddata->workqueue) {
1298		r = -ENOMEM;
1299		goto err_reg;
1300	}
1301	INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work);
1302
1303	dsicm_hw_reset(ddata);
1304
1305	if (ddata->use_dsi_backlight) {
1306		struct backlight_properties props = { 0 };
1307		props.max_brightness = 255;
 
1308		props.type = BACKLIGHT_RAW;
1309
1310		bldev = devm_backlight_device_register(dev, dev_name(dev),
1311			dev, ddata, &dsicm_bl_ops, &props);
1312		if (IS_ERR(bldev)) {
1313			r = PTR_ERR(bldev);
1314			goto err_bl;
1315		}
1316
1317		ddata->bldev = bldev;
 
 
 
 
 
 
1318	}
1319
1320	r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
1321	if (r) {
1322		dev_err(dev, "failed to create sysfs files\n");
1323		goto err_bl;
1324	}
1325
1326	return 0;
1327
 
 
 
1328err_bl:
1329	destroy_workqueue(ddata->workqueue);
1330err_reg:
1331	if (ddata->extbldev)
1332		put_device(&ddata->extbldev->dev);
1333
1334	return r;
1335}
1336
1337static int __exit dsicm_remove(struct platform_device *pdev)
1338{
1339	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1340	struct omap_dss_device *dssdev = &ddata->dssdev;
 
1341
1342	dev_dbg(&pdev->dev, "remove\n");
1343
1344	omapdss_device_unregister(dssdev);
1345
1346	if (omapdss_device_is_enabled(dssdev))
1347		dsicm_disable(dssdev);
1348	omapdss_device_disconnect(ddata->src, dssdev);
1349
1350	sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group);
1351
1352	if (ddata->extbldev)
1353		put_device(&ddata->extbldev->dev);
 
 
 
 
 
 
1354
1355	dsicm_cancel_ulps_work(ddata);
1356	destroy_workqueue(ddata->workqueue);
1357
1358	/* reset, to be sure that the panel is in a valid state */
1359	dsicm_hw_reset(ddata);
1360
1361	return 0;
1362}
1363
1364static const struct of_device_id dsicm_of_match[] = {
1365	{ .compatible = "omapdss,panel-dsi-cm", },
1366	{},
1367};
1368
1369MODULE_DEVICE_TABLE(of, dsicm_of_match);
1370
1371static struct platform_driver dsicm_driver = {
1372	.probe = dsicm_probe,
1373	.remove = __exit_p(dsicm_remove),
1374	.driver = {
1375		.name = "panel-dsi-cm",
1376		.of_match_table = dsicm_of_match,
1377		.suppress_bind_attrs = true,
1378	},
1379};
1380
1381module_platform_driver(dsicm_driver);
1382
1383MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
1384MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
1385MODULE_LICENSE("GPL");