microchip-isc-base.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Microchip Image Sensor Controller (ISC) common driver base
  4. *
  5. * Copyright (C) 2016-2019 Microchip Technology, Inc.
  6. *
  7. * Author: Songjun Wu
  8. * Author: Eugen Hristev <eugen.hristev@microchip.com>
  9. *
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/math64.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_graph.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/regmap.h>
  20. #include <linux/videodev2.h>
  21. #include <linux/atmel-isc-media.h>
  22. #include <media/v4l2-ctrls.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-event.h>
  25. #include <media/v4l2-image-sizes.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include <media/v4l2-fwnode.h>
  28. #include <media/v4l2-subdev.h>
  29. #include <media/videobuf2-dma-contig.h>
  30. #include "microchip-isc-regs.h"
  31. #include "microchip-isc.h"
  32. #define ISC_IS_FORMAT_RAW(mbus_code) \
  33. (((mbus_code) & 0xf000) == 0x3000)
  34. #define ISC_IS_FORMAT_GREY(mbus_code) \
  35. (((mbus_code) == MEDIA_BUS_FMT_Y10_1X10) | \
  36. (((mbus_code) == MEDIA_BUS_FMT_Y8_1X8)))
  37. static inline void isc_update_v4l2_ctrls(struct isc_device *isc)
  38. {
  39. struct isc_ctrls *ctrls = &isc->ctrls;
  40. /* In here we set the v4l2 controls w.r.t. our pipeline config */
  41. v4l2_ctrl_s_ctrl(isc->r_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_R]);
  42. v4l2_ctrl_s_ctrl(isc->b_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_B]);
  43. v4l2_ctrl_s_ctrl(isc->gr_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_GR]);
  44. v4l2_ctrl_s_ctrl(isc->gb_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_GB]);
  45. v4l2_ctrl_s_ctrl(isc->r_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_R]);
  46. v4l2_ctrl_s_ctrl(isc->b_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_B]);
  47. v4l2_ctrl_s_ctrl(isc->gr_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_GR]);
  48. v4l2_ctrl_s_ctrl(isc->gb_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_GB]);
  49. }
  50. static inline void isc_update_awb_ctrls(struct isc_device *isc)
  51. {
  52. struct isc_ctrls *ctrls = &isc->ctrls;
  53. /* In here we set our actual hw pipeline config */
  54. regmap_write(isc->regmap, ISC_WB_O_RGR,
  55. ((ctrls->offset[ISC_HIS_CFG_MODE_R])) |
  56. ((ctrls->offset[ISC_HIS_CFG_MODE_GR]) << 16));
  57. regmap_write(isc->regmap, ISC_WB_O_BGB,
  58. ((ctrls->offset[ISC_HIS_CFG_MODE_B])) |
  59. ((ctrls->offset[ISC_HIS_CFG_MODE_GB]) << 16));
  60. regmap_write(isc->regmap, ISC_WB_G_RGR,
  61. ctrls->gain[ISC_HIS_CFG_MODE_R] |
  62. (ctrls->gain[ISC_HIS_CFG_MODE_GR] << 16));
  63. regmap_write(isc->regmap, ISC_WB_G_BGB,
  64. ctrls->gain[ISC_HIS_CFG_MODE_B] |
  65. (ctrls->gain[ISC_HIS_CFG_MODE_GB] << 16));
  66. }
  67. static inline void isc_reset_awb_ctrls(struct isc_device *isc)
  68. {
  69. unsigned int c;
  70. for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
  71. /* gains have a fixed point at 9 decimals */
  72. isc->ctrls.gain[c] = 1 << 9;
  73. /* offsets are in 2's complements */
  74. isc->ctrls.offset[c] = 0;
  75. }
  76. }
  77. static int isc_queue_setup(struct vb2_queue *vq,
  78. unsigned int *nbuffers, unsigned int *nplanes,
  79. unsigned int sizes[], struct device *alloc_devs[])
  80. {
  81. struct isc_device *isc = vb2_get_drv_priv(vq);
  82. unsigned int size = isc->fmt.fmt.pix.sizeimage;
  83. if (*nplanes)
  84. return sizes[0] < size ? -EINVAL : 0;
  85. *nplanes = 1;
  86. sizes[0] = size;
  87. return 0;
  88. }
  89. static int isc_buffer_prepare(struct vb2_buffer *vb)
  90. {
  91. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  92. struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
  93. unsigned long size = isc->fmt.fmt.pix.sizeimage;
  94. if (vb2_plane_size(vb, 0) < size) {
  95. dev_err(isc->dev, "buffer too small (%lu < %lu)\n",
  96. vb2_plane_size(vb, 0), size);
  97. return -EINVAL;
  98. }
  99. vb2_set_plane_payload(vb, 0, size);
  100. vbuf->field = isc->fmt.fmt.pix.field;
  101. return 0;
  102. }
  103. static void isc_crop_pfe(struct isc_device *isc)
  104. {
  105. struct regmap *regmap = isc->regmap;
  106. u32 h, w;
  107. h = isc->fmt.fmt.pix.height;
  108. w = isc->fmt.fmt.pix.width;
  109. /*
  110. * In case the sensor is not RAW, it will output a pixel (12-16 bits)
  111. * with two samples on the ISC Data bus (which is 8-12)
  112. * ISC will count each sample, so, we need to multiply these values
  113. * by two, to get the real number of samples for the required pixels.
  114. */
  115. if (!ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code)) {
  116. h <<= 1;
  117. w <<= 1;
  118. }
  119. /*
  120. * We limit the column/row count that the ISC will output according
  121. * to the configured resolution that we want.
  122. * This will avoid the situation where the sensor is misconfigured,
  123. * sending more data, and the ISC will just take it and DMA to memory,
  124. * causing corruption.
  125. */
  126. regmap_write(regmap, ISC_PFE_CFG1,
  127. (ISC_PFE_CFG1_COLMIN(0) & ISC_PFE_CFG1_COLMIN_MASK) |
  128. (ISC_PFE_CFG1_COLMAX(w - 1) & ISC_PFE_CFG1_COLMAX_MASK));
  129. regmap_write(regmap, ISC_PFE_CFG2,
  130. (ISC_PFE_CFG2_ROWMIN(0) & ISC_PFE_CFG2_ROWMIN_MASK) |
  131. (ISC_PFE_CFG2_ROWMAX(h - 1) & ISC_PFE_CFG2_ROWMAX_MASK));
  132. regmap_update_bits(regmap, ISC_PFE_CFG0,
  133. ISC_PFE_CFG0_COLEN | ISC_PFE_CFG0_ROWEN,
  134. ISC_PFE_CFG0_COLEN | ISC_PFE_CFG0_ROWEN);
  135. }
  136. static void isc_start_dma(struct isc_device *isc)
  137. {
  138. struct regmap *regmap = isc->regmap;
  139. u32 sizeimage = isc->fmt.fmt.pix.sizeimage;
  140. u32 dctrl_dview;
  141. dma_addr_t addr0;
  142. addr0 = vb2_dma_contig_plane_dma_addr(&isc->cur_frm->vb.vb2_buf, 0);
  143. regmap_write(regmap, ISC_DAD0 + isc->offsets.dma, addr0);
  144. switch (isc->config.fourcc) {
  145. case V4L2_PIX_FMT_YUV420:
  146. regmap_write(regmap, ISC_DAD1 + isc->offsets.dma,
  147. addr0 + (sizeimage * 2) / 3);
  148. regmap_write(regmap, ISC_DAD2 + isc->offsets.dma,
  149. addr0 + (sizeimage * 5) / 6);
  150. break;
  151. case V4L2_PIX_FMT_YUV422P:
  152. regmap_write(regmap, ISC_DAD1 + isc->offsets.dma,
  153. addr0 + sizeimage / 2);
  154. regmap_write(regmap, ISC_DAD2 + isc->offsets.dma,
  155. addr0 + (sizeimage * 3) / 4);
  156. break;
  157. default:
  158. break;
  159. }
  160. dctrl_dview = isc->config.dctrl_dview;
  161. regmap_write(regmap, ISC_DCTRL + isc->offsets.dma,
  162. dctrl_dview | ISC_DCTRL_IE_IS);
  163. spin_lock(&isc->awb_lock);
  164. regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_CAPTURE);
  165. spin_unlock(&isc->awb_lock);
  166. }
  167. static void isc_set_pipeline(struct isc_device *isc, u32 pipeline)
  168. {
  169. struct regmap *regmap = isc->regmap;
  170. struct isc_ctrls *ctrls = &isc->ctrls;
  171. u32 val, bay_cfg;
  172. const u32 *gamma;
  173. unsigned int i;
  174. /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
  175. for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
  176. val = pipeline & BIT(i) ? 1 : 0;
  177. regmap_field_write(isc->pipeline[i], val);
  178. }
  179. if (!pipeline)
  180. return;
  181. bay_cfg = isc->config.sd_format->cfa_baycfg;
  182. regmap_write(regmap, ISC_WB_CFG, bay_cfg);
  183. isc_update_awb_ctrls(isc);
  184. isc_update_v4l2_ctrls(isc);
  185. regmap_write(regmap, ISC_CFA_CFG, bay_cfg | ISC_CFA_CFG_EITPOL);
  186. gamma = &isc->gamma_table[ctrls->gamma_index][0];
  187. regmap_bulk_write(regmap, ISC_GAM_BENTRY, gamma, GAMMA_ENTRIES);
  188. regmap_bulk_write(regmap, ISC_GAM_GENTRY, gamma, GAMMA_ENTRIES);
  189. regmap_bulk_write(regmap, ISC_GAM_RENTRY, gamma, GAMMA_ENTRIES);
  190. isc->config_dpc(isc);
  191. isc->config_csc(isc);
  192. isc->config_cbc(isc);
  193. isc->config_cc(isc);
  194. isc->config_gam(isc);
  195. }
  196. static int isc_update_profile(struct isc_device *isc)
  197. {
  198. struct regmap *regmap = isc->regmap;
  199. u32 sr;
  200. int counter = 100;
  201. regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_UPPRO);
  202. regmap_read(regmap, ISC_CTRLSR, &sr);
  203. while ((sr & ISC_CTRL_UPPRO) && counter--) {
  204. usleep_range(1000, 2000);
  205. regmap_read(regmap, ISC_CTRLSR, &sr);
  206. }
  207. if (counter < 0) {
  208. v4l2_warn(&isc->v4l2_dev, "Time out to update profile\n");
  209. return -ETIMEDOUT;
  210. }
  211. return 0;
  212. }
  213. static void isc_set_histogram(struct isc_device *isc, bool enable)
  214. {
  215. struct regmap *regmap = isc->regmap;
  216. struct isc_ctrls *ctrls = &isc->ctrls;
  217. if (enable) {
  218. regmap_write(regmap, ISC_HIS_CFG + isc->offsets.his,
  219. ISC_HIS_CFG_MODE_GR |
  220. (isc->config.sd_format->cfa_baycfg
  221. << ISC_HIS_CFG_BAYSEL_SHIFT) |
  222. ISC_HIS_CFG_RAR);
  223. regmap_write(regmap, ISC_HIS_CTRL + isc->offsets.his,
  224. ISC_HIS_CTRL_EN);
  225. regmap_write(regmap, ISC_INTEN, ISC_INT_HISDONE);
  226. ctrls->hist_id = ISC_HIS_CFG_MODE_GR;
  227. isc_update_profile(isc);
  228. regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
  229. ctrls->hist_stat = HIST_ENABLED;
  230. } else {
  231. regmap_write(regmap, ISC_INTDIS, ISC_INT_HISDONE);
  232. regmap_write(regmap, ISC_HIS_CTRL + isc->offsets.his,
  233. ISC_HIS_CTRL_DIS);
  234. ctrls->hist_stat = HIST_DISABLED;
  235. }
  236. }
  237. static int isc_configure(struct isc_device *isc)
  238. {
  239. struct regmap *regmap = isc->regmap;
  240. u32 pfe_cfg0, dcfg, mask, pipeline;
  241. struct isc_subdev_entity *subdev = isc->current_subdev;
  242. pfe_cfg0 = isc->config.sd_format->pfe_cfg0_bps;
  243. pipeline = isc->config.bits_pipeline;
  244. dcfg = isc->config.dcfg_imode | isc->dcfg;
  245. pfe_cfg0 |= subdev->pfe_cfg0 | ISC_PFE_CFG0_MODE_PROGRESSIVE;
  246. mask = ISC_PFE_CFG0_BPS_MASK | ISC_PFE_CFG0_HPOL_LOW |
  247. ISC_PFE_CFG0_VPOL_LOW | ISC_PFE_CFG0_PPOL_LOW |
  248. ISC_PFE_CFG0_MODE_MASK | ISC_PFE_CFG0_CCIR_CRC |
  249. ISC_PFE_CFG0_CCIR656 | ISC_PFE_CFG0_MIPI;
  250. regmap_update_bits(regmap, ISC_PFE_CFG0, mask, pfe_cfg0);
  251. isc->config_rlp(isc);
  252. regmap_write(regmap, ISC_DCFG + isc->offsets.dma, dcfg);
  253. /* Set the pipeline */
  254. isc_set_pipeline(isc, pipeline);
  255. /*
  256. * The current implemented histogram is available for RAW R, B, GB, GR
  257. * channels. We need to check if sensor is outputting RAW BAYER
  258. */
  259. if (isc->ctrls.awb &&
  260. ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
  261. isc_set_histogram(isc, true);
  262. else
  263. isc_set_histogram(isc, false);
  264. /* Update profile */
  265. return isc_update_profile(isc);
  266. }
  267. static int isc_prepare_streaming(struct vb2_queue *vq)
  268. {
  269. struct isc_device *isc = vb2_get_drv_priv(vq);
  270. return media_pipeline_start(isc->video_dev.entity.pads, &isc->mpipe);
  271. }
  272. static int isc_start_streaming(struct vb2_queue *vq, unsigned int count)
  273. {
  274. struct isc_device *isc = vb2_get_drv_priv(vq);
  275. struct regmap *regmap = isc->regmap;
  276. struct isc_buffer *buf;
  277. unsigned long flags;
  278. int ret;
  279. /* Enable stream on the sub device */
  280. ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 1);
  281. if (ret && ret != -ENOIOCTLCMD) {
  282. dev_err(isc->dev, "stream on failed in subdev %d\n", ret);
  283. goto err_start_stream;
  284. }
  285. ret = pm_runtime_resume_and_get(isc->dev);
  286. if (ret < 0) {
  287. dev_err(isc->dev, "RPM resume failed in subdev %d\n",
  288. ret);
  289. goto err_pm_get;
  290. }
  291. ret = isc_configure(isc);
  292. if (unlikely(ret))
  293. goto err_configure;
  294. /* Enable DMA interrupt */
  295. regmap_write(regmap, ISC_INTEN, ISC_INT_DDONE);
  296. spin_lock_irqsave(&isc->dma_queue_lock, flags);
  297. isc->sequence = 0;
  298. isc->stop = false;
  299. reinit_completion(&isc->comp);
  300. isc->cur_frm = list_first_entry(&isc->dma_queue,
  301. struct isc_buffer, list);
  302. list_del(&isc->cur_frm->list);
  303. isc_crop_pfe(isc);
  304. isc_start_dma(isc);
  305. spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
  306. /* if we streaming from RAW, we can do one-shot white balance adj */
  307. if (ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
  308. v4l2_ctrl_activate(isc->do_wb_ctrl, true);
  309. return 0;
  310. err_configure:
  311. pm_runtime_put_sync(isc->dev);
  312. err_pm_get:
  313. v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
  314. err_start_stream:
  315. spin_lock_irqsave(&isc->dma_queue_lock, flags);
  316. list_for_each_entry(buf, &isc->dma_queue, list)
  317. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  318. INIT_LIST_HEAD(&isc->dma_queue);
  319. spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
  320. return ret;
  321. }
  322. static void isc_unprepare_streaming(struct vb2_queue *vq)
  323. {
  324. struct isc_device *isc = vb2_get_drv_priv(vq);
  325. /* Stop media pipeline */
  326. media_pipeline_stop(isc->video_dev.entity.pads);
  327. }
  328. static void isc_stop_streaming(struct vb2_queue *vq)
  329. {
  330. struct isc_device *isc = vb2_get_drv_priv(vq);
  331. unsigned long flags;
  332. struct isc_buffer *buf;
  333. int ret;
  334. mutex_lock(&isc->awb_mutex);
  335. v4l2_ctrl_activate(isc->do_wb_ctrl, false);
  336. isc->stop = true;
  337. /* Wait until the end of the current frame */
  338. if (isc->cur_frm && !wait_for_completion_timeout(&isc->comp, 5 * HZ))
  339. dev_err(isc->dev, "Timeout waiting for end of the capture\n");
  340. mutex_unlock(&isc->awb_mutex);
  341. /* Disable DMA interrupt */
  342. regmap_write(isc->regmap, ISC_INTDIS, ISC_INT_DDONE);
  343. pm_runtime_put_sync(isc->dev);
  344. /* Disable stream on the sub device */
  345. ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
  346. if (ret && ret != -ENOIOCTLCMD)
  347. dev_err(isc->dev, "stream off failed in subdev\n");
  348. /* Release all active buffers */
  349. spin_lock_irqsave(&isc->dma_queue_lock, flags);
  350. if (unlikely(isc->cur_frm)) {
  351. vb2_buffer_done(&isc->cur_frm->vb.vb2_buf,
  352. VB2_BUF_STATE_ERROR);
  353. isc->cur_frm = NULL;
  354. }
  355. list_for_each_entry(buf, &isc->dma_queue, list)
  356. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  357. INIT_LIST_HEAD(&isc->dma_queue);
  358. spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
  359. }
  360. static void isc_buffer_queue(struct vb2_buffer *vb)
  361. {
  362. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  363. struct isc_buffer *buf = container_of(vbuf, struct isc_buffer, vb);
  364. struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
  365. unsigned long flags;
  366. spin_lock_irqsave(&isc->dma_queue_lock, flags);
  367. if (!isc->cur_frm && list_empty(&isc->dma_queue) &&
  368. vb2_start_streaming_called(vb->vb2_queue)) {
  369. isc->cur_frm = buf;
  370. isc_start_dma(isc);
  371. } else {
  372. list_add_tail(&buf->list, &isc->dma_queue);
  373. }
  374. spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
  375. }
  376. static const struct vb2_ops isc_vb2_ops = {
  377. .queue_setup = isc_queue_setup,
  378. .wait_prepare = vb2_ops_wait_prepare,
  379. .wait_finish = vb2_ops_wait_finish,
  380. .buf_prepare = isc_buffer_prepare,
  381. .start_streaming = isc_start_streaming,
  382. .stop_streaming = isc_stop_streaming,
  383. .buf_queue = isc_buffer_queue,
  384. .prepare_streaming = isc_prepare_streaming,
  385. .unprepare_streaming = isc_unprepare_streaming,
  386. };
  387. static int isc_querycap(struct file *file, void *priv,
  388. struct v4l2_capability *cap)
  389. {
  390. strscpy(cap->driver, "microchip-isc", sizeof(cap->driver));
  391. strscpy(cap->card, "Microchip Image Sensor Controller", sizeof(cap->card));
  392. return 0;
  393. }
  394. static int isc_enum_fmt_vid_cap(struct file *file, void *priv,
  395. struct v4l2_fmtdesc *f)
  396. {
  397. struct isc_device *isc = video_drvdata(file);
  398. u32 index = f->index;
  399. u32 i, supported_index = 0;
  400. struct isc_format *fmt;
  401. /*
  402. * If we are not asked a specific mbus_code, we have to report all
  403. * the formats that we can output.
  404. */
  405. if (!f->mbus_code) {
  406. if (index >= isc->controller_formats_size)
  407. return -EINVAL;
  408. f->pixelformat = isc->controller_formats[index].fourcc;
  409. return 0;
  410. }
  411. /*
  412. * If a specific mbus_code is requested, check if we support
  413. * this mbus_code as input for the ISC.
  414. * If it's supported, then we report the corresponding pixelformat
  415. * as first possible option for the ISC.
  416. * E.g. mbus MEDIA_BUS_FMT_YUYV8_2X8 and report
  417. * 'YUYV' (YUYV 4:2:2)
  418. */
  419. fmt = isc_find_format_by_code(isc, f->mbus_code, &i);
  420. if (!fmt)
  421. return -EINVAL;
  422. if (!index) {
  423. f->pixelformat = fmt->fourcc;
  424. return 0;
  425. }
  426. supported_index++;
  427. /* If the index is not raw, we don't have anymore formats to report */
  428. if (!ISC_IS_FORMAT_RAW(f->mbus_code))
  429. return -EINVAL;
  430. /*
  431. * We are asked for a specific mbus code, which is raw.
  432. * We have to search through the formats we can convert to.
  433. * We have to skip the raw formats, we cannot convert to raw.
  434. * E.g. 'AR12' (16-bit ARGB 4-4-4-4), 'AR15' (16-bit ARGB 1-5-5-5), etc.
  435. */
  436. for (i = 0; i < isc->controller_formats_size; i++) {
  437. if (isc->controller_formats[i].raw)
  438. continue;
  439. if (index == supported_index) {
  440. f->pixelformat = isc->controller_formats[i].fourcc;
  441. return 0;
  442. }
  443. supported_index++;
  444. }
  445. return -EINVAL;
  446. }
  447. static int isc_g_fmt_vid_cap(struct file *file, void *priv,
  448. struct v4l2_format *fmt)
  449. {
  450. struct isc_device *isc = video_drvdata(file);
  451. *fmt = isc->fmt;
  452. return 0;
  453. }
  454. /*
  455. * Checks the current configured format, if ISC can output it,
  456. * considering which type of format the ISC receives from the sensor
  457. */
  458. static int isc_try_validate_formats(struct isc_device *isc)
  459. {
  460. int ret;
  461. bool bayer = false, yuv = false, rgb = false, grey = false;
  462. /* all formats supported by the RLP module are OK */
  463. switch (isc->try_config.fourcc) {
  464. case V4L2_PIX_FMT_SBGGR8:
  465. case V4L2_PIX_FMT_SGBRG8:
  466. case V4L2_PIX_FMT_SGRBG8:
  467. case V4L2_PIX_FMT_SRGGB8:
  468. case V4L2_PIX_FMT_SBGGR10:
  469. case V4L2_PIX_FMT_SGBRG10:
  470. case V4L2_PIX_FMT_SGRBG10:
  471. case V4L2_PIX_FMT_SRGGB10:
  472. case V4L2_PIX_FMT_SBGGR12:
  473. case V4L2_PIX_FMT_SGBRG12:
  474. case V4L2_PIX_FMT_SGRBG12:
  475. case V4L2_PIX_FMT_SRGGB12:
  476. ret = 0;
  477. bayer = true;
  478. break;
  479. case V4L2_PIX_FMT_YUV420:
  480. case V4L2_PIX_FMT_YUV422P:
  481. case V4L2_PIX_FMT_YUYV:
  482. case V4L2_PIX_FMT_UYVY:
  483. case V4L2_PIX_FMT_VYUY:
  484. ret = 0;
  485. yuv = true;
  486. break;
  487. case V4L2_PIX_FMT_RGB565:
  488. case V4L2_PIX_FMT_ABGR32:
  489. case V4L2_PIX_FMT_XBGR32:
  490. case V4L2_PIX_FMT_ARGB444:
  491. case V4L2_PIX_FMT_ARGB555:
  492. ret = 0;
  493. rgb = true;
  494. break;
  495. case V4L2_PIX_FMT_GREY:
  496. case V4L2_PIX_FMT_Y10:
  497. case V4L2_PIX_FMT_Y16:
  498. ret = 0;
  499. grey = true;
  500. break;
  501. default:
  502. /* any other different formats are not supported */
  503. dev_err(isc->dev, "Requested unsupported format.\n");
  504. ret = -EINVAL;
  505. }
  506. dev_dbg(isc->dev,
  507. "Format validation, requested rgb=%u, yuv=%u, grey=%u, bayer=%u\n",
  508. rgb, yuv, grey, bayer);
  509. if (bayer &&
  510. !ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
  511. dev_err(isc->dev, "Cannot output RAW if we do not receive RAW.\n");
  512. return -EINVAL;
  513. }
  514. if (grey && !ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code) &&
  515. !ISC_IS_FORMAT_GREY(isc->try_config.sd_format->mbus_code)) {
  516. dev_err(isc->dev, "Cannot output GREY if we do not receive RAW/GREY.\n");
  517. return -EINVAL;
  518. }
  519. if ((rgb || bayer || yuv) &&
  520. ISC_IS_FORMAT_GREY(isc->try_config.sd_format->mbus_code)) {
  521. dev_err(isc->dev, "Cannot convert GREY to another format.\n");
  522. return -EINVAL;
  523. }
  524. return ret;
  525. }
  526. /*
  527. * Configures the RLP and DMA modules, depending on the output format
  528. * configured for the ISC.
  529. * If direct_dump == true, just dump raw data 8/16 bits depending on format.
  530. */
  531. static int isc_try_configure_rlp_dma(struct isc_device *isc, bool direct_dump)
  532. {
  533. isc->try_config.rlp_cfg_mode = 0;
  534. switch (isc->try_config.fourcc) {
  535. case V4L2_PIX_FMT_SBGGR8:
  536. case V4L2_PIX_FMT_SGBRG8:
  537. case V4L2_PIX_FMT_SGRBG8:
  538. case V4L2_PIX_FMT_SRGGB8:
  539. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8;
  540. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
  541. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  542. isc->try_config.bpp = 8;
  543. isc->try_config.bpp_v4l2 = 8;
  544. break;
  545. case V4L2_PIX_FMT_SBGGR10:
  546. case V4L2_PIX_FMT_SGBRG10:
  547. case V4L2_PIX_FMT_SGRBG10:
  548. case V4L2_PIX_FMT_SRGGB10:
  549. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT10;
  550. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
  551. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  552. isc->try_config.bpp = 16;
  553. isc->try_config.bpp_v4l2 = 16;
  554. break;
  555. case V4L2_PIX_FMT_SBGGR12:
  556. case V4L2_PIX_FMT_SGBRG12:
  557. case V4L2_PIX_FMT_SGRBG12:
  558. case V4L2_PIX_FMT_SRGGB12:
  559. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT12;
  560. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
  561. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  562. isc->try_config.bpp = 16;
  563. isc->try_config.bpp_v4l2 = 16;
  564. break;
  565. case V4L2_PIX_FMT_RGB565:
  566. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_RGB565;
  567. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
  568. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  569. isc->try_config.bpp = 16;
  570. isc->try_config.bpp_v4l2 = 16;
  571. break;
  572. case V4L2_PIX_FMT_ARGB444:
  573. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB444;
  574. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
  575. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  576. isc->try_config.bpp = 16;
  577. isc->try_config.bpp_v4l2 = 16;
  578. break;
  579. case V4L2_PIX_FMT_ARGB555:
  580. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB555;
  581. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
  582. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  583. isc->try_config.bpp = 16;
  584. isc->try_config.bpp_v4l2 = 16;
  585. break;
  586. case V4L2_PIX_FMT_ABGR32:
  587. case V4L2_PIX_FMT_XBGR32:
  588. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB32;
  589. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
  590. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  591. isc->try_config.bpp = 32;
  592. isc->try_config.bpp_v4l2 = 32;
  593. break;
  594. case V4L2_PIX_FMT_YUV420:
  595. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
  596. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_YC420P;
  597. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PLANAR;
  598. isc->try_config.bpp = 12;
  599. isc->try_config.bpp_v4l2 = 8; /* only first plane */
  600. break;
  601. case V4L2_PIX_FMT_YUV422P:
  602. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
  603. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_YC422P;
  604. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PLANAR;
  605. isc->try_config.bpp = 16;
  606. isc->try_config.bpp_v4l2 = 8; /* only first plane */
  607. break;
  608. case V4L2_PIX_FMT_YUYV:
  609. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YCYC | ISC_RLP_CFG_YMODE_YUYV;
  610. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
  611. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  612. isc->try_config.bpp = 16;
  613. isc->try_config.bpp_v4l2 = 16;
  614. break;
  615. case V4L2_PIX_FMT_UYVY:
  616. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YCYC | ISC_RLP_CFG_YMODE_UYVY;
  617. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
  618. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  619. isc->try_config.bpp = 16;
  620. isc->try_config.bpp_v4l2 = 16;
  621. break;
  622. case V4L2_PIX_FMT_VYUY:
  623. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YCYC | ISC_RLP_CFG_YMODE_VYUY;
  624. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
  625. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  626. isc->try_config.bpp = 16;
  627. isc->try_config.bpp_v4l2 = 16;
  628. break;
  629. case V4L2_PIX_FMT_GREY:
  630. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY8;
  631. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
  632. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  633. isc->try_config.bpp = 8;
  634. isc->try_config.bpp_v4l2 = 8;
  635. break;
  636. case V4L2_PIX_FMT_Y16:
  637. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY10 | ISC_RLP_CFG_LSH;
  638. fallthrough;
  639. case V4L2_PIX_FMT_Y10:
  640. isc->try_config.rlp_cfg_mode |= ISC_RLP_CFG_MODE_DATY10;
  641. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
  642. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  643. isc->try_config.bpp = 16;
  644. isc->try_config.bpp_v4l2 = 16;
  645. break;
  646. default:
  647. return -EINVAL;
  648. }
  649. if (direct_dump) {
  650. isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8;
  651. isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
  652. isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  653. return 0;
  654. }
  655. return 0;
  656. }
  657. /*
  658. * Configuring pipeline modules, depending on which format the ISC outputs
  659. * and considering which format it has as input from the sensor.
  660. */
  661. static int isc_try_configure_pipeline(struct isc_device *isc)
  662. {
  663. switch (isc->try_config.fourcc) {
  664. case V4L2_PIX_FMT_RGB565:
  665. case V4L2_PIX_FMT_ARGB555:
  666. case V4L2_PIX_FMT_ARGB444:
  667. case V4L2_PIX_FMT_ABGR32:
  668. case V4L2_PIX_FMT_XBGR32:
  669. /* if sensor format is RAW, we convert inside ISC */
  670. if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
  671. isc->try_config.bits_pipeline = CFA_ENABLE |
  672. WB_ENABLE | GAM_ENABLES | DPC_BLCENABLE |
  673. CC_ENABLE;
  674. } else {
  675. isc->try_config.bits_pipeline = 0x0;
  676. }
  677. break;
  678. case V4L2_PIX_FMT_YUV420:
  679. /* if sensor format is RAW, we convert inside ISC */
  680. if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
  681. isc->try_config.bits_pipeline = CFA_ENABLE |
  682. CSC_ENABLE | GAM_ENABLES | WB_ENABLE |
  683. SUB420_ENABLE | SUB422_ENABLE | CBC_ENABLE |
  684. DPC_BLCENABLE;
  685. } else {
  686. isc->try_config.bits_pipeline = 0x0;
  687. }
  688. break;
  689. case V4L2_PIX_FMT_YUV422P:
  690. /* if sensor format is RAW, we convert inside ISC */
  691. if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
  692. isc->try_config.bits_pipeline = CFA_ENABLE |
  693. CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
  694. SUB422_ENABLE | CBC_ENABLE | DPC_BLCENABLE;
  695. } else {
  696. isc->try_config.bits_pipeline = 0x0;
  697. }
  698. break;
  699. case V4L2_PIX_FMT_YUYV:
  700. case V4L2_PIX_FMT_UYVY:
  701. case V4L2_PIX_FMT_VYUY:
  702. /* if sensor format is RAW, we convert inside ISC */
  703. if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
  704. isc->try_config.bits_pipeline = CFA_ENABLE |
  705. CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
  706. SUB422_ENABLE | CBC_ENABLE | DPC_BLCENABLE;
  707. } else {
  708. isc->try_config.bits_pipeline = 0x0;
  709. }
  710. break;
  711. case V4L2_PIX_FMT_GREY:
  712. case V4L2_PIX_FMT_Y16:
  713. /* if sensor format is RAW, we convert inside ISC */
  714. if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
  715. isc->try_config.bits_pipeline = CFA_ENABLE |
  716. CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
  717. CBC_ENABLE | DPC_BLCENABLE;
  718. } else {
  719. isc->try_config.bits_pipeline = 0x0;
  720. }
  721. break;
  722. default:
  723. if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code))
  724. isc->try_config.bits_pipeline = WB_ENABLE | DPC_BLCENABLE;
  725. else
  726. isc->try_config.bits_pipeline = 0x0;
  727. }
  728. /* Tune the pipeline to product specific */
  729. isc->adapt_pipeline(isc);
  730. return 0;
  731. }
  732. static int isc_try_fmt(struct isc_device *isc, struct v4l2_format *f)
  733. {
  734. struct v4l2_pix_format *pixfmt = &f->fmt.pix;
  735. unsigned int i;
  736. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  737. return -EINVAL;
  738. isc->try_config.fourcc = isc->controller_formats[0].fourcc;
  739. /* find if the format requested is supported */
  740. for (i = 0; i < isc->controller_formats_size; i++)
  741. if (isc->controller_formats[i].fourcc == pixfmt->pixelformat) {
  742. isc->try_config.fourcc = pixfmt->pixelformat;
  743. break;
  744. }
  745. isc_try_configure_rlp_dma(isc, false);
  746. /* Limit to Microchip ISC hardware capabilities */
  747. v4l_bound_align_image(&pixfmt->width, 16, isc->max_width, 0,
  748. &pixfmt->height, 16, isc->max_height, 0, 0);
  749. /* If we did not find the requested format, we will fallback here */
  750. pixfmt->pixelformat = isc->try_config.fourcc;
  751. pixfmt->colorspace = V4L2_COLORSPACE_SRGB;
  752. pixfmt->field = V4L2_FIELD_NONE;
  753. pixfmt->bytesperline = (pixfmt->width * isc->try_config.bpp_v4l2) >> 3;
  754. pixfmt->sizeimage = ((pixfmt->width * isc->try_config.bpp) >> 3) *
  755. pixfmt->height;
  756. isc->try_fmt = *f;
  757. return 0;
  758. }
  759. static int isc_set_fmt(struct isc_device *isc, struct v4l2_format *f)
  760. {
  761. isc_try_fmt(isc, f);
  762. /* make the try configuration active */
  763. isc->config = isc->try_config;
  764. isc->fmt = isc->try_fmt;
  765. dev_dbg(isc->dev, "ISC set_fmt to %.4s @%dx%d\n",
  766. (char *)&f->fmt.pix.pixelformat,
  767. f->fmt.pix.width, f->fmt.pix.height);
  768. return 0;
  769. }
  770. static int isc_link_validate(struct media_link *link)
  771. {
  772. struct video_device *vdev =
  773. media_entity_to_video_device(link->sink->entity);
  774. struct isc_device *isc = video_get_drvdata(vdev);
  775. int ret;
  776. int i;
  777. struct isc_format *sd_fmt = NULL;
  778. struct v4l2_pix_format *pixfmt = &isc->fmt.fmt.pix;
  779. struct v4l2_subdev_format format = {
  780. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  781. .pad = isc->remote_pad,
  782. };
  783. /* Get current format from subdev */
  784. ret = v4l2_subdev_call(isc->current_subdev->sd, pad, get_fmt, NULL,
  785. &format);
  786. if (ret)
  787. return ret;
  788. /* Identify the subdev's format configuration */
  789. for (i = 0; i < isc->formats_list_size; i++)
  790. if (isc->formats_list[i].mbus_code == format.format.code) {
  791. sd_fmt = &isc->formats_list[i];
  792. break;
  793. }
  794. /* Check if the format is not supported */
  795. if (!sd_fmt) {
  796. dev_err(isc->dev,
  797. "Current subdevice is streaming a media bus code that is not supported 0x%x\n",
  798. format.format.code);
  799. return -EPIPE;
  800. }
  801. /* At this moment we know which format the subdev will use */
  802. isc->try_config.sd_format = sd_fmt;
  803. /* If the sensor is not RAW, we can only do a direct dump */
  804. if (!ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code))
  805. isc_try_configure_rlp_dma(isc, true);
  806. /* Limit to Microchip ISC hardware capabilities */
  807. v4l_bound_align_image(&format.format.width, 16, isc->max_width, 0,
  808. &format.format.height, 16, isc->max_height, 0, 0);
  809. /* Check if the frame size is the same. Otherwise we may overflow */
  810. if (pixfmt->height != format.format.height ||
  811. pixfmt->width != format.format.width) {
  812. dev_err(isc->dev,
  813. "ISC not configured with the proper frame size: %dx%d\n",
  814. format.format.width, format.format.height);
  815. return -EPIPE;
  816. }
  817. dev_dbg(isc->dev,
  818. "Identified subdev using format %.4s with %dx%d %d bpp\n",
  819. (char *)&sd_fmt->fourcc, pixfmt->width, pixfmt->height,
  820. isc->try_config.bpp);
  821. /* Reset and restart AWB if the subdevice changed the format */
  822. if (isc->try_config.sd_format && isc->config.sd_format &&
  823. isc->try_config.sd_format != isc->config.sd_format) {
  824. isc->ctrls.hist_stat = HIST_INIT;
  825. isc_reset_awb_ctrls(isc);
  826. isc_update_v4l2_ctrls(isc);
  827. }
  828. /* Validate formats */
  829. ret = isc_try_validate_formats(isc);
  830. if (ret)
  831. return ret;
  832. /* Configure ISC pipeline for the config */
  833. ret = isc_try_configure_pipeline(isc);
  834. if (ret)
  835. return ret;
  836. isc->config = isc->try_config;
  837. dev_dbg(isc->dev, "New ISC configuration in place\n");
  838. return 0;
  839. }
  840. static int isc_s_fmt_vid_cap(struct file *file, void *priv,
  841. struct v4l2_format *f)
  842. {
  843. struct isc_device *isc = video_drvdata(file);
  844. if (vb2_is_busy(&isc->vb2_vidq))
  845. return -EBUSY;
  846. return isc_set_fmt(isc, f);
  847. }
  848. static int isc_try_fmt_vid_cap(struct file *file, void *priv,
  849. struct v4l2_format *f)
  850. {
  851. struct isc_device *isc = video_drvdata(file);
  852. return isc_try_fmt(isc, f);
  853. }
  854. static int isc_enum_input(struct file *file, void *priv,
  855. struct v4l2_input *inp)
  856. {
  857. if (inp->index != 0)
  858. return -EINVAL;
  859. inp->type = V4L2_INPUT_TYPE_CAMERA;
  860. inp->std = 0;
  861. strscpy(inp->name, "Camera", sizeof(inp->name));
  862. return 0;
  863. }
  864. static int isc_g_input(struct file *file, void *priv, unsigned int *i)
  865. {
  866. *i = 0;
  867. return 0;
  868. }
  869. static int isc_s_input(struct file *file, void *priv, unsigned int i)
  870. {
  871. if (i > 0)
  872. return -EINVAL;
  873. return 0;
  874. }
  875. static int isc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  876. {
  877. struct isc_device *isc = video_drvdata(file);
  878. return v4l2_g_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
  879. }
  880. static int isc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  881. {
  882. struct isc_device *isc = video_drvdata(file);
  883. return v4l2_s_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
  884. }
  885. static int isc_enum_framesizes(struct file *file, void *fh,
  886. struct v4l2_frmsizeenum *fsize)
  887. {
  888. struct isc_device *isc = video_drvdata(file);
  889. int ret = -EINVAL;
  890. int i;
  891. if (fsize->index)
  892. return -EINVAL;
  893. for (i = 0; i < isc->controller_formats_size; i++)
  894. if (isc->controller_formats[i].fourcc == fsize->pixel_format)
  895. ret = 0;
  896. if (ret)
  897. return ret;
  898. fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
  899. fsize->stepwise.min_width = 16;
  900. fsize->stepwise.max_width = isc->max_width;
  901. fsize->stepwise.min_height = 16;
  902. fsize->stepwise.max_height = isc->max_height;
  903. fsize->stepwise.step_width = 1;
  904. fsize->stepwise.step_height = 1;
  905. return 0;
  906. }
  907. static const struct v4l2_ioctl_ops isc_ioctl_ops = {
  908. .vidioc_querycap = isc_querycap,
  909. .vidioc_enum_fmt_vid_cap = isc_enum_fmt_vid_cap,
  910. .vidioc_g_fmt_vid_cap = isc_g_fmt_vid_cap,
  911. .vidioc_s_fmt_vid_cap = isc_s_fmt_vid_cap,
  912. .vidioc_try_fmt_vid_cap = isc_try_fmt_vid_cap,
  913. .vidioc_enum_input = isc_enum_input,
  914. .vidioc_g_input = isc_g_input,
  915. .vidioc_s_input = isc_s_input,
  916. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  917. .vidioc_querybuf = vb2_ioctl_querybuf,
  918. .vidioc_qbuf = vb2_ioctl_qbuf,
  919. .vidioc_expbuf = vb2_ioctl_expbuf,
  920. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  921. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  922. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  923. .vidioc_streamon = vb2_ioctl_streamon,
  924. .vidioc_streamoff = vb2_ioctl_streamoff,
  925. .vidioc_g_parm = isc_g_parm,
  926. .vidioc_s_parm = isc_s_parm,
  927. .vidioc_enum_framesizes = isc_enum_framesizes,
  928. .vidioc_log_status = v4l2_ctrl_log_status,
  929. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  930. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  931. };
  932. static int isc_open(struct file *file)
  933. {
  934. struct isc_device *isc = video_drvdata(file);
  935. struct v4l2_subdev *sd = isc->current_subdev->sd;
  936. int ret;
  937. if (mutex_lock_interruptible(&isc->lock))
  938. return -ERESTARTSYS;
  939. ret = v4l2_fh_open(file);
  940. if (ret < 0)
  941. goto unlock;
  942. if (!v4l2_fh_is_singular_file(file))
  943. goto unlock;
  944. ret = v4l2_subdev_call(sd, core, s_power, 1);
  945. if (ret < 0 && ret != -ENOIOCTLCMD) {
  946. v4l2_fh_release(file);
  947. goto unlock;
  948. }
  949. ret = isc_set_fmt(isc, &isc->fmt);
  950. if (ret) {
  951. v4l2_subdev_call(sd, core, s_power, 0);
  952. v4l2_fh_release(file);
  953. }
  954. unlock:
  955. mutex_unlock(&isc->lock);
  956. return ret;
  957. }
  958. static int isc_release(struct file *file)
  959. {
  960. struct isc_device *isc = video_drvdata(file);
  961. struct v4l2_subdev *sd = isc->current_subdev->sd;
  962. bool fh_singular;
  963. int ret;
  964. mutex_lock(&isc->lock);
  965. fh_singular = v4l2_fh_is_singular_file(file);
  966. ret = _vb2_fop_release(file, NULL);
  967. if (fh_singular)
  968. v4l2_subdev_call(sd, core, s_power, 0);
  969. mutex_unlock(&isc->lock);
  970. return ret;
  971. }
  972. static const struct v4l2_file_operations isc_fops = {
  973. .owner = THIS_MODULE,
  974. .open = isc_open,
  975. .release = isc_release,
  976. .unlocked_ioctl = video_ioctl2,
  977. .read = vb2_fop_read,
  978. .mmap = vb2_fop_mmap,
  979. .poll = vb2_fop_poll,
  980. };
  981. irqreturn_t microchip_isc_interrupt(int irq, void *dev_id)
  982. {
  983. struct isc_device *isc = (struct isc_device *)dev_id;
  984. struct regmap *regmap = isc->regmap;
  985. u32 isc_intsr, isc_intmask, pending;
  986. irqreturn_t ret = IRQ_NONE;
  987. regmap_read(regmap, ISC_INTSR, &isc_intsr);
  988. regmap_read(regmap, ISC_INTMASK, &isc_intmask);
  989. pending = isc_intsr & isc_intmask;
  990. if (likely(pending & ISC_INT_DDONE)) {
  991. spin_lock(&isc->dma_queue_lock);
  992. if (isc->cur_frm) {
  993. struct vb2_v4l2_buffer *vbuf = &isc->cur_frm->vb;
  994. struct vb2_buffer *vb = &vbuf->vb2_buf;
  995. vb->timestamp = ktime_get_ns();
  996. vbuf->sequence = isc->sequence++;
  997. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  998. isc->cur_frm = NULL;
  999. }
  1000. if (!list_empty(&isc->dma_queue) && !isc->stop) {
  1001. isc->cur_frm = list_first_entry(&isc->dma_queue,
  1002. struct isc_buffer, list);
  1003. list_del(&isc->cur_frm->list);
  1004. isc_start_dma(isc);
  1005. }
  1006. if (isc->stop)
  1007. complete(&isc->comp);
  1008. ret = IRQ_HANDLED;
  1009. spin_unlock(&isc->dma_queue_lock);
  1010. }
  1011. if (pending & ISC_INT_HISDONE) {
  1012. schedule_work(&isc->awb_work);
  1013. ret = IRQ_HANDLED;
  1014. }
  1015. return ret;
  1016. }
  1017. EXPORT_SYMBOL_GPL(microchip_isc_interrupt);
  1018. static void isc_hist_count(struct isc_device *isc, u32 *min, u32 *max)
  1019. {
  1020. struct regmap *regmap = isc->regmap;
  1021. struct isc_ctrls *ctrls = &isc->ctrls;
  1022. u32 *hist_count = &ctrls->hist_count[ctrls->hist_id];
  1023. u32 *hist_entry = &ctrls->hist_entry[0];
  1024. u32 i;
  1025. *min = 0;
  1026. *max = HIST_ENTRIES;
  1027. regmap_bulk_read(regmap, ISC_HIS_ENTRY + isc->offsets.his_entry,
  1028. hist_entry, HIST_ENTRIES);
  1029. *hist_count = 0;
  1030. /*
  1031. * we deliberately ignore the end of the histogram,
  1032. * the most white pixels
  1033. */
  1034. for (i = 1; i < HIST_ENTRIES; i++) {
  1035. if (*hist_entry && !*min)
  1036. *min = i;
  1037. if (*hist_entry)
  1038. *max = i;
  1039. *hist_count += i * (*hist_entry++);
  1040. }
  1041. if (!*min)
  1042. *min = 1;
  1043. dev_dbg(isc->dev, "isc wb: hist_id %u, hist_count %u",
  1044. ctrls->hist_id, *hist_count);
  1045. }
  1046. static void isc_wb_update(struct isc_ctrls *ctrls)
  1047. {
  1048. struct isc_device *isc = container_of(ctrls, struct isc_device, ctrls);
  1049. u32 *hist_count = &ctrls->hist_count[0];
  1050. u32 c, offset[4];
  1051. u64 avg = 0;
  1052. /* We compute two gains, stretch gain and grey world gain */
  1053. u32 s_gain[4], gw_gain[4];
  1054. /*
  1055. * According to Grey World, we need to set gains for R/B to normalize
  1056. * them towards the green channel.
  1057. * Thus we want to keep Green as fixed and adjust only Red/Blue
  1058. * Compute the average of the both green channels first
  1059. */
  1060. avg = (u64)hist_count[ISC_HIS_CFG_MODE_GR] +
  1061. (u64)hist_count[ISC_HIS_CFG_MODE_GB];
  1062. avg >>= 1;
  1063. dev_dbg(isc->dev, "isc wb: green components average %llu\n", avg);
  1064. /* Green histogram is null, nothing to do */
  1065. if (!avg)
  1066. return;
  1067. for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
  1068. /*
  1069. * the color offset is the minimum value of the histogram.
  1070. * we stretch this color to the full range by substracting
  1071. * this value from the color component.
  1072. */
  1073. offset[c] = ctrls->hist_minmax[c][HIST_MIN_INDEX];
  1074. /*
  1075. * The offset is always at least 1. If the offset is 1, we do
  1076. * not need to adjust it, so our result must be zero.
  1077. * the offset is computed in a histogram on 9 bits (0..512)
  1078. * but the offset in register is based on
  1079. * 12 bits pipeline (0..4096).
  1080. * we need to shift with the 3 bits that the histogram is
  1081. * ignoring
  1082. */
  1083. ctrls->offset[c] = (offset[c] - 1) << 3;
  1084. /*
  1085. * the offset is then taken and converted to 2's complements,
  1086. * and must be negative, as we subtract this value from the
  1087. * color components
  1088. */
  1089. ctrls->offset[c] = -ctrls->offset[c];
  1090. /*
  1091. * the stretch gain is the total number of histogram bins
  1092. * divided by the actual range of color component (Max - Min)
  1093. * If we compute gain like this, the actual color component
  1094. * will be stretched to the full histogram.
  1095. * We need to shift 9 bits for precision, we have 9 bits for
  1096. * decimals
  1097. */
  1098. s_gain[c] = (HIST_ENTRIES << 9) /
  1099. (ctrls->hist_minmax[c][HIST_MAX_INDEX] -
  1100. ctrls->hist_minmax[c][HIST_MIN_INDEX] + 1);
  1101. /*
  1102. * Now we have to compute the gain w.r.t. the average.
  1103. * Add/lose gain to the component towards the average.
  1104. * If it happens that the component is zero, use the
  1105. * fixed point value : 1.0 gain.
  1106. */
  1107. if (hist_count[c])
  1108. gw_gain[c] = div_u64(avg << 9, hist_count[c]);
  1109. else
  1110. gw_gain[c] = 1 << 9;
  1111. dev_dbg(isc->dev,
  1112. "isc wb: component %d, s_gain %u, gw_gain %u\n",
  1113. c, s_gain[c], gw_gain[c]);
  1114. /* multiply both gains and adjust for decimals */
  1115. ctrls->gain[c] = s_gain[c] * gw_gain[c];
  1116. ctrls->gain[c] >>= 9;
  1117. /* make sure we are not out of range */
  1118. ctrls->gain[c] = clamp_val(ctrls->gain[c], 0, GENMASK(12, 0));
  1119. dev_dbg(isc->dev, "isc wb: component %d, final gain %u\n",
  1120. c, ctrls->gain[c]);
  1121. }
  1122. }
  1123. static void isc_awb_work(struct work_struct *w)
  1124. {
  1125. struct isc_device *isc =
  1126. container_of(w, struct isc_device, awb_work);
  1127. struct regmap *regmap = isc->regmap;
  1128. struct isc_ctrls *ctrls = &isc->ctrls;
  1129. u32 hist_id = ctrls->hist_id;
  1130. u32 baysel;
  1131. unsigned long flags;
  1132. u32 min, max;
  1133. int ret;
  1134. if (ctrls->hist_stat != HIST_ENABLED)
  1135. return;
  1136. isc_hist_count(isc, &min, &max);
  1137. dev_dbg(isc->dev,
  1138. "isc wb mode %d: hist min %u , max %u\n", hist_id, min, max);
  1139. ctrls->hist_minmax[hist_id][HIST_MIN_INDEX] = min;
  1140. ctrls->hist_minmax[hist_id][HIST_MAX_INDEX] = max;
  1141. if (hist_id != ISC_HIS_CFG_MODE_B) {
  1142. hist_id++;
  1143. } else {
  1144. isc_wb_update(ctrls);
  1145. hist_id = ISC_HIS_CFG_MODE_GR;
  1146. }
  1147. ctrls->hist_id = hist_id;
  1148. baysel = isc->config.sd_format->cfa_baycfg << ISC_HIS_CFG_BAYSEL_SHIFT;
  1149. ret = pm_runtime_resume_and_get(isc->dev);
  1150. if (ret < 0)
  1151. return;
  1152. /*
  1153. * only update if we have all the required histograms and controls
  1154. * if awb has been disabled, we need to reset registers as well.
  1155. */
  1156. if (hist_id == ISC_HIS_CFG_MODE_GR || ctrls->awb == ISC_WB_NONE) {
  1157. /*
  1158. * It may happen that DMA Done IRQ will trigger while we are
  1159. * updating white balance registers here.
  1160. * In that case, only parts of the controls have been updated.
  1161. * We can avoid that by locking the section.
  1162. */
  1163. spin_lock_irqsave(&isc->awb_lock, flags);
  1164. isc_update_awb_ctrls(isc);
  1165. spin_unlock_irqrestore(&isc->awb_lock, flags);
  1166. /*
  1167. * if we are doing just the one time white balance adjustment,
  1168. * we are basically done.
  1169. */
  1170. if (ctrls->awb == ISC_WB_ONETIME) {
  1171. dev_info(isc->dev,
  1172. "Completed one time white-balance adjustment.\n");
  1173. /* update the v4l2 controls values */
  1174. isc_update_v4l2_ctrls(isc);
  1175. ctrls->awb = ISC_WB_NONE;
  1176. }
  1177. }
  1178. regmap_write(regmap, ISC_HIS_CFG + isc->offsets.his,
  1179. hist_id | baysel | ISC_HIS_CFG_RAR);
  1180. /*
  1181. * We have to make sure the streaming has not stopped meanwhile.
  1182. * ISC requires a frame to clock the internal profile update.
  1183. * To avoid issues, lock the sequence with a mutex
  1184. */
  1185. mutex_lock(&isc->awb_mutex);
  1186. /* streaming is not active anymore */
  1187. if (isc->stop) {
  1188. mutex_unlock(&isc->awb_mutex);
  1189. return;
  1190. }
  1191. isc_update_profile(isc);
  1192. mutex_unlock(&isc->awb_mutex);
  1193. /* if awb has been disabled, we don't need to start another histogram */
  1194. if (ctrls->awb)
  1195. regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
  1196. pm_runtime_put_sync(isc->dev);
  1197. }
  1198. static int isc_s_ctrl(struct v4l2_ctrl *ctrl)
  1199. {
  1200. struct isc_device *isc = container_of(ctrl->handler,
  1201. struct isc_device, ctrls.handler);
  1202. struct isc_ctrls *ctrls = &isc->ctrls;
  1203. if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
  1204. return 0;
  1205. switch (ctrl->id) {
  1206. case V4L2_CID_BRIGHTNESS:
  1207. ctrls->brightness = ctrl->val & ISC_CBC_BRIGHT_MASK;
  1208. break;
  1209. case V4L2_CID_CONTRAST:
  1210. ctrls->contrast = ctrl->val & ISC_CBC_CONTRAST_MASK;
  1211. break;
  1212. case V4L2_CID_GAMMA:
  1213. ctrls->gamma_index = ctrl->val;
  1214. break;
  1215. default:
  1216. return -EINVAL;
  1217. }
  1218. return 0;
  1219. }
  1220. static const struct v4l2_ctrl_ops isc_ctrl_ops = {
  1221. .s_ctrl = isc_s_ctrl,
  1222. };
  1223. static int isc_s_awb_ctrl(struct v4l2_ctrl *ctrl)
  1224. {
  1225. struct isc_device *isc = container_of(ctrl->handler,
  1226. struct isc_device, ctrls.handler);
  1227. struct isc_ctrls *ctrls = &isc->ctrls;
  1228. if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
  1229. return 0;
  1230. switch (ctrl->id) {
  1231. case V4L2_CID_AUTO_WHITE_BALANCE:
  1232. if (ctrl->val == 1)
  1233. ctrls->awb = ISC_WB_AUTO;
  1234. else
  1235. ctrls->awb = ISC_WB_NONE;
  1236. /* configure the controls with new values from v4l2 */
  1237. if (ctrl->cluster[ISC_CTRL_R_GAIN]->is_new)
  1238. ctrls->gain[ISC_HIS_CFG_MODE_R] = isc->r_gain_ctrl->val;
  1239. if (ctrl->cluster[ISC_CTRL_B_GAIN]->is_new)
  1240. ctrls->gain[ISC_HIS_CFG_MODE_B] = isc->b_gain_ctrl->val;
  1241. if (ctrl->cluster[ISC_CTRL_GR_GAIN]->is_new)
  1242. ctrls->gain[ISC_HIS_CFG_MODE_GR] = isc->gr_gain_ctrl->val;
  1243. if (ctrl->cluster[ISC_CTRL_GB_GAIN]->is_new)
  1244. ctrls->gain[ISC_HIS_CFG_MODE_GB] = isc->gb_gain_ctrl->val;
  1245. if (ctrl->cluster[ISC_CTRL_R_OFF]->is_new)
  1246. ctrls->offset[ISC_HIS_CFG_MODE_R] = isc->r_off_ctrl->val;
  1247. if (ctrl->cluster[ISC_CTRL_B_OFF]->is_new)
  1248. ctrls->offset[ISC_HIS_CFG_MODE_B] = isc->b_off_ctrl->val;
  1249. if (ctrl->cluster[ISC_CTRL_GR_OFF]->is_new)
  1250. ctrls->offset[ISC_HIS_CFG_MODE_GR] = isc->gr_off_ctrl->val;
  1251. if (ctrl->cluster[ISC_CTRL_GB_OFF]->is_new)
  1252. ctrls->offset[ISC_HIS_CFG_MODE_GB] = isc->gb_off_ctrl->val;
  1253. isc_update_awb_ctrls(isc);
  1254. mutex_lock(&isc->awb_mutex);
  1255. if (vb2_is_streaming(&isc->vb2_vidq)) {
  1256. /*
  1257. * If we are streaming, we can update profile to
  1258. * have the new settings in place.
  1259. */
  1260. isc_update_profile(isc);
  1261. } else {
  1262. /*
  1263. * The auto cluster will activate automatically this
  1264. * control. This has to be deactivated when not
  1265. * streaming.
  1266. */
  1267. v4l2_ctrl_activate(isc->do_wb_ctrl, false);
  1268. }
  1269. mutex_unlock(&isc->awb_mutex);
  1270. /* if we have autowhitebalance on, start histogram procedure */
  1271. if (ctrls->awb == ISC_WB_AUTO &&
  1272. vb2_is_streaming(&isc->vb2_vidq) &&
  1273. ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
  1274. isc_set_histogram(isc, true);
  1275. /*
  1276. * for one time whitebalance adjustment, check the button,
  1277. * if it's pressed, perform the one time operation.
  1278. */
  1279. if (ctrls->awb == ISC_WB_NONE &&
  1280. ctrl->cluster[ISC_CTRL_DO_WB]->is_new &&
  1281. !(ctrl->cluster[ISC_CTRL_DO_WB]->flags &
  1282. V4L2_CTRL_FLAG_INACTIVE)) {
  1283. ctrls->awb = ISC_WB_ONETIME;
  1284. isc_set_histogram(isc, true);
  1285. dev_dbg(isc->dev, "One time white-balance started.\n");
  1286. }
  1287. return 0;
  1288. }
  1289. return 0;
  1290. }
  1291. static int isc_g_volatile_awb_ctrl(struct v4l2_ctrl *ctrl)
  1292. {
  1293. struct isc_device *isc = container_of(ctrl->handler,
  1294. struct isc_device, ctrls.handler);
  1295. struct isc_ctrls *ctrls = &isc->ctrls;
  1296. switch (ctrl->id) {
  1297. /* being a cluster, this id will be called for every control */
  1298. case V4L2_CID_AUTO_WHITE_BALANCE:
  1299. ctrl->cluster[ISC_CTRL_R_GAIN]->val =
  1300. ctrls->gain[ISC_HIS_CFG_MODE_R];
  1301. ctrl->cluster[ISC_CTRL_B_GAIN]->val =
  1302. ctrls->gain[ISC_HIS_CFG_MODE_B];
  1303. ctrl->cluster[ISC_CTRL_GR_GAIN]->val =
  1304. ctrls->gain[ISC_HIS_CFG_MODE_GR];
  1305. ctrl->cluster[ISC_CTRL_GB_GAIN]->val =
  1306. ctrls->gain[ISC_HIS_CFG_MODE_GB];
  1307. ctrl->cluster[ISC_CTRL_R_OFF]->val =
  1308. ctrls->offset[ISC_HIS_CFG_MODE_R];
  1309. ctrl->cluster[ISC_CTRL_B_OFF]->val =
  1310. ctrls->offset[ISC_HIS_CFG_MODE_B];
  1311. ctrl->cluster[ISC_CTRL_GR_OFF]->val =
  1312. ctrls->offset[ISC_HIS_CFG_MODE_GR];
  1313. ctrl->cluster[ISC_CTRL_GB_OFF]->val =
  1314. ctrls->offset[ISC_HIS_CFG_MODE_GB];
  1315. break;
  1316. }
  1317. return 0;
  1318. }
  1319. static const struct v4l2_ctrl_ops isc_awb_ops = {
  1320. .s_ctrl = isc_s_awb_ctrl,
  1321. .g_volatile_ctrl = isc_g_volatile_awb_ctrl,
  1322. };
  1323. #define ISC_CTRL_OFF(_name, _id, _name_str) \
  1324. static const struct v4l2_ctrl_config _name = { \
  1325. .ops = &isc_awb_ops, \
  1326. .id = _id, \
  1327. .name = _name_str, \
  1328. .type = V4L2_CTRL_TYPE_INTEGER, \
  1329. .flags = V4L2_CTRL_FLAG_SLIDER, \
  1330. .min = -4095, \
  1331. .max = 4095, \
  1332. .step = 1, \
  1333. .def = 0, \
  1334. }
  1335. ISC_CTRL_OFF(isc_r_off_ctrl, ISC_CID_R_OFFSET, "Red Component Offset");
  1336. ISC_CTRL_OFF(isc_b_off_ctrl, ISC_CID_B_OFFSET, "Blue Component Offset");
  1337. ISC_CTRL_OFF(isc_gr_off_ctrl, ISC_CID_GR_OFFSET, "Green Red Component Offset");
  1338. ISC_CTRL_OFF(isc_gb_off_ctrl, ISC_CID_GB_OFFSET, "Green Blue Component Offset");
  1339. #define ISC_CTRL_GAIN(_name, _id, _name_str) \
  1340. static const struct v4l2_ctrl_config _name = { \
  1341. .ops = &isc_awb_ops, \
  1342. .id = _id, \
  1343. .name = _name_str, \
  1344. .type = V4L2_CTRL_TYPE_INTEGER, \
  1345. .flags = V4L2_CTRL_FLAG_SLIDER, \
  1346. .min = 0, \
  1347. .max = 8191, \
  1348. .step = 1, \
  1349. .def = 512, \
  1350. }
  1351. ISC_CTRL_GAIN(isc_r_gain_ctrl, ISC_CID_R_GAIN, "Red Component Gain");
  1352. ISC_CTRL_GAIN(isc_b_gain_ctrl, ISC_CID_B_GAIN, "Blue Component Gain");
  1353. ISC_CTRL_GAIN(isc_gr_gain_ctrl, ISC_CID_GR_GAIN, "Green Red Component Gain");
  1354. ISC_CTRL_GAIN(isc_gb_gain_ctrl, ISC_CID_GB_GAIN, "Green Blue Component Gain");
  1355. static int isc_ctrl_init(struct isc_device *isc)
  1356. {
  1357. const struct v4l2_ctrl_ops *ops = &isc_ctrl_ops;
  1358. struct isc_ctrls *ctrls = &isc->ctrls;
  1359. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  1360. int ret;
  1361. ctrls->hist_stat = HIST_INIT;
  1362. isc_reset_awb_ctrls(isc);
  1363. ret = v4l2_ctrl_handler_init(hdl, 13);
  1364. if (ret < 0)
  1365. return ret;
  1366. /* Initialize product specific controls. For example, contrast */
  1367. isc->config_ctrls(isc, ops);
  1368. ctrls->brightness = 0;
  1369. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -1024, 1023, 1, 0);
  1370. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAMMA, 0, isc->gamma_max, 1,
  1371. isc->gamma_max);
  1372. isc->awb_ctrl = v4l2_ctrl_new_std(hdl, &isc_awb_ops,
  1373. V4L2_CID_AUTO_WHITE_BALANCE,
  1374. 0, 1, 1, 1);
  1375. /* do_white_balance is a button, so min,max,step,default are ignored */
  1376. isc->do_wb_ctrl = v4l2_ctrl_new_std(hdl, &isc_awb_ops,
  1377. V4L2_CID_DO_WHITE_BALANCE,
  1378. 0, 0, 0, 0);
  1379. if (!isc->do_wb_ctrl) {
  1380. ret = hdl->error;
  1381. v4l2_ctrl_handler_free(hdl);
  1382. return ret;
  1383. }
  1384. v4l2_ctrl_activate(isc->do_wb_ctrl, false);
  1385. isc->r_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_r_gain_ctrl, NULL);
  1386. isc->b_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_b_gain_ctrl, NULL);
  1387. isc->gr_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gr_gain_ctrl, NULL);
  1388. isc->gb_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gb_gain_ctrl, NULL);
  1389. isc->r_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_r_off_ctrl, NULL);
  1390. isc->b_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_b_off_ctrl, NULL);
  1391. isc->gr_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gr_off_ctrl, NULL);
  1392. isc->gb_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gb_off_ctrl, NULL);
  1393. /*
  1394. * The cluster is in auto mode with autowhitebalance enabled
  1395. * and manual mode otherwise.
  1396. */
  1397. v4l2_ctrl_auto_cluster(10, &isc->awb_ctrl, 0, true);
  1398. v4l2_ctrl_handler_setup(hdl);
  1399. return 0;
  1400. }
  1401. static int isc_async_bound(struct v4l2_async_notifier *notifier,
  1402. struct v4l2_subdev *subdev,
  1403. struct v4l2_async_connection *asd)
  1404. {
  1405. struct isc_device *isc = container_of(notifier->v4l2_dev,
  1406. struct isc_device, v4l2_dev);
  1407. struct isc_subdev_entity *subdev_entity =
  1408. container_of(notifier, struct isc_subdev_entity, notifier);
  1409. int pad;
  1410. if (video_is_registered(&isc->video_dev)) {
  1411. dev_err(isc->dev, "only supports one sub-device.\n");
  1412. return -EBUSY;
  1413. }
  1414. subdev_entity->sd = subdev;
  1415. pad = media_entity_get_fwnode_pad(&subdev->entity, asd->match.fwnode,
  1416. MEDIA_PAD_FL_SOURCE);
  1417. if (pad < 0) {
  1418. dev_err(isc->dev, "failed to find pad for %s\n", subdev->name);
  1419. return pad;
  1420. }
  1421. isc->remote_pad = pad;
  1422. return 0;
  1423. }
  1424. static void isc_async_unbind(struct v4l2_async_notifier *notifier,
  1425. struct v4l2_subdev *subdev,
  1426. struct v4l2_async_connection *asd)
  1427. {
  1428. struct isc_device *isc = container_of(notifier->v4l2_dev,
  1429. struct isc_device, v4l2_dev);
  1430. mutex_destroy(&isc->awb_mutex);
  1431. cancel_work_sync(&isc->awb_work);
  1432. video_unregister_device(&isc->video_dev);
  1433. v4l2_ctrl_handler_free(&isc->ctrls.handler);
  1434. }
  1435. struct isc_format *isc_find_format_by_code(struct isc_device *isc,
  1436. unsigned int code, int *index)
  1437. {
  1438. struct isc_format *fmt = &isc->formats_list[0];
  1439. unsigned int i;
  1440. for (i = 0; i < isc->formats_list_size; i++) {
  1441. if (fmt->mbus_code == code) {
  1442. *index = i;
  1443. return fmt;
  1444. }
  1445. fmt++;
  1446. }
  1447. return NULL;
  1448. }
  1449. EXPORT_SYMBOL_GPL(isc_find_format_by_code);
  1450. static int isc_set_default_fmt(struct isc_device *isc)
  1451. {
  1452. struct v4l2_format f = {
  1453. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1454. .fmt.pix = {
  1455. .width = VGA_WIDTH,
  1456. .height = VGA_HEIGHT,
  1457. .field = V4L2_FIELD_NONE,
  1458. .pixelformat = isc->controller_formats[0].fourcc,
  1459. },
  1460. };
  1461. int ret;
  1462. ret = isc_try_fmt(isc, &f);
  1463. if (ret)
  1464. return ret;
  1465. isc->fmt = f;
  1466. return 0;
  1467. }
  1468. static int isc_async_complete(struct v4l2_async_notifier *notifier)
  1469. {
  1470. struct isc_device *isc = container_of(notifier->v4l2_dev,
  1471. struct isc_device, v4l2_dev);
  1472. struct video_device *vdev = &isc->video_dev;
  1473. struct vb2_queue *q = &isc->vb2_vidq;
  1474. int ret = 0;
  1475. INIT_WORK(&isc->awb_work, isc_awb_work);
  1476. ret = v4l2_device_register_subdev_nodes(&isc->v4l2_dev);
  1477. if (ret < 0) {
  1478. dev_err(isc->dev, "Failed to register subdev nodes\n");
  1479. return ret;
  1480. }
  1481. isc->current_subdev = container_of(notifier,
  1482. struct isc_subdev_entity, notifier);
  1483. mutex_init(&isc->lock);
  1484. mutex_init(&isc->awb_mutex);
  1485. init_completion(&isc->comp);
  1486. /* Initialize videobuf2 queue */
  1487. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1488. q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  1489. q->drv_priv = isc;
  1490. q->buf_struct_size = sizeof(struct isc_buffer);
  1491. q->ops = &isc_vb2_ops;
  1492. q->mem_ops = &vb2_dma_contig_memops;
  1493. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1494. q->lock = &isc->lock;
  1495. q->min_queued_buffers = 1;
  1496. q->dev = isc->dev;
  1497. ret = vb2_queue_init(q);
  1498. if (ret < 0) {
  1499. dev_err(isc->dev, "vb2_queue_init() failed: %d\n", ret);
  1500. goto isc_async_complete_err;
  1501. }
  1502. /* Init video dma queues */
  1503. INIT_LIST_HEAD(&isc->dma_queue);
  1504. spin_lock_init(&isc->dma_queue_lock);
  1505. spin_lock_init(&isc->awb_lock);
  1506. ret = isc_set_default_fmt(isc);
  1507. if (ret) {
  1508. dev_err(isc->dev, "Could not set default format\n");
  1509. goto isc_async_complete_err;
  1510. }
  1511. ret = isc_ctrl_init(isc);
  1512. if (ret) {
  1513. dev_err(isc->dev, "Init isc ctrols failed: %d\n", ret);
  1514. goto isc_async_complete_err;
  1515. }
  1516. /* Register video device */
  1517. strscpy(vdev->name, KBUILD_MODNAME, sizeof(vdev->name));
  1518. vdev->release = video_device_release_empty;
  1519. vdev->fops = &isc_fops;
  1520. vdev->ioctl_ops = &isc_ioctl_ops;
  1521. vdev->v4l2_dev = &isc->v4l2_dev;
  1522. vdev->vfl_dir = VFL_DIR_RX;
  1523. vdev->queue = q;
  1524. vdev->lock = &isc->lock;
  1525. vdev->ctrl_handler = &isc->ctrls.handler;
  1526. vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
  1527. V4L2_CAP_IO_MC;
  1528. video_set_drvdata(vdev, isc);
  1529. ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
  1530. if (ret < 0) {
  1531. dev_err(isc->dev, "video_register_device failed: %d\n", ret);
  1532. goto isc_async_complete_err;
  1533. }
  1534. ret = isc_scaler_link(isc);
  1535. if (ret < 0)
  1536. goto isc_async_complete_unregister_device;
  1537. ret = media_device_register(&isc->mdev);
  1538. if (ret < 0)
  1539. goto isc_async_complete_unregister_device;
  1540. return 0;
  1541. isc_async_complete_unregister_device:
  1542. video_unregister_device(vdev);
  1543. isc_async_complete_err:
  1544. mutex_destroy(&isc->awb_mutex);
  1545. mutex_destroy(&isc->lock);
  1546. return ret;
  1547. }
  1548. const struct v4l2_async_notifier_operations microchip_isc_async_ops = {
  1549. .bound = isc_async_bound,
  1550. .unbind = isc_async_unbind,
  1551. .complete = isc_async_complete,
  1552. };
  1553. EXPORT_SYMBOL_GPL(microchip_isc_async_ops);
  1554. void microchip_isc_subdev_cleanup(struct isc_device *isc)
  1555. {
  1556. struct isc_subdev_entity *subdev_entity;
  1557. list_for_each_entry(subdev_entity, &isc->subdev_entities, list) {
  1558. v4l2_async_nf_unregister(&subdev_entity->notifier);
  1559. v4l2_async_nf_cleanup(&subdev_entity->notifier);
  1560. }
  1561. INIT_LIST_HEAD(&isc->subdev_entities);
  1562. }
  1563. EXPORT_SYMBOL_GPL(microchip_isc_subdev_cleanup);
  1564. int microchip_isc_pipeline_init(struct isc_device *isc)
  1565. {
  1566. struct device *dev = isc->dev;
  1567. struct regmap *regmap = isc->regmap;
  1568. struct regmap_field *regs;
  1569. unsigned int i;
  1570. /*
  1571. * DPCEN-->GDCEN-->BLCEN-->WB-->CFA-->CC-->
  1572. * GAM-->VHXS-->CSC-->CBC-->SUB422-->SUB420
  1573. */
  1574. const struct reg_field regfields[ISC_PIPE_LINE_NODE_NUM] = {
  1575. REG_FIELD(ISC_DPC_CTRL, 0, 0),
  1576. REG_FIELD(ISC_DPC_CTRL, 1, 1),
  1577. REG_FIELD(ISC_DPC_CTRL, 2, 2),
  1578. REG_FIELD(ISC_WB_CTRL, 0, 0),
  1579. REG_FIELD(ISC_CFA_CTRL, 0, 0),
  1580. REG_FIELD(ISC_CC_CTRL, 0, 0),
  1581. REG_FIELD(ISC_GAM_CTRL, 0, 0),
  1582. REG_FIELD(ISC_GAM_CTRL, 1, 1),
  1583. REG_FIELD(ISC_GAM_CTRL, 2, 2),
  1584. REG_FIELD(ISC_GAM_CTRL, 3, 3),
  1585. REG_FIELD(ISC_VHXS_CTRL, 0, 0),
  1586. REG_FIELD(ISC_CSC_CTRL + isc->offsets.csc, 0, 0),
  1587. REG_FIELD(ISC_CBC_CTRL + isc->offsets.cbc, 0, 0),
  1588. REG_FIELD(ISC_SUB422_CTRL + isc->offsets.sub422, 0, 0),
  1589. REG_FIELD(ISC_SUB420_CTRL + isc->offsets.sub420, 0, 0),
  1590. };
  1591. for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
  1592. regs = devm_regmap_field_alloc(dev, regmap, regfields[i]);
  1593. if (IS_ERR(regs))
  1594. return PTR_ERR(regs);
  1595. isc->pipeline[i] = regs;
  1596. }
  1597. return 0;
  1598. }
  1599. EXPORT_SYMBOL_GPL(microchip_isc_pipeline_init);
  1600. static const struct media_entity_operations isc_entity_operations = {
  1601. .link_validate = isc_link_validate,
  1602. };
  1603. int isc_mc_init(struct isc_device *isc, u32 ver)
  1604. {
  1605. const struct of_device_id *match;
  1606. int ret;
  1607. isc->video_dev.entity.function = MEDIA_ENT_F_IO_V4L;
  1608. isc->video_dev.entity.flags = MEDIA_ENT_FL_DEFAULT;
  1609. isc->video_dev.entity.ops = &isc_entity_operations;
  1610. isc->pads[ISC_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1611. ret = media_entity_pads_init(&isc->video_dev.entity, ISC_PADS_NUM,
  1612. isc->pads);
  1613. if (ret < 0) {
  1614. dev_err(isc->dev, "media entity init failed\n");
  1615. return ret;
  1616. }
  1617. isc->mdev.dev = isc->dev;
  1618. match = of_match_node(isc->dev->driver->of_match_table,
  1619. isc->dev->of_node);
  1620. strscpy(isc->mdev.driver_name, KBUILD_MODNAME,
  1621. sizeof(isc->mdev.driver_name));
  1622. strscpy(isc->mdev.model, match->compatible, sizeof(isc->mdev.model));
  1623. isc->mdev.hw_revision = ver;
  1624. media_device_init(&isc->mdev);
  1625. isc->v4l2_dev.mdev = &isc->mdev;
  1626. return isc_scaler_init(isc);
  1627. }
  1628. EXPORT_SYMBOL_GPL(isc_mc_init);
  1629. void isc_mc_cleanup(struct isc_device *isc)
  1630. {
  1631. media_entity_cleanup(&isc->video_dev.entity);
  1632. media_device_cleanup(&isc->mdev);
  1633. }
  1634. EXPORT_SYMBOL_GPL(isc_mc_cleanup);
  1635. /* regmap configuration */
  1636. #define MICROCHIP_ISC_REG_MAX 0xd5c
  1637. const struct regmap_config microchip_isc_regmap_config = {
  1638. .reg_bits = 32,
  1639. .reg_stride = 4,
  1640. .val_bits = 32,
  1641. .max_register = MICROCHIP_ISC_REG_MAX,
  1642. };
  1643. EXPORT_SYMBOL_GPL(microchip_isc_regmap_config);
  1644. MODULE_AUTHOR("Songjun Wu");
  1645. MODULE_AUTHOR("Eugen Hristev");
  1646. MODULE_DESCRIPTION("Microchip ISC common code base");
  1647. MODULE_LICENSE("GPL v2");