mma9551.c 15 KB

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