pinctrl-bcm2835.c 28 KB

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