pinctrl-st.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 STMicroelectronics (R&D) Limited.
  4. * Authors:
  5. * Srinivas Kandagatla <srinivas.kandagatla@st.com>
  6. */
  7. #include <linux/err.h>
  8. #include <linux/gpio/driver.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/slab.h>
  20. #include <linux/string_helpers.h>
  21. #include <linux/pinctrl/consumer.h>
  22. #include <linux/pinctrl/pinconf.h>
  23. #include <linux/pinctrl/pinctrl.h>
  24. #include <linux/pinctrl/pinmux.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. static const unsigned int stih407_delays[] = {0, 300, 500, 750, 1000, 1250,
  302. 1500, 1750, 2000, 2250, 2500, 2750, 3000, 3250 };
  303. static const struct st_pctl_data stih407_data = {
  304. .rt_style = st_retime_style_dedicated,
  305. .input_delays = stih407_delays,
  306. .ninput_delays = ARRAY_SIZE(stih407_delays),
  307. .output_delays = stih407_delays,
  308. .noutput_delays = ARRAY_SIZE(stih407_delays),
  309. .alt = 0, .oe = 40, .pu = 50, .od = 60, .rt = 100,
  310. };
  311. static const struct st_pctl_data stih407_flashdata = {
  312. .rt_style = st_retime_style_none,
  313. .input_delays = stih407_delays,
  314. .ninput_delays = ARRAY_SIZE(stih407_delays),
  315. .output_delays = stih407_delays,
  316. .noutput_delays = ARRAY_SIZE(stih407_delays),
  317. .alt = 0,
  318. .oe = -1, /* Not Available */
  319. .pu = -1, /* Not Available */
  320. .od = 60,
  321. .rt = 100,
  322. };
  323. static struct st_pio_control *st_get_pio_control(
  324. struct pinctrl_dev *pctldev, int pin)
  325. {
  326. struct pinctrl_gpio_range *range =
  327. pinctrl_find_gpio_range_from_pin(pctldev, pin);
  328. struct st_gpio_bank *bank = gpio_range_to_bank(range);
  329. return &bank->pc;
  330. }
  331. /* Low level functions.. */
  332. static inline int st_gpio_pin(int gpio)
  333. {
  334. return gpio%ST_GPIO_PINS_PER_BANK;
  335. }
  336. static void st_pinconf_set_config(struct st_pio_control *pc,
  337. int pin, unsigned long config)
  338. {
  339. struct regmap_field *output_enable = pc->oe;
  340. struct regmap_field *pull_up = pc->pu;
  341. struct regmap_field *open_drain = pc->od;
  342. unsigned int oe_value, pu_value, od_value;
  343. unsigned long mask = BIT(pin);
  344. if (output_enable) {
  345. regmap_field_read(output_enable, &oe_value);
  346. oe_value &= ~mask;
  347. if (config & ST_PINCONF_OE)
  348. oe_value |= mask;
  349. regmap_field_write(output_enable, oe_value);
  350. }
  351. if (pull_up) {
  352. regmap_field_read(pull_up, &pu_value);
  353. pu_value &= ~mask;
  354. if (config & ST_PINCONF_PU)
  355. pu_value |= mask;
  356. regmap_field_write(pull_up, pu_value);
  357. }
  358. if (open_drain) {
  359. regmap_field_read(open_drain, &od_value);
  360. od_value &= ~mask;
  361. if (config & ST_PINCONF_OD)
  362. od_value |= mask;
  363. regmap_field_write(open_drain, od_value);
  364. }
  365. }
  366. static void st_pctl_set_function(struct st_pio_control *pc,
  367. int pin_id, int function)
  368. {
  369. struct regmap_field *alt = pc->alt;
  370. unsigned int val;
  371. int pin = st_gpio_pin(pin_id);
  372. int offset = pin * 4;
  373. if (!alt)
  374. return;
  375. regmap_field_read(alt, &val);
  376. val &= ~(0xf << offset);
  377. val |= function << offset;
  378. regmap_field_write(alt, val);
  379. }
  380. static unsigned int st_pctl_get_pin_function(struct st_pio_control *pc, int pin)
  381. {
  382. struct regmap_field *alt = pc->alt;
  383. unsigned int val;
  384. int offset = pin * 4;
  385. if (!alt)
  386. return 0;
  387. regmap_field_read(alt, &val);
  388. return (val >> offset) & 0xf;
  389. }
  390. static unsigned long st_pinconf_delay_to_bit(unsigned int delay,
  391. const struct st_pctl_data *data, unsigned long config)
  392. {
  393. const unsigned int *delay_times;
  394. int num_delay_times, i, closest_index = -1;
  395. unsigned int closest_divergence = UINT_MAX;
  396. if (ST_PINCONF_UNPACK_OE(config)) {
  397. delay_times = data->output_delays;
  398. num_delay_times = data->noutput_delays;
  399. } else {
  400. delay_times = data->input_delays;
  401. num_delay_times = data->ninput_delays;
  402. }
  403. for (i = 0; i < num_delay_times; i++) {
  404. unsigned int divergence = abs(delay - delay_times[i]);
  405. if (divergence == 0)
  406. return i;
  407. if (divergence < closest_divergence) {
  408. closest_divergence = divergence;
  409. closest_index = i;
  410. }
  411. }
  412. pr_warn("Attempt to set delay %d, closest available %d\n",
  413. delay, delay_times[closest_index]);
  414. return closest_index;
  415. }
  416. static unsigned long st_pinconf_bit_to_delay(unsigned int index,
  417. const struct st_pctl_data *data, unsigned long output)
  418. {
  419. const unsigned int *delay_times;
  420. int num_delay_times;
  421. if (output) {
  422. delay_times = data->output_delays;
  423. num_delay_times = data->noutput_delays;
  424. } else {
  425. delay_times = data->input_delays;
  426. num_delay_times = data->ninput_delays;
  427. }
  428. if (index < num_delay_times) {
  429. return delay_times[index];
  430. } else {
  431. pr_warn("Delay not found in/out delay list\n");
  432. return 0;
  433. }
  434. }
  435. static void st_regmap_field_bit_set_clear_pin(struct regmap_field *field,
  436. int enable, int pin)
  437. {
  438. unsigned int val = 0;
  439. regmap_field_read(field, &val);
  440. if (enable)
  441. val |= BIT(pin);
  442. else
  443. val &= ~BIT(pin);
  444. regmap_field_write(field, val);
  445. }
  446. static void st_pinconf_set_retime_packed(struct st_pinctrl *info,
  447. struct st_pio_control *pc, unsigned long config, int pin)
  448. {
  449. const struct st_pctl_data *data = info->data;
  450. struct st_retime_packed *rt_p = &pc->rt.rt_p;
  451. unsigned int delay;
  452. st_regmap_field_bit_set_clear_pin(rt_p->clk1notclk0,
  453. ST_PINCONF_UNPACK_RT_CLK(config), pin);
  454. st_regmap_field_bit_set_clear_pin(rt_p->clknotdata,
  455. ST_PINCONF_UNPACK_RT_CLKNOTDATA(config), pin);
  456. st_regmap_field_bit_set_clear_pin(rt_p->double_edge,
  457. ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config), pin);
  458. st_regmap_field_bit_set_clear_pin(rt_p->invertclk,
  459. ST_PINCONF_UNPACK_RT_INVERTCLK(config), pin);
  460. st_regmap_field_bit_set_clear_pin(rt_p->retime,
  461. ST_PINCONF_UNPACK_RT(config), pin);
  462. delay = st_pinconf_delay_to_bit(ST_PINCONF_UNPACK_RT_DELAY(config),
  463. data, config);
  464. /* 2 bit delay, lsb */
  465. st_regmap_field_bit_set_clear_pin(rt_p->delay_0, delay & 0x1, pin);
  466. /* 2 bit delay, msb */
  467. st_regmap_field_bit_set_clear_pin(rt_p->delay_1, delay & 0x2, pin);
  468. }
  469. static void st_pinconf_set_retime_dedicated(struct st_pinctrl *info,
  470. struct st_pio_control *pc, unsigned long config, int pin)
  471. {
  472. int input = ST_PINCONF_UNPACK_OE(config) ? 0 : 1;
  473. int clk = ST_PINCONF_UNPACK_RT_CLK(config);
  474. int clknotdata = ST_PINCONF_UNPACK_RT_CLKNOTDATA(config);
  475. int double_edge = ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config);
  476. int invertclk = ST_PINCONF_UNPACK_RT_INVERTCLK(config);
  477. int retime = ST_PINCONF_UNPACK_RT(config);
  478. unsigned long delay = st_pinconf_delay_to_bit(
  479. ST_PINCONF_UNPACK_RT_DELAY(config),
  480. info->data, config);
  481. struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
  482. unsigned long retime_config =
  483. ((clk) << RT_D_CFG_CLK_SHIFT) |
  484. ((delay) << RT_D_CFG_DELAY_SHIFT) |
  485. ((input) << RT_D_CFG_DELAY_INNOTOUT_SHIFT) |
  486. ((retime) << RT_D_CFG_RETIME_SHIFT) |
  487. ((clknotdata) << RT_D_CFG_CLKNOTDATA_SHIFT) |
  488. ((invertclk) << RT_D_CFG_INVERTCLK_SHIFT) |
  489. ((double_edge) << RT_D_CFG_DOUBLE_EDGE_SHIFT);
  490. regmap_field_write(rt_d->rt[pin], retime_config);
  491. }
  492. static void st_pinconf_get_direction(struct st_pio_control *pc,
  493. int pin, unsigned long *config)
  494. {
  495. unsigned int oe_value, pu_value, od_value;
  496. if (pc->oe) {
  497. regmap_field_read(pc->oe, &oe_value);
  498. if (oe_value & BIT(pin))
  499. ST_PINCONF_PACK_OE(*config);
  500. }
  501. if (pc->pu) {
  502. regmap_field_read(pc->pu, &pu_value);
  503. if (pu_value & BIT(pin))
  504. ST_PINCONF_PACK_PU(*config);
  505. }
  506. if (pc->od) {
  507. regmap_field_read(pc->od, &od_value);
  508. if (od_value & BIT(pin))
  509. ST_PINCONF_PACK_OD(*config);
  510. }
  511. }
  512. static int st_pinconf_get_retime_packed(struct st_pinctrl *info,
  513. struct st_pio_control *pc, int pin, unsigned long *config)
  514. {
  515. const struct st_pctl_data *data = info->data;
  516. struct st_retime_packed *rt_p = &pc->rt.rt_p;
  517. unsigned int delay_bits, delay, delay0, delay1, val;
  518. int output = ST_PINCONF_UNPACK_OE(*config);
  519. if (!regmap_field_read(rt_p->retime, &val) && (val & BIT(pin)))
  520. ST_PINCONF_PACK_RT(*config);
  521. if (!regmap_field_read(rt_p->clk1notclk0, &val) && (val & BIT(pin)))
  522. ST_PINCONF_PACK_RT_CLK(*config, 1);
  523. if (!regmap_field_read(rt_p->clknotdata, &val) && (val & BIT(pin)))
  524. ST_PINCONF_PACK_RT_CLKNOTDATA(*config);
  525. if (!regmap_field_read(rt_p->double_edge, &val) && (val & BIT(pin)))
  526. ST_PINCONF_PACK_RT_DOUBLE_EDGE(*config);
  527. if (!regmap_field_read(rt_p->invertclk, &val) && (val & BIT(pin)))
  528. ST_PINCONF_PACK_RT_INVERTCLK(*config);
  529. regmap_field_read(rt_p->delay_0, &delay0);
  530. regmap_field_read(rt_p->delay_1, &delay1);
  531. delay_bits = (((delay1 & BIT(pin)) ? 1 : 0) << 1) |
  532. (((delay0 & BIT(pin)) ? 1 : 0));
  533. delay = st_pinconf_bit_to_delay(delay_bits, data, output);
  534. ST_PINCONF_PACK_RT_DELAY(*config, delay);
  535. return 0;
  536. }
  537. static int st_pinconf_get_retime_dedicated(struct st_pinctrl *info,
  538. struct st_pio_control *pc, int pin, unsigned long *config)
  539. {
  540. unsigned int value;
  541. unsigned long delay_bits, delay, rt_clk;
  542. int output = ST_PINCONF_UNPACK_OE(*config);
  543. struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
  544. regmap_field_read(rt_d->rt[pin], &value);
  545. rt_clk = (value & RT_D_CFG_CLK_MASK) >> RT_D_CFG_CLK_SHIFT;
  546. ST_PINCONF_PACK_RT_CLK(*config, rt_clk);
  547. delay_bits = (value & RT_D_CFG_DELAY_MASK) >> RT_D_CFG_DELAY_SHIFT;
  548. delay = st_pinconf_bit_to_delay(delay_bits, info->data, output);
  549. ST_PINCONF_PACK_RT_DELAY(*config, delay);
  550. if (value & RT_D_CFG_CLKNOTDATA_MASK)
  551. ST_PINCONF_PACK_RT_CLKNOTDATA(*config);
  552. if (value & RT_D_CFG_DOUBLE_EDGE_MASK)
  553. ST_PINCONF_PACK_RT_DOUBLE_EDGE(*config);
  554. if (value & RT_D_CFG_INVERTCLK_MASK)
  555. ST_PINCONF_PACK_RT_INVERTCLK(*config);
  556. if (value & RT_D_CFG_RETIME_MASK)
  557. ST_PINCONF_PACK_RT(*config);
  558. return 0;
  559. }
  560. /* GPIO related functions */
  561. static inline void __st_gpio_set(struct st_gpio_bank *bank,
  562. unsigned offset, int value)
  563. {
  564. if (value)
  565. writel(BIT(offset), bank->base + REG_PIO_SET_POUT);
  566. else
  567. writel(BIT(offset), bank->base + REG_PIO_CLR_POUT);
  568. }
  569. static void st_gpio_direction(struct st_gpio_bank *bank,
  570. unsigned int gpio, unsigned int direction)
  571. {
  572. int offset = st_gpio_pin(gpio);
  573. int i = 0;
  574. /**
  575. * There are three configuration registers (PIOn_PC0, PIOn_PC1
  576. * and PIOn_PC2) for each port. These are used to configure the
  577. * PIO port pins. Each pin can be configured as an input, output,
  578. * bidirectional, or alternative function pin. Three bits, one bit
  579. * from each of the three registers, configure the corresponding bit of
  580. * the port. Valid bit settings is:
  581. *
  582. * PC2 PC1 PC0 Direction.
  583. * 0 0 0 [Input Weak pull-up]
  584. * 0 0 or 1 1 [Bidirection]
  585. * 0 1 0 [Output]
  586. * 1 0 0 [Input]
  587. *
  588. * PIOn_SET_PC and PIOn_CLR_PC registers are used to set and clear bits
  589. * individually.
  590. */
  591. for (i = 0; i <= 2; i++) {
  592. if (direction & BIT(i))
  593. writel(BIT(offset), bank->base + REG_PIO_SET_PC(i));
  594. else
  595. writel(BIT(offset), bank->base + REG_PIO_CLR_PC(i));
  596. }
  597. }
  598. static int st_gpio_get(struct gpio_chip *chip, unsigned offset)
  599. {
  600. struct st_gpio_bank *bank = gpiochip_get_data(chip);
  601. return !!(readl(bank->base + REG_PIO_PIN) & BIT(offset));
  602. }
  603. static int st_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
  604. {
  605. struct st_gpio_bank *bank = gpiochip_get_data(chip);
  606. __st_gpio_set(bank, offset, value);
  607. return 0;
  608. }
  609. static int st_gpio_direction_output(struct gpio_chip *chip,
  610. unsigned offset, int value)
  611. {
  612. struct st_gpio_bank *bank = gpiochip_get_data(chip);
  613. __st_gpio_set(bank, offset, value);
  614. return pinctrl_gpio_direction_output(chip, offset);
  615. }
  616. static int st_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  617. {
  618. struct st_gpio_bank *bank = gpiochip_get_data(chip);
  619. struct st_pio_control pc = bank->pc;
  620. unsigned long config;
  621. unsigned int direction = 0;
  622. unsigned int function;
  623. unsigned int value;
  624. int i = 0;
  625. /* Alternate function direction is handled by Pinctrl */
  626. function = st_pctl_get_pin_function(&pc, offset);
  627. if (function) {
  628. st_pinconf_get_direction(&pc, offset, &config);
  629. if (ST_PINCONF_UNPACK_OE(config))
  630. return GPIO_LINE_DIRECTION_OUT;
  631. return GPIO_LINE_DIRECTION_IN;
  632. }
  633. /*
  634. * GPIO direction is handled differently
  635. * - See st_gpio_direction() above for an explanation
  636. */
  637. for (i = 0; i <= 2; i++) {
  638. value = readl(bank->base + REG_PIO_PC(i));
  639. direction |= ((value >> offset) & 0x1) << i;
  640. }
  641. if (direction == ST_GPIO_DIRECTION_IN)
  642. return GPIO_LINE_DIRECTION_IN;
  643. return GPIO_LINE_DIRECTION_OUT;
  644. }
  645. /* Pinctrl Groups */
  646. static int st_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  647. {
  648. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  649. return info->ngroups;
  650. }
  651. static const char *st_pctl_get_group_name(struct pinctrl_dev *pctldev,
  652. unsigned selector)
  653. {
  654. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  655. return info->groups[selector].name;
  656. }
  657. static int st_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  658. unsigned selector, const unsigned **pins, unsigned *npins)
  659. {
  660. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  661. if (selector >= info->ngroups)
  662. return -EINVAL;
  663. *pins = info->groups[selector].pins;
  664. *npins = info->groups[selector].npins;
  665. return 0;
  666. }
  667. static inline const struct st_pctl_group *st_pctl_find_group_by_name(
  668. const struct st_pinctrl *info, const char *name)
  669. {
  670. int i;
  671. for (i = 0; i < info->ngroups; i++) {
  672. if (!strcmp(info->groups[i].name, name))
  673. return &info->groups[i];
  674. }
  675. return NULL;
  676. }
  677. static int st_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  678. struct device_node *np, struct pinctrl_map **map, unsigned *num_maps)
  679. {
  680. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  681. const struct st_pctl_group *grp;
  682. struct device *dev = info->dev;
  683. struct pinctrl_map *new_map;
  684. struct device_node *parent;
  685. int map_num, i;
  686. grp = st_pctl_find_group_by_name(info, np->name);
  687. if (!grp) {
  688. dev_err(dev, "unable to find group for node %pOFn\n", np);
  689. return -EINVAL;
  690. }
  691. map_num = grp->npins + 1;
  692. new_map = devm_kcalloc(dev, map_num, sizeof(*new_map), GFP_KERNEL);
  693. if (!new_map)
  694. return -ENOMEM;
  695. parent = of_get_parent(np);
  696. if (!parent) {
  697. devm_kfree(dev, new_map);
  698. return -EINVAL;
  699. }
  700. *map = new_map;
  701. *num_maps = map_num;
  702. new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
  703. new_map[0].data.mux.function = parent->name;
  704. new_map[0].data.mux.group = np->name;
  705. of_node_put(parent);
  706. /* create config map per pin */
  707. new_map++;
  708. for (i = 0; i < grp->npins; i++) {
  709. new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
  710. new_map[i].data.configs.group_or_pin =
  711. pin_get_name(pctldev, grp->pins[i]);
  712. new_map[i].data.configs.configs = &grp->pin_conf[i].config;
  713. new_map[i].data.configs.num_configs = 1;
  714. }
  715. dev_info(dev, "maps: function %s group %s num %d\n",
  716. (*map)->data.mux.function, grp->name, map_num);
  717. return 0;
  718. }
  719. static void st_pctl_dt_free_map(struct pinctrl_dev *pctldev,
  720. struct pinctrl_map *map, unsigned num_maps)
  721. {
  722. }
  723. static const struct pinctrl_ops st_pctlops = {
  724. .get_groups_count = st_pctl_get_groups_count,
  725. .get_group_pins = st_pctl_get_group_pins,
  726. .get_group_name = st_pctl_get_group_name,
  727. .dt_node_to_map = st_pctl_dt_node_to_map,
  728. .dt_free_map = st_pctl_dt_free_map,
  729. };
  730. /* Pinmux */
  731. static int st_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  732. {
  733. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  734. return info->nfunctions;
  735. }
  736. static const char *st_pmx_get_fname(struct pinctrl_dev *pctldev,
  737. unsigned selector)
  738. {
  739. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  740. return info->functions[selector].name;
  741. }
  742. static int st_pmx_get_groups(struct pinctrl_dev *pctldev,
  743. unsigned selector, const char * const **grps, unsigned * const ngrps)
  744. {
  745. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  746. *grps = info->functions[selector].groups;
  747. *ngrps = info->functions[selector].ngroups;
  748. return 0;
  749. }
  750. static int st_pmx_set_mux(struct pinctrl_dev *pctldev, unsigned fselector,
  751. unsigned group)
  752. {
  753. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  754. struct st_pinconf *conf = info->groups[group].pin_conf;
  755. struct st_pio_control *pc;
  756. int i;
  757. for (i = 0; i < info->groups[group].npins; i++) {
  758. pc = st_get_pio_control(pctldev, conf[i].pin);
  759. st_pctl_set_function(pc, conf[i].pin, conf[i].altfunc);
  760. }
  761. return 0;
  762. }
  763. static int st_pmx_set_gpio_direction(struct pinctrl_dev *pctldev,
  764. struct pinctrl_gpio_range *range, unsigned gpio,
  765. bool input)
  766. {
  767. struct st_gpio_bank *bank = gpio_range_to_bank(range);
  768. /*
  769. * When a PIO bank is used in its primary function mode (altfunc = 0)
  770. * Output Enable (OE), Open Drain(OD), and Pull Up (PU)
  771. * for the primary PIO functions are driven by the related PIO block
  772. */
  773. st_pctl_set_function(&bank->pc, gpio, 0);
  774. st_gpio_direction(bank, gpio, input ?
  775. ST_GPIO_DIRECTION_IN : ST_GPIO_DIRECTION_OUT);
  776. return 0;
  777. }
  778. static const struct pinmux_ops st_pmxops = {
  779. .get_functions_count = st_pmx_get_funcs_count,
  780. .get_function_name = st_pmx_get_fname,
  781. .get_function_groups = st_pmx_get_groups,
  782. .set_mux = st_pmx_set_mux,
  783. .gpio_set_direction = st_pmx_set_gpio_direction,
  784. .strict = true,
  785. };
  786. /* Pinconf */
  787. static void st_pinconf_get_retime(struct st_pinctrl *info,
  788. struct st_pio_control *pc, int pin, unsigned long *config)
  789. {
  790. if (info->data->rt_style == st_retime_style_packed)
  791. st_pinconf_get_retime_packed(info, pc, pin, config);
  792. else if (info->data->rt_style == st_retime_style_dedicated)
  793. if ((BIT(pin) & pc->rt_pin_mask))
  794. st_pinconf_get_retime_dedicated(info, pc,
  795. pin, config);
  796. }
  797. static void st_pinconf_set_retime(struct st_pinctrl *info,
  798. struct st_pio_control *pc, int pin, unsigned long config)
  799. {
  800. if (info->data->rt_style == st_retime_style_packed)
  801. st_pinconf_set_retime_packed(info, pc, config, pin);
  802. else if (info->data->rt_style == st_retime_style_dedicated)
  803. if ((BIT(pin) & pc->rt_pin_mask))
  804. st_pinconf_set_retime_dedicated(info, pc,
  805. config, pin);
  806. }
  807. static int st_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin_id,
  808. unsigned long *configs, unsigned num_configs)
  809. {
  810. int pin = st_gpio_pin(pin_id);
  811. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  812. struct st_pio_control *pc = st_get_pio_control(pctldev, pin_id);
  813. int i;
  814. for (i = 0; i < num_configs; i++) {
  815. st_pinconf_set_config(pc, pin, configs[i]);
  816. st_pinconf_set_retime(info, pc, pin, configs[i]);
  817. } /* for each config */
  818. return 0;
  819. }
  820. static int st_pinconf_get(struct pinctrl_dev *pctldev,
  821. unsigned pin_id, unsigned long *config)
  822. {
  823. int pin = st_gpio_pin(pin_id);
  824. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  825. struct st_pio_control *pc = st_get_pio_control(pctldev, pin_id);
  826. *config = 0;
  827. st_pinconf_get_direction(pc, pin, config);
  828. st_pinconf_get_retime(info, pc, pin, config);
  829. return 0;
  830. }
  831. static void st_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  832. struct seq_file *s, unsigned pin_id)
  833. __must_hold(&pctldev->mutex)
  834. {
  835. struct st_pio_control *pc;
  836. unsigned long config;
  837. unsigned int function;
  838. int offset = st_gpio_pin(pin_id);
  839. char f[16];
  840. int oe;
  841. mutex_unlock(&pctldev->mutex);
  842. pc = st_get_pio_control(pctldev, pin_id);
  843. st_pinconf_get(pctldev, pin_id, &config);
  844. mutex_lock(&pctldev->mutex);
  845. function = st_pctl_get_pin_function(pc, offset);
  846. if (function)
  847. snprintf(f, 10, "Alt Fn %u", function);
  848. else
  849. snprintf(f, 5, "GPIO");
  850. oe = st_gpio_get_direction(&pc_to_bank(pc)->gpio_chip, offset);
  851. seq_printf(s, "[OE:%d,PU:%ld,OD:%ld]\t%s\n"
  852. "\t\t[retime:%ld,invclk:%ld,clknotdat:%ld,"
  853. "de:%ld,rt-clk:%ld,rt-delay:%ld]",
  854. (oe == GPIO_LINE_DIRECTION_OUT),
  855. ST_PINCONF_UNPACK_PU(config),
  856. ST_PINCONF_UNPACK_OD(config),
  857. f,
  858. ST_PINCONF_UNPACK_RT(config),
  859. ST_PINCONF_UNPACK_RT_INVERTCLK(config),
  860. ST_PINCONF_UNPACK_RT_CLKNOTDATA(config),
  861. ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config),
  862. ST_PINCONF_UNPACK_RT_CLK(config),
  863. ST_PINCONF_UNPACK_RT_DELAY(config));
  864. }
  865. static const struct pinconf_ops st_confops = {
  866. .pin_config_get = st_pinconf_get,
  867. .pin_config_set = st_pinconf_set,
  868. .pin_config_dbg_show = st_pinconf_dbg_show,
  869. };
  870. static void st_pctl_dt_child_count(struct st_pinctrl *info,
  871. struct device_node *np)
  872. {
  873. struct device_node *child;
  874. for_each_child_of_node(np, child) {
  875. if (of_property_read_bool(child, "gpio-controller")) {
  876. info->nbanks++;
  877. } else {
  878. info->nfunctions++;
  879. info->ngroups += of_get_child_count(child);
  880. }
  881. }
  882. }
  883. static int st_pctl_dt_setup_retime_packed(struct st_pinctrl *info,
  884. int bank, struct st_pio_control *pc)
  885. {
  886. struct device *dev = info->dev;
  887. struct regmap *rm = info->regmap;
  888. const struct st_pctl_data *data = info->data;
  889. /* 2 registers per bank */
  890. int reg = (data->rt + bank * RT_P_CFGS_PER_BANK) * 4;
  891. struct st_retime_packed *rt_p = &pc->rt.rt_p;
  892. /* cfg0 */
  893. struct reg_field clk1notclk0 = RT_P_CFG0_CLK1NOTCLK0_FIELD(reg);
  894. struct reg_field delay_0 = RT_P_CFG0_DELAY_0_FIELD(reg);
  895. struct reg_field delay_1 = RT_P_CFG0_DELAY_1_FIELD(reg);
  896. /* cfg1 */
  897. struct reg_field invertclk = RT_P_CFG1_INVERTCLK_FIELD(reg + 4);
  898. struct reg_field retime = RT_P_CFG1_RETIME_FIELD(reg + 4);
  899. struct reg_field clknotdata = RT_P_CFG1_CLKNOTDATA_FIELD(reg + 4);
  900. struct reg_field double_edge = RT_P_CFG1_DOUBLE_EDGE_FIELD(reg + 4);
  901. rt_p->clk1notclk0 = devm_regmap_field_alloc(dev, rm, clk1notclk0);
  902. rt_p->delay_0 = devm_regmap_field_alloc(dev, rm, delay_0);
  903. rt_p->delay_1 = devm_regmap_field_alloc(dev, rm, delay_1);
  904. rt_p->invertclk = devm_regmap_field_alloc(dev, rm, invertclk);
  905. rt_p->retime = devm_regmap_field_alloc(dev, rm, retime);
  906. rt_p->clknotdata = devm_regmap_field_alloc(dev, rm, clknotdata);
  907. rt_p->double_edge = devm_regmap_field_alloc(dev, rm, double_edge);
  908. if (IS_ERR(rt_p->clk1notclk0) || IS_ERR(rt_p->delay_0) ||
  909. IS_ERR(rt_p->delay_1) || IS_ERR(rt_p->invertclk) ||
  910. IS_ERR(rt_p->retime) || IS_ERR(rt_p->clknotdata) ||
  911. IS_ERR(rt_p->double_edge))
  912. return -EINVAL;
  913. return 0;
  914. }
  915. static int st_pctl_dt_setup_retime_dedicated(struct st_pinctrl *info,
  916. int bank, struct st_pio_control *pc)
  917. {
  918. struct device *dev = info->dev;
  919. struct regmap *rm = info->regmap;
  920. const struct st_pctl_data *data = info->data;
  921. /* 8 registers per bank */
  922. int reg_offset = (data->rt + bank * RT_D_CFGS_PER_BANK) * 4;
  923. struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
  924. unsigned int j;
  925. u32 pin_mask = pc->rt_pin_mask;
  926. for (j = 0; j < RT_D_CFGS_PER_BANK; j++) {
  927. if (BIT(j) & pin_mask) {
  928. struct reg_field reg = REG_FIELD(reg_offset, 0, 31);
  929. rt_d->rt[j] = devm_regmap_field_alloc(dev, rm, reg);
  930. if (IS_ERR(rt_d->rt[j]))
  931. return -EINVAL;
  932. reg_offset += 4;
  933. }
  934. }
  935. return 0;
  936. }
  937. static int st_pctl_dt_setup_retime(struct st_pinctrl *info,
  938. int bank, struct st_pio_control *pc)
  939. {
  940. const struct st_pctl_data *data = info->data;
  941. if (data->rt_style == st_retime_style_packed)
  942. return st_pctl_dt_setup_retime_packed(info, bank, pc);
  943. else if (data->rt_style == st_retime_style_dedicated)
  944. return st_pctl_dt_setup_retime_dedicated(info, bank, pc);
  945. return -EINVAL;
  946. }
  947. static struct regmap_field *st_pc_get_value(struct device *dev,
  948. struct regmap *regmap, int bank,
  949. int data, int lsb, int msb)
  950. {
  951. struct reg_field reg = REG_FIELD((data + bank) * 4, lsb, msb);
  952. if (data < 0)
  953. return NULL;
  954. return devm_regmap_field_alloc(dev, regmap, reg);
  955. }
  956. static void st_parse_syscfgs(struct st_pinctrl *info, int bank,
  957. struct device_node *np)
  958. {
  959. const struct st_pctl_data *data = info->data;
  960. /**
  961. * For a given shared register like OE/PU/OD, there are 8 bits per bank
  962. * 0:7 belongs to bank0, 8:15 belongs to bank1 ...
  963. * So each register is shared across 4 banks.
  964. */
  965. int lsb = (bank%4) * ST_GPIO_PINS_PER_BANK;
  966. int msb = lsb + ST_GPIO_PINS_PER_BANK - 1;
  967. struct st_pio_control *pc = &info->banks[bank].pc;
  968. struct device *dev = info->dev;
  969. struct regmap *regmap = info->regmap;
  970. pc->alt = st_pc_get_value(dev, regmap, bank, data->alt, 0, 31);
  971. pc->oe = st_pc_get_value(dev, regmap, bank/4, data->oe, lsb, msb);
  972. pc->pu = st_pc_get_value(dev, regmap, bank/4, data->pu, lsb, msb);
  973. pc->od = st_pc_get_value(dev, regmap, bank/4, data->od, lsb, msb);
  974. /* retime avaiable for all pins by default */
  975. pc->rt_pin_mask = 0xff;
  976. of_property_read_u32(np, "st,retime-pin-mask", &pc->rt_pin_mask);
  977. st_pctl_dt_setup_retime(info, bank, pc);
  978. return;
  979. }
  980. static int st_pctl_dt_calculate_pin(struct st_pinctrl *info,
  981. phandle bank, unsigned int offset)
  982. {
  983. struct device_node *np;
  984. struct gpio_chip *chip;
  985. int retval = -EINVAL;
  986. int i;
  987. np = of_find_node_by_phandle(bank);
  988. if (!np)
  989. return -EINVAL;
  990. for (i = 0; i < info->nbanks; i++) {
  991. chip = &info->banks[i].gpio_chip;
  992. if (chip->fwnode == of_fwnode_handle(np)) {
  993. if (offset < chip->ngpio)
  994. retval = chip->base + offset;
  995. break;
  996. }
  997. }
  998. of_node_put(np);
  999. return retval;
  1000. }
  1001. /*
  1002. * Each pin is represented in of the below forms.
  1003. * <bank offset mux direction rt_type rt_delay rt_clk>
  1004. */
  1005. static int st_pctl_dt_parse_groups(struct device_node *np,
  1006. struct st_pctl_group *grp, struct st_pinctrl *info, int idx)
  1007. {
  1008. /* bank pad direction val altfunction */
  1009. const __be32 *list;
  1010. struct property *pp;
  1011. struct device *dev = info->dev;
  1012. struct st_pinconf *conf;
  1013. struct device_node *pins __free(device_node) = NULL;
  1014. phandle bank;
  1015. unsigned int offset;
  1016. int i = 0, npins = 0, nr_props;
  1017. pins = of_get_child_by_name(np, "st,pins");
  1018. if (!pins)
  1019. return -ENODATA;
  1020. for_each_property_of_node(pins, pp) {
  1021. /* Skip those we do not want to proceed */
  1022. if (!strcmp(pp->name, "name"))
  1023. continue;
  1024. if (pp->length / sizeof(__be32) >= OF_GPIO_ARGS_MIN) {
  1025. npins++;
  1026. } else {
  1027. pr_warn("Invalid st,pins in %pOFn node\n", np);
  1028. return -EINVAL;
  1029. }
  1030. }
  1031. grp->npins = npins;
  1032. grp->name = np->name;
  1033. grp->pins = devm_kcalloc(dev, npins, sizeof(*grp->pins), GFP_KERNEL);
  1034. grp->pin_conf = devm_kcalloc(dev, npins, sizeof(*grp->pin_conf), GFP_KERNEL);
  1035. if (!grp->pins || !grp->pin_conf)
  1036. return -ENOMEM;
  1037. /* <bank offset mux direction rt_type rt_delay rt_clk> */
  1038. for_each_property_of_node(pins, pp) {
  1039. if (!strcmp(pp->name, "name"))
  1040. continue;
  1041. nr_props = pp->length/sizeof(u32);
  1042. list = pp->value;
  1043. conf = &grp->pin_conf[i];
  1044. /* bank & offset */
  1045. bank = be32_to_cpup(list++);
  1046. offset = be32_to_cpup(list++);
  1047. conf->pin = st_pctl_dt_calculate_pin(info, bank, offset);
  1048. conf->name = pp->name;
  1049. grp->pins[i] = conf->pin;
  1050. /* mux */
  1051. conf->altfunc = be32_to_cpup(list++);
  1052. conf->config = 0;
  1053. /* direction */
  1054. conf->config |= be32_to_cpup(list++);
  1055. /* rt_type rt_delay rt_clk */
  1056. if (nr_props >= OF_GPIO_ARGS_MIN + OF_RT_ARGS_MIN) {
  1057. /* rt_type */
  1058. conf->config |= be32_to_cpup(list++);
  1059. /* rt_delay */
  1060. conf->config |= be32_to_cpup(list++);
  1061. /* rt_clk */
  1062. if (nr_props > OF_GPIO_ARGS_MIN + OF_RT_ARGS_MIN)
  1063. conf->config |= be32_to_cpup(list++);
  1064. }
  1065. i++;
  1066. }
  1067. return 0;
  1068. }
  1069. static int st_pctl_parse_functions(struct device_node *np,
  1070. struct st_pinctrl *info, u32 index, int *grp_index)
  1071. {
  1072. struct device *dev = info->dev;
  1073. struct st_pmx_func *func;
  1074. struct st_pctl_group *grp;
  1075. int ret, i;
  1076. func = &info->functions[index];
  1077. func->name = np->name;
  1078. func->ngroups = of_get_child_count(np);
  1079. if (func->ngroups == 0)
  1080. return dev_err_probe(dev, -EINVAL, "No groups defined\n");
  1081. func->groups = devm_kcalloc(dev, func->ngroups, sizeof(*func->groups), GFP_KERNEL);
  1082. if (!func->groups)
  1083. return -ENOMEM;
  1084. i = 0;
  1085. for_each_child_of_node_scoped(np, child) {
  1086. func->groups[i] = child->name;
  1087. grp = &info->groups[*grp_index];
  1088. *grp_index += 1;
  1089. ret = st_pctl_dt_parse_groups(child, grp, info, i++);
  1090. if (ret)
  1091. return ret;
  1092. }
  1093. dev_info(dev, "Function[%d\t name:%s,\tgroups:%d]\n", index, func->name, func->ngroups);
  1094. return 0;
  1095. }
  1096. static void st_gpio_irq_mask(struct irq_data *d)
  1097. {
  1098. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1099. struct st_gpio_bank *bank = gpiochip_get_data(gc);
  1100. writel(BIT(irqd_to_hwirq(d)), bank->base + REG_PIO_CLR_PMASK);
  1101. gpiochip_disable_irq(gc, irqd_to_hwirq(d));
  1102. }
  1103. static void st_gpio_irq_unmask(struct irq_data *d)
  1104. {
  1105. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1106. struct st_gpio_bank *bank = gpiochip_get_data(gc);
  1107. gpiochip_enable_irq(gc, irqd_to_hwirq(d));
  1108. writel(BIT(irqd_to_hwirq(d)), bank->base + REG_PIO_SET_PMASK);
  1109. }
  1110. static int st_gpio_irq_request_resources(struct irq_data *d)
  1111. {
  1112. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1113. pinctrl_gpio_direction_input(gc, d->hwirq);
  1114. return gpiochip_reqres_irq(gc, d->hwirq);
  1115. }
  1116. static void st_gpio_irq_release_resources(struct irq_data *d)
  1117. {
  1118. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1119. gpiochip_relres_irq(gc, d->hwirq);
  1120. }
  1121. static int st_gpio_irq_set_type(struct irq_data *d, unsigned type)
  1122. {
  1123. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1124. struct st_gpio_bank *bank = gpiochip_get_data(gc);
  1125. unsigned long flags;
  1126. int comp, pin = d->hwirq;
  1127. u32 val;
  1128. u32 pin_edge_conf = 0;
  1129. switch (type) {
  1130. case IRQ_TYPE_LEVEL_HIGH:
  1131. comp = 0;
  1132. break;
  1133. case IRQ_TYPE_EDGE_FALLING:
  1134. comp = 0;
  1135. pin_edge_conf = ST_IRQ_FALLING_EDGE_CONF(pin);
  1136. break;
  1137. case IRQ_TYPE_LEVEL_LOW:
  1138. comp = 1;
  1139. break;
  1140. case IRQ_TYPE_EDGE_RISING:
  1141. comp = 1;
  1142. pin_edge_conf = ST_IRQ_RISING_EDGE_CONF(pin);
  1143. break;
  1144. case IRQ_TYPE_EDGE_BOTH:
  1145. comp = st_gpio_get(&bank->gpio_chip, pin);
  1146. pin_edge_conf = ST_IRQ_BOTH_EDGE_CONF(pin);
  1147. break;
  1148. default:
  1149. return -EINVAL;
  1150. }
  1151. spin_lock_irqsave(&bank->lock, flags);
  1152. bank->irq_edge_conf &= ~(ST_IRQ_EDGE_MASK << (
  1153. pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN));
  1154. bank->irq_edge_conf |= pin_edge_conf;
  1155. spin_unlock_irqrestore(&bank->lock, flags);
  1156. val = readl(bank->base + REG_PIO_PCOMP);
  1157. val &= ~BIT(pin);
  1158. val |= (comp << pin);
  1159. writel(val, bank->base + REG_PIO_PCOMP);
  1160. return 0;
  1161. }
  1162. /*
  1163. * As edge triggers are not supported at hardware level, it is supported by
  1164. * software by exploiting the level trigger support in hardware.
  1165. *
  1166. * Steps for detection raising edge interrupt in software.
  1167. *
  1168. * Step 1: CONFIGURE pin to detect level LOW interrupts.
  1169. *
  1170. * Step 2: DETECT level LOW interrupt and in irqmux/gpio bank interrupt handler,
  1171. * if the value of pin is low, then CONFIGURE pin for level HIGH interrupt.
  1172. * IGNORE calling the actual interrupt handler for the pin at this stage.
  1173. *
  1174. * Step 3: DETECT level HIGH interrupt and in irqmux/gpio-bank interrupt handler
  1175. * if the value of pin is HIGH, CONFIGURE pin for level LOW interrupt and then
  1176. * DISPATCH the interrupt to the interrupt handler of the pin.
  1177. *
  1178. * step-1 ________ __________
  1179. * | | step - 3
  1180. * | |
  1181. * step -2 |_____|
  1182. *
  1183. * falling edge is also detected int the same way.
  1184. *
  1185. */
  1186. static void __gpio_irq_handler(struct st_gpio_bank *bank)
  1187. {
  1188. unsigned long port_in, port_mask, port_comp, active_irqs;
  1189. unsigned long bank_edge_mask, flags;
  1190. int n, val, ecfg;
  1191. spin_lock_irqsave(&bank->lock, flags);
  1192. bank_edge_mask = bank->irq_edge_conf;
  1193. spin_unlock_irqrestore(&bank->lock, flags);
  1194. for (;;) {
  1195. port_in = readl(bank->base + REG_PIO_PIN);
  1196. port_comp = readl(bank->base + REG_PIO_PCOMP);
  1197. port_mask = readl(bank->base + REG_PIO_PMASK);
  1198. active_irqs = (port_in ^ port_comp) & port_mask;
  1199. if (active_irqs == 0)
  1200. break;
  1201. for_each_set_bit(n, &active_irqs, BITS_PER_LONG) {
  1202. /* check if we are detecting fake edges ... */
  1203. ecfg = ST_IRQ_EDGE_CONF(bank_edge_mask, n);
  1204. if (ecfg) {
  1205. /* edge detection. */
  1206. val = st_gpio_get(&bank->gpio_chip, n);
  1207. writel(BIT(n),
  1208. val ? bank->base + REG_PIO_SET_PCOMP :
  1209. bank->base + REG_PIO_CLR_PCOMP);
  1210. if (ecfg != ST_IRQ_EDGE_BOTH &&
  1211. !((ecfg & ST_IRQ_EDGE_FALLING) ^ val))
  1212. continue;
  1213. }
  1214. generic_handle_domain_irq(bank->gpio_chip.irq.domain, n);
  1215. }
  1216. }
  1217. }
  1218. static void st_gpio_irq_handler(struct irq_desc *desc)
  1219. {
  1220. /* interrupt dedicated per bank */
  1221. struct irq_chip *chip = irq_desc_get_chip(desc);
  1222. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  1223. struct st_gpio_bank *bank = gpiochip_get_data(gc);
  1224. chained_irq_enter(chip, desc);
  1225. __gpio_irq_handler(bank);
  1226. chained_irq_exit(chip, desc);
  1227. }
  1228. static void st_gpio_irqmux_handler(struct irq_desc *desc)
  1229. {
  1230. struct irq_chip *chip = irq_desc_get_chip(desc);
  1231. struct st_pinctrl *info = irq_desc_get_handler_data(desc);
  1232. unsigned long status;
  1233. int n;
  1234. chained_irq_enter(chip, desc);
  1235. status = readl(info->irqmux_base);
  1236. for_each_set_bit(n, &status, info->nbanks)
  1237. __gpio_irq_handler(&info->banks[n]);
  1238. chained_irq_exit(chip, desc);
  1239. }
  1240. static const struct gpio_chip st_gpio_template = {
  1241. .request = gpiochip_generic_request,
  1242. .free = gpiochip_generic_free,
  1243. .get = st_gpio_get,
  1244. .set = st_gpio_set,
  1245. .direction_input = pinctrl_gpio_direction_input,
  1246. .direction_output = st_gpio_direction_output,
  1247. .get_direction = st_gpio_get_direction,
  1248. .ngpio = ST_GPIO_PINS_PER_BANK,
  1249. };
  1250. static const struct irq_chip st_gpio_irqchip = {
  1251. .name = "GPIO",
  1252. .irq_request_resources = st_gpio_irq_request_resources,
  1253. .irq_release_resources = st_gpio_irq_release_resources,
  1254. .irq_disable = st_gpio_irq_mask,
  1255. .irq_mask = st_gpio_irq_mask,
  1256. .irq_unmask = st_gpio_irq_unmask,
  1257. .irq_set_type = st_gpio_irq_set_type,
  1258. .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_IMMUTABLE,
  1259. };
  1260. static int st_gpiolib_register_bank(struct st_pinctrl *info,
  1261. int bank_nr, struct device_node *np)
  1262. {
  1263. struct st_gpio_bank *bank = &info->banks[bank_nr];
  1264. struct pinctrl_gpio_range *range = &bank->range;
  1265. struct device *dev = info->dev;
  1266. int bank_num = of_alias_get_id(np, "gpio");
  1267. struct resource res, irq_res;
  1268. int err;
  1269. if (of_address_to_resource(np, 0, &res))
  1270. return -ENODEV;
  1271. bank->base = devm_ioremap_resource(dev, &res);
  1272. if (IS_ERR(bank->base))
  1273. return PTR_ERR(bank->base);
  1274. bank->gpio_chip = st_gpio_template;
  1275. bank->gpio_chip.base = bank_num * ST_GPIO_PINS_PER_BANK;
  1276. bank->gpio_chip.ngpio = ST_GPIO_PINS_PER_BANK;
  1277. bank->gpio_chip.fwnode = of_fwnode_handle(np);
  1278. bank->gpio_chip.parent = dev;
  1279. spin_lock_init(&bank->lock);
  1280. of_property_read_string(np, "st,bank-name", &range->name);
  1281. bank->gpio_chip.label = range->name;
  1282. range->id = bank_num;
  1283. range->pin_base = range->base = range->id * ST_GPIO_PINS_PER_BANK;
  1284. range->npins = bank->gpio_chip.ngpio;
  1285. range->gc = &bank->gpio_chip;
  1286. /**
  1287. * GPIO bank can have one of the two possible types of
  1288. * interrupt-wirings.
  1289. *
  1290. * First type is via irqmux, single interrupt is used by multiple
  1291. * gpio banks. This reduces number of overall interrupts numbers
  1292. * required. All these banks belong to a single pincontroller.
  1293. * _________
  1294. * | |----> [gpio-bank (n) ]
  1295. * | |----> [gpio-bank (n + 1)]
  1296. * [irqN]-- | irq-mux |----> [gpio-bank (n + 2)]
  1297. * | |----> [gpio-bank (... )]
  1298. * |_________|----> [gpio-bank (n + 7)]
  1299. *
  1300. * Second type has a dedicated interrupt per each gpio bank.
  1301. *
  1302. * [irqN]----> [gpio-bank (n)]
  1303. */
  1304. if (of_irq_to_resource(np, 0, &irq_res) > 0) {
  1305. struct gpio_irq_chip *girq;
  1306. int gpio_irq = irq_res.start;
  1307. /* This is not a valid IRQ */
  1308. if (gpio_irq <= 0) {
  1309. dev_err(dev, "invalid IRQ for %pOF bank\n", np);
  1310. goto skip_irq;
  1311. }
  1312. /* We need to have a mux as well */
  1313. if (!info->irqmux_base) {
  1314. dev_err(dev, "no irqmux for %pOF bank\n", np);
  1315. goto skip_irq;
  1316. }
  1317. girq = &bank->gpio_chip.irq;
  1318. gpio_irq_chip_set_chip(girq, &st_gpio_irqchip);
  1319. girq->parent_handler = st_gpio_irq_handler;
  1320. girq->num_parents = 1;
  1321. girq->parents = devm_kcalloc(dev, 1, sizeof(*girq->parents),
  1322. GFP_KERNEL);
  1323. if (!girq->parents)
  1324. return -ENOMEM;
  1325. girq->parents[0] = gpio_irq;
  1326. girq->default_type = IRQ_TYPE_NONE;
  1327. girq->handler = handle_simple_irq;
  1328. }
  1329. skip_irq:
  1330. err = gpiochip_add_data(&bank->gpio_chip, bank);
  1331. if (err)
  1332. return dev_err_probe(dev, err, "Failed to add gpiochip(%d)!\n", bank_num);
  1333. dev_info(dev, "%s bank added.\n", range->name);
  1334. return 0;
  1335. }
  1336. static const struct of_device_id st_pctl_of_match[] = {
  1337. { .compatible = "st,stih407-sbc-pinctrl", .data = &stih407_data},
  1338. { .compatible = "st,stih407-front-pinctrl", .data = &stih407_data},
  1339. { .compatible = "st,stih407-rear-pinctrl", .data = &stih407_data},
  1340. { .compatible = "st,stih407-flash-pinctrl", .data = &stih407_flashdata},
  1341. { /* sentinel */ }
  1342. };
  1343. static int st_pctl_probe_dt(struct platform_device *pdev,
  1344. struct pinctrl_desc *pctl_desc, struct st_pinctrl *info)
  1345. {
  1346. struct device *dev = &pdev->dev;
  1347. int ret = 0;
  1348. int i = 0, j = 0, k = 0, bank;
  1349. struct pinctrl_pin_desc *pdesc;
  1350. struct device_node *np = dev->of_node;
  1351. int grp_index = 0;
  1352. int irq = 0;
  1353. st_pctl_dt_child_count(info, np);
  1354. if (!info->nbanks)
  1355. return dev_err_probe(dev, -EINVAL, "you need at least one gpio bank\n");
  1356. dev_info(dev, "nbanks = %d\n", info->nbanks);
  1357. dev_info(dev, "nfunctions = %d\n", info->nfunctions);
  1358. dev_info(dev, "ngroups = %d\n", info->ngroups);
  1359. info->functions = devm_kcalloc(dev, info->nfunctions, sizeof(*info->functions), GFP_KERNEL);
  1360. info->groups = devm_kcalloc(dev, info->ngroups, sizeof(*info->groups), GFP_KERNEL);
  1361. info->banks = devm_kcalloc(dev, info->nbanks, sizeof(*info->banks), GFP_KERNEL);
  1362. if (!info->functions || !info->groups || !info->banks)
  1363. return -ENOMEM;
  1364. info->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  1365. if (IS_ERR(info->regmap))
  1366. return dev_err_probe(dev, PTR_ERR(info->regmap), "No syscfg phandle specified\n");
  1367. info->data = of_match_node(st_pctl_of_match, np)->data;
  1368. irq = platform_get_irq(pdev, 0);
  1369. if (irq > 0) {
  1370. info->irqmux_base = devm_platform_ioremap_resource_byname(pdev, "irqmux");
  1371. if (IS_ERR(info->irqmux_base))
  1372. return PTR_ERR(info->irqmux_base);
  1373. irq_set_chained_handler_and_data(irq, st_gpio_irqmux_handler,
  1374. info);
  1375. }
  1376. pctl_desc->npins = info->nbanks * ST_GPIO_PINS_PER_BANK;
  1377. pdesc = devm_kcalloc(dev, pctl_desc->npins, sizeof(*pdesc), GFP_KERNEL);
  1378. if (!pdesc)
  1379. return -ENOMEM;
  1380. pctl_desc->pins = pdesc;
  1381. bank = 0;
  1382. for_each_child_of_node_scoped(np, child) {
  1383. if (of_property_read_bool(child, "gpio-controller")) {
  1384. const char *bank_name = NULL;
  1385. char **pin_names;
  1386. ret = st_gpiolib_register_bank(info, bank, child);
  1387. if (ret)
  1388. return ret;
  1389. k = info->banks[bank].range.pin_base;
  1390. bank_name = info->banks[bank].range.name;
  1391. pin_names = devm_kasprintf_strarray(dev, bank_name, ST_GPIO_PINS_PER_BANK);
  1392. if (IS_ERR(pin_names))
  1393. return PTR_ERR(pin_names);
  1394. for (j = 0; j < ST_GPIO_PINS_PER_BANK; j++, k++) {
  1395. pdesc->number = k;
  1396. pdesc->name = pin_names[j];
  1397. pdesc++;
  1398. }
  1399. st_parse_syscfgs(info, bank, child);
  1400. bank++;
  1401. } else {
  1402. ret = st_pctl_parse_functions(child, info,
  1403. i++, &grp_index);
  1404. if (ret) {
  1405. dev_err(dev, "No functions found.\n");
  1406. return ret;
  1407. }
  1408. }
  1409. }
  1410. return 0;
  1411. }
  1412. static int st_pctl_probe(struct platform_device *pdev)
  1413. {
  1414. struct device *dev = &pdev->dev;
  1415. struct st_pinctrl *info;
  1416. struct pinctrl_desc *pctl_desc;
  1417. int ret, i;
  1418. if (!dev->of_node) {
  1419. dev_err(dev, "device node not found.\n");
  1420. return -EINVAL;
  1421. }
  1422. pctl_desc = devm_kzalloc(dev, sizeof(*pctl_desc), GFP_KERNEL);
  1423. if (!pctl_desc)
  1424. return -ENOMEM;
  1425. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  1426. if (!info)
  1427. return -ENOMEM;
  1428. info->dev = dev;
  1429. platform_set_drvdata(pdev, info);
  1430. ret = st_pctl_probe_dt(pdev, pctl_desc, info);
  1431. if (ret)
  1432. return ret;
  1433. pctl_desc->owner = THIS_MODULE;
  1434. pctl_desc->pctlops = &st_pctlops;
  1435. pctl_desc->pmxops = &st_pmxops;
  1436. pctl_desc->confops = &st_confops;
  1437. pctl_desc->name = dev_name(dev);
  1438. info->pctl = devm_pinctrl_register(dev, pctl_desc, info);
  1439. if (IS_ERR(info->pctl))
  1440. return dev_err_probe(dev, PTR_ERR(info->pctl), "Failed pinctrl registration\n");
  1441. for (i = 0; i < info->nbanks; i++)
  1442. pinctrl_add_gpio_range(info->pctl, &info->banks[i].range);
  1443. return 0;
  1444. }
  1445. static struct platform_driver st_pctl_driver = {
  1446. .driver = {
  1447. .name = "st-pinctrl",
  1448. .of_match_table = st_pctl_of_match,
  1449. },
  1450. .probe = st_pctl_probe,
  1451. };
  1452. static int __init st_pctl_init(void)
  1453. {
  1454. return platform_driver_register(&st_pctl_driver);
  1455. }
  1456. arch_initcall(st_pctl_init);