pinctrl-st.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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_gpio.h>
  17. #include <linux/of_address.h>
  18. #include <linux/regmap.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include <linux/pinctrl/pinconf.h>
  23. #include <linux/platform_device.h>
  24. #include "core.h"
  25. /* PIO Block registers */
  26. /* PIO output */
  27. #define REG_PIO_POUT 0x00
  28. /* Set bits of POUT */
  29. #define REG_PIO_SET_POUT 0x04
  30. /* Clear bits of POUT */
  31. #define REG_PIO_CLR_POUT 0x08
  32. /* PIO input */
  33. #define REG_PIO_PIN 0x10
  34. /* PIO configuration */
  35. #define REG_PIO_PC(n) (0x20 + (n) * 0x10)
  36. /* Set bits of PC[2:0] */
  37. #define REG_PIO_SET_PC(n) (0x24 + (n) * 0x10)
  38. /* Clear bits of PC[2:0] */
  39. #define REG_PIO_CLR_PC(n) (0x28 + (n) * 0x10)
  40. /* PIO input comparison */
  41. #define REG_PIO_PCOMP 0x50
  42. /* Set bits of PCOMP */
  43. #define REG_PIO_SET_PCOMP 0x54
  44. /* Clear bits of PCOMP */
  45. #define REG_PIO_CLR_PCOMP 0x58
  46. /* PIO input comparison mask */
  47. #define REG_PIO_PMASK 0x60
  48. /* Set bits of PMASK */
  49. #define REG_PIO_SET_PMASK 0x64
  50. /* Clear bits of PMASK */
  51. #define REG_PIO_CLR_PMASK 0x68
  52. #define ST_GPIO_DIRECTION_BIDIR 0x1
  53. #define ST_GPIO_DIRECTION_OUT 0x2
  54. #define ST_GPIO_DIRECTION_IN 0x4
  55. /**
  56. * Packed style retime configuration.
  57. * There are two registers cfg0 and cfg1 in this style for each bank.
  58. * Each field in this register is 8 bit corresponding to 8 pins in the bank.
  59. */
  60. #define RT_P_CFGS_PER_BANK 2
  61. #define RT_P_CFG0_CLK1NOTCLK0_FIELD(reg) REG_FIELD(reg, 0, 7)
  62. #define RT_P_CFG0_DELAY_0_FIELD(reg) REG_FIELD(reg, 16, 23)
  63. #define RT_P_CFG0_DELAY_1_FIELD(reg) REG_FIELD(reg, 24, 31)
  64. #define RT_P_CFG1_INVERTCLK_FIELD(reg) REG_FIELD(reg, 0, 7)
  65. #define RT_P_CFG1_RETIME_FIELD(reg) REG_FIELD(reg, 8, 15)
  66. #define RT_P_CFG1_CLKNOTDATA_FIELD(reg) REG_FIELD(reg, 16, 23)
  67. #define RT_P_CFG1_DOUBLE_EDGE_FIELD(reg) REG_FIELD(reg, 24, 31)
  68. /**
  69. * Dedicated style retime Configuration register
  70. * each register is dedicated per pin.
  71. */
  72. #define RT_D_CFGS_PER_BANK 8
  73. #define RT_D_CFG_CLK_SHIFT 0
  74. #define RT_D_CFG_CLK_MASK (0x3 << 0)
  75. #define RT_D_CFG_CLKNOTDATA_SHIFT 2
  76. #define RT_D_CFG_CLKNOTDATA_MASK BIT(2)
  77. #define RT_D_CFG_DELAY_SHIFT 3
  78. #define RT_D_CFG_DELAY_MASK (0xf << 3)
  79. #define RT_D_CFG_DELAY_INNOTOUT_SHIFT 7
  80. #define RT_D_CFG_DELAY_INNOTOUT_MASK BIT(7)
  81. #define RT_D_CFG_DOUBLE_EDGE_SHIFT 8
  82. #define RT_D_CFG_DOUBLE_EDGE_MASK BIT(8)
  83. #define RT_D_CFG_INVERTCLK_SHIFT 9
  84. #define RT_D_CFG_INVERTCLK_MASK BIT(9)
  85. #define RT_D_CFG_RETIME_SHIFT 10
  86. #define RT_D_CFG_RETIME_MASK BIT(10)
  87. /*
  88. * Pinconf is represented in an opaque unsigned long variable.
  89. * Below is the bit allocation details for each possible configuration.
  90. * All the bit fields can be encapsulated into four variables
  91. * (direction, retime-type, retime-clk, retime-delay)
  92. *
  93. * +----------------+
  94. *[31:28]| reserved-3 |
  95. * +----------------+-------------
  96. *[27] | oe | |
  97. * +----------------+ v
  98. *[26] | pu | [Direction ]
  99. * +----------------+ ^
  100. *[25] | od | |
  101. * +----------------+-------------
  102. *[24] | reserved-2 |
  103. * +----------------+-------------
  104. *[23] | retime | |
  105. * +----------------+ |
  106. *[22] | retime-invclk | |
  107. * +----------------+ v
  108. *[21] |retime-clknotdat| [Retime-type ]
  109. * +----------------+ ^
  110. *[20] | retime-de | |
  111. * +----------------+-------------
  112. *[19:18]| retime-clk |------>[Retime-Clk ]
  113. * +----------------+
  114. *[17:16]| reserved-1 |
  115. * +----------------+
  116. *[15..0]| retime-delay |------>[Retime Delay]
  117. * +----------------+
  118. */
  119. #define ST_PINCONF_UNPACK(conf, param)\
  120. ((conf >> ST_PINCONF_ ##param ##_SHIFT) \
  121. & ST_PINCONF_ ##param ##_MASK)
  122. #define ST_PINCONF_PACK(conf, val, param) (conf |=\
  123. ((val & ST_PINCONF_ ##param ##_MASK) << \
  124. ST_PINCONF_ ##param ##_SHIFT))
  125. /* Output enable */
  126. #define ST_PINCONF_OE_MASK 0x1
  127. #define ST_PINCONF_OE_SHIFT 27
  128. #define ST_PINCONF_OE BIT(27)
  129. #define ST_PINCONF_UNPACK_OE(conf) ST_PINCONF_UNPACK(conf, OE)
  130. #define ST_PINCONF_PACK_OE(conf) ST_PINCONF_PACK(conf, 1, OE)
  131. /* Pull Up */
  132. #define ST_PINCONF_PU_MASK 0x1
  133. #define ST_PINCONF_PU_SHIFT 26
  134. #define ST_PINCONF_PU BIT(26)
  135. #define ST_PINCONF_UNPACK_PU(conf) ST_PINCONF_UNPACK(conf, PU)
  136. #define ST_PINCONF_PACK_PU(conf) ST_PINCONF_PACK(conf, 1, PU)
  137. /* Open Drain */
  138. #define ST_PINCONF_OD_MASK 0x1
  139. #define ST_PINCONF_OD_SHIFT 25
  140. #define ST_PINCONF_OD BIT(25)
  141. #define ST_PINCONF_UNPACK_OD(conf) ST_PINCONF_UNPACK(conf, OD)
  142. #define ST_PINCONF_PACK_OD(conf) ST_PINCONF_PACK(conf, 1, OD)
  143. #define ST_PINCONF_RT_MASK 0x1
  144. #define ST_PINCONF_RT_SHIFT 23
  145. #define ST_PINCONF_RT BIT(23)
  146. #define ST_PINCONF_UNPACK_RT(conf) ST_PINCONF_UNPACK(conf, RT)
  147. #define ST_PINCONF_PACK_RT(conf) ST_PINCONF_PACK(conf, 1, RT)
  148. #define ST_PINCONF_RT_INVERTCLK_MASK 0x1
  149. #define ST_PINCONF_RT_INVERTCLK_SHIFT 22
  150. #define ST_PINCONF_RT_INVERTCLK BIT(22)
  151. #define ST_PINCONF_UNPACK_RT_INVERTCLK(conf) \
  152. ST_PINCONF_UNPACK(conf, RT_INVERTCLK)
  153. #define ST_PINCONF_PACK_RT_INVERTCLK(conf) \
  154. ST_PINCONF_PACK(conf, 1, RT_INVERTCLK)
  155. #define ST_PINCONF_RT_CLKNOTDATA_MASK 0x1
  156. #define ST_PINCONF_RT_CLKNOTDATA_SHIFT 21
  157. #define ST_PINCONF_RT_CLKNOTDATA BIT(21)
  158. #define ST_PINCONF_UNPACK_RT_CLKNOTDATA(conf) \
  159. ST_PINCONF_UNPACK(conf, RT_CLKNOTDATA)
  160. #define ST_PINCONF_PACK_RT_CLKNOTDATA(conf) \
  161. ST_PINCONF_PACK(conf, 1, RT_CLKNOTDATA)
  162. #define ST_PINCONF_RT_DOUBLE_EDGE_MASK 0x1
  163. #define ST_PINCONF_RT_DOUBLE_EDGE_SHIFT 20
  164. #define ST_PINCONF_RT_DOUBLE_EDGE BIT(20)
  165. #define ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(conf) \
  166. ST_PINCONF_UNPACK(conf, RT_DOUBLE_EDGE)
  167. #define ST_PINCONF_PACK_RT_DOUBLE_EDGE(conf) \
  168. ST_PINCONF_PACK(conf, 1, RT_DOUBLE_EDGE)
  169. #define ST_PINCONF_RT_CLK_MASK 0x3
  170. #define ST_PINCONF_RT_CLK_SHIFT 18
  171. #define ST_PINCONF_RT_CLK BIT(18)
  172. #define ST_PINCONF_UNPACK_RT_CLK(conf) ST_PINCONF_UNPACK(conf, RT_CLK)
  173. #define ST_PINCONF_PACK_RT_CLK(conf, val) ST_PINCONF_PACK(conf, val, RT_CLK)
  174. /* RETIME_DELAY in Pico Secs */
  175. #define ST_PINCONF_RT_DELAY_MASK 0xffff
  176. #define ST_PINCONF_RT_DELAY_SHIFT 0
  177. #define ST_PINCONF_UNPACK_RT_DELAY(conf) ST_PINCONF_UNPACK(conf, RT_DELAY)
  178. #define ST_PINCONF_PACK_RT_DELAY(conf, val) \
  179. ST_PINCONF_PACK(conf, val, RT_DELAY)
  180. #define ST_GPIO_PINS_PER_BANK (8)
  181. #define OF_GPIO_ARGS_MIN (4)
  182. #define OF_RT_ARGS_MIN (2)
  183. #define gpio_range_to_bank(chip) \
  184. container_of(chip, struct st_gpio_bank, range)
  185. #define gpio_chip_to_bank(chip) \
  186. container_of(chip, struct st_gpio_bank, gpio_chip)
  187. enum st_retime_style {
  188. st_retime_style_none,
  189. st_retime_style_packed,
  190. st_retime_style_dedicated,
  191. };
  192. struct st_retime_dedicated {
  193. struct regmap_field *rt[ST_GPIO_PINS_PER_BANK];
  194. };
  195. struct st_retime_packed {
  196. struct regmap_field *clk1notclk0;
  197. struct regmap_field *delay_0;
  198. struct regmap_field *delay_1;
  199. struct regmap_field *invertclk;
  200. struct regmap_field *retime;
  201. struct regmap_field *clknotdata;
  202. struct regmap_field *double_edge;
  203. };
  204. struct st_pio_control {
  205. u32 rt_pin_mask;
  206. struct regmap_field *alt, *oe, *pu, *od;
  207. /* retiming */
  208. union {
  209. struct st_retime_packed rt_p;
  210. struct st_retime_dedicated rt_d;
  211. } rt;
  212. };
  213. struct st_pctl_data {
  214. enum st_retime_style rt_style;
  215. unsigned int *input_delays;
  216. int ninput_delays;
  217. unsigned int *output_delays;
  218. int noutput_delays;
  219. /* register offset information */
  220. int alt, oe, pu, od, rt;
  221. };
  222. struct st_pinconf {
  223. int pin;
  224. const char *name;
  225. unsigned long config;
  226. int altfunc;
  227. };
  228. struct st_pmx_func {
  229. const char *name;
  230. const char **groups;
  231. unsigned ngroups;
  232. };
  233. struct st_pctl_group {
  234. const char *name;
  235. unsigned int *pins;
  236. unsigned npins;
  237. struct st_pinconf *pin_conf;
  238. };
  239. struct st_gpio_bank {
  240. struct gpio_chip gpio_chip;
  241. struct pinctrl_gpio_range range;
  242. void __iomem *base;
  243. struct st_pio_control pc;
  244. };
  245. struct st_pinctrl {
  246. struct device *dev;
  247. struct pinctrl_dev *pctl;
  248. struct st_gpio_bank *banks;
  249. int nbanks;
  250. struct st_pmx_func *functions;
  251. int nfunctions;
  252. struct st_pctl_group *groups;
  253. int ngroups;
  254. struct regmap *regmap;
  255. const struct st_pctl_data *data;
  256. };
  257. /* SOC specific data */
  258. /* STiH415 data */
  259. unsigned int stih415_input_delays[] = {0, 500, 1000, 1500};
  260. unsigned int stih415_output_delays[] = {0, 1000, 2000, 3000};
  261. #define STIH415_PCTRL_COMMON_DATA \
  262. .rt_style = st_retime_style_packed, \
  263. .input_delays = stih415_input_delays, \
  264. .ninput_delays = 4, \
  265. .output_delays = stih415_output_delays, \
  266. .noutput_delays = 4
  267. static const struct st_pctl_data stih415_sbc_data = {
  268. STIH415_PCTRL_COMMON_DATA,
  269. .alt = 0, .oe = 5, .pu = 7, .od = 9, .rt = 16,
  270. };
  271. static const struct st_pctl_data stih415_front_data = {
  272. STIH415_PCTRL_COMMON_DATA,
  273. .alt = 0, .oe = 8, .pu = 10, .od = 12, .rt = 16,
  274. };
  275. static const struct st_pctl_data stih415_rear_data = {
  276. STIH415_PCTRL_COMMON_DATA,
  277. .alt = 0, .oe = 6, .pu = 8, .od = 10, .rt = 38,
  278. };
  279. static const struct st_pctl_data stih415_left_data = {
  280. STIH415_PCTRL_COMMON_DATA,
  281. .alt = 0, .oe = 3, .pu = 4, .od = 5, .rt = 6,
  282. };
  283. static const struct st_pctl_data stih415_right_data = {
  284. STIH415_PCTRL_COMMON_DATA,
  285. .alt = 0, .oe = 5, .pu = 7, .od = 9, .rt = 11,
  286. };
  287. /* STiH416 data */
  288. unsigned int stih416_delays[] = {0, 300, 500, 750, 1000, 1250, 1500,
  289. 1750, 2000, 2250, 2500, 2750, 3000, 3250 };
  290. static const struct st_pctl_data stih416_data = {
  291. .rt_style = st_retime_style_dedicated,
  292. .input_delays = stih416_delays,
  293. .ninput_delays = 14,
  294. .output_delays = stih416_delays,
  295. .noutput_delays = 14,
  296. .alt = 0, .oe = 40, .pu = 50, .od = 60, .rt = 100,
  297. };
  298. /* Low level functions.. */
  299. static inline int st_gpio_bank(int gpio)
  300. {
  301. return gpio/ST_GPIO_PINS_PER_BANK;
  302. }
  303. static inline int st_gpio_pin(int gpio)
  304. {
  305. return gpio%ST_GPIO_PINS_PER_BANK;
  306. }
  307. static void st_pinconf_set_config(struct st_pio_control *pc,
  308. int pin, unsigned long config)
  309. {
  310. struct regmap_field *output_enable = pc->oe;
  311. struct regmap_field *pull_up = pc->pu;
  312. struct regmap_field *open_drain = pc->od;
  313. unsigned int oe_value, pu_value, od_value;
  314. unsigned long mask = BIT(pin);
  315. regmap_field_read(output_enable, &oe_value);
  316. regmap_field_read(pull_up, &pu_value);
  317. regmap_field_read(open_drain, &od_value);
  318. /* Clear old values */
  319. oe_value &= ~mask;
  320. pu_value &= ~mask;
  321. od_value &= ~mask;
  322. if (config & ST_PINCONF_OE)
  323. oe_value |= mask;
  324. if (config & ST_PINCONF_PU)
  325. pu_value |= mask;
  326. if (config & ST_PINCONF_OD)
  327. od_value |= mask;
  328. regmap_field_write(output_enable, oe_value);
  329. regmap_field_write(pull_up, pu_value);
  330. regmap_field_write(open_drain, od_value);
  331. }
  332. static void st_pctl_set_function(struct st_pio_control *pc,
  333. int pin_id, int function)
  334. {
  335. struct regmap_field *alt = pc->alt;
  336. unsigned int val;
  337. int pin = st_gpio_pin(pin_id);
  338. int offset = pin * 4;
  339. regmap_field_read(alt, &val);
  340. val &= ~(0xf << offset);
  341. val |= function << offset;
  342. regmap_field_write(alt, val);
  343. }
  344. static unsigned long st_pinconf_delay_to_bit(unsigned int delay,
  345. const struct st_pctl_data *data, unsigned long config)
  346. {
  347. unsigned int *delay_times;
  348. int num_delay_times, i, closest_index = -1;
  349. unsigned int closest_divergence = UINT_MAX;
  350. if (ST_PINCONF_UNPACK_OE(config)) {
  351. delay_times = data->output_delays;
  352. num_delay_times = data->noutput_delays;
  353. } else {
  354. delay_times = data->input_delays;
  355. num_delay_times = data->ninput_delays;
  356. }
  357. for (i = 0; i < num_delay_times; i++) {
  358. unsigned int divergence = abs(delay - delay_times[i]);
  359. if (divergence == 0)
  360. return i;
  361. if (divergence < closest_divergence) {
  362. closest_divergence = divergence;
  363. closest_index = i;
  364. }
  365. }
  366. pr_warn("Attempt to set delay %d, closest available %d\n",
  367. delay, delay_times[closest_index]);
  368. return closest_index;
  369. }
  370. static unsigned long st_pinconf_bit_to_delay(unsigned int index,
  371. const struct st_pctl_data *data, unsigned long output)
  372. {
  373. unsigned int *delay_times;
  374. int num_delay_times;
  375. if (output) {
  376. delay_times = data->output_delays;
  377. num_delay_times = data->noutput_delays;
  378. } else {
  379. delay_times = data->input_delays;
  380. num_delay_times = data->ninput_delays;
  381. }
  382. if (index < num_delay_times) {
  383. return delay_times[index];
  384. } else {
  385. pr_warn("Delay not found in/out delay list\n");
  386. return 0;
  387. }
  388. }
  389. static void st_regmap_field_bit_set_clear_pin(struct regmap_field *field,
  390. int enable, int pin)
  391. {
  392. unsigned int val = 0;
  393. regmap_field_read(field, &val);
  394. if (enable)
  395. val |= BIT(pin);
  396. else
  397. val &= ~BIT(pin);
  398. regmap_field_write(field, val);
  399. }
  400. static void st_pinconf_set_retime_packed(struct st_pinctrl *info,
  401. struct st_pio_control *pc, unsigned long config, int pin)
  402. {
  403. const struct st_pctl_data *data = info->data;
  404. struct st_retime_packed *rt_p = &pc->rt.rt_p;
  405. unsigned int delay;
  406. st_regmap_field_bit_set_clear_pin(rt_p->clk1notclk0,
  407. ST_PINCONF_UNPACK_RT_CLK(config), pin);
  408. st_regmap_field_bit_set_clear_pin(rt_p->clknotdata,
  409. ST_PINCONF_UNPACK_RT_CLKNOTDATA(config), pin);
  410. st_regmap_field_bit_set_clear_pin(rt_p->double_edge,
  411. ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config), pin);
  412. st_regmap_field_bit_set_clear_pin(rt_p->invertclk,
  413. ST_PINCONF_UNPACK_RT_INVERTCLK(config), pin);
  414. st_regmap_field_bit_set_clear_pin(rt_p->retime,
  415. ST_PINCONF_UNPACK_RT(config), pin);
  416. delay = st_pinconf_delay_to_bit(ST_PINCONF_UNPACK_RT_DELAY(config),
  417. data, config);
  418. /* 2 bit delay, lsb */
  419. st_regmap_field_bit_set_clear_pin(rt_p->delay_0, delay & 0x1, pin);
  420. /* 2 bit delay, msb */
  421. st_regmap_field_bit_set_clear_pin(rt_p->delay_1, delay & 0x2, pin);
  422. }
  423. static void st_pinconf_set_retime_dedicated(struct st_pinctrl *info,
  424. struct st_pio_control *pc, unsigned long config, int pin)
  425. {
  426. int input = ST_PINCONF_UNPACK_OE(config) ? 0 : 1;
  427. int clk = ST_PINCONF_UNPACK_RT_CLK(config);
  428. int clknotdata = ST_PINCONF_UNPACK_RT_CLKNOTDATA(config);
  429. int double_edge = ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config);
  430. int invertclk = ST_PINCONF_UNPACK_RT_INVERTCLK(config);
  431. int retime = ST_PINCONF_UNPACK_RT(config);
  432. unsigned long delay = st_pinconf_delay_to_bit(
  433. ST_PINCONF_UNPACK_RT_DELAY(config),
  434. info->data, config);
  435. struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
  436. unsigned long retime_config =
  437. ((clk) << RT_D_CFG_CLK_SHIFT) |
  438. ((delay) << RT_D_CFG_DELAY_SHIFT) |
  439. ((input) << RT_D_CFG_DELAY_INNOTOUT_SHIFT) |
  440. ((retime) << RT_D_CFG_RETIME_SHIFT) |
  441. ((clknotdata) << RT_D_CFG_CLKNOTDATA_SHIFT) |
  442. ((invertclk) << RT_D_CFG_INVERTCLK_SHIFT) |
  443. ((double_edge) << RT_D_CFG_DOUBLE_EDGE_SHIFT);
  444. regmap_field_write(rt_d->rt[pin], retime_config);
  445. }
  446. static void st_pinconf_get_direction(struct st_pio_control *pc,
  447. int pin, unsigned long *config)
  448. {
  449. unsigned int oe_value, pu_value, od_value;
  450. regmap_field_read(pc->oe, &oe_value);
  451. regmap_field_read(pc->pu, &pu_value);
  452. regmap_field_read(pc->od, &od_value);
  453. if (oe_value & BIT(pin))
  454. ST_PINCONF_PACK_OE(*config);
  455. if (pu_value & BIT(pin))
  456. ST_PINCONF_PACK_PU(*config);
  457. if (od_value & BIT(pin))
  458. ST_PINCONF_PACK_OD(*config);
  459. }
  460. static int st_pinconf_get_retime_packed(struct st_pinctrl *info,
  461. struct st_pio_control *pc, int pin, unsigned long *config)
  462. {
  463. const struct st_pctl_data *data = info->data;
  464. struct st_retime_packed *rt_p = &pc->rt.rt_p;
  465. unsigned int delay_bits, delay, delay0, delay1, val;
  466. int output = ST_PINCONF_UNPACK_OE(*config);
  467. if (!regmap_field_read(rt_p->retime, &val) && (val & BIT(pin)))
  468. ST_PINCONF_PACK_RT(*config);
  469. if (!regmap_field_read(rt_p->clk1notclk0, &val) && (val & BIT(pin)))
  470. ST_PINCONF_PACK_RT_CLK(*config, 1);
  471. if (!regmap_field_read(rt_p->clknotdata, &val) && (val & BIT(pin)))
  472. ST_PINCONF_PACK_RT_CLKNOTDATA(*config);
  473. if (!regmap_field_read(rt_p->double_edge, &val) && (val & BIT(pin)))
  474. ST_PINCONF_PACK_RT_DOUBLE_EDGE(*config);
  475. if (!regmap_field_read(rt_p->invertclk, &val) && (val & BIT(pin)))
  476. ST_PINCONF_PACK_RT_INVERTCLK(*config);
  477. regmap_field_read(rt_p->delay_0, &delay0);
  478. regmap_field_read(rt_p->delay_1, &delay1);
  479. delay_bits = (((delay1 & BIT(pin)) ? 1 : 0) << 1) |
  480. (((delay0 & BIT(pin)) ? 1 : 0));
  481. delay = st_pinconf_bit_to_delay(delay_bits, data, output);
  482. ST_PINCONF_PACK_RT_DELAY(*config, delay);
  483. return 0;
  484. }
  485. static int st_pinconf_get_retime_dedicated(struct st_pinctrl *info,
  486. struct st_pio_control *pc, int pin, unsigned long *config)
  487. {
  488. unsigned int value;
  489. unsigned long delay_bits, delay, rt_clk;
  490. int output = ST_PINCONF_UNPACK_OE(*config);
  491. struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
  492. regmap_field_read(rt_d->rt[pin], &value);
  493. rt_clk = (value & RT_D_CFG_CLK_MASK) >> RT_D_CFG_CLK_SHIFT;
  494. ST_PINCONF_PACK_RT_CLK(*config, rt_clk);
  495. delay_bits = (value & RT_D_CFG_DELAY_MASK) >> RT_D_CFG_DELAY_SHIFT;
  496. delay = st_pinconf_bit_to_delay(delay_bits, info->data, output);
  497. ST_PINCONF_PACK_RT_DELAY(*config, delay);
  498. if (value & RT_D_CFG_CLKNOTDATA_MASK)
  499. ST_PINCONF_PACK_RT_CLKNOTDATA(*config);
  500. if (value & RT_D_CFG_DOUBLE_EDGE_MASK)
  501. ST_PINCONF_PACK_RT_DOUBLE_EDGE(*config);
  502. if (value & RT_D_CFG_INVERTCLK_MASK)
  503. ST_PINCONF_PACK_RT_INVERTCLK(*config);
  504. if (value & RT_D_CFG_RETIME_MASK)
  505. ST_PINCONF_PACK_RT(*config);
  506. return 0;
  507. }
  508. /* GPIO related functions */
  509. static inline void __st_gpio_set(struct st_gpio_bank *bank,
  510. unsigned offset, int value)
  511. {
  512. if (value)
  513. writel(BIT(offset), bank->base + REG_PIO_SET_POUT);
  514. else
  515. writel(BIT(offset), bank->base + REG_PIO_CLR_POUT);
  516. }
  517. static void st_gpio_direction(struct st_gpio_bank *bank,
  518. unsigned int gpio, unsigned int direction)
  519. {
  520. int offset = st_gpio_pin(gpio);
  521. int i = 0;
  522. /**
  523. * There are three configuration registers (PIOn_PC0, PIOn_PC1
  524. * and PIOn_PC2) for each port. These are used to configure the
  525. * PIO port pins. Each pin can be configured as an input, output,
  526. * bidirectional, or alternative function pin. Three bits, one bit
  527. * from each of the three registers, configure the corresponding bit of
  528. * the port. Valid bit settings is:
  529. *
  530. * PC2 PC1 PC0 Direction.
  531. * 0 0 0 [Input Weak pull-up]
  532. * 0 0 or 1 1 [Bidirection]
  533. * 0 1 0 [Output]
  534. * 1 0 0 [Input]
  535. *
  536. * PIOn_SET_PC and PIOn_CLR_PC registers are used to set and clear bits
  537. * individually.
  538. */
  539. for (i = 0; i <= 2; i++) {
  540. if (direction & BIT(i))
  541. writel(BIT(offset), bank->base + REG_PIO_SET_PC(i));
  542. else
  543. writel(BIT(offset), bank->base + REG_PIO_CLR_PC(i));
  544. }
  545. }
  546. static int st_gpio_request(struct gpio_chip *chip, unsigned offset)
  547. {
  548. return pinctrl_request_gpio(chip->base + offset);
  549. }
  550. static void st_gpio_free(struct gpio_chip *chip, unsigned offset)
  551. {
  552. pinctrl_free_gpio(chip->base + offset);
  553. }
  554. static int st_gpio_get(struct gpio_chip *chip, unsigned offset)
  555. {
  556. struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
  557. return !!(readl(bank->base + REG_PIO_PIN) & BIT(offset));
  558. }
  559. static void st_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  560. {
  561. struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
  562. __st_gpio_set(bank, offset, value);
  563. }
  564. static int st_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  565. {
  566. pinctrl_gpio_direction_input(chip->base + offset);
  567. return 0;
  568. }
  569. static int st_gpio_direction_output(struct gpio_chip *chip,
  570. unsigned offset, int value)
  571. {
  572. struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
  573. __st_gpio_set(bank, offset, value);
  574. pinctrl_gpio_direction_output(chip->base + offset);
  575. return 0;
  576. }
  577. static int st_gpio_xlate(struct gpio_chip *gc,
  578. const struct of_phandle_args *gpiospec, u32 *flags)
  579. {
  580. if (WARN_ON(gc->of_gpio_n_cells < 1))
  581. return -EINVAL;
  582. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  583. return -EINVAL;
  584. if (gpiospec->args[0] > gc->ngpio)
  585. return -EINVAL;
  586. return gpiospec->args[0];
  587. }
  588. /* Pinctrl Groups */
  589. static int st_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  590. {
  591. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  592. return info->ngroups;
  593. }
  594. static const char *st_pctl_get_group_name(struct pinctrl_dev *pctldev,
  595. unsigned selector)
  596. {
  597. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  598. return info->groups[selector].name;
  599. }
  600. static int st_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  601. unsigned selector, const unsigned **pins, unsigned *npins)
  602. {
  603. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  604. if (selector >= info->ngroups)
  605. return -EINVAL;
  606. *pins = info->groups[selector].pins;
  607. *npins = info->groups[selector].npins;
  608. return 0;
  609. }
  610. static const inline struct st_pctl_group *st_pctl_find_group_by_name(
  611. const struct st_pinctrl *info, const char *name)
  612. {
  613. int i;
  614. for (i = 0; i < info->ngroups; i++) {
  615. if (!strcmp(info->groups[i].name, name))
  616. return &info->groups[i];
  617. }
  618. return NULL;
  619. }
  620. static int st_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  621. struct device_node *np, struct pinctrl_map **map, unsigned *num_maps)
  622. {
  623. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  624. const struct st_pctl_group *grp;
  625. struct pinctrl_map *new_map;
  626. struct device_node *parent;
  627. int map_num, i;
  628. grp = st_pctl_find_group_by_name(info, np->name);
  629. if (!grp) {
  630. dev_err(info->dev, "unable to find group for node %s\n",
  631. np->name);
  632. return -EINVAL;
  633. }
  634. map_num = grp->npins + 1;
  635. new_map = devm_kzalloc(pctldev->dev,
  636. sizeof(*new_map) * map_num, GFP_KERNEL);
  637. if (!new_map)
  638. return -ENOMEM;
  639. parent = of_get_parent(np);
  640. if (!parent) {
  641. devm_kfree(pctldev->dev, new_map);
  642. return -EINVAL;
  643. }
  644. *map = new_map;
  645. *num_maps = map_num;
  646. new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
  647. new_map[0].data.mux.function = parent->name;
  648. new_map[0].data.mux.group = np->name;
  649. of_node_put(parent);
  650. /* create config map per pin */
  651. new_map++;
  652. for (i = 0; i < grp->npins; i++) {
  653. new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
  654. new_map[i].data.configs.group_or_pin =
  655. pin_get_name(pctldev, grp->pins[i]);
  656. new_map[i].data.configs.configs = &grp->pin_conf[i].config;
  657. new_map[i].data.configs.num_configs = 1;
  658. }
  659. dev_info(pctldev->dev, "maps: function %s group %s num %d\n",
  660. (*map)->data.mux.function, grp->name, map_num);
  661. return 0;
  662. }
  663. static void st_pctl_dt_free_map(struct pinctrl_dev *pctldev,
  664. struct pinctrl_map *map, unsigned num_maps)
  665. {
  666. }
  667. static struct pinctrl_ops st_pctlops = {
  668. .get_groups_count = st_pctl_get_groups_count,
  669. .get_group_pins = st_pctl_get_group_pins,
  670. .get_group_name = st_pctl_get_group_name,
  671. .dt_node_to_map = st_pctl_dt_node_to_map,
  672. .dt_free_map = st_pctl_dt_free_map,
  673. };
  674. /* Pinmux */
  675. static int st_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  676. {
  677. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  678. return info->nfunctions;
  679. }
  680. const char *st_pmx_get_fname(struct pinctrl_dev *pctldev,
  681. unsigned selector)
  682. {
  683. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  684. return info->functions[selector].name;
  685. }
  686. static int st_pmx_get_groups(struct pinctrl_dev *pctldev,
  687. unsigned selector, const char * const **grps, unsigned * const ngrps)
  688. {
  689. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  690. *grps = info->functions[selector].groups;
  691. *ngrps = info->functions[selector].ngroups;
  692. return 0;
  693. }
  694. static struct st_pio_control *st_get_pio_control(
  695. struct pinctrl_dev *pctldev, int pin)
  696. {
  697. struct pinctrl_gpio_range *range =
  698. pinctrl_find_gpio_range_from_pin(pctldev, pin);
  699. struct st_gpio_bank *bank = gpio_range_to_bank(range);
  700. return &bank->pc;
  701. }
  702. static int st_pmx_enable(struct pinctrl_dev *pctldev, unsigned fselector,
  703. unsigned group)
  704. {
  705. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  706. struct st_pinconf *conf = info->groups[group].pin_conf;
  707. struct st_pio_control *pc;
  708. int i;
  709. for (i = 0; i < info->groups[group].npins; i++) {
  710. pc = st_get_pio_control(pctldev, conf[i].pin);
  711. st_pctl_set_function(pc, conf[i].pin, conf[i].altfunc);
  712. }
  713. return 0;
  714. }
  715. static void st_pmx_disable(struct pinctrl_dev *pctldev, unsigned selector,
  716. unsigned group)
  717. {
  718. }
  719. static int st_pmx_set_gpio_direction(struct pinctrl_dev *pctldev,
  720. struct pinctrl_gpio_range *range, unsigned gpio,
  721. bool input)
  722. {
  723. struct st_gpio_bank *bank = gpio_range_to_bank(range);
  724. /*
  725. * When a PIO bank is used in its primary function mode (altfunc = 0)
  726. * Output Enable (OE), Open Drain(OD), and Pull Up (PU)
  727. * for the primary PIO functions are driven by the related PIO block
  728. */
  729. st_pctl_set_function(&bank->pc, gpio, 0);
  730. st_gpio_direction(bank, gpio, input ?
  731. ST_GPIO_DIRECTION_IN : ST_GPIO_DIRECTION_OUT);
  732. return 0;
  733. }
  734. static struct pinmux_ops st_pmxops = {
  735. .get_functions_count = st_pmx_get_funcs_count,
  736. .get_function_name = st_pmx_get_fname,
  737. .get_function_groups = st_pmx_get_groups,
  738. .enable = st_pmx_enable,
  739. .disable = st_pmx_disable,
  740. .gpio_set_direction = st_pmx_set_gpio_direction,
  741. };
  742. /* Pinconf */
  743. static void st_pinconf_get_retime(struct st_pinctrl *info,
  744. struct st_pio_control *pc, int pin, unsigned long *config)
  745. {
  746. if (info->data->rt_style == st_retime_style_packed)
  747. st_pinconf_get_retime_packed(info, pc, pin, config);
  748. else if (info->data->rt_style == st_retime_style_dedicated)
  749. if ((BIT(pin) & pc->rt_pin_mask))
  750. st_pinconf_get_retime_dedicated(info, pc,
  751. pin, config);
  752. }
  753. static void st_pinconf_set_retime(struct st_pinctrl *info,
  754. struct st_pio_control *pc, int pin, unsigned long config)
  755. {
  756. if (info->data->rt_style == st_retime_style_packed)
  757. st_pinconf_set_retime_packed(info, pc, config, pin);
  758. else if (info->data->rt_style == st_retime_style_dedicated)
  759. if ((BIT(pin) & pc->rt_pin_mask))
  760. st_pinconf_set_retime_dedicated(info, pc,
  761. config, pin);
  762. }
  763. static int st_pinconf_set(struct pinctrl_dev *pctldev,
  764. unsigned pin_id, unsigned long config)
  765. {
  766. int pin = st_gpio_pin(pin_id);
  767. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  768. struct st_pio_control *pc = st_get_pio_control(pctldev, pin_id);
  769. st_pinconf_set_config(pc, pin, config);
  770. st_pinconf_set_retime(info, pc, pin, config);
  771. return 0;
  772. }
  773. static int st_pinconf_get(struct pinctrl_dev *pctldev,
  774. unsigned pin_id, unsigned long *config)
  775. {
  776. int pin = st_gpio_pin(pin_id);
  777. struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  778. struct st_pio_control *pc = st_get_pio_control(pctldev, pin_id);
  779. *config = 0;
  780. st_pinconf_get_direction(pc, pin, config);
  781. st_pinconf_get_retime(info, pc, pin, config);
  782. return 0;
  783. }
  784. static void st_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  785. struct seq_file *s, unsigned pin_id)
  786. {
  787. unsigned long config;
  788. st_pinconf_get(pctldev, pin_id, &config);
  789. seq_printf(s, "[OE:%ld,PU:%ld,OD:%ld]\n"
  790. "\t\t[retime:%ld,invclk:%ld,clknotdat:%ld,"
  791. "de:%ld,rt-clk:%ld,rt-delay:%ld]",
  792. ST_PINCONF_UNPACK_OE(config),
  793. ST_PINCONF_UNPACK_PU(config),
  794. ST_PINCONF_UNPACK_OD(config),
  795. ST_PINCONF_UNPACK_RT(config),
  796. ST_PINCONF_UNPACK_RT_INVERTCLK(config),
  797. ST_PINCONF_UNPACK_RT_CLKNOTDATA(config),
  798. ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config),
  799. ST_PINCONF_UNPACK_RT_CLK(config),
  800. ST_PINCONF_UNPACK_RT_DELAY(config));
  801. }
  802. static struct pinconf_ops st_confops = {
  803. .pin_config_get = st_pinconf_get,
  804. .pin_config_set = st_pinconf_set,
  805. .pin_config_dbg_show = st_pinconf_dbg_show,
  806. };
  807. static void st_pctl_dt_child_count(struct st_pinctrl *info,
  808. struct device_node *np)
  809. {
  810. struct device_node *child;
  811. for_each_child_of_node(np, child) {
  812. if (of_property_read_bool(child, "gpio-controller")) {
  813. info->nbanks++;
  814. } else {
  815. info->nfunctions++;
  816. info->ngroups += of_get_child_count(child);
  817. }
  818. }
  819. }
  820. static int st_pctl_dt_setup_retime_packed(struct st_pinctrl *info,
  821. int bank, struct st_pio_control *pc)
  822. {
  823. struct device *dev = info->dev;
  824. struct regmap *rm = info->regmap;
  825. const struct st_pctl_data *data = info->data;
  826. /* 2 registers per bank */
  827. int reg = (data->rt + bank * RT_P_CFGS_PER_BANK) * 4;
  828. struct st_retime_packed *rt_p = &pc->rt.rt_p;
  829. /* cfg0 */
  830. struct reg_field clk1notclk0 = RT_P_CFG0_CLK1NOTCLK0_FIELD(reg);
  831. struct reg_field delay_0 = RT_P_CFG0_DELAY_0_FIELD(reg);
  832. struct reg_field delay_1 = RT_P_CFG0_DELAY_1_FIELD(reg);
  833. /* cfg1 */
  834. struct reg_field invertclk = RT_P_CFG1_INVERTCLK_FIELD(reg + 4);
  835. struct reg_field retime = RT_P_CFG1_RETIME_FIELD(reg + 4);
  836. struct reg_field clknotdata = RT_P_CFG1_CLKNOTDATA_FIELD(reg + 4);
  837. struct reg_field double_edge = RT_P_CFG1_DOUBLE_EDGE_FIELD(reg + 4);
  838. rt_p->clk1notclk0 = devm_regmap_field_alloc(dev, rm, clk1notclk0);
  839. rt_p->delay_0 = devm_regmap_field_alloc(dev, rm, delay_0);
  840. rt_p->delay_1 = devm_regmap_field_alloc(dev, rm, delay_1);
  841. rt_p->invertclk = devm_regmap_field_alloc(dev, rm, invertclk);
  842. rt_p->retime = devm_regmap_field_alloc(dev, rm, retime);
  843. rt_p->clknotdata = devm_regmap_field_alloc(dev, rm, clknotdata);
  844. rt_p->double_edge = devm_regmap_field_alloc(dev, rm, double_edge);
  845. if (IS_ERR(rt_p->clk1notclk0) || IS_ERR(rt_p->delay_0) ||
  846. IS_ERR(rt_p->delay_1) || IS_ERR(rt_p->invertclk) ||
  847. IS_ERR(rt_p->retime) || IS_ERR(rt_p->clknotdata) ||
  848. IS_ERR(rt_p->double_edge))
  849. return -EINVAL;
  850. return 0;
  851. }
  852. static int st_pctl_dt_setup_retime_dedicated(struct st_pinctrl *info,
  853. int bank, struct st_pio_control *pc)
  854. {
  855. struct device *dev = info->dev;
  856. struct regmap *rm = info->regmap;
  857. const struct st_pctl_data *data = info->data;
  858. /* 8 registers per bank */
  859. int reg_offset = (data->rt + bank * RT_D_CFGS_PER_BANK) * 4;
  860. struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
  861. unsigned int j;
  862. u32 pin_mask = pc->rt_pin_mask;
  863. for (j = 0; j < RT_D_CFGS_PER_BANK; j++) {
  864. if (BIT(j) & pin_mask) {
  865. struct reg_field reg = REG_FIELD(reg_offset, 0, 31);
  866. rt_d->rt[j] = devm_regmap_field_alloc(dev, rm, reg);
  867. if (IS_ERR(rt_d->rt[j]))
  868. return -EINVAL;
  869. reg_offset += 4;
  870. }
  871. }
  872. return 0;
  873. }
  874. static int st_pctl_dt_setup_retime(struct st_pinctrl *info,
  875. int bank, struct st_pio_control *pc)
  876. {
  877. const struct st_pctl_data *data = info->data;
  878. if (data->rt_style == st_retime_style_packed)
  879. return st_pctl_dt_setup_retime_packed(info, bank, pc);
  880. else if (data->rt_style == st_retime_style_dedicated)
  881. return st_pctl_dt_setup_retime_dedicated(info, bank, pc);
  882. return -EINVAL;
  883. }
  884. static int st_parse_syscfgs(struct st_pinctrl *info,
  885. int bank, struct device_node *np)
  886. {
  887. const struct st_pctl_data *data = info->data;
  888. /**
  889. * For a given shared register like OE/PU/OD, there are 8 bits per bank
  890. * 0:7 belongs to bank0, 8:15 belongs to bank1 ...
  891. * So each register is shared across 4 banks.
  892. */
  893. int lsb = (bank%4) * ST_GPIO_PINS_PER_BANK;
  894. int msb = lsb + ST_GPIO_PINS_PER_BANK - 1;
  895. struct reg_field alt_reg = REG_FIELD((data->alt + bank) * 4, 0, 31);
  896. struct reg_field oe_reg = REG_FIELD((data->oe + bank/4) * 4, lsb, msb);
  897. struct reg_field pu_reg = REG_FIELD((data->pu + bank/4) * 4, lsb, msb);
  898. struct reg_field od_reg = REG_FIELD((data->od + bank/4) * 4, lsb, msb);
  899. struct st_pio_control *pc = &info->banks[bank].pc;
  900. struct device *dev = info->dev;
  901. struct regmap *regmap = info->regmap;
  902. pc->alt = devm_regmap_field_alloc(dev, regmap, alt_reg);
  903. pc->oe = devm_regmap_field_alloc(dev, regmap, oe_reg);
  904. pc->pu = devm_regmap_field_alloc(dev, regmap, pu_reg);
  905. pc->od = devm_regmap_field_alloc(dev, regmap, od_reg);
  906. if (IS_ERR(pc->alt) || IS_ERR(pc->oe) ||
  907. IS_ERR(pc->pu) || IS_ERR(pc->od))
  908. return -EINVAL;
  909. /* retime avaiable for all pins by default */
  910. pc->rt_pin_mask = 0xff;
  911. of_property_read_u32(np, "st,retime-pin-mask", &pc->rt_pin_mask);
  912. st_pctl_dt_setup_retime(info, bank, pc);
  913. return 0;
  914. }
  915. /*
  916. * Each pin is represented in of the below forms.
  917. * <bank offset mux direction rt_type rt_delay rt_clk>
  918. */
  919. static int st_pctl_dt_parse_groups(struct device_node *np,
  920. struct st_pctl_group *grp, struct st_pinctrl *info, int idx)
  921. {
  922. /* bank pad direction val altfunction */
  923. const __be32 *list;
  924. struct property *pp;
  925. struct st_pinconf *conf;
  926. phandle phandle;
  927. struct device_node *pins;
  928. u32 pin;
  929. int i = 0, npins = 0, nr_props;
  930. pins = of_get_child_by_name(np, "st,pins");
  931. if (!pins)
  932. return -ENODATA;
  933. for_each_property_of_node(pins, pp) {
  934. /* Skip those we do not want to proceed */
  935. if (!strcmp(pp->name, "name"))
  936. continue;
  937. if (pp && (pp->length/sizeof(__be32)) >= OF_GPIO_ARGS_MIN) {
  938. npins++;
  939. } else {
  940. pr_warn("Invalid st,pins in %s node\n", np->name);
  941. return -EINVAL;
  942. }
  943. }
  944. grp->npins = npins;
  945. grp->name = np->name;
  946. grp->pins = devm_kzalloc(info->dev, npins * sizeof(u32), GFP_KERNEL);
  947. grp->pin_conf = devm_kzalloc(info->dev,
  948. npins * sizeof(*conf), GFP_KERNEL);
  949. if (!grp->pins || !grp->pin_conf)
  950. return -ENOMEM;
  951. /* <bank offset mux direction rt_type rt_delay rt_clk> */
  952. for_each_property_of_node(pins, pp) {
  953. if (!strcmp(pp->name, "name"))
  954. continue;
  955. nr_props = pp->length/sizeof(u32);
  956. list = pp->value;
  957. conf = &grp->pin_conf[i];
  958. /* bank & offset */
  959. phandle = be32_to_cpup(list++);
  960. pin = be32_to_cpup(list++);
  961. conf->pin = of_get_named_gpio(pins, pp->name, 0);
  962. conf->name = pp->name;
  963. grp->pins[i] = conf->pin;
  964. /* mux */
  965. conf->altfunc = be32_to_cpup(list++);
  966. conf->config = 0;
  967. /* direction */
  968. conf->config |= be32_to_cpup(list++);
  969. /* rt_type rt_delay rt_clk */
  970. if (nr_props >= OF_GPIO_ARGS_MIN + OF_RT_ARGS_MIN) {
  971. /* rt_type */
  972. conf->config |= be32_to_cpup(list++);
  973. /* rt_delay */
  974. conf->config |= be32_to_cpup(list++);
  975. /* rt_clk */
  976. if (nr_props > OF_GPIO_ARGS_MIN + OF_RT_ARGS_MIN)
  977. conf->config |= be32_to_cpup(list++);
  978. }
  979. i++;
  980. }
  981. of_node_put(pins);
  982. return 0;
  983. }
  984. static int st_pctl_parse_functions(struct device_node *np,
  985. struct st_pinctrl *info, u32 index, int *grp_index)
  986. {
  987. struct device_node *child;
  988. struct st_pmx_func *func;
  989. struct st_pctl_group *grp;
  990. int ret, i;
  991. func = &info->functions[index];
  992. func->name = np->name;
  993. func->ngroups = of_get_child_count(np);
  994. if (func->ngroups <= 0) {
  995. dev_err(info->dev, "No groups defined\n");
  996. return -EINVAL;
  997. }
  998. func->groups = devm_kzalloc(info->dev,
  999. func->ngroups * sizeof(char *), GFP_KERNEL);
  1000. if (!func->groups)
  1001. return -ENOMEM;
  1002. i = 0;
  1003. for_each_child_of_node(np, child) {
  1004. func->groups[i] = child->name;
  1005. grp = &info->groups[*grp_index];
  1006. *grp_index += 1;
  1007. ret = st_pctl_dt_parse_groups(child, grp, info, i++);
  1008. if (ret)
  1009. return ret;
  1010. }
  1011. dev_info(info->dev, "Function[%d\t name:%s,\tgroups:%d]\n",
  1012. index, func->name, func->ngroups);
  1013. return 0;
  1014. }
  1015. static struct gpio_chip st_gpio_template = {
  1016. .request = st_gpio_request,
  1017. .free = st_gpio_free,
  1018. .get = st_gpio_get,
  1019. .set = st_gpio_set,
  1020. .direction_input = st_gpio_direction_input,
  1021. .direction_output = st_gpio_direction_output,
  1022. .ngpio = ST_GPIO_PINS_PER_BANK,
  1023. .of_gpio_n_cells = 1,
  1024. .of_xlate = st_gpio_xlate,
  1025. };
  1026. static int st_gpiolib_register_bank(struct st_pinctrl *info,
  1027. int bank_nr, struct device_node *np)
  1028. {
  1029. struct st_gpio_bank *bank = &info->banks[bank_nr];
  1030. struct pinctrl_gpio_range *range = &bank->range;
  1031. struct device *dev = info->dev;
  1032. int bank_num = of_alias_get_id(np, "gpio");
  1033. struct resource res;
  1034. int err;
  1035. if (of_address_to_resource(np, 0, &res))
  1036. return -ENODEV;
  1037. bank->base = devm_request_and_ioremap(dev, &res);
  1038. if (!bank->base) {
  1039. dev_err(dev, "Can't get IO memory mapping!\n");
  1040. return -ENODEV;
  1041. }
  1042. bank->gpio_chip = st_gpio_template;
  1043. bank->gpio_chip.base = bank_num * ST_GPIO_PINS_PER_BANK;
  1044. bank->gpio_chip.ngpio = ST_GPIO_PINS_PER_BANK;
  1045. bank->gpio_chip.of_node = np;
  1046. of_property_read_string(np, "st,bank-name", &range->name);
  1047. bank->gpio_chip.label = range->name;
  1048. range->id = bank_num;
  1049. range->pin_base = range->base = range->id * ST_GPIO_PINS_PER_BANK;
  1050. range->npins = bank->gpio_chip.ngpio;
  1051. range->gc = &bank->gpio_chip;
  1052. err = gpiochip_add(&bank->gpio_chip);
  1053. if (err) {
  1054. dev_err(dev, "Failed to add gpiochip(%d)!\n", bank_num);
  1055. return err;
  1056. }
  1057. dev_info(dev, "%s bank added.\n", range->name);
  1058. return 0;
  1059. }
  1060. static struct of_device_id st_pctl_of_match[] = {
  1061. { .compatible = "st,stih415-sbc-pinctrl", .data = &stih415_sbc_data },
  1062. { .compatible = "st,stih415-rear-pinctrl", .data = &stih415_rear_data },
  1063. { .compatible = "st,stih415-left-pinctrl", .data = &stih415_left_data },
  1064. { .compatible = "st,stih415-right-pinctrl",
  1065. .data = &stih415_right_data },
  1066. { .compatible = "st,stih415-front-pinctrl",
  1067. .data = &stih415_front_data },
  1068. { .compatible = "st,stih416-sbc-pinctrl", .data = &stih416_data},
  1069. { .compatible = "st,stih416-front-pinctrl", .data = &stih416_data},
  1070. { .compatible = "st,stih416-rear-pinctrl", .data = &stih416_data},
  1071. { .compatible = "st,stih416-fvdp-fe-pinctrl", .data = &stih416_data},
  1072. { .compatible = "st,stih416-fvdp-lite-pinctrl", .data = &stih416_data},
  1073. { /* sentinel */ }
  1074. };
  1075. static int st_pctl_probe_dt(struct platform_device *pdev,
  1076. struct pinctrl_desc *pctl_desc, struct st_pinctrl *info)
  1077. {
  1078. int ret = 0;
  1079. int i = 0, j = 0, k = 0, bank;
  1080. struct pinctrl_pin_desc *pdesc;
  1081. struct device_node *np = pdev->dev.of_node;
  1082. struct device_node *child;
  1083. int grp_index = 0;
  1084. st_pctl_dt_child_count(info, np);
  1085. if (!info->nbanks) {
  1086. dev_err(&pdev->dev, "you need atleast one gpio bank\n");
  1087. return -EINVAL;
  1088. }
  1089. dev_info(&pdev->dev, "nbanks = %d\n", info->nbanks);
  1090. dev_info(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  1091. dev_info(&pdev->dev, "ngroups = %d\n", info->ngroups);
  1092. info->functions = devm_kzalloc(&pdev->dev,
  1093. info->nfunctions * sizeof(*info->functions), GFP_KERNEL);
  1094. info->groups = devm_kzalloc(&pdev->dev,
  1095. info->ngroups * sizeof(*info->groups) , GFP_KERNEL);
  1096. info->banks = devm_kzalloc(&pdev->dev,
  1097. info->nbanks * sizeof(*info->banks), GFP_KERNEL);
  1098. if (!info->functions || !info->groups || !info->banks)
  1099. return -ENOMEM;
  1100. info->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  1101. if (IS_ERR(info->regmap)) {
  1102. dev_err(info->dev, "No syscfg phandle specified\n");
  1103. return PTR_ERR(info->regmap);
  1104. }
  1105. info->data = of_match_node(st_pctl_of_match, np)->data;
  1106. pctl_desc->npins = info->nbanks * ST_GPIO_PINS_PER_BANK;
  1107. pdesc = devm_kzalloc(&pdev->dev,
  1108. sizeof(*pdesc) * pctl_desc->npins, GFP_KERNEL);
  1109. if (!pdesc)
  1110. return -ENOMEM;
  1111. pctl_desc->pins = pdesc;
  1112. bank = 0;
  1113. for_each_child_of_node(np, child) {
  1114. if (of_property_read_bool(child, "gpio-controller")) {
  1115. const char *bank_name = NULL;
  1116. ret = st_gpiolib_register_bank(info, bank, child);
  1117. if (ret)
  1118. return ret;
  1119. k = info->banks[bank].range.pin_base;
  1120. bank_name = info->banks[bank].range.name;
  1121. for (j = 0; j < ST_GPIO_PINS_PER_BANK; j++, k++) {
  1122. pdesc->number = k;
  1123. pdesc->name = kasprintf(GFP_KERNEL, "%s[%d]",
  1124. bank_name, j);
  1125. pdesc++;
  1126. }
  1127. st_parse_syscfgs(info, bank, child);
  1128. bank++;
  1129. } else {
  1130. ret = st_pctl_parse_functions(child, info,
  1131. i++, &grp_index);
  1132. if (ret) {
  1133. dev_err(&pdev->dev, "No functions found.\n");
  1134. return ret;
  1135. }
  1136. }
  1137. }
  1138. return 0;
  1139. }
  1140. static int st_pctl_probe(struct platform_device *pdev)
  1141. {
  1142. struct st_pinctrl *info;
  1143. struct pinctrl_desc *pctl_desc;
  1144. int ret, i;
  1145. if (!pdev->dev.of_node) {
  1146. dev_err(&pdev->dev, "device node not found.\n");
  1147. return -EINVAL;
  1148. }
  1149. pctl_desc = devm_kzalloc(&pdev->dev, sizeof(*pctl_desc), GFP_KERNEL);
  1150. if (!pctl_desc)
  1151. return -ENOMEM;
  1152. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  1153. if (!info)
  1154. return -ENOMEM;
  1155. info->dev = &pdev->dev;
  1156. platform_set_drvdata(pdev, info);
  1157. ret = st_pctl_probe_dt(pdev, pctl_desc, info);
  1158. if (ret)
  1159. return ret;
  1160. pctl_desc->owner = THIS_MODULE,
  1161. pctl_desc->pctlops = &st_pctlops,
  1162. pctl_desc->pmxops = &st_pmxops,
  1163. pctl_desc->confops = &st_confops,
  1164. pctl_desc->name = dev_name(&pdev->dev);
  1165. info->pctl = pinctrl_register(pctl_desc, &pdev->dev, info);
  1166. if (!info->pctl) {
  1167. dev_err(&pdev->dev, "Failed pinctrl registration\n");
  1168. return -EINVAL;
  1169. }
  1170. for (i = 0; i < info->nbanks; i++)
  1171. pinctrl_add_gpio_range(info->pctl, &info->banks[i].range);
  1172. return 0;
  1173. }
  1174. static struct platform_driver st_pctl_driver = {
  1175. .driver = {
  1176. .name = "st-pinctrl",
  1177. .owner = THIS_MODULE,
  1178. .of_match_table = st_pctl_of_match,
  1179. },
  1180. .probe = st_pctl_probe,
  1181. };
  1182. static int __init st_pctl_init(void)
  1183. {
  1184. return platform_driver_register(&st_pctl_driver);
  1185. }
  1186. arch_initcall(st_pctl_init);