gpio-samsung.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2009-2011 Samsung Electronics Co., Ltd.
  4. // http://www.samsung.com/
  5. //
  6. // Copyright 2008 Openmoko, Inc.
  7. // Copyright 2008 Simtec Electronics
  8. // Ben Dooks <ben@simtec.co.uk>
  9. // http://armlinux.simtec.co.uk/
  10. //
  11. // Samsung - GPIOlib support
  12. #include <linux/kernel.h>
  13. #include <linux/irq.h>
  14. #include <linux/io.h>
  15. #include <linux/gpio.h>
  16. #include <linux/init.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/device.h>
  21. #include <linux/ioport.h>
  22. #include <linux/of.h>
  23. #include <linux/slab.h>
  24. #include <linux/of_address.h>
  25. #include <asm/irq.h>
  26. #include "irqs.h"
  27. #include "map.h"
  28. #include "regs-gpio.h"
  29. #include "gpio-samsung.h"
  30. #include "cpu.h"
  31. #include "gpio-core.h"
  32. #include "gpio-cfg.h"
  33. #include "gpio-cfg-helpers.h"
  34. #include "pm.h"
  35. static int samsung_gpio_setpull_updown(struct samsung_gpio_chip *chip,
  36. unsigned int off, samsung_gpio_pull_t pull)
  37. {
  38. void __iomem *reg = chip->base + 0x08;
  39. int shift = off * 2;
  40. u32 pup;
  41. pup = __raw_readl(reg);
  42. pup &= ~(3 << shift);
  43. pup |= pull << shift;
  44. __raw_writel(pup, reg);
  45. return 0;
  46. }
  47. static samsung_gpio_pull_t samsung_gpio_getpull_updown(struct samsung_gpio_chip *chip,
  48. unsigned int off)
  49. {
  50. void __iomem *reg = chip->base + 0x08;
  51. int shift = off * 2;
  52. u32 pup = __raw_readl(reg);
  53. pup >>= shift;
  54. pup &= 0x3;
  55. return (__force samsung_gpio_pull_t)pup;
  56. }
  57. static int samsung_gpio_setcfg_2bit(struct samsung_gpio_chip *chip,
  58. unsigned int off, unsigned int cfg)
  59. {
  60. void __iomem *reg = chip->base;
  61. unsigned int shift = off * 2;
  62. u32 con;
  63. if (samsung_gpio_is_cfg_special(cfg)) {
  64. cfg &= 0xf;
  65. if (cfg > 3)
  66. return -EINVAL;
  67. cfg <<= shift;
  68. }
  69. con = __raw_readl(reg);
  70. con &= ~(0x3 << shift);
  71. con |= cfg;
  72. __raw_writel(con, reg);
  73. return 0;
  74. }
  75. /*
  76. * samsung_gpio_getcfg_2bit - Samsung 2bit style GPIO configuration read.
  77. * @chip: The gpio chip that is being configured.
  78. * @off: The offset for the GPIO being configured.
  79. *
  80. * The reverse of samsung_gpio_setcfg_2bit(). Will return a value which
  81. * could be directly passed back to samsung_gpio_setcfg_2bit(), from the
  82. * S3C_GPIO_SPECIAL() macro.
  83. */
  84. static unsigned int samsung_gpio_getcfg_2bit(struct samsung_gpio_chip *chip,
  85. unsigned int off)
  86. {
  87. u32 con;
  88. con = __raw_readl(chip->base);
  89. con >>= off * 2;
  90. con &= 3;
  91. /* this conversion works for IN and OUT as well as special mode */
  92. return S3C_GPIO_SPECIAL(con);
  93. }
  94. /*
  95. * samsung_gpio_setcfg_4bit - Samsung 4bit single register GPIO config.
  96. * @chip: The gpio chip that is being configured.
  97. * @off: The offset for the GPIO being configured.
  98. * @cfg: The configuration value to set.
  99. *
  100. * This helper deal with the GPIO cases where the control register has 4 bits
  101. * of control per GPIO, generally in the form of:
  102. * 0000 = Input
  103. * 0001 = Output
  104. * others = Special functions (dependent on bank)
  105. *
  106. * Note, since the code to deal with the case where there are two control
  107. * registers instead of one, we do not have a separate set of functions for
  108. * each case.
  109. */
  110. static int samsung_gpio_setcfg_4bit(struct samsung_gpio_chip *chip,
  111. unsigned int off, unsigned int cfg)
  112. {
  113. void __iomem *reg = chip->base;
  114. unsigned int shift = (off & 7) * 4;
  115. u32 con;
  116. if (off < 8 && chip->chip.ngpio > 8)
  117. reg -= 4;
  118. if (samsung_gpio_is_cfg_special(cfg)) {
  119. cfg &= 0xf;
  120. cfg <<= shift;
  121. }
  122. con = __raw_readl(reg);
  123. con &= ~(0xf << shift);
  124. con |= cfg;
  125. __raw_writel(con, reg);
  126. return 0;
  127. }
  128. /*
  129. * samsung_gpio_getcfg_4bit - Samsung 4bit single register GPIO config read.
  130. * @chip: The gpio chip that is being configured.
  131. * @off: The offset for the GPIO being configured.
  132. *
  133. * The reverse of samsung_gpio_setcfg_4bit(), turning a gpio configuration
  134. * register setting into a value the software can use, such as could be passed
  135. * to samsung_gpio_setcfg_4bit().
  136. *
  137. * @sa samsung_gpio_getcfg_2bit
  138. */
  139. static unsigned samsung_gpio_getcfg_4bit(struct samsung_gpio_chip *chip,
  140. unsigned int off)
  141. {
  142. void __iomem *reg = chip->base;
  143. unsigned int shift = (off & 7) * 4;
  144. u32 con;
  145. if (off < 8 && chip->chip.ngpio > 8)
  146. reg -= 4;
  147. con = __raw_readl(reg);
  148. con >>= shift;
  149. con &= 0xf;
  150. /* this conversion works for IN and OUT as well as special mode */
  151. return S3C_GPIO_SPECIAL(con);
  152. }
  153. static void __init samsung_gpiolib_set_cfg(struct samsung_gpio_cfg *chipcfg,
  154. int nr_chips)
  155. {
  156. for (; nr_chips > 0; nr_chips--, chipcfg++) {
  157. if (!chipcfg->set_config)
  158. chipcfg->set_config = samsung_gpio_setcfg_4bit;
  159. if (!chipcfg->get_config)
  160. chipcfg->get_config = samsung_gpio_getcfg_4bit;
  161. if (!chipcfg->set_pull)
  162. chipcfg->set_pull = samsung_gpio_setpull_updown;
  163. if (!chipcfg->get_pull)
  164. chipcfg->get_pull = samsung_gpio_getpull_updown;
  165. }
  166. }
  167. static struct samsung_gpio_cfg samsung_gpio_cfgs[] = {
  168. [0] = {
  169. .cfg_eint = 0x0,
  170. },
  171. [1] = {
  172. .cfg_eint = 0x3,
  173. },
  174. [2] = {
  175. .cfg_eint = 0x7,
  176. },
  177. [3] = {
  178. .cfg_eint = 0xF,
  179. },
  180. [4] = {
  181. .cfg_eint = 0x0,
  182. .set_config = samsung_gpio_setcfg_2bit,
  183. .get_config = samsung_gpio_getcfg_2bit,
  184. },
  185. [5] = {
  186. .cfg_eint = 0x2,
  187. .set_config = samsung_gpio_setcfg_2bit,
  188. .get_config = samsung_gpio_getcfg_2bit,
  189. },
  190. [6] = {
  191. .cfg_eint = 0x3,
  192. .set_config = samsung_gpio_setcfg_2bit,
  193. .get_config = samsung_gpio_getcfg_2bit,
  194. },
  195. [7] = {
  196. .set_config = samsung_gpio_setcfg_2bit,
  197. .get_config = samsung_gpio_getcfg_2bit,
  198. },
  199. };
  200. /*
  201. * Default routines for controlling GPIO, based on the original S3C24XX
  202. * GPIO functions which deal with the case where each gpio bank of the
  203. * chip is as following:
  204. *
  205. * base + 0x00: Control register, 2 bits per gpio
  206. * gpio n: 2 bits starting at (2*n)
  207. * 00 = input, 01 = output, others mean special-function
  208. * base + 0x04: Data register, 1 bit per gpio
  209. * bit n: data bit n
  210. */
  211. static int samsung_gpiolib_2bit_input(struct gpio_chip *chip, unsigned offset)
  212. {
  213. struct samsung_gpio_chip *ourchip = to_samsung_gpio(chip);
  214. void __iomem *base = ourchip->base;
  215. unsigned long flags;
  216. unsigned long con;
  217. samsung_gpio_lock(ourchip, flags);
  218. con = __raw_readl(base + 0x00);
  219. con &= ~(3 << (offset * 2));
  220. __raw_writel(con, base + 0x00);
  221. samsung_gpio_unlock(ourchip, flags);
  222. return 0;
  223. }
  224. static int samsung_gpiolib_2bit_output(struct gpio_chip *chip,
  225. unsigned offset, int value)
  226. {
  227. struct samsung_gpio_chip *ourchip = to_samsung_gpio(chip);
  228. void __iomem *base = ourchip->base;
  229. unsigned long flags;
  230. unsigned long dat;
  231. unsigned long con;
  232. samsung_gpio_lock(ourchip, flags);
  233. dat = __raw_readl(base + 0x04);
  234. dat &= ~(1 << offset);
  235. if (value)
  236. dat |= 1 << offset;
  237. __raw_writel(dat, base + 0x04);
  238. con = __raw_readl(base + 0x00);
  239. con &= ~(3 << (offset * 2));
  240. con |= 1 << (offset * 2);
  241. __raw_writel(con, base + 0x00);
  242. __raw_writel(dat, base + 0x04);
  243. samsung_gpio_unlock(ourchip, flags);
  244. return 0;
  245. }
  246. /*
  247. * The samsung_gpiolib_4bit routines are to control the gpio banks where
  248. * the gpio configuration register (GPxCON) has 4 bits per GPIO, as the
  249. * following example:
  250. *
  251. * base + 0x00: Control register, 4 bits per gpio
  252. * gpio n: 4 bits starting at (4*n)
  253. * 0000 = input, 0001 = output, others mean special-function
  254. * base + 0x04: Data register, 1 bit per gpio
  255. * bit n: data bit n
  256. *
  257. * Note, since the data register is one bit per gpio and is at base + 0x4
  258. * we can use samsung_gpiolib_get and samsung_gpiolib_set to change the
  259. * state of the output.
  260. */
  261. static int samsung_gpiolib_4bit_input(struct gpio_chip *chip,
  262. unsigned int offset)
  263. {
  264. struct samsung_gpio_chip *ourchip = to_samsung_gpio(chip);
  265. void __iomem *base = ourchip->base;
  266. unsigned long con;
  267. con = __raw_readl(base + GPIOCON_OFF);
  268. if (ourchip->bitmap_gpio_int & BIT(offset))
  269. con |= 0xf << con_4bit_shift(offset);
  270. else
  271. con &= ~(0xf << con_4bit_shift(offset));
  272. __raw_writel(con, base + GPIOCON_OFF);
  273. pr_debug("%s: %p: CON now %08lx\n", __func__, base, con);
  274. return 0;
  275. }
  276. static int samsung_gpiolib_4bit_output(struct gpio_chip *chip,
  277. unsigned int offset, int value)
  278. {
  279. struct samsung_gpio_chip *ourchip = to_samsung_gpio(chip);
  280. void __iomem *base = ourchip->base;
  281. unsigned long con;
  282. unsigned long dat;
  283. con = __raw_readl(base + GPIOCON_OFF);
  284. con &= ~(0xf << con_4bit_shift(offset));
  285. con |= 0x1 << con_4bit_shift(offset);
  286. dat = __raw_readl(base + GPIODAT_OFF);
  287. if (value)
  288. dat |= 1 << offset;
  289. else
  290. dat &= ~(1 << offset);
  291. __raw_writel(dat, base + GPIODAT_OFF);
  292. __raw_writel(con, base + GPIOCON_OFF);
  293. __raw_writel(dat, base + GPIODAT_OFF);
  294. pr_debug("%s: %p: CON %08lx, DAT %08lx\n", __func__, base, con, dat);
  295. return 0;
  296. }
  297. /*
  298. * The next set of routines are for the case where the GPIO configuration
  299. * registers are 4 bits per GPIO but there is more than one register (the
  300. * bank has more than 8 GPIOs.
  301. *
  302. * This case is the similar to the 4 bit case, but the registers are as
  303. * follows:
  304. *
  305. * base + 0x00: Control register, 4 bits per gpio (lower 8 GPIOs)
  306. * gpio n: 4 bits starting at (4*n)
  307. * 0000 = input, 0001 = output, others mean special-function
  308. * base + 0x04: Control register, 4 bits per gpio (up to 8 additions GPIOs)
  309. * gpio n: 4 bits starting at (4*n)
  310. * 0000 = input, 0001 = output, others mean special-function
  311. * base + 0x08: Data register, 1 bit per gpio
  312. * bit n: data bit n
  313. *
  314. * To allow us to use the samsung_gpiolib_get and samsung_gpiolib_set
  315. * routines we store the 'base + 0x4' address so that these routines see
  316. * the data register at ourchip->base + 0x04.
  317. */
  318. static int samsung_gpiolib_4bit2_input(struct gpio_chip *chip,
  319. unsigned int offset)
  320. {
  321. struct samsung_gpio_chip *ourchip = to_samsung_gpio(chip);
  322. void __iomem *base = ourchip->base;
  323. void __iomem *regcon = base;
  324. unsigned long con;
  325. if (offset > 7)
  326. offset -= 8;
  327. else
  328. regcon -= 4;
  329. con = __raw_readl(regcon);
  330. con &= ~(0xf << con_4bit_shift(offset));
  331. __raw_writel(con, regcon);
  332. pr_debug("%s: %p: CON %08lx\n", __func__, base, con);
  333. return 0;
  334. }
  335. static int samsung_gpiolib_4bit2_output(struct gpio_chip *chip,
  336. unsigned int offset, int value)
  337. {
  338. struct samsung_gpio_chip *ourchip = to_samsung_gpio(chip);
  339. void __iomem *base = ourchip->base;
  340. void __iomem *regcon = base;
  341. unsigned long con;
  342. unsigned long dat;
  343. unsigned con_offset = offset;
  344. if (con_offset > 7)
  345. con_offset -= 8;
  346. else
  347. regcon -= 4;
  348. con = __raw_readl(regcon);
  349. con &= ~(0xf << con_4bit_shift(con_offset));
  350. con |= 0x1 << con_4bit_shift(con_offset);
  351. dat = __raw_readl(base + GPIODAT_OFF);
  352. if (value)
  353. dat |= 1 << offset;
  354. else
  355. dat &= ~(1 << offset);
  356. __raw_writel(dat, base + GPIODAT_OFF);
  357. __raw_writel(con, regcon);
  358. __raw_writel(dat, base + GPIODAT_OFF);
  359. pr_debug("%s: %p: CON %08lx, DAT %08lx\n", __func__, base, con, dat);
  360. return 0;
  361. }
  362. static void samsung_gpiolib_set(struct gpio_chip *chip,
  363. unsigned offset, int value)
  364. {
  365. struct samsung_gpio_chip *ourchip = to_samsung_gpio(chip);
  366. void __iomem *base = ourchip->base;
  367. unsigned long flags;
  368. unsigned long dat;
  369. samsung_gpio_lock(ourchip, flags);
  370. dat = __raw_readl(base + 0x04);
  371. dat &= ~(1 << offset);
  372. if (value)
  373. dat |= 1 << offset;
  374. __raw_writel(dat, base + 0x04);
  375. samsung_gpio_unlock(ourchip, flags);
  376. }
  377. static int samsung_gpiolib_get(struct gpio_chip *chip, unsigned offset)
  378. {
  379. struct samsung_gpio_chip *ourchip = to_samsung_gpio(chip);
  380. unsigned long val;
  381. val = __raw_readl(ourchip->base + 0x04);
  382. val >>= offset;
  383. val &= 1;
  384. return val;
  385. }
  386. /*
  387. * CONFIG_S3C_GPIO_TRACK enables the tracking of the s3c specific gpios
  388. * for use with the configuration calls, and other parts of the s3c gpiolib
  389. * support code.
  390. *
  391. * Not all s3c support code will need this, as some configurations of cpu
  392. * may only support one or two different configuration options and have an
  393. * easy gpio to samsung_gpio_chip mapping function. If this is the case, then
  394. * the machine support file should provide its own samsung_gpiolib_getchip()
  395. * and any other necessary functions.
  396. */
  397. #ifdef CONFIG_S3C_GPIO_TRACK
  398. struct samsung_gpio_chip *s3c_gpios[S3C_GPIO_END];
  399. static __init void s3c_gpiolib_track(struct samsung_gpio_chip *chip)
  400. {
  401. unsigned int gpn;
  402. int i;
  403. gpn = chip->chip.base;
  404. for (i = 0; i < chip->chip.ngpio; i++, gpn++) {
  405. BUG_ON(gpn >= ARRAY_SIZE(s3c_gpios));
  406. s3c_gpios[gpn] = chip;
  407. }
  408. }
  409. #endif /* CONFIG_S3C_GPIO_TRACK */
  410. /*
  411. * samsung_gpiolib_add() - add the Samsung gpio_chip.
  412. * @chip: The chip to register
  413. *
  414. * This is a wrapper to gpiochip_add() that takes our specific gpio chip
  415. * information and makes the necessary alterations for the platform and
  416. * notes the information for use with the configuration systems and any
  417. * other parts of the system.
  418. */
  419. static void __init samsung_gpiolib_add(struct samsung_gpio_chip *chip)
  420. {
  421. struct gpio_chip *gc = &chip->chip;
  422. int ret;
  423. BUG_ON(!chip->base);
  424. BUG_ON(!gc->label);
  425. BUG_ON(!gc->ngpio);
  426. spin_lock_init(&chip->lock);
  427. if (!gc->direction_input)
  428. gc->direction_input = samsung_gpiolib_2bit_input;
  429. if (!gc->direction_output)
  430. gc->direction_output = samsung_gpiolib_2bit_output;
  431. if (!gc->set)
  432. gc->set = samsung_gpiolib_set;
  433. if (!gc->get)
  434. gc->get = samsung_gpiolib_get;
  435. #ifdef CONFIG_PM
  436. if (chip->pm != NULL) {
  437. if (!chip->pm->save || !chip->pm->resume)
  438. pr_err("gpio: %s has missing PM functions\n",
  439. gc->label);
  440. } else
  441. pr_err("gpio: %s has no PM function\n", gc->label);
  442. #endif
  443. /* gpiochip_add() prints own failure message on error. */
  444. ret = gpiochip_add_data(gc, chip);
  445. if (ret >= 0)
  446. s3c_gpiolib_track(chip);
  447. }
  448. static void __init samsung_gpiolib_add_2bit_chips(struct samsung_gpio_chip *chip,
  449. int nr_chips, void __iomem *base,
  450. unsigned int offset)
  451. {
  452. int i;
  453. for (i = 0 ; i < nr_chips; i++, chip++) {
  454. chip->chip.direction_input = samsung_gpiolib_2bit_input;
  455. chip->chip.direction_output = samsung_gpiolib_2bit_output;
  456. if (!chip->config)
  457. chip->config = &samsung_gpio_cfgs[7];
  458. if (!chip->pm)
  459. chip->pm = __gpio_pm(&samsung_gpio_pm_2bit);
  460. if ((base != NULL) && (chip->base == NULL))
  461. chip->base = base + ((i) * offset);
  462. samsung_gpiolib_add(chip);
  463. }
  464. }
  465. /*
  466. * samsung_gpiolib_add_4bit_chips - 4bit single register GPIO config.
  467. * @chip: The gpio chip that is being configured.
  468. * @nr_chips: The no of chips (gpio ports) for the GPIO being configured.
  469. *
  470. * This helper deal with the GPIO cases where the control register has 4 bits
  471. * of control per GPIO, generally in the form of:
  472. * 0000 = Input
  473. * 0001 = Output
  474. * others = Special functions (dependent on bank)
  475. *
  476. * Note, since the code to deal with the case where there are two control
  477. * registers instead of one, we do not have a separate set of function
  478. * (samsung_gpiolib_add_4bit2_chips)for each case.
  479. */
  480. static void __init samsung_gpiolib_add_4bit_chips(struct samsung_gpio_chip *chip,
  481. int nr_chips, void __iomem *base)
  482. {
  483. int i;
  484. for (i = 0 ; i < nr_chips; i++, chip++) {
  485. chip->chip.direction_input = samsung_gpiolib_4bit_input;
  486. chip->chip.direction_output = samsung_gpiolib_4bit_output;
  487. if (!chip->config)
  488. chip->config = &samsung_gpio_cfgs[2];
  489. if (!chip->pm)
  490. chip->pm = __gpio_pm(&samsung_gpio_pm_4bit);
  491. if ((base != NULL) && (chip->base == NULL))
  492. chip->base = base + ((i) * 0x20);
  493. chip->bitmap_gpio_int = 0;
  494. samsung_gpiolib_add(chip);
  495. }
  496. }
  497. static void __init samsung_gpiolib_add_4bit2_chips(struct samsung_gpio_chip *chip,
  498. int nr_chips)
  499. {
  500. for (; nr_chips > 0; nr_chips--, chip++) {
  501. chip->chip.direction_input = samsung_gpiolib_4bit2_input;
  502. chip->chip.direction_output = samsung_gpiolib_4bit2_output;
  503. if (!chip->config)
  504. chip->config = &samsung_gpio_cfgs[2];
  505. if (!chip->pm)
  506. chip->pm = __gpio_pm(&samsung_gpio_pm_4bit);
  507. samsung_gpiolib_add(chip);
  508. }
  509. }
  510. int samsung_gpiolib_to_irq(struct gpio_chip *chip, unsigned int offset)
  511. {
  512. struct samsung_gpio_chip *samsung_chip = gpiochip_get_data(chip);
  513. return samsung_chip->irq_base + offset;
  514. }
  515. static int s3c64xx_gpiolib_mbank_to_irq(struct gpio_chip *chip, unsigned pin)
  516. {
  517. return pin < 5 ? IRQ_EINT(23) + pin : -ENXIO;
  518. }
  519. static int s3c64xx_gpiolib_lbank_to_irq(struct gpio_chip *chip, unsigned pin)
  520. {
  521. return pin >= 8 ? IRQ_EINT(16) + pin - 8 : -ENXIO;
  522. }
  523. /*
  524. * GPIO bank summary:
  525. *
  526. * Bank GPIOs Style SlpCon ExtInt Group
  527. * A 8 4Bit Yes 1
  528. * B 7 4Bit Yes 1
  529. * C 8 4Bit Yes 2
  530. * D 5 4Bit Yes 3
  531. * E 5 4Bit Yes None
  532. * F 16 2Bit Yes 4 [1]
  533. * G 7 4Bit Yes 5
  534. * H 10 4Bit[2] Yes 6
  535. * I 16 2Bit Yes None
  536. * J 12 2Bit Yes None
  537. * K 16 4Bit[2] No None
  538. * L 15 4Bit[2] No None
  539. * M 6 4Bit No IRQ_EINT
  540. * N 16 2Bit No IRQ_EINT
  541. * O 16 2Bit Yes 7
  542. * P 15 2Bit Yes 8
  543. * Q 9 2Bit Yes 9
  544. *
  545. * [1] BANKF pins 14,15 do not form part of the external interrupt sources
  546. * [2] BANK has two control registers, GPxCON0 and GPxCON1
  547. */
  548. static struct samsung_gpio_chip s3c64xx_gpios_4bit[] = {
  549. {
  550. .chip = {
  551. .base = S3C64XX_GPA(0),
  552. .ngpio = S3C64XX_GPIO_A_NR,
  553. .label = "GPA",
  554. },
  555. }, {
  556. .chip = {
  557. .base = S3C64XX_GPB(0),
  558. .ngpio = S3C64XX_GPIO_B_NR,
  559. .label = "GPB",
  560. },
  561. }, {
  562. .chip = {
  563. .base = S3C64XX_GPC(0),
  564. .ngpio = S3C64XX_GPIO_C_NR,
  565. .label = "GPC",
  566. },
  567. }, {
  568. .chip = {
  569. .base = S3C64XX_GPD(0),
  570. .ngpio = S3C64XX_GPIO_D_NR,
  571. .label = "GPD",
  572. },
  573. }, {
  574. .config = &samsung_gpio_cfgs[0],
  575. .chip = {
  576. .base = S3C64XX_GPE(0),
  577. .ngpio = S3C64XX_GPIO_E_NR,
  578. .label = "GPE",
  579. },
  580. }, {
  581. .base = S3C64XX_GPG_BASE,
  582. .chip = {
  583. .base = S3C64XX_GPG(0),
  584. .ngpio = S3C64XX_GPIO_G_NR,
  585. .label = "GPG",
  586. },
  587. }, {
  588. .base = S3C64XX_GPM_BASE,
  589. .config = &samsung_gpio_cfgs[1],
  590. .chip = {
  591. .base = S3C64XX_GPM(0),
  592. .ngpio = S3C64XX_GPIO_M_NR,
  593. .label = "GPM",
  594. .to_irq = s3c64xx_gpiolib_mbank_to_irq,
  595. },
  596. },
  597. };
  598. static struct samsung_gpio_chip s3c64xx_gpios_4bit2[] = {
  599. {
  600. .base = S3C64XX_GPH_BASE + 0x4,
  601. .chip = {
  602. .base = S3C64XX_GPH(0),
  603. .ngpio = S3C64XX_GPIO_H_NR,
  604. .label = "GPH",
  605. },
  606. }, {
  607. .base = S3C64XX_GPK_BASE + 0x4,
  608. .config = &samsung_gpio_cfgs[0],
  609. .chip = {
  610. .base = S3C64XX_GPK(0),
  611. .ngpio = S3C64XX_GPIO_K_NR,
  612. .label = "GPK",
  613. },
  614. }, {
  615. .base = S3C64XX_GPL_BASE + 0x4,
  616. .config = &samsung_gpio_cfgs[1],
  617. .chip = {
  618. .base = S3C64XX_GPL(0),
  619. .ngpio = S3C64XX_GPIO_L_NR,
  620. .label = "GPL",
  621. .to_irq = s3c64xx_gpiolib_lbank_to_irq,
  622. },
  623. },
  624. };
  625. static struct samsung_gpio_chip s3c64xx_gpios_2bit[] = {
  626. {
  627. .base = S3C64XX_GPF_BASE,
  628. .config = &samsung_gpio_cfgs[6],
  629. .chip = {
  630. .base = S3C64XX_GPF(0),
  631. .ngpio = S3C64XX_GPIO_F_NR,
  632. .label = "GPF",
  633. },
  634. }, {
  635. .config = &samsung_gpio_cfgs[7],
  636. .chip = {
  637. .base = S3C64XX_GPI(0),
  638. .ngpio = S3C64XX_GPIO_I_NR,
  639. .label = "GPI",
  640. },
  641. }, {
  642. .config = &samsung_gpio_cfgs[7],
  643. .chip = {
  644. .base = S3C64XX_GPJ(0),
  645. .ngpio = S3C64XX_GPIO_J_NR,
  646. .label = "GPJ",
  647. },
  648. }, {
  649. .config = &samsung_gpio_cfgs[6],
  650. .chip = {
  651. .base = S3C64XX_GPO(0),
  652. .ngpio = S3C64XX_GPIO_O_NR,
  653. .label = "GPO",
  654. },
  655. }, {
  656. .config = &samsung_gpio_cfgs[6],
  657. .chip = {
  658. .base = S3C64XX_GPP(0),
  659. .ngpio = S3C64XX_GPIO_P_NR,
  660. .label = "GPP",
  661. },
  662. }, {
  663. .config = &samsung_gpio_cfgs[6],
  664. .chip = {
  665. .base = S3C64XX_GPQ(0),
  666. .ngpio = S3C64XX_GPIO_Q_NR,
  667. .label = "GPQ",
  668. },
  669. }, {
  670. .base = S3C64XX_GPN_BASE,
  671. .irq_base = IRQ_EINT(0),
  672. .config = &samsung_gpio_cfgs[5],
  673. .chip = {
  674. .base = S3C64XX_GPN(0),
  675. .ngpio = S3C64XX_GPIO_N_NR,
  676. .label = "GPN",
  677. .to_irq = samsung_gpiolib_to_irq,
  678. },
  679. },
  680. };
  681. /* TODO: cleanup soc_is_* */
  682. static __init int samsung_gpiolib_init(void)
  683. {
  684. /*
  685. * Currently there are two drivers that can provide GPIO support for
  686. * Samsung SoCs. For device tree enabled platforms, the new
  687. * pinctrl-samsung driver is used, providing both GPIO and pin control
  688. * interfaces. For legacy (non-DT) platforms this driver is used.
  689. */
  690. if (of_have_populated_dt())
  691. return 0;
  692. if (soc_is_s3c64xx()) {
  693. samsung_gpiolib_set_cfg(samsung_gpio_cfgs,
  694. ARRAY_SIZE(samsung_gpio_cfgs));
  695. samsung_gpiolib_add_2bit_chips(s3c64xx_gpios_2bit,
  696. ARRAY_SIZE(s3c64xx_gpios_2bit),
  697. S3C64XX_VA_GPIO + 0xE0, 0x20);
  698. samsung_gpiolib_add_4bit_chips(s3c64xx_gpios_4bit,
  699. ARRAY_SIZE(s3c64xx_gpios_4bit),
  700. S3C64XX_VA_GPIO);
  701. samsung_gpiolib_add_4bit2_chips(s3c64xx_gpios_4bit2,
  702. ARRAY_SIZE(s3c64xx_gpios_4bit2));
  703. }
  704. return 0;
  705. }
  706. core_initcall(samsung_gpiolib_init);
  707. int s3c_gpio_cfgpin(unsigned int pin, unsigned int config)
  708. {
  709. struct samsung_gpio_chip *chip = samsung_gpiolib_getchip(pin);
  710. unsigned long flags;
  711. int offset;
  712. int ret;
  713. if (!chip)
  714. return -EINVAL;
  715. offset = pin - chip->chip.base;
  716. samsung_gpio_lock(chip, flags);
  717. ret = samsung_gpio_do_setcfg(chip, offset, config);
  718. samsung_gpio_unlock(chip, flags);
  719. return ret;
  720. }
  721. EXPORT_SYMBOL(s3c_gpio_cfgpin);
  722. int s3c_gpio_cfgpin_range(unsigned int start, unsigned int nr,
  723. unsigned int cfg)
  724. {
  725. int ret;
  726. for (; nr > 0; nr--, start++) {
  727. ret = s3c_gpio_cfgpin(start, cfg);
  728. if (ret != 0)
  729. return ret;
  730. }
  731. return 0;
  732. }
  733. EXPORT_SYMBOL_GPL(s3c_gpio_cfgpin_range);
  734. int s3c_gpio_cfgall_range(unsigned int start, unsigned int nr,
  735. unsigned int cfg, samsung_gpio_pull_t pull)
  736. {
  737. int ret;
  738. for (; nr > 0; nr--, start++) {
  739. s3c_gpio_setpull(start, pull);
  740. ret = s3c_gpio_cfgpin(start, cfg);
  741. if (ret != 0)
  742. return ret;
  743. }
  744. return 0;
  745. }
  746. EXPORT_SYMBOL_GPL(s3c_gpio_cfgall_range);
  747. int s3c_gpio_setpull(unsigned int pin, samsung_gpio_pull_t pull)
  748. {
  749. struct samsung_gpio_chip *chip = samsung_gpiolib_getchip(pin);
  750. unsigned long flags;
  751. int offset, ret;
  752. if (!chip)
  753. return -EINVAL;
  754. offset = pin - chip->chip.base;
  755. samsung_gpio_lock(chip, flags);
  756. ret = samsung_gpio_do_setpull(chip, offset, pull);
  757. samsung_gpio_unlock(chip, flags);
  758. return ret;
  759. }
  760. EXPORT_SYMBOL(s3c_gpio_setpull);