isph3a_af.c 11 KB

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