pinctrl-sunxi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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. u8 mode;
  428. switch (type) {
  429. case IRQ_TYPE_EDGE_RISING:
  430. mode = IRQ_EDGE_RISING;
  431. break;
  432. case IRQ_TYPE_EDGE_FALLING:
  433. mode = IRQ_EDGE_FALLING;
  434. break;
  435. case IRQ_TYPE_EDGE_BOTH:
  436. mode = IRQ_EDGE_BOTH;
  437. break;
  438. case IRQ_TYPE_LEVEL_HIGH:
  439. mode = IRQ_LEVEL_HIGH;
  440. break;
  441. case IRQ_TYPE_LEVEL_LOW:
  442. mode = IRQ_LEVEL_LOW;
  443. break;
  444. default:
  445. return -EINVAL;
  446. }
  447. writel((mode & IRQ_CFG_IRQ_MASK) << index, pctl->membase + reg);
  448. return 0;
  449. }
  450. static void sunxi_pinctrl_irq_mask_ack(struct irq_data *d)
  451. {
  452. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  453. u32 ctrl_reg = sunxi_irq_ctrl_reg(d->hwirq);
  454. u8 ctrl_idx = sunxi_irq_ctrl_offset(d->hwirq);
  455. u32 status_reg = sunxi_irq_status_reg(d->hwirq);
  456. u8 status_idx = sunxi_irq_status_offset(d->hwirq);
  457. u32 val;
  458. /* Mask the IRQ */
  459. val = readl(pctl->membase + ctrl_reg);
  460. writel(val & ~(1 << ctrl_idx), pctl->membase + ctrl_reg);
  461. /* Clear the IRQ */
  462. writel(1 << status_idx, pctl->membase + status_reg);
  463. }
  464. static void sunxi_pinctrl_irq_mask(struct irq_data *d)
  465. {
  466. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  467. u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
  468. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  469. u32 val;
  470. /* Mask the IRQ */
  471. val = readl(pctl->membase + reg);
  472. writel(val & ~(1 << idx), pctl->membase + reg);
  473. }
  474. static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
  475. {
  476. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  477. struct sunxi_desc_function *func;
  478. u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
  479. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  480. u32 val;
  481. func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
  482. pctl->irq_array[d->hwirq],
  483. "irq");
  484. /* Change muxing to INT mode */
  485. sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
  486. /* Unmask the IRQ */
  487. val = readl(pctl->membase + reg);
  488. writel(val | (1 << idx), pctl->membase + reg);
  489. }
  490. static struct irq_chip sunxi_pinctrl_irq_chip = {
  491. .irq_mask = sunxi_pinctrl_irq_mask,
  492. .irq_mask_ack = sunxi_pinctrl_irq_mask_ack,
  493. .irq_unmask = sunxi_pinctrl_irq_unmask,
  494. .irq_set_type = sunxi_pinctrl_irq_set_type,
  495. };
  496. static void sunxi_pinctrl_irq_handler(unsigned irq, struct irq_desc *desc)
  497. {
  498. struct sunxi_pinctrl *pctl = irq_get_handler_data(irq);
  499. const unsigned long reg = readl(pctl->membase + IRQ_STATUS_REG);
  500. /* Clear all interrupts */
  501. writel(reg, pctl->membase + IRQ_STATUS_REG);
  502. if (reg) {
  503. int irqoffset;
  504. for_each_set_bit(irqoffset, &reg, SUNXI_IRQ_NUMBER) {
  505. int pin_irq = irq_find_mapping(pctl->domain, irqoffset);
  506. generic_handle_irq(pin_irq);
  507. }
  508. }
  509. }
  510. static struct of_device_id sunxi_pinctrl_match[] = {
  511. { .compatible = "allwinner,sun4i-a10-pinctrl", .data = (void *)&sun4i_a10_pinctrl_data },
  512. { .compatible = "allwinner,sun5i-a10s-pinctrl", .data = (void *)&sun5i_a10s_pinctrl_data },
  513. { .compatible = "allwinner,sun5i-a13-pinctrl", .data = (void *)&sun5i_a13_pinctrl_data },
  514. {}
  515. };
  516. MODULE_DEVICE_TABLE(of, sunxi_pinctrl_match);
  517. static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
  518. const char *name)
  519. {
  520. struct sunxi_pinctrl_function *func = pctl->functions;
  521. while (func->name) {
  522. /* function already there */
  523. if (strcmp(func->name, name) == 0) {
  524. func->ngroups++;
  525. return -EEXIST;
  526. }
  527. func++;
  528. }
  529. func->name = name;
  530. func->ngroups = 1;
  531. pctl->nfunctions++;
  532. return 0;
  533. }
  534. static int sunxi_pinctrl_build_state(struct platform_device *pdev)
  535. {
  536. struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
  537. int i;
  538. pctl->ngroups = pctl->desc->npins;
  539. /* Allocate groups */
  540. pctl->groups = devm_kzalloc(&pdev->dev,
  541. pctl->ngroups * sizeof(*pctl->groups),
  542. GFP_KERNEL);
  543. if (!pctl->groups)
  544. return -ENOMEM;
  545. for (i = 0; i < pctl->desc->npins; i++) {
  546. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  547. struct sunxi_pinctrl_group *group = pctl->groups + i;
  548. group->name = pin->pin.name;
  549. group->pin = pin->pin.number;
  550. }
  551. /*
  552. * We suppose that we won't have any more functions than pins,
  553. * we'll reallocate that later anyway
  554. */
  555. pctl->functions = devm_kzalloc(&pdev->dev,
  556. pctl->desc->npins * sizeof(*pctl->functions),
  557. GFP_KERNEL);
  558. if (!pctl->functions)
  559. return -ENOMEM;
  560. /* Count functions and their associated groups */
  561. for (i = 0; i < pctl->desc->npins; i++) {
  562. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  563. struct sunxi_desc_function *func = pin->functions;
  564. while (func->name) {
  565. sunxi_pinctrl_add_function(pctl, func->name);
  566. func++;
  567. }
  568. }
  569. pctl->functions = krealloc(pctl->functions,
  570. pctl->nfunctions * sizeof(*pctl->functions),
  571. GFP_KERNEL);
  572. for (i = 0; i < pctl->desc->npins; i++) {
  573. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  574. struct sunxi_desc_function *func = pin->functions;
  575. while (func->name) {
  576. struct sunxi_pinctrl_function *func_item;
  577. const char **func_grp;
  578. func_item = sunxi_pinctrl_find_function_by_name(pctl,
  579. func->name);
  580. if (!func_item)
  581. return -EINVAL;
  582. if (!func_item->groups) {
  583. func_item->groups =
  584. devm_kzalloc(&pdev->dev,
  585. func_item->ngroups * sizeof(*func_item->groups),
  586. GFP_KERNEL);
  587. if (!func_item->groups)
  588. return -ENOMEM;
  589. }
  590. func_grp = func_item->groups;
  591. while (*func_grp)
  592. func_grp++;
  593. *func_grp = pin->pin.name;
  594. func++;
  595. }
  596. }
  597. return 0;
  598. }
  599. static int sunxi_pinctrl_probe(struct platform_device *pdev)
  600. {
  601. struct device_node *node = pdev->dev.of_node;
  602. const struct of_device_id *device;
  603. struct pinctrl_pin_desc *pins;
  604. struct sunxi_pinctrl *pctl;
  605. int i, ret, last_pin;
  606. struct clk *clk;
  607. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  608. if (!pctl)
  609. return -ENOMEM;
  610. platform_set_drvdata(pdev, pctl);
  611. pctl->membase = of_iomap(node, 0);
  612. if (!pctl->membase)
  613. return -ENOMEM;
  614. device = of_match_device(sunxi_pinctrl_match, &pdev->dev);
  615. if (!device)
  616. return -ENODEV;
  617. pctl->desc = (struct sunxi_pinctrl_desc *)device->data;
  618. ret = sunxi_pinctrl_build_state(pdev);
  619. if (ret) {
  620. dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
  621. return ret;
  622. }
  623. pins = devm_kzalloc(&pdev->dev,
  624. pctl->desc->npins * sizeof(*pins),
  625. GFP_KERNEL);
  626. if (!pins)
  627. return -ENOMEM;
  628. for (i = 0; i < pctl->desc->npins; i++)
  629. pins[i] = pctl->desc->pins[i].pin;
  630. sunxi_pctrl_desc.name = dev_name(&pdev->dev);
  631. sunxi_pctrl_desc.owner = THIS_MODULE;
  632. sunxi_pctrl_desc.pins = pins;
  633. sunxi_pctrl_desc.npins = pctl->desc->npins;
  634. pctl->dev = &pdev->dev;
  635. pctl->pctl_dev = pinctrl_register(&sunxi_pctrl_desc,
  636. &pdev->dev, pctl);
  637. if (!pctl->pctl_dev) {
  638. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  639. return -EINVAL;
  640. }
  641. pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
  642. if (!pctl->chip) {
  643. ret = -ENOMEM;
  644. goto pinctrl_error;
  645. }
  646. last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
  647. pctl->chip = &sunxi_pinctrl_gpio_chip;
  648. pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK);
  649. pctl->chip->label = dev_name(&pdev->dev);
  650. pctl->chip->dev = &pdev->dev;
  651. pctl->chip->base = 0;
  652. ret = gpiochip_add(pctl->chip);
  653. if (ret)
  654. goto pinctrl_error;
  655. for (i = 0; i < pctl->desc->npins; i++) {
  656. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  657. ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
  658. pin->pin.number,
  659. pin->pin.number, 1);
  660. if (ret)
  661. goto gpiochip_error;
  662. }
  663. clk = devm_clk_get(&pdev->dev, NULL);
  664. if (IS_ERR(clk)) {
  665. ret = PTR_ERR(clk);
  666. goto gpiochip_error;
  667. }
  668. clk_prepare_enable(clk);
  669. pctl->irq = irq_of_parse_and_map(node, 0);
  670. if (!pctl->irq) {
  671. ret = -EINVAL;
  672. goto gpiochip_error;
  673. }
  674. pctl->domain = irq_domain_add_linear(node, SUNXI_IRQ_NUMBER,
  675. &irq_domain_simple_ops, NULL);
  676. if (!pctl->domain) {
  677. dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
  678. ret = -ENOMEM;
  679. goto gpiochip_error;
  680. }
  681. for (i = 0; i < SUNXI_IRQ_NUMBER; i++) {
  682. int irqno = irq_create_mapping(pctl->domain, i);
  683. irq_set_chip_and_handler(irqno, &sunxi_pinctrl_irq_chip,
  684. handle_simple_irq);
  685. irq_set_chip_data(irqno, pctl);
  686. };
  687. irq_set_chained_handler(pctl->irq, sunxi_pinctrl_irq_handler);
  688. irq_set_handler_data(pctl->irq, pctl);
  689. dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
  690. return 0;
  691. gpiochip_error:
  692. if (gpiochip_remove(pctl->chip))
  693. dev_err(&pdev->dev, "failed to remove gpio chip\n");
  694. pinctrl_error:
  695. pinctrl_unregister(pctl->pctl_dev);
  696. return ret;
  697. }
  698. static struct platform_driver sunxi_pinctrl_driver = {
  699. .probe = sunxi_pinctrl_probe,
  700. .driver = {
  701. .name = "sunxi-pinctrl",
  702. .owner = THIS_MODULE,
  703. .of_match_table = sunxi_pinctrl_match,
  704. },
  705. };
  706. module_platform_driver(sunxi_pinctrl_driver);
  707. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  708. MODULE_DESCRIPTION("Allwinner A1X pinctrl driver");
  709. MODULE_LICENSE("GPL");