/* SPDX-License-Identifier: GPL-2.0-only */
/* Copyright (C) 2013--2024 Intel Corporation */

#ifndef IPU6_ISYS_H
#define IPU6_ISYS_H

#include <linux/idr.h>
#include <linux/irqreturn.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/pm_qos.h>
#include <linux/spinlock_types.h>
#include <linux/types.h>

#include <media/media-device.h>
#include <media/v4l2-async.h>
#include <media/v4l2-device.h>
#include <media/v4l2-mediabus.h>

#include "ipu6.h"
#include "ipu6-fw-isys.h"
#include "ipu7-fw-isys.h"
#include "ipu6-isys-csi2.h"
#include "ipu6-isys-video.h"

struct ipu6_bus_device;

#define IPU6_ISYS_ENTITY_VERSION	"6"
#define IPU7_ISYS_ENTITY_VERSION	"7"
#define IPU7P5_ISYS_ENTITY_VERSION	"7.5"
#define IPU8_ISYS_ENTITY_VERSION	"8"

static inline const char *ipu6_isys_entity_version(struct ipu6_device *isp)
{
	if (IS_IPU8(isp))
		return IPU8_ISYS_ENTITY_VERSION;
	if (IS_IPU7P5(isp))
		return IPU7P5_ISYS_ENTITY_VERSION;
	if (IS_IPU7_MTL(isp))
		return IPU7_ISYS_ENTITY_VERSION;

	return IPU6_ISYS_ENTITY_VERSION;
}

/* FW support max 16 streams */
#define IPU6_ISYS_MAX_STREAMS		16
#define ISYS_UNISPART_IRQS	(IPU6_ISYS_UNISPART_IRQ_SW |	\
				 IPU6_ISYS_UNISPART_IRQ_CSI0 |	\
				 IPU6_ISYS_UNISPART_IRQ_CSI1)

/*
 * Current message queue configuration. These must be big enough
 * so that they never gets full. Queues are located in system memory
 */
#define IPU6_ISYS_SIZE_RECV_QUEUE 40
#define IPU6_ISYS_SIZE_SEND_QUEUE 40
#define IPU6_ISYS_SIZE_PROXY_RECV_QUEUE 5
#define IPU6_ISYS_SIZE_PROXY_SEND_QUEUE 5
#define IPU6_ISYS_NUM_RECV_QUEUE 1

#define IPU6_ISYS_MIN_WIDTH		2U
#define IPU6_ISYS_MIN_HEIGHT		1U
#define IPU6_ISYS_MAX_WIDTH		4672U
#define IPU6_ISYS_MAX_HEIGHT		3416U

/* the threshold granularity is 2KB on IPU6 */
#define IPU6_SRAM_GRANULARITY_SHIFT	11
#define IPU6_SRAM_GRANULARITY_SIZE	2048
/* the threshold granularity is 1KB on IPU6SE */
#define IPU6SE_SRAM_GRANULARITY_SHIFT	10
#define IPU6SE_SRAM_GRANULARITY_SIZE	1024
/* IS pixel buffer is 256KB, MaxSRAMSize is 200KB on IPU6 */
#define IPU6_MAX_SRAM_SIZE			(200 << 10)
/* IS pixel buffer is 128KB, MaxSRAMSize is 96KB on IPU6SE */
#define IPU6SE_MAX_SRAM_SIZE			(96 << 10)

#define IPU6EP_LTR_VALUE			200
#define IPU6EP_MIN_MEMOPEN_TH			0x4
#define IPU6EP_MTL_LTR_VALUE			1023
#define IPU6EP_MTL_MIN_MEMOPEN_TH		0xc

struct ipu6_isys_csi2_config {
	u32 nlanes;
	u32 port;
	enum v4l2_mbus_type bus_type;
};

struct sensor_async_sd {
	struct v4l2_async_connection asc;
	struct ipu6_isys_csi2_config csi2;
};

/*
 * struct ipu6_isys
 *
 * @media_dev: Media device
 * @v4l2_dev: V4L2 device
 * @adev: ISYS bus device
 * @csi2_rx_ctrl_cached: cached shared value between all CSI2 receivers
 * @streams_lock: serialise access to streams
 * @streams: streams per firmware stream ID
 * @fwctx: fw communication layer context pointer
 * @phy_termcal_val: the termination calibration value, only used for DWC PHY
 * @need_reset: Isys requires d0i0->i3 transition
 * @mutex: serialise access isys video open/release related operations
 * @stream_mutex: serialise stream start and stop, queueing requests
 * @pdata: platform data pointer
 * @csi2: CSI-2 receivers
 */
struct ipu6_isys {
	struct media_device media_dev;
	struct v4l2_device v4l2_dev;
	struct ipu6_bus_device *adev;

