gpio-sta2x11.c 11 KB

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