isp_driver.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2020 VeriSilicon Holdings Co., Ltd.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. *****************************************************************************
  26. *
  27. * The GPL License (GPL)
  28. *
  29. * Copyright (c) 2020 VeriSilicon Holdings Co., Ltd.
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version 2
  34. * of the License, or (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program;
  43. *
  44. *****************************************************************************
  45. *
  46. * Note: This software is released under dual MIT and GPL licenses. A
  47. * recipient may use this file under the terms of either the MIT license or
  48. * GPL License. If you wish to use only one license not the other, you can
  49. * indicate your decision by deleting one of the above license notices in your
  50. * version of this file.
  51. *
  52. *****************************************************************************/
  53. #include <linux/delay.h>
  54. #include <linux/clk.h>
  55. #include <linux/clk/clk-conf.h>
  56. #include <linux/dma-mapping.h>
  57. #include <linux/io.h>
  58. #include <linux/module.h>
  59. #include <linux/proc_fs.h>
  60. #include <linux/debugfs.h>
  61. #include <linux/videodev2.h>
  62. #include <linux/pm_runtime.h>
  63. #include <linux/of_device.h>
  64. #include <linux/sched_clock.h>
  65. #include <media/v4l2-ctrls.h>
  66. #include <media/v4l2-subdev.h>
  67. #include <media/v4l2-device.h>
  68. #include <media/v4l2-ioctl.h>
  69. #include <media/v4l2-event.h>
  70. #include <media/media-entity.h>
  71. #include <uapi/linux/media-bus-format.h>
  72. #include <linux/mfd/syscon.h>
  73. #include <linux/regmap.h>
  74. #include <linux/of_reserved_mem.h>
  75. #include "isp_driver.h"
  76. #include "isp_ioctl.h"
  77. #include "mrv_all_bits.h"
  78. #include "viv_video_kevent.h"
  79. struct clk *clk_isp;
  80. extern MrvAllRegister_t *all_regs;
  81. #ifdef CONFIG_COMPAT
  82. static long isp_ioctl_compat(struct v4l2_subdev *sd,
  83. unsigned int cmd, void *arg)
  84. {
  85. struct isp_device *isp_dev = v4l2_get_subdevdata(sd);
  86. return isp_priv_ioctl(&isp_dev->ic_dev, cmd, arg);
  87. }
  88. long isp_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  89. {
  90. return isp_ioctl_compat(sd, cmd, arg);
  91. }
  92. #else /* CONFIG_COMPAT */
  93. long isp_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  94. {
  95. struct isp_device *isp_dev = v4l2_get_subdevdata(sd);
  96. return isp_priv_ioctl(&isp_dev->ic_dev, cmd, arg);
  97. }
  98. #endif /* CONFIG_COMPAT */
  99. static int isp_enable_clocks(struct isp_device *isp_dev)
  100. {
  101. int ret;
  102. ret = clk_prepare_enable(isp_dev->clk_core);
  103. if (ret)
  104. return ret;
  105. ret = clk_prepare_enable(isp_dev->clk_axi);
  106. if (ret)
  107. goto disable_clk_core;
  108. ret = clk_prepare_enable(isp_dev->clk_ahb);
  109. if (ret)
  110. goto disable_clk_axi;
  111. return 0;
  112. disable_clk_axi:
  113. clk_disable_unprepare(isp_dev->clk_axi);
  114. disable_clk_core:
  115. clk_disable_unprepare(isp_dev->clk_core);
  116. return ret;
  117. }
  118. static void isp_disable_clocks(struct isp_device *isp_dev)
  119. {
  120. clk_disable_unprepare(isp_dev->clk_ahb);
  121. clk_disable_unprepare(isp_dev->clk_axi);
  122. clk_disable_unprepare(isp_dev->clk_core);
  123. }
  124. int isp_set_stream(struct v4l2_subdev *sd, int enable)
  125. {
  126. return 0;
  127. }
  128. static void isp_post_event(struct isp_ic_dev *dev, void *data, size_t size)
  129. {
  130. struct isp_device *isp_dev;
  131. struct video_device *vdev;
  132. struct v4l2_event event;
  133. if (!dev || !data || !size)
  134. return;
  135. isp_dev = container_of(dev, struct isp_device, ic_dev);
  136. vdev = isp_dev->sd.devnode;
  137. if (!vdev)
  138. return;
  139. memset(&event, 0, sizeof(event));
  140. memcpy(event.u.data, data, min_t(size_t, size, 64));
  141. event.type = VIV_VIDEO_ISPIRQ_TYPE;
  142. v4l2_event_queue(vdev, &event);
  143. }
  144. static int isp_subdev_subscribe_event(struct v4l2_subdev *sd,
  145. struct v4l2_fh *fh, struct v4l2_event_subscription *sub)
  146. {
  147. struct isp_device *isp_dev = v4l2_get_subdevdata(sd);
  148. if (sub->type != VIV_VIDEO_ISPIRQ_TYPE)
  149. return -EINVAL;
  150. if (!isp_dev->ic_dev.post_event)
  151. isp_dev->ic_dev.post_event = isp_post_event;
  152. return v4l2_event_subscribe(fh, sub, 8, NULL);
  153. }
  154. static int isp_subdev_unsubscribe_event(struct v4l2_subdev *sd,
  155. struct v4l2_fh *fh, struct v4l2_event_subscription *sub)
  156. {
  157. struct isp_device *isp_dev = v4l2_get_subdevdata(sd);
  158. if (sub->type != VIV_VIDEO_ISPIRQ_TYPE)
  159. return -EINVAL;
  160. if (isp_dev->ic_dev.post_event)
  161. isp_dev->ic_dev.post_event = NULL;
  162. return v4l2_event_unsubscribe(fh, sub);
  163. }
  164. static struct v4l2_subdev_core_ops isp_v4l2_subdev_core_ops = {
  165. .ioctl = isp_ioctl,
  166. .subscribe_event = isp_subdev_subscribe_event,
  167. .unsubscribe_event = isp_subdev_unsubscribe_event,
  168. };
  169. static struct v4l2_subdev_video_ops isp_v4l2_subdev_video_ops = {
  170. .s_stream = isp_set_stream,
  171. };
  172. irqreturn_t isp_hw_isr_reg_update(int irq, void *data)
  173. {
  174. u32 isp_mis, isp_ctrl;
  175. struct isp_irq_data irq_data;
  176. struct isp_ic_dev *dev = (struct isp_ic_dev *)data;
  177. if (!dev)
  178. return IRQ_HANDLED;
  179. isp_mis = isp_read_reg(dev, REG_ADDR(isp_mis));
  180. isp_write_reg(dev, REG_ADDR(isp_icr), isp_mis);
  181. if (isp_mis) {
  182. if (isp_mis & MRV_ISP_MIS_FRAME_MASK) {
  183. if (dev->isp_update_flag & ISP_FLT_UPDATE) {
  184. isp_s_flt(dev);
  185. dev->isp_update_flag &= (~ISP_FLT_UPDATE);
  186. }
  187. if (dev->gamma_out.changed) {
  188. isp_s_gamma_out(dev);
  189. }
  190. if(dev->update_gamma_en) {
  191. isp_ctrl = isp_read_reg(dev, REG_ADDR(isp_ctrl));
  192. REG_SET_SLICE(isp_ctrl, MRV_ISP_ISP_GAMMA_OUT_ENABLE,
  193. dev->gamma_out.enableGamma);
  194. isp_write_reg(dev, REG_ADDR(isp_ctrl), isp_ctrl);
  195. dev->update_gamma_en = false;
  196. }
  197. }
  198. memset(&irq_data, 0, sizeof(irq_data));
  199. irq_data.val = isp_mis;
  200. if (dev->post_event)
  201. dev->post_event(dev, &irq_data, sizeof(irq_data));
  202. }
  203. return IRQ_HANDLED;
  204. }
  205. struct v4l2_subdev_ops isp_v4l2_subdev_ops = {
  206. .core = &isp_v4l2_subdev_core_ops,
  207. .video = &isp_v4l2_subdev_video_ops,
  208. };
  209. static int isp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  210. {
  211. pm_runtime_get_sync(sd->dev);
  212. return 0;
  213. }
  214. static int isp_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  215. {
  216. pm_runtime_put_sync(sd->dev);
  217. return 0;
  218. }
  219. static struct v4l2_subdev_internal_ops isp_internal_ops = {
  220. .open = isp_open,
  221. .close = isp_close,
  222. };
  223. int isp_hw_probe(struct platform_device *pdev)
  224. {
  225. struct device *dev = &pdev->dev;
  226. struct isp_device *isp_dev;
  227. struct resource *mem_res;
  228. int irq;
  229. int rc;
  230. struct device_node *mem_node;
  231. pr_info("enter %s\n", __func__);
  232. isp_dev = kzalloc(sizeof(struct isp_device), GFP_KERNEL);
  233. if (!isp_dev) {
  234. goto end;
  235. }
  236. rc = fwnode_property_read_u32(of_fwnode_handle(pdev->dev.of_node),
  237. "id", &isp_dev->id);
  238. if (rc) {
  239. pr_info("isp device id not found, use the default.\n");
  240. isp_dev->id = 0;
  241. }
  242. isp_dev->ic_dev.id = isp_dev->id;
  243. isp_dev->clk_core = devm_clk_get(dev, "core");
  244. if (IS_ERR(isp_dev->clk_core)) {
  245. rc = PTR_ERR(isp_dev->clk_core);
  246. dev_err(dev, "can't get core clock: %d\n", rc);
  247. return rc;
  248. }
  249. isp_dev->clk_axi = devm_clk_get(dev, "axi");
  250. if (IS_ERR(isp_dev->clk_axi)) {
  251. rc = PTR_ERR(isp_dev->clk_axi);
  252. dev_err(dev, "can't get axi clock: %d\n", rc);
  253. return rc;
  254. }
  255. isp_dev->clk_ahb = devm_clk_get(dev, "ahb");
  256. if (IS_ERR(isp_dev->clk_ahb)) {
  257. rc = PTR_ERR(isp_dev->clk_ahb);
  258. dev_err(dev, "can't get ahb clock: %d\n", rc);
  259. return rc;
  260. }
  261. isp_dev->sd.internal_ops = &isp_internal_ops;
  262. #ifdef ISP8000NANO_V1802
  263. isp_dev->ic_dev.mix_gpr = syscon_regmap_lookup_by_phandle(
  264. pdev->dev.of_node, "gpr");
  265. if (IS_ERR(isp_dev->ic_dev.mix_gpr)) {
  266. pr_warn("failed to get mix gpr\n");
  267. isp_dev->ic_dev.mix_gpr = NULL;
  268. return -ENOMEM;
  269. }
  270. #endif
  271. mem_node = of_parse_phandle(pdev->dev.of_node, "memory-region", 0);
  272. if (!mem_node) {
  273. pr_err("No memory-region found\n");
  274. return -ENODEV;
  275. }
  276. isp_dev->ic_dev.rmem = of_reserved_mem_lookup(mem_node);
  277. if (!isp_dev->ic_dev.rmem) {
  278. pr_err("of_reserved_mem_lookup() returned NULL\n");
  279. return -ENODEV;
  280. }
  281. v4l2_subdev_init(&isp_dev->sd, &isp_v4l2_subdev_ops);
  282. snprintf(isp_dev->sd.name, sizeof(isp_dev->sd.name),
  283. "vvcam-isp.%d", isp_dev->ic_dev.id);
  284. isp_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  285. isp_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
  286. isp_dev->sd.owner = THIS_MODULE;
  287. isp_dev->sd.dev = &pdev->dev;
  288. v4l2_set_subdevdata(&isp_dev->sd, isp_dev);
  289. isp_dev->vd = kzalloc(sizeof(*isp_dev->vd), GFP_KERNEL);
  290. if (WARN_ON(!isp_dev->vd)) {
  291. rc = -ENOMEM;
  292. goto end;
  293. }
  294. rc = v4l2_device_register(&(pdev->dev), isp_dev->vd);
  295. if (WARN_ON(rc < 0))
  296. goto end;
  297. rc = v4l2_device_register_subdev(isp_dev->vd, &isp_dev->sd);
  298. if (rc) {
  299. pr_err("failed to register subdev %d\n", rc);
  300. goto end;
  301. }
  302. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  303. isp_dev->ic_dev.base = devm_ioremap_resource(&pdev->dev, mem_res);
  304. if (IS_ERR(isp_dev->ic_dev.base)) {
  305. pr_err("failed to get ioremap resource.\n");
  306. goto end;
  307. }
  308. #ifdef ISP_REG_RESET
  309. isp_dev->ic_dev.reset = ioremap(ISP_REG_RESET, 4);
  310. #endif
  311. pr_debug("ioremap addr: %px", isp_dev->ic_dev.base);
  312. irq = platform_get_irq(pdev, 0);
  313. if (irq < 0) {
  314. pr_err("failed to get irq number.\n");
  315. goto end;
  316. }
  317. rc = devm_request_irq(&pdev->dev, irq, isp_hw_isr_reg_update, IRQF_SHARED,
  318. dev_name(&pdev->dev), &isp_dev->ic_dev);
  319. if (rc) {
  320. pr_err("failed to request irq.\n");
  321. irq = -1;
  322. goto end;
  323. }
  324. isp_dev->irq = irq;
  325. pr_debug("request_irq num:%d, rc:%d", irq, rc);
  326. platform_set_drvdata(pdev, isp_dev);
  327. rc = v4l2_device_register_subdev_nodes(isp_dev->vd);
  328. pm_runtime_enable(&pdev->dev);
  329. pr_info("vvcam isp driver registered\n");
  330. return 0;
  331. end:
  332. if (irq >= 0)
  333. devm_free_irq(&pdev->dev, irq, &isp_dev->ic_dev);
  334. kfree(isp_dev);
  335. return 1;
  336. }
  337. int isp_hw_remove(struct platform_device *pdev)
  338. {
  339. struct isp_device *isp = platform_get_drvdata(pdev);
  340. pr_info("enter %s\n", __func__);
  341. if (!isp)
  342. return -1;
  343. devm_free_irq(&pdev->dev, isp->irq, &isp->ic_dev);
  344. v4l2_device_unregister_subdev(&isp->sd);
  345. if (isp->vd) {
  346. v4l2_device_disconnect(isp->vd);
  347. v4l2_device_put(isp->vd);
  348. }
  349. kfree(isp);
  350. pm_runtime_disable(&pdev->dev);
  351. return 0;
  352. }
  353. static int isp_system_suspend(struct device *dev)
  354. {
  355. return pm_runtime_force_suspend(dev);;
  356. }
  357. static int isp_system_resume(struct device *dev)
  358. {
  359. int ret;
  360. ret = pm_runtime_force_resume(dev);
  361. if (ret < 0) {
  362. dev_err(dev, "force resume %s failed!\n", dev_name(dev));
  363. return ret;
  364. }
  365. return 0;
  366. }
  367. static int isp_runtime_suspend(struct device *dev)
  368. {
  369. struct isp_device *isp_dev = dev_get_drvdata(dev);
  370. isp_disable_clocks(isp_dev);
  371. return 0;
  372. }
  373. static int isp_runtime_resume(struct device *dev)
  374. {
  375. struct isp_device *isp_dev = dev_get_drvdata(dev);
  376. isp_enable_clocks(isp_dev);
  377. return 0;
  378. }
  379. static const struct dev_pm_ops isp_pm_ops = {
  380. SET_SYSTEM_SLEEP_PM_OPS(isp_system_suspend, isp_system_resume)
  381. SET_RUNTIME_PM_OPS(isp_runtime_suspend, isp_runtime_resume, NULL)
  382. };
  383. static const struct of_device_id isp_of_match[] = {
  384. {.compatible = "fsl,imx8mp-isp",},
  385. { /* sentinel */ },
  386. };
  387. MODULE_DEVICE_TABLE(of, isp_of_match);
  388. static struct platform_driver viv_isp_driver = {
  389. .probe = isp_hw_probe,
  390. .remove = isp_hw_remove,
  391. .driver = {
  392. .name = "vvcam-isp",
  393. .owner = THIS_MODULE,
  394. .of_match_table = isp_of_match,
  395. .pm = &isp_pm_ops,
  396. }
  397. };
  398. static int __init viv_isp_init_module(void)
  399. {
  400. int ret = 0;
  401. pr_info("enter %s\n", __func__);
  402. ret = platform_driver_register(&viv_isp_driver);
  403. if (ret) {
  404. pr_err("register platform driver failed.\n");
  405. return ret;
  406. }
  407. return ret;
  408. }
  409. static void __exit viv_isp_exit_module(void)
  410. {
  411. pr_info("enter %s\n", __func__);
  412. platform_driver_unregister(&viv_isp_driver);
  413. }
  414. module_init(viv_isp_init_module);
  415. module_exit(viv_isp_exit_module);
  416. MODULE_AUTHOR("Verisilicon ISP SW Team");
  417. MODULE_LICENSE("GPL");
  418. MODULE_ALIAS("Verisilicon-ISP");
  419. MODULE_VERSION("1.0");