pinctrl-samsung.c 26 KB

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