Linux 4.2.1
[linux/fpc-iii.git] / drivers / gpu / drm / tegra / dpaux.c
blob07b26972f487967cb1b4203488f0387c9e0736fc
1 /*
2 * Copyright (C) 2013 NVIDIA Corporation
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 */
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/gpio.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/of_gpio.h>
15 #include <linux/platform_device.h>
16 #include <linux/reset.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/workqueue.h>
20 #include <drm/drm_dp_helper.h>
21 #include <drm/drm_panel.h>
23 #include "dpaux.h"
24 #include "drm.h"
26 static DEFINE_MUTEX(dpaux_lock);
27 static LIST_HEAD(dpaux_list);
29 struct tegra_dpaux {
30 struct drm_dp_aux aux;
31 struct device *dev;
33 void __iomem *regs;
34 int irq;
36 struct tegra_output *output;
38 struct reset_control *rst;
39 struct clk *clk_parent;
40 struct clk *clk;
42 struct regulator *vdd;
44 struct completion complete;
45 struct work_struct work;
46 struct list_head list;
49 static inline struct tegra_dpaux *to_dpaux(struct drm_dp_aux *aux)
51 return container_of(aux, struct tegra_dpaux, aux);
54 static inline struct tegra_dpaux *work_to_dpaux(struct work_struct *work)
56 return container_of(work, struct tegra_dpaux, work);
59 static inline u32 tegra_dpaux_readl(struct tegra_dpaux *dpaux,
60 unsigned long offset)
62 return readl(dpaux->regs + (offset << 2));
65 static inline void tegra_dpaux_writel(struct tegra_dpaux *dpaux,
66 u32 value, unsigned long offset)
68 writel(value, dpaux->regs + (offset << 2));
71 static void tegra_dpaux_write_fifo(struct tegra_dpaux *dpaux, const u8 *buffer,
72 size_t size)
74 size_t i, j;
76 for (i = 0; i < DIV_ROUND_UP(size, 4); i++) {
77 size_t num = min_t(size_t, size - i * 4, 4);
78 u32 value = 0;
80 for (j = 0; j < num; j++)
81 value |= buffer[i * 4 + j] << (j * 8);
83 tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXDATA_WRITE(i));
87 static void tegra_dpaux_read_fifo(struct tegra_dpaux *dpaux, u8 *buffer,
88 size_t size)
90 size_t i, j;
92 for (i = 0; i < DIV_ROUND_UP(size, 4); i++) {
93 size_t num = min_t(size_t, size - i * 4, 4);
94 u32 value;
96 value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXDATA_READ(i));
98 for (j = 0; j < num; j++)
99 buffer[i * 4 + j] = value >> (j * 8);
103 static ssize_t tegra_dpaux_transfer(struct drm_dp_aux *aux,
104 struct drm_dp_aux_msg *msg)
106 unsigned long timeout = msecs_to_jiffies(250);
107 struct tegra_dpaux *dpaux = to_dpaux(aux);
108 unsigned long status;
109 ssize_t ret = 0;
110 u32 value;
112 /* Tegra has 4x4 byte DP AUX transmit and receive FIFOs. */
113 if (msg->size > 16)
114 return -EINVAL;
117 * Allow zero-sized messages only for I2C, in which case they specify
118 * address-only transactions.
120 if (msg->size < 1) {
121 switch (msg->request & ~DP_AUX_I2C_MOT) {
122 case DP_AUX_I2C_WRITE:
123 case DP_AUX_I2C_READ:
124 value = DPAUX_DP_AUXCTL_CMD_ADDRESS_ONLY;
125 break;
127 default:
128 return -EINVAL;
130 } else {
131 /* For non-zero-sized messages, set the CMDLEN field. */
132 value = DPAUX_DP_AUXCTL_CMDLEN(msg->size - 1);
135 switch (msg->request & ~DP_AUX_I2C_MOT) {
136 case DP_AUX_I2C_WRITE:
137 if (msg->request & DP_AUX_I2C_MOT)
138 value |= DPAUX_DP_AUXCTL_CMD_MOT_WR;
139 else
140 value |= DPAUX_DP_AUXCTL_CMD_I2C_WR;
142 break;
144 case DP_AUX_I2C_READ:
145 if (msg->request & DP_AUX_I2C_MOT)
146 value |= DPAUX_DP_AUXCTL_CMD_MOT_RD;
147 else
148 value |= DPAUX_DP_AUXCTL_CMD_I2C_RD;
150 break;
152 case DP_AUX_I2C_STATUS:
153 if (msg->request & DP_AUX_I2C_MOT)
154 value |= DPAUX_DP_AUXCTL_CMD_MOT_RQ;
155 else
156 value |= DPAUX_DP_AUXCTL_CMD_I2C_RQ;
158 break;
160 case DP_AUX_NATIVE_WRITE:
161 value |= DPAUX_DP_AUXCTL_CMD_AUX_WR;
162 break;
164 case DP_AUX_NATIVE_READ:
165 value |= DPAUX_DP_AUXCTL_CMD_AUX_RD;
166 break;
168 default:
169 return -EINVAL;
172 tegra_dpaux_writel(dpaux, msg->address, DPAUX_DP_AUXADDR);
173 tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXCTL);
175 if ((msg->request & DP_AUX_I2C_READ) == 0) {
176 tegra_dpaux_write_fifo(dpaux, msg->buffer, msg->size);
177 ret = msg->size;
180 /* start transaction */
181 value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXCTL);
182 value |= DPAUX_DP_AUXCTL_TRANSACTREQ;
183 tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXCTL);
185 status = wait_for_completion_timeout(&dpaux->complete, timeout);
186 if (!status)
187 return -ETIMEDOUT;
189 /* read status and clear errors */
190 value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXSTAT);
191 tegra_dpaux_writel(dpaux, 0xf00, DPAUX_DP_AUXSTAT);
193 if (value & DPAUX_DP_AUXSTAT_TIMEOUT_ERROR)
194 return -ETIMEDOUT;
196 if ((value & DPAUX_DP_AUXSTAT_RX_ERROR) ||
197 (value & DPAUX_DP_AUXSTAT_SINKSTAT_ERROR) ||
198 (value & DPAUX_DP_AUXSTAT_NO_STOP_ERROR))
199 return -EIO;
201 switch ((value & DPAUX_DP_AUXSTAT_REPLY_TYPE_MASK) >> 16) {
202 case 0x00:
203 msg->reply = DP_AUX_NATIVE_REPLY_ACK;
204 break;
206 case 0x01:
207 msg->reply = DP_AUX_NATIVE_REPLY_NACK;
208 break;
210 case 0x02:
211 msg->reply = DP_AUX_NATIVE_REPLY_DEFER;
212 break;
214 case 0x04:
215 msg->reply = DP_AUX_I2C_REPLY_NACK;
216 break;
218 case 0x08:
219 msg->reply = DP_AUX_I2C_REPLY_DEFER;
220 break;
223 if ((msg->size > 0) && (msg->reply == DP_AUX_NATIVE_REPLY_ACK)) {
224 if (msg->request & DP_AUX_I2C_READ) {
225 size_t count = value & DPAUX_DP_AUXSTAT_REPLY_MASK;
227 if (WARN_ON(count != msg->size))
228 count = min_t(size_t, count, msg->size);
230 tegra_dpaux_read_fifo(dpaux, msg->buffer, count);
231 ret = count;
235 return ret;
238 static void tegra_dpaux_hotplug(struct work_struct *work)
240 struct tegra_dpaux *dpaux = work_to_dpaux(work);
242 if (dpaux->output)
243 drm_helper_hpd_irq_event(dpaux->output->connector.dev);
246 static irqreturn_t tegra_dpaux_irq(int irq, void *data)
248 struct tegra_dpaux *dpaux = data;
249 irqreturn_t ret = IRQ_HANDLED;
250 u32 value;
252 /* clear interrupts */
253 value = tegra_dpaux_readl(dpaux, DPAUX_INTR_AUX);
254 tegra_dpaux_writel(dpaux, value, DPAUX_INTR_AUX);
256 if (value & (DPAUX_INTR_PLUG_EVENT | DPAUX_INTR_UNPLUG_EVENT))
257 schedule_work(&dpaux->work);
259 if (value & DPAUX_INTR_IRQ_EVENT) {
260 /* TODO: handle this */
263 if (value & DPAUX_INTR_AUX_DONE)
264 complete(&dpaux->complete);
266 return ret;
269 static int tegra_dpaux_probe(struct platform_device *pdev)
271 struct tegra_dpaux *dpaux;
272 struct resource *regs;
273 u32 value;
274 int err;
276 dpaux = devm_kzalloc(&pdev->dev, sizeof(*dpaux), GFP_KERNEL);
277 if (!dpaux)
278 return -ENOMEM;
280 INIT_WORK(&dpaux->work, tegra_dpaux_hotplug);
281 init_completion(&dpaux->complete);
282 INIT_LIST_HEAD(&dpaux->list);
283 dpaux->dev = &pdev->dev;
285 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
286 dpaux->regs = devm_ioremap_resource(&pdev->dev, regs);
287 if (IS_ERR(dpaux->regs))
288 return PTR_ERR(dpaux->regs);
290 dpaux->irq = platform_get_irq(pdev, 0);
291 if (dpaux->irq < 0) {
292 dev_err(&pdev->dev, "failed to get IRQ\n");
293 return -ENXIO;
296 dpaux->rst = devm_reset_control_get(&pdev->dev, "dpaux");
297 if (IS_ERR(dpaux->rst))
298 return PTR_ERR(dpaux->rst);
300 dpaux->clk = devm_clk_get(&pdev->dev, NULL);
301 if (IS_ERR(dpaux->clk))
302 return PTR_ERR(dpaux->clk);
304 err = clk_prepare_enable(dpaux->clk);
305 if (err < 0)
306 return err;
308 reset_control_deassert(dpaux->rst);
310 dpaux->clk_parent = devm_clk_get(&pdev->dev, "parent");
311 if (IS_ERR(dpaux->clk_parent))
312 return PTR_ERR(dpaux->clk_parent);
314 err = clk_prepare_enable(dpaux->clk_parent);
315 if (err < 0)
316 return err;
318 err = clk_set_rate(dpaux->clk_parent, 270000000);
319 if (err < 0) {
320 dev_err(&pdev->dev, "failed to set clock to 270 MHz: %d\n",
321 err);
322 return err;
325 dpaux->vdd = devm_regulator_get(&pdev->dev, "vdd");
326 if (IS_ERR(dpaux->vdd))
327 return PTR_ERR(dpaux->vdd);
329 err = devm_request_irq(dpaux->dev, dpaux->irq, tegra_dpaux_irq, 0,
330 dev_name(dpaux->dev), dpaux);
331 if (err < 0) {
332 dev_err(dpaux->dev, "failed to request IRQ#%u: %d\n",
333 dpaux->irq, err);
334 return err;
337 dpaux->aux.transfer = tegra_dpaux_transfer;
338 dpaux->aux.dev = &pdev->dev;
340 err = drm_dp_aux_register(&dpaux->aux);
341 if (err < 0)
342 return err;
344 /* enable and clear all interrupts */
345 value = DPAUX_INTR_AUX_DONE | DPAUX_INTR_IRQ_EVENT |
346 DPAUX_INTR_UNPLUG_EVENT | DPAUX_INTR_PLUG_EVENT;
347 tegra_dpaux_writel(dpaux, value, DPAUX_INTR_EN_AUX);
348 tegra_dpaux_writel(dpaux, value, DPAUX_INTR_AUX);
350 mutex_lock(&dpaux_lock);
351 list_add_tail(&dpaux->list, &dpaux_list);
352 mutex_unlock(&dpaux_lock);
354 platform_set_drvdata(pdev, dpaux);
356 return 0;
359 static int tegra_dpaux_remove(struct platform_device *pdev)
361 struct tegra_dpaux *dpaux = platform_get_drvdata(pdev);
363 drm_dp_aux_unregister(&dpaux->aux);
365 mutex_lock(&dpaux_lock);
366 list_del(&dpaux->list);
367 mutex_unlock(&dpaux_lock);
369 cancel_work_sync(&dpaux->work);
371 clk_disable_unprepare(dpaux->clk_parent);
372 reset_control_assert(dpaux->rst);
373 clk_disable_unprepare(dpaux->clk);
375 return 0;
378 static const struct of_device_id tegra_dpaux_of_match[] = {
379 { .compatible = "nvidia,tegra124-dpaux", },
380 { },
382 MODULE_DEVICE_TABLE(of, tegra_dpaux_of_match);
384 struct platform_driver tegra_dpaux_driver = {
385 .driver = {
386 .name = "tegra-dpaux",
387 .of_match_table = tegra_dpaux_of_match,
389 .probe = tegra_dpaux_probe,
390 .remove = tegra_dpaux_remove,
393 struct tegra_dpaux *tegra_dpaux_find_by_of_node(struct device_node *np)
395 struct tegra_dpaux *dpaux;
397 mutex_lock(&dpaux_lock);
399 list_for_each_entry(dpaux, &dpaux_list, list)
400 if (np == dpaux->dev->of_node) {
401 mutex_unlock(&dpaux_lock);
402 return dpaux;
405 mutex_unlock(&dpaux_lock);
407 return NULL;
410 int tegra_dpaux_attach(struct tegra_dpaux *dpaux, struct tegra_output *output)
412 unsigned long timeout;
413 int err;
415 output->connector.polled = DRM_CONNECTOR_POLL_HPD;
416 dpaux->output = output;
418 err = regulator_enable(dpaux->vdd);
419 if (err < 0)
420 return err;
422 timeout = jiffies + msecs_to_jiffies(250);
424 while (time_before(jiffies, timeout)) {
425 enum drm_connector_status status;
427 status = tegra_dpaux_detect(dpaux);
428 if (status == connector_status_connected)
429 return 0;
431 usleep_range(1000, 2000);
434 return -ETIMEDOUT;
437 int tegra_dpaux_detach(struct tegra_dpaux *dpaux)
439 unsigned long timeout;
440 int err;
442 err = regulator_disable(dpaux->vdd);
443 if (err < 0)
444 return err;
446 timeout = jiffies + msecs_to_jiffies(250);
448 while (time_before(jiffies, timeout)) {
449 enum drm_connector_status status;
451 status = tegra_dpaux_detect(dpaux);
452 if (status == connector_status_disconnected) {
453 dpaux->output = NULL;
454 return 0;
457 usleep_range(1000, 2000);
460 return -ETIMEDOUT;
463 enum drm_connector_status tegra_dpaux_detect(struct tegra_dpaux *dpaux)
465 u32 value;
467 value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXSTAT);
469 if (value & DPAUX_DP_AUXSTAT_HPD_STATUS)
470 return connector_status_connected;
472 return connector_status_disconnected;
475 int tegra_dpaux_enable(struct tegra_dpaux *dpaux)
477 u32 value;
479 value = DPAUX_HYBRID_PADCTL_AUX_CMH(2) |
480 DPAUX_HYBRID_PADCTL_AUX_DRVZ(4) |
481 DPAUX_HYBRID_PADCTL_AUX_DRVI(0x18) |
482 DPAUX_HYBRID_PADCTL_AUX_INPUT_RCV |
483 DPAUX_HYBRID_PADCTL_MODE_AUX;
484 tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_PADCTL);
486 value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);
487 value &= ~DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;
488 tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
490 return 0;
493 int tegra_dpaux_disable(struct tegra_dpaux *dpaux)
495 u32 value;
497 value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);
498 value |= DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;
499 tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
501 return 0;
504 int tegra_dpaux_prepare(struct tegra_dpaux *dpaux, u8 encoding)
506 int err;
508 err = drm_dp_dpcd_writeb(&dpaux->aux, DP_MAIN_LINK_CHANNEL_CODING_SET,
509 encoding);
510 if (err < 0)
511 return err;
513 return 0;
516 int tegra_dpaux_train(struct tegra_dpaux *dpaux, struct drm_dp_link *link,
517 u8 pattern)
519 u8 tp = pattern & DP_TRAINING_PATTERN_MASK;
520 u8 status[DP_LINK_STATUS_SIZE], values[4];
521 unsigned int i;
522 int err;
524 err = drm_dp_dpcd_writeb(&dpaux->aux, DP_TRAINING_PATTERN_SET, pattern);
525 if (err < 0)
526 return err;
528 if (tp == DP_TRAINING_PATTERN_DISABLE)
529 return 0;
531 for (i = 0; i < link->num_lanes; i++)
532 values[i] = DP_TRAIN_MAX_PRE_EMPHASIS_REACHED |
533 DP_TRAIN_PRE_EMPH_LEVEL_0 |
534 DP_TRAIN_MAX_SWING_REACHED |
535 DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
537 err = drm_dp_dpcd_write(&dpaux->aux, DP_TRAINING_LANE0_SET, values,
538 link->num_lanes);
539 if (err < 0)
540 return err;
542 usleep_range(500, 1000);
544 err = drm_dp_dpcd_read_link_status(&dpaux->aux, status);
545 if (err < 0)
546 return err;
548 switch (tp) {
549 case DP_TRAINING_PATTERN_1:
550 if (!drm_dp_clock_recovery_ok(status, link->num_lanes))
551 return -EAGAIN;
553 break;
555 case DP_TRAINING_PATTERN_2:
556 if (!drm_dp_channel_eq_ok(status, link->num_lanes))
557 return -EAGAIN;
559 break;
561 default:
562 dev_err(dpaux->dev, "unsupported training pattern %u\n", tp);
563 return -EINVAL;
566 err = drm_dp_dpcd_writeb(&dpaux->aux, DP_EDP_CONFIGURATION_SET, 0);
567 if (err < 0)
568 return err;
570 return 0;