pinctrl-samsung.c 27 KB

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