| 1 |
// SPDX-License-Identifier: GPL-2.0+ |
| 2 |
/* |
| 3 |
* Copyright (C) 2019 Bootlin |
| 4 |
* Author: Paul Kocialkowski <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/err.h> |
| 8 |
#include <linux/gpio/driver.h> |
| 9 |
#include <linux/module.h> |
| 10 |
#include <linux/of.h> |
| 11 |
#include <linux/of_address.h> |
| 12 |
#include <linux/platform_device.h> |
| 13 |
#include <linux/regmap.h> |
| 14 |
#include <linux/mfd/syscon.h> |
| 15 |
|
| 16 |
#define LOGICVC_CTRL_REG 0x40 |
| 17 |
#define LOGICVC_CTRL_GPIO_SHIFT 11 |
| 18 |
#define LOGICVC_CTRL_GPIO_BITS 5 |
| 19 |
|
| 20 |
#define LOGICVC_POWER_CTRL_REG 0x78 |
| 21 |
#define LOGICVC_POWER_CTRL_GPIO_SHIFT 0 |
| 22 |
#define LOGICVC_POWER_CTRL_GPIO_BITS 4 |
| 23 |
|
| 24 |
struct logicvc_gpio { |
| 25 |
struct gpio_chip chip; |
| 26 |
struct regmap *regmap; |
| 27 |
}; |
| 28 |
|
| 29 |
static void logicvc_gpio_offset(struct logicvc_gpio *logicvc, unsigned offset, |
| 30 |
unsigned int *reg, unsigned int *bit) |
| 31 |
{ |
| 32 |
if (offset >= LOGICVC_CTRL_GPIO_BITS) { |
| 33 |
*reg = LOGICVC_POWER_CTRL_REG; |
| 34 |
|
| 35 |
/* To the (virtual) power ctrl offset. */ |
| 36 |
offset -= LOGICVC_CTRL_GPIO_BITS; |
| 37 |
/* To the actual bit offset in reg. */ |
| 38 |
offset += LOGICVC_POWER_CTRL_GPIO_SHIFT; |
| 39 |
} else { |
| 40 |
*reg = LOGICVC_CTRL_REG; |
| 41 |
|
| 42 |
/* To the actual bit offset in reg. */ |
| 43 |
offset += LOGICVC_CTRL_GPIO_SHIFT; |
| 44 |
} |
| 45 |
|
| 46 |
*bit = BIT(offset); |
| 47 |
} |
| 48 |
|
| 49 |
static int logicvc_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 50 |
{ |
| 51 |
struct logicvc_gpio *logicvc = gpiochip_get_data(chip); |
| 52 |
unsigned int reg, bit, value; |
| 53 |
int ret; |
| 54 |
|
| 55 |
logicvc_gpio_offset(logicvc, offset, ®, &bit); |
| 56 |
|
| 57 |
ret = regmap_read(logicvc->regmap, reg, &value); |
| 58 |
if (ret) |
| 59 |
return ret; |
| 60 |
|
| 61 |
return !!(value & bit); |
| 62 |
} |
| 63 |
|
| 64 |
static int logicvc_gpio_set(struct gpio_chip *chip, unsigned int offset, |
| 65 |
int value) |
| 66 |
{ |
| 67 |
struct logicvc_gpio *logicvc = gpiochip_get_data(chip); |
| 68 |
unsigned int reg, bit; |
| 69 |
|
| 70 |
logicvc_gpio_offset(logicvc, offset, ®, &bit); |
| 71 |
|
| 72 |
return regmap_update_bits(logicvc->regmap, reg, bit, value ? bit : 0); |
| 73 |
} |
| 74 |
|
| 75 |
static int logicvc_gpio_direction_output(struct gpio_chip *chip, |
| 76 |
unsigned offset, int value) |
| 77 |
{ |
| 78 |
/* Pins are always configured as output, so just set the value. */ |
| 79 |
return logicvc_gpio_set(chip, offset, value); |
| 80 |
} |
| 81 |
|
| 82 |
static struct regmap_config logicvc_gpio_regmap_config = { |
| 83 |
.reg_bits = 32, |
| 84 |
.val_bits = 32, |
| 85 |
.reg_stride = 4, |
| 86 |
.name = "logicvc-gpio", |
| 87 |
}; |
| 88 |
|
| 89 |
static int logicvc_gpio_probe(struct platform_device *pdev) |
| 90 |
{ |
| 91 |
struct device *dev = &pdev->dev; |
| 92 |
struct device_node *of_node = dev->of_node; |
| 93 |
struct logicvc_gpio *logicvc; |
| 94 |
int ret; |
| 95 |
|
| 96 |
logicvc = devm_kzalloc(dev, sizeof(*logicvc), GFP_KERNEL); |
| 97 |
if (!logicvc) |
| 98 |
return -ENOMEM; |
| 99 |
|
| 100 |
/* Try to get regmap from parent first. */ |
| 101 |
logicvc->regmap = syscon_node_to_regmap(of_node->parent); |
| 102 |
|
| 103 |
/* Grab our own regmap if that fails. */ |
| 104 |
if (IS_ERR(logicvc->regmap)) { |
| 105 |
struct resource res; |
| 106 |
void __iomem *base; |
| 107 |
|
| 108 |
ret = of_address_to_resource(of_node, 0, &res); |
| 109 |
if (ret) { |
| 110 |
dev_err(dev, "Failed to get resource from address\n"); |
| 111 |
return ret; |
| 112 |
} |
| 113 |
|
| 114 |
base = devm_ioremap_resource(dev, &res); |
| 115 |
if (IS_ERR(base)) |
| 116 |
return PTR_ERR(base); |
| 117 |
|
| 118 |
logicvc_gpio_regmap_config.max_register = resource_size(&res) - |
| 119 |
logicvc_gpio_regmap_config.reg_stride; |
| 120 |
|
| 121 |
logicvc->regmap = |
| 122 |
devm_regmap_init_mmio(dev, base, |
| 123 |
&logicvc_gpio_regmap_config); |
| 124 |
if (IS_ERR(logicvc->regmap)) { |
| 125 |
dev_err(dev, "Failed to create regmap for I/O\n"); |
| 126 |
return PTR_ERR(logicvc->regmap); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
logicvc->chip.parent = dev; |
| 131 |
logicvc->chip.owner = THIS_MODULE; |
| 132 |
logicvc->chip.label = dev_name(dev); |
| 133 |
logicvc->chip.base = -1; |
| 134 |
logicvc->chip.ngpio = LOGICVC_CTRL_GPIO_BITS + |
| 135 |
LOGICVC_POWER_CTRL_GPIO_BITS; |
| 136 |
logicvc->chip.get = logicvc_gpio_get; |
| 137 |
logicvc->chip.set = logicvc_gpio_set; |
| 138 |
logicvc->chip.direction_output = logicvc_gpio_direction_output; |
| 139 |
|
| 140 |
return devm_gpiochip_add_data(dev, &logicvc->chip, logicvc); |
| 141 |
} |
| 142 |
|
| 143 |
static const struct of_device_id logicivc_gpio_of_table[] = { |
| 144 |
{ |
| 145 |
.compatible = "xylon,logicvc-3.02.a-gpio", |
| 146 |
}, |
| 147 |
{ } |
| 148 |
}; |
| 149 |
|
| 150 |
MODULE_DEVICE_TABLE(of, logicivc_gpio_of_table); |
| 151 |
|
| 152 |
static struct platform_driver logicvc_gpio_driver = { |
| 153 |
.driver = { |
| 154 |
.name = "gpio-logicvc", |
| 155 |
.of_match_table = logicivc_gpio_of_table, |
| 156 |
}, |
| 157 |
.probe = logicvc_gpio_probe, |
| 158 |
}; |
| 159 |
|
| 160 |
module_platform_driver(logicvc_gpio_driver); |
| 161 |
|
| 162 |
MODULE_AUTHOR("Paul Kocialkowski <[email protected]>"); |
| 163 |
MODULE_DESCRIPTION("Xylon LogiCVC GPIO driver"); |
| 164 |
MODULE_LICENSE("GPL"); |