Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
/*
 * Copyright (c) 2016 Intel Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file Driver for the Atmel SAM3 PIO Controller.
 */

#include <errno.h>

#include <nanokernel.h>

#include <device.h>
#include <init.h>

#include <soc.h>

#include <gpio.h>
#include "gpio_utils.h"
#include "gpio_api_compat.h"

typedef void (*config_func_t)(struct device *port);

/* Configuration data */
struct gpio_sam3_config {
	volatile struct __pio	*port;

	/* callbacks */
	sys_slist_t		cb;

	config_func_t		config_func;
};

static void _config(struct device *dev, uint32_t mask, int flags)
{
	struct gpio_sam3_config *cfg = dev->config->config_info;

	/* Disable the pin and return as setup is meaningless now */
	if (flags & GPIO_PIN_DISABLE) {
		cfg->port->pdr = mask;
		return;
	}

	/* Setup the pin direction */
	if ((flags & GPIO_DIR_MASK) == GPIO_DIR_OUT) {
		cfg->port->oer = mask;
	} else {
		cfg->port->odr = mask;
	}

	/* Setup interrupt config */
	if (flags & GPIO_INT) {
		if (flags & GPIO_INT_DOUBLE_EDGE) {
			cfg->port->aimdr = mask;
		} else {
			cfg->port->aimer = mask;

			if (flags & GPIO_INT_EDGE) {
				cfg->port->esr = mask;
			} else if (flags & GPIO_INT_LEVEL) {
				cfg->port->lsr = mask;
			}

			if (flags & GPIO_INT_ACTIVE_LOW) {
				cfg->port->fellsr = mask;
			} else if (flags & GPIO_INT_ACTIVE_HIGH) {
				cfg->port->rehlsr = mask;
			}
		}
	}

	/* Pull-up? */
	if ((flags & GPIO_PUD_MASK) == GPIO_PUD_PULL_UP) {
		/* Enable pull-up */
		cfg->port->puer = mask;
	} else {
		/* Disable pull-up */
		cfg->port->pudr = mask;
	}

	/* Debounce */
	if (flags & GPIO_INT_DEBOUNCE) {
		cfg->port->difsr = mask;
	} else {
		cfg->port->scifsr = mask;
	}

	/* Enable the pin last after pin setup */
	if (flags & GPIO_PIN_ENABLE) {
		cfg->port->per = mask;
	}
}

/**
 * @brief Configurate pin or port
 *
 * @param dev Device struct
 * @param access_op Access operation (pin or port)
 * @param pin The pin number
 * @param flags Flags of pin or port
 *
 * @return 0 if successful, failed otherwise
 */
static int gpio_sam3_config(struct device *dev, int access_op,
			    uint32_t pin, int flags)
{
	switch (access_op) {
	case GPIO_ACCESS_BY_PIN:
		_config(dev, BIT(pin), flags);
		break;
	case GPIO_ACCESS_BY_PORT:
		_config(dev, (0xFFFFFFFF), flags);
		break;
	default:
		return -ENOTSUP;
	}

	return 0;
}

/**
 * @brief Set the pin or port output
 *
 * @param dev Device struct
 * @param access_op Access operation (pin or port)
 * @param pin The pin number
 * @param value Value to set (0 or 1)
 *
 * @return 0 if successful, failed otherwise
 */
static int gpio_sam3_write(struct device *dev, int access_op,
			   uint32_t pin, uint32_t value)
{
	struct gpio_sam3_config *cfg = dev->config->config_info;

	switch (access_op) {
	case GPIO_ACCESS_BY_PIN:
		if (value) {
			/* set the pin */
			cfg->port->sodr = BIT(pin);
		} else {
			/* clear the pin */
			cfg->port->codr = BIT(pin);
		}
		break;
	case GPIO_ACCESS_BY_PORT:
		if (value) {
			/* set all pins */
			cfg->port->sodr = 0xFFFFFFFF;
		} else {
			/* clear all pins */
			cfg->port->codr = 0xFFFFFFFF;
		}
		break;
	default:
		return -ENOTSUP;
	}

	return 0;
}

