pinctrl-cherryview.c 48 KB

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