pinctrl-samsung.c 28 KB

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