pinctrl-sunxi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * Allwinner A1X SoCs pinctrl driver.
  3. *
  4. * Copyright (C) 2012 Maxime Ripard
  5. *
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/io.h>
  13. #include <linux/clk.h>
  14. #include <linux/gpio/driver.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/irqchip/chained_irq.h>
  17. #include <linux/export.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/pinctrl/consumer.h>
  23. #include <linux/pinctrl/machine.h>
  24. #include <linux/pinctrl/pinctrl.h>
  25. #include <linux/pinctrl/pinconf-generic.h>
  26. #include <linux/pinctrl/pinmux.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include "../core.h"
  30. #include "pinctrl-sunxi.h"
  31. static struct irq_chip sunxi_pinctrl_edge_irq_chip;
  32. static struct irq_chip sunxi_pinctrl_level_irq_chip;
  33. static struct sunxi_pinctrl_group *
  34. sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
  35. {
  36. int i;
  37. for (i = 0; i < pctl->ngroups; i++) {
  38. struct sunxi_pinctrl_group *grp = pctl->groups + i;
  39. if (!strcmp(grp->name, group))
  40. return grp;
  41. }
  42. return NULL;
  43. }
  44. static struct sunxi_pinctrl_function *
  45. sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
  46. const char *name)
  47. {
  48. struct sunxi_pinctrl_function *func = pctl->functions;
  49. int i;
  50. for (i = 0; i < pctl->nfunctions; i++) {
  51. if (!func[i].name)
  52. break;
  53. if (!strcmp(func[i].name, name))
  54. return func + i;
  55. }
  56. return NULL;
  57. }
  58. static struct sunxi_desc_function *
  59. sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
  60. const char *pin_name,
  61. const char *func_name)
  62. {
  63. int i;
  64. for (i = 0; i < pctl->desc->npins; i++) {
  65. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  66. if (!strcmp(pin->pin.name, pin_name)) {
  67. struct sunxi_desc_function *func = pin->functions;
  68. while (func->name) {
  69. if (!strcmp(func->name, func_name))
  70. return func;
  71. func++;
  72. }
  73. }
  74. }
  75. return NULL;
  76. }
  77. static struct sunxi_desc_function *
  78. sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl,
  79. const u16 pin_num,
  80. const char *func_name)
  81. {
  82. int i;
  83. for (i = 0; i < pctl->desc->npins; i++) {
  84. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  85. if (pin->pin.number == pin_num) {
  86. struct sunxi_desc_function *func = pin->functions;
  87. while (func->name) {
  88. if (!strcmp(func->name, func_name))
  89. return func;
  90. func++;
  91. }
  92. }
  93. }
  94. return NULL;
  95. }
  96. static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
  97. {
  98. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  99. return pctl->ngroups;
  100. }
  101. static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
  102. unsigned group)
  103. {
  104. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  105. return pctl->groups[group].name;
  106. }
  107. static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
  108. unsigned group,
  109. const unsigned **pins,
  110. unsigned *num_pins)
  111. {
  112. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  113. *pins = (unsigned *)&pctl->groups[group].pin;
  114. *num_pins = 1;
  115. return 0;
  116. }
  117. static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  118. struct device_node *node,
  119. struct pinctrl_map **map,
  120. unsigned *num_maps)
  121. {
  122. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  123. unsigned long *pinconfig;
  124. struct property *prop;
  125. const char *function;
  126. const char *group;
  127. int ret, nmaps, i = 0;
  128. u32 val;
  129. *map = NULL;
  130. *num_maps = 0;
  131. ret = of_property_read_string(node, "allwinner,function", &function);
  132. if (ret) {
  133. dev_err(pctl->dev,
  134. "missing allwinner,function property in node %s\n",
  135. node->name);
  136. return -EINVAL;
  137. }
  138. nmaps = of_property_count_strings(node, "allwinner,pins") * 2;
  139. if (nmaps < 0) {
  140. dev_err(pctl->dev,
  141. "missing allwinner,pins property in node %s\n",
  142. node->name);
  143. return -EINVAL;
  144. }
  145. *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
  146. if (!*map)
  147. return -ENOMEM;
  148. of_property_for_each_string(node, "allwinner,pins", prop, group) {
  149. struct sunxi_pinctrl_group *grp =
  150. sunxi_pinctrl_find_group_by_name(pctl, group);
  151. int j = 0, configlen = 0;
  152. if (!grp) {
  153. dev_err(pctl->dev, "unknown pin %s", group);
  154. continue;
  155. }
  156. if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
  157. grp->name,
  158. function)) {
  159. dev_err(pctl->dev, "unsupported function %s on pin %s",
  160. function, group);
  161. continue;
  162. }
  163. (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
  164. (*map)[i].data.mux.group = group;
  165. (*map)[i].data.mux.function = function;
  166. i++;
  167. (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
  168. (*map)[i].data.configs.group_or_pin = group;
  169. if (of_find_property(node, "allwinner,drive", NULL))
  170. configlen++;
  171. if (of_find_property(node, "allwinner,pull", NULL))
  172. configlen++;
  173. pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);
  174. if (!pinconfig) {
  175. kfree(*map);
  176. return -ENOMEM;
  177. }
  178. if (!of_property_read_u32(node, "allwinner,drive", &val)) {
  179. u16 strength = (val + 1) * 10;
  180. pinconfig[j++] =
  181. pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
  182. strength);
  183. }
  184. if (!of_property_read_u32(node, "allwinner,pull", &val)) {
  185. enum pin_config_param pull = PIN_CONFIG_END;
  186. if (val == 1)
  187. pull = PIN_CONFIG_BIAS_PULL_UP;
  188. else if (val == 2)
  189. pull = PIN_CONFIG_BIAS_PULL_DOWN;
  190. pinconfig[j++] = pinconf_to_config_packed(pull, 0);
  191. }
  192. (*map)[i].data.configs.configs = pinconfig;
  193. (*map)[i].data.configs.num_configs = configlen;
  194. i++;
  195. }
  196. *num_maps = nmaps;
  197. return 0;
  198. }
  199. static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
  200. struct pinctrl_map *map,
  201. unsigned num_maps)
  202. {
  203. int i;
  204. for (i = 0; i < num_maps; i++) {
  205. if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
  206. kfree(map[i].data.configs.configs);
  207. }
  208. kfree(map);
  209. }
  210. static const struct pinctrl_ops sunxi_pctrl_ops = {
  211. .dt_node_to_map = sunxi_pctrl_dt_node_to_map,
  212. .dt_free_map = sunxi_pctrl_dt_free_map,
  213. .get_groups_count = sunxi_pctrl_get_groups_count,
  214. .get_group_name = sunxi_pctrl_get_group_name,
  215. .get_group_pins = sunxi_pctrl_get_group_pins,
  216. };
  217. static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
  218. unsigned group,
  219. unsigned long *config)
  220. {
  221. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  222. *config = pctl->groups[group].config;
  223. return 0;
  224. }
  225. static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev,
  226. unsigned group,
  227. unsigned long *configs,
  228. unsigned num_configs)
  229. {
  230. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  231. struct sunxi_pinctrl_group *g = &pctl->groups[group];
  232. unsigned long flags;
  233. unsigned pin = g->pin - pctl->desc->pin_base;
  234. u32 val, mask;
  235. u16 strength;
  236. u8 dlevel;
  237. int i;
  238. spin_lock_irqsave(&pctl->lock, flags);
  239. for (i = 0; i < num_configs; i++) {
  240. switch (pinconf_to_config_param(configs[i])) {
  241. case PIN_CONFIG_DRIVE_STRENGTH:
  242. strength = pinconf_to_config_argument(configs[i]);
  243. if (strength > 40) {
  244. spin_unlock_irqrestore(&pctl->lock, flags);
  245. return -EINVAL;
  246. }
  247. /*
  248. * We convert from mA to what the register expects:
  249. * 0: 10mA
  250. * 1: 20mA
  251. * 2: 30mA
  252. * 3: 40mA
  253. */
  254. dlevel = strength / 10 - 1;
  255. val = readl(pctl->membase + sunxi_dlevel_reg(pin));
  256. mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(pin);
  257. writel((val & ~mask)
  258. | dlevel << sunxi_dlevel_offset(pin),
  259. pctl->membase + sunxi_dlevel_reg(pin));
  260. break;
  261. case PIN_CONFIG_BIAS_PULL_UP:
  262. val = readl(pctl->membase + sunxi_pull_reg(pin));
  263. mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
  264. writel((val & ~mask) | 1 << sunxi_pull_offset(pin),
  265. pctl->membase + sunxi_pull_reg(pin));
  266. break;
  267. case PIN_CONFIG_BIAS_PULL_DOWN:
  268. val = readl(pctl->membase + sunxi_pull_reg(pin));
  269. mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
  270. writel((val & ~mask) | 2 << sunxi_pull_offset(pin),
  271. pctl->membase + sunxi_pull_reg(pin));
  272. break;
  273. default:
  274. break;
  275. }
  276. /* cache the config value */
  277. g->config = configs[i];
  278. } /* for each config */
  279. spin_unlock_irqrestore(&pctl->lock, flags);
  280. return 0;
  281. }
  282. static const struct pinconf_ops sunxi_pconf_ops = {
  283. .pin_config_group_get = sunxi_pconf_group_get,
  284. .pin_config_group_set = sunxi_pconf_group_set,
  285. };
  286. static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  287. {
  288. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  289. return pctl->nfunctions;
  290. }
  291. static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
  292. unsigned function)
  293. {
  294. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  295. return pctl->functions[function].name;
  296. }
  297. static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  298. unsigned function,
  299. const char * const **groups,
  300. unsigned * const num_groups)
  301. {
  302. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  303. *groups = pctl->functions[function].groups;
  304. *num_groups = pctl->functions[function].ngroups;
  305. return 0;
  306. }
  307. static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
  308. unsigned pin,
  309. u8 config)
  310. {
  311. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  312. unsigned long flags;
  313. u32 val, mask;
  314. spin_lock_irqsave(&pctl->lock, flags);
  315. pin -= pctl->desc->pin_base;
  316. val = readl(pctl->membase + sunxi_mux_reg(pin));
  317. mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
  318. writel((val & ~mask) | config << sunxi_mux_offset(pin),
  319. pctl->membase + sunxi_mux_reg(pin));
  320. spin_unlock_irqrestore(&pctl->lock, flags);
  321. }
  322. static int sunxi_pmx_set_mux(struct pinctrl_dev *pctldev,
  323. unsigned function,
  324. unsigned group)
  325. {
  326. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  327. struct sunxi_pinctrl_group *g = pctl->groups + group;
  328. struct sunxi_pinctrl_function *func = pctl->functions + function;
  329. struct sunxi_desc_function *desc =
  330. sunxi_pinctrl_desc_find_function_by_name(pctl,
  331. g->name,
  332. func->name);
  333. if (!desc)
  334. return -EINVAL;
  335. sunxi_pmx_set(pctldev, g->pin, desc->muxval);
  336. return 0;
  337. }
  338. static int
  339. sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  340. struct pinctrl_gpio_range *range,
  341. unsigned offset,
  342. bool input)
  343. {
  344. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  345. struct sunxi_desc_function *desc;
  346. const char *func;
  347. if (input)
  348. func = "gpio_in";
  349. else
  350. func = "gpio_out";
  351. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
  352. if (!desc)
  353. return -EINVAL;
  354. sunxi_pmx_set(pctldev, offset, desc->muxval);
  355. return 0;
  356. }
  357. static const struct pinmux_ops sunxi_pmx_ops = {
  358. .get_functions_count = sunxi_pmx_get_funcs_cnt,
  359. .get_function_name = sunxi_pmx_get_func_name,
  360. .get_function_groups = sunxi_pmx_get_func_groups,
  361. .set_mux = sunxi_pmx_set_mux,
  362. .gpio_set_direction = sunxi_pmx_gpio_set_direction,
  363. };
  364. static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
  365. unsigned offset)
  366. {
  367. return pinctrl_gpio_direction_input(chip->base + offset);
  368. }
  369. static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
  370. {
  371. struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
  372. u32 reg = sunxi_data_reg(offset);
  373. u8 index = sunxi_data_offset(offset);
  374. bool set_mux = pctl->desc->irq_read_needs_mux &&
  375. gpiochip_line_is_irq(chip, offset);
  376. u32 pin = offset + chip->base;
  377. u32 val;
  378. if (set_mux)
  379. sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_INPUT);
  380. val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
  381. if (set_mux)
  382. sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_IRQ);
  383. return !!val;
  384. }
  385. static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
  386. unsigned offset, int value)
  387. {
  388. struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
  389. u32 reg = sunxi_data_reg(offset);
  390. u8 index = sunxi_data_offset(offset);
  391. unsigned long flags;
  392. u32 regval;
  393. spin_lock_irqsave(&pctl->lock, flags);
  394. regval = readl(pctl->membase + reg);
  395. if (value)
  396. regval |= BIT(index);
  397. else
  398. regval &= ~(BIT(index));
  399. writel(regval, pctl->membase + reg);
  400. spin_unlock_irqrestore(&pctl->lock, flags);
  401. }
  402. static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
  403. unsigned offset, int value)
  404. {
  405. sunxi_pinctrl_gpio_set(chip, offset, value);
  406. return pinctrl_gpio_direction_output(chip->base + offset);
  407. }
  408. static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
  409. const struct of_phandle_args *gpiospec,
  410. u32 *flags)
  411. {
  412. int pin, base;
  413. base = PINS_PER_BANK * gpiospec->args[0];
  414. pin = base + gpiospec->args[1];
  415. if (pin > gc->ngpio)
  416. return -EINVAL;
  417. if (flags)
  418. *flags = gpiospec->args[2];
  419. return pin;
  420. }
  421. static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  422. {
  423. struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
  424. struct sunxi_desc_function *desc;
  425. unsigned pinnum = pctl->desc->pin_base + offset;
  426. unsigned irqnum;
  427. if (offset >= chip->ngpio)
  428. return -ENXIO;
  429. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pinnum, "irq");
  430. if (!desc)
  431. return -EINVAL;
  432. irqnum = desc->irqbank * IRQ_PER_BANK + desc->irqnum;
  433. dev_dbg(chip->parent, "%s: request IRQ for GPIO %d, return %d\n",
  434. chip->label, offset + chip->base, irqnum);
  435. return irq_find_mapping(pctl->domain, irqnum);
  436. }
  437. static int sunxi_pinctrl_irq_request_resources(struct irq_data *d)
  438. {
  439. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  440. struct sunxi_desc_function *func;
  441. int ret;
  442. func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
  443. pctl->irq_array[d->hwirq], "irq");
  444. if (!func)
  445. return -EINVAL;
  446. ret = gpiochip_lock_as_irq(pctl->chip,
  447. pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
  448. if (ret) {
  449. dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
  450. irqd_to_hwirq(d));
  451. return ret;
  452. }
  453. /* Change muxing to INT mode */
  454. sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
  455. return 0;
  456. }
  457. static void sunxi_pinctrl_irq_release_resources(struct irq_data *d)
  458. {
  459. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  460. gpiochip_unlock_as_irq(pctl->chip,
  461. pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
  462. }
  463. static int sunxi_pinctrl_irq_set_type(struct irq_data *d, unsigned int type)
  464. {
  465. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  466. u32 reg = sunxi_irq_cfg_reg(d->hwirq, pctl->desc->irq_bank_base);
  467. u8 index = sunxi_irq_cfg_offset(d->hwirq);
  468. unsigned long flags;
  469. u32 regval;
  470. u8 mode;
  471. switch (type) {
  472. case IRQ_TYPE_EDGE_RISING:
  473. mode = IRQ_EDGE_RISING;
  474. break;
  475. case IRQ_TYPE_EDGE_FALLING:
  476. mode = IRQ_EDGE_FALLING;
  477. break;
  478. case IRQ_TYPE_EDGE_BOTH:
  479. mode = IRQ_EDGE_BOTH;
  480. break;
  481. case IRQ_TYPE_LEVEL_HIGH:
  482. mode = IRQ_LEVEL_HIGH;
  483. break;
  484. case IRQ_TYPE_LEVEL_LOW:
  485. mode = IRQ_LEVEL_LOW;
  486. break;
  487. default:
  488. return -EINVAL;
  489. }
  490. spin_lock_irqsave(&pctl->lock, flags);
  491. if (type & IRQ_TYPE_LEVEL_MASK)
  492. irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_level_irq_chip,
  493. handle_fasteoi_irq, NULL);
  494. else
  495. irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_edge_irq_chip,
  496. handle_edge_irq, NULL);
  497. regval = readl(pctl->membase + reg);
  498. regval &= ~(IRQ_CFG_IRQ_MASK << index);
  499. writel(regval | (mode << index), pctl->membase + reg);
  500. spin_unlock_irqrestore(&pctl->lock, flags);
  501. return 0;
  502. }
  503. static void sunxi_pinctrl_irq_ack(struct irq_data *d)
  504. {
  505. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  506. u32 status_reg = sunxi_irq_status_reg(d->hwirq,
  507. pctl->desc->irq_bank_base);
  508. u8 status_idx = sunxi_irq_status_offset(d->hwirq);
  509. /* Clear the IRQ */
  510. writel(1 << status_idx, pctl->membase + status_reg);
  511. }
  512. static void sunxi_pinctrl_irq_mask(struct irq_data *d)
  513. {
  514. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  515. u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base);
  516. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  517. unsigned long flags;
  518. u32 val;
  519. spin_lock_irqsave(&pctl->lock, flags);
  520. /* Mask the IRQ */
  521. val = readl(pctl->membase + reg);
  522. writel(val & ~(1 << idx), pctl->membase + reg);
  523. spin_unlock_irqrestore(&pctl->lock, flags);
  524. }
  525. static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
  526. {
  527. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  528. u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base);
  529. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  530. unsigned long flags;
  531. u32 val;
  532. spin_lock_irqsave(&pctl->lock, flags);
  533. /* Unmask the IRQ */
  534. val = readl(pctl->membase + reg);
  535. writel(val | (1 << idx), pctl->membase + reg);
  536. spin_unlock_irqrestore(&pctl->lock, flags);
  537. }
  538. static void sunxi_pinctrl_irq_ack_unmask(struct irq_data *d)
  539. {
  540. sunxi_pinctrl_irq_ack(d);
  541. sunxi_pinctrl_irq_unmask(d);
  542. }
  543. static struct irq_chip sunxi_pinctrl_edge_irq_chip = {
  544. .name = "sunxi_pio_edge",
  545. .irq_ack = sunxi_pinctrl_irq_ack,
  546. .irq_mask = sunxi_pinctrl_irq_mask,
  547. .irq_unmask = sunxi_pinctrl_irq_unmask,
  548. .irq_request_resources = sunxi_pinctrl_irq_request_resources,
  549. .irq_release_resources = sunxi_pinctrl_irq_release_resources,
  550. .irq_set_type = sunxi_pinctrl_irq_set_type,
  551. .flags = IRQCHIP_SKIP_SET_WAKE,
  552. };
  553. static struct irq_chip sunxi_pinctrl_level_irq_chip = {
  554. .name = "sunxi_pio_level",
  555. .irq_eoi = sunxi_pinctrl_irq_ack,
  556. .irq_mask = sunxi_pinctrl_irq_mask,
  557. .irq_unmask = sunxi_pinctrl_irq_unmask,
  558. /* Define irq_enable / disable to avoid spurious irqs for drivers
  559. * using these to suppress irqs while they clear the irq source */
  560. .irq_enable = sunxi_pinctrl_irq_ack_unmask,
  561. .irq_disable = sunxi_pinctrl_irq_mask,
  562. .irq_request_resources = sunxi_pinctrl_irq_request_resources,
  563. .irq_release_resources = sunxi_pinctrl_irq_release_resources,
  564. .irq_set_type = sunxi_pinctrl_irq_set_type,
  565. .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_EOI_THREADED |
  566. IRQCHIP_EOI_IF_HANDLED,
  567. };
  568. static int sunxi_pinctrl_irq_of_xlate(struct irq_domain *d,
  569. struct device_node *node,
  570. const u32 *intspec,
  571. unsigned int intsize,
  572. unsigned long *out_hwirq,
  573. unsigned int *out_type)
  574. {
  575. struct sunxi_pinctrl *pctl = d->host_data;
  576. struct sunxi_desc_function *desc;
  577. int pin, base;
  578. if (intsize < 3)
  579. return -EINVAL;
  580. base = PINS_PER_BANK * intspec[0];
  581. pin = pctl->desc->pin_base + base + intspec[1];
  582. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
  583. if (!desc)
  584. return -EINVAL;
  585. *out_hwirq = desc->irqbank * PINS_PER_BANK + desc->irqnum;
  586. *out_type = intspec[2];
  587. return 0;
  588. }
  589. static struct irq_domain_ops sunxi_pinctrl_irq_domain_ops = {
  590. .xlate = sunxi_pinctrl_irq_of_xlate,
  591. };
  592. static void sunxi_pinctrl_irq_handler(struct irq_desc *desc)
  593. {
  594. unsigned int irq = irq_desc_get_irq(desc);
  595. struct irq_chip *chip = irq_desc_get_chip(desc);
  596. struct sunxi_pinctrl *pctl = irq_desc_get_handler_data(desc);
  597. unsigned long bank, reg, val;
  598. for (bank = 0; bank < pctl->desc->irq_banks; bank++)
  599. if (irq == pctl->irq[bank])
  600. break;
  601. if (bank == pctl->desc->irq_banks)
  602. return;
  603. reg = sunxi_irq_status_reg_from_bank(bank, pctl->desc->irq_bank_base);
  604. val = readl(pctl->membase + reg);
  605. if (val) {
  606. int irqoffset;
  607. chained_irq_enter(chip, desc);
  608. for_each_set_bit(irqoffset, &val, IRQ_PER_BANK) {
  609. int pin_irq = irq_find_mapping(pctl->domain,
  610. bank * IRQ_PER_BANK + irqoffset);
  611. generic_handle_irq(pin_irq);
  612. }
  613. chained_irq_exit(chip, desc);
  614. }
  615. }
  616. static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
  617. const char *name)
  618. {
  619. struct sunxi_pinctrl_function *func = pctl->functions;
  620. while (func->name) {
  621. /* function already there */
  622. if (strcmp(func->name, name) == 0) {
  623. func->ngroups++;
  624. return -EEXIST;
  625. }
  626. func++;
  627. }
  628. func->name = name;
  629. func->ngroups = 1;
  630. pctl->nfunctions++;
  631. return 0;
  632. }
  633. static int sunxi_pinctrl_build_state(struct platform_device *pdev)
  634. {
  635. struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
  636. int i;
  637. pctl->ngroups = pctl->desc->npins;
  638. /* Allocate groups */
  639. pctl->groups = devm_kzalloc(&pdev->dev,
  640. pctl->ngroups * sizeof(*pctl->groups),
  641. GFP_KERNEL);
  642. if (!pctl->groups)
  643. return -ENOMEM;
  644. for (i = 0; i < pctl->desc->npins; i++) {
  645. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  646. struct sunxi_pinctrl_group *group = pctl->groups + i;
  647. group->name = pin->pin.name;
  648. group->pin = pin->pin.number;
  649. }
  650. /*
  651. * We suppose that we won't have any more functions than pins,
  652. * we'll reallocate that later anyway
  653. */
  654. pctl->functions = devm_kzalloc(&pdev->dev,
  655. pctl->desc->npins * sizeof(*pctl->functions),
  656. GFP_KERNEL);
  657. if (!pctl->functions)
  658. return -ENOMEM;
  659. /* Count functions and their associated groups */
  660. for (i = 0; i < pctl->desc->npins; i++) {
  661. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  662. struct sunxi_desc_function *func = pin->functions;
  663. while (func->name) {
  664. /* Create interrupt mapping while we're at it */
  665. if (!strcmp(func->name, "irq")) {
  666. int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
  667. pctl->irq_array[irqnum] = pin->pin.number;
  668. }
  669. sunxi_pinctrl_add_function(pctl, func->name);
  670. func++;
  671. }
  672. }
  673. pctl->functions = krealloc(pctl->functions,
  674. pctl->nfunctions * sizeof(*pctl->functions),
  675. GFP_KERNEL);
  676. for (i = 0; i < pctl->desc->npins; i++) {
  677. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  678. struct sunxi_desc_function *func = pin->functions;
  679. while (func->name) {
  680. struct sunxi_pinctrl_function *func_item;
  681. const char **func_grp;
  682. func_item = sunxi_pinctrl_find_function_by_name(pctl,
  683. func->name);
  684. if (!func_item)
  685. return -EINVAL;
  686. if (!func_item->groups) {
  687. func_item->groups =
  688. devm_kzalloc(&pdev->dev,
  689. func_item->ngroups * sizeof(*func_item->groups),
  690. GFP_KERNEL);
  691. if (!func_item->groups)
  692. return -ENOMEM;
  693. }
  694. func_grp = func_item->groups;
  695. while (*func_grp)
  696. func_grp++;
  697. *func_grp = pin->pin.name;
  698. func++;
  699. }
  700. }
  701. return 0;
  702. }
  703. int sunxi_pinctrl_init(struct platform_device *pdev,
  704. const struct sunxi_pinctrl_desc *desc)
  705. {
  706. struct device_node *node = pdev->dev.of_node;
  707. struct pinctrl_desc *pctrl_desc;
  708. struct pinctrl_pin_desc *pins;
  709. struct sunxi_pinctrl *pctl;
  710. struct resource *res;
  711. int i, ret, last_pin;
  712. struct clk *clk;
  713. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  714. if (!pctl)
  715. return -ENOMEM;
  716. platform_set_drvdata(pdev, pctl);
  717. spin_lock_init(&pctl->lock);
  718. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  719. pctl->membase = devm_ioremap_resource(&pdev->dev, res);
  720. if (IS_ERR(pctl->membase))
  721. return PTR_ERR(pctl->membase);
  722. pctl->dev = &pdev->dev;
  723. pctl->desc = desc;
  724. pctl->irq_array = devm_kcalloc(&pdev->dev,
  725. IRQ_PER_BANK * pctl->desc->irq_banks,
  726. sizeof(*pctl->irq_array),
  727. GFP_KERNEL);
  728. if (!pctl->irq_array)
  729. return -ENOMEM;
  730. ret = sunxi_pinctrl_build_state(pdev);
  731. if (ret) {
  732. dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
  733. return ret;
  734. }
  735. pins = devm_kzalloc(&pdev->dev,
  736. pctl->desc->npins * sizeof(*pins),
  737. GFP_KERNEL);
  738. if (!pins)
  739. return -ENOMEM;
  740. for (i = 0; i < pctl->desc->npins; i++)
  741. pins[i] = pctl->desc->pins[i].pin;
  742. pctrl_desc = devm_kzalloc(&pdev->dev,
  743. sizeof(*pctrl_desc),
  744. GFP_KERNEL);
  745. if (!pctrl_desc)
  746. return -ENOMEM;
  747. pctrl_desc->name = dev_name(&pdev->dev);
  748. pctrl_desc->owner = THIS_MODULE;
  749. pctrl_desc->pins = pins;
  750. pctrl_desc->npins = pctl->desc->npins;
  751. pctrl_desc->confops = &sunxi_pconf_ops;
  752. pctrl_desc->pctlops = &sunxi_pctrl_ops;
  753. pctrl_desc->pmxops = &sunxi_pmx_ops;
  754. pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl);
  755. if (IS_ERR(pctl->pctl_dev)) {
  756. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  757. return PTR_ERR(pctl->pctl_dev);
  758. }
  759. pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
  760. if (!pctl->chip)
  761. return -ENOMEM;
  762. last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
  763. pctl->chip->owner = THIS_MODULE;
  764. pctl->chip->request = gpiochip_generic_request,
  765. pctl->chip->free = gpiochip_generic_free,
  766. pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input,
  767. pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output,
  768. pctl->chip->get = sunxi_pinctrl_gpio_get,
  769. pctl->chip->set = sunxi_pinctrl_gpio_set,
  770. pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate,
  771. pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq,
  772. pctl->chip->of_gpio_n_cells = 3,
  773. pctl->chip->can_sleep = false,
  774. pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
  775. pctl->desc->pin_base;
  776. pctl->chip->label = dev_name(&pdev->dev);
  777. pctl->chip->parent = &pdev->dev;
  778. pctl->chip->base = pctl->desc->pin_base;
  779. ret = gpiochip_add_data(pctl->chip, pctl);
  780. if (ret)
  781. return ret;
  782. for (i = 0; i < pctl->desc->npins; i++) {
  783. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  784. ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
  785. pin->pin.number - pctl->desc->pin_base,
  786. pin->pin.number, 1);
  787. if (ret)
  788. goto gpiochip_error;
  789. }
  790. clk = devm_clk_get(&pdev->dev, NULL);
  791. if (IS_ERR(clk)) {
  792. ret = PTR_ERR(clk);
  793. goto gpiochip_error;
  794. }
  795. ret = clk_prepare_enable(clk);
  796. if (ret)
  797. goto gpiochip_error;
  798. pctl->irq = devm_kcalloc(&pdev->dev,
  799. pctl->desc->irq_banks,
  800. sizeof(*pctl->irq),
  801. GFP_KERNEL);
  802. if (!pctl->irq) {
  803. ret = -ENOMEM;
  804. goto clk_error;
  805. }
  806. for (i = 0; i < pctl->desc->irq_banks; i++) {
  807. pctl->irq[i] = platform_get_irq(pdev, i);
  808. if (pctl->irq[i] < 0) {
  809. ret = pctl->irq[i];
  810. goto clk_error;
  811. }
  812. }
  813. pctl->domain = irq_domain_add_linear(node,
  814. pctl->desc->irq_banks * IRQ_PER_BANK,
  815. &sunxi_pinctrl_irq_domain_ops,
  816. pctl);
  817. if (!pctl->domain) {
  818. dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
  819. ret = -ENOMEM;
  820. goto clk_error;
  821. }
  822. for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
  823. int irqno = irq_create_mapping(pctl->domain, i);
  824. irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip,
  825. handle_edge_irq);
  826. irq_set_chip_data(irqno, pctl);
  827. }
  828. for (i = 0; i < pctl->desc->irq_banks; i++) {
  829. /* Mask and clear all IRQs before registering a handler */
  830. writel(0, pctl->membase + sunxi_irq_ctrl_reg_from_bank(i,
  831. pctl->desc->irq_bank_base));
  832. writel(0xffffffff,
  833. pctl->membase + sunxi_irq_status_reg_from_bank(i,
  834. pctl->desc->irq_bank_base));
  835. irq_set_chained_handler_and_data(pctl->irq[i],
  836. sunxi_pinctrl_irq_handler,
  837. pctl);
  838. }
  839. dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
  840. return 0;
  841. clk_error:
  842. clk_disable_unprepare(clk);
  843. gpiochip_error:
  844. gpiochip_remove(pctl->chip);
  845. return ret;
  846. }