pinctrl-da850-pupd.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. u16 arg;
  102. int i;
  103. ena = readl(data->base + DA850_PUPD_ENA);
  104. sel = readl(data->base + DA850_PUPD_SEL);
  105. for (i = 0; i < num_configs; i++) {
  106. param = pinconf_to_config_param(configs[i]);
  107. arg = pinconf_to_config_argument(configs[i]);
  108. switch (param) {
  109. case PIN_CONFIG_BIAS_DISABLE:
  110. ena &= ~BIT(selector);
  111. break;
  112. case PIN_CONFIG_BIAS_PULL_UP:
  113. ena |= BIT(selector);
  114. sel |= BIT(selector);
  115. break;
  116. case PIN_CONFIG_BIAS_PULL_DOWN:
  117. ena |= BIT(selector);
  118. sel &= ~BIT(selector);
  119. break;
  120. default:
  121. return -EINVAL;
  122. }
  123. }
  124. writel(sel, data->base + DA850_PUPD_SEL);
  125. writel(ena, data->base + DA850_PUPD_ENA);
  126. return 0;
  127. }
  128. static const struct pinconf_ops da850_pupd_confops = {
  129. .is_generic = true,
  130. .pin_config_group_get = da850_pupd_pin_config_group_get,
  131. .pin_config_group_set = da850_pupd_pin_config_group_set,
  132. };
  133. static int da850_pupd_probe(struct platform_device *pdev)
  134. {
  135. struct device *dev = &pdev->dev;
  136. struct da850_pupd_data *data;
  137. struct resource *res;
  138. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  139. if (!data)
  140. return -ENOMEM;
  141. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  142. data->base = devm_ioremap_resource(dev, res);
  143. if (IS_ERR(data->base)) {
  144. dev_err(dev, "Could not map resource\n");
  145. return PTR_ERR(data->base);
  146. }
  147. data->desc.name = dev_name(dev);
  148. data->desc.pctlops = &da850_pupd_pctlops;
  149. data->desc.confops = &da850_pupd_confops;
  150. data->desc.owner = THIS_MODULE;
  151. data->pinctrl = devm_pinctrl_register(dev, &data->desc, data);
  152. if (IS_ERR(data->pinctrl)) {
  153. dev_err(dev, "Failed to register pinctrl\n");
  154. return PTR_ERR(data->pinctrl);
  155. }
  156. platform_set_drvdata(pdev, data);
  157. return 0;
  158. }
  159. static int da850_pupd_remove(struct platform_device *pdev)
  160. {
  161. return 0;
  162. }
  163. static const struct of_device_id da850_pupd_of_match[] = {
  164. { .compatible = "ti,da850-pupd" },
  165. { }
  166. };
  167. MODULE_DEVICE_TABLE(of, da850_pupd_of_match);
  168. static struct platform_driver da850_pupd_driver = {
  169. .driver = {
  170. .name = "ti-da850-pupd",
  171. .of_match_table = da850_pupd_of_match,
  172. },
  173. .probe = da850_pupd_probe,
  174. .remove = da850_pupd_remove,
  175. };
  176. module_platform_driver(da850_pupd_driver);
  177. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  178. MODULE_DESCRIPTION("TI DA850/OMAP-L138/AM18XX pullup/pulldown configuration");
  179. MODULE_LICENSE("GPL");