pinctrl-bcm2835.c 28 KB

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