pinctrl-baytrail.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Pinctrl GPIO driver for Intel Baytrail
  4. *
  5. * Copyright (c) 2012-2013, Intel Corporation
  6. * Author: Mathias Nyman <[email protected]>
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/bitops.h>
  10. #include <linux/gpio/driver.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/property.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include <linux/pinctrl/pinconf.h>
  23. #include <linux/pinctrl/pinconf-generic.h>
  24. #include "pinctrl-intel.h"
  25. /* memory mapped register offsets */
  26. #define BYT_CONF0_REG 0x000
  27. #define BYT_CONF1_REG 0x004
  28. #define BYT_VAL_REG 0x008
  29. #define BYT_DFT_REG 0x00c
  30. #define BYT_INT_STAT_REG 0x800
  31. #define BYT_DIRECT_IRQ_REG 0x980
  32. #define BYT_DEBOUNCE_REG 0x9d0
  33. /* BYT_CONF0_REG register bits */
  34. #define BYT_IODEN BIT(31)
  35. #define BYT_DIRECT_IRQ_EN BIT(27)
  36. #define BYT_TRIG_MASK GENMASK(26, 24)
  37. #define BYT_TRIG_NEG BIT(26)
  38. #define BYT_TRIG_POS BIT(25)
  39. #define BYT_TRIG_LVL BIT(24)
  40. #define BYT_DEBOUNCE_EN BIT(20)
  41. #define BYT_GLITCH_FILTER_EN BIT(19)
  42. #define BYT_GLITCH_F_SLOW_CLK BIT(17)
  43. #define BYT_GLITCH_F_FAST_CLK BIT(16)
  44. #define BYT_PULL_STR_SHIFT 9
  45. #define BYT_PULL_STR_MASK GENMASK(10, 9)
  46. #define BYT_PULL_STR_2K (0 << BYT_PULL_STR_SHIFT)
  47. #define BYT_PULL_STR_10K (1 << BYT_PULL_STR_SHIFT)
  48. #define BYT_PULL_STR_20K (2 << BYT_PULL_STR_SHIFT)
  49. #define BYT_PULL_STR_40K (3 << BYT_PULL_STR_SHIFT)
  50. #define BYT_PULL_ASSIGN_SHIFT 7
  51. #define BYT_PULL_ASSIGN_MASK GENMASK(8, 7)
  52. #define BYT_PULL_ASSIGN_UP (1 << BYT_PULL_ASSIGN_SHIFT)
  53. #define BYT_PULL_ASSIGN_DOWN (2 << BYT_PULL_ASSIGN_SHIFT)
  54. #define BYT_PIN_MUX GENMASK(2, 0)
  55. /* BYT_VAL_REG register bits */
  56. #define BYT_DIR_MASK GENMASK(2, 1)
  57. #define BYT_INPUT_EN BIT(2) /* 0: input enabled (active low)*/
  58. #define BYT_OUTPUT_EN BIT(1) /* 0: output enabled (active low)*/
  59. #define BYT_LEVEL BIT(0)
  60. #define BYT_CONF0_RESTORE_MASK (BYT_DIRECT_IRQ_EN | BYT_TRIG_MASK | BYT_PIN_MUX)
  61. #define BYT_VAL_RESTORE_MASK (BYT_DIR_MASK | BYT_LEVEL)
  62. /* BYT_DEBOUNCE_REG bits */
  63. #define BYT_DEBOUNCE_PULSE_MASK GENMASK(2, 0)
  64. #define BYT_DEBOUNCE_PULSE_375US 1
  65. #define BYT_DEBOUNCE_PULSE_750US 2
  66. #define BYT_DEBOUNCE_PULSE_1500US 3
  67. #define BYT_DEBOUNCE_PULSE_3MS 4
  68. #define BYT_DEBOUNCE_PULSE_6MS 5
  69. #define BYT_DEBOUNCE_PULSE_12MS 6
  70. #define BYT_DEBOUNCE_PULSE_24MS 7
  71. #define BYT_NGPIO_SCORE 102
  72. #define BYT_NGPIO_NCORE 28
  73. #define BYT_NGPIO_SUS 44
  74. #define BYT_SCORE_ACPI_UID "1"
  75. #define BYT_NCORE_ACPI_UID "2"
  76. #define BYT_SUS_ACPI_UID "3"
  77. /*
  78. * This is the function value most pins have for GPIO muxing. If the value
  79. * differs from the default one, it must be explicitly mentioned. Otherwise, the
  80. * pin control implementation will set the muxing value to default GPIO if it
  81. * does not find a match for the requested function.
  82. */
  83. #define BYT_DEFAULT_GPIO_MUX 0
  84. #define BYT_ALTER_GPIO_MUX 1
  85. struct intel_pad_context {
  86. u32 conf0;
  87. u32 val;
  88. };
  89. #define COMMUNITY(p, n, map) \
  90. { \
  91. .pin_base = (p), \
  92. .npins = (n), \
  93. .pad_map = (map),\
  94. }
  95. /* SCORE pins, aka GPIOC_<pin_no> or GPIO_S0_SC[<pin_no>] */
  96. static const struct pinctrl_pin_desc byt_score_pins[] = {
  97. PINCTRL_PIN(0, "SATA_GP0"),
  98. PINCTRL_PIN(1, "SATA_GP1"),
  99. PINCTRL_PIN(2, "SATA_LED#"),
  100. PINCTRL_PIN(3, "PCIE_CLKREQ0"),
  101. PINCTRL_PIN(4, "PCIE_CLKREQ1"),
  102. PINCTRL_PIN(5, "PCIE_CLKREQ2"),
  103. PINCTRL_PIN(6, "PCIE_CLKREQ3"),
  104. PINCTRL_PIN(7, "SD3_WP"),
  105. PINCTRL_PIN(8, "HDA_RST"),
  106. PINCTRL_PIN(9, "HDA_SYNC"),
  107. PINCTRL_PIN(10, "HDA_CLK"),
  108. PINCTRL_PIN(11, "HDA_SDO"),
  109. PINCTRL_PIN(12, "HDA_SDI0"),
  110. PINCTRL_PIN(13, "HDA_SDI1"),
  111. PINCTRL_PIN(14, "GPIO_S0_SC14"),
  112. PINCTRL_PIN(15, "GPIO_S0_SC15"),
  113. PINCTRL_PIN(16, "MMC1_CLK"),
  114. PINCTRL_PIN(17, "MMC1_D0"),
  115. PINCTRL_PIN(18, "MMC1_D1"),
  116. PINCTRL_PIN(19, "MMC1_D2"),
  117. PINCTRL_PIN(20, "MMC1_D3"),
  118. PINCTRL_PIN(21, "MMC1_D4"),
  119. PINCTRL_PIN(22, "MMC1_D5"),
  120. PINCTRL_PIN(23, "MMC1_D6"),
  121. PINCTRL_PIN(24, "MMC1_D7"),
  122. PINCTRL_PIN(25, "MMC1_CMD"),
  123. PINCTRL_PIN(26, "MMC1_RST"),
  124. PINCTRL_PIN(27, "SD2_CLK"),
  125. PINCTRL_PIN(28, "SD2_D0"),
  126. PINCTRL_PIN(29, "SD2_D1"),
  127. PINCTRL_PIN(30, "SD2_D2"),
  128. PINCTRL_PIN(31, "SD2_D3_CD"),
  129. PINCTRL_PIN(32, "SD2_CMD"),
  130. PINCTRL_PIN(33, "SD3_CLK"),
  131. PINCTRL_PIN(34, "SD3_D0"),
  132. PINCTRL_PIN(35, "SD3_D1"),
  133. PINCTRL_PIN(36, "SD3_D2"),
  134. PINCTRL_PIN(37, "SD3_D3"),
  135. PINCTRL_PIN(38, "SD3_CD"),
  136. PINCTRL_PIN(39, "SD3_CMD"),
  137. PINCTRL_PIN(40, "SD3_1P8EN"),
  138. PINCTRL_PIN(41, "SD3_PWREN#"),
  139. PINCTRL_PIN(42, "ILB_LPC_AD0"),
  140. PINCTRL_PIN(43, "ILB_LPC_AD1"),
  141. PINCTRL_PIN(44, "ILB_LPC_AD2"),
  142. PINCTRL_PIN(45, "ILB_LPC_AD3"),
  143. PINCTRL_PIN(46, "ILB_LPC_FRAME"),
  144. PINCTRL_PIN(47, "ILB_LPC_CLK0"),
  145. PINCTRL_PIN(48, "ILB_LPC_CLK1"),
  146. PINCTRL_PIN(49, "ILB_LPC_CLKRUN"),
  147. PINCTRL_PIN(50, "ILB_LPC_SERIRQ"),
  148. PINCTRL_PIN(51, "PCU_SMB_DATA"),
  149. PINCTRL_PIN(52, "PCU_SMB_CLK"),
  150. PINCTRL_PIN(53, "PCU_SMB_ALERT"),
  151. PINCTRL_PIN(54, "ILB_8254_SPKR"),
  152. PINCTRL_PIN(55, "GPIO_S0_SC55"),
  153. PINCTRL_PIN(56, "GPIO_S0_SC56"),
  154. PINCTRL_PIN(57, "GPIO_S0_SC57"),
  155. PINCTRL_PIN(58, "GPIO_S0_SC58"),
  156. PINCTRL_PIN(59, "GPIO_S0_SC59"),
  157. PINCTRL_PIN(60, "GPIO_S0_SC60"),
  158. PINCTRL_PIN(61, "GPIO_S0_SC61"),
  159. PINCTRL_PIN(62, "LPE_I2S2_CLK"),
  160. PINCTRL_PIN(63, "LPE_I2S2_FRM"),
  161. PINCTRL_PIN(64, "LPE_I2S2_DATAIN"),
  162. PINCTRL_PIN(65, "LPE_I2S2_DATAOUT"),
  163. PINCTRL_PIN(66, "SIO_SPI_CS"),
  164. PINCTRL_PIN(67, "SIO_SPI_MISO"),
  165. PINCTRL_PIN(68, "SIO_SPI_MOSI"),
  166. PINCTRL_PIN(69, "SIO_SPI_CLK"),
  167. PINCTRL_PIN(70, "SIO_UART1_RXD"),
  168. PINCTRL_PIN(71, "SIO_UART1_TXD"),
  169. PINCTRL_PIN(72, "SIO_UART1_RTS"),
  170. PINCTRL_PIN(73, "SIO_UART1_CTS"),
  171. PINCTRL_PIN(74, "SIO_UART2_RXD"),
  172. PINCTRL_PIN(75, "SIO_UART2_TXD"),
  173. PINCTRL_PIN(76, "SIO_UART2_RTS"),
  174. PINCTRL_PIN(77, "SIO_UART2_CTS"),
  175. PINCTRL_PIN(78, "SIO_I2C0_DATA"),
  176. PINCTRL_PIN(79, "SIO_I2C0_CLK"),
  177. PINCTRL_PIN(80, "SIO_I2C1_DATA"),
  178. PINCTRL_PIN(81, "SIO_I2C1_CLK"),
  179. PINCTRL_PIN(82, "SIO_I2C2_DATA"),
  180. PINCTRL_PIN(83, "SIO_I2C2_CLK"),
  181. PINCTRL_PIN(84, "SIO_I2C3_DATA"),
  182. PINCTRL_PIN(85, "SIO_I2C3_CLK"),
  183. PINCTRL_PIN(86, "SIO_I2C4_DATA"),
  184. PINCTRL_PIN(87, "SIO_I2C4_CLK"),
  185. PINCTRL_PIN(88, "SIO_I2C5_DATA"),
  186. PINCTRL_PIN(89, "SIO_I2C5_CLK"),
  187. PINCTRL_PIN(90, "SIO_I2C6_DATA"),
  188. PINCTRL_PIN(91, "SIO_I2C6_CLK"),
  189. PINCTRL_PIN(92, "GPIO_S0_SC92"),
  190. PINCTRL_PIN(93, "GPIO_S0_SC93"),
  191. PINCTRL_PIN(94, "SIO_PWM0"),
  192. PINCTRL_PIN(95, "SIO_PWM1"),
  193. PINCTRL_PIN(96, "PMC_PLT_CLK0"),
  194. PINCTRL_PIN(97, "PMC_PLT_CLK1"),
  195. PINCTRL_PIN(98, "PMC_PLT_CLK2"),
  196. PINCTRL_PIN(99, "PMC_PLT_CLK3"),
  197. PINCTRL_PIN(100, "PMC_PLT_CLK4"),
  198. PINCTRL_PIN(101, "PMC_PLT_CLK5"),
  199. };
  200. static const unsigned int byt_score_pins_map[BYT_NGPIO_SCORE] = {
  201. 85, 89, 93, 96, 99, 102, 98, 101, 34, 37,
  202. 36, 38, 39, 35, 40, 84, 62, 61, 64, 59,
  203. 54, 56, 60, 55, 63, 57, 51, 50, 53, 47,
  204. 52, 49, 48, 43, 46, 41, 45, 42, 58, 44,
  205. 95, 105, 70, 68, 67, 66, 69, 71, 65, 72,
  206. 86, 90, 88, 92, 103, 77, 79, 83, 78, 81,
  207. 80, 82, 13, 12, 15, 14, 17, 18, 19, 16,
  208. 2, 1, 0, 4, 6, 7, 9, 8, 33, 32,
  209. 31, 30, 29, 27, 25, 28, 26, 23, 21, 20,
  210. 24, 22, 5, 3, 10, 11, 106, 87, 91, 104,
  211. 97, 100,
  212. };
  213. /* SCORE groups */
  214. static const unsigned int byt_score_uart1_pins[] = { 70, 71, 72, 73 };
  215. static const unsigned int byt_score_uart2_pins[] = { 74, 75, 76, 77 };
  216. static const unsigned int byt_score_pwm0_pins[] = { 94 };
  217. static const unsigned int byt_score_pwm1_pins[] = { 95 };
  218. static const unsigned int byt_score_sio_spi_pins[] = { 66, 67, 68, 69 };
  219. static const unsigned int byt_score_i2c5_pins[] = { 88, 89 };
  220. static const unsigned int byt_score_i2c6_pins[] = { 90, 91 };
  221. static const unsigned int byt_score_i2c4_pins[] = { 86, 87 };
  222. static const unsigned int byt_score_i2c3_pins[] = { 84, 85 };
  223. static const unsigned int byt_score_i2c2_pins[] = { 82, 83 };
  224. static const unsigned int byt_score_i2c1_pins[] = { 80, 81 };
  225. static const unsigned int byt_score_i2c0_pins[] = { 78, 79 };
  226. static const unsigned int byt_score_ssp0_pins[] = { 8, 9, 10, 11 };
  227. static const unsigned int byt_score_ssp1_pins[] = { 12, 13, 14, 15 };
  228. static const unsigned int byt_score_ssp2_pins[] = { 62, 63, 64, 65 };
  229. static const unsigned int byt_score_sdcard_pins[] = {
  230. 7, 33, 34, 35, 36, 37, 38, 39, 40, 41,
  231. };
  232. static const unsigned int byt_score_sdcard_mux_values[] = {
  233. 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  234. };
  235. static const unsigned int byt_score_sdio_pins[] = { 27, 28, 29, 30, 31, 32 };
  236. static const unsigned int byt_score_emmc_pins[] = {
  237. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  238. };
  239. static const unsigned int byt_score_ilb_lpc_pins[] = {
  240. 42, 43, 44, 45, 46, 47, 48, 49, 50,
  241. };
  242. static const unsigned int byt_score_sata_pins[] = { 0, 1, 2 };
  243. static const unsigned int byt_score_plt_clk0_pins[] = { 96 };
  244. static const unsigned int byt_score_plt_clk1_pins[] = { 97 };
  245. static const unsigned int byt_score_plt_clk2_pins[] = { 98 };
  246. static const unsigned int byt_score_plt_clk3_pins[] = { 99 };
  247. static const unsigned int byt_score_plt_clk4_pins[] = { 100 };
  248. static const unsigned int byt_score_plt_clk5_pins[] = { 101 };
  249. static const unsigned int byt_score_smbus_pins[] = { 51, 52, 53 };
  250. static const struct intel_pingroup byt_score_groups[] = {
  251. PIN_GROUP("uart1_grp", byt_score_uart1_pins, 1),
  252. PIN_GROUP("uart2_grp", byt_score_uart2_pins, 1),
  253. PIN_GROUP("pwm0_grp", byt_score_pwm0_pins, 1),
  254. PIN_GROUP("pwm1_grp", byt_score_pwm1_pins, 1),
  255. PIN_GROUP("ssp2_grp", byt_score_ssp2_pins, 1),
  256. PIN_GROUP("sio_spi_grp", byt_score_sio_spi_pins, 1),
  257. PIN_GROUP("i2c5_grp", byt_score_i2c5_pins, 1),
  258. PIN_GROUP("i2c6_grp", byt_score_i2c6_pins, 1),
  259. PIN_GROUP("i2c4_grp", byt_score_i2c4_pins, 1),
  260. PIN_GROUP("i2c3_grp", byt_score_i2c3_pins, 1),
  261. PIN_GROUP("i2c2_grp", byt_score_i2c2_pins, 1),
  262. PIN_GROUP("i2c1_grp", byt_score_i2c1_pins, 1),
  263. PIN_GROUP("i2c0_grp", byt_score_i2c0_pins, 1),
  264. PIN_GROUP("ssp0_grp", byt_score_ssp0_pins, 1),
  265. PIN_GROUP("ssp1_grp", byt_score_ssp1_pins, 1),
  266. PIN_GROUP("sdcard_grp", byt_score_sdcard_pins, byt_score_sdcard_mux_values),
  267. PIN_GROUP("sdio_grp", byt_score_sdio_pins, 1),
  268. PIN_GROUP("emmc_grp", byt_score_emmc_pins, 1),
  269. PIN_GROUP("lpc_grp", byt_score_ilb_lpc_pins, 1),
  270. PIN_GROUP("sata_grp", byt_score_sata_pins, 1),
  271. PIN_GROUP("plt_clk0_grp", byt_score_plt_clk0_pins, 1),
  272. PIN_GROUP("plt_clk1_grp", byt_score_plt_clk1_pins, 1),
  273. PIN_GROUP("plt_clk2_grp", byt_score_plt_clk2_pins, 1),
  274. PIN_GROUP("plt_clk3_grp", byt_score_plt_clk3_pins, 1),
  275. PIN_GROUP("plt_clk4_grp", byt_score_plt_clk4_pins, 1),
  276. PIN_GROUP("plt_clk5_grp", byt_score_plt_clk5_pins, 1),
  277. PIN_GROUP("smbus_grp", byt_score_smbus_pins, 1),
  278. };
  279. static const char * const byt_score_uart_groups[] = {
  280. "uart1_grp", "uart2_grp",
  281. };
  282. static const char * const byt_score_pwm_groups[] = {
  283. "pwm0_grp", "pwm1_grp",
  284. };
  285. static const char * const byt_score_ssp_groups[] = {
  286. "ssp0_grp", "ssp1_grp", "ssp2_grp",
  287. };
  288. static const char * const byt_score_spi_groups[] = { "sio_spi_grp" };
  289. static const char * const byt_score_i2c_groups[] = {
  290. "i2c0_grp", "i2c1_grp", "i2c2_grp", "i2c3_grp", "i2c4_grp", "i2c5_grp",
  291. "i2c6_grp",
  292. };
  293. static const char * const byt_score_sdcard_groups[] = { "sdcard_grp" };
  294. static const char * const byt_score_sdio_groups[] = { "sdio_grp" };
  295. static const char * const byt_score_emmc_groups[] = { "emmc_grp" };
  296. static const char * const byt_score_lpc_groups[] = { "lpc_grp" };
  297. static const char * const byt_score_sata_groups[] = { "sata_grp" };
  298. static const char * const byt_score_plt_clk_groups[] = {
  299. "plt_clk0_grp", "plt_clk1_grp", "plt_clk2_grp", "plt_clk3_grp",
  300. "plt_clk4_grp", "plt_clk5_grp",
  301. };
  302. static const char * const byt_score_smbus_groups[] = { "smbus_grp" };
  303. static const char * const byt_score_gpio_groups[] = {
  304. "uart1_grp", "uart2_grp", "pwm0_grp", "pwm1_grp", "ssp0_grp",
  305. "ssp1_grp", "ssp2_grp", "sio_spi_grp", "i2c0_grp", "i2c1_grp",
  306. "i2c2_grp", "i2c3_grp", "i2c4_grp", "i2c5_grp", "i2c6_grp",
  307. "sdcard_grp", "sdio_grp", "emmc_grp", "lpc_grp", "sata_grp",
  308. "plt_clk0_grp", "plt_clk1_grp", "plt_clk2_grp", "plt_clk3_grp",
  309. "plt_clk4_grp", "plt_clk5_grp", "smbus_grp",
  310. };
  311. static const struct intel_function byt_score_functions[] = {
  312. FUNCTION("uart", byt_score_uart_groups),
  313. FUNCTION("pwm", byt_score_pwm_groups),
  314. FUNCTION("ssp", byt_score_ssp_groups),
  315. FUNCTION("spi", byt_score_spi_groups),
  316. FUNCTION("i2c", byt_score_i2c_groups),
  317. FUNCTION("sdcard", byt_score_sdcard_groups),
  318. FUNCTION("sdio", byt_score_sdio_groups),
  319. FUNCTION("emmc", byt_score_emmc_groups),
  320. FUNCTION("lpc", byt_score_lpc_groups),
  321. FUNCTION("sata", byt_score_sata_groups),
  322. FUNCTION("plt_clk", byt_score_plt_clk_groups),
  323. FUNCTION("smbus", byt_score_smbus_groups),
  324. FUNCTION("gpio", byt_score_gpio_groups),
  325. };
  326. static const struct intel_community byt_score_communities[] = {
  327. COMMUNITY(0, BYT_NGPIO_SCORE, byt_score_pins_map),
  328. };
  329. static const struct intel_pinctrl_soc_data byt_score_soc_data = {
  330. .uid = BYT_SCORE_ACPI_UID,
  331. .pins = byt_score_pins,
  332. .npins = ARRAY_SIZE(byt_score_pins),
  333. .groups = byt_score_groups,
  334. .ngroups = ARRAY_SIZE(byt_score_groups),
  335. .functions = byt_score_functions,
  336. .nfunctions = ARRAY_SIZE(byt_score_functions),
  337. .communities = byt_score_communities,
  338. .ncommunities = ARRAY_SIZE(byt_score_communities),
  339. };
  340. /* SUS pins, aka GPIOS_<pin_no> or GPIO_S5[<pin_no>] */
  341. static const struct pinctrl_pin_desc byt_sus_pins[] = {
  342. PINCTRL_PIN(0, "GPIO_S50"),
  343. PINCTRL_PIN(1, "GPIO_S51"),
  344. PINCTRL_PIN(2, "GPIO_S52"),
  345. PINCTRL_PIN(3, "GPIO_S53"),
  346. PINCTRL_PIN(4, "GPIO_S54"),
  347. PINCTRL_PIN(5, "GPIO_S55"),
  348. PINCTRL_PIN(6, "GPIO_S56"),
  349. PINCTRL_PIN(7, "GPIO_S57"),
  350. PINCTRL_PIN(8, "GPIO_S58"),
  351. PINCTRL_PIN(9, "GPIO_S59"),
  352. PINCTRL_PIN(10, "GPIO_S510"),
  353. PINCTRL_PIN(11, "PMC_SUSPWRDNACK"),
  354. PINCTRL_PIN(12, "PMC_SUSCLK0"),
  355. PINCTRL_PIN(13, "GPIO_S513"),
  356. PINCTRL_PIN(14, "USB_ULPI_RST"),
  357. PINCTRL_PIN(15, "PMC_WAKE_PCIE0#"),
  358. PINCTRL_PIN(16, "PMC_PWRBTN"),
  359. PINCTRL_PIN(17, "GPIO_S517"),
  360. PINCTRL_PIN(18, "PMC_SUS_STAT"),
  361. PINCTRL_PIN(19, "USB_OC0"),
  362. PINCTRL_PIN(20, "USB_OC1"),
  363. PINCTRL_PIN(21, "PCU_SPI_CS1"),
  364. PINCTRL_PIN(22, "GPIO_S522"),
  365. PINCTRL_PIN(23, "GPIO_S523"),
  366. PINCTRL_PIN(24, "GPIO_S524"),
  367. PINCTRL_PIN(25, "GPIO_S525"),
  368. PINCTRL_PIN(26, "GPIO_S526"),
  369. PINCTRL_PIN(27, "GPIO_S527"),
  370. PINCTRL_PIN(28, "GPIO_S528"),
  371. PINCTRL_PIN(29, "GPIO_S529"),
  372. PINCTRL_PIN(30, "GPIO_S530"),
  373. PINCTRL_PIN(31, "USB_ULPI_CLK"),
  374. PINCTRL_PIN(32, "USB_ULPI_DATA0"),
  375. PINCTRL_PIN(33, "USB_ULPI_DATA1"),
  376. PINCTRL_PIN(34, "USB_ULPI_DATA2"),
  377. PINCTRL_PIN(35, "USB_ULPI_DATA3"),
  378. PINCTRL_PIN(36, "USB_ULPI_DATA4"),
  379. PINCTRL_PIN(37, "USB_ULPI_DATA5"),
  380. PINCTRL_PIN(38, "USB_ULPI_DATA6"),
  381. PINCTRL_PIN(39, "USB_ULPI_DATA7"),
  382. PINCTRL_PIN(40, "USB_ULPI_DIR"),
  383. PINCTRL_PIN(41, "USB_ULPI_NXT"),
  384. PINCTRL_PIN(42, "USB_ULPI_STP"),
  385. PINCTRL_PIN(43, "USB_ULPI_REFCLK"),
  386. };
  387. static const unsigned int byt_sus_pins_map[BYT_NGPIO_SUS] = {
  388. 29, 33, 30, 31, 32, 34, 36, 35, 38, 37,
  389. 18, 7, 11, 20, 17, 1, 8, 10, 19, 12,
  390. 0, 2, 23, 39, 28, 27, 22, 21, 24, 25,
  391. 26, 51, 56, 54, 49, 55, 48, 57, 50, 58,
  392. 52, 53, 59, 40,
  393. };
  394. static const unsigned int byt_sus_usb_over_current_pins[] = { 19, 20 };
  395. static const unsigned int byt_sus_usb_over_current_mode_values[] = { 0, 0 };
  396. static const unsigned int byt_sus_usb_over_current_gpio_mode_values[] = { 1, 1 };
  397. static const unsigned int byt_sus_usb_ulpi_pins[] = {
  398. 14, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
  399. };
  400. static const unsigned int byt_sus_usb_ulpi_mode_values[] = {
  401. 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  402. };
  403. static const unsigned int byt_sus_usb_ulpi_gpio_mode_values[] = {
  404. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  405. };
  406. static const unsigned int byt_sus_pcu_spi_pins[] = { 21 };
  407. static const unsigned int byt_sus_pcu_spi_mode_values[] = { 0 };
  408. static const unsigned int byt_sus_pcu_spi_gpio_mode_values[] = { 1 };
  409. static const unsigned int byt_sus_pmu_clk1_pins[] = { 5 };
  410. static const unsigned int byt_sus_pmu_clk2_pins[] = { 6 };
  411. static const struct intel_pingroup byt_sus_groups[] = {
  412. PIN_GROUP("usb_oc_grp", byt_sus_usb_over_current_pins, byt_sus_usb_over_current_mode_values),
  413. PIN_GROUP("usb_ulpi_grp", byt_sus_usb_ulpi_pins, byt_sus_usb_ulpi_mode_values),
  414. PIN_GROUP("pcu_spi_grp", byt_sus_pcu_spi_pins, byt_sus_pcu_spi_mode_values),
  415. PIN_GROUP("usb_oc_grp_gpio", byt_sus_usb_over_current_pins, byt_sus_usb_over_current_gpio_mode_values),
  416. PIN_GROUP("usb_ulpi_grp_gpio", byt_sus_usb_ulpi_pins, byt_sus_usb_ulpi_gpio_mode_values),
  417. PIN_GROUP("pcu_spi_grp_gpio", byt_sus_pcu_spi_pins, byt_sus_pcu_spi_gpio_mode_values),
  418. PIN_GROUP("pmu_clk1_grp", byt_sus_pmu_clk1_pins, 1),
  419. PIN_GROUP("pmu_clk2_grp", byt_sus_pmu_clk2_pins, 1),
  420. };
  421. static const char * const byt_sus_usb_groups[] = {
  422. "usb_oc_grp", "usb_ulpi_grp",
  423. };
  424. static const char * const byt_sus_spi_groups[] = { "pcu_spi_grp" };
  425. static const char * const byt_sus_pmu_clk_groups[] = {
  426. "pmu_clk1_grp", "pmu_clk2_grp",
  427. };
  428. static const char * const byt_sus_gpio_groups[] = {
  429. "usb_oc_grp_gpio", "usb_ulpi_grp_gpio", "pcu_spi_grp_gpio",
  430. "pmu_clk1_grp", "pmu_clk2_grp",
  431. };
  432. static const struct intel_function byt_sus_functions[] = {
  433. FUNCTION("usb", byt_sus_usb_groups),
  434. FUNCTION("spi", byt_sus_spi_groups),
  435. FUNCTION("gpio", byt_sus_gpio_groups),
  436. FUNCTION("pmu_clk", byt_sus_pmu_clk_groups),
  437. };
  438. static const struct intel_community byt_sus_communities[] = {
  439. COMMUNITY(0, BYT_NGPIO_SUS, byt_sus_pins_map),
  440. };
  441. static const struct intel_pinctrl_soc_data byt_sus_soc_data = {
  442. .uid = BYT_SUS_ACPI_UID,
  443. .pins = byt_sus_pins,
  444. .npins = ARRAY_SIZE(byt_sus_pins),
  445. .groups = byt_sus_groups,
  446. .ngroups = ARRAY_SIZE(byt_sus_groups),
  447. .functions = byt_sus_functions,
  448. .nfunctions = ARRAY_SIZE(byt_sus_functions),
  449. .communities = byt_sus_communities,
  450. .ncommunities = ARRAY_SIZE(byt_sus_communities),
  451. };
  452. static const struct pinctrl_pin_desc byt_ncore_pins[] = {
  453. PINCTRL_PIN(0, "HV_DDI0_HPD"),
  454. PINCTRL_PIN(1, "HV_DDI0_DDC_SDA"),
  455. PINCTRL_PIN(2, "HV_DDI0_DDC_SCL"),
  456. PINCTRL_PIN(3, "PANEL0_VDDEN"),
  457. PINCTRL_PIN(4, "PANEL0_BKLTEN"),
  458. PINCTRL_PIN(5, "PANEL0_BKLTCTL"),
  459. PINCTRL_PIN(6, "HV_DDI1_HPD"),
  460. PINCTRL_PIN(7, "HV_DDI1_DDC_SDA"),
  461. PINCTRL_PIN(8, "HV_DDI1_DDC_SCL"),
  462. PINCTRL_PIN(9, "PANEL1_VDDEN"),
  463. PINCTRL_PIN(10, "PANEL1_BKLTEN"),
  464. PINCTRL_PIN(11, "PANEL1_BKLTCTL"),
  465. PINCTRL_PIN(12, "GP_INTD_DSI_TE1"),
  466. PINCTRL_PIN(13, "HV_DDI2_DDC_SDA"),
  467. PINCTRL_PIN(14, "HV_DDI2_DDC_SCL"),
  468. PINCTRL_PIN(15, "GP_CAMERASB00"),
  469. PINCTRL_PIN(16, "GP_CAMERASB01"),
  470. PINCTRL_PIN(17, "GP_CAMERASB02"),
  471. PINCTRL_PIN(18, "GP_CAMERASB03"),
  472. PINCTRL_PIN(19, "GP_CAMERASB04"),
  473. PINCTRL_PIN(20, "GP_CAMERASB05"),
  474. PINCTRL_PIN(21, "GP_CAMERASB06"),
  475. PINCTRL_PIN(22, "GP_CAMERASB07"),
  476. PINCTRL_PIN(23, "GP_CAMERASB08"),
  477. PINCTRL_PIN(24, "GP_CAMERASB09"),
  478. PINCTRL_PIN(25, "GP_CAMERASB10"),
  479. PINCTRL_PIN(26, "GP_CAMERASB11"),
  480. PINCTRL_PIN(27, "GP_INTD_DSI_TE2"),
  481. };
  482. static const unsigned int byt_ncore_pins_map[BYT_NGPIO_NCORE] = {
  483. 19, 18, 17, 20, 21, 22, 24, 25, 23, 16,
  484. 14, 15, 12, 26, 27, 1, 4, 8, 11, 0,
  485. 3, 6, 10, 13, 2, 5, 9, 7,
  486. };
  487. static const struct intel_community byt_ncore_communities[] = {
  488. COMMUNITY(0, BYT_NGPIO_NCORE, byt_ncore_pins_map),
  489. };
  490. static const struct intel_pinctrl_soc_data byt_ncore_soc_data = {
  491. .uid = BYT_NCORE_ACPI_UID,
  492. .pins = byt_ncore_pins,
  493. .npins = ARRAY_SIZE(byt_ncore_pins),
  494. .communities = byt_ncore_communities,
  495. .ncommunities = ARRAY_SIZE(byt_ncore_communities),
  496. };
  497. static const struct intel_pinctrl_soc_data *byt_soc_data[] = {
  498. &byt_score_soc_data,
  499. &byt_sus_soc_data,
  500. &byt_ncore_soc_data,
  501. NULL
  502. };
  503. static DEFINE_RAW_SPINLOCK(byt_lock);
  504. static struct intel_community *byt_get_community(struct intel_pinctrl *vg,
  505. unsigned int pin)
  506. {
  507. struct intel_community *comm;
  508. int i;
  509. for (i = 0; i < vg->ncommunities; i++) {
  510. comm = vg->communities + i;
  511. if (pin < comm->pin_base + comm->npins && pin >= comm->pin_base)
  512. return comm;
  513. }
  514. return NULL;
  515. }
  516. static void __iomem *byt_gpio_reg(struct intel_pinctrl *vg, unsigned int offset,
  517. int reg)
  518. {
  519. struct intel_community *comm = byt_get_community(vg, offset);
  520. u32 reg_offset;
  521. if (!comm)
  522. return NULL;
  523. offset -= comm->pin_base;
  524. switch (reg) {
  525. case BYT_INT_STAT_REG:
  526. reg_offset = (offset / 32) * 4;
  527. break;
  528. case BYT_DEBOUNCE_REG:
  529. reg_offset = 0;
  530. break;
  531. default:
  532. reg_offset = comm->pad_map[offset] * 16;
  533. break;
  534. }
  535. return comm->pad_regs + reg_offset + reg;
  536. }
  537. static int byt_get_groups_count(struct pinctrl_dev *pctldev)
  538. {
  539. struct intel_pinctrl *vg = pinctrl_dev_get_drvdata(pctldev);
  540. return vg->soc->ngroups;
  541. }
  542. static const char *byt_get_group_name(struct pinctrl_dev *pctldev,
  543. unsigned int selector)
  544. {
  545. struct intel_pinctrl *vg = pinctrl_dev_get_drvdata(pctldev);
  546. return vg->soc->groups[selector].grp.name;
  547. }
  548. static int byt_get_group_pins(struct pinctrl_dev *pctldev,
  549. unsigned int selector,
  550. const unsigned int **pins,
  551. unsigned int *num_pins)
  552. {
  553. struct intel_pinctrl *vg = pinctrl_dev_get_drvdata(pctldev);
  554. *pins = vg->soc->groups[selector].grp.pins;
  555. *num_pins = vg->soc->groups[selector].grp.npins;
  556. return 0;
  557. }
  558. static const struct pinctrl_ops byt_pinctrl_ops = {
  559. .get_groups_count = byt_get_groups_count,
  560. .get_group_name = byt_get_group_name,
  561. .get_group_pins = byt_get_group_pins,
  562. };
  563. static int byt_get_functions_count(struct pinctrl_dev *pctldev)
  564. {
  565. struct intel_pinctrl *vg = pinctrl_dev_get_drvdata(pctldev);
  566. return vg->soc->nfunctions;
  567. }
  568. static const char *byt_get_function_name(struct pinctrl_dev *pctldev,
  569. unsigned int selector)
  570. {
  571. struct intel_pinctrl *vg = pinctrl_dev_get_drvdata(pctldev);
  572. return vg->soc->functions[selector].name;
  573. }
  574. static int byt_get_function_groups(struct pinctrl_dev *pctldev,
  575. unsigned int selector,
  576. const char * const **groups,
  577. unsigned int *num_groups)
  578. {
  579. struct intel_pinctrl *vg = pinctrl_dev_get_drvdata(pctldev);
  580. *groups = vg->soc->functions[selector].groups;
  581. *num_groups = vg->soc->functions[selector].ngroups;
  582. return 0;
  583. }
  584. static void byt_set_group_simple_mux(struct intel_pinctrl *vg,
  585. const struct intel_pingroup group,
  586. unsigned int func)
  587. {
  588. unsigned long flags;
  589. int i;
  590. raw_spin_lock_irqsave(&byt_lock, flags);
  591. for (i = 0; i < group.grp.npins; i++) {
  592. void __iomem *padcfg0;
  593. u32 value;
  594. padcfg0 = byt_gpio_reg(vg, group.grp.pins[i], BYT_CONF0_REG);
  595. if (!padcfg0) {
  596. dev_warn(vg->dev,
  597. "Group %s, pin %i not muxed (no padcfg0)\n",
  598. group.grp.name, i);
  599. continue;
  600. }
  601. value = readl(padcfg0);
  602. value &= ~BYT_PIN_MUX;
  603. value |= func;
  604. writel(value, padcfg0);
  605. }
  606. raw_spin_unlock_irqrestore(&byt_lock, flags);
  607. }
  608. static void byt_set_group_mixed_mux(struct intel_pinctrl *vg,
  609. const struct intel_pingroup group,
  610. const unsigned int *func)
  611. {
  612. unsigned long flags;
  613. int i;
  614. raw_spin_lock_irqsave(&byt_lock, flags);
  615. for (i = 0; i < group.grp.npins; i++) {
  616. void __iomem *padcfg0;
  617. u32 value;
  618. padcfg0 = byt_gpio_reg(vg, group.grp.pins[i], BYT_CONF0_REG);
  619. if (!padcfg0) {
  620. dev_warn(vg->dev,
  621. "Group %s, pin %i not muxed (no padcfg0)\n",
  622. group.grp.name, i);
  623. continue;
  624. }
  625. value = readl(padcfg0);
  626. value &= ~BYT_PIN_MUX;
  627. value |= func[i];
  628. writel(value, padcfg0);
  629. }
  630. raw_spin_unlock_irqrestore(&byt_lock, flags);
  631. }
  632. static int byt_set_mux(struct pinctrl_dev *pctldev, unsigned int func_selector,
  633. unsigned int group_selector)
  634. {
  635. struct intel_pinctrl *vg = pinctrl_dev_get_drvdata(pctldev);
  636. const struct intel_function func = vg->soc->functions[func_selector];
  637. const struct intel_pingroup group = vg->soc->groups[group_selector];
  638. if (group.modes)
  639. byt_set_group_mixed_mux(vg, group, group.modes);
  640. else if (!strcmp(func.name, "gpio"))
  641. byt_set_group_simple_mux(vg, group, BYT_DEFAULT_GPIO_MUX);
  642. else
  643. byt_set_group_simple_mux(vg, group, group.mode);
  644. return 0;
  645. }
  646. static u32 byt_get_gpio_mux(struct intel_pinctrl *vg, unsigned int offset)
  647. {
  648. /* SCORE pin 92-93 */
  649. if (!strcmp(vg->soc->uid, BYT_SCORE_ACPI_UID) &&
  650. offset >= 92 && offset <= 93)
  651. return BYT_ALTER_GPIO_MUX;
  652. /* SUS pin 11-21 */
  653. if (!strcmp(vg->soc->uid, BYT_SUS_ACPI_UID) &&
  654. offset >= 11 && offset <= 21)
  655. return BYT_ALTER_GPIO_MUX;
  656. return BYT_DEFAULT_GPIO_MUX;
  657. }
  658. static void byt_gpio_clear_triggering(struct intel_pinctrl *vg, unsigned int offset)
  659. {
  660. void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
  661. unsigned long flags;
  662. u32 value;
  663. raw_spin_lock_irqsave(&byt_lock, flags);
  664. value = readl(reg);
  665. /* Do not clear direct-irq enabled IRQs (from gpio_disable_free) */
  666. if (value & BYT_DIRECT_IRQ_EN)
  667. /* nothing to do */ ;
  668. else
  669. value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
  670. writel(value, reg);
  671. raw_spin_unlock_irqrestore(&byt_lock, flags);
  672. }
  673. static int byt_gpio_request_enable(struct pinctrl_dev *pctl_dev,
  674. struct pinctrl_gpio_range *range,
  675. unsigned int offset)
  676. {
  677. struct intel_pinctrl *vg = pinctrl_dev_get_drvdata(pctl_dev);
  678. void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
  679. u32 value, gpio_mux;
  680. unsigned long flags;
  681. raw_spin_lock_irqsave(&byt_lock, flags);
  682. /*
  683. * In most cases, func pin mux 000 means GPIO function.
  684. * But, some pins may have func pin mux 001 represents
  685. * GPIO function.
  686. *
  687. * Because there are devices out there where some pins were not
  688. * configured correctly we allow changing the mux value from
  689. * request (but print out warning about that).
  690. */
  691. value = readl(reg) & BYT_PIN_MUX;
  692. gpio_mux = byt_get_gpio_mux(vg, offset);
  693. if (gpio_mux != value) {
  694. value = readl(reg) & ~BYT_PIN_MUX;
  695. value |= gpio_mux;
  696. writel(value, reg);
  697. dev_warn(vg->dev, FW_BUG "pin %u forcibly re-configured as GPIO\n", offset);
  698. }
  699. raw_spin_unlock_irqrestore(&byt_lock, flags);
  700. pm_runtime_get(vg->dev);
  701. return 0;
  702. }
  703. static void byt_gpio_disable_free(struct pinctrl_dev *pctl_dev,
  704. struct pinctrl_gpio_range *range,
  705. unsigned int offset)
  706. {
  707. struct intel_pinctrl *vg = pinctrl_dev_get_drvdata(pctl_dev);
  708. byt_gpio_clear_triggering(vg, offset);
  709. pm_runtime_put(vg->dev);
  710. }
  711. static void byt_gpio_direct_irq_check(struct intel_pinctrl *vg,
  712. unsigned int offset)
  713. {
  714. void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
  715. /*
  716. * Before making any direction modifications, do a check if gpio is set
  717. * for direct IRQ. On Bay Trail, setting GPIO to output does not make
  718. * sense, so let's at least inform the caller before they shoot
  719. * themselves in the foot.
  720. */
  721. if (readl(conf_reg) & BYT_DIRECT_IRQ_EN)
  722. dev_info_once(vg->dev, "Potential Error: Setting GPIO with direct_irq_en to output");
  723. }
  724. static int byt_gpio_set_direction(struct pinctrl_dev *pctl_dev,
  725. struct pinctrl_gpio_range *range,
  726. unsigned int offset,
  727. bool input)
  728. {
  729. struct intel_pinctrl *vg = pinctrl_dev_get_drvdata(pctl_dev);
  730. void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  731. unsigned long flags;
  732. u32 value;
  733. raw_spin_lock_irqsave(&byt_lock, flags);
  734. value = readl(val_reg);
  735. value &= ~BYT_DIR_MASK;
  736. if (input)
  737. value |= BYT_OUTPUT_EN;
  738. else
  739. byt_gpio_direct_irq_check(vg, offset);
  740. writel(value, val_reg);
  741. raw_spin_unlock_irqrestore(&byt_lock, flags);
  742. return 0;
  743. }
  744. static const struct pinmux_ops byt_pinmux_ops = {
  745. .get_functions_count = byt_get_functions_count,
  746. .get_function_name = byt_get_function_name,
  747. .get_function_groups = byt_get_function_groups,
  748. .set_mux = byt_set_mux,
  749. .gpio_request_enable = byt_gpio_request_enable,
  750. .gpio_disable_free = byt_gpio_disable_free,
  751. .gpio_set_direction = byt_gpio_set_direction,
  752. };
  753. static void byt_get_pull_strength(u32 reg, u16 *strength)
  754. {
  755. switch (reg & BYT_PULL_STR_MASK) {
  756. case BYT_PULL_STR_2K:
  757. *strength = 2000;
  758. break;
  759. case BYT_PULL_STR_10K:
  760. *strength = 10000;
  761. break;
  762. case BYT_PULL_STR_20K:
  763. *strength = 20000;
  764. break;
  765. case BYT_PULL_STR_40K:
  766. *strength = 40000;
  767. break;
  768. }
  769. }
  770. static int byt_set_pull_strength(u32 *reg, u16 strength)
  771. {
  772. *reg &= ~BYT_PULL_STR_MASK;
  773. switch (strength) {
  774. case 2000:
  775. *reg |= BYT_PULL_STR_2K;
  776. break;
  777. case 10000:
  778. *reg |= BYT_PULL_STR_10K;
  779. break;
  780. case 20000:
  781. *reg |= BYT_PULL_STR_20K;
  782. break;
  783. case 40000:
  784. *reg |= BYT_PULL_STR_40K;
  785. break;
  786. default:
  787. return -EINVAL;
  788. }
  789. return 0;
  790. }
  791. static int byt_pin_config_get(struct pinctrl_dev *pctl_dev, unsigned int offset,
  792. unsigned long *config)
  793. {
  794. struct intel_pinctrl *vg = pinctrl_dev_get_drvdata(pctl_dev);
  795. enum pin_config_param param = pinconf_to_config_param(*config);
  796. void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
  797. void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  798. void __iomem *db_reg = byt_gpio_reg(vg, offset, BYT_DEBOUNCE_REG);
  799. unsigned long flags;
  800. u32 conf, pull, val, debounce;
  801. u16 arg = 0;
  802. raw_spin_lock_irqsave(&byt_lock, flags);
  803. conf = readl(conf_reg);
  804. pull = conf & BYT_PULL_ASSIGN_MASK;
  805. val = readl(val_reg);
  806. raw_spin_unlock_irqrestore(&byt_lock, flags);
  807. switch (param) {
  808. case PIN_CONFIG_BIAS_DISABLE:
  809. if (pull)
  810. return -EINVAL;
  811. break;
  812. case PIN_CONFIG_BIAS_PULL_DOWN:
  813. /* Pull assignment is only applicable in input mode */
  814. if ((val & BYT_INPUT_EN) || pull != BYT_PULL_ASSIGN_DOWN)
  815. return -EINVAL;
  816. byt_get_pull_strength(conf, &arg);
  817. break;
  818. case PIN_CONFIG_BIAS_PULL_UP:
  819. /* Pull assignment is only applicable in input mode */
  820. if ((val & BYT_INPUT_EN) || pull != BYT_PULL_ASSIGN_UP)
  821. return -EINVAL;
  822. byt_get_pull_strength(conf, &arg);
  823. break;
  824. case PIN_CONFIG_INPUT_DEBOUNCE:
  825. if (!(conf & BYT_DEBOUNCE_EN))
  826. return -EINVAL;
  827. raw_spin_lock_irqsave(&byt_lock, flags);
  828. debounce = readl(db_reg);
  829. raw_spin_unlock_irqrestore(&byt_lock, flags);
  830. switch (debounce & BYT_DEBOUNCE_PULSE_MASK) {
  831. case BYT_DEBOUNCE_PULSE_375US:
  832. arg = 375;
  833. break;
  834. case BYT_DEBOUNCE_PULSE_750US:
  835. arg = 750;
  836. break;
  837. case BYT_DEBOUNCE_PULSE_1500US:
  838. arg = 1500;
  839. break;
  840. case BYT_DEBOUNCE_PULSE_3MS:
  841. arg = 3000;
  842. break;
  843. case BYT_DEBOUNCE_PULSE_6MS:
  844. arg = 6000;
  845. break;
  846. case BYT_DEBOUNCE_PULSE_12MS:
  847. arg = 12000;
  848. break;
  849. case BYT_DEBOUNCE_PULSE_24MS:
  850. arg = 24000;
  851. break;
  852. default:
  853. return -EINVAL;
  854. }
  855. break;
  856. default:
  857. return -ENOTSUPP;
  858. }
  859. *config = pinconf_to_config_packed(param, arg);
  860. return 0;
  861. }
  862. static int byt_pin_config_set(struct pinctrl_dev *pctl_dev,
  863. unsigned int offset,
  864. unsigned long *configs,
  865. unsigned int num_configs)
  866. {
  867. struct intel_pinctrl *vg = pinctrl_dev_get_drvdata(pctl_dev);
  868. unsigned int param, arg;
  869. void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
  870. void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  871. void __iomem *db_reg = byt_gpio_reg(vg, offset, BYT_DEBOUNCE_REG);
  872. unsigned long flags;
  873. u32 conf, val, debounce;
  874. int i, ret = 0;
  875. raw_spin_lock_irqsave(&byt_lock, flags);
  876. conf = readl(conf_reg);
  877. val = readl(val_reg);
  878. for (i = 0; i < num_configs; i++) {
  879. param = pinconf_to_config_param(configs[i]);
  880. arg = pinconf_to_config_argument(configs[i]);
  881. switch (param) {
  882. case PIN_CONFIG_BIAS_DISABLE:
  883. conf &= ~BYT_PULL_ASSIGN_MASK;
  884. break;
  885. case PIN_CONFIG_BIAS_PULL_DOWN:
  886. /* Set default strength value in case none is given */
  887. if (arg == 1)
  888. arg = 2000;
  889. /*
  890. * Pull assignment is only applicable in input mode. If
  891. * chip is not in input mode, set it and warn about it.
  892. */
  893. if (val & BYT_INPUT_EN) {
  894. val &= ~BYT_INPUT_EN;
  895. writel(val, val_reg);
  896. dev_warn(vg->dev,
  897. "pin %u forcibly set to input mode\n",
  898. offset);
  899. }
  900. conf &= ~BYT_PULL_ASSIGN_MASK;
  901. conf |= BYT_PULL_ASSIGN_DOWN;
  902. ret = byt_set_pull_strength(&conf, arg);
  903. break;
  904. case PIN_CONFIG_BIAS_PULL_UP:
  905. /* Set default strength value in case none is given */
  906. if (arg == 1)
  907. arg = 2000;
  908. /*
  909. * Pull assignment is only applicable in input mode. If
  910. * chip is not in input mode, set it and warn about it.
  911. */
  912. if (val & BYT_INPUT_EN) {
  913. val &= ~BYT_INPUT_EN;
  914. writel(val, val_reg);
  915. dev_warn(vg->dev,
  916. "pin %u forcibly set to input mode\n",
  917. offset);
  918. }
  919. conf &= ~BYT_PULL_ASSIGN_MASK;
  920. conf |= BYT_PULL_ASSIGN_UP;
  921. ret = byt_set_pull_strength(&conf, arg);
  922. break;
  923. case PIN_CONFIG_INPUT_DEBOUNCE:
  924. debounce = readl(db_reg);
  925. if (arg)
  926. conf |= BYT_DEBOUNCE_EN;
  927. else
  928. conf &= ~BYT_DEBOUNCE_EN;
  929. switch (arg) {
  930. case 375:
  931. debounce &= ~BYT_DEBOUNCE_PULSE_MASK;
  932. debounce |= BYT_DEBOUNCE_PULSE_375US;
  933. break;
  934. case 750:
  935. debounce &= ~BYT_DEBOUNCE_PULSE_MASK;
  936. debounce |= BYT_DEBOUNCE_PULSE_750US;
  937. break;
  938. case 1500:
  939. debounce &= ~BYT_DEBOUNCE_PULSE_MASK;
  940. debounce |= BYT_DEBOUNCE_PULSE_1500US;
  941. break;
  942. case 3000:
  943. debounce &= ~BYT_DEBOUNCE_PULSE_MASK;
  944. debounce |= BYT_DEBOUNCE_PULSE_3MS;
  945. break;
  946. case 6000:
  947. debounce &= ~BYT_DEBOUNCE_PULSE_MASK;
  948. debounce |= BYT_DEBOUNCE_PULSE_6MS;
  949. break;
  950. case 12000:
  951. debounce &= ~BYT_DEBOUNCE_PULSE_MASK;
  952. debounce |= BYT_DEBOUNCE_PULSE_12MS;
  953. break;
  954. case 24000:
  955. debounce &= ~BYT_DEBOUNCE_PULSE_MASK;
  956. debounce |= BYT_DEBOUNCE_PULSE_24MS;
  957. break;
  958. default:
  959. if (arg)
  960. ret = -EINVAL;
  961. break;
  962. }
  963. if (!ret)
  964. writel(debounce, db_reg);
  965. break;
  966. default:
  967. ret = -ENOTSUPP;
  968. }
  969. if (ret)
  970. break;
  971. }
  972. if (!ret)
  973. writel(conf, conf_reg);
  974. raw_spin_unlock_irqrestore(&byt_lock, flags);
  975. return ret;
  976. }
  977. static const struct pinconf_ops byt_pinconf_ops = {
  978. .is_generic = true,
  979. .pin_config_get = byt_pin_config_get,
  980. .pin_config_set = byt_pin_config_set,
  981. };
  982. static const struct pinctrl_desc byt_pinctrl_desc = {
  983. .pctlops = &byt_pinctrl_ops,
  984. .pmxops = &byt_pinmux_ops,
  985. .confops = &byt_pinconf_ops,
  986. .owner = THIS_MODULE,
  987. };
  988. static int byt_gpio_get(struct gpio_chip *chip, unsigned int offset)
  989. {
  990. struct intel_pinctrl *vg = gpiochip_get_data(chip);
  991. void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  992. unsigned long flags;
  993. u32 val;
  994. raw_spin_lock_irqsave(&byt_lock, flags);
  995. val = readl(reg);
  996. raw_spin_unlock_irqrestore(&byt_lock, flags);
  997. return !!(val & BYT_LEVEL);
  998. }
  999. static void byt_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
  1000. {
  1001. struct intel_pinctrl *vg = gpiochip_get_data(chip);
  1002. void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  1003. unsigned long flags;
  1004. u32 old_val;
  1005. if (!reg)
  1006. return;
  1007. raw_spin_lock_irqsave(&byt_lock, flags);
  1008. old_val = readl(reg);
  1009. if (value)
  1010. writel(old_val | BYT_LEVEL, reg);
  1011. else
  1012. writel(old_val & ~BYT_LEVEL, reg);
  1013. raw_spin_unlock_irqrestore(&byt_lock, flags);
  1014. }
  1015. static int byt_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  1016. {
  1017. struct intel_pinctrl *vg = gpiochip_get_data(chip);
  1018. void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  1019. unsigned long flags;
  1020. u32 value;
  1021. if (!reg)
  1022. return -EINVAL;
  1023. raw_spin_lock_irqsave(&byt_lock, flags);
  1024. value = readl(reg);
  1025. raw_spin_unlock_irqrestore(&byt_lock, flags);
  1026. if (!(value & BYT_OUTPUT_EN))
  1027. return GPIO_LINE_DIRECTION_OUT;
  1028. if (!(value & BYT_INPUT_EN))
  1029. return GPIO_LINE_DIRECTION_IN;
  1030. return -EINVAL;
  1031. }
  1032. static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
  1033. {
  1034. struct intel_pinctrl *vg = gpiochip_get_data(chip);
  1035. void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  1036. unsigned long flags;
  1037. u32 reg;
  1038. raw_spin_lock_irqsave(&byt_lock, flags);
  1039. reg = readl(val_reg);
  1040. reg &= ~BYT_DIR_MASK;
  1041. reg |= BYT_OUTPUT_EN;
  1042. writel(reg, val_reg);
  1043. raw_spin_unlock_irqrestore(&byt_lock, flags);
  1044. return 0;
  1045. }
  1046. /*
  1047. * Note despite the temptation this MUST NOT be converted into a call to
  1048. * pinctrl_gpio_direction_output() + byt_gpio_set() that does not work this
  1049. * MUST be done as a single BYT_VAL_REG register write.
  1050. * See the commit message of the commit adding this comment for details.
  1051. */
  1052. static int byt_gpio_direction_output(struct gpio_chip *chip,
  1053. unsigned int offset, int value)
  1054. {
  1055. struct intel_pinctrl *vg = gpiochip_get_data(chip);
  1056. void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  1057. unsigned long flags;
  1058. u32 reg;
  1059. raw_spin_lock_irqsave(&byt_lock, flags);
  1060. byt_gpio_direct_irq_check(vg, offset);
  1061. reg = readl(val_reg);
  1062. reg &= ~BYT_DIR_MASK;
  1063. if (value)
  1064. reg |= BYT_LEVEL;
  1065. else
  1066. reg &= ~BYT_LEVEL;
  1067. writel(reg, val_reg);
  1068. raw_spin_unlock_irqrestore(&byt_lock, flags);
  1069. return 0;
  1070. }
  1071. static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1072. {
  1073. struct intel_pinctrl *vg = gpiochip_get_data(chip);
  1074. int i;
  1075. u32 conf0, val;
  1076. for (i = 0; i < vg->soc->npins; i++) {
  1077. const struct intel_community *comm;
  1078. const char *pull_str = NULL;
  1079. const char *pull = NULL;
  1080. void __iomem *reg;
  1081. unsigned long flags;
  1082. const char *label;
  1083. unsigned int pin;
  1084. raw_spin_lock_irqsave(&byt_lock, flags);
  1085. pin = vg->soc->pins[i].number;
  1086. reg = byt_gpio_reg(vg, pin, BYT_CONF0_REG);
  1087. if (!reg) {
  1088. seq_printf(s,
  1089. "Could not retrieve pin %i conf0 reg\n",
  1090. pin);
  1091. raw_spin_unlock_irqrestore(&byt_lock, flags);
  1092. continue;
  1093. }
  1094. conf0 = readl(reg);
  1095. reg = byt_gpio_reg(vg, pin, BYT_VAL_REG);
  1096. if (!reg) {
  1097. seq_printf(s,
  1098. "Could not retrieve pin %i val reg\n", pin);
  1099. raw_spin_unlock_irqrestore(&byt_lock, flags);
  1100. continue;
  1101. }
  1102. val = readl(reg);
  1103. raw_spin_unlock_irqrestore(&byt_lock, flags);
  1104. comm = byt_get_community(vg, pin);
  1105. if (!comm) {
  1106. seq_printf(s,
  1107. "Could not get community for pin %i\n", pin);
  1108. continue;
  1109. }
  1110. label = gpiochip_is_requested(chip, i);
  1111. if (!label)
  1112. label = "Unrequested";
  1113. switch (conf0 & BYT_PULL_ASSIGN_MASK) {
  1114. case BYT_PULL_ASSIGN_UP:
  1115. pull = "up";
  1116. break;
  1117. case BYT_PULL_ASSIGN_DOWN:
  1118. pull = "down";
  1119. break;
  1120. }
  1121. switch (conf0 & BYT_PULL_STR_MASK) {
  1122. case BYT_PULL_STR_2K:
  1123. pull_str = "2k";
  1124. break;
  1125. case BYT_PULL_STR_10K:
  1126. pull_str = "10k";
  1127. break;
  1128. case BYT_PULL_STR_20K:
  1129. pull_str = "20k";
  1130. break;
  1131. case BYT_PULL_STR_40K:
  1132. pull_str = "40k";
  1133. break;
  1134. }
  1135. seq_printf(s,
  1136. " gpio-%-3d (%-20.20s) %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s",
  1137. pin,
  1138. label,
  1139. val & BYT_INPUT_EN ? " " : "in",
  1140. val & BYT_OUTPUT_EN ? " " : "out",
  1141. val & BYT_LEVEL ? "hi" : "lo",
  1142. comm->pad_map[i], comm->pad_map[i] * 16,
  1143. conf0 & 0x7,
  1144. conf0 & BYT_TRIG_NEG ? " fall" : " ",
  1145. conf0 & BYT_TRIG_POS ? " rise" : " ",
  1146. conf0 & BYT_TRIG_LVL ? " level" : " ");
  1147. if (pull && pull_str)
  1148. seq_printf(s, " %-4s %-3s", pull, pull_str);
  1149. else
  1150. seq_puts(s, " ");
  1151. if (conf0 & BYT_IODEN)
  1152. seq_puts(s, " open-drain");
  1153. seq_puts(s, "\n");
  1154. }
  1155. }
  1156. static const struct gpio_chip byt_gpio_chip = {
  1157. .owner = THIS_MODULE,
  1158. .request = gpiochip_generic_request,
  1159. .free = gpiochip_generic_free,
  1160. .get_direction = byt_gpio_get_direction,
  1161. .direction_input = byt_gpio_direction_input,
  1162. .direction_output = byt_gpio_direction_output,
  1163. .get = byt_gpio_get,
  1164. .set = byt_gpio_set,
  1165. .set_config = gpiochip_generic_config,
  1166. .dbg_show = byt_gpio_dbg_show,
  1167. };
  1168. static void byt_irq_ack(struct irq_data *d)
  1169. {
  1170. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1171. struct intel_pinctrl *vg = gpiochip_get_data(gc);
  1172. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  1173. void __iomem *reg;
  1174. reg = byt_gpio_reg(vg, hwirq, BYT_INT_STAT_REG);
  1175. if (!reg)
  1176. return;
  1177. raw_spin_lock(&byt_lock);
  1178. writel(BIT(hwirq % 32), reg);
  1179. raw_spin_unlock(&byt_lock);
  1180. }
  1181. static void byt_irq_mask(struct irq_data *d)
  1182. {
  1183. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1184. struct intel_pinctrl *vg = gpiochip_get_data(gc);
  1185. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  1186. byt_gpio_clear_triggering(vg, hwirq);
  1187. gpiochip_disable_irq(gc, hwirq);
  1188. }
  1189. static void byt_irq_unmask(struct irq_data *d)
  1190. {
  1191. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1192. struct intel_pinctrl *vg = gpiochip_get_data(gc);
  1193. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  1194. unsigned long flags;
  1195. void __iomem *reg;
  1196. u32 value;
  1197. gpiochip_enable_irq(gc, hwirq);
  1198. reg = byt_gpio_reg(vg, hwirq, BYT_CONF0_REG);
  1199. if (!reg)
  1200. return;
  1201. raw_spin_lock_irqsave(&byt_lock, flags);
  1202. value = readl(reg);
  1203. switch (irqd_get_trigger_type(d)) {
  1204. case IRQ_TYPE_LEVEL_HIGH:
  1205. value |= BYT_TRIG_LVL;
  1206. fallthrough;
  1207. case IRQ_TYPE_EDGE_RISING:
  1208. value |= BYT_TRIG_POS;
  1209. break;
  1210. case IRQ_TYPE_LEVEL_LOW:
  1211. value |= BYT_TRIG_LVL;
  1212. fallthrough;
  1213. case IRQ_TYPE_EDGE_FALLING:
  1214. value |= BYT_TRIG_NEG;
  1215. break;
  1216. case IRQ_TYPE_EDGE_BOTH:
  1217. value |= (BYT_TRIG_NEG | BYT_TRIG_POS);
  1218. break;
  1219. }
  1220. writel(value, reg);
  1221. raw_spin_unlock_irqrestore(&byt_lock, flags);
  1222. }
  1223. static int byt_irq_type(struct irq_data *d, unsigned int type)
  1224. {
  1225. struct intel_pinctrl *vg = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  1226. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  1227. u32 value;
  1228. unsigned long flags;
  1229. void __iomem *reg;
  1230. reg = byt_gpio_reg(vg, hwirq, BYT_CONF0_REG);
  1231. if (!reg)
  1232. return -EINVAL;
  1233. raw_spin_lock_irqsave(&byt_lock, flags);
  1234. value = readl(reg);
  1235. WARN(value & BYT_DIRECT_IRQ_EN,
  1236. "Bad pad config for io mode, force direct_irq_en bit clearing");
  1237. /* For level trigges the BYT_TRIG_POS and BYT_TRIG_NEG bits
  1238. * are used to indicate high and low level triggering
  1239. */
  1240. value &= ~(BYT_DIRECT_IRQ_EN | BYT_TRIG_POS | BYT_TRIG_NEG |
  1241. BYT_TRIG_LVL);
  1242. /* Enable glitch filtering */
  1243. value |= BYT_GLITCH_FILTER_EN | BYT_GLITCH_F_SLOW_CLK |
  1244. BYT_GLITCH_F_FAST_CLK;
  1245. writel(value, reg);
  1246. if (type & IRQ_TYPE_EDGE_BOTH)
  1247. irq_set_handler_locked(d, handle_edge_irq);
  1248. else if (type & IRQ_TYPE_LEVEL_MASK)
  1249. irq_set_handler_locked(d, handle_level_irq);
  1250. raw_spin_unlock_irqrestore(&byt_lock, flags);
  1251. return 0;
  1252. }
  1253. static const struct irq_chip byt_gpio_irq_chip = {
  1254. .name = "BYT-GPIO",
  1255. .irq_ack = byt_irq_ack,
  1256. .irq_mask = byt_irq_mask,
  1257. .irq_unmask = byt_irq_unmask,
  1258. .irq_set_type = byt_irq_type,
  1259. .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_SET_TYPE_MASKED | IRQCHIP_IMMUTABLE,
  1260. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  1261. };
  1262. static void byt_gpio_irq_handler(struct irq_desc *desc)
  1263. {
  1264. struct irq_data *data = irq_desc_get_irq_data(desc);
  1265. struct intel_pinctrl *vg = gpiochip_get_data(irq_desc_get_handler_data(desc));
  1266. struct irq_chip *chip = irq_data_get_irq_chip(data);
  1267. u32 base, pin;
  1268. void __iomem *reg;
  1269. unsigned long pending;
  1270. /* check from GPIO controller which pin triggered the interrupt */
  1271. for (base = 0; base < vg->chip.ngpio; base += 32) {
  1272. reg = byt_gpio_reg(vg, base, BYT_INT_STAT_REG);
  1273. if (!reg) {
  1274. dev_warn(vg->dev,
  1275. "Pin %i: could not retrieve interrupt status register\n",
  1276. base);
  1277. continue;
  1278. }
  1279. raw_spin_lock(&byt_lock);
  1280. pending = readl(reg);
  1281. raw_spin_unlock(&byt_lock);
  1282. for_each_set_bit(pin, &pending, 32)
  1283. generic_handle_domain_irq(vg->chip.irq.domain, base + pin);
  1284. }
  1285. chip->irq_eoi(data);
  1286. }
  1287. static bool byt_direct_irq_sanity_check(struct intel_pinctrl *vg, int pin, u32 conf0)
  1288. {
  1289. int direct_irq, ioapic_direct_irq_base;
  1290. u8 *match, direct_irq_mux[16];
  1291. u32 trig;
  1292. memcpy_fromio(direct_irq_mux, vg->communities->pad_regs + BYT_DIRECT_IRQ_REG,
  1293. sizeof(direct_irq_mux));
  1294. match = memchr(direct_irq_mux, pin, sizeof(direct_irq_mux));
  1295. if (!match) {
  1296. dev_warn(vg->dev, FW_BUG "pin %i: direct_irq_en set but no IRQ assigned, clearing\n", pin);
  1297. return false;
  1298. }
  1299. direct_irq = match - direct_irq_mux;
  1300. /* Base IO-APIC pin numbers come from atom-e3800-family-datasheet.pdf */
  1301. ioapic_direct_irq_base = (vg->communities->npins == BYT_NGPIO_SCORE) ? 51 : 67;
  1302. dev_dbg(vg->dev, "Pin %i: uses direct IRQ %d (IO-APIC %d)\n", pin,
  1303. direct_irq, direct_irq + ioapic_direct_irq_base);
  1304. /*
  1305. * Testing has shown that the way direct IRQs work is that the combination of the
  1306. * direct-irq-en flag and the direct IRQ mux connect the output of the GPIO's IRQ
  1307. * trigger block, which normally sets the status flag in the IRQ status reg at
  1308. * 0x800, to one of the IO-APIC pins according to the mux registers.
  1309. *
  1310. * This means that:
  1311. * 1. The TRIG_MASK bits must be set to configure the GPIO's IRQ trigger block
  1312. * 2. The TRIG_LVL bit *must* be set, so that the GPIO's input value is directly
  1313. * passed (1:1 or inverted) to the IO-APIC pin, if TRIG_LVL is not set,
  1314. * selecting edge mode operation then on the first edge the IO-APIC pin goes
  1315. * high, but since no write-to-clear write will be done to the IRQ status reg
  1316. * at 0x800, the detected edge condition will never get cleared.
  1317. */
  1318. trig = conf0 & BYT_TRIG_MASK;
  1319. if (trig != (BYT_TRIG_POS | BYT_TRIG_LVL) &&
  1320. trig != (BYT_TRIG_NEG | BYT_TRIG_LVL)) {
  1321. dev_warn(vg->dev, FW_BUG "pin %i: direct_irq_en set without trigger (conf0: %xh), clearing\n",
  1322. pin, conf0);
  1323. return false;
  1324. }
  1325. return true;
  1326. }
  1327. static void byt_init_irq_valid_mask(struct gpio_chip *chip,
  1328. unsigned long *valid_mask,
  1329. unsigned int ngpios)
  1330. {
  1331. struct intel_pinctrl *vg = gpiochip_get_data(chip);
  1332. void __iomem *reg;
  1333. u32 value;
  1334. int i;
  1335. /*
  1336. * Clear interrupt triggers for all pins that are GPIOs and
  1337. * do not use direct IRQ mode. This will prevent spurious
  1338. * interrupts from misconfigured pins.
  1339. */
  1340. for (i = 0; i < vg->soc->npins; i++) {
  1341. unsigned int pin = vg->soc->pins[i].number;
  1342. reg = byt_gpio_reg(vg, pin, BYT_CONF0_REG);
  1343. if (!reg) {
  1344. dev_warn(vg->dev,
  1345. "Pin %i: could not retrieve conf0 register\n",
  1346. i);
  1347. continue;
  1348. }
  1349. value = readl(reg);
  1350. if (value & BYT_DIRECT_IRQ_EN) {
  1351. if (byt_direct_irq_sanity_check(vg, i, value)) {
  1352. clear_bit(i, valid_mask);
  1353. } else {
  1354. value &= ~(BYT_DIRECT_IRQ_EN | BYT_TRIG_POS |
  1355. BYT_TRIG_NEG | BYT_TRIG_LVL);
  1356. writel(value, reg);
  1357. }
  1358. } else if ((value & BYT_PIN_MUX) == byt_get_gpio_mux(vg, i)) {
  1359. byt_gpio_clear_triggering(vg, i);
  1360. dev_dbg(vg->dev, "disabling GPIO %d\n", i);
  1361. }
  1362. }
  1363. }
  1364. static int byt_gpio_irq_init_hw(struct gpio_chip *chip)
  1365. {
  1366. struct intel_pinctrl *vg = gpiochip_get_data(chip);
  1367. void __iomem *reg;
  1368. u32 base, value;
  1369. /* clear interrupt status trigger registers */
  1370. for (base = 0; base < vg->soc->npins; base += 32) {
  1371. reg = byt_gpio_reg(vg, base, BYT_INT_STAT_REG);
  1372. if (!reg) {
  1373. dev_warn(vg->dev,
  1374. "Pin %i: could not retrieve irq status reg\n",
  1375. base);
  1376. continue;
  1377. }
  1378. writel(0xffffffff, reg);
  1379. /* make sure trigger bits are cleared, if not then a pin
  1380. might be misconfigured in bios */
  1381. value = readl(reg);
  1382. if (value)
  1383. dev_err(vg->dev,
  1384. "GPIO interrupt error, pins misconfigured. INT_STAT%u: 0x%08x\n",
  1385. base / 32, value);
  1386. }
  1387. return 0;
  1388. }
  1389. static int byt_gpio_add_pin_ranges(struct gpio_chip *chip)
  1390. {
  1391. struct intel_pinctrl *vg = gpiochip_get_data(chip);
  1392. struct device *dev = vg->dev;
  1393. int ret;
  1394. ret = gpiochip_add_pin_range(chip, dev_name(dev), 0, 0, vg->soc->npins);
  1395. if (ret)
  1396. dev_err(dev, "failed to add GPIO pin range\n");
  1397. return ret;
  1398. }
  1399. static int byt_gpio_probe(struct intel_pinctrl *vg)
  1400. {
  1401. struct platform_device *pdev = to_platform_device(vg->dev);
  1402. struct gpio_chip *gc;
  1403. int irq, ret;
  1404. /* Set up gpio chip */
  1405. vg->chip = byt_gpio_chip;
  1406. gc = &vg->chip;
  1407. gc->label = dev_name(vg->dev);
  1408. gc->base = -1;
  1409. gc->can_sleep = false;
  1410. gc->add_pin_ranges = byt_gpio_add_pin_ranges;
  1411. gc->parent = vg->dev;
  1412. gc->ngpio = vg->soc->npins;
  1413. #ifdef CONFIG_PM_SLEEP
  1414. vg->context.pads = devm_kcalloc(vg->dev, gc->ngpio, sizeof(*vg->context.pads),
  1415. GFP_KERNEL);
  1416. if (!vg->context.pads)
  1417. return -ENOMEM;
  1418. #endif
  1419. /* set up interrupts */
  1420. irq = platform_get_irq_optional(pdev, 0);
  1421. if (irq > 0) {
  1422. struct gpio_irq_chip *girq;
  1423. girq = &gc->irq;
  1424. gpio_irq_chip_set_chip(girq, &byt_gpio_irq_chip);
  1425. girq->init_hw = byt_gpio_irq_init_hw;
  1426. girq->init_valid_mask = byt_init_irq_valid_mask;
  1427. girq->parent_handler = byt_gpio_irq_handler;
  1428. girq->num_parents = 1;
  1429. girq->parents = devm_kcalloc(vg->dev, girq->num_parents,
  1430. sizeof(*girq->parents), GFP_KERNEL);
  1431. if (!girq->parents)
  1432. return -ENOMEM;
  1433. girq->parents[0] = irq;
  1434. girq->default_type = IRQ_TYPE_NONE;
  1435. girq->handler = handle_bad_irq;
  1436. }
  1437. ret = devm_gpiochip_add_data(vg->dev, gc, vg);
  1438. if (ret) {
  1439. dev_err(vg->dev, "failed adding byt-gpio chip\n");
  1440. return ret;
  1441. }
  1442. return ret;
  1443. }
  1444. static int byt_set_soc_data(struct intel_pinctrl *vg,
  1445. const struct intel_pinctrl_soc_data *soc)
  1446. {
  1447. struct platform_device *pdev = to_platform_device(vg->dev);
  1448. int i;
  1449. vg->soc = soc;
  1450. vg->ncommunities = vg->soc->ncommunities;
  1451. vg->communities = devm_kcalloc(vg->dev, vg->ncommunities,
  1452. sizeof(*vg->communities), GFP_KERNEL);
  1453. if (!vg->communities)
  1454. return -ENOMEM;
  1455. for (i = 0; i < vg->soc->ncommunities; i++) {
  1456. struct intel_community *comm = vg->communities + i;
  1457. *comm = vg->soc->communities[i];
  1458. comm->pad_regs = devm_platform_ioremap_resource(pdev, 0);
  1459. if (IS_ERR(comm->pad_regs))
  1460. return PTR_ERR(comm->pad_regs);
  1461. }
  1462. return 0;
  1463. }
  1464. static const struct acpi_device_id byt_gpio_acpi_match[] = {
  1465. { "INT33B2", (kernel_ulong_t)byt_soc_data },
  1466. { "INT33FC", (kernel_ulong_t)byt_soc_data },
  1467. { }
  1468. };
  1469. static int byt_pinctrl_probe(struct platform_device *pdev)
  1470. {
  1471. const struct intel_pinctrl_soc_data *soc_data;
  1472. struct device *dev = &pdev->dev;
  1473. struct intel_pinctrl *vg;
  1474. int ret;
  1475. soc_data = intel_pinctrl_get_soc_data(pdev);
  1476. if (IS_ERR(soc_data))
  1477. return PTR_ERR(soc_data);
  1478. vg = devm_kzalloc(dev, sizeof(*vg), GFP_KERNEL);
  1479. if (!vg)
  1480. return -ENOMEM;
  1481. vg->dev = dev;
  1482. ret = byt_set_soc_data(vg, soc_data);
  1483. if (ret) {
  1484. dev_err(dev, "failed to set soc data\n");
  1485. return ret;
  1486. }
  1487. vg->pctldesc = byt_pinctrl_desc;
  1488. vg->pctldesc.name = dev_name(dev);
  1489. vg->pctldesc.pins = vg->soc->pins;
  1490. vg->pctldesc.npins = vg->soc->npins;
  1491. vg->pctldev = devm_pinctrl_register(dev, &vg->pctldesc, vg);
  1492. if (IS_ERR(vg->pctldev)) {
  1493. dev_err(dev, "failed to register pinctrl driver\n");
  1494. return PTR_ERR(vg->pctldev);
  1495. }
  1496. ret = byt_gpio_probe(vg);
  1497. if (ret)
  1498. return ret;
  1499. platform_set_drvdata(pdev, vg);
  1500. pm_runtime_enable(dev);
  1501. return 0;
  1502. }
  1503. #ifdef CONFIG_PM_SLEEP
  1504. static int byt_gpio_suspend(struct device *dev)
  1505. {
  1506. struct intel_pinctrl *vg = dev_get_drvdata(dev);
  1507. unsigned long flags;
  1508. int i;
  1509. raw_spin_lock_irqsave(&byt_lock, flags);
  1510. for (i = 0; i < vg->soc->npins; i++) {
  1511. void __iomem *reg;
  1512. u32 value;
  1513. unsigned int pin = vg->soc->pins[i].number;
  1514. reg = byt_gpio_reg(vg, pin, BYT_CONF0_REG);
  1515. if (!reg) {
  1516. dev_warn(vg->dev,
  1517. "Pin %i: could not retrieve conf0 register\n",
  1518. i);
  1519. continue;
  1520. }
  1521. value = readl(reg) & BYT_CONF0_RESTORE_MASK;
  1522. vg->context.pads[i].conf0 = value;
  1523. reg = byt_gpio_reg(vg, pin, BYT_VAL_REG);
  1524. value = readl(reg) & BYT_VAL_RESTORE_MASK;
  1525. vg->context.pads[i].val = value;
  1526. }
  1527. raw_spin_unlock_irqrestore(&byt_lock, flags);
  1528. return 0;
  1529. }
  1530. static int byt_gpio_resume(struct device *dev)
  1531. {
  1532. struct intel_pinctrl *vg = dev_get_drvdata(dev);
  1533. unsigned long flags;
  1534. int i;
  1535. raw_spin_lock_irqsave(&byt_lock, flags);
  1536. for (i = 0; i < vg->soc->npins; i++) {
  1537. void __iomem *reg;
  1538. u32 value;
  1539. unsigned int pin = vg->soc->pins[i].number;
  1540. reg = byt_gpio_reg(vg, pin, BYT_CONF0_REG);
  1541. if (!reg) {
  1542. dev_warn(vg->dev,
  1543. "Pin %i: could not retrieve conf0 register\n",
  1544. i);
  1545. continue;
  1546. }
  1547. value = readl(reg);
  1548. if ((value & BYT_CONF0_RESTORE_MASK) !=
  1549. vg->context.pads[i].conf0) {
  1550. value &= ~BYT_CONF0_RESTORE_MASK;
  1551. value |= vg->context.pads[i].conf0;
  1552. writel(value, reg);
  1553. dev_info(dev, "restored pin %d conf0 %#08x", i, value);
  1554. }
  1555. reg = byt_gpio_reg(vg, pin, BYT_VAL_REG);
  1556. value = readl(reg);
  1557. if ((value & BYT_VAL_RESTORE_MASK) !=
  1558. vg->context.pads[i].val) {
  1559. u32 v;
  1560. v = value & ~BYT_VAL_RESTORE_MASK;
  1561. v |= vg->context.pads[i].val;
  1562. if (v != value) {
  1563. writel(v, reg);
  1564. dev_dbg(dev, "restored pin %d val %#08x\n",
  1565. i, v);
  1566. }
  1567. }
  1568. }
  1569. raw_spin_unlock_irqrestore(&byt_lock, flags);
  1570. return 0;
  1571. }
  1572. #endif
  1573. #ifdef CONFIG_PM
  1574. static int byt_gpio_runtime_suspend(struct device *dev)
  1575. {
  1576. return 0;
  1577. }
  1578. static int byt_gpio_runtime_resume(struct device *dev)
  1579. {
  1580. return 0;
  1581. }
  1582. #endif
  1583. static const struct dev_pm_ops byt_gpio_pm_ops = {
  1584. SET_LATE_SYSTEM_SLEEP_PM_OPS(byt_gpio_suspend, byt_gpio_resume)
  1585. SET_RUNTIME_PM_OPS(byt_gpio_runtime_suspend, byt_gpio_runtime_resume,
  1586. NULL)
  1587. };
  1588. static struct platform_driver byt_gpio_driver = {
  1589. .probe = byt_pinctrl_probe,
  1590. .driver = {
  1591. .name = "byt_gpio",
  1592. .pm = &byt_gpio_pm_ops,
  1593. .acpi_match_table = byt_gpio_acpi_match,
  1594. .suppress_bind_attrs = true,
  1595. },
  1596. };
  1597. static int __init byt_gpio_init(void)
  1598. {
  1599. return platform_driver_register(&byt_gpio_driver);
  1600. }
  1601. subsys_initcall(byt_gpio_init);