pinctrl-nsp-gpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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_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. static int nsp_pin_config_group_get(struct pinctrl_dev *pctldev,
  390. unsigned selector,
  391. unsigned long *config)
  392. {
  393. return 0;
  394. }
  395. static int nsp_pin_config_group_set(struct pinctrl_dev *pctldev,
  396. unsigned selector,
  397. unsigned long *configs, unsigned num_configs)
  398. {
  399. return 0;
  400. }
  401. static int nsp_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
  402. unsigned long *config)
  403. {
  404. struct nsp_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
  405. enum pin_config_param param = pinconf_to_config_param(*config);
  406. unsigned int gpio;
  407. u16 arg = 0;
  408. bool pull_up, pull_down;
  409. int ret;
  410. gpio = nsp_pin_to_gpio(pin);
  411. switch (param) {
  412. case PIN_CONFIG_BIAS_DISABLE:
  413. nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
  414. if ((pull_up == false) && (pull_down == false))
  415. return 0;
  416. else
  417. return -EINVAL;
  418. case PIN_CONFIG_BIAS_PULL_UP:
  419. nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
  420. if (pull_up)
  421. return 0;
  422. else
  423. return -EINVAL;
  424. case PIN_CONFIG_BIAS_PULL_DOWN:
  425. nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
  426. if (pull_down)
  427. return 0;
  428. else
  429. return -EINVAL;
  430. case PIN_CONFIG_DRIVE_STRENGTH:
  431. ret = nsp_gpio_get_strength(chip, gpio, &arg);
  432. if (ret)
  433. return ret;
  434. *config = pinconf_to_config_packed(param, arg);
  435. return 0;
  436. default:
  437. return -ENOTSUPP;
  438. }
  439. }
  440. static int nsp_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
  441. unsigned long *configs, unsigned num_configs)
  442. {
  443. struct nsp_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
  444. enum pin_config_param param;
  445. u16 arg;
  446. unsigned int i, gpio;
  447. int ret = -ENOTSUPP;
  448. gpio = nsp_pin_to_gpio(pin);
  449. for (i = 0; i < num_configs; i++) {
  450. param = pinconf_to_config_param(configs[i]);
  451. arg = pinconf_to_config_argument(configs[i]);
  452. switch (param) {
  453. case PIN_CONFIG_BIAS_DISABLE:
  454. ret = nsp_gpio_set_pull(chip, gpio, false, false);
  455. if (ret < 0)
  456. goto out;
  457. break;
  458. case PIN_CONFIG_BIAS_PULL_UP:
  459. ret = nsp_gpio_set_pull(chip, gpio, true, false);
  460. if (ret < 0)
  461. goto out;
  462. break;
  463. case PIN_CONFIG_BIAS_PULL_DOWN:
  464. ret = nsp_gpio_set_pull(chip, gpio, false, true);
  465. if (ret < 0)
  466. goto out;
  467. break;
  468. case PIN_CONFIG_DRIVE_STRENGTH:
  469. ret = nsp_gpio_set_strength(chip, gpio, arg);
  470. if (ret < 0)
  471. goto out;
  472. break;
  473. case PIN_CONFIG_SLEW_RATE:
  474. ret = nsp_gpio_set_slew(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. }
  483. out:
  484. return ret;
  485. }
  486. static const struct pinconf_ops nsp_pconf_ops = {
  487. .is_generic = true,
  488. .pin_config_get = nsp_pin_config_get,
  489. .pin_config_set = nsp_pin_config_set,
  490. .pin_config_group_get = nsp_pin_config_group_get,
  491. .pin_config_group_set = nsp_pin_config_group_set,
  492. };
  493. /*
  494. * NSP GPIO controller supports some PINCONF related configurations such as
  495. * pull up, pull down, slew and drive strength, when the pin is configured
  496. * to GPIO.
  497. *
  498. * Here a local pinctrl device is created with simple 1-to-1 pin mapping to the
  499. * local GPIO pins
  500. */
  501. static int nsp_gpio_register_pinconf(struct nsp_gpio *chip)
  502. {
  503. struct pinctrl_desc *pctldesc = &chip->pctldesc;
  504. struct pinctrl_pin_desc *pins;
  505. struct gpio_chip *gc = &chip->gc;
  506. int i;
  507. pins = devm_kcalloc(chip->dev, gc->ngpio, sizeof(*pins), GFP_KERNEL);
  508. if (!pins)
  509. return -ENOMEM;
  510. for (i = 0; i < gc->ngpio; i++) {
  511. pins[i].number = i;
  512. pins[i].name = devm_kasprintf(chip->dev, GFP_KERNEL,
  513. "gpio-%d", i);
  514. if (!pins[i].name)
  515. return -ENOMEM;
  516. }
  517. pctldesc->name = dev_name(chip->dev);
  518. pctldesc->pctlops = &nsp_pctrl_ops;
  519. pctldesc->pins = pins;
  520. pctldesc->npins = gc->ngpio;
  521. pctldesc->confops = &nsp_pconf_ops;
  522. chip->pctl = devm_pinctrl_register(chip->dev, pctldesc, chip);
  523. if (IS_ERR(chip->pctl)) {
  524. dev_err(chip->dev, "unable to register pinctrl device\n");
  525. return PTR_ERR(chip->pctl);
  526. }
  527. return 0;
  528. }
  529. static const struct of_device_id nsp_gpio_of_match[] = {
  530. {.compatible = "brcm,nsp-gpio-a",},
  531. {}
  532. };
  533. static int nsp_gpio_probe(struct platform_device *pdev)
  534. {
  535. struct device *dev = &pdev->dev;
  536. struct resource *res;
  537. struct nsp_gpio *chip;
  538. struct gpio_chip *gc;
  539. u32 val, count;
  540. int irq, ret;
  541. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &val)) {
  542. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  543. return -ENODEV;
  544. }
  545. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  546. if (!chip)
  547. return -ENOMEM;
  548. chip->dev = dev;
  549. platform_set_drvdata(pdev, chip);
  550. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  551. chip->base = devm_ioremap_resource(dev, res);
  552. if (IS_ERR(chip->base)) {
  553. dev_err(dev, "unable to map I/O memory\n");
  554. return PTR_ERR(chip->base);
  555. }
  556. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  557. chip->io_ctrl = devm_ioremap_resource(dev, res);
  558. if (IS_ERR(chip->io_ctrl)) {
  559. dev_err(dev, "unable to map I/O memory\n");
  560. return PTR_ERR(chip->io_ctrl);
  561. }
  562. spin_lock_init(&chip->lock);
  563. gc = &chip->gc;
  564. gc->base = -1;
  565. gc->can_sleep = false;
  566. gc->ngpio = val;
  567. gc->label = dev_name(dev);
  568. gc->parent = dev;
  569. gc->of_node = dev->of_node;
  570. gc->request = nsp_gpio_request;
  571. gc->free = nsp_gpio_free;
  572. gc->direction_input = nsp_gpio_direction_input;
  573. gc->direction_output = nsp_gpio_direction_output;
  574. gc->set = nsp_gpio_set;
  575. gc->get = nsp_gpio_get;
  576. gc->to_irq = nsp_gpio_to_irq;
  577. /* optional GPIO interrupt support */
  578. irq = platform_get_irq(pdev, 0);
  579. if (irq > 0) {
  580. /* Create irq domain so that each pin can be assigned an IRQ.*/
  581. chip->irq_domain = irq_domain_add_linear(gc->of_node, gc->ngpio,
  582. &irq_domain_simple_ops,
  583. chip);
  584. if (!chip->irq_domain) {
  585. dev_err(&pdev->dev, "Couldn't allocate IRQ domain\n");
  586. return -ENXIO;
  587. }
  588. /* Map each gpio to an IRQ and set the handler for gpiolib. */
  589. for (count = 0; count < gc->ngpio; count++) {
  590. int irq = irq_create_mapping(chip->irq_domain, count);
  591. irq_set_chip_and_handler(irq, &nsp_gpio_irq_chip,
  592. handle_simple_irq);
  593. irq_set_chip_data(irq, chip);
  594. }
  595. /* Install ISR for this GPIO controller. */
  596. ret = devm_request_irq(&pdev->dev, irq, nsp_gpio_irq_handler,
  597. IRQF_SHARED, "gpio-a", chip);
  598. if (ret) {
  599. dev_err(&pdev->dev, "Unable to request IRQ%d: %d\n",
  600. irq, ret);
  601. goto err_rm_gpiochip;
  602. }
  603. val = readl(chip->base + NSP_CHIP_A_INT_MASK);
  604. val = val | NSP_CHIP_A_GPIO_INT_BIT;
  605. writel(val, (chip->base + NSP_CHIP_A_INT_MASK));
  606. }
  607. ret = gpiochip_add_data(gc, chip);
  608. if (ret < 0) {
  609. dev_err(dev, "unable to add GPIO chip\n");
  610. return ret;
  611. }
  612. ret = nsp_gpio_register_pinconf(chip);
  613. if (ret) {
  614. dev_err(dev, "unable to register pinconf\n");
  615. goto err_rm_gpiochip;
  616. }
  617. return 0;
  618. err_rm_gpiochip:
  619. gpiochip_remove(gc);
  620. return ret;
  621. }
  622. static struct platform_driver nsp_gpio_driver = {
  623. .driver = {
  624. .name = "nsp-gpio-a",
  625. .of_match_table = nsp_gpio_of_match,
  626. },
  627. .probe = nsp_gpio_probe,
  628. };
  629. static int __init nsp_gpio_init(void)
  630. {
  631. return platform_driver_register(&nsp_gpio_driver);
  632. }
  633. arch_initcall_sync(nsp_gpio_init);