pinctrl-bcm2835.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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. 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_request(struct gpio_chip *chip, unsigned offset)
  300. {
  301. return pinctrl_request_gpio(chip->base + offset);
  302. }
  303. static void bcm2835_gpio_free(struct gpio_chip *chip, unsigned offset)
  304. {
  305. pinctrl_free_gpio(chip->base + offset);
  306. }
  307. static int bcm2835_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  308. {
  309. return pinctrl_gpio_direction_input(chip->base + offset);
  310. }
  311. static int bcm2835_gpio_get(struct gpio_chip *chip, unsigned offset)
  312. {
  313. struct bcm2835_pinctrl *pc = dev_get_drvdata(chip->dev);
  314. return bcm2835_gpio_get_bit(pc, GPLEV0, offset);
  315. }
  316. static int bcm2835_gpio_direction_output(struct gpio_chip *chip,
  317. unsigned offset, int value)
  318. {
  319. return pinctrl_gpio_direction_output(chip->base + offset);
  320. }
  321. static void bcm2835_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  322. {
  323. struct bcm2835_pinctrl *pc = dev_get_drvdata(chip->dev);
  324. bcm2835_gpio_set_bit(pc, value ? GPSET0 : GPCLR0, offset);
  325. }
  326. static int bcm2835_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  327. {
  328. struct bcm2835_pinctrl *pc = dev_get_drvdata(chip->dev);
  329. return irq_linear_revmap(pc->irq_domain, offset);
  330. }
  331. static struct gpio_chip bcm2835_gpio_chip = {
  332. .label = MODULE_NAME,
  333. .owner = THIS_MODULE,
  334. .request = bcm2835_gpio_request,
  335. .free = bcm2835_gpio_free,
  336. .direction_input = bcm2835_gpio_direction_input,
  337. .direction_output = bcm2835_gpio_direction_output,
  338. .get = bcm2835_gpio_get,
  339. .set = bcm2835_gpio_set,
  340. .to_irq = bcm2835_gpio_to_irq,
  341. .base = -1,
  342. .ngpio = BCM2835_NUM_GPIOS,
  343. .can_sleep = 0,
  344. };
  345. static irqreturn_t bcm2835_gpio_irq_handler(int irq, void *dev_id)
  346. {
  347. struct bcm2835_gpio_irqdata *irqdata = dev_id;
  348. struct bcm2835_pinctrl *pc = irqdata->pc;
  349. int bank = irqdata->bank;
  350. unsigned long events;
  351. unsigned offset;
  352. unsigned gpio;
  353. unsigned int type;
  354. events = bcm2835_gpio_rd(pc, GPEDS0 + bank * 4);
  355. events &= pc->enabled_irq_map[bank];
  356. for_each_set_bit(offset, &events, 32) {
  357. gpio = (32 * bank) + offset;
  358. type = pc->irq_type[gpio];
  359. /* ack edge triggered IRQs immediately */
  360. if (!(type & IRQ_TYPE_LEVEL_MASK))
  361. bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
  362. generic_handle_irq(irq_linear_revmap(pc->irq_domain, gpio));
  363. /* ack level triggered IRQ after handling them */
  364. if (type & IRQ_TYPE_LEVEL_MASK)
  365. bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
  366. }
  367. return events ? IRQ_HANDLED : IRQ_NONE;
  368. }
  369. static inline void __bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
  370. unsigned reg, unsigned offset, bool enable)
  371. {
  372. u32 value;
  373. reg += GPIO_REG_OFFSET(offset) * 4;
  374. value = bcm2835_gpio_rd(pc, reg);
  375. if (enable)
  376. value |= BIT(GPIO_REG_SHIFT(offset));
  377. else
  378. value &= ~(BIT(GPIO_REG_SHIFT(offset)));
  379. bcm2835_gpio_wr(pc, reg, value);
  380. }
  381. /* fast path for IRQ handler */
  382. static void bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
  383. unsigned offset, bool enable)
  384. {
  385. switch (pc->irq_type[offset]) {
  386. case IRQ_TYPE_EDGE_RISING:
  387. __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
  388. break;
  389. case IRQ_TYPE_EDGE_FALLING:
  390. __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
  391. break;
  392. case IRQ_TYPE_EDGE_BOTH:
  393. __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
  394. __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
  395. break;
  396. case IRQ_TYPE_LEVEL_HIGH:
  397. __bcm2835_gpio_irq_config(pc, GPHEN0, offset, enable);
  398. break;
  399. case IRQ_TYPE_LEVEL_LOW:
  400. __bcm2835_gpio_irq_config(pc, GPLEN0, offset, enable);
  401. break;
  402. }
  403. }
  404. static void bcm2835_gpio_irq_enable(struct irq_data *data)
  405. {
  406. struct bcm2835_pinctrl *pc = irq_data_get_irq_chip_data(data);
  407. unsigned gpio = irqd_to_hwirq(data);
  408. unsigned offset = GPIO_REG_SHIFT(gpio);
  409. unsigned bank = GPIO_REG_OFFSET(gpio);
  410. unsigned long flags;
  411. spin_lock_irqsave(&pc->irq_lock[bank], flags);
  412. set_bit(offset, &pc->enabled_irq_map[bank]);
  413. bcm2835_gpio_irq_config(pc, gpio, true);
  414. spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  415. }
  416. static void bcm2835_gpio_irq_disable(struct irq_data *data)
  417. {
  418. struct bcm2835_pinctrl *pc = irq_data_get_irq_chip_data(data);
  419. unsigned gpio = irqd_to_hwirq(data);
  420. unsigned offset = GPIO_REG_SHIFT(gpio);
  421. unsigned bank = GPIO_REG_OFFSET(gpio);
  422. unsigned long flags;
  423. spin_lock_irqsave(&pc->irq_lock[bank], flags);
  424. bcm2835_gpio_irq_config(pc, gpio, false);
  425. clear_bit(offset, &pc->enabled_irq_map[bank]);
  426. 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 bcm2835_pinctrl *pc = irq_data_get_irq_chip_data(data);
  513. unsigned gpio = irqd_to_hwirq(data);
  514. unsigned offset = GPIO_REG_SHIFT(gpio);
  515. unsigned bank = GPIO_REG_OFFSET(gpio);
  516. unsigned long flags;
  517. int ret;
  518. spin_lock_irqsave(&pc->irq_lock[bank], flags);
  519. if (test_bit(offset, &pc->enabled_irq_map[bank]))
  520. ret = __bcm2835_gpio_irq_set_type_enabled(pc, gpio, type);
  521. else
  522. ret = __bcm2835_gpio_irq_set_type_disabled(pc, gpio, type);
  523. spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  524. return ret;
  525. }
  526. static struct irq_chip bcm2835_gpio_irq_chip = {
  527. .name = MODULE_NAME,
  528. .irq_enable = bcm2835_gpio_irq_enable,
  529. .irq_disable = bcm2835_gpio_irq_disable,
  530. .irq_set_type = bcm2835_gpio_irq_set_type,
  531. };
  532. static int bcm2835_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  533. {
  534. return ARRAY_SIZE(bcm2835_gpio_groups);
  535. }
  536. static const char *bcm2835_pctl_get_group_name(struct pinctrl_dev *pctldev,
  537. unsigned selector)
  538. {
  539. return bcm2835_gpio_groups[selector];
  540. }
  541. static int bcm2835_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  542. unsigned selector,
  543. const unsigned **pins,
  544. unsigned *num_pins)
  545. {
  546. *pins = &bcm2835_gpio_pins[selector].number;
  547. *num_pins = 1;
  548. return 0;
  549. }
  550. static void bcm2835_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
  551. struct seq_file *s,
  552. unsigned offset)
  553. {
  554. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  555. enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
  556. const char *fname = bcm2835_functions[fsel];
  557. int value = bcm2835_gpio_get_bit(pc, GPLEV0, offset);
  558. int irq = irq_find_mapping(pc->irq_domain, offset);
  559. seq_printf(s, "function %s in %s; irq %d (%s)",
  560. fname, value ? "hi" : "lo",
  561. irq, irq_type_names[pc->irq_type[offset]]);
  562. }
  563. static void bcm2835_pctl_dt_free_map(struct pinctrl_dev *pctldev,
  564. struct pinctrl_map *maps, unsigned num_maps)
  565. {
  566. int i;
  567. for (i = 0; i < num_maps; i++)
  568. if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  569. kfree(maps[i].data.configs.configs);
  570. kfree(maps);
  571. }
  572. static int bcm2835_pctl_dt_node_to_map_func(struct bcm2835_pinctrl *pc,
  573. struct device_node *np, u32 pin, u32 fnum,
  574. struct pinctrl_map **maps)
  575. {
  576. struct pinctrl_map *map = *maps;
  577. if (fnum >= ARRAY_SIZE(bcm2835_functions)) {
  578. dev_err(pc->dev, "%s: invalid brcm,function %d\n",
  579. of_node_full_name(np), fnum);
  580. return -EINVAL;
  581. }
  582. map->type = PIN_MAP_TYPE_MUX_GROUP;
  583. map->data.mux.group = bcm2835_gpio_groups[pin];
  584. map->data.mux.function = bcm2835_functions[fnum];
  585. (*maps)++;
  586. return 0;
  587. }
  588. static int bcm2835_pctl_dt_node_to_map_pull(struct bcm2835_pinctrl *pc,
  589. struct device_node *np, u32 pin, u32 pull,
  590. struct pinctrl_map **maps)
  591. {
  592. struct pinctrl_map *map = *maps;
  593. unsigned long *configs;
  594. if (pull > 2) {
  595. dev_err(pc->dev, "%s: invalid brcm,pull %d\n",
  596. of_node_full_name(np), pull);
  597. return -EINVAL;
  598. }
  599. configs = kzalloc(sizeof(*configs), GFP_KERNEL);
  600. if (!configs)
  601. return -ENOMEM;
  602. configs[0] = BCM2835_PINCONF_PACK(BCM2835_PINCONF_PARAM_PULL, pull);
  603. map->type = PIN_MAP_TYPE_CONFIGS_PIN;
  604. map->data.configs.group_or_pin = bcm2835_gpio_pins[pin].name;
  605. map->data.configs.configs = configs;
  606. map->data.configs.num_configs = 1;
  607. (*maps)++;
  608. return 0;
  609. }
  610. static inline u32 prop_u32(struct property *p, int i)
  611. {
  612. return be32_to_cpup(((__be32 *)p->value) + i);
  613. }
  614. static int bcm2835_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  615. struct device_node *np,
  616. struct pinctrl_map **map, unsigned *num_maps)
  617. {
  618. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  619. struct property *pins, *funcs, *pulls;
  620. int num_pins, num_funcs, num_pulls, maps_per_pin;
  621. struct pinctrl_map *maps, *cur_map;
  622. int i, err;
  623. u32 pin, func, pull;
  624. pins = of_find_property(np, "brcm,pins", NULL);
  625. if (!pins) {
  626. dev_err(pc->dev, "%s: missing brcm,pins property\n",
  627. of_node_full_name(np));
  628. return -EINVAL;
  629. }
  630. funcs = of_find_property(np, "brcm,function", NULL);
  631. pulls = of_find_property(np, "brcm,pull", NULL);
  632. if (!funcs && !pulls) {
  633. dev_err(pc->dev,
  634. "%s: neither brcm,function nor brcm,pull specified\n",
  635. of_node_full_name(np));
  636. return -EINVAL;
  637. }
  638. num_pins = pins->length / 4;
  639. num_funcs = funcs ? (funcs->length / 4) : 0;
  640. num_pulls = pulls ? (pulls->length / 4) : 0;
  641. if (num_funcs > 1 && num_funcs != num_pins) {
  642. dev_err(pc->dev,
  643. "%s: brcm,function must have 1 or %d entries\n",
  644. of_node_full_name(np), num_pins);
  645. return -EINVAL;
  646. }
  647. if (num_pulls > 1 && num_pulls != num_pins) {
  648. dev_err(pc->dev,
  649. "%s: brcm,pull must have 1 or %d entries\n",
  650. of_node_full_name(np), num_pins);
  651. return -EINVAL;
  652. }
  653. maps_per_pin = 0;
  654. if (num_funcs)
  655. maps_per_pin++;
  656. if (num_pulls)
  657. maps_per_pin++;
  658. cur_map = maps = kzalloc(num_pins * maps_per_pin * sizeof(*maps),
  659. GFP_KERNEL);
  660. if (!maps)
  661. return -ENOMEM;
  662. for (i = 0; i < num_pins; i++) {
  663. pin = prop_u32(pins, i);
  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. func = prop_u32(funcs, (num_funcs > 1) ? i : 0);
  672. err = bcm2835_pctl_dt_node_to_map_func(pc, np, pin,
  673. func, &cur_map);
  674. if (err)
  675. goto out;
  676. }
  677. if (num_pulls) {
  678. pull = prop_u32(pulls, (num_pulls > 1) ? i : 0);
  679. err = bcm2835_pctl_dt_node_to_map_pull(pc, np, pin,
  680. pull, &cur_map);
  681. if (err)
  682. goto out;
  683. }
  684. }
  685. *map = maps;
  686. *num_maps = num_pins * maps_per_pin;
  687. return 0;
  688. out:
  689. kfree(maps);
  690. return err;
  691. }
  692. static struct pinctrl_ops bcm2835_pctl_ops = {
  693. .get_groups_count = bcm2835_pctl_get_groups_count,
  694. .get_group_name = bcm2835_pctl_get_group_name,
  695. .get_group_pins = bcm2835_pctl_get_group_pins,
  696. .pin_dbg_show = bcm2835_pctl_pin_dbg_show,
  697. .dt_node_to_map = bcm2835_pctl_dt_node_to_map,
  698. .dt_free_map = bcm2835_pctl_dt_free_map,
  699. };
  700. static int bcm2835_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  701. {
  702. return BCM2835_FSEL_COUNT;
  703. }
  704. static const char *bcm2835_pmx_get_function_name(struct pinctrl_dev *pctldev,
  705. unsigned selector)
  706. {
  707. return bcm2835_functions[selector];
  708. }
  709. static int bcm2835_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  710. unsigned selector,
  711. const char * const **groups,
  712. unsigned * const num_groups)
  713. {
  714. /* every pin can do every function */
  715. *groups = bcm2835_gpio_groups;
  716. *num_groups = ARRAY_SIZE(bcm2835_gpio_groups);
  717. return 0;
  718. }
  719. static int bcm2835_pmx_enable(struct pinctrl_dev *pctldev,
  720. unsigned func_selector,
  721. unsigned group_selector)
  722. {
  723. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  724. bcm2835_pinctrl_fsel_set(pc, group_selector, func_selector);
  725. return 0;
  726. }
  727. static void bcm2835_pmx_disable(struct pinctrl_dev *pctldev,
  728. unsigned func_selector,
  729. unsigned group_selector)
  730. {
  731. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  732. /* disable by setting to GPIO_IN */
  733. bcm2835_pinctrl_fsel_set(pc, group_selector, BCM2835_FSEL_GPIO_IN);
  734. }
  735. static void bcm2835_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
  736. struct pinctrl_gpio_range *range,
  737. unsigned offset)
  738. {
  739. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  740. /* disable by setting to GPIO_IN */
  741. bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
  742. }
  743. static int bcm2835_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  744. struct pinctrl_gpio_range *range,
  745. unsigned offset,
  746. bool input)
  747. {
  748. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  749. enum bcm2835_fsel fsel = input ?
  750. BCM2835_FSEL_GPIO_IN : BCM2835_FSEL_GPIO_OUT;
  751. bcm2835_pinctrl_fsel_set(pc, offset, fsel);
  752. return 0;
  753. }
  754. static struct pinmux_ops bcm2835_pmx_ops = {
  755. .get_functions_count = bcm2835_pmx_get_functions_count,
  756. .get_function_name = bcm2835_pmx_get_function_name,
  757. .get_function_groups = bcm2835_pmx_get_function_groups,
  758. .enable = bcm2835_pmx_enable,
  759. .disable = bcm2835_pmx_disable,
  760. .gpio_disable_free = bcm2835_pmx_gpio_disable_free,
  761. .gpio_set_direction = bcm2835_pmx_gpio_set_direction,
  762. };
  763. static int bcm2835_pinconf_get(struct pinctrl_dev *pctldev,
  764. unsigned pin, unsigned long *config)
  765. {
  766. /* No way to read back config in HW */
  767. return -ENOTSUPP;
  768. }
  769. static int bcm2835_pinconf_set(struct pinctrl_dev *pctldev,
  770. unsigned pin, unsigned long config)
  771. {
  772. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  773. enum bcm2835_pinconf_param param = BCM2835_PINCONF_UNPACK_PARAM(config);
  774. u16 arg = BCM2835_PINCONF_UNPACK_ARG(config);
  775. u32 off, bit;
  776. if (param != BCM2835_PINCONF_PARAM_PULL)
  777. return -EINVAL;
  778. off = GPIO_REG_OFFSET(pin);
  779. bit = GPIO_REG_SHIFT(pin);
  780. bcm2835_gpio_wr(pc, GPPUD, arg & 3);
  781. /*
  782. * Docs say to wait 150 cycles, but not of what. We assume a
  783. * 1 MHz clock here, which is pretty slow...
  784. */
  785. udelay(150);
  786. bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), BIT(bit));
  787. udelay(150);
  788. bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), 0);
  789. return 0;
  790. }
  791. static struct pinconf_ops bcm2835_pinconf_ops = {
  792. .pin_config_get = bcm2835_pinconf_get,
  793. .pin_config_set = bcm2835_pinconf_set,
  794. };
  795. static struct pinctrl_desc bcm2835_pinctrl_desc = {
  796. .name = MODULE_NAME,
  797. .pins = bcm2835_gpio_pins,
  798. .npins = ARRAY_SIZE(bcm2835_gpio_pins),
  799. .pctlops = &bcm2835_pctl_ops,
  800. .pmxops = &bcm2835_pmx_ops,
  801. .confops = &bcm2835_pinconf_ops,
  802. .owner = THIS_MODULE,
  803. };
  804. static struct pinctrl_gpio_range bcm2835_pinctrl_gpio_range = {
  805. .name = MODULE_NAME,
  806. .npins = BCM2835_NUM_GPIOS,
  807. };
  808. static int bcm2835_pinctrl_probe(struct platform_device *pdev)
  809. {
  810. struct device *dev = &pdev->dev;
  811. struct device_node *np = dev->of_node;
  812. struct bcm2835_pinctrl *pc;
  813. struct resource iomem;
  814. int err, i;
  815. BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_pins) != BCM2835_NUM_GPIOS);
  816. BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_groups) != BCM2835_NUM_GPIOS);
  817. pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
  818. if (!pc)
  819. return -ENOMEM;
  820. platform_set_drvdata(pdev, pc);
  821. pc->dev = dev;
  822. err = of_address_to_resource(np, 0, &iomem);
  823. if (err) {
  824. dev_err(dev, "could not get IO memory\n");
  825. return err;
  826. }
  827. pc->base = devm_request_and_ioremap(dev, &iomem);
  828. if (!pc->base)
  829. return -EADDRNOTAVAIL;
  830. pc->gpio_chip = bcm2835_gpio_chip;
  831. pc->gpio_chip.dev = dev;
  832. pc->gpio_chip.of_node = np;
  833. pc->irq_domain = irq_domain_add_linear(np, BCM2835_NUM_GPIOS,
  834. &irq_domain_simple_ops, NULL);
  835. if (!pc->irq_domain) {
  836. dev_err(dev, "could not create IRQ domain\n");
  837. return -ENOMEM;
  838. }
  839. for (i = 0; i < BCM2835_NUM_GPIOS; i++) {
  840. int irq = irq_create_mapping(pc->irq_domain, i);
  841. irq_set_lockdep_class(irq, &gpio_lock_class);
  842. irq_set_chip_and_handler(irq, &bcm2835_gpio_irq_chip,
  843. handle_simple_irq);
  844. irq_set_chip_data(irq, pc);
  845. set_irq_flags(irq, IRQF_VALID);
  846. }
  847. for (i = 0; i < BCM2835_NUM_BANKS; i++) {
  848. unsigned long events;
  849. unsigned offset;
  850. int len;
  851. char *name;
  852. /* clear event detection flags */
  853. bcm2835_gpio_wr(pc, GPREN0 + i * 4, 0);
  854. bcm2835_gpio_wr(pc, GPFEN0 + i * 4, 0);
  855. bcm2835_gpio_wr(pc, GPHEN0 + i * 4, 0);
  856. bcm2835_gpio_wr(pc, GPLEN0 + i * 4, 0);
  857. bcm2835_gpio_wr(pc, GPAREN0 + i * 4, 0);
  858. bcm2835_gpio_wr(pc, GPAFEN0 + i * 4, 0);
  859. /* clear all the events */
  860. events = bcm2835_gpio_rd(pc, GPEDS0 + i * 4);
  861. for_each_set_bit(offset, &events, 32)
  862. bcm2835_gpio_wr(pc, GPEDS0 + i * 4, BIT(offset));
  863. pc->irq[i] = irq_of_parse_and_map(np, i);
  864. pc->irq_data[i].pc = pc;
  865. pc->irq_data[i].bank = i;
  866. spin_lock_init(&pc->irq_lock[i]);
  867. len = strlen(dev_name(pc->dev)) + 16;
  868. name = devm_kzalloc(pc->dev, len, GFP_KERNEL);
  869. if (!name)
  870. return -ENOMEM;
  871. snprintf(name, len, "%s:bank%d", dev_name(pc->dev), i);
  872. err = devm_request_irq(dev, pc->irq[i],
  873. bcm2835_gpio_irq_handler, IRQF_SHARED,
  874. name, &pc->irq_data[i]);
  875. if (err) {
  876. dev_err(dev, "unable to request IRQ %d\n", pc->irq[i]);
  877. return err;
  878. }
  879. }
  880. err = gpiochip_add(&pc->gpio_chip);
  881. if (err) {
  882. dev_err(dev, "could not add GPIO chip\n");
  883. return err;
  884. }
  885. pc->pctl_dev = pinctrl_register(&bcm2835_pinctrl_desc, dev, pc);
  886. if (!pc->pctl_dev) {
  887. gpiochip_remove(&pc->gpio_chip);
  888. return -EINVAL;
  889. }
  890. pc->gpio_range = bcm2835_pinctrl_gpio_range;
  891. pc->gpio_range.base = pc->gpio_chip.base;
  892. pc->gpio_range.gc = &pc->gpio_chip;
  893. pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
  894. return 0;
  895. }
  896. static int bcm2835_pinctrl_remove(struct platform_device *pdev)
  897. {
  898. struct bcm2835_pinctrl *pc = platform_get_drvdata(pdev);
  899. pinctrl_unregister(pc->pctl_dev);
  900. gpiochip_remove(&pc->gpio_chip);
  901. return 0;
  902. }
  903. static struct of_device_id bcm2835_pinctrl_match[] = {
  904. { .compatible = "brcm,bcm2835-gpio" },
  905. {}
  906. };
  907. MODULE_DEVICE_TABLE(of, bcm2835_pinctrl_match);
  908. static struct platform_driver bcm2835_pinctrl_driver = {
  909. .probe = bcm2835_pinctrl_probe,
  910. .remove = bcm2835_pinctrl_remove,
  911. .driver = {
  912. .name = MODULE_NAME,
  913. .owner = THIS_MODULE,
  914. .of_match_table = bcm2835_pinctrl_match,
  915. },
  916. };
  917. module_platform_driver(bcm2835_pinctrl_driver);
  918. MODULE_AUTHOR("Chris Boot, Simon Arlott, Stephen Warren");
  919. MODULE_DESCRIPTION("BCM2835 Pin control driver");
  920. MODULE_LICENSE("GPL");