vsp1_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. static int vsp1_clocks_enable(struct vsp1_device *vsp1)
  237. {
  238. int ret;
  239. ret = clk_prepare_enable(vsp1->clock);
  240. if (ret < 0)
  241. return ret;
  242. if (IS_ERR(vsp1->rt_clock))
  243. return 0;
  244. ret = clk_prepare_enable(vsp1->rt_clock);
  245. if (ret < 0) {
  246. clk_disable_unprepare(vsp1->clock);
  247. return ret;
  248. }
  249. return 0;
  250. }
  251. static void vsp1_clocks_disable(struct vsp1_device *vsp1)
  252. {
  253. if (!IS_ERR(vsp1->rt_clock))
  254. clk_disable_unprepare(vsp1->rt_clock);
  255. clk_disable_unprepare(vsp1->clock);
  256. }
  257. /*
  258. * vsp1_device_get - Acquire the VSP1 device
  259. *
  260. * Increment the VSP1 reference count and initialize the device if the first
  261. * reference is taken.
  262. *
  263. * Return a pointer to the VSP1 device or NULL if an error occured.
  264. */
  265. struct vsp1_device *vsp1_device_get(struct vsp1_device *vsp1)
  266. {
  267. struct vsp1_device *__vsp1 = vsp1;
  268. int ret;
  269. mutex_lock(&vsp1->lock);
  270. if (vsp1->ref_count > 0)
  271. goto done;
  272. ret = vsp1_clocks_enable(vsp1);
  273. if (ret < 0) {
  274. __vsp1 = NULL;
  275. goto done;
  276. }
  277. ret = vsp1_device_init(vsp1);
  278. if (ret < 0) {
  279. vsp1_clocks_disable(vsp1);
  280. __vsp1 = NULL;
  281. goto done;
  282. }
  283. done:
  284. if (__vsp1)
  285. vsp1->ref_count++;
  286. mutex_unlock(&vsp1->lock);
  287. return __vsp1;
  288. }
  289. /*
  290. * vsp1_device_put - Release the VSP1 device
  291. *
  292. * Decrement the VSP1 reference count and cleanup the device if the last
  293. * reference is released.
  294. */
  295. void vsp1_device_put(struct vsp1_device *vsp1)
  296. {
  297. mutex_lock(&vsp1->lock);
  298. if (--vsp1->ref_count == 0)
  299. vsp1_clocks_disable(vsp1);
  300. mutex_unlock(&vsp1->lock);
  301. }
  302. /* -----------------------------------------------------------------------------
  303. * Power Management
  304. */
  305. #ifdef CONFIG_PM_SLEEP
  306. static int vsp1_pm_suspend(struct device *dev)
  307. {
  308. struct vsp1_device *vsp1 = dev_get_drvdata(dev);
  309. WARN_ON(mutex_is_locked(&vsp1->lock));
  310. if (vsp1->ref_count == 0)
  311. return 0;
  312. vsp1_clocks_disable(vsp1);
  313. return 0;
  314. }
  315. static int vsp1_pm_resume(struct device *dev)
  316. {
  317. struct vsp1_device *vsp1 = dev_get_drvdata(dev);
  318. WARN_ON(mutex_is_locked(&vsp1->lock));
  319. if (vsp1->ref_count)
  320. return 0;
  321. return vsp1_clocks_enable(vsp1);
  322. }
  323. #endif
  324. static const struct dev_pm_ops vsp1_pm_ops = {
  325. SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
  326. };
  327. /* -----------------------------------------------------------------------------
  328. * Platform Driver
  329. */
  330. static struct vsp1_platform_data *
  331. vsp1_get_platform_data(struct platform_device *pdev)
  332. {
  333. struct vsp1_platform_data *pdata = pdev->dev.platform_data;
  334. if (pdata == NULL) {
  335. dev_err(&pdev->dev, "missing platform data\n");
  336. return NULL;
  337. }
  338. if (pdata->rpf_count <= 0 || pdata->rpf_count > VPS1_MAX_RPF) {
  339. dev_err(&pdev->dev, "invalid number of RPF (%u)\n",
  340. pdata->rpf_count);
  341. return NULL;
  342. }
  343. if (pdata->uds_count <= 0 || pdata->uds_count > VPS1_MAX_UDS) {
  344. dev_err(&pdev->dev, "invalid number of UDS (%u)\n",
  345. pdata->uds_count);
  346. return NULL;
  347. }
  348. if (pdata->wpf_count <= 0 || pdata->wpf_count > VPS1_MAX_WPF) {
  349. dev_err(&pdev->dev, "invalid number of WPF (%u)\n",
  350. pdata->wpf_count);
  351. return NULL;
  352. }
  353. return pdata;
  354. }
  355. static int vsp1_probe(struct platform_device *pdev)
  356. {
  357. struct vsp1_device *vsp1;
  358. struct resource *irq;
  359. struct resource *io;
  360. int ret;
  361. vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
  362. if (vsp1 == NULL)
  363. return -ENOMEM;
  364. vsp1->dev = &pdev->dev;
  365. mutex_init(&vsp1->lock);
  366. INIT_LIST_HEAD(&vsp1->entities);
  367. vsp1->pdata = vsp1_get_platform_data(pdev);
  368. if (vsp1->pdata == NULL)
  369. return -ENODEV;
  370. /* I/O, IRQ and clock resources */
  371. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  372. vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
  373. if (IS_ERR(vsp1->mmio))
  374. return PTR_ERR(vsp1->mmio);
  375. vsp1->clock = devm_clk_get(&pdev->dev, NULL);
  376. if (IS_ERR(vsp1->clock)) {
  377. dev_err(&pdev->dev, "failed to get clock\n");
  378. return PTR_ERR(vsp1->clock);
  379. }
  380. /* The RT clock is optional */
  381. vsp1->rt_clock = devm_clk_get(&pdev->dev, "rt");
  382. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  383. if (!irq) {
  384. dev_err(&pdev->dev, "missing IRQ\n");
  385. return -EINVAL;
  386. }
  387. ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
  388. IRQF_SHARED, dev_name(&pdev->dev), vsp1);
  389. if (ret < 0) {
  390. dev_err(&pdev->dev, "failed to request IRQ\n");
  391. return ret;
  392. }
  393. /* Instanciate entities */
  394. ret = vsp1_create_entities(vsp1);
  395. if (ret < 0) {
  396. dev_err(&pdev->dev, "failed to create entities\n");
  397. return ret;
  398. }
  399. platform_set_drvdata(pdev, vsp1);
  400. return 0;
  401. }
  402. static int vsp1_remove(struct platform_device *pdev)
  403. {
  404. struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
  405. vsp1_destroy_entities(vsp1);
  406. return 0;
  407. }
  408. static struct platform_driver vsp1_platform_driver = {
  409. .probe = vsp1_probe,
  410. .remove = vsp1_remove,
  411. .driver = {
  412. .owner = THIS_MODULE,
  413. .name = "vsp1",
  414. .pm = &vsp1_pm_ops,
  415. },
  416. };
  417. module_platform_driver(vsp1_platform_driver);
  418. MODULE_ALIAS("vsp1");
  419. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  420. MODULE_DESCRIPTION("Renesas VSP1 Driver");
  421. MODULE_LICENSE("GPL");