isphist.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * isphist.c
  3. *
  4. * TI OMAP3 ISP - Histogram 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. #include <linux/delay.h>
  28. #include <linux/slab.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/device.h>
  31. #include "isp.h"
  32. #include "ispreg.h"
  33. #include "isphist.h"
  34. #define HIST_CONFIG_DMA 1
  35. #define HIST_USING_DMA(hist) ((hist)->dma_ch >= 0)
  36. /*
  37. * hist_reset_mem - clear Histogram memory before start stats engine.
  38. */
  39. static void hist_reset_mem(struct ispstat *hist)
  40. {
  41. struct isp_device *isp = hist->isp;
  42. struct omap3isp_hist_config *conf = hist->priv;
  43. unsigned int i;
  44. isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_HIST, ISPHIST_ADDR);
  45. /*
  46. * By setting it, the histogram internal buffer is being cleared at the
  47. * same time it's being read. This bit must be cleared afterwards.
  48. */
  49. isp_reg_set(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT, ISPHIST_CNT_CLEAR);
  50. /*
  51. * We'll clear 4 words at each iteration for optimization. It avoids
  52. * 3/4 of the jumps. We also know HIST_MEM_SIZE is divisible by 4.
  53. */
  54. for (i = OMAP3ISP_HIST_MEM_SIZE / 4; i > 0; i--) {
  55. isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  56. isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  57. isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  58. isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  59. }
  60. isp_reg_clr(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT, ISPHIST_CNT_CLEAR);
  61. hist->wait_acc_frames = conf->num_acc_frames;
  62. }
  63. static void hist_dma_config(struct ispstat *hist)
  64. {
  65. hist->dma_config.data_type = OMAP_DMA_DATA_TYPE_S32;
  66. hist->dma_config.sync_mode = OMAP_DMA_SYNC_ELEMENT;
  67. hist->dma_config.frame_count = 1;
  68. hist->dma_config.src_amode = OMAP_DMA_AMODE_CONSTANT;
  69. hist->dma_config.src_start = OMAP3ISP_HIST_REG_BASE + ISPHIST_DATA;
  70. hist->dma_config.dst_amode = OMAP_DMA_AMODE_POST_INC;
  71. hist->dma_config.src_or_dst_synch = OMAP_DMA_SRC_SYNC;
  72. }
  73. /*
  74. * hist_setup_regs - Helper function to update Histogram registers.
  75. */
  76. static void hist_setup_regs(struct ispstat *hist, void *priv)
  77. {
  78. struct isp_device *isp = hist->isp;
  79. struct omap3isp_hist_config *conf = priv;
  80. int c;
  81. u32 cnt;
  82. u32 wb_gain;
  83. u32 reg_hor[OMAP3ISP_HIST_MAX_REGIONS];
  84. u32 reg_ver[OMAP3ISP_HIST_MAX_REGIONS];
  85. if (!hist->update || hist->state == ISPSTAT_DISABLED ||
  86. hist->state == ISPSTAT_DISABLING)
  87. return;
  88. cnt = conf->cfa << ISPHIST_CNT_CFA_SHIFT;
  89. wb_gain = conf->wg[0] << ISPHIST_WB_GAIN_WG00_SHIFT;
  90. wb_gain |= conf->wg[1] << ISPHIST_WB_GAIN_WG01_SHIFT;
  91. wb_gain |= conf->wg[2] << ISPHIST_WB_GAIN_WG02_SHIFT;
  92. if (conf->cfa == OMAP3ISP_HIST_CFA_BAYER)
  93. wb_gain |= conf->wg[3] << ISPHIST_WB_GAIN_WG03_SHIFT;
  94. /* Regions size and position */
  95. for (c = 0; c < OMAP3ISP_HIST_MAX_REGIONS; c++) {
  96. if (c < conf->num_regions) {
  97. reg_hor[c] = conf->region[c].h_start <<
  98. ISPHIST_REG_START_SHIFT;
  99. reg_hor[c] = conf->region[c].h_end <<
  100. ISPHIST_REG_END_SHIFT;
  101. reg_ver[c] = conf->region[c].v_start <<
  102. ISPHIST_REG_START_SHIFT;
  103. reg_ver[c] = conf->region[c].v_end <<
  104. ISPHIST_REG_END_SHIFT;
  105. } else {
  106. reg_hor[c] = 0;
  107. reg_ver[c] = 0;
  108. }
  109. }
  110. cnt |= conf->hist_bins << ISPHIST_CNT_BINS_SHIFT;
  111. switch (conf->hist_bins) {
  112. case OMAP3ISP_HIST_BINS_256:
  113. cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 8) <<
  114. ISPHIST_CNT_SHIFT_SHIFT;
  115. break;
  116. case OMAP3ISP_HIST_BINS_128:
  117. cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 7) <<
  118. ISPHIST_CNT_SHIFT_SHIFT;
  119. break;
  120. case OMAP3ISP_HIST_BINS_64:
  121. cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 6) <<
  122. ISPHIST_CNT_SHIFT_SHIFT;
  123. break;
  124. default: /* OMAP3ISP_HIST_BINS_32 */
  125. cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 5) <<
  126. ISPHIST_CNT_SHIFT_SHIFT;
  127. break;
  128. }
  129. hist_reset_mem(hist);
  130. isp_reg_writel(isp, cnt, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT);
  131. isp_reg_writel(isp, wb_gain, OMAP3_ISP_IOMEM_HIST, ISPHIST_WB_GAIN);
  132. isp_reg_writel(isp, reg_hor[0], OMAP3_ISP_IOMEM_HIST, ISPHIST_R0_HORZ);
  133. isp_reg_writel(isp, reg_ver[0], OMAP3_ISP_IOMEM_HIST, ISPHIST_R0_VERT);
  134. isp_reg_writel(isp, reg_hor[1], OMAP3_ISP_IOMEM_HIST, ISPHIST_R1_HORZ);
  135. isp_reg_writel(isp, reg_ver[1], OMAP3_ISP_IOMEM_HIST, ISPHIST_R1_VERT);
  136. isp_reg_writel(isp, reg_hor[2], OMAP3_ISP_IOMEM_HIST, ISPHIST_R2_HORZ);
  137. isp_reg_writel(isp, reg_ver[2], OMAP3_ISP_IOMEM_HIST, ISPHIST_R2_VERT);
  138. isp_reg_writel(isp, reg_hor[3], OMAP3_ISP_IOMEM_HIST, ISPHIST_R3_HORZ);
  139. isp_reg_writel(isp, reg_ver[3], OMAP3_ISP_IOMEM_HIST, ISPHIST_R3_VERT);
  140. hist->update = 0;
  141. hist->config_counter += hist->inc_config;
  142. hist->inc_config = 0;
  143. hist->buf_size = conf->buf_size;
  144. }
  145. static void hist_enable(struct ispstat *hist, int enable)
  146. {
  147. if (enable) {
  148. isp_reg_set(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_PCR,
  149. ISPHIST_PCR_ENABLE);
  150. isp_reg_set(hist->isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL,
  151. ISPCTRL_HIST_CLK_EN);
  152. } else {
  153. isp_reg_clr(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_PCR,
  154. ISPHIST_PCR_ENABLE);
  155. isp_reg_clr(hist->isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL,
  156. ISPCTRL_HIST_CLK_EN);
  157. }
  158. }
  159. static int hist_busy(struct ispstat *hist)
  160. {
  161. return isp_reg_readl(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_PCR)
  162. & ISPHIST_PCR_BUSY;
  163. }
  164. static void hist_dma_cb(int lch, u16 ch_status, void *data)
  165. {
  166. struct ispstat *hist = data;
  167. if (ch_status & ~OMAP_DMA_BLOCK_IRQ) {
  168. dev_dbg(hist->isp->dev, "hist: DMA error. status = 0x%04x\n",
  169. ch_status);
  170. omap_stop_dma(lch);
  171. hist_reset_mem(hist);
  172. atomic_set(&hist->buf_err, 1);
  173. }
  174. isp_reg_clr(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT,
  175. ISPHIST_CNT_CLEAR);
  176. omap3isp_stat_dma_isr(hist);
  177. if (hist->state != ISPSTAT_DISABLED)
  178. omap3isp_hist_dma_done(hist->isp);
  179. }
  180. static int hist_buf_dma(struct ispstat *hist)
  181. {
  182. dma_addr_t dma_addr = hist->active_buf->dma_addr;
  183. if (unlikely(!dma_addr)) {
  184. dev_dbg(hist->isp->dev, "hist: invalid DMA buffer address\n");
  185. hist_reset_mem(hist);
  186. return STAT_NO_BUF;
  187. }
  188. isp_reg_writel(hist->isp, 0, OMAP3_ISP_IOMEM_HIST, ISPHIST_ADDR);
  189. isp_reg_set(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT,
  190. ISPHIST_CNT_CLEAR);
  191. omap3isp_flush(hist->isp);
  192. hist->dma_config.dst_start = dma_addr;
  193. hist->dma_config.elem_count = hist->buf_size / sizeof(u32);
  194. omap_set_dma_params(hist->dma_ch, &hist->dma_config);
  195. omap_start_dma(hist->dma_ch);
  196. return STAT_BUF_WAITING_DMA;
  197. }
  198. static int hist_buf_pio(struct ispstat *hist)
  199. {
  200. struct isp_device *isp = hist->isp;
  201. u32 *buf = hist->active_buf->virt_addr;
  202. unsigned int i;
  203. if (!buf) {
  204. dev_dbg(isp->dev, "hist: invalid PIO buffer address\n");
  205. hist_reset_mem(hist);
  206. return STAT_NO_BUF;
  207. }
  208. isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_HIST, ISPHIST_ADDR);
  209. /*
  210. * By setting it, the histogram internal buffer is being cleared at the
  211. * same time it's being read. This bit must be cleared just after all
  212. * data is acquired.
  213. */
  214. isp_reg_set(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT, ISPHIST_CNT_CLEAR);
  215. /*
  216. * We'll read 4 times a 4-bytes-word at each iteration for
  217. * optimization. It avoids 3/4 of the jumps. We also know buf_size is
  218. * divisible by 16.
  219. */
  220. for (i = hist->buf_size / 16; i > 0; i--) {
  221. *buf++ = isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  222. *buf++ = isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  223. *buf++ = isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  224. *buf++ = isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  225. }
  226. isp_reg_clr(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT,
  227. ISPHIST_CNT_CLEAR);
  228. return STAT_BUF_DONE;
  229. }
  230. /*
  231. * hist_buf_process - Callback from ISP driver for HIST interrupt.
  232. */
  233. static int hist_buf_process(struct ispstat *hist)
  234. {
  235. struct omap3isp_hist_config *user_cfg = hist->priv;
  236. int ret;
  237. if (atomic_read(&hist->buf_err) || hist->state != ISPSTAT_ENABLED) {
  238. hist_reset_mem(hist);
  239. return STAT_NO_BUF;
  240. }
  241. if (--(hist->wait_acc_frames))
  242. return STAT_NO_BUF;
  243. if (HIST_USING_DMA(hist))
  244. ret = hist_buf_dma(hist);
  245. else
  246. ret = hist_buf_pio(hist);
  247. hist->wait_acc_frames = user_cfg->num_acc_frames;
  248. return ret;
  249. }
  250. static u32 hist_get_buf_size(struct omap3isp_hist_config *conf)
  251. {
  252. return OMAP3ISP_HIST_MEM_SIZE_BINS(conf->hist_bins) * conf->num_regions;
  253. }
  254. /*
  255. * hist_validate_params - Helper function to check user given params.
  256. * @user_cfg: Pointer to user configuration structure.
  257. *
  258. * Returns 0 on success configuration.
  259. */
  260. static int hist_validate_params(struct ispstat *hist, void *new_conf)
  261. {
  262. struct omap3isp_hist_config *user_cfg = new_conf;
  263. int c;
  264. u32 buf_size;
  265. if (user_cfg->cfa > OMAP3ISP_HIST_CFA_FOVEONX3)
  266. return -EINVAL;
  267. /* Regions size and position */
  268. if ((user_cfg->num_regions < OMAP3ISP_HIST_MIN_REGIONS) ||
  269. (user_cfg->num_regions > OMAP3ISP_HIST_MAX_REGIONS))
  270. return -EINVAL;
  271. /* Regions */
  272. for (c = 0; c < user_cfg->num_regions; c++) {
  273. if (user_cfg->region[c].h_start & ~ISPHIST_REG_START_END_MASK)
  274. return -EINVAL;
  275. if (user_cfg->region[c].h_end & ~ISPHIST_REG_START_END_MASK)
  276. return -EINVAL;
  277. if (user_cfg->region[c].v_start & ~ISPHIST_REG_START_END_MASK)
  278. return -EINVAL;
  279. if (user_cfg->region[c].v_end & ~ISPHIST_REG_START_END_MASK)
  280. return -EINVAL;
  281. if (user_cfg->region[c].h_start > user_cfg->region[c].h_end)
  282. return -EINVAL;
  283. if (user_cfg->region[c].v_start > user_cfg->region[c].v_end)
  284. return -EINVAL;
  285. }
  286. switch (user_cfg->num_regions) {
  287. case 1:
  288. if (user_cfg->hist_bins > OMAP3ISP_HIST_BINS_256)
  289. return -EINVAL;
  290. break;
  291. case 2:
  292. if (user_cfg->hist_bins > OMAP3ISP_HIST_BINS_128)
  293. return -EINVAL;
  294. break;
  295. default: /* 3 or 4 */
  296. if (user_cfg->hist_bins > OMAP3ISP_HIST_BINS_64)
  297. return -EINVAL;
  298. break;
  299. }
  300. buf_size = hist_get_buf_size(user_cfg);
  301. if (buf_size > user_cfg->buf_size)
  302. /* User's buf_size request wasn't enoght */
  303. user_cfg->buf_size = buf_size;
  304. else if (user_cfg->buf_size > OMAP3ISP_HIST_MAX_BUF_SIZE)
  305. user_cfg->buf_size = OMAP3ISP_HIST_MAX_BUF_SIZE;
  306. return 0;
  307. }
  308. static int hist_comp_params(struct ispstat *hist,
  309. struct omap3isp_hist_config *user_cfg)
  310. {
  311. struct omap3isp_hist_config *cur_cfg = hist->priv;
  312. int c;
  313. if (cur_cfg->cfa != user_cfg->cfa)
  314. return 1;
  315. if (cur_cfg->num_acc_frames != user_cfg->num_acc_frames)
  316. return 1;
  317. if (cur_cfg->hist_bins != user_cfg->hist_bins)
  318. return 1;
  319. for (c = 0; c < OMAP3ISP_HIST_MAX_WG; c++) {
  320. if (c == 3 && user_cfg->cfa == OMAP3ISP_HIST_CFA_FOVEONX3)
  321. break;
  322. else if (cur_cfg->wg[c] != user_cfg->wg[c])
  323. return 1;
  324. }
  325. if (cur_cfg->num_regions != user_cfg->num_regions)
  326. return 1;
  327. /* Regions */
  328. for (c = 0; c < user_cfg->num_regions; c++) {
  329. if (cur_cfg->region[c].h_start != user_cfg->region[c].h_start)
  330. return 1;
  331. if (cur_cfg->region[c].h_end != user_cfg->region[c].h_end)
  332. return 1;
  333. if (cur_cfg->region[c].v_start != user_cfg->region[c].v_start)
  334. return 1;
  335. if (cur_cfg->region[c].v_end != user_cfg->region[c].v_end)
  336. return 1;
  337. }
  338. return 0;
  339. }
  340. /*
  341. * hist_update_params - Helper function to check and store user given params.
  342. * @new_conf: Pointer to user configuration structure.
  343. */
  344. static void hist_set_params(struct ispstat *hist, void *new_conf)
  345. {
  346. struct omap3isp_hist_config *user_cfg = new_conf;
  347. struct omap3isp_hist_config *cur_cfg = hist->priv;
  348. if (!hist->configured || hist_comp_params(hist, user_cfg)) {
  349. memcpy(cur_cfg, user_cfg, sizeof(*user_cfg));
  350. if (user_cfg->num_acc_frames == 0)
  351. user_cfg->num_acc_frames = 1;
  352. hist->inc_config++;
  353. hist->update = 1;
  354. /*
  355. * User might be asked for a bigger buffer than necessary for
  356. * this configuration. In order to return the right amount of
  357. * data during buffer request, let's calculate the size here
  358. * instead of stick with user_cfg->buf_size.
  359. */
  360. cur_cfg->buf_size = hist_get_buf_size(cur_cfg);
  361. }
  362. }
  363. static long hist_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  364. {
  365. struct ispstat *stat = v4l2_get_subdevdata(sd);
  366. switch (cmd) {
  367. case VIDIOC_OMAP3ISP_HIST_CFG:
  368. return omap3isp_stat_config(stat, arg);
  369. case VIDIOC_OMAP3ISP_STAT_REQ:
  370. return omap3isp_stat_request_statistics(stat, arg);
  371. case VIDIOC_OMAP3ISP_STAT_EN: {
  372. int *en = arg;
  373. return omap3isp_stat_enable(stat, !!*en);
  374. }
  375. }
  376. return -ENOIOCTLCMD;
  377. }
  378. static const struct ispstat_ops hist_ops = {
  379. .validate_params = hist_validate_params,
  380. .set_params = hist_set_params,
  381. .setup_regs = hist_setup_regs,
  382. .enable = hist_enable,
  383. .busy = hist_busy,
  384. .buf_process = hist_buf_process,
  385. };
  386. static const struct v4l2_subdev_core_ops hist_subdev_core_ops = {
  387. .ioctl = hist_ioctl,
  388. .subscribe_event = omap3isp_stat_subscribe_event,
  389. .unsubscribe_event = omap3isp_stat_unsubscribe_event,
  390. };
  391. static const struct v4l2_subdev_video_ops hist_subdev_video_ops = {
  392. .s_stream = omap3isp_stat_s_stream,
  393. };
  394. static const struct v4l2_subdev_ops hist_subdev_ops = {
  395. .core = &hist_subdev_core_ops,
  396. .video = &hist_subdev_video_ops,
  397. };
  398. /*
  399. * omap3isp_hist_init - Module Initialization.
  400. */
  401. int omap3isp_hist_init(struct isp_device *isp)
  402. {
  403. struct ispstat *hist = &isp->isp_hist;
  404. struct omap3isp_hist_config *hist_cfg;
  405. int ret = -1;
  406. hist_cfg = kzalloc(sizeof(*hist_cfg), GFP_KERNEL);
  407. if (hist_cfg == NULL)
  408. return -ENOMEM;
  409. memset(hist, 0, sizeof(*hist));
  410. if (HIST_CONFIG_DMA)
  411. ret = omap_request_dma(OMAP24XX_DMA_NO_DEVICE, "DMA_ISP_HIST",
  412. hist_dma_cb, hist, &hist->dma_ch);
  413. if (ret) {
  414. if (HIST_CONFIG_DMA)
  415. dev_warn(isp->dev, "hist: DMA request channel failed. "
  416. "Using PIO only.\n");
  417. hist->dma_ch = -1;
  418. } else {
  419. dev_dbg(isp->dev, "hist: DMA channel = %d\n", hist->dma_ch);
  420. hist_dma_config(hist);
  421. omap_enable_dma_irq(hist->dma_ch, OMAP_DMA_BLOCK_IRQ);
  422. }
  423. hist->ops = &hist_ops;
  424. hist->priv = hist_cfg;
  425. hist->event_type = V4L2_EVENT_OMAP3ISP_HIST;
  426. hist->isp = isp;
  427. ret = omap3isp_stat_init(hist, "histogram", &hist_subdev_ops);
  428. if (ret) {
  429. kfree(hist_cfg);
  430. if (HIST_USING_DMA(hist))
  431. omap_free_dma(hist->dma_ch);
  432. }
  433. return ret;
  434. }
  435. /*
  436. * omap3isp_hist_cleanup - Module cleanup.
  437. */
  438. void omap3isp_hist_cleanup(struct isp_device *isp)
  439. {
  440. if (HIST_USING_DMA(&isp->isp_hist))
  441. omap_free_dma(isp->isp_hist.dma_ch);
  442. kfree(isp->isp_hist.priv);
  443. omap3isp_stat_cleanup(&isp->isp_hist);
  444. }