2 * Copyright 2018 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include <linux/delay.h>
29 #include "dce_i2c_sw.h"
30 #include "include/gpio_service_interface.h"
34 void dce_i2c_sw_construct(
35 struct dce_i2c_sw *dce_i2c_sw,
36 struct dc_context *ctx)
38 dce_i2c_sw->ctx = ctx;
41 static inline bool read_bit_from_ddc(
48 dal_gpio_get_value(ddc->pin_data, &value);
50 dal_gpio_get_value(ddc->pin_clock, &value);
55 static inline void write_bit_to_ddc(
60 uint32_t value = bit ? 1 : 0;
63 dal_gpio_set_value(ddc->pin_data, value);
65 dal_gpio_set_value(ddc->pin_clock, value);
68 static void release_engine_dce_sw(
69 struct resource_pool *pool,
70 struct dce_i2c_sw *dce_i2c_sw)
72 dal_ddc_close(dce_i2c_sw->ddc);
73 dce_i2c_sw->ddc = NULL;
76 static bool wait_for_scl_high_sw(
77 struct dc_context *ctx,
79 uint16_t clock_delay_div_4)
81 uint32_t scl_retry = 0;
82 uint32_t scl_retry_max = I2C_SW_TIMEOUT_DELAY / clock_delay_div_4;
84 udelay(clock_delay_div_4);
87 if (read_bit_from_ddc(ddc, SCL))
90 udelay(clock_delay_div_4);
93 } while (scl_retry <= scl_retry_max);
97 static bool write_byte_sw(
98 struct dc_context *ctx,
99 struct ddc *ddc_handle,
100 uint16_t clock_delay_div_4,
106 /* bits are transmitted serially, starting from MSB */
109 udelay(clock_delay_div_4);
111 write_bit_to_ddc(ddc_handle, SDA, (byte >> shift) & 1);
113 udelay(clock_delay_div_4);
115 write_bit_to_ddc(ddc_handle, SCL, true);
117 if (!wait_for_scl_high_sw(ctx, ddc_handle, clock_delay_div_4))
120 write_bit_to_ddc(ddc_handle, SCL, false);
123 } while (shift >= 0);
125 /* The display sends ACK by preventing the SDA from going high
126 * after the SCL pulse we use to send our last data bit.
127 * If the SDA goes high after that bit, it's a NACK
130 udelay(clock_delay_div_4);
132 write_bit_to_ddc(ddc_handle, SDA, true);
134 udelay(clock_delay_div_4);
136 write_bit_to_ddc(ddc_handle, SCL, true);
138 if (!wait_for_scl_high_sw(ctx, ddc_handle, clock_delay_div_4))
143 ack = !read_bit_from_ddc(ddc_handle, SDA);
145 udelay(clock_delay_div_4 << 1);
147 write_bit_to_ddc(ddc_handle, SCL, false);
149 udelay(clock_delay_div_4 << 1);
154 static bool read_byte_sw(
155 struct dc_context *ctx,
156 struct ddc *ddc_handle,
157 uint16_t clock_delay_div_4,
165 /* The data bits are read from MSB to LSB;
166 * bit is read while SCL is high
170 write_bit_to_ddc(ddc_handle, SCL, true);
172 if (!wait_for_scl_high_sw(ctx, ddc_handle, clock_delay_div_4))
175 if (read_bit_from_ddc(ddc_handle, SDA))
176 data |= (1 << shift);
178 write_bit_to_ddc(ddc_handle, SCL, false);
180 udelay(clock_delay_div_4 << 1);
183 } while (shift >= 0);
185 /* read only whole byte */
189 udelay(clock_delay_div_4);
191 /* send the acknowledge bit:
192 * SDA low means ACK, SDA high means NACK
195 write_bit_to_ddc(ddc_handle, SDA, !more);
197 udelay(clock_delay_div_4);
199 write_bit_to_ddc(ddc_handle, SCL, true);
201 if (!wait_for_scl_high_sw(ctx, ddc_handle, clock_delay_div_4))
204 write_bit_to_ddc(ddc_handle, SCL, false);
206 udelay(clock_delay_div_4);
208 write_bit_to_ddc(ddc_handle, SDA, true);
210 udelay(clock_delay_div_4);
214 static bool stop_sync_sw(
215 struct dc_context *ctx,
216 struct ddc *ddc_handle,
217 uint16_t clock_delay_div_4)
221 /* The I2C communications stop signal is:
222 * the SDA going high from low, while the SCL is high.
225 write_bit_to_ddc(ddc_handle, SCL, false);
227 udelay(clock_delay_div_4);
229 write_bit_to_ddc(ddc_handle, SDA, false);
231 udelay(clock_delay_div_4);
233 write_bit_to_ddc(ddc_handle, SCL, true);
235 if (!wait_for_scl_high_sw(ctx, ddc_handle, clock_delay_div_4))
238 write_bit_to_ddc(ddc_handle, SDA, true);
241 udelay(clock_delay_div_4);
243 if (read_bit_from_ddc(ddc_handle, SDA))
247 } while (retry <= 2);
251 static bool i2c_write_sw(
252 struct dc_context *ctx,
253 struct ddc *ddc_handle,
254 uint16_t clock_delay_div_4,
261 if (!write_byte_sw(ctx, ddc_handle, clock_delay_div_4, address))
265 if (!write_byte_sw(ctx, ddc_handle, clock_delay_div_4, data[i]))
273 static bool i2c_read_sw(
274 struct dc_context *ctx,
275 struct ddc *ddc_handle,
276 uint16_t clock_delay_div_4,
283 if (!write_byte_sw(ctx, ddc_handle, clock_delay_div_4, address))
287 if (!read_byte_sw(ctx, ddc_handle, clock_delay_div_4, data + i,
298 static bool start_sync_sw(
299 struct dc_context *ctx,
300 struct ddc *ddc_handle,
301 uint16_t clock_delay_div_4)
305 /* The I2C communications start signal is:
306 * the SDA going low from high, while the SCL is high.
309 write_bit_to_ddc(ddc_handle, SCL, true);
311 udelay(clock_delay_div_4);
314 write_bit_to_ddc(ddc_handle, SDA, true);
316 if (!read_bit_from_ddc(ddc_handle, SDA)) {
321 udelay(clock_delay_div_4);
323 write_bit_to_ddc(ddc_handle, SCL, true);
325 if (!wait_for_scl_high_sw(ctx, ddc_handle, clock_delay_div_4))
328 write_bit_to_ddc(ddc_handle, SDA, false);
330 udelay(clock_delay_div_4);
332 write_bit_to_ddc(ddc_handle, SCL, false);
334 udelay(clock_delay_div_4);
337 } while (retry <= I2C_SW_RETRIES);
342 static void dce_i2c_sw_engine_set_speed(
343 struct dce_i2c_sw *engine,
348 engine->speed = speed ? speed : DCE_I2C_DEFAULT_I2C_SW_SPEED;
350 engine->clock_delay = 1000 / engine->speed;
352 if (engine->clock_delay < 12)
353 engine->clock_delay = 12;
356 static bool dce_i2c_sw_engine_acquire_engine(
357 struct dce_i2c_sw *engine,
360 enum gpio_result result;
362 result = dal_ddc_open(ddc, GPIO_MODE_FAST_OUTPUT,
363 GPIO_DDC_CONFIG_TYPE_MODE_I2C);
365 if (result != GPIO_RESULT_OK)
372 bool dce_i2c_engine_acquire_sw(
373 struct dce_i2c_sw *dce_i2c_sw,
374 struct ddc *ddc_handle)
376 uint32_t counter = 0;
381 result = dce_i2c_sw_engine_acquire_engine(
382 dce_i2c_sw, ddc_handle);
387 /* i2c_engine is busy by VBios, lets wait and retry */
392 } while (counter < 2);
400 static void dce_i2c_sw_engine_submit_channel_request(
401 struct dce_i2c_sw *engine,
402 struct i2c_request_transaction_data *req)
404 struct ddc *ddc = engine->ddc;
405 uint16_t clock_delay_div_4 = engine->clock_delay >> 2;
407 /* send sync (start / repeated start) */
409 bool result = start_sync_sw(engine->ctx, ddc, clock_delay_div_4);
411 /* process payload */
414 switch (req->action) {
415 case DCE_I2C_TRANSACTION_ACTION_I2C_WRITE:
416 case DCE_I2C_TRANSACTION_ACTION_I2C_WRITE_MOT:
417 result = i2c_write_sw(engine->ctx, ddc, clock_delay_div_4,
418 req->address, req->length, req->data);
420 case DCE_I2C_TRANSACTION_ACTION_I2C_READ:
421 case DCE_I2C_TRANSACTION_ACTION_I2C_READ_MOT:
422 result = i2c_read_sw(engine->ctx, ddc, clock_delay_div_4,
423 req->address, req->length, req->data);
431 /* send stop if not 'mot' or operation failed */
434 (req->action == DCE_I2C_TRANSACTION_ACTION_I2C_WRITE) ||
435 (req->action == DCE_I2C_TRANSACTION_ACTION_I2C_READ))
436 if (!stop_sync_sw(engine->ctx, ddc, clock_delay_div_4))
439 req->status = result ?
440 I2C_CHANNEL_OPERATION_SUCCEEDED :
441 I2C_CHANNEL_OPERATION_FAILED;
444 static bool dce_i2c_sw_engine_submit_payload(
445 struct dce_i2c_sw *engine,
446 struct i2c_payload *payload,
447 bool middle_of_transaction)
449 struct i2c_request_transaction_data request;
452 request.action = middle_of_transaction ?
453 DCE_I2C_TRANSACTION_ACTION_I2C_READ_MOT :
454 DCE_I2C_TRANSACTION_ACTION_I2C_READ;
456 request.action = middle_of_transaction ?
457 DCE_I2C_TRANSACTION_ACTION_I2C_WRITE_MOT :
458 DCE_I2C_TRANSACTION_ACTION_I2C_WRITE;
460 request.address = (uint8_t) ((payload->address << 1) | !payload->write);
461 request.length = payload->length;
462 request.data = payload->data;
464 dce_i2c_sw_engine_submit_channel_request(engine, &request);
466 if ((request.status == I2C_CHANNEL_OPERATION_ENGINE_BUSY) ||
467 (request.status == I2C_CHANNEL_OPERATION_FAILED))
472 bool dce_i2c_submit_command_sw(
473 struct resource_pool *pool,
475 struct i2c_command *cmd,
476 struct dce_i2c_sw *dce_i2c_sw)
478 uint8_t index_of_payload = 0;
481 dce_i2c_sw_engine_set_speed(dce_i2c_sw, cmd->speed);
485 while (index_of_payload < cmd->number_of_payloads) {
486 bool mot = (index_of_payload != cmd->number_of_payloads - 1);
488 struct i2c_payload *payload = cmd->payloads + index_of_payload;
490 if (!dce_i2c_sw_engine_submit_payload(
491 dce_i2c_sw, payload, mot)) {
499 release_engine_dce_sw(pool, dce_i2c_sw);