pinctrl-samsung.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * pin-controller/pin-mux/pin-config/gpio-driver for Samsung's SoC's.
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com
  6. * Copyright (c) 2012 Linaro Ltd
  7. * http://www.linaro.org
  8. *
  9. * Author: Thomas Abraham <thomas.ab@samsung.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This driver implements the Samsung pinctrl driver. It supports setting up of
  17. * pinmux and pinconf configurations. The gpiolib interface is also included.
  18. * External interrupt (gpio and wakeup) support are not included in this driver
  19. * but provides extensions to which platform specific implementation of the gpio
  20. * and wakeup interrupts can be hooked to.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/io.h>
  25. #include <linux/slab.h>
  26. #include <linux/err.h>
  27. #include <linux/gpio.h>
  28. #include "core.h"
  29. #include "pinctrl-samsung.h"
  30. #define GROUP_SUFFIX "-grp"
  31. #define GSUFFIX_LEN sizeof(GROUP_SUFFIX)
  32. #define FUNCTION_SUFFIX "-mux"
  33. #define FSUFFIX_LEN sizeof(FUNCTION_SUFFIX)
  34. /* list of all possible config options supported */
  35. struct pin_config {
  36. char *prop_cfg;
  37. unsigned int cfg_type;
  38. } pcfgs[] = {
  39. { "samsung,pin-pud", PINCFG_TYPE_PUD },
  40. { "samsung,pin-drv", PINCFG_TYPE_DRV },
  41. { "samsung,pin-con-pdn", PINCFG_TYPE_CON_PDN },
  42. { "samsung,pin-pud-pdn", PINCFG_TYPE_PUD_PDN },
  43. };
  44. /* check if the selector is a valid pin group selector */
  45. static int samsung_get_group_count(struct pinctrl_dev *pctldev)
  46. {
  47. struct samsung_pinctrl_drv_data *drvdata;
  48. drvdata = pinctrl_dev_get_drvdata(pctldev);
  49. return drvdata->nr_groups;
  50. }
  51. /* return the name of the group selected by the group selector */
  52. static const char *samsung_get_group_name(struct pinctrl_dev *pctldev,
  53. unsigned selector)
  54. {
  55. struct samsung_pinctrl_drv_data *drvdata;
  56. drvdata = pinctrl_dev_get_drvdata(pctldev);
  57. return drvdata->pin_groups[selector].name;
  58. }
  59. /* return the pin numbers associated with the specified group */
  60. static int samsung_get_group_pins(struct pinctrl_dev *pctldev,
  61. unsigned selector, const unsigned **pins, unsigned *num_pins)
  62. {
  63. struct samsung_pinctrl_drv_data *drvdata;
  64. drvdata = pinctrl_dev_get_drvdata(pctldev);
  65. *pins = drvdata->pin_groups[selector].pins;
  66. *num_pins = drvdata->pin_groups[selector].num_pins;
  67. return 0;
  68. }
  69. /* create pinctrl_map entries by parsing device tree nodes */
  70. static int samsung_dt_node_to_map(struct pinctrl_dev *pctldev,
  71. struct device_node *np, struct pinctrl_map **maps,
  72. unsigned *nmaps)
  73. {
  74. struct device *dev = pctldev->dev;
  75. struct pinctrl_map *map;
  76. unsigned long *cfg = NULL;
  77. char *gname, *fname;
  78. int cfg_cnt = 0, map_cnt = 0, idx = 0;
  79. /* count the number of config options specfied in the node */
  80. for (idx = 0; idx < ARRAY_SIZE(pcfgs); idx++) {
  81. if (of_find_property(np, pcfgs[idx].prop_cfg, NULL))
  82. cfg_cnt++;
  83. }
  84. /*
  85. * Find out the number of map entries to create. All the config options
  86. * can be accomadated into a single config map entry.
  87. */
  88. if (cfg_cnt)
  89. map_cnt = 1;
  90. if (of_find_property(np, "samsung,pin-function", NULL))
  91. map_cnt++;
  92. if (!map_cnt) {
  93. dev_err(dev, "node %s does not have either config or function "
  94. "configurations\n", np->name);
  95. return -EINVAL;
  96. }
  97. /* Allocate memory for pin-map entries */
  98. map = kzalloc(sizeof(*map) * map_cnt, GFP_KERNEL);
  99. if (!map) {
  100. dev_err(dev, "could not alloc memory for pin-maps\n");
  101. return -ENOMEM;
  102. }
  103. *nmaps = 0;
  104. /*
  105. * Allocate memory for pin group name. The pin group name is derived
  106. * from the node name from which these map entries are be created.
  107. */
  108. gname = kzalloc(strlen(np->name) + GSUFFIX_LEN, GFP_KERNEL);
  109. if (!gname) {
  110. dev_err(dev, "failed to alloc memory for group name\n");
  111. goto free_map;
  112. }
  113. sprintf(gname, "%s%s", np->name, GROUP_SUFFIX);
  114. /*
  115. * don't have config options? then skip over to creating function
  116. * map entries.
  117. */
  118. if (!cfg_cnt)
  119. goto skip_cfgs;
  120. /* Allocate memory for config entries */
  121. cfg = kzalloc(sizeof(*cfg) * cfg_cnt, GFP_KERNEL);
  122. if (!cfg) {
  123. dev_err(dev, "failed to alloc memory for configs\n");
  124. goto free_gname;
  125. }
  126. /* Prepare a list of config settings */
  127. for (idx = 0, cfg_cnt = 0; idx < ARRAY_SIZE(pcfgs); idx++) {
  128. u32 value;
  129. if (!of_property_read_u32(np, pcfgs[idx].prop_cfg, &value))
  130. cfg[cfg_cnt++] =
  131. PINCFG_PACK(pcfgs[idx].cfg_type, value);
  132. }
  133. /* create the config map entry */
  134. map[*nmaps].data.configs.group_or_pin = gname;
  135. map[*nmaps].data.configs.configs = cfg;
  136. map[*nmaps].data.configs.num_configs = cfg_cnt;
  137. map[*nmaps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
  138. *nmaps += 1;
  139. skip_cfgs:
  140. /* create the function map entry */
  141. if (of_find_property(np, "samsung,pin-function", NULL)) {
  142. fname = kzalloc(strlen(np->name) + FSUFFIX_LEN, GFP_KERNEL);
  143. if (!fname) {
  144. dev_err(dev, "failed to alloc memory for func name\n");
  145. goto free_cfg;
  146. }
  147. sprintf(fname, "%s%s", np->name, FUNCTION_SUFFIX);
  148. map[*nmaps].data.mux.group = gname;
  149. map[*nmaps].data.mux.function = fname;
  150. map[*nmaps].type = PIN_MAP_TYPE_MUX_GROUP;
  151. *nmaps += 1;
  152. }
  153. *maps = map;
  154. return 0;
  155. free_cfg:
  156. kfree(cfg);
  157. free_gname:
  158. kfree(gname);
  159. free_map:
  160. kfree(map);
  161. return -ENOMEM;
  162. }
  163. /* free the memory allocated to hold the pin-map table */
  164. static void samsung_dt_free_map(struct pinctrl_dev *pctldev,
  165. struct pinctrl_map *map, unsigned num_maps)
  166. {
  167. int idx;
  168. for (idx = 0; idx < num_maps; idx++) {
  169. if (map[idx].type == PIN_MAP_TYPE_MUX_GROUP) {
  170. kfree(map[idx].data.mux.function);
  171. if (!idx)
  172. kfree(map[idx].data.mux.group);
  173. } else if (map->type == PIN_MAP_TYPE_CONFIGS_GROUP) {
  174. kfree(map[idx].data.configs.configs);
  175. if (!idx)
  176. kfree(map[idx].data.configs.group_or_pin);
  177. }
  178. };
  179. kfree(map);
  180. }
  181. /* list of pinctrl callbacks for the pinctrl core */
  182. static struct pinctrl_ops samsung_pctrl_ops = {
  183. .get_groups_count = samsung_get_group_count,
  184. .get_group_name = samsung_get_group_name,
  185. .get_group_pins = samsung_get_group_pins,
  186. .dt_node_to_map = samsung_dt_node_to_map,
  187. .dt_free_map = samsung_dt_free_map,
  188. };
  189. /* check if the selector is a valid pin function selector */
  190. static int samsung_get_functions_count(struct pinctrl_dev *pctldev)
  191. {
  192. struct samsung_pinctrl_drv_data *drvdata;
  193. drvdata = pinctrl_dev_get_drvdata(pctldev);
  194. return drvdata->nr_functions;
  195. }
  196. /* return the name of the pin function specified */
  197. static const char *samsung_pinmux_get_fname(struct pinctrl_dev *pctldev,
  198. unsigned selector)
  199. {
  200. struct samsung_pinctrl_drv_data *drvdata;
  201. drvdata = pinctrl_dev_get_drvdata(pctldev);
  202. return drvdata->pmx_functions[selector].name;
  203. }
  204. /* return the groups associated for the specified function selector */
  205. static int samsung_pinmux_get_groups(struct pinctrl_dev *pctldev,
  206. unsigned selector, const char * const **groups,
  207. unsigned * const num_groups)
  208. {
  209. struct samsung_pinctrl_drv_data *drvdata;
  210. drvdata = pinctrl_dev_get_drvdata(pctldev);
  211. *groups = drvdata->pmx_functions[selector].groups;
  212. *num_groups = drvdata->pmx_functions[selector].num_groups;
  213. return 0;
  214. }
  215. /*
  216. * given a pin number that is local to a pin controller, find out the pin bank
  217. * and the register base of the pin bank.
  218. */
  219. static void pin_to_reg_bank(struct gpio_chip *gc, unsigned pin,
  220. void __iomem **reg, u32 *offset,
  221. struct samsung_pin_bank **bank)
  222. {
  223. struct samsung_pinctrl_drv_data *drvdata;
  224. struct samsung_pin_bank *b;
  225. drvdata = dev_get_drvdata(gc->dev);
  226. b = drvdata->ctrl->pin_banks;
  227. while ((pin >= b->pin_base) &&
  228. ((b->pin_base + b->nr_pins - 1) < pin))
  229. b++;
  230. *reg = drvdata->virt_base + b->pctl_offset;
  231. *offset = pin - b->pin_base;
  232. if (bank)
  233. *bank = b;
  234. /* some banks have two config registers in a single bank */
  235. if (*offset * b->func_width > BITS_PER_LONG)
  236. *reg += 4;
  237. }
  238. /* enable or disable a pinmux function */
  239. static void samsung_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
  240. unsigned group, bool enable)
  241. {
  242. struct samsung_pinctrl_drv_data *drvdata;
  243. const unsigned int *pins;
  244. struct samsung_pin_bank *bank;
  245. void __iomem *reg;
  246. u32 mask, shift, data, pin_offset, cnt;
  247. drvdata = pinctrl_dev_get_drvdata(pctldev);
  248. pins = drvdata->pin_groups[group].pins;
  249. /*
  250. * for each pin in the pin group selected, program the correspoding pin
  251. * pin function number in the config register.
  252. */
  253. for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++) {
  254. pin_to_reg_bank(drvdata->gc, pins[cnt] - drvdata->ctrl->base,
  255. &reg, &pin_offset, &bank);
  256. mask = (1 << bank->func_width) - 1;
  257. shift = pin_offset * bank->func_width;
  258. data = readl(reg);
  259. data &= ~(mask << shift);
  260. if (enable)
  261. data |= drvdata->pin_groups[group].func << shift;
  262. writel(data, reg);
  263. }
  264. }
  265. /* enable a specified pinmux by writing to registers */
  266. static int samsung_pinmux_enable(struct pinctrl_dev *pctldev, unsigned selector,
  267. unsigned group)
  268. {
  269. samsung_pinmux_setup(pctldev, selector, group, true);
  270. return 0;
  271. }
  272. /* disable a specified pinmux by writing to registers */
  273. static void samsung_pinmux_disable(struct pinctrl_dev *pctldev,
  274. unsigned selector, unsigned group)
  275. {
  276. samsung_pinmux_setup(pctldev, selector, group, false);
  277. }
  278. /*
  279. * The calls to gpio_direction_output() and gpio_direction_input()
  280. * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
  281. * function called from the gpiolib interface).
  282. */
  283. static int samsung_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
  284. struct pinctrl_gpio_range *range, unsigned offset, bool input)
  285. {
  286. struct samsung_pin_bank *bank;
  287. void __iomem *reg;
  288. u32 data, pin_offset, mask, shift;
  289. pin_to_reg_bank(range->gc, offset, &reg, &pin_offset, &bank);
  290. mask = (1 << bank->func_width) - 1;
  291. shift = pin_offset * bank->func_width;
  292. data = readl(reg);
  293. data &= ~(mask << shift);
  294. if (!input)
  295. data |= FUNC_OUTPUT << shift;
  296. writel(data, reg);
  297. return 0;
  298. }
  299. /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
  300. static struct pinmux_ops samsung_pinmux_ops = {
  301. .get_functions_count = samsung_get_functions_count,
  302. .get_function_name = samsung_pinmux_get_fname,
  303. .get_function_groups = samsung_pinmux_get_groups,
  304. .enable = samsung_pinmux_enable,
  305. .disable = samsung_pinmux_disable,
  306. .gpio_set_direction = samsung_pinmux_gpio_set_direction,
  307. };
  308. /* set or get the pin config settings for a specified pin */
  309. static int samsung_pinconf_rw(struct pinctrl_dev *pctldev, unsigned int pin,
  310. unsigned long *config, bool set)
  311. {
  312. struct samsung_pinctrl_drv_data *drvdata;
  313. struct samsung_pin_bank *bank;
  314. void __iomem *reg_base;
  315. enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config);
  316. u32 data, width, pin_offset, mask, shift;
  317. u32 cfg_value, cfg_reg;
  318. drvdata = pinctrl_dev_get_drvdata(pctldev);
  319. pin_to_reg_bank(drvdata->gc, pin - drvdata->ctrl->base, &reg_base,
  320. &pin_offset, &bank);
  321. switch (cfg_type) {
  322. case PINCFG_TYPE_PUD:
  323. width = bank->pud_width;
  324. cfg_reg = PUD_REG;
  325. break;
  326. case PINCFG_TYPE_DRV:
  327. width = bank->drv_width;
  328. cfg_reg = DRV_REG;
  329. break;
  330. case PINCFG_TYPE_CON_PDN:
  331. width = bank->conpdn_width;
  332. cfg_reg = CONPDN_REG;
  333. break;
  334. case PINCFG_TYPE_PUD_PDN:
  335. width = bank->pudpdn_width;
  336. cfg_reg = PUDPDN_REG;
  337. break;
  338. default:
  339. WARN_ON(1);
  340. return -EINVAL;
  341. }
  342. mask = (1 << width) - 1;
  343. shift = pin_offset * width;
  344. data = readl(reg_base + cfg_reg);
  345. if (set) {
  346. cfg_value = PINCFG_UNPACK_VALUE(*config);
  347. data &= ~(mask << shift);
  348. data |= (cfg_value << shift);
  349. writel(data, reg_base + cfg_reg);
  350. } else {
  351. data >>= shift;
  352. data &= mask;
  353. *config = PINCFG_PACK(cfg_type, data);
  354. }
  355. return 0;
  356. }
  357. /* set the pin config settings for a specified pin */
  358. static int samsung_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  359. unsigned long config)
  360. {
  361. return samsung_pinconf_rw(pctldev, pin, &config, true);
  362. }
  363. /* get the pin config settings for a specified pin */
  364. static int samsung_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  365. unsigned long *config)
  366. {
  367. return samsung_pinconf_rw(pctldev, pin, config, false);
  368. }
  369. /* set the pin config settings for a specified pin group */
  370. static int samsung_pinconf_group_set(struct pinctrl_dev *pctldev,
  371. unsigned group, unsigned long config)
  372. {
  373. struct samsung_pinctrl_drv_data *drvdata;
  374. const unsigned int *pins;
  375. unsigned int cnt;
  376. drvdata = pinctrl_dev_get_drvdata(pctldev);
  377. pins = drvdata->pin_groups[group].pins;
  378. for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++)
  379. samsung_pinconf_set(pctldev, pins[cnt], config);
  380. return 0;
  381. }
  382. /* get the pin config settings for a specified pin group */
  383. static int samsung_pinconf_group_get(struct pinctrl_dev *pctldev,
  384. unsigned int group, unsigned long *config)
  385. {
  386. struct samsung_pinctrl_drv_data *drvdata;
  387. const unsigned int *pins;
  388. drvdata = pinctrl_dev_get_drvdata(pctldev);
  389. pins = drvdata->pin_groups[group].pins;
  390. samsung_pinconf_get(pctldev, pins[0], config);
  391. return 0;
  392. }
  393. /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
  394. static struct pinconf_ops samsung_pinconf_ops = {
  395. .pin_config_get = samsung_pinconf_get,
  396. .pin_config_set = samsung_pinconf_set,
  397. .pin_config_group_get = samsung_pinconf_group_get,
  398. .pin_config_group_set = samsung_pinconf_group_set,
  399. };
  400. /* gpiolib gpio_set callback function */
  401. static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  402. {
  403. void __iomem *reg;
  404. u32 pin_offset, data;
  405. pin_to_reg_bank(gc, offset, &reg, &pin_offset, NULL);
  406. data = readl(reg + DAT_REG);
  407. data &= ~(1 << pin_offset);
  408. if (value)
  409. data |= 1 << pin_offset;
  410. writel(data, reg + DAT_REG);
  411. }
  412. /* gpiolib gpio_get callback function */
  413. static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset)
  414. {
  415. void __iomem *reg;
  416. u32 pin_offset, data;
  417. pin_to_reg_bank(gc, offset, &reg, &pin_offset, NULL);
  418. data = readl(reg + DAT_REG);
  419. data >>= pin_offset;
  420. data &= 1;
  421. return data;
  422. }
  423. /*
  424. * gpiolib gpio_direction_input callback function. The setting of the pin
  425. * mux function as 'gpio input' will be handled by the pinctrl susbsystem
  426. * interface.
  427. */
  428. static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  429. {
  430. return pinctrl_gpio_direction_input(gc->base + offset);
  431. }
  432. /*
  433. * gpiolib gpio_direction_output callback function. The setting of the pin
  434. * mux function as 'gpio output' will be handled by the pinctrl susbsystem
  435. * interface.
  436. */
  437. static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  438. int value)
  439. {
  440. samsung_gpio_set(gc, offset, value);
  441. return pinctrl_gpio_direction_output(gc->base + offset);
  442. }
  443. /*
  444. * Parse the pin names listed in the 'samsung,pins' property and convert it
  445. * into a list of gpio numbers are create a pin group from it.
  446. */
  447. static int __init samsung_pinctrl_parse_dt_pins(struct platform_device *pdev,
  448. struct device_node *cfg_np, struct pinctrl_desc *pctl,
  449. unsigned int **pin_list, unsigned int *npins)
  450. {
  451. struct device *dev = &pdev->dev;
  452. struct property *prop;
  453. struct pinctrl_pin_desc const *pdesc = pctl->pins;
  454. unsigned int idx = 0, cnt;
  455. const char *pin_name;
  456. *npins = of_property_count_strings(cfg_np, "samsung,pins");
  457. if (*npins < 0) {
  458. dev_err(dev, "invalid pin list in %s node", cfg_np->name);
  459. return -EINVAL;
  460. }
  461. *pin_list = devm_kzalloc(dev, *npins * sizeof(**pin_list), GFP_KERNEL);
  462. if (!*pin_list) {
  463. dev_err(dev, "failed to allocate memory for pin list\n");
  464. return -ENOMEM;
  465. }
  466. of_property_for_each_string(cfg_np, "samsung,pins", prop, pin_name) {
  467. for (cnt = 0; cnt < pctl->npins; cnt++) {
  468. if (pdesc[cnt].name) {
  469. if (!strcmp(pin_name, pdesc[cnt].name)) {
  470. (*pin_list)[idx++] = pdesc[cnt].number;
  471. break;
  472. }
  473. }
  474. }
  475. if (cnt == pctl->npins) {
  476. dev_err(dev, "pin %s not valid in %s node\n",
  477. pin_name, cfg_np->name);
  478. devm_kfree(dev, *pin_list);
  479. return -EINVAL;
  480. }
  481. }
  482. return 0;
  483. }
  484. /*
  485. * Parse the information about all the available pin groups and pin functions
  486. * from device node of the pin-controller. A pin group is formed with all
  487. * the pins listed in the "samsung,pins" property.
  488. */
  489. static int __init samsung_pinctrl_parse_dt(struct platform_device *pdev,
  490. struct samsung_pinctrl_drv_data *drvdata)
  491. {
  492. struct device *dev = &pdev->dev;
  493. struct device_node *dev_np = dev->of_node;
  494. struct device_node *cfg_np;
  495. struct samsung_pin_group *groups, *grp;
  496. struct samsung_pmx_func *functions, *func;
  497. unsigned *pin_list;
  498. unsigned int npins, grp_cnt, func_idx = 0;
  499. char *gname, *fname;
  500. int ret;
  501. grp_cnt = of_get_child_count(dev_np);
  502. if (!grp_cnt)
  503. return -EINVAL;
  504. groups = devm_kzalloc(dev, grp_cnt * sizeof(*groups), GFP_KERNEL);
  505. if (!groups) {
  506. dev_err(dev, "failed allocate memory for ping group list\n");
  507. return -EINVAL;
  508. }
  509. grp = groups;
  510. functions = devm_kzalloc(dev, grp_cnt * sizeof(*functions), GFP_KERNEL);
  511. if (!functions) {
  512. dev_err(dev, "failed to allocate memory for function list\n");
  513. return -EINVAL;
  514. }
  515. func = functions;
  516. /*
  517. * Iterate over all the child nodes of the pin controller node
  518. * and create pin groups and pin function lists.
  519. */
  520. for_each_child_of_node(dev_np, cfg_np) {
  521. u32 function;
  522. if (of_find_property(cfg_np, "interrupt-controller", NULL))
  523. continue;
  524. ret = samsung_pinctrl_parse_dt_pins(pdev, cfg_np,
  525. &drvdata->pctl, &pin_list, &npins);
  526. if (ret)
  527. return ret;
  528. /* derive pin group name from the node name */
  529. gname = devm_kzalloc(dev, strlen(cfg_np->name) + GSUFFIX_LEN,
  530. GFP_KERNEL);
  531. if (!gname) {
  532. dev_err(dev, "failed to alloc memory for group name\n");
  533. return -ENOMEM;
  534. }
  535. sprintf(gname, "%s%s", cfg_np->name, GROUP_SUFFIX);
  536. grp->name = gname;
  537. grp->pins = pin_list;
  538. grp->num_pins = npins;
  539. of_property_read_u32(cfg_np, "samsung,pin-function", &function);
  540. grp->func = function;
  541. grp++;
  542. if (!of_find_property(cfg_np, "samsung,pin-function", NULL))
  543. continue;
  544. /* derive function name from the node name */
  545. fname = devm_kzalloc(dev, strlen(cfg_np->name) + FSUFFIX_LEN,
  546. GFP_KERNEL);
  547. if (!fname) {
  548. dev_err(dev, "failed to alloc memory for func name\n");
  549. return -ENOMEM;
  550. }
  551. sprintf(fname, "%s%s", cfg_np->name, FUNCTION_SUFFIX);
  552. func->name = fname;
  553. func->groups = devm_kzalloc(dev, sizeof(char *), GFP_KERNEL);
  554. if (!func->groups) {
  555. dev_err(dev, "failed to alloc memory for group list "
  556. "in pin function");
  557. return -ENOMEM;
  558. }
  559. func->groups[0] = gname;
  560. func->num_groups = 1;
  561. func++;
  562. func_idx++;
  563. }
  564. drvdata->pin_groups = groups;
  565. drvdata->nr_groups = grp_cnt;
  566. drvdata->pmx_functions = functions;
  567. drvdata->nr_functions = func_idx;
  568. return 0;
  569. }
  570. /* register the pinctrl interface with the pinctrl subsystem */
  571. static int __init samsung_pinctrl_register(struct platform_device *pdev,
  572. struct samsung_pinctrl_drv_data *drvdata)
  573. {
  574. struct pinctrl_desc *ctrldesc = &drvdata->pctl;
  575. struct pinctrl_pin_desc *pindesc, *pdesc;
  576. struct samsung_pin_bank *pin_bank;
  577. char *pin_names;
  578. int pin, bank, ret;
  579. ctrldesc->name = "samsung-pinctrl";
  580. ctrldesc->owner = THIS_MODULE;
  581. ctrldesc->pctlops = &samsung_pctrl_ops;
  582. ctrldesc->pmxops = &samsung_pinmux_ops;
  583. ctrldesc->confops = &samsung_pinconf_ops;
  584. pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
  585. drvdata->ctrl->nr_pins, GFP_KERNEL);
  586. if (!pindesc) {
  587. dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
  588. return -ENOMEM;
  589. }
  590. ctrldesc->pins = pindesc;
  591. ctrldesc->npins = drvdata->ctrl->nr_pins;
  592. ctrldesc->npins = drvdata->ctrl->nr_pins;
  593. /* dynamically populate the pin number and pin name for pindesc */
  594. for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
  595. pdesc->number = pin + drvdata->ctrl->base;
  596. /*
  597. * allocate space for storing the dynamically generated names for all
  598. * the pins which belong to this pin-controller.
  599. */
  600. pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
  601. drvdata->ctrl->nr_pins, GFP_KERNEL);
  602. if (!pin_names) {
  603. dev_err(&pdev->dev, "mem alloc for pin names failed\n");
  604. return -ENOMEM;
  605. }
  606. /* for each pin, the name of the pin is pin-bank name + pin number */
  607. for (bank = 0; bank < drvdata->ctrl->nr_banks; bank++) {
  608. pin_bank = &drvdata->ctrl->pin_banks[bank];
  609. for (pin = 0; pin < pin_bank->nr_pins; pin++) {
  610. sprintf(pin_names, "%s-%d", pin_bank->name, pin);
  611. pdesc = pindesc + pin_bank->pin_base + pin;
  612. pdesc->name = pin_names;
  613. pin_names += PIN_NAME_LENGTH;
  614. }
  615. }
  616. drvdata->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, drvdata);
  617. if (!drvdata->pctl_dev) {
  618. dev_err(&pdev->dev, "could not register pinctrl driver\n");
  619. return -EINVAL;
  620. }
  621. drvdata->grange.name = "samsung-pctrl-gpio-range";
  622. drvdata->grange.id = 0;
  623. drvdata->grange.base = drvdata->ctrl->base;
  624. drvdata->grange.npins = drvdata->ctrl->nr_pins;
  625. drvdata->grange.gc = drvdata->gc;
  626. pinctrl_add_gpio_range(drvdata->pctl_dev, &drvdata->grange);
  627. ret = samsung_pinctrl_parse_dt(pdev, drvdata);
  628. if (ret) {
  629. pinctrl_unregister(drvdata->pctl_dev);
  630. return ret;
  631. }
  632. return 0;
  633. }
  634. /* register the gpiolib interface with the gpiolib subsystem */
  635. static int __init samsung_gpiolib_register(struct platform_device *pdev,
  636. struct samsung_pinctrl_drv_data *drvdata)
  637. {
  638. struct gpio_chip *gc;
  639. int ret;
  640. gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
  641. if (!gc) {
  642. dev_err(&pdev->dev, "mem alloc for gpio_chip failed\n");
  643. return -ENOMEM;
  644. }
  645. drvdata->gc = gc;
  646. gc->base = drvdata->ctrl->base;
  647. gc->ngpio = drvdata->ctrl->nr_pins;
  648. gc->dev = &pdev->dev;
  649. gc->set = samsung_gpio_set;
  650. gc->get = samsung_gpio_get;
  651. gc->direction_input = samsung_gpio_direction_input;
  652. gc->direction_output = samsung_gpio_direction_output;
  653. gc->label = drvdata->ctrl->label;
  654. gc->owner = THIS_MODULE;
  655. ret = gpiochip_add(gc);
  656. if (ret) {
  657. dev_err(&pdev->dev, "failed to register gpio_chip %s, error "
  658. "code: %d\n", gc->label, ret);
  659. return ret;
  660. }
  661. return 0;
  662. }
  663. /* unregister the gpiolib interface with the gpiolib subsystem */
  664. static int __init samsung_gpiolib_unregister(struct platform_device *pdev,
  665. struct samsung_pinctrl_drv_data *drvdata)
  666. {
  667. int ret = gpiochip_remove(drvdata->gc);
  668. if (ret) {
  669. dev_err(&pdev->dev, "gpio chip remove failed\n");
  670. return ret;
  671. }
  672. return 0;
  673. }
  674. static const struct of_device_id samsung_pinctrl_dt_match[];
  675. /* retrieve the soc specific data */
  676. static struct samsung_pin_ctrl *samsung_pinctrl_get_soc_data(
  677. struct platform_device *pdev)
  678. {
  679. int id;
  680. const struct of_device_id *match;
  681. const struct device_node *node = pdev->dev.of_node;
  682. id = of_alias_get_id(pdev->dev.of_node, "pinctrl");
  683. if (id < 0) {
  684. dev_err(&pdev->dev, "failed to get alias id\n");
  685. return NULL;
  686. }
  687. match = of_match_node(samsung_pinctrl_dt_match, node);
  688. return (struct samsung_pin_ctrl *)match->data + id;
  689. }
  690. static int __devinit samsung_pinctrl_probe(struct platform_device *pdev)
  691. {
  692. struct samsung_pinctrl_drv_data *drvdata;
  693. struct device *dev = &pdev->dev;
  694. struct samsung_pin_ctrl *ctrl;
  695. struct resource *res;
  696. int ret;
  697. if (!dev->of_node) {
  698. dev_err(dev, "device tree node not found\n");
  699. return -ENODEV;
  700. }
  701. ctrl = samsung_pinctrl_get_soc_data(pdev);
  702. if (!ctrl) {
  703. dev_err(&pdev->dev, "driver data not available\n");
  704. return -EINVAL;
  705. }
  706. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  707. if (!drvdata) {
  708. dev_err(dev, "failed to allocate memory for driver's "
  709. "private data\n");
  710. return -ENOMEM;
  711. }
  712. drvdata->ctrl = ctrl;
  713. drvdata->dev = dev;
  714. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  715. if (!res) {
  716. dev_err(dev, "cannot find IO resource\n");
  717. return -ENOENT;
  718. }
  719. drvdata->virt_base = devm_request_and_ioremap(&pdev->dev, res);
  720. if (!drvdata->virt_base) {
  721. dev_err(dev, "ioremap failed\n");
  722. return -ENODEV;
  723. }
  724. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  725. if (res)
  726. drvdata->irq = res->start;
  727. ret = samsung_gpiolib_register(pdev, drvdata);
  728. if (ret)
  729. return ret;
  730. ret = samsung_pinctrl_register(pdev, drvdata);
  731. if (ret) {
  732. samsung_gpiolib_unregister(pdev, drvdata);
  733. return ret;
  734. }
  735. if (ctrl->eint_gpio_init)
  736. ctrl->eint_gpio_init(drvdata);
  737. if (ctrl->eint_wkup_init)
  738. ctrl->eint_wkup_init(drvdata);
  739. platform_set_drvdata(pdev, drvdata);
  740. return 0;
  741. }
  742. static const struct of_device_id samsung_pinctrl_dt_match[] = {
  743. { .compatible = "samsung,pinctrl-exynos4210",
  744. .data = (void *)exynos4210_pin_ctrl },
  745. {},
  746. };
  747. MODULE_DEVICE_TABLE(of, samsung_pinctrl_dt_match);
  748. static struct platform_driver samsung_pinctrl_driver = {
  749. .probe = samsung_pinctrl_probe,
  750. .driver = {
  751. .name = "samsung-pinctrl",
  752. .owner = THIS_MODULE,
  753. .of_match_table = of_match_ptr(samsung_pinctrl_dt_match),
  754. },
  755. };
  756. static int __init samsung_pinctrl_drv_register(void)
  757. {
  758. return platform_driver_register(&samsung_pinctrl_driver);
  759. }
  760. postcore_initcall(samsung_pinctrl_drv_register);
  761. static void __exit samsung_pinctrl_drv_unregister(void)
  762. {
  763. platform_driver_unregister(&samsung_pinctrl_driver);
  764. }
  765. module_exit(samsung_pinctrl_drv_unregister);
  766. MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
  767. MODULE_DESCRIPTION("Samsung pinctrl driver");
  768. MODULE_LICENSE("GPL v2");