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 = kzalloc(number_of_uints * sizeof(uint32_t),
110 slot[index_of_uint] = 0;
113 } while (index_of_uint < number_of_uints);
117 service->busyness[index_of_id] = slot;
120 } while (index_of_id < GPIO_ID_COUNT);
126 while (index_of_id) {
131 slot = service->busyness[index_of_id];
142 struct gpio *dal_gpio_service_create_irq(
143 struct gpio_service *service,
150 if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en)) {
151 ASSERT_CRITICAL(false);
155 return dal_gpio_create_irq(service, id, en);
158 void dal_gpio_service_destroy(
159 struct gpio_service **ptr)
166 /* free business storage */
168 uint32_t index_of_id = 0;
171 uint32_t *slot = (*ptr)->busyness[index_of_id];
176 } while (index_of_id < GPIO_ID_COUNT);
189 static bool is_pin_busy(
190 const struct gpio_service *service,
194 const uint32_t bits_per_uint = sizeof(uint32_t) << 3;
196 const uint32_t *slot = service->busyness[id] + (en / bits_per_uint);
198 return 0 != (*slot & (1 << (en % bits_per_uint)));
201 static void set_pin_busy(
202 struct gpio_service *service,
206 const uint32_t bits_per_uint = sizeof(uint32_t) << 3;
208 service->busyness[id][en / bits_per_uint] |=
209 (1 << (en % bits_per_uint));
212 static void set_pin_free(
213 struct gpio_service *service,
217 const uint32_t bits_per_uint = sizeof(uint32_t) << 3;
219 service->busyness[id][en / bits_per_uint] &=
220 ~(1 << (en % bits_per_uint));
223 enum gpio_result dal_gpio_service_open(
224 struct gpio_service *service,
228 struct hw_gpio_pin **ptr)
230 struct hw_gpio_pin *pin;
232 if (!service->busyness[id]) {
233 ASSERT_CRITICAL(false);
234 return GPIO_RESULT_OPEN_FAILED;
237 if (is_pin_busy(service, id, en)) {
238 ASSERT_CRITICAL(false);
239 return GPIO_RESULT_DEVICE_BUSY;
243 case GPIO_ID_DDC_DATA:
244 pin = service->factory.funcs->create_ddc_data(
245 service->ctx, id, en);
246 service->factory.funcs->define_ddc_registers(pin, en);
248 case GPIO_ID_DDC_CLOCK:
249 pin = service->factory.funcs->create_ddc_clock(
250 service->ctx, id, en);
251 service->factory.funcs->define_ddc_registers(pin, en);
253 case GPIO_ID_GENERIC:
254 pin = service->factory.funcs->create_generic(
255 service->ctx, id, en);
258 pin = service->factory.funcs->create_hpd(
259 service->ctx, id, en);
260 service->factory.funcs->define_hpd_registers(pin, en);
263 pin = service->factory.funcs->create_sync(
264 service->ctx, id, en);
267 pin = service->factory.funcs->create_gsl(
268 service->ctx, id, en);
271 ASSERT_CRITICAL(false);
272 return GPIO_RESULT_NON_SPECIFIC_ERROR;
276 ASSERT_CRITICAL(false);
277 return GPIO_RESULT_NON_SPECIFIC_ERROR;
280 if (!pin->funcs->open(pin, mode)) {
281 ASSERT_CRITICAL(false);
282 dal_gpio_service_close(service, &pin);
283 return GPIO_RESULT_OPEN_FAILED;
286 set_pin_busy(service, id, en);
288 return GPIO_RESULT_OK;
291 void dal_gpio_service_close(
292 struct gpio_service *service,
293 struct hw_gpio_pin **ptr)
295 struct hw_gpio_pin *pin;
298 ASSERT_CRITICAL(false);
305 set_pin_free(service, pin->id, pin->en);
307 pin->funcs->close(pin);
309 pin->funcs->destroy(ptr);
314 enum dc_irq_source dal_irq_get_source(
315 const struct gpio *irq)
317 enum gpio_id id = dal_gpio_get_id(irq);
321 return (enum dc_irq_source)(DC_IRQ_SOURCE_HPD1 +
322 dal_gpio_get_enum(irq));
323 case GPIO_ID_GPIO_PAD:
324 return (enum dc_irq_source)(DC_IRQ_SOURCE_GPIOPAD0 +
325 dal_gpio_get_enum(irq));
327 return DC_IRQ_SOURCE_INVALID;
331 enum dc_irq_source dal_irq_get_rx_source(
332 const struct gpio *irq)
334 enum gpio_id id = dal_gpio_get_id(irq);
338 return (enum dc_irq_source)(DC_IRQ_SOURCE_HPD1RX +
339 dal_gpio_get_enum(irq));
341 return DC_IRQ_SOURCE_INVALID;
345 enum gpio_result dal_irq_setup_hpd_filter(
347 struct gpio_hpd_config *config)
349 struct gpio_config_data config_data;
352 return GPIO_RESULT_INVALID_DATA;
354 config_data.type = GPIO_CONFIG_TYPE_HPD;
355 config_data.config.hpd = *config;
357 return dal_gpio_set_config(irq, &config_data);
362 * Creation and destruction
365 struct gpio *dal_gpio_create_irq(
366 struct gpio_service *service,
374 case GPIO_ID_GPIO_PAD:
377 ASSERT_CRITICAL(false);
381 irq = dal_gpio_create(
382 service, id, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
387 ASSERT_CRITICAL(false);
391 void dal_gpio_destroy_irq(
395 ASSERT_CRITICAL(false);
399 dal_gpio_close(*irq);
400 dal_gpio_destroy(irq);
406 struct ddc *dal_gpio_create_ddc(
407 struct gpio_service *service,
410 struct gpio_ddc_hw_info *info)
416 if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en))
419 ddc = kzalloc(sizeof(struct ddc), GFP_KERNEL);
426 ddc->pin_data = dal_gpio_create(
427 service, GPIO_ID_DDC_DATA, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
429 if (!ddc->pin_data) {
434 ddc->pin_clock = dal_gpio_create(
435 service, GPIO_ID_DDC_CLOCK, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
437 if (!ddc->pin_clock) {
442 ddc->hw_info = *info;
444 ddc->ctx = service->ctx;
449 dal_gpio_destroy(&ddc->pin_data);
457 void dal_gpio_destroy_ddc(
466 dal_gpio_destroy(&(*ddc)->pin_data);
467 dal_gpio_destroy(&(*ddc)->pin_clock);
473 enum gpio_result dal_ddc_open(
476 enum gpio_ddc_config_type config_type)
478 enum gpio_result result;
480 struct gpio_config_data config_data;
481 struct hw_gpio *hw_data;
482 struct hw_gpio *hw_clock;
484 result = dal_gpio_open_ex(ddc->pin_data, mode);
486 if (result != GPIO_RESULT_OK) {
491 result = dal_gpio_open_ex(ddc->pin_clock, mode);
493 if (result != GPIO_RESULT_OK) {
498 /* DDC clock and data pins should belong
499 * to the same DDC block id,
500 * we use the data pin to set the pad mode. */
502 if (mode == GPIO_MODE_INPUT)
503 /* this is from detect_sink_type,
504 * we need extra delay there */
505 config_data.type = GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE;
507 config_data.type = GPIO_CONFIG_TYPE_DDC;
509 config_data.config.ddc.type = config_type;
511 hw_data = FROM_HW_GPIO_PIN(ddc->pin_data->pin);
512 hw_clock = FROM_HW_GPIO_PIN(ddc->pin_clock->pin);
514 config_data.config.ddc.data_en_bit_present = hw_data->store.en != 0;
515 config_data.config.ddc.clock_en_bit_present = hw_clock->store.en != 0;
517 result = dal_gpio_set_config(ddc->pin_data, &config_data);
519 if (result == GPIO_RESULT_OK)
524 dal_gpio_close(ddc->pin_clock);
527 dal_gpio_close(ddc->pin_data);
532 enum gpio_result dal_ddc_change_mode(
536 enum gpio_result result;
538 enum gpio_mode original_mode =
539 dal_gpio_get_mode(ddc->pin_data);
541 result = dal_gpio_change_mode(ddc->pin_data, mode);
543 /* [anaumov] DAL2 code returns GPIO_RESULT_NON_SPECIFIC_ERROR
544 * in case of failures;
545 * set_mode() is so that, in case of failure,
546 * we must explicitly set original mode */
548 if (result != GPIO_RESULT_OK)
551 result = dal_gpio_change_mode(ddc->pin_clock, mode);
553 if (result == GPIO_RESULT_OK)
556 dal_gpio_change_mode(ddc->pin_clock, original_mode);
559 dal_gpio_change_mode(ddc->pin_data, original_mode);
564 enum gpio_ddc_line dal_ddc_get_line(
565 const struct ddc *ddc)
567 return (enum gpio_ddc_line)dal_gpio_get_enum(ddc->pin_data);
570 enum gpio_result dal_ddc_set_config(
572 enum gpio_ddc_config_type config_type)
574 struct gpio_config_data config_data;
576 config_data.type = GPIO_CONFIG_TYPE_DDC;
578 config_data.config.ddc.type = config_type;
579 config_data.config.ddc.data_en_bit_present = false;
580 config_data.config.ddc.clock_en_bit_present = false;
582 return dal_gpio_set_config(ddc->pin_data, &config_data);
588 dal_gpio_close(ddc->pin_clock);
589 dal_gpio_close(ddc->pin_data);