/**
 * @brief Read the pin or port status
 *
 * @param dev Device struct
 * @param access_op Access operation (pin or port)
 * @param pin The pin number
 * @param value Value of input pin(s)
 *
 * @return 0 if successful, failed otherwise
 */
static int gpio_sam3_read(struct device *dev, int access_op,
				       uint32_t pin, uint32_t *value)
{
	struct gpio_sam3_config *cfg = dev->config->config_info;

	*value = cfg->port->pdsr;

	switch (access_op) {
	case GPIO_ACCESS_BY_PIN:
		*value = (*value >> pin) & 0x01;
		break;
	case GPIO_ACCESS_BY_PORT:
		break;
	default:
		return -ENOTSUP;
	}

	return 0;
}

static void gpio_sam3_isr(void *arg)
{
	struct device *dev = (struct device *)arg;
	struct gpio_sam3_config *cfg = dev->config->config_info;
	uint32_t int_stat;

	int_stat = cfg->port->isr;

	_gpio_fire_callbacks(&cfg->cb, dev, int_stat);
}

static int gpio_sam3_manage_callback(struct device *dev,
				     struct gpio_callback *callback,
				     bool set)
{
	struct gpio_sam3_config *cfg = dev->config->config_info;

	_gpio_manage_callback(&cfg->cb, callback, set);

	return 0;
}

static int gpio_sam3_enable_callback(struct device *dev,
				     int access_op, uint32_t pin)
{
	struct gpio_sam3_config *cfg = dev->config->config_info;
	uint32_t mask;

	switch (access_op) {
	case GPIO_ACCESS_BY_PIN:
		mask = BIT(pin);
		break;
	case GPIO_ACCESS_BY_PORT:
		mask = 0xFFFFFFFF;
		break;
	default:
		return -ENOTSUP;
	}

	_gpio_enable_callback(dev, mask);
	cfg->port->ier |= mask;

	return 0;
}

static int gpio_sam3_disable_callback(struct device *dev,
				      int access_op, uint32_t pin)
{
	struct gpio_sam3_config *cfg = dev->config->config_info;
	uint32_t mask;

	switch (access_op) {
	case GPIO_ACCESS_BY_PIN:
		mask = BIT(pin);
		break;
	case GPIO_ACCESS_BY_PORT:
		mask = 0xFFFFFFFF;
		break;
	default:
		return -ENOTSUP;
	}

	_gpio_disable_callback(dev, mask);
	cfg->port->idr |= mask;

	return 0;
}

static struct gpio_driver_api gpio_sam3_drv_api_funcs = {
	.config = gpio_sam3_config,
	.write = gpio_sam3_write,
	.read = gpio_sam3_read,
	.manage_callback = gpio_sam3_manage_callback,
	.enable_callback = gpio_sam3_enable_callback,
	.disable_callback = gpio_sam3_disable_callback,
};

/**
 * @brief Initialization function of MMIO
 *
 * @param dev Device struct
 * @return 0 if successful, failed otherwise.
 */
int gpio_sam3_init(struct device *dev)
{
	struct gpio_sam3_config *cfg = dev->config->config_info;

	cfg->config_func(dev);

	return 0;
}

/* Port A */
#ifdef CONFIG_GPIO_ATMEL_SAM3_PORTA
void gpio_sam3_config_a(struct device *dev);

static struct gpio_sam3_config gpio_sam3_a_cfg = {
	.port = __PIOA,

	.config_func = gpio_sam3_config_a,
};

DEVICE_AND_API_INIT(gpio_sam3_a, CONFIG_GPIO_ATMEL_SAM3_PORTA_DEV_NAME,
		    gpio_sam3_init, NULL, &gpio_sam3_a_cfg,
		    SECONDARY, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		    &gpio_sam3_drv_api_funcs);
GPIO_SETUP_COMPAT_DEV(gpio_sam3_a);

