vpdma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /*
  2. * VPDMA helper library
  3. *
  4. * Copyright (c) 2013 Texas Instruments Inc.
  5. *
  6. * David Griego, <dagriego@biglakesoftware.com>
  7. * Dale Farnsworth, <dale@farnsworth.org>
  8. * Archit Taneja, <archit@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published by
  12. * the Free Software Foundation.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/err.h>
  17. #include <linux/firmware.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/videodev2.h>
  24. #include "vpdma.h"
  25. #include "vpdma_priv.h"
  26. #define VPDMA_FIRMWARE "vpdma-1b8.bin"
  27. const struct vpdma_data_format vpdma_yuv_fmts[] = {
  28. [VPDMA_DATA_FMT_Y444] = {
  29. .type = VPDMA_DATA_FMT_TYPE_YUV,
  30. .data_type = DATA_TYPE_Y444,
  31. .depth = 8,
  32. },
  33. [VPDMA_DATA_FMT_Y422] = {
  34. .type = VPDMA_DATA_FMT_TYPE_YUV,
  35. .data_type = DATA_TYPE_Y422,
  36. .depth = 8,
  37. },
  38. [VPDMA_DATA_FMT_Y420] = {
  39. .type = VPDMA_DATA_FMT_TYPE_YUV,
  40. .data_type = DATA_TYPE_Y420,
  41. .depth = 8,
  42. },
  43. [VPDMA_DATA_FMT_C444] = {
  44. .type = VPDMA_DATA_FMT_TYPE_YUV,
  45. .data_type = DATA_TYPE_C444,
  46. .depth = 8,
  47. },
  48. [VPDMA_DATA_FMT_C422] = {
  49. .type = VPDMA_DATA_FMT_TYPE_YUV,
  50. .data_type = DATA_TYPE_C422,
  51. .depth = 8,
  52. },
  53. [VPDMA_DATA_FMT_C420] = {
  54. .type = VPDMA_DATA_FMT_TYPE_YUV,
  55. .data_type = DATA_TYPE_C420,
  56. .depth = 4,
  57. },
  58. [VPDMA_DATA_FMT_YC422] = {
  59. .type = VPDMA_DATA_FMT_TYPE_YUV,
  60. .data_type = DATA_TYPE_YC422,
  61. .depth = 16,
  62. },
  63. [VPDMA_DATA_FMT_YC444] = {
  64. .type = VPDMA_DATA_FMT_TYPE_YUV,
  65. .data_type = DATA_TYPE_YC444,
  66. .depth = 24,
  67. },
  68. [VPDMA_DATA_FMT_CY422] = {
  69. .type = VPDMA_DATA_FMT_TYPE_YUV,
  70. .data_type = DATA_TYPE_CY422,
  71. .depth = 16,
  72. },
  73. };
  74. const struct vpdma_data_format vpdma_rgb_fmts[] = {
  75. [VPDMA_DATA_FMT_RGB565] = {
  76. .type = VPDMA_DATA_FMT_TYPE_RGB,
  77. .data_type = DATA_TYPE_RGB16_565,
  78. .depth = 16,
  79. },
  80. [VPDMA_DATA_FMT_ARGB16_1555] = {
  81. .type = VPDMA_DATA_FMT_TYPE_RGB,
  82. .data_type = DATA_TYPE_ARGB_1555,
  83. .depth = 16,
  84. },
  85. [VPDMA_DATA_FMT_ARGB16] = {
  86. .type = VPDMA_DATA_FMT_TYPE_RGB,
  87. .data_type = DATA_TYPE_ARGB_4444,
  88. .depth = 16,
  89. },
  90. [VPDMA_DATA_FMT_RGBA16_5551] = {
  91. .type = VPDMA_DATA_FMT_TYPE_RGB,
  92. .data_type = DATA_TYPE_RGBA_5551,
  93. .depth = 16,
  94. },
  95. [VPDMA_DATA_FMT_RGBA16] = {
  96. .type = VPDMA_DATA_FMT_TYPE_RGB,
  97. .data_type = DATA_TYPE_RGBA_4444,
  98. .depth = 16,
  99. },
  100. [VPDMA_DATA_FMT_ARGB24] = {
  101. .type = VPDMA_DATA_FMT_TYPE_RGB,
  102. .data_type = DATA_TYPE_ARGB24_6666,
  103. .depth = 24,
  104. },
  105. [VPDMA_DATA_FMT_RGB24] = {
  106. .type = VPDMA_DATA_FMT_TYPE_RGB,
  107. .data_type = DATA_TYPE_RGB24_888,
  108. .depth = 24,
  109. },
  110. [VPDMA_DATA_FMT_ARGB32] = {
  111. .type = VPDMA_DATA_FMT_TYPE_RGB,
  112. .data_type = DATA_TYPE_ARGB32_8888,
  113. .depth = 32,
  114. },
  115. [VPDMA_DATA_FMT_RGBA24] = {
  116. .type = VPDMA_DATA_FMT_TYPE_RGB,
  117. .data_type = DATA_TYPE_RGBA24_6666,
  118. .depth = 24,
  119. },
  120. [VPDMA_DATA_FMT_RGBA32] = {
  121. .type = VPDMA_DATA_FMT_TYPE_RGB,
  122. .data_type = DATA_TYPE_RGBA32_8888,
  123. .depth = 32,
  124. },
  125. [VPDMA_DATA_FMT_BGR565] = {
  126. .type = VPDMA_DATA_FMT_TYPE_RGB,
  127. .data_type = DATA_TYPE_BGR16_565,
  128. .depth = 16,
  129. },
  130. [VPDMA_DATA_FMT_ABGR16_1555] = {
  131. .type = VPDMA_DATA_FMT_TYPE_RGB,
  132. .data_type = DATA_TYPE_ABGR_1555,
  133. .depth = 16,
  134. },
  135. [VPDMA_DATA_FMT_ABGR16] = {
  136. .type = VPDMA_DATA_FMT_TYPE_RGB,
  137. .data_type = DATA_TYPE_ABGR_4444,
  138. .depth = 16,
  139. },
  140. [VPDMA_DATA_FMT_BGRA16_5551] = {
  141. .type = VPDMA_DATA_FMT_TYPE_RGB,
  142. .data_type = DATA_TYPE_BGRA_5551,
  143. .depth = 16,
  144. },
  145. [VPDMA_DATA_FMT_BGRA16] = {
  146. .type = VPDMA_DATA_FMT_TYPE_RGB,
  147. .data_type = DATA_TYPE_BGRA_4444,
  148. .depth = 16,
  149. },
  150. [VPDMA_DATA_FMT_ABGR24] = {
  151. .type = VPDMA_DATA_FMT_TYPE_RGB,
  152. .data_type = DATA_TYPE_ABGR24_6666,
  153. .depth = 24,
  154. },
  155. [VPDMA_DATA_FMT_BGR24] = {
  156. .type = VPDMA_DATA_FMT_TYPE_RGB,
  157. .data_type = DATA_TYPE_BGR24_888,
  158. .depth = 24,
  159. },
  160. [VPDMA_DATA_FMT_ABGR32] = {
  161. .type = VPDMA_DATA_FMT_TYPE_RGB,
  162. .data_type = DATA_TYPE_ABGR32_8888,
  163. .depth = 32,
  164. },
  165. [VPDMA_DATA_FMT_BGRA24] = {
  166. .type = VPDMA_DATA_FMT_TYPE_RGB,
  167. .data_type = DATA_TYPE_BGRA24_6666,
  168. .depth = 24,
  169. },
  170. [VPDMA_DATA_FMT_BGRA32] = {
  171. .type = VPDMA_DATA_FMT_TYPE_RGB,
  172. .data_type = DATA_TYPE_BGRA32_8888,
  173. .depth = 32,
  174. },
  175. };
  176. const struct vpdma_data_format vpdma_misc_fmts[] = {
  177. [VPDMA_DATA_FMT_MV] = {
  178. .type = VPDMA_DATA_FMT_TYPE_MISC,
  179. .data_type = DATA_TYPE_MV,
  180. .depth = 4,
  181. },
  182. };
  183. struct vpdma_channel_info {
  184. int num; /* VPDMA channel number */
  185. int cstat_offset; /* client CSTAT register offset */
  186. };
  187. static const struct vpdma_channel_info chan_info[] = {
  188. [VPE_CHAN_LUMA1_IN] = {
  189. .num = VPE_CHAN_NUM_LUMA1_IN,
  190. .cstat_offset = VPDMA_DEI_LUMA1_CSTAT,
  191. },
  192. [VPE_CHAN_CHROMA1_IN] = {
  193. .num = VPE_CHAN_NUM_CHROMA1_IN,
  194. .cstat_offset = VPDMA_DEI_CHROMA1_CSTAT,
  195. },
  196. [VPE_CHAN_LUMA2_IN] = {
  197. .num = VPE_CHAN_NUM_LUMA2_IN,
  198. .cstat_offset = VPDMA_DEI_LUMA2_CSTAT,
  199. },
  200. [VPE_CHAN_CHROMA2_IN] = {
  201. .num = VPE_CHAN_NUM_CHROMA2_IN,
  202. .cstat_offset = VPDMA_DEI_CHROMA2_CSTAT,
  203. },
  204. [VPE_CHAN_LUMA3_IN] = {
  205. .num = VPE_CHAN_NUM_LUMA3_IN,
  206. .cstat_offset = VPDMA_DEI_LUMA3_CSTAT,
  207. },
  208. [VPE_CHAN_CHROMA3_IN] = {
  209. .num = VPE_CHAN_NUM_CHROMA3_IN,
  210. .cstat_offset = VPDMA_DEI_CHROMA3_CSTAT,
  211. },
  212. [VPE_CHAN_MV_IN] = {
  213. .num = VPE_CHAN_NUM_MV_IN,
  214. .cstat_offset = VPDMA_DEI_MV_IN_CSTAT,
  215. },
  216. [VPE_CHAN_MV_OUT] = {
  217. .num = VPE_CHAN_NUM_MV_OUT,
  218. .cstat_offset = VPDMA_DEI_MV_OUT_CSTAT,
  219. },
  220. [VPE_CHAN_LUMA_OUT] = {
  221. .num = VPE_CHAN_NUM_LUMA_OUT,
  222. .cstat_offset = VPDMA_VIP_UP_Y_CSTAT,
  223. },
  224. [VPE_CHAN_CHROMA_OUT] = {
  225. .num = VPE_CHAN_NUM_CHROMA_OUT,
  226. .cstat_offset = VPDMA_VIP_UP_UV_CSTAT,
  227. },
  228. [VPE_CHAN_RGB_OUT] = {
  229. .num = VPE_CHAN_NUM_RGB_OUT,
  230. .cstat_offset = VPDMA_VIP_UP_Y_CSTAT,
  231. },
  232. };
  233. static u32 read_reg(struct vpdma_data *vpdma, int offset)
  234. {
  235. return ioread32(vpdma->base + offset);
  236. }
  237. static void write_reg(struct vpdma_data *vpdma, int offset, u32 value)
  238. {
  239. iowrite32(value, vpdma->base + offset);
  240. }
  241. static int read_field_reg(struct vpdma_data *vpdma, int offset,
  242. u32 mask, int shift)
  243. {
  244. return (read_reg(vpdma, offset) & (mask << shift)) >> shift;
  245. }
  246. static void write_field_reg(struct vpdma_data *vpdma, int offset, u32 field,
  247. u32 mask, int shift)
  248. {
  249. u32 val = read_reg(vpdma, offset);
  250. val &= ~(mask << shift);
  251. val |= (field & mask) << shift;
  252. write_reg(vpdma, offset, val);
  253. }
  254. void vpdma_dump_regs(struct vpdma_data *vpdma)
  255. {
  256. struct device *dev = &vpdma->pdev->dev;
  257. #define DUMPREG(r) dev_dbg(dev, "%-35s %08x\n", #r, read_reg(vpdma, VPDMA_##r))
  258. dev_dbg(dev, "VPDMA Registers:\n");
  259. DUMPREG(PID);
  260. DUMPREG(LIST_ADDR);
  261. DUMPREG(LIST_ATTR);
  262. DUMPREG(LIST_STAT_SYNC);
  263. DUMPREG(BG_RGB);
  264. DUMPREG(BG_YUV);
  265. DUMPREG(SETUP);
  266. DUMPREG(MAX_SIZE1);
  267. DUMPREG(MAX_SIZE2);
  268. DUMPREG(MAX_SIZE3);
  269. /*
  270. * dumping registers of only group0 and group3, because VPE channels
  271. * lie within group0 and group3 registers
  272. */
  273. DUMPREG(INT_CHAN_STAT(0));
  274. DUMPREG(INT_CHAN_MASK(0));
  275. DUMPREG(INT_CHAN_STAT(3));
  276. DUMPREG(INT_CHAN_MASK(3));
  277. DUMPREG(INT_CLIENT0_STAT);
  278. DUMPREG(INT_CLIENT0_MASK);
  279. DUMPREG(INT_CLIENT1_STAT);
  280. DUMPREG(INT_CLIENT1_MASK);
  281. DUMPREG(INT_LIST0_STAT);
  282. DUMPREG(INT_LIST0_MASK);
  283. /*
  284. * these are registers specific to VPE clients, we can make this
  285. * function dump client registers specific to VPE or VIP based on
  286. * who is using it
  287. */
  288. DUMPREG(DEI_CHROMA1_CSTAT);
  289. DUMPREG(DEI_LUMA1_CSTAT);
  290. DUMPREG(DEI_CHROMA2_CSTAT);
  291. DUMPREG(DEI_LUMA2_CSTAT);
  292. DUMPREG(DEI_CHROMA3_CSTAT);
  293. DUMPREG(DEI_LUMA3_CSTAT);
  294. DUMPREG(DEI_MV_IN_CSTAT);
  295. DUMPREG(DEI_MV_OUT_CSTAT);
  296. DUMPREG(VIP_UP_Y_CSTAT);
  297. DUMPREG(VIP_UP_UV_CSTAT);
  298. DUMPREG(VPI_CTL_CSTAT);
  299. }
  300. /*
  301. * Allocate a DMA buffer
  302. */
  303. int vpdma_alloc_desc_buf(struct vpdma_buf *buf, size_t size)
  304. {
  305. buf->size = size;
  306. buf->mapped = false;
  307. buf->addr = kzalloc(size, GFP_KERNEL);
  308. if (!buf->addr)
  309. return -ENOMEM;
  310. WARN_ON(((unsigned long)buf->addr & VPDMA_DESC_ALIGN) != 0);
  311. return 0;
  312. }
  313. void vpdma_free_desc_buf(struct vpdma_buf *buf)
  314. {
  315. WARN_ON(buf->mapped);
  316. kfree(buf->addr);
  317. buf->addr = NULL;
  318. buf->size = 0;
  319. }
  320. /*
  321. * map descriptor/payload DMA buffer, enabling DMA access
  322. */
  323. int vpdma_map_desc_buf(struct vpdma_data *vpdma, struct vpdma_buf *buf)
  324. {
  325. struct device *dev = &vpdma->pdev->dev;
  326. WARN_ON(buf->mapped);
  327. buf->dma_addr = dma_map_single(dev, buf->addr, buf->size,
  328. DMA_TO_DEVICE);
  329. if (dma_mapping_error(dev, buf->dma_addr)) {
  330. dev_err(dev, "failed to map buffer\n");
  331. return -EINVAL;
  332. }
  333. buf->mapped = true;
  334. return 0;
  335. }
  336. /*
  337. * unmap descriptor/payload DMA buffer, disabling DMA access and
  338. * allowing the main processor to acces the data
  339. */
  340. void vpdma_unmap_desc_buf(struct vpdma_data *vpdma, struct vpdma_buf *buf)
  341. {
  342. struct device *dev = &vpdma->pdev->dev;
  343. if (buf->mapped)
  344. dma_unmap_single(dev, buf->dma_addr, buf->size, DMA_TO_DEVICE);
  345. buf->mapped = false;
  346. }
  347. /*
  348. * create a descriptor list, the user of this list will append configuration,
  349. * control and data descriptors to this list, this list will be submitted to
  350. * VPDMA. VPDMA's list parser will go through each descriptor and perform the
  351. * required DMA operations
  352. */
  353. int vpdma_create_desc_list(struct vpdma_desc_list *list, size_t size, int type)
  354. {
  355. int r;
  356. r = vpdma_alloc_desc_buf(&list->buf, size);
  357. if (r)
  358. return r;
  359. list->next = list->buf.addr;
  360. list->type = type;
  361. return 0;
  362. }
  363. /*
  364. * once a descriptor list is parsed by VPDMA, we reset the list by emptying it,
  365. * to allow new descriptors to be added to the list.
  366. */
  367. void vpdma_reset_desc_list(struct vpdma_desc_list *list)
  368. {
  369. list->next = list->buf.addr;
  370. }
  371. /*
  372. * free the buffer allocated fot the VPDMA descriptor list, this should be
  373. * called when the user doesn't want to use VPDMA any more.
  374. */
  375. void vpdma_free_desc_list(struct vpdma_desc_list *list)
  376. {
  377. vpdma_free_desc_buf(&list->buf);
  378. list->next = NULL;
  379. }
  380. static bool vpdma_list_busy(struct vpdma_data *vpdma, int list_num)
  381. {
  382. return read_reg(vpdma, VPDMA_LIST_STAT_SYNC) & BIT(list_num + 16);
  383. }
  384. /*
  385. * submit a list of DMA descriptors to the VPE VPDMA, do not wait for completion
  386. */
  387. int vpdma_submit_descs(struct vpdma_data *vpdma, struct vpdma_desc_list *list)
  388. {
  389. /* we always use the first list */
  390. int list_num = 0;
  391. int list_size;
  392. if (vpdma_list_busy(vpdma, list_num))
  393. return -EBUSY;
  394. /* 16-byte granularity */
  395. list_size = (list->next - list->buf.addr) >> 4;
  396. write_reg(vpdma, VPDMA_LIST_ADDR, (u32) list->buf.dma_addr);
  397. write_reg(vpdma, VPDMA_LIST_ATTR,
  398. (list_num << VPDMA_LIST_NUM_SHFT) |
  399. (list->type << VPDMA_LIST_TYPE_SHFT) |
  400. list_size);
  401. return 0;
  402. }
  403. static void dump_cfd(struct vpdma_cfd *cfd)
  404. {
  405. int class;
  406. class = cfd_get_class(cfd);
  407. pr_debug("config descriptor of payload class: %s\n",
  408. class == CFD_CLS_BLOCK ? "simple block" :
  409. "address data block");
  410. if (class == CFD_CLS_BLOCK)
  411. pr_debug("word0: dst_addr_offset = 0x%08x\n",
  412. cfd->dest_addr_offset);
  413. if (class == CFD_CLS_BLOCK)
  414. pr_debug("word1: num_data_wrds = %d\n", cfd->block_len);
  415. pr_debug("word2: payload_addr = 0x%08x\n", cfd->payload_addr);
  416. pr_debug("word3: pkt_type = %d, direct = %d, class = %d, dest = %d, "
  417. "payload_len = %d\n", cfd_get_pkt_type(cfd),
  418. cfd_get_direct(cfd), class, cfd_get_dest(cfd),
  419. cfd_get_payload_len(cfd));
  420. }
  421. /*
  422. * append a configuration descriptor to the given descriptor list, where the
  423. * payload is in the form of a simple data block specified in the descriptor
  424. * header, this is used to upload scaler coefficients to the scaler module
  425. */
  426. void vpdma_add_cfd_block(struct vpdma_desc_list *list, int client,
  427. struct vpdma_buf *blk, u32 dest_offset)
  428. {
  429. struct vpdma_cfd *cfd;
  430. int len = blk->size;
  431. WARN_ON(blk->dma_addr & VPDMA_DESC_ALIGN);
  432. cfd = list->next;
  433. WARN_ON((void *)(cfd + 1) > (list->buf.addr + list->buf.size));
  434. cfd->dest_addr_offset = dest_offset;
  435. cfd->block_len = len;
  436. cfd->payload_addr = (u32) blk->dma_addr;
  437. cfd->ctl_payload_len = cfd_pkt_payload_len(CFD_INDIRECT, CFD_CLS_BLOCK,
  438. client, len >> 4);
  439. list->next = cfd + 1;
  440. dump_cfd(cfd);
  441. }
  442. /*
  443. * append a configuration descriptor to the given descriptor list, where the
  444. * payload is in the address data block format, this is used to a configure a
  445. * discontiguous set of MMRs
  446. */
  447. void vpdma_add_cfd_adb(struct vpdma_desc_list *list, int client,
  448. struct vpdma_buf *adb)
  449. {
  450. struct vpdma_cfd *cfd;
  451. unsigned int len = adb->size;
  452. WARN_ON(len & VPDMA_ADB_SIZE_ALIGN);
  453. WARN_ON(adb->dma_addr & VPDMA_DESC_ALIGN);
  454. cfd = list->next;
  455. BUG_ON((void *)(cfd + 1) > (list->buf.addr + list->buf.size));
  456. cfd->w0 = 0;
  457. cfd->w1 = 0;
  458. cfd->payload_addr = (u32) adb->dma_addr;
  459. cfd->ctl_payload_len = cfd_pkt_payload_len(CFD_INDIRECT, CFD_CLS_ADB,
  460. client, len >> 4);
  461. list->next = cfd + 1;
  462. dump_cfd(cfd);
  463. };
  464. /*
  465. * control descriptor format change based on what type of control descriptor it
  466. * is, we only use 'sync on channel' control descriptors for now, so assume it's
  467. * that
  468. */
  469. static void dump_ctd(struct vpdma_ctd *ctd)
  470. {
  471. pr_debug("control descriptor\n");
  472. pr_debug("word3: pkt_type = %d, source = %d, ctl_type = %d\n",
  473. ctd_get_pkt_type(ctd), ctd_get_source(ctd), ctd_get_ctl(ctd));
  474. }
  475. /*
  476. * append a 'sync on channel' type control descriptor to the given descriptor
  477. * list, this descriptor stalls the VPDMA list till the time DMA is completed
  478. * on the specified channel
  479. */
  480. void vpdma_add_sync_on_channel_ctd(struct vpdma_desc_list *list,
  481. enum vpdma_channel chan)
  482. {
  483. struct vpdma_ctd *ctd;
  484. ctd = list->next;
  485. WARN_ON((void *)(ctd + 1) > (list->buf.addr + list->buf.size));
  486. ctd->w0 = 0;
  487. ctd->w1 = 0;
  488. ctd->w2 = 0;
  489. ctd->type_source_ctl = ctd_type_source_ctl(chan_info[chan].num,
  490. CTD_TYPE_SYNC_ON_CHANNEL);
  491. list->next = ctd + 1;
  492. dump_ctd(ctd);
  493. }
  494. static void dump_dtd(struct vpdma_dtd *dtd)
  495. {
  496. int dir, chan;
  497. dir = dtd_get_dir(dtd);
  498. chan = dtd_get_chan(dtd);
  499. pr_debug("%s data transfer descriptor for channel %d\n",
  500. dir == DTD_DIR_OUT ? "outbound" : "inbound", chan);
  501. pr_debug("word0: data_type = %d, notify = %d, field = %d, 1D = %d, "
  502. "even_ln_skp = %d, odd_ln_skp = %d, line_stride = %d\n",
  503. dtd_get_data_type(dtd), dtd_get_notify(dtd), dtd_get_field(dtd),
  504. dtd_get_1d(dtd), dtd_get_even_line_skip(dtd),
  505. dtd_get_odd_line_skip(dtd), dtd_get_line_stride(dtd));
  506. if (dir == DTD_DIR_IN)
  507. pr_debug("word1: line_length = %d, xfer_height = %d\n",
  508. dtd_get_line_length(dtd), dtd_get_xfer_height(dtd));
  509. pr_debug("word2: start_addr = %pad\n", &dtd->start_addr);
  510. pr_debug("word3: pkt_type = %d, mode = %d, dir = %d, chan = %d, "
  511. "pri = %d, next_chan = %d\n", dtd_get_pkt_type(dtd),
  512. dtd_get_mode(dtd), dir, chan, dtd_get_priority(dtd),
  513. dtd_get_next_chan(dtd));
  514. if (dir == DTD_DIR_IN)
  515. pr_debug("word4: frame_width = %d, frame_height = %d\n",
  516. dtd_get_frame_width(dtd), dtd_get_frame_height(dtd));
  517. else
  518. pr_debug("word4: desc_write_addr = 0x%08x, write_desc = %d, "
  519. "drp_data = %d, use_desc_reg = %d\n",
  520. dtd_get_desc_write_addr(dtd), dtd_get_write_desc(dtd),
  521. dtd_get_drop_data(dtd), dtd_get_use_desc(dtd));
  522. if (dir == DTD_DIR_IN)
  523. pr_debug("word5: hor_start = %d, ver_start = %d\n",
  524. dtd_get_h_start(dtd), dtd_get_v_start(dtd));
  525. else
  526. pr_debug("word5: max_width %d, max_height %d\n",
  527. dtd_get_max_width(dtd), dtd_get_max_height(dtd));
  528. pr_debug("word6: client specific attr0 = 0x%08x\n", dtd->client_attr0);
  529. pr_debug("word7: client specific attr1 = 0x%08x\n", dtd->client_attr1);
  530. }
  531. /*
  532. * append an outbound data transfer descriptor to the given descriptor list,
  533. * this sets up a 'client to memory' VPDMA transfer for the given VPDMA channel
  534. *
  535. * @list: vpdma desc list to which we add this decriptor
  536. * @width: width of the image in pixels in memory
  537. * @c_rect: compose params of output image
  538. * @fmt: vpdma data format of the buffer
  539. * dma_addr: dma address as seen by VPDMA
  540. * chan: VPDMA channel
  541. * flags: VPDMA flags to configure some descriptor fileds
  542. */
  543. void vpdma_add_out_dtd(struct vpdma_desc_list *list, int width,
  544. const struct v4l2_rect *c_rect,
  545. const struct vpdma_data_format *fmt, dma_addr_t dma_addr,
  546. enum vpdma_channel chan, u32 flags)
  547. {
  548. int priority = 0;
  549. int field = 0;
  550. int notify = 1;
  551. int channel, next_chan;
  552. struct v4l2_rect rect = *c_rect;
  553. int depth = fmt->depth;
  554. int stride;
  555. struct vpdma_dtd *dtd;
  556. channel = next_chan = chan_info[chan].num;
  557. if (fmt->type == VPDMA_DATA_FMT_TYPE_YUV &&
  558. fmt->data_type == DATA_TYPE_C420) {
  559. rect.height >>= 1;
  560. rect.top >>= 1;
  561. depth = 8;
  562. }
  563. stride = ALIGN((depth * width) >> 3, VPDMA_STRIDE_ALIGN);
  564. dma_addr += rect.top * stride + (rect.left * depth >> 3);
  565. dtd = list->next;
  566. WARN_ON((void *)(dtd + 1) > (list->buf.addr + list->buf.size));
  567. dtd->type_ctl_stride = dtd_type_ctl_stride(fmt->data_type,
  568. notify,
  569. field,
  570. !!(flags & VPDMA_DATA_FRAME_1D),
  571. !!(flags & VPDMA_DATA_EVEN_LINE_SKIP),
  572. !!(flags & VPDMA_DATA_ODD_LINE_SKIP),
  573. stride);
  574. dtd->w1 = 0;
  575. dtd->start_addr = (u32) dma_addr;
  576. dtd->pkt_ctl = dtd_pkt_ctl(!!(flags & VPDMA_DATA_MODE_TILED),
  577. DTD_DIR_OUT, channel, priority, next_chan);
  578. dtd->desc_write_addr = dtd_desc_write_addr(0, 0, 0, 0);
  579. dtd->max_width_height = dtd_max_width_height(MAX_OUT_WIDTH_1920,
  580. MAX_OUT_HEIGHT_1080);
  581. dtd->client_attr0 = 0;
  582. dtd->client_attr1 = 0;
  583. list->next = dtd + 1;
  584. dump_dtd(dtd);
  585. }
  586. /*
  587. * append an inbound data transfer descriptor to the given descriptor list,
  588. * this sets up a 'memory to client' VPDMA transfer for the given VPDMA channel
  589. *
  590. * @list: vpdma desc list to which we add this decriptor
  591. * @width: width of the image in pixels in memory(not the cropped width)
  592. * @c_rect: crop params of input image
  593. * @fmt: vpdma data format of the buffer
  594. * dma_addr: dma address as seen by VPDMA
  595. * chan: VPDMA channel
  596. * field: top or bottom field info of the input image
  597. * flags: VPDMA flags to configure some descriptor fileds
  598. * frame_width/height: the complete width/height of the image presented to the
  599. * client (this makes sense when multiple channels are
  600. * connected to the same client, forming a larger frame)
  601. * start_h, start_v: position where the given channel starts providing pixel
  602. * data to the client (makes sense when multiple channels
  603. * contribute to the client)
  604. */
  605. void vpdma_add_in_dtd(struct vpdma_desc_list *list, int width,
  606. const struct v4l2_rect *c_rect,
  607. const struct vpdma_data_format *fmt, dma_addr_t dma_addr,
  608. enum vpdma_channel chan, int field, u32 flags, int frame_width,
  609. int frame_height, int start_h, int start_v)
  610. {
  611. int priority = 0;
  612. int notify = 1;
  613. int depth = fmt->depth;
  614. int channel, next_chan;
  615. struct v4l2_rect rect = *c_rect;
  616. int stride;
  617. struct vpdma_dtd *dtd;
  618. channel = next_chan = chan_info[chan].num;
  619. if (fmt->type == VPDMA_DATA_FMT_TYPE_YUV &&
  620. fmt->data_type == DATA_TYPE_C420) {
  621. rect.height >>= 1;
  622. rect.top >>= 1;
  623. depth = 8;
  624. }
  625. stride = ALIGN((depth * width) >> 3, VPDMA_STRIDE_ALIGN);
  626. dma_addr += rect.top * stride + (rect.left * depth >> 3);
  627. dtd = list->next;
  628. WARN_ON((void *)(dtd + 1) > (list->buf.addr + list->buf.size));
  629. dtd->type_ctl_stride = dtd_type_ctl_stride(fmt->data_type,
  630. notify,
  631. field,
  632. !!(flags & VPDMA_DATA_FRAME_1D),
  633. !!(flags & VPDMA_DATA_EVEN_LINE_SKIP),
  634. !!(flags & VPDMA_DATA_ODD_LINE_SKIP),
  635. stride);
  636. dtd->xfer_length_height = dtd_xfer_length_height(rect.width,
  637. rect.height);
  638. dtd->start_addr = (u32) dma_addr;
  639. dtd->pkt_ctl = dtd_pkt_ctl(!!(flags & VPDMA_DATA_MODE_TILED),
  640. DTD_DIR_IN, channel, priority, next_chan);
  641. dtd->frame_width_height = dtd_frame_width_height(frame_width,
  642. frame_height);
  643. dtd->start_h_v = dtd_start_h_v(start_h, start_v);
  644. dtd->client_attr0 = 0;
  645. dtd->client_attr1 = 0;
  646. list->next = dtd + 1;
  647. dump_dtd(dtd);
  648. }
  649. /* set or clear the mask for list complete interrupt */
  650. void vpdma_enable_list_complete_irq(struct vpdma_data *vpdma, int list_num,
  651. bool enable)
  652. {
  653. u32 val;
  654. val = read_reg(vpdma, VPDMA_INT_LIST0_MASK);
  655. if (enable)
  656. val |= (1 << (list_num * 2));
  657. else
  658. val &= ~(1 << (list_num * 2));
  659. write_reg(vpdma, VPDMA_INT_LIST0_MASK, val);
  660. }
  661. /* clear previosuly occured list intterupts in the LIST_STAT register */
  662. void vpdma_clear_list_stat(struct vpdma_data *vpdma)
  663. {
  664. write_reg(vpdma, VPDMA_INT_LIST0_STAT,
  665. read_reg(vpdma, VPDMA_INT_LIST0_STAT));
  666. }
  667. /*
  668. * configures the output mode of the line buffer for the given client, the
  669. * line buffer content can either be mirrored(each line repeated twice) or
  670. * passed to the client as is
  671. */
  672. void vpdma_set_line_mode(struct vpdma_data *vpdma, int line_mode,
  673. enum vpdma_channel chan)
  674. {
  675. int client_cstat = chan_info[chan].cstat_offset;
  676. write_field_reg(vpdma, client_cstat, line_mode,
  677. VPDMA_CSTAT_LINE_MODE_MASK, VPDMA_CSTAT_LINE_MODE_SHIFT);
  678. }
  679. /*
  680. * configures the event which should trigger VPDMA transfer for the given
  681. * client
  682. */
  683. void vpdma_set_frame_start_event(struct vpdma_data *vpdma,
  684. enum vpdma_frame_start_event fs_event,
  685. enum vpdma_channel chan)
  686. {
  687. int client_cstat = chan_info[chan].cstat_offset;
  688. write_field_reg(vpdma, client_cstat, fs_event,
  689. VPDMA_CSTAT_FRAME_START_MASK, VPDMA_CSTAT_FRAME_START_SHIFT);
  690. }
  691. static void vpdma_firmware_cb(const struct firmware *f, void *context)
  692. {
  693. struct vpdma_data *vpdma = context;
  694. struct vpdma_buf fw_dma_buf;
  695. int i, r;
  696. dev_dbg(&vpdma->pdev->dev, "firmware callback\n");
  697. if (!f || !f->data) {
  698. dev_err(&vpdma->pdev->dev, "couldn't get firmware\n");
  699. return;
  700. }
  701. /* already initialized */
  702. if (read_field_reg(vpdma, VPDMA_LIST_ATTR, VPDMA_LIST_RDY_MASK,
  703. VPDMA_LIST_RDY_SHFT)) {
  704. vpdma->cb(vpdma->pdev);
  705. return;
  706. }
  707. r = vpdma_alloc_desc_buf(&fw_dma_buf, f->size);
  708. if (r) {
  709. dev_err(&vpdma->pdev->dev,
  710. "failed to allocate dma buffer for firmware\n");
  711. goto rel_fw;
  712. }
  713. memcpy(fw_dma_buf.addr, f->data, f->size);
  714. vpdma_map_desc_buf(vpdma, &fw_dma_buf);
  715. write_reg(vpdma, VPDMA_LIST_ADDR, (u32) fw_dma_buf.dma_addr);
  716. for (i = 0; i < 100; i++) { /* max 1 second */
  717. msleep_interruptible(10);
  718. if (read_field_reg(vpdma, VPDMA_LIST_ATTR, VPDMA_LIST_RDY_MASK,
  719. VPDMA_LIST_RDY_SHFT))
  720. break;
  721. }
  722. if (i == 100) {
  723. dev_err(&vpdma->pdev->dev, "firmware upload failed\n");
  724. goto free_buf;
  725. }
  726. vpdma->cb(vpdma->pdev);
  727. free_buf:
  728. vpdma_unmap_desc_buf(vpdma, &fw_dma_buf);
  729. vpdma_free_desc_buf(&fw_dma_buf);
  730. rel_fw:
  731. release_firmware(f);
  732. }
  733. static int vpdma_load_firmware(struct vpdma_data *vpdma)
  734. {
  735. int r;
  736. struct device *dev = &vpdma->pdev->dev;
  737. r = request_firmware_nowait(THIS_MODULE, 1,
  738. (const char *) VPDMA_FIRMWARE, dev, GFP_KERNEL, vpdma,
  739. vpdma_firmware_cb);
  740. if (r) {
  741. dev_err(dev, "firmware not available %s\n", VPDMA_FIRMWARE);
  742. return r;
  743. } else {
  744. dev_info(dev, "loading firmware %s\n", VPDMA_FIRMWARE);
  745. }
  746. return 0;
  747. }
  748. struct vpdma_data *vpdma_create(struct platform_device *pdev,
  749. void (*cb)(struct platform_device *pdev))
  750. {
  751. struct resource *res;
  752. struct vpdma_data *vpdma;
  753. int r;
  754. dev_dbg(&pdev->dev, "vpdma_create\n");
  755. vpdma = devm_kzalloc(&pdev->dev, sizeof(*vpdma), GFP_KERNEL);
  756. if (!vpdma) {
  757. dev_err(&pdev->dev, "couldn't alloc vpdma_dev\n");
  758. return ERR_PTR(-ENOMEM);
  759. }
  760. vpdma->pdev = pdev;
  761. vpdma->cb = cb;
  762. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpdma");
  763. if (res == NULL) {
  764. dev_err(&pdev->dev, "missing platform resources data\n");
  765. return ERR_PTR(-ENODEV);
  766. }
  767. vpdma->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  768. if (!vpdma->base) {
  769. dev_err(&pdev->dev, "failed to ioremap\n");
  770. return ERR_PTR(-ENOMEM);
  771. }
  772. r = vpdma_load_firmware(vpdma);
  773. if (r) {
  774. pr_err("failed to load firmware %s\n", VPDMA_FIRMWARE);
  775. return ERR_PTR(r);
  776. }
  777. return vpdma;
  778. }
  779. MODULE_FIRMWARE(VPDMA_FIRMWARE);