gpio.c 19 KB

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