pinctrl-cygnus-gpio.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. * Copyright (C) 2014-2015 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * This file contains the Broadcom Cygnus GPIO driver that supports 3
  14. * GPIO controllers on Cygnus including the ASIU GPIO controller, the
  15. * chipCommonG GPIO controller, and the always-on GPIO controller. Basic
  16. * PINCONF such as bias pull up/down, and drive strength are also supported
  17. * in this driver.
  18. *
  19. * Pins from the ASIU GPIO can be individually muxed to GPIO function,
  20. * through the interaction with the Cygnus IOMUX controller
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/gpio.h>
  27. #include <linux/ioport.h>
  28. #include <linux/of_device.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/pinctrl/pinctrl.h>
  31. #include <linux/pinctrl/pinmux.h>
  32. #include <linux/pinctrl/pinconf.h>
  33. #include <linux/pinctrl/pinconf-generic.h>
  34. #include "../pinctrl-utils.h"
  35. #define CYGNUS_GPIO_DATA_IN_OFFSET 0x00
  36. #define CYGNUS_GPIO_DATA_OUT_OFFSET 0x04
  37. #define CYGNUS_GPIO_OUT_EN_OFFSET 0x08
  38. #define CYGNUS_GPIO_IN_TYPE_OFFSET 0x0c
  39. #define CYGNUS_GPIO_INT_DE_OFFSET 0x10
  40. #define CYGNUS_GPIO_INT_EDGE_OFFSET 0x14
  41. #define CYGNUS_GPIO_INT_MSK_OFFSET 0x18
  42. #define CYGNUS_GPIO_INT_STAT_OFFSET 0x1c
  43. #define CYGNUS_GPIO_INT_MSTAT_OFFSET 0x20
  44. #define CYGNUS_GPIO_INT_CLR_OFFSET 0x24
  45. #define CYGNUS_GPIO_PAD_RES_OFFSET 0x34
  46. #define CYGNUS_GPIO_RES_EN_OFFSET 0x38
  47. /* drive strength control for ASIU GPIO */
  48. #define CYGNUS_GPIO_ASIU_DRV0_CTRL_OFFSET 0x58
  49. /* drive strength control for CCM/CRMU (AON) GPIO */
  50. #define CYGNUS_GPIO_DRV0_CTRL_OFFSET 0x00
  51. #define GPIO_BANK_SIZE 0x200
  52. #define NGPIOS_PER_BANK 32
  53. #define GPIO_BANK(pin) ((pin) / NGPIOS_PER_BANK)
  54. #define CYGNUS_GPIO_REG(pin, reg) (GPIO_BANK(pin) * GPIO_BANK_SIZE + (reg))
  55. #define CYGNUS_GPIO_SHIFT(pin) ((pin) % NGPIOS_PER_BANK)
  56. #define GPIO_DRV_STRENGTH_BIT_SHIFT 20
  57. #define GPIO_DRV_STRENGTH_BITS 3
  58. #define GPIO_DRV_STRENGTH_BIT_MASK ((1 << GPIO_DRV_STRENGTH_BITS) - 1)
  59. /*
  60. * Cygnus GPIO core
  61. *
  62. * @dev: pointer to device
  63. * @base: I/O register base for Cygnus GPIO controller
  64. * @io_ctrl: I/O register base for certain type of Cygnus GPIO controller that
  65. * has the PINCONF support implemented outside of the GPIO block
  66. * @lock: lock to protect access to I/O registers
  67. * @gc: GPIO chip
  68. * @num_banks: number of GPIO banks, each bank supports up to 32 GPIOs
  69. * @pinmux_is_supported: flag to indicate this GPIO controller contains pins
  70. * that can be individually muxed to GPIO
  71. * @pctl: pointer to pinctrl_dev
  72. * @pctldesc: pinctrl descriptor
  73. */
  74. struct cygnus_gpio {
  75. struct device *dev;
  76. void __iomem *base;
  77. void __iomem *io_ctrl;
  78. spinlock_t lock;
  79. struct gpio_chip gc;
  80. unsigned num_banks;
  81. bool pinmux_is_supported;
  82. struct pinctrl_dev *pctl;
  83. struct pinctrl_desc pctldesc;
  84. };
  85. static inline struct cygnus_gpio *to_cygnus_gpio(struct gpio_chip *gc)
  86. {
  87. return container_of(gc, struct cygnus_gpio, gc);
  88. }
  89. /*
  90. * Mapping from PINCONF pins to GPIO pins is 1-to-1
  91. */
  92. static inline unsigned cygnus_pin_to_gpio(unsigned pin)
  93. {
  94. return pin;
  95. }
  96. /**
  97. * cygnus_set_bit - set or clear one bit (corresponding to the GPIO pin) in a
  98. * Cygnus GPIO register
  99. *
  100. * @cygnus_gpio: Cygnus GPIO device
  101. * @reg: register offset
  102. * @gpio: GPIO pin
  103. * @set: set or clear
  104. */
  105. static inline void cygnus_set_bit(struct cygnus_gpio *chip, unsigned int reg,
  106. unsigned gpio, bool set)
  107. {
  108. unsigned int offset = CYGNUS_GPIO_REG(gpio, reg);
  109. unsigned int shift = CYGNUS_GPIO_SHIFT(gpio);
  110. u32 val;
  111. val = readl(chip->base + offset);
  112. if (set)
  113. val |= BIT(shift);
  114. else
  115. val &= ~BIT(shift);
  116. writel(val, chip->base + offset);
  117. }
  118. static inline bool cygnus_get_bit(struct cygnus_gpio *chip, unsigned int reg,
  119. unsigned gpio)
  120. {
  121. unsigned int offset = CYGNUS_GPIO_REG(gpio, reg);
  122. unsigned int shift = CYGNUS_GPIO_SHIFT(gpio);
  123. return !!(readl(chip->base + offset) & BIT(shift));
  124. }
  125. static void cygnus_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  126. {
  127. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  128. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  129. struct irq_chip *irq_chip = irq_desc_get_chip(desc);
  130. int i, bit;
  131. chained_irq_enter(irq_chip, desc);
  132. /* go through the entire GPIO banks and handle all interrupts */
  133. for (i = 0; i < chip->num_banks; i++) {
  134. unsigned long val = readl(chip->base + (i * GPIO_BANK_SIZE) +
  135. CYGNUS_GPIO_INT_MSTAT_OFFSET);
  136. for_each_set_bit(bit, &val, NGPIOS_PER_BANK) {
  137. unsigned pin = NGPIOS_PER_BANK * i + bit;
  138. int child_irq = irq_find_mapping(gc->irqdomain, pin);
  139. /*
  140. * Clear the interrupt before invoking the
  141. * handler, so we do not leave any window
  142. */
  143. writel(BIT(bit), chip->base + (i * GPIO_BANK_SIZE) +
  144. CYGNUS_GPIO_INT_CLR_OFFSET);
  145. generic_handle_irq(child_irq);
  146. }
  147. }
  148. chained_irq_exit(irq_chip, desc);
  149. }
  150. static void cygnus_gpio_irq_ack(struct irq_data *d)
  151. {
  152. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  153. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  154. unsigned gpio = d->hwirq;
  155. unsigned int offset = CYGNUS_GPIO_REG(gpio,
  156. CYGNUS_GPIO_INT_CLR_OFFSET);
  157. unsigned int shift = CYGNUS_GPIO_SHIFT(gpio);
  158. u32 val = BIT(shift);
  159. writel(val, chip->base + offset);
  160. }
  161. /**
  162. * cygnus_gpio_irq_set_mask - mask/unmask a GPIO interrupt
  163. *
  164. * @d: IRQ chip data
  165. * @unmask: mask/unmask GPIO interrupt
  166. */
  167. static void cygnus_gpio_irq_set_mask(struct irq_data *d, bool unmask)
  168. {
  169. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  170. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  171. unsigned gpio = d->hwirq;
  172. cygnus_set_bit(chip, CYGNUS_GPIO_INT_MSK_OFFSET, gpio, unmask);
  173. }
  174. static void cygnus_gpio_irq_mask(struct irq_data *d)
  175. {
  176. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  177. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  178. unsigned long flags;
  179. spin_lock_irqsave(&chip->lock, flags);
  180. cygnus_gpio_irq_set_mask(d, false);
  181. spin_unlock_irqrestore(&chip->lock, flags);
  182. }
  183. static void cygnus_gpio_irq_unmask(struct irq_data *d)
  184. {
  185. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  186. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  187. unsigned long flags;
  188. spin_lock_irqsave(&chip->lock, flags);
  189. cygnus_gpio_irq_set_mask(d, true);
  190. spin_unlock_irqrestore(&chip->lock, flags);
  191. }
  192. static int cygnus_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  193. {
  194. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  195. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  196. unsigned gpio = d->hwirq;
  197. bool level_triggered = false;
  198. bool dual_edge = false;
  199. bool rising_or_high = false;
  200. unsigned long flags;
  201. switch (type & IRQ_TYPE_SENSE_MASK) {
  202. case IRQ_TYPE_EDGE_RISING:
  203. rising_or_high = true;
  204. break;
  205. case IRQ_TYPE_EDGE_FALLING:
  206. break;
  207. case IRQ_TYPE_EDGE_BOTH:
  208. dual_edge = true;
  209. break;
  210. case IRQ_TYPE_LEVEL_HIGH:
  211. level_triggered = true;
  212. rising_or_high = true;
  213. break;
  214. case IRQ_TYPE_LEVEL_LOW:
  215. level_triggered = true;
  216. break;
  217. default:
  218. dev_err(chip->dev, "invalid GPIO IRQ type 0x%x\n",
  219. type);
  220. return -EINVAL;
  221. }
  222. spin_lock_irqsave(&chip->lock, flags);
  223. cygnus_set_bit(chip, CYGNUS_GPIO_IN_TYPE_OFFSET, gpio,
  224. level_triggered);
  225. cygnus_set_bit(chip, CYGNUS_GPIO_INT_DE_OFFSET, gpio, dual_edge);
  226. cygnus_set_bit(chip, CYGNUS_GPIO_INT_EDGE_OFFSET, gpio,
  227. rising_or_high);
  228. spin_unlock_irqrestore(&chip->lock, flags);
  229. dev_dbg(chip->dev,
  230. "gpio:%u level_triggered:%d dual_edge:%d rising_or_high:%d\n",
  231. gpio, level_triggered, dual_edge, rising_or_high);
  232. return 0;
  233. }
  234. static struct irq_chip cygnus_gpio_irq_chip = {
  235. .name = "bcm-cygnus-gpio",
  236. .irq_ack = cygnus_gpio_irq_ack,
  237. .irq_mask = cygnus_gpio_irq_mask,
  238. .irq_unmask = cygnus_gpio_irq_unmask,
  239. .irq_set_type = cygnus_gpio_irq_set_type,
  240. };
  241. /*
  242. * Request the Cygnus IOMUX pinmux controller to mux individual pins to GPIO
  243. */
  244. static int cygnus_gpio_request(struct gpio_chip *gc, unsigned offset)
  245. {
  246. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  247. unsigned gpio = gc->base + offset;
  248. /* not all Cygnus GPIO pins can be muxed individually */
  249. if (!chip->pinmux_is_supported)
  250. return 0;
  251. return pinctrl_request_gpio(gpio);
  252. }
  253. static void cygnus_gpio_free(struct gpio_chip *gc, unsigned offset)
  254. {
  255. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  256. unsigned gpio = gc->base + offset;
  257. if (!chip->pinmux_is_supported)
  258. return;
  259. pinctrl_free_gpio(gpio);
  260. }
  261. static int cygnus_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
  262. {
  263. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  264. unsigned long flags;
  265. spin_lock_irqsave(&chip->lock, flags);
  266. cygnus_set_bit(chip, CYGNUS_GPIO_OUT_EN_OFFSET, gpio, false);
  267. spin_unlock_irqrestore(&chip->lock, flags);
  268. dev_dbg(chip->dev, "gpio:%u set input\n", gpio);
  269. return 0;
  270. }
  271. static int cygnus_gpio_direction_output(struct gpio_chip *gc, unsigned gpio,
  272. int val)
  273. {
  274. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  275. unsigned long flags;
  276. spin_lock_irqsave(&chip->lock, flags);
  277. cygnus_set_bit(chip, CYGNUS_GPIO_OUT_EN_OFFSET, gpio, true);
  278. cygnus_set_bit(chip, CYGNUS_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
  279. spin_unlock_irqrestore(&chip->lock, flags);
  280. dev_dbg(chip->dev, "gpio:%u set output, value:%d\n", gpio, val);
  281. return 0;
  282. }
  283. static void cygnus_gpio_set(struct gpio_chip *gc, unsigned gpio, int val)
  284. {
  285. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  286. unsigned long flags;
  287. spin_lock_irqsave(&chip->lock, flags);
  288. cygnus_set_bit(chip, CYGNUS_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
  289. spin_unlock_irqrestore(&chip->lock, flags);
  290. dev_dbg(chip->dev, "gpio:%u set, value:%d\n", gpio, val);
  291. }
  292. static int cygnus_gpio_get(struct gpio_chip *gc, unsigned gpio)
  293. {
  294. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  295. unsigned int offset = CYGNUS_GPIO_REG(gpio,
  296. CYGNUS_GPIO_DATA_IN_OFFSET);
  297. unsigned int shift = CYGNUS_GPIO_SHIFT(gpio);
  298. return !!(readl(chip->base + offset) & BIT(shift));
  299. }
  300. static int cygnus_get_groups_count(struct pinctrl_dev *pctldev)
  301. {
  302. return 1;
  303. }
  304. /*
  305. * Only one group: "gpio_grp", since this local pinctrl device only performs
  306. * GPIO specific PINCONF configurations
  307. */
  308. static const char *cygnus_get_group_name(struct pinctrl_dev *pctldev,
  309. unsigned selector)
  310. {
  311. return "gpio_grp";
  312. }
  313. static const struct pinctrl_ops cygnus_pctrl_ops = {
  314. .get_groups_count = cygnus_get_groups_count,
  315. .get_group_name = cygnus_get_group_name,
  316. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  317. .dt_free_map = pinctrl_utils_dt_free_map,
  318. };
  319. static int cygnus_gpio_set_pull(struct cygnus_gpio *chip, unsigned gpio,
  320. bool disable, bool pull_up)
  321. {
  322. unsigned long flags;
  323. spin_lock_irqsave(&chip->lock, flags);
  324. if (disable) {
  325. cygnus_set_bit(chip, CYGNUS_GPIO_RES_EN_OFFSET, gpio, false);
  326. } else {
  327. cygnus_set_bit(chip, CYGNUS_GPIO_PAD_RES_OFFSET, gpio,
  328. pull_up);
  329. cygnus_set_bit(chip, CYGNUS_GPIO_RES_EN_OFFSET, gpio, true);
  330. }
  331. spin_unlock_irqrestore(&chip->lock, flags);
  332. dev_dbg(chip->dev, "gpio:%u set pullup:%d\n", gpio, pull_up);
  333. return 0;
  334. }
  335. static void cygnus_gpio_get_pull(struct cygnus_gpio *chip, unsigned gpio,
  336. bool *disable, bool *pull_up)
  337. {
  338. unsigned long flags;
  339. spin_lock_irqsave(&chip->lock, flags);
  340. *disable = !cygnus_get_bit(chip, CYGNUS_GPIO_RES_EN_OFFSET, gpio);
  341. *pull_up = cygnus_get_bit(chip, CYGNUS_GPIO_PAD_RES_OFFSET, gpio);
  342. spin_unlock_irqrestore(&chip->lock, flags);
  343. }
  344. static int cygnus_gpio_set_strength(struct cygnus_gpio *chip, unsigned gpio,
  345. unsigned strength)
  346. {
  347. void __iomem *base;
  348. unsigned int i, offset, shift;
  349. u32 val;
  350. unsigned long flags;
  351. /* make sure drive strength is supported */
  352. if (strength < 2 || strength > 16 || (strength % 2))
  353. return -ENOTSUPP;
  354. if (chip->io_ctrl) {
  355. base = chip->io_ctrl;
  356. offset = CYGNUS_GPIO_DRV0_CTRL_OFFSET;
  357. } else {
  358. base = chip->base;
  359. offset = CYGNUS_GPIO_REG(gpio,
  360. CYGNUS_GPIO_ASIU_DRV0_CTRL_OFFSET);
  361. }
  362. shift = CYGNUS_GPIO_SHIFT(gpio);
  363. dev_dbg(chip->dev, "gpio:%u set drive strength:%d mA\n", gpio,
  364. strength);
  365. spin_lock_irqsave(&chip->lock, flags);
  366. strength = (strength / 2) - 1;
  367. for (i = 0; i < GPIO_DRV_STRENGTH_BITS; i++) {
  368. val = readl(base + offset);
  369. val &= ~BIT(shift);
  370. val |= ((strength >> i) & 0x1) << shift;
  371. writel(val, base + offset);
  372. offset += 4;
  373. }
  374. spin_unlock_irqrestore(&chip->lock, flags);
  375. return 0;
  376. }
  377. static int cygnus_gpio_get_strength(struct cygnus_gpio *chip, unsigned gpio,
  378. u16 *strength)
  379. {
  380. void __iomem *base;
  381. unsigned int i, offset, shift;
  382. u32 val;
  383. unsigned long flags;
  384. if (chip->io_ctrl) {
  385. base = chip->io_ctrl;
  386. offset = CYGNUS_GPIO_DRV0_CTRL_OFFSET;
  387. } else {
  388. base = chip->base;
  389. offset = CYGNUS_GPIO_REG(gpio,
  390. CYGNUS_GPIO_ASIU_DRV0_CTRL_OFFSET);
  391. }
  392. shift = CYGNUS_GPIO_SHIFT(gpio);
  393. spin_lock_irqsave(&chip->lock, flags);
  394. *strength = 0;
  395. for (i = 0; i < GPIO_DRV_STRENGTH_BITS; i++) {
  396. val = readl(base + offset) & BIT(shift);
  397. val >>= shift;
  398. *strength += (val << i);
  399. offset += 4;
  400. }
  401. /* convert to mA */
  402. *strength = (*strength + 1) * 2;
  403. spin_unlock_irqrestore(&chip->lock, flags);
  404. return 0;
  405. }
  406. static int cygnus_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
  407. unsigned long *config)
  408. {
  409. struct cygnus_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
  410. enum pin_config_param param = pinconf_to_config_param(*config);
  411. unsigned gpio = cygnus_pin_to_gpio(pin);
  412. u16 arg;
  413. bool disable, pull_up;
  414. int ret;
  415. switch (param) {
  416. case PIN_CONFIG_BIAS_DISABLE:
  417. cygnus_gpio_get_pull(chip, gpio, &disable, &pull_up);
  418. if (disable)
  419. return 0;
  420. else
  421. return -EINVAL;
  422. case PIN_CONFIG_BIAS_PULL_UP:
  423. cygnus_gpio_get_pull(chip, gpio, &disable, &pull_up);
  424. if (!disable && pull_up)
  425. return 0;
  426. else
  427. return -EINVAL;
  428. case PIN_CONFIG_BIAS_PULL_DOWN:
  429. cygnus_gpio_get_pull(chip, gpio, &disable, &pull_up);
  430. if (!disable && !pull_up)
  431. return 0;
  432. else
  433. return -EINVAL;
  434. case PIN_CONFIG_DRIVE_STRENGTH:
  435. ret = cygnus_gpio_get_strength(chip, gpio, &arg);
  436. if (ret)
  437. return ret;
  438. else
  439. *config = pinconf_to_config_packed(param, arg);
  440. return 0;
  441. default:
  442. return -ENOTSUPP;
  443. }
  444. return -ENOTSUPP;
  445. }
  446. static int cygnus_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
  447. unsigned long *configs, unsigned num_configs)
  448. {
  449. struct cygnus_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
  450. enum pin_config_param param;
  451. u16 arg;
  452. unsigned i, gpio = cygnus_pin_to_gpio(pin);
  453. int ret = -ENOTSUPP;
  454. for (i = 0; i < num_configs; i++) {
  455. param = pinconf_to_config_param(configs[i]);
  456. arg = pinconf_to_config_argument(configs[i]);
  457. switch (param) {
  458. case PIN_CONFIG_BIAS_DISABLE:
  459. ret = cygnus_gpio_set_pull(chip, gpio, true, false);
  460. if (ret < 0)
  461. goto out;
  462. break;
  463. case PIN_CONFIG_BIAS_PULL_UP:
  464. ret = cygnus_gpio_set_pull(chip, gpio, false, true);
  465. if (ret < 0)
  466. goto out;
  467. break;
  468. case PIN_CONFIG_BIAS_PULL_DOWN:
  469. ret = cygnus_gpio_set_pull(chip, gpio, false, false);
  470. if (ret < 0)
  471. goto out;
  472. break;
  473. case PIN_CONFIG_DRIVE_STRENGTH:
  474. ret = cygnus_gpio_set_strength(chip, gpio, arg);
  475. if (ret < 0)
  476. goto out;
  477. break;
  478. default:
  479. dev_err(chip->dev, "invalid configuration\n");
  480. return -ENOTSUPP;
  481. }
  482. } /* for each config */
  483. out:
  484. return ret;
  485. }
  486. static const struct pinconf_ops cygnus_pconf_ops = {
  487. .is_generic = true,
  488. .pin_config_get = cygnus_pin_config_get,
  489. .pin_config_set = cygnus_pin_config_set,
  490. };
  491. /*
  492. * Map a GPIO in the local gpio_chip pin space to a pin in the Cygnus IOMUX
  493. * pinctrl pin space
  494. */
  495. struct cygnus_gpio_pin_range {
  496. unsigned offset;
  497. unsigned pin_base;
  498. unsigned num_pins;
  499. };
  500. #define CYGNUS_PINRANGE(o, p, n) { .offset = o, .pin_base = p, .num_pins = n }
  501. /*
  502. * Pin mapping table for mapping local GPIO pins to Cygnus IOMUX pinctrl pins
  503. */
  504. static const struct cygnus_gpio_pin_range cygnus_gpio_pintable[] = {
  505. CYGNUS_PINRANGE(0, 42, 1),
  506. CYGNUS_PINRANGE(1, 44, 3),
  507. CYGNUS_PINRANGE(4, 48, 1),
  508. CYGNUS_PINRANGE(5, 50, 3),
  509. CYGNUS_PINRANGE(8, 126, 1),
  510. CYGNUS_PINRANGE(9, 155, 1),
  511. CYGNUS_PINRANGE(10, 152, 1),
  512. CYGNUS_PINRANGE(11, 154, 1),
  513. CYGNUS_PINRANGE(12, 153, 1),
  514. CYGNUS_PINRANGE(13, 127, 3),
  515. CYGNUS_PINRANGE(16, 140, 1),
  516. CYGNUS_PINRANGE(17, 145, 7),
  517. CYGNUS_PINRANGE(24, 130, 10),
  518. CYGNUS_PINRANGE(34, 141, 4),
  519. CYGNUS_PINRANGE(38, 54, 1),
  520. CYGNUS_PINRANGE(39, 56, 3),
  521. CYGNUS_PINRANGE(42, 60, 3),
  522. CYGNUS_PINRANGE(45, 64, 3),
  523. CYGNUS_PINRANGE(48, 68, 2),
  524. CYGNUS_PINRANGE(50, 84, 6),
  525. CYGNUS_PINRANGE(56, 94, 6),
  526. CYGNUS_PINRANGE(62, 72, 1),
  527. CYGNUS_PINRANGE(63, 70, 1),
  528. CYGNUS_PINRANGE(64, 80, 1),
  529. CYGNUS_PINRANGE(65, 74, 3),
  530. CYGNUS_PINRANGE(68, 78, 1),
  531. CYGNUS_PINRANGE(69, 82, 1),
  532. CYGNUS_PINRANGE(70, 156, 17),
  533. CYGNUS_PINRANGE(87, 104, 12),
  534. CYGNUS_PINRANGE(99, 102, 2),
  535. CYGNUS_PINRANGE(101, 90, 4),
  536. CYGNUS_PINRANGE(105, 116, 6),
  537. CYGNUS_PINRANGE(111, 100, 2),
  538. CYGNUS_PINRANGE(113, 122, 4),
  539. CYGNUS_PINRANGE(123, 11, 1),
  540. CYGNUS_PINRANGE(124, 38, 4),
  541. CYGNUS_PINRANGE(128, 43, 1),
  542. CYGNUS_PINRANGE(129, 47, 1),
  543. CYGNUS_PINRANGE(130, 49, 1),
  544. CYGNUS_PINRANGE(131, 53, 1),
  545. CYGNUS_PINRANGE(132, 55, 1),
  546. CYGNUS_PINRANGE(133, 59, 1),
  547. CYGNUS_PINRANGE(134, 63, 1),
  548. CYGNUS_PINRANGE(135, 67, 1),
  549. CYGNUS_PINRANGE(136, 71, 1),
  550. CYGNUS_PINRANGE(137, 73, 1),
  551. CYGNUS_PINRANGE(138, 77, 1),
  552. CYGNUS_PINRANGE(139, 79, 1),
  553. CYGNUS_PINRANGE(140, 81, 1),
  554. CYGNUS_PINRANGE(141, 83, 1),
  555. CYGNUS_PINRANGE(142, 10, 1)
  556. };
  557. /*
  558. * The Cygnus IOMUX controller mainly supports group based mux configuration,
  559. * but certain pins can be muxed to GPIO individually. Only the ASIU GPIO
  560. * controller can support this, so it's an optional configuration
  561. *
  562. * Return -ENODEV means no support and that's fine
  563. */
  564. static int cygnus_gpio_pinmux_add_range(struct cygnus_gpio *chip)
  565. {
  566. struct device_node *node = chip->dev->of_node;
  567. struct device_node *pinmux_node;
  568. struct platform_device *pinmux_pdev;
  569. struct gpio_chip *gc = &chip->gc;
  570. int i, ret = 0;
  571. /* parse DT to find the phandle to the pinmux controller */
  572. pinmux_node = of_parse_phandle(node, "pinmux", 0);
  573. if (!pinmux_node)
  574. return -ENODEV;
  575. pinmux_pdev = of_find_device_by_node(pinmux_node);
  576. /* no longer need the pinmux node */
  577. of_node_put(pinmux_node);
  578. if (!pinmux_pdev) {
  579. dev_err(chip->dev, "failed to get pinmux device\n");
  580. return -EINVAL;
  581. }
  582. /* now need to create the mapping between local GPIO and PINMUX pins */
  583. for (i = 0; i < ARRAY_SIZE(cygnus_gpio_pintable); i++) {
  584. ret = gpiochip_add_pin_range(gc, dev_name(&pinmux_pdev->dev),
  585. cygnus_gpio_pintable[i].offset,
  586. cygnus_gpio_pintable[i].pin_base,
  587. cygnus_gpio_pintable[i].num_pins);
  588. if (ret) {
  589. dev_err(chip->dev, "unable to add GPIO pin range\n");
  590. goto err_put_device;
  591. }
  592. }
  593. chip->pinmux_is_supported = true;
  594. /* no need for pinmux_pdev device reference anymore */
  595. put_device(&pinmux_pdev->dev);
  596. return 0;
  597. err_put_device:
  598. put_device(&pinmux_pdev->dev);
  599. gpiochip_remove_pin_ranges(gc);
  600. return ret;
  601. }
  602. /*
  603. * Cygnus GPIO controller supports some PINCONF related configurations such as
  604. * pull up, pull down, and drive strength, when the pin is configured to GPIO
  605. *
  606. * Here a local pinctrl device is created with simple 1-to-1 pin mapping to the
  607. * local GPIO pins
  608. */
  609. static int cygnus_gpio_register_pinconf(struct cygnus_gpio *chip)
  610. {
  611. struct pinctrl_desc *pctldesc = &chip->pctldesc;
  612. struct pinctrl_pin_desc *pins;
  613. struct gpio_chip *gc = &chip->gc;
  614. int i;
  615. pins = devm_kcalloc(chip->dev, gc->ngpio, sizeof(*pins), GFP_KERNEL);
  616. if (!pins)
  617. return -ENOMEM;
  618. for (i = 0; i < gc->ngpio; i++) {
  619. pins[i].number = i;
  620. pins[i].name = devm_kasprintf(chip->dev, GFP_KERNEL,
  621. "gpio-%d", i);
  622. if (!pins[i].name)
  623. return -ENOMEM;
  624. }
  625. pctldesc->name = dev_name(chip->dev);
  626. pctldesc->pctlops = &cygnus_pctrl_ops;
  627. pctldesc->pins = pins;
  628. pctldesc->npins = gc->ngpio;
  629. pctldesc->confops = &cygnus_pconf_ops;
  630. chip->pctl = pinctrl_register(pctldesc, chip->dev, chip);
  631. if (!chip->pctl) {
  632. dev_err(chip->dev, "unable to register pinctrl device\n");
  633. return -EINVAL;
  634. }
  635. return 0;
  636. }
  637. static void cygnus_gpio_unregister_pinconf(struct cygnus_gpio *chip)
  638. {
  639. if (chip->pctl)
  640. pinctrl_unregister(chip->pctl);
  641. }
  642. struct cygnus_gpio_data {
  643. unsigned num_gpios;
  644. };
  645. static const struct cygnus_gpio_data cygnus_cmm_gpio_data = {
  646. .num_gpios = 24,
  647. };
  648. static const struct cygnus_gpio_data cygnus_asiu_gpio_data = {
  649. .num_gpios = 146,
  650. };
  651. static const struct cygnus_gpio_data cygnus_crmu_gpio_data = {
  652. .num_gpios = 6,
  653. };
  654. static const struct of_device_id cygnus_gpio_of_match[] = {
  655. {
  656. .compatible = "brcm,cygnus-ccm-gpio",
  657. .data = &cygnus_cmm_gpio_data,
  658. },
  659. {
  660. .compatible = "brcm,cygnus-asiu-gpio",
  661. .data = &cygnus_asiu_gpio_data,
  662. },
  663. {
  664. .compatible = "brcm,cygnus-crmu-gpio",
  665. .data = &cygnus_crmu_gpio_data,
  666. }
  667. };
  668. static int cygnus_gpio_probe(struct platform_device *pdev)
  669. {
  670. struct device *dev = &pdev->dev;
  671. struct resource *res;
  672. struct cygnus_gpio *chip;
  673. struct gpio_chip *gc;
  674. u32 ngpios;
  675. int irq, ret;
  676. const struct of_device_id *match;
  677. const struct cygnus_gpio_data *gpio_data;
  678. match = of_match_device(cygnus_gpio_of_match, dev);
  679. if (!match)
  680. return -ENODEV;
  681. gpio_data = match->data;
  682. ngpios = gpio_data->num_gpios;
  683. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  684. if (!chip)
  685. return -ENOMEM;
  686. chip->dev = dev;
  687. platform_set_drvdata(pdev, chip);
  688. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  689. chip->base = devm_ioremap_resource(dev, res);
  690. if (IS_ERR(chip->base)) {
  691. dev_err(dev, "unable to map I/O memory\n");
  692. return PTR_ERR(chip->base);
  693. }
  694. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  695. if (res) {
  696. chip->io_ctrl = devm_ioremap_resource(dev, res);
  697. if (IS_ERR(chip->io_ctrl)) {
  698. dev_err(dev, "unable to map I/O memory\n");
  699. return PTR_ERR(chip->io_ctrl);
  700. }
  701. }
  702. spin_lock_init(&chip->lock);
  703. gc = &chip->gc;
  704. gc->base = -1;
  705. gc->ngpio = ngpios;
  706. chip->num_banks = (ngpios + NGPIOS_PER_BANK - 1) / NGPIOS_PER_BANK;
  707. gc->label = dev_name(dev);
  708. gc->dev = dev;
  709. gc->of_node = dev->of_node;
  710. gc->request = cygnus_gpio_request;
  711. gc->free = cygnus_gpio_free;
  712. gc->direction_input = cygnus_gpio_direction_input;
  713. gc->direction_output = cygnus_gpio_direction_output;
  714. gc->set = cygnus_gpio_set;
  715. gc->get = cygnus_gpio_get;
  716. ret = gpiochip_add(gc);
  717. if (ret < 0) {
  718. dev_err(dev, "unable to add GPIO chip\n");
  719. return ret;
  720. }
  721. ret = cygnus_gpio_pinmux_add_range(chip);
  722. if (ret && ret != -ENODEV) {
  723. dev_err(dev, "unable to add GPIO pin range\n");
  724. goto err_rm_gpiochip;
  725. }
  726. ret = cygnus_gpio_register_pinconf(chip);
  727. if (ret) {
  728. dev_err(dev, "unable to register pinconf\n");
  729. goto err_rm_gpiochip;
  730. }
  731. /* optional GPIO interrupt support */
  732. irq = platform_get_irq(pdev, 0);
  733. if (irq) {
  734. ret = gpiochip_irqchip_add(gc, &cygnus_gpio_irq_chip, 0,
  735. handle_simple_irq, IRQ_TYPE_NONE);
  736. if (ret) {
  737. dev_err(dev, "no GPIO irqchip\n");
  738. goto err_unregister_pinconf;
  739. }
  740. gpiochip_set_chained_irqchip(gc, &cygnus_gpio_irq_chip, irq,
  741. cygnus_gpio_irq_handler);
  742. }
  743. return 0;
  744. err_unregister_pinconf:
  745. cygnus_gpio_unregister_pinconf(chip);
  746. err_rm_gpiochip:
  747. gpiochip_remove(gc);
  748. return ret;
  749. }
  750. static struct platform_driver cygnus_gpio_driver = {
  751. .driver = {
  752. .name = "cygnus-gpio",
  753. .of_match_table = cygnus_gpio_of_match,
  754. },
  755. .probe = cygnus_gpio_probe,
  756. };
  757. static int __init cygnus_gpio_init(void)
  758. {
  759. return platform_driver_probe(&cygnus_gpio_driver, cygnus_gpio_probe);
  760. }
  761. arch_initcall_sync(cygnus_gpio_init);