exynos_drm_mic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright (C) 2015 Samsung Electronics Co.Ltd
  3. * Authors:
  4. * Hyungwon Hwang <human.hwang@samsung.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundationr
  9. */
  10. #include <linux/platform_device.h>
  11. #include <video/of_videomode.h>
  12. #include <linux/of_address.h>
  13. #include <video/videomode.h>
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of.h>
  18. #include <linux/of_graph.h>
  19. #include <linux/clk.h>
  20. #include <drm/drmP.h>
  21. #include <linux/mfd/syscon.h>
  22. #include <linux/regmap.h>
  23. /* Sysreg registers for MIC */
  24. #define DSD_CFG_MUX 0x1004
  25. #define MIC0_RGB_MUX (1 << 0)
  26. #define MIC0_I80_MUX (1 << 1)
  27. #define MIC0_ON_MUX (1 << 5)
  28. /* MIC registers */
  29. #define MIC_OP 0x0
  30. #define MIC_IP_VER 0x0004
  31. #define MIC_V_TIMING_0 0x0008
  32. #define MIC_V_TIMING_1 0x000C
  33. #define MIC_IMG_SIZE 0x0010
  34. #define MIC_INPUT_TIMING_0 0x0014
  35. #define MIC_INPUT_TIMING_1 0x0018
  36. #define MIC_2D_OUTPUT_TIMING_0 0x001C
  37. #define MIC_2D_OUTPUT_TIMING_1 0x0020
  38. #define MIC_2D_OUTPUT_TIMING_2 0x0024
  39. #define MIC_3D_OUTPUT_TIMING_0 0x0028
  40. #define MIC_3D_OUTPUT_TIMING_1 0x002C
  41. #define MIC_3D_OUTPUT_TIMING_2 0x0030
  42. #define MIC_CORE_PARA_0 0x0034
  43. #define MIC_CORE_PARA_1 0x0038
  44. #define MIC_CTC_CTRL 0x0040
  45. #define MIC_RD_DATA 0x0044
  46. #define MIC_UPD_REG (1 << 31)
  47. #define MIC_ON_REG (1 << 30)
  48. #define MIC_TD_ON_REG (1 << 29)
  49. #define MIC_BS_CHG_OUT (1 << 16)
  50. #define MIC_VIDEO_TYPE(x) (((x) & 0xf) << 12)
  51. #define MIC_PSR_EN (1 << 5)
  52. #define MIC_SW_RST (1 << 4)
  53. #define MIC_ALL_RST (1 << 3)
  54. #define MIC_CORE_VER_CONTROL (1 << 2)
  55. #define MIC_MODE_SEL_COMMAND_MODE (1 << 1)
  56. #define MIC_MODE_SEL_MASK (1 << 1)
  57. #define MIC_CORE_EN (1 << 0)
  58. #define MIC_V_PULSE_WIDTH(x) (((x) & 0x3fff) << 16)
  59. #define MIC_V_PERIOD_LINE(x) ((x) & 0x3fff)
  60. #define MIC_VBP_SIZE(x) (((x) & 0x3fff) << 16)
  61. #define MIC_VFP_SIZE(x) ((x) & 0x3fff)
  62. #define MIC_IMG_V_SIZE(x) (((x) & 0x3fff) << 16)
  63. #define MIC_IMG_H_SIZE(x) ((x) & 0x3fff)
  64. #define MIC_H_PULSE_WIDTH_IN(x) (((x) & 0x3fff) << 16)
  65. #define MIC_H_PERIOD_PIXEL_IN(x) ((x) & 0x3fff)
  66. #define MIC_HBP_SIZE_IN(x) (((x) & 0x3fff) << 16)
  67. #define MIC_HFP_SIZE_IN(x) ((x) & 0x3fff)
  68. #define MIC_H_PULSE_WIDTH_2D(x) (((x) & 0x3fff) << 16)
  69. #define MIC_H_PERIOD_PIXEL_2D(x) ((x) & 0x3fff)
  70. #define MIC_HBP_SIZE_2D(x) (((x) & 0x3fff) << 16)
  71. #define MIC_HFP_SIZE_2D(x) ((x) & 0x3fff)
  72. #define MIC_BS_SIZE_2D(x) ((x) & 0x3fff)
  73. enum {
  74. ENDPOINT_DECON_NODE,
  75. ENDPOINT_DSI_NODE,
  76. NUM_ENDPOINTS
  77. };
  78. static char *clk_names[] = { "pclk_mic0", "sclk_rgb_vclk_to_mic0" };
  79. #define NUM_CLKS ARRAY_SIZE(clk_names)
  80. static DEFINE_MUTEX(mic_mutex);
  81. struct exynos_mic {
  82. struct device *dev;
  83. void __iomem *reg;
  84. struct regmap *sysreg;
  85. struct clk *clks[NUM_CLKS];
  86. bool i80_mode;
  87. struct videomode vm;
  88. struct drm_encoder *encoder;
  89. struct drm_bridge bridge;
  90. bool enabled;
  91. };
  92. static void mic_set_path(struct exynos_mic *mic, bool enable)
  93. {
  94. int ret;
  95. unsigned int val;
  96. ret = regmap_read(mic->sysreg, DSD_CFG_MUX, &val);
  97. if (ret) {
  98. DRM_ERROR("mic: Failed to read system register\n");
  99. return;
  100. }
  101. if (enable) {
  102. if (mic->i80_mode)
  103. val |= MIC0_I80_MUX;
  104. else
  105. val |= MIC0_RGB_MUX;
  106. val |= MIC0_ON_MUX;
  107. } else
  108. val &= ~(MIC0_RGB_MUX | MIC0_I80_MUX | MIC0_ON_MUX);
  109. regmap_write(mic->sysreg, DSD_CFG_MUX, val);
  110. if (ret)
  111. DRM_ERROR("mic: Failed to read system register\n");
  112. }
  113. static int mic_sw_reset(struct exynos_mic *mic)
  114. {
  115. unsigned int retry = 100;
  116. int ret;
  117. writel(MIC_SW_RST, mic->reg + MIC_OP);
  118. while (retry-- > 0) {
  119. ret = readl(mic->reg + MIC_OP);
  120. if (!(ret & MIC_SW_RST))
  121. return 0;
  122. udelay(10);
  123. }
  124. return -ETIMEDOUT;
  125. }
  126. static void mic_set_porch_timing(struct exynos_mic *mic)
  127. {
  128. struct videomode vm = mic->vm;
  129. u32 reg;
  130. reg = MIC_V_PULSE_WIDTH(vm.vsync_len) +
  131. MIC_V_PERIOD_LINE(vm.vsync_len + vm.vactive +
  132. vm.vback_porch + vm.vfront_porch);
  133. writel(reg, mic->reg + MIC_V_TIMING_0);
  134. reg = MIC_VBP_SIZE(vm.vback_porch) +
  135. MIC_VFP_SIZE(vm.vfront_porch);
  136. writel(reg, mic->reg + MIC_V_TIMING_1);
  137. reg = MIC_V_PULSE_WIDTH(vm.hsync_len) +
  138. MIC_V_PERIOD_LINE(vm.hsync_len + vm.hactive +
  139. vm.hback_porch + vm.hfront_porch);
  140. writel(reg, mic->reg + MIC_INPUT_TIMING_0);
  141. reg = MIC_VBP_SIZE(vm.hback_porch) +
  142. MIC_VFP_SIZE(vm.hfront_porch);
  143. writel(reg, mic->reg + MIC_INPUT_TIMING_1);
  144. }
  145. static void mic_set_img_size(struct exynos_mic *mic)
  146. {
  147. struct videomode *vm = &mic->vm;
  148. u32 reg;
  149. reg = MIC_IMG_H_SIZE(vm->hactive) +
  150. MIC_IMG_V_SIZE(vm->vactive);
  151. writel(reg, mic->reg + MIC_IMG_SIZE);
  152. }
  153. static void mic_set_output_timing(struct exynos_mic *mic)
  154. {
  155. struct videomode vm = mic->vm;
  156. u32 reg, bs_size_2d;
  157. DRM_DEBUG("w: %u, h: %u\n", vm.hactive, vm.vactive);
  158. bs_size_2d = ((vm.hactive >> 2) << 1) + (vm.vactive % 4);
  159. reg = MIC_BS_SIZE_2D(bs_size_2d);
  160. writel(reg, mic->reg + MIC_2D_OUTPUT_TIMING_2);
  161. if (!mic->i80_mode) {
  162. reg = MIC_H_PULSE_WIDTH_2D(vm.hsync_len) +
  163. MIC_H_PERIOD_PIXEL_2D(vm.hsync_len + bs_size_2d +
  164. vm.hback_porch + vm.hfront_porch);
  165. writel(reg, mic->reg + MIC_2D_OUTPUT_TIMING_0);
  166. reg = MIC_HBP_SIZE_2D(vm.hback_porch) +
  167. MIC_H_PERIOD_PIXEL_2D(vm.hfront_porch);
  168. writel(reg, mic->reg + MIC_2D_OUTPUT_TIMING_1);
  169. }
  170. }
  171. static void mic_set_reg_on(struct exynos_mic *mic, bool enable)
  172. {
  173. u32 reg = readl(mic->reg + MIC_OP);
  174. if (enable) {
  175. reg &= ~(MIC_MODE_SEL_MASK | MIC_CORE_VER_CONTROL | MIC_PSR_EN);
  176. reg |= (MIC_CORE_EN | MIC_BS_CHG_OUT | MIC_ON_REG);
  177. reg &= ~MIC_MODE_SEL_COMMAND_MODE;
  178. if (mic->i80_mode)
  179. reg |= MIC_MODE_SEL_COMMAND_MODE;
  180. } else {
  181. reg &= ~MIC_CORE_EN;
  182. }
  183. reg |= MIC_UPD_REG;
  184. writel(reg, mic->reg + MIC_OP);
  185. }
  186. static struct device_node *get_remote_node(struct device_node *from, int reg)
  187. {
  188. struct device_node *endpoint = NULL, *remote_node = NULL;
  189. endpoint = of_graph_get_endpoint_by_regs(from, reg, -1);
  190. if (!endpoint) {
  191. DRM_ERROR("mic: Failed to find remote port from %s",
  192. from->full_name);
  193. goto exit;
  194. }
  195. remote_node = of_graph_get_remote_port_parent(endpoint);
  196. if (!remote_node) {
  197. DRM_ERROR("mic: Failed to find remote port parent from %s",
  198. from->full_name);
  199. goto exit;
  200. }
  201. exit:
  202. of_node_put(endpoint);
  203. return remote_node;
  204. }
  205. static int parse_dt(struct exynos_mic *mic)
  206. {
  207. int ret = 0, i, j;
  208. struct device_node *remote_node;
  209. struct device_node *nodes[3];
  210. /*
  211. * The order of endpoints does matter.
  212. * The first node must be for decon and the second one must be for dsi.
  213. */
  214. for (i = 0, j = 0; i < NUM_ENDPOINTS; i++) {
  215. remote_node = get_remote_node(mic->dev->of_node, i);
  216. if (!remote_node) {
  217. ret = -EPIPE;
  218. goto exit;
  219. }
  220. nodes[j++] = remote_node;
  221. switch (i) {
  222. case ENDPOINT_DECON_NODE:
  223. /* decon node */
  224. if (of_get_child_by_name(remote_node,
  225. "i80-if-timings"))
  226. mic->i80_mode = 1;
  227. break;
  228. case ENDPOINT_DSI_NODE:
  229. /* panel node */
  230. remote_node = get_remote_node(remote_node, 1);
  231. if (!remote_node) {
  232. ret = -EPIPE;
  233. goto exit;
  234. }
  235. nodes[j++] = remote_node;
  236. ret = of_get_videomode(remote_node,
  237. &mic->vm, 0);
  238. if (ret) {
  239. DRM_ERROR("mic: failed to get videomode");
  240. goto exit;
  241. }
  242. break;
  243. default:
  244. DRM_ERROR("mic: Unknown endpoint from MIC");
  245. break;
  246. }
  247. }
  248. exit:
  249. while (--j > -1)
  250. of_node_put(nodes[j]);
  251. return ret;
  252. }
  253. void mic_disable(struct drm_bridge *bridge) { }
  254. void mic_post_disable(struct drm_bridge *bridge)
  255. {
  256. struct exynos_mic *mic = bridge->driver_private;
  257. int i;
  258. mutex_lock(&mic_mutex);
  259. if (!mic->enabled)
  260. goto already_disabled;
  261. mic_set_path(mic, 0);
  262. for (i = NUM_CLKS - 1; i > -1; i--)
  263. clk_disable_unprepare(mic->clks[i]);
  264. mic->enabled = 0;
  265. already_disabled:
  266. mutex_unlock(&mic_mutex);
  267. }
  268. void mic_pre_enable(struct drm_bridge *bridge)
  269. {
  270. struct exynos_mic *mic = bridge->driver_private;
  271. int ret, i;
  272. mutex_lock(&mic_mutex);
  273. if (mic->enabled)
  274. goto already_enabled;
  275. for (i = 0; i < NUM_CLKS; i++) {
  276. ret = clk_prepare_enable(mic->clks[i]);
  277. if (ret < 0) {
  278. DRM_ERROR("Failed to enable clock (%s)\n",
  279. clk_names[i]);
  280. goto turn_off_clks;
  281. }
  282. }
  283. mic_set_path(mic, 1);
  284. ret = mic_sw_reset(mic);
  285. if (ret) {
  286. DRM_ERROR("Failed to reset\n");
  287. goto turn_off_clks;
  288. }
  289. if (!mic->i80_mode)
  290. mic_set_porch_timing(mic);
  291. mic_set_img_size(mic);
  292. mic_set_output_timing(mic);
  293. mic_set_reg_on(mic, 1);
  294. mic->enabled = 1;
  295. mutex_unlock(&mic_mutex);
  296. return;
  297. turn_off_clks:
  298. while (--i > -1)
  299. clk_disable_unprepare(mic->clks[i]);
  300. already_enabled:
  301. mutex_unlock(&mic_mutex);
  302. }
  303. void mic_enable(struct drm_bridge *bridge) { }
  304. void mic_destroy(struct drm_bridge *bridge)
  305. {
  306. struct exynos_mic *mic = bridge->driver_private;
  307. int i;
  308. mutex_lock(&mic_mutex);
  309. if (!mic->enabled)
  310. goto already_disabled;
  311. for (i = NUM_CLKS - 1; i > -1; i--)
  312. clk_disable_unprepare(mic->clks[i]);
  313. already_disabled:
  314. mutex_unlock(&mic_mutex);
  315. }
  316. struct drm_bridge_funcs mic_bridge_funcs = {
  317. .disable = mic_disable,
  318. .post_disable = mic_post_disable,
  319. .pre_enable = mic_pre_enable,
  320. .enable = mic_enable,
  321. };
  322. int exynos_mic_probe(struct platform_device *pdev)
  323. {
  324. struct device *dev = &pdev->dev;
  325. struct exynos_mic *mic;
  326. struct resource res;
  327. int ret, i;
  328. mic = devm_kzalloc(dev, sizeof(*mic), GFP_KERNEL);
  329. if (!mic) {
  330. DRM_ERROR("mic: Failed to allocate memory for MIC object\n");
  331. ret = -ENOMEM;
  332. goto err;
  333. }
  334. mic->dev = dev;
  335. ret = parse_dt(mic);
  336. if (ret)
  337. goto err;
  338. ret = of_address_to_resource(dev->of_node, 0, &res);
  339. if (ret) {
  340. DRM_ERROR("mic: Failed to get mem region for MIC\n");
  341. goto err;
  342. }
  343. mic->reg = devm_ioremap(dev, res.start, resource_size(&res));
  344. if (!mic->reg) {
  345. DRM_ERROR("mic: Failed to remap for MIC\n");
  346. ret = -ENOMEM;
  347. goto err;
  348. }
  349. mic->sysreg = syscon_regmap_lookup_by_phandle(dev->of_node,
  350. "samsung,disp-syscon");
  351. if (IS_ERR(mic->sysreg)) {
  352. DRM_ERROR("mic: Failed to get system register.\n");
  353. goto err;
  354. }
  355. mic->bridge.funcs = &mic_bridge_funcs;
  356. mic->bridge.of_node = dev->of_node;
  357. mic->bridge.driver_private = mic;
  358. ret = drm_bridge_add(&mic->bridge);
  359. if (ret) {
  360. DRM_ERROR("mic: Failed to add MIC to the global bridge list\n");
  361. goto err;
  362. }
  363. for (i = 0; i < NUM_CLKS; i++) {
  364. mic->clks[i] = of_clk_get_by_name(dev->of_node, clk_names[i]);
  365. if (IS_ERR(mic->clks[i])) {
  366. DRM_ERROR("mic: Failed to get clock (%s)\n",
  367. clk_names[i]);
  368. ret = PTR_ERR(mic->clks[i]);
  369. goto err;
  370. }
  371. }
  372. DRM_DEBUG_KMS("MIC has been probed\n");
  373. err:
  374. return ret;
  375. }
  376. static int exynos_mic_remove(struct platform_device *pdev)
  377. {
  378. struct exynos_mic *mic = platform_get_drvdata(pdev);
  379. int i;
  380. drm_bridge_remove(&mic->bridge);
  381. for (i = NUM_CLKS - 1; i > -1; i--)
  382. clk_put(mic->clks[i]);
  383. return 0;
  384. }
  385. static const struct of_device_id exynos_mic_of_match[] = {
  386. { .compatible = "samsung,exynos5433-mic" },
  387. { }
  388. };
  389. MODULE_DEVICE_TABLE(of, exynos_mic_of_match);
  390. struct platform_driver mic_driver = {
  391. .probe = exynos_mic_probe,
  392. .remove = exynos_mic_remove,
  393. .driver = {
  394. .name = "exynos-mic",
  395. .owner = THIS_MODULE,
  396. .of_match_table = exynos_mic_of_match,
  397. },
  398. };