bmg160.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. /*
  2. * Copyright (C) 2013 Samsung Electronics. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/module.h>
  21. #include <linux/i2c.h>
  22. #include <linux/errno.h>
  23. #include <linux/input.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/slab.h>
  26. #include <linux/delay.h>
  27. #include <linux/of_gpio.h>
  28. #include "sensors_core.h"
  29. #include "bmg160_reg.h"
  30. #define VENDOR_NAME "BOSCH"
  31. #define MODEL_NAME "BMG160"
  32. #define MODULE_NAME "gyro_sensor"
  33. #define I2C_M_WR 0 /* for i2c Write */
  34. #define I2c_M_RD 1 /* for i2c Read */
  35. #define READ_DATA_LENTH 6
  36. #define CALIBRATION_FILE_PATH "/efs/gyro_calibration_data"
  37. #define CALIBRATION_DATA_AMOUNT 20
  38. #define SELFTEST_DATA_AMOUNT 64
  39. #define SELFTEST_LIMITATION_OF_ERROR 5250
  40. #define BMG160_DEFAULT_DELAY 200000000LL
  41. #define BMG160_CHIP_ID 0x0F
  42. #define BMG160_TOP_UPPER_RIGHT 0
  43. #define BMG160_TOP_LOWER_RIGHT 1
  44. #define BMG160_TOP_LOWER_LEFT 2
  45. #define BMG160_TOP_UPPER_LEFT 3
  46. #define BMG160_BOTTOM_UPPER_RIGHT 4
  47. #define BMG160_BOTTOM_LOWER_RIGHT 5
  48. #define BMG160_BOTTOM_LOWER_LEFT 6
  49. #define BMG160_BOTTOM_UPPER_LEFT 7
  50. struct bmg160_v {
  51. union {
  52. s16 v[3];
  53. struct {
  54. s16 x;
  55. s16 y;
  56. s16 z;
  57. };
  58. };
  59. };
  60. struct bmg160_p {
  61. struct i2c_client *client;
  62. struct input_dev *input;
  63. struct device *factory_device;
  64. struct bmg160_v gyrodata;
  65. struct bmg160_v caldata;
  66. struct work_struct work;
  67. struct hrtimer gyro_timer;
  68. struct workqueue_struct *gyro_wq;
  69. ktime_t poll_delay;
  70. atomic_t enable;
  71. u32 chip_pos;
  72. int gyro_dps;
  73. int gyro_int;
  74. int gyro_drdy;
  75. u64 timestamp;
  76. u64 old_timestamp;
  77. };
  78. static int bmg160_open_calibration(struct bmg160_p *);
  79. static int bmg160_i2c_read(struct i2c_client *client,
  80. unsigned char reg_addr, unsigned char *buf)
  81. {
  82. int ret;
  83. struct i2c_msg msg[2];
  84. msg[0].addr = client->addr;
  85. msg[0].flags = I2C_M_WR;
  86. msg[0].len = 1;
  87. msg[0].buf = &reg_addr;
  88. msg[1].addr = client->addr;
  89. msg[1].flags = I2C_M_RD;
  90. msg[1].len = 1;
  91. msg[1].buf = buf;
  92. ret = i2c_transfer(client->adapter, msg, 2);
  93. if (ret < 0) {
  94. pr_err("[SENSOR]: %s - i2c read error %d\n", __func__, ret);
  95. return ret;
  96. }
  97. return 0;
  98. }
  99. static int bmg160_i2c_write(struct i2c_client *client,
  100. unsigned char reg_addr, unsigned char buf)
  101. {
  102. int ret;
  103. struct i2c_msg msg;
  104. unsigned char w_buf[2];
  105. w_buf[0] = reg_addr;
  106. w_buf[1] = buf;
  107. msg.addr = client->addr;
  108. msg.flags = I2C_M_WR;
  109. msg.len = 2;
  110. msg.buf = (char *)w_buf;
  111. ret = i2c_transfer(client->adapter, &msg, 1);
  112. if (ret < 0) {
  113. pr_err("[SENSOR]: %s - i2c write error %d\n", __func__, ret);
  114. return ret;
  115. }
  116. return 0;
  117. }
  118. static int bmg160_get_bw(struct bmg160_p *data, unsigned char *bandwidth)
  119. {
  120. int ret;
  121. unsigned char temp;
  122. ret = bmg160_i2c_read(data->client, BMG160_BW_ADDR__REG, &temp);
  123. *bandwidth = BMG160_GET_BITSLICE(temp, BMG160_BW_ADDR);
  124. return ret;
  125. }
  126. static int bmg160_get_autosleepdur(struct bmg160_p *data,
  127. unsigned char *duration)
  128. {
  129. int ret = 0;
  130. unsigned char temp;
  131. ret = bmg160_i2c_read(data->client,
  132. BMG160_MODE_LPM2_ADDR_AUTOSLEEPDUR__REG, &temp);
  133. *duration = BMG160_GET_BITSLICE(temp,
  134. BMG160_MODE_LPM2_ADDR_AUTOSLEEPDUR);
  135. return ret;
  136. }
  137. static int bmg160_set_autosleepdur(struct bmg160_p *data,
  138. unsigned char duration, unsigned char bandwith)
  139. {
  140. int ret = 0;
  141. unsigned char temp, autosleepduration;
  142. ret = bmg160_i2c_read(data->client,
  143. BMG160_MODE_LPM2_ADDR_AUTOSLEEPDUR__REG, &temp);
  144. switch (bandwith) {
  145. case BMG160_No_Filter:
  146. if (duration > BMG160_4ms_AutoSleepDur)
  147. autosleepduration = duration;
  148. else
  149. autosleepduration = BMG160_4ms_AutoSleepDur;
  150. break;
  151. case BMG160_BW_230Hz:
  152. if (duration > BMG160_4ms_AutoSleepDur)
  153. autosleepduration = duration;
  154. else
  155. autosleepduration = BMG160_4ms_AutoSleepDur;
  156. break;
  157. case BMG160_BW_116Hz:
  158. if (duration > BMG160_4ms_AutoSleepDur)
  159. autosleepduration = duration;
  160. else
  161. autosleepduration = BMG160_4ms_AutoSleepDur;
  162. break;
  163. case BMG160_BW_47Hz:
  164. if (duration > BMG160_5ms_AutoSleepDur)
  165. autosleepduration = duration;
  166. else
  167. autosleepduration = BMG160_5ms_AutoSleepDur;
  168. break;
  169. case BMG160_BW_23Hz:
  170. if (duration > BMG160_10ms_AutoSleepDur)
  171. autosleepduration = duration;
  172. else
  173. autosleepduration = BMG160_10ms_AutoSleepDur;
  174. break;
  175. case BMG160_BW_12Hz:
  176. if (duration > BMG160_20ms_AutoSleepDur)
  177. autosleepduration = duration;
  178. else
  179. autosleepduration = BMG160_20ms_AutoSleepDur;
  180. break;
  181. case BMG160_BW_64Hz:
  182. if (duration > BMG160_10ms_AutoSleepDur)
  183. autosleepduration = duration;
  184. else
  185. autosleepduration = BMG160_10ms_AutoSleepDur;
  186. break;
  187. case BMG160_BW_32Hz:
  188. if (duration > BMG160_20ms_AutoSleepDur)
  189. autosleepduration = duration;
  190. else
  191. autosleepduration = BMG160_20ms_AutoSleepDur;
  192. break;
  193. default:
  194. autosleepduration = BMG160_No_AutoSleepDur;
  195. }
  196. temp = BMG160_SET_BITSLICE(temp, BMG160_MODE_LPM2_ADDR_AUTOSLEEPDUR,
  197. autosleepduration);
  198. ret += bmg160_i2c_write(data->client,
  199. BMG160_MODE_LPM2_ADDR_AUTOSLEEPDUR__REG, temp);
  200. return ret;
  201. }
  202. static int bmg160_get_mode(struct bmg160_p *data, unsigned char *mode)
  203. {
  204. int ret = 0;
  205. unsigned char buf1 = 0;
  206. unsigned char buf2 = 0;
  207. unsigned char buf3 = 0;
  208. ret = bmg160_i2c_read(data->client, BMG160_MODE_LPM1_ADDR, &buf1);
  209. ret += bmg160_i2c_read(data->client, BMG160_MODE_LPM2_ADDR, &buf2);
  210. buf1 = (buf1 & 0xA0) >> 5;
  211. buf3 = (buf2 & 0x40) >> 6;
  212. buf2 = (buf2 & 0x80) >> 7;
  213. if (buf3 == 0x01)
  214. *mode = BMG160_MODE_ADVANCEDPOWERSAVING;
  215. else if ((buf1 == 0x00) && (buf2 == 0x00))
  216. *mode = BMG160_MODE_NORMAL;
  217. else if ((buf1 == 0x01) || (buf1 == 0x05))
  218. *mode = BMG160_MODE_DEEPSUSPEND;
  219. else if ((buf1 == 0x04) && (buf2 == 0x00))
  220. *mode = BMG160_MODE_SUSPEND;
  221. else if ((buf1 == 0x04) && (buf2 == 0x01))
  222. *mode = BMG160_MODE_FASTPOWERUP;
  223. return ret;
  224. }
  225. static int bmg160_set_range(struct bmg160_p *data, unsigned char range)
  226. {
  227. int ret = 0;
  228. unsigned char temp;
  229. ret = bmg160_i2c_read(data->client,
  230. BMG160_RANGE_ADDR_RANGE__REG, &temp);
  231. temp = BMG160_SET_BITSLICE(temp, BMG160_RANGE_ADDR_RANGE, range);
  232. ret += bmg160_i2c_write(data->client,
  233. BMG160_RANGE_ADDR_RANGE__REG, temp);
  234. return ret;
  235. }
  236. static int bmg160_set_bw(struct bmg160_p *data, unsigned char bandwidth)
  237. {
  238. int ret = 0;
  239. unsigned char temp, autosleepduration, mode = 0;
  240. bmg160_get_mode(data, &mode);
  241. if (mode == BMG160_MODE_ADVANCEDPOWERSAVING) {
  242. bmg160_get_autosleepdur(data, &autosleepduration);
  243. bmg160_set_autosleepdur(data, autosleepduration, bandwidth);
  244. }
  245. ret = bmg160_i2c_read(data->client, BMG160_BW_ADDR__REG, &temp);
  246. temp = BMG160_SET_BITSLICE(temp, BMG160_BW_ADDR, bandwidth);
  247. ret += bmg160_i2c_write(data->client, BMG160_BW_ADDR__REG, temp);
  248. pr_info("[SENSOR]: %s - bandwidth = %u, ret = %d\n", __func__, bandwidth, ret);
  249. return ret;
  250. }
  251. static int bmg160_set_mode(struct bmg160_p *data, unsigned char mode)
  252. {
  253. int ret = 0;
  254. unsigned char buf1, buf2, buf3;
  255. unsigned char autosleepduration;
  256. unsigned char v_bw_u8r;
  257. ret = bmg160_i2c_read(data->client, BMG160_MODE_LPM1_ADDR, &buf1);
  258. ret += bmg160_i2c_read(data->client, BMG160_MODE_LPM2_ADDR, &buf2);
  259. switch (mode) {
  260. case BMG160_MODE_NORMAL:
  261. buf1 = BMG160_SET_BITSLICE(buf1, BMG160_MODE_LPM1, 0);
  262. buf2 = BMG160_SET_BITSLICE(buf2,
  263. BMG160_MODE_LPM2_ADDR_FAST_POWERUP, 0);
  264. buf3 = BMG160_SET_BITSLICE(buf2,
  265. BMG160_MODE_LPM2_ADDR_ADV_POWERSAVING, 0);
  266. ret += bmg160_i2c_write(data->client,
  267. BMG160_MODE_LPM1_ADDR, buf1);
  268. mdelay(1);
  269. ret += bmg160_i2c_write(data->client,
  270. BMG160_MODE_LPM2_ADDR, buf3);
  271. break;
  272. case BMG160_MODE_DEEPSUSPEND:
  273. buf1 = BMG160_SET_BITSLICE(buf1, BMG160_MODE_LPM1, 1);
  274. buf2 = BMG160_SET_BITSLICE(buf2,
  275. BMG160_MODE_LPM2_ADDR_FAST_POWERUP, 0);
  276. buf3 = BMG160_SET_BITSLICE(buf2,
  277. BMG160_MODE_LPM2_ADDR_ADV_POWERSAVING, 0);
  278. ret += bmg160_i2c_write(data->client,
  279. BMG160_MODE_LPM1_ADDR, buf1);
  280. mdelay(1);
  281. ret += bmg160_i2c_write(data->client,
  282. BMG160_MODE_LPM2_ADDR, buf3);
  283. break;
  284. case BMG160_MODE_SUSPEND:
  285. buf1 = BMG160_SET_BITSLICE(buf1, BMG160_MODE_LPM1, 4);
  286. buf2 = BMG160_SET_BITSLICE(buf2,
  287. BMG160_MODE_LPM2_ADDR_FAST_POWERUP, 0);
  288. buf3 = BMG160_SET_BITSLICE(buf2,
  289. BMG160_MODE_LPM2_ADDR_ADV_POWERSAVING, 0);
  290. ret += bmg160_i2c_write(data->client,
  291. BMG160_MODE_LPM1_ADDR, buf1);
  292. mdelay(1);
  293. ret += bmg160_i2c_write(data->client,
  294. BMG160_MODE_LPM2_ADDR, buf3);
  295. break;
  296. case BMG160_MODE_FASTPOWERUP:
  297. buf1 = BMG160_SET_BITSLICE(buf1, BMG160_MODE_LPM1, 4);
  298. buf2 = BMG160_SET_BITSLICE(buf2,
  299. BMG160_MODE_LPM2_ADDR_FAST_POWERUP, 1);
  300. buf3 = BMG160_SET_BITSLICE(buf2,
  301. BMG160_MODE_LPM2_ADDR_ADV_POWERSAVING, 0);
  302. ret += bmg160_i2c_write(data->client,
  303. BMG160_MODE_LPM1_ADDR, buf1);
  304. mdelay(1);
  305. ret += bmg160_i2c_write(data->client,
  306. BMG160_MODE_LPM2_ADDR, buf3);
  307. break;
  308. case BMG160_MODE_ADVANCEDPOWERSAVING:
  309. /* Configuring the proper settings for auto
  310. sleep duration */
  311. bmg160_get_bw(data, &v_bw_u8r);
  312. bmg160_get_autosleepdur(data, &autosleepduration);
  313. bmg160_set_autosleepdur(data, autosleepduration, v_bw_u8r);
  314. ret += bmg160_i2c_read(data->client,
  315. BMG160_MODE_LPM2_ADDR, &buf2);
  316. /* Configuring the advanced power saving mode*/
  317. buf1 = BMG160_SET_BITSLICE(buf1, BMG160_MODE_LPM1, 0);
  318. buf2 = BMG160_SET_BITSLICE(buf2,
  319. BMG160_MODE_LPM2_ADDR_FAST_POWERUP, 0);
  320. buf3 = BMG160_SET_BITSLICE(buf2,
  321. BMG160_MODE_LPM2_ADDR_ADV_POWERSAVING, 1);
  322. ret += bmg160_i2c_write(data->client,
  323. BMG160_MODE_LPM1_ADDR, buf1);
  324. mdelay(1);
  325. ret += bmg160_i2c_write(data->client,
  326. BMG160_MODE_LPM2_ADDR, buf3);
  327. break;
  328. default:
  329. ret = -EINVAL;
  330. break;
  331. }
  332. pr_info("[SENSOR]: %s - mode = %u, ret = %d\n", __func__, mode, ret);
  333. return ret;
  334. }
  335. static int bmg160_read_gyro_xyz(struct bmg160_p *data, struct bmg160_v *gyro)
  336. {
  337. int ret = 0, i;
  338. unsigned char temp[READ_DATA_LENTH];
  339. #ifdef CONFIG_SENSORS_BMI058
  340. for (i = 0; i < READ_DATA_LENTH; i++) {
  341. ret += bmg160_i2c_read(data->client,
  342. BMG160_RATE_Y_LSB_VALUEY__REG + i, &temp[i]);
  343. }
  344. temp[0] = BMG160_GET_BITSLICE(temp[0], BMG160_RATE_Y_LSB_VALUEY);
  345. gyro->y = (short)((((short)((signed char)temp[1])) << 8) | (temp[0]));
  346. temp[2] = BMG160_GET_BITSLICE(temp[2], BMG160_RATE_X_LSB_VALUEX);
  347. gyro->x = (short)((((short)((signed char)temp[3])) << 8) | (temp[2]));
  348. #else
  349. for (i = 0; i < READ_DATA_LENTH; i++) {
  350. ret += bmg160_i2c_read(data->client,
  351. BMG160_RATE_X_LSB_VALUEX__REG + i, &temp[i]);
  352. }
  353. temp[0] = BMG160_GET_BITSLICE(temp[0], BMG160_RATE_X_LSB_VALUEX);
  354. gyro->x = (short)((((short)((signed char)temp[1])) << 8) | (temp[0]));
  355. temp[2] = BMG160_GET_BITSLICE(temp[2], BMG160_RATE_Y_LSB_VALUEY);
  356. gyro->y = (short)((((short)((signed char)temp[3])) << 8) | (temp[2]));
  357. #endif
  358. temp[4] = BMG160_GET_BITSLICE(temp[4], BMG160_RATE_Z_LSB_VALUEZ);
  359. gyro->z = (short)((((short)((signed char)temp[5])) << 8) | (temp[4]));
  360. remap_sensor_data(gyro->v, data->chip_pos);
  361. if (data->gyro_dps == BMG160_RANGE_250DPS) {
  362. gyro->x = gyro->x >> 1;
  363. gyro->y = gyro->y >> 1;
  364. gyro->z = gyro->z >> 1;
  365. } else if (data->gyro_dps == BMG160_RANGE_2000DPS) {
  366. gyro->x = gyro->x << 2;
  367. gyro->y = gyro->y << 2;
  368. gyro->z = gyro->z << 2;
  369. }
  370. return ret;
  371. }
  372. static enum hrtimer_restart bmg160_timer_func(struct hrtimer *timer)
  373. {
  374. struct bmg160_p *data = container_of(timer,
  375. struct bmg160_p, gyro_timer);
  376. if (!work_pending(&data->work))
  377. queue_work(data->gyro_wq, &data->work);
  378. hrtimer_forward_now(&data->gyro_timer, data->poll_delay);
  379. return HRTIMER_RESTART;
  380. }
  381. static void bmg160_work_func(struct work_struct *work)
  382. {
  383. int ret;
  384. struct bmg160_v gyro;
  385. struct bmg160_p *data = container_of(work, struct bmg160_p, work);
  386. struct timespec ts;
  387. int time_hi, time_lo;
  388. u64 delay = ktime_to_ns(data->poll_delay);
  389. get_monotonic_boottime(&ts);
  390. data->timestamp = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
  391. ret = bmg160_read_gyro_xyz(data, &gyro);
  392. if (ret < 0)
  393. return;
  394. if (data->old_timestamp != 0 &&
  395. ((data->timestamp - data->old_timestamp) > ktime_to_ms(data->poll_delay) * 1800000LL)) {
  396. u64 shift_timestamp = delay >> 1;
  397. u64 timestamp = 0ULL;
  398. //u64 diff = 0ULL;
  399. for (timestamp = data->old_timestamp + delay; timestamp < data->timestamp - shift_timestamp; timestamp+=delay) {
  400. time_hi = (int)((timestamp & TIME_HI_MASK) >> TIME_HI_SHIFT);
  401. time_lo = (int)(timestamp & TIME_LO_MASK);
  402. input_report_rel(data->input, REL_RX, gyro.x - data->caldata.x);
  403. input_report_rel(data->input, REL_RY, gyro.y - data->caldata.y);
  404. input_report_rel(data->input, REL_RZ, gyro.z - data->caldata.z);
  405. input_report_rel(data->input, REL_X, time_hi);
  406. input_report_rel(data->input, REL_Y, time_lo);
  407. input_sync(data->input);
  408. data->gyrodata = gyro;
  409. }
  410. }
  411. time_hi = (int)((data->timestamp & TIME_HI_MASK) >> TIME_HI_SHIFT);
  412. time_lo = (int)(data->timestamp & TIME_LO_MASK);
  413. input_report_rel(data->input, REL_RX, gyro.x - data->caldata.x);
  414. input_report_rel(data->input, REL_RY, gyro.y - data->caldata.y);
  415. input_report_rel(data->input, REL_RZ, gyro.z - data->caldata.z);
  416. input_report_rel(data->input, REL_X, time_hi);
  417. input_report_rel(data->input, REL_Y, time_lo);
  418. input_sync(data->input);
  419. data->gyrodata = gyro;
  420. data->old_timestamp = data->timestamp;
  421. }
  422. static void bmg160_set_enable(struct bmg160_p *data, int enable)
  423. {
  424. if (enable == ON) {
  425. hrtimer_start(&data->gyro_timer, data->poll_delay,
  426. HRTIMER_MODE_REL);
  427. } else {
  428. hrtimer_cancel(&data->gyro_timer);
  429. cancel_work_sync(&data->work);
  430. }
  431. }
  432. static ssize_t bmg160_enable_show(struct device *dev,
  433. struct device_attribute *attr, char *buf)
  434. {
  435. struct bmg160_p *data = dev_get_drvdata(dev);
  436. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&data->enable));
  437. }
  438. static ssize_t bmg160_enable_store(struct device *dev,
  439. struct device_attribute *attr, const char *buf, size_t size)
  440. {
  441. u8 enable;
  442. int ret, pre_enable;
  443. struct bmg160_p *data = dev_get_drvdata(dev);
  444. ret = kstrtou8(buf, 2, &enable);
  445. if (ret) {
  446. pr_err("[SENSOR]: %s - Invalid Argument\n", __func__);
  447. return ret;
  448. }
  449. pr_info("[SENSOR]: %s - new_value = %u\n", __func__, enable);
  450. pre_enable = atomic_read(&data->enable);
  451. if (enable) {
  452. if (pre_enable == OFF) {
  453. data->old_timestamp = 0LL;
  454. bmg160_open_calibration(data);
  455. bmg160_set_mode(data, BMG160_MODE_NORMAL);
  456. atomic_set(&data->enable, ON);
  457. bmg160_set_enable(data, ON);
  458. }
  459. } else {
  460. if (pre_enable == ON) {
  461. atomic_set(&data->enable, OFF);
  462. bmg160_set_mode(data, BMG160_MODE_SUSPEND);
  463. bmg160_set_enable(data, OFF);
  464. }
  465. }
  466. return size;
  467. }
  468. static ssize_t bmg160_delay_show(struct device *dev,
  469. struct device_attribute *attr, char *buf)
  470. {
  471. struct bmg160_p *data = dev_get_drvdata(dev);
  472. return snprintf(buf, PAGE_SIZE, "%lld\n",
  473. ktime_to_ns(data->poll_delay));
  474. }
  475. static ssize_t bmg160_delay_store(struct device *dev,
  476. struct device_attribute *attr, const char *buf, size_t size)
  477. {
  478. int ret;
  479. int64_t delay;
  480. struct bmg160_p *data = dev_get_drvdata(dev);
  481. ret = kstrtoll(buf, 10, &delay);
  482. if (ret) {
  483. pr_err("[SENSOR]: %s - Invalid Argument\n", __func__);
  484. return ret;
  485. }
  486. if (delay <= 5000000LL)
  487. bmg160_set_bw(data, BMG160_BW_116Hz);
  488. else
  489. bmg160_set_bw(data, BMG160_BW_32Hz);
  490. data->poll_delay = ns_to_ktime(delay);
  491. pr_info("[SENSOR]: %s - poll_delay = %lld\n", __func__, delay);
  492. if (atomic_read(&data->enable) == ON) {
  493. bmg160_set_mode(data, BMG160_MODE_SUSPEND);
  494. bmg160_set_enable(data, OFF);
  495. bmg160_set_mode(data, BMG160_MODE_NORMAL);
  496. bmg160_set_enable(data, ON);
  497. }
  498. return size;
  499. }
  500. static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
  501. bmg160_delay_show, bmg160_delay_store);
  502. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  503. bmg160_enable_show, bmg160_enable_store);
  504. static struct attribute *bmg160_attributes[] = {
  505. &dev_attr_poll_delay.attr,
  506. &dev_attr_enable.attr,
  507. NULL
  508. };
  509. static struct attribute_group bmg160_attribute_group = {
  510. .attrs = bmg160_attributes
  511. };
  512. static ssize_t bmg160_vendor_show(struct device *dev,
  513. struct device_attribute *attr, char *buf)
  514. {
  515. return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR_NAME);
  516. }
  517. static ssize_t bmg160_name_show(struct device *dev,
  518. struct device_attribute *attr, char *buf)
  519. {
  520. return snprintf(buf, PAGE_SIZE, "%s\n", MODEL_NAME);
  521. }
  522. static int bmg160_open_calibration(struct bmg160_p *data)
  523. {
  524. int ret = 0;
  525. mm_segment_t old_fs;
  526. struct file *cal_filp = NULL;
  527. old_fs = get_fs();
  528. set_fs(KERNEL_DS);
  529. cal_filp = filp_open(CALIBRATION_FILE_PATH, O_RDONLY, 0666);
  530. if (IS_ERR(cal_filp)) {
  531. set_fs(old_fs);
  532. ret = PTR_ERR(cal_filp);
  533. data->caldata.x = 0;
  534. data->caldata.y = 0;
  535. data->caldata.z = 0;
  536. pr_info("[SENSOR]: %s - No Calibration\n", __func__);
  537. return ret;
  538. }
  539. ret = cal_filp->f_op->read(cal_filp, (char *)&data->caldata.v,
  540. 3 * sizeof(s16), &cal_filp->f_pos);
  541. if (ret != 3 * sizeof(s16)) {
  542. pr_err("[SENSOR] %s: - Can't read the cal data\n", __func__);
  543. ret = -EIO;
  544. }
  545. filp_close(cal_filp, current->files);
  546. set_fs(old_fs);
  547. pr_info("[SENSOR]: open gyro calibration %d, %d, %d\n",
  548. data->caldata.x, data->caldata.y, data->caldata.z);
  549. if ((data->caldata.x == 0) && (data->caldata.y == 0)
  550. && (data->caldata.z == 0))
  551. return -EIO;
  552. return ret;
  553. }
  554. static int bmg160_save_calibration(struct bmg160_p *data)
  555. {
  556. int ret = 0;
  557. mm_segment_t old_fs;
  558. struct file *cal_filp = NULL;
  559. old_fs = get_fs();
  560. set_fs(KERNEL_DS);
  561. cal_filp = filp_open(CALIBRATION_FILE_PATH,
  562. O_CREAT | O_TRUNC | O_WRONLY, 0666);
  563. if (IS_ERR(cal_filp)) {
  564. set_fs(old_fs);
  565. ret = PTR_ERR(cal_filp);
  566. data->caldata.x = 0;
  567. data->caldata.y = 0;
  568. data->caldata.z = 0;
  569. pr_err("[SENSOR]: %s - cal_filp open failed(%d)\n",
  570. __func__, ret);
  571. return ret;
  572. }
  573. ret = cal_filp->f_op->write(cal_filp, (char *)&data->caldata.v,
  574. 3 * sizeof(s16), &cal_filp->f_pos);
  575. if (ret != 3 * sizeof(s16)) {
  576. pr_err("[SENSOR]: %s - Can't write the caldata to file\n",
  577. __func__);
  578. ret = -EIO;
  579. }
  580. filp_close(cal_filp, current->files);
  581. set_fs(old_fs);
  582. pr_info("[SENSOR]: save gyro calibration %d, %d, %d\n",
  583. data->caldata.x, data->caldata.y, data->caldata.z);
  584. return ret;
  585. }
  586. static int bmg160_get_caldata(struct bmg160_p *data)
  587. {
  588. int sum[3] = { 0, };
  589. int cnt;
  590. struct bmg160_v gyro;
  591. for (cnt = 0; cnt < CALIBRATION_DATA_AMOUNT; cnt++) {
  592. bmg160_read_gyro_xyz(data, &gyro);
  593. sum[0] += gyro.x;
  594. sum[1] += gyro.y;
  595. sum[2] += gyro.z;
  596. mdelay(10);
  597. }
  598. data->caldata.x = (sum[0] / CALIBRATION_DATA_AMOUNT);
  599. data->caldata.y = (sum[1] / CALIBRATION_DATA_AMOUNT);
  600. data->caldata.z = (sum[2] / CALIBRATION_DATA_AMOUNT);
  601. return 0;
  602. }
  603. static ssize_t bmg160_calibration_show(struct device *dev,
  604. struct device_attribute *attr, char *buf)
  605. {
  606. int ret;
  607. struct bmg160_p *data = dev_get_drvdata(dev);
  608. ret = bmg160_open_calibration(data);
  609. if (ret < 0)
  610. pr_err("[SENSOR]: %s - calibration open failed(%d)\n",
  611. __func__, ret);
  612. pr_info("[SENSOR]: %s - cal data %d %d %d - ret : %d\n", __func__,
  613. data->caldata.x, data->caldata.y, data->caldata.z, ret);
  614. return snprintf(buf, PAGE_SIZE, "%d,%d,%d,%d\n", ret, data->caldata.x,
  615. data->caldata.y, data->caldata.z);
  616. }
  617. static ssize_t bmg160_calibration_store(struct device *dev,
  618. struct device_attribute *attr, const char *buf, size_t size)
  619. {
  620. int ret;
  621. int64_t dEnable;
  622. struct bmg160_p *data = dev_get_drvdata(dev);
  623. ret = kstrtoll(buf, 10, &dEnable);
  624. if (ret < 0)
  625. return ret;
  626. if (atomic_read(&data->enable) == ON)
  627. bmg160_set_enable(data, OFF);
  628. else
  629. bmg160_set_mode(data, BMG160_MODE_NORMAL);
  630. msleep(100);
  631. if (dEnable == 1)
  632. bmg160_get_caldata(data);
  633. else
  634. memset(&data->caldata, 0, sizeof(struct bmg160_v));
  635. bmg160_save_calibration(data);
  636. if (atomic_read(&data->enable) == ON)
  637. bmg160_set_enable(data, ON);
  638. else
  639. bmg160_set_mode(data, BMG160_MODE_SUSPEND);
  640. return size;
  641. }
  642. static ssize_t bmg160_raw_data_show(struct device *dev,
  643. struct device_attribute *attr, char *buf)
  644. {
  645. struct bmg160_p *data = dev_get_drvdata(dev);
  646. if (atomic_read(&data->enable) == OFF) {
  647. bmg160_set_mode(data, BMG160_MODE_NORMAL);
  648. msleep(30);
  649. bmg160_read_gyro_xyz(data, &data->gyrodata);
  650. bmg160_set_mode(data, BMG160_MODE_SUSPEND);
  651. }
  652. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
  653. data->gyrodata.x - data->caldata.x,
  654. data->gyrodata.y - data->caldata.y,
  655. data->gyrodata.z - data->caldata.z);
  656. }
  657. static ssize_t bmg160_get_temp(struct device *dev,
  658. struct device_attribute *attr, char *buf)
  659. {
  660. unsigned char tmp;
  661. s8 temperature;
  662. struct bmg160_p *data = dev_get_drvdata(dev);
  663. if (atomic_read(&data->enable) == OFF)
  664. bmg160_set_mode(data, BMG160_MODE_NORMAL);
  665. msleep(100);
  666. bmg160_i2c_read(data->client, BMG160_TEMP_ADDR, &tmp);
  667. temperature = 24 + ((s8)tmp / 2);
  668. if (atomic_read(&data->enable) == OFF)
  669. bmg160_set_mode(data, BMG160_MODE_SUSPEND);
  670. pr_info("[SENSOR]: %s - temperature = %d\n", __func__, temperature);
  671. return sprintf(buf, "%d\n", temperature);
  672. }
  673. static ssize_t bmg160_selftest_dps_store(struct device *dev,
  674. struct device_attribute *attr, const char *buf, size_t size)
  675. {
  676. int newdps = 0;
  677. struct bmg160_p *data = dev_get_drvdata(dev);
  678. sscanf(buf, "%d", &newdps);
  679. if (newdps == 250)
  680. data->gyro_dps = BMG160_RANGE_250DPS;
  681. else if (newdps == 500)
  682. data->gyro_dps = BMG160_RANGE_500DPS;
  683. else if (newdps == 2000)
  684. data->gyro_dps = BMG160_RANGE_2000DPS;
  685. else
  686. data->gyro_dps = BMG160_RANGE_500DPS;
  687. bmg160_set_range(data, data->gyro_dps);
  688. pr_info("[SENSOR]: %s - dps = %d\n", __func__, data->gyro_dps);
  689. return size;
  690. }
  691. static ssize_t bmg160_selftest_dps_show(struct device *dev,
  692. struct device_attribute *attr, char *buf)
  693. {
  694. struct bmg160_p *data = dev_get_drvdata(dev);
  695. return snprintf(buf, PAGE_SIZE, "%d\n", data->gyro_dps);
  696. }
  697. static unsigned char bmg160_selftest(struct bmg160_p *data)
  698. {
  699. int ret = 0;
  700. unsigned char bist = 0;
  701. unsigned char rateok = 0;
  702. ret = bmg160_i2c_read(data->client, BMG160_SELF_TEST_ADDR, &bist);
  703. rateok = BMG160_GET_BITSLICE(bist, BMG160_SELF_TEST_ADDR_RATEOK);
  704. bist = BMG160_SET_BITSLICE(bist, BMG160_SELF_TEST_ADDR_TRIGBIST, 1);
  705. ret += bmg160_i2c_write(data->client,
  706. BMG160_SELF_TEST_ADDR_TRIGBIST__REG, bist);
  707. /* Waiting time to complete the selftest process */
  708. mdelay(10);
  709. /* Reading Selftest result bir bist_failure */
  710. ret += bmg160_i2c_read(data->client,
  711. BMG160_SELF_TEST_ADDR_BISTFAIL__REG, &bist);
  712. if (ret < 0)
  713. pr_err("[SENSOR]: %s - i2c failed %d\n", __func__, ret);
  714. bist = !(BMG160_GET_BITSLICE(bist, BMG160_SELF_TEST_ADDR_BISTFAIL));
  715. pr_info("[SENSOR]: %s - rate %u, bist %u\n", __func__, rateok, bist);
  716. return (rateok && bist);
  717. }
  718. static int bmg160_selftest_show(struct device *dev,
  719. struct device_attribute *attr, char *buf)
  720. {
  721. unsigned char bist, selftest = 0;
  722. int datax_check = 0;
  723. int datay_check = 0;
  724. int dataz_check = 0;
  725. int sum[3], cnt;
  726. struct bmg160_v avg;
  727. struct bmg160_p *data = dev_get_drvdata(dev);
  728. if (atomic_read(&data->enable) == ON)
  729. bmg160_set_enable(data, OFF);
  730. else
  731. bmg160_set_mode(data, BMG160_MODE_NORMAL);
  732. msleep(100);
  733. bist = bmg160_selftest(data);
  734. if (bist == 0)
  735. selftest |= 1;
  736. data->gyro_dps = BMG160_RANGE_2000DPS;
  737. bmg160_set_range(data, data->gyro_dps);
  738. msleep(100);
  739. memset(sum, 0, sizeof(int) * 3);
  740. for (cnt = 0; cnt < SELFTEST_DATA_AMOUNT; cnt++) {
  741. bmg160_read_gyro_xyz(data, &avg);
  742. sum[0] += avg.x;
  743. sum[1] += avg.y;
  744. sum[2] += avg.z;
  745. }
  746. avg.x = (s16)(sum[0] / SELFTEST_DATA_AMOUNT);
  747. avg.y = (s16)(sum[1] / SELFTEST_DATA_AMOUNT);
  748. avg.z = (s16)(sum[2] / SELFTEST_DATA_AMOUNT);
  749. datax_check = (int)abs((2000 * (long)avg.x * 1000) / 32768);
  750. datay_check = (int)abs((2000 * (long)avg.y * 1000) / 32768);
  751. dataz_check = (int)abs((2000 * (long)avg.z * 1000) / 32768);
  752. pr_info("[SENSOR]: %s - x = %d.%03d, y = %d.%03d, z= %d.%03d\n",
  753. __func__, (datax_check / 1000), (datax_check % 1000),
  754. (datay_check / 1000), (datay_check % 1000),
  755. (dataz_check / 1000), (dataz_check % 1000));
  756. data->gyro_dps = BMG160_RANGE_500DPS;
  757. bmg160_set_range(data, data->gyro_dps);
  758. if ((datax_check <= SELFTEST_LIMITATION_OF_ERROR)
  759. && (datay_check <= SELFTEST_LIMITATION_OF_ERROR)
  760. && (dataz_check <= SELFTEST_LIMITATION_OF_ERROR)) {
  761. pr_info("[SENSOR]: %s - Gyro zero rate OK!\n", __func__);
  762. bmg160_get_caldata(data);
  763. bmg160_save_calibration(data);
  764. } else {
  765. pr_info("[SENSOR]: %s - Gyro zero rate NG!\n", __func__);
  766. selftest |= 1;
  767. }
  768. if (atomic_read(&data->enable) == ON)
  769. bmg160_set_enable(data, ON);
  770. else
  771. bmg160_set_mode(data, BMG160_MODE_SUSPEND);
  772. if (selftest == 0)
  773. pr_info("[SENSOR]: %s - Gyro selftest Pass!\n", __func__);
  774. else
  775. pr_err("[SENSOR]: %s - Gyro selftest fail!\n", __func__);
  776. return snprintf(buf, PAGE_SIZE, "%u,%u,%d.%03d,%d.%03d,%d.%03d\n",
  777. selftest ? 0 : 1, bist,
  778. (datax_check / 1000), (datax_check % 1000),
  779. (datay_check / 1000), (datay_check % 1000),
  780. (dataz_check / 1000), (dataz_check % 1000));
  781. }
  782. static DEVICE_ATTR(name, S_IRUGO, bmg160_name_show, NULL);
  783. static DEVICE_ATTR(vendor, S_IRUGO, bmg160_vendor_show, NULL);
  784. static DEVICE_ATTR(calibration, S_IRUGO | S_IWUSR | S_IWGRP,
  785. bmg160_calibration_show, bmg160_calibration_store);
  786. static DEVICE_ATTR(raw_data, S_IRUGO, bmg160_raw_data_show, NULL);
  787. static DEVICE_ATTR(temperature, S_IRUGO, bmg160_get_temp, NULL);
  788. static DEVICE_ATTR(selftest, S_IRUGO, bmg160_selftest_show, NULL);
  789. static DEVICE_ATTR(selftest_dps, S_IRUGO | S_IWUSR | S_IWGRP,
  790. bmg160_selftest_dps_show, bmg160_selftest_dps_store);
  791. static struct device_attribute *sensor_attrs[] = {
  792. &dev_attr_name,
  793. &dev_attr_vendor,
  794. &dev_attr_calibration,
  795. &dev_attr_raw_data,
  796. &dev_attr_temperature,
  797. &dev_attr_selftest,
  798. &dev_attr_selftest_dps,
  799. NULL,
  800. };
  801. static void bmg160_setup_pin(struct bmg160_p *data)
  802. {
  803. int ret;
  804. ret = gpio_request(data->gyro_int, "GYRO_INT");
  805. if (ret < 0) {
  806. pr_err("[SENSOR] %s - gpio %d request failed (%d)\n",
  807. __func__, data->gyro_int, ret);
  808. } else {
  809. ret = gpio_direction_input(data->gyro_int);
  810. if (ret < 0)
  811. pr_err("[SENSOR]: %s - failed to set gpio %d as input"
  812. " (%d)\n", __func__, data->gyro_int, ret);
  813. gpio_free(data->gyro_int);
  814. }
  815. ret = gpio_request(data->gyro_drdy, "GYRO_DRDY");
  816. if (ret < 0) {
  817. pr_err("[SENSOR]: %s - gpio %d request failed (%d)\n",
  818. __func__, data->gyro_drdy, ret);
  819. } else {
  820. ret = gpio_direction_input(data->gyro_drdy);
  821. if (ret < 0)
  822. pr_err("[SENSOR]: %s - failed to set gpio %d as input"
  823. " (%d)\n", __func__, data->gyro_drdy, ret);
  824. gpio_free(data->gyro_drdy);
  825. }
  826. }
  827. static int bmg160_input_init(struct bmg160_p *data)
  828. {
  829. int ret = 0;
  830. struct input_dev *dev;
  831. dev = input_allocate_device();
  832. if (!dev)
  833. return -ENOMEM;
  834. dev->name = MODULE_NAME;
  835. dev->id.bustype = BUS_I2C;
  836. input_set_capability(dev, EV_REL, REL_RX);
  837. input_set_capability(dev, EV_REL, REL_RY);
  838. input_set_capability(dev, EV_REL, REL_RZ);
  839. input_set_capability(dev, EV_REL, REL_X); /* time_hi */
  840. input_set_capability(dev, EV_REL, REL_Y); /* time_lo */
  841. input_set_drvdata(dev, data);
  842. ret = input_register_device(dev);
  843. if (ret < 0) {
  844. input_free_device(dev);
  845. return ret;
  846. }
  847. ret = sensors_create_symlink(&dev->dev.kobj, dev->name);
  848. if (ret < 0) {
  849. input_unregister_device(dev);
  850. return ret;
  851. }
  852. /* sysfs node creation */
  853. ret = sysfs_create_group(&dev->dev.kobj, &bmg160_attribute_group);
  854. if (ret < 0) {
  855. sensors_remove_symlink(&data->input->dev.kobj,
  856. data->input->name);
  857. input_unregister_device(dev);
  858. return ret;
  859. }
  860. data->input = dev;
  861. return 0;
  862. }
  863. static void bmg160_parse_dt(struct bmg160_p *data, struct device *dev)
  864. {
  865. struct device_node *dNode = dev->of_node;
  866. enum of_gpio_flags flags;
  867. if (dNode == NULL) {
  868. pr_err("[SENSOR]: %s - can't find dNode\n", __func__);
  869. data->chip_pos = BMG160_TOP_LOWER_RIGHT;
  870. return;
  871. }
  872. if (of_property_read_u32(dNode,
  873. "bmg160-i2c,chip_pos", &data->chip_pos) < 0)
  874. data->chip_pos = BMG160_TOP_LOWER_RIGHT;
  875. data->gyro_int = of_get_named_gpio_flags(dNode,
  876. "bmg160-i2c,gyro_int-gpio", 0, &flags);
  877. if (data->gyro_int < 0) {
  878. pr_err("[SENSOR]: %s - get gyro_int failed\n", __func__);
  879. return;
  880. }
  881. data->gyro_drdy = of_get_named_gpio_flags(dNode,
  882. "bmg160-i2c,gyro_drdy-gpio", 0, &flags);
  883. if (data->gyro_drdy < 0) {
  884. pr_err("[SENSOR]: %s - gyro_drdy failed\n", __func__);
  885. return;
  886. }
  887. bmg160_setup_pin(data);
  888. }
  889. static int bmg160_probe(struct i2c_client *client,
  890. const struct i2c_device_id *id)
  891. {
  892. int ret = -ENODEV;
  893. struct bmg160_p *data = NULL;
  894. pr_info("[SENSOR]: %s - Probe Start!\n", __func__);
  895. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  896. pr_err("[SENSOR]: %s - i2c_check_functionality error\n",
  897. __func__);
  898. goto exit;
  899. }
  900. data = kzalloc(sizeof(struct bmg160_p), GFP_KERNEL);
  901. if (data == NULL) {
  902. pr_err("[SENSOR]: %s - kzalloc error\n", __func__);
  903. ret = -ENOMEM;
  904. goto exit_kzalloc;
  905. }
  906. bmg160_parse_dt(data, &client->dev);
  907. i2c_set_clientdata(client, data);
  908. data->client = client;
  909. /* read chip id */
  910. ret = i2c_smbus_read_word_data(data->client, BMG160_CHIP_ID_REG);
  911. if ((ret & 0x00ff) != BMG160_CHIP_ID) {
  912. pr_err("[SENSOR]: %s - chip id failed 0x%x\n",
  913. __func__, (unsigned int)ret & 0x00ff);
  914. ret = -ENODEV;
  915. goto exit_read_chipid;
  916. }
  917. /* input device init */
  918. ret = bmg160_input_init(data);
  919. if (ret < 0)
  920. goto exit_input_init;
  921. sensors_register(data->factory_device, data, sensor_attrs, MODULE_NAME);
  922. /* gyro_timer settings. we poll for light values using a timer. */
  923. hrtimer_init(&data->gyro_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  924. data->poll_delay = ns_to_ktime(BMG160_DEFAULT_DELAY);
  925. data->gyro_timer.function = bmg160_timer_func;
  926. /* the timer just fires off a work queue request. we need a thread
  927. to read the i2c (can be slow and blocking). */
  928. data->gyro_wq = create_singlethread_workqueue("gyro_wq");
  929. if (!data->gyro_wq) {
  930. ret = -ENOMEM;
  931. pr_err("[SENSOR]: %s - could not create workqueue\n", __func__);
  932. goto exit_create_workqueue;
  933. }
  934. /* workqueue init */
  935. INIT_WORK(&data->work, bmg160_work_func);
  936. atomic_set(&data->enable, OFF);
  937. data->gyro_dps = BMG160_RANGE_500DPS;
  938. bmg160_set_range(data, data->gyro_dps);
  939. bmg160_set_bw(data, BMG160_BW_32Hz);
  940. bmg160_set_mode(data, BMG160_MODE_SUSPEND);
  941. pr_info("[SENSOR]: %s - Probe done!(chip pos : %d)\n",
  942. __func__, data->chip_pos);
  943. return 0;
  944. exit_create_workqueue:
  945. sensors_unregister(data->factory_device, sensor_attrs);
  946. sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
  947. sysfs_remove_group(&data->input->dev.kobj, &bmg160_attribute_group);
  948. input_unregister_device(data->input);
  949. exit_input_init:
  950. exit_read_chipid:
  951. kfree(data);
  952. exit_kzalloc:
  953. exit:
  954. pr_err("[SENSOR]: %s - Probe fail!\n", __func__);
  955. return ret;
  956. }
  957. static void bmg160_shutdown(struct i2c_client *client)
  958. {
  959. struct bmg160_p *data = (struct bmg160_p *)i2c_get_clientdata(client);
  960. pr_info("[SENSOR]: %s\n", __func__);
  961. if (atomic_read(&data->enable) == ON)
  962. bmg160_set_enable(data, OFF);
  963. atomic_set(&data->enable, OFF);
  964. bmg160_set_mode(data, BMG160_MODE_SUSPEND);
  965. }
  966. static int __devexit bmg160_remove(struct i2c_client *client)
  967. {
  968. struct bmg160_p *data = (struct bmg160_p *)i2c_get_clientdata(client);
  969. if (atomic_read(&data->enable) == ON)
  970. bmg160_set_enable(data, OFF);
  971. atomic_set(&data->enable, OFF);
  972. bmg160_set_mode(data, BMG160_MODE_SUSPEND);
  973. sensors_unregister(data->factory_device, sensor_attrs);
  974. sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
  975. sysfs_remove_group(&data->input->dev.kobj, &bmg160_attribute_group);
  976. input_unregister_device(data->input);
  977. kfree(data);
  978. return 0;
  979. }
  980. static int bmg160_suspend(struct device *dev)
  981. {
  982. struct bmg160_p *data = dev_get_drvdata(dev);
  983. if (atomic_read(&data->enable) == ON) {
  984. bmg160_set_mode(data, BMG160_MODE_SUSPEND);
  985. bmg160_set_enable(data, OFF);
  986. }
  987. return 0;
  988. }
  989. static int bmg160_resume(struct device *dev)
  990. {
  991. struct bmg160_p *data = dev_get_drvdata(dev);
  992. if (atomic_read(&data->enable) == ON) {
  993. bmg160_set_mode(data, BMG160_MODE_NORMAL);
  994. bmg160_set_enable(data, ON);
  995. }
  996. return 0;
  997. }
  998. static struct of_device_id bmg160_match_table[] = {
  999. { .compatible = "bmg160-i2c",},
  1000. {},
  1001. };
  1002. static const struct i2c_device_id bmg160_id[] = {
  1003. { "bmg160_match_table", 0 },
  1004. { }
  1005. };
  1006. static const struct dev_pm_ops bmg160_pm_ops = {
  1007. .suspend = bmg160_suspend,
  1008. .resume = bmg160_resume,
  1009. };
  1010. static struct i2c_driver bmg160_driver = {
  1011. .driver = {
  1012. .name = MODEL_NAME,
  1013. .owner = THIS_MODULE,
  1014. .of_match_table = bmg160_match_table,
  1015. .pm = &bmg160_pm_ops
  1016. },
  1017. .probe = bmg160_probe,
  1018. .shutdown = bmg160_shutdown,
  1019. .remove = __devexit_p(bmg160_remove),
  1020. .id_table = bmg160_id,
  1021. };
  1022. static int __init bmg160_init(void)
  1023. {
  1024. return i2c_add_driver(&bmg160_driver);
  1025. }
  1026. static void __exit bmg160_exit(void)
  1027. {
  1028. i2c_del_driver(&bmg160_driver);
  1029. }
  1030. module_init(bmg160_init);
  1031. module_exit(bmg160_exit);
  1032. MODULE_DESCRIPTION("bmg160 gyroscope sensor driver");
  1033. MODULE_AUTHOR("Samsung Electronics");
  1034. MODULE_LICENSE("GPL");