1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
6 #include <linux/delay.h>
7 #include <linux/phy/phy.h>
8 #include <drm/drm_print.h>
19 DP_AUX_ERR_NACK_DEFER,
23 struct dp_aux_private {
25 struct dp_catalog *catalog;
30 struct completion comp;
32 enum msm_dp_aux_err aux_error_num;
44 struct drm_dp_aux dp_aux;
47 #define MAX_AUX_RETRIES 5
49 static ssize_t dp_aux_write(struct dp_aux_private *aux,
50 struct drm_dp_aux_msg *msg)
55 u8 *msgdata = msg->buffer;
56 int const AUX_CMD_FIFO_LEN = 128;
65 * cmd fifo only has depth of 144 bytes
66 * limit buf length to 128 bytes here
68 if (len > AUX_CMD_FIFO_LEN - 4) {
69 DRM_ERROR("buf size greater than allowed size of 128 bytes\n");
73 /* Pack cmd and write to HW */
74 data[0] = (msg->address >> 16) & 0xf; /* addr[19:16] */
76 data[0] |= BIT(4); /* R/W */
78 data[1] = msg->address >> 8; /* addr[15:8] */
79 data[2] = msg->address; /* addr[7:0] */
80 data[3] = msg->size - 1; /* len[7:0] */
82 for (i = 0; i < len + 4; i++) {
83 reg = (i < 4) ? data[i] : msgdata[i - 4];
84 reg <<= DP_AUX_DATA_OFFSET;
85 reg &= DP_AUX_DATA_MASK;
86 reg |= DP_AUX_DATA_WRITE;
87 /* index = 0, write */
89 reg |= DP_AUX_DATA_INDEX_WRITE;
90 aux->catalog->aux_data = reg;
91 dp_catalog_aux_write_data(aux->catalog);
94 dp_catalog_aux_clear_trans(aux->catalog, false);
95 dp_catalog_aux_clear_hw_interrupts(aux->catalog);
97 reg = 0; /* Transaction number == 1 */
98 if (!aux->native) { /* i2c */
99 reg |= DP_AUX_TRANS_CTRL_I2C;
101 if (aux->no_send_addr)
102 reg |= DP_AUX_TRANS_CTRL_NO_SEND_ADDR;
104 if (aux->no_send_stop)
105 reg |= DP_AUX_TRANS_CTRL_NO_SEND_STOP;
108 reg |= DP_AUX_TRANS_CTRL_GO;
109 aux->catalog->aux_data = reg;
110 dp_catalog_aux_write_trans(aux->catalog);
115 static ssize_t dp_aux_cmd_fifo_tx(struct dp_aux_private *aux,
116 struct drm_dp_aux_msg *msg)
119 unsigned long time_left;
121 reinit_completion(&aux->comp);
123 ret = dp_aux_write(aux, msg);
127 time_left = wait_for_completion_timeout(&aux->comp,
128 msecs_to_jiffies(250));
135 static ssize_t dp_aux_cmd_fifo_rx(struct dp_aux_private *aux,
136 struct drm_dp_aux_msg *msg)
143 dp_catalog_aux_clear_trans(aux->catalog, true);
145 data = DP_AUX_DATA_INDEX_WRITE; /* INDEX_WRITE */
146 data |= DP_AUX_DATA_READ; /* read */
148 aux->catalog->aux_data = data;
149 dp_catalog_aux_write_data(aux->catalog);
153 /* discard first byte */
154 data = dp_catalog_aux_read_data(aux->catalog);
156 for (i = 0; i < len; i++) {
157 data = dp_catalog_aux_read_data(aux->catalog);
158 *dp++ = (u8)((data >> DP_AUX_DATA_OFFSET) & 0xff);
160 actual_i = (data >> DP_AUX_DATA_INDEX_OFFSET) & 0xFF;
168 static void dp_aux_update_offset_and_segment(struct dp_aux_private *aux,
169 struct drm_dp_aux_msg *input_msg)
171 u32 edid_address = 0x50;
172 u32 segment_address = 0x30;
173 bool i2c_read = input_msg->request &
174 (DP_AUX_I2C_READ & DP_AUX_NATIVE_READ);
177 if (aux->native || i2c_read || ((input_msg->address != edid_address) &&
178 (input_msg->address != segment_address)))
182 data = input_msg->buffer;
183 if (input_msg->address == segment_address)
184 aux->segment = *data;
190 * dp_aux_transfer_helper() - helper function for EDID read transactions
192 * @aux: DP AUX private structure
193 * @input_msg: input message from DRM upstream APIs
194 * @send_seg: send the segment to sink
198 * This helper function is used to fix EDID reads for non-compliant
199 * sinks that do not handle the i2c middle-of-transaction flag correctly.
201 static void dp_aux_transfer_helper(struct dp_aux_private *aux,
202 struct drm_dp_aux_msg *input_msg,
205 struct drm_dp_aux_msg helper_msg;
206 u32 message_size = 0x10;
207 u32 segment_address = 0x30;
208 u32 const edid_block_length = 0x80;
209 bool i2c_mot = input_msg->request & DP_AUX_I2C_MOT;
210 bool i2c_read = input_msg->request &
211 (DP_AUX_I2C_READ & DP_AUX_NATIVE_READ);
213 if (!i2c_mot || !i2c_read || (input_msg->size == 0))
217 * Sending the segment value and EDID offset will be performed
218 * from the DRM upstream EDID driver for each block. Avoid
219 * duplicate AUX transactions related to this while reading the
220 * first 16 bytes of each block.
222 if (!(aux->offset % edid_block_length) || !send_seg)
226 aux->cmd_busy = true;
227 aux->no_send_addr = true;
228 aux->no_send_stop = true;
231 * Send the segment address for every i2c read in which the
232 * middle-of-tranaction flag is set. This is required to support EDID
233 * reads of more than 2 blocks as the segment address is reset to 0
234 * since we are overriding the middle-of-transaction flag for read
239 memset(&helper_msg, 0, sizeof(helper_msg));
240 helper_msg.address = segment_address;
241 helper_msg.buffer = &aux->segment;
243 dp_aux_cmd_fifo_tx(aux, &helper_msg);
247 * Send the offset address for every i2c read in which the
248 * middle-of-transaction flag is set. This will ensure that the sink
249 * will update its read pointer and return the correct portion of the
250 * EDID buffer in the subsequent i2c read trasntion triggered in the
251 * native AUX transfer function.
253 memset(&helper_msg, 0, sizeof(helper_msg));
254 helper_msg.address = input_msg->address;
255 helper_msg.buffer = &aux->offset;
257 dp_aux_cmd_fifo_tx(aux, &helper_msg);
260 aux->offset += message_size;
261 if (aux->offset == 0x80 || aux->offset == 0x100)
262 aux->segment = 0x0; /* reset segment at end of block */
266 * This function does the real job to process an AUX transaction.
267 * It will call aux_reset() function to reset the AUX channel,
268 * if the waiting is timeout.
270 static ssize_t dp_aux_transfer(struct drm_dp_aux *dp_aux,
271 struct drm_dp_aux_msg *msg)
274 int const aux_cmd_native_max = 16;
275 int const aux_cmd_i2c_max = 128;
276 struct dp_aux_private *aux;
278 aux = container_of(dp_aux, struct dp_aux_private, dp_aux);
280 aux->native = msg->request & (DP_AUX_NATIVE_WRITE & DP_AUX_NATIVE_READ);
282 /* Ignore address only message */
283 if (msg->size == 0 || !msg->buffer) {
284 msg->reply = aux->native ?
285 DP_AUX_NATIVE_REPLY_ACK : DP_AUX_I2C_REPLY_ACK;
289 /* msg sanity check */
290 if ((aux->native && msg->size > aux_cmd_native_max) ||
291 msg->size > aux_cmd_i2c_max) {
292 DRM_ERROR("%s: invalid msg: size(%zu), request(%x)\n",
293 __func__, msg->size, msg->request);
297 ret = pm_runtime_resume_and_get(dp_aux->dev);
301 mutex_lock(&aux->mutex);
308 * For eDP it's important to give a reasonably long wait here for HPD
309 * to be asserted. This is because the panel driver may have _just_
310 * turned on the panel and then tried to do an AUX transfer. The panel
311 * driver has no way of knowing when the panel is ready, so it's up
312 * to us to wait. For DP we never get into this situation so let's
313 * avoid ever doing the extra long wait for DP.
316 ret = dp_catalog_aux_wait_for_hpd_connect_state(aux->catalog);
318 DRM_DEBUG_DP("Panel not ready for aux transactions\n");
323 dp_aux_update_offset_and_segment(aux, msg);
324 dp_aux_transfer_helper(aux, msg, true);
326 aux->read = msg->request & (DP_AUX_I2C_READ & DP_AUX_NATIVE_READ);
327 aux->cmd_busy = true;
330 aux->no_send_addr = true;
331 aux->no_send_stop = false;
333 aux->no_send_addr = true;
334 aux->no_send_stop = true;
337 ret = dp_aux_cmd_fifo_tx(aux, msg);
341 if (!(aux->retry_cnt % MAX_AUX_RETRIES))
342 phy_calibrate(aux->phy);
344 /* reset aux if link is in connected state */
345 if (dp_catalog_link_is_connected(aux->catalog))
346 dp_catalog_aux_reset(aux->catalog);
349 switch (aux->aux_error_num) {
350 case DP_AUX_ERR_NONE:
352 ret = dp_aux_cmd_fifo_rx(aux, msg);
353 msg->reply = aux->native ? DP_AUX_NATIVE_REPLY_ACK : DP_AUX_I2C_REPLY_ACK;
355 case DP_AUX_ERR_DEFER:
356 msg->reply = aux->native ? DP_AUX_NATIVE_REPLY_DEFER : DP_AUX_I2C_REPLY_DEFER;
359 case DP_AUX_ERR_ADDR:
360 case DP_AUX_ERR_NACK:
361 case DP_AUX_ERR_NACK_DEFER:
362 msg->reply = aux->native ? DP_AUX_NATIVE_REPLY_NACK : DP_AUX_I2C_REPLY_NACK;
364 case DP_AUX_ERR_TOUT:
370 aux->cmd_busy = false;
373 mutex_unlock(&aux->mutex);
374 pm_runtime_put_sync(dp_aux->dev);
379 irqreturn_t dp_aux_isr(struct drm_dp_aux *dp_aux)
382 struct dp_aux_private *aux;
385 DRM_ERROR("invalid input\n");
389 aux = container_of(dp_aux, struct dp_aux_private, dp_aux);
391 isr = dp_catalog_aux_get_irq(aux->catalog);
393 /* no interrupts pending, return immediately */
397 if (!aux->cmd_busy) {
398 DRM_ERROR("Unexpected DP AUX IRQ %#010x when not busy\n", isr);
403 * The logic below assumes only one error bit is set (other than "done"
404 * which can apparently be set at the same time as some of the other
405 * bits). Warn if more than one get set so we know we need to improve
408 if (hweight32(isr & ~DP_INTR_AUX_XFER_DONE) > 1)
409 DRM_WARN("Some DP AUX interrupts unhandled: %#010x\n", isr);
411 if (isr & DP_INTR_AUX_ERROR) {
412 aux->aux_error_num = DP_AUX_ERR_PHY;
413 dp_catalog_aux_clear_hw_interrupts(aux->catalog);
414 } else if (isr & DP_INTR_NACK_DEFER) {
415 aux->aux_error_num = DP_AUX_ERR_NACK_DEFER;
416 } else if (isr & DP_INTR_WRONG_ADDR) {
417 aux->aux_error_num = DP_AUX_ERR_ADDR;
418 } else if (isr & DP_INTR_TIMEOUT) {
419 aux->aux_error_num = DP_AUX_ERR_TOUT;
420 } else if (!aux->native && (isr & DP_INTR_I2C_NACK)) {
421 aux->aux_error_num = DP_AUX_ERR_NACK;
422 } else if (!aux->native && (isr & DP_INTR_I2C_DEFER)) {
423 if (isr & DP_INTR_AUX_XFER_DONE)
424 aux->aux_error_num = DP_AUX_ERR_NACK;
426 aux->aux_error_num = DP_AUX_ERR_DEFER;
427 } else if (isr & DP_INTR_AUX_XFER_DONE) {
428 aux->aux_error_num = DP_AUX_ERR_NONE;
430 DRM_WARN("Unexpected interrupt: %#010x\n", isr);
434 complete(&aux->comp);
439 void dp_aux_reconfig(struct drm_dp_aux *dp_aux)
441 struct dp_aux_private *aux;
443 aux = container_of(dp_aux, struct dp_aux_private, dp_aux);
445 phy_calibrate(aux->phy);
446 dp_catalog_aux_reset(aux->catalog);
449 void dp_aux_init(struct drm_dp_aux *dp_aux)
451 struct dp_aux_private *aux;
454 DRM_ERROR("invalid input\n");
458 aux = container_of(dp_aux, struct dp_aux_private, dp_aux);
460 mutex_lock(&aux->mutex);
462 dp_catalog_aux_enable(aux->catalog, true);
466 mutex_unlock(&aux->mutex);
469 void dp_aux_deinit(struct drm_dp_aux *dp_aux)
471 struct dp_aux_private *aux;
473 aux = container_of(dp_aux, struct dp_aux_private, dp_aux);
475 mutex_lock(&aux->mutex);
477 aux->initted = false;
478 dp_catalog_aux_enable(aux->catalog, false);
480 mutex_unlock(&aux->mutex);
483 int dp_aux_register(struct drm_dp_aux *dp_aux)
488 DRM_ERROR("invalid input\n");
492 ret = drm_dp_aux_register(dp_aux);
494 DRM_ERROR("%s: failed to register drm aux: %d\n", __func__,
502 void dp_aux_unregister(struct drm_dp_aux *dp_aux)
504 drm_dp_aux_unregister(dp_aux);
507 static int dp_wait_hpd_asserted(struct drm_dp_aux *dp_aux,
508 unsigned long wait_us)
511 struct dp_aux_private *aux;
513 aux = container_of(dp_aux, struct dp_aux_private, dp_aux);
515 pm_runtime_get_sync(aux->dev);
516 ret = dp_catalog_aux_wait_for_hpd_connect_state(aux->catalog);
517 pm_runtime_put_sync(aux->dev);
522 struct drm_dp_aux *dp_aux_get(struct device *dev, struct dp_catalog *catalog,
526 struct dp_aux_private *aux;
529 DRM_ERROR("invalid input\n");
530 return ERR_PTR(-ENODEV);
533 aux = devm_kzalloc(dev, sizeof(*aux), GFP_KERNEL);
535 return ERR_PTR(-ENOMEM);
537 init_completion(&aux->comp);
538 aux->cmd_busy = false;
539 aux->is_edp = is_edp;
540 mutex_init(&aux->mutex);
543 aux->catalog = catalog;
548 * Use the drm_dp_aux_init() to use the aux adapter
549 * before registering AUX with the DRM device so that
550 * msm eDP panel can be detected by generic_dep_panel_probe().
552 aux->dp_aux.name = "dpu_dp_aux";
553 aux->dp_aux.dev = dev;
554 aux->dp_aux.transfer = dp_aux_transfer;
555 aux->dp_aux.wait_hpd_asserted = dp_wait_hpd_asserted;
556 drm_dp_aux_init(&aux->dp_aux);
561 void dp_aux_put(struct drm_dp_aux *dp_aux)
563 struct dp_aux_private *aux;
568 aux = container_of(dp_aux, struct dp_aux_private, dp_aux);
570 mutex_destroy(&aux->mutex);
572 devm_kfree(aux->dev, aux);