blob: 88d330d95f2c904175f821773ef1f15397c2790e [file] [log] [blame]
Kirill A. Shutemov026abc32012-03-08 16:02:20 +00001/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * jim liu <jim.liu@intel.com>
25 * Jackie Li<yaodong.li@intel.com>
26 */
27
28#include <linux/module.h>
29
30#include "mdfld_dsi_output.h"
31#include "mdfld_dsi_dpi.h"
32#include "mdfld_output.h"
33#include "mdfld_dsi_pkg_sender.h"
34#include "tc35876x-dsi-lvds.h"
35#include <linux/pm_runtime.h>
36#include <asm/intel_scu_ipc.h>
37
38/* get the LABC from command line. */
39static int LABC_control = 1;
40
41#ifdef MODULE
42module_param(LABC_control, int, 0644);
43#else
44
45static int __init parse_LABC_control(char *arg)
46{
47 /* LABC control can be passed in as a cmdline parameter */
48 /* to enable this feature add LABC=1 to cmdline */
49 /* to disable this feature add LABC=0 to cmdline */
50 if (!arg)
51 return -EINVAL;
52
53 if (!strcasecmp(arg, "0"))
54 LABC_control = 0;
55 else if (!strcasecmp(arg, "1"))
56 LABC_control = 1;
57
58 return 0;
59}
60early_param("LABC", parse_LABC_control);
61#endif
62
63/**
64 * Check and see if the generic control or data buffer is empty and ready.
65 */
66void mdfld_dsi_gen_fifo_ready(struct drm_device *dev, u32 gen_fifo_stat_reg,
67 u32 fifo_stat)
68{
69 u32 GEN_BF_time_out_count;
70
71 /* Check MIPI Adatper command registers */
72 for (GEN_BF_time_out_count = 0;
73 GEN_BF_time_out_count < GEN_FB_TIME_OUT;
74 GEN_BF_time_out_count++) {
75 if ((REG_READ(gen_fifo_stat_reg) & fifo_stat) == fifo_stat)
76 break;
77 udelay(100);
78 }
79
80 if (GEN_BF_time_out_count == GEN_FB_TIME_OUT)
81 DRM_ERROR("mdfld_dsi_gen_fifo_ready, Timeout. gen_fifo_stat_reg = 0x%x.\n",
82 gen_fifo_stat_reg);
83}
84
85/**
86 * Manage the DSI MIPI keyboard and display brightness.
87 * FIXME: this is exported to OSPM code. should work out an specific
88 * display interface to OSPM.
89 */
90
91void mdfld_dsi_brightness_init(struct mdfld_dsi_config *dsi_config, int pipe)
92{
93 struct mdfld_dsi_pkg_sender *sender =
94 mdfld_dsi_get_pkg_sender(dsi_config);
95 struct drm_device *dev = sender->dev;
96 struct drm_psb_private *dev_priv = dev->dev_private;
97 u32 gen_ctrl_val;
98
99 if (!sender) {
100 DRM_ERROR("No sender found\n");
101 return;
102 }
103
104 /* Set default display backlight value to 85% (0xd8)*/
105 mdfld_dsi_send_mcs_short(sender, write_display_brightness, 0xd8, 1,
106 true);
107
108 /* Set minimum brightness setting of CABC function to 20% (0x33)*/
109 mdfld_dsi_send_mcs_short(sender, write_cabc_min_bright, 0x33, 1, true);
110
111 /* Enable backlight or/and LABC */
112 gen_ctrl_val = BRIGHT_CNTL_BLOCK_ON | DISPLAY_DIMMING_ON |
113 BACKLIGHT_ON;
114 if (LABC_control == 1)
115 gen_ctrl_val |= DISPLAY_DIMMING_ON | DISPLAY_BRIGHTNESS_AUTO
116 | GAMMA_AUTO;
117
118 if (LABC_control == 1)
119 gen_ctrl_val |= AMBIENT_LIGHT_SENSE_ON;
120
121 dev_priv->mipi_ctrl_display = gen_ctrl_val;
122
123 mdfld_dsi_send_mcs_short(sender, write_ctrl_display, (u8)gen_ctrl_val,
124 1, true);
125
126 mdfld_dsi_send_mcs_short(sender, write_ctrl_cabc, UI_IMAGE, 1, true);
127}
128
129void mdfld_dsi_brightness_control(struct drm_device *dev, int pipe, int level)
130{
131 struct mdfld_dsi_pkg_sender *sender;
132 struct drm_psb_private *dev_priv;
133 struct mdfld_dsi_config *dsi_config;
134 u32 gen_ctrl_val = 0;
135 int p_type = TMD_VID;
136
137 if (!dev || (pipe != 0 && pipe != 2)) {
138 DRM_ERROR("Invalid parameter\n");
139 return;
140 }
141
142 p_type = mdfld_get_panel_type(dev, 0);
143
144 dev_priv = dev->dev_private;
145
146 if (pipe)
147 dsi_config = dev_priv->dsi_configs[1];
148 else
149 dsi_config = dev_priv->dsi_configs[0];
150
151 sender = mdfld_dsi_get_pkg_sender(dsi_config);
152
153 if (!sender) {
154 DRM_ERROR("No sender found\n");
155 return;
156 }
157
158 gen_ctrl_val = (level * 0xff / MDFLD_DSI_BRIGHTNESS_MAX_LEVEL) & 0xff;
159
160 dev_dbg(sender->dev->dev, "pipe = %d, gen_ctrl_val = %d.\n",
161 pipe, gen_ctrl_val);
162
163 if (p_type == TMD_VID) {
164 /* Set display backlight value */
165 mdfld_dsi_send_mcs_short(sender, tmd_write_display_brightness,
166 (u8)gen_ctrl_val, 1, true);
167 } else {
168 /* Set display backlight value */
169 mdfld_dsi_send_mcs_short(sender, write_display_brightness,
170 (u8)gen_ctrl_val, 1, true);
171
172 /* Enable backlight control */
173 if (level == 0)
174 gen_ctrl_val = 0;
175 else
176 gen_ctrl_val = dev_priv->mipi_ctrl_display;
177
178 mdfld_dsi_send_mcs_short(sender, write_ctrl_display,
179 (u8)gen_ctrl_val, 1, true);
180 }
181}
182
183static int mdfld_dsi_get_panel_status(struct mdfld_dsi_config *dsi_config,
184 u8 dcs, u32 *data, bool hs)
185{
186 struct mdfld_dsi_pkg_sender *sender
187 = mdfld_dsi_get_pkg_sender(dsi_config);
188
189 if (!sender || !data) {
190 DRM_ERROR("Invalid parameter\n");
191 return -EINVAL;
192 }
193
194 return mdfld_dsi_read_mcs(sender, dcs, data, 1, hs);
195}
196
197int mdfld_dsi_get_power_mode(struct mdfld_dsi_config *dsi_config, u32 *mode,
198 bool hs)
199{
200 if (!dsi_config || !mode) {
201 DRM_ERROR("Invalid parameter\n");
202 return -EINVAL;
203 }
204
205 return mdfld_dsi_get_panel_status(dsi_config, 0x0a, mode, hs);
206}
207
208int mdfld_dsi_get_diagnostic_result(struct mdfld_dsi_config *dsi_config,
209 u32 *result, bool hs)
210{
211 if (!dsi_config || !result) {
212 DRM_ERROR("Invalid parameter\n");
213 return -EINVAL;
214 }
215
216 return mdfld_dsi_get_panel_status(dsi_config, 0x0f, result, hs);
217}
218
219/*
220 * NOTE: this function was used by OSPM.
221 * TODO: will be removed later, should work out display interfaces for OSPM
222 */
223void mdfld_dsi_controller_init(struct mdfld_dsi_config *dsi_config, int pipe)
224{
225 if (!dsi_config || ((pipe != 0) && (pipe != 2))) {
226 DRM_ERROR("Invalid parameters\n");
227 return;
228 }
229
230 mdfld_dsi_dpi_controller_init(dsi_config, pipe);
231}
232
233static void mdfld_dsi_connector_save(struct drm_connector *connector)
234{
235}
236
237static void mdfld_dsi_connector_restore(struct drm_connector *connector)
238{
239}
240
241/* FIXME: start using the force parameter */
242static enum drm_connector_status
243mdfld_dsi_connector_detect(struct drm_connector *connector, bool force)
244{
245 struct mdfld_dsi_connector *dsi_connector
246 = mdfld_dsi_connector(connector);
247
248 dsi_connector->status = connector_status_connected;
249
250 return dsi_connector->status;
251}
252
253static int mdfld_dsi_connector_set_property(struct drm_connector *connector,
254 struct drm_property *property,
255 uint64_t value)
256{
257 struct drm_encoder *encoder = connector->encoder;
258 struct backlight_device *psb_bd;
259
260 if (!strcmp(property->name, "scaling mode") && encoder) {
261 struct psb_intel_crtc *psb_crtc =
262 to_psb_intel_crtc(encoder->crtc);
263 bool centerechange;
264 uint64_t val;
265
266 if (!psb_crtc)
267 goto set_prop_error;
268
269 switch (value) {
270 case DRM_MODE_SCALE_FULLSCREEN:
271 break;
272 case DRM_MODE_SCALE_NO_SCALE:
273 break;
274 case DRM_MODE_SCALE_ASPECT:
275 break;
276 default:
277 goto set_prop_error;
278 }
279
280 if (drm_connector_property_get_value(connector, property, &val))
281 goto set_prop_error;
282
283 if (val == value)
284 goto set_prop_done;
285
286 if (drm_connector_property_set_value(connector,
287 property, value))
288 goto set_prop_error;
289
290 centerechange = (val == DRM_MODE_SCALE_NO_SCALE) ||
291 (value == DRM_MODE_SCALE_NO_SCALE);
292
293 if (psb_crtc->saved_mode.hdisplay != 0 &&
294 psb_crtc->saved_mode.vdisplay != 0) {
295 if (centerechange) {
296 if (!drm_crtc_helper_set_mode(encoder->crtc,
297 &psb_crtc->saved_mode,
298 encoder->crtc->x,
299 encoder->crtc->y,
300 encoder->crtc->fb))
301 goto set_prop_error;
302 } else {
303 struct drm_encoder_helper_funcs *funcs =
304 encoder->helper_private;
305 funcs->mode_set(encoder,
306 &psb_crtc->saved_mode,
307 &psb_crtc->saved_adjusted_mode);
308 }
309 }
310 } else if (!strcmp(property->name, "backlight") && encoder) {
311 if (drm_connector_property_set_value(connector, property,
312 value))
313 goto set_prop_error;
314 else {
315 psb_bd = mdfld_get_backlight_device();
316 if (psb_bd) {
317 psb_bd->props.brightness = value;
318 mdfld_set_brightness(psb_bd);
319 }
320 }
321 }
322set_prop_done:
323 return 0;
324set_prop_error:
325 return -1;
326}
327
328static void mdfld_dsi_connector_destroy(struct drm_connector *connector)
329{
330 struct mdfld_dsi_connector *dsi_connector =
331 mdfld_dsi_connector(connector);
332 struct mdfld_dsi_pkg_sender *sender;
333
334 if (!dsi_connector)
335 return;
336 drm_sysfs_connector_remove(connector);
337 drm_connector_cleanup(connector);
338 sender = dsi_connector->pkg_sender;
339 mdfld_dsi_pkg_sender_destroy(sender);
340 kfree(dsi_connector);
341}
342
343static int mdfld_dsi_connector_get_modes(struct drm_connector *connector)
344{
345 struct mdfld_dsi_connector *dsi_connector =
346 mdfld_dsi_connector(connector);
347 struct mdfld_dsi_config *dsi_config =
348 mdfld_dsi_get_config(dsi_connector);
349 struct drm_display_mode *fixed_mode = dsi_config->fixed_mode;
350 struct drm_display_mode *dup_mode = NULL;
351 struct drm_device *dev = connector->dev;
352
353 connector->display_info.min_vfreq = 0;
354 connector->display_info.max_vfreq = 200;
355 connector->display_info.min_hfreq = 0;
356 connector->display_info.max_hfreq = 200;
357
358 if (fixed_mode) {
359 dev_dbg(dev->dev, "fixed_mode %dx%d\n",
360 fixed_mode->hdisplay, fixed_mode->vdisplay);
361 dup_mode = drm_mode_duplicate(dev, fixed_mode);
362 drm_mode_probed_add(connector, dup_mode);
363 return 1;
364 }
365 DRM_ERROR("Didn't get any modes!\n");
366 return 0;
367}
368
369static int mdfld_dsi_connector_mode_valid(struct drm_connector *connector,
370 struct drm_display_mode *mode)
371{
372 struct mdfld_dsi_connector *dsi_connector =
373 mdfld_dsi_connector(connector);
374 struct mdfld_dsi_config *dsi_config =
375 mdfld_dsi_get_config(dsi_connector);
376 struct drm_display_mode *fixed_mode = dsi_config->fixed_mode;
377
378 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
379 return MODE_NO_DBLESCAN;
380
381 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
382 return MODE_NO_INTERLACE;
383
384 /**
385 * FIXME: current DC has no fitting unit, reject any mode setting
386 * request
387 * Will figure out a way to do up-scaling(pannel fitting) later.
388 **/
389 if (fixed_mode) {
390 if (mode->hdisplay != fixed_mode->hdisplay)
391 return MODE_PANEL;
392
393 if (mode->vdisplay != fixed_mode->vdisplay)
394 return MODE_PANEL;
395 }
396
397 return MODE_OK;
398}
399
400static void mdfld_dsi_connector_dpms(struct drm_connector *connector, int mode)
401{
402 if (mode == connector->dpms)
403 return;
404
405 /*first, execute dpms*/
406
407 drm_helper_connector_dpms(connector, mode);
408}
409
410static struct drm_encoder *mdfld_dsi_connector_best_encoder(
411 struct drm_connector *connector)
412{
413 struct mdfld_dsi_connector *dsi_connector =
414 mdfld_dsi_connector(connector);
415 struct mdfld_dsi_config *dsi_config =
416 mdfld_dsi_get_config(dsi_connector);
417 return &dsi_config->encoder->base.base;
418}
419
420/*DSI connector funcs*/
421static const struct drm_connector_funcs mdfld_dsi_connector_funcs = {
422 .dpms = /*drm_helper_connector_dpms*/mdfld_dsi_connector_dpms,
423 .save = mdfld_dsi_connector_save,
424 .restore = mdfld_dsi_connector_restore,
425 .detect = mdfld_dsi_connector_detect,
426 .fill_modes = drm_helper_probe_single_connector_modes,
427 .set_property = mdfld_dsi_connector_set_property,
428 .destroy = mdfld_dsi_connector_destroy,
429};
430
431/*DSI connector helper funcs*/
432static const struct drm_connector_helper_funcs
433 mdfld_dsi_connector_helper_funcs = {
434 .get_modes = mdfld_dsi_connector_get_modes,
435 .mode_valid = mdfld_dsi_connector_mode_valid,
436 .best_encoder = mdfld_dsi_connector_best_encoder,
437};
438
439static int mdfld_dsi_get_default_config(struct drm_device *dev,
440 struct mdfld_dsi_config *config, int pipe)
441{
442 if (!dev || !config) {
443 DRM_ERROR("Invalid parameters");
444 return -EINVAL;
445 }
446
447 config->bpp = 24;
448 if (mdfld_get_panel_type(dev, pipe) == TC35876X)
449 config->lane_count = 4;
450 else
451 config->lane_count = 2;
452 config->channel_num = 0;
453
454 if (mdfld_get_panel_type(dev, pipe) == TMD_VID)
455 config->video_mode = MDFLD_DSI_VIDEO_NON_BURST_MODE_SYNC_PULSE;
456 else if (mdfld_get_panel_type(dev, pipe) == TC35876X)
457 config->video_mode =
458 MDFLD_DSI_VIDEO_NON_BURST_MODE_SYNC_EVENTS;
459 else
460 config->video_mode = MDFLD_DSI_VIDEO_BURST_MODE;
461
462 return 0;
463}
464
465int mdfld_dsi_panel_reset(int pipe)
466{
467 unsigned gpio;
468 int ret = 0;
469
470 switch (pipe) {
471 case 0:
472 gpio = 128;
473 break;
474 case 2:
475 gpio = 34;
476 break;
477 default:
478 DRM_ERROR("Invalid output\n");
479 return -EINVAL;
480 }
481
482 ret = gpio_request(gpio, "gfx");
483 if (ret) {
484 DRM_ERROR("gpio_rqueset failed\n");
485 return ret;
486 }
487
488 ret = gpio_direction_output(gpio, 1);
489 if (ret) {
490 DRM_ERROR("gpio_direction_output failed\n");
491 goto gpio_error;
492 }
493
494 gpio_get_value(128);
495
496gpio_error:
497 if (gpio_is_valid(gpio))
498 gpio_free(gpio);
499
500 return ret;
501}
502
503/*
504 * MIPI output init
505 * @dev drm device
506 * @pipe pipe number. 0 or 2
507 * @config
508 *
509 * Do the initialization of a MIPI output, including create DRM mode objects
510 * initialization of DSI output on @pipe
511 */
512void mdfld_dsi_output_init(struct drm_device *dev,
513 int pipe,
Kirill A. Shutemov026abc32012-03-08 16:02:20 +0000514 const struct panel_funcs *p_vid_funcs)
515{
516 struct mdfld_dsi_config *dsi_config;
517 struct mdfld_dsi_connector *dsi_connector;
518 struct drm_connector *connector;
519 struct mdfld_dsi_encoder *encoder;
520 struct drm_psb_private *dev_priv = dev->dev_private;
521 struct panel_info dsi_panel_info;
522 u32 width_mm, height_mm;
523
524 dev_dbg(dev->dev, "init DSI output on pipe %d\n", pipe);
525
526 if (!dev || ((pipe != 0) && (pipe != 2))) {
527 DRM_ERROR("Invalid parameter\n");
528 return;
529 }
530
531 /*create a new connetor*/
532 dsi_connector = kzalloc(sizeof(struct mdfld_dsi_connector), GFP_KERNEL);
533 if (!dsi_connector) {
534 DRM_ERROR("No memory");
535 return;
536 }
537
538 dsi_connector->pipe = pipe;
539
Kirill A. Shutemovfda95c22012-03-08 16:02:50 +0000540 dsi_config = kzalloc(sizeof(struct mdfld_dsi_config),
541 GFP_KERNEL);
542 if (!dsi_config) {
543 DRM_ERROR("cannot allocate memory for DSI config\n");
544 goto dsi_init_err0;
Kirill A. Shutemov026abc32012-03-08 16:02:20 +0000545 }
Kirill A. Shutemovfda95c22012-03-08 16:02:50 +0000546 mdfld_dsi_get_default_config(dev, dsi_config, pipe);
Kirill A. Shutemov026abc32012-03-08 16:02:20 +0000547
548 dsi_connector->private = dsi_config;
549
550 dsi_config->changed = 1;
551 dsi_config->dev = dev;
552
553 dsi_config->fixed_mode = p_vid_funcs->get_config_mode(dev);
554 if (p_vid_funcs->get_panel_info(dev, pipe, &dsi_panel_info))
555 goto dsi_init_err0;
556
557 width_mm = dsi_panel_info.width_mm;
558 height_mm = dsi_panel_info.height_mm;
559
560 dsi_config->mode = dsi_config->fixed_mode;
561 dsi_config->connector = dsi_connector;
562
563 if (!dsi_config->fixed_mode) {
564 DRM_ERROR("No pannel fixed mode was found\n");
565 goto dsi_init_err0;
566 }
567
568 if (pipe && dev_priv->dsi_configs[0]) {
569 dsi_config->dvr_ic_inited = 0;
570 dev_priv->dsi_configs[1] = dsi_config;
571 } else if (pipe == 0) {
572 dsi_config->dvr_ic_inited = 1;
573 dev_priv->dsi_configs[0] = dsi_config;
574 } else {
575 DRM_ERROR("Trying to init MIPI1 before MIPI0\n");
576 goto dsi_init_err0;
577 }
578
579
580 connector = &dsi_connector->base.base;
581 drm_connector_init(dev, connector, &mdfld_dsi_connector_funcs,
582 DRM_MODE_CONNECTOR_LVDS);
583 drm_connector_helper_add(connector, &mdfld_dsi_connector_helper_funcs);
584
585 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
586 connector->display_info.width_mm = width_mm;
587 connector->display_info.height_mm = height_mm;
588 connector->interlace_allowed = false;
589 connector->doublescan_allowed = false;
590
591 /*attach properties*/
592 drm_connector_attach_property(connector,
593 dev->mode_config.scaling_mode_property,
594 DRM_MODE_SCALE_FULLSCREEN);
595 drm_connector_attach_property(connector,
596 dev_priv->backlight_property,
597 MDFLD_DSI_BRIGHTNESS_MAX_LEVEL);
598
599 /*init DSI package sender on this output*/
600 if (mdfld_dsi_pkg_sender_init(dsi_connector, pipe)) {
601 DRM_ERROR("Package Sender initialization failed on pipe %d\n",
602 pipe);
603 goto dsi_init_err0;
604 }
605
606 encoder = mdfld_dsi_dpi_init(dev, dsi_connector, p_vid_funcs);
607 if (!encoder) {
608 DRM_ERROR("Create DPI encoder failed\n");
609 goto dsi_init_err1;
610 }
611 encoder->private = dsi_config;
612 dsi_config->encoder = encoder;
613 encoder->base.type = (pipe == 0) ? INTEL_OUTPUT_MIPI :
614 INTEL_OUTPUT_MIPI2;
615 drm_sysfs_connector_add(connector);
616 return;
617
618 /*TODO: add code to destroy outputs on error*/
619dsi_init_err1:
620 /*destroy sender*/
621 mdfld_dsi_pkg_sender_destroy(dsi_connector->pkg_sender);
622
623 drm_connector_cleanup(connector);
624
625 kfree(dsi_config->fixed_mode);
626 kfree(dsi_config);
627dsi_init_err0:
628 kfree(dsi_connector);
629}