isphist.c 14 KB

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