pinctrl-exynos5440.c 29 KB

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