gpio-ljca.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel La Jolla Cove Adapter USB-GPIO driver
  4. *
  5. * Copyright (c) 2023, Intel Corporation.
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/auxiliary_bus.h>
  9. #include <linux/bitfield.h>
  10. #include <linux/bitops.h>
  11. #include <linux/dev_printk.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/irq.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kref.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/types.h>
  19. #include <linux/usb/ljca.h>
  20. /* GPIO commands */
  21. #define LJCA_GPIO_CONFIG 1
  22. #define LJCA_GPIO_READ 2
  23. #define LJCA_GPIO_WRITE 3
  24. #define LJCA_GPIO_INT_EVENT 4
  25. #define LJCA_GPIO_INT_MASK 5
  26. #define LJCA_GPIO_INT_UNMASK 6
  27. #define LJCA_GPIO_CONF_DISABLE BIT(0)
  28. #define LJCA_GPIO_CONF_INPUT BIT(1)
  29. #define LJCA_GPIO_CONF_OUTPUT BIT(2)
  30. #define LJCA_GPIO_CONF_PULLUP BIT(3)
  31. #define LJCA_GPIO_CONF_PULLDOWN BIT(4)
  32. #define LJCA_GPIO_CONF_DEFAULT BIT(5)
  33. #define LJCA_GPIO_CONF_INTERRUPT BIT(6)
  34. #define LJCA_GPIO_INT_TYPE BIT(7)
  35. #define LJCA_GPIO_CONF_EDGE FIELD_PREP(LJCA_GPIO_INT_TYPE, 1)
  36. #define LJCA_GPIO_CONF_LEVEL FIELD_PREP(LJCA_GPIO_INT_TYPE, 0)
  37. /* Intentional overlap with PULLUP / PULLDOWN */
  38. #define LJCA_GPIO_CONF_SET BIT(3)
  39. #define LJCA_GPIO_CONF_CLR BIT(4)
  40. #define LJCA_GPIO_BUF_SIZE 60u
  41. struct ljca_gpio_op {
  42. u8 index;
  43. u8 value;
  44. } __packed;
  45. struct ljca_gpio_packet {
  46. u8 num;
  47. struct ljca_gpio_op item[] __counted_by(num);
  48. } __packed;
  49. struct ljca_gpio_dev {
  50. struct ljca_client *ljca;
  51. struct gpio_chip gc;
  52. struct ljca_gpio_info *gpio_info;
  53. DECLARE_BITMAP(unmasked_irqs, LJCA_MAX_GPIO_NUM);
  54. DECLARE_BITMAP(enabled_irqs, LJCA_MAX_GPIO_NUM);
  55. DECLARE_BITMAP(reenable_irqs, LJCA_MAX_GPIO_NUM);
  56. DECLARE_BITMAP(output_enabled, LJCA_MAX_GPIO_NUM);
  57. u8 *connect_mode;
  58. /* protect irq bus */
  59. struct mutex irq_lock;
  60. struct work_struct work;
  61. /* protect package transfer to hardware */
  62. struct mutex trans_lock;
  63. u8 obuf[LJCA_GPIO_BUF_SIZE];
  64. u8 ibuf[LJCA_GPIO_BUF_SIZE];
  65. };
  66. static int ljca_gpio_config(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id,
  67. u8 config)
  68. {
  69. struct ljca_gpio_packet *packet =
  70. (struct ljca_gpio_packet *)ljca_gpio->obuf;
  71. int ret;
  72. mutex_lock(&ljca_gpio->trans_lock);
  73. packet->num = 1;
  74. packet->item[0].index = gpio_id;
  75. packet->item[0].value = config | ljca_gpio->connect_mode[gpio_id];
  76. ret = ljca_transfer(ljca_gpio->ljca, LJCA_GPIO_CONFIG, (u8 *)packet,
  77. struct_size(packet, item, packet->num), NULL, 0);
  78. mutex_unlock(&ljca_gpio->trans_lock);
  79. return ret < 0 ? ret : 0;
  80. }
  81. static int ljca_gpio_read(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id)
  82. {
  83. struct ljca_gpio_packet *ack_packet =
  84. (struct ljca_gpio_packet *)ljca_gpio->ibuf;
  85. struct ljca_gpio_packet *packet =
  86. (struct ljca_gpio_packet *)ljca_gpio->obuf;
  87. int ret;
  88. mutex_lock(&ljca_gpio->trans_lock);
  89. packet->num = 1;
  90. packet->item[0].index = gpio_id;
  91. ret = ljca_transfer(ljca_gpio->ljca, LJCA_GPIO_READ, (u8 *)packet,
  92. struct_size(packet, item, packet->num),
  93. ljca_gpio->ibuf, LJCA_GPIO_BUF_SIZE);
  94. if (ret <= 0 || ack_packet->num != packet->num) {
  95. dev_err(&ljca_gpio->ljca->auxdev.dev,
  96. "read package error, gpio_id: %u num: %u ret: %d\n",
  97. gpio_id, ack_packet->num, ret);
  98. ret = ret < 0 ? ret : -EIO;
  99. }
  100. mutex_unlock(&ljca_gpio->trans_lock);
  101. return ret < 0 ? ret : ack_packet->item[0].value > 0;
  102. }
  103. static int ljca_gpio_write(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id, int value)
  104. {
  105. struct ljca_gpio_packet *packet =
  106. (struct ljca_gpio_packet *)ljca_gpio->obuf;
  107. int ret;
  108. mutex_lock(&ljca_gpio->trans_lock);
  109. packet->num = 1;
  110. packet->item[0].index = gpio_id;
  111. packet->item[0].value = value & 1;
  112. ret = ljca_transfer(ljca_gpio->ljca, LJCA_GPIO_WRITE, (u8 *)packet,
  113. struct_size(packet, item, packet->num), NULL, 0);
  114. mutex_unlock(&ljca_gpio->trans_lock);
  115. return ret < 0 ? ret : 0;
  116. }
  117. static int ljca_gpio_get_value(struct gpio_chip *chip, unsigned int offset)
  118. {
  119. struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
  120. return ljca_gpio_read(ljca_gpio, offset);
  121. }
  122. static int ljca_gpio_set_value(struct gpio_chip *chip, unsigned int offset,
  123. int val)
  124. {
  125. struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
  126. int ret;
  127. ret = ljca_gpio_write(ljca_gpio, offset, val);
  128. if (ret)
  129. dev_err(chip->parent,
  130. "set value failed offset: %u val: %d ret: %d\n",
  131. offset, val, ret);
  132. return ret;
  133. }
  134. static int ljca_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
  135. {
  136. struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
  137. u8 config = LJCA_GPIO_CONF_INPUT | LJCA_GPIO_CONF_CLR;
  138. int ret;
  139. ret = ljca_gpio_config(ljca_gpio, offset, config);
  140. if (ret)
  141. return ret;
  142. clear_bit(offset, ljca_gpio->output_enabled);
  143. return 0;
  144. }
  145. static int ljca_gpio_direction_output(struct gpio_chip *chip,
  146. unsigned int offset, int val)
  147. {
  148. struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
  149. u8 config = LJCA_GPIO_CONF_OUTPUT | LJCA_GPIO_CONF_CLR;
  150. int ret;
  151. ret = ljca_gpio_config(ljca_gpio, offset, config);
  152. if (ret)
  153. return ret;
  154. ret = ljca_gpio_set_value(chip, offset, val);
  155. if (ret)
  156. return ret;
  157. set_bit(offset, ljca_gpio->output_enabled);
  158. return 0;
  159. }
  160. static int ljca_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  161. {
  162. struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
  163. if (test_bit(offset, ljca_gpio->output_enabled))
  164. return GPIO_LINE_DIRECTION_OUT;
  165. return GPIO_LINE_DIRECTION_IN;
  166. }
  167. static int ljca_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
  168. unsigned long config)
  169. {
  170. struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
  171. ljca_gpio->connect_mode[offset] = 0;
  172. switch (pinconf_to_config_param(config)) {
  173. case PIN_CONFIG_BIAS_PULL_UP:
  174. ljca_gpio->connect_mode[offset] |= LJCA_GPIO_CONF_PULLUP;
  175. break;
  176. case PIN_CONFIG_BIAS_PULL_DOWN:
  177. ljca_gpio->connect_mode[offset] |= LJCA_GPIO_CONF_PULLDOWN;
  178. break;
  179. case PIN_CONFIG_DRIVE_PUSH_PULL:
  180. case PIN_CONFIG_PERSIST_STATE:
  181. break;
  182. default:
  183. return -ENOTSUPP;
  184. }
  185. return 0;
  186. }
  187. static int ljca_gpio_init_valid_mask(struct gpio_chip *chip,
  188. unsigned long *valid_mask,
  189. unsigned int ngpios)
  190. {
  191. struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
  192. WARN_ON_ONCE(ngpios != ljca_gpio->gpio_info->num);
  193. bitmap_copy(valid_mask, ljca_gpio->gpio_info->valid_pin_map, ngpios);
  194. return 0;
  195. }
  196. static void ljca_gpio_irq_init_valid_mask(struct gpio_chip *chip,
  197. unsigned long *valid_mask,
  198. unsigned int ngpios)
  199. {
  200. ljca_gpio_init_valid_mask(chip, valid_mask, ngpios);
  201. }
  202. static int ljca_enable_irq(struct ljca_gpio_dev *ljca_gpio, int gpio_id,
  203. bool enable)
  204. {
  205. struct ljca_gpio_packet *packet =
  206. (struct ljca_gpio_packet *)ljca_gpio->obuf;
  207. int ret;
  208. mutex_lock(&ljca_gpio->trans_lock);
  209. packet->num = 1;
  210. packet->item[0].index = gpio_id;
  211. packet->item[0].value = 0;
  212. ret = ljca_transfer(ljca_gpio->ljca,
  213. enable ? LJCA_GPIO_INT_UNMASK : LJCA_GPIO_INT_MASK,
  214. (u8 *)packet, struct_size(packet, item, packet->num),
  215. NULL, 0);
  216. mutex_unlock(&ljca_gpio->trans_lock);
  217. return ret < 0 ? ret : 0;
  218. }
  219. static void ljca_gpio_async(struct work_struct *work)
  220. {
  221. struct ljca_gpio_dev *ljca_gpio =
  222. container_of(work, struct ljca_gpio_dev, work);
  223. int gpio_id, unmasked;
  224. for_each_set_bit(gpio_id, ljca_gpio->reenable_irqs, ljca_gpio->gc.ngpio) {
  225. clear_bit(gpio_id, ljca_gpio->reenable_irqs);
  226. unmasked = test_bit(gpio_id, ljca_gpio->unmasked_irqs);
  227. if (unmasked)
  228. ljca_enable_irq(ljca_gpio, gpio_id, true);
  229. }
  230. }
  231. static void ljca_gpio_event_cb(void *context, u8 cmd, const void *evt_data,
  232. int len)
  233. {
  234. const struct ljca_gpio_packet *packet = evt_data;
  235. struct ljca_gpio_dev *ljca_gpio = context;
  236. int i;
  237. if (cmd != LJCA_GPIO_INT_EVENT)
  238. return;
  239. for (i = 0; i < packet->num; i++) {
  240. generic_handle_domain_irq(ljca_gpio->gc.irq.domain,
  241. packet->item[i].index);
  242. set_bit(packet->item[i].index, ljca_gpio->reenable_irqs);
  243. }
  244. schedule_work(&ljca_gpio->work);
  245. }
  246. static void ljca_irq_unmask(struct irq_data *irqd)
  247. {
  248. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  249. struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
  250. int gpio_id = irqd_to_hwirq(irqd);
  251. gpiochip_enable_irq(gc, gpio_id);
  252. set_bit(gpio_id, ljca_gpio->unmasked_irqs);
  253. }
  254. static void ljca_irq_mask(struct irq_data *irqd)
  255. {
  256. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  257. struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
  258. int gpio_id = irqd_to_hwirq(irqd);
  259. clear_bit(gpio_id, ljca_gpio->unmasked_irqs);
  260. gpiochip_disable_irq(gc, gpio_id);
  261. }
  262. static int ljca_irq_set_type(struct irq_data *irqd, unsigned int type)
  263. {
  264. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  265. struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
  266. int gpio_id = irqd_to_hwirq(irqd);
  267. ljca_gpio->connect_mode[gpio_id] = LJCA_GPIO_CONF_INTERRUPT;
  268. switch (type) {
  269. case IRQ_TYPE_LEVEL_HIGH:
  270. ljca_gpio->connect_mode[gpio_id] |=
  271. (LJCA_GPIO_CONF_LEVEL | LJCA_GPIO_CONF_PULLUP);
  272. break;
  273. case IRQ_TYPE_LEVEL_LOW:
  274. ljca_gpio->connect_mode[gpio_id] |=
  275. (LJCA_GPIO_CONF_LEVEL | LJCA_GPIO_CONF_PULLDOWN);
  276. break;
  277. case IRQ_TYPE_EDGE_BOTH:
  278. break;
  279. case IRQ_TYPE_EDGE_RISING:
  280. ljca_gpio->connect_mode[gpio_id] |=
  281. (LJCA_GPIO_CONF_EDGE | LJCA_GPIO_CONF_PULLUP);
  282. break;
  283. case IRQ_TYPE_EDGE_FALLING:
  284. ljca_gpio->connect_mode[gpio_id] |=
  285. (LJCA_GPIO_CONF_EDGE | LJCA_GPIO_CONF_PULLDOWN);
  286. break;
  287. default:
  288. return -EINVAL;
  289. }
  290. return 0;
  291. }
  292. static void ljca_irq_bus_lock(struct irq_data *irqd)
  293. {
  294. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  295. struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
  296. mutex_lock(&ljca_gpio->irq_lock);
  297. }
  298. static void ljca_irq_bus_unlock(struct irq_data *irqd)
  299. {
  300. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  301. struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
  302. int gpio_id = irqd_to_hwirq(irqd);
  303. int enabled, unmasked;
  304. enabled = test_bit(gpio_id, ljca_gpio->enabled_irqs);
  305. unmasked = test_bit(gpio_id, ljca_gpio->unmasked_irqs);
  306. if (enabled != unmasked) {
  307. if (unmasked) {
  308. ljca_gpio_config(ljca_gpio, gpio_id, 0);
  309. ljca_enable_irq(ljca_gpio, gpio_id, true);
  310. set_bit(gpio_id, ljca_gpio->enabled_irqs);
  311. } else {
  312. ljca_enable_irq(ljca_gpio, gpio_id, false);
  313. clear_bit(gpio_id, ljca_gpio->enabled_irqs);
  314. }
  315. }
  316. mutex_unlock(&ljca_gpio->irq_lock);
  317. }
  318. static const struct irq_chip ljca_gpio_irqchip = {
  319. .name = "ljca-irq",
  320. .irq_mask = ljca_irq_mask,
  321. .irq_unmask = ljca_irq_unmask,
  322. .irq_set_type = ljca_irq_set_type,
  323. .irq_bus_lock = ljca_irq_bus_lock,
  324. .irq_bus_sync_unlock = ljca_irq_bus_unlock,
  325. .flags = IRQCHIP_IMMUTABLE,
  326. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  327. };
  328. static int ljca_gpio_probe(struct auxiliary_device *auxdev,
  329. const struct auxiliary_device_id *aux_dev_id)
  330. {
  331. struct ljca_client *ljca = auxiliary_dev_to_ljca_client(auxdev);
  332. struct ljca_gpio_dev *ljca_gpio;
  333. struct gpio_irq_chip *girq;
  334. int ret;
  335. ljca_gpio = devm_kzalloc(&auxdev->dev, sizeof(*ljca_gpio), GFP_KERNEL);
  336. if (!ljca_gpio)
  337. return -ENOMEM;
  338. ljca_gpio->ljca = ljca;
  339. ljca_gpio->gpio_info = dev_get_platdata(&auxdev->dev);
  340. ljca_gpio->connect_mode = devm_kcalloc(&auxdev->dev,
  341. ljca_gpio->gpio_info->num,
  342. sizeof(*ljca_gpio->connect_mode),
  343. GFP_KERNEL);
  344. if (!ljca_gpio->connect_mode)
  345. return -ENOMEM;
  346. ret = devm_mutex_init(&auxdev->dev, &ljca_gpio->irq_lock);
  347. if (ret)
  348. return ret;
  349. ret = devm_mutex_init(&auxdev->dev, &ljca_gpio->trans_lock);
  350. if (ret)
  351. return ret;
  352. ljca_gpio->gc.direction_input = ljca_gpio_direction_input;
  353. ljca_gpio->gc.direction_output = ljca_gpio_direction_output;
  354. ljca_gpio->gc.get_direction = ljca_gpio_get_direction;
  355. ljca_gpio->gc.get = ljca_gpio_get_value;
  356. ljca_gpio->gc.set = ljca_gpio_set_value;
  357. ljca_gpio->gc.set_config = ljca_gpio_set_config;
  358. ljca_gpio->gc.init_valid_mask = ljca_gpio_init_valid_mask;
  359. ljca_gpio->gc.can_sleep = true;
  360. ljca_gpio->gc.parent = &auxdev->dev;
  361. ljca_gpio->gc.base = -1;
  362. ljca_gpio->gc.ngpio = ljca_gpio->gpio_info->num;
  363. ljca_gpio->gc.label = ACPI_COMPANION(&auxdev->dev) ?
  364. acpi_dev_name(ACPI_COMPANION(&auxdev->dev)) :
  365. dev_name(&auxdev->dev);
  366. ljca_gpio->gc.owner = THIS_MODULE;
  367. auxiliary_set_drvdata(auxdev, ljca_gpio);
  368. ljca_register_event_cb(ljca, ljca_gpio_event_cb, ljca_gpio);
  369. girq = &ljca_gpio->gc.irq;
  370. gpio_irq_chip_set_chip(girq, &ljca_gpio_irqchip);
  371. girq->parent_handler = NULL;
  372. girq->num_parents = 0;
  373. girq->parents = NULL;
  374. girq->default_type = IRQ_TYPE_NONE;
  375. girq->handler = handle_simple_irq;
  376. girq->init_valid_mask = ljca_gpio_irq_init_valid_mask;
  377. INIT_WORK(&ljca_gpio->work, ljca_gpio_async);
  378. ret = gpiochip_add_data(&ljca_gpio->gc, ljca_gpio);
  379. if (ret)
  380. ljca_unregister_event_cb(ljca);
  381. return ret;
  382. }
  383. static void ljca_gpio_remove(struct auxiliary_device *auxdev)
  384. {
  385. struct ljca_gpio_dev *ljca_gpio = auxiliary_get_drvdata(auxdev);
  386. gpiochip_remove(&ljca_gpio->gc);
  387. ljca_unregister_event_cb(ljca_gpio->ljca);
  388. cancel_work_sync(&ljca_gpio->work);
  389. }
  390. static const struct auxiliary_device_id ljca_gpio_id_table[] = {
  391. { "usb_ljca.ljca-gpio", 0 },
  392. { /* sentinel */ },
  393. };
  394. MODULE_DEVICE_TABLE(auxiliary, ljca_gpio_id_table);
  395. static struct auxiliary_driver ljca_gpio_driver = {
  396. .probe = ljca_gpio_probe,
  397. .remove = ljca_gpio_remove,
  398. .id_table = ljca_gpio_id_table,
  399. };
  400. module_auxiliary_driver(ljca_gpio_driver);
  401. MODULE_AUTHOR("Wentong Wu <wentong.wu@intel.com>");
  402. MODULE_AUTHOR("Zhifeng Wang <zhifeng.wang@intel.com>");
  403. MODULE_AUTHOR("Lixu Zhang <lixu.zhang@intel.com>");
  404. MODULE_DESCRIPTION("Intel La Jolla Cove Adapter USB-GPIO driver");
  405. MODULE_LICENSE("GPL");
  406. MODULE_IMPORT_NS("LJCA");