pinctrl-iproc-gpio.c 24 KB

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