pinctrl-baytrail.c 51 KB

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