pinctrl-baytrail.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. #define to_byt_gpio(c) container_of(c, struct byt_gpio, chip)
  120. static void __iomem *byt_gpio_reg(struct gpio_chip *chip, unsigned offset,
  121. int reg)
  122. {
  123. struct byt_gpio *vg = to_byt_gpio(chip);
  124. u32 reg_offset;
  125. if (reg == BYT_INT_STAT_REG)
  126. reg_offset = (offset / 32) * 4;
  127. else
  128. reg_offset = vg->range->pins[offset] * 16;
  129. return vg->reg_base + reg_offset + reg;
  130. }
  131. static int byt_gpio_request(struct gpio_chip *chip, unsigned offset)
  132. {
  133. struct byt_gpio *vg = to_byt_gpio(chip);
  134. pm_runtime_get(&vg->pdev->dev);
  135. return 0;
  136. }
  137. static void byt_gpio_free(struct gpio_chip *chip, unsigned offset)
  138. {
  139. struct byt_gpio *vg = to_byt_gpio(chip);
  140. void __iomem *reg = byt_gpio_reg(&vg->chip, offset, BYT_CONF0_REG);
  141. u32 value;
  142. /* clear interrupt triggering */
  143. value = readl(reg);
  144. value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
  145. writel(value, reg);
  146. pm_runtime_put(&vg->pdev->dev);
  147. }
  148. static int byt_irq_type(struct irq_data *d, unsigned type)
  149. {
  150. struct byt_gpio *vg = irq_data_get_irq_chip_data(d);
  151. u32 offset = irqd_to_hwirq(d);
  152. u32 value;
  153. unsigned long flags;
  154. void __iomem *reg = byt_gpio_reg(&vg->chip, offset, BYT_CONF0_REG);
  155. if (offset >= vg->chip.ngpio)
  156. return -EINVAL;
  157. spin_lock_irqsave(&vg->lock, flags);
  158. value = readl(reg);
  159. /* For level trigges the BYT_TRIG_POS and BYT_TRIG_NEG bits
  160. * are used to indicate high and low level triggering
  161. */
  162. value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
  163. switch (type) {
  164. case IRQ_TYPE_LEVEL_HIGH:
  165. value |= BYT_TRIG_LVL;
  166. case IRQ_TYPE_EDGE_RISING:
  167. value |= BYT_TRIG_POS;
  168. break;
  169. case IRQ_TYPE_LEVEL_LOW:
  170. value |= BYT_TRIG_LVL;
  171. case IRQ_TYPE_EDGE_FALLING:
  172. value |= BYT_TRIG_NEG;
  173. break;
  174. case IRQ_TYPE_EDGE_BOTH:
  175. value |= (BYT_TRIG_NEG | BYT_TRIG_POS);
  176. break;
  177. }
  178. writel(value, reg);
  179. spin_unlock_irqrestore(&vg->lock, flags);
  180. return 0;
  181. }
  182. static int byt_gpio_get(struct gpio_chip *chip, unsigned offset)
  183. {
  184. void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
  185. return readl(reg) & BYT_LEVEL;
  186. }
  187. static void byt_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  188. {
  189. struct byt_gpio *vg = to_byt_gpio(chip);
  190. void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
  191. unsigned long flags;
  192. u32 old_val;
  193. spin_lock_irqsave(&vg->lock, flags);
  194. old_val = readl(reg);
  195. if (value)
  196. writel(old_val | BYT_LEVEL, reg);
  197. else
  198. writel(old_val & ~BYT_LEVEL, reg);
  199. spin_unlock_irqrestore(&vg->lock, flags);
  200. }
  201. static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  202. {
  203. struct byt_gpio *vg = to_byt_gpio(chip);
  204. void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
  205. unsigned long flags;
  206. u32 value;
  207. spin_lock_irqsave(&vg->lock, flags);
  208. value = readl(reg) | BYT_DIR_MASK;
  209. value &= ~BYT_INPUT_EN; /* active low */
  210. writel(value, reg);
  211. spin_unlock_irqrestore(&vg->lock, flags);
  212. return 0;
  213. }
  214. static int byt_gpio_direction_output(struct gpio_chip *chip,
  215. unsigned gpio, int value)
  216. {
  217. struct byt_gpio *vg = to_byt_gpio(chip);
  218. void __iomem *reg = byt_gpio_reg(chip, gpio, BYT_VAL_REG);
  219. unsigned long flags;
  220. u32 reg_val;
  221. spin_lock_irqsave(&vg->lock, flags);
  222. reg_val = readl(reg) | BYT_DIR_MASK;
  223. reg_val &= ~BYT_OUTPUT_EN;
  224. if (value)
  225. writel(reg_val | BYT_LEVEL, reg);
  226. else
  227. writel(reg_val & ~BYT_LEVEL, reg);
  228. spin_unlock_irqrestore(&vg->lock, flags);
  229. return 0;
  230. }
  231. static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  232. {
  233. struct byt_gpio *vg = to_byt_gpio(chip);
  234. int i;
  235. unsigned long flags;
  236. u32 conf0, val, offs;
  237. spin_lock_irqsave(&vg->lock, flags);
  238. for (i = 0; i < vg->chip.ngpio; i++) {
  239. offs = vg->range->pins[i] * 16;
  240. conf0 = readl(vg->reg_base + offs + BYT_CONF0_REG);
  241. val = readl(vg->reg_base + offs + BYT_VAL_REG);
  242. seq_printf(s,
  243. " gpio-%-3d %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s\n",
  244. i,
  245. val & BYT_INPUT_EN ? " " : "in",
  246. val & BYT_OUTPUT_EN ? " " : "out",
  247. val & BYT_LEVEL ? "hi" : "lo",
  248. vg->range->pins[i], offs,
  249. conf0 & 0x7,
  250. conf0 & BYT_TRIG_NEG ? " fall" : "",
  251. conf0 & BYT_TRIG_POS ? " rise" : "",
  252. conf0 & BYT_TRIG_LVL ? " level" : "");
  253. }
  254. spin_unlock_irqrestore(&vg->lock, flags);
  255. }
  256. static int byt_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  257. {
  258. struct byt_gpio *vg = to_byt_gpio(chip);
  259. return irq_create_mapping(vg->domain, offset);
  260. }
  261. static void byt_gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  262. {
  263. struct irq_data *data = irq_desc_get_irq_data(desc);
  264. struct byt_gpio *vg = irq_data_get_irq_handler_data(data);
  265. struct irq_chip *chip = irq_data_get_irq_chip(data);
  266. u32 base, pin, mask;
  267. void __iomem *reg;
  268. u32 pending;
  269. unsigned virq;
  270. int looplimit = 0;
  271. /* check from GPIO controller which pin triggered the interrupt */
  272. for (base = 0; base < vg->chip.ngpio; base += 32) {
  273. reg = byt_gpio_reg(&vg->chip, base, BYT_INT_STAT_REG);
  274. while ((pending = readl(reg))) {
  275. pin = __ffs(pending);
  276. mask = BIT(pin);
  277. /* Clear before handling so we can't lose an edge */
  278. writel(mask, reg);
  279. virq = irq_find_mapping(vg->domain, base + pin);
  280. generic_handle_irq(virq);
  281. /* In case bios or user sets triggering incorretly a pin
  282. * might remain in "interrupt triggered" state.
  283. */
  284. if (looplimit++ > 32) {
  285. dev_err(&vg->pdev->dev,
  286. "Gpio %d interrupt flood, disabling\n",
  287. base + pin);
  288. reg = byt_gpio_reg(&vg->chip, base + pin,
  289. BYT_CONF0_REG);
  290. mask = readl(reg);
  291. mask &= ~(BYT_TRIG_NEG | BYT_TRIG_POS |
  292. BYT_TRIG_LVL);
  293. writel(mask, reg);
  294. mask = readl(reg); /* flush */
  295. break;
  296. }
  297. }
  298. }
  299. chip->irq_eoi(data);
  300. }
  301. static void byt_irq_unmask(struct irq_data *d)
  302. {
  303. }
  304. static void byt_irq_mask(struct irq_data *d)
  305. {
  306. }
  307. static struct irq_chip byt_irqchip = {
  308. .name = "BYT-GPIO",
  309. .irq_mask = byt_irq_mask,
  310. .irq_unmask = byt_irq_unmask,
  311. .irq_set_type = byt_irq_type,
  312. };
  313. static void byt_gpio_irq_init_hw(struct byt_gpio *vg)
  314. {
  315. void __iomem *reg;
  316. u32 base, value;
  317. /* clear interrupt status trigger registers */
  318. for (base = 0; base < vg->chip.ngpio; base += 32) {
  319. reg = byt_gpio_reg(&vg->chip, base, BYT_INT_STAT_REG);
  320. writel(0xffffffff, reg);
  321. /* make sure trigger bits are cleared, if not then a pin
  322. might be misconfigured in bios */
  323. value = readl(reg);
  324. if (value)
  325. dev_err(&vg->pdev->dev,
  326. "GPIO interrupt error, pins misconfigured\n");
  327. }
  328. }
  329. static int byt_gpio_irq_map(struct irq_domain *d, unsigned int virq,
  330. irq_hw_number_t hw)
  331. {
  332. struct byt_gpio *vg = d->host_data;
  333. irq_set_chip_and_handler_name(virq, &byt_irqchip, handle_simple_irq,
  334. "demux");
  335. irq_set_chip_data(virq, vg);
  336. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  337. return 0;
  338. }
  339. static const struct irq_domain_ops byt_gpio_irq_ops = {
  340. .map = byt_gpio_irq_map,
  341. };
  342. static int byt_gpio_probe(struct platform_device *pdev)
  343. {
  344. struct byt_gpio *vg;
  345. struct gpio_chip *gc;
  346. struct resource *mem_rc, *irq_rc;
  347. struct device *dev = &pdev->dev;
  348. struct acpi_device *acpi_dev;
  349. struct pinctrl_gpio_range *range;
  350. acpi_handle handle = ACPI_HANDLE(dev);
  351. unsigned hwirq;
  352. int ret;
  353. if (acpi_bus_get_device(handle, &acpi_dev))
  354. return -ENODEV;
  355. vg = devm_kzalloc(dev, sizeof(struct byt_gpio), GFP_KERNEL);
  356. if (!vg) {
  357. dev_err(&pdev->dev, "can't allocate byt_gpio chip data\n");
  358. return -ENOMEM;
  359. }
  360. for (range = byt_ranges; range->name; range++) {
  361. if (!strcmp(acpi_dev->pnp.unique_id, range->name)) {
  362. vg->chip.ngpio = range->npins;
  363. vg->range = range;
  364. break;
  365. }
  366. }
  367. if (!vg->chip.ngpio || !vg->range)
  368. return -ENODEV;
  369. vg->pdev = pdev;
  370. platform_set_drvdata(pdev, vg);
  371. mem_rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  372. vg->reg_base = devm_ioremap_resource(dev, mem_rc);
  373. if (IS_ERR(vg->reg_base))
  374. return PTR_ERR(vg->reg_base);
  375. spin_lock_init(&vg->lock);
  376. gc = &vg->chip;
  377. gc->label = dev_name(&pdev->dev);
  378. gc->owner = THIS_MODULE;
  379. gc->request = byt_gpio_request;
  380. gc->free = byt_gpio_free;
  381. gc->direction_input = byt_gpio_direction_input;
  382. gc->direction_output = byt_gpio_direction_output;
  383. gc->get = byt_gpio_get;
  384. gc->set = byt_gpio_set;
  385. gc->dbg_show = byt_gpio_dbg_show;
  386. gc->base = -1;
  387. gc->can_sleep = 0;
  388. gc->dev = dev;
  389. ret = gpiochip_add(gc);
  390. if (ret) {
  391. dev_err(&pdev->dev, "failed adding byt-gpio chip\n");
  392. return ret;
  393. }
  394. /* set up interrupts */
  395. irq_rc = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  396. if (irq_rc && irq_rc->start) {
  397. hwirq = irq_rc->start;
  398. gc->to_irq = byt_gpio_to_irq;
  399. vg->domain = irq_domain_add_linear(NULL, gc->ngpio,
  400. &byt_gpio_irq_ops, vg);
  401. if (!vg->domain)
  402. return -ENXIO;
  403. byt_gpio_irq_init_hw(vg);
  404. irq_set_handler_data(hwirq, vg);
  405. irq_set_chained_handler(hwirq, byt_gpio_irq_handler);
  406. /* Register interrupt handlers for gpio signaled acpi events */
  407. acpi_gpiochip_request_interrupts(gc);
  408. }
  409. pm_runtime_enable(dev);
  410. return 0;
  411. }
  412. static int byt_gpio_runtime_suspend(struct device *dev)
  413. {
  414. return 0;
  415. }
  416. static int byt_gpio_runtime_resume(struct device *dev)
  417. {
  418. return 0;
  419. }
  420. static const struct dev_pm_ops byt_gpio_pm_ops = {
  421. .runtime_suspend = byt_gpio_runtime_suspend,
  422. .runtime_resume = byt_gpio_runtime_resume,
  423. };
  424. static const struct acpi_device_id byt_gpio_acpi_match[] = {
  425. { "INT33B2", 0 },
  426. { }
  427. };
  428. MODULE_DEVICE_TABLE(acpi, byt_gpio_acpi_match);
  429. static int byt_gpio_remove(struct platform_device *pdev)
  430. {
  431. struct byt_gpio *vg = platform_get_drvdata(pdev);
  432. int err;
  433. pm_runtime_disable(&pdev->dev);
  434. err = gpiochip_remove(&vg->chip);
  435. if (err)
  436. dev_warn(&pdev->dev, "failed to remove gpio_chip.\n");
  437. return 0;
  438. }
  439. static struct platform_driver byt_gpio_driver = {
  440. .probe = byt_gpio_probe,
  441. .remove = byt_gpio_remove,
  442. .driver = {
  443. .name = "byt_gpio",
  444. .owner = THIS_MODULE,
  445. .pm = &byt_gpio_pm_ops,
  446. .acpi_match_table = ACPI_PTR(byt_gpio_acpi_match),
  447. },
  448. };
  449. static int __init byt_gpio_init(void)
  450. {
  451. return platform_driver_register(&byt_gpio_driver);
  452. }
  453. subsys_initcall(byt_gpio_init);