pinctrl-samsung.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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 samsung_pinctrl_drv_data *drvdata,
  220. unsigned pin, void __iomem **reg, u32 *offset,
  221. struct samsung_pin_bank **bank)
  222. {
  223. struct samsung_pin_bank *b;
  224. b = drvdata->ctrl->pin_banks;
  225. while ((pin >= b->pin_base) &&
  226. ((b->pin_base + b->nr_pins - 1) < pin))
  227. b++;
  228. *reg = drvdata->virt_base + b->pctl_offset;
  229. *offset = pin - b->pin_base;
  230. if (bank)
  231. *bank = b;
  232. /* some banks have two config registers in a single bank */
  233. if (*offset * b->func_width > BITS_PER_LONG)
  234. *reg += 4;
  235. }
  236. /* enable or disable a pinmux function */
  237. static void samsung_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
  238. unsigned group, bool enable)
  239. {
  240. struct samsung_pinctrl_drv_data *drvdata;
  241. const unsigned int *pins;
  242. struct samsung_pin_bank *bank;
  243. void __iomem *reg;
  244. u32 mask, shift, data, pin_offset, cnt;
  245. drvdata = pinctrl_dev_get_drvdata(pctldev);
  246. pins = drvdata->pin_groups[group].pins;
  247. /*
  248. * for each pin in the pin group selected, program the correspoding pin
  249. * pin function number in the config register.
  250. */
  251. for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++) {
  252. pin_to_reg_bank(drvdata, pins[cnt] - drvdata->ctrl->base,
  253. &reg, &pin_offset, &bank);
  254. mask = (1 << bank->func_width) - 1;
  255. shift = pin_offset * bank->func_width;
  256. data = readl(reg);
  257. data &= ~(mask << shift);
  258. if (enable)
  259. data |= drvdata->pin_groups[group].func << shift;
  260. writel(data, reg);
  261. }
  262. }
  263. /* enable a specified pinmux by writing to registers */
  264. static int samsung_pinmux_enable(struct pinctrl_dev *pctldev, unsigned selector,
  265. unsigned group)
  266. {
  267. samsung_pinmux_setup(pctldev, selector, group, true);
  268. return 0;
  269. }
  270. /* disable a specified pinmux by writing to registers */
  271. static void samsung_pinmux_disable(struct pinctrl_dev *pctldev,
  272. unsigned selector, unsigned group)
  273. {
  274. samsung_pinmux_setup(pctldev, selector, group, false);
  275. }
  276. /*
  277. * The calls to gpio_direction_output() and gpio_direction_input()
  278. * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
  279. * function called from the gpiolib interface).
  280. */
  281. static int samsung_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
  282. struct pinctrl_gpio_range *range, unsigned offset, bool input)
  283. {
  284. struct samsung_pin_bank *bank;
  285. struct samsung_pinctrl_drv_data *drvdata;
  286. void __iomem *reg;
  287. u32 data, pin_offset, mask, shift;
  288. drvdata = pinctrl_dev_get_drvdata(pctldev);
  289. pin_to_reg_bank(drvdata, 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, 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. if (!width)
  343. return -EINVAL;
  344. mask = (1 << width) - 1;
  345. shift = pin_offset * width;
  346. data = readl(reg_base + cfg_reg);
  347. if (set) {
  348. cfg_value = PINCFG_UNPACK_VALUE(*config);
  349. data &= ~(mask << shift);
  350. data |= (cfg_value << shift);
  351. writel(data, reg_base + cfg_reg);
  352. } else {
  353. data >>= shift;
  354. data &= mask;
  355. *config = PINCFG_PACK(cfg_type, data);
  356. }
  357. return 0;
  358. }
  359. /* set the pin config settings for a specified pin */
  360. static int samsung_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  361. unsigned long config)
  362. {
  363. return samsung_pinconf_rw(pctldev, pin, &config, true);
  364. }
  365. /* get the pin config settings for a specified pin */
  366. static int samsung_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  367. unsigned long *config)
  368. {
  369. return samsung_pinconf_rw(pctldev, pin, config, false);
  370. }
  371. /* set the pin config settings for a specified pin group */
  372. static int samsung_pinconf_group_set(struct pinctrl_dev *pctldev,
  373. unsigned group, unsigned long config)
  374. {
  375. struct samsung_pinctrl_drv_data *drvdata;
  376. const unsigned int *pins;
  377. unsigned int cnt;
  378. drvdata = pinctrl_dev_get_drvdata(pctldev);
  379. pins = drvdata->pin_groups[group].pins;
  380. for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++)
  381. samsung_pinconf_set(pctldev, pins[cnt], config);
  382. return 0;
  383. }
  384. /* get the pin config settings for a specified pin group */
  385. static int samsung_pinconf_group_get(struct pinctrl_dev *pctldev,
  386. unsigned int group, unsigned long *config)
  387. {
  388. struct samsung_pinctrl_drv_data *drvdata;
  389. const unsigned int *pins;
  390. drvdata = pinctrl_dev_get_drvdata(pctldev);
  391. pins = drvdata->pin_groups[group].pins;
  392. samsung_pinconf_get(pctldev, pins[0], config);
  393. return 0;
  394. }
  395. /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
  396. static struct pinconf_ops samsung_pinconf_ops = {
  397. .pin_config_get = samsung_pinconf_get,
  398. .pin_config_set = samsung_pinconf_set,
  399. .pin_config_group_get = samsung_pinconf_group_get,
  400. .pin_config_group_set = samsung_pinconf_group_set,
  401. };
  402. /* gpiolib gpio_set callback function */
  403. static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  404. {
  405. void __iomem *reg;
  406. u32 pin_offset, data;
  407. struct samsung_pinctrl_drv_data *drvdata;
  408. drvdata = dev_get_drvdata(gc->dev);
  409. pin_to_reg_bank(drvdata, offset, &reg, &pin_offset, NULL);
  410. data = readl(reg + DAT_REG);
  411. data &= ~(1 << pin_offset);
  412. if (value)
  413. data |= 1 << pin_offset;
  414. writel(data, reg + DAT_REG);
  415. }
  416. /* gpiolib gpio_get callback function */
  417. static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset)
  418. {
  419. void __iomem *reg;
  420. u32 pin_offset, data;
  421. struct samsung_pinctrl_drv_data *drvdata;
  422. drvdata = dev_get_drvdata(gc->dev);
  423. pin_to_reg_bank(drvdata, offset, &reg, &pin_offset, NULL);
  424. data = readl(reg + DAT_REG);
  425. data >>= pin_offset;
  426. data &= 1;
  427. return data;
  428. }
  429. /*
  430. * gpiolib gpio_direction_input callback function. The setting of the pin
  431. * mux function as 'gpio input' will be handled by the pinctrl susbsystem
  432. * interface.
  433. */
  434. static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  435. {
  436. return pinctrl_gpio_direction_input(gc->base + offset);
  437. }
  438. /*
  439. * gpiolib gpio_direction_output callback function. The setting of the pin
  440. * mux function as 'gpio output' will be handled by the pinctrl susbsystem
  441. * interface.
  442. */
  443. static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  444. int value)
  445. {
  446. samsung_gpio_set(gc, offset, value);
  447. return pinctrl_gpio_direction_output(gc->base + offset);
  448. }
  449. /*
  450. * Parse the pin names listed in the 'samsung,pins' property and convert it
  451. * into a list of gpio numbers are create a pin group from it.
  452. */
  453. static int __init samsung_pinctrl_parse_dt_pins(struct platform_device *pdev,
  454. struct device_node *cfg_np, struct pinctrl_desc *pctl,
  455. unsigned int **pin_list, unsigned int *npins)
  456. {
  457. struct device *dev = &pdev->dev;
  458. struct property *prop;
  459. struct pinctrl_pin_desc const *pdesc = pctl->pins;
  460. unsigned int idx = 0, cnt;
  461. const char *pin_name;
  462. *npins = of_property_count_strings(cfg_np, "samsung,pins");
  463. if (*npins < 0) {
  464. dev_err(dev, "invalid pin list in %s node", cfg_np->name);
  465. return -EINVAL;
  466. }
  467. *pin_list = devm_kzalloc(dev, *npins * sizeof(**pin_list), GFP_KERNEL);
  468. if (!*pin_list) {
  469. dev_err(dev, "failed to allocate memory for pin list\n");
  470. return -ENOMEM;
  471. }
  472. of_property_for_each_string(cfg_np, "samsung,pins", prop, pin_name) {
  473. for (cnt = 0; cnt < pctl->npins; cnt++) {
  474. if (pdesc[cnt].name) {
  475. if (!strcmp(pin_name, pdesc[cnt].name)) {
  476. (*pin_list)[idx++] = pdesc[cnt].number;
  477. break;
  478. }
  479. }
  480. }
  481. if (cnt == pctl->npins) {
  482. dev_err(dev, "pin %s not valid in %s node\n",
  483. pin_name, cfg_np->name);
  484. devm_kfree(dev, *pin_list);
  485. return -EINVAL;
  486. }
  487. }
  488. return 0;
  489. }
  490. /*
  491. * Parse the information about all the available pin groups and pin functions
  492. * from device node of the pin-controller. A pin group is formed with all
  493. * the pins listed in the "samsung,pins" property.
  494. */
  495. static int __init samsung_pinctrl_parse_dt(struct platform_device *pdev,
  496. struct samsung_pinctrl_drv_data *drvdata)
  497. {
  498. struct device *dev = &pdev->dev;
  499. struct device_node *dev_np = dev->of_node;
  500. struct device_node *cfg_np;
  501. struct samsung_pin_group *groups, *grp;
  502. struct samsung_pmx_func *functions, *func;
  503. unsigned *pin_list;
  504. unsigned int npins, grp_cnt, func_idx = 0;
  505. char *gname, *fname;
  506. int ret;
  507. grp_cnt = of_get_child_count(dev_np);
  508. if (!grp_cnt)
  509. return -EINVAL;
  510. groups = devm_kzalloc(dev, grp_cnt * sizeof(*groups), GFP_KERNEL);
  511. if (!groups) {
  512. dev_err(dev, "failed allocate memory for ping group list\n");
  513. return -EINVAL;
  514. }
  515. grp = groups;
  516. functions = devm_kzalloc(dev, grp_cnt * sizeof(*functions), GFP_KERNEL);
  517. if (!functions) {
  518. dev_err(dev, "failed to allocate memory for function list\n");
  519. return -EINVAL;
  520. }
  521. func = functions;
  522. /*
  523. * Iterate over all the child nodes of the pin controller node
  524. * and create pin groups and pin function lists.
  525. */
  526. for_each_child_of_node(dev_np, cfg_np) {
  527. u32 function;
  528. if (of_find_property(cfg_np, "interrupt-controller", NULL))
  529. continue;
  530. ret = samsung_pinctrl_parse_dt_pins(pdev, cfg_np,
  531. &drvdata->pctl, &pin_list, &npins);
  532. if (ret)
  533. return ret;
  534. /* derive pin group name from the node name */
  535. gname = devm_kzalloc(dev, strlen(cfg_np->name) + GSUFFIX_LEN,
  536. GFP_KERNEL);
  537. if (!gname) {
  538. dev_err(dev, "failed to alloc memory for group name\n");
  539. return -ENOMEM;
  540. }
  541. sprintf(gname, "%s%s", cfg_np->name, GROUP_SUFFIX);
  542. grp->name = gname;
  543. grp->pins = pin_list;
  544. grp->num_pins = npins;
  545. of_property_read_u32(cfg_np, "samsung,pin-function", &function);
  546. grp->func = function;
  547. grp++;
  548. if (!of_find_property(cfg_np, "samsung,pin-function", NULL))
  549. continue;
  550. /* derive function name from the node name */
  551. fname = devm_kzalloc(dev, strlen(cfg_np->name) + FSUFFIX_LEN,
  552. GFP_KERNEL);
  553. if (!fname) {
  554. dev_err(dev, "failed to alloc memory for func name\n");
  555. return -ENOMEM;
  556. }
  557. sprintf(fname, "%s%s", cfg_np->name, FUNCTION_SUFFIX);
  558. func->name = fname;
  559. func->groups = devm_kzalloc(dev, sizeof(char *), GFP_KERNEL);
  560. if (!func->groups) {
  561. dev_err(dev, "failed to alloc memory for group list "
  562. "in pin function");
  563. return -ENOMEM;
  564. }
  565. func->groups[0] = gname;
  566. func->num_groups = 1;
  567. func++;
  568. func_idx++;
  569. }
  570. drvdata->pin_groups = groups;
  571. drvdata->nr_groups = grp_cnt;
  572. drvdata->pmx_functions = functions;
  573. drvdata->nr_functions = func_idx;
  574. return 0;
  575. }
  576. /* register the pinctrl interface with the pinctrl subsystem */
  577. static int __init samsung_pinctrl_register(struct platform_device *pdev,
  578. struct samsung_pinctrl_drv_data *drvdata)
  579. {
  580. struct pinctrl_desc *ctrldesc = &drvdata->pctl;
  581. struct pinctrl_pin_desc *pindesc, *pdesc;
  582. struct samsung_pin_bank *pin_bank;
  583. char *pin_names;
  584. int pin, bank, ret;
  585. ctrldesc->name = "samsung-pinctrl";
  586. ctrldesc->owner = THIS_MODULE;
  587. ctrldesc->pctlops = &samsung_pctrl_ops;
  588. ctrldesc->pmxops = &samsung_pinmux_ops;
  589. ctrldesc->confops = &samsung_pinconf_ops;
  590. pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
  591. drvdata->ctrl->nr_pins, GFP_KERNEL);
  592. if (!pindesc) {
  593. dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
  594. return -ENOMEM;
  595. }
  596. ctrldesc->pins = pindesc;
  597. ctrldesc->npins = drvdata->ctrl->nr_pins;
  598. ctrldesc->npins = drvdata->ctrl->nr_pins;
  599. /* dynamically populate the pin number and pin name for pindesc */
  600. for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
  601. pdesc->number = pin + drvdata->ctrl->base;
  602. /*
  603. * allocate space for storing the dynamically generated names for all
  604. * the pins which belong to this pin-controller.
  605. */
  606. pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
  607. drvdata->ctrl->nr_pins, GFP_KERNEL);
  608. if (!pin_names) {
  609. dev_err(&pdev->dev, "mem alloc for pin names failed\n");
  610. return -ENOMEM;
  611. }
  612. /* for each pin, the name of the pin is pin-bank name + pin number */
  613. for (bank = 0; bank < drvdata->ctrl->nr_banks; bank++) {
  614. pin_bank = &drvdata->ctrl->pin_banks[bank];
  615. for (pin = 0; pin < pin_bank->nr_pins; pin++) {
  616. sprintf(pin_names, "%s-%d", pin_bank->name, pin);
  617. pdesc = pindesc + pin_bank->pin_base + pin;
  618. pdesc->name = pin_names;
  619. pin_names += PIN_NAME_LENGTH;
  620. }
  621. }
  622. drvdata->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, drvdata);
  623. if (!drvdata->pctl_dev) {
  624. dev_err(&pdev->dev, "could not register pinctrl driver\n");
  625. return -EINVAL;
  626. }
  627. drvdata->grange.name = "samsung-pctrl-gpio-range";
  628. drvdata->grange.id = 0;
  629. drvdata->grange.base = drvdata->ctrl->base;
  630. drvdata->grange.npins = drvdata->ctrl->nr_pins;
  631. drvdata->grange.gc = drvdata->gc;
  632. pinctrl_add_gpio_range(drvdata->pctl_dev, &drvdata->grange);
  633. ret = samsung_pinctrl_parse_dt(pdev, drvdata);
  634. if (ret) {
  635. pinctrl_unregister(drvdata->pctl_dev);
  636. return ret;
  637. }
  638. return 0;
  639. }
  640. /* register the gpiolib interface with the gpiolib subsystem */
  641. static int __init samsung_gpiolib_register(struct platform_device *pdev,
  642. struct samsung_pinctrl_drv_data *drvdata)
  643. {
  644. struct gpio_chip *gc;
  645. int ret;
  646. gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
  647. if (!gc) {
  648. dev_err(&pdev->dev, "mem alloc for gpio_chip failed\n");
  649. return -ENOMEM;
  650. }
  651. drvdata->gc = gc;
  652. gc->base = drvdata->ctrl->base;
  653. gc->ngpio = drvdata->ctrl->nr_pins;
  654. gc->dev = &pdev->dev;
  655. gc->set = samsung_gpio_set;
  656. gc->get = samsung_gpio_get;
  657. gc->direction_input = samsung_gpio_direction_input;
  658. gc->direction_output = samsung_gpio_direction_output;
  659. gc->label = drvdata->ctrl->label;
  660. gc->owner = THIS_MODULE;
  661. ret = gpiochip_add(gc);
  662. if (ret) {
  663. dev_err(&pdev->dev, "failed to register gpio_chip %s, error "
  664. "code: %d\n", gc->label, ret);
  665. return ret;
  666. }
  667. return 0;
  668. }
  669. /* unregister the gpiolib interface with the gpiolib subsystem */
  670. static int __init samsung_gpiolib_unregister(struct platform_device *pdev,
  671. struct samsung_pinctrl_drv_data *drvdata)
  672. {
  673. int ret = gpiochip_remove(drvdata->gc);
  674. if (ret) {
  675. dev_err(&pdev->dev, "gpio chip remove failed\n");
  676. return ret;
  677. }
  678. return 0;
  679. }
  680. static const struct of_device_id samsung_pinctrl_dt_match[];
  681. /* retrieve the soc specific data */
  682. static struct samsung_pin_ctrl *samsung_pinctrl_get_soc_data(
  683. struct platform_device *pdev)
  684. {
  685. int id;
  686. const struct of_device_id *match;
  687. const struct device_node *node = pdev->dev.of_node;
  688. id = of_alias_get_id(pdev->dev.of_node, "pinctrl");
  689. if (id < 0) {
  690. dev_err(&pdev->dev, "failed to get alias id\n");
  691. return NULL;
  692. }
  693. match = of_match_node(samsung_pinctrl_dt_match, node);
  694. return (struct samsung_pin_ctrl *)match->data + id;
  695. }
  696. static int __devinit samsung_pinctrl_probe(struct platform_device *pdev)
  697. {
  698. struct samsung_pinctrl_drv_data *drvdata;
  699. struct device *dev = &pdev->dev;
  700. struct samsung_pin_ctrl *ctrl;
  701. struct resource *res;
  702. int ret;
  703. if (!dev->of_node) {
  704. dev_err(dev, "device tree node not found\n");
  705. return -ENODEV;
  706. }
  707. ctrl = samsung_pinctrl_get_soc_data(pdev);
  708. if (!ctrl) {
  709. dev_err(&pdev->dev, "driver data not available\n");
  710. return -EINVAL;
  711. }
  712. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  713. if (!drvdata) {
  714. dev_err(dev, "failed to allocate memory for driver's "
  715. "private data\n");
  716. return -ENOMEM;
  717. }
  718. drvdata->ctrl = ctrl;
  719. drvdata->dev = dev;
  720. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  721. if (!res) {
  722. dev_err(dev, "cannot find IO resource\n");
  723. return -ENOENT;
  724. }
  725. drvdata->virt_base = devm_request_and_ioremap(&pdev->dev, res);
  726. if (!drvdata->virt_base) {
  727. dev_err(dev, "ioremap failed\n");
  728. return -ENODEV;
  729. }
  730. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  731. if (res)
  732. drvdata->irq = res->start;
  733. ret = samsung_gpiolib_register(pdev, drvdata);
  734. if (ret)
  735. return ret;
  736. ret = samsung_pinctrl_register(pdev, drvdata);
  737. if (ret) {
  738. samsung_gpiolib_unregister(pdev, drvdata);
  739. return ret;
  740. }
  741. if (ctrl->eint_gpio_init)
  742. ctrl->eint_gpio_init(drvdata);
  743. if (ctrl->eint_wkup_init)
  744. ctrl->eint_wkup_init(drvdata);
  745. platform_set_drvdata(pdev, drvdata);
  746. return 0;
  747. }
  748. static const struct of_device_id samsung_pinctrl_dt_match[] = {
  749. { .compatible = "samsung,pinctrl-exynos4210",
  750. .data = (void *)exynos4210_pin_ctrl },
  751. {},
  752. };
  753. MODULE_DEVICE_TABLE(of, samsung_pinctrl_dt_match);
  754. static struct platform_driver samsung_pinctrl_driver = {
  755. .probe = samsung_pinctrl_probe,
  756. .driver = {
  757. .name = "samsung-pinctrl",
  758. .owner = THIS_MODULE,
  759. .of_match_table = of_match_ptr(samsung_pinctrl_dt_match),
  760. },
  761. };
  762. static int __init samsung_pinctrl_drv_register(void)
  763. {
  764. return platform_driver_register(&samsung_pinctrl_driver);
  765. }
  766. postcore_initcall(samsung_pinctrl_drv_register);
  767. static void __exit samsung_pinctrl_drv_unregister(void)
  768. {
  769. platform_driver_unregister(&samsung_pinctrl_driver);
  770. }
  771. module_exit(samsung_pinctrl_drv_unregister);
  772. MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
  773. MODULE_DESCRIPTION("Samsung pinctrl driver");
  774. MODULE_LICENSE("GPL v2");