k303c_acc.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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/err.h>
  19. #include <linux/errno.h>
  20. #include <linux/delay.h>
  21. #include <linux/fs.h>
  22. #include <linux/i2c.h>
  23. #include <linux/input.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/irq.h>
  27. #include <linux/gpio.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/slab.h>
  30. #include <linux/kernel.h>
  31. #include <linux/device.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/regulator/consumer.h>
  35. #include <linux/of_gpio.h>
  36. #include <linux/wakelock.h>
  37. #include <linux/sensor/sensors_core.h>
  38. #define I2C_M_WR 0 /* for i2c Write */
  39. #define READ_DATA_LENTH 6
  40. #define VENDOR_NAME "STM"
  41. #define MODEL_NAME "K303C"
  42. #define MODULE_NAME "accelerometer_sensor"
  43. #define CALIBRATION_FILE_PATH "/efs/accel_calibration_data"
  44. #define CALIBRATION_DATA_AMOUNT 20
  45. #define MAX_ACCEL_1G 16384
  46. #define K303C_DEFAULT_DELAY 200000000LL
  47. #define CHIP_ID_RETRIES 3
  48. #define ACCEL_LOG_TIME 15 /* 15 sec */
  49. #define K303C_TOP_UPPER_RIGHT 0
  50. #define K303C_TOP_LOWER_RIGHT 1
  51. #define K303C_TOP_LOWER_LEFT 2
  52. #define K303C_TOP_UPPER_LEFT 3
  53. #define K303C_BOTTOM_UPPER_RIGHT 4
  54. #define K303C_BOTTOM_LOWER_RIGHT 5
  55. #define K303C_BOTTOM_LOWER_LEFT 6
  56. #define K303C_BOTTOM_UPPER_LEFT 7
  57. #define K303C_MODE_SUSPEND 0
  58. #define K303C_MODE_NORMAL 1
  59. #define SENSITIVITY_2G 61
  60. #define SENSITIVITY_4G 122
  61. #define SENSITIVITY_8G 244
  62. #define K303C_RANGE_2G 0
  63. #define K303C_RANGE_4G 1
  64. #define K303C_RANGE_8G 2
  65. #define WHOAMI_REG 0x0F
  66. #define AXISDATA_REG 0x28
  67. #define CTRL1_REG 0x20
  68. #define CTRL2_REG 0x21
  69. #define CTRL3_REG 0x22
  70. #define CTRL4_REG 0x23
  71. #define CTRL5_REG 0x24
  72. #define CTRL6_REG 0x25
  73. #define CTRL7_REG 0x26
  74. #define STATUS_REG 0x27
  75. /* CTRL1 */
  76. #define CTRL1_HR_DISABLE 0x00
  77. #define CTRL1_HR_ENABLE 0x80
  78. #define CTRL1_HR_MASK 0x80
  79. #define CTRL1_BDU_ENABLE 0x08
  80. #define CTRL1_BDU_MASK 0x08
  81. /* CTRL2 */
  82. #define CTRL2_IG1_INT1 0x08
  83. /* CTRL3 */
  84. #define CTRL3_IG1_INT1 0x08
  85. /* CTRL7 */
  86. #define CTRL7_LIR2 0x08
  87. #define CTRL7_LIR1 0x04
  88. #define ACC_PM_OFF 0x00
  89. #define ACC_ENABLE_ALL_AXES 0x07
  90. #define INT_CFG1_REG 0x30
  91. #define INT_SRC1_REG 0x31
  92. #define K303C_CHIP_ID 0x41
  93. #define K303C_ACC_FS_MASK 0x30
  94. #define K303C_ACC_ODR_MASK 0x70
  95. #define K303C_ACC_AXES_MASK 0x07
  96. #define SELF_TEST_2G_MAX_LSB 24576
  97. #define SELF_TEST_2G_MIN_LSB 1146
  98. #define K303C_ACC_FS_2G 0x00
  99. #define K303C_ACC_FS_4G 0x20
  100. #define K303C_ACC_FS_8G 0x30
  101. #define INT_THSX1_REG 0x32
  102. #define INT_THSY1_REG 0x33
  103. #define INT_THSZ1_REG 0x34
  104. #define DYNAMIC_THRESHOLD 5000
  105. enum {
  106. OFF = 0,
  107. ON = 1
  108. };
  109. struct k303c_acc_v {
  110. union {
  111. s16 v[3];
  112. struct {
  113. s16 x;
  114. s16 y;
  115. s16 z;
  116. };
  117. };
  118. };
  119. struct k303c_acc_p {
  120. struct wake_lock reactive_wake_lock;
  121. struct i2c_client *client;
  122. struct input_dev *input;
  123. struct delayed_work irq_work;
  124. struct device *factory_device;
  125. struct k303c_acc_v accdata;
  126. struct k303c_acc_v caldata;
  127. struct mutex mode_mutex;
  128. struct hrtimer accel_timer;
  129. struct workqueue_struct *accel_wq;
  130. struct work_struct work;
  131. struct regulator *vdd;
  132. struct regulator *vio;
  133. ktime_t poll_delay;
  134. atomic_t enable;
  135. struct regulator *reg_vdd;
  136. struct regulator *reg_vio;
  137. int recog_flag;
  138. int irq1;
  139. int irq_state;
  140. int acc_int1;
  141. int sda_gpio;
  142. int scl_gpio;
  143. int time_count;
  144. u8 axis_map_x;
  145. u8 axis_map_y;
  146. u8 axis_map_z;
  147. u8 negate_x;
  148. u8 negate_y;
  149. u8 negate_z;
  150. };
  151. #define ACC_ODR10 0x10 /* 10Hz output data rate */
  152. #define ACC_ODR50 0x20 /* 50Hz output data rate */
  153. #define ACC_ODR100 0x30 /* 100Hz output data rate */
  154. #define ACC_ODR200 0x40 /* 200Hz output data rate */
  155. #define ACC_ODR400 0x50 /* 400Hz output data rate */
  156. #define ACC_ODR800 0x60 /* 800Hz output data rate */
  157. #define ACC_ODR_MASK 0X70
  158. struct k303c_acc_odr {
  159. unsigned int cutoff_ms;
  160. unsigned int mask;
  161. };
  162. #define OUTPUT_ALWAYS_ANTI_ALIASED
  163. const struct k303c_acc_odr k303c_acc_odr_table[] = {
  164. { 2, ACC_ODR800},
  165. { 3, ACC_ODR400},
  166. { 5, ACC_ODR200},
  167. { 10, ACC_ODR100},
  168. #ifndef OUTPUT_ALWAYS_ANTI_ALIASED
  169. { 20, ACC_ODR50},
  170. {100, ACC_ODR10},
  171. #endif
  172. };
  173. #define MAX_VDD 3000000
  174. #define AVG_VDD 2850000
  175. #define AVG_VIO 1800000
  176. static int k303c_acc_i2c_read(struct k303c_acc_p *data, unsigned char reg_addr,
  177. unsigned char *buf, unsigned int len)
  178. {
  179. int ret, retries = 0;
  180. struct i2c_msg msg[2];
  181. msg[0].addr = data->client->addr;
  182. msg[0].flags = I2C_M_WR;
  183. msg[0].len = 1;
  184. msg[0].buf = &reg_addr;
  185. msg[1].addr = data->client->addr;
  186. msg[1].flags = I2C_M_RD;
  187. msg[1].len = len;
  188. msg[1].buf = buf;
  189. do {
  190. ret = i2c_transfer(data->client->adapter, msg, 2);
  191. if (ret >= 0)
  192. break;
  193. } while (retries++ < 2);
  194. if (ret < 0) {
  195. pr_err("%s - i2c read error %d\n", __func__, ret);
  196. return ret;
  197. }
  198. return 0;
  199. }
  200. static int k303c_acc_i2c_write(struct k303c_acc_p *data,
  201. unsigned char reg_addr, unsigned char buf)
  202. {
  203. int ret, retries = 0;
  204. struct i2c_msg msg;
  205. unsigned char w_buf[2];
  206. w_buf[0] = reg_addr;
  207. w_buf[1] = buf;
  208. msg.addr = data->client->addr;
  209. msg.flags = I2C_M_WR;
  210. msg.len = 2;
  211. msg.buf = (char *)w_buf;
  212. do {
  213. ret = i2c_transfer(data->client->adapter, &msg, 1);
  214. if (ret >= 0)
  215. break;
  216. } while (retries++ < 2);
  217. if (ret < 0) {
  218. pr_err("%s - i2c write error %d\n", __func__, ret);
  219. return ret;
  220. }
  221. return 0;
  222. }
  223. static int k303c_regulator_onoff(struct k303c_acc_p *data, bool onoff)
  224. {
  225. int ret;
  226. if (IS_ERR(data->reg_vdd) || IS_ERR(data->reg_vio)) {
  227. pr_err("%s: Failed to enable regulator.\n", __func__);
  228. return -ENODEV;
  229. }
  230. if (onoff) {
  231. ret = regulator_enable(data->reg_vdd);
  232. if (ret) {
  233. pr_err("%s: Failed to enable vdd.\n", __func__);
  234. return ret;
  235. }
  236. ret = regulator_enable(data->reg_vio);
  237. if (ret) {
  238. pr_err("%s: Failed to enable vio.\n", __func__);
  239. return ret;
  240. }
  241. msleep(30);
  242. } else {
  243. ret = regulator_disable(data->reg_vdd);
  244. if (ret) {
  245. pr_err("%s: Failed to disable vdd.\n", __func__);
  246. return ret;
  247. }
  248. ret = regulator_disable(data->reg_vio);
  249. if (ret) {
  250. pr_err("%s: Failed to disable vio.\n", __func__);
  251. return ret;
  252. }
  253. }
  254. return 0;
  255. }
  256. static int k303c_acc_set_range(struct k303c_acc_p *data, unsigned char range)
  257. {
  258. int ret = 0;
  259. unsigned char temp, new_range, buf, mask;
  260. switch (range) {
  261. case K303C_RANGE_2G:
  262. new_range = K303C_ACC_FS_2G;
  263. break;
  264. case K303C_RANGE_4G:
  265. new_range = K303C_ACC_FS_4G;
  266. break;
  267. case K303C_RANGE_8G:
  268. new_range = K303C_ACC_FS_8G;
  269. break;
  270. default:
  271. new_range = K303C_ACC_FS_2G;
  272. break;
  273. }
  274. mask = K303C_ACC_FS_MASK;
  275. ret = k303c_acc_i2c_read(data, CTRL4_REG, &temp, 1);
  276. buf = (mask & new_range) | ((~mask) & temp);
  277. #ifdef OUTPUT_ALWAYS_ANTI_ALIASED
  278. buf |= 0xC8;
  279. #endif
  280. ret += k303c_acc_i2c_write(data, CTRL4_REG, buf);
  281. return ret;
  282. }
  283. static int k303c_acc_set_odr(struct k303c_acc_p *data)
  284. {
  285. int ret = 0, i;
  286. unsigned char buf, new_odr, mask, temp;
  287. pr_info("%s\n", __func__);
  288. /* Following, looks for the longest possible odr interval scrolling the
  289. * odr_table vector from the end (shortest interval) backward (longest
  290. * interval), to support the poll_interval requested by the system.
  291. * It must be the longest interval lower then the poll interval.*/
  292. for (i = ARRAY_SIZE(k303c_acc_odr_table) - 1; i >= 0; i--) {
  293. if ((k303c_acc_odr_table[i].cutoff_ms <=
  294. ktime_to_ms(data->poll_delay)) || (i == 0))
  295. break;
  296. }
  297. if (data->recog_flag == ON)
  298. i = ARRAY_SIZE(k303c_acc_odr_table) - 1;
  299. new_odr = k303c_acc_odr_table[i].mask;
  300. mask = K303C_ACC_ODR_MASK;
  301. ret = k303c_acc_i2c_read(data, CTRL1_REG, &temp, 1);
  302. buf = ((mask & new_odr) | ((~mask) & temp));
  303. ret += k303c_acc_i2c_write(data, CTRL1_REG, buf);
  304. pr_info("%s - change odr %d\n", __func__, i);
  305. return ret;
  306. }
  307. static int k303c_acc_set_mode(struct k303c_acc_p *data, unsigned char mode)
  308. {
  309. int ret = 0;
  310. unsigned char buf, mask, temp;
  311. static unsigned char acc_odr;
  312. mutex_lock(&data->mode_mutex);
  313. switch (mode) {
  314. case K303C_MODE_NORMAL:
  315. mask = K303C_ACC_ODR_MASK;
  316. ret = k303c_acc_i2c_read(data, CTRL1_REG, &temp, 1);
  317. buf = ((mask & acc_odr) | ((~mask) & temp));
  318. buf |= 0x8f;
  319. ret += k303c_acc_i2c_write(data, CTRL1_REG, buf);
  320. break;
  321. case K303C_MODE_SUSPEND:
  322. if (data->recog_flag == ON)
  323. break;
  324. mask = K303C_ACC_ODR_MASK;
  325. ret = k303c_acc_i2c_read(data, CTRL1_REG, &temp, 1);
  326. acc_odr = temp & mask;
  327. buf = ((mask & ACC_PM_OFF) | ((~mask) & temp));
  328. ret += k303c_acc_i2c_write(data, CTRL1_REG, buf);
  329. break;
  330. default:
  331. ret = -EINVAL;
  332. break;
  333. }
  334. mutex_unlock(&data->mode_mutex);
  335. msleep(20);
  336. return ret;
  337. }
  338. static int k303c_acc_read_accel_xyz(struct k303c_acc_p *data,
  339. struct k303c_acc_v *acc)
  340. {
  341. int ret = 0;
  342. struct k303c_acc_v rawdata;
  343. unsigned char buf[READ_DATA_LENTH];
  344. ret += k303c_acc_i2c_read(data, AXISDATA_REG, buf, READ_DATA_LENTH);
  345. if (ret < 0)
  346. goto exit;
  347. rawdata.v[0] = ((s16) ((buf[1] << 8) | buf[0]));
  348. rawdata.v[1] = ((s16) ((buf[3] << 8) | buf[2]));
  349. rawdata.v[2] = ((s16) ((buf[5] << 8) | buf[4]));
  350. acc->v[0] = ((data->negate_x) ? (-rawdata.v[data->axis_map_x])
  351. : (rawdata.v[data->axis_map_x]));
  352. acc->v[1] = ((data->negate_y) ? (-rawdata.v[data->axis_map_y])
  353. : (rawdata.v[data->axis_map_y]));
  354. acc->v[2] = ((data->negate_z) ? (-rawdata.v[data->axis_map_z])
  355. : (rawdata.v[data->axis_map_z]));
  356. exit:
  357. return ret;
  358. }
  359. static void k303c_acc_set_enable(struct k303c_acc_p *data, int enable)
  360. {
  361. pr_info("%s %d\n", __func__, enable);
  362. if (enable == ON) {
  363. hrtimer_start(&data->accel_timer, data->poll_delay,
  364. HRTIMER_MODE_REL);
  365. } else {
  366. hrtimer_cancel(&data->accel_timer);
  367. cancel_work_sync(&data->work);
  368. }
  369. }
  370. static int k303c_acc_open_calibration(struct k303c_acc_p *data)
  371. {
  372. int ret = 0;
  373. mm_segment_t old_fs;
  374. struct file *cal_filp = NULL;
  375. pr_info("%s\n", __func__);
  376. old_fs = get_fs();
  377. set_fs(KERNEL_DS);
  378. cal_filp = filp_open(CALIBRATION_FILE_PATH, O_RDONLY,
  379. S_IRUGO | S_IWUSR | S_IWGRP);
  380. if (IS_ERR(cal_filp)) {
  381. set_fs(old_fs);
  382. ret = PTR_ERR(cal_filp);
  383. data->caldata.x = 0;
  384. data->caldata.y = 0;
  385. data->caldata.z = 0;
  386. pr_info("%s - No Calibration\n", __func__);
  387. return ret;
  388. }
  389. ret = cal_filp->f_op->read(cal_filp, (char *)&data->caldata.v,
  390. 3 * sizeof(s16), &cal_filp->f_pos);
  391. if (ret != 3 * sizeof(s16)) {
  392. pr_err("%s: - Can't read the cal data\n", __func__);
  393. ret = -EIO;
  394. }
  395. filp_close(cal_filp, current->files);
  396. set_fs(old_fs);
  397. pr_info("%s open accel calibration %d, %d, %d\n", __func__,
  398. data->caldata.x, data->caldata.y, data->caldata.z);
  399. if ((data->caldata.x == 0) && (data->caldata.y == 0)
  400. && (data->caldata.z == 0))
  401. return -EIO;
  402. return ret;
  403. }
  404. static int k303c_acc_do_calibrate(struct k303c_acc_p *data, int enable)
  405. {
  406. int sum[3] = { 0, };
  407. int ret = 0, cnt;
  408. struct file *cal_filp = NULL;
  409. struct k303c_acc_v acc;
  410. mm_segment_t old_fs;
  411. pr_info("%s\n", __func__);
  412. if (enable) {
  413. data->caldata.x = 0;
  414. data->caldata.y = 0;
  415. data->caldata.z = 0;
  416. if (atomic_read(&data->enable) == ON)
  417. k303c_acc_set_enable(data, OFF);
  418. else
  419. k303c_acc_set_mode(data, K303C_MODE_NORMAL);
  420. msleep(300);
  421. for (cnt = 0; cnt < CALIBRATION_DATA_AMOUNT; cnt++) {
  422. k303c_acc_read_accel_xyz(data, &acc);
  423. sum[0] += acc.x;
  424. sum[1] += acc.y;
  425. sum[2] += acc.z;
  426. msleep(20);
  427. }
  428. if (atomic_read(&data->enable) == ON)
  429. k303c_acc_set_enable(data, ON);
  430. else
  431. k303c_acc_set_mode(data, K303C_MODE_SUSPEND);
  432. data->caldata.x = (sum[0] / CALIBRATION_DATA_AMOUNT);
  433. data->caldata.y = (sum[1] / CALIBRATION_DATA_AMOUNT);
  434. data->caldata.z = (sum[2] / CALIBRATION_DATA_AMOUNT);
  435. if (data->caldata.z > 0)
  436. data->caldata.z -= MAX_ACCEL_1G;
  437. else if (data->caldata.z < 0)
  438. data->caldata.z += MAX_ACCEL_1G;
  439. } else {
  440. data->caldata.x = 0;
  441. data->caldata.y = 0;
  442. data->caldata.z = 0;
  443. }
  444. pr_info("%s - do accel calibrate %d, %d, %d\n", __func__,
  445. data->caldata.x, data->caldata.y, data->caldata.z);
  446. old_fs = get_fs();
  447. set_fs(KERNEL_DS);
  448. cal_filp = filp_open(CALIBRATION_FILE_PATH,
  449. O_CREAT | O_TRUNC | O_WRONLY, S_IRUGO | S_IWUSR | S_IWGRP);
  450. if (IS_ERR(cal_filp)) {
  451. pr_err("%s - Can't open calibration file\n", __func__);
  452. set_fs(old_fs);
  453. ret = PTR_ERR(cal_filp);
  454. return ret;
  455. }
  456. ret = cal_filp->f_op->write(cal_filp, (char *)&data->caldata.v,
  457. 3 * sizeof(s16), &cal_filp->f_pos);
  458. if (ret != 3 * sizeof(s16)) {
  459. pr_err("%s - Can't write the caldata to file\n", __func__);
  460. ret = -EIO;
  461. }
  462. filp_close(cal_filp, current->files);
  463. set_fs(old_fs);
  464. return ret;
  465. }
  466. static ssize_t k303c_acc_enable_show(struct device *dev,
  467. struct device_attribute *attr, char *buf)
  468. {
  469. struct k303c_acc_p *data = dev_get_drvdata(dev);
  470. pr_info("%s\n", __func__);
  471. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&data->enable));
  472. }
  473. static ssize_t k303c_acc_enable_store(struct device *dev,
  474. struct device_attribute *attr, const char *buf, size_t size)
  475. {
  476. u8 enable;
  477. int ret, pre_enable;
  478. struct k303c_acc_p *data = dev_get_drvdata(dev);
  479. ret = kstrtou8(buf, 2, &enable);
  480. if (ret) {
  481. pr_err("%s - Invalid Argument\n", __func__);
  482. return ret;
  483. }
  484. pre_enable = atomic_read(&data->enable);
  485. pr_info("%s new=%u, pre=%u\n", __func__, enable, pre_enable);
  486. if (enable) {
  487. if (pre_enable == OFF) {
  488. k303c_acc_open_calibration(data);
  489. k303c_acc_set_range(data, K303C_RANGE_2G);
  490. k303c_acc_set_enable(data, ON);
  491. k303c_acc_set_mode(data, K303C_MODE_NORMAL);
  492. atomic_set(&data->enable, ON);
  493. }
  494. } else {
  495. if (pre_enable == ON) {
  496. atomic_set(&data->enable, OFF);
  497. k303c_acc_set_mode(data, K303C_MODE_SUSPEND);
  498. k303c_acc_set_enable(data, OFF);
  499. }
  500. }
  501. return size;
  502. }
  503. static ssize_t k303c_acc_delay_show(struct device *dev,
  504. struct device_attribute *attr, char *buf)
  505. {
  506. struct k303c_acc_p *data = dev_get_drvdata(dev);
  507. pr_info("%s\n", __func__);
  508. return snprintf(buf, PAGE_SIZE, "%lld\n",
  509. ktime_to_ns(data->poll_delay));
  510. }
  511. static ssize_t k303c_acc_delay_store(struct device *dev,
  512. struct device_attribute *attr, const char *buf, size_t size)
  513. {
  514. int ret;
  515. int64_t delay;
  516. struct k303c_acc_p *data = dev_get_drvdata(dev);
  517. pr_info("%s\n", __func__);
  518. ret = kstrtoll(buf, 10, &delay);
  519. if (ret) {
  520. pr_err("%s - Invalid Argument\n", __func__);
  521. return ret;
  522. }
  523. data->poll_delay = ns_to_ktime(delay);
  524. k303c_acc_set_odr(data);
  525. if (atomic_read(&data->enable) == ON) {
  526. k303c_acc_set_mode(data, K303C_MODE_SUSPEND);
  527. k303c_acc_set_mode(data, K303C_MODE_NORMAL);
  528. }
  529. pr_info("%s - poll_delay = %lld\n", __func__, delay);
  530. return size;
  531. }
  532. static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
  533. k303c_acc_delay_show, k303c_acc_delay_store);
  534. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  535. k303c_acc_enable_show, k303c_acc_enable_store);
  536. static struct attribute *k303c_acc_attributes[] = {
  537. &dev_attr_poll_delay.attr,
  538. &dev_attr_enable.attr,
  539. NULL
  540. };
  541. static struct attribute_group k303c_acc_attribute_group = {
  542. .attrs = k303c_acc_attributes
  543. };
  544. static ssize_t k303c_acc_name_show(struct device *dev,
  545. struct device_attribute *attr, char *buf)
  546. {
  547. pr_info("%s\n", __func__);
  548. return snprintf(buf, PAGE_SIZE, "%s\n", MODEL_NAME);
  549. }
  550. static ssize_t k303c_acc_vendor_show(struct device *dev,
  551. struct device_attribute *attr, char *buf)
  552. {
  553. pr_info("%s\n", __func__);
  554. return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR_NAME);
  555. }
  556. static ssize_t k303c_acc_calibration_show(struct device *dev,
  557. struct device_attribute *attr, char *buf)
  558. {
  559. int ret;
  560. struct k303c_acc_p *data = dev_get_drvdata(dev);
  561. pr_info("%s\n", __func__);
  562. ret = k303c_acc_open_calibration(data);
  563. if (ret < 0)
  564. pr_err("%s - calibration open failed(%d)\n", __func__, ret);
  565. pr_info("%s - cal data %d %d %d - ret : %d\n", __func__,
  566. data->caldata.x, data->caldata.y, data->caldata.z, ret);
  567. return snprintf(buf, PAGE_SIZE, "%d %d %d %d\n", ret, data->caldata.x,
  568. data->caldata.y, data->caldata.z);
  569. }
  570. static ssize_t k303c_acc_calibration_store(struct device *dev,
  571. struct device_attribute *attr, const char *buf, size_t size)
  572. {
  573. int ret;
  574. int64_t dEnable;
  575. struct k303c_acc_p *data = dev_get_drvdata(dev);
  576. pr_info("%s\n", __func__);
  577. ret = kstrtoll(buf, 10, &dEnable);
  578. if (ret < 0)
  579. return ret;
  580. ret = k303c_acc_do_calibrate(data, (int)dEnable);
  581. if (ret < 0)
  582. pr_err("%s - accel calibrate failed\n", __func__);
  583. return size;
  584. }
  585. static ssize_t k303c_acc_raw_data_read(struct device *dev,
  586. struct device_attribute *attr, char *buf)
  587. {
  588. struct k303c_acc_v acc;
  589. struct k303c_acc_p *data = dev_get_drvdata(dev);
  590. if (atomic_read(&data->enable) == OFF) {
  591. k303c_acc_set_mode(data, K303C_MODE_NORMAL);
  592. k303c_acc_read_accel_xyz(data, &acc);
  593. k303c_acc_set_mode(data, K303C_MODE_SUSPEND);
  594. acc.x = acc.x - data->caldata.x;
  595. acc.y = acc.y - data->caldata.y;
  596. acc.z = acc.z - data->caldata.z;
  597. } else {
  598. acc = data->accdata;
  599. }
  600. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", acc.x, acc.y, acc.z);
  601. }
  602. static ssize_t k303c_acc_reactive_alert_show(struct device *dev,
  603. struct device_attribute *attr, char *buf)
  604. {
  605. struct k303c_acc_p *data = dev_get_drvdata(dev);
  606. pr_info("%s\n", __func__);
  607. return snprintf(buf, PAGE_SIZE, "%d\n", data->irq_state);
  608. }
  609. static ssize_t k303c_acc_reactive_alert_store(struct device *dev,
  610. struct device_attribute *attr, const char *buf, size_t size)
  611. {
  612. unsigned char threshx, threshy, threshz;
  613. int enable = OFF, factory_mode = OFF;
  614. struct k303c_acc_v acc;
  615. struct k303c_acc_p *data = dev_get_drvdata(dev);
  616. pr_info("%s\n", __func__);
  617. if (sysfs_streq(buf, "0")) {
  618. enable = OFF;
  619. factory_mode = OFF;
  620. pr_info("%s - disable\n", __func__);
  621. } else if (sysfs_streq(buf, "1")) {
  622. enable = ON;
  623. factory_mode = OFF;
  624. pr_info("%s - enable\n", __func__);
  625. } else if (sysfs_streq(buf, "2")) {
  626. enable = ON;
  627. factory_mode = ON;
  628. pr_info("%s - factory mode\n", __func__);
  629. } else {
  630. pr_err("%s - invalid value %d\n", __func__, *buf);
  631. return -EINVAL;
  632. }
  633. if ((enable == ON) && (data->recog_flag == OFF)) {
  634. data->irq_state = 0;
  635. data->recog_flag = ON;
  636. if (factory_mode == ON) {
  637. k303c_acc_i2c_write(data, INT_THSX1_REG, 0x00);
  638. k303c_acc_i2c_write(data, INT_THSY1_REG, 0x00);
  639. k303c_acc_i2c_write(data, INT_THSZ1_REG, 0x00);
  640. k303c_acc_i2c_write(data, INT_CFG1_REG, 0x3f);
  641. } else {
  642. k303c_acc_set_odr(data);
  643. if (atomic_read(&data->enable) == OFF) {
  644. k303c_acc_set_mode(data, K303C_MODE_NORMAL);
  645. k303c_acc_read_accel_xyz(data, &acc);
  646. k303c_acc_set_mode(data, K303C_MODE_SUSPEND);
  647. } else {
  648. acc.x = data->accdata.x;
  649. acc.y = data->accdata.y;
  650. acc.z = data->accdata.z;
  651. }
  652. threshx = (abs(acc.v[data->axis_map_x])
  653. + DYNAMIC_THRESHOLD) >> 8;
  654. threshy = (abs(acc.v[data->axis_map_y])
  655. + DYNAMIC_THRESHOLD) >> 8;
  656. threshz = (abs(acc.v[data->axis_map_z])
  657. + DYNAMIC_THRESHOLD) >> 8;
  658. k303c_acc_i2c_write(data, INT_THSX1_REG, threshx);
  659. k303c_acc_i2c_write(data, INT_THSY1_REG, threshy);
  660. k303c_acc_i2c_write(data, INT_THSZ1_REG, threshz);
  661. k303c_acc_i2c_write(data, INT_CFG1_REG, 0x0a);
  662. }
  663. k303c_acc_i2c_write(data, CTRL7_REG, CTRL7_LIR1);
  664. k303c_acc_i2c_write(data, CTRL3_REG, CTRL3_IG1_INT1);
  665. enable_irq(data->irq1);
  666. enable_irq_wake(data->irq1);
  667. pr_info("%s - reactive alert is on!\n", __func__);
  668. } else if ((enable == OFF) && (data->recog_flag == ON)) {
  669. k303c_acc_i2c_write(data, CTRL3_REG, 0x00);
  670. disable_irq_wake(data->irq1);
  671. disable_irq_nosync(data->irq1);
  672. data->recog_flag = OFF;
  673. pr_info("%s - reactive alert is off! irq = %d\n",
  674. __func__, data->irq_state);
  675. }
  676. return size;
  677. }
  678. static ssize_t k303c_acc_selftest_show(struct device *dev,
  679. struct device_attribute *attr, char *buf)
  680. {
  681. struct k303c_acc_p *data = dev_get_drvdata(dev);
  682. struct k303c_acc_v acc;
  683. unsigned char temp;
  684. int result = 1, i;
  685. ssize_t ret;
  686. s32 NO_ST[3] = {0, 0, 0};
  687. s32 ST[3] = {0, 0, 0};
  688. if (atomic_read(&data->enable) == OFF)
  689. k303c_acc_set_mode(data, K303C_MODE_NORMAL);
  690. else
  691. k303c_acc_set_enable(data, OFF);
  692. k303c_acc_i2c_write(data, CTRL1_REG, 0x3f);
  693. k303c_acc_i2c_write(data, CTRL4_REG, 0x04);
  694. k303c_acc_i2c_write(data, CTRL5_REG, 0x00);
  695. k303c_acc_i2c_write(data, CTRL6_REG, 0x00);
  696. msleep(80);
  697. k303c_acc_read_accel_xyz(data, &acc);
  698. for (i = 0; i < 5; i++) {
  699. while (1) {
  700. if (k303c_acc_i2c_read(data, STATUS_REG, &temp, 1)
  701. < 0) {
  702. pr_err("%s: i2c error", __func__);
  703. goto exit_status_err;
  704. }
  705. if (temp & 0x08)
  706. break;
  707. }
  708. k303c_acc_read_accel_xyz(data, &acc);
  709. NO_ST[0] += acc.x;
  710. NO_ST[1] += acc.y;
  711. NO_ST[2] += acc.z;
  712. }
  713. NO_ST[0] /= 5;
  714. NO_ST[1] /= 5;
  715. NO_ST[2] /= 5;
  716. k303c_acc_i2c_write(data, CTRL5_REG, 0x04);
  717. msleep(80);
  718. k303c_acc_read_accel_xyz(data, &acc);
  719. for (i = 0; i < 5; i++) {
  720. while (1) {
  721. if (k303c_acc_i2c_read(data, STATUS_REG, &temp, 1)
  722. < 0) {
  723. pr_err("%s: i2c error", __func__);
  724. goto exit_status_err;
  725. }
  726. if (temp & 0x08)
  727. break;
  728. }
  729. k303c_acc_read_accel_xyz(data, &acc);
  730. ST[0] += acc.x;
  731. ST[1] += acc.y;
  732. ST[2] += acc.z;
  733. }
  734. ST[0] /= 5;
  735. ST[1] /= 5;
  736. ST[2] /= 5;
  737. for (i = 0; i < 3; i++) {
  738. ST[i] -= NO_ST[i];
  739. ST[i] = abs(ST[i]);
  740. if ((SELF_TEST_2G_MIN_LSB > ST[i])
  741. || (ST[i] > SELF_TEST_2G_MAX_LSB)) {
  742. pr_info("%s: %d Out of range!! (%d)\n",
  743. __func__, i, ST[i]);
  744. result = 0;
  745. }
  746. }
  747. if (result)
  748. ret = snprintf(buf, 0xff, "1,%d,%d,%d\n", ST[0], ST[1], ST[2]);
  749. else
  750. ret = snprintf(buf, 0xff, "0,%d,%d,%d\n", ST[0], ST[1], ST[2]);
  751. goto exit;
  752. exit_status_err:
  753. ret = snprintf(buf, 0xff, "-1,0,0,0\n");
  754. exit:
  755. k303c_acc_i2c_write(data, CTRL1_REG, 0x00);
  756. k303c_acc_i2c_write(data, CTRL5_REG, 0x00);
  757. if (atomic_read(&data->enable) == OFF) {
  758. k303c_acc_set_mode(data, K303C_MODE_SUSPEND);
  759. } else {
  760. k303c_acc_set_mode(data, K303C_MODE_NORMAL);
  761. k303c_acc_set_enable(data, ON);
  762. }
  763. pr_info("%s %s\n", __func__, buf);
  764. return ret;
  765. }
  766. static DEVICE_ATTR(name, S_IRUGO, k303c_acc_name_show, NULL);
  767. static DEVICE_ATTR(vendor, S_IRUGO, k303c_acc_vendor_show, NULL);
  768. static DEVICE_ATTR(calibration, S_IRUGO | S_IWUSR | S_IWGRP,
  769. k303c_acc_calibration_show, k303c_acc_calibration_store);
  770. static DEVICE_ATTR(raw_data, S_IRUGO, k303c_acc_raw_data_read, NULL);
  771. static DEVICE_ATTR(reactive_alert, S_IRUGO | S_IWUSR | S_IWGRP,
  772. k303c_acc_reactive_alert_show, k303c_acc_reactive_alert_store);
  773. static DEVICE_ATTR(selftest, S_IRUGO, k303c_acc_selftest_show, NULL);
  774. static struct device_attribute *sensor_attrs[] = {
  775. &dev_attr_name,
  776. &dev_attr_vendor,
  777. &dev_attr_calibration,
  778. &dev_attr_raw_data,
  779. &dev_attr_reactive_alert,
  780. &dev_attr_selftest,
  781. NULL,
  782. };
  783. static enum hrtimer_restart k303c_acc_timer_func(struct hrtimer *timer)
  784. {
  785. struct k303c_acc_p *data = container_of(timer, struct k303c_acc_p,
  786. accel_timer);
  787. if (!work_pending(&data->work))
  788. queue_work(data->accel_wq, &data->work);
  789. hrtimer_forward_now(&data->accel_timer, data->poll_delay);
  790. return HRTIMER_RESTART;
  791. }
  792. static void k303c_acc_work_func(struct work_struct *work)
  793. {
  794. int ret;
  795. struct k303c_acc_v acc;
  796. struct k303c_acc_p *data = container_of(work, struct k303c_acc_p,
  797. work);
  798. ret = k303c_acc_read_accel_xyz(data, &acc);
  799. if (ret < 0)
  800. goto exit;
  801. data->accdata.x = acc.x - data->caldata.x;
  802. data->accdata.y = acc.y - data->caldata.y;
  803. data->accdata.z = acc.z - data->caldata.z;
  804. input_report_rel(data->input, REL_X, data->accdata.x);
  805. input_report_rel(data->input, REL_Y, data->accdata.y);
  806. input_report_rel(data->input, REL_Z, data->accdata.z);
  807. input_sync(data->input);
  808. exit:
  809. if ((ktime_to_ns(data->poll_delay) * (int64_t)data->time_count)
  810. >= ((int64_t)ACCEL_LOG_TIME * NSEC_PER_SEC)) {
  811. pr_info("%s - x = %d, y = %d, z = %d (ra:%d)\n", __func__,
  812. data->accdata.x, data->accdata.y, data->accdata.z,
  813. data->recog_flag);
  814. data->time_count = 0;
  815. } else
  816. data->time_count++;
  817. }
  818. static void k303c_acc_irq_work_func(struct work_struct *work)
  819. {
  820. struct k303c_acc_p *data = container_of((struct delayed_work *)work,
  821. struct k303c_acc_p, irq_work);
  822. unsigned char buf;
  823. pr_info("%s\n", __func__);
  824. k303c_acc_i2c_write(data, INT_CFG1_REG, 0x00);
  825. k303c_acc_i2c_read(data, INT_SRC1_REG, &buf, 1);
  826. }
  827. static irqreturn_t k303c_acc_irq_thread(int irq, void *k303c_acc_data_p)
  828. {
  829. struct k303c_acc_p *data = k303c_acc_data_p;
  830. pr_info("%s\n", __func__);
  831. data->irq_state = 1;
  832. wake_lock_timeout(&data->reactive_wake_lock, msecs_to_jiffies(2000));
  833. schedule_delayed_work(&data->irq_work, msecs_to_jiffies(100));
  834. return IRQ_HANDLED;
  835. }
  836. static int k303c_acc_parse_dt(struct k303c_acc_p *data, struct device *dev)
  837. {
  838. struct device_node *dev_node = dev->of_node;
  839. enum of_gpio_flags flags;
  840. int ret;
  841. u32 temp;
  842. if (dev_node == NULL)
  843. return -ENODEV;
  844. data->acc_int1 = of_get_named_gpio_flags(dev_node,
  845. "k303c_acc,gpio_int1", 0, &flags);
  846. if (data->acc_int1 < 0) {
  847. pr_err("%s - get acc_int1 error\n", __func__);
  848. return -ENODEV;
  849. }
  850. ret = of_property_read_u32(dev_node, "k303c_acc,axis_map_x", &temp);
  851. if ((data->axis_map_x > 2) || (ret < 0)) {
  852. pr_err("%s: invalid x axis_map value %u\n",
  853. __func__, data->axis_map_x);
  854. data->axis_map_x = 0;
  855. } else {
  856. data->axis_map_x = (u8)temp;
  857. }
  858. ret = of_property_read_u32(dev_node, "k303c_acc,axis_map_y", &temp);
  859. if ((data->axis_map_y > 2) || (ret < 0)) {
  860. pr_err("%s: invalid y axis_map value %u\n",
  861. __func__, data->axis_map_y);
  862. data->axis_map_y = 1;
  863. } else {
  864. data->axis_map_y = (u8)temp;
  865. }
  866. ret = of_property_read_u32(dev_node, "k303c_acc,axis_map_z", &temp);
  867. if ((data->axis_map_z > 2) || (ret < 0)) {
  868. pr_err("%s: invalid z axis_map value %u\n",
  869. __func__, data->axis_map_z);
  870. data->axis_map_z = 2;
  871. } else {
  872. data->axis_map_z = (u8)temp;
  873. }
  874. ret = of_property_read_u32(dev_node, "k303c_acc,negate_x", &temp);
  875. if ((data->negate_x > 1) || (ret < 0)) {
  876. pr_err("%s: invalid x axis_map value %u\n",
  877. __func__, data->negate_x);
  878. data->negate_x = 0;
  879. } else {
  880. data->negate_x = (u8)temp;
  881. }
  882. ret = of_property_read_u32(dev_node, "k303c_acc,negate_y", &temp);
  883. if ((data->negate_y > 1) || (ret < 0)) {
  884. pr_err("%s: invalid y axis_map value %u\n",
  885. __func__, data->negate_y);
  886. data->negate_y = 0;
  887. } else {
  888. data->negate_y = (u8)temp;
  889. }
  890. ret = of_property_read_u32(dev_node, "k303c_acc,negate_z", &temp);
  891. if ((data->negate_z > 1) || (ret < 0)) {
  892. pr_err("%s: invalid z axis_map value %u\n",
  893. __func__, data->negate_z);
  894. data->negate_z = 0;
  895. } else {
  896. data->negate_z = (u8)temp;
  897. }
  898. data->reg_vdd = devm_regulator_get(dev, "k303c_acc,vdd");
  899. if (IS_ERR(data->reg_vdd)) {
  900. pr_err("could not get vdd, %ld\n", PTR_ERR(data->reg_vdd));
  901. } else {
  902. if (!((AVG_VDD <= regulator_get_voltage(data->reg_vdd))
  903. && (regulator_get_voltage(data->reg_vdd) <= MAX_VDD)))
  904. ret = regulator_set_voltage(data->reg_vdd,
  905. AVG_VDD, AVG_VDD);
  906. devm_regulator_put(data->reg_vdd);
  907. if (ret) {
  908. pr_err("%s: set voltage failed on vdd, rc=%d\n",
  909. __func__, ret);
  910. return ret;
  911. }
  912. }
  913. data->reg_vio = devm_regulator_get(dev, "k303c_acc,vio");
  914. if (IS_ERR(data->reg_vio)) {
  915. pr_err("%s, could not get vio, %ld\n",
  916. __func__, PTR_ERR(data->reg_vio));
  917. } else {
  918. ret = regulator_set_voltage(data->reg_vio, AVG_VIO, AVG_VIO);
  919. if (ret) {
  920. pr_err("%s: set voltage failed on vio rc=%d\n",
  921. __func__, ret);
  922. return ret;
  923. }
  924. }
  925. return 0;
  926. }
  927. static int k303c_acc_probe(struct i2c_client *client,
  928. const struct i2c_device_id *id)
  929. {
  930. u8 temp;
  931. int ret = -ENODEV, i;
  932. struct k303c_acc_p *data = NULL;
  933. pr_info("[SENSOR] %s, Probe Start!\n", __func__);
  934. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  935. pr_err("%s - i2c_check_functionality error\n",
  936. __func__);
  937. goto err_i2c_check_functionality;
  938. }
  939. data = kzalloc(sizeof(struct k303c_acc_p), GFP_KERNEL);
  940. if (data == NULL) {
  941. pr_err("%s - kzalloc error\n", __func__);
  942. ret = -ENOMEM;
  943. goto err_kzalloc;
  944. }
  945. i2c_set_clientdata(client, data);
  946. data->client = client;
  947. ret = k303c_acc_parse_dt(data, &client->dev);
  948. if (ret < 0) {
  949. pr_err("%s - of_node error\n", __func__);
  950. ret = -ENODEV;
  951. goto err_of_node;
  952. }
  953. ret = k303c_regulator_onoff(data, true);
  954. if (ret < 0) {
  955. pr_err("%s - No regulator\n", __func__);
  956. goto err_regulator_onoff;
  957. }
  958. ret = gpio_request(data->acc_int1, "ACCEL_nINT");
  959. if (ret < 0) {
  960. pr_err("%s - gpio %d request failed (%d)\n",
  961. __func__, data->acc_int1, ret);
  962. goto err_setup_pin;
  963. }
  964. ret = gpio_direction_input(data->acc_int1);
  965. if (ret < 0) {
  966. pr_err("%s - failed to set gpio %d as input (%d)\n",
  967. __func__, data->acc_int1, ret);
  968. goto err_acc_int1;
  969. }
  970. wake_lock_init(&data->reactive_wake_lock, WAKE_LOCK_SUSPEND,
  971. "reactive_wake_lock");
  972. data->irq1 = gpio_to_irq(data->acc_int1);
  973. ret = request_threaded_irq(data->irq1, NULL, k303c_acc_irq_thread,
  974. IRQF_TRIGGER_RISING | IRQF_ONESHOT | IRQF_NO_SUSPEND,
  975. MODEL_NAME, data);
  976. if (ret < 0) {
  977. pr_err("%s - can't allocate irq.\n", __func__);
  978. goto err_reactive_irq;
  979. }
  980. disable_irq(data->irq1);
  981. mutex_init(&data->mode_mutex);
  982. k303c_acc_set_mode(data, K303C_MODE_NORMAL);
  983. for (i = 0; i < CHIP_ID_RETRIES; i++) {
  984. ret = k303c_acc_i2c_read(data, WHOAMI_REG, &temp, 1);
  985. if (temp != K303C_CHIP_ID) {
  986. pr_err("%s - chip id failed 0x%x : %d\n",
  987. __func__, temp, ret);
  988. } else {
  989. pr_info("%s - chip id success 0x%x\n",
  990. __func__, temp);
  991. break;
  992. }
  993. msleep(20);
  994. }
  995. if (i >= CHIP_ID_RETRIES) {
  996. ret = -ENODEV;
  997. goto err_read_chipid;
  998. }
  999. data->input = input_allocate_device();
  1000. if (!data->input) {
  1001. ret = -ENOMEM;
  1002. pr_err("%s failed input_allocate_device\n", __func__);
  1003. goto err_input_allocate_device;
  1004. }
  1005. data->input->name = MODULE_NAME;
  1006. data->input->id.bustype = BUS_I2C;
  1007. input_set_capability(data->input, EV_REL, REL_X);
  1008. input_set_capability(data->input, EV_REL, REL_Y);
  1009. input_set_capability(data->input, EV_REL, REL_Z);
  1010. input_set_drvdata(data->input, data);
  1011. ret = input_register_device(data->input);
  1012. if (ret < 0) {
  1013. pr_err("%s failed input_register_device\n", __func__);
  1014. goto err_input_register_device;
  1015. }
  1016. ret = sensors_create_symlink(&data->input->dev.kobj, data->input->name);
  1017. if (ret < 0) {
  1018. pr_err("%s failed sensors_create_symlink\n", __func__);
  1019. goto err_sensors_create_symlink;
  1020. }
  1021. ret = sysfs_create_group(&data->input->dev.kobj,
  1022. &k303c_acc_attribute_group);
  1023. if (ret < 0) {
  1024. pr_err("%s failed sysfs_create_group\n", __func__);
  1025. goto err_sysfs_create_group;
  1026. }
  1027. ret = sensors_register(data->factory_device, data, sensor_attrs,
  1028. MODULE_NAME);
  1029. if (ret < 0) {
  1030. pr_err("%s failed sensors_register\n", __func__);
  1031. goto err_sensors_register;
  1032. }
  1033. /* accel_timer settings. we poll for light values using a timer. */
  1034. hrtimer_init(&data->accel_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1035. data->poll_delay = ns_to_ktime(K303C_DEFAULT_DELAY);
  1036. data->accel_timer.function = k303c_acc_timer_func;
  1037. /* the timer just fires off a work queue request. we need a thread
  1038. to read the i2c (can be slow and blocking). */
  1039. data->accel_wq = create_singlethread_workqueue("accel_wq");
  1040. if (!data->accel_wq) {
  1041. ret = -ENOMEM;
  1042. pr_err("%s - could not create workqueue\n", __func__);
  1043. goto err_create_workqueue;
  1044. }
  1045. /* this is the thread function we run on the work queue */
  1046. INIT_WORK(&data->work, k303c_acc_work_func);
  1047. INIT_DELAYED_WORK(&data->irq_work, k303c_acc_irq_work_func);
  1048. atomic_set(&data->enable, OFF);
  1049. data->time_count = 0;
  1050. data->irq_state = 0;
  1051. data->recog_flag = OFF;
  1052. k303c_acc_set_range(data, K303C_RANGE_2G);
  1053. k303c_acc_set_mode(data, K303C_MODE_SUSPEND);
  1054. k303c_acc_i2c_write(data, CTRL2_REG, 0x20);
  1055. pr_err("[SENSOR] %s, Probe done!\n", __func__);
  1056. return 0;
  1057. cancel_delayed_work_sync(&data->irq_work);
  1058. err_create_workqueue:
  1059. sensors_unregister(data->factory_device, sensor_attrs);
  1060. err_sensors_register:
  1061. sysfs_remove_group(&data->input->dev.kobj, &k303c_acc_attribute_group);
  1062. err_sysfs_create_group:
  1063. sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
  1064. err_sensors_create_symlink:
  1065. input_unregister_device(data->input);
  1066. err_input_register_device:
  1067. input_free_device(data->input);
  1068. err_input_allocate_device:
  1069. err_read_chipid:
  1070. mutex_destroy(&data->mode_mutex);
  1071. free_irq(data->irq1, data);
  1072. err_reactive_irq:
  1073. wake_lock_destroy(&data->reactive_wake_lock);
  1074. err_acc_int1:
  1075. gpio_free(data->acc_int1);
  1076. err_setup_pin:
  1077. err_regulator_onoff:
  1078. err_of_node:
  1079. kfree(data);
  1080. err_kzalloc:
  1081. err_i2c_check_functionality:
  1082. pr_err("[SENSOR] %s, Probe fail!\n", __func__);
  1083. return ret;
  1084. }
  1085. static int k303c_acc_remove(struct i2c_client *client)
  1086. {
  1087. struct k303c_acc_p *data = (struct k303c_acc_p *)
  1088. i2c_get_clientdata(client);
  1089. pr_info("%s\n", __func__);
  1090. if (atomic_read(&data->enable) == ON) {
  1091. k303c_acc_set_mode(data, K303C_MODE_SUSPEND);
  1092. k303c_acc_set_enable(data, OFF);
  1093. atomic_set(&data->enable, OFF);
  1094. }
  1095. cancel_delayed_work_sync(&data->irq_work);
  1096. sensors_unregister(data->factory_device, sensor_attrs);
  1097. sysfs_remove_group(&data->input->dev.kobj, &k303c_acc_attribute_group);
  1098. sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
  1099. input_unregister_device(data->input);
  1100. input_free_device(data->input);
  1101. mutex_destroy(&data->mode_mutex);
  1102. free_irq(data->irq1, data);
  1103. wake_lock_destroy(&data->reactive_wake_lock);
  1104. gpio_free(data->acc_int1);
  1105. kfree(data);
  1106. return 0;
  1107. }
  1108. static int k303c_acc_suspend(struct device *dev)
  1109. {
  1110. struct k303c_acc_p *data = dev_get_drvdata(dev);
  1111. pr_info("%s en:%d, ra:%d\n", __func__, atomic_read(&data->enable),
  1112. data->recog_flag);
  1113. if (atomic_read(&data->enable) == ON) {
  1114. k303c_acc_set_mode(data, K303C_MODE_SUSPEND);
  1115. k303c_acc_set_enable(data, OFF);
  1116. }
  1117. return 0;
  1118. }
  1119. static int k303c_acc_resume(struct device *dev)
  1120. {
  1121. struct k303c_acc_p *data = dev_get_drvdata(dev);
  1122. pr_info("%s en:%d, ra:%d\n", __func__, atomic_read(&data->enable),
  1123. data->recog_flag);
  1124. if (atomic_read(&data->enable) == ON) {
  1125. k303c_acc_set_mode(data, K303C_MODE_NORMAL);
  1126. k303c_acc_set_enable(data, ON);
  1127. }
  1128. return 0;
  1129. }
  1130. static struct of_device_id k303c_acc_table[] = {
  1131. { .compatible = "k303c_acc",},
  1132. {},
  1133. };
  1134. static const struct i2c_device_id k303c_acc_id[] = {
  1135. { "k303c_acc_table", 0 },
  1136. { }
  1137. };
  1138. static const struct dev_pm_ops k303c_acc_pm_ops = {
  1139. .suspend = k303c_acc_suspend,
  1140. .resume = k303c_acc_resume
  1141. };
  1142. static struct i2c_driver k303c_acc_driver = {
  1143. .driver = {
  1144. .name = MODEL_NAME,
  1145. .owner = THIS_MODULE,
  1146. .of_match_table = k303c_acc_table,
  1147. .pm = &k303c_acc_pm_ops
  1148. },
  1149. .probe = k303c_acc_probe,
  1150. .remove = k303c_acc_remove,
  1151. .id_table = k303c_acc_id,
  1152. };
  1153. static int __init k303c_acc_init(void)
  1154. {
  1155. return i2c_add_driver(&k303c_acc_driver);
  1156. }
  1157. static void __exit k303c_acc_exit(void)
  1158. {
  1159. i2c_del_driver(&k303c_acc_driver);
  1160. }
  1161. module_init(k303c_acc_init);
  1162. module_exit(k303c_acc_exit);
  1163. MODULE_DESCRIPTION("K303C accelerometer sensor driver");
  1164. MODULE_AUTHOR("Samsung Electronics");
  1165. MODULE_LICENSE("GPL");