2 * Sony ACX565AKM LCD Panel driver
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>
31 #include <linux/gpio/consumer.h>
33 #include <linux/of_gpio.h>
35 #include "../dss/omapdss.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 panel_drv_data {
66 struct omap_dss_device dssdev;
67 struct omap_dss_device *in;
81 unsigned long hw_guard_end; /* next value of jiffies
83 next sleep in/out command */
84 unsigned long hw_guard_wait; /* max guard time in jiffies */
86 struct spi_device *spi;
89 struct backlight_device *bl_dev;
92 static const struct videomode acx565akm_panel_vm = {
95 .pixelclock = 24000000,
103 .flags = DISPLAY_FLAGS_HSYNC_LOW | DISPLAY_FLAGS_VSYNC_LOW |
104 DISPLAY_FLAGS_DE_HIGH | DISPLAY_FLAGS_SYNC_NEGEDGE |
105 DISPLAY_FLAGS_PIXDATA_POSEDGE,
108 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
110 static void acx565akm_transfer(struct panel_drv_data *ddata, int cmd,
111 const u8 *wbuf, int wlen, u8 *rbuf, int rlen)
113 struct spi_message m;
114 struct spi_transfer *x, xfer[5];
117 BUG_ON(ddata->spi == NULL);
119 spi_message_init(&m);
121 memset(xfer, 0, sizeof(xfer));
126 x->bits_per_word = 9;
129 if (rlen > 1 && wlen == 0) {
131 * Between the command and the response data there is a
132 * dummy clock cycle. Add an extra bit after the command
133 * word to account for this.
135 x->bits_per_word = 10;
138 spi_message_add_tail(x, &m);
144 x->bits_per_word = 9;
145 spi_message_add_tail(x, &m);
152 spi_message_add_tail(x, &m);
155 r = spi_sync(ddata->spi, &m);
157 dev_dbg(&ddata->spi->dev, "spi_sync %d\n", r);
160 static inline void acx565akm_cmd(struct panel_drv_data *ddata, int cmd)
162 acx565akm_transfer(ddata, cmd, NULL, 0, NULL, 0);
165 static inline void acx565akm_write(struct panel_drv_data *ddata,
166 int reg, const u8 *buf, int len)
168 acx565akm_transfer(ddata, reg, buf, len, NULL, 0);
171 static inline void acx565akm_read(struct panel_drv_data *ddata,
172 int reg, u8 *buf, int len)
174 acx565akm_transfer(ddata, reg, NULL, 0, buf, len);
177 static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec)
179 ddata->hw_guard_wait = msecs_to_jiffies(guard_msec);
180 ddata->hw_guard_end = jiffies + ddata->hw_guard_wait;
183 static void hw_guard_wait(struct panel_drv_data *ddata)
185 unsigned long wait = ddata->hw_guard_end - jiffies;
187 if ((long)wait > 0 && wait <= ddata->hw_guard_wait) {
188 set_current_state(TASK_UNINTERRUPTIBLE);
189 schedule_timeout(wait);
193 static void set_sleep_mode(struct panel_drv_data *ddata, int on)
198 cmd = MIPID_CMD_SLEEP_IN;
200 cmd = MIPID_CMD_SLEEP_OUT;
202 * We have to keep 120msec between sleep in/out commands.
205 hw_guard_wait(ddata);
206 acx565akm_cmd(ddata, cmd);
207 hw_guard_start(ddata, 120);
210 static void set_display_state(struct panel_drv_data *ddata, int enabled)
212 int cmd = enabled ? MIPID_CMD_DISP_ON : MIPID_CMD_DISP_OFF;
214 acx565akm_cmd(ddata, cmd);
217 static int panel_enabled(struct panel_drv_data *ddata)
222 acx565akm_read(ddata, MIPID_CMD_READ_DISP_STATUS,
223 (u8 *)&disp_status, 4);
224 disp_status = __be32_to_cpu(disp_status);
225 enabled = (disp_status & (1 << 17)) && (disp_status & (1 << 10));
226 dev_dbg(&ddata->spi->dev,
227 "LCD panel %senabled by bootloader (status 0x%04x)\n",
228 enabled ? "" : "not ", disp_status);
232 static int panel_detect(struct panel_drv_data *ddata)
234 acx565akm_read(ddata, MIPID_CMD_READ_DISP_ID, ddata->display_id, 3);
235 dev_dbg(&ddata->spi->dev, "MIPI display ID: %02x%02x%02x\n",
236 ddata->display_id[0],
237 ddata->display_id[1],
238 ddata->display_id[2]);
240 switch (ddata->display_id[0]) {
242 ddata->model = MIPID_VER_ACX565AKM;
243 ddata->name = "acx565akm";
248 ddata->model = MIPID_VER_L4F00311;
249 ddata->name = "l4f00311";
252 ddata->model = MIPID_VER_LPH8923;
253 ddata->name = "lph8923";
256 ddata->model = MIPID_VER_LS041Y3;
257 ddata->name = "ls041y3";
260 ddata->name = "unknown";
261 dev_err(&ddata->spi->dev, "invalid display ID\n");
265 ddata->revision = ddata->display_id[1];
267 dev_info(&ddata->spi->dev, "omapfb: %s rev %02x LCD detected\n",
268 ddata->name, ddata->revision);
273 /*----------------------Backlight Control-------------------------*/
275 static void enable_backlight_ctrl(struct panel_drv_data *ddata, int enable)
279 acx565akm_read(ddata, MIPID_CMD_READ_CTRL_DISP, (u8 *)&ctrl, 1);
281 ctrl |= CTRL_DISP_BRIGHTNESS_CTRL_ON |
282 CTRL_DISP_BACKLIGHT_ON;
284 ctrl &= ~(CTRL_DISP_BRIGHTNESS_CTRL_ON |
285 CTRL_DISP_BACKLIGHT_ON);
289 acx565akm_write(ddata, MIPID_CMD_WRITE_CTRL_DISP, (u8 *)&ctrl, 2);
292 static void set_cabc_mode(struct panel_drv_data *ddata, unsigned mode)
296 ddata->cabc_mode = mode;
300 acx565akm_read(ddata, MIPID_CMD_READ_CABC, (u8 *)&cabc_ctrl, 1);
302 cabc_ctrl |= (1 << 8) | (mode & 3);
303 acx565akm_write(ddata, MIPID_CMD_WRITE_CABC, (u8 *)&cabc_ctrl, 2);
306 static unsigned get_cabc_mode(struct panel_drv_data *ddata)
308 return ddata->cabc_mode;
311 static unsigned get_hw_cabc_mode(struct panel_drv_data *ddata)
315 acx565akm_read(ddata, MIPID_CMD_READ_CABC, &cabc_ctrl, 1);
316 return cabc_ctrl & 3;
319 static void acx565akm_set_brightness(struct panel_drv_data *ddata, int level)
323 bv = level | (1 << 8);
324 acx565akm_write(ddata, MIPID_CMD_WRITE_DISP_BRIGHTNESS, (u8 *)&bv, 2);
327 enable_backlight_ctrl(ddata, 1);
329 enable_backlight_ctrl(ddata, 0);
332 static int acx565akm_get_actual_brightness(struct panel_drv_data *ddata)
336 acx565akm_read(ddata, MIPID_CMD_READ_DISP_BRIGHTNESS, &bv, 1);
342 static int acx565akm_bl_update_status(struct backlight_device *dev)
344 struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
347 dev_dbg(&ddata->spi->dev, "%s\n", __func__);
349 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
350 dev->props.power == FB_BLANK_UNBLANK)
351 level = dev->props.brightness;
356 acx565akm_set_brightness(ddata, level);
363 static int acx565akm_bl_get_intensity(struct backlight_device *dev)
365 struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
367 dev_dbg(&dev->dev, "%s\n", __func__);
372 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
373 dev->props.power == FB_BLANK_UNBLANK) {
375 return acx565akm_get_actual_brightness(ddata);
377 return dev->props.brightness;
383 static int acx565akm_bl_update_status_locked(struct backlight_device *dev)
385 struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
388 mutex_lock(&ddata->mutex);
389 r = acx565akm_bl_update_status(dev);
390 mutex_unlock(&ddata->mutex);
395 static int acx565akm_bl_get_intensity_locked(struct backlight_device *dev)
397 struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
400 mutex_lock(&ddata->mutex);
401 r = acx565akm_bl_get_intensity(dev);
402 mutex_unlock(&ddata->mutex);
407 static const struct backlight_ops acx565akm_bl_ops = {
408 .get_brightness = acx565akm_bl_get_intensity_locked,
409 .update_status = acx565akm_bl_update_status_locked,
412 /*--------------------Auto Brightness control via Sysfs---------------------*/
414 static const char * const cabc_modes[] = {
415 "off", /* always used when CABC is not supported */
421 static ssize_t show_cabc_mode(struct device *dev,
422 struct device_attribute *attr,
425 struct panel_drv_data *ddata = dev_get_drvdata(dev);
426 const char *mode_str;
430 if (!ddata->has_cabc)
433 mode = get_cabc_mode(ddata);
434 mode_str = "unknown";
435 if (mode >= 0 && mode < ARRAY_SIZE(cabc_modes))
436 mode_str = cabc_modes[mode];
437 len = snprintf(buf, PAGE_SIZE, "%s\n", mode_str);
439 return len < PAGE_SIZE - 1 ? len : PAGE_SIZE - 1;
442 static ssize_t store_cabc_mode(struct device *dev,
443 struct device_attribute *attr,
444 const char *buf, size_t count)
446 struct panel_drv_data *ddata = dev_get_drvdata(dev);
449 for (i = 0; i < ARRAY_SIZE(cabc_modes); i++) {
450 const char *mode_str = cabc_modes[i];
451 int cmp_len = strlen(mode_str);
453 if (count > 0 && buf[count - 1] == '\n')
455 if (count != cmp_len)
458 if (strncmp(buf, mode_str, cmp_len) == 0)
462 if (i == ARRAY_SIZE(cabc_modes))
465 if (!ddata->has_cabc && i != 0)
468 mutex_lock(&ddata->mutex);
469 set_cabc_mode(ddata, i);
470 mutex_unlock(&ddata->mutex);
475 static ssize_t show_cabc_available_modes(struct device *dev,
476 struct device_attribute *attr,
479 struct panel_drv_data *ddata = dev_get_drvdata(dev);
483 if (!ddata->has_cabc)
484 return snprintf(buf, PAGE_SIZE, "%s\n", cabc_modes[0]);
487 len < PAGE_SIZE && i < ARRAY_SIZE(cabc_modes); i++)
488 len += snprintf(&buf[len], PAGE_SIZE - len, "%s%s%s",
489 i ? " " : "", cabc_modes[i],
490 i == ARRAY_SIZE(cabc_modes) - 1 ? "\n" : "");
492 return len < PAGE_SIZE ? len : PAGE_SIZE - 1;
495 static DEVICE_ATTR(cabc_mode, S_IRUGO | S_IWUSR,
496 show_cabc_mode, store_cabc_mode);
497 static DEVICE_ATTR(cabc_available_modes, S_IRUGO,
498 show_cabc_available_modes, NULL);
500 static struct attribute *bldev_attrs[] = {
501 &dev_attr_cabc_mode.attr,
502 &dev_attr_cabc_available_modes.attr,
506 static const struct attribute_group bldev_attr_group = {
507 .attrs = bldev_attrs,
510 static int acx565akm_connect(struct omap_dss_device *dssdev)
512 struct panel_drv_data *ddata = to_panel_data(dssdev);
513 struct omap_dss_device *in = ddata->in;
516 if (omapdss_device_is_connected(dssdev))
519 r = in->ops.sdi->connect(in, dssdev);
526 static void acx565akm_disconnect(struct omap_dss_device *dssdev)
528 struct panel_drv_data *ddata = to_panel_data(dssdev);
529 struct omap_dss_device *in = ddata->in;
531 if (!omapdss_device_is_connected(dssdev))
534 in->ops.sdi->disconnect(in, dssdev);
537 static int acx565akm_panel_power_on(struct omap_dss_device *dssdev)
539 struct panel_drv_data *ddata = to_panel_data(dssdev);
540 struct omap_dss_device *in = ddata->in;
543 dev_dbg(&ddata->spi->dev, "%s\n", __func__);
545 in->ops.sdi->set_timings(in, &ddata->vm);
547 r = in->ops.sdi->enable(in);
549 pr_err("%s sdi enable failed\n", __func__);
556 if (gpio_is_valid(ddata->reset_gpio))
557 gpio_set_value(ddata->reset_gpio, 1);
559 if (ddata->enabled) {
560 dev_dbg(&ddata->spi->dev, "panel already enabled\n");
565 * We have to meet all the following delay requirements:
566 * 1. tRW: reset pulse width 10usec (7.12.1)
567 * 2. tRT: reset cancel time 5msec (7.12.1)
568 * 3. Providing PCLK,HS,VS signals for 2 frames = ~50msec worst
570 * 4. 120msec before the sleep out command (7.12.1)
574 set_sleep_mode(ddata, 0);
577 /* 5msec between sleep out and the next command. (8.2.16) */
578 usleep_range(5000, 10000);
579 set_display_state(ddata, 1);
580 set_cabc_mode(ddata, ddata->cabc_mode);
582 return acx565akm_bl_update_status(ddata->bl_dev);
585 static void acx565akm_panel_power_off(struct omap_dss_device *dssdev)
587 struct panel_drv_data *ddata = to_panel_data(dssdev);
588 struct omap_dss_device *in = ddata->in;
590 dev_dbg(dssdev->dev, "%s\n", __func__);
595 set_display_state(ddata, 0);
596 set_sleep_mode(ddata, 1);
599 * We have to provide PCLK,HS,VS signals for 2 frames (worst case
600 * ~50msec) after sending the sleep in command and asserting the
601 * reset signal. We probably could assert the reset w/o the delay
602 * but we still delay to avoid possible artifacts. (7.6.1)
606 if (gpio_is_valid(ddata->reset_gpio))
607 gpio_set_value(ddata->reset_gpio, 0);
609 /* FIXME need to tweak this delay */
612 in->ops.sdi->disable(in);
615 static int acx565akm_enable(struct omap_dss_device *dssdev)
617 struct panel_drv_data *ddata = to_panel_data(dssdev);
620 dev_dbg(dssdev->dev, "%s\n", __func__);
622 if (!omapdss_device_is_connected(dssdev))
625 if (omapdss_device_is_enabled(dssdev))
628 mutex_lock(&ddata->mutex);
629 r = acx565akm_panel_power_on(dssdev);
630 mutex_unlock(&ddata->mutex);
634 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
639 static void acx565akm_disable(struct omap_dss_device *dssdev)
641 struct panel_drv_data *ddata = to_panel_data(dssdev);
643 dev_dbg(dssdev->dev, "%s\n", __func__);
645 if (!omapdss_device_is_enabled(dssdev))
648 mutex_lock(&ddata->mutex);
649 acx565akm_panel_power_off(dssdev);
650 mutex_unlock(&ddata->mutex);
652 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
655 static void acx565akm_set_timings(struct omap_dss_device *dssdev,
656 struct videomode *vm)
658 struct panel_drv_data *ddata = to_panel_data(dssdev);
659 struct omap_dss_device *in = ddata->in;
662 dssdev->panel.vm = *vm;
664 in->ops.sdi->set_timings(in, vm);
667 static void acx565akm_get_timings(struct omap_dss_device *dssdev,
668 struct videomode *vm)
670 struct panel_drv_data *ddata = to_panel_data(dssdev);
675 static int acx565akm_check_timings(struct omap_dss_device *dssdev,
676 struct videomode *vm)
678 struct panel_drv_data *ddata = to_panel_data(dssdev);
679 struct omap_dss_device *in = ddata->in;
681 return in->ops.sdi->check_timings(in, vm);
684 static struct omap_dss_driver acx565akm_ops = {
685 .connect = acx565akm_connect,
686 .disconnect = acx565akm_disconnect,
688 .enable = acx565akm_enable,
689 .disable = acx565akm_disable,
691 .set_timings = acx565akm_set_timings,
692 .get_timings = acx565akm_get_timings,
693 .check_timings = acx565akm_check_timings,
696 static int acx565akm_probe_of(struct spi_device *spi)
698 struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
699 struct device_node *np = spi->dev.of_node;
701 ddata->reset_gpio = of_get_named_gpio(np, "reset-gpios", 0);
703 ddata->in = omapdss_of_find_source_for_first_ep(np);
704 if (IS_ERR(ddata->in)) {
705 dev_err(&spi->dev, "failed to find video source\n");
706 return PTR_ERR(ddata->in);
712 static int acx565akm_probe(struct spi_device *spi)
714 struct panel_drv_data *ddata;
715 struct omap_dss_device *dssdev;
716 struct backlight_device *bldev;
717 int max_brightness, brightness;
718 struct backlight_properties props;
721 dev_dbg(&spi->dev, "%s\n", __func__);
723 if (!spi->dev.of_node)
726 spi->mode = SPI_MODE_3;
728 ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
732 dev_set_drvdata(&spi->dev, ddata);
736 mutex_init(&ddata->mutex);
738 r = acx565akm_probe_of(spi);
742 if (gpio_is_valid(ddata->reset_gpio)) {
743 r = devm_gpio_request_one(&spi->dev, ddata->reset_gpio,
744 GPIOF_OUT_INIT_LOW, "lcd reset");
749 if (gpio_is_valid(ddata->reset_gpio))
750 gpio_set_value(ddata->reset_gpio, 1);
753 * After reset we have to wait 5 msec before the first
754 * command can be sent.
756 usleep_range(5000, 10000);
758 ddata->enabled = panel_enabled(ddata);
760 r = panel_detect(ddata);
762 if (!ddata->enabled && gpio_is_valid(ddata->reset_gpio))
763 gpio_set_value(ddata->reset_gpio, 0);
766 dev_err(&spi->dev, "%s panel detect error\n", __func__);
770 memset(&props, 0, sizeof(props));
771 props.fb_blank = FB_BLANK_UNBLANK;
772 props.power = FB_BLANK_UNBLANK;
773 props.type = BACKLIGHT_RAW;
775 bldev = backlight_device_register("acx565akm", &ddata->spi->dev,
776 ddata, &acx565akm_bl_ops, &props);
781 ddata->bl_dev = bldev;
782 if (ddata->has_cabc) {
783 r = sysfs_create_group(&bldev->dev.kobj, &bldev_attr_group);
786 "%s failed to create sysfs files\n", __func__);
789 ddata->cabc_mode = get_hw_cabc_mode(ddata);
792 max_brightness = 255;
795 brightness = acx565akm_get_actual_brightness(ddata);
799 bldev->props.max_brightness = max_brightness;
800 bldev->props.brightness = brightness;
802 acx565akm_bl_update_status(bldev);
805 ddata->vm = acx565akm_panel_vm;
807 dssdev = &ddata->dssdev;
808 dssdev->dev = &spi->dev;
809 dssdev->driver = &acx565akm_ops;
810 dssdev->type = OMAP_DISPLAY_TYPE_SDI;
811 dssdev->owner = THIS_MODULE;
812 dssdev->panel.vm = ddata->vm;
814 r = omapdss_register_display(dssdev);
816 dev_err(&spi->dev, "Failed to register panel\n");
823 sysfs_remove_group(&bldev->dev.kobj, &bldev_attr_group);
825 backlight_device_unregister(bldev);
829 omap_dss_put_device(ddata->in);
833 static int acx565akm_remove(struct spi_device *spi)
835 struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
836 struct omap_dss_device *dssdev = &ddata->dssdev;
837 struct omap_dss_device *in = ddata->in;
839 dev_dbg(&ddata->spi->dev, "%s\n", __func__);
841 sysfs_remove_group(&ddata->bl_dev->dev.kobj, &bldev_attr_group);
842 backlight_device_unregister(ddata->bl_dev);
844 omapdss_unregister_display(dssdev);
846 acx565akm_disable(dssdev);
847 acx565akm_disconnect(dssdev);
849 omap_dss_put_device(in);
854 static const struct of_device_id acx565akm_of_match[] = {
855 { .compatible = "omapdss,sony,acx565akm", },
858 MODULE_DEVICE_TABLE(of, acx565akm_of_match);
860 static struct spi_driver acx565akm_driver = {
863 .of_match_table = acx565akm_of_match,
864 .suppress_bind_attrs = true,
866 .probe = acx565akm_probe,
867 .remove = acx565akm_remove,
870 module_spi_driver(acx565akm_driver);
872 MODULE_ALIAS("spi:sony,acx565akm");
873 MODULE_AUTHOR("Nokia Corporation");
874 MODULE_DESCRIPTION("acx565akm LCD Driver");
875 MODULE_LICENSE("GPL");