2 * Samsung Standard Definition Output (SDO) driver
4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
6 * Tomasz Stanislawski, <t.stanislaws@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundiation. either version 2 of the License,
11 * or (at your option) any later version
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
26 #include <media/v4l2-subdev.h>
30 MODULE_AUTHOR("Tomasz Stanislawski, <t.stanislaws@samsung.com>");
31 MODULE_DESCRIPTION("Samsung Standard Definition Output (SDO)");
32 MODULE_LICENSE("GPL");
34 #define SDO_DEFAULT_STD V4L2_STD_PAL
38 /* all modes are 720 pixels wide */
44 /** pointer to device parent */
46 /** base address of SDO registers */
50 /** DAC source clock */
54 /** DAC physical interface */
56 /** clock for control of VPLL */
57 struct clk
*fout_vpll
;
58 /** vpll rate before sdo stream was on */
59 unsigned long vpll_rate
;
60 /** regulator for SDO IP power */
61 struct regulator
*vdac
;
62 /** regulator for SDO plug detection */
63 struct regulator
*vdet
;
64 /** subdev used as device interface */
65 struct v4l2_subdev sd
;
67 const struct sdo_format
*fmt
;
70 static inline struct sdo_device
*sd_to_sdev(struct v4l2_subdev
*sd
)
72 return container_of(sd
, struct sdo_device
, sd
);
76 void sdo_write_mask(struct sdo_device
*sdev
, u32 reg_id
, u32 value
, u32 mask
)
78 u32 old
= readl(sdev
->regs
+ reg_id
);
79 value
= (value
& mask
) | (old
& ~mask
);
80 writel(value
, sdev
->regs
+ reg_id
);
84 void sdo_write(struct sdo_device
*sdev
, u32 reg_id
, u32 value
)
86 writel(value
, sdev
->regs
+ reg_id
);
90 u32
sdo_read(struct sdo_device
*sdev
, u32 reg_id
)
92 return readl(sdev
->regs
+ reg_id
);
95 static irqreturn_t
sdo_irq_handler(int irq
, void *dev_data
)
97 struct sdo_device
*sdev
= dev_data
;
100 sdo_write_mask(sdev
, SDO_IRQ
, ~0, SDO_VSYNC_IRQ_PEND
);
104 static void sdo_reg_debug(struct sdo_device
*sdev
)
106 #define DBGREG(reg_id) \
107 dev_info(sdev->dev, #reg_id " = %08x\n", \
108 sdo_read(sdev, reg_id))
119 static const struct sdo_format sdo_format
[] = {
120 { V4L2_STD_PAL_N
, .height
= 576, .cookie
= SDO_PAL_N
},
121 { V4L2_STD_PAL_Nc
, .height
= 576, .cookie
= SDO_PAL_NC
},
122 { V4L2_STD_PAL_M
, .height
= 480, .cookie
= SDO_PAL_M
},
123 { V4L2_STD_PAL_60
, .height
= 480, .cookie
= SDO_PAL_60
},
124 { V4L2_STD_NTSC_443
, .height
= 480, .cookie
= SDO_NTSC_443
},
125 { V4L2_STD_PAL
, .height
= 576, .cookie
= SDO_PAL_BGHID
},
126 { V4L2_STD_NTSC_M
, .height
= 480, .cookie
= SDO_NTSC_M
},
129 static const struct sdo_format
*sdo_find_format(v4l2_std_id id
)
132 for (i
= 0; i
< ARRAY_SIZE(sdo_format
); ++i
)
133 if (sdo_format
[i
].id
& id
)
134 return &sdo_format
[i
];
138 static int sdo_g_tvnorms_output(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
140 *std
= V4L2_STD_NTSC_M
| V4L2_STD_PAL_M
| V4L2_STD_PAL
|
141 V4L2_STD_PAL_N
| V4L2_STD_PAL_Nc
|
142 V4L2_STD_NTSC_443
| V4L2_STD_PAL_60
;
146 static int sdo_s_std_output(struct v4l2_subdev
*sd
, v4l2_std_id std
)
148 struct sdo_device
*sdev
= sd_to_sdev(sd
);
149 const struct sdo_format
*fmt
;
150 fmt
= sdo_find_format(std
);
157 static int sdo_g_std_output(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
159 *std
= sd_to_sdev(sd
)->fmt
->id
;
163 static int sdo_get_fmt(struct v4l2_subdev
*sd
,
164 struct v4l2_subdev_pad_config
*cfg
,
165 struct v4l2_subdev_format
*format
)
167 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
168 struct sdo_device
*sdev
= sd_to_sdev(sd
);
174 /* all modes are 720 pixels wide */
176 fmt
->height
= sdev
->fmt
->height
;
177 fmt
->code
= MEDIA_BUS_FMT_FIXED
;
178 fmt
->field
= V4L2_FIELD_INTERLACED
;
179 fmt
->colorspace
= V4L2_COLORSPACE_JPEG
;
183 static int sdo_s_power(struct v4l2_subdev
*sd
, int on
)
185 struct sdo_device
*sdev
= sd_to_sdev(sd
);
186 struct device
*dev
= sdev
->dev
;
189 dev_info(dev
, "sdo_s_power(%d)\n", on
);
192 ret
= pm_runtime_get_sync(dev
);
194 ret
= pm_runtime_put_sync(dev
);
196 /* only values < 0 indicate errors */
197 return ret
< 0 ? ret
: 0;
200 static int sdo_streamon(struct sdo_device
*sdev
)
204 /* set proper clock for Timing Generator */
205 sdev
->vpll_rate
= clk_get_rate(sdev
->fout_vpll
);
206 ret
= clk_set_rate(sdev
->fout_vpll
, 54000000);
208 dev_err(sdev
->dev
, "Failed to set vpll rate\n");
211 dev_info(sdev
->dev
, "fout_vpll.rate = %lu\n",
212 clk_get_rate(sdev
->fout_vpll
));
213 /* enable clock in SDO */
214 sdo_write_mask(sdev
, SDO_CLKCON
, ~0, SDO_TVOUT_CLOCK_ON
);
215 ret
= clk_prepare_enable(sdev
->dacphy
);
217 dev_err(sdev
->dev
, "clk_prepare_enable(dacphy) failed\n");
221 sdo_write_mask(sdev
, SDO_DAC
, ~0, SDO_POWER_ON_DAC
);
226 sdo_write_mask(sdev
, SDO_CLKCON
, 0, SDO_TVOUT_CLOCK_ON
);
227 clk_set_rate(sdev
->fout_vpll
, sdev
->vpll_rate
);
231 static int sdo_streamoff(struct sdo_device
*sdev
)
235 sdo_write_mask(sdev
, SDO_DAC
, 0, SDO_POWER_ON_DAC
);
236 clk_disable_unprepare(sdev
->dacphy
);
237 sdo_write_mask(sdev
, SDO_CLKCON
, 0, SDO_TVOUT_CLOCK_ON
);
238 for (tries
= 100; tries
; --tries
) {
239 if (sdo_read(sdev
, SDO_CLKCON
) & SDO_TVOUT_CLOCK_READY
)
244 dev_err(sdev
->dev
, "failed to stop streaming\n");
245 clk_set_rate(sdev
->fout_vpll
, sdev
->vpll_rate
);
246 return tries
? 0 : -EIO
;
249 static int sdo_s_stream(struct v4l2_subdev
*sd
, int on
)
251 struct sdo_device
*sdev
= sd_to_sdev(sd
);
252 return on
? sdo_streamon(sdev
) : sdo_streamoff(sdev
);
255 static const struct v4l2_subdev_core_ops sdo_sd_core_ops
= {
256 .s_power
= sdo_s_power
,
259 static const struct v4l2_subdev_video_ops sdo_sd_video_ops
= {
260 .s_std_output
= sdo_s_std_output
,
261 .g_std_output
= sdo_g_std_output
,
262 .g_tvnorms_output
= sdo_g_tvnorms_output
,
263 .s_stream
= sdo_s_stream
,
266 static const struct v4l2_subdev_pad_ops sdo_sd_pad_ops
= {
267 .get_fmt
= sdo_get_fmt
,
270 static const struct v4l2_subdev_ops sdo_sd_ops
= {
271 .core
= &sdo_sd_core_ops
,
272 .video
= &sdo_sd_video_ops
,
273 .pad
= &sdo_sd_pad_ops
,
276 static int sdo_runtime_suspend(struct device
*dev
)
278 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
279 struct sdo_device
*sdev
= sd_to_sdev(sd
);
281 dev_info(dev
, "suspend\n");
282 regulator_disable(sdev
->vdet
);
283 regulator_disable(sdev
->vdac
);
284 clk_disable_unprepare(sdev
->sclk_dac
);
288 static int sdo_runtime_resume(struct device
*dev
)
290 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
291 struct sdo_device
*sdev
= sd_to_sdev(sd
);
294 dev_info(dev
, "resume\n");
296 ret
= clk_prepare_enable(sdev
->sclk_dac
);
300 ret
= regulator_enable(sdev
->vdac
);
304 ret
= regulator_enable(sdev
->vdet
);
309 sdo_write_mask(sdev
, SDO_CLKCON
, ~0, SDO_TVOUT_SW_RESET
);
311 sdo_write_mask(sdev
, SDO_CLKCON
, 0, SDO_TVOUT_SW_RESET
);
313 /* setting TV mode */
314 sdo_write_mask(sdev
, SDO_CONFIG
, sdev
->fmt
->cookie
, SDO_STANDARD_MASK
);
315 /* XXX: forcing interlaced mode using undocumented bit */
316 sdo_write_mask(sdev
, SDO_CONFIG
, 0, SDO_PROGRESSIVE
);
317 /* turn all VBI off */
318 sdo_write_mask(sdev
, SDO_VBI
, 0, SDO_CVBS_WSS_INS
|
319 SDO_CVBS_CLOSED_CAPTION_MASK
);
320 /* turn all post processing off */
321 sdo_write_mask(sdev
, SDO_CCCON
, ~0, SDO_COMPENSATION_BHS_ADJ_OFF
|
322 SDO_COMPENSATION_CVBS_COMP_OFF
);
327 regulator_disable(sdev
->vdac
);
329 clk_disable_unprepare(sdev
->sclk_dac
);
333 static const struct dev_pm_ops sdo_pm_ops
= {
334 .runtime_suspend
= sdo_runtime_suspend
,
335 .runtime_resume
= sdo_runtime_resume
,
338 static int sdo_probe(struct platform_device
*pdev
)
340 struct device
*dev
= &pdev
->dev
;
341 struct sdo_device
*sdev
;
342 struct resource
*res
;
344 struct clk
*sclk_vpll
;
346 dev_info(dev
, "probe start\n");
347 sdev
= devm_kzalloc(&pdev
->dev
, sizeof(*sdev
), GFP_KERNEL
);
349 dev_err(dev
, "not enough memory.\n");
355 /* mapping registers */
356 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
358 dev_err(dev
, "get memory resource failed.\n");
363 sdev
->regs
= devm_ioremap(&pdev
->dev
, res
->start
, resource_size(res
));
364 if (sdev
->regs
== NULL
) {
365 dev_err(dev
, "register mapping failed.\n");
370 /* acquiring interrupt */
371 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
373 dev_err(dev
, "get interrupt resource failed.\n");
377 ret
= devm_request_irq(&pdev
->dev
, res
->start
, sdo_irq_handler
, 0,
380 dev_err(dev
, "request interrupt failed.\n");
383 sdev
->irq
= res
->start
;
386 sdev
->sclk_dac
= clk_get(dev
, "sclk_dac");
387 if (IS_ERR(sdev
->sclk_dac
)) {
388 dev_err(dev
, "failed to get clock 'sclk_dac'\n");
389 ret
= PTR_ERR(sdev
->sclk_dac
);
392 sdev
->dac
= clk_get(dev
, "dac");
393 if (IS_ERR(sdev
->dac
)) {
394 dev_err(dev
, "failed to get clock 'dac'\n");
395 ret
= PTR_ERR(sdev
->dac
);
398 sdev
->dacphy
= clk_get(dev
, "dacphy");
399 if (IS_ERR(sdev
->dacphy
)) {
400 dev_err(dev
, "failed to get clock 'dacphy'\n");
401 ret
= PTR_ERR(sdev
->dacphy
);
404 sclk_vpll
= clk_get(dev
, "sclk_vpll");
405 if (IS_ERR(sclk_vpll
)) {
406 dev_err(dev
, "failed to get clock 'sclk_vpll'\n");
407 ret
= PTR_ERR(sclk_vpll
);
410 clk_set_parent(sdev
->sclk_dac
, sclk_vpll
);
412 sdev
->fout_vpll
= clk_get(dev
, "fout_vpll");
413 if (IS_ERR(sdev
->fout_vpll
)) {
414 dev_err(dev
, "failed to get clock 'fout_vpll'\n");
415 ret
= PTR_ERR(sdev
->fout_vpll
);
418 dev_info(dev
, "fout_vpll.rate = %lu\n", clk_get_rate(sclk_vpll
));
420 /* acquire regulator */
421 sdev
->vdac
= devm_regulator_get(dev
, "vdd33a_dac");
422 if (IS_ERR(sdev
->vdac
)) {
423 dev_err(dev
, "failed to get regulator 'vdac'\n");
424 ret
= PTR_ERR(sdev
->vdac
);
427 sdev
->vdet
= devm_regulator_get(dev
, "vdet");
428 if (IS_ERR(sdev
->vdet
)) {
429 dev_err(dev
, "failed to get regulator 'vdet'\n");
430 ret
= PTR_ERR(sdev
->vdet
);
434 /* enable gate for dac clock, because mixer uses it */
435 ret
= clk_prepare_enable(sdev
->dac
);
437 dev_err(dev
, "clk_prepare_enable(dac) failed\n");
441 /* configure power management */
442 pm_runtime_enable(dev
);
444 /* configuration of interface subdevice */
445 v4l2_subdev_init(&sdev
->sd
, &sdo_sd_ops
);
446 sdev
->sd
.owner
= THIS_MODULE
;
447 strlcpy(sdev
->sd
.name
, "s5p-sdo", sizeof(sdev
->sd
.name
));
449 /* set default format */
450 sdev
->fmt
= sdo_find_format(SDO_DEFAULT_STD
);
451 BUG_ON(sdev
->fmt
== NULL
);
453 /* keeping subdev in device's private for use by other drivers */
454 dev_set_drvdata(dev
, &sdev
->sd
);
456 dev_info(dev
, "probe succeeded\n");
460 clk_put(sdev
->fout_vpll
);
462 clk_put(sdev
->dacphy
);
466 clk_put(sdev
->sclk_dac
);
468 dev_info(dev
, "probe failed\n");
472 static int sdo_remove(struct platform_device
*pdev
)
474 struct v4l2_subdev
*sd
= dev_get_drvdata(&pdev
->dev
);
475 struct sdo_device
*sdev
= sd_to_sdev(sd
);
477 pm_runtime_disable(&pdev
->dev
);
478 clk_disable_unprepare(sdev
->dac
);
479 clk_put(sdev
->fout_vpll
);
480 clk_put(sdev
->dacphy
);
482 clk_put(sdev
->sclk_dac
);
484 dev_info(&pdev
->dev
, "remove successful\n");
488 static struct platform_driver sdo_driver __refdata
= {
490 .remove
= sdo_remove
,
497 module_platform_driver(sdo_driver
);