platform_gen6_driver.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2020 VeriSilicon Holdings Co., Ltd.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. *****************************************************************************
  26. *
  27. * The GPL License (GPL)
  28. *
  29. * Copyright (c) 2020 VeriSilicon Holdings Co., Ltd.
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version 2
  34. * of the License, or (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program;
  43. *
  44. *****************************************************************************
  45. *
  46. * Note: This software is released under dual MIT and GPL licenses. A
  47. * recipient may use this file under the terms of either the MIT license or
  48. * GPL License. If you wish to use only one license not the other, you can
  49. * indicate your decision by deleting one of the above license notices in your
  50. * version of this file.
  51. *
  52. *****************************************************************************/
  53. #include <linux/module.h>
  54. #include <linux/uaccess.h>
  55. #include "vvsensor.h"
  56. #include "../native/sensor/sensor_common.h"
  57. int32_t sensor_reset(void *dev);
  58. int32_t sensor_set_clk(void *dev, uint32_t clk);
  59. int32_t sensor_get_clk(void *dev, uint32_t *pclk);
  60. int32_t sensor_set_power(void *dev, uint32_t power);
  61. int32_t sensor_get_power(void *dev, uint32_t *ppower);
  62. int32_t sensor_reset(void *dev)
  63. {
  64. int ret = 0;
  65. struct vvcam_sensor_dev *psensor_dev = (struct vvcam_sensor_dev *)dev;
  66. if (gpio_is_valid(psensor_dev->rst_pin)) {
  67. gpio_direction_output(psensor_dev->rst_pin, 0);
  68. }
  69. if (gpio_is_valid(psensor_dev->pdn_pin)) {
  70. gpio_direction_output(psensor_dev->pdn_pin, 0);
  71. }
  72. udelay(10000);
  73. if (gpio_is_valid(psensor_dev->rst_pin)) {
  74. gpio_direction_output(psensor_dev->rst_pin, 1);
  75. }
  76. if (gpio_is_valid(psensor_dev->pdn_pin)) {
  77. gpio_direction_output(psensor_dev->pdn_pin, 1);
  78. }
  79. udelay(psensor_dev->pdn_delay_us);
  80. return 0;
  81. }
  82. int32_t sensor_set_clk(void *dev, uint32_t clk)
  83. {
  84. return 0;
  85. }
  86. int32_t sensor_get_clk(void *dev, uint32_t *pclk)
  87. {
  88. return 0;
  89. }
  90. int32_t sensor_set_power(void *dev, uint32_t power)
  91. {
  92. int ret = 0, i = 0, set_volt = 0;
  93. struct vvcam_sensor_dev *psensor_dev = (struct vvcam_sensor_dev *)dev;
  94. printk("sensor_set_power enter, power=%d\n", power);
  95. if (psensor_dev->regulators.num <= 0) {
  96. printk("no regulator for %s", psensor_dev->sensor_name);
  97. return -ENODEV;
  98. }
  99. for (i = 0; i < psensor_dev->regulators.num; i++) {
  100. if (gpio_is_valid(psensor_dev->rst_pin)) {
  101. gpio_direction_output(psensor_dev->rst_pin, 0); // make sure reset is low before power up
  102. }
  103. if (gpio_is_valid(psensor_dev->pdn_pin)) {
  104. gpio_direction_output(psensor_dev->pdn_pin, 0);
  105. }
  106. if (power == 1) {
  107. if(psensor_dev->regulators.supply[i] == NULL) {
  108. psensor_dev->regulators.supply[i] = devm_regulator_get(psensor_dev->dev, psensor_dev->regulators.name[i]);
  109. }
  110. if(psensor_dev->regulators.voltage[i] != 0) {
  111. ret = regulator_set_voltage_tol(psensor_dev->regulators.supply[i], psensor_dev->regulators.voltage[i], 0);
  112. if(ret != 0){
  113. pr_err("fail to set voltage to %s\n", psensor_dev->regulators.name[i] );
  114. return -1;
  115. }
  116. set_volt = regulator_get_voltage(psensor_dev->regulators.supply[i]);
  117. pr_info("%s set regulator %s to %d uV\n",psensor_dev->sensor_name, psensor_dev->regulators.name[i], set_volt );
  118. }
  119. ret = regulator_enable(psensor_dev->regulators.supply[i]);
  120. } else if (power == 0) {
  121. if (psensor_dev->regulators.supply[i] == NULL) {
  122. pr_info("the supply already is null\n");
  123. return 0;
  124. }
  125. pr_info("%s disable regulator %s \n",psensor_dev->sensor_name, psensor_dev->regulators.name[i] );
  126. ret = regulator_disable(psensor_dev->regulators.supply[i]);
  127. if (ret == 0) {
  128. devm_regulator_put(psensor_dev->regulators.supply[i]);
  129. psensor_dev->regulators.supply[i] = NULL;
  130. }
  131. } else {
  132. return -1;
  133. }
  134. if (ret)
  135. return ret;
  136. udelay(psensor_dev->regulators.delay_us[i]);
  137. }
  138. return 0;
  139. }
  140. int32_t sensor_get_power(void *dev, uint32_t *ppower)
  141. {
  142. return 0;
  143. }