pinctrl-microchip-sgpio.c 26 KB

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