cal.c 49 KB

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