/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
/*
 * Copyright (c) 2021 MediaTek Inc.
 */

#ifndef MTK_DMX_TM_H
#define MTK_DMX_TM_H

#include <linux/spinlock.h>
#include <media/dvb_demux2.h>

#include "mtk_dmx_filter.h"

#define TSP_FW_NAME			"fwTSP.bin"
#define SCMB_MON_TIMEOUT	(20)
#define	MAX_SCRM_WAITING_TIME	(500)
#define	MAX_SCRM_WAITING_TIME_FILEIN	(40)
#define LOCK_WAIT_COUNT		(MAX_SCRM_WAITING_TIME/SCMB_MON_TIMEOUT)
#define LOCK_WAIT_COUNT_FILEIN	(MAX_SCRM_WAITING_TIME_FILEIN/SCMB_MON_TIMEOUT)
#define CID_MON_TIMEOUT		(40)
#define PCR_INT_MON_TIMEOUT		(10)
#define MAD_INT_ID	(2)

typedef int (*timer_fn_t)(void *data);

enum sw_interrupt_event {
	SW_INT_EVENT_SEC_READY,
	SW_INT_EVENT_SEC_CRC_ERR,
	SW_INT_EVENT_SEC_BUF_OVERFLOW,
	SW_INT_EVENT_HACKED,
	SW_INT_EVENT_MAX,
	SW_INT_EVENT_NULL = -1
};

enum tsp_irq_type {
	TSP_IRQ_NONE,
	TSP_IRQ_PCR,
	TSP_IRQ_SEC,
	TSP_IRQ_NTP
};

struct tsp_irq_pcr {
	u16 idx;
};

struct tsp_irq_sec {
	u16 idx;
	enum sw_interrupt_event event;
};

struct tsp_irq_ntp {
	u16 path;
};

struct tsp_irq_args {
	enum tsp_irq_type type;
	union {
		struct tsp_irq_pcr pcr;
		struct tsp_irq_sec sec;
		struct tsp_irq_ntp ntp;
	};
	struct list_head list;
	int uniq;
};

struct mtk_dmx_tm {
	int irq;
	int pas_irq;
	int mad_irq;
	int mad1_irq;
	int mad2_irq;
	int mad3_irq;
	int mad4_irq;
	struct device *dev;
	struct mtk_dmx_rm *rm;
	rwlock_t fh_lock;
	atomic_t fh_lock_wr_cnt;
	atomic_t fh_lock_rd_cnt;
	spinlock_t irq_lock;
	struct list_head irq_list_head;
	atomic_t irq_uniq;
};

int mtk_dmx_tm_pvr_rec_task(void *data);
int mtk_dmx_tm_pvr_rec_monitor(void *data);
int mtk_dmx_tm_pvr_feed_task(void *data);
int mtk_dmx_tm_scmb_monitor(void *data);
int mtk_dmx_tm_cid_monitor(void *data);
int mtk_dmx_tm_time_int_monitor(void *data);
int mtk_dmx_tm_path_input_monitor(void *data);

int mtk_dmx_tm_init(struct device *dev, struct mtk_dmx_tm *tm);
int mtk_dmx_tm_exit(void);

int mtk_dmx_tm_handle_stc_event(
		struct dmx2_filter *dmx2_filter,
		struct stc_event *stc_event,
		struct stc_params *stc_params);

#endif				/* MTK_DMX_TM_H */

