pinctrl-bcm2835.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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. if (fsel == BCM2835_FSEL_GPIO_OUT)
  311. return GPIO_LINE_DIRECTION_OUT;
  312. /*
  313. * Alternative function doesn't clearly provide a direction. Default
  314. * to INPUT.
  315. */
  316. return GPIO_LINE_DIRECTION_IN;
  317. }
  318. static int bcm2835_gpio_set(struct gpio_chip *chip, unsigned int offset,
  319. int value)
  320. {
  321. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  322. bcm2835_gpio_set_bit(pc, value ? GPSET0 : GPCLR0, offset);
  323. return 0;
  324. }
  325. static int bcm2835_gpio_direction_output(struct gpio_chip *chip,
  326. unsigned offset, int value)
  327. {
  328. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  329. bcm2835_gpio_set_bit(pc, value ? GPSET0 : GPCLR0, offset);
  330. bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_OUT);
  331. return 0;
  332. }
  333. static int bcm2835_add_pin_ranges_fallback(struct gpio_chip *gc)
  334. {
  335. struct device_node *np = dev_of_node(gc->parent);
  336. struct pinctrl_dev *pctldev = of_pinctrl_get(np);
  337. if (!pctldev)
  338. return 0;
  339. return gpiochip_add_pin_range(gc, pinctrl_dev_get_devname(pctldev), 0, 0,
  340. gc->ngpio);
  341. }
  342. static const struct gpio_chip bcm2835_gpio_chip = {
  343. .label = MODULE_NAME,
  344. .owner = THIS_MODULE,
  345. .request = gpiochip_generic_request,
  346. .free = gpiochip_generic_free,
  347. .direction_input = bcm2835_gpio_direction_input,
  348. .direction_output = bcm2835_gpio_direction_output,
  349. .get_direction = bcm2835_gpio_get_direction,
  350. .get = bcm2835_gpio_get,
  351. .set = bcm2835_gpio_set,
  352. .set_config = gpiochip_generic_config,
  353. .base = -1,
  354. .ngpio = BCM2835_NUM_GPIOS,
  355. .can_sleep = false,
  356. .add_pin_ranges = bcm2835_add_pin_ranges_fallback,
  357. };
  358. static const struct gpio_chip bcm2711_gpio_chip = {
  359. .label = "pinctrl-bcm2711",
  360. .owner = THIS_MODULE,
  361. .request = gpiochip_generic_request,
  362. .free = gpiochip_generic_free,
  363. .direction_input = bcm2835_gpio_direction_input,
  364. .direction_output = bcm2835_gpio_direction_output,
  365. .get_direction = bcm2835_gpio_get_direction,
  366. .get = bcm2835_gpio_get,
  367. .set = bcm2835_gpio_set,
  368. .set_config = gpiochip_generic_config,
  369. .base = -1,
  370. .ngpio = BCM2711_NUM_GPIOS,
  371. .can_sleep = false,
  372. .add_pin_ranges = bcm2835_add_pin_ranges_fallback,
  373. };
  374. static void bcm2835_gpio_irq_handle_bank(struct bcm2835_pinctrl *pc,
  375. unsigned int bank, u32 mask)
  376. {
  377. unsigned long events;
  378. unsigned offset;
  379. unsigned gpio;
  380. events = bcm2835_gpio_rd(pc, GPEDS0 + bank * 4);
  381. events &= mask;
  382. events &= pc->enabled_irq_map[bank];
  383. for_each_set_bit(offset, &events, 32) {
  384. gpio = (32 * bank) + offset;
  385. generic_handle_domain_irq(pc->gpio_chip.irq.domain,
  386. gpio);
  387. }
  388. }
  389. static void bcm2835_gpio_irq_handler(struct irq_desc *desc)
  390. {
  391. struct gpio_chip *chip = irq_desc_get_handler_data(desc);
  392. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  393. struct irq_chip *host_chip = irq_desc_get_chip(desc);
  394. int irq = irq_desc_get_irq(desc);
  395. int group = 0;
  396. int i;
  397. for (i = 0; i < BCM2835_NUM_IRQS; i++) {
  398. if (chip->irq.parents[i] == irq) {
  399. group = i;
  400. break;
  401. }
  402. }
  403. /* This should not happen, every IRQ has a bank */
  404. BUG_ON(i == BCM2835_NUM_IRQS);
  405. chained_irq_enter(host_chip, desc);
  406. switch (group) {
  407. case 0: /* IRQ0 covers GPIOs 0-27 */
  408. bcm2835_gpio_irq_handle_bank(pc, 0, 0x0fffffff);
  409. break;
  410. case 1: /* IRQ1 covers GPIOs 28-45 */
  411. bcm2835_gpio_irq_handle_bank(pc, 0, 0xf0000000);
  412. bcm2835_gpio_irq_handle_bank(pc, 1, 0x00003fff);
  413. break;
  414. case 2: /* IRQ2 covers GPIOs 46-57 */
  415. bcm2835_gpio_irq_handle_bank(pc, 1, 0x003fc000);
  416. break;
  417. }
  418. chained_irq_exit(host_chip, desc);
  419. }
  420. static irqreturn_t bcm2835_gpio_wake_irq_handler(int irq, void *dev_id)
  421. {
  422. return IRQ_HANDLED;
  423. }
  424. static inline void __bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
  425. unsigned reg, unsigned offset, bool enable)
  426. {
  427. u32 value;
  428. reg += GPIO_REG_OFFSET(offset) * 4;
  429. value = bcm2835_gpio_rd(pc, reg);
  430. if (enable)
  431. value |= BIT(GPIO_REG_SHIFT(offset));
  432. else
  433. value &= ~(BIT(GPIO_REG_SHIFT(offset)));
  434. bcm2835_gpio_wr(pc, reg, value);
  435. }
  436. /* fast path for IRQ handler */
  437. static void bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
  438. unsigned offset, bool enable)
  439. {
  440. switch (pc->irq_type[offset]) {
  441. case IRQ_TYPE_EDGE_RISING:
  442. __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
  443. break;
  444. case IRQ_TYPE_EDGE_FALLING:
  445. __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
  446. break;
  447. case IRQ_TYPE_EDGE_BOTH:
  448. __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
  449. __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
  450. break;
  451. case IRQ_TYPE_LEVEL_HIGH:
  452. __bcm2835_gpio_irq_config(pc, GPHEN0, offset, enable);
  453. break;
  454. case IRQ_TYPE_LEVEL_LOW:
  455. __bcm2835_gpio_irq_config(pc, GPLEN0, offset, enable);
  456. break;
  457. }
  458. }
  459. static void bcm2835_gpio_irq_unmask(struct irq_data *data)
  460. {
  461. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  462. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  463. unsigned gpio = irqd_to_hwirq(data);
  464. unsigned offset = GPIO_REG_SHIFT(gpio);
  465. unsigned bank = GPIO_REG_OFFSET(gpio);
  466. unsigned long flags;
  467. gpiochip_enable_irq(chip, gpio);
  468. raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
  469. set_bit(offset, &pc->enabled_irq_map[bank]);
  470. bcm2835_gpio_irq_config(pc, gpio, true);
  471. raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  472. }
  473. static void bcm2835_gpio_irq_mask(struct irq_data *data)
  474. {
  475. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  476. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  477. unsigned gpio = irqd_to_hwirq(data);
  478. unsigned offset = GPIO_REG_SHIFT(gpio);
  479. unsigned bank = GPIO_REG_OFFSET(gpio);
  480. unsigned long flags;
  481. raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
  482. bcm2835_gpio_irq_config(pc, gpio, false);
  483. /* Clear events that were latched prior to clearing event sources */
  484. bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
  485. clear_bit(offset, &pc->enabled_irq_map[bank]);
  486. raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  487. gpiochip_disable_irq(chip, gpio);
  488. }
  489. static int __bcm2835_gpio_irq_set_type_disabled(struct bcm2835_pinctrl *pc,
  490. unsigned offset, unsigned int type)
  491. {
  492. switch (type) {
  493. case IRQ_TYPE_NONE:
  494. case IRQ_TYPE_EDGE_RISING:
  495. case IRQ_TYPE_EDGE_FALLING:
  496. case IRQ_TYPE_EDGE_BOTH:
  497. case IRQ_TYPE_LEVEL_HIGH:
  498. case IRQ_TYPE_LEVEL_LOW:
  499. pc->irq_type[offset] = type;
  500. break;
  501. default:
  502. return -EINVAL;
  503. }
  504. return 0;
  505. }
  506. /* slower path for reconfiguring IRQ type */
  507. static int __bcm2835_gpio_irq_set_type_enabled(struct bcm2835_pinctrl *pc,
  508. unsigned offset, unsigned int type)
  509. {
  510. switch (type) {
  511. case IRQ_TYPE_NONE:
  512. if (pc->irq_type[offset] != type) {
  513. bcm2835_gpio_irq_config(pc, offset, false);
  514. pc->irq_type[offset] = type;
  515. }
  516. break;
  517. case IRQ_TYPE_EDGE_RISING:
  518. if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
  519. /* RISING already enabled, disable FALLING */
  520. pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
  521. bcm2835_gpio_irq_config(pc, offset, false);
  522. pc->irq_type[offset] = type;
  523. } else if (pc->irq_type[offset] != type) {
  524. bcm2835_gpio_irq_config(pc, offset, false);
  525. pc->irq_type[offset] = type;
  526. bcm2835_gpio_irq_config(pc, offset, true);
  527. }
  528. break;
  529. case IRQ_TYPE_EDGE_FALLING:
  530. if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
  531. /* FALLING already enabled, disable RISING */
  532. pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
  533. bcm2835_gpio_irq_config(pc, offset, false);
  534. pc->irq_type[offset] = type;
  535. } else if (pc->irq_type[offset] != type) {
  536. bcm2835_gpio_irq_config(pc, offset, false);
  537. pc->irq_type[offset] = type;
  538. bcm2835_gpio_irq_config(pc, offset, true);
  539. }
  540. break;
  541. case IRQ_TYPE_EDGE_BOTH:
  542. if (pc->irq_type[offset] == IRQ_TYPE_EDGE_RISING) {
  543. /* RISING already enabled, enable FALLING too */
  544. pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
  545. bcm2835_gpio_irq_config(pc, offset, true);
  546. pc->irq_type[offset] = type;
  547. } else if (pc->irq_type[offset] == IRQ_TYPE_EDGE_FALLING) {
  548. /* FALLING already enabled, enable RISING too */
  549. pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
  550. bcm2835_gpio_irq_config(pc, offset, true);
  551. pc->irq_type[offset] = type;
  552. } else if (pc->irq_type[offset] != type) {
  553. bcm2835_gpio_irq_config(pc, offset, false);
  554. pc->irq_type[offset] = type;
  555. bcm2835_gpio_irq_config(pc, offset, true);
  556. }
  557. break;
  558. case IRQ_TYPE_LEVEL_HIGH:
  559. case IRQ_TYPE_LEVEL_LOW:
  560. if (pc->irq_type[offset] != type) {
  561. bcm2835_gpio_irq_config(pc, offset, false);
  562. pc->irq_type[offset] = type;
  563. bcm2835_gpio_irq_config(pc, offset, true);
  564. }
  565. break;
  566. default:
  567. return -EINVAL;
  568. }
  569. return 0;
  570. }
  571. static int bcm2835_gpio_irq_set_type(struct irq_data *data, unsigned int type)
  572. {
  573. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  574. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  575. unsigned gpio = irqd_to_hwirq(data);
  576. unsigned offset = GPIO_REG_SHIFT(gpio);
  577. unsigned bank = GPIO_REG_OFFSET(gpio);
  578. unsigned long flags;
  579. int ret;
  580. raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
  581. if (test_bit(offset, &pc->enabled_irq_map[bank]))
  582. ret = __bcm2835_gpio_irq_set_type_enabled(pc, gpio, type);
  583. else
  584. ret = __bcm2835_gpio_irq_set_type_disabled(pc, gpio, type);
  585. if (type & IRQ_TYPE_EDGE_BOTH)
  586. irq_set_handler_locked(data, handle_edge_irq);
  587. else
  588. irq_set_handler_locked(data, handle_level_irq);
  589. raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
  590. return ret;
  591. }
  592. static void bcm2835_gpio_irq_ack(struct irq_data *data)
  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. bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
  598. }
  599. static int bcm2835_gpio_irq_set_wake(struct irq_data *data, unsigned int on)
  600. {
  601. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  602. struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
  603. unsigned gpio = irqd_to_hwirq(data);
  604. unsigned int irqgroup;
  605. int ret = -EINVAL;
  606. if (!pc->wake_irq)
  607. return ret;
  608. if (gpio <= 27)
  609. irqgroup = 0;
  610. else if (gpio >= 28 && gpio <= 45)
  611. irqgroup = 1;
  612. else if (gpio >= 46 && gpio <= 57)
  613. irqgroup = 2;
  614. else
  615. return ret;
  616. if (on)
  617. ret = enable_irq_wake(pc->wake_irq[irqgroup]);
  618. else
  619. ret = disable_irq_wake(pc->wake_irq[irqgroup]);
  620. return ret;
  621. }
  622. static const struct irq_chip bcm2835_gpio_irq_chip = {
  623. .name = MODULE_NAME,
  624. .irq_set_type = bcm2835_gpio_irq_set_type,
  625. .irq_ack = bcm2835_gpio_irq_ack,
  626. .irq_mask = bcm2835_gpio_irq_mask,
  627. .irq_unmask = bcm2835_gpio_irq_unmask,
  628. .irq_set_wake = bcm2835_gpio_irq_set_wake,
  629. .flags = (IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE),
  630. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  631. };
  632. static int bcm2835_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  633. {
  634. return BCM2835_NUM_GPIOS;
  635. }
  636. static const char *bcm2835_pctl_get_group_name(struct pinctrl_dev *pctldev,
  637. unsigned selector)
  638. {
  639. return bcm2835_gpio_groups[selector];
  640. }
  641. static int bcm2835_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  642. unsigned selector,
  643. const unsigned **pins,
  644. unsigned *num_pins)
  645. {
  646. *pins = &bcm2835_gpio_pins[selector].number;
  647. *num_pins = 1;
  648. return 0;
  649. }
  650. static void bcm2835_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
  651. struct seq_file *s,
  652. unsigned offset)
  653. {
  654. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  655. struct gpio_chip *chip = &pc->gpio_chip;
  656. enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
  657. const char *fname = bcm2835_functions[fsel];
  658. int value = bcm2835_gpio_get_bit(pc, GPLEV0, offset);
  659. int irq = irq_find_mapping(chip->irq.domain, offset);
  660. seq_printf(s, "function %s in %s; irq %d (%s)",
  661. fname, str_hi_lo(value),
  662. irq, irq_type_names[pc->irq_type[offset]]);
  663. }
  664. static void bcm2835_pctl_dt_free_map(struct pinctrl_dev *pctldev,
  665. struct pinctrl_map *maps, unsigned num_maps)
  666. {
  667. int i;
  668. for (i = 0; i < num_maps; i++)
  669. if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  670. kfree(maps[i].data.configs.configs);
  671. kfree(maps);
  672. }
  673. static int bcm2835_pctl_dt_node_to_map_func(struct bcm2835_pinctrl *pc,
  674. struct device_node *np, u32 pin, u32 fnum,
  675. struct pinctrl_map **maps)
  676. {
  677. struct pinctrl_map *map = *maps;
  678. if (fnum >= ARRAY_SIZE(bcm2835_functions)) {
  679. dev_err(pc->dev, "%pOF: invalid brcm,function %d\n", np, fnum);
  680. return -EINVAL;
  681. }
  682. map->type = PIN_MAP_TYPE_MUX_GROUP;
  683. map->data.mux.group = bcm2835_gpio_groups[pin];
  684. map->data.mux.function = bcm2835_functions[fnum];
  685. (*maps)++;
  686. return 0;
  687. }
  688. static int bcm2835_pctl_dt_node_to_map_pull(struct bcm2835_pinctrl *pc,
  689. struct device_node *np, u32 pin, u32 pull,
  690. struct pinctrl_map **maps)
  691. {
  692. struct pinctrl_map *map = *maps;
  693. unsigned long *configs;
  694. if (pull > 2) {
  695. dev_err(pc->dev, "%pOF: invalid brcm,pull %d\n", np, pull);
  696. return -EINVAL;
  697. }
  698. configs = kzalloc_obj(*configs);
  699. if (!configs)
  700. return -ENOMEM;
  701. configs[0] = pinconf_to_config_packed(BCM2835_PINCONF_PARAM_PULL, pull);
  702. map->type = PIN_MAP_TYPE_CONFIGS_PIN;
  703. map->data.configs.group_or_pin = bcm2835_gpio_pins[pin].name;
  704. map->data.configs.configs = configs;
  705. map->data.configs.num_configs = 1;
  706. (*maps)++;
  707. return 0;
  708. }
  709. static int bcm2835_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  710. struct device_node *np,
  711. struct pinctrl_map **map, unsigned int *num_maps)
  712. {
  713. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  714. struct property *pins, *funcs, *pulls;
  715. int num_pins, num_funcs, num_pulls, maps_per_pin;
  716. struct pinctrl_map *maps, *cur_map;
  717. int i, err;
  718. u32 pin, func, pull;
  719. /* Check for generic binding in this node */
  720. err = pinconf_generic_dt_node_to_map_all(pctldev, np, map, num_maps);
  721. if (err || *num_maps)
  722. return err;
  723. /* Generic binding did not find anything continue with legacy parse */
  724. pins = of_find_property(np, "brcm,pins", NULL);
  725. if (!pins) {
  726. dev_err(pc->dev, "%pOF: missing brcm,pins property\n", np);
  727. return -EINVAL;
  728. }
  729. funcs = of_find_property(np, "brcm,function", NULL);
  730. pulls = of_find_property(np, "brcm,pull", NULL);
  731. if (!funcs && !pulls) {
  732. dev_err(pc->dev,
  733. "%pOF: neither brcm,function nor brcm,pull specified\n",
  734. np);
  735. return -EINVAL;
  736. }
  737. num_pins = pins->length / 4;
  738. num_funcs = funcs ? (funcs->length / 4) : 0;
  739. num_pulls = pulls ? (pulls->length / 4) : 0;
  740. if (num_funcs > 1 && num_funcs != num_pins) {
  741. dev_err(pc->dev,
  742. "%pOF: brcm,function must have 1 or %d entries\n",
  743. np, num_pins);
  744. return -EINVAL;
  745. }
  746. if (num_pulls > 1 && num_pulls != num_pins) {
  747. dev_err(pc->dev,
  748. "%pOF: brcm,pull must have 1 or %d entries\n",
  749. np, num_pins);
  750. return -EINVAL;
  751. }
  752. maps_per_pin = 0;
  753. if (num_funcs)
  754. maps_per_pin++;
  755. if (num_pulls)
  756. maps_per_pin++;
  757. cur_map = maps = kzalloc_objs(*maps, num_pins * maps_per_pin);
  758. if (!maps)
  759. return -ENOMEM;
  760. for (i = 0; i < num_pins; i++) {
  761. err = of_property_read_u32_index(np, "brcm,pins", i, &pin);
  762. if (err)
  763. goto out;
  764. if (pin >= pc->pctl_desc.npins) {
  765. dev_err(pc->dev, "%pOF: invalid brcm,pins value %d\n",
  766. np, pin);
  767. err = -EINVAL;
  768. goto out;
  769. }
  770. if (num_funcs) {
  771. err = of_property_read_u32_index(np, "brcm,function",
  772. (num_funcs > 1) ? i : 0, &func);
  773. if (err)
  774. goto out;
  775. err = bcm2835_pctl_dt_node_to_map_func(pc, np, pin,
  776. func, &cur_map);
  777. if (err)
  778. goto out;
  779. }
  780. if (num_pulls) {
  781. err = of_property_read_u32_index(np, "brcm,pull",
  782. (num_pulls > 1) ? i : 0, &pull);
  783. if (err)
  784. goto out;
  785. err = bcm2835_pctl_dt_node_to_map_pull(pc, np, pin,
  786. pull, &cur_map);
  787. if (err)
  788. goto out;
  789. }
  790. }
  791. *map = maps;
  792. *num_maps = num_pins * maps_per_pin;
  793. return 0;
  794. out:
  795. bcm2835_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
  796. return err;
  797. }
  798. static const struct pinctrl_ops bcm2835_pctl_ops = {
  799. .get_groups_count = bcm2835_pctl_get_groups_count,
  800. .get_group_name = bcm2835_pctl_get_group_name,
  801. .get_group_pins = bcm2835_pctl_get_group_pins,
  802. .pin_dbg_show = bcm2835_pctl_pin_dbg_show,
  803. .dt_node_to_map = bcm2835_pctl_dt_node_to_map,
  804. .dt_free_map = bcm2835_pctl_dt_free_map,
  805. };
  806. static int bcm2835_pmx_free(struct pinctrl_dev *pctldev,
  807. unsigned offset)
  808. {
  809. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  810. enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
  811. if (fsel == BCM2835_FSEL_GPIO_IN)
  812. return 0;
  813. if (persist_gpio_outputs && fsel == BCM2835_FSEL_GPIO_OUT)
  814. return 0;
  815. /* disable by setting to GPIO_IN */
  816. bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
  817. return 0;
  818. }
  819. static int bcm2835_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  820. {
  821. return BCM2835_FSEL_COUNT;
  822. }
  823. static const char *bcm2835_pmx_get_function_name(struct pinctrl_dev *pctldev,
  824. unsigned selector)
  825. {
  826. return bcm2835_functions[selector];
  827. }
  828. static int bcm2835_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  829. unsigned selector,
  830. const char * const **groups,
  831. unsigned * const num_groups)
  832. {
  833. /* every pin can do every function */
  834. *groups = bcm2835_gpio_groups;
  835. *num_groups = BCM2835_NUM_GPIOS;
  836. return 0;
  837. }
  838. static int bcm2835_pmx_set(struct pinctrl_dev *pctldev,
  839. unsigned func_selector,
  840. unsigned group_selector)
  841. {
  842. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  843. bcm2835_pinctrl_fsel_set(pc, group_selector, func_selector);
  844. return 0;
  845. }
  846. static void bcm2835_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
  847. struct pinctrl_gpio_range *range,
  848. unsigned offset)
  849. {
  850. bcm2835_pmx_free(pctldev, offset);
  851. }
  852. static int bcm2835_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  853. struct pinctrl_gpio_range *range,
  854. unsigned offset,
  855. bool input)
  856. {
  857. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  858. enum bcm2835_fsel fsel = input ?
  859. BCM2835_FSEL_GPIO_IN : BCM2835_FSEL_GPIO_OUT;
  860. bcm2835_pinctrl_fsel_set(pc, offset, fsel);
  861. return 0;
  862. }
  863. static const struct pinmux_ops bcm2835_pmx_ops = {
  864. .free = bcm2835_pmx_free,
  865. .get_functions_count = bcm2835_pmx_get_functions_count,
  866. .get_function_name = bcm2835_pmx_get_function_name,
  867. .get_function_groups = bcm2835_pmx_get_function_groups,
  868. .set_mux = bcm2835_pmx_set,
  869. .gpio_disable_free = bcm2835_pmx_gpio_disable_free,
  870. .gpio_set_direction = bcm2835_pmx_gpio_set_direction,
  871. };
  872. static int bcm2835_pinconf_get(struct pinctrl_dev *pctldev,
  873. unsigned pin, unsigned long *config)
  874. {
  875. enum pin_config_param param = pinconf_to_config_param(*config);
  876. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  877. enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, pin);
  878. u32 val;
  879. /* No way to read back bias config in HW */
  880. switch (param) {
  881. case PIN_CONFIG_LEVEL:
  882. if (fsel != BCM2835_FSEL_GPIO_OUT)
  883. return -EINVAL;
  884. val = bcm2835_gpio_get_bit(pc, GPLEV0, pin);
  885. *config = pinconf_to_config_packed(param, val);
  886. break;
  887. default:
  888. return -ENOTSUPP;
  889. }
  890. return 0;
  891. }
  892. static void bcm2835_pull_config_set(struct bcm2835_pinctrl *pc,
  893. unsigned int pin, unsigned int arg)
  894. {
  895. u32 off, bit;
  896. off = GPIO_REG_OFFSET(pin);
  897. bit = GPIO_REG_SHIFT(pin);
  898. bcm2835_gpio_wr(pc, GPPUD, arg & 3);
  899. /*
  900. * BCM2835 datasheet say to wait 150 cycles, but not of what.
  901. * But the VideoCore firmware delay for this operation
  902. * based nearly on the same amount of VPU cycles and this clock
  903. * runs at 250 MHz.
  904. */
  905. udelay(1);
  906. bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), BIT(bit));
  907. udelay(1);
  908. bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), 0);
  909. }
  910. static int bcm2835_pinconf_set(struct pinctrl_dev *pctldev,
  911. unsigned int pin, unsigned long *configs,
  912. unsigned int num_configs)
  913. {
  914. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  915. u32 param, arg;
  916. int i;
  917. for (i = 0; i < num_configs; i++) {
  918. param = pinconf_to_config_param(configs[i]);
  919. arg = pinconf_to_config_argument(configs[i]);
  920. switch (param) {
  921. /* Set legacy brcm,pull */
  922. case BCM2835_PINCONF_PARAM_PULL:
  923. bcm2835_pull_config_set(pc, pin, arg);
  924. break;
  925. /* Set pull generic bindings */
  926. case PIN_CONFIG_BIAS_DISABLE:
  927. bcm2835_pull_config_set(pc, pin, BCM2835_PUD_OFF);
  928. break;
  929. case PIN_CONFIG_BIAS_PULL_DOWN:
  930. bcm2835_pull_config_set(pc, pin, BCM2835_PUD_DOWN);
  931. break;
  932. case PIN_CONFIG_BIAS_PULL_UP:
  933. bcm2835_pull_config_set(pc, pin, BCM2835_PUD_UP);
  934. break;
  935. /* Set output-high or output-low */
  936. case PIN_CONFIG_LEVEL:
  937. bcm2835_gpio_set_bit(pc, arg ? GPSET0 : GPCLR0, pin);
  938. break;
  939. default:
  940. return -ENOTSUPP;
  941. } /* switch param type */
  942. } /* for each config */
  943. return 0;
  944. }
  945. static const struct pinconf_ops bcm2835_pinconf_ops = {
  946. .is_generic = true,
  947. .pin_config_get = bcm2835_pinconf_get,
  948. .pin_config_set = bcm2835_pinconf_set,
  949. };
  950. static int bcm2711_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
  951. unsigned long *config)
  952. {
  953. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  954. enum pin_config_param param = pinconf_to_config_param(*config);
  955. u32 offset, shift, val;
  956. offset = PUD_2711_REG_OFFSET(pin);
  957. shift = PUD_2711_REG_SHIFT(pin);
  958. val = bcm2835_gpio_rd(pc, GP_GPIO_PUP_PDN_CNTRL_REG0 + (offset * 4));
  959. switch (param) {
  960. case PIN_CONFIG_BIAS_DISABLE:
  961. if (((val >> shift) & PUD_2711_MASK) != BCM2711_PULL_NONE)
  962. return -EINVAL;
  963. break;
  964. case PIN_CONFIG_BIAS_PULL_UP:
  965. if (((val >> shift) & PUD_2711_MASK) != BCM2711_PULL_UP)
  966. return -EINVAL;
  967. *config = pinconf_to_config_packed(param, 50000);
  968. break;
  969. case PIN_CONFIG_BIAS_PULL_DOWN:
  970. if (((val >> shift) & PUD_2711_MASK) != BCM2711_PULL_DOWN)
  971. return -EINVAL;
  972. *config = pinconf_to_config_packed(param, 50000);
  973. break;
  974. default:
  975. return bcm2835_pinconf_get(pctldev, pin, config);
  976. }
  977. return 0;
  978. }
  979. static void bcm2711_pull_config_set(struct bcm2835_pinctrl *pc,
  980. unsigned int pin, unsigned int arg)
  981. {
  982. u32 shifter;
  983. u32 value;
  984. u32 off;
  985. off = PUD_2711_REG_OFFSET(pin);
  986. shifter = PUD_2711_REG_SHIFT(pin);
  987. value = bcm2835_gpio_rd(pc, GP_GPIO_PUP_PDN_CNTRL_REG0 + (off * 4));
  988. value &= ~(PUD_2711_MASK << shifter);
  989. value |= (arg << shifter);
  990. bcm2835_gpio_wr(pc, GP_GPIO_PUP_PDN_CNTRL_REG0 + (off * 4), value);
  991. }
  992. static int bcm2711_pinconf_set(struct pinctrl_dev *pctldev,
  993. unsigned int pin, unsigned long *configs,
  994. unsigned int num_configs)
  995. {
  996. struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  997. u32 param, arg;
  998. int i;
  999. for (i = 0; i < num_configs; i++) {
  1000. param = pinconf_to_config_param(configs[i]);
  1001. arg = pinconf_to_config_argument(configs[i]);
  1002. switch (param) {
  1003. /* convert legacy brcm,pull */
  1004. case BCM2835_PINCONF_PARAM_PULL:
  1005. if (arg == BCM2835_PUD_UP)
  1006. arg = BCM2711_PULL_UP;
  1007. else if (arg == BCM2835_PUD_DOWN)
  1008. arg = BCM2711_PULL_DOWN;
  1009. else
  1010. arg = BCM2711_PULL_NONE;
  1011. bcm2711_pull_config_set(pc, pin, arg);
  1012. break;
  1013. /* Set pull generic bindings */
  1014. case PIN_CONFIG_BIAS_DISABLE:
  1015. bcm2711_pull_config_set(pc, pin, BCM2711_PULL_NONE);
  1016. break;
  1017. case PIN_CONFIG_BIAS_PULL_DOWN:
  1018. bcm2711_pull_config_set(pc, pin, BCM2711_PULL_DOWN);
  1019. break;
  1020. case PIN_CONFIG_BIAS_PULL_UP:
  1021. bcm2711_pull_config_set(pc, pin, BCM2711_PULL_UP);
  1022. break;
  1023. /* Set output-high or output-low */
  1024. case PIN_CONFIG_LEVEL:
  1025. bcm2835_gpio_set_bit(pc, arg ? GPSET0 : GPCLR0, pin);
  1026. break;
  1027. default:
  1028. return -ENOTSUPP;
  1029. }
  1030. } /* for each config */
  1031. return 0;
  1032. }
  1033. static const struct pinconf_ops bcm2711_pinconf_ops = {
  1034. .is_generic = true,
  1035. .pin_config_get = bcm2711_pinconf_get,
  1036. .pin_config_set = bcm2711_pinconf_set,
  1037. };
  1038. static const struct pinctrl_desc bcm2835_pinctrl_desc = {
  1039. .name = MODULE_NAME,
  1040. .pins = bcm2835_gpio_pins,
  1041. .npins = BCM2835_NUM_GPIOS,
  1042. .pctlops = &bcm2835_pctl_ops,
  1043. .pmxops = &bcm2835_pmx_ops,
  1044. .confops = &bcm2835_pinconf_ops,
  1045. .owner = THIS_MODULE,
  1046. };
  1047. static const struct pinctrl_desc bcm2711_pinctrl_desc = {
  1048. .name = "pinctrl-bcm2711",
  1049. .pins = bcm2835_gpio_pins,
  1050. .npins = BCM2711_NUM_GPIOS,
  1051. .pctlops = &bcm2835_pctl_ops,
  1052. .pmxops = &bcm2835_pmx_ops,
  1053. .confops = &bcm2711_pinconf_ops,
  1054. .owner = THIS_MODULE,
  1055. };
  1056. static const struct pinctrl_gpio_range bcm2835_pinctrl_gpio_range = {
  1057. .name = MODULE_NAME,
  1058. .npins = BCM2835_NUM_GPIOS,
  1059. };
  1060. static const struct pinctrl_gpio_range bcm2711_pinctrl_gpio_range = {
  1061. .name = "pinctrl-bcm2711",
  1062. .npins = BCM2711_NUM_GPIOS,
  1063. };
  1064. struct bcm_plat_data {
  1065. const struct gpio_chip *gpio_chip;
  1066. const struct pinctrl_desc *pctl_desc;
  1067. const struct pinctrl_gpio_range *gpio_range;
  1068. };
  1069. static const struct bcm_plat_data bcm2835_plat_data = {
  1070. .gpio_chip = &bcm2835_gpio_chip,
  1071. .pctl_desc = &bcm2835_pinctrl_desc,
  1072. .gpio_range = &bcm2835_pinctrl_gpio_range,
  1073. };
  1074. static const struct bcm_plat_data bcm2711_plat_data = {
  1075. .gpio_chip = &bcm2711_gpio_chip,
  1076. .pctl_desc = &bcm2711_pinctrl_desc,
  1077. .gpio_range = &bcm2711_pinctrl_gpio_range,
  1078. };
  1079. static const struct of_device_id bcm2835_pinctrl_match[] = {
  1080. {
  1081. .compatible = "brcm,bcm2835-gpio",
  1082. .data = &bcm2835_plat_data,
  1083. },
  1084. {
  1085. .compatible = "brcm,bcm2711-gpio",
  1086. .data = &bcm2711_plat_data,
  1087. },
  1088. {
  1089. .compatible = "brcm,bcm7211-gpio",
  1090. .data = &bcm2711_plat_data,
  1091. },
  1092. {}
  1093. };
  1094. MODULE_DEVICE_TABLE(of, bcm2835_pinctrl_match);
  1095. static int bcm2835_pinctrl_probe(struct platform_device *pdev)
  1096. {
  1097. struct device *dev = &pdev->dev;
  1098. struct device_node *np = dev->of_node;
  1099. const struct bcm_plat_data *pdata;
  1100. struct bcm2835_pinctrl *pc;
  1101. struct gpio_irq_chip *girq;
  1102. struct resource iomem;
  1103. int err, i;
  1104. const struct of_device_id *match;
  1105. int is_7211 = 0;
  1106. BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_pins) != BCM2711_NUM_GPIOS);
  1107. BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_groups) != BCM2711_NUM_GPIOS);
  1108. pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
  1109. if (!pc)
  1110. return -ENOMEM;
  1111. platform_set_drvdata(pdev, pc);
  1112. pc->dev = dev;
  1113. err = of_address_to_resource(np, 0, &iomem);
  1114. if (err) {
  1115. dev_err(dev, "could not get IO memory\n");
  1116. return err;
  1117. }
  1118. pc->base = devm_ioremap_resource(dev, &iomem);
  1119. if (IS_ERR(pc->base))
  1120. return PTR_ERR(pc->base);
  1121. match = of_match_node(bcm2835_pinctrl_match, pdev->dev.of_node);
  1122. if (!match)
  1123. return -EINVAL;
  1124. pdata = match->data;
  1125. is_7211 = of_device_is_compatible(np, "brcm,bcm7211-gpio");
  1126. pc->gpio_chip = *pdata->gpio_chip;
  1127. pc->gpio_chip.parent = dev;
  1128. spin_lock_init(&pc->fsel_lock);
  1129. for (i = 0; i < BCM2835_NUM_BANKS; i++) {
  1130. unsigned long events;
  1131. unsigned offset;
  1132. /* clear event detection flags */
  1133. bcm2835_gpio_wr(pc, GPREN0 + i * 4, 0);
  1134. bcm2835_gpio_wr(pc, GPFEN0 + i * 4, 0);
  1135. bcm2835_gpio_wr(pc, GPHEN0 + i * 4, 0);
  1136. bcm2835_gpio_wr(pc, GPLEN0 + i * 4, 0);
  1137. bcm2835_gpio_wr(pc, GPAREN0 + i * 4, 0);
  1138. bcm2835_gpio_wr(pc, GPAFEN0 + i * 4, 0);
  1139. /* clear all the events */
  1140. events = bcm2835_gpio_rd(pc, GPEDS0 + i * 4);
  1141. for_each_set_bit(offset, &events, 32)
  1142. bcm2835_gpio_wr(pc, GPEDS0 + i * 4, BIT(offset));
  1143. raw_spin_lock_init(&pc->irq_lock[i]);
  1144. }
  1145. pc->pctl_desc = *pdata->pctl_desc;
  1146. pc->pctl_dev = devm_pinctrl_register(dev, &pc->pctl_desc, pc);
  1147. if (IS_ERR(pc->pctl_dev)) {
  1148. gpiochip_remove(&pc->gpio_chip);
  1149. return PTR_ERR(pc->pctl_dev);
  1150. }
  1151. pc->gpio_range = *pdata->gpio_range;
  1152. pc->gpio_range.base = pc->gpio_chip.base;
  1153. pc->gpio_range.gc = &pc->gpio_chip;
  1154. pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
  1155. girq = &pc->gpio_chip.irq;
  1156. gpio_irq_chip_set_chip(girq, &bcm2835_gpio_irq_chip);
  1157. girq->parent_handler = bcm2835_gpio_irq_handler;
  1158. girq->num_parents = BCM2835_NUM_IRQS;
  1159. girq->parents = devm_kcalloc(dev, BCM2835_NUM_IRQS,
  1160. sizeof(*girq->parents),
  1161. GFP_KERNEL);
  1162. if (!girq->parents) {
  1163. err = -ENOMEM;
  1164. goto out_remove;
  1165. }
  1166. if (is_7211) {
  1167. pc->wake_irq = devm_kcalloc(dev, BCM2835_NUM_IRQS,
  1168. sizeof(*pc->wake_irq),
  1169. GFP_KERNEL);
  1170. if (!pc->wake_irq) {
  1171. err = -ENOMEM;
  1172. goto out_remove;
  1173. }
  1174. }
  1175. /*
  1176. * Use the same handler for all groups: this is necessary
  1177. * since we use one gpiochip to cover all lines - the
  1178. * irq handler then needs to figure out which group and
  1179. * bank that was firing the IRQ and look up the per-group
  1180. * and bank data.
  1181. */
  1182. for (i = 0; i < BCM2835_NUM_IRQS; i++) {
  1183. int len;
  1184. char *name;
  1185. girq->parents[i] = irq_of_parse_and_map(np, i);
  1186. if (!is_7211) {
  1187. if (!girq->parents[i]) {
  1188. girq->num_parents = i;
  1189. break;
  1190. }
  1191. continue;
  1192. }
  1193. /* Skip over the all banks interrupts */
  1194. pc->wake_irq[i] = irq_of_parse_and_map(np, i +
  1195. BCM2835_NUM_IRQS + 1);
  1196. len = strlen(dev_name(pc->dev)) + 16;
  1197. name = devm_kzalloc(pc->dev, len, GFP_KERNEL);
  1198. if (!name) {
  1199. err = -ENOMEM;
  1200. goto out_remove;
  1201. }
  1202. snprintf(name, len, "%s:bank%d", dev_name(pc->dev), i);
  1203. /* These are optional interrupts */
  1204. err = devm_request_irq(dev, pc->wake_irq[i],
  1205. bcm2835_gpio_wake_irq_handler,
  1206. IRQF_SHARED, name, pc);
  1207. if (err)
  1208. dev_warn(dev, "unable to request wake IRQ %d\n",
  1209. pc->wake_irq[i]);
  1210. }
  1211. girq->default_type = IRQ_TYPE_NONE;
  1212. girq->handler = handle_level_irq;
  1213. err = gpiochip_add_data(&pc->gpio_chip, pc);
  1214. if (err) {
  1215. dev_err(dev, "could not add GPIO chip\n");
  1216. goto out_remove;
  1217. }
  1218. dev_info(dev, "GPIO_OUT persistence: %s\n",
  1219. str_yes_no(persist_gpio_outputs));
  1220. return 0;
  1221. out_remove:
  1222. pinctrl_remove_gpio_range(pc->pctl_dev, &pc->gpio_range);
  1223. return err;
  1224. }
  1225. static struct platform_driver bcm2835_pinctrl_driver = {
  1226. .probe = bcm2835_pinctrl_probe,
  1227. .driver = {
  1228. .name = MODULE_NAME,
  1229. .of_match_table = bcm2835_pinctrl_match,
  1230. .suppress_bind_attrs = true,
  1231. },
  1232. };
  1233. module_platform_driver(bcm2835_pinctrl_driver);
  1234. MODULE_AUTHOR("Chris Boot");
  1235. MODULE_AUTHOR("Simon Arlott");
  1236. MODULE_AUTHOR("Stephen Warren");
  1237. MODULE_DESCRIPTION("Broadcom BCM2835/2711 pinctrl and GPIO driver");
  1238. MODULE_LICENSE("GPL");