isph3a_af.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * isph3a_af.c
  3. *
  4. * TI OMAP3 ISP - H3A AF module
  5. *
  6. * Copyright (C) 2010 Nokia Corporation
  7. * Copyright (C) 2009 Texas Instruments, Inc.
  8. *
  9. * Contacts: David Cohen <dacohen@gmail.com>
  10. * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  11. * Sakari Ailus <sakari.ailus@iki.fi>
  12. *
  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.
  16. *
  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.
  21. *
  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
  25. * 02110-1301 USA
  26. */
  27. /* Linux specific include files */
  28. #include <linux/device.h>
  29. #include <linux/slab.h>
  30. #include "isp.h"
  31. #include "isph3a.h"
  32. #include "ispstat.h"
  33. #define IS_OUT_OF_BOUNDS(value, min, max) \
  34. (((value) < (min)) || ((value) > (max)))
  35. static void h3a_af_setup_regs(struct ispstat *af, void *priv)
  36. {
  37. struct omap3isp_h3a_af_config *conf = priv;
  38. u32 pcr;
  39. u32 pax1;
  40. u32 pax2;
  41. u32 paxstart;
  42. u32 coef;
  43. u32 base_coef_set0;
  44. u32 base_coef_set1;
  45. int index;
  46. if (af->state == ISPSTAT_DISABLED)
  47. return;
  48. isp_reg_writel(af->isp, af->active_buf->iommu_addr, OMAP3_ISP_IOMEM_H3A,
  49. ISPH3A_AFBUFST);
  50. if (!af->update)
  51. return;
  52. /* Configure Hardware Registers */
  53. pax1 = ((conf->paxel.width >> 1) - 1) << AF_PAXW_SHIFT;
  54. /* Set height in AFPAX1 */
  55. pax1 |= (conf->paxel.height >> 1) - 1;
  56. isp_reg_writel(af->isp, pax1, OMAP3_ISP_IOMEM_H3A, ISPH3A_AFPAX1);
  57. /* Configure AFPAX2 Register */
  58. /* Set Line Increment in AFPAX2 Register */
  59. pax2 = ((conf->paxel.line_inc >> 1) - 1) << AF_LINE_INCR_SHIFT;
  60. /* Set Vertical Count */
  61. pax2 |= (conf->paxel.v_cnt - 1) << AF_VT_COUNT_SHIFT;
  62. /* Set Horizontal Count */
  63. pax2 |= (conf->paxel.h_cnt - 1);
  64. 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,
  71. ISPH3A_AFPAXSTART);
  72. /*SetIIRSH Register */
  73. isp_reg_writel(af->isp, conf->iir.h_start,
  74. OMAP3_ISP_IOMEM_H3A, ISPH3A_AFIIRSH);
  75. base_coef_set0 = ISPH3A_AFCOEF010;
  76. base_coef_set1 = ISPH3A_AFCOEF110;
  77. for (index = 0; index <= 8; index += 2) {
  78. /*Set IIR Filter0 Coefficients */
  79. coef = 0;
  80. coef |= conf->iir.coeff_set0[index];
  81. coef |= conf->iir.coeff_set0[index + 1] <<
  82. AF_COEF_SHIFT;
  83. isp_reg_writel(af->isp, coef, OMAP3_ISP_IOMEM_H3A,
  84. base_coef_set0);
  85. base_coef_set0 += AFCOEF_OFFSET;
  86. /*Set IIR Filter1 Coefficients */
  87. coef = 0;
  88. coef |= conf->iir.coeff_set1[index];
  89. coef |= conf->iir.coeff_set1[index + 1] <<
  90. AF_COEF_SHIFT;
  91. isp_reg_writel(af->isp, coef, OMAP3_ISP_IOMEM_H3A,
  92. base_coef_set1);
  93. base_coef_set1 += AFCOEF_OFFSET;
  94. }
  95. /* set AFCOEF0010 Register */
  96. isp_reg_writel(af->isp, conf->iir.coeff_set0[10],
  97. OMAP3_ISP_IOMEM_H3A, ISPH3A_AFCOEF0010);
  98. /* set AFCOEF1010 Register */
  99. isp_reg_writel(af->isp, conf->iir.coeff_set1[10],
  100. OMAP3_ISP_IOMEM_H3A, ISPH3A_AFCOEF1010);
  101. /* PCR Register */
  102. /* Set RGB Position */
  103. pcr = conf->rgb_pos << AF_RGBPOS_SHIFT;
  104. /* Set Accumulator Mode */
  105. if (conf->fvmode == OMAP3ISP_AF_MODE_PEAK)
  106. pcr |= AF_FVMODE;
  107. /* Set A-law */
  108. if (conf->alaw_enable)
  109. pcr |= AF_ALAW_EN;
  110. /* HMF Configurations */
  111. if (conf->hmf.enable) {
  112. /* Enable HMF */
  113. pcr |= AF_MED_EN;
  114. /* Set Median Threshold */
  115. pcr |= conf->hmf.threshold << AF_MED_TH_SHIFT;
  116. }
  117. /* Set PCR Register */
  118. isp_reg_clr_set(af->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
  119. AF_PCR_MASK, pcr);
  120. af->update = 0;
  121. af->config_counter += af->inc_config;
  122. af->inc_config = 0;
  123. af->buf_size = conf->buf_size;
  124. }
  125. static void h3a_af_enable(struct ispstat *af, int enable)
  126. {
  127. if (enable) {
  128. isp_reg_set(af->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
  129. ISPH3A_PCR_AF_EN);
  130. /* This bit is already set if AEWB is enabled */
  131. if (af->isp->isp_aewb.state != ISPSTAT_ENABLED)
  132. isp_reg_set(af->isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL,
  133. ISPCTRL_H3A_CLK_EN);
  134. } else {
  135. isp_reg_clr(af->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
  136. ISPH3A_PCR_AF_EN);
  137. /* This bit can't be cleared if AEWB is enabled */
  138. if (af->isp->isp_aewb.state != ISPSTAT_ENABLED)
  139. isp_reg_clr(af->isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL,
  140. ISPCTRL_H3A_CLK_EN);
  141. }
  142. }
  143. static int h3a_af_busy(struct ispstat *af)
  144. {
  145. return isp_reg_readl(af->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR)
  146. & ISPH3A_PCR_BUSYAF;
  147. }
  148. static u32 h3a_af_get_buf_size(struct omap3isp_h3a_af_config *conf)
  149. {
  150. return conf->paxel.h_cnt * conf->paxel.v_cnt * OMAP3ISP_AF_PAXEL_SIZE;
  151. }
  152. /* Function to check paxel parameters */
  153. static int h3a_af_validate_params(struct ispstat *af, void *new_conf)
  154. {
  155. struct omap3isp_h3a_af_config *user_cfg = new_conf;
  156. struct omap3isp_h3a_af_paxel *paxel_cfg = &user_cfg->paxel;
  157. struct omap3isp_h3a_af_iir *iir_cfg = &user_cfg->iir;
  158. int index;
  159. u32 buf_size;
  160. /* Check horizontal Count */
  161. if (IS_OUT_OF_BOUNDS(paxel_cfg->h_cnt,
  162. OMAP3ISP_AF_PAXEL_HORIZONTAL_COUNT_MIN,
  163. OMAP3ISP_AF_PAXEL_HORIZONTAL_COUNT_MAX))
  164. return -EINVAL;
  165. /* Check Vertical Count */
  166. if (IS_OUT_OF_BOUNDS(paxel_cfg->v_cnt,
  167. OMAP3ISP_AF_PAXEL_VERTICAL_COUNT_MIN,
  168. OMAP3ISP_AF_PAXEL_VERTICAL_COUNT_MAX))
  169. return -EINVAL;
  170. if (IS_OUT_OF_BOUNDS(paxel_cfg->height, OMAP3ISP_AF_PAXEL_HEIGHT_MIN,
  171. OMAP3ISP_AF_PAXEL_HEIGHT_MAX) ||
  172. paxel_cfg->height % 2)
  173. return -EINVAL;
  174. /* Check width */
  175. if (IS_OUT_OF_BOUNDS(paxel_cfg->width, OMAP3ISP_AF_PAXEL_WIDTH_MIN,
  176. OMAP3ISP_AF_PAXEL_WIDTH_MAX) ||
  177. paxel_cfg->width % 2)
  178. return -EINVAL;
  179. /* Check Line Increment */
  180. if (IS_OUT_OF_BOUNDS(paxel_cfg->line_inc,
  181. OMAP3ISP_AF_PAXEL_INCREMENT_MIN,
  182. OMAP3ISP_AF_PAXEL_INCREMENT_MAX) ||
  183. paxel_cfg->line_inc % 2)
  184. return -EINVAL;
  185. /* Check Horizontal Start */
  186. if ((paxel_cfg->h_start < iir_cfg->h_start) ||
  187. IS_OUT_OF_BOUNDS(paxel_cfg->h_start,
  188. OMAP3ISP_AF_PAXEL_HZSTART_MIN,
  189. OMAP3ISP_AF_PAXEL_HZSTART_MAX))
  190. return -EINVAL;
  191. /* Check IIR */
  192. for (index = 0; index < OMAP3ISP_AF_NUM_COEF; index++) {
  193. if ((iir_cfg->coeff_set0[index]) > OMAP3ISP_AF_COEF_MAX)
  194. return -EINVAL;
  195. if ((iir_cfg->coeff_set1[index]) > OMAP3ISP_AF_COEF_MAX)
  196. return -EINVAL;
  197. }
  198. if (IS_OUT_OF_BOUNDS(iir_cfg->h_start, OMAP3ISP_AF_IIRSH_MIN,
  199. OMAP3ISP_AF_IIRSH_MAX))
  200. return -EINVAL;
  201. /* Hack: If paxel size is 12, the 10th AF window may be corrupted */
  202. if ((paxel_cfg->h_cnt * paxel_cfg->v_cnt > 9) &&
  203. (paxel_cfg->width * paxel_cfg->height == 12))
  204. return -EINVAL;
  205. buf_size = h3a_af_get_buf_size(user_cfg);
  206. if (buf_size > user_cfg->buf_size)
  207. /* User buf_size request wasn't enough */
  208. user_cfg->buf_size = buf_size;
  209. else if (user_cfg->buf_size > OMAP3ISP_AF_MAX_BUF_SIZE)
  210. user_cfg->buf_size = OMAP3ISP_AF_MAX_BUF_SIZE;
  211. return 0;
  212. }
  213. /* Update local parameters */
  214. static void h3a_af_set_params(struct ispstat *af, void *new_conf)
  215. {
  216. struct omap3isp_h3a_af_config *user_cfg = new_conf;
  217. struct omap3isp_h3a_af_config *cur_cfg = af->priv;
  218. int update = 0;
  219. int index;
  220. /* alaw */
  221. if (cur_cfg->alaw_enable != user_cfg->alaw_enable) {
  222. update = 1;
  223. goto out;
  224. }
  225. /* hmf */
  226. if (cur_cfg->hmf.enable != user_cfg->hmf.enable) {
  227. update = 1;
  228. goto out;
  229. }
  230. if (cur_cfg->hmf.threshold != user_cfg->hmf.threshold) {
  231. update = 1;
  232. goto out;
  233. }
  234. /* rgbpos */
  235. if (cur_cfg->rgb_pos != user_cfg->rgb_pos) {
  236. update = 1;
  237. goto out;
  238. }
  239. /* iir */
  240. if (cur_cfg->iir.h_start != user_cfg->iir.h_start) {
  241. update = 1;
  242. goto out;
  243. }
  244. for (index = 0; index < OMAP3ISP_AF_NUM_COEF; index++) {
  245. if (cur_cfg->iir.coeff_set0[index] !=
  246. user_cfg->iir.coeff_set0[index]) {
  247. update = 1;
  248. goto out;
  249. }
  250. if (cur_cfg->iir.coeff_set1[index] !=
  251. user_cfg->iir.coeff_set1[index]) {
  252. update = 1;
  253. goto out;
  254. }
  255. }
  256. /* paxel */
  257. if ((cur_cfg->paxel.width != user_cfg->paxel.width) ||
  258. (cur_cfg->paxel.height != user_cfg->paxel.height) ||
  259. (cur_cfg->paxel.h_start != user_cfg->paxel.h_start) ||
  260. (cur_cfg->paxel.v_start != user_cfg->paxel.v_start) ||
  261. (cur_cfg->paxel.h_cnt != user_cfg->paxel.h_cnt) ||
  262. (cur_cfg->paxel.v_cnt != user_cfg->paxel.v_cnt) ||
  263. (cur_cfg->paxel.line_inc != user_cfg->paxel.line_inc)) {
  264. update = 1;
  265. goto out;
  266. }
  267. /* af_mode */
  268. if (cur_cfg->fvmode != user_cfg->fvmode)
  269. update = 1;
  270. out:
  271. if (update || !af->configured) {
  272. memcpy(cur_cfg, user_cfg, sizeof(*cur_cfg));
  273. af->inc_config++;
  274. af->update = 1;
  275. /*
  276. * User might be asked for a bigger buffer than necessary for
  277. * this configuration. In order to return the right amount of
  278. * data during buffer request, let's calculate the size here
  279. * instead of stick with user_cfg->buf_size.
  280. */
  281. cur_cfg->buf_size = h3a_af_get_buf_size(cur_cfg);
  282. }
  283. }
  284. static long h3a_af_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  285. {
  286. struct ispstat *stat = v4l2_get_subdevdata(sd);
  287. switch (cmd) {
  288. case VIDIOC_OMAP3ISP_AF_CFG:
  289. return omap3isp_stat_config(stat, arg);
  290. case VIDIOC_OMAP3ISP_STAT_REQ:
  291. return omap3isp_stat_request_statistics(stat, arg);
  292. case VIDIOC_OMAP3ISP_STAT_EN: {
  293. int *en = arg;
  294. return omap3isp_stat_enable(stat, !!*en);
  295. }
  296. }
  297. return -ENOIOCTLCMD;
  298. }
  299. static const struct ispstat_ops h3a_af_ops = {
  300. .validate_params = h3a_af_validate_params,
  301. .set_params = h3a_af_set_params,
  302. .setup_regs = h3a_af_setup_regs,
  303. .enable = h3a_af_enable,
  304. .busy = h3a_af_busy,
  305. };
  306. static const struct v4l2_subdev_core_ops h3a_af_subdev_core_ops = {
  307. .ioctl = h3a_af_ioctl,
  308. .subscribe_event = omap3isp_stat_subscribe_event,
  309. .unsubscribe_event = omap3isp_stat_unsubscribe_event,
  310. };
  311. static const struct v4l2_subdev_video_ops h3a_af_subdev_video_ops = {
  312. .s_stream = omap3isp_stat_s_stream,
  313. };
  314. static const struct v4l2_subdev_ops h3a_af_subdev_ops = {
  315. .core = &h3a_af_subdev_core_ops,
  316. .video = &h3a_af_subdev_video_ops,
  317. };
  318. /* Function to register the AF character device driver. */
  319. int omap3isp_h3a_af_init(struct isp_device *isp)
  320. {
  321. struct ispstat *af = &isp->isp_af;
  322. struct omap3isp_h3a_af_config *af_cfg;
  323. struct omap3isp_h3a_af_config *af_recover_cfg;
  324. int ret;
  325. af_cfg = kzalloc(sizeof(*af_cfg), GFP_KERNEL);
  326. if (af_cfg == NULL)
  327. return -ENOMEM;
  328. memset(af, 0, sizeof(*af));
  329. af->ops = &h3a_af_ops;
  330. af->priv = af_cfg;
  331. af->dma_ch = -1;
  332. af->event_type = V4L2_EVENT_OMAP3ISP_AF;
  333. af->isp = isp;
  334. /* Set recover state configuration */
  335. af_recover_cfg = kzalloc(sizeof(*af_recover_cfg), GFP_KERNEL);
  336. if (!af_recover_cfg) {
  337. dev_err(af->isp->dev, "AF: cannot allocate memory for recover "
  338. "configuration.\n");
  339. ret = -ENOMEM;
  340. goto err_recover_alloc;
  341. }
  342. af_recover_cfg->paxel.h_start = OMAP3ISP_AF_PAXEL_HZSTART_MIN;
  343. af_recover_cfg->paxel.width = OMAP3ISP_AF_PAXEL_WIDTH_MIN;
  344. af_recover_cfg->paxel.height = OMAP3ISP_AF_PAXEL_HEIGHT_MIN;
  345. af_recover_cfg->paxel.h_cnt = OMAP3ISP_AF_PAXEL_HORIZONTAL_COUNT_MIN;
  346. af_recover_cfg->paxel.v_cnt = OMAP3ISP_AF_PAXEL_VERTICAL_COUNT_MIN;
  347. af_recover_cfg->paxel.line_inc = OMAP3ISP_AF_PAXEL_INCREMENT_MIN;
  348. if (h3a_af_validate_params(af, af_recover_cfg)) {
  349. dev_err(af->isp->dev, "AF: recover configuration is "
  350. "invalid.\n");
  351. ret = -EINVAL;
  352. goto err_conf;
  353. }
  354. af_recover_cfg->buf_size = h3a_af_get_buf_size(af_recover_cfg);
  355. af->recover_priv = af_recover_cfg;
  356. ret = omap3isp_stat_init(af, "AF", &h3a_af_subdev_ops);
  357. if (ret)
  358. goto err_conf;
  359. return 0;
  360. err_conf:
  361. kfree(af_recover_cfg);
  362. err_recover_alloc:
  363. kfree(af_cfg);
  364. return ret;
  365. }
  366. void omap3isp_h3a_af_cleanup(struct isp_device *isp)
  367. {
  368. kfree(isp->isp_af.priv);
  369. kfree(isp->isp_af.recover_priv);
  370. omap3isp_stat_cleanup(&isp->isp_af);
  371. }