sti_dvo.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2014
  3. * Author: Vincent Abriou <vincent.abriou@st.com> for STMicroelectronics.
  4. * License terms: GNU General Public License (GPL), version 2
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/component.h>
  8. #include <linux/module.h>
  9. #include <linux/of_gpio.h>
  10. #include <linux/platform_device.h>
  11. #include <drm/drmP.h>
  12. #include <drm/drm_atomic_helper.h>
  13. #include <drm/drm_crtc_helper.h>
  14. #include <drm/drm_panel.h>
  15. #include "sti_awg_utils.h"
  16. #include "sti_mixer.h"
  17. /* DVO registers */
  18. #define DVO_AWG_DIGSYNC_CTRL 0x0000
  19. #define DVO_DOF_CFG 0x0004
  20. #define DVO_LUT_PROG_LOW 0x0008
  21. #define DVO_LUT_PROG_MID 0x000C
  22. #define DVO_LUT_PROG_HIGH 0x0010
  23. #define DVO_DIGSYNC_INSTR_I 0x0100
  24. #define DVO_AWG_CTRL_EN BIT(0)
  25. #define DVO_AWG_FRAME_BASED_SYNC BIT(2)
  26. #define DVO_DOF_EN_LOWBYTE BIT(0)
  27. #define DVO_DOF_EN_MIDBYTE BIT(1)
  28. #define DVO_DOF_EN_HIGHBYTE BIT(2)
  29. #define DVO_DOF_EN BIT(6)
  30. #define DVO_DOF_MOD_COUNT_SHIFT 8
  31. #define DVO_LUT_ZERO 0
  32. #define DVO_LUT_Y_G 1
  33. #define DVO_LUT_Y_G_DEL 2
  34. #define DVO_LUT_CB_B 3
  35. #define DVO_LUT_CB_B_DEL 4
  36. #define DVO_LUT_CR_R 5
  37. #define DVO_LUT_CR_R_DEL 6
  38. #define DVO_LUT_HOLD 7
  39. struct dvo_config {
  40. u32 flags;
  41. u32 lowbyte;
  42. u32 midbyte;
  43. u32 highbyte;
  44. int (*awg_fwgen_fct)(
  45. struct awg_code_generation_params *fw_gen_params,
  46. struct awg_timing *timing);
  47. };
  48. static struct dvo_config rgb_24bit_de_cfg = {
  49. .flags = (0L << DVO_DOF_MOD_COUNT_SHIFT),
  50. .lowbyte = DVO_LUT_CR_R,
  51. .midbyte = DVO_LUT_Y_G,
  52. .highbyte = DVO_LUT_CB_B,
  53. .awg_fwgen_fct = sti_awg_generate_code_data_enable_mode,
  54. };
  55. /**
  56. * STI digital video output structure
  57. *
  58. * @dev: driver device
  59. * @drm_dev: pointer to drm device
  60. * @mode: current display mode selected
  61. * @regs: dvo registers
  62. * @clk_pix: pixel clock for dvo
  63. * @clk: clock for dvo
  64. * @clk_main_parent: dvo parent clock if main path used
  65. * @clk_aux_parent: dvo parent clock if aux path used
  66. * @panel_node: panel node reference from device tree
  67. * @panel: reference to the panel connected to the dvo
  68. * @enabled: true if dvo is enabled else false
  69. * @encoder: drm_encoder it is bound
  70. */
  71. struct sti_dvo {
  72. struct device dev;
  73. struct drm_device *drm_dev;
  74. struct drm_display_mode mode;
  75. void __iomem *regs;
  76. struct clk *clk_pix;
  77. struct clk *clk;
  78. struct clk *clk_main_parent;
  79. struct clk *clk_aux_parent;
  80. struct device_node *panel_node;
  81. struct drm_panel *panel;
  82. struct dvo_config *config;
  83. bool enabled;
  84. struct drm_encoder *encoder;
  85. struct drm_bridge *bridge;
  86. };
  87. struct sti_dvo_connector {
  88. struct drm_connector drm_connector;
  89. struct drm_encoder *encoder;
  90. struct sti_dvo *dvo;
  91. };
  92. #define to_sti_dvo_connector(x) \
  93. container_of(x, struct sti_dvo_connector, drm_connector)
  94. #define BLANKING_LEVEL 16
  95. int dvo_awg_generate_code(struct sti_dvo *dvo, u8 *ram_size, u32 *ram_code)
  96. {
  97. struct drm_display_mode *mode = &dvo->mode;
  98. struct dvo_config *config = dvo->config;
  99. struct awg_code_generation_params fw_gen_params;
  100. struct awg_timing timing;
  101. fw_gen_params.ram_code = ram_code;
  102. fw_gen_params.instruction_offset = 0;
  103. timing.total_lines = mode->vtotal;
  104. timing.active_lines = mode->vdisplay;
  105. timing.blanking_lines = mode->vsync_start - mode->vdisplay;
  106. timing.trailing_lines = mode->vtotal - mode->vsync_start;
  107. timing.total_pixels = mode->htotal;
  108. timing.active_pixels = mode->hdisplay;
  109. timing.blanking_pixels = mode->hsync_start - mode->hdisplay;
  110. timing.trailing_pixels = mode->htotal - mode->hsync_start;
  111. timing.blanking_level = BLANKING_LEVEL;
  112. if (config->awg_fwgen_fct(&fw_gen_params, &timing)) {
  113. DRM_ERROR("AWG firmware not properly generated\n");
  114. return -EINVAL;
  115. }
  116. *ram_size = fw_gen_params.instruction_offset;
  117. return 0;
  118. }
  119. /* Configure AWG, writing instructions
  120. *
  121. * @dvo: pointer to DVO structure
  122. * @awg_ram_code: pointer to AWG instructions table
  123. * @nb: nb of AWG instructions
  124. */
  125. static void dvo_awg_configure(struct sti_dvo *dvo, u32 *awg_ram_code, int nb)
  126. {
  127. int i;
  128. DRM_DEBUG_DRIVER("\n");
  129. for (i = 0; i < nb; i++)
  130. writel(awg_ram_code[i],
  131. dvo->regs + DVO_DIGSYNC_INSTR_I + i * 4);
  132. for (i = nb; i < AWG_MAX_INST; i++)
  133. writel(0, dvo->regs + DVO_DIGSYNC_INSTR_I + i * 4);
  134. writel(DVO_AWG_CTRL_EN, dvo->regs + DVO_AWG_DIGSYNC_CTRL);
  135. }
  136. static void sti_dvo_disable(struct drm_bridge *bridge)
  137. {
  138. struct sti_dvo *dvo = bridge->driver_private;
  139. if (!dvo->enabled)
  140. return;
  141. DRM_DEBUG_DRIVER("\n");
  142. if (dvo->config->awg_fwgen_fct)
  143. writel(0x00000000, dvo->regs + DVO_AWG_DIGSYNC_CTRL);
  144. writel(0x00000000, dvo->regs + DVO_DOF_CFG);
  145. if (dvo->panel)
  146. dvo->panel->funcs->disable(dvo->panel);
  147. /* Disable/unprepare dvo clock */
  148. clk_disable_unprepare(dvo->clk_pix);
  149. clk_disable_unprepare(dvo->clk);
  150. dvo->enabled = false;
  151. }
  152. static void sti_dvo_pre_enable(struct drm_bridge *bridge)
  153. {
  154. struct sti_dvo *dvo = bridge->driver_private;
  155. struct dvo_config *config = dvo->config;
  156. u32 val;
  157. DRM_DEBUG_DRIVER("\n");
  158. if (dvo->enabled)
  159. return;
  160. /* Make sure DVO is disabled */
  161. writel(0x00000000, dvo->regs + DVO_DOF_CFG);
  162. writel(0x00000000, dvo->regs + DVO_AWG_DIGSYNC_CTRL);
  163. if (config->awg_fwgen_fct) {
  164. u8 nb_instr;
  165. u32 awg_ram_code[AWG_MAX_INST];
  166. /* Configure AWG */
  167. if (!dvo_awg_generate_code(dvo, &nb_instr, awg_ram_code))
  168. dvo_awg_configure(dvo, awg_ram_code, nb_instr);
  169. else
  170. return;
  171. }
  172. /* Prepare/enable clocks */
  173. if (clk_prepare_enable(dvo->clk_pix))
  174. DRM_ERROR("Failed to prepare/enable dvo_pix clk\n");
  175. if (clk_prepare_enable(dvo->clk))
  176. DRM_ERROR("Failed to prepare/enable dvo clk\n");
  177. if (dvo->panel)
  178. dvo->panel->funcs->enable(dvo->panel);
  179. /* Set LUT */
  180. writel(config->lowbyte, dvo->regs + DVO_LUT_PROG_LOW);
  181. writel(config->midbyte, dvo->regs + DVO_LUT_PROG_MID);
  182. writel(config->highbyte, dvo->regs + DVO_LUT_PROG_HIGH);
  183. /* Digital output formatter config */
  184. val = (config->flags | DVO_DOF_EN);
  185. writel(val, dvo->regs + DVO_DOF_CFG);
  186. dvo->enabled = true;
  187. }
  188. static void sti_dvo_set_mode(struct drm_bridge *bridge,
  189. struct drm_display_mode *mode,
  190. struct drm_display_mode *adjusted_mode)
  191. {
  192. struct sti_dvo *dvo = bridge->driver_private;
  193. struct sti_mixer *mixer = to_sti_mixer(dvo->encoder->crtc);
  194. int rate = mode->clock * 1000;
  195. struct clk *clkp;
  196. int ret;
  197. DRM_DEBUG_DRIVER("\n");
  198. memcpy(&dvo->mode, mode, sizeof(struct drm_display_mode));
  199. /* According to the path used (main or aux), the dvo clocks should
  200. * have a different parent clock. */
  201. if (mixer->id == STI_MIXER_MAIN)
  202. clkp = dvo->clk_main_parent;
  203. else
  204. clkp = dvo->clk_aux_parent;
  205. if (clkp) {
  206. clk_set_parent(dvo->clk_pix, clkp);
  207. clk_set_parent(dvo->clk, clkp);
  208. }
  209. /* DVO clocks = compositor clock */
  210. ret = clk_set_rate(dvo->clk_pix, rate);
  211. if (ret < 0) {
  212. DRM_ERROR("Cannot set rate (%dHz) for dvo_pix clk\n", rate);
  213. return;
  214. }
  215. ret = clk_set_rate(dvo->clk, rate);
  216. if (ret < 0) {
  217. DRM_ERROR("Cannot set rate (%dHz) for dvo clk\n", rate);
  218. return;
  219. }
  220. /* For now, we only support 24bit data enable (DE) synchro format */
  221. dvo->config = &rgb_24bit_de_cfg;
  222. }
  223. static void sti_dvo_bridge_nope(struct drm_bridge *bridge)
  224. {
  225. /* do nothing */
  226. }
  227. static const struct drm_bridge_funcs sti_dvo_bridge_funcs = {
  228. .pre_enable = sti_dvo_pre_enable,
  229. .enable = sti_dvo_bridge_nope,
  230. .disable = sti_dvo_disable,
  231. .post_disable = sti_dvo_bridge_nope,
  232. .mode_set = sti_dvo_set_mode,
  233. };
  234. static int sti_dvo_connector_get_modes(struct drm_connector *connector)
  235. {
  236. struct sti_dvo_connector *dvo_connector
  237. = to_sti_dvo_connector(connector);
  238. struct sti_dvo *dvo = dvo_connector->dvo;
  239. if (dvo->panel)
  240. return dvo->panel->funcs->get_modes(dvo->panel);
  241. return 0;
  242. }
  243. #define CLK_TOLERANCE_HZ 50
  244. static int sti_dvo_connector_mode_valid(struct drm_connector *connector,
  245. struct drm_display_mode *mode)
  246. {
  247. int target = mode->clock * 1000;
  248. int target_min = target - CLK_TOLERANCE_HZ;
  249. int target_max = target + CLK_TOLERANCE_HZ;
  250. int result;
  251. struct sti_dvo_connector *dvo_connector
  252. = to_sti_dvo_connector(connector);
  253. struct sti_dvo *dvo = dvo_connector->dvo;
  254. result = clk_round_rate(dvo->clk_pix, target);
  255. DRM_DEBUG_DRIVER("target rate = %d => available rate = %d\n",
  256. target, result);
  257. if ((result < target_min) || (result > target_max)) {
  258. DRM_DEBUG_DRIVER("dvo pixclk=%d not supported\n", target);
  259. return MODE_BAD;
  260. }
  261. return MODE_OK;
  262. }
  263. struct drm_encoder *sti_dvo_best_encoder(struct drm_connector *connector)
  264. {
  265. struct sti_dvo_connector *dvo_connector
  266. = to_sti_dvo_connector(connector);
  267. /* Best encoder is the one associated during connector creation */
  268. return dvo_connector->encoder;
  269. }
  270. static const
  271. struct drm_connector_helper_funcs sti_dvo_connector_helper_funcs = {
  272. .get_modes = sti_dvo_connector_get_modes,
  273. .mode_valid = sti_dvo_connector_mode_valid,
  274. .best_encoder = sti_dvo_best_encoder,
  275. };
  276. static enum drm_connector_status
  277. sti_dvo_connector_detect(struct drm_connector *connector, bool force)
  278. {
  279. struct sti_dvo_connector *dvo_connector
  280. = to_sti_dvo_connector(connector);
  281. struct sti_dvo *dvo = dvo_connector->dvo;
  282. DRM_DEBUG_DRIVER("\n");
  283. if (!dvo->panel)
  284. dvo->panel = of_drm_find_panel(dvo->panel_node);
  285. if (dvo->panel)
  286. if (!drm_panel_attach(dvo->panel, connector))
  287. return connector_status_connected;
  288. return connector_status_disconnected;
  289. }
  290. static void sti_dvo_connector_destroy(struct drm_connector *connector)
  291. {
  292. struct sti_dvo_connector *dvo_connector
  293. = to_sti_dvo_connector(connector);
  294. drm_connector_unregister(connector);
  295. drm_connector_cleanup(connector);
  296. kfree(dvo_connector);
  297. }
  298. static const struct drm_connector_funcs sti_dvo_connector_funcs = {
  299. .dpms = drm_atomic_helper_connector_dpms,
  300. .fill_modes = drm_helper_probe_single_connector_modes,
  301. .detect = sti_dvo_connector_detect,
  302. .destroy = sti_dvo_connector_destroy,
  303. .reset = drm_atomic_helper_connector_reset,
  304. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  305. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  306. };
  307. static struct drm_encoder *sti_dvo_find_encoder(struct drm_device *dev)
  308. {
  309. struct drm_encoder *encoder;
  310. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  311. if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
  312. return encoder;
  313. }
  314. return NULL;
  315. }
  316. static int sti_dvo_bind(struct device *dev, struct device *master, void *data)
  317. {
  318. struct sti_dvo *dvo = dev_get_drvdata(dev);
  319. struct drm_device *drm_dev = data;
  320. struct drm_encoder *encoder;
  321. struct sti_dvo_connector *connector;
  322. struct drm_connector *drm_connector;
  323. struct drm_bridge *bridge;
  324. int err;
  325. /* Set the drm device handle */
  326. dvo->drm_dev = drm_dev;
  327. encoder = sti_dvo_find_encoder(drm_dev);
  328. if (!encoder)
  329. return -ENOMEM;
  330. connector = devm_kzalloc(dev, sizeof(*connector), GFP_KERNEL);
  331. if (!connector)
  332. return -ENOMEM;
  333. connector->dvo = dvo;
  334. bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
  335. if (!bridge)
  336. return -ENOMEM;
  337. bridge->driver_private = dvo;
  338. bridge->funcs = &sti_dvo_bridge_funcs;
  339. bridge->of_node = dvo->dev.of_node;
  340. err = drm_bridge_add(bridge);
  341. if (err) {
  342. DRM_ERROR("Failed to add bridge\n");
  343. return err;
  344. }
  345. err = drm_bridge_attach(drm_dev, bridge);
  346. if (err) {
  347. DRM_ERROR("Failed to attach bridge\n");
  348. return err;
  349. }
  350. dvo->bridge = bridge;
  351. encoder->bridge = bridge;
  352. connector->encoder = encoder;
  353. dvo->encoder = encoder;
  354. drm_connector = (struct drm_connector *)connector;
  355. drm_connector->polled = DRM_CONNECTOR_POLL_HPD;
  356. drm_connector_init(drm_dev, drm_connector,
  357. &sti_dvo_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
  358. drm_connector_helper_add(drm_connector,
  359. &sti_dvo_connector_helper_funcs);
  360. err = drm_connector_register(drm_connector);
  361. if (err)
  362. goto err_connector;
  363. err = drm_mode_connector_attach_encoder(drm_connector, encoder);
  364. if (err) {
  365. DRM_ERROR("Failed to attach a connector to a encoder\n");
  366. goto err_sysfs;
  367. }
  368. return 0;
  369. err_sysfs:
  370. drm_connector_unregister(drm_connector);
  371. err_connector:
  372. drm_bridge_remove(bridge);
  373. drm_connector_cleanup(drm_connector);
  374. return -EINVAL;
  375. }
  376. static void sti_dvo_unbind(struct device *dev,
  377. struct device *master, void *data)
  378. {
  379. struct sti_dvo *dvo = dev_get_drvdata(dev);
  380. drm_bridge_remove(dvo->bridge);
  381. }
  382. static const struct component_ops sti_dvo_ops = {
  383. .bind = sti_dvo_bind,
  384. .unbind = sti_dvo_unbind,
  385. };
  386. static int sti_dvo_probe(struct platform_device *pdev)
  387. {
  388. struct device *dev = &pdev->dev;
  389. struct sti_dvo *dvo;
  390. struct resource *res;
  391. struct device_node *np = dev->of_node;
  392. DRM_INFO("%s\n", __func__);
  393. dvo = devm_kzalloc(dev, sizeof(*dvo), GFP_KERNEL);
  394. if (!dvo) {
  395. DRM_ERROR("Failed to allocate memory for DVO\n");
  396. return -ENOMEM;
  397. }
  398. dvo->dev = pdev->dev;
  399. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvo-reg");
  400. if (!res) {
  401. DRM_ERROR("Invalid dvo resource\n");
  402. return -ENOMEM;
  403. }
  404. dvo->regs = devm_ioremap_nocache(dev, res->start,
  405. resource_size(res));
  406. if (!dvo->regs)
  407. return -ENOMEM;
  408. dvo->clk_pix = devm_clk_get(dev, "dvo_pix");
  409. if (IS_ERR(dvo->clk_pix)) {
  410. DRM_ERROR("Cannot get dvo_pix clock\n");
  411. return PTR_ERR(dvo->clk_pix);
  412. }
  413. dvo->clk = devm_clk_get(dev, "dvo");
  414. if (IS_ERR(dvo->clk)) {
  415. DRM_ERROR("Cannot get dvo clock\n");
  416. return PTR_ERR(dvo->clk);
  417. }
  418. dvo->clk_main_parent = devm_clk_get(dev, "main_parent");
  419. if (IS_ERR(dvo->clk_main_parent)) {
  420. DRM_DEBUG_DRIVER("Cannot get main_parent clock\n");
  421. dvo->clk_main_parent = NULL;
  422. }
  423. dvo->clk_aux_parent = devm_clk_get(dev, "aux_parent");
  424. if (IS_ERR(dvo->clk_aux_parent)) {
  425. DRM_DEBUG_DRIVER("Cannot get aux_parent clock\n");
  426. dvo->clk_aux_parent = NULL;
  427. }
  428. dvo->panel_node = of_parse_phandle(np, "sti,panel", 0);
  429. if (!dvo->panel_node)
  430. DRM_ERROR("No panel associated to the dvo output\n");
  431. platform_set_drvdata(pdev, dvo);
  432. return component_add(&pdev->dev, &sti_dvo_ops);
  433. }
  434. static int sti_dvo_remove(struct platform_device *pdev)
  435. {
  436. component_del(&pdev->dev, &sti_dvo_ops);
  437. return 0;
  438. }
  439. static struct of_device_id dvo_of_match[] = {
  440. { .compatible = "st,stih407-dvo", },
  441. { /* end node */ }
  442. };
  443. MODULE_DEVICE_TABLE(of, dvo_of_match);
  444. struct platform_driver sti_dvo_driver = {
  445. .driver = {
  446. .name = "sti-dvo",
  447. .owner = THIS_MODULE,
  448. .of_match_table = dvo_of_match,
  449. },
  450. .probe = sti_dvo_probe,
  451. .remove = sti_dvo_remove,
  452. };
  453. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  454. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  455. MODULE_LICENSE("GPL");