| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* GPIO Greybus driver. |
| 4 |
* |
| 5 |
* Copyright 2014 Google Inc. |
| 6 |
* Copyright 2014 Linaro Ltd. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <linux/kernel.h> |
| 10 |
#include <linux/module.h> |
| 11 |
#include <linux/slab.h> |
| 12 |
#include <linux/irq.h> |
| 13 |
#include <linux/irqdomain.h> |
| 14 |
#include <linux/gpio/driver.h> |
| 15 |
#include <linux/mutex.h> |
| 16 |
#include <linux/greybus.h> |
| 17 |
|
| 18 |
#include "gbphy.h" |
| 19 |
|
| 20 |
struct gb_gpio_line { |
| 21 |
/* The following has to be an array of line_max entries */ |
| 22 |
/* --> make them just a flags field */ |
| 23 |
u8 active: 1, |
| 24 |
direction: 1, /* 0 = output, 1 = input */ |
| 25 |
value: 1; /* 0 = low, 1 = high */ |
| 26 |
u16 debounce_usec; |
| 27 |
|
| 28 |
u8 irq_type; |
| 29 |
bool irq_type_pending; |
| 30 |
bool masked; |
| 31 |
bool masked_pending; |
| 32 |
}; |
| 33 |
|
| 34 |
struct gb_gpio_controller { |
| 35 |
struct gbphy_device *gbphy_dev; |
| 36 |
struct gb_connection *connection; |
| 37 |
u8 line_max; /* max line number */ |
| 38 |
struct gb_gpio_line *lines; |
| 39 |
|
| 40 |
struct gpio_chip chip; |
| 41 |
struct irq_chip irqc; |
| 42 |
struct mutex irq_lock; |
| 43 |
}; |
| 44 |
|
| 45 |
static struct gpio_chip *irq_data_to_gpio_chip(struct irq_data *d) |
| 46 |
{ |
| 47 |
return d->domain->host_data; |
| 48 |
} |
| 49 |
|
| 50 |
static int gb_gpio_line_count_operation(struct gb_gpio_controller *ggc) |
| 51 |
{ |
| 52 |
struct gb_gpio_line_count_response response; |
| 53 |
int ret; |
| 54 |
|
| 55 |
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_LINE_COUNT, |
| 56 |
NULL, 0, &response, sizeof(response)); |
| 57 |
if (!ret) |
| 58 |
ggc->line_max = response.count; |
| 59 |
return ret; |
| 60 |
} |
| 61 |
|
| 62 |
static int gb_gpio_activate_operation(struct gb_gpio_controller *ggc, u8 which) |
| 63 |
{ |
| 64 |
struct gb_gpio_activate_request request; |
| 65 |
struct gbphy_device *gbphy_dev = ggc->gbphy_dev; |
| 66 |
int ret; |
| 67 |
|
| 68 |
ret = gbphy_runtime_get_sync(gbphy_dev); |
| 69 |
if (ret) |
| 70 |
return ret; |
| 71 |
|
| 72 |
request.which = which; |
| 73 |
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE, |
| 74 |
&request, sizeof(request), NULL, 0); |
| 75 |
if (ret) { |
| 76 |
gbphy_runtime_put_autosuspend(gbphy_dev); |
| 77 |
return ret; |
| 78 |
} |
| 79 |
|
| 80 |
ggc->lines[which].active = true; |
| 81 |
|
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
static void gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc, |
| 86 |
u8 which) |
| 87 |
{ |
| 88 |
struct gbphy_device *gbphy_dev = ggc->gbphy_dev; |
| 89 |
struct device *dev = &gbphy_dev->dev; |
| 90 |
struct gb_gpio_deactivate_request request; |
| 91 |
int ret; |
| 92 |
|
| 93 |
request.which = which; |
| 94 |
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE, |
| 95 |
&request, sizeof(request), NULL, 0); |
| 96 |
if (ret) { |
| 97 |
dev_err(dev, "failed to deactivate gpio %u\n", which); |
| 98 |
goto out_pm_put; |
| 99 |
} |
| 100 |
|
| 101 |
ggc->lines[which].active = false; |
| 102 |
|
| 103 |
out_pm_put: |
| 104 |
gbphy_runtime_put_autosuspend(gbphy_dev); |
| 105 |
} |
| 106 |
|
| 107 |
static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc, |
| 108 |
u8 which) |
| 109 |
{ |
| 110 |
struct device *dev = &ggc->gbphy_dev->dev; |
| 111 |
struct gb_gpio_get_direction_request request; |
| 112 |
struct gb_gpio_get_direction_response response; |
| 113 |
int ret; |
| 114 |
u8 direction; |
| 115 |
|
| 116 |
request.which = which; |
| 117 |
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION, |
| 118 |
&request, sizeof(request), |
| 119 |
&response, sizeof(response)); |
| 120 |
if (ret) |
| 121 |
return ret; |
| 122 |
|
| 123 |
direction = response.direction; |
| 124 |
if (direction && direction != 1) { |
| 125 |
dev_warn(dev, "gpio %u direction was %u (should be 0 or 1)\n", |
| 126 |
which, direction); |
| 127 |
} |
| 128 |
ggc->lines[which].direction = direction ? 1 : 0; |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc, |
| 133 |
u8 which) |
| 134 |
{ |
| 135 |
struct gb_gpio_direction_in_request request; |
| 136 |
int ret; |
| 137 |
|
| 138 |
request.which = which; |
| 139 |
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN, |
| 140 |
&request, sizeof(request), NULL, 0); |
| 141 |
if (!ret) |
| 142 |
ggc->lines[which].direction = 1; |
| 143 |
return ret; |
| 144 |
} |
| 145 |
|
| 146 |
static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc, |
| 147 |
u8 which, bool value_high) |
| 148 |
{ |
| 149 |
struct gb_gpio_direction_out_request request; |
| 150 |
int ret; |
| 151 |
|
| 152 |
request.which = which; |
| 153 |
request.value = value_high ? 1 : 0; |
| 154 |
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_OUT, |
| 155 |
&request, sizeof(request), NULL, 0); |
| 156 |
if (!ret) |
| 157 |
ggc->lines[which].direction = 0; |
| 158 |
return ret; |
| 159 |
} |
| 160 |
|
| 161 |
static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc, |
| 162 |
u8 which) |
| 163 |
{ |
| 164 |
struct device *dev = &ggc->gbphy_dev->dev; |
| 165 |
struct gb_gpio_get_value_request request; |
| 166 |
struct gb_gpio_get_value_response response; |
| 167 |
int ret; |
| 168 |
u8 value; |
| 169 |
|
| 170 |
request.which = which; |
| 171 |
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE, |
| 172 |
&request, sizeof(request), |
| 173 |
&response, sizeof(response)); |
| 174 |
if (ret) { |
| 175 |
dev_err(dev, "failed to get value of gpio %u\n", which); |
| 176 |
return ret; |
| 177 |
} |
| 178 |
|
| 179 |
value = response.value; |
| 180 |
if (value && value != 1) { |
| 181 |
dev_warn(dev, "gpio %u value was %u (should be 0 or 1)\n", |
| 182 |
which, value); |
| 183 |
} |
| 184 |
ggc->lines[which].value = value ? 1 : 0; |
| 185 |
return 0; |
| 186 |
} |
| 187 |
|
| 188 |
static int gb_gpio_set_value_operation(struct gb_gpio_controller *ggc, |
| 189 |
u8 which, bool value_high) |
| 190 |
{ |
| 191 |
struct device *dev = &ggc->gbphy_dev->dev; |
| 192 |
struct gb_gpio_set_value_request request; |
| 193 |
int ret; |
| 194 |
|
| 195 |
request.which = which; |
| 196 |
request.value = value_high ? 1 : 0; |
| 197 |
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_VALUE, |
| 198 |
&request, sizeof(request), NULL, 0); |
| 199 |
if (ret) { |
| 200 |
dev_err(dev, "failed to set value of gpio %u\n", which); |
| 201 |
return ret; |
| 202 |
} |
| 203 |
|
| 204 |
ggc->lines[which].value = request.value; |
| 205 |
|
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc, |
| 210 |
u8 which, u16 debounce_usec) |
| 211 |
{ |
| 212 |
struct gb_gpio_set_debounce_request request; |
| 213 |
int ret; |
| 214 |
|
| 215 |
request.which = which; |
| 216 |
request.usec = cpu_to_le16(debounce_usec); |
| 217 |
ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_DEBOUNCE, |
| 218 |
&request, sizeof(request), NULL, 0); |
| 219 |
if (!ret) |
| 220 |
ggc->lines[which].debounce_usec = debounce_usec; |
| 221 |
return ret; |
| 222 |
} |
| 223 |
|
| 224 |
static void _gb_gpio_irq_mask(struct gb_gpio_controller *ggc, u8 hwirq) |
| 225 |
{ |
| 226 |
struct device *dev = &ggc->gbphy_dev->dev; |
| 227 |
struct gb_gpio_irq_mask_request request; |
| 228 |
int ret; |
| 229 |
|
| 230 |
request.which = hwirq; |
| 231 |
ret = gb_operation_sync(ggc->connection, |
| 232 |
GB_GPIO_TYPE_IRQ_MASK, |
| 233 |
&request, sizeof(request), NULL, 0); |
| 234 |
if (ret) |
| 235 |
dev_err(dev, "failed to mask irq: %d\n", ret); |
| 236 |
} |
| 237 |
|
| 238 |
static void _gb_gpio_irq_unmask(struct gb_gpio_controller *ggc, u8 hwirq) |
| 239 |
{ |
| 240 |
struct device *dev = &ggc->gbphy_dev->dev; |
| 241 |
struct gb_gpio_irq_unmask_request request; |
| 242 |
int ret; |
| 243 |
|
| 244 |
request.which = hwirq; |
| 245 |
ret = gb_operation_sync(ggc->connection, |
| 246 |
GB_GPIO_TYPE_IRQ_UNMASK, |
| 247 |
&request, sizeof(request), NULL, 0); |
| 248 |
if (ret) |
| 249 |
dev_err(dev, "failed to unmask irq: %d\n", ret); |
| 250 |
} |
| 251 |
|
| 252 |
static void _gb_gpio_irq_set_type(struct gb_gpio_controller *ggc, |
| 253 |
u8 hwirq, u8 type) |
| 254 |
{ |
| 255 |
struct device *dev = &ggc->gbphy_dev->dev; |
| 256 |
struct gb_gpio_irq_type_request request; |
| 257 |
int ret; |
| 258 |
|
| 259 |
request.which = hwirq; |
| 260 |
request.type = type; |
| 261 |
|
| 262 |
ret = gb_operation_sync(ggc->connection, |
| 263 |
GB_GPIO_TYPE_IRQ_TYPE, |
| 264 |
&request, sizeof(request), NULL, 0); |
| 265 |
if (ret) |
| 266 |
dev_err(dev, "failed to set irq type: %d\n", ret); |
| 267 |
} |
| 268 |
|
| 269 |
static void gb_gpio_irq_mask(struct irq_data *d) |
| 270 |
{ |
| 271 |
struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 272 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 273 |
struct gb_gpio_line *line = &ggc->lines[d->hwirq]; |
| 274 |
|
| 275 |
line->masked = true; |
| 276 |
line->masked_pending = true; |
| 277 |
} |
| 278 |
|
| 279 |
static void gb_gpio_irq_unmask(struct irq_data *d) |
| 280 |
{ |
| 281 |
struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 282 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 283 |
struct gb_gpio_line *line = &ggc->lines[d->hwirq]; |
| 284 |
|
| 285 |
line->masked = false; |
| 286 |
line->masked_pending = true; |
| 287 |
} |
| 288 |
|
| 289 |
static int gb_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
| 290 |
{ |
| 291 |
struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 292 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 293 |
struct gb_gpio_line *line = &ggc->lines[d->hwirq]; |
| 294 |
struct device *dev = &ggc->gbphy_dev->dev; |
| 295 |
u8 irq_type; |
| 296 |
|
| 297 |
switch (type) { |
| 298 |
case IRQ_TYPE_NONE: |
| 299 |
irq_type = GB_GPIO_IRQ_TYPE_NONE; |
| 300 |
break; |
| 301 |
case IRQ_TYPE_EDGE_RISING: |
| 302 |
irq_type = GB_GPIO_IRQ_TYPE_EDGE_RISING; |
| 303 |
break; |
| 304 |
case IRQ_TYPE_EDGE_FALLING: |
| 305 |
irq_type = GB_GPIO_IRQ_TYPE_EDGE_FALLING; |
| 306 |
break; |
| 307 |
case IRQ_TYPE_EDGE_BOTH: |
| 308 |
irq_type = GB_GPIO_IRQ_TYPE_EDGE_BOTH; |
| 309 |
break; |
| 310 |
case IRQ_TYPE_LEVEL_LOW: |
| 311 |
irq_type = GB_GPIO_IRQ_TYPE_LEVEL_LOW; |
| 312 |
break; |
| 313 |
case IRQ_TYPE_LEVEL_HIGH: |
| 314 |
irq_type = GB_GPIO_IRQ_TYPE_LEVEL_HIGH; |
| 315 |
break; |
| 316 |
default: |
| 317 |
dev_err(dev, "unsupported irq type: %u\n", type); |
| 318 |
return -EINVAL; |
| 319 |
} |
| 320 |
|
| 321 |
line->irq_type = irq_type; |
| 322 |
line->irq_type_pending = true; |
| 323 |
|
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
static void gb_gpio_irq_bus_lock(struct irq_data *d) |
| 328 |
{ |
| 329 |
struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 330 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 331 |
|
| 332 |
mutex_lock(&ggc->irq_lock); |
| 333 |
} |
| 334 |
|
| 335 |
static void gb_gpio_irq_bus_sync_unlock(struct irq_data *d) |
| 336 |
{ |
| 337 |
struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 338 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 339 |
struct gb_gpio_line *line = &ggc->lines[d->hwirq]; |
| 340 |
|
| 341 |
if (line->irq_type_pending) { |
| 342 |
_gb_gpio_irq_set_type(ggc, d->hwirq, line->irq_type); |
| 343 |
line->irq_type_pending = false; |
| 344 |
} |
| 345 |
|
| 346 |
if (line->masked_pending) { |
| 347 |
if (line->masked) |
| 348 |
_gb_gpio_irq_mask(ggc, d->hwirq); |
| 349 |
else |
| 350 |
_gb_gpio_irq_unmask(ggc, d->hwirq); |
| 351 |
line->masked_pending = false; |
| 352 |
} |
| 353 |
|
| 354 |
mutex_unlock(&ggc->irq_lock); |
| 355 |
} |
| 356 |
|
| 357 |
static int gb_gpio_request_handler(struct gb_operation *op) |
| 358 |
{ |
| 359 |
struct gb_connection *connection = op->connection; |
| 360 |
struct gb_gpio_controller *ggc = gb_connection_get_data(connection); |
| 361 |
struct device *dev = &ggc->gbphy_dev->dev; |
| 362 |
struct gb_message *request; |
| 363 |
struct gb_gpio_irq_event_request *event; |
| 364 |
u8 type = op->type; |
| 365 |
int irq, ret; |
| 366 |
|
| 367 |
if (type != GB_GPIO_TYPE_IRQ_EVENT) { |
| 368 |
dev_err(dev, "unsupported unsolicited request: %u\n", type); |
| 369 |
return -EINVAL; |
| 370 |
} |
| 371 |
|
| 372 |
request = op->request; |
| 373 |
|
| 374 |
if (request->payload_size < sizeof(*event)) { |
| 375 |
dev_err(dev, "short event received (%zu < %zu)\n", |
| 376 |
request->payload_size, sizeof(*event)); |
| 377 |
return -EINVAL; |
| 378 |
} |
| 379 |
|
| 380 |
event = request->payload; |
| 381 |
if (event->which > ggc->line_max) { |
| 382 |
dev_err(dev, "invalid hw irq: %d\n", event->which); |
| 383 |
return -EINVAL; |
| 384 |
} |
| 385 |
|
| 386 |
irq = irq_find_mapping(ggc->chip.irq.domain, event->which); |
| 387 |
if (!irq) { |
| 388 |
dev_err(dev, "failed to find IRQ\n"); |
| 389 |
return -EINVAL; |
| 390 |
} |
| 391 |
|
| 392 |
ret = generic_handle_irq_safe(irq); |
| 393 |
if (ret) |
| 394 |
dev_err(dev, "failed to invoke irq handler\n"); |
| 395 |
|
| 396 |
return ret; |
| 397 |
} |
| 398 |
|
| 399 |
static int gb_gpio_request(struct gpio_chip *chip, unsigned int offset) |
| 400 |
{ |
| 401 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 402 |
|
| 403 |
return gb_gpio_activate_operation(ggc, (u8)offset); |
| 404 |
} |
| 405 |
|
| 406 |
static void gb_gpio_free(struct gpio_chip *chip, unsigned int offset) |
| 407 |
{ |
| 408 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 409 |
|
| 410 |
gb_gpio_deactivate_operation(ggc, (u8)offset); |
| 411 |
} |
| 412 |
|
| 413 |
static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned int offset) |
| 414 |
{ |
| 415 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 416 |
u8 which; |
| 417 |
int ret; |
| 418 |
|
| 419 |
which = (u8)offset; |
| 420 |
ret = gb_gpio_get_direction_operation(ggc, which); |
| 421 |
if (ret) |
| 422 |
return ret; |
| 423 |
|
| 424 |
return ggc->lines[which].direction ? 1 : 0; |
| 425 |
} |
| 426 |
|
| 427 |
static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned int offset) |
| 428 |
{ |
| 429 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 430 |
|
| 431 |
return gb_gpio_direction_in_operation(ggc, (u8)offset); |
| 432 |
} |
| 433 |
|
| 434 |
static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned int offset, |
| 435 |
int value) |
| 436 |
{ |
| 437 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 438 |
|
| 439 |
return gb_gpio_direction_out_operation(ggc, (u8)offset, !!value); |
| 440 |
} |
| 441 |
|
| 442 |
static int gb_gpio_get(struct gpio_chip *chip, unsigned int offset) |
| 443 |
{ |
| 444 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 445 |
u8 which; |
| 446 |
int ret; |
| 447 |
|
| 448 |
which = (u8)offset; |
| 449 |
ret = gb_gpio_get_value_operation(ggc, which); |
| 450 |
if (ret) |
| 451 |
return ret; |
| 452 |
|
| 453 |
return ggc->lines[which].value; |
| 454 |
} |
| 455 |
|
| 456 |
static int gb_gpio_set(struct gpio_chip *chip, unsigned int offset, int value) |
| 457 |
{ |
| 458 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 459 |
|
| 460 |
return gb_gpio_set_value_operation(ggc, (u8)offset, !!value); |
| 461 |
} |
| 462 |
|
| 463 |
static int gb_gpio_set_config(struct gpio_chip *chip, unsigned int offset, |
| 464 |
unsigned long config) |
| 465 |
{ |
| 466 |
struct gb_gpio_controller *ggc = gpiochip_get_data(chip); |
| 467 |
u32 debounce; |
| 468 |
|
| 469 |
if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE) |
| 470 |
return -ENOTSUPP; |
| 471 |
|
| 472 |
debounce = pinconf_to_config_argument(config); |
| 473 |
if (debounce > U16_MAX) |
| 474 |
return -EINVAL; |
| 475 |
|
| 476 |
return gb_gpio_set_debounce_operation(ggc, (u8)offset, (u16)debounce); |
| 477 |
} |
| 478 |
|
| 479 |
static int gb_gpio_controller_setup(struct gb_gpio_controller *ggc) |
| 480 |
{ |
| 481 |
int ret; |
| 482 |
|
| 483 |
/* Now find out how many lines there are */ |
| 484 |
ret = gb_gpio_line_count_operation(ggc); |
| 485 |
if (ret) |
| 486 |
return ret; |
| 487 |
|
| 488 |
ggc->lines = kzalloc_objs(*ggc->lines, ggc->line_max + 1); |
| 489 |
if (!ggc->lines) |
| 490 |
return -ENOMEM; |
| 491 |
|
| 492 |
return ret; |
| 493 |
} |
| 494 |
|
| 495 |
static int gb_gpio_probe(struct gbphy_device *gbphy_dev, |
| 496 |
const struct gbphy_device_id *id) |
| 497 |
{ |
| 498 |
struct gb_connection *connection; |
| 499 |
struct gb_gpio_controller *ggc; |
| 500 |
struct gpio_chip *gpio; |
| 501 |
struct gpio_irq_chip *girq; |
| 502 |
struct irq_chip *irqc; |
| 503 |
int ret; |
| 504 |
|
| 505 |
ggc = kzalloc_obj(*ggc); |
| 506 |
if (!ggc) |
| 507 |
return -ENOMEM; |
| 508 |
|
| 509 |
connection = |
| 510 |
gb_connection_create(gbphy_dev->bundle, |
| 511 |
le16_to_cpu(gbphy_dev->cport_desc->id), |
| 512 |
gb_gpio_request_handler); |
| 513 |
if (IS_ERR(connection)) { |
| 514 |
ret = PTR_ERR(connection); |
| 515 |
goto exit_ggc_free; |
| 516 |
} |
| 517 |
|
| 518 |
ggc->connection = connection; |
| 519 |
gb_connection_set_data(connection, ggc); |
| 520 |
ggc->gbphy_dev = gbphy_dev; |
| 521 |
gb_gbphy_set_data(gbphy_dev, ggc); |
| 522 |
|
| 523 |
ret = gb_connection_enable_tx(connection); |
| 524 |
if (ret) |
| 525 |
goto exit_connection_destroy; |
| 526 |
|
| 527 |
ret = gb_gpio_controller_setup(ggc); |
| 528 |
if (ret) |
| 529 |
goto exit_connection_disable; |
| 530 |
|
| 531 |
irqc = &ggc->irqc; |
| 532 |
irqc->irq_mask = gb_gpio_irq_mask; |
| 533 |
irqc->irq_unmask = gb_gpio_irq_unmask; |
| 534 |
irqc->irq_set_type = gb_gpio_irq_set_type; |
| 535 |
irqc->irq_bus_lock = gb_gpio_irq_bus_lock; |
| 536 |
irqc->irq_bus_sync_unlock = gb_gpio_irq_bus_sync_unlock; |
| 537 |
irqc->name = "greybus_gpio"; |
| 538 |
|
| 539 |
mutex_init(&ggc->irq_lock); |
| 540 |
|
| 541 |
gpio = &ggc->chip; |
| 542 |
|
| 543 |
gpio->label = "greybus_gpio"; |
| 544 |
gpio->parent = &gbphy_dev->dev; |
| 545 |
gpio->owner = THIS_MODULE; |
| 546 |
|
| 547 |
gpio->request = gb_gpio_request; |
| 548 |
gpio->free = gb_gpio_free; |
| 549 |
gpio->get_direction = gb_gpio_get_direction; |
| 550 |
gpio->direction_input = gb_gpio_direction_input; |
| 551 |
gpio->direction_output = gb_gpio_direction_output; |
| 552 |
gpio->get = gb_gpio_get; |
| 553 |
gpio->set = gb_gpio_set; |
| 554 |
gpio->set_config = gb_gpio_set_config; |
| 555 |
gpio->base = -1; /* Allocate base dynamically */ |
| 556 |
gpio->ngpio = ggc->line_max + 1; |
| 557 |
gpio->can_sleep = true; |
| 558 |
|
| 559 |
girq = &gpio->irq; |
| 560 |
girq->chip = irqc; |
| 561 |
/* The event comes from the outside so no parent handler */ |
| 562 |
girq->parent_handler = NULL; |
| 563 |
girq->num_parents = 0; |
| 564 |
girq->parents = NULL; |
| 565 |
girq->default_type = IRQ_TYPE_NONE; |
| 566 |
girq->handler = handle_level_irq; |
| 567 |
|
| 568 |
ret = gb_connection_enable(connection); |
| 569 |
if (ret) |
| 570 |
goto exit_line_free; |
| 571 |
|
| 572 |
ret = gpiochip_add_data(gpio, ggc); |
| 573 |
if (ret) { |
| 574 |
dev_err(&gbphy_dev->dev, "failed to add gpio chip: %d\n", ret); |
| 575 |
goto exit_line_free; |
| 576 |
} |
| 577 |
|
| 578 |
gbphy_runtime_put_autosuspend(gbphy_dev); |
| 579 |
return 0; |
| 580 |
|
| 581 |
exit_line_free: |
| 582 |
kfree(ggc->lines); |
| 583 |
exit_connection_disable: |
| 584 |
gb_connection_disable(connection); |
| 585 |
exit_connection_destroy: |
| 586 |
gb_connection_destroy(connection); |
| 587 |
exit_ggc_free: |
| 588 |
kfree(ggc); |
| 589 |
return ret; |
| 590 |
} |
| 591 |
|
| 592 |
static void gb_gpio_remove(struct gbphy_device *gbphy_dev) |
| 593 |
{ |
| 594 |
struct gb_gpio_controller *ggc = gb_gbphy_get_data(gbphy_dev); |
| 595 |
struct gb_connection *connection = ggc->connection; |
| 596 |
int ret; |
| 597 |
|
| 598 |
ret = gbphy_runtime_get_sync(gbphy_dev); |
| 599 |
if (ret) |
| 600 |
gbphy_runtime_get_noresume(gbphy_dev); |
| 601 |
|
| 602 |
gb_connection_disable_rx(connection); |
| 603 |
gpiochip_remove(&ggc->chip); |
| 604 |
gb_connection_disable(connection); |
| 605 |
gb_connection_destroy(connection); |
| 606 |
kfree(ggc->lines); |
| 607 |
kfree(ggc); |
| 608 |
} |
| 609 |
|
| 610 |
static const struct gbphy_device_id gb_gpio_id_table[] = { |
| 611 |
{ GBPHY_PROTOCOL(GREYBUS_PROTOCOL_GPIO) }, |
| 612 |
{ }, |
| 613 |
}; |
| 614 |
MODULE_DEVICE_TABLE(gbphy, gb_gpio_id_table); |
| 615 |
|
| 616 |
static struct gbphy_driver gpio_driver = { |
| 617 |
.name = "gpio", |
| 618 |
.probe = gb_gpio_probe, |
| 619 |
.remove = gb_gpio_remove, |
| 620 |
.id_table = gb_gpio_id_table, |
| 621 |
}; |
| 622 |
|
| 623 |
module_gbphy_driver(gpio_driver); |
| 624 |
MODULE_DESCRIPTION("GPIO Greybus driver"); |
| 625 |
MODULE_LICENSE("GPL v2"); |