mipi-csis.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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 int s5pcsis_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  365. {
  366. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(fh, 0);
  367. format->colorspace = V4L2_COLORSPACE_JPEG;
  368. format->code = s5pcsis_formats[0].code;
  369. format->width = S5PCSIS_DEF_PIX_WIDTH;
  370. format->height = S5PCSIS_DEF_PIX_HEIGHT;
  371. format->field = V4L2_FIELD_NONE;
  372. return 0;
  373. }
  374. static const struct v4l2_subdev_internal_ops s5pcsis_sd_internal_ops = {
  375. .open = s5pcsis_open,
  376. };
  377. static struct v4l2_subdev_core_ops s5pcsis_core_ops = {
  378. .s_power = s5pcsis_s_power,
  379. };
  380. static struct v4l2_subdev_pad_ops s5pcsis_pad_ops = {
  381. .enum_mbus_code = s5pcsis_enum_mbus_code,
  382. .get_fmt = s5pcsis_get_fmt,
  383. .set_fmt = s5pcsis_set_fmt,
  384. };
  385. static struct v4l2_subdev_video_ops s5pcsis_video_ops = {
  386. .s_stream = s5pcsis_s_stream,
  387. };
  388. static struct v4l2_subdev_ops s5pcsis_subdev_ops = {
  389. .core = &s5pcsis_core_ops,
  390. .pad = &s5pcsis_pad_ops,
  391. .video = &s5pcsis_video_ops,
  392. };
  393. static irqreturn_t s5pcsis_irq_handler(int irq, void *dev_id)
  394. {
  395. struct csis_state *state = dev_id;
  396. u32 val;
  397. /* Just clear the interrupt pending bits. */
  398. val = s5pcsis_read(state, S5PCSIS_INTSRC);
  399. s5pcsis_write(state, S5PCSIS_INTSRC, val);
  400. return IRQ_HANDLED;
  401. }
  402. static int __devinit s5pcsis_probe(struct platform_device *pdev)
  403. {
  404. struct s5p_platform_mipi_csis *pdata;
  405. struct resource *mem_res;
  406. struct resource *regs_res;
  407. struct csis_state *state;
  408. int ret = -ENOMEM;
  409. int i;
  410. state = kzalloc(sizeof(*state), GFP_KERNEL);
  411. if (!state)
  412. return -ENOMEM;
  413. mutex_init(&state->lock);
  414. state->pdev = pdev;
  415. pdata = pdev->dev.platform_data;
  416. if (pdata == NULL || pdata->phy_enable == NULL) {
  417. dev_err(&pdev->dev, "Platform data not fully specified\n");
  418. goto e_free;
  419. }
  420. if ((pdev->id == 1 && pdata->lanes > CSIS1_MAX_LANES) ||
  421. pdata->lanes > CSIS0_MAX_LANES) {
  422. ret = -EINVAL;
  423. dev_err(&pdev->dev, "Unsupported number of data lanes: %d\n",
  424. pdata->lanes);
  425. goto e_free;
  426. }
  427. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  428. if (!mem_res) {
  429. dev_err(&pdev->dev, "Failed to get IO memory region\n");
  430. goto e_free;
  431. }
  432. regs_res = request_mem_region(mem_res->start, resource_size(mem_res),
  433. pdev->name);
  434. if (!regs_res) {
  435. dev_err(&pdev->dev, "Failed to request IO memory region\n");
  436. goto e_free;
  437. }
  438. state->regs_res = regs_res;
  439. state->regs = ioremap(mem_res->start, resource_size(mem_res));
  440. if (!state->regs) {
  441. dev_err(&pdev->dev, "Failed to remap IO region\n");
  442. goto e_reqmem;
  443. }
  444. ret = s5pcsis_clk_get(state);
  445. if (ret)
  446. goto e_unmap;
  447. clk_enable(state->clock[CSIS_CLK_MUX]);
  448. if (pdata->clk_rate)
  449. clk_set_rate(state->clock[CSIS_CLK_MUX], pdata->clk_rate);
  450. else
  451. dev_WARN(&pdev->dev, "No clock frequency specified!\n");
  452. state->irq = platform_get_irq(pdev, 0);
  453. if (state->irq < 0) {
  454. ret = state->irq;
  455. dev_err(&pdev->dev, "Failed to get irq\n");
  456. goto e_clkput;
  457. }
  458. for (i = 0; i < CSIS_NUM_SUPPLIES; i++)
  459. state->supplies[i].supply = csis_supply_name[i];
  460. ret = regulator_bulk_get(&pdev->dev, CSIS_NUM_SUPPLIES,
  461. state->supplies);
  462. if (ret)
  463. goto e_clkput;
  464. ret = request_irq(state->irq, s5pcsis_irq_handler, 0,
  465. dev_name(&pdev->dev), state);
  466. if (ret) {
  467. dev_err(&pdev->dev, "request_irq failed\n");
  468. goto e_regput;
  469. }
  470. v4l2_subdev_init(&state->sd, &s5pcsis_subdev_ops);
  471. state->sd.owner = THIS_MODULE;
  472. strlcpy(state->sd.name, dev_name(&pdev->dev), sizeof(state->sd.name));
  473. state->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  474. state->csis_fmt = &s5pcsis_formats[0];
  475. state->format.code = s5pcsis_formats[0].code;
  476. state->format.width = S5PCSIS_DEF_PIX_WIDTH;
  477. state->format.height = S5PCSIS_DEF_PIX_HEIGHT;
  478. state->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  479. state->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  480. ret = media_entity_init(&state->sd.entity,
  481. CSIS_PADS_NUM, state->pads, 0);
  482. if (ret < 0)
  483. goto e_irqfree;
  484. /* This allows to retrieve the platform device id by the host driver */
  485. v4l2_set_subdevdata(&state->sd, pdev);
  486. /* .. and a pointer to the subdev. */
  487. platform_set_drvdata(pdev, &state->sd);
  488. pm_runtime_enable(&pdev->dev);
  489. return 0;
  490. e_irqfree:
  491. free_irq(state->irq, state);
  492. e_regput:
  493. regulator_bulk_free(CSIS_NUM_SUPPLIES, state->supplies);
  494. e_clkput:
  495. clk_disable(state->clock[CSIS_CLK_MUX]);
  496. s5pcsis_clk_put(state);
  497. e_unmap:
  498. iounmap(state->regs);
  499. e_reqmem:
  500. release_mem_region(regs_res->start, resource_size(regs_res));
  501. e_free:
  502. kfree(state);
  503. return ret;
  504. }
  505. static int s5pcsis_pm_suspend(struct device *dev, bool runtime)
  506. {
  507. struct s5p_platform_mipi_csis *pdata = dev->platform_data;
  508. struct platform_device *pdev = to_platform_device(dev);
  509. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  510. struct csis_state *state = sd_to_csis_state(sd);
  511. int ret = 0;
  512. v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
  513. __func__, state->flags);
  514. mutex_lock(&state->lock);
  515. if (state->flags & ST_POWERED) {
  516. s5pcsis_stop_stream(state);
  517. ret = pdata->phy_enable(state->pdev, false);
  518. if (ret)
  519. goto unlock;
  520. ret = regulator_bulk_disable(CSIS_NUM_SUPPLIES,
  521. state->supplies);
  522. if (ret)
  523. goto unlock;
  524. clk_disable(state->clock[CSIS_CLK_GATE]);
  525. state->flags &= ~ST_POWERED;
  526. if (!runtime)
  527. state->flags |= ST_SUSPENDED;
  528. }
  529. unlock:
  530. mutex_unlock(&state->lock);
  531. return ret ? -EAGAIN : 0;
  532. }
  533. static int s5pcsis_pm_resume(struct device *dev, bool runtime)
  534. {
  535. struct s5p_platform_mipi_csis *pdata = dev->platform_data;
  536. struct platform_device *pdev = to_platform_device(dev);
  537. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  538. struct csis_state *state = sd_to_csis_state(sd);
  539. int ret = 0;
  540. v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
  541. __func__, state->flags);
  542. mutex_lock(&state->lock);
  543. if (!runtime && !(state->flags & ST_SUSPENDED))
  544. goto unlock;
  545. if (!(state->flags & ST_POWERED)) {
  546. ret = regulator_bulk_enable(CSIS_NUM_SUPPLIES,
  547. state->supplies);
  548. if (ret)
  549. goto unlock;
  550. ret = pdata->phy_enable(state->pdev, true);
  551. if (!ret) {
  552. state->flags |= ST_POWERED;
  553. } else {
  554. regulator_bulk_disable(CSIS_NUM_SUPPLIES,
  555. state->supplies);
  556. goto unlock;
  557. }
  558. clk_enable(state->clock[CSIS_CLK_GATE]);
  559. }
  560. if (state->flags & ST_STREAMING)
  561. s5pcsis_start_stream(state);
  562. state->flags &= ~ST_SUSPENDED;
  563. unlock:
  564. mutex_unlock(&state->lock);
  565. return ret ? -EAGAIN : 0;
  566. }
  567. #ifdef CONFIG_PM_SLEEP
  568. static int s5pcsis_suspend(struct device *dev)
  569. {
  570. return s5pcsis_pm_suspend(dev, false);
  571. }
  572. static int s5pcsis_resume(struct device *dev)
  573. {
  574. return s5pcsis_pm_resume(dev, false);
  575. }
  576. #endif
  577. #ifdef CONFIG_PM_RUNTIME
  578. static int s5pcsis_runtime_suspend(struct device *dev)
  579. {
  580. return s5pcsis_pm_suspend(dev, true);
  581. }
  582. static int s5pcsis_runtime_resume(struct device *dev)
  583. {
  584. return s5pcsis_pm_resume(dev, true);
  585. }
  586. #endif
  587. static int __devexit s5pcsis_remove(struct platform_device *pdev)
  588. {
  589. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  590. struct csis_state *state = sd_to_csis_state(sd);
  591. struct resource *res = state->regs_res;
  592. pm_runtime_disable(&pdev->dev);
  593. s5pcsis_suspend(&pdev->dev);
  594. clk_disable(state->clock[CSIS_CLK_MUX]);
  595. pm_runtime_set_suspended(&pdev->dev);
  596. s5pcsis_clk_put(state);
  597. regulator_bulk_free(CSIS_NUM_SUPPLIES, state->supplies);
  598. media_entity_cleanup(&state->sd.entity);
  599. free_irq(state->irq, state);
  600. iounmap(state->regs);
  601. release_mem_region(res->start, resource_size(res));
  602. kfree(state);
  603. return 0;
  604. }
  605. static const struct dev_pm_ops s5pcsis_pm_ops = {
  606. SET_RUNTIME_PM_OPS(s5pcsis_runtime_suspend, s5pcsis_runtime_resume,
  607. NULL)
  608. SET_SYSTEM_SLEEP_PM_OPS(s5pcsis_suspend, s5pcsis_resume)
  609. };
  610. static struct platform_driver s5pcsis_driver = {
  611. .probe = s5pcsis_probe,
  612. .remove = __devexit_p(s5pcsis_remove),
  613. .driver = {
  614. .name = CSIS_DRIVER_NAME,
  615. .owner = THIS_MODULE,
  616. .pm = &s5pcsis_pm_ops,
  617. },
  618. };
  619. static int __init s5pcsis_init(void)
  620. {
  621. return platform_driver_probe(&s5pcsis_driver, s5pcsis_probe);
  622. }
  623. static void __exit s5pcsis_exit(void)
  624. {
  625. platform_driver_unregister(&s5pcsis_driver);
  626. }
  627. module_init(s5pcsis_init);
  628. module_exit(s5pcsis_exit);
  629. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  630. MODULE_DESCRIPTION("S5P/EXYNOS4 MIPI CSI receiver driver");
  631. MODULE_LICENSE("GPL");