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 <linux/slab.h>
32 #include "dm_services.h"
33 #include "include/gpio_interface.h"
34 #include "include/gpio_service_interface.h"
35 #include "hw_translate.h"
36 #include "hw_factory.h"
42 #include "gpio_service.h"
45 * Post-requisites: headers required by this unit
55 struct gpio_service
*dal_gpio_service_create(
56 enum dce_version dce_version_major
,
57 enum dce_version dce_version_minor
,
58 struct dc_context
*ctx
)
60 struct gpio_service
*service
;
63 service
= kzalloc(sizeof(struct gpio_service
), GFP_KERNEL
);
70 if (!dal_hw_translate_init(&service
->translate
, dce_version_major
,
76 if (!dal_hw_factory_init(&service
->factory
, dce_version_major
,
82 /* allocate and initialize busyness storage */
88 uint32_t number_of_bits
=
89 service
->factory
.number_of_pins
[index_of_id
];
93 service
->busyness
[index_of_id
] =
94 kcalloc(number_of_bits
, sizeof(char),
97 if (!service
->busyness
[index_of_id
]) {
103 service
->busyness
[index_of_id
][i
] = 0;
105 } while (i
< number_of_bits
);
107 service
->busyness
[index_of_id
] = NULL
;
111 } while (index_of_id
< GPIO_ID_COUNT
);
117 while (index_of_id
) {
119 kfree(service
->busyness
[index_of_id
]);
128 struct gpio
*dal_gpio_service_create_irq(
129 struct gpio_service
*service
,
136 if (!service
->translate
.funcs
->offset_to_id(offset
, mask
, &id
, &en
)) {
137 ASSERT_CRITICAL(false);
141 return dal_gpio_create_irq(service
, id
, en
);
144 struct gpio
*dal_gpio_service_create_generic_mux(
145 struct gpio_service
*service
,
151 struct gpio
*generic
;
153 if (!service
->translate
.funcs
->offset_to_id(offset
, mask
, &id
, &en
)) {
154 ASSERT_CRITICAL(false);
158 generic
= dal_gpio_create(
159 service
, id
, en
, GPIO_PIN_OUTPUT_STATE_DEFAULT
);
164 void dal_gpio_destroy_generic_mux(
168 ASSERT_CRITICAL(false);
172 dal_gpio_destroy(mux
);
178 struct gpio_pin_info
dal_gpio_get_generic_pin_info(
179 struct gpio_service
*service
,
183 struct gpio_pin_info pin
;
185 if (service
->translate
.funcs
->id_to_offset
) {
186 service
->translate
.funcs
->id_to_offset(id
, en
, &pin
);
188 pin
.mask
= 0xFFFFFFFF;
189 pin
.offset
= 0xFFFFFFFF;
195 void dal_gpio_service_destroy(
196 struct gpio_service
**ptr
)
203 /* free business storage */
205 uint32_t index_of_id
= 0;
208 kfree((*ptr
)->busyness
[index_of_id
]);
211 } while (index_of_id
< GPIO_ID_COUNT
);
219 enum gpio_result
dal_mux_setup_config(
221 struct gpio_generic_mux_config
*config
)
223 struct gpio_config_data config_data
;
226 return GPIO_RESULT_INVALID_DATA
;
228 config_data
.config
.generic_mux
= *config
;
229 config_data
.type
= GPIO_CONFIG_TYPE_GENERIC_MUX
;
231 return dal_gpio_set_config(mux
, &config_data
);
239 static bool is_pin_busy(
240 const struct gpio_service
*service
,
244 return service
->busyness
[id
][en
];
247 static void set_pin_busy(
248 struct gpio_service
*service
,
252 service
->busyness
[id
][en
] = true;
255 static void set_pin_free(
256 struct gpio_service
*service
,
260 service
->busyness
[id
][en
] = false;
263 enum gpio_result
dal_gpio_service_lock(
264 struct gpio_service
*service
,
268 if (!service
->busyness
[id
]) {
269 ASSERT_CRITICAL(false);
270 return GPIO_RESULT_OPEN_FAILED
;
273 set_pin_busy(service
, id
, en
);
274 return GPIO_RESULT_OK
;
277 enum gpio_result
dal_gpio_service_unlock(
278 struct gpio_service
*service
,
282 if (!service
->busyness
[id
]) {
283 ASSERT_CRITICAL(false);
284 return GPIO_RESULT_OPEN_FAILED
;
287 set_pin_free(service
, id
, en
);
288 return GPIO_RESULT_OK
;
291 enum gpio_result
dal_gpio_service_open(
294 struct gpio_service
*service
= gpio
->service
;
295 enum gpio_id id
= gpio
->id
;
296 uint32_t en
= gpio
->en
;
297 enum gpio_mode mode
= gpio
->mode
;
299 struct hw_gpio_pin
**pin
= &gpio
->pin
;
302 if (!service
->busyness
[id
]) {
303 ASSERT_CRITICAL(false);
304 return GPIO_RESULT_OPEN_FAILED
;
307 if (is_pin_busy(service
, id
, en
)) {
308 ASSERT_CRITICAL(false);
309 return GPIO_RESULT_DEVICE_BUSY
;
313 case GPIO_ID_DDC_DATA
:
314 *pin
= service
->factory
.funcs
->get_ddc_pin(gpio
);
315 service
->factory
.funcs
->define_ddc_registers(*pin
, en
);
317 case GPIO_ID_DDC_CLOCK
:
318 *pin
= service
->factory
.funcs
->get_ddc_pin(gpio
);
319 service
->factory
.funcs
->define_ddc_registers(*pin
, en
);
321 case GPIO_ID_GENERIC
:
322 *pin
= service
->factory
.funcs
->get_generic_pin(gpio
);
323 service
->factory
.funcs
->define_generic_registers(*pin
, en
);
326 *pin
= service
->factory
.funcs
->get_hpd_pin(gpio
);
327 service
->factory
.funcs
->define_hpd_registers(*pin
, en
);
330 //TODO: gsl and sync support? create_sync and create_gsl are NULL
335 ASSERT_CRITICAL(false);
336 return GPIO_RESULT_NON_SPECIFIC_ERROR
;
340 ASSERT_CRITICAL(false);
341 return GPIO_RESULT_NON_SPECIFIC_ERROR
;
344 if (!(*pin
)->funcs
->open(*pin
, mode
)) {
345 ASSERT_CRITICAL(false);
346 dal_gpio_service_close(service
, pin
);
347 return GPIO_RESULT_OPEN_FAILED
;
350 set_pin_busy(service
, id
, en
);
351 return GPIO_RESULT_OK
;
354 void dal_gpio_service_close(
355 struct gpio_service
*service
,
356 struct hw_gpio_pin
**ptr
)
358 struct hw_gpio_pin
*pin
;
361 ASSERT_CRITICAL(false);
368 set_pin_free(service
, pin
->id
, pin
->en
);
370 pin
->funcs
->close(pin
);
376 enum dc_irq_source
dal_irq_get_source(
377 const struct gpio
*irq
)
379 enum gpio_id id
= dal_gpio_get_id(irq
);
383 return (enum dc_irq_source
)(DC_IRQ_SOURCE_HPD1
+
384 dal_gpio_get_enum(irq
));
385 case GPIO_ID_GPIO_PAD
:
386 return (enum dc_irq_source
)(DC_IRQ_SOURCE_GPIOPAD0
+
387 dal_gpio_get_enum(irq
));
389 return DC_IRQ_SOURCE_INVALID
;
393 enum dc_irq_source
dal_irq_get_rx_source(
394 const struct gpio
*irq
)
396 enum gpio_id id
= dal_gpio_get_id(irq
);
400 return (enum dc_irq_source
)(DC_IRQ_SOURCE_HPD1RX
+
401 dal_gpio_get_enum(irq
));
403 return DC_IRQ_SOURCE_INVALID
;
407 enum gpio_result
dal_irq_setup_hpd_filter(
409 struct gpio_hpd_config
*config
)
411 struct gpio_config_data config_data
;
414 return GPIO_RESULT_INVALID_DATA
;
416 config_data
.type
= GPIO_CONFIG_TYPE_HPD
;
417 config_data
.config
.hpd
= *config
;
419 return dal_gpio_set_config(irq
, &config_data
);
424 * Creation and destruction
427 struct gpio
*dal_gpio_create_irq(
428 struct gpio_service
*service
,
436 case GPIO_ID_GPIO_PAD
:
440 ASSERT_CRITICAL(false);
444 irq
= dal_gpio_create(
445 service
, id
, en
, GPIO_PIN_OUTPUT_STATE_DEFAULT
);
450 ASSERT_CRITICAL(false);
454 void dal_gpio_destroy_irq(
458 ASSERT_CRITICAL(false);
462 dal_gpio_destroy(irq
);
468 struct ddc
*dal_gpio_create_ddc(
469 struct gpio_service
*service
,
472 struct gpio_ddc_hw_info
*info
)
478 if (!service
->translate
.funcs
->offset_to_id(offset
, mask
, &id
, &en
))
481 ddc
= kzalloc(sizeof(struct ddc
), GFP_KERNEL
);
488 ddc
->pin_data
= dal_gpio_create(
489 service
, GPIO_ID_DDC_DATA
, en
, GPIO_PIN_OUTPUT_STATE_DEFAULT
);
491 if (!ddc
->pin_data
) {
496 ddc
->pin_clock
= dal_gpio_create(
497 service
, GPIO_ID_DDC_CLOCK
, en
, GPIO_PIN_OUTPUT_STATE_DEFAULT
);
499 if (!ddc
->pin_clock
) {
504 ddc
->hw_info
= *info
;
506 ddc
->ctx
= service
->ctx
;
511 dal_gpio_destroy(&ddc
->pin_data
);
519 void dal_gpio_destroy_ddc(
528 dal_gpio_destroy(&(*ddc
)->pin_data
);
529 dal_gpio_destroy(&(*ddc
)->pin_clock
);
535 enum gpio_result
dal_ddc_open(
538 enum gpio_ddc_config_type config_type
)
540 enum gpio_result result
;
542 struct gpio_config_data config_data
;
543 struct hw_gpio
*hw_data
;
544 struct hw_gpio
*hw_clock
;
546 result
= dal_gpio_open_ex(ddc
->pin_data
, mode
);
548 if (result
!= GPIO_RESULT_OK
) {
553 result
= dal_gpio_open_ex(ddc
->pin_clock
, mode
);
555 if (result
!= GPIO_RESULT_OK
) {
560 /* DDC clock and data pins should belong
561 * to the same DDC block id,
562 * we use the data pin to set the pad mode. */
564 if (mode
== GPIO_MODE_INPUT
)
565 /* this is from detect_sink_type,
566 * we need extra delay there */
567 config_data
.type
= GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE
;
569 config_data
.type
= GPIO_CONFIG_TYPE_DDC
;
571 config_data
.config
.ddc
.type
= config_type
;
573 hw_data
= FROM_HW_GPIO_PIN(ddc
->pin_data
->pin
);
574 hw_clock
= FROM_HW_GPIO_PIN(ddc
->pin_clock
->pin
);
576 config_data
.config
.ddc
.data_en_bit_present
= hw_data
->store
.en
!= 0;
577 config_data
.config
.ddc
.clock_en_bit_present
= hw_clock
->store
.en
!= 0;
579 result
= dal_gpio_set_config(ddc
->pin_data
, &config_data
);
581 if (result
== GPIO_RESULT_OK
)
586 dal_gpio_close(ddc
->pin_clock
);
589 dal_gpio_close(ddc
->pin_data
);
594 enum gpio_result
dal_ddc_change_mode(
598 enum gpio_result result
;
600 enum gpio_mode original_mode
=
601 dal_gpio_get_mode(ddc
->pin_data
);
603 result
= dal_gpio_change_mode(ddc
->pin_data
, mode
);
605 /* [anaumov] DAL2 code returns GPIO_RESULT_NON_SPECIFIC_ERROR
606 * in case of failures;
607 * set_mode() is so that, in case of failure,
608 * we must explicitly set original mode */
610 if (result
!= GPIO_RESULT_OK
)
613 result
= dal_gpio_change_mode(ddc
->pin_clock
, mode
);
615 if (result
== GPIO_RESULT_OK
)
618 dal_gpio_change_mode(ddc
->pin_clock
, original_mode
);
621 dal_gpio_change_mode(ddc
->pin_data
, original_mode
);
626 enum gpio_ddc_line
dal_ddc_get_line(
627 const struct ddc
*ddc
)
629 return (enum gpio_ddc_line
)dal_gpio_get_enum(ddc
->pin_data
);
632 enum gpio_result
dal_ddc_set_config(
634 enum gpio_ddc_config_type config_type
)
636 struct gpio_config_data config_data
;
638 config_data
.type
= GPIO_CONFIG_TYPE_DDC
;
640 config_data
.config
.ddc
.type
= config_type
;
641 config_data
.config
.ddc
.data_en_bit_present
= false;
642 config_data
.config
.ddc
.clock_en_bit_present
= false;
644 return dal_gpio_set_config(ddc
->pin_data
, &config_data
);
650 dal_gpio_close(ddc
->pin_clock
);
651 dal_gpio_close(ddc
->pin_data
);