bma280.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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 <linux/wakelock.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <mach/gpiomux.h>
  31. #include "sensors_core.h"
  32. #include "bma280_reg.h"
  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 VENDOR_NAME "BOSCH"
  37. #define MODEL_NAME "BMA280"
  38. #define MODULE_NAME "accelerometer_sensor"
  39. #define CALIBRATION_FILE_PATH "/efs/accel_calibration_data"
  40. #define CALIBRATION_DATA_AMOUNT 20
  41. #define MAX_ACCEL_1G 4096
  42. #define BMA280_DEFAULT_DELAY 200000000LL
  43. #define BMA280_CHIP_ID 0xFB
  44. #define CHIP_ID_RETRIES 3
  45. #define ACCEL_LOG_TIME 15 /* 15 sec */
  46. #define SLOPE_X_INT 0
  47. #define SLOPE_Y_INT 1
  48. #define SLOPE_Z_INT 2
  49. #define SLOPE_DURATION_VALUE 2
  50. #define SLOPE_THRESHOLD_VALUE 0x0A
  51. #define BMA280_TOP_UPPER_RIGHT 0
  52. #define BMA280_TOP_LOWER_RIGHT 1
  53. #define BMA280_TOP_LOWER_LEFT 2
  54. #define BMA280_TOP_UPPER_LEFT 3
  55. #define BMA280_BOTTOM_UPPER_RIGHT 4
  56. #define BMA280_BOTTOM_LOWER_RIGHT 5
  57. #define BMA280_BOTTOM_LOWER_LEFT 6
  58. #define BMA280_BOTTOM_UPPER_LEFT 7
  59. enum {
  60. OFF = 0,
  61. ON = 1
  62. };
  63. struct bma280_v {
  64. union {
  65. s16 v[3];
  66. struct {
  67. s16 x;
  68. s16 y;
  69. s16 z;
  70. };
  71. };
  72. };
  73. struct bma280_p {
  74. struct wake_lock reactive_wake_lock;
  75. struct i2c_client *client;
  76. struct input_dev *input;
  77. struct delayed_work irq_work;
  78. struct device *factory_device;
  79. struct bma280_v accdata;
  80. struct bma280_v caldata;
  81. struct mutex mode_mutex;
  82. struct hrtimer accel_timer;
  83. struct workqueue_struct *accel_wq;
  84. struct work_struct work;
  85. ktime_t poll_delay;
  86. atomic_t enable;
  87. u32 chip_pos;
  88. int recog_flag;
  89. int irq1;
  90. int irq_state;
  91. int acc_int1;
  92. int sda_gpio;
  93. int scl_gpio;
  94. int time_count;
  95. u64 old_timestamp;
  96. };
  97. static int bma280_open_calibration(struct bma280_p *);
  98. static int bma280_i2c_recovery(struct bma280_p *data)
  99. {
  100. int ret, i;
  101. struct gpiomux_setting old_config[2];
  102. struct gpiomux_setting recovery_config = {
  103. .func = GPIOMUX_FUNC_GPIO,
  104. .drv = GPIOMUX_DRV_8MA,
  105. .pull = GPIOMUX_PULL_NONE,
  106. };
  107. if ((data->sda_gpio < 0) || (data->scl_gpio < 0)) {
  108. pr_info("[SENSOR]: %s - no sda, scl gpio\n", __func__);
  109. return -1;
  110. }
  111. pr_info("[SENSOR] ################# %s #################\n", __func__);
  112. ret = msm_gpiomux_write(data->sda_gpio, GPIOMUX_ACTIVE,
  113. &recovery_config, &old_config[0]);
  114. if (ret < 0) {
  115. pr_err("[SENSOR]: %s sda_gpio have no active setting %d\n",
  116. __func__, ret);
  117. goto exit;
  118. }
  119. ret = msm_gpiomux_write(data->scl_gpio, GPIOMUX_ACTIVE,
  120. &recovery_config, &old_config[1]);
  121. if (ret < 0) {
  122. pr_err("[SENSOR]: %s scl_gpio have no active setting %d\n",
  123. __func__, ret);
  124. goto exit;
  125. }
  126. ret = gpio_request(data->sda_gpio, "SENSOR_SDA");
  127. if (ret < 0) {
  128. pr_err("[SENSOR]: %s - gpio %d request failed (%d)\n",
  129. __func__, data->sda_gpio, ret);
  130. goto exit;
  131. }
  132. ret = gpio_request(data->scl_gpio, "SENSOR_SCL");
  133. if (ret < 0) {
  134. pr_err("[SENSOR]: %s - gpio %d request failed (%d)\n",
  135. __func__, data->scl_gpio, ret);
  136. gpio_free(data->scl_gpio);
  137. goto exit;
  138. }
  139. ret = gpio_direction_output(data->sda_gpio, 1);
  140. if (ret < 0) {
  141. pr_err("[SENSOR]: %s - failed to set gpio %d as output (%d)\n",
  142. __func__, data->sda_gpio, ret);
  143. goto exit_to_free;
  144. }
  145. ret = gpio_direction_output(data->scl_gpio, 1);
  146. if (ret < 0) {
  147. pr_err("[SENSOR]: %s - failed to set gpio %d as output (%d)\n",
  148. __func__, data->scl_gpio, ret);
  149. goto exit_to_free;
  150. }
  151. for (i = 0; i < 5; i++) {
  152. udelay(100);
  153. gpio_set_value_cansleep(data->sda_gpio, 0);
  154. gpio_set_value_cansleep(data->scl_gpio, 0);
  155. udelay(100);
  156. gpio_set_value_cansleep(data->sda_gpio, 1);
  157. gpio_set_value_cansleep(data->scl_gpio, 1);
  158. }
  159. ret = gpio_direction_input(data->sda_gpio);
  160. if (ret < 0) {
  161. pr_err("[SENSOR]: %s - failed to set gpio %d as input (%d)\n",
  162. __func__, data->sda_gpio, ret);
  163. goto exit_to_free;
  164. }
  165. ret = gpio_direction_input(data->scl_gpio);
  166. if (ret < 0) {
  167. pr_err("[SENSOR]: %s - failed to set gpio %d as input (%d)\n",
  168. __func__, data->scl_gpio, ret);
  169. goto exit_to_free;
  170. }
  171. exit_to_free:
  172. gpio_free(data->sda_gpio);
  173. gpio_free(data->scl_gpio);
  174. exit:
  175. msm_gpiomux_write(data->sda_gpio, GPIOMUX_ACTIVE, &old_config[0], NULL);
  176. msm_gpiomux_write(data->scl_gpio, GPIOMUX_ACTIVE, &old_config[1], NULL);
  177. return ret;
  178. }
  179. static int bma280_i2c_read(struct bma280_p *data,
  180. unsigned char reg_addr, unsigned char *buf)
  181. {
  182. int ret, retries = 0;
  183. struct i2c_msg msg[2];
  184. msg[0].addr = data->client->addr;
  185. msg[0].flags = I2C_M_WR;
  186. msg[0].len = 1;
  187. msg[0].buf = &reg_addr;
  188. msg[1].addr = data->client->addr;
  189. msg[1].flags = I2C_M_RD;
  190. msg[1].len = 1;
  191. msg[1].buf = buf;
  192. do {
  193. ret = i2c_transfer(data->client->adapter, msg, 2);
  194. if (ret < 0)
  195. bma280_i2c_recovery(data);
  196. else
  197. break;
  198. } while (retries++ < 2);
  199. if (ret < 0) {
  200. pr_err("[SENSOR]: %s - i2c read error %d\n", __func__, ret);
  201. return ret;
  202. }
  203. return 0;
  204. }
  205. static int bma280_i2c_write(struct bma280_p *data,
  206. unsigned char reg_addr, unsigned char buf)
  207. {
  208. int ret, retries = 0;
  209. struct i2c_msg msg;
  210. unsigned char w_buf[2];
  211. w_buf[0] = reg_addr;
  212. w_buf[1] = buf;
  213. msg.addr = data->client->addr;
  214. msg.flags = I2C_M_WR;
  215. msg.len = 2;
  216. msg.buf = (char *)w_buf;
  217. do {
  218. ret = i2c_transfer(data->client->adapter, &msg, 1);
  219. if (ret < 0)
  220. bma280_i2c_recovery(data);
  221. else
  222. break;
  223. } while (retries++ < 2);
  224. if (ret < 0) {
  225. pr_err("[SENSOR]: %s - i2c write error %d\n", __func__, ret);
  226. return ret;
  227. }
  228. return 0;
  229. }
  230. static int bma280_set_mode(struct bma280_p *data, unsigned char mode)
  231. {
  232. int ret = 0;
  233. unsigned char buf1, buf2;
  234. mutex_lock(&data->mode_mutex);
  235. ret = bma280_i2c_read(data, BMA280_MODE_CTRL_REG, &buf1);
  236. ret += bma280_i2c_read(data, BMA280_LOW_NOISE_CTRL_REG, &buf2);
  237. switch (mode) {
  238. case BMA280_MODE_NORMAL:
  239. buf1 = BMA280_SET_BITSLICE(buf1, BMA280_MODE_CTRL, 0);
  240. buf2 = BMA280_SET_BITSLICE(buf2, BMA280_LOW_POWER_MODE, 0);
  241. ret += bma280_i2c_write(data, BMA280_MODE_CTRL_REG, buf1);
  242. mdelay(1);
  243. ret += bma280_i2c_write(data, BMA280_LOW_NOISE_CTRL_REG, buf2);
  244. break;
  245. case BMA280_MODE_LOWPOWER1:
  246. buf1 = BMA280_SET_BITSLICE(buf1, BMA280_MODE_CTRL, 2);
  247. buf2 = BMA280_SET_BITSLICE(buf2, BMA280_LOW_POWER_MODE, 0);
  248. ret += bma280_i2c_write(data, BMA280_MODE_CTRL_REG, buf1);
  249. mdelay(1);
  250. ret += bma280_i2c_write(data, BMA280_LOW_NOISE_CTRL_REG, buf2);
  251. break;
  252. case BMA280_MODE_SUSPEND:
  253. buf1 = BMA280_SET_BITSLICE(buf1, BMA280_MODE_CTRL, 4);
  254. buf2 = BMA280_SET_BITSLICE(buf2, BMA280_LOW_POWER_MODE, 0);
  255. ret += bma280_i2c_write(data, BMA280_LOW_NOISE_CTRL_REG, buf2);
  256. mdelay(1);
  257. ret += bma280_i2c_write(data, BMA280_MODE_CTRL_REG, buf1);
  258. break;
  259. case BMA280_MODE_DEEP_SUSPEND:
  260. buf1 = BMA280_SET_BITSLICE(buf1, BMA280_MODE_CTRL, 1);
  261. buf2 = BMA280_SET_BITSLICE(buf2, BMA280_LOW_POWER_MODE, 1);
  262. ret += bma280_i2c_write(data, BMA280_MODE_CTRL_REG, buf1);
  263. mdelay(1);
  264. ret += bma280_i2c_write(data, BMA280_LOW_NOISE_CTRL_REG, buf2);
  265. break;
  266. case BMA280_MODE_LOWPOWER2:
  267. buf1 = BMA280_SET_BITSLICE(buf1, BMA280_MODE_CTRL, 2);
  268. buf2 = BMA280_SET_BITSLICE(buf2, BMA280_LOW_POWER_MODE, 1);
  269. ret += bma280_i2c_write(data, BMA280_MODE_CTRL_REG, buf1);
  270. mdelay(1);
  271. ret += bma280_i2c_write(data, BMA280_LOW_NOISE_CTRL_REG, buf2);
  272. break;
  273. case BMA280_MODE_STANDBY:
  274. buf1 = BMA280_SET_BITSLICE(buf1, BMA280_MODE_CTRL, 4);
  275. buf2 = BMA280_SET_BITSLICE(buf2, BMA280_LOW_POWER_MODE, 1);
  276. ret += bma280_i2c_write(data, BMA280_LOW_NOISE_CTRL_REG, buf2);
  277. mdelay(1);
  278. ret += bma280_i2c_write(data, BMA280_MODE_CTRL_REG, buf1);
  279. break;
  280. default:
  281. ret = -EINVAL;
  282. break;
  283. }
  284. pr_info("[SENSOR]: %s - change mode %u\n", __func__, mode);
  285. mutex_unlock(&data->mode_mutex);
  286. return ret;
  287. }
  288. static int bma280_set_range(struct bma280_p *data, unsigned char range)
  289. {
  290. int ret = 0 ;
  291. unsigned char buf;
  292. ret = bma280_i2c_read(data, BMA280_RANGE_SEL_REG, &buf);
  293. switch (range) {
  294. case BMA280_RANGE_2G:
  295. buf = BMA280_SET_BITSLICE(buf, BMA280_RANGE_SEL, 3);
  296. break;
  297. case BMA280_RANGE_4G:
  298. buf = BMA280_SET_BITSLICE(buf, BMA280_RANGE_SEL, 5);
  299. break;
  300. case BMA280_RANGE_8G:
  301. buf = BMA280_SET_BITSLICE(buf, BMA280_RANGE_SEL, 8);
  302. break;
  303. case BMA280_RANGE_16G:
  304. buf = BMA280_SET_BITSLICE(buf, BMA280_RANGE_SEL, 12);
  305. break;
  306. default:
  307. buf = BMA280_SET_BITSLICE(buf, BMA280_RANGE_SEL, 3);
  308. break;
  309. }
  310. ret += bma280_i2c_write(data, BMA280_RANGE_SEL_REG, buf);
  311. return ret;
  312. }
  313. static int bma280_set_bandwidth(struct bma280_p *data,
  314. unsigned char bandwidth)
  315. {
  316. int ret = 0;
  317. unsigned char buf;
  318. if (bandwidth <= 7 || bandwidth >= 15)
  319. bandwidth = BMA280_BW_250HZ;
  320. ret = bma280_i2c_read(data, BMA280_BANDWIDTH__REG, &buf);
  321. buf = BMA280_SET_BITSLICE(buf, BMA280_BANDWIDTH, bandwidth);
  322. ret += bma280_i2c_write(data, BMA280_BANDWIDTH__REG, buf);
  323. pr_info("[SENSOR]: %s - change bandwidth %u\n", __func__, bandwidth);
  324. return ret;
  325. }
  326. static int bma280_read_accel_xyz(struct bma280_p *data, struct bma280_v *acc)
  327. {
  328. int ret = 0, i;
  329. unsigned char buf[READ_DATA_LENTH];
  330. for (i = 0; i < READ_DATA_LENTH; i++) {
  331. ret += bma280_i2c_read(data,
  332. BMA280_ACC_Y14_LSB__REG + i, &buf[i]);
  333. }
  334. if (ret < 0)
  335. goto exit;
  336. acc->y = BMA280_GET_BITSLICE(buf[0], BMA280_ACC_Y14_LSB) |
  337. (BMA280_GET_BITSLICE(buf[1],
  338. BMA280_ACC_Y_MSB) << (BMA280_ACC_Y14_LSB__LEN));
  339. acc->y = acc->y << (sizeof(short) * 8 - (BMA280_ACC_Y14_LSB__LEN +
  340. BMA280_ACC_Y_MSB__LEN));
  341. acc->y = acc->y >> (sizeof(short) * 8 - (BMA280_ACC_Y14_LSB__LEN +
  342. BMA280_ACC_Y_MSB__LEN));
  343. acc->x = BMA280_GET_BITSLICE(buf[2], BMA280_ACC_X14_LSB) |
  344. (BMA280_GET_BITSLICE(buf[3], BMA280_ACC_X_MSB) <<
  345. (BMA280_ACC_X14_LSB__LEN));
  346. acc->x = acc->x << (sizeof(short) * 8 - (BMA280_ACC_X14_LSB__LEN +
  347. BMA280_ACC_X_MSB__LEN));
  348. acc->x = acc->x >> (sizeof(short) * 8 - (BMA280_ACC_X14_LSB__LEN +
  349. BMA280_ACC_X_MSB__LEN));
  350. acc->z = BMA280_GET_BITSLICE(buf[4], BMA280_ACC_Z14_LSB) |
  351. (BMA280_GET_BITSLICE(buf[5],
  352. BMA280_ACC_Z_MSB) << (BMA280_ACC_Z14_LSB__LEN));
  353. acc->z = acc->z << (sizeof(short) * 8 - (BMA280_ACC_Z14_LSB__LEN +
  354. BMA280_ACC_Z_MSB__LEN));
  355. acc->z = acc->z >> (sizeof(short) * 8 - (BMA280_ACC_Z14_LSB__LEN +
  356. BMA280_ACC_Z_MSB__LEN));
  357. remap_sensor_data(acc->v, data->chip_pos);
  358. exit:
  359. return ret;
  360. }
  361. static enum hrtimer_restart bma280_timer_func(struct hrtimer *timer)
  362. {
  363. struct bma280_p *data = container_of(timer,
  364. struct bma280_p, accel_timer);
  365. if (!work_pending(&data->work))
  366. queue_work(data->accel_wq, &data->work);
  367. hrtimer_forward_now(&data->accel_timer, data->poll_delay);
  368. return HRTIMER_RESTART;
  369. }
  370. static void bma280_work_func(struct work_struct *work)
  371. {
  372. int ret;
  373. struct bma280_v acc;
  374. struct bma280_p *data = container_of(work, struct bma280_p, work);
  375. struct timespec ts;
  376. u64 timestamp_new;
  377. int time_hi, time_lo;
  378. //u64 diff = 0ULL;
  379. u64 delay = ktime_to_ns(data->poll_delay);
  380. get_monotonic_boottime(&ts);
  381. timestamp_new = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
  382. ret = bma280_read_accel_xyz(data, &acc);
  383. if (ret < 0)
  384. goto exit;
  385. data->accdata.x = acc.x - data->caldata.x;
  386. data->accdata.y = acc.y - data->caldata.y;
  387. data->accdata.z = acc.z - data->caldata.z;
  388. if (data->old_timestamp != 0 &&
  389. ((timestamp_new - data->old_timestamp) > ktime_to_ms(data->poll_delay) * 1800000LL)) {
  390. u64 shift_timestamp = delay >> 1;
  391. u64 timestamp = 0ULL;
  392. for (timestamp = data->old_timestamp + delay; timestamp < timestamp_new - shift_timestamp; timestamp+=delay) {
  393. time_hi = (int)((timestamp & TIME_HI_MASK) >> TIME_HI_SHIFT);
  394. time_lo = (int)(timestamp & TIME_LO_MASK);
  395. input_report_rel(data->input, REL_X, data->accdata.x);
  396. input_report_rel(data->input, REL_Y, data->accdata.y);
  397. input_report_rel(data->input, REL_Z, data->accdata.z);
  398. input_report_rel(data->input, REL_DIAL, time_hi);
  399. input_report_rel(data->input, REL_MISC, time_lo);
  400. input_sync(data->input);
  401. }
  402. }
  403. time_hi = (int)((timestamp_new & TIME_HI_MASK) >> TIME_HI_SHIFT);
  404. time_lo = (int)(timestamp_new & TIME_LO_MASK);
  405. input_report_rel(data->input, REL_X, data->accdata.x);
  406. input_report_rel(data->input, REL_Y, data->accdata.y);
  407. input_report_rel(data->input, REL_Z, data->accdata.z);
  408. input_report_rel(data->input, REL_DIAL, time_hi);
  409. input_report_rel(data->input, REL_MISC, time_lo);
  410. input_sync(data->input);
  411. data->old_timestamp = timestamp_new;
  412. exit:
  413. if ((ktime_to_ns(data->poll_delay) * (int64_t)data->time_count)
  414. >= ((int64_t)ACCEL_LOG_TIME * NSEC_PER_SEC)) {
  415. pr_info("[SENSOR]: %s - x = %d, y = %d, z = %d (ra:%d)\n",
  416. __func__, data->accdata.x, data->accdata.y,
  417. data->accdata.z, data->recog_flag);
  418. data->time_count = 0;
  419. } else
  420. data->time_count++;
  421. }
  422. static void bma280_set_enable(struct bma280_p *data, int enable)
  423. {
  424. if (enable == ON) {
  425. hrtimer_start(&data->accel_timer, data->poll_delay,
  426. HRTIMER_MODE_REL);
  427. } else {
  428. hrtimer_cancel(&data->accel_timer);
  429. cancel_work_sync(&data->work);
  430. }
  431. }
  432. static ssize_t bma280_enable_show(struct device *dev,
  433. struct device_attribute *attr, char *buf)
  434. {
  435. struct bma280_p *data = dev_get_drvdata(dev);
  436. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&data->enable));
  437. }
  438. static ssize_t bma280_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 bma280_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. bma280_open_calibration(data);
  455. bma280_set_mode(data, BMA280_MODE_NORMAL);
  456. atomic_set(&data->enable, ON);
  457. bma280_set_enable(data, ON);
  458. }
  459. } else {
  460. if (pre_enable == ON) {
  461. atomic_set(&data->enable, OFF);
  462. if (data->recog_flag == ON)
  463. bma280_set_mode(data, BMA280_MODE_LOWPOWER1);
  464. else
  465. bma280_set_mode(data, BMA280_MODE_SUSPEND);
  466. bma280_set_enable(data, OFF);
  467. }
  468. }
  469. return size;
  470. }
  471. static ssize_t bma280_delay_show(struct device *dev,
  472. struct device_attribute *attr, char *buf)
  473. {
  474. struct bma280_p *data = dev_get_drvdata(dev);
  475. return snprintf(buf, PAGE_SIZE, "%lld\n",
  476. ktime_to_ns(data->poll_delay));
  477. }
  478. static ssize_t bma280_delay_store(struct device *dev,
  479. struct device_attribute *attr, const char *buf, size_t size)
  480. {
  481. int ret;
  482. int64_t delay;
  483. struct bma280_p *data = dev_get_drvdata(dev);
  484. ret = kstrtoll(buf, 10, &delay);
  485. if (ret) {
  486. pr_err("[SENSOR]: %s - Invalid Argument\n", __func__);
  487. return ret;
  488. }
  489. if (delay <= 3000000LL)
  490. bma280_set_bandwidth(data, BMA280_BW_500HZ);
  491. else if (delay <= 5000000LL)
  492. bma280_set_bandwidth(data, BMA280_BW_125HZ);
  493. else if (delay <= 10000000LL)
  494. bma280_set_bandwidth(data, BMA280_BW_62_50HZ);
  495. else if (delay <= 20000000LL)
  496. bma280_set_bandwidth(data, BMA280_BW_31_25HZ);
  497. else
  498. bma280_set_bandwidth(data, BMA280_BW_7_81HZ);
  499. data->poll_delay = ns_to_ktime(delay);
  500. pr_info("[SENSOR]: %s - poll_delay = %lld\n", __func__, delay);
  501. if (atomic_read(&data->enable) == ON) {
  502. bma280_set_mode(data, BMA280_MODE_SUSPEND);
  503. bma280_set_enable(data, OFF);
  504. bma280_set_mode(data, BMA280_MODE_NORMAL);
  505. bma280_set_enable(data, ON);
  506. }
  507. return size;
  508. }
  509. static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
  510. bma280_delay_show, bma280_delay_store);
  511. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  512. bma280_enable_show, bma280_enable_store);
  513. static struct attribute *bma280_attributes[] = {
  514. &dev_attr_poll_delay.attr,
  515. &dev_attr_enable.attr,
  516. NULL
  517. };
  518. static struct attribute_group bma280_attribute_group = {
  519. .attrs = bma280_attributes
  520. };
  521. static ssize_t bma280_vendor_show(struct device *dev,
  522. struct device_attribute *attr, char *buf)
  523. {
  524. return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR_NAME);
  525. }
  526. static ssize_t bma280_name_show(struct device *dev,
  527. struct device_attribute *attr, char *buf)
  528. {
  529. return snprintf(buf, PAGE_SIZE, "%s\n", MODEL_NAME);
  530. }
  531. static int bma280_open_calibration(struct bma280_p *data)
  532. {
  533. int ret = 0;
  534. mm_segment_t old_fs;
  535. struct file *cal_filp = NULL;
  536. old_fs = get_fs();
  537. set_fs(KERNEL_DS);
  538. cal_filp = filp_open(CALIBRATION_FILE_PATH, O_RDONLY, 0666);
  539. if (IS_ERR(cal_filp)) {
  540. set_fs(old_fs);
  541. ret = PTR_ERR(cal_filp);
  542. data->caldata.x = 0;
  543. data->caldata.y = 0;
  544. data->caldata.z = 0;
  545. pr_info("[SENSOR]: %s - No Calibration\n", __func__);
  546. return ret;
  547. }
  548. ret = cal_filp->f_op->read(cal_filp, (char *)&data->caldata.v,
  549. 3 * sizeof(s16), &cal_filp->f_pos);
  550. if (ret != 3 * sizeof(s16)) {
  551. pr_err("[SENSOR] %s: - Can't read the cal data\n", __func__);
  552. ret = -EIO;
  553. }
  554. filp_close(cal_filp, current->files);
  555. set_fs(old_fs);
  556. pr_info("[SENSOR]: open accel calibration %d, %d, %d\n",
  557. data->caldata.x, data->caldata.y, data->caldata.z);
  558. if ((data->caldata.x == 0) && (data->caldata.y == 0)
  559. && (data->caldata.z == 0))
  560. return -EIO;
  561. return ret;
  562. }
  563. static int bma280_do_calibrate(struct bma280_p *data, int enable)
  564. {
  565. int sum[3] = { 0, };
  566. int ret = 0, cnt;
  567. struct file *cal_filp = NULL;
  568. struct bma280_v acc;
  569. mm_segment_t old_fs;
  570. if (enable) {
  571. data->caldata.x = 0;
  572. data->caldata.y = 0;
  573. data->caldata.z = 0;
  574. if (atomic_read(&data->enable) == ON)
  575. bma280_set_enable(data, OFF);
  576. else
  577. bma280_set_mode(data, BMA280_MODE_NORMAL);
  578. msleep(300);
  579. for (cnt = 0; cnt < CALIBRATION_DATA_AMOUNT; cnt++) {
  580. bma280_read_accel_xyz(data, &acc);
  581. sum[0] += acc.x;
  582. sum[1] += acc.y;
  583. sum[2] += acc.z;
  584. mdelay(10);
  585. }
  586. if (atomic_read(&data->enable) == ON)
  587. bma280_set_enable(data, ON);
  588. else
  589. bma280_set_mode(data, BMA280_MODE_SUSPEND);
  590. data->caldata.x = (sum[0] / CALIBRATION_DATA_AMOUNT);
  591. data->caldata.y = (sum[1] / CALIBRATION_DATA_AMOUNT);
  592. data->caldata.z = (sum[2] / CALIBRATION_DATA_AMOUNT);
  593. if (data->caldata.z > 0)
  594. data->caldata.z -= MAX_ACCEL_1G;
  595. else if (data->caldata.z < 0)
  596. data->caldata.z += MAX_ACCEL_1G;
  597. } else {
  598. data->caldata.x = 0;
  599. data->caldata.y = 0;
  600. data->caldata.z = 0;
  601. }
  602. pr_info("[SENSOR]: %s - do accel calibrate %d, %d, %d\n", __func__,
  603. data->caldata.x, data->caldata.y, data->caldata.z);
  604. old_fs = get_fs();
  605. set_fs(KERNEL_DS);
  606. cal_filp = filp_open(CALIBRATION_FILE_PATH,
  607. O_CREAT | O_TRUNC | O_WRONLY, 0666);
  608. if (IS_ERR(cal_filp)) {
  609. pr_err("[SENSOR]: %s - Can't open calibration file\n",
  610. __func__);
  611. set_fs(old_fs);
  612. ret = PTR_ERR(cal_filp);
  613. return ret;
  614. }
  615. ret = cal_filp->f_op->write(cal_filp, (char *)&data->caldata.v,
  616. 3 * sizeof(s16), &cal_filp->f_pos);
  617. if (ret != 3 * sizeof(s16)) {
  618. pr_err("[SENSOR]: %s - Can't write the caldata to file\n",
  619. __func__);
  620. ret = -EIO;
  621. }
  622. filp_close(cal_filp, current->files);
  623. set_fs(old_fs);
  624. return ret;
  625. }
  626. static ssize_t bma280_calibration_show(struct device *dev,
  627. struct device_attribute *attr, char *buf)
  628. {
  629. int ret;
  630. struct bma280_p *data = dev_get_drvdata(dev);
  631. ret = bma280_open_calibration(data);
  632. if (ret < 0)
  633. pr_err("[SENSOR]: %s - calibration open failed(%d)\n",
  634. __func__, ret);
  635. pr_info("[SENSOR]: %s - cal data %d %d %d - ret : %d\n", __func__,
  636. data->caldata.x, data->caldata.y, data->caldata.z, ret);
  637. return snprintf(buf, PAGE_SIZE, "%d %d %d %d\n", ret, data->caldata.x,
  638. data->caldata.y, data->caldata.z);
  639. }
  640. static ssize_t bma280_calibration_store(struct device *dev,
  641. struct device_attribute *attr, const char *buf, size_t size)
  642. {
  643. int ret;
  644. int64_t dEnable;
  645. struct bma280_p *data = dev_get_drvdata(dev);
  646. ret = kstrtoll(buf, 10, &dEnable);
  647. if (ret < 0)
  648. return ret;
  649. ret = bma280_do_calibrate(data, (int)dEnable);
  650. if (ret < 0)
  651. pr_err("[SENSOR]: %s - accel calibrate failed\n", __func__);
  652. return size;
  653. }
  654. static ssize_t bma280_raw_data_read(struct device *dev,
  655. struct device_attribute *attr, char *buf)
  656. {
  657. struct bma280_v acc;
  658. struct bma280_p *data = dev_get_drvdata(dev);
  659. if (atomic_read(&data->enable) == OFF) {
  660. bma280_set_mode(data, BMA280_MODE_NORMAL);
  661. msleep(20);
  662. bma280_read_accel_xyz(data, &acc);
  663. bma280_set_mode(data, BMA280_MODE_SUSPEND);
  664. acc.x = acc.x - data->caldata.x;
  665. acc.y = acc.y - data->caldata.y;
  666. acc.z = acc.z - data->caldata.z;
  667. } else {
  668. acc = data->accdata;
  669. }
  670. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
  671. acc.x, acc.y, acc.z);
  672. }
  673. static void bma280_set_int_enable(struct bma280_p *data,
  674. unsigned char InterruptType , unsigned char value)
  675. {
  676. unsigned char reg;
  677. bma280_i2c_read(data, BMA280_INT_ENABLE1_REG, &reg);
  678. switch (InterruptType) {
  679. case SLOPE_X_INT:
  680. /* Slope X Interrupt */
  681. reg = BMA280_SET_BITSLICE(reg, BMA280_EN_SLOPE_X_INT, value);
  682. break;
  683. case SLOPE_Y_INT:
  684. /* Slope Y Interrupt */
  685. reg = BMA280_SET_BITSLICE(reg, BMA280_EN_SLOPE_Y_INT, value);
  686. break;
  687. case SLOPE_Z_INT:
  688. /* Slope Z Interrupt */
  689. reg = BMA280_SET_BITSLICE(reg, BMA280_EN_SLOPE_Z_INT, value);
  690. break;
  691. default:
  692. break;
  693. }
  694. bma280_i2c_write(data, BMA280_INT_ENABLE1_REG, reg);
  695. }
  696. static void bma280_slope_enable(struct bma280_p *data,
  697. int enable, int factory_mode)
  698. {
  699. unsigned char reg;
  700. if (enable == ON) {
  701. bma280_i2c_read(data, BMA280_EN_INT1_PAD_SLOPE__REG, &reg);
  702. reg = BMA280_SET_BITSLICE(reg, BMA280_EN_INT1_PAD_SLOPE, ON);
  703. bma280_i2c_write(data, BMA280_EN_INT1_PAD_SLOPE__REG, reg);
  704. bma280_i2c_read(data, BMA280_INT_MODE_SEL__REG, &reg);
  705. reg = BMA280_SET_BITSLICE(reg, BMA280_INT_MODE_SEL, 0x01);
  706. bma280_i2c_write(data, BMA280_INT_MODE_SEL__REG, reg);
  707. if (factory_mode == OFF) {
  708. bma280_i2c_read(data, BMA280_SLOPE_DUR__REG, &reg);
  709. reg = BMA280_SET_BITSLICE(reg, BMA280_SLOPE_DUR,
  710. SLOPE_DURATION_VALUE);
  711. bma280_i2c_write(data, BMA280_SLOPE_DUR__REG, reg);
  712. reg = SLOPE_THRESHOLD_VALUE;
  713. bma280_i2c_write(data, BMA280_SLOPE_THRES__REG, reg);
  714. bma280_set_int_enable(data, SLOPE_X_INT, ON);
  715. bma280_set_int_enable(data, SLOPE_Y_INT, ON);
  716. bma280_set_int_enable(data, SLOPE_Z_INT, ON);
  717. } else {
  718. bma280_i2c_read(data, BMA280_SLOPE_DUR__REG, &reg);
  719. reg = BMA280_SET_BITSLICE(reg, BMA280_SLOPE_DUR, 0x01);
  720. bma280_i2c_write(data, BMA280_SLOPE_DUR__REG, reg);
  721. reg = 0x00;
  722. bma280_i2c_write(data, BMA280_SLOPE_THRES__REG, reg);
  723. bma280_set_int_enable(data, SLOPE_Z_INT, ON);
  724. bma280_set_bandwidth(data, BMA280_BW_250HZ);
  725. }
  726. } else if (enable == OFF) {
  727. bma280_i2c_read(data, BMA280_EN_INT1_PAD_SLOPE__REG, &reg);
  728. reg = BMA280_SET_BITSLICE(reg, BMA280_EN_INT1_PAD_SLOPE, OFF);
  729. bma280_i2c_write(data, BMA280_EN_INT1_PAD_SLOPE__REG, reg);
  730. bma280_set_int_enable(data, SLOPE_X_INT, OFF);
  731. bma280_set_int_enable(data, SLOPE_Y_INT, OFF);
  732. bma280_set_int_enable(data, SLOPE_Z_INT, OFF);
  733. }
  734. }
  735. static ssize_t bma280_reactive_alert_store(struct device *dev,
  736. struct device_attribute *attr, const char *buf, size_t size)
  737. {
  738. int enable = OFF, factory_mode = OFF;
  739. struct bma280_p *data = dev_get_drvdata(dev);
  740. if (sysfs_streq(buf, "0")) {
  741. enable = OFF;
  742. factory_mode = OFF;
  743. pr_info("[SENSOR]: %s - disable\n", __func__);
  744. } else if (sysfs_streq(buf, "1")) {
  745. enable = ON;
  746. factory_mode = OFF;
  747. pr_info("[SENSOR]: %s - enable\n", __func__);
  748. } else if (sysfs_streq(buf, "2")) {
  749. enable = ON;
  750. factory_mode = ON;
  751. pr_info("[SENSOR]: %s - factory mode\n", __func__);
  752. } else {
  753. pr_err("[SENSOR]: %s - invalid value %d\n", __func__, *buf);
  754. return -EINVAL;
  755. }
  756. if ((enable == ON) && (data->recog_flag == OFF)) {
  757. data->irq_state = 0;
  758. bma280_slope_enable(data, ON, factory_mode);
  759. enable_irq(data->irq1);
  760. enable_irq_wake(data->irq1);
  761. data->recog_flag = ON;
  762. if (atomic_read(&data->enable) == OFF)
  763. bma280_set_mode(data, BMA280_MODE_LOWPOWER1);
  764. pr_info("[SENSOR]: %s - reactive alert is on!\n", __func__);
  765. } else if ((enable == OFF) && (data->recog_flag == ON)) {
  766. bma280_slope_enable(data, OFF, factory_mode);
  767. disable_irq_wake(data->irq1);
  768. disable_irq_nosync(data->irq1);
  769. data->recog_flag = OFF;
  770. if (atomic_read(&data->enable) == OFF)
  771. bma280_set_mode(data, BMA280_MODE_SUSPEND);
  772. pr_info("[SENSOR]: %s - reactive alert is off! irq = %d\n",
  773. __func__, data->irq_state);
  774. }
  775. return size;
  776. }
  777. static ssize_t bma280_reactive_alert_show(struct device *dev,
  778. struct device_attribute *attr, char *buf)
  779. {
  780. struct bma280_p *data = dev_get_drvdata(dev);
  781. return snprintf(buf, PAGE_SIZE, "%d\n", data->irq_state);
  782. }
  783. static DEVICE_ATTR(name, S_IRUGO, bma280_name_show, NULL);
  784. static DEVICE_ATTR(vendor, S_IRUGO, bma280_vendor_show, NULL);
  785. static DEVICE_ATTR(calibration, S_IRUGO | S_IWUSR | S_IWGRP,
  786. bma280_calibration_show, bma280_calibration_store);
  787. static DEVICE_ATTR(raw_data, S_IRUGO, bma280_raw_data_read, NULL);
  788. static DEVICE_ATTR(reactive_alert, S_IRUGO | S_IWUSR | S_IWGRP,
  789. bma280_reactive_alert_show, bma280_reactive_alert_store);
  790. static struct device_attribute *sensor_attrs[] = {
  791. &dev_attr_name,
  792. &dev_attr_vendor,
  793. &dev_attr_calibration,
  794. &dev_attr_raw_data,
  795. &dev_attr_reactive_alert,
  796. NULL,
  797. };
  798. static void bma280_irq_work_func(struct work_struct *work)
  799. {
  800. struct bma280_p *data = container_of((struct delayed_work *)work,
  801. struct bma280_p, irq_work);
  802. bma280_set_int_enable(data, SLOPE_X_INT, OFF);
  803. bma280_set_int_enable(data, SLOPE_Y_INT, OFF);
  804. bma280_set_int_enable(data, SLOPE_Z_INT, OFF);
  805. }
  806. static irqreturn_t bma280_irq_thread(int irq, void *bma280_data_p)
  807. {
  808. struct bma280_p *data = bma280_data_p;
  809. data->irq_state = 1;
  810. wake_lock_timeout(&data->reactive_wake_lock,
  811. msecs_to_jiffies(2000));
  812. schedule_delayed_work(&data->irq_work, msecs_to_jiffies(100));
  813. pr_info("###### [SENSOR]: %s reactive irq ######\n", __func__);
  814. return IRQ_HANDLED;
  815. }
  816. static int bma280_setup_pin(struct bma280_p *data)
  817. {
  818. int ret;
  819. ret = gpio_request(data->acc_int1, "ACC_INT1");
  820. if (ret < 0) {
  821. pr_err("[SENSOR] %s - gpio %d request failed (%d)\n",
  822. __func__, data->acc_int1, ret);
  823. goto exit;
  824. }
  825. ret = gpio_direction_input(data->acc_int1);
  826. if (ret < 0) {
  827. pr_err("[SENSOR]: %s - failed to set gpio %d as input (%d)\n",
  828. __func__, data->acc_int1, ret);
  829. goto exit_acc_int1;
  830. }
  831. goto exit;
  832. exit_acc_int1:
  833. gpio_free(data->acc_int1);
  834. exit:
  835. return ret;
  836. }
  837. static int bma280_input_init(struct bma280_p *data)
  838. {
  839. int ret = 0;
  840. struct input_dev *dev;
  841. dev = input_allocate_device();
  842. if (!dev)
  843. return -ENOMEM;
  844. dev->name = MODULE_NAME;
  845. dev->id.bustype = BUS_I2C;
  846. input_set_capability(dev, EV_REL, REL_X);
  847. input_set_capability(dev, EV_REL, REL_Y);
  848. input_set_capability(dev, EV_REL, REL_Z);
  849. input_set_capability(dev, EV_REL, REL_DIAL);
  850. input_set_capability(dev, EV_REL, REL_MISC);
  851. input_set_drvdata(dev, data);
  852. ret = input_register_device(dev);
  853. if (ret < 0) {
  854. input_free_device(dev);
  855. return ret;
  856. }
  857. ret = sensors_create_symlink(&dev->dev.kobj, dev->name);
  858. if (ret < 0) {
  859. input_unregister_device(dev);
  860. return ret;
  861. }
  862. /* sysfs node creation */
  863. ret = sysfs_create_group(&dev->dev.kobj, &bma280_attribute_group);
  864. if (ret < 0) {
  865. sensors_remove_symlink(&data->input->dev.kobj,
  866. data->input->name);
  867. input_unregister_device(dev);
  868. return ret;
  869. }
  870. data->input = dev;
  871. return 0;
  872. }
  873. static int bma280_parse_dt(struct bma280_p *data, struct device *dev)
  874. {
  875. struct device_node *dNode = dev->of_node;
  876. enum of_gpio_flags flags;
  877. if (dNode == NULL)
  878. return -ENODEV;
  879. data->acc_int1 = of_get_named_gpio_flags(dNode,
  880. "bma280-i2c,acc_int1-gpio", 0, &flags);
  881. if (data->acc_int1 < 0) {
  882. pr_err("[SENSOR]: %s - get acc_int1 error\n", __func__);
  883. return -ENODEV;
  884. }
  885. data->sda_gpio = of_get_named_gpio_flags(dNode,
  886. "bma280-i2c,sda", 0, &flags);
  887. if (data->sda_gpio < 0)
  888. pr_info("[SENSOR]: %s - no sda_gpio\n", __func__);
  889. data->scl_gpio = of_get_named_gpio_flags(dNode,
  890. "bma280-i2c,scl", 0, &flags);
  891. if (data->scl_gpio < 0)
  892. pr_info("[SENSOR]: %s - no scl_gpio\n", __func__);
  893. if (of_property_read_u32(dNode,
  894. "bma280-i2c,chip_pos", &data->chip_pos) < 0)
  895. data->chip_pos = BMA280_TOP_LOWER_RIGHT;
  896. return 0;
  897. }
  898. static int sensor_regulator_onoff(struct device *dev, bool onoff)
  899. {
  900. struct regulator *sensor_vcc, *sensor_lvs1;
  901. sensor_vcc = devm_regulator_get(dev, "bma280-i2c,vdd");
  902. if (IS_ERR(sensor_vcc)) {
  903. pr_err("[SENSOR]: %s - cannot get sensor_vcc\n", __func__);
  904. return -ENOMEM;
  905. }
  906. sensor_lvs1 = devm_regulator_get(dev, "bma280-i2c,vdd-io");
  907. if (IS_ERR(sensor_lvs1)) {
  908. pr_err("[SENSOR]: %s - cannot get sensor_lvs1\n", __func__);
  909. devm_regulator_put(sensor_vcc);
  910. return -ENOMEM;
  911. }
  912. if (onoff) {
  913. regulator_enable(sensor_vcc);
  914. regulator_enable(sensor_lvs1);
  915. } else {
  916. regulator_disable(sensor_vcc);
  917. regulator_disable(sensor_lvs1);
  918. }
  919. devm_regulator_put(sensor_vcc);
  920. devm_regulator_put(sensor_lvs1);
  921. mdelay(5);
  922. return 0;
  923. }
  924. static int bma280_probe(struct i2c_client *client,
  925. const struct i2c_device_id *id)
  926. {
  927. int ret = -ENODEV, i;
  928. struct bma280_p *data = NULL;
  929. pr_info("##########################################################\n");
  930. pr_info("[SENSOR]: %s - Probe Start!\n", __func__);
  931. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  932. pr_err("[SENSOR]: %s - i2c_check_functionality error\n",
  933. __func__);
  934. goto exit;
  935. }
  936. ret = sensor_regulator_onoff(&client->dev, true);
  937. if (ret < 0)
  938. pr_err("[SENSOR]: %s - No regulator\n", __func__);
  939. data = kzalloc(sizeof(struct bma280_p), GFP_KERNEL);
  940. if (data == NULL) {
  941. pr_err("[SENSOR]: %s - kzalloc error\n", __func__);
  942. ret = -ENOMEM;
  943. goto exit_kzalloc;
  944. }
  945. ret = bma280_parse_dt(data, &client->dev);
  946. if (ret < 0) {
  947. pr_err("[SENSOR]: %s - of_node error\n", __func__);
  948. ret = -ENODEV;
  949. goto exit_of_node;
  950. }
  951. ret = bma280_setup_pin(data);
  952. if (ret < 0) {
  953. pr_err("[SENSOR]: %s - could not setup pin\n", __func__);
  954. goto exit_setup_pin;
  955. }
  956. i2c_set_clientdata(client, data);
  957. data->client = client;
  958. mutex_init(&data->mode_mutex);
  959. wake_lock_init(&data->reactive_wake_lock, WAKE_LOCK_SUSPEND,
  960. "reactive_wake_lock");
  961. /* read chip id */
  962. for (i = 0; i < CHIP_ID_RETRIES; i++) {
  963. ret = i2c_smbus_read_word_data(client, BMA280_CHIP_ID_REG);
  964. if ((ret & 0x00ff) != BMA280_CHIP_ID) {
  965. pr_err("[SENSOR]: %s - chip id failed 0x%x\n",
  966. __func__, (unsigned int)ret & 0x00ff);
  967. } else {
  968. pr_info("[SENSOR]: %s - chip id success 0x%x\n",
  969. __func__, (unsigned int)ret & 0x00ff);
  970. break;
  971. }
  972. msleep(20);
  973. }
  974. if (i >= CHIP_ID_RETRIES) {
  975. ret = -ENODEV;
  976. goto exit_read_chipid;
  977. }
  978. /* input device init */
  979. ret = bma280_input_init(data);
  980. if (ret < 0)
  981. goto exit_input_init;
  982. sensors_register(data->factory_device, data, sensor_attrs, MODULE_NAME);
  983. /* accel_timer settings. we poll for light values using a timer. */
  984. hrtimer_init(&data->accel_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  985. data->poll_delay = ns_to_ktime(BMA280_DEFAULT_DELAY);
  986. data->accel_timer.function = bma280_timer_func;
  987. /* the timer just fires off a work queue request. we need a thread
  988. to read the i2c (can be slow and blocking). */
  989. data->accel_wq = create_singlethread_workqueue("accel_wq");
  990. if (!data->accel_wq) {
  991. ret = -ENOMEM;
  992. pr_err("[SENSOR]: %s - could not create workqueue\n", __func__);
  993. goto exit_create_workqueue;
  994. }
  995. /* this is the thread function we run on the work queue */
  996. INIT_WORK(&data->work, bma280_work_func);
  997. INIT_DELAYED_WORK(&data->irq_work, bma280_irq_work_func);
  998. data->irq1 = gpio_to_irq(data->acc_int1);
  999. ret = request_threaded_irq(data->irq1, NULL, bma280_irq_thread,
  1000. IRQF_TRIGGER_RISING | IRQF_ONESHOT, "bma280_accel", data);
  1001. if (ret < 0) {
  1002. pr_err("[SENSOR]: %s - can't allocate irq.\n", __func__);
  1003. goto exit_request_threaded_irq;
  1004. }
  1005. disable_irq(data->irq1);
  1006. atomic_set(&data->enable, OFF);
  1007. data->time_count = 0;
  1008. data->irq_state = 0;
  1009. data->recog_flag = OFF;
  1010. bma280_set_bandwidth(data, BMA280_BW_125HZ);
  1011. bma280_set_range(data, BMA280_RANGE_2G);
  1012. bma280_set_mode(data, BMA280_MODE_SUSPEND);
  1013. pr_info("[SENSOR]: %s - Probe done!(chip pos : %d)\n",
  1014. __func__, data->chip_pos);
  1015. return 0;
  1016. exit_request_threaded_irq:
  1017. exit_create_workqueue:
  1018. sensors_unregister(data->factory_device, sensor_attrs);
  1019. sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
  1020. sysfs_remove_group(&data->input->dev.kobj, &bma280_attribute_group);
  1021. input_unregister_device(data->input);
  1022. exit_input_init:
  1023. exit_read_chipid:
  1024. mutex_destroy(&data->mode_mutex);
  1025. wake_lock_destroy(&data->reactive_wake_lock);
  1026. gpio_free(data->acc_int1);
  1027. exit_setup_pin:
  1028. exit_of_node:
  1029. kfree(data);
  1030. exit_kzalloc:
  1031. exit:
  1032. pr_err("[SENSOR]: %s - Probe fail!\n", __func__);
  1033. return ret;
  1034. }
  1035. static void bma280_shutdown(struct i2c_client *client)
  1036. {
  1037. struct bma280_p *data = (struct bma280_p *)i2c_get_clientdata(client);
  1038. pr_info("[SENSOR]: %s\n", __func__);
  1039. if (atomic_read(&data->enable) == ON)
  1040. bma280_set_enable(data, OFF);
  1041. atomic_set(&data->enable, OFF);
  1042. bma280_set_mode(data, BMA280_MODE_SUSPEND);
  1043. }
  1044. static int __devexit bma280_remove(struct i2c_client *client)
  1045. {
  1046. struct bma280_p *data = (struct bma280_p *)i2c_get_clientdata(client);
  1047. if (atomic_read(&data->enable) == ON)
  1048. bma280_set_enable(data, OFF);
  1049. atomic_set(&data->enable, OFF);
  1050. cancel_delayed_work_sync(&data->irq_work);
  1051. bma280_set_mode(data, BMA280_MODE_SUSPEND);
  1052. sensors_unregister(data->factory_device, sensor_attrs);
  1053. sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
  1054. sysfs_remove_group(&data->input->dev.kobj, &bma280_attribute_group);
  1055. input_unregister_device(data->input);
  1056. free_irq(data->irq1, data);
  1057. wake_lock_destroy(&data->reactive_wake_lock);
  1058. mutex_destroy(&data->mode_mutex);
  1059. gpio_free(data->acc_int1);
  1060. kfree(data);
  1061. return 0;
  1062. }
  1063. static int bma280_suspend(struct device *dev)
  1064. {
  1065. struct bma280_p *data = dev_get_drvdata(dev);
  1066. pr_info("[SENSOR]: %s\n", __func__);
  1067. if (atomic_read(&data->enable) == ON) {
  1068. if (data->recog_flag == ON)
  1069. bma280_set_mode(data, BMA280_MODE_LOWPOWER1);
  1070. else
  1071. bma280_set_mode(data, BMA280_MODE_SUSPEND);
  1072. bma280_set_enable(data, OFF);
  1073. }
  1074. return 0;
  1075. }
  1076. static int bma280_resume(struct device *dev)
  1077. {
  1078. struct bma280_p *data = dev_get_drvdata(dev);
  1079. pr_info("[SENSOR]: %s\n", __func__);
  1080. if (atomic_read(&data->enable) == ON) {
  1081. bma280_set_mode(data, BMA280_MODE_NORMAL);
  1082. bma280_set_enable(data, ON);
  1083. }
  1084. return 0;
  1085. }
  1086. static struct of_device_id bma280_match_table[] = {
  1087. { .compatible = "bma280-i2c",},
  1088. {},
  1089. };
  1090. static const struct i2c_device_id bma280_id[] = {
  1091. { "bma280_match_table", 0 },
  1092. { }
  1093. };
  1094. static const struct dev_pm_ops bma280_pm_ops = {
  1095. .suspend = bma280_suspend,
  1096. .resume = bma280_resume
  1097. };
  1098. static struct i2c_driver bma280_driver = {
  1099. .driver = {
  1100. .name = MODEL_NAME,
  1101. .owner = THIS_MODULE,
  1102. .of_match_table = bma280_match_table,
  1103. .pm = &bma280_pm_ops
  1104. },
  1105. .probe = bma280_probe,
  1106. .shutdown = bma280_shutdown,
  1107. .remove = __devexit_p(bma280_remove),
  1108. .id_table = bma280_id,
  1109. };
  1110. static int __init bma280_init(void)
  1111. {
  1112. return i2c_add_driver(&bma280_driver);
  1113. }
  1114. static void __exit bma280_exit(void)
  1115. {
  1116. i2c_del_driver(&bma280_driver);
  1117. }
  1118. module_init(bma280_init);
  1119. module_exit(bma280_exit);
  1120. MODULE_DESCRIPTION("bma280 accelerometer sensor driver");
  1121. MODULE_AUTHOR("Samsung Electronics");
  1122. MODULE_LICENSE("GPL");