pinctrl-st.c 49 KB

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