pinctrl-iproc-gpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /*
  2. * Copyright (C) 2014-2015 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * This file contains the Broadcom Iproc GPIO driver that supports 3
  14. * GPIO controllers on Iproc including the ASIU GPIO controller, the
  15. * chipCommonG GPIO controller, and the always-on GPIO controller. Basic
  16. * PINCONF such as bias pull up/down, and drive strength are also supported
  17. * in this driver.
  18. *
  19. * It provides the functionality where pins from the GPIO can be
  20. * individually muxed to GPIO function, if individual pad
  21. * configuration is supported, through the interaction with respective
  22. * SoCs IOMUX controller.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/gpio/driver.h>
  29. #include <linux/ioport.h>
  30. #include <linux/of_device.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/pinctrl/pinctrl.h>
  33. #include <linux/pinctrl/pinconf.h>
  34. #include <linux/pinctrl/pinconf-generic.h>
  35. #include "../pinctrl-utils.h"
  36. #define IPROC_GPIO_DATA_IN_OFFSET 0x00
  37. #define IPROC_GPIO_DATA_OUT_OFFSET 0x04
  38. #define IPROC_GPIO_OUT_EN_OFFSET 0x08
  39. #define IPROC_GPIO_INT_TYPE_OFFSET 0x0c
  40. #define IPROC_GPIO_INT_DE_OFFSET 0x10
  41. #define IPROC_GPIO_INT_EDGE_OFFSET 0x14
  42. #define IPROC_GPIO_INT_MSK_OFFSET 0x18
  43. #define IPROC_GPIO_INT_STAT_OFFSET 0x1c
  44. #define IPROC_GPIO_INT_MSTAT_OFFSET 0x20
  45. #define IPROC_GPIO_INT_CLR_OFFSET 0x24
  46. #define IPROC_GPIO_PAD_RES_OFFSET 0x34
  47. #define IPROC_GPIO_RES_EN_OFFSET 0x38
  48. /* drive strength control for ASIU GPIO */
  49. #define IPROC_GPIO_ASIU_DRV0_CTRL_OFFSET 0x58
  50. /* drive strength control for CCM/CRMU (AON) GPIO */
  51. #define IPROC_GPIO_DRV0_CTRL_OFFSET 0x00
  52. #define GPIO_BANK_SIZE 0x200
  53. #define NGPIOS_PER_BANK 32
  54. #define GPIO_BANK(pin) ((pin) / NGPIOS_PER_BANK)
  55. #define IPROC_GPIO_REG(pin, reg) (GPIO_BANK(pin) * GPIO_BANK_SIZE + (reg))
  56. #define IPROC_GPIO_SHIFT(pin) ((pin) % NGPIOS_PER_BANK)
  57. #define GPIO_DRV_STRENGTH_BIT_SHIFT 20
  58. #define GPIO_DRV_STRENGTH_BITS 3
  59. #define GPIO_DRV_STRENGTH_BIT_MASK ((1 << GPIO_DRV_STRENGTH_BITS) - 1)
  60. /*
  61. * Iproc GPIO core
  62. *
  63. * @dev: pointer to device
  64. * @base: I/O register base for Iproc GPIO controller
  65. * @io_ctrl: I/O register base for certain type of Iproc GPIO controller that
  66. * has the PINCONF support implemented outside of the GPIO block
  67. * @lock: lock to protect access to I/O registers
  68. * @gc: GPIO chip
  69. * @num_banks: number of GPIO banks, each bank supports up to 32 GPIOs
  70. * @pinmux_is_supported: flag to indicate this GPIO controller contains pins
  71. * that can be individually muxed to GPIO
  72. * @pctl: pointer to pinctrl_dev
  73. * @pctldesc: pinctrl descriptor
  74. */
  75. struct iproc_gpio {
  76. struct device *dev;
  77. void __iomem *base;
  78. void __iomem *io_ctrl;
  79. spinlock_t lock;
  80. struct gpio_chip gc;
  81. unsigned num_banks;
  82. bool pinmux_is_supported;
  83. struct pinctrl_dev *pctl;
  84. struct pinctrl_desc pctldesc;
  85. };
  86. /*
  87. * Mapping from PINCONF pins to GPIO pins is 1-to-1
  88. */
  89. static inline unsigned iproc_pin_to_gpio(unsigned pin)
  90. {
  91. return pin;
  92. }
  93. /**
  94. * iproc_set_bit - set or clear one bit (corresponding to the GPIO pin) in a
  95. * Iproc GPIO register
  96. *
  97. * @iproc_gpio: Iproc GPIO device
  98. * @reg: register offset
  99. * @gpio: GPIO pin
  100. * @set: set or clear
  101. */
  102. static inline void iproc_set_bit(struct iproc_gpio *chip, unsigned int reg,
  103. unsigned gpio, bool set)
  104. {
  105. unsigned int offset = IPROC_GPIO_REG(gpio, reg);
  106. unsigned int shift = IPROC_GPIO_SHIFT(gpio);
  107. u32 val;
  108. val = readl(chip->base + offset);
  109. if (set)
  110. val |= BIT(shift);
  111. else
  112. val &= ~BIT(shift);
  113. writel(val, chip->base + offset);
  114. }
  115. static inline bool iproc_get_bit(struct iproc_gpio *chip, unsigned int reg,
  116. unsigned gpio)
  117. {
  118. unsigned int offset = IPROC_GPIO_REG(gpio, reg);
  119. unsigned int shift = IPROC_GPIO_SHIFT(gpio);
  120. return !!(readl(chip->base + offset) & BIT(shift));
  121. }
  122. static void iproc_gpio_irq_handler(struct irq_desc *desc)
  123. {
  124. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  125. struct iproc_gpio *chip = gpiochip_get_data(gc);
  126. struct irq_chip *irq_chip = irq_desc_get_chip(desc);
  127. int i, bit;
  128. chained_irq_enter(irq_chip, desc);
  129. /* go through the entire GPIO banks and handle all interrupts */
  130. for (i = 0; i < chip->num_banks; i++) {
  131. unsigned long val = readl(chip->base + (i * GPIO_BANK_SIZE) +
  132. IPROC_GPIO_INT_MSTAT_OFFSET);
  133. for_each_set_bit(bit, &val, NGPIOS_PER_BANK) {
  134. unsigned pin = NGPIOS_PER_BANK * i + bit;
  135. int child_irq = irq_find_mapping(gc->irqdomain, pin);
  136. /*
  137. * Clear the interrupt before invoking the
  138. * handler, so we do not leave any window
  139. */
  140. writel(BIT(bit), chip->base + (i * GPIO_BANK_SIZE) +
  141. IPROC_GPIO_INT_CLR_OFFSET);
  142. generic_handle_irq(child_irq);
  143. }
  144. }
  145. chained_irq_exit(irq_chip, desc);
  146. }
  147. static void iproc_gpio_irq_ack(struct irq_data *d)
  148. {
  149. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  150. struct iproc_gpio *chip = gpiochip_get_data(gc);
  151. unsigned gpio = d->hwirq;
  152. unsigned int offset = IPROC_GPIO_REG(gpio,
  153. IPROC_GPIO_INT_CLR_OFFSET);
  154. unsigned int shift = IPROC_GPIO_SHIFT(gpio);
  155. u32 val = BIT(shift);
  156. writel(val, chip->base + offset);
  157. }
  158. /**
  159. * iproc_gpio_irq_set_mask - mask/unmask a GPIO interrupt
  160. *
  161. * @d: IRQ chip data
  162. * @unmask: mask/unmask GPIO interrupt
  163. */
  164. static void iproc_gpio_irq_set_mask(struct irq_data *d, bool unmask)
  165. {
  166. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  167. struct iproc_gpio *chip = gpiochip_get_data(gc);
  168. unsigned gpio = d->hwirq;
  169. iproc_set_bit(chip, IPROC_GPIO_INT_MSK_OFFSET, gpio, unmask);
  170. }
  171. static void iproc_gpio_irq_mask(struct irq_data *d)
  172. {
  173. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  174. struct iproc_gpio *chip = gpiochip_get_data(gc);
  175. unsigned long flags;
  176. spin_lock_irqsave(&chip->lock, flags);
  177. iproc_gpio_irq_set_mask(d, false);
  178. spin_unlock_irqrestore(&chip->lock, flags);
  179. }
  180. static void iproc_gpio_irq_unmask(struct irq_data *d)
  181. {
  182. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  183. struct iproc_gpio *chip = gpiochip_get_data(gc);
  184. unsigned long flags;
  185. spin_lock_irqsave(&chip->lock, flags);
  186. iproc_gpio_irq_set_mask(d, true);
  187. spin_unlock_irqrestore(&chip->lock, flags);
  188. }
  189. static int iproc_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  190. {
  191. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  192. struct iproc_gpio *chip = gpiochip_get_data(gc);
  193. unsigned gpio = d->hwirq;
  194. bool level_triggered = false;
  195. bool dual_edge = false;
  196. bool rising_or_high = false;
  197. unsigned long flags;
  198. switch (type & IRQ_TYPE_SENSE_MASK) {
  199. case IRQ_TYPE_EDGE_RISING:
  200. rising_or_high = true;
  201. break;
  202. case IRQ_TYPE_EDGE_FALLING:
  203. break;
  204. case IRQ_TYPE_EDGE_BOTH:
  205. dual_edge = true;
  206. break;
  207. case IRQ_TYPE_LEVEL_HIGH:
  208. level_triggered = true;
  209. rising_or_high = true;
  210. break;
  211. case IRQ_TYPE_LEVEL_LOW:
  212. level_triggered = true;
  213. break;
  214. default:
  215. dev_err(chip->dev, "invalid GPIO IRQ type 0x%x\n",
  216. type);
  217. return -EINVAL;
  218. }
  219. spin_lock_irqsave(&chip->lock, flags);
  220. iproc_set_bit(chip, IPROC_GPIO_INT_TYPE_OFFSET, gpio,
  221. level_triggered);
  222. iproc_set_bit(chip, IPROC_GPIO_INT_DE_OFFSET, gpio, dual_edge);
  223. iproc_set_bit(chip, IPROC_GPIO_INT_EDGE_OFFSET, gpio,
  224. rising_or_high);
  225. spin_unlock_irqrestore(&chip->lock, flags);
  226. dev_dbg(chip->dev,
  227. "gpio:%u level_triggered:%d dual_edge:%d rising_or_high:%d\n",
  228. gpio, level_triggered, dual_edge, rising_or_high);
  229. return 0;
  230. }
  231. static struct irq_chip iproc_gpio_irq_chip = {
  232. .name = "bcm-iproc-gpio",
  233. .irq_ack = iproc_gpio_irq_ack,
  234. .irq_mask = iproc_gpio_irq_mask,
  235. .irq_unmask = iproc_gpio_irq_unmask,
  236. .irq_set_type = iproc_gpio_irq_set_type,
  237. };
  238. /*
  239. * Request the Iproc IOMUX pinmux controller to mux individual pins to GPIO
  240. */
  241. static int iproc_gpio_request(struct gpio_chip *gc, unsigned offset)
  242. {
  243. struct iproc_gpio *chip = gpiochip_get_data(gc);
  244. unsigned gpio = gc->base + offset;
  245. /* not all Iproc GPIO pins can be muxed individually */
  246. if (!chip->pinmux_is_supported)
  247. return 0;
  248. return pinctrl_request_gpio(gpio);
  249. }
  250. static void iproc_gpio_free(struct gpio_chip *gc, unsigned offset)
  251. {
  252. struct iproc_gpio *chip = gpiochip_get_data(gc);
  253. unsigned gpio = gc->base + offset;
  254. if (!chip->pinmux_is_supported)
  255. return;
  256. pinctrl_free_gpio(gpio);
  257. }
  258. static int iproc_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
  259. {
  260. struct iproc_gpio *chip = gpiochip_get_data(gc);
  261. unsigned long flags;
  262. spin_lock_irqsave(&chip->lock, flags);
  263. iproc_set_bit(chip, IPROC_GPIO_OUT_EN_OFFSET, gpio, false);
  264. spin_unlock_irqrestore(&chip->lock, flags);
  265. dev_dbg(chip->dev, "gpio:%u set input\n", gpio);
  266. return 0;
  267. }
  268. static int iproc_gpio_direction_output(struct gpio_chip *gc, unsigned gpio,
  269. int val)
  270. {
  271. struct iproc_gpio *chip = gpiochip_get_data(gc);
  272. unsigned long flags;
  273. spin_lock_irqsave(&chip->lock, flags);
  274. iproc_set_bit(chip, IPROC_GPIO_OUT_EN_OFFSET, gpio, true);
  275. iproc_set_bit(chip, IPROC_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
  276. spin_unlock_irqrestore(&chip->lock, flags);
  277. dev_dbg(chip->dev, "gpio:%u set output, value:%d\n", gpio, val);
  278. return 0;
  279. }
  280. static void iproc_gpio_set(struct gpio_chip *gc, unsigned gpio, int val)
  281. {
  282. struct iproc_gpio *chip = gpiochip_get_data(gc);
  283. unsigned long flags;
  284. spin_lock_irqsave(&chip->lock, flags);
  285. iproc_set_bit(chip, IPROC_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
  286. spin_unlock_irqrestore(&chip->lock, flags);
  287. dev_dbg(chip->dev, "gpio:%u set, value:%d\n", gpio, val);
  288. }
  289. static int iproc_gpio_get(struct gpio_chip *gc, unsigned gpio)
  290. {
  291. struct iproc_gpio *chip = gpiochip_get_data(gc);
  292. unsigned int offset = IPROC_GPIO_REG(gpio,
  293. IPROC_GPIO_DATA_IN_OFFSET);
  294. unsigned int shift = IPROC_GPIO_SHIFT(gpio);
  295. return !!(readl(chip->base + offset) & BIT(shift));
  296. }
  297. static int iproc_get_groups_count(struct pinctrl_dev *pctldev)
  298. {
  299. return 1;
  300. }
  301. /*
  302. * Only one group: "gpio_grp", since this local pinctrl device only performs
  303. * GPIO specific PINCONF configurations
  304. */
  305. static const char *iproc_get_group_name(struct pinctrl_dev *pctldev,
  306. unsigned selector)
  307. {
  308. return "gpio_grp";
  309. }
  310. static const struct pinctrl_ops iproc_pctrl_ops = {
  311. .get_groups_count = iproc_get_groups_count,
  312. .get_group_name = iproc_get_group_name,
  313. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  314. .dt_free_map = pinctrl_utils_dt_free_map,
  315. };
  316. static int iproc_gpio_set_pull(struct iproc_gpio *chip, unsigned gpio,
  317. bool disable, bool pull_up)
  318. {
  319. unsigned long flags;
  320. spin_lock_irqsave(&chip->lock, flags);
  321. if (disable) {
  322. iproc_set_bit(chip, IPROC_GPIO_RES_EN_OFFSET, gpio, false);
  323. } else {
  324. iproc_set_bit(chip, IPROC_GPIO_PAD_RES_OFFSET, gpio,
  325. pull_up);
  326. iproc_set_bit(chip, IPROC_GPIO_RES_EN_OFFSET, gpio, true);
  327. }
  328. spin_unlock_irqrestore(&chip->lock, flags);
  329. dev_dbg(chip->dev, "gpio:%u set pullup:%d\n", gpio, pull_up);
  330. return 0;
  331. }
  332. static void iproc_gpio_get_pull(struct iproc_gpio *chip, unsigned gpio,
  333. bool *disable, bool *pull_up)
  334. {
  335. unsigned long flags;
  336. spin_lock_irqsave(&chip->lock, flags);
  337. *disable = !iproc_get_bit(chip, IPROC_GPIO_RES_EN_OFFSET, gpio);
  338. *pull_up = iproc_get_bit(chip, IPROC_GPIO_PAD_RES_OFFSET, gpio);
  339. spin_unlock_irqrestore(&chip->lock, flags);
  340. }
  341. static int iproc_gpio_set_strength(struct iproc_gpio *chip, unsigned gpio,
  342. unsigned strength)
  343. {
  344. void __iomem *base;
  345. unsigned int i, offset, shift;
  346. u32 val;
  347. unsigned long flags;
  348. /* make sure drive strength is supported */
  349. if (strength < 2 || strength > 16 || (strength % 2))
  350. return -ENOTSUPP;
  351. if (chip->io_ctrl) {
  352. base = chip->io_ctrl;
  353. offset = IPROC_GPIO_DRV0_CTRL_OFFSET;
  354. } else {
  355. base = chip->base;
  356. offset = IPROC_GPIO_REG(gpio,
  357. IPROC_GPIO_ASIU_DRV0_CTRL_OFFSET);
  358. }
  359. shift = IPROC_GPIO_SHIFT(gpio);
  360. dev_dbg(chip->dev, "gpio:%u set drive strength:%d mA\n", gpio,
  361. strength);
  362. spin_lock_irqsave(&chip->lock, flags);
  363. strength = (strength / 2) - 1;
  364. for (i = 0; i < GPIO_DRV_STRENGTH_BITS; i++) {
  365. val = readl(base + offset);
  366. val &= ~BIT(shift);
  367. val |= ((strength >> i) & 0x1) << shift;
  368. writel(val, base + offset);
  369. offset += 4;
  370. }
  371. spin_unlock_irqrestore(&chip->lock, flags);
  372. return 0;
  373. }
  374. static int iproc_gpio_get_strength(struct iproc_gpio *chip, unsigned gpio,
  375. u16 *strength)
  376. {
  377. void __iomem *base;
  378. unsigned int i, offset, shift;
  379. u32 val;
  380. unsigned long flags;
  381. if (chip->io_ctrl) {
  382. base = chip->io_ctrl;
  383. offset = IPROC_GPIO_DRV0_CTRL_OFFSET;
  384. } else {
  385. base = chip->base;
  386. offset = IPROC_GPIO_REG(gpio,
  387. IPROC_GPIO_ASIU_DRV0_CTRL_OFFSET);
  388. }
  389. shift = IPROC_GPIO_SHIFT(gpio);
  390. spin_lock_irqsave(&chip->lock, flags);
  391. *strength = 0;
  392. for (i = 0; i < GPIO_DRV_STRENGTH_BITS; i++) {
  393. val = readl(base + offset) & BIT(shift);
  394. val >>= shift;
  395. *strength += (val << i);
  396. offset += 4;
  397. }
  398. /* convert to mA */
  399. *strength = (*strength + 1) * 2;
  400. spin_unlock_irqrestore(&chip->lock, flags);
  401. return 0;
  402. }
  403. static int iproc_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
  404. unsigned long *config)
  405. {
  406. struct iproc_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
  407. enum pin_config_param param = pinconf_to_config_param(*config);
  408. unsigned gpio = iproc_pin_to_gpio(pin);
  409. u16 arg;
  410. bool disable, pull_up;
  411. int ret;
  412. switch (param) {
  413. case PIN_CONFIG_BIAS_DISABLE:
  414. iproc_gpio_get_pull(chip, gpio, &disable, &pull_up);
  415. if (disable)
  416. return 0;
  417. else
  418. return -EINVAL;
  419. case PIN_CONFIG_BIAS_PULL_UP:
  420. iproc_gpio_get_pull(chip, gpio, &disable, &pull_up);
  421. if (!disable && pull_up)
  422. return 0;
  423. else
  424. return -EINVAL;
  425. case PIN_CONFIG_BIAS_PULL_DOWN:
  426. iproc_gpio_get_pull(chip, gpio, &disable, &pull_up);
  427. if (!disable && !pull_up)
  428. return 0;
  429. else
  430. return -EINVAL;
  431. case PIN_CONFIG_DRIVE_STRENGTH:
  432. ret = iproc_gpio_get_strength(chip, gpio, &arg);
  433. if (ret)
  434. return ret;
  435. *config = pinconf_to_config_packed(param, arg);
  436. return 0;
  437. default:
  438. return -ENOTSUPP;
  439. }
  440. return -ENOTSUPP;
  441. }
  442. static int iproc_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
  443. unsigned long *configs, unsigned num_configs)
  444. {
  445. struct iproc_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
  446. enum pin_config_param param;
  447. u16 arg;
  448. unsigned i, gpio = iproc_pin_to_gpio(pin);
  449. int ret = -ENOTSUPP;
  450. for (i = 0; i < num_configs; i++) {
  451. param = pinconf_to_config_param(configs[i]);
  452. arg = pinconf_to_config_argument(configs[i]);
  453. switch (param) {
  454. case PIN_CONFIG_BIAS_DISABLE:
  455. ret = iproc_gpio_set_pull(chip, gpio, true, false);
  456. if (ret < 0)
  457. goto out;
  458. break;
  459. case PIN_CONFIG_BIAS_PULL_UP:
  460. ret = iproc_gpio_set_pull(chip, gpio, false, true);
  461. if (ret < 0)
  462. goto out;
  463. break;
  464. case PIN_CONFIG_BIAS_PULL_DOWN:
  465. ret = iproc_gpio_set_pull(chip, gpio, false, false);
  466. if (ret < 0)
  467. goto out;
  468. break;
  469. case PIN_CONFIG_DRIVE_STRENGTH:
  470. ret = iproc_gpio_set_strength(chip, gpio, arg);
  471. if (ret < 0)
  472. goto out;
  473. break;
  474. default:
  475. dev_err(chip->dev, "invalid configuration\n");
  476. return -ENOTSUPP;
  477. }
  478. } /* for each config */
  479. out:
  480. return ret;
  481. }
  482. static const struct pinconf_ops iproc_pconf_ops = {
  483. .is_generic = true,
  484. .pin_config_get = iproc_pin_config_get,
  485. .pin_config_set = iproc_pin_config_set,
  486. };
  487. /*
  488. * Iproc GPIO controller supports some PINCONF related configurations such as
  489. * pull up, pull down, and drive strength, when the pin is configured to GPIO
  490. *
  491. * Here a local pinctrl device is created with simple 1-to-1 pin mapping to the
  492. * local GPIO pins
  493. */
  494. static int iproc_gpio_register_pinconf(struct iproc_gpio *chip)
  495. {
  496. struct pinctrl_desc *pctldesc = &chip->pctldesc;
  497. struct pinctrl_pin_desc *pins;
  498. struct gpio_chip *gc = &chip->gc;
  499. int i;
  500. pins = devm_kcalloc(chip->dev, gc->ngpio, sizeof(*pins), GFP_KERNEL);
  501. if (!pins)
  502. return -ENOMEM;
  503. for (i = 0; i < gc->ngpio; i++) {
  504. pins[i].number = i;
  505. pins[i].name = devm_kasprintf(chip->dev, GFP_KERNEL,
  506. "gpio-%d", i);
  507. if (!pins[i].name)
  508. return -ENOMEM;
  509. }
  510. pctldesc->name = dev_name(chip->dev);
  511. pctldesc->pctlops = &iproc_pctrl_ops;
  512. pctldesc->pins = pins;
  513. pctldesc->npins = gc->ngpio;
  514. pctldesc->confops = &iproc_pconf_ops;
  515. chip->pctl = pinctrl_register(pctldesc, chip->dev, chip);
  516. if (IS_ERR(chip->pctl)) {
  517. dev_err(chip->dev, "unable to register pinctrl device\n");
  518. return PTR_ERR(chip->pctl);
  519. }
  520. return 0;
  521. }
  522. static void iproc_gpio_unregister_pinconf(struct iproc_gpio *chip)
  523. {
  524. pinctrl_unregister(chip->pctl);
  525. }
  526. static const struct of_device_id iproc_gpio_of_match[] = {
  527. { .compatible = "brcm,cygnus-ccm-gpio" },
  528. { .compatible = "brcm,cygnus-asiu-gpio" },
  529. { .compatible = "brcm,cygnus-crmu-gpio" },
  530. { .compatible = "brcm,iproc-gpio" },
  531. { }
  532. };
  533. static int iproc_gpio_probe(struct platform_device *pdev)
  534. {
  535. struct device *dev = &pdev->dev;
  536. struct resource *res;
  537. struct iproc_gpio *chip;
  538. struct gpio_chip *gc;
  539. u32 ngpios;
  540. int irq, ret;
  541. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  542. if (!chip)
  543. return -ENOMEM;
  544. chip->dev = dev;
  545. platform_set_drvdata(pdev, chip);
  546. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  547. chip->base = devm_ioremap_resource(dev, res);
  548. if (IS_ERR(chip->base)) {
  549. dev_err(dev, "unable to map I/O memory\n");
  550. return PTR_ERR(chip->base);
  551. }
  552. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  553. if (res) {
  554. chip->io_ctrl = devm_ioremap_resource(dev, res);
  555. if (IS_ERR(chip->io_ctrl)) {
  556. dev_err(dev, "unable to map I/O memory\n");
  557. return PTR_ERR(chip->io_ctrl);
  558. }
  559. }
  560. if (of_property_read_u32(dev->of_node, "ngpios", &ngpios)) {
  561. dev_err(&pdev->dev, "missing ngpios DT property\n");
  562. return -ENODEV;
  563. }
  564. spin_lock_init(&chip->lock);
  565. gc = &chip->gc;
  566. gc->base = -1;
  567. gc->ngpio = ngpios;
  568. chip->num_banks = (ngpios + NGPIOS_PER_BANK - 1) / NGPIOS_PER_BANK;
  569. gc->label = dev_name(dev);
  570. gc->parent = dev;
  571. gc->of_node = dev->of_node;
  572. gc->request = iproc_gpio_request;
  573. gc->free = iproc_gpio_free;
  574. gc->direction_input = iproc_gpio_direction_input;
  575. gc->direction_output = iproc_gpio_direction_output;
  576. gc->set = iproc_gpio_set;
  577. gc->get = iproc_gpio_get;
  578. chip->pinmux_is_supported = of_property_read_bool(dev->of_node,
  579. "gpio-ranges");
  580. ret = gpiochip_add_data(gc, chip);
  581. if (ret < 0) {
  582. dev_err(dev, "unable to add GPIO chip\n");
  583. return ret;
  584. }
  585. ret = iproc_gpio_register_pinconf(chip);
  586. if (ret) {
  587. dev_err(dev, "unable to register pinconf\n");
  588. goto err_rm_gpiochip;
  589. }
  590. /* optional GPIO interrupt support */
  591. irq = platform_get_irq(pdev, 0);
  592. if (irq) {
  593. ret = gpiochip_irqchip_add(gc, &iproc_gpio_irq_chip, 0,
  594. handle_simple_irq, IRQ_TYPE_NONE);
  595. if (ret) {
  596. dev_err(dev, "no GPIO irqchip\n");
  597. goto err_unregister_pinconf;
  598. }
  599. gpiochip_set_chained_irqchip(gc, &iproc_gpio_irq_chip, irq,
  600. iproc_gpio_irq_handler);
  601. }
  602. return 0;
  603. err_unregister_pinconf:
  604. iproc_gpio_unregister_pinconf(chip);
  605. err_rm_gpiochip:
  606. gpiochip_remove(gc);
  607. return ret;
  608. }
  609. static struct platform_driver iproc_gpio_driver = {
  610. .driver = {
  611. .name = "iproc-gpio",
  612. .of_match_table = iproc_gpio_of_match,
  613. },
  614. .probe = iproc_gpio_probe,
  615. };
  616. static int __init iproc_gpio_init(void)
  617. {
  618. return platform_driver_probe(&iproc_gpio_driver, iproc_gpio_probe);
  619. }
  620. arch_initcall_sync(iproc_gpio_init);