pinctrl-exynos5440.c 25 KB

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