npcm-video.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Video Capture/Differentiation Engine (VCD) and Encoding
  4. * Compression Engine (ECE) present on Nuvoton NPCM SoCs.
  5. *
  6. * Copyright (C) 2022 Nuvoton Technologies
  7. */
  8. #include <linux/atomic.h>
  9. #include <linux/bitfield.h>
  10. #include <linux/bitmap.h>
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/of.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/of_reserved_mem.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/regmap.h>
  26. #include <linux/reset.h>
  27. #include <linux/sched.h>
  28. #include <linux/string.h>
  29. #include <linux/v4l2-controls.h>
  30. #include <linux/videodev2.h>
  31. #include <media/v4l2-ctrls.h>
  32. #include <media/v4l2-dev.h>
  33. #include <media/v4l2-device.h>
  34. #include <media/v4l2-dv-timings.h>
  35. #include <media/v4l2-event.h>
  36. #include <media/v4l2-ioctl.h>
  37. #include <media/videobuf2-dma-contig.h>
  38. #include <uapi/linux/npcm-video.h>
  39. #include "npcm-regs.h"
  40. #define DEVICE_NAME "npcm-video"
  41. #define MAX_WIDTH 1920
  42. #define MAX_HEIGHT 1200
  43. #define MIN_WIDTH 320
  44. #define MIN_HEIGHT 240
  45. #define MIN_LP 512
  46. #define MAX_LP 4096
  47. #define RECT_W 16
  48. #define RECT_H 16
  49. #define BITMAP_SIZE 32
  50. struct npcm_video_addr {
  51. size_t size;
  52. dma_addr_t dma;
  53. void *virt;
  54. };
  55. struct npcm_video_buffer {
  56. struct vb2_v4l2_buffer vb;
  57. struct list_head link;
  58. };
  59. #define to_npcm_video_buffer(x) \
  60. container_of((x), struct npcm_video_buffer, vb)
  61. /*
  62. * VIDEO_STREAMING: a flag indicating if the video has started streaming
  63. * VIDEO_CAPTURING: a flag indicating if the VCD is capturing a frame
  64. * VIDEO_RES_CHANGING: a flag indicating if the resolution is changing
  65. * VIDEO_STOPPED: a flag indicating if the video has stopped streaming
  66. */
  67. enum {
  68. VIDEO_STREAMING,
  69. VIDEO_CAPTURING,
  70. VIDEO_RES_CHANGING,
  71. VIDEO_STOPPED,
  72. };
  73. struct rect_list {
  74. struct v4l2_clip clip;
  75. struct list_head list;
  76. };
  77. struct rect_list_info {
  78. struct rect_list *list;
  79. struct rect_list *first;
  80. struct list_head *head;
  81. unsigned int index;
  82. unsigned int tile_perline;
  83. unsigned int tile_perrow;
  84. unsigned int offset_perline;
  85. unsigned int tile_size;
  86. unsigned int tile_cnt;
  87. };
  88. struct npcm_ece {
  89. struct regmap *regmap;
  90. atomic_t clients;
  91. struct reset_control *reset;
  92. bool enable;
  93. };
  94. struct npcm_video {
  95. struct regmap *gcr_regmap;
  96. struct regmap *gfx_regmap;
  97. struct regmap *vcd_regmap;
  98. struct device *dev;
  99. struct v4l2_ctrl_handler ctrl_handler;
  100. struct v4l2_ctrl *rect_cnt_ctrl;
  101. struct v4l2_device v4l2_dev;
  102. struct v4l2_pix_format pix_fmt;
  103. struct v4l2_bt_timings active_timings;
  104. struct v4l2_bt_timings detected_timings;
  105. unsigned int v4l2_input_status;
  106. struct vb2_queue queue;
  107. struct video_device vdev;
  108. struct mutex video_lock; /* v4l2 and videobuf2 lock */
  109. struct list_head buffers;
  110. struct mutex buffer_lock; /* buffer list lock */
  111. unsigned long flags;
  112. unsigned int sequence;
  113. struct npcm_video_addr src;
  114. struct reset_control *reset;
  115. struct npcm_ece ece;
  116. unsigned int bytesperline;
  117. unsigned int bytesperpixel;
  118. unsigned int rect_cnt;
  119. struct list_head list[VIDEO_MAX_FRAME];
  120. unsigned int rect[VIDEO_MAX_FRAME];
  121. unsigned int ctrl_cmd;
  122. unsigned int op_cmd;
  123. };
  124. #define to_npcm_video(x) container_of((x), struct npcm_video, v4l2_dev)
  125. struct npcm_fmt {
  126. unsigned int fourcc;
  127. unsigned int bpp; /* bytes per pixel */
  128. };
  129. static const struct npcm_fmt npcm_fmt_list[] = {
  130. {
  131. .fourcc = V4L2_PIX_FMT_RGB565,
  132. .bpp = 2,
  133. },
  134. {
  135. .fourcc = V4L2_PIX_FMT_HEXTILE,
  136. .bpp = 2,
  137. },
  138. };
  139. #define NUM_FORMATS ARRAY_SIZE(npcm_fmt_list)
  140. static const struct v4l2_dv_timings_cap npcm_video_timings_cap = {
  141. .type = V4L2_DV_BT_656_1120,
  142. .bt = {
  143. .min_width = MIN_WIDTH,
  144. .max_width = MAX_WIDTH,
  145. .min_height = MIN_HEIGHT,
  146. .max_height = MAX_HEIGHT,
  147. .min_pixelclock = 6574080, /* 640 x 480 x 24Hz */
  148. .max_pixelclock = 138240000, /* 1920 x 1200 x 60Hz */
  149. .standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  150. V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF,
  151. .capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
  152. V4L2_DV_BT_CAP_REDUCED_BLANKING |
  153. V4L2_DV_BT_CAP_CUSTOM,
  154. },
  155. };
  156. static DECLARE_BITMAP(bitmap, BITMAP_SIZE);
  157. static const struct npcm_fmt *npcm_video_find_format(struct v4l2_format *f)
  158. {
  159. const struct npcm_fmt *fmt;
  160. unsigned int k;
  161. for (k = 0; k < NUM_FORMATS; k++) {
  162. fmt = &npcm_fmt_list[k];
  163. if (fmt->fourcc == f->fmt.pix.pixelformat)
  164. break;
  165. }
  166. if (k == NUM_FORMATS)
  167. return NULL;
  168. return &npcm_fmt_list[k];
  169. }
  170. static void npcm_video_ece_prepend_rect_header(void *addr, u16 x, u16 y, u16 w, u16 h)
  171. {
  172. __be16 x_pos = cpu_to_be16(x);
  173. __be16 y_pos = cpu_to_be16(y);
  174. __be16 width = cpu_to_be16(w);
  175. __be16 height = cpu_to_be16(h);
  176. __be32 encoding = cpu_to_be32(5); /* Hextile encoding */
  177. memcpy(addr, &x_pos, 2);
  178. memcpy(addr + 2, &y_pos, 2);
  179. memcpy(addr + 4, &width, 2);
  180. memcpy(addr + 6, &height, 2);
  181. memcpy(addr + 8, &encoding, 4);
  182. }
  183. static unsigned int npcm_video_ece_get_ed_size(struct npcm_video *video,
  184. unsigned int offset, void *addr)
  185. {
  186. struct regmap *ece = video->ece.regmap;
  187. unsigned int size, gap, val;
  188. int ret;
  189. ret = regmap_read_poll_timeout(ece, ECE_DDA_STS, val,
  190. (val & ECE_DDA_STS_CDREADY), 0,
  191. ECE_POLL_TIMEOUT_US);
  192. if (ret) {
  193. dev_warn(video->dev, "Wait for ECE_DDA_STS_CDREADY timeout\n");
  194. return 0;
  195. }
  196. size = readl((void __iomem *)addr + offset);
  197. regmap_read(ece, ECE_HEX_CTRL, &val);
  198. gap = FIELD_GET(ECE_HEX_CTRL_ENC_GAP, val);
  199. dev_dbg(video->dev, "offset = %u, ed_size = %u, gap = %u\n", offset,
  200. size, gap);
  201. return size + gap;
  202. }
  203. static void npcm_video_ece_enc_rect(struct npcm_video *video,
  204. unsigned int r_off_x, unsigned int r_off_y,
  205. unsigned int r_w, unsigned int r_h)
  206. {
  207. struct regmap *ece = video->ece.regmap;
  208. unsigned int rect_offset = (r_off_y * video->bytesperline) + (r_off_x * 2);
  209. unsigned int w_size = ECE_TILE_W, h_size = ECE_TILE_H;
  210. unsigned int temp, w_tile, h_tile;
  211. regmap_update_bits(ece, ECE_DDA_CTRL, ECE_DDA_CTRL_ECEEN, 0);
  212. regmap_update_bits(ece, ECE_DDA_CTRL, ECE_DDA_CTRL_ECEEN, ECE_DDA_CTRL_ECEEN);
  213. regmap_write(ece, ECE_DDA_STS, ECE_DDA_STS_CDREADY | ECE_DDA_STS_ACDRDY);
  214. regmap_write(ece, ECE_RECT_XY, rect_offset);
  215. w_tile = r_w / ECE_TILE_W;
  216. h_tile = r_h / ECE_TILE_H;
  217. if (r_w % ECE_TILE_W) {
  218. w_tile += 1;
  219. w_size = r_w % ECE_TILE_W;
  220. }
  221. if (r_h % ECE_TILE_H || !h_tile) {
  222. h_tile += 1;
  223. h_size = r_h % ECE_TILE_H;
  224. }
  225. temp = FIELD_PREP(ECE_RECT_DIMEN_WLTR, w_size - 1) |
  226. FIELD_PREP(ECE_RECT_DIMEN_HLTR, h_size - 1) |
  227. FIELD_PREP(ECE_RECT_DIMEN_WR, w_tile - 1) |
  228. FIELD_PREP(ECE_RECT_DIMEN_HR, h_tile - 1);
  229. regmap_write(ece, ECE_RECT_DIMEN, temp);
  230. }
  231. static unsigned int npcm_video_ece_read_rect_offset(struct npcm_video *video)
  232. {
  233. struct regmap *ece = video->ece.regmap;
  234. unsigned int offset;
  235. regmap_read(ece, ECE_HEX_RECT_OFFSET, &offset);
  236. return FIELD_GET(ECE_HEX_RECT_OFFSET_MASK, offset);
  237. }
  238. /*
  239. * Set the line pitch (in bytes) for the frame buffers.
  240. * Can be on of those values: 512, 1024, 2048, 2560 or 4096 bytes.
  241. */
  242. static void npcm_video_ece_set_lp(struct npcm_video *video, unsigned int pitch)
  243. {
  244. struct regmap *ece = video->ece.regmap;
  245. unsigned int lp;
  246. switch (pitch) {
  247. case 512:
  248. lp = ECE_RESOL_FB_LP_512;
  249. break;
  250. case 1024:
  251. lp = ECE_RESOL_FB_LP_1024;
  252. break;
  253. case 2048:
  254. lp = ECE_RESOL_FB_LP_2048;
  255. break;
  256. case 2560:
  257. lp = ECE_RESOL_FB_LP_2560;
  258. break;
  259. case 4096:
  260. lp = ECE_RESOL_FB_LP_4096;
  261. break;
  262. default:
  263. return;
  264. }
  265. regmap_write(ece, ECE_RESOL, lp);
  266. }
  267. static inline void npcm_video_ece_set_fb_addr(struct npcm_video *video,
  268. unsigned int buffer)
  269. {
  270. struct regmap *ece = video->ece.regmap;
  271. regmap_write(ece, ECE_FBR_BA, buffer);
  272. }
  273. static inline void npcm_video_ece_set_enc_dba(struct npcm_video *video,
  274. unsigned int addr)
  275. {
  276. struct regmap *ece = video->ece.regmap;
  277. regmap_write(ece, ECE_ED_BA, addr);
  278. }
  279. static inline void npcm_video_ece_clear_rect_offset(struct npcm_video *video)
  280. {
  281. struct regmap *ece = video->ece.regmap;
  282. regmap_write(ece, ECE_HEX_RECT_OFFSET, 0);
  283. }
  284. static void npcm_video_ece_ctrl_reset(struct npcm_video *video)
  285. {
  286. struct regmap *ece = video->ece.regmap;
  287. regmap_update_bits(ece, ECE_DDA_CTRL, ECE_DDA_CTRL_ECEEN, 0);
  288. regmap_update_bits(ece, ECE_HEX_CTRL, ECE_HEX_CTRL_ENCDIS, ECE_HEX_CTRL_ENCDIS);
  289. regmap_update_bits(ece, ECE_DDA_CTRL, ECE_DDA_CTRL_ECEEN, ECE_DDA_CTRL_ECEEN);
  290. regmap_update_bits(ece, ECE_HEX_CTRL, ECE_HEX_CTRL_ENCDIS, 0);
  291. npcm_video_ece_clear_rect_offset(video);
  292. }
  293. static void npcm_video_ece_ip_reset(struct npcm_video *video)
  294. {
  295. /*
  296. * After resetting a module and clearing the reset bit, it should wait
  297. * at least 10 us before accessing the module.
  298. */
  299. reset_control_assert(video->ece.reset);
  300. usleep_range(10, 20);
  301. reset_control_deassert(video->ece.reset);
  302. usleep_range(10, 20);
  303. }
  304. static void npcm_video_ece_stop(struct npcm_video *video)
  305. {
  306. struct regmap *ece = video->ece.regmap;
  307. regmap_update_bits(ece, ECE_DDA_CTRL, ECE_DDA_CTRL_ECEEN, 0);
  308. regmap_update_bits(ece, ECE_DDA_CTRL, ECE_DDA_CTRL_INTEN, 0);
  309. regmap_update_bits(ece, ECE_HEX_CTRL, ECE_HEX_CTRL_ENCDIS, ECE_HEX_CTRL_ENCDIS);
  310. npcm_video_ece_clear_rect_offset(video);
  311. }
  312. static bool npcm_video_alloc_fb(struct npcm_video *video,
  313. struct npcm_video_addr *addr)
  314. {
  315. addr->virt = dma_alloc_coherent(video->dev, VCD_FB_SIZE, &addr->dma,
  316. GFP_KERNEL);
  317. if (!addr->virt)
  318. return false;
  319. addr->size = VCD_FB_SIZE;
  320. return true;
  321. }
  322. static void npcm_video_free_fb(struct npcm_video *video,
  323. struct npcm_video_addr *addr)
  324. {
  325. dma_free_coherent(video->dev, addr->size, addr->virt, addr->dma);
  326. addr->size = 0;
  327. addr->dma = 0ULL;
  328. addr->virt = NULL;
  329. }
  330. static void npcm_video_free_diff_table(struct npcm_video *video)
  331. {
  332. struct list_head *head, *pos, *nx;
  333. struct rect_list *tmp;
  334. unsigned int i;
  335. for (i = 0; i < vb2_get_num_buffers(&video->queue); i++) {
  336. head = &video->list[i];
  337. list_for_each_safe(pos, nx, head) {
  338. tmp = list_entry(pos, struct rect_list, list);
  339. list_del(&tmp->list);
  340. kfree(tmp);
  341. }
  342. }
  343. }
  344. static unsigned int npcm_video_add_rect(struct npcm_video *video,
  345. unsigned int index,
  346. unsigned int x, unsigned int y,
  347. unsigned int w, unsigned int h)
  348. {
  349. struct list_head *head = &video->list[index];
  350. struct rect_list *list = NULL;
  351. struct v4l2_rect *r;
  352. list = kzalloc(sizeof(*list), GFP_KERNEL);
  353. if (!list)
  354. return 0;
  355. r = &list->clip.c;
  356. r->left = x;
  357. r->top = y;
  358. r->width = w;
  359. r->height = h;
  360. list_add_tail(&list->list, head);
  361. return 1;
  362. }
  363. static void npcm_video_merge_rect(struct npcm_video *video,
  364. struct rect_list_info *info)
  365. {
  366. struct list_head *head = info->head;
  367. struct rect_list *list = info->list, *first = info->first;
  368. struct v4l2_rect *r = &list->clip.c, *f = &first->clip.c;
  369. if (!first) {
  370. first = list;
  371. info->first = first;
  372. list_add_tail(&list->list, head);
  373. video->rect_cnt++;
  374. } else {
  375. if ((r->left == (f->left + f->width)) && r->top == f->top) {
  376. f->width += r->width;
  377. kfree(list);
  378. } else if ((r->top == (f->top + f->height)) &&
  379. (r->left == f->left)) {
  380. f->height += r->height;
  381. kfree(list);
  382. } else if (((r->top > f->top) &&
  383. (r->top < (f->top + f->height))) &&
  384. ((r->left > f->left) &&
  385. (r->left < (f->left + f->width)))) {
  386. kfree(list);
  387. } else {
  388. list_add_tail(&list->list, head);
  389. video->rect_cnt++;
  390. info->first = list;
  391. }
  392. }
  393. }
  394. static struct rect_list *npcm_video_new_rect(struct npcm_video *video,
  395. unsigned int offset,
  396. unsigned int index)
  397. {
  398. struct v4l2_bt_timings *act = &video->active_timings;
  399. struct rect_list *list = NULL;
  400. struct v4l2_rect *r;
  401. list = kzalloc(sizeof(*list), GFP_KERNEL);
  402. if (!list)
  403. return NULL;
  404. r = &list->clip.c;
  405. r->left = (offset << 4);
  406. r->top = (index >> 2);
  407. r->width = RECT_W;
  408. r->height = RECT_H;
  409. if ((r->left + RECT_W) > act->width)
  410. r->width = act->width - r->left;
  411. if ((r->top + RECT_H) > act->height)
  412. r->height = act->height - r->top;
  413. return list;
  414. }
  415. static int npcm_video_find_rect(struct npcm_video *video,
  416. struct rect_list_info *info,
  417. unsigned int offset)
  418. {
  419. if (offset < info->tile_perline) {
  420. info->list = npcm_video_new_rect(video, offset, info->index);
  421. if (!info->list) {
  422. dev_err(video->dev, "Failed to allocate rect_list\n");
  423. return -ENOMEM;
  424. }
  425. npcm_video_merge_rect(video, info);
  426. }
  427. return 0;
  428. }
  429. static int npcm_video_build_table(struct npcm_video *video,
  430. struct rect_list_info *info)
  431. {
  432. struct regmap *vcd = video->vcd_regmap;
  433. unsigned int j, bit, value;
  434. int ret;
  435. for (j = 0; j < info->offset_perline; j += 4) {
  436. regmap_read(vcd, VCD_DIFF_TBL + (j + info->index), &value);
  437. bitmap_from_arr32(bitmap, &value, BITMAP_SIZE);
  438. for_each_set_bit(bit, bitmap, BITMAP_SIZE) {
  439. ret = npcm_video_find_rect(video, info, bit + (j << 3));
  440. if (ret)
  441. return ret;
  442. }
  443. }
  444. info->index += 64;
  445. return info->tile_perline;
  446. }
  447. static void npcm_video_get_rect_list(struct npcm_video *video, unsigned int index)
  448. {
  449. struct v4l2_bt_timings *act = &video->active_timings;
  450. struct rect_list_info info;
  451. unsigned int tile_cnt = 0, mod;
  452. int ret = 0;
  453. memset(&info, 0, sizeof(struct rect_list_info));
  454. info.head = &video->list[index];
  455. info.tile_perline = act->width >> 4;
  456. mod = act->width % RECT_W;
  457. if (mod != 0)
  458. info.tile_perline += 1;
  459. info.tile_perrow = act->height >> 4;
  460. mod = act->height % RECT_H;
  461. if (mod != 0)
  462. info.tile_perrow += 1;
  463. info.tile_size = info.tile_perrow * info.tile_perline;
  464. info.offset_perline = info.tile_perline >> 5;
  465. mod = info.tile_perline % 32;
  466. if (mod != 0)
  467. info.offset_perline += 1;
  468. info.offset_perline *= 4;
  469. do {
  470. ret = npcm_video_build_table(video, &info);
  471. if (ret < 0)
  472. return;
  473. tile_cnt += ret;
  474. } while (tile_cnt < info.tile_size);
  475. }
  476. static unsigned int npcm_video_is_mga(struct npcm_video *video)
  477. {
  478. struct regmap *gfxi = video->gfx_regmap;
  479. unsigned int dispst;
  480. regmap_read(gfxi, DISPST, &dispst);
  481. return ((dispst & DISPST_MGAMODE) == DISPST_MGAMODE);
  482. }
  483. static unsigned int npcm_video_hres(struct npcm_video *video)
  484. {
  485. struct regmap *gfxi = video->gfx_regmap;
  486. unsigned int hvcnth, hvcntl, apb_hor_res;
  487. regmap_read(gfxi, HVCNTH, &hvcnth);
  488. regmap_read(gfxi, HVCNTL, &hvcntl);
  489. apb_hor_res = (((hvcnth & HVCNTH_MASK) << 8) + (hvcntl & HVCNTL_MASK) + 1);
  490. return apb_hor_res;
  491. }
  492. static unsigned int npcm_video_vres(struct npcm_video *video)
  493. {
  494. struct regmap *gfxi = video->gfx_regmap;
  495. unsigned int vvcnth, vvcntl, apb_ver_res;
  496. regmap_read(gfxi, VVCNTH, &vvcnth);
  497. regmap_read(gfxi, VVCNTL, &vvcntl);
  498. apb_ver_res = (((vvcnth & VVCNTH_MASK) << 8) + (vvcntl & VVCNTL_MASK));
  499. return apb_ver_res;
  500. }
  501. static int npcm_video_capres(struct npcm_video *video, unsigned int hor_res,
  502. unsigned int vert_res)
  503. {
  504. struct regmap *vcd = video->vcd_regmap;
  505. unsigned int res, cap_res;
  506. if (hor_res > MAX_WIDTH || vert_res > MAX_HEIGHT)
  507. return -EINVAL;
  508. res = FIELD_PREP(VCD_CAP_RES_VERT_RES, vert_res) |
  509. FIELD_PREP(VCD_CAP_RES_HOR_RES, hor_res);
  510. regmap_write(vcd, VCD_CAP_RES, res);
  511. regmap_read(vcd, VCD_CAP_RES, &cap_res);
  512. if (cap_res != res)
  513. return -EINVAL;
  514. return 0;
  515. }
  516. static void npcm_video_vcd_ip_reset(struct npcm_video *video)
  517. {
  518. /*
  519. * After resetting a module and clearing the reset bit, it should wait
  520. * at least 10 us before accessing the module.
  521. */
  522. reset_control_assert(video->reset);
  523. usleep_range(10, 20);
  524. reset_control_deassert(video->reset);
  525. usleep_range(10, 20);
  526. }
  527. static void npcm_video_vcd_state_machine_reset(struct npcm_video *video)
  528. {
  529. struct regmap *vcd = video->vcd_regmap;
  530. regmap_update_bits(vcd, VCD_MODE, VCD_MODE_VCDE, 0);
  531. regmap_update_bits(vcd, VCD_MODE, VCD_MODE_IDBC, 0);
  532. regmap_update_bits(vcd, VCD_CMD, VCD_CMD_RST, VCD_CMD_RST);
  533. /*
  534. * VCD_CMD_RST will reset VCD internal state machines and clear FIFOs,
  535. * it should wait at least 800 us for the reset operations completed.
  536. */
  537. usleep_range(800, 1000);
  538. regmap_write(vcd, VCD_STAT, VCD_STAT_CLEAR);
  539. regmap_update_bits(vcd, VCD_MODE, VCD_MODE_VCDE, VCD_MODE_VCDE);
  540. regmap_update_bits(vcd, VCD_MODE, VCD_MODE_IDBC, VCD_MODE_IDBC);
  541. }
  542. static void npcm_video_gfx_reset(struct npcm_video *video)
  543. {
  544. struct regmap *gcr = video->gcr_regmap;
  545. regmap_update_bits(gcr, INTCR2, INTCR2_GIRST2, INTCR2_GIRST2);
  546. npcm_video_vcd_state_machine_reset(video);
  547. regmap_update_bits(gcr, INTCR2, INTCR2_GIRST2, 0);
  548. }
  549. static void npcm_video_kvm_bw(struct npcm_video *video, bool set_bw)
  550. {
  551. struct regmap *vcd = video->vcd_regmap;
  552. if (set_bw || !npcm_video_is_mga(video))
  553. regmap_update_bits(vcd, VCD_MODE, VCD_MODE_KVM_BW_SET,
  554. VCD_MODE_KVM_BW_SET);
  555. else
  556. regmap_update_bits(vcd, VCD_MODE, VCD_MODE_KVM_BW_SET, 0);
  557. }
  558. static unsigned int npcm_video_pclk(struct npcm_video *video)
  559. {
  560. struct regmap *gfxi = video->gfx_regmap;
  561. unsigned int tmp, pllfbdiv, pllinotdiv, gpllfbdiv;
  562. unsigned int gpllfbdv109, gpllfbdv8, gpllindiv;
  563. unsigned int gpllst_pllotdiv1, gpllst_pllotdiv2;
  564. regmap_read(gfxi, GPLLST, &tmp);
  565. gpllfbdv109 = FIELD_GET(GPLLST_GPLLFBDV109, tmp);
  566. gpllst_pllotdiv1 = FIELD_GET(GPLLST_PLLOTDIV1, tmp);
  567. gpllst_pllotdiv2 = FIELD_GET(GPLLST_PLLOTDIV2, tmp);
  568. regmap_read(gfxi, GPLLINDIV, &tmp);
  569. gpllfbdv8 = FIELD_GET(GPLLINDIV_GPLLFBDV8, tmp);
  570. gpllindiv = FIELD_GET(GPLLINDIV_MASK, tmp);
  571. regmap_read(gfxi, GPLLFBDIV, &tmp);
  572. gpllfbdiv = FIELD_GET(GPLLFBDIV_MASK, tmp);
  573. pllfbdiv = (512 * gpllfbdv109 + 256 * gpllfbdv8 + gpllfbdiv);
  574. pllinotdiv = (gpllindiv * gpllst_pllotdiv1 * gpllst_pllotdiv2);
  575. if (pllfbdiv == 0 || pllinotdiv == 0)
  576. return 0;
  577. return ((pllfbdiv * 25000) / pllinotdiv) * 1000;
  578. }
  579. static unsigned int npcm_video_get_bpp(struct npcm_video *video)
  580. {
  581. const struct npcm_fmt *fmt;
  582. unsigned int k;
  583. for (k = 0; k < NUM_FORMATS; k++) {
  584. fmt = &npcm_fmt_list[k];
  585. if (fmt->fourcc == video->pix_fmt.pixelformat)
  586. break;
  587. }
  588. return fmt->bpp;
  589. }
  590. /*
  591. * Pitch must be a power of 2, >= linebytes,
  592. * at least 512, and no more than 4096.
  593. */
  594. static void npcm_video_set_linepitch(struct npcm_video *video,
  595. unsigned int linebytes)
  596. {
  597. struct regmap *vcd = video->vcd_regmap;
  598. unsigned int pitch = MIN_LP;
  599. while ((pitch < linebytes) && (pitch < MAX_LP))
  600. pitch *= 2;
  601. regmap_write(vcd, VCD_FB_LP, FIELD_PREP(VCD_FBA_LP, pitch) |
  602. FIELD_PREP(VCD_FBB_LP, pitch));
  603. }
  604. static unsigned int npcm_video_get_linepitch(struct npcm_video *video)
  605. {
  606. struct regmap *vcd = video->vcd_regmap;
  607. unsigned int linepitch;
  608. regmap_read(vcd, VCD_FB_LP, &linepitch);
  609. return FIELD_GET(VCD_FBA_LP, linepitch);
  610. }
  611. static void npcm_video_command(struct npcm_video *video, unsigned int value)
  612. {
  613. struct regmap *vcd = video->vcd_regmap;
  614. unsigned int cmd;
  615. regmap_write(vcd, VCD_STAT, VCD_STAT_CLEAR);
  616. regmap_read(vcd, VCD_CMD, &cmd);
  617. cmd |= FIELD_PREP(VCD_CMD_OPERATION, value);
  618. regmap_write(vcd, VCD_CMD, cmd);
  619. regmap_update_bits(vcd, VCD_CMD, VCD_CMD_GO, VCD_CMD_GO);
  620. video->op_cmd = value;
  621. }
  622. static void npcm_video_init_reg(struct npcm_video *video)
  623. {
  624. struct regmap *gcr = video->gcr_regmap, *vcd = video->vcd_regmap;
  625. /* Selects Data Enable */
  626. regmap_update_bits(gcr, INTCR, INTCR_DEHS, 0);
  627. /* Enable display of KVM GFX and access to memory */
  628. regmap_update_bits(gcr, INTCR, INTCR_GFXIFDIS, 0);
  629. /* Active Vertical/Horizontal Counters Reset */
  630. regmap_update_bits(gcr, INTCR2, INTCR2_GIHCRST | INTCR2_GIVCRST,
  631. INTCR2_GIHCRST | INTCR2_GIVCRST);
  632. /* Reset video modules */
  633. npcm_video_vcd_ip_reset(video);
  634. npcm_video_gfx_reset(video);
  635. /* Set the FIFO thresholds */
  636. regmap_write(vcd, VCD_FIFO, VCD_FIFO_TH);
  637. /* Set RCHG timer */
  638. regmap_write(vcd, VCD_RCHG, FIELD_PREP(VCD_RCHG_TIM_PRSCL, 0xf) |
  639. FIELD_PREP(VCD_RCHG_IG_CHG0, 0x3));
  640. /* Set video mode */
  641. regmap_write(vcd, VCD_MODE, VCD_MODE_VCDE | VCD_MODE_CM565 |
  642. VCD_MODE_IDBC | VCD_MODE_KVM_BW_SET);
  643. }
  644. static int npcm_video_start_frame(struct npcm_video *video)
  645. {
  646. struct npcm_video_buffer *buf;
  647. struct regmap *vcd = video->vcd_regmap;
  648. unsigned int val;
  649. int ret;
  650. if (video->v4l2_input_status) {
  651. dev_dbg(video->dev, "No video signal; skip capture frame\n");
  652. return 0;
  653. }
  654. ret = regmap_read_poll_timeout(vcd, VCD_STAT, val, !(val & VCD_STAT_BUSY),
  655. 1000, VCD_TIMEOUT_US);
  656. if (ret) {
  657. dev_err(video->dev, "Wait for VCD_STAT_BUSY timeout\n");
  658. return -EBUSY;
  659. }
  660. mutex_lock(&video->buffer_lock);
  661. buf = list_first_entry_or_null(&video->buffers,
  662. struct npcm_video_buffer, link);
  663. if (!buf) {
  664. mutex_unlock(&video->buffer_lock);
  665. dev_dbg(video->dev, "No empty buffers; skip capture frame\n");
  666. return 0;
  667. }
  668. set_bit(VIDEO_CAPTURING, &video->flags);
  669. mutex_unlock(&video->buffer_lock);
  670. npcm_video_vcd_state_machine_reset(video);
  671. regmap_read(vcd, VCD_HOR_AC_TIM, &val);
  672. regmap_update_bits(vcd, VCD_HOR_AC_LST, VCD_HOR_AC_LAST,
  673. FIELD_GET(VCD_HOR_AC_TIME, val));
  674. regmap_read(vcd, VCD_VER_HI_TIM, &val);
  675. regmap_update_bits(vcd, VCD_VER_HI_LST, VCD_VER_HI_LAST,
  676. FIELD_GET(VCD_VER_HI_TIME, val));
  677. regmap_update_bits(vcd, VCD_INTE, VCD_INTE_DONE_IE | VCD_INTE_IFOT_IE |
  678. VCD_INTE_IFOR_IE | VCD_INTE_HAC_IE | VCD_INTE_VHT_IE,
  679. VCD_INTE_DONE_IE | VCD_INTE_IFOT_IE | VCD_INTE_IFOR_IE |
  680. VCD_INTE_HAC_IE | VCD_INTE_VHT_IE);
  681. npcm_video_command(video, video->ctrl_cmd);
  682. return 0;
  683. }
  684. static void npcm_video_bufs_done(struct npcm_video *video,
  685. enum vb2_buffer_state state)
  686. {
  687. struct npcm_video_buffer *buf;
  688. mutex_lock(&video->buffer_lock);
  689. list_for_each_entry(buf, &video->buffers, link)
  690. vb2_buffer_done(&buf->vb.vb2_buf, state);
  691. INIT_LIST_HEAD(&video->buffers);
  692. mutex_unlock(&video->buffer_lock);
  693. }
  694. static void npcm_video_get_diff_rect(struct npcm_video *video, unsigned int index)
  695. {
  696. unsigned int width = video->active_timings.width;
  697. unsigned int height = video->active_timings.height;
  698. if (video->op_cmd != VCD_CMD_OPERATION_CAPTURE) {
  699. video->rect_cnt = 0;
  700. npcm_video_get_rect_list(video, index);
  701. video->rect[index] = video->rect_cnt;
  702. } else {
  703. video->rect[index] = npcm_video_add_rect(video, index, 0, 0,
  704. width, height);
  705. }
  706. }
  707. static void npcm_video_detect_resolution(struct npcm_video *video)
  708. {
  709. struct v4l2_bt_timings *act = &video->active_timings;
  710. struct v4l2_bt_timings *det = &video->detected_timings;
  711. struct regmap *gfxi = video->gfx_regmap;
  712. unsigned int dispst;
  713. video->v4l2_input_status = V4L2_IN_ST_NO_SIGNAL;
  714. det->width = npcm_video_hres(video);
  715. det->height = npcm_video_vres(video);
  716. if (act->width != det->width || act->height != det->height) {
  717. dev_dbg(video->dev, "Resolution changed\n");
  718. if (npcm_video_hres(video) > 0 && npcm_video_vres(video) > 0) {
  719. if (test_bit(VIDEO_STREAMING, &video->flags)) {
  720. /*
  721. * Wait for resolution is available,
  722. * and it is also captured by host.
  723. */
  724. do {
  725. mdelay(100);
  726. regmap_read(gfxi, DISPST, &dispst);
  727. } while (npcm_video_vres(video) < 100 ||
  728. npcm_video_pclk(video) == 0 ||
  729. (dispst & DISPST_HSCROFF));
  730. }
  731. det->width = npcm_video_hres(video);
  732. det->height = npcm_video_vres(video);
  733. det->pixelclock = npcm_video_pclk(video);
  734. }
  735. clear_bit(VIDEO_RES_CHANGING, &video->flags);
  736. }
  737. if (det->width && det->height)
  738. video->v4l2_input_status = 0;
  739. dev_dbg(video->dev, "Got resolution[%dx%d] -> [%dx%d], status %d\n",
  740. act->width, act->height, det->width, det->height,
  741. video->v4l2_input_status);
  742. }
  743. static int npcm_video_set_resolution(struct npcm_video *video,
  744. struct v4l2_bt_timings *timing)
  745. {
  746. struct regmap *vcd = video->vcd_regmap;
  747. unsigned int mode;
  748. if (npcm_video_capres(video, timing->width, timing->height)) {
  749. dev_err(video->dev, "Failed to set VCD_CAP_RES\n");
  750. return -EINVAL;
  751. }
  752. video->active_timings = *timing;
  753. video->bytesperpixel = npcm_video_get_bpp(video);
  754. npcm_video_set_linepitch(video, timing->width * video->bytesperpixel);
  755. video->bytesperline = npcm_video_get_linepitch(video);
  756. video->pix_fmt.width = timing->width ? timing->width : MIN_WIDTH;
  757. video->pix_fmt.height = timing->height ? timing->height : MIN_HEIGHT;
  758. video->pix_fmt.sizeimage = video->pix_fmt.width * video->pix_fmt.height *
  759. video->bytesperpixel;
  760. video->pix_fmt.bytesperline = video->bytesperline;
  761. npcm_video_kvm_bw(video, timing->pixelclock > VCD_KVM_BW_PCLK);
  762. npcm_video_gfx_reset(video);
  763. regmap_read(vcd, VCD_MODE, &mode);
  764. dev_dbg(video->dev, "VCD mode = 0x%x, %s mode\n", mode,
  765. npcm_video_is_mga(video) ? "Hi Res" : "VGA");
  766. dev_dbg(video->dev,
  767. "Digital mode: %d x %d x %d, pixelclock %lld, bytesperline %d\n",
  768. timing->width, timing->height, video->bytesperpixel,
  769. timing->pixelclock, video->bytesperline);
  770. return 0;
  771. }
  772. static void npcm_video_start(struct npcm_video *video)
  773. {
  774. npcm_video_init_reg(video);
  775. if (!npcm_video_alloc_fb(video, &video->src)) {
  776. dev_err(video->dev, "Failed to allocate VCD frame buffer\n");
  777. return;
  778. }
  779. npcm_video_detect_resolution(video);
  780. if (npcm_video_set_resolution(video, &video->detected_timings)) {
  781. dev_err(video->dev, "Failed to set resolution\n");
  782. return;
  783. }
  784. /* Set frame buffer physical address */
  785. regmap_write(video->vcd_regmap, VCD_FBA_ADR, video->src.dma);
  786. regmap_write(video->vcd_regmap, VCD_FBB_ADR, video->src.dma);
  787. if (video->ece.enable && atomic_inc_return(&video->ece.clients) == 1) {
  788. npcm_video_ece_ip_reset(video);
  789. npcm_video_ece_ctrl_reset(video);
  790. npcm_video_ece_set_fb_addr(video, video->src.dma);
  791. npcm_video_ece_set_lp(video, video->bytesperline);
  792. dev_dbg(video->dev, "ECE open: client %d\n",
  793. atomic_read(&video->ece.clients));
  794. }
  795. }
  796. static void npcm_video_stop(struct npcm_video *video)
  797. {
  798. struct regmap *vcd = video->vcd_regmap;
  799. set_bit(VIDEO_STOPPED, &video->flags);
  800. regmap_write(vcd, VCD_INTE, 0);
  801. regmap_write(vcd, VCD_MODE, 0);
  802. regmap_write(vcd, VCD_RCHG, 0);
  803. regmap_write(vcd, VCD_STAT, VCD_STAT_CLEAR);
  804. if (video->src.size)
  805. npcm_video_free_fb(video, &video->src);
  806. npcm_video_free_diff_table(video);
  807. video->v4l2_input_status = V4L2_IN_ST_NO_SIGNAL;
  808. video->flags = 0;
  809. video->ctrl_cmd = VCD_CMD_OPERATION_CAPTURE;
  810. if (video->ece.enable && atomic_dec_return(&video->ece.clients) == 0) {
  811. npcm_video_ece_stop(video);
  812. dev_dbg(video->dev, "ECE close: client %d\n",
  813. atomic_read(&video->ece.clients));
  814. }
  815. }
  816. static unsigned int npcm_video_raw(struct npcm_video *video, int index, void *addr)
  817. {
  818. unsigned int width = video->active_timings.width;
  819. unsigned int height = video->active_timings.height;
  820. unsigned int i, len, offset, bytes = 0;
  821. video->rect[index] = npcm_video_add_rect(video, index, 0, 0, width, height);
  822. for (i = 0; i < height; i++) {
  823. len = width * video->bytesperpixel;
  824. offset = i * video->bytesperline;
  825. memcpy(addr + bytes, video->src.virt + offset, len);
  826. bytes += len;
  827. }
  828. return bytes;
  829. }
  830. static unsigned int npcm_video_hextile(struct npcm_video *video, unsigned int index,
  831. unsigned int dma_addr, void *vaddr)
  832. {
  833. struct rect_list *rect_list;
  834. struct v4l2_rect *rect;
  835. unsigned int offset, len, bytes = 0;
  836. npcm_video_ece_ctrl_reset(video);
  837. npcm_video_ece_clear_rect_offset(video);
  838. npcm_video_ece_set_fb_addr(video, video->src.dma);
  839. /* Set base address of encoded data to video buffer */
  840. npcm_video_ece_set_enc_dba(video, dma_addr);
  841. npcm_video_ece_set_lp(video, video->bytesperline);
  842. npcm_video_get_diff_rect(video, index);
  843. list_for_each_entry(rect_list, &video->list[index], list) {
  844. rect = &rect_list->clip.c;
  845. offset = npcm_video_ece_read_rect_offset(video);
  846. npcm_video_ece_enc_rect(video, rect->left, rect->top,
  847. rect->width, rect->height);
  848. len = npcm_video_ece_get_ed_size(video, offset, vaddr);
  849. npcm_video_ece_prepend_rect_header(vaddr + offset,
  850. rect->left, rect->top,
  851. rect->width, rect->height);
  852. bytes += len;
  853. }
  854. return bytes;
  855. }
  856. static irqreturn_t npcm_video_irq(int irq, void *arg)
  857. {
  858. struct npcm_video *video = arg;
  859. struct regmap *vcd = video->vcd_regmap;
  860. struct npcm_video_buffer *buf;
  861. unsigned int index, size, status, fmt;
  862. dma_addr_t dma_addr;
  863. void *addr;
  864. static const struct v4l2_event ev = {
  865. .type = V4L2_EVENT_SOURCE_CHANGE,
  866. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  867. };
  868. regmap_read(vcd, VCD_STAT, &status);
  869. dev_dbg(video->dev, "VCD irq status 0x%x\n", status);
  870. regmap_write(vcd, VCD_STAT, VCD_STAT_CLEAR);
  871. if (test_bit(VIDEO_STOPPED, &video->flags) ||
  872. !test_bit(VIDEO_STREAMING, &video->flags))
  873. return IRQ_NONE;
  874. if (status & VCD_STAT_DONE) {
  875. regmap_write(vcd, VCD_INTE, 0);
  876. mutex_lock(&video->buffer_lock);
  877. clear_bit(VIDEO_CAPTURING, &video->flags);
  878. buf = list_first_entry_or_null(&video->buffers,
  879. struct npcm_video_buffer, link);
  880. if (!buf) {
  881. mutex_unlock(&video->buffer_lock);
  882. return IRQ_NONE;
  883. }
  884. addr = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  885. index = buf->vb.vb2_buf.index;
  886. fmt = video->pix_fmt.pixelformat;
  887. switch (fmt) {
  888. case V4L2_PIX_FMT_RGB565:
  889. size = npcm_video_raw(video, index, addr);
  890. break;
  891. case V4L2_PIX_FMT_HEXTILE:
  892. dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
  893. size = npcm_video_hextile(video, index, dma_addr, addr);
  894. break;
  895. default:
  896. mutex_unlock(&video->buffer_lock);
  897. return IRQ_NONE;
  898. }
  899. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
  900. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  901. buf->vb.sequence = video->sequence++;
  902. buf->vb.field = V4L2_FIELD_NONE;
  903. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  904. list_del(&buf->link);
  905. mutex_unlock(&video->buffer_lock);
  906. if (npcm_video_start_frame(video))
  907. dev_err(video->dev, "Failed to capture next frame\n");
  908. }
  909. /* Resolution changed */
  910. if (status & VCD_STAT_VHT_CHG || status & VCD_STAT_HAC_CHG) {
  911. if (!test_bit(VIDEO_RES_CHANGING, &video->flags)) {
  912. set_bit(VIDEO_RES_CHANGING, &video->flags);
  913. vb2_queue_error(&video->queue);
  914. v4l2_event_queue(&video->vdev, &ev);
  915. }
  916. }
  917. if (status & VCD_STAT_IFOR || status & VCD_STAT_IFOT) {
  918. dev_warn(video->dev, "VCD FIFO overrun or over thresholds\n");
  919. if (npcm_video_start_frame(video))
  920. dev_err(video->dev, "Failed to recover from FIFO overrun\n");
  921. }
  922. return IRQ_HANDLED;
  923. }
  924. static int npcm_video_querycap(struct file *file, void *fh,
  925. struct v4l2_capability *cap)
  926. {
  927. strscpy(cap->driver, DEVICE_NAME, sizeof(cap->driver));
  928. strscpy(cap->card, "NPCM Video Engine", sizeof(cap->card));
  929. return 0;
  930. }
  931. static int npcm_video_enum_format(struct file *file, void *fh,
  932. struct v4l2_fmtdesc *f)
  933. {
  934. struct npcm_video *video = video_drvdata(file);
  935. const struct npcm_fmt *fmt;
  936. if (f->index >= NUM_FORMATS)
  937. return -EINVAL;
  938. fmt = &npcm_fmt_list[f->index];
  939. if (fmt->fourcc == V4L2_PIX_FMT_HEXTILE && !video->ece.enable)
  940. return -EINVAL;
  941. f->pixelformat = fmt->fourcc;
  942. return 0;
  943. }
  944. static int npcm_video_try_format(struct file *file, void *fh,
  945. struct v4l2_format *f)
  946. {
  947. struct npcm_video *video = video_drvdata(file);
  948. const struct npcm_fmt *fmt;
  949. fmt = npcm_video_find_format(f);
  950. /* If format not found or HEXTILE not supported, use RGB565 as default */
  951. if (!fmt || (fmt->fourcc == V4L2_PIX_FMT_HEXTILE && !video->ece.enable))
  952. f->fmt.pix.pixelformat = npcm_fmt_list[0].fourcc;
  953. f->fmt.pix.field = V4L2_FIELD_NONE;
  954. f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  955. f->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
  956. f->fmt.pix.width = video->pix_fmt.width;
  957. f->fmt.pix.height = video->pix_fmt.height;
  958. f->fmt.pix.bytesperline = video->bytesperline;
  959. f->fmt.pix.sizeimage = video->pix_fmt.sizeimage;
  960. return 0;
  961. }
  962. static int npcm_video_get_format(struct file *file, void *fh,
  963. struct v4l2_format *f)
  964. {
  965. struct npcm_video *video = video_drvdata(file);
  966. f->fmt.pix = video->pix_fmt;
  967. return 0;
  968. }
  969. static int npcm_video_set_format(struct file *file, void *fh,
  970. struct v4l2_format *f)
  971. {
  972. struct npcm_video *video = video_drvdata(file);
  973. int ret;
  974. ret = npcm_video_try_format(file, fh, f);
  975. if (ret)
  976. return ret;
  977. if (vb2_is_busy(&video->queue)) {
  978. dev_err(video->dev, "%s device busy\n", __func__);
  979. return -EBUSY;
  980. }
  981. video->pix_fmt.pixelformat = f->fmt.pix.pixelformat;
  982. return 0;
  983. }
  984. static int npcm_video_enum_input(struct file *file, void *fh,
  985. struct v4l2_input *inp)
  986. {
  987. struct npcm_video *video = video_drvdata(file);
  988. if (inp->index)
  989. return -EINVAL;
  990. strscpy(inp->name, "Host VGA capture", sizeof(inp->name));
  991. inp->type = V4L2_INPUT_TYPE_CAMERA;
  992. inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
  993. inp->status = video->v4l2_input_status;
  994. return 0;
  995. }
  996. static int npcm_video_get_input(struct file *file, void *fh, unsigned int *i)
  997. {
  998. *i = 0;
  999. return 0;
  1000. }
  1001. static int npcm_video_set_input(struct file *file, void *fh, unsigned int i)
  1002. {
  1003. if (i)
  1004. return -EINVAL;
  1005. return 0;
  1006. }
  1007. static int npcm_video_set_dv_timings(struct file *file, void *fh,
  1008. struct v4l2_dv_timings *timings)
  1009. {
  1010. struct npcm_video *video = video_drvdata(file);
  1011. int rc;
  1012. if (timings->bt.width == video->active_timings.width &&
  1013. timings->bt.height == video->active_timings.height)
  1014. return 0;
  1015. if (vb2_is_busy(&video->queue)) {
  1016. dev_err(video->dev, "%s device busy\n", __func__);
  1017. return -EBUSY;
  1018. }
  1019. rc = npcm_video_set_resolution(video, &timings->bt);
  1020. if (rc)
  1021. return rc;
  1022. timings->type = V4L2_DV_BT_656_1120;
  1023. return 0;
  1024. }
  1025. static int npcm_video_get_dv_timings(struct file *file, void *fh,
  1026. struct v4l2_dv_timings *timings)
  1027. {
  1028. struct npcm_video *video = video_drvdata(file);
  1029. timings->type = V4L2_DV_BT_656_1120;
  1030. timings->bt = video->active_timings;
  1031. return 0;
  1032. }
  1033. static int npcm_video_query_dv_timings(struct file *file, void *fh,
  1034. struct v4l2_dv_timings *timings)
  1035. {
  1036. struct npcm_video *video = video_drvdata(file);
  1037. npcm_video_detect_resolution(video);
  1038. timings->type = V4L2_DV_BT_656_1120;
  1039. timings->bt = video->detected_timings;
  1040. return video->v4l2_input_status ? -ENOLINK : 0;
  1041. }
  1042. static int npcm_video_enum_dv_timings(struct file *file, void *fh,
  1043. struct v4l2_enum_dv_timings *timings)
  1044. {
  1045. return v4l2_enum_dv_timings_cap(timings, &npcm_video_timings_cap,
  1046. NULL, NULL);
  1047. }
  1048. static int npcm_video_dv_timings_cap(struct file *file, void *fh,
  1049. struct v4l2_dv_timings_cap *cap)
  1050. {
  1051. *cap = npcm_video_timings_cap;
  1052. return 0;
  1053. }
  1054. static int npcm_video_sub_event(struct v4l2_fh *fh,
  1055. const struct v4l2_event_subscription *sub)
  1056. {
  1057. switch (sub->type) {
  1058. case V4L2_EVENT_SOURCE_CHANGE:
  1059. return v4l2_src_change_event_subscribe(fh, sub);
  1060. }
  1061. return v4l2_ctrl_subscribe_event(fh, sub);
  1062. }
  1063. static const struct v4l2_ioctl_ops npcm_video_ioctls = {
  1064. .vidioc_querycap = npcm_video_querycap,
  1065. .vidioc_enum_fmt_vid_cap = npcm_video_enum_format,
  1066. .vidioc_g_fmt_vid_cap = npcm_video_get_format,
  1067. .vidioc_s_fmt_vid_cap = npcm_video_set_format,
  1068. .vidioc_try_fmt_vid_cap = npcm_video_try_format,
  1069. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1070. .vidioc_querybuf = vb2_ioctl_querybuf,
  1071. .vidioc_qbuf = vb2_ioctl_qbuf,
  1072. .vidioc_expbuf = vb2_ioctl_expbuf,
  1073. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1074. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1075. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1076. .vidioc_streamon = vb2_ioctl_streamon,
  1077. .vidioc_streamoff = vb2_ioctl_streamoff,
  1078. .vidioc_enum_input = npcm_video_enum_input,
  1079. .vidioc_g_input = npcm_video_get_input,
  1080. .vidioc_s_input = npcm_video_set_input,
  1081. .vidioc_s_dv_timings = npcm_video_set_dv_timings,
  1082. .vidioc_g_dv_timings = npcm_video_get_dv_timings,
  1083. .vidioc_query_dv_timings = npcm_video_query_dv_timings,
  1084. .vidioc_enum_dv_timings = npcm_video_enum_dv_timings,
  1085. .vidioc_dv_timings_cap = npcm_video_dv_timings_cap,
  1086. .vidioc_subscribe_event = npcm_video_sub_event,
  1087. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1088. };
  1089. static int npcm_video_set_ctrl(struct v4l2_ctrl *ctrl)
  1090. {
  1091. struct npcm_video *video = container_of(ctrl->handler, struct npcm_video,
  1092. ctrl_handler);
  1093. switch (ctrl->id) {
  1094. case V4L2_CID_NPCM_CAPTURE_MODE:
  1095. if (ctrl->val == V4L2_NPCM_CAPTURE_MODE_COMPLETE)
  1096. video->ctrl_cmd = VCD_CMD_OPERATION_CAPTURE;
  1097. else if (ctrl->val == V4L2_NPCM_CAPTURE_MODE_DIFF)
  1098. video->ctrl_cmd = VCD_CMD_OPERATION_COMPARE;
  1099. break;
  1100. default:
  1101. return -EINVAL;
  1102. }
  1103. return 0;
  1104. }
  1105. static const struct v4l2_ctrl_ops npcm_video_ctrl_ops = {
  1106. .s_ctrl = npcm_video_set_ctrl,
  1107. };
  1108. static const char * const npcm_ctrl_capture_mode_menu[] = {
  1109. "COMPLETE",
  1110. "DIFF",
  1111. NULL,
  1112. };
  1113. static const struct v4l2_ctrl_config npcm_ctrl_capture_mode = {
  1114. .ops = &npcm_video_ctrl_ops,
  1115. .id = V4L2_CID_NPCM_CAPTURE_MODE,
  1116. .name = "NPCM Video Capture Mode",
  1117. .type = V4L2_CTRL_TYPE_MENU,
  1118. .min = 0,
  1119. .max = V4L2_NPCM_CAPTURE_MODE_DIFF,
  1120. .def = 0,
  1121. .qmenu = npcm_ctrl_capture_mode_menu,
  1122. };
  1123. /*
  1124. * This control value is set when a buffer is dequeued by userspace, i.e. in
  1125. * npcm_video_buf_finish function.
  1126. */
  1127. static const struct v4l2_ctrl_config npcm_ctrl_rect_count = {
  1128. .id = V4L2_CID_NPCM_RECT_COUNT,
  1129. .name = "NPCM Hextile Rectangle Count",
  1130. .type = V4L2_CTRL_TYPE_INTEGER,
  1131. .min = 0,
  1132. .max = (MAX_WIDTH / RECT_W) * (MAX_HEIGHT / RECT_H),
  1133. .step = 1,
  1134. .def = 0,
  1135. };
  1136. static int npcm_video_open(struct file *file)
  1137. {
  1138. struct npcm_video *video = video_drvdata(file);
  1139. int rc;
  1140. mutex_lock(&video->video_lock);
  1141. rc = v4l2_fh_open(file);
  1142. if (rc) {
  1143. mutex_unlock(&video->video_lock);
  1144. return rc;
  1145. }
  1146. if (v4l2_fh_is_singular_file(file))
  1147. npcm_video_start(video);
  1148. mutex_unlock(&video->video_lock);
  1149. return 0;
  1150. }
  1151. static int npcm_video_release(struct file *file)
  1152. {
  1153. struct npcm_video *video = video_drvdata(file);
  1154. int rc;
  1155. mutex_lock(&video->video_lock);
  1156. if (v4l2_fh_is_singular_file(file))
  1157. npcm_video_stop(video);
  1158. rc = _vb2_fop_release(file, NULL);
  1159. mutex_unlock(&video->video_lock);
  1160. return rc;
  1161. }
  1162. static const struct v4l2_file_operations npcm_video_v4l2_fops = {
  1163. .owner = THIS_MODULE,
  1164. .read = vb2_fop_read,
  1165. .poll = vb2_fop_poll,
  1166. .unlocked_ioctl = video_ioctl2,
  1167. .mmap = vb2_fop_mmap,
  1168. .open = npcm_video_open,
  1169. .release = npcm_video_release,
  1170. };
  1171. static int npcm_video_queue_setup(struct vb2_queue *q, unsigned int *num_buffers,
  1172. unsigned int *num_planes, unsigned int sizes[],
  1173. struct device *alloc_devs[])
  1174. {
  1175. struct npcm_video *video = vb2_get_drv_priv(q);
  1176. unsigned int i;
  1177. if (*num_planes) {
  1178. if (sizes[0] < video->pix_fmt.sizeimage)
  1179. return -EINVAL;
  1180. return 0;
  1181. }
  1182. *num_planes = 1;
  1183. sizes[0] = video->pix_fmt.sizeimage;
  1184. for (i = 0; i < VIDEO_MAX_FRAME; i++)
  1185. INIT_LIST_HEAD(&video->list[i]);
  1186. return 0;
  1187. }
  1188. static int npcm_video_buf_prepare(struct vb2_buffer *vb)
  1189. {
  1190. struct npcm_video *video = vb2_get_drv_priv(vb->vb2_queue);
  1191. if (vb2_plane_size(vb, 0) < video->pix_fmt.sizeimage)
  1192. return -EINVAL;
  1193. return 0;
  1194. }
  1195. static int npcm_video_start_streaming(struct vb2_queue *q, unsigned int count)
  1196. {
  1197. struct npcm_video *video = vb2_get_drv_priv(q);
  1198. int rc;
  1199. video->sequence = 0;
  1200. rc = npcm_video_start_frame(video);
  1201. if (rc) {
  1202. npcm_video_bufs_done(video, VB2_BUF_STATE_QUEUED);
  1203. return rc;
  1204. }
  1205. set_bit(VIDEO_STREAMING, &video->flags);
  1206. return 0;
  1207. }
  1208. static void npcm_video_stop_streaming(struct vb2_queue *q)
  1209. {
  1210. struct npcm_video *video = vb2_get_drv_priv(q);
  1211. struct regmap *vcd = video->vcd_regmap;
  1212. clear_bit(VIDEO_STREAMING, &video->flags);
  1213. regmap_write(vcd, VCD_INTE, 0);
  1214. regmap_write(vcd, VCD_STAT, VCD_STAT_CLEAR);
  1215. npcm_video_gfx_reset(video);
  1216. npcm_video_bufs_done(video, VB2_BUF_STATE_ERROR);
  1217. video->ctrl_cmd = VCD_CMD_OPERATION_CAPTURE;
  1218. v4l2_ctrl_s_ctrl(video->rect_cnt_ctrl, 0);
  1219. }
  1220. static void npcm_video_buf_queue(struct vb2_buffer *vb)
  1221. {
  1222. struct npcm_video *video = vb2_get_drv_priv(vb->vb2_queue);
  1223. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1224. struct npcm_video_buffer *nvb = to_npcm_video_buffer(vbuf);
  1225. bool empty;
  1226. mutex_lock(&video->buffer_lock);
  1227. empty = list_empty(&video->buffers);
  1228. list_add_tail(&nvb->link, &video->buffers);
  1229. mutex_unlock(&video->buffer_lock);
  1230. if (test_bit(VIDEO_STREAMING, &video->flags) &&
  1231. !test_bit(VIDEO_CAPTURING, &video->flags) && empty) {
  1232. if (npcm_video_start_frame(video))
  1233. dev_err(video->dev, "Failed to capture next frame\n");
  1234. }
  1235. }
  1236. static void npcm_video_buf_finish(struct vb2_buffer *vb)
  1237. {
  1238. struct npcm_video *video = vb2_get_drv_priv(vb->vb2_queue);
  1239. struct list_head *head, *pos, *nx;
  1240. struct rect_list *tmp;
  1241. /*
  1242. * This callback is called when the buffer is dequeued, so update
  1243. * V4L2_CID_NPCM_RECT_COUNT control value with the number of rectangles
  1244. * in this buffer and free associated rect_list.
  1245. */
  1246. if (test_bit(VIDEO_STREAMING, &video->flags)) {
  1247. v4l2_ctrl_s_ctrl(video->rect_cnt_ctrl, video->rect[vb->index]);
  1248. head = &video->list[vb->index];
  1249. list_for_each_safe(pos, nx, head) {
  1250. tmp = list_entry(pos, struct rect_list, list);
  1251. list_del(&tmp->list);
  1252. kfree(tmp);
  1253. }
  1254. }
  1255. }
  1256. static const struct regmap_config npcm_video_regmap_cfg = {
  1257. .reg_bits = 32,
  1258. .reg_stride = 4,
  1259. .val_bits = 32,
  1260. .max_register = VCD_FIFO,
  1261. };
  1262. static const struct regmap_config npcm_video_ece_regmap_cfg = {
  1263. .reg_bits = 32,
  1264. .reg_stride = 4,
  1265. .val_bits = 32,
  1266. .max_register = ECE_HEX_RECT_OFFSET,
  1267. };
  1268. static const struct vb2_ops npcm_video_vb2_ops = {
  1269. .queue_setup = npcm_video_queue_setup,
  1270. .wait_prepare = vb2_ops_wait_prepare,
  1271. .wait_finish = vb2_ops_wait_finish,
  1272. .buf_prepare = npcm_video_buf_prepare,
  1273. .buf_finish = npcm_video_buf_finish,
  1274. .start_streaming = npcm_video_start_streaming,
  1275. .stop_streaming = npcm_video_stop_streaming,
  1276. .buf_queue = npcm_video_buf_queue,
  1277. };
  1278. static int npcm_video_setup_video(struct npcm_video *video)
  1279. {
  1280. struct v4l2_device *v4l2_dev = &video->v4l2_dev;
  1281. struct video_device *vdev = &video->vdev;
  1282. struct vb2_queue *vbq = &video->queue;
  1283. int rc;
  1284. if (video->ece.enable)
  1285. video->pix_fmt.pixelformat = V4L2_PIX_FMT_HEXTILE;
  1286. else
  1287. video->pix_fmt.pixelformat = V4L2_PIX_FMT_RGB565;
  1288. video->pix_fmt.field = V4L2_FIELD_NONE;
  1289. video->pix_fmt.colorspace = V4L2_COLORSPACE_SRGB;
  1290. video->pix_fmt.quantization = V4L2_QUANTIZATION_FULL_RANGE;
  1291. video->v4l2_input_status = V4L2_IN_ST_NO_SIGNAL;
  1292. rc = v4l2_device_register(video->dev, v4l2_dev);
  1293. if (rc) {
  1294. dev_err(video->dev, "Failed to register v4l2 device\n");
  1295. return rc;
  1296. }
  1297. v4l2_ctrl_handler_init(&video->ctrl_handler, 2);
  1298. v4l2_ctrl_new_custom(&video->ctrl_handler, &npcm_ctrl_capture_mode, NULL);
  1299. video->rect_cnt_ctrl = v4l2_ctrl_new_custom(&video->ctrl_handler,
  1300. &npcm_ctrl_rect_count, NULL);
  1301. if (video->ctrl_handler.error) {
  1302. dev_err(video->dev, "Failed to init controls: %d\n",
  1303. video->ctrl_handler.error);
  1304. rc = video->ctrl_handler.error;
  1305. goto rel_ctrl_handler;
  1306. }
  1307. v4l2_dev->ctrl_handler = &video->ctrl_handler;
  1308. vbq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1309. vbq->io_modes = VB2_MMAP | VB2_DMABUF;
  1310. vbq->dev = v4l2_dev->dev;
  1311. vbq->lock = &video->video_lock;
  1312. vbq->ops = &npcm_video_vb2_ops;
  1313. vbq->mem_ops = &vb2_dma_contig_memops;
  1314. vbq->drv_priv = video;
  1315. vbq->buf_struct_size = sizeof(struct npcm_video_buffer);
  1316. vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1317. vbq->min_queued_buffers = 3;
  1318. rc = vb2_queue_init(vbq);
  1319. if (rc) {
  1320. dev_err(video->dev, "Failed to init vb2 queue\n");
  1321. goto rel_ctrl_handler;
  1322. }
  1323. vdev->queue = vbq;
  1324. vdev->fops = &npcm_video_v4l2_fops;
  1325. vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1326. vdev->v4l2_dev = v4l2_dev;
  1327. strscpy(vdev->name, DEVICE_NAME, sizeof(vdev->name));
  1328. vdev->vfl_type = VFL_TYPE_VIDEO;
  1329. vdev->vfl_dir = VFL_DIR_RX;
  1330. vdev->release = video_device_release_empty;
  1331. vdev->ioctl_ops = &npcm_video_ioctls;
  1332. vdev->lock = &video->video_lock;
  1333. video_set_drvdata(vdev, video);
  1334. rc = video_register_device(vdev, VFL_TYPE_VIDEO, 0);
  1335. if (rc) {
  1336. dev_err(video->dev, "Failed to register video device\n");
  1337. goto rel_vb_queue;
  1338. }
  1339. return 0;
  1340. rel_vb_queue:
  1341. vb2_queue_release(vbq);
  1342. rel_ctrl_handler:
  1343. v4l2_ctrl_handler_free(&video->ctrl_handler);
  1344. v4l2_device_unregister(v4l2_dev);
  1345. return rc;
  1346. }
  1347. static int npcm_video_ece_init(struct npcm_video *video)
  1348. {
  1349. struct device *dev = video->dev;
  1350. struct device_node *ece_node;
  1351. struct platform_device *ece_pdev;
  1352. void __iomem *regs;
  1353. ece_node = of_parse_phandle(video->dev->of_node, "nuvoton,ece", 0);
  1354. if (!ece_node) {
  1355. dev_err(dev, "Failed to get ECE phandle in DTS\n");
  1356. return -ENODEV;
  1357. }
  1358. video->ece.enable = of_device_is_available(ece_node);
  1359. if (video->ece.enable) {
  1360. dev_info(dev, "Support HEXTILE pixel format\n");
  1361. ece_pdev = of_find_device_by_node(ece_node);
  1362. if (!ece_pdev) {
  1363. dev_err(dev, "Failed to find ECE device\n");
  1364. return -ENODEV;
  1365. }
  1366. of_node_put(ece_node);
  1367. regs = devm_platform_ioremap_resource(ece_pdev, 0);
  1368. if (IS_ERR(regs)) {
  1369. dev_err(dev, "Failed to parse ECE reg in DTS\n");
  1370. return PTR_ERR(regs);
  1371. }
  1372. video->ece.regmap = devm_regmap_init_mmio(dev, regs,
  1373. &npcm_video_ece_regmap_cfg);
  1374. if (IS_ERR(video->ece.regmap)) {
  1375. dev_err(dev, "Failed to initialize ECE regmap\n");
  1376. return PTR_ERR(video->ece.regmap);
  1377. }
  1378. video->ece.reset = devm_reset_control_get(&ece_pdev->dev, NULL);
  1379. if (IS_ERR(video->ece.reset)) {
  1380. dev_err(dev, "Failed to get ECE reset control in DTS\n");
  1381. return PTR_ERR(video->ece.reset);
  1382. }
  1383. }
  1384. return 0;
  1385. }
  1386. static int npcm_video_init(struct npcm_video *video)
  1387. {
  1388. struct device *dev = video->dev;
  1389. int irq, rc;
  1390. irq = irq_of_parse_and_map(dev->of_node, 0);
  1391. if (!irq) {
  1392. dev_err(dev, "Failed to find VCD IRQ\n");
  1393. return -ENODEV;
  1394. }
  1395. rc = devm_request_threaded_irq(dev, irq, NULL, npcm_video_irq,
  1396. IRQF_ONESHOT, DEVICE_NAME, video);
  1397. if (rc < 0) {
  1398. dev_err(dev, "Failed to request IRQ %d\n", irq);
  1399. return rc;
  1400. }
  1401. of_reserved_mem_device_init(dev);
  1402. rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  1403. if (rc) {
  1404. dev_err(dev, "Failed to set DMA mask\n");
  1405. of_reserved_mem_device_release(dev);
  1406. }
  1407. rc = npcm_video_ece_init(video);
  1408. if (rc) {
  1409. dev_err(dev, "Failed to initialize ECE\n");
  1410. return rc;
  1411. }
  1412. return 0;
  1413. }
  1414. static int npcm_video_probe(struct platform_device *pdev)
  1415. {
  1416. struct npcm_video *video = kzalloc(sizeof(*video), GFP_KERNEL);
  1417. int rc;
  1418. void __iomem *regs;
  1419. if (!video)
  1420. return -ENOMEM;
  1421. video->dev = &pdev->dev;
  1422. mutex_init(&video->video_lock);
  1423. mutex_init(&video->buffer_lock);
  1424. INIT_LIST_HEAD(&video->buffers);
  1425. regs = devm_platform_ioremap_resource(pdev, 0);
  1426. if (IS_ERR(regs)) {
  1427. dev_err(&pdev->dev, "Failed to parse VCD reg in DTS\n");
  1428. return PTR_ERR(regs);
  1429. }
  1430. video->vcd_regmap = devm_regmap_init_mmio(&pdev->dev, regs,
  1431. &npcm_video_regmap_cfg);
  1432. if (IS_ERR(video->vcd_regmap)) {
  1433. dev_err(&pdev->dev, "Failed to initialize VCD regmap\n");
  1434. return PTR_ERR(video->vcd_regmap);
  1435. }
  1436. video->reset = devm_reset_control_get(&pdev->dev, NULL);
  1437. if (IS_ERR(video->reset)) {
  1438. dev_err(&pdev->dev, "Failed to get VCD reset control in DTS\n");
  1439. return PTR_ERR(video->reset);
  1440. }
  1441. video->gcr_regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  1442. "nuvoton,sysgcr");
  1443. if (IS_ERR(video->gcr_regmap))
  1444. return PTR_ERR(video->gcr_regmap);
  1445. video->gfx_regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  1446. "nuvoton,sysgfxi");
  1447. if (IS_ERR(video->gfx_regmap))
  1448. return PTR_ERR(video->gfx_regmap);
  1449. rc = npcm_video_init(video);
  1450. if (rc)
  1451. return rc;
  1452. rc = npcm_video_setup_video(video);
  1453. if (rc)
  1454. return rc;
  1455. dev_info(video->dev, "NPCM video driver probed\n");
  1456. return 0;
  1457. }
  1458. static void npcm_video_remove(struct platform_device *pdev)
  1459. {
  1460. struct device *dev = &pdev->dev;
  1461. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  1462. struct npcm_video *video = to_npcm_video(v4l2_dev);
  1463. video_unregister_device(&video->vdev);
  1464. vb2_queue_release(&video->queue);
  1465. v4l2_ctrl_handler_free(&video->ctrl_handler);
  1466. v4l2_device_unregister(v4l2_dev);
  1467. if (video->ece.enable)
  1468. npcm_video_ece_stop(video);
  1469. of_reserved_mem_device_release(dev);
  1470. }
  1471. static const struct of_device_id npcm_video_match[] = {
  1472. { .compatible = "nuvoton,npcm750-vcd" },
  1473. { .compatible = "nuvoton,npcm845-vcd" },
  1474. {},
  1475. };
  1476. MODULE_DEVICE_TABLE(of, npcm_video_match);
  1477. static struct platform_driver npcm_video_driver = {
  1478. .driver = {
  1479. .name = DEVICE_NAME,
  1480. .of_match_table = npcm_video_match,
  1481. },
  1482. .probe = npcm_video_probe,
  1483. .remove_new = npcm_video_remove,
  1484. };
  1485. module_platform_driver(npcm_video_driver);
  1486. MODULE_AUTHOR("Joseph Liu <kwliu@nuvoton.com>");
  1487. MODULE_AUTHOR("Marvin Lin <kflin@nuvoton.com>");
  1488. MODULE_DESCRIPTION("Driver for Nuvoton NPCM Video Capture/Encode Engine");
  1489. MODULE_LICENSE("GPL v2");