pinctrl-sunxi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. unsigned long flags;
  226. u32 val, mask;
  227. u16 strength;
  228. u8 dlevel;
  229. switch (pinconf_to_config_param(config)) {
  230. case PIN_CONFIG_DRIVE_STRENGTH:
  231. strength = pinconf_to_config_argument(config);
  232. if (strength > 40)
  233. return -EINVAL;
  234. /*
  235. * We convert from mA to what the register expects:
  236. * 0: 10mA
  237. * 1: 20mA
  238. * 2: 30mA
  239. * 3: 40mA
  240. */
  241. dlevel = strength / 10 - 1;
  242. spin_lock_irqsave(&pctl->lock, flags);
  243. val = readl(pctl->membase + sunxi_dlevel_reg(g->pin));
  244. mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(g->pin);
  245. writel((val & ~mask) | dlevel << sunxi_dlevel_offset(g->pin),
  246. pctl->membase + sunxi_dlevel_reg(g->pin));
  247. spin_unlock_irqrestore(&pctl->lock, flags);
  248. break;
  249. case PIN_CONFIG_BIAS_PULL_UP:
  250. spin_lock_irqsave(&pctl->lock, flags);
  251. val = readl(pctl->membase + sunxi_pull_reg(g->pin));
  252. mask = PULL_PINS_MASK << sunxi_pull_offset(g->pin);
  253. writel((val & ~mask) | 1 << sunxi_pull_offset(g->pin),
  254. pctl->membase + sunxi_pull_reg(g->pin));
  255. spin_unlock_irqrestore(&pctl->lock, flags);
  256. break;
  257. case PIN_CONFIG_BIAS_PULL_DOWN:
  258. spin_lock_irqsave(&pctl->lock, flags);
  259. val = readl(pctl->membase + sunxi_pull_reg(g->pin));
  260. mask = PULL_PINS_MASK << sunxi_pull_offset(g->pin);
  261. writel((val & ~mask) | 2 << sunxi_pull_offset(g->pin),
  262. pctl->membase + sunxi_pull_reg(g->pin));
  263. spin_unlock_irqrestore(&pctl->lock, flags);
  264. break;
  265. default:
  266. break;
  267. }
  268. /* cache the config value */
  269. g->config = config;
  270. return 0;
  271. }
  272. static const struct pinconf_ops sunxi_pconf_ops = {
  273. .pin_config_group_get = sunxi_pconf_group_get,
  274. .pin_config_group_set = sunxi_pconf_group_set,
  275. };
  276. static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  277. {
  278. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  279. return pctl->nfunctions;
  280. }
  281. static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
  282. unsigned function)
  283. {
  284. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  285. return pctl->functions[function].name;
  286. }
  287. static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  288. unsigned function,
  289. const char * const **groups,
  290. unsigned * const num_groups)
  291. {
  292. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  293. *groups = pctl->functions[function].groups;
  294. *num_groups = pctl->functions[function].ngroups;
  295. return 0;
  296. }
  297. static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
  298. unsigned pin,
  299. u8 config)
  300. {
  301. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  302. unsigned long flags;
  303. u32 val, mask;
  304. spin_lock_irqsave(&pctl->lock, flags);
  305. val = readl(pctl->membase + sunxi_mux_reg(pin));
  306. mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
  307. writel((val & ~mask) | config << sunxi_mux_offset(pin),
  308. pctl->membase + sunxi_mux_reg(pin));
  309. spin_unlock_irqrestore(&pctl->lock, flags);
  310. }
  311. static int sunxi_pmx_enable(struct pinctrl_dev *pctldev,
  312. unsigned function,
  313. unsigned group)
  314. {
  315. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  316. struct sunxi_pinctrl_group *g = pctl->groups + group;
  317. struct sunxi_pinctrl_function *func = pctl->functions + function;
  318. struct sunxi_desc_function *desc =
  319. sunxi_pinctrl_desc_find_function_by_name(pctl,
  320. g->name,
  321. func->name);
  322. if (!desc)
  323. return -EINVAL;
  324. sunxi_pmx_set(pctldev, g->pin, desc->muxval);
  325. return 0;
  326. }
  327. static int
  328. sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  329. struct pinctrl_gpio_range *range,
  330. unsigned offset,
  331. bool input)
  332. {
  333. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  334. struct sunxi_desc_function *desc;
  335. const char *func;
  336. if (input)
  337. func = "gpio_in";
  338. else
  339. func = "gpio_out";
  340. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
  341. if (!desc)
  342. return -EINVAL;
  343. sunxi_pmx_set(pctldev, offset, desc->muxval);
  344. return 0;
  345. }
  346. static const struct pinmux_ops sunxi_pmx_ops = {
  347. .get_functions_count = sunxi_pmx_get_funcs_cnt,
  348. .get_function_name = sunxi_pmx_get_func_name,
  349. .get_function_groups = sunxi_pmx_get_func_groups,
  350. .enable = sunxi_pmx_enable,
  351. .gpio_set_direction = sunxi_pmx_gpio_set_direction,
  352. };
  353. static struct pinctrl_desc sunxi_pctrl_desc = {
  354. .confops = &sunxi_pconf_ops,
  355. .pctlops = &sunxi_pctrl_ops,
  356. .pmxops = &sunxi_pmx_ops,
  357. };
  358. static int sunxi_pinctrl_gpio_request(struct gpio_chip *chip, unsigned offset)
  359. {
  360. return pinctrl_request_gpio(chip->base + offset);
  361. }
  362. static void sunxi_pinctrl_gpio_free(struct gpio_chip *chip, unsigned offset)
  363. {
  364. pinctrl_free_gpio(chip->base + offset);
  365. }
  366. static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
  367. unsigned offset)
  368. {
  369. return pinctrl_gpio_direction_input(chip->base + offset);
  370. }
  371. static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
  372. {
  373. struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
  374. u32 reg = sunxi_data_reg(offset);
  375. u8 index = sunxi_data_offset(offset);
  376. u32 val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
  377. return val;
  378. }
  379. static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
  380. unsigned offset, int value)
  381. {
  382. return pinctrl_gpio_direction_output(chip->base + offset);
  383. }
  384. static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
  385. unsigned offset, int value)
  386. {
  387. struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
  388. u32 reg = sunxi_data_reg(offset);
  389. u8 index = sunxi_data_offset(offset);
  390. unsigned long flags;
  391. u32 regval;
  392. spin_lock_irqsave(&pctl->lock, flags);
  393. regval = readl(pctl->membase + reg);
  394. if (value)
  395. regval |= BIT(index);
  396. else
  397. regval &= ~(BIT(index));
  398. writel(regval, pctl->membase + reg);
  399. spin_unlock_irqrestore(&pctl->lock, flags);
  400. }
  401. static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
  402. const struct of_phandle_args *gpiospec,
  403. u32 *flags)
  404. {
  405. int pin, base;
  406. base = PINS_PER_BANK * gpiospec->args[0];
  407. pin = base + gpiospec->args[1];
  408. if (pin > (gc->base + gc->ngpio))
  409. return -EINVAL;
  410. if (flags)
  411. *flags = gpiospec->args[2];
  412. return pin;
  413. }
  414. static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  415. {
  416. struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
  417. struct sunxi_desc_function *desc;
  418. if (offset > chip->ngpio)
  419. return -ENXIO;
  420. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, "irq");
  421. if (!desc)
  422. return -EINVAL;
  423. pctl->irq_array[desc->irqnum] = offset;
  424. dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
  425. chip->label, offset + chip->base, desc->irqnum);
  426. return irq_find_mapping(pctl->domain, desc->irqnum);
  427. }
  428. static struct gpio_chip sunxi_pinctrl_gpio_chip = {
  429. .owner = THIS_MODULE,
  430. .request = sunxi_pinctrl_gpio_request,
  431. .free = sunxi_pinctrl_gpio_free,
  432. .direction_input = sunxi_pinctrl_gpio_direction_input,
  433. .direction_output = sunxi_pinctrl_gpio_direction_output,
  434. .get = sunxi_pinctrl_gpio_get,
  435. .set = sunxi_pinctrl_gpio_set,
  436. .of_xlate = sunxi_pinctrl_gpio_of_xlate,
  437. .to_irq = sunxi_pinctrl_gpio_to_irq,
  438. .of_gpio_n_cells = 3,
  439. .can_sleep = 0,
  440. };
  441. static int sunxi_pinctrl_irq_set_type(struct irq_data *d,
  442. unsigned int type)
  443. {
  444. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  445. u32 reg = sunxi_irq_cfg_reg(d->hwirq);
  446. u8 index = sunxi_irq_cfg_offset(d->hwirq);
  447. unsigned long flags;
  448. u32 regval;
  449. u8 mode;
  450. switch (type) {
  451. case IRQ_TYPE_EDGE_RISING:
  452. mode = IRQ_EDGE_RISING;
  453. break;
  454. case IRQ_TYPE_EDGE_FALLING:
  455. mode = IRQ_EDGE_FALLING;
  456. break;
  457. case IRQ_TYPE_EDGE_BOTH:
  458. mode = IRQ_EDGE_BOTH;
  459. break;
  460. case IRQ_TYPE_LEVEL_HIGH:
  461. mode = IRQ_LEVEL_HIGH;
  462. break;
  463. case IRQ_TYPE_LEVEL_LOW:
  464. mode = IRQ_LEVEL_LOW;
  465. break;
  466. default:
  467. return -EINVAL;
  468. }
  469. spin_lock_irqsave(&pctl->lock, flags);
  470. regval = readl(pctl->membase + reg);
  471. regval &= ~IRQ_CFG_IRQ_MASK;
  472. writel(regval | (mode << index), pctl->membase + reg);
  473. spin_unlock_irqrestore(&pctl->lock, flags);
  474. return 0;
  475. }
  476. static void sunxi_pinctrl_irq_mask_ack(struct irq_data *d)
  477. {
  478. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  479. u32 ctrl_reg = sunxi_irq_ctrl_reg(d->hwirq);
  480. u8 ctrl_idx = sunxi_irq_ctrl_offset(d->hwirq);
  481. u32 status_reg = sunxi_irq_status_reg(d->hwirq);
  482. u8 status_idx = sunxi_irq_status_offset(d->hwirq);
  483. unsigned long flags;
  484. u32 val;
  485. spin_lock_irqsave(&pctl->lock, flags);
  486. /* Mask the IRQ */
  487. val = readl(pctl->membase + ctrl_reg);
  488. writel(val & ~(1 << ctrl_idx), pctl->membase + ctrl_reg);
  489. /* Clear the IRQ */
  490. writel(1 << status_idx, pctl->membase + status_reg);
  491. spin_unlock_irqrestore(&pctl->lock, flags);
  492. }
  493. static void sunxi_pinctrl_irq_mask(struct irq_data *d)
  494. {
  495. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  496. u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
  497. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  498. unsigned long flags;
  499. u32 val;
  500. spin_lock_irqsave(&pctl->lock, flags);
  501. /* Mask the IRQ */
  502. val = readl(pctl->membase + reg);
  503. writel(val & ~(1 << idx), pctl->membase + reg);
  504. spin_unlock_irqrestore(&pctl->lock, flags);
  505. }
  506. static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
  507. {
  508. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  509. struct sunxi_desc_function *func;
  510. u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
  511. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  512. unsigned long flags;
  513. u32 val;
  514. func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
  515. pctl->irq_array[d->hwirq],
  516. "irq");
  517. /* Change muxing to INT mode */
  518. sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
  519. spin_lock_irqsave(&pctl->lock, flags);
  520. /* Unmask 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 struct irq_chip sunxi_pinctrl_irq_chip = {
  526. .irq_mask = sunxi_pinctrl_irq_mask,
  527. .irq_mask_ack = sunxi_pinctrl_irq_mask_ack,
  528. .irq_unmask = sunxi_pinctrl_irq_unmask,
  529. .irq_set_type = sunxi_pinctrl_irq_set_type,
  530. };
  531. static void sunxi_pinctrl_irq_handler(unsigned irq, struct irq_desc *desc)
  532. {
  533. struct sunxi_pinctrl *pctl = irq_get_handler_data(irq);
  534. const unsigned long reg = readl(pctl->membase + IRQ_STATUS_REG);
  535. /* Clear all interrupts */
  536. writel(reg, pctl->membase + IRQ_STATUS_REG);
  537. if (reg) {
  538. int irqoffset;
  539. for_each_set_bit(irqoffset, &reg, SUNXI_IRQ_NUMBER) {
  540. int pin_irq = irq_find_mapping(pctl->domain, irqoffset);
  541. generic_handle_irq(pin_irq);
  542. }
  543. }
  544. }
  545. static struct of_device_id sunxi_pinctrl_match[] = {
  546. { .compatible = "allwinner,sun4i-a10-pinctrl", .data = (void *)&sun4i_a10_pinctrl_data },
  547. { .compatible = "allwinner,sun5i-a10s-pinctrl", .data = (void *)&sun5i_a10s_pinctrl_data },
  548. { .compatible = "allwinner,sun5i-a13-pinctrl", .data = (void *)&sun5i_a13_pinctrl_data },
  549. {}
  550. };
  551. MODULE_DEVICE_TABLE(of, sunxi_pinctrl_match);
  552. static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
  553. const char *name)
  554. {
  555. struct sunxi_pinctrl_function *func = pctl->functions;
  556. while (func->name) {
  557. /* function already there */
  558. if (strcmp(func->name, name) == 0) {
  559. func->ngroups++;
  560. return -EEXIST;
  561. }
  562. func++;
  563. }
  564. func->name = name;
  565. func->ngroups = 1;
  566. pctl->nfunctions++;
  567. return 0;
  568. }
  569. static int sunxi_pinctrl_build_state(struct platform_device *pdev)
  570. {
  571. struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
  572. int i;
  573. pctl->ngroups = pctl->desc->npins;
  574. /* Allocate groups */
  575. pctl->groups = devm_kzalloc(&pdev->dev,
  576. pctl->ngroups * sizeof(*pctl->groups),
  577. GFP_KERNEL);
  578. if (!pctl->groups)
  579. return -ENOMEM;
  580. for (i = 0; i < pctl->desc->npins; i++) {
  581. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  582. struct sunxi_pinctrl_group *group = pctl->groups + i;
  583. group->name = pin->pin.name;
  584. group->pin = pin->pin.number;
  585. }
  586. /*
  587. * We suppose that we won't have any more functions than pins,
  588. * we'll reallocate that later anyway
  589. */
  590. pctl->functions = devm_kzalloc(&pdev->dev,
  591. pctl->desc->npins * sizeof(*pctl->functions),
  592. GFP_KERNEL);
  593. if (!pctl->functions)
  594. return -ENOMEM;
  595. /* Count functions and their associated groups */
  596. for (i = 0; i < pctl->desc->npins; i++) {
  597. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  598. struct sunxi_desc_function *func = pin->functions;
  599. while (func->name) {
  600. sunxi_pinctrl_add_function(pctl, func->name);
  601. func++;
  602. }
  603. }
  604. pctl->functions = krealloc(pctl->functions,
  605. pctl->nfunctions * sizeof(*pctl->functions),
  606. GFP_KERNEL);
  607. for (i = 0; i < pctl->desc->npins; i++) {
  608. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  609. struct sunxi_desc_function *func = pin->functions;
  610. while (func->name) {
  611. struct sunxi_pinctrl_function *func_item;
  612. const char **func_grp;
  613. func_item = sunxi_pinctrl_find_function_by_name(pctl,
  614. func->name);
  615. if (!func_item)
  616. return -EINVAL;
  617. if (!func_item->groups) {
  618. func_item->groups =
  619. devm_kzalloc(&pdev->dev,
  620. func_item->ngroups * sizeof(*func_item->groups),
  621. GFP_KERNEL);
  622. if (!func_item->groups)
  623. return -ENOMEM;
  624. }
  625. func_grp = func_item->groups;
  626. while (*func_grp)
  627. func_grp++;
  628. *func_grp = pin->pin.name;
  629. func++;
  630. }
  631. }
  632. return 0;
  633. }
  634. static int sunxi_pinctrl_probe(struct platform_device *pdev)
  635. {
  636. struct device_node *node = pdev->dev.of_node;
  637. const struct of_device_id *device;
  638. struct pinctrl_pin_desc *pins;
  639. struct sunxi_pinctrl *pctl;
  640. int i, ret, last_pin;
  641. struct clk *clk;
  642. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  643. if (!pctl)
  644. return -ENOMEM;
  645. platform_set_drvdata(pdev, pctl);
  646. spin_lock_init(&pctl->lock);
  647. pctl->membase = of_iomap(node, 0);
  648. if (!pctl->membase)
  649. return -ENOMEM;
  650. device = of_match_device(sunxi_pinctrl_match, &pdev->dev);
  651. if (!device)
  652. return -ENODEV;
  653. pctl->desc = (struct sunxi_pinctrl_desc *)device->data;
  654. ret = sunxi_pinctrl_build_state(pdev);
  655. if (ret) {
  656. dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
  657. return ret;
  658. }
  659. pins = devm_kzalloc(&pdev->dev,
  660. pctl->desc->npins * sizeof(*pins),
  661. GFP_KERNEL);
  662. if (!pins)
  663. return -ENOMEM;
  664. for (i = 0; i < pctl->desc->npins; i++)
  665. pins[i] = pctl->desc->pins[i].pin;
  666. sunxi_pctrl_desc.name = dev_name(&pdev->dev);
  667. sunxi_pctrl_desc.owner = THIS_MODULE;
  668. sunxi_pctrl_desc.pins = pins;
  669. sunxi_pctrl_desc.npins = pctl->desc->npins;
  670. pctl->dev = &pdev->dev;
  671. pctl->pctl_dev = pinctrl_register(&sunxi_pctrl_desc,
  672. &pdev->dev, pctl);
  673. if (!pctl->pctl_dev) {
  674. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  675. return -EINVAL;
  676. }
  677. pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
  678. if (!pctl->chip) {
  679. ret = -ENOMEM;
  680. goto pinctrl_error;
  681. }
  682. last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
  683. pctl->chip = &sunxi_pinctrl_gpio_chip;
  684. pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK);
  685. pctl->chip->label = dev_name(&pdev->dev);
  686. pctl->chip->dev = &pdev->dev;
  687. pctl->chip->base = 0;
  688. ret = gpiochip_add(pctl->chip);
  689. if (ret)
  690. goto pinctrl_error;
  691. for (i = 0; i < pctl->desc->npins; i++) {
  692. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  693. ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
  694. pin->pin.number,
  695. pin->pin.number, 1);
  696. if (ret)
  697. goto gpiochip_error;
  698. }
  699. clk = devm_clk_get(&pdev->dev, NULL);
  700. if (IS_ERR(clk)) {
  701. ret = PTR_ERR(clk);
  702. goto gpiochip_error;
  703. }
  704. clk_prepare_enable(clk);
  705. pctl->irq = irq_of_parse_and_map(node, 0);
  706. if (!pctl->irq) {
  707. ret = -EINVAL;
  708. goto gpiochip_error;
  709. }
  710. pctl->domain = irq_domain_add_linear(node, SUNXI_IRQ_NUMBER,
  711. &irq_domain_simple_ops, NULL);
  712. if (!pctl->domain) {
  713. dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
  714. ret = -ENOMEM;
  715. goto gpiochip_error;
  716. }
  717. for (i = 0; i < SUNXI_IRQ_NUMBER; i++) {
  718. int irqno = irq_create_mapping(pctl->domain, i);
  719. irq_set_chip_and_handler(irqno, &sunxi_pinctrl_irq_chip,
  720. handle_simple_irq);
  721. irq_set_chip_data(irqno, pctl);
  722. };
  723. irq_set_chained_handler(pctl->irq, sunxi_pinctrl_irq_handler);
  724. irq_set_handler_data(pctl->irq, pctl);
  725. dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
  726. return 0;
  727. gpiochip_error:
  728. if (gpiochip_remove(pctl->chip))
  729. dev_err(&pdev->dev, "failed to remove gpio chip\n");
  730. pinctrl_error:
  731. pinctrl_unregister(pctl->pctl_dev);
  732. return ret;
  733. }
  734. static struct platform_driver sunxi_pinctrl_driver = {
  735. .probe = sunxi_pinctrl_probe,
  736. .driver = {
  737. .name = "sunxi-pinctrl",
  738. .owner = THIS_MODULE,
  739. .of_match_table = sunxi_pinctrl_match,
  740. },
  741. };
  742. module_platform_driver(sunxi_pinctrl_driver);
  743. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  744. MODULE_DESCRIPTION("Allwinner A1X pinctrl driver");
  745. MODULE_LICENSE("GPL");