sensor_dts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (C) 2011-2014 MediaTek Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify it under the terms of the
  5. * GNU General Public License version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  8. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. * See the GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License along with this program.
  12. * If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <linux/of.h>
  15. #include <linux/of_irq.h>
  16. #include <cust_alsps.h>
  17. #include <cust_acc.h>
  18. #include <cust_gyro.h>
  19. #include <cust_mag.h>
  20. #include <cust_baro.h>
  21. #include <cust_hmdy.h>
  22. #define SENSOR_TAG "[Sensor dts] "
  23. #define SENSOR_ERR(fmt, args...) pr_err(SENSOR_TAG fmt, ##args)
  24. #define SENSOR_LOG(fmt, args...) pr_debug(SENSOR_TAG fmt, ##args)
  25. struct acc_hw *get_accel_dts_func(const char *name, struct acc_hw *hw)
  26. {
  27. int i, ret;
  28. u32 i2c_num[] = {0};
  29. u32 i2c_addr[G_CUST_I2C_ADDR_NUM] = {0};
  30. u32 direction[] = {0};
  31. u32 power_id[] = {0};
  32. u32 power_vol[] = {0};
  33. u32 firlen[] = {0};
  34. u32 is_batch_supported[] = {0};
  35. struct device_node *node = NULL;
  36. SENSOR_LOG("Device Tree get accel info!\n");
  37. if (name == NULL)
  38. return NULL;
  39. node = of_find_compatible_node(NULL, NULL, name);
  40. if (node) {
  41. ret = of_property_read_u32_array(node , "i2c_num", i2c_num, ARRAY_SIZE(i2c_num));
  42. if (ret == 0)
  43. hw->i2c_num = i2c_num[0];
  44. ret = of_property_read_u32_array(node , "i2c_addr", i2c_addr, ARRAY_SIZE(i2c_addr));
  45. if (ret == 0) {
  46. for (i = 0; i < G_CUST_I2C_ADDR_NUM; i++)
  47. hw->i2c_addr[i] = i2c_addr[i];
  48. }
  49. ret = of_property_read_u32_array(node , "direction", direction, ARRAY_SIZE(direction));
  50. if (ret == 0)
  51. hw->direction = direction[0];
  52. ret = of_property_read_u32_array(node , "power_id", power_id, ARRAY_SIZE(power_id));
  53. if (ret == 0) {
  54. if (power_id[0] == 0xffff)
  55. hw->power_id = -1;
  56. else
  57. hw->power_id = power_id[0];
  58. }
  59. ret = of_property_read_u32_array(node , "power_vol", power_vol, ARRAY_SIZE(power_vol));
  60. if (ret == 0)
  61. hw->power_vol = power_vol[0];
  62. ret = of_property_read_u32_array(node , "firlen", firlen, ARRAY_SIZE(firlen));
  63. if (ret == 0)
  64. hw->firlen = firlen[0];
  65. ret = of_property_read_u32_array(node , "is_batch_supported",
  66. is_batch_supported, ARRAY_SIZE(is_batch_supported));
  67. if (ret == 0)
  68. hw->is_batch_supported = is_batch_supported[0];
  69. } else {
  70. SENSOR_ERR("Device Tree: can not find accel node!. Go to use old cust info\n");
  71. return NULL;
  72. }
  73. return hw;
  74. }
  75. struct alsps_hw *get_alsps_dts_func(const char *name, struct alsps_hw *hw)
  76. {
  77. int i, ret;
  78. u32 i2c_num[] = {0};
  79. u32 i2c_addr[C_CUST_I2C_ADDR_NUM] = {0};
  80. u32 power_id[] = {0};
  81. u32 power_vol[] = {0};
  82. u32 polling_mode_ps[] = {0};
  83. u32 polling_mode_als[] = {0};
  84. u32 als_level[C_CUST_ALS_LEVEL-1] = {0};
  85. u32 als_value[C_CUST_ALS_LEVEL] = {0};
  86. u32 ps_threshold_high[] = {0};
  87. u32 ps_threshold_low[] = {0};
  88. u32 is_batch_supported_ps[] = {0};
  89. u32 is_batch_supported_als[] = {0};
  90. struct device_node *node = NULL;
  91. SENSOR_LOG("Device Tree get alsps info!\n");
  92. if (name == NULL)
  93. return NULL;
  94. node = of_find_compatible_node(NULL, NULL, name);
  95. if (node) {
  96. ret = of_property_read_u32_array(node , "i2c_num", i2c_num, ARRAY_SIZE(i2c_num));
  97. if (ret == 0)
  98. hw->i2c_num = i2c_num[0];
  99. ret = of_property_read_u32_array(node , "i2c_addr", i2c_addr, ARRAY_SIZE(i2c_addr));
  100. if (ret == 0) {
  101. for (i = 0; i < C_CUST_I2C_ADDR_NUM; i++)
  102. hw->i2c_addr[i] = i2c_addr[i];
  103. }
  104. ret = of_property_read_u32_array(node , "power_id", power_id, ARRAY_SIZE(power_id));
  105. if (ret == 0) {
  106. if (power_id[0] == 0xffff)
  107. hw->power_id = -1;
  108. else
  109. hw->power_id = power_id[0];
  110. }
  111. ret = of_property_read_u32_array(node , "power_vol", power_vol, ARRAY_SIZE(power_vol));
  112. if (ret == 0)
  113. hw->power_vol = power_vol[0];
  114. ret = of_property_read_u32_array(node , "als_level", als_level, ARRAY_SIZE(als_level));
  115. if (ret == 0) {
  116. for (i = 0; i < ARRAY_SIZE(als_level); i++)
  117. hw->als_level[i] = als_level[i];
  118. }
  119. ret = of_property_read_u32_array(node , "als_value", als_value, ARRAY_SIZE(als_value));
  120. if (ret == 0) {
  121. for (i = 0; i < ARRAY_SIZE(als_value); i++)
  122. hw->als_value[i] = als_value[i];
  123. }
  124. ret = of_property_read_u32_array(node , "polling_mode_ps", polling_mode_ps, ARRAY_SIZE(polling_mode_ps));
  125. if (ret == 0)
  126. hw->polling_mode_ps = polling_mode_ps[0];
  127. ret = of_property_read_u32_array(node , "polling_mode_als", polling_mode_als, ARRAY_SIZE(polling_mode_als));
  128. if (ret == 0)
  129. hw->polling_mode_als = polling_mode_als[0];
  130. ret = of_property_read_u32_array(node , "ps_threshold_high", ps_threshold_high, ARRAY_SIZE(ps_threshold_high));
  131. if (ret == 0)
  132. hw->ps_threshold_high = ps_threshold_high[0];
  133. ret = of_property_read_u32_array(node , "ps_threshold_low", ps_threshold_low, ARRAY_SIZE(ps_threshold_low));
  134. if (ret == 0)
  135. hw->ps_threshold_low = ps_threshold_low[0];
  136. ret = of_property_read_u32_array(node , "is_batch_supported_ps", is_batch_supported_ps,
  137. ARRAY_SIZE(is_batch_supported_ps));
  138. if (ret == 0)
  139. hw->is_batch_supported_ps = is_batch_supported_ps[0];
  140. ret = of_property_read_u32_array(node , "is_batch_supported_als", is_batch_supported_als,
  141. ARRAY_SIZE(is_batch_supported_als));
  142. if (ret == 0)
  143. hw->is_batch_supported_als = is_batch_supported_als[0];
  144. } else {
  145. SENSOR_ERR("Device Tree: can not find alsps node!. Go to use old cust info\n");
  146. return NULL;
  147. }
  148. return hw;
  149. }
  150. struct mag_hw *get_mag_dts_func(const char *name, struct mag_hw *hw)
  151. {
  152. int i, ret;
  153. u32 i2c_num[] = {0};
  154. u32 i2c_addr[M_CUST_I2C_ADDR_NUM] = {0};
  155. u32 direction[] = {0};
  156. u32 power_id[] = {0};
  157. u32 power_vol[] = {0};
  158. u32 is_batch_supported[] = {0};
  159. struct device_node *node = NULL;
  160. SENSOR_LOG("Device Tree get mag info!\n");
  161. if (name == NULL)
  162. return NULL;
  163. node = of_find_compatible_node(NULL, NULL, name);
  164. if (node) {
  165. ret = of_property_read_u32_array(node , "i2c_num", i2c_num, ARRAY_SIZE(i2c_num));
  166. if (ret == 0)
  167. hw->i2c_num = i2c_num[0];
  168. ret = of_property_read_u32_array(node , "i2c_addr", i2c_addr, ARRAY_SIZE(i2c_addr));
  169. if (ret == 0) {
  170. for (i = 0; i < M_CUST_I2C_ADDR_NUM; i++)
  171. hw->i2c_addr[i] = i2c_addr[i];
  172. }
  173. ret = of_property_read_u32_array(node , "direction", direction, ARRAY_SIZE(direction));
  174. if (ret == 0)
  175. hw->direction = direction[0];
  176. ret = of_property_read_u32_array(node , "power_id", power_id, ARRAY_SIZE(power_id));
  177. if (ret == 0) {
  178. if (power_id[0] == 0xffff)
  179. hw->power_id = -1;
  180. else
  181. hw->power_id = power_id[0];
  182. }
  183. ret = of_property_read_u32_array(node , "power_vol", power_vol, ARRAY_SIZE(power_vol));
  184. if (ret == 0)
  185. hw->power_vol = power_vol[0];
  186. ret = of_property_read_u32_array(node , "is_batch_supported", is_batch_supported,
  187. ARRAY_SIZE(is_batch_supported));
  188. if (ret == 0)
  189. hw->is_batch_supported = is_batch_supported[0];
  190. } else {
  191. SENSOR_ERR("Device Tree: can not find mag node!. Go to use old cust info\n");
  192. return NULL;
  193. }
  194. return hw;
  195. }
  196. struct gyro_hw *get_gyro_dts_func(const char *name, struct gyro_hw *hw)
  197. {
  198. int i, ret;
  199. u32 i2c_num[] = {0};
  200. u32 i2c_addr[C_CUST_I2C_ADDR_NUM] = {0};
  201. u32 direction[] = {0};
  202. u32 power_id[] = {0};
  203. u32 power_vol[] = {0};
  204. u32 firlen[] = {0};
  205. u32 is_batch_supported[] = {0};
  206. struct device_node *node = NULL;
  207. SENSOR_LOG("Device Tree get gyro info!\n");
  208. if (name == NULL)
  209. return NULL;
  210. node = of_find_compatible_node(NULL, NULL, name);
  211. if (node) {
  212. ret = of_property_read_u32_array(node , "i2c_num", i2c_num, ARRAY_SIZE(i2c_num));
  213. if (ret == 0)
  214. hw->i2c_num = i2c_num[0];
  215. ret = of_property_read_u32_array(node , "i2c_addr", i2c_addr, ARRAY_SIZE(i2c_addr));
  216. if (ret == 0) {
  217. for (i = 0; i < GYRO_CUST_I2C_ADDR_NUM; i++)
  218. hw->i2c_addr[i] = i2c_addr[i];
  219. }
  220. ret = of_property_read_u32_array(node , "direction", direction, ARRAY_SIZE(direction));
  221. if (ret == 0)
  222. hw->direction = direction[0];
  223. ret = of_property_read_u32_array(node , "power_id", power_id, ARRAY_SIZE(power_id));
  224. if (ret == 0) {
  225. if (power_id[0] == 0xffff)
  226. hw->power_id = -1;
  227. else
  228. hw->power_id = power_id[0];
  229. }
  230. ret = of_property_read_u32_array(node , "power_vol", power_vol, ARRAY_SIZE(power_vol));
  231. if (ret == 0)
  232. hw->power_vol = power_vol[0];
  233. ret = of_property_read_u32_array(node , "firlen", firlen, ARRAY_SIZE(firlen));
  234. if (ret == 0)
  235. hw->firlen = firlen[0];
  236. ret = of_property_read_u32_array(node , "is_batch_supported", is_batch_supported,
  237. ARRAY_SIZE(is_batch_supported));
  238. if (ret == 0)
  239. hw->is_batch_supported = is_batch_supported[0];
  240. } else {
  241. SENSOR_ERR("Device Tree: can not find gyro node!. Go to use old cust info\n");
  242. return NULL;
  243. }
  244. return hw;
  245. }
  246. struct baro_hw *get_baro_dts_func(const char *name, struct baro_hw *hw)
  247. {
  248. int i, ret;
  249. u32 i2c_num[] = {0};
  250. u32 i2c_addr[C_CUST_I2C_ADDR_NUM] = {0};
  251. u32 direction[] = {0};
  252. u32 power_id[] = {0};
  253. u32 power_vol[] = {0};
  254. u32 firlen[] = {0};
  255. u32 is_batch_supported[] = {0};
  256. struct device_node *node = NULL;
  257. SENSOR_LOG("Device Tree get gyro info!\n");
  258. if (name == NULL)
  259. return NULL;
  260. node = of_find_compatible_node(NULL, NULL, name);
  261. if (node) {
  262. ret = of_property_read_u32_array(node , "i2c_num", i2c_num, ARRAY_SIZE(i2c_num));
  263. if (ret == 0)
  264. hw->i2c_num = i2c_num[0];
  265. ret = of_property_read_u32_array(node , "i2c_addr", i2c_addr, ARRAY_SIZE(i2c_addr));
  266. if (ret == 0) {
  267. for (i = 0; i < GYRO_CUST_I2C_ADDR_NUM; i++)
  268. hw->i2c_addr[i] = i2c_addr[i];
  269. }
  270. ret = of_property_read_u32_array(node , "direction", direction, ARRAY_SIZE(direction));
  271. if (ret == 0)
  272. hw->direction = direction[0];
  273. ret = of_property_read_u32_array(node , "power_id", power_id, ARRAY_SIZE(power_id));
  274. if (ret == 0) {
  275. if (power_id[0] == 0xffff)
  276. hw->power_id = -1;
  277. else
  278. hw->power_id = power_id[0];
  279. }
  280. ret = of_property_read_u32_array(node , "power_vol", power_vol, ARRAY_SIZE(power_vol));
  281. if (ret == 0)
  282. hw->power_vol = power_vol[0];
  283. ret = of_property_read_u32_array(node , "firlen", firlen, ARRAY_SIZE(firlen));
  284. if (ret == 0)
  285. hw->firlen = firlen[0];
  286. ret = of_property_read_u32_array(node , "is_batch_supported", is_batch_supported,
  287. ARRAY_SIZE(is_batch_supported));
  288. if (ret == 0)
  289. hw->is_batch_supported = is_batch_supported[0];
  290. } else {
  291. SENSOR_ERR("Device Tree: can not find gyro node!. Go to use old cust info\n");
  292. return NULL;
  293. }
  294. return hw;
  295. }
  296. struct hmdy_hw *get_hmdy_dts_func(const char *name, struct hmdy_hw *hw)
  297. {
  298. int i, ret;
  299. u32 i2c_num[] = {0};
  300. u32 i2c_addr[C_CUST_I2C_ADDR_NUM] = {0};
  301. u32 direction[] = {0};
  302. u32 power_id[] = {0};
  303. u32 power_vol[] = {0};
  304. u32 firlen[] = {0};
  305. u32 is_batch_supported[] = {0};
  306. struct device_node *node = NULL;
  307. SENSOR_LOG("Device Tree get gyro info!\n");
  308. if (name == NULL)
  309. return NULL;
  310. node = of_find_compatible_node(NULL, NULL, name);
  311. if (node) {
  312. ret = of_property_read_u32_array(node , "i2c_num", i2c_num, ARRAY_SIZE(i2c_num));
  313. if (ret == 0)
  314. hw->i2c_num = i2c_num[0];
  315. ret = of_property_read_u32_array(node , "i2c_addr", i2c_addr, ARRAY_SIZE(i2c_addr));
  316. if (ret == 0) {
  317. for (i = 0; i < GYRO_CUST_I2C_ADDR_NUM; i++)
  318. hw->i2c_addr[i] = i2c_addr[i];
  319. }
  320. ret = of_property_read_u32_array(node , "direction", direction, ARRAY_SIZE(direction));
  321. if (ret == 0)
  322. hw->direction = direction[0];
  323. ret = of_property_read_u32_array(node , "power_id", power_id, ARRAY_SIZE(power_id));
  324. if (ret == 0) {
  325. if (power_id[0] == 0xffff)
  326. hw->power_id = -1;
  327. else
  328. hw->power_id = power_id[0];
  329. }
  330. ret = of_property_read_u32_array(node , "power_vol", power_vol, ARRAY_SIZE(power_vol));
  331. if (ret == 0)
  332. hw->power_vol = power_vol[0];
  333. ret = of_property_read_u32_array(node , "firlen", firlen, ARRAY_SIZE(firlen));
  334. if (ret == 0)
  335. hw->firlen = firlen[0];
  336. ret = of_property_read_u32_array(node , "is_batch_supported", is_batch_supported,
  337. ARRAY_SIZE(is_batch_supported));
  338. if (ret == 0)
  339. hw->is_batch_supported = is_batch_supported[0];
  340. } else {
  341. SENSOR_ERR("Device Tree: can not find gyro node!. Go to use old cust info\n");
  342. return NULL;
  343. }
  344. return hw;
  345. }