mma9551.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * Freescale MMA9551L Intelligent Motion-Sensing Platform driver
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/slab.h>
  18. #include <linux/acpi.h>
  19. #include <linux/delay.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/sysfs.h>
  23. #include <linux/iio/events.h>
  24. #include <linux/pm_runtime.h>
  25. #include "mma9551_core.h"
  26. #define MMA9551_DRV_NAME "mma9551"
  27. #define MMA9551_IRQ_NAME "mma9551_event"
  28. #define MMA9551_GPIO_COUNT 4
  29. /* Tilt application (inclination in IIO terms). */
  30. #define MMA9551_TILT_XZ_ANG_REG 0x00
  31. #define MMA9551_TILT_YZ_ANG_REG 0x01
  32. #define MMA9551_TILT_XY_ANG_REG 0x02
  33. #define MMA9551_TILT_ANGFLG BIT(7)
  34. #define MMA9551_TILT_QUAD_REG 0x03
  35. #define MMA9551_TILT_XY_QUAD_SHIFT 0
  36. #define MMA9551_TILT_YZ_QUAD_SHIFT 2
  37. #define MMA9551_TILT_XZ_QUAD_SHIFT 4
  38. #define MMA9551_TILT_CFG_REG 0x01
  39. #define MMA9551_TILT_ANG_THRESH_MASK GENMASK(3, 0)
  40. #define MMA9551_DEFAULT_SAMPLE_RATE 122 /* Hz */
  41. /* Tilt events are mapped to the first three GPIO pins. */
  42. enum mma9551_tilt_axis {
  43. mma9551_x = 0,
  44. mma9551_y,
  45. mma9551_z,
  46. };
  47. struct mma9551_data {
  48. struct i2c_client *client;
  49. struct mutex mutex;
  50. int event_enabled[3];
  51. int irqs[MMA9551_GPIO_COUNT];
  52. };
  53. static int mma9551_read_incli_chan(struct i2c_client *client,
  54. const struct iio_chan_spec *chan,
  55. int *val)
  56. {
  57. u8 quad_shift, angle, quadrant;
  58. u16 reg_addr;
  59. int ret;
  60. switch (chan->channel2) {
  61. case IIO_MOD_X:
  62. reg_addr = MMA9551_TILT_YZ_ANG_REG;
  63. quad_shift = MMA9551_TILT_YZ_QUAD_SHIFT;
  64. break;
  65. case IIO_MOD_Y:
  66. reg_addr = MMA9551_TILT_XZ_ANG_REG;
  67. quad_shift = MMA9551_TILT_XZ_QUAD_SHIFT;
  68. break;
  69. case IIO_MOD_Z:
  70. reg_addr = MMA9551_TILT_XY_ANG_REG;
  71. quad_shift = MMA9551_TILT_XY_QUAD_SHIFT;
  72. break;
  73. default:
  74. return -EINVAL;
  75. }
  76. ret = mma9551_set_power_state(client, true);
  77. if (ret < 0)
  78. return ret;
  79. ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
  80. reg_addr, &angle);
  81. if (ret < 0)
  82. goto out_poweroff;
  83. ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
  84. MMA9551_TILT_QUAD_REG, &quadrant);
  85. if (ret < 0)
  86. goto out_poweroff;
  87. angle &= ~MMA9551_TILT_ANGFLG;
  88. quadrant = (quadrant >> quad_shift) & 0x03;
  89. if (quadrant == 1 || quadrant == 3)
  90. *val = 90 * (quadrant + 1) - angle;
  91. else
  92. *val = angle + 90 * quadrant;
  93. ret = IIO_VAL_INT;
  94. out_poweroff:
  95. mma9551_set_power_state(client, false);
  96. return ret;
  97. }
  98. static int mma9551_read_raw(struct iio_dev *indio_dev,
  99. struct iio_chan_spec const *chan,
  100. int *val, int *val2, long mask)
  101. {
  102. struct mma9551_data *data = iio_priv(indio_dev);
  103. int ret;
  104. switch (mask) {
  105. case IIO_CHAN_INFO_PROCESSED:
  106. switch (chan->type) {
  107. case IIO_INCLI:
  108. mutex_lock(&data->mutex);
  109. ret = mma9551_read_incli_chan(data->client, chan, val);
  110. mutex_unlock(&data->mutex);
  111. return ret;
  112. default:
  113. return -EINVAL;
  114. }
  115. case IIO_CHAN_INFO_RAW:
  116. switch (chan->type) {
  117. case IIO_ACCEL:
  118. mutex_lock(&data->mutex);
  119. ret = mma9551_read_accel_chan(data->client,
  120. chan, val, val2);
  121. mutex_unlock(&data->mutex);
  122. return ret;
  123. default:
  124. return -EINVAL;
  125. }
  126. case IIO_CHAN_INFO_SCALE:
  127. switch (chan->type) {
  128. case IIO_ACCEL:
  129. return mma9551_read_accel_scale(val, val2);
  130. default:
  131. return -EINVAL;
  132. }
  133. default:
  134. return -EINVAL;
  135. }
  136. }
  137. static int mma9551_read_event_config(struct iio_dev *indio_dev,
  138. const struct iio_chan_spec *chan,
  139. enum iio_event_type type,
  140. enum iio_event_direction dir)
  141. {
  142. struct mma9551_data *data = iio_priv(indio_dev);
  143. switch (chan->type) {
  144. case IIO_INCLI:
  145. /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
  146. return data->event_enabled[chan->channel2 - 1];
  147. default:
  148. return -EINVAL;
  149. }
  150. }
  151. static int mma9551_config_incli_event(struct iio_dev *indio_dev,
  152. enum iio_modifier axis,
  153. int state)
  154. {
  155. struct mma9551_data *data = iio_priv(indio_dev);
  156. enum mma9551_tilt_axis mma_axis;
  157. int ret;
  158. /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
  159. mma_axis = axis - 1;
  160. if (data->event_enabled[mma_axis] == state)
  161. return 0;
  162. if (state == 0) {
  163. ret = mma9551_gpio_config(data->client,
  164. (enum mma9551_gpio_pin)mma_axis,
  165. MMA9551_APPID_NONE, 0, 0);
  166. if (ret < 0)
  167. return ret;
  168. ret = mma9551_set_power_state(data->client, false);
  169. if (ret < 0)
  170. return ret;
  171. } else {
  172. int bitnum;
  173. /* Bit 7 of each angle register holds the angle flag. */
  174. switch (axis) {
  175. case IIO_MOD_X:
  176. bitnum = 7 + 8 * MMA9551_TILT_YZ_ANG_REG;
  177. break;
  178. case IIO_MOD_Y:
  179. bitnum = 7 + 8 * MMA9551_TILT_XZ_ANG_REG;
  180. break;
  181. case IIO_MOD_Z:
  182. bitnum = 7 + 8 * MMA9551_TILT_XY_ANG_REG;
  183. break;
  184. default:
  185. return -EINVAL;
  186. }
  187. ret = mma9551_set_power_state(data->client, true);
  188. if (ret < 0)
  189. return ret;
  190. ret = mma9551_gpio_config(data->client,
  191. (enum mma9551_gpio_pin)mma_axis,
  192. MMA9551_APPID_TILT, bitnum, 0);
  193. if (ret < 0) {
  194. mma9551_set_power_state(data->client, false);
  195. return ret;
  196. }
  197. }
  198. data->event_enabled[mma_axis] = state;
  199. return ret;
  200. }
  201. static int mma9551_write_event_config(struct iio_dev *indio_dev,
  202. const struct iio_chan_spec *chan,
  203. enum iio_event_type type,
  204. enum iio_event_direction dir,
  205. int state)
  206. {
  207. struct mma9551_data *data = iio_priv(indio_dev);
  208. int ret;
  209. switch (chan->type) {
  210. case IIO_INCLI:
  211. mutex_lock(&data->mutex);
  212. ret = mma9551_config_incli_event(indio_dev,
  213. chan->channel2, state);
  214. mutex_unlock(&data->mutex);
  215. return ret;
  216. default:
  217. return -EINVAL;
  218. }
  219. }
  220. static int mma9551_write_event_value(struct iio_dev *indio_dev,
  221. const struct iio_chan_spec *chan,
  222. enum iio_event_type type,
  223. enum iio_event_direction dir,
  224. enum iio_event_info info,
  225. int val, int val2)
  226. {
  227. struct mma9551_data *data = iio_priv(indio_dev);
  228. int ret;
  229. switch (chan->type) {
  230. case IIO_INCLI:
  231. if (val2 != 0 || val < 1 || val > 10)
  232. return -EINVAL;
  233. mutex_lock(&data->mutex);
  234. ret = mma9551_update_config_bits(data->client,
  235. MMA9551_APPID_TILT,
  236. MMA9551_TILT_CFG_REG,
  237. MMA9551_TILT_ANG_THRESH_MASK,
  238. val);
  239. mutex_unlock(&data->mutex);
  240. return ret;
  241. default:
  242. return -EINVAL;
  243. }
  244. }
  245. static int mma9551_read_event_value(struct iio_dev *indio_dev,
  246. const struct iio_chan_spec *chan,
  247. enum iio_event_type type,
  248. enum iio_event_direction dir,
  249. enum iio_event_info info,
  250. int *val, int *val2)
  251. {
  252. struct mma9551_data *data = iio_priv(indio_dev);
  253. int ret;
  254. u8 tmp;
  255. switch (chan->type) {
  256. case IIO_INCLI:
  257. mutex_lock(&data->mutex);
  258. ret = mma9551_read_config_byte(data->client,
  259. MMA9551_APPID_TILT,
  260. MMA9551_TILT_CFG_REG, &tmp);
  261. mutex_unlock(&data->mutex);
  262. if (ret < 0)
  263. return ret;
  264. *val = tmp & MMA9551_TILT_ANG_THRESH_MASK;
  265. *val2 = 0;
  266. return IIO_VAL_INT;
  267. default:
  268. return -EINVAL;
  269. }
  270. }
  271. static const struct iio_event_spec mma9551_incli_event = {
  272. .type = IIO_EV_TYPE_ROC,
  273. .dir = IIO_EV_DIR_RISING,
  274. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  275. .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE),
  276. };
  277. #define MMA9551_INCLI_CHANNEL(axis) { \
  278. .type = IIO_INCLI, \
  279. .modified = 1, \
  280. .channel2 = axis, \
  281. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
  282. .event_spec = &mma9551_incli_event, \
  283. .num_event_specs = 1, \
  284. }
  285. static const struct iio_chan_spec mma9551_channels[] = {
  286. MMA9551_ACCEL_CHANNEL(IIO_MOD_X),
  287. MMA9551_ACCEL_CHANNEL(IIO_MOD_Y),
  288. MMA9551_ACCEL_CHANNEL(IIO_MOD_Z),
  289. MMA9551_INCLI_CHANNEL(IIO_MOD_X),
  290. MMA9551_INCLI_CHANNEL(IIO_MOD_Y),
  291. MMA9551_INCLI_CHANNEL(IIO_MOD_Z),
  292. };
  293. static const struct iio_info mma9551_info = {
  294. .driver_module = THIS_MODULE,
  295. .read_raw = mma9551_read_raw,
  296. .read_event_config = mma9551_read_event_config,
  297. .write_event_config = mma9551_write_event_config,
  298. .read_event_value = mma9551_read_event_value,
  299. .write_event_value = mma9551_write_event_value,
  300. };
  301. static irqreturn_t mma9551_event_handler(int irq, void *private)
  302. {
  303. struct iio_dev *indio_dev = private;
  304. struct mma9551_data *data = iio_priv(indio_dev);
  305. int i, ret, mma_axis = -1;
  306. u16 reg;
  307. u8 val;
  308. mutex_lock(&data->mutex);
  309. for (i = 0; i < 3; i++)
  310. if (irq == data->irqs[i]) {
  311. mma_axis = i;
  312. break;
  313. }
  314. if (mma_axis == -1) {
  315. /* IRQ was triggered on 4th line, which we don't use. */
  316. dev_warn(&data->client->dev,
  317. "irq triggered on unused line %d\n", data->irqs[3]);
  318. goto out;
  319. }
  320. switch (mma_axis) {
  321. case mma9551_x:
  322. reg = MMA9551_TILT_YZ_ANG_REG;
  323. break;
  324. case mma9551_y:
  325. reg = MMA9551_TILT_XZ_ANG_REG;
  326. break;
  327. case mma9551_z:
  328. reg = MMA9551_TILT_XY_ANG_REG;
  329. break;
  330. }
  331. /*
  332. * Read the angle even though we don't use it, otherwise we
  333. * won't get any further interrupts.
  334. */
  335. ret = mma9551_read_status_byte(data->client, MMA9551_APPID_TILT,
  336. reg, &val);
  337. if (ret < 0) {
  338. dev_err(&data->client->dev,
  339. "error %d reading tilt register in IRQ\n", ret);
  340. goto out;
  341. }
  342. iio_push_event(indio_dev,
  343. IIO_MOD_EVENT_CODE(IIO_INCLI, 0, (mma_axis + 1),
  344. IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING),
  345. iio_get_time_ns(indio_dev));
  346. out:
  347. mutex_unlock(&data->mutex);
  348. return IRQ_HANDLED;
  349. }
  350. static int mma9551_init(struct mma9551_data *data)
  351. {
  352. int ret;
  353. ret = mma9551_read_version(data->client);
  354. if (ret)
  355. return ret;
  356. return mma9551_set_device_state(data->client, true);
  357. }
  358. static int mma9551_gpio_probe(struct iio_dev *indio_dev)
  359. {
  360. struct gpio_desc *gpio;
  361. int i, ret;
  362. struct mma9551_data *data = iio_priv(indio_dev);
  363. struct device *dev = &data->client->dev;
  364. for (i = 0; i < MMA9551_GPIO_COUNT; i++) {
  365. gpio = devm_gpiod_get_index(dev, NULL, i, GPIOD_IN);
  366. if (IS_ERR(gpio)) {
  367. dev_err(dev, "acpi gpio get index failed\n");
  368. return PTR_ERR(gpio);
  369. }
  370. ret = gpiod_to_irq(gpio);
  371. if (ret < 0)
  372. return ret;
  373. data->irqs[i] = ret;
  374. ret = devm_request_threaded_irq(dev, data->irqs[i],
  375. NULL, mma9551_event_handler,
  376. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  377. MMA9551_IRQ_NAME, indio_dev);
  378. if (ret < 0) {
  379. dev_err(dev, "request irq %d failed\n", data->irqs[i]);
  380. return ret;
  381. }
  382. dev_dbg(dev, "gpio resource, no:%d irq:%d\n",
  383. desc_to_gpio(gpio), data->irqs[i]);
  384. }
  385. return 0;
  386. }
  387. static const char *mma9551_match_acpi_device(struct device *dev)
  388. {
  389. const struct acpi_device_id *id;
  390. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  391. if (!id)
  392. return NULL;
  393. return dev_name(dev);
  394. }
  395. static int mma9551_probe(struct i2c_client *client,
  396. const struct i2c_device_id *id)
  397. {
  398. struct mma9551_data *data;
  399. struct iio_dev *indio_dev;
  400. const char *name = NULL;
  401. int ret;
  402. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  403. if (!indio_dev)
  404. return -ENOMEM;
  405. data = iio_priv(indio_dev);
  406. i2c_set_clientdata(client, indio_dev);
  407. data->client = client;
  408. if (id)
  409. name = id->name;
  410. else if (ACPI_HANDLE(&client->dev))
  411. name = mma9551_match_acpi_device(&client->dev);
  412. ret = mma9551_init(data);
  413. if (ret < 0)
  414. return ret;
  415. mutex_init(&data->mutex);
  416. indio_dev->dev.parent = &client->dev;
  417. indio_dev->channels = mma9551_channels;
  418. indio_dev->num_channels = ARRAY_SIZE(mma9551_channels);
  419. indio_dev->name = name;
  420. indio_dev->modes = INDIO_DIRECT_MODE;
  421. indio_dev->info = &mma9551_info;
  422. ret = mma9551_gpio_probe(indio_dev);
  423. if (ret < 0)
  424. goto out_poweroff;
  425. ret = pm_runtime_set_active(&client->dev);
  426. if (ret < 0)
  427. goto out_poweroff;
  428. pm_runtime_enable(&client->dev);
  429. pm_runtime_set_autosuspend_delay(&client->dev,
  430. MMA9551_AUTO_SUSPEND_DELAY_MS);
  431. pm_runtime_use_autosuspend(&client->dev);
  432. ret = iio_device_register(indio_dev);
  433. if (ret < 0) {
  434. dev_err(&client->dev, "unable to register iio device\n");
  435. goto out_poweroff;
  436. }
  437. return 0;
  438. out_poweroff:
  439. mma9551_set_device_state(client, false);
  440. return ret;
  441. }
  442. static int mma9551_remove(struct i2c_client *client)
  443. {
  444. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  445. struct mma9551_data *data = iio_priv(indio_dev);
  446. iio_device_unregister(indio_dev);
  447. pm_runtime_disable(&client->dev);
  448. pm_runtime_set_suspended(&client->dev);
  449. pm_runtime_put_noidle(&client->dev);
  450. mutex_lock(&data->mutex);
  451. mma9551_set_device_state(data->client, false);
  452. mutex_unlock(&data->mutex);
  453. return 0;
  454. }
  455. #ifdef CONFIG_PM
  456. static int mma9551_runtime_suspend(struct device *dev)
  457. {
  458. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  459. struct mma9551_data *data = iio_priv(indio_dev);
  460. int ret;
  461. mutex_lock(&data->mutex);
  462. ret = mma9551_set_device_state(data->client, false);
  463. mutex_unlock(&data->mutex);
  464. if (ret < 0) {
  465. dev_err(&data->client->dev, "powering off device failed\n");
  466. return -EAGAIN;
  467. }
  468. return 0;
  469. }
  470. static int mma9551_runtime_resume(struct device *dev)
  471. {
  472. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  473. struct mma9551_data *data = iio_priv(indio_dev);
  474. int ret;
  475. ret = mma9551_set_device_state(data->client, true);
  476. if (ret < 0)
  477. return ret;
  478. mma9551_sleep(MMA9551_DEFAULT_SAMPLE_RATE);
  479. return 0;
  480. }
  481. #endif
  482. #ifdef CONFIG_PM_SLEEP
  483. static int mma9551_suspend(struct device *dev)
  484. {
  485. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  486. struct mma9551_data *data = iio_priv(indio_dev);
  487. int ret;
  488. mutex_lock(&data->mutex);
  489. ret = mma9551_set_device_state(data->client, false);
  490. mutex_unlock(&data->mutex);
  491. return ret;
  492. }
  493. static int mma9551_resume(struct device *dev)
  494. {
  495. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  496. struct mma9551_data *data = iio_priv(indio_dev);
  497. int ret;
  498. mutex_lock(&data->mutex);
  499. ret = mma9551_set_device_state(data->client, true);
  500. mutex_unlock(&data->mutex);
  501. return ret;
  502. }
  503. #endif
  504. static const struct dev_pm_ops mma9551_pm_ops = {
  505. SET_SYSTEM_SLEEP_PM_OPS(mma9551_suspend, mma9551_resume)
  506. SET_RUNTIME_PM_OPS(mma9551_runtime_suspend,
  507. mma9551_runtime_resume, NULL)
  508. };
  509. static const struct acpi_device_id mma9551_acpi_match[] = {
  510. {"MMA9551", 0},
  511. {},
  512. };
  513. MODULE_DEVICE_TABLE(acpi, mma9551_acpi_match);
  514. static const struct i2c_device_id mma9551_id[] = {
  515. {"mma9551", 0},
  516. {}
  517. };
  518. MODULE_DEVICE_TABLE(i2c, mma9551_id);
  519. static struct i2c_driver mma9551_driver = {
  520. .driver = {
  521. .name = MMA9551_DRV_NAME,
  522. .acpi_match_table = ACPI_PTR(mma9551_acpi_match),
  523. .pm = &mma9551_pm_ops,
  524. },
  525. .probe = mma9551_probe,
  526. .remove = mma9551_remove,
  527. .id_table = mma9551_id,
  528. };
  529. module_i2c_driver(mma9551_driver);
  530. MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
  531. MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
  532. MODULE_LICENSE("GPL v2");
  533. MODULE_DESCRIPTION("MMA9551L motion-sensing platform driver");