pinctrl-samsung.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. /* some banks have two config registers in a single bank */
  240. if (*offset * b->func_width > BITS_PER_LONG)
  241. *reg += 4;
  242. }
  243. /* enable or disable a pinmux function */
  244. static void samsung_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
  245. unsigned group, bool enable)
  246. {
  247. struct samsung_pinctrl_drv_data *drvdata;
  248. const unsigned int *pins;
  249. struct samsung_pin_bank *bank;
  250. void __iomem *reg;
  251. u32 mask, shift, data, pin_offset, cnt;
  252. unsigned long flags;
  253. drvdata = pinctrl_dev_get_drvdata(pctldev);
  254. pins = drvdata->pin_groups[group].pins;
  255. /*
  256. * for each pin in the pin group selected, program the correspoding pin
  257. * pin function number in the config register.
  258. */
  259. for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++) {
  260. struct samsung_pin_bank_type *type;
  261. pin_to_reg_bank(drvdata, pins[cnt] - drvdata->ctrl->base,
  262. &reg, &pin_offset, &bank);
  263. type = bank->type;
  264. mask = (1 << type->fld_width[PINCFG_TYPE_FUNC]) - 1;
  265. shift = pin_offset * type->fld_width[PINCFG_TYPE_FUNC];
  266. spin_lock_irqsave(&bank->slock, flags);
  267. data = readl(reg);
  268. data &= ~(mask << shift);
  269. if (enable)
  270. data |= drvdata->pin_groups[group].func << shift;
  271. writel(data, reg);
  272. spin_unlock_irqrestore(&bank->slock, flags);
  273. }
  274. }
  275. /* enable a specified pinmux by writing to registers */
  276. static int samsung_pinmux_enable(struct pinctrl_dev *pctldev, unsigned selector,
  277. unsigned group)
  278. {
  279. samsung_pinmux_setup(pctldev, selector, group, true);
  280. return 0;
  281. }
  282. /* disable a specified pinmux by writing to registers */
  283. static void samsung_pinmux_disable(struct pinctrl_dev *pctldev,
  284. unsigned selector, unsigned group)
  285. {
  286. samsung_pinmux_setup(pctldev, selector, group, false);
  287. }
  288. /*
  289. * The calls to gpio_direction_output() and gpio_direction_input()
  290. * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
  291. * function called from the gpiolib interface).
  292. */
  293. static int samsung_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
  294. struct pinctrl_gpio_range *range, unsigned offset, bool input)
  295. {
  296. struct samsung_pin_bank_type *type;
  297. struct samsung_pin_bank *bank;
  298. struct samsung_pinctrl_drv_data *drvdata;
  299. void __iomem *reg;
  300. u32 data, pin_offset, mask, shift;
  301. unsigned long flags;
  302. bank = gc_to_pin_bank(range->gc);
  303. type = bank->type;
  304. drvdata = pinctrl_dev_get_drvdata(pctldev);
  305. pin_offset = offset - bank->pin_base;
  306. reg = drvdata->virt_base + bank->pctl_offset;
  307. mask = (1 << type->fld_width[PINCFG_TYPE_FUNC]) - 1;
  308. shift = pin_offset * type->fld_width[PINCFG_TYPE_FUNC];
  309. spin_lock_irqsave(&bank->slock, flags);
  310. data = readl(reg);
  311. data &= ~(mask << shift);
  312. if (!input)
  313. data |= FUNC_OUTPUT << shift;
  314. writel(data, reg);
  315. spin_unlock_irqrestore(&bank->slock, flags);
  316. return 0;
  317. }
  318. /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
  319. static const struct pinmux_ops samsung_pinmux_ops = {
  320. .get_functions_count = samsung_get_functions_count,
  321. .get_function_name = samsung_pinmux_get_fname,
  322. .get_function_groups = samsung_pinmux_get_groups,
  323. .enable = samsung_pinmux_enable,
  324. .disable = samsung_pinmux_disable,
  325. .gpio_set_direction = samsung_pinmux_gpio_set_direction,
  326. };
  327. /* set or get the pin config settings for a specified pin */
  328. static int samsung_pinconf_rw(struct pinctrl_dev *pctldev, unsigned int pin,
  329. unsigned long *config, bool set)
  330. {
  331. struct samsung_pinctrl_drv_data *drvdata;
  332. struct samsung_pin_bank_type *type;
  333. struct samsung_pin_bank *bank;
  334. void __iomem *reg_base;
  335. enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config);
  336. u32 data, width, pin_offset, mask, shift;
  337. u32 cfg_value, cfg_reg;
  338. unsigned long flags;
  339. drvdata = pinctrl_dev_get_drvdata(pctldev);
  340. pin_to_reg_bank(drvdata, pin - drvdata->ctrl->base, &reg_base,
  341. &pin_offset, &bank);
  342. type = bank->type;
  343. switch (cfg_type) {
  344. case PINCFG_TYPE_PUD:
  345. cfg_reg = PUD_REG;
  346. break;
  347. case PINCFG_TYPE_DRV:
  348. cfg_reg = DRV_REG;
  349. break;
  350. case PINCFG_TYPE_CON_PDN:
  351. cfg_reg = CONPDN_REG;
  352. break;
  353. case PINCFG_TYPE_PUD_PDN:
  354. cfg_reg = PUDPDN_REG;
  355. break;
  356. default:
  357. WARN_ON(1);
  358. return -EINVAL;
  359. }
  360. if (cfg_type >= PINCFG_TYPE_NUM || !type->fld_width[cfg_type])
  361. return -EINVAL;
  362. width = type->fld_width[cfg_type];
  363. spin_lock_irqsave(&bank->slock, flags);
  364. mask = (1 << width) - 1;
  365. shift = pin_offset * width;
  366. data = readl(reg_base + cfg_reg);
  367. if (set) {
  368. cfg_value = PINCFG_UNPACK_VALUE(*config);
  369. data &= ~(mask << shift);
  370. data |= (cfg_value << shift);
  371. writel(data, reg_base + cfg_reg);
  372. } else {
  373. data >>= shift;
  374. data &= mask;
  375. *config = PINCFG_PACK(cfg_type, data);
  376. }
  377. spin_unlock_irqrestore(&bank->slock, flags);
  378. return 0;
  379. }
  380. /* set the pin config settings for a specified pin */
  381. static int samsung_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  382. unsigned long config)
  383. {
  384. return samsung_pinconf_rw(pctldev, pin, &config, true);
  385. }
  386. /* get the pin config settings for a specified pin */
  387. static int samsung_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  388. unsigned long *config)
  389. {
  390. return samsung_pinconf_rw(pctldev, pin, config, false);
  391. }
  392. /* set the pin config settings for a specified pin group */
  393. static int samsung_pinconf_group_set(struct pinctrl_dev *pctldev,
  394. unsigned group, unsigned long config)
  395. {
  396. struct samsung_pinctrl_drv_data *drvdata;
  397. const unsigned int *pins;
  398. unsigned int cnt;
  399. drvdata = pinctrl_dev_get_drvdata(pctldev);
  400. pins = drvdata->pin_groups[group].pins;
  401. for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++)
  402. samsung_pinconf_set(pctldev, pins[cnt], config);
  403. return 0;
  404. }
  405. /* get the pin config settings for a specified pin group */
  406. static int samsung_pinconf_group_get(struct pinctrl_dev *pctldev,
  407. unsigned int group, unsigned long *config)
  408. {
  409. struct samsung_pinctrl_drv_data *drvdata;
  410. const unsigned int *pins;
  411. drvdata = pinctrl_dev_get_drvdata(pctldev);
  412. pins = drvdata->pin_groups[group].pins;
  413. samsung_pinconf_get(pctldev, pins[0], config);
  414. return 0;
  415. }
  416. /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
  417. static const struct pinconf_ops samsung_pinconf_ops = {
  418. .pin_config_get = samsung_pinconf_get,
  419. .pin_config_set = samsung_pinconf_set,
  420. .pin_config_group_get = samsung_pinconf_group_get,
  421. .pin_config_group_set = samsung_pinconf_group_set,
  422. };
  423. /* gpiolib gpio_set callback function */
  424. static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  425. {
  426. struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
  427. struct samsung_pin_bank_type *type = bank->type;
  428. unsigned long flags;
  429. void __iomem *reg;
  430. u32 data;
  431. reg = bank->drvdata->virt_base + bank->pctl_offset;
  432. spin_lock_irqsave(&bank->slock, flags);
  433. data = readl(reg + DAT_REG);
  434. data &= ~(1 << offset);
  435. if (value)
  436. data |= 1 << offset;
  437. writel(data, reg + DAT_REG);
  438. spin_unlock_irqrestore(&bank->slock, flags);
  439. }
  440. /* gpiolib gpio_get callback function */
  441. static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset)
  442. {
  443. void __iomem *reg;
  444. u32 data;
  445. struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
  446. reg = bank->drvdata->virt_base + bank->pctl_offset;
  447. data = readl(reg + DAT_REG);
  448. data >>= offset;
  449. data &= 1;
  450. return data;
  451. }
  452. /*
  453. * gpiolib gpio_direction_input callback function. The setting of the pin
  454. * mux function as 'gpio input' will be handled by the pinctrl susbsystem
  455. * interface.
  456. */
  457. static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  458. {
  459. return pinctrl_gpio_direction_input(gc->base + offset);
  460. }
  461. /*
  462. * gpiolib gpio_direction_output callback function. The setting of the pin
  463. * mux function as 'gpio output' will be handled by the pinctrl susbsystem
  464. * interface.
  465. */
  466. static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  467. int value)
  468. {
  469. samsung_gpio_set(gc, offset, value);
  470. return pinctrl_gpio_direction_output(gc->base + offset);
  471. }
  472. /*
  473. * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
  474. * and a virtual IRQ, if not already present.
  475. */
  476. static int samsung_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  477. {
  478. struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
  479. unsigned int virq;
  480. if (!bank->irq_domain)
  481. return -ENXIO;
  482. virq = irq_create_mapping(bank->irq_domain, offset);
  483. return (virq) ? : -ENXIO;
  484. }
  485. /*
  486. * Parse the pin names listed in the 'samsung,pins' property and convert it
  487. * into a list of gpio numbers are create a pin group from it.
  488. */
  489. static int samsung_pinctrl_parse_dt_pins(struct platform_device *pdev,
  490. struct device_node *cfg_np,
  491. struct pinctrl_desc *pctl,
  492. unsigned int **pin_list,
  493. unsigned int *npins)
  494. {
  495. struct device *dev = &pdev->dev;
  496. struct property *prop;
  497. struct pinctrl_pin_desc const *pdesc = pctl->pins;
  498. unsigned int idx = 0, cnt;
  499. const char *pin_name;
  500. *npins = of_property_count_strings(cfg_np, "samsung,pins");
  501. if (IS_ERR_VALUE(*npins)) {
  502. dev_err(dev, "invalid pin list in %s node", cfg_np->name);
  503. return -EINVAL;
  504. }
  505. *pin_list = devm_kzalloc(dev, *npins * sizeof(**pin_list), GFP_KERNEL);
  506. if (!*pin_list) {
  507. dev_err(dev, "failed to allocate memory for pin list\n");
  508. return -ENOMEM;
  509. }
  510. of_property_for_each_string(cfg_np, "samsung,pins", prop, pin_name) {
  511. for (cnt = 0; cnt < pctl->npins; cnt++) {
  512. if (pdesc[cnt].name) {
  513. if (!strcmp(pin_name, pdesc[cnt].name)) {
  514. (*pin_list)[idx++] = pdesc[cnt].number;
  515. break;
  516. }
  517. }
  518. }
  519. if (cnt == pctl->npins) {
  520. dev_err(dev, "pin %s not valid in %s node\n",
  521. pin_name, cfg_np->name);
  522. devm_kfree(dev, *pin_list);
  523. return -EINVAL;
  524. }
  525. }
  526. return 0;
  527. }
  528. /*
  529. * Parse the information about all the available pin groups and pin functions
  530. * from device node of the pin-controller. A pin group is formed with all
  531. * the pins listed in the "samsung,pins" property.
  532. */
  533. static int samsung_pinctrl_parse_dt(struct platform_device *pdev,
  534. struct samsung_pinctrl_drv_data *drvdata)
  535. {
  536. struct device *dev = &pdev->dev;
  537. struct device_node *dev_np = dev->of_node;
  538. struct device_node *cfg_np;
  539. struct samsung_pin_group *groups, *grp;
  540. struct samsung_pmx_func *functions, *func;
  541. unsigned *pin_list;
  542. unsigned int npins, grp_cnt, func_idx = 0;
  543. char *gname, *fname;
  544. int ret;
  545. grp_cnt = of_get_child_count(dev_np);
  546. if (!grp_cnt)
  547. return -EINVAL;
  548. groups = devm_kzalloc(dev, grp_cnt * sizeof(*groups), GFP_KERNEL);
  549. if (!groups) {
  550. dev_err(dev, "failed allocate memory for ping group list\n");
  551. return -EINVAL;
  552. }
  553. grp = groups;
  554. functions = devm_kzalloc(dev, grp_cnt * sizeof(*functions), GFP_KERNEL);
  555. if (!functions) {
  556. dev_err(dev, "failed to allocate memory for function list\n");
  557. return -EINVAL;
  558. }
  559. func = functions;
  560. /*
  561. * Iterate over all the child nodes of the pin controller node
  562. * and create pin groups and pin function lists.
  563. */
  564. for_each_child_of_node(dev_np, cfg_np) {
  565. u32 function;
  566. if (!of_find_property(cfg_np, "samsung,pins", NULL))
  567. continue;
  568. ret = samsung_pinctrl_parse_dt_pins(pdev, cfg_np,
  569. &drvdata->pctl, &pin_list, &npins);
  570. if (ret)
  571. return ret;
  572. /* derive pin group name from the node name */
  573. gname = devm_kzalloc(dev, strlen(cfg_np->name) + GSUFFIX_LEN,
  574. GFP_KERNEL);
  575. if (!gname) {
  576. dev_err(dev, "failed to alloc memory for group name\n");
  577. return -ENOMEM;
  578. }
  579. sprintf(gname, "%s%s", cfg_np->name, GROUP_SUFFIX);
  580. grp->name = gname;
  581. grp->pins = pin_list;
  582. grp->num_pins = npins;
  583. of_property_read_u32(cfg_np, "samsung,pin-function", &function);
  584. grp->func = function;
  585. grp++;
  586. if (!of_find_property(cfg_np, "samsung,pin-function", NULL))
  587. continue;
  588. /* derive function name from the node name */
  589. fname = devm_kzalloc(dev, strlen(cfg_np->name) + FSUFFIX_LEN,
  590. GFP_KERNEL);
  591. if (!fname) {
  592. dev_err(dev, "failed to alloc memory for func name\n");
  593. return -ENOMEM;
  594. }
  595. sprintf(fname, "%s%s", cfg_np->name, FUNCTION_SUFFIX);
  596. func->name = fname;
  597. func->groups = devm_kzalloc(dev, sizeof(char *), GFP_KERNEL);
  598. if (!func->groups) {
  599. dev_err(dev, "failed to alloc memory for group list "
  600. "in pin function");
  601. return -ENOMEM;
  602. }
  603. func->groups[0] = gname;
  604. func->num_groups = 1;
  605. func++;
  606. func_idx++;
  607. }
  608. drvdata->pin_groups = groups;
  609. drvdata->nr_groups = grp_cnt;
  610. drvdata->pmx_functions = functions;
  611. drvdata->nr_functions = func_idx;
  612. return 0;
  613. }
  614. /* register the pinctrl interface with the pinctrl subsystem */
  615. static int samsung_pinctrl_register(struct platform_device *pdev,
  616. struct samsung_pinctrl_drv_data *drvdata)
  617. {
  618. struct pinctrl_desc *ctrldesc = &drvdata->pctl;
  619. struct pinctrl_pin_desc *pindesc, *pdesc;
  620. struct samsung_pin_bank *pin_bank;
  621. char *pin_names;
  622. int pin, bank, ret;
  623. ctrldesc->name = "samsung-pinctrl";
  624. ctrldesc->owner = THIS_MODULE;
  625. ctrldesc->pctlops = &samsung_pctrl_ops;
  626. ctrldesc->pmxops = &samsung_pinmux_ops;
  627. ctrldesc->confops = &samsung_pinconf_ops;
  628. pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
  629. drvdata->ctrl->nr_pins, GFP_KERNEL);
  630. if (!pindesc) {
  631. dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
  632. return -ENOMEM;
  633. }
  634. ctrldesc->pins = pindesc;
  635. ctrldesc->npins = drvdata->ctrl->nr_pins;
  636. /* dynamically populate the pin number and pin name for pindesc */
  637. for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
  638. pdesc->number = pin + drvdata->ctrl->base;
  639. /*
  640. * allocate space for storing the dynamically generated names for all
  641. * the pins which belong to this pin-controller.
  642. */
  643. pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
  644. drvdata->ctrl->nr_pins, GFP_KERNEL);
  645. if (!pin_names) {
  646. dev_err(&pdev->dev, "mem alloc for pin names failed\n");
  647. return -ENOMEM;
  648. }
  649. /* for each pin, the name of the pin is pin-bank name + pin number */
  650. for (bank = 0; bank < drvdata->ctrl->nr_banks; bank++) {
  651. pin_bank = &drvdata->ctrl->pin_banks[bank];
  652. for (pin = 0; pin < pin_bank->nr_pins; pin++) {
  653. sprintf(pin_names, "%s-%d", pin_bank->name, pin);
  654. pdesc = pindesc + pin_bank->pin_base + pin;
  655. pdesc->name = pin_names;
  656. pin_names += PIN_NAME_LENGTH;
  657. }
  658. }
  659. drvdata->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, drvdata);
  660. if (!drvdata->pctl_dev) {
  661. dev_err(&pdev->dev, "could not register pinctrl driver\n");
  662. return -EINVAL;
  663. }
  664. for (bank = 0; bank < drvdata->ctrl->nr_banks; ++bank) {
  665. pin_bank = &drvdata->ctrl->pin_banks[bank];
  666. pin_bank->grange.name = pin_bank->name;
  667. pin_bank->grange.id = bank;
  668. pin_bank->grange.pin_base = pin_bank->pin_base;
  669. pin_bank->grange.base = pin_bank->gpio_chip.base;
  670. pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
  671. pin_bank->grange.gc = &pin_bank->gpio_chip;
  672. pinctrl_add_gpio_range(drvdata->pctl_dev, &pin_bank->grange);
  673. }
  674. ret = samsung_pinctrl_parse_dt(pdev, drvdata);
  675. if (ret) {
  676. pinctrl_unregister(drvdata->pctl_dev);
  677. return ret;
  678. }
  679. return 0;
  680. }
  681. static const struct gpio_chip samsung_gpiolib_chip = {
  682. .set = samsung_gpio_set,
  683. .get = samsung_gpio_get,
  684. .direction_input = samsung_gpio_direction_input,
  685. .direction_output = samsung_gpio_direction_output,
  686. .to_irq = samsung_gpio_to_irq,
  687. .owner = THIS_MODULE,
  688. };
  689. /* register the gpiolib interface with the gpiolib subsystem */
  690. static int samsung_gpiolib_register(struct platform_device *pdev,
  691. struct samsung_pinctrl_drv_data *drvdata)
  692. {
  693. struct samsung_pin_ctrl *ctrl = drvdata->ctrl;
  694. struct samsung_pin_bank *bank = ctrl->pin_banks;
  695. struct gpio_chip *gc;
  696. int ret;
  697. int i;
  698. for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
  699. bank->gpio_chip = samsung_gpiolib_chip;
  700. gc = &bank->gpio_chip;
  701. gc->base = ctrl->base + bank->pin_base;
  702. gc->ngpio = bank->nr_pins;
  703. gc->dev = &pdev->dev;
  704. gc->of_node = bank->of_node;
  705. gc->label = bank->name;
  706. ret = gpiochip_add(gc);
  707. if (ret) {
  708. dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
  709. gc->label, ret);
  710. goto fail;
  711. }
  712. }
  713. return 0;
  714. fail:
  715. for (--i, --bank; i >= 0; --i, --bank)
  716. if (gpiochip_remove(&bank->gpio_chip))
  717. dev_err(&pdev->dev, "gpio chip %s remove failed\n",
  718. bank->gpio_chip.label);
  719. return ret;
  720. }
  721. /* unregister the gpiolib interface with the gpiolib subsystem */
  722. static int samsung_gpiolib_unregister(struct platform_device *pdev,
  723. struct samsung_pinctrl_drv_data *drvdata)
  724. {
  725. struct samsung_pin_ctrl *ctrl = drvdata->ctrl;
  726. struct samsung_pin_bank *bank = ctrl->pin_banks;
  727. int ret = 0;
  728. int i;
  729. for (i = 0; !ret && i < ctrl->nr_banks; ++i, ++bank)
  730. ret = gpiochip_remove(&bank->gpio_chip);
  731. if (ret)
  732. dev_err(&pdev->dev, "gpio chip remove failed\n");
  733. return ret;
  734. }
  735. static const struct of_device_id samsung_pinctrl_dt_match[];
  736. /* retrieve the soc specific data */
  737. static struct samsung_pin_ctrl *samsung_pinctrl_get_soc_data(
  738. struct samsung_pinctrl_drv_data *d,
  739. struct platform_device *pdev)
  740. {
  741. int id;
  742. const struct of_device_id *match;
  743. struct device_node *node = pdev->dev.of_node;
  744. struct device_node *np;
  745. struct samsung_pin_ctrl *ctrl;
  746. struct samsung_pin_bank *bank;
  747. int i;
  748. id = of_alias_get_id(node, "pinctrl");
  749. if (id < 0) {
  750. dev_err(&pdev->dev, "failed to get alias id\n");
  751. return NULL;
  752. }
  753. match = of_match_node(samsung_pinctrl_dt_match, node);
  754. ctrl = (struct samsung_pin_ctrl *)match->data + id;
  755. bank = ctrl->pin_banks;
  756. for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
  757. spin_lock_init(&bank->slock);
  758. bank->drvdata = d;
  759. bank->pin_base = ctrl->nr_pins;
  760. ctrl->nr_pins += bank->nr_pins;
  761. }
  762. for_each_child_of_node(node, np) {
  763. if (!of_find_property(np, "gpio-controller", NULL))
  764. continue;
  765. bank = ctrl->pin_banks;
  766. for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
  767. if (!strcmp(bank->name, np->name)) {
  768. bank->of_node = np;
  769. break;
  770. }
  771. }
  772. }
  773. ctrl->base = pin_base;
  774. pin_base += ctrl->nr_pins;
  775. return ctrl;
  776. }
  777. static int samsung_pinctrl_probe(struct platform_device *pdev)
  778. {
  779. struct samsung_pinctrl_drv_data *drvdata;
  780. struct device *dev = &pdev->dev;
  781. struct samsung_pin_ctrl *ctrl;
  782. struct resource *res;
  783. int ret;
  784. if (!dev->of_node) {
  785. dev_err(dev, "device tree node not found\n");
  786. return -ENODEV;
  787. }
  788. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  789. if (!drvdata) {
  790. dev_err(dev, "failed to allocate memory for driver's "
  791. "private data\n");
  792. return -ENOMEM;
  793. }
  794. ctrl = samsung_pinctrl_get_soc_data(drvdata, pdev);
  795. if (!ctrl) {
  796. dev_err(&pdev->dev, "driver data not available\n");
  797. return -EINVAL;
  798. }
  799. drvdata->ctrl = ctrl;
  800. drvdata->dev = dev;
  801. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  802. if (!res) {
  803. dev_err(dev, "cannot find IO resource\n");
  804. return -ENOENT;
  805. }
  806. drvdata->virt_base = devm_ioremap_resource(&pdev->dev, res);
  807. if (IS_ERR(drvdata->virt_base))
  808. return PTR_ERR(drvdata->virt_base);
  809. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  810. if (res)
  811. drvdata->irq = res->start;
  812. ret = samsung_gpiolib_register(pdev, drvdata);
  813. if (ret)
  814. return ret;
  815. ret = samsung_pinctrl_register(pdev, drvdata);
  816. if (ret) {
  817. samsung_gpiolib_unregister(pdev, drvdata);
  818. return ret;
  819. }
  820. if (ctrl->eint_gpio_init)
  821. ctrl->eint_gpio_init(drvdata);
  822. if (ctrl->eint_wkup_init)
  823. ctrl->eint_wkup_init(drvdata);
  824. platform_set_drvdata(pdev, drvdata);
  825. return 0;
  826. }
  827. static const struct of_device_id samsung_pinctrl_dt_match[] = {
  828. #ifdef CONFIG_PINCTRL_EXYNOS
  829. { .compatible = "samsung,exynos4210-pinctrl",
  830. .data = (void *)exynos4210_pin_ctrl },
  831. { .compatible = "samsung,exynos4x12-pinctrl",
  832. .data = (void *)exynos4x12_pin_ctrl },
  833. #endif
  834. {},
  835. };
  836. MODULE_DEVICE_TABLE(of, samsung_pinctrl_dt_match);
  837. static struct platform_driver samsung_pinctrl_driver = {
  838. .probe = samsung_pinctrl_probe,
  839. .driver = {
  840. .name = "samsung-pinctrl",
  841. .owner = THIS_MODULE,
  842. .of_match_table = of_match_ptr(samsung_pinctrl_dt_match),
  843. },
  844. };
  845. static int __init samsung_pinctrl_drv_register(void)
  846. {
  847. return platform_driver_register(&samsung_pinctrl_driver);
  848. }
  849. postcore_initcall(samsung_pinctrl_drv_register);
  850. static void __exit samsung_pinctrl_drv_unregister(void)
  851. {
  852. platform_driver_unregister(&samsung_pinctrl_driver);
  853. }
  854. module_exit(samsung_pinctrl_drv_unregister);
  855. MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
  856. MODULE_DESCRIPTION("Samsung pinctrl driver");
  857. MODULE_LICENSE("GPL v2");