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 msm_dp_aux_private {
25 struct msm_dp_catalog *catalog;
30 struct completion comp;
32 enum msm_dp_aux_err aux_error_num;
45 struct drm_dp_aux msm_dp_aux;
48 #define MAX_AUX_RETRIES 5
50 static ssize_t msm_dp_aux_write(struct msm_dp_aux_private *aux,
51 struct drm_dp_aux_msg *msg)
56 u8 *msgdata = msg->buffer;
57 int const AUX_CMD_FIFO_LEN = 128;
66 * cmd fifo only has depth of 144 bytes
67 * limit buf length to 128 bytes here
69 if (len > AUX_CMD_FIFO_LEN - 4) {
70 DRM_ERROR("buf size greater than allowed size of 128 bytes\n");
74 /* Pack cmd and write to HW */
75 data[0] = (msg->address >> 16) & 0xf; /* addr[19:16] */
77 data[0] |= BIT(4); /* R/W */
79 data[1] = msg->address >> 8; /* addr[15:8] */
80 data[2] = msg->address; /* addr[7:0] */
81 data[3] = msg->size - 1; /* len[7:0] */
83 for (i = 0; i < len + 4; i++) {
84 reg = (i < 4) ? data[i] : msgdata[i - 4];
85 reg <<= DP_AUX_DATA_OFFSET;
86 reg &= DP_AUX_DATA_MASK;
87 reg |= DP_AUX_DATA_WRITE;
88 /* index = 0, write */
90 reg |= DP_AUX_DATA_INDEX_WRITE;
91 msm_dp_catalog_aux_write_data(aux->catalog, reg);
94 msm_dp_catalog_aux_clear_trans(aux->catalog, false);
95 msm_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 msm_dp_catalog_aux_write_trans(aux->catalog, reg);
114 static ssize_t msm_dp_aux_cmd_fifo_tx(struct msm_dp_aux_private *aux,
115 struct drm_dp_aux_msg *msg)
118 unsigned long time_left;
120 reinit_completion(&aux->comp);
122 ret = msm_dp_aux_write(aux, msg);
126 time_left = wait_for_completion_timeout(&aux->comp,
127 msecs_to_jiffies(250));
134 static ssize_t msm_dp_aux_cmd_fifo_rx(struct msm_dp_aux_private *aux,
135 struct drm_dp_aux_msg *msg)
142 msm_dp_catalog_aux_clear_trans(aux->catalog, true);
144 data = DP_AUX_DATA_INDEX_WRITE; /* INDEX_WRITE */
145 data |= DP_AUX_DATA_READ; /* read */
147 msm_dp_catalog_aux_write_data(aux->catalog, data);
151 /* discard first byte */
152 data = msm_dp_catalog_aux_read_data(aux->catalog);
154 for (i = 0; i < len; i++) {
155 data = msm_dp_catalog_aux_read_data(aux->catalog);
156 *dp++ = (u8)((data >> DP_AUX_DATA_OFFSET) & 0xff);
158 actual_i = (data >> DP_AUX_DATA_INDEX_OFFSET) & 0xFF;
166 static void msm_dp_aux_update_offset_and_segment(struct msm_dp_aux_private *aux,
167 struct drm_dp_aux_msg *input_msg)
169 u32 edid_address = 0x50;
170 u32 segment_address = 0x30;
171 bool i2c_read = input_msg->request &
172 (DP_AUX_I2C_READ & DP_AUX_NATIVE_READ);
175 if (aux->native || i2c_read || ((input_msg->address != edid_address) &&
176 (input_msg->address != segment_address)))
180 data = input_msg->buffer;
181 if (input_msg->address == segment_address)
182 aux->segment = *data;
188 * msm_dp_aux_transfer_helper() - helper function for EDID read transactions
190 * @aux: DP AUX private structure
191 * @input_msg: input message from DRM upstream APIs
192 * @send_seg: send the segment to sink
196 * This helper function is used to fix EDID reads for non-compliant
197 * sinks that do not handle the i2c middle-of-transaction flag correctly.
199 static void msm_dp_aux_transfer_helper(struct msm_dp_aux_private *aux,
200 struct drm_dp_aux_msg *input_msg,
203 struct drm_dp_aux_msg helper_msg;
204 u32 message_size = 0x10;
205 u32 segment_address = 0x30;
206 u32 const edid_block_length = 0x80;
207 bool i2c_mot = input_msg->request & DP_AUX_I2C_MOT;
208 bool i2c_read = input_msg->request &
209 (DP_AUX_I2C_READ & DP_AUX_NATIVE_READ);
211 if (!i2c_mot || !i2c_read || (input_msg->size == 0))
215 * Sending the segment value and EDID offset will be performed
216 * from the DRM upstream EDID driver for each block. Avoid
217 * duplicate AUX transactions related to this while reading the
218 * first 16 bytes of each block.
220 if (!(aux->offset % edid_block_length) || !send_seg)
224 aux->cmd_busy = true;
225 aux->no_send_addr = true;
226 aux->no_send_stop = true;
229 * Send the segment address for every i2c read in which the
230 * middle-of-tranaction flag is set. This is required to support EDID
231 * reads of more than 2 blocks as the segment address is reset to 0
232 * since we are overriding the middle-of-transaction flag for read
237 memset(&helper_msg, 0, sizeof(helper_msg));
238 helper_msg.address = segment_address;
239 helper_msg.buffer = &aux->segment;
241 msm_dp_aux_cmd_fifo_tx(aux, &helper_msg);
245 * Send the offset address for every i2c read in which the
246 * middle-of-transaction flag is set. This will ensure that the sink
247 * will update its read pointer and return the correct portion of the
248 * EDID buffer in the subsequent i2c read trasntion triggered in the
249 * native AUX transfer function.
251 memset(&helper_msg, 0, sizeof(helper_msg));
252 helper_msg.address = input_msg->address;
253 helper_msg.buffer = &aux->offset;
255 msm_dp_aux_cmd_fifo_tx(aux, &helper_msg);
258 aux->offset += message_size;
259 if (aux->offset == 0x80 || aux->offset == 0x100)
260 aux->segment = 0x0; /* reset segment at end of block */
264 * This function does the real job to process an AUX transaction.
265 * It will call aux_reset() function to reset the AUX channel,
266 * if the waiting is timeout.
268 static ssize_t msm_dp_aux_transfer(struct drm_dp_aux *msm_dp_aux,
269 struct drm_dp_aux_msg *msg)
272 int const aux_cmd_native_max = 16;
273 int const aux_cmd_i2c_max = 128;
274 struct msm_dp_aux_private *aux;
276 aux = container_of(msm_dp_aux, struct msm_dp_aux_private, msm_dp_aux);
278 aux->native = msg->request & (DP_AUX_NATIVE_WRITE & DP_AUX_NATIVE_READ);
280 /* Ignore address only message */
281 if (msg->size == 0 || !msg->buffer) {
282 msg->reply = aux->native ?
283 DP_AUX_NATIVE_REPLY_ACK : DP_AUX_I2C_REPLY_ACK;
287 /* msg sanity check */
288 if ((aux->native && msg->size > aux_cmd_native_max) ||
289 msg->size > aux_cmd_i2c_max) {
290 DRM_ERROR("%s: invalid msg: size(%zu), request(%x)\n",
291 __func__, msg->size, msg->request);
295 ret = pm_runtime_resume_and_get(msm_dp_aux->dev);
299 mutex_lock(&aux->mutex);
306 * If we're using DP and an external display isn't connected then the
307 * transfer won't succeed. Return right away. If we don't do this we
308 * can end up with long timeouts if someone tries to access the DP AUX
309 * character device when no DP device is connected.
311 if (!aux->is_edp && !aux->enable_xfers) {
316 msm_dp_aux_update_offset_and_segment(aux, msg);
317 msm_dp_aux_transfer_helper(aux, msg, true);
319 aux->read = msg->request & (DP_AUX_I2C_READ & DP_AUX_NATIVE_READ);
320 aux->cmd_busy = true;
323 aux->no_send_addr = true;
324 aux->no_send_stop = false;
326 aux->no_send_addr = true;
327 aux->no_send_stop = true;
330 ret = msm_dp_aux_cmd_fifo_tx(aux, msg);
334 if (!(aux->retry_cnt % MAX_AUX_RETRIES))
335 phy_calibrate(aux->phy);
337 /* reset aux if link is in connected state */
338 if (msm_dp_catalog_link_is_connected(aux->catalog))
339 msm_dp_catalog_aux_reset(aux->catalog);
342 switch (aux->aux_error_num) {
343 case DP_AUX_ERR_NONE:
345 ret = msm_dp_aux_cmd_fifo_rx(aux, msg);
346 msg->reply = aux->native ? DP_AUX_NATIVE_REPLY_ACK : DP_AUX_I2C_REPLY_ACK;
348 case DP_AUX_ERR_DEFER:
349 msg->reply = aux->native ? DP_AUX_NATIVE_REPLY_DEFER : DP_AUX_I2C_REPLY_DEFER;
352 case DP_AUX_ERR_ADDR:
353 case DP_AUX_ERR_NACK:
354 case DP_AUX_ERR_NACK_DEFER:
355 msg->reply = aux->native ? DP_AUX_NATIVE_REPLY_NACK : DP_AUX_I2C_REPLY_NACK;
357 case DP_AUX_ERR_TOUT:
363 aux->cmd_busy = false;
366 mutex_unlock(&aux->mutex);
367 pm_runtime_put_sync(msm_dp_aux->dev);
372 irqreturn_t msm_dp_aux_isr(struct drm_dp_aux *msm_dp_aux)
375 struct msm_dp_aux_private *aux;
378 DRM_ERROR("invalid input\n");
382 aux = container_of(msm_dp_aux, struct msm_dp_aux_private, msm_dp_aux);
384 isr = msm_dp_catalog_aux_get_irq(aux->catalog);
386 /* no interrupts pending, return immediately */
390 if (!aux->cmd_busy) {
391 DRM_ERROR("Unexpected DP AUX IRQ %#010x when not busy\n", isr);
396 * The logic below assumes only one error bit is set (other than "done"
397 * which can apparently be set at the same time as some of the other
398 * bits). Warn if more than one get set so we know we need to improve
401 if (hweight32(isr & ~DP_INTR_AUX_XFER_DONE) > 1)
402 DRM_WARN("Some DP AUX interrupts unhandled: %#010x\n", isr);
404 if (isr & DP_INTR_AUX_ERROR) {
405 aux->aux_error_num = DP_AUX_ERR_PHY;
406 msm_dp_catalog_aux_clear_hw_interrupts(aux->catalog);
407 } else if (isr & DP_INTR_NACK_DEFER) {
408 aux->aux_error_num = DP_AUX_ERR_NACK_DEFER;
409 } else if (isr & DP_INTR_WRONG_ADDR) {
410 aux->aux_error_num = DP_AUX_ERR_ADDR;
411 } else if (isr & DP_INTR_TIMEOUT) {
412 aux->aux_error_num = DP_AUX_ERR_TOUT;
413 } else if (!aux->native && (isr & DP_INTR_I2C_NACK)) {
414 aux->aux_error_num = DP_AUX_ERR_NACK;
415 } else if (!aux->native && (isr & DP_INTR_I2C_DEFER)) {
416 if (isr & DP_INTR_AUX_XFER_DONE)
417 aux->aux_error_num = DP_AUX_ERR_NACK;
419 aux->aux_error_num = DP_AUX_ERR_DEFER;
420 } else if (isr & DP_INTR_AUX_XFER_DONE) {
421 aux->aux_error_num = DP_AUX_ERR_NONE;
423 DRM_WARN("Unexpected interrupt: %#010x\n", isr);
427 complete(&aux->comp);
432 void msm_dp_aux_enable_xfers(struct drm_dp_aux *msm_dp_aux, bool enabled)
434 struct msm_dp_aux_private *aux;
436 aux = container_of(msm_dp_aux, struct msm_dp_aux_private, msm_dp_aux);
437 aux->enable_xfers = enabled;
440 void msm_dp_aux_reconfig(struct drm_dp_aux *msm_dp_aux)
442 struct msm_dp_aux_private *aux;
444 aux = container_of(msm_dp_aux, struct msm_dp_aux_private, msm_dp_aux);
446 phy_calibrate(aux->phy);
447 msm_dp_catalog_aux_reset(aux->catalog);
450 void msm_dp_aux_init(struct drm_dp_aux *msm_dp_aux)
452 struct msm_dp_aux_private *aux;
455 DRM_ERROR("invalid input\n");
459 aux = container_of(msm_dp_aux, struct msm_dp_aux_private, msm_dp_aux);
461 mutex_lock(&aux->mutex);
463 msm_dp_catalog_aux_enable(aux->catalog, true);
467 mutex_unlock(&aux->mutex);
470 void msm_dp_aux_deinit(struct drm_dp_aux *msm_dp_aux)
472 struct msm_dp_aux_private *aux;
474 aux = container_of(msm_dp_aux, struct msm_dp_aux_private, msm_dp_aux);
476 mutex_lock(&aux->mutex);
478 aux->initted = false;
479 msm_dp_catalog_aux_enable(aux->catalog, false);
481 mutex_unlock(&aux->mutex);
484 int msm_dp_aux_register(struct drm_dp_aux *msm_dp_aux)
489 DRM_ERROR("invalid input\n");
493 ret = drm_dp_aux_register(msm_dp_aux);
495 DRM_ERROR("%s: failed to register drm aux: %d\n", __func__,
503 void msm_dp_aux_unregister(struct drm_dp_aux *msm_dp_aux)
505 drm_dp_aux_unregister(msm_dp_aux);
508 static int msm_dp_wait_hpd_asserted(struct drm_dp_aux *msm_dp_aux,
509 unsigned long wait_us)
512 struct msm_dp_aux_private *aux;
514 aux = container_of(msm_dp_aux, struct msm_dp_aux_private, msm_dp_aux);
516 ret = pm_runtime_resume_and_get(aux->dev);
520 ret = msm_dp_catalog_aux_wait_for_hpd_connect_state(aux->catalog, wait_us);
521 pm_runtime_put_sync(aux->dev);
526 struct drm_dp_aux *msm_dp_aux_get(struct device *dev, struct msm_dp_catalog *catalog,
530 struct msm_dp_aux_private *aux;
533 DRM_ERROR("invalid input\n");
534 return ERR_PTR(-ENODEV);
537 aux = devm_kzalloc(dev, sizeof(*aux), GFP_KERNEL);
539 return ERR_PTR(-ENOMEM);
541 init_completion(&aux->comp);
542 aux->cmd_busy = false;
543 aux->is_edp = is_edp;
544 mutex_init(&aux->mutex);
547 aux->catalog = catalog;
552 * Use the drm_dp_aux_init() to use the aux adapter
553 * before registering AUX with the DRM device so that
554 * msm eDP panel can be detected by generic_dep_panel_probe().
556 aux->msm_dp_aux.name = "dpu_dp_aux";
557 aux->msm_dp_aux.dev = dev;
558 aux->msm_dp_aux.transfer = msm_dp_aux_transfer;
559 aux->msm_dp_aux.wait_hpd_asserted = msm_dp_wait_hpd_asserted;
560 drm_dp_aux_init(&aux->msm_dp_aux);
562 return &aux->msm_dp_aux;
565 void msm_dp_aux_put(struct drm_dp_aux *msm_dp_aux)
567 struct msm_dp_aux_private *aux;
572 aux = container_of(msm_dp_aux, struct msm_dp_aux_private, msm_dp_aux);
574 mutex_destroy(&aux->mutex);
576 devm_kfree(aux->dev, aux);