pinctrl-st.c 48 KB

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