ARM: pmu: add support for interrupt-affinity property
[linux/fpc-iii.git] / drivers / gpu / drm / tegra / dpaux.c
blobd6b55e3e3716c8bcaf9e10d2f6625fbfee981797
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 unsigned long 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 unsigned long value,
67 unsigned long offset)
69 writel(value, dpaux->regs + (offset << 2));
72 static void tegra_dpaux_write_fifo(struct tegra_dpaux *dpaux, const u8 *buffer,
73 size_t size)
75 unsigned long offset = DPAUX_DP_AUXDATA_WRITE(0);
76 size_t i, j;
78 for (i = 0; i < size; i += 4) {
79 size_t num = min_t(size_t, size - i, 4);
80 unsigned long value = 0;
82 for (j = 0; j < num; j++)
83 value |= buffer[i + j] << (j * 8);
85 tegra_dpaux_writel(dpaux, value, offset++);
89 static void tegra_dpaux_read_fifo(struct tegra_dpaux *dpaux, u8 *buffer,
90 size_t size)
92 unsigned long offset = DPAUX_DP_AUXDATA_READ(0);
93 size_t i, j;
95 for (i = 0; i < size; i += 4) {
96 size_t num = min_t(size_t, size - i, 4);
97 unsigned long value;
99 value = tegra_dpaux_readl(dpaux, offset++);
101 for (j = 0; j < num; j++)
102 buffer[i + j] = value >> (j * 8);
106 static ssize_t tegra_dpaux_transfer(struct drm_dp_aux *aux,
107 struct drm_dp_aux_msg *msg)
109 unsigned long timeout = msecs_to_jiffies(250);
110 struct tegra_dpaux *dpaux = to_dpaux(aux);
111 unsigned long status;
112 ssize_t ret = 0;
113 u32 value;
115 /* Tegra has 4x4 byte DP AUX transmit and receive FIFOs. */
116 if (msg->size > 16)
117 return -EINVAL;
120 * Allow zero-sized messages only for I2C, in which case they specify
121 * address-only transactions.
123 if (msg->size < 1) {
124 switch (msg->request & ~DP_AUX_I2C_MOT) {
125 case DP_AUX_I2C_WRITE:
126 case DP_AUX_I2C_READ:
127 value = DPAUX_DP_AUXCTL_CMD_ADDRESS_ONLY;
128 break;
130 default:
131 return -EINVAL;
133 } else {
134 /* For non-zero-sized messages, set the CMDLEN field. */
135 value = DPAUX_DP_AUXCTL_CMDLEN(msg->size - 1);
138 switch (msg->request & ~DP_AUX_I2C_MOT) {
139 case DP_AUX_I2C_WRITE:
140 if (msg->request & DP_AUX_I2C_MOT)
141 value |= DPAUX_DP_AUXCTL_CMD_MOT_WR;
142 else
143 value |= DPAUX_DP_AUXCTL_CMD_I2C_WR;
145 break;
147 case DP_AUX_I2C_READ:
148 if (msg->request & DP_AUX_I2C_MOT)
149 value |= DPAUX_DP_AUXCTL_CMD_MOT_RD;
150 else
151 value |= DPAUX_DP_AUXCTL_CMD_I2C_RD;
153 break;
155 case DP_AUX_I2C_STATUS:
156 if (msg->request & DP_AUX_I2C_MOT)
157 value |= DPAUX_DP_AUXCTL_CMD_MOT_RQ;
158 else
159 value |= DPAUX_DP_AUXCTL_CMD_I2C_RQ;
161 break;
163 case DP_AUX_NATIVE_WRITE:
164 value |= DPAUX_DP_AUXCTL_CMD_AUX_WR;
165 break;
167 case DP_AUX_NATIVE_READ:
168 value |= DPAUX_DP_AUXCTL_CMD_AUX_RD;
169 break;
171 default:
172 return -EINVAL;
175 tegra_dpaux_writel(dpaux, msg->address, DPAUX_DP_AUXADDR);
176 tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXCTL);
178 if ((msg->request & DP_AUX_I2C_READ) == 0) {
179 tegra_dpaux_write_fifo(dpaux, msg->buffer, msg->size);
180 ret = msg->size;
183 /* start transaction */
184 value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXCTL);
185 value |= DPAUX_DP_AUXCTL_TRANSACTREQ;
186 tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXCTL);
188 status = wait_for_completion_timeout(&dpaux->complete, timeout);
189 if (!status)
190 return -ETIMEDOUT;
192 /* read status and clear errors */
193 value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXSTAT);
194 tegra_dpaux_writel(dpaux, 0xf00, DPAUX_DP_AUXSTAT);
196 if (value & DPAUX_DP_AUXSTAT_TIMEOUT_ERROR)
197 return -ETIMEDOUT;
199 if ((value & DPAUX_DP_AUXSTAT_RX_ERROR) ||
200 (value & DPAUX_DP_AUXSTAT_SINKSTAT_ERROR) ||
201 (value & DPAUX_DP_AUXSTAT_NO_STOP_ERROR))
202 return -EIO;
204 switch ((value & DPAUX_DP_AUXSTAT_REPLY_TYPE_MASK) >> 16) {
205 case 0x00:
206 msg->reply = DP_AUX_NATIVE_REPLY_ACK;
207 break;
209 case 0x01:
210 msg->reply = DP_AUX_NATIVE_REPLY_NACK;
211 break;
213 case 0x02:
214 msg->reply = DP_AUX_NATIVE_REPLY_DEFER;
215 break;
217 case 0x04:
218 msg->reply = DP_AUX_I2C_REPLY_NACK;
219 break;
221 case 0x08:
222 msg->reply = DP_AUX_I2C_REPLY_DEFER;
223 break;
226 if ((msg->size > 0) && (msg->reply == DP_AUX_NATIVE_REPLY_ACK)) {
227 if (msg->request & DP_AUX_I2C_READ) {
228 size_t count = value & DPAUX_DP_AUXSTAT_REPLY_MASK;
230 if (WARN_ON(count != msg->size))
231 count = min_t(size_t, count, msg->size);
233 tegra_dpaux_read_fifo(dpaux, msg->buffer, count);
234 ret = count;
238 return ret;
241 static void tegra_dpaux_hotplug(struct work_struct *work)
243 struct tegra_dpaux *dpaux = work_to_dpaux(work);
245 if (dpaux->output)
246 drm_helper_hpd_irq_event(dpaux->output->connector.dev);
249 static irqreturn_t tegra_dpaux_irq(int irq, void *data)
251 struct tegra_dpaux *dpaux = data;
252 irqreturn_t ret = IRQ_HANDLED;
253 unsigned long value;
255 /* clear interrupts */
256 value = tegra_dpaux_readl(dpaux, DPAUX_INTR_AUX);
257 tegra_dpaux_writel(dpaux, value, DPAUX_INTR_AUX);
259 if (value & (DPAUX_INTR_PLUG_EVENT | DPAUX_INTR_UNPLUG_EVENT))
260 schedule_work(&dpaux->work);
262 if (value & DPAUX_INTR_IRQ_EVENT) {
263 /* TODO: handle this */
266 if (value & DPAUX_INTR_AUX_DONE)
267 complete(&dpaux->complete);
269 return ret;
272 static int tegra_dpaux_probe(struct platform_device *pdev)
274 struct tegra_dpaux *dpaux;
275 struct resource *regs;
276 unsigned long value;
277 int err;
279 dpaux = devm_kzalloc(&pdev->dev, sizeof(*dpaux), GFP_KERNEL);
280 if (!dpaux)
281 return -ENOMEM;
283 INIT_WORK(&dpaux->work, tegra_dpaux_hotplug);
284 init_completion(&dpaux->complete);
285 INIT_LIST_HEAD(&dpaux->list);
286 dpaux->dev = &pdev->dev;
288 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
289 dpaux->regs = devm_ioremap_resource(&pdev->dev, regs);
290 if (IS_ERR(dpaux->regs))
291 return PTR_ERR(dpaux->regs);
293 dpaux->irq = platform_get_irq(pdev, 0);
294 if (dpaux->irq < 0) {
295 dev_err(&pdev->dev, "failed to get IRQ\n");
296 return -ENXIO;
299 dpaux->rst = devm_reset_control_get(&pdev->dev, "dpaux");
300 if (IS_ERR(dpaux->rst))
301 return PTR_ERR(dpaux->rst);
303 dpaux->clk = devm_clk_get(&pdev->dev, NULL);
304 if (IS_ERR(dpaux->clk))
305 return PTR_ERR(dpaux->clk);
307 err = clk_prepare_enable(dpaux->clk);
308 if (err < 0)
309 return err;
311 reset_control_deassert(dpaux->rst);
313 dpaux->clk_parent = devm_clk_get(&pdev->dev, "parent");
314 if (IS_ERR(dpaux->clk_parent))
315 return PTR_ERR(dpaux->clk_parent);
317 err = clk_prepare_enable(dpaux->clk_parent);
318 if (err < 0)
319 return err;
321 err = clk_set_rate(dpaux->clk_parent, 270000000);
322 if (err < 0) {
323 dev_err(&pdev->dev, "failed to set clock to 270 MHz: %d\n",
324 err);
325 return err;
328 dpaux->vdd = devm_regulator_get(&pdev->dev, "vdd");
329 if (IS_ERR(dpaux->vdd))
330 return PTR_ERR(dpaux->vdd);
332 err = devm_request_irq(dpaux->dev, dpaux->irq, tegra_dpaux_irq, 0,
333 dev_name(dpaux->dev), dpaux);
334 if (err < 0) {
335 dev_err(dpaux->dev, "failed to request IRQ#%u: %d\n",
336 dpaux->irq, err);
337 return err;
340 dpaux->aux.transfer = tegra_dpaux_transfer;
341 dpaux->aux.dev = &pdev->dev;
343 err = drm_dp_aux_register(&dpaux->aux);
344 if (err < 0)
345 return err;
347 /* enable and clear all interrupts */
348 value = DPAUX_INTR_AUX_DONE | DPAUX_INTR_IRQ_EVENT |
349 DPAUX_INTR_UNPLUG_EVENT | DPAUX_INTR_PLUG_EVENT;
350 tegra_dpaux_writel(dpaux, value, DPAUX_INTR_EN_AUX);
351 tegra_dpaux_writel(dpaux, value, DPAUX_INTR_AUX);
353 mutex_lock(&dpaux_lock);
354 list_add_tail(&dpaux->list, &dpaux_list);
355 mutex_unlock(&dpaux_lock);
357 platform_set_drvdata(pdev, dpaux);
359 return 0;
362 static int tegra_dpaux_remove(struct platform_device *pdev)
364 struct tegra_dpaux *dpaux = platform_get_drvdata(pdev);
366 drm_dp_aux_unregister(&dpaux->aux);
368 mutex_lock(&dpaux_lock);
369 list_del(&dpaux->list);
370 mutex_unlock(&dpaux_lock);
372 cancel_work_sync(&dpaux->work);
374 clk_disable_unprepare(dpaux->clk_parent);
375 reset_control_assert(dpaux->rst);
376 clk_disable_unprepare(dpaux->clk);
378 return 0;
381 static const struct of_device_id tegra_dpaux_of_match[] = {
382 { .compatible = "nvidia,tegra124-dpaux", },
383 { },
385 MODULE_DEVICE_TABLE(of, tegra_dpaux_of_match);
387 struct platform_driver tegra_dpaux_driver = {
388 .driver = {
389 .name = "tegra-dpaux",
390 .of_match_table = tegra_dpaux_of_match,
392 .probe = tegra_dpaux_probe,
393 .remove = tegra_dpaux_remove,
396 struct tegra_dpaux *tegra_dpaux_find_by_of_node(struct device_node *np)
398 struct tegra_dpaux *dpaux;
400 mutex_lock(&dpaux_lock);
402 list_for_each_entry(dpaux, &dpaux_list, list)
403 if (np == dpaux->dev->of_node) {
404 mutex_unlock(&dpaux_lock);
405 return dpaux;
408 mutex_unlock(&dpaux_lock);
410 return NULL;
413 int tegra_dpaux_attach(struct tegra_dpaux *dpaux, struct tegra_output *output)
415 unsigned long timeout;
416 int err;
418 output->connector.polled = DRM_CONNECTOR_POLL_HPD;
419 dpaux->output = output;
421 err = regulator_enable(dpaux->vdd);
422 if (err < 0)
423 return err;
425 timeout = jiffies + msecs_to_jiffies(250);
427 while (time_before(jiffies, timeout)) {
428 enum drm_connector_status status;
430 status = tegra_dpaux_detect(dpaux);
431 if (status == connector_status_connected)
432 return 0;
434 usleep_range(1000, 2000);
437 return -ETIMEDOUT;
440 int tegra_dpaux_detach(struct tegra_dpaux *dpaux)
442 unsigned long timeout;
443 int err;
445 err = regulator_disable(dpaux->vdd);
446 if (err < 0)
447 return err;
449 timeout = jiffies + msecs_to_jiffies(250);
451 while (time_before(jiffies, timeout)) {
452 enum drm_connector_status status;
454 status = tegra_dpaux_detect(dpaux);
455 if (status == connector_status_disconnected) {
456 dpaux->output = NULL;
457 return 0;
460 usleep_range(1000, 2000);
463 return -ETIMEDOUT;
466 enum drm_connector_status tegra_dpaux_detect(struct tegra_dpaux *dpaux)
468 unsigned long value;
470 value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXSTAT);
472 if (value & DPAUX_DP_AUXSTAT_HPD_STATUS)
473 return connector_status_connected;
475 return connector_status_disconnected;
478 int tegra_dpaux_enable(struct tegra_dpaux *dpaux)
480 unsigned long value;
482 value = DPAUX_HYBRID_PADCTL_AUX_CMH(2) |
483 DPAUX_HYBRID_PADCTL_AUX_DRVZ(4) |
484 DPAUX_HYBRID_PADCTL_AUX_DRVI(0x18) |
485 DPAUX_HYBRID_PADCTL_AUX_INPUT_RCV |
486 DPAUX_HYBRID_PADCTL_MODE_AUX;
487 tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_PADCTL);
489 value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);
490 value &= ~DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;
491 tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
493 return 0;
496 int tegra_dpaux_disable(struct tegra_dpaux *dpaux)
498 unsigned long value;
500 value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);
501 value |= DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;
502 tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
504 return 0;
507 int tegra_dpaux_prepare(struct tegra_dpaux *dpaux, u8 encoding)
509 int err;
511 err = drm_dp_dpcd_writeb(&dpaux->aux, DP_MAIN_LINK_CHANNEL_CODING_SET,
512 encoding);
513 if (err < 0)
514 return err;
516 return 0;
519 int tegra_dpaux_train(struct tegra_dpaux *dpaux, struct drm_dp_link *link,
520 u8 pattern)
522 u8 tp = pattern & DP_TRAINING_PATTERN_MASK;
523 u8 status[DP_LINK_STATUS_SIZE], values[4];
524 unsigned int i;
525 int err;
527 err = drm_dp_dpcd_writeb(&dpaux->aux, DP_TRAINING_PATTERN_SET, pattern);
528 if (err < 0)
529 return err;
531 if (tp == DP_TRAINING_PATTERN_DISABLE)
532 return 0;
534 for (i = 0; i < link->num_lanes; i++)
535 values[i] = DP_TRAIN_MAX_PRE_EMPHASIS_REACHED |
536 DP_TRAIN_PRE_EMPH_LEVEL_0 |
537 DP_TRAIN_MAX_SWING_REACHED |
538 DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
540 err = drm_dp_dpcd_write(&dpaux->aux, DP_TRAINING_LANE0_SET, values,
541 link->num_lanes);
542 if (err < 0)
543 return err;
545 usleep_range(500, 1000);
547 err = drm_dp_dpcd_read_link_status(&dpaux->aux, status);
548 if (err < 0)
549 return err;
551 switch (tp) {
552 case DP_TRAINING_PATTERN_1:
553 if (!drm_dp_clock_recovery_ok(status, link->num_lanes))
554 return -EAGAIN;
556 break;
558 case DP_TRAINING_PATTERN_2:
559 if (!drm_dp_channel_eq_ok(status, link->num_lanes))
560 return -EAGAIN;
562 break;
564 default:
565 dev_err(dpaux->dev, "unsupported training pattern %u\n", tp);
566 return -EINVAL;
569 err = drm_dp_dpcd_writeb(&dpaux->aux, DP_EDP_CONFIGURATION_SET, 0);
570 if (err < 0)
571 return err;
573 return 0;