New files diff --git a/include/sound/tda998x.h b/include/sound/tda998x.h new file mode 100644 index 0000000..91a069c --- /dev/null +++ b/include/sound/tda998x.h @@ -0,0 +1,18 @@ +#ifndef SND_TDA998X_H +#define SND_TDA998X_H + +/* port index for audio stream stop */ +#define PORT_NONE (-1) + +struct tda998x_audio { + u8 ports[2]; /* AP value */ + u8 port_types[2]; /* AFMT_xxx */ + u8 *eld; + int (*set_audio_input)(struct device *dev, + int port_index, + unsigned sample_rate); +}; + +int tda9998x_codec_register(struct device *dev); +void tda9998x_codec_unregister(struct device *dev); +#endif diff --git a/drivers/gpu/drm/dove/Kconfig b/drivers/gpu/drm/dove/Kconfig new file mode 100644 index 0000000..a9aa452 --- /dev/null +++ b/drivers/gpu/drm/dove/Kconfig @@ -0,0 +1,10 @@ +config DRM_DOVE + tristate "DRM Support for Marvell Dove" + depends on DRM && MACH_DOVE + depends on OF + select DRM_KMS_HELPER + select DRM_KMS_CMA_HELPER + select DRM_GEM_CMA_HELPER + help + Choose this option if you have a Marvell Dove chipset. + If M is selected the module will be called dove-drm. diff --git a/drivers/gpu/drm/dove/Makefile b/drivers/gpu/drm/dove/Makefile new file mode 100644 index 0000000..a3d700d --- /dev/null +++ b/drivers/gpu/drm/dove/Makefile @@ -0,0 +1,6 @@ +# +# Makefile for Marvell Dove's DRM device driver +# + +dove-drm-objs := dove_drv.o dove_crtc.o +obj-$(CONFIG_DRM_DOVE) += dove-drm.o diff --git a/drivers/gpu/drm/dove/dove_crtc.c b/drivers/gpu/drm/dove/dove_crtc.c new file mode 100644 index 0000000..354626b --- /dev/null +++ b/drivers/gpu/drm/dove/dove_crtc.c @@ -0,0 +1,1393 @@ +/* + * Marvell Dove DRM driver - CRTC + * + * Copyright (C) 2013-2015 + * Jean-Francois Moine + * Sebastian Hesselbarth + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include +#include +#include +#include + +#include "dove_drv.h" +#include "dove_lcd.h" + +//#define LCD_DEBUG 1 + +#define to_dove_lcd(x) container_of(x, struct dove_lcd, crtc) + +static inline void dove_write(struct dove_lcd *dove_lcd, u32 reg, u32 data) +{ + writel_relaxed(data, dove_lcd->mmio + reg); +} +static inline u32 dove_read(struct dove_lcd *dove_lcd, u32 reg) +{ + return readl_relaxed(dove_lcd->mmio + reg); +} +static inline void dove_set(struct dove_lcd *dove_lcd, u32 reg, u32 mask) +{ + dove_write(dove_lcd, reg, dove_read(dove_lcd, reg) | mask); +} +static inline void dove_clear(struct dove_lcd *dove_lcd, u32 reg, u32 mask) +{ + dove_write(dove_lcd, reg, dove_read(dove_lcd, reg) & ~mask); +} + +#ifdef LCD_DEBUG +static ssize_t lcd_read(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t size) +{ + struct dove_lcd *dove_lcd = dev_get_drvdata(dev); + u32 addr, len, a; + u32 val; + unsigned char tmp[4 * 5 + 2], *p; + + if (sscanf(buf, "%x %x", &addr, &len) == 2) { + if ((unsigned) len > 4) + len = 4; + p = tmp; + a = addr; + while (len--) { + val = dove_read(dove_lcd, a); + p += sprintf(p, " %04x", val); + a += 4; + } + pr_info("lcd read %04x:%s\n", addr, tmp); + } else { + if (sscanf(buf, "%x", &addr) != 1) { + pr_info("lcd read use: 'addr' [ 'len' ]\n"); + return size; + } + val = dove_read(dove_lcd, addr); + pr_info("lcd read %04x: %04x\n", addr, val); + } + return size; +} + +static ssize_t lcd_write(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t size) +{ + struct dove_lcd *dove_lcd = dev_get_drvdata(dev); + unsigned int addr, val; + + sscanf(buf, "%x %x", &addr, &val); + + dove_write(dove_lcd, addr, val); + + pr_info("lcd write %04x @ %04x\n", val, addr); + return size; +} +static DEVICE_ATTR(lcd_read, S_IWUSR, NULL, lcd_read); +static DEVICE_ATTR(lcd_write, S_IWUSR, NULL, lcd_write); +#endif + +/* + * vertical blank functions + */ +u32 dove_vblank_count(struct drm_device *drm, int crtc) +{ + struct dove_drm *dove_drm = drm_to_dove(drm); + struct dove_lcd *dove_lcd = dove_drm->lcds[crtc]; + + return STA_GRA_FRAME_COUNT(dove_read(dove_lcd, SPU_IRQ_ISR)); +} + +int dove_enable_vblank(struct drm_device *drm, int crtc) +{ + struct dove_drm *dove_drm = drm_to_dove(drm); + struct dove_lcd *dove_lcd = dove_drm->lcds[crtc]; + +#ifdef HANDLE_INTERLACE + dove_lcd->vblank_enabled = 1; +#endif + dove_set(dove_lcd, SPU_IRQ_ENA, IRQ_GRA_FRAME_DONE); + return 0; +} + +void dove_disable_vblank(struct drm_device *drm, int crtc) +{ + struct dove_drm *dove_drm = drm_to_dove(drm); + struct dove_lcd *dove_lcd = dove_drm->lcds[crtc]; + +#ifdef HANDLE_INTERLACE + dove_lcd->vblank_enabled = 0; + if (!dove_lcd->v_sync0) +#endif + dove_clear(dove_lcd, SPU_IRQ_ENA, IRQ_GRA_FRAME_DONE); +} + +#ifdef CONFIG_DEBUG_FS +static int dove_lcd_regs_show(struct seq_file *m, + struct dove_lcd *dove_lcd) +{ + u32 x, shl, shh, total_v, total_h, active_h, active_v; + u32 orig_buff_x, orig_buff_y, zoomed_x, zoomed_y; + unsigned i; + + seq_printf(m, "\t\t*** LCD %d ***\n", dove_lcd->num); + + /* Get resolution */ + x = dove_read(dove_lcd, LCD_SPU_V_H_ACTIVE); + active_h = H_LCD(x); + active_v = V_LCD(x); + + /* Get total line */ + x = dove_read(dove_lcd, LCD_SPUT_V_H_TOTAL); + total_h = H_LCD(x); + total_v = V_LCD(x); + seq_printf(m, "----total-------------------------<%4dx%4d>" + "-------------------------\n" + "----active--------------|", total_h, total_v); + + /* Get H Timings */ + x = dove_read(dove_lcd, LCD_SPU_H_PORCH); + shl = F_LCD(x); + shh = B_LCD(x); + seq_printf(m, "->front porch(%d)->hsync(%d)->back porch(%d)\n", + shl, total_h - shl -shh - active_h, shh); + + seq_printf(m, "|\t\t\t|\n" + "|\t\t\t|\n" + "|\t<%4dx%4d>\t|\n" + "|\t\t\t|\n" + "|\t\t\t|\n" + "------------------------|\n", active_h, active_v); + + /* Get V Timings */ + x = dove_read(dove_lcd, LCD_SPU_V_PORCH); + shl = F_LCD(x); + shh = B_LCD(x); + seq_printf(m, "|\n|front porch(%d)\n|vsync(%d)\n|back porch(%d)\n", + shl, total_v - shl - shh - active_v, shh); + seq_printf(m, "----------------------------------" + "-----------------------------------\n"); + + /* Get Line Pitch */ + x = dove_read(dove_lcd, LCD_CFG_GRA_PITCH); + shl = x & 0x0000ffff; + seq_printf(m, "gfx line pitch in memory is <%d>\n", shl); + + /* Get scaling info */ + x = dove_read(dove_lcd, LCD_SPU_GRA_HPXL_VLN); + orig_buff_x = H_LCD(x); + orig_buff_y = V_LCD(x); + x = dove_read(dove_lcd, LCD_SPU_GZM_HPXL_VLN); + zoomed_x = H_LCD(x); + zoomed_y = V_LCD(x); + seq_printf(m, "Scaled from <%dx%d> to <%dx%d>\n", + orig_buff_x, orig_buff_y, zoomed_x, zoomed_y); + + seq_printf(m, "======================================\n"); + + for (i = 0x0080; i <= 0x01c4; i += 4) { + x = dove_read(dove_lcd, i); + seq_printf(m, "0x%04x 0x%08x\n", i, x); + } + return 0; +} + +static int dove_regs_show(struct seq_file *m, void *arg) +{ + struct drm_info_node *node = m->private; + struct drm_device *drm = node->minor->dev; + struct dove_drm *dove_drm = drm_to_dove(drm); + struct dove_lcd *dove_lcd; + unsigned i; + + for (i = 0; i < MAX_DOVE_LCD; i++) { + dove_lcd = dove_drm->lcds[i]; + if (dove_lcd) + dove_lcd_regs_show(m, dove_lcd); + } + return 0; +} + +static struct drm_info_list dove_debugfs_list[] = { + { "lcd_regs", dove_regs_show, 0 }, + { "fb", drm_fb_cma_debugfs_show, 0 }, +}; + +int dove_debugfs_init(struct drm_minor *minor) +{ + struct drm_device *dev = minor->dev; + int ret; + + DRM_DEBUG_DRIVER("\n"); + + ret = drm_debugfs_create_files(dove_debugfs_list, + ARRAY_SIZE(dove_debugfs_list), + minor->debugfs_root, minor); + if (ret) + dev_err(dev->dev, "could not install dove_debugfs_list\n"); + + return ret; +} + +void dove_debugfs_cleanup(struct drm_minor *minor) +{ + drm_debugfs_remove_files(dove_debugfs_list, + ARRAY_SIZE(dove_debugfs_list), minor); +} +#endif + +static void dove_update_base(struct dove_lcd *dove_lcd) +{ + struct drm_crtc *crtc = &dove_lcd->crtc; + struct drm_framebuffer *fb = crtc->primary->fb; + struct drm_gem_cma_object *gem; + unsigned int depth, bpp; + dma_addr_t start; + + drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp); + gem = drm_fb_cma_get_gem_obj(fb, 0); + start = gem->paddr + fb->offsets[0] + + crtc->y * fb->pitches[0] + + crtc->x * bpp / 8; + + dove_write(dove_lcd, LCD_CFG_GRA_START_ADDR0, start); +#ifdef HANDLE_INTERLACE + if (dove_lcd->crtc.mode.mode->flags & DRM_MODE_FLAG_INTERLACE) { + dove_write(dove_lcd, LCD_CFG_GRA_START_ADDR1, + start + fb->pitches[0]); + dove_write(dove_lcd, LCD_CFG_GRA_PITCH, fb->pitches[0] * 2); + return; + } +#endif + dove_write(dove_lcd, LCD_CFG_GRA_START_ADDR1, start); + dove_write(dove_lcd, LCD_CFG_GRA_PITCH, fb->pitches[0]); +} + +static void set_frame_timings(struct dove_lcd *dove_lcd) +{ + struct drm_crtc *crtc = &dove_lcd->crtc; + const struct drm_display_mode *mode = &crtc->mode; + u32 h_active, v_active, h_orig, v_orig, h_zoom, v_zoom; + u32 hfp, hbp, vfp, vbp, hs, vs, v_total; + u32 x; + + /* + * Calc active size, zoomed size, porch. + */ + h_active = h_zoom = mode->hdisplay; + v_active = v_zoom = mode->vdisplay; + hfp = mode->hsync_start - mode->hdisplay; + hbp = mode->htotal - mode->hsync_end; + vfp = mode->vsync_start - mode->vdisplay; + vbp = mode->vtotal - mode->vsync_end; + hs = mode->hsync_end - mode->hsync_start; + vs = mode->vsync_end - mode->vsync_start; + + /* + * Calc original size. + */ + h_orig = h_active; + v_orig = v_active; + +#ifdef HANDLE_INTERLACE + /* interlaced workaround */ + if (mode->flags & DRM_MODE_FLAG_INTERLACE) { + v_active /= 2; + v_zoom /= 2; + v_orig /= 2; + } +#endif + + /* calc total width and height */ + v_total = v_active + vfp + vs + vbp; + + /* apply setting to registers */ + dove_write(dove_lcd, LCD_SPU_V_H_ACTIVE, LCD_H_V(h_active, v_active)); + dove_write(dove_lcd, LCD_SPU_GRA_HPXL_VLN, LCD_H_V(h_orig, v_orig)); + dove_write(dove_lcd, LCD_SPU_GZM_HPXL_VLN, LCD_H_V(h_zoom, v_zoom)); + dove_write(dove_lcd, LCD_SPU_H_PORCH, LCD_F_B(hfp, hbp)); + dove_write(dove_lcd, LCD_SPU_V_PORCH, LCD_F_B(vfp, vbp)); + dove_write(dove_lcd, LCD_SPUT_V_H_TOTAL, + LCD_H_V(mode->htotal, v_total)); + + /* configure vsync adjust logic */ + x = dove_read(dove_lcd, LCD_TV_CONTROL1); + x &= ~(VSYNC_L_OFFSET_MASK | VSYNC_H_OFFSET_MASK); + x |= VSYNC_OFFSET_EN | /* VSYNC adjust enable */ + VSYNC_L_OFFSET(h_active + hfp) | + VSYNC_H_OFFSET(h_active + hfp); +#ifdef HANDLE_INTERLACE + if (mode->flags & DRM_MODE_FLAG_INTERLACE) { + dove_lcd->v_sync0 = VSYNC_L_OFFSET(h_active + hfp) | + VSYNC_H_OFFSET(h_active + hfp); + dove_lcd->v_sync1 = VSYNC_L_OFFSET(h_active / 2 + hfp) | + VSYNC_H_OFFSET(h_active / 2 + hfp); + } else { + dove_lcd->v_sync0 = 0; + } +#endif + dove_write(dove_lcd, LCD_TV_CONTROL1, x); +} + +static void dove_set_clock(struct dove_lcd *dove_lcd) +{ + struct drm_crtc *crtc = &dove_lcd->crtc; + const struct drm_display_mode *mode = &crtc->mode; + struct clk *clk; + u32 x, needed_pixclk, ref_clk, div, fract; + int clk_src; + + fract = 0; + needed_pixclk = mode->clock * 1000; +#ifdef HANDLE_INTERLACE + if (mode->flags & DRM_MODE_FLAG_INTERLACE) + needed_pixclk /= 2; +#endif + + /* first check if pixclk is multiple of current clock */ + clk_src = dove_lcd->clk_src; + clk = dove_lcd->clk; +// ref_clk = clk_get_rate(clk); + ref_clk = clk_round_rate(clk, needed_pixclk); + + DRM_DEBUG_DRIVER("clk src %d rate %u needed %u div %u mod %u\n", + clk_src, ref_clk, needed_pixclk, + ref_clk / needed_pixclk, ref_clk % needed_pixclk); + + if (ref_clk % needed_pixclk == 0) { + div = ref_clk / needed_pixclk; + goto set_clock; + } + + /* try to set current clock to requested pixclk */ +// clk_set_rate(clk, needed_pixclk); +// ref_clk = clk_get_rate(clk); + if (ref_clk == needed_pixclk) { + div = 1; + goto set_clock; + } + + /* use internal divider */ + if (false) { +/*fixme: does not work*/ + ref_clk /= 1000; + needed_pixclk /= 1000; + x = (ref_clk * 0x1000 + needed_pixclk - 1) / needed_pixclk; + div = x >> 12; + if (div < 1) + div = 1; + else + fract = x & 0xfff; + } else { + div = (ref_clk + needed_pixclk - 1) / needed_pixclk; + if (div < 1) + div = 1; + } + +set_clock: + clk_set_rate(clk, ref_clk); +// DRM_DEBUG_DRIVER("set clk src %d ref %u div %u fract %u needed %u\n", + pr_info("set clk src %d ref %u div %u fract %u needed %u\n", + clk_src, ref_clk, div, fract, needed_pixclk); + x = SET_SCLK(clk_src, div, fract); + dove_write(dove_lcd, LCD_CFG_SCLK_DIV, x); +} + +static void set_dma_control(struct dove_lcd *dove_lcd) +{ + const struct drm_display_mode *mode = &dove_lcd->crtc.mode; + u32 x; + int fmt, rbswap; + + DRM_DEBUG_DRIVER("fmt %.4s\n", + (char *) dove_lcd->crtc.primary->fb->pixel_format); + + rbswap = 1; /* default */ + switch (dove_lcd->crtc.primary->fb->pixel_format) { + case DRM_FORMAT_BGR565: + rbswap = 0; + case DRM_FORMAT_RGB565: + fmt = GMODE_RGB565; + break; + case DRM_FORMAT_XBGR1555: + rbswap = 0; + case DRM_FORMAT_XRGB1555: + fmt = GMODE_RGB1555; + break; + case DRM_FORMAT_BGR888: + rbswap = 0; + case DRM_FORMAT_RGB888: + fmt = GMODE_RGB888PACKED; + break; + case DRM_FORMAT_XBGR8888: + rbswap = 0; + case DRM_FORMAT_XRGB8888: /* depth 24 */ + fmt = GMODE_RGBA888; + break; + case DRM_FORMAT_ABGR8888: + rbswap = 0; + case DRM_FORMAT_ARGB8888: /* depth 32 */ + fmt = GMODE_RGB888UNPACKED; + break; + case DRM_FORMAT_YVYU: + rbswap = 0; + case DRM_FORMAT_YUYV: + fmt = GMODE_YUV422PACKED; + break; + case DRM_FORMAT_YVU422: + rbswap = 0; + case DRM_FORMAT_YUV422: + fmt = GMODE_YUV422PLANAR; + break; + case DRM_FORMAT_YVU420: + rbswap = 0; + default: +/* case DRM_FORMAT_YUV420: */ + fmt = GMODE_YUV420PLANAR; + break; + } + + x = dove_read(dove_lcd, LCD_SPU_DMA_CTRL0); + x &= ~(CFG_PALETTE_ENA | /* true color */ + CFG_GRAFORMAT_MASK | + CFG_GRA_SWAPRB | + CFG_GRA_FTOGGLE); + x |= CFG_GRA_ENA | /* graphic enable */ + CFG_GRA_HSMOOTH; /* horiz. smooth scaling */ + x |= CFG_GRAFORMAT(fmt); + + if (!rbswap) + x |= CFG_GRA_SWAPRB; +#ifdef HANDLE_INTERLACE + if (mode->flags & DRM_MODE_FLAG_INTERLACE) + x |= CFG_GRA_FTOGGLE; +#endif + dove_write(dove_lcd, LCD_SPU_DMA_CTRL0, x); + + /* + * trigger DMA on the falling edge of vsync if vsync is + * active low, or on the rising edge if vsync is active high + */ + x = dove_read(dove_lcd, LCD_SPU_DMA_CTRL1); + if (mode->flags & DRM_MODE_FLAG_NVSYNC) + x |= CFG_VSYNC_INV; + else + x &= ~CFG_VSYNC_INV; + dove_write(dove_lcd, LCD_SPU_DMA_CTRL1, x); +} + +/* this function is called on mode DRM_MODE_DPMS_ON + * and also at loading time with gpio_only set */ +static void set_dumb_panel_control(struct dove_lcd *dove_lcd, + int gpio_only) +{ + const struct drm_display_mode *mode = &dove_lcd->crtc.mode; + u32 x; + + x = 0; + if (dove_lcd->dpms == DRM_MODE_DPMS_ON) + x = CFG_DUMB_ENA; + if (!gpio_only) { + if (dove_lcd->dpms == DRM_MODE_DPMS_ON) + /* + * When dumb interface isn't under 24bit + * It might be under SPI or GPIO. If set + * to 0x7 will force LCD_D[23:0] output + * blank color and damage GPIO and SPI + * behavior. + */ + x |= CFG_DUMBMODE(DUMB24_RGB888_0); + else + x |= CFG_DUMBMODE(7); + if (mode->flags & DRM_MODE_FLAG_NVSYNC) + x |= CFG_INV_VSYNC; + if (mode->flags & DRM_MODE_FLAG_NHSYNC) + x |= CFG_INV_HSYNC; + } + + dove_write(dove_lcd, LCD_SPU_DUMB_CTRL, x); +} + +void dove_crtc_start(struct dove_lcd *dove_lcd) +{ + struct drm_crtc *crtc = &dove_lcd->crtc; + struct drm_display_mode *mode = &crtc->mode; + + DRM_DEBUG_DRIVER("\n"); + if (mode->clock == 0) { + dev_err(dove_lcd->dev, "crtc_start: no clock!\n"); + dove_lcd->dpms = DRM_MODE_DPMS_OFF; + return; + } + + set_frame_timings(dove_lcd); + dove_set_clock(dove_lcd); + set_dma_control(dove_lcd); + dove_update_base(dove_lcd); + set_dumb_panel_control(dove_lcd, 0); + +#ifdef HANDLE_INTERLACE + if (dove_lcd->v_sync0) { /* interlace mode on */ + dove_set(dove_lcd, SPU_IRQ_ENA, IRQ_GRA_FRAME_DONE); + } else { /* interlace mode off */ + if (!dove_lcd->vblank_enabled) + dove_clear(dove_lcd, SPU_IRQ_ENA, IRQ_GRA_FRAME_DONE); + } +#endif + + drm_mode_debug_printmodeline(mode); +} + +void dove_crtc_stop(struct dove_lcd *dove_lcd) +{ + DRM_DEBUG_DRIVER("\n"); + + dove_clear(dove_lcd, LCD_SPU_DMA_CTRL0, CFG_GRA_ENA); + dove_clear(dove_lcd, LCD_SPU_DUMB_CTRL, CFG_DUMB_ENA); +#ifdef HANDLE_INTERLACE + if (dove_lcd->v_sync0 + && !dove_lcd->vblank_enabled) + dove_clear(dove_lcd, SPU_IRQ_ENA, IRQ_GRA_FRAME_DONE); +#endif +} + +/* ----------------------------------------------------------------------------- + * cursor + */ + +/* load the hardware cursor */ +static int load_cursor(struct dove_lcd *dove_lcd, + struct drm_file *file_priv, + uint32_t handle, + int data_len) +{ + struct dove_drm *dove_drm = dove_lcd->dove_drm; + struct drm_gem_object *obj; + struct drm_gem_cma_object *cma_obj; + u8 *p_pixel; + u32 u, val; + u32 ram, color; + int i, j, ret; + + obj = drm_gem_object_lookup(dove_drm->drm, file_priv, handle); + if (!obj) + return -ENOENT; + + if (!drm_vma_node_has_offset(&obj->vma_node)) { + dev_warn(dove_lcd->dev, "cursor not mapped\n"); + ret = -EINVAL; + goto out; + } + + if (data_len != obj->size) { + dev_warn(dove_lcd->dev, "bad cursor size\n"); + ret = -EINVAL; + goto out; + } + + cma_obj = to_drm_gem_cma_obj(obj); + p_pixel = cma_obj->vaddr; + + u = CFG_SRAM_INIT_WR_RD(SRAMID_INIT_WRITE) | + CFG_SRAM_ADDR_LCDID(SRAMID_HWC); + ram = CFG_SRAM_INIT_WR_RD(SRAMID_INIT_WRITE); + + /* load the RGBA cursor to SRAM */ + for (i = 0; i < data_len / 4 / 4; i++) { + color = (p_pixel[3 * 4 + 0] << 24) | /* red */ + (p_pixel[2 * 4 + 0] << 16) | + (p_pixel[1 * 4 + 0] << 8) | + p_pixel[0]; + dove_write(dove_lcd, LCD_SPU_SRAM_WRDAT, color); + dove_write(dove_lcd, LCD_SPU_SRAM_CTRL, + ram | CFG_SRAM_ADDR_LCDID(SRAMID_HWC32_RAM1)); + color = (p_pixel[3 * 4 + 1] << 24) | /* green */ + (p_pixel[2 * 4 + 1] << 16) | + (p_pixel[1 * 4 + 1] << 8) | + p_pixel[1]; + dove_write(dove_lcd, LCD_SPU_SRAM_WRDAT, color); + dove_write(dove_lcd, LCD_SPU_SRAM_CTRL, + ram | CFG_SRAM_ADDR_LCDID(SRAMID_HWC32_RAM2)); + color = (p_pixel[3 * 4 + 2] << 24) | /* blue */ + (p_pixel[2 * 4 + 2] << 16) | + (p_pixel[1 * 4 + 2] << 8) | + p_pixel[2]; + dove_write(dove_lcd, LCD_SPU_SRAM_WRDAT, color); + dove_write(dove_lcd, LCD_SPU_SRAM_CTRL, + ram | CFG_SRAM_ADDR_LCDID(SRAMID_HWC32_RAM3)); + p_pixel += 4 * 4; + if ((++ram & 0xff) == 0) { + ram -= 0x100; /* I[7:0] */ + ram += 1 << 12; /* J[1:0] */ + } + } + + /* set the transparency */ + p_pixel = cma_obj->vaddr; + for (i = 0; i < data_len / 16 / 4; i++) { + val = 0; + for (j = 16 * 4 - 4; j >= 0 ; j -= 4) { + val <<= 2; + if (p_pixel[j + 3]) /* alpha */ + val |= 1; /* not transparent */ + } + dove_write(dove_lcd, LCD_SPU_SRAM_WRDAT, val); + dove_write(dove_lcd, LCD_SPU_SRAM_CTRL, u++); + p_pixel += 16 * 4; + } + ret = 0; +out: + drm_gem_object_unreference_unlocked(obj); + return ret; +} + +static int dove_cursor_set(struct drm_crtc *crtc, + struct drm_file *file_priv, + uint32_t handle, + uint32_t width, + uint32_t height) +{ + struct dove_lcd *dove_lcd = to_dove_lcd(crtc); + int ret; + + DRM_DEBUG_DRIVER("%dx%d handle %d\n", width, height, handle); + + /* disable cursor */ + dove_clear(dove_lcd, LCD_SPU_DMA_CTRL0, CFG_HWC_ENA); + + if (!handle) + return 0; /* cursor off */ + + if (width != 64 || height != 64) { + dev_err(dove_lcd->dev, "bad cursor size\n"); + return -EINVAL; + } + + /* load the cursor */ + ret = load_cursor(dove_lcd, file_priv, handle, width * height * 4); + if (ret < 0) + return ret; + + /* set cursor size */ + dove_write(dove_lcd, LCD_SPU_HWC_HPXL_VLN, LCD_H_V(width, height)); + + /* enable cursor */ + dove_set(dove_lcd, LCD_SPU_DMA_CTRL0, CFG_HWC_ENA); + + return 0; +} + +static int dove_cursor_move(struct drm_crtc *crtc, + int x, int y) +{ + struct dove_lcd *dove_lcd = to_dove_lcd(crtc); + + if (x < 0) + x = 0; + if (y < 0) + y = 0; + dove_clear(dove_lcd, LCD_SPU_DMA_CTRL0, CFG_HWC_ENA); + dove_write(dove_lcd, LCD_SPU_HWC_OVSA_HPXL_VLN, LCD_H_V(x, y)); + dove_set(dove_lcd, LCD_SPU_DMA_CTRL0, CFG_HWC_ENA); + return 0; +} + +static void dove_crtc_destroy(struct drm_crtc *crtc) +{ + struct dove_lcd *dove_lcd = to_dove_lcd(crtc); + + DRM_DEBUG_DRIVER("\n"); + + WARN_ON(dove_lcd->dpms == DRM_MODE_DPMS_ON); + + drm_crtc_cleanup(crtc); +} + +static int dove_crtc_page_flip(struct drm_crtc *crtc, + struct drm_framebuffer *fb, + struct drm_pending_vblank_event *event, + uint32_t page_flip_flags) +{ + struct dove_lcd *dove_lcd = to_dove_lcd(crtc); + struct drm_device *drm = crtc->dev; + unsigned long flags; + + DRM_DEBUG_DRIVER("\n"); + + spin_lock_irqsave(&drm->event_lock, flags); + if (dove_lcd->event) { + spin_unlock_irqrestore(&drm->event_lock, flags); + dev_err(drm->dev, "already pending page flip!\n"); + return -EBUSY; + } + spin_unlock_irqrestore(&drm->event_lock, flags); + + crtc->primary->fb = fb; + dove_update_base(dove_lcd); + + if (event) { + event->pipe = 0; + spin_lock_irqsave(&drm->event_lock, flags); + dove_lcd->event = event; + spin_unlock_irqrestore(&drm->event_lock, flags); + drm_vblank_get(drm, dove_lcd->num); + } + + return 0; +} + +static void dove_crtc_dpms(struct drm_crtc *crtc, int mode) +{ + struct dove_lcd *dove_lcd = to_dove_lcd(crtc); + + /* we really only care about on or off */ + if (mode != DRM_MODE_DPMS_ON) + mode = DRM_MODE_DPMS_OFF; + + DRM_DEBUG_DRIVER("dpms %s\n", mode == DRM_MODE_DPMS_ON ? "on" : "off"); + + if (dove_lcd->dpms == mode) + return; + + dove_lcd->dpms = mode; + + if (mode == DRM_MODE_DPMS_ON) + dove_crtc_start(dove_lcd); + else + dove_crtc_stop(dove_lcd); +} + +static bool dove_crtc_mode_fixup(struct drm_crtc *crtc, + const struct drm_display_mode *mode, + struct drm_display_mode *adjusted_mode) +{ + DRM_DEBUG_DRIVER("\n"); + if (mode->vrefresh == 0) { +pr_info("dove no vrefresh\n"); +#if 0 +/* drm_mode_set_name(mode); */ + mode->vrefresh = drm_mode_vrefresh(mode); + drm_mode_debug_printmodeline(mode); +#endif + } + return true; +} + +static void dove_crtc_prepare(struct drm_crtc *crtc) +{ + DRM_DEBUG_DRIVER("\n"); + dove_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); +} + +static void dove_crtc_commit(struct drm_crtc *crtc) +{ + DRM_DEBUG_DRIVER("\n"); + dove_crtc_dpms(crtc, DRM_MODE_DPMS_ON); +} + +static int dove_crtc_mode_set(struct drm_crtc *crtc, + struct drm_display_mode *mode, + struct drm_display_mode *adjusted_mode, + int x, int y, + struct drm_framebuffer *old_fb) +{ + DRM_DEBUG_DRIVER("\n"); + + if (mode->hdisplay > 2048) + return MODE_VIRTUAL_X; + + /* width must be multiple of 16 */ + if (mode->hdisplay & 0xf) + return MODE_VIRTUAL_X; + + if (mode->vdisplay > 2048) + return MODE_VIRTUAL_Y; + + return MODE_OK; +} + +static int dove_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y, + struct drm_framebuffer *old_fb) +{ + struct dove_lcd *dove_lcd = to_dove_lcd(crtc); + + DRM_DEBUG_DRIVER("\n"); + dove_update_base(dove_lcd); + return 0; +} + +static const struct drm_crtc_funcs dove_crtc_funcs = { + .cursor_set = dove_cursor_set, + .cursor_move = dove_cursor_move, + .destroy = dove_crtc_destroy, + .set_config = drm_crtc_helper_set_config, + .page_flip = dove_crtc_page_flip, +}; + +static const struct drm_crtc_helper_funcs dove_crtc_helper_funcs = { + .dpms = dove_crtc_dpms, + .mode_fixup = dove_crtc_mode_fixup, + .prepare = dove_crtc_prepare, + .commit = dove_crtc_commit, + .mode_set = dove_crtc_mode_set, + .mode_set_base = dove_crtc_mode_set_base, +}; + +void dove_crtc_cancel_page_flip(struct dove_lcd *dove_lcd, + struct drm_file *file) +{ + struct drm_pending_vblank_event *event; + struct drm_device *drm = dove_lcd->crtc.dev; + unsigned long flags; + + DRM_DEBUG_DRIVER("\n"); + + /* + * Destroy the pending vertical blanking event associated with the + * pending page flip, if any, and disable vertical blanking interrupts. + */ + spin_lock_irqsave(&drm->event_lock, flags); + event = dove_lcd->event; + if (event && event->base.file_priv == file) { + dove_lcd->event = NULL; + event->base.destroy(&event->base); + drm_vblank_put(drm, dove_lcd->num); + } + spin_unlock_irqrestore(&drm->event_lock, flags); +} + +/* configure default register values */ +static void dove_set_defaults(struct dove_lcd *dove_lcd) +{ + u32 x; + + x = SET_SCLK(dove_lcd->clk_src, 1, 0); + dove_write(dove_lcd, LCD_CFG_SCLK_DIV, x); + dove_write(dove_lcd, LCD_SPU_BLANKCOLOR, 0); + + dove_write(dove_lcd, SPU_IOPAD_CONTROL, IOPAD_DUMB24); + dove_write(dove_lcd, LCD_CFG_GRA_START_ADDR1, 0); + dove_write(dove_lcd, LCD_SPU_GRA_OVSA_HPXL_VLN, 0); + dove_write(dove_lcd, LCD_SPU_SRAM_PARA0, 0); + dove_write(dove_lcd, LCD_SPU_SRAM_PARA1, CFG_CSB_256x32 | + CFG_CSB_256x24 | + CFG_CSB_256x8); + dove_write(dove_lcd, LCD_SPU_DMA_CTRL1, CFG_VSYNC_TRIG(2) | + CFG_GATED_ENA | + CFG_PWRDN_ENA | + CFG_ALPHA_MODE(2) | + CFG_ALPHA(0xff) | + CFG_PXLCMD(0x81)); + + /* + * Fix me: to avoid jiggling issue for high resolution in + * dual display, we set watermark to affect LCD AXI read + * from MC (default 0x80). Lower watermark means LCD will + * do DMA read more often. + */ + x = dove_read(dove_lcd, LCD_CFG_RDREG4F); + x &= ~DMA_WATERMARK_MASK; + x |= DMA_WATERMARK(0x20); + + /* + * Disable LCD SRAM Read Wait State to resolve HWC32 make + * system hang while use external clock. + */ + x &= ~LCD_SRAM_WAIT; + dove_write(dove_lcd, LCD_CFG_RDREG4F, x); + + /* prepare the hwc32 */ + dove_set(dove_lcd, LCD_TV_CONTROL1, HWC32_ENABLE); + + /* set hwc32 with 100% static alpha blending factor */ + dove_write(dove_lcd, LCD_SPU_ALPHA_COLOR1, + HWC32_CFG_ALPHA(0xff)); +} + +static irqreturn_t dove_lcd_irq(int irq, void *dev_id) +{ + struct dove_lcd *dove_lcd = (struct dove_lcd *) dev_id; + struct drm_pending_vblank_event *event; + struct drm_device *drm = dove_lcd->crtc.dev; + u32 isr; + unsigned long flags; + + isr = dove_read(dove_lcd, SPU_IRQ_ISR); + dove_write(dove_lcd, SPU_IRQ_ISR, 0); + + DRM_DEBUG_DRIVER("\n"); + + if (isr & IRQ_GRA_FRAME_DONE) { +#ifdef HANDLE_INTERLACE + if (dove_lcd->v_sync0) { + u32 x; + + x = dove_read(dove_lcd, LCD_TV_CONTROL1); + x &= ~(VSYNC_L_OFFSET_MASK | VSYNC_H_OFFSET_MASK); + if (isr & IRQ_GRA_FRAME0) + x |= dove_lcd->v_sync0; + else + x |= dove_lcd->v_sync1; + dove_write(dove_lcd, LCD_TV_CONTROL1, x); + } + if (dove_lcd->vblank_enabled) +#endif + drm_handle_vblank(drm, dove_lcd->num); + spin_lock_irqsave(&drm->event_lock, flags); + event = dove_lcd->event; + dove_lcd->event = NULL; + if (event) + drm_send_vblank_event(drm, dove_lcd->num, event); + spin_unlock_irqrestore(&drm->event_lock, flags); + if (event) + drm_vblank_put(drm, dove_lcd->num); + } + + return IRQ_HANDLED; +} + +/* initialize a lcd */ +static int dove_crtc_init(struct dove_lcd *dove_lcd) +{ + struct drm_crtc *crtc = &dove_lcd->crtc; + struct dove_drm *dove_drm = dove_lcd->dove_drm; + struct drm_device *drm = dove_drm->drm; + int ret; + + DRM_DEBUG_DRIVER("\n"); + + dove_lcd->dpms = DRM_MODE_DPMS_OFF; + + ret = drm_crtc_init(drm, crtc, &dove_crtc_funcs); + if (ret < 0) + return ret; + + dove_write(dove_lcd, SPU_IRQ_ENA, 0); /* disable interrupts */ + ret = devm_request_irq(dove_lcd->dev, dove_lcd->irq, dove_lcd_irq, 0, + dove_lcd->name, dove_lcd); + if (ret < 0) { + dev_err(dove_lcd->dev, "unable to request irq %d\n", + dove_lcd->irq); + goto fail; + } + + dove_set_defaults(dove_lcd); + set_dumb_panel_control(dove_lcd, 1); + + drm_crtc_helper_add(crtc, &dove_crtc_helper_funcs); + + return 0; + +fail: + dove_crtc_destroy(crtc); + return ret; +} + +/* ----------------------------------------------------------------------------- + * Overlay plane + */ + +static void plane_update_base(struct dove_lcd *dove_lcd, + int plane_num, + struct drm_framebuffer *fb, + int fmt, + int x, int y, + int w, int h) +{ + struct drm_gem_cma_object *gem; + dma_addr_t start, addr; + + DRM_DEBUG_DRIVER("%dx%d+%d+%d\n", w, h, x, y); + + gem = drm_fb_cma_get_gem_obj(fb, 0); + if (!gem) { + dev_err(dove_lcd->dev, "cannot get gem obj"); + return; + } + + start = addr = gem->paddr + fb->offsets[0] + y * fb->pitches[0] + x; + dove_write(dove_lcd, LCD_SPU_DMA_START_ADDR_Y0, addr); + + switch (fmt) { + case VMODE_YUV422PLANAR: + case VMODE_YUV420PLANAR: + addr += fb->offsets[1]; + break; + } + dove_write(dove_lcd, LCD_SPU_DMA_START_ADDR_U0, addr); + + switch (fmt) { + case VMODE_YUV422PLANAR: + case VMODE_YUV420PLANAR: + addr = start + fb->offsets[2]; + break; + } + dove_write(dove_lcd, LCD_SPU_DMA_START_ADDR_V0, addr); + + dove_write(dove_lcd, LCD_SPU_DMA_PITCH_YC, + LCD_Y_C(fb->pitches[0], fb->pitches[0])); + switch (fmt) { + case VMODE_YUV422PLANAR: + case VMODE_YUV420PLANAR: + dove_write(dove_lcd, LCD_SPU_DMA_PITCH_UV, + LCD_U_V(fb->pitches[1], fb->pitches[2])); + break; + default: + dove_write(dove_lcd, LCD_SPU_DMA_PITCH_UV, + LCD_U_V(fb->pitches[0], fb->pitches[0])); + break; + } +} + +static int dove_plane_update(struct drm_plane *plane, + struct drm_crtc *crtc, + struct drm_framebuffer *fb, + int crtc_x, int crtc_y, + unsigned int crtc_w, unsigned int crtc_h, + uint32_t src_x, uint32_t src_y, + uint32_t src_w, uint32_t src_h) +{ + struct dove_lcd *dove_lcd = to_dove_lcd(crtc); + u32 x, x_bk; + int fmt, rbswap; + + DRM_DEBUG_DRIVER("plane fmt %.4s\n", (char *) &fb->pixel_format); + + rbswap = 1; /* default */ + switch (fb->pixel_format) { + case DRM_FORMAT_RGB888: + rbswap = 0; + case DRM_FORMAT_BGR888: +// fmt = VMODE_RGB888UNPACKED; + fmt = VMODE_RGB888PACKED; + break; + case DRM_FORMAT_YVYU: + rbswap = 0; + case DRM_FORMAT_YUYV: + case DRM_FORMAT_UYVY: + fmt = VMODE_YUV422PACKED; + break; + case DRM_FORMAT_YVU422: + rbswap = 0; + case DRM_FORMAT_YUV422: + fmt = VMODE_YUV422PLANAR; + break; + case DRM_FORMAT_YVU420: + rbswap = 0; + default: +/* case DRM_FORMAT_YUV420: */ + fmt = VMODE_YUV420PLANAR; + break; + } + + x_bk = x = dove_read(dove_lcd, LCD_SPU_DMA_CTRL0); + /* clear video layer's field */ + x &= ~(CFG_YUV2RGB_DMA | CFG_DMA_SWAP_MASK | + CFG_DMA_TSTMODE | CFG_DMA_HSMOOTH | CFG_DMA_FTOGGLE | + CFG_DMAFORMAT_MASK | CFG_PALETTE_ENA); + x |= CFG_DMA_HSMOOTH; /* enable horizontal smooth scaling */ + x |= CFG_DMAFORMAT(fmt); /* configure hardware pixel format */ + if (fmt == VMODE_RGB888PACKED) { + if (rbswap) + x |= CFG_DMA_SWAPRB; + } else if (fb->pixel_format == DRM_FORMAT_UYVY) { + x |= CFG_YUV2RGB_DMA; + } else if (fmt == VMODE_YUV422PACKED) { + x |= CFG_YUV2RGB_DMA | + CFG_DMA_SWAPYU | + CFG_DMA_SWAPRB; + if (rbswap) + x |= CFG_DMA_SWAPUV; + } else { /* planar */ + x |= CFG_YUV2RGB_DMA | + CFG_DMA_SWAPRB; + if (!rbswap) + x |= CFG_DMA_SWAPUV; + } + + /* set the dma addresses */ + plane_update_base(dove_lcd, 0, + fb, fmt, src_x, src_y, src_w, src_h); + + /* original size */ + dove_write(dove_lcd, LCD_SPU_DMA_HPXL_VLN, + LCD_H_V(src_w, src_h)); + + /* scaled size */ + dove_write(dove_lcd, LCD_SPU_DZM_HPXL_VLN, + LCD_H_V(crtc_w, crtc_h)); + + /* update video position offset */ + dove_write(dove_lcd, LCD_SPUT_DMA_OVSA_HPXL_VLN, + LCD_H_V(crtc_x, crtc_y)); + + x |= CFG_DMA_ENA; + if (x != x_bk) { + dove_write(dove_lcd, LCD_SPU_DMA_CTRL0, x); +// dove_set(dove_lcd, SPU_IRQ_ENA, +// DOVEFB_VID_INT_MASK | DOVEFB_VSYNC_INT_MASK); + } + + return 0; +} + +static int dove_plane_disable(struct drm_plane *plane) +{ + struct dove_lcd *dove_lcd = to_dove_lcd(plane->crtc); + + DRM_DEBUG_DRIVER("\n"); +//pr_info("dove_drm plane_disable crtc: %p", plane->crtc); + if (!plane->crtc) + return 0; + + dove_clear(dove_lcd, LCD_SPU_DMA_CTRL0, CFG_DMA_ENA); +// dove_clear(dove_lcd, SPU_IRQ_ENA, +// DOVEFB_VID_INT_MASK | DOVEFB_VSYNC_INT_MASK); + return 0; +} + +static void dove_plane_destroy(struct drm_plane *plane) +{ + dove_plane_disable(plane); + drm_plane_cleanup(plane); +} + +static const struct drm_plane_funcs plane_funcs = { + .update_plane = dove_plane_update, + .disable_plane = dove_plane_disable, + .destroy = dove_plane_destroy, +}; +static const uint32_t gfx_formats[] = { + DRM_FORMAT_BGR888, + DRM_FORMAT_RGB888, + DRM_FORMAT_XBGR8888, + DRM_FORMAT_XRGB8888, + DRM_FORMAT_ABGR8888, + DRM_FORMAT_ARGB8888, + DRM_FORMAT_YVYU, + DRM_FORMAT_YUYV, + DRM_FORMAT_YVU422, + DRM_FORMAT_YUV422, + DRM_FORMAT_YVU420, + DRM_FORMAT_YUV420, +}; +static const uint32_t vid_formats[] = { + DRM_FORMAT_BGR888, + DRM_FORMAT_RGB888, + DRM_FORMAT_YVYU, + DRM_FORMAT_YUYV, + DRM_FORMAT_YVU422, + DRM_FORMAT_YUV422, + DRM_FORMAT_YVU420, + DRM_FORMAT_YUV420, + DRM_FORMAT_UYVY, +}; + +static int dove_plane_init(struct dove_lcd *dove_lcd) +{ + struct drm_device *drm = dove_lcd->crtc.dev; + struct drm_plane *plane; + u32 x; + int ret; + + plane = &dove_lcd->plane; + ret = drm_plane_init(drm, plane, 1 << dove_lcd->num, + &plane_funcs, + vid_formats, ARRAY_SIZE(vid_formats), false); + if (ret < 0) + return ret; + + dove_write(dove_lcd, LCD_SPU_COLORKEY_Y, 0xfefefe00); + dove_write(dove_lcd, LCD_SPU_COLORKEY_U, 0x01010100); + dove_write(dove_lcd, LCD_SPU_COLORKEY_V, 0x01010100); + x = dove_read(dove_lcd, LCD_SPU_DMA_CTRL1); + x &= ~(CFG_COLOR_KEY_MASK | CFG_ALPHA_MODE_MASK | CFG_ALPHA_MASK); + x |= CFG_COLOR_KEY_MODE(3) | CFG_ALPHA_MODE(1); + dove_write(dove_lcd, LCD_SPU_DMA_CTRL1, x); + + plane->crtc = &dove_lcd->crtc; + return ret; +} + +/* ----------------------------------------------------------------------------- + * Initialization + */ + +/* at probe time, get the possible LCD clocks from the DT */ +static int get_lcd_clocks(struct dove_lcd *dove_lcd) +{ + struct device *dev = dove_lcd->dev; + struct device_node *np = dev->of_node; + char *clk_name; + struct clk *clk; + int clk_src, ret; + static char *clock_names[] = { /* !! index SCLK_SRC_xxx !! */ + "axibus", "ext_ref_clk0", "plldivider", "ext_ref_clk1" + }; + + /* get the clock and its name */ + ret = of_property_read_string(np, "clock-names", + (const char **) &clk_name); + if (ret) { + dev_err(dev, "no available clock\n"); + return -EINVAL; + } + for (clk_src = 0; clk_src < ARRAY_SIZE(clock_names); clk_src++) { + if (strcmp(clk_name, clock_names[clk_src]) == 0) + break; + } + if (clk_src >= ARRAY_SIZE(clock_names)) { + dev_err(dev, "unknown clock %s\n", clk_name); + return -EINVAL; + } + clk = clk_get(dev, clk_name); + if (IS_ERR(clk)) + return PTR_ERR(clk); + DRM_DEBUG_DRIVER("clock %s ok\n", clk_name); + clk_prepare_enable(clk); + dove_lcd->clk = clk; + dove_lcd->clk_src = clk_src; + return 0; +} + +static int dove_lcd_bind(struct device *dev, struct device *master, + void *data) +{ + struct drm_device *drm = data; + struct dove_drm *dove_drm = drm_to_dove(drm); + struct dove_lcd *dove_lcd = dev_get_drvdata(dev); + int ret; + + DRM_DEBUG_DRIVER("\n"); + + dove_lcd->dove_drm = dove_drm; + + ret = get_lcd_clocks(dove_lcd); + if (ret < 0) + return ret; + + dove_drm->lcds[dove_lcd->num] = dove_lcd; + + ret = dove_crtc_init(dove_lcd); + if (ret < 0) + goto fail; + ret = dove_plane_init(dove_lcd); + if (ret < 0) + dev_err(dove_lcd->dev, "failed to create the video plane\n"); + + return 0; + +fail: + if (dove_lcd->clk) { + clk_disable_unprepare(dove_lcd->clk); + clk_put(dove_lcd->clk); + } + return ret; +} + +static void dove_lcd_unbind(struct device *dev, struct device *master, + void *data) +{ + struct dove_drm *dove_drm = data; + struct platform_device *pdev = to_platform_device(dev); + struct dove_lcd *dove_lcd = platform_get_drvdata(pdev); + struct clk *clk; + + dove_write(dove_lcd, SPU_IRQ_ENA, 0); /* disable interrupts */ + + clk = dove_lcd->clk; + if (clk) { + clk_disable_unprepare(clk); + clk_put(clk); + } + +// is this useful? + if (dove_drm->lcds[dove_lcd->num] == dove_lcd) + dove_drm->lcds[dove_lcd->num] = NULL; +} + +static const struct component_ops comp_ops = { + .bind = dove_lcd_bind, + .unbind = dove_lcd_unbind, +}; + +static int dove_lcd_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct device_node *np = dev->of_node; + struct dove_lcd *dove_lcd; + struct resource *res; + int id; + + id = of_alias_get_id(np, "lcd"); + if ((unsigned) id >= MAX_DOVE_LCD) { + dev_err(dev, "no or bad alias for lcd\n"); + return -ENXIO; + } + + dove_lcd = devm_kzalloc(dev, sizeof *dove_lcd, GFP_KERNEL); + if (!dove_lcd) { + dev_err(dev, "failed to allocate private data\n"); + return -ENOMEM; + } + platform_set_drvdata(pdev, dove_lcd); + dove_lcd->dev = dev; + dove_lcd->num = id; + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (!res) { + dev_err(dev, "failed to get memory resource\n"); + return -EINVAL; + } + + dove_lcd->mmio = devm_ioremap_resource(dev, res); + if (IS_ERR(dove_lcd->mmio)) { + dev_err(dev, "failed to map registers\n"); + return PTR_ERR(dove_lcd->mmio); + } + + snprintf(dove_lcd->name, sizeof dove_lcd->name, "dove-lcd%d", id); + + dove_lcd->irq = irq_of_parse_and_map(np, 0); + if (dove_lcd->irq <= 0 || dove_lcd->irq == NO_IRQ) { + dev_err(dev, "unable to get irq lcd %d\n", id); + return -EINVAL; + } + +#ifdef LCD_DEBUG + device_create_file(dev, &dev_attr_lcd_read); + device_create_file(dev, &dev_attr_lcd_write); +/* dev_set_drvdata(dev, dove_lcd); */ +#endif + + return component_add(dev, &comp_ops); +} +static int dove_lcd_remove(struct platform_device *pdev) +{ + component_del(&pdev->dev, &comp_ops); + return 0; +} + +static struct of_device_id dove_lcd_of_match[] = { + { .compatible = "marvell,dove-lcd" }, + { }, +}; +struct platform_driver dove_lcd_platform_driver = { + .probe = dove_lcd_probe, + .remove = dove_lcd_remove, + .driver = { + .owner = THIS_MODULE, + .name = "dove-lcd", + .of_match_table = dove_lcd_of_match, + }, +}; diff --git a/drivers/gpu/drm/dove/dove_drv.c b/drivers/gpu/drm/dove/dove_drv.c new file mode 100644 index 0000000..bab5cd3 --- /dev/null +++ b/drivers/gpu/drm/dove/dove_drv.c @@ -0,0 +1,438 @@ +/* + * Marvell Dove DRM driver - main + * + * Copyright (C) 2013-2015 + * Jean-Francois Moine + * Sebastian Hesselbarth + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include + +#include "dove_drv.h" + +#define DRIVER_NAME "dove-drm" +#define DRIVER_DESC "Marvell Dove DRM" +#define DRIVER_DATE "20140204" +#define DRIVER_MAJOR 1 +#define DRIVER_MINOR 0 + +static struct drm_framebuffer *dove_fb_create(struct drm_device *drm, + struct drm_file *file_priv, + struct drm_mode_fb_cmd2 *mode_cmd) +{ + DRM_DEBUG_DRIVER("%.4s %dx%d\n", + (char *) &mode_cmd->pixel_format, + mode_cmd->width, mode_cmd->height); + + switch (mode_cmd->pixel_format) { + case DRM_FORMAT_BGR888: + case DRM_FORMAT_RGB888: + case DRM_FORMAT_XBGR8888: + case DRM_FORMAT_XRGB8888: + case DRM_FORMAT_ABGR8888: + case DRM_FORMAT_ARGB8888: + case DRM_FORMAT_YVYU: + case DRM_FORMAT_YUYV: + case DRM_FORMAT_UYVY: + case DRM_FORMAT_YVU422: + case DRM_FORMAT_YUV422: + case DRM_FORMAT_YVU420: + case DRM_FORMAT_YUV420: + break; + default: + return ERR_PTR(-EINVAL); + } + return drm_fb_cma_create(drm, file_priv, mode_cmd); +} + +static void dove_fb_output_poll_changed(struct drm_device *drm) +{ + struct dove_drm *dove_drm = drm_to_dove(drm); + + DRM_DEBUG_DRIVER("fb:%d\n", dove_drm->fbdev != NULL); +// if (dove_drm->fbdev) + drm_fbdev_cma_hotplug_event(dove_drm->fbdev); +} + +static const struct drm_mode_config_funcs mode_config_funcs = { + .fb_create = dove_fb_create, + .output_poll_changed = dove_fb_output_poll_changed, +}; + +/* + * DRM operations: + */ +static int dove_unload(struct drm_device *drm) +{ + struct dove_drm *dove_drm = drm_to_dove(drm); + struct dove_lcd *dove_lcd; + int i; + + DRM_DEBUG_DRIVER("\n"); + + for (i = 0; i < MAX_DOVE_LCD; i++) { + dove_lcd = dove_drm->lcds[i]; + if (dove_lcd) + drm_plane_cleanup(&dove_lcd->plane); + } + drm_kms_helper_poll_fini(drm); + drm_mode_config_cleanup(drm); + drm_vblank_cleanup(drm); + + return 0; +} + +static int dove_load(struct drm_device *drm, unsigned long flags) +{ + struct platform_device *pdev = drm->platformdev; + struct dove_drm *dove_drm; + int ret; + + DRM_DEBUG_DRIVER("\n"); + + drm_mode_config_init(drm); + +/* pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); */ + + dove_drm = devm_kzalloc(&pdev->dev, sizeof *dove_drm, GFP_KERNEL); + if (!dove_drm) { + dev_err(&pdev->dev, "failed to allocate dove drm\n"); + return -ENOMEM; + } + platform_set_drvdata(pdev, dove_drm); + + dove_drm->drm = drm; + drm->dev_private = dove_drm; + + /* initialize the subdevices */ + ret = component_bind_all(&pdev->dev, drm); + if (ret < 0) + goto fail; +// if (dove_drm->dcon) +// dove_drm->dcon->dove_drm = dove_drm; + + drm->mode_config.min_width = 0; + drm->mode_config.min_height = 0; + drm->mode_config.max_width = 2048; + drm->mode_config.max_height = 2048; + drm->mode_config.funcs = &mode_config_funcs; + + ret = drm_vblank_init(drm, drm->mode_config.num_crtc); + if (ret < 0) { + dev_err(drm->dev, "failed to initialize vblank\n"); + goto fail; + } + + dove_drm->fbdev = drm_fbdev_cma_init(drm, + 32, /* bpp */ + drm->mode_config.num_crtc, + drm->mode_config.num_connector); + + drm_kms_helper_poll_init(drm); + return 0; +fail: + dove_unload(drm); + return ret; +} + +static void dove_preclose(struct drm_device *drm, struct drm_file *file) +{ + struct dove_drm *dove_drm = drm_to_dove(drm); + struct dove_lcd *dove_lcd; + int i; + +// is the crtc list usable here? + for (i = 0; i < MAX_DOVE_LCD; i++) { + dove_lcd = dove_drm->lcds[i]; + if (dove_lcd) + dove_crtc_cancel_page_flip(dove_lcd, file); + } +} + +static void dove_lastclose(struct drm_device *drm) +{ + struct dove_drm *dove_drm = drm_to_dove(drm); + + drm_fbdev_cma_restore_mode(dove_drm->fbdev); +} + +static const struct file_operations fops = { + .owner = THIS_MODULE, + .open = drm_open, + .release = drm_release, + .unlocked_ioctl = drm_ioctl, + .poll = drm_poll, + .read = drm_read, + .llseek = no_llseek, + .mmap = drm_gem_cma_mmap, +}; + +static struct drm_driver dove_driver = { + .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME, + .load = dove_load, + .unload = dove_unload, + .preclose = dove_preclose, + .lastclose = dove_lastclose, + .get_vblank_counter = dove_vblank_count, + .enable_vblank = dove_enable_vblank, + .disable_vblank = dove_disable_vblank, + .gem_free_object = drm_gem_cma_free_object, + .gem_vm_ops = &drm_gem_cma_vm_ops, + .prime_handle_to_fd = drm_gem_prime_handle_to_fd, + .prime_fd_to_handle = drm_gem_prime_fd_to_handle, + .gem_prime_import = drm_gem_prime_import, + .gem_prime_export = drm_gem_prime_export, + .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, + .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, + .gem_prime_vmap = drm_gem_cma_prime_vmap, + .gem_prime_vunmap = drm_gem_cma_prime_vunmap, + .gem_prime_mmap = drm_gem_cma_prime_mmap, + .dumb_create = drm_gem_cma_dumb_create, + .dumb_map_offset = drm_gem_cma_dumb_map_offset, + .dumb_destroy = drm_gem_dumb_destroy, +#ifdef CONFIG_DEBUG_FS + .debugfs_init = dove_debugfs_init, + .debugfs_cleanup = dove_debugfs_cleanup, +#endif + .fops = &fops, + + .name = DRIVER_NAME, + .desc = DRIVER_DESC, + .date = DRIVER_DATE, + .major = DRIVER_MAJOR, + .minor = DRIVER_MINOR, +}; + +#ifdef CONFIG_PM_SLEEP +/* + * Power management + */ +static int dove_pm_suspend(struct device *dev) +{ + struct dove_drm *dove_drm = dev_get_drvdata(dev); + struct dove_lcd *dove_lcd; + int i; + + drm_kms_helper_poll_disable(&dove_drm->drm); + for (i = 0; i < MAX_DOVE_LCD; i++) { + dove_lcd = dove_drm->lcds[i]; + if (dove_lcd) + dove_crtc_stop(dove_lcd); + } + return 0; +} + +static int dove_pm_resume(struct device *dev) +{ + struct dove_drm *dove_drm = dev_get_drvdata(dev); + struct dove_lcd *dove_lcd; + int i; + + for (i = 0; i < MAX_DOVE_LCD; i++) { + dove_lcd = dove_drm->lcds[i]; + if (dove_lcd + && dove_lcd->dpms == DRM_MODE_DPMS_ON) + dove_crtc_start(dove_lcd); + } + drm_kms_helper_poll_enable(&dove_drm->drm); + return 0; +} +#endif + +static const struct dev_pm_ops dove_pm_ops = { + SET_SYSTEM_SLEEP_PM_OPS(dove_pm_suspend, dove_pm_resume) +}; + +/* + * Platform driver + */ + +/* component stuff */ +static int of_dev_node_match(struct device *dev, void *data) +{ + return dev->of_node == data; +} + +static int dove_drm_add_components(struct device *master, struct master *m) +{ + struct device_node *node, *child, *np = master->of_node; + int i, ret; + + /* search the video devices */ + for (i = 0; ; i++) { + node = of_parse_phandle(np, "marvell,video-devices", i); + if (!node) + return 0; /* all devices are found */ + + ret = component_master_add_child(m, of_dev_node_match, + node); + if (ret) + goto err; + +//fixme: voir armada pour simplifier... + /* search the encoders/connectors as child "port" */ + child = NULL; + for (;;) { + struct device_node *endpoint, *port, *i2c_node; + + child = of_get_next_child(node, child); + if (!child) + break; + if (strcmp(child->name, "port") != 0) + continue; + + endpoint = of_get_next_child(child, NULL); + if (!endpoint) { + dev_err(master, + "dove drm: no port description\n"); + ret = -EINVAL; + goto err; + } + port = of_parse_phandle(endpoint, + "remote-endpoint", 0); + of_node_put(endpoint); + if (!port) { + dev_err(master, + "dove drm: no remote-endpoint\n"); + ret = -EINVAL; + goto err; + } + i2c_node = of_get_parent(of_get_parent(port)); + of_node_put(port); + ret = component_master_add_child(m, of_dev_node_match, + i2c_node); + of_node_put(i2c_node); + if (ret) + goto err; + } + of_node_put(node); + } + +err: + of_node_put(node); + return ret; +} +static int dove_drm_bind(struct device *dev) +{ + struct drm_device *drm; + int ret; + + drm = drm_dev_alloc(&dove_driver, &to_platform_device(dev)->dev); + if (!drm) + return -ENOMEM; + drm->platformdev = to_platform_device(dev); + + ret = drm_dev_set_unique(drm, dev_name(drm->dev)); + if (ret < 0) { + drm_dev_unref(drm); + return ret; + } + + ret = drm_dev_register(drm, 0); + if (ret < 0) { + drm_dev_unref(drm); + return ret; + } + return ret; +} + +static void dove_drm_unbind(struct device *dev) +{ + struct dove_drm *dove_drm = dev_get_drvdata(dev); + + drm_dev_unregister(dove_drm->drm); + drm_dev_unref(dove_drm->drm); +} + +static const struct component_master_ops dove_drm_comp_ops = { + .add_components = dove_drm_add_components, + .bind = dove_drm_bind, + .unbind = dove_drm_unbind, +}; + +static int dove_pdev_probe(struct platform_device *pdev) +{ + DRM_DEBUG_DRIVER("\n"); + + if (!pdev->dev.of_node) { + dev_err(&pdev->dev, "no device-tree\n"); + return -ENXIO; + } + + return component_master_add(&pdev->dev, &dove_drm_comp_ops); +} + +static int dove_pdev_remove(struct platform_device *pdev) +{ + component_master_del(&pdev->dev, &dove_drm_comp_ops); + return 0; +} + +static struct of_device_id dove_of_match[] = { + { .compatible = "marvell,dove-video" }, + { }, +}; +MODULE_DEVICE_TABLE(of, dove_of_match); + +static struct platform_driver dove_platform_driver = { + .probe = dove_pdev_probe, + .remove = dove_pdev_remove, + .driver = { + .owner = THIS_MODULE, + .name = "dove-drm", + .pm = &dove_pm_ops, + .of_match_table = dove_of_match, + }, +}; + +static int __init dove_drm_init(void) +{ + int ret; + + /* wait for other drivers to be loaded (si5351) */ + msleep(200); + +/* uncomment to activate the drm trace at startup time */ +// drm_debug = DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS; + + DRM_DEBUG_DRIVER("\n"); + + ret = platform_driver_register(&dove_lcd_platform_driver); + if (ret < 0) + return ret; +// ret = platform_driver_register(&dove_dcon_platform_driver); +// if (ret < 0) +// goto out1; + ret = platform_driver_register(&dove_platform_driver); + if (ret < 0) + goto out2; + return 0; + +out2: +// platform_driver_unregister(&dove_dcon_platform_driver); +//out1: + platform_driver_unregister(&dove_lcd_platform_driver); + return ret; +} +static void __exit dove_drm_fini(void) +{ + platform_driver_unregister(&dove_platform_driver); +// platform_driver_unregister(&dove_dcon_platform_driver); + platform_driver_unregister(&dove_lcd_platform_driver); +} +module_init(dove_drm_init); +module_exit(dove_drm_fini); + +MODULE_AUTHOR("Jean-Francois Moine "); +MODULE_AUTHOR("Sebastian Hesselbarth "); +MODULE_DESCRIPTION("Marvell Dove DRM Driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/gpu/drm/dove/dove_drv.h b/drivers/gpu/drm/dove/dove_drv.h new file mode 100644 index 0000000..0cc8321 --- /dev/null +++ b/drivers/gpu/drm/dove/dove_drv.h @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2013-2014 Jean-François Moine + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __DOVE_DRV_H__ +#define __DOVE_DRV_H__ + +#include +#include +#include +#include +#include + +/* (not tested) */ +/*#define HANDLE_INTERLACE 1*/ + +#define MAX_DOVE_LCD 2 /* max number of dove lcd devices */ + +struct dove_drm; + +struct dove_lcd { + void __iomem *mmio; + struct device *dev; + struct dove_drm *dove_drm; + struct drm_crtc crtc; + + u8 num; /* index in dove_drm */ + u8 dpms; + +#ifdef HANDLE_INTERLACE + u32 v_sync0; + u32 v_sync1; + u8 vblank_enabled; +#endif + + u8 clk_src; /* clock source index = SCLK_SRC_xxx */ + struct clk *clk; + + int irq; + char name[16]; + + struct drm_pending_vblank_event *event; + + struct drm_plane plane; /* video plane */ +}; + +struct dove_dcon { + void __iomem *mmio; + struct device *dev; + struct dove_drm *dove_drm; +}; + +struct dove_drm { + struct drm_device *drm; + struct dove_lcd *lcds[MAX_DOVE_LCD]; + struct dove_dcon *dcon; + + struct drm_fbdev_cma *fbdev; +}; + +#define drm_to_dove(x) x->dev_private + +u32 dove_vblank_count(struct drm_device *dev, int crtc); +int dove_enable_vblank(struct drm_device *dev, int crtc); +void dove_disable_vblank(struct drm_device *dev, int crtc); +int dove_lcd_init(struct dove_lcd *dove_lcd); +void dove_crtc_cancel_page_flip(struct dove_lcd *dove_lcd, + struct drm_file *file); +void dove_crtc_start(struct dove_lcd *dove_lcd); +void dove_crtc_stop(struct dove_lcd *dove_lcd); +#ifdef CONFIG_DEBUG_FS +int dove_debugfs_init(struct drm_minor *minor); +void dove_debugfs_cleanup(struct drm_minor *minor); +#endif +extern struct platform_driver dove_lcd_platform_driver; + +#endif /* __DOVE_DRV_H__ */ diff --git a/drivers/gpu/drm/dove/dove_lcd.h b/drivers/gpu/drm/dove/dove_lcd.h new file mode 100644 index 0000000..03b198b --- /dev/null +++ b/drivers/gpu/drm/dove/dove_lcd.h @@ -0,0 +1,519 @@ +/* + * LCD controller registers of Marvell DOVE + * + * Copyright (C) 2013 + * Jean-Francois Moine + * Sebastian Hesselbarth + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef _DOVE_LCD_H_ +#define _DOVE_LCD_H_ + +/* ------------< LCD register >------------ */ + +/* Video Frame 0&1 start address registers */ +#define LCD_TV_CONTROL1 0x0084 +#define VSYNC_L_OFFSET(o) ((o) << 20) +#define VSYNC_L_OFFSET_MASK (0xfff << 20) +#define HWC32_ENABLE BIT(13) +#define VSYNC_OFFSET_EN BIT(12) +#define VSYNC_H_OFFSET(o) (o) +#define VSYNC_H_OFFSET_MASK 0xfff + +/* Video Frame 0&1 start address registers */ +#define LCD_SPU_DMA_START_ADDR_Y0 0x00c0 +#define LCD_SPU_DMA_START_ADDR_U0 0x00c4 +#define LCD_SPU_DMA_START_ADDR_V0 0x00c8 +#define LCD_CFG_DMA_START_ADDR_0 0x00cc /* Cmd address */ +#define LCD_SPU_DMA_START_ADDR_Y1 0x00d0 +#define LCD_SPU_DMA_START_ADDR_U1 0x00d4 +#define LCD_SPU_DMA_START_ADDR_V1 0x00d8 +#define LCD_CFG_DMA_START_ADDR_1 0x00dc /* Cmd address */ + +/* YC & UV Pitch */ +#define LCD_SPU_DMA_PITCH_YC 0x00e0 +#define LCD_Y_C(y, c) (((c) << 16) | (y)) +#define LCD_SPU_DMA_PITCH_UV 0x00e4 +#define LCD_U_V(u, v) (((v) << 16) | (u)) + +/* Video Starting Point on Screen Register */ +#define LCD_SPUT_DMA_OVSA_HPXL_VLN 0x00e8 + +/* Video Size Register */ +#define LCD_SPU_DMA_HPXL_VLN 0x00ec + +/* Video Size After zooming Register */ +#define LCD_SPU_DZM_HPXL_VLN 0x00f0 + +/* Graphic Frame 0&1 Starting Address Register */ +#define LCD_CFG_GRA_START_ADDR0 0x00f4 +#define LCD_CFG_GRA_START_ADDR1 0x00f8 + +/* Graphic Frame Pitch */ +#define LCD_CFG_GRA_PITCH 0x00fc + +/* Graphic Starting Point on Screen Register */ +#define LCD_SPU_GRA_OVSA_HPXL_VLN 0x0100 + +/* Graphic Size Register */ +#define LCD_SPU_GRA_HPXL_VLN 0x0104 + +/* Graphic Size after Zooming Register */ +#define LCD_SPU_GZM_HPXL_VLN 0x0108 + +/* HW Cursor Starting Point on Screen Register */ +#define LCD_SPU_HWC_OVSA_HPXL_VLN 0x010c + +/* HW Cursor Size */ +#define LCD_SPU_HWC_HPXL_VLN 0x0110 + +/* Total Screen Size Register */ +#define LCD_SPUT_V_H_TOTAL 0x0114 + +/* Total Screen Active Size Register */ +#define LCD_SPU_V_H_ACTIVE 0x0118 +#define LCD_H_V(h, v) (((v) << 16) | (h)) +#define H_LCD(x) ((x) & 0xffff) +#define V_LCD(x) (((x) >> 16) & 0xffff) + +/* Screen H&V Porch Register */ +#define LCD_SPU_H_PORCH 0x011c +#define LCD_SPU_V_PORCH 0x0120 +#define LCD_F_B(f, b) (((b) << 16) | (f)) +#define F_LCD(x) ((x) & 0xffff) +#define B_LCD(x) (((x) >> 16) & 0xffff) + +/* Screen Blank Color Register */ +#define LCD_SPU_BLANKCOLOR 0x0124 + +/* HW Cursor Color 1&2 Register */ +#define LCD_SPU_ALPHA_COLOR1 0x0128 +#define HWC32_CFG_ALPHA(alpha) ((alpha) << 24) +#define LCD_SPU_ALPHA_COLOR2 0x012c +#define COLOR_MASK 0x00ffffff +#define COLOR_RGB(r, g, b) (((b) << 16) | ((g) << 8) | (r)) + +/* Video YUV Color Key Control */ +#define LCD_SPU_COLORKEY_Y 0x0130 +#define CFG_CKEY_Y2(y2) ((y2) << 24) +#define CFG_CKEY_Y2_MASK 0xff000000 +#define CFG_CKEY_Y1(y1) ((y1) << 16) +#define CFG_CKEY_Y1_MASK 0x00ff0000 +#define CFG_CKEY_Y(y) ((y) << 8) +#define CFG_CKEY_Y_MASK 0x0000ff00 +#define CFG_ALPHA_Y(y) (y) +#define CFG_ALPHA_Y_MASK 0x000000ff +#define LCD_SPU_COLORKEY_U 0x0134 +#define CFG_CKEY_U2(u2) ((u2) << 24) +#define CFG_CKEY_U2_MASK 0xff000000 +#define CFG_CKEY_U1(u1) ((u1) << 16) +#define CFG_CKEY_U1_MASK 0x00ff0000 +#define CFG_CKEY_U(u) ((u) << 8) +#define CFG_CKEY_U_MASK 0x0000ff00 +#define CFG_ALPHA_U(u) (u) +#define CFG_ALPHA_U_MASK 0x000000ff +#define LCD_SPU_COLORKEY_V 0x0138 +#define CFG_CKEY_V2(v2) ((v2) << 24) +#define CFG_CKEY_V2_MASK 0xff000000 +#define CFG_CKEY_V1(v1) ((v1) << 16) +#define CFG_CKEY_V1_MASK 0x00ff0000 +#define CFG_CKEY_V(v) ((v) << 8) +#define CFG_CKEY_V_MASK 0x0000ff00 +#define CFG_ALPHA_V(v) (v) +#define CFG_ALPHA_V_MASK 0x000000ff + +/* LCD General Configuration Register */ +#define LCD_CFG_RDREG4F 0x013c +#define LCD_SRAM_WAIT BIT(11) +#define DMA_WATERMARK_MASK 0xff +#define DMA_WATERMARK(m) (m) + +/* SPI Read Data Register */ +#define LCD_SPU_SPI_RXDATA 0x0140 + +/* Smart Panel Read Data Register */ +#define LCD_SPU_ISA_RSDATA 0x0144 +#define ISA_RXDATA_16BIT_1_DATA_MASK 0x000000ff +#define ISA_RXDATA_16BIT_2_DATA_MASK 0x0000ff00 +#define ISA_RXDATA_16BIT_3_DATA_MASK 0x00ff0000 +#define ISA_RXDATA_16BIT_4_DATA_MASK 0xff000000 +#define ISA_RXDATA_32BIT_1_DATA_MASK 0x00ffffff + +/* HWC SRAM Read Data Register */ +#define LCD_SPU_HWC_RDDAT 0x0158 + +/* Gamma Table SRAM Read Data Register */ +#define LCD_SPU_GAMMA_RDDAT 0x015c +#define GAMMA_RDDAT_MASK 0x000000ff + +/* Palette Table SRAM Read Data Register */ +#define LCD_SPU_PALETTE_RDDAT 0x0160 +#define PALETTE_RDDAT_MASK 0x00ffffff + +/* I/O Pads Input Read Only Register */ +#define LCD_SPU_IOPAD_IN 0x0178 +#define IOPAD_IN_MASK 0x0fffffff + +/* Reserved Read Only Registers */ +#define LCD_CFG_RDREG5F 0x017c +#define IRE_FRAME_CNT_MASK 0x000000c0 +#define IPE_FRAME_CNT_MASK 0x00000030 +#define GRA_FRAME_CNT_MASK 0x0000000c /* Graphic */ +#define DMA_FRAME_CNT_MASK 0x00000003 /* Video */ + +/* SPI Control Register. */ +#define LCD_SPU_SPI_CTRL 0x0180 +#define CFG_SCLKCNT(div) ((div) << 24) +#define CFG_SCLKCNT_MASK 0xff000000 +#define CFG_RXBITS(rx) ((rx) << 16) +#define CFG_RXBITS_MASK 0x00ff0000 +#define CFG_TXBITS(tx) ((tx) << 8) +#define CFG_TXBITS_MASK 0x0000ff00 +#define CFG_CLKINV BIT(7) +#define CFG_KEEPXFER BIT(6) +#define CFG_RXBITSTO0 BIT(5) +#define CFG_TXBITSTO0 BIT(4) +#define CFG_SPI_ENA BIT(3) +#define CFG_SPI_SEL BIT(2) +#define CFG_SPI_3W4WB BIT(1) +#define CFG_SPI_START BIT(0) + +/* SPI Tx Data Register */ +#define LCD_SPU_SPI_TXDATA 0x0184 + +/* + * 1. Smart Pannel 8-bit Bus Control Register. + * 2. AHB Slave Path Data Port Register + */ +#define LCD_SPU_SMPN_CTRL 0x0188 + +/* DMA Control 0 Register */ +#define LCD_SPU_DMA_CTRL0 0x0190 +#define CFG_NOBLENDING BIT(31) +#define CFG_GAMMA_ENA BIT(30) +#define CFG_CBSH_ENA BIT(29) +#define CFG_PALETTE_ENA BIT(28) +#define CFG_ARBFAST_ENA BIT(27) +#define CFG_HWC_1BITMOD BIT(26) +#define CFG_HWC_1BITENA BIT(25) +#define CFG_HWC_ENA BIT(24) +#define CFG_DMAFORMAT(dmaformat) ((dmaformat) << 20) +#define CFG_DMAFORMAT_MASK 0x00f00000 +#define CFG_GRAFORMAT(graformat) ((graformat) << 16) +#define CFG_GRAFORMAT_MASK 0x000f0000 +/* for graphic part */ +#define CFG_GRA_FTOGGLE BIT(15) +#define CFG_GRA_HSMOOTH BIT(14) +#define CFG_GRA_TSTMODE BIT(13) +#define CFG_GRA_SWAPRB BIT(12) +#define CFG_GRA_SWAPUV BIT(11) +#define CFG_GRA_SWAPYU BIT(10) +#define CFG_YUV2RGB_GRA BIT(9) +#define CFG_GRA_ENA BIT(8) +/* for video part */ +#define CFG_DMA_FTOGGLE BIT(7) +#define CFG_DMA_HSMOOTH BIT(6) +#define CFG_DMA_TSTMODE BIT(5) +#define CFG_DMA_SWAPRB BIT(4) +#define CFG_DMA_SWAPUV BIT(3) +#define CFG_DMA_SWAPYU BIT(2) +#define CFG_DMA_SWAP_MASK 0x0000001c +#define CFG_YUV2RGB_DMA BIT(1) +#define CFG_DMA_ENA BIT(0) + +/* DMA Control 1 Register */ +#define LCD_SPU_DMA_CTRL1 0x0194 +#define CFG_FRAME_TRIG BIT(31) +#define CFG_VSYNC_TRIG(trig) ((trig) << 28) +#define CFG_VSYNC_TRIG_MASK 0x70000000 +#define CFG_VSYNC_INV BIT(27) +#define CFG_COLOR_KEY_MODE(cmode) ((cmode) << 24) +#define CFG_COLOR_KEY_MASK 0x07000000 +#define CFG_CARRY BIT(23) +#define CFG_GATED_ENA BIT(21) +#define CFG_PWRDN_ENA BIT(20) +#define CFG_DSCALE(dscale) ((dscale) << 18) +#define CFG_DSCALE_MASK 0x000c0000 +#define CFG_ALPHA_MODE(amode) ((amode) << 16) +#define CFG_ALPHA_MODE_MASK 0x00030000 +#define CFG_ALPHA(alpha) ((alpha) << 8) +#define CFG_ALPHA_MASK 0x0000ff00 +#define CFG_PXLCMD(pxlcmd) (pxlcmd) +#define CFG_PXLCMD_MASK 0x000000ff + +/* SRAM Control Register */ +#define LCD_SPU_SRAM_CTRL 0x0198 +#define CFG_SRAM_INIT_WR_RD(mode) ((mode) << 14) +#define CFG_SRAM_INIT_WR_RD_MASK 0x0000c000 +#define CFG_SRAM_ADDR_LCDID(id) ((id) << 8) +#define CFG_SRAM_ADDR_LCDID_MASK 0x00000f00 +#define CFG_SRAM_ADDR(addr) (addr) +#define CFG_SRAM_ADDR_MASK 0x000000ff + +/* SRAM Write Data Register */ +#define LCD_SPU_SRAM_WRDAT 0x019c + +/* SRAM RTC/WTC Control Register */ +#define LCD_SPU_SRAM_PARA0 0x01a0 + +/* SRAM Power Down Control Register */ +#define LCD_SPU_SRAM_PARA1 0x01a4 +#define CFG_CSB_256x32 BIT(15) /* HWC */ +#define CFG_CSB_256x24 BIT(14) /* Palette */ +#define CFG_CSB_256x8 BIT(13) /* Gamma */ +#define CFG_PDWN256x32 BIT(7) /* HWC */ +#define CFG_PDWN256x24 BIT(6) /* Palette */ +#define CFG_PDWN256x8 BIT(5) /* Gamma */ +#define CFG_PDWN32x32 BIT(3) +#define CFG_PDWN16x66 BIT(2) +#define CFG_PDWN32x66 BIT(1) +#define CFG_PDWN64x66 BIT(0) + +/* Smart or Dumb Panel Clock Divider */ +#define LCD_CFG_SCLK_DIV 0x01a8 +#define SET_SCLK(src, div, frac) (((src) << 30) | ((frac) << 16 ) | (div)) + +/* Video Contrast Register */ +#define LCD_SPU_CONTRAST 0x01ac +#define CFG_BRIGHTNESS(bright) ((bright) << 16) +#define CFG_BRIGHTNESS_MASK 0xffff0000 +#define CFG_CONTRAST(contrast) (contrast) +#define CFG_CONTRAST_MASK 0x0000ffff + +/* Video Saturation Register */ +#define LCD_SPU_SATURATION 0x01b0 +#define CFG_C_MULTS(mult) ((mult) << 16) +#define CFG_C_MULTS_MASK 0xffff0000 +#define CFG_SATURATION(sat) (sat) +#define CFG_SATURATION_MASK 0x0000ffff + +/* Video Hue Adjust Register */ +#define LCD_SPU_CBSH_HUE 0x01b4 +#define CFG_SIN0(sin0) ((sin0) << 16) +#define CFG_SIN0_MASK 0xffff0000 +#define CFG_COS0(con0) (con0) +#define CFG_COS0_MASK 0x0000ffff + +/* Dump LCD Panel Control Register */ +#define LCD_SPU_DUMB_CTRL 0x01b8 +#define CFG_DUMBMODE(mode) ((mode) << 28) +#define CFG_DUMBMODE_MASK 0xf0000000 +#define CFG_LCDGPIO_O(data) ((data) << 20) +#define CFG_LCDGPIO_O_MASK 0x0ff00000 +#define CFG_LCDGPIO_ENA(gpio) ((gpio) << 12) +#define CFG_LCDGPIO_ENA_MASK 0x000ff000 +#define CFG_BIAS_OUT BIT(8) +#define CFG_REVERSE_RGB BIT(7) +#define CFG_INV_COMPBLANK BIT(6) +#define CFG_INV_COMPSYNC BIT(5) +#define CFG_INV_HENA BIT(4) +#define CFG_INV_VSYNC BIT(3) +#define CFG_INV_HSYNC BIT(2) +#define CFG_INV_PCLK BIT(1) +#define CFG_DUMB_ENA BIT(0) + +/* LCD I/O Pads Control Register */ +#define SPU_IOPAD_CONTROL 0x01bc +#define CFG_VSC_LINEAR(vm) ((vm) << 18) /* gfx */ +#define CFG_VSC_LINEAR_MASK 0x000c0000 +#define CFG_GRA_VM_ENA BIT(15) /* gfx */ +#define CFG_DMA_VM_ENA BIT(14) /* video */ +#define CFG_CMD_VM_ENA BIT(13) +#define CFG_CSC(csc) ((csc) << 8) +#define CFG_CSC_MASK 0x00000300 +#define CFG_AXICTRL(axi) ((axi) << 4) +#define CFG_AXICTRL_MASK 0x000000f0 +#define CFG_IOPADMODE(iopad) (iopad) +#define CFG_IOPADMODE_MASK 0x0000000f + +/* LCD Interrupt Control Register */ +#define SPU_IRQ_ENA 0x1c0 +/* LCD Interrupt Status Register */ +#define SPU_IRQ_ISR 0x1c4 +#define IRQ_DMA_FRAME0 BIT(31) +#define IRQ_DMA_FRAME1 BIT(30) +#define IRQ_DMA_FIFO_UNDERFLOW BIT(29) +#define IRQ_GRA_FRAME0 BIT(27) +#define IRQ_GRA_FRAME1 BIT(26) +#define IRQ_GRA_FIFO_UNDERFLOW BIT(25) +#define IRQ_SMART_VSYNC BIT(23) +#define IRQ_DUMB_FRAME_DONE BIT(22) +#define IRQ_SMART_FRAME_DONE BIT(21) +#define IRQ_HWCURSOR_FRAME_DONE BIT(20) +#define IRQ_AHB_CMD_EMPTY BIT(19) +#define IRQ_SPI_TRANSFER_DONE BIT(18) +#define IRQ_POWERDOWN BIT(17) +#define IRQ_AXI_ERROR BIT(16) +/* read only status */ +#define STA_DMA_FRAME0 BIT(15) +#define STA_DMA_FRAME1 BIT(14) +#define STA_DMA_FRAME_COUNT(x) (((x) & (BIT(13) | BIT(12))) >> 12) +#define STA_GRA_FRAME0 BIT(11) +#define STA_GRA_FRAME1 BIT(10) +#define STA_GRA_FRAME_COUNT(x) (((x) & (BIT(9) | BIT(8))) >> 8) +#define STA_SMART_VSYNC BIT(7) +#define STA_DUMB_FRAME_DONE BIT(6) +#define STA_SMART_FRAME_DONE BIT(5) +#define STA_HWCURSOR_FRAME_DONE BIT(4) +#define STA_AHB_CMD_EMPTY BIT(3) +#define STA_DMA_FIFO_EMPTY BIT(2) +#define STA_GRA_FIFO_EMPTY BIT(1) +#define STA_POWERDOWN BIT(0) + +#define IRQ_DMA_FRAME_DONE (IRQ_DMA_FRAME0 | IRQ_DMA_FRAME1) +#define IRQ_GRA_FRAME_DONE \ + (IRQ_GRA_FRAME0 | IRQ_GRA_FRAME1 | IRQ_SMART_VSYNC) + +/* + * defined Video Memory Color format for DMA control 0 register + * DMA0 bit[23:20] + */ +#define VMODE_RGB565 0x0 +#define VMODE_RGB1555 0x1 +#define VMODE_RGB888PACKED 0x2 +#define VMODE_RGB888UNPACKED 0x3 +#define VMODE_RGBA888 0x4 +#define VMODE_YUV422PACKED 0x5 +#define VMODE_YUV422PLANAR 0x6 +#define VMODE_YUV420PLANAR 0x7 +#define VMODE_SMPNCMD 0x8 +#define VMODE_PALETTE4BIT 0x9 +#define VMODE_PALETTE8BIT 0xa +#define VMODE_RESERVED 0xb + +/* + * defined Graphic Memory Color format for DMA control 0 register + * DMA0 bit[19:16] + */ +#define GMODE_RGB565 0x0 +#define GMODE_RGB1555 0x1 +#define GMODE_RGB888PACKED 0x2 +#define GMODE_RGB888UNPACKED 0x3 +#define GMODE_RGBA888 0x4 +#define GMODE_YUV422PACKED 0x5 +#define GMODE_YUV422PLANAR 0x6 +#define GMODE_YUV420PLANAR 0x7 +#define GMODE_SMPNCMD 0x8 +#define GMODE_PALETTE4BIT 0x9 +#define GMODE_PALETTE8BIT 0xa +#define GMODE_RESERVED 0xb + +/* + * define for DMA control 1 register + */ +#define DMA1_FRAME_TRIG 31 /* bit location */ +#define DMA1_VSYNC_MODE 28 +#define DMA1_VSYNC_INV 27 +#define DMA1_CKEY 24 +#define DMA1_CARRY 23 +#define DMA1_LNBUF_ENA 22 +#define DMA1_GATED_ENA 21 +#define DMA1_PWRDN_ENA 20 +#define DMA1_DSCALE 18 +#define DMA1_ALPHA_MODE 16 +#define DMA1_ALPHA 8 +#define DMA1_PXLCMD 0 + +/* + * defined for Configure Dumb Mode + * DUMB LCD Panel bit[31:28] + */ +#define DUMB16_RGB565_0 0x0 +#define DUMB16_RGB565_1 0x1 +#define DUMB18_RGB666_0 0x2 +#define DUMB18_RGB666_1 0x3 +#define DUMB12_RGB444_0 0x4 +#define DUMB12_RGB444_1 0x5 +#define DUMB24_RGB888_0 0x6 +#define DUMB_BLANK 0x7 + +/* + * defined for Configure I/O Pin Allocation Mode + * LCD LCD I/O Pads control register bit[3:0] + */ +#define IOPAD_DUMB24 0x0 +#define IOPAD_DUMB18SPI 0x1 +#define IOPAD_DUMB18GPIO 0x2 +#define IOPAD_DUMB16SPI 0x3 +#define IOPAD_DUMB16GPIO 0x4 +#define IOPAD_DUMB12 0x5 +#define IOPAD_SMART18SPI 0x6 +#define IOPAD_SMART16SPI 0x7 +#define IOPAD_SMART8BOTH 0x8 + +/* + * clock source SCLK_Source bit[31:30] + */ +#define SCLK_SRC_AXI 0 +#define SCLK_SRC_EXTCLK0 1 +#define SCLK_SRC_PLLDIV 2 +#define SCLK_SRC_EXTCLK1 3 + +/* + * defined Dumb Panel Clock Divider register + * SCLK_Source bit[31] + */ +#define AXI_BUS_SEL 0x80000000 /* 0: PLL clock select*/ +#define CCD_CLK_SEL 0x40000000 +#define DCON_CLK_SEL 0x20000000 +#define IDLE_CLK_INT_DIV 0x1 /* idle Integer Divider */ +#define DIS_CLK_INT_DIV 0x0 /* Disable Integer Divider */ + +/* SRAM ID */ +#define SRAMID_GAMMA_YR 0x0 +#define SRAMID_GAMMA_UG 0x1 +#define SRAMID_GAMMA_VB 0x2 +#define SRAMID_PALETTE 0x3 +#define SRAMID_HWC32_RAM1 0xc +#define SRAMID_HWC32_RAM2 0xd +#define SRAMID_HWC32_RAM3 0xe +#define SRAMID_HWC 0xf + +/* SRAM INIT Read/Write */ +#define SRAMID_INIT_READ 0x0 +#define SRAMID_INIT_WRITE 0x2 +#define SRAMID_INIT_DEFAULT 0x3 + +/* + * defined VSYNC selection mode for DMA control 1 register + * DMA1 bit[30:28] + */ +#define VMODE_SMPN 0x0 +#define VMODE_SMPNIRQ 0x1 +#define VMODE_DUMB 0x2 +#define VMODE_IPE 0x3 +#define VMODE_IRE 0x4 + +/* + * defined Configure Alpha and Alpha mode for DMA control 1 register + * DMA1 bit[15:08](alpha) / bit[17:16](alpha mode) + */ +/* ALPHA mode */ +#define MODE_ALPHA_DMA 0xa0 +#define MODE_ALPHA_GRA 0x1 +#define MODE_ALPHA_CFG 0x2 + +/* alpha value */ +#define ALPHA_NOGRAPHIC 0xff /* all video, no graphic */ +#define ALPHA_NOVIDEO 0x00 /* all graphic, no video */ +#define ALPHA_GRAPHnVIDEO 0x0f /* Selects graphic & video */ + +/* + * defined Pixel Command for DMA control 1 register + * DMA1 bit[07:00] + */ +#define PIXEL_CMD 0x81 + +#endif /* _DOVE_LCD_H_ */ diff --git a/sound/soc/codecs/tda998x.c b/sound/soc/codecs/tda998x.c new file mode 100644 index 0000000..1f53592 --- /dev/null +++ b/sound/soc/codecs/tda998x.c @@ -0,0 +1,145 @@ +/* + * ALSA SoC TDA998x CODEC + * + * Copyright (C) 2015 Jean-Francois Moine + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include + +static int tda998x_codec_startup(struct snd_pcm_substream *substream, + struct snd_soc_dai *dai) +{ + struct snd_pcm_runtime *runtime = substream->runtime; + struct tda998x_audio *tda998x_audio = dev_get_drvdata(dai->dev); + u8 *eld; + + eld = tda998x_audio->eld; + if (!eld) + return -ENODEV; + return snd_pcm_hw_constraint_eld(runtime, eld); +} + +/* ask the HDMI transmitter to activate the audio input port */ +static int tda998x_codec_hw_params(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct snd_soc_dai *dai) +{ + struct tda998x_audio *tda998x_audio = dev_get_drvdata(dai->dev); + + return tda998x_audio->set_audio_input(dai->dev, dai->id, + params_rate(params)); +} + +static void tda998x_codec_shutdown(struct snd_pcm_substream *substream, + struct snd_soc_dai *dai) +{ + struct tda998x_audio *tda998x_audio = dev_get_drvdata(dai->dev); + + tda998x_audio->set_audio_input(dai->dev, PORT_NONE, 0); +} + +static const struct snd_soc_dai_ops tda998x_codec_ops = { + .startup = tda998x_codec_startup, + .hw_params = tda998x_codec_hw_params, + .shutdown = tda998x_codec_shutdown, +}; + +#define TDA998X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \ + SNDRV_PCM_FMTBIT_S20_3LE | \ + SNDRV_PCM_FMTBIT_S24_LE | \ + SNDRV_PCM_FMTBIT_S32_LE) + +static const struct snd_soc_dai_driver tda998x_dai_i2s = { + .name = "i2s-hifi", + .playback = { + .stream_name = "HDMI I2S Playback", + .channels_min = 1, + .channels_max = 8, + .rates = SNDRV_PCM_RATE_CONTINUOUS, + .rate_min = 5512, + .rate_max = 192000, + .formats = TDA998X_FORMATS, + }, + .ops = &tda998x_codec_ops, +}; +static const struct snd_soc_dai_driver tda998x_dai_spdif = { + .name = "spdif-hifi", + .playback = { + .stream_name = "HDMI SPDIF Playback", + .channels_min = 1, + .channels_max = 2, + .rates = SNDRV_PCM_RATE_CONTINUOUS, + .rate_min = 22050, + .rate_max = 192000, + .formats = TDA998X_FORMATS, + }, + .ops = &tda998x_codec_ops, +}; + +static const struct snd_soc_dapm_widget tda998x_widgets[] = { + SND_SOC_DAPM_OUTPUT("hdmi-out"), +}; +static const struct snd_soc_dapm_route tda998x_routes[] = { + { "hdmi-out", NULL, "HDMI I2S Playback" }, + { "hdmi-out", NULL, "HDMI SPDIF Playback" }, +}; + +static struct snd_soc_codec_driver tda998x_codec_drv = { + .dapm_widgets = tda998x_widgets, + .num_dapm_widgets = ARRAY_SIZE(tda998x_widgets), + .dapm_routes = tda998x_routes, + .num_dapm_routes = ARRAY_SIZE(tda998x_routes), + .ignore_pmdown_time = true, +}; + +int tda9998x_codec_register(struct device *dev) +{ + struct snd_soc_dai_driver *dais, *p_dai; + struct tda998x_audio *tda998x_audio = dev_get_drvdata(dev); + int i, ndais; + + /* build the DAIs */ + for (ndais = 0; ndais < ARRAY_SIZE(tda998x_audio->ports); ndais++) { + if (!tda998x_audio->ports[ndais]) + break; + } +//test +//pr_info("tda998x codec %d dais\n", ndais); + dais = devm_kzalloc(dev, sizeof(*dais) * ndais, GFP_KERNEL); + if (!dais) + return -ENOMEM; + for (i = 0, p_dai = dais; i < ndais ; i++, p_dai++) { + if (tda998x_audio->port_types[i] == AFMT_I2S) + memcpy(p_dai, &tda998x_dai_i2s, sizeof(*p_dai)); + else + memcpy(p_dai, &tda998x_dai_spdif, sizeof(*p_dai)); + p_dai->id = i; +//test +//pr_info("tda998x codec %d %s\n", p_dai->id, p_dai->name); + } + + return snd_soc_register_codec(dev, + &tda998x_codec_drv, + dais, ndais); +} +EXPORT_SYMBOL_GPL(tda9998x_codec_register); + +void tda9998x_codec_unregister(struct device *dev) +{ + snd_soc_unregister_codec(dev); +} +EXPORT_SYMBOL_GPL(tda9998x_codec_unregister); + +MODULE_AUTHOR("Jean-Francois Moine "); +MODULE_DESCRIPTION("TDA998X CODEC"); +MODULE_LICENSE("GPL"); diff --git a/sound/soc/generic/dt-card.c b/sound/soc/generic/dt-card.c new file mode 100644 index 0000000..0d388e1 --- /dev/null +++ b/sound/soc/generic/dt-card.c @@ -0,0 +1,285 @@ +/* + * ALSA SoC DT based sound card support + * + * Copyright (C) 2015 Jean-Francois Moine + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include + +/* check if a node is an audio port */ +static int asoc_dt_card_is_audio_port(struct device_node *of_port) +{ + const char *name; + int ret; + + if (!of_port->name || + of_node_cmp(of_port->name, "port") != 0) + return 0; + ret = of_property_read_string(of_port, + "port-type", + &name); + if (!ret && + (strcmp(name, "i2s") == 0 || + strcmp(name, "spdif") == 0)) + return 1; + return 0; +} + +/* + * Get the DAI number from the DT by counting the audio ports + * of the remote device node (codec). + */ +static int asoc_dt_card_get_dai_number(struct device_node *of_codec, + struct device_node *of_remote_endpoint) +{ + struct device_node *of_port, *of_endpoint; + int ndai; + + ndai = 0; + for_each_child_of_node(of_codec, of_port) { + if (!asoc_dt_card_is_audio_port(of_port)) + continue; + for_each_child_of_node(of_port, of_endpoint) { + if (!of_endpoint->name || + of_node_cmp(of_endpoint->name, "endpoint") != 0) + continue; + if (of_endpoint == of_remote_endpoint) { + of_node_put(of_port); + of_node_put(of_endpoint); + return ndai; + } + } + ndai++; + } + return 0; /* should never be reached */ +} + +/* + * Parse a graph of audio ports + * @dev: Card device + * @of_cpu: Device node of the audio controller + * @card: Card definition + * + * Builds the DAI links of the card from the DT graph of audio ports + * starting from the audio controller. + * It does not handle the port groups. + * The CODEC device nodes in the DAI links must be dereferenced by the caller. + * + * Returns the number of DAI links or (< 0) on error + */ +static int asoc_dt_card_of_parse_graph(struct device *dev, + struct device_node *of_cpu, + struct snd_soc_card *card) +{ + struct device_node *of_codec, *of_port, *of_endpoint, + *of_remote_endpoint; + struct snd_soc_dai_link *link; + struct snd_soc_dai_link_component *component; + struct of_phandle_args args, args2; + int ret, ilink, icodec, nlinks, ncodecs; + +//test +//pr_info("dt-card cpu %s\n", of_cpu->name ? of_cpu->name : "(null)"); + /* count the number of DAI links */ + nlinks = 0; + for_each_child_of_node(of_cpu, of_port) { +//test +//pr_info("dt-card cpu child %s\n", of_port->name ? of_port->name : "(null)"); + if (asoc_dt_card_is_audio_port(of_port)) + nlinks++; + } +//test +if (nlinks == 0) pr_info("dt-card no link!\n"); + + /* allocate the DAI link array */ + link = devm_kzalloc(dev, sizeof(*link) * nlinks, GFP_KERNEL); + if (!link) + return -ENOMEM; + card->dai_link = link; + + /* build the DAI links */ + ilink = 0; + args.np = of_cpu; + args.args_count = 1; + for_each_child_of_node(of_cpu, of_port) { + if (!asoc_dt_card_is_audio_port(of_port)) + continue; + + link->platform_of_node = + link->cpu_of_node = of_cpu; + args.args[0] = ilink; + ret = snd_soc_get_dai_name(&args, &link->cpu_dai_name); + if (ret) { + dev_err(dev, "no CPU DAI name for link %d!\n", + ilink); + continue; + } + + /* count the number of codecs of this DAI link */ + ncodecs = 0; + for_each_child_of_node(of_port, of_endpoint) { + if (of_parse_phandle(of_endpoint, + "remote-endpoint", 0)) + ncodecs++; + } + if (ncodecs == 0) + continue; + component = devm_kzalloc(dev, + sizeof(*component) * ncodecs, + GFP_KERNEL); + if (!component) + return -ENOMEM; + link->codecs = component; + + icodec = 0; + args2.args_count = 1; + for_each_child_of_node(of_port, of_endpoint) { + of_remote_endpoint = of_parse_phandle(of_endpoint, + "remote-endpoint", 0); + if (!of_remote_endpoint) + continue; + component->of_node = of_codec = + of_remote_endpoint->parent->parent; + args2.np = of_codec; + args2.args[0] = asoc_dt_card_get_dai_number(of_codec, + of_remote_endpoint); + ret = snd_soc_get_dai_name(&args2, + &component->dai_name); + if (ret) { + if (ret == -EPROBE_DEFER) { + card->num_links = ilink + 1; + link->num_codecs = icodec; +//test +//pr_info("dt-card probe defer\n"); + return ret; + } + dev_err(dev, + "no CODEC DAI name for link %d\n", + ilink); + continue; + } +//test +//pr_info("dt-card codec %d %s\n", args2.args[0], component->dai_name); + of_node_get(of_codec); + + icodec++; + if (icodec >= ncodecs) + break; + component++; + } + if (icodec == 0) + continue; + link->num_codecs = icodec; + + ilink++; + if (ilink >= nlinks) + break; + link++; + } + card->num_links = ilink; + + return ilink; +} + +static void asoc_dt_card_unref(struct platform_device *pdev) +{ + struct snd_soc_card *card = platform_get_drvdata(pdev); + struct snd_soc_dai_link *link; + int nlinks, ncodecs; + + if (card) { + for (nlinks = 0, link = card->dai_link; + nlinks < card->num_links; + nlinks++, link++) { + for (ncodecs = 0; + ncodecs < link->num_codecs; + ncodecs++) + of_node_put(card->dai_link->codecs[ncodecs].of_node); + } + } +} + +/* + * The platform data contains the pointer to the device node + * which starts the description of the graph of the audio ports, + * This device node is usually the audio controller. + */ +static int asoc_dt_card_probe(struct platform_device *pdev) +{ + struct device_node **p_np = pdev->dev.platform_data; + struct device_node *of_cpu = *p_np; + struct snd_soc_card *card; + struct snd_soc_dai_link *link; + char *name; + int ret, i; + + card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL); + if (!card) + return -ENOMEM; + ret = asoc_dt_card_of_parse_graph(&pdev->dev, of_cpu, card); + if (ret < 0) + goto err; + + /* fill the remaining values of the card */ + card->owner = THIS_MODULE; + card->dev = &pdev->dev; + card->name = "DT-card"; + for (i = 0, link = card->dai_link; + i < card->num_links; + i++, link++) { + name = devm_kzalloc(&pdev->dev, + strlen(link->cpu_dai_name) + + strlen(link->codecs[0].dai_name) + + 2, + GFP_KERNEL); + if (!name) { + ret = -ENOMEM; + goto err; + } + sprintf(name, "%s-%s", link->cpu_dai_name, + link->codecs[0].dai_name); + link->name = link->stream_name = name; + } + + card->dai_link->dai_fmt = + snd_soc_of_parse_daifmt(of_cpu, "dt-audio-card,", + NULL, NULL) & + ~SND_SOC_DAIFMT_MASTER_MASK; + + ret = devm_snd_soc_register_card(&pdev->dev, card); + if (ret >= 0) + return ret; + +err: + asoc_dt_card_unref(pdev); + return ret; +} + +static int asoc_dt_card_remove(struct platform_device *pdev) +{ + asoc_dt_card_unref(pdev); + snd_soc_unregister_card(platform_get_drvdata(pdev)); + return 0; +} + +static struct platform_driver asoc_dt_card = { + .driver = { + .name = "asoc-dt-card", + }, + .probe = asoc_dt_card_probe, + .remove = asoc_dt_card_remove, +}; + +module_platform_driver(asoc_dt_card); + +MODULE_ALIAS("platform:asoc-dt-card"); +MODULE_DESCRIPTION("ASoC DT Sound Card"); +MODULE_AUTHOR("Jean-Francois Moine "); +MODULE_LICENSE("GPL");