0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/slab.h>
0012 #include <linux/irq.h>
0013 #include <linux/irqdomain.h>
0014 #include <linux/gpio/driver.h>
0015 #include <linux/mutex.h>
0016 #include <linux/greybus.h>
0017
0018 #include "gbphy.h"
0019
0020 struct gb_gpio_line {
0021
0022
0023 u8 active: 1,
0024 direction: 1,
0025 value: 1;
0026 u16 debounce_usec;
0027
0028 u8 irq_type;
0029 bool irq_type_pending;
0030 bool masked;
0031 bool masked_pending;
0032 };
0033
0034 struct gb_gpio_controller {
0035 struct gbphy_device *gbphy_dev;
0036 struct gb_connection *connection;
0037 u8 line_max;
0038 struct gb_gpio_line *lines;
0039
0040 struct gpio_chip chip;
0041 struct irq_chip irqc;
0042 struct mutex irq_lock;
0043 };
0044 #define gpio_chip_to_gb_gpio_controller(chip) \
0045 container_of(chip, struct gb_gpio_controller, chip)
0046 #define irq_data_to_gpio_chip(d) (d->domain->host_data)
0047
0048 static int gb_gpio_line_count_operation(struct gb_gpio_controller *ggc)
0049 {
0050 struct gb_gpio_line_count_response response;
0051 int ret;
0052
0053 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_LINE_COUNT,
0054 NULL, 0, &response, sizeof(response));
0055 if (!ret)
0056 ggc->line_max = response.count;
0057 return ret;
0058 }
0059
0060 static int gb_gpio_activate_operation(struct gb_gpio_controller *ggc, u8 which)
0061 {
0062 struct gb_gpio_activate_request request;
0063 struct gbphy_device *gbphy_dev = ggc->gbphy_dev;
0064 int ret;
0065
0066 ret = gbphy_runtime_get_sync(gbphy_dev);
0067 if (ret)
0068 return ret;
0069
0070 request.which = which;
0071 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE,
0072 &request, sizeof(request), NULL, 0);
0073 if (ret) {
0074 gbphy_runtime_put_autosuspend(gbphy_dev);
0075 return ret;
0076 }
0077
0078 ggc->lines[which].active = true;
0079
0080 return 0;
0081 }
0082
0083 static void gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc,
0084 u8 which)
0085 {
0086 struct gbphy_device *gbphy_dev = ggc->gbphy_dev;
0087 struct device *dev = &gbphy_dev->dev;
0088 struct gb_gpio_deactivate_request request;
0089 int ret;
0090
0091 request.which = which;
0092 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE,
0093 &request, sizeof(request), NULL, 0);
0094 if (ret) {
0095 dev_err(dev, "failed to deactivate gpio %u\n", which);
0096 goto out_pm_put;
0097 }
0098
0099 ggc->lines[which].active = false;
0100
0101 out_pm_put:
0102 gbphy_runtime_put_autosuspend(gbphy_dev);
0103 }
0104
0105 static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc,
0106 u8 which)
0107 {
0108 struct device *dev = &ggc->gbphy_dev->dev;
0109 struct gb_gpio_get_direction_request request;
0110 struct gb_gpio_get_direction_response response;
0111 int ret;
0112 u8 direction;
0113
0114 request.which = which;
0115 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION,
0116 &request, sizeof(request),
0117 &response, sizeof(response));
0118 if (ret)
0119 return ret;
0120
0121 direction = response.direction;
0122 if (direction && direction != 1) {
0123 dev_warn(dev, "gpio %u direction was %u (should be 0 or 1)\n",
0124 which, direction);
0125 }
0126 ggc->lines[which].direction = direction ? 1 : 0;
0127 return 0;
0128 }
0129
0130 static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc,
0131 u8 which)
0132 {
0133 struct gb_gpio_direction_in_request request;
0134 int ret;
0135
0136 request.which = which;
0137 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN,
0138 &request, sizeof(request), NULL, 0);
0139 if (!ret)
0140 ggc->lines[which].direction = 1;
0141 return ret;
0142 }
0143
0144 static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc,
0145 u8 which, bool value_high)
0146 {
0147 struct gb_gpio_direction_out_request request;
0148 int ret;
0149
0150 request.which = which;
0151 request.value = value_high ? 1 : 0;
0152 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_OUT,
0153 &request, sizeof(request), NULL, 0);
0154 if (!ret)
0155 ggc->lines[which].direction = 0;
0156 return ret;
0157 }
0158
0159 static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc,
0160 u8 which)
0161 {
0162 struct device *dev = &ggc->gbphy_dev->dev;
0163 struct gb_gpio_get_value_request request;
0164 struct gb_gpio_get_value_response response;
0165 int ret;
0166 u8 value;
0167
0168 request.which = which;
0169 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE,
0170 &request, sizeof(request),
0171 &response, sizeof(response));
0172 if (ret) {
0173 dev_err(dev, "failed to get value of gpio %u\n", which);
0174 return ret;
0175 }
0176
0177 value = response.value;
0178 if (value && value != 1) {
0179 dev_warn(dev, "gpio %u value was %u (should be 0 or 1)\n",
0180 which, value);
0181 }
0182 ggc->lines[which].value = value ? 1 : 0;
0183 return 0;
0184 }
0185
0186 static void gb_gpio_set_value_operation(struct gb_gpio_controller *ggc,
0187 u8 which, bool value_high)
0188 {
0189 struct device *dev = &ggc->gbphy_dev->dev;
0190 struct gb_gpio_set_value_request request;
0191 int ret;
0192
0193 if (ggc->lines[which].direction == 1) {
0194 dev_warn(dev, "refusing to set value of input gpio %u\n",
0195 which);
0196 return;
0197 }
0198
0199 request.which = which;
0200 request.value = value_high ? 1 : 0;
0201 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_VALUE,
0202 &request, sizeof(request), NULL, 0);
0203 if (ret) {
0204 dev_err(dev, "failed to set value of gpio %u\n", which);
0205 return;
0206 }
0207
0208 ggc->lines[which].value = request.value;
0209 }
0210
0211 static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc,
0212 u8 which, u16 debounce_usec)
0213 {
0214 struct gb_gpio_set_debounce_request request;
0215 int ret;
0216
0217 request.which = which;
0218 request.usec = cpu_to_le16(debounce_usec);
0219 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_DEBOUNCE,
0220 &request, sizeof(request), NULL, 0);
0221 if (!ret)
0222 ggc->lines[which].debounce_usec = debounce_usec;
0223 return ret;
0224 }
0225
0226 static void _gb_gpio_irq_mask(struct gb_gpio_controller *ggc, u8 hwirq)
0227 {
0228 struct device *dev = &ggc->gbphy_dev->dev;
0229 struct gb_gpio_irq_mask_request request;
0230 int ret;
0231
0232 request.which = hwirq;
0233 ret = gb_operation_sync(ggc->connection,
0234 GB_GPIO_TYPE_IRQ_MASK,
0235 &request, sizeof(request), NULL, 0);
0236 if (ret)
0237 dev_err(dev, "failed to mask irq: %d\n", ret);
0238 }
0239
0240 static void _gb_gpio_irq_unmask(struct gb_gpio_controller *ggc, u8 hwirq)
0241 {
0242 struct device *dev = &ggc->gbphy_dev->dev;
0243 struct gb_gpio_irq_unmask_request request;
0244 int ret;
0245
0246 request.which = hwirq;
0247 ret = gb_operation_sync(ggc->connection,
0248 GB_GPIO_TYPE_IRQ_UNMASK,
0249 &request, sizeof(request), NULL, 0);
0250 if (ret)
0251 dev_err(dev, "failed to unmask irq: %d\n", ret);
0252 }
0253
0254 static void _gb_gpio_irq_set_type(struct gb_gpio_controller *ggc,
0255 u8 hwirq, u8 type)
0256 {
0257 struct device *dev = &ggc->gbphy_dev->dev;
0258 struct gb_gpio_irq_type_request request;
0259 int ret;
0260
0261 request.which = hwirq;
0262 request.type = type;
0263
0264 ret = gb_operation_sync(ggc->connection,
0265 GB_GPIO_TYPE_IRQ_TYPE,
0266 &request, sizeof(request), NULL, 0);
0267 if (ret)
0268 dev_err(dev, "failed to set irq type: %d\n", ret);
0269 }
0270
0271 static void gb_gpio_irq_mask(struct irq_data *d)
0272 {
0273 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
0274 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0275 struct gb_gpio_line *line = &ggc->lines[d->hwirq];
0276
0277 line->masked = true;
0278 line->masked_pending = true;
0279 }
0280
0281 static void gb_gpio_irq_unmask(struct irq_data *d)
0282 {
0283 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
0284 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0285 struct gb_gpio_line *line = &ggc->lines[d->hwirq];
0286
0287 line->masked = false;
0288 line->masked_pending = true;
0289 }
0290
0291 static int gb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
0292 {
0293 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
0294 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0295 struct gb_gpio_line *line = &ggc->lines[d->hwirq];
0296 struct device *dev = &ggc->gbphy_dev->dev;
0297 u8 irq_type;
0298
0299 switch (type) {
0300 case IRQ_TYPE_NONE:
0301 irq_type = GB_GPIO_IRQ_TYPE_NONE;
0302 break;
0303 case IRQ_TYPE_EDGE_RISING:
0304 irq_type = GB_GPIO_IRQ_TYPE_EDGE_RISING;
0305 break;
0306 case IRQ_TYPE_EDGE_FALLING:
0307 irq_type = GB_GPIO_IRQ_TYPE_EDGE_FALLING;
0308 break;
0309 case IRQ_TYPE_EDGE_BOTH:
0310 irq_type = GB_GPIO_IRQ_TYPE_EDGE_BOTH;
0311 break;
0312 case IRQ_TYPE_LEVEL_LOW:
0313 irq_type = GB_GPIO_IRQ_TYPE_LEVEL_LOW;
0314 break;
0315 case IRQ_TYPE_LEVEL_HIGH:
0316 irq_type = GB_GPIO_IRQ_TYPE_LEVEL_HIGH;
0317 break;
0318 default:
0319 dev_err(dev, "unsupported irq type: %u\n", type);
0320 return -EINVAL;
0321 }
0322
0323 line->irq_type = irq_type;
0324 line->irq_type_pending = true;
0325
0326 return 0;
0327 }
0328
0329 static void gb_gpio_irq_bus_lock(struct irq_data *d)
0330 {
0331 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
0332 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0333
0334 mutex_lock(&ggc->irq_lock);
0335 }
0336
0337 static void gb_gpio_irq_bus_sync_unlock(struct irq_data *d)
0338 {
0339 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
0340 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0341 struct gb_gpio_line *line = &ggc->lines[d->hwirq];
0342
0343 if (line->irq_type_pending) {
0344 _gb_gpio_irq_set_type(ggc, d->hwirq, line->irq_type);
0345 line->irq_type_pending = false;
0346 }
0347
0348 if (line->masked_pending) {
0349 if (line->masked)
0350 _gb_gpio_irq_mask(ggc, d->hwirq);
0351 else
0352 _gb_gpio_irq_unmask(ggc, d->hwirq);
0353 line->masked_pending = false;
0354 }
0355
0356 mutex_unlock(&ggc->irq_lock);
0357 }
0358
0359 static int gb_gpio_request_handler(struct gb_operation *op)
0360 {
0361 struct gb_connection *connection = op->connection;
0362 struct gb_gpio_controller *ggc = gb_connection_get_data(connection);
0363 struct device *dev = &ggc->gbphy_dev->dev;
0364 struct gb_message *request;
0365 struct gb_gpio_irq_event_request *event;
0366 u8 type = op->type;
0367 int irq, ret;
0368
0369 if (type != GB_GPIO_TYPE_IRQ_EVENT) {
0370 dev_err(dev, "unsupported unsolicited request: %u\n", type);
0371 return -EINVAL;
0372 }
0373
0374 request = op->request;
0375
0376 if (request->payload_size < sizeof(*event)) {
0377 dev_err(dev, "short event received (%zu < %zu)\n",
0378 request->payload_size, sizeof(*event));
0379 return -EINVAL;
0380 }
0381
0382 event = request->payload;
0383 if (event->which > ggc->line_max) {
0384 dev_err(dev, "invalid hw irq: %d\n", event->which);
0385 return -EINVAL;
0386 }
0387
0388 irq = irq_find_mapping(ggc->chip.irq.domain, event->which);
0389 if (!irq) {
0390 dev_err(dev, "failed to find IRQ\n");
0391 return -EINVAL;
0392 }
0393
0394 ret = generic_handle_irq_safe(irq);
0395 if (ret)
0396 dev_err(dev, "failed to invoke irq handler\n");
0397
0398 return ret;
0399 }
0400
0401 static int gb_gpio_request(struct gpio_chip *chip, unsigned int offset)
0402 {
0403 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0404
0405 return gb_gpio_activate_operation(ggc, (u8)offset);
0406 }
0407
0408 static void gb_gpio_free(struct gpio_chip *chip, unsigned int offset)
0409 {
0410 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0411
0412 gb_gpio_deactivate_operation(ggc, (u8)offset);
0413 }
0414
0415 static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
0416 {
0417 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0418 u8 which;
0419 int ret;
0420
0421 which = (u8)offset;
0422 ret = gb_gpio_get_direction_operation(ggc, which);
0423 if (ret)
0424 return ret;
0425
0426 return ggc->lines[which].direction ? 1 : 0;
0427 }
0428
0429 static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
0430 {
0431 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0432
0433 return gb_gpio_direction_in_operation(ggc, (u8)offset);
0434 }
0435
0436 static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
0437 int value)
0438 {
0439 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0440
0441 return gb_gpio_direction_out_operation(ggc, (u8)offset, !!value);
0442 }
0443
0444 static int gb_gpio_get(struct gpio_chip *chip, unsigned int offset)
0445 {
0446 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0447 u8 which;
0448 int ret;
0449
0450 which = (u8)offset;
0451 ret = gb_gpio_get_value_operation(ggc, which);
0452 if (ret)
0453 return ret;
0454
0455 return ggc->lines[which].value;
0456 }
0457
0458 static void gb_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
0459 {
0460 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0461
0462 gb_gpio_set_value_operation(ggc, (u8)offset, !!value);
0463 }
0464
0465 static int gb_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
0466 unsigned long config)
0467 {
0468 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
0469 u32 debounce;
0470
0471 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
0472 return -ENOTSUPP;
0473
0474 debounce = pinconf_to_config_argument(config);
0475 if (debounce > U16_MAX)
0476 return -EINVAL;
0477
0478 return gb_gpio_set_debounce_operation(ggc, (u8)offset, (u16)debounce);
0479 }
0480
0481 static int gb_gpio_controller_setup(struct gb_gpio_controller *ggc)
0482 {
0483 int ret;
0484
0485
0486 ret = gb_gpio_line_count_operation(ggc);
0487 if (ret)
0488 return ret;
0489
0490 ggc->lines = kcalloc(ggc->line_max + 1, sizeof(*ggc->lines),
0491 GFP_KERNEL);
0492 if (!ggc->lines)
0493 return -ENOMEM;
0494
0495 return ret;
0496 }
0497
0498 static int gb_gpio_probe(struct gbphy_device *gbphy_dev,
0499 const struct gbphy_device_id *id)
0500 {
0501 struct gb_connection *connection;
0502 struct gb_gpio_controller *ggc;
0503 struct gpio_chip *gpio;
0504 struct gpio_irq_chip *girq;
0505 struct irq_chip *irqc;
0506 int ret;
0507
0508 ggc = kzalloc(sizeof(*ggc), GFP_KERNEL);
0509 if (!ggc)
0510 return -ENOMEM;
0511
0512 connection =
0513 gb_connection_create(gbphy_dev->bundle,
0514 le16_to_cpu(gbphy_dev->cport_desc->id),
0515 gb_gpio_request_handler);
0516 if (IS_ERR(connection)) {
0517 ret = PTR_ERR(connection);
0518 goto exit_ggc_free;
0519 }
0520
0521 ggc->connection = connection;
0522 gb_connection_set_data(connection, ggc);
0523 ggc->gbphy_dev = gbphy_dev;
0524 gb_gbphy_set_data(gbphy_dev, ggc);
0525
0526 ret = gb_connection_enable_tx(connection);
0527 if (ret)
0528 goto exit_connection_destroy;
0529
0530 ret = gb_gpio_controller_setup(ggc);
0531 if (ret)
0532 goto exit_connection_disable;
0533
0534 irqc = &ggc->irqc;
0535 irqc->irq_mask = gb_gpio_irq_mask;
0536 irqc->irq_unmask = gb_gpio_irq_unmask;
0537 irqc->irq_set_type = gb_gpio_irq_set_type;
0538 irqc->irq_bus_lock = gb_gpio_irq_bus_lock;
0539 irqc->irq_bus_sync_unlock = gb_gpio_irq_bus_sync_unlock;
0540 irqc->name = "greybus_gpio";
0541
0542 mutex_init(&ggc->irq_lock);
0543
0544 gpio = &ggc->chip;
0545
0546 gpio->label = "greybus_gpio";
0547 gpio->parent = &gbphy_dev->dev;
0548 gpio->owner = THIS_MODULE;
0549
0550 gpio->request = gb_gpio_request;
0551 gpio->free = gb_gpio_free;
0552 gpio->get_direction = gb_gpio_get_direction;
0553 gpio->direction_input = gb_gpio_direction_input;
0554 gpio->direction_output = gb_gpio_direction_output;
0555 gpio->get = gb_gpio_get;
0556 gpio->set = gb_gpio_set;
0557 gpio->set_config = gb_gpio_set_config;
0558 gpio->base = -1;
0559 gpio->ngpio = ggc->line_max + 1;
0560 gpio->can_sleep = true;
0561
0562 girq = &gpio->irq;
0563 girq->chip = irqc;
0564
0565 girq->parent_handler = NULL;
0566 girq->num_parents = 0;
0567 girq->parents = NULL;
0568 girq->default_type = IRQ_TYPE_NONE;
0569 girq->handler = handle_level_irq;
0570
0571 ret = gb_connection_enable(connection);
0572 if (ret)
0573 goto exit_line_free;
0574
0575 ret = gpiochip_add(gpio);
0576 if (ret) {
0577 dev_err(&gbphy_dev->dev, "failed to add gpio chip: %d\n", ret);
0578 goto exit_line_free;
0579 }
0580
0581 gbphy_runtime_put_autosuspend(gbphy_dev);
0582 return 0;
0583
0584 exit_line_free:
0585 kfree(ggc->lines);
0586 exit_connection_disable:
0587 gb_connection_disable(connection);
0588 exit_connection_destroy:
0589 gb_connection_destroy(connection);
0590 exit_ggc_free:
0591 kfree(ggc);
0592 return ret;
0593 }
0594
0595 static void gb_gpio_remove(struct gbphy_device *gbphy_dev)
0596 {
0597 struct gb_gpio_controller *ggc = gb_gbphy_get_data(gbphy_dev);
0598 struct gb_connection *connection = ggc->connection;
0599 int ret;
0600
0601 ret = gbphy_runtime_get_sync(gbphy_dev);
0602 if (ret)
0603 gbphy_runtime_get_noresume(gbphy_dev);
0604
0605 gb_connection_disable_rx(connection);
0606 gpiochip_remove(&ggc->chip);
0607 gb_connection_disable(connection);
0608 gb_connection_destroy(connection);
0609 kfree(ggc->lines);
0610 kfree(ggc);
0611 }
0612
0613 static const struct gbphy_device_id gb_gpio_id_table[] = {
0614 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_GPIO) },
0615 { },
0616 };
0617 MODULE_DEVICE_TABLE(gbphy, gb_gpio_id_table);
0618
0619 static struct gbphy_driver gpio_driver = {
0620 .name = "gpio",
0621 .probe = gb_gpio_probe,
0622 .remove = gb_gpio_remove,
0623 .id_table = gb_gpio_id_table,
0624 };
0625
0626 module_gbphy_driver(gpio_driver);
0627 MODULE_LICENSE("GPL v2");