mma8x5x.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /*
  2. * mma8x5x.c - Linux kernel modules for 3-Axis Orientation/Motion
  3. * Detection Sensor MMA8451/MMA8452/MMA8453
  4. *
  5. * Copyright (c) 2013-2014, The Linux Foundation. All Rights Reserved.
  6. * Linux Foundation chooses to take subject only to the GPLv2 license
  7. * terms, and distributes only under these terms.
  8. * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/delay.h>
  28. #include <linux/i2c.h>
  29. #include <linux/sensors.h>
  30. #include <linux/input.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/irq.h>
  34. #define ACCEL_INPUT_DEV_NAME "accelerometer"
  35. #define MMA8451_ID 0x1A
  36. #define MMA8452_ID 0x2A
  37. #define MMA8453_ID 0x3A
  38. #define MMA8652_ID 0x4A
  39. #define MMA8653_ID 0x5A
  40. /* Polling delay in msecs */
  41. #define POLL_INTERVAL_MIN 1
  42. #define POLL_INTERVAL_MAX 10000
  43. #define POLL_INTERVAL 100 /* msecs */
  44. #define INPUT_FUZZ 32
  45. #define INPUT_FLAT 32
  46. #define INPUT_DATA_DIVIDER 16
  47. #define MODE_CHANGE_DELAY_MS 100
  48. #define MMA8X5X_STATUS_ZYXDR 0x08
  49. #define MMA8X5X_BUF_SIZE 7
  50. #define MMA_SHUTTEDDOWN (1 << 31)
  51. #define MMA_STATE_MASK (~MMA_SHUTTEDDOWN)
  52. static struct sensors_classdev sensors_cdev = {
  53. .name = "mma8x5x-accel",
  54. .vendor = "Freescale",
  55. .version = 1,
  56. .handle = SENSORS_ACCELERATION_HANDLE,
  57. .type = SENSOR_TYPE_ACCELEROMETER,
  58. .max_range = "19.6",
  59. .resolution = "0.01",
  60. .sensor_power = "0.2",
  61. .min_delay = 2000,
  62. .fifo_reserved_event_count = 0,
  63. .fifo_max_event_count = 0,
  64. .enabled = 0,
  65. .delay_msec = POLL_INTERVAL,
  66. .sensors_enable = NULL,
  67. .sensors_poll_delay = NULL,
  68. };
  69. #define MMA_WAKE_CFG 0x02
  70. #define MMA_INT_EN_DRDY 0x01
  71. #define MMA_INT_EN_FF_MT 0x04
  72. #define MMA_INT_ROUTING_CFG 0x01
  73. #define MMA_POWER_CFG_MASK 0xFE
  74. #define MMA_ODR_MASK 0x38
  75. struct sensor_regulator {
  76. struct regulator *vreg;
  77. const char *name;
  78. u32 min_uV;
  79. u32 max_uV;
  80. };
  81. static struct sensor_regulator mma_vreg[] = {
  82. {NULL, "vdd", 2850000, 2850000},
  83. {NULL, "vio", 1800000, 1800000},
  84. };
  85. struct mma_odr_selection_table {
  86. u32 odr_cfg;
  87. u32 delay_bottom;
  88. u32 delay_top;
  89. };
  90. static struct mma_odr_selection_table mma_odr_table[] = {
  91. {0x00, 0, 1500},
  92. {0x08, 1501, 3500},
  93. {0x10, 3501, 7500},
  94. {0x18, 7501, 15000},
  95. {0x20, 15001, 50000},
  96. {0x28, 50001, 120000},
  97. {0x30, 120001, 400000},
  98. {0x38, 400001, 10000000},
  99. };
  100. /* register enum for mma8x5x registers */
  101. enum {
  102. MMA8X5X_STATUS = 0x00,
  103. MMA8X5X_OUT_X_MSB,
  104. MMA8X5X_OUT_X_LSB,
  105. MMA8X5X_OUT_Y_MSB,
  106. MMA8X5X_OUT_Y_LSB,
  107. MMA8X5X_OUT_Z_MSB,
  108. MMA8X5X_OUT_Z_LSB,
  109. MMA8X5X_F_SETUP = 0x09,
  110. MMA8X5X_TRIG_CFG,
  111. MMA8X5X_SYSMOD,
  112. MMA8X5X_INT_SOURCE,
  113. MMA8X5X_WHO_AM_I,
  114. MMA8X5X_XYZ_DATA_CFG,
  115. MMA8X5X_HP_FILTER_CUTOFF,
  116. MMA8X5X_PL_STATUS,
  117. MMA8X5X_PL_CFG,
  118. MMA8X5X_PL_COUNT,
  119. MMA8X5X_PL_BF_ZCOMP,
  120. MMA8X5X_P_L_THS_REG,
  121. MMA8X5X_FF_MT_CFG,
  122. MMA8X5X_FF_MT_SRC,
  123. MMA8X5X_FF_MT_THS,
  124. MMA8X5X_FF_MT_COUNT,
  125. MMA8X5X_TRANSIENT_CFG = 0x1D,
  126. MMA8X5X_TRANSIENT_SRC,
  127. MMA8X5X_TRANSIENT_THS,
  128. MMA8X5X_TRANSIENT_COUNT,
  129. MMA8X5X_PULSE_CFG,
  130. MMA8X5X_PULSE_SRC,
  131. MMA8X5X_PULSE_THSX,
  132. MMA8X5X_PULSE_THSY,
  133. MMA8X5X_PULSE_THSZ,
  134. MMA8X5X_PULSE_TMLT,
  135. MMA8X5X_PULSE_LTCY,
  136. MMA8X5X_PULSE_WIND,
  137. MMA8X5X_ASLP_COUNT,
  138. MMA8X5X_CTRL_REG1,
  139. MMA8X5X_CTRL_REG2,
  140. MMA8X5X_CTRL_REG3,
  141. MMA8X5X_CTRL_REG4,
  142. MMA8X5X_CTRL_REG5,
  143. MMA8X5X_OFF_X,
  144. MMA8X5X_OFF_Y,
  145. MMA8X5X_OFF_Z,
  146. MMA8X5X_REG_END,
  147. };
  148. /* The sensitivity is represented in counts/g. In 2g mode the
  149. sensitivity is 1024 counts/g. In 4g mode the sensitivity is 512
  150. counts/g and in 8g mode the sensitivity is 256 counts/g.
  151. */
  152. enum {
  153. MODE_2G = 0,
  154. MODE_4G,
  155. MODE_8G,
  156. };
  157. enum {
  158. MMA_STANDBY = 0,
  159. MMA_ACTIVED,
  160. };
  161. struct mma8x5x_data_axis {
  162. short x;
  163. short y;
  164. short z;
  165. };
  166. struct mma8x5x_data {
  167. struct i2c_client *client;
  168. struct delayed_work dwork;
  169. struct input_dev *idev;
  170. struct mutex data_lock;
  171. struct sensors_classdev cdev;
  172. int active;
  173. int position;
  174. u8 chip_id;
  175. int mode;
  176. int int_pin;
  177. u32 int_flags;
  178. int poll_delay;
  179. bool use_int;
  180. };
  181. /* Addresses scanned */
  182. static const unsigned short normal_i2c[] = {0x1c, 0x1d, I2C_CLIENT_END};
  183. static int mma8x5x_chip_id[] = {
  184. MMA8451_ID,
  185. MMA8452_ID,
  186. MMA8453_ID,
  187. MMA8652_ID,
  188. MMA8653_ID,
  189. };
  190. static char *mma8x5x_names[] = {
  191. "mma8451",
  192. "mma8452",
  193. "mma8453",
  194. "mma8652",
  195. "mma8653",
  196. };
  197. static int mma8x5x_position_setting[8][3][3] = {
  198. {{ 0, -1, 0}, { 1, 0, 0}, {0, 0, 1} },
  199. {{-1, 0, 0}, { 0, -1, 0}, {0, 0, 1} },
  200. {{ 0, 1, 0}, {-1, 0, 0}, {0, 0, 1} },
  201. {{ 1, 0, 0}, { 0, 1, 0}, {0, 0, 1} },
  202. {{ 0, -1, 0}, {-1, 0, 0}, {0, 0, -1} },
  203. {{-1, 0, 0}, { 0, 1, 0}, {0, 0, -1} },
  204. {{ 0, 1, 0}, { 1, 0, 0}, {0, 0, -1} },
  205. {{ 1, 0, 0}, { 0, -1, 0}, {0, 0, -1} },
  206. };
  207. static int mma8x5x_config_regulator(struct i2c_client *client, bool on)
  208. {
  209. int rc = 0, i;
  210. int num_vreg = sizeof(mma_vreg)/sizeof(struct sensor_regulator);
  211. if (on) {
  212. for (i = 0; i < num_vreg; i++) {
  213. mma_vreg[i].vreg = regulator_get(&client->dev,
  214. mma_vreg[i].name);
  215. if (IS_ERR(mma_vreg[i].vreg)) {
  216. rc = PTR_ERR(mma_vreg[i].vreg);
  217. dev_err(&client->dev, "%s:regulator get failed rc=%d\n",
  218. __func__, rc);
  219. mma_vreg[i].vreg = NULL;
  220. goto error_vdd;
  221. }
  222. if (regulator_count_voltages(mma_vreg[i].vreg) > 0) {
  223. rc = regulator_set_voltage(mma_vreg[i].vreg,
  224. mma_vreg[i].min_uV, mma_vreg[i].max_uV);
  225. if (rc) {
  226. dev_err(&client->dev, "%s:set_voltage failed rc=%d\n",
  227. __func__, rc);
  228. regulator_put(mma_vreg[i].vreg);
  229. mma_vreg[i].vreg = NULL;
  230. goto error_vdd;
  231. }
  232. }
  233. rc = regulator_enable(mma_vreg[i].vreg);
  234. if (rc) {
  235. dev_err(&client->dev, "%s: regulator_enable failed rc =%d\n",
  236. __func__, rc);
  237. if (regulator_count_voltages(mma_vreg[i].vreg)
  238. > 0) {
  239. regulator_set_voltage(mma_vreg[i].vreg,
  240. 0, mma_vreg[i].max_uV);
  241. }
  242. regulator_put(mma_vreg[i].vreg);
  243. mma_vreg[i].vreg = NULL;
  244. goto error_vdd;
  245. }
  246. }
  247. return rc;
  248. } else {
  249. i = num_vreg;
  250. }
  251. error_vdd:
  252. while (--i >= 0) {
  253. if (!IS_ERR_OR_NULL(mma_vreg[i].vreg)) {
  254. if (regulator_count_voltages(
  255. mma_vreg[i].vreg) > 0) {
  256. regulator_set_voltage(mma_vreg[i].vreg, 0,
  257. mma_vreg[i].max_uV);
  258. }
  259. regulator_disable(mma_vreg[i].vreg);
  260. regulator_put(mma_vreg[i].vreg);
  261. mma_vreg[i].vreg = NULL;
  262. }
  263. }
  264. return rc;
  265. }
  266. static int mma8x5x_data_convert(struct mma8x5x_data *pdata,
  267. struct mma8x5x_data_axis *axis_data)
  268. {
  269. short rawdata[3], data[3];
  270. int i, j;
  271. int position = pdata->position ;
  272. if (position < 0 || position > 7)
  273. position = 0;
  274. rawdata[0] = axis_data->x;
  275. rawdata[1] = axis_data->y;
  276. rawdata[2] = axis_data->z;
  277. for (i = 0; i < 3 ; i++) {
  278. data[i] = 0;
  279. for (j = 0; j < 3; j++)
  280. data[i] += rawdata[j] *
  281. mma8x5x_position_setting[position][i][j];
  282. }
  283. axis_data->x = data[0]/INPUT_DATA_DIVIDER;
  284. axis_data->y = data[1]/INPUT_DATA_DIVIDER;
  285. axis_data->z = data[2]/INPUT_DATA_DIVIDER;
  286. return 0;
  287. }
  288. static int mma8x5x_check_id(int id)
  289. {
  290. int i = 0;
  291. for (i = 0; i < sizeof(mma8x5x_chip_id)/sizeof(mma8x5x_chip_id[0]);
  292. i++)
  293. if (id == mma8x5x_chip_id[i])
  294. return 1;
  295. return 0;
  296. }
  297. static char *mma8x5x_id2name(u8 id)
  298. {
  299. return mma8x5x_names[(id >> 4)-1];
  300. }
  301. static int mma8x5x_device_init(struct i2c_client *client)
  302. {
  303. int result;
  304. struct mma8x5x_data *pdata = i2c_get_clientdata(client);
  305. result = i2c_smbus_write_byte_data(client, MMA8X5X_CTRL_REG1, 0);
  306. if (result < 0)
  307. goto out;
  308. result = i2c_smbus_write_byte_data(client, MMA8X5X_XYZ_DATA_CFG,
  309. pdata->mode);
  310. if (result < 0)
  311. goto out;
  312. pdata->active = MMA_STANDBY;
  313. msleep(MODE_CHANGE_DELAY_MS);
  314. return 0;
  315. out:
  316. dev_err(&client->dev, "error when init mma8x5x:(%d)", result);
  317. return result;
  318. }
  319. static int mma8x5x_device_stop(struct i2c_client *client)
  320. {
  321. u8 val;
  322. val = i2c_smbus_read_byte_data(client, MMA8X5X_CTRL_REG1);
  323. i2c_smbus_write_byte_data(client, MMA8X5X_CTRL_REG1, val & 0xfe);
  324. return 0;
  325. }
  326. static int mma8x5x_device_start(struct i2c_client *client)
  327. {
  328. struct mma8x5x_data *pdata = i2c_get_clientdata(client);
  329. if (i2c_smbus_write_byte_data(client, MMA8X5X_CTRL_REG1, 0))
  330. goto err_out;
  331. if (i2c_smbus_write_byte_data(client, MMA8X5X_XYZ_DATA_CFG,
  332. pdata->mode))
  333. goto err_out;
  334. /* The BT(boot time) for mma8x5x is 1.55ms according to
  335. *Freescale mma8450Q document. Document Number:MMA8450Q
  336. *Rev: 9.1, 04/2012
  337. */
  338. usleep_range(1600, 2000);
  339. return 0;
  340. err_out:
  341. dev_err(&client->dev, "%s:start device failed", __func__);
  342. return -EIO;
  343. }
  344. static int mma8x5x_delay2odr(u32 delay_ms)
  345. {
  346. int i;
  347. u32 delay_us;
  348. delay_us = delay_ms * 1000;
  349. for (i = 0; i < sizeof(mma_odr_table) /
  350. sizeof(struct mma_odr_selection_table); i++) {
  351. if ((delay_us <= mma_odr_table[i].delay_top) &&
  352. (delay_us > mma_odr_table[i].delay_bottom))
  353. break;
  354. }
  355. if (i < sizeof(mma_odr_table) /
  356. sizeof(struct mma_odr_selection_table))
  357. return mma_odr_table[i].odr_cfg;
  358. else
  359. return -EINVAL;
  360. }
  361. static int mma8x5x_device_set_odr(struct i2c_client *client, u32 delay_ms)
  362. {
  363. struct mma8x5x_data *pdata = i2c_get_clientdata(client);
  364. int result;
  365. u8 val;
  366. /* set ODR is only required for interrupt mode */
  367. if (!pdata->use_int)
  368. return 0;
  369. result = mma8x5x_delay2odr(delay_ms);
  370. if (result < 0)
  371. goto out;
  372. val = (u8)result;
  373. result = i2c_smbus_read_byte_data(client, MMA8X5X_CTRL_REG1);
  374. if (result < 0)
  375. goto out;
  376. val = ((u8)result & ~MMA_ODR_MASK) | val;
  377. result = i2c_smbus_write_byte_data(client, MMA8X5X_CTRL_REG1,
  378. (val & MMA_POWER_CFG_MASK));
  379. if (result < 0)
  380. goto out;
  381. result = i2c_smbus_write_byte_data(client, MMA8X5X_CTRL_REG4,
  382. MMA_INT_EN_DRDY);
  383. if (result < 0)
  384. goto out;
  385. result = i2c_smbus_write_byte_data(client, MMA8X5X_CTRL_REG5,
  386. MMA_INT_ROUTING_CFG);
  387. if (result < 0)
  388. goto out;
  389. result = i2c_smbus_write_byte_data(client, MMA8X5X_CTRL_REG1,
  390. val);
  391. if (result < 0)
  392. goto out;
  393. return 0;
  394. out:
  395. dev_err(&client->dev, "error when set ODR mma8x5x:(%d)", result);
  396. return result;
  397. }
  398. static int mma8x5x_device_int_init(struct i2c_client *client)
  399. {
  400. struct mma8x5x_data *pdata = i2c_get_clientdata(client);
  401. int result;
  402. int val;
  403. result = mma8x5x_device_set_odr(client, pdata->poll_delay);
  404. if (result < 0)
  405. goto out;
  406. val = MMA_WAKE_CFG;
  407. result = i2c_smbus_write_byte_data(client, MMA8X5X_CTRL_REG3,
  408. val);
  409. if (result < 0)
  410. goto out;
  411. val = MMA_INT_EN_DRDY;
  412. result = i2c_smbus_write_byte_data(client, MMA8X5X_CTRL_REG4,
  413. val);
  414. if (result < 0)
  415. goto out;
  416. val = MMA_INT_ROUTING_CFG;
  417. result = i2c_smbus_write_byte_data(client, MMA8X5X_CTRL_REG5,
  418. val);
  419. if (result < 0)
  420. goto out;
  421. return 0;
  422. out:
  423. dev_err(&client->dev, "error when int init mma8x5x:(%d)", result);
  424. return result;
  425. }
  426. static int mma8x5x_read_data(struct i2c_client *client,
  427. struct mma8x5x_data_axis *data)
  428. {
  429. u8 tmp_data[MMA8X5X_BUF_SIZE];
  430. int ret;
  431. ret = i2c_smbus_read_i2c_block_data(client,
  432. MMA8X5X_OUT_X_MSB, 7, tmp_data);
  433. if (ret < MMA8X5X_BUF_SIZE) {
  434. dev_err(&client->dev, "i2c block read failed\n");
  435. return -EIO;
  436. }
  437. data->x = ((tmp_data[0] << 8) & 0xff00) | tmp_data[1];
  438. data->y = ((tmp_data[2] << 8) & 0xff00) | tmp_data[3];
  439. data->z = ((tmp_data[4] << 8) & 0xff00) | tmp_data[5];
  440. return 0;
  441. }
  442. static void mma8x5x_report_data(struct mma8x5x_data *pdata)
  443. {
  444. struct mma8x5x_data_axis data;
  445. mutex_lock(&pdata->data_lock);
  446. if (mma8x5x_read_data(pdata->client, &data) != 0)
  447. goto out;
  448. mma8x5x_data_convert(pdata, &data);
  449. input_report_abs(pdata->idev, ABS_X, data.x);
  450. input_report_abs(pdata->idev, ABS_Y, data.y);
  451. input_report_abs(pdata->idev, ABS_Z, data.z);
  452. input_sync(pdata->idev);
  453. out:
  454. mutex_unlock(&pdata->data_lock);
  455. }
  456. static void mma8x5x_dev_poll(struct work_struct *work)
  457. {
  458. struct mma8x5x_data *pdata = container_of((struct delayed_work *)work,
  459. struct mma8x5x_data, dwork);
  460. if ((pdata->active & MMA_STATE_MASK) == MMA_ACTIVED) {
  461. mma8x5x_report_data(pdata);
  462. schedule_delayed_work(&pdata->dwork,
  463. msecs_to_jiffies(pdata->poll_delay));
  464. }
  465. }
  466. static irqreturn_t mma8x5x_interrupt(int vec, void *data)
  467. {
  468. struct i2c_client *client = (struct i2c_client *)data;
  469. struct mma8x5x_data *pdata = i2c_get_clientdata(client);
  470. struct input_dev *idev = pdata->idev;
  471. struct mma8x5x_data_axis data_axis;
  472. mutex_lock(&pdata->data_lock);
  473. if (mma8x5x_read_data(pdata->client, &data_axis) != 0)
  474. goto out;
  475. mma8x5x_data_convert(pdata, &data_axis);
  476. input_report_abs(idev, ABS_X, data_axis.x);
  477. input_report_abs(idev, ABS_Y, data_axis.y);
  478. input_report_abs(idev, ABS_Z, data_axis.z);
  479. input_sync(idev);
  480. out:
  481. mutex_unlock(&pdata->data_lock);
  482. return IRQ_HANDLED;
  483. }
  484. static int mma8x5x_enable_set(struct sensors_classdev *sensors_cdev,
  485. unsigned int enable)
  486. {
  487. struct mma8x5x_data *pdata = container_of(sensors_cdev,
  488. struct mma8x5x_data, cdev);
  489. struct i2c_client *client = pdata->client;
  490. int ret;
  491. u8 val = 0;
  492. mutex_lock(&pdata->data_lock);
  493. if (enable) {
  494. if (pdata->active & MMA_SHUTTEDDOWN) {
  495. ret = mma8x5x_config_regulator(client, 1);
  496. if (ret)
  497. goto err_failed;
  498. ret = mma8x5x_device_start(client);
  499. if (ret)
  500. goto err_failed;
  501. ret = mma8x5x_device_set_odr(client, pdata->poll_delay);
  502. if (ret)
  503. goto err_failed;
  504. pdata->active &= ~MMA_SHUTTEDDOWN;
  505. }
  506. if (pdata->active == MMA_STANDBY) {
  507. val = i2c_smbus_read_byte_data(client,
  508. MMA8X5X_CTRL_REG1);
  509. if (val < 0) {
  510. dev_err(&client->dev, "read device state failed!");
  511. ret = val;
  512. goto err_failed;
  513. }
  514. ret = i2c_smbus_write_byte_data(client,
  515. MMA8X5X_CTRL_REG1, val | 0x01);
  516. if (ret) {
  517. dev_err(&client->dev, "change device state failed!");
  518. goto err_failed;
  519. }
  520. if (!pdata->use_int)
  521. schedule_delayed_work(&pdata->dwork,
  522. msecs_to_jiffies(pdata->poll_delay));
  523. pdata->active = MMA_ACTIVED;
  524. dev_dbg(&client->dev, "%s:mma enable setting active.\n",
  525. __func__);
  526. }
  527. } else if (enable == 0) {
  528. if (pdata->active == MMA_ACTIVED) {
  529. val = i2c_smbus_read_byte_data(client,
  530. MMA8X5X_CTRL_REG1);
  531. if (val < 0) {
  532. dev_err(&client->dev, "read device state failed!");
  533. ret = val;
  534. goto err_failed;
  535. }
  536. ret = i2c_smbus_write_byte_data(client,
  537. MMA8X5X_CTRL_REG1, val & 0xFE);
  538. if (ret) {
  539. dev_err(&client->dev, "change device state failed!");
  540. goto err_failed;
  541. }
  542. /*
  543. * Set standby state,
  544. * polling work queue will stop after next call.
  545. */
  546. pdata->active = MMA_STANDBY;
  547. dev_dbg(&client->dev, "%s:mma enable setting inactive.\n",
  548. __func__);
  549. }
  550. if (!mma8x5x_config_regulator(client, 0))
  551. pdata->active |= MMA_SHUTTEDDOWN;
  552. }
  553. mutex_unlock(&pdata->data_lock);
  554. return 0;
  555. err_failed:
  556. mutex_unlock(&pdata->data_lock);
  557. return ret;
  558. }
  559. static ssize_t mma8x5x_enable_show(struct device *dev,
  560. struct device_attribute *attr, char *buf)
  561. {
  562. struct mma8x5x_data *pdata = dev_get_drvdata(dev);
  563. struct i2c_client *client;
  564. u8 val;
  565. int enable;
  566. if (!pdata) {
  567. dev_err(dev, "Invalid driver private data!");
  568. return -EINVAL;
  569. }
  570. client = pdata->client;
  571. mutex_lock(&pdata->data_lock);
  572. val = i2c_smbus_read_byte_data(client, MMA8X5X_CTRL_REG1);
  573. if ((val & 0x01) && pdata->active == MMA_ACTIVED)
  574. enable = 1;
  575. else
  576. enable = 0;
  577. mutex_unlock(&pdata->data_lock);
  578. return snprintf(buf, PAGE_SIZE, "%d\n", enable);
  579. }
  580. static ssize_t mma8x5x_enable_store(struct device *dev,
  581. struct device_attribute *attr,
  582. const char *buf, size_t count)
  583. {
  584. struct mma8x5x_data *pdata = dev_get_drvdata(dev);
  585. struct i2c_client *client;
  586. int ret;
  587. unsigned long enable;
  588. if (!pdata) {
  589. dev_err(dev, "Invalid driver private data!");
  590. return -EINVAL;
  591. }
  592. client = pdata->client;
  593. ret = kstrtoul(buf, 10, &enable);
  594. if (ret)
  595. return ret;
  596. enable = (enable > 0) ? 1 : 0;
  597. ret = mma8x5x_enable_set(&pdata->cdev, enable);
  598. if (ret < 0)
  599. return ret;
  600. return count;
  601. }
  602. static ssize_t mma8x5x_position_show(struct device *dev,
  603. struct device_attribute *attr, char *buf)
  604. {
  605. struct mma8x5x_data *pdata = dev_get_drvdata(dev);
  606. int position = 0;
  607. if (!pdata) {
  608. dev_err(dev, "Invalid driver private data!");
  609. return -EINVAL;
  610. }
  611. mutex_lock(&pdata->data_lock);
  612. position = pdata->position ;
  613. mutex_unlock(&pdata->data_lock);
  614. return snprintf(buf, PAGE_SIZE, "%d\n", position);
  615. }
  616. static ssize_t mma8x5x_position_store(struct device *dev,
  617. struct device_attribute *attr,
  618. const char *buf, size_t count)
  619. {
  620. struct mma8x5x_data *pdata = dev_get_drvdata(dev);
  621. int position;
  622. int ret;
  623. if (!pdata) {
  624. dev_err(dev, "Invalid driver private data!");
  625. return -EINVAL;
  626. }
  627. ret = kstrtoint(buf, 10, &position);
  628. if (ret)
  629. return ret;
  630. mutex_lock(&pdata->data_lock);
  631. pdata->position = position;
  632. mutex_unlock(&pdata->data_lock);
  633. return count;
  634. }
  635. static int mma8x5x_poll_delay_set(struct sensors_classdev *sensors_cdev,
  636. unsigned int delay_ms)
  637. {
  638. struct mma8x5x_data *pdata = container_of(sensors_cdev,
  639. struct mma8x5x_data, cdev);
  640. int ret;
  641. if (pdata->use_int) {
  642. mutex_lock(&pdata->data_lock);
  643. pdata->poll_delay = delay_ms;
  644. ret = mma8x5x_device_set_odr(pdata->client, delay_ms);
  645. mutex_unlock(&pdata->data_lock);
  646. if (ret < 0)
  647. return ret;
  648. } else {
  649. mutex_lock(&pdata->data_lock);
  650. pdata->poll_delay = delay_ms;
  651. mutex_unlock(&pdata->data_lock);
  652. }
  653. return 0;
  654. }
  655. static ssize_t mma8x5x_poll_delay_show(struct device *dev,
  656. struct device_attribute *attr, char *buf)
  657. {
  658. struct mma8x5x_data *pdata = dev_get_drvdata(dev);
  659. if (!pdata) {
  660. dev_err(dev, "Invalid driver private data!");
  661. return -EINVAL;
  662. }
  663. return snprintf(buf, PAGE_SIZE, "%d\n", pdata->poll_delay);
  664. }
  665. static ssize_t mma8x5x_poll_delay_store(struct device *dev,
  666. struct device_attribute *attr,
  667. const char *buf, size_t count)
  668. {
  669. struct mma8x5x_data *pdata = dev_get_drvdata(dev);
  670. int delay;
  671. int ret;
  672. if (!pdata) {
  673. dev_err(dev, "Invalid driver private data!");
  674. return -EINVAL;
  675. }
  676. ret = kstrtoint(buf, 10, &delay);
  677. if (ret)
  678. return ret;
  679. if (delay <= POLL_INTERVAL_MIN)
  680. delay = POLL_INTERVAL_MIN;
  681. if (delay > POLL_INTERVAL_MAX)
  682. delay = POLL_INTERVAL_MAX;
  683. mma8x5x_poll_delay_set(&pdata->cdev, delay);
  684. return count;
  685. }
  686. static DEVICE_ATTR(enable, S_IWUSR | S_IRUGO,
  687. mma8x5x_enable_show, mma8x5x_enable_store);
  688. static DEVICE_ATTR(position, S_IWUSR | S_IRUGO,
  689. mma8x5x_position_show, mma8x5x_position_store);
  690. static DEVICE_ATTR(poll_delay, S_IWUSR | S_IRUGO,
  691. mma8x5x_poll_delay_show, mma8x5x_poll_delay_store);
  692. static struct attribute *mma8x5x_attributes[] = {
  693. &dev_attr_enable.attr,
  694. &dev_attr_position.attr,
  695. &dev_attr_poll_delay.attr,
  696. NULL
  697. };
  698. static const struct attribute_group mma8x5x_attr_group = {
  699. .attrs = mma8x5x_attributes,
  700. };
  701. static int mma8x5x_detect(struct i2c_client *client,
  702. struct i2c_board_info *info)
  703. {
  704. struct i2c_adapter *adapter = client->adapter;
  705. int chip_id;
  706. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA))
  707. return -ENODEV;
  708. chip_id = i2c_smbus_read_byte_data(client, MMA8X5X_WHO_AM_I);
  709. if (!mma8x5x_check_id(chip_id))
  710. return -ENODEV;
  711. dev_dbg(&client->dev, "%s,check %s i2c address 0x%x.\n",
  712. __func__, mma8x5x_id2name(chip_id), client->addr);
  713. strlcpy(info->type, "mma8x5x", I2C_NAME_SIZE);
  714. return 0;
  715. }
  716. static int mma8x5x_parse_dt(struct device *dev, struct mma8x5x_data *data)
  717. {
  718. int rc;
  719. struct device_node *np = dev->of_node;
  720. u32 temp_val;
  721. data->int_pin = of_get_named_gpio_flags(np, "fsl,irq-gpio",
  722. 0, &data->int_flags);
  723. if (data->int_pin < 0) {
  724. dev_err(dev, "Unable to read irq-gpio\n");
  725. return data->int_pin;
  726. }
  727. rc = of_property_read_u32(np, "fsl,sensors-position", &temp_val);
  728. if (!rc)
  729. data->position = temp_val;
  730. else {
  731. dev_err(dev, "Unable to read sensors-position\n");
  732. return rc;
  733. }
  734. data->use_int = of_property_read_bool(np, "fsl,use-interrupt");
  735. return 0;
  736. }
  737. static int __devinit mma8x5x_probe(struct i2c_client *client,
  738. const struct i2c_device_id *id)
  739. {
  740. int result, chip_id;
  741. struct input_dev *idev;
  742. struct mma8x5x_data *pdata;
  743. struct i2c_adapter *adapter;
  744. adapter = to_i2c_adapter(client->dev.parent);
  745. /* power on the device */
  746. result = mma8x5x_config_regulator(client, 1);
  747. if (result)
  748. goto err_power_on;
  749. result = i2c_check_functionality(adapter,
  750. I2C_FUNC_SMBUS_BYTE |
  751. I2C_FUNC_SMBUS_BYTE_DATA);
  752. if (!result)
  753. goto err_check_id;
  754. chip_id = i2c_smbus_read_byte_data(client, MMA8X5X_WHO_AM_I);
  755. if (!mma8x5x_check_id(chip_id)) {
  756. dev_err(&client->dev,
  757. "read chip ID 0x%x is not equal to 0x%x,0x%x,0x%x,0x%x,0x%x!\n",
  758. chip_id, MMA8451_ID, MMA8452_ID, MMA8453_ID,
  759. MMA8652_ID, MMA8653_ID);
  760. result = -EINVAL;
  761. goto err_check_id;
  762. }
  763. /* set the private data */
  764. pdata = kzalloc(sizeof(struct mma8x5x_data), GFP_KERNEL);
  765. if (!pdata) {
  766. result = -ENOMEM;
  767. dev_err(&client->dev, "alloc data memory error!\n");
  768. goto err_check_id;
  769. }
  770. if (client->dev.of_node) {
  771. result = mma8x5x_parse_dt(&client->dev, pdata);
  772. if (result)
  773. goto err_parse_dt;
  774. } else {
  775. pdata->position = CONFIG_SENSORS_MMA_POSITION;
  776. pdata->int_pin = -1;
  777. pdata->int_flags = 0;
  778. }
  779. /* Initialize the MMA8X5X chip */
  780. pdata->client = client;
  781. pdata->chip_id = chip_id;
  782. pdata->mode = MODE_2G;
  783. pdata->poll_delay = POLL_INTERVAL;
  784. mutex_init(&pdata->data_lock);
  785. i2c_set_clientdata(client, pdata);
  786. /* Initialize the MMA8X5X chip */
  787. mma8x5x_device_init(client);
  788. if (pdata->use_int) {
  789. if (pdata->int_pin >= 0)
  790. client->irq = gpio_to_irq(pdata->int_pin);
  791. if (gpio_is_valid(pdata->int_pin)) {
  792. result = gpio_request(pdata->int_pin,
  793. "mma8x5x_irq_gpio");
  794. if (result) {
  795. dev_err(&client->dev, "irq gpio(%d) request failed",
  796. pdata->int_pin);
  797. goto err_request_gpio;
  798. }
  799. result = gpio_direction_input(pdata->int_pin);
  800. if (result) {
  801. dev_err(&client->dev,
  802. "set_direction for irq gpio failed\n");
  803. goto err_set_gpio_direction;
  804. }
  805. }
  806. device_init_wakeup(&client->dev, true);
  807. enable_irq_wake(client->irq);
  808. result = request_threaded_irq(client->irq, NULL,
  809. mma8x5x_interrupt,
  810. IRQ_TYPE_EDGE_RISING | IRQF_ONESHOT | IRQF_NO_SUSPEND,
  811. ACCEL_INPUT_DEV_NAME, (void *)client);
  812. if (result) {
  813. dev_err(&client->dev, "Could not allocate irq(%d) !\n",
  814. client->irq);
  815. goto err_register_irq;
  816. }
  817. mma8x5x_device_int_init(client);
  818. } else {
  819. INIT_DELAYED_WORK(&pdata->dwork, mma8x5x_dev_poll);
  820. }
  821. idev = input_allocate_device();
  822. if (!idev) {
  823. result = -ENOMEM;
  824. dev_err(&client->dev, "alloc input device failed!\n");
  825. goto err_alloc_poll_device;
  826. }
  827. input_set_drvdata(idev, pdata);
  828. idev->name = ACCEL_INPUT_DEV_NAME;
  829. idev->uniq = mma8x5x_id2name(pdata->chip_id);
  830. idev->id.bustype = BUS_I2C;
  831. idev->evbit[0] = BIT_MASK(EV_ABS);
  832. input_set_abs_params(idev, ABS_X, -0x7fff, 0x7fff, 0, 0);
  833. input_set_abs_params(idev, ABS_Y, -0x7fff, 0x7fff, 0, 0);
  834. input_set_abs_params(idev, ABS_Z, -0x7fff, 0x7fff, 0, 0);
  835. result = input_register_device(idev);
  836. if (result) {
  837. dev_err(&client->dev, "register input device failed!\n");
  838. goto err_register_device;
  839. }
  840. pdata->idev = idev;
  841. result = sysfs_create_group(&idev->dev.kobj, &mma8x5x_attr_group);
  842. if (result) {
  843. dev_err(&client->dev, "create device file failed!\n");
  844. result = -EINVAL;
  845. goto err_create_sysfs;
  846. }
  847. pdata->cdev = sensors_cdev;
  848. pdata->cdev.min_delay = POLL_INTERVAL_MIN * 1000;
  849. pdata->cdev.delay_msec = pdata->poll_delay;
  850. pdata->cdev.sensors_enable = mma8x5x_enable_set;
  851. pdata->cdev.sensors_poll_delay = mma8x5x_poll_delay_set;
  852. result = sensors_classdev_register(&client->dev, &pdata->cdev);
  853. if (result) {
  854. dev_err(&client->dev, "create class device file failed!\n");
  855. result = -EINVAL;
  856. goto err_create_class_sysfs;
  857. }
  858. dev_info(&client->dev,
  859. "%s:mma8x5x device driver probe successfully, position =%d\n",
  860. __func__, pdata->position);
  861. return 0;
  862. err_create_class_sysfs:
  863. sysfs_remove_group(&idev->dev.kobj, &mma8x5x_attr_group);
  864. err_create_sysfs:
  865. input_unregister_device(idev);
  866. err_register_device:
  867. input_free_device(idev);
  868. err_alloc_poll_device:
  869. err_register_irq:
  870. if (pdata->use_int)
  871. device_init_wakeup(&client->dev, false);
  872. err_set_gpio_direction:
  873. if (gpio_is_valid(pdata->int_pin) && pdata->use_int)
  874. gpio_free(pdata->int_pin);
  875. err_request_gpio:
  876. err_parse_dt:
  877. kfree(pdata);
  878. err_check_id:
  879. mma8x5x_config_regulator(client, 0);
  880. err_power_on:
  881. return result;
  882. }
  883. static int __devexit mma8x5x_remove(struct i2c_client *client)
  884. {
  885. struct mma8x5x_data *pdata = i2c_get_clientdata(client);
  886. struct input_dev *idev;
  887. mma8x5x_device_stop(client);
  888. if (pdata) {
  889. idev = pdata->idev;
  890. sysfs_remove_group(&idev->dev.kobj, &mma8x5x_attr_group);
  891. if (pdata->use_int) {
  892. device_init_wakeup(&client->dev, false);
  893. if (gpio_is_valid(pdata->int_pin))
  894. gpio_free(pdata->int_pin);
  895. }
  896. input_unregister_device(idev);
  897. input_free_device(idev);
  898. kfree(pdata);
  899. }
  900. mma8x5x_config_regulator(client, 0);
  901. return 0;
  902. }
  903. #ifdef CONFIG_PM_SLEEP
  904. static int mma8x5x_suspend(struct device *dev)
  905. {
  906. struct i2c_client *client = to_i2c_client(dev);
  907. struct mma8x5x_data *pdata = i2c_get_clientdata(client);
  908. if (pdata->use_int && pdata->active == MMA_ACTIVED)
  909. return 0;
  910. if (pdata->active == MMA_ACTIVED) {
  911. mma8x5x_device_stop(client);
  912. cancel_delayed_work_sync(&pdata->dwork);
  913. }
  914. if (pdata->active & MMA_SHUTTEDDOWN)
  915. return 0;
  916. if (!mma8x5x_config_regulator(client, 0))
  917. /* The highest bit sotres the power state */
  918. pdata->active |= MMA_SHUTTEDDOWN;
  919. return 0;
  920. }
  921. static int mma8x5x_resume(struct device *dev)
  922. {
  923. int val = 0;
  924. struct i2c_client *client = to_i2c_client(dev);
  925. struct mma8x5x_data *pdata = i2c_get_clientdata(client);
  926. if (pdata->use_int && pdata->active == MMA_ACTIVED)
  927. return 0;
  928. /* No need to power on while device is shutdowned from standby state */
  929. if (pdata->active == (MMA_SHUTTEDDOWN | MMA_STANDBY))
  930. return 0;
  931. if (pdata->active & MMA_SHUTTEDDOWN) {
  932. if (mma8x5x_config_regulator(client, 1))
  933. goto out;
  934. if (mma8x5x_device_start(client))
  935. goto out;
  936. pdata->active &= ~MMA_SHUTTEDDOWN;
  937. }
  938. if (pdata->active == MMA_ACTIVED) {
  939. val = i2c_smbus_read_byte_data(client, MMA8X5X_CTRL_REG1);
  940. i2c_smbus_write_byte_data(client, MMA8X5X_CTRL_REG1, val|0x01);
  941. schedule_delayed_work(&pdata->dwork,
  942. msecs_to_jiffies(pdata->poll_delay));
  943. }
  944. return 0;
  945. out:
  946. dev_err(&client->dev, "%s:failed during resume operation", __func__);
  947. return -EIO;
  948. }
  949. #endif
  950. static const struct i2c_device_id mma8x5x_id[] = {
  951. {"mma8x5x", 0},
  952. { }
  953. };
  954. MODULE_DEVICE_TABLE(i2c, mma8x5x_id);
  955. static const struct of_device_id mma8x5x_of_match[] = {
  956. { .compatible = "fsl,mma8x5x", },
  957. { },
  958. };
  959. static SIMPLE_DEV_PM_OPS(mma8x5x_pm_ops, mma8x5x_suspend, mma8x5x_resume);
  960. static struct i2c_driver mma8x5x_driver = {
  961. .class = I2C_CLASS_HWMON,
  962. .driver = {
  963. .name = "mma8x5x",
  964. .owner = THIS_MODULE,
  965. .pm = &mma8x5x_pm_ops,
  966. .of_match_table = mma8x5x_of_match,
  967. },
  968. .probe = mma8x5x_probe,
  969. .remove = __devexit_p(mma8x5x_remove),
  970. .id_table = mma8x5x_id,
  971. .detect = mma8x5x_detect,
  972. .address_list = normal_i2c,
  973. };
  974. static int __init mma8x5x_init(void)
  975. {
  976. /* register driver */
  977. int res;
  978. res = i2c_add_driver(&mma8x5x_driver);
  979. if (res < 0) {
  980. pr_info("%s:add mma8x5x i2c driver failed\n", __func__);
  981. return -ENODEV;
  982. }
  983. return res;
  984. }
  985. static void __exit mma8x5x_exit(void)
  986. {
  987. i2c_del_driver(&mma8x5x_driver);
  988. }
  989. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  990. MODULE_DESCRIPTION("MMA8X5X 3-Axis Orientation/Motion Detection Sensor driver");
  991. MODULE_LICENSE("GPL");
  992. module_init(mma8x5x_init);
  993. module_exit(mma8x5x_exit);