162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * ispstat.h
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * TI OMAP3 ISP - Statistics core
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright (C) 2010 Nokia Corporation
862306a36Sopenharmony_ci * Copyright (C) 2009 Texas Instruments, Inc
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * Contacts: David Cohen <dacohen@gmail.com>
1162306a36Sopenharmony_ci *	     Laurent Pinchart <laurent.pinchart@ideasonboard.com>
1262306a36Sopenharmony_ci *	     Sakari Ailus <sakari.ailus@iki.fi>
1362306a36Sopenharmony_ci */
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#ifndef OMAP3_ISP_STAT_H
1662306a36Sopenharmony_ci#define OMAP3_ISP_STAT_H
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#include <linux/types.h>
1962306a36Sopenharmony_ci#include <linux/omap3isp.h>
2062306a36Sopenharmony_ci#include <media/v4l2-event.h>
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#include "isp.h"
2362306a36Sopenharmony_ci#include "ispvideo.h"
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#define STAT_MAX_BUFS		5
2662306a36Sopenharmony_ci#define STAT_NEVENTS		8
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define STAT_BUF_DONE		0	/* Buffer is ready */
2962306a36Sopenharmony_ci#define STAT_NO_BUF		1	/* An error has occurred */
3062306a36Sopenharmony_ci#define STAT_BUF_WAITING_DMA	2	/* Histogram only: DMA is running */
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistruct dma_chan;
3362306a36Sopenharmony_cistruct ispstat;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_cistruct ispstat_buffer {
3662306a36Sopenharmony_ci	struct sg_table sgt;
3762306a36Sopenharmony_ci	void *virt_addr;
3862306a36Sopenharmony_ci	dma_addr_t dma_addr;
3962306a36Sopenharmony_ci	struct timespec64 ts;
4062306a36Sopenharmony_ci	u32 buf_size;
4162306a36Sopenharmony_ci	u32 frame_number;
4262306a36Sopenharmony_ci	u16 config_counter;
4362306a36Sopenharmony_ci	u8 empty;
4462306a36Sopenharmony_ci};
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistruct ispstat_ops {
4762306a36Sopenharmony_ci	/*
4862306a36Sopenharmony_ci	 * Validate new params configuration.
4962306a36Sopenharmony_ci	 * new_conf->buf_size value must be changed to the exact buffer size
5062306a36Sopenharmony_ci	 * necessary for the new configuration if it's smaller.
5162306a36Sopenharmony_ci	 */
5262306a36Sopenharmony_ci	int (*validate_params)(struct ispstat *stat, void *new_conf);
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	/*
5562306a36Sopenharmony_ci	 * Save new params configuration.
5662306a36Sopenharmony_ci	 * stat->priv->buf_size value must be set to the exact buffer size for
5762306a36Sopenharmony_ci	 * the new configuration.
5862306a36Sopenharmony_ci	 * stat->update is set to 1 if new configuration is different than
5962306a36Sopenharmony_ci	 * current one.
6062306a36Sopenharmony_ci	 */
6162306a36Sopenharmony_ci	void (*set_params)(struct ispstat *stat, void *new_conf);
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	/* Apply stored configuration. */
6462306a36Sopenharmony_ci	void (*setup_regs)(struct ispstat *stat, void *priv);
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	/* Enable/Disable module. */
6762306a36Sopenharmony_ci	void (*enable)(struct ispstat *stat, int enable);
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	/* Verify is module is busy. */
7062306a36Sopenharmony_ci	int (*busy)(struct ispstat *stat);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	/* Used for specific operations during generic buf process task. */
7362306a36Sopenharmony_ci	int (*buf_process)(struct ispstat *stat);
7462306a36Sopenharmony_ci};
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cienum ispstat_state_t {
7762306a36Sopenharmony_ci	ISPSTAT_DISABLED = 0,
7862306a36Sopenharmony_ci	ISPSTAT_DISABLING,
7962306a36Sopenharmony_ci	ISPSTAT_ENABLED,
8062306a36Sopenharmony_ci	ISPSTAT_ENABLING,
8162306a36Sopenharmony_ci	ISPSTAT_SUSPENDED,
8262306a36Sopenharmony_ci};
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_cistruct ispstat {
8562306a36Sopenharmony_ci	struct v4l2_subdev subdev;
8662306a36Sopenharmony_ci	struct media_pad pad;	/* sink pad */
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	/* Control */
8962306a36Sopenharmony_ci	unsigned configured:1;
9062306a36Sopenharmony_ci	unsigned update:1;
9162306a36Sopenharmony_ci	unsigned buf_processing:1;
9262306a36Sopenharmony_ci	unsigned sbl_ovl_recover:1;
9362306a36Sopenharmony_ci	u8 inc_config;
9462306a36Sopenharmony_ci	atomic_t buf_err;
9562306a36Sopenharmony_ci	enum ispstat_state_t state;	/* enabling/disabling state */
9662306a36Sopenharmony_ci	struct isp_device *isp;
9762306a36Sopenharmony_ci	void *priv;		/* pointer to priv config struct */
9862306a36Sopenharmony_ci	void *recover_priv;	/* pointer to recover priv configuration */
9962306a36Sopenharmony_ci	struct mutex ioctl_lock; /* serialize private ioctl */
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	const struct ispstat_ops *ops;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	/* Buffer */
10462306a36Sopenharmony_ci	u8 wait_acc_frames;
10562306a36Sopenharmony_ci	u16 config_counter;
10662306a36Sopenharmony_ci	u32 frame_number;
10762306a36Sopenharmony_ci	u32 buf_size;
10862306a36Sopenharmony_ci	u32 buf_alloc_size;
10962306a36Sopenharmony_ci	struct dma_chan *dma_ch;
11062306a36Sopenharmony_ci	unsigned long event_type;
11162306a36Sopenharmony_ci	struct ispstat_buffer *buf;
11262306a36Sopenharmony_ci	struct ispstat_buffer *active_buf;
11362306a36Sopenharmony_ci	struct ispstat_buffer *locked_buf;
11462306a36Sopenharmony_ci};
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistruct ispstat_generic_config {
11762306a36Sopenharmony_ci	/*
11862306a36Sopenharmony_ci	 * Fields must be in the same order as in:
11962306a36Sopenharmony_ci	 *  - omap3isp_h3a_aewb_config
12062306a36Sopenharmony_ci	 *  - omap3isp_h3a_af_config
12162306a36Sopenharmony_ci	 *  - omap3isp_hist_config
12262306a36Sopenharmony_ci	 */
12362306a36Sopenharmony_ci	u32 buf_size;
12462306a36Sopenharmony_ci	u16 config_counter;
12562306a36Sopenharmony_ci};
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ciint omap3isp_stat_config(struct ispstat *stat, void *new_conf);
12862306a36Sopenharmony_ciint omap3isp_stat_request_statistics(struct ispstat *stat,
12962306a36Sopenharmony_ci				     struct omap3isp_stat_data *data);
13062306a36Sopenharmony_ciint omap3isp_stat_request_statistics_time32(struct ispstat *stat,
13162306a36Sopenharmony_ci				     struct omap3isp_stat_data_time32 *data);
13262306a36Sopenharmony_ciint omap3isp_stat_init(struct ispstat *stat, const char *name,
13362306a36Sopenharmony_ci		       const struct v4l2_subdev_ops *sd_ops);
13462306a36Sopenharmony_civoid omap3isp_stat_cleanup(struct ispstat *stat);
13562306a36Sopenharmony_ciint omap3isp_stat_subscribe_event(struct v4l2_subdev *subdev,
13662306a36Sopenharmony_ci				  struct v4l2_fh *fh,
13762306a36Sopenharmony_ci				  struct v4l2_event_subscription *sub);
13862306a36Sopenharmony_ciint omap3isp_stat_unsubscribe_event(struct v4l2_subdev *subdev,
13962306a36Sopenharmony_ci				    struct v4l2_fh *fh,
14062306a36Sopenharmony_ci				    struct v4l2_event_subscription *sub);
14162306a36Sopenharmony_ciint omap3isp_stat_s_stream(struct v4l2_subdev *subdev, int enable);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ciint omap3isp_stat_busy(struct ispstat *stat);
14462306a36Sopenharmony_ciint omap3isp_stat_pcr_busy(struct ispstat *stat);
14562306a36Sopenharmony_civoid omap3isp_stat_suspend(struct ispstat *stat);
14662306a36Sopenharmony_civoid omap3isp_stat_resume(struct ispstat *stat);
14762306a36Sopenharmony_ciint omap3isp_stat_enable(struct ispstat *stat, u8 enable);
14862306a36Sopenharmony_civoid omap3isp_stat_sbl_overflow(struct ispstat *stat);
14962306a36Sopenharmony_civoid omap3isp_stat_isr(struct ispstat *stat);
15062306a36Sopenharmony_civoid omap3isp_stat_isr_frame_sync(struct ispstat *stat);
15162306a36Sopenharmony_civoid omap3isp_stat_dma_isr(struct ispstat *stat);
15262306a36Sopenharmony_ciint omap3isp_stat_register_entities(struct ispstat *stat,
15362306a36Sopenharmony_ci				    struct v4l2_device *vdev);
15462306a36Sopenharmony_civoid omap3isp_stat_unregister_entities(struct ispstat *stat);
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci#endif /* OMAP3_ISP_STAT_H */
157