isph3a_af.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. omap3isp_subclk_enable(af->isp, OMAP3_ISP_SUBCLK_AF);
  131. } else {
  132. isp_reg_clr(af->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
  133. ISPH3A_PCR_AF_EN);
  134. omap3isp_subclk_disable(af->isp, OMAP3_ISP_SUBCLK_AF);
  135. }
  136. }
  137. static int h3a_af_busy(struct ispstat *af)
  138. {
  139. return isp_reg_readl(af->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR)
  140. & ISPH3A_PCR_BUSYAF;
  141. }
  142. static u32 h3a_af_get_buf_size(struct omap3isp_h3a_af_config *conf)
  143. {
  144. return conf->paxel.h_cnt * conf->paxel.v_cnt * OMAP3ISP_AF_PAXEL_SIZE;
  145. }
  146. /* Function to check paxel parameters */
  147. static int h3a_af_validate_params(struct ispstat *af, void *new_conf)
  148. {
  149. struct omap3isp_h3a_af_config *user_cfg = new_conf;
  150. struct omap3isp_h3a_af_paxel *paxel_cfg = &user_cfg->paxel;
  151. struct omap3isp_h3a_af_iir *iir_cfg = &user_cfg->iir;
  152. int index;
  153. u32 buf_size;
  154. /* Check horizontal Count */
  155. if (IS_OUT_OF_BOUNDS(paxel_cfg->h_cnt,
  156. OMAP3ISP_AF_PAXEL_HORIZONTAL_COUNT_MIN,
  157. OMAP3ISP_AF_PAXEL_HORIZONTAL_COUNT_MAX))
  158. return -EINVAL;
  159. /* Check Vertical Count */
  160. if (IS_OUT_OF_BOUNDS(paxel_cfg->v_cnt,
  161. OMAP3ISP_AF_PAXEL_VERTICAL_COUNT_MIN,
  162. OMAP3ISP_AF_PAXEL_VERTICAL_COUNT_MAX))
  163. return -EINVAL;
  164. if (IS_OUT_OF_BOUNDS(paxel_cfg->height, OMAP3ISP_AF_PAXEL_HEIGHT_MIN,
  165. OMAP3ISP_AF_PAXEL_HEIGHT_MAX) ||
  166. paxel_cfg->height % 2)
  167. return -EINVAL;
  168. /* Check width */
  169. if (IS_OUT_OF_BOUNDS(paxel_cfg->width, OMAP3ISP_AF_PAXEL_WIDTH_MIN,
  170. OMAP3ISP_AF_PAXEL_WIDTH_MAX) ||
  171. paxel_cfg->width % 2)
  172. return -EINVAL;
  173. /* Check Line Increment */
  174. if (IS_OUT_OF_BOUNDS(paxel_cfg->line_inc,
  175. OMAP3ISP_AF_PAXEL_INCREMENT_MIN,
  176. OMAP3ISP_AF_PAXEL_INCREMENT_MAX) ||
  177. paxel_cfg->line_inc % 2)
  178. return -EINVAL;
  179. /* Check Horizontal Start */
  180. if ((paxel_cfg->h_start < iir_cfg->h_start) ||
  181. IS_OUT_OF_BOUNDS(paxel_cfg->h_start,
  182. OMAP3ISP_AF_PAXEL_HZSTART_MIN,
  183. OMAP3ISP_AF_PAXEL_HZSTART_MAX))
  184. return -EINVAL;
  185. /* Check IIR */
  186. for (index = 0; index < OMAP3ISP_AF_NUM_COEF; index++) {
  187. if ((iir_cfg->coeff_set0[index]) > OMAP3ISP_AF_COEF_MAX)
  188. return -EINVAL;
  189. if ((iir_cfg->coeff_set1[index]) > OMAP3ISP_AF_COEF_MAX)
  190. return -EINVAL;
  191. }
  192. if (IS_OUT_OF_BOUNDS(iir_cfg->h_start, OMAP3ISP_AF_IIRSH_MIN,
  193. OMAP3ISP_AF_IIRSH_MAX))
  194. return -EINVAL;
  195. /* Hack: If paxel size is 12, the 10th AF window may be corrupted */
  196. if ((paxel_cfg->h_cnt * paxel_cfg->v_cnt > 9) &&
  197. (paxel_cfg->width * paxel_cfg->height == 12))
  198. return -EINVAL;
  199. buf_size = h3a_af_get_buf_size(user_cfg);
  200. if (buf_size > user_cfg->buf_size)
  201. /* User buf_size request wasn't enough */
  202. user_cfg->buf_size = buf_size;
  203. else if (user_cfg->buf_size > OMAP3ISP_AF_MAX_BUF_SIZE)
  204. user_cfg->buf_size = OMAP3ISP_AF_MAX_BUF_SIZE;
  205. return 0;
  206. }
  207. /* Update local parameters */
  208. static void h3a_af_set_params(struct ispstat *af, void *new_conf)
  209. {
  210. struct omap3isp_h3a_af_config *user_cfg = new_conf;
  211. struct omap3isp_h3a_af_config *cur_cfg = af->priv;
  212. int update = 0;
  213. int index;
  214. /* alaw */
  215. if (cur_cfg->alaw_enable != user_cfg->alaw_enable) {
  216. update = 1;
  217. goto out;
  218. }
  219. /* hmf */
  220. if (cur_cfg->hmf.enable != user_cfg->hmf.enable) {
  221. update = 1;
  222. goto out;
  223. }
  224. if (cur_cfg->hmf.threshold != user_cfg->hmf.threshold) {
  225. update = 1;
  226. goto out;
  227. }
  228. /* rgbpos */
  229. if (cur_cfg->rgb_pos != user_cfg->rgb_pos) {
  230. update = 1;
  231. goto out;
  232. }
  233. /* iir */
  234. if (cur_cfg->iir.h_start != user_cfg->iir.h_start) {
  235. update = 1;
  236. goto out;
  237. }
  238. for (index = 0; index < OMAP3ISP_AF_NUM_COEF; index++) {
  239. if (cur_cfg->iir.coeff_set0[index] !=
  240. user_cfg->iir.coeff_set0[index]) {
  241. update = 1;
  242. goto out;
  243. }
  244. if (cur_cfg->iir.coeff_set1[index] !=
  245. user_cfg->iir.coeff_set1[index]) {
  246. update = 1;
  247. goto out;
  248. }
  249. }
  250. /* paxel */
  251. if ((cur_cfg->paxel.width != user_cfg->paxel.width) ||
  252. (cur_cfg->paxel.height != user_cfg->paxel.height) ||
  253. (cur_cfg->paxel.h_start != user_cfg->paxel.h_start) ||
  254. (cur_cfg->paxel.v_start != user_cfg->paxel.v_start) ||
  255. (cur_cfg->paxel.h_cnt != user_cfg->paxel.h_cnt) ||
  256. (cur_cfg->paxel.v_cnt != user_cfg->paxel.v_cnt) ||
  257. (cur_cfg->paxel.line_inc != user_cfg->paxel.line_inc)) {
  258. update = 1;
  259. goto out;
  260. }
  261. /* af_mode */
  262. if (cur_cfg->fvmode != user_cfg->fvmode)
  263. update = 1;
  264. out:
  265. if (update || !af->configured) {
  266. memcpy(cur_cfg, user_cfg, sizeof(*cur_cfg));
  267. af->inc_config++;
  268. af->update = 1;
  269. /*
  270. * User might be asked for a bigger buffer than necessary for
  271. * this configuration. In order to return the right amount of
  272. * data during buffer request, let's calculate the size here
  273. * instead of stick with user_cfg->buf_size.
  274. */
  275. cur_cfg->buf_size = h3a_af_get_buf_size(cur_cfg);
  276. }
  277. }
  278. static long h3a_af_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  279. {
  280. struct ispstat *stat = v4l2_get_subdevdata(sd);
  281. switch (cmd) {
  282. case VIDIOC_OMAP3ISP_AF_CFG:
  283. return omap3isp_stat_config(stat, arg);
  284. case VIDIOC_OMAP3ISP_STAT_REQ:
  285. return omap3isp_stat_request_statistics(stat, arg);
  286. case VIDIOC_OMAP3ISP_STAT_EN: {
  287. int *en = arg;
  288. return omap3isp_stat_enable(stat, !!*en);
  289. }
  290. }
  291. return -ENOIOCTLCMD;
  292. }
  293. static const struct ispstat_ops h3a_af_ops = {
  294. .validate_params = h3a_af_validate_params,
  295. .set_params = h3a_af_set_params,
  296. .setup_regs = h3a_af_setup_regs,
  297. .enable = h3a_af_enable,
  298. .busy = h3a_af_busy,
  299. };
  300. static const struct v4l2_subdev_core_ops h3a_af_subdev_core_ops = {
  301. .ioctl = h3a_af_ioctl,
  302. .subscribe_event = omap3isp_stat_subscribe_event,
  303. .unsubscribe_event = omap3isp_stat_unsubscribe_event,
  304. };
  305. static const struct v4l2_subdev_video_ops h3a_af_subdev_video_ops = {
  306. .s_stream = omap3isp_stat_s_stream,
  307. };
  308. static const struct v4l2_subdev_ops h3a_af_subdev_ops = {
  309. .core = &h3a_af_subdev_core_ops,
  310. .video = &h3a_af_subdev_video_ops,
  311. };
  312. /* Function to register the AF character device driver. */
  313. int omap3isp_h3a_af_init(struct isp_device *isp)
  314. {
  315. struct ispstat *af = &isp->isp_af;
  316. struct omap3isp_h3a_af_config *af_cfg;
  317. struct omap3isp_h3a_af_config *af_recover_cfg;
  318. af_cfg = devm_kzalloc(isp->dev, sizeof(*af_cfg), GFP_KERNEL);
  319. if (af_cfg == NULL)
  320. return -ENOMEM;
  321. af->ops = &h3a_af_ops;
  322. af->priv = af_cfg;
  323. af->dma_ch = -1;
  324. af->event_type = V4L2_EVENT_OMAP3ISP_AF;
  325. af->isp = isp;
  326. /* Set recover state configuration */
  327. af_recover_cfg = devm_kzalloc(isp->dev, sizeof(*af_recover_cfg),
  328. GFP_KERNEL);
  329. if (!af_recover_cfg) {
  330. dev_err(af->isp->dev, "AF: cannot allocate memory for recover "
  331. "configuration.\n");
  332. return -ENOMEM;
  333. }
  334. af_recover_cfg->paxel.h_start = OMAP3ISP_AF_PAXEL_HZSTART_MIN;
  335. af_recover_cfg->paxel.width = OMAP3ISP_AF_PAXEL_WIDTH_MIN;
  336. af_recover_cfg->paxel.height = OMAP3ISP_AF_PAXEL_HEIGHT_MIN;
  337. af_recover_cfg->paxel.h_cnt = OMAP3ISP_AF_PAXEL_HORIZONTAL_COUNT_MIN;
  338. af_recover_cfg->paxel.v_cnt = OMAP3ISP_AF_PAXEL_VERTICAL_COUNT_MIN;
  339. af_recover_cfg->paxel.line_inc = OMAP3ISP_AF_PAXEL_INCREMENT_MIN;
  340. if (h3a_af_validate_params(af, af_recover_cfg)) {
  341. dev_err(af->isp->dev, "AF: recover configuration is "
  342. "invalid.\n");
  343. return -EINVAL;
  344. }
  345. af_recover_cfg->buf_size = h3a_af_get_buf_size(af_recover_cfg);
  346. af->recover_priv = af_recover_cfg;
  347. return omap3isp_stat_init(af, "AF", &h3a_af_subdev_ops);
  348. }
  349. void omap3isp_h3a_af_cleanup(struct isp_device *isp)
  350. {
  351. omap3isp_stat_cleanup(&isp->isp_af);
  352. }