pinctrl-iproc-gpio.c 22 KB

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