gp2ap30_proximity.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2012, Samsung Electronics Co. Ltd. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include "adsp.h"
  18. #define VENDOR "Sharp"
  19. #define CHIP_ID "GP2AP30"
  20. static ssize_t proximity_vendor_show(struct device *dev,
  21. struct device_attribute *attr, char *buf)
  22. {
  23. return sprintf(buf, "%s\n", VENDOR);
  24. }
  25. static ssize_t proximity_name_show(struct device *dev,
  26. struct device_attribute *attr, char *buf)
  27. {
  28. return sprintf(buf, "%s\n", CHIP_ID);
  29. }
  30. /*
  31. static ssize_t proximity_state_show(struct device *dev,
  32. struct device_attribute *attr, char *buf)
  33. {
  34. struct cm3323_p *data = dev_get_drvdata(dev);
  35. return snprintf(buf, PAGE_SIZE, "%u,%u,%u,%u\n",
  36. data->color[0], data->color[1],
  37. data->color[2], data->color[3]);
  38. return 0;
  39. }*/
  40. static ssize_t proximity_data_show(struct device *dev,
  41. struct device_attribute *attr, char *buf)
  42. {
  43. struct adsp_data *data = dev_get_drvdata(dev);
  44. static int count; /*count for proximity average */
  45. if( !(data->data_ready_flag & 1 << ADSP_FACTORY_MODE_PROX) ){
  46. adsp_unicast(ADSP_FACTORY_MODE_PROX,NETLINK_MESSAGE_GET_RAW_DATA,0,0);
  47. }
  48. while( !(data->data_ready_flag & 1 << ADSP_FACTORY_MODE_PROX) )
  49. msleep(20);
  50. data->prox_average[count] = data->sensor_data[ADSP_FACTORY_MODE_PROX].y;
  51. count++;
  52. if (count == PROX_READ_NUM)
  53. count = 0;
  54. return snprintf(buf, PAGE_SIZE, "%d\n",
  55. data->sensor_data[ADSP_FACTORY_MODE_PROX].y);
  56. }
  57. static ssize_t proximity_avg_show(struct device *dev,
  58. struct device_attribute *attr, char *buf)
  59. {
  60. struct adsp_data *data = dev_get_drvdata(dev);
  61. int min = 0, max = 0, avg = 0;
  62. int i;
  63. int proximity_value = 0;
  64. for (i = 0; i < PROX_READ_NUM; i++) {
  65. proximity_value = data->prox_average[i];
  66. if (proximity_value > 0) {
  67. avg += proximity_value;
  68. if (!i)
  69. min = proximity_value;
  70. else if (proximity_value < min)
  71. min = proximity_value;
  72. if (proximity_value > max)
  73. max = proximity_value;
  74. }
  75. }
  76. avg /= i;
  77. return snprintf(buf, PAGE_SIZE, "%d, %d, %d\n", min, avg, max);
  78. }
  79. static ssize_t proximity_thresh_show(struct device *dev,
  80. struct device_attribute *attr, char *buf)
  81. {
  82. struct adsp_data *data = dev_get_drvdata(dev);
  83. adsp_unicast(ADSP_FACTORY_MODE_PROX,NETLINK_MESSAGE_GET_CALIB_DATA,0,0);
  84. while( !(data->calib_ready_flag & 1 << ADSP_FACTORY_MODE_PROX) )
  85. msleep(20);
  86. data->calib_ready_flag |= 0 << ADSP_FACTORY_MODE_PROX;
  87. return sprintf(buf, "%d\n", data->sensor_calib_data[ADSP_FACTORY_MODE_PROX].y);
  88. }
  89. static ssize_t proximity_offset_pass_show(struct device *dev,
  90. struct device_attribute *attr, char *buf)
  91. {
  92. struct adsp_data *data = dev_get_drvdata(dev);
  93. adsp_unicast(ADSP_FACTORY_MODE_PROX,NETLINK_MESSAGE_GET_CALIB_DATA,0,0);
  94. while( !(data->calib_ready_flag & 1 << ADSP_FACTORY_MODE_PROX) )
  95. msleep(20);
  96. data->calib_ready_flag |= 0 << ADSP_FACTORY_MODE_PROX;
  97. return sprintf(buf, "%d\n", data->sensor_calib_data[ADSP_FACTORY_MODE_PROX].x);
  98. }
  99. static ssize_t proximity_cal_show(struct device *dev,
  100. struct device_attribute *attr, char *buf)
  101. {
  102. struct adsp_data *data = dev_get_drvdata(dev);
  103. adsp_unicast(ADSP_FACTORY_MODE_PROX,NETLINK_MESSAGE_GET_CALIB_DATA,0,0);
  104. while( !(data->calib_ready_flag & 1 << ADSP_FACTORY_MODE_PROX) )
  105. msleep(20);
  106. data->calib_ready_flag |= 0 << ADSP_FACTORY_MODE_PROX;
  107. printk("prox_offset_pass_show %d\n",data->sensor_calib_data[ADSP_FACTORY_MODE_PROX].x);
  108. return sprintf(buf, "%d %d\n", data->sensor_calib_data[ADSP_FACTORY_MODE_PROX].x, data->sensor_calib_data[ADSP_FACTORY_MODE_PROX].y);
  109. }
  110. static ssize_t proximity_cal_store(struct device *dev,
  111. struct device_attribute *attr, const char *buf, size_t size)
  112. {
  113. struct adsp_data *data = dev_get_drvdata(dev);
  114. adsp_unicast(ADSP_FACTORY_MODE_PROX,NETLINK_MESSAGE_CALIB_STORE_DATA,0,0);
  115. while( !(data->calib_store_ready_flag & 1 << ADSP_FACTORY_MODE_PROX) )
  116. msleep(20);
  117. if(data->sensor_calib_result[ADSP_FACTORY_MODE_PROX].nCalibstoreresult < 0)
  118. pr_err("[FACTORY]: %s - prox_do_calibrate() failed\n", __func__);
  119. data->calib_store_ready_flag |= 0 << ADSP_FACTORY_MODE_PROX;
  120. return size;
  121. }
  122. int proximity_factory_init(struct adsp_data *data)
  123. {
  124. return 0;
  125. }
  126. int proximity_factory_exit(struct adsp_data *data)
  127. {
  128. return 0;
  129. }
  130. int proximity_factory_receive_data(struct adsp_data *data)
  131. {
  132. pr_err("(%s): factory \n", __func__);
  133. return 0;
  134. }
  135. static struct adsp_fac_ctl adsp_fact_cb = {
  136. .init_fnc = proximity_factory_init,
  137. .exit_fnc = proximity_factory_exit,
  138. .receive_data_fnc = proximity_factory_receive_data
  139. };
  140. static DEVICE_ATTR(name, 0664, proximity_name_show, NULL);
  141. static DEVICE_ATTR(vendor, 0664, proximity_vendor_show, NULL);
  142. static DEVICE_ATTR(state, 0664, proximity_data_show, NULL);
  143. static DEVICE_ATTR(raw_data, 0664, proximity_data_show, NULL);
  144. static DEVICE_ATTR(thresh, 0664, proximity_thresh_show, NULL);
  145. static DEVICE_ATTR(prox_avg, 0664,proximity_avg_show, NULL);
  146. static DEVICE_ATTR(offset_pass, 0664,proximity_offset_pass_show, NULL);
  147. static DEVICE_ATTR(prox_cal, 0664,proximity_cal_show, proximity_cal_store);
  148. static struct device_attribute *proximity_attrs[] = {
  149. &dev_attr_name,
  150. &dev_attr_vendor,
  151. &dev_attr_state,
  152. &dev_attr_raw_data,
  153. &dev_attr_thresh,
  154. &dev_attr_prox_avg,
  155. &dev_attr_offset_pass,
  156. &dev_attr_prox_cal,
  157. NULL,
  158. };
  159. static int __devinit gp2ap30proximity_factory_init(void)
  160. {
  161. adsp_factory_register("proximity_sensor",ADSP_FACTORY_MODE_PROX,proximity_attrs,&adsp_fact_cb);
  162. pr_err("(%s): factory \n", __func__);
  163. return 0;
  164. }
  165. static void __devexit gp2ap30proximity_factory_exit(void)
  166. {
  167. pr_err("(%s): factory \n", __func__);
  168. }
  169. module_init(gp2ap30proximity_factory_init);
  170. module_exit(gp2ap30proximity_factory_exit);