2017-11-07 13:58:41 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-08-12 04:00:16 +00:00
|
|
|
/*
|
|
|
|
* GPIO Greybus driver.
|
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
2014-12-12 18:08:42 +00:00
|
|
|
* Copyright 2014 Linaro Ltd.
|
2014-08-12 04:00:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
2015-02-17 15:48:23 +00:00
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/irqdomain.h>
|
2019-01-14 14:56:16 +00:00
|
|
|
#include <linux/gpio/driver.h>
|
2015-05-26 13:29:24 +00:00
|
|
|
#include <linux/mutex.h>
|
2019-08-25 05:54:27 +00:00
|
|
|
#include <linux/greybus.h>
|
2015-05-26 13:29:24 +00:00
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
#include "gbphy.h"
|
2014-08-12 04:00:16 +00:00
|
|
|
|
2014-10-16 11:35:39 +00:00
|
|
|
struct gb_gpio_line {
|
|
|
|
/* The following has to be an array of line_max entries */
|
|
|
|
/* --> make them just a flags field */
|
|
|
|
u8 active: 1,
|
|
|
|
direction: 1, /* 0 = output, 1 = input */
|
|
|
|
value: 1; /* 0 = low, 1 = high */
|
|
|
|
u16 debounce_usec;
|
2015-05-26 13:29:24 +00:00
|
|
|
|
|
|
|
u8 irq_type;
|
|
|
|
bool irq_type_pending;
|
|
|
|
bool masked;
|
|
|
|
bool masked_pending;
|
2014-10-16 11:35:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct gb_gpio_controller {
|
2016-05-19 15:52:39 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
2014-10-16 11:35:39 +00:00
|
|
|
struct gb_connection *connection;
|
|
|
|
u8 line_max; /* max line number */
|
|
|
|
struct gb_gpio_line *lines;
|
|
|
|
|
|
|
|
struct gpio_chip chip;
|
2015-02-17 15:48:23 +00:00
|
|
|
struct irq_chip irqc;
|
2015-05-26 13:29:24 +00:00
|
|
|
struct mutex irq_lock;
|
2014-10-16 11:35:39 +00:00
|
|
|
};
|
2023-03-25 08:23:33 +00:00
|
|
|
|
|
|
|
static inline struct gb_gpio_controller *gpio_chip_to_gb_gpio_controller(struct gpio_chip *chip)
|
|
|
|
{
|
|
|
|
return container_of(chip, struct gb_gpio_controller, chip);
|
|
|
|
}
|
2023-01-17 15:28:57 +00:00
|
|
|
|
|
|
|
static struct gpio_chip *irq_data_to_gpio_chip(struct irq_data *d)
|
|
|
|
{
|
|
|
|
return d->domain->host_data;
|
|
|
|
}
|
2014-10-16 11:35:39 +00:00
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
static int gb_gpio_line_count_operation(struct gb_gpio_controller *ggc)
|
2014-08-12 04:00:16 +00:00
|
|
|
{
|
2014-11-24 01:45:22 +00:00
|
|
|
struct gb_gpio_line_count_response response;
|
2014-10-16 11:35:39 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_LINE_COUNT,
|
|
|
|
NULL, 0, &response, sizeof(response));
|
|
|
|
if (!ret)
|
|
|
|
ggc->line_max = response.count;
|
2014-10-16 11:35:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
static int gb_gpio_activate_operation(struct gb_gpio_controller *ggc, u8 which)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
2014-11-24 01:45:22 +00:00
|
|
|
struct gb_gpio_activate_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev = ggc->gbphy_dev;
|
2014-10-16 11:35:39 +00:00
|
|
|
int ret;
|
|
|
|
|
2016-07-14 20:13:00 +00:00
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
request.which = which;
|
|
|
|
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE,
|
2018-11-25 16:58:15 +00:00
|
|
|
&request, sizeof(request), NULL, 0);
|
2016-07-14 20:13:00 +00:00
|
|
|
if (ret) {
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ggc->lines[which].active = true;
|
|
|
|
|
|
|
|
return 0;
|
2014-10-16 11:35:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 15:51:15 +00:00
|
|
|
static void gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc,
|
2018-11-25 16:58:15 +00:00
|
|
|
u8 which)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev = ggc->gbphy_dev;
|
|
|
|
struct device *dev = &gbphy_dev->dev;
|
2014-11-24 01:45:22 +00:00
|
|
|
struct gb_gpio_deactivate_request request;
|
2014-10-16 11:35:39 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
request.which = which;
|
|
|
|
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE,
|
2018-11-25 16:58:15 +00:00
|
|
|
&request, sizeof(request), NULL, 0);
|
2015-03-19 15:55:23 +00:00
|
|
|
if (ret) {
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_err(dev, "failed to deactivate gpio %u\n", which);
|
2016-07-14 20:13:00 +00:00
|
|
|
goto out_pm_put;
|
2015-03-19 15:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ggc->lines[which].active = false;
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
out_pm_put:
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
2014-10-16 11:35:39 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc,
|
2018-11-25 16:58:15 +00:00
|
|
|
u8 which)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
2016-05-19 15:52:39 +00:00
|
|
|
struct device *dev = &ggc->gbphy_dev->dev;
|
2014-11-24 01:45:22 +00:00
|
|
|
struct gb_gpio_get_direction_request request;
|
|
|
|
struct gb_gpio_get_direction_response response;
|
2014-10-16 11:35:39 +00:00
|
|
|
int ret;
|
2014-11-22 01:29:12 +00:00
|
|
|
u8 direction;
|
2014-10-16 11:35:39 +00:00
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
request.which = which;
|
|
|
|
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION,
|
|
|
|
&request, sizeof(request),
|
|
|
|
&response, sizeof(response));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-10-16 11:35:39 +00:00
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
direction = response.direction;
|
2015-03-19 15:51:16 +00:00
|
|
|
if (direction && direction != 1) {
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_warn(dev, "gpio %u direction was %u (should be 0 or 1)\n",
|
2015-03-19 15:51:16 +00:00
|
|
|
which, direction);
|
|
|
|
}
|
2014-11-24 01:45:22 +00:00
|
|
|
ggc->lines[which].direction = direction ? 1 : 0;
|
|
|
|
return 0;
|
2014-10-16 11:35:39 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc,
|
2018-11-25 16:58:15 +00:00
|
|
|
u8 which)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
2014-11-24 01:45:22 +00:00
|
|
|
struct gb_gpio_direction_in_request request;
|
2014-10-16 11:35:39 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
request.which = which;
|
|
|
|
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
if (!ret)
|
|
|
|
ggc->lines[which].direction = 1;
|
2014-10-16 11:35:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc,
|
2018-11-25 16:58:15 +00:00
|
|
|
u8 which, bool value_high)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
2014-11-24 01:45:22 +00:00
|
|
|
struct gb_gpio_direction_out_request request;
|
2014-10-16 11:35:39 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
request.which = which;
|
|
|
|
request.value = value_high ? 1 : 0;
|
|
|
|
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_OUT,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
if (!ret)
|
|
|
|
ggc->lines[which].direction = 0;
|
2014-10-16 11:35:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc,
|
2018-11-25 16:58:15 +00:00
|
|
|
u8 which)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
2016-05-19 15:52:39 +00:00
|
|
|
struct device *dev = &ggc->gbphy_dev->dev;
|
2014-11-24 01:45:22 +00:00
|
|
|
struct gb_gpio_get_value_request request;
|
|
|
|
struct gb_gpio_get_value_response response;
|
2014-10-16 11:35:39 +00:00
|
|
|
int ret;
|
2014-11-22 01:29:12 +00:00
|
|
|
u8 value;
|
2014-10-16 11:35:39 +00:00
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
request.which = which;
|
|
|
|
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE,
|
|
|
|
&request, sizeof(request),
|
|
|
|
&response, sizeof(response));
|
2015-03-19 15:55:23 +00:00
|
|
|
if (ret) {
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_err(dev, "failed to get value of gpio %u\n", which);
|
2014-11-24 01:45:22 +00:00
|
|
|
return ret;
|
2015-03-19 15:55:23 +00:00
|
|
|
}
|
2014-10-16 11:35:39 +00:00
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
value = response.value;
|
2015-03-19 15:51:16 +00:00
|
|
|
if (value && value != 1) {
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_warn(dev, "gpio %u value was %u (should be 0 or 1)\n",
|
2015-03-19 15:51:16 +00:00
|
|
|
which, value);
|
|
|
|
}
|
2014-11-24 01:45:22 +00:00
|
|
|
ggc->lines[which].value = value ? 1 : 0;
|
|
|
|
return 0;
|
2014-10-16 11:35:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 15:51:15 +00:00
|
|
|
static void gb_gpio_set_value_operation(struct gb_gpio_controller *ggc,
|
2014-10-16 11:35:39 +00:00
|
|
|
u8 which, bool value_high)
|
|
|
|
{
|
2016-05-19 15:52:39 +00:00
|
|
|
struct device *dev = &ggc->gbphy_dev->dev;
|
2014-11-24 01:45:22 +00:00
|
|
|
struct gb_gpio_set_value_request request;
|
2014-10-16 11:35:39 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-03-19 15:55:22 +00:00
|
|
|
if (ggc->lines[which].direction == 1) {
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_warn(dev, "refusing to set value of input gpio %u\n",
|
|
|
|
which);
|
2015-03-19 15:55:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
request.which = which;
|
|
|
|
request.value = value_high ? 1 : 0;
|
|
|
|
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_VALUE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
2015-03-19 15:55:23 +00:00
|
|
|
if (ret) {
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_err(dev, "failed to set value of gpio %u\n", which);
|
2015-03-19 15:55:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ggc->lines[which].value = request.value;
|
2014-10-16 11:35:39 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc,
|
2018-11-25 16:58:15 +00:00
|
|
|
u8 which, u16 debounce_usec)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
2014-11-24 01:45:22 +00:00
|
|
|
struct gb_gpio_set_debounce_request request;
|
2014-10-16 11:35:39 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-11-24 01:45:22 +00:00
|
|
|
request.which = which;
|
|
|
|
request.usec = cpu_to_le16(debounce_usec);
|
|
|
|
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_DEBOUNCE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
if (!ret)
|
|
|
|
ggc->lines[which].debounce_usec = debounce_usec;
|
2014-10-16 11:35:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-26 13:29:24 +00:00
|
|
|
static void _gb_gpio_irq_mask(struct gb_gpio_controller *ggc, u8 hwirq)
|
2015-02-17 15:48:23 +00:00
|
|
|
{
|
2016-05-19 15:52:39 +00:00
|
|
|
struct device *dev = &ggc->gbphy_dev->dev;
|
2015-02-17 15:48:23 +00:00
|
|
|
struct gb_gpio_irq_mask_request request;
|
|
|
|
int ret;
|
|
|
|
|
2015-05-26 13:29:24 +00:00
|
|
|
request.which = hwirq;
|
2015-02-17 15:48:23 +00:00
|
|
|
ret = gb_operation_sync(ggc->connection,
|
|
|
|
GB_GPIO_TYPE_IRQ_MASK,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
if (ret)
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_err(dev, "failed to mask irq: %d\n", ret);
|
2015-02-17 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-26 13:29:24 +00:00
|
|
|
static void _gb_gpio_irq_unmask(struct gb_gpio_controller *ggc, u8 hwirq)
|
2015-02-17 15:48:23 +00:00
|
|
|
{
|
2016-05-19 15:52:39 +00:00
|
|
|
struct device *dev = &ggc->gbphy_dev->dev;
|
2015-02-17 15:48:23 +00:00
|
|
|
struct gb_gpio_irq_unmask_request request;
|
|
|
|
int ret;
|
|
|
|
|
2015-05-26 13:29:24 +00:00
|
|
|
request.which = hwirq;
|
2015-02-17 15:48:23 +00:00
|
|
|
ret = gb_operation_sync(ggc->connection,
|
|
|
|
GB_GPIO_TYPE_IRQ_UNMASK,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
if (ret)
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_err(dev, "failed to unmask irq: %d\n", ret);
|
2015-02-17 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-26 13:29:24 +00:00
|
|
|
static void _gb_gpio_irq_set_type(struct gb_gpio_controller *ggc,
|
2018-11-25 16:58:15 +00:00
|
|
|
u8 hwirq, u8 type)
|
2015-02-17 15:48:23 +00:00
|
|
|
{
|
2016-05-19 15:52:39 +00:00
|
|
|
struct device *dev = &ggc->gbphy_dev->dev;
|
2015-02-17 15:48:23 +00:00
|
|
|
struct gb_gpio_irq_type_request request;
|
2015-05-26 13:29:24 +00:00
|
|
|
int ret;
|
2015-02-17 15:48:23 +00:00
|
|
|
|
2015-05-26 13:29:24 +00:00
|
|
|
request.which = hwirq;
|
2015-02-17 15:48:23 +00:00
|
|
|
request.type = type;
|
|
|
|
|
2015-05-26 13:29:24 +00:00
|
|
|
ret = gb_operation_sync(ggc->connection,
|
|
|
|
GB_GPIO_TYPE_IRQ_TYPE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
if (ret)
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_err(dev, "failed to set irq type: %d\n", ret);
|
2015-05-26 13:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_gpio_irq_mask(struct irq_data *d)
|
|
|
|
{
|
|
|
|
struct gpio_chip *chip = irq_data_to_gpio_chip(d);
|
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
|
|
|
struct gb_gpio_line *line = &ggc->lines[d->hwirq];
|
|
|
|
|
|
|
|
line->masked = true;
|
|
|
|
line->masked_pending = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_gpio_irq_unmask(struct irq_data *d)
|
|
|
|
{
|
|
|
|
struct gpio_chip *chip = irq_data_to_gpio_chip(d);
|
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
|
|
|
struct gb_gpio_line *line = &ggc->lines[d->hwirq];
|
|
|
|
|
|
|
|
line->masked = false;
|
|
|
|
line->masked_pending = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
|
|
|
|
{
|
|
|
|
struct gpio_chip *chip = irq_data_to_gpio_chip(d);
|
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
|
|
|
struct gb_gpio_line *line = &ggc->lines[d->hwirq];
|
2016-05-19 15:52:39 +00:00
|
|
|
struct device *dev = &ggc->gbphy_dev->dev;
|
2015-05-28 17:03:34 +00:00
|
|
|
u8 irq_type;
|
2015-05-26 13:29:24 +00:00
|
|
|
|
2015-02-17 15:48:23 +00:00
|
|
|
switch (type) {
|
|
|
|
case IRQ_TYPE_NONE:
|
2015-05-28 17:03:34 +00:00
|
|
|
irq_type = GB_GPIO_IRQ_TYPE_NONE;
|
|
|
|
break;
|
2015-02-17 15:48:23 +00:00
|
|
|
case IRQ_TYPE_EDGE_RISING:
|
2015-05-28 17:03:34 +00:00
|
|
|
irq_type = GB_GPIO_IRQ_TYPE_EDGE_RISING;
|
|
|
|
break;
|
2015-02-17 15:48:23 +00:00
|
|
|
case IRQ_TYPE_EDGE_FALLING:
|
2015-05-28 17:03:34 +00:00
|
|
|
irq_type = GB_GPIO_IRQ_TYPE_EDGE_FALLING;
|
|
|
|
break;
|
2015-02-17 15:48:23 +00:00
|
|
|
case IRQ_TYPE_EDGE_BOTH:
|
2015-05-28 17:03:34 +00:00
|
|
|
irq_type = GB_GPIO_IRQ_TYPE_EDGE_BOTH;
|
|
|
|
break;
|
2015-02-17 15:48:23 +00:00
|
|
|
case IRQ_TYPE_LEVEL_LOW:
|
2015-05-28 17:03:34 +00:00
|
|
|
irq_type = GB_GPIO_IRQ_TYPE_LEVEL_LOW;
|
|
|
|
break;
|
2015-02-17 15:48:23 +00:00
|
|
|
case IRQ_TYPE_LEVEL_HIGH:
|
2015-05-28 17:03:34 +00:00
|
|
|
irq_type = GB_GPIO_IRQ_TYPE_LEVEL_HIGH;
|
2015-05-20 11:02:28 +00:00
|
|
|
break;
|
2015-02-17 15:48:23 +00:00
|
|
|
default:
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_err(dev, "unsupported irq type: %u\n", type);
|
2015-05-26 13:29:24 +00:00
|
|
|
return -EINVAL;
|
2015-02-17 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 17:03:34 +00:00
|
|
|
line->irq_type = irq_type;
|
2015-05-26 13:29:24 +00:00
|
|
|
line->irq_type_pending = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_gpio_irq_bus_lock(struct irq_data *d)
|
|
|
|
{
|
|
|
|
struct gpio_chip *chip = irq_data_to_gpio_chip(d);
|
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
|
|
|
|
|
|
|
mutex_lock(&ggc->irq_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_gpio_irq_bus_sync_unlock(struct irq_data *d)
|
|
|
|
{
|
|
|
|
struct gpio_chip *chip = irq_data_to_gpio_chip(d);
|
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
|
|
|
struct gb_gpio_line *line = &ggc->lines[d->hwirq];
|
|
|
|
|
|
|
|
if (line->irq_type_pending) {
|
|
|
|
_gb_gpio_irq_set_type(ggc, d->hwirq, line->irq_type);
|
|
|
|
line->irq_type_pending = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line->masked_pending) {
|
|
|
|
if (line->masked)
|
|
|
|
_gb_gpio_irq_mask(ggc, d->hwirq);
|
|
|
|
else
|
|
|
|
_gb_gpio_irq_unmask(ggc, d->hwirq);
|
|
|
|
line->masked_pending = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&ggc->irq_lock);
|
2015-02-17 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 09:02:33 +00:00
|
|
|
static int gb_gpio_request_handler(struct gb_operation *op)
|
2015-02-17 15:48:23 +00:00
|
|
|
{
|
2015-03-19 15:51:16 +00:00
|
|
|
struct gb_connection *connection = op->connection;
|
2016-03-22 18:30:35 +00:00
|
|
|
struct gb_gpio_controller *ggc = gb_connection_get_data(connection);
|
2016-05-19 15:52:39 +00:00
|
|
|
struct device *dev = &ggc->gbphy_dev->dev;
|
2015-02-17 15:48:23 +00:00
|
|
|
struct gb_message *request;
|
|
|
|
struct gb_gpio_irq_event_request *event;
|
2016-05-05 09:02:33 +00:00
|
|
|
u8 type = op->type;
|
2020-03-06 13:24:13 +00:00
|
|
|
int irq, ret;
|
2015-02-17 15:48:23 +00:00
|
|
|
|
|
|
|
if (type != GB_GPIO_TYPE_IRQ_EVENT) {
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_err(dev, "unsupported unsolicited request: %u\n", type);
|
2015-03-27 11:45:49 +00:00
|
|
|
return -EINVAL;
|
2015-02-17 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
request = op->request;
|
2015-03-27 11:45:41 +00:00
|
|
|
|
|
|
|
if (request->payload_size < sizeof(*event)) {
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_err(dev, "short event received (%zu < %zu)\n",
|
2015-08-06 07:14:51 +00:00
|
|
|
request->payload_size, sizeof(*event));
|
2015-03-27 11:45:49 +00:00
|
|
|
return -EINVAL;
|
2015-03-27 11:45:41 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 15:48:23 +00:00
|
|
|
event = request->payload;
|
|
|
|
if (event->which > ggc->line_max) {
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_err(dev, "invalid hw irq: %d\n", event->which);
|
2015-03-27 11:45:49 +00:00
|
|
|
return -EINVAL;
|
2015-02-17 15:48:23 +00:00
|
|
|
}
|
2015-03-27 11:45:49 +00:00
|
|
|
|
2019-01-14 14:56:16 +00:00
|
|
|
irq = irq_find_mapping(ggc->chip.irq.domain, event->which);
|
2015-05-26 13:29:22 +00:00
|
|
|
if (!irq) {
|
2016-02-24 06:51:23 +00:00
|
|
|
dev_err(dev, "failed to find IRQ\n");
|
2015-03-27 11:45:49 +00:00
|
|
|
return -EINVAL;
|
2015-03-27 11:45:42 +00:00
|
|
|
}
|
2015-02-17 15:48:23 +00:00
|
|
|
|
2022-02-11 18:15:00 +00:00
|
|
|
ret = generic_handle_irq_safe(irq);
|
2020-03-06 13:24:13 +00:00
|
|
|
if (ret)
|
|
|
|
dev_err(dev, "failed to invoke irq handler\n");
|
|
|
|
|
|
|
|
return ret;
|
2015-02-17 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:54:51 +00:00
|
|
|
static int gb_gpio_request(struct gpio_chip *chip, unsigned int offset)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
2015-03-19 15:51:14 +00:00
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
2014-10-16 11:35:39 +00:00
|
|
|
|
2015-03-19 15:51:15 +00:00
|
|
|
return gb_gpio_activate_operation(ggc, (u8)offset);
|
2014-08-12 04:00:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:54:51 +00:00
|
|
|
static void gb_gpio_free(struct gpio_chip *chip, unsigned int offset)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
2015-03-19 15:51:14 +00:00
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
2014-10-16 11:35:39 +00:00
|
|
|
|
2015-03-19 15:51:15 +00:00
|
|
|
gb_gpio_deactivate_operation(ggc, (u8)offset);
|
2014-10-16 11:35:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:54:51 +00:00
|
|
|
static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
2015-03-19 15:51:14 +00:00
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
2014-10-16 11:35:39 +00:00
|
|
|
u8 which;
|
|
|
|
int ret;
|
|
|
|
|
2014-10-20 10:39:45 +00:00
|
|
|
which = (u8)offset;
|
2015-03-19 15:51:14 +00:00
|
|
|
ret = gb_gpio_get_direction_operation(ggc, which);
|
2014-10-16 11:35:39 +00:00
|
|
|
if (ret)
|
2015-03-19 15:51:15 +00:00
|
|
|
return ret;
|
|
|
|
|
2015-03-19 15:51:14 +00:00
|
|
|
return ggc->lines[which].direction ? 1 : 0;
|
2014-10-16 11:35:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:54:51 +00:00
|
|
|
static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
|
2014-08-12 04:00:16 +00:00
|
|
|
{
|
2015-03-19 15:51:14 +00:00
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
2014-10-16 11:35:39 +00:00
|
|
|
|
2015-03-19 15:51:15 +00:00
|
|
|
return gb_gpio_direction_in_operation(ggc, (u8)offset);
|
2014-08-12 04:00:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:54:51 +00:00
|
|
|
static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
|
|
|
|
int value)
|
2014-08-12 04:00:16 +00:00
|
|
|
{
|
2015-03-19 15:51:14 +00:00
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
2014-10-16 11:35:39 +00:00
|
|
|
|
2015-03-19 15:51:15 +00:00
|
|
|
return gb_gpio_direction_out_operation(ggc, (u8)offset, !!value);
|
2014-08-12 04:00:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:54:51 +00:00
|
|
|
static int gb_gpio_get(struct gpio_chip *chip, unsigned int offset)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
2015-03-19 15:51:14 +00:00
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
2014-10-16 11:35:39 +00:00
|
|
|
u8 which;
|
|
|
|
int ret;
|
|
|
|
|
2014-10-20 10:39:45 +00:00
|
|
|
which = (u8)offset;
|
2015-03-19 15:51:14 +00:00
|
|
|
ret = gb_gpio_get_value_operation(ggc, which);
|
2014-10-16 11:35:39 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-03-19 15:51:15 +00:00
|
|
|
|
2015-03-19 15:51:14 +00:00
|
|
|
return ggc->lines[which].value;
|
2014-10-16 11:35:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:54:51 +00:00
|
|
|
static void gb_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
|
2014-08-12 04:00:16 +00:00
|
|
|
{
|
2015-03-19 15:51:14 +00:00
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
2014-10-16 11:35:39 +00:00
|
|
|
|
2015-03-19 15:51:15 +00:00
|
|
|
gb_gpio_set_value_operation(ggc, (u8)offset, !!value);
|
2014-08-12 04:00:16 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 20:14:01 +00:00
|
|
|
static int gb_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
|
2017-01-23 12:34:34 +00:00
|
|
|
unsigned long config)
|
2014-08-12 04:00:16 +00:00
|
|
|
{
|
2015-03-19 15:51:14 +00:00
|
|
|
struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
|
2017-01-23 12:34:34 +00:00
|
|
|
u32 debounce;
|
2014-10-16 11:35:39 +00:00
|
|
|
|
2017-01-23 12:34:34 +00:00
|
|
|
if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
|
|
|
debounce = pinconf_to_config_argument(config);
|
2015-03-19 15:51:09 +00:00
|
|
|
if (debounce > U16_MAX)
|
2014-10-16 11:35:39 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2017-01-23 12:34:34 +00:00
|
|
|
return gb_gpio_set_debounce_operation(ggc, (u8)offset, (u16)debounce);
|
2014-10-16 11:35:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 15:51:14 +00:00
|
|
|
static int gb_gpio_controller_setup(struct gb_gpio_controller *ggc)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Now find out how many lines there are */
|
2015-03-19 15:51:14 +00:00
|
|
|
ret = gb_gpio_line_count_operation(ggc);
|
2014-10-16 11:35:39 +00:00
|
|
|
if (ret)
|
2015-03-19 15:51:15 +00:00
|
|
|
return ret;
|
|
|
|
|
2015-03-19 15:51:17 +00:00
|
|
|
ggc->lines = kcalloc(ggc->line_max + 1, sizeof(*ggc->lines),
|
|
|
|
GFP_KERNEL);
|
2015-03-19 15:51:14 +00:00
|
|
|
if (!ggc->lines)
|
2014-10-16 11:35:39 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
static int gb_gpio_probe(struct gbphy_device *gbphy_dev,
|
|
|
|
const struct gbphy_device_id *id)
|
2014-10-16 11:35:39 +00:00
|
|
|
{
|
2016-05-05 09:02:33 +00:00
|
|
|
struct gb_connection *connection;
|
2015-03-19 15:51:14 +00:00
|
|
|
struct gb_gpio_controller *ggc;
|
2014-08-12 04:00:16 +00:00
|
|
|
struct gpio_chip *gpio;
|
2020-07-22 07:44:14 +00:00
|
|
|
struct gpio_irq_chip *girq;
|
2015-02-17 15:48:23 +00:00
|
|
|
struct irq_chip *irqc;
|
2014-10-16 11:35:39 +00:00
|
|
|
int ret;
|
2014-08-12 04:00:16 +00:00
|
|
|
|
2015-03-19 15:51:14 +00:00
|
|
|
ggc = kzalloc(sizeof(*ggc), GFP_KERNEL);
|
|
|
|
if (!ggc)
|
2014-08-12 04:00:16 +00:00
|
|
|
return -ENOMEM;
|
2016-05-05 09:02:33 +00:00
|
|
|
|
2018-11-25 16:58:15 +00:00
|
|
|
connection =
|
|
|
|
gb_connection_create(gbphy_dev->bundle,
|
|
|
|
le16_to_cpu(gbphy_dev->cport_desc->id),
|
|
|
|
gb_gpio_request_handler);
|
2016-05-05 09:02:33 +00:00
|
|
|
if (IS_ERR(connection)) {
|
|
|
|
ret = PTR_ERR(connection);
|
|
|
|
goto exit_ggc_free;
|
|
|
|
}
|
|
|
|
|
2015-03-19 15:51:14 +00:00
|
|
|
ggc->connection = connection;
|
2016-03-22 18:30:35 +00:00
|
|
|
gb_connection_set_data(connection, ggc);
|
2016-05-19 15:52:39 +00:00
|
|
|
ggc->gbphy_dev = gbphy_dev;
|
|
|
|
gb_gbphy_set_data(gbphy_dev, ggc);
|
2016-05-05 09:02:33 +00:00
|
|
|
|
|
|
|
ret = gb_connection_enable_tx(connection);
|
|
|
|
if (ret)
|
|
|
|
goto exit_connection_destroy;
|
|
|
|
|
2015-03-19 15:51:14 +00:00
|
|
|
ret = gb_gpio_controller_setup(ggc);
|
2014-10-16 11:35:39 +00:00
|
|
|
if (ret)
|
2016-05-05 09:02:33 +00:00
|
|
|
goto exit_connection_disable;
|
2014-08-12 04:00:16 +00:00
|
|
|
|
2015-03-19 15:51:14 +00:00
|
|
|
irqc = &ggc->irqc;
|
2015-05-26 13:29:23 +00:00
|
|
|
irqc->irq_mask = gb_gpio_irq_mask;
|
|
|
|
irqc->irq_unmask = gb_gpio_irq_unmask;
|
2015-02-17 15:48:23 +00:00
|
|
|
irqc->irq_set_type = gb_gpio_irq_set_type;
|
2015-05-26 13:29:24 +00:00
|
|
|
irqc->irq_bus_lock = gb_gpio_irq_bus_lock;
|
|
|
|
irqc->irq_bus_sync_unlock = gb_gpio_irq_bus_sync_unlock;
|
2015-02-17 15:48:23 +00:00
|
|
|
irqc->name = "greybus_gpio";
|
|
|
|
|
2015-05-26 13:29:24 +00:00
|
|
|
mutex_init(&ggc->irq_lock);
|
|
|
|
|
2015-03-19 15:51:14 +00:00
|
|
|
gpio = &ggc->chip;
|
2014-08-12 04:00:16 +00:00
|
|
|
|
|
|
|
gpio->label = "greybus_gpio";
|
2016-05-19 15:52:39 +00:00
|
|
|
gpio->parent = &gbphy_dev->dev;
|
2015-03-19 15:51:10 +00:00
|
|
|
gpio->owner = THIS_MODULE;
|
2014-10-16 11:35:39 +00:00
|
|
|
|
|
|
|
gpio->request = gb_gpio_request;
|
|
|
|
gpio->free = gb_gpio_free;
|
|
|
|
gpio->get_direction = gb_gpio_get_direction;
|
|
|
|
gpio->direction_input = gb_gpio_direction_input;
|
|
|
|
gpio->direction_output = gb_gpio_direction_output;
|
|
|
|
gpio->get = gb_gpio_get;
|
|
|
|
gpio->set = gb_gpio_set;
|
2017-01-23 12:34:34 +00:00
|
|
|
gpio->set_config = gb_gpio_set_config;
|
2014-10-16 11:35:39 +00:00
|
|
|
gpio->base = -1; /* Allocate base dynamically */
|
2015-03-19 15:51:14 +00:00
|
|
|
gpio->ngpio = ggc->line_max + 1;
|
2015-03-19 15:51:10 +00:00
|
|
|
gpio->can_sleep = true;
|
2014-08-12 04:00:16 +00:00
|
|
|
|
2020-07-22 07:44:14 +00:00
|
|
|
girq = &gpio->irq;
|
|
|
|
girq->chip = irqc;
|
|
|
|
/* The event comes from the outside so no parent handler */
|
|
|
|
girq->parent_handler = NULL;
|
|
|
|
girq->num_parents = 0;
|
|
|
|
girq->parents = NULL;
|
|
|
|
girq->default_type = IRQ_TYPE_NONE;
|
|
|
|
girq->handler = handle_level_irq;
|
|
|
|
|
2016-05-05 09:02:33 +00:00
|
|
|
ret = gb_connection_enable(connection);
|
|
|
|
if (ret)
|
|
|
|
goto exit_line_free;
|
|
|
|
|
2019-01-14 14:56:16 +00:00
|
|
|
ret = gpiochip_add(gpio);
|
2014-10-16 11:35:39 +00:00
|
|
|
if (ret) {
|
2019-01-14 14:56:16 +00:00
|
|
|
dev_err(&gbphy_dev->dev, "failed to add gpio chip: %d\n", ret);
|
2016-05-05 09:02:33 +00:00
|
|
|
goto exit_line_free;
|
2015-02-17 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 20:13:00 +00:00
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
2014-08-12 04:00:16 +00:00
|
|
|
return 0;
|
2015-02-17 15:48:23 +00:00
|
|
|
|
2016-05-05 09:02:33 +00:00
|
|
|
exit_line_free:
|
2015-03-19 15:51:14 +00:00
|
|
|
kfree(ggc->lines);
|
2016-05-05 09:02:33 +00:00
|
|
|
exit_connection_disable:
|
|
|
|
gb_connection_disable(connection);
|
|
|
|
exit_connection_destroy:
|
|
|
|
gb_connection_destroy(connection);
|
|
|
|
exit_ggc_free:
|
2015-03-19 15:51:14 +00:00
|
|
|
kfree(ggc);
|
2014-10-16 11:35:39 +00:00
|
|
|
return ret;
|
2014-08-12 04:00:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
static void gb_gpio_remove(struct gbphy_device *gbphy_dev)
|
2014-08-12 04:00:16 +00:00
|
|
|
{
|
2016-05-19 15:52:39 +00:00
|
|
|
struct gb_gpio_controller *ggc = gb_gbphy_get_data(gbphy_dev);
|
2016-05-05 09:02:33 +00:00
|
|
|
struct gb_connection *connection = ggc->connection;
|
2016-07-14 20:13:00 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
gbphy_runtime_get_noresume(gbphy_dev);
|
2014-08-12 04:00:16 +00:00
|
|
|
|
2016-05-05 09:02:33 +00:00
|
|
|
gb_connection_disable_rx(connection);
|
2016-09-08 13:52:06 +00:00
|
|
|
gpiochip_remove(&ggc->chip);
|
2016-05-05 09:02:33 +00:00
|
|
|
gb_connection_disable(connection);
|
|
|
|
gb_connection_destroy(connection);
|
2015-03-19 15:51:14 +00:00
|
|
|
kfree(ggc->lines);
|
|
|
|
kfree(ggc);
|
2014-08-12 04:00:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
static const struct gbphy_device_id gb_gpio_id_table[] = {
|
|
|
|
{ GBPHY_PROTOCOL(GREYBUS_PROTOCOL_GPIO) },
|
2016-05-05 09:02:33 +00:00
|
|
|
{ },
|
2014-11-05 22:12:53 +00:00
|
|
|
};
|
2016-05-19 15:52:39 +00:00
|
|
|
MODULE_DEVICE_TABLE(gbphy, gb_gpio_id_table);
|
2014-11-05 22:12:53 +00:00
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
static struct gbphy_driver gpio_driver = {
|
2016-05-05 09:02:33 +00:00
|
|
|
.name = "gpio",
|
|
|
|
.probe = gb_gpio_probe,
|
|
|
|
.remove = gb_gpio_remove,
|
|
|
|
.id_table = gb_gpio_id_table,
|
|
|
|
};
|
2016-05-09 12:45:06 +00:00
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
module_gbphy_driver(gpio_driver);
|
2016-05-09 12:45:06 +00:00
|
|
|
MODULE_LICENSE("GPL v2");
|