vsp1_drv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * vsp1_drv.c -- R-Car VSP1 Driver
  3. *
  4. * Copyright (C) 2013 Renesas Corporation
  5. *
  6. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/videodev2.h>
  20. #include "vsp1.h"
  21. #include "vsp1_lif.h"
  22. #include "vsp1_rwpf.h"
  23. #include "vsp1_uds.h"
  24. /* -----------------------------------------------------------------------------
  25. * Interrupt Handling
  26. */
  27. static irqreturn_t vsp1_irq_handler(int irq, void *data)
  28. {
  29. u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
  30. struct vsp1_device *vsp1 = data;
  31. irqreturn_t ret = IRQ_NONE;
  32. unsigned int i;
  33. for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
  34. struct vsp1_rwpf *wpf = vsp1->wpf[i];
  35. struct vsp1_pipeline *pipe;
  36. u32 status;
  37. if (wpf == NULL)
  38. continue;
  39. pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
  40. status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
  41. vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
  42. if (status & VI6_WFP_IRQ_STA_FRE) {
  43. vsp1_pipeline_frame_end(pipe);
  44. ret = IRQ_HANDLED;
  45. }
  46. }
  47. return ret;
  48. }
  49. /* -----------------------------------------------------------------------------
  50. * Entities
  51. */
  52. /*
  53. * vsp1_create_links - Create links from all sources to the given sink
  54. *
  55. * This function creates media links from all valid sources to the given sink
  56. * pad. Links that would be invalid according to the VSP1 hardware capabilities
  57. * are skipped. Those include all links
  58. *
  59. * - from a UDS to a UDS (UDS entities can't be chained)
  60. * - from an entity to itself (no loops are allowed)
  61. */
  62. static int vsp1_create_links(struct vsp1_device *vsp1, struct vsp1_entity *sink)
  63. {
  64. struct media_entity *entity = &sink->subdev.entity;
  65. struct vsp1_entity *source;
  66. unsigned int pad;
  67. int ret;
  68. list_for_each_entry(source, &vsp1->entities, list_dev) {
  69. u32 flags;
  70. if (source->type == sink->type)
  71. continue;
  72. if (source->type == VSP1_ENTITY_LIF ||
  73. source->type == VSP1_ENTITY_WPF)
  74. continue;
  75. flags = source->type == VSP1_ENTITY_RPF &&
  76. sink->type == VSP1_ENTITY_WPF &&
  77. source->index == sink->index
  78. ? MEDIA_LNK_FL_ENABLED : 0;
  79. for (pad = 0; pad < entity->num_pads; ++pad) {
  80. if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
  81. continue;
  82. ret = media_entity_create_link(&source->subdev.entity,
  83. source->source_pad,
  84. entity, pad, flags);
  85. if (ret < 0)
  86. return ret;
  87. if (flags & MEDIA_LNK_FL_ENABLED)
  88. source->sink = entity;
  89. }
  90. }
  91. return 0;
  92. }
  93. static void vsp1_destroy_entities(struct vsp1_device *vsp1)
  94. {
  95. struct vsp1_entity *entity;
  96. struct vsp1_entity *next;
  97. list_for_each_entry_safe(entity, next, &vsp1->entities, list_dev) {
  98. list_del(&entity->list_dev);
  99. vsp1_entity_destroy(entity);
  100. }
  101. v4l2_device_unregister(&vsp1->v4l2_dev);
  102. media_device_unregister(&vsp1->media_dev);
  103. }
  104. static int vsp1_create_entities(struct vsp1_device *vsp1)
  105. {
  106. struct media_device *mdev = &vsp1->media_dev;
  107. struct v4l2_device *vdev = &vsp1->v4l2_dev;
  108. struct vsp1_entity *entity;
  109. unsigned int i;
  110. int ret;
  111. mdev->dev = vsp1->dev;
  112. strlcpy(mdev->model, "VSP1", sizeof(mdev->model));
  113. ret = media_device_register(mdev);
  114. if (ret < 0) {
  115. dev_err(vsp1->dev, "media device registration failed (%d)\n",
  116. ret);
  117. return ret;
  118. }
  119. vdev->mdev = mdev;
  120. ret = v4l2_device_register(vsp1->dev, vdev);
  121. if (ret < 0) {
  122. dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
  123. ret);
  124. goto done;
  125. }
  126. /* Instantiate all the entities. */
  127. if (vsp1->pdata->features & VSP1_HAS_LIF) {
  128. vsp1->lif = vsp1_lif_create(vsp1);
  129. if (IS_ERR(vsp1->lif)) {
  130. ret = PTR_ERR(vsp1->lif);
  131. goto done;
  132. }
  133. list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
  134. }
  135. for (i = 0; i < vsp1->pdata->rpf_count; ++i) {
  136. struct vsp1_rwpf *rpf;
  137. rpf = vsp1_rpf_create(vsp1, i);
  138. if (IS_ERR(rpf)) {
  139. ret = PTR_ERR(rpf);
  140. goto done;
  141. }
  142. vsp1->rpf[i] = rpf;
  143. list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
  144. }
  145. for (i = 0; i < vsp1->pdata->uds_count; ++i) {
  146. struct vsp1_uds *uds;
  147. uds = vsp1_uds_create(vsp1, i);
  148. if (IS_ERR(uds)) {
  149. ret = PTR_ERR(uds);
  150. goto done;
  151. }
  152. vsp1->uds[i] = uds;
  153. list_add_tail(&uds->entity.list_dev, &vsp1->entities);
  154. }
  155. for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
  156. struct vsp1_rwpf *wpf;
  157. wpf = vsp1_wpf_create(vsp1, i);
  158. if (IS_ERR(wpf)) {
  159. ret = PTR_ERR(wpf);
  160. goto done;
  161. }
  162. vsp1->wpf[i] = wpf;
  163. list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
  164. }
  165. /* Create links. */
  166. list_for_each_entry(entity, &vsp1->entities, list_dev) {
  167. if (entity->type == VSP1_ENTITY_LIF ||
  168. entity->type == VSP1_ENTITY_RPF)
  169. continue;
  170. ret = vsp1_create_links(vsp1, entity);
  171. if (ret < 0)
  172. goto done;
  173. }
  174. if (vsp1->pdata->features & VSP1_HAS_LIF) {
  175. ret = media_entity_create_link(
  176. &vsp1->wpf[0]->entity.subdev.entity, RWPF_PAD_SOURCE,
  177. &vsp1->lif->entity.subdev.entity, LIF_PAD_SINK, 0);
  178. if (ret < 0)
  179. return ret;
  180. }
  181. /* Register all subdevs. */
  182. list_for_each_entry(entity, &vsp1->entities, list_dev) {
  183. ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
  184. &entity->subdev);
  185. if (ret < 0)
  186. goto done;
  187. }
  188. ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
  189. done:
  190. if (ret < 0)
  191. vsp1_destroy_entities(vsp1);
  192. return ret;
  193. }
  194. static int vsp1_device_init(struct vsp1_device *vsp1)
  195. {
  196. unsigned int i;
  197. u32 status;
  198. /* Reset any channel that might be running. */
  199. status = vsp1_read(vsp1, VI6_STATUS);
  200. for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
  201. unsigned int timeout;
  202. if (!(status & VI6_STATUS_SYS_ACT(i)))
  203. continue;
  204. vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(i));
  205. for (timeout = 10; timeout > 0; --timeout) {
  206. status = vsp1_read(vsp1, VI6_STATUS);
  207. if (!(status & VI6_STATUS_SYS_ACT(i)))
  208. break;
  209. usleep_range(1000, 2000);
  210. }
  211. if (!timeout) {
  212. dev_err(vsp1->dev, "failed to reset wpf.%u\n", i);
  213. return -ETIMEDOUT;
  214. }
  215. }
  216. vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
  217. (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
  218. for (i = 0; i < vsp1->pdata->rpf_count; ++i)
  219. vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
  220. for (i = 0; i < vsp1->pdata->uds_count; ++i)
  221. vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
  222. vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
  223. vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
  224. vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
  225. vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
  226. vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
  227. vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
  228. vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
  229. (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
  230. vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
  231. (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
  232. return 0;
  233. }
  234. /*
  235. * vsp1_device_get - Acquire the VSP1 device
  236. *
  237. * Increment the VSP1 reference count and initialize the device if the first
  238. * reference is taken.
  239. *
  240. * Return a pointer to the VSP1 device or NULL if an error occured.
  241. */
  242. struct vsp1_device *vsp1_device_get(struct vsp1_device *vsp1)
  243. {
  244. struct vsp1_device *__vsp1 = vsp1;
  245. int ret;
  246. mutex_lock(&vsp1->lock);
  247. if (vsp1->ref_count > 0)
  248. goto done;
  249. ret = clk_prepare_enable(vsp1->clock);
  250. if (ret < 0) {
  251. __vsp1 = NULL;
  252. goto done;
  253. }
  254. ret = vsp1_device_init(vsp1);
  255. if (ret < 0) {
  256. clk_disable_unprepare(vsp1->clock);
  257. __vsp1 = NULL;
  258. goto done;
  259. }
  260. done:
  261. if (__vsp1)
  262. vsp1->ref_count++;
  263. mutex_unlock(&vsp1->lock);
  264. return __vsp1;
  265. }
  266. /*
  267. * vsp1_device_put - Release the VSP1 device
  268. *
  269. * Decrement the VSP1 reference count and cleanup the device if the last
  270. * reference is released.
  271. */
  272. void vsp1_device_put(struct vsp1_device *vsp1)
  273. {
  274. mutex_lock(&vsp1->lock);
  275. if (--vsp1->ref_count == 0)
  276. clk_disable_unprepare(vsp1->clock);
  277. mutex_unlock(&vsp1->lock);
  278. }
  279. /* -----------------------------------------------------------------------------
  280. * Power Management
  281. */
  282. #ifdef CONFIG_PM_SLEEP
  283. static int vsp1_pm_suspend(struct device *dev)
  284. {
  285. struct vsp1_device *vsp1 = dev_get_drvdata(dev);
  286. WARN_ON(mutex_is_locked(&vsp1->lock));
  287. if (vsp1->ref_count == 0)
  288. return 0;
  289. clk_disable_unprepare(vsp1->clock);
  290. return 0;
  291. }
  292. static int vsp1_pm_resume(struct device *dev)
  293. {
  294. struct vsp1_device *vsp1 = dev_get_drvdata(dev);
  295. WARN_ON(mutex_is_locked(&vsp1->lock));
  296. if (vsp1->ref_count)
  297. return 0;
  298. return clk_prepare_enable(vsp1->clock);
  299. }
  300. #endif
  301. static const struct dev_pm_ops vsp1_pm_ops = {
  302. SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
  303. };
  304. /* -----------------------------------------------------------------------------
  305. * Platform Driver
  306. */
  307. static struct vsp1_platform_data *
  308. vsp1_get_platform_data(struct platform_device *pdev)
  309. {
  310. struct vsp1_platform_data *pdata = pdev->dev.platform_data;
  311. if (pdata == NULL) {
  312. dev_err(&pdev->dev, "missing platform data\n");
  313. return NULL;
  314. }
  315. if (pdata->rpf_count <= 0 || pdata->rpf_count > VPS1_MAX_RPF) {
  316. dev_err(&pdev->dev, "invalid number of RPF (%u)\n",
  317. pdata->rpf_count);
  318. return NULL;
  319. }
  320. if (pdata->uds_count <= 0 || pdata->uds_count > VPS1_MAX_UDS) {
  321. dev_err(&pdev->dev, "invalid number of UDS (%u)\n",
  322. pdata->uds_count);
  323. return NULL;
  324. }
  325. if (pdata->wpf_count <= 0 || pdata->wpf_count > VPS1_MAX_WPF) {
  326. dev_err(&pdev->dev, "invalid number of WPF (%u)\n",
  327. pdata->wpf_count);
  328. return NULL;
  329. }
  330. return pdata;
  331. }
  332. static int vsp1_probe(struct platform_device *pdev)
  333. {
  334. struct vsp1_device *vsp1;
  335. struct resource *irq;
  336. struct resource *io;
  337. int ret;
  338. vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
  339. if (vsp1 == NULL)
  340. return -ENOMEM;
  341. vsp1->dev = &pdev->dev;
  342. mutex_init(&vsp1->lock);
  343. INIT_LIST_HEAD(&vsp1->entities);
  344. vsp1->pdata = vsp1_get_platform_data(pdev);
  345. if (vsp1->pdata == NULL)
  346. return -ENODEV;
  347. /* I/O, IRQ and clock resources */
  348. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  349. vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
  350. if (IS_ERR(vsp1->mmio))
  351. return PTR_ERR(vsp1->mmio);
  352. vsp1->clock = devm_clk_get(&pdev->dev, NULL);
  353. if (IS_ERR(vsp1->clock)) {
  354. dev_err(&pdev->dev, "failed to get clock\n");
  355. return PTR_ERR(vsp1->clock);
  356. }
  357. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  358. if (!irq) {
  359. dev_err(&pdev->dev, "missing IRQ\n");
  360. return -EINVAL;
  361. }
  362. ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
  363. IRQF_SHARED, dev_name(&pdev->dev), vsp1);
  364. if (ret < 0) {
  365. dev_err(&pdev->dev, "failed to request IRQ\n");
  366. return ret;
  367. }
  368. /* Instanciate entities */
  369. ret = vsp1_create_entities(vsp1);
  370. if (ret < 0) {
  371. dev_err(&pdev->dev, "failed to create entities\n");
  372. return ret;
  373. }
  374. platform_set_drvdata(pdev, vsp1);
  375. return 0;
  376. }
  377. static int vsp1_remove(struct platform_device *pdev)
  378. {
  379. struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
  380. vsp1_destroy_entities(vsp1);
  381. return 0;
  382. }
  383. static struct platform_driver vsp1_platform_driver = {
  384. .probe = vsp1_probe,
  385. .remove = vsp1_remove,
  386. .driver = {
  387. .owner = THIS_MODULE,
  388. .name = "vsp1",
  389. .pm = &vsp1_pm_ops,
  390. },
  391. };
  392. module_platform_driver(vsp1_platform_driver);
  393. MODULE_ALIAS("vsp1");
  394. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  395. MODULE_DESCRIPTION("Renesas VSP1 Driver");
  396. MODULE_LICENSE("GPL");