pinconf.c 13 KB

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