gpio-mlxbf2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020-2021 NVIDIA CORPORATION & AFFILIATES
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/bitops.h>
  7. #include <linux/device.h>
  8. #include <linux/gpio/driver.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/ioport.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm.h>
  17. #include <linux/resource.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/types.h>
  20. /*
  21. * There are 3 YU GPIO blocks:
  22. * gpio[0]: HOST_GPIO0->HOST_GPIO31
  23. * gpio[1]: HOST_GPIO32->HOST_GPIO63
  24. * gpio[2]: HOST_GPIO64->HOST_GPIO69
  25. */
  26. #define MLXBF2_GPIO_MAX_PINS_PER_BLOCK 32
  27. /*
  28. * arm_gpio_lock register:
  29. * bit[31] lock status: active if set
  30. * bit[15:0] set lock
  31. * The lock is enabled only if 0xd42f is written to this field
  32. */
  33. #define YU_ARM_GPIO_LOCK_ADDR 0x2801088
  34. #define YU_ARM_GPIO_LOCK_SIZE 0x8
  35. #define YU_LOCK_ACTIVE_BIT(val) (val >> 31)
  36. #define YU_ARM_GPIO_LOCK_ACQUIRE 0xd42f
  37. #define YU_ARM_GPIO_LOCK_RELEASE 0x0
  38. /*
  39. * gpio[x] block registers and their offset
  40. */
  41. #define YU_GPIO_DATAIN 0x04
  42. #define YU_GPIO_MODE1 0x08
  43. #define YU_GPIO_MODE0 0x0c
  44. #define YU_GPIO_DATASET 0x14
  45. #define YU_GPIO_DATACLEAR 0x18
  46. #define YU_GPIO_CAUSE_RISE_EN 0x44
  47. #define YU_GPIO_CAUSE_FALL_EN 0x48
  48. #define YU_GPIO_MODE1_CLEAR 0x50
  49. #define YU_GPIO_MODE0_SET 0x54
  50. #define YU_GPIO_MODE0_CLEAR 0x58
  51. #define YU_GPIO_CAUSE_OR_CAUSE_EVTEN0 0x80
  52. #define YU_GPIO_CAUSE_OR_EVTEN0 0x94
  53. #define YU_GPIO_CAUSE_OR_CLRCAUSE 0x98
  54. struct mlxbf2_gpio_context_save_regs {
  55. u32 gpio_mode0;
  56. u32 gpio_mode1;
  57. };
  58. /* BlueField-2 gpio block context structure. */
  59. struct mlxbf2_gpio_context {
  60. struct gpio_chip gc;
  61. struct irq_chip irq_chip;
  62. /* YU GPIO blocks address */
  63. void __iomem *gpio_io;
  64. struct mlxbf2_gpio_context_save_regs *csave_regs;
  65. };
  66. /* BlueField-2 gpio shared structure. */
  67. struct mlxbf2_gpio_param {
  68. void __iomem *io;
  69. struct resource *res;
  70. struct mutex *lock;
  71. };
  72. static struct resource yu_arm_gpio_lock_res =
  73. DEFINE_RES_MEM_NAMED(YU_ARM_GPIO_LOCK_ADDR, YU_ARM_GPIO_LOCK_SIZE, "YU_ARM_GPIO_LOCK");
  74. static DEFINE_MUTEX(yu_arm_gpio_lock_mutex);
  75. static struct mlxbf2_gpio_param yu_arm_gpio_lock_param = {
  76. .res = &yu_arm_gpio_lock_res,
  77. .lock = &yu_arm_gpio_lock_mutex,
  78. };
  79. /* Request memory region and map yu_arm_gpio_lock resource */
  80. static int mlxbf2_gpio_get_lock_res(struct platform_device *pdev)
  81. {
  82. struct device *dev = &pdev->dev;
  83. struct resource *res;
  84. resource_size_t size;
  85. int ret = 0;
  86. mutex_lock(yu_arm_gpio_lock_param.lock);
  87. /* Check if the memory map already exists */
  88. if (yu_arm_gpio_lock_param.io)
  89. goto exit;
  90. res = yu_arm_gpio_lock_param.res;
  91. size = resource_size(res);
  92. if (!devm_request_mem_region(dev, res->start, size, res->name)) {
  93. ret = -EFAULT;
  94. goto exit;
  95. }
  96. yu_arm_gpio_lock_param.io = devm_ioremap(dev, res->start, size);
  97. if (!yu_arm_gpio_lock_param.io)
  98. ret = -ENOMEM;
  99. exit:
  100. mutex_unlock(yu_arm_gpio_lock_param.lock);
  101. return ret;
  102. }
  103. /*
  104. * Acquire the YU arm_gpio_lock to be able to change the direction
  105. * mode. If the lock_active bit is already set, return an error.
  106. */
  107. static int mlxbf2_gpio_lock_acquire(struct mlxbf2_gpio_context *gs)
  108. {
  109. u32 arm_gpio_lock_val;
  110. mutex_lock(yu_arm_gpio_lock_param.lock);
  111. raw_spin_lock(&gs->gc.bgpio_lock);
  112. arm_gpio_lock_val = readl(yu_arm_gpio_lock_param.io);
  113. /*
  114. * When lock active bit[31] is set, ModeX is write enabled
  115. */
  116. if (YU_LOCK_ACTIVE_BIT(arm_gpio_lock_val)) {
  117. raw_spin_unlock(&gs->gc.bgpio_lock);
  118. mutex_unlock(yu_arm_gpio_lock_param.lock);
  119. return -EINVAL;
  120. }
  121. writel(YU_ARM_GPIO_LOCK_ACQUIRE, yu_arm_gpio_lock_param.io);
  122. return 0;
  123. }
  124. /*
  125. * Release the YU arm_gpio_lock after changing the direction mode.
  126. */
  127. static void mlxbf2_gpio_lock_release(struct mlxbf2_gpio_context *gs)
  128. __releases(&gs->gc.bgpio_lock)
  129. __releases(yu_arm_gpio_lock_param.lock)
  130. {
  131. writel(YU_ARM_GPIO_LOCK_RELEASE, yu_arm_gpio_lock_param.io);
  132. raw_spin_unlock(&gs->gc.bgpio_lock);
  133. mutex_unlock(yu_arm_gpio_lock_param.lock);
  134. }
  135. /*
  136. * mode0 and mode1 are both locked by the gpio_lock field.
  137. *
  138. * Together, mode0 and mode1 define the gpio Mode dependeing also
  139. * on Reg_DataOut.
  140. *
  141. * {mode1,mode0}:{Reg_DataOut=0,Reg_DataOut=1}->{DataOut=0,DataOut=1}
  142. *
  143. * {0,0}:Reg_DataOut{0,1}->{Z,Z} Input PAD
  144. * {0,1}:Reg_DataOut{0,1}->{0,1} Full drive Output PAD
  145. * {1,0}:Reg_DataOut{0,1}->{0,Z} 0-set PAD to low, 1-float
  146. * {1,1}:Reg_DataOut{0,1}->{Z,1} 0-float, 1-set PAD to high
  147. */
  148. /*
  149. * Set input direction:
  150. * {mode1,mode0} = {0,0}
  151. */
  152. static int mlxbf2_gpio_direction_input(struct gpio_chip *chip,
  153. unsigned int offset)
  154. {
  155. struct mlxbf2_gpio_context *gs = gpiochip_get_data(chip);
  156. int ret;
  157. /*
  158. * Although the arm_gpio_lock was set in the probe function, check again
  159. * if it is still enabled to be able to write to the ModeX registers.
  160. */
  161. ret = mlxbf2_gpio_lock_acquire(gs);
  162. if (ret < 0)
  163. return ret;
  164. writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE0_CLEAR);
  165. writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE1_CLEAR);
  166. mlxbf2_gpio_lock_release(gs);
  167. return ret;
  168. }
  169. /*
  170. * Set output direction:
  171. * {mode1,mode0} = {0,1}
  172. */
  173. static int mlxbf2_gpio_direction_output(struct gpio_chip *chip,
  174. unsigned int offset,
  175. int value)
  176. {
  177. struct mlxbf2_gpio_context *gs = gpiochip_get_data(chip);
  178. int ret = 0;
  179. /*
  180. * Although the arm_gpio_lock was set in the probe function,
  181. * check again it is still enabled to be able to write to the
  182. * ModeX registers.
  183. */
  184. ret = mlxbf2_gpio_lock_acquire(gs);
  185. if (ret < 0)
  186. return ret;
  187. writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE1_CLEAR);
  188. writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE0_SET);
  189. mlxbf2_gpio_lock_release(gs);
  190. return ret;
  191. }
  192. static void mlxbf2_gpio_irq_enable(struct irq_data *irqd)
  193. {
  194. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  195. struct mlxbf2_gpio_context *gs = gpiochip_get_data(gc);
  196. int offset = irqd_to_hwirq(irqd);
  197. unsigned long flags;
  198. u32 val;
  199. raw_spin_lock_irqsave(&gs->gc.bgpio_lock, flags);
  200. val = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_CLRCAUSE);
  201. val |= BIT(offset);
  202. writel(val, gs->gpio_io + YU_GPIO_CAUSE_OR_CLRCAUSE);
  203. val = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_EVTEN0);
  204. val |= BIT(offset);
  205. writel(val, gs->gpio_io + YU_GPIO_CAUSE_OR_EVTEN0);
  206. raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
  207. }
  208. static void mlxbf2_gpio_irq_disable(struct irq_data *irqd)
  209. {
  210. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  211. struct mlxbf2_gpio_context *gs = gpiochip_get_data(gc);
  212. int offset = irqd_to_hwirq(irqd);
  213. unsigned long flags;
  214. u32 val;
  215. raw_spin_lock_irqsave(&gs->gc.bgpio_lock, flags);
  216. val = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_EVTEN0);
  217. val &= ~BIT(offset);
  218. writel(val, gs->gpio_io + YU_GPIO_CAUSE_OR_EVTEN0);
  219. raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
  220. }
  221. static irqreturn_t mlxbf2_gpio_irq_handler(int irq, void *ptr)
  222. {
  223. struct mlxbf2_gpio_context *gs = ptr;
  224. struct gpio_chip *gc = &gs->gc;
  225. unsigned long pending;
  226. u32 level;
  227. pending = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_CAUSE_EVTEN0);
  228. writel(pending, gs->gpio_io + YU_GPIO_CAUSE_OR_CLRCAUSE);
  229. for_each_set_bit(level, &pending, gc->ngpio)
  230. generic_handle_domain_irq_safe(gc->irq.domain, level);
  231. return IRQ_RETVAL(pending);
  232. }
  233. static int
  234. mlxbf2_gpio_irq_set_type(struct irq_data *irqd, unsigned int type)
  235. {
  236. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  237. struct mlxbf2_gpio_context *gs = gpiochip_get_data(gc);
  238. int offset = irqd_to_hwirq(irqd);
  239. unsigned long flags;
  240. bool fall = false;
  241. bool rise = false;
  242. u32 val;
  243. switch (type & IRQ_TYPE_SENSE_MASK) {
  244. case IRQ_TYPE_EDGE_BOTH:
  245. fall = true;
  246. rise = true;
  247. break;
  248. case IRQ_TYPE_EDGE_RISING:
  249. rise = true;
  250. break;
  251. case IRQ_TYPE_EDGE_FALLING:
  252. fall = true;
  253. break;
  254. default:
  255. return -EINVAL;
  256. }
  257. raw_spin_lock_irqsave(&gs->gc.bgpio_lock, flags);
  258. if (fall) {
  259. val = readl(gs->gpio_io + YU_GPIO_CAUSE_FALL_EN);
  260. val |= BIT(offset);
  261. writel(val, gs->gpio_io + YU_GPIO_CAUSE_FALL_EN);
  262. }
  263. if (rise) {
  264. val = readl(gs->gpio_io + YU_GPIO_CAUSE_RISE_EN);
  265. val |= BIT(offset);
  266. writel(val, gs->gpio_io + YU_GPIO_CAUSE_RISE_EN);
  267. }
  268. raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
  269. return 0;
  270. }
  271. /* BlueField-2 GPIO driver initialization routine. */
  272. static int
  273. mlxbf2_gpio_probe(struct platform_device *pdev)
  274. {
  275. struct mlxbf2_gpio_context *gs;
  276. struct device *dev = &pdev->dev;
  277. struct gpio_irq_chip *girq;
  278. struct gpio_chip *gc;
  279. unsigned int npins;
  280. const char *name;
  281. int ret, irq;
  282. name = dev_name(dev);
  283. gs = devm_kzalloc(dev, sizeof(*gs), GFP_KERNEL);
  284. if (!gs)
  285. return -ENOMEM;
  286. /* YU GPIO block address */
  287. gs->gpio_io = devm_platform_ioremap_resource(pdev, 0);
  288. if (IS_ERR(gs->gpio_io))
  289. return PTR_ERR(gs->gpio_io);
  290. ret = mlxbf2_gpio_get_lock_res(pdev);
  291. if (ret) {
  292. dev_err(dev, "Failed to get yu_arm_gpio_lock resource\n");
  293. return ret;
  294. }
  295. if (device_property_read_u32(dev, "npins", &npins))
  296. npins = MLXBF2_GPIO_MAX_PINS_PER_BLOCK;
  297. gc = &gs->gc;
  298. ret = bgpio_init(gc, dev, 4,
  299. gs->gpio_io + YU_GPIO_DATAIN,
  300. gs->gpio_io + YU_GPIO_DATASET,
  301. gs->gpio_io + YU_GPIO_DATACLEAR,
  302. NULL,
  303. NULL,
  304. 0);
  305. if (ret) {
  306. dev_err(dev, "bgpio_init failed\n");
  307. return ret;
  308. }
  309. gc->direction_input = mlxbf2_gpio_direction_input;
  310. gc->direction_output = mlxbf2_gpio_direction_output;
  311. gc->ngpio = npins;
  312. gc->owner = THIS_MODULE;
  313. irq = platform_get_irq(pdev, 0);
  314. if (irq >= 0) {
  315. gs->irq_chip.name = name;
  316. gs->irq_chip.irq_set_type = mlxbf2_gpio_irq_set_type;
  317. gs->irq_chip.irq_enable = mlxbf2_gpio_irq_enable;
  318. gs->irq_chip.irq_disable = mlxbf2_gpio_irq_disable;
  319. girq = &gs->gc.irq;
  320. girq->chip = &gs->irq_chip;
  321. girq->handler = handle_simple_irq;
  322. girq->default_type = IRQ_TYPE_NONE;
  323. /* This will let us handle the parent IRQ in the driver */
  324. girq->num_parents = 0;
  325. girq->parents = NULL;
  326. girq->parent_handler = NULL;
  327. /*
  328. * Directly request the irq here instead of passing
  329. * a flow-handler because the irq is shared.
  330. */
  331. ret = devm_request_irq(dev, irq, mlxbf2_gpio_irq_handler,
  332. IRQF_SHARED, name, gs);
  333. if (ret) {
  334. dev_err(dev, "failed to request IRQ");
  335. return ret;
  336. }
  337. }
  338. platform_set_drvdata(pdev, gs);
  339. ret = devm_gpiochip_add_data(dev, &gs->gc, gs);
  340. if (ret) {
  341. dev_err(dev, "Failed adding memory mapped gpiochip\n");
  342. return ret;
  343. }
  344. return 0;
  345. }
  346. static int __maybe_unused mlxbf2_gpio_suspend(struct device *dev)
  347. {
  348. struct mlxbf2_gpio_context *gs = dev_get_drvdata(dev);
  349. gs->csave_regs->gpio_mode0 = readl(gs->gpio_io +
  350. YU_GPIO_MODE0);
  351. gs->csave_regs->gpio_mode1 = readl(gs->gpio_io +
  352. YU_GPIO_MODE1);
  353. return 0;
  354. }
  355. static int __maybe_unused mlxbf2_gpio_resume(struct device *dev)
  356. {
  357. struct mlxbf2_gpio_context *gs = dev_get_drvdata(dev);
  358. writel(gs->csave_regs->gpio_mode0, gs->gpio_io +
  359. YU_GPIO_MODE0);
  360. writel(gs->csave_regs->gpio_mode1, gs->gpio_io +
  361. YU_GPIO_MODE1);
  362. return 0;
  363. }
  364. static SIMPLE_DEV_PM_OPS(mlxbf2_pm_ops, mlxbf2_gpio_suspend, mlxbf2_gpio_resume);
  365. static const struct acpi_device_id __maybe_unused mlxbf2_gpio_acpi_match[] = {
  366. { "MLNXBF22", 0 },
  367. {},
  368. };
  369. MODULE_DEVICE_TABLE(acpi, mlxbf2_gpio_acpi_match);
  370. static struct platform_driver mlxbf2_gpio_driver = {
  371. .driver = {
  372. .name = "mlxbf2_gpio",
  373. .acpi_match_table = mlxbf2_gpio_acpi_match,
  374. .pm = &mlxbf2_pm_ops,
  375. },
  376. .probe = mlxbf2_gpio_probe,
  377. };
  378. module_platform_driver(mlxbf2_gpio_driver);
  379. MODULE_DESCRIPTION("Mellanox BlueField-2 GPIO Driver");
  380. MODULE_AUTHOR("Asmaa Mnebhi <[email protected]>");
  381. MODULE_LICENSE("GPL v2");