pinctrl-baytrail.c 51 KB

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