pinctrl-baytrail.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/types.h>
  20. #include <linux/bitops.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/gpio.h>
  23. #include <linux/acpi.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/io.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/pinctrl/pinctrl.h>
  29. /* memory mapped register offsets */
  30. #define BYT_CONF0_REG 0x000
  31. #define BYT_CONF1_REG 0x004
  32. #define BYT_VAL_REG 0x008
  33. #define BYT_DFT_REG 0x00c
  34. #define BYT_INT_STAT_REG 0x800
  35. /* BYT_CONF0_REG register bits */
  36. #define BYT_IODEN BIT(31)
  37. #define BYT_DIRECT_IRQ_EN BIT(27)
  38. #define BYT_TRIG_NEG BIT(26)
  39. #define BYT_TRIG_POS BIT(25)
  40. #define BYT_TRIG_LVL BIT(24)
  41. #define BYT_PULL_STR_SHIFT 9
  42. #define BYT_PULL_STR_MASK (3 << BYT_PULL_STR_SHIFT)
  43. #define BYT_PULL_STR_2K (0 << BYT_PULL_STR_SHIFT)
  44. #define BYT_PULL_STR_10K (1 << BYT_PULL_STR_SHIFT)
  45. #define BYT_PULL_STR_20K (2 << BYT_PULL_STR_SHIFT)
  46. #define BYT_PULL_STR_40K (3 << BYT_PULL_STR_SHIFT)
  47. #define BYT_PULL_ASSIGN_SHIFT 7
  48. #define BYT_PULL_ASSIGN_MASK (3 << BYT_PULL_ASSIGN_SHIFT)
  49. #define BYT_PULL_ASSIGN_UP (1 << BYT_PULL_ASSIGN_SHIFT)
  50. #define BYT_PULL_ASSIGN_DOWN (2 << BYT_PULL_ASSIGN_SHIFT)
  51. #define BYT_PIN_MUX 0x07
  52. /* BYT_VAL_REG register bits */
  53. #define BYT_INPUT_EN BIT(2) /* 0: input enabled (active low)*/
  54. #define BYT_OUTPUT_EN BIT(1) /* 0: output enabled (active low)*/
  55. #define BYT_LEVEL BIT(0)
  56. #define BYT_DIR_MASK (BIT(1) | BIT(2))
  57. #define BYT_TRIG_MASK (BIT(26) | BIT(25) | BIT(24))
  58. #define BYT_CONF0_RESTORE_MASK (BYT_DIRECT_IRQ_EN | BYT_TRIG_MASK | \
  59. BYT_PIN_MUX)
  60. #define BYT_VAL_RESTORE_MASK (BYT_DIR_MASK | BYT_LEVEL)
  61. #define BYT_NGPIO_SCORE 102
  62. #define BYT_NGPIO_NCORE 28
  63. #define BYT_NGPIO_SUS 44
  64. #define BYT_SCORE_ACPI_UID "1"
  65. #define BYT_NCORE_ACPI_UID "2"
  66. #define BYT_SUS_ACPI_UID "3"
  67. /*
  68. * Baytrail gpio controller consist of three separate sub-controllers called
  69. * SCORE, NCORE and SUS. The sub-controllers are identified by their acpi UID.
  70. *
  71. * GPIO numbering is _not_ ordered meaning that gpio # 0 in ACPI namespace does
  72. * _not_ correspond to the first gpio register at controller's gpio base.
  73. * There is no logic or pattern in mapping gpio numbers to registers (pads) so
  74. * each sub-controller needs to have its own mapping table
  75. */
  76. /* score_pins[gpio_nr] = pad_nr */
  77. static unsigned const score_pins[BYT_NGPIO_SCORE] = {
  78. 85, 89, 93, 96, 99, 102, 98, 101, 34, 37,
  79. 36, 38, 39, 35, 40, 84, 62, 61, 64, 59,
  80. 54, 56, 60, 55, 63, 57, 51, 50, 53, 47,
  81. 52, 49, 48, 43, 46, 41, 45, 42, 58, 44,
  82. 95, 105, 70, 68, 67, 66, 69, 71, 65, 72,
  83. 86, 90, 88, 92, 103, 77, 79, 83, 78, 81,
  84. 80, 82, 13, 12, 15, 14, 17, 18, 19, 16,
  85. 2, 1, 0, 4, 6, 7, 9, 8, 33, 32,
  86. 31, 30, 29, 27, 25, 28, 26, 23, 21, 20,
  87. 24, 22, 5, 3, 10, 11, 106, 87, 91, 104,
  88. 97, 100,
  89. };
  90. static unsigned const ncore_pins[BYT_NGPIO_NCORE] = {
  91. 19, 18, 17, 20, 21, 22, 24, 25, 23, 16,
  92. 14, 15, 12, 26, 27, 1, 4, 8, 11, 0,
  93. 3, 6, 10, 13, 2, 5, 9, 7,
  94. };
  95. static unsigned const sus_pins[BYT_NGPIO_SUS] = {
  96. 29, 33, 30, 31, 32, 34, 36, 35, 38, 37,
  97. 18, 7, 11, 20, 17, 1, 8, 10, 19, 12,
  98. 0, 2, 23, 39, 28, 27, 22, 21, 24, 25,
  99. 26, 51, 56, 54, 49, 55, 48, 57, 50, 58,
  100. 52, 53, 59, 40,
  101. };
  102. static struct pinctrl_gpio_range byt_ranges[] = {
  103. {
  104. .name = BYT_SCORE_ACPI_UID, /* match with acpi _UID in probe */
  105. .npins = BYT_NGPIO_SCORE,
  106. .pins = score_pins,
  107. },
  108. {
  109. .name = BYT_NCORE_ACPI_UID,
  110. .npins = BYT_NGPIO_NCORE,
  111. .pins = ncore_pins,
  112. },
  113. {
  114. .name = BYT_SUS_ACPI_UID,
  115. .npins = BYT_NGPIO_SUS,
  116. .pins = sus_pins,
  117. },
  118. {
  119. },
  120. };
  121. struct byt_gpio_pin_context {
  122. u32 conf0;
  123. u32 val;
  124. };
  125. struct byt_gpio {
  126. struct gpio_chip chip;
  127. struct platform_device *pdev;
  128. raw_spinlock_t lock;
  129. void __iomem *reg_base;
  130. struct pinctrl_gpio_range *range;
  131. struct byt_gpio_pin_context *saved_context;
  132. };
  133. #define to_byt_gpio(c) container_of(c, struct byt_gpio, chip)
  134. static void __iomem *byt_gpio_reg(struct gpio_chip *chip, unsigned offset,
  135. int reg)
  136. {
  137. struct byt_gpio *vg = to_byt_gpio(chip);
  138. u32 reg_offset;
  139. if (reg == BYT_INT_STAT_REG)
  140. reg_offset = (offset / 32) * 4;
  141. else
  142. reg_offset = vg->range->pins[offset] * 16;
  143. return vg->reg_base + reg_offset + reg;
  144. }
  145. static void byt_gpio_clear_triggering(struct byt_gpio *vg, unsigned offset)
  146. {
  147. void __iomem *reg = byt_gpio_reg(&vg->chip, offset, BYT_CONF0_REG);
  148. unsigned long flags;
  149. u32 value;
  150. raw_spin_lock_irqsave(&vg->lock, flags);
  151. value = readl(reg);
  152. value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
  153. writel(value, reg);
  154. raw_spin_unlock_irqrestore(&vg->lock, flags);
  155. }
  156. static u32 byt_get_gpio_mux(struct byt_gpio *vg, unsigned offset)
  157. {
  158. /* SCORE pin 92-93 */
  159. if (!strcmp(vg->range->name, BYT_SCORE_ACPI_UID) &&
  160. offset >= 92 && offset <= 93)
  161. return 1;
  162. /* SUS pin 11-21 */
  163. if (!strcmp(vg->range->name, BYT_SUS_ACPI_UID) &&
  164. offset >= 11 && offset <= 21)
  165. return 1;
  166. return 0;
  167. }
  168. static int byt_gpio_request(struct gpio_chip *chip, unsigned offset)
  169. {
  170. struct byt_gpio *vg = to_byt_gpio(chip);
  171. void __iomem *reg = byt_gpio_reg(chip, offset, BYT_CONF0_REG);
  172. u32 value, gpio_mux;
  173. unsigned long flags;
  174. raw_spin_lock_irqsave(&vg->lock, flags);
  175. /*
  176. * In most cases, func pin mux 000 means GPIO function.
  177. * But, some pins may have func pin mux 001 represents
  178. * GPIO function.
  179. *
  180. * Because there are devices out there where some pins were not
  181. * configured correctly we allow changing the mux value from
  182. * request (but print out warning about that).
  183. */
  184. value = readl(reg) & BYT_PIN_MUX;
  185. gpio_mux = byt_get_gpio_mux(vg, offset);
  186. if (WARN_ON(gpio_mux != value)) {
  187. value = readl(reg) & ~BYT_PIN_MUX;
  188. value |= gpio_mux;
  189. writel(value, reg);
  190. dev_warn(&vg->pdev->dev,
  191. "pin %u forcibly re-configured as GPIO\n", offset);
  192. }
  193. raw_spin_unlock_irqrestore(&vg->lock, flags);
  194. pm_runtime_get(&vg->pdev->dev);
  195. return 0;
  196. }
  197. static void byt_gpio_free(struct gpio_chip *chip, unsigned offset)
  198. {
  199. struct byt_gpio *vg = to_byt_gpio(chip);
  200. byt_gpio_clear_triggering(vg, offset);
  201. pm_runtime_put(&vg->pdev->dev);
  202. }
  203. static int byt_irq_type(struct irq_data *d, unsigned type)
  204. {
  205. struct byt_gpio *vg = to_byt_gpio(irq_data_get_irq_chip_data(d));
  206. u32 offset = irqd_to_hwirq(d);
  207. u32 value;
  208. unsigned long flags;
  209. void __iomem *reg = byt_gpio_reg(&vg->chip, offset, BYT_CONF0_REG);
  210. if (offset >= vg->chip.ngpio)
  211. return -EINVAL;
  212. raw_spin_lock_irqsave(&vg->lock, flags);
  213. value = readl(reg);
  214. WARN(value & BYT_DIRECT_IRQ_EN,
  215. "Bad pad config for io mode, force direct_irq_en bit clearing");
  216. /* For level trigges the BYT_TRIG_POS and BYT_TRIG_NEG bits
  217. * are used to indicate high and low level triggering
  218. */
  219. value &= ~(BYT_DIRECT_IRQ_EN | BYT_TRIG_POS | BYT_TRIG_NEG |
  220. BYT_TRIG_LVL);
  221. writel(value, reg);
  222. if (type & IRQ_TYPE_EDGE_BOTH)
  223. irq_set_handler_locked(d, handle_edge_irq);
  224. else if (type & IRQ_TYPE_LEVEL_MASK)
  225. irq_set_handler_locked(d, handle_level_irq);
  226. raw_spin_unlock_irqrestore(&vg->lock, flags);
  227. return 0;
  228. }
  229. static int byt_gpio_get(struct gpio_chip *chip, unsigned offset)
  230. {
  231. void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
  232. struct byt_gpio *vg = to_byt_gpio(chip);
  233. unsigned long flags;
  234. u32 val;
  235. raw_spin_lock_irqsave(&vg->lock, flags);
  236. val = readl(reg);
  237. raw_spin_unlock_irqrestore(&vg->lock, flags);
  238. return val & BYT_LEVEL;
  239. }
  240. static void byt_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  241. {
  242. struct byt_gpio *vg = to_byt_gpio(chip);
  243. void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
  244. unsigned long flags;
  245. u32 old_val;
  246. raw_spin_lock_irqsave(&vg->lock, flags);
  247. old_val = readl(reg);
  248. if (value)
  249. writel(old_val | BYT_LEVEL, reg);
  250. else
  251. writel(old_val & ~BYT_LEVEL, reg);
  252. raw_spin_unlock_irqrestore(&vg->lock, flags);
  253. }
  254. static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  255. {
  256. struct byt_gpio *vg = to_byt_gpio(chip);
  257. void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
  258. unsigned long flags;
  259. u32 value;
  260. raw_spin_lock_irqsave(&vg->lock, flags);
  261. value = readl(reg) | BYT_DIR_MASK;
  262. value &= ~BYT_INPUT_EN; /* active low */
  263. writel(value, reg);
  264. raw_spin_unlock_irqrestore(&vg->lock, flags);
  265. return 0;
  266. }
  267. static int byt_gpio_direction_output(struct gpio_chip *chip,
  268. unsigned gpio, int value)
  269. {
  270. struct byt_gpio *vg = to_byt_gpio(chip);
  271. void __iomem *conf_reg = byt_gpio_reg(chip, gpio, BYT_CONF0_REG);
  272. void __iomem *reg = byt_gpio_reg(chip, gpio, BYT_VAL_REG);
  273. unsigned long flags;
  274. u32 reg_val;
  275. raw_spin_lock_irqsave(&vg->lock, flags);
  276. /*
  277. * Before making any direction modifications, do a check if gpio
  278. * is set for direct IRQ. On baytrail, setting GPIO to output does
  279. * not make sense, so let's at least warn the caller before they shoot
  280. * themselves in the foot.
  281. */
  282. WARN(readl(conf_reg) & BYT_DIRECT_IRQ_EN,
  283. "Potential Error: Setting GPIO with direct_irq_en to output");
  284. reg_val = readl(reg) | BYT_DIR_MASK;
  285. reg_val &= ~(BYT_OUTPUT_EN | BYT_INPUT_EN);
  286. if (value)
  287. writel(reg_val | BYT_LEVEL, reg);
  288. else
  289. writel(reg_val & ~BYT_LEVEL, reg);
  290. raw_spin_unlock_irqrestore(&vg->lock, flags);
  291. return 0;
  292. }
  293. static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  294. {
  295. struct byt_gpio *vg = to_byt_gpio(chip);
  296. int i;
  297. u32 conf0, val, offs;
  298. for (i = 0; i < vg->chip.ngpio; i++) {
  299. const char *pull_str = NULL;
  300. const char *pull = NULL;
  301. unsigned long flags;
  302. const char *label;
  303. offs = vg->range->pins[i] * 16;
  304. raw_spin_lock_irqsave(&vg->lock, flags);
  305. conf0 = readl(vg->reg_base + offs + BYT_CONF0_REG);
  306. val = readl(vg->reg_base + offs + BYT_VAL_REG);
  307. raw_spin_unlock_irqrestore(&vg->lock, flags);
  308. label = gpiochip_is_requested(chip, i);
  309. if (!label)
  310. label = "Unrequested";
  311. switch (conf0 & BYT_PULL_ASSIGN_MASK) {
  312. case BYT_PULL_ASSIGN_UP:
  313. pull = "up";
  314. break;
  315. case BYT_PULL_ASSIGN_DOWN:
  316. pull = "down";
  317. break;
  318. }
  319. switch (conf0 & BYT_PULL_STR_MASK) {
  320. case BYT_PULL_STR_2K:
  321. pull_str = "2k";
  322. break;
  323. case BYT_PULL_STR_10K:
  324. pull_str = "10k";
  325. break;
  326. case BYT_PULL_STR_20K:
  327. pull_str = "20k";
  328. break;
  329. case BYT_PULL_STR_40K:
  330. pull_str = "40k";
  331. break;
  332. }
  333. seq_printf(s,
  334. " gpio-%-3d (%-20.20s) %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s",
  335. i,
  336. label,
  337. val & BYT_INPUT_EN ? " " : "in",
  338. val & BYT_OUTPUT_EN ? " " : "out",
  339. val & BYT_LEVEL ? "hi" : "lo",
  340. vg->range->pins[i], offs,
  341. conf0 & 0x7,
  342. conf0 & BYT_TRIG_NEG ? " fall" : " ",
  343. conf0 & BYT_TRIG_POS ? " rise" : " ",
  344. conf0 & BYT_TRIG_LVL ? " level" : " ");
  345. if (pull && pull_str)
  346. seq_printf(s, " %-4s %-3s", pull, pull_str);
  347. else
  348. seq_puts(s, " ");
  349. if (conf0 & BYT_IODEN)
  350. seq_puts(s, " open-drain");
  351. seq_puts(s, "\n");
  352. }
  353. }
  354. static void byt_gpio_irq_handler(struct irq_desc *desc)
  355. {
  356. struct irq_data *data = irq_desc_get_irq_data(desc);
  357. struct byt_gpio *vg = to_byt_gpio(irq_desc_get_handler_data(desc));
  358. struct irq_chip *chip = irq_data_get_irq_chip(data);
  359. u32 base, pin;
  360. void __iomem *reg;
  361. unsigned long pending;
  362. unsigned virq;
  363. /* check from GPIO controller which pin triggered the interrupt */
  364. for (base = 0; base < vg->chip.ngpio; base += 32) {
  365. reg = byt_gpio_reg(&vg->chip, base, BYT_INT_STAT_REG);
  366. pending = readl(reg);
  367. for_each_set_bit(pin, &pending, 32) {
  368. virq = irq_find_mapping(vg->chip.irqdomain, base + pin);
  369. generic_handle_irq(virq);
  370. }
  371. }
  372. chip->irq_eoi(data);
  373. }
  374. static void byt_irq_ack(struct irq_data *d)
  375. {
  376. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  377. struct byt_gpio *vg = to_byt_gpio(gc);
  378. unsigned offset = irqd_to_hwirq(d);
  379. void __iomem *reg;
  380. raw_spin_lock(&vg->lock);
  381. reg = byt_gpio_reg(&vg->chip, offset, BYT_INT_STAT_REG);
  382. writel(BIT(offset % 32), reg);
  383. raw_spin_unlock(&vg->lock);
  384. }
  385. static void byt_irq_unmask(struct irq_data *d)
  386. {
  387. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  388. struct byt_gpio *vg = to_byt_gpio(gc);
  389. unsigned offset = irqd_to_hwirq(d);
  390. unsigned long flags;
  391. void __iomem *reg;
  392. u32 value;
  393. reg = byt_gpio_reg(&vg->chip, offset, BYT_CONF0_REG);
  394. raw_spin_lock_irqsave(&vg->lock, flags);
  395. value = readl(reg);
  396. switch (irqd_get_trigger_type(d)) {
  397. case IRQ_TYPE_LEVEL_HIGH:
  398. value |= BYT_TRIG_LVL;
  399. case IRQ_TYPE_EDGE_RISING:
  400. value |= BYT_TRIG_POS;
  401. break;
  402. case IRQ_TYPE_LEVEL_LOW:
  403. value |= BYT_TRIG_LVL;
  404. case IRQ_TYPE_EDGE_FALLING:
  405. value |= BYT_TRIG_NEG;
  406. break;
  407. case IRQ_TYPE_EDGE_BOTH:
  408. value |= (BYT_TRIG_NEG | BYT_TRIG_POS);
  409. break;
  410. }
  411. writel(value, reg);
  412. raw_spin_unlock_irqrestore(&vg->lock, flags);
  413. }
  414. static void byt_irq_mask(struct irq_data *d)
  415. {
  416. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  417. struct byt_gpio *vg = to_byt_gpio(gc);
  418. byt_gpio_clear_triggering(vg, irqd_to_hwirq(d));
  419. }
  420. static struct irq_chip byt_irqchip = {
  421. .name = "BYT-GPIO",
  422. .irq_ack = byt_irq_ack,
  423. .irq_mask = byt_irq_mask,
  424. .irq_unmask = byt_irq_unmask,
  425. .irq_set_type = byt_irq_type,
  426. .flags = IRQCHIP_SKIP_SET_WAKE,
  427. };
  428. static void byt_gpio_irq_init_hw(struct byt_gpio *vg)
  429. {
  430. void __iomem *reg;
  431. u32 base, value;
  432. int i;
  433. /*
  434. * Clear interrupt triggers for all pins that are GPIOs and
  435. * do not use direct IRQ mode. This will prevent spurious
  436. * interrupts from misconfigured pins.
  437. */
  438. for (i = 0; i < vg->chip.ngpio; i++) {
  439. value = readl(byt_gpio_reg(&vg->chip, i, BYT_CONF0_REG));
  440. if ((value & BYT_PIN_MUX) == byt_get_gpio_mux(vg, i) &&
  441. !(value & BYT_DIRECT_IRQ_EN)) {
  442. byt_gpio_clear_triggering(vg, i);
  443. dev_dbg(&vg->pdev->dev, "disabling GPIO %d\n", i);
  444. }
  445. }
  446. /* clear interrupt status trigger registers */
  447. for (base = 0; base < vg->chip.ngpio; base += 32) {
  448. reg = byt_gpio_reg(&vg->chip, base, BYT_INT_STAT_REG);
  449. writel(0xffffffff, reg);
  450. /* make sure trigger bits are cleared, if not then a pin
  451. might be misconfigured in bios */
  452. value = readl(reg);
  453. if (value)
  454. dev_err(&vg->pdev->dev,
  455. "GPIO interrupt error, pins misconfigured\n");
  456. }
  457. }
  458. static int byt_gpio_probe(struct platform_device *pdev)
  459. {
  460. struct byt_gpio *vg;
  461. struct gpio_chip *gc;
  462. struct resource *mem_rc, *irq_rc;
  463. struct device *dev = &pdev->dev;
  464. struct acpi_device *acpi_dev;
  465. struct pinctrl_gpio_range *range;
  466. acpi_handle handle = ACPI_HANDLE(dev);
  467. int ret;
  468. if (acpi_bus_get_device(handle, &acpi_dev))
  469. return -ENODEV;
  470. vg = devm_kzalloc(dev, sizeof(struct byt_gpio), GFP_KERNEL);
  471. if (!vg) {
  472. dev_err(&pdev->dev, "can't allocate byt_gpio chip data\n");
  473. return -ENOMEM;
  474. }
  475. for (range = byt_ranges; range->name; range++) {
  476. if (!strcmp(acpi_dev->pnp.unique_id, range->name)) {
  477. vg->chip.ngpio = range->npins;
  478. vg->range = range;
  479. break;
  480. }
  481. }
  482. if (!vg->chip.ngpio || !vg->range)
  483. return -ENODEV;
  484. vg->pdev = pdev;
  485. platform_set_drvdata(pdev, vg);
  486. mem_rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  487. vg->reg_base = devm_ioremap_resource(dev, mem_rc);
  488. if (IS_ERR(vg->reg_base))
  489. return PTR_ERR(vg->reg_base);
  490. raw_spin_lock_init(&vg->lock);
  491. gc = &vg->chip;
  492. gc->label = dev_name(&pdev->dev);
  493. gc->owner = THIS_MODULE;
  494. gc->request = byt_gpio_request;
  495. gc->free = byt_gpio_free;
  496. gc->direction_input = byt_gpio_direction_input;
  497. gc->direction_output = byt_gpio_direction_output;
  498. gc->get = byt_gpio_get;
  499. gc->set = byt_gpio_set;
  500. gc->dbg_show = byt_gpio_dbg_show;
  501. gc->base = -1;
  502. gc->can_sleep = false;
  503. gc->dev = dev;
  504. #ifdef CONFIG_PM_SLEEP
  505. vg->saved_context = devm_kcalloc(&pdev->dev, gc->ngpio,
  506. sizeof(*vg->saved_context), GFP_KERNEL);
  507. #endif
  508. ret = gpiochip_add(gc);
  509. if (ret) {
  510. dev_err(&pdev->dev, "failed adding byt-gpio chip\n");
  511. return ret;
  512. }
  513. /* set up interrupts */
  514. irq_rc = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  515. if (irq_rc && irq_rc->start) {
  516. byt_gpio_irq_init_hw(vg);
  517. ret = gpiochip_irqchip_add(gc, &byt_irqchip, 0,
  518. handle_simple_irq, IRQ_TYPE_NONE);
  519. if (ret) {
  520. dev_err(dev, "failed to add irqchip\n");
  521. gpiochip_remove(gc);
  522. return ret;
  523. }
  524. gpiochip_set_chained_irqchip(gc, &byt_irqchip,
  525. (unsigned)irq_rc->start,
  526. byt_gpio_irq_handler);
  527. }
  528. pm_runtime_enable(dev);
  529. return 0;
  530. }
  531. #ifdef CONFIG_PM_SLEEP
  532. static int byt_gpio_suspend(struct device *dev)
  533. {
  534. struct platform_device *pdev = to_platform_device(dev);
  535. struct byt_gpio *vg = platform_get_drvdata(pdev);
  536. int i;
  537. for (i = 0; i < vg->chip.ngpio; i++) {
  538. void __iomem *reg;
  539. u32 value;
  540. reg = byt_gpio_reg(&vg->chip, i, BYT_CONF0_REG);
  541. value = readl(reg) & BYT_CONF0_RESTORE_MASK;
  542. vg->saved_context[i].conf0 = value;
  543. reg = byt_gpio_reg(&vg->chip, i, BYT_VAL_REG);
  544. value = readl(reg) & BYT_VAL_RESTORE_MASK;
  545. vg->saved_context[i].val = value;
  546. }
  547. return 0;
  548. }
  549. static int byt_gpio_resume(struct device *dev)
  550. {
  551. struct platform_device *pdev = to_platform_device(dev);
  552. struct byt_gpio *vg = platform_get_drvdata(pdev);
  553. int i;
  554. for (i = 0; i < vg->chip.ngpio; i++) {
  555. void __iomem *reg;
  556. u32 value;
  557. reg = byt_gpio_reg(&vg->chip, i, BYT_CONF0_REG);
  558. value = readl(reg);
  559. if ((value & BYT_CONF0_RESTORE_MASK) !=
  560. vg->saved_context[i].conf0) {
  561. value &= ~BYT_CONF0_RESTORE_MASK;
  562. value |= vg->saved_context[i].conf0;
  563. writel(value, reg);
  564. dev_info(dev, "restored pin %d conf0 %#08x", i, value);
  565. }
  566. reg = byt_gpio_reg(&vg->chip, i, BYT_VAL_REG);
  567. value = readl(reg);
  568. if ((value & BYT_VAL_RESTORE_MASK) !=
  569. vg->saved_context[i].val) {
  570. u32 v;
  571. v = value & ~BYT_VAL_RESTORE_MASK;
  572. v |= vg->saved_context[i].val;
  573. if (v != value) {
  574. writel(v, reg);
  575. dev_dbg(dev, "restored pin %d val %#08x\n",
  576. i, v);
  577. }
  578. }
  579. }
  580. return 0;
  581. }
  582. #endif
  583. #ifdef CONFIG_PM
  584. static int byt_gpio_runtime_suspend(struct device *dev)
  585. {
  586. return 0;
  587. }
  588. static int byt_gpio_runtime_resume(struct device *dev)
  589. {
  590. return 0;
  591. }
  592. #endif
  593. static const struct dev_pm_ops byt_gpio_pm_ops = {
  594. SET_LATE_SYSTEM_SLEEP_PM_OPS(byt_gpio_suspend, byt_gpio_resume)
  595. SET_RUNTIME_PM_OPS(byt_gpio_runtime_suspend, byt_gpio_runtime_resume,
  596. NULL)
  597. };
  598. static const struct acpi_device_id byt_gpio_acpi_match[] = {
  599. { "INT33B2", 0 },
  600. { "INT33FC", 0 },
  601. { }
  602. };
  603. MODULE_DEVICE_TABLE(acpi, byt_gpio_acpi_match);
  604. static int byt_gpio_remove(struct platform_device *pdev)
  605. {
  606. struct byt_gpio *vg = platform_get_drvdata(pdev);
  607. pm_runtime_disable(&pdev->dev);
  608. gpiochip_remove(&vg->chip);
  609. return 0;
  610. }
  611. static struct platform_driver byt_gpio_driver = {
  612. .probe = byt_gpio_probe,
  613. .remove = byt_gpio_remove,
  614. .driver = {
  615. .name = "byt_gpio",
  616. .pm = &byt_gpio_pm_ops,
  617. .acpi_match_table = ACPI_PTR(byt_gpio_acpi_match),
  618. },
  619. };
  620. static int __init byt_gpio_init(void)
  621. {
  622. return platform_driver_register(&byt_gpio_driver);
  623. }
  624. subsys_initcall(byt_gpio_init);
  625. static void __exit byt_gpio_exit(void)
  626. {
  627. platform_driver_unregister(&byt_gpio_driver);
  628. }
  629. module_exit(byt_gpio_exit);