via-gpio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/driver.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 = gpiochip_get_data(chip);
  79. u8 reg;
  80. struct viafb_gpio *gpio;
  81. unsigned long flags;
  82. spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
  83. gpio = cfg->active_gpios[nr];
  84. reg = via_read_reg(VIASR, gpio->vg_port_index);
  85. reg |= 0x40 << gpio->vg_mask_shift; /* output enable */
  86. if (value)
  87. reg |= 0x10 << gpio->vg_mask_shift;
  88. else
  89. reg &= ~(0x10 << gpio->vg_mask_shift);
  90. via_write_reg(VIASR, gpio->vg_port_index, reg);
  91. spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
  92. }
  93. static int via_gpio_dir_out(struct gpio_chip *chip, unsigned int nr,
  94. int value)
  95. {
  96. via_gpio_set(chip, nr, value);
  97. return 0;
  98. }
  99. /*
  100. * Set the input direction. I'm not sure this is right; we should
  101. * be able to do input without disabling output.
  102. */
  103. static int via_gpio_dir_input(struct gpio_chip *chip, unsigned int nr)
  104. {
  105. struct viafb_gpio_cfg *cfg = gpiochip_get_data(chip);
  106. struct viafb_gpio *gpio;
  107. unsigned long flags;
  108. spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
  109. gpio = cfg->active_gpios[nr];
  110. via_write_reg_mask(VIASR, gpio->vg_port_index, 0,
  111. 0x40 << gpio->vg_mask_shift);
  112. spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
  113. return 0;
  114. }
  115. static int via_gpio_get(struct gpio_chip *chip, unsigned int nr)
  116. {
  117. struct viafb_gpio_cfg *cfg = gpiochip_get_data(chip);
  118. u8 reg;
  119. struct viafb_gpio *gpio;
  120. unsigned long flags;
  121. spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
  122. gpio = cfg->active_gpios[nr];
  123. reg = via_read_reg(VIASR, gpio->vg_port_index);
  124. spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
  125. return !!(reg & (0x04 << gpio->vg_mask_shift));
  126. }
  127. static struct viafb_gpio_cfg viafb_gpio_config = {
  128. .gpio_chip = {
  129. .label = "VIAFB onboard GPIO",
  130. .owner = THIS_MODULE,
  131. .direction_output = via_gpio_dir_out,
  132. .set = via_gpio_set,
  133. .direction_input = via_gpio_dir_input,
  134. .get = via_gpio_get,
  135. .base = -1,
  136. .ngpio = 0,
  137. .can_sleep = 0
  138. }
  139. };
  140. /*
  141. * Manage the software enable bit.
  142. */
  143. static void viafb_gpio_enable(struct viafb_gpio *gpio)
  144. {
  145. via_write_reg_mask(VIASR, gpio->vg_port_index, 0x02, 0x02);
  146. }
  147. static void viafb_gpio_disable(struct viafb_gpio *gpio)
  148. {
  149. via_write_reg_mask(VIASR, gpio->vg_port_index, 0, 0x02);
  150. }
  151. #ifdef CONFIG_PM
  152. static int viafb_gpio_suspend(void *private)
  153. {
  154. return 0;
  155. }
  156. static int viafb_gpio_resume(void *private)
  157. {
  158. int i;
  159. for (i = 0; i < viafb_gpio_config.gpio_chip.ngpio; i += 2)
  160. viafb_gpio_enable(viafb_gpio_config.active_gpios[i]);
  161. return 0;
  162. }
  163. static struct viafb_pm_hooks viafb_gpio_pm_hooks = {
  164. .suspend = viafb_gpio_suspend,
  165. .resume = viafb_gpio_resume
  166. };
  167. #endif /* CONFIG_PM */
  168. /*
  169. * Look up a specific gpio and return the number it was assigned.
  170. */
  171. int viafb_gpio_lookup(const char *name)
  172. {
  173. int i;
  174. for (i = 0; i < viafb_gpio_config.gpio_chip.ngpio; i++)
  175. if (!strcmp(name, viafb_gpio_config.active_gpios[i]->vg_name))
  176. return viafb_gpio_config.gpio_chip.base + i;
  177. return -1;
  178. }
  179. EXPORT_SYMBOL_GPL(viafb_gpio_lookup);
  180. /*
  181. * Platform device stuff.
  182. */
  183. static int viafb_gpio_probe(struct platform_device *platdev)
  184. {
  185. struct viafb_dev *vdev = platdev->dev.platform_data;
  186. struct via_port_cfg *port_cfg = vdev->port_cfg;
  187. int i, ngpio = 0, ret;
  188. struct viafb_gpio *gpio;
  189. unsigned long flags;
  190. /*
  191. * Set up entries for all GPIOs which have been configured to
  192. * operate as such (as opposed to as i2c ports).
  193. */
  194. for (i = 0; i < VIAFB_NUM_PORTS; i++) {
  195. if (port_cfg[i].mode != VIA_MODE_GPIO)
  196. continue;
  197. for (gpio = viafb_all_gpios;
  198. gpio < viafb_all_gpios + VIAFB_NUM_GPIOS; gpio++)
  199. if (gpio->vg_port_index == port_cfg[i].ioport_index) {
  200. viafb_gpio_config.active_gpios[ngpio] = gpio;
  201. viafb_gpio_config.gpio_names[ngpio] =
  202. gpio->vg_name;
  203. ngpio++;
  204. }
  205. }
  206. viafb_gpio_config.gpio_chip.ngpio = ngpio;
  207. viafb_gpio_config.gpio_chip.names = viafb_gpio_config.gpio_names;
  208. viafb_gpio_config.vdev = vdev;
  209. if (ngpio == 0) {
  210. printk(KERN_INFO "viafb: no GPIOs configured\n");
  211. return 0;
  212. }
  213. /*
  214. * Enable the ports. They come in pairs, with a single
  215. * enable bit for both.
  216. */
  217. spin_lock_irqsave(&viafb_gpio_config.vdev->reg_lock, flags);
  218. for (i = 0; i < ngpio; i += 2)
  219. viafb_gpio_enable(viafb_gpio_config.active_gpios[i]);
  220. spin_unlock_irqrestore(&viafb_gpio_config.vdev->reg_lock, flags);
  221. /*
  222. * Get registered.
  223. */
  224. viafb_gpio_config.gpio_chip.base = -1; /* Dynamic */
  225. ret = gpiochip_add_data(&viafb_gpio_config.gpio_chip,
  226. &viafb_gpio_config);
  227. if (ret) {
  228. printk(KERN_ERR "viafb: failed to add gpios (%d)\n", ret);
  229. viafb_gpio_config.gpio_chip.ngpio = 0;
  230. }
  231. #ifdef CONFIG_PM
  232. viafb_pm_register(&viafb_gpio_pm_hooks);
  233. #endif
  234. return ret;
  235. }
  236. static int viafb_gpio_remove(struct platform_device *platdev)
  237. {
  238. unsigned long flags;
  239. int i;
  240. #ifdef CONFIG_PM
  241. viafb_pm_unregister(&viafb_gpio_pm_hooks);
  242. #endif
  243. /*
  244. * Get unregistered.
  245. */
  246. if (viafb_gpio_config.gpio_chip.ngpio > 0) {
  247. gpiochip_remove(&viafb_gpio_config.gpio_chip);
  248. }
  249. /*
  250. * Disable the ports.
  251. */
  252. spin_lock_irqsave(&viafb_gpio_config.vdev->reg_lock, flags);
  253. for (i = 0; i < viafb_gpio_config.gpio_chip.ngpio; i += 2)
  254. viafb_gpio_disable(viafb_gpio_config.active_gpios[i]);
  255. viafb_gpio_config.gpio_chip.ngpio = 0;
  256. spin_unlock_irqrestore(&viafb_gpio_config.vdev->reg_lock, flags);
  257. return 0;
  258. }
  259. static struct platform_driver via_gpio_driver = {
  260. .driver = {
  261. .name = "viafb-gpio",
  262. },
  263. .probe = viafb_gpio_probe,
  264. .remove = viafb_gpio_remove,
  265. };
  266. int viafb_gpio_init(void)
  267. {
  268. return platform_driver_register(&via_gpio_driver);
  269. }
  270. void viafb_gpio_exit(void)
  271. {
  272. platform_driver_unregister(&via_gpio_driver);
  273. }