fimc-is.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. * Samsung EXYNOS4x12 FIMC-IS (Imaging Subsystem) driver
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  5. *
  6. * Authors: Sylwester Nawrocki <s.nawrocki@samsung.com>
  7. * Younghwan Joo <yhwan.joo@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
  14. #include <linux/device.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/delay.h>
  17. #include <linux/dma-contiguous.h>
  18. #include <linux/errno.h>
  19. #include <linux/firmware.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/i2c.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_graph.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/slab.h>
  31. #include <linux/types.h>
  32. #include <linux/videodev2.h>
  33. #include <media/videobuf2-dma-contig.h>
  34. #include "media-dev.h"
  35. #include "fimc-is.h"
  36. #include "fimc-is-command.h"
  37. #include "fimc-is-errno.h"
  38. #include "fimc-is-i2c.h"
  39. #include "fimc-is-param.h"
  40. #include "fimc-is-regs.h"
  41. static char *fimc_is_clocks[ISS_CLKS_MAX] = {
  42. [ISS_CLK_PPMUISPX] = "ppmuispx",
  43. [ISS_CLK_PPMUISPMX] = "ppmuispmx",
  44. [ISS_CLK_LITE0] = "lite0",
  45. [ISS_CLK_LITE1] = "lite1",
  46. [ISS_CLK_MPLL] = "mpll",
  47. [ISS_CLK_ISP] = "isp",
  48. [ISS_CLK_DRC] = "drc",
  49. [ISS_CLK_FD] = "fd",
  50. [ISS_CLK_MCUISP] = "mcuisp",
  51. [ISS_CLK_UART] = "uart",
  52. [ISS_CLK_ISP_DIV0] = "ispdiv0",
  53. [ISS_CLK_ISP_DIV1] = "ispdiv1",
  54. [ISS_CLK_MCUISP_DIV0] = "mcuispdiv0",
  55. [ISS_CLK_MCUISP_DIV1] = "mcuispdiv1",
  56. [ISS_CLK_ACLK200] = "aclk200",
  57. [ISS_CLK_ACLK200_DIV] = "div_aclk200",
  58. [ISS_CLK_ACLK400MCUISP] = "aclk400mcuisp",
  59. [ISS_CLK_ACLK400MCUISP_DIV] = "div_aclk400mcuisp",
  60. };
  61. static void fimc_is_put_clocks(struct fimc_is *is)
  62. {
  63. int i;
  64. for (i = 0; i < ISS_CLKS_MAX; i++) {
  65. if (IS_ERR(is->clocks[i]))
  66. continue;
  67. clk_put(is->clocks[i]);
  68. is->clocks[i] = ERR_PTR(-EINVAL);
  69. }
  70. }
  71. static int fimc_is_get_clocks(struct fimc_is *is)
  72. {
  73. int i, ret;
  74. for (i = 0; i < ISS_CLKS_MAX; i++)
  75. is->clocks[i] = ERR_PTR(-EINVAL);
  76. for (i = 0; i < ISS_CLKS_MAX; i++) {
  77. is->clocks[i] = clk_get(&is->pdev->dev, fimc_is_clocks[i]);
  78. if (IS_ERR(is->clocks[i])) {
  79. ret = PTR_ERR(is->clocks[i]);
  80. goto err;
  81. }
  82. }
  83. return 0;
  84. err:
  85. fimc_is_put_clocks(is);
  86. dev_err(&is->pdev->dev, "failed to get clock: %s\n",
  87. fimc_is_clocks[i]);
  88. return ret;
  89. }
  90. static int fimc_is_setup_clocks(struct fimc_is *is)
  91. {
  92. int ret;
  93. ret = clk_set_parent(is->clocks[ISS_CLK_ACLK200],
  94. is->clocks[ISS_CLK_ACLK200_DIV]);
  95. if (ret < 0)
  96. return ret;
  97. ret = clk_set_parent(is->clocks[ISS_CLK_ACLK400MCUISP],
  98. is->clocks[ISS_CLK_ACLK400MCUISP_DIV]);
  99. if (ret < 0)
  100. return ret;
  101. ret = clk_set_rate(is->clocks[ISS_CLK_ISP_DIV0], ACLK_AXI_FREQUENCY);
  102. if (ret < 0)
  103. return ret;
  104. ret = clk_set_rate(is->clocks[ISS_CLK_ISP_DIV1], ACLK_AXI_FREQUENCY);
  105. if (ret < 0)
  106. return ret;
  107. ret = clk_set_rate(is->clocks[ISS_CLK_MCUISP_DIV0],
  108. ATCLK_MCUISP_FREQUENCY);
  109. if (ret < 0)
  110. return ret;
  111. return clk_set_rate(is->clocks[ISS_CLK_MCUISP_DIV1],
  112. ATCLK_MCUISP_FREQUENCY);
  113. }
  114. static int fimc_is_enable_clocks(struct fimc_is *is)
  115. {
  116. int i, ret;
  117. for (i = 0; i < ISS_GATE_CLKS_MAX; i++) {
  118. if (IS_ERR(is->clocks[i]))
  119. continue;
  120. ret = clk_prepare_enable(is->clocks[i]);
  121. if (ret < 0) {
  122. dev_err(&is->pdev->dev, "clock %s enable failed\n",
  123. fimc_is_clocks[i]);
  124. for (--i; i >= 0; i--)
  125. clk_disable(is->clocks[i]);
  126. return ret;
  127. }
  128. pr_debug("enabled clock: %s\n", fimc_is_clocks[i]);
  129. }
  130. return 0;
  131. }
  132. static void fimc_is_disable_clocks(struct fimc_is *is)
  133. {
  134. int i;
  135. for (i = 0; i < ISS_GATE_CLKS_MAX; i++) {
  136. if (!IS_ERR(is->clocks[i])) {
  137. clk_disable_unprepare(is->clocks[i]);
  138. pr_debug("disabled clock: %s\n", fimc_is_clocks[i]);
  139. }
  140. }
  141. }
  142. static int fimc_is_parse_sensor_config(struct fimc_is *is, unsigned int index,
  143. struct device_node *node)
  144. {
  145. struct fimc_is_sensor *sensor = &is->sensor[index];
  146. u32 tmp = 0;
  147. int ret;
  148. sensor->drvdata = fimc_is_sensor_get_drvdata(node);
  149. if (!sensor->drvdata) {
  150. dev_err(&is->pdev->dev, "no driver data found for: %s\n",
  151. node->full_name);
  152. return -EINVAL;
  153. }
  154. node = of_graph_get_next_endpoint(node, NULL);
  155. if (!node)
  156. return -ENXIO;
  157. node = of_graph_get_remote_port(node);
  158. if (!node)
  159. return -ENXIO;
  160. /* Use MIPI-CSIS channel id to determine the ISP I2C bus index. */
  161. ret = of_property_read_u32(node, "reg", &tmp);
  162. if (ret < 0) {
  163. dev_err(&is->pdev->dev, "reg property not found at: %s\n",
  164. node->full_name);
  165. return ret;
  166. }
  167. sensor->i2c_bus = tmp - FIMC_INPUT_MIPI_CSI2_0;
  168. return 0;
  169. }
  170. static int fimc_is_register_subdevs(struct fimc_is *is)
  171. {
  172. struct device_node *i2c_bus, *child;
  173. int ret, index = 0;
  174. ret = fimc_isp_subdev_create(&is->isp);
  175. if (ret < 0)
  176. return ret;
  177. /* Initialize memory allocator context for the ISP DMA. */
  178. is->isp.alloc_ctx = is->alloc_ctx;
  179. for_each_compatible_node(i2c_bus, NULL, FIMC_IS_I2C_COMPATIBLE) {
  180. for_each_available_child_of_node(i2c_bus, child) {
  181. ret = fimc_is_parse_sensor_config(is, index, child);
  182. if (ret < 0 || index >= FIMC_IS_SENSORS_NUM) {
  183. of_node_put(child);
  184. return ret;
  185. }
  186. index++;
  187. }
  188. }
  189. return 0;
  190. }
  191. static int fimc_is_unregister_subdevs(struct fimc_is *is)
  192. {
  193. fimc_isp_subdev_destroy(&is->isp);
  194. return 0;
  195. }
  196. static int fimc_is_load_setfile(struct fimc_is *is, char *file_name)
  197. {
  198. const struct firmware *fw;
  199. void *buf;
  200. int ret;
  201. ret = request_firmware(&fw, file_name, &is->pdev->dev);
  202. if (ret < 0) {
  203. dev_err(&is->pdev->dev, "firmware request failed (%d)\n", ret);
  204. return ret;
  205. }
  206. buf = is->memory.vaddr + is->setfile.base;
  207. memcpy(buf, fw->data, fw->size);
  208. fimc_is_mem_barrier();
  209. is->setfile.size = fw->size;
  210. pr_debug("mem vaddr: %p, setfile buf: %p\n", is->memory.vaddr, buf);
  211. memcpy(is->fw.setfile_info,
  212. fw->data + fw->size - FIMC_IS_SETFILE_INFO_LEN,
  213. FIMC_IS_SETFILE_INFO_LEN - 1);
  214. is->fw.setfile_info[FIMC_IS_SETFILE_INFO_LEN - 1] = '\0';
  215. is->setfile.state = 1;
  216. pr_debug("FIMC-IS setfile loaded: base: %#x, size: %zu B\n",
  217. is->setfile.base, fw->size);
  218. release_firmware(fw);
  219. return ret;
  220. }
  221. int fimc_is_cpu_set_power(struct fimc_is *is, int on)
  222. {
  223. unsigned int timeout = FIMC_IS_POWER_ON_TIMEOUT;
  224. if (on) {
  225. /* Disable watchdog */
  226. mcuctl_write(0, is, REG_WDT_ISP);
  227. /* Cortex-A5 start address setting */
  228. mcuctl_write(is->memory.paddr, is, MCUCTL_REG_BBOAR);
  229. /* Enable and start Cortex-A5 */
  230. pmuisp_write(0x18000, is, REG_PMU_ISP_ARM_OPTION);
  231. pmuisp_write(0x1, is, REG_PMU_ISP_ARM_CONFIGURATION);
  232. } else {
  233. /* A5 power off */
  234. pmuisp_write(0x10000, is, REG_PMU_ISP_ARM_OPTION);
  235. pmuisp_write(0x0, is, REG_PMU_ISP_ARM_CONFIGURATION);
  236. while (pmuisp_read(is, REG_PMU_ISP_ARM_STATUS) & 1) {
  237. if (timeout == 0)
  238. return -ETIME;
  239. timeout--;
  240. udelay(1);
  241. }
  242. }
  243. return 0;
  244. }
  245. /* Wait until @bit of @is->state is set to @state in the interrupt handler. */
  246. int fimc_is_wait_event(struct fimc_is *is, unsigned long bit,
  247. unsigned int state, unsigned int timeout)
  248. {
  249. int ret = wait_event_timeout(is->irq_queue,
  250. !state ^ test_bit(bit, &is->state),
  251. timeout);
  252. if (ret == 0) {
  253. dev_WARN(&is->pdev->dev, "%s() timed out\n", __func__);
  254. return -ETIME;
  255. }
  256. return 0;
  257. }
  258. int fimc_is_start_firmware(struct fimc_is *is)
  259. {
  260. struct device *dev = &is->pdev->dev;
  261. int ret;
  262. if (is->fw.f_w == NULL) {
  263. dev_err(dev, "firmware is not loaded\n");
  264. return -EINVAL;
  265. }
  266. memcpy(is->memory.vaddr, is->fw.f_w->data, is->fw.f_w->size);
  267. wmb();
  268. ret = fimc_is_cpu_set_power(is, 1);
  269. if (ret < 0)
  270. return ret;
  271. ret = fimc_is_wait_event(is, IS_ST_A5_PWR_ON, 1,
  272. msecs_to_jiffies(FIMC_IS_FW_LOAD_TIMEOUT));
  273. if (ret < 0)
  274. dev_err(dev, "FIMC-IS CPU power on failed\n");
  275. return ret;
  276. }
  277. /* Allocate working memory for the FIMC-IS CPU. */
  278. static int fimc_is_alloc_cpu_memory(struct fimc_is *is)
  279. {
  280. struct device *dev = &is->pdev->dev;
  281. is->memory.vaddr = dma_alloc_coherent(dev, FIMC_IS_CPU_MEM_SIZE,
  282. &is->memory.paddr, GFP_KERNEL);
  283. if (is->memory.vaddr == NULL)
  284. return -ENOMEM;
  285. is->memory.size = FIMC_IS_CPU_MEM_SIZE;
  286. memset(is->memory.vaddr, 0, is->memory.size);
  287. dev_info(dev, "FIMC-IS CPU memory base: %#x\n", (u32)is->memory.paddr);
  288. if (((u32)is->memory.paddr) & FIMC_IS_FW_ADDR_MASK) {
  289. dev_err(dev, "invalid firmware memory alignment: %#x\n",
  290. (u32)is->memory.paddr);
  291. dma_free_coherent(dev, is->memory.size, is->memory.vaddr,
  292. is->memory.paddr);
  293. return -EIO;
  294. }
  295. is->is_p_region = (struct is_region *)(is->memory.vaddr +
  296. FIMC_IS_CPU_MEM_SIZE - FIMC_IS_REGION_SIZE);
  297. is->is_dma_p_region = is->memory.paddr +
  298. FIMC_IS_CPU_MEM_SIZE - FIMC_IS_REGION_SIZE;
  299. is->is_shared_region = (struct is_share_region *)(is->memory.vaddr +
  300. FIMC_IS_SHARED_REGION_OFFSET);
  301. return 0;
  302. }
  303. static void fimc_is_free_cpu_memory(struct fimc_is *is)
  304. {
  305. struct device *dev = &is->pdev->dev;
  306. if (is->memory.vaddr == NULL)
  307. return;
  308. dma_free_coherent(dev, is->memory.size, is->memory.vaddr,
  309. is->memory.paddr);
  310. }
  311. static void fimc_is_load_firmware(const struct firmware *fw, void *context)
  312. {
  313. struct fimc_is *is = context;
  314. struct device *dev = &is->pdev->dev;
  315. void *buf;
  316. int ret;
  317. if (fw == NULL) {
  318. dev_err(dev, "firmware request failed\n");
  319. return;
  320. }
  321. mutex_lock(&is->lock);
  322. if (fw->size < FIMC_IS_FW_SIZE_MIN || fw->size > FIMC_IS_FW_SIZE_MAX) {
  323. dev_err(dev, "wrong firmware size: %zu\n", fw->size);
  324. goto done;
  325. }
  326. is->fw.size = fw->size;
  327. ret = fimc_is_alloc_cpu_memory(is);
  328. if (ret < 0) {
  329. dev_err(dev, "failed to allocate FIMC-IS CPU memory\n");
  330. goto done;
  331. }
  332. memcpy(is->memory.vaddr, fw->data, fw->size);
  333. wmb();
  334. /* Read firmware description. */
  335. buf = (void *)(is->memory.vaddr + fw->size - FIMC_IS_FW_DESC_LEN);
  336. memcpy(&is->fw.info, buf, FIMC_IS_FW_INFO_LEN);
  337. is->fw.info[FIMC_IS_FW_INFO_LEN] = 0;
  338. buf = (void *)(is->memory.vaddr + fw->size - FIMC_IS_FW_VER_LEN);
  339. memcpy(&is->fw.version, buf, FIMC_IS_FW_VER_LEN);
  340. is->fw.version[FIMC_IS_FW_VER_LEN - 1] = 0;
  341. is->fw.state = 1;
  342. dev_info(dev, "loaded firmware: %s, rev. %s\n",
  343. is->fw.info, is->fw.version);
  344. dev_dbg(dev, "FW size: %zu, paddr: %pad\n", fw->size, &is->memory.paddr);
  345. is->is_shared_region->chip_id = 0xe4412;
  346. is->is_shared_region->chip_rev_no = 1;
  347. fimc_is_mem_barrier();
  348. /*
  349. * FIXME: The firmware is not being released for now, as it is
  350. * needed around for copying to the IS working memory every
  351. * time before the Cortex-A5 is restarted.
  352. */
  353. release_firmware(is->fw.f_w);
  354. is->fw.f_w = fw;
  355. done:
  356. mutex_unlock(&is->lock);
  357. }
  358. static int fimc_is_request_firmware(struct fimc_is *is, const char *fw_name)
  359. {
  360. return request_firmware_nowait(THIS_MODULE,
  361. FW_ACTION_HOTPLUG, fw_name, &is->pdev->dev,
  362. GFP_KERNEL, is, fimc_is_load_firmware);
  363. }
  364. /* General IS interrupt handler */
  365. static void fimc_is_general_irq_handler(struct fimc_is *is)
  366. {
  367. is->i2h_cmd.cmd = mcuctl_read(is, MCUCTL_REG_ISSR(10));
  368. switch (is->i2h_cmd.cmd) {
  369. case IHC_GET_SENSOR_NUM:
  370. fimc_is_hw_get_params(is, 1);
  371. fimc_is_hw_wait_intmsr0_intmsd0(is);
  372. fimc_is_hw_set_sensor_num(is);
  373. pr_debug("ISP FW version: %#x\n", is->i2h_cmd.args[0]);
  374. break;
  375. case IHC_SET_FACE_MARK:
  376. case IHC_FRAME_DONE:
  377. fimc_is_hw_get_params(is, 2);
  378. break;
  379. case IHC_SET_SHOT_MARK:
  380. case IHC_AA_DONE:
  381. case IH_REPLY_DONE:
  382. fimc_is_hw_get_params(is, 3);
  383. break;
  384. case IH_REPLY_NOT_DONE:
  385. fimc_is_hw_get_params(is, 4);
  386. break;
  387. case IHC_NOT_READY:
  388. break;
  389. default:
  390. pr_info("unknown command: %#x\n", is->i2h_cmd.cmd);
  391. }
  392. fimc_is_fw_clear_irq1(is, FIMC_IS_INT_GENERAL);
  393. switch (is->i2h_cmd.cmd) {
  394. case IHC_GET_SENSOR_NUM:
  395. fimc_is_hw_set_intgr0_gd0(is);
  396. set_bit(IS_ST_A5_PWR_ON, &is->state);
  397. break;
  398. case IHC_SET_SHOT_MARK:
  399. break;
  400. case IHC_SET_FACE_MARK:
  401. is->fd_header.count = is->i2h_cmd.args[0];
  402. is->fd_header.index = is->i2h_cmd.args[1];
  403. is->fd_header.offset = 0;
  404. break;
  405. case IHC_FRAME_DONE:
  406. break;
  407. case IHC_AA_DONE:
  408. pr_debug("AA_DONE - %d, %d, %d\n", is->i2h_cmd.args[0],
  409. is->i2h_cmd.args[1], is->i2h_cmd.args[2]);
  410. break;
  411. case IH_REPLY_DONE:
  412. pr_debug("ISR_DONE: args[0]: %#x\n", is->i2h_cmd.args[0]);
  413. switch (is->i2h_cmd.args[0]) {
  414. case HIC_PREVIEW_STILL...HIC_CAPTURE_VIDEO:
  415. /* Get CAC margin */
  416. set_bit(IS_ST_CHANGE_MODE, &is->state);
  417. is->isp.cac_margin_x = is->i2h_cmd.args[1];
  418. is->isp.cac_margin_y = is->i2h_cmd.args[2];
  419. pr_debug("CAC margin (x,y): (%d,%d)\n",
  420. is->isp.cac_margin_x, is->isp.cac_margin_y);
  421. break;
  422. case HIC_STREAM_ON:
  423. clear_bit(IS_ST_STREAM_OFF, &is->state);
  424. set_bit(IS_ST_STREAM_ON, &is->state);
  425. break;
  426. case HIC_STREAM_OFF:
  427. clear_bit(IS_ST_STREAM_ON, &is->state);
  428. set_bit(IS_ST_STREAM_OFF, &is->state);
  429. break;
  430. case HIC_SET_PARAMETER:
  431. is->config[is->config_index].p_region_index[0] = 0;
  432. is->config[is->config_index].p_region_index[1] = 0;
  433. set_bit(IS_ST_BLOCK_CMD_CLEARED, &is->state);
  434. pr_debug("HIC_SET_PARAMETER\n");
  435. break;
  436. case HIC_GET_PARAMETER:
  437. break;
  438. case HIC_SET_TUNE:
  439. break;
  440. case HIC_GET_STATUS:
  441. break;
  442. case HIC_OPEN_SENSOR:
  443. set_bit(IS_ST_OPEN_SENSOR, &is->state);
  444. pr_debug("data lanes: %d, settle line: %d\n",
  445. is->i2h_cmd.args[2], is->i2h_cmd.args[1]);
  446. break;
  447. case HIC_CLOSE_SENSOR:
  448. clear_bit(IS_ST_OPEN_SENSOR, &is->state);
  449. is->sensor_index = 0;
  450. break;
  451. case HIC_MSG_TEST:
  452. pr_debug("config MSG level completed\n");
  453. break;
  454. case HIC_POWER_DOWN:
  455. clear_bit(IS_ST_PWR_SUBIP_ON, &is->state);
  456. break;
  457. case HIC_GET_SET_FILE_ADDR:
  458. is->setfile.base = is->i2h_cmd.args[1];
  459. set_bit(IS_ST_SETFILE_LOADED, &is->state);
  460. break;
  461. case HIC_LOAD_SET_FILE:
  462. set_bit(IS_ST_SETFILE_LOADED, &is->state);
  463. break;
  464. }
  465. break;
  466. case IH_REPLY_NOT_DONE:
  467. pr_err("ISR_NDONE: %d: %#x, %s\n", is->i2h_cmd.args[0],
  468. is->i2h_cmd.args[1],
  469. fimc_is_strerr(is->i2h_cmd.args[1]));
  470. if (is->i2h_cmd.args[1] & IS_ERROR_TIME_OUT_FLAG)
  471. pr_err("IS_ERROR_TIME_OUT\n");
  472. switch (is->i2h_cmd.args[1]) {
  473. case IS_ERROR_SET_PARAMETER:
  474. fimc_is_mem_barrier();
  475. }
  476. switch (is->i2h_cmd.args[0]) {
  477. case HIC_SET_PARAMETER:
  478. is->config[is->config_index].p_region_index[0] = 0;
  479. is->config[is->config_index].p_region_index[1] = 0;
  480. set_bit(IS_ST_BLOCK_CMD_CLEARED, &is->state);
  481. break;
  482. }
  483. break;
  484. case IHC_NOT_READY:
  485. pr_err("IS control sequence error: Not Ready\n");
  486. break;
  487. }
  488. wake_up(&is->irq_queue);
  489. }
  490. static irqreturn_t fimc_is_irq_handler(int irq, void *priv)
  491. {
  492. struct fimc_is *is = priv;
  493. unsigned long flags;
  494. u32 status;
  495. spin_lock_irqsave(&is->slock, flags);
  496. status = mcuctl_read(is, MCUCTL_REG_INTSR1);
  497. if (status & (1UL << FIMC_IS_INT_GENERAL))
  498. fimc_is_general_irq_handler(is);
  499. if (status & (1UL << FIMC_IS_INT_FRAME_DONE_ISP))
  500. fimc_isp_irq_handler(is);
  501. spin_unlock_irqrestore(&is->slock, flags);
  502. return IRQ_HANDLED;
  503. }
  504. static int fimc_is_hw_open_sensor(struct fimc_is *is,
  505. struct fimc_is_sensor *sensor)
  506. {
  507. struct sensor_open_extended *soe = (void *)&is->is_p_region->shared;
  508. fimc_is_hw_wait_intmsr0_intmsd0(is);
  509. soe->self_calibration_mode = 1;
  510. soe->actuator_type = 0;
  511. soe->mipi_lane_num = 0;
  512. soe->mclk = 0;
  513. soe->mipi_speed = 0;
  514. soe->fast_open_sensor = 0;
  515. soe->i2c_sclk = 88000000;
  516. fimc_is_mem_barrier();
  517. mcuctl_write(HIC_OPEN_SENSOR, is, MCUCTL_REG_ISSR(0));
  518. mcuctl_write(is->sensor_index, is, MCUCTL_REG_ISSR(1));
  519. mcuctl_write(sensor->drvdata->id, is, MCUCTL_REG_ISSR(2));
  520. mcuctl_write(sensor->i2c_bus, is, MCUCTL_REG_ISSR(3));
  521. mcuctl_write(is->is_dma_p_region, is, MCUCTL_REG_ISSR(4));
  522. fimc_is_hw_set_intgr0_gd0(is);
  523. return fimc_is_wait_event(is, IS_ST_OPEN_SENSOR, 1,
  524. sensor->drvdata->open_timeout);
  525. }
  526. int fimc_is_hw_initialize(struct fimc_is *is)
  527. {
  528. const int config_ids[] = {
  529. IS_SC_PREVIEW_STILL, IS_SC_PREVIEW_VIDEO,
  530. IS_SC_CAPTURE_STILL, IS_SC_CAPTURE_VIDEO
  531. };
  532. struct device *dev = &is->pdev->dev;
  533. u32 prev_id;
  534. int i, ret;
  535. /* Sensor initialization. Only one sensor is currently supported. */
  536. ret = fimc_is_hw_open_sensor(is, &is->sensor[0]);
  537. if (ret < 0)
  538. return ret;
  539. /* Get the setfile address. */
  540. fimc_is_hw_get_setfile_addr(is);
  541. ret = fimc_is_wait_event(is, IS_ST_SETFILE_LOADED, 1,
  542. FIMC_IS_CONFIG_TIMEOUT);
  543. if (ret < 0) {
  544. dev_err(dev, "get setfile address timed out\n");
  545. return ret;
  546. }
  547. pr_debug("setfile.base: %#x\n", is->setfile.base);
  548. /* Load the setfile. */
  549. fimc_is_load_setfile(is, FIMC_IS_SETFILE_6A3);
  550. clear_bit(IS_ST_SETFILE_LOADED, &is->state);
  551. fimc_is_hw_load_setfile(is);
  552. ret = fimc_is_wait_event(is, IS_ST_SETFILE_LOADED, 1,
  553. FIMC_IS_CONFIG_TIMEOUT);
  554. if (ret < 0) {
  555. dev_err(dev, "loading setfile timed out\n");
  556. return ret;
  557. }
  558. pr_debug("setfile: base: %#x, size: %d\n",
  559. is->setfile.base, is->setfile.size);
  560. pr_info("FIMC-IS Setfile info: %s\n", is->fw.setfile_info);
  561. /* Check magic number. */
  562. if (is->is_p_region->shared[MAX_SHARED_COUNT - 1] !=
  563. FIMC_IS_MAGIC_NUMBER) {
  564. dev_err(dev, "magic number error!\n");
  565. return -EIO;
  566. }
  567. pr_debug("shared region: %pad, parameter region: %pad\n",
  568. &is->memory.paddr + FIMC_IS_SHARED_REGION_OFFSET,
  569. &is->is_dma_p_region);
  570. is->setfile.sub_index = 0;
  571. /* Stream off. */
  572. fimc_is_hw_stream_off(is);
  573. ret = fimc_is_wait_event(is, IS_ST_STREAM_OFF, 1,
  574. FIMC_IS_CONFIG_TIMEOUT);
  575. if (ret < 0) {
  576. dev_err(dev, "stream off timeout\n");
  577. return ret;
  578. }
  579. /* Preserve previous mode. */
  580. prev_id = is->config_index;
  581. /* Set initial parameter values. */
  582. for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
  583. is->config_index = config_ids[i];
  584. fimc_is_set_initial_params(is);
  585. ret = fimc_is_itf_s_param(is, true);
  586. if (ret < 0) {
  587. is->config_index = prev_id;
  588. return ret;
  589. }
  590. }
  591. is->config_index = prev_id;
  592. set_bit(IS_ST_INIT_DONE, &is->state);
  593. dev_info(dev, "initialization sequence completed (%d)\n",
  594. is->config_index);
  595. return 0;
  596. }
  597. static int fimc_is_log_show(struct seq_file *s, void *data)
  598. {
  599. struct fimc_is *is = s->private;
  600. const u8 *buf = is->memory.vaddr + FIMC_IS_DEBUG_REGION_OFFSET;
  601. if (is->memory.vaddr == NULL) {
  602. dev_err(&is->pdev->dev, "firmware memory is not initialized\n");
  603. return -EIO;
  604. }
  605. seq_printf(s, "%s\n", buf);
  606. return 0;
  607. }
  608. static int fimc_is_debugfs_open(struct inode *inode, struct file *file)
  609. {
  610. return single_open(file, fimc_is_log_show, inode->i_private);
  611. }
  612. static const struct file_operations fimc_is_debugfs_fops = {
  613. .open = fimc_is_debugfs_open,
  614. .read = seq_read,
  615. .llseek = seq_lseek,
  616. .release = single_release,
  617. };
  618. static void fimc_is_debugfs_remove(struct fimc_is *is)
  619. {
  620. debugfs_remove_recursive(is->debugfs_entry);
  621. is->debugfs_entry = NULL;
  622. }
  623. static int fimc_is_debugfs_create(struct fimc_is *is)
  624. {
  625. struct dentry *dentry;
  626. is->debugfs_entry = debugfs_create_dir("fimc_is", NULL);
  627. dentry = debugfs_create_file("fw_log", S_IRUGO, is->debugfs_entry,
  628. is, &fimc_is_debugfs_fops);
  629. if (!dentry)
  630. fimc_is_debugfs_remove(is);
  631. return is->debugfs_entry == NULL ? -EIO : 0;
  632. }
  633. static int fimc_is_runtime_resume(struct device *dev);
  634. static int fimc_is_runtime_suspend(struct device *dev);
  635. static int fimc_is_probe(struct platform_device *pdev)
  636. {
  637. struct device *dev = &pdev->dev;
  638. struct fimc_is *is;
  639. struct resource res;
  640. struct device_node *node;
  641. int ret;
  642. is = devm_kzalloc(&pdev->dev, sizeof(*is), GFP_KERNEL);
  643. if (!is)
  644. return -ENOMEM;
  645. is->pdev = pdev;
  646. is->isp.pdev = pdev;
  647. init_waitqueue_head(&is->irq_queue);
  648. spin_lock_init(&is->slock);
  649. mutex_init(&is->lock);
  650. ret = of_address_to_resource(dev->of_node, 0, &res);
  651. if (ret < 0)
  652. return ret;
  653. is->regs = devm_ioremap_resource(dev, &res);
  654. if (IS_ERR(is->regs))
  655. return PTR_ERR(is->regs);
  656. node = of_get_child_by_name(dev->of_node, "pmu");
  657. if (!node)
  658. return -ENODEV;
  659. is->pmu_regs = of_iomap(node, 0);
  660. if (!is->pmu_regs)
  661. return -ENOMEM;
  662. is->irq = irq_of_parse_and_map(dev->of_node, 0);
  663. if (!is->irq) {
  664. dev_err(dev, "no irq found\n");
  665. ret = -EINVAL;
  666. goto err_iounmap;
  667. }
  668. ret = fimc_is_get_clocks(is);
  669. if (ret < 0)
  670. goto err_iounmap;
  671. platform_set_drvdata(pdev, is);
  672. ret = request_irq(is->irq, fimc_is_irq_handler, 0, dev_name(dev), is);
  673. if (ret < 0) {
  674. dev_err(dev, "irq request failed\n");
  675. goto err_clk;
  676. }
  677. pm_runtime_enable(dev);
  678. if (!pm_runtime_enabled(dev)) {
  679. ret = fimc_is_runtime_resume(dev);
  680. if (ret < 0)
  681. goto err_irq;
  682. }
  683. ret = pm_runtime_get_sync(dev);
  684. if (ret < 0)
  685. goto err_pm;
  686. is->alloc_ctx = vb2_dma_contig_init_ctx(dev);
  687. if (IS_ERR(is->alloc_ctx)) {
  688. ret = PTR_ERR(is->alloc_ctx);
  689. goto err_pm;
  690. }
  691. /*
  692. * Register FIMC-IS V4L2 subdevs to this driver. The video nodes
  693. * will be created within the subdev's registered() callback.
  694. */
  695. ret = fimc_is_register_subdevs(is);
  696. if (ret < 0)
  697. goto err_vb;
  698. ret = fimc_is_debugfs_create(is);
  699. if (ret < 0)
  700. goto err_sd;
  701. ret = fimc_is_request_firmware(is, FIMC_IS_FW_FILENAME);
  702. if (ret < 0)
  703. goto err_dfs;
  704. pm_runtime_put_sync(dev);
  705. dev_dbg(dev, "FIMC-IS registered successfully\n");
  706. return 0;
  707. err_dfs:
  708. fimc_is_debugfs_remove(is);
  709. err_sd:
  710. fimc_is_unregister_subdevs(is);
  711. err_vb:
  712. vb2_dma_contig_cleanup_ctx(is->alloc_ctx);
  713. err_pm:
  714. if (!pm_runtime_enabled(dev))
  715. fimc_is_runtime_suspend(dev);
  716. err_irq:
  717. free_irq(is->irq, is);
  718. err_clk:
  719. fimc_is_put_clocks(is);
  720. err_iounmap:
  721. iounmap(is->pmu_regs);
  722. return ret;
  723. }
  724. static int fimc_is_runtime_resume(struct device *dev)
  725. {
  726. struct fimc_is *is = dev_get_drvdata(dev);
  727. int ret;
  728. ret = fimc_is_setup_clocks(is);
  729. if (ret)
  730. return ret;
  731. return fimc_is_enable_clocks(is);
  732. }
  733. static int fimc_is_runtime_suspend(struct device *dev)
  734. {
  735. struct fimc_is *is = dev_get_drvdata(dev);
  736. fimc_is_disable_clocks(is);
  737. return 0;
  738. }
  739. #ifdef CONFIG_PM_SLEEP
  740. static int fimc_is_resume(struct device *dev)
  741. {
  742. /* TODO: */
  743. return 0;
  744. }
  745. static int fimc_is_suspend(struct device *dev)
  746. {
  747. struct fimc_is *is = dev_get_drvdata(dev);
  748. /* TODO: */
  749. if (test_bit(IS_ST_A5_PWR_ON, &is->state))
  750. return -EBUSY;
  751. return 0;
  752. }
  753. #endif /* CONFIG_PM_SLEEP */
  754. static int fimc_is_remove(struct platform_device *pdev)
  755. {
  756. struct device *dev = &pdev->dev;
  757. struct fimc_is *is = dev_get_drvdata(dev);
  758. pm_runtime_disable(dev);
  759. pm_runtime_set_suspended(dev);
  760. if (!pm_runtime_status_suspended(dev))
  761. fimc_is_runtime_suspend(dev);
  762. free_irq(is->irq, is);
  763. fimc_is_unregister_subdevs(is);
  764. vb2_dma_contig_cleanup_ctx(is->alloc_ctx);
  765. fimc_is_put_clocks(is);
  766. iounmap(is->pmu_regs);
  767. fimc_is_debugfs_remove(is);
  768. release_firmware(is->fw.f_w);
  769. fimc_is_free_cpu_memory(is);
  770. return 0;
  771. }
  772. static const struct of_device_id fimc_is_of_match[] = {
  773. { .compatible = "samsung,exynos4212-fimc-is" },
  774. { /* sentinel */ },
  775. };
  776. MODULE_DEVICE_TABLE(of, fimc_is_of_match);
  777. static const struct dev_pm_ops fimc_is_pm_ops = {
  778. SET_SYSTEM_SLEEP_PM_OPS(fimc_is_suspend, fimc_is_resume)
  779. SET_RUNTIME_PM_OPS(fimc_is_runtime_suspend, fimc_is_runtime_resume,
  780. NULL)
  781. };
  782. static struct platform_driver fimc_is_driver = {
  783. .probe = fimc_is_probe,
  784. .remove = fimc_is_remove,
  785. .driver = {
  786. .of_match_table = fimc_is_of_match,
  787. .name = FIMC_IS_DRV_NAME,
  788. .pm = &fimc_is_pm_ops,
  789. }
  790. };
  791. static int fimc_is_module_init(void)
  792. {
  793. int ret;
  794. ret = fimc_is_register_i2c_driver();
  795. if (ret < 0)
  796. return ret;
  797. ret = platform_driver_register(&fimc_is_driver);
  798. if (ret < 0)
  799. fimc_is_unregister_i2c_driver();
  800. return ret;
  801. }
  802. static void fimc_is_module_exit(void)
  803. {
  804. fimc_is_unregister_i2c_driver();
  805. platform_driver_unregister(&fimc_is_driver);
  806. }
  807. module_init(fimc_is_module_init);
  808. module_exit(fimc_is_module_exit);
  809. MODULE_ALIAS("platform:" FIMC_IS_DRV_NAME);
  810. MODULE_AUTHOR("Younghwan Joo <yhwan.joo@samsung.com>");
  811. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  812. MODULE_LICENSE("GPL v2");