pinctrl-exynos5440.c 27 KB

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