pinctrl-iproc-gpio.c 24 KB

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