pinctrl-sunxi.c 23 KB

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