cal.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  1. /*
  2. * TI CAL camera interface driver
  3. *
  4. * Copyright (c) 2015 Texas Instruments Inc.
  5. * Benoit Parrot, <bparrot@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/ioctl.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/delay.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/slab.h>
  19. #include <linux/videodev2.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_graph.h>
  22. #include <media/v4l2-of.h>
  23. #include <media/v4l2-async.h>
  24. #include <media/v4l2-common.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-event.h>
  28. #include <media/v4l2-ioctl.h>
  29. #include <media/v4l2-ctrls.h>
  30. #include <media/v4l2-fh.h>
  31. #include <media/v4l2-event.h>
  32. #include <media/v4l2-common.h>
  33. #include <media/videobuf2-core.h>
  34. #include <media/videobuf2-dma-contig.h>
  35. #include "cal_regs.h"
  36. #define CAL_MODULE_NAME "cal"
  37. #define MAX_WIDTH 1920
  38. #define MAX_HEIGHT 1200
  39. #define CAL_VERSION "0.1.0"
  40. MODULE_DESCRIPTION("TI CAL driver");
  41. MODULE_AUTHOR("Benoit Parrot, <bparrot@ti.com>");
  42. MODULE_LICENSE("GPL v2");
  43. MODULE_VERSION(CAL_VERSION);
  44. static unsigned video_nr = -1;
  45. module_param(video_nr, uint, 0644);
  46. MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect");
  47. static unsigned debug;
  48. module_param(debug, uint, 0644);
  49. MODULE_PARM_DESC(debug, "activates debug info");
  50. /* timeperframe: min/max and default */
  51. static const struct v4l2_fract
  52. tpf_default = {.numerator = 1001, .denominator = 30000};
  53. #define cal_dbg(level, caldev, fmt, arg...) \
  54. v4l2_dbg(level, debug, &caldev->v4l2_dev, fmt, ##arg)
  55. #define cal_info(caldev, fmt, arg...) \
  56. v4l2_info(&caldev->v4l2_dev, fmt, ##arg)
  57. #define cal_err(caldev, fmt, arg...) \
  58. v4l2_err(&caldev->v4l2_dev, fmt, ##arg)
  59. #define ctx_dbg(level, ctx, fmt, arg...) \
  60. v4l2_dbg(level, debug, &ctx->v4l2_dev, fmt, ##arg)
  61. #define ctx_info(ctx, fmt, arg...) \
  62. v4l2_info(&ctx->v4l2_dev, fmt, ##arg)
  63. #define ctx_err(ctx, fmt, arg...) \
  64. v4l2_err(&ctx->v4l2_dev, fmt, ##arg)
  65. #define CAL_NUM_INPUT 1
  66. #define CAL_NUM_CONTEXT 2
  67. #define bytes_per_line(pixel, bpp) (ALIGN(pixel * bpp, 16))
  68. #define reg_read(dev, offset) ioread32(dev->base + offset)
  69. #define reg_write(dev, offset, val) iowrite32(val, dev->base + offset)
  70. #define reg_read_field(dev, offset, mask) get_field(reg_read(dev, offset), \
  71. mask)
  72. #define reg_write_field(dev, offset, field, mask) { \
  73. u32 val = reg_read(dev, offset); \
  74. set_field(&val, field, mask); \
  75. reg_write(dev, offset, val); }
  76. /* ------------------------------------------------------------------
  77. * Basic structures
  78. * ------------------------------------------------------------------
  79. */
  80. struct cal_fmt {
  81. u32 fourcc;
  82. u32 code;
  83. u8 depth;
  84. };
  85. static struct cal_fmt cal_formats[] = {
  86. {
  87. .fourcc = V4L2_PIX_FMT_YUYV,
  88. .code = MEDIA_BUS_FMT_YUYV8_2X8,
  89. .depth = 16,
  90. }, {
  91. .fourcc = V4L2_PIX_FMT_UYVY,
  92. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  93. .depth = 16,
  94. }, {
  95. .fourcc = V4L2_PIX_FMT_YVYU,
  96. .code = MEDIA_BUS_FMT_YVYU8_2X8,
  97. .depth = 16,
  98. }, {
  99. .fourcc = V4L2_PIX_FMT_VYUY,
  100. .code = MEDIA_BUS_FMT_VYUY8_2X8,
  101. .depth = 16,
  102. }, {
  103. .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
  104. .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  105. .depth = 16,
  106. }, {
  107. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  108. .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
  109. .depth = 16,
  110. }, {
  111. .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
  112. .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
  113. .depth = 16,
  114. }, {
  115. .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
  116. .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
  117. .depth = 16,
  118. }, {
  119. .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
  120. .code = MEDIA_BUS_FMT_RGB888_2X12_LE,
  121. .depth = 24,
  122. }, {
  123. .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
  124. .code = MEDIA_BUS_FMT_RGB888_2X12_BE,
  125. .depth = 24,
  126. }, {
  127. .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
  128. .code = MEDIA_BUS_FMT_ARGB8888_1X32,
  129. .depth = 32,
  130. }, {
  131. .fourcc = V4L2_PIX_FMT_SBGGR8,
  132. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  133. .depth = 8,
  134. }, {
  135. .fourcc = V4L2_PIX_FMT_SGBRG8,
  136. .code = MEDIA_BUS_FMT_SGBRG8_1X8,
  137. .depth = 8,
  138. }, {
  139. .fourcc = V4L2_PIX_FMT_SGRBG8,
  140. .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  141. .depth = 8,
  142. }, {
  143. .fourcc = V4L2_PIX_FMT_SRGGB8,
  144. .code = MEDIA_BUS_FMT_SRGGB8_1X8,
  145. .depth = 8,
  146. }, {
  147. .fourcc = V4L2_PIX_FMT_SBGGR10,
  148. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  149. .depth = 16,
  150. }, {
  151. .fourcc = V4L2_PIX_FMT_SGBRG10,
  152. .code = MEDIA_BUS_FMT_SGBRG10_1X10,
  153. .depth = 16,
  154. }, {
  155. .fourcc = V4L2_PIX_FMT_SGRBG10,
  156. .code = MEDIA_BUS_FMT_SGRBG10_1X10,
  157. .depth = 16,
  158. }, {
  159. .fourcc = V4L2_PIX_FMT_SRGGB10,
  160. .code = MEDIA_BUS_FMT_SRGGB10_1X10,
  161. .depth = 16,
  162. }, {
  163. .fourcc = V4L2_PIX_FMT_SBGGR12,
  164. .code = MEDIA_BUS_FMT_SBGGR12_1X12,
  165. .depth = 16,
  166. }, {
  167. .fourcc = V4L2_PIX_FMT_SGBRG12,
  168. .code = MEDIA_BUS_FMT_SGBRG12_1X12,
  169. .depth = 16,
  170. }, {
  171. .fourcc = V4L2_PIX_FMT_SGRBG12,
  172. .code = MEDIA_BUS_FMT_SGRBG12_1X12,
  173. .depth = 16,
  174. }, {
  175. .fourcc = V4L2_PIX_FMT_SRGGB12,
  176. .code = MEDIA_BUS_FMT_SRGGB12_1X12,
  177. .depth = 16,
  178. },
  179. };
  180. /* Print Four-character-code (FOURCC) */
  181. static char *fourcc_to_str(u32 fmt)
  182. {
  183. static char code[5];
  184. code[0] = (unsigned char)(fmt & 0xff);
  185. code[1] = (unsigned char)((fmt >> 8) & 0xff);
  186. code[2] = (unsigned char)((fmt >> 16) & 0xff);
  187. code[3] = (unsigned char)((fmt >> 24) & 0xff);
  188. code[4] = '\0';
  189. return code;
  190. }
  191. /* buffer for one video frame */
  192. struct cal_buffer {
  193. /* common v4l buffer stuff -- must be first */
  194. struct vb2_v4l2_buffer vb;
  195. struct list_head list;
  196. const struct cal_fmt *fmt;
  197. };
  198. struct cal_dmaqueue {
  199. struct list_head active;
  200. /* Counters to control fps rate */
  201. int frame;
  202. int ini_jiffies;
  203. };
  204. struct cm_data {
  205. void __iomem *base;
  206. struct resource *res;
  207. unsigned int camerrx_control;
  208. struct platform_device *pdev;
  209. };
  210. struct cc_data {
  211. void __iomem *base;
  212. struct resource *res;
  213. struct platform_device *pdev;
  214. };
  215. /*
  216. * there is one cal_dev structure in the driver, it is shared by
  217. * all instances.
  218. */
  219. struct cal_dev {
  220. int irq;
  221. void __iomem *base;
  222. struct resource *res;
  223. struct platform_device *pdev;
  224. struct v4l2_device v4l2_dev;
  225. /* Control Module handle */
  226. struct cm_data *cm;
  227. /* Camera Core Module handle */
  228. struct cc_data *cc[CAL_NUM_CSI2_PORTS];
  229. struct cal_ctx *ctx[CAL_NUM_CONTEXT];
  230. };
  231. /*
  232. * There is one cal_ctx structure for each camera core context.
  233. */
  234. struct cal_ctx {
  235. struct v4l2_device v4l2_dev;
  236. struct v4l2_ctrl_handler ctrl_handler;
  237. struct video_device vdev;
  238. struct v4l2_async_notifier notifier;
  239. struct v4l2_subdev *sensor;
  240. struct v4l2_of_endpoint endpoint;
  241. struct v4l2_async_subdev asd;
  242. struct v4l2_async_subdev *asd_list[1];
  243. struct v4l2_fh fh;
  244. struct cal_dev *dev;
  245. struct cc_data *cc;
  246. /* v4l2_ioctl mutex */
  247. struct mutex mutex;
  248. /* v4l2 buffers lock */
  249. spinlock_t slock;
  250. /* Several counters */
  251. unsigned long jiffies;
  252. struct cal_dmaqueue vidq;
  253. /* Input Number */
  254. int input;
  255. /* video capture */
  256. const struct cal_fmt *fmt;
  257. /* Used to store current pixel format */
  258. struct v4l2_format v_fmt;
  259. /* Used to store current mbus frame format */
  260. struct v4l2_mbus_framefmt m_fmt;
  261. /* Current subdev enumerated format */
  262. struct cal_fmt *active_fmt[ARRAY_SIZE(cal_formats)];
  263. int num_active_fmt;
  264. struct v4l2_fract timeperframe;
  265. unsigned int sequence;
  266. unsigned int external_rate;
  267. struct vb2_queue vb_vidq;
  268. unsigned int seq_count;
  269. unsigned int csi2_port;
  270. unsigned int virtual_channel;
  271. /* Pointer pointing to current v4l2_buffer */
  272. struct cal_buffer *cur_frm;
  273. /* Pointer pointing to next v4l2_buffer */
  274. struct cal_buffer *next_frm;
  275. };
  276. static const struct cal_fmt *find_format_by_pix(struct cal_ctx *ctx,
  277. u32 pixelformat)
  278. {
  279. const struct cal_fmt *fmt;
  280. unsigned int k;
  281. for (k = 0; k < ctx->num_active_fmt; k++) {
  282. fmt = ctx->active_fmt[k];
  283. if (fmt->fourcc == pixelformat)
  284. return fmt;
  285. }
  286. return NULL;
  287. }
  288. static const struct cal_fmt *find_format_by_code(struct cal_ctx *ctx,
  289. u32 code)
  290. {
  291. const struct cal_fmt *fmt;
  292. unsigned int k;
  293. for (k = 0; k < ctx->num_active_fmt; k++) {
  294. fmt = ctx->active_fmt[k];
  295. if (fmt->code == code)
  296. return fmt;
  297. }
  298. return NULL;
  299. }
  300. static inline struct cal_ctx *notifier_to_ctx(struct v4l2_async_notifier *n)
  301. {
  302. return container_of(n, struct cal_ctx, notifier);
  303. }
  304. static inline int get_field(u32 value, u32 mask)
  305. {
  306. return (value & mask) >> __ffs(mask);
  307. }
  308. static inline void set_field(u32 *valp, u32 field, u32 mask)
  309. {
  310. u32 val = *valp;
  311. val &= ~mask;
  312. val |= (field << __ffs(mask)) & mask;
  313. *valp = val;
  314. }
  315. /*
  316. * Control Module block access
  317. */
  318. static struct cm_data *cm_create(struct cal_dev *dev)
  319. {
  320. struct platform_device *pdev = dev->pdev;
  321. struct cm_data *cm;
  322. cm = devm_kzalloc(&pdev->dev, sizeof(*cm), GFP_KERNEL);
  323. if (!cm)
  324. return ERR_PTR(-ENOMEM);
  325. cm->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  326. "camerrx_control");
  327. cm->base = devm_ioremap_resource(&pdev->dev, cm->res);
  328. if (IS_ERR(cm->base)) {
  329. cal_err(dev, "failed to ioremap\n");
  330. return ERR_CAST(cm->base);
  331. }
  332. cal_dbg(1, dev, "ioresource %s at %pa - %pa\n",
  333. cm->res->name, &cm->res->start, &cm->res->end);
  334. return cm;
  335. }
  336. static void camerarx_phy_enable(struct cal_ctx *ctx)
  337. {
  338. u32 val;
  339. if (!ctx->dev->cm->base) {
  340. ctx_err(ctx, "cm not mapped\n");
  341. return;
  342. }
  343. val = reg_read(ctx->dev->cm, CM_CTRL_CORE_CAMERRX_CONTROL);
  344. if (ctx->csi2_port == 1) {
  345. set_field(&val, 1, CM_CAMERRX_CTRL_CSI0_CTRLCLKEN_MASK);
  346. set_field(&val, 0, CM_CAMERRX_CTRL_CSI0_CAMMODE_MASK);
  347. /* enable all lanes by default */
  348. set_field(&val, 0xf, CM_CAMERRX_CTRL_CSI0_LANEENABLE_MASK);
  349. set_field(&val, 1, CM_CAMERRX_CTRL_CSI0_MODE_MASK);
  350. } else if (ctx->csi2_port == 2) {
  351. set_field(&val, 1, CM_CAMERRX_CTRL_CSI1_CTRLCLKEN_MASK);
  352. set_field(&val, 0, CM_CAMERRX_CTRL_CSI1_CAMMODE_MASK);
  353. /* enable all lanes by default */
  354. set_field(&val, 0x3, CM_CAMERRX_CTRL_CSI1_LANEENABLE_MASK);
  355. set_field(&val, 1, CM_CAMERRX_CTRL_CSI1_MODE_MASK);
  356. }
  357. reg_write(ctx->dev->cm, CM_CTRL_CORE_CAMERRX_CONTROL, val);
  358. }
  359. static void camerarx_phy_disable(struct cal_ctx *ctx)
  360. {
  361. u32 val;
  362. if (!ctx->dev->cm->base) {
  363. ctx_err(ctx, "cm not mapped\n");
  364. return;
  365. }
  366. val = reg_read(ctx->dev->cm, CM_CTRL_CORE_CAMERRX_CONTROL);
  367. if (ctx->csi2_port == 1)
  368. set_field(&val, 0x0, CM_CAMERRX_CTRL_CSI0_CTRLCLKEN_MASK);
  369. else if (ctx->csi2_port == 2)
  370. set_field(&val, 0x0, CM_CAMERRX_CTRL_CSI1_CTRLCLKEN_MASK);
  371. reg_write(ctx->dev->cm, CM_CTRL_CORE_CAMERRX_CONTROL, val);
  372. }
  373. /*
  374. * Camera Instance access block
  375. */
  376. static struct cc_data *cc_create(struct cal_dev *dev, unsigned int core)
  377. {
  378. struct platform_device *pdev = dev->pdev;
  379. struct cc_data *cc;
  380. cc = devm_kzalloc(&pdev->dev, sizeof(*cc), GFP_KERNEL);
  381. if (!cc)
  382. return ERR_PTR(-ENOMEM);
  383. cc->res = platform_get_resource_byname(pdev,
  384. IORESOURCE_MEM,
  385. (core == 0) ?
  386. "cal_rx_core0" :
  387. "cal_rx_core1");
  388. cc->base = devm_ioremap_resource(&pdev->dev, cc->res);
  389. if (IS_ERR(cc->base)) {
  390. cal_err(dev, "failed to ioremap\n");
  391. return ERR_CAST(cc->base);
  392. }
  393. cal_dbg(1, dev, "ioresource %s at %pa - %pa\n",
  394. cc->res->name, &cc->res->start, &cc->res->end);
  395. return cc;
  396. }
  397. /*
  398. * Get Revision and HW info
  399. */
  400. static void cal_get_hwinfo(struct cal_dev *dev)
  401. {
  402. u32 revision = 0;
  403. u32 hwinfo = 0;
  404. revision = reg_read(dev, CAL_HL_REVISION);
  405. cal_dbg(3, dev, "CAL_HL_REVISION = 0x%08x (expecting 0x40000200)\n",
  406. revision);
  407. hwinfo = reg_read(dev, CAL_HL_HWINFO);
  408. cal_dbg(3, dev, "CAL_HL_HWINFO = 0x%08x (expecting 0xA3C90469)\n",
  409. hwinfo);
  410. }
  411. static inline int cal_runtime_get(struct cal_dev *dev)
  412. {
  413. return pm_runtime_get_sync(&dev->pdev->dev);
  414. }
  415. static inline void cal_runtime_put(struct cal_dev *dev)
  416. {
  417. pm_runtime_put_sync(&dev->pdev->dev);
  418. }
  419. static void cal_quickdump_regs(struct cal_dev *dev)
  420. {
  421. cal_info(dev, "CAL Registers @ 0x%pa:\n", &dev->res->start);
  422. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
  423. (__force const void *)dev->base,
  424. resource_size(dev->res), false);
  425. if (dev->ctx[0]) {
  426. cal_info(dev, "CSI2 Core 0 Registers @ %pa:\n",
  427. &dev->ctx[0]->cc->res->start);
  428. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
  429. (__force const void *)dev->ctx[0]->cc->base,
  430. resource_size(dev->ctx[0]->cc->res),
  431. false);
  432. }
  433. if (dev->ctx[1]) {
  434. cal_info(dev, "CSI2 Core 1 Registers @ %pa:\n",
  435. &dev->ctx[1]->cc->res->start);
  436. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
  437. (__force const void *)dev->ctx[1]->cc->base,
  438. resource_size(dev->ctx[1]->cc->res),
  439. false);
  440. }
  441. cal_info(dev, "CAMERRX_Control Registers @ %pa:\n",
  442. &dev->cm->res->start);
  443. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
  444. (__force const void *)dev->cm->base,
  445. resource_size(dev->cm->res), false);
  446. }
  447. /*
  448. * Enable the expected IRQ sources
  449. */
  450. static void enable_irqs(struct cal_ctx *ctx)
  451. {
  452. /* Enable IRQ_WDMA_END 0/1 */
  453. reg_write_field(ctx->dev,
  454. CAL_HL_IRQENABLE_SET(2),
  455. CAL_HL_IRQ_ENABLE,
  456. CAL_HL_IRQ_MASK(ctx->csi2_port));
  457. /* Enable IRQ_WDMA_START 0/1 */
  458. reg_write_field(ctx->dev,
  459. CAL_HL_IRQENABLE_SET(3),
  460. CAL_HL_IRQ_ENABLE,
  461. CAL_HL_IRQ_MASK(ctx->csi2_port));
  462. /* Todo: Add VC_IRQ and CSI2_COMPLEXIO_IRQ handling */
  463. reg_write(ctx->dev, CAL_CSI2_VC_IRQENABLE(1), 0xFF000000);
  464. }
  465. static void disable_irqs(struct cal_ctx *ctx)
  466. {
  467. /* Disable IRQ_WDMA_END 0/1 */
  468. reg_write_field(ctx->dev,
  469. CAL_HL_IRQENABLE_CLR(2),
  470. CAL_HL_IRQ_CLEAR,
  471. CAL_HL_IRQ_MASK(ctx->csi2_port));
  472. /* Disable IRQ_WDMA_START 0/1 */
  473. reg_write_field(ctx->dev,
  474. CAL_HL_IRQENABLE_CLR(3),
  475. CAL_HL_IRQ_CLEAR,
  476. CAL_HL_IRQ_MASK(ctx->csi2_port));
  477. /* Todo: Add VC_IRQ and CSI2_COMPLEXIO_IRQ handling */
  478. reg_write(ctx->dev, CAL_CSI2_VC_IRQENABLE(1), 0);
  479. }
  480. static void csi2_init(struct cal_ctx *ctx)
  481. {
  482. int i;
  483. u32 val;
  484. val = reg_read(ctx->dev, CAL_CSI2_TIMING(ctx->csi2_port));
  485. set_field(&val, CAL_GEN_ENABLE,
  486. CAL_CSI2_TIMING_FORCE_RX_MODE_IO1_MASK);
  487. set_field(&val, CAL_GEN_ENABLE,
  488. CAL_CSI2_TIMING_STOP_STATE_X16_IO1_MASK);
  489. set_field(&val, CAL_GEN_DISABLE,
  490. CAL_CSI2_TIMING_STOP_STATE_X4_IO1_MASK);
  491. set_field(&val, 407, CAL_CSI2_TIMING_STOP_STATE_COUNTER_IO1_MASK);
  492. reg_write(ctx->dev, CAL_CSI2_TIMING(ctx->csi2_port), val);
  493. ctx_dbg(3, ctx, "CAL_CSI2_TIMING(%d) = 0x%08x\n", ctx->csi2_port,
  494. reg_read(ctx->dev, CAL_CSI2_TIMING(ctx->csi2_port)));
  495. val = reg_read(ctx->dev, CAL_CSI2_COMPLEXIO_CFG(ctx->csi2_port));
  496. set_field(&val, CAL_CSI2_COMPLEXIO_CFG_RESET_CTRL_OPERATIONAL,
  497. CAL_CSI2_COMPLEXIO_CFG_RESET_CTRL_MASK);
  498. set_field(&val, CAL_CSI2_COMPLEXIO_CFG_PWR_CMD_STATE_ON,
  499. CAL_CSI2_COMPLEXIO_CFG_PWR_CMD_MASK);
  500. reg_write(ctx->dev, CAL_CSI2_COMPLEXIO_CFG(ctx->csi2_port), val);
  501. for (i = 0; i < 10; i++) {
  502. if (reg_read_field(ctx->dev,
  503. CAL_CSI2_COMPLEXIO_CFG(ctx->csi2_port),
  504. CAL_CSI2_COMPLEXIO_CFG_PWR_STATUS_MASK) ==
  505. CAL_CSI2_COMPLEXIO_CFG_PWR_STATUS_STATE_ON)
  506. break;
  507. usleep_range(1000, 1100);
  508. }
  509. ctx_dbg(3, ctx, "CAL_CSI2_COMPLEXIO_CFG(%d) = 0x%08x\n", ctx->csi2_port,
  510. reg_read(ctx->dev, CAL_CSI2_COMPLEXIO_CFG(ctx->csi2_port)));
  511. val = reg_read(ctx->dev, CAL_CTRL);
  512. set_field(&val, CAL_CTRL_BURSTSIZE_BURST128, CAL_CTRL_BURSTSIZE_MASK);
  513. set_field(&val, 0xF, CAL_CTRL_TAGCNT_MASK);
  514. set_field(&val, CAL_CTRL_POSTED_WRITES_NONPOSTED,
  515. CAL_CTRL_POSTED_WRITES_MASK);
  516. set_field(&val, 0xFF, CAL_CTRL_MFLAGL_MASK);
  517. set_field(&val, 0xFF, CAL_CTRL_MFLAGH_MASK);
  518. reg_write(ctx->dev, CAL_CTRL, val);
  519. ctx_dbg(3, ctx, "CAL_CTRL = 0x%08x\n", reg_read(ctx->dev, CAL_CTRL));
  520. }
  521. static void csi2_lane_config(struct cal_ctx *ctx)
  522. {
  523. u32 val = reg_read(ctx->dev, CAL_CSI2_COMPLEXIO_CFG(ctx->csi2_port));
  524. u32 lane_mask = CAL_CSI2_COMPLEXIO_CFG_CLOCK_POSITION_MASK;
  525. u32 polarity_mask = CAL_CSI2_COMPLEXIO_CFG_CLOCK_POL_MASK;
  526. struct v4l2_of_bus_mipi_csi2 *mipi_csi2 = &ctx->endpoint.bus.mipi_csi2;
  527. int lane;
  528. set_field(&val, mipi_csi2->clock_lane + 1, lane_mask);
  529. set_field(&val, mipi_csi2->lane_polarities[0], polarity_mask);
  530. for (lane = 0; lane < mipi_csi2->num_data_lanes; lane++) {
  531. /*
  532. * Every lane are one nibble apart starting with the
  533. * clock followed by the data lanes so shift masks by 4.
  534. */
  535. lane_mask <<= 4;
  536. polarity_mask <<= 4;
  537. set_field(&val, mipi_csi2->data_lanes[lane] + 1, lane_mask);
  538. set_field(&val, mipi_csi2->lane_polarities[lane + 1],
  539. polarity_mask);
  540. }
  541. reg_write(ctx->dev, CAL_CSI2_COMPLEXIO_CFG(ctx->csi2_port), val);
  542. ctx_dbg(3, ctx, "CAL_CSI2_COMPLEXIO_CFG(%d) = 0x%08x\n",
  543. ctx->csi2_port, val);
  544. }
  545. static void csi2_ppi_enable(struct cal_ctx *ctx)
  546. {
  547. reg_write_field(ctx->dev, CAL_CSI2_PPI_CTRL(ctx->csi2_port),
  548. CAL_GEN_ENABLE, CAL_CSI2_PPI_CTRL_IF_EN_MASK);
  549. }
  550. static void csi2_ppi_disable(struct cal_ctx *ctx)
  551. {
  552. reg_write_field(ctx->dev, CAL_CSI2_PPI_CTRL(ctx->csi2_port),
  553. CAL_GEN_DISABLE, CAL_CSI2_PPI_CTRL_IF_EN_MASK);
  554. }
  555. static void csi2_ctx_config(struct cal_ctx *ctx)
  556. {
  557. u32 val;
  558. val = reg_read(ctx->dev, CAL_CSI2_CTX0(ctx->csi2_port));
  559. set_field(&val, ctx->csi2_port, CAL_CSI2_CTX_CPORT_MASK);
  560. /*
  561. * DT type: MIPI CSI-2 Specs
  562. * 0x1: All - DT filter is disabled
  563. * 0x24: RGB888 1 pixel = 3 bytes
  564. * 0x2B: RAW10 4 pixels = 5 bytes
  565. * 0x2A: RAW8 1 pixel = 1 byte
  566. * 0x1E: YUV422 2 pixels = 4 bytes
  567. */
  568. set_field(&val, 0x1, CAL_CSI2_CTX_DT_MASK);
  569. /* Virtual Channel from the CSI2 sensor usually 0! */
  570. set_field(&val, ctx->virtual_channel, CAL_CSI2_CTX_VC_MASK);
  571. /* NUM_LINES_PER_FRAME => 0 means auto detect */
  572. set_field(&val, 0, CAL_CSI2_CTX_LINES_MASK);
  573. set_field(&val, CAL_CSI2_CTX_ATT_PIX, CAL_CSI2_CTX_ATT_MASK);
  574. set_field(&val, CAL_CSI2_CTX_PACK_MODE_LINE,
  575. CAL_CSI2_CTX_PACK_MODE_MASK);
  576. reg_write(ctx->dev, CAL_CSI2_CTX0(ctx->csi2_port), val);
  577. ctx_dbg(3, ctx, "CAL_CSI2_CTX0(%d) = 0x%08x\n", ctx->csi2_port,
  578. reg_read(ctx->dev, CAL_CSI2_CTX0(ctx->csi2_port)));
  579. }
  580. static void pix_proc_config(struct cal_ctx *ctx)
  581. {
  582. u32 val;
  583. val = reg_read(ctx->dev, CAL_PIX_PROC(ctx->csi2_port));
  584. set_field(&val, CAL_PIX_PROC_EXTRACT_B8, CAL_PIX_PROC_EXTRACT_MASK);
  585. set_field(&val, CAL_PIX_PROC_DPCMD_BYPASS, CAL_PIX_PROC_DPCMD_MASK);
  586. set_field(&val, CAL_PIX_PROC_DPCME_BYPASS, CAL_PIX_PROC_DPCME_MASK);
  587. set_field(&val, CAL_PIX_PROC_PACK_B8, CAL_PIX_PROC_PACK_MASK);
  588. set_field(&val, ctx->csi2_port, CAL_PIX_PROC_CPORT_MASK);
  589. set_field(&val, CAL_GEN_ENABLE, CAL_PIX_PROC_EN_MASK);
  590. reg_write(ctx->dev, CAL_PIX_PROC(ctx->csi2_port), val);
  591. ctx_dbg(3, ctx, "CAL_PIX_PROC(%d) = 0x%08x\n", ctx->csi2_port,
  592. reg_read(ctx->dev, CAL_PIX_PROC(ctx->csi2_port)));
  593. }
  594. static void cal_wr_dma_config(struct cal_ctx *ctx,
  595. unsigned int width)
  596. {
  597. u32 val;
  598. val = reg_read(ctx->dev, CAL_WR_DMA_CTRL(ctx->csi2_port));
  599. set_field(&val, ctx->csi2_port, CAL_WR_DMA_CTRL_CPORT_MASK);
  600. set_field(&val, CAL_WR_DMA_CTRL_DTAG_PIX_DAT,
  601. CAL_WR_DMA_CTRL_DTAG_MASK);
  602. set_field(&val, CAL_WR_DMA_CTRL_MODE_CONST,
  603. CAL_WR_DMA_CTRL_MODE_MASK);
  604. set_field(&val, CAL_WR_DMA_CTRL_PATTERN_LINEAR,
  605. CAL_WR_DMA_CTRL_PATTERN_MASK);
  606. set_field(&val, CAL_GEN_ENABLE, CAL_WR_DMA_CTRL_STALL_RD_MASK);
  607. reg_write(ctx->dev, CAL_WR_DMA_CTRL(ctx->csi2_port), val);
  608. ctx_dbg(3, ctx, "CAL_WR_DMA_CTRL(%d) = 0x%08x\n", ctx->csi2_port,
  609. reg_read(ctx->dev, CAL_WR_DMA_CTRL(ctx->csi2_port)));
  610. /*
  611. * width/16 not sure but giving it a whirl.
  612. * zero does not work right
  613. */
  614. reg_write_field(ctx->dev,
  615. CAL_WR_DMA_OFST(ctx->csi2_port),
  616. (width / 16),
  617. CAL_WR_DMA_OFST_MASK);
  618. ctx_dbg(3, ctx, "CAL_WR_DMA_OFST(%d) = 0x%08x\n", ctx->csi2_port,
  619. reg_read(ctx->dev, CAL_WR_DMA_OFST(ctx->csi2_port)));
  620. val = reg_read(ctx->dev, CAL_WR_DMA_XSIZE(ctx->csi2_port));
  621. /* 64 bit word means no skipping */
  622. set_field(&val, 0, CAL_WR_DMA_XSIZE_XSKIP_MASK);
  623. /*
  624. * (width*8)/64 this should be size of an entire line
  625. * in 64bit word but 0 means all data until the end
  626. * is detected automagically
  627. */
  628. set_field(&val, (width / 8), CAL_WR_DMA_XSIZE_MASK);
  629. reg_write(ctx->dev, CAL_WR_DMA_XSIZE(ctx->csi2_port), val);
  630. ctx_dbg(3, ctx, "CAL_WR_DMA_XSIZE(%d) = 0x%08x\n", ctx->csi2_port,
  631. reg_read(ctx->dev, CAL_WR_DMA_XSIZE(ctx->csi2_port)));
  632. }
  633. static void cal_wr_dma_addr(struct cal_ctx *ctx, unsigned int dmaaddr)
  634. {
  635. reg_write(ctx->dev, CAL_WR_DMA_ADDR(ctx->csi2_port), dmaaddr);
  636. }
  637. /*
  638. * TCLK values are OK at their reset values
  639. */
  640. #define TCLK_TERM 0
  641. #define TCLK_MISS 1
  642. #define TCLK_SETTLE 14
  643. #define THS_SETTLE 15
  644. static void csi2_phy_config(struct cal_ctx *ctx)
  645. {
  646. unsigned int reg0, reg1;
  647. unsigned int ths_term, ths_settle;
  648. unsigned int ddrclkperiod_us;
  649. /*
  650. * THS_TERM: Programmed value = floor(20 ns/DDRClk period) - 2.
  651. */
  652. ddrclkperiod_us = ctx->external_rate / 2000000;
  653. ddrclkperiod_us = 1000000 / ddrclkperiod_us;
  654. ctx_dbg(1, ctx, "ddrclkperiod_us: %d\n", ddrclkperiod_us);
  655. ths_term = 20000 / ddrclkperiod_us;
  656. ths_term = (ths_term >= 2) ? ths_term - 2 : ths_term;
  657. ctx_dbg(1, ctx, "ths_term: %d (0x%02x)\n", ths_term, ths_term);
  658. /*
  659. * THS_SETTLE: Programmed value = floor(176.3 ns/CtrlClk period) - 1.
  660. * Since CtrlClk is fixed at 96Mhz then we get
  661. * ths_settle = floor(176.3 / 10.416) - 1 = 15
  662. * If we ever switch to a dynamic clock then this code might be useful
  663. *
  664. * unsigned int ctrlclkperiod_us;
  665. * ctrlclkperiod_us = 96000000 / 1000000;
  666. * ctrlclkperiod_us = 1000000 / ctrlclkperiod_us;
  667. * ctx_dbg(1, ctx, "ctrlclkperiod_us: %d\n", ctrlclkperiod_us);
  668. * ths_settle = 176300 / ctrlclkperiod_us;
  669. * ths_settle = (ths_settle > 1) ? ths_settle - 1 : ths_settle;
  670. */
  671. ths_settle = THS_SETTLE;
  672. ctx_dbg(1, ctx, "ths_settle: %d (0x%02x)\n", ths_settle, ths_settle);
  673. reg0 = reg_read(ctx->cc, CAL_CSI2_PHY_REG0);
  674. set_field(&reg0, CAL_CSI2_PHY_REG0_HSCLOCKCONFIG_DISABLE,
  675. CAL_CSI2_PHY_REG0_HSCLOCKCONFIG_MASK);
  676. set_field(&reg0, ths_term, CAL_CSI2_PHY_REG0_THS_TERM_MASK);
  677. set_field(&reg0, ths_settle, CAL_CSI2_PHY_REG0_THS_SETTLE_MASK);
  678. ctx_dbg(1, ctx, "CSI2_%d_REG0 = 0x%08x\n", (ctx->csi2_port - 1), reg0);
  679. reg_write(ctx->cc, CAL_CSI2_PHY_REG0, reg0);
  680. reg1 = reg_read(ctx->cc, CAL_CSI2_PHY_REG1);
  681. set_field(&reg1, TCLK_TERM, CAL_CSI2_PHY_REG1_TCLK_TERM_MASK);
  682. set_field(&reg1, 0xb8, CAL_CSI2_PHY_REG1_DPHY_HS_SYNC_PATTERN_MASK);
  683. set_field(&reg1, TCLK_MISS, CAL_CSI2_PHY_REG1_CTRLCLK_DIV_FACTOR_MASK);
  684. set_field(&reg1, TCLK_SETTLE, CAL_CSI2_PHY_REG1_TCLK_SETTLE_MASK);
  685. ctx_dbg(1, ctx, "CSI2_%d_REG1 = 0x%08x\n", (ctx->csi2_port - 1), reg1);
  686. reg_write(ctx->cc, CAL_CSI2_PHY_REG1, reg1);
  687. }
  688. static int cal_get_external_info(struct cal_ctx *ctx)
  689. {
  690. struct v4l2_ctrl *ctrl;
  691. if (!ctx->sensor)
  692. return -ENODEV;
  693. ctrl = v4l2_ctrl_find(ctx->sensor->ctrl_handler, V4L2_CID_PIXEL_RATE);
  694. if (!ctrl) {
  695. ctx_err(ctx, "no pixel rate control in subdev: %s\n",
  696. ctx->sensor->name);
  697. return -EPIPE;
  698. }
  699. ctx->external_rate = v4l2_ctrl_g_ctrl_int64(ctrl);
  700. ctx_dbg(3, ctx, "sensor Pixel Rate: %d\n", ctx->external_rate);
  701. return 0;
  702. }
  703. static inline void cal_schedule_next_buffer(struct cal_ctx *ctx)
  704. {
  705. struct cal_dmaqueue *dma_q = &ctx->vidq;
  706. struct cal_buffer *buf;
  707. unsigned long addr;
  708. buf = list_entry(dma_q->active.next, struct cal_buffer, list);
  709. ctx->next_frm = buf;
  710. list_del(&buf->list);
  711. addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
  712. cal_wr_dma_addr(ctx, addr);
  713. }
  714. static inline void cal_process_buffer_complete(struct cal_ctx *ctx)
  715. {
  716. ctx->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
  717. ctx->cur_frm->vb.field = ctx->m_fmt.field;
  718. ctx->cur_frm->vb.sequence = ctx->sequence++;
  719. vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
  720. ctx->cur_frm = ctx->next_frm;
  721. }
  722. #define isvcirqset(irq, vc, ff) (irq & \
  723. (CAL_CSI2_VC_IRQENABLE_ ##ff ##_IRQ_##vc ##_MASK))
  724. #define isportirqset(irq, port) (irq & CAL_HL_IRQ_MASK(port))
  725. static irqreturn_t cal_irq(int irq_cal, void *data)
  726. {
  727. struct cal_dev *dev = (struct cal_dev *)data;
  728. struct cal_ctx *ctx;
  729. struct cal_dmaqueue *dma_q;
  730. u32 irqst2, irqst3;
  731. /* Check which DMA just finished */
  732. irqst2 = reg_read(dev, CAL_HL_IRQSTATUS(2));
  733. if (irqst2) {
  734. /* Clear Interrupt status */
  735. reg_write(dev, CAL_HL_IRQSTATUS(2), irqst2);
  736. /* Need to check both port */
  737. if (isportirqset(irqst2, 1)) {
  738. ctx = dev->ctx[0];
  739. if (ctx->cur_frm != ctx->next_frm)
  740. cal_process_buffer_complete(ctx);
  741. }
  742. if (isportirqset(irqst2, 2)) {
  743. ctx = dev->ctx[1];
  744. if (ctx->cur_frm != ctx->next_frm)
  745. cal_process_buffer_complete(ctx);
  746. }
  747. }
  748. /* Check which DMA just started */
  749. irqst3 = reg_read(dev, CAL_HL_IRQSTATUS(3));
  750. if (irqst3) {
  751. /* Clear Interrupt status */
  752. reg_write(dev, CAL_HL_IRQSTATUS(3), irqst3);
  753. /* Need to check both port */
  754. if (isportirqset(irqst3, 1)) {
  755. ctx = dev->ctx[0];
  756. dma_q = &ctx->vidq;
  757. spin_lock(&ctx->slock);
  758. if (!list_empty(&dma_q->active) &&
  759. ctx->cur_frm == ctx->next_frm)
  760. cal_schedule_next_buffer(ctx);
  761. spin_unlock(&ctx->slock);
  762. }
  763. if (isportirqset(irqst3, 2)) {
  764. ctx = dev->ctx[1];
  765. dma_q = &ctx->vidq;
  766. spin_lock(&ctx->slock);
  767. if (!list_empty(&dma_q->active) &&
  768. ctx->cur_frm == ctx->next_frm)
  769. cal_schedule_next_buffer(ctx);
  770. spin_unlock(&ctx->slock);
  771. }
  772. }
  773. return IRQ_HANDLED;
  774. }
  775. /*
  776. * video ioctls
  777. */
  778. static int cal_querycap(struct file *file, void *priv,
  779. struct v4l2_capability *cap)
  780. {
  781. struct cal_ctx *ctx = video_drvdata(file);
  782. strlcpy(cap->driver, CAL_MODULE_NAME, sizeof(cap->driver));
  783. strlcpy(cap->card, CAL_MODULE_NAME, sizeof(cap->card));
  784. snprintf(cap->bus_info, sizeof(cap->bus_info),
  785. "platform:%s", ctx->v4l2_dev.name);
  786. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
  787. V4L2_CAP_READWRITE;
  788. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  789. return 0;
  790. }
  791. static int cal_enum_fmt_vid_cap(struct file *file, void *priv,
  792. struct v4l2_fmtdesc *f)
  793. {
  794. struct cal_ctx *ctx = video_drvdata(file);
  795. const struct cal_fmt *fmt = NULL;
  796. if (f->index >= ctx->num_active_fmt)
  797. return -EINVAL;
  798. fmt = ctx->active_fmt[f->index];
  799. f->pixelformat = fmt->fourcc;
  800. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  801. return 0;
  802. }
  803. static int __subdev_get_format(struct cal_ctx *ctx,
  804. struct v4l2_mbus_framefmt *fmt)
  805. {
  806. struct v4l2_subdev_format sd_fmt;
  807. struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
  808. int ret;
  809. sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  810. sd_fmt.pad = 0;
  811. ret = v4l2_subdev_call(ctx->sensor, pad, get_fmt, NULL, &sd_fmt);
  812. if (ret)
  813. return ret;
  814. *fmt = *mbus_fmt;
  815. ctx_dbg(1, ctx, "%s %dx%d code:%04X\n", __func__,
  816. fmt->width, fmt->height, fmt->code);
  817. return 0;
  818. }
  819. static int __subdev_set_format(struct cal_ctx *ctx,
  820. struct v4l2_mbus_framefmt *fmt)
  821. {
  822. struct v4l2_subdev_format sd_fmt;
  823. struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
  824. int ret;
  825. sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  826. sd_fmt.pad = 0;
  827. *mbus_fmt = *fmt;
  828. ret = v4l2_subdev_call(ctx->sensor, pad, set_fmt, NULL, &sd_fmt);
  829. if (ret)
  830. return ret;
  831. ctx_dbg(1, ctx, "%s %dx%d code:%04X\n", __func__,
  832. fmt->width, fmt->height, fmt->code);
  833. return 0;
  834. }
  835. static int cal_calc_format_size(struct cal_ctx *ctx,
  836. const struct cal_fmt *fmt,
  837. struct v4l2_format *f)
  838. {
  839. if (!fmt) {
  840. ctx_dbg(3, ctx, "No cal_fmt provided!\n");
  841. return -EINVAL;
  842. }
  843. v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 2,
  844. &f->fmt.pix.height, 32, MAX_HEIGHT, 0, 0);
  845. f->fmt.pix.bytesperline = bytes_per_line(f->fmt.pix.width,
  846. fmt->depth >> 3);
  847. f->fmt.pix.sizeimage = f->fmt.pix.height *
  848. f->fmt.pix.bytesperline;
  849. ctx_dbg(3, ctx, "%s: fourcc: %s size: %dx%d bpl:%d img_size:%d\n",
  850. __func__, fourcc_to_str(f->fmt.pix.pixelformat),
  851. f->fmt.pix.width, f->fmt.pix.height,
  852. f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
  853. return 0;
  854. }
  855. static int cal_g_fmt_vid_cap(struct file *file, void *priv,
  856. struct v4l2_format *f)
  857. {
  858. struct cal_ctx *ctx = video_drvdata(file);
  859. *f = ctx->v_fmt;
  860. return 0;
  861. }
  862. static int cal_try_fmt_vid_cap(struct file *file, void *priv,
  863. struct v4l2_format *f)
  864. {
  865. struct cal_ctx *ctx = video_drvdata(file);
  866. const struct cal_fmt *fmt;
  867. struct v4l2_subdev_frame_size_enum fse;
  868. int ret, found;
  869. fmt = find_format_by_pix(ctx, f->fmt.pix.pixelformat);
  870. if (!fmt) {
  871. ctx_dbg(3, ctx, "Fourcc format (0x%08x) not found.\n",
  872. f->fmt.pix.pixelformat);
  873. /* Just get the first one enumerated */
  874. fmt = ctx->active_fmt[0];
  875. f->fmt.pix.pixelformat = fmt->fourcc;
  876. }
  877. f->fmt.pix.field = ctx->v_fmt.fmt.pix.field;
  878. /* check for/find a valid width/height */
  879. ret = 0;
  880. found = false;
  881. fse.pad = 0;
  882. fse.code = fmt->code;
  883. fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  884. for (fse.index = 0; ; fse.index++) {
  885. ret = v4l2_subdev_call(ctx->sensor, pad, enum_frame_size,
  886. NULL, &fse);
  887. if (ret)
  888. break;
  889. if ((f->fmt.pix.width == fse.max_width) &&
  890. (f->fmt.pix.height == fse.max_height)) {
  891. found = true;
  892. break;
  893. } else if ((f->fmt.pix.width >= fse.min_width) &&
  894. (f->fmt.pix.width <= fse.max_width) &&
  895. (f->fmt.pix.height >= fse.min_height) &&
  896. (f->fmt.pix.height <= fse.max_height)) {
  897. found = true;
  898. break;
  899. }
  900. }
  901. if (!found) {
  902. /* use existing values as default */
  903. f->fmt.pix.width = ctx->v_fmt.fmt.pix.width;
  904. f->fmt.pix.height = ctx->v_fmt.fmt.pix.height;
  905. }
  906. /*
  907. * Use current colorspace for now, it will get
  908. * updated properly during s_fmt
  909. */
  910. f->fmt.pix.colorspace = ctx->v_fmt.fmt.pix.colorspace;
  911. return cal_calc_format_size(ctx, fmt, f);
  912. }
  913. static int cal_s_fmt_vid_cap(struct file *file, void *priv,
  914. struct v4l2_format *f)
  915. {
  916. struct cal_ctx *ctx = video_drvdata(file);
  917. struct vb2_queue *q = &ctx->vb_vidq;
  918. const struct cal_fmt *fmt;
  919. struct v4l2_mbus_framefmt mbus_fmt;
  920. int ret;
  921. if (vb2_is_busy(q)) {
  922. ctx_dbg(3, ctx, "%s device busy\n", __func__);
  923. return -EBUSY;
  924. }
  925. ret = cal_try_fmt_vid_cap(file, priv, f);
  926. if (ret < 0)
  927. return ret;
  928. fmt = find_format_by_pix(ctx, f->fmt.pix.pixelformat);
  929. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, fmt->code);
  930. ret = __subdev_set_format(ctx, &mbus_fmt);
  931. if (ret)
  932. return ret;
  933. /* Just double check nothing has gone wrong */
  934. if (mbus_fmt.code != fmt->code) {
  935. ctx_dbg(3, ctx,
  936. "%s subdev changed format on us, this should not happen\n",
  937. __func__);
  938. return -EINVAL;
  939. }
  940. v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &mbus_fmt);
  941. ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  942. ctx->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
  943. cal_calc_format_size(ctx, fmt, &ctx->v_fmt);
  944. ctx->fmt = fmt;
  945. ctx->m_fmt = mbus_fmt;
  946. *f = ctx->v_fmt;
  947. return 0;
  948. }
  949. static int cal_enum_framesizes(struct file *file, void *fh,
  950. struct v4l2_frmsizeenum *fsize)
  951. {
  952. struct cal_ctx *ctx = video_drvdata(file);
  953. const struct cal_fmt *fmt;
  954. struct v4l2_subdev_frame_size_enum fse;
  955. int ret;
  956. /* check for valid format */
  957. fmt = find_format_by_pix(ctx, fsize->pixel_format);
  958. if (!fmt) {
  959. ctx_dbg(3, ctx, "Invalid pixel code: %x\n",
  960. fsize->pixel_format);
  961. return -EINVAL;
  962. }
  963. fse.index = fsize->index;
  964. fse.pad = 0;
  965. fse.code = fmt->code;
  966. ret = v4l2_subdev_call(ctx->sensor, pad, enum_frame_size, NULL, &fse);
  967. if (ret)
  968. return ret;
  969. ctx_dbg(1, ctx, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
  970. __func__, fse.index, fse.code, fse.min_width, fse.max_width,
  971. fse.min_height, fse.max_height);
  972. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  973. fsize->discrete.width = fse.max_width;
  974. fsize->discrete.height = fse.max_height;
  975. return 0;
  976. }
  977. static int cal_enum_input(struct file *file, void *priv,
  978. struct v4l2_input *inp)
  979. {
  980. if (inp->index >= CAL_NUM_INPUT)
  981. return -EINVAL;
  982. inp->type = V4L2_INPUT_TYPE_CAMERA;
  983. sprintf(inp->name, "Camera %u", inp->index);
  984. return 0;
  985. }
  986. static int cal_g_input(struct file *file, void *priv, unsigned int *i)
  987. {
  988. struct cal_ctx *ctx = video_drvdata(file);
  989. *i = ctx->input;
  990. return 0;
  991. }
  992. static int cal_s_input(struct file *file, void *priv, unsigned int i)
  993. {
  994. struct cal_ctx *ctx = video_drvdata(file);
  995. if (i >= CAL_NUM_INPUT)
  996. return -EINVAL;
  997. ctx->input = i;
  998. return 0;
  999. }
  1000. /* timeperframe is arbitrary and continuous */
  1001. static int cal_enum_frameintervals(struct file *file, void *priv,
  1002. struct v4l2_frmivalenum *fival)
  1003. {
  1004. struct cal_ctx *ctx = video_drvdata(file);
  1005. const struct cal_fmt *fmt;
  1006. struct v4l2_subdev_frame_interval_enum fie = {
  1007. .index = fival->index,
  1008. .width = fival->width,
  1009. .height = fival->height,
  1010. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1011. };
  1012. int ret;
  1013. fmt = find_format_by_pix(ctx, fival->pixel_format);
  1014. if (!fmt)
  1015. return -EINVAL;
  1016. fie.code = fmt->code;
  1017. ret = v4l2_subdev_call(ctx->sensor, pad, enum_frame_interval,
  1018. NULL, &fie);
  1019. if (ret)
  1020. return ret;
  1021. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1022. fival->discrete = fie.interval;
  1023. return 0;
  1024. }
  1025. /*
  1026. * Videobuf operations
  1027. */
  1028. static int cal_queue_setup(struct vb2_queue *vq,
  1029. unsigned int *nbuffers, unsigned int *nplanes,
  1030. unsigned int sizes[], struct device *alloc_devs[])
  1031. {
  1032. struct cal_ctx *ctx = vb2_get_drv_priv(vq);
  1033. unsigned size = ctx->v_fmt.fmt.pix.sizeimage;
  1034. if (vq->num_buffers + *nbuffers < 3)
  1035. *nbuffers = 3 - vq->num_buffers;
  1036. if (*nplanes) {
  1037. if (sizes[0] < size)
  1038. return -EINVAL;
  1039. size = sizes[0];
  1040. }
  1041. *nplanes = 1;
  1042. sizes[0] = size;
  1043. ctx_dbg(3, ctx, "nbuffers=%d, size=%d\n", *nbuffers, sizes[0]);
  1044. return 0;
  1045. }
  1046. static int cal_buffer_prepare(struct vb2_buffer *vb)
  1047. {
  1048. struct cal_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1049. struct cal_buffer *buf = container_of(vb, struct cal_buffer,
  1050. vb.vb2_buf);
  1051. unsigned long size;
  1052. if (WARN_ON(!ctx->fmt))
  1053. return -EINVAL;
  1054. size = ctx->v_fmt.fmt.pix.sizeimage;
  1055. if (vb2_plane_size(vb, 0) < size) {
  1056. ctx_err(ctx,
  1057. "data will not fit into plane (%lu < %lu)\n",
  1058. vb2_plane_size(vb, 0), size);
  1059. return -EINVAL;
  1060. }
  1061. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
  1062. return 0;
  1063. }
  1064. static void cal_buffer_queue(struct vb2_buffer *vb)
  1065. {
  1066. struct cal_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1067. struct cal_buffer *buf = container_of(vb, struct cal_buffer,
  1068. vb.vb2_buf);
  1069. struct cal_dmaqueue *vidq = &ctx->vidq;
  1070. unsigned long flags = 0;
  1071. /* recheck locking */
  1072. spin_lock_irqsave(&ctx->slock, flags);
  1073. list_add_tail(&buf->list, &vidq->active);
  1074. spin_unlock_irqrestore(&ctx->slock, flags);
  1075. }
  1076. static int cal_start_streaming(struct vb2_queue *vq, unsigned int count)
  1077. {
  1078. struct cal_ctx *ctx = vb2_get_drv_priv(vq);
  1079. struct cal_dmaqueue *dma_q = &ctx->vidq;
  1080. struct cal_buffer *buf, *tmp;
  1081. unsigned long addr = 0;
  1082. unsigned long flags;
  1083. int ret;
  1084. spin_lock_irqsave(&ctx->slock, flags);
  1085. if (list_empty(&dma_q->active)) {
  1086. spin_unlock_irqrestore(&ctx->slock, flags);
  1087. ctx_dbg(3, ctx, "buffer queue is empty\n");
  1088. return -EIO;
  1089. }
  1090. buf = list_entry(dma_q->active.next, struct cal_buffer, list);
  1091. ctx->cur_frm = buf;
  1092. ctx->next_frm = buf;
  1093. list_del(&buf->list);
  1094. spin_unlock_irqrestore(&ctx->slock, flags);
  1095. addr = vb2_dma_contig_plane_dma_addr(&ctx->cur_frm->vb.vb2_buf, 0);
  1096. ctx->sequence = 0;
  1097. ret = cal_get_external_info(ctx);
  1098. if (ret < 0)
  1099. goto err;
  1100. cal_runtime_get(ctx->dev);
  1101. enable_irqs(ctx);
  1102. camerarx_phy_enable(ctx);
  1103. csi2_init(ctx);
  1104. csi2_phy_config(ctx);
  1105. csi2_lane_config(ctx);
  1106. csi2_ctx_config(ctx);
  1107. pix_proc_config(ctx);
  1108. cal_wr_dma_config(ctx, ctx->v_fmt.fmt.pix.bytesperline);
  1109. cal_wr_dma_addr(ctx, addr);
  1110. csi2_ppi_enable(ctx);
  1111. ret = v4l2_subdev_call(ctx->sensor, video, s_stream, 1);
  1112. if (ret) {
  1113. ctx_err(ctx, "stream on failed in subdev\n");
  1114. cal_runtime_put(ctx->dev);
  1115. goto err;
  1116. }
  1117. if (debug >= 4)
  1118. cal_quickdump_regs(ctx->dev);
  1119. return 0;
  1120. err:
  1121. list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
  1122. list_del(&buf->list);
  1123. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  1124. }
  1125. return ret;
  1126. }
  1127. static void cal_stop_streaming(struct vb2_queue *vq)
  1128. {
  1129. struct cal_ctx *ctx = vb2_get_drv_priv(vq);
  1130. struct cal_dmaqueue *dma_q = &ctx->vidq;
  1131. struct cal_buffer *buf, *tmp;
  1132. unsigned long flags;
  1133. if (v4l2_subdev_call(ctx->sensor, video, s_stream, 0))
  1134. ctx_err(ctx, "stream off failed in subdev\n");
  1135. csi2_ppi_disable(ctx);
  1136. disable_irqs(ctx);
  1137. /* Release all active buffers */
  1138. spin_lock_irqsave(&ctx->slock, flags);
  1139. list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
  1140. list_del(&buf->list);
  1141. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1142. }
  1143. if (ctx->cur_frm == ctx->next_frm) {
  1144. vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1145. } else {
  1146. vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1147. vb2_buffer_done(&ctx->next_frm->vb.vb2_buf,
  1148. VB2_BUF_STATE_ERROR);
  1149. }
  1150. ctx->cur_frm = NULL;
  1151. ctx->next_frm = NULL;
  1152. spin_unlock_irqrestore(&ctx->slock, flags);
  1153. cal_runtime_put(ctx->dev);
  1154. }
  1155. static const struct vb2_ops cal_video_qops = {
  1156. .queue_setup = cal_queue_setup,
  1157. .buf_prepare = cal_buffer_prepare,
  1158. .buf_queue = cal_buffer_queue,
  1159. .start_streaming = cal_start_streaming,
  1160. .stop_streaming = cal_stop_streaming,
  1161. .wait_prepare = vb2_ops_wait_prepare,
  1162. .wait_finish = vb2_ops_wait_finish,
  1163. };
  1164. static const struct v4l2_file_operations cal_fops = {
  1165. .owner = THIS_MODULE,
  1166. .open = v4l2_fh_open,
  1167. .release = vb2_fop_release,
  1168. .read = vb2_fop_read,
  1169. .poll = vb2_fop_poll,
  1170. .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  1171. .mmap = vb2_fop_mmap,
  1172. };
  1173. static const struct v4l2_ioctl_ops cal_ioctl_ops = {
  1174. .vidioc_querycap = cal_querycap,
  1175. .vidioc_enum_fmt_vid_cap = cal_enum_fmt_vid_cap,
  1176. .vidioc_g_fmt_vid_cap = cal_g_fmt_vid_cap,
  1177. .vidioc_try_fmt_vid_cap = cal_try_fmt_vid_cap,
  1178. .vidioc_s_fmt_vid_cap = cal_s_fmt_vid_cap,
  1179. .vidioc_enum_framesizes = cal_enum_framesizes,
  1180. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1181. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1182. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1183. .vidioc_querybuf = vb2_ioctl_querybuf,
  1184. .vidioc_qbuf = vb2_ioctl_qbuf,
  1185. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1186. .vidioc_enum_input = cal_enum_input,
  1187. .vidioc_g_input = cal_g_input,
  1188. .vidioc_s_input = cal_s_input,
  1189. .vidioc_enum_frameintervals = cal_enum_frameintervals,
  1190. .vidioc_streamon = vb2_ioctl_streamon,
  1191. .vidioc_streamoff = vb2_ioctl_streamoff,
  1192. .vidioc_log_status = v4l2_ctrl_log_status,
  1193. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1194. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1195. };
  1196. static struct video_device cal_videodev = {
  1197. .name = CAL_MODULE_NAME,
  1198. .fops = &cal_fops,
  1199. .ioctl_ops = &cal_ioctl_ops,
  1200. .minor = -1,
  1201. .release = video_device_release_empty,
  1202. };
  1203. /* -----------------------------------------------------------------
  1204. * Initialization and module stuff
  1205. * ------------------------------------------------------------------
  1206. */
  1207. static int cal_complete_ctx(struct cal_ctx *ctx);
  1208. static int cal_async_bound(struct v4l2_async_notifier *notifier,
  1209. struct v4l2_subdev *subdev,
  1210. struct v4l2_async_subdev *asd)
  1211. {
  1212. struct cal_ctx *ctx = notifier_to_ctx(notifier);
  1213. struct v4l2_subdev_mbus_code_enum mbus_code;
  1214. int ret = 0;
  1215. int i, j, k;
  1216. if (ctx->sensor) {
  1217. ctx_info(ctx, "Rejecting subdev %s (Already set!!)",
  1218. subdev->name);
  1219. return 0;
  1220. }
  1221. ctx->sensor = subdev;
  1222. ctx_dbg(1, ctx, "Using sensor %s for capture\n", subdev->name);
  1223. /* Enumerate sub device formats and enable all matching local formats */
  1224. ctx->num_active_fmt = 0;
  1225. for (j = 0, i = 0; ret != -EINVAL; ++j) {
  1226. struct cal_fmt *fmt;
  1227. memset(&mbus_code, 0, sizeof(mbus_code));
  1228. mbus_code.index = j;
  1229. ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
  1230. NULL, &mbus_code);
  1231. if (ret)
  1232. continue;
  1233. ctx_dbg(2, ctx,
  1234. "subdev %s: code: %04x idx: %d\n",
  1235. subdev->name, mbus_code.code, j);
  1236. for (k = 0; k < ARRAY_SIZE(cal_formats); k++) {
  1237. fmt = &cal_formats[k];
  1238. if (mbus_code.code == fmt->code) {
  1239. ctx->active_fmt[i] = fmt;
  1240. ctx_dbg(2, ctx,
  1241. "matched fourcc: %s: code: %04x idx: %d\n",
  1242. fourcc_to_str(fmt->fourcc),
  1243. fmt->code, i);
  1244. ctx->num_active_fmt = ++i;
  1245. }
  1246. }
  1247. }
  1248. if (i == 0) {
  1249. ctx_err(ctx, "No suitable format reported by subdev %s\n",
  1250. subdev->name);
  1251. return -EINVAL;
  1252. }
  1253. cal_complete_ctx(ctx);
  1254. return 0;
  1255. }
  1256. static int cal_async_complete(struct v4l2_async_notifier *notifier)
  1257. {
  1258. struct cal_ctx *ctx = notifier_to_ctx(notifier);
  1259. const struct cal_fmt *fmt;
  1260. struct v4l2_mbus_framefmt mbus_fmt;
  1261. int ret;
  1262. ret = __subdev_get_format(ctx, &mbus_fmt);
  1263. if (ret)
  1264. return ret;
  1265. fmt = find_format_by_code(ctx, mbus_fmt.code);
  1266. if (!fmt) {
  1267. ctx_dbg(3, ctx, "mbus code format (0x%08x) not found.\n",
  1268. mbus_fmt.code);
  1269. return -EINVAL;
  1270. }
  1271. /* Save current subdev format */
  1272. v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &mbus_fmt);
  1273. ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1274. ctx->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
  1275. cal_calc_format_size(ctx, fmt, &ctx->v_fmt);
  1276. ctx->fmt = fmt;
  1277. ctx->m_fmt = mbus_fmt;
  1278. return 0;
  1279. }
  1280. static int cal_complete_ctx(struct cal_ctx *ctx)
  1281. {
  1282. struct video_device *vfd;
  1283. struct vb2_queue *q;
  1284. int ret;
  1285. ctx->timeperframe = tpf_default;
  1286. ctx->external_rate = 192000000;
  1287. /* initialize locks */
  1288. spin_lock_init(&ctx->slock);
  1289. mutex_init(&ctx->mutex);
  1290. /* initialize queue */
  1291. q = &ctx->vb_vidq;
  1292. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1293. q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  1294. q->drv_priv = ctx;
  1295. q->buf_struct_size = sizeof(struct cal_buffer);
  1296. q->ops = &cal_video_qops;
  1297. q->mem_ops = &vb2_dma_contig_memops;
  1298. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1299. q->lock = &ctx->mutex;
  1300. q->min_buffers_needed = 3;
  1301. q->dev = ctx->v4l2_dev.dev;
  1302. ret = vb2_queue_init(q);
  1303. if (ret)
  1304. return ret;
  1305. /* init video dma queues */
  1306. INIT_LIST_HEAD(&ctx->vidq.active);
  1307. vfd = &ctx->vdev;
  1308. *vfd = cal_videodev;
  1309. vfd->v4l2_dev = &ctx->v4l2_dev;
  1310. vfd->queue = q;
  1311. /*
  1312. * Provide a mutex to v4l2 core. It will be used to protect
  1313. * all fops and v4l2 ioctls.
  1314. */
  1315. vfd->lock = &ctx->mutex;
  1316. video_set_drvdata(vfd, ctx);
  1317. ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
  1318. if (ret < 0)
  1319. return ret;
  1320. v4l2_info(&ctx->v4l2_dev, "V4L2 device registered as %s\n",
  1321. video_device_node_name(vfd));
  1322. return 0;
  1323. }
  1324. static struct device_node *
  1325. of_get_next_port(const struct device_node *parent,
  1326. struct device_node *prev)
  1327. {
  1328. struct device_node *port = NULL;
  1329. if (!parent)
  1330. return NULL;
  1331. if (!prev) {
  1332. struct device_node *ports;
  1333. /*
  1334. * It's the first call, we have to find a port subnode
  1335. * within this node or within an optional 'ports' node.
  1336. */
  1337. ports = of_get_child_by_name(parent, "ports");
  1338. if (ports)
  1339. parent = ports;
  1340. port = of_get_child_by_name(parent, "port");
  1341. /* release the 'ports' node */
  1342. of_node_put(ports);
  1343. } else {
  1344. struct device_node *ports;
  1345. ports = of_get_parent(prev);
  1346. if (!ports)
  1347. return NULL;
  1348. do {
  1349. port = of_get_next_child(ports, prev);
  1350. if (!port) {
  1351. of_node_put(ports);
  1352. return NULL;
  1353. }
  1354. prev = port;
  1355. } while (of_node_cmp(port->name, "port") != 0);
  1356. }
  1357. return port;
  1358. }
  1359. static struct device_node *
  1360. of_get_next_endpoint(const struct device_node *parent,
  1361. struct device_node *prev)
  1362. {
  1363. struct device_node *ep = NULL;
  1364. if (!parent)
  1365. return NULL;
  1366. do {
  1367. ep = of_get_next_child(parent, prev);
  1368. if (!ep)
  1369. return NULL;
  1370. prev = ep;
  1371. } while (of_node_cmp(ep->name, "endpoint") != 0);
  1372. return ep;
  1373. }
  1374. static int of_cal_create_instance(struct cal_ctx *ctx, int inst)
  1375. {
  1376. struct platform_device *pdev = ctx->dev->pdev;
  1377. struct device_node *ep_node, *port, *remote_ep,
  1378. *sensor_node, *parent;
  1379. struct v4l2_of_endpoint *endpoint;
  1380. struct v4l2_async_subdev *asd;
  1381. u32 regval = 0;
  1382. int ret, index, found_port = 0, lane;
  1383. parent = pdev->dev.of_node;
  1384. asd = &ctx->asd;
  1385. endpoint = &ctx->endpoint;
  1386. ep_node = NULL;
  1387. port = NULL;
  1388. remote_ep = NULL;
  1389. sensor_node = NULL;
  1390. ret = -EINVAL;
  1391. ctx_dbg(3, ctx, "Scanning Port node for csi2 port: %d\n", inst);
  1392. for (index = 0; index < CAL_NUM_CSI2_PORTS; index++) {
  1393. port = of_get_next_port(parent, port);
  1394. if (!port) {
  1395. ctx_dbg(1, ctx, "No port node found for csi2 port:%d\n",
  1396. index);
  1397. goto cleanup_exit;
  1398. }
  1399. /* Match the slice number with <REG> */
  1400. of_property_read_u32(port, "reg", &regval);
  1401. ctx_dbg(3, ctx, "port:%d inst:%d <reg>:%d\n",
  1402. index, inst, regval);
  1403. if ((regval == inst) && (index == inst)) {
  1404. found_port = 1;
  1405. break;
  1406. }
  1407. }
  1408. if (!found_port) {
  1409. ctx_dbg(1, ctx, "No port node matches csi2 port:%d\n",
  1410. inst);
  1411. goto cleanup_exit;
  1412. }
  1413. ctx_dbg(3, ctx, "Scanning sub-device for csi2 port: %d\n",
  1414. inst);
  1415. ep_node = of_get_next_endpoint(port, ep_node);
  1416. if (!ep_node) {
  1417. ctx_dbg(3, ctx, "can't get next endpoint\n");
  1418. goto cleanup_exit;
  1419. }
  1420. sensor_node = of_graph_get_remote_port_parent(ep_node);
  1421. if (!sensor_node) {
  1422. ctx_dbg(3, ctx, "can't get remote parent\n");
  1423. goto cleanup_exit;
  1424. }
  1425. asd->match_type = V4L2_ASYNC_MATCH_OF;
  1426. asd->match.of.node = sensor_node;
  1427. remote_ep = of_parse_phandle(ep_node, "remote-endpoint", 0);
  1428. if (!remote_ep) {
  1429. ctx_dbg(3, ctx, "can't get remote-endpoint\n");
  1430. goto cleanup_exit;
  1431. }
  1432. v4l2_of_parse_endpoint(remote_ep, endpoint);
  1433. if (endpoint->bus_type != V4L2_MBUS_CSI2) {
  1434. ctx_err(ctx, "Port:%d sub-device %s is not a CSI2 device\n",
  1435. inst, sensor_node->name);
  1436. goto cleanup_exit;
  1437. }
  1438. /* Store Virtual Channel number */
  1439. ctx->virtual_channel = endpoint->base.id;
  1440. ctx_dbg(3, ctx, "Port:%d v4l2-endpoint: CSI2\n", inst);
  1441. ctx_dbg(3, ctx, "Virtual Channel=%d\n", ctx->virtual_channel);
  1442. ctx_dbg(3, ctx, "flags=0x%08x\n", endpoint->bus.mipi_csi2.flags);
  1443. ctx_dbg(3, ctx, "clock_lane=%d\n", endpoint->bus.mipi_csi2.clock_lane);
  1444. ctx_dbg(3, ctx, "num_data_lanes=%d\n",
  1445. endpoint->bus.mipi_csi2.num_data_lanes);
  1446. ctx_dbg(3, ctx, "data_lanes= <\n");
  1447. for (lane = 0; lane < endpoint->bus.mipi_csi2.num_data_lanes; lane++)
  1448. ctx_dbg(3, ctx, "\t%d\n",
  1449. endpoint->bus.mipi_csi2.data_lanes[lane]);
  1450. ctx_dbg(3, ctx, "\t>\n");
  1451. ctx_dbg(1, ctx, "Port: %d found sub-device %s\n",
  1452. inst, sensor_node->name);
  1453. ctx->asd_list[0] = asd;
  1454. ctx->notifier.subdevs = ctx->asd_list;
  1455. ctx->notifier.num_subdevs = 1;
  1456. ctx->notifier.bound = cal_async_bound;
  1457. ctx->notifier.complete = cal_async_complete;
  1458. ret = v4l2_async_notifier_register(&ctx->v4l2_dev,
  1459. &ctx->notifier);
  1460. if (ret) {
  1461. ctx_err(ctx, "Error registering async notifier\n");
  1462. ret = -EINVAL;
  1463. }
  1464. cleanup_exit:
  1465. if (remote_ep)
  1466. of_node_put(remote_ep);
  1467. if (sensor_node)
  1468. of_node_put(sensor_node);
  1469. if (ep_node)
  1470. of_node_put(ep_node);
  1471. if (port)
  1472. of_node_put(port);
  1473. return ret;
  1474. }
  1475. static struct cal_ctx *cal_create_instance(struct cal_dev *dev, int inst)
  1476. {
  1477. struct cal_ctx *ctx;
  1478. struct v4l2_ctrl_handler *hdl;
  1479. int ret;
  1480. ctx = devm_kzalloc(&dev->pdev->dev, sizeof(*ctx), GFP_KERNEL);
  1481. if (!ctx)
  1482. return NULL;
  1483. /* save the cal_dev * for future ref */
  1484. ctx->dev = dev;
  1485. snprintf(ctx->v4l2_dev.name, sizeof(ctx->v4l2_dev.name),
  1486. "%s-%03d", CAL_MODULE_NAME, inst);
  1487. ret = v4l2_device_register(&dev->pdev->dev, &ctx->v4l2_dev);
  1488. if (ret)
  1489. goto err_exit;
  1490. hdl = &ctx->ctrl_handler;
  1491. ret = v4l2_ctrl_handler_init(hdl, 11);
  1492. if (ret) {
  1493. ctx_err(ctx, "Failed to init ctrl handler\n");
  1494. goto unreg_dev;
  1495. }
  1496. ctx->v4l2_dev.ctrl_handler = hdl;
  1497. /* Make sure Camera Core H/W register area is available */
  1498. ctx->cc = dev->cc[inst];
  1499. /* Store the instance id */
  1500. ctx->csi2_port = inst + 1;
  1501. ret = of_cal_create_instance(ctx, inst);
  1502. if (ret) {
  1503. ret = -EINVAL;
  1504. goto free_hdl;
  1505. }
  1506. return ctx;
  1507. free_hdl:
  1508. v4l2_ctrl_handler_free(hdl);
  1509. unreg_dev:
  1510. v4l2_device_unregister(&ctx->v4l2_dev);
  1511. err_exit:
  1512. return NULL;
  1513. }
  1514. static int cal_probe(struct platform_device *pdev)
  1515. {
  1516. struct cal_dev *dev;
  1517. int ret;
  1518. int irq;
  1519. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  1520. if (!dev)
  1521. return -ENOMEM;
  1522. /* set pseudo v4l2 device name so we can use v4l2_printk */
  1523. strlcpy(dev->v4l2_dev.name, CAL_MODULE_NAME,
  1524. sizeof(dev->v4l2_dev.name));
  1525. /* save pdev pointer */
  1526. dev->pdev = pdev;
  1527. dev->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1528. "cal_top");
  1529. dev->base = devm_ioremap_resource(&pdev->dev, dev->res);
  1530. if (IS_ERR(dev->base))
  1531. return PTR_ERR(dev->base);
  1532. cal_dbg(1, dev, "ioresource %s at %pa - %pa\n",
  1533. dev->res->name, &dev->res->start, &dev->res->end);
  1534. irq = platform_get_irq(pdev, 0);
  1535. cal_dbg(1, dev, "got irq# %d\n", irq);
  1536. ret = devm_request_irq(&pdev->dev, irq, cal_irq, 0, CAL_MODULE_NAME,
  1537. dev);
  1538. if (ret)
  1539. return ret;
  1540. platform_set_drvdata(pdev, dev);
  1541. dev->cm = cm_create(dev);
  1542. if (IS_ERR(dev->cm))
  1543. return PTR_ERR(dev->cm);
  1544. dev->cc[0] = cc_create(dev, 0);
  1545. if (IS_ERR(dev->cc[0]))
  1546. return PTR_ERR(dev->cc[0]);
  1547. dev->cc[1] = cc_create(dev, 1);
  1548. if (IS_ERR(dev->cc[1]))
  1549. return PTR_ERR(dev->cc[1]);
  1550. dev->ctx[0] = NULL;
  1551. dev->ctx[1] = NULL;
  1552. dev->ctx[0] = cal_create_instance(dev, 0);
  1553. dev->ctx[1] = cal_create_instance(dev, 1);
  1554. if (!dev->ctx[0] && !dev->ctx[1]) {
  1555. cal_err(dev, "Neither port is configured, no point in staying up\n");
  1556. return -ENODEV;
  1557. }
  1558. pm_runtime_enable(&pdev->dev);
  1559. ret = cal_runtime_get(dev);
  1560. if (ret)
  1561. goto runtime_disable;
  1562. /* Just check we can actually access the module */
  1563. cal_get_hwinfo(dev);
  1564. cal_runtime_put(dev);
  1565. return 0;
  1566. runtime_disable:
  1567. pm_runtime_disable(&pdev->dev);
  1568. return ret;
  1569. }
  1570. static int cal_remove(struct platform_device *pdev)
  1571. {
  1572. struct cal_dev *dev =
  1573. (struct cal_dev *)platform_get_drvdata(pdev);
  1574. struct cal_ctx *ctx;
  1575. int i;
  1576. cal_dbg(1, dev, "Removing %s\n", CAL_MODULE_NAME);
  1577. cal_runtime_get(dev);
  1578. for (i = 0; i < CAL_NUM_CONTEXT; i++) {
  1579. ctx = dev->ctx[i];
  1580. if (ctx) {
  1581. ctx_dbg(1, ctx, "unregistering %s\n",
  1582. video_device_node_name(&ctx->vdev));
  1583. camerarx_phy_disable(ctx);
  1584. v4l2_async_notifier_unregister(&ctx->notifier);
  1585. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1586. v4l2_device_unregister(&ctx->v4l2_dev);
  1587. video_unregister_device(&ctx->vdev);
  1588. }
  1589. }
  1590. cal_runtime_put(dev);
  1591. pm_runtime_disable(&pdev->dev);
  1592. return 0;
  1593. }
  1594. #if defined(CONFIG_OF)
  1595. static const struct of_device_id cal_of_match[] = {
  1596. { .compatible = "ti,dra72-cal", },
  1597. {},
  1598. };
  1599. MODULE_DEVICE_TABLE(of, cal_of_match);
  1600. #endif
  1601. static struct platform_driver cal_pdrv = {
  1602. .probe = cal_probe,
  1603. .remove = cal_remove,
  1604. .driver = {
  1605. .name = CAL_MODULE_NAME,
  1606. .of_match_table = of_match_ptr(cal_of_match),
  1607. },
  1608. };
  1609. module_platform_driver(cal_pdrv);