k3dh.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /*
  2. * STMicroelectronics k3dh acceleration sensor driver
  3. *
  4. * Copyright (C) 2010 Samsung Electronics Co.Ltd
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/fs.h>
  19. #include <linux/slab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/k3dh.h>
  24. #include "k3dh_reg.h"
  25. #include <linux/of_gpio.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/delay.h>
  28. #ifdef CONFIG_SENSOR_K3DH_INPUTDEV
  29. #include <linux/input.h>
  30. #endif
  31. #include "sensors_core.h"
  32. #define MODULE_NAME "accelerometer_sensor"
  33. #define VREG_LVS1_1P8_MIN 1800000
  34. #define VREG_LVS1_1P8_MAX 1800000
  35. /* For Debugging */
  36. #if 1
  37. #define k3dh_dbgmsg(str, args...) pr_debug("%s: " str, __func__, ##args)
  38. #endif
  39. #define k3dh_infomsg(str, args...) pr_info("%s: " str, __func__, ##args)
  40. #define VENDOR "STM"
  41. #define CHIP_ID "K3DH"
  42. /* The default settings when sensor is on is for all 3 axis to be enabled
  43. * and output data rate set to 400Hz. Output is via a ioctl read call.
  44. */
  45. #define DEFAULT_POWER_ON_SETTING (ODR400 | ENABLE_ALL_AXES)
  46. #define ACC_DEV_MAJOR 241
  47. #define CALIBRATION_FILE_PATH "/efs/calibration_data"
  48. #define CAL_DATA_AMOUNT 20
  49. #ifdef CONFIG_SENSOR_K3DH_INPUTDEV
  50. /* ABS axes parameter range [um/s^2] (for input event) */
  51. #define GRAVITY_EARTH 9806550
  52. #define ABSMAX_2G (GRAVITY_EARTH * 2)
  53. #define ABSMIN_2G (-GRAVITY_EARTH * 2)
  54. #define MIN_DELAY 5
  55. #define MAX_DELAY 200
  56. #endif
  57. static const struct odr_delay {
  58. u8 odr; /* odr reg setting */
  59. s64 delay_ns; /* odr in ns */
  60. } odr_delay_table[] = {
  61. { ODR1344, 744047LL }, /* 1344Hz */
  62. { ODR400, 2500000LL }, /* 400Hz */
  63. { ODR200, 5000000LL }, /* 200Hz */
  64. { ODR100, 10000000LL }, /* 100Hz */
  65. { ODR50, 20000000LL }, /* 50Hz */
  66. { ODR25, 40000000LL }, /* 25Hz */
  67. { ODR10, 100000000LL }, /* 10Hz */
  68. { ODR1, 1000000000LL }, /* 1Hz */
  69. };
  70. /* K3DH acceleration data */
  71. struct k3dh_acc {
  72. s16 x;
  73. s16 y;
  74. s16 z;
  75. };
  76. struct k3dh_data {
  77. struct i2c_client *client;
  78. struct miscdevice k3dh_device;
  79. struct mutex read_lock;
  80. struct mutex write_lock;
  81. struct completion data_ready;
  82. #if defined(CONFIG_MACH_U1) || defined(CONFIG_MACH_TRATS)
  83. struct class *acc_class;
  84. #else
  85. struct device *dev;
  86. #endif
  87. struct k3dh_acc cal_data;
  88. struct k3dh_acc acc_xyz;
  89. u8 ctrl_reg1_shadow;
  90. atomic_t opened; /* opened implies enabled */
  91. #ifdef CONFIG_SENSOR_K3DH_INPUTDEV
  92. struct input_dev *input;
  93. struct delayed_work work;
  94. atomic_t delay;
  95. atomic_t enable;
  96. #endif
  97. bool axis_adjust;
  98. int position;
  99. struct accel_platform_data_k2dh *pdata;
  100. struct regulator *l19;
  101. struct regulator *lvs1_1p8;
  102. };
  103. static struct k3dh_data *g_k3dh;
  104. static void k3dh_xyz_position_adjust(struct k3dh_acc *acc,
  105. int position)
  106. {
  107. const int position_map[][3][3] = {
  108. {{ 0, 1, 0}, {-1, 0, 0}, { 0, 0, 1} }, /* 0 top/upper-left */
  109. {{-1, 0, 0}, { 0, -1, 0}, { 0, 0, 1} }, /* 1 top/upper-right */
  110. {{ 0, -1, 0}, { 1, 0, 0}, { 0, 0, 1} }, /* 2 top/lower-right */
  111. {{ 1, 0, 0}, { 0, 1, 0}, { 0, 0, 1} }, /* 3 top/lower-left */
  112. {{ 0, -1, 0}, {-1, 0, 0}, { 0, 0, -1} }, /* 4 bottom/upper-left */
  113. {{ 1, 0, 0}, { 0, -1, 0}, { 0, 0, -1} }, /* 5 bottom/upper-right */
  114. {{ 0, 1, 0}, { 1, 0, 0}, { 0, 0, -1} }, /* 6 bottom/lower-right */
  115. {{-1, 0, 0}, { 0, 1, 0}, { 0, 0, -1} }, /* 7 bottom/lower-left*/
  116. };
  117. struct k3dh_acc xyz_adjusted = {0,};
  118. s16 raw[3] = {0,};
  119. int j;
  120. raw[0] = acc->x;
  121. raw[1] = acc->y;
  122. raw[2] = acc->z;
  123. for (j = 0; j < 3; j++) {
  124. xyz_adjusted.x +=
  125. (position_map[position][0][j] * raw[j]);
  126. xyz_adjusted.y +=
  127. (position_map[position][1][j] * raw[j]);
  128. xyz_adjusted.z +=
  129. (position_map[position][2][j] * raw[j]);
  130. }
  131. acc->x = xyz_adjusted.x;
  132. acc->y = xyz_adjusted.y;
  133. acc->z = xyz_adjusted.z;
  134. }
  135. int k3dh_power_on(struct k3dh_data *data, bool onoff)
  136. {
  137. int ret = -1;
  138. if (!data->l19) {
  139. data->l19 = regulator_get(&data->client->dev, "8226_l19");
  140. if (!data->l19) {
  141. pr_err("%s: regulator pointer null l19, rc=%d\n",
  142. __func__, ret);
  143. return ret;
  144. }
  145. ret = regulator_set_voltage(data->l19, 2850000, 2850000);
  146. if (ret) {
  147. pr_err("%s: set voltage failed on l19, rc=%d\n",
  148. __func__, ret);
  149. return ret;
  150. }
  151. }
  152. if (!data->lvs1_1p8) {
  153. data->lvs1_1p8 = regulator_get(&data->client->dev, "8226_lvs1");
  154. if(!data->lvs1_1p8){
  155. pr_err("%s: regulator_get for 8226_lvs1 failed\n", __func__);
  156. return 0;
  157. }
  158. }
  159. if(onoff){
  160. ret = regulator_enable(data->l19);
  161. if (ret) {
  162. pr_err("%s: Failed to enable regulator l19.\n",
  163. __func__);
  164. return ret;
  165. }
  166. ret = regulator_enable(data->lvs1_1p8);
  167. if (ret) {
  168. pr_err("%s: Failed to enable regulator lvs1_1p8.\n",
  169. __func__);
  170. return ret;
  171. }
  172. }
  173. else {
  174. ret = regulator_disable(data->l19);
  175. if (ret) {
  176. pr_err("%s: Failed to disable regulatorl19.\n",
  177. __func__);
  178. return ret;
  179. }
  180. ret = regulator_enable(data->lvs1_1p8);
  181. if (ret) {
  182. pr_err("%s: Failed to disable regulator lvs1_1p8.\n",
  183. __func__);
  184. return ret;
  185. }
  186. }
  187. return 0;
  188. }
  189. /* Read X,Y and Z-axis acceleration raw data */
  190. static int k3dh_read_accel_raw_xyz(struct k3dh_data *data,
  191. struct k3dh_acc *acc)
  192. {
  193. int err;
  194. s8 reg = OUT_X_L | AC; /* read from OUT_X_L to OUT_Z_H by auto-inc */
  195. u8 acc_data[6];
  196. err = i2c_smbus_read_i2c_block_data(data->client, reg,
  197. sizeof(acc_data), acc_data);
  198. if (err != sizeof(acc_data)) {
  199. pr_err("%s : failed to read 6 bytes for getting x/y/z\n",
  200. __func__);
  201. return -EIO;
  202. }
  203. acc->x = (acc_data[1] << 8) | acc_data[0];
  204. acc->y = (acc_data[3] << 8) | acc_data[2];
  205. acc->z = (acc_data[5] << 8) | acc_data[4];
  206. acc->x = acc->x >> 4;
  207. acc->y = acc->y >> 4;
  208. #if defined(CONFIG_MACH_U1_NA_SPR_REV05) \
  209. || defined(CONFIG_MACH_U1_NA_SPR_EPIC2_REV00) \
  210. || defined(CONFIG_MACH_U1_NA_USCC_REV05) \
  211. || defined(CONFIG_MACH_Q1_BD) \
  212. || defined(CONFIG_MACH_U1_NA_USCC) \
  213. || defined(CONFIG_MACH_U1_NA_SPR)
  214. acc->z = -acc->z >> 4;
  215. #else
  216. acc->z = acc->z >> 4;
  217. #endif
  218. if (data->axis_adjust)
  219. k3dh_xyz_position_adjust(acc, data->position);
  220. return 0;
  221. }
  222. static int k3dh_read_accel_xyz(struct k3dh_data *data,
  223. struct k3dh_acc *acc)
  224. {
  225. int err = 0;
  226. mutex_lock(&data->read_lock);
  227. err = k3dh_read_accel_raw_xyz(data, acc);
  228. mutex_unlock(&data->read_lock);
  229. if (err < 0) {
  230. pr_err("%s: k3dh_read_accel_raw_xyz() failed\n", __func__);
  231. return err;
  232. }
  233. acc->x -= data->cal_data.x;
  234. acc->y -= data->cal_data.y;
  235. acc->z -= data->cal_data.z;
  236. return err;
  237. }
  238. static int k3dh_open_calibration(struct k3dh_data *data)
  239. {
  240. struct file *cal_filp = NULL;
  241. int err = 0;
  242. mm_segment_t old_fs;
  243. old_fs = get_fs();
  244. set_fs(KERNEL_DS);
  245. cal_filp = filp_open(CALIBRATION_FILE_PATH, O_RDONLY, 0666);
  246. if (IS_ERR(cal_filp)) {
  247. err = PTR_ERR(cal_filp);
  248. if (err != -ENOENT)
  249. pr_err("%s: Can't open calibration file\n", __func__);
  250. set_fs(old_fs);
  251. return err;
  252. }
  253. err = cal_filp->f_op->read(cal_filp,
  254. (char *)&data->cal_data, 3 * sizeof(s16), &cal_filp->f_pos);
  255. if (err != 3 * sizeof(s16)) {
  256. pr_err("%s: Can't read the cal data from file\n", __func__);
  257. err = -EIO;
  258. }
  259. k3dh_dbgmsg("%s: (%u,%u,%u)\n", __func__,
  260. data->cal_data.x, data->cal_data.y, data->cal_data.z);
  261. filp_close(cal_filp, current->files);
  262. set_fs(old_fs);
  263. return err;
  264. }
  265. static int k3dh_do_calibrate(struct device *dev, bool do_calib)
  266. {
  267. struct k3dh_data *acc_data = dev_get_drvdata(dev);
  268. struct k3dh_acc data = { 0, };
  269. struct file *cal_filp = NULL;
  270. int sum[3] = { 0, };
  271. int err = 0;
  272. int i;
  273. mm_segment_t old_fs;
  274. if (do_calib) {
  275. for (i = 0; i < CAL_DATA_AMOUNT; i++) {
  276. mutex_lock(&acc_data->read_lock);
  277. err = k3dh_read_accel_raw_xyz(acc_data, &data);
  278. mutex_unlock(&acc_data->read_lock);
  279. if (err < 0) {
  280. pr_err("%s: k3dh_read_accel_raw_xyz() "
  281. "failed in the %dth loop\n",
  282. __func__, i);
  283. return err;
  284. }
  285. sum[0] += data.x;
  286. sum[1] += data.y;
  287. sum[2] += data.z;
  288. }
  289. acc_data->cal_data.x = sum[0] / CAL_DATA_AMOUNT;
  290. acc_data->cal_data.y = sum[1] / CAL_DATA_AMOUNT;
  291. if (sum[2] >= 0)
  292. acc_data->cal_data.z = (sum[2] / CAL_DATA_AMOUNT)-1024;
  293. else
  294. acc_data->cal_data.z = (sum[2] / CAL_DATA_AMOUNT)+1024;
  295. } else {
  296. acc_data->cal_data.x = 0;
  297. acc_data->cal_data.y = 0;
  298. acc_data->cal_data.z = 0;
  299. }
  300. printk(KERN_INFO "%s: cal data (%d,%d,%d)\n", __func__,
  301. acc_data->cal_data.x, acc_data->cal_data.y, acc_data->cal_data.z);
  302. old_fs = get_fs();
  303. set_fs(KERNEL_DS);
  304. cal_filp = filp_open(CALIBRATION_FILE_PATH,
  305. O_CREAT | O_TRUNC | O_WRONLY, 0666);
  306. if (IS_ERR(cal_filp)) {
  307. pr_err("%s: Can't open calibration file\n", __func__);
  308. set_fs(old_fs);
  309. err = PTR_ERR(cal_filp);
  310. return err;
  311. }
  312. err = cal_filp->f_op->write(cal_filp,
  313. (char *)&acc_data->cal_data, 3 * sizeof(s16), &cal_filp->f_pos);
  314. if (err != 3 * sizeof(s16)) {
  315. pr_err("%s: Can't write the cal data to file\n", __func__);
  316. err = -EIO;
  317. }
  318. filp_close(cal_filp, current->files);
  319. set_fs(old_fs);
  320. return err;
  321. }
  322. static int k3dh_accel_enable(struct k3dh_data *data)
  323. {
  324. int err = 0;
  325. if (atomic_read(&data->opened) == 0) {
  326. err = k3dh_open_calibration(data);
  327. if (err < 0 && err != -ENOENT)
  328. pr_err("%s: k3dh_open_calibration() failed\n",
  329. __func__);
  330. data->ctrl_reg1_shadow = DEFAULT_POWER_ON_SETTING;
  331. err = i2c_smbus_write_byte_data(data->client, CTRL_REG1,
  332. DEFAULT_POWER_ON_SETTING);
  333. if (err)
  334. pr_err("%s: i2c write ctrl_reg1 failed\n", __func__);
  335. #if defined(CONFIG_SENSORS_K2DH)
  336. err = i2c_smbus_write_byte_data(data->client, CTRL_REG4,
  337. CTRL_REG4_HR | CTRL_REG4_BDU);
  338. #else
  339. err = i2c_smbus_write_byte_data(data->client, CTRL_REG4,
  340. CTRL_REG4_HR);
  341. #endif
  342. if (err)
  343. pr_err("%s: i2c write ctrl_reg4 failed\n", __func__);
  344. }
  345. atomic_add(1, &data->opened);
  346. return err;
  347. }
  348. static int k3dh_accel_disable(struct k3dh_data *data)
  349. {
  350. int err = 0;
  351. atomic_sub(1, &data->opened);
  352. if (atomic_read(&data->opened) == 0) {
  353. err = i2c_smbus_write_byte_data(data->client, CTRL_REG1,
  354. PM_OFF);
  355. data->ctrl_reg1_shadow = PM_OFF;
  356. }
  357. return err;
  358. }
  359. /* open command for K3DH device file */
  360. static int k3dh_open(struct inode *inode, struct file *file)
  361. {
  362. k3dh_infomsg("is called.\n");
  363. return 0;
  364. }
  365. /* release command for K3DH device file */
  366. static int k3dh_close(struct inode *inode, struct file *file)
  367. {
  368. k3dh_infomsg("is called.\n");
  369. return 0;
  370. }
  371. static s64 k3dh_get_delay(struct k3dh_data *data)
  372. {
  373. int i;
  374. u8 odr;
  375. s64 delay = -1;
  376. odr = data->ctrl_reg1_shadow & ODR_MASK;
  377. for (i = 0; i < ARRAY_SIZE(odr_delay_table); i++) {
  378. if (odr == odr_delay_table[i].odr) {
  379. delay = odr_delay_table[i].delay_ns;
  380. break;
  381. }
  382. }
  383. return delay;
  384. }
  385. static int k3dh_set_delay(struct k3dh_data *data, s64 delay_ns)
  386. {
  387. int odr_value = ODR1;
  388. int res = 0;
  389. int i;
  390. /* round to the nearest delay that is less than
  391. * the requested value (next highest freq)
  392. */
  393. for (i = 0; i < ARRAY_SIZE(odr_delay_table); i++) {
  394. if (delay_ns < odr_delay_table[i].delay_ns)
  395. break;
  396. }
  397. if (i > 0)
  398. i--;
  399. odr_value = odr_delay_table[i].odr;
  400. delay_ns = odr_delay_table[i].delay_ns;
  401. k3dh_infomsg("old=%lldns, new=%lldns, odr=0x%x/0x%x\n",
  402. k3dh_get_delay(data), delay_ns, odr_value,
  403. data->ctrl_reg1_shadow);
  404. mutex_lock(&data->write_lock);
  405. if (odr_value != (data->ctrl_reg1_shadow & ODR_MASK)) {
  406. u8 ctrl = (data->ctrl_reg1_shadow & ~ODR_MASK);
  407. ctrl |= odr_value;
  408. data->ctrl_reg1_shadow = ctrl;
  409. res = i2c_smbus_write_byte_data(data->client, CTRL_REG1, ctrl);
  410. }
  411. mutex_unlock(&data->write_lock);
  412. return res;
  413. }
  414. /* ioctl command for K3DH device file */
  415. static long k3dh_ioctl(struct file *file,
  416. unsigned int cmd, unsigned long arg)
  417. {
  418. int err = 0;
  419. struct k3dh_data *data = container_of(file->private_data,
  420. struct k3dh_data, k3dh_device);
  421. s64 delay_ns;
  422. int enable = 0;
  423. /* cmd mapping */
  424. switch (cmd) {
  425. case K3DH_IOCTL_SET_ENABLE:
  426. if (copy_from_user(&enable, (void __user *)arg,
  427. sizeof(enable)))
  428. return -EFAULT;
  429. k3dh_infomsg("opened = %d, enable = %d\n",
  430. atomic_read(&data->opened), enable);
  431. if (enable)
  432. err = k3dh_accel_enable(data);
  433. else
  434. err = k3dh_accel_disable(data);
  435. break;
  436. case K3DH_IOCTL_SET_DELAY:
  437. if (copy_from_user(&delay_ns, (void __user *)arg,
  438. sizeof(delay_ns)))
  439. return -EFAULT;
  440. err = k3dh_set_delay(data, delay_ns);
  441. break;
  442. case K3DH_IOCTL_GET_DELAY:
  443. delay_ns = k3dh_get_delay(data);
  444. if (put_user(delay_ns, (s64 __user *)arg))
  445. return -EFAULT;
  446. break;
  447. case K3DH_IOCTL_READ_ACCEL_XYZ:
  448. err = k3dh_read_accel_xyz(data, &data->acc_xyz);
  449. if (err)
  450. break;
  451. if (copy_to_user((void __user *)arg,
  452. &data->acc_xyz, sizeof(data->acc_xyz)))
  453. return -EFAULT;
  454. break;
  455. default:
  456. err = -EINVAL;
  457. break;
  458. }
  459. return err;
  460. }
  461. static int k3dh_suspend(struct device *dev)
  462. {
  463. int res = 0;
  464. struct k3dh_data *data = dev_get_drvdata(dev);
  465. #ifdef CONFIG_SENSOR_K3DH_INPUTDEV
  466. if (atomic_read(&data->enable))
  467. cancel_delayed_work_sync(&data->work);
  468. #endif
  469. if (atomic_read(&data->opened) > 0)
  470. res = i2c_smbus_write_byte_data(data->client,
  471. CTRL_REG1, PM_OFF);
  472. return res;
  473. }
  474. static int k3dh_resume(struct device *dev)
  475. {
  476. int res = 0;
  477. struct k3dh_data *data = dev_get_drvdata(dev);
  478. if (atomic_read(&data->opened) > 0)
  479. res = i2c_smbus_write_byte_data(data->client, CTRL_REG1,
  480. data->ctrl_reg1_shadow);
  481. #ifdef CONFIG_SENSOR_K3DH_INPUTDEV
  482. if (atomic_read(&data->enable))
  483. schedule_delayed_work(&data->work,
  484. msecs_to_jiffies(5));
  485. #endif
  486. return res;
  487. }
  488. static const struct dev_pm_ops k3dh_pm_ops = {
  489. .suspend = k3dh_suspend,
  490. .resume = k3dh_resume,
  491. };
  492. static const struct file_operations k3dh_fops = {
  493. .owner = THIS_MODULE,
  494. .open = k3dh_open,
  495. .release = k3dh_close,
  496. .unlocked_ioctl = k3dh_ioctl,
  497. };
  498. #ifdef CONFIG_SENSOR_K3DH_INPUTDEV
  499. static ssize_t k3dh_enable_show(struct device *dev,
  500. struct device_attribute *attr, char *buf)
  501. {
  502. struct input_dev *input = to_input_dev(dev);
  503. struct k3dh_data *data = input_get_drvdata(input);
  504. return sprintf(buf, "%d\n", atomic_read(&data->enable));
  505. }
  506. static ssize_t k3dh_enable_store(struct device *dev,
  507. struct device_attribute *attr,
  508. const char *buf, size_t count)
  509. {
  510. struct input_dev *input = to_input_dev(dev);
  511. struct k3dh_data *data = input_get_drvdata(input);
  512. unsigned long enable = 0;
  513. int err;
  514. if (strict_strtoul(buf, 10, &enable))
  515. return -EINVAL;
  516. k3dh_open_calibration(data);
  517. if (enable) {
  518. err = k3dh_accel_enable(data);
  519. if (err < 0)
  520. goto done;
  521. schedule_delayed_work(&data->work,
  522. msecs_to_jiffies(5));
  523. } else {
  524. cancel_delayed_work_sync(&data->work);
  525. err = k3dh_accel_disable(data);
  526. if (err < 0)
  527. goto done;
  528. }
  529. atomic_set(&data->enable, enable);
  530. pr_info("%s, enable = %ld\n", __func__, enable);
  531. done:
  532. return count;
  533. }
  534. static DEVICE_ATTR(enable,
  535. S_IRUGO | S_IWUSR | S_IWGRP,
  536. k3dh_enable_show, k3dh_enable_store);
  537. static ssize_t k3dh_delay_show(struct device *dev,
  538. struct device_attribute *attr, char *buf)
  539. {
  540. struct input_dev *input = to_input_dev(dev);
  541. struct k3dh_data *data = input_get_drvdata(input);
  542. return sprintf(buf, "%d\n", atomic_read(&data->delay));
  543. }
  544. static ssize_t k3dh_delay_store(struct device *dev,
  545. struct device_attribute *attr,
  546. const char *buf, size_t count)
  547. {
  548. struct input_dev *input = to_input_dev(dev);
  549. struct k3dh_data *data = input_get_drvdata(input);
  550. unsigned long delay = 0;
  551. if (strict_strtoul(buf, 10, &delay))
  552. return -EINVAL;
  553. if (delay > MAX_DELAY)
  554. delay = MAX_DELAY;
  555. if (delay < MIN_DELAY)
  556. delay = MIN_DELAY;
  557. atomic_set(&data->delay, delay);
  558. k3dh_set_delay(data, delay * 1000000);
  559. pr_info("%s, delay = %ld\n", __func__, delay);
  560. return count;
  561. }
  562. static DEVICE_ATTR(poll_delay,
  563. S_IRUGO | S_IWUSR | S_IWGRP,
  564. k3dh_delay_show, k3dh_delay_store);
  565. static struct attribute *k3dh_attributes[] = {
  566. &dev_attr_enable.attr,
  567. &dev_attr_poll_delay.attr,
  568. NULL
  569. };
  570. static struct attribute_group k3dh_attribute_group = {
  571. .attrs = k3dh_attributes
  572. };
  573. #endif
  574. static ssize_t k3dh_fs_read(struct device *dev,
  575. struct device_attribute *attr, char *buf)
  576. {
  577. struct k3dh_data *data = dev_get_drvdata(dev);
  578. #if defined(CONFIG_MACH_U1) || defined(CONFIG_MACH_TRATS)
  579. int err = 0;
  580. int on;
  581. mutex_lock(&data->write_lock);
  582. on = atomic_read(&data->opened);
  583. if (on == 0) {
  584. err = i2c_smbus_write_byte_data(data->client, CTRL_REG1,
  585. DEFAULT_POWER_ON_SETTING);
  586. }
  587. mutex_unlock(&data->write_lock);
  588. if (err < 0) {
  589. pr_err("%s: i2c write ctrl_reg1 failed\n", __func__);
  590. return err;
  591. }
  592. err = k3dh_read_accel_xyz(data, &data->acc_xyz);
  593. if (err < 0) {
  594. pr_err("%s: k3dh_read_accel_xyz failed\n", __func__);
  595. return err;
  596. }
  597. if (on == 0) {
  598. mutex_lock(&data->write_lock);
  599. err = i2c_smbus_write_byte_data(data->client, CTRL_REG1,
  600. PM_OFF);
  601. mutex_unlock(&data->write_lock);
  602. if (err)
  603. pr_err("%s: i2c write ctrl_reg1 failed\n", __func__);
  604. }
  605. #endif
  606. return sprintf(buf, "%d,%d,%d\n",
  607. data->acc_xyz.x, data->acc_xyz.y, data->acc_xyz.z);
  608. }
  609. static ssize_t k3dh_calibration_show(struct device *dev,
  610. struct device_attribute *attr,
  611. char *buf)
  612. {
  613. int err;
  614. struct k3dh_data *data = dev_get_drvdata(dev);
  615. err = k3dh_open_calibration(data);
  616. if (err < 0)
  617. pr_err("%s: k3dh_open_calibration() failed\n", __func__);
  618. if (!data->cal_data.x && !data->cal_data.y && !data->cal_data.z)
  619. err = -1;
  620. return sprintf(buf, "%d %d %d %d\n",
  621. err, data->cal_data.x, data->cal_data.y, data->cal_data.z);
  622. }
  623. static ssize_t k3dh_calibration_store(struct device *dev,
  624. struct device_attribute *attr,
  625. const char *buf, size_t count)
  626. {
  627. struct k3dh_data *data = dev_get_drvdata(dev);
  628. bool do_calib;
  629. int err;
  630. if (sysfs_streq(buf, "1"))
  631. do_calib = true;
  632. else if (sysfs_streq(buf, "0"))
  633. do_calib = false;
  634. else {
  635. pr_debug("%s: invalid value %d\n", __func__, *buf);
  636. return -EINVAL;
  637. }
  638. if (atomic_read(&data->opened) == 0) {
  639. /* if off, turn on the device.*/
  640. err = i2c_smbus_write_byte_data(data->client, CTRL_REG1,
  641. DEFAULT_POWER_ON_SETTING);
  642. if (err) {
  643. pr_err("%s: i2c write ctrl_reg1 failed(err=%d)\n",
  644. __func__, err);
  645. }
  646. }
  647. err = k3dh_do_calibrate(dev, do_calib);
  648. if (err < 0) {
  649. pr_err("%s: k3dh_do_calibrate() failed\n", __func__);
  650. return err;
  651. }
  652. if (atomic_read(&data->opened) == 0) {
  653. /* if off, turn on the device.*/
  654. err = i2c_smbus_write_byte_data(data->client, CTRL_REG1,
  655. PM_OFF);
  656. if (err) {
  657. pr_err("%s: i2c write ctrl_reg1 failed(err=%d)\n",
  658. __func__, err);
  659. }
  660. }
  661. return count;
  662. }
  663. #if defined(CONFIG_MACH_U1) || defined(CONFIG_MACH_TRATS)
  664. static DEVICE_ATTR(acc_file, 0664, k3dh_fs_read, NULL);
  665. #else
  666. static ssize_t
  667. k3dh_accel_position_show(struct device *dev,
  668. struct device_attribute *attr,
  669. char *buf)
  670. {
  671. struct k3dh_data *data = dev_get_drvdata(dev);
  672. return sprintf(buf, "%d\n", data->position);
  673. }
  674. static ssize_t
  675. k3dh_accel_position_store(struct device *dev,
  676. struct device_attribute *attr,
  677. const char *buf,
  678. size_t count)
  679. {
  680. struct k3dh_data *data = dev_get_drvdata(dev);
  681. int err = 0;
  682. err = kstrtoint(buf, 10, &data->position);
  683. if (err < 0)
  684. pr_err("%s, kstrtoint failed.", __func__);
  685. return count;
  686. }
  687. static ssize_t k3dh_accel_vendor_show(struct device *dev,
  688. struct device_attribute *attr, char *buf)
  689. {
  690. return sprintf(buf, "%s\n", VENDOR);
  691. }
  692. static ssize_t k3dh_accel_name_show(struct device *dev,
  693. struct device_attribute *attr, char *buf)
  694. {
  695. return sprintf(buf, "%s\n", CHIP_ID);
  696. }
  697. static DEVICE_ATTR(name, 0664, k3dh_accel_name_show, NULL);
  698. static DEVICE_ATTR(vendor, 0664, k3dh_accel_vendor_show, NULL);
  699. static DEVICE_ATTR(raw_data, 0664, k3dh_fs_read, NULL);
  700. static DEVICE_ATTR(position, 0664,
  701. k3dh_accel_position_show, k3dh_accel_position_store);
  702. #endif
  703. static DEVICE_ATTR(calibration, 0664,
  704. k3dh_calibration_show, k3dh_calibration_store);
  705. static struct device_attribute *sensor_attrs[] = {
  706. &dev_attr_name,
  707. &dev_attr_vendor,
  708. &dev_attr_calibration,
  709. &dev_attr_position,
  710. &dev_attr_raw_data,
  711. NULL,
  712. };
  713. void k3dh_shutdown(struct i2c_client *client)
  714. {
  715. int res = 0;
  716. struct k3dh_data *data = i2c_get_clientdata(client);
  717. k3dh_infomsg("is called.\n");
  718. #ifdef CONFIG_SENSOR_K3DH_INPUTDEV
  719. if (atomic_read(&data->enable))
  720. cancel_delayed_work_sync(&data->work);
  721. #endif
  722. res = i2c_smbus_write_byte_data(data->client,
  723. CTRL_REG1, PM_OFF);
  724. if (res < 0)
  725. pr_err("%s: pm_off failed %d\n", __func__, res);
  726. }
  727. #ifdef CONFIG_SENSOR_K3DH_INPUTDEV
  728. static void k3dh_work_func(struct work_struct *work)
  729. {
  730. k3dh_read_accel_xyz(g_k3dh, &g_k3dh->acc_xyz);
  731. pr_debug("%s: x: %d, y: %d, z: %d\n", __func__,
  732. g_k3dh->acc_xyz.x, g_k3dh->acc_xyz.y, g_k3dh->acc_xyz.z);
  733. input_report_abs(g_k3dh->input, ABS_X, g_k3dh->acc_xyz.x);
  734. input_report_abs(g_k3dh->input, ABS_Y, g_k3dh->acc_xyz.y);
  735. input_report_abs(g_k3dh->input, ABS_Z, g_k3dh->acc_xyz.z);
  736. input_sync(g_k3dh->input);
  737. schedule_delayed_work(&g_k3dh->work, msecs_to_jiffies(
  738. atomic_read(&g_k3dh->delay)));
  739. }
  740. /* ----------------- *
  741. Input device interface
  742. * ------------------ */
  743. static int k3dh_input_init(struct k3dh_data *data)
  744. {
  745. struct input_dev *dev;
  746. int err = 0;
  747. dev = input_allocate_device();
  748. if (!dev)
  749. return -ENOMEM;
  750. dev->name = MODULE_NAME;
  751. dev->id.bustype = BUS_I2C;
  752. input_set_capability(dev, EV_ABS, ABS_MISC);
  753. input_set_abs_params(dev, ABS_X, ABSMIN_2G, ABSMAX_2G, 0, 0);
  754. input_set_abs_params(dev, ABS_Y, ABSMIN_2G, ABSMAX_2G, 0, 0);
  755. input_set_abs_params(dev, ABS_Z, ABSMIN_2G, ABSMAX_2G, 0, 0);
  756. input_set_drvdata(dev, data);
  757. err = input_register_device(dev);
  758. if (err < 0)
  759. return err;
  760. err = sensors_create_symlink(&dev->dev.kobj, dev->name);
  761. if (err < 0) {
  762. input_unregister_device(dev);
  763. return err;
  764. }
  765. /* Setup sysfs */
  766. err =sysfs_create_group(&dev->dev.kobj,&k3dh_attribute_group);
  767. if (err < 0)
  768. {
  769. sensors_remove_symlink(&data->input->dev.kobj,data->input->name);
  770. input_unregister_device(dev);
  771. return err;
  772. }
  773. data->input = dev;
  774. return 0;
  775. }
  776. #endif
  777. #ifdef CONFIG_OF
  778. /* device tree parsing function */
  779. static int k3dh_parse_dt(struct device *dev,
  780. struct accel_platform_data_k2dh *pdata)
  781. {
  782. struct device_node *np = dev->of_node;
  783. /*scl,sda and irq */
  784. pdata->i2c_pull_up = of_property_read_bool(np, "stm,i2c-pull-up");
  785. pdata->str_l19 = of_get_property(np,"stm,reg_vdd", NULL);
  786. pdata->str_lvs1 = of_get_property(np,"stm,reg_vio", NULL);
  787. printk(KERN_INFO "%s pull-up:%d \n", __func__, pdata->i2c_pull_up);
  788. return 0;
  789. }
  790. #else
  791. static int k3dh_parse_dt(struct device *dev,
  792. struct accel_platform_data_k2dh)
  793. {
  794. return -ENODEV;
  795. }
  796. #endif
  797. static int k3dh_probe(struct i2c_client *client,
  798. const struct i2c_device_id *id)
  799. {
  800. struct k3dh_data *data;
  801. #if defined(CONFIG_MACH_U1) || defined(CONFIG_MACH_TRATS)
  802. struct device *dev_t, *dev_cal;
  803. #endif
  804. struct accel_platform_data_k2dh *pdata;
  805. int err;
  806. k3dh_infomsg("is started.\n");
  807. if (!i2c_check_functionality(client->adapter,
  808. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  809. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  810. pr_err("%s: i2c functionality check failed!\n", __func__);
  811. err = -ENODEV;
  812. goto exit;
  813. }
  814. if (client-> dev.of_node) {
  815. pdata = devm_kzalloc (&client->dev ,
  816. sizeof (struct accel_platform_data_k2dh), GFP_KERNEL);
  817. if (!pdata) {
  818. dev_err(&client->dev, "Failed to allocate memory\n");
  819. return -ENOMEM;
  820. }
  821. } else
  822. pdata = client->dev.platform_data;
  823. if (!pdata)
  824. return -EINVAL;
  825. err = k3dh_parse_dt(&client->dev, pdata);
  826. if (err) {
  827. goto err_free_pdata;
  828. }
  829. data = kzalloc(sizeof(struct k3dh_data), GFP_KERNEL);
  830. if (data == NULL) {
  831. dev_err(&client->dev,
  832. "failed to allocate memory for module data\n");
  833. err = -ENOMEM;
  834. goto exit;
  835. }
  836. /* Checking device */
  837. err = i2c_smbus_write_byte_data(client, CTRL_REG1,
  838. PM_OFF);
  839. if (err) {
  840. pr_err("%s: there is no such device, err = %d\n",
  841. __func__, err);
  842. goto err_read_reg;
  843. }
  844. data->client = client;
  845. g_k3dh = data;
  846. i2c_set_clientdata(client, data);
  847. k3dh_power_on(data, 1);
  848. msleep(100);
  849. init_completion(&data->data_ready);
  850. mutex_init(&data->read_lock);
  851. mutex_init(&data->write_lock);
  852. atomic_set(&data->opened, 0);
  853. /* sensor HAL expects to find /dev/accelerometer */
  854. data->k3dh_device.minor = MISC_DYNAMIC_MINOR;
  855. data->k3dh_device.name = ACC_DEV_NAME;
  856. data->k3dh_device.fops = &k3dh_fops;
  857. err = misc_register(&data->k3dh_device);
  858. if (err) {
  859. pr_err("%s: misc_register failed\n", __FILE__);
  860. goto err_read_reg;
  861. }
  862. err=sensors_register(data->dev, data, sensor_attrs, MODULE_NAME); //factory attributs
  863. if (err < 0)
  864. goto err_misc_register;
  865. #ifdef CONFIG_SENSOR_K3DH_INPUTDEV
  866. atomic_set(&data->enable, 0);
  867. atomic_set(&data->delay, 200);
  868. k3dh_input_init(data);
  869. /* Setup driver interface */
  870. INIT_DELAYED_WORK(&data->work, k3dh_work_func);
  871. #endif
  872. #if defined(CONFIG_MACH_U1) || defined(CONFIG_MACH_TRATS)
  873. /* creating class/device for test */
  874. data->acc_class = class_create(THIS_MODULE, "accelerometer");
  875. if (IS_ERR(data->acc_class)) {
  876. pr_err("%s: class create failed(accelerometer)\n", __func__);
  877. err = PTR_ERR(data->acc_class);
  878. goto err_class_create;
  879. }
  880. dev_t = device_create(data->acc_class, NULL,
  881. MKDEV(ACC_DEV_MAJOR, 0), "%s", "accelerometer");
  882. if (IS_ERR(dev_t)) {
  883. pr_err("%s: class create failed(accelerometer)\n", __func__);
  884. err = PTR_ERR(dev_t);
  885. goto err_acc_device_create;
  886. }
  887. err = device_create_file(dev_t, &dev_attr_acc_file);
  888. if (err < 0) {
  889. pr_err("%s: Failed to create device file(%s)\n",
  890. __func__, dev_attr_acc_file.attr.name);
  891. goto err_acc_device_create_file;
  892. }
  893. dev_set_drvdata(dev_t, data);
  894. /* creating device for calibration */
  895. dev_cal = device_create(sec_class, NULL, 0, NULL, "gsensorcal");
  896. if (IS_ERR(dev_cal)) {
  897. pr_err("%s: class create failed(gsensorcal)\n", __func__);
  898. err = PTR_ERR(dev_cal);
  899. goto err_cal_device_create;
  900. }
  901. err = device_create_file(dev_cal, &dev_attr_calibration);
  902. if (err < 0) {
  903. pr_err("%s: Failed to create device file(%s)\n",
  904. __func__, dev_attr_calibration.attr.name);
  905. goto err_cal_device_create_file;
  906. }
  907. dev_set_drvdata(dev_cal, data);
  908. #endif
  909. k3dh_infomsg("is successful.\n");
  910. return 0;
  911. #if defined(CONFIG_MACH_U1) || defined(CONFIG_MACH_TRATS)
  912. err_cal_device_create_file:
  913. device_destroy(sec_class, 0);
  914. err_cal_device_create:
  915. device_remove_file(dev_t, &dev_attr_acc_file);
  916. err_acc_device_create_file:
  917. device_destroy(data->acc_class, MKDEV(ACC_DEV_MAJOR, 0));
  918. err_acc_device_create:
  919. class_destroy(data->acc_class);
  920. err_class_create:
  921. #endif
  922. err_misc_register:
  923. misc_deregister(&data->k3dh_device);
  924. mutex_destroy(&data->read_lock);
  925. mutex_destroy(&data->write_lock);
  926. err_read_reg:
  927. kfree(data);
  928. err_free_pdata:
  929. kfree(pdata);
  930. exit:
  931. return err;
  932. }
  933. static int k3dh_remove(struct i2c_client *client)
  934. {
  935. struct k3dh_data *data = i2c_get_clientdata(client);
  936. int err = 0;
  937. if (atomic_read(&data->opened) > 0) {
  938. err = i2c_smbus_write_byte_data(data->client,
  939. CTRL_REG1, PM_OFF);
  940. if (err < 0)
  941. pr_err("%s: pm_off failed %d\n", __func__, err);
  942. }
  943. #if defined(CONFIG_MACH_U1) || defined(CONFIG_MACH_TRATS)
  944. device_destroy(sec_class, 0);
  945. device_destroy(data->acc_class, MKDEV(ACC_DEV_MAJOR, 0));
  946. class_destroy(data->acc_class);
  947. #else
  948. sensors_unregister(data->dev, sensor_attrs);
  949. sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
  950. sysfs_remove_group(&data->input->dev.kobj, &k3dh_attribute_group);
  951. input_unregister_device(data->input);
  952. #endif
  953. misc_deregister(&data->k3dh_device);
  954. mutex_destroy(&data->read_lock);
  955. mutex_destroy(&data->write_lock);
  956. kfree(data);
  957. return 0;
  958. }
  959. static const struct i2c_device_id k3dh_id[] = {
  960. { "k3dh", 0 },
  961. { }
  962. };
  963. MODULE_DEVICE_TABLE(i2c, k3dh_id);
  964. #ifdef CONFIG_OF
  965. static struct of_device_id k3dh_match_table[] = {
  966. { .compatible = "stm,k3dh",},
  967. {},
  968. };
  969. #else
  970. #define k3dh_match_table NULL
  971. #endif
  972. static struct i2c_driver k3dh_driver = {
  973. .probe = k3dh_probe,
  974. .shutdown = k3dh_shutdown,
  975. .remove = __devexit_p(k3dh_remove),
  976. .id_table = k3dh_id,
  977. .driver = {
  978. .pm = &k3dh_pm_ops,
  979. .owner = THIS_MODULE,
  980. .name = "k3dh",
  981. .of_match_table = k3dh_match_table,
  982. },
  983. };
  984. module_i2c_driver(k3dh_driver)
  985. MODULE_DESCRIPTION("k3dh accelerometer driver");
  986. MODULE_AUTHOR("Samsung Electronics");
  987. MODULE_LICENSE("GPL");