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,
55 enum dce_environment dce_environment,
56 struct dc_context *ctx)
58 struct gpio_service *service;
61 service = kzalloc(sizeof(struct gpio_service), GFP_KERNEL);
68 if (!dal_hw_translate_init(&service->translate, dce_version,
74 if (!dal_hw_factory_init(&service->factory, dce_version,
80 /* allocate and initialize busyness storage */
86 uint32_t number_of_bits =
87 service->factory.number_of_pins[index_of_id];
91 service->busyness[index_of_id] =
92 kcalloc(number_of_bits, sizeof(char),
95 if (!service->busyness[index_of_id]) {
101 service->busyness[index_of_id][i] = 0;
103 } while (i < number_of_bits);
105 service->busyness[index_of_id] = NULL;
109 } while (index_of_id < GPIO_ID_COUNT);
115 while (index_of_id) {
117 kfree(service->busyness[index_of_id]);
126 struct gpio *dal_gpio_service_create_irq(
127 struct gpio_service *service,
134 if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en)) {
135 ASSERT_CRITICAL(false);
139 return dal_gpio_create_irq(service, id, en);
142 struct gpio *dal_gpio_service_create_generic_mux(
143 struct gpio_service *service,
149 struct gpio *generic;
151 if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en)) {
152 ASSERT_CRITICAL(false);
156 generic = dal_gpio_create(
157 service, id, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
162 void dal_gpio_destroy_generic_mux(
166 ASSERT_CRITICAL(false);
170 dal_gpio_destroy(mux);
176 struct gpio_pin_info dal_gpio_get_generic_pin_info(
177 struct gpio_service *service,
181 struct gpio_pin_info pin;
183 if (service->translate.funcs->id_to_offset) {
184 service->translate.funcs->id_to_offset(id, en, &pin);
186 pin.mask = 0xFFFFFFFF;
187 pin.offset = 0xFFFFFFFF;
193 void dal_gpio_service_destroy(
194 struct gpio_service **ptr)
201 /* free business storage */
203 uint32_t index_of_id = 0;
206 kfree((*ptr)->busyness[index_of_id]);
209 } while (index_of_id < GPIO_ID_COUNT);
217 enum gpio_result dal_mux_setup_config(
219 struct gpio_generic_mux_config *config)
221 struct gpio_config_data config_data;
224 return GPIO_RESULT_INVALID_DATA;
226 config_data.config.generic_mux = *config;
227 config_data.type = GPIO_CONFIG_TYPE_GENERIC_MUX;
229 return dal_gpio_set_config(mux, &config_data);
237 static bool is_pin_busy(
238 const struct gpio_service *service,
242 return service->busyness[id][en];
245 static void set_pin_busy(
246 struct gpio_service *service,
250 service->busyness[id][en] = true;
253 static void set_pin_free(
254 struct gpio_service *service,
258 service->busyness[id][en] = false;
261 enum gpio_result dal_gpio_service_lock(
262 struct gpio_service *service,
266 if (!service->busyness[id]) {
267 ASSERT_CRITICAL(false);
268 return GPIO_RESULT_OPEN_FAILED;
271 set_pin_busy(service, id, en);
272 return GPIO_RESULT_OK;
275 enum gpio_result dal_gpio_service_unlock(
276 struct gpio_service *service,
280 if (!service->busyness[id]) {
281 ASSERT_CRITICAL(false);
282 return GPIO_RESULT_OPEN_FAILED;
285 set_pin_free(service, id, en);
286 return GPIO_RESULT_OK;
289 enum gpio_result dal_gpio_service_open(
292 struct gpio_service *service = gpio->service;
293 enum gpio_id id = gpio->id;
294 uint32_t en = gpio->en;
295 enum gpio_mode mode = gpio->mode;
297 struct hw_gpio_pin **pin = &gpio->pin;
300 if (!service->busyness[id]) {
301 ASSERT_CRITICAL(false);
302 return GPIO_RESULT_OPEN_FAILED;
305 if (is_pin_busy(service, id, en)) {
306 ASSERT_CRITICAL(false);
307 return GPIO_RESULT_DEVICE_BUSY;
311 case GPIO_ID_DDC_DATA:
312 *pin = service->factory.funcs->get_ddc_pin(gpio);
313 service->factory.funcs->define_ddc_registers(*pin, en);
315 case GPIO_ID_DDC_CLOCK:
316 *pin = service->factory.funcs->get_ddc_pin(gpio);
317 service->factory.funcs->define_ddc_registers(*pin, en);
319 case GPIO_ID_GENERIC:
320 *pin = service->factory.funcs->get_generic_pin(gpio);
321 service->factory.funcs->define_generic_registers(*pin, en);
324 *pin = service->factory.funcs->get_hpd_pin(gpio);
325 service->factory.funcs->define_hpd_registers(*pin, en);
328 //TODO: gsl and sync support? create_sync and create_gsl are NULL
333 ASSERT_CRITICAL(false);
334 return GPIO_RESULT_NON_SPECIFIC_ERROR;
338 ASSERT_CRITICAL(false);
339 return GPIO_RESULT_NON_SPECIFIC_ERROR;
342 if (!(*pin)->funcs->open(*pin, mode)) {
343 ASSERT_CRITICAL(false);
344 dal_gpio_service_close(service, pin);
345 return GPIO_RESULT_OPEN_FAILED;
348 set_pin_busy(service, id, en);
349 return GPIO_RESULT_OK;
352 void dal_gpio_service_close(
353 struct gpio_service *service,
354 struct hw_gpio_pin **ptr)
356 struct hw_gpio_pin *pin;
359 ASSERT_CRITICAL(false);
366 set_pin_free(service, pin->id, pin->en);
368 pin->funcs->close(pin);
374 enum dc_irq_source dal_irq_get_source(
375 const struct gpio *irq)
377 enum gpio_id id = dal_gpio_get_id(irq);
381 return (enum dc_irq_source)(DC_IRQ_SOURCE_HPD1 +
382 dal_gpio_get_enum(irq));
383 case GPIO_ID_GPIO_PAD:
384 return (enum dc_irq_source)(DC_IRQ_SOURCE_GPIOPAD0 +
385 dal_gpio_get_enum(irq));
387 return DC_IRQ_SOURCE_INVALID;
391 enum dc_irq_source dal_irq_get_rx_source(
392 const struct gpio *irq)
394 enum gpio_id id = dal_gpio_get_id(irq);
398 return (enum dc_irq_source)(DC_IRQ_SOURCE_HPD1RX +
399 dal_gpio_get_enum(irq));
401 return DC_IRQ_SOURCE_INVALID;
405 enum gpio_result dal_irq_setup_hpd_filter(
407 struct gpio_hpd_config *config)
409 struct gpio_config_data config_data;
412 return GPIO_RESULT_INVALID_DATA;
414 config_data.type = GPIO_CONFIG_TYPE_HPD;
415 config_data.config.hpd = *config;
417 return dal_gpio_set_config(irq, &config_data);
422 * Creation and destruction
425 struct gpio *dal_gpio_create_irq(
426 struct gpio_service *service,
434 case GPIO_ID_GPIO_PAD:
438 ASSERT_CRITICAL(false);
442 irq = dal_gpio_create(
443 service, id, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
448 ASSERT_CRITICAL(false);
452 void dal_gpio_destroy_irq(
456 ASSERT_CRITICAL(false);
460 dal_gpio_destroy(irq);
466 struct ddc *dal_gpio_create_ddc(
467 struct gpio_service *service,
470 struct gpio_ddc_hw_info *info)
476 if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en))
479 ddc = kzalloc(sizeof(struct ddc), GFP_KERNEL);
486 ddc->pin_data = dal_gpio_create(
487 service, GPIO_ID_DDC_DATA, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
489 if (!ddc->pin_data) {
494 ddc->pin_clock = dal_gpio_create(
495 service, GPIO_ID_DDC_CLOCK, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
497 if (!ddc->pin_clock) {
502 ddc->hw_info = *info;
504 ddc->ctx = service->ctx;
509 dal_gpio_destroy(&ddc->pin_data);
517 void dal_gpio_destroy_ddc(
526 dal_gpio_destroy(&(*ddc)->pin_data);
527 dal_gpio_destroy(&(*ddc)->pin_clock);
533 enum gpio_result dal_ddc_open(
536 enum gpio_ddc_config_type config_type)
538 enum gpio_result result;
540 struct gpio_config_data config_data;
541 struct hw_gpio *hw_data;
542 struct hw_gpio *hw_clock;
544 result = dal_gpio_open_ex(ddc->pin_data, mode);
546 if (result != GPIO_RESULT_OK) {
551 result = dal_gpio_open_ex(ddc->pin_clock, mode);
553 if (result != GPIO_RESULT_OK) {
558 /* DDC clock and data pins should belong
559 * to the same DDC block id,
560 * we use the data pin to set the pad mode. */
562 if (mode == GPIO_MODE_INPUT)
563 /* this is from detect_sink_type,
564 * we need extra delay there */
565 config_data.type = GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE;
567 config_data.type = GPIO_CONFIG_TYPE_DDC;
569 config_data.config.ddc.type = config_type;
571 hw_data = FROM_HW_GPIO_PIN(ddc->pin_data->pin);
572 hw_clock = FROM_HW_GPIO_PIN(ddc->pin_clock->pin);
574 config_data.config.ddc.data_en_bit_present = hw_data->store.en != 0;
575 config_data.config.ddc.clock_en_bit_present = hw_clock->store.en != 0;
577 result = dal_gpio_set_config(ddc->pin_data, &config_data);
579 if (result == GPIO_RESULT_OK)
584 dal_gpio_close(ddc->pin_clock);
587 dal_gpio_close(ddc->pin_data);
592 enum gpio_result dal_ddc_change_mode(
596 enum gpio_result result;
598 enum gpio_mode original_mode =
599 dal_gpio_get_mode(ddc->pin_data);
601 result = dal_gpio_change_mode(ddc->pin_data, mode);
603 /* [anaumov] DAL2 code returns GPIO_RESULT_NON_SPECIFIC_ERROR
604 * in case of failures;
605 * set_mode() is so that, in case of failure,
606 * we must explicitly set original mode */
608 if (result != GPIO_RESULT_OK)
611 result = dal_gpio_change_mode(ddc->pin_clock, mode);
613 if (result == GPIO_RESULT_OK)
616 dal_gpio_change_mode(ddc->pin_clock, original_mode);
619 dal_gpio_change_mode(ddc->pin_data, original_mode);
624 enum gpio_ddc_line dal_ddc_get_line(
625 const struct ddc *ddc)
627 return (enum gpio_ddc_line)dal_gpio_get_enum(ddc->pin_data);
630 enum gpio_result dal_ddc_set_config(
632 enum gpio_ddc_config_type config_type)
634 struct gpio_config_data config_data;
636 config_data.type = GPIO_CONFIG_TYPE_DDC;
638 config_data.config.ddc.type = config_type;
639 config_data.config.ddc.data_en_bit_present = false;
640 config_data.config.ddc.clock_en_bit_present = false;
642 return dal_gpio_set_config(ddc->pin_data, &config_data);
649 dal_gpio_close(ddc->pin_clock);
650 dal_gpio_close(ddc->pin_data);