	u32 isr_csi2_bits;
	u32 csi2_rx_ctrl_cached;
	spinlock_t streams_lock;
	struct ipu6_isys_stream *streams_by_handle[IPU6_ISYS_MAX_STREAMS];
	void *fwctx;
	u32 phy_termcal_val;
	u32 phy_rext_cal;
	bool need_reset;
	bool icache_prefetch;
	bool csi2_cse_ipc_not_supported;
	bool iwake_watermark_enabled;
	unsigned int sensor_type;

	struct mutex mutex;
	struct mutex stream_mutex;

	struct ipu6_isys_pdata *pdata;

	int (*phy_set_power)(struct ipu6_isys *isys,
			     struct ipu6_isys_csi2_config *cfg,
			     const struct ipu6_isys_csi2_timing *timing,
			     bool on);

	struct ipu6_isys_csi2 *csi2;

	struct pm_qos_request pm_qos;
	spinlock_t listlock;	/* Protect framebuflist */
	struct list_head framebuflist;
	struct list_head framebuflist_fw;
	struct v4l2_async_notifier notifier;
	struct ida streams;
};

struct isys_fw_msgs {
	union {
		u64 dummy;
		union {
			struct ipu6_fw_isys_frame_buff_set_abi frame;
			struct ipu6_fw_isys_stream_cfg_data_abi stream;
		} ipu6;
		union {
			struct ipu7_fw_isys_frame_buff_set frame;
			struct ipu7_fw_isys_stream_cfg stream;
			/* sizes the union for the larger IPU8 (ABI 1.0.14) layout */
			struct ipu7_fw_isys_frame_buff_set_ipu8 frame_ipu8;
			struct ipu7_fw_isys_stream_cfg_ipu8 stream_ipu8;
		} ipu7;
	};
	struct list_head head;
	dma_addr_t dma_addr;
};

struct ipu6_fw_isys_ops {
	int (*init)(struct ipu6_isys *isys, unsigned int num_streams);
	int (*close)(struct ipu6_isys *isys);
	int (*send_cmd)(struct ipu6_isys *isys,
			const unsigned int stream_handle,
			void *cpu_mapped_buf,
			dma_addr_t dma_mapped_buf,
			size_t size, u16 send_type);
	void (*cleanup)(struct ipu6_isys *isys);
	int (*prepare_stream_cfg)(struct ipu6_isys_stream *stream,
				  struct v4l2_mbus_frame_desc *desc,
				  struct isys_fw_msgs *msg);
	void (*prepare_buf_set)(struct isys_fw_msgs *msg,
				struct ipu6_isys_stream *stream,
				struct ipu6_isys_buffer_list *bl);
	int (*stream_open)(struct ipu6_isys *isys,
			   const unsigned int stream_handle,
			   struct isys_fw_msgs *msg);
	int (*stream_start)(struct ipu6_isys *isys,
			    const unsigned int stream_handle,
			    struct isys_fw_msgs *msg, bool capture);
	int (*stream_capture)(struct ipu6_isys *isys,
			      const unsigned int stream_handle,
			      struct isys_fw_msgs *msg);
	int (*stream_flush)(struct ipu6_isys *isys,
			    const unsigned int stream_handle);
	int (*stream_close)(struct ipu6_isys *isys,
			    const unsigned int stream_handle);
	void (*dump_stream_cfg)(struct device *dev, struct isys_fw_msgs *msg);
	void (*dump_frame_buf_set)(struct device *dev, struct isys_fw_msgs *msg,
				   unsigned int outputs);
};

struct isys_fw_msgs *ipu6_get_fw_msg_buf(struct ipu6_isys_stream *stream);
void ipu6_put_fw_msg_buf(struct ipu6_isys *isys, struct isys_fw_msgs *msg);
void ipu6_cleanup_fw_msg_bufs(struct ipu6_isys *isys);

extern const struct v4l2_ioctl_ops ipu6_isys_ioctl_ops;

void ipu6_isys_update_watermark_setting(struct ipu6_isys *isys);

int ipu6_isys_mcd_phy_set_power(struct ipu6_isys *isys,
				struct ipu6_isys_csi2_config *cfg,
				const struct ipu6_isys_csi2_timing *timing,
				bool on);

int ipu6_isys_dwc_phy_set_power(struct ipu6_isys *isys,
				struct ipu6_isys_csi2_config *cfg,
				const struct ipu6_isys_csi2_timing *timing,
				bool on);

int ipu6_isys_jsl_phy_set_power(struct ipu6_isys *isys,
				struct ipu6_isys_csi2_config *cfg,
				const struct ipu6_isys_csi2_timing *timing,
				bool on);
#endif /* IPU6_ISYS_H */