pinctrl-cherryview.c 49 KB

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