pinctrl-baytrail.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  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;
  674. if (!comm)
  675. return NULL;
  676. offset -= comm->pin_base;
  677. switch (reg) {
  678. case BYT_INT_STAT_REG:
  679. reg_offset = (offset / 32) * 4;
  680. break;
  681. case BYT_DEBOUNCE_REG:
  682. reg_offset = 0;
  683. break;
  684. default:
  685. reg_offset = comm->pad_map[offset] * 16;
  686. break;
  687. }
  688. return comm->reg_base + reg_offset + reg;
  689. }
  690. static int byt_get_groups_count(struct pinctrl_dev *pctldev)
  691. {
  692. struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
  693. return vg->soc_data->ngroups;
  694. }
  695. static const char *byt_get_group_name(struct pinctrl_dev *pctldev,
  696. unsigned int selector)
  697. {
  698. struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
  699. return vg->soc_data->groups[selector].name;
  700. }
  701. static int byt_get_group_pins(struct pinctrl_dev *pctldev,
  702. unsigned int selector,
  703. const unsigned int **pins,
  704. unsigned int *num_pins)
  705. {
  706. struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
  707. *pins = vg->soc_data->groups[selector].pins;
  708. *num_pins = vg->soc_data->groups[selector].npins;
  709. return 0;
  710. }
  711. static const struct pinctrl_ops byt_pinctrl_ops = {
  712. .get_groups_count = byt_get_groups_count,
  713. .get_group_name = byt_get_group_name,
  714. .get_group_pins = byt_get_group_pins,
  715. };
  716. static int byt_get_functions_count(struct pinctrl_dev *pctldev)
  717. {
  718. struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
  719. return vg->soc_data->nfunctions;
  720. }
  721. static const char *byt_get_function_name(struct pinctrl_dev *pctldev,
  722. unsigned int selector)
  723. {
  724. struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
  725. return vg->soc_data->functions[selector].name;
  726. }
  727. static int byt_get_function_groups(struct pinctrl_dev *pctldev,
  728. unsigned int selector,
  729. const char * const **groups,
  730. unsigned int *num_groups)
  731. {
  732. struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
  733. *groups = vg->soc_data->functions[selector].groups;
  734. *num_groups = vg->soc_data->functions[selector].ngroups;
  735. return 0;
  736. }
  737. static int byt_get_group_simple_mux(const struct byt_pingroup group,
  738. const char *func_name,
  739. unsigned short *func)
  740. {
  741. int i;
  742. for (i = 0; i < group.nfuncs; i++) {
  743. if (!strcmp(group.simple_funcs[i].name, func_name)) {
  744. *func = group.simple_funcs[i].func;
  745. return 0;
  746. }
  747. }
  748. return 1;
  749. }
  750. static int byt_get_group_mixed_mux(const struct byt_pingroup group,
  751. const char *func_name,
  752. const unsigned short **func)
  753. {
  754. int i;
  755. for (i = 0; i < group.nfuncs; i++) {
  756. if (!strcmp(group.mixed_funcs[i].name, func_name)) {
  757. *func = group.mixed_funcs[i].func_values;
  758. return 0;
  759. }
  760. }
  761. return 1;
  762. }
  763. static void byt_set_group_simple_mux(struct byt_gpio *vg,
  764. const struct byt_pingroup group,
  765. unsigned short func)
  766. {
  767. unsigned long flags;
  768. int i;
  769. raw_spin_lock_irqsave(&vg->lock, flags);
  770. for (i = 0; i < group.npins; i++) {
  771. void __iomem *padcfg0;
  772. u32 value;
  773. padcfg0 = byt_gpio_reg(vg, group.pins[i], BYT_CONF0_REG);
  774. if (!padcfg0) {
  775. dev_warn(&vg->pdev->dev,
  776. "Group %s, pin %i not muxed (no padcfg0)\n",
  777. group.name, i);
  778. continue;
  779. }
  780. value = readl(padcfg0);
  781. value &= ~BYT_PIN_MUX;
  782. value |= func;
  783. writel(value, padcfg0);
  784. }
  785. raw_spin_unlock_irqrestore(&vg->lock, flags);
  786. }
  787. static void byt_set_group_mixed_mux(struct byt_gpio *vg,
  788. const struct byt_pingroup group,
  789. const unsigned short *func)
  790. {
  791. unsigned long flags;
  792. int i;
  793. raw_spin_lock_irqsave(&vg->lock, flags);
  794. for (i = 0; i < group.npins; i++) {
  795. void __iomem *padcfg0;
  796. u32 value;
  797. padcfg0 = byt_gpio_reg(vg, group.pins[i], BYT_CONF0_REG);
  798. if (!padcfg0) {
  799. dev_warn(&vg->pdev->dev,
  800. "Group %s, pin %i not muxed (no padcfg0)\n",
  801. group.name, i);
  802. continue;
  803. }
  804. value = readl(padcfg0);
  805. value &= ~BYT_PIN_MUX;
  806. value |= func[i];
  807. writel(value, padcfg0);
  808. }
  809. raw_spin_unlock_irqrestore(&vg->lock, flags);
  810. }
  811. static int byt_set_mux(struct pinctrl_dev *pctldev, unsigned int func_selector,
  812. unsigned int group_selector)
  813. {
  814. struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
  815. const struct byt_function func = vg->soc_data->functions[func_selector];
  816. const struct byt_pingroup group = vg->soc_data->groups[group_selector];
  817. const unsigned short *mixed_func;
  818. unsigned short simple_func;
  819. int ret = 1;
  820. if (group.has_simple_funcs)
  821. ret = byt_get_group_simple_mux(group, func.name, &simple_func);
  822. else
  823. ret = byt_get_group_mixed_mux(group, func.name, &mixed_func);
  824. if (ret)
  825. byt_set_group_simple_mux(vg, group, BYT_DEFAULT_GPIO_MUX);
  826. else if (group.has_simple_funcs)
  827. byt_set_group_simple_mux(vg, group, simple_func);
  828. else
  829. byt_set_group_mixed_mux(vg, group, mixed_func);
  830. return 0;
  831. }
  832. static u32 byt_get_gpio_mux(struct byt_gpio *vg, unsigned offset)
  833. {
  834. /* SCORE pin 92-93 */
  835. if (!strcmp(vg->soc_data->uid, BYT_SCORE_ACPI_UID) &&
  836. offset >= 92 && offset <= 93)
  837. return 1;
  838. /* SUS pin 11-21 */
  839. if (!strcmp(vg->soc_data->uid, BYT_SUS_ACPI_UID) &&
  840. offset >= 11 && offset <= 21)
  841. return 1;
  842. return 0;
  843. }
  844. static void byt_gpio_clear_triggering(struct byt_gpio *vg, unsigned int offset)
  845. {
  846. void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
  847. unsigned long flags;
  848. u32 value;
  849. raw_spin_lock_irqsave(&vg->lock, flags);
  850. value = readl(reg);
  851. value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
  852. writel(value, reg);
  853. raw_spin_unlock_irqrestore(&vg->lock, flags);
  854. }
  855. static int byt_gpio_request_enable(struct pinctrl_dev *pctl_dev,
  856. struct pinctrl_gpio_range *range,
  857. unsigned int offset)
  858. {
  859. struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
  860. void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
  861. u32 value, gpio_mux;
  862. unsigned long flags;
  863. raw_spin_lock_irqsave(&vg->lock, flags);
  864. /*
  865. * In most cases, func pin mux 000 means GPIO function.
  866. * But, some pins may have func pin mux 001 represents
  867. * GPIO function.
  868. *
  869. * Because there are devices out there where some pins were not
  870. * configured correctly we allow changing the mux value from
  871. * request (but print out warning about that).
  872. */
  873. value = readl(reg) & BYT_PIN_MUX;
  874. gpio_mux = byt_get_gpio_mux(vg, offset);
  875. if (WARN_ON(gpio_mux != value)) {
  876. value = readl(reg) & ~BYT_PIN_MUX;
  877. value |= gpio_mux;
  878. writel(value, reg);
  879. dev_warn(&vg->pdev->dev,
  880. "pin %u forcibly re-configured as GPIO\n", offset);
  881. }
  882. raw_spin_unlock_irqrestore(&vg->lock, flags);
  883. pm_runtime_get(&vg->pdev->dev);
  884. return 0;
  885. }
  886. static void byt_gpio_disable_free(struct pinctrl_dev *pctl_dev,
  887. struct pinctrl_gpio_range *range,
  888. unsigned int offset)
  889. {
  890. struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
  891. byt_gpio_clear_triggering(vg, offset);
  892. pm_runtime_put(&vg->pdev->dev);
  893. }
  894. static int byt_gpio_set_direction(struct pinctrl_dev *pctl_dev,
  895. struct pinctrl_gpio_range *range,
  896. unsigned int offset,
  897. bool input)
  898. {
  899. struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
  900. void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  901. void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
  902. unsigned long flags;
  903. u32 value;
  904. raw_spin_lock_irqsave(&vg->lock, flags);
  905. value = readl(val_reg);
  906. value &= ~BYT_DIR_MASK;
  907. if (input)
  908. value |= BYT_OUTPUT_EN;
  909. else
  910. /*
  911. * Before making any direction modifications, do a check if gpio
  912. * is set for direct IRQ. On baytrail, setting GPIO to output
  913. * does not make sense, so let's at least warn the caller before
  914. * they shoot themselves in the foot.
  915. */
  916. WARN(readl(conf_reg) & BYT_DIRECT_IRQ_EN,
  917. "Potential Error: Setting GPIO with direct_irq_en to output");
  918. writel(value, val_reg);
  919. raw_spin_unlock_irqrestore(&vg->lock, flags);
  920. return 0;
  921. }
  922. static const struct pinmux_ops byt_pinmux_ops = {
  923. .get_functions_count = byt_get_functions_count,
  924. .get_function_name = byt_get_function_name,
  925. .get_function_groups = byt_get_function_groups,
  926. .set_mux = byt_set_mux,
  927. .gpio_request_enable = byt_gpio_request_enable,
  928. .gpio_disable_free = byt_gpio_disable_free,
  929. .gpio_set_direction = byt_gpio_set_direction,
  930. };
  931. static void byt_get_pull_strength(u32 reg, u16 *strength)
  932. {
  933. switch (reg & BYT_PULL_STR_MASK) {
  934. case BYT_PULL_STR_2K:
  935. *strength = 2000;
  936. break;
  937. case BYT_PULL_STR_10K:
  938. *strength = 10000;
  939. break;
  940. case BYT_PULL_STR_20K:
  941. *strength = 20000;
  942. break;
  943. case BYT_PULL_STR_40K:
  944. *strength = 40000;
  945. break;
  946. }
  947. }
  948. static int byt_set_pull_strength(u32 *reg, u16 strength)
  949. {
  950. *reg &= ~BYT_PULL_STR_MASK;
  951. switch (strength) {
  952. case 2000:
  953. *reg |= BYT_PULL_STR_2K;
  954. break;
  955. case 10000:
  956. *reg |= BYT_PULL_STR_10K;
  957. break;
  958. case 20000:
  959. *reg |= BYT_PULL_STR_20K;
  960. break;
  961. case 40000:
  962. *reg |= BYT_PULL_STR_40K;
  963. break;
  964. default:
  965. return -EINVAL;
  966. }
  967. return 0;
  968. }
  969. static int byt_pin_config_get(struct pinctrl_dev *pctl_dev, unsigned int offset,
  970. unsigned long *config)
  971. {
  972. struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
  973. enum pin_config_param param = pinconf_to_config_param(*config);
  974. void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
  975. void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  976. void __iomem *db_reg = byt_gpio_reg(vg, offset, BYT_DEBOUNCE_REG);
  977. unsigned long flags;
  978. u32 conf, pull, val, debounce;
  979. u16 arg = 0;
  980. raw_spin_lock_irqsave(&vg->lock, flags);
  981. conf = readl(conf_reg);
  982. pull = conf & BYT_PULL_ASSIGN_MASK;
  983. val = readl(val_reg);
  984. raw_spin_unlock_irqrestore(&vg->lock, flags);
  985. switch (param) {
  986. case PIN_CONFIG_BIAS_DISABLE:
  987. if (pull)
  988. return -EINVAL;
  989. break;
  990. case PIN_CONFIG_BIAS_PULL_DOWN:
  991. /* Pull assignment is only applicable in input mode */
  992. if ((val & BYT_INPUT_EN) || pull != BYT_PULL_ASSIGN_DOWN)
  993. return -EINVAL;
  994. byt_get_pull_strength(conf, &arg);
  995. break;
  996. case PIN_CONFIG_BIAS_PULL_UP:
  997. /* Pull assignment is only applicable in input mode */
  998. if ((val & BYT_INPUT_EN) || pull != BYT_PULL_ASSIGN_UP)
  999. return -EINVAL;
  1000. byt_get_pull_strength(conf, &arg);
  1001. break;
  1002. case PIN_CONFIG_INPUT_DEBOUNCE:
  1003. if (!(conf & BYT_DEBOUNCE_EN))
  1004. return -EINVAL;
  1005. raw_spin_lock_irqsave(&vg->lock, flags);
  1006. debounce = readl(db_reg);
  1007. raw_spin_unlock_irqrestore(&vg->lock, flags);
  1008. switch (debounce & BYT_DEBOUNCE_PULSE_MASK) {
  1009. case BYT_DEBOUNCE_PULSE_375US:
  1010. arg = 375;
  1011. break;
  1012. case BYT_DEBOUNCE_PULSE_750US:
  1013. arg = 750;
  1014. break;
  1015. case BYT_DEBOUNCE_PULSE_1500US:
  1016. arg = 1500;
  1017. break;
  1018. case BYT_DEBOUNCE_PULSE_3MS:
  1019. arg = 3000;
  1020. break;
  1021. case BYT_DEBOUNCE_PULSE_6MS:
  1022. arg = 6000;
  1023. break;
  1024. case BYT_DEBOUNCE_PULSE_12MS:
  1025. arg = 12000;
  1026. break;
  1027. case BYT_DEBOUNCE_PULSE_24MS:
  1028. arg = 24000;
  1029. break;
  1030. default:
  1031. return -EINVAL;
  1032. }
  1033. break;
  1034. default:
  1035. return -ENOTSUPP;
  1036. }
  1037. *config = pinconf_to_config_packed(param, arg);
  1038. return 0;
  1039. }
  1040. static int byt_pin_config_set(struct pinctrl_dev *pctl_dev,
  1041. unsigned int offset,
  1042. unsigned long *configs,
  1043. unsigned int num_configs)
  1044. {
  1045. struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
  1046. unsigned int param, arg;
  1047. void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
  1048. void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  1049. void __iomem *db_reg = byt_gpio_reg(vg, offset, BYT_DEBOUNCE_REG);
  1050. unsigned long flags;
  1051. u32 conf, val, debounce;
  1052. int i, ret = 0;
  1053. raw_spin_lock_irqsave(&vg->lock, flags);
  1054. conf = readl(conf_reg);
  1055. val = readl(val_reg);
  1056. for (i = 0; i < num_configs; i++) {
  1057. param = pinconf_to_config_param(configs[i]);
  1058. arg = pinconf_to_config_argument(configs[i]);
  1059. switch (param) {
  1060. case PIN_CONFIG_BIAS_DISABLE:
  1061. conf &= ~BYT_PULL_ASSIGN_MASK;
  1062. break;
  1063. case PIN_CONFIG_BIAS_PULL_DOWN:
  1064. /* Set default strength value in case none is given */
  1065. if (arg == 1)
  1066. arg = 2000;
  1067. /*
  1068. * Pull assignment is only applicable in input mode. If
  1069. * chip is not in input mode, set it and warn about it.
  1070. */
  1071. if (val & BYT_INPUT_EN) {
  1072. val &= ~BYT_INPUT_EN;
  1073. writel(val, val_reg);
  1074. dev_warn(&vg->pdev->dev,
  1075. "pin %u forcibly set to input mode\n",
  1076. offset);
  1077. }
  1078. conf &= ~BYT_PULL_ASSIGN_MASK;
  1079. conf |= BYT_PULL_ASSIGN_DOWN;
  1080. ret = byt_set_pull_strength(&conf, arg);
  1081. break;
  1082. case PIN_CONFIG_BIAS_PULL_UP:
  1083. /* Set default strength value in case none is given */
  1084. if (arg == 1)
  1085. arg = 2000;
  1086. /*
  1087. * Pull assignment is only applicable in input mode. If
  1088. * chip is not in input mode, set it and warn about it.
  1089. */
  1090. if (val & BYT_INPUT_EN) {
  1091. val &= ~BYT_INPUT_EN;
  1092. writel(val, val_reg);
  1093. dev_warn(&vg->pdev->dev,
  1094. "pin %u forcibly set to input mode\n",
  1095. offset);
  1096. }
  1097. conf &= ~BYT_PULL_ASSIGN_MASK;
  1098. conf |= BYT_PULL_ASSIGN_UP;
  1099. ret = byt_set_pull_strength(&conf, arg);
  1100. break;
  1101. case PIN_CONFIG_INPUT_DEBOUNCE:
  1102. debounce = readl(db_reg);
  1103. debounce &= ~BYT_DEBOUNCE_PULSE_MASK;
  1104. if (arg)
  1105. conf |= BYT_DEBOUNCE_EN;
  1106. else
  1107. conf &= ~BYT_DEBOUNCE_EN;
  1108. switch (arg) {
  1109. case 375:
  1110. debounce |= BYT_DEBOUNCE_PULSE_375US;
  1111. break;
  1112. case 750:
  1113. debounce |= BYT_DEBOUNCE_PULSE_750US;
  1114. break;
  1115. case 1500:
  1116. debounce |= BYT_DEBOUNCE_PULSE_1500US;
  1117. break;
  1118. case 3000:
  1119. debounce |= BYT_DEBOUNCE_PULSE_3MS;
  1120. break;
  1121. case 6000:
  1122. debounce |= BYT_DEBOUNCE_PULSE_6MS;
  1123. break;
  1124. case 12000:
  1125. debounce |= BYT_DEBOUNCE_PULSE_12MS;
  1126. break;
  1127. case 24000:
  1128. debounce |= BYT_DEBOUNCE_PULSE_24MS;
  1129. break;
  1130. default:
  1131. if (arg)
  1132. ret = -EINVAL;
  1133. break;
  1134. }
  1135. if (!ret)
  1136. writel(debounce, db_reg);
  1137. break;
  1138. default:
  1139. ret = -ENOTSUPP;
  1140. }
  1141. if (ret)
  1142. break;
  1143. }
  1144. if (!ret)
  1145. writel(conf, conf_reg);
  1146. raw_spin_unlock_irqrestore(&vg->lock, flags);
  1147. return ret;
  1148. }
  1149. static const struct pinconf_ops byt_pinconf_ops = {
  1150. .is_generic = true,
  1151. .pin_config_get = byt_pin_config_get,
  1152. .pin_config_set = byt_pin_config_set,
  1153. };
  1154. static const struct pinctrl_desc byt_pinctrl_desc = {
  1155. .pctlops = &byt_pinctrl_ops,
  1156. .pmxops = &byt_pinmux_ops,
  1157. .confops = &byt_pinconf_ops,
  1158. .owner = THIS_MODULE,
  1159. };
  1160. static int byt_gpio_get(struct gpio_chip *chip, unsigned offset)
  1161. {
  1162. struct byt_gpio *vg = gpiochip_get_data(chip);
  1163. void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  1164. unsigned long flags;
  1165. u32 val;
  1166. raw_spin_lock_irqsave(&vg->lock, flags);
  1167. val = readl(reg);
  1168. raw_spin_unlock_irqrestore(&vg->lock, flags);
  1169. return !!(val & BYT_LEVEL);
  1170. }
  1171. static void byt_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  1172. {
  1173. struct byt_gpio *vg = gpiochip_get_data(chip);
  1174. void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  1175. unsigned long flags;
  1176. u32 old_val;
  1177. if (!reg)
  1178. return;
  1179. raw_spin_lock_irqsave(&vg->lock, flags);
  1180. old_val = readl(reg);
  1181. if (value)
  1182. writel(old_val | BYT_LEVEL, reg);
  1183. else
  1184. writel(old_val & ~BYT_LEVEL, reg);
  1185. raw_spin_unlock_irqrestore(&vg->lock, flags);
  1186. }
  1187. static int byt_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  1188. {
  1189. struct byt_gpio *vg = gpiochip_get_data(chip);
  1190. void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
  1191. unsigned long flags;
  1192. u32 value;
  1193. if (!reg)
  1194. return -EINVAL;
  1195. raw_spin_lock_irqsave(&vg->lock, flags);
  1196. value = readl(reg);
  1197. raw_spin_unlock_irqrestore(&vg->lock, flags);
  1198. if (!(value & BYT_OUTPUT_EN))
  1199. return GPIOF_DIR_OUT;
  1200. if (!(value & BYT_INPUT_EN))
  1201. return GPIOF_DIR_IN;
  1202. return -EINVAL;
  1203. }
  1204. static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
  1205. {
  1206. return pinctrl_gpio_direction_input(chip->base + offset);
  1207. }
  1208. static int byt_gpio_direction_output(struct gpio_chip *chip,
  1209. unsigned int offset, int value)
  1210. {
  1211. int ret = pinctrl_gpio_direction_output(chip->base + offset);
  1212. if (ret)
  1213. return ret;
  1214. byt_gpio_set(chip, offset, value);
  1215. return 0;
  1216. }
  1217. static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1218. {
  1219. struct byt_gpio *vg = gpiochip_get_data(chip);
  1220. int i;
  1221. u32 conf0, val;
  1222. for (i = 0; i < vg->soc_data->npins; i++) {
  1223. const struct byt_community *comm;
  1224. const char *pull_str = NULL;
  1225. const char *pull = NULL;
  1226. void __iomem *reg;
  1227. unsigned long flags;
  1228. const char *label;
  1229. unsigned int pin;
  1230. raw_spin_lock_irqsave(&vg->lock, flags);
  1231. pin = vg->soc_data->pins[i].number;
  1232. reg = byt_gpio_reg(vg, pin, BYT_CONF0_REG);
  1233. if (!reg) {
  1234. seq_printf(s,
  1235. "Could not retrieve pin %i conf0 reg\n",
  1236. pin);
  1237. raw_spin_unlock_irqrestore(&vg->lock, flags);
  1238. continue;
  1239. }
  1240. conf0 = readl(reg);
  1241. reg = byt_gpio_reg(vg, pin, BYT_VAL_REG);
  1242. if (!reg) {
  1243. seq_printf(s,
  1244. "Could not retrieve pin %i val reg\n", pin);
  1245. raw_spin_unlock_irqrestore(&vg->lock, flags);
  1246. continue;
  1247. }
  1248. val = readl(reg);
  1249. raw_spin_unlock_irqrestore(&vg->lock, flags);
  1250. comm = byt_get_community(vg, pin);
  1251. if (!comm) {
  1252. seq_printf(s,
  1253. "Could not get community for pin %i\n", pin);
  1254. continue;
  1255. }
  1256. label = gpiochip_is_requested(chip, i);
  1257. if (!label)
  1258. label = "Unrequested";
  1259. switch (conf0 & BYT_PULL_ASSIGN_MASK) {
  1260. case BYT_PULL_ASSIGN_UP:
  1261. pull = "up";
  1262. break;
  1263. case BYT_PULL_ASSIGN_DOWN:
  1264. pull = "down";
  1265. break;
  1266. }
  1267. switch (conf0 & BYT_PULL_STR_MASK) {
  1268. case BYT_PULL_STR_2K:
  1269. pull_str = "2k";
  1270. break;
  1271. case BYT_PULL_STR_10K:
  1272. pull_str = "10k";
  1273. break;
  1274. case BYT_PULL_STR_20K:
  1275. pull_str = "20k";
  1276. break;
  1277. case BYT_PULL_STR_40K:
  1278. pull_str = "40k";
  1279. break;
  1280. }
  1281. seq_printf(s,
  1282. " gpio-%-3d (%-20.20s) %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s",
  1283. pin,
  1284. label,
  1285. val & BYT_INPUT_EN ? " " : "in",
  1286. val & BYT_OUTPUT_EN ? " " : "out",
  1287. val & BYT_LEVEL ? "hi" : "lo",
  1288. comm->pad_map[i], comm->pad_map[i] * 32,
  1289. conf0 & 0x7,
  1290. conf0 & BYT_TRIG_NEG ? " fall" : " ",
  1291. conf0 & BYT_TRIG_POS ? " rise" : " ",
  1292. conf0 & BYT_TRIG_LVL ? " level" : " ");
  1293. if (pull && pull_str)
  1294. seq_printf(s, " %-4s %-3s", pull, pull_str);
  1295. else
  1296. seq_puts(s, " ");
  1297. if (conf0 & BYT_IODEN)
  1298. seq_puts(s, " open-drain");
  1299. seq_puts(s, "\n");
  1300. }
  1301. }
  1302. static const struct gpio_chip byt_gpio_chip = {
  1303. .owner = THIS_MODULE,
  1304. .request = gpiochip_generic_request,
  1305. .free = gpiochip_generic_free,
  1306. .get_direction = byt_gpio_get_direction,
  1307. .direction_input = byt_gpio_direction_input,
  1308. .direction_output = byt_gpio_direction_output,
  1309. .get = byt_gpio_get,
  1310. .set = byt_gpio_set,
  1311. .dbg_show = byt_gpio_dbg_show,
  1312. };
  1313. static void byt_irq_ack(struct irq_data *d)
  1314. {
  1315. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1316. struct byt_gpio *vg = gpiochip_get_data(gc);
  1317. unsigned offset = irqd_to_hwirq(d);
  1318. void __iomem *reg;
  1319. reg = byt_gpio_reg(vg, offset, BYT_INT_STAT_REG);
  1320. if (!reg)
  1321. return;
  1322. raw_spin_lock(&vg->lock);
  1323. writel(BIT(offset % 32), reg);
  1324. raw_spin_unlock(&vg->lock);
  1325. }
  1326. static void byt_irq_mask(struct irq_data *d)
  1327. {
  1328. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1329. struct byt_gpio *vg = gpiochip_get_data(gc);
  1330. byt_gpio_clear_triggering(vg, irqd_to_hwirq(d));
  1331. }
  1332. static void byt_irq_unmask(struct irq_data *d)
  1333. {
  1334. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1335. struct byt_gpio *vg = gpiochip_get_data(gc);
  1336. unsigned offset = irqd_to_hwirq(d);
  1337. unsigned long flags;
  1338. void __iomem *reg;
  1339. u32 value;
  1340. reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
  1341. if (!reg)
  1342. return;
  1343. raw_spin_lock_irqsave(&vg->lock, flags);
  1344. value = readl(reg);
  1345. switch (irqd_get_trigger_type(d)) {
  1346. case IRQ_TYPE_LEVEL_HIGH:
  1347. value |= BYT_TRIG_LVL;
  1348. case IRQ_TYPE_EDGE_RISING:
  1349. value |= BYT_TRIG_POS;
  1350. break;
  1351. case IRQ_TYPE_LEVEL_LOW:
  1352. value |= BYT_TRIG_LVL;
  1353. case IRQ_TYPE_EDGE_FALLING:
  1354. value |= BYT_TRIG_NEG;
  1355. break;
  1356. case IRQ_TYPE_EDGE_BOTH:
  1357. value |= (BYT_TRIG_NEG | BYT_TRIG_POS);
  1358. break;
  1359. }
  1360. writel(value, reg);
  1361. raw_spin_unlock_irqrestore(&vg->lock, flags);
  1362. }
  1363. static int byt_irq_type(struct irq_data *d, unsigned int type)
  1364. {
  1365. struct byt_gpio *vg = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  1366. u32 offset = irqd_to_hwirq(d);
  1367. u32 value;
  1368. unsigned long flags;
  1369. void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
  1370. if (!reg || offset >= vg->chip.ngpio)
  1371. return -EINVAL;
  1372. raw_spin_lock_irqsave(&vg->lock, flags);
  1373. value = readl(reg);
  1374. WARN(value & BYT_DIRECT_IRQ_EN,
  1375. "Bad pad config for io mode, force direct_irq_en bit clearing");
  1376. /* For level trigges the BYT_TRIG_POS and BYT_TRIG_NEG bits
  1377. * are used to indicate high and low level triggering
  1378. */
  1379. value &= ~(BYT_DIRECT_IRQ_EN | BYT_TRIG_POS | BYT_TRIG_NEG |
  1380. BYT_TRIG_LVL);
  1381. writel(value, reg);
  1382. if (type & IRQ_TYPE_EDGE_BOTH)
  1383. irq_set_handler_locked(d, handle_edge_irq);
  1384. else if (type & IRQ_TYPE_LEVEL_MASK)
  1385. irq_set_handler_locked(d, handle_level_irq);
  1386. raw_spin_unlock_irqrestore(&vg->lock, flags);
  1387. return 0;
  1388. }
  1389. static struct irq_chip byt_irqchip = {
  1390. .name = "BYT-GPIO",
  1391. .irq_ack = byt_irq_ack,
  1392. .irq_mask = byt_irq_mask,
  1393. .irq_unmask = byt_irq_unmask,
  1394. .irq_set_type = byt_irq_type,
  1395. .flags = IRQCHIP_SKIP_SET_WAKE,
  1396. };
  1397. static void byt_gpio_irq_handler(struct irq_desc *desc)
  1398. {
  1399. struct irq_data *data = irq_desc_get_irq_data(desc);
  1400. struct byt_gpio *vg = gpiochip_get_data(
  1401. irq_desc_get_handler_data(desc));
  1402. struct irq_chip *chip = irq_data_get_irq_chip(data);
  1403. u32 base, pin;
  1404. void __iomem *reg;
  1405. unsigned long pending;
  1406. unsigned int virq;
  1407. /* check from GPIO controller which pin triggered the interrupt */
  1408. for (base = 0; base < vg->chip.ngpio; base += 32) {
  1409. reg = byt_gpio_reg(vg, base, BYT_INT_STAT_REG);
  1410. if (!reg) {
  1411. dev_warn(&vg->pdev->dev,
  1412. "Pin %i: could not retrieve interrupt status register\n",
  1413. base);
  1414. continue;
  1415. }
  1416. raw_spin_lock(&vg->lock);
  1417. pending = readl(reg);
  1418. raw_spin_unlock(&vg->lock);
  1419. for_each_set_bit(pin, &pending, 32) {
  1420. virq = irq_find_mapping(vg->chip.irqdomain, base + pin);
  1421. generic_handle_irq(virq);
  1422. }
  1423. }
  1424. chip->irq_eoi(data);
  1425. }
  1426. static void byt_gpio_irq_init_hw(struct byt_gpio *vg)
  1427. {
  1428. void __iomem *reg;
  1429. u32 base, value;
  1430. int i;
  1431. /*
  1432. * Clear interrupt triggers for all pins that are GPIOs and
  1433. * do not use direct IRQ mode. This will prevent spurious
  1434. * interrupts from misconfigured pins.
  1435. */
  1436. for (i = 0; i < vg->soc_data->npins; i++) {
  1437. unsigned int pin = vg->soc_data->pins[i].number;
  1438. reg = byt_gpio_reg(vg, pin, BYT_CONF0_REG);
  1439. if (!reg) {
  1440. dev_warn(&vg->pdev->dev,
  1441. "Pin %i: could not retrieve conf0 register\n",
  1442. i);
  1443. continue;
  1444. }
  1445. value = readl(reg);
  1446. if ((value & BYT_PIN_MUX) == byt_get_gpio_mux(vg, i) &&
  1447. !(value & BYT_DIRECT_IRQ_EN)) {
  1448. byt_gpio_clear_triggering(vg, i);
  1449. dev_dbg(&vg->pdev->dev, "disabling GPIO %d\n", i);
  1450. }
  1451. }
  1452. /* clear interrupt status trigger registers */
  1453. for (base = 0; base < vg->soc_data->npins; base += 32) {
  1454. reg = byt_gpio_reg(vg, base, BYT_INT_STAT_REG);
  1455. if (!reg) {
  1456. dev_warn(&vg->pdev->dev,
  1457. "Pin %i: could not retrieve irq status reg\n",
  1458. base);
  1459. continue;
  1460. }
  1461. writel(0xffffffff, reg);
  1462. /* make sure trigger bits are cleared, if not then a pin
  1463. might be misconfigured in bios */
  1464. value = readl(reg);
  1465. if (value)
  1466. dev_err(&vg->pdev->dev,
  1467. "GPIO interrupt error, pins misconfigured\n");
  1468. }
  1469. }
  1470. static int byt_gpio_probe(struct byt_gpio *vg)
  1471. {
  1472. struct gpio_chip *gc;
  1473. struct resource *irq_rc;
  1474. int ret;
  1475. /* Set up gpio chip */
  1476. vg->chip = byt_gpio_chip;
  1477. gc = &vg->chip;
  1478. gc->label = dev_name(&vg->pdev->dev);
  1479. gc->base = -1;
  1480. gc->can_sleep = false;
  1481. gc->parent = &vg->pdev->dev;
  1482. gc->ngpio = vg->soc_data->npins;
  1483. #ifdef CONFIG_PM_SLEEP
  1484. vg->saved_context = devm_kcalloc(&vg->pdev->dev, gc->ngpio,
  1485. sizeof(*vg->saved_context), GFP_KERNEL);
  1486. #endif
  1487. ret = gpiochip_add_data(gc, vg);
  1488. if (ret) {
  1489. dev_err(&vg->pdev->dev, "failed adding byt-gpio chip\n");
  1490. return ret;
  1491. }
  1492. ret = gpiochip_add_pin_range(&vg->chip, dev_name(&vg->pdev->dev),
  1493. 0, 0, vg->soc_data->npins);
  1494. if (ret) {
  1495. dev_err(&vg->pdev->dev, "failed to add GPIO pin range\n");
  1496. goto fail;
  1497. }
  1498. /* set up interrupts */
  1499. irq_rc = platform_get_resource(vg->pdev, IORESOURCE_IRQ, 0);
  1500. if (irq_rc && irq_rc->start) {
  1501. byt_gpio_irq_init_hw(vg);
  1502. ret = gpiochip_irqchip_add(gc, &byt_irqchip, 0,
  1503. handle_simple_irq, IRQ_TYPE_NONE);
  1504. if (ret) {
  1505. dev_err(&vg->pdev->dev, "failed to add irqchip\n");
  1506. goto fail;
  1507. }
  1508. gpiochip_set_chained_irqchip(gc, &byt_irqchip,
  1509. (unsigned)irq_rc->start,
  1510. byt_gpio_irq_handler);
  1511. }
  1512. return ret;
  1513. fail:
  1514. gpiochip_remove(&vg->chip);
  1515. return ret;
  1516. }
  1517. static int byt_set_soc_data(struct byt_gpio *vg,
  1518. const struct byt_pinctrl_soc_data *soc_data)
  1519. {
  1520. int i;
  1521. vg->soc_data = soc_data;
  1522. vg->communities_copy = devm_kcalloc(&vg->pdev->dev,
  1523. soc_data->ncommunities,
  1524. sizeof(*vg->communities_copy),
  1525. GFP_KERNEL);
  1526. if (!vg->communities_copy)
  1527. return -ENOMEM;
  1528. for (i = 0; i < soc_data->ncommunities; i++) {
  1529. struct byt_community *comm = vg->communities_copy + i;
  1530. struct resource *mem_rc;
  1531. *comm = vg->soc_data->communities[i];
  1532. mem_rc = platform_get_resource(vg->pdev, IORESOURCE_MEM, 0);
  1533. comm->reg_base = devm_ioremap_resource(&vg->pdev->dev, mem_rc);
  1534. if (IS_ERR(comm->reg_base))
  1535. return PTR_ERR(comm->reg_base);
  1536. }
  1537. return 0;
  1538. }
  1539. static const struct acpi_device_id byt_gpio_acpi_match[] = {
  1540. { "INT33B2", (kernel_ulong_t)byt_soc_data },
  1541. { "INT33FC", (kernel_ulong_t)byt_soc_data },
  1542. { }
  1543. };
  1544. MODULE_DEVICE_TABLE(acpi, byt_gpio_acpi_match);
  1545. static int byt_pinctrl_probe(struct platform_device *pdev)
  1546. {
  1547. const struct byt_pinctrl_soc_data *soc_data = NULL;
  1548. const struct byt_pinctrl_soc_data **soc_table;
  1549. const struct acpi_device_id *acpi_id;
  1550. struct acpi_device *acpi_dev;
  1551. struct byt_gpio *vg;
  1552. int i, ret;
  1553. acpi_dev = ACPI_COMPANION(&pdev->dev);
  1554. if (!acpi_dev)
  1555. return -ENODEV;
  1556. acpi_id = acpi_match_device(byt_gpio_acpi_match, &pdev->dev);
  1557. if (!acpi_id)
  1558. return -ENODEV;
  1559. soc_table = (const struct byt_pinctrl_soc_data **)acpi_id->driver_data;
  1560. for (i = 0; soc_table[i]; i++) {
  1561. if (!strcmp(acpi_dev->pnp.unique_id, soc_table[i]->uid)) {
  1562. soc_data = soc_table[i];
  1563. break;
  1564. }
  1565. }
  1566. if (!soc_data)
  1567. return -ENODEV;
  1568. vg = devm_kzalloc(&pdev->dev, sizeof(*vg), GFP_KERNEL);
  1569. if (!vg)
  1570. return -ENOMEM;
  1571. vg->pdev = pdev;
  1572. ret = byt_set_soc_data(vg, soc_data);
  1573. if (ret) {
  1574. dev_err(&pdev->dev, "failed to set soc data\n");
  1575. return ret;
  1576. }
  1577. vg->pctl_desc = byt_pinctrl_desc;
  1578. vg->pctl_desc.name = dev_name(&pdev->dev);
  1579. vg->pctl_desc.pins = vg->soc_data->pins;
  1580. vg->pctl_desc.npins = vg->soc_data->npins;
  1581. vg->pctl_dev = pinctrl_register(&vg->pctl_desc, &pdev->dev, vg);
  1582. if (IS_ERR(vg->pctl_dev)) {
  1583. dev_err(&pdev->dev, "failed to register pinctrl driver\n");
  1584. return PTR_ERR(vg->pctl_dev);
  1585. }
  1586. raw_spin_lock_init(&vg->lock);
  1587. ret = byt_gpio_probe(vg);
  1588. if (ret) {
  1589. pinctrl_unregister(vg->pctl_dev);
  1590. return ret;
  1591. }
  1592. platform_set_drvdata(pdev, vg);
  1593. pm_runtime_enable(&pdev->dev);
  1594. return 0;
  1595. }
  1596. #ifdef CONFIG_PM_SLEEP
  1597. static int byt_gpio_suspend(struct device *dev)
  1598. {
  1599. struct platform_device *pdev = to_platform_device(dev);
  1600. struct byt_gpio *vg = platform_get_drvdata(pdev);
  1601. int i;
  1602. for (i = 0; i < vg->soc_data->npins; i++) {
  1603. void __iomem *reg;
  1604. u32 value;
  1605. unsigned int pin = vg->soc_data->pins[i].number;
  1606. reg = byt_gpio_reg(vg, pin, BYT_CONF0_REG);
  1607. if (!reg) {
  1608. dev_warn(&vg->pdev->dev,
  1609. "Pin %i: could not retrieve conf0 register\n",
  1610. i);
  1611. continue;
  1612. }
  1613. value = readl(reg) & BYT_CONF0_RESTORE_MASK;
  1614. vg->saved_context[i].conf0 = value;
  1615. reg = byt_gpio_reg(vg, pin, BYT_VAL_REG);
  1616. value = readl(reg) & BYT_VAL_RESTORE_MASK;
  1617. vg->saved_context[i].val = value;
  1618. }
  1619. return 0;
  1620. }
  1621. static int byt_gpio_resume(struct device *dev)
  1622. {
  1623. struct platform_device *pdev = to_platform_device(dev);
  1624. struct byt_gpio *vg = platform_get_drvdata(pdev);
  1625. int i;
  1626. for (i = 0; i < vg->soc_data->npins; i++) {
  1627. void __iomem *reg;
  1628. u32 value;
  1629. unsigned int pin = vg->soc_data->pins[i].number;
  1630. reg = byt_gpio_reg(vg, pin, BYT_CONF0_REG);
  1631. if (!reg) {
  1632. dev_warn(&vg->pdev->dev,
  1633. "Pin %i: could not retrieve conf0 register\n",
  1634. i);
  1635. continue;
  1636. }
  1637. value = readl(reg);
  1638. if ((value & BYT_CONF0_RESTORE_MASK) !=
  1639. vg->saved_context[i].conf0) {
  1640. value &= ~BYT_CONF0_RESTORE_MASK;
  1641. value |= vg->saved_context[i].conf0;
  1642. writel(value, reg);
  1643. dev_info(dev, "restored pin %d conf0 %#08x", i, value);
  1644. }
  1645. reg = byt_gpio_reg(vg, pin, BYT_VAL_REG);
  1646. value = readl(reg);
  1647. if ((value & BYT_VAL_RESTORE_MASK) !=
  1648. vg->saved_context[i].val) {
  1649. u32 v;
  1650. v = value & ~BYT_VAL_RESTORE_MASK;
  1651. v |= vg->saved_context[i].val;
  1652. if (v != value) {
  1653. writel(v, reg);
  1654. dev_dbg(dev, "restored pin %d val %#08x\n",
  1655. i, v);
  1656. }
  1657. }
  1658. }
  1659. return 0;
  1660. }
  1661. #endif
  1662. #ifdef CONFIG_PM
  1663. static int byt_gpio_runtime_suspend(struct device *dev)
  1664. {
  1665. return 0;
  1666. }
  1667. static int byt_gpio_runtime_resume(struct device *dev)
  1668. {
  1669. return 0;
  1670. }
  1671. #endif
  1672. static const struct dev_pm_ops byt_gpio_pm_ops = {
  1673. SET_LATE_SYSTEM_SLEEP_PM_OPS(byt_gpio_suspend, byt_gpio_resume)
  1674. SET_RUNTIME_PM_OPS(byt_gpio_runtime_suspend, byt_gpio_runtime_resume,
  1675. NULL)
  1676. };
  1677. static struct platform_driver byt_gpio_driver = {
  1678. .probe = byt_pinctrl_probe,
  1679. .driver = {
  1680. .name = "byt_gpio",
  1681. .pm = &byt_gpio_pm_ops,
  1682. .suppress_bind_attrs = true,
  1683. .acpi_match_table = ACPI_PTR(byt_gpio_acpi_match),
  1684. },
  1685. };
  1686. static int __init byt_gpio_init(void)
  1687. {
  1688. return platform_driver_register(&byt_gpio_driver);
  1689. }
  1690. subsys_initcall(byt_gpio_init);