pinctrl-baytrail.c 19 KB

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