via-gpio.c 7.3 KB

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