pinctrl-exynos5440.c 29 KB

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