gpio-dln2.c 14 KB

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