pinctrl-iproc-gpio.c 23 KB

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