2 * Support for ACX565AKM LCD Panel used on Nokia N900
4 * Copyright (C) 2010 Nokia Corporation
6 * Original Driver Author: Imre Deak <imre.deak@nokia.com>
7 * Based on panel-generic.c by Tomi Valkeinen <tomi.valkeinen@nokia.com>
8 * Adapted to new DSS2 framework: Roger Quadros <roger.quadros@nokia.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/delay.h>
27 #include <linux/spi/spi.h>
28 #include <linux/jiffies.h>
29 #include <linux/sched.h>
30 #include <linux/backlight.h>
32 #include <linux/gpio.h>
34 #include <video/omapdss.h>
35 #include <video/omap-panel-data.h>
37 #define MIPID_CMD_READ_DISP_ID 0x04
38 #define MIPID_CMD_READ_RED 0x06
39 #define MIPID_CMD_READ_GREEN 0x07
40 #define MIPID_CMD_READ_BLUE 0x08
41 #define MIPID_CMD_READ_DISP_STATUS 0x09
42 #define MIPID_CMD_RDDSDR 0x0F
43 #define MIPID_CMD_SLEEP_IN 0x10
44 #define MIPID_CMD_SLEEP_OUT 0x11
45 #define MIPID_CMD_DISP_OFF 0x28
46 #define MIPID_CMD_DISP_ON 0x29
47 #define MIPID_CMD_WRITE_DISP_BRIGHTNESS 0x51
48 #define MIPID_CMD_READ_DISP_BRIGHTNESS 0x52
49 #define MIPID_CMD_WRITE_CTRL_DISP 0x53
51 #define CTRL_DISP_BRIGHTNESS_CTRL_ON (1 << 5)
52 #define CTRL_DISP_AMBIENT_LIGHT_CTRL_ON (1 << 4)
53 #define CTRL_DISP_BACKLIGHT_ON (1 << 2)
54 #define CTRL_DISP_AUTO_BRIGHTNESS_ON (1 << 1)
56 #define MIPID_CMD_READ_CTRL_DISP 0x54
57 #define MIPID_CMD_WRITE_CABC 0x55
58 #define MIPID_CMD_READ_CABC 0x56
60 #define MIPID_VER_LPH8923 3
61 #define MIPID_VER_LS041Y3 4
62 #define MIPID_VER_L4F00311 8
63 #define MIPID_VER_ACX565AKM 9
65 struct acx565akm_device {
74 unsigned long hw_guard_end; /* next value of jiffies
76 next sleep in/out command */
77 unsigned long hw_guard_wait; /* max guard time in jiffies */
79 struct spi_device *spi;
82 struct omap_dss_device *dssdev;
83 struct backlight_device *bl_dev;
86 static struct acx565akm_device acx_dev;
87 static int acx565akm_bl_update_status(struct backlight_device *dev);
89 /*--------------------MIPID interface-----------------------------*/
91 static void acx565akm_transfer(struct acx565akm_device *md, int cmd,
92 const u8 *wbuf, int wlen, u8 *rbuf, int rlen)
95 struct spi_transfer *x, xfer[5];
98 BUG_ON(md->spi == NULL);
100 spi_message_init(&m);
102 memset(xfer, 0, sizeof(xfer));
107 x->bits_per_word = 9;
110 if (rlen > 1 && wlen == 0) {
112 * Between the command and the response data there is a
113 * dummy clock cycle. Add an extra bit after the command
114 * word to account for this.
116 x->bits_per_word = 10;
119 spi_message_add_tail(x, &m);
125 x->bits_per_word = 9;
126 spi_message_add_tail(x, &m);
133 spi_message_add_tail(x, &m);
136 r = spi_sync(md->spi, &m);
138 dev_dbg(&md->spi->dev, "spi_sync %d\n", r);
141 static inline void acx565akm_cmd(struct acx565akm_device *md, int cmd)
143 acx565akm_transfer(md, cmd, NULL, 0, NULL, 0);
146 static inline void acx565akm_write(struct acx565akm_device *md,
147 int reg, const u8 *buf, int len)
149 acx565akm_transfer(md, reg, buf, len, NULL, 0);
152 static inline void acx565akm_read(struct acx565akm_device *md,
153 int reg, u8 *buf, int len)
155 acx565akm_transfer(md, reg, NULL, 0, buf, len);
158 static void hw_guard_start(struct acx565akm_device *md, int guard_msec)
160 md->hw_guard_wait = msecs_to_jiffies(guard_msec);
161 md->hw_guard_end = jiffies + md->hw_guard_wait;
164 static void hw_guard_wait(struct acx565akm_device *md)
166 unsigned long wait = md->hw_guard_end - jiffies;
168 if ((long)wait > 0 && wait <= md->hw_guard_wait) {
169 set_current_state(TASK_UNINTERRUPTIBLE);
170 schedule_timeout(wait);
174 /*----------------------MIPID wrappers----------------------------*/
176 static void set_sleep_mode(struct acx565akm_device *md, int on)
181 cmd = MIPID_CMD_SLEEP_IN;
183 cmd = MIPID_CMD_SLEEP_OUT;
185 * We have to keep 120msec between sleep in/out commands.
189 acx565akm_cmd(md, cmd);
190 hw_guard_start(md, 120);
193 static void set_display_state(struct acx565akm_device *md, int enabled)
195 int cmd = enabled ? MIPID_CMD_DISP_ON : MIPID_CMD_DISP_OFF;
197 acx565akm_cmd(md, cmd);
200 static int panel_enabled(struct acx565akm_device *md)
205 acx565akm_read(md, MIPID_CMD_READ_DISP_STATUS, (u8 *)&disp_status, 4);
206 disp_status = __be32_to_cpu(disp_status);
207 enabled = (disp_status & (1 << 17)) && (disp_status & (1 << 10));
208 dev_dbg(&md->spi->dev,
209 "LCD panel %senabled by bootloader (status 0x%04x)\n",
210 enabled ? "" : "not ", disp_status);
214 static int panel_detect(struct acx565akm_device *md)
216 acx565akm_read(md, MIPID_CMD_READ_DISP_ID, md->display_id, 3);
217 dev_dbg(&md->spi->dev, "MIPI display ID: %02x%02x%02x\n",
218 md->display_id[0], md->display_id[1], md->display_id[2]);
220 switch (md->display_id[0]) {
222 md->model = MIPID_VER_ACX565AKM;
223 md->name = "acx565akm";
228 md->model = MIPID_VER_L4F00311;
229 md->name = "l4f00311";
232 md->model = MIPID_VER_LPH8923;
233 md->name = "lph8923";
236 md->model = MIPID_VER_LS041Y3;
237 md->name = "ls041y3";
240 md->name = "unknown";
241 dev_err(&md->spi->dev, "invalid display ID\n");
245 md->revision = md->display_id[1];
247 dev_info(&md->spi->dev, "omapfb: %s rev %02x LCD detected\n",
248 md->name, md->revision);
253 /*----------------------Backlight Control-------------------------*/
255 static void enable_backlight_ctrl(struct acx565akm_device *md, int enable)
259 acx565akm_read(md, MIPID_CMD_READ_CTRL_DISP, (u8 *)&ctrl, 1);
261 ctrl |= CTRL_DISP_BRIGHTNESS_CTRL_ON |
262 CTRL_DISP_BACKLIGHT_ON;
264 ctrl &= ~(CTRL_DISP_BRIGHTNESS_CTRL_ON |
265 CTRL_DISP_BACKLIGHT_ON);
269 acx565akm_write(md, MIPID_CMD_WRITE_CTRL_DISP, (u8 *)&ctrl, 2);
272 static void set_cabc_mode(struct acx565akm_device *md, unsigned mode)
276 md->cabc_mode = mode;
280 acx565akm_read(md, MIPID_CMD_READ_CABC, (u8 *)&cabc_ctrl, 1);
282 cabc_ctrl |= (1 << 8) | (mode & 3);
283 acx565akm_write(md, MIPID_CMD_WRITE_CABC, (u8 *)&cabc_ctrl, 2);
286 static unsigned get_cabc_mode(struct acx565akm_device *md)
288 return md->cabc_mode;
291 static unsigned get_hw_cabc_mode(struct acx565akm_device *md)
295 acx565akm_read(md, MIPID_CMD_READ_CABC, &cabc_ctrl, 1);
296 return cabc_ctrl & 3;
299 static void acx565akm_set_brightness(struct acx565akm_device *md, int level)
303 bv = level | (1 << 8);
304 acx565akm_write(md, MIPID_CMD_WRITE_DISP_BRIGHTNESS, (u8 *)&bv, 2);
307 enable_backlight_ctrl(md, 1);
309 enable_backlight_ctrl(md, 0);
312 static int acx565akm_get_actual_brightness(struct acx565akm_device *md)
316 acx565akm_read(md, MIPID_CMD_READ_DISP_BRIGHTNESS, &bv, 1);
322 static int acx565akm_bl_update_status(struct backlight_device *dev)
324 struct acx565akm_device *md = dev_get_drvdata(&dev->dev);
328 dev_dbg(&md->spi->dev, "%s\n", __func__);
330 mutex_lock(&md->mutex);
332 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
333 dev->props.power == FB_BLANK_UNBLANK)
334 level = dev->props.brightness;
340 acx565akm_set_brightness(md, level);
344 mutex_unlock(&md->mutex);
349 static int acx565akm_bl_get_intensity(struct backlight_device *dev)
351 struct acx565akm_device *md = dev_get_drvdata(&dev->dev);
353 dev_dbg(&dev->dev, "%s\n", __func__);
358 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
359 dev->props.power == FB_BLANK_UNBLANK) {
361 return acx565akm_get_actual_brightness(md);
363 return dev->props.brightness;
369 static const struct backlight_ops acx565akm_bl_ops = {
370 .get_brightness = acx565akm_bl_get_intensity,
371 .update_status = acx565akm_bl_update_status,
374 /*--------------------Auto Brightness control via Sysfs---------------------*/
376 static const char *cabc_modes[] = {
377 "off", /* always used when CABC is not supported */
383 static ssize_t show_cabc_mode(struct device *dev,
384 struct device_attribute *attr,
387 struct acx565akm_device *md = dev_get_drvdata(dev);
388 const char *mode_str;
395 mode = get_cabc_mode(md);
396 mode_str = "unknown";
397 if (mode >= 0 && mode < ARRAY_SIZE(cabc_modes))
398 mode_str = cabc_modes[mode];
399 len = snprintf(buf, PAGE_SIZE, "%s\n", mode_str);
401 return len < PAGE_SIZE - 1 ? len : PAGE_SIZE - 1;
404 static ssize_t store_cabc_mode(struct device *dev,
405 struct device_attribute *attr,
406 const char *buf, size_t count)
408 struct acx565akm_device *md = dev_get_drvdata(dev);
411 for (i = 0; i < ARRAY_SIZE(cabc_modes); i++) {
412 const char *mode_str = cabc_modes[i];
413 int cmp_len = strlen(mode_str);
415 if (count > 0 && buf[count - 1] == '\n')
417 if (count != cmp_len)
420 if (strncmp(buf, mode_str, cmp_len) == 0)
424 if (i == ARRAY_SIZE(cabc_modes))
427 if (!md->has_cabc && i != 0)
430 mutex_lock(&md->mutex);
431 set_cabc_mode(md, i);
432 mutex_unlock(&md->mutex);
437 static ssize_t show_cabc_available_modes(struct device *dev,
438 struct device_attribute *attr,
441 struct acx565akm_device *md = dev_get_drvdata(dev);
446 return snprintf(buf, PAGE_SIZE, "%s\n", cabc_modes[0]);
449 len < PAGE_SIZE && i < ARRAY_SIZE(cabc_modes); i++)
450 len += snprintf(&buf[len], PAGE_SIZE - len, "%s%s%s",
451 i ? " " : "", cabc_modes[i],
452 i == ARRAY_SIZE(cabc_modes) - 1 ? "\n" : "");
454 return len < PAGE_SIZE ? len : PAGE_SIZE - 1;
457 static DEVICE_ATTR(cabc_mode, S_IRUGO | S_IWUSR,
458 show_cabc_mode, store_cabc_mode);
459 static DEVICE_ATTR(cabc_available_modes, S_IRUGO,
460 show_cabc_available_modes, NULL);
462 static struct attribute *bldev_attrs[] = {
463 &dev_attr_cabc_mode.attr,
464 &dev_attr_cabc_available_modes.attr,
468 static struct attribute_group bldev_attr_group = {
469 .attrs = bldev_attrs,
473 /*---------------------------ACX Panel----------------------------*/
475 static int acx_get_recommended_bpp(struct omap_dss_device *dssdev)
480 static struct omap_video_timings acx_panel_timings = {
483 .pixel_clock = 24000,
491 .vsync_level = OMAPDSS_SIG_ACTIVE_LOW,
492 .hsync_level = OMAPDSS_SIG_ACTIVE_LOW,
494 .data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE,
495 .de_level = OMAPDSS_SIG_ACTIVE_HIGH,
496 .sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES,
499 static struct panel_acx565akm_data *get_panel_data(struct omap_dss_device *dssdev)
501 return (struct panel_acx565akm_data *) dssdev->data;
504 static int acx_panel_probe(struct omap_dss_device *dssdev)
507 struct acx565akm_device *md = &acx_dev;
508 struct panel_acx565akm_data *panel_data = get_panel_data(dssdev);
509 struct backlight_device *bldev;
510 int max_brightness, brightness;
511 struct backlight_properties props;
513 dev_dbg(dssdev->dev, "%s\n", __func__);
518 /* FIXME AC bias ? */
519 dssdev->panel.timings = acx_panel_timings;
521 if (gpio_is_valid(panel_data->reset_gpio)) {
522 r = devm_gpio_request_one(dssdev->dev, panel_data->reset_gpio,
523 GPIOF_OUT_INIT_LOW, "lcd reset");
528 if (gpio_is_valid(panel_data->reset_gpio))
529 gpio_set_value(panel_data->reset_gpio, 1);
532 * After reset we have to wait 5 msec before the first
533 * command can be sent.
537 md->enabled = panel_enabled(md);
539 r = panel_detect(md);
541 dev_err(dssdev->dev, "%s panel detect error\n", __func__);
542 if (!md->enabled && gpio_is_valid(panel_data->reset_gpio))
543 gpio_set_value(panel_data->reset_gpio, 0);
548 mutex_lock(&acx_dev.mutex);
549 acx_dev.dssdev = dssdev;
550 mutex_unlock(&acx_dev.mutex);
553 if (gpio_is_valid(panel_data->reset_gpio))
554 gpio_set_value(panel_data->reset_gpio, 0);
557 /*------- Backlight control --------*/
559 memset(&props, 0, sizeof(props));
560 props.fb_blank = FB_BLANK_UNBLANK;
561 props.power = FB_BLANK_UNBLANK;
562 props.type = BACKLIGHT_RAW;
564 bldev = backlight_device_register("acx565akm", &md->spi->dev,
565 md, &acx565akm_bl_ops, &props);
568 r = sysfs_create_group(&bldev->dev.kobj, &bldev_attr_group);
571 "%s failed to create sysfs files\n", __func__);
572 backlight_device_unregister(bldev);
575 md->cabc_mode = get_hw_cabc_mode(md);
578 max_brightness = 255;
581 brightness = acx565akm_get_actual_brightness(md);
585 bldev->props.max_brightness = max_brightness;
586 bldev->props.brightness = brightness;
588 acx565akm_bl_update_status(bldev);
592 static void acx_panel_remove(struct omap_dss_device *dssdev)
594 struct acx565akm_device *md = &acx_dev;
596 dev_dbg(dssdev->dev, "%s\n", __func__);
597 sysfs_remove_group(&md->bl_dev->dev.kobj, &bldev_attr_group);
598 backlight_device_unregister(md->bl_dev);
599 mutex_lock(&acx_dev.mutex);
600 acx_dev.dssdev = NULL;
601 mutex_unlock(&acx_dev.mutex);
604 static int acx_panel_power_on(struct omap_dss_device *dssdev)
606 struct acx565akm_device *md = &acx_dev;
607 struct panel_acx565akm_data *panel_data = get_panel_data(dssdev);
610 dev_dbg(dssdev->dev, "%s\n", __func__);
612 if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
615 mutex_lock(&md->mutex);
617 omapdss_sdi_set_timings(dssdev, &dssdev->panel.timings);
618 omapdss_sdi_set_datapairs(dssdev, dssdev->phy.sdi.datapairs);
620 r = omapdss_sdi_display_enable(dssdev);
622 pr_err("%s sdi enable failed\n", __func__);
629 if (gpio_is_valid(panel_data->reset_gpio))
630 gpio_set_value(panel_data->reset_gpio, 1);
633 dev_dbg(&md->spi->dev, "panel already enabled\n");
634 mutex_unlock(&md->mutex);
639 * We have to meet all the following delay requirements:
640 * 1. tRW: reset pulse width 10usec (7.12.1)
641 * 2. tRT: reset cancel time 5msec (7.12.1)
642 * 3. Providing PCLK,HS,VS signals for 2 frames = ~50msec worst
644 * 4. 120msec before the sleep out command (7.12.1)
648 set_sleep_mode(md, 0);
651 /* 5msec between sleep out and the next command. (8.2.16) */
653 set_display_state(md, 1);
654 set_cabc_mode(md, md->cabc_mode);
656 mutex_unlock(&md->mutex);
658 return acx565akm_bl_update_status(md->bl_dev);
661 mutex_unlock(&md->mutex);
665 static void acx_panel_power_off(struct omap_dss_device *dssdev)
667 struct acx565akm_device *md = &acx_dev;
668 struct panel_acx565akm_data *panel_data = get_panel_data(dssdev);
670 dev_dbg(dssdev->dev, "%s\n", __func__);
672 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
675 mutex_lock(&md->mutex);
678 mutex_unlock(&md->mutex);
681 set_display_state(md, 0);
682 set_sleep_mode(md, 1);
685 * We have to provide PCLK,HS,VS signals for 2 frames (worst case
686 * ~50msec) after sending the sleep in command and asserting the
687 * reset signal. We probably could assert the reset w/o the delay
688 * but we still delay to avoid possible artifacts. (7.6.1)
692 if (gpio_is_valid(panel_data->reset_gpio))
693 gpio_set_value(panel_data->reset_gpio, 0);
695 /* FIXME need to tweak this delay */
698 omapdss_sdi_display_disable(dssdev);
700 mutex_unlock(&md->mutex);
703 static int acx_panel_enable(struct omap_dss_device *dssdev)
707 dev_dbg(dssdev->dev, "%s\n", __func__);
708 r = acx_panel_power_on(dssdev);
713 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
717 static void acx_panel_disable(struct omap_dss_device *dssdev)
719 dev_dbg(dssdev->dev, "%s\n", __func__);
720 acx_panel_power_off(dssdev);
721 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
724 static void acx_panel_set_timings(struct omap_dss_device *dssdev,
725 struct omap_video_timings *timings)
727 omapdss_sdi_set_timings(dssdev, timings);
729 dssdev->panel.timings = *timings;
732 static int acx_panel_check_timings(struct omap_dss_device *dssdev,
733 struct omap_video_timings *timings)
739 static struct omap_dss_driver acx_panel_driver = {
740 .probe = acx_panel_probe,
741 .remove = acx_panel_remove,
743 .enable = acx_panel_enable,
744 .disable = acx_panel_disable,
746 .set_timings = acx_panel_set_timings,
747 .check_timings = acx_panel_check_timings,
749 .get_recommended_bpp = acx_get_recommended_bpp,
752 .name = "panel-acx565akm",
753 .owner = THIS_MODULE,
757 /*--------------------SPI probe-------------------------*/
759 static int acx565akm_spi_probe(struct spi_device *spi)
761 struct acx565akm_device *md = &acx_dev;
763 dev_dbg(&spi->dev, "%s\n", __func__);
765 spi->mode = SPI_MODE_3;
767 mutex_init(&md->mutex);
768 dev_set_drvdata(&spi->dev, md);
770 omap_dss_register_driver(&acx_panel_driver);
775 static int acx565akm_spi_remove(struct spi_device *spi)
777 struct acx565akm_device *md = dev_get_drvdata(&spi->dev);
779 dev_dbg(&md->spi->dev, "%s\n", __func__);
780 omap_dss_unregister_driver(&acx_panel_driver);
785 static struct spi_driver acx565akm_spi_driver = {
788 .owner = THIS_MODULE,
790 .probe = acx565akm_spi_probe,
791 .remove = acx565akm_spi_remove,
794 module_spi_driver(acx565akm_spi_driver);
796 MODULE_AUTHOR("Nokia Corporation");
797 MODULE_DESCRIPTION("acx565akm LCD Driver");
798 MODULE_LICENSE("GPL");