pinctrl-sunxi.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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/irqchip/chained_irq.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/pinctrl/consumer.h>
  23. #include <linux/pinctrl/machine.h>
  24. #include <linux/pinctrl/pinctrl.h>
  25. #include <linux/pinctrl/pinconf-generic.h>
  26. #include <linux/pinctrl/pinmux.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include "../core.h"
  30. #include "../../gpio/gpiolib.h"
  31. #include "pinctrl-sunxi.h"
  32. static struct irq_chip sunxi_pinctrl_edge_irq_chip;
  33. static struct irq_chip sunxi_pinctrl_level_irq_chip;
  34. static struct sunxi_pinctrl_group *
  35. sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
  36. {
  37. int i;
  38. for (i = 0; i < pctl->ngroups; i++) {
  39. struct sunxi_pinctrl_group *grp = pctl->groups + i;
  40. if (!strcmp(grp->name, group))
  41. return grp;
  42. }
  43. return NULL;
  44. }
  45. static struct sunxi_pinctrl_function *
  46. sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
  47. const char *name)
  48. {
  49. struct sunxi_pinctrl_function *func = pctl->functions;
  50. int i;
  51. for (i = 0; i < pctl->nfunctions; i++) {
  52. if (!func[i].name)
  53. break;
  54. if (!strcmp(func[i].name, name))
  55. return func + i;
  56. }
  57. return NULL;
  58. }
  59. static struct sunxi_desc_function *
  60. sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
  61. const char *pin_name,
  62. const char *func_name)
  63. {
  64. int i;
  65. for (i = 0; i < pctl->desc->npins; i++) {
  66. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  67. if (!strcmp(pin->pin.name, pin_name)) {
  68. struct sunxi_desc_function *func = pin->functions;
  69. while (func->name) {
  70. if (!strcmp(func->name, func_name))
  71. return func;
  72. func++;
  73. }
  74. }
  75. }
  76. return NULL;
  77. }
  78. static struct sunxi_desc_function *
  79. sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl,
  80. const u16 pin_num,
  81. const char *func_name)
  82. {
  83. int i;
  84. for (i = 0; i < pctl->desc->npins; i++) {
  85. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  86. if (pin->pin.number == pin_num) {
  87. struct sunxi_desc_function *func = pin->functions;
  88. while (func->name) {
  89. if (!strcmp(func->name, func_name))
  90. return func;
  91. func++;
  92. }
  93. }
  94. }
  95. return NULL;
  96. }
  97. static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
  98. {
  99. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  100. return pctl->ngroups;
  101. }
  102. static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
  103. unsigned group)
  104. {
  105. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  106. return pctl->groups[group].name;
  107. }
  108. static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
  109. unsigned group,
  110. const unsigned **pins,
  111. unsigned *num_pins)
  112. {
  113. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  114. *pins = (unsigned *)&pctl->groups[group].pin;
  115. *num_pins = 1;
  116. return 0;
  117. }
  118. static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  119. struct device_node *node,
  120. struct pinctrl_map **map,
  121. unsigned *num_maps)
  122. {
  123. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  124. unsigned long *pinconfig;
  125. struct property *prop;
  126. const char *function;
  127. const char *group;
  128. int ret, nmaps, i = 0;
  129. u32 val;
  130. *map = NULL;
  131. *num_maps = 0;
  132. ret = of_property_read_string(node, "allwinner,function", &function);
  133. if (ret) {
  134. dev_err(pctl->dev,
  135. "missing allwinner,function property in node %s\n",
  136. node->name);
  137. return -EINVAL;
  138. }
  139. nmaps = of_property_count_strings(node, "allwinner,pins") * 2;
  140. if (nmaps < 0) {
  141. dev_err(pctl->dev,
  142. "missing allwinner,pins property in node %s\n",
  143. node->name);
  144. return -EINVAL;
  145. }
  146. *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
  147. if (!*map)
  148. return -ENOMEM;
  149. of_property_for_each_string(node, "allwinner,pins", prop, group) {
  150. struct sunxi_pinctrl_group *grp =
  151. sunxi_pinctrl_find_group_by_name(pctl, group);
  152. int j = 0, configlen = 0;
  153. if (!grp) {
  154. dev_err(pctl->dev, "unknown pin %s", group);
  155. continue;
  156. }
  157. if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
  158. grp->name,
  159. function)) {
  160. dev_err(pctl->dev, "unsupported function %s on pin %s",
  161. function, group);
  162. continue;
  163. }
  164. (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
  165. (*map)[i].data.mux.group = group;
  166. (*map)[i].data.mux.function = function;
  167. i++;
  168. (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
  169. (*map)[i].data.configs.group_or_pin = group;
  170. if (of_find_property(node, "allwinner,drive", NULL))
  171. configlen++;
  172. if (of_find_property(node, "allwinner,pull", NULL))
  173. configlen++;
  174. pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);
  175. if (!pinconfig) {
  176. kfree(*map);
  177. return -ENOMEM;
  178. }
  179. if (!of_property_read_u32(node, "allwinner,drive", &val)) {
  180. u16 strength = (val + 1) * 10;
  181. pinconfig[j++] =
  182. pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
  183. strength);
  184. }
  185. if (!of_property_read_u32(node, "allwinner,pull", &val)) {
  186. enum pin_config_param pull = PIN_CONFIG_END;
  187. if (val == 1)
  188. pull = PIN_CONFIG_BIAS_PULL_UP;
  189. else if (val == 2)
  190. pull = PIN_CONFIG_BIAS_PULL_DOWN;
  191. pinconfig[j++] = pinconf_to_config_packed(pull, 0);
  192. }
  193. (*map)[i].data.configs.configs = pinconfig;
  194. (*map)[i].data.configs.num_configs = configlen;
  195. i++;
  196. }
  197. *num_maps = nmaps;
  198. return 0;
  199. }
  200. static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
  201. struct pinctrl_map *map,
  202. unsigned num_maps)
  203. {
  204. int i;
  205. for (i = 0; i < num_maps; i++) {
  206. if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
  207. kfree(map[i].data.configs.configs);
  208. }
  209. kfree(map);
  210. }
  211. static const struct pinctrl_ops sunxi_pctrl_ops = {
  212. .dt_node_to_map = sunxi_pctrl_dt_node_to_map,
  213. .dt_free_map = sunxi_pctrl_dt_free_map,
  214. .get_groups_count = sunxi_pctrl_get_groups_count,
  215. .get_group_name = sunxi_pctrl_get_group_name,
  216. .get_group_pins = sunxi_pctrl_get_group_pins,
  217. };
  218. static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
  219. unsigned group,
  220. unsigned long *config)
  221. {
  222. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  223. *config = pctl->groups[group].config;
  224. return 0;
  225. }
  226. static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev,
  227. unsigned group,
  228. unsigned long *configs,
  229. unsigned num_configs)
  230. {
  231. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  232. struct sunxi_pinctrl_group *g = &pctl->groups[group];
  233. unsigned long flags;
  234. unsigned pin = g->pin - pctl->desc->pin_base;
  235. u32 val, mask;
  236. u16 strength;
  237. u8 dlevel;
  238. int i;
  239. spin_lock_irqsave(&pctl->lock, flags);
  240. for (i = 0; i < num_configs; i++) {
  241. switch (pinconf_to_config_param(configs[i])) {
  242. case PIN_CONFIG_DRIVE_STRENGTH:
  243. strength = pinconf_to_config_argument(configs[i]);
  244. if (strength > 40) {
  245. spin_unlock_irqrestore(&pctl->lock, flags);
  246. return -EINVAL;
  247. }
  248. /*
  249. * We convert from mA to what the register expects:
  250. * 0: 10mA
  251. * 1: 20mA
  252. * 2: 30mA
  253. * 3: 40mA
  254. */
  255. dlevel = strength / 10 - 1;
  256. val = readl(pctl->membase + sunxi_dlevel_reg(pin));
  257. mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(pin);
  258. writel((val & ~mask)
  259. | dlevel << sunxi_dlevel_offset(pin),
  260. pctl->membase + sunxi_dlevel_reg(pin));
  261. break;
  262. case PIN_CONFIG_BIAS_PULL_UP:
  263. val = readl(pctl->membase + sunxi_pull_reg(pin));
  264. mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
  265. writel((val & ~mask) | 1 << sunxi_pull_offset(pin),
  266. pctl->membase + sunxi_pull_reg(pin));
  267. break;
  268. case PIN_CONFIG_BIAS_PULL_DOWN:
  269. val = readl(pctl->membase + sunxi_pull_reg(pin));
  270. mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
  271. writel((val & ~mask) | 2 << sunxi_pull_offset(pin),
  272. pctl->membase + sunxi_pull_reg(pin));
  273. break;
  274. default:
  275. break;
  276. }
  277. /* cache the config value */
  278. g->config = configs[i];
  279. } /* for each config */
  280. spin_unlock_irqrestore(&pctl->lock, flags);
  281. return 0;
  282. }
  283. static const struct pinconf_ops sunxi_pconf_ops = {
  284. .pin_config_group_get = sunxi_pconf_group_get,
  285. .pin_config_group_set = sunxi_pconf_group_set,
  286. };
  287. static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  288. {
  289. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  290. return pctl->nfunctions;
  291. }
  292. static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
  293. unsigned function)
  294. {
  295. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  296. return pctl->functions[function].name;
  297. }
  298. static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  299. unsigned function,
  300. const char * const **groups,
  301. unsigned * const num_groups)
  302. {
  303. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  304. *groups = pctl->functions[function].groups;
  305. *num_groups = pctl->functions[function].ngroups;
  306. return 0;
  307. }
  308. static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
  309. unsigned pin,
  310. u8 config)
  311. {
  312. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  313. unsigned long flags;
  314. u32 val, mask;
  315. spin_lock_irqsave(&pctl->lock, flags);
  316. pin -= pctl->desc->pin_base;
  317. val = readl(pctl->membase + sunxi_mux_reg(pin));
  318. mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
  319. writel((val & ~mask) | config << sunxi_mux_offset(pin),
  320. pctl->membase + sunxi_mux_reg(pin));
  321. spin_unlock_irqrestore(&pctl->lock, flags);
  322. }
  323. static int sunxi_pmx_set_mux(struct pinctrl_dev *pctldev,
  324. unsigned function,
  325. unsigned group)
  326. {
  327. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  328. struct sunxi_pinctrl_group *g = pctl->groups + group;
  329. struct sunxi_pinctrl_function *func = pctl->functions + function;
  330. struct sunxi_desc_function *desc =
  331. sunxi_pinctrl_desc_find_function_by_name(pctl,
  332. g->name,
  333. func->name);
  334. if (!desc)
  335. return -EINVAL;
  336. sunxi_pmx_set(pctldev, g->pin, desc->muxval);
  337. return 0;
  338. }
  339. static int
  340. sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  341. struct pinctrl_gpio_range *range,
  342. unsigned offset,
  343. bool input)
  344. {
  345. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  346. struct sunxi_desc_function *desc;
  347. const char *func;
  348. if (input)
  349. func = "gpio_in";
  350. else
  351. func = "gpio_out";
  352. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
  353. if (!desc)
  354. return -EINVAL;
  355. sunxi_pmx_set(pctldev, offset, desc->muxval);
  356. return 0;
  357. }
  358. static const struct pinmux_ops sunxi_pmx_ops = {
  359. .get_functions_count = sunxi_pmx_get_funcs_cnt,
  360. .get_function_name = sunxi_pmx_get_func_name,
  361. .get_function_groups = sunxi_pmx_get_func_groups,
  362. .set_mux = sunxi_pmx_set_mux,
  363. .gpio_set_direction = sunxi_pmx_gpio_set_direction,
  364. };
  365. static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
  366. unsigned offset)
  367. {
  368. return pinctrl_gpio_direction_input(chip->base + offset);
  369. }
  370. static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
  371. {
  372. struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
  373. u32 reg = sunxi_data_reg(offset);
  374. u8 index = sunxi_data_offset(offset);
  375. u32 set_mux = pctl->desc->irq_read_needs_mux &&
  376. test_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
  377. u32 val;
  378. if (set_mux)
  379. sunxi_pmx_set(pctl->pctl_dev, offset, SUN4I_FUNC_INPUT);
  380. val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
  381. if (set_mux)
  382. sunxi_pmx_set(pctl->pctl_dev, offset, SUN4I_FUNC_IRQ);
  383. return val;
  384. }
  385. static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
  386. unsigned offset, int value)
  387. {
  388. struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
  389. u32 reg = sunxi_data_reg(offset);
  390. u8 index = sunxi_data_offset(offset);
  391. unsigned long flags;
  392. u32 regval;
  393. spin_lock_irqsave(&pctl->lock, flags);
  394. regval = readl(pctl->membase + reg);
  395. if (value)
  396. regval |= BIT(index);
  397. else
  398. regval &= ~(BIT(index));
  399. writel(regval, pctl->membase + reg);
  400. spin_unlock_irqrestore(&pctl->lock, flags);
  401. }
  402. static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
  403. unsigned offset, int value)
  404. {
  405. sunxi_pinctrl_gpio_set(chip, offset, value);
  406. return pinctrl_gpio_direction_output(chip->base + offset);
  407. }
  408. static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
  409. const struct of_phandle_args *gpiospec,
  410. u32 *flags)
  411. {
  412. int pin, base;
  413. base = PINS_PER_BANK * gpiospec->args[0];
  414. pin = base + gpiospec->args[1];
  415. if (pin > gc->ngpio)
  416. return -EINVAL;
  417. if (flags)
  418. *flags = gpiospec->args[2];
  419. return pin;
  420. }
  421. static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  422. {
  423. struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
  424. struct sunxi_desc_function *desc;
  425. unsigned pinnum = pctl->desc->pin_base + offset;
  426. unsigned irqnum;
  427. if (offset >= chip->ngpio)
  428. return -ENXIO;
  429. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pinnum, "irq");
  430. if (!desc)
  431. return -EINVAL;
  432. irqnum = desc->irqbank * IRQ_PER_BANK + desc->irqnum;
  433. dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
  434. chip->label, offset + chip->base, irqnum);
  435. return irq_find_mapping(pctl->domain, irqnum);
  436. }
  437. static int sunxi_pinctrl_irq_request_resources(struct irq_data *d)
  438. {
  439. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  440. struct sunxi_desc_function *func;
  441. int ret;
  442. func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
  443. pctl->irq_array[d->hwirq], "irq");
  444. if (!func)
  445. return -EINVAL;
  446. ret = gpiochip_lock_as_irq(pctl->chip,
  447. pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
  448. if (ret) {
  449. dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
  450. irqd_to_hwirq(d));
  451. return ret;
  452. }
  453. /* Change muxing to INT mode */
  454. sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
  455. return 0;
  456. }
  457. static void sunxi_pinctrl_irq_release_resources(struct irq_data *d)
  458. {
  459. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  460. gpiochip_unlock_as_irq(pctl->chip,
  461. pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
  462. }
  463. static int sunxi_pinctrl_irq_set_type(struct irq_data *d, unsigned int type)
  464. {
  465. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  466. u32 reg = sunxi_irq_cfg_reg(d->hwirq, pctl->desc->irq_bank_base);
  467. u8 index = sunxi_irq_cfg_offset(d->hwirq);
  468. unsigned long flags;
  469. u32 regval;
  470. u8 mode;
  471. switch (type) {
  472. case IRQ_TYPE_EDGE_RISING:
  473. mode = IRQ_EDGE_RISING;
  474. break;
  475. case IRQ_TYPE_EDGE_FALLING:
  476. mode = IRQ_EDGE_FALLING;
  477. break;
  478. case IRQ_TYPE_EDGE_BOTH:
  479. mode = IRQ_EDGE_BOTH;
  480. break;
  481. case IRQ_TYPE_LEVEL_HIGH:
  482. mode = IRQ_LEVEL_HIGH;
  483. break;
  484. case IRQ_TYPE_LEVEL_LOW:
  485. mode = IRQ_LEVEL_LOW;
  486. break;
  487. default:
  488. return -EINVAL;
  489. }
  490. spin_lock_irqsave(&pctl->lock, flags);
  491. if (type & IRQ_TYPE_LEVEL_MASK)
  492. irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_level_irq_chip,
  493. handle_fasteoi_irq, NULL);
  494. else
  495. irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_edge_irq_chip,
  496. handle_edge_irq, NULL);
  497. regval = readl(pctl->membase + reg);
  498. regval &= ~(IRQ_CFG_IRQ_MASK << index);
  499. writel(regval | (mode << index), pctl->membase + reg);
  500. spin_unlock_irqrestore(&pctl->lock, flags);
  501. return 0;
  502. }
  503. static void sunxi_pinctrl_irq_ack(struct irq_data *d)
  504. {
  505. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  506. u32 status_reg = sunxi_irq_status_reg(d->hwirq,
  507. pctl->desc->irq_bank_base);
  508. u8 status_idx = sunxi_irq_status_offset(d->hwirq);
  509. /* Clear the IRQ */
  510. writel(1 << status_idx, pctl->membase + status_reg);
  511. }
  512. static void sunxi_pinctrl_irq_mask(struct irq_data *d)
  513. {
  514. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  515. u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base);
  516. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  517. unsigned long flags;
  518. u32 val;
  519. spin_lock_irqsave(&pctl->lock, flags);
  520. /* Mask 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 void sunxi_pinctrl_irq_unmask(struct irq_data *d)
  526. {
  527. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  528. u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base);
  529. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  530. unsigned long flags;
  531. u32 val;
  532. spin_lock_irqsave(&pctl->lock, flags);
  533. /* Unmask the IRQ */
  534. val = readl(pctl->membase + reg);
  535. writel(val | (1 << idx), pctl->membase + reg);
  536. spin_unlock_irqrestore(&pctl->lock, flags);
  537. }
  538. static void sunxi_pinctrl_irq_ack_unmask(struct irq_data *d)
  539. {
  540. sunxi_pinctrl_irq_ack(d);
  541. sunxi_pinctrl_irq_unmask(d);
  542. }
  543. static struct irq_chip sunxi_pinctrl_edge_irq_chip = {
  544. .name = "sunxi_pio_edge",
  545. .irq_ack = sunxi_pinctrl_irq_ack,
  546. .irq_mask = sunxi_pinctrl_irq_mask,
  547. .irq_unmask = sunxi_pinctrl_irq_unmask,
  548. .irq_request_resources = sunxi_pinctrl_irq_request_resources,
  549. .irq_release_resources = sunxi_pinctrl_irq_release_resources,
  550. .irq_set_type = sunxi_pinctrl_irq_set_type,
  551. .flags = IRQCHIP_SKIP_SET_WAKE,
  552. };
  553. static struct irq_chip sunxi_pinctrl_level_irq_chip = {
  554. .name = "sunxi_pio_level",
  555. .irq_eoi = sunxi_pinctrl_irq_ack,
  556. .irq_mask = sunxi_pinctrl_irq_mask,
  557. .irq_unmask = sunxi_pinctrl_irq_unmask,
  558. /* Define irq_enable / disable to avoid spurious irqs for drivers
  559. * using these to suppress irqs while they clear the irq source */
  560. .irq_enable = sunxi_pinctrl_irq_ack_unmask,
  561. .irq_disable = sunxi_pinctrl_irq_mask,
  562. .irq_request_resources = sunxi_pinctrl_irq_request_resources,
  563. .irq_release_resources = sunxi_pinctrl_irq_release_resources,
  564. .irq_set_type = sunxi_pinctrl_irq_set_type,
  565. .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_EOI_THREADED |
  566. IRQCHIP_EOI_IF_HANDLED,
  567. };
  568. static int sunxi_pinctrl_irq_of_xlate(struct irq_domain *d,
  569. struct device_node *node,
  570. const u32 *intspec,
  571. unsigned int intsize,
  572. unsigned long *out_hwirq,
  573. unsigned int *out_type)
  574. {
  575. struct sunxi_pinctrl *pctl = d->host_data;
  576. struct sunxi_desc_function *desc;
  577. int pin, base;
  578. if (intsize < 3)
  579. return -EINVAL;
  580. base = PINS_PER_BANK * intspec[0];
  581. pin = pctl->desc->pin_base + base + intspec[1];
  582. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
  583. if (!desc)
  584. return -EINVAL;
  585. *out_hwirq = desc->irqbank * PINS_PER_BANK + desc->irqnum;
  586. *out_type = intspec[2];
  587. return 0;
  588. }
  589. static struct irq_domain_ops sunxi_pinctrl_irq_domain_ops = {
  590. .xlate = sunxi_pinctrl_irq_of_xlate,
  591. };
  592. static void sunxi_pinctrl_irq_handler(struct irq_desc *desc)
  593. {
  594. unsigned int irq = irq_desc_get_irq(desc);
  595. struct irq_chip *chip = irq_desc_get_chip(desc);
  596. struct sunxi_pinctrl *pctl = irq_desc_get_handler_data(desc);
  597. unsigned long bank, reg, val;
  598. for (bank = 0; bank < pctl->desc->irq_banks; bank++)
  599. if (irq == pctl->irq[bank])
  600. break;
  601. if (bank == pctl->desc->irq_banks)
  602. return;
  603. reg = sunxi_irq_status_reg_from_bank(bank, pctl->desc->irq_bank_base);
  604. val = readl(pctl->membase + reg);
  605. if (val) {
  606. int irqoffset;
  607. chained_irq_enter(chip, desc);
  608. for_each_set_bit(irqoffset, &val, IRQ_PER_BANK) {
  609. int pin_irq = irq_find_mapping(pctl->domain,
  610. bank * IRQ_PER_BANK + irqoffset);
  611. generic_handle_irq(pin_irq);
  612. }
  613. chained_irq_exit(chip, desc);
  614. }
  615. }
  616. static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
  617. const char *name)
  618. {
  619. struct sunxi_pinctrl_function *func = pctl->functions;
  620. while (func->name) {
  621. /* function already there */
  622. if (strcmp(func->name, name) == 0) {
  623. func->ngroups++;
  624. return -EEXIST;
  625. }
  626. func++;
  627. }
  628. func->name = name;
  629. func->ngroups = 1;
  630. pctl->nfunctions++;
  631. return 0;
  632. }
  633. static int sunxi_pinctrl_build_state(struct platform_device *pdev)
  634. {
  635. struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
  636. int i;
  637. pctl->ngroups = pctl->desc->npins;
  638. /* Allocate groups */
  639. pctl->groups = devm_kzalloc(&pdev->dev,
  640. pctl->ngroups * sizeof(*pctl->groups),
  641. GFP_KERNEL);
  642. if (!pctl->groups)
  643. return -ENOMEM;
  644. for (i = 0; i < pctl->desc->npins; i++) {
  645. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  646. struct sunxi_pinctrl_group *group = pctl->groups + i;
  647. group->name = pin->pin.name;
  648. group->pin = pin->pin.number;
  649. }
  650. /*
  651. * We suppose that we won't have any more functions than pins,
  652. * we'll reallocate that later anyway
  653. */
  654. pctl->functions = devm_kzalloc(&pdev->dev,
  655. pctl->desc->npins * sizeof(*pctl->functions),
  656. GFP_KERNEL);
  657. if (!pctl->functions)
  658. return -ENOMEM;
  659. /* Count functions and their associated groups */
  660. for (i = 0; i < pctl->desc->npins; i++) {
  661. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  662. struct sunxi_desc_function *func = pin->functions;
  663. while (func->name) {
  664. /* Create interrupt mapping while we're at it */
  665. if (!strcmp(func->name, "irq")) {
  666. int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
  667. pctl->irq_array[irqnum] = pin->pin.number;
  668. }
  669. sunxi_pinctrl_add_function(pctl, func->name);
  670. func++;
  671. }
  672. }
  673. pctl->functions = krealloc(pctl->functions,
  674. pctl->nfunctions * sizeof(*pctl->functions),
  675. GFP_KERNEL);
  676. for (i = 0; i < pctl->desc->npins; i++) {
  677. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  678. struct sunxi_desc_function *func = pin->functions;
  679. while (func->name) {
  680. struct sunxi_pinctrl_function *func_item;
  681. const char **func_grp;
  682. func_item = sunxi_pinctrl_find_function_by_name(pctl,
  683. func->name);
  684. if (!func_item)
  685. return -EINVAL;
  686. if (!func_item->groups) {
  687. func_item->groups =
  688. devm_kzalloc(&pdev->dev,
  689. func_item->ngroups * sizeof(*func_item->groups),
  690. GFP_KERNEL);
  691. if (!func_item->groups)
  692. return -ENOMEM;
  693. }
  694. func_grp = func_item->groups;
  695. while (*func_grp)
  696. func_grp++;
  697. *func_grp = pin->pin.name;
  698. func++;
  699. }
  700. }
  701. return 0;
  702. }
  703. int sunxi_pinctrl_init(struct platform_device *pdev,
  704. const struct sunxi_pinctrl_desc *desc)
  705. {
  706. struct device_node *node = pdev->dev.of_node;
  707. struct pinctrl_desc *pctrl_desc;
  708. struct pinctrl_pin_desc *pins;
  709. struct sunxi_pinctrl *pctl;
  710. struct resource *res;
  711. int i, ret, last_pin;
  712. struct clk *clk;
  713. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  714. if (!pctl)
  715. return -ENOMEM;
  716. platform_set_drvdata(pdev, pctl);
  717. spin_lock_init(&pctl->lock);
  718. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  719. pctl->membase = devm_ioremap_resource(&pdev->dev, res);
  720. if (IS_ERR(pctl->membase))
  721. return PTR_ERR(pctl->membase);
  722. pctl->dev = &pdev->dev;
  723. pctl->desc = desc;
  724. pctl->irq_array = devm_kcalloc(&pdev->dev,
  725. IRQ_PER_BANK * pctl->desc->irq_banks,
  726. sizeof(*pctl->irq_array),
  727. GFP_KERNEL);
  728. if (!pctl->irq_array)
  729. return -ENOMEM;
  730. ret = sunxi_pinctrl_build_state(pdev);
  731. if (ret) {
  732. dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
  733. return ret;
  734. }
  735. pins = devm_kzalloc(&pdev->dev,
  736. pctl->desc->npins * sizeof(*pins),
  737. GFP_KERNEL);
  738. if (!pins)
  739. return -ENOMEM;
  740. for (i = 0; i < pctl->desc->npins; i++)
  741. pins[i] = pctl->desc->pins[i].pin;
  742. pctrl_desc = devm_kzalloc(&pdev->dev,
  743. sizeof(*pctrl_desc),
  744. GFP_KERNEL);
  745. if (!pctrl_desc)
  746. return -ENOMEM;
  747. pctrl_desc->name = dev_name(&pdev->dev);
  748. pctrl_desc->owner = THIS_MODULE;
  749. pctrl_desc->pins = pins;
  750. pctrl_desc->npins = pctl->desc->npins;
  751. pctrl_desc->confops = &sunxi_pconf_ops;
  752. pctrl_desc->pctlops = &sunxi_pctrl_ops;
  753. pctrl_desc->pmxops = &sunxi_pmx_ops;
  754. pctl->pctl_dev = pinctrl_register(pctrl_desc,
  755. &pdev->dev, pctl);
  756. if (IS_ERR(pctl->pctl_dev)) {
  757. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  758. return PTR_ERR(pctl->pctl_dev);
  759. }
  760. pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
  761. if (!pctl->chip) {
  762. ret = -ENOMEM;
  763. goto pinctrl_error;
  764. }
  765. last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
  766. pctl->chip->owner = THIS_MODULE;
  767. pctl->chip->request = gpiochip_generic_request,
  768. pctl->chip->free = gpiochip_generic_free,
  769. pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input,
  770. pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output,
  771. pctl->chip->get = sunxi_pinctrl_gpio_get,
  772. pctl->chip->set = sunxi_pinctrl_gpio_set,
  773. pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate,
  774. pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq,
  775. pctl->chip->of_gpio_n_cells = 3,
  776. pctl->chip->can_sleep = false,
  777. pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
  778. pctl->desc->pin_base;
  779. pctl->chip->label = dev_name(&pdev->dev);
  780. pctl->chip->dev = &pdev->dev;
  781. pctl->chip->base = pctl->desc->pin_base;
  782. ret = gpiochip_add(pctl->chip);
  783. if (ret)
  784. goto pinctrl_error;
  785. for (i = 0; i < pctl->desc->npins; i++) {
  786. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  787. ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
  788. pin->pin.number - pctl->desc->pin_base,
  789. pin->pin.number, 1);
  790. if (ret)
  791. goto gpiochip_error;
  792. }
  793. clk = devm_clk_get(&pdev->dev, NULL);
  794. if (IS_ERR(clk)) {
  795. ret = PTR_ERR(clk);
  796. goto gpiochip_error;
  797. }
  798. ret = clk_prepare_enable(clk);
  799. if (ret)
  800. goto gpiochip_error;
  801. pctl->irq = devm_kcalloc(&pdev->dev,
  802. pctl->desc->irq_banks,
  803. sizeof(*pctl->irq),
  804. GFP_KERNEL);
  805. if (!pctl->irq) {
  806. ret = -ENOMEM;
  807. goto clk_error;
  808. }
  809. for (i = 0; i < pctl->desc->irq_banks; i++) {
  810. pctl->irq[i] = platform_get_irq(pdev, i);
  811. if (pctl->irq[i] < 0) {
  812. ret = pctl->irq[i];
  813. goto clk_error;
  814. }
  815. }
  816. pctl->domain = irq_domain_add_linear(node,
  817. pctl->desc->irq_banks * IRQ_PER_BANK,
  818. &sunxi_pinctrl_irq_domain_ops,
  819. pctl);
  820. if (!pctl->domain) {
  821. dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
  822. ret = -ENOMEM;
  823. goto clk_error;
  824. }
  825. for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
  826. int irqno = irq_create_mapping(pctl->domain, i);
  827. irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip,
  828. handle_edge_irq);
  829. irq_set_chip_data(irqno, pctl);
  830. }
  831. for (i = 0; i < pctl->desc->irq_banks; i++) {
  832. /* Mask and clear all IRQs before registering a handler */
  833. writel(0, pctl->membase + sunxi_irq_ctrl_reg_from_bank(i,
  834. pctl->desc->irq_bank_base));
  835. writel(0xffffffff,
  836. pctl->membase + sunxi_irq_status_reg_from_bank(i,
  837. pctl->desc->irq_bank_base));
  838. irq_set_chained_handler_and_data(pctl->irq[i],
  839. sunxi_pinctrl_irq_handler,
  840. pctl);
  841. }
  842. dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
  843. return 0;
  844. clk_error:
  845. clk_disable_unprepare(clk);
  846. gpiochip_error:
  847. gpiochip_remove(pctl->chip);
  848. pinctrl_error:
  849. pinctrl_unregister(pctl->pctl_dev);
  850. return ret;
  851. }