pinctrl-nsp-gpio.c 19 KB

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