pinconf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Core driver for the pin config portions of the pin control subsystem
  3. *
  4. * Copyright (C) 2011 ST-Ericsson SA
  5. * Written on behalf of Linaro for ST-Ericsson
  6. *
  7. * Author: Linus Walleij <linus.walleij@linaro.org>
  8. *
  9. * License terms: GNU General Public License (GPL) version 2
  10. */
  11. #define pr_fmt(fmt) "pinconfig core: " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/device.h>
  16. #include <linux/slab.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/pinctrl/machine.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinconf.h>
  22. #include "core.h"
  23. #include "pinconf.h"
  24. int pinconf_check_ops(struct pinctrl_dev *pctldev)
  25. {
  26. const struct pinconf_ops *ops = pctldev->desc->confops;
  27. /* We must be able to read out pin status */
  28. if (!ops->pin_config_get && !ops->pin_config_group_get) {
  29. dev_err(pctldev->dev,
  30. "pinconf must be able to read out pin status\n");
  31. return -EINVAL;
  32. }
  33. /* We have to be able to config the pins in SOME way */
  34. if (!ops->pin_config_set && !ops->pin_config_group_set) {
  35. dev_err(pctldev->dev,
  36. "pinconf has to be able to set a pins config\n");
  37. return -EINVAL;
  38. }
  39. return 0;
  40. }
  41. int pinconf_validate_map(struct pinctrl_map const *map, int i)
  42. {
  43. if (!map->data.configs.group_or_pin) {
  44. pr_err("failed to register map %s (%d): no group/pin given\n",
  45. map->name, i);
  46. return -EINVAL;
  47. }
  48. if (!map->data.configs.num_configs ||
  49. !map->data.configs.configs) {
  50. pr_err("failed to register map %s (%d): no configs given\n",
  51. map->name, i);
  52. return -EINVAL;
  53. }
  54. return 0;
  55. }
  56. int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
  57. unsigned long *config)
  58. {
  59. const struct pinconf_ops *ops = pctldev->desc->confops;
  60. if (!ops || !ops->pin_config_get) {
  61. dev_err(pctldev->dev, "cannot get pin configuration, missing "
  62. "pin_config_get() function in driver\n");
  63. return -EINVAL;
  64. }
  65. return ops->pin_config_get(pctldev, pin, config);
  66. }
  67. /**
  68. * pin_config_get() - get the configuration of a single pin parameter
  69. * @dev_name: name of the pin controller device for this pin
  70. * @name: name of the pin to get the config for
  71. * @config: the config pointed to by this argument will be filled in with the
  72. * current pin state, it can be used directly by drivers as a numeral, or
  73. * it can be dereferenced to any struct.
  74. */
  75. int pin_config_get(const char *dev_name, const char *name,
  76. unsigned long *config)
  77. {
  78. struct pinctrl_dev *pctldev;
  79. int pin;
  80. mutex_lock(&pinctrl_mutex);
  81. pctldev = get_pinctrl_dev_from_devname(dev_name);
  82. if (!pctldev) {
  83. pin = -EINVAL;
  84. goto unlock;
  85. }
  86. pin = pin_get_from_name(pctldev, name);
  87. if (pin < 0)
  88. goto unlock;
  89. pin = pin_config_get_for_pin(pctldev, pin, config);
  90. unlock:
  91. mutex_unlock(&pinctrl_mutex);
  92. return pin;
  93. }
  94. EXPORT_SYMBOL(pin_config_get);
  95. static int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
  96. unsigned long config)
  97. {
  98. const struct pinconf_ops *ops = pctldev->desc->confops;
  99. int ret;
  100. if (!ops || !ops->pin_config_set) {
  101. dev_err(pctldev->dev, "cannot configure pin, missing "
  102. "config function in driver\n");
  103. return -EINVAL;
  104. }
  105. ret = ops->pin_config_set(pctldev, pin, config);
  106. if (ret) {
  107. dev_err(pctldev->dev,
  108. "unable to set pin configuration on pin %d\n", pin);
  109. return ret;
  110. }
  111. return 0;
  112. }
  113. /**
  114. * pin_config_set() - set the configuration of a single pin parameter
  115. * @dev_name: name of pin controller device for this pin
  116. * @name: name of the pin to set the config for
  117. * @config: the config in this argument will contain the desired pin state, it
  118. * can be used directly by drivers as a numeral, or it can be dereferenced
  119. * to any struct.
  120. */
  121. int pin_config_set(const char *dev_name, const char *name,
  122. unsigned long config)
  123. {
  124. struct pinctrl_dev *pctldev;
  125. int pin, ret;
  126. mutex_lock(&pinctrl_mutex);
  127. pctldev = get_pinctrl_dev_from_devname(dev_name);
  128. if (!pctldev) {
  129. ret = -EINVAL;
  130. goto unlock;
  131. }
  132. pin = pin_get_from_name(pctldev, name);
  133. if (pin < 0) {
  134. ret = pin;
  135. goto unlock;
  136. }
  137. ret = pin_config_set_for_pin(pctldev, pin, config);
  138. unlock:
  139. mutex_unlock(&pinctrl_mutex);
  140. return ret;
  141. }
  142. EXPORT_SYMBOL(pin_config_set);
  143. int pin_config_group_get(const char *dev_name, const char *pin_group,
  144. unsigned long *config)
  145. {
  146. struct pinctrl_dev *pctldev;
  147. const struct pinconf_ops *ops;
  148. int selector, ret;
  149. mutex_lock(&pinctrl_mutex);
  150. pctldev = get_pinctrl_dev_from_devname(dev_name);
  151. if (!pctldev) {
  152. ret = -EINVAL;
  153. goto unlock;
  154. }
  155. ops = pctldev->desc->confops;
  156. if (!ops || !ops->pin_config_group_get) {
  157. dev_err(pctldev->dev, "cannot get configuration for pin "
  158. "group, missing group config get function in "
  159. "driver\n");
  160. ret = -EINVAL;
  161. goto unlock;
  162. }
  163. selector = pinctrl_get_group_selector(pctldev, pin_group);
  164. if (selector < 0) {
  165. ret = selector;
  166. goto unlock;
  167. }
  168. ret = ops->pin_config_group_get(pctldev, selector, config);
  169. unlock:
  170. mutex_unlock(&pinctrl_mutex);
  171. return ret;
  172. }
  173. EXPORT_SYMBOL(pin_config_group_get);
  174. int pin_config_group_set(const char *dev_name, const char *pin_group,
  175. unsigned long config)
  176. {
  177. struct pinctrl_dev *pctldev;
  178. const struct pinconf_ops *ops;
  179. const struct pinctrl_ops *pctlops;
  180. int selector;
  181. const unsigned *pins;
  182. unsigned num_pins;
  183. int ret;
  184. int i;
  185. mutex_lock(&pinctrl_mutex);
  186. pctldev = get_pinctrl_dev_from_devname(dev_name);
  187. if (!pctldev) {
  188. ret = -EINVAL;
  189. goto unlock;
  190. }
  191. ops = pctldev->desc->confops;
  192. pctlops = pctldev->desc->pctlops;
  193. if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
  194. dev_err(pctldev->dev, "cannot configure pin group, missing "
  195. "config function in driver\n");
  196. ret = -EINVAL;
  197. goto unlock;
  198. }
  199. selector = pinctrl_get_group_selector(pctldev, pin_group);
  200. if (selector < 0) {
  201. ret = selector;
  202. goto unlock;
  203. }
  204. ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
  205. if (ret) {
  206. dev_err(pctldev->dev, "cannot configure pin group, error "
  207. "getting pins\n");
  208. goto unlock;
  209. }
  210. /*
  211. * If the pin controller supports handling entire groups we use that
  212. * capability.
  213. */
  214. if (ops->pin_config_group_set) {
  215. ret = ops->pin_config_group_set(pctldev, selector, config);
  216. /*
  217. * If the pin controller prefer that a certain group be handled
  218. * pin-by-pin as well, it returns -EAGAIN.
  219. */
  220. if (ret != -EAGAIN)
  221. goto unlock;
  222. }
  223. /*
  224. * If the controller cannot handle entire groups, we configure each pin
  225. * individually.
  226. */
  227. if (!ops->pin_config_set) {
  228. ret = 0;
  229. goto unlock;
  230. }
  231. for (i = 0; i < num_pins; i++) {
  232. ret = ops->pin_config_set(pctldev, pins[i], config);
  233. if (ret < 0)
  234. goto unlock;
  235. }
  236. ret = 0;
  237. unlock:
  238. mutex_unlock(&pinctrl_mutex);
  239. return ret;
  240. }
  241. EXPORT_SYMBOL(pin_config_group_set);
  242. int pinconf_map_to_setting(struct pinctrl_map const *map,
  243. struct pinctrl_setting *setting)
  244. {
  245. struct pinctrl_dev *pctldev = setting->pctldev;
  246. int pin;
  247. switch (setting->type) {
  248. case PIN_MAP_TYPE_CONFIGS_PIN:
  249. pin = pin_get_from_name(pctldev,
  250. map->data.configs.group_or_pin);
  251. if (pin < 0) {
  252. dev_err(pctldev->dev, "could not map pin config for \"%s\"",
  253. map->data.configs.group_or_pin);
  254. return pin;
  255. }
  256. setting->data.configs.group_or_pin = pin;
  257. break;
  258. case PIN_MAP_TYPE_CONFIGS_GROUP:
  259. pin = pinctrl_get_group_selector(pctldev,
  260. map->data.configs.group_or_pin);
  261. if (pin < 0) {
  262. dev_err(pctldev->dev, "could not map group config for \"%s\"",
  263. map->data.configs.group_or_pin);
  264. return pin;
  265. }
  266. setting->data.configs.group_or_pin = pin;
  267. break;
  268. default:
  269. return -EINVAL;
  270. }
  271. setting->data.configs.num_configs = map->data.configs.num_configs;
  272. setting->data.configs.configs = map->data.configs.configs;
  273. return 0;
  274. }
  275. void pinconf_free_setting(struct pinctrl_setting const *setting)
  276. {
  277. }
  278. int pinconf_apply_setting(struct pinctrl_setting const *setting)
  279. {
  280. struct pinctrl_dev *pctldev = setting->pctldev;
  281. const struct pinconf_ops *ops = pctldev->desc->confops;
  282. int i, ret;
  283. if (!ops) {
  284. dev_err(pctldev->dev, "missing confops\n");
  285. return -EINVAL;
  286. }
  287. switch (setting->type) {
  288. case PIN_MAP_TYPE_CONFIGS_PIN:
  289. if (!ops->pin_config_set) {
  290. dev_err(pctldev->dev, "missing pin_config_set op\n");
  291. return -EINVAL;
  292. }
  293. for (i = 0; i < setting->data.configs.num_configs; i++) {
  294. ret = ops->pin_config_set(pctldev,
  295. setting->data.configs.group_or_pin,
  296. setting->data.configs.configs[i]);
  297. if (ret < 0) {
  298. dev_err(pctldev->dev,
  299. "pin_config_set op failed for pin %d config %08lx\n",
  300. setting->data.configs.group_or_pin,
  301. setting->data.configs.configs[i]);
  302. return ret;
  303. }
  304. }
  305. break;
  306. case PIN_MAP_TYPE_CONFIGS_GROUP:
  307. if (!ops->pin_config_group_set) {
  308. dev_err(pctldev->dev,
  309. "missing pin_config_group_set op\n");
  310. return -EINVAL;
  311. }
  312. for (i = 0; i < setting->data.configs.num_configs; i++) {
  313. ret = ops->pin_config_group_set(pctldev,
  314. setting->data.configs.group_or_pin,
  315. setting->data.configs.configs[i]);
  316. if (ret < 0) {
  317. dev_err(pctldev->dev,
  318. "pin_config_group_set op failed for group %d config %08lx\n",
  319. setting->data.configs.group_or_pin,
  320. setting->data.configs.configs[i]);
  321. return ret;
  322. }
  323. }
  324. break;
  325. default:
  326. return -EINVAL;
  327. }
  328. return 0;
  329. }
  330. #ifdef CONFIG_DEBUG_FS
  331. void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
  332. {
  333. struct pinctrl_dev *pctldev;
  334. const struct pinconf_ops *confops;
  335. int i;
  336. pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
  337. if (pctldev)
  338. confops = pctldev->desc->confops;
  339. else
  340. confops = NULL;
  341. switch (map->type) {
  342. case PIN_MAP_TYPE_CONFIGS_PIN:
  343. seq_printf(s, "pin ");
  344. break;
  345. case PIN_MAP_TYPE_CONFIGS_GROUP:
  346. seq_printf(s, "group ");
  347. break;
  348. default:
  349. break;
  350. }
  351. seq_printf(s, "%s\n", map->data.configs.group_or_pin);
  352. for (i = 0; i < map->data.configs.num_configs; i++) {
  353. seq_printf(s, "config ");
  354. if (confops && confops->pin_config_config_dbg_show)
  355. confops->pin_config_config_dbg_show(pctldev, s,
  356. map->data.configs.configs[i]);
  357. else
  358. seq_printf(s, "%08lx", map->data.configs.configs[i]);
  359. seq_printf(s, "\n");
  360. }
  361. }
  362. void pinconf_show_setting(struct seq_file *s,
  363. struct pinctrl_setting const *setting)
  364. {
  365. struct pinctrl_dev *pctldev = setting->pctldev;
  366. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  367. const struct pinconf_ops *confops = pctldev->desc->confops;
  368. struct pin_desc *desc;
  369. int i;
  370. switch (setting->type) {
  371. case PIN_MAP_TYPE_CONFIGS_PIN:
  372. desc = pin_desc_get(setting->pctldev,
  373. setting->data.configs.group_or_pin);
  374. seq_printf(s, "pin %s (%d)",
  375. desc->name ? desc->name : "unnamed",
  376. setting->data.configs.group_or_pin);
  377. break;
  378. case PIN_MAP_TYPE_CONFIGS_GROUP:
  379. seq_printf(s, "group %s (%d)",
  380. pctlops->get_group_name(pctldev,
  381. setting->data.configs.group_or_pin),
  382. setting->data.configs.group_or_pin);
  383. break;
  384. default:
  385. break;
  386. }
  387. /*
  388. * FIXME: We should really get the pin controler to dump the config
  389. * values, so they can be decoded to something meaningful.
  390. */
  391. for (i = 0; i < setting->data.configs.num_configs; i++) {
  392. seq_printf(s, " ");
  393. if (confops && confops->pin_config_config_dbg_show)
  394. confops->pin_config_config_dbg_show(pctldev, s,
  395. setting->data.configs.configs[i]);
  396. else
  397. seq_printf(s, "%08lx",
  398. setting->data.configs.configs[i]);
  399. }
  400. seq_printf(s, "\n");
  401. }
  402. static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
  403. struct seq_file *s, int pin)
  404. {
  405. const struct pinconf_ops *ops = pctldev->desc->confops;
  406. /* no-op when not using generic pin config */
  407. pinconf_generic_dump_pin(pctldev, s, pin);
  408. if (ops && ops->pin_config_dbg_show)
  409. ops->pin_config_dbg_show(pctldev, s, pin);
  410. }
  411. static int pinconf_pins_show(struct seq_file *s, void *what)
  412. {
  413. struct pinctrl_dev *pctldev = s->private;
  414. const struct pinconf_ops *ops = pctldev->desc->confops;
  415. unsigned i, pin;
  416. if (!ops || !ops->pin_config_get)
  417. return 0;
  418. seq_puts(s, "Pin config settings per pin\n");
  419. seq_puts(s, "Format: pin (name): configs\n");
  420. mutex_lock(&pinctrl_mutex);
  421. /* The pin number can be retrived from the pin controller descriptor */
  422. for (i = 0; i < pctldev->desc->npins; i++) {
  423. struct pin_desc *desc;
  424. pin = pctldev->desc->pins[i].number;
  425. desc = pin_desc_get(pctldev, pin);
  426. /* Skip if we cannot search the pin */
  427. if (desc == NULL)
  428. continue;
  429. seq_printf(s, "pin %d (%s):", pin,
  430. desc->name ? desc->name : "unnamed");
  431. pinconf_dump_pin(pctldev, s, pin);
  432. seq_printf(s, "\n");
  433. }
  434. mutex_unlock(&pinctrl_mutex);
  435. return 0;
  436. }
  437. static void pinconf_dump_group(struct pinctrl_dev *pctldev,
  438. struct seq_file *s, unsigned selector,
  439. const char *gname)
  440. {
  441. const struct pinconf_ops *ops = pctldev->desc->confops;
  442. /* no-op when not using generic pin config */
  443. pinconf_generic_dump_group(pctldev, s, gname);
  444. if (ops && ops->pin_config_group_dbg_show)
  445. ops->pin_config_group_dbg_show(pctldev, s, selector);
  446. }
  447. static int pinconf_groups_show(struct seq_file *s, void *what)
  448. {
  449. struct pinctrl_dev *pctldev = s->private;
  450. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  451. const struct pinconf_ops *ops = pctldev->desc->confops;
  452. unsigned ngroups = pctlops->get_groups_count(pctldev);
  453. unsigned selector = 0;
  454. if (!ops || !ops->pin_config_group_get)
  455. return 0;
  456. seq_puts(s, "Pin config settings per pin group\n");
  457. seq_puts(s, "Format: group (name): configs\n");
  458. while (selector < ngroups) {
  459. const char *gname = pctlops->get_group_name(pctldev, selector);
  460. seq_printf(s, "%u (%s):", selector, gname);
  461. pinconf_dump_group(pctldev, s, selector, gname);
  462. seq_printf(s, "\n");
  463. selector++;
  464. }
  465. return 0;
  466. }
  467. static int pinconf_pins_open(struct inode *inode, struct file *file)
  468. {
  469. return single_open(file, pinconf_pins_show, inode->i_private);
  470. }
  471. static int pinconf_groups_open(struct inode *inode, struct file *file)
  472. {
  473. return single_open(file, pinconf_groups_show, inode->i_private);
  474. }
  475. static const struct file_operations pinconf_pins_ops = {
  476. .open = pinconf_pins_open,
  477. .read = seq_read,
  478. .llseek = seq_lseek,
  479. .release = single_release,
  480. };
  481. static const struct file_operations pinconf_groups_ops = {
  482. .open = pinconf_groups_open,
  483. .read = seq_read,
  484. .llseek = seq_lseek,
  485. .release = single_release,
  486. };
  487. void pinconf_init_device_debugfs(struct dentry *devroot,
  488. struct pinctrl_dev *pctldev)
  489. {
  490. debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
  491. devroot, pctldev, &pinconf_pins_ops);
  492. debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
  493. devroot, pctldev, &pinconf_groups_ops);
  494. }
  495. #endif