isphist.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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/slab.h>
  21. #include <linux/uaccess.h>
  22. #include "isp.h"
  23. #include "ispreg.h"
  24. #include "isphist.h"
  25. #define HIST_CONFIG_DMA 1
  26. /*
  27. * hist_reset_mem - clear Histogram memory before start stats engine.
  28. */
  29. static void hist_reset_mem(struct ispstat *hist)
  30. {
  31. struct isp_device *isp = hist->isp;
  32. struct omap3isp_hist_config *conf = hist->priv;
  33. unsigned int i;
  34. isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_HIST, ISPHIST_ADDR);
  35. /*
  36. * By setting it, the histogram internal buffer is being cleared at the
  37. * same time it's being read. This bit must be cleared afterwards.
  38. */
  39. isp_reg_set(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT, ISPHIST_CNT_CLEAR);
  40. /*
  41. * We'll clear 4 words at each iteration for optimization. It avoids
  42. * 3/4 of the jumps. We also know HIST_MEM_SIZE is divisible by 4.
  43. */
  44. for (i = OMAP3ISP_HIST_MEM_SIZE / 4; i > 0; i--) {
  45. isp_reg_readl(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_DATA);
  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. }
  50. isp_reg_clr(isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT, ISPHIST_CNT_CLEAR);
  51. hist->wait_acc_frames = conf->num_acc_frames;
  52. }
  53. /*
  54. * hist_setup_regs - Helper function to update Histogram registers.
  55. */
  56. static void hist_setup_regs(struct ispstat *hist, void *priv)
  57. {
  58. struct isp_device *isp = hist->isp;
  59. struct omap3isp_hist_config *conf = priv;
  60. int c;
  61. u32 cnt;
  62. u32 wb_gain;
  63. u32 reg_hor[OMAP3ISP_HIST_MAX_REGIONS];
  64. u32 reg_ver[OMAP3ISP_HIST_MAX_REGIONS];
  65. if (!hist->update || hist->state == ISPSTAT_DISABLED ||
  66. hist->state == ISPSTAT_DISABLING)
  67. return;
  68. cnt = conf->cfa << ISPHIST_CNT_CFA_SHIFT;
  69. wb_gain = conf->wg[0] << ISPHIST_WB_GAIN_WG00_SHIFT;
  70. wb_gain |= conf->wg[1] << ISPHIST_WB_GAIN_WG01_SHIFT;
  71. wb_gain |= conf->wg[2] << ISPHIST_WB_GAIN_WG02_SHIFT;
  72. if (conf->cfa == OMAP3ISP_HIST_CFA_BAYER)
  73. wb_gain |= conf->wg[3] << ISPHIST_WB_GAIN_WG03_SHIFT;
  74. /* Regions size and position */
  75. for (c = 0; c < OMAP3ISP_HIST_MAX_REGIONS; c++) {
  76. if (c < conf->num_regions) {
  77. reg_hor[c] = (conf->region[c].h_start <<
  78. ISPHIST_REG_START_SHIFT)
  79. | (conf->region[c].h_end <<
  80. ISPHIST_REG_END_SHIFT);
  81. reg_ver[c] = (conf->region[c].v_start <<
  82. ISPHIST_REG_START_SHIFT)
  83. | (conf->region[c].v_end <<
  84. ISPHIST_REG_END_SHIFT);
  85. } else {
  86. reg_hor[c] = 0;
  87. reg_ver[c] = 0;
  88. }
  89. }
  90. cnt |= conf->hist_bins << ISPHIST_CNT_BINS_SHIFT;
  91. switch (conf->hist_bins) {
  92. case OMAP3ISP_HIST_BINS_256:
  93. cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 8) <<
  94. ISPHIST_CNT_SHIFT_SHIFT;
  95. break;
  96. case OMAP3ISP_HIST_BINS_128:
  97. cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 7) <<
  98. ISPHIST_CNT_SHIFT_SHIFT;
  99. break;
  100. case OMAP3ISP_HIST_BINS_64:
  101. cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 6) <<
  102. ISPHIST_CNT_SHIFT_SHIFT;
  103. break;
  104. default: /* OMAP3ISP_HIST_BINS_32 */
  105. cnt |= (ISPHIST_IN_BIT_WIDTH_CCDC - 5) <<
  106. ISPHIST_CNT_SHIFT_SHIFT;
  107. break;
  108. }
  109. hist_reset_mem(hist);
  110. isp_reg_writel(isp, cnt, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT);
  111. isp_reg_writel(isp, wb_gain, OMAP3_ISP_IOMEM_HIST, ISPHIST_WB_GAIN);
  112. isp_reg_writel(isp, reg_hor[0], OMAP3_ISP_IOMEM_HIST, ISPHIST_R0_HORZ);
  113. isp_reg_writel(isp, reg_ver[0], OMAP3_ISP_IOMEM_HIST, ISPHIST_R0_VERT);
  114. isp_reg_writel(isp, reg_hor[1], OMAP3_ISP_IOMEM_HIST, ISPHIST_R1_HORZ);
  115. isp_reg_writel(isp, reg_ver[1], OMAP3_ISP_IOMEM_HIST, ISPHIST_R1_VERT);
  116. isp_reg_writel(isp, reg_hor[2], OMAP3_ISP_IOMEM_HIST, ISPHIST_R2_HORZ);
  117. isp_reg_writel(isp, reg_ver[2], OMAP3_ISP_IOMEM_HIST, ISPHIST_R2_VERT);
  118. isp_reg_writel(isp, reg_hor[3], OMAP3_ISP_IOMEM_HIST, ISPHIST_R3_HORZ);
  119. isp_reg_writel(isp, reg_ver[3], OMAP3_ISP_IOMEM_HIST, ISPHIST_R3_VERT);
  120. hist->update = 0;
  121. hist->config_counter += hist->inc_config;
  122. hist->inc_config = 0;
  123. hist->buf_size = conf->buf_size;
  124. }
  125. static void hist_enable(struct ispstat *hist, int enable)
  126. {
  127. if (enable) {
  128. isp_reg_set(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_PCR,
  129. ISPHIST_PCR_ENABLE);
  130. omap3isp_subclk_enable(hist->isp, OMAP3_ISP_SUBCLK_HIST);
  131. } else {
  132. isp_reg_clr(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_PCR,
  133. ISPHIST_PCR_ENABLE);
  134. omap3isp_subclk_disable(hist->isp, OMAP3_ISP_SUBCLK_HIST);
  135. }
  136. }
  137. static int hist_busy(struct ispstat *hist)
  138. {
  139. return isp_reg_readl(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_PCR)
  140. & ISPHIST_PCR_BUSY;
  141. }
  142. static void hist_dma_cb(void *data)
  143. {
  144. struct ispstat *hist = data;
  145. /* FIXME: The DMA engine API can't report transfer errors :-/ */
  146. isp_reg_clr(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT,
  147. ISPHIST_CNT_CLEAR);
  148. omap3isp_stat_dma_isr(hist);
  149. if (hist->state != ISPSTAT_DISABLED)
  150. omap3isp_hist_dma_done(hist->isp);
  151. }
  152. static int hist_buf_dma(struct ispstat *hist)
  153. {
  154. dma_addr_t dma_addr = hist->active_buf->dma_addr;
  155. struct dma_async_tx_descriptor *tx;
  156. struct dma_slave_config cfg;
  157. dma_cookie_t cookie;
  158. int ret;
  159. if (unlikely(!dma_addr)) {
  160. dev_dbg(hist->isp->dev, "hist: invalid DMA buffer address\n");
  161. goto error;
  162. }
  163. isp_reg_writel(hist->isp, 0, OMAP3_ISP_IOMEM_HIST, ISPHIST_ADDR);
  164. isp_reg_set(hist->isp, OMAP3_ISP_IOMEM_HIST, ISPHIST_CNT,
  165. ISPHIST_CNT_CLEAR);
  166. omap3isp_flush(hist->isp);
  167. memset(&cfg, 0, sizeof(cfg));
  168. cfg.src_addr = hist->isp->mmio_hist_base_phys + ISPHIST_DATA;
  169. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  170. cfg.src_maxburst = hist->buf_size / 4;
  171. ret = dmaengine_slave_config(hist->dma_ch, &cfg);
  172. if (ret < 0) {
  173. dev_dbg(hist->isp->dev,
  174. "hist: DMA slave configuration failed\n");
  175. goto error;
  176. }
  177. tx = dmaengine_prep_slave_single(hist->dma_ch, dma_addr,
  178. hist->buf_size, DMA_DEV_TO_MEM,
  179. DMA_CTRL_ACK);
  180. if (tx == NULL) {
  181. dev_dbg(hist->isp->dev,
  182. "hist: DMA slave preparation failed\n");
  183. goto error;
  184. }
  185. tx->callback = hist_dma_cb;
  186. tx->callback_param = hist;
  187. cookie = tx->tx_submit(tx);
  188. if (dma_submit_error(cookie)) {
  189. dev_dbg(hist->isp->dev, "hist: DMA submission failed\n");
  190. goto error;
  191. }
  192. dma_async_issue_pending(hist->dma_ch);
  193. return STAT_BUF_WAITING_DMA;
  194. error:
  195. hist_reset_mem(hist);
  196. return STAT_NO_BUF;
  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->dma_ch)
  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. * @new_conf: 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 enough */
  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_REQ_TIME32:
  372. return omap3isp_stat_request_statistics_time32(stat, arg);
  373. case VIDIOC_OMAP3ISP_STAT_EN: {
  374. int *en = arg;
  375. return omap3isp_stat_enable(stat, !!*en);
  376. }
  377. }
  378. return -ENOIOCTLCMD;
  379. }
  380. static const struct ispstat_ops hist_ops = {
  381. .validate_params = hist_validate_params,
  382. .set_params = hist_set_params,
  383. .setup_regs = hist_setup_regs,
  384. .enable = hist_enable,
  385. .busy = hist_busy,
  386. .buf_process = hist_buf_process,
  387. };
  388. static const struct v4l2_subdev_core_ops hist_subdev_core_ops = {
  389. .ioctl = hist_ioctl,
  390. .subscribe_event = omap3isp_stat_subscribe_event,
  391. .unsubscribe_event = omap3isp_stat_unsubscribe_event,
  392. };
  393. static const struct v4l2_subdev_video_ops hist_subdev_video_ops = {
  394. .s_stream = omap3isp_stat_s_stream,
  395. };
  396. static const struct v4l2_subdev_ops hist_subdev_ops = {
  397. .core = &hist_subdev_core_ops,
  398. .video = &hist_subdev_video_ops,
  399. };
  400. /*
  401. * omap3isp_hist_init - Module Initialization.
  402. */
  403. int omap3isp_hist_init(struct isp_device *isp)
  404. {
  405. struct ispstat *hist = &isp->isp_hist;
  406. struct omap3isp_hist_config *hist_cfg;
  407. int ret = -1;
  408. hist_cfg = devm_kzalloc(isp->dev, sizeof(*hist_cfg), GFP_KERNEL);
  409. if (hist_cfg == NULL)
  410. return -ENOMEM;
  411. hist->isp = isp;
  412. if (HIST_CONFIG_DMA) {
  413. dma_cap_mask_t mask;
  414. /*
  415. * We need slave capable channel without DMA request line for
  416. * reading out the data.
  417. * For this we can use dma_request_chan_by_mask() as we are
  418. * happy with any channel as long as it is capable of slave
  419. * configuration.
  420. */
  421. dma_cap_zero(mask);
  422. dma_cap_set(DMA_SLAVE, mask);
  423. hist->dma_ch = dma_request_chan_by_mask(&mask);
  424. if (IS_ERR(hist->dma_ch)) {
  425. ret = PTR_ERR(hist->dma_ch);
  426. if (ret == -EPROBE_DEFER)
  427. return ret;
  428. hist->dma_ch = NULL;
  429. dev_warn(isp->dev,
  430. "hist: DMA channel request failed, using PIO\n");
  431. } else {
  432. dev_dbg(isp->dev, "hist: using DMA channel %s\n",
  433. dma_chan_name(hist->dma_ch));
  434. }
  435. }
  436. hist->ops = &hist_ops;
  437. hist->priv = hist_cfg;
  438. hist->event_type = V4L2_EVENT_OMAP3ISP_HIST;
  439. ret = omap3isp_stat_init(hist, "histogram", &hist_subdev_ops);
  440. if (ret) {
  441. if (hist->dma_ch)
  442. dma_release_channel(hist->dma_ch);
  443. }
  444. return ret;
  445. }
  446. /*
  447. * omap3isp_hist_cleanup - Module cleanup.
  448. */
  449. void omap3isp_hist_cleanup(struct isp_device *isp)
  450. {
  451. struct ispstat *hist = &isp->isp_hist;
  452. if (hist->dma_ch)
  453. dma_release_channel(hist->dma_ch);
  454. omap3isp_stat_cleanup(hist);
  455. }