isphist.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/dmaengine.h>
  20. #include <linux/omap-dmaengine.h>
  21. #include <linux/slab.h>
  22. #include <linux/uaccess.h>
  23. #include "isp.h"
  24. #include "ispreg.h"
  25. #include "isphist.h"
  26. #define HIST_CONFIG_DMA 1
  27. /*
  28. * hist_reset_mem - clear Histogram memory before start stats engine.
  29. */
  30. static void hist_reset_mem(struct ispstat *hist)
  31. {
  32. struct isp_device *isp = hist->isp;
  33. struct omap3isp_hist_config *conf = hist->priv;
  34. unsigned int i;
  35. isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_HIST, ISPHIST_ADDR);
  36. /*
  37. * By setting it, the histogram internal buffer is being cleared at the
  38. * same time it's being read. This bit must be cleared afterwards.
  39. */
  40. isp_reg_set(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT, ISPHIST_CNT_CLEAR);
  41. /*
  42. * We'll clear 4 words at each iteration for optimization. It avoids
  43. * 3/4 of the jumps. We also know HIST_MEM_SIZE is divisible by 4.
  44. */
  45. for (i = OMAP3ISP_HIST_MEM_SIZE / 4; i > 0; i--) {
  46. isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  47. isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  48. isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  49. isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  50. }
  51. isp_reg_clr(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT, ISPHIST_CNT_CLEAR);
  52. hist->wait_acc_frames = conf->num_acc_frames;
  53. }
  54. /*
  55. * hist_setup_regs - Helper function to update Histogram registers.
  56. */
  57. static void hist_setup_regs(struct ispstat *hist, void *priv)
  58. {
  59. struct isp_device *isp = hist->isp;
  60. struct omap3isp_hist_config *conf = priv;
  61. int c;
  62. u32 cnt;
  63. u32 wb_gain;
  64. u32 reg_hor[OMAP3ISP_HIST_MAX_REGIONS];
  65. u32 reg_ver[OMAP3ISP_HIST_MAX_REGIONS];
  66. if (!hist->update || hist->state == ISPSTAT_DISABLED ||
  67. hist->state == ISPSTAT_DISABLING)
  68. return;
  69. cnt = conf->cfa << ISPHIST_CNT_CFA_SHIFT;
  70. wb_gain = conf->wg[0] << ISPHIST_WB_GAIN_WG00_SHIFT;
  71. wb_gain |= conf->wg[1] << ISPHIST_WB_GAIN_WG01_SHIFT;
  72. wb_gain |= conf->wg[2] << ISPHIST_WB_GAIN_WG02_SHIFT;
  73. if (conf->cfa == OMAP3ISP_HIST_CFA_BAYER)
  74. wb_gain |= conf->wg[3] << ISPHIST_WB_GAIN_WG03_SHIFT;
  75. /* Regions size and position */
  76. for (c = 0; c < OMAP3ISP_HIST_MAX_REGIONS; c++) {
  77. if (c < conf->num_regions) {
  78. reg_hor[c] = (conf->region[c].h_start <<
  79. ISPHIST_REG_START_SHIFT)
  80. | (conf->region[c].h_end <<
  81. ISPHIST_REG_END_SHIFT);
  82. reg_ver[c] = (conf->region[c].v_start <<
  83. ISPHIST_REG_START_SHIFT)
  84. | (conf->region[c].v_end <<
  85. ISPHIST_REG_END_SHIFT);
  86. } else {
  87. reg_hor[c] = 0;
  88. reg_ver[c] = 0;
  89. }
  90. }
  91. cnt |= conf->hist_bins << ISPHIST_CNT_BINS_SHIFT;
  92. switch (conf->hist_bins) {
  93. case OMAP3ISP_HIST_BINS_256:
  94. cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 8) <<
  95. ISPHIST_CNT_SHIFT_SHIFT;
  96. break;
  97. case OMAP3ISP_HIST_BINS_128:
  98. cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 7) <<
  99. ISPHIST_CNT_SHIFT_SHIFT;
  100. break;
  101. case OMAP3ISP_HIST_BINS_64:
  102. cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 6) <<
  103. ISPHIST_CNT_SHIFT_SHIFT;
  104. break;
  105. default: /* OMAP3ISP_HIST_BINS_32 */
  106. cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 5) <<
  107. ISPHIST_CNT_SHIFT_SHIFT;
  108. break;
  109. }
  110. hist_reset_mem(hist);
  111. isp_reg_writel(isp, cnt, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT);
  112. isp_reg_writel(isp, wb_gain, OMAP3_ISP_IOMEM_HIST, ISPHIST_WB_GAIN);
  113. isp_reg_writel(isp, reg_hor[0], OMAP3_ISP_IOMEM_HIST, ISPHIST_R0_HORZ);
  114. isp_reg_writel(isp, reg_ver[0], OMAP3_ISP_IOMEM_HIST, ISPHIST_R0_VERT);
  115. isp_reg_writel(isp, reg_hor[1], OMAP3_ISP_IOMEM_HIST, ISPHIST_R1_HORZ);
  116. isp_reg_writel(isp, reg_ver[1], OMAP3_ISP_IOMEM_HIST, ISPHIST_R1_VERT);
  117. isp_reg_writel(isp, reg_hor[2], OMAP3_ISP_IOMEM_HIST, ISPHIST_R2_HORZ);
  118. isp_reg_writel(isp, reg_ver[2], OMAP3_ISP_IOMEM_HIST, ISPHIST_R2_VERT);
  119. isp_reg_writel(isp, reg_hor[3], OMAP3_ISP_IOMEM_HIST, ISPHIST_R3_HORZ);
  120. isp_reg_writel(isp, reg_ver[3], OMAP3_ISP_IOMEM_HIST, ISPHIST_R3_VERT);
  121. hist->update = 0;
  122. hist->config_counter += hist->inc_config;
  123. hist->inc_config = 0;
  124. hist->buf_size = conf->buf_size;
  125. }
  126. static void hist_enable(struct ispstat *hist, int enable)
  127. {
  128. if (enable) {
  129. isp_reg_set(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_PCR,
  130. ISPHIST_PCR_ENABLE);
  131. omap3isp_subclk_enable(hist->isp, OMAP3_ISP_SUBCLK_HIST);
  132. } else {
  133. isp_reg_clr(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_PCR,
  134. ISPHIST_PCR_ENABLE);
  135. omap3isp_subclk_disable(hist->isp, OMAP3_ISP_SUBCLK_HIST);
  136. }
  137. }
  138. static int hist_busy(struct ispstat *hist)
  139. {
  140. return isp_reg_readl(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_PCR)
  141. & ISPHIST_PCR_BUSY;
  142. }
  143. static void hist_dma_cb(void *data)
  144. {
  145. struct ispstat *hist = data;
  146. /* FIXME: The DMA engine API can't report transfer errors :-/ */
  147. isp_reg_clr(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT,
  148. ISPHIST_CNT_CLEAR);
  149. omap3isp_stat_dma_isr(hist);
  150. if (hist->state != ISPSTAT_DISABLED)
  151. omap3isp_hist_dma_done(hist->isp);
  152. }
  153. static int hist_buf_dma(struct ispstat *hist)
  154. {
  155. dma_addr_t dma_addr = hist->active_buf->dma_addr;
  156. struct dma_async_tx_descriptor *tx;
  157. struct dma_slave_config cfg;
  158. dma_cookie_t cookie;
  159. int ret;
  160. if (unlikely(!dma_addr)) {
  161. dev_dbg(hist->isp->dev, "hist: invalid DMA buffer address\n");
  162. goto error;
  163. }
  164. isp_reg_writel(hist->isp, 0, OMAP3_ISP_IOMEM_HIST, ISPHIST_ADDR);
  165. isp_reg_set(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT,
  166. ISPHIST_CNT_CLEAR);
  167. omap3isp_flush(hist->isp);
  168. memset(&cfg, 0, sizeof(cfg));
  169. cfg.src_addr = hist->isp->mmio_hist_base_phys + ISPHIST_DATA;
  170. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  171. cfg.src_maxburst = hist->buf_size / 4;
  172. ret = dmaengine_slave_config(hist->dma_ch, &cfg);
  173. if (ret < 0) {
  174. dev_dbg(hist->isp->dev,
  175. "hist: DMA slave configuration failed\n");
  176. goto error;
  177. }
  178. tx = dmaengine_prep_slave_single(hist->dma_ch, dma_addr,
  179. hist->buf_size, DMA_DEV_TO_MEM,
  180. DMA_CTRL_ACK);
  181. if (tx == NULL) {
  182. dev_dbg(hist->isp->dev,
  183. "hist: DMA slave preparation failed\n");
  184. goto error;
  185. }
  186. tx->callback = hist_dma_cb;
  187. tx->callback_param = hist;
  188. cookie = tx->tx_submit(tx);
  189. if (dma_submit_error(cookie)) {
  190. dev_dbg(hist->isp->dev, "hist: DMA submission failed\n");
  191. goto error;
  192. }
  193. dma_async_issue_pending(hist->dma_ch);
  194. return STAT_BUF_WAITING_DMA;
  195. error:
  196. hist_reset_mem(hist);
  197. return STAT_NO_BUF;
  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->dma_ch)
  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. * @new_conf: 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 enough */
  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. struct platform_device *pdev = to_platform_device(isp->dev);
  413. struct resource *res;
  414. unsigned int sig = 0;
  415. dma_cap_mask_t mask;
  416. dma_cap_zero(mask);
  417. dma_cap_set(DMA_SLAVE, mask);
  418. res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  419. "hist");
  420. if (res)
  421. sig = res->start;
  422. hist->dma_ch = dma_request_slave_channel_compat(mask,
  423. omap_dma_filter_fn, &sig, isp->dev, "hist");
  424. if (!hist->dma_ch)
  425. dev_warn(isp->dev,
  426. "hist: DMA channel request failed, using PIO\n");
  427. else
  428. dev_dbg(isp->dev, "hist: using DMA channel %s\n",
  429. dma_chan_name(hist->dma_ch));
  430. }
  431. hist->ops = &hist_ops;
  432. hist->priv = hist_cfg;
  433. hist->event_type = V4L2_EVENT_OMAP3ISP_HIST;
  434. ret = omap3isp_stat_init(hist, "histogram", &hist_subdev_ops);
  435. if (ret) {
  436. if (hist->dma_ch)
  437. dma_release_channel(hist->dma_ch);
  438. }
  439. return ret;
  440. }
  441. /*
  442. * omap3isp_hist_cleanup - Module cleanup.
  443. */
  444. void omap3isp_hist_cleanup(struct isp_device *isp)
  445. {
  446. struct ispstat *hist = &isp->isp_hist;
  447. if (hist->dma_ch)
  448. dma_release_channel(hist->dma_ch);
  449. omap3isp_stat_cleanup(hist);
  450. }