pinconf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. 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. int pin;
  241. switch (setting->type) {
  242. case PIN_MAP_TYPE_CONFIGS_PIN:
  243. pin = pin_get_from_name(pctldev,
  244. map->data.configs.group_or_pin);
  245. if (pin < 0) {
  246. dev_err(pctldev->dev, "could not map pin config for \"%s\"",
  247. map->data.configs.group_or_pin);
  248. return pin;
  249. }
  250. setting->data.configs.group_or_pin = pin;
  251. break;
  252. case PIN_MAP_TYPE_CONFIGS_GROUP:
  253. pin = pinctrl_get_group_selector(pctldev,
  254. map->data.configs.group_or_pin);
  255. if (pin < 0) {
  256. dev_err(pctldev->dev, "could not map group config for \"%s\"",
  257. map->data.configs.group_or_pin);
  258. return pin;
  259. }
  260. setting->data.configs.group_or_pin = pin;
  261. break;
  262. default:
  263. return -EINVAL;
  264. }
  265. setting->data.configs.num_configs = map->data.configs.num_configs;
  266. setting->data.configs.configs = map->data.configs.configs;
  267. return 0;
  268. }
  269. void pinconf_free_setting(struct pinctrl_setting const *setting)
  270. {
  271. }
  272. int pinconf_apply_setting(struct pinctrl_setting const *setting)
  273. {
  274. struct pinctrl_dev *pctldev = setting->pctldev;
  275. const struct pinconf_ops *ops = pctldev->desc->confops;
  276. int i, ret;
  277. if (!ops) {
  278. dev_err(pctldev->dev, "missing confops\n");
  279. return -EINVAL;
  280. }
  281. switch (setting->type) {
  282. case PIN_MAP_TYPE_CONFIGS_PIN:
  283. if (!ops->pin_config_set) {
  284. dev_err(pctldev->dev, "missing pin_config_set op\n");
  285. return -EINVAL;
  286. }
  287. for (i = 0; i < setting->data.configs.num_configs; i++) {
  288. ret = ops->pin_config_set(pctldev,
  289. setting->data.configs.group_or_pin,
  290. setting->data.configs.configs[i]);
  291. if (ret < 0) {
  292. dev_err(pctldev->dev,
  293. "pin_config_set op failed for pin %d config %08lx\n",
  294. setting->data.configs.group_or_pin,
  295. setting->data.configs.configs[i]);
  296. return ret;
  297. }
  298. }
  299. break;
  300. case PIN_MAP_TYPE_CONFIGS_GROUP:
  301. if (!ops->pin_config_group_set) {
  302. dev_err(pctldev->dev,
  303. "missing pin_config_group_set op\n");
  304. return -EINVAL;
  305. }
  306. for (i = 0; i < setting->data.configs.num_configs; i++) {
  307. ret = ops->pin_config_group_set(pctldev,
  308. setting->data.configs.group_or_pin,
  309. setting->data.configs.configs[i]);
  310. if (ret < 0) {
  311. dev_err(pctldev->dev,
  312. "pin_config_group_set op failed for group %d config %08lx\n",
  313. setting->data.configs.group_or_pin,
  314. setting->data.configs.configs[i]);
  315. return ret;
  316. }
  317. }
  318. break;
  319. default:
  320. return -EINVAL;
  321. }
  322. return 0;
  323. }
  324. #ifdef CONFIG_DEBUG_FS
  325. void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
  326. {
  327. int i;
  328. switch (map->type) {
  329. case PIN_MAP_TYPE_CONFIGS_PIN:
  330. seq_printf(s, "pin ");
  331. break;
  332. case PIN_MAP_TYPE_CONFIGS_GROUP:
  333. seq_printf(s, "group ");
  334. break;
  335. default:
  336. break;
  337. }
  338. seq_printf(s, "%s\n", map->data.configs.group_or_pin);
  339. for (i = 0; i < map->data.configs.num_configs; i++)
  340. seq_printf(s, "config %08lx\n", map->data.configs.configs[i]);
  341. }
  342. void pinconf_show_setting(struct seq_file *s,
  343. struct pinctrl_setting const *setting)
  344. {
  345. struct pinctrl_dev *pctldev = setting->pctldev;
  346. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  347. struct pin_desc *desc;
  348. int i;
  349. switch (setting->type) {
  350. case PIN_MAP_TYPE_CONFIGS_PIN:
  351. desc = pin_desc_get(setting->pctldev,
  352. setting->data.configs.group_or_pin);
  353. seq_printf(s, "pin %s (%d)",
  354. desc->name ? desc->name : "unnamed",
  355. setting->data.configs.group_or_pin);
  356. break;
  357. case PIN_MAP_TYPE_CONFIGS_GROUP:
  358. seq_printf(s, "group %s (%d)",
  359. pctlops->get_group_name(pctldev,
  360. setting->data.configs.group_or_pin),
  361. setting->data.configs.group_or_pin);
  362. break;
  363. default:
  364. break;
  365. }
  366. /*
  367. * FIXME: We should really get the pin controler to dump the config
  368. * values, so they can be decoded to something meaningful.
  369. */
  370. for (i = 0; i < setting->data.configs.num_configs; i++)
  371. seq_printf(s, " %08lx", setting->data.configs.configs[i]);
  372. seq_printf(s, "\n");
  373. }
  374. static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
  375. struct seq_file *s, int pin)
  376. {
  377. const struct pinconf_ops *ops = pctldev->desc->confops;
  378. /* no-op when not using generic pin config */
  379. pinconf_generic_dump_pin(pctldev, s, pin);
  380. if (ops && ops->pin_config_dbg_show)
  381. ops->pin_config_dbg_show(pctldev, s, pin);
  382. }
  383. static int pinconf_pins_show(struct seq_file *s, void *what)
  384. {
  385. struct pinctrl_dev *pctldev = s->private;
  386. unsigned i, pin;
  387. seq_puts(s, "Pin config settings per pin\n");
  388. seq_puts(s, "Format: pin (name): pinmux setting array\n");
  389. mutex_lock(&pinctrl_mutex);
  390. /* The pin number can be retrived from the pin controller descriptor */
  391. for (i = 0; i < pctldev->desc->npins; i++) {
  392. struct pin_desc *desc;
  393. pin = pctldev->desc->pins[i].number;
  394. desc = pin_desc_get(pctldev, pin);
  395. /* Skip if we cannot search the pin */
  396. if (desc == NULL)
  397. continue;
  398. seq_printf(s, "pin %d (%s):", pin,
  399. desc->name ? desc->name : "unnamed");
  400. pinconf_dump_pin(pctldev, s, pin);
  401. seq_printf(s, "\n");
  402. }
  403. mutex_unlock(&pinctrl_mutex);
  404. return 0;
  405. }
  406. static void pinconf_dump_group(struct pinctrl_dev *pctldev,
  407. struct seq_file *s, unsigned selector,
  408. const char *gname)
  409. {
  410. const struct pinconf_ops *ops = pctldev->desc->confops;
  411. /* no-op when not using generic pin config */
  412. pinconf_generic_dump_group(pctldev, s, gname);
  413. if (ops && ops->pin_config_group_dbg_show)
  414. ops->pin_config_group_dbg_show(pctldev, s, selector);
  415. }
  416. static int pinconf_groups_show(struct seq_file *s, void *what)
  417. {
  418. struct pinctrl_dev *pctldev = s->private;
  419. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  420. const struct pinconf_ops *ops = pctldev->desc->confops;
  421. unsigned selector = 0;
  422. if (!ops || !ops->pin_config_group_get)
  423. return 0;
  424. seq_puts(s, "Pin config settings per pin group\n");
  425. seq_puts(s, "Format: group (name): pinmux setting array\n");
  426. mutex_lock(&pinctrl_mutex);
  427. while (pctlops->list_groups(pctldev, selector) >= 0) {
  428. const char *gname = pctlops->get_group_name(pctldev, selector);
  429. seq_printf(s, "%u (%s):", selector, gname);
  430. pinconf_dump_group(pctldev, s, selector, gname);
  431. seq_printf(s, "\n");
  432. selector++;
  433. }
  434. mutex_unlock(&pinctrl_mutex);
  435. return 0;
  436. }
  437. static int pinconf_pins_open(struct inode *inode, struct file *file)
  438. {
  439. return single_open(file, pinconf_pins_show, inode->i_private);
  440. }
  441. static int pinconf_groups_open(struct inode *inode, struct file *file)
  442. {
  443. return single_open(file, pinconf_groups_show, inode->i_private);
  444. }
  445. static const struct file_operations pinconf_pins_ops = {
  446. .open = pinconf_pins_open,
  447. .read = seq_read,
  448. .llseek = seq_lseek,
  449. .release = single_release,
  450. };
  451. static const struct file_operations pinconf_groups_ops = {
  452. .open = pinconf_groups_open,
  453. .read = seq_read,
  454. .llseek = seq_lseek,
  455. .release = single_release,
  456. };
  457. void pinconf_init_device_debugfs(struct dentry *devroot,
  458. struct pinctrl_dev *pctldev)
  459. {
  460. debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
  461. devroot, pctldev, &pinconf_pins_ops);
  462. debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
  463. devroot, pctldev, &pinconf_groups_ops);
  464. }
  465. #endif