pinctrl-bcm2835.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for Broadcom BCM2835 GPIO unit (pinctrl + GPIO)
  4. *
  5. * Copyright (C) 2012 Chris Boot, Simon Arlott, Stephen Warren
  6. *
  7. * This driver is inspired by:
  8. * pinctrl-nomadik.c, please see original file for copyright information
  9. * pinctrl-tegra.c, please see original file for copyright information
  10. */
  11. #include <linux/bitmap.h>
  12. #include <linux/bug.h>
  13. #include <linux/delay.h>
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/gpio/driver.h>
  17. #include <linux/io.h>
  18. #include <linux/irq.h>
  19. #include <linux/irqdesc.h>
  20. #include <linux/init.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/pinctrl/consumer.h>
  27. #include <linux/pinctrl/machine.h>
  28. #include <linux/pinctrl/pinconf.h>
  29. #include <linux/pinctrl/pinctrl.h>
  30. #include <linux/pinctrl/pinmux.h>
  31. #include <linux/pinctrl/pinconf-generic.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/slab.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/types.h>
  37. #include <dt-bindings/pinctrl/bcm2835.h>
  38. #define MODULE_NAME "pinctrl-bcm2835"
  39. #define BCM2835_NUM_GPIOS 54
  40. #define BCM2711_NUM_GPIOS 58
  41. #define BCM2835_NUM_BANKS 2
  42. #define BCM2835_NUM_IRQS 3
  43. /* GPIO register offsets */
  44. #define GPFSEL0 0x0 /* Function Select */
  45. #define GPSET0 0x1c /* Pin Output Set */
  46. #define GPCLR0 0x28 /* Pin Output Clear */
  47. #define GPLEV0 0x34 /* Pin Level */
  48. #define GPEDS0 0x40 /* Pin Event Detect Status */
  49. #define GPREN0 0x4c /* Pin Rising Edge Detect Enable */
  50. #define GPFEN0 0x58 /* Pin Falling Edge Detect Enable */
  51. #define GPHEN0 0x64 /* Pin High Detect Enable */
  52. #define GPLEN0 0x70 /* Pin Low Detect Enable */
  53. #define GPAREN0 0x7c /* Pin Async Rising Edge Detect */
  54. #define GPAFEN0 0x88 /* Pin Async Falling Edge Detect */
  55. #define GPPUD 0x94 /* Pin Pull-up/down Enable */
  56. #define GPPUDCLK0 0x98 /* Pin Pull-up/down Enable Clock */
  57. #define GP_GPIO_PUP_PDN_CNTRL_REG0 0xe4 /* 2711 Pin Pull-up/down select */
  58. #define FSEL_REG(p) (GPFSEL0 + (((p) / 10) * 4))
  59. #define FSEL_SHIFT(p) (((p) % 10) * 3)
  60. #define GPIO_REG_OFFSET(p) ((p) / 32)
  61. #define GPIO_REG_SHIFT(p) ((p) % 32)
  62. #define PUD_2711_MASK 0x3
  63. #define PUD_2711_REG_OFFSET(p) ((p) / 16)
  64. #define PUD_2711_REG_SHIFT(p) (((p) % 16) * 2)
  65. /* argument: bcm2835_pinconf_pull */
  66. #define BCM2835_PINCONF_PARAM_PULL (PIN_CONFIG_END + 1)
  67. #define BCM2711_PULL_NONE 0x0
  68. #define BCM2711_PULL_UP 0x1
  69. #define BCM2711_PULL_DOWN 0x2
  70. struct bcm2835_pinctrl {
  71. struct device *dev;
  72. void __iomem *base;
  73. int *wake_irq;
  74. /* note: locking assumes each bank will have its own unsigned long */
  75. unsigned long enabled_irq_map[BCM2835_NUM_BANKS];
  76. unsigned int irq_type[BCM2711_NUM_GPIOS];
  77. struct pinctrl_dev *pctl_dev;
  78. struct gpio_chip gpio_chip;
  79. struct pinctrl_desc pctl_desc;
  80. struct pinctrl_gpio_range gpio_range;
  81. raw_spinlock_t irq_lock[BCM2835_NUM_BANKS];
  82. /* Protect FSEL registers */
  83. spinlock_t fsel_lock;
  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. BCM2835_GPIO_PIN(54),
  143. BCM2835_GPIO_PIN(55),
  144. BCM2835_GPIO_PIN(56),
  145. BCM2835_GPIO_PIN(57),
  146. };
  147. /* one pin per group */
  148. static const char * const bcm2835_gpio_groups[] = {
  149. "gpio0",
  150. "gpio1",
  151. "gpio2",
  152. "gpio3",
  153. "gpio4",
  154. "gpio5",
  155. "gpio6",
  156. "gpio7",
  157. "gpio8",
  158. "gpio9",
  159. "gpio10",
  160. "gpio11",
  161. "gpio12",
  162. "gpio13",
  163. "gpio14",
  164. "gpio15",
  165. "gpio16",
  166. "gpio17",
  167. "gpio18",
  168. "gpio19",
  169. "gpio20",
  170. "gpio21",
  171. "gpio22",
  172. "gpio23",
  173. "gpio24",
  174. "gpio25",
  175. "gpio26",
  176. "gpio27",
  177. "gpio28",
  178. "gpio29",
  179. "gpio30",
  180. "gpio31",
  181. "gpio32",
  182. "gpio33",
  183. "gpio34",
  184. "gpio35",
  185. "gpio36",
  186. "gpio37",
  187. "gpio38",
  188. "gpio39",
  189. "gpio40",
  190. "gpio41",
  191. "gpio42",
  192. "gpio43",
  193. "gpio44",
  194. "gpio45",
  195. "gpio46",
  196. "gpio47",
  197. "gpio48",
  198. "gpio49",
  199. "gpio50",
  200. "gpio51",
  201. "gpio52",
  202. "gpio53",
  203. "gpio54",
  204. "gpio55",
  205. "gpio56",
  206. "gpio57",
  207. };
  208. enum bcm2835_fsel {
  209. BCM2835_FSEL_COUNT = 8,
  210. BCM2835_FSEL_MASK = 0x7,
  211. };
  212. static const char * const bcm2835_functions[BCM2835_FSEL_COUNT] = {
  213. [BCM2835_FSEL_GPIO_IN] = "gpio_in",
  214. [BCM2835_FSEL_GPIO_OUT] = "gpio_out",
  215. [BCM2835_FSEL_ALT0] = "alt0",
  216. [BCM2835_FSEL_ALT1] = "alt1",
  217. [BCM2835_FSEL_ALT2] = "alt2",
  218. [BCM2835_FSEL_ALT3] = "alt3",
  219. [BCM2835_FSEL_ALT4] = "alt4",
  220. [BCM2835_FSEL_ALT5] = "alt5",
  221. };
  222. static const char * const irq_type_names[] = {
  223. [IRQ_TYPE_NONE] = "none",
  224. [IRQ_TYPE_EDGE_RISING] = "edge-rising",
  225. [IRQ_TYPE_EDGE_FALLING] = "edge-falling",
  226. [IRQ_TYPE_EDGE_BOTH] = "edge-both",
  227. [IRQ_TYPE_LEVEL_HIGH] = "level-high",
  228. [IRQ_TYPE_LEVEL_LOW] = "level-low",
  229. };
  230. static inline u32 bcm2835_gpio_rd(struct bcm2835_pinctrl *pc, unsigned reg)
  231. {
  232. return readl(pc->base + reg);
  233. }
  234. static inline void bcm2835_gpio_wr(struct bcm2835_pinctrl *pc, unsigned reg,
  235. u32 val)
  236. {
  237. writel(val, pc->base + reg);
  238. }
  239. static inline int bcm2835_gpio_get_bit(struct bcm2835_pinctrl *pc, unsigned reg,
  240. unsigned bit)
  241. {
  242. reg += GPIO_REG_OFFSET(bit) * 4;
  243. return (bcm2835_gpio_rd(pc, reg) >> GPIO_REG_SHIFT(bit)) & 1;
  244. }
  245. /* note NOT a read/modify/write cycle */
  246. static inline void bcm2835_gpio_set_bit(struct bcm2835_pinctrl *pc,
  247. unsigned reg, unsigned bit)
  248. {
  249. reg += GPIO_REG_OFFSET(bit) * 4;
  250. bcm2835_gpio_wr(pc, reg, BIT(GPIO_REG_SHIFT(bit)));
  251. }
  252. static inline enum bcm2835_fsel bcm2835_pinctrl_fsel_get(
  253. struct bcm2835_pinctrl *pc, unsigned pin)
  254. {
  255. u32 val = bcm2835_gpio_rd(pc, FSEL_REG(pin));
  256. enum bcm2835_fsel status = (val >> FSEL_SHIFT(pin)) & BCM2835_FSEL_MASK;
  257. dev_dbg(pc->dev, "get %08x (%u => %s)\n", val, pin,
  258. bcm2835_functions[status]);
  259. return status;
  260. }
  261. static inline void bcm2835_pinctrl_fsel_set(
  262. struct bcm2835_pinctrl *pc, unsigned pin,
  263. enum bcm2835_fsel fsel)
  264. {
  265. u32 val;
  266. enum bcm2835_fsel cur;
  267. unsigned long flags;
  268. spin_lock_irqsave(&pc->fsel_lock, flags);
  269. val = bcm2835_gpio_rd(pc, FSEL_REG(pin));
  270. cur = (val >> FSEL_SHIFT(pin)) & BCM2835_FSEL_MASK;
  271. dev_dbg(pc->dev, "read %08x (%u => %s)\n", val, pin,
  272. bcm2835_functions[cur]);
  273. if (cur == fsel)
  274. goto unlock;
  275. if (cur != BCM2835_FSEL_GPIO_IN && fsel != BCM2835_FSEL_GPIO_IN) {
  276. /* always transition through GPIO_IN */
  277. val &= ~(BCM2835_FSEL_MASK << FSEL_SHIFT(pin));
  278. val |= BCM2835_FSEL_GPIO_IN << FSEL_SHIFT(pin);
  279. dev_dbg(pc->dev, "trans %08x (%u <= %s)\n", val, pin,
  280. bcm2835_functions[BCM2835_FSEL_GPIO_IN]);
  281. bcm2835_gpio_wr(pc, FSEL_REG(pin), val);
  282. }
  283. val &= ~(BCM2835_FSEL_MASK << FSEL_SHIFT(pin));
  284. val |= fsel << FSEL_SHIFT(pin);
  285. dev_dbg(pc->dev, "write %08x (%u <= %s)\n", val, pin,
  286. bcm2835_functions[fsel]);
  287. bcm2835_gpio_wr(pc, FSEL_REG(pin), val);
  288. unlock:
  289. spin_unlock_irqrestore(&pc->fsel_lock, flags);
  290. }
  291. static int bcm2835_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  292. {
  293. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  294. bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
  295. return 0;
  296. }
  297. static int bcm2835_gpio_get(struct gpio_chip *chip, unsigned offset)
  298. {
  299. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  300. return bcm2835_gpio_get_bit(pc, GPLEV0, offset);
  301. }
  302. static int bcm2835_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  303. {
  304. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  305. enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
  306. /* Alternative function doesn't clearly provide a direction */
  307. if (fsel > BCM2835_FSEL_GPIO_OUT)
  308. return -EINVAL;
  309. if (fsel == BCM2835_FSEL_GPIO_IN)
  310. return GPIO_LINE_DIRECTION_IN;
  311. return GPIO_LINE_DIRECTION_OUT;
  312. }
  313. static void bcm2835_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  314. {
  315. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  316. bcm2835_gpio_set_bit(pc, value ? GPSET0 : GPCLR0, offset);
  317. }
  318. static int bcm2835_gpio_direction_output(struct gpio_chip *chip,
  319. unsigned offset, int value)
  320. {
  321. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  322. bcm2835_gpio_set_bit(pc, value ? GPSET0 : GPCLR0, offset);
  323. bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_OUT);
  324. return 0;
  325. }
  326. static int bcm2835_of_gpio_ranges_fallback(struct gpio_chip *gc,
  327. struct device_node *np)
  328. {
  329. struct pinctrl_dev *pctldev = of_pinctrl_get(np);
  330. if (!pctldev)
  331. return 0;
  332. return gpiochip_add_pin_range(gc, pinctrl_dev_get_devname(pctldev), 0, 0,
  333. gc->ngpio);
  334. }
  335. static const struct gpio_chip bcm2835_gpio_chip = {
  336. .label = MODULE_NAME,
  337. .owner = THIS_MODULE,
  338. .request = gpiochip_generic_request,
  339. .free = gpiochip_generic_free,
  340. .direction_input = bcm2835_gpio_direction_input,
  341. .direction_output = bcm2835_gpio_direction_output,
  342. .get_direction = bcm2835_gpio_get_direction,
  343. .get = bcm2835_gpio_get,
  344. .set = bcm2835_gpio_set,
  345. .set_config = gpiochip_generic_config,
  346. .base = -1,
  347. .ngpio = BCM2835_NUM_GPIOS,
  348. .can_sleep = false,
  349. .of_gpio_ranges_fallback = bcm2835_of_gpio_ranges_fallback,
  350. };
  351. static const struct gpio_chip bcm2711_gpio_chip = {
  352. .label = "pinctrl-bcm2711",
  353. .owner = THIS_MODULE,
  354. .request = gpiochip_generic_request,
  355. .free = gpiochip_generic_free,
  356. .direction_input = bcm2835_gpio_direction_input,
  357. .direction_output = bcm2835_gpio_direction_output,
  358. .get_direction = bcm2835_gpio_get_direction,
  359. .get = bcm2835_gpio_get,
  360. .set = bcm2835_gpio_set,
  361. .set_config = gpiochip_generic_config,
  362. .base = -1,
  363. .ngpio = BCM2711_NUM_GPIOS,
  364. .can_sleep = false,
  365. .of_gpio_ranges_fallback = bcm2835_of_gpio_ranges_fallback,
  366. };
  367. static void bcm2835_gpio_irq_handle_bank(struct bcm2835_pinctrl *pc,
  368. unsigned int bank, u32 mask)
  369. {
  370. unsigned long events;
  371. unsigned offset;
  372. unsigned gpio;
  373. events = bcm2835_gpio_rd(pc, GPEDS0 + bank * 4);
  374. events &= mask;
  375. events &= pc->enabled_irq_map[bank];
  376. for_each_set_bit(offset, &events, 32) {
  377. gpio = (32 * bank) + offset;
  378. generic_handle_domain_irq(pc->gpio_chip.irq.domain,
  379. gpio);
  380. }
  381. }
  382. static void bcm2835_gpio_irq_handler(struct irq_desc *desc)
  383. {
  384. struct gpio_chip *chip = irq_desc_get_handler_data(desc);
  385. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  386. struct irq_chip *host_chip = irq_desc_get_chip(desc);
  387. int irq = irq_desc_get_irq(desc);
  388. int group = 0;
  389. int i;
  390. for (i = 0; i < BCM2835_NUM_IRQS; i++) {
  391. if (chip->irq.parents[i] == irq) {
  392. group = i;
  393. break;
  394. }
  395. }
  396. /* This should not happen, every IRQ has a bank */
  397. BUG_ON(i == BCM2835_NUM_IRQS);
  398. chained_irq_enter(host_chip, desc);
  399. switch (group) {
  400. case 0: /* IRQ0 covers GPIOs 0-27 */
  401. bcm2835_gpio_irq_handle_bank(pc, 0, 0x0fffffff);
  402. break;
  403. case 1: /* IRQ1 covers GPIOs 28-45 */
  404. bcm2835_gpio_irq_handle_bank(pc, 0, 0xf0000000);
  405. bcm2835_gpio_irq_handle_bank(pc, 1, 0x00003fff);
  406. break;
  407. case 2: /* IRQ2 covers GPIOs 46-57 */
  408. bcm2835_gpio_irq_handle_bank(pc, 1, 0x003fc000);
  409. break;
  410. }
  411. chained_irq_exit(host_chip, desc);
  412. }
  413. static irqreturn_t bcm2835_gpio_wake_irq_handler(int irq, void *dev_id)
  414. {
  415. return IRQ_HANDLED;
  416. }
  417. static inline void __bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
  418. unsigned reg, unsigned offset, bool enable)
  419. {
  420. u32 value;
  421. reg += GPIO_REG_OFFSET(offset) * 4;
  422. value = bcm2835_gpio_rd(pc, reg);
  423. if (enable)
  424. value |= BIT(GPIO_REG_SHIFT(offset));
  425. else
  426. value &= ~(BIT(GPIO_REG_SHIFT(offset)));
  427. bcm2835_gpio_wr(pc, reg, value);
  428. }
  429. /* fast path for IRQ handler */
  430. static void bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
  431. unsigned offset, bool enable)
  432. {
  433. switch (pc->irq_type[offset]) {
  434. case IRQ_TYPE_EDGE_RISING:
  435. __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
  436. break;
  437. case IRQ_TYPE_EDGE_FALLING:
  438. __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
  439. break;
  440. case IRQ_TYPE_EDGE_BOTH:
  441. __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
  442. __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
  443. break;
  444. case IRQ_TYPE_LEVEL_HIGH:
  445. __bcm2835_gpio_irq_config(pc, GPHEN0, offset, enable);
  446. break;
  447. case IRQ_TYPE_LEVEL_LOW:
  448. __bcm2835_gpio_irq_config(pc, GPLEN0, offset, enable);
  449. break;
  450. }
  451. }
  452. static void bcm2835_gpio_irq_unmask(struct irq_data *data)
  453. {
  454. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  455. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  456. unsigned gpio = irqd_to_hwirq(data);
  457. unsigned offset = GPIO_REG_SHIFT(gpio);
  458. unsigned bank = GPIO_REG_OFFSET(gpio);
  459. unsigned long flags;
  460. gpiochip_enable_irq(chip, gpio);
  461. raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
  462. set_bit(offset, &pc->enabled_irq_map[bank]);
  463. bcm2835_gpio_irq_config(pc, gpio, true);
  464. raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  465. }
  466. static void bcm2835_gpio_irq_mask(struct irq_data *data)
  467. {
  468. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  469. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  470. unsigned gpio = irqd_to_hwirq(data);
  471. unsigned offset = GPIO_REG_SHIFT(gpio);
  472. unsigned bank = GPIO_REG_OFFSET(gpio);
  473. unsigned long flags;
  474. raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
  475. bcm2835_gpio_irq_config(pc, gpio, false);
  476. /* Clear events that were latched prior to clearing event sources */
  477. bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
  478. clear_bit(offset, &pc->enabled_irq_map[bank]);
  479. raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  480. gpiochip_disable_irq(chip, gpio);
  481. }
  482. static int __bcm2835_gpio_irq_set_type_disabled(struct bcm2835_pinctrl *pc,
  483. unsigned offset, unsigned int type)
  484. {
  485. switch (type) {
  486. case IRQ_TYPE_NONE:
  487. case IRQ_TYPE_EDGE_RISING:
  488. case IRQ_TYPE_EDGE_FALLING:
  489. case IRQ_TYPE_EDGE_BOTH:
  490. case IRQ_TYPE_LEVEL_HIGH:
  491. case IRQ_TYPE_LEVEL_LOW:
  492. pc->irq_type[offset] = type;
  493. break;
  494. default:
  495. return -EINVAL;
  496. }
  497. return 0;
  498. }
  499. /* slower path for reconfiguring IRQ type */
  500. static int __bcm2835_gpio_irq_set_type_enabled(struct bcm2835_pinctrl *pc,
  501. unsigned offset, unsigned int type)
  502. {
  503. switch (type) {
  504. case IRQ_TYPE_NONE:
  505. if (pc->irq_type[offset] != type) {
  506. bcm2835_gpio_irq_config(pc, offset, false);
  507. pc->irq_type[offset] = type;
  508. }
  509. break;
  510. case IRQ_TYPE_EDGE_RISING:
  511. if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
  512. /* RISING already enabled, disable FALLING */
  513. pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
  514. bcm2835_gpio_irq_config(pc, offset, false);
  515. pc->irq_type[offset] = type;
  516. } else if (pc->irq_type[offset] != type) {
  517. bcm2835_gpio_irq_config(pc, offset, false);
  518. pc->irq_type[offset] = type;
  519. bcm2835_gpio_irq_config(pc, offset, true);
  520. }
  521. break;
  522. case IRQ_TYPE_EDGE_FALLING:
  523. if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
  524. /* FALLING already enabled, disable RISING */
  525. pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
  526. bcm2835_gpio_irq_config(pc, offset, false);
  527. pc->irq_type[offset] = type;
  528. } else if (pc->irq_type[offset] != type) {
  529. bcm2835_gpio_irq_config(pc, offset, false);
  530. pc->irq_type[offset] = type;
  531. bcm2835_gpio_irq_config(pc, offset, true);
  532. }
  533. break;
  534. case IRQ_TYPE_EDGE_BOTH:
  535. if (pc->irq_type[offset] == IRQ_TYPE_EDGE_RISING) {
  536. /* RISING already enabled, enable FALLING too */
  537. pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
  538. bcm2835_gpio_irq_config(pc, offset, true);
  539. pc->irq_type[offset] = type;
  540. } else if (pc->irq_type[offset] == IRQ_TYPE_EDGE_FALLING) {
  541. /* FALLING already enabled, enable RISING too */
  542. pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
  543. bcm2835_gpio_irq_config(pc, offset, true);
  544. pc->irq_type[offset] = type;
  545. } else if (pc->irq_type[offset] != type) {
  546. bcm2835_gpio_irq_config(pc, offset, false);
  547. pc->irq_type[offset] = type;
  548. bcm2835_gpio_irq_config(pc, offset, true);
  549. }
  550. break;
  551. case IRQ_TYPE_LEVEL_HIGH:
  552. case IRQ_TYPE_LEVEL_LOW:
  553. if (pc->irq_type[offset] != type) {
  554. bcm2835_gpio_irq_config(pc, offset, false);
  555. pc->irq_type[offset] = type;
  556. bcm2835_gpio_irq_config(pc, offset, true);
  557. }
  558. break;
  559. default:
  560. return -EINVAL;
  561. }
  562. return 0;
  563. }
  564. static int bcm2835_gpio_irq_set_type(struct irq_data *data, unsigned int type)
  565. {
  566. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  567. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  568. unsigned gpio = irqd_to_hwirq(data);
  569. unsigned offset = GPIO_REG_SHIFT(gpio);
  570. unsigned bank = GPIO_REG_OFFSET(gpio);
  571. unsigned long flags;
  572. int ret;
  573. raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
  574. if (test_bit(offset, &pc->enabled_irq_map[bank]))
  575. ret = __bcm2835_gpio_irq_set_type_enabled(pc, gpio, type);
  576. else
  577. ret = __bcm2835_gpio_irq_set_type_disabled(pc, gpio, type);
  578. if (type & IRQ_TYPE_EDGE_BOTH)
  579. irq_set_handler_locked(data, handle_edge_irq);
  580. else
  581. irq_set_handler_locked(data, handle_level_irq);
  582. raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  583. return ret;
  584. }
  585. static void bcm2835_gpio_irq_ack(struct irq_data *data)
  586. {
  587. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  588. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  589. unsigned gpio = irqd_to_hwirq(data);
  590. bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
  591. }
  592. static int bcm2835_gpio_irq_set_wake(struct irq_data *data, unsigned int on)
  593. {
  594. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  595. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  596. unsigned gpio = irqd_to_hwirq(data);
  597. unsigned int irqgroup;
  598. int ret = -EINVAL;
  599. if (!pc->wake_irq)
  600. return ret;
  601. if (gpio <= 27)
  602. irqgroup = 0;
  603. else if (gpio >= 28 && gpio <= 45)
  604. irqgroup = 1;
  605. else if (gpio >= 46 && gpio <= 57)
  606. irqgroup = 2;
  607. else
  608. return ret;
  609. if (on)
  610. ret = enable_irq_wake(pc->wake_irq[irqgroup]);
  611. else
  612. ret = disable_irq_wake(pc->wake_irq[irqgroup]);
  613. return ret;
  614. }
  615. static const struct irq_chip bcm2835_gpio_irq_chip = {
  616. .name = MODULE_NAME,
  617. .irq_set_type = bcm2835_gpio_irq_set_type,
  618. .irq_ack = bcm2835_gpio_irq_ack,
  619. .irq_mask = bcm2835_gpio_irq_mask,
  620. .irq_unmask = bcm2835_gpio_irq_unmask,
  621. .irq_set_wake = bcm2835_gpio_irq_set_wake,
  622. .flags = (IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE),
  623. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  624. };
  625. static int bcm2835_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  626. {
  627. return BCM2835_NUM_GPIOS;
  628. }
  629. static const char *bcm2835_pctl_get_group_name(struct pinctrl_dev *pctldev,
  630. unsigned selector)
  631. {
  632. return bcm2835_gpio_groups[selector];
  633. }
  634. static int bcm2835_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  635. unsigned selector,
  636. const unsigned **pins,
  637. unsigned *num_pins)
  638. {
  639. *pins = &bcm2835_gpio_pins[selector].number;
  640. *num_pins = 1;
  641. return 0;
  642. }
  643. static void bcm2835_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
  644. struct seq_file *s,
  645. unsigned offset)
  646. {
  647. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  648. struct gpio_chip *chip = &pc->gpio_chip;
  649. enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
  650. const char *fname = bcm2835_functions[fsel];
  651. int value = bcm2835_gpio_get_bit(pc, GPLEV0, offset);
  652. int irq = irq_find_mapping(chip->irq.domain, offset);
  653. seq_printf(s, "function %s in %s; irq %d (%s)",
  654. fname, value ? "hi" : "lo",
  655. irq, irq_type_names[pc->irq_type[offset]]);
  656. }
  657. static void bcm2835_pctl_dt_free_map(struct pinctrl_dev *pctldev,
  658. struct pinctrl_map *maps, unsigned num_maps)
  659. {
  660. int i;
  661. for (i = 0; i < num_maps; i++)
  662. if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  663. kfree(maps[i].data.configs.configs);
  664. kfree(maps);
  665. }
  666. static int bcm2835_pctl_dt_node_to_map_func(struct bcm2835_pinctrl *pc,
  667. struct device_node *np, u32 pin, u32 fnum,
  668. struct pinctrl_map **maps)
  669. {
  670. struct pinctrl_map *map = *maps;
  671. if (fnum >= ARRAY_SIZE(bcm2835_functions)) {
  672. dev_err(pc->dev, "%pOF: invalid brcm,function %d\n", np, fnum);
  673. return -EINVAL;
  674. }
  675. map->type = PIN_MAP_TYPE_MUX_GROUP;
  676. map->data.mux.group = bcm2835_gpio_groups[pin];
  677. map->data.mux.function = bcm2835_functions[fnum];
  678. (*maps)++;
  679. return 0;
  680. }
  681. static int bcm2835_pctl_dt_node_to_map_pull(struct bcm2835_pinctrl *pc,
  682. struct device_node *np, u32 pin, u32 pull,
  683. struct pinctrl_map **maps)
  684. {
  685. struct pinctrl_map *map = *maps;
  686. unsigned long *configs;
  687. if (pull > 2) {
  688. dev_err(pc->dev, "%pOF: invalid brcm,pull %d\n", np, pull);
  689. return -EINVAL;
  690. }
  691. configs = kzalloc(sizeof(*configs), GFP_KERNEL);
  692. if (!configs)
  693. return -ENOMEM;
  694. configs[0] = pinconf_to_config_packed(BCM2835_PINCONF_PARAM_PULL, pull);
  695. map->type = PIN_MAP_TYPE_CONFIGS_PIN;
  696. map->data.configs.group_or_pin = bcm2835_gpio_pins[pin].name;
  697. map->data.configs.configs = configs;
  698. map->data.configs.num_configs = 1;
  699. (*maps)++;
  700. return 0;
  701. }
  702. static int bcm2835_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  703. struct device_node *np,
  704. struct pinctrl_map **map, unsigned int *num_maps)
  705. {
  706. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  707. struct property *pins, *funcs, *pulls;
  708. int num_pins, num_funcs, num_pulls, maps_per_pin;
  709. struct pinctrl_map *maps, *cur_map;
  710. int i, err;
  711. u32 pin, func, pull;
  712. /* Check for generic binding in this node */
  713. err = pinconf_generic_dt_node_to_map_all(pctldev, np, map, num_maps);
  714. if (err || *num_maps)
  715. return err;
  716. /* Generic binding did not find anything continue with legacy parse */
  717. pins = of_find_property(np, "brcm,pins", NULL);
  718. if (!pins) {
  719. dev_err(pc->dev, "%pOF: missing brcm,pins property\n", np);
  720. return -EINVAL;
  721. }
  722. funcs = of_find_property(np, "brcm,function", NULL);
  723. pulls = of_find_property(np, "brcm,pull", NULL);
  724. if (!funcs && !pulls) {
  725. dev_err(pc->dev,
  726. "%pOF: neither brcm,function nor brcm,pull specified\n",
  727. np);
  728. return -EINVAL;
  729. }
  730. num_pins = pins->length / 4;
  731. num_funcs = funcs ? (funcs->length / 4) : 0;
  732. num_pulls = pulls ? (pulls->length / 4) : 0;
  733. if (num_funcs > 1 && num_funcs != num_pins) {
  734. dev_err(pc->dev,
  735. "%pOF: brcm,function must have 1 or %d entries\n",
  736. np, num_pins);
  737. return -EINVAL;
  738. }
  739. if (num_pulls > 1 && num_pulls != num_pins) {
  740. dev_err(pc->dev,
  741. "%pOF: brcm,pull must have 1 or %d entries\n",
  742. np, num_pins);
  743. return -EINVAL;
  744. }
  745. maps_per_pin = 0;
  746. if (num_funcs)
  747. maps_per_pin++;
  748. if (num_pulls)
  749. maps_per_pin++;
  750. cur_map = maps = kcalloc(num_pins * maps_per_pin, sizeof(*maps),
  751. GFP_KERNEL);
  752. if (!maps)
  753. return -ENOMEM;
  754. for (i = 0; i < num_pins; i++) {
  755. err = of_property_read_u32_index(np, "brcm,pins", i, &pin);
  756. if (err)
  757. goto out;
  758. if (pin >= pc->pctl_desc.npins) {
  759. dev_err(pc->dev, "%pOF: invalid brcm,pins value %d\n",
  760. np, pin);
  761. err = -EINVAL;
  762. goto out;
  763. }
  764. if (num_funcs) {
  765. err = of_property_read_u32_index(np, "brcm,function",
  766. (num_funcs > 1) ? i : 0, &func);
  767. if (err)
  768. goto out;
  769. err = bcm2835_pctl_dt_node_to_map_func(pc, np, pin,
  770. func, &cur_map);
  771. if (err)
  772. goto out;
  773. }
  774. if (num_pulls) {
  775. err = of_property_read_u32_index(np, "brcm,pull",
  776. (num_pulls > 1) ? i : 0, &pull);
  777. if (err)
  778. goto out;
  779. err = bcm2835_pctl_dt_node_to_map_pull(pc, np, pin,
  780. pull, &cur_map);
  781. if (err)
  782. goto out;
  783. }
  784. }
  785. *map = maps;
  786. *num_maps = num_pins * maps_per_pin;
  787. return 0;
  788. out:
  789. bcm2835_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
  790. return err;
  791. }
  792. static const struct pinctrl_ops bcm2835_pctl_ops = {
  793. .get_groups_count = bcm2835_pctl_get_groups_count,
  794. .get_group_name = bcm2835_pctl_get_group_name,
  795. .get_group_pins = bcm2835_pctl_get_group_pins,
  796. .pin_dbg_show = bcm2835_pctl_pin_dbg_show,
  797. .dt_node_to_map = bcm2835_pctl_dt_node_to_map,
  798. .dt_free_map = bcm2835_pctl_dt_free_map,
  799. };
  800. static int bcm2835_pmx_free(struct pinctrl_dev *pctldev,
  801. unsigned offset)
  802. {
  803. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  804. /* disable by setting to GPIO_IN */
  805. bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
  806. return 0;
  807. }
  808. static int bcm2835_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  809. {
  810. return BCM2835_FSEL_COUNT;
  811. }
  812. static const char *bcm2835_pmx_get_function_name(struct pinctrl_dev *pctldev,
  813. unsigned selector)
  814. {
  815. return bcm2835_functions[selector];
  816. }
  817. static int bcm2835_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  818. unsigned selector,
  819. const char * const **groups,
  820. unsigned * const num_groups)
  821. {
  822. /* every pin can do every function */
  823. *groups = bcm2835_gpio_groups;
  824. *num_groups = BCM2835_NUM_GPIOS;
  825. return 0;
  826. }
  827. static int bcm2835_pmx_set(struct pinctrl_dev *pctldev,
  828. unsigned func_selector,
  829. unsigned group_selector)
  830. {
  831. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  832. bcm2835_pinctrl_fsel_set(pc, group_selector, func_selector);
  833. return 0;
  834. }
  835. static void bcm2835_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
  836. struct pinctrl_gpio_range *range,
  837. unsigned offset)
  838. {
  839. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  840. /* disable by setting to GPIO_IN */
  841. bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
  842. }
  843. static int bcm2835_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  844. struct pinctrl_gpio_range *range,
  845. unsigned offset,
  846. bool input)
  847. {
  848. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  849. enum bcm2835_fsel fsel = input ?
  850. BCM2835_FSEL_GPIO_IN : BCM2835_FSEL_GPIO_OUT;
  851. bcm2835_pinctrl_fsel_set(pc, offset, fsel);
  852. return 0;
  853. }
  854. static const struct pinmux_ops bcm2835_pmx_ops = {
  855. .free = bcm2835_pmx_free,
  856. .get_functions_count = bcm2835_pmx_get_functions_count,
  857. .get_function_name = bcm2835_pmx_get_function_name,
  858. .get_function_groups = bcm2835_pmx_get_function_groups,
  859. .set_mux = bcm2835_pmx_set,
  860. .gpio_disable_free = bcm2835_pmx_gpio_disable_free,
  861. .gpio_set_direction = bcm2835_pmx_gpio_set_direction,
  862. };
  863. static int bcm2835_pinconf_get(struct pinctrl_dev *pctldev,
  864. unsigned pin, unsigned long *config)
  865. {
  866. /* No way to read back config in HW */
  867. return -ENOTSUPP;
  868. }
  869. static void bcm2835_pull_config_set(struct bcm2835_pinctrl *pc,
  870. unsigned int pin, unsigned int arg)
  871. {
  872. u32 off, bit;
  873. off = GPIO_REG_OFFSET(pin);
  874. bit = GPIO_REG_SHIFT(pin);
  875. bcm2835_gpio_wr(pc, GPPUD, arg & 3);
  876. /*
  877. * BCM2835 datasheet say to wait 150 cycles, but not of what.
  878. * But the VideoCore firmware delay for this operation
  879. * based nearly on the same amount of VPU cycles and this clock
  880. * runs at 250 MHz.
  881. */
  882. udelay(1);
  883. bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), BIT(bit));
  884. udelay(1);
  885. bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), 0);
  886. }
  887. static int bcm2835_pinconf_set(struct pinctrl_dev *pctldev,
  888. unsigned int pin, unsigned long *configs,
  889. unsigned int num_configs)
  890. {
  891. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  892. u32 param, arg;
  893. int i;
  894. for (i = 0; i < num_configs; i++) {
  895. param = pinconf_to_config_param(configs[i]);
  896. arg = pinconf_to_config_argument(configs[i]);
  897. switch (param) {
  898. /* Set legacy brcm,pull */
  899. case BCM2835_PINCONF_PARAM_PULL:
  900. bcm2835_pull_config_set(pc, pin, arg);
  901. break;
  902. /* Set pull generic bindings */
  903. case PIN_CONFIG_BIAS_DISABLE:
  904. bcm2835_pull_config_set(pc, pin, BCM2835_PUD_OFF);
  905. break;
  906. case PIN_CONFIG_BIAS_PULL_DOWN:
  907. bcm2835_pull_config_set(pc, pin, BCM2835_PUD_DOWN);
  908. break;
  909. case PIN_CONFIG_BIAS_PULL_UP:
  910. bcm2835_pull_config_set(pc, pin, BCM2835_PUD_UP);
  911. break;
  912. /* Set output-high or output-low */
  913. case PIN_CONFIG_OUTPUT:
  914. bcm2835_gpio_set_bit(pc, arg ? GPSET0 : GPCLR0, pin);
  915. break;
  916. default:
  917. return -ENOTSUPP;
  918. } /* switch param type */
  919. } /* for each config */
  920. return 0;
  921. }
  922. static const struct pinconf_ops bcm2835_pinconf_ops = {
  923. .is_generic = true,
  924. .pin_config_get = bcm2835_pinconf_get,
  925. .pin_config_set = bcm2835_pinconf_set,
  926. };
  927. static void bcm2711_pull_config_set(struct bcm2835_pinctrl *pc,
  928. unsigned int pin, unsigned int arg)
  929. {
  930. u32 shifter;
  931. u32 value;
  932. u32 off;
  933. off = PUD_2711_REG_OFFSET(pin);
  934. shifter = PUD_2711_REG_SHIFT(pin);
  935. value = bcm2835_gpio_rd(pc, GP_GPIO_PUP_PDN_CNTRL_REG0 + (off * 4));
  936. value &= ~(PUD_2711_MASK << shifter);
  937. value |= (arg << shifter);
  938. bcm2835_gpio_wr(pc, GP_GPIO_PUP_PDN_CNTRL_REG0 + (off * 4), value);
  939. }
  940. static int bcm2711_pinconf_set(struct pinctrl_dev *pctldev,
  941. unsigned int pin, unsigned long *configs,
  942. unsigned int num_configs)
  943. {
  944. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  945. u32 param, arg;
  946. int i;
  947. for (i = 0; i < num_configs; i++) {
  948. param = pinconf_to_config_param(configs[i]);
  949. arg = pinconf_to_config_argument(configs[i]);
  950. switch (param) {
  951. /* convert legacy brcm,pull */
  952. case BCM2835_PINCONF_PARAM_PULL:
  953. if (arg == BCM2835_PUD_UP)
  954. arg = BCM2711_PULL_UP;
  955. else if (arg == BCM2835_PUD_DOWN)
  956. arg = BCM2711_PULL_DOWN;
  957. else
  958. arg = BCM2711_PULL_NONE;
  959. bcm2711_pull_config_set(pc, pin, arg);
  960. break;
  961. /* Set pull generic bindings */
  962. case PIN_CONFIG_BIAS_DISABLE:
  963. bcm2711_pull_config_set(pc, pin, BCM2711_PULL_NONE);
  964. break;
  965. case PIN_CONFIG_BIAS_PULL_DOWN:
  966. bcm2711_pull_config_set(pc, pin, BCM2711_PULL_DOWN);
  967. break;
  968. case PIN_CONFIG_BIAS_PULL_UP:
  969. bcm2711_pull_config_set(pc, pin, BCM2711_PULL_UP);
  970. break;
  971. /* Set output-high or output-low */
  972. case PIN_CONFIG_OUTPUT:
  973. bcm2835_gpio_set_bit(pc, arg ? GPSET0 : GPCLR0, pin);
  974. break;
  975. default:
  976. return -ENOTSUPP;
  977. }
  978. } /* for each config */
  979. return 0;
  980. }
  981. static const struct pinconf_ops bcm2711_pinconf_ops = {
  982. .is_generic = true,
  983. .pin_config_get = bcm2835_pinconf_get,
  984. .pin_config_set = bcm2711_pinconf_set,
  985. };
  986. static const struct pinctrl_desc bcm2835_pinctrl_desc = {
  987. .name = MODULE_NAME,
  988. .pins = bcm2835_gpio_pins,
  989. .npins = BCM2835_NUM_GPIOS,
  990. .pctlops = &bcm2835_pctl_ops,
  991. .pmxops = &bcm2835_pmx_ops,
  992. .confops = &bcm2835_pinconf_ops,
  993. .owner = THIS_MODULE,
  994. };
  995. static const struct pinctrl_desc bcm2711_pinctrl_desc = {
  996. .name = "pinctrl-bcm2711",
  997. .pins = bcm2835_gpio_pins,
  998. .npins = BCM2711_NUM_GPIOS,
  999. .pctlops = &bcm2835_pctl_ops,
  1000. .pmxops = &bcm2835_pmx_ops,
  1001. .confops = &bcm2711_pinconf_ops,
  1002. .owner = THIS_MODULE,
  1003. };
  1004. static const struct pinctrl_gpio_range bcm2835_pinctrl_gpio_range = {
  1005. .name = MODULE_NAME,
  1006. .npins = BCM2835_NUM_GPIOS,
  1007. };
  1008. static const struct pinctrl_gpio_range bcm2711_pinctrl_gpio_range = {
  1009. .name = "pinctrl-bcm2711",
  1010. .npins = BCM2711_NUM_GPIOS,
  1011. };
  1012. struct bcm_plat_data {
  1013. const struct gpio_chip *gpio_chip;
  1014. const struct pinctrl_desc *pctl_desc;
  1015. const struct pinctrl_gpio_range *gpio_range;
  1016. };
  1017. static const struct bcm_plat_data bcm2835_plat_data = {
  1018. .gpio_chip = &bcm2835_gpio_chip,
  1019. .pctl_desc = &bcm2835_pinctrl_desc,
  1020. .gpio_range = &bcm2835_pinctrl_gpio_range,
  1021. };
  1022. static const struct bcm_plat_data bcm2711_plat_data = {
  1023. .gpio_chip = &bcm2711_gpio_chip,
  1024. .pctl_desc = &bcm2711_pinctrl_desc,
  1025. .gpio_range = &bcm2711_pinctrl_gpio_range,
  1026. };
  1027. static const struct of_device_id bcm2835_pinctrl_match[] = {
  1028. {
  1029. .compatible = "brcm,bcm2835-gpio",
  1030. .data = &bcm2835_plat_data,
  1031. },
  1032. {
  1033. .compatible = "brcm,bcm2711-gpio",
  1034. .data = &bcm2711_plat_data,
  1035. },
  1036. {
  1037. .compatible = "brcm,bcm7211-gpio",
  1038. .data = &bcm2711_plat_data,
  1039. },
  1040. {}
  1041. };
  1042. static int bcm2835_pinctrl_probe(struct platform_device *pdev)
  1043. {
  1044. struct device *dev = &pdev->dev;
  1045. struct device_node *np = dev->of_node;
  1046. const struct bcm_plat_data *pdata;
  1047. struct bcm2835_pinctrl *pc;
  1048. struct gpio_irq_chip *girq;
  1049. struct resource iomem;
  1050. int err, i;
  1051. const struct of_device_id *match;
  1052. int is_7211 = 0;
  1053. BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_pins) != BCM2711_NUM_GPIOS);
  1054. BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_groups) != BCM2711_NUM_GPIOS);
  1055. pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
  1056. if (!pc)
  1057. return -ENOMEM;
  1058. platform_set_drvdata(pdev, pc);
  1059. pc->dev = dev;
  1060. err = of_address_to_resource(np, 0, &iomem);
  1061. if (err) {
  1062. dev_err(dev, "could not get IO memory\n");
  1063. return err;
  1064. }
  1065. pc->base = devm_ioremap_resource(dev, &iomem);
  1066. if (IS_ERR(pc->base))
  1067. return PTR_ERR(pc->base);
  1068. match = of_match_node(bcm2835_pinctrl_match, pdev->dev.of_node);
  1069. if (!match)
  1070. return -EINVAL;
  1071. pdata = match->data;
  1072. is_7211 = of_device_is_compatible(np, "brcm,bcm7211-gpio");
  1073. pc->gpio_chip = *pdata->gpio_chip;
  1074. pc->gpio_chip.parent = dev;
  1075. spin_lock_init(&pc->fsel_lock);
  1076. for (i = 0; i < BCM2835_NUM_BANKS; i++) {
  1077. unsigned long events;
  1078. unsigned offset;
  1079. /* clear event detection flags */
  1080. bcm2835_gpio_wr(pc, GPREN0 + i * 4, 0);
  1081. bcm2835_gpio_wr(pc, GPFEN0 + i * 4, 0);
  1082. bcm2835_gpio_wr(pc, GPHEN0 + i * 4, 0);
  1083. bcm2835_gpio_wr(pc, GPLEN0 + i * 4, 0);
  1084. bcm2835_gpio_wr(pc, GPAREN0 + i * 4, 0);
  1085. bcm2835_gpio_wr(pc, GPAFEN0 + i * 4, 0);
  1086. /* clear all the events */
  1087. events = bcm2835_gpio_rd(pc, GPEDS0 + i * 4);
  1088. for_each_set_bit(offset, &events, 32)
  1089. bcm2835_gpio_wr(pc, GPEDS0 + i * 4, BIT(offset));
  1090. raw_spin_lock_init(&pc->irq_lock[i]);
  1091. }
  1092. pc->pctl_desc = *pdata->pctl_desc;
  1093. pc->pctl_dev = devm_pinctrl_register(dev, &pc->pctl_desc, pc);
  1094. if (IS_ERR(pc->pctl_dev)) {
  1095. gpiochip_remove(&pc->gpio_chip);
  1096. return PTR_ERR(pc->pctl_dev);
  1097. }
  1098. pc->gpio_range = *pdata->gpio_range;
  1099. pc->gpio_range.base = pc->gpio_chip.base;
  1100. pc->gpio_range.gc = &pc->gpio_chip;
  1101. pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
  1102. girq = &pc->gpio_chip.irq;
  1103. gpio_irq_chip_set_chip(girq, &bcm2835_gpio_irq_chip);
  1104. girq->parent_handler = bcm2835_gpio_irq_handler;
  1105. girq->num_parents = BCM2835_NUM_IRQS;
  1106. girq->parents = devm_kcalloc(dev, BCM2835_NUM_IRQS,
  1107. sizeof(*girq->parents),
  1108. GFP_KERNEL);
  1109. if (!girq->parents) {
  1110. err = -ENOMEM;
  1111. goto out_remove;
  1112. }
  1113. if (is_7211) {
  1114. pc->wake_irq = devm_kcalloc(dev, BCM2835_NUM_IRQS,
  1115. sizeof(*pc->wake_irq),
  1116. GFP_KERNEL);
  1117. if (!pc->wake_irq) {
  1118. err = -ENOMEM;
  1119. goto out_remove;
  1120. }
  1121. }
  1122. /*
  1123. * Use the same handler for all groups: this is necessary
  1124. * since we use one gpiochip to cover all lines - the
  1125. * irq handler then needs to figure out which group and
  1126. * bank that was firing the IRQ and look up the per-group
  1127. * and bank data.
  1128. */
  1129. for (i = 0; i < BCM2835_NUM_IRQS; i++) {
  1130. int len;
  1131. char *name;
  1132. girq->parents[i] = irq_of_parse_and_map(np, i);
  1133. if (!is_7211) {
  1134. if (!girq->parents[i]) {
  1135. girq->num_parents = i;
  1136. break;
  1137. }
  1138. continue;
  1139. }
  1140. /* Skip over the all banks interrupts */
  1141. pc->wake_irq[i] = irq_of_parse_and_map(np, i +
  1142. BCM2835_NUM_IRQS + 1);
  1143. len = strlen(dev_name(pc->dev)) + 16;
  1144. name = devm_kzalloc(pc->dev, len, GFP_KERNEL);
  1145. if (!name) {
  1146. err = -ENOMEM;
  1147. goto out_remove;
  1148. }
  1149. snprintf(name, len, "%s:bank%d", dev_name(pc->dev), i);
  1150. /* These are optional interrupts */
  1151. err = devm_request_irq(dev, pc->wake_irq[i],
  1152. bcm2835_gpio_wake_irq_handler,
  1153. IRQF_SHARED, name, pc);
  1154. if (err)
  1155. dev_warn(dev, "unable to request wake IRQ %d\n",
  1156. pc->wake_irq[i]);
  1157. }
  1158. girq->default_type = IRQ_TYPE_NONE;
  1159. girq->handler = handle_level_irq;
  1160. err = gpiochip_add_data(&pc->gpio_chip, pc);
  1161. if (err) {
  1162. dev_err(dev, "could not add GPIO chip\n");
  1163. goto out_remove;
  1164. }
  1165. return 0;
  1166. out_remove:
  1167. pinctrl_remove_gpio_range(pc->pctl_dev, &pc->gpio_range);
  1168. return err;
  1169. }
  1170. static struct platform_driver bcm2835_pinctrl_driver = {
  1171. .probe = bcm2835_pinctrl_probe,
  1172. .driver = {
  1173. .name = MODULE_NAME,
  1174. .of_match_table = bcm2835_pinctrl_match,
  1175. .suppress_bind_attrs = true,
  1176. },
  1177. };
  1178. module_platform_driver(bcm2835_pinctrl_driver);
  1179. MODULE_AUTHOR("Chris Boot");
  1180. MODULE_AUTHOR("Simon Arlott");
  1181. MODULE_AUTHOR("Stephen Warren");
  1182. MODULE_DESCRIPTION("Broadcom BCM2835/2711 pinctrl and GPIO driver");
  1183. MODULE_LICENSE("GPL");