pinctrl-da850-pupd.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Pinconf driver for TI DA850/OMAP-L138/AM18XX pullup/pulldown groups
  3. *
  4. * Copyright (C) 2016 David Lechner
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/device.h>
  17. #include <linux/io.h>
  18. #include <linux/ioport.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/module.h>
  21. #include <linux/pinctrl/pinconf.h>
  22. #include <linux/pinctrl/pinconf-generic.h>
  23. #include <linux/pinctrl/pinctrl.h>
  24. #include <linux/platform_device.h>
  25. #define DA850_PUPD_ENA 0x00
  26. #define DA850_PUPD_SEL 0x04
  27. struct da850_pupd_data {
  28. void __iomem *base;
  29. struct pinctrl_desc desc;
  30. struct pinctrl_dev *pinctrl;
  31. };
  32. static const char * const da850_pupd_group_names[] = {
  33. "cp0", "cp1", "cp2", "cp3", "cp4", "cp5", "cp6", "cp7",
  34. "cp8", "cp9", "cp10", "cp11", "cp12", "cp13", "cp14", "cp15",
  35. "cp16", "cp17", "cp18", "cp19", "cp20", "cp21", "cp22", "cp23",
  36. "cp24", "cp25", "cp26", "cp27", "cp28", "cp29", "cp30", "cp31",
  37. };
  38. static int da850_pupd_get_groups_count(struct pinctrl_dev *pctldev)
  39. {
  40. return ARRAY_SIZE(da850_pupd_group_names);
  41. }
  42. static const char *da850_pupd_get_group_name(struct pinctrl_dev *pctldev,
  43. unsigned int selector)
  44. {
  45. return da850_pupd_group_names[selector];
  46. }
  47. static int da850_pupd_get_group_pins(struct pinctrl_dev *pctldev,
  48. unsigned int selector,
  49. const unsigned int **pins,
  50. unsigned int *num_pins)
  51. {
  52. *num_pins = 0;
  53. return 0;
  54. }
  55. static const struct pinctrl_ops da850_pupd_pctlops = {
  56. .get_groups_count = da850_pupd_get_groups_count,
  57. .get_group_name = da850_pupd_get_group_name,
  58. .get_group_pins = da850_pupd_get_group_pins,
  59. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  60. .dt_free_map = pinconf_generic_dt_free_map,
  61. };
  62. static int da850_pupd_pin_config_group_get(struct pinctrl_dev *pctldev,
  63. unsigned int selector,
  64. unsigned long *config)
  65. {
  66. struct da850_pupd_data *data = pinctrl_dev_get_drvdata(pctldev);
  67. enum pin_config_param param = pinconf_to_config_param(*config);
  68. u32 val;
  69. u16 arg;
  70. val = readl(data->base + DA850_PUPD_ENA);
  71. arg = !!(~val & BIT(selector));
  72. switch (param) {
  73. case PIN_CONFIG_BIAS_DISABLE:
  74. break;
  75. case PIN_CONFIG_BIAS_PULL_UP:
  76. case PIN_CONFIG_BIAS_PULL_DOWN:
  77. if (arg) {
  78. /* bias is disabled */
  79. arg = 0;
  80. break;
  81. }
  82. val = readl(data->base + DA850_PUPD_SEL);
  83. if (param == PIN_CONFIG_BIAS_PULL_DOWN)
  84. val = ~val;
  85. arg = !!(val & BIT(selector));
  86. break;
  87. default:
  88. return -EINVAL;
  89. }
  90. *config = pinconf_to_config_packed(param, arg);
  91. return 0;
  92. }
  93. static int da850_pupd_pin_config_group_set(struct pinctrl_dev *pctldev,
  94. unsigned int selector,
  95. unsigned long *configs,
  96. unsigned int num_configs)
  97. {
  98. struct da850_pupd_data *data = pinctrl_dev_get_drvdata(pctldev);
  99. u32 ena, sel;
  100. enum pin_config_param param;
  101. int i;
  102. ena = readl(data->base + DA850_PUPD_ENA);
  103. sel = readl(data->base + DA850_PUPD_SEL);
  104. for (i = 0; i < num_configs; i++) {
  105. param = pinconf_to_config_param(configs[i]);
  106. switch (param) {
  107. case PIN_CONFIG_BIAS_DISABLE:
  108. ena &= ~BIT(selector);
  109. break;
  110. case PIN_CONFIG_BIAS_PULL_UP:
  111. ena |= BIT(selector);
  112. sel |= BIT(selector);
  113. break;
  114. case PIN_CONFIG_BIAS_PULL_DOWN:
  115. ena |= BIT(selector);
  116. sel &= ~BIT(selector);
  117. break;
  118. default:
  119. return -EINVAL;
  120. }
  121. }
  122. writel(sel, data->base + DA850_PUPD_SEL);
  123. writel(ena, data->base + DA850_PUPD_ENA);
  124. return 0;
  125. }
  126. static const struct pinconf_ops da850_pupd_confops = {
  127. .is_generic = true,
  128. .pin_config_group_get = da850_pupd_pin_config_group_get,
  129. .pin_config_group_set = da850_pupd_pin_config_group_set,
  130. };
  131. static int da850_pupd_probe(struct platform_device *pdev)
  132. {
  133. struct device *dev = &pdev->dev;
  134. struct da850_pupd_data *data;
  135. struct resource *res;
  136. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  137. if (!data)
  138. return -ENOMEM;
  139. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  140. data->base = devm_ioremap_resource(dev, res);
  141. if (IS_ERR(data->base)) {
  142. dev_err(dev, "Could not map resource\n");
  143. return PTR_ERR(data->base);
  144. }
  145. data->desc.name = dev_name(dev);
  146. data->desc.pctlops = &da850_pupd_pctlops;
  147. data->desc.confops = &da850_pupd_confops;
  148. data->desc.owner = THIS_MODULE;
  149. data->pinctrl = devm_pinctrl_register(dev, &data->desc, data);
  150. if (IS_ERR(data->pinctrl)) {
  151. dev_err(dev, "Failed to register pinctrl\n");
  152. return PTR_ERR(data->pinctrl);
  153. }
  154. platform_set_drvdata(pdev, data);
  155. return 0;
  156. }
  157. static int da850_pupd_remove(struct platform_device *pdev)
  158. {
  159. return 0;
  160. }
  161. static const struct of_device_id da850_pupd_of_match[] = {
  162. { .compatible = "ti,da850-pupd" },
  163. { }
  164. };
  165. MODULE_DEVICE_TABLE(of, da850_pupd_of_match);
  166. static struct platform_driver da850_pupd_driver = {
  167. .driver = {
  168. .name = "ti-da850-pupd",
  169. .of_match_table = da850_pupd_of_match,
  170. },
  171. .probe = da850_pupd_probe,
  172. .remove = da850_pupd_remove,
  173. };
  174. module_platform_driver(da850_pupd_driver);
  175. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  176. MODULE_DESCRIPTION("TI DA850/OMAP-L138/AM18XX pullup/pulldown configuration");
  177. MODULE_LICENSE("GPL");