pinctrl-cherryview.c 49 KB

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