pinctrl-bcm2835.c 29 KB

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