Skip to content

Commit

Permalink
Merge pull request gschorcht#17 from mskalski/kernel-5.x-backport
Browse files Browse the repository at this point in the history
Enable compile for 5.x Linux kernel
  • Loading branch information
frank-zago authored Oct 4, 2023
2 parents e90d230 + 651892a commit 4cdf57f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
33 changes: 33 additions & 0 deletions gpio-ch341.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <linux/platform_device.h>
#include <linux/types.h>
#include <linux/usb.h>
#include <linux/version.h>

static const struct mfd_cell ch341_gpio_devs[] = {
{ .name = "ch341-spi", },
Expand Down Expand Up @@ -68,6 +69,38 @@ static const u16 pin_can_output = 0b111111;
/* Only GPIO 10 (INT# line) has hardware interrupt */
#define CH341_GPIO_INT_LINE 10

/* this macro/inline function are available since linux 5.19 */
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,19,0)

static int gpiochip_irq_reqres(struct irq_data *d)
{
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
return gpiochip_reqres_irq(gc, d->hwirq);
}

static void gpiochip_irq_relres(struct irq_data *d)
{
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
gpiochip_relres_irq(gc, d->hwirq);
}

# define GPIOCHIP_IRQ_RESOURCE_HELPERS \
.irq_request_resources = gpiochip_irq_reqres, \
.irq_release_resources = gpiochip_irq_relres

static inline void gpio_irq_chip_set_chip(struct gpio_irq_chip *girq,
const struct irq_chip *chip)
{
/* Yes, dropping const is ugly, but it isn't like we have a choice */
girq->chip = (struct irq_chip *)chip;
}

/* Flag not supported before 5.19 */
#define IRQCHIP_IMMUTABLE 0

#endif


/* Send a command and get a reply if requested */
static int gpio_transfer(struct ch341_gpio *dev, int out_len, int in_len)
{
Expand Down
16 changes: 16 additions & 0 deletions i2c-ch341.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <linux/platform_device.h>
#include <linux/types.h>
#include <linux/usb.h>
#include <linux/version.h>

/* I2C bus speed. Speed selection is not implemented. */
#define CH341_I2C_20KHZ 0
Expand Down Expand Up @@ -249,6 +250,13 @@ static const struct i2c_adapter_quirks ch341_i2c_quirks = {
.max_read_len = MAX_RD_LENGTH,
};

#if LINUX_VERSION_CODE < KERNEL_VERSION(5,13,0)
static void devm_i2c_del_adapter(void *adapter)
{
i2c_del_adapter(adapter);
}
#endif

static int ch341_i2c_probe(struct platform_device *pdev)
{
struct ch341_ddata *ddata = dev_get_drvdata(pdev->dev.parent);
Expand Down Expand Up @@ -292,7 +300,15 @@ static int ch341_i2c_probe(struct platform_device *pdev)
return ret;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,13,0)
return devm_i2c_add_adapter(&pdev->dev, &ch341_i2c->adapter);
#else
ret = i2c_add_adapter(&ch341_i2c->adapter);
if (ret < 0)
return ret;

return devm_add_action_or_reset(&pdev->dev, devm_i2c_del_adapter, &ch341_i2c->adapter);
#endif
}

static struct platform_driver ch341_i2c_driver = {
Expand Down

0 comments on commit 4cdf57f

Please sign in to comment.