pinctrl-sunxi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/pinctrl/consumer.h>
  22. #include <linux/pinctrl/machine.h>
  23. #include <linux/pinctrl/pinctrl.h>
  24. #include <linux/pinctrl/pinconf-generic.h>
  25. #include <linux/pinctrl/pinmux.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include "core.h"
  29. #include "pinctrl-sunxi.h"
  30. #include "pinctrl-sunxi-pins.h"
  31. static struct sunxi_pinctrl_group *
  32. sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
  33. {
  34. int i;
  35. for (i = 0; i < pctl->ngroups; i++) {
  36. struct sunxi_pinctrl_group *grp = pctl->groups + i;
  37. if (!strcmp(grp->name, group))
  38. return grp;
  39. }
  40. return NULL;
  41. }
  42. static struct sunxi_pinctrl_function *
  43. sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
  44. const char *name)
  45. {
  46. struct sunxi_pinctrl_function *func = pctl->functions;
  47. int i;
  48. for (i = 0; i < pctl->nfunctions; i++) {
  49. if (!func[i].name)
  50. break;
  51. if (!strcmp(func[i].name, name))
  52. return func + i;
  53. }
  54. return NULL;
  55. }
  56. static struct sunxi_desc_function *
  57. sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
  58. const char *pin_name,
  59. const char *func_name)
  60. {
  61. int i;
  62. for (i = 0; i < pctl->desc->npins; i++) {
  63. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  64. if (!strcmp(pin->pin.name, pin_name)) {
  65. struct sunxi_desc_function *func = pin->functions;
  66. while (func->name) {
  67. if (!strcmp(func->name, func_name))
  68. return func;
  69. func++;
  70. }
  71. }
  72. }
  73. return NULL;
  74. }
  75. static struct sunxi_desc_function *
  76. sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl,
  77. const u16 pin_num,
  78. const char *func_name)
  79. {
  80. int i;
  81. for (i = 0; i < pctl->desc->npins; i++) {
  82. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  83. if (pin->pin.number == pin_num) {
  84. struct sunxi_desc_function *func = pin->functions;
  85. while (func->name) {
  86. if (!strcmp(func->name, func_name))
  87. return func;
  88. func++;
  89. }
  90. }
  91. }
  92. return NULL;
  93. }
  94. static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
  95. {
  96. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  97. return pctl->ngroups;
  98. }
  99. static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
  100. unsigned group)
  101. {
  102. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  103. return pctl->groups[group].name;
  104. }
  105. static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
  106. unsigned group,
  107. const unsigned **pins,
  108. unsigned *num_pins)
  109. {
  110. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  111. *pins = (unsigned *)&pctl->groups[group].pin;
  112. *num_pins = 1;
  113. return 0;
  114. }
  115. static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  116. struct device_node *node,
  117. struct pinctrl_map **map,
  118. unsigned *num_maps)
  119. {
  120. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  121. unsigned long *pinconfig;
  122. struct property *prop;
  123. const char *function;
  124. const char *group;
  125. int ret, nmaps, i = 0;
  126. u32 val;
  127. *map = NULL;
  128. *num_maps = 0;
  129. ret = of_property_read_string(node, "allwinner,function", &function);
  130. if (ret) {
  131. dev_err(pctl->dev,
  132. "missing allwinner,function property in node %s\n",
  133. node->name);
  134. return -EINVAL;
  135. }
  136. nmaps = of_property_count_strings(node, "allwinner,pins") * 2;
  137. if (nmaps < 0) {
  138. dev_err(pctl->dev,
  139. "missing allwinner,pins property in node %s\n",
  140. node->name);
  141. return -EINVAL;
  142. }
  143. *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
  144. if (!map)
  145. return -ENOMEM;
  146. of_property_for_each_string(node, "allwinner,pins", prop, group) {
  147. struct sunxi_pinctrl_group *grp =
  148. sunxi_pinctrl_find_group_by_name(pctl, group);
  149. int j = 0, configlen = 0;
  150. if (!grp) {
  151. dev_err(pctl->dev, "unknown pin %s", group);
  152. continue;
  153. }
  154. if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
  155. grp->name,
  156. function)) {
  157. dev_err(pctl->dev, "unsupported function %s on pin %s",
  158. function, group);
  159. continue;
  160. }
  161. (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
  162. (*map)[i].data.mux.group = group;
  163. (*map)[i].data.mux.function = function;
  164. i++;
  165. (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
  166. (*map)[i].data.configs.group_or_pin = group;
  167. if (of_find_property(node, "allwinner,drive", NULL))
  168. configlen++;
  169. if (of_find_property(node, "allwinner,pull", NULL))
  170. configlen++;
  171. pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);
  172. if (!of_property_read_u32(node, "allwinner,drive", &val)) {
  173. u16 strength = (val + 1) * 10;
  174. pinconfig[j++] =
  175. pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
  176. strength);
  177. }
  178. if (!of_property_read_u32(node, "allwinner,pull", &val)) {
  179. enum pin_config_param pull = PIN_CONFIG_END;
  180. if (val == 1)
  181. pull = PIN_CONFIG_BIAS_PULL_UP;
  182. else if (val == 2)
  183. pull = PIN_CONFIG_BIAS_PULL_DOWN;
  184. pinconfig[j++] = pinconf_to_config_packed(pull, 0);
  185. }
  186. (*map)[i].data.configs.configs = pinconfig;
  187. (*map)[i].data.configs.num_configs = configlen;
  188. i++;
  189. }
  190. *num_maps = nmaps;
  191. return 0;
  192. }
  193. static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
  194. struct pinctrl_map *map,
  195. unsigned num_maps)
  196. {
  197. int i;
  198. for (i = 0; i < num_maps; i++) {
  199. if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
  200. kfree(map[i].data.configs.configs);
  201. }
  202. kfree(map);
  203. }
  204. static const struct pinctrl_ops sunxi_pctrl_ops = {
  205. .dt_node_to_map = sunxi_pctrl_dt_node_to_map,
  206. .dt_free_map = sunxi_pctrl_dt_free_map,
  207. .get_groups_count = sunxi_pctrl_get_groups_count,
  208. .get_group_name = sunxi_pctrl_get_group_name,
  209. .get_group_pins = sunxi_pctrl_get_group_pins,
  210. };
  211. static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
  212. unsigned group,
  213. unsigned long *config)
  214. {
  215. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  216. *config = pctl->groups[group].config;
  217. return 0;
  218. }
  219. static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev,
  220. unsigned group,
  221. unsigned long config)
  222. {
  223. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  224. struct sunxi_pinctrl_group *g = &pctl->groups[group];
  225. u32 val, mask;
  226. u16 strength;
  227. u8 dlevel;
  228. switch (pinconf_to_config_param(config)) {
  229. case PIN_CONFIG_DRIVE_STRENGTH:
  230. strength = pinconf_to_config_argument(config);
  231. if (strength > 40)
  232. return -EINVAL;
  233. /*
  234. * We convert from mA to what the register expects:
  235. * 0: 10mA
  236. * 1: 20mA
  237. * 2: 30mA
  238. * 3: 40mA
  239. */
  240. dlevel = strength / 10 - 1;
  241. val = readl(pctl->membase + sunxi_dlevel_reg(g->pin));
  242. mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(g->pin);
  243. writel((val & ~mask) | dlevel << sunxi_dlevel_offset(g->pin),
  244. pctl->membase + sunxi_dlevel_reg(g->pin));
  245. break;
  246. case PIN_CONFIG_BIAS_PULL_UP:
  247. val = readl(pctl->membase + sunxi_pull_reg(g->pin));
  248. mask = PULL_PINS_MASK << sunxi_pull_offset(g->pin);
  249. writel((val & ~mask) | 1 << sunxi_pull_offset(g->pin),
  250. pctl->membase + sunxi_pull_reg(g->pin));
  251. break;
  252. case PIN_CONFIG_BIAS_PULL_DOWN:
  253. val = readl(pctl->membase + sunxi_pull_reg(g->pin));
  254. mask = PULL_PINS_MASK << sunxi_pull_offset(g->pin);
  255. writel((val & ~mask) | 2 << sunxi_pull_offset(g->pin),
  256. pctl->membase + sunxi_pull_reg(g->pin));
  257. break;
  258. default:
  259. break;
  260. }
  261. /* cache the config value */
  262. g->config = config;
  263. return 0;
  264. }
  265. static const struct pinconf_ops sunxi_pconf_ops = {
  266. .pin_config_group_get = sunxi_pconf_group_get,
  267. .pin_config_group_set = sunxi_pconf_group_set,
  268. };
  269. static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  270. {
  271. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  272. return pctl->nfunctions;
  273. }
  274. static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
  275. unsigned function)
  276. {
  277. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  278. return pctl->functions[function].name;
  279. }
  280. static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  281. unsigned function,
  282. const char * const **groups,
  283. unsigned * const num_groups)
  284. {
  285. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  286. *groups = pctl->functions[function].groups;
  287. *num_groups = pctl->functions[function].ngroups;
  288. return 0;
  289. }
  290. static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
  291. unsigned pin,
  292. u8 config)
  293. {
  294. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  295. u32 val = readl(pctl->membase + sunxi_mux_reg(pin));
  296. u32 mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
  297. writel((val & ~mask) | config << sunxi_mux_offset(pin),
  298. pctl->membase + sunxi_mux_reg(pin));
  299. }
  300. static int sunxi_pmx_enable(struct pinctrl_dev *pctldev,
  301. unsigned function,
  302. unsigned group)
  303. {
  304. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  305. struct sunxi_pinctrl_group *g = pctl->groups + group;
  306. struct sunxi_pinctrl_function *func = pctl->functions + function;
  307. struct sunxi_desc_function *desc =
  308. sunxi_pinctrl_desc_find_function_by_name(pctl,
  309. g->name,
  310. func->name);
  311. if (!desc)
  312. return -EINVAL;
  313. sunxi_pmx_set(pctldev, g->pin, desc->muxval);
  314. return 0;
  315. }
  316. static int
  317. sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  318. struct pinctrl_gpio_range *range,
  319. unsigned offset,
  320. bool input)
  321. {
  322. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  323. struct sunxi_desc_function *desc;
  324. const char *func;
  325. if (input)
  326. func = "gpio_in";
  327. else
  328. func = "gpio_out";
  329. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
  330. if (!desc)
  331. return -EINVAL;
  332. sunxi_pmx_set(pctldev, offset, desc->muxval);
  333. return 0;
  334. }
  335. static const struct pinmux_ops sunxi_pmx_ops = {
  336. .get_functions_count = sunxi_pmx_get_funcs_cnt,
  337. .get_function_name = sunxi_pmx_get_func_name,
  338. .get_function_groups = sunxi_pmx_get_func_groups,
  339. .enable = sunxi_pmx_enable,
  340. .gpio_set_direction = sunxi_pmx_gpio_set_direction,
  341. };
  342. static struct pinctrl_desc sunxi_pctrl_desc = {
  343. .confops = &sunxi_pconf_ops,
  344. .pctlops = &sunxi_pctrl_ops,
  345. .pmxops = &sunxi_pmx_ops,
  346. };
  347. static int sunxi_pinctrl_gpio_request(struct gpio_chip *chip, unsigned offset)
  348. {
  349. return pinctrl_request_gpio(chip->base + offset);
  350. }
  351. static void sunxi_pinctrl_gpio_free(struct gpio_chip *chip, unsigned offset)
  352. {
  353. pinctrl_free_gpio(chip->base + offset);
  354. }
  355. static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
  356. unsigned offset)
  357. {
  358. return pinctrl_gpio_direction_input(chip->base + offset);
  359. }
  360. static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
  361. {
  362. struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
  363. u32 reg = sunxi_data_reg(offset);
  364. u8 index = sunxi_data_offset(offset);
  365. u32 val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
  366. return val;
  367. }
  368. static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
  369. unsigned offset, int value)
  370. {
  371. return pinctrl_gpio_direction_output(chip->base + offset);
  372. }
  373. static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
  374. unsigned offset, int value)
  375. {
  376. struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
  377. u32 reg = sunxi_data_reg(offset);
  378. u8 index = sunxi_data_offset(offset);
  379. writel((value & DATA_PINS_MASK) << index, pctl->membase + reg);
  380. }
  381. static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
  382. const struct of_phandle_args *gpiospec,
  383. u32 *flags)
  384. {
  385. int pin, base;
  386. base = PINS_PER_BANK * gpiospec->args[0];
  387. pin = base + gpiospec->args[1];
  388. if (pin > (gc->base + gc->ngpio))
  389. return -EINVAL;
  390. if (flags)
  391. *flags = gpiospec->args[2];
  392. return pin;
  393. }
  394. static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  395. {
  396. struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
  397. struct sunxi_desc_function *desc;
  398. if (offset > chip->ngpio)
  399. return -ENXIO;
  400. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, "irq");
  401. if (!desc)
  402. return -EINVAL;
  403. pctl->irq_array[desc->irqnum] = offset;
  404. dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
  405. chip->label, offset + chip->base, desc->irqnum);
  406. return irq_find_mapping(pctl->domain, desc->irqnum);
  407. }
  408. static struct gpio_chip sunxi_pinctrl_gpio_chip = {
  409. .owner = THIS_MODULE,
  410. .request = sunxi_pinctrl_gpio_request,
  411. .free = sunxi_pinctrl_gpio_free,
  412. .direction_input = sunxi_pinctrl_gpio_direction_input,
  413. .direction_output = sunxi_pinctrl_gpio_direction_output,
  414. .get = sunxi_pinctrl_gpio_get,
  415. .set = sunxi_pinctrl_gpio_set,
  416. .of_xlate = sunxi_pinctrl_gpio_of_xlate,
  417. .to_irq = sunxi_pinctrl_gpio_to_irq,
  418. .of_gpio_n_cells = 3,
  419. .can_sleep = 0,
  420. };
  421. static int sunxi_pinctrl_irq_set_type(struct irq_data *d,
  422. unsigned int type)
  423. {
  424. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  425. u32 reg = sunxi_irq_cfg_reg(d->hwirq);
  426. u8 index = sunxi_irq_cfg_offset(d->hwirq);
  427. u32 regval;
  428. u8 mode;
  429. switch (type) {
  430. case IRQ_TYPE_EDGE_RISING:
  431. mode = IRQ_EDGE_RISING;
  432. break;
  433. case IRQ_TYPE_EDGE_FALLING:
  434. mode = IRQ_EDGE_FALLING;
  435. break;
  436. case IRQ_TYPE_EDGE_BOTH:
  437. mode = IRQ_EDGE_BOTH;
  438. break;
  439. case IRQ_TYPE_LEVEL_HIGH:
  440. mode = IRQ_LEVEL_HIGH;
  441. break;
  442. case IRQ_TYPE_LEVEL_LOW:
  443. mode = IRQ_LEVEL_LOW;
  444. break;
  445. default:
  446. return -EINVAL;
  447. }
  448. regval = readl(pctl->membase + reg);
  449. regval &= ~IRQ_CFG_IRQ_MASK;
  450. writel(regval | (mode << index), pctl->membase + reg);
  451. return 0;
  452. }
  453. static void sunxi_pinctrl_irq_mask_ack(struct irq_data *d)
  454. {
  455. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  456. u32 ctrl_reg = sunxi_irq_ctrl_reg(d->hwirq);
  457. u8 ctrl_idx = sunxi_irq_ctrl_offset(d->hwirq);
  458. u32 status_reg = sunxi_irq_status_reg(d->hwirq);
  459. u8 status_idx = sunxi_irq_status_offset(d->hwirq);
  460. u32 val;
  461. /* Mask the IRQ */
  462. val = readl(pctl->membase + ctrl_reg);
  463. writel(val & ~(1 << ctrl_idx), pctl->membase + ctrl_reg);
  464. /* Clear the IRQ */
  465. writel(1 << status_idx, pctl->membase + status_reg);
  466. }
  467. static void sunxi_pinctrl_irq_mask(struct irq_data *d)
  468. {
  469. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  470. u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
  471. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  472. u32 val;
  473. /* Mask the IRQ */
  474. val = readl(pctl->membase + reg);
  475. writel(val & ~(1 << idx), pctl->membase + reg);
  476. }
  477. static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
  478. {
  479. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  480. struct sunxi_desc_function *func;
  481. u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
  482. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  483. u32 val;
  484. func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
  485. pctl->irq_array[d->hwirq],
  486. "irq");
  487. /* Change muxing to INT mode */
  488. sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
  489. /* Unmask the IRQ */
  490. val = readl(pctl->membase + reg);
  491. writel(val | (1 << idx), pctl->membase + reg);
  492. }
  493. static struct irq_chip sunxi_pinctrl_irq_chip = {
  494. .irq_mask = sunxi_pinctrl_irq_mask,
  495. .irq_mask_ack = sunxi_pinctrl_irq_mask_ack,
  496. .irq_unmask = sunxi_pinctrl_irq_unmask,
  497. .irq_set_type = sunxi_pinctrl_irq_set_type,
  498. };
  499. static void sunxi_pinctrl_irq_handler(unsigned irq, struct irq_desc *desc)
  500. {
  501. struct sunxi_pinctrl *pctl = irq_get_handler_data(irq);
  502. const unsigned long reg = readl(pctl->membase + IRQ_STATUS_REG);
  503. /* Clear all interrupts */
  504. writel(reg, pctl->membase + IRQ_STATUS_REG);
  505. if (reg) {
  506. int irqoffset;
  507. for_each_set_bit(irqoffset, &reg, SUNXI_IRQ_NUMBER) {
  508. int pin_irq = irq_find_mapping(pctl->domain, irqoffset);
  509. generic_handle_irq(pin_irq);
  510. }
  511. }
  512. }
  513. static struct of_device_id sunxi_pinctrl_match[] = {
  514. { .compatible = "allwinner,sun4i-a10-pinctrl", .data = (void *)&sun4i_a10_pinctrl_data },
  515. { .compatible = "allwinner,sun5i-a10s-pinctrl", .data = (void *)&sun5i_a10s_pinctrl_data },
  516. { .compatible = "allwinner,sun5i-a13-pinctrl", .data = (void *)&sun5i_a13_pinctrl_data },
  517. {}
  518. };
  519. MODULE_DEVICE_TABLE(of, sunxi_pinctrl_match);
  520. static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
  521. const char *name)
  522. {
  523. struct sunxi_pinctrl_function *func = pctl->functions;
  524. while (func->name) {
  525. /* function already there */
  526. if (strcmp(func->name, name) == 0) {
  527. func->ngroups++;
  528. return -EEXIST;
  529. }
  530. func++;
  531. }
  532. func->name = name;
  533. func->ngroups = 1;
  534. pctl->nfunctions++;
  535. return 0;
  536. }
  537. static int sunxi_pinctrl_build_state(struct platform_device *pdev)
  538. {
  539. struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
  540. int i;
  541. pctl->ngroups = pctl->desc->npins;
  542. /* Allocate groups */
  543. pctl->groups = devm_kzalloc(&pdev->dev,
  544. pctl->ngroups * sizeof(*pctl->groups),
  545. GFP_KERNEL);
  546. if (!pctl->groups)
  547. return -ENOMEM;
  548. for (i = 0; i < pctl->desc->npins; i++) {
  549. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  550. struct sunxi_pinctrl_group *group = pctl->groups + i;
  551. group->name = pin->pin.name;
  552. group->pin = pin->pin.number;
  553. }
  554. /*
  555. * We suppose that we won't have any more functions than pins,
  556. * we'll reallocate that later anyway
  557. */
  558. pctl->functions = devm_kzalloc(&pdev->dev,
  559. pctl->desc->npins * sizeof(*pctl->functions),
  560. GFP_KERNEL);
  561. if (!pctl->functions)
  562. return -ENOMEM;
  563. /* Count functions and their associated groups */
  564. for (i = 0; i < pctl->desc->npins; i++) {
  565. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  566. struct sunxi_desc_function *func = pin->functions;
  567. while (func->name) {
  568. sunxi_pinctrl_add_function(pctl, func->name);
  569. func++;
  570. }
  571. }
  572. pctl->functions = krealloc(pctl->functions,
  573. pctl->nfunctions * sizeof(*pctl->functions),
  574. GFP_KERNEL);
  575. for (i = 0; i < pctl->desc->npins; i++) {
  576. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  577. struct sunxi_desc_function *func = pin->functions;
  578. while (func->name) {
  579. struct sunxi_pinctrl_function *func_item;
  580. const char **func_grp;
  581. func_item = sunxi_pinctrl_find_function_by_name(pctl,
  582. func->name);
  583. if (!func_item)
  584. return -EINVAL;
  585. if (!func_item->groups) {
  586. func_item->groups =
  587. devm_kzalloc(&pdev->dev,
  588. func_item->ngroups * sizeof(*func_item->groups),
  589. GFP_KERNEL);
  590. if (!func_item->groups)
  591. return -ENOMEM;
  592. }
  593. func_grp = func_item->groups;
  594. while (*func_grp)
  595. func_grp++;
  596. *func_grp = pin->pin.name;
  597. func++;
  598. }
  599. }
  600. return 0;
  601. }
  602. static int sunxi_pinctrl_probe(struct platform_device *pdev)
  603. {
  604. struct device_node *node = pdev->dev.of_node;
  605. const struct of_device_id *device;
  606. struct pinctrl_pin_desc *pins;
  607. struct sunxi_pinctrl *pctl;
  608. int i, ret, last_pin;
  609. struct clk *clk;
  610. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  611. if (!pctl)
  612. return -ENOMEM;
  613. platform_set_drvdata(pdev, pctl);
  614. pctl->membase = of_iomap(node, 0);
  615. if (!pctl->membase)
  616. return -ENOMEM;
  617. device = of_match_device(sunxi_pinctrl_match, &pdev->dev);
  618. if (!device)
  619. return -ENODEV;
  620. pctl->desc = (struct sunxi_pinctrl_desc *)device->data;
  621. ret = sunxi_pinctrl_build_state(pdev);
  622. if (ret) {
  623. dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
  624. return ret;
  625. }
  626. pins = devm_kzalloc(&pdev->dev,
  627. pctl->desc->npins * sizeof(*pins),
  628. GFP_KERNEL);
  629. if (!pins)
  630. return -ENOMEM;
  631. for (i = 0; i < pctl->desc->npins; i++)
  632. pins[i] = pctl->desc->pins[i].pin;
  633. sunxi_pctrl_desc.name = dev_name(&pdev->dev);
  634. sunxi_pctrl_desc.owner = THIS_MODULE;
  635. sunxi_pctrl_desc.pins = pins;
  636. sunxi_pctrl_desc.npins = pctl->desc->npins;
  637. pctl->dev = &pdev->dev;
  638. pctl->pctl_dev = pinctrl_register(&sunxi_pctrl_desc,
  639. &pdev->dev, pctl);
  640. if (!pctl->pctl_dev) {
  641. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  642. return -EINVAL;
  643. }
  644. pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
  645. if (!pctl->chip) {
  646. ret = -ENOMEM;
  647. goto pinctrl_error;
  648. }
  649. last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
  650. pctl->chip = &sunxi_pinctrl_gpio_chip;
  651. pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK);
  652. pctl->chip->label = dev_name(&pdev->dev);
  653. pctl->chip->dev = &pdev->dev;
  654. pctl->chip->base = 0;
  655. ret = gpiochip_add(pctl->chip);
  656. if (ret)
  657. goto pinctrl_error;
  658. for (i = 0; i < pctl->desc->npins; i++) {
  659. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  660. ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
  661. pin->pin.number,
  662. pin->pin.number, 1);
  663. if (ret)
  664. goto gpiochip_error;
  665. }
  666. clk = devm_clk_get(&pdev->dev, NULL);
  667. if (IS_ERR(clk)) {
  668. ret = PTR_ERR(clk);
  669. goto gpiochip_error;
  670. }
  671. clk_prepare_enable(clk);
  672. pctl->irq = irq_of_parse_and_map(node, 0);
  673. if (!pctl->irq) {
  674. ret = -EINVAL;
  675. goto gpiochip_error;
  676. }
  677. pctl->domain = irq_domain_add_linear(node, SUNXI_IRQ_NUMBER,
  678. &irq_domain_simple_ops, NULL);
  679. if (!pctl->domain) {
  680. dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
  681. ret = -ENOMEM;
  682. goto gpiochip_error;
  683. }
  684. for (i = 0; i < SUNXI_IRQ_NUMBER; i++) {
  685. int irqno = irq_create_mapping(pctl->domain, i);
  686. irq_set_chip_and_handler(irqno, &sunxi_pinctrl_irq_chip,
  687. handle_simple_irq);
  688. irq_set_chip_data(irqno, pctl);
  689. };
  690. irq_set_chained_handler(pctl->irq, sunxi_pinctrl_irq_handler);
  691. irq_set_handler_data(pctl->irq, pctl);
  692. dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
  693. return 0;
  694. gpiochip_error:
  695. if (gpiochip_remove(pctl->chip))
  696. dev_err(&pdev->dev, "failed to remove gpio chip\n");
  697. pinctrl_error:
  698. pinctrl_unregister(pctl->pctl_dev);
  699. return ret;
  700. }
  701. static struct platform_driver sunxi_pinctrl_driver = {
  702. .probe = sunxi_pinctrl_probe,
  703. .driver = {
  704. .name = "sunxi-pinctrl",
  705. .owner = THIS_MODULE,
  706. .of_match_table = sunxi_pinctrl_match,
  707. },
  708. };
  709. module_platform_driver(sunxi_pinctrl_driver);
  710. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  711. MODULE_DESCRIPTION("Allwinner A1X pinctrl driver");
  712. MODULE_LICENSE("GPL");