2 * Copyright 2012-15 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.
27 * Pre-requisites: headers required by header of this unit
30 #include "dm_services.h"
31 #include "include/gpio_interface.h"
32 #include "include/gpio_service_interface.h"
33 #include "hw_translate.h"
34 #include "hw_factory.h"
40 #include "gpio_service.h"
43 * Post-requisites: headers required by this unit
53 struct gpio_service *dal_gpio_service_create(
54 enum dce_version dce_version_major,
55 enum dce_version dce_version_minor,
56 struct dc_context *ctx)
58 struct gpio_service *service;
62 service = kzalloc(sizeof(struct gpio_service), GFP_KERNEL);
69 if (!dal_hw_translate_init(&service->translate, dce_version_major,
75 if (!dal_hw_factory_init(&service->factory, dce_version_major,
81 /* allocate and initialize business storage */
83 const uint32_t bits_per_uint = sizeof(uint32_t) << 3;
89 uint32_t number_of_bits =
90 service->factory.number_of_pins[index_of_id];
92 uint32_t number_of_uints =
93 (number_of_bits + bits_per_uint - 1) /
99 uint32_t index_of_uint = 0;
101 slot = kcalloc(number_of_uints,
111 slot[index_of_uint] = 0;
114 } while (index_of_uint < number_of_uints);
118 service->busyness[index_of_id] = slot;
121 } while (index_of_id < GPIO_ID_COUNT);
127 while (index_of_id) {
132 slot = service->busyness[index_of_id];
143 struct gpio *dal_gpio_service_create_irq(
144 struct gpio_service *service,
151 if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en)) {
152 ASSERT_CRITICAL(false);
156 return dal_gpio_create_irq(service, id, en);
159 void dal_gpio_service_destroy(
160 struct gpio_service **ptr)
167 /* free business storage */
169 uint32_t index_of_id = 0;
172 uint32_t *slot = (*ptr)->busyness[index_of_id];
177 } while (index_of_id < GPIO_ID_COUNT);
190 static bool is_pin_busy(
191 const struct gpio_service *service,
195 const uint32_t bits_per_uint = sizeof(uint32_t) << 3;
197 const uint32_t *slot = service->busyness[id] + (en / bits_per_uint);
199 return 0 != (*slot & (1 << (en % bits_per_uint)));
202 static void set_pin_busy(
203 struct gpio_service *service,
207 const uint32_t bits_per_uint = sizeof(uint32_t) << 3;
209 service->busyness[id][en / bits_per_uint] |=
210 (1 << (en % bits_per_uint));
213 static void set_pin_free(
214 struct gpio_service *service,
218 const uint32_t bits_per_uint = sizeof(uint32_t) << 3;
220 service->busyness[id][en / bits_per_uint] &=
221 ~(1 << (en % bits_per_uint));
224 enum gpio_result dal_gpio_service_open(
225 struct gpio_service *service,
229 struct hw_gpio_pin **ptr)
231 struct hw_gpio_pin *pin;
233 if (!service->busyness[id]) {
234 ASSERT_CRITICAL(false);
235 return GPIO_RESULT_OPEN_FAILED;
238 if (is_pin_busy(service, id, en)) {
239 ASSERT_CRITICAL(false);
240 return GPIO_RESULT_DEVICE_BUSY;
244 case GPIO_ID_DDC_DATA:
245 pin = service->factory.funcs->create_ddc_data(
246 service->ctx, id, en);
247 service->factory.funcs->define_ddc_registers(pin, en);
249 case GPIO_ID_DDC_CLOCK:
250 pin = service->factory.funcs->create_ddc_clock(
251 service->ctx, id, en);
252 service->factory.funcs->define_ddc_registers(pin, en);
254 case GPIO_ID_GENERIC:
255 pin = service->factory.funcs->create_generic(
256 service->ctx, id, en);
259 pin = service->factory.funcs->create_hpd(
260 service->ctx, id, en);
261 service->factory.funcs->define_hpd_registers(pin, en);
264 pin = service->factory.funcs->create_sync(
265 service->ctx, id, en);
268 pin = service->factory.funcs->create_gsl(
269 service->ctx, id, en);
272 ASSERT_CRITICAL(false);
273 return GPIO_RESULT_NON_SPECIFIC_ERROR;
277 ASSERT_CRITICAL(false);
278 return GPIO_RESULT_NON_SPECIFIC_ERROR;
281 if (!pin->funcs->open(pin, mode)) {
282 ASSERT_CRITICAL(false);
283 dal_gpio_service_close(service, &pin);
284 return GPIO_RESULT_OPEN_FAILED;
287 set_pin_busy(service, id, en);
289 return GPIO_RESULT_OK;
292 void dal_gpio_service_close(
293 struct gpio_service *service,
294 struct hw_gpio_pin **ptr)
296 struct hw_gpio_pin *pin;
299 ASSERT_CRITICAL(false);
306 set_pin_free(service, pin->id, pin->en);
308 pin->funcs->close(pin);
310 pin->funcs->destroy(ptr);
315 enum dc_irq_source dal_irq_get_source(
316 const struct gpio *irq)
318 enum gpio_id id = dal_gpio_get_id(irq);
322 return (enum dc_irq_source)(DC_IRQ_SOURCE_HPD1 +
323 dal_gpio_get_enum(irq));
324 case GPIO_ID_GPIO_PAD:
325 return (enum dc_irq_source)(DC_IRQ_SOURCE_GPIOPAD0 +
326 dal_gpio_get_enum(irq));
328 return DC_IRQ_SOURCE_INVALID;
332 enum dc_irq_source dal_irq_get_rx_source(
333 const struct gpio *irq)
335 enum gpio_id id = dal_gpio_get_id(irq);
339 return (enum dc_irq_source)(DC_IRQ_SOURCE_HPD1RX +
340 dal_gpio_get_enum(irq));
342 return DC_IRQ_SOURCE_INVALID;
346 enum gpio_result dal_irq_setup_hpd_filter(
348 struct gpio_hpd_config *config)
350 struct gpio_config_data config_data;
353 return GPIO_RESULT_INVALID_DATA;
355 config_data.type = GPIO_CONFIG_TYPE_HPD;
356 config_data.config.hpd = *config;
358 return dal_gpio_set_config(irq, &config_data);
363 * Creation and destruction
366 struct gpio *dal_gpio_create_irq(
367 struct gpio_service *service,
375 case GPIO_ID_GPIO_PAD:
378 ASSERT_CRITICAL(false);
382 irq = dal_gpio_create(
383 service, id, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
388 ASSERT_CRITICAL(false);
392 void dal_gpio_destroy_irq(
396 ASSERT_CRITICAL(false);
400 dal_gpio_close(*irq);
401 dal_gpio_destroy(irq);
407 struct ddc *dal_gpio_create_ddc(
408 struct gpio_service *service,
411 struct gpio_ddc_hw_info *info)
417 if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en))
420 ddc = kzalloc(sizeof(struct ddc), GFP_KERNEL);
427 ddc->pin_data = dal_gpio_create(
428 service, GPIO_ID_DDC_DATA, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
430 if (!ddc->pin_data) {
435 ddc->pin_clock = dal_gpio_create(
436 service, GPIO_ID_DDC_CLOCK, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
438 if (!ddc->pin_clock) {
443 ddc->hw_info = *info;
445 ddc->ctx = service->ctx;
450 dal_gpio_destroy(&ddc->pin_data);
458 void dal_gpio_destroy_ddc(
467 dal_gpio_destroy(&(*ddc)->pin_data);
468 dal_gpio_destroy(&(*ddc)->pin_clock);
474 enum gpio_result dal_ddc_open(
477 enum gpio_ddc_config_type config_type)
479 enum gpio_result result;
481 struct gpio_config_data config_data;
482 struct hw_gpio *hw_data;
483 struct hw_gpio *hw_clock;
485 result = dal_gpio_open_ex(ddc->pin_data, mode);
487 if (result != GPIO_RESULT_OK) {
492 result = dal_gpio_open_ex(ddc->pin_clock, mode);
494 if (result != GPIO_RESULT_OK) {
499 /* DDC clock and data pins should belong
500 * to the same DDC block id,
501 * we use the data pin to set the pad mode. */
503 if (mode == GPIO_MODE_INPUT)
504 /* this is from detect_sink_type,
505 * we need extra delay there */
506 config_data.type = GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE;
508 config_data.type = GPIO_CONFIG_TYPE_DDC;
510 config_data.config.ddc.type = config_type;
512 hw_data = FROM_HW_GPIO_PIN(ddc->pin_data->pin);
513 hw_clock = FROM_HW_GPIO_PIN(ddc->pin_clock->pin);
515 config_data.config.ddc.data_en_bit_present = hw_data->store.en != 0;
516 config_data.config.ddc.clock_en_bit_present = hw_clock->store.en != 0;
518 result = dal_gpio_set_config(ddc->pin_data, &config_data);
520 if (result == GPIO_RESULT_OK)
525 dal_gpio_close(ddc->pin_clock);
528 dal_gpio_close(ddc->pin_data);
533 enum gpio_result dal_ddc_change_mode(
537 enum gpio_result result;
539 enum gpio_mode original_mode =
540 dal_gpio_get_mode(ddc->pin_data);
542 result = dal_gpio_change_mode(ddc->pin_data, mode);
544 /* [anaumov] DAL2 code returns GPIO_RESULT_NON_SPECIFIC_ERROR
545 * in case of failures;
546 * set_mode() is so that, in case of failure,
547 * we must explicitly set original mode */
549 if (result != GPIO_RESULT_OK)
552 result = dal_gpio_change_mode(ddc->pin_clock, mode);
554 if (result == GPIO_RESULT_OK)
557 dal_gpio_change_mode(ddc->pin_clock, original_mode);
560 dal_gpio_change_mode(ddc->pin_data, original_mode);
565 enum gpio_ddc_line dal_ddc_get_line(
566 const struct ddc *ddc)
568 return (enum gpio_ddc_line)dal_gpio_get_enum(ddc->pin_data);
571 enum gpio_result dal_ddc_set_config(
573 enum gpio_ddc_config_type config_type)
575 struct gpio_config_data config_data;
577 config_data.type = GPIO_CONFIG_TYPE_DDC;
579 config_data.config.ddc.type = config_type;
580 config_data.config.ddc.data_en_bit_present = false;
581 config_data.config.ddc.clock_en_bit_present = false;
583 return dal_gpio_set_config(ddc->pin_data, &config_data);
589 dal_gpio_close(ddc->pin_clock);
590 dal_gpio_close(ddc->pin_data);