pinctrl-exynos5440.c 29 KB

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