solo6x10-gpio.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
  3. *
  4. * Original author:
  5. * Ben Collins <bcollins@ubuntu.com>
  6. *
  7. * Additional work by:
  8. * John Brooks <john.brooks@bluecherry.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/fs.h>
  22. #include <linux/delay.h>
  23. #include <linux/uaccess.h>
  24. #include "solo6x10.h"
  25. static void solo_gpio_mode(struct solo_dev *solo_dev,
  26. unsigned int port_mask, unsigned int mode)
  27. {
  28. int port;
  29. unsigned int ret;
  30. ret = solo_reg_read(solo_dev, SOLO_GPIO_CONFIG_0);
  31. /* To set gpio */
  32. for (port = 0; port < 16; port++) {
  33. if (!((1 << port) & port_mask))
  34. continue;
  35. ret &= (~(3 << (port << 1)));
  36. ret |= ((mode & 3) << (port << 1));
  37. }
  38. solo_reg_write(solo_dev, SOLO_GPIO_CONFIG_0, ret);
  39. /* To set extended gpio - sensor */
  40. ret = solo_reg_read(solo_dev, SOLO_GPIO_CONFIG_1);
  41. for (port = 0; port < 16; port++) {
  42. if (!((1 << (port + 16)) & port_mask))
  43. continue;
  44. if (!mode)
  45. ret &= ~(1 << port);
  46. else
  47. ret |= 1 << port;
  48. }
  49. solo_reg_write(solo_dev, SOLO_GPIO_CONFIG_1, ret);
  50. }
  51. static void solo_gpio_set(struct solo_dev *solo_dev, unsigned int value)
  52. {
  53. solo_reg_write(solo_dev, SOLO_GPIO_DATA_OUT,
  54. solo_reg_read(solo_dev, SOLO_GPIO_DATA_OUT) | value);
  55. }
  56. static void solo_gpio_clear(struct solo_dev *solo_dev, unsigned int value)
  57. {
  58. solo_reg_write(solo_dev, SOLO_GPIO_DATA_OUT,
  59. solo_reg_read(solo_dev, SOLO_GPIO_DATA_OUT) & ~value);
  60. }
  61. static void solo_gpio_config(struct solo_dev *solo_dev)
  62. {
  63. /* Video reset */
  64. solo_gpio_mode(solo_dev, 0x30, 1);
  65. solo_gpio_clear(solo_dev, 0x30);
  66. udelay(100);
  67. solo_gpio_set(solo_dev, 0x30);
  68. udelay(100);
  69. /* Warning: Don't touch the next line unless you're sure of what
  70. * you're doing: first four gpio [0-3] are used for video. */
  71. solo_gpio_mode(solo_dev, 0x0f, 2);
  72. /* We use bit 8-15 of SOLO_GPIO_CONFIG_0 for relay purposes */
  73. solo_gpio_mode(solo_dev, 0xff00, 1);
  74. /* Initially set relay status to 0 */
  75. solo_gpio_clear(solo_dev, 0xff00);
  76. }
  77. int solo_gpio_init(struct solo_dev *solo_dev)
  78. {
  79. solo_gpio_config(solo_dev);
  80. return 0;
  81. }
  82. void solo_gpio_exit(struct solo_dev *solo_dev)
  83. {
  84. solo_gpio_clear(solo_dev, 0x30);
  85. solo_gpio_config(solo_dev);
  86. }