pinctrl-microchip-sgpio.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Microsemi/Microchip SoCs serial gpio driver
  4. *
  5. * Author: Lars Povlsen <[email protected]>
  6. *
  7. * Copyright (c) 2020 Microchip Technology Inc. and its subsidiaries.
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/bits.h>
  11. #include <linux/clk.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/io.h>
  14. #include <linux/mfd/ocelot.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/module.h>
  17. #include <linux/pinctrl/pinmux.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/property.h>
  20. #include <linux/regmap.h>
  21. #include <linux/reset.h>
  22. #include <linux/spinlock.h>
  23. #include "core.h"
  24. #include "pinconf.h"
  25. #define SGPIO_BITS_PER_WORD 32
  26. #define SGPIO_MAX_BITS 4
  27. #define SGPIO_SRC_BITS 3 /* 3 bit wide field per pin */
  28. enum {
  29. REG_INPUT_DATA,
  30. REG_PORT_CONFIG,
  31. REG_PORT_ENABLE,
  32. REG_SIO_CONFIG,
  33. REG_SIO_CLOCK,
  34. REG_INT_POLARITY,
  35. REG_INT_TRIGGER,
  36. REG_INT_ACK,
  37. REG_INT_ENABLE,
  38. REG_INT_IDENT,
  39. MAXREG
  40. };
  41. enum {
  42. SGPIO_ARCH_LUTON,
  43. SGPIO_ARCH_OCELOT,
  44. SGPIO_ARCH_SPARX5,
  45. };
  46. enum {
  47. SGPIO_FLAGS_HAS_IRQ = BIT(0),
  48. };
  49. struct sgpio_properties {
  50. int arch;
  51. int flags;
  52. u8 regoff[MAXREG];
  53. };
  54. #define SGPIO_LUTON_AUTO_REPEAT BIT(5)
  55. #define SGPIO_LUTON_PORT_WIDTH GENMASK(3, 2)
  56. #define SGPIO_LUTON_CLK_FREQ GENMASK(11, 0)
  57. #define SGPIO_LUTON_BIT_SOURCE GENMASK(11, 0)
  58. #define SGPIO_OCELOT_AUTO_REPEAT BIT(10)
  59. #define SGPIO_OCELOT_SINGLE_SHOT BIT(11)
  60. #define SGPIO_OCELOT_PORT_WIDTH GENMASK(8, 7)
  61. #define SGPIO_OCELOT_CLK_FREQ GENMASK(19, 8)
  62. #define SGPIO_OCELOT_BIT_SOURCE GENMASK(23, 12)
  63. #define SGPIO_SPARX5_AUTO_REPEAT BIT(6)
  64. #define SGPIO_SPARX5_SINGLE_SHOT BIT(7)
  65. #define SGPIO_SPARX5_PORT_WIDTH GENMASK(4, 3)
  66. #define SGPIO_SPARX5_CLK_FREQ GENMASK(19, 8)
  67. #define SGPIO_SPARX5_BIT_SOURCE GENMASK(23, 12)
  68. #define SGPIO_MASTER_INTR_ENA BIT(0)
  69. #define SGPIO_INT_TRG_LEVEL 0
  70. #define SGPIO_INT_TRG_EDGE 1
  71. #define SGPIO_INT_TRG_EDGE_FALL 2
  72. #define SGPIO_INT_TRG_EDGE_RISE 3
  73. #define SGPIO_TRG_LEVEL_HIGH 0
  74. #define SGPIO_TRG_LEVEL_LOW 1
  75. static const struct sgpio_properties properties_luton = {
  76. .arch = SGPIO_ARCH_LUTON,
  77. .regoff = { 0x00, 0x09, 0x29, 0x2a, 0x2b },
  78. };
  79. static const struct sgpio_properties properties_ocelot = {
  80. .arch = SGPIO_ARCH_OCELOT,
  81. .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05 },
  82. };
  83. static const struct sgpio_properties properties_sparx5 = {
  84. .arch = SGPIO_ARCH_SPARX5,
  85. .flags = SGPIO_FLAGS_HAS_IRQ,
  86. .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05, 0x2a, 0x32, 0x3a, 0x3e, 0x42 },
  87. };
  88. static const char * const functions[] = { "gpio" };
  89. struct sgpio_bank {
  90. struct sgpio_priv *priv;
  91. bool is_input;
  92. struct gpio_chip gpio;
  93. struct pinctrl_desc pctl_desc;
  94. };
  95. struct sgpio_priv {
  96. struct device *dev;
  97. struct sgpio_bank in;
  98. struct sgpio_bank out;
  99. u32 bitcount;
  100. u32 ports;
  101. u32 clock;
  102. struct regmap *regs;
  103. const struct sgpio_properties *properties;
  104. spinlock_t lock;
  105. /* protects the config register and single shot mode */
  106. struct mutex poll_lock;
  107. };
  108. struct sgpio_port_addr {
  109. u8 port;
  110. u8 bit;
  111. };
  112. static inline void sgpio_pin_to_addr(struct sgpio_priv *priv, int pin,
  113. struct sgpio_port_addr *addr)
  114. {
  115. addr->port = pin / priv->bitcount;
  116. addr->bit = pin % priv->bitcount;
  117. }
  118. static inline int sgpio_addr_to_pin(struct sgpio_priv *priv, int port, int bit)
  119. {
  120. return bit + port * priv->bitcount;
  121. }
  122. static inline u32 sgpio_get_addr(struct sgpio_priv *priv, u32 rno, u32 off)
  123. {
  124. return (priv->properties->regoff[rno] + off) *
  125. regmap_get_reg_stride(priv->regs);
  126. }
  127. static u32 sgpio_readl(struct sgpio_priv *priv, u32 rno, u32 off)
  128. {
  129. u32 addr = sgpio_get_addr(priv, rno, off);
  130. u32 val = 0;
  131. int ret;
  132. ret = regmap_read(priv->regs, addr, &val);
  133. WARN_ONCE(ret, "error reading sgpio reg %d\n", ret);
  134. return val;
  135. }
  136. static void sgpio_writel(struct sgpio_priv *priv,
  137. u32 val, u32 rno, u32 off)
  138. {
  139. u32 addr = sgpio_get_addr(priv, rno, off);
  140. int ret;
  141. ret = regmap_write(priv->regs, addr, val);
  142. WARN_ONCE(ret, "error writing sgpio reg %d\n", ret);
  143. }
  144. static inline void sgpio_clrsetbits(struct sgpio_priv *priv,
  145. u32 rno, u32 off, u32 clear, u32 set)
  146. {
  147. u32 addr = sgpio_get_addr(priv, rno, off);
  148. int ret;
  149. ret = regmap_update_bits(priv->regs, addr, clear | set, set);
  150. WARN_ONCE(ret, "error updating sgpio reg %d\n", ret);
  151. }
  152. static inline void sgpio_configure_bitstream(struct sgpio_priv *priv)
  153. {
  154. int width = priv->bitcount - 1;
  155. u32 clr, set;
  156. switch (priv->properties->arch) {
  157. case SGPIO_ARCH_LUTON:
  158. clr = SGPIO_LUTON_PORT_WIDTH;
  159. set = SGPIO_LUTON_AUTO_REPEAT |
  160. FIELD_PREP(SGPIO_LUTON_PORT_WIDTH, width);
  161. break;
  162. case SGPIO_ARCH_OCELOT:
  163. clr = SGPIO_OCELOT_PORT_WIDTH;
  164. set = SGPIO_OCELOT_AUTO_REPEAT |
  165. FIELD_PREP(SGPIO_OCELOT_PORT_WIDTH, width);
  166. break;
  167. case SGPIO_ARCH_SPARX5:
  168. clr = SGPIO_SPARX5_PORT_WIDTH;
  169. set = SGPIO_SPARX5_AUTO_REPEAT |
  170. FIELD_PREP(SGPIO_SPARX5_PORT_WIDTH, width);
  171. break;
  172. default:
  173. return;
  174. }
  175. sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, clr, set);
  176. }
  177. static inline void sgpio_configure_clock(struct sgpio_priv *priv, u32 clkfrq)
  178. {
  179. u32 clr, set;
  180. switch (priv->properties->arch) {
  181. case SGPIO_ARCH_LUTON:
  182. clr = SGPIO_LUTON_CLK_FREQ;
  183. set = FIELD_PREP(SGPIO_LUTON_CLK_FREQ, clkfrq);
  184. break;
  185. case SGPIO_ARCH_OCELOT:
  186. clr = SGPIO_OCELOT_CLK_FREQ;
  187. set = FIELD_PREP(SGPIO_OCELOT_CLK_FREQ, clkfrq);
  188. break;
  189. case SGPIO_ARCH_SPARX5:
  190. clr = SGPIO_SPARX5_CLK_FREQ;
  191. set = FIELD_PREP(SGPIO_SPARX5_CLK_FREQ, clkfrq);
  192. break;
  193. default:
  194. return;
  195. }
  196. sgpio_clrsetbits(priv, REG_SIO_CLOCK, 0, clr, set);
  197. }
  198. static int sgpio_single_shot(struct sgpio_priv *priv)
  199. {
  200. u32 addr = sgpio_get_addr(priv, REG_SIO_CONFIG, 0);
  201. int ret, ret2;
  202. u32 ctrl;
  203. unsigned int single_shot;
  204. unsigned int auto_repeat;
  205. switch (priv->properties->arch) {
  206. case SGPIO_ARCH_LUTON:
  207. /* not supported for now */
  208. return 0;
  209. case SGPIO_ARCH_OCELOT:
  210. single_shot = SGPIO_OCELOT_SINGLE_SHOT;
  211. auto_repeat = SGPIO_OCELOT_AUTO_REPEAT;
  212. break;
  213. case SGPIO_ARCH_SPARX5:
  214. single_shot = SGPIO_SPARX5_SINGLE_SHOT;
  215. auto_repeat = SGPIO_SPARX5_AUTO_REPEAT;
  216. break;
  217. default:
  218. return -EINVAL;
  219. }
  220. /*
  221. * Trigger immediate burst. This only works when auto repeat is turned
  222. * off. Otherwise, the single shot bit will never be cleared by the
  223. * hardware. Measurements showed that an update might take as long as
  224. * the burst gap. On a LAN9668 this is about 50ms for the largest
  225. * setting.
  226. * After the manual burst, reenable the auto repeat mode again.
  227. */
  228. mutex_lock(&priv->poll_lock);
  229. ret = regmap_update_bits(priv->regs, addr, single_shot | auto_repeat,
  230. single_shot);
  231. if (ret)
  232. goto out;
  233. ret = regmap_read_poll_timeout(priv->regs, addr, ctrl,
  234. !(ctrl & single_shot), 100, 60000);
  235. /* reenable auto repeat mode even if there was an error */
  236. ret2 = regmap_update_bits(priv->regs, addr, auto_repeat, auto_repeat);
  237. out:
  238. mutex_unlock(&priv->poll_lock);
  239. return ret ?: ret2;
  240. }
  241. static int sgpio_output_set(struct sgpio_priv *priv,
  242. struct sgpio_port_addr *addr,
  243. int value)
  244. {
  245. unsigned int bit = SGPIO_SRC_BITS * addr->bit;
  246. u32 reg = sgpio_get_addr(priv, REG_PORT_CONFIG, addr->port);
  247. bool changed;
  248. u32 clr, set;
  249. int ret;
  250. switch (priv->properties->arch) {
  251. case SGPIO_ARCH_LUTON:
  252. clr = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, BIT(bit));
  253. set = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, value << bit);
  254. break;
  255. case SGPIO_ARCH_OCELOT:
  256. clr = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, BIT(bit));
  257. set = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, value << bit);
  258. break;
  259. case SGPIO_ARCH_SPARX5:
  260. clr = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, BIT(bit));
  261. set = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, value << bit);
  262. break;
  263. default:
  264. return -EINVAL;
  265. }
  266. ret = regmap_update_bits_check(priv->regs, reg, clr | set, set,
  267. &changed);
  268. if (ret)
  269. return ret;
  270. if (changed) {
  271. ret = sgpio_single_shot(priv);
  272. if (ret)
  273. return ret;
  274. }
  275. return 0;
  276. }
  277. static int sgpio_output_get(struct sgpio_priv *priv,
  278. struct sgpio_port_addr *addr)
  279. {
  280. u32 val, portval = sgpio_readl(priv, REG_PORT_CONFIG, addr->port);
  281. unsigned int bit = SGPIO_SRC_BITS * addr->bit;
  282. switch (priv->properties->arch) {
  283. case SGPIO_ARCH_LUTON:
  284. val = FIELD_GET(SGPIO_LUTON_BIT_SOURCE, portval);
  285. break;
  286. case SGPIO_ARCH_OCELOT:
  287. val = FIELD_GET(SGPIO_OCELOT_BIT_SOURCE, portval);
  288. break;
  289. case SGPIO_ARCH_SPARX5:
  290. val = FIELD_GET(SGPIO_SPARX5_BIT_SOURCE, portval);
  291. break;
  292. default:
  293. val = 0;
  294. break;
  295. }
  296. return !!(val & BIT(bit));
  297. }
  298. static int sgpio_input_get(struct sgpio_priv *priv,
  299. struct sgpio_port_addr *addr)
  300. {
  301. return !!(sgpio_readl(priv, REG_INPUT_DATA, addr->bit) & BIT(addr->port));
  302. }
  303. static int sgpio_pinconf_get(struct pinctrl_dev *pctldev,
  304. unsigned int pin, unsigned long *config)
  305. {
  306. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  307. u32 param = pinconf_to_config_param(*config);
  308. struct sgpio_priv *priv = bank->priv;
  309. struct sgpio_port_addr addr;
  310. int val;
  311. sgpio_pin_to_addr(priv, pin, &addr);
  312. switch (param) {
  313. case PIN_CONFIG_INPUT_ENABLE:
  314. val = bank->is_input;
  315. break;
  316. case PIN_CONFIG_OUTPUT_ENABLE:
  317. val = !bank->is_input;
  318. break;
  319. case PIN_CONFIG_OUTPUT:
  320. if (bank->is_input)
  321. return -EINVAL;
  322. val = sgpio_output_get(priv, &addr);
  323. break;
  324. default:
  325. return -ENOTSUPP;
  326. }
  327. *config = pinconf_to_config_packed(param, val);
  328. return 0;
  329. }
  330. static int sgpio_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  331. unsigned long *configs, unsigned int num_configs)
  332. {
  333. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  334. struct sgpio_priv *priv = bank->priv;
  335. struct sgpio_port_addr addr;
  336. int cfg, err = 0;
  337. u32 param, arg;
  338. sgpio_pin_to_addr(priv, pin, &addr);
  339. for (cfg = 0; cfg < num_configs; cfg++) {
  340. param = pinconf_to_config_param(configs[cfg]);
  341. arg = pinconf_to_config_argument(configs[cfg]);
  342. switch (param) {
  343. case PIN_CONFIG_OUTPUT:
  344. if (bank->is_input)
  345. return -EINVAL;
  346. err = sgpio_output_set(priv, &addr, arg);
  347. break;
  348. default:
  349. err = -ENOTSUPP;
  350. }
  351. }
  352. return err;
  353. }
  354. static const struct pinconf_ops sgpio_confops = {
  355. .is_generic = true,
  356. .pin_config_get = sgpio_pinconf_get,
  357. .pin_config_set = sgpio_pinconf_set,
  358. .pin_config_config_dbg_show = pinconf_generic_dump_config,
  359. };
  360. static int sgpio_get_functions_count(struct pinctrl_dev *pctldev)
  361. {
  362. return 1;
  363. }
  364. static const char *sgpio_get_function_name(struct pinctrl_dev *pctldev,
  365. unsigned int function)
  366. {
  367. return functions[0];
  368. }
  369. static int sgpio_get_function_groups(struct pinctrl_dev *pctldev,
  370. unsigned int function,
  371. const char *const **groups,
  372. unsigned *const num_groups)
  373. {
  374. *groups = functions;
  375. *num_groups = ARRAY_SIZE(functions);
  376. return 0;
  377. }
  378. static int sgpio_pinmux_set_mux(struct pinctrl_dev *pctldev,
  379. unsigned int selector, unsigned int group)
  380. {
  381. return 0;
  382. }
  383. static int sgpio_gpio_set_direction(struct pinctrl_dev *pctldev,
  384. struct pinctrl_gpio_range *range,
  385. unsigned int pin, bool input)
  386. {
  387. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  388. return (input == bank->is_input) ? 0 : -EINVAL;
  389. }
  390. static int sgpio_gpio_request_enable(struct pinctrl_dev *pctldev,
  391. struct pinctrl_gpio_range *range,
  392. unsigned int offset)
  393. {
  394. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  395. struct sgpio_priv *priv = bank->priv;
  396. struct sgpio_port_addr addr;
  397. sgpio_pin_to_addr(priv, offset, &addr);
  398. if ((priv->ports & BIT(addr.port)) == 0) {
  399. dev_warn(priv->dev, "Request port %d.%d: Port is not enabled\n",
  400. addr.port, addr.bit);
  401. return -EINVAL;
  402. }
  403. return 0;
  404. }
  405. static const struct pinmux_ops sgpio_pmx_ops = {
  406. .get_functions_count = sgpio_get_functions_count,
  407. .get_function_name = sgpio_get_function_name,
  408. .get_function_groups = sgpio_get_function_groups,
  409. .set_mux = sgpio_pinmux_set_mux,
  410. .gpio_set_direction = sgpio_gpio_set_direction,
  411. .gpio_request_enable = sgpio_gpio_request_enable,
  412. };
  413. static int sgpio_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  414. {
  415. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  416. return bank->pctl_desc.npins;
  417. }
  418. static const char *sgpio_pctl_get_group_name(struct pinctrl_dev *pctldev,
  419. unsigned int group)
  420. {
  421. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  422. return bank->pctl_desc.pins[group].name;
  423. }
  424. static int sgpio_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  425. unsigned int group,
  426. const unsigned int **pins,
  427. unsigned int *num_pins)
  428. {
  429. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  430. *pins = &bank->pctl_desc.pins[group].number;
  431. *num_pins = 1;
  432. return 0;
  433. }
  434. static const struct pinctrl_ops sgpio_pctl_ops = {
  435. .get_groups_count = sgpio_pctl_get_groups_count,
  436. .get_group_name = sgpio_pctl_get_group_name,
  437. .get_group_pins = sgpio_pctl_get_group_pins,
  438. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  439. .dt_free_map = pinconf_generic_dt_free_map,
  440. };
  441. static int microchip_sgpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
  442. {
  443. struct sgpio_bank *bank = gpiochip_get_data(gc);
  444. /* Fixed-position function */
  445. return bank->is_input ? 0 : -EINVAL;
  446. }
  447. static int microchip_sgpio_direction_output(struct gpio_chip *gc,
  448. unsigned int gpio, int value)
  449. {
  450. struct sgpio_bank *bank = gpiochip_get_data(gc);
  451. struct sgpio_priv *priv = bank->priv;
  452. struct sgpio_port_addr addr;
  453. /* Fixed-position function */
  454. if (bank->is_input)
  455. return -EINVAL;
  456. sgpio_pin_to_addr(priv, gpio, &addr);
  457. return sgpio_output_set(priv, &addr, value);
  458. }
  459. static int microchip_sgpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
  460. {
  461. struct sgpio_bank *bank = gpiochip_get_data(gc);
  462. return bank->is_input ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
  463. }
  464. static void microchip_sgpio_set_value(struct gpio_chip *gc,
  465. unsigned int gpio, int value)
  466. {
  467. microchip_sgpio_direction_output(gc, gpio, value);
  468. }
  469. static int microchip_sgpio_get_value(struct gpio_chip *gc, unsigned int gpio)
  470. {
  471. struct sgpio_bank *bank = gpiochip_get_data(gc);
  472. struct sgpio_priv *priv = bank->priv;
  473. struct sgpio_port_addr addr;
  474. sgpio_pin_to_addr(priv, gpio, &addr);
  475. return bank->is_input ? sgpio_input_get(priv, &addr) : sgpio_output_get(priv, &addr);
  476. }
  477. static int microchip_sgpio_of_xlate(struct gpio_chip *gc,
  478. const struct of_phandle_args *gpiospec,
  479. u32 *flags)
  480. {
  481. struct sgpio_bank *bank = gpiochip_get_data(gc);
  482. struct sgpio_priv *priv = bank->priv;
  483. int pin;
  484. /*
  485. * Note that the SGIO pin is defined by *2* numbers, a port
  486. * number between 0 and 31, and a bit index, 0 to 3.
  487. */
  488. if (gpiospec->args[0] > SGPIO_BITS_PER_WORD ||
  489. gpiospec->args[1] > priv->bitcount)
  490. return -EINVAL;
  491. pin = sgpio_addr_to_pin(priv, gpiospec->args[0], gpiospec->args[1]);
  492. if (pin > gc->ngpio)
  493. return -EINVAL;
  494. if (flags)
  495. *flags = gpiospec->args[2];
  496. return pin;
  497. }
  498. static int microchip_sgpio_get_ports(struct sgpio_priv *priv)
  499. {
  500. const char *range_property_name = "microchip,sgpio-port-ranges";
  501. struct device *dev = priv->dev;
  502. u32 range_params[64];
  503. int i, nranges, ret;
  504. /* Calculate port mask */
  505. nranges = device_property_count_u32(dev, range_property_name);
  506. if (nranges < 2 || nranges % 2 || nranges > ARRAY_SIZE(range_params)) {
  507. dev_err(dev, "%s port range: '%s' property\n",
  508. nranges == -EINVAL ? "Missing" : "Invalid",
  509. range_property_name);
  510. return -EINVAL;
  511. }
  512. ret = device_property_read_u32_array(dev, range_property_name,
  513. range_params, nranges);
  514. if (ret) {
  515. dev_err(dev, "failed to parse '%s' property: %d\n",
  516. range_property_name, ret);
  517. return ret;
  518. }
  519. for (i = 0; i < nranges; i += 2) {
  520. int start, end;
  521. start = range_params[i];
  522. end = range_params[i + 1];
  523. if (start > end || end >= SGPIO_BITS_PER_WORD) {
  524. dev_err(dev, "Ill-formed port-range [%d:%d]\n",
  525. start, end);
  526. }
  527. priv->ports |= GENMASK(end, start);
  528. }
  529. return 0;
  530. }
  531. static void microchip_sgpio_irq_settype(struct irq_data *data,
  532. int type,
  533. int polarity)
  534. {
  535. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  536. struct sgpio_bank *bank = gpiochip_get_data(chip);
  537. unsigned int gpio = irqd_to_hwirq(data);
  538. struct sgpio_port_addr addr;
  539. unsigned long flags;
  540. u32 ena;
  541. sgpio_pin_to_addr(bank->priv, gpio, &addr);
  542. spin_lock_irqsave(&bank->priv->lock, flags);
  543. /* Disable interrupt while changing type */
  544. ena = sgpio_readl(bank->priv, REG_INT_ENABLE, addr.bit);
  545. sgpio_writel(bank->priv, ena & ~BIT(addr.port), REG_INT_ENABLE, addr.bit);
  546. /* Type value spread over 2 registers sets: low, high bit */
  547. sgpio_clrsetbits(bank->priv, REG_INT_TRIGGER, addr.bit,
  548. BIT(addr.port), (!!(type & 0x1)) << addr.port);
  549. sgpio_clrsetbits(bank->priv, REG_INT_TRIGGER, SGPIO_MAX_BITS + addr.bit,
  550. BIT(addr.port), (!!(type & 0x2)) << addr.port);
  551. if (type == SGPIO_INT_TRG_LEVEL)
  552. sgpio_clrsetbits(bank->priv, REG_INT_POLARITY, addr.bit,
  553. BIT(addr.port), polarity << addr.port);
  554. /* Possibly re-enable interrupts */
  555. sgpio_writel(bank->priv, ena, REG_INT_ENABLE, addr.bit);
  556. spin_unlock_irqrestore(&bank->priv->lock, flags);
  557. }
  558. static void microchip_sgpio_irq_setreg(struct irq_data *data,
  559. int reg,
  560. bool clear)
  561. {
  562. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  563. struct sgpio_bank *bank = gpiochip_get_data(chip);
  564. unsigned int gpio = irqd_to_hwirq(data);
  565. struct sgpio_port_addr addr;
  566. sgpio_pin_to_addr(bank->priv, gpio, &addr);
  567. if (clear)
  568. sgpio_clrsetbits(bank->priv, reg, addr.bit, BIT(addr.port), 0);
  569. else
  570. sgpio_clrsetbits(bank->priv, reg, addr.bit, 0, BIT(addr.port));
  571. }
  572. static void microchip_sgpio_irq_mask(struct irq_data *data)
  573. {
  574. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  575. microchip_sgpio_irq_setreg(data, REG_INT_ENABLE, true);
  576. gpiochip_disable_irq(chip, data->hwirq);
  577. }
  578. static void microchip_sgpio_irq_unmask(struct irq_data *data)
  579. {
  580. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  581. gpiochip_enable_irq(chip, data->hwirq);
  582. microchip_sgpio_irq_setreg(data, REG_INT_ENABLE, false);
  583. }
  584. static void microchip_sgpio_irq_ack(struct irq_data *data)
  585. {
  586. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  587. struct sgpio_bank *bank = gpiochip_get_data(chip);
  588. unsigned int gpio = irqd_to_hwirq(data);
  589. struct sgpio_port_addr addr;
  590. sgpio_pin_to_addr(bank->priv, gpio, &addr);
  591. sgpio_writel(bank->priv, BIT(addr.port), REG_INT_ACK, addr.bit);
  592. }
  593. static int microchip_sgpio_irq_set_type(struct irq_data *data, unsigned int type)
  594. {
  595. type &= IRQ_TYPE_SENSE_MASK;
  596. switch (type) {
  597. case IRQ_TYPE_EDGE_BOTH:
  598. irq_set_handler_locked(data, handle_edge_irq);
  599. microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE, 0);
  600. break;
  601. case IRQ_TYPE_EDGE_RISING:
  602. irq_set_handler_locked(data, handle_edge_irq);
  603. microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE_RISE, 0);
  604. break;
  605. case IRQ_TYPE_EDGE_FALLING:
  606. irq_set_handler_locked(data, handle_edge_irq);
  607. microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE_FALL, 0);
  608. break;
  609. case IRQ_TYPE_LEVEL_HIGH:
  610. irq_set_handler_locked(data, handle_level_irq);
  611. microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_LEVEL, SGPIO_TRG_LEVEL_HIGH);
  612. break;
  613. case IRQ_TYPE_LEVEL_LOW:
  614. irq_set_handler_locked(data, handle_level_irq);
  615. microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_LEVEL, SGPIO_TRG_LEVEL_LOW);
  616. break;
  617. default:
  618. return -EINVAL;
  619. }
  620. return 0;
  621. }
  622. static const struct irq_chip microchip_sgpio_irqchip = {
  623. .name = "gpio",
  624. .irq_mask = microchip_sgpio_irq_mask,
  625. .irq_ack = microchip_sgpio_irq_ack,
  626. .irq_unmask = microchip_sgpio_irq_unmask,
  627. .irq_set_type = microchip_sgpio_irq_set_type,
  628. .flags = IRQCHIP_IMMUTABLE,
  629. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  630. };
  631. static void sgpio_irq_handler(struct irq_desc *desc)
  632. {
  633. struct irq_chip *parent_chip = irq_desc_get_chip(desc);
  634. struct gpio_chip *chip = irq_desc_get_handler_data(desc);
  635. struct sgpio_bank *bank = gpiochip_get_data(chip);
  636. struct sgpio_priv *priv = bank->priv;
  637. int bit, port, gpio;
  638. long val;
  639. for (bit = 0; bit < priv->bitcount; bit++) {
  640. val = sgpio_readl(priv, REG_INT_IDENT, bit);
  641. if (!val)
  642. continue;
  643. chained_irq_enter(parent_chip, desc);
  644. for_each_set_bit(port, &val, SGPIO_BITS_PER_WORD) {
  645. gpio = sgpio_addr_to_pin(priv, port, bit);
  646. generic_handle_domain_irq(chip->irq.domain, gpio);
  647. }
  648. chained_irq_exit(parent_chip, desc);
  649. }
  650. }
  651. static int microchip_sgpio_register_bank(struct device *dev,
  652. struct sgpio_priv *priv,
  653. struct fwnode_handle *fwnode,
  654. int bankno)
  655. {
  656. struct pinctrl_pin_desc *pins;
  657. struct pinctrl_desc *pctl_desc;
  658. struct pinctrl_dev *pctldev;
  659. struct sgpio_bank *bank;
  660. struct gpio_chip *gc;
  661. u32 ngpios;
  662. int i, ret;
  663. /* Get overall bank struct */
  664. bank = (bankno == 0) ? &priv->in : &priv->out;
  665. bank->priv = priv;
  666. if (fwnode_property_read_u32(fwnode, "ngpios", &ngpios)) {
  667. dev_info(dev, "failed to get number of gpios for bank%d\n",
  668. bankno);
  669. ngpios = 64;
  670. }
  671. priv->bitcount = ngpios / SGPIO_BITS_PER_WORD;
  672. if (priv->bitcount > SGPIO_MAX_BITS) {
  673. dev_err(dev, "Bit width exceeds maximum (%d)\n",
  674. SGPIO_MAX_BITS);
  675. return -EINVAL;
  676. }
  677. pctl_desc = &bank->pctl_desc;
  678. pctl_desc->name = devm_kasprintf(dev, GFP_KERNEL, "%s-%sput",
  679. dev_name(dev),
  680. bank->is_input ? "in" : "out");
  681. if (!pctl_desc->name)
  682. return -ENOMEM;
  683. pctl_desc->pctlops = &sgpio_pctl_ops;
  684. pctl_desc->pmxops = &sgpio_pmx_ops;
  685. pctl_desc->confops = &sgpio_confops;
  686. pctl_desc->owner = THIS_MODULE;
  687. pins = devm_kzalloc(dev, sizeof(*pins)*ngpios, GFP_KERNEL);
  688. if (!pins)
  689. return -ENOMEM;
  690. pctl_desc->npins = ngpios;
  691. pctl_desc->pins = pins;
  692. for (i = 0; i < ngpios; i++) {
  693. struct sgpio_port_addr addr;
  694. sgpio_pin_to_addr(priv, i, &addr);
  695. pins[i].number = i;
  696. pins[i].name = devm_kasprintf(dev, GFP_KERNEL,
  697. "SGPIO_%c_p%db%d",
  698. bank->is_input ? 'I' : 'O',
  699. addr.port, addr.bit);
  700. if (!pins[i].name)
  701. return -ENOMEM;
  702. }
  703. pctldev = devm_pinctrl_register(dev, pctl_desc, bank);
  704. if (IS_ERR(pctldev))
  705. return dev_err_probe(dev, PTR_ERR(pctldev), "Failed to register pinctrl\n");
  706. gc = &bank->gpio;
  707. gc->label = pctl_desc->name;
  708. gc->parent = dev;
  709. gc->fwnode = fwnode;
  710. gc->owner = THIS_MODULE;
  711. gc->get_direction = microchip_sgpio_get_direction;
  712. gc->direction_input = microchip_sgpio_direction_input;
  713. gc->direction_output = microchip_sgpio_direction_output;
  714. gc->get = microchip_sgpio_get_value;
  715. gc->set = microchip_sgpio_set_value;
  716. gc->request = gpiochip_generic_request;
  717. gc->free = gpiochip_generic_free;
  718. gc->of_xlate = microchip_sgpio_of_xlate;
  719. gc->of_gpio_n_cells = 3;
  720. gc->base = -1;
  721. gc->ngpio = ngpios;
  722. gc->can_sleep = !bank->is_input;
  723. if (bank->is_input && priv->properties->flags & SGPIO_FLAGS_HAS_IRQ) {
  724. int irq;
  725. irq = fwnode_irq_get(fwnode, 0);
  726. if (irq > 0) {
  727. struct gpio_irq_chip *girq = &gc->irq;
  728. gpio_irq_chip_set_chip(girq, &microchip_sgpio_irqchip);
  729. girq->parent_handler = sgpio_irq_handler;
  730. girq->num_parents = 1;
  731. girq->parents = devm_kcalloc(dev, 1,
  732. sizeof(*girq->parents),
  733. GFP_KERNEL);
  734. if (!girq->parents)
  735. return -ENOMEM;
  736. girq->parents[0] = irq;
  737. girq->default_type = IRQ_TYPE_NONE;
  738. girq->handler = handle_bad_irq;
  739. /* Disable all individual pins */
  740. for (i = 0; i < SGPIO_MAX_BITS; i++)
  741. sgpio_writel(priv, 0, REG_INT_ENABLE, i);
  742. /* Master enable */
  743. sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, 0, SGPIO_MASTER_INTR_ENA);
  744. }
  745. }
  746. ret = devm_gpiochip_add_data(dev, gc, bank);
  747. if (ret)
  748. dev_err(dev, "Failed to register: ret %d\n", ret);
  749. return ret;
  750. }
  751. static int microchip_sgpio_probe(struct platform_device *pdev)
  752. {
  753. int div_clock = 0, ret, port, i, nbanks;
  754. struct device *dev = &pdev->dev;
  755. struct fwnode_handle *fwnode;
  756. struct reset_control *reset;
  757. struct sgpio_priv *priv;
  758. struct clk *clk;
  759. u32 val;
  760. struct regmap_config regmap_config = {
  761. .reg_bits = 32,
  762. .val_bits = 32,
  763. .reg_stride = 4,
  764. };
  765. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  766. if (!priv)
  767. return -ENOMEM;
  768. priv->dev = dev;
  769. spin_lock_init(&priv->lock);
  770. mutex_init(&priv->poll_lock);
  771. reset = devm_reset_control_get_optional_shared(&pdev->dev, "switch");
  772. if (IS_ERR(reset))
  773. return dev_err_probe(dev, PTR_ERR(reset), "Failed to get reset\n");
  774. reset_control_reset(reset);
  775. clk = devm_clk_get(dev, NULL);
  776. if (IS_ERR(clk))
  777. return dev_err_probe(dev, PTR_ERR(clk), "Failed to get clock\n");
  778. div_clock = clk_get_rate(clk);
  779. if (device_property_read_u32(dev, "bus-frequency", &priv->clock))
  780. priv->clock = 12500000;
  781. if (priv->clock == 0 || priv->clock > (div_clock / 2)) {
  782. dev_err(dev, "Invalid frequency %d\n", priv->clock);
  783. return -EINVAL;
  784. }
  785. priv->regs = ocelot_regmap_from_resource(pdev, 0, &regmap_config);
  786. if (IS_ERR(priv->regs))
  787. return PTR_ERR(priv->regs);
  788. priv->properties = device_get_match_data(dev);
  789. priv->in.is_input = true;
  790. /* Get rest of device properties */
  791. ret = microchip_sgpio_get_ports(priv);
  792. if (ret)
  793. return ret;
  794. nbanks = device_get_child_node_count(dev);
  795. if (nbanks != 2) {
  796. dev_err(dev, "Must have 2 banks (have %d)\n", nbanks);
  797. return -EINVAL;
  798. }
  799. i = 0;
  800. device_for_each_child_node(dev, fwnode) {
  801. ret = microchip_sgpio_register_bank(dev, priv, fwnode, i++);
  802. if (ret) {
  803. fwnode_handle_put(fwnode);
  804. return ret;
  805. }
  806. }
  807. if (priv->in.gpio.ngpio != priv->out.gpio.ngpio) {
  808. dev_err(dev, "Banks must have same GPIO count\n");
  809. return -ERANGE;
  810. }
  811. sgpio_configure_bitstream(priv);
  812. val = max(2U, div_clock / priv->clock);
  813. sgpio_configure_clock(priv, val);
  814. for (port = 0; port < SGPIO_BITS_PER_WORD; port++)
  815. sgpio_writel(priv, 0, REG_PORT_CONFIG, port);
  816. sgpio_writel(priv, priv->ports, REG_PORT_ENABLE, 0);
  817. return 0;
  818. }
  819. static const struct of_device_id microchip_sgpio_gpio_of_match[] = {
  820. {
  821. .compatible = "microchip,sparx5-sgpio",
  822. .data = &properties_sparx5,
  823. }, {
  824. .compatible = "mscc,luton-sgpio",
  825. .data = &properties_luton,
  826. }, {
  827. .compatible = "mscc,ocelot-sgpio",
  828. .data = &properties_ocelot,
  829. }, {
  830. /* sentinel */
  831. }
  832. };
  833. MODULE_DEVICE_TABLE(of, microchip_sgpio_gpio_of_match);
  834. static struct platform_driver microchip_sgpio_pinctrl_driver = {
  835. .driver = {
  836. .name = "pinctrl-microchip-sgpio",
  837. .of_match_table = microchip_sgpio_gpio_of_match,
  838. .suppress_bind_attrs = true,
  839. },
  840. .probe = microchip_sgpio_probe,
  841. };
  842. module_platform_driver(microchip_sgpio_pinctrl_driver);
  843. MODULE_DESCRIPTION("Microchip SGPIO Pinctrl Driver");
  844. MODULE_LICENSE("GPL");