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.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 #include <linux/slab.h>
29 #include <linux/uaccess.h>
36 * h3a_aewb_update_regs - Helper function to update h3a registers.
38 static void h3a_aewb_setup_regs(struct ispstat
*aewb
, void *priv
)
40 struct omap3isp_h3a_aewb_config
*conf
= priv
;
47 if (aewb
->state
== ISPSTAT_DISABLED
)
50 isp_reg_writel(aewb
->isp
, aewb
->active_buf
->iommu_addr
,
51 OMAP3_ISP_IOMEM_H3A
, ISPH3A_AEWBUFST
);
56 /* Converting config metadata into reg values */
57 pcr
= conf
->saturation_limit
<< ISPH3A_PCR_AEW_AVE2LMT_SHIFT
;
58 pcr
|= !!conf
->alaw_enable
<< ISPH3A_PCR_AEW_ALAW_EN_SHIFT
;
60 win1
= ((conf
->win_height
>> 1) - 1) << ISPH3A_AEWWIN1_WINH_SHIFT
;
61 win1
|= ((conf
->win_width
>> 1) - 1) << ISPH3A_AEWWIN1_WINW_SHIFT
;
62 win1
|= (conf
->ver_win_count
- 1) << ISPH3A_AEWWIN1_WINVC_SHIFT
;
63 win1
|= (conf
->hor_win_count
- 1) << ISPH3A_AEWWIN1_WINHC_SHIFT
;
65 start
= conf
->hor_win_start
<< ISPH3A_AEWINSTART_WINSH_SHIFT
;
66 start
|= conf
->ver_win_start
<< ISPH3A_AEWINSTART_WINSV_SHIFT
;
68 blk
= conf
->blk_ver_win_start
<< ISPH3A_AEWINBLK_WINSV_SHIFT
;
69 blk
|= ((conf
->blk_win_height
>> 1) - 1) << ISPH3A_AEWINBLK_WINH_SHIFT
;
71 subwin
= ((conf
->subsample_ver_inc
>> 1) - 1) <<
72 ISPH3A_AEWSUBWIN_AEWINCV_SHIFT
;
73 subwin
|= ((conf
->subsample_hor_inc
>> 1) - 1) <<
74 ISPH3A_AEWSUBWIN_AEWINCH_SHIFT
;
76 isp_reg_writel(aewb
->isp
, win1
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_AEWWIN1
);
77 isp_reg_writel(aewb
->isp
, start
, OMAP3_ISP_IOMEM_H3A
,
79 isp_reg_writel(aewb
->isp
, blk
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_AEWINBLK
);
80 isp_reg_writel(aewb
->isp
, subwin
, OMAP3_ISP_IOMEM_H3A
,
82 isp_reg_clr_set(aewb
->isp
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_PCR
,
83 ISPH3A_PCR_AEW_MASK
, pcr
);
86 aewb
->config_counter
+= aewb
->inc_config
;
88 aewb
->buf_size
= conf
->buf_size
;
91 static void h3a_aewb_enable(struct ispstat
*aewb
, int enable
)
94 isp_reg_set(aewb
->isp
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_PCR
,
96 omap3isp_subclk_enable(aewb
->isp
, OMAP3_ISP_SUBCLK_AEWB
);
98 isp_reg_clr(aewb
->isp
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_PCR
,
100 omap3isp_subclk_disable(aewb
->isp
, OMAP3_ISP_SUBCLK_AEWB
);
104 static int h3a_aewb_busy(struct ispstat
*aewb
)
106 return isp_reg_readl(aewb
->isp
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_PCR
)
107 & ISPH3A_PCR_BUSYAEAWB
;
110 static u32
h3a_aewb_get_buf_size(struct omap3isp_h3a_aewb_config
*conf
)
112 /* Number of configured windows + extra row for black data */
113 u32 win_count
= (conf
->ver_win_count
+ 1) * conf
->hor_win_count
;
116 * Unsaturated block counts for each 8 windows.
117 * 1 extra for the last (win_count % 8) windows if win_count is not
120 win_count
+= (win_count
+ 7) / 8;
122 return win_count
* AEWB_PACKET_SIZE
;
125 static int h3a_aewb_validate_params(struct ispstat
*aewb
, void *new_conf
)
127 struct omap3isp_h3a_aewb_config
*user_cfg
= new_conf
;
130 if (unlikely(user_cfg
->saturation_limit
>
131 OMAP3ISP_AEWB_MAX_SATURATION_LIM
))
134 if (unlikely(user_cfg
->win_height
< OMAP3ISP_AEWB_MIN_WIN_H
||
135 user_cfg
->win_height
> OMAP3ISP_AEWB_MAX_WIN_H
||
136 user_cfg
->win_height
& 0x01))
139 if (unlikely(user_cfg
->win_width
< OMAP3ISP_AEWB_MIN_WIN_W
||
140 user_cfg
->win_width
> OMAP3ISP_AEWB_MAX_WIN_W
||
141 user_cfg
->win_width
& 0x01))
144 if (unlikely(user_cfg
->ver_win_count
< OMAP3ISP_AEWB_MIN_WINVC
||
145 user_cfg
->ver_win_count
> OMAP3ISP_AEWB_MAX_WINVC
))
148 if (unlikely(user_cfg
->hor_win_count
< OMAP3ISP_AEWB_MIN_WINHC
||
149 user_cfg
->hor_win_count
> OMAP3ISP_AEWB_MAX_WINHC
))
152 if (unlikely(user_cfg
->ver_win_start
> OMAP3ISP_AEWB_MAX_WINSTART
))
155 if (unlikely(user_cfg
->hor_win_start
> OMAP3ISP_AEWB_MAX_WINSTART
))
158 if (unlikely(user_cfg
->blk_ver_win_start
> OMAP3ISP_AEWB_MAX_WINSTART
))
161 if (unlikely(user_cfg
->blk_win_height
< OMAP3ISP_AEWB_MIN_WIN_H
||
162 user_cfg
->blk_win_height
> OMAP3ISP_AEWB_MAX_WIN_H
||
163 user_cfg
->blk_win_height
& 0x01))
166 if (unlikely(user_cfg
->subsample_ver_inc
< OMAP3ISP_AEWB_MIN_SUB_INC
||
167 user_cfg
->subsample_ver_inc
> OMAP3ISP_AEWB_MAX_SUB_INC
||
168 user_cfg
->subsample_ver_inc
& 0x01))
171 if (unlikely(user_cfg
->subsample_hor_inc
< OMAP3ISP_AEWB_MIN_SUB_INC
||
172 user_cfg
->subsample_hor_inc
> OMAP3ISP_AEWB_MAX_SUB_INC
||
173 user_cfg
->subsample_hor_inc
& 0x01))
176 buf_size
= h3a_aewb_get_buf_size(user_cfg
);
177 if (buf_size
> user_cfg
->buf_size
)
178 user_cfg
->buf_size
= buf_size
;
179 else if (user_cfg
->buf_size
> OMAP3ISP_AEWB_MAX_BUF_SIZE
)
180 user_cfg
->buf_size
= OMAP3ISP_AEWB_MAX_BUF_SIZE
;
186 * h3a_aewb_set_params - Helper function to check & store user given params.
187 * @new_conf: Pointer to AE and AWB parameters struct.
189 * As most of them are busy-lock registers, need to wait until AEW_BUSY = 0 to
190 * program them during ISR.
192 static void h3a_aewb_set_params(struct ispstat
*aewb
, void *new_conf
)
194 struct omap3isp_h3a_aewb_config
*user_cfg
= new_conf
;
195 struct omap3isp_h3a_aewb_config
*cur_cfg
= aewb
->priv
;
198 if (cur_cfg
->saturation_limit
!= user_cfg
->saturation_limit
) {
199 cur_cfg
->saturation_limit
= user_cfg
->saturation_limit
;
202 if (cur_cfg
->alaw_enable
!= user_cfg
->alaw_enable
) {
203 cur_cfg
->alaw_enable
= user_cfg
->alaw_enable
;
206 if (cur_cfg
->win_height
!= user_cfg
->win_height
) {
207 cur_cfg
->win_height
= user_cfg
->win_height
;
210 if (cur_cfg
->win_width
!= user_cfg
->win_width
) {
211 cur_cfg
->win_width
= user_cfg
->win_width
;
214 if (cur_cfg
->ver_win_count
!= user_cfg
->ver_win_count
) {
215 cur_cfg
->ver_win_count
= user_cfg
->ver_win_count
;
218 if (cur_cfg
->hor_win_count
!= user_cfg
->hor_win_count
) {
219 cur_cfg
->hor_win_count
= user_cfg
->hor_win_count
;
222 if (cur_cfg
->ver_win_start
!= user_cfg
->ver_win_start
) {
223 cur_cfg
->ver_win_start
= user_cfg
->ver_win_start
;
226 if (cur_cfg
->hor_win_start
!= user_cfg
->hor_win_start
) {
227 cur_cfg
->hor_win_start
= user_cfg
->hor_win_start
;
230 if (cur_cfg
->blk_ver_win_start
!= user_cfg
->blk_ver_win_start
) {
231 cur_cfg
->blk_ver_win_start
= user_cfg
->blk_ver_win_start
;
234 if (cur_cfg
->blk_win_height
!= user_cfg
->blk_win_height
) {
235 cur_cfg
->blk_win_height
= user_cfg
->blk_win_height
;
238 if (cur_cfg
->subsample_ver_inc
!= user_cfg
->subsample_ver_inc
) {
239 cur_cfg
->subsample_ver_inc
= user_cfg
->subsample_ver_inc
;
242 if (cur_cfg
->subsample_hor_inc
!= user_cfg
->subsample_hor_inc
) {
243 cur_cfg
->subsample_hor_inc
= user_cfg
->subsample_hor_inc
;
247 if (update
|| !aewb
->configured
) {
250 cur_cfg
->buf_size
= h3a_aewb_get_buf_size(cur_cfg
);
254 static long h3a_aewb_ioctl(struct v4l2_subdev
*sd
, unsigned int cmd
, void *arg
)
256 struct ispstat
*stat
= v4l2_get_subdevdata(sd
);
259 case VIDIOC_OMAP3ISP_AEWB_CFG
:
260 return omap3isp_stat_config(stat
, arg
);
261 case VIDIOC_OMAP3ISP_STAT_REQ
:
262 return omap3isp_stat_request_statistics(stat
, arg
);
263 case VIDIOC_OMAP3ISP_STAT_EN
: {
264 unsigned long *en
= arg
;
265 return omap3isp_stat_enable(stat
, !!*en
);
272 static const struct ispstat_ops h3a_aewb_ops
= {
273 .validate_params
= h3a_aewb_validate_params
,
274 .set_params
= h3a_aewb_set_params
,
275 .setup_regs
= h3a_aewb_setup_regs
,
276 .enable
= h3a_aewb_enable
,
277 .busy
= h3a_aewb_busy
,
280 static const struct v4l2_subdev_core_ops h3a_aewb_subdev_core_ops
= {
281 .ioctl
= h3a_aewb_ioctl
,
282 .subscribe_event
= omap3isp_stat_subscribe_event
,
283 .unsubscribe_event
= omap3isp_stat_unsubscribe_event
,
286 static const struct v4l2_subdev_video_ops h3a_aewb_subdev_video_ops
= {
287 .s_stream
= omap3isp_stat_s_stream
,
290 static const struct v4l2_subdev_ops h3a_aewb_subdev_ops
= {
291 .core
= &h3a_aewb_subdev_core_ops
,
292 .video
= &h3a_aewb_subdev_video_ops
,
296 * omap3isp_h3a_aewb_init - Module Initialisation.
298 int omap3isp_h3a_aewb_init(struct isp_device
*isp
)
300 struct ispstat
*aewb
= &isp
->isp_aewb
;
301 struct omap3isp_h3a_aewb_config
*aewb_cfg
;
302 struct omap3isp_h3a_aewb_config
*aewb_recover_cfg
;
304 aewb_cfg
= devm_kzalloc(isp
->dev
, sizeof(*aewb_cfg
), GFP_KERNEL
);
308 aewb
->ops
= &h3a_aewb_ops
;
309 aewb
->priv
= aewb_cfg
;
311 aewb
->event_type
= V4L2_EVENT_OMAP3ISP_AEWB
;
314 /* Set recover state configuration */
315 aewb_recover_cfg
= devm_kzalloc(isp
->dev
, sizeof(*aewb_recover_cfg
),
317 if (!aewb_recover_cfg
) {
318 dev_err(aewb
->isp
->dev
, "AEWB: cannot allocate memory for "
319 "recover configuration.\n");
323 aewb_recover_cfg
->saturation_limit
= OMAP3ISP_AEWB_MAX_SATURATION_LIM
;
324 aewb_recover_cfg
->win_height
= OMAP3ISP_AEWB_MIN_WIN_H
;
325 aewb_recover_cfg
->win_width
= OMAP3ISP_AEWB_MIN_WIN_W
;
326 aewb_recover_cfg
->ver_win_count
= OMAP3ISP_AEWB_MIN_WINVC
;
327 aewb_recover_cfg
->hor_win_count
= OMAP3ISP_AEWB_MIN_WINHC
;
328 aewb_recover_cfg
->blk_ver_win_start
= aewb_recover_cfg
->ver_win_start
+
329 aewb_recover_cfg
->win_height
* aewb_recover_cfg
->ver_win_count
;
330 aewb_recover_cfg
->blk_win_height
= OMAP3ISP_AEWB_MIN_WIN_H
;
331 aewb_recover_cfg
->subsample_ver_inc
= OMAP3ISP_AEWB_MIN_SUB_INC
;
332 aewb_recover_cfg
->subsample_hor_inc
= OMAP3ISP_AEWB_MIN_SUB_INC
;
334 if (h3a_aewb_validate_params(aewb
, aewb_recover_cfg
)) {
335 dev_err(aewb
->isp
->dev
, "AEWB: recover configuration is "
340 aewb_recover_cfg
->buf_size
= h3a_aewb_get_buf_size(aewb_recover_cfg
);
341 aewb
->recover_priv
= aewb_recover_cfg
;
343 return omap3isp_stat_init(aewb
, "AEWB", &h3a_aewb_subdev_ops
);
347 * omap3isp_h3a_aewb_cleanup - Module exit.
349 void omap3isp_h3a_aewb_cleanup(struct isp_device
*isp
)
351 omap3isp_stat_cleanup(&isp
->isp_aewb
);