void gpio_sam3_config_a(struct device *dev)
{
	/* Enable clock for PIO controller */
	__PMC->pcer0 = BIT(PID_PIOA);

	IRQ_CONNECT(IRQ_PIOA, CONFIG_GPIO_ATMEL_SAM3_PORTA_IRQ_PRI,
		    gpio_sam3_isr, DEVICE_GET(gpio_sam3_a), 0);
	irq_enable(IRQ_PIOA);
}
#endif /* CONFIG_GPIO_ATMEL_SAM3_PORTA */

/* Port B */
#ifdef CONFIG_GPIO_ATMEL_SAM3_PORTB
void gpio_sam3_config_b(struct device *dev);

static struct gpio_sam3_config gpio_sam3_b_cfg = {
	.port = __PIOB,

	.config_func = gpio_sam3_config_b,
};

DEVICE_AND_API_INIT(gpio_sam3_b, CONFIG_GPIO_ATMEL_SAM3_PORTB_DEV_NAME,
		    gpio_sam3_init, NULL, &gpio_sam3_b_cfg,
		    SECONDARY, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		    &gpio_sam3_drv_api_funcs);
GPIO_SETUP_COMPAT_DEV(gpio_sam3_b);

void gpio_sam3_config_b(struct device *dev)
{
	/* Enable clock for PIO controller */
	__PMC->pcer0 = BIT(PID_PIOB);

	IRQ_CONNECT(IRQ_PIOB, CONFIG_GPIO_ATMEL_SAM3_PORTB_IRQ_PRI,
		    gpio_sam3_isr, DEVICE_GET(gpio_sam3_b), 0);
	irq_enable(IRQ_PIOB);
}
#endif /* CONFIG_GPIO_ATMEL_SAM3_PORTB */

/* Port C */
#ifdef CONFIG_GPIO_ATMEL_SAM3_PORTC
void gpio_sam3_config_c(struct device *dev);

static struct gpio_sam3_config gpio_sam3_c_cfg = {
	.port = __PIOC,

	.config_func = gpio_sam3_config_c,
};

DEVICE_AND_API_INIT(gpio_sam3_c, CONFIG_GPIO_ATMEL_SAM3_PORTC_DEV_NAME,
		    gpio_sam3_init, NULL, &gpio_sam3_c_cfg,
		    SECONDARY, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		    &gpio_sam3_drv_api_funcs);
GPIO_SETUP_COMPAT_DEV(gpio_sam3_c);

void gpio_sam3_config_c(struct device *dev)
{
	/* Enable clock for PIO controller */
	__PMC->pcer0 = BIT(PID_PIOC);

	IRQ_CONNECT(IRQ_PIOC, CONFIG_GPIO_ATMEL_SAM3_PORTC_IRQ_PRI,
		    gpio_sam3_isr, DEVICE_GET(gpio_sam3_c), 0);
	irq_enable(IRQ_PIOC);
}
#endif /* CONFIG_GPIO_ATMEL_SAM3_PORTA */

/* Port D */
#ifdef CONFIG_GPIO_ATMEL_SAM3_PORTD
void gpio_sam3_config_d(struct device *dev);

static struct gpio_sam3_config gpio_sam3_d_cfg = {
	.port = __PIOD,

	.config_func = gpio_sam3_config_d,
};

DEVICE_AND_API_INIT(gpio_sam3_d, CONFIG_GPIO_ATMEL_SAM3_PORTD_DEV_NAME,
		    gpio_sam3_init, NULL, &gpio_sam3_d_cfg,
		    SECONDARY, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		    &gpio_sam3_drv_api_funcs);
GPIO_SETUP_COMPAT_DEV(gpio_sam3_d);

void gpio_sam3_config_d(struct device *dev)
{
	/* Enable clock for PIO controller */
	__PMC->pcer0 = BIT(PID_PIOD);

	IRQ_CONNECT(IRQ_PIOD, CONFIG_GPIO_ATMEL_SAM3_PORTD_IRQ_PRI,
		    gpio_sam3_isr, DEVICE_GET(gpio_sam3_d), 0);
	irq_enable(IRQ_PIOD);
}
#endif /* CONFIG_GPIO_ATMEL_SAM3_PORTD */