dpaux.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * Copyright (C) 2013 NVIDIA Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/gpio.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/reset.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/workqueue.h>
  18. #include <drm/drm_dp_helper.h>
  19. #include <drm/drm_panel.h>
  20. #include "dpaux.h"
  21. #include "drm.h"
  22. static DEFINE_MUTEX(dpaux_lock);
  23. static LIST_HEAD(dpaux_list);
  24. struct tegra_dpaux {
  25. struct drm_dp_aux aux;
  26. struct device *dev;
  27. void __iomem *regs;
  28. int irq;
  29. struct tegra_output *output;
  30. struct reset_control *rst;
  31. struct clk *clk_parent;
  32. struct clk *clk;
  33. struct regulator *vdd;
  34. struct completion complete;
  35. struct work_struct work;
  36. struct list_head list;
  37. };
  38. static inline struct tegra_dpaux *to_dpaux(struct drm_dp_aux *aux)
  39. {
  40. return container_of(aux, struct tegra_dpaux, aux);
  41. }
  42. static inline struct tegra_dpaux *work_to_dpaux(struct work_struct *work)
  43. {
  44. return container_of(work, struct tegra_dpaux, work);
  45. }
  46. static inline u32 tegra_dpaux_readl(struct tegra_dpaux *dpaux,
  47. unsigned long offset)
  48. {
  49. return readl(dpaux->regs + (offset << 2));
  50. }
  51. static inline void tegra_dpaux_writel(struct tegra_dpaux *dpaux,
  52. u32 value, unsigned long offset)
  53. {
  54. writel(value, dpaux->regs + (offset << 2));
  55. }
  56. static void tegra_dpaux_write_fifo(struct tegra_dpaux *dpaux, const u8 *buffer,
  57. size_t size)
  58. {
  59. size_t i, j;
  60. for (i = 0; i < DIV_ROUND_UP(size, 4); i++) {
  61. size_t num = min_t(size_t, size - i * 4, 4);
  62. u32 value = 0;
  63. for (j = 0; j < num; j++)
  64. value |= buffer[i * 4 + j] << (j * 8);
  65. tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXDATA_WRITE(i));
  66. }
  67. }
  68. static void tegra_dpaux_read_fifo(struct tegra_dpaux *dpaux, u8 *buffer,
  69. size_t size)
  70. {
  71. size_t i, j;
  72. for (i = 0; i < DIV_ROUND_UP(size, 4); i++) {
  73. size_t num = min_t(size_t, size - i * 4, 4);
  74. u32 value;
  75. value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXDATA_READ(i));
  76. for (j = 0; j < num; j++)
  77. buffer[i * 4 + j] = value >> (j * 8);
  78. }
  79. }
  80. static ssize_t tegra_dpaux_transfer(struct drm_dp_aux *aux,
  81. struct drm_dp_aux_msg *msg)
  82. {
  83. unsigned long timeout = msecs_to_jiffies(250);
  84. struct tegra_dpaux *dpaux = to_dpaux(aux);
  85. unsigned long status;
  86. ssize_t ret = 0;
  87. u32 value;
  88. /* Tegra has 4x4 byte DP AUX transmit and receive FIFOs. */
  89. if (msg->size > 16)
  90. return -EINVAL;
  91. /*
  92. * Allow zero-sized messages only for I2C, in which case they specify
  93. * address-only transactions.
  94. */
  95. if (msg->size < 1) {
  96. switch (msg->request & ~DP_AUX_I2C_MOT) {
  97. case DP_AUX_I2C_WRITE_STATUS_UPDATE:
  98. case DP_AUX_I2C_WRITE:
  99. case DP_AUX_I2C_READ:
  100. value = DPAUX_DP_AUXCTL_CMD_ADDRESS_ONLY;
  101. break;
  102. default:
  103. return -EINVAL;
  104. }
  105. } else {
  106. /* For non-zero-sized messages, set the CMDLEN field. */
  107. value = DPAUX_DP_AUXCTL_CMDLEN(msg->size - 1);
  108. }
  109. switch (msg->request & ~DP_AUX_I2C_MOT) {
  110. case DP_AUX_I2C_WRITE:
  111. if (msg->request & DP_AUX_I2C_MOT)
  112. value |= DPAUX_DP_AUXCTL_CMD_MOT_WR;
  113. else
  114. value |= DPAUX_DP_AUXCTL_CMD_I2C_WR;
  115. break;
  116. case DP_AUX_I2C_READ:
  117. if (msg->request & DP_AUX_I2C_MOT)
  118. value |= DPAUX_DP_AUXCTL_CMD_MOT_RD;
  119. else
  120. value |= DPAUX_DP_AUXCTL_CMD_I2C_RD;
  121. break;
  122. case DP_AUX_I2C_WRITE_STATUS_UPDATE:
  123. if (msg->request & DP_AUX_I2C_MOT)
  124. value |= DPAUX_DP_AUXCTL_CMD_MOT_RQ;
  125. else
  126. value |= DPAUX_DP_AUXCTL_CMD_I2C_RQ;
  127. break;
  128. case DP_AUX_NATIVE_WRITE:
  129. value |= DPAUX_DP_AUXCTL_CMD_AUX_WR;
  130. break;
  131. case DP_AUX_NATIVE_READ:
  132. value |= DPAUX_DP_AUXCTL_CMD_AUX_RD;
  133. break;
  134. default:
  135. return -EINVAL;
  136. }
  137. tegra_dpaux_writel(dpaux, msg->address, DPAUX_DP_AUXADDR);
  138. tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXCTL);
  139. if ((msg->request & DP_AUX_I2C_READ) == 0) {
  140. tegra_dpaux_write_fifo(dpaux, msg->buffer, msg->size);
  141. ret = msg->size;
  142. }
  143. /* start transaction */
  144. value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXCTL);
  145. value |= DPAUX_DP_AUXCTL_TRANSACTREQ;
  146. tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXCTL);
  147. status = wait_for_completion_timeout(&dpaux->complete, timeout);
  148. if (!status)
  149. return -ETIMEDOUT;
  150. /* read status and clear errors */
  151. value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXSTAT);
  152. tegra_dpaux_writel(dpaux, 0xf00, DPAUX_DP_AUXSTAT);
  153. if (value & DPAUX_DP_AUXSTAT_TIMEOUT_ERROR)
  154. return -ETIMEDOUT;
  155. if ((value & DPAUX_DP_AUXSTAT_RX_ERROR) ||
  156. (value & DPAUX_DP_AUXSTAT_SINKSTAT_ERROR) ||
  157. (value & DPAUX_DP_AUXSTAT_NO_STOP_ERROR))
  158. return -EIO;
  159. switch ((value & DPAUX_DP_AUXSTAT_REPLY_TYPE_MASK) >> 16) {
  160. case 0x00:
  161. msg->reply = DP_AUX_NATIVE_REPLY_ACK;
  162. break;
  163. case 0x01:
  164. msg->reply = DP_AUX_NATIVE_REPLY_NACK;
  165. break;
  166. case 0x02:
  167. msg->reply = DP_AUX_NATIVE_REPLY_DEFER;
  168. break;
  169. case 0x04:
  170. msg->reply = DP_AUX_I2C_REPLY_NACK;
  171. break;
  172. case 0x08:
  173. msg->reply = DP_AUX_I2C_REPLY_DEFER;
  174. break;
  175. }
  176. if ((msg->size > 0) && (msg->reply == DP_AUX_NATIVE_REPLY_ACK)) {
  177. if (msg->request & DP_AUX_I2C_READ) {
  178. size_t count = value & DPAUX_DP_AUXSTAT_REPLY_MASK;
  179. if (WARN_ON(count != msg->size))
  180. count = min_t(size_t, count, msg->size);
  181. tegra_dpaux_read_fifo(dpaux, msg->buffer, count);
  182. ret = count;
  183. }
  184. }
  185. return ret;
  186. }
  187. static void tegra_dpaux_hotplug(struct work_struct *work)
  188. {
  189. struct tegra_dpaux *dpaux = work_to_dpaux(work);
  190. if (dpaux->output)
  191. drm_helper_hpd_irq_event(dpaux->output->connector.dev);
  192. }
  193. static irqreturn_t tegra_dpaux_irq(int irq, void *data)
  194. {
  195. struct tegra_dpaux *dpaux = data;
  196. irqreturn_t ret = IRQ_HANDLED;
  197. u32 value;
  198. /* clear interrupts */
  199. value = tegra_dpaux_readl(dpaux, DPAUX_INTR_AUX);
  200. tegra_dpaux_writel(dpaux, value, DPAUX_INTR_AUX);
  201. if (value & (DPAUX_INTR_PLUG_EVENT | DPAUX_INTR_UNPLUG_EVENT))
  202. schedule_work(&dpaux->work);
  203. if (value & DPAUX_INTR_IRQ_EVENT) {
  204. /* TODO: handle this */
  205. }
  206. if (value & DPAUX_INTR_AUX_DONE)
  207. complete(&dpaux->complete);
  208. return ret;
  209. }
  210. static int tegra_dpaux_probe(struct platform_device *pdev)
  211. {
  212. struct tegra_dpaux *dpaux;
  213. struct resource *regs;
  214. u32 value;
  215. int err;
  216. dpaux = devm_kzalloc(&pdev->dev, sizeof(*dpaux), GFP_KERNEL);
  217. if (!dpaux)
  218. return -ENOMEM;
  219. INIT_WORK(&dpaux->work, tegra_dpaux_hotplug);
  220. init_completion(&dpaux->complete);
  221. INIT_LIST_HEAD(&dpaux->list);
  222. dpaux->dev = &pdev->dev;
  223. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  224. dpaux->regs = devm_ioremap_resource(&pdev->dev, regs);
  225. if (IS_ERR(dpaux->regs))
  226. return PTR_ERR(dpaux->regs);
  227. dpaux->irq = platform_get_irq(pdev, 0);
  228. if (dpaux->irq < 0) {
  229. dev_err(&pdev->dev, "failed to get IRQ\n");
  230. return -ENXIO;
  231. }
  232. dpaux->rst = devm_reset_control_get(&pdev->dev, "dpaux");
  233. if (IS_ERR(dpaux->rst)) {
  234. dev_err(&pdev->dev, "failed to get reset control: %ld\n",
  235. PTR_ERR(dpaux->rst));
  236. return PTR_ERR(dpaux->rst);
  237. }
  238. dpaux->clk = devm_clk_get(&pdev->dev, NULL);
  239. if (IS_ERR(dpaux->clk)) {
  240. dev_err(&pdev->dev, "failed to get module clock: %ld\n",
  241. PTR_ERR(dpaux->clk));
  242. return PTR_ERR(dpaux->clk);
  243. }
  244. err = clk_prepare_enable(dpaux->clk);
  245. if (err < 0) {
  246. dev_err(&pdev->dev, "failed to enable module clock: %d\n",
  247. err);
  248. return err;
  249. }
  250. reset_control_deassert(dpaux->rst);
  251. dpaux->clk_parent = devm_clk_get(&pdev->dev, "parent");
  252. if (IS_ERR(dpaux->clk_parent)) {
  253. dev_err(&pdev->dev, "failed to get parent clock: %ld\n",
  254. PTR_ERR(dpaux->clk_parent));
  255. return PTR_ERR(dpaux->clk_parent);
  256. }
  257. err = clk_prepare_enable(dpaux->clk_parent);
  258. if (err < 0) {
  259. dev_err(&pdev->dev, "failed to enable parent clock: %d\n",
  260. err);
  261. return err;
  262. }
  263. err = clk_set_rate(dpaux->clk_parent, 270000000);
  264. if (err < 0) {
  265. dev_err(&pdev->dev, "failed to set clock to 270 MHz: %d\n",
  266. err);
  267. return err;
  268. }
  269. dpaux->vdd = devm_regulator_get(&pdev->dev, "vdd");
  270. if (IS_ERR(dpaux->vdd)) {
  271. dev_err(&pdev->dev, "failed to get VDD supply: %ld\n",
  272. PTR_ERR(dpaux->vdd));
  273. return PTR_ERR(dpaux->vdd);
  274. }
  275. err = devm_request_irq(dpaux->dev, dpaux->irq, tegra_dpaux_irq, 0,
  276. dev_name(dpaux->dev), dpaux);
  277. if (err < 0) {
  278. dev_err(dpaux->dev, "failed to request IRQ#%u: %d\n",
  279. dpaux->irq, err);
  280. return err;
  281. }
  282. disable_irq(dpaux->irq);
  283. dpaux->aux.transfer = tegra_dpaux_transfer;
  284. dpaux->aux.dev = &pdev->dev;
  285. err = drm_dp_aux_register(&dpaux->aux);
  286. if (err < 0)
  287. return err;
  288. /*
  289. * Assume that by default the DPAUX/I2C pads will be used for HDMI,
  290. * so power them up and configure them in I2C mode.
  291. *
  292. * The DPAUX code paths reconfigure the pads in AUX mode, but there
  293. * is no possibility to perform the I2C mode configuration in the
  294. * HDMI path.
  295. */
  296. value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);
  297. value &= ~DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;
  298. tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
  299. value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_PADCTL);
  300. value = DPAUX_HYBRID_PADCTL_I2C_SDA_INPUT_RCV |
  301. DPAUX_HYBRID_PADCTL_I2C_SCL_INPUT_RCV |
  302. DPAUX_HYBRID_PADCTL_MODE_I2C;
  303. tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_PADCTL);
  304. /* enable and clear all interrupts */
  305. value = DPAUX_INTR_AUX_DONE | DPAUX_INTR_IRQ_EVENT |
  306. DPAUX_INTR_UNPLUG_EVENT | DPAUX_INTR_PLUG_EVENT;
  307. tegra_dpaux_writel(dpaux, value, DPAUX_INTR_EN_AUX);
  308. tegra_dpaux_writel(dpaux, value, DPAUX_INTR_AUX);
  309. mutex_lock(&dpaux_lock);
  310. list_add_tail(&dpaux->list, &dpaux_list);
  311. mutex_unlock(&dpaux_lock);
  312. platform_set_drvdata(pdev, dpaux);
  313. return 0;
  314. }
  315. static int tegra_dpaux_remove(struct platform_device *pdev)
  316. {
  317. struct tegra_dpaux *dpaux = platform_get_drvdata(pdev);
  318. u32 value;
  319. /* make sure pads are powered down when not in use */
  320. value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);
  321. value |= DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;
  322. tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
  323. drm_dp_aux_unregister(&dpaux->aux);
  324. mutex_lock(&dpaux_lock);
  325. list_del(&dpaux->list);
  326. mutex_unlock(&dpaux_lock);
  327. cancel_work_sync(&dpaux->work);
  328. clk_disable_unprepare(dpaux->clk_parent);
  329. reset_control_assert(dpaux->rst);
  330. clk_disable_unprepare(dpaux->clk);
  331. return 0;
  332. }
  333. static const struct of_device_id tegra_dpaux_of_match[] = {
  334. { .compatible = "nvidia,tegra210-dpaux", },
  335. { .compatible = "nvidia,tegra124-dpaux", },
  336. { },
  337. };
  338. MODULE_DEVICE_TABLE(of, tegra_dpaux_of_match);
  339. struct platform_driver tegra_dpaux_driver = {
  340. .driver = {
  341. .name = "tegra-dpaux",
  342. .of_match_table = tegra_dpaux_of_match,
  343. },
  344. .probe = tegra_dpaux_probe,
  345. .remove = tegra_dpaux_remove,
  346. };
  347. struct tegra_dpaux *tegra_dpaux_find_by_of_node(struct device_node *np)
  348. {
  349. struct tegra_dpaux *dpaux;
  350. mutex_lock(&dpaux_lock);
  351. list_for_each_entry(dpaux, &dpaux_list, list)
  352. if (np == dpaux->dev->of_node) {
  353. mutex_unlock(&dpaux_lock);
  354. return dpaux;
  355. }
  356. mutex_unlock(&dpaux_lock);
  357. return NULL;
  358. }
  359. int tegra_dpaux_attach(struct tegra_dpaux *dpaux, struct tegra_output *output)
  360. {
  361. unsigned long timeout;
  362. int err;
  363. output->connector.polled = DRM_CONNECTOR_POLL_HPD;
  364. dpaux->output = output;
  365. err = regulator_enable(dpaux->vdd);
  366. if (err < 0)
  367. return err;
  368. timeout = jiffies + msecs_to_jiffies(250);
  369. while (time_before(jiffies, timeout)) {
  370. enum drm_connector_status status;
  371. status = tegra_dpaux_detect(dpaux);
  372. if (status == connector_status_connected) {
  373. enable_irq(dpaux->irq);
  374. return 0;
  375. }
  376. usleep_range(1000, 2000);
  377. }
  378. return -ETIMEDOUT;
  379. }
  380. int tegra_dpaux_detach(struct tegra_dpaux *dpaux)
  381. {
  382. unsigned long timeout;
  383. int err;
  384. disable_irq(dpaux->irq);
  385. err = regulator_disable(dpaux->vdd);
  386. if (err < 0)
  387. return err;
  388. timeout = jiffies + msecs_to_jiffies(250);
  389. while (time_before(jiffies, timeout)) {
  390. enum drm_connector_status status;
  391. status = tegra_dpaux_detect(dpaux);
  392. if (status == connector_status_disconnected) {
  393. dpaux->output = NULL;
  394. return 0;
  395. }
  396. usleep_range(1000, 2000);
  397. }
  398. return -ETIMEDOUT;
  399. }
  400. enum drm_connector_status tegra_dpaux_detect(struct tegra_dpaux *dpaux)
  401. {
  402. u32 value;
  403. value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXSTAT);
  404. if (value & DPAUX_DP_AUXSTAT_HPD_STATUS)
  405. return connector_status_connected;
  406. return connector_status_disconnected;
  407. }
  408. int tegra_dpaux_enable(struct tegra_dpaux *dpaux)
  409. {
  410. u32 value;
  411. value = DPAUX_HYBRID_PADCTL_AUX_CMH(2) |
  412. DPAUX_HYBRID_PADCTL_AUX_DRVZ(4) |
  413. DPAUX_HYBRID_PADCTL_AUX_DRVI(0x18) |
  414. DPAUX_HYBRID_PADCTL_AUX_INPUT_RCV |
  415. DPAUX_HYBRID_PADCTL_MODE_AUX;
  416. tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_PADCTL);
  417. value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);
  418. value &= ~DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;
  419. tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
  420. return 0;
  421. }
  422. int tegra_dpaux_disable(struct tegra_dpaux *dpaux)
  423. {
  424. u32 value;
  425. value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);
  426. value |= DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;
  427. tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
  428. return 0;
  429. }
  430. int tegra_dpaux_prepare(struct tegra_dpaux *dpaux, u8 encoding)
  431. {
  432. int err;
  433. err = drm_dp_dpcd_writeb(&dpaux->aux, DP_MAIN_LINK_CHANNEL_CODING_SET,
  434. encoding);
  435. if (err < 0)
  436. return err;
  437. return 0;
  438. }
  439. int tegra_dpaux_train(struct tegra_dpaux *dpaux, struct drm_dp_link *link,
  440. u8 pattern)
  441. {
  442. u8 tp = pattern & DP_TRAINING_PATTERN_MASK;
  443. u8 status[DP_LINK_STATUS_SIZE], values[4];
  444. unsigned int i;
  445. int err;
  446. err = drm_dp_dpcd_writeb(&dpaux->aux, DP_TRAINING_PATTERN_SET, pattern);
  447. if (err < 0)
  448. return err;
  449. if (tp == DP_TRAINING_PATTERN_DISABLE)
  450. return 0;
  451. for (i = 0; i < link->num_lanes; i++)
  452. values[i] = DP_TRAIN_MAX_PRE_EMPHASIS_REACHED |
  453. DP_TRAIN_PRE_EMPH_LEVEL_0 |
  454. DP_TRAIN_MAX_SWING_REACHED |
  455. DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
  456. err = drm_dp_dpcd_write(&dpaux->aux, DP_TRAINING_LANE0_SET, values,
  457. link->num_lanes);
  458. if (err < 0)
  459. return err;
  460. usleep_range(500, 1000);
  461. err = drm_dp_dpcd_read_link_status(&dpaux->aux, status);
  462. if (err < 0)
  463. return err;
  464. switch (tp) {
  465. case DP_TRAINING_PATTERN_1:
  466. if (!drm_dp_clock_recovery_ok(status, link->num_lanes))
  467. return -EAGAIN;
  468. break;
  469. case DP_TRAINING_PATTERN_2:
  470. if (!drm_dp_channel_eq_ok(status, link->num_lanes))
  471. return -EAGAIN;
  472. break;
  473. default:
  474. dev_err(dpaux->dev, "unsupported training pattern %u\n", tp);
  475. return -EINVAL;
  476. }
  477. err = drm_dp_dpcd_writeb(&dpaux->aux, DP_EDP_CONFIGURATION_SET, 0);
  478. if (err < 0)
  479. return err;
  480. return 0;
  481. }