pinctrl-samsung.c 25 KB

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