pinctrl-cherryview.c 51 KB

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