pinctrl-nsp-gpio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 Northstar Plus (NSP) GPIO driver that
  15. * supports the chipCommonA GPIO controller. Basic PINCONF such as bias,
  16. * pull up/down, slew and drive strength are also supported in this driver.
  17. *
  18. * Pins from the chipCommonA GPIO can be individually muxed to GPIO function,
  19. * through the interaction with the NSP IOMUX controller.
  20. */
  21. #include <linux/gpio/driver.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/ioport.h>
  25. #include <linux/kernel.h>
  26. #include <linux/of_address.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/pinctrl/pinconf.h>
  30. #include <linux/pinctrl/pinconf-generic.h>
  31. #include <linux/pinctrl/pinctrl.h>
  32. #include <linux/slab.h>
  33. #include "../pinctrl-utils.h"
  34. #define NSP_CHIP_A_INT_STATUS 0x00
  35. #define NSP_CHIP_A_INT_MASK 0x04
  36. #define NSP_GPIO_DATA_IN 0x40
  37. #define NSP_GPIO_DATA_OUT 0x44
  38. #define NSP_GPIO_OUT_EN 0x48
  39. #define NSP_GPIO_INT_POLARITY 0x50
  40. #define NSP_GPIO_INT_MASK 0x54
  41. #define NSP_GPIO_EVENT 0x58
  42. #define NSP_GPIO_EVENT_INT_MASK 0x5c
  43. #define NSP_GPIO_EVENT_INT_POLARITY 0x64
  44. #define NSP_CHIP_A_GPIO_INT_BIT 0x01
  45. /* I/O parameters offset for chipcommon A GPIO */
  46. #define NSP_GPIO_DRV_CTRL 0x00
  47. #define NSP_GPIO_HYSTERESIS_EN 0x10
  48. #define NSP_GPIO_SLEW_RATE_EN 0x14
  49. #define NSP_PULL_UP_EN 0x18
  50. #define NSP_PULL_DOWN_EN 0x1c
  51. #define GPIO_DRV_STRENGTH_BITS 0x03
  52. /*
  53. * nsp GPIO core
  54. *
  55. * @dev: pointer to device
  56. * @base: I/O register base for nsp GPIO controller
  57. * @io_ctrl: I/O register base for PINCONF support outside the GPIO block
  58. * @gc: GPIO chip
  59. * @pctl: pointer to pinctrl_dev
  60. * @pctldesc: pinctrl descriptor
  61. * @irq_domain: pointer to irq domain
  62. * @lock: lock to protect access to I/O registers
  63. */
  64. struct nsp_gpio {
  65. struct device *dev;
  66. void __iomem *base;
  67. void __iomem *io_ctrl;
  68. struct gpio_chip gc;
  69. struct pinctrl_dev *pctl;
  70. struct pinctrl_desc pctldesc;
  71. struct irq_domain *irq_domain;
  72. raw_spinlock_t lock;
  73. };
  74. enum base_type {
  75. REG,
  76. IO_CTRL
  77. };
  78. /*
  79. * Mapping from PINCONF pins to GPIO pins is 1-to-1
  80. */
  81. static inline unsigned nsp_pin_to_gpio(unsigned pin)
  82. {
  83. return pin;
  84. }
  85. /*
  86. * nsp_set_bit - set or clear one bit (corresponding to the GPIO pin) in a
  87. * nsp GPIO register
  88. *
  89. * @nsp_gpio: nsp GPIO device
  90. * @base_type: reg base to modify
  91. * @reg: register offset
  92. * @gpio: GPIO pin
  93. * @set: set or clear
  94. */
  95. static inline void nsp_set_bit(struct nsp_gpio *chip, enum base_type address,
  96. unsigned int reg, unsigned gpio, bool set)
  97. {
  98. u32 val;
  99. void __iomem *base_address;
  100. if (address == IO_CTRL)
  101. base_address = chip->io_ctrl;
  102. else
  103. base_address = chip->base;
  104. val = readl(base_address + reg);
  105. if (set)
  106. val |= BIT(gpio);
  107. else
  108. val &= ~BIT(gpio);
  109. writel(val, base_address + reg);
  110. }
  111. /*
  112. * nsp_get_bit - get one bit (corresponding to the GPIO pin) in a
  113. * nsp GPIO register
  114. */
  115. static inline bool nsp_get_bit(struct nsp_gpio *chip, enum base_type address,
  116. unsigned int reg, unsigned gpio)
  117. {
  118. if (address == IO_CTRL)
  119. return !!(readl(chip->io_ctrl + reg) & BIT(gpio));
  120. else
  121. return !!(readl(chip->base + reg) & BIT(gpio));
  122. }
  123. static irqreturn_t nsp_gpio_irq_handler(int irq, void *data)
  124. {
  125. struct nsp_gpio *chip = (struct nsp_gpio *)data;
  126. struct gpio_chip gc = chip->gc;
  127. int bit;
  128. unsigned long int_bits = 0;
  129. u32 int_status;
  130. /* go through the entire GPIOs and handle all interrupts */
  131. int_status = readl(chip->base + NSP_CHIP_A_INT_STATUS);
  132. if (int_status & NSP_CHIP_A_GPIO_INT_BIT) {
  133. unsigned int event, level;
  134. /* Get level and edge interrupts */
  135. event = readl(chip->base + NSP_GPIO_EVENT_INT_MASK) &
  136. readl(chip->base + NSP_GPIO_EVENT);
  137. level = readl(chip->base + NSP_GPIO_DATA_IN) ^
  138. readl(chip->base + NSP_GPIO_INT_POLARITY);
  139. level &= readl(chip->base + NSP_GPIO_INT_MASK);
  140. int_bits = level | event;
  141. for_each_set_bit(bit, &int_bits, gc.ngpio) {
  142. /*
  143. * Clear the interrupt before invoking the
  144. * handler, so we do not leave any window
  145. */
  146. writel(BIT(bit), chip->base + NSP_GPIO_EVENT);
  147. generic_handle_irq(
  148. irq_linear_revmap(chip->irq_domain, bit));
  149. }
  150. }
  151. return int_bits ? IRQ_HANDLED : IRQ_NONE;
  152. }
  153. static void nsp_gpio_irq_ack(struct irq_data *d)
  154. {
  155. struct nsp_gpio *chip = irq_data_get_irq_chip_data(d);
  156. unsigned gpio = d->hwirq;
  157. u32 val = BIT(gpio);
  158. u32 trigger_type;
  159. trigger_type = irq_get_trigger_type(d->irq);
  160. if (trigger_type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  161. nsp_set_bit(chip, REG, NSP_GPIO_EVENT, gpio, val);
  162. }
  163. /*
  164. * nsp_gpio_irq_set_mask - mask/unmask a GPIO interrupt
  165. *
  166. * @d: IRQ chip data
  167. * @unmask: mask/unmask GPIO interrupt
  168. */
  169. static void nsp_gpio_irq_set_mask(struct irq_data *d, bool unmask)
  170. {
  171. struct nsp_gpio *chip = irq_data_get_irq_chip_data(d);
  172. unsigned gpio = d->hwirq;
  173. u32 trigger_type;
  174. trigger_type = irq_get_trigger_type(d->irq);
  175. if (trigger_type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  176. nsp_set_bit(chip, REG, NSP_GPIO_EVENT_INT_MASK, gpio, unmask);
  177. else
  178. nsp_set_bit(chip, REG, NSP_GPIO_INT_MASK, gpio, unmask);
  179. }
  180. static void nsp_gpio_irq_mask(struct irq_data *d)
  181. {
  182. struct nsp_gpio *chip = irq_data_get_irq_chip_data(d);
  183. unsigned long flags;
  184. raw_spin_lock_irqsave(&chip->lock, flags);
  185. nsp_gpio_irq_set_mask(d, false);
  186. raw_spin_unlock_irqrestore(&chip->lock, flags);
  187. }
  188. static void nsp_gpio_irq_unmask(struct irq_data *d)
  189. {
  190. struct nsp_gpio *chip = irq_data_get_irq_chip_data(d);
  191. unsigned long flags;
  192. raw_spin_lock_irqsave(&chip->lock, flags);
  193. nsp_gpio_irq_set_mask(d, true);
  194. raw_spin_unlock_irqrestore(&chip->lock, flags);
  195. }
  196. static int nsp_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  197. {
  198. struct nsp_gpio *chip = irq_data_get_irq_chip_data(d);
  199. unsigned gpio = d->hwirq;
  200. bool level_low;
  201. bool falling;
  202. unsigned long flags;
  203. raw_spin_lock_irqsave(&chip->lock, flags);
  204. falling = nsp_get_bit(chip, REG, NSP_GPIO_EVENT_INT_POLARITY, gpio);
  205. level_low = nsp_get_bit(chip, REG, NSP_GPIO_INT_POLARITY, gpio);
  206. switch (type & IRQ_TYPE_SENSE_MASK) {
  207. case IRQ_TYPE_EDGE_RISING:
  208. falling = false;
  209. break;
  210. case IRQ_TYPE_EDGE_FALLING:
  211. falling = true;
  212. break;
  213. case IRQ_TYPE_LEVEL_HIGH:
  214. level_low = false;
  215. break;
  216. case IRQ_TYPE_LEVEL_LOW:
  217. level_low = true;
  218. break;
  219. default:
  220. dev_err(chip->dev, "invalid GPIO IRQ type 0x%x\n",
  221. type);
  222. raw_spin_unlock_irqrestore(&chip->lock, flags);
  223. return -EINVAL;
  224. }
  225. nsp_set_bit(chip, REG, NSP_GPIO_EVENT_INT_POLARITY, gpio, falling);
  226. nsp_set_bit(chip, REG, NSP_GPIO_INT_POLARITY, gpio, level_low);
  227. raw_spin_unlock_irqrestore(&chip->lock, flags);
  228. dev_dbg(chip->dev, "gpio:%u level_low:%s falling:%s\n", gpio,
  229. level_low ? "true" : "false", falling ? "true" : "false");
  230. return 0;
  231. }
  232. static struct irq_chip nsp_gpio_irq_chip = {
  233. .name = "gpio-a",
  234. .irq_enable = nsp_gpio_irq_unmask,
  235. .irq_disable = nsp_gpio_irq_mask,
  236. .irq_ack = nsp_gpio_irq_ack,
  237. .irq_mask = nsp_gpio_irq_mask,
  238. .irq_unmask = nsp_gpio_irq_unmask,
  239. .irq_set_type = nsp_gpio_irq_set_type,
  240. };
  241. static int nsp_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
  242. {
  243. struct nsp_gpio *chip = gpiochip_get_data(gc);
  244. unsigned long flags;
  245. raw_spin_lock_irqsave(&chip->lock, flags);
  246. nsp_set_bit(chip, REG, NSP_GPIO_OUT_EN, gpio, false);
  247. raw_spin_unlock_irqrestore(&chip->lock, flags);
  248. dev_dbg(chip->dev, "gpio:%u set input\n", gpio);
  249. return 0;
  250. }
  251. static int nsp_gpio_direction_output(struct gpio_chip *gc, unsigned gpio,
  252. int val)
  253. {
  254. struct nsp_gpio *chip = gpiochip_get_data(gc);
  255. unsigned long flags;
  256. raw_spin_lock_irqsave(&chip->lock, flags);
  257. nsp_set_bit(chip, REG, NSP_GPIO_OUT_EN, gpio, true);
  258. nsp_set_bit(chip, REG, NSP_GPIO_DATA_OUT, gpio, !!(val));
  259. raw_spin_unlock_irqrestore(&chip->lock, flags);
  260. dev_dbg(chip->dev, "gpio:%u set output, value:%d\n", gpio, val);
  261. return 0;
  262. }
  263. static void nsp_gpio_set(struct gpio_chip *gc, unsigned gpio, int val)
  264. {
  265. struct nsp_gpio *chip = gpiochip_get_data(gc);
  266. unsigned long flags;
  267. raw_spin_lock_irqsave(&chip->lock, flags);
  268. nsp_set_bit(chip, REG, NSP_GPIO_DATA_OUT, gpio, !!(val));
  269. raw_spin_unlock_irqrestore(&chip->lock, flags);
  270. dev_dbg(chip->dev, "gpio:%u set, value:%d\n", gpio, val);
  271. }
  272. static int nsp_gpio_get(struct gpio_chip *gc, unsigned gpio)
  273. {
  274. struct nsp_gpio *chip = gpiochip_get_data(gc);
  275. return !!(readl(chip->base + NSP_GPIO_DATA_IN) & BIT(gpio));
  276. }
  277. static int nsp_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  278. {
  279. struct nsp_gpio *chip = gpiochip_get_data(gc);
  280. return irq_linear_revmap(chip->irq_domain, offset);
  281. }
  282. static int nsp_get_groups_count(struct pinctrl_dev *pctldev)
  283. {
  284. return 1;
  285. }
  286. /*
  287. * Only one group: "gpio_grp", since this local pinctrl device only performs
  288. * GPIO specific PINCONF configurations
  289. */
  290. static const char *nsp_get_group_name(struct pinctrl_dev *pctldev,
  291. unsigned selector)
  292. {
  293. return "gpio_grp";
  294. }
  295. static const struct pinctrl_ops nsp_pctrl_ops = {
  296. .get_groups_count = nsp_get_groups_count,
  297. .get_group_name = nsp_get_group_name,
  298. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  299. .dt_free_map = pinctrl_utils_free_map,
  300. };
  301. static int nsp_gpio_set_slew(struct nsp_gpio *chip, unsigned gpio, u32 slew)
  302. {
  303. if (slew)
  304. nsp_set_bit(chip, IO_CTRL, NSP_GPIO_SLEW_RATE_EN, gpio, true);
  305. else
  306. nsp_set_bit(chip, IO_CTRL, NSP_GPIO_SLEW_RATE_EN, gpio, false);
  307. return 0;
  308. }
  309. static int nsp_gpio_set_pull(struct nsp_gpio *chip, unsigned gpio,
  310. bool pull_up, bool pull_down)
  311. {
  312. unsigned long flags;
  313. raw_spin_lock_irqsave(&chip->lock, flags);
  314. nsp_set_bit(chip, IO_CTRL, NSP_PULL_DOWN_EN, gpio, pull_down);
  315. nsp_set_bit(chip, IO_CTRL, NSP_PULL_UP_EN, gpio, pull_up);
  316. raw_spin_unlock_irqrestore(&chip->lock, flags);
  317. dev_dbg(chip->dev, "gpio:%u set pullup:%d pulldown: %d\n",
  318. gpio, pull_up, pull_down);
  319. return 0;
  320. }
  321. static void nsp_gpio_get_pull(struct nsp_gpio *chip, unsigned gpio,
  322. bool *pull_up, bool *pull_down)
  323. {
  324. unsigned long flags;
  325. raw_spin_lock_irqsave(&chip->lock, flags);
  326. *pull_up = nsp_get_bit(chip, IO_CTRL, NSP_PULL_UP_EN, gpio);
  327. *pull_down = nsp_get_bit(chip, IO_CTRL, NSP_PULL_DOWN_EN, gpio);
  328. raw_spin_unlock_irqrestore(&chip->lock, flags);
  329. }
  330. static int nsp_gpio_set_strength(struct nsp_gpio *chip, unsigned gpio,
  331. u32 strength)
  332. {
  333. u32 offset, shift, i;
  334. u32 val;
  335. unsigned long flags;
  336. /* make sure drive strength is supported */
  337. if (strength < 2 || strength > 16 || (strength % 2))
  338. return -ENOTSUPP;
  339. shift = gpio;
  340. offset = NSP_GPIO_DRV_CTRL;
  341. dev_dbg(chip->dev, "gpio:%u set drive strength:%d mA\n", gpio,
  342. strength);
  343. raw_spin_lock_irqsave(&chip->lock, flags);
  344. strength = (strength / 2) - 1;
  345. for (i = GPIO_DRV_STRENGTH_BITS; i > 0; i--) {
  346. val = readl(chip->io_ctrl + offset);
  347. val &= ~BIT(shift);
  348. val |= ((strength >> (i-1)) & 0x1) << shift;
  349. writel(val, chip->io_ctrl + offset);
  350. offset += 4;
  351. }
  352. raw_spin_unlock_irqrestore(&chip->lock, flags);
  353. return 0;
  354. }
  355. static int nsp_gpio_get_strength(struct nsp_gpio *chip, unsigned gpio,
  356. u16 *strength)
  357. {
  358. unsigned int offset, shift;
  359. u32 val;
  360. unsigned long flags;
  361. int i;
  362. offset = NSP_GPIO_DRV_CTRL;
  363. shift = gpio;
  364. raw_spin_lock_irqsave(&chip->lock, flags);
  365. *strength = 0;
  366. for (i = (GPIO_DRV_STRENGTH_BITS - 1); i >= 0; i--) {
  367. val = readl(chip->io_ctrl + offset) & BIT(shift);
  368. val >>= shift;
  369. *strength += (val << i);
  370. offset += 4;
  371. }
  372. /* convert to mA */
  373. *strength = (*strength + 1) * 2;
  374. raw_spin_unlock_irqrestore(&chip->lock, flags);
  375. return 0;
  376. }
  377. static int nsp_pin_config_group_get(struct pinctrl_dev *pctldev,
  378. unsigned selector,
  379. unsigned long *config)
  380. {
  381. return 0;
  382. }
  383. static int nsp_pin_config_group_set(struct pinctrl_dev *pctldev,
  384. unsigned selector,
  385. unsigned long *configs, unsigned num_configs)
  386. {
  387. return 0;
  388. }
  389. static int nsp_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
  390. unsigned long *config)
  391. {
  392. struct nsp_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
  393. enum pin_config_param param = pinconf_to_config_param(*config);
  394. unsigned int gpio;
  395. u16 arg = 0;
  396. bool pull_up, pull_down;
  397. int ret;
  398. gpio = nsp_pin_to_gpio(pin);
  399. switch (param) {
  400. case PIN_CONFIG_BIAS_DISABLE:
  401. nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
  402. if ((pull_up == false) && (pull_down == false))
  403. return 0;
  404. else
  405. return -EINVAL;
  406. case PIN_CONFIG_BIAS_PULL_UP:
  407. nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
  408. if (pull_up)
  409. return 0;
  410. else
  411. return -EINVAL;
  412. case PIN_CONFIG_BIAS_PULL_DOWN:
  413. nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
  414. if (pull_down)
  415. return 0;
  416. else
  417. return -EINVAL;
  418. case PIN_CONFIG_DRIVE_STRENGTH:
  419. ret = nsp_gpio_get_strength(chip, gpio, &arg);
  420. if (ret)
  421. return ret;
  422. *config = pinconf_to_config_packed(param, arg);
  423. return 0;
  424. default:
  425. return -ENOTSUPP;
  426. }
  427. }
  428. static int nsp_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
  429. unsigned long *configs, unsigned num_configs)
  430. {
  431. struct nsp_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
  432. enum pin_config_param param;
  433. u32 arg;
  434. unsigned int i, gpio;
  435. int ret = -ENOTSUPP;
  436. gpio = nsp_pin_to_gpio(pin);
  437. for (i = 0; i < num_configs; i++) {
  438. param = pinconf_to_config_param(configs[i]);
  439. arg = pinconf_to_config_argument(configs[i]);
  440. switch (param) {
  441. case PIN_CONFIG_BIAS_DISABLE:
  442. ret = nsp_gpio_set_pull(chip, gpio, false, false);
  443. if (ret < 0)
  444. goto out;
  445. break;
  446. case PIN_CONFIG_BIAS_PULL_UP:
  447. ret = nsp_gpio_set_pull(chip, gpio, true, false);
  448. if (ret < 0)
  449. goto out;
  450. break;
  451. case PIN_CONFIG_BIAS_PULL_DOWN:
  452. ret = nsp_gpio_set_pull(chip, gpio, false, true);
  453. if (ret < 0)
  454. goto out;
  455. break;
  456. case PIN_CONFIG_DRIVE_STRENGTH:
  457. ret = nsp_gpio_set_strength(chip, gpio, arg);
  458. if (ret < 0)
  459. goto out;
  460. break;
  461. case PIN_CONFIG_SLEW_RATE:
  462. ret = nsp_gpio_set_slew(chip, gpio, arg);
  463. if (ret < 0)
  464. goto out;
  465. break;
  466. default:
  467. dev_err(chip->dev, "invalid configuration\n");
  468. return -ENOTSUPP;
  469. }
  470. }
  471. out:
  472. return ret;
  473. }
  474. static const struct pinconf_ops nsp_pconf_ops = {
  475. .is_generic = true,
  476. .pin_config_get = nsp_pin_config_get,
  477. .pin_config_set = nsp_pin_config_set,
  478. .pin_config_group_get = nsp_pin_config_group_get,
  479. .pin_config_group_set = nsp_pin_config_group_set,
  480. };
  481. /*
  482. * NSP GPIO controller supports some PINCONF related configurations such as
  483. * pull up, pull down, slew and drive strength, when the pin is configured
  484. * to GPIO.
  485. *
  486. * Here a local pinctrl device is created with simple 1-to-1 pin mapping to the
  487. * local GPIO pins
  488. */
  489. static int nsp_gpio_register_pinconf(struct nsp_gpio *chip)
  490. {
  491. struct pinctrl_desc *pctldesc = &chip->pctldesc;
  492. struct pinctrl_pin_desc *pins;
  493. struct gpio_chip *gc = &chip->gc;
  494. int i;
  495. pins = devm_kcalloc(chip->dev, gc->ngpio, sizeof(*pins), GFP_KERNEL);
  496. if (!pins)
  497. return -ENOMEM;
  498. for (i = 0; i < gc->ngpio; i++) {
  499. pins[i].number = i;
  500. pins[i].name = devm_kasprintf(chip->dev, GFP_KERNEL,
  501. "gpio-%d", i);
  502. if (!pins[i].name)
  503. return -ENOMEM;
  504. }
  505. pctldesc->name = dev_name(chip->dev);
  506. pctldesc->pctlops = &nsp_pctrl_ops;
  507. pctldesc->pins = pins;
  508. pctldesc->npins = gc->ngpio;
  509. pctldesc->confops = &nsp_pconf_ops;
  510. chip->pctl = devm_pinctrl_register(chip->dev, pctldesc, chip);
  511. if (IS_ERR(chip->pctl)) {
  512. dev_err(chip->dev, "unable to register pinctrl device\n");
  513. return PTR_ERR(chip->pctl);
  514. }
  515. return 0;
  516. }
  517. static const struct of_device_id nsp_gpio_of_match[] = {
  518. {.compatible = "brcm,nsp-gpio-a",},
  519. {}
  520. };
  521. static int nsp_gpio_probe(struct platform_device *pdev)
  522. {
  523. struct device *dev = &pdev->dev;
  524. struct resource *res;
  525. struct nsp_gpio *chip;
  526. struct gpio_chip *gc;
  527. u32 val, count;
  528. int irq, ret;
  529. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &val)) {
  530. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  531. return -ENODEV;
  532. }
  533. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  534. if (!chip)
  535. return -ENOMEM;
  536. chip->dev = dev;
  537. platform_set_drvdata(pdev, chip);
  538. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  539. chip->base = devm_ioremap_resource(dev, res);
  540. if (IS_ERR(chip->base)) {
  541. dev_err(dev, "unable to map I/O memory\n");
  542. return PTR_ERR(chip->base);
  543. }
  544. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  545. chip->io_ctrl = devm_ioremap_resource(dev, res);
  546. if (IS_ERR(chip->io_ctrl)) {
  547. dev_err(dev, "unable to map I/O memory\n");
  548. return PTR_ERR(chip->io_ctrl);
  549. }
  550. raw_spin_lock_init(&chip->lock);
  551. gc = &chip->gc;
  552. gc->base = -1;
  553. gc->can_sleep = false;
  554. gc->ngpio = val;
  555. gc->label = dev_name(dev);
  556. gc->parent = dev;
  557. gc->of_node = dev->of_node;
  558. gc->request = gpiochip_generic_request;
  559. gc->free = gpiochip_generic_free;
  560. gc->direction_input = nsp_gpio_direction_input;
  561. gc->direction_output = nsp_gpio_direction_output;
  562. gc->set = nsp_gpio_set;
  563. gc->get = nsp_gpio_get;
  564. gc->to_irq = nsp_gpio_to_irq;
  565. /* optional GPIO interrupt support */
  566. irq = platform_get_irq(pdev, 0);
  567. if (irq > 0) {
  568. /* Create irq domain so that each pin can be assigned an IRQ.*/
  569. chip->irq_domain = irq_domain_add_linear(gc->of_node, gc->ngpio,
  570. &irq_domain_simple_ops,
  571. chip);
  572. if (!chip->irq_domain) {
  573. dev_err(&pdev->dev, "Couldn't allocate IRQ domain\n");
  574. return -ENXIO;
  575. }
  576. /* Map each gpio to an IRQ and set the handler for gpiolib. */
  577. for (count = 0; count < gc->ngpio; count++) {
  578. int irq = irq_create_mapping(chip->irq_domain, count);
  579. irq_set_chip_and_handler(irq, &nsp_gpio_irq_chip,
  580. handle_simple_irq);
  581. irq_set_chip_data(irq, chip);
  582. }
  583. /* Install ISR for this GPIO controller. */
  584. ret = devm_request_irq(&pdev->dev, irq, nsp_gpio_irq_handler,
  585. IRQF_SHARED, "gpio-a", chip);
  586. if (ret) {
  587. dev_err(&pdev->dev, "Unable to request IRQ%d: %d\n",
  588. irq, ret);
  589. goto err_rm_gpiochip;
  590. }
  591. val = readl(chip->base + NSP_CHIP_A_INT_MASK);
  592. val = val | NSP_CHIP_A_GPIO_INT_BIT;
  593. writel(val, (chip->base + NSP_CHIP_A_INT_MASK));
  594. }
  595. ret = gpiochip_add_data(gc, chip);
  596. if (ret < 0) {
  597. dev_err(dev, "unable to add GPIO chip\n");
  598. return ret;
  599. }
  600. ret = nsp_gpio_register_pinconf(chip);
  601. if (ret) {
  602. dev_err(dev, "unable to register pinconf\n");
  603. goto err_rm_gpiochip;
  604. }
  605. return 0;
  606. err_rm_gpiochip:
  607. gpiochip_remove(gc);
  608. return ret;
  609. }
  610. static struct platform_driver nsp_gpio_driver = {
  611. .driver = {
  612. .name = "nsp-gpio-a",
  613. .of_match_table = nsp_gpio_of_match,
  614. },
  615. .probe = nsp_gpio_probe,
  616. };
  617. static int __init nsp_gpio_init(void)
  618. {
  619. return platform_driver_register(&nsp_gpio_driver);
  620. }
  621. arch_initcall_sync(nsp_gpio_init);