vvcam_isp_driver_of.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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 <asm/io.h>
  54. #include <linux/cdev.h>
  55. #include <linux/module.h>
  56. #include <linux/platform_device.h>
  57. #include <linux/delay.h>
  58. #include <linux/clk.h>
  59. #include <linux/io.h>
  60. #include <linux/mm.h>
  61. #include <linux/timer.h>
  62. #include <linux/kernel.h>
  63. #include <linux/init.h>
  64. #include <linux/workqueue.h>
  65. #include <linux/slab.h>
  66. #include <linux/proc_fs.h>
  67. #include <linux/debugfs.h>
  68. #include <linux/miscdevice.h>
  69. #include <linux/uaccess.h>
  70. #include <linux/interrupt.h>
  71. #include <linux/of_reserved_mem.h>
  72. #include <linux/pm_runtime.h>
  73. #include <linux/of.h>
  74. #include <linux/of_address.h>
  75. #include <linux/dma-mapping.h>
  76. #include <linux/dma-buf.h>
  77. #include "isp_ioctl.h"
  78. #include "mrv_all_regs.h"
  79. #include "isp_wdr.h"
  80. extern MrvAllRegister_t *all_regs;
  81. #define VIVCAM_ISP_NAME "vivisp"
  82. #define VIVCAM_ISP_MAXCNT 2
  83. #define VIVCAM_ISP_IRQ_NUMBER 16
  84. typedef struct {
  85. volatile uint64_t frame_irq_cnt;
  86. volatile uint64_t frame_time_us;
  87. volatile int x[2];
  88. } frame_mark_t;
  89. struct vvcam_isp_driver_dev
  90. {
  91. struct cdev cdev;
  92. dev_t devt;
  93. struct class *class;
  94. struct mutex vvmutex;
  95. unsigned int device_idx;
  96. struct timer_list isp_timer;
  97. struct work_struct vvnative_wq;
  98. wait_queue_head_t irq_wait;
  99. int irq_num[2];
  100. void *private;
  101. struct clk *aclk;
  102. struct clk *hclk;
  103. struct clk *isp0_pclk;
  104. struct clk *cclk;
  105. struct clk *isp1_pclk;
  106. struct platform_device *pdev;
  107. frame_mark_t *frame_mark;
  108. };
  109. static unsigned int vvcam_isp_major = 0;
  110. static unsigned int vvcam_isp_minor = 0;
  111. static struct class *vvcam_isp_class;
  112. static unsigned int devise_register_index = 0;
  113. static bool isp_irq = false;
  114. static unsigned int vvnative_isp_poll(struct file * filp, poll_table *wait)
  115. {
  116. unsigned int mask = 0;
  117. struct vvcam_isp_driver_dev *pdriver_dev = filp->private_data;
  118. poll_wait(filp, &pdriver_dev->irq_wait, wait);
  119. //pr_info("poll isp_irq %d\n", isp_irq);
  120. if (isp_irq) {
  121. mask |= POLLIN |POLLRDNORM;
  122. isp_irq = false;
  123. }
  124. return mask;
  125. }
  126. static uint64_t get_us_time(void)
  127. {
  128. struct timespec64 ts;
  129. static uint64_t us = 0;
  130. ktime_get_real_ts64(&ts);
  131. us = timespec64_to_ns(&ts) / 1000UL;
  132. return us;
  133. }
  134. static void vvnative_isp_work(struct work_struct *work)
  135. {
  136. /*Todo update those module that does not have shandow register*/
  137. struct vvcam_isp_driver_dev *pdriver_dev = container_of(work, struct vvcam_isp_driver_dev, vvnative_wq);
  138. struct isp_ic_dev * pisp_dev = pdriver_dev->private;
  139. isp_irq = true;
  140. wake_up_interruptible(&pdriver_dev->irq_wait);
  141. if (pisp_dev->isp_mis & MRV_ISP_MIS_FRAME_MASK) {
  142. if (pisp_dev->wdr.changed) {
  143. pr_info("%s pisp_dev->wdr.changed %d\n", __func__,
  144. pisp_dev->wdr.changed);
  145. isp_s_wdr(pisp_dev);
  146. }
  147. if (pisp_dev->flt.changed) {
  148. pr_info("%s pisp_dev->flt.changed %d\n", __func__,
  149. pisp_dev->flt.changed);
  150. isp_s_flt(pisp_dev);
  151. }
  152. #ifndef ISP_CPROC_SHD
  153. if (pisp_dev->cproc.changed) {
  154. pr_info("%s pisp_dev->cproc.changed %d\n", __func__,
  155. pisp_dev->cproc.changed);
  156. isp_s_cproc(pisp_dev);
  157. }
  158. #endif
  159. if (pisp_dev->gamma_out.changed) {
  160. pr_info("%s pisp_dev->gamma_out.changed %d\n", __func__,
  161. pisp_dev->gamma_out.changed);
  162. isp_s_gamma_out(pisp_dev);
  163. }
  164. if (pisp_dev->rgbgamma.data_changed) {
  165. //pr_info("%s pisp_dev->rgbgamma.data_changed %d\n", __func__,
  166. //pisp_dev->rgbgamma.data_changed);
  167. isp_s_rgbgamma(pisp_dev);
  168. }
  169. if (pisp_dev->rgbgamma.changed) {
  170. //pr_info("%s pisp_dev->rgbgamma.changed %d\n", __func__,pisp_dev->rgbgamma.changed);
  171. if (pisp_dev->rgbgamma.enable) {
  172. isp_enable_rgbgamma(pisp_dev);
  173. } else {
  174. isp_disable_rgbgamma(pisp_dev);
  175. }
  176. }
  177. if (pisp_dev->dgain.changed) {
  178. isp_s_digital_gain(pisp_dev);
  179. }
  180. }
  181. }
  182. static irqreturn_t vvcam_isp_irq(int irq, void *dev_id)
  183. {
  184. struct vvcam_isp_driver_dev *pdriver_dev ;
  185. struct isp_ic_dev * pisp_dev;
  186. u64 isp_mis, mi_mis = 0;
  187. pdriver_dev = (struct vvcam_isp_driver_dev *)dev_id;
  188. pisp_dev = pdriver_dev->private;
  189. isp_mis = isp_read_reg(pisp_dev, REG_ADDR(isp_mis));
  190. if (isp_mis & MRV_ISP_MIS_FLASH_ON_MASK) {
  191. mi_mis |= 0x4;
  192. }
  193. volatile frame_mark_t *frame_mark = pdriver_dev->frame_mark;
  194. if (isp_mis & MRV_ISP_MIS_FRAME_IN_MASK) {
  195. frame_mark->frame_time_us = get_us_time();
  196. frame_mark->frame_irq_cnt += 1;
  197. }
  198. if (mi_mis) {
  199. isp_mis_t mis_data;
  200. mis_data.irq_src = SRC_MI_IRQ;
  201. mis_data.val = mi_mis;
  202. isp_irq_write_circle_queue(&mis_data, &pisp_dev->circle_list);
  203. }
  204. if (isp_mis) {
  205. isp_mis_t mis_data;
  206. mis_data.irq_src = SRC_ISP_IRQ;
  207. mis_data.val = isp_mis;
  208. pisp_dev->isp_mis = isp_mis;
  209. isp_irq_write_circle_queue(&mis_data, &pisp_dev->circle_list);
  210. isp_write_reg(pisp_dev, REG_ADDR(isp_icr), isp_mis);
  211. if(isp_mis & MRV_ISP_MIS_ISP_OFF_MASK) {
  212. isp_write_reg(pisp_dev, REG_ADDR(isp_imsc), isp_read_reg(pisp_dev, REG_ADDR(isp_imsc))&(~MRV_ISP_MIS_ISP_OFF_MASK));
  213. }
  214. }
  215. if (isp_mis != 0 ||mi_mis != 0 ) {
  216. schedule_work(&pdriver_dev->vvnative_wq);
  217. } else {
  218. return IRQ_HANDLED; // return IRQ_NONE;
  219. }
  220. return IRQ_HANDLED;
  221. }
  222. static irqreturn_t vvcam_mi_irq(int irq, void *dev_id)
  223. {
  224. struct vvcam_isp_driver_dev *pdriver_dev ;
  225. struct isp_ic_dev * pisp_dev;
  226. u32 mi_mis, mi_mis_addr, mi_icr_addr;
  227. #ifdef ISP_MIV2
  228. u32 miv2_mis1, miv2_mis3;
  229. #endif
  230. #if defined(ISP_MI_PP_READ) || defined (ISP_3DNR_V3) || defined (ISP_MI_PP_WRITE) || defined (ISP_MI_HDR)
  231. u32 miv2_mis2;
  232. #endif
  233. pdriver_dev = (struct vvcam_isp_driver_dev *)dev_id;
  234. pisp_dev = pdriver_dev->private;
  235. #ifdef ISP_MIV2
  236. mi_icr_addr = REG_ADDR(miv2_icr);
  237. mi_mis_addr = REG_ADDR(miv2_mis);
  238. miv2_mis1 = isp_read_reg(pisp_dev, REG_ADDR(miv2_mis1));
  239. if (miv2_mis1) {
  240. /*pr_info("%s mi mis1 0x%08x\n", __func__, miv2_mis1);*/
  241. isp_write_reg(pisp_dev, REG_ADDR(miv2_icr1), miv2_mis1);
  242. }
  243. /*u32 miv2_mis3 = isp_read_reg(pisp_dev, REG_ADDR(miv2_mis3));
  244. if (miv2_mis3) {
  245. pr_info("%s mi mis3 0x%08x\n", __func__, miv2_mis3);
  246. isp_write_reg(pisp_dev, REG_ADDR(miv2_icr3), miv2_mis3);
  247. }*/
  248. #elif defined(ISP_MIV1)
  249. mi_icr_addr = REG_ADDR(mi_icr);
  250. mi_mis_addr = REG_ADDR(mi_mis);
  251. #endif
  252. mi_mis = isp_read_reg(pisp_dev, mi_mis_addr);
  253. #if defined(ISP_MI_PP_READ) || defined (ISP_3DNR_V3) || defined (ISP_MI_PP_WRITE) || defined (ISP_MI_HDR)
  254. miv2_mis2 = isp_read_reg(pisp_dev, REG_ADDR(miv2_mis2));
  255. miv2_mis2 &= (~PPW_FRAME_END_MASK);
  256. /*pr_info("%s isp mis 0x%08x, mi mis 0x%08x post mis 0x%08x\n", __func__, \
  257. isp_mis, mi_mis, miv2_mis2);*/
  258. #else
  259. /*pr_info("%s isp mis 0x%08x, mi mis 0x%08x\n", __func__, \
  260. isp_mis, mi_mis);*/
  261. #endif
  262. if (mi_mis) {
  263. isp_mis_t mis_data;
  264. mis_data.irq_src = SRC_MI_IRQ;
  265. mis_data.val = mi_mis;
  266. isp_irq_write_circle_queue(&mis_data, &pisp_dev->circle_list);
  267. isp_write_reg(pisp_dev, mi_icr_addr, mi_mis);
  268. }
  269. #ifdef ISP_MIV2
  270. if (miv2_mis1) {
  271. isp_mis_t mis_data;
  272. mis_data.irq_src = SRC_MI1_IRQ;
  273. mis_data.val = mi_mis;
  274. isp_irq_write_circle_queue(&mis_data, &pisp_dev->circle_list);
  275. isp_write_reg(pisp_dev, mi_icr_addr, mi_mis);
  276. }
  277. #endif
  278. #if defined(ISP_MI_PP_READ) || defined (ISP_3DNR_V3) || defined (ISP_MI_PP_WRITE) || defined (ISP_MI_HDR)
  279. if (miv2_mis2) {
  280. isp_mis_t mis_data;
  281. mis_data.irq_src = SRC_MI2_IRQ;
  282. mis_data.val = miv2_mis2;
  283. isp_irq_write_circle_queue(&mis_data, &pisp_dev->circle_list);
  284. isp_write_reg(pisp_dev, REG_ADDR(miv2_icr2), miv2_mis2);
  285. }
  286. #endif
  287. #if defined(ISP_MI_PP_READ) || defined (ISP_3DNR_V3) || defined (ISP_MI_PP_WRITE) || defined (ISP_MI_HDR)
  288. if (mi_mis != 0 || miv2_mis2 != 0) {
  289. #else
  290. if (mi_mis != 0 ) {
  291. #endif
  292. schedule_work(&pdriver_dev->vvnative_wq);
  293. } else {
  294. return IRQ_HANDLED;
  295. }
  296. return IRQ_HANDLED;
  297. }
  298. static int vvcam_isp_runtime_suspend(struct device *dev)
  299. {
  300. struct vvcam_isp_driver_dev *pdriver_dev = dev_get_drvdata(dev);
  301. clk_disable_unprepare(pdriver_dev->aclk);
  302. clk_disable_unprepare(pdriver_dev->hclk);
  303. clk_disable_unprepare(pdriver_dev->isp0_pclk);
  304. clk_disable_unprepare(pdriver_dev->cclk);
  305. if (IS_ERR(pdriver_dev->isp1_pclk))
  306. dev_err(dev, "isp1_pclk is null\n");
  307. else
  308. clk_disable_unprepare(pdriver_dev->isp1_pclk);
  309. pr_info("isp %s\n", __func__);
  310. return 0;
  311. }
  312. static int vvcam_isp_runtime_resume(struct device *dev)
  313. {
  314. struct vvcam_isp_driver_dev *pdriver_dev = dev_get_drvdata(dev);
  315. int ret = 0;
  316. ret = clk_prepare_enable(pdriver_dev->isp0_pclk);
  317. if (ret < 0) {
  318. dev_err(dev, "could not prepare or enable isp0 pixel clock\n");
  319. }
  320. ret = clk_prepare_enable(pdriver_dev->cclk);
  321. if (ret < 0) {
  322. dev_err(dev, "could not prepare or enable core clock\n");
  323. clk_disable_unprepare(pdriver_dev->isp0_pclk);
  324. //return ret;
  325. }
  326. ret = clk_prepare_enable(pdriver_dev->hclk);
  327. if (ret < 0) {
  328. dev_err(dev, "could not prepare or enable ahb clock\n");
  329. clk_disable_unprepare(pdriver_dev->isp0_pclk);
  330. clk_disable_unprepare(pdriver_dev->cclk);
  331. //return ret;
  332. }
  333. ret = clk_prepare_enable(pdriver_dev->aclk);
  334. if (ret < 0) {
  335. dev_err(dev, "could not prepare or enable axi clock\n");
  336. clk_disable_unprepare(pdriver_dev->isp0_pclk);
  337. clk_disable_unprepare(pdriver_dev->cclk);
  338. clk_disable_unprepare(pdriver_dev->hclk);
  339. //return ret;
  340. }
  341. if (IS_ERR(pdriver_dev->isp1_pclk)) {
  342. dev_err(dev, "isp1_pclk is null\n");
  343. } else {
  344. ret = clk_prepare_enable(pdriver_dev->isp1_pclk);
  345. if (ret < 0) {
  346. clk_disable_unprepare(pdriver_dev->isp0_pclk);
  347. clk_disable_unprepare(pdriver_dev->cclk);
  348. clk_disable_unprepare(pdriver_dev->hclk);
  349. dev_err(dev, "could not prepare or enable isp1 pixel clock\n");
  350. }
  351. }
  352. pr_info("%s isp Enabled clock\n", __func__);
  353. return ret;
  354. }
  355. static const struct dev_pm_ops vvcam_isp_runtime_pm_ops = {
  356. SET_RUNTIME_PM_OPS(vvcam_isp_runtime_suspend, vvcam_isp_runtime_resume, NULL)
  357. };
  358. static int vvcam_isp_open(struct inode * inode, struct file * file)
  359. {
  360. struct vvcam_isp_driver_dev *pdriver_dev;
  361. struct isp_ic_dev * pisp_dev;
  362. int ret = 0;
  363. pdriver_dev = container_of(inode->i_cdev, struct vvcam_isp_driver_dev, cdev);
  364. file->private_data = pdriver_dev;
  365. pisp_dev = pdriver_dev->private;
  366. struct device *dev = &pdriver_dev->pdev->dev;
  367. /*create circle queue*/
  368. isp_irq_create_circle_queue(&(pisp_dev->circle_list), QUEUE_NODE_COUNT);
  369. if (pm_runtime_get_sync(dev)) {
  370. ret = vvcam_isp_runtime_resume(dev);
  371. if (ret)
  372. pr_err("fail to resume isp %s %d\n", __func__, __LINE__);
  373. }
  374. return 0;
  375. };
  376. static long vvcam_isp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  377. {
  378. long ret = 0;
  379. struct vvcam_isp_driver_dev *pdriver_dev;
  380. struct isp_ic_dev * pisp_dev;
  381. pdriver_dev = file->private_data;
  382. if (pdriver_dev == NULL)
  383. {
  384. pr_err("%s:file private is null point error\n", __func__);
  385. return -ENOMEM;
  386. }
  387. pisp_dev = pdriver_dev->private;
  388. //pr_info("%s:isp[%d] pdriver_dev =0x%px\n", __func__,pdriver_dev->device_idx,pdriver_dev);
  389. //pr_info("%s:pisp_dev =0x%px\n", __func__,pisp_dev);
  390. mutex_lock(&pdriver_dev->vvmutex);
  391. ret = isp_priv_ioctl(pisp_dev, cmd ,(void __user *)arg);
  392. mutex_unlock(&pdriver_dev->vvmutex);
  393. return ret;
  394. };
  395. static int vvcam_isp_release(struct inode * inode, struct file * file)
  396. {
  397. int ret = 0;
  398. struct vvcam_isp_driver_dev *pdriver_dev;
  399. struct isp_ic_dev *pisp_dev;
  400. if ((inode == NULL) || (file == NULL) ) {
  401. pr_info("%s: %dx\n", __func__, __LINE__);
  402. return 0;
  403. }
  404. pdriver_dev = container_of(inode->i_cdev, struct vvcam_isp_driver_dev, cdev);
  405. struct device *dev = &pdriver_dev->pdev->dev;
  406. file->private_data = pdriver_dev;
  407. pisp_dev = pdriver_dev->private;
  408. pr_info("enter %s\n", __func__);
  409. isp_irq_destroy_circle_queue(&(pisp_dev->circle_list));
  410. if (pisp_dev->ut_addr != NULL) {
  411. #define UT_USED_SIZE 0x01000000
  412. dma_free_coherent(dev, UT_USED_SIZE,
  413. pisp_dev->ut_addr, pisp_dev->ut_phy_addr);
  414. pisp_dev->ut_addr = NULL;
  415. }
  416. ret = pm_runtime_put_sync(dev);
  417. if (ret) {
  418. pr_err("fail to suspen isp %s %d ret = %d\n", __func__, __LINE__, ret);
  419. }
  420. return 0;
  421. };
  422. static int vvcam_isp_mmap(struct file *pFile, struct vm_area_struct *vma)
  423. {
  424. int ret = 0;
  425. ulong phy_base_addr = 0;
  426. unsigned long pfn_start = (phy_base_addr >> PAGE_SHIFT) + vma->vm_pgoff;
  427. unsigned long size = vma->vm_end - vma->vm_start;
  428. /*pr_info("phy: 0x%lx, size: 0x%lx\n", pfn_start << PAGE_SHIFT, size);*/
  429. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  430. if (remap_pfn_range(vma, vma->vm_start,pfn_start,size, vma->vm_page_prot))
  431. {
  432. pr_err("-->%s: remap_pfn_range error!\n", __func__);
  433. return -EIO;
  434. }
  435. return ret;
  436. };
  437. static struct file_operations vvcam_isp_fops = {
  438. .owner = THIS_MODULE,
  439. .open = vvcam_isp_open,
  440. .release = vvcam_isp_release,
  441. .unlocked_ioctl = vvcam_isp_ioctl,
  442. .mmap = vvcam_isp_mmap,
  443. .poll = vvnative_isp_poll,
  444. };
  445. static int vvcam_isp_probe(struct platform_device *pdev)
  446. {
  447. int ret = 0;
  448. struct device_node *np;
  449. struct vvcam_isp_driver_dev *pdriver_dev;
  450. struct isp_ic_dev * pisp_dev;
  451. struct resource *mem;
  452. pr_info("enter %s\n", __func__);
  453. pdev->id = devise_register_index;
  454. if (pdev->id >= VIVCAM_ISP_MAXCNT)
  455. {
  456. pr_err("%s:pdev id is %d error\n", __func__,pdev->id);
  457. return -EINVAL;
  458. }
  459. pdriver_dev = devm_kzalloc(&pdev->dev,sizeof(struct vvcam_isp_driver_dev), GFP_KERNEL);
  460. if (pdriver_dev == NULL)
  461. {
  462. pr_err("%s:alloc struct vvcam_isp_driver_dev error\n", __func__);
  463. return -ENOMEM;
  464. }
  465. memset(pdriver_dev,0,sizeof(struct vvcam_isp_driver_dev ));
  466. pr_info("%s:isp[%d]: pdriver_dev =0x%px\n", __func__,pdev->id,pdriver_dev);
  467. pisp_dev = devm_kzalloc(&pdev->dev,sizeof(struct isp_ic_dev), GFP_KERNEL);
  468. if (pisp_dev == NULL)
  469. {
  470. pr_err("%s:alloc struct isp_ic_dev error\n", __func__);
  471. return -ENOMEM;
  472. }
  473. memset(pisp_dev,0,sizeof(struct isp_ic_dev ));
  474. pr_info("%s:isp[%d]: psensor_dev =0x%px\n", __func__,pdev->id,pisp_dev);
  475. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  476. pisp_dev->base = devm_ioremap_resource(&pdev->dev, mem);
  477. if (IS_ERR(pisp_dev->base))
  478. return PTR_ERR(pisp_dev->base);
  479. pr_info("%s:isp[%d]: pisp_dev->base=0x%px, phy_addr base=0x%llx\n", __func__,
  480. pdev->id, pisp_dev->base, mem->start);
  481. pisp_dev->reset = NULL;
  482. pisp_dev->device = &pdev->dev;
  483. pdriver_dev->private = pisp_dev;
  484. pdriver_dev->device_idx = pdev->id;
  485. mutex_init(&pdriver_dev->vvmutex);
  486. frame_mark_t *frame_mark = dma_alloc_coherent(&pdev->dev, sizeof(frame_mark_t),
  487. &pisp_dev->frame_mark_info_addr, GFP_KERNEL);
  488. if (frame_mark == NULL ) {
  489. pr_err("dma_alloc_coherent error\n");
  490. return -1;
  491. }
  492. pr_info("isp_frame_mark_info_addr = 0x%lx\n", pisp_dev->frame_mark_info_addr);
  493. memset(frame_mark, 0, sizeof(frame_mark_t));
  494. pdriver_dev->frame_mark = frame_mark;
  495. platform_set_drvdata(pdev, pdriver_dev);
  496. pdriver_dev->pdev = pdev;
  497. pdriver_dev->irq_num[0] = platform_get_irq(pdev, 0);
  498. if (pdriver_dev->irq_num[0] == 0) {
  499. pr_err("%s:isp[%d]: could not map IRQ\n", __func__, pdev->id);
  500. dev_err(&pdev->dev, "could not map IRQ.\n");
  501. return -ENXIO;
  502. }
  503. pr_info("%s:isp[%d]: pdriver_dev->irq_num[0]=%d\n", __func__, pdev->id, pdriver_dev->irq_num[0]);
  504. pdriver_dev->irq_num[1] = platform_get_irq(pdev, 1);
  505. if (pdriver_dev->irq_num[1] == 0) {
  506. pr_err("%s:isp[%d]: could not map IRQ\n", __func__, pdev->id);
  507. dev_err(&pdev->dev, "could not map IRQ.\n");
  508. return -ENXIO;
  509. }
  510. pr_info("%s:isp[%d]: pdriver_dev->irq_num[1]=%d\n", __func__, pdev->id, pdriver_dev->irq_num[1]);
  511. /*init work queue*/
  512. INIT_WORK(&pdriver_dev->vvnative_wq, vvnative_isp_work);
  513. ret = devm_request_irq(&pdev->dev, pdriver_dev->irq_num[0], vvcam_isp_irq,
  514. IRQF_TRIGGER_RISING | IRQF_SHARED, "ISP_IRQ", (char *)pdriver_dev);
  515. if (ret) {
  516. pr_err("%s[%d]:request irq error!\n", __func__, __LINE__);
  517. return ret;
  518. }
  519. ret = devm_request_irq(&pdev->dev, pdriver_dev->irq_num[1], vvcam_mi_irq,
  520. IRQF_TRIGGER_RISING, "MI_IRQ", (char *)pdriver_dev);
  521. if (ret) {
  522. pr_err("%s[%d]:request irq error!\n", __func__, __LINE__);
  523. return ret;
  524. }
  525. init_waitqueue_head(&pdriver_dev->irq_wait);
  526. /*parse clk info from dts*/
  527. pdriver_dev->aclk = devm_clk_get(&pdev->dev, "aclk");
  528. if (IS_ERR(pdriver_dev->aclk)) {
  529. dev_err(&pdev->dev, "failed to get aclk");
  530. //return -1;
  531. }
  532. pdriver_dev->hclk = devm_clk_get(&pdev->dev, "hclk");
  533. if (IS_ERR(pdriver_dev->hclk)) {
  534. dev_err(&pdev->dev, "failed to get hclk");
  535. //return -1;
  536. }
  537. pdriver_dev->isp0_pclk = devm_clk_get(&pdev->dev, "isp0_pclk");
  538. if (IS_ERR(pdriver_dev->isp0_pclk)) {
  539. dev_err(&pdev->dev, "failed to get isp0_pclk");
  540. //return -1;
  541. }
  542. pdriver_dev->cclk = devm_clk_get(&pdev->dev, "cclk");
  543. if (IS_ERR(pdriver_dev->cclk)) {
  544. dev_err(&pdev->dev, "failed to get core_clk");
  545. //return -1;
  546. }
  547. pdriver_dev->isp1_pclk = devm_clk_get(&pdev->dev, "isp1_pclk");
  548. if (IS_ERR(pdriver_dev->isp1_pclk)) {
  549. dev_err(&pdev->dev, "failed to get isp1_pclk");
  550. //return -1;
  551. }
  552. if ((devise_register_index == 0)) {
  553. int ret;
  554. if (vvcam_isp_major == 0) {
  555. ret = alloc_chrdev_region(&pdriver_dev->devt, 0, VIVCAM_ISP_MAXCNT, VIVCAM_ISP_NAME);
  556. if (ret != 0)
  557. {
  558. pr_err("%s:alloc_chrdev_region error\n", __func__);
  559. return ret;
  560. }
  561. vvcam_isp_major = MAJOR(pdriver_dev->devt);
  562. vvcam_isp_minor = MINOR(pdriver_dev->devt);
  563. }
  564. else
  565. {
  566. pdriver_dev->devt = MKDEV(vvcam_isp_major, vvcam_isp_minor);
  567. ret = register_chrdev_region(pdriver_dev->devt, VIVCAM_ISP_MAXCNT, VIVCAM_ISP_NAME);
  568. if (ret)
  569. {
  570. pr_err("%s:register_chrdev_region error\n", __func__);
  571. return ret;
  572. }
  573. }
  574. vvcam_isp_class = class_create(THIS_MODULE, VIVCAM_ISP_NAME);
  575. if (IS_ERR(vvcam_isp_class))
  576. {
  577. pr_err("%s[%d]:class_create error!\n", __func__, __LINE__);
  578. return -EINVAL;
  579. }
  580. }
  581. pdriver_dev->devt = MKDEV(vvcam_isp_major, vvcam_isp_minor + pdev->id);
  582. cdev_init(&pdriver_dev->cdev, &vvcam_isp_fops);
  583. ret = cdev_add(&pdriver_dev->cdev, pdriver_dev->devt, 1);
  584. if ( ret )
  585. {
  586. pr_err("%s[%d]:cdev_add error!\n", __func__, __LINE__);
  587. return ret;
  588. }
  589. pdriver_dev->class = vvcam_isp_class;
  590. device_create(pdriver_dev->class, NULL, pdriver_dev->devt,
  591. pdriver_dev, "%s%d", VIVCAM_ISP_NAME, pdev->id);
  592. struct device *dev = &pdriver_dev->pdev->dev;
  593. pm_runtime_enable(dev);
  594. ret = vvcam_isp_runtime_resume(dev);
  595. if (ret < 0) {
  596. dev_err(dev, "fail to resume isp\n");
  597. }
  598. vvcam_isp_runtime_suspend(dev);
  599. if (ret < 0) {
  600. dev_err(dev, "fail to suspend isp\n");
  601. }
  602. devise_register_index++;
  603. pr_info("exit %s\n", __func__);
  604. return ret;
  605. }
  606. static int vvcam_isp_remove(struct platform_device *pdev)
  607. {
  608. struct vvcam_isp_driver_dev *pdriver_dev;
  609. struct isp_ic_dev * pisp_dev;
  610. pr_info("enter %s\n", __func__);
  611. devise_register_index--;
  612. pdriver_dev = platform_get_drvdata(pdev);
  613. pisp_dev = pdriver_dev->private;
  614. dma_free_coherent(&pdev->dev, sizeof(*pdriver_dev->frame_mark),
  615. pdriver_dev->frame_mark, pisp_dev->frame_mark_info_addr);
  616. if (pisp_dev->ut_addr != NULL) {
  617. #define UT_USED_SIZE 0x01000000
  618. dma_free_coherent(&pdev->dev, UT_USED_SIZE,
  619. pisp_dev->ut_addr, pisp_dev->ut_phy_addr);
  620. pisp_dev->ut_addr =NULL;
  621. }
  622. free_irq(pdriver_dev->irq_num[0], pdriver_dev);
  623. free_irq(pdriver_dev->irq_num[1], pdriver_dev);
  624. cdev_del(&pdriver_dev->cdev);
  625. device_destroy(pdriver_dev->class, pdriver_dev->devt);
  626. unregister_chrdev_region(pdriver_dev->devt, VIVCAM_ISP_MAXCNT);
  627. if (devise_register_index == 0)
  628. {
  629. class_destroy(pdriver_dev->class);
  630. }
  631. return 0;
  632. }
  633. static const struct of_device_id isp_of_match[] = {
  634. { .compatible = "thead,light-isp", },
  635. { /* sentinel */ },
  636. };
  637. static struct platform_driver vvcam_isp_driver = {
  638. .probe = vvcam_isp_probe,
  639. .remove = vvcam_isp_remove,
  640. .driver = {
  641. .name = VIVCAM_ISP_NAME,
  642. .owner = THIS_MODULE,
  643. .of_match_table = of_match_ptr(isp_of_match),
  644. .pm = &vvcam_isp_runtime_pm_ops,
  645. }
  646. };
  647. static int __init vvcam_isp_init_module(void)
  648. {
  649. int ret = 0;
  650. pr_info("enter %s\n", __func__);
  651. ret = platform_driver_register(&vvcam_isp_driver);
  652. if (ret)
  653. {
  654. pr_err("register platform driver failed.\n");
  655. return ret;
  656. }
  657. return ret;
  658. }
  659. static void __exit vvcam_isp_exit_module(void)
  660. {
  661. pr_info("enter %s\n", __func__);
  662. platform_driver_unregister(&vvcam_isp_driver);
  663. }
  664. module_init(vvcam_isp_init_module);
  665. module_exit(vvcam_isp_exit_module);
  666. MODULE_DESCRIPTION("ISP");
  667. MODULE_LICENSE("GPL");