gpio-dln2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * Driver for the Diolan DLN-2 USB-GPIO adapter
  3. *
  4. * Copyright (c) 2014 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/types.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqchip/chained_irq.h>
  17. #include <linux/gpio.h>
  18. #include <linux/gpio/driver.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/mfd/dln2.h>
  21. #define DLN2_GPIO_ID 0x01
  22. #define DLN2_GPIO_GET_PIN_COUNT DLN2_CMD(0x01, DLN2_GPIO_ID)
  23. #define DLN2_GPIO_SET_DEBOUNCE DLN2_CMD(0x04, DLN2_GPIO_ID)
  24. #define DLN2_GPIO_GET_DEBOUNCE DLN2_CMD(0x05, DLN2_GPIO_ID)
  25. #define DLN2_GPIO_PORT_GET_VAL DLN2_CMD(0x06, DLN2_GPIO_ID)
  26. #define DLN2_GPIO_PIN_GET_VAL DLN2_CMD(0x0B, DLN2_GPIO_ID)
  27. #define DLN2_GPIO_PIN_SET_OUT_VAL DLN2_CMD(0x0C, DLN2_GPIO_ID)
  28. #define DLN2_GPIO_PIN_GET_OUT_VAL DLN2_CMD(0x0D, DLN2_GPIO_ID)
  29. #define DLN2_GPIO_CONDITION_MET_EV DLN2_CMD(0x0F, DLN2_GPIO_ID)
  30. #define DLN2_GPIO_PIN_ENABLE DLN2_CMD(0x10, DLN2_GPIO_ID)
  31. #define DLN2_GPIO_PIN_DISABLE DLN2_CMD(0x11, DLN2_GPIO_ID)
  32. #define DLN2_GPIO_PIN_SET_DIRECTION DLN2_CMD(0x13, DLN2_GPIO_ID)
  33. #define DLN2_GPIO_PIN_GET_DIRECTION DLN2_CMD(0x14, DLN2_GPIO_ID)
  34. #define DLN2_GPIO_PIN_SET_EVENT_CFG DLN2_CMD(0x1E, DLN2_GPIO_ID)
  35. #define DLN2_GPIO_PIN_GET_EVENT_CFG DLN2_CMD(0x1F, DLN2_GPIO_ID)
  36. #define DLN2_GPIO_EVENT_NONE 0
  37. #define DLN2_GPIO_EVENT_CHANGE 1
  38. #define DLN2_GPIO_EVENT_LVL_HIGH 2
  39. #define DLN2_GPIO_EVENT_LVL_LOW 3
  40. #define DLN2_GPIO_EVENT_CHANGE_RISING 0x11
  41. #define DLN2_GPIO_EVENT_CHANGE_FALLING 0x21
  42. #define DLN2_GPIO_EVENT_MASK 0x0F
  43. #define DLN2_GPIO_MAX_PINS 32
  44. struct dln2_gpio {
  45. struct platform_device *pdev;
  46. struct gpio_chip gpio;
  47. /*
  48. * Cache pin direction to save us one transfer, since the hardware has
  49. * separate commands to read the in and out values.
  50. */
  51. DECLARE_BITMAP(output_enabled, DLN2_GPIO_MAX_PINS);
  52. /* active IRQs - not synced to hardware */
  53. DECLARE_BITMAP(unmasked_irqs, DLN2_GPIO_MAX_PINS);
  54. /* active IRQS - synced to hardware */
  55. DECLARE_BITMAP(enabled_irqs, DLN2_GPIO_MAX_PINS);
  56. int irq_type[DLN2_GPIO_MAX_PINS];
  57. struct mutex irq_lock;
  58. };
  59. struct dln2_gpio_pin {
  60. __le16 pin;
  61. };
  62. struct dln2_gpio_pin_val {
  63. __le16 pin __packed;
  64. u8 value;
  65. };
  66. static int dln2_gpio_get_pin_count(struct platform_device *pdev)
  67. {
  68. int ret;
  69. __le16 count;
  70. int len = sizeof(count);
  71. ret = dln2_transfer_rx(pdev, DLN2_GPIO_GET_PIN_COUNT, &count, &len);
  72. if (ret < 0)
  73. return ret;
  74. if (len < sizeof(count))
  75. return -EPROTO;
  76. return le16_to_cpu(count);
  77. }
  78. static int dln2_gpio_pin_cmd(struct dln2_gpio *dln2, int cmd, unsigned pin)
  79. {
  80. struct dln2_gpio_pin req = {
  81. .pin = cpu_to_le16(pin),
  82. };
  83. return dln2_transfer_tx(dln2->pdev, cmd, &req, sizeof(req));
  84. }
  85. static int dln2_gpio_pin_val(struct dln2_gpio *dln2, int cmd, unsigned int pin)
  86. {
  87. int ret;
  88. struct dln2_gpio_pin req = {
  89. .pin = cpu_to_le16(pin),
  90. };
  91. struct dln2_gpio_pin_val rsp;
  92. int len = sizeof(rsp);
  93. ret = dln2_transfer(dln2->pdev, cmd, &req, sizeof(req), &rsp, &len);
  94. if (ret < 0)
  95. return ret;
  96. if (len < sizeof(rsp) || req.pin != rsp.pin)
  97. return -EPROTO;
  98. return rsp.value;
  99. }
  100. static int dln2_gpio_pin_get_in_val(struct dln2_gpio *dln2, unsigned int pin)
  101. {
  102. int ret;
  103. ret = dln2_gpio_pin_val(dln2, DLN2_GPIO_PIN_GET_VAL, pin);
  104. if (ret < 0)
  105. return ret;
  106. return !!ret;
  107. }
  108. static int dln2_gpio_pin_get_out_val(struct dln2_gpio *dln2, unsigned int pin)
  109. {
  110. int ret;
  111. ret = dln2_gpio_pin_val(dln2, DLN2_GPIO_PIN_GET_OUT_VAL, pin);
  112. if (ret < 0)
  113. return ret;
  114. return !!ret;
  115. }
  116. static int dln2_gpio_pin_set_out_val(struct dln2_gpio *dln2,
  117. unsigned int pin, int value)
  118. {
  119. struct dln2_gpio_pin_val req = {
  120. .pin = cpu_to_le16(pin),
  121. .value = value,
  122. };
  123. return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_OUT_VAL, &req,
  124. sizeof(req));
  125. }
  126. #define DLN2_GPIO_DIRECTION_IN 0
  127. #define DLN2_GPIO_DIRECTION_OUT 1
  128. static int dln2_gpio_request(struct gpio_chip *chip, unsigned offset)
  129. {
  130. struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
  131. struct dln2_gpio_pin req = {
  132. .pin = cpu_to_le16(offset),
  133. };
  134. struct dln2_gpio_pin_val rsp;
  135. int len = sizeof(rsp);
  136. int ret;
  137. ret = dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_ENABLE, offset);
  138. if (ret < 0)
  139. return ret;
  140. /* cache the pin direction */
  141. ret = dln2_transfer(dln2->pdev, DLN2_GPIO_PIN_GET_DIRECTION,
  142. &req, sizeof(req), &rsp, &len);
  143. if (ret < 0)
  144. return ret;
  145. if (len < sizeof(rsp) || req.pin != rsp.pin) {
  146. ret = -EPROTO;
  147. goto out_disable;
  148. }
  149. switch (rsp.value) {
  150. case DLN2_GPIO_DIRECTION_IN:
  151. clear_bit(offset, dln2->output_enabled);
  152. return 0;
  153. case DLN2_GPIO_DIRECTION_OUT:
  154. set_bit(offset, dln2->output_enabled);
  155. return 0;
  156. default:
  157. ret = -EPROTO;
  158. goto out_disable;
  159. }
  160. out_disable:
  161. dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_DISABLE, offset);
  162. return ret;
  163. }
  164. static void dln2_gpio_free(struct gpio_chip *chip, unsigned offset)
  165. {
  166. struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
  167. dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_DISABLE, offset);
  168. }
  169. static int dln2_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  170. {
  171. struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
  172. if (test_bit(offset, dln2->output_enabled))
  173. return GPIOF_DIR_OUT;
  174. return GPIOF_DIR_IN;
  175. }
  176. static int dln2_gpio_get(struct gpio_chip *chip, unsigned int offset)
  177. {
  178. struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
  179. int dir;
  180. dir = dln2_gpio_get_direction(chip, offset);
  181. if (dir < 0)
  182. return dir;
  183. if (dir == GPIOF_DIR_IN)
  184. return dln2_gpio_pin_get_in_val(dln2, offset);
  185. return dln2_gpio_pin_get_out_val(dln2, offset);
  186. }
  187. static void dln2_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  188. {
  189. struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
  190. dln2_gpio_pin_set_out_val(dln2, offset, value);
  191. }
  192. static int dln2_gpio_set_direction(struct gpio_chip *chip, unsigned offset,
  193. unsigned dir)
  194. {
  195. struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
  196. struct dln2_gpio_pin_val req = {
  197. .pin = cpu_to_le16(offset),
  198. .value = dir,
  199. };
  200. int ret;
  201. ret = dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_DIRECTION,
  202. &req, sizeof(req));
  203. if (ret < 0)
  204. return ret;
  205. if (dir == DLN2_GPIO_DIRECTION_OUT)
  206. set_bit(offset, dln2->output_enabled);
  207. else
  208. clear_bit(offset, dln2->output_enabled);
  209. return ret;
  210. }
  211. static int dln2_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  212. {
  213. return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_IN);
  214. }
  215. static int dln2_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  216. int value)
  217. {
  218. struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
  219. int ret;
  220. ret = dln2_gpio_pin_set_out_val(dln2, offset, value);
  221. if (ret < 0)
  222. return ret;
  223. return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_OUT);
  224. }
  225. static int dln2_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
  226. unsigned debounce)
  227. {
  228. struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
  229. __le32 duration = cpu_to_le32(debounce);
  230. return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_SET_DEBOUNCE,
  231. &duration, sizeof(duration));
  232. }
  233. static int dln2_gpio_set_event_cfg(struct dln2_gpio *dln2, unsigned pin,
  234. unsigned type, unsigned period)
  235. {
  236. struct {
  237. __le16 pin;
  238. u8 type;
  239. __le16 period;
  240. } __packed req = {
  241. .pin = cpu_to_le16(pin),
  242. .type = type,
  243. .period = cpu_to_le16(period),
  244. };
  245. return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_EVENT_CFG,
  246. &req, sizeof(req));
  247. }
  248. static void dln2_irq_unmask(struct irq_data *irqd)
  249. {
  250. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  251. struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
  252. int pin = irqd_to_hwirq(irqd);
  253. set_bit(pin, dln2->unmasked_irqs);
  254. }
  255. static void dln2_irq_mask(struct irq_data *irqd)
  256. {
  257. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  258. struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
  259. int pin = irqd_to_hwirq(irqd);
  260. clear_bit(pin, dln2->unmasked_irqs);
  261. }
  262. static int dln2_irq_set_type(struct irq_data *irqd, unsigned type)
  263. {
  264. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  265. struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
  266. int pin = irqd_to_hwirq(irqd);
  267. switch (type) {
  268. case IRQ_TYPE_LEVEL_HIGH:
  269. dln2->irq_type[pin] = DLN2_GPIO_EVENT_LVL_HIGH;
  270. break;
  271. case IRQ_TYPE_LEVEL_LOW:
  272. dln2->irq_type[pin] = DLN2_GPIO_EVENT_LVL_LOW;
  273. break;
  274. case IRQ_TYPE_EDGE_BOTH:
  275. dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE;
  276. break;
  277. case IRQ_TYPE_EDGE_RISING:
  278. dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE_RISING;
  279. break;
  280. case IRQ_TYPE_EDGE_FALLING:
  281. dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE_FALLING;
  282. break;
  283. default:
  284. return -EINVAL;
  285. }
  286. return 0;
  287. }
  288. static void dln2_irq_bus_lock(struct irq_data *irqd)
  289. {
  290. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  291. struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
  292. mutex_lock(&dln2->irq_lock);
  293. }
  294. static void dln2_irq_bus_unlock(struct irq_data *irqd)
  295. {
  296. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  297. struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
  298. int pin = irqd_to_hwirq(irqd);
  299. int enabled, unmasked;
  300. unsigned type;
  301. int ret;
  302. enabled = test_bit(pin, dln2->enabled_irqs);
  303. unmasked = test_bit(pin, dln2->unmasked_irqs);
  304. if (enabled != unmasked) {
  305. if (unmasked) {
  306. type = dln2->irq_type[pin] & DLN2_GPIO_EVENT_MASK;
  307. set_bit(pin, dln2->enabled_irqs);
  308. } else {
  309. type = DLN2_GPIO_EVENT_NONE;
  310. clear_bit(pin, dln2->enabled_irqs);
  311. }
  312. ret = dln2_gpio_set_event_cfg(dln2, pin, type, 0);
  313. if (ret)
  314. dev_err(dln2->gpio.dev, "failed to set event\n");
  315. }
  316. mutex_unlock(&dln2->irq_lock);
  317. }
  318. static struct irq_chip dln2_gpio_irqchip = {
  319. .name = "dln2-irq",
  320. .irq_mask = dln2_irq_mask,
  321. .irq_unmask = dln2_irq_unmask,
  322. .irq_set_type = dln2_irq_set_type,
  323. .irq_bus_lock = dln2_irq_bus_lock,
  324. .irq_bus_sync_unlock = dln2_irq_bus_unlock,
  325. };
  326. static void dln2_gpio_event(struct platform_device *pdev, u16 echo,
  327. const void *data, int len)
  328. {
  329. int pin, irq;
  330. const struct {
  331. __le16 count;
  332. __u8 type;
  333. __le16 pin;
  334. __u8 value;
  335. } __packed *event = data;
  336. struct dln2_gpio *dln2 = platform_get_drvdata(pdev);
  337. if (len < sizeof(*event)) {
  338. dev_err(dln2->gpio.dev, "short event message\n");
  339. return;
  340. }
  341. pin = le16_to_cpu(event->pin);
  342. if (pin >= dln2->gpio.ngpio) {
  343. dev_err(dln2->gpio.dev, "out of bounds pin %d\n", pin);
  344. return;
  345. }
  346. irq = irq_find_mapping(dln2->gpio.irqdomain, pin);
  347. if (!irq) {
  348. dev_err(dln2->gpio.dev, "pin %d not mapped to IRQ\n", pin);
  349. return;
  350. }
  351. switch (dln2->irq_type[pin]) {
  352. case DLN2_GPIO_EVENT_CHANGE_RISING:
  353. if (event->value)
  354. generic_handle_irq(irq);
  355. break;
  356. case DLN2_GPIO_EVENT_CHANGE_FALLING:
  357. if (!event->value)
  358. generic_handle_irq(irq);
  359. break;
  360. default:
  361. generic_handle_irq(irq);
  362. }
  363. }
  364. static int dln2_gpio_probe(struct platform_device *pdev)
  365. {
  366. struct dln2_gpio *dln2;
  367. struct device *dev = &pdev->dev;
  368. int pins;
  369. int ret;
  370. pins = dln2_gpio_get_pin_count(pdev);
  371. if (pins < 0) {
  372. dev_err(dev, "failed to get pin count: %d\n", pins);
  373. return pins;
  374. }
  375. if (pins > DLN2_GPIO_MAX_PINS) {
  376. pins = DLN2_GPIO_MAX_PINS;
  377. dev_warn(dev, "clamping pins to %d\n", DLN2_GPIO_MAX_PINS);
  378. }
  379. dln2 = devm_kzalloc(&pdev->dev, sizeof(*dln2), GFP_KERNEL);
  380. if (!dln2)
  381. return -ENOMEM;
  382. mutex_init(&dln2->irq_lock);
  383. dln2->pdev = pdev;
  384. dln2->gpio.label = "dln2";
  385. dln2->gpio.dev = dev;
  386. dln2->gpio.owner = THIS_MODULE;
  387. dln2->gpio.base = -1;
  388. dln2->gpio.ngpio = pins;
  389. dln2->gpio.can_sleep = true;
  390. dln2->gpio.irq_not_threaded = true;
  391. dln2->gpio.set = dln2_gpio_set;
  392. dln2->gpio.get = dln2_gpio_get;
  393. dln2->gpio.request = dln2_gpio_request;
  394. dln2->gpio.free = dln2_gpio_free;
  395. dln2->gpio.get_direction = dln2_gpio_get_direction;
  396. dln2->gpio.direction_input = dln2_gpio_direction_input;
  397. dln2->gpio.direction_output = dln2_gpio_direction_output;
  398. dln2->gpio.set_debounce = dln2_gpio_set_debounce;
  399. platform_set_drvdata(pdev, dln2);
  400. ret = gpiochip_add(&dln2->gpio);
  401. if (ret < 0) {
  402. dev_err(dev, "failed to add gpio chip: %d\n", ret);
  403. goto out;
  404. }
  405. ret = gpiochip_irqchip_add(&dln2->gpio, &dln2_gpio_irqchip, 0,
  406. handle_simple_irq, IRQ_TYPE_NONE);
  407. if (ret < 0) {
  408. dev_err(dev, "failed to add irq chip: %d\n", ret);
  409. goto out_gpiochip_remove;
  410. }
  411. ret = dln2_register_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV,
  412. dln2_gpio_event);
  413. if (ret) {
  414. dev_err(dev, "failed to register event cb: %d\n", ret);
  415. goto out_gpiochip_remove;
  416. }
  417. return 0;
  418. out_gpiochip_remove:
  419. gpiochip_remove(&dln2->gpio);
  420. out:
  421. return ret;
  422. }
  423. static int dln2_gpio_remove(struct platform_device *pdev)
  424. {
  425. struct dln2_gpio *dln2 = platform_get_drvdata(pdev);
  426. dln2_unregister_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV);
  427. gpiochip_remove(&dln2->gpio);
  428. return 0;
  429. }
  430. static struct platform_driver dln2_gpio_driver = {
  431. .driver.name = "dln2-gpio",
  432. .probe = dln2_gpio_probe,
  433. .remove = dln2_gpio_remove,
  434. };
  435. module_platform_driver(dln2_gpio_driver);
  436. MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com");
  437. MODULE_DESCRIPTION("Driver for the Diolan DLN2 GPIO interface");
  438. MODULE_LICENSE("GPL v2");
  439. MODULE_ALIAS("platform:dln2-gpio");