mipi-csis.c 18 KB

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