vsp1_drv.c 13 KB

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