144cfc623SGuido Günther // SPDX-License-Identifier: GPL-2.0+
244cfc623SGuido Günther /*
344cfc623SGuido Günther * i.MX8 NWL MIPI DSI host driver
444cfc623SGuido Günther *
544cfc623SGuido Günther * Copyright (C) 2017 NXP
644cfc623SGuido Günther * Copyright (C) 2020 Purism SPC
744cfc623SGuido Günther */
844cfc623SGuido Günther
944cfc623SGuido Günther #include <linux/bitfield.h>
109a8406baSLiu Ying #include <linux/bits.h>
1144cfc623SGuido Günther #include <linux/clk.h>
1244cfc623SGuido Günther #include <linux/irq.h>
1344cfc623SGuido Günther #include <linux/math64.h>
1444cfc623SGuido Günther #include <linux/mfd/syscon.h>
1572bd9ea3SVille Syrjälä #include <linux/media-bus-format.h>
1644cfc623SGuido Günther #include <linux/module.h>
1744cfc623SGuido Günther #include <linux/mux/consumer.h>
1844cfc623SGuido Günther #include <linux/of.h>
1944cfc623SGuido Günther #include <linux/phy/phy.h>
20*722d4f06SRob Herring #include <linux/platform_device.h>
2144cfc623SGuido Günther #include <linux/regmap.h>
2244cfc623SGuido Günther #include <linux/reset.h>
2344cfc623SGuido Günther #include <linux/sys_soc.h>
2444cfc623SGuido Günther #include <linux/time64.h>
2544cfc623SGuido Günther
263afb2a28SLiu Ying #include <drm/drm_atomic_state_helper.h>
2744cfc623SGuido Günther #include <drm/drm_bridge.h>
2844cfc623SGuido Günther #include <drm/drm_mipi_dsi.h>
2944cfc623SGuido Günther #include <drm/drm_of.h>
3044cfc623SGuido Günther #include <drm/drm_print.h>
3144cfc623SGuido Günther
3244cfc623SGuido Günther #include <video/mipi_display.h>
3344cfc623SGuido Günther
3444cfc623SGuido Günther #include "nwl-dsi.h"
3544cfc623SGuido Günther
3644cfc623SGuido Günther #define DRV_NAME "nwl-dsi"
3744cfc623SGuido Günther
3844cfc623SGuido Günther /* i.MX8 NWL quirks */
3944cfc623SGuido Günther /* i.MX8MQ errata E11418 */
4044cfc623SGuido Günther #define E11418_HS_MODE_QUIRK BIT(0)
4144cfc623SGuido Günther
4244cfc623SGuido Günther #define NWL_DSI_MIPI_FIFO_TIMEOUT msecs_to_jiffies(500)
4344cfc623SGuido Günther
4444cfc623SGuido Günther enum transfer_direction {
4544cfc623SGuido Günther DSI_PACKET_SEND,
4644cfc623SGuido Günther DSI_PACKET_RECEIVE,
4744cfc623SGuido Günther };
4844cfc623SGuido Günther
4944cfc623SGuido Günther #define NWL_DSI_ENDPOINT_LCDIF 0
5044cfc623SGuido Günther #define NWL_DSI_ENDPOINT_DCSS 1
5144cfc623SGuido Günther
5244cfc623SGuido Günther struct nwl_dsi_transfer {
5344cfc623SGuido Günther const struct mipi_dsi_msg *msg;
5444cfc623SGuido Günther struct mipi_dsi_packet packet;
5544cfc623SGuido Günther struct completion completed;
5644cfc623SGuido Günther
5744cfc623SGuido Günther int status; /* status of transmission */
5844cfc623SGuido Günther enum transfer_direction direction;
5944cfc623SGuido Günther bool need_bta;
6044cfc623SGuido Günther u8 cmd;
6144cfc623SGuido Günther u16 rx_word_count;
6244cfc623SGuido Günther size_t tx_len; /* in bytes */
6344cfc623SGuido Günther size_t rx_len; /* in bytes */
6444cfc623SGuido Günther };
6544cfc623SGuido Günther
6644cfc623SGuido Günther struct nwl_dsi {
6744cfc623SGuido Günther struct drm_bridge bridge;
6844cfc623SGuido Günther struct mipi_dsi_host dsi_host;
6944cfc623SGuido Günther struct device *dev;
7044cfc623SGuido Günther struct phy *phy;
7144cfc623SGuido Günther union phy_configure_opts phy_cfg;
7244cfc623SGuido Günther unsigned int quirks;
7344cfc623SGuido Günther
7444cfc623SGuido Günther struct regmap *regmap;
7544cfc623SGuido Günther int irq;
7644cfc623SGuido Günther /*
7744cfc623SGuido Günther * The DSI host controller needs this reset sequence according to NWL:
7844cfc623SGuido Günther * 1. Deassert pclk reset to get access to DSI regs
7944cfc623SGuido Günther * 2. Configure DSI Host and DPHY and enable DPHY
8044cfc623SGuido Günther * 3. Deassert ESC and BYTE resets to allow host TX operations)
8144cfc623SGuido Günther * 4. Send DSI cmds to configure peripheral (handled by panel drv)
8244cfc623SGuido Günther * 5. Deassert DPI reset so DPI receives pixels and starts sending
8344cfc623SGuido Günther * DSI data
8444cfc623SGuido Günther *
8544cfc623SGuido Günther * TODO: Since panel_bridges do their DSI setup in enable we
8644cfc623SGuido Günther * currently have 4. and 5. swapped.
8744cfc623SGuido Günther */
8844cfc623SGuido Günther struct reset_control *rst_byte;
8944cfc623SGuido Günther struct reset_control *rst_esc;
9044cfc623SGuido Günther struct reset_control *rst_dpi;
9144cfc623SGuido Günther struct reset_control *rst_pclk;
9244cfc623SGuido Günther struct mux_control *mux;
9344cfc623SGuido Günther
9444cfc623SGuido Günther /* DSI clocks */
9544cfc623SGuido Günther struct clk *phy_ref_clk;
9644cfc623SGuido Günther struct clk *rx_esc_clk;
9744cfc623SGuido Günther struct clk *tx_esc_clk;
9844cfc623SGuido Günther struct clk *core_clk;
9944cfc623SGuido Günther /*
10044cfc623SGuido Günther * hardware bug: the i.MX8MQ needs this clock on during reset
10144cfc623SGuido Günther * even when not using LCDIF.
10244cfc623SGuido Günther */
10344cfc623SGuido Günther struct clk *lcdif_clk;
10444cfc623SGuido Günther
10544cfc623SGuido Günther /* dsi lanes */
10644cfc623SGuido Günther u32 lanes;
10744cfc623SGuido Günther enum mipi_dsi_pixel_format format;
10844cfc623SGuido Günther struct drm_display_mode mode;
10944cfc623SGuido Günther unsigned long dsi_mode_flags;
11044cfc623SGuido Günther int error;
11144cfc623SGuido Günther
11244cfc623SGuido Günther struct nwl_dsi_transfer *xfer;
11344cfc623SGuido Günther };
11444cfc623SGuido Günther
11544cfc623SGuido Günther static const struct regmap_config nwl_dsi_regmap_config = {
11644cfc623SGuido Günther .reg_bits = 16,
11744cfc623SGuido Günther .val_bits = 32,
11844cfc623SGuido Günther .reg_stride = 4,
11944cfc623SGuido Günther .max_register = NWL_DSI_IRQ_MASK2,
12044cfc623SGuido Günther .name = DRV_NAME,
12144cfc623SGuido Günther };
12244cfc623SGuido Günther
bridge_to_dsi(struct drm_bridge * bridge)12344cfc623SGuido Günther static inline struct nwl_dsi *bridge_to_dsi(struct drm_bridge *bridge)
12444cfc623SGuido Günther {
12544cfc623SGuido Günther return container_of(bridge, struct nwl_dsi, bridge);
12644cfc623SGuido Günther }
12744cfc623SGuido Günther
nwl_dsi_clear_error(struct nwl_dsi * dsi)12844cfc623SGuido Günther static int nwl_dsi_clear_error(struct nwl_dsi *dsi)
12944cfc623SGuido Günther {
13044cfc623SGuido Günther int ret = dsi->error;
13144cfc623SGuido Günther
13244cfc623SGuido Günther dsi->error = 0;
13344cfc623SGuido Günther return ret;
13444cfc623SGuido Günther }
13544cfc623SGuido Günther
nwl_dsi_write(struct nwl_dsi * dsi,unsigned int reg,u32 val)13644cfc623SGuido Günther static void nwl_dsi_write(struct nwl_dsi *dsi, unsigned int reg, u32 val)
13744cfc623SGuido Günther {
13844cfc623SGuido Günther int ret;
13944cfc623SGuido Günther
14044cfc623SGuido Günther if (dsi->error)
14144cfc623SGuido Günther return;
14244cfc623SGuido Günther
14344cfc623SGuido Günther ret = regmap_write(dsi->regmap, reg, val);
14444cfc623SGuido Günther if (ret < 0) {
14544cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev,
14644cfc623SGuido Günther "Failed to write NWL DSI reg 0x%x: %d\n", reg,
14744cfc623SGuido Günther ret);
14844cfc623SGuido Günther dsi->error = ret;
14944cfc623SGuido Günther }
15044cfc623SGuido Günther }
15144cfc623SGuido Günther
nwl_dsi_read(struct nwl_dsi * dsi,u32 reg)15244cfc623SGuido Günther static u32 nwl_dsi_read(struct nwl_dsi *dsi, u32 reg)
15344cfc623SGuido Günther {
15444cfc623SGuido Günther unsigned int val;
15544cfc623SGuido Günther int ret;
15644cfc623SGuido Günther
15744cfc623SGuido Günther if (dsi->error)
15844cfc623SGuido Günther return 0;
15944cfc623SGuido Günther
16044cfc623SGuido Günther ret = regmap_read(dsi->regmap, reg, &val);
16144cfc623SGuido Günther if (ret < 0) {
16244cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to read NWL DSI reg 0x%x: %d\n",
16344cfc623SGuido Günther reg, ret);
16444cfc623SGuido Günther dsi->error = ret;
16544cfc623SGuido Günther }
16644cfc623SGuido Günther return val;
16744cfc623SGuido Günther }
16844cfc623SGuido Günther
nwl_dsi_get_dpi_pixel_format(enum mipi_dsi_pixel_format format)16944cfc623SGuido Günther static int nwl_dsi_get_dpi_pixel_format(enum mipi_dsi_pixel_format format)
17044cfc623SGuido Günther {
17144cfc623SGuido Günther switch (format) {
17244cfc623SGuido Günther case MIPI_DSI_FMT_RGB565:
17344cfc623SGuido Günther return NWL_DSI_PIXEL_FORMAT_16;
17444cfc623SGuido Günther case MIPI_DSI_FMT_RGB666:
17544cfc623SGuido Günther return NWL_DSI_PIXEL_FORMAT_18L;
17644cfc623SGuido Günther case MIPI_DSI_FMT_RGB666_PACKED:
17744cfc623SGuido Günther return NWL_DSI_PIXEL_FORMAT_18;
17844cfc623SGuido Günther case MIPI_DSI_FMT_RGB888:
17944cfc623SGuido Günther return NWL_DSI_PIXEL_FORMAT_24;
18044cfc623SGuido Günther default:
18144cfc623SGuido Günther return -EINVAL;
18244cfc623SGuido Günther }
18344cfc623SGuido Günther }
18444cfc623SGuido Günther
18544cfc623SGuido Günther /*
18644cfc623SGuido Günther * ps2bc - Picoseconds to byte clock cycles
18744cfc623SGuido Günther */
ps2bc(struct nwl_dsi * dsi,unsigned long long ps)18844cfc623SGuido Günther static u32 ps2bc(struct nwl_dsi *dsi, unsigned long long ps)
18944cfc623SGuido Günther {
19044cfc623SGuido Günther u32 bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
19144cfc623SGuido Günther
19244cfc623SGuido Günther return DIV64_U64_ROUND_UP(ps * dsi->mode.clock * bpp,
19347956bc8SGeert Uytterhoeven dsi->lanes * 8ULL * NSEC_PER_SEC);
19444cfc623SGuido Günther }
19544cfc623SGuido Günther
19644cfc623SGuido Günther /*
19744cfc623SGuido Günther * ui2bc - UI time periods to byte clock cycles
19844cfc623SGuido Günther */
ui2bc(unsigned int ui)1999a8406baSLiu Ying static u32 ui2bc(unsigned int ui)
20044cfc623SGuido Günther {
2019a8406baSLiu Ying return DIV_ROUND_UP(ui, BITS_PER_BYTE);
20244cfc623SGuido Günther }
20344cfc623SGuido Günther
20444cfc623SGuido Günther /*
20544cfc623SGuido Günther * us2bc - micro seconds to lp clock cycles
20644cfc623SGuido Günther */
us2lp(u32 lp_clk_rate,unsigned long us)20744cfc623SGuido Günther static u32 us2lp(u32 lp_clk_rate, unsigned long us)
20844cfc623SGuido Günther {
20944cfc623SGuido Günther return DIV_ROUND_UP(us * lp_clk_rate, USEC_PER_SEC);
21044cfc623SGuido Günther }
21144cfc623SGuido Günther
nwl_dsi_config_host(struct nwl_dsi * dsi)21244cfc623SGuido Günther static int nwl_dsi_config_host(struct nwl_dsi *dsi)
21344cfc623SGuido Günther {
21444cfc623SGuido Günther u32 cycles;
21544cfc623SGuido Günther struct phy_configure_opts_mipi_dphy *cfg = &dsi->phy_cfg.mipi_dphy;
21644cfc623SGuido Günther
21744cfc623SGuido Günther if (dsi->lanes < 1 || dsi->lanes > 4)
21844cfc623SGuido Günther return -EINVAL;
21944cfc623SGuido Günther
22044cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "DSI Lanes %d\n", dsi->lanes);
22144cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_NUM_LANES, dsi->lanes - 1);
22244cfc623SGuido Günther
22344cfc623SGuido Günther if (dsi->dsi_mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) {
22444cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_NONCONTINUOUS_CLK, 0x01);
22544cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_AUTOINSERT_EOTP, 0x01);
22644cfc623SGuido Günther } else {
22744cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_NONCONTINUOUS_CLK, 0x00);
22844cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_AUTOINSERT_EOTP, 0x00);
22944cfc623SGuido Günther }
23044cfc623SGuido Günther
23144cfc623SGuido Günther /* values in byte clock cycles */
2329a8406baSLiu Ying cycles = ui2bc(cfg->clk_pre);
23344cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "cfg_t_pre: 0x%x\n", cycles);
23444cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_T_PRE, cycles);
23544cfc623SGuido Günther cycles = ps2bc(dsi, cfg->lpx + cfg->clk_prepare + cfg->clk_zero);
23644cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "cfg_tx_gap (pre): 0x%x\n", cycles);
2379a8406baSLiu Ying cycles += ui2bc(cfg->clk_pre);
23844cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "cfg_t_post: 0x%x\n", cycles);
23944cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_T_POST, cycles);
24044cfc623SGuido Günther cycles = ps2bc(dsi, cfg->hs_exit);
24144cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "cfg_tx_gap: 0x%x\n", cycles);
24244cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_TX_GAP, cycles);
24344cfc623SGuido Günther
24444cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_EXTRA_CMDS_AFTER_EOTP, 0x01);
24544cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_HTX_TO_COUNT, 0x00);
24644cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_LRX_H_TO_COUNT, 0x00);
24744cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_BTA_H_TO_COUNT, 0x00);
24844cfc623SGuido Günther /* In LP clock cycles */
24944cfc623SGuido Günther cycles = us2lp(cfg->lp_clk_rate, cfg->wakeup);
25044cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "cfg_twakeup: 0x%x\n", cycles);
25144cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_CFG_TWAKEUP, cycles);
25244cfc623SGuido Günther
25344cfc623SGuido Günther return nwl_dsi_clear_error(dsi);
25444cfc623SGuido Günther }
25544cfc623SGuido Günther
nwl_dsi_config_dpi(struct nwl_dsi * dsi)25644cfc623SGuido Günther static int nwl_dsi_config_dpi(struct nwl_dsi *dsi)
25744cfc623SGuido Günther {
25844cfc623SGuido Günther u32 mode;
25944cfc623SGuido Günther int color_format;
26044cfc623SGuido Günther bool burst_mode;
26144cfc623SGuido Günther int hfront_porch, hback_porch, vfront_porch, vback_porch;
26244cfc623SGuido Günther int hsync_len, vsync_len;
26344cfc623SGuido Günther
26444cfc623SGuido Günther hfront_porch = dsi->mode.hsync_start - dsi->mode.hdisplay;
26544cfc623SGuido Günther hsync_len = dsi->mode.hsync_end - dsi->mode.hsync_start;
26644cfc623SGuido Günther hback_porch = dsi->mode.htotal - dsi->mode.hsync_end;
26744cfc623SGuido Günther
26844cfc623SGuido Günther vfront_porch = dsi->mode.vsync_start - dsi->mode.vdisplay;
26944cfc623SGuido Günther vsync_len = dsi->mode.vsync_end - dsi->mode.vsync_start;
27044cfc623SGuido Günther vback_porch = dsi->mode.vtotal - dsi->mode.vsync_end;
27144cfc623SGuido Günther
27244cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "hfront_porch = %d\n", hfront_porch);
27344cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "hback_porch = %d\n", hback_porch);
27444cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "hsync_len = %d\n", hsync_len);
27544cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "hdisplay = %d\n", dsi->mode.hdisplay);
27644cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "vfront_porch = %d\n", vfront_porch);
27744cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "vback_porch = %d\n", vback_porch);
27844cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "vsync_len = %d\n", vsync_len);
27944cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "vactive = %d\n", dsi->mode.vdisplay);
28044cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "clock = %d kHz\n", dsi->mode.clock);
28144cfc623SGuido Günther
28244cfc623SGuido Günther color_format = nwl_dsi_get_dpi_pixel_format(dsi->format);
28344cfc623SGuido Günther if (color_format < 0) {
28444cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Invalid color format 0x%x\n",
28544cfc623SGuido Günther dsi->format);
28644cfc623SGuido Günther return color_format;
28744cfc623SGuido Günther }
28844cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "pixel fmt = %d\n", dsi->format);
28944cfc623SGuido Günther
29044cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_INTERFACE_COLOR_CODING, NWL_DSI_DPI_24_BIT);
29144cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_PIXEL_FORMAT, color_format);
29244cfc623SGuido Günther /*
29344cfc623SGuido Günther * Adjusting input polarity based on the video mode results in
29444cfc623SGuido Günther * a black screen so always pick active low:
29544cfc623SGuido Günther */
29644cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_VSYNC_POLARITY,
29744cfc623SGuido Günther NWL_DSI_VSYNC_POLARITY_ACTIVE_LOW);
29844cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_HSYNC_POLARITY,
29944cfc623SGuido Günther NWL_DSI_HSYNC_POLARITY_ACTIVE_LOW);
30044cfc623SGuido Günther
30144cfc623SGuido Günther burst_mode = (dsi->dsi_mode_flags & MIPI_DSI_MODE_VIDEO_BURST) &&
30244cfc623SGuido Günther !(dsi->dsi_mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE);
30344cfc623SGuido Günther
30444cfc623SGuido Günther if (burst_mode) {
30544cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_VIDEO_MODE, NWL_DSI_VM_BURST_MODE);
30644cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_PIXEL_FIFO_SEND_LEVEL, 256);
30744cfc623SGuido Günther } else {
30844cfc623SGuido Günther mode = ((dsi->dsi_mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) ?
30944cfc623SGuido Günther NWL_DSI_VM_BURST_MODE_WITH_SYNC_PULSES :
31044cfc623SGuido Günther NWL_DSI_VM_NON_BURST_MODE_WITH_SYNC_EVENTS);
31144cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_VIDEO_MODE, mode);
31244cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_PIXEL_FIFO_SEND_LEVEL,
31344cfc623SGuido Günther dsi->mode.hdisplay);
31444cfc623SGuido Günther }
31544cfc623SGuido Günther
31644cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_HFP, hfront_porch);
31744cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_HBP, hback_porch);
31844cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_HSA, hsync_len);
31944cfc623SGuido Günther
32044cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_ENABLE_MULT_PKTS, 0x0);
32144cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_BLLP_MODE, 0x1);
32244cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_USE_NULL_PKT_BLLP, 0x0);
32344cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_VC, 0x0);
32444cfc623SGuido Günther
32544cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_PIXEL_PAYLOAD_SIZE, dsi->mode.hdisplay);
32644cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_VACTIVE, dsi->mode.vdisplay - 1);
32744cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_VBP, vback_porch);
32844cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_VFP, vfront_porch);
32944cfc623SGuido Günther
33044cfc623SGuido Günther return nwl_dsi_clear_error(dsi);
33144cfc623SGuido Günther }
33244cfc623SGuido Günther
nwl_dsi_init_interrupts(struct nwl_dsi * dsi)33344cfc623SGuido Günther static int nwl_dsi_init_interrupts(struct nwl_dsi *dsi)
33444cfc623SGuido Günther {
33515043036SLiu Ying u32 irq_enable = ~(u32)(NWL_DSI_TX_PKT_DONE_MASK |
33644cfc623SGuido Günther NWL_DSI_RX_PKT_HDR_RCVD_MASK |
33744cfc623SGuido Günther NWL_DSI_TX_FIFO_OVFLW_MASK |
33844cfc623SGuido Günther NWL_DSI_HS_TX_TIMEOUT_MASK);
33944cfc623SGuido Günther
34044cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_IRQ_MASK, irq_enable);
34115043036SLiu Ying nwl_dsi_write(dsi, NWL_DSI_IRQ_MASK2, 0x7);
34244cfc623SGuido Günther
34344cfc623SGuido Günther return nwl_dsi_clear_error(dsi);
34444cfc623SGuido Günther }
34544cfc623SGuido Günther
nwl_dsi_host_attach(struct mipi_dsi_host * dsi_host,struct mipi_dsi_device * device)34644cfc623SGuido Günther static int nwl_dsi_host_attach(struct mipi_dsi_host *dsi_host,
34744cfc623SGuido Günther struct mipi_dsi_device *device)
34844cfc623SGuido Günther {
34944cfc623SGuido Günther struct nwl_dsi *dsi = container_of(dsi_host, struct nwl_dsi, dsi_host);
35044cfc623SGuido Günther struct device *dev = dsi->dev;
35144cfc623SGuido Günther
35244cfc623SGuido Günther DRM_DEV_INFO(dev, "lanes=%u, format=0x%x flags=0x%lx\n", device->lanes,
35344cfc623SGuido Günther device->format, device->mode_flags);
35444cfc623SGuido Günther
35544cfc623SGuido Günther if (device->lanes < 1 || device->lanes > 4)
35644cfc623SGuido Günther return -EINVAL;
35744cfc623SGuido Günther
35844cfc623SGuido Günther dsi->lanes = device->lanes;
35944cfc623SGuido Günther dsi->format = device->format;
36044cfc623SGuido Günther dsi->dsi_mode_flags = device->mode_flags;
36144cfc623SGuido Günther
36244cfc623SGuido Günther return 0;
36344cfc623SGuido Günther }
36444cfc623SGuido Günther
nwl_dsi_read_packet(struct nwl_dsi * dsi,u32 status)36544cfc623SGuido Günther static bool nwl_dsi_read_packet(struct nwl_dsi *dsi, u32 status)
36644cfc623SGuido Günther {
36744cfc623SGuido Günther struct device *dev = dsi->dev;
36844cfc623SGuido Günther struct nwl_dsi_transfer *xfer = dsi->xfer;
36944cfc623SGuido Günther int err;
37044cfc623SGuido Günther u8 *payload = xfer->msg->rx_buf;
37144cfc623SGuido Günther u32 val;
37244cfc623SGuido Günther u16 word_count;
37344cfc623SGuido Günther u8 channel;
37444cfc623SGuido Günther u8 data_type;
37544cfc623SGuido Günther
37644cfc623SGuido Günther xfer->status = 0;
37744cfc623SGuido Günther
37844cfc623SGuido Günther if (xfer->rx_word_count == 0) {
37944cfc623SGuido Günther if (!(status & NWL_DSI_RX_PKT_HDR_RCVD))
38044cfc623SGuido Günther return false;
38144cfc623SGuido Günther /* Get the RX header and parse it */
38244cfc623SGuido Günther val = nwl_dsi_read(dsi, NWL_DSI_RX_PKT_HEADER);
38344cfc623SGuido Günther err = nwl_dsi_clear_error(dsi);
38444cfc623SGuido Günther if (err)
38544cfc623SGuido Günther xfer->status = err;
38644cfc623SGuido Günther word_count = NWL_DSI_WC(val);
38744cfc623SGuido Günther channel = NWL_DSI_RX_VC(val);
38844cfc623SGuido Günther data_type = NWL_DSI_RX_DT(val);
38944cfc623SGuido Günther
39044cfc623SGuido Günther if (channel != xfer->msg->channel) {
39144cfc623SGuido Günther DRM_DEV_ERROR(dev,
39244cfc623SGuido Günther "[%02X] Channel mismatch (%u != %u)\n",
39344cfc623SGuido Günther xfer->cmd, channel, xfer->msg->channel);
39444cfc623SGuido Günther xfer->status = -EINVAL;
39544cfc623SGuido Günther return true;
39644cfc623SGuido Günther }
39744cfc623SGuido Günther
39844cfc623SGuido Günther switch (data_type) {
39944cfc623SGuido Günther case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
40044cfc623SGuido Günther case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
40144cfc623SGuido Günther if (xfer->msg->rx_len > 1) {
40244cfc623SGuido Günther /* read second byte */
40344cfc623SGuido Günther payload[1] = word_count >> 8;
40444cfc623SGuido Günther ++xfer->rx_len;
40544cfc623SGuido Günther }
40644cfc623SGuido Günther fallthrough;
40744cfc623SGuido Günther case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
40844cfc623SGuido Günther case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
40944cfc623SGuido Günther if (xfer->msg->rx_len > 0) {
41044cfc623SGuido Günther /* read first byte */
41144cfc623SGuido Günther payload[0] = word_count & 0xff;
41244cfc623SGuido Günther ++xfer->rx_len;
41344cfc623SGuido Günther }
41444cfc623SGuido Günther xfer->status = xfer->rx_len;
41544cfc623SGuido Günther return true;
41644cfc623SGuido Günther case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
41744cfc623SGuido Günther word_count &= 0xff;
41844cfc623SGuido Günther DRM_DEV_ERROR(dev, "[%02X] DSI error report: 0x%02x\n",
41944cfc623SGuido Günther xfer->cmd, word_count);
42044cfc623SGuido Günther xfer->status = -EPROTO;
42144cfc623SGuido Günther return true;
42244cfc623SGuido Günther }
42344cfc623SGuido Günther
42444cfc623SGuido Günther if (word_count > xfer->msg->rx_len) {
42544cfc623SGuido Günther DRM_DEV_ERROR(dev,
42644cfc623SGuido Günther "[%02X] Receive buffer too small: %zu (< %u)\n",
42744cfc623SGuido Günther xfer->cmd, xfer->msg->rx_len, word_count);
42844cfc623SGuido Günther xfer->status = -EINVAL;
42944cfc623SGuido Günther return true;
43044cfc623SGuido Günther }
43144cfc623SGuido Günther
43244cfc623SGuido Günther xfer->rx_word_count = word_count;
43344cfc623SGuido Günther } else {
43444cfc623SGuido Günther /* Set word_count from previous header read */
43544cfc623SGuido Günther word_count = xfer->rx_word_count;
43644cfc623SGuido Günther }
43744cfc623SGuido Günther
43844cfc623SGuido Günther /* If RX payload is not yet received, wait for it */
43944cfc623SGuido Günther if (!(status & NWL_DSI_RX_PKT_PAYLOAD_DATA_RCVD))
44044cfc623SGuido Günther return false;
44144cfc623SGuido Günther
44244cfc623SGuido Günther /* Read the RX payload */
44344cfc623SGuido Günther while (word_count >= 4) {
44444cfc623SGuido Günther val = nwl_dsi_read(dsi, NWL_DSI_RX_PAYLOAD);
44544cfc623SGuido Günther payload[0] = (val >> 0) & 0xff;
44644cfc623SGuido Günther payload[1] = (val >> 8) & 0xff;
44744cfc623SGuido Günther payload[2] = (val >> 16) & 0xff;
44844cfc623SGuido Günther payload[3] = (val >> 24) & 0xff;
44944cfc623SGuido Günther payload += 4;
45044cfc623SGuido Günther xfer->rx_len += 4;
45144cfc623SGuido Günther word_count -= 4;
45244cfc623SGuido Günther }
45344cfc623SGuido Günther
45444cfc623SGuido Günther if (word_count > 0) {
45544cfc623SGuido Günther val = nwl_dsi_read(dsi, NWL_DSI_RX_PAYLOAD);
45644cfc623SGuido Günther switch (word_count) {
45744cfc623SGuido Günther case 3:
45844cfc623SGuido Günther payload[2] = (val >> 16) & 0xff;
45944cfc623SGuido Günther ++xfer->rx_len;
46044cfc623SGuido Günther fallthrough;
46144cfc623SGuido Günther case 2:
46244cfc623SGuido Günther payload[1] = (val >> 8) & 0xff;
46344cfc623SGuido Günther ++xfer->rx_len;
46444cfc623SGuido Günther fallthrough;
46544cfc623SGuido Günther case 1:
46644cfc623SGuido Günther payload[0] = (val >> 0) & 0xff;
46744cfc623SGuido Günther ++xfer->rx_len;
46844cfc623SGuido Günther break;
46944cfc623SGuido Günther }
47044cfc623SGuido Günther }
47144cfc623SGuido Günther
47244cfc623SGuido Günther xfer->status = xfer->rx_len;
47344cfc623SGuido Günther err = nwl_dsi_clear_error(dsi);
47444cfc623SGuido Günther if (err)
47544cfc623SGuido Günther xfer->status = err;
47644cfc623SGuido Günther
47744cfc623SGuido Günther return true;
47844cfc623SGuido Günther }
47944cfc623SGuido Günther
nwl_dsi_finish_transmission(struct nwl_dsi * dsi,u32 status)48044cfc623SGuido Günther static void nwl_dsi_finish_transmission(struct nwl_dsi *dsi, u32 status)
48144cfc623SGuido Günther {
48244cfc623SGuido Günther struct nwl_dsi_transfer *xfer = dsi->xfer;
48344cfc623SGuido Günther bool end_packet = false;
48444cfc623SGuido Günther
48544cfc623SGuido Günther if (!xfer)
48644cfc623SGuido Günther return;
48744cfc623SGuido Günther
48844cfc623SGuido Günther if (xfer->direction == DSI_PACKET_SEND &&
48944cfc623SGuido Günther status & NWL_DSI_TX_PKT_DONE) {
49044cfc623SGuido Günther xfer->status = xfer->tx_len;
49144cfc623SGuido Günther end_packet = true;
49244cfc623SGuido Günther } else if (status & NWL_DSI_DPHY_DIRECTION &&
49344cfc623SGuido Günther ((status & (NWL_DSI_RX_PKT_HDR_RCVD |
49444cfc623SGuido Günther NWL_DSI_RX_PKT_PAYLOAD_DATA_RCVD)))) {
49544cfc623SGuido Günther end_packet = nwl_dsi_read_packet(dsi, status);
49644cfc623SGuido Günther }
49744cfc623SGuido Günther
49844cfc623SGuido Günther if (end_packet)
49944cfc623SGuido Günther complete(&xfer->completed);
50044cfc623SGuido Günther }
50144cfc623SGuido Günther
nwl_dsi_begin_transmission(struct nwl_dsi * dsi)50244cfc623SGuido Günther static void nwl_dsi_begin_transmission(struct nwl_dsi *dsi)
50344cfc623SGuido Günther {
50444cfc623SGuido Günther struct nwl_dsi_transfer *xfer = dsi->xfer;
50544cfc623SGuido Günther struct mipi_dsi_packet *pkt = &xfer->packet;
50644cfc623SGuido Günther const u8 *payload;
50744cfc623SGuido Günther size_t length;
50844cfc623SGuido Günther u16 word_count;
50944cfc623SGuido Günther u8 hs_mode;
51044cfc623SGuido Günther u32 val;
51144cfc623SGuido Günther u32 hs_workaround = 0;
51244cfc623SGuido Günther
51344cfc623SGuido Günther /* Send the payload, if any */
51444cfc623SGuido Günther length = pkt->payload_length;
51544cfc623SGuido Günther payload = pkt->payload;
51644cfc623SGuido Günther
51744cfc623SGuido Günther while (length >= 4) {
51844cfc623SGuido Günther val = *(u32 *)payload;
51944cfc623SGuido Günther hs_workaround |= !(val & 0xFFFF00);
52044cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_TX_PAYLOAD, val);
52144cfc623SGuido Günther payload += 4;
52244cfc623SGuido Günther length -= 4;
52344cfc623SGuido Günther }
52444cfc623SGuido Günther /* Send the rest of the payload */
52544cfc623SGuido Günther val = 0;
52644cfc623SGuido Günther switch (length) {
52744cfc623SGuido Günther case 3:
52844cfc623SGuido Günther val |= payload[2] << 16;
52944cfc623SGuido Günther fallthrough;
53044cfc623SGuido Günther case 2:
53144cfc623SGuido Günther val |= payload[1] << 8;
53244cfc623SGuido Günther hs_workaround |= !(val & 0xFFFF00);
53344cfc623SGuido Günther fallthrough;
53444cfc623SGuido Günther case 1:
53544cfc623SGuido Günther val |= payload[0];
53644cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_TX_PAYLOAD, val);
53744cfc623SGuido Günther break;
53844cfc623SGuido Günther }
53944cfc623SGuido Günther xfer->tx_len = pkt->payload_length;
54044cfc623SGuido Günther
54144cfc623SGuido Günther /*
54244cfc623SGuido Günther * Send the header
54344cfc623SGuido Günther * header[0] = Virtual Channel + Data Type
54444cfc623SGuido Günther * header[1] = Word Count LSB (LP) or first param (SP)
54544cfc623SGuido Günther * header[2] = Word Count MSB (LP) or second param (SP)
54644cfc623SGuido Günther */
54744cfc623SGuido Günther word_count = pkt->header[1] | (pkt->header[2] << 8);
54844cfc623SGuido Günther if (hs_workaround && (dsi->quirks & E11418_HS_MODE_QUIRK)) {
54944cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev,
55044cfc623SGuido Günther "Using hs mode workaround for cmd 0x%x\n",
55144cfc623SGuido Günther xfer->cmd);
55244cfc623SGuido Günther hs_mode = 1;
55344cfc623SGuido Günther } else {
55444cfc623SGuido Günther hs_mode = (xfer->msg->flags & MIPI_DSI_MSG_USE_LPM) ? 0 : 1;
55544cfc623SGuido Günther }
55644cfc623SGuido Günther val = NWL_DSI_WC(word_count) | NWL_DSI_TX_VC(xfer->msg->channel) |
55744cfc623SGuido Günther NWL_DSI_TX_DT(xfer->msg->type) | NWL_DSI_HS_SEL(hs_mode) |
55844cfc623SGuido Günther NWL_DSI_BTA_TX(xfer->need_bta);
55944cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_PKT_CONTROL, val);
56044cfc623SGuido Günther
56144cfc623SGuido Günther /* Send packet command */
56244cfc623SGuido Günther nwl_dsi_write(dsi, NWL_DSI_SEND_PACKET, 0x1);
56344cfc623SGuido Günther }
56444cfc623SGuido Günther
nwl_dsi_host_transfer(struct mipi_dsi_host * dsi_host,const struct mipi_dsi_msg * msg)56544cfc623SGuido Günther static ssize_t nwl_dsi_host_transfer(struct mipi_dsi_host *dsi_host,
56644cfc623SGuido Günther const struct mipi_dsi_msg *msg)
56744cfc623SGuido Günther {
56844cfc623SGuido Günther struct nwl_dsi *dsi = container_of(dsi_host, struct nwl_dsi, dsi_host);
56944cfc623SGuido Günther struct nwl_dsi_transfer xfer;
57044cfc623SGuido Günther ssize_t ret = 0;
57144cfc623SGuido Günther
57244cfc623SGuido Günther /* Create packet to be sent */
57344cfc623SGuido Günther dsi->xfer = &xfer;
57444cfc623SGuido Günther ret = mipi_dsi_create_packet(&xfer.packet, msg);
57544cfc623SGuido Günther if (ret < 0) {
57644cfc623SGuido Günther dsi->xfer = NULL;
57744cfc623SGuido Günther return ret;
57844cfc623SGuido Günther }
57944cfc623SGuido Günther
58044cfc623SGuido Günther if ((msg->type & MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM ||
58144cfc623SGuido Günther msg->type & MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM ||
58244cfc623SGuido Günther msg->type & MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM ||
58344cfc623SGuido Günther msg->type & MIPI_DSI_DCS_READ) &&
58444cfc623SGuido Günther msg->rx_len > 0 && msg->rx_buf)
58544cfc623SGuido Günther xfer.direction = DSI_PACKET_RECEIVE;
58644cfc623SGuido Günther else
58744cfc623SGuido Günther xfer.direction = DSI_PACKET_SEND;
58844cfc623SGuido Günther
58944cfc623SGuido Günther xfer.need_bta = (xfer.direction == DSI_PACKET_RECEIVE);
59044cfc623SGuido Günther xfer.need_bta |= (msg->flags & MIPI_DSI_MSG_REQ_ACK) ? 1 : 0;
59144cfc623SGuido Günther xfer.msg = msg;
59244cfc623SGuido Günther xfer.status = -ETIMEDOUT;
59344cfc623SGuido Günther xfer.rx_word_count = 0;
59444cfc623SGuido Günther xfer.rx_len = 0;
59544cfc623SGuido Günther xfer.cmd = 0x00;
59644cfc623SGuido Günther if (msg->tx_len > 0)
59744cfc623SGuido Günther xfer.cmd = ((u8 *)(msg->tx_buf))[0];
59844cfc623SGuido Günther init_completion(&xfer.completed);
59944cfc623SGuido Günther
60044cfc623SGuido Günther ret = clk_prepare_enable(dsi->rx_esc_clk);
60144cfc623SGuido Günther if (ret < 0) {
60244cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to enable rx_esc clk: %zd\n",
60344cfc623SGuido Günther ret);
60444cfc623SGuido Günther return ret;
60544cfc623SGuido Günther }
60644cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "Enabled rx_esc clk @%lu Hz\n",
60744cfc623SGuido Günther clk_get_rate(dsi->rx_esc_clk));
60844cfc623SGuido Günther
60944cfc623SGuido Günther /* Initiate the DSI packet transmision */
61044cfc623SGuido Günther nwl_dsi_begin_transmission(dsi);
61144cfc623SGuido Günther
61244cfc623SGuido Günther if (!wait_for_completion_timeout(&xfer.completed,
61344cfc623SGuido Günther NWL_DSI_MIPI_FIFO_TIMEOUT)) {
61444cfc623SGuido Günther DRM_DEV_ERROR(dsi_host->dev, "[%02X] DSI transfer timed out\n",
61544cfc623SGuido Günther xfer.cmd);
61644cfc623SGuido Günther ret = -ETIMEDOUT;
61744cfc623SGuido Günther } else {
61844cfc623SGuido Günther ret = xfer.status;
61944cfc623SGuido Günther }
62044cfc623SGuido Günther
62144cfc623SGuido Günther clk_disable_unprepare(dsi->rx_esc_clk);
62244cfc623SGuido Günther
62344cfc623SGuido Günther return ret;
62444cfc623SGuido Günther }
62544cfc623SGuido Günther
62644cfc623SGuido Günther static const struct mipi_dsi_host_ops nwl_dsi_host_ops = {
62744cfc623SGuido Günther .attach = nwl_dsi_host_attach,
62844cfc623SGuido Günther .transfer = nwl_dsi_host_transfer,
62944cfc623SGuido Günther };
63044cfc623SGuido Günther
nwl_dsi_irq_handler(int irq,void * data)63144cfc623SGuido Günther static irqreturn_t nwl_dsi_irq_handler(int irq, void *data)
63244cfc623SGuido Günther {
63344cfc623SGuido Günther u32 irq_status;
63444cfc623SGuido Günther struct nwl_dsi *dsi = data;
63544cfc623SGuido Günther
63644cfc623SGuido Günther irq_status = nwl_dsi_read(dsi, NWL_DSI_IRQ_STATUS);
63744cfc623SGuido Günther
63844cfc623SGuido Günther if (irq_status & NWL_DSI_TX_FIFO_OVFLW)
63944cfc623SGuido Günther DRM_DEV_ERROR_RATELIMITED(dsi->dev, "tx fifo overflow\n");
64044cfc623SGuido Günther
64144cfc623SGuido Günther if (irq_status & NWL_DSI_HS_TX_TIMEOUT)
64244cfc623SGuido Günther DRM_DEV_ERROR_RATELIMITED(dsi->dev, "HS tx timeout\n");
64344cfc623SGuido Günther
64444cfc623SGuido Günther if (irq_status & NWL_DSI_TX_PKT_DONE ||
64544cfc623SGuido Günther irq_status & NWL_DSI_RX_PKT_HDR_RCVD ||
64644cfc623SGuido Günther irq_status & NWL_DSI_RX_PKT_PAYLOAD_DATA_RCVD)
64744cfc623SGuido Günther nwl_dsi_finish_transmission(dsi, irq_status);
64844cfc623SGuido Günther
64944cfc623SGuido Günther return IRQ_HANDLED;
65044cfc623SGuido Günther }
65144cfc623SGuido Günther
nwl_dsi_mode_set(struct nwl_dsi * dsi)65288581137SLiu Ying static int nwl_dsi_mode_set(struct nwl_dsi *dsi)
65344cfc623SGuido Günther {
65444cfc623SGuido Günther struct device *dev = dsi->dev;
65544cfc623SGuido Günther union phy_configure_opts *phy_cfg = &dsi->phy_cfg;
65644cfc623SGuido Günther int ret;
65744cfc623SGuido Günther
65844cfc623SGuido Günther if (!dsi->lanes) {
65944cfc623SGuido Günther DRM_DEV_ERROR(dev, "Need DSI lanes: %d\n", dsi->lanes);
66044cfc623SGuido Günther return -EINVAL;
66144cfc623SGuido Günther }
66244cfc623SGuido Günther
66344cfc623SGuido Günther ret = phy_init(dsi->phy);
66444cfc623SGuido Günther if (ret < 0) {
66544cfc623SGuido Günther DRM_DEV_ERROR(dev, "Failed to init DSI phy: %d\n", ret);
66644cfc623SGuido Günther return ret;
66744cfc623SGuido Günther }
66844cfc623SGuido Günther
66969ed3dd6SLiu Ying ret = phy_set_mode(dsi->phy, PHY_MODE_MIPI_DPHY);
67069ed3dd6SLiu Ying if (ret < 0) {
67169ed3dd6SLiu Ying DRM_DEV_ERROR(dev, "Failed to set DSI phy mode: %d\n", ret);
67269ed3dd6SLiu Ying goto uninit_phy;
67369ed3dd6SLiu Ying }
67469ed3dd6SLiu Ying
67544cfc623SGuido Günther ret = phy_configure(dsi->phy, phy_cfg);
67644cfc623SGuido Günther if (ret < 0) {
67744cfc623SGuido Günther DRM_DEV_ERROR(dev, "Failed to configure DSI phy: %d\n", ret);
67844cfc623SGuido Günther goto uninit_phy;
67944cfc623SGuido Günther }
68044cfc623SGuido Günther
68144cfc623SGuido Günther ret = clk_prepare_enable(dsi->tx_esc_clk);
68244cfc623SGuido Günther if (ret < 0) {
68344cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to enable tx_esc clk: %d\n",
68444cfc623SGuido Günther ret);
68544cfc623SGuido Günther goto uninit_phy;
68644cfc623SGuido Günther }
68744cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "Enabled tx_esc clk @%lu Hz\n",
68844cfc623SGuido Günther clk_get_rate(dsi->tx_esc_clk));
68944cfc623SGuido Günther
69044cfc623SGuido Günther ret = nwl_dsi_config_host(dsi);
69144cfc623SGuido Günther if (ret < 0) {
69244cfc623SGuido Günther DRM_DEV_ERROR(dev, "Failed to set up DSI: %d", ret);
69344cfc623SGuido Günther goto disable_clock;
69444cfc623SGuido Günther }
69544cfc623SGuido Günther
69644cfc623SGuido Günther ret = nwl_dsi_config_dpi(dsi);
69744cfc623SGuido Günther if (ret < 0) {
69844cfc623SGuido Günther DRM_DEV_ERROR(dev, "Failed to set up DPI: %d", ret);
69944cfc623SGuido Günther goto disable_clock;
70044cfc623SGuido Günther }
70144cfc623SGuido Günther
70244cfc623SGuido Günther ret = phy_power_on(dsi->phy);
70344cfc623SGuido Günther if (ret < 0) {
70444cfc623SGuido Günther DRM_DEV_ERROR(dev, "Failed to power on DPHY (%d)\n", ret);
70544cfc623SGuido Günther goto disable_clock;
70644cfc623SGuido Günther }
70744cfc623SGuido Günther
70844cfc623SGuido Günther ret = nwl_dsi_init_interrupts(dsi);
70944cfc623SGuido Günther if (ret < 0)
71044cfc623SGuido Günther goto power_off_phy;
71144cfc623SGuido Günther
71244cfc623SGuido Günther return ret;
71344cfc623SGuido Günther
71444cfc623SGuido Günther power_off_phy:
71544cfc623SGuido Günther phy_power_off(dsi->phy);
71644cfc623SGuido Günther disable_clock:
71744cfc623SGuido Günther clk_disable_unprepare(dsi->tx_esc_clk);
71844cfc623SGuido Günther uninit_phy:
71944cfc623SGuido Günther phy_exit(dsi->phy);
72044cfc623SGuido Günther
72144cfc623SGuido Günther return ret;
72244cfc623SGuido Günther }
72344cfc623SGuido Günther
nwl_dsi_disable(struct nwl_dsi * dsi)72444cfc623SGuido Günther static int nwl_dsi_disable(struct nwl_dsi *dsi)
72544cfc623SGuido Günther {
72644cfc623SGuido Günther struct device *dev = dsi->dev;
72744cfc623SGuido Günther
72844cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dev, "Disabling clocks and phy\n");
72944cfc623SGuido Günther
73044cfc623SGuido Günther phy_power_off(dsi->phy);
73144cfc623SGuido Günther phy_exit(dsi->phy);
73244cfc623SGuido Günther
73344cfc623SGuido Günther /* Disabling the clock before the phy breaks enabling dsi again */
73444cfc623SGuido Günther clk_disable_unprepare(dsi->tx_esc_clk);
73544cfc623SGuido Günther
73644cfc623SGuido Günther return 0;
73744cfc623SGuido Günther }
73844cfc623SGuido Günther
7393afb2a28SLiu Ying static void
nwl_dsi_bridge_atomic_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_bridge_state)7403afb2a28SLiu Ying nwl_dsi_bridge_atomic_disable(struct drm_bridge *bridge,
7413afb2a28SLiu Ying struct drm_bridge_state *old_bridge_state)
74244cfc623SGuido Günther {
74344cfc623SGuido Günther struct nwl_dsi *dsi = bridge_to_dsi(bridge);
74444cfc623SGuido Günther int ret;
74544cfc623SGuido Günther
74644cfc623SGuido Günther nwl_dsi_disable(dsi);
74744cfc623SGuido Günther
74844cfc623SGuido Günther ret = reset_control_assert(dsi->rst_dpi);
74944cfc623SGuido Günther if (ret < 0) {
75044cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to assert DPI: %d\n", ret);
75144cfc623SGuido Günther return;
75244cfc623SGuido Günther }
75344cfc623SGuido Günther ret = reset_control_assert(dsi->rst_byte);
75444cfc623SGuido Günther if (ret < 0) {
75544cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to assert ESC: %d\n", ret);
75644cfc623SGuido Günther return;
75744cfc623SGuido Günther }
75844cfc623SGuido Günther ret = reset_control_assert(dsi->rst_esc);
75944cfc623SGuido Günther if (ret < 0) {
76044cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to assert BYTE: %d\n", ret);
76144cfc623SGuido Günther return;
76244cfc623SGuido Günther }
76344cfc623SGuido Günther ret = reset_control_assert(dsi->rst_pclk);
76444cfc623SGuido Günther if (ret < 0) {
76544cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to assert PCLK: %d\n", ret);
76644cfc623SGuido Günther return;
76744cfc623SGuido Günther }
76844cfc623SGuido Günther
76944cfc623SGuido Günther clk_disable_unprepare(dsi->core_clk);
77044cfc623SGuido Günther clk_disable_unprepare(dsi->lcdif_clk);
77144cfc623SGuido Günther
77244cfc623SGuido Günther pm_runtime_put(dsi->dev);
77344cfc623SGuido Günther }
77444cfc623SGuido Günther
nwl_dsi_get_dphy_params(struct nwl_dsi * dsi,const struct drm_display_mode * mode,union phy_configure_opts * phy_opts)77544cfc623SGuido Günther static int nwl_dsi_get_dphy_params(struct nwl_dsi *dsi,
77644cfc623SGuido Günther const struct drm_display_mode *mode,
77744cfc623SGuido Günther union phy_configure_opts *phy_opts)
77844cfc623SGuido Günther {
77944cfc623SGuido Günther unsigned long rate;
78044cfc623SGuido Günther int ret;
78144cfc623SGuido Günther
78244cfc623SGuido Günther if (dsi->lanes < 1 || dsi->lanes > 4)
78344cfc623SGuido Günther return -EINVAL;
78444cfc623SGuido Günther
78544cfc623SGuido Günther /*
78644cfc623SGuido Günther * So far the DPHY spec minimal timings work for both mixel
78744cfc623SGuido Günther * dphy and nwl dsi host
78844cfc623SGuido Günther */
78944cfc623SGuido Günther ret = phy_mipi_dphy_get_default_config(mode->clock * 1000,
79044cfc623SGuido Günther mipi_dsi_pixel_format_to_bpp(dsi->format), dsi->lanes,
79144cfc623SGuido Günther &phy_opts->mipi_dphy);
79244cfc623SGuido Günther if (ret < 0)
79344cfc623SGuido Günther return ret;
79444cfc623SGuido Günther
79544cfc623SGuido Günther rate = clk_get_rate(dsi->tx_esc_clk);
79644cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dsi->dev, "LP clk is @%lu Hz\n", rate);
79744cfc623SGuido Günther phy_opts->mipi_dphy.lp_clk_rate = rate;
79844cfc623SGuido Günther
79944cfc623SGuido Günther return 0;
80044cfc623SGuido Günther }
80144cfc623SGuido Günther
80244cfc623SGuido Günther static enum drm_mode_status
nwl_dsi_bridge_mode_valid(struct drm_bridge * bridge,const struct drm_display_info * info,const struct drm_display_mode * mode)80344cfc623SGuido Günther nwl_dsi_bridge_mode_valid(struct drm_bridge *bridge,
80412c683e1SLaurent Pinchart const struct drm_display_info *info,
80544cfc623SGuido Günther const struct drm_display_mode *mode)
80644cfc623SGuido Günther {
80744cfc623SGuido Günther struct nwl_dsi *dsi = bridge_to_dsi(bridge);
80844cfc623SGuido Günther int bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
80944cfc623SGuido Günther
81044cfc623SGuido Günther if (mode->clock * bpp > 15000000 * dsi->lanes)
81144cfc623SGuido Günther return MODE_CLOCK_HIGH;
81244cfc623SGuido Günther
81344cfc623SGuido Günther if (mode->clock * bpp < 80000 * dsi->lanes)
81444cfc623SGuido Günther return MODE_CLOCK_LOW;
81544cfc623SGuido Günther
81644cfc623SGuido Günther return MODE_OK;
81744cfc623SGuido Günther }
81844cfc623SGuido Günther
nwl_dsi_bridge_atomic_check(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)8193afb2a28SLiu Ying static int nwl_dsi_bridge_atomic_check(struct drm_bridge *bridge,
8203afb2a28SLiu Ying struct drm_bridge_state *bridge_state,
8213afb2a28SLiu Ying struct drm_crtc_state *crtc_state,
8223afb2a28SLiu Ying struct drm_connector_state *conn_state)
8233afb2a28SLiu Ying {
8243afb2a28SLiu Ying struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
8253afb2a28SLiu Ying
8263afb2a28SLiu Ying /* At least LCDIF + NWL needs active high sync */
8273afb2a28SLiu Ying adjusted_mode->flags |= (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
8283afb2a28SLiu Ying adjusted_mode->flags &= ~(DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
8293afb2a28SLiu Ying
83088581137SLiu Ying /*
83188581137SLiu Ying * Do a full modeset if crtc_state->active is changed to be true.
83288581137SLiu Ying * This ensures our ->mode_set() is called to get the DSI controller
83388581137SLiu Ying * and the PHY ready to send DCS commands, when only the connector's
83488581137SLiu Ying * DPMS is brought out of "Off" status.
83588581137SLiu Ying */
8363afb2a28SLiu Ying if (crtc_state->active_changed && crtc_state->active)
8373afb2a28SLiu Ying crtc_state->mode_changed = true;
8383afb2a28SLiu Ying
8393afb2a28SLiu Ying return 0;
8403afb2a28SLiu Ying }
8413afb2a28SLiu Ying
84244cfc623SGuido Günther static void
nwl_dsi_bridge_mode_set(struct drm_bridge * bridge,const struct drm_display_mode * mode,const struct drm_display_mode * adjusted_mode)84344cfc623SGuido Günther nwl_dsi_bridge_mode_set(struct drm_bridge *bridge,
84444cfc623SGuido Günther const struct drm_display_mode *mode,
84544cfc623SGuido Günther const struct drm_display_mode *adjusted_mode)
84644cfc623SGuido Günther {
84744cfc623SGuido Günther struct nwl_dsi *dsi = bridge_to_dsi(bridge);
84844cfc623SGuido Günther struct device *dev = dsi->dev;
84944cfc623SGuido Günther union phy_configure_opts new_cfg;
85044cfc623SGuido Günther unsigned long phy_ref_rate;
85144cfc623SGuido Günther int ret;
85244cfc623SGuido Günther
85344cfc623SGuido Günther ret = nwl_dsi_get_dphy_params(dsi, adjusted_mode, &new_cfg);
85444cfc623SGuido Günther if (ret < 0)
85544cfc623SGuido Günther return;
85644cfc623SGuido Günther
85744cfc623SGuido Günther phy_ref_rate = clk_get_rate(dsi->phy_ref_clk);
85844cfc623SGuido Günther DRM_DEV_DEBUG_DRIVER(dev, "PHY at ref rate: %lu\n", phy_ref_rate);
85944cfc623SGuido Günther /* Save the new desired phy config */
86044cfc623SGuido Günther memcpy(&dsi->phy_cfg, &new_cfg, sizeof(new_cfg));
86144cfc623SGuido Günther
862d008bc33SVille Syrjälä drm_mode_copy(&dsi->mode, adjusted_mode);
86344cfc623SGuido Günther drm_mode_debug_printmodeline(adjusted_mode);
86444cfc623SGuido Günther
86546f47807SYongzhi Liu if (pm_runtime_resume_and_get(dev) < 0)
86646f47807SYongzhi Liu return;
86744cfc623SGuido Günther
86844cfc623SGuido Günther if (clk_prepare_enable(dsi->lcdif_clk) < 0)
86946f47807SYongzhi Liu goto runtime_put;
87044cfc623SGuido Günther if (clk_prepare_enable(dsi->core_clk) < 0)
87146f47807SYongzhi Liu goto runtime_put;
87244cfc623SGuido Günther
87344cfc623SGuido Günther /* Step 1 from DSI reset-out instructions */
87444cfc623SGuido Günther ret = reset_control_deassert(dsi->rst_pclk);
87544cfc623SGuido Günther if (ret < 0) {
87688581137SLiu Ying DRM_DEV_ERROR(dev, "Failed to deassert PCLK: %d\n", ret);
87746f47807SYongzhi Liu goto runtime_put;
87844cfc623SGuido Günther }
87944cfc623SGuido Günther
88044cfc623SGuido Günther /* Step 2 from DSI reset-out instructions */
88188581137SLiu Ying nwl_dsi_mode_set(dsi);
88244cfc623SGuido Günther
88344cfc623SGuido Günther /* Step 3 from DSI reset-out instructions */
88444cfc623SGuido Günther ret = reset_control_deassert(dsi->rst_esc);
88544cfc623SGuido Günther if (ret < 0) {
88688581137SLiu Ying DRM_DEV_ERROR(dev, "Failed to deassert ESC: %d\n", ret);
88746f47807SYongzhi Liu goto runtime_put;
88844cfc623SGuido Günther }
88944cfc623SGuido Günther ret = reset_control_deassert(dsi->rst_byte);
89044cfc623SGuido Günther if (ret < 0) {
89188581137SLiu Ying DRM_DEV_ERROR(dev, "Failed to deassert BYTE: %d\n", ret);
89246f47807SYongzhi Liu goto runtime_put;
89344cfc623SGuido Günther }
89446f47807SYongzhi Liu
89546f47807SYongzhi Liu return;
89646f47807SYongzhi Liu
89746f47807SYongzhi Liu runtime_put:
89846f47807SYongzhi Liu pm_runtime_put_sync(dev);
89944cfc623SGuido Günther }
90044cfc623SGuido Günther
9013afb2a28SLiu Ying static void
nwl_dsi_bridge_atomic_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_bridge_state)9023afb2a28SLiu Ying nwl_dsi_bridge_atomic_enable(struct drm_bridge *bridge,
9033afb2a28SLiu Ying struct drm_bridge_state *old_bridge_state)
90444cfc623SGuido Günther {
90544cfc623SGuido Günther struct nwl_dsi *dsi = bridge_to_dsi(bridge);
90644cfc623SGuido Günther int ret;
90744cfc623SGuido Günther
90844cfc623SGuido Günther /* Step 5 from DSI reset-out instructions */
90944cfc623SGuido Günther ret = reset_control_deassert(dsi->rst_dpi);
91044cfc623SGuido Günther if (ret < 0)
91144cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to deassert DPI: %d\n", ret);
91244cfc623SGuido Günther }
91344cfc623SGuido Günther
nwl_dsi_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)91444cfc623SGuido Günther static int nwl_dsi_bridge_attach(struct drm_bridge *bridge,
91544cfc623SGuido Günther enum drm_bridge_attach_flags flags)
91644cfc623SGuido Günther {
91744cfc623SGuido Günther struct nwl_dsi *dsi = bridge_to_dsi(bridge);
91844cfc623SGuido Günther struct drm_bridge *panel_bridge;
91944cfc623SGuido Günther
9204f460107SJagan Teki panel_bridge = devm_drm_of_get_bridge(dsi->dev, dsi->dev->of_node, 1, 0);
92144cfc623SGuido Günther if (IS_ERR(panel_bridge))
92244cfc623SGuido Günther return PTR_ERR(panel_bridge);
92344cfc623SGuido Günther
9247b153418SJagan Teki return drm_bridge_attach(bridge->encoder, panel_bridge, bridge, flags);
92544cfc623SGuido Günther }
92644cfc623SGuido Günther
nwl_bridge_atomic_get_input_bus_fmts(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state,u32 output_fmt,unsigned int * num_input_fmts)9272f1495faSGuido Günther static u32 *nwl_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
9282f1495faSGuido Günther struct drm_bridge_state *bridge_state,
9292f1495faSGuido Günther struct drm_crtc_state *crtc_state,
9302f1495faSGuido Günther struct drm_connector_state *conn_state,
9312f1495faSGuido Günther u32 output_fmt,
9322f1495faSGuido Günther unsigned int *num_input_fmts)
9332f1495faSGuido Günther {
9342f1495faSGuido Günther u32 *input_fmts, input_fmt;
9352f1495faSGuido Günther
9362f1495faSGuido Günther *num_input_fmts = 0;
9372f1495faSGuido Günther
9382f1495faSGuido Günther switch (output_fmt) {
9392f1495faSGuido Günther /* If MEDIA_BUS_FMT_FIXED is tested, return default bus format */
9402f1495faSGuido Günther case MEDIA_BUS_FMT_FIXED:
9412f1495faSGuido Günther input_fmt = MEDIA_BUS_FMT_RGB888_1X24;
9422f1495faSGuido Günther break;
9432f1495faSGuido Günther case MEDIA_BUS_FMT_RGB888_1X24:
9442f1495faSGuido Günther case MEDIA_BUS_FMT_RGB666_1X18:
9452f1495faSGuido Günther case MEDIA_BUS_FMT_RGB565_1X16:
9462f1495faSGuido Günther input_fmt = output_fmt;
9472f1495faSGuido Günther break;
9482f1495faSGuido Günther default:
9492f1495faSGuido Günther return NULL;
9502f1495faSGuido Günther }
9512f1495faSGuido Günther
9522f1495faSGuido Günther input_fmts = kcalloc(1, sizeof(*input_fmts), GFP_KERNEL);
9532f1495faSGuido Günther if (!input_fmts)
9542f1495faSGuido Günther return NULL;
9552f1495faSGuido Günther input_fmts[0] = input_fmt;
9562f1495faSGuido Günther *num_input_fmts = 1;
9572f1495faSGuido Günther
9582f1495faSGuido Günther return input_fmts;
9592f1495faSGuido Günther }
9602f1495faSGuido Günther
96144cfc623SGuido Günther static const struct drm_bridge_funcs nwl_dsi_bridge_funcs = {
9623afb2a28SLiu Ying .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
9633afb2a28SLiu Ying .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
9643afb2a28SLiu Ying .atomic_reset = drm_atomic_helper_bridge_reset,
9653afb2a28SLiu Ying .atomic_check = nwl_dsi_bridge_atomic_check,
9663afb2a28SLiu Ying .atomic_enable = nwl_dsi_bridge_atomic_enable,
9673afb2a28SLiu Ying .atomic_disable = nwl_dsi_bridge_atomic_disable,
9682f1495faSGuido Günther .atomic_get_input_bus_fmts = nwl_bridge_atomic_get_input_bus_fmts,
96944cfc623SGuido Günther .mode_set = nwl_dsi_bridge_mode_set,
97044cfc623SGuido Günther .mode_valid = nwl_dsi_bridge_mode_valid,
97144cfc623SGuido Günther .attach = nwl_dsi_bridge_attach,
97244cfc623SGuido Günther };
97344cfc623SGuido Günther
nwl_dsi_parse_dt(struct nwl_dsi * dsi)97444cfc623SGuido Günther static int nwl_dsi_parse_dt(struct nwl_dsi *dsi)
97544cfc623SGuido Günther {
97644cfc623SGuido Günther struct platform_device *pdev = to_platform_device(dsi->dev);
97744cfc623SGuido Günther struct clk *clk;
97844cfc623SGuido Günther void __iomem *base;
97944cfc623SGuido Günther int ret;
98044cfc623SGuido Günther
98144cfc623SGuido Günther dsi->phy = devm_phy_get(dsi->dev, "dphy");
98244cfc623SGuido Günther if (IS_ERR(dsi->phy)) {
98344cfc623SGuido Günther ret = PTR_ERR(dsi->phy);
98444cfc623SGuido Günther if (ret != -EPROBE_DEFER)
98544cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Could not get PHY: %d\n", ret);
98644cfc623SGuido Günther return ret;
98744cfc623SGuido Günther }
98844cfc623SGuido Günther
98944cfc623SGuido Günther clk = devm_clk_get(dsi->dev, "lcdif");
99044cfc623SGuido Günther if (IS_ERR(clk)) {
99144cfc623SGuido Günther ret = PTR_ERR(clk);
99244cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to get lcdif clock: %d\n",
99344cfc623SGuido Günther ret);
99444cfc623SGuido Günther return ret;
99544cfc623SGuido Günther }
99644cfc623SGuido Günther dsi->lcdif_clk = clk;
99744cfc623SGuido Günther
99844cfc623SGuido Günther clk = devm_clk_get(dsi->dev, "core");
99944cfc623SGuido Günther if (IS_ERR(clk)) {
100044cfc623SGuido Günther ret = PTR_ERR(clk);
100144cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to get core clock: %d\n",
100244cfc623SGuido Günther ret);
100344cfc623SGuido Günther return ret;
100444cfc623SGuido Günther }
100544cfc623SGuido Günther dsi->core_clk = clk;
100644cfc623SGuido Günther
100744cfc623SGuido Günther clk = devm_clk_get(dsi->dev, "phy_ref");
100844cfc623SGuido Günther if (IS_ERR(clk)) {
100944cfc623SGuido Günther ret = PTR_ERR(clk);
101044cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to get phy_ref clock: %d\n",
101144cfc623SGuido Günther ret);
101244cfc623SGuido Günther return ret;
101344cfc623SGuido Günther }
101444cfc623SGuido Günther dsi->phy_ref_clk = clk;
101544cfc623SGuido Günther
101644cfc623SGuido Günther clk = devm_clk_get(dsi->dev, "rx_esc");
101744cfc623SGuido Günther if (IS_ERR(clk)) {
101844cfc623SGuido Günther ret = PTR_ERR(clk);
101944cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to get rx_esc clock: %d\n",
102044cfc623SGuido Günther ret);
102144cfc623SGuido Günther return ret;
102244cfc623SGuido Günther }
102344cfc623SGuido Günther dsi->rx_esc_clk = clk;
102444cfc623SGuido Günther
102544cfc623SGuido Günther clk = devm_clk_get(dsi->dev, "tx_esc");
102644cfc623SGuido Günther if (IS_ERR(clk)) {
102744cfc623SGuido Günther ret = PTR_ERR(clk);
102844cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to get tx_esc clock: %d\n",
102944cfc623SGuido Günther ret);
103044cfc623SGuido Günther return ret;
103144cfc623SGuido Günther }
103244cfc623SGuido Günther dsi->tx_esc_clk = clk;
103344cfc623SGuido Günther
103444cfc623SGuido Günther dsi->mux = devm_mux_control_get(dsi->dev, NULL);
103544cfc623SGuido Günther if (IS_ERR(dsi->mux)) {
103644cfc623SGuido Günther ret = PTR_ERR(dsi->mux);
103744cfc623SGuido Günther if (ret != -EPROBE_DEFER)
103844cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to get mux: %d\n", ret);
103944cfc623SGuido Günther return ret;
104044cfc623SGuido Günther }
104144cfc623SGuido Günther
104244cfc623SGuido Günther base = devm_platform_ioremap_resource(pdev, 0);
104344cfc623SGuido Günther if (IS_ERR(base))
104444cfc623SGuido Günther return PTR_ERR(base);
104544cfc623SGuido Günther
104644cfc623SGuido Günther dsi->regmap =
104744cfc623SGuido Günther devm_regmap_init_mmio(dsi->dev, base, &nwl_dsi_regmap_config);
104844cfc623SGuido Günther if (IS_ERR(dsi->regmap)) {
104944cfc623SGuido Günther ret = PTR_ERR(dsi->regmap);
105044cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to create NWL DSI regmap: %d\n",
105144cfc623SGuido Günther ret);
105244cfc623SGuido Günther return ret;
105344cfc623SGuido Günther }
105444cfc623SGuido Günther
105544cfc623SGuido Günther dsi->irq = platform_get_irq(pdev, 0);
105644cfc623SGuido Günther if (dsi->irq < 0) {
105744cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to get device IRQ: %d\n",
105844cfc623SGuido Günther dsi->irq);
105944cfc623SGuido Günther return dsi->irq;
106044cfc623SGuido Günther }
106144cfc623SGuido Günther
106244cfc623SGuido Günther dsi->rst_pclk = devm_reset_control_get_exclusive(dsi->dev, "pclk");
106344cfc623SGuido Günther if (IS_ERR(dsi->rst_pclk)) {
106444cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to get pclk reset: %ld\n",
106544cfc623SGuido Günther PTR_ERR(dsi->rst_pclk));
106644cfc623SGuido Günther return PTR_ERR(dsi->rst_pclk);
106744cfc623SGuido Günther }
106844cfc623SGuido Günther dsi->rst_byte = devm_reset_control_get_exclusive(dsi->dev, "byte");
106944cfc623SGuido Günther if (IS_ERR(dsi->rst_byte)) {
107044cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to get byte reset: %ld\n",
107144cfc623SGuido Günther PTR_ERR(dsi->rst_byte));
107244cfc623SGuido Günther return PTR_ERR(dsi->rst_byte);
107344cfc623SGuido Günther }
107444cfc623SGuido Günther dsi->rst_esc = devm_reset_control_get_exclusive(dsi->dev, "esc");
107544cfc623SGuido Günther if (IS_ERR(dsi->rst_esc)) {
107644cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to get esc reset: %ld\n",
107744cfc623SGuido Günther PTR_ERR(dsi->rst_esc));
107844cfc623SGuido Günther return PTR_ERR(dsi->rst_esc);
107944cfc623SGuido Günther }
108044cfc623SGuido Günther dsi->rst_dpi = devm_reset_control_get_exclusive(dsi->dev, "dpi");
108144cfc623SGuido Günther if (IS_ERR(dsi->rst_dpi)) {
108244cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to get dpi reset: %ld\n",
108344cfc623SGuido Günther PTR_ERR(dsi->rst_dpi));
108444cfc623SGuido Günther return PTR_ERR(dsi->rst_dpi);
108544cfc623SGuido Günther }
108644cfc623SGuido Günther return 0;
108744cfc623SGuido Günther }
108844cfc623SGuido Günther
nwl_dsi_select_input(struct nwl_dsi * dsi)108944cfc623SGuido Günther static int nwl_dsi_select_input(struct nwl_dsi *dsi)
109044cfc623SGuido Günther {
109144cfc623SGuido Günther struct device_node *remote;
109244cfc623SGuido Günther u32 use_dcss = 1;
109344cfc623SGuido Günther int ret;
109444cfc623SGuido Günther
109544cfc623SGuido Günther remote = of_graph_get_remote_node(dsi->dev->of_node, 0,
109644cfc623SGuido Günther NWL_DSI_ENDPOINT_LCDIF);
109744cfc623SGuido Günther if (remote) {
109844cfc623SGuido Günther use_dcss = 0;
109944cfc623SGuido Günther } else {
110044cfc623SGuido Günther remote = of_graph_get_remote_node(dsi->dev->of_node, 0,
110144cfc623SGuido Günther NWL_DSI_ENDPOINT_DCSS);
110244cfc623SGuido Günther if (!remote) {
110344cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev,
110444cfc623SGuido Günther "No valid input endpoint found\n");
110544cfc623SGuido Günther return -EINVAL;
110644cfc623SGuido Günther }
110744cfc623SGuido Günther }
110844cfc623SGuido Günther
110944cfc623SGuido Günther DRM_DEV_INFO(dsi->dev, "Using %s as input source\n",
111044cfc623SGuido Günther (use_dcss) ? "DCSS" : "LCDIF");
111144cfc623SGuido Günther ret = mux_control_try_select(dsi->mux, use_dcss);
111244cfc623SGuido Günther if (ret < 0)
111344cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to select input: %d\n", ret);
111444cfc623SGuido Günther
111544cfc623SGuido Günther of_node_put(remote);
111644cfc623SGuido Günther return ret;
111744cfc623SGuido Günther }
111844cfc623SGuido Günther
nwl_dsi_deselect_input(struct nwl_dsi * dsi)111944cfc623SGuido Günther static int nwl_dsi_deselect_input(struct nwl_dsi *dsi)
112044cfc623SGuido Günther {
112144cfc623SGuido Günther int ret;
112244cfc623SGuido Günther
112344cfc623SGuido Günther ret = mux_control_deselect(dsi->mux);
112444cfc623SGuido Günther if (ret < 0)
112544cfc623SGuido Günther DRM_DEV_ERROR(dsi->dev, "Failed to deselect input: %d\n", ret);
112644cfc623SGuido Günther
112744cfc623SGuido Günther return ret;
112844cfc623SGuido Günther }
112944cfc623SGuido Günther
113044cfc623SGuido Günther static const struct drm_bridge_timings nwl_dsi_timings = {
113144cfc623SGuido Günther .input_bus_flags = DRM_BUS_FLAG_DE_LOW,
113244cfc623SGuido Günther };
113344cfc623SGuido Günther
113444cfc623SGuido Günther static const struct of_device_id nwl_dsi_dt_ids[] = {
113544cfc623SGuido Günther { .compatible = "fsl,imx8mq-nwl-dsi", },
113644cfc623SGuido Günther { /* sentinel */ }
113744cfc623SGuido Günther };
113844cfc623SGuido Günther MODULE_DEVICE_TABLE(of, nwl_dsi_dt_ids);
113944cfc623SGuido Günther
114044cfc623SGuido Günther static const struct soc_device_attribute nwl_dsi_quirks_match[] = {
114144cfc623SGuido Günther { .soc_id = "i.MX8MQ", .revision = "2.0",
114244cfc623SGuido Günther .data = (void *)E11418_HS_MODE_QUIRK },
1143f6e68388SGeert Uytterhoeven { /* sentinel. */ }
114444cfc623SGuido Günther };
114544cfc623SGuido Günther
nwl_dsi_probe(struct platform_device * pdev)114644cfc623SGuido Günther static int nwl_dsi_probe(struct platform_device *pdev)
114744cfc623SGuido Günther {
114844cfc623SGuido Günther struct device *dev = &pdev->dev;
114944cfc623SGuido Günther const struct soc_device_attribute *attr;
115044cfc623SGuido Günther struct nwl_dsi *dsi;
115144cfc623SGuido Günther int ret;
115244cfc623SGuido Günther
115344cfc623SGuido Günther dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
115444cfc623SGuido Günther if (!dsi)
115544cfc623SGuido Günther return -ENOMEM;
115644cfc623SGuido Günther
115744cfc623SGuido Günther dsi->dev = dev;
115844cfc623SGuido Günther
115944cfc623SGuido Günther ret = nwl_dsi_parse_dt(dsi);
116044cfc623SGuido Günther if (ret)
116144cfc623SGuido Günther return ret;
116244cfc623SGuido Günther
116344cfc623SGuido Günther ret = devm_request_irq(dev, dsi->irq, nwl_dsi_irq_handler, 0,
116444cfc623SGuido Günther dev_name(dev), dsi);
116544cfc623SGuido Günther if (ret < 0) {
116644cfc623SGuido Günther DRM_DEV_ERROR(dev, "Failed to request IRQ %d: %d\n", dsi->irq,
116744cfc623SGuido Günther ret);
116844cfc623SGuido Günther return ret;
116944cfc623SGuido Günther }
117044cfc623SGuido Günther
117144cfc623SGuido Günther dsi->dsi_host.ops = &nwl_dsi_host_ops;
117244cfc623SGuido Günther dsi->dsi_host.dev = dev;
117344cfc623SGuido Günther ret = mipi_dsi_host_register(&dsi->dsi_host);
117444cfc623SGuido Günther if (ret) {
117544cfc623SGuido Günther DRM_DEV_ERROR(dev, "Failed to register MIPI host: %d\n", ret);
117644cfc623SGuido Günther return ret;
117744cfc623SGuido Günther }
117844cfc623SGuido Günther
117944cfc623SGuido Günther attr = soc_device_match(nwl_dsi_quirks_match);
118044cfc623SGuido Günther if (attr)
118144cfc623SGuido Günther dsi->quirks = (uintptr_t)attr->data;
118244cfc623SGuido Günther
118344cfc623SGuido Günther dsi->bridge.driver_private = dsi;
118444cfc623SGuido Günther dsi->bridge.funcs = &nwl_dsi_bridge_funcs;
118544cfc623SGuido Günther dsi->bridge.of_node = dev->of_node;
118644cfc623SGuido Günther dsi->bridge.timings = &nwl_dsi_timings;
118744cfc623SGuido Günther
118844cfc623SGuido Günther dev_set_drvdata(dev, dsi);
118944cfc623SGuido Günther pm_runtime_enable(dev);
119044cfc623SGuido Günther
119144cfc623SGuido Günther ret = nwl_dsi_select_input(dsi);
119244cfc623SGuido Günther if (ret < 0) {
1193b146e343SMiaoqian Lin pm_runtime_disable(dev);
119444cfc623SGuido Günther mipi_dsi_host_unregister(&dsi->dsi_host);
119544cfc623SGuido Günther return ret;
119644cfc623SGuido Günther }
119744cfc623SGuido Günther
119844cfc623SGuido Günther drm_bridge_add(&dsi->bridge);
119944cfc623SGuido Günther return 0;
120044cfc623SGuido Günther }
120144cfc623SGuido Günther
nwl_dsi_remove(struct platform_device * pdev)12026780b94dSUwe Kleine-König static void nwl_dsi_remove(struct platform_device *pdev)
120344cfc623SGuido Günther {
120444cfc623SGuido Günther struct nwl_dsi *dsi = platform_get_drvdata(pdev);
120544cfc623SGuido Günther
120644cfc623SGuido Günther nwl_dsi_deselect_input(dsi);
120744cfc623SGuido Günther mipi_dsi_host_unregister(&dsi->dsi_host);
120844cfc623SGuido Günther drm_bridge_remove(&dsi->bridge);
120944cfc623SGuido Günther pm_runtime_disable(&pdev->dev);
121044cfc623SGuido Günther }
121144cfc623SGuido Günther
121244cfc623SGuido Günther static struct platform_driver nwl_dsi_driver = {
121344cfc623SGuido Günther .probe = nwl_dsi_probe,
12146780b94dSUwe Kleine-König .remove_new = nwl_dsi_remove,
121544cfc623SGuido Günther .driver = {
121644cfc623SGuido Günther .of_match_table = nwl_dsi_dt_ids,
121744cfc623SGuido Günther .name = DRV_NAME,
121844cfc623SGuido Günther },
121944cfc623SGuido Günther };
122044cfc623SGuido Günther
122144cfc623SGuido Günther module_platform_driver(nwl_dsi_driver);
122244cfc623SGuido Günther
122344cfc623SGuido Günther MODULE_AUTHOR("NXP Semiconductor");
122444cfc623SGuido Günther MODULE_AUTHOR("Purism SPC");
122544cfc623SGuido Günther MODULE_DESCRIPTION("Northwest Logic MIPI-DSI driver");
122644cfc623SGuido Günther MODULE_LICENSE("GPL"); /* GPLv2 or later */
1227