perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / platform / omap3isp / isph3a_aewb.c
blob3c82dea4d375f16c3b1712683ac4a55e80309fb1
1 /*
2 * isph3a.c
4 * TI OMAP3 ISP - H3A module
6 * Copyright (C) 2010 Nokia Corporation
7 * Copyright (C) 2009 Texas Instruments, Inc.
9 * Contacts: David Cohen <dacohen@gmail.com>
10 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11 * Sakari Ailus <sakari.ailus@iki.fi>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/slab.h>
19 #include <linux/uaccess.h>
21 #include "isp.h"
22 #include "isph3a.h"
23 #include "ispstat.h"
26 * h3a_aewb_update_regs - Helper function to update h3a registers.
28 static void h3a_aewb_setup_regs(struct ispstat *aewb, void *priv)
30 struct omap3isp_h3a_aewb_config *conf = priv;
31 u32 pcr;
32 u32 win1;
33 u32 start;
34 u32 blk;
35 u32 subwin;
37 if (aewb->state == ISPSTAT_DISABLED)
38 return;
40 isp_reg_writel(aewb->isp, aewb->active_buf->dma_addr,
41 OMAP3_ISP_IOMEM_H3A, ISPH3A_AEWBUFST);
43 if (!aewb->update)
44 return;
46 /* Converting config metadata into reg values */
47 pcr = conf->saturation_limit << ISPH3A_PCR_AEW_AVE2LMT_SHIFT;
48 pcr |= !!conf->alaw_enable << ISPH3A_PCR_AEW_ALAW_EN_SHIFT;
50 win1 = ((conf->win_height >> 1) - 1) << ISPH3A_AEWWIN1_WINH_SHIFT;
51 win1 |= ((conf->win_width >> 1) - 1) << ISPH3A_AEWWIN1_WINW_SHIFT;
52 win1 |= (conf->ver_win_count - 1) << ISPH3A_AEWWIN1_WINVC_SHIFT;
53 win1 |= (conf->hor_win_count - 1) << ISPH3A_AEWWIN1_WINHC_SHIFT;
55 start = conf->hor_win_start << ISPH3A_AEWINSTART_WINSH_SHIFT;
56 start |= conf->ver_win_start << ISPH3A_AEWINSTART_WINSV_SHIFT;
58 blk = conf->blk_ver_win_start << ISPH3A_AEWINBLK_WINSV_SHIFT;
59 blk |= ((conf->blk_win_height >> 1) - 1) << ISPH3A_AEWINBLK_WINH_SHIFT;
61 subwin = ((conf->subsample_ver_inc >> 1) - 1) <<
62 ISPH3A_AEWSUBWIN_AEWINCV_SHIFT;
63 subwin |= ((conf->subsample_hor_inc >> 1) - 1) <<
64 ISPH3A_AEWSUBWIN_AEWINCH_SHIFT;
66 isp_reg_writel(aewb->isp, win1, OMAP3_ISP_IOMEM_H3A, ISPH3A_AEWWIN1);
67 isp_reg_writel(aewb->isp, start, OMAP3_ISP_IOMEM_H3A,
68 ISPH3A_AEWINSTART);
69 isp_reg_writel(aewb->isp, blk, OMAP3_ISP_IOMEM_H3A, ISPH3A_AEWINBLK);
70 isp_reg_writel(aewb->isp, subwin, OMAP3_ISP_IOMEM_H3A,
71 ISPH3A_AEWSUBWIN);
72 isp_reg_clr_set(aewb->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
73 ISPH3A_PCR_AEW_MASK, pcr);
75 aewb->update = 0;
76 aewb->config_counter += aewb->inc_config;
77 aewb->inc_config = 0;
78 aewb->buf_size = conf->buf_size;
81 static void h3a_aewb_enable(struct ispstat *aewb, int enable)
83 if (enable) {
84 isp_reg_set(aewb->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
85 ISPH3A_PCR_AEW_EN);
86 omap3isp_subclk_enable(aewb->isp, OMAP3_ISP_SUBCLK_AEWB);
87 } else {
88 isp_reg_clr(aewb->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
89 ISPH3A_PCR_AEW_EN);
90 omap3isp_subclk_disable(aewb->isp, OMAP3_ISP_SUBCLK_AEWB);
94 static int h3a_aewb_busy(struct ispstat *aewb)
96 return isp_reg_readl(aewb->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR)
97 & ISPH3A_PCR_BUSYAEAWB;
100 static u32 h3a_aewb_get_buf_size(struct omap3isp_h3a_aewb_config *conf)
102 /* Number of configured windows + extra row for black data */
103 u32 win_count = (conf->ver_win_count + 1) * conf->hor_win_count;
106 * Unsaturated block counts for each 8 windows.
107 * 1 extra for the last (win_count % 8) windows if win_count is not
108 * divisible by 8.
110 win_count += (win_count + 7) / 8;
112 return win_count * AEWB_PACKET_SIZE;
115 static int h3a_aewb_validate_params(struct ispstat *aewb, void *new_conf)
117 struct omap3isp_h3a_aewb_config *user_cfg = new_conf;
118 u32 buf_size;
120 if (unlikely(user_cfg->saturation_limit >
121 OMAP3ISP_AEWB_MAX_SATURATION_LIM))
122 return -EINVAL;
124 if (unlikely(user_cfg->win_height < OMAP3ISP_AEWB_MIN_WIN_H ||
125 user_cfg->win_height > OMAP3ISP_AEWB_MAX_WIN_H ||
126 user_cfg->win_height & 0x01))
127 return -EINVAL;
129 if (unlikely(user_cfg->win_width < OMAP3ISP_AEWB_MIN_WIN_W ||
130 user_cfg->win_width > OMAP3ISP_AEWB_MAX_WIN_W ||
131 user_cfg->win_width & 0x01))
132 return -EINVAL;
134 if (unlikely(user_cfg->ver_win_count < OMAP3ISP_AEWB_MIN_WINVC ||
135 user_cfg->ver_win_count > OMAP3ISP_AEWB_MAX_WINVC))
136 return -EINVAL;
138 if (unlikely(user_cfg->hor_win_count < OMAP3ISP_AEWB_MIN_WINHC ||
139 user_cfg->hor_win_count > OMAP3ISP_AEWB_MAX_WINHC))
140 return -EINVAL;
142 if (unlikely(user_cfg->ver_win_start > OMAP3ISP_AEWB_MAX_WINSTART))
143 return -EINVAL;
145 if (unlikely(user_cfg->hor_win_start > OMAP3ISP_AEWB_MAX_WINSTART))
146 return -EINVAL;
148 if (unlikely(user_cfg->blk_ver_win_start > OMAP3ISP_AEWB_MAX_WINSTART))
149 return -EINVAL;
151 if (unlikely(user_cfg->blk_win_height < OMAP3ISP_AEWB_MIN_WIN_H ||
152 user_cfg->blk_win_height > OMAP3ISP_AEWB_MAX_WIN_H ||
153 user_cfg->blk_win_height & 0x01))
154 return -EINVAL;
156 if (unlikely(user_cfg->subsample_ver_inc < OMAP3ISP_AEWB_MIN_SUB_INC ||
157 user_cfg->subsample_ver_inc > OMAP3ISP_AEWB_MAX_SUB_INC ||
158 user_cfg->subsample_ver_inc & 0x01))
159 return -EINVAL;
161 if (unlikely(user_cfg->subsample_hor_inc < OMAP3ISP_AEWB_MIN_SUB_INC ||
162 user_cfg->subsample_hor_inc > OMAP3ISP_AEWB_MAX_SUB_INC ||
163 user_cfg->subsample_hor_inc & 0x01))
164 return -EINVAL;
166 buf_size = h3a_aewb_get_buf_size(user_cfg);
167 if (buf_size > user_cfg->buf_size)
168 user_cfg->buf_size = buf_size;
169 else if (user_cfg->buf_size > OMAP3ISP_AEWB_MAX_BUF_SIZE)
170 user_cfg->buf_size = OMAP3ISP_AEWB_MAX_BUF_SIZE;
172 return 0;
176 * h3a_aewb_set_params - Helper function to check & store user given params.
177 * @new_conf: Pointer to AE and AWB parameters struct.
179 * As most of them are busy-lock registers, need to wait until AEW_BUSY = 0 to
180 * program them during ISR.
182 static void h3a_aewb_set_params(struct ispstat *aewb, void *new_conf)
184 struct omap3isp_h3a_aewb_config *user_cfg = new_conf;
185 struct omap3isp_h3a_aewb_config *cur_cfg = aewb->priv;
186 int update = 0;
188 if (cur_cfg->saturation_limit != user_cfg->saturation_limit) {
189 cur_cfg->saturation_limit = user_cfg->saturation_limit;
190 update = 1;
192 if (cur_cfg->alaw_enable != user_cfg->alaw_enable) {
193 cur_cfg->alaw_enable = user_cfg->alaw_enable;
194 update = 1;
196 if (cur_cfg->win_height != user_cfg->win_height) {
197 cur_cfg->win_height = user_cfg->win_height;
198 update = 1;
200 if (cur_cfg->win_width != user_cfg->win_width) {
201 cur_cfg->win_width = user_cfg->win_width;
202 update = 1;
204 if (cur_cfg->ver_win_count != user_cfg->ver_win_count) {
205 cur_cfg->ver_win_count = user_cfg->ver_win_count;
206 update = 1;
208 if (cur_cfg->hor_win_count != user_cfg->hor_win_count) {
209 cur_cfg->hor_win_count = user_cfg->hor_win_count;
210 update = 1;
212 if (cur_cfg->ver_win_start != user_cfg->ver_win_start) {
213 cur_cfg->ver_win_start = user_cfg->ver_win_start;
214 update = 1;
216 if (cur_cfg->hor_win_start != user_cfg->hor_win_start) {
217 cur_cfg->hor_win_start = user_cfg->hor_win_start;
218 update = 1;
220 if (cur_cfg->blk_ver_win_start != user_cfg->blk_ver_win_start) {
221 cur_cfg->blk_ver_win_start = user_cfg->blk_ver_win_start;
222 update = 1;
224 if (cur_cfg->blk_win_height != user_cfg->blk_win_height) {
225 cur_cfg->blk_win_height = user_cfg->blk_win_height;
226 update = 1;
228 if (cur_cfg->subsample_ver_inc != user_cfg->subsample_ver_inc) {
229 cur_cfg->subsample_ver_inc = user_cfg->subsample_ver_inc;
230 update = 1;
232 if (cur_cfg->subsample_hor_inc != user_cfg->subsample_hor_inc) {
233 cur_cfg->subsample_hor_inc = user_cfg->subsample_hor_inc;
234 update = 1;
237 if (update || !aewb->configured) {
238 aewb->inc_config++;
239 aewb->update = 1;
240 cur_cfg->buf_size = h3a_aewb_get_buf_size(cur_cfg);
244 static long h3a_aewb_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
246 struct ispstat *stat = v4l2_get_subdevdata(sd);
248 switch (cmd) {
249 case VIDIOC_OMAP3ISP_AEWB_CFG:
250 return omap3isp_stat_config(stat, arg);
251 case VIDIOC_OMAP3ISP_STAT_REQ:
252 return omap3isp_stat_request_statistics(stat, arg);
253 case VIDIOC_OMAP3ISP_STAT_REQ_TIME32:
254 return omap3isp_stat_request_statistics_time32(stat, arg);
255 case VIDIOC_OMAP3ISP_STAT_EN: {
256 unsigned long *en = arg;
257 return omap3isp_stat_enable(stat, !!*en);
261 return -ENOIOCTLCMD;
264 static const struct ispstat_ops h3a_aewb_ops = {
265 .validate_params = h3a_aewb_validate_params,
266 .set_params = h3a_aewb_set_params,
267 .setup_regs = h3a_aewb_setup_regs,
268 .enable = h3a_aewb_enable,
269 .busy = h3a_aewb_busy,
272 static const struct v4l2_subdev_core_ops h3a_aewb_subdev_core_ops = {
273 .ioctl = h3a_aewb_ioctl,
274 .subscribe_event = omap3isp_stat_subscribe_event,
275 .unsubscribe_event = omap3isp_stat_unsubscribe_event,
278 static const struct v4l2_subdev_video_ops h3a_aewb_subdev_video_ops = {
279 .s_stream = omap3isp_stat_s_stream,
282 static const struct v4l2_subdev_ops h3a_aewb_subdev_ops = {
283 .core = &h3a_aewb_subdev_core_ops,
284 .video = &h3a_aewb_subdev_video_ops,
288 * omap3isp_h3a_aewb_init - Module Initialisation.
290 int omap3isp_h3a_aewb_init(struct isp_device *isp)
292 struct ispstat *aewb = &isp->isp_aewb;
293 struct omap3isp_h3a_aewb_config *aewb_cfg;
294 struct omap3isp_h3a_aewb_config *aewb_recover_cfg;
296 aewb_cfg = devm_kzalloc(isp->dev, sizeof(*aewb_cfg), GFP_KERNEL);
297 if (!aewb_cfg)
298 return -ENOMEM;
300 aewb->ops = &h3a_aewb_ops;
301 aewb->priv = aewb_cfg;
302 aewb->event_type = V4L2_EVENT_OMAP3ISP_AEWB;
303 aewb->isp = isp;
305 /* Set recover state configuration */
306 aewb_recover_cfg = devm_kzalloc(isp->dev, sizeof(*aewb_recover_cfg),
307 GFP_KERNEL);
308 if (!aewb_recover_cfg) {
309 dev_err(aewb->isp->dev,
310 "AEWB: cannot allocate memory for recover configuration.\n");
311 return -ENOMEM;
314 aewb_recover_cfg->saturation_limit = OMAP3ISP_AEWB_MAX_SATURATION_LIM;
315 aewb_recover_cfg->win_height = OMAP3ISP_AEWB_MIN_WIN_H;
316 aewb_recover_cfg->win_width = OMAP3ISP_AEWB_MIN_WIN_W;
317 aewb_recover_cfg->ver_win_count = OMAP3ISP_AEWB_MIN_WINVC;
318 aewb_recover_cfg->hor_win_count = OMAP3ISP_AEWB_MIN_WINHC;
319 aewb_recover_cfg->blk_ver_win_start = aewb_recover_cfg->ver_win_start +
320 aewb_recover_cfg->win_height * aewb_recover_cfg->ver_win_count;
321 aewb_recover_cfg->blk_win_height = OMAP3ISP_AEWB_MIN_WIN_H;
322 aewb_recover_cfg->subsample_ver_inc = OMAP3ISP_AEWB_MIN_SUB_INC;
323 aewb_recover_cfg->subsample_hor_inc = OMAP3ISP_AEWB_MIN_SUB_INC;
325 if (h3a_aewb_validate_params(aewb, aewb_recover_cfg)) {
326 dev_err(aewb->isp->dev,
327 "AEWB: recover configuration is invalid.\n");
328 return -EINVAL;
331 aewb_recover_cfg->buf_size = h3a_aewb_get_buf_size(aewb_recover_cfg);
332 aewb->recover_priv = aewb_recover_cfg;
334 return omap3isp_stat_init(aewb, "AEWB", &h3a_aewb_subdev_ops);
338 * omap3isp_h3a_aewb_cleanup - Module exit.
340 void omap3isp_h3a_aewb_cleanup(struct isp_device *isp)
342 omap3isp_stat_cleanup(&isp->isp_aewb);