sdo_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Samsung Standard Definition Output (SDO) driver
  3. *
  4. * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
  5. *
  6. * Tomasz Stanislawski, <t.stanislaws@samsung.com>
  7. *
  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
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/irq.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/slab.h>
  24. #include <media/v4l2-subdev.h>
  25. #include "regs-sdo.h"
  26. MODULE_AUTHOR("Tomasz Stanislawski, <t.stanislaws@samsung.com>");
  27. MODULE_DESCRIPTION("Samsung Standard Definition Output (SDO)");
  28. MODULE_LICENSE("GPL");
  29. #define SDO_DEFAULT_STD V4L2_STD_PAL
  30. struct sdo_format {
  31. v4l2_std_id id;
  32. /* all modes are 720 pixels wide */
  33. unsigned int height;
  34. unsigned int cookie;
  35. };
  36. struct sdo_device {
  37. /** pointer to device parent */
  38. struct device *dev;
  39. /** base address of SDO registers */
  40. void __iomem *regs;
  41. /** SDO interrupt */
  42. unsigned int irq;
  43. /** DAC source clock */
  44. struct clk *sclk_dac;
  45. /** DAC clock */
  46. struct clk *dac;
  47. /** DAC physical interface */
  48. struct clk *dacphy;
  49. /** clock for control of VPLL */
  50. struct clk *fout_vpll;
  51. /** vpll rate before sdo stream was on */
  52. unsigned long vpll_rate;
  53. /** regulator for SDO IP power */
  54. struct regulator *vdac;
  55. /** regulator for SDO plug detection */
  56. struct regulator *vdet;
  57. /** subdev used as device interface */
  58. struct v4l2_subdev sd;
  59. /** current format */
  60. const struct sdo_format *fmt;
  61. };
  62. static inline struct sdo_device *sd_to_sdev(struct v4l2_subdev *sd)
  63. {
  64. return container_of(sd, struct sdo_device, sd);
  65. }
  66. static inline
  67. void sdo_write_mask(struct sdo_device *sdev, u32 reg_id, u32 value, u32 mask)
  68. {
  69. u32 old = readl(sdev->regs + reg_id);
  70. value = (value & mask) | (old & ~mask);
  71. writel(value, sdev->regs + reg_id);
  72. }
  73. static inline
  74. void sdo_write(struct sdo_device *sdev, u32 reg_id, u32 value)
  75. {
  76. writel(value, sdev->regs + reg_id);
  77. }
  78. static inline
  79. u32 sdo_read(struct sdo_device *sdev, u32 reg_id)
  80. {
  81. return readl(sdev->regs + reg_id);
  82. }
  83. static irqreturn_t sdo_irq_handler(int irq, void *dev_data)
  84. {
  85. struct sdo_device *sdev = dev_data;
  86. /* clear interrupt */
  87. sdo_write_mask(sdev, SDO_IRQ, ~0, SDO_VSYNC_IRQ_PEND);
  88. return IRQ_HANDLED;
  89. }
  90. static void sdo_reg_debug(struct sdo_device *sdev)
  91. {
  92. #define DBGREG(reg_id) \
  93. dev_info(sdev->dev, #reg_id " = %08x\n", \
  94. sdo_read(sdev, reg_id))
  95. DBGREG(SDO_CLKCON);
  96. DBGREG(SDO_CONFIG);
  97. DBGREG(SDO_VBI);
  98. DBGREG(SDO_DAC);
  99. DBGREG(SDO_IRQ);
  100. DBGREG(SDO_IRQMASK);
  101. DBGREG(SDO_VERSION);
  102. }
  103. static const struct sdo_format sdo_format[] = {
  104. { V4L2_STD_PAL_N, .height = 576, .cookie = SDO_PAL_N },
  105. { V4L2_STD_PAL_Nc, .height = 576, .cookie = SDO_PAL_NC },
  106. { V4L2_STD_PAL_M, .height = 480, .cookie = SDO_PAL_M },
  107. { V4L2_STD_PAL_60, .height = 480, .cookie = SDO_PAL_60 },
  108. { V4L2_STD_NTSC_443, .height = 480, .cookie = SDO_NTSC_443 },
  109. { V4L2_STD_PAL, .height = 576, .cookie = SDO_PAL_BGHID },
  110. { V4L2_STD_NTSC_M, .height = 480, .cookie = SDO_NTSC_M },
  111. };
  112. static const struct sdo_format *sdo_find_format(v4l2_std_id id)
  113. {
  114. int i;
  115. for (i = 0; i < ARRAY_SIZE(sdo_format); ++i)
  116. if (sdo_format[i].id & id)
  117. return &sdo_format[i];
  118. return NULL;
  119. }
  120. static int sdo_g_tvnorms_output(struct v4l2_subdev *sd, v4l2_std_id *std)
  121. {
  122. *std = V4L2_STD_NTSC_M | V4L2_STD_PAL_M | V4L2_STD_PAL |
  123. V4L2_STD_PAL_N | V4L2_STD_PAL_Nc |
  124. V4L2_STD_NTSC_443 | V4L2_STD_PAL_60;
  125. return 0;
  126. }
  127. static int sdo_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  128. {
  129. struct sdo_device *sdev = sd_to_sdev(sd);
  130. const struct sdo_format *fmt;
  131. fmt = sdo_find_format(std);
  132. if (fmt == NULL)
  133. return -EINVAL;
  134. sdev->fmt = fmt;
  135. return 0;
  136. }
  137. static int sdo_g_std_output(struct v4l2_subdev *sd, v4l2_std_id *std)
  138. {
  139. *std = sd_to_sdev(sd)->fmt->id;
  140. return 0;
  141. }
  142. static int sdo_get_fmt(struct v4l2_subdev *sd,
  143. struct v4l2_subdev_pad_config *cfg,
  144. struct v4l2_subdev_format *format)
  145. {
  146. struct v4l2_mbus_framefmt *fmt = &format->format;
  147. struct sdo_device *sdev = sd_to_sdev(sd);
  148. if (!sdev->fmt)
  149. return -ENXIO;
  150. if (format->pad)
  151. return -EINVAL;
  152. /* all modes are 720 pixels wide */
  153. fmt->width = 720;
  154. fmt->height = sdev->fmt->height;
  155. fmt->code = MEDIA_BUS_FMT_FIXED;
  156. fmt->field = V4L2_FIELD_INTERLACED;
  157. fmt->colorspace = V4L2_COLORSPACE_JPEG;
  158. return 0;
  159. }
  160. static int sdo_s_power(struct v4l2_subdev *sd, int on)
  161. {
  162. struct sdo_device *sdev = sd_to_sdev(sd);
  163. struct device *dev = sdev->dev;
  164. int ret;
  165. dev_info(dev, "sdo_s_power(%d)\n", on);
  166. if (on)
  167. ret = pm_runtime_get_sync(dev);
  168. else
  169. ret = pm_runtime_put_sync(dev);
  170. /* only values < 0 indicate errors */
  171. return ret < 0 ? ret : 0;
  172. }
  173. static int sdo_streamon(struct sdo_device *sdev)
  174. {
  175. int ret;
  176. /* set proper clock for Timing Generator */
  177. sdev->vpll_rate = clk_get_rate(sdev->fout_vpll);
  178. ret = clk_set_rate(sdev->fout_vpll, 54000000);
  179. if (ret < 0) {
  180. dev_err(sdev->dev, "Failed to set vpll rate\n");
  181. return ret;
  182. }
  183. dev_info(sdev->dev, "fout_vpll.rate = %lu\n",
  184. clk_get_rate(sdev->fout_vpll));
  185. /* enable clock in SDO */
  186. sdo_write_mask(sdev, SDO_CLKCON, ~0, SDO_TVOUT_CLOCK_ON);
  187. ret = clk_prepare_enable(sdev->dacphy);
  188. if (ret < 0) {
  189. dev_err(sdev->dev, "clk_prepare_enable(dacphy) failed\n");
  190. goto fail;
  191. }
  192. /* enable DAC */
  193. sdo_write_mask(sdev, SDO_DAC, ~0, SDO_POWER_ON_DAC);
  194. sdo_reg_debug(sdev);
  195. return 0;
  196. fail:
  197. sdo_write_mask(sdev, SDO_CLKCON, 0, SDO_TVOUT_CLOCK_ON);
  198. clk_set_rate(sdev->fout_vpll, sdev->vpll_rate);
  199. return ret;
  200. }
  201. static int sdo_streamoff(struct sdo_device *sdev)
  202. {
  203. int tries;
  204. sdo_write_mask(sdev, SDO_DAC, 0, SDO_POWER_ON_DAC);
  205. clk_disable_unprepare(sdev->dacphy);
  206. sdo_write_mask(sdev, SDO_CLKCON, 0, SDO_TVOUT_CLOCK_ON);
  207. for (tries = 100; tries; --tries) {
  208. if (sdo_read(sdev, SDO_CLKCON) & SDO_TVOUT_CLOCK_READY)
  209. break;
  210. mdelay(1);
  211. }
  212. if (tries == 0)
  213. dev_err(sdev->dev, "failed to stop streaming\n");
  214. clk_set_rate(sdev->fout_vpll, sdev->vpll_rate);
  215. return tries ? 0 : -EIO;
  216. }
  217. static int sdo_s_stream(struct v4l2_subdev *sd, int on)
  218. {
  219. struct sdo_device *sdev = sd_to_sdev(sd);
  220. return on ? sdo_streamon(sdev) : sdo_streamoff(sdev);
  221. }
  222. static const struct v4l2_subdev_core_ops sdo_sd_core_ops = {
  223. .s_power = sdo_s_power,
  224. };
  225. static const struct v4l2_subdev_video_ops sdo_sd_video_ops = {
  226. .s_std_output = sdo_s_std_output,
  227. .g_std_output = sdo_g_std_output,
  228. .g_tvnorms_output = sdo_g_tvnorms_output,
  229. .s_stream = sdo_s_stream,
  230. };
  231. static const struct v4l2_subdev_pad_ops sdo_sd_pad_ops = {
  232. .get_fmt = sdo_get_fmt,
  233. };
  234. static const struct v4l2_subdev_ops sdo_sd_ops = {
  235. .core = &sdo_sd_core_ops,
  236. .video = &sdo_sd_video_ops,
  237. .pad = &sdo_sd_pad_ops,
  238. };
  239. static int sdo_runtime_suspend(struct device *dev)
  240. {
  241. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  242. struct sdo_device *sdev = sd_to_sdev(sd);
  243. dev_info(dev, "suspend\n");
  244. regulator_disable(sdev->vdet);
  245. regulator_disable(sdev->vdac);
  246. clk_disable_unprepare(sdev->sclk_dac);
  247. return 0;
  248. }
  249. static int sdo_runtime_resume(struct device *dev)
  250. {
  251. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  252. struct sdo_device *sdev = sd_to_sdev(sd);
  253. int ret;
  254. dev_info(dev, "resume\n");
  255. ret = clk_prepare_enable(sdev->sclk_dac);
  256. if (ret < 0)
  257. return ret;
  258. ret = regulator_enable(sdev->vdac);
  259. if (ret < 0)
  260. goto dac_clk_dis;
  261. ret = regulator_enable(sdev->vdet);
  262. if (ret < 0)
  263. goto vdac_r_dis;
  264. /* software reset */
  265. sdo_write_mask(sdev, SDO_CLKCON, ~0, SDO_TVOUT_SW_RESET);
  266. mdelay(10);
  267. sdo_write_mask(sdev, SDO_CLKCON, 0, SDO_TVOUT_SW_RESET);
  268. /* setting TV mode */
  269. sdo_write_mask(sdev, SDO_CONFIG, sdev->fmt->cookie, SDO_STANDARD_MASK);
  270. /* XXX: forcing interlaced mode using undocumented bit */
  271. sdo_write_mask(sdev, SDO_CONFIG, 0, SDO_PROGRESSIVE);
  272. /* turn all VBI off */
  273. sdo_write_mask(sdev, SDO_VBI, 0, SDO_CVBS_WSS_INS |
  274. SDO_CVBS_CLOSED_CAPTION_MASK);
  275. /* turn all post processing off */
  276. sdo_write_mask(sdev, SDO_CCCON, ~0, SDO_COMPENSATION_BHS_ADJ_OFF |
  277. SDO_COMPENSATION_CVBS_COMP_OFF);
  278. sdo_reg_debug(sdev);
  279. return 0;
  280. vdac_r_dis:
  281. regulator_disable(sdev->vdac);
  282. dac_clk_dis:
  283. clk_disable_unprepare(sdev->sclk_dac);
  284. return ret;
  285. }
  286. static const struct dev_pm_ops sdo_pm_ops = {
  287. .runtime_suspend = sdo_runtime_suspend,
  288. .runtime_resume = sdo_runtime_resume,
  289. };
  290. static int sdo_probe(struct platform_device *pdev)
  291. {
  292. struct device *dev = &pdev->dev;
  293. struct sdo_device *sdev;
  294. struct resource *res;
  295. int ret = 0;
  296. struct clk *sclk_vpll;
  297. dev_info(dev, "probe start\n");
  298. sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
  299. if (!sdev) {
  300. dev_err(dev, "not enough memory.\n");
  301. ret = -ENOMEM;
  302. goto fail;
  303. }
  304. sdev->dev = dev;
  305. /* mapping registers */
  306. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  307. if (res == NULL) {
  308. dev_err(dev, "get memory resource failed.\n");
  309. ret = -ENXIO;
  310. goto fail;
  311. }
  312. sdev->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  313. if (sdev->regs == NULL) {
  314. dev_err(dev, "register mapping failed.\n");
  315. ret = -ENXIO;
  316. goto fail;
  317. }
  318. /* acquiring interrupt */
  319. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  320. if (res == NULL) {
  321. dev_err(dev, "get interrupt resource failed.\n");
  322. ret = -ENXIO;
  323. goto fail;
  324. }
  325. ret = devm_request_irq(&pdev->dev, res->start, sdo_irq_handler, 0,
  326. "s5p-sdo", sdev);
  327. if (ret) {
  328. dev_err(dev, "request interrupt failed.\n");
  329. goto fail;
  330. }
  331. sdev->irq = res->start;
  332. /* acquire clocks */
  333. sdev->sclk_dac = clk_get(dev, "sclk_dac");
  334. if (IS_ERR(sdev->sclk_dac)) {
  335. dev_err(dev, "failed to get clock 'sclk_dac'\n");
  336. ret = PTR_ERR(sdev->sclk_dac);
  337. goto fail;
  338. }
  339. sdev->dac = clk_get(dev, "dac");
  340. if (IS_ERR(sdev->dac)) {
  341. dev_err(dev, "failed to get clock 'dac'\n");
  342. ret = PTR_ERR(sdev->dac);
  343. goto fail_sclk_dac;
  344. }
  345. sdev->dacphy = clk_get(dev, "dacphy");
  346. if (IS_ERR(sdev->dacphy)) {
  347. dev_err(dev, "failed to get clock 'dacphy'\n");
  348. ret = PTR_ERR(sdev->dacphy);
  349. goto fail_dac;
  350. }
  351. sclk_vpll = clk_get(dev, "sclk_vpll");
  352. if (IS_ERR(sclk_vpll)) {
  353. dev_err(dev, "failed to get clock 'sclk_vpll'\n");
  354. ret = PTR_ERR(sclk_vpll);
  355. goto fail_dacphy;
  356. }
  357. clk_set_parent(sdev->sclk_dac, sclk_vpll);
  358. clk_put(sclk_vpll);
  359. sdev->fout_vpll = clk_get(dev, "fout_vpll");
  360. if (IS_ERR(sdev->fout_vpll)) {
  361. dev_err(dev, "failed to get clock 'fout_vpll'\n");
  362. ret = PTR_ERR(sdev->fout_vpll);
  363. goto fail_dacphy;
  364. }
  365. dev_info(dev, "fout_vpll.rate = %lu\n", clk_get_rate(sclk_vpll));
  366. /* acquire regulator */
  367. sdev->vdac = devm_regulator_get(dev, "vdd33a_dac");
  368. if (IS_ERR(sdev->vdac)) {
  369. dev_err(dev, "failed to get regulator 'vdac'\n");
  370. ret = PTR_ERR(sdev->vdac);
  371. goto fail_fout_vpll;
  372. }
  373. sdev->vdet = devm_regulator_get(dev, "vdet");
  374. if (IS_ERR(sdev->vdet)) {
  375. dev_err(dev, "failed to get regulator 'vdet'\n");
  376. ret = PTR_ERR(sdev->vdet);
  377. goto fail_fout_vpll;
  378. }
  379. /* enable gate for dac clock, because mixer uses it */
  380. ret = clk_prepare_enable(sdev->dac);
  381. if (ret < 0) {
  382. dev_err(dev, "clk_prepare_enable(dac) failed\n");
  383. goto fail_fout_vpll;
  384. }
  385. /* configure power management */
  386. pm_runtime_enable(dev);
  387. /* configuration of interface subdevice */
  388. v4l2_subdev_init(&sdev->sd, &sdo_sd_ops);
  389. sdev->sd.owner = THIS_MODULE;
  390. strlcpy(sdev->sd.name, "s5p-sdo", sizeof(sdev->sd.name));
  391. /* set default format */
  392. sdev->fmt = sdo_find_format(SDO_DEFAULT_STD);
  393. BUG_ON(sdev->fmt == NULL);
  394. /* keeping subdev in device's private for use by other drivers */
  395. dev_set_drvdata(dev, &sdev->sd);
  396. dev_info(dev, "probe succeeded\n");
  397. return 0;
  398. fail_fout_vpll:
  399. clk_put(sdev->fout_vpll);
  400. fail_dacphy:
  401. clk_put(sdev->dacphy);
  402. fail_dac:
  403. clk_put(sdev->dac);
  404. fail_sclk_dac:
  405. clk_put(sdev->sclk_dac);
  406. fail:
  407. dev_info(dev, "probe failed\n");
  408. return ret;
  409. }
  410. static int sdo_remove(struct platform_device *pdev)
  411. {
  412. struct v4l2_subdev *sd = dev_get_drvdata(&pdev->dev);
  413. struct sdo_device *sdev = sd_to_sdev(sd);
  414. pm_runtime_disable(&pdev->dev);
  415. clk_disable_unprepare(sdev->dac);
  416. clk_put(sdev->fout_vpll);
  417. clk_put(sdev->dacphy);
  418. clk_put(sdev->dac);
  419. clk_put(sdev->sclk_dac);
  420. dev_info(&pdev->dev, "remove successful\n");
  421. return 0;
  422. }
  423. static struct platform_driver sdo_driver __refdata = {
  424. .probe = sdo_probe,
  425. .remove = sdo_remove,
  426. .driver = {
  427. .name = "s5p-sdo",
  428. .pm = &sdo_pm_ops,
  429. }
  430. };
  431. module_platform_driver(sdo_driver);