pinctrl-baytrail.c 13 KB

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