pinctrl-digicolor.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Driver for Conexant Digicolor General Purpose Pin Mapping
  3. *
  4. * Author: Baruch Siach <baruch@tkos.co.il>
  5. *
  6. * Copyright (C) 2015 Paradox Innovation Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * TODO:
  14. * - GPIO interrupt support
  15. * - Pin pad configuration (pull up/down, strength)
  16. */
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/io.h>
  22. #include <linux/gpio.h>
  23. #include <linux/gpio/driver.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/pinctrl/machine.h>
  26. #include <linux/pinctrl/pinconf.h>
  27. #include <linux/pinctrl/pinconf-generic.h>
  28. #include <linux/pinctrl/pinctrl.h>
  29. #include <linux/pinctrl/pinmux.h>
  30. #include "pinctrl-utils.h"
  31. #define DRIVER_NAME "pinctrl-digicolor"
  32. #define GP_CLIENTSEL(clct) ((clct)*8 + 0x20)
  33. #define GP_DRIVE0(clct) (GP_CLIENTSEL(clct) + 2)
  34. #define GP_OUTPUT0(clct) (GP_CLIENTSEL(clct) + 3)
  35. #define GP_INPUT(clct) (GP_CLIENTSEL(clct) + 6)
  36. #define PIN_COLLECTIONS ('R' - 'A' + 1)
  37. #define PINS_PER_COLLECTION 8
  38. #define PINS_COUNT (PIN_COLLECTIONS * PINS_PER_COLLECTION)
  39. struct dc_pinmap {
  40. void __iomem *regs;
  41. struct device *dev;
  42. struct pinctrl_dev *pctl;
  43. struct pinctrl_desc *desc;
  44. const char *pin_names[PINS_COUNT];
  45. struct gpio_chip chip;
  46. spinlock_t lock;
  47. };
  48. static int dc_get_groups_count(struct pinctrl_dev *pctldev)
  49. {
  50. return PINS_COUNT;
  51. }
  52. static const char *dc_get_group_name(struct pinctrl_dev *pctldev,
  53. unsigned selector)
  54. {
  55. struct dc_pinmap *pmap = pinctrl_dev_get_drvdata(pctldev);
  56. /* Exactly one group per pin */
  57. return pmap->desc->pins[selector].name;
  58. }
  59. static int dc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  60. const unsigned **pins,
  61. unsigned *num_pins)
  62. {
  63. struct dc_pinmap *pmap = pinctrl_dev_get_drvdata(pctldev);
  64. *pins = &pmap->desc->pins[selector].number;
  65. *num_pins = 1;
  66. return 0;
  67. }
  68. static struct pinctrl_ops dc_pinctrl_ops = {
  69. .get_groups_count = dc_get_groups_count,
  70. .get_group_name = dc_get_group_name,
  71. .get_group_pins = dc_get_group_pins,
  72. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  73. .dt_free_map = pinctrl_utils_dt_free_map,
  74. };
  75. static const char *const dc_functions[] = {
  76. "gpio",
  77. "client_a",
  78. "client_b",
  79. "client_c",
  80. };
  81. static int dc_get_functions_count(struct pinctrl_dev *pctldev)
  82. {
  83. return ARRAY_SIZE(dc_functions);
  84. }
  85. static const char *dc_get_fname(struct pinctrl_dev *pctldev, unsigned selector)
  86. {
  87. return dc_functions[selector];
  88. }
  89. static int dc_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
  90. const char * const **groups,
  91. unsigned * const num_groups)
  92. {
  93. struct dc_pinmap *pmap = pinctrl_dev_get_drvdata(pctldev);
  94. *groups = pmap->pin_names;
  95. *num_groups = PINS_COUNT;
  96. return 0;
  97. }
  98. static void dc_client_sel(int pin_num, int *reg, int *bit)
  99. {
  100. *bit = (pin_num % PINS_PER_COLLECTION) * 2;
  101. *reg = GP_CLIENTSEL(pin_num/PINS_PER_COLLECTION);
  102. if (*bit >= PINS_PER_COLLECTION) {
  103. *bit -= PINS_PER_COLLECTION;
  104. *reg += 1;
  105. }
  106. }
  107. static int dc_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
  108. unsigned group)
  109. {
  110. struct dc_pinmap *pmap = pinctrl_dev_get_drvdata(pctldev);
  111. int bit_off, reg_off;
  112. u8 reg;
  113. dc_client_sel(group, &reg_off, &bit_off);
  114. reg = readb_relaxed(pmap->regs + reg_off);
  115. reg &= ~(3 << bit_off);
  116. reg |= (selector << bit_off);
  117. writeb_relaxed(reg, pmap->regs + reg_off);
  118. return 0;
  119. }
  120. static int dc_pmx_request_gpio(struct pinctrl_dev *pcdev,
  121. struct pinctrl_gpio_range *range,
  122. unsigned offset)
  123. {
  124. struct dc_pinmap *pmap = pinctrl_dev_get_drvdata(pcdev);
  125. int bit_off, reg_off;
  126. u8 reg;
  127. dc_client_sel(offset, &reg_off, &bit_off);
  128. reg = readb_relaxed(pmap->regs + reg_off);
  129. if ((reg & (3 << bit_off)) != 0)
  130. return -EBUSY;
  131. return 0;
  132. }
  133. static struct pinmux_ops dc_pmxops = {
  134. .get_functions_count = dc_get_functions_count,
  135. .get_function_name = dc_get_fname,
  136. .get_function_groups = dc_get_groups,
  137. .set_mux = dc_set_mux,
  138. .gpio_request_enable = dc_pmx_request_gpio,
  139. };
  140. static int dc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  141. {
  142. struct dc_pinmap *pmap = container_of(chip, struct dc_pinmap, chip);
  143. int reg_off = GP_DRIVE0(gpio/PINS_PER_COLLECTION);
  144. int bit_off = gpio % PINS_PER_COLLECTION;
  145. u8 drive;
  146. unsigned long flags;
  147. spin_lock_irqsave(&pmap->lock, flags);
  148. drive = readb_relaxed(pmap->regs + reg_off);
  149. drive &= ~BIT(bit_off);
  150. writeb_relaxed(drive, pmap->regs + reg_off);
  151. spin_unlock_irqrestore(&pmap->lock, flags);
  152. return 0;
  153. }
  154. static void dc_gpio_set(struct gpio_chip *chip, unsigned gpio, int value);
  155. static int dc_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
  156. int value)
  157. {
  158. struct dc_pinmap *pmap = container_of(chip, struct dc_pinmap, chip);
  159. int reg_off = GP_DRIVE0(gpio/PINS_PER_COLLECTION);
  160. int bit_off = gpio % PINS_PER_COLLECTION;
  161. u8 drive;
  162. unsigned long flags;
  163. dc_gpio_set(chip, gpio, value);
  164. spin_lock_irqsave(&pmap->lock, flags);
  165. drive = readb_relaxed(pmap->regs + reg_off);
  166. drive |= BIT(bit_off);
  167. writeb_relaxed(drive, pmap->regs + reg_off);
  168. spin_unlock_irqrestore(&pmap->lock, flags);
  169. return 0;
  170. }
  171. static int dc_gpio_get(struct gpio_chip *chip, unsigned gpio)
  172. {
  173. struct dc_pinmap *pmap = container_of(chip, struct dc_pinmap, chip);
  174. int reg_off = GP_INPUT(gpio/PINS_PER_COLLECTION);
  175. int bit_off = gpio % PINS_PER_COLLECTION;
  176. u8 input;
  177. input = readb_relaxed(pmap->regs + reg_off);
  178. return !!(input & BIT(bit_off));
  179. }
  180. static void dc_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
  181. {
  182. struct dc_pinmap *pmap = container_of(chip, struct dc_pinmap, chip);
  183. int reg_off = GP_OUTPUT0(gpio/PINS_PER_COLLECTION);
  184. int bit_off = gpio % PINS_PER_COLLECTION;
  185. u8 output;
  186. unsigned long flags;
  187. spin_lock_irqsave(&pmap->lock, flags);
  188. output = readb_relaxed(pmap->regs + reg_off);
  189. if (value)
  190. output |= BIT(bit_off);
  191. else
  192. output &= ~BIT(bit_off);
  193. writeb_relaxed(output, pmap->regs + reg_off);
  194. spin_unlock_irqrestore(&pmap->lock, flags);
  195. }
  196. static int dc_gpiochip_add(struct dc_pinmap *pmap, struct device_node *np)
  197. {
  198. struct gpio_chip *chip = &pmap->chip;
  199. int ret;
  200. chip->label = DRIVER_NAME;
  201. chip->dev = pmap->dev;
  202. chip->request = gpiochip_generic_request;
  203. chip->free = gpiochip_generic_free;
  204. chip->direction_input = dc_gpio_direction_input;
  205. chip->direction_output = dc_gpio_direction_output;
  206. chip->get = dc_gpio_get;
  207. chip->set = dc_gpio_set;
  208. chip->base = -1;
  209. chip->ngpio = PINS_COUNT;
  210. chip->of_node = np;
  211. chip->of_gpio_n_cells = 2;
  212. spin_lock_init(&pmap->lock);
  213. ret = gpiochip_add(chip);
  214. if (ret < 0)
  215. return ret;
  216. ret = gpiochip_add_pin_range(chip, dev_name(pmap->dev), 0, 0,
  217. PINS_COUNT);
  218. if (ret < 0) {
  219. gpiochip_remove(chip);
  220. return ret;
  221. }
  222. return 0;
  223. }
  224. static int dc_pinctrl_probe(struct platform_device *pdev)
  225. {
  226. struct dc_pinmap *pmap;
  227. struct resource *r;
  228. struct pinctrl_pin_desc *pins;
  229. struct pinctrl_desc *pctl_desc;
  230. char *pin_names;
  231. int name_len = strlen("GP_xx") + 1;
  232. int i, j, ret;
  233. pmap = devm_kzalloc(&pdev->dev, sizeof(*pmap), GFP_KERNEL);
  234. if (!pmap)
  235. return -ENOMEM;
  236. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  237. pmap->regs = devm_ioremap_resource(&pdev->dev, r);
  238. if (IS_ERR(pmap->regs))
  239. return PTR_ERR(pmap->regs);
  240. pins = devm_kzalloc(&pdev->dev, sizeof(*pins)*PINS_COUNT, GFP_KERNEL);
  241. if (!pins)
  242. return -ENOMEM;
  243. pin_names = devm_kzalloc(&pdev->dev, name_len * PINS_COUNT,
  244. GFP_KERNEL);
  245. if (!pin_names)
  246. return -ENOMEM;
  247. for (i = 0; i < PIN_COLLECTIONS; i++) {
  248. for (j = 0; j < PINS_PER_COLLECTION; j++) {
  249. int pin_id = i*PINS_PER_COLLECTION + j;
  250. char *name = &pin_names[pin_id * name_len];
  251. snprintf(name, name_len, "GP_%c%c", 'A'+i, '0'+j);
  252. pins[pin_id].number = pin_id;
  253. pins[pin_id].name = name;
  254. pmap->pin_names[pin_id] = name;
  255. }
  256. }
  257. pctl_desc = devm_kzalloc(&pdev->dev, sizeof(*pctl_desc), GFP_KERNEL);
  258. if (!pctl_desc)
  259. return -ENOMEM;
  260. pctl_desc->name = DRIVER_NAME,
  261. pctl_desc->owner = THIS_MODULE,
  262. pctl_desc->pctlops = &dc_pinctrl_ops,
  263. pctl_desc->pmxops = &dc_pmxops,
  264. pctl_desc->npins = PINS_COUNT;
  265. pctl_desc->pins = pins;
  266. pmap->desc = pctl_desc;
  267. pmap->dev = &pdev->dev;
  268. pmap->pctl = pinctrl_register(pctl_desc, &pdev->dev, pmap);
  269. if (IS_ERR(pmap->pctl)) {
  270. dev_err(&pdev->dev, "pinctrl driver registration failed\n");
  271. return PTR_ERR(pmap->pctl);
  272. }
  273. ret = dc_gpiochip_add(pmap, pdev->dev.of_node);
  274. if (ret < 0) {
  275. pinctrl_unregister(pmap->pctl);
  276. return ret;
  277. }
  278. return 0;
  279. }
  280. static int dc_pinctrl_remove(struct platform_device *pdev)
  281. {
  282. struct dc_pinmap *pmap = platform_get_drvdata(pdev);
  283. pinctrl_unregister(pmap->pctl);
  284. gpiochip_remove(&pmap->chip);
  285. return 0;
  286. }
  287. static const struct of_device_id dc_pinctrl_ids[] = {
  288. { .compatible = "cnxt,cx92755-pinctrl" },
  289. { /* sentinel */ }
  290. };
  291. MODULE_DEVICE_TABLE(of, dc_pinctrl_ids);
  292. static struct platform_driver dc_pinctrl_driver = {
  293. .driver = {
  294. .name = DRIVER_NAME,
  295. .of_match_table = dc_pinctrl_ids,
  296. },
  297. .probe = dc_pinctrl_probe,
  298. .remove = dc_pinctrl_remove,
  299. };
  300. module_platform_driver(dc_pinctrl_driver);