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