pinctrl-baytrail.c 14 KB

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