gpio-sta2x11.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * STMicroelectronics ConneXt (STA2X11) GPIO driver
  3. *
  4. * Copyright 2012 ST Microelectronics (Alessandro Rubini)
  5. * Based on gpio-ml-ioh.c, Copyright 2010 OKI Semiconductors Ltd.
  6. * Also based on previous sta2x11 work, Copyright 2011 Wind River Systems, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/gpio/driver.h>
  26. #include <linux/bitops.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/irq.h>
  29. #include <linux/pci.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/mfd/sta2x11-mfd.h>
  32. struct gsta_regs {
  33. u32 dat; /* 0x00 */
  34. u32 dats;
  35. u32 datc;
  36. u32 pdis;
  37. u32 dir; /* 0x10 */
  38. u32 dirs;
  39. u32 dirc;
  40. u32 unused_1c;
  41. u32 afsela; /* 0x20 */
  42. u32 unused_24[7];
  43. u32 rimsc; /* 0x40 */
  44. u32 fimsc;
  45. u32 is;
  46. u32 ic;
  47. };
  48. struct gsta_gpio {
  49. spinlock_t lock;
  50. struct device *dev;
  51. void __iomem *reg_base;
  52. struct gsta_regs __iomem *regs[GSTA_NR_BLOCKS];
  53. struct gpio_chip gpio;
  54. int irq_base;
  55. /* FIXME: save the whole config here (AF, ...) */
  56. unsigned irq_type[GSTA_NR_GPIO];
  57. };
  58. /*
  59. * gpio methods
  60. */
  61. static void gsta_gpio_set(struct gpio_chip *gpio, unsigned nr, int val)
  62. {
  63. struct gsta_gpio *chip = gpiochip_get_data(gpio);
  64. struct gsta_regs __iomem *regs = chip->regs[nr / GSTA_GPIO_PER_BLOCK];
  65. u32 bit = BIT(nr % GSTA_GPIO_PER_BLOCK);
  66. if (val)
  67. writel(bit, &regs->dats);
  68. else
  69. writel(bit, &regs->datc);
  70. }
  71. static int gsta_gpio_get(struct gpio_chip *gpio, unsigned nr)
  72. {
  73. struct gsta_gpio *chip = gpiochip_get_data(gpio);
  74. struct gsta_regs __iomem *regs = chip->regs[nr / GSTA_GPIO_PER_BLOCK];
  75. u32 bit = BIT(nr % GSTA_GPIO_PER_BLOCK);
  76. return !!(readl(&regs->dat) & bit);
  77. }
  78. static int gsta_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
  79. int val)
  80. {
  81. struct gsta_gpio *chip = gpiochip_get_data(gpio);
  82. struct gsta_regs __iomem *regs = chip->regs[nr / GSTA_GPIO_PER_BLOCK];
  83. u32 bit = BIT(nr % GSTA_GPIO_PER_BLOCK);
  84. writel(bit, &regs->dirs);
  85. /* Data register after direction, otherwise pullup/down is selected */
  86. if (val)
  87. writel(bit, &regs->dats);
  88. else
  89. writel(bit, &regs->datc);
  90. return 0;
  91. }
  92. static int gsta_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
  93. {
  94. struct gsta_gpio *chip = gpiochip_get_data(gpio);
  95. struct gsta_regs __iomem *regs = chip->regs[nr / GSTA_GPIO_PER_BLOCK];
  96. u32 bit = BIT(nr % GSTA_GPIO_PER_BLOCK);
  97. writel(bit, &regs->dirc);
  98. return 0;
  99. }
  100. static int gsta_gpio_to_irq(struct gpio_chip *gpio, unsigned offset)
  101. {
  102. struct gsta_gpio *chip = gpiochip_get_data(gpio);
  103. return chip->irq_base + offset;
  104. }
  105. static void gsta_gpio_setup(struct gsta_gpio *chip) /* called from probe */
  106. {
  107. struct gpio_chip *gpio = &chip->gpio;
  108. /*
  109. * ARCH_NR_GPIOS is currently 256 and dynamic allocation starts
  110. * from the end. However, for compatibility, we need the first
  111. * ConneXt device to start from gpio 0: it's the main chipset
  112. * on most boards so documents and drivers assume gpio0..gpio127
  113. */
  114. static int gpio_base;
  115. gpio->label = dev_name(chip->dev);
  116. gpio->owner = THIS_MODULE;
  117. gpio->direction_input = gsta_gpio_direction_input;
  118. gpio->get = gsta_gpio_get;
  119. gpio->direction_output = gsta_gpio_direction_output;
  120. gpio->set = gsta_gpio_set;
  121. gpio->dbg_show = NULL;
  122. gpio->base = gpio_base;
  123. gpio->ngpio = GSTA_NR_GPIO;
  124. gpio->can_sleep = false;
  125. gpio->to_irq = gsta_gpio_to_irq;
  126. /*
  127. * After the first device, turn to dynamic gpio numbers.
  128. * For example, with ARCH_NR_GPIOS = 256 we can fit two cards
  129. */
  130. if (!gpio_base)
  131. gpio_base = -1;
  132. }
  133. /*
  134. * Special method: alternate functions and pullup/pulldown. This is only
  135. * invoked on startup to configure gpio's according to platform data.
  136. * FIXME : this functionality shall be managed (and exported to other drivers)
  137. * via the pin control subsystem.
  138. */
  139. static void gsta_set_config(struct gsta_gpio *chip, int nr, unsigned cfg)
  140. {
  141. struct gsta_regs __iomem *regs = chip->regs[nr / GSTA_GPIO_PER_BLOCK];
  142. unsigned long flags;
  143. u32 bit = BIT(nr % GSTA_GPIO_PER_BLOCK);
  144. u32 val;
  145. int err = 0;
  146. pr_info("%s: %p %i %i\n", __func__, chip, nr, cfg);
  147. if (cfg == PINMUX_TYPE_NONE)
  148. return;
  149. /* Alternate function or not? */
  150. spin_lock_irqsave(&chip->lock, flags);
  151. val = readl(&regs->afsela);
  152. if (cfg == PINMUX_TYPE_FUNCTION)
  153. val |= bit;
  154. else
  155. val &= ~bit;
  156. writel(val | bit, &regs->afsela);
  157. if (cfg == PINMUX_TYPE_FUNCTION) {
  158. spin_unlock_irqrestore(&chip->lock, flags);
  159. return;
  160. }
  161. /* not alternate function: set details */
  162. switch (cfg) {
  163. case PINMUX_TYPE_OUTPUT_LOW:
  164. writel(bit, &regs->dirs);
  165. writel(bit, &regs->datc);
  166. break;
  167. case PINMUX_TYPE_OUTPUT_HIGH:
  168. writel(bit, &regs->dirs);
  169. writel(bit, &regs->dats);
  170. break;
  171. case PINMUX_TYPE_INPUT:
  172. writel(bit, &regs->dirc);
  173. val = readl(&regs->pdis) | bit;
  174. writel(val, &regs->pdis);
  175. break;
  176. case PINMUX_TYPE_INPUT_PULLUP:
  177. writel(bit, &regs->dirc);
  178. val = readl(&regs->pdis) & ~bit;
  179. writel(val, &regs->pdis);
  180. writel(bit, &regs->dats);
  181. break;
  182. case PINMUX_TYPE_INPUT_PULLDOWN:
  183. writel(bit, &regs->dirc);
  184. val = readl(&regs->pdis) & ~bit;
  185. writel(val, &regs->pdis);
  186. writel(bit, &regs->datc);
  187. break;
  188. default:
  189. err = 1;
  190. }
  191. spin_unlock_irqrestore(&chip->lock, flags);
  192. if (err)
  193. pr_err("%s: chip %p, pin %i, cfg %i is invalid\n",
  194. __func__, chip, nr, cfg);
  195. }
  196. /*
  197. * Irq methods
  198. */
  199. static void gsta_irq_disable(struct irq_data *data)
  200. {
  201. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
  202. struct gsta_gpio *chip = gc->private;
  203. int nr = data->irq - chip->irq_base;
  204. struct gsta_regs __iomem *regs = chip->regs[nr / GSTA_GPIO_PER_BLOCK];
  205. u32 bit = BIT(nr % GSTA_GPIO_PER_BLOCK);
  206. u32 val;
  207. unsigned long flags;
  208. spin_lock_irqsave(&chip->lock, flags);
  209. if (chip->irq_type[nr] & IRQ_TYPE_EDGE_RISING) {
  210. val = readl(&regs->rimsc) & ~bit;
  211. writel(val, &regs->rimsc);
  212. }
  213. if (chip->irq_type[nr] & IRQ_TYPE_EDGE_FALLING) {
  214. val = readl(&regs->fimsc) & ~bit;
  215. writel(val, &regs->fimsc);
  216. }
  217. spin_unlock_irqrestore(&chip->lock, flags);
  218. return;
  219. }
  220. static void gsta_irq_enable(struct irq_data *data)
  221. {
  222. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
  223. struct gsta_gpio *chip = gc->private;
  224. int nr = data->irq - chip->irq_base;
  225. struct gsta_regs __iomem *regs = chip->regs[nr / GSTA_GPIO_PER_BLOCK];
  226. u32 bit = BIT(nr % GSTA_GPIO_PER_BLOCK);
  227. u32 val;
  228. int type;
  229. unsigned long flags;
  230. type = chip->irq_type[nr];
  231. spin_lock_irqsave(&chip->lock, flags);
  232. val = readl(&regs->rimsc);
  233. if (type & IRQ_TYPE_EDGE_RISING)
  234. writel(val | bit, &regs->rimsc);
  235. else
  236. writel(val & ~bit, &regs->rimsc);
  237. val = readl(&regs->rimsc);
  238. if (type & IRQ_TYPE_EDGE_FALLING)
  239. writel(val | bit, &regs->fimsc);
  240. else
  241. writel(val & ~bit, &regs->fimsc);
  242. spin_unlock_irqrestore(&chip->lock, flags);
  243. return;
  244. }
  245. static int gsta_irq_type(struct irq_data *d, unsigned int type)
  246. {
  247. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  248. struct gsta_gpio *chip = gc->private;
  249. int nr = d->irq - chip->irq_base;
  250. /* We only support edge interrupts */
  251. if (!(type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))) {
  252. pr_debug("%s: unsupported type 0x%x\n", __func__, type);
  253. return -EINVAL;
  254. }
  255. chip->irq_type[nr] = type; /* used for enable/disable */
  256. gsta_irq_enable(d);
  257. return 0;
  258. }
  259. static irqreturn_t gsta_gpio_handler(int irq, void *dev_id)
  260. {
  261. struct gsta_gpio *chip = dev_id;
  262. struct gsta_regs __iomem *regs;
  263. u32 is;
  264. int i, nr, base;
  265. irqreturn_t ret = IRQ_NONE;
  266. for (i = 0; i < GSTA_NR_BLOCKS; i++) {
  267. regs = chip->regs[i];
  268. base = chip->irq_base + i * GSTA_GPIO_PER_BLOCK;
  269. while ((is = readl(&regs->is))) {
  270. nr = __ffs(is);
  271. irq = base + nr;
  272. generic_handle_irq(irq);
  273. writel(1 << nr, &regs->ic);
  274. ret = IRQ_HANDLED;
  275. }
  276. }
  277. return ret;
  278. }
  279. static int gsta_alloc_irq_chip(struct gsta_gpio *chip)
  280. {
  281. struct irq_chip_generic *gc;
  282. struct irq_chip_type *ct;
  283. int rv;
  284. gc = devm_irq_alloc_generic_chip(chip->dev, KBUILD_MODNAME, 1,
  285. chip->irq_base,
  286. chip->reg_base, handle_simple_irq);
  287. if (!gc)
  288. return -ENOMEM;
  289. gc->private = chip;
  290. ct = gc->chip_types;
  291. ct->chip.irq_set_type = gsta_irq_type;
  292. ct->chip.irq_disable = gsta_irq_disable;
  293. ct->chip.irq_enable = gsta_irq_enable;
  294. /* FIXME: this makes at most 32 interrupts. Request 0 by now */
  295. rv = devm_irq_setup_generic_chip(chip->dev, gc,
  296. 0 /* IRQ_MSK(GSTA_GPIO_PER_BLOCK) */,
  297. 0, IRQ_NOREQUEST | IRQ_NOPROBE, 0);
  298. if (rv)
  299. return rv;
  300. /* Set up all all 128 interrupts: code from setup_generic_chip */
  301. {
  302. struct irq_chip_type *ct = gc->chip_types;
  303. int i, j;
  304. for (j = 0; j < GSTA_NR_GPIO; j++) {
  305. i = chip->irq_base + j;
  306. irq_set_chip_and_handler(i, &ct->chip, ct->handler);
  307. irq_set_chip_data(i, gc);
  308. irq_clear_status_flags(i, IRQ_NOREQUEST | IRQ_NOPROBE);
  309. }
  310. gc->irq_cnt = i - gc->irq_base;
  311. }
  312. return 0;
  313. }
  314. /* The platform device used here is instantiated by the MFD device */
  315. static int gsta_probe(struct platform_device *dev)
  316. {
  317. int i, err;
  318. struct pci_dev *pdev;
  319. struct sta2x11_gpio_pdata *gpio_pdata;
  320. struct gsta_gpio *chip;
  321. struct resource *res;
  322. pdev = *(struct pci_dev **)dev_get_platdata(&dev->dev);
  323. gpio_pdata = dev_get_platdata(&pdev->dev);
  324. if (gpio_pdata == NULL)
  325. dev_err(&dev->dev, "no gpio config\n");
  326. pr_debug("gpio config: %p\n", gpio_pdata);
  327. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  328. chip = devm_kzalloc(&dev->dev, sizeof(*chip), GFP_KERNEL);
  329. if (!chip)
  330. return -ENOMEM;
  331. chip->dev = &dev->dev;
  332. chip->reg_base = devm_ioremap_resource(&dev->dev, res);
  333. if (IS_ERR(chip->reg_base))
  334. return PTR_ERR(chip->reg_base);
  335. for (i = 0; i < GSTA_NR_BLOCKS; i++) {
  336. chip->regs[i] = chip->reg_base + i * 4096;
  337. /* disable all irqs */
  338. writel(0, &chip->regs[i]->rimsc);
  339. writel(0, &chip->regs[i]->fimsc);
  340. writel(~0, &chip->regs[i]->ic);
  341. }
  342. spin_lock_init(&chip->lock);
  343. gsta_gpio_setup(chip);
  344. if (gpio_pdata)
  345. for (i = 0; i < GSTA_NR_GPIO; i++)
  346. gsta_set_config(chip, i, gpio_pdata->pinconfig[i]);
  347. /* 384 was used in previous code: be compatible for other drivers */
  348. err = devm_irq_alloc_descs(&dev->dev, -1, 384,
  349. GSTA_NR_GPIO, NUMA_NO_NODE);
  350. if (err < 0) {
  351. dev_warn(&dev->dev, "sta2x11 gpio: Can't get irq base (%i)\n",
  352. -err);
  353. return err;
  354. }
  355. chip->irq_base = err;
  356. err = gsta_alloc_irq_chip(chip);
  357. if (err)
  358. return err;
  359. err = devm_request_irq(&dev->dev, pdev->irq, gsta_gpio_handler,
  360. IRQF_SHARED, KBUILD_MODNAME, chip);
  361. if (err < 0) {
  362. dev_err(&dev->dev, "sta2x11 gpio: Can't request irq (%i)\n",
  363. -err);
  364. return err;
  365. }
  366. err = devm_gpiochip_add_data(&dev->dev, &chip->gpio, chip);
  367. if (err < 0) {
  368. dev_err(&dev->dev, "sta2x11 gpio: Can't register (%i)\n",
  369. -err);
  370. return err;
  371. }
  372. platform_set_drvdata(dev, chip);
  373. return 0;
  374. }
  375. static struct platform_driver sta2x11_gpio_platform_driver = {
  376. .driver = {
  377. .name = "sta2x11-gpio",
  378. .suppress_bind_attrs = true,
  379. },
  380. .probe = gsta_probe,
  381. };
  382. builtin_platform_driver(sta2x11_gpio_platform_driver);