pinctrl-cherryview.c 51 KB

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