camss-vfe.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * camss-vfe.c
  4. *
  5. * Qualcomm MSM Camera Subsystem - VFE (Video Front End) Module
  6. *
  7. * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
  8. * Copyright (C) 2015-2018 Linaro Ltd.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/completion.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/iommu.h>
  14. #include <linux/mutex.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/spinlock_types.h>
  19. #include <linux/spinlock.h>
  20. #include <media/media-entity.h>
  21. #include <media/v4l2-device.h>
  22. #include <media/v4l2-subdev.h>
  23. #include "camss-vfe.h"
  24. #include "camss.h"
  25. #define MSM_VFE_NAME "msm_vfe"
  26. #define vfe_line_array(ptr_line) \
  27. ((const struct vfe_line (*)[]) &(ptr_line[-(ptr_line->id)]))
  28. #define to_vfe(ptr_line) \
  29. container_of(vfe_line_array(ptr_line), struct vfe_device, line)
  30. /* VFE reset timeout */
  31. #define VFE_RESET_TIMEOUT_MS 50
  32. /* VFE halt timeout */
  33. #define VFE_HALT_TIMEOUT_MS 100
  34. /* Max number of frame drop updates per frame */
  35. #define VFE_FRAME_DROP_UPDATES 5
  36. /* Frame drop value. NOTE: VAL + UPDATES should not exceed 31 */
  37. #define VFE_FRAME_DROP_VAL 20
  38. #define VFE_NEXT_SOF_MS 500
  39. #define SCALER_RATIO_MAX 16
  40. struct vfe_format {
  41. u32 code;
  42. u8 bpp;
  43. };
  44. static const struct vfe_format formats_rdi_8x16[] = {
  45. { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
  46. { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
  47. { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
  48. { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
  49. { MEDIA_BUS_FMT_SBGGR8_1X8, 8 },
  50. { MEDIA_BUS_FMT_SGBRG8_1X8, 8 },
  51. { MEDIA_BUS_FMT_SGRBG8_1X8, 8 },
  52. { MEDIA_BUS_FMT_SRGGB8_1X8, 8 },
  53. { MEDIA_BUS_FMT_SBGGR10_1X10, 10 },
  54. { MEDIA_BUS_FMT_SGBRG10_1X10, 10 },
  55. { MEDIA_BUS_FMT_SGRBG10_1X10, 10 },
  56. { MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
  57. { MEDIA_BUS_FMT_SBGGR12_1X12, 12 },
  58. { MEDIA_BUS_FMT_SGBRG12_1X12, 12 },
  59. { MEDIA_BUS_FMT_SGRBG12_1X12, 12 },
  60. { MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
  61. { MEDIA_BUS_FMT_Y10_1X10, 10 },
  62. };
  63. static const struct vfe_format formats_pix_8x16[] = {
  64. { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
  65. { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
  66. { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
  67. { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
  68. };
  69. static const struct vfe_format formats_rdi_8x96[] = {
  70. { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
  71. { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
  72. { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
  73. { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
  74. { MEDIA_BUS_FMT_SBGGR8_1X8, 8 },
  75. { MEDIA_BUS_FMT_SGBRG8_1X8, 8 },
  76. { MEDIA_BUS_FMT_SGRBG8_1X8, 8 },
  77. { MEDIA_BUS_FMT_SRGGB8_1X8, 8 },
  78. { MEDIA_BUS_FMT_SBGGR10_1X10, 10 },
  79. { MEDIA_BUS_FMT_SGBRG10_1X10, 10 },
  80. { MEDIA_BUS_FMT_SGRBG10_1X10, 10 },
  81. { MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
  82. { MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, 16 },
  83. { MEDIA_BUS_FMT_SBGGR12_1X12, 12 },
  84. { MEDIA_BUS_FMT_SGBRG12_1X12, 12 },
  85. { MEDIA_BUS_FMT_SGRBG12_1X12, 12 },
  86. { MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
  87. { MEDIA_BUS_FMT_SBGGR14_1X14, 14 },
  88. { MEDIA_BUS_FMT_SGBRG14_1X14, 14 },
  89. { MEDIA_BUS_FMT_SGRBG14_1X14, 14 },
  90. { MEDIA_BUS_FMT_SRGGB14_1X14, 14 },
  91. { MEDIA_BUS_FMT_Y10_1X10, 10 },
  92. { MEDIA_BUS_FMT_Y10_2X8_PADHI_LE, 16 },
  93. };
  94. static const struct vfe_format formats_pix_8x96[] = {
  95. { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
  96. { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
  97. { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
  98. { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
  99. };
  100. /*
  101. * vfe_get_bpp - map media bus format to bits per pixel
  102. * @formats: supported media bus formats array
  103. * @nformats: size of @formats array
  104. * @code: media bus format code
  105. *
  106. * Return number of bits per pixel
  107. */
  108. static u8 vfe_get_bpp(const struct vfe_format *formats,
  109. unsigned int nformats, u32 code)
  110. {
  111. unsigned int i;
  112. for (i = 0; i < nformats; i++)
  113. if (code == formats[i].code)
  114. return formats[i].bpp;
  115. WARN(1, "Unknown format\n");
  116. return formats[0].bpp;
  117. }
  118. static u32 vfe_find_code(u32 *code, unsigned int n_code,
  119. unsigned int index, u32 req_code)
  120. {
  121. int i;
  122. if (!req_code && (index >= n_code))
  123. return 0;
  124. for (i = 0; i < n_code; i++)
  125. if (req_code) {
  126. if (req_code == code[i])
  127. return req_code;
  128. } else {
  129. if (i == index)
  130. return code[i];
  131. }
  132. return code[0];
  133. }
  134. static u32 vfe_src_pad_code(struct vfe_line *line, u32 sink_code,
  135. unsigned int index, u32 src_req_code)
  136. {
  137. struct vfe_device *vfe = to_vfe(line);
  138. if (vfe->camss->version == CAMSS_8x16)
  139. switch (sink_code) {
  140. case MEDIA_BUS_FMT_YUYV8_2X8:
  141. {
  142. u32 src_code[] = {
  143. MEDIA_BUS_FMT_YUYV8_2X8,
  144. MEDIA_BUS_FMT_YUYV8_1_5X8,
  145. };
  146. return vfe_find_code(src_code, ARRAY_SIZE(src_code),
  147. index, src_req_code);
  148. }
  149. case MEDIA_BUS_FMT_YVYU8_2X8:
  150. {
  151. u32 src_code[] = {
  152. MEDIA_BUS_FMT_YVYU8_2X8,
  153. MEDIA_BUS_FMT_YVYU8_1_5X8,
  154. };
  155. return vfe_find_code(src_code, ARRAY_SIZE(src_code),
  156. index, src_req_code);
  157. }
  158. case MEDIA_BUS_FMT_UYVY8_2X8:
  159. {
  160. u32 src_code[] = {
  161. MEDIA_BUS_FMT_UYVY8_2X8,
  162. MEDIA_BUS_FMT_UYVY8_1_5X8,
  163. };
  164. return vfe_find_code(src_code, ARRAY_SIZE(src_code),
  165. index, src_req_code);
  166. }
  167. case MEDIA_BUS_FMT_VYUY8_2X8:
  168. {
  169. u32 src_code[] = {
  170. MEDIA_BUS_FMT_VYUY8_2X8,
  171. MEDIA_BUS_FMT_VYUY8_1_5X8,
  172. };
  173. return vfe_find_code(src_code, ARRAY_SIZE(src_code),
  174. index, src_req_code);
  175. }
  176. default:
  177. if (index > 0)
  178. return 0;
  179. return sink_code;
  180. }
  181. else if (vfe->camss->version == CAMSS_8x96)
  182. switch (sink_code) {
  183. case MEDIA_BUS_FMT_YUYV8_2X8:
  184. {
  185. u32 src_code[] = {
  186. MEDIA_BUS_FMT_YUYV8_2X8,
  187. MEDIA_BUS_FMT_YVYU8_2X8,
  188. MEDIA_BUS_FMT_UYVY8_2X8,
  189. MEDIA_BUS_FMT_VYUY8_2X8,
  190. MEDIA_BUS_FMT_YUYV8_1_5X8,
  191. };
  192. return vfe_find_code(src_code, ARRAY_SIZE(src_code),
  193. index, src_req_code);
  194. }
  195. case MEDIA_BUS_FMT_YVYU8_2X8:
  196. {
  197. u32 src_code[] = {
  198. MEDIA_BUS_FMT_YVYU8_2X8,
  199. MEDIA_BUS_FMT_YUYV8_2X8,
  200. MEDIA_BUS_FMT_UYVY8_2X8,
  201. MEDIA_BUS_FMT_VYUY8_2X8,
  202. MEDIA_BUS_FMT_YVYU8_1_5X8,
  203. };
  204. return vfe_find_code(src_code, ARRAY_SIZE(src_code),
  205. index, src_req_code);
  206. }
  207. case MEDIA_BUS_FMT_UYVY8_2X8:
  208. {
  209. u32 src_code[] = {
  210. MEDIA_BUS_FMT_UYVY8_2X8,
  211. MEDIA_BUS_FMT_YUYV8_2X8,
  212. MEDIA_BUS_FMT_YVYU8_2X8,
  213. MEDIA_BUS_FMT_VYUY8_2X8,
  214. MEDIA_BUS_FMT_UYVY8_1_5X8,
  215. };
  216. return vfe_find_code(src_code, ARRAY_SIZE(src_code),
  217. index, src_req_code);
  218. }
  219. case MEDIA_BUS_FMT_VYUY8_2X8:
  220. {
  221. u32 src_code[] = {
  222. MEDIA_BUS_FMT_VYUY8_2X8,
  223. MEDIA_BUS_FMT_YUYV8_2X8,
  224. MEDIA_BUS_FMT_YVYU8_2X8,
  225. MEDIA_BUS_FMT_UYVY8_2X8,
  226. MEDIA_BUS_FMT_VYUY8_1_5X8,
  227. };
  228. return vfe_find_code(src_code, ARRAY_SIZE(src_code),
  229. index, src_req_code);
  230. }
  231. default:
  232. if (index > 0)
  233. return 0;
  234. return sink_code;
  235. }
  236. else
  237. return 0;
  238. }
  239. /*
  240. * vfe_reset - Trigger reset on VFE module and wait to complete
  241. * @vfe: VFE device
  242. *
  243. * Return 0 on success or a negative error code otherwise
  244. */
  245. static int vfe_reset(struct vfe_device *vfe)
  246. {
  247. unsigned long time;
  248. reinit_completion(&vfe->reset_complete);
  249. vfe->ops->global_reset(vfe);
  250. time = wait_for_completion_timeout(&vfe->reset_complete,
  251. msecs_to_jiffies(VFE_RESET_TIMEOUT_MS));
  252. if (!time) {
  253. dev_err(vfe->camss->dev, "VFE reset timeout\n");
  254. return -EIO;
  255. }
  256. return 0;
  257. }
  258. /*
  259. * vfe_halt - Trigger halt on VFE module and wait to complete
  260. * @vfe: VFE device
  261. *
  262. * Return 0 on success or a negative error code otherwise
  263. */
  264. static int vfe_halt(struct vfe_device *vfe)
  265. {
  266. unsigned long time;
  267. reinit_completion(&vfe->halt_complete);
  268. vfe->ops->halt_request(vfe);
  269. time = wait_for_completion_timeout(&vfe->halt_complete,
  270. msecs_to_jiffies(VFE_HALT_TIMEOUT_MS));
  271. if (!time) {
  272. dev_err(vfe->camss->dev, "VFE halt timeout\n");
  273. return -EIO;
  274. }
  275. return 0;
  276. }
  277. static void vfe_init_outputs(struct vfe_device *vfe)
  278. {
  279. int i;
  280. for (i = 0; i < ARRAY_SIZE(vfe->line); i++) {
  281. struct vfe_output *output = &vfe->line[i].output;
  282. output->state = VFE_OUTPUT_OFF;
  283. output->buf[0] = NULL;
  284. output->buf[1] = NULL;
  285. INIT_LIST_HEAD(&output->pending_bufs);
  286. }
  287. }
  288. static void vfe_reset_output_maps(struct vfe_device *vfe)
  289. {
  290. int i;
  291. for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++)
  292. vfe->wm_output_map[i] = VFE_LINE_NONE;
  293. }
  294. static void vfe_output_init_addrs(struct vfe_device *vfe,
  295. struct vfe_output *output, u8 sync)
  296. {
  297. u32 ping_addr;
  298. u32 pong_addr;
  299. unsigned int i;
  300. output->active_buf = 0;
  301. for (i = 0; i < output->wm_num; i++) {
  302. if (output->buf[0])
  303. ping_addr = output->buf[0]->addr[i];
  304. else
  305. ping_addr = 0;
  306. if (output->buf[1])
  307. pong_addr = output->buf[1]->addr[i];
  308. else
  309. pong_addr = ping_addr;
  310. vfe->ops->wm_set_ping_addr(vfe, output->wm_idx[i], ping_addr);
  311. vfe->ops->wm_set_pong_addr(vfe, output->wm_idx[i], pong_addr);
  312. if (sync)
  313. vfe->ops->bus_reload_wm(vfe, output->wm_idx[i]);
  314. }
  315. }
  316. static void vfe_output_update_ping_addr(struct vfe_device *vfe,
  317. struct vfe_output *output, u8 sync)
  318. {
  319. u32 addr;
  320. unsigned int i;
  321. for (i = 0; i < output->wm_num; i++) {
  322. if (output->buf[0])
  323. addr = output->buf[0]->addr[i];
  324. else
  325. addr = 0;
  326. vfe->ops->wm_set_ping_addr(vfe, output->wm_idx[i], addr);
  327. if (sync)
  328. vfe->ops->bus_reload_wm(vfe, output->wm_idx[i]);
  329. }
  330. }
  331. static void vfe_output_update_pong_addr(struct vfe_device *vfe,
  332. struct vfe_output *output, u8 sync)
  333. {
  334. u32 addr;
  335. unsigned int i;
  336. for (i = 0; i < output->wm_num; i++) {
  337. if (output->buf[1])
  338. addr = output->buf[1]->addr[i];
  339. else
  340. addr = 0;
  341. vfe->ops->wm_set_pong_addr(vfe, output->wm_idx[i], addr);
  342. if (sync)
  343. vfe->ops->bus_reload_wm(vfe, output->wm_idx[i]);
  344. }
  345. }
  346. static int vfe_reserve_wm(struct vfe_device *vfe, enum vfe_line_id line_id)
  347. {
  348. int ret = -EBUSY;
  349. int i;
  350. for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++) {
  351. if (vfe->wm_output_map[i] == VFE_LINE_NONE) {
  352. vfe->wm_output_map[i] = line_id;
  353. ret = i;
  354. break;
  355. }
  356. }
  357. return ret;
  358. }
  359. static int vfe_release_wm(struct vfe_device *vfe, u8 wm)
  360. {
  361. if (wm >= ARRAY_SIZE(vfe->wm_output_map))
  362. return -EINVAL;
  363. vfe->wm_output_map[wm] = VFE_LINE_NONE;
  364. return 0;
  365. }
  366. static void vfe_output_frame_drop(struct vfe_device *vfe,
  367. struct vfe_output *output,
  368. u32 drop_pattern)
  369. {
  370. u8 drop_period;
  371. unsigned int i;
  372. /* We need to toggle update period to be valid on next frame */
  373. output->drop_update_idx++;
  374. output->drop_update_idx %= VFE_FRAME_DROP_UPDATES;
  375. drop_period = VFE_FRAME_DROP_VAL + output->drop_update_idx;
  376. for (i = 0; i < output->wm_num; i++) {
  377. vfe->ops->wm_set_framedrop_period(vfe, output->wm_idx[i],
  378. drop_period);
  379. vfe->ops->wm_set_framedrop_pattern(vfe, output->wm_idx[i],
  380. drop_pattern);
  381. }
  382. vfe->ops->reg_update(vfe,
  383. container_of(output, struct vfe_line, output)->id);
  384. }
  385. static struct camss_buffer *vfe_buf_get_pending(struct vfe_output *output)
  386. {
  387. struct camss_buffer *buffer = NULL;
  388. if (!list_empty(&output->pending_bufs)) {
  389. buffer = list_first_entry(&output->pending_bufs,
  390. struct camss_buffer,
  391. queue);
  392. list_del(&buffer->queue);
  393. }
  394. return buffer;
  395. }
  396. /*
  397. * vfe_buf_add_pending - Add output buffer to list of pending
  398. * @output: VFE output
  399. * @buffer: Video buffer
  400. */
  401. static void vfe_buf_add_pending(struct vfe_output *output,
  402. struct camss_buffer *buffer)
  403. {
  404. INIT_LIST_HEAD(&buffer->queue);
  405. list_add_tail(&buffer->queue, &output->pending_bufs);
  406. }
  407. /*
  408. * vfe_buf_flush_pending - Flush all pending buffers.
  409. * @output: VFE output
  410. * @state: vb2 buffer state
  411. */
  412. static void vfe_buf_flush_pending(struct vfe_output *output,
  413. enum vb2_buffer_state state)
  414. {
  415. struct camss_buffer *buf;
  416. struct camss_buffer *t;
  417. list_for_each_entry_safe(buf, t, &output->pending_bufs, queue) {
  418. vb2_buffer_done(&buf->vb.vb2_buf, state);
  419. list_del(&buf->queue);
  420. }
  421. }
  422. static void vfe_buf_update_wm_on_next(struct vfe_device *vfe,
  423. struct vfe_output *output)
  424. {
  425. switch (output->state) {
  426. case VFE_OUTPUT_CONTINUOUS:
  427. vfe_output_frame_drop(vfe, output, 3);
  428. break;
  429. case VFE_OUTPUT_SINGLE:
  430. default:
  431. dev_err_ratelimited(vfe->camss->dev,
  432. "Next buf in wrong state! %d\n",
  433. output->state);
  434. break;
  435. }
  436. }
  437. static void vfe_buf_update_wm_on_last(struct vfe_device *vfe,
  438. struct vfe_output *output)
  439. {
  440. switch (output->state) {
  441. case VFE_OUTPUT_CONTINUOUS:
  442. output->state = VFE_OUTPUT_SINGLE;
  443. vfe_output_frame_drop(vfe, output, 1);
  444. break;
  445. case VFE_OUTPUT_SINGLE:
  446. output->state = VFE_OUTPUT_STOPPING;
  447. vfe_output_frame_drop(vfe, output, 0);
  448. break;
  449. default:
  450. dev_err_ratelimited(vfe->camss->dev,
  451. "Last buff in wrong state! %d\n",
  452. output->state);
  453. break;
  454. }
  455. }
  456. static void vfe_buf_update_wm_on_new(struct vfe_device *vfe,
  457. struct vfe_output *output,
  458. struct camss_buffer *new_buf)
  459. {
  460. int inactive_idx;
  461. switch (output->state) {
  462. case VFE_OUTPUT_SINGLE:
  463. inactive_idx = !output->active_buf;
  464. if (!output->buf[inactive_idx]) {
  465. output->buf[inactive_idx] = new_buf;
  466. if (inactive_idx)
  467. vfe_output_update_pong_addr(vfe, output, 0);
  468. else
  469. vfe_output_update_ping_addr(vfe, output, 0);
  470. vfe_output_frame_drop(vfe, output, 3);
  471. output->state = VFE_OUTPUT_CONTINUOUS;
  472. } else {
  473. vfe_buf_add_pending(output, new_buf);
  474. dev_err_ratelimited(vfe->camss->dev,
  475. "Inactive buffer is busy\n");
  476. }
  477. break;
  478. case VFE_OUTPUT_IDLE:
  479. if (!output->buf[0]) {
  480. output->buf[0] = new_buf;
  481. vfe_output_init_addrs(vfe, output, 1);
  482. vfe_output_frame_drop(vfe, output, 1);
  483. output->state = VFE_OUTPUT_SINGLE;
  484. } else {
  485. vfe_buf_add_pending(output, new_buf);
  486. dev_err_ratelimited(vfe->camss->dev,
  487. "Output idle with buffer set!\n");
  488. }
  489. break;
  490. case VFE_OUTPUT_CONTINUOUS:
  491. default:
  492. vfe_buf_add_pending(output, new_buf);
  493. break;
  494. }
  495. }
  496. static int vfe_get_output(struct vfe_line *line)
  497. {
  498. struct vfe_device *vfe = to_vfe(line);
  499. struct vfe_output *output;
  500. struct v4l2_format *f = &line->video_out.active_fmt;
  501. unsigned long flags;
  502. int i;
  503. int wm_idx;
  504. spin_lock_irqsave(&vfe->output_lock, flags);
  505. output = &line->output;
  506. if (output->state != VFE_OUTPUT_OFF) {
  507. dev_err(vfe->camss->dev, "Output is running\n");
  508. goto error;
  509. }
  510. output->state = VFE_OUTPUT_RESERVED;
  511. output->active_buf = 0;
  512. switch (f->fmt.pix_mp.pixelformat) {
  513. case V4L2_PIX_FMT_NV12:
  514. case V4L2_PIX_FMT_NV21:
  515. case V4L2_PIX_FMT_NV16:
  516. case V4L2_PIX_FMT_NV61:
  517. output->wm_num = 2;
  518. break;
  519. default:
  520. output->wm_num = 1;
  521. break;
  522. }
  523. for (i = 0; i < output->wm_num; i++) {
  524. wm_idx = vfe_reserve_wm(vfe, line->id);
  525. if (wm_idx < 0) {
  526. dev_err(vfe->camss->dev, "Can not reserve wm\n");
  527. goto error_get_wm;
  528. }
  529. output->wm_idx[i] = wm_idx;
  530. }
  531. output->drop_update_idx = 0;
  532. spin_unlock_irqrestore(&vfe->output_lock, flags);
  533. return 0;
  534. error_get_wm:
  535. for (i--; i >= 0; i--)
  536. vfe_release_wm(vfe, output->wm_idx[i]);
  537. output->state = VFE_OUTPUT_OFF;
  538. error:
  539. spin_unlock_irqrestore(&vfe->output_lock, flags);
  540. return -EINVAL;
  541. }
  542. static int vfe_put_output(struct vfe_line *line)
  543. {
  544. struct vfe_device *vfe = to_vfe(line);
  545. struct vfe_output *output = &line->output;
  546. unsigned long flags;
  547. unsigned int i;
  548. spin_lock_irqsave(&vfe->output_lock, flags);
  549. for (i = 0; i < output->wm_num; i++)
  550. vfe_release_wm(vfe, output->wm_idx[i]);
  551. output->state = VFE_OUTPUT_OFF;
  552. spin_unlock_irqrestore(&vfe->output_lock, flags);
  553. return 0;
  554. }
  555. static int vfe_enable_output(struct vfe_line *line)
  556. {
  557. struct vfe_device *vfe = to_vfe(line);
  558. struct vfe_output *output = &line->output;
  559. const struct vfe_hw_ops *ops = vfe->ops;
  560. unsigned long flags;
  561. unsigned int i;
  562. u16 ub_size;
  563. ub_size = ops->get_ub_size(vfe->id);
  564. if (!ub_size)
  565. return -EINVAL;
  566. spin_lock_irqsave(&vfe->output_lock, flags);
  567. ops->reg_update_clear(vfe, line->id);
  568. if (output->state != VFE_OUTPUT_RESERVED) {
  569. dev_err(vfe->camss->dev, "Output is not in reserved state %d\n",
  570. output->state);
  571. spin_unlock_irqrestore(&vfe->output_lock, flags);
  572. return -EINVAL;
  573. }
  574. output->state = VFE_OUTPUT_IDLE;
  575. output->buf[0] = vfe_buf_get_pending(output);
  576. output->buf[1] = vfe_buf_get_pending(output);
  577. if (!output->buf[0] && output->buf[1]) {
  578. output->buf[0] = output->buf[1];
  579. output->buf[1] = NULL;
  580. }
  581. if (output->buf[0])
  582. output->state = VFE_OUTPUT_SINGLE;
  583. if (output->buf[1])
  584. output->state = VFE_OUTPUT_CONTINUOUS;
  585. switch (output->state) {
  586. case VFE_OUTPUT_SINGLE:
  587. vfe_output_frame_drop(vfe, output, 1);
  588. break;
  589. case VFE_OUTPUT_CONTINUOUS:
  590. vfe_output_frame_drop(vfe, output, 3);
  591. break;
  592. default:
  593. vfe_output_frame_drop(vfe, output, 0);
  594. break;
  595. }
  596. output->sequence = 0;
  597. output->wait_sof = 0;
  598. output->wait_reg_update = 0;
  599. reinit_completion(&output->sof);
  600. reinit_completion(&output->reg_update);
  601. vfe_output_init_addrs(vfe, output, 0);
  602. if (line->id != VFE_LINE_PIX) {
  603. ops->set_cgc_override(vfe, output->wm_idx[0], 1);
  604. ops->enable_irq_wm_line(vfe, output->wm_idx[0], line->id, 1);
  605. ops->bus_connect_wm_to_rdi(vfe, output->wm_idx[0], line->id);
  606. ops->wm_set_subsample(vfe, output->wm_idx[0]);
  607. ops->set_rdi_cid(vfe, line->id, 0);
  608. ops->wm_set_ub_cfg(vfe, output->wm_idx[0],
  609. (ub_size + 1) * output->wm_idx[0], ub_size);
  610. ops->wm_frame_based(vfe, output->wm_idx[0], 1);
  611. ops->wm_enable(vfe, output->wm_idx[0], 1);
  612. ops->bus_reload_wm(vfe, output->wm_idx[0]);
  613. } else {
  614. ub_size /= output->wm_num;
  615. for (i = 0; i < output->wm_num; i++) {
  616. ops->set_cgc_override(vfe, output->wm_idx[i], 1);
  617. ops->wm_set_subsample(vfe, output->wm_idx[i]);
  618. ops->wm_set_ub_cfg(vfe, output->wm_idx[i],
  619. (ub_size + 1) * output->wm_idx[i],
  620. ub_size);
  621. ops->wm_line_based(vfe, output->wm_idx[i],
  622. &line->video_out.active_fmt.fmt.pix_mp,
  623. i, 1);
  624. ops->wm_enable(vfe, output->wm_idx[i], 1);
  625. ops->bus_reload_wm(vfe, output->wm_idx[i]);
  626. }
  627. ops->enable_irq_pix_line(vfe, 0, line->id, 1);
  628. ops->set_module_cfg(vfe, 1);
  629. ops->set_camif_cfg(vfe, line);
  630. ops->set_realign_cfg(vfe, line, 1);
  631. ops->set_xbar_cfg(vfe, output, 1);
  632. ops->set_demux_cfg(vfe, line);
  633. ops->set_scale_cfg(vfe, line);
  634. ops->set_crop_cfg(vfe, line);
  635. ops->set_clamp_cfg(vfe);
  636. ops->set_camif_cmd(vfe, 1);
  637. }
  638. ops->reg_update(vfe, line->id);
  639. spin_unlock_irqrestore(&vfe->output_lock, flags);
  640. return 0;
  641. }
  642. static int vfe_disable_output(struct vfe_line *line)
  643. {
  644. struct vfe_device *vfe = to_vfe(line);
  645. struct vfe_output *output = &line->output;
  646. const struct vfe_hw_ops *ops = vfe->ops;
  647. unsigned long flags;
  648. unsigned long time;
  649. unsigned int i;
  650. spin_lock_irqsave(&vfe->output_lock, flags);
  651. output->wait_sof = 1;
  652. spin_unlock_irqrestore(&vfe->output_lock, flags);
  653. time = wait_for_completion_timeout(&output->sof,
  654. msecs_to_jiffies(VFE_NEXT_SOF_MS));
  655. if (!time)
  656. dev_err(vfe->camss->dev, "VFE sof timeout\n");
  657. spin_lock_irqsave(&vfe->output_lock, flags);
  658. for (i = 0; i < output->wm_num; i++)
  659. ops->wm_enable(vfe, output->wm_idx[i], 0);
  660. ops->reg_update(vfe, line->id);
  661. output->wait_reg_update = 1;
  662. spin_unlock_irqrestore(&vfe->output_lock, flags);
  663. time = wait_for_completion_timeout(&output->reg_update,
  664. msecs_to_jiffies(VFE_NEXT_SOF_MS));
  665. if (!time)
  666. dev_err(vfe->camss->dev, "VFE reg update timeout\n");
  667. spin_lock_irqsave(&vfe->output_lock, flags);
  668. if (line->id != VFE_LINE_PIX) {
  669. ops->wm_frame_based(vfe, output->wm_idx[0], 0);
  670. ops->bus_disconnect_wm_from_rdi(vfe, output->wm_idx[0],
  671. line->id);
  672. ops->enable_irq_wm_line(vfe, output->wm_idx[0], line->id, 0);
  673. ops->set_cgc_override(vfe, output->wm_idx[0], 0);
  674. spin_unlock_irqrestore(&vfe->output_lock, flags);
  675. } else {
  676. for (i = 0; i < output->wm_num; i++) {
  677. ops->wm_line_based(vfe, output->wm_idx[i], NULL, i, 0);
  678. ops->set_cgc_override(vfe, output->wm_idx[i], 0);
  679. }
  680. ops->enable_irq_pix_line(vfe, 0, line->id, 0);
  681. ops->set_module_cfg(vfe, 0);
  682. ops->set_realign_cfg(vfe, line, 0);
  683. ops->set_xbar_cfg(vfe, output, 0);
  684. ops->set_camif_cmd(vfe, 0);
  685. spin_unlock_irqrestore(&vfe->output_lock, flags);
  686. ops->camif_wait_for_stop(vfe, vfe->camss->dev);
  687. }
  688. return 0;
  689. }
  690. /*
  691. * vfe_enable - Enable streaming on VFE line
  692. * @line: VFE line
  693. *
  694. * Return 0 on success or a negative error code otherwise
  695. */
  696. static int vfe_enable(struct vfe_line *line)
  697. {
  698. struct vfe_device *vfe = to_vfe(line);
  699. int ret;
  700. mutex_lock(&vfe->stream_lock);
  701. if (!vfe->stream_count) {
  702. vfe->ops->enable_irq_common(vfe);
  703. vfe->ops->bus_enable_wr_if(vfe, 1);
  704. vfe->ops->set_qos(vfe);
  705. vfe->ops->set_ds(vfe);
  706. }
  707. vfe->stream_count++;
  708. mutex_unlock(&vfe->stream_lock);
  709. ret = vfe_get_output(line);
  710. if (ret < 0)
  711. goto error_get_output;
  712. ret = vfe_enable_output(line);
  713. if (ret < 0)
  714. goto error_enable_output;
  715. vfe->was_streaming = 1;
  716. return 0;
  717. error_enable_output:
  718. vfe_put_output(line);
  719. error_get_output:
  720. mutex_lock(&vfe->stream_lock);
  721. if (vfe->stream_count == 1)
  722. vfe->ops->bus_enable_wr_if(vfe, 0);
  723. vfe->stream_count--;
  724. mutex_unlock(&vfe->stream_lock);
  725. return ret;
  726. }
  727. /*
  728. * vfe_disable - Disable streaming on VFE line
  729. * @line: VFE line
  730. *
  731. * Return 0 on success or a negative error code otherwise
  732. */
  733. static int vfe_disable(struct vfe_line *line)
  734. {
  735. struct vfe_device *vfe = to_vfe(line);
  736. vfe_disable_output(line);
  737. vfe_put_output(line);
  738. mutex_lock(&vfe->stream_lock);
  739. if (vfe->stream_count == 1)
  740. vfe->ops->bus_enable_wr_if(vfe, 0);
  741. vfe->stream_count--;
  742. mutex_unlock(&vfe->stream_lock);
  743. return 0;
  744. }
  745. /*
  746. * vfe_isr_sof - Process start of frame interrupt
  747. * @vfe: VFE Device
  748. * @line_id: VFE line
  749. */
  750. static void vfe_isr_sof(struct vfe_device *vfe, enum vfe_line_id line_id)
  751. {
  752. struct vfe_output *output;
  753. unsigned long flags;
  754. spin_lock_irqsave(&vfe->output_lock, flags);
  755. output = &vfe->line[line_id].output;
  756. if (output->wait_sof) {
  757. output->wait_sof = 0;
  758. complete(&output->sof);
  759. }
  760. spin_unlock_irqrestore(&vfe->output_lock, flags);
  761. }
  762. /*
  763. * vfe_isr_reg_update - Process reg update interrupt
  764. * @vfe: VFE Device
  765. * @line_id: VFE line
  766. */
  767. static void vfe_isr_reg_update(struct vfe_device *vfe, enum vfe_line_id line_id)
  768. {
  769. struct vfe_output *output;
  770. unsigned long flags;
  771. spin_lock_irqsave(&vfe->output_lock, flags);
  772. vfe->ops->reg_update_clear(vfe, line_id);
  773. output = &vfe->line[line_id].output;
  774. if (output->wait_reg_update) {
  775. output->wait_reg_update = 0;
  776. complete(&output->reg_update);
  777. spin_unlock_irqrestore(&vfe->output_lock, flags);
  778. return;
  779. }
  780. if (output->state == VFE_OUTPUT_STOPPING) {
  781. /* Release last buffer when hw is idle */
  782. if (output->last_buffer) {
  783. vb2_buffer_done(&output->last_buffer->vb.vb2_buf,
  784. VB2_BUF_STATE_DONE);
  785. output->last_buffer = NULL;
  786. }
  787. output->state = VFE_OUTPUT_IDLE;
  788. /* Buffers received in stopping state are queued in */
  789. /* dma pending queue, start next capture here */
  790. output->buf[0] = vfe_buf_get_pending(output);
  791. output->buf[1] = vfe_buf_get_pending(output);
  792. if (!output->buf[0] && output->buf[1]) {
  793. output->buf[0] = output->buf[1];
  794. output->buf[1] = NULL;
  795. }
  796. if (output->buf[0])
  797. output->state = VFE_OUTPUT_SINGLE;
  798. if (output->buf[1])
  799. output->state = VFE_OUTPUT_CONTINUOUS;
  800. switch (output->state) {
  801. case VFE_OUTPUT_SINGLE:
  802. vfe_output_frame_drop(vfe, output, 2);
  803. break;
  804. case VFE_OUTPUT_CONTINUOUS:
  805. vfe_output_frame_drop(vfe, output, 3);
  806. break;
  807. default:
  808. vfe_output_frame_drop(vfe, output, 0);
  809. break;
  810. }
  811. vfe_output_init_addrs(vfe, output, 1);
  812. }
  813. spin_unlock_irqrestore(&vfe->output_lock, flags);
  814. }
  815. /*
  816. * vfe_isr_wm_done - Process write master done interrupt
  817. * @vfe: VFE Device
  818. * @wm: Write master id
  819. */
  820. static void vfe_isr_wm_done(struct vfe_device *vfe, u8 wm)
  821. {
  822. struct camss_buffer *ready_buf;
  823. struct vfe_output *output;
  824. dma_addr_t *new_addr;
  825. unsigned long flags;
  826. u32 active_index;
  827. u64 ts = ktime_get_ns();
  828. unsigned int i;
  829. active_index = vfe->ops->wm_get_ping_pong_status(vfe, wm);
  830. spin_lock_irqsave(&vfe->output_lock, flags);
  831. if (vfe->wm_output_map[wm] == VFE_LINE_NONE) {
  832. dev_err_ratelimited(vfe->camss->dev,
  833. "Received wm done for unmapped index\n");
  834. goto out_unlock;
  835. }
  836. output = &vfe->line[vfe->wm_output_map[wm]].output;
  837. if (output->active_buf == active_index) {
  838. dev_err_ratelimited(vfe->camss->dev,
  839. "Active buffer mismatch!\n");
  840. goto out_unlock;
  841. }
  842. output->active_buf = active_index;
  843. ready_buf = output->buf[!active_index];
  844. if (!ready_buf) {
  845. dev_err_ratelimited(vfe->camss->dev,
  846. "Missing ready buf %d %d!\n",
  847. !active_index, output->state);
  848. goto out_unlock;
  849. }
  850. ready_buf->vb.vb2_buf.timestamp = ts;
  851. ready_buf->vb.sequence = output->sequence++;
  852. /* Get next buffer */
  853. output->buf[!active_index] = vfe_buf_get_pending(output);
  854. if (!output->buf[!active_index]) {
  855. /* No next buffer - set same address */
  856. new_addr = ready_buf->addr;
  857. vfe_buf_update_wm_on_last(vfe, output);
  858. } else {
  859. new_addr = output->buf[!active_index]->addr;
  860. vfe_buf_update_wm_on_next(vfe, output);
  861. }
  862. if (active_index)
  863. for (i = 0; i < output->wm_num; i++)
  864. vfe->ops->wm_set_ping_addr(vfe, output->wm_idx[i],
  865. new_addr[i]);
  866. else
  867. for (i = 0; i < output->wm_num; i++)
  868. vfe->ops->wm_set_pong_addr(vfe, output->wm_idx[i],
  869. new_addr[i]);
  870. spin_unlock_irqrestore(&vfe->output_lock, flags);
  871. if (output->state == VFE_OUTPUT_STOPPING)
  872. output->last_buffer = ready_buf;
  873. else
  874. vb2_buffer_done(&ready_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  875. return;
  876. out_unlock:
  877. spin_unlock_irqrestore(&vfe->output_lock, flags);
  878. }
  879. /*
  880. * vfe_isr_wm_done - Process composite image done interrupt
  881. * @vfe: VFE Device
  882. * @comp: Composite image id
  883. */
  884. static void vfe_isr_comp_done(struct vfe_device *vfe, u8 comp)
  885. {
  886. unsigned int i;
  887. for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++)
  888. if (vfe->wm_output_map[i] == VFE_LINE_PIX) {
  889. vfe_isr_wm_done(vfe, i);
  890. break;
  891. }
  892. }
  893. static inline void vfe_isr_reset_ack(struct vfe_device *vfe)
  894. {
  895. complete(&vfe->reset_complete);
  896. }
  897. static inline void vfe_isr_halt_ack(struct vfe_device *vfe)
  898. {
  899. complete(&vfe->halt_complete);
  900. vfe->ops->halt_clear(vfe);
  901. }
  902. /*
  903. * vfe_set_clock_rates - Calculate and set clock rates on VFE module
  904. * @vfe: VFE device
  905. *
  906. * Return 0 on success or a negative error code otherwise
  907. */
  908. static int vfe_set_clock_rates(struct vfe_device *vfe)
  909. {
  910. struct device *dev = vfe->camss->dev;
  911. u32 pixel_clock[MSM_VFE_LINE_NUM];
  912. int i, j;
  913. int ret;
  914. for (i = VFE_LINE_RDI0; i <= VFE_LINE_PIX; i++) {
  915. ret = camss_get_pixel_clock(&vfe->line[i].subdev.entity,
  916. &pixel_clock[i]);
  917. if (ret)
  918. pixel_clock[i] = 0;
  919. }
  920. for (i = 0; i < vfe->nclocks; i++) {
  921. struct camss_clock *clock = &vfe->clock[i];
  922. if (!strcmp(clock->name, "vfe0") ||
  923. !strcmp(clock->name, "vfe1")) {
  924. u64 min_rate = 0;
  925. long rate;
  926. for (j = VFE_LINE_RDI0; j <= VFE_LINE_PIX; j++) {
  927. u32 tmp;
  928. u8 bpp;
  929. if (j == VFE_LINE_PIX) {
  930. tmp = pixel_clock[j];
  931. } else {
  932. struct vfe_line *l = &vfe->line[j];
  933. bpp = vfe_get_bpp(l->formats,
  934. l->nformats,
  935. l->fmt[MSM_VFE_PAD_SINK].code);
  936. tmp = pixel_clock[j] * bpp / 64;
  937. }
  938. if (min_rate < tmp)
  939. min_rate = tmp;
  940. }
  941. camss_add_clock_margin(&min_rate);
  942. for (j = 0; j < clock->nfreqs; j++)
  943. if (min_rate < clock->freq[j])
  944. break;
  945. if (j == clock->nfreqs) {
  946. dev_err(dev,
  947. "Pixel clock is too high for VFE");
  948. return -EINVAL;
  949. }
  950. /* if sensor pixel clock is not available */
  951. /* set highest possible VFE clock rate */
  952. if (min_rate == 0)
  953. j = clock->nfreqs - 1;
  954. rate = clk_round_rate(clock->clk, clock->freq[j]);
  955. if (rate < 0) {
  956. dev_err(dev, "clk round rate failed: %ld\n",
  957. rate);
  958. return -EINVAL;
  959. }
  960. ret = clk_set_rate(clock->clk, rate);
  961. if (ret < 0) {
  962. dev_err(dev, "clk set rate failed: %d\n", ret);
  963. return ret;
  964. }
  965. }
  966. }
  967. return 0;
  968. }
  969. /*
  970. * vfe_check_clock_rates - Check current clock rates on VFE module
  971. * @vfe: VFE device
  972. *
  973. * Return 0 if current clock rates are suitable for a new pipeline
  974. * or a negative error code otherwise
  975. */
  976. static int vfe_check_clock_rates(struct vfe_device *vfe)
  977. {
  978. u32 pixel_clock[MSM_VFE_LINE_NUM];
  979. int i, j;
  980. int ret;
  981. for (i = VFE_LINE_RDI0; i <= VFE_LINE_PIX; i++) {
  982. ret = camss_get_pixel_clock(&vfe->line[i].subdev.entity,
  983. &pixel_clock[i]);
  984. if (ret)
  985. pixel_clock[i] = 0;
  986. }
  987. for (i = 0; i < vfe->nclocks; i++) {
  988. struct camss_clock *clock = &vfe->clock[i];
  989. if (!strcmp(clock->name, "vfe0") ||
  990. !strcmp(clock->name, "vfe1")) {
  991. u64 min_rate = 0;
  992. unsigned long rate;
  993. for (j = VFE_LINE_RDI0; j <= VFE_LINE_PIX; j++) {
  994. u32 tmp;
  995. u8 bpp;
  996. if (j == VFE_LINE_PIX) {
  997. tmp = pixel_clock[j];
  998. } else {
  999. struct vfe_line *l = &vfe->line[j];
  1000. bpp = vfe_get_bpp(l->formats,
  1001. l->nformats,
  1002. l->fmt[MSM_VFE_PAD_SINK].code);
  1003. tmp = pixel_clock[j] * bpp / 64;
  1004. }
  1005. if (min_rate < tmp)
  1006. min_rate = tmp;
  1007. }
  1008. camss_add_clock_margin(&min_rate);
  1009. rate = clk_get_rate(clock->clk);
  1010. if (rate < min_rate)
  1011. return -EBUSY;
  1012. }
  1013. }
  1014. return 0;
  1015. }
  1016. /*
  1017. * vfe_get - Power up and reset VFE module
  1018. * @vfe: VFE Device
  1019. *
  1020. * Return 0 on success or a negative error code otherwise
  1021. */
  1022. static int vfe_get(struct vfe_device *vfe)
  1023. {
  1024. int ret;
  1025. mutex_lock(&vfe->power_lock);
  1026. if (vfe->power_count == 0) {
  1027. ret = camss_pm_domain_on(vfe->camss, vfe->id);
  1028. if (ret < 0)
  1029. goto error_pm_domain;
  1030. ret = pm_runtime_get_sync(vfe->camss->dev);
  1031. if (ret < 0)
  1032. goto error_pm_runtime_get;
  1033. ret = vfe_set_clock_rates(vfe);
  1034. if (ret < 0)
  1035. goto error_clocks;
  1036. ret = camss_enable_clocks(vfe->nclocks, vfe->clock,
  1037. vfe->camss->dev);
  1038. if (ret < 0)
  1039. goto error_clocks;
  1040. ret = vfe_reset(vfe);
  1041. if (ret < 0)
  1042. goto error_reset;
  1043. vfe_reset_output_maps(vfe);
  1044. vfe_init_outputs(vfe);
  1045. } else {
  1046. ret = vfe_check_clock_rates(vfe);
  1047. if (ret < 0)
  1048. goto error_clocks;
  1049. }
  1050. vfe->power_count++;
  1051. mutex_unlock(&vfe->power_lock);
  1052. return 0;
  1053. error_reset:
  1054. camss_disable_clocks(vfe->nclocks, vfe->clock);
  1055. error_clocks:
  1056. pm_runtime_put_sync(vfe->camss->dev);
  1057. error_pm_runtime_get:
  1058. camss_pm_domain_off(vfe->camss, vfe->id);
  1059. error_pm_domain:
  1060. mutex_unlock(&vfe->power_lock);
  1061. return ret;
  1062. }
  1063. /*
  1064. * vfe_put - Power down VFE module
  1065. * @vfe: VFE Device
  1066. */
  1067. static void vfe_put(struct vfe_device *vfe)
  1068. {
  1069. mutex_lock(&vfe->power_lock);
  1070. if (vfe->power_count == 0) {
  1071. dev_err(vfe->camss->dev, "vfe power off on power_count == 0\n");
  1072. goto exit;
  1073. } else if (vfe->power_count == 1) {
  1074. if (vfe->was_streaming) {
  1075. vfe->was_streaming = 0;
  1076. vfe_halt(vfe);
  1077. }
  1078. camss_disable_clocks(vfe->nclocks, vfe->clock);
  1079. pm_runtime_put_sync(vfe->camss->dev);
  1080. camss_pm_domain_off(vfe->camss, vfe->id);
  1081. }
  1082. vfe->power_count--;
  1083. exit:
  1084. mutex_unlock(&vfe->power_lock);
  1085. }
  1086. /*
  1087. * vfe_queue_buffer - Add empty buffer
  1088. * @vid: Video device structure
  1089. * @buf: Buffer to be enqueued
  1090. *
  1091. * Add an empty buffer - depending on the current number of buffers it will be
  1092. * put in pending buffer queue or directly given to the hardware to be filled.
  1093. *
  1094. * Return 0 on success or a negative error code otherwise
  1095. */
  1096. static int vfe_queue_buffer(struct camss_video *vid,
  1097. struct camss_buffer *buf)
  1098. {
  1099. struct vfe_line *line = container_of(vid, struct vfe_line, video_out);
  1100. struct vfe_device *vfe = to_vfe(line);
  1101. struct vfe_output *output;
  1102. unsigned long flags;
  1103. output = &line->output;
  1104. spin_lock_irqsave(&vfe->output_lock, flags);
  1105. vfe_buf_update_wm_on_new(vfe, output, buf);
  1106. spin_unlock_irqrestore(&vfe->output_lock, flags);
  1107. return 0;
  1108. }
  1109. /*
  1110. * vfe_flush_buffers - Return all vb2 buffers
  1111. * @vid: Video device structure
  1112. * @state: vb2 buffer state of the returned buffers
  1113. *
  1114. * Return all buffers to vb2. This includes queued pending buffers (still
  1115. * unused) and any buffers given to the hardware but again still not used.
  1116. *
  1117. * Return 0 on success or a negative error code otherwise
  1118. */
  1119. static int vfe_flush_buffers(struct camss_video *vid,
  1120. enum vb2_buffer_state state)
  1121. {
  1122. struct vfe_line *line = container_of(vid, struct vfe_line, video_out);
  1123. struct vfe_device *vfe = to_vfe(line);
  1124. struct vfe_output *output;
  1125. unsigned long flags;
  1126. output = &line->output;
  1127. spin_lock_irqsave(&vfe->output_lock, flags);
  1128. vfe_buf_flush_pending(output, state);
  1129. if (output->buf[0])
  1130. vb2_buffer_done(&output->buf[0]->vb.vb2_buf, state);
  1131. if (output->buf[1])
  1132. vb2_buffer_done(&output->buf[1]->vb.vb2_buf, state);
  1133. if (output->last_buffer) {
  1134. vb2_buffer_done(&output->last_buffer->vb.vb2_buf, state);
  1135. output->last_buffer = NULL;
  1136. }
  1137. spin_unlock_irqrestore(&vfe->output_lock, flags);
  1138. return 0;
  1139. }
  1140. /*
  1141. * vfe_set_power - Power on/off VFE module
  1142. * @sd: VFE V4L2 subdevice
  1143. * @on: Requested power state
  1144. *
  1145. * Return 0 on success or a negative error code otherwise
  1146. */
  1147. static int vfe_set_power(struct v4l2_subdev *sd, int on)
  1148. {
  1149. struct vfe_line *line = v4l2_get_subdevdata(sd);
  1150. struct vfe_device *vfe = to_vfe(line);
  1151. int ret;
  1152. if (on) {
  1153. ret = vfe_get(vfe);
  1154. if (ret < 0)
  1155. return ret;
  1156. vfe->ops->hw_version_read(vfe, vfe->camss->dev);
  1157. } else {
  1158. vfe_put(vfe);
  1159. }
  1160. return 0;
  1161. }
  1162. /*
  1163. * vfe_set_stream - Enable/disable streaming on VFE module
  1164. * @sd: VFE V4L2 subdevice
  1165. * @enable: Requested streaming state
  1166. *
  1167. * Main configuration of VFE module is triggered here.
  1168. *
  1169. * Return 0 on success or a negative error code otherwise
  1170. */
  1171. static int vfe_set_stream(struct v4l2_subdev *sd, int enable)
  1172. {
  1173. struct vfe_line *line = v4l2_get_subdevdata(sd);
  1174. struct vfe_device *vfe = to_vfe(line);
  1175. int ret;
  1176. if (enable) {
  1177. ret = vfe_enable(line);
  1178. if (ret < 0)
  1179. dev_err(vfe->camss->dev,
  1180. "Failed to enable vfe outputs\n");
  1181. } else {
  1182. ret = vfe_disable(line);
  1183. if (ret < 0)
  1184. dev_err(vfe->camss->dev,
  1185. "Failed to disable vfe outputs\n");
  1186. }
  1187. return ret;
  1188. }
  1189. /*
  1190. * __vfe_get_format - Get pointer to format structure
  1191. * @line: VFE line
  1192. * @cfg: V4L2 subdev pad configuration
  1193. * @pad: pad from which format is requested
  1194. * @which: TRY or ACTIVE format
  1195. *
  1196. * Return pointer to TRY or ACTIVE format structure
  1197. */
  1198. static struct v4l2_mbus_framefmt *
  1199. __vfe_get_format(struct vfe_line *line,
  1200. struct v4l2_subdev_pad_config *cfg,
  1201. unsigned int pad,
  1202. enum v4l2_subdev_format_whence which)
  1203. {
  1204. if (which == V4L2_SUBDEV_FORMAT_TRY)
  1205. return v4l2_subdev_get_try_format(&line->subdev, cfg, pad);
  1206. return &line->fmt[pad];
  1207. }
  1208. /*
  1209. * __vfe_get_compose - Get pointer to compose selection structure
  1210. * @line: VFE line
  1211. * @cfg: V4L2 subdev pad configuration
  1212. * @which: TRY or ACTIVE format
  1213. *
  1214. * Return pointer to TRY or ACTIVE compose rectangle structure
  1215. */
  1216. static struct v4l2_rect *
  1217. __vfe_get_compose(struct vfe_line *line,
  1218. struct v4l2_subdev_pad_config *cfg,
  1219. enum v4l2_subdev_format_whence which)
  1220. {
  1221. if (which == V4L2_SUBDEV_FORMAT_TRY)
  1222. return v4l2_subdev_get_try_compose(&line->subdev, cfg,
  1223. MSM_VFE_PAD_SINK);
  1224. return &line->compose;
  1225. }
  1226. /*
  1227. * __vfe_get_crop - Get pointer to crop selection structure
  1228. * @line: VFE line
  1229. * @cfg: V4L2 subdev pad configuration
  1230. * @which: TRY or ACTIVE format
  1231. *
  1232. * Return pointer to TRY or ACTIVE crop rectangle structure
  1233. */
  1234. static struct v4l2_rect *
  1235. __vfe_get_crop(struct vfe_line *line,
  1236. struct v4l2_subdev_pad_config *cfg,
  1237. enum v4l2_subdev_format_whence which)
  1238. {
  1239. if (which == V4L2_SUBDEV_FORMAT_TRY)
  1240. return v4l2_subdev_get_try_crop(&line->subdev, cfg,
  1241. MSM_VFE_PAD_SRC);
  1242. return &line->crop;
  1243. }
  1244. /*
  1245. * vfe_try_format - Handle try format by pad subdev method
  1246. * @line: VFE line
  1247. * @cfg: V4L2 subdev pad configuration
  1248. * @pad: pad on which format is requested
  1249. * @fmt: pointer to v4l2 format structure
  1250. * @which: wanted subdev format
  1251. */
  1252. static void vfe_try_format(struct vfe_line *line,
  1253. struct v4l2_subdev_pad_config *cfg,
  1254. unsigned int pad,
  1255. struct v4l2_mbus_framefmt *fmt,
  1256. enum v4l2_subdev_format_whence which)
  1257. {
  1258. unsigned int i;
  1259. u32 code;
  1260. switch (pad) {
  1261. case MSM_VFE_PAD_SINK:
  1262. /* Set format on sink pad */
  1263. for (i = 0; i < line->nformats; i++)
  1264. if (fmt->code == line->formats[i].code)
  1265. break;
  1266. /* If not found, use UYVY as default */
  1267. if (i >= line->nformats)
  1268. fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
  1269. fmt->width = clamp_t(u32, fmt->width, 1, 8191);
  1270. fmt->height = clamp_t(u32, fmt->height, 1, 8191);
  1271. fmt->field = V4L2_FIELD_NONE;
  1272. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  1273. break;
  1274. case MSM_VFE_PAD_SRC:
  1275. /* Set and return a format same as sink pad */
  1276. code = fmt->code;
  1277. *fmt = *__vfe_get_format(line, cfg, MSM_VFE_PAD_SINK, which);
  1278. fmt->code = vfe_src_pad_code(line, fmt->code, 0, code);
  1279. if (line->id == VFE_LINE_PIX) {
  1280. struct v4l2_rect *rect;
  1281. rect = __vfe_get_crop(line, cfg, which);
  1282. fmt->width = rect->width;
  1283. fmt->height = rect->height;
  1284. }
  1285. break;
  1286. }
  1287. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  1288. }
  1289. /*
  1290. * vfe_try_compose - Handle try compose selection by pad subdev method
  1291. * @line: VFE line
  1292. * @cfg: V4L2 subdev pad configuration
  1293. * @rect: pointer to v4l2 rect structure
  1294. * @which: wanted subdev format
  1295. */
  1296. static void vfe_try_compose(struct vfe_line *line,
  1297. struct v4l2_subdev_pad_config *cfg,
  1298. struct v4l2_rect *rect,
  1299. enum v4l2_subdev_format_whence which)
  1300. {
  1301. struct v4l2_mbus_framefmt *fmt;
  1302. fmt = __vfe_get_format(line, cfg, MSM_VFE_PAD_SINK, which);
  1303. if (rect->width > fmt->width)
  1304. rect->width = fmt->width;
  1305. if (rect->height > fmt->height)
  1306. rect->height = fmt->height;
  1307. if (fmt->width > rect->width * SCALER_RATIO_MAX)
  1308. rect->width = (fmt->width + SCALER_RATIO_MAX - 1) /
  1309. SCALER_RATIO_MAX;
  1310. rect->width &= ~0x1;
  1311. if (fmt->height > rect->height * SCALER_RATIO_MAX)
  1312. rect->height = (fmt->height + SCALER_RATIO_MAX - 1) /
  1313. SCALER_RATIO_MAX;
  1314. if (rect->width < 16)
  1315. rect->width = 16;
  1316. if (rect->height < 4)
  1317. rect->height = 4;
  1318. }
  1319. /*
  1320. * vfe_try_crop - Handle try crop selection by pad subdev method
  1321. * @line: VFE line
  1322. * @cfg: V4L2 subdev pad configuration
  1323. * @rect: pointer to v4l2 rect structure
  1324. * @which: wanted subdev format
  1325. */
  1326. static void vfe_try_crop(struct vfe_line *line,
  1327. struct v4l2_subdev_pad_config *cfg,
  1328. struct v4l2_rect *rect,
  1329. enum v4l2_subdev_format_whence which)
  1330. {
  1331. struct v4l2_rect *compose;
  1332. compose = __vfe_get_compose(line, cfg, which);
  1333. if (rect->width > compose->width)
  1334. rect->width = compose->width;
  1335. if (rect->width + rect->left > compose->width)
  1336. rect->left = compose->width - rect->width;
  1337. if (rect->height > compose->height)
  1338. rect->height = compose->height;
  1339. if (rect->height + rect->top > compose->height)
  1340. rect->top = compose->height - rect->height;
  1341. /* wm in line based mode writes multiple of 16 horizontally */
  1342. rect->left += (rect->width & 0xf) >> 1;
  1343. rect->width &= ~0xf;
  1344. if (rect->width < 16) {
  1345. rect->left = 0;
  1346. rect->width = 16;
  1347. }
  1348. if (rect->height < 4) {
  1349. rect->top = 0;
  1350. rect->height = 4;
  1351. }
  1352. }
  1353. /*
  1354. * vfe_enum_mbus_code - Handle pixel format enumeration
  1355. * @sd: VFE V4L2 subdevice
  1356. * @cfg: V4L2 subdev pad configuration
  1357. * @code: pointer to v4l2_subdev_mbus_code_enum structure
  1358. *
  1359. * return -EINVAL or zero on success
  1360. */
  1361. static int vfe_enum_mbus_code(struct v4l2_subdev *sd,
  1362. struct v4l2_subdev_pad_config *cfg,
  1363. struct v4l2_subdev_mbus_code_enum *code)
  1364. {
  1365. struct vfe_line *line = v4l2_get_subdevdata(sd);
  1366. if (code->pad == MSM_VFE_PAD_SINK) {
  1367. if (code->index >= line->nformats)
  1368. return -EINVAL;
  1369. code->code = line->formats[code->index].code;
  1370. } else {
  1371. struct v4l2_mbus_framefmt *sink_fmt;
  1372. sink_fmt = __vfe_get_format(line, cfg, MSM_VFE_PAD_SINK,
  1373. code->which);
  1374. code->code = vfe_src_pad_code(line, sink_fmt->code,
  1375. code->index, 0);
  1376. if (!code->code)
  1377. return -EINVAL;
  1378. }
  1379. return 0;
  1380. }
  1381. /*
  1382. * vfe_enum_frame_size - Handle frame size enumeration
  1383. * @sd: VFE V4L2 subdevice
  1384. * @cfg: V4L2 subdev pad configuration
  1385. * @fse: pointer to v4l2_subdev_frame_size_enum structure
  1386. *
  1387. * Return -EINVAL or zero on success
  1388. */
  1389. static int vfe_enum_frame_size(struct v4l2_subdev *sd,
  1390. struct v4l2_subdev_pad_config *cfg,
  1391. struct v4l2_subdev_frame_size_enum *fse)
  1392. {
  1393. struct vfe_line *line = v4l2_get_subdevdata(sd);
  1394. struct v4l2_mbus_framefmt format;
  1395. if (fse->index != 0)
  1396. return -EINVAL;
  1397. format.code = fse->code;
  1398. format.width = 1;
  1399. format.height = 1;
  1400. vfe_try_format(line, cfg, fse->pad, &format, fse->which);
  1401. fse->min_width = format.width;
  1402. fse->min_height = format.height;
  1403. if (format.code != fse->code)
  1404. return -EINVAL;
  1405. format.code = fse->code;
  1406. format.width = -1;
  1407. format.height = -1;
  1408. vfe_try_format(line, cfg, fse->pad, &format, fse->which);
  1409. fse->max_width = format.width;
  1410. fse->max_height = format.height;
  1411. return 0;
  1412. }
  1413. /*
  1414. * vfe_get_format - Handle get format by pads subdev method
  1415. * @sd: VFE V4L2 subdevice
  1416. * @cfg: V4L2 subdev pad configuration
  1417. * @fmt: pointer to v4l2 subdev format structure
  1418. *
  1419. * Return -EINVAL or zero on success
  1420. */
  1421. static int vfe_get_format(struct v4l2_subdev *sd,
  1422. struct v4l2_subdev_pad_config *cfg,
  1423. struct v4l2_subdev_format *fmt)
  1424. {
  1425. struct vfe_line *line = v4l2_get_subdevdata(sd);
  1426. struct v4l2_mbus_framefmt *format;
  1427. format = __vfe_get_format(line, cfg, fmt->pad, fmt->which);
  1428. if (format == NULL)
  1429. return -EINVAL;
  1430. fmt->format = *format;
  1431. return 0;
  1432. }
  1433. static int vfe_set_selection(struct v4l2_subdev *sd,
  1434. struct v4l2_subdev_pad_config *cfg,
  1435. struct v4l2_subdev_selection *sel);
  1436. /*
  1437. * vfe_set_format - Handle set format by pads subdev method
  1438. * @sd: VFE V4L2 subdevice
  1439. * @cfg: V4L2 subdev pad configuration
  1440. * @fmt: pointer to v4l2 subdev format structure
  1441. *
  1442. * Return -EINVAL or zero on success
  1443. */
  1444. static int vfe_set_format(struct v4l2_subdev *sd,
  1445. struct v4l2_subdev_pad_config *cfg,
  1446. struct v4l2_subdev_format *fmt)
  1447. {
  1448. struct vfe_line *line = v4l2_get_subdevdata(sd);
  1449. struct v4l2_mbus_framefmt *format;
  1450. format = __vfe_get_format(line, cfg, fmt->pad, fmt->which);
  1451. if (format == NULL)
  1452. return -EINVAL;
  1453. vfe_try_format(line, cfg, fmt->pad, &fmt->format, fmt->which);
  1454. *format = fmt->format;
  1455. if (fmt->pad == MSM_VFE_PAD_SINK) {
  1456. struct v4l2_subdev_selection sel = { 0 };
  1457. int ret;
  1458. /* Propagate the format from sink to source */
  1459. format = __vfe_get_format(line, cfg, MSM_VFE_PAD_SRC,
  1460. fmt->which);
  1461. *format = fmt->format;
  1462. vfe_try_format(line, cfg, MSM_VFE_PAD_SRC, format,
  1463. fmt->which);
  1464. if (line->id != VFE_LINE_PIX)
  1465. return 0;
  1466. /* Reset sink pad compose selection */
  1467. sel.which = fmt->which;
  1468. sel.pad = MSM_VFE_PAD_SINK;
  1469. sel.target = V4L2_SEL_TGT_COMPOSE;
  1470. sel.r.width = fmt->format.width;
  1471. sel.r.height = fmt->format.height;
  1472. ret = vfe_set_selection(sd, cfg, &sel);
  1473. if (ret < 0)
  1474. return ret;
  1475. }
  1476. return 0;
  1477. }
  1478. /*
  1479. * vfe_get_selection - Handle get selection by pads subdev method
  1480. * @sd: VFE V4L2 subdevice
  1481. * @cfg: V4L2 subdev pad configuration
  1482. * @sel: pointer to v4l2 subdev selection structure
  1483. *
  1484. * Return -EINVAL or zero on success
  1485. */
  1486. static int vfe_get_selection(struct v4l2_subdev *sd,
  1487. struct v4l2_subdev_pad_config *cfg,
  1488. struct v4l2_subdev_selection *sel)
  1489. {
  1490. struct vfe_line *line = v4l2_get_subdevdata(sd);
  1491. struct v4l2_subdev_format fmt = { 0 };
  1492. struct v4l2_rect *rect;
  1493. int ret;
  1494. if (line->id != VFE_LINE_PIX)
  1495. return -EINVAL;
  1496. if (sel->pad == MSM_VFE_PAD_SINK)
  1497. switch (sel->target) {
  1498. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1499. fmt.pad = sel->pad;
  1500. fmt.which = sel->which;
  1501. ret = vfe_get_format(sd, cfg, &fmt);
  1502. if (ret < 0)
  1503. return ret;
  1504. sel->r.left = 0;
  1505. sel->r.top = 0;
  1506. sel->r.width = fmt.format.width;
  1507. sel->r.height = fmt.format.height;
  1508. break;
  1509. case V4L2_SEL_TGT_COMPOSE:
  1510. rect = __vfe_get_compose(line, cfg, sel->which);
  1511. if (rect == NULL)
  1512. return -EINVAL;
  1513. sel->r = *rect;
  1514. break;
  1515. default:
  1516. return -EINVAL;
  1517. }
  1518. else if (sel->pad == MSM_VFE_PAD_SRC)
  1519. switch (sel->target) {
  1520. case V4L2_SEL_TGT_CROP_BOUNDS:
  1521. rect = __vfe_get_compose(line, cfg, sel->which);
  1522. if (rect == NULL)
  1523. return -EINVAL;
  1524. sel->r.left = rect->left;
  1525. sel->r.top = rect->top;
  1526. sel->r.width = rect->width;
  1527. sel->r.height = rect->height;
  1528. break;
  1529. case V4L2_SEL_TGT_CROP:
  1530. rect = __vfe_get_crop(line, cfg, sel->which);
  1531. if (rect == NULL)
  1532. return -EINVAL;
  1533. sel->r = *rect;
  1534. break;
  1535. default:
  1536. return -EINVAL;
  1537. }
  1538. return 0;
  1539. }
  1540. /*
  1541. * vfe_set_selection - Handle set selection by pads subdev method
  1542. * @sd: VFE V4L2 subdevice
  1543. * @cfg: V4L2 subdev pad configuration
  1544. * @sel: pointer to v4l2 subdev selection structure
  1545. *
  1546. * Return -EINVAL or zero on success
  1547. */
  1548. static int vfe_set_selection(struct v4l2_subdev *sd,
  1549. struct v4l2_subdev_pad_config *cfg,
  1550. struct v4l2_subdev_selection *sel)
  1551. {
  1552. struct vfe_line *line = v4l2_get_subdevdata(sd);
  1553. struct v4l2_rect *rect;
  1554. int ret;
  1555. if (line->id != VFE_LINE_PIX)
  1556. return -EINVAL;
  1557. if (sel->target == V4L2_SEL_TGT_COMPOSE &&
  1558. sel->pad == MSM_VFE_PAD_SINK) {
  1559. struct v4l2_subdev_selection crop = { 0 };
  1560. rect = __vfe_get_compose(line, cfg, sel->which);
  1561. if (rect == NULL)
  1562. return -EINVAL;
  1563. vfe_try_compose(line, cfg, &sel->r, sel->which);
  1564. *rect = sel->r;
  1565. /* Reset source crop selection */
  1566. crop.which = sel->which;
  1567. crop.pad = MSM_VFE_PAD_SRC;
  1568. crop.target = V4L2_SEL_TGT_CROP;
  1569. crop.r = *rect;
  1570. ret = vfe_set_selection(sd, cfg, &crop);
  1571. } else if (sel->target == V4L2_SEL_TGT_CROP &&
  1572. sel->pad == MSM_VFE_PAD_SRC) {
  1573. struct v4l2_subdev_format fmt = { 0 };
  1574. rect = __vfe_get_crop(line, cfg, sel->which);
  1575. if (rect == NULL)
  1576. return -EINVAL;
  1577. vfe_try_crop(line, cfg, &sel->r, sel->which);
  1578. *rect = sel->r;
  1579. /* Reset source pad format width and height */
  1580. fmt.which = sel->which;
  1581. fmt.pad = MSM_VFE_PAD_SRC;
  1582. ret = vfe_get_format(sd, cfg, &fmt);
  1583. if (ret < 0)
  1584. return ret;
  1585. fmt.format.width = rect->width;
  1586. fmt.format.height = rect->height;
  1587. ret = vfe_set_format(sd, cfg, &fmt);
  1588. } else {
  1589. ret = -EINVAL;
  1590. }
  1591. return ret;
  1592. }
  1593. /*
  1594. * vfe_init_formats - Initialize formats on all pads
  1595. * @sd: VFE V4L2 subdevice
  1596. * @fh: V4L2 subdev file handle
  1597. *
  1598. * Initialize all pad formats with default values.
  1599. *
  1600. * Return 0 on success or a negative error code otherwise
  1601. */
  1602. static int vfe_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1603. {
  1604. struct v4l2_subdev_format format = {
  1605. .pad = MSM_VFE_PAD_SINK,
  1606. .which = fh ? V4L2_SUBDEV_FORMAT_TRY :
  1607. V4L2_SUBDEV_FORMAT_ACTIVE,
  1608. .format = {
  1609. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  1610. .width = 1920,
  1611. .height = 1080
  1612. }
  1613. };
  1614. return vfe_set_format(sd, fh ? fh->pad : NULL, &format);
  1615. }
  1616. /*
  1617. * msm_vfe_subdev_init - Initialize VFE device structure and resources
  1618. * @vfe: VFE device
  1619. * @res: VFE module resources table
  1620. *
  1621. * Return 0 on success or a negative error code otherwise
  1622. */
  1623. int msm_vfe_subdev_init(struct camss *camss, struct vfe_device *vfe,
  1624. const struct resources *res, u8 id)
  1625. {
  1626. struct device *dev = camss->dev;
  1627. struct platform_device *pdev = to_platform_device(dev);
  1628. struct resource *r;
  1629. int i, j;
  1630. int ret;
  1631. vfe->isr_ops.reset_ack = vfe_isr_reset_ack;
  1632. vfe->isr_ops.halt_ack = vfe_isr_halt_ack;
  1633. vfe->isr_ops.reg_update = vfe_isr_reg_update;
  1634. vfe->isr_ops.sof = vfe_isr_sof;
  1635. vfe->isr_ops.comp_done = vfe_isr_comp_done;
  1636. vfe->isr_ops.wm_done = vfe_isr_wm_done;
  1637. if (camss->version == CAMSS_8x16)
  1638. vfe->ops = &vfe_ops_4_1;
  1639. else if (camss->version == CAMSS_8x96)
  1640. vfe->ops = &vfe_ops_4_7;
  1641. else
  1642. return -EINVAL;
  1643. /* Memory */
  1644. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res->reg[0]);
  1645. vfe->base = devm_ioremap_resource(dev, r);
  1646. if (IS_ERR(vfe->base)) {
  1647. dev_err(dev, "could not map memory\n");
  1648. return PTR_ERR(vfe->base);
  1649. }
  1650. /* Interrupt */
  1651. r = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  1652. res->interrupt[0]);
  1653. if (!r) {
  1654. dev_err(dev, "missing IRQ\n");
  1655. return -EINVAL;
  1656. }
  1657. vfe->irq = r->start;
  1658. snprintf(vfe->irq_name, sizeof(vfe->irq_name), "%s_%s%d",
  1659. dev_name(dev), MSM_VFE_NAME, vfe->id);
  1660. ret = devm_request_irq(dev, vfe->irq, vfe->ops->isr,
  1661. IRQF_TRIGGER_RISING, vfe->irq_name, vfe);
  1662. if (ret < 0) {
  1663. dev_err(dev, "request_irq failed: %d\n", ret);
  1664. return ret;
  1665. }
  1666. /* Clocks */
  1667. vfe->nclocks = 0;
  1668. while (res->clock[vfe->nclocks])
  1669. vfe->nclocks++;
  1670. vfe->clock = devm_kcalloc(dev, vfe->nclocks, sizeof(*vfe->clock),
  1671. GFP_KERNEL);
  1672. if (!vfe->clock)
  1673. return -ENOMEM;
  1674. for (i = 0; i < vfe->nclocks; i++) {
  1675. struct camss_clock *clock = &vfe->clock[i];
  1676. clock->clk = devm_clk_get(dev, res->clock[i]);
  1677. if (IS_ERR(clock->clk))
  1678. return PTR_ERR(clock->clk);
  1679. clock->name = res->clock[i];
  1680. clock->nfreqs = 0;
  1681. while (res->clock_rate[i][clock->nfreqs])
  1682. clock->nfreqs++;
  1683. if (!clock->nfreqs) {
  1684. clock->freq = NULL;
  1685. continue;
  1686. }
  1687. clock->freq = devm_kcalloc(dev,
  1688. clock->nfreqs,
  1689. sizeof(*clock->freq),
  1690. GFP_KERNEL);
  1691. if (!clock->freq)
  1692. return -ENOMEM;
  1693. for (j = 0; j < clock->nfreqs; j++)
  1694. clock->freq[j] = res->clock_rate[i][j];
  1695. }
  1696. mutex_init(&vfe->power_lock);
  1697. vfe->power_count = 0;
  1698. mutex_init(&vfe->stream_lock);
  1699. vfe->stream_count = 0;
  1700. spin_lock_init(&vfe->output_lock);
  1701. vfe->camss = camss;
  1702. vfe->id = id;
  1703. vfe->reg_update = 0;
  1704. for (i = VFE_LINE_RDI0; i <= VFE_LINE_PIX; i++) {
  1705. struct vfe_line *l = &vfe->line[i];
  1706. l->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1707. l->video_out.camss = camss;
  1708. l->id = i;
  1709. init_completion(&l->output.sof);
  1710. init_completion(&l->output.reg_update);
  1711. if (camss->version == CAMSS_8x16) {
  1712. if (i == VFE_LINE_PIX) {
  1713. l->formats = formats_pix_8x16;
  1714. l->nformats = ARRAY_SIZE(formats_pix_8x16);
  1715. } else {
  1716. l->formats = formats_rdi_8x16;
  1717. l->nformats = ARRAY_SIZE(formats_rdi_8x16);
  1718. }
  1719. } else if (camss->version == CAMSS_8x96) {
  1720. if (i == VFE_LINE_PIX) {
  1721. l->formats = formats_pix_8x96;
  1722. l->nformats = ARRAY_SIZE(formats_pix_8x96);
  1723. } else {
  1724. l->formats = formats_rdi_8x96;
  1725. l->nformats = ARRAY_SIZE(formats_rdi_8x96);
  1726. }
  1727. } else {
  1728. return -EINVAL;
  1729. }
  1730. }
  1731. init_completion(&vfe->reset_complete);
  1732. init_completion(&vfe->halt_complete);
  1733. return 0;
  1734. }
  1735. /*
  1736. * msm_vfe_get_vfe_id - Get VFE HW module id
  1737. * @entity: Pointer to VFE media entity structure
  1738. * @id: Return CSID HW module id here
  1739. */
  1740. void msm_vfe_get_vfe_id(struct media_entity *entity, u8 *id)
  1741. {
  1742. struct v4l2_subdev *sd;
  1743. struct vfe_line *line;
  1744. struct vfe_device *vfe;
  1745. sd = media_entity_to_v4l2_subdev(entity);
  1746. line = v4l2_get_subdevdata(sd);
  1747. vfe = to_vfe(line);
  1748. *id = vfe->id;
  1749. }
  1750. /*
  1751. * msm_vfe_get_vfe_line_id - Get VFE line id by media entity
  1752. * @entity: Pointer to VFE media entity structure
  1753. * @id: Return VFE line id here
  1754. */
  1755. void msm_vfe_get_vfe_line_id(struct media_entity *entity, enum vfe_line_id *id)
  1756. {
  1757. struct v4l2_subdev *sd;
  1758. struct vfe_line *line;
  1759. sd = media_entity_to_v4l2_subdev(entity);
  1760. line = v4l2_get_subdevdata(sd);
  1761. *id = line->id;
  1762. }
  1763. /*
  1764. * vfe_link_setup - Setup VFE connections
  1765. * @entity: Pointer to media entity structure
  1766. * @local: Pointer to local pad
  1767. * @remote: Pointer to remote pad
  1768. * @flags: Link flags
  1769. *
  1770. * Return 0 on success
  1771. */
  1772. static int vfe_link_setup(struct media_entity *entity,
  1773. const struct media_pad *local,
  1774. const struct media_pad *remote, u32 flags)
  1775. {
  1776. if (flags & MEDIA_LNK_FL_ENABLED)
  1777. if (media_entity_remote_pad(local))
  1778. return -EBUSY;
  1779. return 0;
  1780. }
  1781. static const struct v4l2_subdev_core_ops vfe_core_ops = {
  1782. .s_power = vfe_set_power,
  1783. };
  1784. static const struct v4l2_subdev_video_ops vfe_video_ops = {
  1785. .s_stream = vfe_set_stream,
  1786. };
  1787. static const struct v4l2_subdev_pad_ops vfe_pad_ops = {
  1788. .enum_mbus_code = vfe_enum_mbus_code,
  1789. .enum_frame_size = vfe_enum_frame_size,
  1790. .get_fmt = vfe_get_format,
  1791. .set_fmt = vfe_set_format,
  1792. .get_selection = vfe_get_selection,
  1793. .set_selection = vfe_set_selection,
  1794. };
  1795. static const struct v4l2_subdev_ops vfe_v4l2_ops = {
  1796. .core = &vfe_core_ops,
  1797. .video = &vfe_video_ops,
  1798. .pad = &vfe_pad_ops,
  1799. };
  1800. static const struct v4l2_subdev_internal_ops vfe_v4l2_internal_ops = {
  1801. .open = vfe_init_formats,
  1802. };
  1803. static const struct media_entity_operations vfe_media_ops = {
  1804. .link_setup = vfe_link_setup,
  1805. .link_validate = v4l2_subdev_link_validate,
  1806. };
  1807. static const struct camss_video_ops camss_vfe_video_ops = {
  1808. .queue_buffer = vfe_queue_buffer,
  1809. .flush_buffers = vfe_flush_buffers,
  1810. };
  1811. void msm_vfe_stop_streaming(struct vfe_device *vfe)
  1812. {
  1813. int i;
  1814. for (i = 0; i < ARRAY_SIZE(vfe->line); i++)
  1815. msm_video_stop_streaming(&vfe->line[i].video_out);
  1816. }
  1817. /*
  1818. * msm_vfe_register_entities - Register subdev node for VFE module
  1819. * @vfe: VFE device
  1820. * @v4l2_dev: V4L2 device
  1821. *
  1822. * Initialize and register a subdev node for the VFE module. Then
  1823. * call msm_video_register() to register the video device node which
  1824. * will be connected to this subdev node. Then actually create the
  1825. * media link between them.
  1826. *
  1827. * Return 0 on success or a negative error code otherwise
  1828. */
  1829. int msm_vfe_register_entities(struct vfe_device *vfe,
  1830. struct v4l2_device *v4l2_dev)
  1831. {
  1832. struct device *dev = vfe->camss->dev;
  1833. struct v4l2_subdev *sd;
  1834. struct media_pad *pads;
  1835. struct camss_video *video_out;
  1836. int ret;
  1837. int i;
  1838. for (i = 0; i < ARRAY_SIZE(vfe->line); i++) {
  1839. char name[32];
  1840. sd = &vfe->line[i].subdev;
  1841. pads = vfe->line[i].pads;
  1842. video_out = &vfe->line[i].video_out;
  1843. v4l2_subdev_init(sd, &vfe_v4l2_ops);
  1844. sd->internal_ops = &vfe_v4l2_internal_ops;
  1845. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1846. if (i == VFE_LINE_PIX)
  1847. snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d_%s",
  1848. MSM_VFE_NAME, vfe->id, "pix");
  1849. else
  1850. snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d_%s%d",
  1851. MSM_VFE_NAME, vfe->id, "rdi", i);
  1852. v4l2_set_subdevdata(sd, &vfe->line[i]);
  1853. ret = vfe_init_formats(sd, NULL);
  1854. if (ret < 0) {
  1855. dev_err(dev, "Failed to init format: %d\n", ret);
  1856. goto error_init;
  1857. }
  1858. pads[MSM_VFE_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1859. pads[MSM_VFE_PAD_SRC].flags = MEDIA_PAD_FL_SOURCE;
  1860. sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
  1861. sd->entity.ops = &vfe_media_ops;
  1862. ret = media_entity_pads_init(&sd->entity, MSM_VFE_PADS_NUM,
  1863. pads);
  1864. if (ret < 0) {
  1865. dev_err(dev, "Failed to init media entity: %d\n", ret);
  1866. goto error_init;
  1867. }
  1868. ret = v4l2_device_register_subdev(v4l2_dev, sd);
  1869. if (ret < 0) {
  1870. dev_err(dev, "Failed to register subdev: %d\n", ret);
  1871. goto error_reg_subdev;
  1872. }
  1873. video_out->ops = &camss_vfe_video_ops;
  1874. video_out->bpl_alignment = 8;
  1875. video_out->line_based = 0;
  1876. if (i == VFE_LINE_PIX) {
  1877. video_out->bpl_alignment = 16;
  1878. video_out->line_based = 1;
  1879. }
  1880. snprintf(name, ARRAY_SIZE(name), "%s%d_%s%d",
  1881. MSM_VFE_NAME, vfe->id, "video", i);
  1882. ret = msm_video_register(video_out, v4l2_dev, name,
  1883. i == VFE_LINE_PIX ? 1 : 0);
  1884. if (ret < 0) {
  1885. dev_err(dev, "Failed to register video node: %d\n",
  1886. ret);
  1887. goto error_reg_video;
  1888. }
  1889. ret = media_create_pad_link(
  1890. &sd->entity, MSM_VFE_PAD_SRC,
  1891. &video_out->vdev.entity, 0,
  1892. MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
  1893. if (ret < 0) {
  1894. dev_err(dev, "Failed to link %s->%s entities: %d\n",
  1895. sd->entity.name, video_out->vdev.entity.name,
  1896. ret);
  1897. goto error_link;
  1898. }
  1899. }
  1900. return 0;
  1901. error_link:
  1902. msm_video_unregister(video_out);
  1903. error_reg_video:
  1904. v4l2_device_unregister_subdev(sd);
  1905. error_reg_subdev:
  1906. media_entity_cleanup(&sd->entity);
  1907. error_init:
  1908. for (i--; i >= 0; i--) {
  1909. sd = &vfe->line[i].subdev;
  1910. video_out = &vfe->line[i].video_out;
  1911. msm_video_unregister(video_out);
  1912. v4l2_device_unregister_subdev(sd);
  1913. media_entity_cleanup(&sd->entity);
  1914. }
  1915. return ret;
  1916. }
  1917. /*
  1918. * msm_vfe_unregister_entities - Unregister VFE module subdev node
  1919. * @vfe: VFE device
  1920. */
  1921. void msm_vfe_unregister_entities(struct vfe_device *vfe)
  1922. {
  1923. int i;
  1924. mutex_destroy(&vfe->power_lock);
  1925. mutex_destroy(&vfe->stream_lock);
  1926. for (i = 0; i < ARRAY_SIZE(vfe->line); i++) {
  1927. struct v4l2_subdev *sd = &vfe->line[i].subdev;
  1928. struct camss_video *video_out = &vfe->line[i].video_out;
  1929. msm_video_unregister(video_out);
  1930. v4l2_device_unregister_subdev(sd);
  1931. media_entity_cleanup(&sd->entity);
  1932. }
  1933. }