gpio-dln2.c 13 KB

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