pinctrl-bcm2835.c 28 KB

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