pinctrl-bcm2835.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. raw_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. events = bcm2835_gpio_rd(pc, GPEDS0 + bank * 4);
  336. events &= mask;
  337. events &= pc->enabled_irq_map[bank];
  338. for_each_set_bit(offset, &events, 32) {
  339. gpio = (32 * bank) + offset;
  340. generic_handle_irq(irq_linear_revmap(pc->gpio_chip.irqdomain,
  341. gpio));
  342. }
  343. }
  344. static void bcm2835_gpio_irq_handler(struct irq_desc *desc)
  345. {
  346. struct gpio_chip *chip = irq_desc_get_handler_data(desc);
  347. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  348. struct irq_chip *host_chip = irq_desc_get_chip(desc);
  349. int irq = irq_desc_get_irq(desc);
  350. int group;
  351. int i;
  352. for (i = 0; i < ARRAY_SIZE(pc->irq); i++) {
  353. if (pc->irq[i] == irq) {
  354. group = i;
  355. break;
  356. }
  357. }
  358. /* This should not happen, every IRQ has a bank */
  359. if (i == ARRAY_SIZE(pc->irq))
  360. BUG();
  361. chained_irq_enter(host_chip, desc);
  362. switch (group) {
  363. case 0: /* IRQ0 covers GPIOs 0-27 */
  364. bcm2835_gpio_irq_handle_bank(pc, 0, 0x0fffffff);
  365. break;
  366. case 1: /* IRQ1 covers GPIOs 28-45 */
  367. bcm2835_gpio_irq_handle_bank(pc, 0, 0xf0000000);
  368. bcm2835_gpio_irq_handle_bank(pc, 1, 0x00003fff);
  369. break;
  370. case 2: /* IRQ2 covers GPIOs 46-53 */
  371. bcm2835_gpio_irq_handle_bank(pc, 1, 0x003fc000);
  372. break;
  373. }
  374. chained_irq_exit(host_chip, desc);
  375. }
  376. static inline void __bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
  377. unsigned reg, unsigned offset, bool enable)
  378. {
  379. u32 value;
  380. reg += GPIO_REG_OFFSET(offset) * 4;
  381. value = bcm2835_gpio_rd(pc, reg);
  382. if (enable)
  383. value |= BIT(GPIO_REG_SHIFT(offset));
  384. else
  385. value &= ~(BIT(GPIO_REG_SHIFT(offset)));
  386. bcm2835_gpio_wr(pc, reg, value);
  387. }
  388. /* fast path for IRQ handler */
  389. static void bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
  390. unsigned offset, bool enable)
  391. {
  392. switch (pc->irq_type[offset]) {
  393. case IRQ_TYPE_EDGE_RISING:
  394. __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
  395. break;
  396. case IRQ_TYPE_EDGE_FALLING:
  397. __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
  398. break;
  399. case IRQ_TYPE_EDGE_BOTH:
  400. __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
  401. __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
  402. break;
  403. case IRQ_TYPE_LEVEL_HIGH:
  404. __bcm2835_gpio_irq_config(pc, GPHEN0, offset, enable);
  405. break;
  406. case IRQ_TYPE_LEVEL_LOW:
  407. __bcm2835_gpio_irq_config(pc, GPLEN0, offset, enable);
  408. break;
  409. }
  410. }
  411. static void bcm2835_gpio_irq_enable(struct irq_data *data)
  412. {
  413. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  414. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  415. unsigned gpio = irqd_to_hwirq(data);
  416. unsigned offset = GPIO_REG_SHIFT(gpio);
  417. unsigned bank = GPIO_REG_OFFSET(gpio);
  418. unsigned long flags;
  419. raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
  420. set_bit(offset, &pc->enabled_irq_map[bank]);
  421. bcm2835_gpio_irq_config(pc, gpio, true);
  422. raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  423. }
  424. static void bcm2835_gpio_irq_disable(struct irq_data *data)
  425. {
  426. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  427. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  428. unsigned gpio = irqd_to_hwirq(data);
  429. unsigned offset = GPIO_REG_SHIFT(gpio);
  430. unsigned bank = GPIO_REG_OFFSET(gpio);
  431. unsigned long flags;
  432. raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
  433. bcm2835_gpio_irq_config(pc, gpio, false);
  434. /* Clear events that were latched prior to clearing event sources */
  435. bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
  436. clear_bit(offset, &pc->enabled_irq_map[bank]);
  437. raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  438. }
  439. static int __bcm2835_gpio_irq_set_type_disabled(struct bcm2835_pinctrl *pc,
  440. unsigned offset, unsigned int type)
  441. {
  442. switch (type) {
  443. case IRQ_TYPE_NONE:
  444. case IRQ_TYPE_EDGE_RISING:
  445. case IRQ_TYPE_EDGE_FALLING:
  446. case IRQ_TYPE_EDGE_BOTH:
  447. case IRQ_TYPE_LEVEL_HIGH:
  448. case IRQ_TYPE_LEVEL_LOW:
  449. pc->irq_type[offset] = type;
  450. break;
  451. default:
  452. return -EINVAL;
  453. }
  454. return 0;
  455. }
  456. /* slower path for reconfiguring IRQ type */
  457. static int __bcm2835_gpio_irq_set_type_enabled(struct bcm2835_pinctrl *pc,
  458. unsigned offset, unsigned int type)
  459. {
  460. switch (type) {
  461. case IRQ_TYPE_NONE:
  462. if (pc->irq_type[offset] != type) {
  463. bcm2835_gpio_irq_config(pc, offset, false);
  464. pc->irq_type[offset] = type;
  465. }
  466. break;
  467. case IRQ_TYPE_EDGE_RISING:
  468. if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
  469. /* RISING already enabled, disable FALLING */
  470. pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
  471. bcm2835_gpio_irq_config(pc, offset, false);
  472. pc->irq_type[offset] = type;
  473. } else if (pc->irq_type[offset] != type) {
  474. bcm2835_gpio_irq_config(pc, offset, false);
  475. pc->irq_type[offset] = type;
  476. bcm2835_gpio_irq_config(pc, offset, true);
  477. }
  478. break;
  479. case IRQ_TYPE_EDGE_FALLING:
  480. if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
  481. /* FALLING already enabled, disable RISING */
  482. pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
  483. bcm2835_gpio_irq_config(pc, offset, false);
  484. pc->irq_type[offset] = type;
  485. } else if (pc->irq_type[offset] != type) {
  486. bcm2835_gpio_irq_config(pc, offset, false);
  487. pc->irq_type[offset] = type;
  488. bcm2835_gpio_irq_config(pc, offset, true);
  489. }
  490. break;
  491. case IRQ_TYPE_EDGE_BOTH:
  492. if (pc->irq_type[offset] == IRQ_TYPE_EDGE_RISING) {
  493. /* RISING already enabled, enable FALLING too */
  494. pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
  495. bcm2835_gpio_irq_config(pc, offset, true);
  496. pc->irq_type[offset] = type;
  497. } else if (pc->irq_type[offset] == IRQ_TYPE_EDGE_FALLING) {
  498. /* FALLING already enabled, enable RISING too */
  499. pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
  500. bcm2835_gpio_irq_config(pc, offset, true);
  501. pc->irq_type[offset] = type;
  502. } else if (pc->irq_type[offset] != type) {
  503. bcm2835_gpio_irq_config(pc, offset, false);
  504. pc->irq_type[offset] = type;
  505. bcm2835_gpio_irq_config(pc, offset, true);
  506. }
  507. break;
  508. case IRQ_TYPE_LEVEL_HIGH:
  509. case IRQ_TYPE_LEVEL_LOW:
  510. if (pc->irq_type[offset] != type) {
  511. bcm2835_gpio_irq_config(pc, offset, false);
  512. pc->irq_type[offset] = type;
  513. bcm2835_gpio_irq_config(pc, offset, true);
  514. }
  515. break;
  516. default:
  517. return -EINVAL;
  518. }
  519. return 0;
  520. }
  521. static int bcm2835_gpio_irq_set_type(struct irq_data *data, unsigned int type)
  522. {
  523. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  524. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  525. unsigned gpio = irqd_to_hwirq(data);
  526. unsigned offset = GPIO_REG_SHIFT(gpio);
  527. unsigned bank = GPIO_REG_OFFSET(gpio);
  528. unsigned long flags;
  529. int ret;
  530. raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
  531. if (test_bit(offset, &pc->enabled_irq_map[bank]))
  532. ret = __bcm2835_gpio_irq_set_type_enabled(pc, gpio, type);
  533. else
  534. ret = __bcm2835_gpio_irq_set_type_disabled(pc, gpio, type);
  535. if (type & IRQ_TYPE_EDGE_BOTH)
  536. irq_set_handler_locked(data, handle_edge_irq);
  537. else
  538. irq_set_handler_locked(data, handle_level_irq);
  539. raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  540. return ret;
  541. }
  542. static void bcm2835_gpio_irq_ack(struct irq_data *data)
  543. {
  544. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  545. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  546. unsigned gpio = irqd_to_hwirq(data);
  547. bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
  548. }
  549. static struct irq_chip bcm2835_gpio_irq_chip = {
  550. .name = MODULE_NAME,
  551. .irq_enable = bcm2835_gpio_irq_enable,
  552. .irq_disable = bcm2835_gpio_irq_disable,
  553. .irq_set_type = bcm2835_gpio_irq_set_type,
  554. .irq_ack = bcm2835_gpio_irq_ack,
  555. .irq_mask = bcm2835_gpio_irq_disable,
  556. .irq_unmask = bcm2835_gpio_irq_enable,
  557. };
  558. static int bcm2835_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  559. {
  560. return ARRAY_SIZE(bcm2835_gpio_groups);
  561. }
  562. static const char *bcm2835_pctl_get_group_name(struct pinctrl_dev *pctldev,
  563. unsigned selector)
  564. {
  565. return bcm2835_gpio_groups[selector];
  566. }
  567. static int bcm2835_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  568. unsigned selector,
  569. const unsigned **pins,
  570. unsigned *num_pins)
  571. {
  572. *pins = &bcm2835_gpio_pins[selector].number;
  573. *num_pins = 1;
  574. return 0;
  575. }
  576. static void bcm2835_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
  577. struct seq_file *s,
  578. unsigned offset)
  579. {
  580. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  581. struct gpio_chip *chip = &pc->gpio_chip;
  582. enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
  583. const char *fname = bcm2835_functions[fsel];
  584. int value = bcm2835_gpio_get_bit(pc, GPLEV0, offset);
  585. int irq = irq_find_mapping(chip->irqdomain, offset);
  586. seq_printf(s, "function %s in %s; irq %d (%s)",
  587. fname, value ? "hi" : "lo",
  588. irq, irq_type_names[pc->irq_type[offset]]);
  589. }
  590. static void bcm2835_pctl_dt_free_map(struct pinctrl_dev *pctldev,
  591. struct pinctrl_map *maps, unsigned num_maps)
  592. {
  593. int i;
  594. for (i = 0; i < num_maps; i++)
  595. if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  596. kfree(maps[i].data.configs.configs);
  597. kfree(maps);
  598. }
  599. static int bcm2835_pctl_dt_node_to_map_func(struct bcm2835_pinctrl *pc,
  600. struct device_node *np, u32 pin, u32 fnum,
  601. struct pinctrl_map **maps)
  602. {
  603. struct pinctrl_map *map = *maps;
  604. if (fnum >= ARRAY_SIZE(bcm2835_functions)) {
  605. dev_err(pc->dev, "%pOF: invalid brcm,function %d\n", np, fnum);
  606. return -EINVAL;
  607. }
  608. map->type = PIN_MAP_TYPE_MUX_GROUP;
  609. map->data.mux.group = bcm2835_gpio_groups[pin];
  610. map->data.mux.function = bcm2835_functions[fnum];
  611. (*maps)++;
  612. return 0;
  613. }
  614. static int bcm2835_pctl_dt_node_to_map_pull(struct bcm2835_pinctrl *pc,
  615. struct device_node *np, u32 pin, u32 pull,
  616. struct pinctrl_map **maps)
  617. {
  618. struct pinctrl_map *map = *maps;
  619. unsigned long *configs;
  620. if (pull > 2) {
  621. dev_err(pc->dev, "%pOF: invalid brcm,pull %d\n", np, pull);
  622. return -EINVAL;
  623. }
  624. configs = kzalloc(sizeof(*configs), GFP_KERNEL);
  625. if (!configs)
  626. return -ENOMEM;
  627. configs[0] = BCM2835_PINCONF_PACK(BCM2835_PINCONF_PARAM_PULL, pull);
  628. map->type = PIN_MAP_TYPE_CONFIGS_PIN;
  629. map->data.configs.group_or_pin = bcm2835_gpio_pins[pin].name;
  630. map->data.configs.configs = configs;
  631. map->data.configs.num_configs = 1;
  632. (*maps)++;
  633. return 0;
  634. }
  635. static int bcm2835_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  636. struct device_node *np,
  637. struct pinctrl_map **map, unsigned *num_maps)
  638. {
  639. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  640. struct property *pins, *funcs, *pulls;
  641. int num_pins, num_funcs, num_pulls, maps_per_pin;
  642. struct pinctrl_map *maps, *cur_map;
  643. int i, err;
  644. u32 pin, func, pull;
  645. pins = of_find_property(np, "brcm,pins", NULL);
  646. if (!pins) {
  647. dev_err(pc->dev, "%pOF: missing brcm,pins property\n", np);
  648. return -EINVAL;
  649. }
  650. funcs = of_find_property(np, "brcm,function", NULL);
  651. pulls = of_find_property(np, "brcm,pull", NULL);
  652. if (!funcs && !pulls) {
  653. dev_err(pc->dev,
  654. "%pOF: neither brcm,function nor brcm,pull specified\n",
  655. np);
  656. return -EINVAL;
  657. }
  658. num_pins = pins->length / 4;
  659. num_funcs = funcs ? (funcs->length / 4) : 0;
  660. num_pulls = pulls ? (pulls->length / 4) : 0;
  661. if (num_funcs > 1 && num_funcs != num_pins) {
  662. dev_err(pc->dev,
  663. "%pOF: brcm,function must have 1 or %d entries\n",
  664. np, num_pins);
  665. return -EINVAL;
  666. }
  667. if (num_pulls > 1 && num_pulls != num_pins) {
  668. dev_err(pc->dev,
  669. "%pOF: brcm,pull must have 1 or %d entries\n",
  670. np, num_pins);
  671. return -EINVAL;
  672. }
  673. maps_per_pin = 0;
  674. if (num_funcs)
  675. maps_per_pin++;
  676. if (num_pulls)
  677. maps_per_pin++;
  678. cur_map = maps = kzalloc(num_pins * maps_per_pin * sizeof(*maps),
  679. GFP_KERNEL);
  680. if (!maps)
  681. return -ENOMEM;
  682. for (i = 0; i < num_pins; i++) {
  683. err = of_property_read_u32_index(np, "brcm,pins", i, &pin);
  684. if (err)
  685. goto out;
  686. if (pin >= ARRAY_SIZE(bcm2835_gpio_pins)) {
  687. dev_err(pc->dev, "%pOF: invalid brcm,pins value %d\n",
  688. np, pin);
  689. err = -EINVAL;
  690. goto out;
  691. }
  692. if (num_funcs) {
  693. err = of_property_read_u32_index(np, "brcm,function",
  694. (num_funcs > 1) ? i : 0, &func);
  695. if (err)
  696. goto out;
  697. err = bcm2835_pctl_dt_node_to_map_func(pc, np, pin,
  698. func, &cur_map);
  699. if (err)
  700. goto out;
  701. }
  702. if (num_pulls) {
  703. err = of_property_read_u32_index(np, "brcm,pull",
  704. (num_pulls > 1) ? i : 0, &pull);
  705. if (err)
  706. goto out;
  707. err = bcm2835_pctl_dt_node_to_map_pull(pc, np, pin,
  708. pull, &cur_map);
  709. if (err)
  710. goto out;
  711. }
  712. }
  713. *map = maps;
  714. *num_maps = num_pins * maps_per_pin;
  715. return 0;
  716. out:
  717. bcm2835_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
  718. return err;
  719. }
  720. static const struct pinctrl_ops bcm2835_pctl_ops = {
  721. .get_groups_count = bcm2835_pctl_get_groups_count,
  722. .get_group_name = bcm2835_pctl_get_group_name,
  723. .get_group_pins = bcm2835_pctl_get_group_pins,
  724. .pin_dbg_show = bcm2835_pctl_pin_dbg_show,
  725. .dt_node_to_map = bcm2835_pctl_dt_node_to_map,
  726. .dt_free_map = bcm2835_pctl_dt_free_map,
  727. };
  728. static int bcm2835_pmx_free(struct pinctrl_dev *pctldev,
  729. unsigned offset)
  730. {
  731. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  732. /* disable by setting to GPIO_IN */
  733. bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
  734. return 0;
  735. }
  736. static int bcm2835_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  737. {
  738. return BCM2835_FSEL_COUNT;
  739. }
  740. static const char *bcm2835_pmx_get_function_name(struct pinctrl_dev *pctldev,
  741. unsigned selector)
  742. {
  743. return bcm2835_functions[selector];
  744. }
  745. static int bcm2835_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  746. unsigned selector,
  747. const char * const **groups,
  748. unsigned * const num_groups)
  749. {
  750. /* every pin can do every function */
  751. *groups = bcm2835_gpio_groups;
  752. *num_groups = ARRAY_SIZE(bcm2835_gpio_groups);
  753. return 0;
  754. }
  755. static int bcm2835_pmx_set(struct pinctrl_dev *pctldev,
  756. unsigned func_selector,
  757. unsigned group_selector)
  758. {
  759. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  760. bcm2835_pinctrl_fsel_set(pc, group_selector, func_selector);
  761. return 0;
  762. }
  763. static void bcm2835_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
  764. struct pinctrl_gpio_range *range,
  765. unsigned offset)
  766. {
  767. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  768. /* disable by setting to GPIO_IN */
  769. bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
  770. }
  771. static int bcm2835_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  772. struct pinctrl_gpio_range *range,
  773. unsigned offset,
  774. bool input)
  775. {
  776. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  777. enum bcm2835_fsel fsel = input ?
  778. BCM2835_FSEL_GPIO_IN : BCM2835_FSEL_GPIO_OUT;
  779. bcm2835_pinctrl_fsel_set(pc, offset, fsel);
  780. return 0;
  781. }
  782. static const struct pinmux_ops bcm2835_pmx_ops = {
  783. .free = bcm2835_pmx_free,
  784. .get_functions_count = bcm2835_pmx_get_functions_count,
  785. .get_function_name = bcm2835_pmx_get_function_name,
  786. .get_function_groups = bcm2835_pmx_get_function_groups,
  787. .set_mux = bcm2835_pmx_set,
  788. .gpio_disable_free = bcm2835_pmx_gpio_disable_free,
  789. .gpio_set_direction = bcm2835_pmx_gpio_set_direction,
  790. };
  791. static int bcm2835_pinconf_get(struct pinctrl_dev *pctldev,
  792. unsigned pin, unsigned long *config)
  793. {
  794. /* No way to read back config in HW */
  795. return -ENOTSUPP;
  796. }
  797. static int bcm2835_pinconf_set(struct pinctrl_dev *pctldev,
  798. unsigned pin, unsigned long *configs,
  799. unsigned num_configs)
  800. {
  801. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  802. enum bcm2835_pinconf_param param;
  803. u16 arg;
  804. u32 off, bit;
  805. int i;
  806. for (i = 0; i < num_configs; i++) {
  807. param = BCM2835_PINCONF_UNPACK_PARAM(configs[i]);
  808. arg = BCM2835_PINCONF_UNPACK_ARG(configs[i]);
  809. if (param != BCM2835_PINCONF_PARAM_PULL)
  810. return -EINVAL;
  811. off = GPIO_REG_OFFSET(pin);
  812. bit = GPIO_REG_SHIFT(pin);
  813. bcm2835_gpio_wr(pc, GPPUD, arg & 3);
  814. /*
  815. * BCM2835 datasheet say to wait 150 cycles, but not of what.
  816. * But the VideoCore firmware delay for this operation
  817. * based nearly on the same amount of VPU cycles and this clock
  818. * runs at 250 MHz.
  819. */
  820. udelay(1);
  821. bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), BIT(bit));
  822. udelay(1);
  823. bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), 0);
  824. } /* for each config */
  825. return 0;
  826. }
  827. static const struct pinconf_ops bcm2835_pinconf_ops = {
  828. .pin_config_get = bcm2835_pinconf_get,
  829. .pin_config_set = bcm2835_pinconf_set,
  830. };
  831. static struct pinctrl_desc bcm2835_pinctrl_desc = {
  832. .name = MODULE_NAME,
  833. .pins = bcm2835_gpio_pins,
  834. .npins = ARRAY_SIZE(bcm2835_gpio_pins),
  835. .pctlops = &bcm2835_pctl_ops,
  836. .pmxops = &bcm2835_pmx_ops,
  837. .confops = &bcm2835_pinconf_ops,
  838. .owner = THIS_MODULE,
  839. };
  840. static struct pinctrl_gpio_range bcm2835_pinctrl_gpio_range = {
  841. .name = MODULE_NAME,
  842. .npins = BCM2835_NUM_GPIOS,
  843. };
  844. static int bcm2835_pinctrl_probe(struct platform_device *pdev)
  845. {
  846. struct device *dev = &pdev->dev;
  847. struct device_node *np = dev->of_node;
  848. struct bcm2835_pinctrl *pc;
  849. struct resource iomem;
  850. int err, i;
  851. BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_pins) != BCM2835_NUM_GPIOS);
  852. BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_groups) != BCM2835_NUM_GPIOS);
  853. pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
  854. if (!pc)
  855. return -ENOMEM;
  856. platform_set_drvdata(pdev, pc);
  857. pc->dev = dev;
  858. err = of_address_to_resource(np, 0, &iomem);
  859. if (err) {
  860. dev_err(dev, "could not get IO memory\n");
  861. return err;
  862. }
  863. pc->base = devm_ioremap_resource(dev, &iomem);
  864. if (IS_ERR(pc->base))
  865. return PTR_ERR(pc->base);
  866. pc->gpio_chip = bcm2835_gpio_chip;
  867. pc->gpio_chip.parent = dev;
  868. pc->gpio_chip.of_node = np;
  869. for (i = 0; i < BCM2835_NUM_BANKS; i++) {
  870. unsigned long events;
  871. unsigned offset;
  872. /* clear event detection flags */
  873. bcm2835_gpio_wr(pc, GPREN0 + i * 4, 0);
  874. bcm2835_gpio_wr(pc, GPFEN0 + i * 4, 0);
  875. bcm2835_gpio_wr(pc, GPHEN0 + i * 4, 0);
  876. bcm2835_gpio_wr(pc, GPLEN0 + i * 4, 0);
  877. bcm2835_gpio_wr(pc, GPAREN0 + i * 4, 0);
  878. bcm2835_gpio_wr(pc, GPAFEN0 + i * 4, 0);
  879. /* clear all the events */
  880. events = bcm2835_gpio_rd(pc, GPEDS0 + i * 4);
  881. for_each_set_bit(offset, &events, 32)
  882. bcm2835_gpio_wr(pc, GPEDS0 + i * 4, BIT(offset));
  883. raw_spin_lock_init(&pc->irq_lock[i]);
  884. }
  885. err = gpiochip_add_data(&pc->gpio_chip, pc);
  886. if (err) {
  887. dev_err(dev, "could not add GPIO chip\n");
  888. return err;
  889. }
  890. err = gpiochip_irqchip_add(&pc->gpio_chip, &bcm2835_gpio_irq_chip,
  891. 0, handle_level_irq, IRQ_TYPE_NONE);
  892. if (err) {
  893. dev_info(dev, "could not add irqchip\n");
  894. return err;
  895. }
  896. for (i = 0; i < BCM2835_NUM_IRQS; i++) {
  897. pc->irq[i] = irq_of_parse_and_map(np, i);
  898. if (pc->irq[i] == 0)
  899. continue;
  900. /*
  901. * Use the same handler for all groups: this is necessary
  902. * since we use one gpiochip to cover all lines - the
  903. * irq handler then needs to figure out which group and
  904. * bank that was firing the IRQ and look up the per-group
  905. * and bank data.
  906. */
  907. gpiochip_set_chained_irqchip(&pc->gpio_chip,
  908. &bcm2835_gpio_irq_chip,
  909. pc->irq[i],
  910. bcm2835_gpio_irq_handler);
  911. }
  912. pc->pctl_dev = devm_pinctrl_register(dev, &bcm2835_pinctrl_desc, pc);
  913. if (IS_ERR(pc->pctl_dev)) {
  914. gpiochip_remove(&pc->gpio_chip);
  915. return PTR_ERR(pc->pctl_dev);
  916. }
  917. pc->gpio_range = bcm2835_pinctrl_gpio_range;
  918. pc->gpio_range.base = pc->gpio_chip.base;
  919. pc->gpio_range.gc = &pc->gpio_chip;
  920. pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
  921. return 0;
  922. }
  923. static const struct of_device_id bcm2835_pinctrl_match[] = {
  924. { .compatible = "brcm,bcm2835-gpio" },
  925. {}
  926. };
  927. static struct platform_driver bcm2835_pinctrl_driver = {
  928. .probe = bcm2835_pinctrl_probe,
  929. .driver = {
  930. .name = MODULE_NAME,
  931. .of_match_table = bcm2835_pinctrl_match,
  932. .suppress_bind_attrs = true,
  933. },
  934. };
  935. builtin_platform_driver(bcm2835_pinctrl_driver);