pinctrl-cherryview.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cherryview/Braswell pinctrl driver
  4. *
  5. * Copyright (C) 2014, Intel Corporation
  6. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  7. *
  8. * This driver is based on the original Cherryview GPIO driver by
  9. * Ning Li <ning.li@intel.com>
  10. * Alan Cox <alan@linux.intel.com>
  11. */
  12. #include <linux/dmi.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/types.h>
  17. #include <linux/gpio.h>
  18. #include <linux/gpio/driver.h>
  19. #include <linux/acpi.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include <linux/pinctrl/pinconf.h>
  23. #include <linux/pinctrl/pinconf-generic.h>
  24. #include <linux/platform_device.h>
  25. #define CHV_INTSTAT 0x300
  26. #define CHV_INTMASK 0x380
  27. #define FAMILY_PAD_REGS_OFF 0x4400
  28. #define FAMILY_PAD_REGS_SIZE 0x400
  29. #define MAX_FAMILY_PAD_GPIO_NO 15
  30. #define GPIO_REGS_SIZE 8
  31. #define CHV_PADCTRL0 0x000
  32. #define CHV_PADCTRL0_INTSEL_SHIFT 28
  33. #define CHV_PADCTRL0_INTSEL_MASK (0xf << CHV_PADCTRL0_INTSEL_SHIFT)
  34. #define CHV_PADCTRL0_TERM_UP BIT(23)
  35. #define CHV_PADCTRL0_TERM_SHIFT 20
  36. #define CHV_PADCTRL0_TERM_MASK (7 << CHV_PADCTRL0_TERM_SHIFT)
  37. #define CHV_PADCTRL0_TERM_20K 1
  38. #define CHV_PADCTRL0_TERM_5K 2
  39. #define CHV_PADCTRL0_TERM_1K 4
  40. #define CHV_PADCTRL0_PMODE_SHIFT 16
  41. #define CHV_PADCTRL0_PMODE_MASK (0xf << CHV_PADCTRL0_PMODE_SHIFT)
  42. #define CHV_PADCTRL0_GPIOEN BIT(15)
  43. #define CHV_PADCTRL0_GPIOCFG_SHIFT 8
  44. #define CHV_PADCTRL0_GPIOCFG_MASK (7 << CHV_PADCTRL0_GPIOCFG_SHIFT)
  45. #define CHV_PADCTRL0_GPIOCFG_GPIO 0
  46. #define CHV_PADCTRL0_GPIOCFG_GPO 1
  47. #define CHV_PADCTRL0_GPIOCFG_GPI 2
  48. #define CHV_PADCTRL0_GPIOCFG_HIZ 3
  49. #define CHV_PADCTRL0_GPIOTXSTATE BIT(1)
  50. #define CHV_PADCTRL0_GPIORXSTATE BIT(0)
  51. #define CHV_PADCTRL1 0x004
  52. #define CHV_PADCTRL1_CFGLOCK BIT(31)
  53. #define CHV_PADCTRL1_INVRXTX_SHIFT 4
  54. #define CHV_PADCTRL1_INVRXTX_MASK (0xf << CHV_PADCTRL1_INVRXTX_SHIFT)
  55. #define CHV_PADCTRL1_INVRXTX_TXENABLE (2 << CHV_PADCTRL1_INVRXTX_SHIFT)
  56. #define CHV_PADCTRL1_ODEN BIT(3)
  57. #define CHV_PADCTRL1_INVRXTX_RXDATA (4 << CHV_PADCTRL1_INVRXTX_SHIFT)
  58. #define CHV_PADCTRL1_INTWAKECFG_MASK 7
  59. #define CHV_PADCTRL1_INTWAKECFG_FALLING 1
  60. #define CHV_PADCTRL1_INTWAKECFG_RISING 2
  61. #define CHV_PADCTRL1_INTWAKECFG_BOTH 3
  62. #define CHV_PADCTRL1_INTWAKECFG_LEVEL 4
  63. /**
  64. * struct chv_alternate_function - A per group or per pin alternate function
  65. * @pin: Pin number (only used in per pin configs)
  66. * @mode: Mode the pin should be set in
  67. * @invert_oe: Invert OE for this pin
  68. */
  69. struct chv_alternate_function {
  70. unsigned pin;
  71. u8 mode;
  72. bool invert_oe;
  73. };
  74. /**
  75. * struct chv_pincgroup - describes a CHV pin group
  76. * @name: Name of the group
  77. * @pins: An array of pins in this group
  78. * @npins: Number of pins in this group
  79. * @altfunc: Alternate function applied to all pins in this group
  80. * @overrides: Alternate function override per pin or %NULL if not used
  81. * @noverrides: Number of per pin alternate function overrides if
  82. * @overrides != NULL.
  83. */
  84. struct chv_pingroup {
  85. const char *name;
  86. const unsigned *pins;
  87. size_t npins;
  88. struct chv_alternate_function altfunc;
  89. const struct chv_alternate_function *overrides;
  90. size_t noverrides;
  91. };
  92. /**
  93. * struct chv_function - A CHV pinmux function
  94. * @name: Name of the function
  95. * @groups: An array of groups for this function
  96. * @ngroups: Number of groups in @groups
  97. */
  98. struct chv_function {
  99. const char *name;
  100. const char * const *groups;
  101. size_t ngroups;
  102. };
  103. /**
  104. * struct chv_gpio_pinrange - A range of pins that can be used as GPIOs
  105. * @base: Start pin number
  106. * @npins: Number of pins in this range
  107. */
  108. struct chv_gpio_pinrange {
  109. unsigned base;
  110. unsigned npins;
  111. };
  112. /**
  113. * struct chv_community - A community specific configuration
  114. * @uid: ACPI _UID used to match the community
  115. * @pins: All pins in this community
  116. * @npins: Number of pins
  117. * @groups: All groups in this community
  118. * @ngroups: Number of groups
  119. * @functions: All functions in this community
  120. * @nfunctions: Number of functions
  121. * @gpio_ranges: An array of GPIO ranges in this community
  122. * @ngpio_ranges: Number of GPIO ranges
  123. * @nirqs: Total number of IRQs this community can generate
  124. */
  125. struct chv_community {
  126. const char *uid;
  127. const struct pinctrl_pin_desc *pins;
  128. size_t npins;
  129. const struct chv_pingroup *groups;
  130. size_t ngroups;
  131. const struct chv_function *functions;
  132. size_t nfunctions;
  133. const struct chv_gpio_pinrange *gpio_ranges;
  134. size_t ngpio_ranges;
  135. size_t nirqs;
  136. acpi_adr_space_type acpi_space_id;
  137. };
  138. struct chv_pin_context {
  139. u32 padctrl0;
  140. u32 padctrl1;
  141. };
  142. /**
  143. * struct chv_pinctrl - CHV pinctrl private structure
  144. * @dev: Pointer to the parent device
  145. * @pctldesc: Pin controller description
  146. * @pctldev: Pointer to the pin controller device
  147. * @chip: GPIO chip in this pin controller
  148. * @regs: MMIO registers
  149. * @intr_lines: Stores mapping between 16 HW interrupt wires and GPIO
  150. * offset (in GPIO number space)
  151. * @community: Community this pinctrl instance represents
  152. *
  153. * The first group in @groups is expected to contain all pins that can be
  154. * used as GPIOs.
  155. */
  156. struct chv_pinctrl {
  157. struct device *dev;
  158. struct pinctrl_desc pctldesc;
  159. struct pinctrl_dev *pctldev;
  160. struct gpio_chip chip;
  161. void __iomem *regs;
  162. unsigned intr_lines[16];
  163. const struct chv_community *community;
  164. u32 saved_intmask;
  165. struct chv_pin_context *saved_pin_context;
  166. };
  167. #define ALTERNATE_FUNCTION(p, m, i) \
  168. { \
  169. .pin = (p), \
  170. .mode = (m), \
  171. .invert_oe = (i), \
  172. }
  173. #define PIN_GROUP(n, p, m, i) \
  174. { \
  175. .name = (n), \
  176. .pins = (p), \
  177. .npins = ARRAY_SIZE((p)), \
  178. .altfunc.mode = (m), \
  179. .altfunc.invert_oe = (i), \
  180. }
  181. #define PIN_GROUP_WITH_OVERRIDE(n, p, m, i, o) \
  182. { \
  183. .name = (n), \
  184. .pins = (p), \
  185. .npins = ARRAY_SIZE((p)), \
  186. .altfunc.mode = (m), \
  187. .altfunc.invert_oe = (i), \
  188. .overrides = (o), \
  189. .noverrides = ARRAY_SIZE((o)), \
  190. }
  191. #define FUNCTION(n, g) \
  192. { \
  193. .name = (n), \
  194. .groups = (g), \
  195. .ngroups = ARRAY_SIZE((g)), \
  196. }
  197. #define GPIO_PINRANGE(start, end) \
  198. { \
  199. .base = (start), \
  200. .npins = (end) - (start) + 1, \
  201. }
  202. static const struct pinctrl_pin_desc southwest_pins[] = {
  203. PINCTRL_PIN(0, "FST_SPI_D2"),
  204. PINCTRL_PIN(1, "FST_SPI_D0"),
  205. PINCTRL_PIN(2, "FST_SPI_CLK"),
  206. PINCTRL_PIN(3, "FST_SPI_D3"),
  207. PINCTRL_PIN(4, "FST_SPI_CS1_B"),
  208. PINCTRL_PIN(5, "FST_SPI_D1"),
  209. PINCTRL_PIN(6, "FST_SPI_CS0_B"),
  210. PINCTRL_PIN(7, "FST_SPI_CS2_B"),
  211. PINCTRL_PIN(15, "UART1_RTS_B"),
  212. PINCTRL_PIN(16, "UART1_RXD"),
  213. PINCTRL_PIN(17, "UART2_RXD"),
  214. PINCTRL_PIN(18, "UART1_CTS_B"),
  215. PINCTRL_PIN(19, "UART2_RTS_B"),
  216. PINCTRL_PIN(20, "UART1_TXD"),
  217. PINCTRL_PIN(21, "UART2_TXD"),
  218. PINCTRL_PIN(22, "UART2_CTS_B"),
  219. PINCTRL_PIN(30, "MF_HDA_CLK"),
  220. PINCTRL_PIN(31, "MF_HDA_RSTB"),
  221. PINCTRL_PIN(32, "MF_HDA_SDIO"),
  222. PINCTRL_PIN(33, "MF_HDA_SDO"),
  223. PINCTRL_PIN(34, "MF_HDA_DOCKRSTB"),
  224. PINCTRL_PIN(35, "MF_HDA_SYNC"),
  225. PINCTRL_PIN(36, "MF_HDA_SDI1"),
  226. PINCTRL_PIN(37, "MF_HDA_DOCKENB"),
  227. PINCTRL_PIN(45, "I2C5_SDA"),
  228. PINCTRL_PIN(46, "I2C4_SDA"),
  229. PINCTRL_PIN(47, "I2C6_SDA"),
  230. PINCTRL_PIN(48, "I2C5_SCL"),
  231. PINCTRL_PIN(49, "I2C_NFC_SDA"),
  232. PINCTRL_PIN(50, "I2C4_SCL"),
  233. PINCTRL_PIN(51, "I2C6_SCL"),
  234. PINCTRL_PIN(52, "I2C_NFC_SCL"),
  235. PINCTRL_PIN(60, "I2C1_SDA"),
  236. PINCTRL_PIN(61, "I2C0_SDA"),
  237. PINCTRL_PIN(62, "I2C2_SDA"),
  238. PINCTRL_PIN(63, "I2C1_SCL"),
  239. PINCTRL_PIN(64, "I2C3_SDA"),
  240. PINCTRL_PIN(65, "I2C0_SCL"),
  241. PINCTRL_PIN(66, "I2C2_SCL"),
  242. PINCTRL_PIN(67, "I2C3_SCL"),
  243. PINCTRL_PIN(75, "SATA_GP0"),
  244. PINCTRL_PIN(76, "SATA_GP1"),
  245. PINCTRL_PIN(77, "SATA_LEDN"),
  246. PINCTRL_PIN(78, "SATA_GP2"),
  247. PINCTRL_PIN(79, "MF_SMB_ALERTB"),
  248. PINCTRL_PIN(80, "SATA_GP3"),
  249. PINCTRL_PIN(81, "MF_SMB_CLK"),
  250. PINCTRL_PIN(82, "MF_SMB_DATA"),
  251. PINCTRL_PIN(90, "PCIE_CLKREQ0B"),
  252. PINCTRL_PIN(91, "PCIE_CLKREQ1B"),
  253. PINCTRL_PIN(92, "GP_SSP_2_CLK"),
  254. PINCTRL_PIN(93, "PCIE_CLKREQ2B"),
  255. PINCTRL_PIN(94, "GP_SSP_2_RXD"),
  256. PINCTRL_PIN(95, "PCIE_CLKREQ3B"),
  257. PINCTRL_PIN(96, "GP_SSP_2_FS"),
  258. PINCTRL_PIN(97, "GP_SSP_2_TXD"),
  259. };
  260. static const unsigned southwest_fspi_pins[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  261. static const unsigned southwest_uart0_pins[] = { 16, 20 };
  262. static const unsigned southwest_uart1_pins[] = { 15, 16, 18, 20 };
  263. static const unsigned southwest_uart2_pins[] = { 17, 19, 21, 22 };
  264. static const unsigned southwest_i2c0_pins[] = { 61, 65 };
  265. static const unsigned southwest_hda_pins[] = { 30, 31, 32, 33, 34, 35, 36, 37 };
  266. static const unsigned southwest_lpe_pins[] = {
  267. 30, 31, 32, 33, 34, 35, 36, 37, 92, 94, 96, 97,
  268. };
  269. static const unsigned southwest_i2c1_pins[] = { 60, 63 };
  270. static const unsigned southwest_i2c2_pins[] = { 62, 66 };
  271. static const unsigned southwest_i2c3_pins[] = { 64, 67 };
  272. static const unsigned southwest_i2c4_pins[] = { 46, 50 };
  273. static const unsigned southwest_i2c5_pins[] = { 45, 48 };
  274. static const unsigned southwest_i2c6_pins[] = { 47, 51 };
  275. static const unsigned southwest_i2c_nfc_pins[] = { 49, 52 };
  276. static const unsigned southwest_smbus_pins[] = { 79, 81, 82 };
  277. static const unsigned southwest_spi3_pins[] = { 76, 79, 80, 81, 82 };
  278. /* LPE I2S TXD pins need to have invert_oe set */
  279. static const struct chv_alternate_function southwest_lpe_altfuncs[] = {
  280. ALTERNATE_FUNCTION(30, 1, true),
  281. ALTERNATE_FUNCTION(34, 1, true),
  282. ALTERNATE_FUNCTION(97, 1, true),
  283. };
  284. /*
  285. * Two spi3 chipselects are available in different mode than the main spi3
  286. * functionality, which is using mode 1.
  287. */
  288. static const struct chv_alternate_function southwest_spi3_altfuncs[] = {
  289. ALTERNATE_FUNCTION(76, 3, false),
  290. ALTERNATE_FUNCTION(80, 3, false),
  291. };
  292. static const struct chv_pingroup southwest_groups[] = {
  293. PIN_GROUP("uart0_grp", southwest_uart0_pins, 2, false),
  294. PIN_GROUP("uart1_grp", southwest_uart1_pins, 1, false),
  295. PIN_GROUP("uart2_grp", southwest_uart2_pins, 1, false),
  296. PIN_GROUP("hda_grp", southwest_hda_pins, 2, false),
  297. PIN_GROUP("i2c0_grp", southwest_i2c0_pins, 1, true),
  298. PIN_GROUP("i2c1_grp", southwest_i2c1_pins, 1, true),
  299. PIN_GROUP("i2c2_grp", southwest_i2c2_pins, 1, true),
  300. PIN_GROUP("i2c3_grp", southwest_i2c3_pins, 1, true),
  301. PIN_GROUP("i2c4_grp", southwest_i2c4_pins, 1, true),
  302. PIN_GROUP("i2c5_grp", southwest_i2c5_pins, 1, true),
  303. PIN_GROUP("i2c6_grp", southwest_i2c6_pins, 1, true),
  304. PIN_GROUP("i2c_nfc_grp", southwest_i2c_nfc_pins, 2, true),
  305. PIN_GROUP_WITH_OVERRIDE("lpe_grp", southwest_lpe_pins, 1, false,
  306. southwest_lpe_altfuncs),
  307. PIN_GROUP_WITH_OVERRIDE("spi3_grp", southwest_spi3_pins, 2, false,
  308. southwest_spi3_altfuncs),
  309. };
  310. static const char * const southwest_uart0_groups[] = { "uart0_grp" };
  311. static const char * const southwest_uart1_groups[] = { "uart1_grp" };
  312. static const char * const southwest_uart2_groups[] = { "uart2_grp" };
  313. static const char * const southwest_hda_groups[] = { "hda_grp" };
  314. static const char * const southwest_lpe_groups[] = { "lpe_grp" };
  315. static const char * const southwest_i2c0_groups[] = { "i2c0_grp" };
  316. static const char * const southwest_i2c1_groups[] = { "i2c1_grp" };
  317. static const char * const southwest_i2c2_groups[] = { "i2c2_grp" };
  318. static const char * const southwest_i2c3_groups[] = { "i2c3_grp" };
  319. static const char * const southwest_i2c4_groups[] = { "i2c4_grp" };
  320. static const char * const southwest_i2c5_groups[] = { "i2c5_grp" };
  321. static const char * const southwest_i2c6_groups[] = { "i2c6_grp" };
  322. static const char * const southwest_i2c_nfc_groups[] = { "i2c_nfc_grp" };
  323. static const char * const southwest_spi3_groups[] = { "spi3_grp" };
  324. /*
  325. * Only do pinmuxing for certain LPSS devices for now. Rest of the pins are
  326. * enabled only as GPIOs.
  327. */
  328. static const struct chv_function southwest_functions[] = {
  329. FUNCTION("uart0", southwest_uart0_groups),
  330. FUNCTION("uart1", southwest_uart1_groups),
  331. FUNCTION("uart2", southwest_uart2_groups),
  332. FUNCTION("hda", southwest_hda_groups),
  333. FUNCTION("lpe", southwest_lpe_groups),
  334. FUNCTION("i2c0", southwest_i2c0_groups),
  335. FUNCTION("i2c1", southwest_i2c1_groups),
  336. FUNCTION("i2c2", southwest_i2c2_groups),
  337. FUNCTION("i2c3", southwest_i2c3_groups),
  338. FUNCTION("i2c4", southwest_i2c4_groups),
  339. FUNCTION("i2c5", southwest_i2c5_groups),
  340. FUNCTION("i2c6", southwest_i2c6_groups),
  341. FUNCTION("i2c_nfc", southwest_i2c_nfc_groups),
  342. FUNCTION("spi3", southwest_spi3_groups),
  343. };
  344. static const struct chv_gpio_pinrange southwest_gpio_ranges[] = {
  345. GPIO_PINRANGE(0, 7),
  346. GPIO_PINRANGE(15, 22),
  347. GPIO_PINRANGE(30, 37),
  348. GPIO_PINRANGE(45, 52),
  349. GPIO_PINRANGE(60, 67),
  350. GPIO_PINRANGE(75, 82),
  351. GPIO_PINRANGE(90, 97),
  352. };
  353. static const struct chv_community southwest_community = {
  354. .uid = "1",
  355. .pins = southwest_pins,
  356. .npins = ARRAY_SIZE(southwest_pins),
  357. .groups = southwest_groups,
  358. .ngroups = ARRAY_SIZE(southwest_groups),
  359. .functions = southwest_functions,
  360. .nfunctions = ARRAY_SIZE(southwest_functions),
  361. .gpio_ranges = southwest_gpio_ranges,
  362. .ngpio_ranges = ARRAY_SIZE(southwest_gpio_ranges),
  363. /*
  364. * Southwest community can benerate GPIO interrupts only for the
  365. * first 8 interrupts. The upper half (8-15) can only be used to
  366. * trigger GPEs.
  367. */
  368. .nirqs = 8,
  369. .acpi_space_id = 0x91,
  370. };
  371. static const struct pinctrl_pin_desc north_pins[] = {
  372. PINCTRL_PIN(0, "GPIO_DFX_0"),
  373. PINCTRL_PIN(1, "GPIO_DFX_3"),
  374. PINCTRL_PIN(2, "GPIO_DFX_7"),
  375. PINCTRL_PIN(3, "GPIO_DFX_1"),
  376. PINCTRL_PIN(4, "GPIO_DFX_5"),
  377. PINCTRL_PIN(5, "GPIO_DFX_4"),
  378. PINCTRL_PIN(6, "GPIO_DFX_8"),
  379. PINCTRL_PIN(7, "GPIO_DFX_2"),
  380. PINCTRL_PIN(8, "GPIO_DFX_6"),
  381. PINCTRL_PIN(15, "GPIO_SUS0"),
  382. PINCTRL_PIN(16, "SEC_GPIO_SUS10"),
  383. PINCTRL_PIN(17, "GPIO_SUS3"),
  384. PINCTRL_PIN(18, "GPIO_SUS7"),
  385. PINCTRL_PIN(19, "GPIO_SUS1"),
  386. PINCTRL_PIN(20, "GPIO_SUS5"),
  387. PINCTRL_PIN(21, "SEC_GPIO_SUS11"),
  388. PINCTRL_PIN(22, "GPIO_SUS4"),
  389. PINCTRL_PIN(23, "SEC_GPIO_SUS8"),
  390. PINCTRL_PIN(24, "GPIO_SUS2"),
  391. PINCTRL_PIN(25, "GPIO_SUS6"),
  392. PINCTRL_PIN(26, "CX_PREQ_B"),
  393. PINCTRL_PIN(27, "SEC_GPIO_SUS9"),
  394. PINCTRL_PIN(30, "TRST_B"),
  395. PINCTRL_PIN(31, "TCK"),
  396. PINCTRL_PIN(32, "PROCHOT_B"),
  397. PINCTRL_PIN(33, "SVIDO_DATA"),
  398. PINCTRL_PIN(34, "TMS"),
  399. PINCTRL_PIN(35, "CX_PRDY_B_2"),
  400. PINCTRL_PIN(36, "TDO_2"),
  401. PINCTRL_PIN(37, "CX_PRDY_B"),
  402. PINCTRL_PIN(38, "SVIDO_ALERT_B"),
  403. PINCTRL_PIN(39, "TDO"),
  404. PINCTRL_PIN(40, "SVIDO_CLK"),
  405. PINCTRL_PIN(41, "TDI"),
  406. PINCTRL_PIN(45, "GP_CAMERASB_05"),
  407. PINCTRL_PIN(46, "GP_CAMERASB_02"),
  408. PINCTRL_PIN(47, "GP_CAMERASB_08"),
  409. PINCTRL_PIN(48, "GP_CAMERASB_00"),
  410. PINCTRL_PIN(49, "GP_CAMERASB_06"),
  411. PINCTRL_PIN(50, "GP_CAMERASB_10"),
  412. PINCTRL_PIN(51, "GP_CAMERASB_03"),
  413. PINCTRL_PIN(52, "GP_CAMERASB_09"),
  414. PINCTRL_PIN(53, "GP_CAMERASB_01"),
  415. PINCTRL_PIN(54, "GP_CAMERASB_07"),
  416. PINCTRL_PIN(55, "GP_CAMERASB_11"),
  417. PINCTRL_PIN(56, "GP_CAMERASB_04"),
  418. PINCTRL_PIN(60, "PANEL0_BKLTEN"),
  419. PINCTRL_PIN(61, "HV_DDI0_HPD"),
  420. PINCTRL_PIN(62, "HV_DDI2_DDC_SDA"),
  421. PINCTRL_PIN(63, "PANEL1_BKLTCTL"),
  422. PINCTRL_PIN(64, "HV_DDI1_HPD"),
  423. PINCTRL_PIN(65, "PANEL0_BKLTCTL"),
  424. PINCTRL_PIN(66, "HV_DDI0_DDC_SDA"),
  425. PINCTRL_PIN(67, "HV_DDI2_DDC_SCL"),
  426. PINCTRL_PIN(68, "HV_DDI2_HPD"),
  427. PINCTRL_PIN(69, "PANEL1_VDDEN"),
  428. PINCTRL_PIN(70, "PANEL1_BKLTEN"),
  429. PINCTRL_PIN(71, "HV_DDI0_DDC_SCL"),
  430. PINCTRL_PIN(72, "PANEL0_VDDEN"),
  431. };
  432. static const struct chv_gpio_pinrange north_gpio_ranges[] = {
  433. GPIO_PINRANGE(0, 8),
  434. GPIO_PINRANGE(15, 27),
  435. GPIO_PINRANGE(30, 41),
  436. GPIO_PINRANGE(45, 56),
  437. GPIO_PINRANGE(60, 72),
  438. };
  439. static const struct chv_community north_community = {
  440. .uid = "2",
  441. .pins = north_pins,
  442. .npins = ARRAY_SIZE(north_pins),
  443. .gpio_ranges = north_gpio_ranges,
  444. .ngpio_ranges = ARRAY_SIZE(north_gpio_ranges),
  445. /*
  446. * North community can generate GPIO interrupts only for the first
  447. * 8 interrupts. The upper half (8-15) can only be used to trigger
  448. * GPEs.
  449. */
  450. .nirqs = 8,
  451. .acpi_space_id = 0x92,
  452. };
  453. static const struct pinctrl_pin_desc east_pins[] = {
  454. PINCTRL_PIN(0, "PMU_SLP_S3_B"),
  455. PINCTRL_PIN(1, "PMU_BATLOW_B"),
  456. PINCTRL_PIN(2, "SUS_STAT_B"),
  457. PINCTRL_PIN(3, "PMU_SLP_S0IX_B"),
  458. PINCTRL_PIN(4, "PMU_AC_PRESENT"),
  459. PINCTRL_PIN(5, "PMU_PLTRST_B"),
  460. PINCTRL_PIN(6, "PMU_SUSCLK"),
  461. PINCTRL_PIN(7, "PMU_SLP_LAN_B"),
  462. PINCTRL_PIN(8, "PMU_PWRBTN_B"),
  463. PINCTRL_PIN(9, "PMU_SLP_S4_B"),
  464. PINCTRL_PIN(10, "PMU_WAKE_B"),
  465. PINCTRL_PIN(11, "PMU_WAKE_LAN_B"),
  466. PINCTRL_PIN(15, "MF_ISH_GPIO_3"),
  467. PINCTRL_PIN(16, "MF_ISH_GPIO_7"),
  468. PINCTRL_PIN(17, "MF_ISH_I2C1_SCL"),
  469. PINCTRL_PIN(18, "MF_ISH_GPIO_1"),
  470. PINCTRL_PIN(19, "MF_ISH_GPIO_5"),
  471. PINCTRL_PIN(20, "MF_ISH_GPIO_9"),
  472. PINCTRL_PIN(21, "MF_ISH_GPIO_0"),
  473. PINCTRL_PIN(22, "MF_ISH_GPIO_4"),
  474. PINCTRL_PIN(23, "MF_ISH_GPIO_8"),
  475. PINCTRL_PIN(24, "MF_ISH_GPIO_2"),
  476. PINCTRL_PIN(25, "MF_ISH_GPIO_6"),
  477. PINCTRL_PIN(26, "MF_ISH_I2C1_SDA"),
  478. };
  479. static const struct chv_gpio_pinrange east_gpio_ranges[] = {
  480. GPIO_PINRANGE(0, 11),
  481. GPIO_PINRANGE(15, 26),
  482. };
  483. static const struct chv_community east_community = {
  484. .uid = "3",
  485. .pins = east_pins,
  486. .npins = ARRAY_SIZE(east_pins),
  487. .gpio_ranges = east_gpio_ranges,
  488. .ngpio_ranges = ARRAY_SIZE(east_gpio_ranges),
  489. .nirqs = 16,
  490. .acpi_space_id = 0x93,
  491. };
  492. static const struct pinctrl_pin_desc southeast_pins[] = {
  493. PINCTRL_PIN(0, "MF_PLT_CLK0"),
  494. PINCTRL_PIN(1, "PWM1"),
  495. PINCTRL_PIN(2, "MF_PLT_CLK1"),
  496. PINCTRL_PIN(3, "MF_PLT_CLK4"),
  497. PINCTRL_PIN(4, "MF_PLT_CLK3"),
  498. PINCTRL_PIN(5, "PWM0"),
  499. PINCTRL_PIN(6, "MF_PLT_CLK5"),
  500. PINCTRL_PIN(7, "MF_PLT_CLK2"),
  501. PINCTRL_PIN(15, "SDMMC2_D3_CD_B"),
  502. PINCTRL_PIN(16, "SDMMC1_CLK"),
  503. PINCTRL_PIN(17, "SDMMC1_D0"),
  504. PINCTRL_PIN(18, "SDMMC2_D1"),
  505. PINCTRL_PIN(19, "SDMMC2_CLK"),
  506. PINCTRL_PIN(20, "SDMMC1_D2"),
  507. PINCTRL_PIN(21, "SDMMC2_D2"),
  508. PINCTRL_PIN(22, "SDMMC2_CMD"),
  509. PINCTRL_PIN(23, "SDMMC1_CMD"),
  510. PINCTRL_PIN(24, "SDMMC1_D1"),
  511. PINCTRL_PIN(25, "SDMMC2_D0"),
  512. PINCTRL_PIN(26, "SDMMC1_D3_CD_B"),
  513. PINCTRL_PIN(30, "SDMMC3_D1"),
  514. PINCTRL_PIN(31, "SDMMC3_CLK"),
  515. PINCTRL_PIN(32, "SDMMC3_D3"),
  516. PINCTRL_PIN(33, "SDMMC3_D2"),
  517. PINCTRL_PIN(34, "SDMMC3_CMD"),
  518. PINCTRL_PIN(35, "SDMMC3_D0"),
  519. PINCTRL_PIN(45, "MF_LPC_AD2"),
  520. PINCTRL_PIN(46, "LPC_CLKRUNB"),
  521. PINCTRL_PIN(47, "MF_LPC_AD0"),
  522. PINCTRL_PIN(48, "LPC_FRAMEB"),
  523. PINCTRL_PIN(49, "MF_LPC_CLKOUT1"),
  524. PINCTRL_PIN(50, "MF_LPC_AD3"),
  525. PINCTRL_PIN(51, "MF_LPC_CLKOUT0"),
  526. PINCTRL_PIN(52, "MF_LPC_AD1"),
  527. PINCTRL_PIN(60, "SPI1_MISO"),
  528. PINCTRL_PIN(61, "SPI1_CSO_B"),
  529. PINCTRL_PIN(62, "SPI1_CLK"),
  530. PINCTRL_PIN(63, "MMC1_D6"),
  531. PINCTRL_PIN(64, "SPI1_MOSI"),
  532. PINCTRL_PIN(65, "MMC1_D5"),
  533. PINCTRL_PIN(66, "SPI1_CS1_B"),
  534. PINCTRL_PIN(67, "MMC1_D4_SD_WE"),
  535. PINCTRL_PIN(68, "MMC1_D7"),
  536. PINCTRL_PIN(69, "MMC1_RCLK"),
  537. PINCTRL_PIN(75, "USB_OC1_B"),
  538. PINCTRL_PIN(76, "PMU_RESETBUTTON_B"),
  539. PINCTRL_PIN(77, "GPIO_ALERT"),
  540. PINCTRL_PIN(78, "SDMMC3_PWR_EN_B"),
  541. PINCTRL_PIN(79, "ILB_SERIRQ"),
  542. PINCTRL_PIN(80, "USB_OC0_B"),
  543. PINCTRL_PIN(81, "SDMMC3_CD_B"),
  544. PINCTRL_PIN(82, "SPKR"),
  545. PINCTRL_PIN(83, "SUSPWRDNACK"),
  546. PINCTRL_PIN(84, "SPARE_PIN"),
  547. PINCTRL_PIN(85, "SDMMC3_1P8_EN"),
  548. };
  549. static const unsigned southeast_pwm0_pins[] = { 5 };
  550. static const unsigned southeast_pwm1_pins[] = { 1 };
  551. static const unsigned southeast_sdmmc1_pins[] = {
  552. 16, 17, 20, 23, 24, 26, 63, 65, 67, 68, 69,
  553. };
  554. static const unsigned southeast_sdmmc2_pins[] = { 15, 18, 19, 21, 22, 25 };
  555. static const unsigned southeast_sdmmc3_pins[] = {
  556. 30, 31, 32, 33, 34, 35, 78, 81, 85,
  557. };
  558. static const unsigned southeast_spi1_pins[] = { 60, 61, 62, 64, 66 };
  559. static const unsigned southeast_spi2_pins[] = { 2, 3, 4, 6, 7 };
  560. static const struct chv_pingroup southeast_groups[] = {
  561. PIN_GROUP("pwm0_grp", southeast_pwm0_pins, 1, false),
  562. PIN_GROUP("pwm1_grp", southeast_pwm1_pins, 1, false),
  563. PIN_GROUP("sdmmc1_grp", southeast_sdmmc1_pins, 1, false),
  564. PIN_GROUP("sdmmc2_grp", southeast_sdmmc2_pins, 1, false),
  565. PIN_GROUP("sdmmc3_grp", southeast_sdmmc3_pins, 1, false),
  566. PIN_GROUP("spi1_grp", southeast_spi1_pins, 1, false),
  567. PIN_GROUP("spi2_grp", southeast_spi2_pins, 4, false),
  568. };
  569. static const char * const southeast_pwm0_groups[] = { "pwm0_grp" };
  570. static const char * const southeast_pwm1_groups[] = { "pwm1_grp" };
  571. static const char * const southeast_sdmmc1_groups[] = { "sdmmc1_grp" };
  572. static const char * const southeast_sdmmc2_groups[] = { "sdmmc2_grp" };
  573. static const char * const southeast_sdmmc3_groups[] = { "sdmmc3_grp" };
  574. static const char * const southeast_spi1_groups[] = { "spi1_grp" };
  575. static const char * const southeast_spi2_groups[] = { "spi2_grp" };
  576. static const struct chv_function southeast_functions[] = {
  577. FUNCTION("pwm0", southeast_pwm0_groups),
  578. FUNCTION("pwm1", southeast_pwm1_groups),
  579. FUNCTION("sdmmc1", southeast_sdmmc1_groups),
  580. FUNCTION("sdmmc2", southeast_sdmmc2_groups),
  581. FUNCTION("sdmmc3", southeast_sdmmc3_groups),
  582. FUNCTION("spi1", southeast_spi1_groups),
  583. FUNCTION("spi2", southeast_spi2_groups),
  584. };
  585. static const struct chv_gpio_pinrange southeast_gpio_ranges[] = {
  586. GPIO_PINRANGE(0, 7),
  587. GPIO_PINRANGE(15, 26),
  588. GPIO_PINRANGE(30, 35),
  589. GPIO_PINRANGE(45, 52),
  590. GPIO_PINRANGE(60, 69),
  591. GPIO_PINRANGE(75, 85),
  592. };
  593. static const struct chv_community southeast_community = {
  594. .uid = "4",
  595. .pins = southeast_pins,
  596. .npins = ARRAY_SIZE(southeast_pins),
  597. .groups = southeast_groups,
  598. .ngroups = ARRAY_SIZE(southeast_groups),
  599. .functions = southeast_functions,
  600. .nfunctions = ARRAY_SIZE(southeast_functions),
  601. .gpio_ranges = southeast_gpio_ranges,
  602. .ngpio_ranges = ARRAY_SIZE(southeast_gpio_ranges),
  603. .nirqs = 16,
  604. .acpi_space_id = 0x94,
  605. };
  606. static const struct chv_community *chv_communities[] = {
  607. &southwest_community,
  608. &north_community,
  609. &east_community,
  610. &southeast_community,
  611. };
  612. /*
  613. * Lock to serialize register accesses
  614. *
  615. * Due to a silicon issue, a shared lock must be used to prevent
  616. * concurrent accesses across the 4 GPIO controllers.
  617. *
  618. * See Intel Atom Z8000 Processor Series Specification Update (Rev. 005),
  619. * errata #CHT34, for further information.
  620. */
  621. static DEFINE_RAW_SPINLOCK(chv_lock);
  622. static void __iomem *chv_padreg(struct chv_pinctrl *pctrl, unsigned offset,
  623. unsigned reg)
  624. {
  625. unsigned family_no = offset / MAX_FAMILY_PAD_GPIO_NO;
  626. unsigned pad_no = offset % MAX_FAMILY_PAD_GPIO_NO;
  627. offset = FAMILY_PAD_REGS_OFF + FAMILY_PAD_REGS_SIZE * family_no +
  628. GPIO_REGS_SIZE * pad_no;
  629. return pctrl->regs + offset + reg;
  630. }
  631. static void chv_writel(u32 value, void __iomem *reg)
  632. {
  633. writel(value, reg);
  634. /* simple readback to confirm the bus transferring done */
  635. readl(reg);
  636. }
  637. /* When Pad Cfg is locked, driver can only change GPIOTXState or GPIORXState */
  638. static bool chv_pad_locked(struct chv_pinctrl *pctrl, unsigned offset)
  639. {
  640. void __iomem *reg;
  641. reg = chv_padreg(pctrl, offset, CHV_PADCTRL1);
  642. return readl(reg) & CHV_PADCTRL1_CFGLOCK;
  643. }
  644. static int chv_get_groups_count(struct pinctrl_dev *pctldev)
  645. {
  646. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  647. return pctrl->community->ngroups;
  648. }
  649. static const char *chv_get_group_name(struct pinctrl_dev *pctldev,
  650. unsigned group)
  651. {
  652. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  653. return pctrl->community->groups[group].name;
  654. }
  655. static int chv_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
  656. const unsigned **pins, unsigned *npins)
  657. {
  658. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  659. *pins = pctrl->community->groups[group].pins;
  660. *npins = pctrl->community->groups[group].npins;
  661. return 0;
  662. }
  663. static void chv_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  664. unsigned offset)
  665. {
  666. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  667. unsigned long flags;
  668. u32 ctrl0, ctrl1;
  669. bool locked;
  670. raw_spin_lock_irqsave(&chv_lock, flags);
  671. ctrl0 = readl(chv_padreg(pctrl, offset, CHV_PADCTRL0));
  672. ctrl1 = readl(chv_padreg(pctrl, offset, CHV_PADCTRL1));
  673. locked = chv_pad_locked(pctrl, offset);
  674. raw_spin_unlock_irqrestore(&chv_lock, flags);
  675. if (ctrl0 & CHV_PADCTRL0_GPIOEN) {
  676. seq_puts(s, "GPIO ");
  677. } else {
  678. u32 mode;
  679. mode = ctrl0 & CHV_PADCTRL0_PMODE_MASK;
  680. mode >>= CHV_PADCTRL0_PMODE_SHIFT;
  681. seq_printf(s, "mode %d ", mode);
  682. }
  683. seq_printf(s, "0x%08x 0x%08x", ctrl0, ctrl1);
  684. if (locked)
  685. seq_puts(s, " [LOCKED]");
  686. }
  687. static const struct pinctrl_ops chv_pinctrl_ops = {
  688. .get_groups_count = chv_get_groups_count,
  689. .get_group_name = chv_get_group_name,
  690. .get_group_pins = chv_get_group_pins,
  691. .pin_dbg_show = chv_pin_dbg_show,
  692. };
  693. static int chv_get_functions_count(struct pinctrl_dev *pctldev)
  694. {
  695. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  696. return pctrl->community->nfunctions;
  697. }
  698. static const char *chv_get_function_name(struct pinctrl_dev *pctldev,
  699. unsigned function)
  700. {
  701. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  702. return pctrl->community->functions[function].name;
  703. }
  704. static int chv_get_function_groups(struct pinctrl_dev *pctldev,
  705. unsigned function,
  706. const char * const **groups,
  707. unsigned * const ngroups)
  708. {
  709. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  710. *groups = pctrl->community->functions[function].groups;
  711. *ngroups = pctrl->community->functions[function].ngroups;
  712. return 0;
  713. }
  714. static int chv_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
  715. unsigned group)
  716. {
  717. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  718. const struct chv_pingroup *grp;
  719. unsigned long flags;
  720. int i;
  721. grp = &pctrl->community->groups[group];
  722. raw_spin_lock_irqsave(&chv_lock, flags);
  723. /* Check first that the pad is not locked */
  724. for (i = 0; i < grp->npins; i++) {
  725. if (chv_pad_locked(pctrl, grp->pins[i])) {
  726. dev_warn(pctrl->dev, "unable to set mode for locked pin %u\n",
  727. grp->pins[i]);
  728. raw_spin_unlock_irqrestore(&chv_lock, flags);
  729. return -EBUSY;
  730. }
  731. }
  732. for (i = 0; i < grp->npins; i++) {
  733. const struct chv_alternate_function *altfunc = &grp->altfunc;
  734. int pin = grp->pins[i];
  735. void __iomem *reg;
  736. u32 value;
  737. /* Check if there is pin-specific config */
  738. if (grp->overrides) {
  739. int j;
  740. for (j = 0; j < grp->noverrides; j++) {
  741. if (grp->overrides[j].pin == pin) {
  742. altfunc = &grp->overrides[j];
  743. break;
  744. }
  745. }
  746. }
  747. reg = chv_padreg(pctrl, pin, CHV_PADCTRL0);
  748. value = readl(reg);
  749. /* Disable GPIO mode */
  750. value &= ~CHV_PADCTRL0_GPIOEN;
  751. /* Set to desired mode */
  752. value &= ~CHV_PADCTRL0_PMODE_MASK;
  753. value |= altfunc->mode << CHV_PADCTRL0_PMODE_SHIFT;
  754. chv_writel(value, reg);
  755. /* Update for invert_oe */
  756. reg = chv_padreg(pctrl, pin, CHV_PADCTRL1);
  757. value = readl(reg) & ~CHV_PADCTRL1_INVRXTX_MASK;
  758. if (altfunc->invert_oe)
  759. value |= CHV_PADCTRL1_INVRXTX_TXENABLE;
  760. chv_writel(value, reg);
  761. dev_dbg(pctrl->dev, "configured pin %u mode %u OE %sinverted\n",
  762. pin, altfunc->mode, altfunc->invert_oe ? "" : "not ");
  763. }
  764. raw_spin_unlock_irqrestore(&chv_lock, flags);
  765. return 0;
  766. }
  767. static int chv_gpio_request_enable(struct pinctrl_dev *pctldev,
  768. struct pinctrl_gpio_range *range,
  769. unsigned offset)
  770. {
  771. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  772. unsigned long flags;
  773. void __iomem *reg;
  774. u32 value;
  775. raw_spin_lock_irqsave(&chv_lock, flags);
  776. if (chv_pad_locked(pctrl, offset)) {
  777. value = readl(chv_padreg(pctrl, offset, CHV_PADCTRL0));
  778. if (!(value & CHV_PADCTRL0_GPIOEN)) {
  779. /* Locked so cannot enable */
  780. raw_spin_unlock_irqrestore(&chv_lock, flags);
  781. return -EBUSY;
  782. }
  783. } else {
  784. int i;
  785. /* Reset the interrupt mapping */
  786. for (i = 0; i < ARRAY_SIZE(pctrl->intr_lines); i++) {
  787. if (pctrl->intr_lines[i] == offset) {
  788. pctrl->intr_lines[i] = 0;
  789. break;
  790. }
  791. }
  792. /* Disable interrupt generation */
  793. reg = chv_padreg(pctrl, offset, CHV_PADCTRL1);
  794. value = readl(reg);
  795. value &= ~CHV_PADCTRL1_INTWAKECFG_MASK;
  796. value &= ~CHV_PADCTRL1_INVRXTX_MASK;
  797. chv_writel(value, reg);
  798. reg = chv_padreg(pctrl, offset, CHV_PADCTRL0);
  799. value = readl(reg);
  800. /*
  801. * If the pin is in HiZ mode (both TX and RX buffers are
  802. * disabled) we turn it to be input now.
  803. */
  804. if ((value & CHV_PADCTRL0_GPIOCFG_MASK) ==
  805. (CHV_PADCTRL0_GPIOCFG_HIZ << CHV_PADCTRL0_GPIOCFG_SHIFT)) {
  806. value &= ~CHV_PADCTRL0_GPIOCFG_MASK;
  807. value |= CHV_PADCTRL0_GPIOCFG_GPI <<
  808. CHV_PADCTRL0_GPIOCFG_SHIFT;
  809. }
  810. /* Switch to a GPIO mode */
  811. value |= CHV_PADCTRL0_GPIOEN;
  812. chv_writel(value, reg);
  813. }
  814. raw_spin_unlock_irqrestore(&chv_lock, flags);
  815. return 0;
  816. }
  817. static void chv_gpio_disable_free(struct pinctrl_dev *pctldev,
  818. struct pinctrl_gpio_range *range,
  819. unsigned offset)
  820. {
  821. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  822. unsigned long flags;
  823. void __iomem *reg;
  824. u32 value;
  825. raw_spin_lock_irqsave(&chv_lock, flags);
  826. reg = chv_padreg(pctrl, offset, CHV_PADCTRL0);
  827. value = readl(reg) & ~CHV_PADCTRL0_GPIOEN;
  828. chv_writel(value, reg);
  829. raw_spin_unlock_irqrestore(&chv_lock, flags);
  830. }
  831. static int chv_gpio_set_direction(struct pinctrl_dev *pctldev,
  832. struct pinctrl_gpio_range *range,
  833. unsigned offset, bool input)
  834. {
  835. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  836. void __iomem *reg = chv_padreg(pctrl, offset, CHV_PADCTRL0);
  837. unsigned long flags;
  838. u32 ctrl0;
  839. raw_spin_lock_irqsave(&chv_lock, flags);
  840. ctrl0 = readl(reg) & ~CHV_PADCTRL0_GPIOCFG_MASK;
  841. if (input)
  842. ctrl0 |= CHV_PADCTRL0_GPIOCFG_GPI << CHV_PADCTRL0_GPIOCFG_SHIFT;
  843. else
  844. ctrl0 |= CHV_PADCTRL0_GPIOCFG_GPO << CHV_PADCTRL0_GPIOCFG_SHIFT;
  845. chv_writel(ctrl0, reg);
  846. raw_spin_unlock_irqrestore(&chv_lock, flags);
  847. return 0;
  848. }
  849. static const struct pinmux_ops chv_pinmux_ops = {
  850. .get_functions_count = chv_get_functions_count,
  851. .get_function_name = chv_get_function_name,
  852. .get_function_groups = chv_get_function_groups,
  853. .set_mux = chv_pinmux_set_mux,
  854. .gpio_request_enable = chv_gpio_request_enable,
  855. .gpio_disable_free = chv_gpio_disable_free,
  856. .gpio_set_direction = chv_gpio_set_direction,
  857. };
  858. static int chv_config_get(struct pinctrl_dev *pctldev, unsigned pin,
  859. unsigned long *config)
  860. {
  861. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  862. enum pin_config_param param = pinconf_to_config_param(*config);
  863. unsigned long flags;
  864. u32 ctrl0, ctrl1;
  865. u16 arg = 0;
  866. u32 term;
  867. raw_spin_lock_irqsave(&chv_lock, flags);
  868. ctrl0 = readl(chv_padreg(pctrl, pin, CHV_PADCTRL0));
  869. ctrl1 = readl(chv_padreg(pctrl, pin, CHV_PADCTRL1));
  870. raw_spin_unlock_irqrestore(&chv_lock, flags);
  871. term = (ctrl0 & CHV_PADCTRL0_TERM_MASK) >> CHV_PADCTRL0_TERM_SHIFT;
  872. switch (param) {
  873. case PIN_CONFIG_BIAS_DISABLE:
  874. if (term)
  875. return -EINVAL;
  876. break;
  877. case PIN_CONFIG_BIAS_PULL_UP:
  878. if (!(ctrl0 & CHV_PADCTRL0_TERM_UP))
  879. return -EINVAL;
  880. switch (term) {
  881. case CHV_PADCTRL0_TERM_20K:
  882. arg = 20000;
  883. break;
  884. case CHV_PADCTRL0_TERM_5K:
  885. arg = 5000;
  886. break;
  887. case CHV_PADCTRL0_TERM_1K:
  888. arg = 1000;
  889. break;
  890. }
  891. break;
  892. case PIN_CONFIG_BIAS_PULL_DOWN:
  893. if (!term || (ctrl0 & CHV_PADCTRL0_TERM_UP))
  894. return -EINVAL;
  895. switch (term) {
  896. case CHV_PADCTRL0_TERM_20K:
  897. arg = 20000;
  898. break;
  899. case CHV_PADCTRL0_TERM_5K:
  900. arg = 5000;
  901. break;
  902. }
  903. break;
  904. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  905. if (!(ctrl1 & CHV_PADCTRL1_ODEN))
  906. return -EINVAL;
  907. break;
  908. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE: {
  909. u32 cfg;
  910. cfg = ctrl0 & CHV_PADCTRL0_GPIOCFG_MASK;
  911. cfg >>= CHV_PADCTRL0_GPIOCFG_SHIFT;
  912. if (cfg != CHV_PADCTRL0_GPIOCFG_HIZ)
  913. return -EINVAL;
  914. break;
  915. }
  916. default:
  917. return -ENOTSUPP;
  918. }
  919. *config = pinconf_to_config_packed(param, arg);
  920. return 0;
  921. }
  922. static int chv_config_set_pull(struct chv_pinctrl *pctrl, unsigned pin,
  923. enum pin_config_param param, u32 arg)
  924. {
  925. void __iomem *reg = chv_padreg(pctrl, pin, CHV_PADCTRL0);
  926. unsigned long flags;
  927. u32 ctrl0, pull;
  928. raw_spin_lock_irqsave(&chv_lock, flags);
  929. ctrl0 = readl(reg);
  930. switch (param) {
  931. case PIN_CONFIG_BIAS_DISABLE:
  932. ctrl0 &= ~(CHV_PADCTRL0_TERM_MASK | CHV_PADCTRL0_TERM_UP);
  933. break;
  934. case PIN_CONFIG_BIAS_PULL_UP:
  935. ctrl0 &= ~(CHV_PADCTRL0_TERM_MASK | CHV_PADCTRL0_TERM_UP);
  936. switch (arg) {
  937. case 1000:
  938. /* For 1k there is only pull up */
  939. pull = CHV_PADCTRL0_TERM_1K << CHV_PADCTRL0_TERM_SHIFT;
  940. break;
  941. case 5000:
  942. pull = CHV_PADCTRL0_TERM_5K << CHV_PADCTRL0_TERM_SHIFT;
  943. break;
  944. case 20000:
  945. pull = CHV_PADCTRL0_TERM_20K << CHV_PADCTRL0_TERM_SHIFT;
  946. break;
  947. default:
  948. raw_spin_unlock_irqrestore(&chv_lock, flags);
  949. return -EINVAL;
  950. }
  951. ctrl0 |= CHV_PADCTRL0_TERM_UP | pull;
  952. break;
  953. case PIN_CONFIG_BIAS_PULL_DOWN:
  954. ctrl0 &= ~(CHV_PADCTRL0_TERM_MASK | CHV_PADCTRL0_TERM_UP);
  955. switch (arg) {
  956. case 5000:
  957. pull = CHV_PADCTRL0_TERM_5K << CHV_PADCTRL0_TERM_SHIFT;
  958. break;
  959. case 20000:
  960. pull = CHV_PADCTRL0_TERM_20K << CHV_PADCTRL0_TERM_SHIFT;
  961. break;
  962. default:
  963. raw_spin_unlock_irqrestore(&chv_lock, flags);
  964. return -EINVAL;
  965. }
  966. ctrl0 |= pull;
  967. break;
  968. default:
  969. raw_spin_unlock_irqrestore(&chv_lock, flags);
  970. return -EINVAL;
  971. }
  972. chv_writel(ctrl0, reg);
  973. raw_spin_unlock_irqrestore(&chv_lock, flags);
  974. return 0;
  975. }
  976. static int chv_config_set_oden(struct chv_pinctrl *pctrl, unsigned int pin,
  977. bool enable)
  978. {
  979. void __iomem *reg = chv_padreg(pctrl, pin, CHV_PADCTRL1);
  980. unsigned long flags;
  981. u32 ctrl1;
  982. raw_spin_lock_irqsave(&chv_lock, flags);
  983. ctrl1 = readl(reg);
  984. if (enable)
  985. ctrl1 |= CHV_PADCTRL1_ODEN;
  986. else
  987. ctrl1 &= ~CHV_PADCTRL1_ODEN;
  988. chv_writel(ctrl1, reg);
  989. raw_spin_unlock_irqrestore(&chv_lock, flags);
  990. return 0;
  991. }
  992. static int chv_config_set(struct pinctrl_dev *pctldev, unsigned pin,
  993. unsigned long *configs, unsigned nconfigs)
  994. {
  995. struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  996. enum pin_config_param param;
  997. int i, ret;
  998. u32 arg;
  999. if (chv_pad_locked(pctrl, pin))
  1000. return -EBUSY;
  1001. for (i = 0; i < nconfigs; i++) {
  1002. param = pinconf_to_config_param(configs[i]);
  1003. arg = pinconf_to_config_argument(configs[i]);
  1004. switch (param) {
  1005. case PIN_CONFIG_BIAS_DISABLE:
  1006. case PIN_CONFIG_BIAS_PULL_UP:
  1007. case PIN_CONFIG_BIAS_PULL_DOWN:
  1008. ret = chv_config_set_pull(pctrl, pin, param, arg);
  1009. if (ret)
  1010. return ret;
  1011. break;
  1012. case PIN_CONFIG_DRIVE_PUSH_PULL:
  1013. ret = chv_config_set_oden(pctrl, pin, false);
  1014. if (ret)
  1015. return ret;
  1016. break;
  1017. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  1018. ret = chv_config_set_oden(pctrl, pin, true);
  1019. if (ret)
  1020. return ret;
  1021. break;
  1022. default:
  1023. return -ENOTSUPP;
  1024. }
  1025. dev_dbg(pctrl->dev, "pin %d set config %d arg %u\n", pin,
  1026. param, arg);
  1027. }
  1028. return 0;
  1029. }
  1030. static int chv_config_group_get(struct pinctrl_dev *pctldev,
  1031. unsigned int group,
  1032. unsigned long *config)
  1033. {
  1034. const unsigned int *pins;
  1035. unsigned int npins;
  1036. int ret;
  1037. ret = chv_get_group_pins(pctldev, group, &pins, &npins);
  1038. if (ret)
  1039. return ret;
  1040. ret = chv_config_get(pctldev, pins[0], config);
  1041. if (ret)
  1042. return ret;
  1043. return 0;
  1044. }
  1045. static int chv_config_group_set(struct pinctrl_dev *pctldev,
  1046. unsigned int group, unsigned long *configs,
  1047. unsigned int num_configs)
  1048. {
  1049. const unsigned int *pins;
  1050. unsigned int npins;
  1051. int i, ret;
  1052. ret = chv_get_group_pins(pctldev, group, &pins, &npins);
  1053. if (ret)
  1054. return ret;
  1055. for (i = 0; i < npins; i++) {
  1056. ret = chv_config_set(pctldev, pins[i], configs, num_configs);
  1057. if (ret)
  1058. return ret;
  1059. }
  1060. return 0;
  1061. }
  1062. static const struct pinconf_ops chv_pinconf_ops = {
  1063. .is_generic = true,
  1064. .pin_config_set = chv_config_set,
  1065. .pin_config_get = chv_config_get,
  1066. .pin_config_group_get = chv_config_group_get,
  1067. .pin_config_group_set = chv_config_group_set,
  1068. };
  1069. static struct pinctrl_desc chv_pinctrl_desc = {
  1070. .pctlops = &chv_pinctrl_ops,
  1071. .pmxops = &chv_pinmux_ops,
  1072. .confops = &chv_pinconf_ops,
  1073. .owner = THIS_MODULE,
  1074. };
  1075. static int chv_gpio_get(struct gpio_chip *chip, unsigned offset)
  1076. {
  1077. struct chv_pinctrl *pctrl = gpiochip_get_data(chip);
  1078. unsigned long flags;
  1079. u32 ctrl0, cfg;
  1080. raw_spin_lock_irqsave(&chv_lock, flags);
  1081. ctrl0 = readl(chv_padreg(pctrl, offset, CHV_PADCTRL0));
  1082. raw_spin_unlock_irqrestore(&chv_lock, flags);
  1083. cfg = ctrl0 & CHV_PADCTRL0_GPIOCFG_MASK;
  1084. cfg >>= CHV_PADCTRL0_GPIOCFG_SHIFT;
  1085. if (cfg == CHV_PADCTRL0_GPIOCFG_GPO)
  1086. return !!(ctrl0 & CHV_PADCTRL0_GPIOTXSTATE);
  1087. return !!(ctrl0 & CHV_PADCTRL0_GPIORXSTATE);
  1088. }
  1089. static void chv_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  1090. {
  1091. struct chv_pinctrl *pctrl = gpiochip_get_data(chip);
  1092. unsigned long flags;
  1093. void __iomem *reg;
  1094. u32 ctrl0;
  1095. raw_spin_lock_irqsave(&chv_lock, flags);
  1096. reg = chv_padreg(pctrl, offset, CHV_PADCTRL0);
  1097. ctrl0 = readl(reg);
  1098. if (value)
  1099. ctrl0 |= CHV_PADCTRL0_GPIOTXSTATE;
  1100. else
  1101. ctrl0 &= ~CHV_PADCTRL0_GPIOTXSTATE;
  1102. chv_writel(ctrl0, reg);
  1103. raw_spin_unlock_irqrestore(&chv_lock, flags);
  1104. }
  1105. static int chv_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  1106. {
  1107. struct chv_pinctrl *pctrl = gpiochip_get_data(chip);
  1108. u32 ctrl0, direction;
  1109. unsigned long flags;
  1110. raw_spin_lock_irqsave(&chv_lock, flags);
  1111. ctrl0 = readl(chv_padreg(pctrl, offset, CHV_PADCTRL0));
  1112. raw_spin_unlock_irqrestore(&chv_lock, flags);
  1113. direction = ctrl0 & CHV_PADCTRL0_GPIOCFG_MASK;
  1114. direction >>= CHV_PADCTRL0_GPIOCFG_SHIFT;
  1115. return direction != CHV_PADCTRL0_GPIOCFG_GPO;
  1116. }
  1117. static int chv_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  1118. {
  1119. return pinctrl_gpio_direction_input(chip->base + offset);
  1120. }
  1121. static int chv_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  1122. int value)
  1123. {
  1124. chv_gpio_set(chip, offset, value);
  1125. return pinctrl_gpio_direction_output(chip->base + offset);
  1126. }
  1127. static const struct gpio_chip chv_gpio_chip = {
  1128. .owner = THIS_MODULE,
  1129. .request = gpiochip_generic_request,
  1130. .free = gpiochip_generic_free,
  1131. .get_direction = chv_gpio_get_direction,
  1132. .direction_input = chv_gpio_direction_input,
  1133. .direction_output = chv_gpio_direction_output,
  1134. .get = chv_gpio_get,
  1135. .set = chv_gpio_set,
  1136. };
  1137. static void chv_gpio_irq_ack(struct irq_data *d)
  1138. {
  1139. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1140. struct chv_pinctrl *pctrl = gpiochip_get_data(gc);
  1141. int pin = irqd_to_hwirq(d);
  1142. u32 intr_line;
  1143. raw_spin_lock(&chv_lock);
  1144. intr_line = readl(chv_padreg(pctrl, pin, CHV_PADCTRL0));
  1145. intr_line &= CHV_PADCTRL0_INTSEL_MASK;
  1146. intr_line >>= CHV_PADCTRL0_INTSEL_SHIFT;
  1147. chv_writel(BIT(intr_line), pctrl->regs + CHV_INTSTAT);
  1148. raw_spin_unlock(&chv_lock);
  1149. }
  1150. static void chv_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
  1151. {
  1152. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1153. struct chv_pinctrl *pctrl = gpiochip_get_data(gc);
  1154. int pin = irqd_to_hwirq(d);
  1155. u32 value, intr_line;
  1156. unsigned long flags;
  1157. raw_spin_lock_irqsave(&chv_lock, flags);
  1158. intr_line = readl(chv_padreg(pctrl, pin, CHV_PADCTRL0));
  1159. intr_line &= CHV_PADCTRL0_INTSEL_MASK;
  1160. intr_line >>= CHV_PADCTRL0_INTSEL_SHIFT;
  1161. value = readl(pctrl->regs + CHV_INTMASK);
  1162. if (mask)
  1163. value &= ~BIT(intr_line);
  1164. else
  1165. value |= BIT(intr_line);
  1166. chv_writel(value, pctrl->regs + CHV_INTMASK);
  1167. raw_spin_unlock_irqrestore(&chv_lock, flags);
  1168. }
  1169. static void chv_gpio_irq_mask(struct irq_data *d)
  1170. {
  1171. chv_gpio_irq_mask_unmask(d, true);
  1172. }
  1173. static void chv_gpio_irq_unmask(struct irq_data *d)
  1174. {
  1175. chv_gpio_irq_mask_unmask(d, false);
  1176. }
  1177. static unsigned chv_gpio_irq_startup(struct irq_data *d)
  1178. {
  1179. /*
  1180. * Check if the interrupt has been requested with 0 as triggering
  1181. * type. In that case it is assumed that the current values
  1182. * programmed to the hardware are used (e.g BIOS configured
  1183. * defaults).
  1184. *
  1185. * In that case ->irq_set_type() will never be called so we need to
  1186. * read back the values from hardware now, set correct flow handler
  1187. * and update mappings before the interrupt is being used.
  1188. */
  1189. if (irqd_get_trigger_type(d) == IRQ_TYPE_NONE) {
  1190. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1191. struct chv_pinctrl *pctrl = gpiochip_get_data(gc);
  1192. unsigned pin = irqd_to_hwirq(d);
  1193. irq_flow_handler_t handler;
  1194. unsigned long flags;
  1195. u32 intsel, value;
  1196. raw_spin_lock_irqsave(&chv_lock, flags);
  1197. intsel = readl(chv_padreg(pctrl, pin, CHV_PADCTRL0));
  1198. intsel &= CHV_PADCTRL0_INTSEL_MASK;
  1199. intsel >>= CHV_PADCTRL0_INTSEL_SHIFT;
  1200. value = readl(chv_padreg(pctrl, pin, CHV_PADCTRL1));
  1201. if (value & CHV_PADCTRL1_INTWAKECFG_LEVEL)
  1202. handler = handle_level_irq;
  1203. else
  1204. handler = handle_edge_irq;
  1205. if (!pctrl->intr_lines[intsel]) {
  1206. irq_set_handler_locked(d, handler);
  1207. pctrl->intr_lines[intsel] = pin;
  1208. }
  1209. raw_spin_unlock_irqrestore(&chv_lock, flags);
  1210. }
  1211. chv_gpio_irq_unmask(d);
  1212. return 0;
  1213. }
  1214. static int chv_gpio_irq_type(struct irq_data *d, unsigned type)
  1215. {
  1216. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1217. struct chv_pinctrl *pctrl = gpiochip_get_data(gc);
  1218. unsigned pin = irqd_to_hwirq(d);
  1219. unsigned long flags;
  1220. u32 value;
  1221. raw_spin_lock_irqsave(&chv_lock, flags);
  1222. /*
  1223. * Pins which can be used as shared interrupt are configured in
  1224. * BIOS. Driver trusts BIOS configurations and assigns different
  1225. * handler according to the irq type.
  1226. *
  1227. * Driver needs to save the mapping between each pin and
  1228. * its interrupt line.
  1229. * 1. If the pin cfg is locked in BIOS:
  1230. * Trust BIOS has programmed IntWakeCfg bits correctly,
  1231. * driver just needs to save the mapping.
  1232. * 2. If the pin cfg is not locked in BIOS:
  1233. * Driver programs the IntWakeCfg bits and save the mapping.
  1234. */
  1235. if (!chv_pad_locked(pctrl, pin)) {
  1236. void __iomem *reg = chv_padreg(pctrl, pin, CHV_PADCTRL1);
  1237. value = readl(reg);
  1238. value &= ~CHV_PADCTRL1_INTWAKECFG_MASK;
  1239. value &= ~CHV_PADCTRL1_INVRXTX_MASK;
  1240. if (type & IRQ_TYPE_EDGE_BOTH) {
  1241. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  1242. value |= CHV_PADCTRL1_INTWAKECFG_BOTH;
  1243. else if (type & IRQ_TYPE_EDGE_RISING)
  1244. value |= CHV_PADCTRL1_INTWAKECFG_RISING;
  1245. else if (type & IRQ_TYPE_EDGE_FALLING)
  1246. value |= CHV_PADCTRL1_INTWAKECFG_FALLING;
  1247. } else if (type & IRQ_TYPE_LEVEL_MASK) {
  1248. value |= CHV_PADCTRL1_INTWAKECFG_LEVEL;
  1249. if (type & IRQ_TYPE_LEVEL_LOW)
  1250. value |= CHV_PADCTRL1_INVRXTX_RXDATA;
  1251. }
  1252. chv_writel(value, reg);
  1253. }
  1254. value = readl(chv_padreg(pctrl, pin, CHV_PADCTRL0));
  1255. value &= CHV_PADCTRL0_INTSEL_MASK;
  1256. value >>= CHV_PADCTRL0_INTSEL_SHIFT;
  1257. pctrl->intr_lines[value] = pin;
  1258. if (type & IRQ_TYPE_EDGE_BOTH)
  1259. irq_set_handler_locked(d, handle_edge_irq);
  1260. else if (type & IRQ_TYPE_LEVEL_MASK)
  1261. irq_set_handler_locked(d, handle_level_irq);
  1262. raw_spin_unlock_irqrestore(&chv_lock, flags);
  1263. return 0;
  1264. }
  1265. static struct irq_chip chv_gpio_irqchip = {
  1266. .name = "chv-gpio",
  1267. .irq_startup = chv_gpio_irq_startup,
  1268. .irq_ack = chv_gpio_irq_ack,
  1269. .irq_mask = chv_gpio_irq_mask,
  1270. .irq_unmask = chv_gpio_irq_unmask,
  1271. .irq_set_type = chv_gpio_irq_type,
  1272. .flags = IRQCHIP_SKIP_SET_WAKE,
  1273. };
  1274. static void chv_gpio_irq_handler(struct irq_desc *desc)
  1275. {
  1276. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  1277. struct chv_pinctrl *pctrl = gpiochip_get_data(gc);
  1278. struct irq_chip *chip = irq_desc_get_chip(desc);
  1279. unsigned long pending;
  1280. u32 intr_line;
  1281. chained_irq_enter(chip, desc);
  1282. pending = readl(pctrl->regs + CHV_INTSTAT);
  1283. for_each_set_bit(intr_line, &pending, pctrl->community->nirqs) {
  1284. unsigned irq, offset;
  1285. offset = pctrl->intr_lines[intr_line];
  1286. irq = irq_find_mapping(gc->irq.domain, offset);
  1287. generic_handle_irq(irq);
  1288. }
  1289. chained_irq_exit(chip, desc);
  1290. }
  1291. /*
  1292. * Certain machines seem to hardcode Linux IRQ numbers in their ACPI
  1293. * tables. Since we leave GPIOs that are not capable of generating
  1294. * interrupts out of the irqdomain the numbering will be different and
  1295. * cause devices using the hardcoded IRQ numbers fail. In order not to
  1296. * break such machines we will only mask pins from irqdomain if the machine
  1297. * is not listed below.
  1298. */
  1299. static const struct dmi_system_id chv_no_valid_mask[] = {
  1300. /* See https://bugzilla.kernel.org/show_bug.cgi?id=194945 */
  1301. {
  1302. .ident = "Intel_Strago based Chromebooks (All models)",
  1303. .matches = {
  1304. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  1305. DMI_MATCH(DMI_PRODUCT_FAMILY, "Intel_Strago"),
  1306. DMI_MATCH(DMI_BOARD_VERSION, "1.0"),
  1307. },
  1308. },
  1309. {
  1310. .ident = "HP Chromebook 11 G5 (Setzer)",
  1311. .matches = {
  1312. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  1313. DMI_MATCH(DMI_PRODUCT_NAME, "Setzer"),
  1314. DMI_MATCH(DMI_BOARD_VERSION, "1.0"),
  1315. },
  1316. },
  1317. {
  1318. .ident = "Acer Chromebook R11 (Cyan)",
  1319. .matches = {
  1320. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  1321. DMI_MATCH(DMI_PRODUCT_NAME, "Cyan"),
  1322. DMI_MATCH(DMI_BOARD_VERSION, "1.0"),
  1323. },
  1324. },
  1325. {
  1326. .ident = "Samsung Chromebook 3 (Celes)",
  1327. .matches = {
  1328. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  1329. DMI_MATCH(DMI_PRODUCT_NAME, "Celes"),
  1330. DMI_MATCH(DMI_BOARD_VERSION, "1.0"),
  1331. },
  1332. },
  1333. {}
  1334. };
  1335. static int chv_gpio_probe(struct chv_pinctrl *pctrl, int irq)
  1336. {
  1337. const struct chv_gpio_pinrange *range;
  1338. struct gpio_chip *chip = &pctrl->chip;
  1339. bool need_valid_mask = !dmi_check_system(chv_no_valid_mask);
  1340. const struct chv_community *community = pctrl->community;
  1341. int ret, i, irq_base;
  1342. *chip = chv_gpio_chip;
  1343. chip->ngpio = community->pins[community->npins - 1].number + 1;
  1344. chip->label = dev_name(pctrl->dev);
  1345. chip->parent = pctrl->dev;
  1346. chip->base = -1;
  1347. chip->irq.need_valid_mask = need_valid_mask;
  1348. ret = devm_gpiochip_add_data(pctrl->dev, chip, pctrl);
  1349. if (ret) {
  1350. dev_err(pctrl->dev, "Failed to register gpiochip\n");
  1351. return ret;
  1352. }
  1353. for (i = 0; i < community->ngpio_ranges; i++) {
  1354. range = &community->gpio_ranges[i];
  1355. ret = gpiochip_add_pin_range(chip, dev_name(pctrl->dev),
  1356. range->base, range->base,
  1357. range->npins);
  1358. if (ret) {
  1359. dev_err(pctrl->dev, "failed to add GPIO pin range\n");
  1360. return ret;
  1361. }
  1362. }
  1363. /* Do not add GPIOs that can only generate GPEs to the IRQ domain */
  1364. for (i = 0; i < community->npins; i++) {
  1365. const struct pinctrl_pin_desc *desc;
  1366. u32 intsel;
  1367. desc = &community->pins[i];
  1368. intsel = readl(chv_padreg(pctrl, desc->number, CHV_PADCTRL0));
  1369. intsel &= CHV_PADCTRL0_INTSEL_MASK;
  1370. intsel >>= CHV_PADCTRL0_INTSEL_SHIFT;
  1371. if (need_valid_mask && intsel >= community->nirqs)
  1372. clear_bit(i, chip->irq.valid_mask);
  1373. }
  1374. /*
  1375. * The same set of machines in chv_no_valid_mask[] have incorrectly
  1376. * configured GPIOs that generate spurious interrupts so we use
  1377. * this same list to apply another quirk for them.
  1378. *
  1379. * See also https://bugzilla.kernel.org/show_bug.cgi?id=197953.
  1380. */
  1381. if (!need_valid_mask) {
  1382. /*
  1383. * Mask all interrupts the community is able to generate
  1384. * but leave the ones that can only generate GPEs unmasked.
  1385. */
  1386. chv_writel(GENMASK(31, pctrl->community->nirqs),
  1387. pctrl->regs + CHV_INTMASK);
  1388. }
  1389. /* Clear all interrupts */
  1390. chv_writel(0xffff, pctrl->regs + CHV_INTSTAT);
  1391. if (!need_valid_mask) {
  1392. irq_base = devm_irq_alloc_descs(pctrl->dev, -1, 0,
  1393. community->npins, NUMA_NO_NODE);
  1394. if (irq_base < 0) {
  1395. dev_err(pctrl->dev, "Failed to allocate IRQ numbers\n");
  1396. return irq_base;
  1397. }
  1398. }
  1399. ret = gpiochip_irqchip_add(chip, &chv_gpio_irqchip, 0,
  1400. handle_bad_irq, IRQ_TYPE_NONE);
  1401. if (ret) {
  1402. dev_err(pctrl->dev, "failed to add IRQ chip\n");
  1403. return ret;
  1404. }
  1405. if (!need_valid_mask) {
  1406. for (i = 0; i < community->ngpio_ranges; i++) {
  1407. range = &community->gpio_ranges[i];
  1408. irq_domain_associate_many(chip->irq.domain, irq_base,
  1409. range->base, range->npins);
  1410. irq_base += range->npins;
  1411. }
  1412. }
  1413. gpiochip_set_chained_irqchip(chip, &chv_gpio_irqchip, irq,
  1414. chv_gpio_irq_handler);
  1415. return 0;
  1416. }
  1417. static acpi_status chv_pinctrl_mmio_access_handler(u32 function,
  1418. acpi_physical_address address, u32 bits, u64 *value,
  1419. void *handler_context, void *region_context)
  1420. {
  1421. struct chv_pinctrl *pctrl = region_context;
  1422. unsigned long flags;
  1423. acpi_status ret = AE_OK;
  1424. raw_spin_lock_irqsave(&chv_lock, flags);
  1425. if (function == ACPI_WRITE)
  1426. chv_writel((u32)(*value), pctrl->regs + (u32)address);
  1427. else if (function == ACPI_READ)
  1428. *value = readl(pctrl->regs + (u32)address);
  1429. else
  1430. ret = AE_BAD_PARAMETER;
  1431. raw_spin_unlock_irqrestore(&chv_lock, flags);
  1432. return ret;
  1433. }
  1434. static int chv_pinctrl_probe(struct platform_device *pdev)
  1435. {
  1436. struct chv_pinctrl *pctrl;
  1437. struct acpi_device *adev;
  1438. struct resource *res;
  1439. acpi_status status;
  1440. int ret, irq, i;
  1441. adev = ACPI_COMPANION(&pdev->dev);
  1442. if (!adev)
  1443. return -ENODEV;
  1444. pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
  1445. if (!pctrl)
  1446. return -ENOMEM;
  1447. for (i = 0; i < ARRAY_SIZE(chv_communities); i++)
  1448. if (!strcmp(adev->pnp.unique_id, chv_communities[i]->uid)) {
  1449. pctrl->community = chv_communities[i];
  1450. break;
  1451. }
  1452. if (i == ARRAY_SIZE(chv_communities))
  1453. return -ENODEV;
  1454. pctrl->dev = &pdev->dev;
  1455. #ifdef CONFIG_PM_SLEEP
  1456. pctrl->saved_pin_context = devm_kcalloc(pctrl->dev,
  1457. pctrl->community->npins, sizeof(*pctrl->saved_pin_context),
  1458. GFP_KERNEL);
  1459. if (!pctrl->saved_pin_context)
  1460. return -ENOMEM;
  1461. #endif
  1462. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1463. pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
  1464. if (IS_ERR(pctrl->regs))
  1465. return PTR_ERR(pctrl->regs);
  1466. irq = platform_get_irq(pdev, 0);
  1467. if (irq < 0) {
  1468. dev_err(&pdev->dev, "failed to get interrupt number\n");
  1469. return irq;
  1470. }
  1471. pctrl->pctldesc = chv_pinctrl_desc;
  1472. pctrl->pctldesc.name = dev_name(&pdev->dev);
  1473. pctrl->pctldesc.pins = pctrl->community->pins;
  1474. pctrl->pctldesc.npins = pctrl->community->npins;
  1475. pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
  1476. pctrl);
  1477. if (IS_ERR(pctrl->pctldev)) {
  1478. dev_err(&pdev->dev, "failed to register pinctrl driver\n");
  1479. return PTR_ERR(pctrl->pctldev);
  1480. }
  1481. ret = chv_gpio_probe(pctrl, irq);
  1482. if (ret)
  1483. return ret;
  1484. status = acpi_install_address_space_handler(adev->handle,
  1485. pctrl->community->acpi_space_id,
  1486. chv_pinctrl_mmio_access_handler,
  1487. NULL, pctrl);
  1488. if (ACPI_FAILURE(status))
  1489. dev_err(&pdev->dev, "failed to install ACPI addr space handler\n");
  1490. platform_set_drvdata(pdev, pctrl);
  1491. return 0;
  1492. }
  1493. static int chv_pinctrl_remove(struct platform_device *pdev)
  1494. {
  1495. struct chv_pinctrl *pctrl = platform_get_drvdata(pdev);
  1496. acpi_remove_address_space_handler(ACPI_COMPANION(&pdev->dev),
  1497. pctrl->community->acpi_space_id,
  1498. chv_pinctrl_mmio_access_handler);
  1499. return 0;
  1500. }
  1501. #ifdef CONFIG_PM_SLEEP
  1502. static int chv_pinctrl_suspend_noirq(struct device *dev)
  1503. {
  1504. struct platform_device *pdev = to_platform_device(dev);
  1505. struct chv_pinctrl *pctrl = platform_get_drvdata(pdev);
  1506. unsigned long flags;
  1507. int i;
  1508. raw_spin_lock_irqsave(&chv_lock, flags);
  1509. pctrl->saved_intmask = readl(pctrl->regs + CHV_INTMASK);
  1510. for (i = 0; i < pctrl->community->npins; i++) {
  1511. const struct pinctrl_pin_desc *desc;
  1512. struct chv_pin_context *ctx;
  1513. void __iomem *reg;
  1514. desc = &pctrl->community->pins[i];
  1515. if (chv_pad_locked(pctrl, desc->number))
  1516. continue;
  1517. ctx = &pctrl->saved_pin_context[i];
  1518. reg = chv_padreg(pctrl, desc->number, CHV_PADCTRL0);
  1519. ctx->padctrl0 = readl(reg) & ~CHV_PADCTRL0_GPIORXSTATE;
  1520. reg = chv_padreg(pctrl, desc->number, CHV_PADCTRL1);
  1521. ctx->padctrl1 = readl(reg);
  1522. }
  1523. raw_spin_unlock_irqrestore(&chv_lock, flags);
  1524. return 0;
  1525. }
  1526. static int chv_pinctrl_resume_noirq(struct device *dev)
  1527. {
  1528. struct platform_device *pdev = to_platform_device(dev);
  1529. struct chv_pinctrl *pctrl = platform_get_drvdata(pdev);
  1530. unsigned long flags;
  1531. int i;
  1532. raw_spin_lock_irqsave(&chv_lock, flags);
  1533. /*
  1534. * Mask all interrupts before restoring per-pin configuration
  1535. * registers because we don't know in which state BIOS left them
  1536. * upon exiting suspend.
  1537. */
  1538. chv_writel(0, pctrl->regs + CHV_INTMASK);
  1539. for (i = 0; i < pctrl->community->npins; i++) {
  1540. const struct pinctrl_pin_desc *desc;
  1541. const struct chv_pin_context *ctx;
  1542. void __iomem *reg;
  1543. u32 val;
  1544. desc = &pctrl->community->pins[i];
  1545. if (chv_pad_locked(pctrl, desc->number))
  1546. continue;
  1547. ctx = &pctrl->saved_pin_context[i];
  1548. /* Only restore if our saved state differs from the current */
  1549. reg = chv_padreg(pctrl, desc->number, CHV_PADCTRL0);
  1550. val = readl(reg) & ~CHV_PADCTRL0_GPIORXSTATE;
  1551. if (ctx->padctrl0 != val) {
  1552. chv_writel(ctx->padctrl0, reg);
  1553. dev_dbg(pctrl->dev, "restored pin %2u ctrl0 0x%08x\n",
  1554. desc->number, readl(reg));
  1555. }
  1556. reg = chv_padreg(pctrl, desc->number, CHV_PADCTRL1);
  1557. val = readl(reg);
  1558. if (ctx->padctrl1 != val) {
  1559. chv_writel(ctx->padctrl1, reg);
  1560. dev_dbg(pctrl->dev, "restored pin %2u ctrl1 0x%08x\n",
  1561. desc->number, readl(reg));
  1562. }
  1563. }
  1564. /*
  1565. * Now that all pins are restored to known state, we can restore
  1566. * the interrupt mask register as well.
  1567. */
  1568. chv_writel(0xffff, pctrl->regs + CHV_INTSTAT);
  1569. chv_writel(pctrl->saved_intmask, pctrl->regs + CHV_INTMASK);
  1570. raw_spin_unlock_irqrestore(&chv_lock, flags);
  1571. return 0;
  1572. }
  1573. #endif
  1574. static const struct dev_pm_ops chv_pinctrl_pm_ops = {
  1575. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(chv_pinctrl_suspend_noirq,
  1576. chv_pinctrl_resume_noirq)
  1577. };
  1578. static const struct acpi_device_id chv_pinctrl_acpi_match[] = {
  1579. { "INT33FF" },
  1580. { }
  1581. };
  1582. MODULE_DEVICE_TABLE(acpi, chv_pinctrl_acpi_match);
  1583. static struct platform_driver chv_pinctrl_driver = {
  1584. .probe = chv_pinctrl_probe,
  1585. .remove = chv_pinctrl_remove,
  1586. .driver = {
  1587. .name = "cherryview-pinctrl",
  1588. .pm = &chv_pinctrl_pm_ops,
  1589. .acpi_match_table = chv_pinctrl_acpi_match,
  1590. },
  1591. };
  1592. static int __init chv_pinctrl_init(void)
  1593. {
  1594. return platform_driver_register(&chv_pinctrl_driver);
  1595. }
  1596. subsys_initcall(chv_pinctrl_init);
  1597. static void __exit chv_pinctrl_exit(void)
  1598. {
  1599. platform_driver_unregister(&chv_pinctrl_driver);
  1600. }
  1601. module_exit(chv_pinctrl_exit);
  1602. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  1603. MODULE_DESCRIPTION("Intel Cherryview/Braswell pinctrl driver");
  1604. MODULE_LICENSE("GPL v2");