1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright(c) 2016, Analogix Semiconductor.
5 * Based on anx7808 driver obtained from chromeos with copyright:
6 * Copyright(c) 2013, Google Inc.
8 #include <linux/regmap.h>
11 #include <drm/drm_dp_helper.h>
12 #include <drm/drm_print.h>
14 #include "analogix-i2c-dptx.h"
16 #define AUX_WAIT_TIMEOUT_MS 15
17 #define AUX_CH_BUFFER_SIZE 16
19 static int anx_i2c_dp_clear_bits(struct regmap
*map
, u8 reg
, u8 mask
)
21 return regmap_update_bits(map
, reg
, mask
, 0);
24 static bool anx_dp_aux_op_finished(struct regmap
*map_dptx
)
29 err
= regmap_read(map_dptx
, SP_DP_AUX_CH_CTRL2_REG
, &value
);
33 return (value
& SP_AUX_EN
) == 0;
36 static int anx_dp_aux_wait(struct regmap
*map_dptx
)
38 unsigned long timeout
;
42 timeout
= jiffies
+ msecs_to_jiffies(AUX_WAIT_TIMEOUT_MS
) + 1;
44 while (!anx_dp_aux_op_finished(map_dptx
)) {
45 if (time_after(jiffies
, timeout
)) {
46 if (!anx_dp_aux_op_finished(map_dptx
)) {
47 DRM_ERROR("Timed out waiting AUX to finish\n");
54 usleep_range(1000, 2000);
57 /* Read the AUX channel access status */
58 err
= regmap_read(map_dptx
, SP_AUX_CH_STATUS_REG
, &status
);
60 DRM_ERROR("Failed to read from AUX channel: %d\n", err
);
64 if (status
& SP_AUX_STATUS
) {
65 DRM_ERROR("Failed to wait for AUX channel (status: %02x)\n",
73 static int anx_dp_aux_address(struct regmap
*map_dptx
, unsigned int addr
)
77 err
= regmap_write(map_dptx
, SP_AUX_ADDR_7_0_REG
, addr
& 0xff);
81 err
= regmap_write(map_dptx
, SP_AUX_ADDR_15_8_REG
,
82 (addr
& 0xff00) >> 8);
87 * DP AUX CH Address Register #2, only update bits[3:0]
89 * [3:0] AUX_ADDR[19:16], Register control AUX CH address.
91 err
= regmap_update_bits(map_dptx
, SP_AUX_ADDR_19_16_REG
,
92 SP_AUX_ADDR_19_16_MASK
,
93 (addr
& 0xf0000) >> 16);
101 ssize_t
anx_dp_aux_transfer(struct regmap
*map_dptx
,
102 struct drm_dp_aux_msg
*msg
)
104 u8 ctrl1
= msg
->request
;
105 u8 ctrl2
= SP_AUX_EN
;
106 u8
*buffer
= msg
->buffer
;
109 /* The DP AUX transmit and receive buffer has 16 bytes. */
110 if (WARN_ON(msg
->size
> AUX_CH_BUFFER_SIZE
))
113 /* Zero-sized messages specify address-only transactions. */
115 ctrl2
|= SP_ADDR_ONLY
;
116 else /* For non-zero-sized set the length field. */
117 ctrl1
|= (msg
->size
- 1) << SP_AUX_LENGTH_SHIFT
;
119 if ((msg
->size
> 0) && ((msg
->request
& DP_AUX_I2C_READ
) == 0)) {
120 /* When WRITE | MOT write values to data buffer */
121 err
= regmap_bulk_write(map_dptx
,
122 SP_DP_BUF_DATA0_REG
, buffer
,
128 /* Write address and request */
129 err
= anx_dp_aux_address(map_dptx
, msg
->address
);
133 err
= regmap_write(map_dptx
, SP_DP_AUX_CH_CTRL1_REG
, ctrl1
);
137 /* Start transaction */
138 err
= regmap_update_bits(map_dptx
, SP_DP_AUX_CH_CTRL2_REG
,
139 SP_ADDR_ONLY
| SP_AUX_EN
, ctrl2
);
143 err
= anx_dp_aux_wait(map_dptx
);
147 msg
->reply
= DP_AUX_I2C_REPLY_ACK
;
149 if ((msg
->size
> 0) && (msg
->request
& DP_AUX_I2C_READ
)) {
150 /* Read values from data buffer */
151 err
= regmap_bulk_read(map_dptx
,
152 SP_DP_BUF_DATA0_REG
, buffer
,
158 err
= anx_i2c_dp_clear_bits(map_dptx
, SP_DP_AUX_CH_CTRL2_REG
,
165 EXPORT_SYMBOL_GPL(anx_dp_aux_transfer
);