isp_scale_drv.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Arkmicro ISP Scale driver
  3. *
  4. * Licensed under GPLv2 or later.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/version.h>
  8. #include <linux/kernel.h>
  9. #include <linux/kthread.h>
  10. #include <linux/cdev.h>
  11. #include <linux/device.h>
  12. #include <linux/fs.h>
  13. #include <linux/io.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/wait.h>
  16. #include <linux/fb.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/delay.h>
  19. #include <linux/completion.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/poll.h>
  22. #include <linux/spinlock.h>
  23. #include "isp_scale.h"
  24. static int get_finish_frame_count(struct ark_isp_scale_context *context)
  25. {
  26. int i;
  27. int count = 0;
  28. unsigned long flags = 0;
  29. spin_lock_irqsave(&context->lock, flags);
  30. for (i = 0; i < context->isbuf_num; i++) {
  31. if (context->isbuf_status[i] == ISBUF_STATUS_READY)
  32. count++;
  33. }
  34. spin_unlock_irqrestore(&context->lock, flags);
  35. return count;
  36. }
  37. static void xm_isp_scalar_start(struct ark_isp_scale_context *context)
  38. {
  39. writel(3, context->mmio_base + ISP_SCALE_EN);
  40. }
  41. static void xm_isp_scalar_stop(struct ark_isp_scale_context *context)
  42. {
  43. writel(0, context->mmio_base + ISP_SCALE_EN);
  44. }
  45. static int ark_isp_scale_open(struct inode *inode, struct file *filp)
  46. {
  47. struct ark_isp_scale_device *dev;
  48. struct ark_isp_scale_context *context;
  49. dev = container_of(inode->i_cdev, struct ark_isp_scale_device, cdev);
  50. context = &dev->context;
  51. filp->private_data = dev;
  52. return 0;
  53. }
  54. static int ark_isp_scale_fasync(int fd, struct file *filp, int mode)
  55. {
  56. int ret;
  57. struct ark_isp_scale_device *isp_scale =
  58. (struct ark_isp_scale_device *)filp->private_data;
  59. ret = fasync_helper(fd, filp, mode, &isp_scale->async_queue_wb);
  60. return ret;
  61. }
  62. static int ark_isp_scale_release(struct inode *inode, struct file *filp)
  63. {
  64. struct ark_isp_scale_device *dev;
  65. dev = container_of(inode->i_cdev, struct ark_isp_scale_device, cdev);
  66. if(filp->f_flags & FASYNC)
  67. {
  68. /* remove this filp from the asynchronusly notified filp's */
  69. ark_isp_scale_fasync(-1, filp, 0);
  70. }
  71. return 0;
  72. }
  73. static long ark_isp_scale_ioctl(struct file *filp,
  74. unsigned int cmd, unsigned long arg)
  75. {
  76. struct ark_isp_scale_device *isp_scale =
  77. (struct ark_isp_scale_device *)filp->private_data;
  78. struct ark_isp_scale_context *context = &isp_scale->context;
  79. int i;
  80. unsigned long flags;
  81. switch (cmd)
  82. {
  83. case ISP_SCALE_IOCTL_INIT:
  84. {
  85. struct isp_scale_init_para para;
  86. if(copy_from_user(&para, (void *)arg, sizeof(para))){
  87. printk("%s: copy from user init error\n", __func__);
  88. return -EFAULT;
  89. }
  90. xm_isp_scalar_stop(context);
  91. udelay(100);
  92. isp_scale_softreset(context);
  93. memset(&context->config, 0, sizeof(context->config));
  94. context->config.src_channel = para.src_channel;
  95. context->config.src_hsync_polarity = para.src_hsync_polarity;
  96. context->config.src_vsync_polarity = para.src_vsync_polarity;
  97. context->config.src_format = para.src_format;
  98. context->config.src_ycbcr_sequence = para.src_ycbcr_sequence;
  99. context->config.src_width = para.src_width;
  100. context->config.src_height = para.src_height;
  101. context->config.src_stride = context->config.src_width;
  102. context->config.src_window_x = para.src_window_x;
  103. context->config.src_window_y = para.src_window_y;
  104. context->config.src_window_width = para.src_window_width;
  105. context->config.src_window_height = para.src_window_height;
  106. context->config.dst_format = XM_ISP_SCALAR_FORMAT_Y_UV420;
  107. context->config.dst_width = para.dst_width;
  108. context->config.dst_height = para.dst_height;
  109. context->config.dst_stride = context->config.dst_width;
  110. context->config.dst_window_x = 0;
  111. context->config.dst_window_y = 0;
  112. context->config.dst_window_width = para.dst_width;
  113. context->config.dst_window_height = para.dst_height;
  114. context->config.mid_line = para.mid_line_counter;
  115. xm_isp_scalar_config (context, &context->config);
  116. }
  117. break;
  118. case ISP_SCALE_IOCTL_SET_BUFFER:
  119. {
  120. struct isp_scale_sbuf_para sbuf;
  121. int num;
  122. if(copy_from_user(&sbuf, (void *)arg, sizeof(sbuf))){
  123. printk("%s: copy from user sbuf error\n", __func__);
  124. return -EFAULT;
  125. }
  126. num = sbuf.num;
  127. if (num > ISBUF_FIFO_DEPTH)
  128. num = ISBUF_FIFO_DEPTH;
  129. spin_lock_irqsave(&context->lock, flags);
  130. context->isbuf_num = num;
  131. for (i = 0; i < num; i++) {
  132. context->isbuf[i] = sbuf.buf[i];
  133. context->isbuf_id[i].id = i;
  134. }
  135. spin_unlock_irqrestore(&context->lock, flags);
  136. }
  137. break;
  138. case ISP_SCALE_IOCTL_START:
  139. isp_scale_buffer_init(context);
  140. xm_isp_scalar_start(context);
  141. break;
  142. case ISP_SCALE_IOCTL_STOP:
  143. xm_isp_scalar_stop(context);
  144. break;
  145. case ISP_SCALE_IOCTL_GET_READY:
  146. {
  147. struct isp_scale_buf buf = {0};
  148. spin_lock_irqsave(&context->lock, flags);
  149. for (i = 0; i < context->isbuf_num; i++) {
  150. if (context->isbuf_status[i] == ISBUF_STATUS_READY) {
  151. buf = context->isbuf[i];
  152. break;
  153. }
  154. }
  155. spin_unlock_irqrestore(&context->lock, flags);
  156. if (copy_to_user((void*)arg, &buf, sizeof(buf))) {
  157. printk("%s %d: copy_to_user error\n",
  158. __FUNCTION__, __LINE__);
  159. return -EFAULT;
  160. }
  161. }
  162. break;
  163. case ISP_SCALE_IOCTL_SET_FREE:
  164. {
  165. struct isp_scale_buf buf;
  166. if(copy_from_user(&buf, (void *)arg, sizeof(buf))){
  167. printk("%s %d: copy_from_user error\n",
  168. __FUNCTION__, __LINE__);
  169. return -EFAULT;
  170. }
  171. spin_lock_irqsave(&context->lock, flags);
  172. for (i = 0; i < context->isbuf_num; i++) {
  173. if (buf.yaddr == context->isbuf[i].yaddr && buf.uvaddr == context->isbuf[i].uvaddr) {
  174. if (context->isbuf_status[i] == ISBUF_STATUS_READY)
  175. context->isbuf_status[i] = ISBUF_STATUS_FREE;
  176. else
  177. printk(KERN_ALERT "app free no-ready buf %d.\n", i);
  178. break;
  179. }
  180. }
  181. spin_unlock_irqrestore(&context->lock, flags);
  182. }
  183. break;
  184. default:
  185. printk("%s %d: undefined cmd (0x%2X)\n",
  186. __FUNCTION__, __LINE__, cmd);
  187. return -EINVAL;
  188. }
  189. return 0;
  190. }
  191. static int ark_isp_scale_read(struct file *filp, char __user *user, size_t size,loff_t *ppos)
  192. {
  193. struct ark_isp_scale_device *isp_scale = (struct ark_isp_scale_device *)filp->private_data;
  194. struct ark_isp_scale_context *context = &isp_scale->context;
  195. int i;
  196. char frames[ISBUF_FIFO_DEPTH];
  197. int count = 0;
  198. unsigned long flags;
  199. if (size > context->isbuf_num)
  200. return -EINVAL;
  201. wait_event_interruptible(isp_scale->frame_finish_waitq, get_finish_frame_count(context) > 0);
  202. spin_lock_irqsave(&context->lock, flags);
  203. for (i = 0; i < context->isbuf_num; i++) {
  204. if (context->isbuf_status[i] == ISBUF_STATUS_READY) {
  205. frames[count++] = i;
  206. if (count >= size)
  207. break;
  208. }
  209. }
  210. spin_unlock_irqrestore(&context->lock, flags);
  211. if (copy_to_user(user, frames, min(count, ISBUF_FIFO_DEPTH))) {
  212. printk("%s %d: copy_to_user error\n",
  213. __FUNCTION__, __LINE__);
  214. return -EFAULT;
  215. }
  216. return count;
  217. }
  218. static unsigned int ark_isp_scale_poll(struct file *filp, poll_table *wait)
  219. {
  220. struct ark_isp_scale_device *isp_scale = (struct ark_isp_scale_device *)filp->private_data;
  221. struct ark_isp_scale_context *context = &isp_scale->context;
  222. unsigned int mask = 0;
  223. unsigned long flags;
  224. poll_wait(filp, &isp_scale->frame_finish_waitq, wait);
  225. if (get_finish_frame_count(context) > 0)
  226. mask |= POLLIN | POLLRDNORM;
  227. return mask;
  228. }
  229. static struct file_operations ark_isp_scale_fops = {
  230. .owner = THIS_MODULE,
  231. .open = ark_isp_scale_open,
  232. .unlocked_ioctl = ark_isp_scale_ioctl,
  233. .release = ark_isp_scale_release,
  234. .fasync = ark_isp_scale_fasync,
  235. .read = ark_isp_scale_read,
  236. .poll = ark_isp_scale_poll,
  237. };
  238. /* This function is invoked when the device module is loaded into the
  239. * kernel. It allocates system resources for constructing driver control
  240. * data structures and initializes them accordingly.
  241. */
  242. static int ark_isp_scale_probe(struct platform_device *pdev)
  243. {
  244. struct ark_isp_scale_device *isp_scale;
  245. struct resource *res;
  246. dev_t dev;
  247. void __iomem *regs;
  248. int error = 0;
  249. isp_scale = devm_kzalloc(&pdev->dev, sizeof(struct ark_isp_scale_device), GFP_KERNEL);
  250. if (isp_scale == NULL) {
  251. dev_err(&pdev->dev, "%s %d: failed to allocate memory\n",
  252. __FUNCTION__, __LINE__);
  253. return -ENOMEM;
  254. }
  255. isp_scale->driver_name = "ark_isp_scale_drv";
  256. isp_scale->name = "ark_isp_scale";
  257. isp_scale->major = 0; /* if 0, let system choose */
  258. isp_scale->minor_start = 0;
  259. isp_scale->minor_num = 1; /* one dev only */
  260. isp_scale->num = 1;
  261. /* register char device */
  262. if (!isp_scale->major) {
  263. error = alloc_chrdev_region(
  264. &dev,
  265. isp_scale->minor_start,
  266. isp_scale->num,
  267. isp_scale->name
  268. );
  269. if (!error) {
  270. isp_scale->major = MAJOR(dev);
  271. isp_scale->minor_start = MINOR(dev);
  272. }
  273. } else {
  274. dev = MKDEV(isp_scale->major, isp_scale->minor_start);
  275. error = register_chrdev_region(dev, isp_scale->num,
  276. (char *)isp_scale->name);
  277. }
  278. if (error < 0) {
  279. dev_err(&pdev->dev, "%s %d: register driver error\n",
  280. __FUNCTION__, __LINE__);
  281. goto err_driver_register;
  282. }
  283. /* associate the file operations */
  284. cdev_init(&isp_scale->cdev, &ark_isp_scale_fops);
  285. isp_scale->cdev.owner = THIS_MODULE; //driver->owner;
  286. isp_scale->cdev.ops = &ark_isp_scale_fops;
  287. error = cdev_add(&isp_scale->cdev, dev, isp_scale->num);
  288. if (error) {
  289. dev_err(&pdev->dev, "%s %d: cdev add error\n", __FUNCTION__, __LINE__);
  290. goto err_cdev_add;
  291. }
  292. isp_scale->isp_scale_class = class_create(THIS_MODULE, "isp_scale_class");
  293. if(IS_ERR(isp_scale->isp_scale_class)) {
  294. dev_err(&pdev->dev, "Err: failed in creating ark isp scale class.\n");
  295. isp_scale->isp_scale_class = NULL;
  296. goto err_cdev_add;
  297. }
  298. isp_scale->isp_scale_device = device_create(isp_scale->isp_scale_class, NULL, dev, NULL, "isp_scale");
  299. if (IS_ERR(isp_scale->isp_scale_device)) {
  300. dev_err(&pdev->dev, "Err: failed in creating ark isp scale device.\n");
  301. isp_scale->isp_scale_device = NULL;
  302. goto err_cdev_add;
  303. }
  304. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  305. if (IS_ERR(res)) {
  306. error = PTR_ERR(res);
  307. goto err_mem_res_req;
  308. }
  309. regs = devm_ioremap_resource(&pdev->dev, res);
  310. if (IS_ERR(regs)) {
  311. error = PTR_ERR(regs);
  312. goto err_mem_res_req;
  313. }
  314. isp_scale->context.mmio_base = regs;
  315. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  316. if (IS_ERR(res)) {
  317. error = PTR_ERR(res);
  318. goto err_mem_res_req;
  319. }
  320. regs = ioremap(res->start, resource_size(res)); /* baseaddr conflict */
  321. if (IS_ERR(regs)) {
  322. error = PTR_ERR(regs);
  323. goto err_mem_res_req;
  324. }
  325. isp_scale->context.sys_base = regs;
  326. isp_scale->context.clk = devm_clk_get(&pdev->dev, NULL);
  327. if (IS_ERR(isp_scale->context.clk)) {
  328. dev_err(&pdev->dev, "Err: failed in getting isp_scale clock.\n");
  329. error = PTR_ERR(isp_scale->context.clk);
  330. goto err_init;
  331. }
  332. /* initialize hardware and data structure */
  333. error = ark_isp_scale_dev_init(&isp_scale->context);
  334. if (error != 0) {
  335. dev_err(&pdev->dev, "%s %d: dev init err\n",
  336. __FUNCTION__, __LINE__);
  337. goto err_init;
  338. }
  339. isp_scale->context.irq = platform_get_irq(pdev, 0);
  340. if (isp_scale->context.irq < 0) {
  341. dev_err(&pdev->dev, "%s %d: can't get irq resource.\n", __FUNCTION__, __LINE__);
  342. goto err_irq;
  343. }
  344. error = devm_request_irq(
  345. &pdev->dev,
  346. isp_scale->context.irq,
  347. ark_isp_scale_intr_handler,
  348. IRQF_SHARED, //SA_SHIRQ,
  349. "isp_scale",
  350. isp_scale
  351. );
  352. if (error) {
  353. dev_err(&pdev->dev, "%s %d: can't get assigned irq %d, error %d\n",
  354. __FUNCTION__, __LINE__, isp_scale->context.irq, error);
  355. goto err_irq;
  356. }
  357. init_waitqueue_head(&isp_scale->frame_finish_waitq);
  358. isp_scale->context.dev = &pdev->dev;
  359. platform_set_drvdata(pdev, isp_scale);
  360. return 0;
  361. err_irq:
  362. err_init:
  363. iounmap(isp_scale->context.sys_base);
  364. err_mem_res_req:
  365. cdev_del(&isp_scale->cdev);
  366. err_cdev_add:
  367. if (isp_scale->isp_scale_class) {
  368. if (isp_scale->isp_scale_device) {
  369. device_destroy(isp_scale->isp_scale_class, dev);
  370. }
  371. class_destroy(isp_scale->isp_scale_class);
  372. }
  373. unregister_chrdev_region(dev, isp_scale->num);
  374. err_driver_register:
  375. return error;
  376. }
  377. /* This function is invoked when the device module is removed from the
  378. * kernel. It releases all resources to the system.
  379. */
  380. static int ark_isp_scale_remove(struct platform_device *pdev)
  381. {
  382. struct ark_isp_scale_device *isp_scale;
  383. dev_t dev;
  384. isp_scale = platform_get_drvdata(pdev);
  385. if (isp_scale == NULL)
  386. return -ENODEV;
  387. iounmap(isp_scale->context.sys_base);
  388. dev = MKDEV(isp_scale->major, isp_scale->minor_start);
  389. cdev_del(&isp_scale->cdev);
  390. if (isp_scale->isp_scale_class) {
  391. if (isp_scale->isp_scale_device) {
  392. device_destroy(isp_scale->isp_scale_class, dev);
  393. }
  394. class_destroy(isp_scale->isp_scale_class);
  395. }
  396. unregister_chrdev_region(dev, isp_scale->num);
  397. return 0;
  398. }
  399. static const struct of_device_id isp_scale_of_match[] = {
  400. { .compatible = "arkmicro,isp-scale", },
  401. { }
  402. };
  403. MODULE_DEVICE_TABLE(of, isp_scale_of_match);
  404. static struct platform_driver ark_isp_scale_driver = {
  405. .driver = {
  406. .name = "isp_scale",
  407. .of_match_table = of_match_ptr(isp_scale_of_match),
  408. },
  409. .probe = ark_isp_scale_probe,
  410. .remove = ark_isp_scale_remove,
  411. };
  412. module_platform_driver(ark_isp_scale_driver);
  413. MODULE_AUTHOR("Sim");
  414. MODULE_DESCRIPTION("ArkMicro ISP Scale Driver");
  415. MODULE_LICENSE("GPL v2");