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