via-gpio.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Support for viafb GPIO ports.
  3. *
  4. * Copyright 2009 Jonathan Corbet <corbet@lwn.net>
  5. * Distributable under version 2 of the GNU General Public License.
  6. */
  7. #include <linux/spinlock.h>
  8. #include <linux/gpio.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/via-core.h>
  11. #include <linux/via-gpio.h>
  12. #include <linux/export.h>
  13. /*
  14. * The ports we know about. Note that the port-25 gpios are not
  15. * mentioned in the datasheet.
  16. */
  17. struct viafb_gpio {
  18. char *vg_name; /* Data sheet name */
  19. u16 vg_io_port;
  20. u8 vg_port_index;
  21. int vg_mask_shift;
  22. };
  23. static struct viafb_gpio viafb_all_gpios[] = {
  24. {
  25. .vg_name = "VGPIO0", /* Guess - not in datasheet */
  26. .vg_io_port = VIASR,
  27. .vg_port_index = 0x25,
  28. .vg_mask_shift = 1
  29. },
  30. {
  31. .vg_name = "VGPIO1",
  32. .vg_io_port = VIASR,
  33. .vg_port_index = 0x25,
  34. .vg_mask_shift = 0
  35. },
  36. {
  37. .vg_name = "VGPIO2", /* aka DISPCLKI0 */
  38. .vg_io_port = VIASR,
  39. .vg_port_index = 0x2c,
  40. .vg_mask_shift = 1
  41. },
  42. {
  43. .vg_name = "VGPIO3", /* aka DISPCLKO0 */
  44. .vg_io_port = VIASR,
  45. .vg_port_index = 0x2c,
  46. .vg_mask_shift = 0
  47. },
  48. {
  49. .vg_name = "VGPIO4", /* DISPCLKI1 */
  50. .vg_io_port = VIASR,
  51. .vg_port_index = 0x3d,
  52. .vg_mask_shift = 1
  53. },
  54. {
  55. .vg_name = "VGPIO5", /* DISPCLKO1 */
  56. .vg_io_port = VIASR,
  57. .vg_port_index = 0x3d,
  58. .vg_mask_shift = 0
  59. },
  60. };
  61. #define VIAFB_NUM_GPIOS ARRAY_SIZE(viafb_all_gpios)
  62. /*
  63. * This structure controls the active GPIOs, which may be a subset
  64. * of those which are known.
  65. */
  66. struct viafb_gpio_cfg {
  67. struct gpio_chip gpio_chip;
  68. struct viafb_dev *vdev;
  69. struct viafb_gpio *active_gpios[VIAFB_NUM_GPIOS];
  70. const char *gpio_names[VIAFB_NUM_GPIOS];
  71. };
  72. /*
  73. * GPIO access functions
  74. */
  75. static void via_gpio_set(struct gpio_chip *chip, unsigned int nr,
  76. int value)
  77. {
  78. struct viafb_gpio_cfg *cfg = container_of(chip,
  79. struct viafb_gpio_cfg,
  80. gpio_chip);
  81. u8 reg;
  82. struct viafb_gpio *gpio;
  83. unsigned long flags;
  84. spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
  85. gpio = cfg->active_gpios[nr];
  86. reg = via_read_reg(VIASR, gpio->vg_port_index);
  87. reg |= 0x40 << gpio->vg_mask_shift; /* output enable */
  88. if (value)
  89. reg |= 0x10 << gpio->vg_mask_shift;
  90. else
  91. reg &= ~(0x10 << gpio->vg_mask_shift);
  92. via_write_reg(VIASR, gpio->vg_port_index, reg);
  93. spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
  94. }
  95. static int via_gpio_dir_out(struct gpio_chip *chip, unsigned int nr,
  96. int value)
  97. {
  98. via_gpio_set(chip, nr, value);
  99. return 0;
  100. }
  101. /*
  102. * Set the input direction. I'm not sure this is right; we should
  103. * be able to do input without disabling output.
  104. */
  105. static int via_gpio_dir_input(struct gpio_chip *chip, unsigned int nr)
  106. {
  107. struct viafb_gpio_cfg *cfg = container_of(chip,
  108. struct viafb_gpio_cfg,
  109. gpio_chip);
  110. struct viafb_gpio *gpio;
  111. unsigned long flags;
  112. spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
  113. gpio = cfg->active_gpios[nr];
  114. via_write_reg_mask(VIASR, gpio->vg_port_index, 0,
  115. 0x40 << gpio->vg_mask_shift);
  116. spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
  117. return 0;
  118. }
  119. static int via_gpio_get(struct gpio_chip *chip, unsigned int nr)
  120. {
  121. struct viafb_gpio_cfg *cfg = container_of(chip,
  122. struct viafb_gpio_cfg,
  123. gpio_chip);
  124. u8 reg;
  125. struct viafb_gpio *gpio;
  126. unsigned long flags;
  127. spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
  128. gpio = cfg->active_gpios[nr];
  129. reg = via_read_reg(VIASR, gpio->vg_port_index);
  130. spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
  131. return reg & (0x04 << gpio->vg_mask_shift);
  132. }
  133. static struct viafb_gpio_cfg viafb_gpio_config = {
  134. .gpio_chip = {
  135. .label = "VIAFB onboard GPIO",
  136. .owner = THIS_MODULE,
  137. .direction_output = via_gpio_dir_out,
  138. .set = via_gpio_set,
  139. .direction_input = via_gpio_dir_input,
  140. .get = via_gpio_get,
  141. .base = -1,
  142. .ngpio = 0,
  143. .can_sleep = 0
  144. }
  145. };
  146. /*
  147. * Manage the software enable bit.
  148. */
  149. static void viafb_gpio_enable(struct viafb_gpio *gpio)
  150. {
  151. via_write_reg_mask(VIASR, gpio->vg_port_index, 0x02, 0x02);
  152. }
  153. static void viafb_gpio_disable(struct viafb_gpio *gpio)
  154. {
  155. via_write_reg_mask(VIASR, gpio->vg_port_index, 0, 0x02);
  156. }
  157. #ifdef CONFIG_PM
  158. static int viafb_gpio_suspend(void *private)
  159. {
  160. return 0;
  161. }
  162. static int viafb_gpio_resume(void *private)
  163. {
  164. int i;
  165. for (i = 0; i < viafb_gpio_config.gpio_chip.ngpio; i += 2)
  166. viafb_gpio_enable(viafb_gpio_config.active_gpios[i]);
  167. return 0;
  168. }
  169. static struct viafb_pm_hooks viafb_gpio_pm_hooks = {
  170. .suspend = viafb_gpio_suspend,
  171. .resume = viafb_gpio_resume
  172. };
  173. #endif /* CONFIG_PM */
  174. /*
  175. * Look up a specific gpio and return the number it was assigned.
  176. */
  177. int viafb_gpio_lookup(const char *name)
  178. {
  179. int i;
  180. for (i = 0; i < viafb_gpio_config.gpio_chip.ngpio; i++)
  181. if (!strcmp(name, viafb_gpio_config.active_gpios[i]->vg_name))
  182. return viafb_gpio_config.gpio_chip.base + i;
  183. return -1;
  184. }
  185. EXPORT_SYMBOL_GPL(viafb_gpio_lookup);
  186. /*
  187. * Platform device stuff.
  188. */
  189. static int viafb_gpio_probe(struct platform_device *platdev)
  190. {
  191. struct viafb_dev *vdev = platdev->dev.platform_data;
  192. struct via_port_cfg *port_cfg = vdev->port_cfg;
  193. int i, ngpio = 0, ret;
  194. struct viafb_gpio *gpio;
  195. unsigned long flags;
  196. /*
  197. * Set up entries for all GPIOs which have been configured to
  198. * operate as such (as opposed to as i2c ports).
  199. */
  200. for (i = 0; i < VIAFB_NUM_PORTS; i++) {
  201. if (port_cfg[i].mode != VIA_MODE_GPIO)
  202. continue;
  203. for (gpio = viafb_all_gpios;
  204. gpio < viafb_all_gpios + VIAFB_NUM_GPIOS; gpio++)
  205. if (gpio->vg_port_index == port_cfg[i].ioport_index) {
  206. viafb_gpio_config.active_gpios[ngpio] = gpio;
  207. viafb_gpio_config.gpio_names[ngpio] =
  208. gpio->vg_name;
  209. ngpio++;
  210. }
  211. }
  212. viafb_gpio_config.gpio_chip.ngpio = ngpio;
  213. viafb_gpio_config.gpio_chip.names = viafb_gpio_config.gpio_names;
  214. viafb_gpio_config.vdev = vdev;
  215. if (ngpio == 0) {
  216. printk(KERN_INFO "viafb: no GPIOs configured\n");
  217. return 0;
  218. }
  219. /*
  220. * Enable the ports. They come in pairs, with a single
  221. * enable bit for both.
  222. */
  223. spin_lock_irqsave(&viafb_gpio_config.vdev->reg_lock, flags);
  224. for (i = 0; i < ngpio; i += 2)
  225. viafb_gpio_enable(viafb_gpio_config.active_gpios[i]);
  226. spin_unlock_irqrestore(&viafb_gpio_config.vdev->reg_lock, flags);
  227. /*
  228. * Get registered.
  229. */
  230. viafb_gpio_config.gpio_chip.base = -1; /* Dynamic */
  231. ret = gpiochip_add(&viafb_gpio_config.gpio_chip);
  232. if (ret) {
  233. printk(KERN_ERR "viafb: failed to add gpios (%d)\n", ret);
  234. viafb_gpio_config.gpio_chip.ngpio = 0;
  235. }
  236. #ifdef CONFIG_PM
  237. viafb_pm_register(&viafb_gpio_pm_hooks);
  238. #endif
  239. return ret;
  240. }
  241. static int viafb_gpio_remove(struct platform_device *platdev)
  242. {
  243. unsigned long flags;
  244. int i;
  245. #ifdef CONFIG_PM
  246. viafb_pm_unregister(&viafb_gpio_pm_hooks);
  247. #endif
  248. /*
  249. * Get unregistered.
  250. */
  251. if (viafb_gpio_config.gpio_chip.ngpio > 0) {
  252. gpiochip_remove(&viafb_gpio_config.gpio_chip);
  253. }
  254. /*
  255. * Disable the ports.
  256. */
  257. spin_lock_irqsave(&viafb_gpio_config.vdev->reg_lock, flags);
  258. for (i = 0; i < viafb_gpio_config.gpio_chip.ngpio; i += 2)
  259. viafb_gpio_disable(viafb_gpio_config.active_gpios[i]);
  260. viafb_gpio_config.gpio_chip.ngpio = 0;
  261. spin_unlock_irqrestore(&viafb_gpio_config.vdev->reg_lock, flags);
  262. return 0;
  263. }
  264. static struct platform_driver via_gpio_driver = {
  265. .driver = {
  266. .name = "viafb-gpio",
  267. },
  268. .probe = viafb_gpio_probe,
  269. .remove = viafb_gpio_remove,
  270. };
  271. int viafb_gpio_init(void)
  272. {
  273. return platform_driver_register(&via_gpio_driver);
  274. }
  275. void viafb_gpio_exit(void)
  276. {
  277. platform_driver_unregister(&via_gpio_driver);
  278. }