pinctrl-bcm2835.c 28 KB

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