pinctrl-bcm2835.c 29 KB

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