pinctrl-st.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. /*
  2. * Copyright (C) 2013 STMicroelectronics (R&D) Limited.
  3. * Authors:
  4. * Srinivas Kandagatla <srinivas.kandagatla@st.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/of.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/of_address.h>
  19. #include <linux/regmap.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/pinctrl/pinctrl.h>
  22. #include <linux/pinctrl/pinmux.h>
  23. #include <linux/pinctrl/pinconf.h>
  24. #include <linux/platform_device.h>
  25. #include "core.h"
  26. /* PIO Block registers */
  27. /* PIO output */
  28. #define REG_PIO_POUT 0x00
  29. /* Set bits of POUT */
  30. #define REG_PIO_SET_POUT 0x04
  31. /* Clear bits of POUT */
  32. #define REG_PIO_CLR_POUT 0x08
  33. /* PIO input */
  34. #define REG_PIO_PIN 0x10
  35. /* PIO configuration */
  36. #define REG_PIO_PC(n) (0x20 + (n) * 0x10)
  37. /* Set bits of PC[2:0] */
  38. #define REG_PIO_SET_PC(n) (0x24 + (n) * 0x10)
  39. /* Clear bits of PC[2:0] */
  40. #define REG_PIO_CLR_PC(n) (0x28 + (n) * 0x10)
  41. /* PIO input comparison */
  42. #define REG_PIO_PCOMP 0x50
  43. /* Set bits of PCOMP */
  44. #define REG_PIO_SET_PCOMP 0x54
  45. /* Clear bits of PCOMP */
  46. #define REG_PIO_CLR_PCOMP 0x58
  47. /* PIO input comparison mask */
  48. #define REG_PIO_PMASK 0x60
  49. /* Set bits of PMASK */
  50. #define REG_PIO_SET_PMASK 0x64
  51. /* Clear bits of PMASK */
  52. #define REG_PIO_CLR_PMASK 0x68
  53. #define ST_GPIO_DIRECTION_BIDIR 0x1
  54. #define ST_GPIO_DIRECTION_OUT 0x2
  55. #define ST_GPIO_DIRECTION_IN 0x4
  56. /**
  57. * Packed style retime configuration.
  58. * There are two registers cfg0 and cfg1 in this style for each bank.
  59. * Each field in this register is 8 bit corresponding to 8 pins in the bank.
  60. */
  61. #define RT_P_CFGS_PER_BANK 2
  62. #define RT_P_CFG0_CLK1NOTCLK0_FIELD(reg) REG_FIELD(reg, 0, 7)
  63. #define RT_P_CFG0_DELAY_0_FIELD(reg) REG_FIELD(reg, 16, 23)
  64. #define RT_P_CFG0_DELAY_1_FIELD(reg) REG_FIELD(reg, 24, 31)
  65. #define RT_P_CFG1_INVERTCLK_FIELD(reg) REG_FIELD(reg, 0, 7)
  66. #define RT_P_CFG1_RETIME_FIELD(reg) REG_FIELD(reg, 8, 15)
  67. #define RT_P_CFG1_CLKNOTDATA_FIELD(reg) REG_FIELD(reg, 16, 23)
  68. #define RT_P_CFG1_DOUBLE_EDGE_FIELD(reg) REG_FIELD(reg, 24, 31)
  69. /**
  70. * Dedicated style retime Configuration register
  71. * each register is dedicated per pin.
  72. */
  73. #define RT_D_CFGS_PER_BANK 8
  74. #define RT_D_CFG_CLK_SHIFT 0
  75. #define RT_D_CFG_CLK_MASK (0x3 << 0)
  76. #define RT_D_CFG_CLKNOTDATA_SHIFT 2
  77. #define RT_D_CFG_CLKNOTDATA_MASK BIT(2)
  78. #define RT_D_CFG_DELAY_SHIFT 3
  79. #define RT_D_CFG_DELAY_MASK (0xf << 3)
  80. #define RT_D_CFG_DELAY_INNOTOUT_SHIFT 7
  81. #define RT_D_CFG_DELAY_INNOTOUT_MASK BIT(7)
  82. #define RT_D_CFG_DOUBLE_EDGE_SHIFT 8
  83. #define RT_D_CFG_DOUBLE_EDGE_MASK BIT(8)
  84. #define RT_D_CFG_INVERTCLK_SHIFT 9
  85. #define RT_D_CFG_INVERTCLK_MASK BIT(9)
  86. #define RT_D_CFG_RETIME_SHIFT 10
  87. #define RT_D_CFG_RETIME_MASK BIT(10)
  88. /*
  89. * Pinconf is represented in an opaque unsigned long variable.
  90. * Below is the bit allocation details for each possible configuration.
  91. * All the bit fields can be encapsulated into four variables
  92. * (direction, retime-type, retime-clk, retime-delay)
  93. *
  94. * +----------------+
  95. *[31:28]| reserved-3 |
  96. * +----------------+-------------
  97. *[27] | oe | |
  98. * +----------------+ v
  99. *[26] | pu | [Direction ]
  100. * +----------------+ ^
  101. *[25] | od | |
  102. * +----------------+-------------
  103. *[24] | reserved-2 |
  104. * +----------------+-------------
  105. *[23] | retime | |
  106. * +----------------+ |
  107. *[22] | retime-invclk | |
  108. * +----------------+ v
  109. *[21] |retime-clknotdat| [Retime-type ]
  110. * +----------------+ ^
  111. *[20] | retime-de | |
  112. * +----------------+-------------
  113. *[19:18]| retime-clk |------>[Retime-Clk ]
  114. * +----------------+
  115. *[17:16]| reserved-1 |
  116. * +----------------+
  117. *[15..0]| retime-delay |------>[Retime Delay]
  118. * +----------------+
  119. */
  120. #define ST_PINCONF_UNPACK(conf, param)\
  121. ((conf >> ST_PINCONF_ ##param ##_SHIFT) \
  122. & ST_PINCONF_ ##param ##_MASK)
  123. #define ST_PINCONF_PACK(conf, val, param) (conf |=\
  124. ((val & ST_PINCONF_ ##param ##_MASK) << \
  125. ST_PINCONF_ ##param ##_SHIFT))
  126. /* Output enable */
  127. #define ST_PINCONF_OE_MASK 0x1
  128. #define ST_PINCONF_OE_SHIFT 27
  129. #define ST_PINCONF_OE BIT(27)
  130. #define ST_PINCONF_UNPACK_OE(conf) ST_PINCONF_UNPACK(conf, OE)
  131. #define ST_PINCONF_PACK_OE(conf) ST_PINCONF_PACK(conf, 1, OE)
  132. /* Pull Up */
  133. #define ST_PINCONF_PU_MASK 0x1
  134. #define ST_PINCONF_PU_SHIFT 26
  135. #define ST_PINCONF_PU BIT(26)
  136. #define ST_PINCONF_UNPACK_PU(conf) ST_PINCONF_UNPACK(conf, PU)
  137. #define ST_PINCONF_PACK_PU(conf) ST_PINCONF_PACK(conf, 1, PU)
  138. /* Open Drain */
  139. #define ST_PINCONF_OD_MASK 0x1
  140. #define ST_PINCONF_OD_SHIFT 25
  141. #define ST_PINCONF_OD BIT(25)
  142. #define ST_PINCONF_UNPACK_OD(conf) ST_PINCONF_UNPACK(conf, OD)
  143. #define ST_PINCONF_PACK_OD(conf) ST_PINCONF_PACK(conf, 1, OD)
  144. #define ST_PINCONF_RT_MASK 0x1
  145. #define ST_PINCONF_RT_SHIFT 23
  146. #define ST_PINCONF_RT BIT(23)
  147. #define ST_PINCONF_UNPACK_RT(conf) ST_PINCONF_UNPACK(conf, RT)
  148. #define ST_PINCONF_PACK_RT(conf) ST_PINCONF_PACK(conf, 1, RT)
  149. #define ST_PINCONF_RT_INVERTCLK_MASK 0x1
  150. #define ST_PINCONF_RT_INVERTCLK_SHIFT 22
  151. #define ST_PINCONF_RT_INVERTCLK BIT(22)
  152. #define ST_PINCONF_UNPACK_RT_INVERTCLK(conf) \
  153. ST_PINCONF_UNPACK(conf, RT_INVERTCLK)
  154. #define ST_PINCONF_PACK_RT_INVERTCLK(conf) \
  155. ST_PINCONF_PACK(conf, 1, RT_INVERTCLK)
  156. #define ST_PINCONF_RT_CLKNOTDATA_MASK 0x1
  157. #define ST_PINCONF_RT_CLKNOTDATA_SHIFT 21
  158. #define ST_PINCONF_RT_CLKNOTDATA BIT(21)
  159. #define ST_PINCONF_UNPACK_RT_CLKNOTDATA(conf) \
  160. ST_PINCONF_UNPACK(conf, RT_CLKNOTDATA)
  161. #define ST_PINCONF_PACK_RT_CLKNOTDATA(conf) \
  162. ST_PINCONF_PACK(conf, 1, RT_CLKNOTDATA)
  163. #define ST_PINCONF_RT_DOUBLE_EDGE_MASK 0x1
  164. #define ST_PINCONF_RT_DOUBLE_EDGE_SHIFT 20
  165. #define ST_PINCONF_RT_DOUBLE_EDGE BIT(20)
  166. #define ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(conf) \
  167. ST_PINCONF_UNPACK(conf, RT_DOUBLE_EDGE)
  168. #define ST_PINCONF_PACK_RT_DOUBLE_EDGE(conf) \
  169. ST_PINCONF_PACK(conf, 1, RT_DOUBLE_EDGE)
  170. #define ST_PINCONF_RT_CLK_MASK 0x3
  171. #define ST_PINCONF_RT_CLK_SHIFT 18
  172. #define ST_PINCONF_RT_CLK BIT(18)
  173. #define ST_PINCONF_UNPACK_RT_CLK(conf) ST_PINCONF_UNPACK(conf, RT_CLK)
  174. #define ST_PINCONF_PACK_RT_CLK(conf, val) ST_PINCONF_PACK(conf, val, RT_CLK)
  175. /* RETIME_DELAY in Pico Secs */
  176. #define ST_PINCONF_RT_DELAY_MASK 0xffff
  177. #define ST_PINCONF_RT_DELAY_SHIFT 0
  178. #define ST_PINCONF_UNPACK_RT_DELAY(conf) ST_PINCONF_UNPACK(conf, RT_DELAY)
  179. #define ST_PINCONF_PACK_RT_DELAY(conf, val) \
  180. ST_PINCONF_PACK(conf, val, RT_DELAY)
  181. #define ST_GPIO_PINS_PER_BANK (8)
  182. #define OF_GPIO_ARGS_MIN (4)
  183. #define OF_RT_ARGS_MIN (2)
  184. #define gpio_range_to_bank(chip) \
  185. container_of(chip, struct st_gpio_bank, range)
  186. #define pc_to_bank(pc) \
  187. container_of(pc, struct st_gpio_bank, pc)
  188. enum st_retime_style {
  189. st_retime_style_none,
  190. st_retime_style_packed,
  191. st_retime_style_dedicated,
  192. };
  193. struct st_retime_dedicated {
  194. struct regmap_field *rt[ST_GPIO_PINS_PER_BANK];
  195. };
  196. struct st_retime_packed {
  197. struct regmap_field *clk1notclk0;
  198. struct regmap_field *delay_0;
  199. struct regmap_field *delay_1;
  200. struct regmap_field *invertclk;
  201. struct regmap_field *retime;
  202. struct regmap_field *clknotdata;
  203. struct regmap_field *double_edge;
  204. };
  205. struct st_pio_control {
  206. u32 rt_pin_mask;
  207. struct regmap_field *alt, *oe, *pu, *od;
  208. /* retiming */
  209. union {
  210. struct st_retime_packed rt_p;
  211. struct st_retime_dedicated rt_d;
  212. } rt;
  213. };
  214. struct st_pctl_data {
  215. const enum st_retime_style rt_style;
  216. const unsigned int *input_delays;
  217. const int ninput_delays;
  218. const unsigned int *output_delays;
  219. const int noutput_delays;
  220. /* register offset information */
  221. const int alt, oe, pu, od, rt;
  222. };
  223. struct st_pinconf {
  224. int pin;
  225. const char *name;
  226. unsigned long config;
  227. int altfunc;
  228. };
  229. struct st_pmx_func {
  230. const char *name;
  231. const char **groups;
  232. unsigned ngroups;
  233. };
  234. struct st_pctl_group {
  235. const char *name;
  236. unsigned int *pins;
  237. unsigned npins;
  238. struct st_pinconf *pin_conf;
  239. };
  240. /*
  241. * Edge triggers are not supported at hardware level, it is supported by
  242. * software by exploiting the level trigger support in hardware.
  243. * Software uses a virtual register (EDGE_CONF) for edge trigger configuration
  244. * of each gpio pin in a GPIO bank.
  245. *
  246. * Each bank has a 32 bit EDGE_CONF register which is divided in to 8 parts of
  247. * 4-bits. Each 4-bit space is allocated for each pin in a gpio bank.
  248. *
  249. * bit allocation per pin is:
  250. * Bits: [0 - 3] | [4 - 7] [8 - 11] ... ... ... ... [ 28 - 31]
  251. * --------------------------------------------------------
  252. * | pin-0 | pin-2 | pin-3 | ... ... ... ... | pin -7 |
  253. * --------------------------------------------------------
  254. *
  255. * A pin can have one of following the values in its edge configuration field.
  256. *
  257. * ------- ----------------------------
  258. * [0-3] - Description
  259. * ------- ----------------------------
  260. * 0000 - No edge IRQ.
  261. * 0001 - Falling edge IRQ.
  262. * 0010 - Rising edge IRQ.
  263. * 0011 - Rising and Falling edge IRQ.
  264. * ------- ----------------------------
  265. */
  266. #define ST_IRQ_EDGE_CONF_BITS_PER_PIN 4
  267. #define ST_IRQ_EDGE_MASK 0xf
  268. #define ST_IRQ_EDGE_FALLING BIT(0)
  269. #define ST_IRQ_EDGE_RISING BIT(1)
  270. #define ST_IRQ_EDGE_BOTH (BIT(0) | BIT(1))
  271. #define ST_IRQ_RISING_EDGE_CONF(pin) \
  272. (ST_IRQ_EDGE_RISING << (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN))
  273. #define ST_IRQ_FALLING_EDGE_CONF(pin) \
  274. (ST_IRQ_EDGE_FALLING << (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN))
  275. #define ST_IRQ_BOTH_EDGE_CONF(pin) \
  276. (ST_IRQ_EDGE_BOTH << (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN))
  277. #define ST_IRQ_EDGE_CONF(conf, pin) \
  278. (conf >> (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN) & ST_IRQ_EDGE_MASK)
  279. struct st_gpio_bank {
  280. struct gpio_chip gpio_chip;
  281. struct pinctrl_gpio_range range;
  282. void __iomem *base;
  283. struct st_pio_control pc;
  284. unsigned long irq_edge_conf;
  285. spinlock_t lock;
  286. };
  287. struct st_pinctrl {
  288. struct device *dev;
  289. struct pinctrl_dev *pctl;
  290. struct st_gpio_bank *banks;
  291. int nbanks;
  292. struct st_pmx_func *functions;
  293. int nfunctions;
  294. struct st_pctl_group *groups;
  295. int ngroups;
  296. struct regmap *regmap;
  297. const struct st_pctl_data *data;
  298. void __iomem *irqmux_base;
  299. };
  300. /* SOC specific data */
  301. /* STiH415 data */
  302. static const unsigned int stih415_input_delays[] = {0, 500, 1000, 1500};
  303. static const unsigned int stih415_output_delays[] = {0, 1000, 2000, 3000};
  304. #define STIH415_PCTRL_COMMON_DATA \
  305. .rt_style = st_retime_style_packed, \
  306. .input_delays = stih415_input_delays, \
  307. .ninput_delays = ARRAY_SIZE(stih415_input_delays), \
  308. .output_delays = stih415_output_delays, \
  309. .noutput_delays = ARRAY_SIZE(stih415_output_delays)
  310. static const struct st_pctl_data stih415_sbc_data = {
  311. STIH415_PCTRL_COMMON_DATA,
  312. .alt = 0, .oe = 5, .pu = 7, .od = 9, .rt = 16,
  313. };
  314. static const struct st_pctl_data stih415_front_data = {
  315. STIH415_PCTRL_COMMON_DATA,
  316. .alt = 0, .oe = 8, .pu = 10, .od = 12, .rt = 16,
  317. };
  318. static const struct st_pctl_data stih415_rear_data = {
  319. STIH415_PCTRL_COMMON_DATA,
  320. .alt = 0, .oe = 6, .pu = 8, .od = 10, .rt = 38,
  321. };
  322. static const struct st_pctl_data stih415_left_data = {
  323. STIH415_PCTRL_COMMON_DATA,
  324. .alt = 0, .oe = 3, .pu = 4, .od = 5, .rt = 6,
  325. };
  326. static const struct st_pctl_data stih415_right_data = {
  327. STIH415_PCTRL_COMMON_DATA,
  328. .alt = 0, .oe = 5, .pu = 7, .od = 9, .rt = 11,
  329. };
  330. /* STiH416 data */
  331. static const unsigned int stih416_delays[] = {0, 300, 500, 750, 1000, 1250,
  332. 1500, 1750, 2000, 2250, 2500, 2750, 3000, 3250 };
  333. static const struct st_pctl_data stih416_data = {
  334. .rt_style = st_retime_style_dedicated,
  335. .input_delays = stih416_delays,
  336. .ninput_delays = ARRAY_SIZE(stih416_delays),
  337. .output_delays = stih416_delays,
  338. .noutput_delays = ARRAY_SIZE(stih416_delays),
  339. .alt = 0, .oe = 40, .pu = 50, .od = 60, .rt = 100,
  340. };
  341. static const struct st_pctl_data stih407_flashdata = {
  342. .rt_style = st_retime_style_none,
  343. .input_delays = stih416_delays,
  344. .ninput_delays = ARRAY_SIZE(stih416_delays),
  345. .output_delays = stih416_delays,
  346. .noutput_delays = ARRAY_SIZE(stih416_delays),
  347. .alt = 0,
  348. .oe = -1, /* Not Available */
  349. .pu = -1, /* Not Available */
  350. .od = 60,
  351. .rt = 100,
  352. };
  353. static struct st_pio_control *st_get_pio_control(
  354. struct pinctrl_dev *pctldev, int pin)
  355. {
  356. struct pinctrl_gpio_range *range =
  357. pinctrl_find_gpio_range_from_pin(pctldev, pin);
  358. struct st_gpio_bank *bank = gpio_range_to_bank(range);
  359. return &bank->pc;
  360. }
  361. /* Low level functions.. */
  362. static inline int st_gpio_bank(int gpio)
  363. {
  364. return gpio/ST_GPIO_PINS_PER_BANK;
  365. }
  366. static inline int st_gpio_pin(int gpio)
  367. {
  368. return gpio%ST_GPIO_PINS_PER_BANK;
  369. }
  370. static void st_pinconf_set_config(struct st_pio_control *pc,
  371. int pin, unsigned long config)
  372. {
  373. struct regmap_field *output_enable = pc->oe;
  374. struct regmap_field *pull_up = pc->pu;
  375. struct regmap_field *open_drain = pc->od;
  376. unsigned int oe_value, pu_value, od_value;
  377. unsigned long mask = BIT(pin);
  378. if (output_enable) {
  379. regmap_field_read(output_enable, &oe_value);
  380. oe_value &= ~mask;
  381. if (config & ST_PINCONF_OE)
  382. oe_value |= mask;
  383. regmap_field_write(output_enable, oe_value);
  384. }
  385. if (pull_up) {
  386. regmap_field_read(pull_up, &pu_value);
  387. pu_value &= ~mask;
  388. if (config & ST_PINCONF_PU)
  389. pu_value |= mask;
  390. regmap_field_write(pull_up, pu_value);
  391. }
  392. if (open_drain) {
  393. regmap_field_read(open_drain, &od_value);
  394. od_value &= ~mask;
  395. if (config & ST_PINCONF_OD)
  396. od_value |= mask;
  397. regmap_field_write(open_drain, od_value);
  398. }
  399. }
  400. static void st_pctl_set_function(struct st_pio_control *pc,
  401. int pin_id, int function)
  402. {
  403. struct regmap_field *alt = pc->alt;
  404. unsigned int val;
  405. int pin = st_gpio_pin(pin_id);
  406. int offset = pin * 4;
  407. if (!alt)
  408. return;
  409. regmap_field_read(alt, &val);
  410. val &= ~(0xf << offset);
  411. val |= function << offset;
  412. regmap_field_write(alt, val);
  413. }
  414. static unsigned int st_pctl_get_pin_function(struct st_pio_control *pc, int pin)
  415. {
  416. struct regmap_field *alt = pc->alt;
  417. unsigned int val;
  418. int offset = pin * 4;
  419. if (!alt)
  420. return 0;
  421. regmap_field_read(alt, &val);
  422. return (val >> offset) & 0xf;
  423. }
  424. static unsigned long st_pinconf_delay_to_bit(unsigned int delay,
  425. const struct st_pctl_data *data, unsigned long config)
  426. {
  427. const unsigned int *delay_times;
  428. int num_delay_times, i, closest_index = -1;
  429. unsigned int closest_divergence = UINT_MAX;
  430. if (ST_PINCONF_UNPACK_OE(config)) {
  431. delay_times = data->output_delays;
  432. num_delay_times = data->noutput_delays;
  433. } else {
  434. delay_times = data->input_delays;
  435. num_delay_times = data->ninput_delays;
  436. }
  437. for (i = 0; i < num_delay_times; i++) {
  438. unsigned int divergence = abs(delay - delay_times[i]);
  439. if (divergence == 0)
  440. return i;
  441. if (divergence < closest_divergence) {
  442. closest_divergence = divergence;
  443. closest_index = i;
  444. }
  445. }
  446. pr_warn("Attempt to set delay %d, closest available %d\n",
  447. delay, delay_times[closest_index]);
  448. return closest_index;
  449. }
  450. static unsigned long st_pinconf_bit_to_delay(unsigned int index,
  451. const struct st_pctl_data *data, unsigned long output)
  452. {
  453. const unsigned int *delay_times;
  454. int num_delay_times;
  455. if (output) {
  456. delay_times = data->output_delays;
  457. num_delay_times = data->noutput_delays;
  458. } else {
  459. delay_times = data->input_delays;
  460. num_delay_times = data->ninput_delays;
  461. }
  462. if (index < num_delay_times) {
  463. return delay_times[index];
  464. } else {
  465. pr_warn("Delay not found in/out delay list\n");
  466. return 0;
  467. }
  468. }
  469. static void st_regmap_field_bit_set_clear_pin(struct regmap_field *field,
  470. int enable, int pin)
  471. {
  472. unsigned int val = 0;
  473. regmap_field_read(field, &val);
  474. if (enable)
  475. val |= BIT(pin);
  476. else
  477. val &= ~BIT(pin);
  478. regmap_field_write(field, val);
  479. }
  480. static void st_pinconf_set_retime_packed(struct st_pinctrl *info,
  481. struct st_pio_control *pc, unsigned long config, int pin)
  482. {
  483. const struct st_pctl_data *data = info->data;
  484. struct st_retime_packed *rt_p = &pc->rt.rt_p;
  485. unsigned int delay;
  486. st_regmap_field_bit_set_clear_pin(rt_p->clk1notclk0,
  487. ST_PINCONF_UNPACK_RT_CLK(config), pin);
  488. st_regmap_field_bit_set_clear_pin(rt_p->clknotdata,
  489. ST_PINCONF_UNPACK_RT_CLKNOTDATA(config), pin);
  490. st_regmap_field_bit_set_clear_pin(rt_p->double_edge,
  491. ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config), pin);
  492. st_regmap_field_bit_set_clear_pin(rt_p->invertclk,
  493. ST_PINCONF_UNPACK_RT_INVERTCLK(config), pin);
  494. st_regmap_field_bit_set_clear_pin(rt_p->retime,
  495. ST_PINCONF_UNPACK_RT(config), pin);
  496. delay = st_pinconf_delay_to_bit(ST_PINCONF_UNPACK_RT_DELAY(config),
  497. data, config);
  498. /* 2 bit delay, lsb */
  499. st_regmap_field_bit_set_clear_pin(rt_p->delay_0, delay & 0x1, pin);
  500. /* 2 bit delay, msb */
  501. st_regmap_field_bit_set_clear_pin(rt_p->delay_1, delay & 0x2, pin);
  502. }
  503. static void st_pinconf_set_retime_dedicated(struct st_pinctrl *info,
  504. struct st_pio_control *pc, unsigned long config, int pin)
  505. {
  506. int input = ST_PINCONF_UNPACK_OE(config) ? 0 : 1;
  507. int clk = ST_PINCONF_UNPACK_RT_CLK(config);
  508. int clknotdata = ST_PINCONF_UNPACK_RT_CLKNOTDATA(config);
  509. int double_edge = ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config);
  510. int invertclk = ST_PINCONF_UNPACK_RT_INVERTCLK(config);
  511. int retime = ST_PINCONF_UNPACK_RT(config);
  512. unsigned long delay = st_pinconf_delay_to_bit(
  513. ST_PINCONF_UNPACK_RT_DELAY(config),
  514. info->data, config);
  515. struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
  516. unsigned long retime_config =
  517. ((clk) << RT_D_CFG_CLK_SHIFT) |
  518. ((delay) << RT_D_CFG_DELAY_SHIFT) |
  519. ((input) << RT_D_CFG_DELAY_INNOTOUT_SHIFT) |
  520. ((retime) << RT_D_CFG_RETIME_SHIFT) |
  521. ((clknotdata) << RT_D_CFG_CLKNOTDATA_SHIFT) |
  522. ((invertclk) << RT_D_CFG_INVERTCLK_SHIFT) |
  523. ((double_edge) << RT_D_CFG_DOUBLE_EDGE_SHIFT);
  524. regmap_field_write(rt_d->rt[pin], retime_config);
  525. }
  526. static void st_pinconf_get_direction(struct st_pio_control *pc,
  527. int pin, unsigned long *config)
  528. {
  529. unsigned int oe_value, pu_value, od_value;
  530. if (pc->oe) {
  531. regmap_field_read(pc->oe, &oe_value);
  532. if (oe_value & BIT(pin))
  533. ST_PINCONF_PACK_OE(*config);
  534. }
  535. if (pc->pu) {
  536. regmap_field_read(pc->pu, &pu_value);
  537. if (pu_value & BIT(pin))
  538. ST_PINCONF_PACK_PU(*config);
  539. }
  540. if (pc->od) {
  541. regmap_field_read(pc->od, &od_value);
  542. if (od_value & BIT(pin))
  543. ST_PINCONF_PACK_OD(*config);
  544. }
  545. }
  546. static int st_pinconf_get_retime_packed(struct st_pinctrl *info,
  547. struct st_pio_control *pc, int pin, unsigned long *config)
  548. {
  549. const struct st_pctl_data *data = info->data;
  550. struct st_retime_packed *rt_p = &pc->rt.rt_p;
  551. unsigned int delay_bits, delay, delay0, delay1, val;
  552. int output = ST_PINCONF_UNPACK_OE(*config);
  553. if (!regmap_field_read(rt_p->retime, &val) && (val & BIT(pin)))
  554. ST_PINCONF_PACK_RT(*config);
  555. if (!regmap_field_read(rt_p->clk1notclk0, &val) && (val & BIT(pin)))
  556. ST_PINCONF_PACK_RT_CLK(*config, 1);
  557. if (!regmap_field_read(rt_p->clknotdata, &val) && (val & BIT(pin)))
  558. ST_PINCONF_PACK_RT_CLKNOTDATA(*config);
  559. if (!regmap_field_read(rt_p->double_edge, &val) && (val & BIT(pin)))
  560. ST_PINCONF_PACK_RT_DOUBLE_EDGE(*config);
  561. if (!regmap_field_read(rt_p->invertclk, &val) && (val & BIT(pin)))
  562. ST_PINCONF_PACK_RT_INVERTCLK(*config);
  563. regmap_field_read(rt_p->delay_0, &delay0);
  564. regmap_field_read(rt_p->delay_1, &delay1);
  565. delay_bits = (((delay1 & BIT(pin)) ? 1 : 0) << 1) |
  566. (((delay0 & BIT(pin)) ? 1 : 0));
  567. delay = st_pinconf_bit_to_delay(delay_bits, data, output);
  568. ST_PINCONF_PACK_RT_DELAY(*config, delay);
  569. return 0;
  570. }
  571. static int st_pinconf_get_retime_dedicated(struct st_pinctrl *info,
  572. struct st_pio_control *pc, int pin, unsigned long *config)
  573. {
  574. unsigned int value;
  575. unsigned long delay_bits, delay, rt_clk;
  576. int output = ST_PINCONF_UNPACK_OE(*config);
  577. struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
  578. regmap_field_read(rt_d->rt[pin], &value);
  579. rt_clk = (value & RT_D_CFG_CLK_MASK) >> RT_D_CFG_CLK_SHIFT;
  580. ST_PINCONF_PACK_RT_CLK(*config, rt_clk);
  581. delay_bits = (value & RT_D_CFG_DELAY_MASK) >> RT_D_CFG_DELAY_SHIFT;
  582. delay = st_pinconf_bit_to_delay(delay_bits, info->data, output);
  583. ST_PINCONF_PACK_RT_DELAY(*config, delay);
  584. if (value & RT_D_CFG_CLKNOTDATA_MASK)
  585. ST_PINCONF_PACK_RT_CLKNOTDATA(*config);
  586. if (value & RT_D_CFG_DOUBLE_EDGE_MASK)
  587. ST_PINCONF_PACK_RT_DOUBLE_EDGE(*config);
  588. if (value & RT_D_CFG_INVERTCLK_MASK)
  589. ST_PINCONF_PACK_RT_INVERTCLK(*config);
  590. if (value & RT_D_CFG_RETIME_MASK)
  591. ST_PINCONF_PACK_RT(*config);
  592. return 0;
  593. }
  594. /* GPIO related functions */
  595. static inline void __st_gpio_set(struct st_gpio_bank *bank,
  596. unsigned offset, int value)
  597. {
  598. if (value)
  599. writel(BIT(offset), bank->base + REG_PIO_SET_POUT);
  600. else
  601. writel(BIT(offset), bank->base + REG_PIO_CLR_POUT);
  602. }
  603. static void st_gpio_direction(struct st_gpio_bank *bank,
  604. unsigned int gpio, unsigned int direction)
  605. {
  606. int offset = st_gpio_pin(gpio);
  607. int i = 0;
  608. /**
  609. * There are three configuration registers (PIOn_PC0, PIOn_PC1
  610. * and PIOn_PC2) for each port. These are used to configure the
  611. * PIO port pins. Each pin can be configured as an input, output,
  612. * bidirectional, or alternative function pin. Three bits, one bit
  613. * from each of the three registers, configure the corresponding bit of
  614. * the port. Valid bit settings is:
  615. *
  616. * PC2 PC1 PC0 Direction.
  617. * 0 0 0 [Input Weak pull-up]
  618. * 0 0 or 1 1 [Bidirection]
  619. * 0 1 0 [Output]
  620. * 1 0 0 [Input]
  621. *
  622. * PIOn_SET_PC and PIOn_CLR_PC registers are used to set and clear bits
  623. * individually.
  624. */
  625. for (i = 0; i <= 2; i++) {
  626. if (direction & BIT(i))
  627. writel(BIT(offset), bank->base + REG_PIO_SET_PC(i));
  628. else
  629. writel(BIT(offset), bank->base + REG_PIO_CLR_PC(i));
  630. }
  631. }
  632. static int st_gpio_get(struct gpio_chip *chip, unsigned offset)
  633. {
  634. struct st_gpio_bank *bank = gpiochip_get_data(chip);
  635. return !!(readl(bank->base + REG_PIO_PIN) & BIT(offset));
  636. }
  637. static void st_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  638. {
  639. struct st_gpio_bank *bank = gpiochip_get_data(chip);
  640. __st_gpio_set(bank, offset, value);
  641. }
  642. static int st_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  643. {
  644. pinctrl_gpio_direction_input(chip->base + offset);
  645. return 0;
  646. }
  647. static int st_gpio_direction_output(struct gpio_chip *chip,
  648. unsigned offset, int value)
  649. {
  650. struct st_gpio_bank *bank = gpiochip_get_data(chip);
  651. __st_gpio_set(bank, offset, value);
  652. pinctrl_gpio_direction_output(chip->base + offset);
  653. return 0;
  654. }
  655. static int st_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  656. {
  657. struct st_gpio_bank *bank = gpiochip_get_data(chip);
  658. struct st_pio_control pc = bank->pc;
  659. unsigned long config;
  660. unsigned int direction = 0;
  661. unsigned int function;
  662. unsigned int value;
  663. int i = 0;
  664. /* Alternate function direction is handled by Pinctrl */
  665. function = st_pctl_get_pin_function(&pc, offset);
  666. if (function) {
  667. st_pinconf_get_direction(&pc, offset, &config);
  668. return !ST_PINCONF_UNPACK_OE(config);
  669. }
  670. /*
  671. * GPIO direction is handled differently
  672. * - See st_gpio_direction() above for an explanation
  673. */
  674. for (i = 0; i <= 2; i++) {
  675. value = readl(bank->base + REG_PIO_PC(i));
  676. direction |= ((value >> offset) & 0x1) << i;
  677. }
  678. return (direction == ST_GPIO_DIRECTION_IN);
  679. }
  680. static int st_gpio_xlate(struct gpio_chip *gc,
  681. const struct of_phandle_args *gpiospec, u32 *flags)
  682. {
  683. if (WARN_ON(gc->of_gpio_n_cells < 1))
  684. return -EINVAL;
  685. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  686. return -EINVAL;
  687. if (gpiospec->args[0] > gc->ngpio)
  688. return -EINVAL;
  689. return gpiospec->args[0];
  690. }
  691. /* Pinctrl Groups */
  692. static int st_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  693. {
  694. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  695. return info->ngroups;
  696. }
  697. static const char *st_pctl_get_group_name(struct pinctrl_dev *pctldev,
  698. unsigned selector)
  699. {
  700. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  701. return info->groups[selector].name;
  702. }
  703. static int st_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  704. unsigned selector, const unsigned **pins, unsigned *npins)
  705. {
  706. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  707. if (selector >= info->ngroups)
  708. return -EINVAL;
  709. *pins = info->groups[selector].pins;
  710. *npins = info->groups[selector].npins;
  711. return 0;
  712. }
  713. static const inline struct st_pctl_group *st_pctl_find_group_by_name(
  714. const struct st_pinctrl *info, const char *name)
  715. {
  716. int i;
  717. for (i = 0; i < info->ngroups; i++) {
  718. if (!strcmp(info->groups[i].name, name))
  719. return &info->groups[i];
  720. }
  721. return NULL;
  722. }
  723. static int st_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  724. struct device_node *np, struct pinctrl_map **map, unsigned *num_maps)
  725. {
  726. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  727. const struct st_pctl_group *grp;
  728. struct pinctrl_map *new_map;
  729. struct device_node *parent;
  730. int map_num, i;
  731. grp = st_pctl_find_group_by_name(info, np->name);
  732. if (!grp) {
  733. dev_err(info->dev, "unable to find group for node %s\n",
  734. np->name);
  735. return -EINVAL;
  736. }
  737. map_num = grp->npins + 1;
  738. new_map = devm_kzalloc(pctldev->dev,
  739. sizeof(*new_map) * map_num, GFP_KERNEL);
  740. if (!new_map)
  741. return -ENOMEM;
  742. parent = of_get_parent(np);
  743. if (!parent) {
  744. devm_kfree(pctldev->dev, new_map);
  745. return -EINVAL;
  746. }
  747. *map = new_map;
  748. *num_maps = map_num;
  749. new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
  750. new_map[0].data.mux.function = parent->name;
  751. new_map[0].data.mux.group = np->name;
  752. of_node_put(parent);
  753. /* create config map per pin */
  754. new_map++;
  755. for (i = 0; i < grp->npins; i++) {
  756. new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
  757. new_map[i].data.configs.group_or_pin =
  758. pin_get_name(pctldev, grp->pins[i]);
  759. new_map[i].data.configs.configs = &grp->pin_conf[i].config;
  760. new_map[i].data.configs.num_configs = 1;
  761. }
  762. dev_info(pctldev->dev, "maps: function %s group %s num %d\n",
  763. (*map)->data.mux.function, grp->name, map_num);
  764. return 0;
  765. }
  766. static void st_pctl_dt_free_map(struct pinctrl_dev *pctldev,
  767. struct pinctrl_map *map, unsigned num_maps)
  768. {
  769. }
  770. static struct pinctrl_ops st_pctlops = {
  771. .get_groups_count = st_pctl_get_groups_count,
  772. .get_group_pins = st_pctl_get_group_pins,
  773. .get_group_name = st_pctl_get_group_name,
  774. .dt_node_to_map = st_pctl_dt_node_to_map,
  775. .dt_free_map = st_pctl_dt_free_map,
  776. };
  777. /* Pinmux */
  778. static int st_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  779. {
  780. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  781. return info->nfunctions;
  782. }
  783. static const char *st_pmx_get_fname(struct pinctrl_dev *pctldev,
  784. unsigned selector)
  785. {
  786. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  787. return info->functions[selector].name;
  788. }
  789. static int st_pmx_get_groups(struct pinctrl_dev *pctldev,
  790. unsigned selector, const char * const **grps, unsigned * const ngrps)
  791. {
  792. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  793. *grps = info->functions[selector].groups;
  794. *ngrps = info->functions[selector].ngroups;
  795. return 0;
  796. }
  797. static int st_pmx_set_mux(struct pinctrl_dev *pctldev, unsigned fselector,
  798. unsigned group)
  799. {
  800. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  801. struct st_pinconf *conf = info->groups[group].pin_conf;
  802. struct st_pio_control *pc;
  803. int i;
  804. for (i = 0; i < info->groups[group].npins; i++) {
  805. pc = st_get_pio_control(pctldev, conf[i].pin);
  806. st_pctl_set_function(pc, conf[i].pin, conf[i].altfunc);
  807. }
  808. return 0;
  809. }
  810. static int st_pmx_set_gpio_direction(struct pinctrl_dev *pctldev,
  811. struct pinctrl_gpio_range *range, unsigned gpio,
  812. bool input)
  813. {
  814. struct st_gpio_bank *bank = gpio_range_to_bank(range);
  815. /*
  816. * When a PIO bank is used in its primary function mode (altfunc = 0)
  817. * Output Enable (OE), Open Drain(OD), and Pull Up (PU)
  818. * for the primary PIO functions are driven by the related PIO block
  819. */
  820. st_pctl_set_function(&bank->pc, gpio, 0);
  821. st_gpio_direction(bank, gpio, input ?
  822. ST_GPIO_DIRECTION_IN : ST_GPIO_DIRECTION_OUT);
  823. return 0;
  824. }
  825. static struct pinmux_ops st_pmxops = {
  826. .get_functions_count = st_pmx_get_funcs_count,
  827. .get_function_name = st_pmx_get_fname,
  828. .get_function_groups = st_pmx_get_groups,
  829. .set_mux = st_pmx_set_mux,
  830. .gpio_set_direction = st_pmx_set_gpio_direction,
  831. .strict = true,
  832. };
  833. /* Pinconf */
  834. static void st_pinconf_get_retime(struct st_pinctrl *info,
  835. struct st_pio_control *pc, int pin, unsigned long *config)
  836. {
  837. if (info->data->rt_style == st_retime_style_packed)
  838. st_pinconf_get_retime_packed(info, pc, pin, config);
  839. else if (info->data->rt_style == st_retime_style_dedicated)
  840. if ((BIT(pin) & pc->rt_pin_mask))
  841. st_pinconf_get_retime_dedicated(info, pc,
  842. pin, config);
  843. }
  844. static void st_pinconf_set_retime(struct st_pinctrl *info,
  845. struct st_pio_control *pc, int pin, unsigned long config)
  846. {
  847. if (info->data->rt_style == st_retime_style_packed)
  848. st_pinconf_set_retime_packed(info, pc, config, pin);
  849. else if (info->data->rt_style == st_retime_style_dedicated)
  850. if ((BIT(pin) & pc->rt_pin_mask))
  851. st_pinconf_set_retime_dedicated(info, pc,
  852. config, pin);
  853. }
  854. static int st_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin_id,
  855. unsigned long *configs, unsigned num_configs)
  856. {
  857. int pin = st_gpio_pin(pin_id);
  858. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  859. struct st_pio_control *pc = st_get_pio_control(pctldev, pin_id);
  860. int i;
  861. for (i = 0; i < num_configs; i++) {
  862. st_pinconf_set_config(pc, pin, configs[i]);
  863. st_pinconf_set_retime(info, pc, pin, configs[i]);
  864. } /* for each config */
  865. return 0;
  866. }
  867. static int st_pinconf_get(struct pinctrl_dev *pctldev,
  868. unsigned pin_id, unsigned long *config)
  869. {
  870. int pin = st_gpio_pin(pin_id);
  871. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  872. struct st_pio_control *pc = st_get_pio_control(pctldev, pin_id);
  873. *config = 0;
  874. st_pinconf_get_direction(pc, pin, config);
  875. st_pinconf_get_retime(info, pc, pin, config);
  876. return 0;
  877. }
  878. static void st_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  879. struct seq_file *s, unsigned pin_id)
  880. {
  881. struct st_pio_control *pc;
  882. unsigned long config;
  883. unsigned int function;
  884. int offset = st_gpio_pin(pin_id);
  885. char f[16];
  886. mutex_unlock(&pctldev->mutex);
  887. pc = st_get_pio_control(pctldev, pin_id);
  888. st_pinconf_get(pctldev, pin_id, &config);
  889. mutex_lock(&pctldev->mutex);
  890. function = st_pctl_get_pin_function(pc, offset);
  891. if (function)
  892. snprintf(f, 10, "Alt Fn %d", function);
  893. else
  894. snprintf(f, 5, "GPIO");
  895. seq_printf(s, "[OE:%d,PU:%ld,OD:%ld]\t%s\n"
  896. "\t\t[retime:%ld,invclk:%ld,clknotdat:%ld,"
  897. "de:%ld,rt-clk:%ld,rt-delay:%ld]",
  898. !st_gpio_get_direction(&pc_to_bank(pc)->gpio_chip, offset),
  899. ST_PINCONF_UNPACK_PU(config),
  900. ST_PINCONF_UNPACK_OD(config),
  901. f,
  902. ST_PINCONF_UNPACK_RT(config),
  903. ST_PINCONF_UNPACK_RT_INVERTCLK(config),
  904. ST_PINCONF_UNPACK_RT_CLKNOTDATA(config),
  905. ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config),
  906. ST_PINCONF_UNPACK_RT_CLK(config),
  907. ST_PINCONF_UNPACK_RT_DELAY(config));
  908. }
  909. static struct pinconf_ops st_confops = {
  910. .pin_config_get = st_pinconf_get,
  911. .pin_config_set = st_pinconf_set,
  912. .pin_config_dbg_show = st_pinconf_dbg_show,
  913. };
  914. static void st_pctl_dt_child_count(struct st_pinctrl *info,
  915. struct device_node *np)
  916. {
  917. struct device_node *child;
  918. for_each_child_of_node(np, child) {
  919. if (of_property_read_bool(child, "gpio-controller")) {
  920. info->nbanks++;
  921. } else {
  922. info->nfunctions++;
  923. info->ngroups += of_get_child_count(child);
  924. }
  925. }
  926. }
  927. static int st_pctl_dt_setup_retime_packed(struct st_pinctrl *info,
  928. int bank, struct st_pio_control *pc)
  929. {
  930. struct device *dev = info->dev;
  931. struct regmap *rm = info->regmap;
  932. const struct st_pctl_data *data = info->data;
  933. /* 2 registers per bank */
  934. int reg = (data->rt + bank * RT_P_CFGS_PER_BANK) * 4;
  935. struct st_retime_packed *rt_p = &pc->rt.rt_p;
  936. /* cfg0 */
  937. struct reg_field clk1notclk0 = RT_P_CFG0_CLK1NOTCLK0_FIELD(reg);
  938. struct reg_field delay_0 = RT_P_CFG0_DELAY_0_FIELD(reg);
  939. struct reg_field delay_1 = RT_P_CFG0_DELAY_1_FIELD(reg);
  940. /* cfg1 */
  941. struct reg_field invertclk = RT_P_CFG1_INVERTCLK_FIELD(reg + 4);
  942. struct reg_field retime = RT_P_CFG1_RETIME_FIELD(reg + 4);
  943. struct reg_field clknotdata = RT_P_CFG1_CLKNOTDATA_FIELD(reg + 4);
  944. struct reg_field double_edge = RT_P_CFG1_DOUBLE_EDGE_FIELD(reg + 4);
  945. rt_p->clk1notclk0 = devm_regmap_field_alloc(dev, rm, clk1notclk0);
  946. rt_p->delay_0 = devm_regmap_field_alloc(dev, rm, delay_0);
  947. rt_p->delay_1 = devm_regmap_field_alloc(dev, rm, delay_1);
  948. rt_p->invertclk = devm_regmap_field_alloc(dev, rm, invertclk);
  949. rt_p->retime = devm_regmap_field_alloc(dev, rm, retime);
  950. rt_p->clknotdata = devm_regmap_field_alloc(dev, rm, clknotdata);
  951. rt_p->double_edge = devm_regmap_field_alloc(dev, rm, double_edge);
  952. if (IS_ERR(rt_p->clk1notclk0) || IS_ERR(rt_p->delay_0) ||
  953. IS_ERR(rt_p->delay_1) || IS_ERR(rt_p->invertclk) ||
  954. IS_ERR(rt_p->retime) || IS_ERR(rt_p->clknotdata) ||
  955. IS_ERR(rt_p->double_edge))
  956. return -EINVAL;
  957. return 0;
  958. }
  959. static int st_pctl_dt_setup_retime_dedicated(struct st_pinctrl *info,
  960. int bank, struct st_pio_control *pc)
  961. {
  962. struct device *dev = info->dev;
  963. struct regmap *rm = info->regmap;
  964. const struct st_pctl_data *data = info->data;
  965. /* 8 registers per bank */
  966. int reg_offset = (data->rt + bank * RT_D_CFGS_PER_BANK) * 4;
  967. struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
  968. unsigned int j;
  969. u32 pin_mask = pc->rt_pin_mask;
  970. for (j = 0; j < RT_D_CFGS_PER_BANK; j++) {
  971. if (BIT(j) & pin_mask) {
  972. struct reg_field reg = REG_FIELD(reg_offset, 0, 31);
  973. rt_d->rt[j] = devm_regmap_field_alloc(dev, rm, reg);
  974. if (IS_ERR(rt_d->rt[j]))
  975. return -EINVAL;
  976. reg_offset += 4;
  977. }
  978. }
  979. return 0;
  980. }
  981. static int st_pctl_dt_setup_retime(struct st_pinctrl *info,
  982. int bank, struct st_pio_control *pc)
  983. {
  984. const struct st_pctl_data *data = info->data;
  985. if (data->rt_style == st_retime_style_packed)
  986. return st_pctl_dt_setup_retime_packed(info, bank, pc);
  987. else if (data->rt_style == st_retime_style_dedicated)
  988. return st_pctl_dt_setup_retime_dedicated(info, bank, pc);
  989. return -EINVAL;
  990. }
  991. static struct regmap_field *st_pc_get_value(struct device *dev,
  992. struct regmap *regmap, int bank,
  993. int data, int lsb, int msb)
  994. {
  995. struct reg_field reg = REG_FIELD((data + bank) * 4, lsb, msb);
  996. if (data < 0)
  997. return NULL;
  998. return devm_regmap_field_alloc(dev, regmap, reg);
  999. }
  1000. static void st_parse_syscfgs(struct st_pinctrl *info, int bank,
  1001. struct device_node *np)
  1002. {
  1003. const struct st_pctl_data *data = info->data;
  1004. /**
  1005. * For a given shared register like OE/PU/OD, there are 8 bits per bank
  1006. * 0:7 belongs to bank0, 8:15 belongs to bank1 ...
  1007. * So each register is shared across 4 banks.
  1008. */
  1009. int lsb = (bank%4) * ST_GPIO_PINS_PER_BANK;
  1010. int msb = lsb + ST_GPIO_PINS_PER_BANK - 1;
  1011. struct st_pio_control *pc = &info->banks[bank].pc;
  1012. struct device *dev = info->dev;
  1013. struct regmap *regmap = info->regmap;
  1014. pc->alt = st_pc_get_value(dev, regmap, bank, data->alt, 0, 31);
  1015. pc->oe = st_pc_get_value(dev, regmap, bank/4, data->oe, lsb, msb);
  1016. pc->pu = st_pc_get_value(dev, regmap, bank/4, data->pu, lsb, msb);
  1017. pc->od = st_pc_get_value(dev, regmap, bank/4, data->od, lsb, msb);
  1018. /* retime avaiable for all pins by default */
  1019. pc->rt_pin_mask = 0xff;
  1020. of_property_read_u32(np, "st,retime-pin-mask", &pc->rt_pin_mask);
  1021. st_pctl_dt_setup_retime(info, bank, pc);
  1022. return;
  1023. }
  1024. /*
  1025. * Each pin is represented in of the below forms.
  1026. * <bank offset mux direction rt_type rt_delay rt_clk>
  1027. */
  1028. static int st_pctl_dt_parse_groups(struct device_node *np,
  1029. struct st_pctl_group *grp, struct st_pinctrl *info, int idx)
  1030. {
  1031. /* bank pad direction val altfunction */
  1032. const __be32 *list;
  1033. struct property *pp;
  1034. struct st_pinconf *conf;
  1035. struct device_node *pins;
  1036. int i = 0, npins = 0, nr_props;
  1037. pins = of_get_child_by_name(np, "st,pins");
  1038. if (!pins)
  1039. return -ENODATA;
  1040. for_each_property_of_node(pins, pp) {
  1041. /* Skip those we do not want to proceed */
  1042. if (!strcmp(pp->name, "name"))
  1043. continue;
  1044. if (pp && (pp->length/sizeof(__be32)) >= OF_GPIO_ARGS_MIN) {
  1045. npins++;
  1046. } else {
  1047. pr_warn("Invalid st,pins in %s node\n", np->name);
  1048. return -EINVAL;
  1049. }
  1050. }
  1051. grp->npins = npins;
  1052. grp->name = np->name;
  1053. grp->pins = devm_kzalloc(info->dev, npins * sizeof(u32), GFP_KERNEL);
  1054. grp->pin_conf = devm_kzalloc(info->dev,
  1055. npins * sizeof(*conf), GFP_KERNEL);
  1056. if (!grp->pins || !grp->pin_conf)
  1057. return -ENOMEM;
  1058. /* <bank offset mux direction rt_type rt_delay rt_clk> */
  1059. for_each_property_of_node(pins, pp) {
  1060. if (!strcmp(pp->name, "name"))
  1061. continue;
  1062. nr_props = pp->length/sizeof(u32);
  1063. list = pp->value;
  1064. conf = &grp->pin_conf[i];
  1065. /* bank & offset */
  1066. be32_to_cpup(list++);
  1067. be32_to_cpup(list++);
  1068. conf->pin = of_get_named_gpio(pins, pp->name, 0);
  1069. conf->name = pp->name;
  1070. grp->pins[i] = conf->pin;
  1071. /* mux */
  1072. conf->altfunc = be32_to_cpup(list++);
  1073. conf->config = 0;
  1074. /* direction */
  1075. conf->config |= be32_to_cpup(list++);
  1076. /* rt_type rt_delay rt_clk */
  1077. if (nr_props >= OF_GPIO_ARGS_MIN + OF_RT_ARGS_MIN) {
  1078. /* rt_type */
  1079. conf->config |= be32_to_cpup(list++);
  1080. /* rt_delay */
  1081. conf->config |= be32_to_cpup(list++);
  1082. /* rt_clk */
  1083. if (nr_props > OF_GPIO_ARGS_MIN + OF_RT_ARGS_MIN)
  1084. conf->config |= be32_to_cpup(list++);
  1085. }
  1086. i++;
  1087. }
  1088. of_node_put(pins);
  1089. return 0;
  1090. }
  1091. static int st_pctl_parse_functions(struct device_node *np,
  1092. struct st_pinctrl *info, u32 index, int *grp_index)
  1093. {
  1094. struct device_node *child;
  1095. struct st_pmx_func *func;
  1096. struct st_pctl_group *grp;
  1097. int ret, i;
  1098. func = &info->functions[index];
  1099. func->name = np->name;
  1100. func->ngroups = of_get_child_count(np);
  1101. if (func->ngroups == 0) {
  1102. dev_err(info->dev, "No groups defined\n");
  1103. return -EINVAL;
  1104. }
  1105. func->groups = devm_kzalloc(info->dev,
  1106. func->ngroups * sizeof(char *), GFP_KERNEL);
  1107. if (!func->groups)
  1108. return -ENOMEM;
  1109. i = 0;
  1110. for_each_child_of_node(np, child) {
  1111. func->groups[i] = child->name;
  1112. grp = &info->groups[*grp_index];
  1113. *grp_index += 1;
  1114. ret = st_pctl_dt_parse_groups(child, grp, info, i++);
  1115. if (ret)
  1116. return ret;
  1117. }
  1118. dev_info(info->dev, "Function[%d\t name:%s,\tgroups:%d]\n",
  1119. index, func->name, func->ngroups);
  1120. return 0;
  1121. }
  1122. static void st_gpio_irq_mask(struct irq_data *d)
  1123. {
  1124. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1125. struct st_gpio_bank *bank = gpiochip_get_data(gc);
  1126. writel(BIT(d->hwirq), bank->base + REG_PIO_CLR_PMASK);
  1127. }
  1128. static void st_gpio_irq_unmask(struct irq_data *d)
  1129. {
  1130. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1131. struct st_gpio_bank *bank = gpiochip_get_data(gc);
  1132. writel(BIT(d->hwirq), bank->base + REG_PIO_SET_PMASK);
  1133. }
  1134. static int st_gpio_irq_set_type(struct irq_data *d, unsigned type)
  1135. {
  1136. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1137. struct st_gpio_bank *bank = gpiochip_get_data(gc);
  1138. unsigned long flags;
  1139. int comp, pin = d->hwirq;
  1140. u32 val;
  1141. u32 pin_edge_conf = 0;
  1142. switch (type) {
  1143. case IRQ_TYPE_LEVEL_HIGH:
  1144. comp = 0;
  1145. break;
  1146. case IRQ_TYPE_EDGE_FALLING:
  1147. comp = 0;
  1148. pin_edge_conf = ST_IRQ_FALLING_EDGE_CONF(pin);
  1149. break;
  1150. case IRQ_TYPE_LEVEL_LOW:
  1151. comp = 1;
  1152. break;
  1153. case IRQ_TYPE_EDGE_RISING:
  1154. comp = 1;
  1155. pin_edge_conf = ST_IRQ_RISING_EDGE_CONF(pin);
  1156. break;
  1157. case IRQ_TYPE_EDGE_BOTH:
  1158. comp = st_gpio_get(&bank->gpio_chip, pin);
  1159. pin_edge_conf = ST_IRQ_BOTH_EDGE_CONF(pin);
  1160. break;
  1161. default:
  1162. return -EINVAL;
  1163. }
  1164. spin_lock_irqsave(&bank->lock, flags);
  1165. bank->irq_edge_conf &= ~(ST_IRQ_EDGE_MASK << (
  1166. pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN));
  1167. bank->irq_edge_conf |= pin_edge_conf;
  1168. spin_unlock_irqrestore(&bank->lock, flags);
  1169. val = readl(bank->base + REG_PIO_PCOMP);
  1170. val &= ~BIT(pin);
  1171. val |= (comp << pin);
  1172. writel(val, bank->base + REG_PIO_PCOMP);
  1173. return 0;
  1174. }
  1175. /*
  1176. * As edge triggers are not supported at hardware level, it is supported by
  1177. * software by exploiting the level trigger support in hardware.
  1178. *
  1179. * Steps for detection raising edge interrupt in software.
  1180. *
  1181. * Step 1: CONFIGURE pin to detect level LOW interrupts.
  1182. *
  1183. * Step 2: DETECT level LOW interrupt and in irqmux/gpio bank interrupt handler,
  1184. * if the value of pin is low, then CONFIGURE pin for level HIGH interrupt.
  1185. * IGNORE calling the actual interrupt handler for the pin at this stage.
  1186. *
  1187. * Step 3: DETECT level HIGH interrupt and in irqmux/gpio-bank interrupt handler
  1188. * if the value of pin is HIGH, CONFIGURE pin for level LOW interrupt and then
  1189. * DISPATCH the interrupt to the interrupt handler of the pin.
  1190. *
  1191. * step-1 ________ __________
  1192. * | | step - 3
  1193. * | |
  1194. * step -2 |_____|
  1195. *
  1196. * falling edge is also detected int the same way.
  1197. *
  1198. */
  1199. static void __gpio_irq_handler(struct st_gpio_bank *bank)
  1200. {
  1201. unsigned long port_in, port_mask, port_comp, active_irqs;
  1202. unsigned long bank_edge_mask, flags;
  1203. int n, val, ecfg;
  1204. spin_lock_irqsave(&bank->lock, flags);
  1205. bank_edge_mask = bank->irq_edge_conf;
  1206. spin_unlock_irqrestore(&bank->lock, flags);
  1207. for (;;) {
  1208. port_in = readl(bank->base + REG_PIO_PIN);
  1209. port_comp = readl(bank->base + REG_PIO_PCOMP);
  1210. port_mask = readl(bank->base + REG_PIO_PMASK);
  1211. active_irqs = (port_in ^ port_comp) & port_mask;
  1212. if (active_irqs == 0)
  1213. break;
  1214. for_each_set_bit(n, &active_irqs, BITS_PER_LONG) {
  1215. /* check if we are detecting fake edges ... */
  1216. ecfg = ST_IRQ_EDGE_CONF(bank_edge_mask, n);
  1217. if (ecfg) {
  1218. /* edge detection. */
  1219. val = st_gpio_get(&bank->gpio_chip, n);
  1220. writel(BIT(n),
  1221. val ? bank->base + REG_PIO_SET_PCOMP :
  1222. bank->base + REG_PIO_CLR_PCOMP);
  1223. if (ecfg != ST_IRQ_EDGE_BOTH &&
  1224. !((ecfg & ST_IRQ_EDGE_FALLING) ^ val))
  1225. continue;
  1226. }
  1227. generic_handle_irq(irq_find_mapping(bank->gpio_chip.irqdomain, n));
  1228. }
  1229. }
  1230. }
  1231. static void st_gpio_irq_handler(struct irq_desc *desc)
  1232. {
  1233. /* interrupt dedicated per bank */
  1234. struct irq_chip *chip = irq_desc_get_chip(desc);
  1235. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  1236. struct st_gpio_bank *bank = gpiochip_get_data(gc);
  1237. chained_irq_enter(chip, desc);
  1238. __gpio_irq_handler(bank);
  1239. chained_irq_exit(chip, desc);
  1240. }
  1241. static void st_gpio_irqmux_handler(struct irq_desc *desc)
  1242. {
  1243. struct irq_chip *chip = irq_desc_get_chip(desc);
  1244. struct st_pinctrl *info = irq_desc_get_handler_data(desc);
  1245. unsigned long status;
  1246. int n;
  1247. chained_irq_enter(chip, desc);
  1248. status = readl(info->irqmux_base);
  1249. for_each_set_bit(n, &status, info->nbanks)
  1250. __gpio_irq_handler(&info->banks[n]);
  1251. chained_irq_exit(chip, desc);
  1252. }
  1253. static struct gpio_chip st_gpio_template = {
  1254. .request = gpiochip_generic_request,
  1255. .free = gpiochip_generic_free,
  1256. .get = st_gpio_get,
  1257. .set = st_gpio_set,
  1258. .direction_input = st_gpio_direction_input,
  1259. .direction_output = st_gpio_direction_output,
  1260. .get_direction = st_gpio_get_direction,
  1261. .ngpio = ST_GPIO_PINS_PER_BANK,
  1262. .of_gpio_n_cells = 1,
  1263. .of_xlate = st_gpio_xlate,
  1264. };
  1265. static struct irq_chip st_gpio_irqchip = {
  1266. .name = "GPIO",
  1267. .irq_disable = st_gpio_irq_mask,
  1268. .irq_mask = st_gpio_irq_mask,
  1269. .irq_unmask = st_gpio_irq_unmask,
  1270. .irq_set_type = st_gpio_irq_set_type,
  1271. .flags = IRQCHIP_SKIP_SET_WAKE,
  1272. };
  1273. static int st_gpiolib_register_bank(struct st_pinctrl *info,
  1274. int bank_nr, struct device_node *np)
  1275. {
  1276. struct st_gpio_bank *bank = &info->banks[bank_nr];
  1277. struct pinctrl_gpio_range *range = &bank->range;
  1278. struct device *dev = info->dev;
  1279. int bank_num = of_alias_get_id(np, "gpio");
  1280. struct resource res, irq_res;
  1281. int gpio_irq = 0, err;
  1282. if (of_address_to_resource(np, 0, &res))
  1283. return -ENODEV;
  1284. bank->base = devm_ioremap_resource(dev, &res);
  1285. if (IS_ERR(bank->base))
  1286. return PTR_ERR(bank->base);
  1287. bank->gpio_chip = st_gpio_template;
  1288. bank->gpio_chip.base = bank_num * ST_GPIO_PINS_PER_BANK;
  1289. bank->gpio_chip.ngpio = ST_GPIO_PINS_PER_BANK;
  1290. bank->gpio_chip.of_node = np;
  1291. bank->gpio_chip.parent = dev;
  1292. spin_lock_init(&bank->lock);
  1293. of_property_read_string(np, "st,bank-name", &range->name);
  1294. bank->gpio_chip.label = range->name;
  1295. range->id = bank_num;
  1296. range->pin_base = range->base = range->id * ST_GPIO_PINS_PER_BANK;
  1297. range->npins = bank->gpio_chip.ngpio;
  1298. range->gc = &bank->gpio_chip;
  1299. err = gpiochip_add_data(&bank->gpio_chip, bank);
  1300. if (err) {
  1301. dev_err(dev, "Failed to add gpiochip(%d)!\n", bank_num);
  1302. return err;
  1303. }
  1304. dev_info(dev, "%s bank added.\n", range->name);
  1305. /**
  1306. * GPIO bank can have one of the two possible types of
  1307. * interrupt-wirings.
  1308. *
  1309. * First type is via irqmux, single interrupt is used by multiple
  1310. * gpio banks. This reduces number of overall interrupts numbers
  1311. * required. All these banks belong to a single pincontroller.
  1312. * _________
  1313. * | |----> [gpio-bank (n) ]
  1314. * | |----> [gpio-bank (n + 1)]
  1315. * [irqN]-- | irq-mux |----> [gpio-bank (n + 2)]
  1316. * | |----> [gpio-bank (... )]
  1317. * |_________|----> [gpio-bank (n + 7)]
  1318. *
  1319. * Second type has a dedicated interrupt per each gpio bank.
  1320. *
  1321. * [irqN]----> [gpio-bank (n)]
  1322. */
  1323. if (of_irq_to_resource(np, 0, &irq_res)) {
  1324. gpio_irq = irq_res.start;
  1325. gpiochip_set_chained_irqchip(&bank->gpio_chip, &st_gpio_irqchip,
  1326. gpio_irq, st_gpio_irq_handler);
  1327. }
  1328. if (info->irqmux_base || gpio_irq > 0) {
  1329. err = gpiochip_irqchip_add(&bank->gpio_chip, &st_gpio_irqchip,
  1330. 0, handle_simple_irq,
  1331. IRQ_TYPE_LEVEL_LOW);
  1332. if (err) {
  1333. gpiochip_remove(&bank->gpio_chip);
  1334. dev_info(dev, "could not add irqchip\n");
  1335. return err;
  1336. }
  1337. } else {
  1338. dev_info(dev, "No IRQ support for %s bank\n", np->full_name);
  1339. }
  1340. return 0;
  1341. }
  1342. static const struct of_device_id st_pctl_of_match[] = {
  1343. { .compatible = "st,stih415-sbc-pinctrl", .data = &stih415_sbc_data },
  1344. { .compatible = "st,stih415-rear-pinctrl", .data = &stih415_rear_data },
  1345. { .compatible = "st,stih415-left-pinctrl", .data = &stih415_left_data },
  1346. { .compatible = "st,stih415-right-pinctrl",
  1347. .data = &stih415_right_data },
  1348. { .compatible = "st,stih415-front-pinctrl",
  1349. .data = &stih415_front_data },
  1350. { .compatible = "st,stih416-sbc-pinctrl", .data = &stih416_data},
  1351. { .compatible = "st,stih416-front-pinctrl", .data = &stih416_data},
  1352. { .compatible = "st,stih416-rear-pinctrl", .data = &stih416_data},
  1353. { .compatible = "st,stih416-fvdp-fe-pinctrl", .data = &stih416_data},
  1354. { .compatible = "st,stih416-fvdp-lite-pinctrl", .data = &stih416_data},
  1355. { .compatible = "st,stih407-sbc-pinctrl", .data = &stih416_data},
  1356. { .compatible = "st,stih407-front-pinctrl", .data = &stih416_data},
  1357. { .compatible = "st,stih407-rear-pinctrl", .data = &stih416_data},
  1358. { .compatible = "st,stih407-flash-pinctrl", .data = &stih407_flashdata},
  1359. { /* sentinel */ }
  1360. };
  1361. static int st_pctl_probe_dt(struct platform_device *pdev,
  1362. struct pinctrl_desc *pctl_desc, struct st_pinctrl *info)
  1363. {
  1364. int ret = 0;
  1365. int i = 0, j = 0, k = 0, bank;
  1366. struct pinctrl_pin_desc *pdesc;
  1367. struct device_node *np = pdev->dev.of_node;
  1368. struct device_node *child;
  1369. int grp_index = 0;
  1370. int irq = 0;
  1371. struct resource *res;
  1372. st_pctl_dt_child_count(info, np);
  1373. if (!info->nbanks) {
  1374. dev_err(&pdev->dev, "you need atleast one gpio bank\n");
  1375. return -EINVAL;
  1376. }
  1377. dev_info(&pdev->dev, "nbanks = %d\n", info->nbanks);
  1378. dev_info(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  1379. dev_info(&pdev->dev, "ngroups = %d\n", info->ngroups);
  1380. info->functions = devm_kzalloc(&pdev->dev,
  1381. info->nfunctions * sizeof(*info->functions), GFP_KERNEL);
  1382. info->groups = devm_kzalloc(&pdev->dev,
  1383. info->ngroups * sizeof(*info->groups) , GFP_KERNEL);
  1384. info->banks = devm_kzalloc(&pdev->dev,
  1385. info->nbanks * sizeof(*info->banks), GFP_KERNEL);
  1386. if (!info->functions || !info->groups || !info->banks)
  1387. return -ENOMEM;
  1388. info->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  1389. if (IS_ERR(info->regmap)) {
  1390. dev_err(info->dev, "No syscfg phandle specified\n");
  1391. return PTR_ERR(info->regmap);
  1392. }
  1393. info->data = of_match_node(st_pctl_of_match, np)->data;
  1394. irq = platform_get_irq(pdev, 0);
  1395. if (irq > 0) {
  1396. res = platform_get_resource_byname(pdev,
  1397. IORESOURCE_MEM, "irqmux");
  1398. info->irqmux_base = devm_ioremap_resource(&pdev->dev, res);
  1399. if (IS_ERR(info->irqmux_base))
  1400. return PTR_ERR(info->irqmux_base);
  1401. irq_set_chained_handler_and_data(irq, st_gpio_irqmux_handler,
  1402. info);
  1403. }
  1404. pctl_desc->npins = info->nbanks * ST_GPIO_PINS_PER_BANK;
  1405. pdesc = devm_kzalloc(&pdev->dev,
  1406. sizeof(*pdesc) * pctl_desc->npins, GFP_KERNEL);
  1407. if (!pdesc)
  1408. return -ENOMEM;
  1409. pctl_desc->pins = pdesc;
  1410. bank = 0;
  1411. for_each_child_of_node(np, child) {
  1412. if (of_property_read_bool(child, "gpio-controller")) {
  1413. const char *bank_name = NULL;
  1414. ret = st_gpiolib_register_bank(info, bank, child);
  1415. if (ret)
  1416. return ret;
  1417. k = info->banks[bank].range.pin_base;
  1418. bank_name = info->banks[bank].range.name;
  1419. for (j = 0; j < ST_GPIO_PINS_PER_BANK; j++, k++) {
  1420. pdesc->number = k;
  1421. pdesc->name = kasprintf(GFP_KERNEL, "%s[%d]",
  1422. bank_name, j);
  1423. pdesc++;
  1424. }
  1425. st_parse_syscfgs(info, bank, child);
  1426. bank++;
  1427. } else {
  1428. ret = st_pctl_parse_functions(child, info,
  1429. i++, &grp_index);
  1430. if (ret) {
  1431. dev_err(&pdev->dev, "No functions found.\n");
  1432. return ret;
  1433. }
  1434. }
  1435. }
  1436. return 0;
  1437. }
  1438. static int st_pctl_probe(struct platform_device *pdev)
  1439. {
  1440. struct st_pinctrl *info;
  1441. struct pinctrl_desc *pctl_desc;
  1442. int ret, i;
  1443. if (!pdev->dev.of_node) {
  1444. dev_err(&pdev->dev, "device node not found.\n");
  1445. return -EINVAL;
  1446. }
  1447. pctl_desc = devm_kzalloc(&pdev->dev, sizeof(*pctl_desc), GFP_KERNEL);
  1448. if (!pctl_desc)
  1449. return -ENOMEM;
  1450. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  1451. if (!info)
  1452. return -ENOMEM;
  1453. info->dev = &pdev->dev;
  1454. platform_set_drvdata(pdev, info);
  1455. ret = st_pctl_probe_dt(pdev, pctl_desc, info);
  1456. if (ret)
  1457. return ret;
  1458. pctl_desc->owner = THIS_MODULE;
  1459. pctl_desc->pctlops = &st_pctlops;
  1460. pctl_desc->pmxops = &st_pmxops;
  1461. pctl_desc->confops = &st_confops;
  1462. pctl_desc->name = dev_name(&pdev->dev);
  1463. info->pctl = pinctrl_register(pctl_desc, &pdev->dev, info);
  1464. if (IS_ERR(info->pctl)) {
  1465. dev_err(&pdev->dev, "Failed pinctrl registration\n");
  1466. return PTR_ERR(info->pctl);
  1467. }
  1468. for (i = 0; i < info->nbanks; i++)
  1469. pinctrl_add_gpio_range(info->pctl, &info->banks[i].range);
  1470. return 0;
  1471. }
  1472. static struct platform_driver st_pctl_driver = {
  1473. .driver = {
  1474. .name = "st-pinctrl",
  1475. .of_match_table = st_pctl_of_match,
  1476. },
  1477. .probe = st_pctl_probe,
  1478. };
  1479. static int __init st_pctl_init(void)
  1480. {
  1481. return platform_driver_register(&st_pctl_driver);
  1482. }
  1483. arch_initcall(st_pctl_init);