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.
18 /* Linux specific include files */
19 #include <linux/device.h>
20 #include <linux/slab.h>
26 #define IS_OUT_OF_BOUNDS(value, min, max) \
27 (((value) < (min)) || ((value) > (max)))
29 static void h3a_af_setup_regs(struct ispstat
*af
, void *priv
)
31 struct omap3isp_h3a_af_config
*conf
= priv
;
41 if (af
->state
== ISPSTAT_DISABLED
)
44 isp_reg_writel(af
->isp
, af
->active_buf
->dma_addr
, OMAP3_ISP_IOMEM_H3A
,
50 /* Configure Hardware Registers */
51 pax1
= ((conf
->paxel
.width
>> 1) - 1) << AF_PAXW_SHIFT
;
52 /* Set height in AFPAX1 */
53 pax1
|= (conf
->paxel
.height
>> 1) - 1;
54 isp_reg_writel(af
->isp
, pax1
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_AFPAX1
);
56 /* Configure AFPAX2 Register */
57 /* Set Line Increment in AFPAX2 Register */
58 pax2
= ((conf
->paxel
.line_inc
>> 1) - 1) << AF_LINE_INCR_SHIFT
;
59 /* Set Vertical Count */
60 pax2
|= (conf
->paxel
.v_cnt
- 1) << AF_VT_COUNT_SHIFT
;
61 /* Set Horizontal Count */
62 pax2
|= (conf
->paxel
.h_cnt
- 1);
63 isp_reg_writel(af
->isp
, pax2
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_AFPAX2
);
65 /* Configure PAXSTART Register */
66 /*Configure Horizontal Start */
67 paxstart
= conf
->paxel
.h_start
<< AF_HZ_START_SHIFT
;
68 /* Configure Vertical Start */
69 paxstart
|= conf
->paxel
.v_start
;
70 isp_reg_writel(af
->isp
, paxstart
, OMAP3_ISP_IOMEM_H3A
,
73 /*SetIIRSH Register */
74 isp_reg_writel(af
->isp
, conf
->iir
.h_start
,
75 OMAP3_ISP_IOMEM_H3A
, ISPH3A_AFIIRSH
);
77 base_coef_set0
= ISPH3A_AFCOEF010
;
78 base_coef_set1
= ISPH3A_AFCOEF110
;
79 for (index
= 0; index
<= 8; index
+= 2) {
80 /*Set IIR Filter0 Coefficients */
82 coef
|= conf
->iir
.coeff_set0
[index
];
83 coef
|= conf
->iir
.coeff_set0
[index
+ 1] <<
85 isp_reg_writel(af
->isp
, coef
, OMAP3_ISP_IOMEM_H3A
,
87 base_coef_set0
+= AFCOEF_OFFSET
;
89 /*Set IIR Filter1 Coefficients */
91 coef
|= conf
->iir
.coeff_set1
[index
];
92 coef
|= conf
->iir
.coeff_set1
[index
+ 1] <<
94 isp_reg_writel(af
->isp
, coef
, OMAP3_ISP_IOMEM_H3A
,
96 base_coef_set1
+= AFCOEF_OFFSET
;
98 /* set AFCOEF0010 Register */
99 isp_reg_writel(af
->isp
, conf
->iir
.coeff_set0
[10],
100 OMAP3_ISP_IOMEM_H3A
, ISPH3A_AFCOEF0010
);
101 /* set AFCOEF1010 Register */
102 isp_reg_writel(af
->isp
, conf
->iir
.coeff_set1
[10],
103 OMAP3_ISP_IOMEM_H3A
, ISPH3A_AFCOEF1010
);
106 /* Set RGB Position */
107 pcr
= conf
->rgb_pos
<< AF_RGBPOS_SHIFT
;
108 /* Set Accumulator Mode */
109 if (conf
->fvmode
== OMAP3ISP_AF_MODE_PEAK
)
112 if (conf
->alaw_enable
)
114 /* HMF Configurations */
115 if (conf
->hmf
.enable
) {
118 /* Set Median Threshold */
119 pcr
|= conf
->hmf
.threshold
<< AF_MED_TH_SHIFT
;
121 /* Set PCR Register */
122 isp_reg_clr_set(af
->isp
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_PCR
,
126 af
->config_counter
+= af
->inc_config
;
128 af
->buf_size
= conf
->buf_size
;
131 static void h3a_af_enable(struct ispstat
*af
, int enable
)
134 isp_reg_set(af
->isp
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_PCR
,
136 omap3isp_subclk_enable(af
->isp
, OMAP3_ISP_SUBCLK_AF
);
138 isp_reg_clr(af
->isp
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_PCR
,
140 omap3isp_subclk_disable(af
->isp
, OMAP3_ISP_SUBCLK_AF
);
144 static int h3a_af_busy(struct ispstat
*af
)
146 return isp_reg_readl(af
->isp
, OMAP3_ISP_IOMEM_H3A
, ISPH3A_PCR
)
150 static u32
h3a_af_get_buf_size(struct omap3isp_h3a_af_config
*conf
)
152 return conf
->paxel
.h_cnt
* conf
->paxel
.v_cnt
* OMAP3ISP_AF_PAXEL_SIZE
;
155 /* Function to check paxel parameters */
156 static int h3a_af_validate_params(struct ispstat
*af
, void *new_conf
)
158 struct omap3isp_h3a_af_config
*user_cfg
= new_conf
;
159 struct omap3isp_h3a_af_paxel
*paxel_cfg
= &user_cfg
->paxel
;
160 struct omap3isp_h3a_af_iir
*iir_cfg
= &user_cfg
->iir
;
164 /* Check horizontal Count */
165 if (IS_OUT_OF_BOUNDS(paxel_cfg
->h_cnt
,
166 OMAP3ISP_AF_PAXEL_HORIZONTAL_COUNT_MIN
,
167 OMAP3ISP_AF_PAXEL_HORIZONTAL_COUNT_MAX
))
170 /* Check Vertical Count */
171 if (IS_OUT_OF_BOUNDS(paxel_cfg
->v_cnt
,
172 OMAP3ISP_AF_PAXEL_VERTICAL_COUNT_MIN
,
173 OMAP3ISP_AF_PAXEL_VERTICAL_COUNT_MAX
))
176 if (IS_OUT_OF_BOUNDS(paxel_cfg
->height
, OMAP3ISP_AF_PAXEL_HEIGHT_MIN
,
177 OMAP3ISP_AF_PAXEL_HEIGHT_MAX
) ||
178 paxel_cfg
->height
% 2)
182 if (IS_OUT_OF_BOUNDS(paxel_cfg
->width
, OMAP3ISP_AF_PAXEL_WIDTH_MIN
,
183 OMAP3ISP_AF_PAXEL_WIDTH_MAX
) ||
184 paxel_cfg
->width
% 2)
187 /* Check Line Increment */
188 if (IS_OUT_OF_BOUNDS(paxel_cfg
->line_inc
,
189 OMAP3ISP_AF_PAXEL_INCREMENT_MIN
,
190 OMAP3ISP_AF_PAXEL_INCREMENT_MAX
) ||
191 paxel_cfg
->line_inc
% 2)
194 /* Check Horizontal Start */
195 if ((paxel_cfg
->h_start
< iir_cfg
->h_start
) ||
196 IS_OUT_OF_BOUNDS(paxel_cfg
->h_start
,
197 OMAP3ISP_AF_PAXEL_HZSTART_MIN
,
198 OMAP3ISP_AF_PAXEL_HZSTART_MAX
))
202 for (index
= 0; index
< OMAP3ISP_AF_NUM_COEF
; index
++) {
203 if ((iir_cfg
->coeff_set0
[index
]) > OMAP3ISP_AF_COEF_MAX
)
206 if ((iir_cfg
->coeff_set1
[index
]) > OMAP3ISP_AF_COEF_MAX
)
210 if (IS_OUT_OF_BOUNDS(iir_cfg
->h_start
, OMAP3ISP_AF_IIRSH_MIN
,
211 OMAP3ISP_AF_IIRSH_MAX
))
214 /* Hack: If paxel size is 12, the 10th AF window may be corrupted */
215 if ((paxel_cfg
->h_cnt
* paxel_cfg
->v_cnt
> 9) &&
216 (paxel_cfg
->width
* paxel_cfg
->height
== 12))
219 buf_size
= h3a_af_get_buf_size(user_cfg
);
220 if (buf_size
> user_cfg
->buf_size
)
221 /* User buf_size request wasn't enough */
222 user_cfg
->buf_size
= buf_size
;
223 else if (user_cfg
->buf_size
> OMAP3ISP_AF_MAX_BUF_SIZE
)
224 user_cfg
->buf_size
= OMAP3ISP_AF_MAX_BUF_SIZE
;
229 /* Update local parameters */
230 static void h3a_af_set_params(struct ispstat
*af
, void *new_conf
)
232 struct omap3isp_h3a_af_config
*user_cfg
= new_conf
;
233 struct omap3isp_h3a_af_config
*cur_cfg
= af
->priv
;
238 if (cur_cfg
->alaw_enable
!= user_cfg
->alaw_enable
) {
244 if (cur_cfg
->hmf
.enable
!= user_cfg
->hmf
.enable
) {
248 if (cur_cfg
->hmf
.threshold
!= user_cfg
->hmf
.threshold
) {
254 if (cur_cfg
->rgb_pos
!= user_cfg
->rgb_pos
) {
260 if (cur_cfg
->iir
.h_start
!= user_cfg
->iir
.h_start
) {
264 for (index
= 0; index
< OMAP3ISP_AF_NUM_COEF
; index
++) {
265 if (cur_cfg
->iir
.coeff_set0
[index
] !=
266 user_cfg
->iir
.coeff_set0
[index
]) {
270 if (cur_cfg
->iir
.coeff_set1
[index
] !=
271 user_cfg
->iir
.coeff_set1
[index
]) {
278 if ((cur_cfg
->paxel
.width
!= user_cfg
->paxel
.width
) ||
279 (cur_cfg
->paxel
.height
!= user_cfg
->paxel
.height
) ||
280 (cur_cfg
->paxel
.h_start
!= user_cfg
->paxel
.h_start
) ||
281 (cur_cfg
->paxel
.v_start
!= user_cfg
->paxel
.v_start
) ||
282 (cur_cfg
->paxel
.h_cnt
!= user_cfg
->paxel
.h_cnt
) ||
283 (cur_cfg
->paxel
.v_cnt
!= user_cfg
->paxel
.v_cnt
) ||
284 (cur_cfg
->paxel
.line_inc
!= user_cfg
->paxel
.line_inc
)) {
290 if (cur_cfg
->fvmode
!= user_cfg
->fvmode
)
294 if (update
|| !af
->configured
) {
295 memcpy(cur_cfg
, user_cfg
, sizeof(*cur_cfg
));
299 * User might be asked for a bigger buffer than necessary for
300 * this configuration. In order to return the right amount of
301 * data during buffer request, let's calculate the size here
302 * instead of stick with user_cfg->buf_size.
304 cur_cfg
->buf_size
= h3a_af_get_buf_size(cur_cfg
);
308 static long h3a_af_ioctl(struct v4l2_subdev
*sd
, unsigned int cmd
, void *arg
)
310 struct ispstat
*stat
= v4l2_get_subdevdata(sd
);
313 case VIDIOC_OMAP3ISP_AF_CFG
:
314 return omap3isp_stat_config(stat
, arg
);
315 case VIDIOC_OMAP3ISP_STAT_REQ
:
316 return omap3isp_stat_request_statistics(stat
, arg
);
317 case VIDIOC_OMAP3ISP_STAT_REQ_TIME32
:
318 return omap3isp_stat_request_statistics_time32(stat
, arg
);
319 case VIDIOC_OMAP3ISP_STAT_EN
: {
321 return omap3isp_stat_enable(stat
, !!*en
);
329 static const struct ispstat_ops h3a_af_ops
= {
330 .validate_params
= h3a_af_validate_params
,
331 .set_params
= h3a_af_set_params
,
332 .setup_regs
= h3a_af_setup_regs
,
333 .enable
= h3a_af_enable
,
337 static const struct v4l2_subdev_core_ops h3a_af_subdev_core_ops
= {
338 .ioctl
= h3a_af_ioctl
,
339 .subscribe_event
= omap3isp_stat_subscribe_event
,
340 .unsubscribe_event
= omap3isp_stat_unsubscribe_event
,
343 static const struct v4l2_subdev_video_ops h3a_af_subdev_video_ops
= {
344 .s_stream
= omap3isp_stat_s_stream
,
347 static const struct v4l2_subdev_ops h3a_af_subdev_ops
= {
348 .core
= &h3a_af_subdev_core_ops
,
349 .video
= &h3a_af_subdev_video_ops
,
352 /* Function to register the AF character device driver. */
353 int omap3isp_h3a_af_init(struct isp_device
*isp
)
355 struct ispstat
*af
= &isp
->isp_af
;
356 struct omap3isp_h3a_af_config
*af_cfg
;
357 struct omap3isp_h3a_af_config
*af_recover_cfg
;
359 af_cfg
= devm_kzalloc(isp
->dev
, sizeof(*af_cfg
), GFP_KERNEL
);
363 af
->ops
= &h3a_af_ops
;
365 af
->event_type
= V4L2_EVENT_OMAP3ISP_AF
;
368 /* Set recover state configuration */
369 af_recover_cfg
= devm_kzalloc(isp
->dev
, sizeof(*af_recover_cfg
),
371 if (!af_recover_cfg
) {
372 dev_err(af
->isp
->dev
,
373 "AF: cannot allocate memory for recover configuration.\n");
377 af_recover_cfg
->paxel
.h_start
= OMAP3ISP_AF_PAXEL_HZSTART_MIN
;
378 af_recover_cfg
->paxel
.width
= OMAP3ISP_AF_PAXEL_WIDTH_MIN
;
379 af_recover_cfg
->paxel
.height
= OMAP3ISP_AF_PAXEL_HEIGHT_MIN
;
380 af_recover_cfg
->paxel
.h_cnt
= OMAP3ISP_AF_PAXEL_HORIZONTAL_COUNT_MIN
;
381 af_recover_cfg
->paxel
.v_cnt
= OMAP3ISP_AF_PAXEL_VERTICAL_COUNT_MIN
;
382 af_recover_cfg
->paxel
.line_inc
= OMAP3ISP_AF_PAXEL_INCREMENT_MIN
;
383 if (h3a_af_validate_params(af
, af_recover_cfg
)) {
384 dev_err(af
->isp
->dev
,
385 "AF: recover configuration is invalid.\n");
389 af_recover_cfg
->buf_size
= h3a_af_get_buf_size(af_recover_cfg
);
390 af
->recover_priv
= af_recover_cfg
;
392 return omap3isp_stat_init(af
, "AF", &h3a_af_subdev_ops
);
395 void omap3isp_h3a_af_cleanup(struct isp_device
*isp
)
397 omap3isp_stat_cleanup(&isp
->isp_af
);