pinctrl-bcm2835.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*
  2. * Driver for Broadcom BCM2835 GPIO unit (pinctrl + GPIO)
  3. *
  4. * Copyright (C) 2012 Chris Boot, Simon Arlott, Stephen Warren
  5. *
  6. * This driver is inspired by:
  7. * pinctrl-nomadik.c, please see original file for copyright information
  8. * pinctrl-tegra.c, please see original file for copyright information
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/bitmap.h>
  21. #include <linux/bug.h>
  22. #include <linux/delay.h>
  23. #include <linux/device.h>
  24. #include <linux/err.h>
  25. #include <linux/gpio/driver.h>
  26. #include <linux/io.h>
  27. #include <linux/irq.h>
  28. #include <linux/irqdesc.h>
  29. #include <linux/init.h>
  30. #include <linux/of_address.h>
  31. #include <linux/of.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/pinctrl/consumer.h>
  34. #include <linux/pinctrl/machine.h>
  35. #include <linux/pinctrl/pinconf.h>
  36. #include <linux/pinctrl/pinctrl.h>
  37. #include <linux/pinctrl/pinmux.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/types.h>
  43. #define MODULE_NAME "pinctrl-bcm2835"
  44. #define BCM2835_NUM_GPIOS 54
  45. #define BCM2835_NUM_BANKS 2
  46. #define BCM2835_NUM_IRQS 3
  47. #define BCM2835_PIN_BITMAP_SZ \
  48. DIV_ROUND_UP(BCM2835_NUM_GPIOS, sizeof(unsigned long) * 8)
  49. /* GPIO register offsets */
  50. #define GPFSEL0 0x0 /* Function Select */
  51. #define GPSET0 0x1c /* Pin Output Set */
  52. #define GPCLR0 0x28 /* Pin Output Clear */
  53. #define GPLEV0 0x34 /* Pin Level */
  54. #define GPEDS0 0x40 /* Pin Event Detect Status */
  55. #define GPREN0 0x4c /* Pin Rising Edge Detect Enable */
  56. #define GPFEN0 0x58 /* Pin Falling Edge Detect Enable */
  57. #define GPHEN0 0x64 /* Pin High Detect Enable */
  58. #define GPLEN0 0x70 /* Pin Low Detect Enable */
  59. #define GPAREN0 0x7c /* Pin Async Rising Edge Detect */
  60. #define GPAFEN0 0x88 /* Pin Async Falling Edge Detect */
  61. #define GPPUD 0x94 /* Pin Pull-up/down Enable */
  62. #define GPPUDCLK0 0x98 /* Pin Pull-up/down Enable Clock */
  63. #define FSEL_REG(p) (GPFSEL0 + (((p) / 10) * 4))
  64. #define FSEL_SHIFT(p) (((p) % 10) * 3)
  65. #define GPIO_REG_OFFSET(p) ((p) / 32)
  66. #define GPIO_REG_SHIFT(p) ((p) % 32)
  67. enum bcm2835_pinconf_param {
  68. /* argument: bcm2835_pinconf_pull */
  69. BCM2835_PINCONF_PARAM_PULL,
  70. };
  71. #define BCM2835_PINCONF_PACK(_param_, _arg_) ((_param_) << 16 | (_arg_))
  72. #define BCM2835_PINCONF_UNPACK_PARAM(_conf_) ((_conf_) >> 16)
  73. #define BCM2835_PINCONF_UNPACK_ARG(_conf_) ((_conf_) & 0xffff)
  74. struct bcm2835_pinctrl {
  75. struct device *dev;
  76. void __iomem *base;
  77. int irq[BCM2835_NUM_IRQS];
  78. /* note: locking assumes each bank will have its own unsigned long */
  79. unsigned long enabled_irq_map[BCM2835_NUM_BANKS];
  80. unsigned int irq_type[BCM2835_NUM_GPIOS];
  81. struct pinctrl_dev *pctl_dev;
  82. struct gpio_chip gpio_chip;
  83. struct pinctrl_gpio_range gpio_range;
  84. spinlock_t irq_lock[BCM2835_NUM_BANKS];
  85. };
  86. /* pins are just named GPIO0..GPIO53 */
  87. #define BCM2835_GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a)
  88. static struct pinctrl_pin_desc bcm2835_gpio_pins[] = {
  89. BCM2835_GPIO_PIN(0),
  90. BCM2835_GPIO_PIN(1),
  91. BCM2835_GPIO_PIN(2),
  92. BCM2835_GPIO_PIN(3),
  93. BCM2835_GPIO_PIN(4),
  94. BCM2835_GPIO_PIN(5),
  95. BCM2835_GPIO_PIN(6),
  96. BCM2835_GPIO_PIN(7),
  97. BCM2835_GPIO_PIN(8),
  98. BCM2835_GPIO_PIN(9),
  99. BCM2835_GPIO_PIN(10),
  100. BCM2835_GPIO_PIN(11),
  101. BCM2835_GPIO_PIN(12),
  102. BCM2835_GPIO_PIN(13),
  103. BCM2835_GPIO_PIN(14),
  104. BCM2835_GPIO_PIN(15),
  105. BCM2835_GPIO_PIN(16),
  106. BCM2835_GPIO_PIN(17),
  107. BCM2835_GPIO_PIN(18),
  108. BCM2835_GPIO_PIN(19),
  109. BCM2835_GPIO_PIN(20),
  110. BCM2835_GPIO_PIN(21),
  111. BCM2835_GPIO_PIN(22),
  112. BCM2835_GPIO_PIN(23),
  113. BCM2835_GPIO_PIN(24),
  114. BCM2835_GPIO_PIN(25),
  115. BCM2835_GPIO_PIN(26),
  116. BCM2835_GPIO_PIN(27),
  117. BCM2835_GPIO_PIN(28),
  118. BCM2835_GPIO_PIN(29),
  119. BCM2835_GPIO_PIN(30),
  120. BCM2835_GPIO_PIN(31),
  121. BCM2835_GPIO_PIN(32),
  122. BCM2835_GPIO_PIN(33),
  123. BCM2835_GPIO_PIN(34),
  124. BCM2835_GPIO_PIN(35),
  125. BCM2835_GPIO_PIN(36),
  126. BCM2835_GPIO_PIN(37),
  127. BCM2835_GPIO_PIN(38),
  128. BCM2835_GPIO_PIN(39),
  129. BCM2835_GPIO_PIN(40),
  130. BCM2835_GPIO_PIN(41),
  131. BCM2835_GPIO_PIN(42),
  132. BCM2835_GPIO_PIN(43),
  133. BCM2835_GPIO_PIN(44),
  134. BCM2835_GPIO_PIN(45),
  135. BCM2835_GPIO_PIN(46),
  136. BCM2835_GPIO_PIN(47),
  137. BCM2835_GPIO_PIN(48),
  138. BCM2835_GPIO_PIN(49),
  139. BCM2835_GPIO_PIN(50),
  140. BCM2835_GPIO_PIN(51),
  141. BCM2835_GPIO_PIN(52),
  142. BCM2835_GPIO_PIN(53),
  143. };
  144. /* one pin per group */
  145. static const char * const bcm2835_gpio_groups[] = {
  146. "gpio0",
  147. "gpio1",
  148. "gpio2",
  149. "gpio3",
  150. "gpio4",
  151. "gpio5",
  152. "gpio6",
  153. "gpio7",
  154. "gpio8",
  155. "gpio9",
  156. "gpio10",
  157. "gpio11",
  158. "gpio12",
  159. "gpio13",
  160. "gpio14",
  161. "gpio15",
  162. "gpio16",
  163. "gpio17",
  164. "gpio18",
  165. "gpio19",
  166. "gpio20",
  167. "gpio21",
  168. "gpio22",
  169. "gpio23",
  170. "gpio24",
  171. "gpio25",
  172. "gpio26",
  173. "gpio27",
  174. "gpio28",
  175. "gpio29",
  176. "gpio30",
  177. "gpio31",
  178. "gpio32",
  179. "gpio33",
  180. "gpio34",
  181. "gpio35",
  182. "gpio36",
  183. "gpio37",
  184. "gpio38",
  185. "gpio39",
  186. "gpio40",
  187. "gpio41",
  188. "gpio42",
  189. "gpio43",
  190. "gpio44",
  191. "gpio45",
  192. "gpio46",
  193. "gpio47",
  194. "gpio48",
  195. "gpio49",
  196. "gpio50",
  197. "gpio51",
  198. "gpio52",
  199. "gpio53",
  200. };
  201. enum bcm2835_fsel {
  202. BCM2835_FSEL_GPIO_IN = 0,
  203. BCM2835_FSEL_GPIO_OUT = 1,
  204. BCM2835_FSEL_ALT0 = 4,
  205. BCM2835_FSEL_ALT1 = 5,
  206. BCM2835_FSEL_ALT2 = 6,
  207. BCM2835_FSEL_ALT3 = 7,
  208. BCM2835_FSEL_ALT4 = 3,
  209. BCM2835_FSEL_ALT5 = 2,
  210. BCM2835_FSEL_COUNT = 8,
  211. BCM2835_FSEL_MASK = 0x7,
  212. };
  213. static const char * const bcm2835_functions[BCM2835_FSEL_COUNT] = {
  214. [BCM2835_FSEL_GPIO_IN] = "gpio_in",
  215. [BCM2835_FSEL_GPIO_OUT] = "gpio_out",
  216. [BCM2835_FSEL_ALT0] = "alt0",
  217. [BCM2835_FSEL_ALT1] = "alt1",
  218. [BCM2835_FSEL_ALT2] = "alt2",
  219. [BCM2835_FSEL_ALT3] = "alt3",
  220. [BCM2835_FSEL_ALT4] = "alt4",
  221. [BCM2835_FSEL_ALT5] = "alt5",
  222. };
  223. static const char * const irq_type_names[] = {
  224. [IRQ_TYPE_NONE] = "none",
  225. [IRQ_TYPE_EDGE_RISING] = "edge-rising",
  226. [IRQ_TYPE_EDGE_FALLING] = "edge-falling",
  227. [IRQ_TYPE_EDGE_BOTH] = "edge-both",
  228. [IRQ_TYPE_LEVEL_HIGH] = "level-high",
  229. [IRQ_TYPE_LEVEL_LOW] = "level-low",
  230. };
  231. static inline u32 bcm2835_gpio_rd(struct bcm2835_pinctrl *pc, unsigned reg)
  232. {
  233. return readl(pc->base + reg);
  234. }
  235. static inline void bcm2835_gpio_wr(struct bcm2835_pinctrl *pc, unsigned reg,
  236. u32 val)
  237. {
  238. writel(val, pc->base + reg);
  239. }
  240. static inline int bcm2835_gpio_get_bit(struct bcm2835_pinctrl *pc, unsigned reg,
  241. unsigned bit)
  242. {
  243. reg += GPIO_REG_OFFSET(bit) * 4;
  244. return (bcm2835_gpio_rd(pc, reg) >> GPIO_REG_SHIFT(bit)) & 1;
  245. }
  246. /* note NOT a read/modify/write cycle */
  247. static inline void bcm2835_gpio_set_bit(struct bcm2835_pinctrl *pc,
  248. unsigned reg, unsigned bit)
  249. {
  250. reg += GPIO_REG_OFFSET(bit) * 4;
  251. bcm2835_gpio_wr(pc, reg, BIT(GPIO_REG_SHIFT(bit)));
  252. }
  253. static inline enum bcm2835_fsel bcm2835_pinctrl_fsel_get(
  254. struct bcm2835_pinctrl *pc, unsigned pin)
  255. {
  256. u32 val = bcm2835_gpio_rd(pc, FSEL_REG(pin));
  257. enum bcm2835_fsel status = (val >> FSEL_SHIFT(pin)) & BCM2835_FSEL_MASK;
  258. dev_dbg(pc->dev, "get %08x (%u => %s)\n", val, pin,
  259. bcm2835_functions[status]);
  260. return status;
  261. }
  262. static inline void bcm2835_pinctrl_fsel_set(
  263. struct bcm2835_pinctrl *pc, unsigned pin,
  264. enum bcm2835_fsel fsel)
  265. {
  266. u32 val = bcm2835_gpio_rd(pc, FSEL_REG(pin));
  267. enum bcm2835_fsel cur = (val >> FSEL_SHIFT(pin)) & BCM2835_FSEL_MASK;
  268. dev_dbg(pc->dev, "read %08x (%u => %s)\n", val, pin,
  269. bcm2835_functions[cur]);
  270. if (cur == fsel)
  271. return;
  272. if (cur != BCM2835_FSEL_GPIO_IN && fsel != BCM2835_FSEL_GPIO_IN) {
  273. /* always transition through GPIO_IN */
  274. val &= ~(BCM2835_FSEL_MASK << FSEL_SHIFT(pin));
  275. val |= BCM2835_FSEL_GPIO_IN << FSEL_SHIFT(pin);
  276. dev_dbg(pc->dev, "trans %08x (%u <= %s)\n", val, pin,
  277. bcm2835_functions[BCM2835_FSEL_GPIO_IN]);
  278. bcm2835_gpio_wr(pc, FSEL_REG(pin), val);
  279. }
  280. val &= ~(BCM2835_FSEL_MASK << FSEL_SHIFT(pin));
  281. val |= fsel << FSEL_SHIFT(pin);
  282. dev_dbg(pc->dev, "write %08x (%u <= %s)\n", val, pin,
  283. bcm2835_functions[fsel]);
  284. bcm2835_gpio_wr(pc, FSEL_REG(pin), val);
  285. }
  286. static int bcm2835_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  287. {
  288. return pinctrl_gpio_direction_input(chip->base + offset);
  289. }
  290. static int bcm2835_gpio_get(struct gpio_chip *chip, unsigned offset)
  291. {
  292. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  293. return bcm2835_gpio_get_bit(pc, GPLEV0, offset);
  294. }
  295. static int bcm2835_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  296. {
  297. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  298. enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
  299. /* Alternative function doesn't clearly provide a direction */
  300. if (fsel > BCM2835_FSEL_GPIO_OUT)
  301. return -EINVAL;
  302. return (fsel == BCM2835_FSEL_GPIO_IN);
  303. }
  304. static void bcm2835_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  305. {
  306. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  307. bcm2835_gpio_set_bit(pc, value ? GPSET0 : GPCLR0, offset);
  308. }
  309. static int bcm2835_gpio_direction_output(struct gpio_chip *chip,
  310. unsigned offset, int value)
  311. {
  312. bcm2835_gpio_set(chip, offset, value);
  313. return pinctrl_gpio_direction_output(chip->base + offset);
  314. }
  315. static const struct gpio_chip bcm2835_gpio_chip = {
  316. .label = MODULE_NAME,
  317. .owner = THIS_MODULE,
  318. .request = gpiochip_generic_request,
  319. .free = gpiochip_generic_free,
  320. .direction_input = bcm2835_gpio_direction_input,
  321. .direction_output = bcm2835_gpio_direction_output,
  322. .get_direction = bcm2835_gpio_get_direction,
  323. .get = bcm2835_gpio_get,
  324. .set = bcm2835_gpio_set,
  325. .base = -1,
  326. .ngpio = BCM2835_NUM_GPIOS,
  327. .can_sleep = false,
  328. };
  329. static void bcm2835_gpio_irq_handle_bank(struct bcm2835_pinctrl *pc,
  330. unsigned int bank, u32 mask)
  331. {
  332. unsigned long events;
  333. unsigned offset;
  334. unsigned gpio;
  335. unsigned int type;
  336. events = bcm2835_gpio_rd(pc, GPEDS0 + bank * 4);
  337. events &= mask;
  338. events &= pc->enabled_irq_map[bank];
  339. for_each_set_bit(offset, &events, 32) {
  340. gpio = (32 * bank) + offset;
  341. /* FIXME: no clue why the code looks up the type here */
  342. type = pc->irq_type[gpio];
  343. generic_handle_irq(irq_linear_revmap(pc->gpio_chip.irqdomain,
  344. gpio));
  345. }
  346. }
  347. static void bcm2835_gpio_irq_handler(struct irq_desc *desc)
  348. {
  349. struct gpio_chip *chip = irq_desc_get_handler_data(desc);
  350. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  351. struct irq_chip *host_chip = irq_desc_get_chip(desc);
  352. int irq = irq_desc_get_irq(desc);
  353. int group;
  354. int i;
  355. for (i = 0; i < ARRAY_SIZE(pc->irq); i++) {
  356. if (pc->irq[i] == irq) {
  357. group = i;
  358. break;
  359. }
  360. }
  361. /* This should not happen, every IRQ has a bank */
  362. if (i == ARRAY_SIZE(pc->irq))
  363. BUG();
  364. chained_irq_enter(host_chip, desc);
  365. switch (group) {
  366. case 0: /* IRQ0 covers GPIOs 0-27 */
  367. bcm2835_gpio_irq_handle_bank(pc, 0, 0x0fffffff);
  368. break;
  369. case 1: /* IRQ1 covers GPIOs 28-45 */
  370. bcm2835_gpio_irq_handle_bank(pc, 0, 0xf0000000);
  371. bcm2835_gpio_irq_handle_bank(pc, 1, 0x00003fff);
  372. break;
  373. case 2: /* IRQ2 covers GPIOs 46-53 */
  374. bcm2835_gpio_irq_handle_bank(pc, 1, 0x003fc000);
  375. break;
  376. }
  377. chained_irq_exit(host_chip, desc);
  378. }
  379. static inline void __bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
  380. unsigned reg, unsigned offset, bool enable)
  381. {
  382. u32 value;
  383. reg += GPIO_REG_OFFSET(offset) * 4;
  384. value = bcm2835_gpio_rd(pc, reg);
  385. if (enable)
  386. value |= BIT(GPIO_REG_SHIFT(offset));
  387. else
  388. value &= ~(BIT(GPIO_REG_SHIFT(offset)));
  389. bcm2835_gpio_wr(pc, reg, value);
  390. }
  391. /* fast path for IRQ handler */
  392. static void bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
  393. unsigned offset, bool enable)
  394. {
  395. switch (pc->irq_type[offset]) {
  396. case IRQ_TYPE_EDGE_RISING:
  397. __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
  398. break;
  399. case IRQ_TYPE_EDGE_FALLING:
  400. __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
  401. break;
  402. case IRQ_TYPE_EDGE_BOTH:
  403. __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
  404. __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
  405. break;
  406. case IRQ_TYPE_LEVEL_HIGH:
  407. __bcm2835_gpio_irq_config(pc, GPHEN0, offset, enable);
  408. break;
  409. case IRQ_TYPE_LEVEL_LOW:
  410. __bcm2835_gpio_irq_config(pc, GPLEN0, offset, enable);
  411. break;
  412. }
  413. }
  414. static void bcm2835_gpio_irq_enable(struct irq_data *data)
  415. {
  416. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  417. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  418. unsigned gpio = irqd_to_hwirq(data);
  419. unsigned offset = GPIO_REG_SHIFT(gpio);
  420. unsigned bank = GPIO_REG_OFFSET(gpio);
  421. unsigned long flags;
  422. spin_lock_irqsave(&pc->irq_lock[bank], flags);
  423. set_bit(offset, &pc->enabled_irq_map[bank]);
  424. bcm2835_gpio_irq_config(pc, gpio, true);
  425. spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  426. }
  427. static void bcm2835_gpio_irq_disable(struct irq_data *data)
  428. {
  429. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  430. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  431. unsigned gpio = irqd_to_hwirq(data);
  432. unsigned offset = GPIO_REG_SHIFT(gpio);
  433. unsigned bank = GPIO_REG_OFFSET(gpio);
  434. unsigned long flags;
  435. spin_lock_irqsave(&pc->irq_lock[bank], flags);
  436. bcm2835_gpio_irq_config(pc, gpio, false);
  437. /* Clear events that were latched prior to clearing event sources */
  438. bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
  439. clear_bit(offset, &pc->enabled_irq_map[bank]);
  440. spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  441. }
  442. static int __bcm2835_gpio_irq_set_type_disabled(struct bcm2835_pinctrl *pc,
  443. unsigned offset, unsigned int type)
  444. {
  445. switch (type) {
  446. case IRQ_TYPE_NONE:
  447. case IRQ_TYPE_EDGE_RISING:
  448. case IRQ_TYPE_EDGE_FALLING:
  449. case IRQ_TYPE_EDGE_BOTH:
  450. case IRQ_TYPE_LEVEL_HIGH:
  451. case IRQ_TYPE_LEVEL_LOW:
  452. pc->irq_type[offset] = type;
  453. break;
  454. default:
  455. return -EINVAL;
  456. }
  457. return 0;
  458. }
  459. /* slower path for reconfiguring IRQ type */
  460. static int __bcm2835_gpio_irq_set_type_enabled(struct bcm2835_pinctrl *pc,
  461. unsigned offset, unsigned int type)
  462. {
  463. switch (type) {
  464. case IRQ_TYPE_NONE:
  465. if (pc->irq_type[offset] != type) {
  466. bcm2835_gpio_irq_config(pc, offset, false);
  467. pc->irq_type[offset] = type;
  468. }
  469. break;
  470. case IRQ_TYPE_EDGE_RISING:
  471. if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
  472. /* RISING already enabled, disable FALLING */
  473. pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
  474. bcm2835_gpio_irq_config(pc, offset, false);
  475. pc->irq_type[offset] = type;
  476. } else if (pc->irq_type[offset] != type) {
  477. bcm2835_gpio_irq_config(pc, offset, false);
  478. pc->irq_type[offset] = type;
  479. bcm2835_gpio_irq_config(pc, offset, true);
  480. }
  481. break;
  482. case IRQ_TYPE_EDGE_FALLING:
  483. if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
  484. /* FALLING already enabled, disable RISING */
  485. pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
  486. bcm2835_gpio_irq_config(pc, offset, false);
  487. pc->irq_type[offset] = type;
  488. } else if (pc->irq_type[offset] != type) {
  489. bcm2835_gpio_irq_config(pc, offset, false);
  490. pc->irq_type[offset] = type;
  491. bcm2835_gpio_irq_config(pc, offset, true);
  492. }
  493. break;
  494. case IRQ_TYPE_EDGE_BOTH:
  495. if (pc->irq_type[offset] == IRQ_TYPE_EDGE_RISING) {
  496. /* RISING already enabled, enable FALLING too */
  497. pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
  498. bcm2835_gpio_irq_config(pc, offset, true);
  499. pc->irq_type[offset] = type;
  500. } else if (pc->irq_type[offset] == IRQ_TYPE_EDGE_FALLING) {
  501. /* FALLING already enabled, enable RISING too */
  502. pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
  503. bcm2835_gpio_irq_config(pc, offset, true);
  504. pc->irq_type[offset] = type;
  505. } else if (pc->irq_type[offset] != type) {
  506. bcm2835_gpio_irq_config(pc, offset, false);
  507. pc->irq_type[offset] = type;
  508. bcm2835_gpio_irq_config(pc, offset, true);
  509. }
  510. break;
  511. case IRQ_TYPE_LEVEL_HIGH:
  512. case IRQ_TYPE_LEVEL_LOW:
  513. if (pc->irq_type[offset] != type) {
  514. bcm2835_gpio_irq_config(pc, offset, false);
  515. pc->irq_type[offset] = type;
  516. bcm2835_gpio_irq_config(pc, offset, true);
  517. }
  518. break;
  519. default:
  520. return -EINVAL;
  521. }
  522. return 0;
  523. }
  524. static int bcm2835_gpio_irq_set_type(struct irq_data *data, unsigned int type)
  525. {
  526. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  527. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  528. unsigned gpio = irqd_to_hwirq(data);
  529. unsigned offset = GPIO_REG_SHIFT(gpio);
  530. unsigned bank = GPIO_REG_OFFSET(gpio);
  531. unsigned long flags;
  532. int ret;
  533. spin_lock_irqsave(&pc->irq_lock[bank], flags);
  534. if (test_bit(offset, &pc->enabled_irq_map[bank]))
  535. ret = __bcm2835_gpio_irq_set_type_enabled(pc, gpio, type);
  536. else
  537. ret = __bcm2835_gpio_irq_set_type_disabled(pc, gpio, type);
  538. if (type & IRQ_TYPE_EDGE_BOTH)
  539. irq_set_handler_locked(data, handle_edge_irq);
  540. else
  541. irq_set_handler_locked(data, handle_level_irq);
  542. spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  543. return ret;
  544. }
  545. static void bcm2835_gpio_irq_ack(struct irq_data *data)
  546. {
  547. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  548. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  549. unsigned gpio = irqd_to_hwirq(data);
  550. bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
  551. }
  552. static struct irq_chip bcm2835_gpio_irq_chip = {
  553. .name = MODULE_NAME,
  554. .irq_enable = bcm2835_gpio_irq_enable,
  555. .irq_disable = bcm2835_gpio_irq_disable,
  556. .irq_set_type = bcm2835_gpio_irq_set_type,
  557. .irq_ack = bcm2835_gpio_irq_ack,
  558. .irq_mask = bcm2835_gpio_irq_disable,
  559. .irq_unmask = bcm2835_gpio_irq_enable,
  560. };
  561. static int bcm2835_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  562. {
  563. return ARRAY_SIZE(bcm2835_gpio_groups);
  564. }
  565. static const char *bcm2835_pctl_get_group_name(struct pinctrl_dev *pctldev,
  566. unsigned selector)
  567. {
  568. return bcm2835_gpio_groups[selector];
  569. }
  570. static int bcm2835_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  571. unsigned selector,
  572. const unsigned **pins,
  573. unsigned *num_pins)
  574. {
  575. *pins = &bcm2835_gpio_pins[selector].number;
  576. *num_pins = 1;
  577. return 0;
  578. }
  579. static void bcm2835_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
  580. struct seq_file *s,
  581. unsigned offset)
  582. {
  583. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  584. struct gpio_chip *chip = &pc->gpio_chip;
  585. enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
  586. const char *fname = bcm2835_functions[fsel];
  587. int value = bcm2835_gpio_get_bit(pc, GPLEV0, offset);
  588. int irq = irq_find_mapping(chip->irqdomain, offset);
  589. seq_printf(s, "function %s in %s; irq %d (%s)",
  590. fname, value ? "hi" : "lo",
  591. irq, irq_type_names[pc->irq_type[offset]]);
  592. }
  593. static void bcm2835_pctl_dt_free_map(struct pinctrl_dev *pctldev,
  594. struct pinctrl_map *maps, unsigned num_maps)
  595. {
  596. int i;
  597. for (i = 0; i < num_maps; i++)
  598. if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  599. kfree(maps[i].data.configs.configs);
  600. kfree(maps);
  601. }
  602. static int bcm2835_pctl_dt_node_to_map_func(struct bcm2835_pinctrl *pc,
  603. struct device_node *np, u32 pin, u32 fnum,
  604. struct pinctrl_map **maps)
  605. {
  606. struct pinctrl_map *map = *maps;
  607. if (fnum >= ARRAY_SIZE(bcm2835_functions)) {
  608. dev_err(pc->dev, "%pOF: invalid brcm,function %d\n", np, fnum);
  609. return -EINVAL;
  610. }
  611. map->type = PIN_MAP_TYPE_MUX_GROUP;
  612. map->data.mux.group = bcm2835_gpio_groups[pin];
  613. map->data.mux.function = bcm2835_functions[fnum];
  614. (*maps)++;
  615. return 0;
  616. }
  617. static int bcm2835_pctl_dt_node_to_map_pull(struct bcm2835_pinctrl *pc,
  618. struct device_node *np, u32 pin, u32 pull,
  619. struct pinctrl_map **maps)
  620. {
  621. struct pinctrl_map *map = *maps;
  622. unsigned long *configs;
  623. if (pull > 2) {
  624. dev_err(pc->dev, "%pOF: invalid brcm,pull %d\n", np, pull);
  625. return -EINVAL;
  626. }
  627. configs = kzalloc(sizeof(*configs), GFP_KERNEL);
  628. if (!configs)
  629. return -ENOMEM;
  630. configs[0] = BCM2835_PINCONF_PACK(BCM2835_PINCONF_PARAM_PULL, pull);
  631. map->type = PIN_MAP_TYPE_CONFIGS_PIN;
  632. map->data.configs.group_or_pin = bcm2835_gpio_pins[pin].name;
  633. map->data.configs.configs = configs;
  634. map->data.configs.num_configs = 1;
  635. (*maps)++;
  636. return 0;
  637. }
  638. static int bcm2835_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  639. struct device_node *np,
  640. struct pinctrl_map **map, unsigned *num_maps)
  641. {
  642. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  643. struct property *pins, *funcs, *pulls;
  644. int num_pins, num_funcs, num_pulls, maps_per_pin;
  645. struct pinctrl_map *maps, *cur_map;
  646. int i, err;
  647. u32 pin, func, pull;
  648. pins = of_find_property(np, "brcm,pins", NULL);
  649. if (!pins) {
  650. dev_err(pc->dev, "%pOF: missing brcm,pins property\n", np);
  651. return -EINVAL;
  652. }
  653. funcs = of_find_property(np, "brcm,function", NULL);
  654. pulls = of_find_property(np, "brcm,pull", NULL);
  655. if (!funcs && !pulls) {
  656. dev_err(pc->dev,
  657. "%pOF: neither brcm,function nor brcm,pull specified\n",
  658. np);
  659. return -EINVAL;
  660. }
  661. num_pins = pins->length / 4;
  662. num_funcs = funcs ? (funcs->length / 4) : 0;
  663. num_pulls = pulls ? (pulls->length / 4) : 0;
  664. if (num_funcs > 1 && num_funcs != num_pins) {
  665. dev_err(pc->dev,
  666. "%pOF: brcm,function must have 1 or %d entries\n",
  667. np, num_pins);
  668. return -EINVAL;
  669. }
  670. if (num_pulls > 1 && num_pulls != num_pins) {
  671. dev_err(pc->dev,
  672. "%pOF: brcm,pull must have 1 or %d entries\n",
  673. np, num_pins);
  674. return -EINVAL;
  675. }
  676. maps_per_pin = 0;
  677. if (num_funcs)
  678. maps_per_pin++;
  679. if (num_pulls)
  680. maps_per_pin++;
  681. cur_map = maps = kzalloc(num_pins * maps_per_pin * sizeof(*maps),
  682. GFP_KERNEL);
  683. if (!maps)
  684. return -ENOMEM;
  685. for (i = 0; i < num_pins; i++) {
  686. err = of_property_read_u32_index(np, "brcm,pins", i, &pin);
  687. if (err)
  688. goto out;
  689. if (pin >= ARRAY_SIZE(bcm2835_gpio_pins)) {
  690. dev_err(pc->dev, "%pOF: invalid brcm,pins value %d\n",
  691. np, pin);
  692. err = -EINVAL;
  693. goto out;
  694. }
  695. if (num_funcs) {
  696. err = of_property_read_u32_index(np, "brcm,function",
  697. (num_funcs > 1) ? i : 0, &func);
  698. if (err)
  699. goto out;
  700. err = bcm2835_pctl_dt_node_to_map_func(pc, np, pin,
  701. func, &cur_map);
  702. if (err)
  703. goto out;
  704. }
  705. if (num_pulls) {
  706. err = of_property_read_u32_index(np, "brcm,pull",
  707. (num_pulls > 1) ? i : 0, &pull);
  708. if (err)
  709. goto out;
  710. err = bcm2835_pctl_dt_node_to_map_pull(pc, np, pin,
  711. pull, &cur_map);
  712. if (err)
  713. goto out;
  714. }
  715. }
  716. *map = maps;
  717. *num_maps = num_pins * maps_per_pin;
  718. return 0;
  719. out:
  720. bcm2835_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
  721. return err;
  722. }
  723. static const struct pinctrl_ops bcm2835_pctl_ops = {
  724. .get_groups_count = bcm2835_pctl_get_groups_count,
  725. .get_group_name = bcm2835_pctl_get_group_name,
  726. .get_group_pins = bcm2835_pctl_get_group_pins,
  727. .pin_dbg_show = bcm2835_pctl_pin_dbg_show,
  728. .dt_node_to_map = bcm2835_pctl_dt_node_to_map,
  729. .dt_free_map = bcm2835_pctl_dt_free_map,
  730. };
  731. static int bcm2835_pmx_free(struct pinctrl_dev *pctldev,
  732. unsigned offset)
  733. {
  734. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  735. /* disable by setting to GPIO_IN */
  736. bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
  737. return 0;
  738. }
  739. static int bcm2835_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  740. {
  741. return BCM2835_FSEL_COUNT;
  742. }
  743. static const char *bcm2835_pmx_get_function_name(struct pinctrl_dev *pctldev,
  744. unsigned selector)
  745. {
  746. return bcm2835_functions[selector];
  747. }
  748. static int bcm2835_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  749. unsigned selector,
  750. const char * const **groups,
  751. unsigned * const num_groups)
  752. {
  753. /* every pin can do every function */
  754. *groups = bcm2835_gpio_groups;
  755. *num_groups = ARRAY_SIZE(bcm2835_gpio_groups);
  756. return 0;
  757. }
  758. static int bcm2835_pmx_set(struct pinctrl_dev *pctldev,
  759. unsigned func_selector,
  760. unsigned group_selector)
  761. {
  762. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  763. bcm2835_pinctrl_fsel_set(pc, group_selector, func_selector);
  764. return 0;
  765. }
  766. static void bcm2835_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
  767. struct pinctrl_gpio_range *range,
  768. unsigned offset)
  769. {
  770. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  771. /* disable by setting to GPIO_IN */
  772. bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
  773. }
  774. static int bcm2835_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  775. struct pinctrl_gpio_range *range,
  776. unsigned offset,
  777. bool input)
  778. {
  779. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  780. enum bcm2835_fsel fsel = input ?
  781. BCM2835_FSEL_GPIO_IN : BCM2835_FSEL_GPIO_OUT;
  782. bcm2835_pinctrl_fsel_set(pc, offset, fsel);
  783. return 0;
  784. }
  785. static const struct pinmux_ops bcm2835_pmx_ops = {
  786. .free = bcm2835_pmx_free,
  787. .get_functions_count = bcm2835_pmx_get_functions_count,
  788. .get_function_name = bcm2835_pmx_get_function_name,
  789. .get_function_groups = bcm2835_pmx_get_function_groups,
  790. .set_mux = bcm2835_pmx_set,
  791. .gpio_disable_free = bcm2835_pmx_gpio_disable_free,
  792. .gpio_set_direction = bcm2835_pmx_gpio_set_direction,
  793. };
  794. static int bcm2835_pinconf_get(struct pinctrl_dev *pctldev,
  795. unsigned pin, unsigned long *config)
  796. {
  797. /* No way to read back config in HW */
  798. return -ENOTSUPP;
  799. }
  800. static int bcm2835_pinconf_set(struct pinctrl_dev *pctldev,
  801. unsigned pin, unsigned long *configs,
  802. unsigned num_configs)
  803. {
  804. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  805. enum bcm2835_pinconf_param param;
  806. u16 arg;
  807. u32 off, bit;
  808. int i;
  809. for (i = 0; i < num_configs; i++) {
  810. param = BCM2835_PINCONF_UNPACK_PARAM(configs[i]);
  811. arg = BCM2835_PINCONF_UNPACK_ARG(configs[i]);
  812. if (param != BCM2835_PINCONF_PARAM_PULL)
  813. return -EINVAL;
  814. off = GPIO_REG_OFFSET(pin);
  815. bit = GPIO_REG_SHIFT(pin);
  816. bcm2835_gpio_wr(pc, GPPUD, arg & 3);
  817. /*
  818. * BCM2835 datasheet say to wait 150 cycles, but not of what.
  819. * But the VideoCore firmware delay for this operation
  820. * based nearly on the same amount of VPU cycles and this clock
  821. * runs at 250 MHz.
  822. */
  823. udelay(1);
  824. bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), BIT(bit));
  825. udelay(1);
  826. bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), 0);
  827. } /* for each config */
  828. return 0;
  829. }
  830. static const struct pinconf_ops bcm2835_pinconf_ops = {
  831. .pin_config_get = bcm2835_pinconf_get,
  832. .pin_config_set = bcm2835_pinconf_set,
  833. };
  834. static struct pinctrl_desc bcm2835_pinctrl_desc = {
  835. .name = MODULE_NAME,
  836. .pins = bcm2835_gpio_pins,
  837. .npins = ARRAY_SIZE(bcm2835_gpio_pins),
  838. .pctlops = &bcm2835_pctl_ops,
  839. .pmxops = &bcm2835_pmx_ops,
  840. .confops = &bcm2835_pinconf_ops,
  841. .owner = THIS_MODULE,
  842. };
  843. static struct pinctrl_gpio_range bcm2835_pinctrl_gpio_range = {
  844. .name = MODULE_NAME,
  845. .npins = BCM2835_NUM_GPIOS,
  846. };
  847. static int bcm2835_pinctrl_probe(struct platform_device *pdev)
  848. {
  849. struct device *dev = &pdev->dev;
  850. struct device_node *np = dev->of_node;
  851. struct bcm2835_pinctrl *pc;
  852. struct resource iomem;
  853. int err, i;
  854. BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_pins) != BCM2835_NUM_GPIOS);
  855. BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_groups) != BCM2835_NUM_GPIOS);
  856. pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
  857. if (!pc)
  858. return -ENOMEM;
  859. platform_set_drvdata(pdev, pc);
  860. pc->dev = dev;
  861. err = of_address_to_resource(np, 0, &iomem);
  862. if (err) {
  863. dev_err(dev, "could not get IO memory\n");
  864. return err;
  865. }
  866. pc->base = devm_ioremap_resource(dev, &iomem);
  867. if (IS_ERR(pc->base))
  868. return PTR_ERR(pc->base);
  869. pc->gpio_chip = bcm2835_gpio_chip;
  870. pc->gpio_chip.parent = dev;
  871. pc->gpio_chip.of_node = np;
  872. for (i = 0; i < BCM2835_NUM_BANKS; i++) {
  873. unsigned long events;
  874. unsigned offset;
  875. /* clear event detection flags */
  876. bcm2835_gpio_wr(pc, GPREN0 + i * 4, 0);
  877. bcm2835_gpio_wr(pc, GPFEN0 + i * 4, 0);
  878. bcm2835_gpio_wr(pc, GPHEN0 + i * 4, 0);
  879. bcm2835_gpio_wr(pc, GPLEN0 + i * 4, 0);
  880. bcm2835_gpio_wr(pc, GPAREN0 + i * 4, 0);
  881. bcm2835_gpio_wr(pc, GPAFEN0 + i * 4, 0);
  882. /* clear all the events */
  883. events = bcm2835_gpio_rd(pc, GPEDS0 + i * 4);
  884. for_each_set_bit(offset, &events, 32)
  885. bcm2835_gpio_wr(pc, GPEDS0 + i * 4, BIT(offset));
  886. spin_lock_init(&pc->irq_lock[i]);
  887. }
  888. err = gpiochip_add_data(&pc->gpio_chip, pc);
  889. if (err) {
  890. dev_err(dev, "could not add GPIO chip\n");
  891. return err;
  892. }
  893. err = gpiochip_irqchip_add(&pc->gpio_chip, &bcm2835_gpio_irq_chip,
  894. 0, handle_level_irq, IRQ_TYPE_NONE);
  895. if (err) {
  896. dev_info(dev, "could not add irqchip\n");
  897. return err;
  898. }
  899. for (i = 0; i < BCM2835_NUM_IRQS; i++) {
  900. pc->irq[i] = irq_of_parse_and_map(np, i);
  901. if (pc->irq[i] == 0)
  902. continue;
  903. /*
  904. * Use the same handler for all groups: this is necessary
  905. * since we use one gpiochip to cover all lines - the
  906. * irq handler then needs to figure out which group and
  907. * bank that was firing the IRQ and look up the per-group
  908. * and bank data.
  909. */
  910. gpiochip_set_chained_irqchip(&pc->gpio_chip,
  911. &bcm2835_gpio_irq_chip,
  912. pc->irq[i],
  913. bcm2835_gpio_irq_handler);
  914. }
  915. pc->pctl_dev = devm_pinctrl_register(dev, &bcm2835_pinctrl_desc, pc);
  916. if (IS_ERR(pc->pctl_dev)) {
  917. gpiochip_remove(&pc->gpio_chip);
  918. return PTR_ERR(pc->pctl_dev);
  919. }
  920. pc->gpio_range = bcm2835_pinctrl_gpio_range;
  921. pc->gpio_range.base = pc->gpio_chip.base;
  922. pc->gpio_range.gc = &pc->gpio_chip;
  923. pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
  924. return 0;
  925. }
  926. static const struct of_device_id bcm2835_pinctrl_match[] = {
  927. { .compatible = "brcm,bcm2835-gpio" },
  928. {}
  929. };
  930. static struct platform_driver bcm2835_pinctrl_driver = {
  931. .probe = bcm2835_pinctrl_probe,
  932. .driver = {
  933. .name = MODULE_NAME,
  934. .of_match_table = bcm2835_pinctrl_match,
  935. .suppress_bind_attrs = true,
  936. },
  937. };
  938. builtin_platform_driver(bcm2835_pinctrl_driver);