pinctrl-cherryview.c 51 KB

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