gpio-nomadik.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * GPIO driver for the IP block found in the Nomadik SoC; it is an AMBA device,
  4. * managing 32 pins with alternate functions. It can also handle the STA2X11
  5. * block from ST.
  6. *
  7. * The GPIO chips are shared with pinctrl-nomadik if used; it needs access for
  8. * pinmuxing functionality and others.
  9. *
  10. * This driver also handles the mobileye,eyeq5-gpio compatible. It is an STA2X11
  11. * but with only data, direction and interrupts register active. We want to
  12. * avoid touching SLPM, RWIMSC, FWIMSC, AFSLA and AFSLB registers; that is,
  13. * wake and alternate function registers. It is NOT compatible with
  14. * pinctrl-nomadik.
  15. *
  16. * Copyright (C) 2008,2009 STMicroelectronics
  17. * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
  18. * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
  19. * Copyright (C) 2011-2013 Linus Walleij <linus.walleij@linaro.org>
  20. */
  21. #include <linux/cleanup.h>
  22. #include <linux/clk.h>
  23. #include <linux/gpio/driver.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/kernel.h>
  26. #include <linux/mod_devicetable.h>
  27. #include <linux/pinctrl/pinctrl.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/property.h>
  30. #include <linux/reset.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/slab.h>
  33. #include <linux/types.h>
  34. #include <linux/gpio/gpio-nomadik.h>
  35. #ifndef CONFIG_PINCTRL_NOMADIK
  36. static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
  37. #endif
  38. void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip, unsigned int offset,
  39. enum nmk_gpio_slpm mode)
  40. {
  41. u32 slpm;
  42. /* We should NOT have been called. */
  43. if (WARN_ON(nmk_chip->is_mobileye_soc))
  44. return;
  45. slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
  46. if (mode == NMK_GPIO_SLPM_NOCHANGE)
  47. slpm |= BIT(offset);
  48. else
  49. slpm &= ~BIT(offset);
  50. writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
  51. }
  52. static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
  53. unsigned int offset, int val)
  54. {
  55. if (val)
  56. writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DATS);
  57. else
  58. writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DATC);
  59. }
  60. void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
  61. unsigned int offset, int val)
  62. {
  63. writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DIRS);
  64. __nmk_gpio_set_output(nmk_chip, offset, val);
  65. }
  66. /* IRQ functions */
  67. static void nmk_gpio_irq_ack(struct irq_data *d)
  68. {
  69. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  70. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(gc);
  71. clk_enable(nmk_chip->clk);
  72. writel(BIT(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
  73. clk_disable(nmk_chip->clk);
  74. }
  75. enum nmk_gpio_irq_type {
  76. NORMAL,
  77. WAKE,
  78. };
  79. static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
  80. int offset, enum nmk_gpio_irq_type which,
  81. bool enable)
  82. {
  83. u32 *rimscval;
  84. u32 *fimscval;
  85. u32 rimscreg;
  86. u32 fimscreg;
  87. if (which == NORMAL) {
  88. rimscreg = NMK_GPIO_RIMSC;
  89. fimscreg = NMK_GPIO_FIMSC;
  90. rimscval = &nmk_chip->rimsc;
  91. fimscval = &nmk_chip->fimsc;
  92. } else {
  93. /* We should NOT have been called. */
  94. if (WARN_ON(nmk_chip->is_mobileye_soc))
  95. return;
  96. rimscreg = NMK_GPIO_RWIMSC;
  97. fimscreg = NMK_GPIO_FWIMSC;
  98. rimscval = &nmk_chip->rwimsc;
  99. fimscval = &nmk_chip->fwimsc;
  100. }
  101. /* we must individually set/clear the two edges */
  102. if (nmk_chip->edge_rising & BIT(offset)) {
  103. if (enable)
  104. *rimscval |= BIT(offset);
  105. else
  106. *rimscval &= ~BIT(offset);
  107. writel(*rimscval, nmk_chip->addr + rimscreg);
  108. }
  109. if (nmk_chip->edge_falling & BIT(offset)) {
  110. if (enable)
  111. *fimscval |= BIT(offset);
  112. else
  113. *fimscval &= ~BIT(offset);
  114. writel(*fimscval, nmk_chip->addr + fimscreg);
  115. }
  116. }
  117. static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
  118. int offset, bool on)
  119. {
  120. /* We should NOT have been called. */
  121. if (WARN_ON(nmk_chip->is_mobileye_soc))
  122. return;
  123. /*
  124. * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
  125. * disabled, since setting SLPM to 1 increases power consumption, and
  126. * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
  127. */
  128. if (nmk_chip->sleepmode && on) {
  129. __nmk_gpio_set_slpm(nmk_chip, offset,
  130. NMK_GPIO_SLPM_WAKEUP_ENABLE);
  131. }
  132. __nmk_gpio_irq_modify(nmk_chip, offset, WAKE, on);
  133. }
  134. static void nmk_gpio_irq_maskunmask(struct nmk_gpio_chip *nmk_chip,
  135. struct irq_data *d, bool enable)
  136. {
  137. unsigned long flags;
  138. clk_enable(nmk_chip->clk);
  139. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  140. spin_lock(&nmk_chip->lock);
  141. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
  142. if (!nmk_chip->is_mobileye_soc && !(nmk_chip->real_wake & BIT(d->hwirq)))
  143. __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
  144. spin_unlock(&nmk_chip->lock);
  145. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  146. clk_disable(nmk_chip->clk);
  147. }
  148. static void nmk_gpio_irq_mask(struct irq_data *d)
  149. {
  150. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  151. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(gc);
  152. nmk_gpio_irq_maskunmask(nmk_chip, d, false);
  153. gpiochip_disable_irq(gc, irqd_to_hwirq(d));
  154. }
  155. static void nmk_gpio_irq_unmask(struct irq_data *d)
  156. {
  157. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  158. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(gc);
  159. gpiochip_enable_irq(gc, irqd_to_hwirq(d));
  160. nmk_gpio_irq_maskunmask(nmk_chip, d, true);
  161. }
  162. static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  163. {
  164. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  165. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(gc);
  166. unsigned long flags;
  167. /* Handler is registered in all cases. */
  168. if (nmk_chip->is_mobileye_soc)
  169. return -ENXIO;
  170. clk_enable(nmk_chip->clk);
  171. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  172. spin_lock(&nmk_chip->lock);
  173. if (irqd_irq_disabled(d))
  174. __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
  175. if (on)
  176. nmk_chip->real_wake |= BIT(d->hwirq);
  177. else
  178. nmk_chip->real_wake &= ~BIT(d->hwirq);
  179. spin_unlock(&nmk_chip->lock);
  180. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  181. clk_disable(nmk_chip->clk);
  182. return 0;
  183. }
  184. static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  185. {
  186. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  187. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(gc);
  188. bool enabled = !irqd_irq_disabled(d);
  189. bool wake = irqd_is_wakeup_set(d);
  190. unsigned long flags;
  191. if (type & IRQ_TYPE_LEVEL_HIGH)
  192. return -EINVAL;
  193. if (type & IRQ_TYPE_LEVEL_LOW)
  194. return -EINVAL;
  195. clk_enable(nmk_chip->clk);
  196. spin_lock_irqsave(&nmk_chip->lock, flags);
  197. if (enabled)
  198. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
  199. if (!nmk_chip->is_mobileye_soc && (enabled || wake))
  200. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
  201. nmk_chip->edge_rising &= ~BIT(d->hwirq);
  202. if (type & IRQ_TYPE_EDGE_RISING)
  203. nmk_chip->edge_rising |= BIT(d->hwirq);
  204. nmk_chip->edge_falling &= ~BIT(d->hwirq);
  205. if (type & IRQ_TYPE_EDGE_FALLING)
  206. nmk_chip->edge_falling |= BIT(d->hwirq);
  207. if (enabled)
  208. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
  209. if (!nmk_chip->is_mobileye_soc && (enabled || wake))
  210. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
  211. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  212. clk_disable(nmk_chip->clk);
  213. return 0;
  214. }
  215. static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
  216. {
  217. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  218. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(gc);
  219. clk_enable(nmk_chip->clk);
  220. nmk_gpio_irq_unmask(d);
  221. return 0;
  222. }
  223. static void nmk_gpio_irq_shutdown(struct irq_data *d)
  224. {
  225. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  226. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(gc);
  227. nmk_gpio_irq_mask(d);
  228. clk_disable(nmk_chip->clk);
  229. }
  230. static irqreturn_t nmk_gpio_irq_handler(int irq, void *dev_id)
  231. {
  232. struct nmk_gpio_chip *nmk_chip = dev_id;
  233. struct gpio_chip *chip = &nmk_chip->chip;
  234. unsigned long mask = GENMASK(chip->ngpio - 1, 0);
  235. unsigned long status;
  236. int bit;
  237. clk_enable(nmk_chip->clk);
  238. status = readl(nmk_chip->addr + NMK_GPIO_IS);
  239. /* Ensure we cannot leave pending bits; this should never occur. */
  240. if (unlikely(status & ~mask))
  241. writel(status & ~mask, nmk_chip->addr + NMK_GPIO_IC);
  242. clk_disable(nmk_chip->clk);
  243. for_each_set_bit(bit, &status, chip->ngpio)
  244. generic_handle_domain_irq_safe(chip->irq.domain, bit);
  245. return IRQ_RETVAL((status & mask) != 0);
  246. }
  247. /* I/O Functions */
  248. static int nmk_gpio_get_dir(struct gpio_chip *chip, unsigned int offset)
  249. {
  250. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
  251. int dir;
  252. clk_enable(nmk_chip->clk);
  253. dir = readl(nmk_chip->addr + NMK_GPIO_DIR) & BIT(offset);
  254. clk_disable(nmk_chip->clk);
  255. if (dir)
  256. return GPIO_LINE_DIRECTION_OUT;
  257. return GPIO_LINE_DIRECTION_IN;
  258. }
  259. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned int offset)
  260. {
  261. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
  262. clk_enable(nmk_chip->clk);
  263. writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DIRC);
  264. clk_disable(nmk_chip->clk);
  265. return 0;
  266. }
  267. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned int offset)
  268. {
  269. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
  270. int value;
  271. clk_enable(nmk_chip->clk);
  272. value = !!(readl(nmk_chip->addr + NMK_GPIO_DAT) & BIT(offset));
  273. clk_disable(nmk_chip->clk);
  274. return value;
  275. }
  276. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned int offset,
  277. int val)
  278. {
  279. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
  280. clk_enable(nmk_chip->clk);
  281. __nmk_gpio_set_output(nmk_chip, offset, val);
  282. clk_disable(nmk_chip->clk);
  283. }
  284. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned int offset,
  285. int val)
  286. {
  287. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
  288. clk_enable(nmk_chip->clk);
  289. __nmk_gpio_make_output(nmk_chip, offset, val);
  290. clk_disable(nmk_chip->clk);
  291. return 0;
  292. }
  293. #ifdef CONFIG_DEBUG_FS
  294. static int nmk_gpio_get_mode(struct nmk_gpio_chip *nmk_chip, int offset)
  295. {
  296. u32 afunc, bfunc;
  297. /* We don't support modes. */
  298. if (nmk_chip->is_mobileye_soc)
  299. return NMK_GPIO_ALT_GPIO;
  300. clk_enable(nmk_chip->clk);
  301. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & BIT(offset);
  302. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & BIT(offset);
  303. clk_disable(nmk_chip->clk);
  304. return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
  305. }
  306. void nmk_gpio_dbg_show_one(struct seq_file *s, struct pinctrl_dev *pctldev,
  307. struct gpio_chip *chip, unsigned int offset,
  308. unsigned int gpio)
  309. {
  310. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
  311. int mode;
  312. bool is_out;
  313. bool data_out;
  314. bool pull;
  315. static const char * const modes[] = {
  316. [NMK_GPIO_ALT_GPIO] = "gpio",
  317. [NMK_GPIO_ALT_A] = "altA",
  318. [NMK_GPIO_ALT_B] = "altB",
  319. [NMK_GPIO_ALT_C] = "altC",
  320. [NMK_GPIO_ALT_C + 1] = "altC1",
  321. [NMK_GPIO_ALT_C + 2] = "altC2",
  322. [NMK_GPIO_ALT_C + 3] = "altC3",
  323. [NMK_GPIO_ALT_C + 4] = "altC4",
  324. };
  325. char *label = gpiochip_dup_line_label(chip, offset);
  326. if (IS_ERR(label))
  327. return;
  328. clk_enable(nmk_chip->clk);
  329. is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & BIT(offset));
  330. pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & BIT(offset));
  331. data_out = !!(readl(nmk_chip->addr + NMK_GPIO_DAT) & BIT(offset));
  332. mode = nmk_gpio_get_mode(nmk_chip, offset);
  333. #ifdef CONFIG_PINCTRL_NOMADIK
  334. if (mode == NMK_GPIO_ALT_C && pctldev)
  335. mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio);
  336. #endif
  337. if (is_out) {
  338. seq_printf(s, " gpio-%-3d (%-20.20s) out %s %s",
  339. gpio,
  340. label ?: "(none)",
  341. data_out ? "hi" : "lo",
  342. (mode < 0) ? "unknown" : modes[mode]);
  343. } else {
  344. int irq = chip->to_irq(chip, offset);
  345. const int pullidx = pull ? 1 : 0;
  346. int val;
  347. static const char * const pulls[] = {
  348. "none ",
  349. "pull enabled",
  350. };
  351. seq_printf(s, " gpio-%-3d (%-20.20s) in %s %s",
  352. gpio,
  353. label ?: "(none)",
  354. pulls[pullidx],
  355. (mode < 0) ? "unknown" : modes[mode]);
  356. val = nmk_gpio_get_input(chip, offset);
  357. seq_printf(s, " VAL %d", val);
  358. /*
  359. * This races with request_irq(), set_irq_type(),
  360. * and set_irq_wake() ... but those are "rare".
  361. */
  362. if (irq > 0 && irq_has_action(irq)) {
  363. char *trigger;
  364. bool wake;
  365. if (nmk_chip->edge_rising & BIT(offset))
  366. trigger = "edge-rising";
  367. else if (nmk_chip->edge_falling & BIT(offset))
  368. trigger = "edge-falling";
  369. else
  370. trigger = "edge-undefined";
  371. wake = !!(nmk_chip->real_wake & BIT(offset));
  372. seq_printf(s, " irq-%d %s%s",
  373. irq, trigger, wake ? " wakeup" : "");
  374. }
  375. }
  376. clk_disable(nmk_chip->clk);
  377. }
  378. static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  379. {
  380. unsigned int i, gpio = chip->base;
  381. for (i = 0; i < chip->ngpio; i++, gpio++) {
  382. nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio);
  383. seq_puts(s, "\n");
  384. }
  385. }
  386. #else
  387. #define nmk_gpio_dbg_show NULL
  388. #endif
  389. /*
  390. * We will allocate memory for the state container using devm* allocators
  391. * binding to the first device reaching this point, it doesn't matter if
  392. * it is the pin controller or GPIO driver. However we need to use the right
  393. * platform device when looking up resources so pay attention to pdev.
  394. */
  395. struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
  396. struct platform_device *pdev)
  397. {
  398. struct nmk_gpio_chip *nmk_chip;
  399. struct platform_device *gpio_pdev;
  400. struct device *dev = &pdev->dev;
  401. struct reset_control *reset;
  402. struct device *gpio_dev;
  403. struct gpio_chip *chip;
  404. struct resource *res;
  405. struct clk *clk;
  406. void __iomem *base;
  407. u32 id, ngpio;
  408. int ret;
  409. gpio_dev = bus_find_device_by_fwnode(&platform_bus_type, fwnode);
  410. if (!gpio_dev) {
  411. dev_err(dev, "populate \"%pfwP\": device not found\n", fwnode);
  412. return ERR_PTR(-ENODEV);
  413. }
  414. gpio_pdev = to_platform_device(gpio_dev);
  415. if (device_property_read_u32(gpio_dev, "gpio-bank", &id)) {
  416. dev_err(dev, "populate: gpio-bank property not found\n");
  417. platform_device_put(gpio_pdev);
  418. return ERR_PTR(-EINVAL);
  419. }
  420. #ifdef CONFIG_PINCTRL_NOMADIK
  421. if (id >= ARRAY_SIZE(nmk_gpio_chips)) {
  422. dev_err(dev, "populate: invalid id: %u\n", id);
  423. platform_device_put(gpio_pdev);
  424. return ERR_PTR(-EINVAL);
  425. }
  426. /* Already populated? */
  427. nmk_chip = nmk_gpio_chips[id];
  428. if (nmk_chip) {
  429. platform_device_put(gpio_pdev);
  430. return nmk_chip;
  431. }
  432. #endif
  433. nmk_chip = devm_kzalloc(dev, sizeof(*nmk_chip), GFP_KERNEL);
  434. if (!nmk_chip) {
  435. platform_device_put(gpio_pdev);
  436. return ERR_PTR(-ENOMEM);
  437. }
  438. if (device_property_read_u32(gpio_dev, "ngpios", &ngpio)) {
  439. ngpio = NMK_GPIO_PER_CHIP;
  440. dev_dbg(dev, "populate: using default ngpio (%u)\n", ngpio);
  441. }
  442. nmk_chip->is_mobileye_soc = device_is_compatible(gpio_dev,
  443. "mobileye,eyeq5-gpio");
  444. nmk_chip->bank = id;
  445. chip = &nmk_chip->chip;
  446. chip->base = -1;
  447. chip->ngpio = ngpio;
  448. chip->label = dev_name(gpio_dev);
  449. chip->parent = gpio_dev;
  450. /* NOTE: different devices! No devm_platform_ioremap_resource() here! */
  451. res = platform_get_resource(gpio_pdev, IORESOURCE_MEM, 0);
  452. base = devm_ioremap_resource(dev, res);
  453. if (IS_ERR(base)) {
  454. platform_device_put(gpio_pdev);
  455. return ERR_CAST(base);
  456. }
  457. nmk_chip->addr = base;
  458. /* NOTE: do not use devm_ here! */
  459. clk = clk_get_optional(gpio_dev, NULL);
  460. if (IS_ERR(clk)) {
  461. platform_device_put(gpio_pdev);
  462. return ERR_CAST(clk);
  463. }
  464. clk_prepare(clk);
  465. nmk_chip->clk = clk;
  466. /* NOTE: do not use devm_ here! */
  467. reset = reset_control_get_optional_shared(gpio_dev, NULL);
  468. if (IS_ERR(reset)) {
  469. clk_unprepare(clk);
  470. clk_put(clk);
  471. platform_device_put(gpio_pdev);
  472. dev_err(dev, "failed getting reset control: %pe\n",
  473. reset);
  474. return ERR_CAST(reset);
  475. }
  476. /*
  477. * Reset might be shared and asserts/deasserts calls are unbalanced. We
  478. * only support sharing this reset with other gpio-nomadik devices that
  479. * use this reset to ensure deassertion at probe.
  480. */
  481. ret = reset_control_deassert(reset);
  482. if (ret) {
  483. reset_control_put(reset);
  484. clk_unprepare(clk);
  485. clk_put(clk);
  486. platform_device_put(gpio_pdev);
  487. dev_err(dev, "failed reset deassert: %d\n", ret);
  488. return ERR_PTR(ret);
  489. }
  490. #ifdef CONFIG_PINCTRL_NOMADIK
  491. nmk_gpio_chips[id] = nmk_chip;
  492. #endif
  493. return nmk_chip;
  494. }
  495. static void nmk_gpio_irq_print_chip(struct irq_data *d, struct seq_file *p)
  496. {
  497. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  498. struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(gc);
  499. seq_printf(p, "nmk%u-%u-%u", nmk_chip->bank,
  500. gc->base, gc->base + gc->ngpio - 1);
  501. }
  502. static const struct irq_chip nmk_irq_chip = {
  503. .irq_ack = nmk_gpio_irq_ack,
  504. .irq_mask = nmk_gpio_irq_mask,
  505. .irq_unmask = nmk_gpio_irq_unmask,
  506. .irq_set_type = nmk_gpio_irq_set_type,
  507. .irq_set_wake = nmk_gpio_irq_set_wake,
  508. .irq_startup = nmk_gpio_irq_startup,
  509. .irq_shutdown = nmk_gpio_irq_shutdown,
  510. .irq_print_chip = nmk_gpio_irq_print_chip,
  511. .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE,
  512. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  513. };
  514. static int nmk_gpio_probe(struct platform_device *pdev)
  515. {
  516. struct device *dev = &pdev->dev;
  517. struct nmk_gpio_chip *nmk_chip;
  518. struct gpio_irq_chip *girq;
  519. bool supports_sleepmode;
  520. struct gpio_chip *chip;
  521. int irq;
  522. int ret;
  523. nmk_chip = nmk_gpio_populate_chip(dev_fwnode(dev), pdev);
  524. if (IS_ERR(nmk_chip)) {
  525. dev_err(dev, "could not populate nmk chip struct\n");
  526. return PTR_ERR(nmk_chip);
  527. }
  528. supports_sleepmode =
  529. device_property_read_bool(dev, "st,supports-sleepmode");
  530. /* Correct platform device ID */
  531. pdev->id = nmk_chip->bank;
  532. irq = platform_get_irq(pdev, 0);
  533. if (irq < 0)
  534. return irq;
  535. /*
  536. * The virt address in nmk_chip->addr is in the nomadik register space,
  537. * so we can simply convert the resource address, without remapping
  538. */
  539. nmk_chip->sleepmode = supports_sleepmode;
  540. spin_lock_init(&nmk_chip->lock);
  541. chip = &nmk_chip->chip;
  542. chip->parent = dev;
  543. chip->request = gpiochip_generic_request;
  544. chip->free = gpiochip_generic_free;
  545. chip->get_direction = nmk_gpio_get_dir;
  546. chip->direction_input = nmk_gpio_make_input;
  547. chip->get = nmk_gpio_get_input;
  548. chip->direction_output = nmk_gpio_make_output;
  549. chip->set = nmk_gpio_set_output;
  550. chip->dbg_show = nmk_gpio_dbg_show;
  551. chip->can_sleep = false;
  552. chip->owner = THIS_MODULE;
  553. girq = &chip->irq;
  554. gpio_irq_chip_set_chip(girq, &nmk_irq_chip);
  555. girq->parent_handler = NULL;
  556. girq->num_parents = 0;
  557. girq->parents = NULL;
  558. girq->default_type = IRQ_TYPE_NONE;
  559. girq->handler = handle_edge_irq;
  560. ret = devm_request_irq(dev, irq, nmk_gpio_irq_handler, IRQF_SHARED,
  561. dev_name(dev), nmk_chip);
  562. if (ret) {
  563. dev_err(dev, "failed requesting IRQ\n");
  564. return ret;
  565. }
  566. if (!nmk_chip->is_mobileye_soc) {
  567. clk_enable(nmk_chip->clk);
  568. nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
  569. clk_disable(nmk_chip->clk);
  570. }
  571. ret = gpiochip_add_data(chip, nmk_chip);
  572. if (ret)
  573. return ret;
  574. platform_set_drvdata(pdev, nmk_chip);
  575. dev_info(dev, "chip registered\n");
  576. return 0;
  577. }
  578. static const struct of_device_id nmk_gpio_match[] = {
  579. { .compatible = "st,nomadik-gpio", },
  580. { .compatible = "mobileye,eyeq5-gpio", },
  581. {}
  582. };
  583. static struct platform_driver nmk_gpio_driver = {
  584. .driver = {
  585. .name = "nomadik-gpio",
  586. .of_match_table = nmk_gpio_match,
  587. .suppress_bind_attrs = true,
  588. },
  589. .probe = nmk_gpio_probe,
  590. };
  591. static int __init nmk_gpio_init(void)
  592. {
  593. return platform_driver_register(&nmk_gpio_driver);
  594. }
  595. subsys_initcall(nmk_gpio_init);