18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * isphist.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * TI OMAP3 ISP - Histogram module
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2010 Nokia Corporation
88c2ecf20Sopenharmony_ci * Copyright (C) 2009 Texas Instruments, Inc.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Contacts: David Cohen <dacohen@gmail.com>
118c2ecf20Sopenharmony_ci *	     Laurent Pinchart <laurent.pinchart@ideasonboard.com>
128c2ecf20Sopenharmony_ci *	     Sakari Ailus <sakari.ailus@iki.fi>
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/delay.h>
168c2ecf20Sopenharmony_ci#include <linux/device.h>
178c2ecf20Sopenharmony_ci#include <linux/dmaengine.h>
188c2ecf20Sopenharmony_ci#include <linux/slab.h>
198c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "isp.h"
228c2ecf20Sopenharmony_ci#include "ispreg.h"
238c2ecf20Sopenharmony_ci#include "isphist.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define HIST_CONFIG_DMA	1
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci * hist_reset_mem - clear Histogram memory before start stats engine.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_cistatic void hist_reset_mem(struct ispstat *hist)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct isp_device *isp = hist->isp;
338c2ecf20Sopenharmony_ci	struct omap3isp_hist_config *conf = hist->priv;
348c2ecf20Sopenharmony_ci	unsigned int i;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_HIST, ISPHIST_ADDR);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/*
398c2ecf20Sopenharmony_ci	 * By setting it, the histogram internal buffer is being cleared at the
408c2ecf20Sopenharmony_ci	 * same time it's being read. This bit must be cleared afterwards.
418c2ecf20Sopenharmony_ci	 */
428c2ecf20Sopenharmony_ci	isp_reg_set(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT, ISPHIST_CNT_CLEAR);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	/*
458c2ecf20Sopenharmony_ci	 * We'll clear 4 words at each iteration for optimization. It avoids
468c2ecf20Sopenharmony_ci	 * 3/4 of the jumps. We also know HIST_MEM_SIZE is divisible by 4.
478c2ecf20Sopenharmony_ci	 */
488c2ecf20Sopenharmony_ci	for (i = OMAP3ISP_HIST_MEM_SIZE / 4; i > 0; i--) {
498c2ecf20Sopenharmony_ci		isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
508c2ecf20Sopenharmony_ci		isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
518c2ecf20Sopenharmony_ci		isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
528c2ecf20Sopenharmony_ci		isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci	isp_reg_clr(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT, ISPHIST_CNT_CLEAR);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	hist->wait_acc_frames = conf->num_acc_frames;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/*
608c2ecf20Sopenharmony_ci * hist_setup_regs - Helper function to update Histogram registers.
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_cistatic void hist_setup_regs(struct ispstat *hist, void *priv)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	struct isp_device *isp = hist->isp;
658c2ecf20Sopenharmony_ci	struct omap3isp_hist_config *conf = priv;
668c2ecf20Sopenharmony_ci	int c;
678c2ecf20Sopenharmony_ci	u32 cnt;
688c2ecf20Sopenharmony_ci	u32 wb_gain;
698c2ecf20Sopenharmony_ci	u32 reg_hor[OMAP3ISP_HIST_MAX_REGIONS];
708c2ecf20Sopenharmony_ci	u32 reg_ver[OMAP3ISP_HIST_MAX_REGIONS];
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if (!hist->update || hist->state == ISPSTAT_DISABLED ||
738c2ecf20Sopenharmony_ci	    hist->state == ISPSTAT_DISABLING)
748c2ecf20Sopenharmony_ci		return;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	cnt = conf->cfa << ISPHIST_CNT_CFA_SHIFT;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	wb_gain = conf->wg[0] << ISPHIST_WB_GAIN_WG00_SHIFT;
798c2ecf20Sopenharmony_ci	wb_gain |= conf->wg[1] << ISPHIST_WB_GAIN_WG01_SHIFT;
808c2ecf20Sopenharmony_ci	wb_gain |= conf->wg[2] << ISPHIST_WB_GAIN_WG02_SHIFT;
818c2ecf20Sopenharmony_ci	if (conf->cfa == OMAP3ISP_HIST_CFA_BAYER)
828c2ecf20Sopenharmony_ci		wb_gain |= conf->wg[3] << ISPHIST_WB_GAIN_WG03_SHIFT;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	/* Regions size and position */
858c2ecf20Sopenharmony_ci	for (c = 0; c < OMAP3ISP_HIST_MAX_REGIONS; c++) {
868c2ecf20Sopenharmony_ci		if (c < conf->num_regions) {
878c2ecf20Sopenharmony_ci			reg_hor[c] = (conf->region[c].h_start <<
888c2ecf20Sopenharmony_ci				     ISPHIST_REG_START_SHIFT)
898c2ecf20Sopenharmony_ci				   | (conf->region[c].h_end <<
908c2ecf20Sopenharmony_ci				     ISPHIST_REG_END_SHIFT);
918c2ecf20Sopenharmony_ci			reg_ver[c] = (conf->region[c].v_start <<
928c2ecf20Sopenharmony_ci				     ISPHIST_REG_START_SHIFT)
938c2ecf20Sopenharmony_ci				   | (conf->region[c].v_end <<
948c2ecf20Sopenharmony_ci				     ISPHIST_REG_END_SHIFT);
958c2ecf20Sopenharmony_ci		} else {
968c2ecf20Sopenharmony_ci			reg_hor[c] = 0;
978c2ecf20Sopenharmony_ci			reg_ver[c] = 0;
988c2ecf20Sopenharmony_ci		}
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	cnt |= conf->hist_bins << ISPHIST_CNT_BINS_SHIFT;
1028c2ecf20Sopenharmony_ci	switch (conf->hist_bins) {
1038c2ecf20Sopenharmony_ci	case OMAP3ISP_HIST_BINS_256:
1048c2ecf20Sopenharmony_ci		cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 8) <<
1058c2ecf20Sopenharmony_ci			ISPHIST_CNT_SHIFT_SHIFT;
1068c2ecf20Sopenharmony_ci		break;
1078c2ecf20Sopenharmony_ci	case OMAP3ISP_HIST_BINS_128:
1088c2ecf20Sopenharmony_ci		cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 7) <<
1098c2ecf20Sopenharmony_ci			ISPHIST_CNT_SHIFT_SHIFT;
1108c2ecf20Sopenharmony_ci		break;
1118c2ecf20Sopenharmony_ci	case OMAP3ISP_HIST_BINS_64:
1128c2ecf20Sopenharmony_ci		cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 6) <<
1138c2ecf20Sopenharmony_ci			ISPHIST_CNT_SHIFT_SHIFT;
1148c2ecf20Sopenharmony_ci		break;
1158c2ecf20Sopenharmony_ci	default: /* OMAP3ISP_HIST_BINS_32 */
1168c2ecf20Sopenharmony_ci		cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 5) <<
1178c2ecf20Sopenharmony_ci			ISPHIST_CNT_SHIFT_SHIFT;
1188c2ecf20Sopenharmony_ci		break;
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	hist_reset_mem(hist);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	isp_reg_writel(isp, cnt, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT);
1248c2ecf20Sopenharmony_ci	isp_reg_writel(isp, wb_gain,  OMAP3_ISP_IOMEM_HIST, ISPHIST_WB_GAIN);
1258c2ecf20Sopenharmony_ci	isp_reg_writel(isp, reg_hor[0], OMAP3_ISP_IOMEM_HIST, ISPHIST_R0_HORZ);
1268c2ecf20Sopenharmony_ci	isp_reg_writel(isp, reg_ver[0], OMAP3_ISP_IOMEM_HIST, ISPHIST_R0_VERT);
1278c2ecf20Sopenharmony_ci	isp_reg_writel(isp, reg_hor[1], OMAP3_ISP_IOMEM_HIST, ISPHIST_R1_HORZ);
1288c2ecf20Sopenharmony_ci	isp_reg_writel(isp, reg_ver[1], OMAP3_ISP_IOMEM_HIST, ISPHIST_R1_VERT);
1298c2ecf20Sopenharmony_ci	isp_reg_writel(isp, reg_hor[2], OMAP3_ISP_IOMEM_HIST, ISPHIST_R2_HORZ);
1308c2ecf20Sopenharmony_ci	isp_reg_writel(isp, reg_ver[2], OMAP3_ISP_IOMEM_HIST, ISPHIST_R2_VERT);
1318c2ecf20Sopenharmony_ci	isp_reg_writel(isp, reg_hor[3], OMAP3_ISP_IOMEM_HIST, ISPHIST_R3_HORZ);
1328c2ecf20Sopenharmony_ci	isp_reg_writel(isp, reg_ver[3], OMAP3_ISP_IOMEM_HIST, ISPHIST_R3_VERT);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	hist->update = 0;
1358c2ecf20Sopenharmony_ci	hist->config_counter += hist->inc_config;
1368c2ecf20Sopenharmony_ci	hist->inc_config = 0;
1378c2ecf20Sopenharmony_ci	hist->buf_size = conf->buf_size;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic void hist_enable(struct ispstat *hist, int enable)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	if (enable) {
1438c2ecf20Sopenharmony_ci		isp_reg_set(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_PCR,
1448c2ecf20Sopenharmony_ci			    ISPHIST_PCR_ENABLE);
1458c2ecf20Sopenharmony_ci		omap3isp_subclk_enable(hist->isp, OMAP3_ISP_SUBCLK_HIST);
1468c2ecf20Sopenharmony_ci	} else {
1478c2ecf20Sopenharmony_ci		isp_reg_clr(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_PCR,
1488c2ecf20Sopenharmony_ci			    ISPHIST_PCR_ENABLE);
1498c2ecf20Sopenharmony_ci		omap3isp_subclk_disable(hist->isp, OMAP3_ISP_SUBCLK_HIST);
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic int hist_busy(struct ispstat *hist)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	return isp_reg_readl(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_PCR)
1568c2ecf20Sopenharmony_ci						& ISPHIST_PCR_BUSY;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic void hist_dma_cb(void *data)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	struct ispstat *hist = data;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	/* FIXME: The DMA engine API can't report transfer errors :-/ */
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	isp_reg_clr(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT,
1668c2ecf20Sopenharmony_ci		    ISPHIST_CNT_CLEAR);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	omap3isp_stat_dma_isr(hist);
1698c2ecf20Sopenharmony_ci	if (hist->state != ISPSTAT_DISABLED)
1708c2ecf20Sopenharmony_ci		omap3isp_hist_dma_done(hist->isp);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic int hist_buf_dma(struct ispstat *hist)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	dma_addr_t dma_addr = hist->active_buf->dma_addr;
1768c2ecf20Sopenharmony_ci	struct dma_async_tx_descriptor *tx;
1778c2ecf20Sopenharmony_ci	struct dma_slave_config cfg;
1788c2ecf20Sopenharmony_ci	dma_cookie_t cookie;
1798c2ecf20Sopenharmony_ci	int ret;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	if (unlikely(!dma_addr)) {
1828c2ecf20Sopenharmony_ci		dev_dbg(hist->isp->dev, "hist: invalid DMA buffer address\n");
1838c2ecf20Sopenharmony_ci		goto error;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	isp_reg_writel(hist->isp, 0, OMAP3_ISP_IOMEM_HIST, ISPHIST_ADDR);
1878c2ecf20Sopenharmony_ci	isp_reg_set(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT,
1888c2ecf20Sopenharmony_ci		    ISPHIST_CNT_CLEAR);
1898c2ecf20Sopenharmony_ci	omap3isp_flush(hist->isp);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	memset(&cfg, 0, sizeof(cfg));
1928c2ecf20Sopenharmony_ci	cfg.src_addr = hist->isp->mmio_hist_base_phys + ISPHIST_DATA;
1938c2ecf20Sopenharmony_ci	cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1948c2ecf20Sopenharmony_ci	cfg.src_maxburst = hist->buf_size / 4;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	ret = dmaengine_slave_config(hist->dma_ch, &cfg);
1978c2ecf20Sopenharmony_ci	if (ret < 0) {
1988c2ecf20Sopenharmony_ci		dev_dbg(hist->isp->dev,
1998c2ecf20Sopenharmony_ci			"hist: DMA slave configuration failed\n");
2008c2ecf20Sopenharmony_ci		goto error;
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	tx = dmaengine_prep_slave_single(hist->dma_ch, dma_addr,
2048c2ecf20Sopenharmony_ci					 hist->buf_size, DMA_DEV_TO_MEM,
2058c2ecf20Sopenharmony_ci					 DMA_CTRL_ACK);
2068c2ecf20Sopenharmony_ci	if (tx == NULL) {
2078c2ecf20Sopenharmony_ci		dev_dbg(hist->isp->dev,
2088c2ecf20Sopenharmony_ci			"hist: DMA slave preparation failed\n");
2098c2ecf20Sopenharmony_ci		goto error;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	tx->callback = hist_dma_cb;
2138c2ecf20Sopenharmony_ci	tx->callback_param = hist;
2148c2ecf20Sopenharmony_ci	cookie = tx->tx_submit(tx);
2158c2ecf20Sopenharmony_ci	if (dma_submit_error(cookie)) {
2168c2ecf20Sopenharmony_ci		dev_dbg(hist->isp->dev, "hist: DMA submission failed\n");
2178c2ecf20Sopenharmony_ci		goto error;
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	dma_async_issue_pending(hist->dma_ch);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	return STAT_BUF_WAITING_DMA;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cierror:
2258c2ecf20Sopenharmony_ci	hist_reset_mem(hist);
2268c2ecf20Sopenharmony_ci	return STAT_NO_BUF;
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cistatic int hist_buf_pio(struct ispstat *hist)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	struct isp_device *isp = hist->isp;
2328c2ecf20Sopenharmony_ci	u32 *buf = hist->active_buf->virt_addr;
2338c2ecf20Sopenharmony_ci	unsigned int i;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	if (!buf) {
2368c2ecf20Sopenharmony_ci		dev_dbg(isp->dev, "hist: invalid PIO buffer address\n");
2378c2ecf20Sopenharmony_ci		hist_reset_mem(hist);
2388c2ecf20Sopenharmony_ci		return STAT_NO_BUF;
2398c2ecf20Sopenharmony_ci	}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_HIST, ISPHIST_ADDR);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/*
2448c2ecf20Sopenharmony_ci	 * By setting it, the histogram internal buffer is being cleared at the
2458c2ecf20Sopenharmony_ci	 * same time it's being read. This bit must be cleared just after all
2468c2ecf20Sopenharmony_ci	 * data is acquired.
2478c2ecf20Sopenharmony_ci	 */
2488c2ecf20Sopenharmony_ci	isp_reg_set(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT, ISPHIST_CNT_CLEAR);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	/*
2518c2ecf20Sopenharmony_ci	 * We'll read 4 times a 4-bytes-word at each iteration for
2528c2ecf20Sopenharmony_ci	 * optimization. It avoids 3/4 of the jumps. We also know buf_size is
2538c2ecf20Sopenharmony_ci	 * divisible by 16.
2548c2ecf20Sopenharmony_ci	 */
2558c2ecf20Sopenharmony_ci	for (i = hist->buf_size / 16; i > 0; i--) {
2568c2ecf20Sopenharmony_ci		*buf++ = isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
2578c2ecf20Sopenharmony_ci		*buf++ = isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
2588c2ecf20Sopenharmony_ci		*buf++ = isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
2598c2ecf20Sopenharmony_ci		*buf++ = isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci	isp_reg_clr(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT,
2628c2ecf20Sopenharmony_ci		    ISPHIST_CNT_CLEAR);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	return STAT_BUF_DONE;
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci/*
2688c2ecf20Sopenharmony_ci * hist_buf_process - Callback from ISP driver for HIST interrupt.
2698c2ecf20Sopenharmony_ci */
2708c2ecf20Sopenharmony_cistatic int hist_buf_process(struct ispstat *hist)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	struct omap3isp_hist_config *user_cfg = hist->priv;
2738c2ecf20Sopenharmony_ci	int ret;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	if (atomic_read(&hist->buf_err) || hist->state != ISPSTAT_ENABLED) {
2768c2ecf20Sopenharmony_ci		hist_reset_mem(hist);
2778c2ecf20Sopenharmony_ci		return STAT_NO_BUF;
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	if (--(hist->wait_acc_frames))
2818c2ecf20Sopenharmony_ci		return STAT_NO_BUF;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	if (hist->dma_ch)
2848c2ecf20Sopenharmony_ci		ret = hist_buf_dma(hist);
2858c2ecf20Sopenharmony_ci	else
2868c2ecf20Sopenharmony_ci		ret = hist_buf_pio(hist);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	hist->wait_acc_frames = user_cfg->num_acc_frames;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	return ret;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic u32 hist_get_buf_size(struct omap3isp_hist_config *conf)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	return OMAP3ISP_HIST_MEM_SIZE_BINS(conf->hist_bins) * conf->num_regions;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci/*
2998c2ecf20Sopenharmony_ci * hist_validate_params - Helper function to check user given params.
3008c2ecf20Sopenharmony_ci * @new_conf: Pointer to user configuration structure.
3018c2ecf20Sopenharmony_ci *
3028c2ecf20Sopenharmony_ci * Returns 0 on success configuration.
3038c2ecf20Sopenharmony_ci */
3048c2ecf20Sopenharmony_cistatic int hist_validate_params(struct ispstat *hist, void *new_conf)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	struct omap3isp_hist_config *user_cfg = new_conf;
3078c2ecf20Sopenharmony_ci	int c;
3088c2ecf20Sopenharmony_ci	u32 buf_size;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if (user_cfg->cfa > OMAP3ISP_HIST_CFA_FOVEONX3)
3118c2ecf20Sopenharmony_ci		return -EINVAL;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	/* Regions size and position */
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	if ((user_cfg->num_regions < OMAP3ISP_HIST_MIN_REGIONS) ||
3168c2ecf20Sopenharmony_ci	    (user_cfg->num_regions > OMAP3ISP_HIST_MAX_REGIONS))
3178c2ecf20Sopenharmony_ci		return -EINVAL;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	/* Regions */
3208c2ecf20Sopenharmony_ci	for (c = 0; c < user_cfg->num_regions; c++) {
3218c2ecf20Sopenharmony_ci		if (user_cfg->region[c].h_start & ~ISPHIST_REG_START_END_MASK)
3228c2ecf20Sopenharmony_ci			return -EINVAL;
3238c2ecf20Sopenharmony_ci		if (user_cfg->region[c].h_end & ~ISPHIST_REG_START_END_MASK)
3248c2ecf20Sopenharmony_ci			return -EINVAL;
3258c2ecf20Sopenharmony_ci		if (user_cfg->region[c].v_start & ~ISPHIST_REG_START_END_MASK)
3268c2ecf20Sopenharmony_ci			return -EINVAL;
3278c2ecf20Sopenharmony_ci		if (user_cfg->region[c].v_end & ~ISPHIST_REG_START_END_MASK)
3288c2ecf20Sopenharmony_ci			return -EINVAL;
3298c2ecf20Sopenharmony_ci		if (user_cfg->region[c].h_start > user_cfg->region[c].h_end)
3308c2ecf20Sopenharmony_ci			return -EINVAL;
3318c2ecf20Sopenharmony_ci		if (user_cfg->region[c].v_start > user_cfg->region[c].v_end)
3328c2ecf20Sopenharmony_ci			return -EINVAL;
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	switch (user_cfg->num_regions) {
3368c2ecf20Sopenharmony_ci	case 1:
3378c2ecf20Sopenharmony_ci		if (user_cfg->hist_bins > OMAP3ISP_HIST_BINS_256)
3388c2ecf20Sopenharmony_ci			return -EINVAL;
3398c2ecf20Sopenharmony_ci		break;
3408c2ecf20Sopenharmony_ci	case 2:
3418c2ecf20Sopenharmony_ci		if (user_cfg->hist_bins > OMAP3ISP_HIST_BINS_128)
3428c2ecf20Sopenharmony_ci			return -EINVAL;
3438c2ecf20Sopenharmony_ci		break;
3448c2ecf20Sopenharmony_ci	default: /* 3 or 4 */
3458c2ecf20Sopenharmony_ci		if (user_cfg->hist_bins > OMAP3ISP_HIST_BINS_64)
3468c2ecf20Sopenharmony_ci			return -EINVAL;
3478c2ecf20Sopenharmony_ci		break;
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	buf_size = hist_get_buf_size(user_cfg);
3518c2ecf20Sopenharmony_ci	if (buf_size > user_cfg->buf_size)
3528c2ecf20Sopenharmony_ci		/* User's buf_size request wasn't enough */
3538c2ecf20Sopenharmony_ci		user_cfg->buf_size = buf_size;
3548c2ecf20Sopenharmony_ci	else if (user_cfg->buf_size > OMAP3ISP_HIST_MAX_BUF_SIZE)
3558c2ecf20Sopenharmony_ci		user_cfg->buf_size = OMAP3ISP_HIST_MAX_BUF_SIZE;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	return 0;
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic int hist_comp_params(struct ispstat *hist,
3618c2ecf20Sopenharmony_ci			    struct omap3isp_hist_config *user_cfg)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	struct omap3isp_hist_config *cur_cfg = hist->priv;
3648c2ecf20Sopenharmony_ci	int c;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	if (cur_cfg->cfa != user_cfg->cfa)
3678c2ecf20Sopenharmony_ci		return 1;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	if (cur_cfg->num_acc_frames != user_cfg->num_acc_frames)
3708c2ecf20Sopenharmony_ci		return 1;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	if (cur_cfg->hist_bins != user_cfg->hist_bins)
3738c2ecf20Sopenharmony_ci		return 1;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	for (c = 0; c < OMAP3ISP_HIST_MAX_WG; c++) {
3768c2ecf20Sopenharmony_ci		if (c == 3 && user_cfg->cfa == OMAP3ISP_HIST_CFA_FOVEONX3)
3778c2ecf20Sopenharmony_ci			break;
3788c2ecf20Sopenharmony_ci		else if (cur_cfg->wg[c] != user_cfg->wg[c])
3798c2ecf20Sopenharmony_ci			return 1;
3808c2ecf20Sopenharmony_ci	}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	if (cur_cfg->num_regions != user_cfg->num_regions)
3838c2ecf20Sopenharmony_ci		return 1;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	/* Regions */
3868c2ecf20Sopenharmony_ci	for (c = 0; c < user_cfg->num_regions; c++) {
3878c2ecf20Sopenharmony_ci		if (cur_cfg->region[c].h_start != user_cfg->region[c].h_start)
3888c2ecf20Sopenharmony_ci			return 1;
3898c2ecf20Sopenharmony_ci		if (cur_cfg->region[c].h_end != user_cfg->region[c].h_end)
3908c2ecf20Sopenharmony_ci			return 1;
3918c2ecf20Sopenharmony_ci		if (cur_cfg->region[c].v_start != user_cfg->region[c].v_start)
3928c2ecf20Sopenharmony_ci			return 1;
3938c2ecf20Sopenharmony_ci		if (cur_cfg->region[c].v_end != user_cfg->region[c].v_end)
3948c2ecf20Sopenharmony_ci			return 1;
3958c2ecf20Sopenharmony_ci	}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	return 0;
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci/*
4018c2ecf20Sopenharmony_ci * hist_update_params - Helper function to check and store user given params.
4028c2ecf20Sopenharmony_ci * @new_conf: Pointer to user configuration structure.
4038c2ecf20Sopenharmony_ci */
4048c2ecf20Sopenharmony_cistatic void hist_set_params(struct ispstat *hist, void *new_conf)
4058c2ecf20Sopenharmony_ci{
4068c2ecf20Sopenharmony_ci	struct omap3isp_hist_config *user_cfg = new_conf;
4078c2ecf20Sopenharmony_ci	struct omap3isp_hist_config *cur_cfg = hist->priv;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	if (!hist->configured || hist_comp_params(hist, user_cfg)) {
4108c2ecf20Sopenharmony_ci		memcpy(cur_cfg, user_cfg, sizeof(*user_cfg));
4118c2ecf20Sopenharmony_ci		if (user_cfg->num_acc_frames == 0)
4128c2ecf20Sopenharmony_ci			user_cfg->num_acc_frames = 1;
4138c2ecf20Sopenharmony_ci		hist->inc_config++;
4148c2ecf20Sopenharmony_ci		hist->update = 1;
4158c2ecf20Sopenharmony_ci		/*
4168c2ecf20Sopenharmony_ci		 * User might be asked for a bigger buffer than necessary for
4178c2ecf20Sopenharmony_ci		 * this configuration. In order to return the right amount of
4188c2ecf20Sopenharmony_ci		 * data during buffer request, let's calculate the size here
4198c2ecf20Sopenharmony_ci		 * instead of stick with user_cfg->buf_size.
4208c2ecf20Sopenharmony_ci		 */
4218c2ecf20Sopenharmony_ci		cur_cfg->buf_size = hist_get_buf_size(cur_cfg);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_cistatic long hist_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	struct ispstat *stat = v4l2_get_subdevdata(sd);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	switch (cmd) {
4318c2ecf20Sopenharmony_ci	case VIDIOC_OMAP3ISP_HIST_CFG:
4328c2ecf20Sopenharmony_ci		return omap3isp_stat_config(stat, arg);
4338c2ecf20Sopenharmony_ci	case VIDIOC_OMAP3ISP_STAT_REQ:
4348c2ecf20Sopenharmony_ci		return omap3isp_stat_request_statistics(stat, arg);
4358c2ecf20Sopenharmony_ci	case VIDIOC_OMAP3ISP_STAT_REQ_TIME32:
4368c2ecf20Sopenharmony_ci		return omap3isp_stat_request_statistics_time32(stat, arg);
4378c2ecf20Sopenharmony_ci	case VIDIOC_OMAP3ISP_STAT_EN: {
4388c2ecf20Sopenharmony_ci		int *en = arg;
4398c2ecf20Sopenharmony_ci		return omap3isp_stat_enable(stat, !!*en);
4408c2ecf20Sopenharmony_ci	}
4418c2ecf20Sopenharmony_ci	}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	return -ENOIOCTLCMD;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistatic const struct ispstat_ops hist_ops = {
4488c2ecf20Sopenharmony_ci	.validate_params	= hist_validate_params,
4498c2ecf20Sopenharmony_ci	.set_params		= hist_set_params,
4508c2ecf20Sopenharmony_ci	.setup_regs		= hist_setup_regs,
4518c2ecf20Sopenharmony_ci	.enable			= hist_enable,
4528c2ecf20Sopenharmony_ci	.busy			= hist_busy,
4538c2ecf20Sopenharmony_ci	.buf_process		= hist_buf_process,
4548c2ecf20Sopenharmony_ci};
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops hist_subdev_core_ops = {
4578c2ecf20Sopenharmony_ci	.ioctl = hist_ioctl,
4588c2ecf20Sopenharmony_ci	.subscribe_event = omap3isp_stat_subscribe_event,
4598c2ecf20Sopenharmony_ci	.unsubscribe_event = omap3isp_stat_unsubscribe_event,
4608c2ecf20Sopenharmony_ci};
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops hist_subdev_video_ops = {
4638c2ecf20Sopenharmony_ci	.s_stream = omap3isp_stat_s_stream,
4648c2ecf20Sopenharmony_ci};
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops hist_subdev_ops = {
4678c2ecf20Sopenharmony_ci	.core = &hist_subdev_core_ops,
4688c2ecf20Sopenharmony_ci	.video = &hist_subdev_video_ops,
4698c2ecf20Sopenharmony_ci};
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci/*
4728c2ecf20Sopenharmony_ci * omap3isp_hist_init - Module Initialization.
4738c2ecf20Sopenharmony_ci */
4748c2ecf20Sopenharmony_ciint omap3isp_hist_init(struct isp_device *isp)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	struct ispstat *hist = &isp->isp_hist;
4778c2ecf20Sopenharmony_ci	struct omap3isp_hist_config *hist_cfg;
4788c2ecf20Sopenharmony_ci	int ret;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	hist_cfg = kzalloc(sizeof(*hist_cfg), GFP_KERNEL);
4818c2ecf20Sopenharmony_ci	if (hist_cfg == NULL)
4828c2ecf20Sopenharmony_ci		return -ENOMEM;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	hist->isp = isp;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	if (HIST_CONFIG_DMA) {
4878c2ecf20Sopenharmony_ci		dma_cap_mask_t mask;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci		/*
4908c2ecf20Sopenharmony_ci		 * We need slave capable channel without DMA request line for
4918c2ecf20Sopenharmony_ci		 * reading out the data.
4928c2ecf20Sopenharmony_ci		 * For this we can use dma_request_chan_by_mask() as we are
4938c2ecf20Sopenharmony_ci		 * happy with any channel as long as it is capable of slave
4948c2ecf20Sopenharmony_ci		 * configuration.
4958c2ecf20Sopenharmony_ci		 */
4968c2ecf20Sopenharmony_ci		dma_cap_zero(mask);
4978c2ecf20Sopenharmony_ci		dma_cap_set(DMA_SLAVE, mask);
4988c2ecf20Sopenharmony_ci		hist->dma_ch = dma_request_chan_by_mask(&mask);
4998c2ecf20Sopenharmony_ci		if (IS_ERR(hist->dma_ch)) {
5008c2ecf20Sopenharmony_ci			ret = PTR_ERR(hist->dma_ch);
5018c2ecf20Sopenharmony_ci			if (ret == -EPROBE_DEFER)
5028c2ecf20Sopenharmony_ci				goto err;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci			hist->dma_ch = NULL;
5058c2ecf20Sopenharmony_ci			dev_warn(isp->dev,
5068c2ecf20Sopenharmony_ci				 "hist: DMA channel request failed, using PIO\n");
5078c2ecf20Sopenharmony_ci		} else {
5088c2ecf20Sopenharmony_ci			dev_dbg(isp->dev, "hist: using DMA channel %s\n",
5098c2ecf20Sopenharmony_ci				dma_chan_name(hist->dma_ch));
5108c2ecf20Sopenharmony_ci		}
5118c2ecf20Sopenharmony_ci	}
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	hist->ops = &hist_ops;
5148c2ecf20Sopenharmony_ci	hist->priv = hist_cfg;
5158c2ecf20Sopenharmony_ci	hist->event_type = V4L2_EVENT_OMAP3ISP_HIST;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	ret = omap3isp_stat_init(hist, "histogram", &hist_subdev_ops);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cierr:
5208c2ecf20Sopenharmony_ci	if (ret) {
5218c2ecf20Sopenharmony_ci		if (!IS_ERR_OR_NULL(hist->dma_ch))
5228c2ecf20Sopenharmony_ci			dma_release_channel(hist->dma_ch);
5238c2ecf20Sopenharmony_ci		kfree(hist_cfg);
5248c2ecf20Sopenharmony_ci	}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	return ret;
5278c2ecf20Sopenharmony_ci}
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci/*
5308c2ecf20Sopenharmony_ci * omap3isp_hist_cleanup - Module cleanup.
5318c2ecf20Sopenharmony_ci */
5328c2ecf20Sopenharmony_civoid omap3isp_hist_cleanup(struct isp_device *isp)
5338c2ecf20Sopenharmony_ci{
5348c2ecf20Sopenharmony_ci	struct ispstat *hist = &isp->isp_hist;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	if (hist->dma_ch)
5378c2ecf20Sopenharmony_ci		dma_release_channel(hist->dma_ch);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	omap3isp_stat_cleanup(hist);
5408c2ecf20Sopenharmony_ci}
541