pinctrl-baytrail.c 51 KB

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