pinctrl-baytrail.c 19 KB

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