pinctrl-exynos5440.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * pin-controller/pin-mux/pin-config/gpio-driver for Samsung's EXYNOS5440 SoC.
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/io.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/gpio/driver.h>
  18. #include <linux/device.h>
  19. #include <linux/pinctrl/pinctrl.h>
  20. #include <linux/pinctrl/pinmux.h>
  21. #include <linux/pinctrl/pinconf.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irqdomain.h>
  24. #include <linux/of_irq.h>
  25. #include "../core.h"
  26. /* EXYNOS5440 GPIO and Pinctrl register offsets */
  27. #define GPIO_MUX 0x00
  28. #define GPIO_IE 0x04
  29. #define GPIO_INT 0x08
  30. #define GPIO_TYPE 0x0C
  31. #define GPIO_VAL 0x10
  32. #define GPIO_OE 0x14
  33. #define GPIO_IN 0x18
  34. #define GPIO_PE 0x1C
  35. #define GPIO_PS 0x20
  36. #define GPIO_SR 0x24
  37. #define GPIO_DS0 0x28
  38. #define GPIO_DS1 0x2C
  39. #define EXYNOS5440_MAX_PINS 23
  40. #define EXYNOS5440_MAX_GPIO_INT 8
  41. #define PIN_NAME_LENGTH 10
  42. #define GROUP_SUFFIX "-grp"
  43. #define FUNCTION_SUFFIX "-mux"
  44. /*
  45. * pin configuration type and its value are packed together into a 16-bits.
  46. * The upper 8-bits represent the configuration type and the lower 8-bits
  47. * hold the value of the configuration type.
  48. */
  49. #define PINCFG_TYPE_MASK 0xFF
  50. #define PINCFG_VALUE_SHIFT 8
  51. #define PINCFG_VALUE_MASK (0xFF << PINCFG_VALUE_SHIFT)
  52. #define PINCFG_PACK(type, value) (((value) << PINCFG_VALUE_SHIFT) | type)
  53. #define PINCFG_UNPACK_TYPE(cfg) ((cfg) & PINCFG_TYPE_MASK)
  54. #define PINCFG_UNPACK_VALUE(cfg) (((cfg) & PINCFG_VALUE_MASK) >> \
  55. PINCFG_VALUE_SHIFT)
  56. /**
  57. * enum pincfg_type - possible pin configuration types supported.
  58. * @PINCFG_TYPE_PUD: Pull up/down configuration.
  59. * @PINCFG_TYPE_DRV: Drive strength configuration.
  60. * @PINCFG_TYPE_SKEW_RATE: Skew rate configuration.
  61. * @PINCFG_TYPE_INPUT_TYPE: Pin input type configuration.
  62. */
  63. enum pincfg_type {
  64. PINCFG_TYPE_PUD,
  65. PINCFG_TYPE_DRV,
  66. PINCFG_TYPE_SKEW_RATE,
  67. PINCFG_TYPE_INPUT_TYPE
  68. };
  69. /**
  70. * struct exynos5440_pin_group: represent group of pins for pincfg setting.
  71. * @name: name of the pin group, used to lookup the group.
  72. * @pins: the pins included in this group.
  73. * @num_pins: number of pins included in this group.
  74. */
  75. struct exynos5440_pin_group {
  76. const char *name;
  77. const unsigned int *pins;
  78. u8 num_pins;
  79. };
  80. /**
  81. * struct exynos5440_pmx_func: represent a pin function.
  82. * @name: name of the pin function, used to lookup the function.
  83. * @groups: one or more names of pin groups that provide this function.
  84. * @num_groups: number of groups included in @groups.
  85. * @function: the function number to be programmed when selected.
  86. */
  87. struct exynos5440_pmx_func {
  88. const char *name;
  89. const char **groups;
  90. u8 num_groups;
  91. unsigned long function;
  92. };
  93. /**
  94. * struct exynos5440_pinctrl_priv_data: driver's private runtime data.
  95. * @reg_base: ioremapped based address of the register space.
  96. * @gc: gpio chip registered with gpiolib.
  97. * @pin_groups: list of pin groups parsed from device tree.
  98. * @nr_groups: number of pin groups available.
  99. * @pmx_functions: list of pin functions parsed from device tree.
  100. * @nr_functions: number of pin functions available.
  101. * @range: gpio range to register with pinctrl
  102. */
  103. struct exynos5440_pinctrl_priv_data {
  104. void __iomem *reg_base;
  105. struct gpio_chip *gc;
  106. struct irq_domain *irq_domain;
  107. const struct exynos5440_pin_group *pin_groups;
  108. unsigned int nr_groups;
  109. const struct exynos5440_pmx_func *pmx_functions;
  110. unsigned int nr_functions;
  111. struct pinctrl_gpio_range range;
  112. };
  113. /**
  114. * struct exynos5440_gpio_intr_data: private data for gpio interrupts.
  115. * @priv: driver's private runtime data.
  116. * @gpio_int: gpio interrupt number.
  117. */
  118. struct exynos5440_gpio_intr_data {
  119. struct exynos5440_pinctrl_priv_data *priv;
  120. unsigned int gpio_int;
  121. };
  122. /* list of all possible config options supported */
  123. static struct pin_config {
  124. char *prop_cfg;
  125. unsigned int cfg_type;
  126. } pcfgs[] = {
  127. { "samsung,exynos5440-pin-pud", PINCFG_TYPE_PUD },
  128. { "samsung,exynos5440-pin-drv", PINCFG_TYPE_DRV },
  129. { "samsung,exynos5440-pin-skew-rate", PINCFG_TYPE_SKEW_RATE },
  130. { "samsung,exynos5440-pin-input-type", PINCFG_TYPE_INPUT_TYPE },
  131. };
  132. /* check if the selector is a valid pin group selector */
  133. static int exynos5440_get_group_count(struct pinctrl_dev *pctldev)
  134. {
  135. struct exynos5440_pinctrl_priv_data *priv;
  136. priv = pinctrl_dev_get_drvdata(pctldev);
  137. return priv->nr_groups;
  138. }
  139. /* return the name of the group selected by the group selector */
  140. static const char *exynos5440_get_group_name(struct pinctrl_dev *pctldev,
  141. unsigned selector)
  142. {
  143. struct exynos5440_pinctrl_priv_data *priv;
  144. priv = pinctrl_dev_get_drvdata(pctldev);
  145. return priv->pin_groups[selector].name;
  146. }
  147. /* return the pin numbers associated with the specified group */
  148. static int exynos5440_get_group_pins(struct pinctrl_dev *pctldev,
  149. unsigned selector, const unsigned **pins, unsigned *num_pins)
  150. {
  151. struct exynos5440_pinctrl_priv_data *priv;
  152. priv = pinctrl_dev_get_drvdata(pctldev);
  153. *pins = priv->pin_groups[selector].pins;
  154. *num_pins = priv->pin_groups[selector].num_pins;
  155. return 0;
  156. }
  157. /* create pinctrl_map entries by parsing device tree nodes */
  158. static int exynos5440_dt_node_to_map(struct pinctrl_dev *pctldev,
  159. struct device_node *np, struct pinctrl_map **maps,
  160. unsigned *nmaps)
  161. {
  162. struct device *dev = pctldev->dev;
  163. struct pinctrl_map *map;
  164. unsigned long *cfg = NULL;
  165. char *gname, *fname;
  166. int cfg_cnt = 0, map_cnt = 0, idx = 0;
  167. /* count the number of config options specfied in the node */
  168. for (idx = 0; idx < ARRAY_SIZE(pcfgs); idx++)
  169. if (of_find_property(np, pcfgs[idx].prop_cfg, NULL))
  170. cfg_cnt++;
  171. /*
  172. * Find out the number of map entries to create. All the config options
  173. * can be accomadated into a single config map entry.
  174. */
  175. if (cfg_cnt)
  176. map_cnt = 1;
  177. if (of_find_property(np, "samsung,exynos5440-pin-function", NULL))
  178. map_cnt++;
  179. if (!map_cnt) {
  180. dev_err(dev, "node %s does not have either config or function "
  181. "configurations\n", np->name);
  182. return -EINVAL;
  183. }
  184. /* Allocate memory for pin-map entries */
  185. map = kzalloc(sizeof(*map) * map_cnt, GFP_KERNEL);
  186. if (!map)
  187. return -ENOMEM;
  188. *nmaps = 0;
  189. /*
  190. * Allocate memory for pin group name. The pin group name is derived
  191. * from the node name from which these map entries are be created.
  192. */
  193. gname = kasprintf(GFP_KERNEL, "%s%s", np->name, GROUP_SUFFIX);
  194. if (!gname)
  195. goto free_map;
  196. /*
  197. * don't have config options? then skip over to creating function
  198. * map entries.
  199. */
  200. if (!cfg_cnt)
  201. goto skip_cfgs;
  202. /* Allocate memory for config entries */
  203. cfg = kzalloc(sizeof(*cfg) * cfg_cnt, GFP_KERNEL);
  204. if (!cfg)
  205. goto free_gname;
  206. /* Prepare a list of config settings */
  207. for (idx = 0, cfg_cnt = 0; idx < ARRAY_SIZE(pcfgs); idx++) {
  208. u32 value;
  209. if (!of_property_read_u32(np, pcfgs[idx].prop_cfg, &value))
  210. cfg[cfg_cnt++] =
  211. PINCFG_PACK(pcfgs[idx].cfg_type, value);
  212. }
  213. /* create the config map entry */
  214. map[*nmaps].data.configs.group_or_pin = gname;
  215. map[*nmaps].data.configs.configs = cfg;
  216. map[*nmaps].data.configs.num_configs = cfg_cnt;
  217. map[*nmaps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
  218. *nmaps += 1;
  219. skip_cfgs:
  220. /* create the function map entry */
  221. if (of_find_property(np, "samsung,exynos5440-pin-function", NULL)) {
  222. fname = kasprintf(GFP_KERNEL,
  223. "%s%s", np->name, FUNCTION_SUFFIX);
  224. if (!fname)
  225. goto free_cfg;
  226. map[*nmaps].data.mux.group = gname;
  227. map[*nmaps].data.mux.function = fname;
  228. map[*nmaps].type = PIN_MAP_TYPE_MUX_GROUP;
  229. *nmaps += 1;
  230. }
  231. *maps = map;
  232. return 0;
  233. free_cfg:
  234. kfree(cfg);
  235. free_gname:
  236. kfree(gname);
  237. free_map:
  238. kfree(map);
  239. return -ENOMEM;
  240. }
  241. /* free the memory allocated to hold the pin-map table */
  242. static void exynos5440_dt_free_map(struct pinctrl_dev *pctldev,
  243. struct pinctrl_map *map, unsigned num_maps)
  244. {
  245. int idx;
  246. for (idx = 0; idx < num_maps; idx++) {
  247. if (map[idx].type == PIN_MAP_TYPE_MUX_GROUP) {
  248. kfree(map[idx].data.mux.function);
  249. if (!idx)
  250. kfree(map[idx].data.mux.group);
  251. } else if (map->type == PIN_MAP_TYPE_CONFIGS_GROUP) {
  252. kfree(map[idx].data.configs.configs);
  253. if (!idx)
  254. kfree(map[idx].data.configs.group_or_pin);
  255. }
  256. }
  257. kfree(map);
  258. }
  259. /* list of pinctrl callbacks for the pinctrl core */
  260. static const struct pinctrl_ops exynos5440_pctrl_ops = {
  261. .get_groups_count = exynos5440_get_group_count,
  262. .get_group_name = exynos5440_get_group_name,
  263. .get_group_pins = exynos5440_get_group_pins,
  264. .dt_node_to_map = exynos5440_dt_node_to_map,
  265. .dt_free_map = exynos5440_dt_free_map,
  266. };
  267. /* check if the selector is a valid pin function selector */
  268. static int exynos5440_get_functions_count(struct pinctrl_dev *pctldev)
  269. {
  270. struct exynos5440_pinctrl_priv_data *priv;
  271. priv = pinctrl_dev_get_drvdata(pctldev);
  272. return priv->nr_functions;
  273. }
  274. /* return the name of the pin function specified */
  275. static const char *exynos5440_pinmux_get_fname(struct pinctrl_dev *pctldev,
  276. unsigned selector)
  277. {
  278. struct exynos5440_pinctrl_priv_data *priv;
  279. priv = pinctrl_dev_get_drvdata(pctldev);
  280. return priv->pmx_functions[selector].name;
  281. }
  282. /* return the groups associated for the specified function selector */
  283. static int exynos5440_pinmux_get_groups(struct pinctrl_dev *pctldev,
  284. unsigned selector, const char * const **groups,
  285. unsigned * const num_groups)
  286. {
  287. struct exynos5440_pinctrl_priv_data *priv;
  288. priv = pinctrl_dev_get_drvdata(pctldev);
  289. *groups = priv->pmx_functions[selector].groups;
  290. *num_groups = priv->pmx_functions[selector].num_groups;
  291. return 0;
  292. }
  293. /* enable or disable a pinmux function */
  294. static void exynos5440_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
  295. unsigned group, bool enable)
  296. {
  297. struct exynos5440_pinctrl_priv_data *priv;
  298. void __iomem *base;
  299. u32 function;
  300. u32 data;
  301. priv = pinctrl_dev_get_drvdata(pctldev);
  302. base = priv->reg_base;
  303. function = priv->pmx_functions[selector].function;
  304. data = readl(base + GPIO_MUX);
  305. if (enable)
  306. data |= (1 << function);
  307. else
  308. data &= ~(1 << function);
  309. writel(data, base + GPIO_MUX);
  310. }
  311. /* enable a specified pinmux by writing to registers */
  312. static int exynos5440_pinmux_set_mux(struct pinctrl_dev *pctldev,
  313. unsigned selector,
  314. unsigned group)
  315. {
  316. exynos5440_pinmux_setup(pctldev, selector, group, true);
  317. return 0;
  318. }
  319. /*
  320. * The calls to gpio_direction_output() and gpio_direction_input()
  321. * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
  322. * function called from the gpiolib interface).
  323. */
  324. static int exynos5440_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
  325. struct pinctrl_gpio_range *range, unsigned offset, bool input)
  326. {
  327. return 0;
  328. }
  329. /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
  330. static const struct pinmux_ops exynos5440_pinmux_ops = {
  331. .get_functions_count = exynos5440_get_functions_count,
  332. .get_function_name = exynos5440_pinmux_get_fname,
  333. .get_function_groups = exynos5440_pinmux_get_groups,
  334. .set_mux = exynos5440_pinmux_set_mux,
  335. .gpio_set_direction = exynos5440_pinmux_gpio_set_direction,
  336. };
  337. /* set the pin config settings for a specified pin */
  338. static int exynos5440_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  339. unsigned long *configs,
  340. unsigned num_configs)
  341. {
  342. struct exynos5440_pinctrl_priv_data *priv;
  343. void __iomem *base;
  344. enum pincfg_type cfg_type;
  345. u32 cfg_value;
  346. u32 data;
  347. int i;
  348. priv = pinctrl_dev_get_drvdata(pctldev);
  349. base = priv->reg_base;
  350. for (i = 0; i < num_configs; i++) {
  351. cfg_type = PINCFG_UNPACK_TYPE(configs[i]);
  352. cfg_value = PINCFG_UNPACK_VALUE(configs[i]);
  353. switch (cfg_type) {
  354. case PINCFG_TYPE_PUD:
  355. /* first set pull enable/disable bit */
  356. data = readl(base + GPIO_PE);
  357. data &= ~(1 << pin);
  358. if (cfg_value)
  359. data |= (1 << pin);
  360. writel(data, base + GPIO_PE);
  361. /* then set pull up/down bit */
  362. data = readl(base + GPIO_PS);
  363. data &= ~(1 << pin);
  364. if (cfg_value == 2)
  365. data |= (1 << pin);
  366. writel(data, base + GPIO_PS);
  367. break;
  368. case PINCFG_TYPE_DRV:
  369. /* set the first bit of the drive strength */
  370. data = readl(base + GPIO_DS0);
  371. data &= ~(1 << pin);
  372. data |= ((cfg_value & 1) << pin);
  373. writel(data, base + GPIO_DS0);
  374. cfg_value >>= 1;
  375. /* set the second bit of the driver strength */
  376. data = readl(base + GPIO_DS1);
  377. data &= ~(1 << pin);
  378. data |= ((cfg_value & 1) << pin);
  379. writel(data, base + GPIO_DS1);
  380. break;
  381. case PINCFG_TYPE_SKEW_RATE:
  382. data = readl(base + GPIO_SR);
  383. data &= ~(1 << pin);
  384. data |= ((cfg_value & 1) << pin);
  385. writel(data, base + GPIO_SR);
  386. break;
  387. case PINCFG_TYPE_INPUT_TYPE:
  388. data = readl(base + GPIO_TYPE);
  389. data &= ~(1 << pin);
  390. data |= ((cfg_value & 1) << pin);
  391. writel(data, base + GPIO_TYPE);
  392. break;
  393. default:
  394. WARN_ON(1);
  395. return -EINVAL;
  396. }
  397. } /* for each config */
  398. return 0;
  399. }
  400. /* get the pin config settings for a specified pin */
  401. static int exynos5440_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  402. unsigned long *config)
  403. {
  404. struct exynos5440_pinctrl_priv_data *priv;
  405. void __iomem *base;
  406. enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config);
  407. u32 data;
  408. priv = pinctrl_dev_get_drvdata(pctldev);
  409. base = priv->reg_base;
  410. switch (cfg_type) {
  411. case PINCFG_TYPE_PUD:
  412. data = readl(base + GPIO_PE);
  413. data = (data >> pin) & 1;
  414. if (!data)
  415. *config = 0;
  416. else
  417. *config = ((readl(base + GPIO_PS) >> pin) & 1) + 1;
  418. break;
  419. case PINCFG_TYPE_DRV:
  420. data = readl(base + GPIO_DS0);
  421. data = (data >> pin) & 1;
  422. *config = data;
  423. data = readl(base + GPIO_DS1);
  424. data = (data >> pin) & 1;
  425. *config |= (data << 1);
  426. break;
  427. case PINCFG_TYPE_SKEW_RATE:
  428. data = readl(base + GPIO_SR);
  429. *config = (data >> pin) & 1;
  430. break;
  431. case PINCFG_TYPE_INPUT_TYPE:
  432. data = readl(base + GPIO_TYPE);
  433. *config = (data >> pin) & 1;
  434. break;
  435. default:
  436. WARN_ON(1);
  437. return -EINVAL;
  438. }
  439. return 0;
  440. }
  441. /* set the pin config settings for a specified pin group */
  442. static int exynos5440_pinconf_group_set(struct pinctrl_dev *pctldev,
  443. unsigned group, unsigned long *configs,
  444. unsigned num_configs)
  445. {
  446. struct exynos5440_pinctrl_priv_data *priv;
  447. const unsigned int *pins;
  448. unsigned int cnt;
  449. priv = pinctrl_dev_get_drvdata(pctldev);
  450. pins = priv->pin_groups[group].pins;
  451. for (cnt = 0; cnt < priv->pin_groups[group].num_pins; cnt++)
  452. exynos5440_pinconf_set(pctldev, pins[cnt], configs,
  453. num_configs);
  454. return 0;
  455. }
  456. /* get the pin config settings for a specified pin group */
  457. static int exynos5440_pinconf_group_get(struct pinctrl_dev *pctldev,
  458. unsigned int group, unsigned long *config)
  459. {
  460. struct exynos5440_pinctrl_priv_data *priv;
  461. const unsigned int *pins;
  462. priv = pinctrl_dev_get_drvdata(pctldev);
  463. pins = priv->pin_groups[group].pins;
  464. exynos5440_pinconf_get(pctldev, pins[0], config);
  465. return 0;
  466. }
  467. /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
  468. static const struct pinconf_ops exynos5440_pinconf_ops = {
  469. .pin_config_get = exynos5440_pinconf_get,
  470. .pin_config_set = exynos5440_pinconf_set,
  471. .pin_config_group_get = exynos5440_pinconf_group_get,
  472. .pin_config_group_set = exynos5440_pinconf_group_set,
  473. };
  474. /* gpiolib gpio_set callback function */
  475. static void exynos5440_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  476. {
  477. struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
  478. void __iomem *base = priv->reg_base;
  479. u32 data;
  480. data = readl(base + GPIO_VAL);
  481. data &= ~(1 << offset);
  482. if (value)
  483. data |= 1 << offset;
  484. writel(data, base + GPIO_VAL);
  485. }
  486. /* gpiolib gpio_get callback function */
  487. static int exynos5440_gpio_get(struct gpio_chip *gc, unsigned offset)
  488. {
  489. struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
  490. void __iomem *base = priv->reg_base;
  491. u32 data;
  492. data = readl(base + GPIO_IN);
  493. data >>= offset;
  494. data &= 1;
  495. return data;
  496. }
  497. /* gpiolib gpio_direction_input callback function */
  498. static int exynos5440_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  499. {
  500. struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
  501. void __iomem *base = priv->reg_base;
  502. u32 data;
  503. /* first disable the data output enable on this pin */
  504. data = readl(base + GPIO_OE);
  505. data &= ~(1 << offset);
  506. writel(data, base + GPIO_OE);
  507. /* now enable input on this pin */
  508. data = readl(base + GPIO_IE);
  509. data |= 1 << offset;
  510. writel(data, base + GPIO_IE);
  511. return 0;
  512. }
  513. /* gpiolib gpio_direction_output callback function */
  514. static int exynos5440_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  515. int value)
  516. {
  517. struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
  518. void __iomem *base = priv->reg_base;
  519. u32 data;
  520. exynos5440_gpio_set(gc, offset, value);
  521. /* first disable the data input enable on this pin */
  522. data = readl(base + GPIO_IE);
  523. data &= ~(1 << offset);
  524. writel(data, base + GPIO_IE);
  525. /* now enable output on this pin */
  526. data = readl(base + GPIO_OE);
  527. data |= 1 << offset;
  528. writel(data, base + GPIO_OE);
  529. return 0;
  530. }
  531. /* gpiolib gpio_to_irq callback function */
  532. static int exynos5440_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  533. {
  534. struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
  535. unsigned int virq;
  536. if (offset < 16 || offset > 23)
  537. return -ENXIO;
  538. if (!priv->irq_domain)
  539. return -ENXIO;
  540. virq = irq_create_mapping(priv->irq_domain, offset - 16);
  541. return virq ? : -ENXIO;
  542. }
  543. /* parse the pin numbers listed in the 'samsung,exynos5440-pins' property */
  544. static int exynos5440_pinctrl_parse_dt_pins(struct platform_device *pdev,
  545. struct device_node *cfg_np, unsigned int **pin_list,
  546. unsigned int *npins)
  547. {
  548. struct device *dev = &pdev->dev;
  549. struct property *prop;
  550. prop = of_find_property(cfg_np, "samsung,exynos5440-pins", NULL);
  551. if (!prop)
  552. return -ENOENT;
  553. *npins = prop->length / sizeof(unsigned long);
  554. if (!*npins) {
  555. dev_err(dev, "invalid pin list in %s node", cfg_np->name);
  556. return -EINVAL;
  557. }
  558. *pin_list = devm_kzalloc(dev, *npins * sizeof(**pin_list), GFP_KERNEL);
  559. if (!*pin_list)
  560. return -ENOMEM;
  561. return of_property_read_u32_array(cfg_np, "samsung,exynos5440-pins",
  562. *pin_list, *npins);
  563. }
  564. /*
  565. * Parse the information about all the available pin groups and pin functions
  566. * from device node of the pin-controller.
  567. */
  568. static int exynos5440_pinctrl_parse_dt(struct platform_device *pdev,
  569. struct exynos5440_pinctrl_priv_data *priv)
  570. {
  571. struct device *dev = &pdev->dev;
  572. struct device_node *dev_np = dev->of_node;
  573. struct device_node *cfg_np;
  574. struct exynos5440_pin_group *groups, *grp;
  575. struct exynos5440_pmx_func *functions, *func;
  576. unsigned *pin_list;
  577. unsigned int npins, grp_cnt, func_idx = 0;
  578. char *gname, *fname;
  579. int ret;
  580. grp_cnt = of_get_child_count(dev_np);
  581. if (!grp_cnt)
  582. return -EINVAL;
  583. groups = devm_kzalloc(dev, grp_cnt * sizeof(*groups), GFP_KERNEL);
  584. if (!groups)
  585. return -EINVAL;
  586. grp = groups;
  587. functions = devm_kzalloc(dev, grp_cnt * sizeof(*functions), GFP_KERNEL);
  588. if (!functions)
  589. return -EINVAL;
  590. func = functions;
  591. /*
  592. * Iterate over all the child nodes of the pin controller node
  593. * and create pin groups and pin function lists.
  594. */
  595. for_each_child_of_node(dev_np, cfg_np) {
  596. u32 function;
  597. ret = exynos5440_pinctrl_parse_dt_pins(pdev, cfg_np,
  598. &pin_list, &npins);
  599. if (ret) {
  600. gname = NULL;
  601. goto skip_to_pin_function;
  602. }
  603. /* derive pin group name from the node name */
  604. gname = devm_kasprintf(dev, GFP_KERNEL,
  605. "%s%s", cfg_np->name, GROUP_SUFFIX);
  606. if (!gname)
  607. return -ENOMEM;
  608. grp->name = gname;
  609. grp->pins = pin_list;
  610. grp->num_pins = npins;
  611. grp++;
  612. skip_to_pin_function:
  613. ret = of_property_read_u32(cfg_np, "samsung,exynos5440-pin-function",
  614. &function);
  615. if (ret)
  616. continue;
  617. /* derive function name from the node name */
  618. fname = devm_kasprintf(dev, GFP_KERNEL,
  619. "%s%s", cfg_np->name, FUNCTION_SUFFIX);
  620. if (!fname)
  621. return -ENOMEM;
  622. func->name = fname;
  623. func->groups = devm_kzalloc(dev, sizeof(char *), GFP_KERNEL);
  624. if (!func->groups)
  625. return -ENOMEM;
  626. func->groups[0] = gname;
  627. func->num_groups = gname ? 1 : 0;
  628. func->function = function;
  629. func++;
  630. func_idx++;
  631. }
  632. priv->pin_groups = groups;
  633. priv->nr_groups = grp_cnt;
  634. priv->pmx_functions = functions;
  635. priv->nr_functions = func_idx;
  636. return 0;
  637. }
  638. /* register the pinctrl interface with the pinctrl subsystem */
  639. static int exynos5440_pinctrl_register(struct platform_device *pdev,
  640. struct exynos5440_pinctrl_priv_data *priv)
  641. {
  642. struct device *dev = &pdev->dev;
  643. struct pinctrl_desc *ctrldesc;
  644. struct pinctrl_dev *pctl_dev;
  645. struct pinctrl_pin_desc *pindesc, *pdesc;
  646. char *pin_names;
  647. int pin, ret;
  648. ctrldesc = devm_kzalloc(dev, sizeof(*ctrldesc), GFP_KERNEL);
  649. if (!ctrldesc)
  650. return -ENOMEM;
  651. ctrldesc->name = "exynos5440-pinctrl";
  652. ctrldesc->owner = THIS_MODULE;
  653. ctrldesc->pctlops = &exynos5440_pctrl_ops;
  654. ctrldesc->pmxops = &exynos5440_pinmux_ops;
  655. ctrldesc->confops = &exynos5440_pinconf_ops;
  656. pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
  657. EXYNOS5440_MAX_PINS, GFP_KERNEL);
  658. if (!pindesc)
  659. return -ENOMEM;
  660. ctrldesc->pins = pindesc;
  661. ctrldesc->npins = EXYNOS5440_MAX_PINS;
  662. /* dynamically populate the pin number and pin name for pindesc */
  663. for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
  664. pdesc->number = pin;
  665. /*
  666. * allocate space for storing the dynamically generated names for all
  667. * the pins which belong to this pin-controller.
  668. */
  669. pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
  670. ctrldesc->npins, GFP_KERNEL);
  671. if (!pin_names)
  672. return -ENOMEM;
  673. /* for each pin, set the name of the pin */
  674. for (pin = 0; pin < ctrldesc->npins; pin++) {
  675. snprintf(pin_names, 6, "gpio%02d", pin);
  676. pdesc = pindesc + pin;
  677. pdesc->name = pin_names;
  678. pin_names += PIN_NAME_LENGTH;
  679. }
  680. ret = exynos5440_pinctrl_parse_dt(pdev, priv);
  681. if (ret)
  682. return ret;
  683. pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, priv);
  684. if (IS_ERR(pctl_dev)) {
  685. dev_err(&pdev->dev, "could not register pinctrl driver\n");
  686. return PTR_ERR(pctl_dev);
  687. }
  688. priv->range.name = "exynos5440-pctrl-gpio-range";
  689. priv->range.id = 0;
  690. priv->range.base = 0;
  691. priv->range.npins = EXYNOS5440_MAX_PINS;
  692. priv->range.gc = priv->gc;
  693. pinctrl_add_gpio_range(pctl_dev, &priv->range);
  694. return 0;
  695. }
  696. /* register the gpiolib interface with the gpiolib subsystem */
  697. static int exynos5440_gpiolib_register(struct platform_device *pdev,
  698. struct exynos5440_pinctrl_priv_data *priv)
  699. {
  700. struct gpio_chip *gc;
  701. int ret;
  702. gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
  703. if (!gc)
  704. return -ENOMEM;
  705. priv->gc = gc;
  706. gc->base = 0;
  707. gc->ngpio = EXYNOS5440_MAX_PINS;
  708. gc->parent = &pdev->dev;
  709. gc->set = exynos5440_gpio_set;
  710. gc->get = exynos5440_gpio_get;
  711. gc->direction_input = exynos5440_gpio_direction_input;
  712. gc->direction_output = exynos5440_gpio_direction_output;
  713. gc->to_irq = exynos5440_gpio_to_irq;
  714. gc->label = "gpiolib-exynos5440";
  715. gc->owner = THIS_MODULE;
  716. ret = gpiochip_add_data(gc, priv);
  717. if (ret) {
  718. dev_err(&pdev->dev, "failed to register gpio_chip %s, error "
  719. "code: %d\n", gc->label, ret);
  720. return ret;
  721. }
  722. return 0;
  723. }
  724. /* unregister the gpiolib interface with the gpiolib subsystem */
  725. static int exynos5440_gpiolib_unregister(struct platform_device *pdev,
  726. struct exynos5440_pinctrl_priv_data *priv)
  727. {
  728. gpiochip_remove(priv->gc);
  729. return 0;
  730. }
  731. static void exynos5440_gpio_irq_unmask(struct irq_data *irqd)
  732. {
  733. struct exynos5440_pinctrl_priv_data *d;
  734. unsigned long gpio_int;
  735. d = irq_data_get_irq_chip_data(irqd);
  736. gpio_int = readl(d->reg_base + GPIO_INT);
  737. gpio_int |= 1 << irqd->hwirq;
  738. writel(gpio_int, d->reg_base + GPIO_INT);
  739. }
  740. static void exynos5440_gpio_irq_mask(struct irq_data *irqd)
  741. {
  742. struct exynos5440_pinctrl_priv_data *d;
  743. unsigned long gpio_int;
  744. d = irq_data_get_irq_chip_data(irqd);
  745. gpio_int = readl(d->reg_base + GPIO_INT);
  746. gpio_int &= ~(1 << irqd->hwirq);
  747. writel(gpio_int, d->reg_base + GPIO_INT);
  748. }
  749. /* irq_chip for gpio interrupts */
  750. static struct irq_chip exynos5440_gpio_irq_chip = {
  751. .name = "exynos5440_gpio_irq_chip",
  752. .irq_unmask = exynos5440_gpio_irq_unmask,
  753. .irq_mask = exynos5440_gpio_irq_mask,
  754. };
  755. /* interrupt handler for GPIO interrupts 0..7 */
  756. static irqreturn_t exynos5440_gpio_irq(int irq, void *data)
  757. {
  758. struct exynos5440_gpio_intr_data *intd = data;
  759. struct exynos5440_pinctrl_priv_data *d = intd->priv;
  760. int virq;
  761. virq = irq_linear_revmap(d->irq_domain, intd->gpio_int);
  762. if (!virq)
  763. return IRQ_NONE;
  764. generic_handle_irq(virq);
  765. return IRQ_HANDLED;
  766. }
  767. static int exynos5440_gpio_irq_map(struct irq_domain *h, unsigned int virq,
  768. irq_hw_number_t hw)
  769. {
  770. struct exynos5440_pinctrl_priv_data *d = h->host_data;
  771. irq_set_chip_data(virq, d);
  772. irq_set_chip_and_handler(virq, &exynos5440_gpio_irq_chip,
  773. handle_level_irq);
  774. return 0;
  775. }
  776. /* irq domain callbacks for gpio interrupt controller */
  777. static const struct irq_domain_ops exynos5440_gpio_irqd_ops = {
  778. .map = exynos5440_gpio_irq_map,
  779. .xlate = irq_domain_xlate_twocell,
  780. };
  781. /* setup handling of gpio interrupts */
  782. static int exynos5440_gpio_irq_init(struct platform_device *pdev,
  783. struct exynos5440_pinctrl_priv_data *priv)
  784. {
  785. struct device *dev = &pdev->dev;
  786. struct exynos5440_gpio_intr_data *intd;
  787. int i, irq, ret;
  788. intd = devm_kzalloc(dev, sizeof(*intd) * EXYNOS5440_MAX_GPIO_INT,
  789. GFP_KERNEL);
  790. if (!intd)
  791. return -ENOMEM;
  792. for (i = 0; i < EXYNOS5440_MAX_GPIO_INT; i++) {
  793. irq = irq_of_parse_and_map(dev->of_node, i);
  794. if (irq <= 0) {
  795. dev_err(dev, "irq parsing failed\n");
  796. return -EINVAL;
  797. }
  798. intd->gpio_int = i;
  799. intd->priv = priv;
  800. ret = devm_request_irq(dev, irq, exynos5440_gpio_irq,
  801. 0, dev_name(dev), intd++);
  802. if (ret) {
  803. dev_err(dev, "irq request failed\n");
  804. return -ENXIO;
  805. }
  806. }
  807. priv->irq_domain = irq_domain_add_linear(dev->of_node,
  808. EXYNOS5440_MAX_GPIO_INT,
  809. &exynos5440_gpio_irqd_ops, priv);
  810. if (!priv->irq_domain) {
  811. dev_err(dev, "failed to create irq domain\n");
  812. return -ENXIO;
  813. }
  814. return 0;
  815. }
  816. static int exynos5440_pinctrl_probe(struct platform_device *pdev)
  817. {
  818. struct device *dev = &pdev->dev;
  819. struct exynos5440_pinctrl_priv_data *priv;
  820. struct resource *res;
  821. int ret;
  822. if (!dev->of_node) {
  823. dev_err(dev, "device tree node not found\n");
  824. return -ENODEV;
  825. }
  826. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  827. if (!priv)
  828. return -ENOMEM;
  829. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  830. priv->reg_base = devm_ioremap_resource(&pdev->dev, res);
  831. if (IS_ERR(priv->reg_base))
  832. return PTR_ERR(priv->reg_base);
  833. ret = exynos5440_gpiolib_register(pdev, priv);
  834. if (ret)
  835. return ret;
  836. ret = exynos5440_pinctrl_register(pdev, priv);
  837. if (ret) {
  838. exynos5440_gpiolib_unregister(pdev, priv);
  839. return ret;
  840. }
  841. ret = exynos5440_gpio_irq_init(pdev, priv);
  842. if (ret) {
  843. dev_err(dev, "failed to setup gpio interrupts\n");
  844. return ret;
  845. }
  846. platform_set_drvdata(pdev, priv);
  847. dev_info(dev, "EXYNOS5440 pinctrl driver registered\n");
  848. return 0;
  849. }
  850. static const struct of_device_id exynos5440_pinctrl_dt_match[] = {
  851. { .compatible = "samsung,exynos5440-pinctrl" },
  852. {},
  853. };
  854. MODULE_DEVICE_TABLE(of, exynos5440_pinctrl_dt_match);
  855. static struct platform_driver exynos5440_pinctrl_driver = {
  856. .probe = exynos5440_pinctrl_probe,
  857. .driver = {
  858. .name = "exynos5440-pinctrl",
  859. .of_match_table = exynos5440_pinctrl_dt_match,
  860. .suppress_bind_attrs = true,
  861. },
  862. };
  863. static int __init exynos5440_pinctrl_drv_register(void)
  864. {
  865. return platform_driver_register(&exynos5440_pinctrl_driver);
  866. }
  867. postcore_initcall(exynos5440_pinctrl_drv_register);
  868. static void __exit exynos5440_pinctrl_drv_unregister(void)
  869. {
  870. platform_driver_unregister(&exynos5440_pinctrl_driver);
  871. }
  872. module_exit(exynos5440_pinctrl_drv_unregister);
  873. MODULE_AUTHOR("Thomas Abraham <[email protected]>");
  874. MODULE_DESCRIPTION("Samsung EXYNOS5440 SoC pinctrl driver");
  875. MODULE_LICENSE("GPL v2");