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 /** regulator for SDO IP power */
59 struct regulator
*vdac
;
60 /** regulator for SDO plug detection */
61 struct regulator
*vdet
;
62 /** subdev used as device interface */
63 struct v4l2_subdev sd
;
65 const struct sdo_format
*fmt
;
68 static inline struct sdo_device
*sd_to_sdev(struct v4l2_subdev
*sd
)
70 return container_of(sd
, struct sdo_device
, sd
);
74 void sdo_write_mask(struct sdo_device
*sdev
, u32 reg_id
, u32 value
, u32 mask
)
76 u32 old
= readl(sdev
->regs
+ reg_id
);
77 value
= (value
& mask
) | (old
& ~mask
);
78 writel(value
, sdev
->regs
+ reg_id
);
82 void sdo_write(struct sdo_device
*sdev
, u32 reg_id
, u32 value
)
84 writel(value
, sdev
->regs
+ reg_id
);
88 u32
sdo_read(struct sdo_device
*sdev
, u32 reg_id
)
90 return readl(sdev
->regs
+ reg_id
);
93 static irqreturn_t
sdo_irq_handler(int irq
, void *dev_data
)
95 struct sdo_device
*sdev
= dev_data
;
98 sdo_write_mask(sdev
, SDO_IRQ
, ~0, SDO_VSYNC_IRQ_PEND
);
102 static void sdo_reg_debug(struct sdo_device
*sdev
)
104 #define DBGREG(reg_id) \
105 dev_info(sdev->dev, #reg_id " = %08x\n", \
106 sdo_read(sdev, reg_id))
117 static const struct sdo_format sdo_format
[] = {
118 { V4L2_STD_PAL_N
, .height
= 576, .cookie
= SDO_PAL_N
},
119 { V4L2_STD_PAL_Nc
, .height
= 576, .cookie
= SDO_PAL_NC
},
120 { V4L2_STD_PAL_M
, .height
= 480, .cookie
= SDO_PAL_M
},
121 { V4L2_STD_PAL_60
, .height
= 480, .cookie
= SDO_PAL_60
},
122 { V4L2_STD_NTSC_443
, .height
= 480, .cookie
= SDO_NTSC_443
},
123 { V4L2_STD_PAL
, .height
= 576, .cookie
= SDO_PAL_BGHID
},
124 { V4L2_STD_NTSC_M
, .height
= 480, .cookie
= SDO_NTSC_M
},
127 static const struct sdo_format
*sdo_find_format(v4l2_std_id id
)
130 for (i
= 0; i
< ARRAY_SIZE(sdo_format
); ++i
)
131 if (sdo_format
[i
].id
& id
)
132 return &sdo_format
[i
];
136 static int sdo_g_tvnorms_output(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
138 *std
= V4L2_STD_NTSC_M
| V4L2_STD_PAL_M
| V4L2_STD_PAL
|
139 V4L2_STD_PAL_N
| V4L2_STD_PAL_Nc
|
140 V4L2_STD_NTSC_443
| V4L2_STD_PAL_60
;
144 static int sdo_s_std_output(struct v4l2_subdev
*sd
, v4l2_std_id std
)
146 struct sdo_device
*sdev
= sd_to_sdev(sd
);
147 const struct sdo_format
*fmt
;
148 fmt
= sdo_find_format(std
);
155 static int sdo_g_std_output(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
157 *std
= sd_to_sdev(sd
)->fmt
->id
;
161 static int sdo_g_mbus_fmt(struct v4l2_subdev
*sd
,
162 struct v4l2_mbus_framefmt
*fmt
)
164 struct sdo_device
*sdev
= sd_to_sdev(sd
);
168 /* all modes are 720 pixels wide */
170 fmt
->height
= sdev
->fmt
->height
;
171 fmt
->code
= V4L2_MBUS_FMT_FIXED
;
172 fmt
->field
= V4L2_FIELD_INTERLACED
;
176 static int sdo_s_power(struct v4l2_subdev
*sd
, int on
)
178 struct sdo_device
*sdev
= sd_to_sdev(sd
);
179 struct device
*dev
= sdev
->dev
;
182 dev_info(dev
, "sdo_s_power(%d)\n", on
);
185 ret
= pm_runtime_get_sync(dev
);
187 ret
= pm_runtime_put_sync(dev
);
189 /* only values < 0 indicate errors */
190 return IS_ERR_VALUE(ret
) ? ret
: 0;
193 static int sdo_streamon(struct sdo_device
*sdev
)
195 /* set proper clock for Timing Generator */
196 clk_set_rate(sdev
->fout_vpll
, 54000000);
197 dev_info(sdev
->dev
, "fout_vpll.rate = %lu\n",
198 clk_get_rate(sdev
->fout_vpll
));
199 /* enable clock in SDO */
200 sdo_write_mask(sdev
, SDO_CLKCON
, ~0, SDO_TVOUT_CLOCK_ON
);
201 clk_enable(sdev
->dacphy
);
203 sdo_write_mask(sdev
, SDO_DAC
, ~0, SDO_POWER_ON_DAC
);
208 static int sdo_streamoff(struct sdo_device
*sdev
)
212 sdo_write_mask(sdev
, SDO_DAC
, 0, SDO_POWER_ON_DAC
);
213 clk_disable(sdev
->dacphy
);
214 sdo_write_mask(sdev
, SDO_CLKCON
, 0, SDO_TVOUT_CLOCK_ON
);
215 for (tries
= 100; tries
; --tries
) {
216 if (sdo_read(sdev
, SDO_CLKCON
) & SDO_TVOUT_CLOCK_READY
)
221 dev_err(sdev
->dev
, "failed to stop streaming\n");
222 return tries
? 0 : -EIO
;
225 static int sdo_s_stream(struct v4l2_subdev
*sd
, int on
)
227 struct sdo_device
*sdev
= sd_to_sdev(sd
);
228 return on
? sdo_streamon(sdev
) : sdo_streamoff(sdev
);
231 static const struct v4l2_subdev_core_ops sdo_sd_core_ops
= {
232 .s_power
= sdo_s_power
,
235 static const struct v4l2_subdev_video_ops sdo_sd_video_ops
= {
236 .s_std_output
= sdo_s_std_output
,
237 .g_std_output
= sdo_g_std_output
,
238 .g_tvnorms_output
= sdo_g_tvnorms_output
,
239 .g_mbus_fmt
= sdo_g_mbus_fmt
,
240 .s_stream
= sdo_s_stream
,
243 static const struct v4l2_subdev_ops sdo_sd_ops
= {
244 .core
= &sdo_sd_core_ops
,
245 .video
= &sdo_sd_video_ops
,
248 static int sdo_runtime_suspend(struct device
*dev
)
250 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
251 struct sdo_device
*sdev
= sd_to_sdev(sd
);
253 dev_info(dev
, "suspend\n");
254 regulator_disable(sdev
->vdet
);
255 regulator_disable(sdev
->vdac
);
256 clk_disable(sdev
->sclk_dac
);
260 static int sdo_runtime_resume(struct device
*dev
)
262 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
263 struct sdo_device
*sdev
= sd_to_sdev(sd
);
265 dev_info(dev
, "resume\n");
266 clk_enable(sdev
->sclk_dac
);
267 regulator_enable(sdev
->vdac
);
268 regulator_enable(sdev
->vdet
);
271 sdo_write_mask(sdev
, SDO_CLKCON
, ~0, SDO_TVOUT_SW_RESET
);
273 sdo_write_mask(sdev
, SDO_CLKCON
, 0, SDO_TVOUT_SW_RESET
);
275 /* setting TV mode */
276 sdo_write_mask(sdev
, SDO_CONFIG
, sdev
->fmt
->cookie
, SDO_STANDARD_MASK
);
277 /* XXX: forcing interlaced mode using undocumented bit */
278 sdo_write_mask(sdev
, SDO_CONFIG
, 0, SDO_PROGRESSIVE
);
279 /* turn all VBI off */
280 sdo_write_mask(sdev
, SDO_VBI
, 0, SDO_CVBS_WSS_INS
|
281 SDO_CVBS_CLOSED_CAPTION_MASK
);
282 /* turn all post processing off */
283 sdo_write_mask(sdev
, SDO_CCCON
, ~0, SDO_COMPENSATION_BHS_ADJ_OFF
|
284 SDO_COMPENSATION_CVBS_COMP_OFF
);
289 static const struct dev_pm_ops sdo_pm_ops
= {
290 .runtime_suspend
= sdo_runtime_suspend
,
291 .runtime_resume
= sdo_runtime_resume
,
294 static int __devinit
sdo_probe(struct platform_device
*pdev
)
296 struct device
*dev
= &pdev
->dev
;
297 struct sdo_device
*sdev
;
298 struct resource
*res
;
300 struct clk
*sclk_vpll
;
302 dev_info(dev
, "probe start\n");
303 sdev
= kzalloc(sizeof *sdev
, GFP_KERNEL
);
305 dev_err(dev
, "not enough memory.\n");
311 /* mapping registers */
312 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
314 dev_err(dev
, "get memory resource failed.\n");
319 sdev
->regs
= ioremap(res
->start
, resource_size(res
));
320 if (sdev
->regs
== NULL
) {
321 dev_err(dev
, "register mapping failed.\n");
326 /* acquiring interrupt */
327 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
329 dev_err(dev
, "get interrupt resource failed.\n");
333 ret
= request_irq(res
->start
, sdo_irq_handler
, 0, "s5p-sdo", sdev
);
335 dev_err(dev
, "request interrupt failed.\n");
338 sdev
->irq
= res
->start
;
341 sdev
->sclk_dac
= clk_get(dev
, "sclk_dac");
342 if (IS_ERR_OR_NULL(sdev
->sclk_dac
)) {
343 dev_err(dev
, "failed to get clock 'sclk_dac'\n");
347 sdev
->dac
= clk_get(dev
, "dac");
348 if (IS_ERR_OR_NULL(sdev
->dac
)) {
349 dev_err(dev
, "failed to get clock 'dac'\n");
353 sdev
->dacphy
= clk_get(dev
, "dacphy");
354 if (IS_ERR_OR_NULL(sdev
->dacphy
)) {
355 dev_err(dev
, "failed to get clock 'dacphy'\n");
359 sclk_vpll
= clk_get(dev
, "sclk_vpll");
360 if (IS_ERR_OR_NULL(sclk_vpll
)) {
361 dev_err(dev
, "failed to get clock 'sclk_vpll'\n");
365 clk_set_parent(sdev
->sclk_dac
, sclk_vpll
);
367 sdev
->fout_vpll
= clk_get(dev
, "fout_vpll");
368 if (IS_ERR_OR_NULL(sdev
->fout_vpll
)) {
369 dev_err(dev
, "failed to get clock 'fout_vpll'\n");
372 dev_info(dev
, "fout_vpll.rate = %lu\n", clk_get_rate(sclk_vpll
));
374 /* acquire regulator */
375 sdev
->vdac
= regulator_get(dev
, "vdd33a_dac");
376 if (IS_ERR_OR_NULL(sdev
->vdac
)) {
377 dev_err(dev
, "failed to get regulator 'vdac'\n");
380 sdev
->vdet
= regulator_get(dev
, "vdet");
381 if (IS_ERR_OR_NULL(sdev
->vdet
)) {
382 dev_err(dev
, "failed to get regulator 'vdet'\n");
386 /* enable gate for dac clock, because mixer uses it */
387 clk_enable(sdev
->dac
);
389 /* configure power management */
390 pm_runtime_enable(dev
);
392 /* configuration of interface subdevice */
393 v4l2_subdev_init(&sdev
->sd
, &sdo_sd_ops
);
394 sdev
->sd
.owner
= THIS_MODULE
;
395 strlcpy(sdev
->sd
.name
, "s5p-sdo", sizeof sdev
->sd
.name
);
397 /* set default format */
398 sdev
->fmt
= sdo_find_format(SDO_DEFAULT_STD
);
399 BUG_ON(sdev
->fmt
== NULL
);
401 /* keeping subdev in device's private for use by other drivers */
402 dev_set_drvdata(dev
, &sdev
->sd
);
404 dev_info(dev
, "probe succeeded\n");
408 regulator_put(sdev
->vdac
);
410 clk_put(sdev
->fout_vpll
);
412 clk_put(sdev
->dacphy
);
416 clk_put(sdev
->sclk_dac
);
418 free_irq(sdev
->irq
, sdev
);
424 dev_info(dev
, "probe failed\n");
428 static int __devexit
sdo_remove(struct platform_device
*pdev
)
430 struct v4l2_subdev
*sd
= dev_get_drvdata(&pdev
->dev
);
431 struct sdo_device
*sdev
= sd_to_sdev(sd
);
433 pm_runtime_disable(&pdev
->dev
);
434 clk_disable(sdev
->dac
);
435 regulator_put(sdev
->vdet
);
436 regulator_put(sdev
->vdac
);
437 clk_put(sdev
->fout_vpll
);
438 clk_put(sdev
->dacphy
);
440 clk_put(sdev
->sclk_dac
);
441 free_irq(sdev
->irq
, sdev
);
445 dev_info(&pdev
->dev
, "remove successful\n");
449 static struct platform_driver sdo_driver __refdata
= {
451 .remove
= __devexit_p(sdo_remove
),
454 .owner
= THIS_MODULE
,
459 static int __init
sdo_init(void)
462 static const char banner
[] __initdata
= KERN_INFO \
463 "Samsung Standard Definition Output (SDO) driver, "
464 "(c) 2010-2011 Samsung Electronics Co., Ltd.\n";
467 ret
= platform_driver_register(&sdo_driver
);
469 printk(KERN_ERR
"SDO platform driver register failed\n");
473 module_init(sdo_init
);
475 static void __exit
sdo_exit(void)
477 platform_driver_unregister(&sdo_driver
);
479 module_exit(sdo_exit
);