cal.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  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. u32 val;
  465. /* Disable IRQ_WDMA_END 0/1 */
  466. val = 0;
  467. set_field(&val, CAL_HL_IRQ_CLEAR, CAL_HL_IRQ_MASK(ctx->csi2_port));
  468. reg_write(ctx->dev, CAL_HL_IRQENABLE_CLR(2), val);
  469. /* Disable IRQ_WDMA_START 0/1 */
  470. val = 0;
  471. set_field(&val, CAL_HL_IRQ_CLEAR, CAL_HL_IRQ_MASK(ctx->csi2_port));
  472. reg_write(ctx->dev, CAL_HL_IRQENABLE_CLR(3), val);
  473. /* Todo: Add VC_IRQ and CSI2_COMPLEXIO_IRQ handling */
  474. reg_write(ctx->dev, CAL_CSI2_VC_IRQENABLE(1), 0);
  475. }
  476. static void csi2_init(struct cal_ctx *ctx)
  477. {
  478. int i;
  479. u32 val;
  480. val = reg_read(ctx->dev, CAL_CSI2_TIMING(ctx->csi2_port));
  481. set_field(&val, CAL_GEN_ENABLE,
  482. CAL_CSI2_TIMING_FORCE_RX_MODE_IO1_MASK);
  483. set_field(&val, CAL_GEN_ENABLE,
  484. CAL_CSI2_TIMING_STOP_STATE_X16_IO1_MASK);
  485. set_field(&val, CAL_GEN_DISABLE,
  486. CAL_CSI2_TIMING_STOP_STATE_X4_IO1_MASK);
  487. set_field(&val, 407, CAL_CSI2_TIMING_STOP_STATE_COUNTER_IO1_MASK);
  488. reg_write(ctx->dev, CAL_CSI2_TIMING(ctx->csi2_port), val);
  489. ctx_dbg(3, ctx, "CAL_CSI2_TIMING(%d) = 0x%08x\n", ctx->csi2_port,
  490. reg_read(ctx->dev, CAL_CSI2_TIMING(ctx->csi2_port)));
  491. val = reg_read(ctx->dev, CAL_CSI2_COMPLEXIO_CFG(ctx->csi2_port));
  492. set_field(&val, CAL_CSI2_COMPLEXIO_CFG_RESET_CTRL_OPERATIONAL,
  493. CAL_CSI2_COMPLEXIO_CFG_RESET_CTRL_MASK);
  494. set_field(&val, CAL_CSI2_COMPLEXIO_CFG_PWR_CMD_STATE_ON,
  495. CAL_CSI2_COMPLEXIO_CFG_PWR_CMD_MASK);
  496. reg_write(ctx->dev, CAL_CSI2_COMPLEXIO_CFG(ctx->csi2_port), val);
  497. for (i = 0; i < 10; i++) {
  498. if (reg_read_field(ctx->dev,
  499. CAL_CSI2_COMPLEXIO_CFG(ctx->csi2_port),
  500. CAL_CSI2_COMPLEXIO_CFG_PWR_STATUS_MASK) ==
  501. CAL_CSI2_COMPLEXIO_CFG_PWR_STATUS_STATE_ON)
  502. break;
  503. usleep_range(1000, 1100);
  504. }
  505. ctx_dbg(3, ctx, "CAL_CSI2_COMPLEXIO_CFG(%d) = 0x%08x\n", ctx->csi2_port,
  506. reg_read(ctx->dev, CAL_CSI2_COMPLEXIO_CFG(ctx->csi2_port)));
  507. val = reg_read(ctx->dev, CAL_CTRL);
  508. set_field(&val, CAL_CTRL_BURSTSIZE_BURST128, CAL_CTRL_BURSTSIZE_MASK);
  509. set_field(&val, 0xF, CAL_CTRL_TAGCNT_MASK);
  510. set_field(&val, CAL_CTRL_POSTED_WRITES_NONPOSTED,
  511. CAL_CTRL_POSTED_WRITES_MASK);
  512. set_field(&val, 0xFF, CAL_CTRL_MFLAGL_MASK);
  513. set_field(&val, 0xFF, CAL_CTRL_MFLAGH_MASK);
  514. reg_write(ctx->dev, CAL_CTRL, val);
  515. ctx_dbg(3, ctx, "CAL_CTRL = 0x%08x\n", reg_read(ctx->dev, CAL_CTRL));
  516. }
  517. static void csi2_lane_config(struct cal_ctx *ctx)
  518. {
  519. u32 val = reg_read(ctx->dev, CAL_CSI2_COMPLEXIO_CFG(ctx->csi2_port));
  520. u32 lane_mask = CAL_CSI2_COMPLEXIO_CFG_CLOCK_POSITION_MASK;
  521. u32 polarity_mask = CAL_CSI2_COMPLEXIO_CFG_CLOCK_POL_MASK;
  522. struct v4l2_fwnode_bus_mipi_csi2 *mipi_csi2 =
  523. &ctx->endpoint.bus.mipi_csi2;
  524. int lane;
  525. set_field(&val, mipi_csi2->clock_lane + 1, lane_mask);
  526. set_field(&val, mipi_csi2->lane_polarities[0], polarity_mask);
  527. for (lane = 0; lane < mipi_csi2->num_data_lanes; lane++) {
  528. /*
  529. * Every lane are one nibble apart starting with the
  530. * clock followed by the data lanes so shift masks by 4.
  531. */
  532. lane_mask <<= 4;
  533. polarity_mask <<= 4;
  534. set_field(&val, mipi_csi2->data_lanes[lane] + 1, lane_mask);
  535. set_field(&val, mipi_csi2->lane_polarities[lane + 1],
  536. polarity_mask);
  537. }
  538. reg_write(ctx->dev, CAL_CSI2_COMPLEXIO_CFG(ctx->csi2_port), val);
  539. ctx_dbg(3, ctx, "CAL_CSI2_COMPLEXIO_CFG(%d) = 0x%08x\n",
  540. ctx->csi2_port, val);
  541. }
  542. static void csi2_ppi_enable(struct cal_ctx *ctx)
  543. {
  544. reg_write_field(ctx->dev, CAL_CSI2_PPI_CTRL(ctx->csi2_port),
  545. CAL_GEN_ENABLE, CAL_CSI2_PPI_CTRL_IF_EN_MASK);
  546. }
  547. static void csi2_ppi_disable(struct cal_ctx *ctx)
  548. {
  549. reg_write_field(ctx->dev, CAL_CSI2_PPI_CTRL(ctx->csi2_port),
  550. CAL_GEN_DISABLE, CAL_CSI2_PPI_CTRL_IF_EN_MASK);
  551. }
  552. static void csi2_ctx_config(struct cal_ctx *ctx)
  553. {
  554. u32 val;
  555. val = reg_read(ctx->dev, CAL_CSI2_CTX0(ctx->csi2_port));
  556. set_field(&val, ctx->csi2_port, CAL_CSI2_CTX_CPORT_MASK);
  557. /*
  558. * DT type: MIPI CSI-2 Specs
  559. * 0x1: All - DT filter is disabled
  560. * 0x24: RGB888 1 pixel = 3 bytes
  561. * 0x2B: RAW10 4 pixels = 5 bytes
  562. * 0x2A: RAW8 1 pixel = 1 byte
  563. * 0x1E: YUV422 2 pixels = 4 bytes
  564. */
  565. set_field(&val, 0x1, CAL_CSI2_CTX_DT_MASK);
  566. /* Virtual Channel from the CSI2 sensor usually 0! */
  567. set_field(&val, ctx->virtual_channel, CAL_CSI2_CTX_VC_MASK);
  568. /* NUM_LINES_PER_FRAME => 0 means auto detect */
  569. set_field(&val, 0, CAL_CSI2_CTX_LINES_MASK);
  570. set_field(&val, CAL_CSI2_CTX_ATT_PIX, CAL_CSI2_CTX_ATT_MASK);
  571. set_field(&val, CAL_CSI2_CTX_PACK_MODE_LINE,
  572. CAL_CSI2_CTX_PACK_MODE_MASK);
  573. reg_write(ctx->dev, CAL_CSI2_CTX0(ctx->csi2_port), val);
  574. ctx_dbg(3, ctx, "CAL_CSI2_CTX0(%d) = 0x%08x\n", ctx->csi2_port,
  575. reg_read(ctx->dev, CAL_CSI2_CTX0(ctx->csi2_port)));
  576. }
  577. static void pix_proc_config(struct cal_ctx *ctx)
  578. {
  579. u32 val;
  580. val = reg_read(ctx->dev, CAL_PIX_PROC(ctx->csi2_port));
  581. set_field(&val, CAL_PIX_PROC_EXTRACT_B8, CAL_PIX_PROC_EXTRACT_MASK);
  582. set_field(&val, CAL_PIX_PROC_DPCMD_BYPASS, CAL_PIX_PROC_DPCMD_MASK);
  583. set_field(&val, CAL_PIX_PROC_DPCME_BYPASS, CAL_PIX_PROC_DPCME_MASK);
  584. set_field(&val, CAL_PIX_PROC_PACK_B8, CAL_PIX_PROC_PACK_MASK);
  585. set_field(&val, ctx->csi2_port, CAL_PIX_PROC_CPORT_MASK);
  586. set_field(&val, CAL_GEN_ENABLE, CAL_PIX_PROC_EN_MASK);
  587. reg_write(ctx->dev, CAL_PIX_PROC(ctx->csi2_port), val);
  588. ctx_dbg(3, ctx, "CAL_PIX_PROC(%d) = 0x%08x\n", ctx->csi2_port,
  589. reg_read(ctx->dev, CAL_PIX_PROC(ctx->csi2_port)));
  590. }
  591. static void cal_wr_dma_config(struct cal_ctx *ctx,
  592. unsigned int width, unsigned int height)
  593. {
  594. u32 val;
  595. val = reg_read(ctx->dev, CAL_WR_DMA_CTRL(ctx->csi2_port));
  596. set_field(&val, ctx->csi2_port, CAL_WR_DMA_CTRL_CPORT_MASK);
  597. set_field(&val, height, CAL_WR_DMA_CTRL_YSIZE_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. ctx->v_fmt.fmt.pix.height);
  1108. cal_wr_dma_addr(ctx, addr);
  1109. csi2_ppi_enable(ctx);
  1110. ret = v4l2_subdev_call(ctx->sensor, video, s_stream, 1);
  1111. if (ret) {
  1112. ctx_err(ctx, "stream on failed in subdev\n");
  1113. cal_runtime_put(ctx->dev);
  1114. goto err;
  1115. }
  1116. if (debug >= 4)
  1117. cal_quickdump_regs(ctx->dev);
  1118. return 0;
  1119. err:
  1120. list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
  1121. list_del(&buf->list);
  1122. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  1123. }
  1124. return ret;
  1125. }
  1126. static void cal_stop_streaming(struct vb2_queue *vq)
  1127. {
  1128. struct cal_ctx *ctx = vb2_get_drv_priv(vq);
  1129. struct cal_dmaqueue *dma_q = &ctx->vidq;
  1130. struct cal_buffer *buf, *tmp;
  1131. unsigned long flags;
  1132. if (v4l2_subdev_call(ctx->sensor, video, s_stream, 0))
  1133. ctx_err(ctx, "stream off failed in subdev\n");
  1134. csi2_ppi_disable(ctx);
  1135. disable_irqs(ctx);
  1136. /* Release all active buffers */
  1137. spin_lock_irqsave(&ctx->slock, flags);
  1138. list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
  1139. list_del(&buf->list);
  1140. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1141. }
  1142. if (ctx->cur_frm == ctx->next_frm) {
  1143. vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1144. } else {
  1145. vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1146. vb2_buffer_done(&ctx->next_frm->vb.vb2_buf,
  1147. VB2_BUF_STATE_ERROR);
  1148. }
  1149. ctx->cur_frm = NULL;
  1150. ctx->next_frm = NULL;
  1151. spin_unlock_irqrestore(&ctx->slock, flags);
  1152. cal_runtime_put(ctx->dev);
  1153. }
  1154. static const struct vb2_ops cal_video_qops = {
  1155. .queue_setup = cal_queue_setup,
  1156. .buf_prepare = cal_buffer_prepare,
  1157. .buf_queue = cal_buffer_queue,
  1158. .start_streaming = cal_start_streaming,
  1159. .stop_streaming = cal_stop_streaming,
  1160. .wait_prepare = vb2_ops_wait_prepare,
  1161. .wait_finish = vb2_ops_wait_finish,
  1162. };
  1163. static const struct v4l2_file_operations cal_fops = {
  1164. .owner = THIS_MODULE,
  1165. .open = v4l2_fh_open,
  1166. .release = vb2_fop_release,
  1167. .read = vb2_fop_read,
  1168. .poll = vb2_fop_poll,
  1169. .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  1170. .mmap = vb2_fop_mmap,
  1171. };
  1172. static const struct v4l2_ioctl_ops cal_ioctl_ops = {
  1173. .vidioc_querycap = cal_querycap,
  1174. .vidioc_enum_fmt_vid_cap = cal_enum_fmt_vid_cap,
  1175. .vidioc_g_fmt_vid_cap = cal_g_fmt_vid_cap,
  1176. .vidioc_try_fmt_vid_cap = cal_try_fmt_vid_cap,
  1177. .vidioc_s_fmt_vid_cap = cal_s_fmt_vid_cap,
  1178. .vidioc_enum_framesizes = cal_enum_framesizes,
  1179. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1180. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1181. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1182. .vidioc_querybuf = vb2_ioctl_querybuf,
  1183. .vidioc_qbuf = vb2_ioctl_qbuf,
  1184. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1185. .vidioc_enum_input = cal_enum_input,
  1186. .vidioc_g_input = cal_g_input,
  1187. .vidioc_s_input = cal_s_input,
  1188. .vidioc_enum_frameintervals = cal_enum_frameintervals,
  1189. .vidioc_streamon = vb2_ioctl_streamon,
  1190. .vidioc_streamoff = vb2_ioctl_streamoff,
  1191. .vidioc_log_status = v4l2_ctrl_log_status,
  1192. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1193. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1194. };
  1195. static const struct video_device cal_videodev = {
  1196. .name = CAL_MODULE_NAME,
  1197. .fops = &cal_fops,
  1198. .ioctl_ops = &cal_ioctl_ops,
  1199. .minor = -1,
  1200. .release = video_device_release_empty,
  1201. };
  1202. /* -----------------------------------------------------------------
  1203. * Initialization and module stuff
  1204. * ------------------------------------------------------------------
  1205. */
  1206. static int cal_complete_ctx(struct cal_ctx *ctx);
  1207. static int cal_async_bound(struct v4l2_async_notifier *notifier,
  1208. struct v4l2_subdev *subdev,
  1209. struct v4l2_async_subdev *asd)
  1210. {
  1211. struct cal_ctx *ctx = notifier_to_ctx(notifier);
  1212. struct v4l2_subdev_mbus_code_enum mbus_code;
  1213. int ret = 0;
  1214. int i, j, k;
  1215. if (ctx->sensor) {
  1216. ctx_info(ctx, "Rejecting subdev %s (Already set!!)",
  1217. subdev->name);
  1218. return 0;
  1219. }
  1220. ctx->sensor = subdev;
  1221. ctx_dbg(1, ctx, "Using sensor %s for capture\n", subdev->name);
  1222. /* Enumerate sub device formats and enable all matching local formats */
  1223. ctx->num_active_fmt = 0;
  1224. for (j = 0, i = 0; ret != -EINVAL; ++j) {
  1225. struct cal_fmt *fmt;
  1226. memset(&mbus_code, 0, sizeof(mbus_code));
  1227. mbus_code.index = j;
  1228. ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
  1229. NULL, &mbus_code);
  1230. if (ret)
  1231. continue;
  1232. ctx_dbg(2, ctx,
  1233. "subdev %s: code: %04x idx: %d\n",
  1234. subdev->name, mbus_code.code, j);
  1235. for (k = 0; k < ARRAY_SIZE(cal_formats); k++) {
  1236. fmt = &cal_formats[k];
  1237. if (mbus_code.code == fmt->code) {
  1238. ctx->active_fmt[i] = fmt;
  1239. ctx_dbg(2, ctx,
  1240. "matched fourcc: %s: code: %04x idx: %d\n",
  1241. fourcc_to_str(fmt->fourcc),
  1242. fmt->code, i);
  1243. ctx->num_active_fmt = ++i;
  1244. }
  1245. }
  1246. }
  1247. if (i == 0) {
  1248. ctx_err(ctx, "No suitable format reported by subdev %s\n",
  1249. subdev->name);
  1250. return -EINVAL;
  1251. }
  1252. cal_complete_ctx(ctx);
  1253. return 0;
  1254. }
  1255. static int cal_async_complete(struct v4l2_async_notifier *notifier)
  1256. {
  1257. struct cal_ctx *ctx = notifier_to_ctx(notifier);
  1258. const struct cal_fmt *fmt;
  1259. struct v4l2_mbus_framefmt mbus_fmt;
  1260. int ret;
  1261. ret = __subdev_get_format(ctx, &mbus_fmt);
  1262. if (ret)
  1263. return ret;
  1264. fmt = find_format_by_code(ctx, mbus_fmt.code);
  1265. if (!fmt) {
  1266. ctx_dbg(3, ctx, "mbus code format (0x%08x) not found.\n",
  1267. mbus_fmt.code);
  1268. return -EINVAL;
  1269. }
  1270. /* Save current subdev format */
  1271. v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &mbus_fmt);
  1272. ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1273. ctx->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
  1274. cal_calc_format_size(ctx, fmt, &ctx->v_fmt);
  1275. ctx->fmt = fmt;
  1276. ctx->m_fmt = mbus_fmt;
  1277. return 0;
  1278. }
  1279. static const struct v4l2_async_notifier_operations cal_async_ops = {
  1280. .bound = cal_async_bound,
  1281. .complete = cal_async_complete,
  1282. };
  1283. static int cal_complete_ctx(struct cal_ctx *ctx)
  1284. {
  1285. struct video_device *vfd;
  1286. struct vb2_queue *q;
  1287. int ret;
  1288. ctx->timeperframe = tpf_default;
  1289. ctx->external_rate = 192000000;
  1290. /* initialize locks */
  1291. spin_lock_init(&ctx->slock);
  1292. mutex_init(&ctx->mutex);
  1293. /* initialize queue */
  1294. q = &ctx->vb_vidq;
  1295. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1296. q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  1297. q->drv_priv = ctx;
  1298. q->buf_struct_size = sizeof(struct cal_buffer);
  1299. q->ops = &cal_video_qops;
  1300. q->mem_ops = &vb2_dma_contig_memops;
  1301. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1302. q->lock = &ctx->mutex;
  1303. q->min_buffers_needed = 3;
  1304. q->dev = ctx->v4l2_dev.dev;
  1305. ret = vb2_queue_init(q);
  1306. if (ret)
  1307. return ret;
  1308. /* init video dma queues */
  1309. INIT_LIST_HEAD(&ctx->vidq.active);
  1310. vfd = &ctx->vdev;
  1311. *vfd = cal_videodev;
  1312. vfd->v4l2_dev = &ctx->v4l2_dev;
  1313. vfd->queue = q;
  1314. /*
  1315. * Provide a mutex to v4l2 core. It will be used to protect
  1316. * all fops and v4l2 ioctls.
  1317. */
  1318. vfd->lock = &ctx->mutex;
  1319. video_set_drvdata(vfd, ctx);
  1320. ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
  1321. if (ret < 0)
  1322. return ret;
  1323. v4l2_info(&ctx->v4l2_dev, "V4L2 device registered as %s\n",
  1324. video_device_node_name(vfd));
  1325. return 0;
  1326. }
  1327. static struct device_node *
  1328. of_get_next_port(const struct device_node *parent,
  1329. struct device_node *prev)
  1330. {
  1331. struct device_node *port = NULL;
  1332. if (!parent)
  1333. return NULL;
  1334. if (!prev) {
  1335. struct device_node *ports;
  1336. /*
  1337. * It's the first call, we have to find a port subnode
  1338. * within this node or within an optional 'ports' node.
  1339. */
  1340. ports = of_get_child_by_name(parent, "ports");
  1341. if (ports)
  1342. parent = ports;
  1343. port = of_get_child_by_name(parent, "port");
  1344. /* release the 'ports' node */
  1345. of_node_put(ports);
  1346. } else {
  1347. struct device_node *ports;
  1348. ports = of_get_parent(prev);
  1349. if (!ports)
  1350. return NULL;
  1351. do {
  1352. port = of_get_next_child(ports, prev);
  1353. if (!port) {
  1354. of_node_put(ports);
  1355. return NULL;
  1356. }
  1357. prev = port;
  1358. } while (of_node_cmp(port->name, "port") != 0);
  1359. }
  1360. return port;
  1361. }
  1362. static struct device_node *
  1363. of_get_next_endpoint(const struct device_node *parent,
  1364. struct device_node *prev)
  1365. {
  1366. struct device_node *ep = NULL;
  1367. if (!parent)
  1368. return NULL;
  1369. do {
  1370. ep = of_get_next_child(parent, prev);
  1371. if (!ep)
  1372. return NULL;
  1373. prev = ep;
  1374. } while (of_node_cmp(ep->name, "endpoint") != 0);
  1375. return ep;
  1376. }
  1377. static int of_cal_create_instance(struct cal_ctx *ctx, int inst)
  1378. {
  1379. struct platform_device *pdev = ctx->dev->pdev;
  1380. struct device_node *ep_node, *port, *remote_ep,
  1381. *sensor_node, *parent;
  1382. struct v4l2_fwnode_endpoint *endpoint;
  1383. struct v4l2_async_subdev *asd;
  1384. u32 regval = 0;
  1385. int ret, index, found_port = 0, lane;
  1386. parent = pdev->dev.of_node;
  1387. asd = &ctx->asd;
  1388. endpoint = &ctx->endpoint;
  1389. ep_node = NULL;
  1390. port = NULL;
  1391. remote_ep = NULL;
  1392. sensor_node = NULL;
  1393. ret = -EINVAL;
  1394. ctx_dbg(3, ctx, "Scanning Port node for csi2 port: %d\n", inst);
  1395. for (index = 0; index < CAL_NUM_CSI2_PORTS; index++) {
  1396. port = of_get_next_port(parent, port);
  1397. if (!port) {
  1398. ctx_dbg(1, ctx, "No port node found for csi2 port:%d\n",
  1399. index);
  1400. goto cleanup_exit;
  1401. }
  1402. /* Match the slice number with <REG> */
  1403. of_property_read_u32(port, "reg", &regval);
  1404. ctx_dbg(3, ctx, "port:%d inst:%d <reg>:%d\n",
  1405. index, inst, regval);
  1406. if ((regval == inst) && (index == inst)) {
  1407. found_port = 1;
  1408. break;
  1409. }
  1410. }
  1411. if (!found_port) {
  1412. ctx_dbg(1, ctx, "No port node matches csi2 port:%d\n",
  1413. inst);
  1414. goto cleanup_exit;
  1415. }
  1416. ctx_dbg(3, ctx, "Scanning sub-device for csi2 port: %d\n",
  1417. inst);
  1418. ep_node = of_get_next_endpoint(port, ep_node);
  1419. if (!ep_node) {
  1420. ctx_dbg(3, ctx, "can't get next endpoint\n");
  1421. goto cleanup_exit;
  1422. }
  1423. sensor_node = of_graph_get_remote_port_parent(ep_node);
  1424. if (!sensor_node) {
  1425. ctx_dbg(3, ctx, "can't get remote parent\n");
  1426. goto cleanup_exit;
  1427. }
  1428. asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
  1429. asd->match.fwnode = of_fwnode_handle(sensor_node);
  1430. remote_ep = of_graph_get_remote_endpoint(ep_node);
  1431. if (!remote_ep) {
  1432. ctx_dbg(3, ctx, "can't get remote-endpoint\n");
  1433. goto cleanup_exit;
  1434. }
  1435. v4l2_fwnode_endpoint_parse(of_fwnode_handle(remote_ep), endpoint);
  1436. if (endpoint->bus_type != V4L2_MBUS_CSI2) {
  1437. ctx_err(ctx, "Port:%d sub-device %s is not a CSI2 device\n",
  1438. inst, sensor_node->name);
  1439. goto cleanup_exit;
  1440. }
  1441. /* Store Virtual Channel number */
  1442. ctx->virtual_channel = endpoint->base.id;
  1443. ctx_dbg(3, ctx, "Port:%d v4l2-endpoint: CSI2\n", inst);
  1444. ctx_dbg(3, ctx, "Virtual Channel=%d\n", ctx->virtual_channel);
  1445. ctx_dbg(3, ctx, "flags=0x%08x\n", endpoint->bus.mipi_csi2.flags);
  1446. ctx_dbg(3, ctx, "clock_lane=%d\n", endpoint->bus.mipi_csi2.clock_lane);
  1447. ctx_dbg(3, ctx, "num_data_lanes=%d\n",
  1448. endpoint->bus.mipi_csi2.num_data_lanes);
  1449. ctx_dbg(3, ctx, "data_lanes= <\n");
  1450. for (lane = 0; lane < endpoint->bus.mipi_csi2.num_data_lanes; lane++)
  1451. ctx_dbg(3, ctx, "\t%d\n",
  1452. endpoint->bus.mipi_csi2.data_lanes[lane]);
  1453. ctx_dbg(3, ctx, "\t>\n");
  1454. ctx_dbg(1, ctx, "Port: %d found sub-device %s\n",
  1455. inst, sensor_node->name);
  1456. ctx->asd_list[0] = asd;
  1457. ctx->notifier.subdevs = ctx->asd_list;
  1458. ctx->notifier.num_subdevs = 1;
  1459. ctx->notifier.ops = &cal_async_ops;
  1460. ret = v4l2_async_notifier_register(&ctx->v4l2_dev,
  1461. &ctx->notifier);
  1462. if (ret) {
  1463. ctx_err(ctx, "Error registering async notifier\n");
  1464. ret = -EINVAL;
  1465. }
  1466. cleanup_exit:
  1467. if (remote_ep)
  1468. of_node_put(remote_ep);
  1469. if (sensor_node)
  1470. of_node_put(sensor_node);
  1471. if (ep_node)
  1472. of_node_put(ep_node);
  1473. if (port)
  1474. of_node_put(port);
  1475. return ret;
  1476. }
  1477. static struct cal_ctx *cal_create_instance(struct cal_dev *dev, int inst)
  1478. {
  1479. struct cal_ctx *ctx;
  1480. struct v4l2_ctrl_handler *hdl;
  1481. int ret;
  1482. ctx = devm_kzalloc(&dev->pdev->dev, sizeof(*ctx), GFP_KERNEL);
  1483. if (!ctx)
  1484. return NULL;
  1485. /* save the cal_dev * for future ref */
  1486. ctx->dev = dev;
  1487. snprintf(ctx->v4l2_dev.name, sizeof(ctx->v4l2_dev.name),
  1488. "%s-%03d", CAL_MODULE_NAME, inst);
  1489. ret = v4l2_device_register(&dev->pdev->dev, &ctx->v4l2_dev);
  1490. if (ret)
  1491. goto err_exit;
  1492. hdl = &ctx->ctrl_handler;
  1493. ret = v4l2_ctrl_handler_init(hdl, 11);
  1494. if (ret) {
  1495. ctx_err(ctx, "Failed to init ctrl handler\n");
  1496. goto unreg_dev;
  1497. }
  1498. ctx->v4l2_dev.ctrl_handler = hdl;
  1499. /* Make sure Camera Core H/W register area is available */
  1500. ctx->cc = dev->cc[inst];
  1501. /* Store the instance id */
  1502. ctx->csi2_port = inst + 1;
  1503. ret = of_cal_create_instance(ctx, inst);
  1504. if (ret) {
  1505. ret = -EINVAL;
  1506. goto free_hdl;
  1507. }
  1508. return ctx;
  1509. free_hdl:
  1510. v4l2_ctrl_handler_free(hdl);
  1511. unreg_dev:
  1512. v4l2_device_unregister(&ctx->v4l2_dev);
  1513. err_exit:
  1514. return NULL;
  1515. }
  1516. static int cal_probe(struct platform_device *pdev)
  1517. {
  1518. struct cal_dev *dev;
  1519. int ret;
  1520. int irq;
  1521. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  1522. if (!dev)
  1523. return -ENOMEM;
  1524. /* set pseudo v4l2 device name so we can use v4l2_printk */
  1525. strlcpy(dev->v4l2_dev.name, CAL_MODULE_NAME,
  1526. sizeof(dev->v4l2_dev.name));
  1527. /* save pdev pointer */
  1528. dev->pdev = pdev;
  1529. dev->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1530. "cal_top");
  1531. dev->base = devm_ioremap_resource(&pdev->dev, dev->res);
  1532. if (IS_ERR(dev->base))
  1533. return PTR_ERR(dev->base);
  1534. cal_dbg(1, dev, "ioresource %s at %pa - %pa\n",
  1535. dev->res->name, &dev->res->start, &dev->res->end);
  1536. irq = platform_get_irq(pdev, 0);
  1537. cal_dbg(1, dev, "got irq# %d\n", irq);
  1538. ret = devm_request_irq(&pdev->dev, irq, cal_irq, 0, CAL_MODULE_NAME,
  1539. dev);
  1540. if (ret)
  1541. return ret;
  1542. platform_set_drvdata(pdev, dev);
  1543. dev->cm = cm_create(dev);
  1544. if (IS_ERR(dev->cm))
  1545. return PTR_ERR(dev->cm);
  1546. dev->cc[0] = cc_create(dev, 0);
  1547. if (IS_ERR(dev->cc[0]))
  1548. return PTR_ERR(dev->cc[0]);
  1549. dev->cc[1] = cc_create(dev, 1);
  1550. if (IS_ERR(dev->cc[1]))
  1551. return PTR_ERR(dev->cc[1]);
  1552. dev->ctx[0] = NULL;
  1553. dev->ctx[1] = NULL;
  1554. dev->ctx[0] = cal_create_instance(dev, 0);
  1555. dev->ctx[1] = cal_create_instance(dev, 1);
  1556. if (!dev->ctx[0] && !dev->ctx[1]) {
  1557. cal_err(dev, "Neither port is configured, no point in staying up\n");
  1558. return -ENODEV;
  1559. }
  1560. pm_runtime_enable(&pdev->dev);
  1561. ret = cal_runtime_get(dev);
  1562. if (ret)
  1563. goto runtime_disable;
  1564. /* Just check we can actually access the module */
  1565. cal_get_hwinfo(dev);
  1566. cal_runtime_put(dev);
  1567. return 0;
  1568. runtime_disable:
  1569. pm_runtime_disable(&pdev->dev);
  1570. return ret;
  1571. }
  1572. static int cal_remove(struct platform_device *pdev)
  1573. {
  1574. struct cal_dev *dev =
  1575. (struct cal_dev *)platform_get_drvdata(pdev);
  1576. struct cal_ctx *ctx;
  1577. int i;
  1578. cal_dbg(1, dev, "Removing %s\n", CAL_MODULE_NAME);
  1579. cal_runtime_get(dev);
  1580. for (i = 0; i < CAL_NUM_CONTEXT; i++) {
  1581. ctx = dev->ctx[i];
  1582. if (ctx) {
  1583. ctx_dbg(1, ctx, "unregistering %s\n",
  1584. video_device_node_name(&ctx->vdev));
  1585. camerarx_phy_disable(ctx);
  1586. v4l2_async_notifier_unregister(&ctx->notifier);
  1587. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1588. v4l2_device_unregister(&ctx->v4l2_dev);
  1589. video_unregister_device(&ctx->vdev);
  1590. }
  1591. }
  1592. cal_runtime_put(dev);
  1593. pm_runtime_disable(&pdev->dev);
  1594. return 0;
  1595. }
  1596. #if defined(CONFIG_OF)
  1597. static const struct of_device_id cal_of_match[] = {
  1598. { .compatible = "ti,dra72-cal", },
  1599. {},
  1600. };
  1601. MODULE_DEVICE_TABLE(of, cal_of_match);
  1602. #endif
  1603. static struct platform_driver cal_pdrv = {
  1604. .probe = cal_probe,
  1605. .remove = cal_remove,
  1606. .driver = {
  1607. .name = CAL_MODULE_NAME,
  1608. .of_match_table = of_match_ptr(cal_of_match),
  1609. },
  1610. };
  1611. module_platform_driver(cal_pdrv);