pinctrl-stm32.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /*
  2. * Copyright (C) Maxime Coquelin 2015
  3. * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
  4. * License terms: GNU General Public License (GPL), version 2
  5. *
  6. * Heavily based on Mediatek's pinctrl driver
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/gpio/driver.h>
  10. #include <linux/io.h>
  11. #include <linux/irq.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/pinctrl/machine.h>
  20. #include <linux/pinctrl/pinconf.h>
  21. #include <linux/pinctrl/pinconf-generic.h>
  22. #include <linux/pinctrl/pinctrl.h>
  23. #include <linux/pinctrl/pinmux.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/regmap.h>
  26. #include <linux/reset.h>
  27. #include <linux/slab.h>
  28. #include "../core.h"
  29. #include "../pinconf.h"
  30. #include "../pinctrl-utils.h"
  31. #include "pinctrl-stm32.h"
  32. #define STM32_GPIO_MODER 0x00
  33. #define STM32_GPIO_TYPER 0x04
  34. #define STM32_GPIO_SPEEDR 0x08
  35. #define STM32_GPIO_PUPDR 0x0c
  36. #define STM32_GPIO_IDR 0x10
  37. #define STM32_GPIO_ODR 0x14
  38. #define STM32_GPIO_BSRR 0x18
  39. #define STM32_GPIO_LCKR 0x1c
  40. #define STM32_GPIO_AFRL 0x20
  41. #define STM32_GPIO_AFRH 0x24
  42. #define STM32_GPIO_PINS_PER_BANK 16
  43. #define STM32_GPIO_IRQ_LINE 16
  44. #define gpio_range_to_bank(chip) \
  45. container_of(chip, struct stm32_gpio_bank, range)
  46. static const char * const stm32_gpio_functions[] = {
  47. "gpio", "af0", "af1",
  48. "af2", "af3", "af4",
  49. "af5", "af6", "af7",
  50. "af8", "af9", "af10",
  51. "af11", "af12", "af13",
  52. "af14", "af15", "analog",
  53. };
  54. struct stm32_pinctrl_group {
  55. const char *name;
  56. unsigned long config;
  57. unsigned pin;
  58. };
  59. struct stm32_gpio_bank {
  60. void __iomem *base;
  61. struct clk *clk;
  62. spinlock_t lock;
  63. struct gpio_chip gpio_chip;
  64. struct pinctrl_gpio_range range;
  65. struct fwnode_handle *fwnode;
  66. struct irq_domain *domain;
  67. };
  68. struct stm32_pinctrl {
  69. struct device *dev;
  70. struct pinctrl_dev *pctl_dev;
  71. struct pinctrl_desc pctl_desc;
  72. struct stm32_pinctrl_group *groups;
  73. unsigned ngroups;
  74. const char **grp_names;
  75. struct stm32_gpio_bank *banks;
  76. unsigned nbanks;
  77. const struct stm32_pinctrl_match_data *match_data;
  78. struct irq_domain *domain;
  79. struct regmap *regmap;
  80. struct regmap_field *irqmux[STM32_GPIO_PINS_PER_BANK];
  81. };
  82. static inline int stm32_gpio_pin(int gpio)
  83. {
  84. return gpio % STM32_GPIO_PINS_PER_BANK;
  85. }
  86. static inline u32 stm32_gpio_get_mode(u32 function)
  87. {
  88. switch (function) {
  89. case STM32_PIN_GPIO:
  90. return 0;
  91. case STM32_PIN_AF(0) ... STM32_PIN_AF(15):
  92. return 2;
  93. case STM32_PIN_ANALOG:
  94. return 3;
  95. }
  96. return 0;
  97. }
  98. static inline u32 stm32_gpio_get_alt(u32 function)
  99. {
  100. switch (function) {
  101. case STM32_PIN_GPIO:
  102. return 0;
  103. case STM32_PIN_AF(0) ... STM32_PIN_AF(15):
  104. return function - 1;
  105. case STM32_PIN_ANALOG:
  106. return 0;
  107. }
  108. return 0;
  109. }
  110. /* GPIO functions */
  111. static inline void __stm32_gpio_set(struct stm32_gpio_bank *bank,
  112. unsigned offset, int value)
  113. {
  114. if (!value)
  115. offset += STM32_GPIO_PINS_PER_BANK;
  116. clk_enable(bank->clk);
  117. writel_relaxed(BIT(offset), bank->base + STM32_GPIO_BSRR);
  118. clk_disable(bank->clk);
  119. }
  120. static int stm32_gpio_request(struct gpio_chip *chip, unsigned offset)
  121. {
  122. return pinctrl_request_gpio(chip->base + offset);
  123. }
  124. static void stm32_gpio_free(struct gpio_chip *chip, unsigned offset)
  125. {
  126. pinctrl_free_gpio(chip->base + offset);
  127. }
  128. static int stm32_gpio_get(struct gpio_chip *chip, unsigned offset)
  129. {
  130. struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
  131. int ret;
  132. clk_enable(bank->clk);
  133. ret = !!(readl_relaxed(bank->base + STM32_GPIO_IDR) & BIT(offset));
  134. clk_disable(bank->clk);
  135. return ret;
  136. }
  137. static void stm32_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  138. {
  139. struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
  140. __stm32_gpio_set(bank, offset, value);
  141. }
  142. static int stm32_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  143. {
  144. return pinctrl_gpio_direction_input(chip->base + offset);
  145. }
  146. static int stm32_gpio_direction_output(struct gpio_chip *chip,
  147. unsigned offset, int value)
  148. {
  149. struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
  150. __stm32_gpio_set(bank, offset, value);
  151. pinctrl_gpio_direction_output(chip->base + offset);
  152. return 0;
  153. }
  154. static int stm32_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
  155. {
  156. struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
  157. struct irq_fwspec fwspec;
  158. fwspec.fwnode = bank->fwnode;
  159. fwspec.param_count = 2;
  160. fwspec.param[0] = offset;
  161. fwspec.param[1] = IRQ_TYPE_NONE;
  162. return irq_create_fwspec_mapping(&fwspec);
  163. }
  164. static const struct gpio_chip stm32_gpio_template = {
  165. .request = stm32_gpio_request,
  166. .free = stm32_gpio_free,
  167. .get = stm32_gpio_get,
  168. .set = stm32_gpio_set,
  169. .direction_input = stm32_gpio_direction_input,
  170. .direction_output = stm32_gpio_direction_output,
  171. .to_irq = stm32_gpio_to_irq,
  172. };
  173. static struct irq_chip stm32_gpio_irq_chip = {
  174. .name = "stm32gpio",
  175. .irq_eoi = irq_chip_eoi_parent,
  176. .irq_mask = irq_chip_mask_parent,
  177. .irq_unmask = irq_chip_unmask_parent,
  178. .irq_set_type = irq_chip_set_type_parent,
  179. };
  180. static int stm32_gpio_domain_translate(struct irq_domain *d,
  181. struct irq_fwspec *fwspec,
  182. unsigned long *hwirq,
  183. unsigned int *type)
  184. {
  185. if ((fwspec->param_count != 2) ||
  186. (fwspec->param[0] >= STM32_GPIO_IRQ_LINE))
  187. return -EINVAL;
  188. *hwirq = fwspec->param[0];
  189. *type = fwspec->param[1];
  190. return 0;
  191. }
  192. static void stm32_gpio_domain_activate(struct irq_domain *d,
  193. struct irq_data *irq_data)
  194. {
  195. struct stm32_gpio_bank *bank = d->host_data;
  196. struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
  197. regmap_field_write(pctl->irqmux[irq_data->hwirq], bank->range.id);
  198. }
  199. static int stm32_gpio_domain_alloc(struct irq_domain *d,
  200. unsigned int virq,
  201. unsigned int nr_irqs, void *data)
  202. {
  203. struct stm32_gpio_bank *bank = d->host_data;
  204. struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
  205. struct irq_fwspec *fwspec = data;
  206. struct irq_fwspec parent_fwspec;
  207. irq_hw_number_t hwirq;
  208. int ret;
  209. hwirq = fwspec->param[0];
  210. parent_fwspec.fwnode = d->parent->fwnode;
  211. parent_fwspec.param_count = 2;
  212. parent_fwspec.param[0] = fwspec->param[0];
  213. parent_fwspec.param[1] = fwspec->param[1];
  214. irq_domain_set_hwirq_and_chip(d, virq, hwirq, &stm32_gpio_irq_chip,
  215. bank);
  216. ret = gpiochip_lock_as_irq(&bank->gpio_chip, hwirq);
  217. if (ret) {
  218. dev_err(pctl->dev, "Unable to configure STM32 %s%ld as IRQ\n",
  219. bank->gpio_chip.label, hwirq);
  220. return ret;
  221. }
  222. ret = irq_domain_alloc_irqs_parent(d, virq, nr_irqs, &parent_fwspec);
  223. if (ret)
  224. gpiochip_unlock_as_irq(&bank->gpio_chip, hwirq);
  225. return ret;
  226. }
  227. static void stm32_gpio_domain_free(struct irq_domain *d, unsigned int virq,
  228. unsigned int nr_irqs)
  229. {
  230. struct stm32_gpio_bank *bank = d->host_data;
  231. struct irq_data *data = irq_get_irq_data(virq);
  232. irq_domain_free_irqs_common(d, virq, nr_irqs);
  233. gpiochip_unlock_as_irq(&bank->gpio_chip, data->hwirq);
  234. }
  235. static const struct irq_domain_ops stm32_gpio_domain_ops = {
  236. .translate = stm32_gpio_domain_translate,
  237. .alloc = stm32_gpio_domain_alloc,
  238. .free = stm32_gpio_domain_free,
  239. .activate = stm32_gpio_domain_activate,
  240. };
  241. /* Pinctrl functions */
  242. static struct stm32_pinctrl_group *
  243. stm32_pctrl_find_group_by_pin(struct stm32_pinctrl *pctl, u32 pin)
  244. {
  245. int i;
  246. for (i = 0; i < pctl->ngroups; i++) {
  247. struct stm32_pinctrl_group *grp = pctl->groups + i;
  248. if (grp->pin == pin)
  249. return grp;
  250. }
  251. return NULL;
  252. }
  253. static bool stm32_pctrl_is_function_valid(struct stm32_pinctrl *pctl,
  254. u32 pin_num, u32 fnum)
  255. {
  256. int i;
  257. for (i = 0; i < pctl->match_data->npins; i++) {
  258. const struct stm32_desc_pin *pin = pctl->match_data->pins + i;
  259. const struct stm32_desc_function *func = pin->functions;
  260. if (pin->pin.number != pin_num)
  261. continue;
  262. while (func && func->name) {
  263. if (func->num == fnum)
  264. return true;
  265. func++;
  266. }
  267. break;
  268. }
  269. return false;
  270. }
  271. static int stm32_pctrl_dt_node_to_map_func(struct stm32_pinctrl *pctl,
  272. u32 pin, u32 fnum, struct stm32_pinctrl_group *grp,
  273. struct pinctrl_map **map, unsigned *reserved_maps,
  274. unsigned *num_maps)
  275. {
  276. if (*num_maps == *reserved_maps)
  277. return -ENOSPC;
  278. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  279. (*map)[*num_maps].data.mux.group = grp->name;
  280. if (!stm32_pctrl_is_function_valid(pctl, pin, fnum)) {
  281. dev_err(pctl->dev, "invalid function %d on pin %d .\n",
  282. fnum, pin);
  283. return -EINVAL;
  284. }
  285. (*map)[*num_maps].data.mux.function = stm32_gpio_functions[fnum];
  286. (*num_maps)++;
  287. return 0;
  288. }
  289. static int stm32_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  290. struct device_node *node,
  291. struct pinctrl_map **map,
  292. unsigned *reserved_maps,
  293. unsigned *num_maps)
  294. {
  295. struct stm32_pinctrl *pctl;
  296. struct stm32_pinctrl_group *grp;
  297. struct property *pins;
  298. u32 pinfunc, pin, func;
  299. unsigned long *configs;
  300. unsigned int num_configs;
  301. bool has_config = 0;
  302. unsigned reserve = 0;
  303. int num_pins, num_funcs, maps_per_pin, i, err;
  304. pctl = pinctrl_dev_get_drvdata(pctldev);
  305. pins = of_find_property(node, "pinmux", NULL);
  306. if (!pins) {
  307. dev_err(pctl->dev, "missing pins property in node %s .\n",
  308. node->name);
  309. return -EINVAL;
  310. }
  311. err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
  312. &num_configs);
  313. if (err)
  314. return err;
  315. if (num_configs)
  316. has_config = 1;
  317. num_pins = pins->length / sizeof(u32);
  318. num_funcs = num_pins;
  319. maps_per_pin = 0;
  320. if (num_funcs)
  321. maps_per_pin++;
  322. if (has_config && num_pins >= 1)
  323. maps_per_pin++;
  324. if (!num_pins || !maps_per_pin)
  325. return -EINVAL;
  326. reserve = num_pins * maps_per_pin;
  327. err = pinctrl_utils_reserve_map(pctldev, map,
  328. reserved_maps, num_maps, reserve);
  329. if (err)
  330. return err;
  331. for (i = 0; i < num_pins; i++) {
  332. err = of_property_read_u32_index(node, "pinmux",
  333. i, &pinfunc);
  334. if (err)
  335. return err;
  336. pin = STM32_GET_PIN_NO(pinfunc);
  337. func = STM32_GET_PIN_FUNC(pinfunc);
  338. if (pin >= pctl->match_data->npins) {
  339. dev_err(pctl->dev, "invalid pin number.\n");
  340. return -EINVAL;
  341. }
  342. if (!stm32_pctrl_is_function_valid(pctl, pin, func)) {
  343. dev_err(pctl->dev, "invalid function.\n");
  344. return -EINVAL;
  345. }
  346. grp = stm32_pctrl_find_group_by_pin(pctl, pin);
  347. if (!grp) {
  348. dev_err(pctl->dev, "unable to match pin %d to group\n",
  349. pin);
  350. return -EINVAL;
  351. }
  352. err = stm32_pctrl_dt_node_to_map_func(pctl, pin, func, grp, map,
  353. reserved_maps, num_maps);
  354. if (err)
  355. return err;
  356. if (has_config) {
  357. err = pinctrl_utils_add_map_configs(pctldev, map,
  358. reserved_maps, num_maps, grp->name,
  359. configs, num_configs,
  360. PIN_MAP_TYPE_CONFIGS_GROUP);
  361. if (err)
  362. return err;
  363. }
  364. }
  365. return 0;
  366. }
  367. static int stm32_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  368. struct device_node *np_config,
  369. struct pinctrl_map **map, unsigned *num_maps)
  370. {
  371. struct device_node *np;
  372. unsigned reserved_maps;
  373. int ret;
  374. *map = NULL;
  375. *num_maps = 0;
  376. reserved_maps = 0;
  377. for_each_child_of_node(np_config, np) {
  378. ret = stm32_pctrl_dt_subnode_to_map(pctldev, np, map,
  379. &reserved_maps, num_maps);
  380. if (ret < 0) {
  381. pinctrl_utils_free_map(pctldev, *map, *num_maps);
  382. return ret;
  383. }
  384. }
  385. return 0;
  386. }
  387. static int stm32_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
  388. {
  389. struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  390. return pctl->ngroups;
  391. }
  392. static const char *stm32_pctrl_get_group_name(struct pinctrl_dev *pctldev,
  393. unsigned group)
  394. {
  395. struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  396. return pctl->groups[group].name;
  397. }
  398. static int stm32_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
  399. unsigned group,
  400. const unsigned **pins,
  401. unsigned *num_pins)
  402. {
  403. struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  404. *pins = (unsigned *)&pctl->groups[group].pin;
  405. *num_pins = 1;
  406. return 0;
  407. }
  408. static const struct pinctrl_ops stm32_pctrl_ops = {
  409. .dt_node_to_map = stm32_pctrl_dt_node_to_map,
  410. .dt_free_map = pinctrl_utils_free_map,
  411. .get_groups_count = stm32_pctrl_get_groups_count,
  412. .get_group_name = stm32_pctrl_get_group_name,
  413. .get_group_pins = stm32_pctrl_get_group_pins,
  414. };
  415. /* Pinmux functions */
  416. static int stm32_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  417. {
  418. return ARRAY_SIZE(stm32_gpio_functions);
  419. }
  420. static const char *stm32_pmx_get_func_name(struct pinctrl_dev *pctldev,
  421. unsigned selector)
  422. {
  423. return stm32_gpio_functions[selector];
  424. }
  425. static int stm32_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  426. unsigned function,
  427. const char * const **groups,
  428. unsigned * const num_groups)
  429. {
  430. struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  431. *groups = pctl->grp_names;
  432. *num_groups = pctl->ngroups;
  433. return 0;
  434. }
  435. static void stm32_pmx_set_mode(struct stm32_gpio_bank *bank,
  436. int pin, u32 mode, u32 alt)
  437. {
  438. u32 val;
  439. int alt_shift = (pin % 8) * 4;
  440. int alt_offset = STM32_GPIO_AFRL + (pin / 8) * 4;
  441. unsigned long flags;
  442. clk_enable(bank->clk);
  443. spin_lock_irqsave(&bank->lock, flags);
  444. val = readl_relaxed(bank->base + alt_offset);
  445. val &= ~GENMASK(alt_shift + 3, alt_shift);
  446. val |= (alt << alt_shift);
  447. writel_relaxed(val, bank->base + alt_offset);
  448. val = readl_relaxed(bank->base + STM32_GPIO_MODER);
  449. val &= ~GENMASK(pin * 2 + 1, pin * 2);
  450. val |= mode << (pin * 2);
  451. writel_relaxed(val, bank->base + STM32_GPIO_MODER);
  452. spin_unlock_irqrestore(&bank->lock, flags);
  453. clk_disable(bank->clk);
  454. }
  455. static void stm32_pmx_get_mode(struct stm32_gpio_bank *bank,
  456. int pin, u32 *mode, u32 *alt)
  457. {
  458. u32 val;
  459. int alt_shift = (pin % 8) * 4;
  460. int alt_offset = STM32_GPIO_AFRL + (pin / 8) * 4;
  461. unsigned long flags;
  462. clk_enable(bank->clk);
  463. spin_lock_irqsave(&bank->lock, flags);
  464. val = readl_relaxed(bank->base + alt_offset);
  465. val &= GENMASK(alt_shift + 3, alt_shift);
  466. *alt = val >> alt_shift;
  467. val = readl_relaxed(bank->base + STM32_GPIO_MODER);
  468. val &= GENMASK(pin * 2 + 1, pin * 2);
  469. *mode = val >> (pin * 2);
  470. spin_unlock_irqrestore(&bank->lock, flags);
  471. clk_disable(bank->clk);
  472. }
  473. static int stm32_pmx_set_mux(struct pinctrl_dev *pctldev,
  474. unsigned function,
  475. unsigned group)
  476. {
  477. bool ret;
  478. struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  479. struct stm32_pinctrl_group *g = pctl->groups + group;
  480. struct pinctrl_gpio_range *range;
  481. struct stm32_gpio_bank *bank;
  482. u32 mode, alt;
  483. int pin;
  484. ret = stm32_pctrl_is_function_valid(pctl, g->pin, function);
  485. if (!ret) {
  486. dev_err(pctl->dev, "invalid function %d on group %d .\n",
  487. function, group);
  488. return -EINVAL;
  489. }
  490. range = pinctrl_find_gpio_range_from_pin(pctldev, g->pin);
  491. bank = gpio_range_to_bank(range);
  492. pin = stm32_gpio_pin(g->pin);
  493. mode = stm32_gpio_get_mode(function);
  494. alt = stm32_gpio_get_alt(function);
  495. stm32_pmx_set_mode(bank, pin, mode, alt);
  496. return 0;
  497. }
  498. static int stm32_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  499. struct pinctrl_gpio_range *range, unsigned gpio,
  500. bool input)
  501. {
  502. struct stm32_gpio_bank *bank = gpio_range_to_bank(range);
  503. int pin = stm32_gpio_pin(gpio);
  504. stm32_pmx_set_mode(bank, pin, !input, 0);
  505. return 0;
  506. }
  507. static const struct pinmux_ops stm32_pmx_ops = {
  508. .get_functions_count = stm32_pmx_get_funcs_cnt,
  509. .get_function_name = stm32_pmx_get_func_name,
  510. .get_function_groups = stm32_pmx_get_func_groups,
  511. .set_mux = stm32_pmx_set_mux,
  512. .gpio_set_direction = stm32_pmx_gpio_set_direction,
  513. };
  514. /* Pinconf functions */
  515. static void stm32_pconf_set_driving(struct stm32_gpio_bank *bank,
  516. unsigned offset, u32 drive)
  517. {
  518. unsigned long flags;
  519. u32 val;
  520. clk_enable(bank->clk);
  521. spin_lock_irqsave(&bank->lock, flags);
  522. val = readl_relaxed(bank->base + STM32_GPIO_TYPER);
  523. val &= ~BIT(offset);
  524. val |= drive << offset;
  525. writel_relaxed(val, bank->base + STM32_GPIO_TYPER);
  526. spin_unlock_irqrestore(&bank->lock, flags);
  527. clk_disable(bank->clk);
  528. }
  529. static u32 stm32_pconf_get_driving(struct stm32_gpio_bank *bank,
  530. unsigned int offset)
  531. {
  532. unsigned long flags;
  533. u32 val;
  534. clk_enable(bank->clk);
  535. spin_lock_irqsave(&bank->lock, flags);
  536. val = readl_relaxed(bank->base + STM32_GPIO_TYPER);
  537. val &= BIT(offset);
  538. spin_unlock_irqrestore(&bank->lock, flags);
  539. clk_disable(bank->clk);
  540. return (val >> offset);
  541. }
  542. static void stm32_pconf_set_speed(struct stm32_gpio_bank *bank,
  543. unsigned offset, u32 speed)
  544. {
  545. unsigned long flags;
  546. u32 val;
  547. clk_enable(bank->clk);
  548. spin_lock_irqsave(&bank->lock, flags);
  549. val = readl_relaxed(bank->base + STM32_GPIO_SPEEDR);
  550. val &= ~GENMASK(offset * 2 + 1, offset * 2);
  551. val |= speed << (offset * 2);
  552. writel_relaxed(val, bank->base + STM32_GPIO_SPEEDR);
  553. spin_unlock_irqrestore(&bank->lock, flags);
  554. clk_disable(bank->clk);
  555. }
  556. static u32 stm32_pconf_get_speed(struct stm32_gpio_bank *bank,
  557. unsigned int offset)
  558. {
  559. unsigned long flags;
  560. u32 val;
  561. clk_enable(bank->clk);
  562. spin_lock_irqsave(&bank->lock, flags);
  563. val = readl_relaxed(bank->base + STM32_GPIO_SPEEDR);
  564. val &= GENMASK(offset * 2 + 1, offset * 2);
  565. spin_unlock_irqrestore(&bank->lock, flags);
  566. clk_disable(bank->clk);
  567. return (val >> (offset * 2));
  568. }
  569. static void stm32_pconf_set_bias(struct stm32_gpio_bank *bank,
  570. unsigned offset, u32 bias)
  571. {
  572. unsigned long flags;
  573. u32 val;
  574. clk_enable(bank->clk);
  575. spin_lock_irqsave(&bank->lock, flags);
  576. val = readl_relaxed(bank->base + STM32_GPIO_PUPDR);
  577. val &= ~GENMASK(offset * 2 + 1, offset * 2);
  578. val |= bias << (offset * 2);
  579. writel_relaxed(val, bank->base + STM32_GPIO_PUPDR);
  580. spin_unlock_irqrestore(&bank->lock, flags);
  581. clk_disable(bank->clk);
  582. }
  583. static u32 stm32_pconf_get_bias(struct stm32_gpio_bank *bank,
  584. unsigned int offset)
  585. {
  586. unsigned long flags;
  587. u32 val;
  588. clk_enable(bank->clk);
  589. spin_lock_irqsave(&bank->lock, flags);
  590. val = readl_relaxed(bank->base + STM32_GPIO_PUPDR);
  591. val &= GENMASK(offset * 2 + 1, offset * 2);
  592. spin_unlock_irqrestore(&bank->lock, flags);
  593. clk_disable(bank->clk);
  594. return (val >> (offset * 2));
  595. }
  596. static bool stm32_pconf_get(struct stm32_gpio_bank *bank,
  597. unsigned int offset, bool dir)
  598. {
  599. unsigned long flags;
  600. u32 val;
  601. clk_enable(bank->clk);
  602. spin_lock_irqsave(&bank->lock, flags);
  603. if (dir)
  604. val = !!(readl_relaxed(bank->base + STM32_GPIO_IDR) &
  605. BIT(offset));
  606. else
  607. val = !!(readl_relaxed(bank->base + STM32_GPIO_ODR) &
  608. BIT(offset));
  609. spin_unlock_irqrestore(&bank->lock, flags);
  610. clk_disable(bank->clk);
  611. return val;
  612. }
  613. static int stm32_pconf_parse_conf(struct pinctrl_dev *pctldev,
  614. unsigned int pin, enum pin_config_param param,
  615. enum pin_config_param arg)
  616. {
  617. struct pinctrl_gpio_range *range;
  618. struct stm32_gpio_bank *bank;
  619. int offset, ret = 0;
  620. range = pinctrl_find_gpio_range_from_pin(pctldev, pin);
  621. bank = gpio_range_to_bank(range);
  622. offset = stm32_gpio_pin(pin);
  623. switch (param) {
  624. case PIN_CONFIG_DRIVE_PUSH_PULL:
  625. stm32_pconf_set_driving(bank, offset, 0);
  626. break;
  627. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  628. stm32_pconf_set_driving(bank, offset, 1);
  629. break;
  630. case PIN_CONFIG_SLEW_RATE:
  631. stm32_pconf_set_speed(bank, offset, arg);
  632. break;
  633. case PIN_CONFIG_BIAS_DISABLE:
  634. stm32_pconf_set_bias(bank, offset, 0);
  635. break;
  636. case PIN_CONFIG_BIAS_PULL_UP:
  637. stm32_pconf_set_bias(bank, offset, 1);
  638. break;
  639. case PIN_CONFIG_BIAS_PULL_DOWN:
  640. stm32_pconf_set_bias(bank, offset, 2);
  641. break;
  642. case PIN_CONFIG_OUTPUT:
  643. __stm32_gpio_set(bank, offset, arg);
  644. ret = stm32_pmx_gpio_set_direction(pctldev, range, pin, false);
  645. break;
  646. default:
  647. ret = -EINVAL;
  648. }
  649. return ret;
  650. }
  651. static int stm32_pconf_group_get(struct pinctrl_dev *pctldev,
  652. unsigned group,
  653. unsigned long *config)
  654. {
  655. struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  656. *config = pctl->groups[group].config;
  657. return 0;
  658. }
  659. static int stm32_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
  660. unsigned long *configs, unsigned num_configs)
  661. {
  662. struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  663. struct stm32_pinctrl_group *g = &pctl->groups[group];
  664. int i, ret;
  665. for (i = 0; i < num_configs; i++) {
  666. ret = stm32_pconf_parse_conf(pctldev, g->pin,
  667. pinconf_to_config_param(configs[i]),
  668. pinconf_to_config_argument(configs[i]));
  669. if (ret < 0)
  670. return ret;
  671. g->config = configs[i];
  672. }
  673. return 0;
  674. }
  675. static void stm32_pconf_dbg_show(struct pinctrl_dev *pctldev,
  676. struct seq_file *s,
  677. unsigned int pin)
  678. {
  679. struct pinctrl_gpio_range *range;
  680. struct stm32_gpio_bank *bank;
  681. int offset;
  682. u32 mode, alt, drive, speed, bias;
  683. static const char * const modes[] = {
  684. "input", "output", "alternate", "analog" };
  685. static const char * const speeds[] = {
  686. "low", "medium", "high", "very high" };
  687. static const char * const biasing[] = {
  688. "floating", "pull up", "pull down", "" };
  689. bool val;
  690. range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
  691. bank = gpio_range_to_bank(range);
  692. offset = stm32_gpio_pin(pin);
  693. stm32_pmx_get_mode(bank, offset, &mode, &alt);
  694. bias = stm32_pconf_get_bias(bank, offset);
  695. seq_printf(s, "%s ", modes[mode]);
  696. switch (mode) {
  697. /* input */
  698. case 0:
  699. val = stm32_pconf_get(bank, offset, true);
  700. seq_printf(s, "- %s - %s",
  701. val ? "high" : "low",
  702. biasing[bias]);
  703. break;
  704. /* output */
  705. case 1:
  706. drive = stm32_pconf_get_driving(bank, offset);
  707. speed = stm32_pconf_get_speed(bank, offset);
  708. val = stm32_pconf_get(bank, offset, false);
  709. seq_printf(s, "- %s - %s - %s - %s %s",
  710. val ? "high" : "low",
  711. drive ? "open drain" : "push pull",
  712. biasing[bias],
  713. speeds[speed], "speed");
  714. break;
  715. /* alternate */
  716. case 2:
  717. drive = stm32_pconf_get_driving(bank, offset);
  718. speed = stm32_pconf_get_speed(bank, offset);
  719. seq_printf(s, "%d - %s - %s - %s %s", alt,
  720. drive ? "open drain" : "push pull",
  721. biasing[bias],
  722. speeds[speed], "speed");
  723. break;
  724. /* analog */
  725. case 3:
  726. break;
  727. }
  728. }
  729. static const struct pinconf_ops stm32_pconf_ops = {
  730. .pin_config_group_get = stm32_pconf_group_get,
  731. .pin_config_group_set = stm32_pconf_group_set,
  732. .pin_config_dbg_show = stm32_pconf_dbg_show,
  733. };
  734. static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl,
  735. struct device_node *np)
  736. {
  737. int bank_nr = pctl->nbanks;
  738. struct stm32_gpio_bank *bank = &pctl->banks[bank_nr];
  739. struct pinctrl_gpio_range *range = &bank->range;
  740. struct device *dev = pctl->dev;
  741. struct resource res;
  742. struct reset_control *rstc;
  743. int err, npins;
  744. rstc = of_reset_control_get(np, NULL);
  745. if (!IS_ERR(rstc))
  746. reset_control_deassert(rstc);
  747. if (of_address_to_resource(np, 0, &res))
  748. return -ENODEV;
  749. bank->base = devm_ioremap_resource(dev, &res);
  750. if (IS_ERR(bank->base))
  751. return PTR_ERR(bank->base);
  752. bank->clk = of_clk_get_by_name(np, NULL);
  753. if (IS_ERR(bank->clk)) {
  754. dev_err(dev, "failed to get clk (%ld)\n", PTR_ERR(bank->clk));
  755. return PTR_ERR(bank->clk);
  756. }
  757. err = clk_prepare(bank->clk);
  758. if (err) {
  759. dev_err(dev, "failed to prepare clk (%d)\n", err);
  760. return err;
  761. }
  762. npins = pctl->match_data->npins;
  763. npins -= bank_nr * STM32_GPIO_PINS_PER_BANK;
  764. if (npins < 0)
  765. return -EINVAL;
  766. else if (npins > STM32_GPIO_PINS_PER_BANK)
  767. npins = STM32_GPIO_PINS_PER_BANK;
  768. bank->gpio_chip = stm32_gpio_template;
  769. bank->gpio_chip.base = bank_nr * STM32_GPIO_PINS_PER_BANK;
  770. bank->gpio_chip.ngpio = npins;
  771. bank->gpio_chip.of_node = np;
  772. bank->gpio_chip.parent = dev;
  773. spin_lock_init(&bank->lock);
  774. of_property_read_string(np, "st,bank-name", &range->name);
  775. bank->gpio_chip.label = range->name;
  776. range->id = bank_nr;
  777. range->pin_base = range->base = range->id * STM32_GPIO_PINS_PER_BANK;
  778. range->npins = bank->gpio_chip.ngpio;
  779. range->gc = &bank->gpio_chip;
  780. /* create irq hierarchical domain */
  781. bank->fwnode = of_node_to_fwnode(np);
  782. bank->domain = irq_domain_create_hierarchy(pctl->domain, 0,
  783. STM32_GPIO_IRQ_LINE, bank->fwnode,
  784. &stm32_gpio_domain_ops, bank);
  785. if (!bank->domain)
  786. return -ENODEV;
  787. err = gpiochip_add_data(&bank->gpio_chip, bank);
  788. if (err) {
  789. dev_err(dev, "Failed to add gpiochip(%d)!\n", bank_nr);
  790. return err;
  791. }
  792. dev_info(dev, "%s bank added\n", range->name);
  793. return 0;
  794. }
  795. static int stm32_pctrl_dt_setup_irq(struct platform_device *pdev,
  796. struct stm32_pinctrl *pctl)
  797. {
  798. struct device_node *np = pdev->dev.of_node, *parent;
  799. struct device *dev = &pdev->dev;
  800. struct regmap *rm;
  801. int offset, ret, i;
  802. parent = of_irq_find_parent(np);
  803. if (!parent)
  804. return -ENXIO;
  805. pctl->domain = irq_find_host(parent);
  806. if (!pctl->domain)
  807. return -ENXIO;
  808. pctl->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  809. if (IS_ERR(pctl->regmap))
  810. return PTR_ERR(pctl->regmap);
  811. rm = pctl->regmap;
  812. ret = of_property_read_u32_index(np, "st,syscfg", 1, &offset);
  813. if (ret)
  814. return ret;
  815. for (i = 0; i < STM32_GPIO_PINS_PER_BANK; i++) {
  816. struct reg_field mux;
  817. mux.reg = offset + (i / 4) * 4;
  818. mux.lsb = (i % 4) * 4;
  819. mux.msb = mux.lsb + 3;
  820. pctl->irqmux[i] = devm_regmap_field_alloc(dev, rm, mux);
  821. if (IS_ERR(pctl->irqmux[i]))
  822. return PTR_ERR(pctl->irqmux[i]);
  823. }
  824. return 0;
  825. }
  826. static int stm32_pctrl_build_state(struct platform_device *pdev)
  827. {
  828. struct stm32_pinctrl *pctl = platform_get_drvdata(pdev);
  829. int i;
  830. pctl->ngroups = pctl->match_data->npins;
  831. /* Allocate groups */
  832. pctl->groups = devm_kcalloc(&pdev->dev, pctl->ngroups,
  833. sizeof(*pctl->groups), GFP_KERNEL);
  834. if (!pctl->groups)
  835. return -ENOMEM;
  836. /* We assume that one pin is one group, use pin name as group name. */
  837. pctl->grp_names = devm_kcalloc(&pdev->dev, pctl->ngroups,
  838. sizeof(*pctl->grp_names), GFP_KERNEL);
  839. if (!pctl->grp_names)
  840. return -ENOMEM;
  841. for (i = 0; i < pctl->match_data->npins; i++) {
  842. const struct stm32_desc_pin *pin = pctl->match_data->pins + i;
  843. struct stm32_pinctrl_group *group = pctl->groups + i;
  844. group->name = pin->pin.name;
  845. group->pin = pin->pin.number;
  846. pctl->grp_names[i] = pin->pin.name;
  847. }
  848. return 0;
  849. }
  850. int stm32_pctl_probe(struct platform_device *pdev)
  851. {
  852. struct device_node *np = pdev->dev.of_node;
  853. struct device_node *child;
  854. const struct of_device_id *match;
  855. struct device *dev = &pdev->dev;
  856. struct stm32_pinctrl *pctl;
  857. struct pinctrl_pin_desc *pins;
  858. int i, ret, banks = 0;
  859. if (!np)
  860. return -EINVAL;
  861. match = of_match_device(dev->driver->of_match_table, dev);
  862. if (!match || !match->data)
  863. return -EINVAL;
  864. if (!of_find_property(np, "pins-are-numbered", NULL)) {
  865. dev_err(dev, "only support pins-are-numbered format\n");
  866. return -EINVAL;
  867. }
  868. pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL);
  869. if (!pctl)
  870. return -ENOMEM;
  871. platform_set_drvdata(pdev, pctl);
  872. pctl->dev = dev;
  873. pctl->match_data = match->data;
  874. ret = stm32_pctrl_build_state(pdev);
  875. if (ret) {
  876. dev_err(dev, "build state failed: %d\n", ret);
  877. return -EINVAL;
  878. }
  879. if (of_find_property(np, "interrupt-parent", NULL)) {
  880. ret = stm32_pctrl_dt_setup_irq(pdev, pctl);
  881. if (ret)
  882. return ret;
  883. }
  884. for_each_child_of_node(np, child)
  885. if (of_property_read_bool(child, "gpio-controller"))
  886. banks++;
  887. if (!banks) {
  888. dev_err(dev, "at least one GPIO bank is required\n");
  889. return -EINVAL;
  890. }
  891. pctl->banks = devm_kcalloc(dev, banks, sizeof(*pctl->banks),
  892. GFP_KERNEL);
  893. if (!pctl->banks)
  894. return -ENOMEM;
  895. for_each_child_of_node(np, child) {
  896. if (of_property_read_bool(child, "gpio-controller")) {
  897. ret = stm32_gpiolib_register_bank(pctl, child);
  898. if (ret)
  899. return ret;
  900. pctl->nbanks++;
  901. }
  902. }
  903. pins = devm_kcalloc(&pdev->dev, pctl->match_data->npins, sizeof(*pins),
  904. GFP_KERNEL);
  905. if (!pins)
  906. return -ENOMEM;
  907. for (i = 0; i < pctl->match_data->npins; i++)
  908. pins[i] = pctl->match_data->pins[i].pin;
  909. pctl->pctl_desc.name = dev_name(&pdev->dev);
  910. pctl->pctl_desc.owner = THIS_MODULE;
  911. pctl->pctl_desc.pins = pins;
  912. pctl->pctl_desc.npins = pctl->match_data->npins;
  913. pctl->pctl_desc.confops = &stm32_pconf_ops;
  914. pctl->pctl_desc.pctlops = &stm32_pctrl_ops;
  915. pctl->pctl_desc.pmxops = &stm32_pmx_ops;
  916. pctl->dev = &pdev->dev;
  917. pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, &pctl->pctl_desc,
  918. pctl);
  919. if (IS_ERR(pctl->pctl_dev)) {
  920. dev_err(&pdev->dev, "Failed pinctrl registration\n");
  921. return PTR_ERR(pctl->pctl_dev);
  922. }
  923. for (i = 0; i < pctl->nbanks; i++)
  924. pinctrl_add_gpio_range(pctl->pctl_dev, &pctl->banks[i].range);
  925. dev_info(dev, "Pinctrl STM32 initialized\n");
  926. return 0;
  927. }