cal.c 49 KB

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