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;
62 service = kzalloc(sizeof(struct gpio_service), GFP_KERNEL);
69 if (!dal_hw_translate_init(&service->translate, dce_version,
75 if (!dal_hw_factory_init(&service->factory, dce_version,
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:
379 ASSERT_CRITICAL(false);
383 irq = dal_gpio_create(
384 service, id, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
389 ASSERT_CRITICAL(false);
393 void dal_gpio_destroy_irq(
397 ASSERT_CRITICAL(false);
401 dal_gpio_close(*irq);
402 dal_gpio_destroy(irq);
408 struct ddc *dal_gpio_create_ddc(
409 struct gpio_service *service,
412 struct gpio_ddc_hw_info *info)
418 if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en))
421 ddc = kzalloc(sizeof(struct ddc), GFP_KERNEL);
428 ddc->pin_data = dal_gpio_create(
429 service, GPIO_ID_DDC_DATA, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
431 if (!ddc->pin_data) {
436 ddc->pin_clock = dal_gpio_create(
437 service, GPIO_ID_DDC_CLOCK, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
439 if (!ddc->pin_clock) {
444 ddc->hw_info = *info;
446 ddc->ctx = service->ctx;
451 dal_gpio_destroy(&ddc->pin_data);
459 void dal_gpio_destroy_ddc(
468 dal_gpio_destroy(&(*ddc)->pin_data);
469 dal_gpio_destroy(&(*ddc)->pin_clock);
475 enum gpio_result dal_ddc_open(
478 enum gpio_ddc_config_type config_type)
480 enum gpio_result result;
482 struct gpio_config_data config_data;
483 struct hw_gpio *hw_data;
484 struct hw_gpio *hw_clock;
486 result = dal_gpio_open_ex(ddc->pin_data, mode);
488 if (result != GPIO_RESULT_OK) {
493 result = dal_gpio_open_ex(ddc->pin_clock, mode);
495 if (result != GPIO_RESULT_OK) {
500 /* DDC clock and data pins should belong
501 * to the same DDC block id,
502 * we use the data pin to set the pad mode. */
504 if (mode == GPIO_MODE_INPUT)
505 /* this is from detect_sink_type,
506 * we need extra delay there */
507 config_data.type = GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE;
509 config_data.type = GPIO_CONFIG_TYPE_DDC;
511 config_data.config.ddc.type = config_type;
513 hw_data = FROM_HW_GPIO_PIN(ddc->pin_data->pin);
514 hw_clock = FROM_HW_GPIO_PIN(ddc->pin_clock->pin);
516 config_data.config.ddc.data_en_bit_present = hw_data->store.en != 0;
517 config_data.config.ddc.clock_en_bit_present = hw_clock->store.en != 0;
519 result = dal_gpio_set_config(ddc->pin_data, &config_data);
521 if (result == GPIO_RESULT_OK)
526 dal_gpio_close(ddc->pin_clock);
529 dal_gpio_close(ddc->pin_data);
534 enum gpio_result dal_ddc_change_mode(
538 enum gpio_result result;
540 enum gpio_mode original_mode =
541 dal_gpio_get_mode(ddc->pin_data);
543 result = dal_gpio_change_mode(ddc->pin_data, mode);
545 /* [anaumov] DAL2 code returns GPIO_RESULT_NON_SPECIFIC_ERROR
546 * in case of failures;
547 * set_mode() is so that, in case of failure,
548 * we must explicitly set original mode */
550 if (result != GPIO_RESULT_OK)
553 result = dal_gpio_change_mode(ddc->pin_clock, mode);
555 if (result == GPIO_RESULT_OK)
558 dal_gpio_change_mode(ddc->pin_clock, original_mode);
561 dal_gpio_change_mode(ddc->pin_data, original_mode);
566 enum gpio_ddc_line dal_ddc_get_line(
567 const struct ddc *ddc)
569 return (enum gpio_ddc_line)dal_gpio_get_enum(ddc->pin_data);
572 enum gpio_result dal_ddc_set_config(
574 enum gpio_ddc_config_type config_type)
576 struct gpio_config_data config_data;
578 config_data.type = GPIO_CONFIG_TYPE_DDC;
580 config_data.config.ddc.type = config_type;
581 config_data.config.ddc.data_en_bit_present = false;
582 config_data.config.ddc.clock_en_bit_present = false;
584 return dal_gpio_set_config(ddc->pin_data, &config_data);
590 dal_gpio_close(ddc->pin_clock);
591 dal_gpio_close(ddc->pin_data);