rkisp1-dev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
  2. /*
  3. * Rockchip ISP1 Driver - Base driver
  4. *
  5. * Copyright (C) 2019 Collabora, Ltd.
  6. *
  7. * Based on Rockchip ISP1 driver by Rockchip Electronics Co., Ltd.
  8. * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
  9. */
  10. #include <linux/build_bug.h>
  11. #include <linux/clk.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_graph.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/pm_domain.h>
  20. #include <linux/pm_runtime.h>
  21. #include <media/v4l2-fwnode.h>
  22. #include <media/v4l2-mc.h>
  23. #include "rkisp1-common.h"
  24. #include "rkisp1-csi.h"
  25. /*
  26. * ISP Details
  27. * -----------
  28. *
  29. * ISP Comprises with:
  30. * MIPI serial camera interface
  31. * Image Signal Processing
  32. * Many Image Enhancement Blocks
  33. * Crop
  34. * Resizer
  35. * RBG display ready image
  36. * Image Rotation
  37. *
  38. * ISP Block Diagram
  39. * -----------------
  40. * rkisp1-resizer.c rkisp1-capture.c
  41. * |====================| |=======================|
  42. * rkisp1-isp.c Main Picture Path
  43. * |==========================| |===============================================|
  44. * +-----------+ +--+--+--+--+ +--------+ +--------+ +-----------+
  45. * | | | | | | | | | | | | |
  46. * +--------+ |\ | | | | | | | -->| Crop |->| RSZ |------------->| |
  47. * | MIPI |--->| \ | | | | | | | | | | | | | |
  48. * +--------+ | | | | |IE|IE|IE|IE| | +--------+ +--------+ | Memory |
  49. * |MUX|--->| ISP |->|0 |1 |2 |3 |---+ | Interface |
  50. * +--------+ | | | | | | | | | | +--------+ +--------+ +--------+ | |
  51. * |Parallel|--->| / | | | | | | | | | | | | | | | |
  52. * +--------+ |/ | | | | | | | -->| Crop |->| RSZ |->| RGB |->| |
  53. * | | | | | | | | | | | | Rotate | | |
  54. * +-----------+ +--+--+--+--+ +--------+ +--------+ +--------+ +-----------+
  55. * ^
  56. * +--------+ | |===============================================|
  57. * | DMA |------------------------------------+ Self Picture Path
  58. * +--------+
  59. *
  60. * rkisp1-stats.c rkisp1-params.c
  61. * |===============| |===============|
  62. * +---------------+ +---------------+
  63. * | | | |
  64. * | ISP | | ISP |
  65. * | | | |
  66. * +---------------+ +---------------+
  67. *
  68. *
  69. * Media Topology
  70. * --------------
  71. *
  72. * +----------+ +----------+
  73. * | Sensor 1 | | Sensor X |
  74. * ------------ ... ------------
  75. * | 0 | | 0 |
  76. * +----------+ +----------+
  77. * | |
  78. * \----\ /----/
  79. * | |
  80. * v v
  81. * +-------------+
  82. * | 0 |
  83. * ---------------
  84. * | CSI-2 RX |
  85. * --------------- +-----------+
  86. * | 1 | | params |
  87. * +-------------+ | (output) |
  88. * | +-----------+
  89. * v |
  90. * +------+------+ |
  91. * | 0 | 1 |<---------+
  92. * |------+------|
  93. * | ISP |
  94. * |------+------|
  95. * +-------------| 2 | 3 |----------+
  96. * | +------+------+ |
  97. * | | |
  98. * v v v
  99. * +- ---------+ +-----------+ +-----------+
  100. * | 0 | | 0 | | stats |
  101. * ------------- ------------- | (capture) |
  102. * | Resizer | | Resizer | +-----------+
  103. * ------------| ------------|
  104. * | 1 | | 1 |
  105. * +-----------+ +-----------+
  106. * | |
  107. * v v
  108. * +-----------+ +-----------+
  109. * | selfpath | | mainpath |
  110. * | (capture) | | (capture) |
  111. * +-----------+ +-----------+
  112. */
  113. struct rkisp1_isr_data {
  114. const char *name;
  115. irqreturn_t (*isr)(int irq, void *ctx);
  116. u32 line_mask;
  117. };
  118. /* ----------------------------------------------------------------------------
  119. * Sensor DT bindings
  120. */
  121. static int rkisp1_subdev_notifier_bound(struct v4l2_async_notifier *notifier,
  122. struct v4l2_subdev *sd,
  123. struct v4l2_async_connection *asc)
  124. {
  125. struct rkisp1_device *rkisp1 =
  126. container_of(notifier, struct rkisp1_device, notifier);
  127. struct rkisp1_sensor_async *s_asd =
  128. container_of(asc, struct rkisp1_sensor_async, asd);
  129. int source_pad;
  130. int ret;
  131. s_asd->sd = sd;
  132. source_pad = media_entity_get_fwnode_pad(&sd->entity, s_asd->source_ep,
  133. MEDIA_PAD_FL_SOURCE);
  134. if (source_pad < 0) {
  135. dev_err(rkisp1->dev, "failed to find source pad for %s\n",
  136. sd->name);
  137. return source_pad;
  138. }
  139. if (s_asd->port == 0)
  140. return rkisp1_csi_link_sensor(rkisp1, sd, s_asd, source_pad);
  141. ret = media_create_pad_link(&sd->entity, source_pad,
  142. &rkisp1->isp.sd.entity,
  143. RKISP1_ISP_PAD_SINK_VIDEO,
  144. !s_asd->index ? MEDIA_LNK_FL_ENABLED : 0);
  145. if (ret) {
  146. dev_err(rkisp1->dev, "failed to link source pad of %s\n",
  147. sd->name);
  148. return ret;
  149. }
  150. return 0;
  151. }
  152. static int rkisp1_subdev_notifier_complete(struct v4l2_async_notifier *notifier)
  153. {
  154. struct rkisp1_device *rkisp1 =
  155. container_of(notifier, struct rkisp1_device, notifier);
  156. return v4l2_device_register_subdev_nodes(&rkisp1->v4l2_dev);
  157. }
  158. static void rkisp1_subdev_notifier_destroy(struct v4l2_async_connection *asc)
  159. {
  160. struct rkisp1_sensor_async *rk_asd =
  161. container_of(asc, struct rkisp1_sensor_async, asd);
  162. fwnode_handle_put(rk_asd->source_ep);
  163. }
  164. static const struct v4l2_async_notifier_operations rkisp1_subdev_notifier_ops = {
  165. .bound = rkisp1_subdev_notifier_bound,
  166. .complete = rkisp1_subdev_notifier_complete,
  167. .destroy = rkisp1_subdev_notifier_destroy,
  168. };
  169. static int rkisp1_subdev_notifier_register(struct rkisp1_device *rkisp1)
  170. {
  171. struct v4l2_async_notifier *ntf = &rkisp1->notifier;
  172. struct fwnode_handle *fwnode = dev_fwnode(rkisp1->dev);
  173. struct fwnode_handle *ep;
  174. unsigned int index = 0;
  175. int ret = 0;
  176. v4l2_async_nf_init(ntf, &rkisp1->v4l2_dev);
  177. ntf->ops = &rkisp1_subdev_notifier_ops;
  178. fwnode_graph_for_each_endpoint(fwnode, ep) {
  179. struct fwnode_handle *port;
  180. struct v4l2_fwnode_endpoint vep = { };
  181. struct rkisp1_sensor_async *rk_asd;
  182. struct fwnode_handle *source;
  183. u32 reg = 0;
  184. /* Select the bus type based on the port. */
  185. port = fwnode_get_parent(ep);
  186. fwnode_property_read_u32(port, "reg", &reg);
  187. fwnode_handle_put(port);
  188. switch (reg) {
  189. case 0:
  190. /* MIPI CSI-2 port */
  191. if (!rkisp1_has_feature(rkisp1, MIPI_CSI2)) {
  192. dev_err(rkisp1->dev,
  193. "internal CSI must be available for port 0\n");
  194. ret = -EINVAL;
  195. break;
  196. }
  197. vep.bus_type = V4L2_MBUS_CSI2_DPHY;
  198. break;
  199. case 1:
  200. /*
  201. * Parallel port. The bus-type property in DT is
  202. * mandatory for port 1, it will be used to determine if
  203. * it's PARALLEL or BT656.
  204. */
  205. vep.bus_type = V4L2_MBUS_UNKNOWN;
  206. break;
  207. }
  208. if (ret)
  209. break;
  210. /* Parse the endpoint and validate the bus type. */
  211. ret = v4l2_fwnode_endpoint_parse(ep, &vep);
  212. if (ret) {
  213. dev_err(rkisp1->dev, "failed to parse endpoint %pfw\n",
  214. ep);
  215. break;
  216. }
  217. if (vep.base.port == 1) {
  218. if (vep.bus_type != V4L2_MBUS_PARALLEL &&
  219. vep.bus_type != V4L2_MBUS_BT656) {
  220. dev_err(rkisp1->dev,
  221. "port 1 must be parallel or BT656\n");
  222. ret = -EINVAL;
  223. break;
  224. }
  225. }
  226. /* Add the async subdev to the notifier. */
  227. source = fwnode_graph_get_remote_endpoint(ep);
  228. if (!source) {
  229. dev_err(rkisp1->dev,
  230. "endpoint %pfw has no remote endpoint\n",
  231. ep);
  232. ret = -ENODEV;
  233. break;
  234. }
  235. rk_asd = v4l2_async_nf_add_fwnode(ntf, source,
  236. struct rkisp1_sensor_async);
  237. if (IS_ERR(rk_asd)) {
  238. fwnode_handle_put(source);
  239. ret = PTR_ERR(rk_asd);
  240. break;
  241. }
  242. rk_asd->index = index++;
  243. rk_asd->source_ep = source;
  244. rk_asd->mbus_type = vep.bus_type;
  245. rk_asd->port = vep.base.port;
  246. if (vep.bus_type == V4L2_MBUS_CSI2_DPHY) {
  247. rk_asd->mbus_flags = vep.bus.mipi_csi2.flags;
  248. rk_asd->lanes = vep.bus.mipi_csi2.num_data_lanes;
  249. } else {
  250. rk_asd->mbus_flags = vep.bus.parallel.flags;
  251. }
  252. dev_dbg(rkisp1->dev, "registered ep id %d, bus type %u, %u lanes\n",
  253. vep.base.id, rk_asd->mbus_type, rk_asd->lanes);
  254. }
  255. if (ret) {
  256. fwnode_handle_put(ep);
  257. v4l2_async_nf_cleanup(ntf);
  258. return ret;
  259. }
  260. if (!index)
  261. dev_dbg(rkisp1->dev, "no remote subdevice found\n");
  262. ret = v4l2_async_nf_register(ntf);
  263. if (ret) {
  264. v4l2_async_nf_cleanup(ntf);
  265. return ret;
  266. }
  267. return 0;
  268. }
  269. /* ----------------------------------------------------------------------------
  270. * Power
  271. */
  272. static int __maybe_unused rkisp1_runtime_suspend(struct device *dev)
  273. {
  274. struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
  275. rkisp1->irqs_enabled = false;
  276. /* Make sure the IRQ handler will see the above */
  277. mb();
  278. /*
  279. * Wait until any running IRQ handler has returned. The IRQ handler
  280. * may get called even after this (as it's a shared interrupt line)
  281. * but the 'irqs_enabled' flag will make the handler return immediately.
  282. */
  283. for (unsigned int il = 0; il < ARRAY_SIZE(rkisp1->irqs); ++il) {
  284. if (rkisp1->irqs[il] == -1)
  285. continue;
  286. /* Skip if the irq line is the same as previous */
  287. if (il == 0 || rkisp1->irqs[il - 1] != rkisp1->irqs[il])
  288. synchronize_irq(rkisp1->irqs[il]);
  289. }
  290. clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks);
  291. return pinctrl_pm_select_sleep_state(dev);
  292. }
  293. static int __maybe_unused rkisp1_runtime_resume(struct device *dev)
  294. {
  295. struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
  296. int ret;
  297. ret = pinctrl_pm_select_default_state(dev);
  298. if (ret)
  299. return ret;
  300. ret = clk_bulk_prepare_enable(rkisp1->clk_size, rkisp1->clks);
  301. if (ret)
  302. return ret;
  303. rkisp1->irqs_enabled = true;
  304. /* Make sure the IRQ handler will see the above */
  305. mb();
  306. return 0;
  307. }
  308. static const struct dev_pm_ops rkisp1_pm_ops = {
  309. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  310. pm_runtime_force_resume)
  311. SET_RUNTIME_PM_OPS(rkisp1_runtime_suspend, rkisp1_runtime_resume, NULL)
  312. };
  313. /* ----------------------------------------------------------------------------
  314. * Core
  315. */
  316. static int rkisp1_create_links(struct rkisp1_device *rkisp1)
  317. {
  318. unsigned int dev_count = rkisp1_path_count(rkisp1);
  319. unsigned int i;
  320. int ret;
  321. if (rkisp1_has_feature(rkisp1, MIPI_CSI2)) {
  322. /* Link the CSI receiver to the ISP. */
  323. ret = media_create_pad_link(&rkisp1->csi.sd.entity,
  324. RKISP1_CSI_PAD_SRC,
  325. &rkisp1->isp.sd.entity,
  326. RKISP1_ISP_PAD_SINK_VIDEO,
  327. MEDIA_LNK_FL_ENABLED);
  328. if (ret)
  329. return ret;
  330. }
  331. /* create ISP->RSZ->CAP links */
  332. for (i = 0; i < dev_count; i++) {
  333. struct media_entity *resizer =
  334. &rkisp1->resizer_devs[i].sd.entity;
  335. struct media_entity *capture =
  336. &rkisp1->capture_devs[i].vnode.vdev.entity;
  337. ret = media_create_pad_link(&rkisp1->isp.sd.entity,
  338. RKISP1_ISP_PAD_SOURCE_VIDEO,
  339. resizer, RKISP1_RSZ_PAD_SINK,
  340. MEDIA_LNK_FL_ENABLED);
  341. if (ret)
  342. return ret;
  343. ret = media_create_pad_link(resizer, RKISP1_RSZ_PAD_SRC,
  344. capture, 0,
  345. MEDIA_LNK_FL_ENABLED |
  346. MEDIA_LNK_FL_IMMUTABLE);
  347. if (ret)
  348. return ret;
  349. }
  350. /* params links */
  351. ret = media_create_pad_link(&rkisp1->params.vnode.vdev.entity, 0,
  352. &rkisp1->isp.sd.entity,
  353. RKISP1_ISP_PAD_SINK_PARAMS,
  354. MEDIA_LNK_FL_ENABLED |
  355. MEDIA_LNK_FL_IMMUTABLE);
  356. if (ret)
  357. return ret;
  358. /* 3A stats links */
  359. return media_create_pad_link(&rkisp1->isp.sd.entity,
  360. RKISP1_ISP_PAD_SOURCE_STATS,
  361. &rkisp1->stats.vnode.vdev.entity, 0,
  362. MEDIA_LNK_FL_ENABLED |
  363. MEDIA_LNK_FL_IMMUTABLE);
  364. }
  365. static void rkisp1_entities_unregister(struct rkisp1_device *rkisp1)
  366. {
  367. if (rkisp1_has_feature(rkisp1, MIPI_CSI2))
  368. rkisp1_csi_unregister(rkisp1);
  369. rkisp1_params_unregister(rkisp1);
  370. rkisp1_stats_unregister(rkisp1);
  371. rkisp1_capture_devs_unregister(rkisp1);
  372. rkisp1_resizer_devs_unregister(rkisp1);
  373. rkisp1_isp_unregister(rkisp1);
  374. }
  375. static int rkisp1_entities_register(struct rkisp1_device *rkisp1)
  376. {
  377. int ret;
  378. ret = rkisp1_isp_register(rkisp1);
  379. if (ret)
  380. goto error;
  381. ret = rkisp1_resizer_devs_register(rkisp1);
  382. if (ret)
  383. goto error;
  384. ret = rkisp1_capture_devs_register(rkisp1);
  385. if (ret)
  386. goto error;
  387. ret = rkisp1_stats_register(rkisp1);
  388. if (ret)
  389. goto error;
  390. ret = rkisp1_params_register(rkisp1);
  391. if (ret)
  392. goto error;
  393. if (rkisp1_has_feature(rkisp1, MIPI_CSI2)) {
  394. ret = rkisp1_csi_register(rkisp1);
  395. if (ret)
  396. goto error;
  397. }
  398. ret = rkisp1_create_links(rkisp1);
  399. if (ret)
  400. goto error;
  401. return 0;
  402. error:
  403. rkisp1_entities_unregister(rkisp1);
  404. return ret;
  405. }
  406. static irqreturn_t rkisp1_isr(int irq, void *ctx)
  407. {
  408. irqreturn_t ret = IRQ_NONE;
  409. /*
  410. * Call rkisp1_capture_isr() first to handle the frame that
  411. * potentially completed using the current frame_sequence number before
  412. * it is potentially incremented by rkisp1_isp_isr() in the vertical
  413. * sync.
  414. */
  415. if (rkisp1_capture_isr(irq, ctx) == IRQ_HANDLED)
  416. ret = IRQ_HANDLED;
  417. if (rkisp1_isp_isr(irq, ctx) == IRQ_HANDLED)
  418. ret = IRQ_HANDLED;
  419. if (rkisp1_csi_isr(irq, ctx) == IRQ_HANDLED)
  420. ret = IRQ_HANDLED;
  421. return ret;
  422. }
  423. static const struct rkisp1_isr_data px30_isp_isrs[] = {
  424. { "isp", rkisp1_isp_isr, BIT(RKISP1_IRQ_ISP) },
  425. { "mi", rkisp1_capture_isr, BIT(RKISP1_IRQ_MI) },
  426. { "mipi", rkisp1_csi_isr, BIT(RKISP1_IRQ_MIPI) },
  427. };
  428. static const struct rkisp1_info px30_isp_info = {
  429. .num_clocks = 4,
  430. .isrs = px30_isp_isrs,
  431. .isr_size = ARRAY_SIZE(px30_isp_isrs),
  432. .isp_ver = RKISP1_V12,
  433. .features = RKISP1_FEATURE_MIPI_CSI2
  434. | RKISP1_FEATURE_SELF_PATH
  435. | RKISP1_FEATURE_DUAL_CROP
  436. | RKISP1_FEATURE_BLS,
  437. .max_width = 3264,
  438. .max_height = 2448,
  439. };
  440. static const struct rkisp1_isr_data rk3399_isp_isrs[] = {
  441. { NULL, rkisp1_isr, BIT(RKISP1_IRQ_ISP) | BIT(RKISP1_IRQ_MI) | BIT(RKISP1_IRQ_MIPI) },
  442. };
  443. static const struct rkisp1_info rk3399_isp_info = {
  444. .num_clocks = 3,
  445. .isrs = rk3399_isp_isrs,
  446. .isr_size = ARRAY_SIZE(rk3399_isp_isrs),
  447. .isp_ver = RKISP1_V10,
  448. .features = RKISP1_FEATURE_MIPI_CSI2
  449. | RKISP1_FEATURE_SELF_PATH
  450. | RKISP1_FEATURE_DUAL_CROP
  451. | RKISP1_FEATURE_BLS,
  452. .max_width = 4416,
  453. .max_height = 3312,
  454. };
  455. static const struct rkisp1_isr_data imx8mp_isp_isrs[] = {
  456. { NULL, rkisp1_isr, BIT(RKISP1_IRQ_ISP) | BIT(RKISP1_IRQ_MI) },
  457. };
  458. static const char * const imx8mp_isp_pm_domains[] = {
  459. "isp",
  460. "csi2",
  461. };
  462. static const struct rkisp1_info imx8mp_isp_info = {
  463. .num_clocks = 3,
  464. .isrs = imx8mp_isp_isrs,
  465. .isr_size = ARRAY_SIZE(imx8mp_isp_isrs),
  466. .isp_ver = RKISP1_V_IMX8MP,
  467. .features = RKISP1_FEATURE_MAIN_STRIDE
  468. | RKISP1_FEATURE_DMA_34BIT
  469. | RKISP1_FEATURE_COMPAND,
  470. .max_width = 4096,
  471. .max_height = 3072,
  472. .pm_domains = {
  473. .names = imx8mp_isp_pm_domains,
  474. .count = ARRAY_SIZE(imx8mp_isp_pm_domains),
  475. },
  476. };
  477. static const struct of_device_id rkisp1_of_match[] = {
  478. {
  479. .compatible = "rockchip,px30-cif-isp",
  480. .data = &px30_isp_info,
  481. },
  482. {
  483. .compatible = "rockchip,rk3399-cif-isp",
  484. .data = &rk3399_isp_info,
  485. },
  486. {
  487. .compatible = "fsl,imx8mp-isp",
  488. .data = &imx8mp_isp_info,
  489. },
  490. {},
  491. };
  492. MODULE_DEVICE_TABLE(of, rkisp1_of_match);
  493. static const char * const rkisp1_clk_names[] = {
  494. "isp",
  495. "aclk",
  496. "hclk",
  497. "pclk",
  498. };
  499. static int rkisp1_init_clocks(struct rkisp1_device *rkisp1)
  500. {
  501. const struct rkisp1_info *info = rkisp1->info;
  502. unsigned int i;
  503. int ret;
  504. static_assert(ARRAY_SIZE(rkisp1_clk_names) == ARRAY_SIZE(rkisp1->clks));
  505. for (i = 0; i < info->num_clocks; i++)
  506. rkisp1->clks[i].id = rkisp1_clk_names[i];
  507. ret = devm_clk_bulk_get(rkisp1->dev, info->num_clocks, rkisp1->clks);
  508. if (ret)
  509. return ret;
  510. rkisp1->clk_size = info->num_clocks;
  511. /*
  512. * On i.MX8MP the pclk clock is needed to access the HDR stitching
  513. * registers, but wasn't required by DT bindings. Try to acquire it as
  514. * an optional clock to avoid breaking backward compatibility.
  515. */
  516. if (info->isp_ver == RKISP1_V_IMX8MP) {
  517. struct clk *clk;
  518. clk = devm_clk_get_optional(rkisp1->dev, "pclk");
  519. if (IS_ERR(clk))
  520. return dev_err_probe(rkisp1->dev, PTR_ERR(clk),
  521. "Failed to acquire pclk clock\n");
  522. if (clk)
  523. rkisp1->clks[rkisp1->clk_size++].clk = clk;
  524. }
  525. return 0;
  526. }
  527. static int rkisp1_init_pm_domains(struct rkisp1_device *rkisp1)
  528. {
  529. const struct rkisp1_info *info = rkisp1->info;
  530. struct dev_pm_domain_attach_data pm_domain_data = {
  531. .pd_names = info->pm_domains.names,
  532. .num_pd_names = info->pm_domains.count,
  533. };
  534. int ret;
  535. /*
  536. * Most platforms have a single power domain, which the PM domain core
  537. * automatically attaches at probe time. When that's the case there's
  538. * nothing to do here.
  539. */
  540. if (rkisp1->dev->pm_domain)
  541. return 0;
  542. if (!pm_domain_data.num_pd_names)
  543. return 0;
  544. ret = devm_pm_domain_attach_list(rkisp1->dev, &pm_domain_data,
  545. &rkisp1->pm_domains);
  546. if (ret < 0) {
  547. dev_err_probe(rkisp1->dev, ret,
  548. "Failed to attach power domains\n");
  549. return ret;
  550. }
  551. return 0;
  552. }
  553. static int rkisp1_probe(struct platform_device *pdev)
  554. {
  555. const struct rkisp1_info *info;
  556. struct device *dev = &pdev->dev;
  557. struct rkisp1_device *rkisp1;
  558. struct v4l2_device *v4l2_dev;
  559. unsigned int i;
  560. u64 dma_mask;
  561. int ret, irq;
  562. u32 cif_id;
  563. rkisp1 = devm_kzalloc(dev, sizeof(*rkisp1), GFP_KERNEL);
  564. if (!rkisp1)
  565. return -ENOMEM;
  566. info = of_device_get_match_data(dev);
  567. rkisp1->info = info;
  568. dev_set_drvdata(dev, rkisp1);
  569. rkisp1->dev = dev;
  570. dma_mask = rkisp1_has_feature(rkisp1, DMA_34BIT) ? DMA_BIT_MASK(34) :
  571. DMA_BIT_MASK(32);
  572. ret = dma_set_mask_and_coherent(dev, dma_mask);
  573. if (ret)
  574. return ret;
  575. mutex_init(&rkisp1->stream_lock);
  576. rkisp1->base_addr = devm_platform_ioremap_resource(pdev, 0);
  577. if (IS_ERR(rkisp1->base_addr))
  578. return PTR_ERR(rkisp1->base_addr);
  579. for (unsigned int il = 0; il < ARRAY_SIZE(rkisp1->irqs); ++il)
  580. rkisp1->irqs[il] = -1;
  581. for (i = 0; i < info->isr_size; i++) {
  582. irq = info->isrs[i].name
  583. ? platform_get_irq_byname(pdev, info->isrs[i].name)
  584. : platform_get_irq(pdev, i);
  585. if (irq < 0)
  586. return irq;
  587. for (unsigned int il = 0; il < ARRAY_SIZE(rkisp1->irqs); ++il) {
  588. if (info->isrs[i].line_mask & BIT(il))
  589. rkisp1->irqs[il] = irq;
  590. }
  591. ret = devm_request_irq(dev, irq, info->isrs[i].isr, IRQF_SHARED,
  592. dev_driver_string(dev), dev);
  593. if (ret) {
  594. dev_err(dev, "request irq failed: %d\n", ret);
  595. return ret;
  596. }
  597. }
  598. ret = rkisp1_init_clocks(rkisp1);
  599. if (ret)
  600. return ret;
  601. ret = rkisp1_init_pm_domains(rkisp1);
  602. if (ret)
  603. return ret;
  604. if (info->isp_ver == RKISP1_V_IMX8MP) {
  605. unsigned int id;
  606. rkisp1->gasket = syscon_regmap_lookup_by_phandle_args(dev->of_node,
  607. "fsl,blk-ctrl",
  608. 1, &id);
  609. if (IS_ERR(rkisp1->gasket)) {
  610. ret = PTR_ERR(rkisp1->gasket);
  611. dev_err(dev, "failed to get gasket: %d\n", ret);
  612. return ret;
  613. }
  614. rkisp1->gasket_id = id;
  615. }
  616. pm_runtime_enable(&pdev->dev);
  617. ret = pm_runtime_resume_and_get(&pdev->dev);
  618. if (ret)
  619. goto err_pm_runtime_disable;
  620. cif_id = rkisp1_read(rkisp1, RKISP1_CIF_VI_ID);
  621. dev_dbg(rkisp1->dev, "CIF_ID 0x%08x\n", cif_id);
  622. pm_runtime_put(&pdev->dev);
  623. rkisp1->media_dev.hw_revision = info->isp_ver;
  624. strscpy(rkisp1->media_dev.model, RKISP1_DRIVER_NAME,
  625. sizeof(rkisp1->media_dev.model));
  626. rkisp1->media_dev.dev = &pdev->dev;
  627. strscpy(rkisp1->media_dev.bus_info, RKISP1_BUS_INFO,
  628. sizeof(rkisp1->media_dev.bus_info));
  629. media_device_init(&rkisp1->media_dev);
  630. v4l2_dev = &rkisp1->v4l2_dev;
  631. v4l2_dev->mdev = &rkisp1->media_dev;
  632. strscpy(v4l2_dev->name, RKISP1_DRIVER_NAME, sizeof(v4l2_dev->name));
  633. ret = v4l2_device_register(rkisp1->dev, &rkisp1->v4l2_dev);
  634. if (ret)
  635. goto err_media_dev_cleanup;
  636. ret = media_device_register(&rkisp1->media_dev);
  637. if (ret) {
  638. dev_err(dev, "Failed to register media device: %d\n", ret);
  639. goto err_unreg_v4l2_dev;
  640. }
  641. if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2) {
  642. ret = rkisp1_csi_init(rkisp1);
  643. if (ret)
  644. goto err_unreg_media_dev;
  645. }
  646. ret = rkisp1_entities_register(rkisp1);
  647. if (ret)
  648. goto err_cleanup_csi;
  649. ret = rkisp1_subdev_notifier_register(rkisp1);
  650. if (ret)
  651. goto err_unreg_entities;
  652. rkisp1_debug_init(rkisp1);
  653. return 0;
  654. err_unreg_entities:
  655. rkisp1_entities_unregister(rkisp1);
  656. err_cleanup_csi:
  657. if (rkisp1_has_feature(rkisp1, MIPI_CSI2))
  658. rkisp1_csi_cleanup(rkisp1);
  659. err_unreg_media_dev:
  660. media_device_unregister(&rkisp1->media_dev);
  661. err_unreg_v4l2_dev:
  662. v4l2_device_unregister(&rkisp1->v4l2_dev);
  663. err_media_dev_cleanup:
  664. media_device_cleanup(&rkisp1->media_dev);
  665. err_pm_runtime_disable:
  666. pm_runtime_disable(&pdev->dev);
  667. return ret;
  668. }
  669. static void rkisp1_remove(struct platform_device *pdev)
  670. {
  671. struct rkisp1_device *rkisp1 = platform_get_drvdata(pdev);
  672. v4l2_async_nf_unregister(&rkisp1->notifier);
  673. v4l2_async_nf_cleanup(&rkisp1->notifier);
  674. rkisp1_entities_unregister(rkisp1);
  675. if (rkisp1_has_feature(rkisp1, MIPI_CSI2))
  676. rkisp1_csi_cleanup(rkisp1);
  677. rkisp1_debug_cleanup(rkisp1);
  678. media_device_unregister(&rkisp1->media_dev);
  679. v4l2_device_unregister(&rkisp1->v4l2_dev);
  680. media_device_cleanup(&rkisp1->media_dev);
  681. pm_runtime_disable(&pdev->dev);
  682. }
  683. static struct platform_driver rkisp1_drv = {
  684. .driver = {
  685. .name = RKISP1_DRIVER_NAME,
  686. .of_match_table = of_match_ptr(rkisp1_of_match),
  687. .pm = &rkisp1_pm_ops,
  688. },
  689. .probe = rkisp1_probe,
  690. .remove = rkisp1_remove,
  691. };
  692. module_platform_driver(rkisp1_drv);
  693. MODULE_DESCRIPTION("Rockchip ISP1 platform driver");
  694. MODULE_LICENSE("Dual MIT/GPL");