vsp1_drv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
  114. dev_name(mdev->dev));
  115. ret = media_device_register(mdev);
  116. if (ret < 0) {
  117. dev_err(vsp1->dev, "media device registration failed (%d)\n",
  118. ret);
  119. return ret;
  120. }
  121. vdev->mdev = mdev;
  122. ret = v4l2_device_register(vsp1->dev, vdev);
  123. if (ret < 0) {
  124. dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
  125. ret);
  126. goto done;
  127. }
  128. /* Instantiate all the entities. */
  129. if (vsp1->pdata->features & VSP1_HAS_LIF) {
  130. vsp1->lif = vsp1_lif_create(vsp1);
  131. if (IS_ERR(vsp1->lif)) {
  132. ret = PTR_ERR(vsp1->lif);
  133. goto done;
  134. }
  135. list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
  136. }
  137. for (i = 0; i < vsp1->pdata->rpf_count; ++i) {
  138. struct vsp1_rwpf *rpf;
  139. rpf = vsp1_rpf_create(vsp1, i);
  140. if (IS_ERR(rpf)) {
  141. ret = PTR_ERR(rpf);
  142. goto done;
  143. }
  144. vsp1->rpf[i] = rpf;
  145. list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
  146. }
  147. for (i = 0; i < vsp1->pdata->uds_count; ++i) {
  148. struct vsp1_uds *uds;
  149. uds = vsp1_uds_create(vsp1, i);
  150. if (IS_ERR(uds)) {
  151. ret = PTR_ERR(uds);
  152. goto done;
  153. }
  154. vsp1->uds[i] = uds;
  155. list_add_tail(&uds->entity.list_dev, &vsp1->entities);
  156. }
  157. for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
  158. struct vsp1_rwpf *wpf;
  159. wpf = vsp1_wpf_create(vsp1, i);
  160. if (IS_ERR(wpf)) {
  161. ret = PTR_ERR(wpf);
  162. goto done;
  163. }
  164. vsp1->wpf[i] = wpf;
  165. list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
  166. }
  167. /* Create links. */
  168. list_for_each_entry(entity, &vsp1->entities, list_dev) {
  169. if (entity->type == VSP1_ENTITY_LIF ||
  170. entity->type == VSP1_ENTITY_RPF)
  171. continue;
  172. ret = vsp1_create_links(vsp1, entity);
  173. if (ret < 0)
  174. goto done;
  175. }
  176. if (vsp1->pdata->features & VSP1_HAS_LIF) {
  177. ret = media_entity_create_link(
  178. &vsp1->wpf[0]->entity.subdev.entity, RWPF_PAD_SOURCE,
  179. &vsp1->lif->entity.subdev.entity, LIF_PAD_SINK, 0);
  180. if (ret < 0)
  181. return ret;
  182. }
  183. /* Register all subdevs. */
  184. list_for_each_entry(entity, &vsp1->entities, list_dev) {
  185. ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
  186. &entity->subdev);
  187. if (ret < 0)
  188. goto done;
  189. }
  190. ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
  191. done:
  192. if (ret < 0)
  193. vsp1_destroy_entities(vsp1);
  194. return ret;
  195. }
  196. static int vsp1_device_init(struct vsp1_device *vsp1)
  197. {
  198. unsigned int i;
  199. u32 status;
  200. /* Reset any channel that might be running. */
  201. status = vsp1_read(vsp1, VI6_STATUS);
  202. for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
  203. unsigned int timeout;
  204. if (!(status & VI6_STATUS_SYS_ACT(i)))
  205. continue;
  206. vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(i));
  207. for (timeout = 10; timeout > 0; --timeout) {
  208. status = vsp1_read(vsp1, VI6_STATUS);
  209. if (!(status & VI6_STATUS_SYS_ACT(i)))
  210. break;
  211. usleep_range(1000, 2000);
  212. }
  213. if (!timeout) {
  214. dev_err(vsp1->dev, "failed to reset wpf.%u\n", i);
  215. return -ETIMEDOUT;
  216. }
  217. }
  218. vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
  219. (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
  220. for (i = 0; i < vsp1->pdata->rpf_count; ++i)
  221. vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
  222. for (i = 0; i < vsp1->pdata->uds_count; ++i)
  223. vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
  224. vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
  225. vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
  226. vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
  227. vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
  228. vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
  229. vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
  230. vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
  231. (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
  232. vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
  233. (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
  234. return 0;
  235. }
  236. /*
  237. * vsp1_device_get - Acquire the VSP1 device
  238. *
  239. * Increment the VSP1 reference count and initialize the device if the first
  240. * reference is taken.
  241. *
  242. * Return a pointer to the VSP1 device or NULL if an error occured.
  243. */
  244. struct vsp1_device *vsp1_device_get(struct vsp1_device *vsp1)
  245. {
  246. struct vsp1_device *__vsp1 = vsp1;
  247. int ret;
  248. mutex_lock(&vsp1->lock);
  249. if (vsp1->ref_count > 0)
  250. goto done;
  251. ret = clk_prepare_enable(vsp1->clock);
  252. if (ret < 0) {
  253. __vsp1 = NULL;
  254. goto done;
  255. }
  256. ret = vsp1_device_init(vsp1);
  257. if (ret < 0) {
  258. clk_disable_unprepare(vsp1->clock);
  259. __vsp1 = NULL;
  260. goto done;
  261. }
  262. done:
  263. if (__vsp1)
  264. vsp1->ref_count++;
  265. mutex_unlock(&vsp1->lock);
  266. return __vsp1;
  267. }
  268. /*
  269. * vsp1_device_put - Release the VSP1 device
  270. *
  271. * Decrement the VSP1 reference count and cleanup the device if the last
  272. * reference is released.
  273. */
  274. void vsp1_device_put(struct vsp1_device *vsp1)
  275. {
  276. mutex_lock(&vsp1->lock);
  277. if (--vsp1->ref_count == 0)
  278. clk_disable_unprepare(vsp1->clock);
  279. mutex_unlock(&vsp1->lock);
  280. }
  281. /* -----------------------------------------------------------------------------
  282. * Power Management
  283. */
  284. #ifdef CONFIG_PM_SLEEP
  285. static int vsp1_pm_suspend(struct device *dev)
  286. {
  287. struct vsp1_device *vsp1 = dev_get_drvdata(dev);
  288. WARN_ON(mutex_is_locked(&vsp1->lock));
  289. if (vsp1->ref_count == 0)
  290. return 0;
  291. clk_disable_unprepare(vsp1->clock);
  292. return 0;
  293. }
  294. static int vsp1_pm_resume(struct device *dev)
  295. {
  296. struct vsp1_device *vsp1 = dev_get_drvdata(dev);
  297. WARN_ON(mutex_is_locked(&vsp1->lock));
  298. if (vsp1->ref_count)
  299. return 0;
  300. return clk_prepare_enable(vsp1->clock);
  301. }
  302. #endif
  303. static const struct dev_pm_ops vsp1_pm_ops = {
  304. SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
  305. };
  306. /* -----------------------------------------------------------------------------
  307. * Platform Driver
  308. */
  309. static struct vsp1_platform_data *
  310. vsp1_get_platform_data(struct platform_device *pdev)
  311. {
  312. struct vsp1_platform_data *pdata = pdev->dev.platform_data;
  313. if (pdata == NULL) {
  314. dev_err(&pdev->dev, "missing platform data\n");
  315. return NULL;
  316. }
  317. if (pdata->rpf_count <= 0 || pdata->rpf_count > VPS1_MAX_RPF) {
  318. dev_err(&pdev->dev, "invalid number of RPF (%u)\n",
  319. pdata->rpf_count);
  320. return NULL;
  321. }
  322. if (pdata->uds_count <= 0 || pdata->uds_count > VPS1_MAX_UDS) {
  323. dev_err(&pdev->dev, "invalid number of UDS (%u)\n",
  324. pdata->uds_count);
  325. return NULL;
  326. }
  327. if (pdata->wpf_count <= 0 || pdata->wpf_count > VPS1_MAX_WPF) {
  328. dev_err(&pdev->dev, "invalid number of WPF (%u)\n",
  329. pdata->wpf_count);
  330. return NULL;
  331. }
  332. return pdata;
  333. }
  334. static int vsp1_probe(struct platform_device *pdev)
  335. {
  336. struct vsp1_device *vsp1;
  337. struct resource *irq;
  338. struct resource *io;
  339. int ret;
  340. vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
  341. if (vsp1 == NULL)
  342. return -ENOMEM;
  343. vsp1->dev = &pdev->dev;
  344. mutex_init(&vsp1->lock);
  345. INIT_LIST_HEAD(&vsp1->entities);
  346. vsp1->pdata = vsp1_get_platform_data(pdev);
  347. if (vsp1->pdata == NULL)
  348. return -ENODEV;
  349. /* I/O, IRQ and clock resources */
  350. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  351. vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
  352. if (IS_ERR(vsp1->mmio))
  353. return PTR_ERR(vsp1->mmio);
  354. vsp1->clock = devm_clk_get(&pdev->dev, NULL);
  355. if (IS_ERR(vsp1->clock)) {
  356. dev_err(&pdev->dev, "failed to get clock\n");
  357. return PTR_ERR(vsp1->clock);
  358. }
  359. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  360. if (!irq) {
  361. dev_err(&pdev->dev, "missing IRQ\n");
  362. return -EINVAL;
  363. }
  364. ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
  365. IRQF_SHARED, dev_name(&pdev->dev), vsp1);
  366. if (ret < 0) {
  367. dev_err(&pdev->dev, "failed to request IRQ\n");
  368. return ret;
  369. }
  370. /* Instanciate entities */
  371. ret = vsp1_create_entities(vsp1);
  372. if (ret < 0) {
  373. dev_err(&pdev->dev, "failed to create entities\n");
  374. return ret;
  375. }
  376. platform_set_drvdata(pdev, vsp1);
  377. return 0;
  378. }
  379. static int vsp1_remove(struct platform_device *pdev)
  380. {
  381. struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
  382. vsp1_destroy_entities(vsp1);
  383. return 0;
  384. }
  385. static struct platform_driver vsp1_platform_driver = {
  386. .probe = vsp1_probe,
  387. .remove = vsp1_remove,
  388. .driver = {
  389. .owner = THIS_MODULE,
  390. .name = "vsp1",
  391. .pm = &vsp1_pm_ops,
  392. },
  393. };
  394. module_platform_driver(vsp1_platform_driver);
  395. MODULE_ALIAS("vsp1");
  396. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  397. MODULE_DESCRIPTION("Renesas VSP1 Driver");
  398. MODULE_LICENSE("GPL");