pinctrl-exynos5440.c 29 KB

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