4 * TI OMAP3 ISP - H3A AF 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 /* Linux specific include files */
29 #include <linux/device.h>
30 #include <linux/slab.h>
36 #define IS_OUT_OF_BOUNDS(value, min, max) \
37 (((value) < (min)) || ((value) > (max)))
39 static void h3a_af_setup_regs(struct ispstat
*af
, void *priv
)
41 struct omap3isp_h3a_af_config
*conf
= priv
;
51 if (af
->state
== ISPSTAT_DISABLED
)
54 isp_reg_writel(af
->isp
, af
->active_buf
->iommu_addr
, OMAP3_ISP_IOMEM_H3A
,
60 /* Configure Hardware Registers */
61 pax1
= ((conf
->paxel
.width
>> 1) - 1) << AF_PAXW_SHIFT
;
62 /* Set height in AFPAX1 */
63 pax1
|= (conf
->paxel
.height
>> 1) - 1;
64 isp_reg_writel(af
->isp
, pax1
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_AFPAX1
);
66 /* Configure AFPAX2 Register */
67 /* Set Line Increment in AFPAX2 Register */
68 pax2
= ((conf
->paxel
.line_inc
>> 1) - 1) << AF_LINE_INCR_SHIFT
;
69 /* Set Vertical Count */
70 pax2
|= (conf
->paxel
.v_cnt
- 1) << AF_VT_COUNT_SHIFT
;
71 /* Set Horizontal Count */
72 pax2
|= (conf
->paxel
.h_cnt
- 1);
73 isp_reg_writel(af
->isp
, pax2
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_AFPAX2
);
75 /* Configure PAXSTART Register */
76 /*Configure Horizontal Start */
77 paxstart
= conf
->paxel
.h_start
<< AF_HZ_START_SHIFT
;
78 /* Configure Vertical Start */
79 paxstart
|= conf
->paxel
.v_start
;
80 isp_reg_writel(af
->isp
, paxstart
, OMAP3_ISP_IOMEM_H3A
,
83 /*SetIIRSH Register */
84 isp_reg_writel(af
->isp
, conf
->iir
.h_start
,
85 OMAP3_ISP_IOMEM_H3A
, ISPH3A_AFIIRSH
);
87 base_coef_set0
= ISPH3A_AFCOEF010
;
88 base_coef_set1
= ISPH3A_AFCOEF110
;
89 for (index
= 0; index
<= 8; index
+= 2) {
90 /*Set IIR Filter0 Coefficients */
92 coef
|= conf
->iir
.coeff_set0
[index
];
93 coef
|= conf
->iir
.coeff_set0
[index
+ 1] <<
95 isp_reg_writel(af
->isp
, coef
, OMAP3_ISP_IOMEM_H3A
,
97 base_coef_set0
+= AFCOEF_OFFSET
;
99 /*Set IIR Filter1 Coefficients */
101 coef
|= conf
->iir
.coeff_set1
[index
];
102 coef
|= conf
->iir
.coeff_set1
[index
+ 1] <<
104 isp_reg_writel(af
->isp
, coef
, OMAP3_ISP_IOMEM_H3A
,
106 base_coef_set1
+= AFCOEF_OFFSET
;
108 /* set AFCOEF0010 Register */
109 isp_reg_writel(af
->isp
, conf
->iir
.coeff_set0
[10],
110 OMAP3_ISP_IOMEM_H3A
, ISPH3A_AFCOEF0010
);
111 /* set AFCOEF1010 Register */
112 isp_reg_writel(af
->isp
, conf
->iir
.coeff_set1
[10],
113 OMAP3_ISP_IOMEM_H3A
, ISPH3A_AFCOEF1010
);
116 /* Set RGB Position */
117 pcr
= conf
->rgb_pos
<< AF_RGBPOS_SHIFT
;
118 /* Set Accumulator Mode */
119 if (conf
->fvmode
== OMAP3ISP_AF_MODE_PEAK
)
122 if (conf
->alaw_enable
)
124 /* HMF Configurations */
125 if (conf
->hmf
.enable
) {
128 /* Set Median Threshold */
129 pcr
|= conf
->hmf
.threshold
<< AF_MED_TH_SHIFT
;
131 /* Set PCR Register */
132 isp_reg_clr_set(af
->isp
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_PCR
,
136 af
->config_counter
+= af
->inc_config
;
138 af
->buf_size
= conf
->buf_size
;
141 static void h3a_af_enable(struct ispstat
*af
, int enable
)
144 isp_reg_set(af
->isp
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_PCR
,
146 omap3isp_subclk_enable(af
->isp
, OMAP3_ISP_SUBCLK_AF
);
148 isp_reg_clr(af
->isp
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_PCR
,
150 omap3isp_subclk_disable(af
->isp
, OMAP3_ISP_SUBCLK_AF
);
154 static int h3a_af_busy(struct ispstat
*af
)
156 return isp_reg_readl(af
->isp
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_PCR
)
160 static u32
h3a_af_get_buf_size(struct omap3isp_h3a_af_config
*conf
)
162 return conf
->paxel
.h_cnt
* conf
->paxel
.v_cnt
* OMAP3ISP_AF_PAXEL_SIZE
;
165 /* Function to check paxel parameters */
166 static int h3a_af_validate_params(struct ispstat
*af
, void *new_conf
)
168 struct omap3isp_h3a_af_config
*user_cfg
= new_conf
;
169 struct omap3isp_h3a_af_paxel
*paxel_cfg
= &user_cfg
->paxel
;
170 struct omap3isp_h3a_af_iir
*iir_cfg
= &user_cfg
->iir
;
174 /* Check horizontal Count */
175 if (IS_OUT_OF_BOUNDS(paxel_cfg
->h_cnt
,
176 OMAP3ISP_AF_PAXEL_HORIZONTAL_COUNT_MIN
,
177 OMAP3ISP_AF_PAXEL_HORIZONTAL_COUNT_MAX
))
180 /* Check Vertical Count */
181 if (IS_OUT_OF_BOUNDS(paxel_cfg
->v_cnt
,
182 OMAP3ISP_AF_PAXEL_VERTICAL_COUNT_MIN
,
183 OMAP3ISP_AF_PAXEL_VERTICAL_COUNT_MAX
))
186 if (IS_OUT_OF_BOUNDS(paxel_cfg
->height
, OMAP3ISP_AF_PAXEL_HEIGHT_MIN
,
187 OMAP3ISP_AF_PAXEL_HEIGHT_MAX
) ||
188 paxel_cfg
->height
% 2)
192 if (IS_OUT_OF_BOUNDS(paxel_cfg
->width
, OMAP3ISP_AF_PAXEL_WIDTH_MIN
,
193 OMAP3ISP_AF_PAXEL_WIDTH_MAX
) ||
194 paxel_cfg
->width
% 2)
197 /* Check Line Increment */
198 if (IS_OUT_OF_BOUNDS(paxel_cfg
->line_inc
,
199 OMAP3ISP_AF_PAXEL_INCREMENT_MIN
,
200 OMAP3ISP_AF_PAXEL_INCREMENT_MAX
) ||
201 paxel_cfg
->line_inc
% 2)
204 /* Check Horizontal Start */
205 if ((paxel_cfg
->h_start
< iir_cfg
->h_start
) ||
206 IS_OUT_OF_BOUNDS(paxel_cfg
->h_start
,
207 OMAP3ISP_AF_PAXEL_HZSTART_MIN
,
208 OMAP3ISP_AF_PAXEL_HZSTART_MAX
))
212 for (index
= 0; index
< OMAP3ISP_AF_NUM_COEF
; index
++) {
213 if ((iir_cfg
->coeff_set0
[index
]) > OMAP3ISP_AF_COEF_MAX
)
216 if ((iir_cfg
->coeff_set1
[index
]) > OMAP3ISP_AF_COEF_MAX
)
220 if (IS_OUT_OF_BOUNDS(iir_cfg
->h_start
, OMAP3ISP_AF_IIRSH_MIN
,
221 OMAP3ISP_AF_IIRSH_MAX
))
224 /* Hack: If paxel size is 12, the 10th AF window may be corrupted */
225 if ((paxel_cfg
->h_cnt
* paxel_cfg
->v_cnt
> 9) &&
226 (paxel_cfg
->width
* paxel_cfg
->height
== 12))
229 buf_size
= h3a_af_get_buf_size(user_cfg
);
230 if (buf_size
> user_cfg
->buf_size
)
231 /* User buf_size request wasn't enough */
232 user_cfg
->buf_size
= buf_size
;
233 else if (user_cfg
->buf_size
> OMAP3ISP_AF_MAX_BUF_SIZE
)
234 user_cfg
->buf_size
= OMAP3ISP_AF_MAX_BUF_SIZE
;
239 /* Update local parameters */
240 static void h3a_af_set_params(struct ispstat
*af
, void *new_conf
)
242 struct omap3isp_h3a_af_config
*user_cfg
= new_conf
;
243 struct omap3isp_h3a_af_config
*cur_cfg
= af
->priv
;
248 if (cur_cfg
->alaw_enable
!= user_cfg
->alaw_enable
) {
254 if (cur_cfg
->hmf
.enable
!= user_cfg
->hmf
.enable
) {
258 if (cur_cfg
->hmf
.threshold
!= user_cfg
->hmf
.threshold
) {
264 if (cur_cfg
->rgb_pos
!= user_cfg
->rgb_pos
) {
270 if (cur_cfg
->iir
.h_start
!= user_cfg
->iir
.h_start
) {
274 for (index
= 0; index
< OMAP3ISP_AF_NUM_COEF
; index
++) {
275 if (cur_cfg
->iir
.coeff_set0
[index
] !=
276 user_cfg
->iir
.coeff_set0
[index
]) {
280 if (cur_cfg
->iir
.coeff_set1
[index
] !=
281 user_cfg
->iir
.coeff_set1
[index
]) {
288 if ((cur_cfg
->paxel
.width
!= user_cfg
->paxel
.width
) ||
289 (cur_cfg
->paxel
.height
!= user_cfg
->paxel
.height
) ||
290 (cur_cfg
->paxel
.h_start
!= user_cfg
->paxel
.h_start
) ||
291 (cur_cfg
->paxel
.v_start
!= user_cfg
->paxel
.v_start
) ||
292 (cur_cfg
->paxel
.h_cnt
!= user_cfg
->paxel
.h_cnt
) ||
293 (cur_cfg
->paxel
.v_cnt
!= user_cfg
->paxel
.v_cnt
) ||
294 (cur_cfg
->paxel
.line_inc
!= user_cfg
->paxel
.line_inc
)) {
300 if (cur_cfg
->fvmode
!= user_cfg
->fvmode
)
304 if (update
|| !af
->configured
) {
305 memcpy(cur_cfg
, user_cfg
, sizeof(*cur_cfg
));
309 * User might be asked for a bigger buffer than necessary for
310 * this configuration. In order to return the right amount of
311 * data during buffer request, let's calculate the size here
312 * instead of stick with user_cfg->buf_size.
314 cur_cfg
->buf_size
= h3a_af_get_buf_size(cur_cfg
);
318 static long h3a_af_ioctl(struct v4l2_subdev
*sd
, unsigned int cmd
, void *arg
)
320 struct ispstat
*stat
= v4l2_get_subdevdata(sd
);
323 case VIDIOC_OMAP3ISP_AF_CFG
:
324 return omap3isp_stat_config(stat
, arg
);
325 case VIDIOC_OMAP3ISP_STAT_REQ
:
326 return omap3isp_stat_request_statistics(stat
, arg
);
327 case VIDIOC_OMAP3ISP_STAT_EN
: {
329 return omap3isp_stat_enable(stat
, !!*en
);
337 static const struct ispstat_ops h3a_af_ops
= {
338 .validate_params
= h3a_af_validate_params
,
339 .set_params
= h3a_af_set_params
,
340 .setup_regs
= h3a_af_setup_regs
,
341 .enable
= h3a_af_enable
,
345 static const struct v4l2_subdev_core_ops h3a_af_subdev_core_ops
= {
346 .ioctl
= h3a_af_ioctl
,
347 .subscribe_event
= omap3isp_stat_subscribe_event
,
348 .unsubscribe_event
= omap3isp_stat_unsubscribe_event
,
351 static const struct v4l2_subdev_video_ops h3a_af_subdev_video_ops
= {
352 .s_stream
= omap3isp_stat_s_stream
,
355 static const struct v4l2_subdev_ops h3a_af_subdev_ops
= {
356 .core
= &h3a_af_subdev_core_ops
,
357 .video
= &h3a_af_subdev_video_ops
,
360 /* Function to register the AF character device driver. */
361 int omap3isp_h3a_af_init(struct isp_device
*isp
)
363 struct ispstat
*af
= &isp
->isp_af
;
364 struct omap3isp_h3a_af_config
*af_cfg
;
365 struct omap3isp_h3a_af_config
*af_recover_cfg
;
367 af_cfg
= devm_kzalloc(isp
->dev
, sizeof(*af_cfg
), GFP_KERNEL
);
371 af
->ops
= &h3a_af_ops
;
374 af
->event_type
= V4L2_EVENT_OMAP3ISP_AF
;
377 /* Set recover state configuration */
378 af_recover_cfg
= devm_kzalloc(isp
->dev
, sizeof(*af_recover_cfg
),
380 if (!af_recover_cfg
) {
381 dev_err(af
->isp
->dev
, "AF: cannot allocate memory for recover "
386 af_recover_cfg
->paxel
.h_start
= OMAP3ISP_AF_PAXEL_HZSTART_MIN
;
387 af_recover_cfg
->paxel
.width
= OMAP3ISP_AF_PAXEL_WIDTH_MIN
;
388 af_recover_cfg
->paxel
.height
= OMAP3ISP_AF_PAXEL_HEIGHT_MIN
;
389 af_recover_cfg
->paxel
.h_cnt
= OMAP3ISP_AF_PAXEL_HORIZONTAL_COUNT_MIN
;
390 af_recover_cfg
->paxel
.v_cnt
= OMAP3ISP_AF_PAXEL_VERTICAL_COUNT_MIN
;
391 af_recover_cfg
->paxel
.line_inc
= OMAP3ISP_AF_PAXEL_INCREMENT_MIN
;
392 if (h3a_af_validate_params(af
, af_recover_cfg
)) {
393 dev_err(af
->isp
->dev
, "AF: recover configuration is "
398 af_recover_cfg
->buf_size
= h3a_af_get_buf_size(af_recover_cfg
);
399 af
->recover_priv
= af_recover_cfg
;
401 return omap3isp_stat_init(af
, "AF", &h3a_af_subdev_ops
);
404 void omap3isp_h3a_af_cleanup(struct isp_device
*isp
)
406 omap3isp_stat_cleanup(&isp
->isp_af
);