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