pinctrl-baytrail.c 53 KB

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