pinctrl-iproc-gpio.c 22 KB

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