mipi-csis.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. * Samsung S5P/EXYNOS4 SoC series MIPI-CSI receiver driver
  3. *
  4. * Copyright (C) 2011 Samsung Electronics Co., Ltd.
  5. * Contact: Sylwester Nawrocki, <s.nawrocki@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/irq.h>
  18. #include <linux/kernel.h>
  19. #include <linux/memory.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/videodev2.h>
  27. #include <media/v4l2-subdev.h>
  28. #include <plat/mipi_csis.h>
  29. #include "mipi-csis.h"
  30. static int debug;
  31. module_param(debug, int, 0644);
  32. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  33. /* Register map definition */
  34. /* CSIS global control */
  35. #define S5PCSIS_CTRL 0x00
  36. #define S5PCSIS_CTRL_DPDN_DEFAULT (0 << 31)
  37. #define S5PCSIS_CTRL_DPDN_SWAP (1 << 31)
  38. #define S5PCSIS_CTRL_ALIGN_32BIT (1 << 20)
  39. #define S5PCSIS_CTRL_UPDATE_SHADOW (1 << 16)
  40. #define S5PCSIS_CTRL_WCLK_EXTCLK (1 << 8)
  41. #define S5PCSIS_CTRL_RESET (1 << 4)
  42. #define S5PCSIS_CTRL_ENABLE (1 << 0)
  43. /* D-PHY control */
  44. #define S5PCSIS_DPHYCTRL 0x04
  45. #define S5PCSIS_DPHYCTRL_HSS_MASK (0x1f << 27)
  46. #define S5PCSIS_DPHYCTRL_ENABLE (0x1f << 0)
  47. #define S5PCSIS_CONFIG 0x08
  48. #define S5PCSIS_CFG_FMT_YCBCR422_8BIT (0x1e << 2)
  49. #define S5PCSIS_CFG_FMT_RAW8 (0x2a << 2)
  50. #define S5PCSIS_CFG_FMT_RAW10 (0x2b << 2)
  51. #define S5PCSIS_CFG_FMT_RAW12 (0x2c << 2)
  52. /* User defined formats, x = 1...4 */
  53. #define S5PCSIS_CFG_FMT_USER(x) ((0x30 + x - 1) << 2)
  54. #define S5PCSIS_CFG_FMT_MASK (0x3f << 2)
  55. #define S5PCSIS_CFG_NR_LANE_MASK 3
  56. /* Interrupt mask. */
  57. #define S5PCSIS_INTMSK 0x10
  58. #define S5PCSIS_INTMSK_EN_ALL 0xf000003f
  59. #define S5PCSIS_INTSRC 0x14
  60. /* Pixel resolution */
  61. #define S5PCSIS_RESOL 0x2c
  62. #define CSIS_MAX_PIX_WIDTH 0xffff
  63. #define CSIS_MAX_PIX_HEIGHT 0xffff
  64. enum {
  65. CSIS_CLK_MUX,
  66. CSIS_CLK_GATE,
  67. };
  68. static char *csi_clock_name[] = {
  69. [CSIS_CLK_MUX] = "sclk_csis",
  70. [CSIS_CLK_GATE] = "csis",
  71. };
  72. #define NUM_CSIS_CLOCKS ARRAY_SIZE(csi_clock_name)
  73. enum {
  74. ST_POWERED = 1,
  75. ST_STREAMING = 2,
  76. ST_SUSPENDED = 4,
  77. };
  78. /**
  79. * struct csis_state - the driver's internal state data structure
  80. * @lock: mutex serializing the subdev and power management operations,
  81. * protecting @format and @flags members
  82. * @pads: CSIS pads array
  83. * @sd: v4l2_subdev associated with CSIS device instance
  84. * @pdev: CSIS platform device
  85. * @regs_res: requested I/O register memory resource
  86. * @regs: mmaped I/O registers memory
  87. * @clock: CSIS clocks
  88. * @irq: requested s5p-mipi-csis irq number
  89. * @flags: the state variable for power and streaming control
  90. * @csis_fmt: current CSIS pixel format
  91. * @format: common media bus format for the source and sink pad
  92. */
  93. struct csis_state {
  94. struct mutex lock;
  95. struct media_pad pads[CSIS_PADS_NUM];
  96. struct v4l2_subdev sd;
  97. struct platform_device *pdev;
  98. struct resource *regs_res;
  99. void __iomem *regs;
  100. struct clk *clock[NUM_CSIS_CLOCKS];
  101. int irq;
  102. struct regulator *supply;
  103. u32 flags;
  104. const struct csis_pix_format *csis_fmt;
  105. struct v4l2_mbus_framefmt format;
  106. };
  107. /**
  108. * struct csis_pix_format - CSIS pixel format description
  109. * @pix_width_alignment: horizontal pixel alignment, width will be
  110. * multiple of 2^pix_width_alignment
  111. * @code: corresponding media bus code
  112. * @fmt_reg: S5PCSIS_CONFIG register value
  113. */
  114. struct csis_pix_format {
  115. unsigned int pix_width_alignment;
  116. enum v4l2_mbus_pixelcode code;
  117. u32 fmt_reg;
  118. };
  119. static const struct csis_pix_format s5pcsis_formats[] = {
  120. {
  121. .code = V4L2_MBUS_FMT_VYUY8_2X8,
  122. .fmt_reg = S5PCSIS_CFG_FMT_YCBCR422_8BIT,
  123. }, {
  124. .code = V4L2_MBUS_FMT_JPEG_1X8,
  125. .fmt_reg = S5PCSIS_CFG_FMT_USER(1),
  126. },
  127. };
  128. #define s5pcsis_write(__csis, __r, __v) writel(__v, __csis->regs + __r)
  129. #define s5pcsis_read(__csis, __r) readl(__csis->regs + __r)
  130. static struct csis_state *sd_to_csis_state(struct v4l2_subdev *sdev)
  131. {
  132. return container_of(sdev, struct csis_state, sd);
  133. }
  134. static const struct csis_pix_format *find_csis_format(
  135. struct v4l2_mbus_framefmt *mf)
  136. {
  137. int i;
  138. for (i = 0; i < ARRAY_SIZE(s5pcsis_formats); i++)
  139. if (mf->code == s5pcsis_formats[i].code)
  140. return &s5pcsis_formats[i];
  141. return NULL;
  142. }
  143. static void s5pcsis_enable_interrupts(struct csis_state *state, bool on)
  144. {
  145. u32 val = s5pcsis_read(state, S5PCSIS_INTMSK);
  146. val = on ? val | S5PCSIS_INTMSK_EN_ALL :
  147. val & ~S5PCSIS_INTMSK_EN_ALL;
  148. s5pcsis_write(state, S5PCSIS_INTMSK, val);
  149. }
  150. static void s5pcsis_reset(struct csis_state *state)
  151. {
  152. u32 val = s5pcsis_read(state, S5PCSIS_CTRL);
  153. s5pcsis_write(state, S5PCSIS_CTRL, val | S5PCSIS_CTRL_RESET);
  154. udelay(10);
  155. }
  156. static void s5pcsis_system_enable(struct csis_state *state, int on)
  157. {
  158. u32 val;
  159. val = s5pcsis_read(state, S5PCSIS_CTRL);
  160. if (on)
  161. val |= S5PCSIS_CTRL_ENABLE;
  162. else
  163. val &= ~S5PCSIS_CTRL_ENABLE;
  164. s5pcsis_write(state, S5PCSIS_CTRL, val);
  165. val = s5pcsis_read(state, S5PCSIS_DPHYCTRL);
  166. if (on)
  167. val |= S5PCSIS_DPHYCTRL_ENABLE;
  168. else
  169. val &= ~S5PCSIS_DPHYCTRL_ENABLE;
  170. s5pcsis_write(state, S5PCSIS_DPHYCTRL, val);
  171. }
  172. /* Called with the state.lock mutex held */
  173. static void __s5pcsis_set_format(struct csis_state *state)
  174. {
  175. struct v4l2_mbus_framefmt *mf = &state->format;
  176. u32 val;
  177. v4l2_dbg(1, debug, &state->sd, "fmt: %d, %d x %d\n",
  178. mf->code, mf->width, mf->height);
  179. /* Color format */
  180. val = s5pcsis_read(state, S5PCSIS_CONFIG);
  181. val = (val & ~S5PCSIS_CFG_FMT_MASK) | state->csis_fmt->fmt_reg;
  182. s5pcsis_write(state, S5PCSIS_CONFIG, val);
  183. /* Pixel resolution */
  184. val = (mf->width << 16) | mf->height;
  185. s5pcsis_write(state, S5PCSIS_RESOL, val);
  186. }
  187. static void s5pcsis_set_hsync_settle(struct csis_state *state, int settle)
  188. {
  189. u32 val = s5pcsis_read(state, S5PCSIS_DPHYCTRL);
  190. val = (val & ~S5PCSIS_DPHYCTRL_HSS_MASK) | (settle << 27);
  191. s5pcsis_write(state, S5PCSIS_DPHYCTRL, val);
  192. }
  193. static void s5pcsis_set_params(struct csis_state *state)
  194. {
  195. struct s5p_platform_mipi_csis *pdata = state->pdev->dev.platform_data;
  196. u32 val;
  197. val = s5pcsis_read(state, S5PCSIS_CONFIG);
  198. val = (val & ~S5PCSIS_CFG_NR_LANE_MASK) | (pdata->lanes - 1);
  199. s5pcsis_write(state, S5PCSIS_CONFIG, val);
  200. __s5pcsis_set_format(state);
  201. s5pcsis_set_hsync_settle(state, pdata->hs_settle);
  202. val = s5pcsis_read(state, S5PCSIS_CTRL);
  203. if (pdata->alignment == 32)
  204. val |= S5PCSIS_CTRL_ALIGN_32BIT;
  205. else /* 24-bits */
  206. val &= ~S5PCSIS_CTRL_ALIGN_32BIT;
  207. /* Not using external clock. */
  208. val &= ~S5PCSIS_CTRL_WCLK_EXTCLK;
  209. s5pcsis_write(state, S5PCSIS_CTRL, val);
  210. /* Update the shadow register. */
  211. val = s5pcsis_read(state, S5PCSIS_CTRL);
  212. s5pcsis_write(state, S5PCSIS_CTRL, val | S5PCSIS_CTRL_UPDATE_SHADOW);
  213. }
  214. static void s5pcsis_clk_put(struct csis_state *state)
  215. {
  216. int i;
  217. for (i = 0; i < NUM_CSIS_CLOCKS; i++)
  218. if (!IS_ERR_OR_NULL(state->clock[i]))
  219. clk_put(state->clock[i]);
  220. }
  221. static int s5pcsis_clk_get(struct csis_state *state)
  222. {
  223. struct device *dev = &state->pdev->dev;
  224. int i;
  225. for (i = 0; i < NUM_CSIS_CLOCKS; i++) {
  226. state->clock[i] = clk_get(dev, csi_clock_name[i]);
  227. if (IS_ERR(state->clock[i])) {
  228. s5pcsis_clk_put(state);
  229. dev_err(dev, "failed to get clock: %s\n",
  230. csi_clock_name[i]);
  231. return -ENXIO;
  232. }
  233. }
  234. return 0;
  235. }
  236. static int s5pcsis_s_power(struct v4l2_subdev *sd, int on)
  237. {
  238. struct csis_state *state = sd_to_csis_state(sd);
  239. struct device *dev = &state->pdev->dev;
  240. if (on)
  241. return pm_runtime_get_sync(dev);
  242. return pm_runtime_put_sync(dev);
  243. }
  244. static void s5pcsis_start_stream(struct csis_state *state)
  245. {
  246. s5pcsis_reset(state);
  247. s5pcsis_set_params(state);
  248. s5pcsis_system_enable(state, true);
  249. s5pcsis_enable_interrupts(state, true);
  250. }
  251. static void s5pcsis_stop_stream(struct csis_state *state)
  252. {
  253. s5pcsis_enable_interrupts(state, false);
  254. s5pcsis_system_enable(state, false);
  255. }
  256. /* v4l2_subdev operations */
  257. static int s5pcsis_s_stream(struct v4l2_subdev *sd, int enable)
  258. {
  259. struct csis_state *state = sd_to_csis_state(sd);
  260. int ret = 0;
  261. v4l2_dbg(1, debug, sd, "%s: %d, state: 0x%x\n",
  262. __func__, enable, state->flags);
  263. if (enable) {
  264. ret = pm_runtime_get_sync(&state->pdev->dev);
  265. if (ret && ret != 1)
  266. return ret;
  267. }
  268. mutex_lock(&state->lock);
  269. if (enable) {
  270. if (state->flags & ST_SUSPENDED) {
  271. ret = -EBUSY;
  272. goto unlock;
  273. }
  274. s5pcsis_start_stream(state);
  275. state->flags |= ST_STREAMING;
  276. } else {
  277. s5pcsis_stop_stream(state);
  278. state->flags &= ~ST_STREAMING;
  279. }
  280. unlock:
  281. mutex_unlock(&state->lock);
  282. if (!enable)
  283. pm_runtime_put(&state->pdev->dev);
  284. return ret == 1 ? 0 : ret;
  285. }
  286. static int s5pcsis_enum_mbus_code(struct v4l2_subdev *sd,
  287. struct v4l2_subdev_fh *fh,
  288. struct v4l2_subdev_mbus_code_enum *code)
  289. {
  290. if (code->index >= ARRAY_SIZE(s5pcsis_formats))
  291. return -EINVAL;
  292. code->code = s5pcsis_formats[code->index].code;
  293. return 0;
  294. }
  295. static struct csis_pix_format const *s5pcsis_try_format(
  296. struct v4l2_mbus_framefmt *mf)
  297. {
  298. struct csis_pix_format const *csis_fmt;
  299. csis_fmt = find_csis_format(mf);
  300. if (csis_fmt == NULL)
  301. csis_fmt = &s5pcsis_formats[0];
  302. mf->code = csis_fmt->code;
  303. v4l_bound_align_image(&mf->width, 1, CSIS_MAX_PIX_WIDTH,
  304. csis_fmt->pix_width_alignment,
  305. &mf->height, 1, CSIS_MAX_PIX_HEIGHT, 1,
  306. 0);
  307. return csis_fmt;
  308. }
  309. static struct v4l2_mbus_framefmt *__s5pcsis_get_format(
  310. struct csis_state *state, struct v4l2_subdev_fh *fh,
  311. u32 pad, enum v4l2_subdev_format_whence which)
  312. {
  313. if (which == V4L2_SUBDEV_FORMAT_TRY)
  314. return fh ? v4l2_subdev_get_try_format(fh, pad) : NULL;
  315. return &state->format;
  316. }
  317. static int s5pcsis_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  318. struct v4l2_subdev_format *fmt)
  319. {
  320. struct csis_state *state = sd_to_csis_state(sd);
  321. struct csis_pix_format const *csis_fmt;
  322. struct v4l2_mbus_framefmt *mf;
  323. if (fmt->pad != CSIS_PAD_SOURCE && fmt->pad != CSIS_PAD_SINK)
  324. return -EINVAL;
  325. mf = __s5pcsis_get_format(state, fh, fmt->pad, fmt->which);
  326. if (fmt->pad == CSIS_PAD_SOURCE) {
  327. if (mf) {
  328. mutex_lock(&state->lock);
  329. fmt->format = *mf;
  330. mutex_unlock(&state->lock);
  331. }
  332. return 0;
  333. }
  334. csis_fmt = s5pcsis_try_format(&fmt->format);
  335. if (mf) {
  336. mutex_lock(&state->lock);
  337. *mf = fmt->format;
  338. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  339. state->csis_fmt = csis_fmt;
  340. mutex_unlock(&state->lock);
  341. }
  342. return 0;
  343. }
  344. static int s5pcsis_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  345. struct v4l2_subdev_format *fmt)
  346. {
  347. struct csis_state *state = sd_to_csis_state(sd);
  348. struct v4l2_mbus_framefmt *mf;
  349. if (fmt->pad != CSIS_PAD_SOURCE && fmt->pad != CSIS_PAD_SINK)
  350. return -EINVAL;
  351. mf = __s5pcsis_get_format(state, fh, fmt->pad, fmt->which);
  352. if (!mf)
  353. return -EINVAL;
  354. mutex_lock(&state->lock);
  355. fmt->format = *mf;
  356. mutex_unlock(&state->lock);
  357. return 0;
  358. }
  359. static struct v4l2_subdev_core_ops s5pcsis_core_ops = {
  360. .s_power = s5pcsis_s_power,
  361. };
  362. static struct v4l2_subdev_pad_ops s5pcsis_pad_ops = {
  363. .enum_mbus_code = s5pcsis_enum_mbus_code,
  364. .get_fmt = s5pcsis_get_fmt,
  365. .set_fmt = s5pcsis_set_fmt,
  366. };
  367. static struct v4l2_subdev_video_ops s5pcsis_video_ops = {
  368. .s_stream = s5pcsis_s_stream,
  369. };
  370. static struct v4l2_subdev_ops s5pcsis_subdev_ops = {
  371. .core = &s5pcsis_core_ops,
  372. .pad = &s5pcsis_pad_ops,
  373. .video = &s5pcsis_video_ops,
  374. };
  375. static irqreturn_t s5pcsis_irq_handler(int irq, void *dev_id)
  376. {
  377. struct csis_state *state = dev_id;
  378. u32 val;
  379. /* Just clear the interrupt pending bits. */
  380. val = s5pcsis_read(state, S5PCSIS_INTSRC);
  381. s5pcsis_write(state, S5PCSIS_INTSRC, val);
  382. return IRQ_HANDLED;
  383. }
  384. static int __devinit s5pcsis_probe(struct platform_device *pdev)
  385. {
  386. struct s5p_platform_mipi_csis *pdata;
  387. struct resource *mem_res;
  388. struct resource *regs_res;
  389. struct csis_state *state;
  390. int ret = -ENOMEM;
  391. state = kzalloc(sizeof(*state), GFP_KERNEL);
  392. if (!state)
  393. return -ENOMEM;
  394. mutex_init(&state->lock);
  395. state->pdev = pdev;
  396. pdata = pdev->dev.platform_data;
  397. if (pdata == NULL || pdata->phy_enable == NULL) {
  398. dev_err(&pdev->dev, "Platform data not fully specified\n");
  399. goto e_free;
  400. }
  401. if ((pdev->id == 1 && pdata->lanes > CSIS1_MAX_LANES) ||
  402. pdata->lanes > CSIS0_MAX_LANES) {
  403. ret = -EINVAL;
  404. dev_err(&pdev->dev, "Unsupported number of data lanes: %d\n",
  405. pdata->lanes);
  406. goto e_free;
  407. }
  408. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  409. if (!mem_res) {
  410. dev_err(&pdev->dev, "Failed to get IO memory region\n");
  411. goto e_free;
  412. }
  413. regs_res = request_mem_region(mem_res->start, resource_size(mem_res),
  414. pdev->name);
  415. if (!regs_res) {
  416. dev_err(&pdev->dev, "Failed to request IO memory region\n");
  417. goto e_free;
  418. }
  419. state->regs_res = regs_res;
  420. state->regs = ioremap(mem_res->start, resource_size(mem_res));
  421. if (!state->regs) {
  422. dev_err(&pdev->dev, "Failed to remap IO region\n");
  423. goto e_reqmem;
  424. }
  425. ret = s5pcsis_clk_get(state);
  426. if (ret)
  427. goto e_unmap;
  428. clk_enable(state->clock[CSIS_CLK_MUX]);
  429. if (pdata->clk_rate)
  430. clk_set_rate(state->clock[CSIS_CLK_MUX], pdata->clk_rate);
  431. else
  432. dev_WARN(&pdev->dev, "No clock frequency specified!\n");
  433. state->irq = platform_get_irq(pdev, 0);
  434. if (state->irq < 0) {
  435. ret = state->irq;
  436. dev_err(&pdev->dev, "Failed to get irq\n");
  437. goto e_clkput;
  438. }
  439. if (!pdata->fixed_phy_vdd) {
  440. state->supply = regulator_get(&pdev->dev, "vdd");
  441. if (IS_ERR(state->supply)) {
  442. ret = PTR_ERR(state->supply);
  443. state->supply = NULL;
  444. goto e_clkput;
  445. }
  446. }
  447. ret = request_irq(state->irq, s5pcsis_irq_handler, 0,
  448. dev_name(&pdev->dev), state);
  449. if (ret) {
  450. dev_err(&pdev->dev, "request_irq failed\n");
  451. goto e_regput;
  452. }
  453. v4l2_subdev_init(&state->sd, &s5pcsis_subdev_ops);
  454. state->sd.owner = THIS_MODULE;
  455. strlcpy(state->sd.name, dev_name(&pdev->dev), sizeof(state->sd.name));
  456. state->csis_fmt = &s5pcsis_formats[0];
  457. state->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  458. state->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  459. ret = media_entity_init(&state->sd.entity,
  460. CSIS_PADS_NUM, state->pads, 0);
  461. if (ret < 0)
  462. goto e_irqfree;
  463. /* This allows to retrieve the platform device id by the host driver */
  464. v4l2_set_subdevdata(&state->sd, pdev);
  465. /* .. and a pointer to the subdev. */
  466. platform_set_drvdata(pdev, &state->sd);
  467. state->flags = ST_SUSPENDED;
  468. pm_runtime_enable(&pdev->dev);
  469. return 0;
  470. e_irqfree:
  471. free_irq(state->irq, state);
  472. e_regput:
  473. if (state->supply)
  474. regulator_put(state->supply);
  475. e_clkput:
  476. clk_disable(state->clock[CSIS_CLK_MUX]);
  477. s5pcsis_clk_put(state);
  478. e_unmap:
  479. iounmap(state->regs);
  480. e_reqmem:
  481. release_mem_region(regs_res->start, resource_size(regs_res));
  482. e_free:
  483. kfree(state);
  484. return ret;
  485. }
  486. static int s5pcsis_suspend(struct device *dev)
  487. {
  488. struct s5p_platform_mipi_csis *pdata = dev->platform_data;
  489. struct platform_device *pdev = to_platform_device(dev);
  490. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  491. struct csis_state *state = sd_to_csis_state(sd);
  492. int ret;
  493. v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
  494. __func__, state->flags);
  495. mutex_lock(&state->lock);
  496. if (state->flags & ST_POWERED) {
  497. s5pcsis_stop_stream(state);
  498. ret = pdata->phy_enable(state->pdev, false);
  499. if (ret)
  500. goto unlock;
  501. if (state->supply) {
  502. ret = regulator_disable(state->supply);
  503. if (ret)
  504. goto unlock;
  505. }
  506. clk_disable(state->clock[CSIS_CLK_GATE]);
  507. state->flags &= ~ST_POWERED;
  508. }
  509. state->flags |= ST_SUSPENDED;
  510. unlock:
  511. mutex_unlock(&state->lock);
  512. return ret ? -EAGAIN : 0;
  513. }
  514. static int s5pcsis_resume(struct device *dev)
  515. {
  516. struct s5p_platform_mipi_csis *pdata = dev->platform_data;
  517. struct platform_device *pdev = to_platform_device(dev);
  518. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  519. struct csis_state *state = sd_to_csis_state(sd);
  520. int ret = 0;
  521. v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
  522. __func__, state->flags);
  523. mutex_lock(&state->lock);
  524. if (!(state->flags & ST_SUSPENDED))
  525. goto unlock;
  526. if (!(state->flags & ST_POWERED)) {
  527. if (state->supply)
  528. ret = regulator_enable(state->supply);
  529. if (ret)
  530. goto unlock;
  531. ret = pdata->phy_enable(state->pdev, true);
  532. if (!ret) {
  533. state->flags |= ST_POWERED;
  534. } else {
  535. regulator_disable(state->supply);
  536. goto unlock;
  537. }
  538. clk_enable(state->clock[CSIS_CLK_GATE]);
  539. }
  540. if (state->flags & ST_STREAMING)
  541. s5pcsis_start_stream(state);
  542. state->flags &= ~ST_SUSPENDED;
  543. unlock:
  544. mutex_unlock(&state->lock);
  545. return ret ? -EAGAIN : 0;
  546. }
  547. #ifdef CONFIG_PM_SLEEP
  548. static int s5pcsis_pm_suspend(struct device *dev)
  549. {
  550. return s5pcsis_suspend(dev);
  551. }
  552. static int s5pcsis_pm_resume(struct device *dev)
  553. {
  554. int ret;
  555. ret = s5pcsis_resume(dev);
  556. if (!ret) {
  557. pm_runtime_disable(dev);
  558. ret = pm_runtime_set_active(dev);
  559. pm_runtime_enable(dev);
  560. }
  561. return ret;
  562. }
  563. #endif
  564. static int __devexit s5pcsis_remove(struct platform_device *pdev)
  565. {
  566. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  567. struct csis_state *state = sd_to_csis_state(sd);
  568. struct resource *res = state->regs_res;
  569. pm_runtime_disable(&pdev->dev);
  570. s5pcsis_suspend(&pdev->dev);
  571. clk_disable(state->clock[CSIS_CLK_MUX]);
  572. pm_runtime_set_suspended(&pdev->dev);
  573. s5pcsis_clk_put(state);
  574. if (state->supply)
  575. regulator_put(state->supply);
  576. media_entity_cleanup(&state->sd.entity);
  577. free_irq(state->irq, state);
  578. iounmap(state->regs);
  579. release_mem_region(res->start, resource_size(res));
  580. kfree(state);
  581. return 0;
  582. }
  583. static const struct dev_pm_ops s5pcsis_pm_ops = {
  584. SET_RUNTIME_PM_OPS(s5pcsis_suspend, s5pcsis_resume, NULL)
  585. SET_SYSTEM_SLEEP_PM_OPS(s5pcsis_pm_suspend, s5pcsis_pm_resume)
  586. };
  587. static struct platform_driver s5pcsis_driver = {
  588. .probe = s5pcsis_probe,
  589. .remove = __devexit_p(s5pcsis_remove),
  590. .driver = {
  591. .name = CSIS_DRIVER_NAME,
  592. .owner = THIS_MODULE,
  593. .pm = &s5pcsis_pm_ops,
  594. },
  595. };
  596. static int __init s5pcsis_init(void)
  597. {
  598. return platform_driver_probe(&s5pcsis_driver, s5pcsis_probe);
  599. }
  600. static void __exit s5pcsis_exit(void)
  601. {
  602. platform_driver_unregister(&s5pcsis_driver);
  603. }
  604. module_init(s5pcsis_init);
  605. module_exit(s5pcsis_exit);
  606. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  607. MODULE_DESCRIPTION("S5P/EXYNOS4 MIPI CSI receiver driver");
  608. MODULE_LICENSE("GPL");