pinctrl-baytrail.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * Pinctrl GPIO driver for Intel Baytrail
  3. * Copyright (c) 2012-2013, Intel Corporation.
  4. *
  5. * Author: Mathias Nyman <mathias.nyman@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/types.h>
  25. #include <linux/bitops.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/irq.h>
  28. #include <linux/gpio.h>
  29. #include <linux/irqdomain.h>
  30. #include <linux/acpi.h>
  31. #include <linux/acpi_gpio.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/io.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/pinctrl/pinctrl.h>
  37. /* memory mapped register offsets */
  38. #define BYT_CONF0_REG 0x000
  39. #define BYT_CONF1_REG 0x004
  40. #define BYT_VAL_REG 0x008
  41. #define BYT_DFT_REG 0x00c
  42. #define BYT_INT_STAT_REG 0x800
  43. /* BYT_CONF0_REG register bits */
  44. #define BYT_TRIG_NEG BIT(26)
  45. #define BYT_TRIG_POS BIT(25)
  46. #define BYT_TRIG_LVL BIT(24)
  47. #define BYT_PIN_MUX 0x07
  48. /* BYT_VAL_REG register bits */
  49. #define BYT_INPUT_EN BIT(2) /* 0: input enabled (active low)*/
  50. #define BYT_OUTPUT_EN BIT(1) /* 0: output enabled (active low)*/
  51. #define BYT_LEVEL BIT(0)
  52. #define BYT_DIR_MASK (BIT(1) | BIT(2))
  53. #define BYT_TRIG_MASK (BIT(26) | BIT(25) | BIT(24))
  54. #define BYT_NGPIO_SCORE 102
  55. #define BYT_NGPIO_NCORE 28
  56. #define BYT_NGPIO_SUS 44
  57. /*
  58. * Baytrail gpio controller consist of three separate sub-controllers called
  59. * SCORE, NCORE and SUS. The sub-controllers are identified by their acpi UID.
  60. *
  61. * GPIO numbering is _not_ ordered meaning that gpio # 0 in ACPI namespace does
  62. * _not_ correspond to the first gpio register at controller's gpio base.
  63. * There is no logic or pattern in mapping gpio numbers to registers (pads) so
  64. * each sub-controller needs to have its own mapping table
  65. */
  66. /* score_pins[gpio_nr] = pad_nr */
  67. static unsigned const score_pins[BYT_NGPIO_SCORE] = {
  68. 85, 89, 93, 96, 99, 102, 98, 101, 34, 37,
  69. 36, 38, 39, 35, 40, 84, 62, 61, 64, 59,
  70. 54, 56, 60, 55, 63, 57, 51, 50, 53, 47,
  71. 52, 49, 48, 43, 46, 41, 45, 42, 58, 44,
  72. 95, 105, 70, 68, 67, 66, 69, 71, 65, 72,
  73. 86, 90, 88, 92, 103, 77, 79, 83, 78, 81,
  74. 80, 82, 13, 12, 15, 14, 17, 18, 19, 16,
  75. 2, 1, 0, 4, 6, 7, 9, 8, 33, 32,
  76. 31, 30, 29, 27, 25, 28, 26, 23, 21, 20,
  77. 24, 22, 5, 3, 10, 11, 106, 87, 91, 104,
  78. 97, 100,
  79. };
  80. static unsigned const ncore_pins[BYT_NGPIO_NCORE] = {
  81. 19, 18, 17, 20, 21, 22, 24, 25, 23, 16,
  82. 14, 15, 12, 26, 27, 1, 4, 8, 11, 0,
  83. 3, 6, 10, 13, 2, 5, 9, 7,
  84. };
  85. static unsigned const sus_pins[BYT_NGPIO_SUS] = {
  86. 29, 33, 30, 31, 32, 34, 36, 35, 38, 37,
  87. 18, 7, 11, 20, 17, 1, 8, 10, 19, 12,
  88. 0, 2, 23, 39, 28, 27, 22, 21, 24, 25,
  89. 26, 51, 56, 54, 49, 55, 48, 57, 50, 58,
  90. 52, 53, 59, 40,
  91. };
  92. static struct pinctrl_gpio_range byt_ranges[] = {
  93. {
  94. .name = "1", /* match with acpi _UID in probe */
  95. .npins = BYT_NGPIO_SCORE,
  96. .pins = score_pins,
  97. },
  98. {
  99. .name = "2",
  100. .npins = BYT_NGPIO_NCORE,
  101. .pins = ncore_pins,
  102. },
  103. {
  104. .name = "3",
  105. .npins = BYT_NGPIO_SUS,
  106. .pins = sus_pins,
  107. },
  108. {
  109. },
  110. };
  111. struct byt_gpio {
  112. struct gpio_chip chip;
  113. struct irq_domain *domain;
  114. struct platform_device *pdev;
  115. spinlock_t lock;
  116. void __iomem *reg_base;
  117. struct pinctrl_gpio_range *range;
  118. };
  119. static void __iomem *byt_gpio_reg(struct gpio_chip *chip, unsigned offset,
  120. int reg)
  121. {
  122. struct byt_gpio *vg = container_of(chip, struct byt_gpio, chip);
  123. u32 reg_offset;
  124. void __iomem *ptr;
  125. if (reg == BYT_INT_STAT_REG)
  126. reg_offset = (offset / 32) * 4;
  127. else
  128. reg_offset = vg->range->pins[offset] * 16;
  129. ptr = (void __iomem *) (vg->reg_base + reg_offset + reg);
  130. return ptr;
  131. }
  132. static int byt_gpio_request(struct gpio_chip *chip, unsigned offset)
  133. {
  134. struct byt_gpio *vg = container_of(chip, struct byt_gpio, chip);
  135. pm_runtime_get(&vg->pdev->dev);
  136. return 0;
  137. }
  138. static void byt_gpio_free(struct gpio_chip *chip, unsigned offset)
  139. {
  140. struct byt_gpio *vg = container_of(chip, struct byt_gpio, chip);
  141. void __iomem *reg = byt_gpio_reg(&vg->chip, offset, BYT_CONF0_REG);
  142. u32 value;
  143. /* clear interrupt triggering */
  144. value = readl(reg);
  145. value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
  146. writel(value, reg);
  147. pm_runtime_put(&vg->pdev->dev);
  148. }
  149. static int byt_irq_type(struct irq_data *d, unsigned type)
  150. {
  151. struct byt_gpio *vg = irq_data_get_irq_chip_data(d);
  152. u32 offset = irqd_to_hwirq(d);
  153. u32 value;
  154. unsigned long flags;
  155. void __iomem *reg = byt_gpio_reg(&vg->chip, offset, BYT_CONF0_REG);
  156. if (offset >= vg->chip.ngpio)
  157. return -EINVAL;
  158. spin_lock_irqsave(&vg->lock, flags);
  159. value = readl(reg);
  160. /* For level trigges the BYT_TRIG_POS and BYT_TRIG_NEG bits
  161. * are used to indicate high and low level triggering
  162. */
  163. value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
  164. switch (type) {
  165. case IRQ_TYPE_LEVEL_HIGH:
  166. value |= BYT_TRIG_LVL;
  167. case IRQ_TYPE_EDGE_RISING:
  168. value |= BYT_TRIG_POS;
  169. break;
  170. case IRQ_TYPE_LEVEL_LOW:
  171. value |= BYT_TRIG_LVL;
  172. case IRQ_TYPE_EDGE_FALLING:
  173. value |= BYT_TRIG_NEG;
  174. break;
  175. case IRQ_TYPE_EDGE_BOTH:
  176. value |= (BYT_TRIG_NEG | BYT_TRIG_POS);
  177. break;
  178. }
  179. writel(value, reg);
  180. spin_unlock_irqrestore(&vg->lock, flags);
  181. return 0;
  182. }
  183. static int byt_gpio_get(struct gpio_chip *chip, unsigned offset)
  184. {
  185. void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
  186. return readl(reg) & BYT_LEVEL;
  187. }
  188. static void byt_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  189. {
  190. struct byt_gpio *vg = container_of(chip, struct byt_gpio, chip);
  191. void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
  192. unsigned long flags;
  193. u32 old_val;
  194. spin_lock_irqsave(&vg->lock, flags);
  195. old_val = readl(reg);
  196. if (value)
  197. writel(old_val | BYT_LEVEL, reg);
  198. else
  199. writel(old_val & ~BYT_LEVEL, reg);
  200. spin_unlock_irqrestore(&vg->lock, flags);
  201. }
  202. static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  203. {
  204. struct byt_gpio *vg = container_of(chip, struct byt_gpio, chip);
  205. void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
  206. unsigned long flags;
  207. u32 value;
  208. spin_lock_irqsave(&vg->lock, flags);
  209. value = readl(reg) | BYT_DIR_MASK;
  210. value = value & (~BYT_INPUT_EN); /* active low */
  211. writel(value, reg);
  212. spin_unlock_irqrestore(&vg->lock, flags);
  213. return 0;
  214. }
  215. static int byt_gpio_direction_output(struct gpio_chip *chip,
  216. unsigned gpio, int value)
  217. {
  218. struct byt_gpio *vg = container_of(chip, struct byt_gpio, chip);
  219. void __iomem *reg = byt_gpio_reg(chip, gpio, BYT_VAL_REG);
  220. unsigned long flags;
  221. u32 reg_val;
  222. spin_lock_irqsave(&vg->lock, flags);
  223. reg_val = readl(reg) | (BYT_DIR_MASK | !!value);
  224. reg_val &= ~(BYT_OUTPUT_EN | !value);
  225. writel(reg_val, reg);
  226. spin_unlock_irqrestore(&vg->lock, flags);
  227. return 0;
  228. }
  229. static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  230. {
  231. struct byt_gpio *vg = container_of(chip, struct byt_gpio, chip);
  232. int i;
  233. unsigned long flags;
  234. u32 conf0, val, offs;
  235. spin_lock_irqsave(&vg->lock, flags);
  236. for (i = 0; i < vg->chip.ngpio; i++) {
  237. offs = vg->range->pins[i] * 16;
  238. conf0 = readl(vg->reg_base + offs + BYT_CONF0_REG);
  239. val = readl(vg->reg_base + offs + BYT_VAL_REG);
  240. seq_printf(s,
  241. " gpio-%-3d %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s\n",
  242. i,
  243. val & BYT_INPUT_EN ? " " : "in",
  244. val & BYT_OUTPUT_EN ? " " : "out",
  245. val & BYT_LEVEL ? "hi" : "lo",
  246. vg->range->pins[i], offs,
  247. conf0 & 0x7,
  248. conf0 & BYT_TRIG_NEG ? "fall " : "",
  249. conf0 & BYT_TRIG_POS ? "rise " : "",
  250. conf0 & BYT_TRIG_LVL ? "lvl " : "");
  251. }
  252. spin_unlock_irqrestore(&vg->lock, flags);
  253. }
  254. static int byt_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  255. {
  256. struct byt_gpio *vg = container_of(chip, struct byt_gpio, chip);
  257. return irq_create_mapping(vg->domain, offset);
  258. }
  259. static void byt_gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  260. {
  261. struct irq_data *data = irq_desc_get_irq_data(desc);
  262. struct byt_gpio *vg = irq_data_get_irq_handler_data(data);
  263. struct irq_chip *chip = irq_data_get_irq_chip(data);
  264. u32 base, pin, mask;
  265. void __iomem *reg;
  266. u32 pending;
  267. unsigned virq;
  268. int looplimit = 0;
  269. /* check from GPIO controller which pin triggered the interrupt */
  270. for (base = 0; base < vg->chip.ngpio; base += 32) {
  271. reg = byt_gpio_reg(&vg->chip, base, BYT_INT_STAT_REG);
  272. while ((pending = readl(reg))) {
  273. pin = __ffs(pending);
  274. mask = BIT(pin);
  275. /* Clear before handling so we can't lose an edge */
  276. writel(mask, reg);
  277. virq = irq_find_mapping(vg->domain, base + pin);
  278. generic_handle_irq(virq);
  279. /* In case bios or user sets triggering incorretly a pin
  280. * might remain in "interrupt triggered" state.
  281. */
  282. if (looplimit++ > 32) {
  283. dev_err(&vg->pdev->dev,
  284. "Gpio %d interrupt flood, disabling\n",
  285. base + pin);
  286. reg = byt_gpio_reg(&vg->chip, base + pin,
  287. BYT_CONF0_REG);
  288. mask = readl(reg);
  289. mask &= ~(BYT_TRIG_NEG | BYT_TRIG_POS |
  290. BYT_TRIG_LVL);
  291. writel(mask, reg);
  292. mask = readl(reg); /* flush */
  293. break;
  294. }
  295. }
  296. }
  297. chip->irq_eoi(data);
  298. }
  299. static void byt_irq_unmask(struct irq_data *d)
  300. {
  301. }
  302. static void byt_irq_mask(struct irq_data *d)
  303. {
  304. }
  305. static struct irq_chip byt_irqchip = {
  306. .name = "BYT-GPIO",
  307. .irq_mask = byt_irq_mask,
  308. .irq_unmask = byt_irq_unmask,
  309. .irq_set_type = byt_irq_type,
  310. };
  311. static void byt_gpio_irq_init_hw(struct byt_gpio *vg)
  312. {
  313. void __iomem *reg;
  314. u32 base, value;
  315. /* clear interrupt status trigger registers */
  316. for (base = 0; base < vg->chip.ngpio; base += 32) {
  317. reg = byt_gpio_reg(&vg->chip, base, BYT_INT_STAT_REG);
  318. writel(0xffffffff, reg);
  319. /* make sure trigger bits are cleared, if not then a pin
  320. might be misconfigured in bios */
  321. value = readl(reg);
  322. if (value)
  323. dev_err(&vg->pdev->dev,
  324. "GPIO interrupt error, pins misconfigured\n");
  325. }
  326. }
  327. static int byt_gpio_irq_map(struct irq_domain *d, unsigned int virq,
  328. irq_hw_number_t hw)
  329. {
  330. struct byt_gpio *vg = d->host_data;
  331. irq_set_chip_and_handler_name(virq, &byt_irqchip, handle_simple_irq,
  332. "demux");
  333. irq_set_chip_data(virq, vg);
  334. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  335. return 0;
  336. }
  337. static const struct irq_domain_ops byt_gpio_irq_ops = {
  338. .map = byt_gpio_irq_map,
  339. };
  340. static int byt_gpio_probe(struct platform_device *pdev)
  341. {
  342. struct byt_gpio *vg;
  343. struct gpio_chip *gc;
  344. struct resource *mem_rc, *irq_rc;
  345. struct device *dev = &pdev->dev;
  346. struct acpi_device *acpi_dev;
  347. struct pinctrl_gpio_range *range;
  348. acpi_handle handle = ACPI_HANDLE(dev);
  349. unsigned hwirq;
  350. int ret;
  351. if (acpi_bus_get_device(handle, &acpi_dev))
  352. return -ENODEV;
  353. vg = devm_kzalloc(dev, sizeof(struct byt_gpio), GFP_KERNEL);
  354. if (!vg) {
  355. dev_err(&pdev->dev, "can't allocate byt_gpio chip data\n");
  356. return -ENOMEM;
  357. }
  358. for (range = byt_ranges; range->name; range++) {
  359. if (!strcmp(acpi_dev->pnp.unique_id, range->name)) {
  360. vg->chip.ngpio = range->npins;
  361. vg->range = range;
  362. break;
  363. }
  364. }
  365. if (!vg->chip.ngpio || !vg->range)
  366. return -ENODEV;
  367. vg->pdev = pdev;
  368. platform_set_drvdata(pdev, vg);
  369. mem_rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  370. vg->reg_base = devm_ioremap_resource(dev, mem_rc);
  371. if (IS_ERR(vg->reg_base))
  372. return PTR_ERR(vg->reg_base);
  373. spin_lock_init(&vg->lock);
  374. gc = &vg->chip;
  375. gc->label = dev_name(&pdev->dev);
  376. gc->owner = THIS_MODULE;
  377. gc->request = byt_gpio_request;
  378. gc->free = byt_gpio_free;
  379. gc->direction_input = byt_gpio_direction_input;
  380. gc->direction_output = byt_gpio_direction_output;
  381. gc->get = byt_gpio_get;
  382. gc->set = byt_gpio_set;
  383. gc->dbg_show = byt_gpio_dbg_show;
  384. gc->base = -1;
  385. gc->can_sleep = 0;
  386. gc->dev = dev;
  387. ret = gpiochip_add(gc);
  388. if (ret) {
  389. dev_err(&pdev->dev, "failed adding byt-gpio chip\n");
  390. return ret;
  391. }
  392. /* set up interrupts */
  393. irq_rc = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  394. if (irq_rc && irq_rc->start) {
  395. hwirq = irq_rc->start;
  396. gc->to_irq = byt_gpio_to_irq;
  397. vg->domain = irq_domain_add_linear(NULL, gc->ngpio,
  398. &byt_gpio_irq_ops, vg);
  399. if (!vg->domain)
  400. return -ENXIO;
  401. byt_gpio_irq_init_hw(vg);
  402. irq_set_handler_data(hwirq, vg);
  403. irq_set_chained_handler(hwirq, byt_gpio_irq_handler);
  404. /* Register interrupt handlers for gpio signaled acpi events */
  405. acpi_gpiochip_request_interrupts(gc);
  406. }
  407. pm_runtime_enable(dev);
  408. return 0;
  409. }
  410. static int byt_gpio_runtime_suspend(struct device *dev)
  411. {
  412. return 0;
  413. }
  414. static int byt_gpio_runtime_resume(struct device *dev)
  415. {
  416. return 0;
  417. }
  418. static const struct dev_pm_ops byt_gpio_pm_ops = {
  419. .runtime_suspend = byt_gpio_runtime_suspend,
  420. .runtime_resume = byt_gpio_runtime_resume,
  421. };
  422. static const struct acpi_device_id byt_gpio_acpi_match[] = {
  423. { "INT33B2", 0 },
  424. { }
  425. };
  426. MODULE_DEVICE_TABLE(acpi, byt_gpio_acpi_match);
  427. static int byt_gpio_remove(struct platform_device *pdev)
  428. {
  429. struct byt_gpio *vg = platform_get_drvdata(pdev);
  430. int err;
  431. pm_runtime_disable(&pdev->dev);
  432. err = gpiochip_remove(&vg->chip);
  433. if (err)
  434. dev_warn(&pdev->dev, "failed to remove gpio_chip.\n");
  435. return 0;
  436. }
  437. static struct platform_driver byt_gpio_driver = {
  438. .probe = byt_gpio_probe,
  439. .remove = byt_gpio_remove,
  440. .driver = {
  441. .name = "byt_gpio",
  442. .owner = THIS_MODULE,
  443. .pm = &byt_gpio_pm_ops,
  444. .acpi_match_table = ACPI_PTR(byt_gpio_acpi_match),
  445. },
  446. };
  447. static int __init byt_gpio_init(void)
  448. {
  449. return platform_driver_register(&byt_gpio_driver);
  450. }
  451. subsys_initcall(byt_gpio_init);