bma254.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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/i2c.h>
  19. #include <linux/fs.h>
  20. #include <linux/errno.h>
  21. #include <linux/device.h>
  22. #include <linux/delay.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/input.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/module.h>
  29. #include <linux/bma254.h>
  30. #include <linux/regulator/consumer.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/sensors_core.h>
  34. #ifdef CONFIG_BMA254_SMART_ALERT
  35. #include <linux/wakelock.h>
  36. #include <linux/gpio.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/mutex.h>
  39. #endif
  40. #define CHIP_DEV_NAME "BMA254"
  41. #define CHIP_DEV_VENDOR "BOSCH"
  42. #define CAL_PATH "/efs/calibration_data"
  43. #define CALIBRATION_DATA_AMOUNT 20
  44. #define MAX_ACCEL_1G 1024
  45. #ifdef CONFIG_BMA254_SMART_ALERT
  46. extern unsigned int system_rev;
  47. struct bma254_platform_data {
  48. int p_out; /* acc-sensor-irq gpio */
  49. };
  50. #endif
  51. struct bma254_data {
  52. struct i2c_client *client;
  53. struct input_dev *input;
  54. struct device *dev;
  55. struct delayed_work work;
  56. #ifdef CONFIG_BMA254_SMART_ALERT
  57. struct bma254_platform_data *pdata;
  58. struct mutex data_mutex;
  59. struct work_struct alert_work;
  60. struct wake_lock reactive_wake_lock;
  61. atomic_t reactive_state;
  62. atomic_t reactive_enable;
  63. bool factory_mode;
  64. int accsns_activate_flag;
  65. int pin_check_fail;
  66. int IRQ;
  67. #endif
  68. int cal_data[3];
  69. int position;
  70. int delay;
  71. int enable;
  72. };
  73. static const int position_map[][3][3] = {
  74. { {-1, 0, 0}, { 0, -1, 0}, { 0, 0, 1} },
  75. { { 0, -1, 0}, { 1, 0, 0}, { 0, 0, 1} },
  76. { { 1, 0, 0}, { 0, 1, 0}, { 0, 0, 1} },
  77. { { 0, 1, 0}, {-1, 0, 0}, { 0, 0, 1} },
  78. { { 1, 0, 0}, { 0, -1, 0}, { 0, 0, -1} },
  79. { { 0, 1, 0}, { 1, 0, 0}, { 0, 0, -1} },
  80. { {-1, 0, 0}, { 0, 1, 0}, { 0, 0, -1} },
  81. { { 0, -1, 0}, {-1, 0, 0}, { 0, 0, -1} },
  82. };
  83. static int bma254_i2c_read(struct bma254_data *bma254, u8 reg, unsigned char *rbuf, int len)
  84. {
  85. int ret = -1;
  86. struct i2c_msg msg[2];
  87. struct i2c_client *client = bma254->client;
  88. if (unlikely((client == NULL) || (!client->adapter)))
  89. return -ENODEV;
  90. msg[0].addr = client->addr;
  91. msg[0].flags = 0;
  92. msg[0].len = 1;
  93. msg[0].buf = &reg;
  94. msg[1].addr = client->addr;
  95. msg[1].flags = 1;
  96. msg[1].len = len;
  97. msg[1].buf = rbuf;
  98. ret = i2c_transfer(client->adapter, &msg[0], 2);
  99. if (unlikely(ret < 0))
  100. pr_err("%s,i2c transfer error ret=%d\n", __func__, ret);
  101. return ret;
  102. }
  103. static int bma254_i2c_write(struct bma254_data *bma254, u8 reg, u8 val)
  104. {
  105. int err = 0;
  106. struct i2c_msg msg[1];
  107. unsigned char data[2];
  108. int retry = 2;
  109. struct i2c_client *client = bma254->client;
  110. if (unlikely((client == NULL) || (!client->adapter)))
  111. return -ENODEV;
  112. do {
  113. data[0] = reg;
  114. data[1] = val;
  115. msg->addr = client->addr;
  116. msg->flags = 0;
  117. msg->len = 2;
  118. msg->buf = data;
  119. err = i2c_transfer(client->adapter, msg, 1);
  120. if (err >= 0)
  121. return 0;
  122. } while (--retry > 0);
  123. pr_err("%s,i2c transfer error(%d)\n", __func__, err);
  124. return err;
  125. }
  126. static void bma254_activate(struct bma254_data *bma254, bool enable)
  127. {
  128. pr_err("%s,acitve(%s)\n", __func__, enable ? "true" : "false");
  129. if (enable == true) {
  130. #ifdef CONFIG_BMA254_SMART_ALERT
  131. if (!bma254->accsns_activate_flag)
  132. {
  133. bma254_i2c_write(bma254, BMA254_REG14, SOFT_RESEET);
  134. msleep(5);
  135. }
  136. #else
  137. bma254_i2c_write(bma254, BMA254_REG14, SOFT_RESEET);
  138. msleep(5);
  139. #endif
  140. bma254_i2c_write(bma254, BMA254_REG0F, BMA2X2_RANGE_SET);
  141. bma254_i2c_write(bma254, BMA254_REG10, BANDWIDTH_31_25);
  142. bma254_i2c_write(bma254, BMA254_REG11, BMA254_MODE_NORMAL);
  143. }
  144. #ifndef CONFIG_BMA254_SMART_ALERT
  145. else {
  146. bma254_i2c_write(bma254, BMA254_REG11, BMA254_MODE_SUSPEND);
  147. }
  148. #else
  149. if(bma254->accsns_activate_flag && enable != true){
  150. pr_info("%s: low power mode\n", __func__);
  151. bma254_i2c_write(bma254, BMA254_REG0F, BMA2X2_RANGE_SET);
  152. bma254_i2c_write(bma254, BMA254_REG10, BANDWIDTH_07_81);
  153. bma254_i2c_write(bma254, BMA254_REG11, BMA254_MODE_LOWPOWER1);
  154. }else if(enable != true){
  155. bma254_i2c_write(bma254, BMA254_REG11, BMA254_MODE_SUSPEND);
  156. }
  157. #endif
  158. }
  159. #ifdef CONFIG_BMA254_SMART_ALERT
  160. static int bma254_get_motion_interrupt(struct bma254_data *bma254)
  161. {
  162. int result = 0;
  163. unsigned char data;
  164. u8 buf[2];
  165. data = bma254_i2c_read(bma254, BMA254_SLOPE_INT_S__REG, buf, 2);
  166. pr_info("%s: call : return %d\n", __func__, data);
  167. result = 1;
  168. return result;
  169. }
  170. static void bma254_set_motion_interrupt(struct bma254_data *bma254, bool enable,
  171. bool factorytest)
  172. {
  173. if (enable) {
  174. bma254_activate(bma254, false);
  175. usleep_range(5000, 6000);
  176. pr_info("%s : enable\n", __func__);
  177. bma254_i2c_write(bma254, BMA254_EN_INT1_PAD_SLOPE__REG, 0x04);
  178. if (factorytest) {
  179. bma254_i2c_write(bma254, BMA254_SLOPE_DUR__REG, 0x00);
  180. bma254_i2c_write(bma254, BMA254_SLOPE_THRES__REG, 0x00);
  181. } else {
  182. bma254_i2c_write(bma254, BMA254_SLOPE_DUR__REG, 0x02);
  183. bma254_i2c_write(bma254, BMA254_SLOPE_THRES__REG, 0x10);
  184. }
  185. bma254_i2c_write(bma254, BMA254_INT_ENABLE1_REG, 0x07);
  186. } else {
  187. pr_info("%s : disable\n", __func__);
  188. bma254_i2c_write(bma254, BMA254_EN_INT1_PAD_SLOPE__REG, 0x00);
  189. bma254_i2c_write(bma254, BMA254_SLOPE_DUR__REG, 0x03);
  190. bma254_i2c_write(bma254, BMA254_INT_ENABLE1_REG, 0x00);
  191. bma254_i2c_write(bma254, BMA254_SLOPE_THRES__REG, 0xff);
  192. usleep_range(5000, 6000);
  193. }
  194. }
  195. #endif
  196. static int bma254_get_data(struct bma254_data *bma254, int *xyz, bool cal)
  197. {
  198. int ret;
  199. int i, j;
  200. int data[3] = {0,};
  201. u8 sx[6] = {0,};
  202. ret = bma254_i2c_read(bma254, BMA254_XOUT, sx, 6);
  203. if (ret < 0)
  204. return ret;
  205. for (i = 0; i < 3; i++) {
  206. data[i] = ((int)((s8)sx[2 * i + 1] << 8 |
  207. ((s8)sx[2 * i] & 0xfe))) >> 4;
  208. }
  209. for (i = 0; i < 3; i++) {
  210. xyz[i] = 0;
  211. for (j = 0; j < 3; j++)
  212. xyz[i] += data[j] * position_map[bma254->position][i][j];
  213. if (cal)
  214. xyz[i] = (xyz[i] - bma254->cal_data[i]);
  215. }
  216. return ret;
  217. }
  218. static void bma254_work_func(struct work_struct *work)
  219. {
  220. struct bma254_data *bma254 = container_of((struct delayed_work *)work,
  221. struct bma254_data, work);
  222. int xyz[3] = {0,};
  223. int ret;
  224. if (bma254 == NULL) {
  225. pr_err("%s, NULL point\n", __func__);
  226. return;
  227. }
  228. ret = bma254_get_data(bma254, xyz, true);
  229. if (ret < 0) {
  230. pr_err("%s, data error(%d)\n", __func__, ret);
  231. } else {
  232. #ifdef REPORT_ABS
  233. input_report_abs(bma254->input, ABS_X, xyz[0]);
  234. input_report_abs(bma254->input, ABS_Y, xyz[1]);
  235. input_report_abs(bma254->input, ABS_Z, xyz[2]);
  236. #else
  237. input_report_rel(bma254->input, REL_X, xyz[0] < 0 ? xyz[0] : xyz[0] + 1);
  238. input_report_rel(bma254->input, REL_Y, xyz[1] < 0 ? xyz[1] : xyz[1] + 1);
  239. input_report_rel(bma254->input, REL_Z, xyz[2] < 0 ? xyz[2] : xyz[2] + 1);
  240. #endif
  241. input_sync(bma254->input);
  242. }
  243. if (bma254->enable)
  244. schedule_delayed_work(&bma254->work, msecs_to_jiffies(bma254->delay));
  245. }
  246. static int bma254_open_calibration(struct bma254_data *bma254)
  247. {
  248. struct file *cal_filp = NULL;
  249. int err = 0;
  250. mm_segment_t old_fs;
  251. old_fs = get_fs();
  252. set_fs(KERNEL_DS);
  253. cal_filp = filp_open(CAL_PATH, O_RDONLY, 0666);
  254. if (IS_ERR(cal_filp)) {
  255. pr_err("%s: Can't open calibration file\n", __func__);
  256. set_fs(old_fs);
  257. err = PTR_ERR(cal_filp);
  258. return err;
  259. }
  260. err = cal_filp->f_op->read(cal_filp,
  261. (char *)&bma254->cal_data, 3 * sizeof(s32), &cal_filp->f_pos);
  262. if (err != 3 * sizeof(s32)) {
  263. pr_err("%s: Can't read the cal data from file\n", __func__);
  264. err = -EIO;
  265. }
  266. pr_info("%s: (%d,%d,%d)\n", __func__,
  267. bma254->cal_data[0], bma254->cal_data[1], bma254->cal_data[2]);
  268. filp_close(cal_filp, current->files);
  269. set_fs(old_fs);
  270. return err;
  271. }
  272. static ssize_t bma254_enable_store(struct device *dev,
  273. struct device_attribute *attr,
  274. const char *buf, size_t size)
  275. {
  276. struct bma254_data *bma254 = dev_get_drvdata(dev);
  277. int value = 0;
  278. int err = 0;
  279. err = kstrtoint(buf, 10, &value);
  280. if (err) {
  281. pr_err("%s, kstrtoint failed.", __func__);
  282. goto done;
  283. }
  284. if (value != 0 && value != 1) {
  285. pr_err("%s,wrong value(%d)\n", __func__, value);
  286. goto done;
  287. }
  288. if (bma254->enable != value) {
  289. pr_info("%s, enable(%d)\n", __func__, value);
  290. if (value) {
  291. bma254_activate(bma254, true);
  292. bma254_open_calibration(bma254);
  293. schedule_delayed_work(&bma254->work, msecs_to_jiffies(bma254->delay));
  294. } else {
  295. cancel_delayed_work_sync(&bma254->work);
  296. bma254_activate(bma254, false);
  297. }
  298. bma254->enable = value;
  299. } else {
  300. pr_err("%s, wrong cmd for enable\n", __func__);
  301. }
  302. done:
  303. return size;
  304. }
  305. static ssize_t bma254_enable_show(struct device *dev,
  306. struct device_attribute *attr,
  307. char *buf)
  308. {
  309. struct bma254_data *data = dev_get_drvdata(dev);
  310. return snprintf(buf, PAGE_SIZE, "%d\n", data->enable);
  311. }
  312. static ssize_t bma254_delay_store(struct device *dev,
  313. struct device_attribute *attr,
  314. const char *buf, size_t size)
  315. {
  316. struct bma254_data *bma254 = dev_get_drvdata(dev);
  317. int value = 0;
  318. int err = 0;
  319. err = kstrtoint(buf, 10, &value);
  320. if (err) {
  321. pr_err("%s, kstrtoint failed\n", __func__);
  322. goto done;
  323. }
  324. if (value < 0 || 200 < value) {
  325. pr_err("%s,wrong value(%d)\n", __func__, value);
  326. goto done;
  327. }
  328. if (bma254->delay != value) {
  329. if (bma254->enable)
  330. cancel_delayed_work_sync(&bma254->work);
  331. bma254->delay = value;
  332. if (bma254->enable)
  333. schedule_delayed_work(&bma254->work, msecs_to_jiffies(bma254->delay));
  334. } else {
  335. pr_err("%s, same delay\n", __func__);
  336. }
  337. pr_info("%s,delay %d\n", __func__, bma254->delay);
  338. done:
  339. return size;
  340. }
  341. static ssize_t bma254_delay_show(struct device *dev,
  342. struct device_attribute *attr,
  343. char *buf)
  344. {
  345. struct bma254_data *bma254 = dev_get_drvdata(dev);
  346. return snprintf(buf, PAGE_SIZE, "%d\n", bma254->delay);
  347. }
  348. static DEVICE_ATTR(enable,
  349. S_IRUGO|S_IWUSR|S_IWGRP,
  350. bma254_enable_show,
  351. bma254_enable_store
  352. );
  353. static DEVICE_ATTR(delay,
  354. S_IRUGO|S_IWUSR|S_IWGRP,
  355. bma254_delay_show,
  356. bma254_delay_store
  357. );
  358. static struct attribute *bma254_attributes[] = {
  359. &dev_attr_enable.attr,
  360. &dev_attr_delay.attr,
  361. NULL
  362. };
  363. static struct attribute_group bma254_attribute_group = {
  364. .attrs = bma254_attributes
  365. };
  366. static int bma254_do_calibrate(struct bma254_data *bma254, int enable)
  367. {
  368. struct file *cal_filp;
  369. int sum[3] = { 0, };
  370. int err;
  371. mm_segment_t old_fs;
  372. if (enable) {
  373. int data[3] = { 0, };
  374. int i;
  375. for (i = 0; i < CALIBRATION_DATA_AMOUNT; i++) {
  376. err = bma254_get_data(bma254, data, false);
  377. if (err < 0) {
  378. pr_err("%s : failed in the %dth loop\n", __func__, i);
  379. return err;
  380. }
  381. sum[0] += data[0];
  382. sum[1] += data[1];
  383. sum[2] += data[2];
  384. }
  385. bma254->cal_data[0] = (sum[0] / CALIBRATION_DATA_AMOUNT);
  386. bma254->cal_data[1] = (sum[1] / CALIBRATION_DATA_AMOUNT);
  387. bma254->cal_data[2] = (sum[2] / CALIBRATION_DATA_AMOUNT);
  388. if(bma254->cal_data[2] > 0)
  389. bma254->cal_data[2] -= MAX_ACCEL_1G;
  390. else if(bma254->cal_data[2] < 0)
  391. bma254->cal_data[2] += MAX_ACCEL_1G;
  392. } else {
  393. bma254->cal_data[0] = 0;
  394. bma254->cal_data[1] = 0;
  395. bma254->cal_data[2] = 0;
  396. }
  397. pr_info("%s: cal data (%d,%d,%d)\n", __func__,
  398. bma254->cal_data[0], bma254->cal_data[1],
  399. bma254->cal_data[2]);
  400. old_fs = get_fs();
  401. set_fs(KERNEL_DS);
  402. cal_filp = filp_open(CAL_PATH,
  403. O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, 0666);
  404. if (IS_ERR(cal_filp)) {
  405. pr_err("%s: Can't open calibration file\n", __func__);
  406. set_fs(old_fs);
  407. err = PTR_ERR(cal_filp);
  408. return err;
  409. }
  410. err = cal_filp->f_op->write(cal_filp,
  411. (char *)&bma254->cal_data, 3 * sizeof(s32), &cal_filp->f_pos);
  412. if (err != 3 * sizeof(s32)) {
  413. pr_err("%s: Can't write the cal data to file\n", __func__);
  414. err = -EIO;
  415. }
  416. filp_close(cal_filp, current->files);
  417. set_fs(old_fs);
  418. return err;
  419. }
  420. static ssize_t bma254_name_show(struct device *dev,
  421. struct device_attribute *attr, char *buf)
  422. {
  423. return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_DEV_NAME);
  424. }
  425. static ssize_t bma254_vendor_show(struct device *dev,
  426. struct device_attribute *attr, char *buf)
  427. {
  428. return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_DEV_VENDOR);
  429. }
  430. static ssize_t bma254_raw_data_show(struct device *dev,
  431. struct device_attribute *attr, char *buf)
  432. {
  433. struct bma254_data *bma254 = dev_get_drvdata(dev);
  434. int xyz[3] = {0, };
  435. int ret;
  436. if (!bma254->enable) {
  437. bma254_activate(bma254, true);
  438. #if defined(CONFIG_MACH_KANAS3G_CTC)||defined(CONFIG_MACH_KANAS3G_CMCC)
  439. msleep(300);
  440. #elif defined (CONFIG_MACH_VICTORLTE_CTC)
  441. msleep(50);
  442. #endif
  443. schedule_delayed_work(&bma254->work, msecs_to_jiffies(bma254->delay));
  444. }
  445. ret = bma254_get_data(bma254, xyz, true);
  446. if (ret < 0) {
  447. pr_err("%s, data error(%d)\n", __func__, ret);
  448. }
  449. if (!bma254->enable) {
  450. cancel_delayed_work_sync(&bma254->work);
  451. bma254_activate(bma254, false);
  452. }
  453. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
  454. xyz[0], xyz[1], xyz[2]);
  455. }
  456. static ssize_t bma254_calibartion_show(struct device *dev,
  457. struct device_attribute *attr, char *buf)
  458. {
  459. struct bma254_data *bma254 = dev_get_drvdata(dev);
  460. int ret;
  461. ret = bma254_open_calibration(bma254);
  462. if (ret < 0) {
  463. pr_err("%s, bma254_open_calibration(%d)\n", __func__, ret);
  464. }
  465. if (bma254->cal_data[0] == 0 &&
  466. bma254->cal_data[1] == 0 &&
  467. bma254->cal_data[2] == 0)
  468. ret = 0;
  469. else
  470. ret = 1;
  471. return snprintf(buf, PAGE_SIZE, "%d %d %d %d\n", ret,
  472. bma254->cal_data[0], bma254->cal_data[1], bma254->cal_data[2]);
  473. }
  474. static ssize_t bma254_calibartion_store(struct device *dev,
  475. struct device_attribute *attr,const char *buf, size_t count)
  476. {
  477. struct bma254_data *bma254 = dev_get_drvdata(dev);
  478. int value;
  479. int err;
  480. char tmp[64];
  481. err = kstrtoint(buf, 10, &value);
  482. if (err) {
  483. pr_err("%s, kstrtoint failed.", __func__);
  484. return -EINVAL;
  485. }
  486. err = bma254_do_calibrate(bma254, value);
  487. if (err < 0)
  488. pr_err("%s, bma254_do_calibrate(%d)\n", __func__, err);
  489. else
  490. err = 0;
  491. count = sprintf(tmp, "%d\n", err);
  492. return count;
  493. }
  494. #ifdef CONFIG_BMA254_SMART_ALERT
  495. static ssize_t bma254_reactive_enable_show(struct device *dev,
  496. struct device_attribute
  497. *attr, char *buf)
  498. {
  499. struct bma254_data *bma254 = dev_get_drvdata(dev);
  500. pr_info("%s: %d\n", __func__, atomic_read(&bma254->reactive_state));
  501. return snprintf(buf, PAGE_SIZE, "%d\n",
  502. atomic_read(&bma254->reactive_state));
  503. }
  504. static ssize_t bma254_reactive_enable_store(struct device *dev,
  505. struct device_attribute
  506. *attr, const char *buf,
  507. size_t count)
  508. {
  509. struct bma254_data *bma254 = dev_get_drvdata(dev);
  510. bool onoff = false;
  511. bool factory_test = false;
  512. unsigned long value = 0;
  513. int err = count;
  514. if (strict_strtoul(buf, 10, &value)) {
  515. err = -EINVAL;
  516. return err;
  517. }
  518. pr_info("%s: %lu\n", __func__, value);
  519. if(bma254->accsns_activate_flag == value)
  520. {
  521. pr_err("%s: duplicate value. Discard\n", __func__);
  522. return count;
  523. }
  524. switch (value) {
  525. case 0:
  526. bma254->accsns_activate_flag = 0;
  527. break;
  528. case 1:
  529. bma254->accsns_activate_flag = 1;
  530. onoff = true;
  531. break;
  532. case 2:
  533. bma254->accsns_activate_flag = 1;
  534. onoff = true;
  535. factory_test = true;
  536. break;
  537. default:
  538. //err = -EINVAL;
  539. pr_err("%s: invalid value %d\n", __func__, *buf);
  540. return count;
  541. }
  542. if (!bma254->pin_check_fail) {
  543. if (bma254->IRQ) {
  544. if (!value) {
  545. disable_irq_wake(bma254->IRQ);
  546. disable_irq(bma254->IRQ);
  547. } else {
  548. enable_irq(bma254->IRQ);
  549. enable_irq_wake(bma254->IRQ);
  550. }
  551. }
  552. mutex_lock(&bma254->data_mutex);
  553. atomic_set(&bma254->reactive_enable, onoff);
  554. if (bma254->IRQ) {
  555. bma254_set_motion_interrupt(bma254,
  556. onoff, factory_test);
  557. }
  558. atomic_set(&bma254->reactive_state, false);
  559. mutex_unlock(&bma254->data_mutex);
  560. pr_info("%s: value = %lu, onoff = %d, state =%d\n",
  561. __func__, value,
  562. atomic_read(&bma254->reactive_enable),
  563. atomic_read(&bma254->reactive_state));
  564. }
  565. return count;
  566. }
  567. #endif
  568. static DEVICE_ATTR(name, 0440, bma254_name_show, NULL);
  569. static DEVICE_ATTR(vendor, 0440, bma254_vendor_show, NULL);
  570. static DEVICE_ATTR(raw_data, 0440, bma254_raw_data_show, NULL);
  571. static DEVICE_ATTR(calibration, S_IRUGO | S_IWUSR | S_IWGRP,
  572. bma254_calibartion_show, bma254_calibartion_store);
  573. #ifdef CONFIG_BMA254_SMART_ALERT
  574. static DEVICE_ATTR(reactive_alert, S_IRUGO|S_IWUSR|S_IWGRP,
  575. bma254_reactive_enable_show, bma254_reactive_enable_store);
  576. #endif
  577. static struct device_attribute *bma254_attrs[] = {
  578. &dev_attr_name,
  579. &dev_attr_vendor,
  580. &dev_attr_raw_data,
  581. &dev_attr_calibration,
  582. #ifdef CONFIG_BMA254_SMART_ALERT
  583. &dev_attr_reactive_alert,
  584. #endif
  585. NULL,
  586. };
  587. #ifdef CONFIG_BMA254_SMART_ALERT
  588. static void bma254_work_func_alert(struct work_struct *work)
  589. {
  590. int result;
  591. struct bma254_data *bma254 = container_of(work,
  592. struct bma254_data, alert_work);
  593. result = bma254_get_motion_interrupt(bma254);
  594. if (result || bma254->factory_mode) {
  595. /*handle motion recognition*/
  596. atomic_set(&bma254->reactive_state, true);
  597. bma254->factory_mode = false;
  598. pr_info("%s: motion interrupt happened\n",
  599. __func__);
  600. wake_lock_timeout(&bma254->reactive_wake_lock,
  601. msecs_to_jiffies(2000));
  602. bma254_i2c_write(bma254, BMA254_INT_ENABLE1_REG, 0x00);
  603. }
  604. }
  605. irqreturn_t bma254_acc_irq_thread(int irq, void *dev)
  606. {
  607. struct bma254_data *data = dev;
  608. pr_info("%s\n", __func__);
  609. schedule_work(&data->alert_work);
  610. return IRQ_HANDLED;
  611. }
  612. static int bma254_setup_irq(struct bma254_data *bma254)
  613. {
  614. int irq = -1;
  615. struct bma254_platform_data *pdata = bma254->pdata;
  616. int rc;
  617. pr_info("%s\n", __func__);
  618. rc = gpio_request(pdata->p_out, "gpio_bma254_int");
  619. if (rc < 0) {
  620. pr_err("%s: gpio %d request failed (%d)\n",
  621. __func__, pdata->p_out, rc);
  622. return rc;
  623. }
  624. rc = gpio_direction_input(pdata->p_out);
  625. if (rc < 0) {
  626. pr_err("%s: failed to set gpio %d as input (%d)\n",
  627. __func__, pdata->p_out, rc);
  628. goto err_gpio_direction_input;
  629. }
  630. irq = gpio_to_irq(pdata->p_out);
  631. if (irq > 0) {
  632. rc = request_threaded_irq(irq,
  633. NULL, bma254_acc_irq_thread,
  634. IRQF_TRIGGER_RISING,
  635. "accelerometer", bma254);
  636. pr_info("%s: irq = %d\n", __func__, irq);
  637. if (rc < 0) {
  638. pr_err("%s request_threaded_irq fail err=%d\n",
  639. __func__, rc);
  640. return rc;
  641. }
  642. /* start with interrupts disabled */
  643. disable_irq(irq);
  644. }
  645. bma254->IRQ = irq;
  646. goto done;
  647. err_gpio_direction_input:
  648. gpio_free(pdata->p_out);
  649. done:
  650. return rc;
  651. }
  652. #endif
  653. #ifdef CONFIG_SENSORS_POWERCONTROL
  654. static int bma254_regulator_onoff(struct device *dev, bool onoff)
  655. {
  656. struct regulator *bma_vdd;
  657. pr_info("%s %s\n", __func__, (onoff) ? "on" : "off");
  658. bma_vdd = devm_regulator_get(dev, "bma254-vdd");
  659. if (IS_ERR(bma_vdd)) {
  660. pr_err("%s: cannot get bma_vdd\n", __func__);
  661. return -ENOMEM;
  662. }
  663. if (onoff) {
  664. regulator_enable(bma_vdd);
  665. } else {
  666. regulator_disable(bma_vdd);
  667. }
  668. devm_regulator_put(bma_vdd);
  669. msleep(10);
  670. return 0;
  671. }
  672. #endif
  673. static int bma254_parse_dt(struct bma254_data *data, struct device *dev)
  674. {
  675. struct device_node *this_node= dev->of_node;
  676. #ifdef CONFIG_BMA254_SMART_ALERT
  677. struct bma254_platform_data *pdata = data->pdata;
  678. enum of_gpio_flags flags;
  679. #endif
  680. u32 temp;
  681. if (this_node == NULL) {
  682. pr_err("%s,this_node is empty\n", __func__);
  683. return -ENODEV;
  684. }
  685. #ifdef CONFIG_BMA254_SMART_ALERT
  686. pdata->p_out = of_get_named_gpio_flags(this_node, "bma254,irq-gpio",
  687. 0, &flags);
  688. if (pdata->p_out < 0) {
  689. pr_err("%s : get irq_gpio(%d) error\n", __func__, pdata->p_out);
  690. return -ENODEV;
  691. }else {
  692. pr_info("%s, get irq_gpio(%d)\n", __func__, pdata->p_out);
  693. }
  694. #endif
  695. if (of_property_read_u32(this_node, "bma254,position", &temp) < 0) {
  696. pr_err("%s,get position(%d) error\n", __func__, temp);
  697. return -ENODEV;
  698. } else {
  699. data->position = (u8)temp;
  700. pr_info("%s, position(%d)\n", __func__, data->position);
  701. return 0;
  702. }
  703. }
  704. static int bma254_probe(struct i2c_client *client,
  705. const struct i2c_device_id *id)
  706. {
  707. int ret = 0;
  708. struct bma254_data *bma254;
  709. struct input_dev *dev;
  710. #ifdef CONFIG_BMA254_SMART_ALERT
  711. int err = 0;
  712. struct bma254_platform_data *pdata;
  713. #endif
  714. pr_info("%s, is called\n", __func__);
  715. if (client == NULL) {
  716. pr_err("%s, client doesn't exist\n", __func__);
  717. ret = -ENOMEM;
  718. return ret;
  719. }
  720. #ifdef CONFIG_SENSORS_POWERCONTROL
  721. ret = bma254_regulator_onoff(&client->dev, true);
  722. if (ret) {
  723. pr_err("%s, Power Up Failed\n", __func__);
  724. return ret;
  725. }
  726. #endif
  727. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  728. pr_err("%s,client not i2c capable\n", __func__);
  729. return -ENOMEM;
  730. }
  731. ret = i2c_smbus_read_word_data(client, BMA254_CHIP_ID);
  732. if ((ret & 0x00ff) != 0xfa) {
  733. pr_err("%s,i2c failed(%x)\n", __func__, ret & 0x00ff);
  734. ret = -ENOMEM;
  735. return ret;
  736. } else {
  737. pr_err("%s,chip id %x\n", __func__, ret & 0x00ff);
  738. }
  739. bma254 = kzalloc(sizeof(struct bma254_data), GFP_KERNEL);
  740. if (!bma254) {
  741. pr_err("%s, kzalloc error\n", __func__);
  742. ret = -ENOMEM;
  743. return ret;
  744. }
  745. #ifdef CONFIG_BMA254_SMART_ALERT
  746. if(client->dev.of_node) {
  747. pdata = devm_kzalloc(&client->dev,
  748. sizeof(struct bma254_platform_data), GFP_KERNEL);
  749. if (!pdata) {
  750. dev_err(&client->dev, "Failed to allocate memory \n");
  751. kfree(bma254);
  752. return -ENOMEM;
  753. }
  754. }else{
  755. pr_err("%s,this_node is empty\n", __func__);
  756. kfree(bma254);
  757. return -ENODEV;
  758. }
  759. bma254->pdata = pdata;
  760. #endif
  761. ret = bma254_parse_dt(bma254, &client->dev);
  762. if (ret) {
  763. pr_err("%s, get gpio is failed\n", __func__);
  764. goto parse_dt_err;
  765. }
  766. i2c_set_clientdata(client, bma254);
  767. bma254->client = client;
  768. bma254_activate(bma254, false);
  769. dev = input_allocate_device();
  770. if (!dev){
  771. goto input_allocate_device_err;
  772. }
  773. dev->name = "accelerometer";
  774. dev->id.bustype = BUS_I2C;
  775. #if !defined(CONFIG_MACH_VICTORLTE) && !defined(CONFIG_MACH_VICTOR3GDSDTV_LTN)
  776. dev->dev.parent = &client->dev;
  777. #endif
  778. #ifdef REPORT_ABS
  779. input_set_capability(dev, EV_ABS, ABS_MISC);
  780. input_set_abs_params(dev, ABS_X, ABSMIN, ABSMAX, 0, 0);
  781. input_set_abs_params(dev, ABS_Y, ABSMIN, ABSMAX, 0, 0);
  782. input_set_abs_params(dev, ABS_Z, ABSMIN, ABSMAX, 0, 0);
  783. #else
  784. input_set_capability(dev, EV_REL, REL_X);
  785. input_set_capability(dev, EV_REL, REL_Y);
  786. input_set_capability(dev, EV_REL, REL_Z);
  787. #endif
  788. input_set_drvdata(dev, bma254);
  789. ret = input_register_device(dev);
  790. if (ret < 0) {
  791. pr_err("%s,sysfs_create_group failed\n", __func__);
  792. goto input_register_device_err;
  793. }
  794. bma254->input = dev;
  795. #ifdef CONFIG_BMA254_SMART_ALERT
  796. pr_info("%s: HW_rev success %d\n", __func__, system_rev);
  797. INIT_WORK(&bma254->alert_work, bma254_work_func_alert);
  798. wake_lock_init(&bma254->reactive_wake_lock, WAKE_LOCK_SUSPEND,
  799. "reactive_wake_lock");
  800. err = bma254_setup_irq(bma254);
  801. if (err) {
  802. bma254->pin_check_fail = true;
  803. pr_err("%s: could not setup irq\n", __func__);
  804. goto err_setup_irq;
  805. }
  806. mutex_init(&bma254->data_mutex);
  807. #endif
  808. ret = sensors_create_symlink(&dev->dev.kobj, dev->name);
  809. if (ret < 0) {
  810. input_unregister_device(dev);
  811. return ret;
  812. }
  813. ret = sysfs_create_group(&bma254->input->dev.kobj,
  814. &bma254_attribute_group);
  815. if (ret < 0) {
  816. pr_err("%s,sysfs_create_group failed\n", __func__);
  817. sensors_remove_symlink(&bma254->input->dev.kobj,
  818. bma254->input->name);
  819. goto sysfs_create_group_err;
  820. }
  821. INIT_DELAYED_WORK(&bma254->work, bma254_work_func);
  822. bma254->delay = MAX_DELAY;
  823. bma254->enable = 0;
  824. ret = sensors_register(bma254->dev, bma254,
  825. bma254_attrs, "accelerometer_sensor");
  826. if (ret < 0) {
  827. pr_info("%s: could not sensors_register\n", __func__);
  828. goto sensors_register_err;
  829. }
  830. #ifdef CONFIG_SENSORS_POWERCONTROL
  831. bma254_regulator_onoff(&client->dev, false);
  832. #endif
  833. pr_info("[SENSOR]: %s - Probe done!(chip pos : %d)\n",
  834. __func__, bma254->position);
  835. return 0;
  836. sensors_register_err:
  837. sysfs_remove_group(&bma254->input->dev.kobj,
  838. &bma254_attribute_group);
  839. sensors_remove_symlink(&bma254->input->dev.kobj, bma254->input->name);
  840. sysfs_create_group_err:
  841. input_unregister_device(bma254->input);
  842. #ifdef CONFIG_BMA254_SMART_ALERT
  843. err_setup_irq:
  844. wake_lock_destroy(&bma254->reactive_wake_lock);
  845. #endif
  846. input_register_device_err:
  847. input_free_device(dev);
  848. input_allocate_device_err:
  849. parse_dt_err:
  850. kfree(bma254);
  851. pr_err("[SENSOR]: %s - Probe fail!\n", __func__);
  852. #ifdef CONFIG_SENSORS_POWERCONTROL
  853. bma254_regulator_onoff(&client->dev, false);
  854. #endif
  855. return ret;
  856. }
  857. static int bma254_suspend(struct device *dev)
  858. {
  859. struct i2c_client *client = to_i2c_client(dev);
  860. struct bma254_data *bma254 = i2c_get_clientdata(client);
  861. pr_info("%s, enable(%d)\n", __func__, bma254->enable);
  862. if (bma254->enable) {
  863. cancel_delayed_work_sync(&bma254->work);
  864. bma254_activate(bma254, false);
  865. }
  866. return 0;
  867. }
  868. static int bma254_resume(struct device *dev)
  869. {
  870. struct i2c_client *client = to_i2c_client(dev);
  871. struct bma254_data *bma254 = i2c_get_clientdata(client);
  872. pr_info("%s, enable(%d)\n", __func__, bma254->enable);
  873. if (bma254->enable) {
  874. bma254_activate(bma254, true);
  875. schedule_delayed_work(&bma254->work, msecs_to_jiffies(bma254->delay));
  876. }
  877. return 0;
  878. }
  879. static const struct dev_pm_ops bma254_dev_pm_ops = {
  880. .suspend = bma254_suspend,
  881. .resume = bma254_resume,
  882. };
  883. static const u16 normal_i2c[] = { I2C_CLIENT_END };
  884. static const struct i2c_device_id bma254_device_id[] = {
  885. {"bma254", 0},
  886. {}
  887. };
  888. MODULE_DEVICE_TABLE(i2c, bma254_device_id);
  889. static struct of_device_id bma254_match_table[] = {
  890. { .compatible = "bma254",},
  891. {},
  892. };
  893. MODULE_DEVICE_TABLE(of, bma254_match_table);
  894. static struct i2c_driver bma254_i2c_driver = {
  895. .driver = {
  896. .name = "bma254",
  897. .owner = THIS_MODULE,
  898. .of_match_table = bma254_match_table,
  899. .pm = &bma254_dev_pm_ops,
  900. },
  901. .probe = bma254_probe,
  902. .id_table = bma254_device_id,
  903. .address_list = normal_i2c,
  904. };
  905. module_i2c_driver(bma254_i2c_driver);
  906. MODULE_AUTHOR("daehan.wi@samsung.com");
  907. MODULE_DESCRIPTION("G-Sensor Driver for BMA254");
  908. MODULE_LICENSE("GPL");