pinconf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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/uaccess.h>
  20. #include <linux/pinctrl/machine.h>
  21. #include <linux/pinctrl/pinctrl.h>
  22. #include <linux/pinctrl/pinconf.h>
  23. #include "core.h"
  24. #include "pinconf.h"
  25. int pinconf_check_ops(struct pinctrl_dev *pctldev)
  26. {
  27. const struct pinconf_ops *ops = pctldev->desc->confops;
  28. /* We have to be able to config the pins in SOME way */
  29. if (!ops->pin_config_set && !ops->pin_config_group_set) {
  30. dev_err(pctldev->dev,
  31. "pinconf has to be able to set a pins config\n");
  32. return -EINVAL;
  33. }
  34. return 0;
  35. }
  36. int pinconf_validate_map(struct pinctrl_map const *map, int i)
  37. {
  38. if (!map->data.configs.group_or_pin) {
  39. pr_err("failed to register map %s (%d): no group/pin given\n",
  40. map->name, i);
  41. return -EINVAL;
  42. }
  43. if (!map->data.configs.num_configs ||
  44. !map->data.configs.configs) {
  45. pr_err("failed to register map %s (%d): no configs given\n",
  46. map->name, i);
  47. return -EINVAL;
  48. }
  49. return 0;
  50. }
  51. int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
  52. unsigned long *config)
  53. {
  54. const struct pinconf_ops *ops = pctldev->desc->confops;
  55. if (!ops || !ops->pin_config_get) {
  56. dev_dbg(pctldev->dev,
  57. "cannot get pin configuration, .pin_config_get missing in driver\n");
  58. return -ENOTSUPP;
  59. }
  60. return ops->pin_config_get(pctldev, pin, config);
  61. }
  62. int pin_config_group_get(const char *dev_name, const char *pin_group,
  63. unsigned long *config)
  64. {
  65. struct pinctrl_dev *pctldev;
  66. const struct pinconf_ops *ops;
  67. int selector, ret;
  68. pctldev = get_pinctrl_dev_from_devname(dev_name);
  69. if (!pctldev) {
  70. ret = -EINVAL;
  71. return ret;
  72. }
  73. mutex_lock(&pctldev->mutex);
  74. ops = pctldev->desc->confops;
  75. if (!ops || !ops->pin_config_group_get) {
  76. dev_dbg(pctldev->dev, "cannot get configuration for pin "
  77. "group, missing group config get function in "
  78. "driver\n");
  79. ret = -ENOTSUPP;
  80. goto unlock;
  81. }
  82. selector = pinctrl_get_group_selector(pctldev, pin_group);
  83. if (selector < 0) {
  84. ret = selector;
  85. goto unlock;
  86. }
  87. ret = ops->pin_config_group_get(pctldev, selector, config);
  88. unlock:
  89. mutex_unlock(&pctldev->mutex);
  90. return ret;
  91. }
  92. int pinconf_map_to_setting(struct pinctrl_map const *map,
  93. struct pinctrl_setting *setting)
  94. {
  95. struct pinctrl_dev *pctldev = setting->pctldev;
  96. int pin;
  97. switch (setting->type) {
  98. case PIN_MAP_TYPE_CONFIGS_PIN:
  99. pin = pin_get_from_name(pctldev,
  100. map->data.configs.group_or_pin);
  101. if (pin < 0) {
  102. dev_err(pctldev->dev, "could not map pin config for \"%s\"",
  103. map->data.configs.group_or_pin);
  104. return pin;
  105. }
  106. setting->data.configs.group_or_pin = pin;
  107. break;
  108. case PIN_MAP_TYPE_CONFIGS_GROUP:
  109. pin = pinctrl_get_group_selector(pctldev,
  110. map->data.configs.group_or_pin);
  111. if (pin < 0) {
  112. dev_err(pctldev->dev, "could not map group config for \"%s\"",
  113. map->data.configs.group_or_pin);
  114. return pin;
  115. }
  116. setting->data.configs.group_or_pin = pin;
  117. break;
  118. default:
  119. return -EINVAL;
  120. }
  121. setting->data.configs.num_configs = map->data.configs.num_configs;
  122. setting->data.configs.configs = map->data.configs.configs;
  123. return 0;
  124. }
  125. void pinconf_free_setting(struct pinctrl_setting const *setting)
  126. {
  127. }
  128. int pinconf_apply_setting(struct pinctrl_setting const *setting)
  129. {
  130. struct pinctrl_dev *pctldev = setting->pctldev;
  131. const struct pinconf_ops *ops = pctldev->desc->confops;
  132. int ret;
  133. if (!ops) {
  134. dev_err(pctldev->dev, "missing confops\n");
  135. return -EINVAL;
  136. }
  137. switch (setting->type) {
  138. case PIN_MAP_TYPE_CONFIGS_PIN:
  139. if (!ops->pin_config_set) {
  140. dev_err(pctldev->dev, "missing pin_config_set op\n");
  141. return -EINVAL;
  142. }
  143. ret = ops->pin_config_set(pctldev,
  144. setting->data.configs.group_or_pin,
  145. setting->data.configs.configs,
  146. setting->data.configs.num_configs);
  147. if (ret < 0) {
  148. dev_err(pctldev->dev,
  149. "pin_config_set op failed for pin %d\n",
  150. setting->data.configs.group_or_pin);
  151. return ret;
  152. }
  153. break;
  154. case PIN_MAP_TYPE_CONFIGS_GROUP:
  155. if (!ops->pin_config_group_set) {
  156. dev_err(pctldev->dev,
  157. "missing pin_config_group_set op\n");
  158. return -EINVAL;
  159. }
  160. ret = ops->pin_config_group_set(pctldev,
  161. setting->data.configs.group_or_pin,
  162. setting->data.configs.configs,
  163. setting->data.configs.num_configs);
  164. if (ret < 0) {
  165. dev_err(pctldev->dev,
  166. "pin_config_group_set op failed for group %d\n",
  167. setting->data.configs.group_or_pin);
  168. return ret;
  169. }
  170. break;
  171. default:
  172. return -EINVAL;
  173. }
  174. return 0;
  175. }
  176. #ifdef CONFIG_DEBUG_FS
  177. static void pinconf_show_config(struct seq_file *s, struct pinctrl_dev *pctldev,
  178. unsigned long *configs, unsigned num_configs)
  179. {
  180. const struct pinconf_ops *confops;
  181. int i;
  182. if (pctldev)
  183. confops = pctldev->desc->confops;
  184. else
  185. confops = NULL;
  186. for (i = 0; i < num_configs; i++) {
  187. seq_puts(s, "config ");
  188. if (confops && confops->pin_config_config_dbg_show)
  189. confops->pin_config_config_dbg_show(pctldev, s,
  190. configs[i]);
  191. else
  192. seq_printf(s, "%08lx", configs[i]);
  193. seq_puts(s, "\n");
  194. }
  195. }
  196. void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
  197. {
  198. struct pinctrl_dev *pctldev;
  199. pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
  200. switch (map->type) {
  201. case PIN_MAP_TYPE_CONFIGS_PIN:
  202. seq_printf(s, "pin ");
  203. break;
  204. case PIN_MAP_TYPE_CONFIGS_GROUP:
  205. seq_printf(s, "group ");
  206. break;
  207. default:
  208. break;
  209. }
  210. seq_printf(s, "%s\n", map->data.configs.group_or_pin);
  211. pinconf_show_config(s, pctldev, map->data.configs.configs,
  212. map->data.configs.num_configs);
  213. }
  214. void pinconf_show_setting(struct seq_file *s,
  215. struct pinctrl_setting const *setting)
  216. {
  217. struct pinctrl_dev *pctldev = setting->pctldev;
  218. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  219. struct pin_desc *desc;
  220. switch (setting->type) {
  221. case PIN_MAP_TYPE_CONFIGS_PIN:
  222. desc = pin_desc_get(setting->pctldev,
  223. setting->data.configs.group_or_pin);
  224. seq_printf(s, "pin %s (%d)",
  225. desc->name ? desc->name : "unnamed",
  226. setting->data.configs.group_or_pin);
  227. break;
  228. case PIN_MAP_TYPE_CONFIGS_GROUP:
  229. seq_printf(s, "group %s (%d)",
  230. pctlops->get_group_name(pctldev,
  231. setting->data.configs.group_or_pin),
  232. setting->data.configs.group_or_pin);
  233. break;
  234. default:
  235. break;
  236. }
  237. /*
  238. * FIXME: We should really get the pin controler to dump the config
  239. * values, so they can be decoded to something meaningful.
  240. */
  241. pinconf_show_config(s, pctldev, setting->data.configs.configs,
  242. setting->data.configs.num_configs);
  243. }
  244. static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
  245. struct seq_file *s, int pin)
  246. {
  247. const struct pinconf_ops *ops = pctldev->desc->confops;
  248. /* no-op when not using generic pin config */
  249. pinconf_generic_dump_pins(pctldev, s, NULL, pin);
  250. if (ops && ops->pin_config_dbg_show)
  251. ops->pin_config_dbg_show(pctldev, s, pin);
  252. }
  253. static int pinconf_pins_show(struct seq_file *s, void *what)
  254. {
  255. struct pinctrl_dev *pctldev = s->private;
  256. unsigned i, pin;
  257. seq_puts(s, "Pin config settings per pin\n");
  258. seq_puts(s, "Format: pin (name): configs\n");
  259. mutex_lock(&pctldev->mutex);
  260. /* The pin number can be retrived from the pin controller descriptor */
  261. for (i = 0; i < pctldev->desc->npins; i++) {
  262. struct pin_desc *desc;
  263. pin = pctldev->desc->pins[i].number;
  264. desc = pin_desc_get(pctldev, pin);
  265. /* Skip if we cannot search the pin */
  266. if (desc == NULL)
  267. continue;
  268. seq_printf(s, "pin %d (%s):", pin,
  269. desc->name ? desc->name : "unnamed");
  270. pinconf_dump_pin(pctldev, s, pin);
  271. seq_printf(s, "\n");
  272. }
  273. mutex_unlock(&pctldev->mutex);
  274. return 0;
  275. }
  276. static void pinconf_dump_group(struct pinctrl_dev *pctldev,
  277. struct seq_file *s, unsigned selector,
  278. const char *gname)
  279. {
  280. const struct pinconf_ops *ops = pctldev->desc->confops;
  281. /* no-op when not using generic pin config */
  282. pinconf_generic_dump_pins(pctldev, s, gname, 0);
  283. if (ops && ops->pin_config_group_dbg_show)
  284. ops->pin_config_group_dbg_show(pctldev, s, selector);
  285. }
  286. static int pinconf_groups_show(struct seq_file *s, void *what)
  287. {
  288. struct pinctrl_dev *pctldev = s->private;
  289. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  290. unsigned ngroups = pctlops->get_groups_count(pctldev);
  291. unsigned selector = 0;
  292. seq_puts(s, "Pin config settings per pin group\n");
  293. seq_puts(s, "Format: group (name): configs\n");
  294. while (selector < ngroups) {
  295. const char *gname = pctlops->get_group_name(pctldev, selector);
  296. seq_printf(s, "%u (%s):", selector, gname);
  297. pinconf_dump_group(pctldev, s, selector, gname);
  298. seq_printf(s, "\n");
  299. selector++;
  300. }
  301. return 0;
  302. }
  303. static int pinconf_pins_open(struct inode *inode, struct file *file)
  304. {
  305. return single_open(file, pinconf_pins_show, inode->i_private);
  306. }
  307. static int pinconf_groups_open(struct inode *inode, struct file *file)
  308. {
  309. return single_open(file, pinconf_groups_show, inode->i_private);
  310. }
  311. static const struct file_operations pinconf_pins_ops = {
  312. .open = pinconf_pins_open,
  313. .read = seq_read,
  314. .llseek = seq_lseek,
  315. .release = single_release,
  316. };
  317. static const struct file_operations pinconf_groups_ops = {
  318. .open = pinconf_groups_open,
  319. .read = seq_read,
  320. .llseek = seq_lseek,
  321. .release = single_release,
  322. };
  323. #define MAX_NAME_LEN 15
  324. struct dbg_cfg {
  325. enum pinctrl_map_type map_type;
  326. char dev_name[MAX_NAME_LEN+1];
  327. char state_name[MAX_NAME_LEN+1];
  328. char pin_name[MAX_NAME_LEN+1];
  329. };
  330. /*
  331. * Goal is to keep this structure as global in order to simply read the
  332. * pinconf-config file after a write to check config is as expected
  333. */
  334. static struct dbg_cfg pinconf_dbg_conf;
  335. /**
  336. * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
  337. * map, of the dev/pin/state that was last written to pinconf-config file.
  338. * @s: string filled in with config description
  339. * @d: not used
  340. */
  341. static int pinconf_dbg_config_print(struct seq_file *s, void *d)
  342. {
  343. struct pinctrl_maps *maps_node;
  344. const struct pinctrl_map *map;
  345. const struct pinctrl_map *found = NULL;
  346. struct pinctrl_dev *pctldev;
  347. struct dbg_cfg *dbg = &pinconf_dbg_conf;
  348. int i;
  349. mutex_lock(&pinctrl_maps_mutex);
  350. /* Parse the pinctrl map and look for the elected pin/state */
  351. for_each_maps(maps_node, i, map) {
  352. if (map->type != dbg->map_type)
  353. continue;
  354. if (strcmp(map->dev_name, dbg->dev_name))
  355. continue;
  356. if (strcmp(map->name, dbg->state_name))
  357. continue;
  358. if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
  359. /* We found the right pin */
  360. found = map;
  361. break;
  362. }
  363. }
  364. if (!found) {
  365. seq_printf(s, "No config found for dev/state/pin, expected:\n");
  366. seq_printf(s, "Searched dev:%s\n", dbg->dev_name);
  367. seq_printf(s, "Searched state:%s\n", dbg->state_name);
  368. seq_printf(s, "Searched pin:%s\n", dbg->pin_name);
  369. seq_printf(s, "Use: modify config_pin <devname> "\
  370. "<state> <pinname> <value>\n");
  371. goto exit;
  372. }
  373. pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
  374. seq_printf(s, "Dev %s has config of %s in state %s:\n",
  375. dbg->dev_name, dbg->pin_name, dbg->state_name);
  376. pinconf_show_config(s, pctldev, found->data.configs.configs,
  377. found->data.configs.num_configs);
  378. exit:
  379. mutex_unlock(&pinctrl_maps_mutex);
  380. return 0;
  381. }
  382. /**
  383. * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
  384. * map, of a dev/pin/state entry based on user entries to pinconf-config
  385. * @user_buf: contains the modification request with expected format:
  386. * modify <config> <devicename> <state> <name> <newvalue>
  387. * modify is literal string, alternatives like add/delete not supported yet
  388. * <config> is the configuration to be changed. Supported configs are
  389. * "config_pin" or "config_group", alternatives like config_mux are not
  390. * supported yet.
  391. * <devicename> <state> <name> are values that should match the pinctrl-maps
  392. * <newvalue> reflects the new config and is driver dependant
  393. */
  394. static ssize_t pinconf_dbg_config_write(struct file *file,
  395. const char __user *user_buf, size_t count, loff_t *ppos)
  396. {
  397. struct pinctrl_maps *maps_node;
  398. const struct pinctrl_map *map;
  399. const struct pinctrl_map *found = NULL;
  400. struct pinctrl_dev *pctldev;
  401. const struct pinconf_ops *confops = NULL;
  402. struct dbg_cfg *dbg = &pinconf_dbg_conf;
  403. const struct pinctrl_map_configs *configs;
  404. char config[MAX_NAME_LEN+1];
  405. char buf[128];
  406. char *b = &buf[0];
  407. int buf_size;
  408. char *token;
  409. int i;
  410. /* Get userspace string and assure termination */
  411. buf_size = min(count, sizeof(buf) - 1);
  412. if (copy_from_user(buf, user_buf, buf_size))
  413. return -EFAULT;
  414. buf[buf_size] = 0;
  415. /*
  416. * need to parse entry and extract parameters:
  417. * modify configs_pin devicename state pinname newvalue
  418. */
  419. /* Get arg: 'modify' */
  420. token = strsep(&b, " ");
  421. if (!token)
  422. return -EINVAL;
  423. if (strcmp(token, "modify"))
  424. return -EINVAL;
  425. /*
  426. * Get arg type: "config_pin" and "config_group"
  427. * types are supported so far
  428. */
  429. token = strsep(&b, " ");
  430. if (!token)
  431. return -EINVAL;
  432. if (!strcmp(token, "config_pin"))
  433. dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN;
  434. else if (!strcmp(token, "config_group"))
  435. dbg->map_type = PIN_MAP_TYPE_CONFIGS_GROUP;
  436. else
  437. return -EINVAL;
  438. /* get arg 'device_name' */
  439. token = strsep(&b, " ");
  440. if (token == NULL)
  441. return -EINVAL;
  442. if (strlen(token) >= MAX_NAME_LEN)
  443. return -EINVAL;
  444. strncpy(dbg->dev_name, token, MAX_NAME_LEN);
  445. /* get arg 'state_name' */
  446. token = strsep(&b, " ");
  447. if (token == NULL)
  448. return -EINVAL;
  449. if (strlen(token) >= MAX_NAME_LEN)
  450. return -EINVAL;
  451. strncpy(dbg->state_name, token, MAX_NAME_LEN);
  452. /* get arg 'pin_name' */
  453. token = strsep(&b, " ");
  454. if (token == NULL)
  455. return -EINVAL;
  456. if (strlen(token) >= MAX_NAME_LEN)
  457. return -EINVAL;
  458. strncpy(dbg->pin_name, token, MAX_NAME_LEN);
  459. /* get new_value of config' */
  460. token = strsep(&b, " ");
  461. if (token == NULL)
  462. return -EINVAL;
  463. if (strlen(token) >= MAX_NAME_LEN)
  464. return -EINVAL;
  465. strncpy(config, token, MAX_NAME_LEN);
  466. mutex_lock(&pinctrl_maps_mutex);
  467. /* Parse the pinctrl map and look for the selected dev/state/pin */
  468. for_each_maps(maps_node, i, map) {
  469. if (strcmp(map->dev_name, dbg->dev_name))
  470. continue;
  471. if (map->type != dbg->map_type)
  472. continue;
  473. if (strcmp(map->name, dbg->state_name))
  474. continue;
  475. /* we found the right pin / state, so overwrite config */
  476. if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
  477. found = map;
  478. break;
  479. }
  480. }
  481. if (!found) {
  482. count = -EINVAL;
  483. goto exit;
  484. }
  485. pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
  486. if (pctldev)
  487. confops = pctldev->desc->confops;
  488. if (confops && confops->pin_config_dbg_parse_modify) {
  489. configs = &found->data.configs;
  490. for (i = 0; i < configs->num_configs; i++) {
  491. confops->pin_config_dbg_parse_modify(pctldev,
  492. config,
  493. &configs->configs[i]);
  494. }
  495. }
  496. exit:
  497. mutex_unlock(&pinctrl_maps_mutex);
  498. return count;
  499. }
  500. static int pinconf_dbg_config_open(struct inode *inode, struct file *file)
  501. {
  502. return single_open(file, pinconf_dbg_config_print, inode->i_private);
  503. }
  504. static const struct file_operations pinconf_dbg_pinconfig_fops = {
  505. .open = pinconf_dbg_config_open,
  506. .write = pinconf_dbg_config_write,
  507. .read = seq_read,
  508. .llseek = seq_lseek,
  509. .release = single_release,
  510. .owner = THIS_MODULE,
  511. };
  512. void pinconf_init_device_debugfs(struct dentry *devroot,
  513. struct pinctrl_dev *pctldev)
  514. {
  515. debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
  516. devroot, pctldev, &pinconf_pins_ops);
  517. debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
  518. devroot, pctldev, &pinconf_groups_ops);
  519. debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP),
  520. devroot, pctldev, &pinconf_dbg_pinconfig_fops);
  521. }
  522. #endif