Linux Audio

Check our new training course

Loading...
v4.17
  1/*
  2 * Copyright 2012-15 Advanced Micro Devices, Inc.
  3 *
  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:
 10 *
 11 * The above copyright notice and this permission notice shall be included in
 12 * all copies or substantial portions of the Software.
 13 *
 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.
 21 *
 22 * Authors: AMD
 23 *
 24 */
 25
 26/*
 27 * Pre-requisites: headers required by header of this unit
 28 */
 29
 
 
 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"
 35
 36/*
 37 * Header of this unit
 38 */
 39
 40#include "gpio_service.h"
 41
 42/*
 43 * Post-requisites: headers required by this unit
 44 */
 45
 46#include "hw_gpio.h"
 47
 48/*
 49 * @brief
 50 * Public API.
 51 */
 52
 53struct 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)
 57{
 58	struct gpio_service *service;
 59
 60	uint32_t index_of_id;
 61
 62	service = kzalloc(sizeof(struct gpio_service), GFP_KERNEL);
 63
 64	if (!service) {
 65		BREAK_TO_DEBUGGER();
 66		return NULL;
 67	}
 68
 69	if (!dal_hw_translate_init(&service->translate, dce_version_major,
 70			dce_version_minor)) {
 71		BREAK_TO_DEBUGGER();
 72		goto failure_1;
 73	}
 74
 75	if (!dal_hw_factory_init(&service->factory, dce_version_major,
 76			dce_version_minor)) {
 77		BREAK_TO_DEBUGGER();
 78		goto failure_1;
 79	}
 80
 81	/* allocate and initialize business storage */
 82	{
 83		const uint32_t bits_per_uint = sizeof(uint32_t) << 3;
 84
 85		index_of_id = 0;
 86		service->ctx = ctx;
 87
 88		do {
 89			uint32_t number_of_bits =
 90				service->factory.number_of_pins[index_of_id];
 
 91
 92			uint32_t number_of_uints =
 93				(number_of_bits + bits_per_uint - 1) /
 94				bits_per_uint;
 95
 96			uint32_t *slot;
 97
 98			if (number_of_bits) {
 99				uint32_t index_of_uint = 0;
100
101				slot = kzalloc(number_of_uints * sizeof(uint32_t),
102					       GFP_KERNEL);
103
104				if (!slot) {
105					BREAK_TO_DEBUGGER();
106					goto failure_2;
107				}
108
109				do {
110					slot[index_of_uint] = 0;
111
112					++index_of_uint;
113				} while (index_of_uint < number_of_uints);
114			} else
115				slot = NULL;
116
117			service->busyness[index_of_id] = slot;
118
119			++index_of_id;
120		} while (index_of_id < GPIO_ID_COUNT);
121	}
122
123	return service;
124
125failure_2:
126	while (index_of_id) {
127		uint32_t *slot;
128
129		--index_of_id;
130
131		slot = service->busyness[index_of_id];
132
133		kfree(slot);
134	}
135
136failure_1:
137	kfree(service);
138
139	return NULL;
140}
141
142struct gpio *dal_gpio_service_create_irq(
143	struct gpio_service *service,
144	uint32_t offset,
145	uint32_t mask)
146{
147	enum gpio_id id;
148	uint32_t en;
149
150	if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en)) {
151		ASSERT_CRITICAL(false);
152		return NULL;
153	}
154
155	return dal_gpio_create_irq(service, id, en);
156}
157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158void dal_gpio_service_destroy(
159	struct gpio_service **ptr)
160{
161	if (!ptr || !*ptr) {
162		BREAK_TO_DEBUGGER();
163		return;
164	}
165
166	/* free business storage */
167	{
168		uint32_t index_of_id = 0;
169
170		do {
171			uint32_t *slot = (*ptr)->busyness[index_of_id];
172
173			kfree(slot);
174
175			++index_of_id;
176		} while (index_of_id < GPIO_ID_COUNT);
177	}
178
179	kfree(*ptr);
180
181	*ptr = NULL;
182}
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184/*
185 * @brief
186 * Private API.
187 */
188
189static bool is_pin_busy(
190	const struct gpio_service *service,
191	enum gpio_id id,
192	uint32_t en)
193{
194	const uint32_t bits_per_uint = sizeof(uint32_t) << 3;
195
196	const uint32_t *slot = service->busyness[id] + (en / bits_per_uint);
197
198	return 0 != (*slot & (1 << (en % bits_per_uint)));
199}
200
201static void set_pin_busy(
202	struct gpio_service *service,
203	enum gpio_id id,
204	uint32_t en)
205{
206	const uint32_t bits_per_uint = sizeof(uint32_t) << 3;
207
208	service->busyness[id][en / bits_per_uint] |=
209		(1 << (en % bits_per_uint));
210}
211
212static void set_pin_free(
213	struct gpio_service *service,
214	enum gpio_id id,
215	uint32_t en)
216{
217	const uint32_t bits_per_uint = sizeof(uint32_t) << 3;
 
 
 
 
 
 
 
 
 
 
 
218
219	service->busyness[id][en / bits_per_uint] &=
220		~(1 << (en % bits_per_uint));
221}
222
223enum gpio_result dal_gpio_service_open(
224	struct gpio_service *service,
225	enum gpio_id id,
226	uint32_t en,
227	enum gpio_mode mode,
228	struct hw_gpio_pin **ptr)
229{
230	struct hw_gpio_pin *pin;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
232	if (!service->busyness[id]) {
233		ASSERT_CRITICAL(false);
234		return GPIO_RESULT_OPEN_FAILED;
235	}
236
237	if (is_pin_busy(service, id, en)) {
238		ASSERT_CRITICAL(false);
239		return GPIO_RESULT_DEVICE_BUSY;
240	}
241
242	switch (id) {
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);
247	break;
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);
252	break;
253	case GPIO_ID_GENERIC:
254		pin = service->factory.funcs->create_generic(
255			service->ctx, id, en);
256	break;
257	case GPIO_ID_HPD:
258		pin = service->factory.funcs->create_hpd(
259			service->ctx, id, en);
260		service->factory.funcs->define_hpd_registers(pin, en);
261	break;
 
 
262	case GPIO_ID_SYNC:
263		pin = service->factory.funcs->create_sync(
264			service->ctx, id, en);
265	break;
266	case GPIO_ID_GSL:
267		pin = service->factory.funcs->create_gsl(
268			service->ctx, id, en);
269	break;
270	default:
271		ASSERT_CRITICAL(false);
272		return GPIO_RESULT_NON_SPECIFIC_ERROR;
273	}
274
275	if (!pin) {
276		ASSERT_CRITICAL(false);
277		return GPIO_RESULT_NON_SPECIFIC_ERROR;
278	}
279
280	if (!pin->funcs->open(pin, mode)) {
281		ASSERT_CRITICAL(false);
282		dal_gpio_service_close(service, &pin);
283		return GPIO_RESULT_OPEN_FAILED;
284	}
285
286	set_pin_busy(service, id, en);
287	*ptr = pin;
288	return GPIO_RESULT_OK;
289}
290
291void dal_gpio_service_close(
292	struct gpio_service *service,
293	struct hw_gpio_pin **ptr)
294{
295	struct hw_gpio_pin *pin;
296
297	if (!ptr) {
298		ASSERT_CRITICAL(false);
299		return;
300	}
301
302	pin = *ptr;
303
304	if (pin) {
305		set_pin_free(service, pin->id, pin->en);
306
307		pin->funcs->close(pin);
308
309		pin->funcs->destroy(ptr);
310	}
311}
312
313
314enum dc_irq_source dal_irq_get_source(
315	const struct gpio *irq)
316{
317	enum gpio_id id = dal_gpio_get_id(irq);
318
319	switch (id) {
320	case GPIO_ID_HPD:
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));
326	default:
327		return DC_IRQ_SOURCE_INVALID;
328	}
329}
330
331enum dc_irq_source dal_irq_get_rx_source(
332	const struct gpio *irq)
333{
334	enum gpio_id id = dal_gpio_get_id(irq);
335
336	switch (id) {
337	case GPIO_ID_HPD:
338		return (enum dc_irq_source)(DC_IRQ_SOURCE_HPD1RX +
339			dal_gpio_get_enum(irq));
340	default:
341		return DC_IRQ_SOURCE_INVALID;
342	}
343}
344
345enum gpio_result dal_irq_setup_hpd_filter(
346	struct gpio *irq,
347	struct gpio_hpd_config *config)
348{
349	struct gpio_config_data config_data;
350
351	if (!config)
352		return GPIO_RESULT_INVALID_DATA;
353
354	config_data.type = GPIO_CONFIG_TYPE_HPD;
355	config_data.config.hpd = *config;
356
357	return dal_gpio_set_config(irq, &config_data);
358}
359
360/*
361 * @brief
362 * Creation and destruction
363 */
364
365struct gpio *dal_gpio_create_irq(
366	struct gpio_service *service,
367	enum gpio_id id,
368	uint32_t en)
369{
370	struct gpio *irq;
371
372	switch (id) {
373	case GPIO_ID_HPD:
374	case GPIO_ID_GPIO_PAD:
375	break;
376	default:
 
377		ASSERT_CRITICAL(false);
378		return NULL;
379	}
380
381	irq = dal_gpio_create(
382		service, id, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
383
384	if (irq)
385		return irq;
386
387	ASSERT_CRITICAL(false);
388	return NULL;
389}
390
391void dal_gpio_destroy_irq(
392	struct gpio **irq)
393{
394	if (!irq || !*irq) {
395		ASSERT_CRITICAL(false);
396		return;
397	}
398
399	dal_gpio_close(*irq);
400	dal_gpio_destroy(irq);
401	kfree(*irq);
402
403	*irq = NULL;
404}
405
406struct ddc *dal_gpio_create_ddc(
407	struct gpio_service *service,
408	uint32_t offset,
409	uint32_t mask,
410	struct gpio_ddc_hw_info *info)
411{
412	enum gpio_id id;
413	uint32_t en;
414	struct ddc *ddc;
415
416	if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en))
417		return NULL;
418
419	ddc = kzalloc(sizeof(struct ddc), GFP_KERNEL);
420
421	if (!ddc) {
422		BREAK_TO_DEBUGGER();
423		return NULL;
424	}
425
426	ddc->pin_data = dal_gpio_create(
427		service, GPIO_ID_DDC_DATA, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
428
429	if (!ddc->pin_data) {
430		BREAK_TO_DEBUGGER();
431		goto failure_1;
432	}
433
434	ddc->pin_clock = dal_gpio_create(
435		service, GPIO_ID_DDC_CLOCK, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
436
437	if (!ddc->pin_clock) {
438		BREAK_TO_DEBUGGER();
439		goto failure_2;
440	}
441
442	ddc->hw_info = *info;
443
444	ddc->ctx = service->ctx;
445
446	return ddc;
447
448failure_2:
449	dal_gpio_destroy(&ddc->pin_data);
450
451failure_1:
452	kfree(ddc);
453
454	return NULL;
455}
456
457void dal_gpio_destroy_ddc(
458	struct ddc **ddc)
459{
460	if (!ddc || !*ddc) {
461		BREAK_TO_DEBUGGER();
462		return;
463	}
464
465	dal_ddc_close(*ddc);
466	dal_gpio_destroy(&(*ddc)->pin_data);
467	dal_gpio_destroy(&(*ddc)->pin_clock);
468	kfree(*ddc);
469
470	*ddc = NULL;
471}
472
473enum gpio_result dal_ddc_open(
474	struct ddc *ddc,
475	enum gpio_mode mode,
476	enum gpio_ddc_config_type config_type)
477{
478	enum gpio_result result;
479
480	struct gpio_config_data config_data;
481	struct hw_gpio *hw_data;
482	struct hw_gpio *hw_clock;
483
484	result = dal_gpio_open_ex(ddc->pin_data, mode);
485
486	if (result != GPIO_RESULT_OK) {
487		BREAK_TO_DEBUGGER();
488		return result;
489	}
490
491	result = dal_gpio_open_ex(ddc->pin_clock, mode);
492
493	if (result != GPIO_RESULT_OK) {
494		BREAK_TO_DEBUGGER();
495		goto failure;
496	}
497
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. */
501
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;
506	else
507		config_data.type = GPIO_CONFIG_TYPE_DDC;
508
509	config_data.config.ddc.type = config_type;
510
511	hw_data = FROM_HW_GPIO_PIN(ddc->pin_data->pin);
512	hw_clock = FROM_HW_GPIO_PIN(ddc->pin_clock->pin);
513
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;
516
517	result = dal_gpio_set_config(ddc->pin_data, &config_data);
518
519	if (result == GPIO_RESULT_OK)
520		return result;
521
522	BREAK_TO_DEBUGGER();
523
524	dal_gpio_close(ddc->pin_clock);
525
526failure:
527	dal_gpio_close(ddc->pin_data);
528
529	return result;
530}
531
532enum gpio_result dal_ddc_change_mode(
533	struct ddc *ddc,
534	enum gpio_mode mode)
535{
536	enum gpio_result result;
537
538	enum gpio_mode original_mode =
539		dal_gpio_get_mode(ddc->pin_data);
540
541	result = dal_gpio_change_mode(ddc->pin_data, mode);
542
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 */
547
548	if (result != GPIO_RESULT_OK)
549		goto failure;
550
551	result = dal_gpio_change_mode(ddc->pin_clock, mode);
552
553	if (result == GPIO_RESULT_OK)
554		return result;
555
556	dal_gpio_change_mode(ddc->pin_clock, original_mode);
557
558failure:
559	dal_gpio_change_mode(ddc->pin_data, original_mode);
560
561	return result;
562}
563
564enum gpio_ddc_line dal_ddc_get_line(
565	const struct ddc *ddc)
566{
567	return (enum gpio_ddc_line)dal_gpio_get_enum(ddc->pin_data);
568}
569
570enum gpio_result dal_ddc_set_config(
571	struct ddc *ddc,
572	enum gpio_ddc_config_type config_type)
573{
574	struct gpio_config_data config_data;
575
576	config_data.type = GPIO_CONFIG_TYPE_DDC;
577
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;
581
582	return dal_gpio_set_config(ddc->pin_data, &config_data);
583}
584
585void dal_ddc_close(
586	struct ddc *ddc)
587{
588	dal_gpio_close(ddc->pin_clock);
589	dal_gpio_close(ddc->pin_data);
590}
591
v5.14.15
  1/*
  2 * Copyright 2012-15 Advanced Micro Devices, Inc.
  3 *
  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:
 10 *
 11 * The above copyright notice and this permission notice shall be included in
 12 * all copies or substantial portions of the Software.
 13 *
 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.
 21 *
 22 * Authors: AMD
 23 *
 24 */
 25
 26/*
 27 * Pre-requisites: headers required by header of this unit
 28 */
 29
 30#include <linux/slab.h>
 31
 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"
 37
 38/*
 39 * Header of this unit
 40 */
 41
 42#include "gpio_service.h"
 43
 44/*
 45 * Post-requisites: headers required by this unit
 46 */
 47
 48#include "hw_gpio.h"
 49
 50/*
 51 * @brief
 52 * Public API.
 53 */
 54
 55struct gpio_service *dal_gpio_service_create(
 56	enum dce_version dce_version,
 57	enum dce_environment dce_environment,
 58	struct dc_context *ctx)
 59{
 60	struct gpio_service *service;
 
 61	uint32_t index_of_id;
 62
 63	service = kzalloc(sizeof(struct gpio_service), GFP_KERNEL);
 64
 65	if (!service) {
 66		BREAK_TO_DEBUGGER();
 67		return NULL;
 68	}
 69
 70	if (!dal_hw_translate_init(&service->translate, dce_version,
 71			dce_environment)) {
 72		BREAK_TO_DEBUGGER();
 73		goto failure_1;
 74	}
 75
 76	if (!dal_hw_factory_init(&service->factory, dce_version,
 77			dce_environment)) {
 78		BREAK_TO_DEBUGGER();
 79		goto failure_1;
 80	}
 81
 82	/* allocate and initialize busyness storage */
 83	{
 
 
 84		index_of_id = 0;
 85		service->ctx = ctx;
 86
 87		do {
 88			uint32_t number_of_bits =
 89				service->factory.number_of_pins[index_of_id];
 90			uint32_t i = 0;
 91
 92			if (number_of_bits)  {
 93				service->busyness[index_of_id] =
 94					kcalloc(number_of_bits, sizeof(char),
 95						GFP_KERNEL);
 
 
 
 
 
 
 
 96
 97				if (!service->busyness[index_of_id]) {
 98					BREAK_TO_DEBUGGER();
 99					goto failure_2;
100				}
101
102				do {
103					service->busyness[index_of_id][i] = 0;
104					++i;
105				} while (i < number_of_bits);
106			} else {
107				service->busyness[index_of_id] = NULL;
108			}
 
 
109
110			++index_of_id;
111		} while (index_of_id < GPIO_ID_COUNT);
112	}
113
114	return service;
115
116failure_2:
117	while (index_of_id) {
 
 
118		--index_of_id;
119		kfree(service->busyness[index_of_id]);
 
 
 
120	}
121
122failure_1:
123	kfree(service);
124
125	return NULL;
126}
127
128struct gpio *dal_gpio_service_create_irq(
129	struct gpio_service *service,
130	uint32_t offset,
131	uint32_t mask)
132{
133	enum gpio_id id;
134	uint32_t en;
135
136	if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en)) {
137		ASSERT_CRITICAL(false);
138		return NULL;
139	}
140
141	return dal_gpio_create_irq(service, id, en);
142}
143
144struct gpio *dal_gpio_service_create_generic_mux(
145	struct gpio_service *service,
146	uint32_t offset,
147	uint32_t mask)
148{
149	enum gpio_id id;
150	uint32_t en;
151	struct gpio *generic;
152
153	if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en)) {
154		ASSERT_CRITICAL(false);
155		return NULL;
156	}
157
158	generic = dal_gpio_create(
159		service, id, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
160
161	return generic;
162}
163
164void dal_gpio_destroy_generic_mux(
165	struct gpio **mux)
166{
167	if (!mux || !*mux) {
168		ASSERT_CRITICAL(false);
169		return;
170	}
171
172	dal_gpio_destroy(mux);
173	kfree(*mux);
174
175	*mux = NULL;
176}
177
178struct gpio_pin_info dal_gpio_get_generic_pin_info(
179	struct gpio_service *service,
180	enum gpio_id id,
181	uint32_t en)
182{
183	struct gpio_pin_info pin;
184
185	if (service->translate.funcs->id_to_offset) {
186		service->translate.funcs->id_to_offset(id, en, &pin);
187	} else {
188		pin.mask = 0xFFFFFFFF;
189		pin.offset = 0xFFFFFFFF;
190	}
191
192	return pin;
193}
194
195void dal_gpio_service_destroy(
196	struct gpio_service **ptr)
197{
198	if (!ptr || !*ptr) {
199		BREAK_TO_DEBUGGER();
200		return;
201	}
202
203	/* free business storage */
204	{
205		uint32_t index_of_id = 0;
206
207		do {
208			kfree((*ptr)->busyness[index_of_id]);
 
 
209
210			++index_of_id;
211		} while (index_of_id < GPIO_ID_COUNT);
212	}
213
214	kfree(*ptr);
215
216	*ptr = NULL;
217}
218
219enum gpio_result dal_mux_setup_config(
220	struct gpio *mux,
221	struct gpio_generic_mux_config *config)
222{
223	struct gpio_config_data config_data;
224
225	if (!config)
226		return GPIO_RESULT_INVALID_DATA;
227
228	config_data.config.generic_mux = *config;
229	config_data.type = GPIO_CONFIG_TYPE_GENERIC_MUX;
230
231	return dal_gpio_set_config(mux, &config_data);
232}
233
234/*
235 * @brief
236 * Private API.
237 */
238
239static bool is_pin_busy(
240	const struct gpio_service *service,
241	enum gpio_id id,
242	uint32_t en)
243{
244	return service->busyness[id][en];
 
 
 
 
245}
246
247static void set_pin_busy(
248	struct gpio_service *service,
249	enum gpio_id id,
250	uint32_t en)
251{
252	service->busyness[id][en] = true;
 
 
 
253}
254
255static void set_pin_free(
256	struct gpio_service *service,
257	enum gpio_id id,
258	uint32_t en)
259{
260	service->busyness[id][en] = false;
261}
262
263enum gpio_result dal_gpio_service_lock(
264	struct gpio_service *service,
265	enum gpio_id id,
266	uint32_t en)
267{
268	if (!service->busyness[id]) {
269		ASSERT_CRITICAL(false);
270		return GPIO_RESULT_OPEN_FAILED;
271	}
272
273	set_pin_busy(service, id, en);
274	return GPIO_RESULT_OK;
275}
276
277enum gpio_result dal_gpio_service_unlock(
278	struct gpio_service *service,
279	enum gpio_id id,
280	uint32_t en)
 
 
281{
282	if (!service->busyness[id]) {
283		ASSERT_CRITICAL(false);
284		return GPIO_RESULT_OPEN_FAILED;
285	}
286
287	set_pin_free(service, id, en);
288	return GPIO_RESULT_OK;
289}
290
291enum gpio_result dal_gpio_service_open(
292	struct gpio *gpio)
293{
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;
298
299	struct hw_gpio_pin **pin = &gpio->pin;
300
301
302	if (!service->busyness[id]) {
303		ASSERT_CRITICAL(false);
304		return GPIO_RESULT_OPEN_FAILED;
305	}
306
307	if (is_pin_busy(service, id, en)) {
308		ASSERT_CRITICAL(false);
309		return GPIO_RESULT_DEVICE_BUSY;
310	}
311
312	switch (id) {
313	case GPIO_ID_DDC_DATA:
314		*pin = service->factory.funcs->get_ddc_pin(gpio);
315		service->factory.funcs->define_ddc_registers(*pin, en);
 
316	break;
317	case GPIO_ID_DDC_CLOCK:
318		*pin = service->factory.funcs->get_ddc_pin(gpio);
319		service->factory.funcs->define_ddc_registers(*pin, en);
 
320	break;
321	case GPIO_ID_GENERIC:
322		*pin = service->factory.funcs->get_generic_pin(gpio);
323		service->factory.funcs->define_generic_registers(*pin, en);
324	break;
325	case GPIO_ID_HPD:
326		*pin = service->factory.funcs->get_hpd_pin(gpio);
327		service->factory.funcs->define_hpd_registers(*pin, en);
 
328	break;
329
330	//TODO: gsl and sync support? create_sync and create_gsl are NULL
331	case GPIO_ID_SYNC:
 
 
 
332	case GPIO_ID_GSL:
 
 
333	break;
334	default:
335		ASSERT_CRITICAL(false);
336		return GPIO_RESULT_NON_SPECIFIC_ERROR;
337	}
338
339	if (!*pin) {
340		ASSERT_CRITICAL(false);
341		return GPIO_RESULT_NON_SPECIFIC_ERROR;
342	}
343
344	if (!(*pin)->funcs->open(*pin, mode)) {
345		ASSERT_CRITICAL(false);
346		dal_gpio_service_close(service, pin);
347		return GPIO_RESULT_OPEN_FAILED;
348	}
349
350	set_pin_busy(service, id, en);
 
351	return GPIO_RESULT_OK;
352}
353
354void dal_gpio_service_close(
355	struct gpio_service *service,
356	struct hw_gpio_pin **ptr)
357{
358	struct hw_gpio_pin *pin;
359
360	if (!ptr) {
361		ASSERT_CRITICAL(false);
362		return;
363	}
364
365	pin = *ptr;
366
367	if (pin) {
368		set_pin_free(service, pin->id, pin->en);
369
370		pin->funcs->close(pin);
371
372		*ptr = NULL;
373	}
374}
375
 
