mipi-csis.c 18 KB

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