pinctrl-sunxi.c 22 KB

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