376enum dc_irq_source dal_irq_get_source(
377	const struct gpio *irq)
378{
379	enum gpio_id id = dal_gpio_get_id(irq);
380
381	switch (id) {
382	case GPIO_ID_HPD:
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));
388	default:
389		return DC_IRQ_SOURCE_INVALID;
390	}
391}
392
393enum dc_irq_source dal_irq_get_rx_source(
394	const struct gpio *irq)
395{
396	enum gpio_id id = dal_gpio_get_id(irq);
397
398	switch (id) {
399	case GPIO_ID_HPD:
400		return (enum dc_irq_source)(DC_IRQ_SOURCE_HPD1RX +
401			dal_gpio_get_enum(irq));
402	default:
403		return DC_IRQ_SOURCE_INVALID;
404	}
405}
406
407enum gpio_result dal_irq_setup_hpd_filter(
408	struct gpio *irq,
409	struct gpio_hpd_config *config)
410{
411	struct gpio_config_data config_data;
412
413	if (!config)
414		return GPIO_RESULT_INVALID_DATA;
415
416	config_data.type = GPIO_CONFIG_TYPE_HPD;
417	config_data.config.hpd = *config;
418
419	return dal_gpio_set_config(irq, &config_data);
420}
421
422/*
423 * @brief
424 * Creation and destruction
425 */
426
427struct gpio *dal_gpio_create_irq(
428	struct gpio_service *service,
429	enum gpio_id id,
430	uint32_t en)
431{
432	struct gpio *irq;
433
434	switch (id) {
435	case GPIO_ID_HPD:
436	case GPIO_ID_GPIO_PAD:
437	break;
438	default:
439		id = GPIO_ID_HPD;
440		ASSERT_CRITICAL(false);
441		return NULL;
442	}
443
444	irq = dal_gpio_create(
445		service, id, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
446
447	if (irq)
448		return irq;
449
450	ASSERT_CRITICAL(false);
451	return NULL;
452}
453
454void dal_gpio_destroy_irq(
455	struct gpio **irq)
456{
457	if (!irq || !*irq) {
458		ASSERT_CRITICAL(false);
459		return;
460	}
461
 
462	dal_gpio_destroy(irq);
463	kfree(*irq);
464
465	*irq = NULL;
466}
467
468struct ddc *dal_gpio_create_ddc(
469	struct gpio_service *service,
470	uint32_t offset,
471	uint32_t mask,
472	struct gpio_ddc_hw_info *info)
473{
474	enum gpio_id id;
475	uint32_t en;
476	struct ddc *ddc;
477
478	if (!service->translate.funcs->offset_to_id(offset, mask, &id, &en))
479		return NULL;
480
481	ddc = kzalloc(sizeof(struct ddc), GFP_KERNEL);
482
483	if (!ddc) {
484		BREAK_TO_DEBUGGER();
485		return NULL;
486	}
487
488	ddc->pin_data = dal_gpio_create(
489		service, GPIO_ID_DDC_DATA, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
490
491	if (!ddc->pin_data) {
492		BREAK_TO_DEBUGGER();
493		goto failure_1;
494	}
495
496	ddc->pin_clock = dal_gpio_create(
497		service, GPIO_ID_DDC_CLOCK, en, GPIO_PIN_OUTPUT_STATE_DEFAULT);
498
499	if (!ddc->pin_clock) {
500		BREAK_TO_DEBUGGER();
501		goto failure_2;
502	}
503
504	ddc->hw_info = *info;
505
506	ddc->ctx = service->ctx;
507
508	return ddc;
509
510failure_2:
511	dal_gpio_destroy(&ddc->pin_data);
512
513failure_1:
514	kfree(ddc);
515
516	return NULL;
517}
518
519void dal_gpio_destroy_ddc(
520	struct ddc **ddc)
521{
522	if (!ddc || !*ddc) {
523		BREAK_TO_DEBUGGER();
524		return;
525	}
526
527	dal_ddc_close(*ddc);
528	dal_gpio_destroy(&(*ddc)->pin_data);
529	dal_gpio_destroy(&(*ddc)->pin_clock);
530	kfree(*ddc);
531
532	*ddc = NULL;
533}
534
535enum gpio_result dal_ddc_open(
536	struct ddc *ddc,
537	enum gpio_mode mode,
538	enum gpio_ddc_config_type config_type)
539{
540	enum gpio_result result;
541
542	struct gpio_config_data config_data;
543	struct hw_gpio *hw_data;
544	struct hw_gpio *hw_clock;
545
546	result = dal_gpio_open_ex(ddc->pin_data, mode);
547
548	if (result != GPIO_RESULT_OK) {
549		BREAK_TO_DEBUGGER();
550		return result;
551	}
552
553	result = dal_gpio_open_ex(ddc->pin_clock, mode);
554
555	if (result != GPIO_RESULT_OK) {
556		BREAK_TO_DEBUGGER();
557		goto failure;
558	}
559
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. */
563
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;
568	else
569		config_data.type = GPIO_CONFIG_TYPE_DDC;
570
571	config_data.config.ddc.type = config_type;
572
573	hw_data = FROM_HW_GPIO_PIN(ddc->pin_data->pin);
574	hw_clock = FROM_HW_GPIO_PIN(ddc->pin_clock->pin);
575
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;
578
579	result = dal_gpio_set_config(ddc->pin_data, &config_data);
580
581	if (result == GPIO_RESULT_OK)
582		return result;
583
584	BREAK_TO_DEBUGGER();
585
586	dal_gpio_close(ddc->pin_clock);
587
588failure:
589	dal_gpio_close(ddc->pin_data);
590
591	return result;
592}
593
594enum gpio_result dal_ddc_change_mode(
595	struct ddc *ddc,
596	enum gpio_mode mode)
597{
598	enum gpio_result result;
599
600	enum gpio_mode original_mode =
601		dal_gpio_get_mode(ddc->pin_data);
602
603	result = dal_gpio_change_mode(ddc->pin_data, mode);
604
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 */
609
610	if (result != GPIO_RESULT_OK)
611		goto failure;
612
613	result = dal_gpio_change_mode(ddc->pin_clock, mode);
614
615	if (result == GPIO_RESULT_OK)
616		return result;
617
618	dal_gpio_change_mode(ddc->pin_clock, original_mode);
619
620failure:
621	dal_gpio_change_mode(ddc->pin_data, original_mode);
622
623	return result;
624}
625
626enum gpio_ddc_line dal_ddc_get_line(
627	const struct ddc *ddc)
628{
629	return (enum gpio_ddc_line)dal_gpio_get_enum(ddc->pin_data);
630}
631
632enum gpio_result dal_ddc_set_config(
633	struct ddc *ddc,
634	enum gpio_ddc_config_type config_type)
635{
636	struct gpio_config_data config_data;
637
638	config_data.type = GPIO_CONFIG_TYPE_DDC;
639
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;
643
644	return dal_gpio_set_config(ddc->pin_data, &config_data);
645}
646
647void dal_ddc_close(
648	struct ddc *ddc)
649{
650	dal_gpio_close(ddc->pin_clock);
651	dal_gpio_close(ddc->pin_data);
652}
653