Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / platform / omap3isp / isph3a_aewb.c
blobd44626f20ac616a345cc1f423771bb8b8d239f48
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_EN: {
254 unsigned long *en = arg;
255 return omap3isp_stat_enable(stat, !!*en);
259 return -ENOIOCTLCMD;
262 static const struct ispstat_ops h3a_aewb_ops = {
263 .validate_params = h3a_aewb_validate_params,
264 .set_params = h3a_aewb_set_params,
265 .setup_regs = h3a_aewb_setup_regs,
266 .enable = h3a_aewb_enable,
267 .busy = h3a_aewb_busy,
270 static const struct v4l2_subdev_core_ops h3a_aewb_subdev_core_ops = {
271 .ioctl = h3a_aewb_ioctl,
272 .subscribe_event = omap3isp_stat_subscribe_event,
273 .unsubscribe_event = omap3isp_stat_unsubscribe_event,
276 static const struct v4l2_subdev_video_ops h3a_aewb_subdev_video_ops = {
277 .s_stream = omap3isp_stat_s_stream,
280 static const struct v4l2_subdev_ops h3a_aewb_subdev_ops = {
281 .core = &h3a_aewb_subdev_core_ops,
282 .video = &h3a_aewb_subdev_video_ops,
286 * omap3isp_h3a_aewb_init - Module Initialisation.
288 int omap3isp_h3a_aewb_init(struct isp_device *isp)
290 struct ispstat *aewb = &isp->isp_aewb;
291 struct omap3isp_h3a_aewb_config *aewb_cfg;
292 struct omap3isp_h3a_aewb_config *aewb_recover_cfg;
294 aewb_cfg = devm_kzalloc(isp->dev, sizeof(*aewb_cfg), GFP_KERNEL);
295 if (!aewb_cfg)
296 return -ENOMEM;
298 aewb->ops = &h3a_aewb_ops;
299 aewb->priv = aewb_cfg;
300 aewb->event_type = V4L2_EVENT_OMAP3ISP_AEWB;
301 aewb->isp = isp;
303 /* Set recover state configuration */
304 aewb_recover_cfg = devm_kzalloc(isp->dev, sizeof(*aewb_recover_cfg),
305 GFP_KERNEL);
306 if (!aewb_recover_cfg) {
307 dev_err(aewb->isp->dev,
308 "AEWB: cannot allocate memory for recover configuration.\n");
309 return -ENOMEM;
312 aewb_recover_cfg->saturation_limit = OMAP3ISP_AEWB_MAX_SATURATION_LIM;
313 aewb_recover_cfg->win_height = OMAP3ISP_AEWB_MIN_WIN_H;
314 aewb_recover_cfg->win_width = OMAP3ISP_AEWB_MIN_WIN_W;
315 aewb_recover_cfg->ver_win_count = OMAP3ISP_AEWB_MIN_WINVC;
316 aewb_recover_cfg->hor_win_count = OMAP3ISP_AEWB_MIN_WINHC;
317 aewb_recover_cfg->blk_ver_win_start = aewb_recover_cfg->ver_win_start +
318 aewb_recover_cfg->win_height * aewb_recover_cfg->ver_win_count;
319 aewb_recover_cfg->blk_win_height = OMAP3ISP_AEWB_MIN_WIN_H;
320 aewb_recover_cfg->subsample_ver_inc = OMAP3ISP_AEWB_MIN_SUB_INC;
321 aewb_recover_cfg->subsample_hor_inc = OMAP3ISP_AEWB_MIN_SUB_INC;
323 if (h3a_aewb_validate_params(aewb, aewb_recover_cfg)) {
324 dev_err(aewb->isp->dev,
325 "AEWB: recover configuration is invalid.\n");
326 return -EINVAL;
329 aewb_recover_cfg->buf_size = h3a_aewb_get_buf_size(aewb_recover_cfg);
330 aewb->recover_priv = aewb_recover_cfg;
332 return omap3isp_stat_init(aewb, "AEWB", &h3a_aewb_subdev_ops);
336 * omap3isp_h3a_aewb_cleanup - Module exit.
338 void omap3isp_h3a_aewb_cleanup(struct isp_device *isp)
340 omap3isp_stat_cleanup(&isp->isp_aewb);