pinctrl-cherryview.c 47 KB

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