bma255.c 35 KB

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