pinctrl-bcm2835.c 37 KB

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