mma9553.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*
  2. * Freescale MMA9553L Intelligent Pedometer 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/gpio/consumer.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/sysfs.h>
  22. #include <linux/iio/events.h>
  23. #include <linux/pm_runtime.h>
  24. #include "mma9551_core.h"
  25. #define MMA9553_DRV_NAME "mma9553"
  26. #define MMA9553_IRQ_NAME "mma9553_event"
  27. /* Pedometer configuration registers (R/W) */
  28. #define MMA9553_REG_CONF_SLEEPMIN 0x00
  29. #define MMA9553_REG_CONF_SLEEPMAX 0x02
  30. #define MMA9553_REG_CONF_SLEEPTHD 0x04
  31. #define MMA9553_MASK_CONF_WORD GENMASK(15, 0)
  32. #define MMA9553_REG_CONF_CONF_STEPLEN 0x06
  33. #define MMA9553_MASK_CONF_CONFIG BIT(15)
  34. #define MMA9553_MASK_CONF_ACT_DBCNTM BIT(14)
  35. #define MMA9553_MASK_CONF_SLP_DBCNTM BIT(13)
  36. #define MMA9553_MASK_CONF_STEPLEN GENMASK(7, 0)
  37. #define MMA9553_REG_CONF_HEIGHT_WEIGHT 0x08
  38. #define MMA9553_MASK_CONF_HEIGHT GENMASK(15, 8)
  39. #define MMA9553_MASK_CONF_WEIGHT GENMASK(7, 0)
  40. #define MMA9553_REG_CONF_FILTER 0x0A
  41. #define MMA9553_MASK_CONF_FILTSTEP GENMASK(15, 8)
  42. #define MMA9553_MASK_CONF_MALE BIT(7)
  43. #define MMA9553_MASK_CONF_FILTTIME GENMASK(6, 0)
  44. #define MMA9553_REG_CONF_SPEED_STEP 0x0C
  45. #define MMA9553_MASK_CONF_SPDPRD GENMASK(15, 8)
  46. #define MMA9553_MASK_CONF_STEPCOALESCE GENMASK(7, 0)
  47. #define MMA9553_REG_CONF_ACTTHD 0x0E
  48. #define MMA9553_MAX_ACTTHD GENMASK(15, 0)
  49. /* Pedometer status registers (R-only) */
  50. #define MMA9553_REG_STATUS 0x00
  51. #define MMA9553_MASK_STATUS_MRGFL BIT(15)
  52. #define MMA9553_MASK_STATUS_SUSPCHG BIT(14)
  53. #define MMA9553_MASK_STATUS_STEPCHG BIT(13)
  54. #define MMA9553_MASK_STATUS_ACTCHG BIT(12)
  55. #define MMA9553_MASK_STATUS_SUSP BIT(11)
  56. #define MMA9553_MASK_STATUS_ACTIVITY GENMASK(10, 8)
  57. #define MMA9553_MASK_STATUS_VERSION GENMASK(7, 0)
  58. #define MMA9553_REG_STEPCNT 0x02
  59. #define MMA9553_REG_DISTANCE 0x04
  60. #define MMA9553_REG_SPEED 0x06
  61. #define MMA9553_REG_CALORIES 0x08
  62. #define MMA9553_REG_SLEEPCNT 0x0A
  63. /* Pedometer events are always mapped to this pin. */
  64. #define MMA9553_DEFAULT_GPIO_PIN mma9551_gpio6
  65. #define MMA9553_DEFAULT_GPIO_POLARITY 0
  66. /* Bitnum used for GPIO configuration = bit number in high status byte */
  67. #define MMA9553_STATUS_TO_BITNUM(bit) (ffs(bit) - 9)
  68. #define MMA9553_MAX_BITNUM MMA9553_STATUS_TO_BITNUM(BIT(16))
  69. #define MMA9553_DEFAULT_SAMPLE_RATE 30 /* Hz */
  70. /*
  71. * The internal activity level must be stable for ACTTHD samples before
  72. * ACTIVITY is updated. The ACTIVITY variable contains the current activity
  73. * level and is updated every time a step is detected or once a second
  74. * if there are no steps.
  75. */
  76. #define MMA9553_ACTIVITY_THD_TO_SEC(thd) ((thd) / MMA9553_DEFAULT_SAMPLE_RATE)
  77. #define MMA9553_ACTIVITY_SEC_TO_THD(sec) ((sec) * MMA9553_DEFAULT_SAMPLE_RATE)
  78. /*
  79. * Autonomously suspend pedometer if acceleration vector magnitude
  80. * is near 1g (4096 at 0.244 mg/LSB resolution) for 30 seconds.
  81. */
  82. #define MMA9553_DEFAULT_SLEEPMIN 3688 /* 0,9 g */
  83. #define MMA9553_DEFAULT_SLEEPMAX 4508 /* 1,1 g */
  84. #define MMA9553_DEFAULT_SLEEPTHD (MMA9553_DEFAULT_SAMPLE_RATE * 30)
  85. #define MMA9553_CONFIG_RETRIES 2
  86. /* Status register - activity field */
  87. enum activity_level {
  88. ACTIVITY_UNKNOWN,
  89. ACTIVITY_REST,
  90. ACTIVITY_WALKING,
  91. ACTIVITY_JOGGING,
  92. ACTIVITY_RUNNING,
  93. };
  94. static struct mma9553_event_info {
  95. enum iio_chan_type type;
  96. enum iio_modifier mod;
  97. enum iio_event_direction dir;
  98. } mma9553_events_info[] = {
  99. {
  100. .type = IIO_STEPS,
  101. .mod = IIO_NO_MOD,
  102. .dir = IIO_EV_DIR_NONE,
  103. },
  104. {
  105. .type = IIO_ACTIVITY,
  106. .mod = IIO_MOD_STILL,
  107. .dir = IIO_EV_DIR_RISING,
  108. },
  109. {
  110. .type = IIO_ACTIVITY,
  111. .mod = IIO_MOD_STILL,
  112. .dir = IIO_EV_DIR_FALLING,
  113. },
  114. {
  115. .type = IIO_ACTIVITY,
  116. .mod = IIO_MOD_WALKING,
  117. .dir = IIO_EV_DIR_RISING,
  118. },
  119. {
  120. .type = IIO_ACTIVITY,
  121. .mod = IIO_MOD_WALKING,
  122. .dir = IIO_EV_DIR_FALLING,
  123. },
  124. {
  125. .type = IIO_ACTIVITY,
  126. .mod = IIO_MOD_JOGGING,
  127. .dir = IIO_EV_DIR_RISING,
  128. },
  129. {
  130. .type = IIO_ACTIVITY,
  131. .mod = IIO_MOD_JOGGING,
  132. .dir = IIO_EV_DIR_FALLING,
  133. },
  134. {
  135. .type = IIO_ACTIVITY,
  136. .mod = IIO_MOD_RUNNING,
  137. .dir = IIO_EV_DIR_RISING,
  138. },
  139. {
  140. .type = IIO_ACTIVITY,
  141. .mod = IIO_MOD_RUNNING,
  142. .dir = IIO_EV_DIR_FALLING,
  143. },
  144. };
  145. #define MMA9553_EVENTS_INFO_SIZE ARRAY_SIZE(mma9553_events_info)
  146. struct mma9553_event {
  147. struct mma9553_event_info *info;
  148. bool enabled;
  149. };
  150. struct mma9553_conf_regs {
  151. u16 sleepmin;
  152. u16 sleepmax;
  153. u16 sleepthd;
  154. u16 config;
  155. u16 height_weight;
  156. u16 filter;
  157. u16 speed_step;
  158. u16 actthd;
  159. } __packed;
  160. struct mma9553_data {
  161. struct i2c_client *client;
  162. /*
  163. * 1. Serialize access to HW (requested by mma9551_core API).
  164. * 2. Serialize sequences that power on/off the device and access HW.
  165. */
  166. struct mutex mutex;
  167. struct mma9553_conf_regs conf;
  168. struct mma9553_event events[MMA9553_EVENTS_INFO_SIZE];
  169. int num_events;
  170. u8 gpio_bitnum;
  171. /*
  172. * This is used for all features that depend on step count:
  173. * step count, distance, speed, calories.
  174. */
  175. bool stepcnt_enabled;
  176. u16 stepcnt;
  177. u8 activity;
  178. s64 timestamp;
  179. };
  180. static u8 mma9553_get_bits(u16 val, u16 mask)
  181. {
  182. return (val & mask) >> (ffs(mask) - 1);
  183. }
  184. static u16 mma9553_set_bits(u16 current_val, u16 val, u16 mask)
  185. {
  186. return (current_val & ~mask) | (val << (ffs(mask) - 1));
  187. }
  188. static enum iio_modifier mma9553_activity_to_mod(enum activity_level activity)
  189. {
  190. switch (activity) {
  191. case ACTIVITY_RUNNING:
  192. return IIO_MOD_RUNNING;
  193. case ACTIVITY_JOGGING:
  194. return IIO_MOD_JOGGING;
  195. case ACTIVITY_WALKING:
  196. return IIO_MOD_WALKING;
  197. case ACTIVITY_REST:
  198. return IIO_MOD_STILL;
  199. case ACTIVITY_UNKNOWN:
  200. default:
  201. return IIO_NO_MOD;
  202. }
  203. }
  204. static void mma9553_init_events(struct mma9553_data *data)
  205. {
  206. int i;
  207. data->num_events = MMA9553_EVENTS_INFO_SIZE;
  208. for (i = 0; i < data->num_events; i++) {
  209. data->events[i].info = &mma9553_events_info[i];
  210. data->events[i].enabled = false;
  211. }
  212. }
  213. static struct mma9553_event *mma9553_get_event(struct mma9553_data *data,
  214. enum iio_chan_type type,
  215. enum iio_modifier mod,
  216. enum iio_event_direction dir)
  217. {
  218. int i;
  219. for (i = 0; i < data->num_events; i++)
  220. if (data->events[i].info->type == type &&
  221. data->events[i].info->mod == mod &&
  222. data->events[i].info->dir == dir)
  223. return &data->events[i];
  224. return NULL;
  225. }
  226. static bool mma9553_is_any_event_enabled(struct mma9553_data *data,
  227. bool check_type,
  228. enum iio_chan_type type)
  229. {
  230. int i;
  231. for (i = 0; i < data->num_events; i++)
  232. if ((check_type && data->events[i].info->type == type &&
  233. data->events[i].enabled) ||
  234. (!check_type && data->events[i].enabled))
  235. return true;
  236. return false;
  237. }
  238. static int mma9553_set_config(struct mma9553_data *data, u16 reg,
  239. u16 *p_reg_val, u16 val, u16 mask)
  240. {
  241. int ret, retries;
  242. u16 reg_val, config;
  243. reg_val = *p_reg_val;
  244. if (val == mma9553_get_bits(reg_val, mask))
  245. return 0;
  246. reg_val = mma9553_set_bits(reg_val, val, mask);
  247. ret = mma9551_write_config_word(data->client, MMA9551_APPID_PEDOMETER,
  248. reg, reg_val);
  249. if (ret < 0) {
  250. dev_err(&data->client->dev,
  251. "error writing config register 0x%x\n", reg);
  252. return ret;
  253. }
  254. *p_reg_val = reg_val;
  255. /* Reinitializes the pedometer with current configuration values */
  256. config = mma9553_set_bits(data->conf.config, 1,
  257. MMA9553_MASK_CONF_CONFIG);
  258. ret = mma9551_write_config_word(data->client, MMA9551_APPID_PEDOMETER,
  259. MMA9553_REG_CONF_CONF_STEPLEN, config);
  260. if (ret < 0) {
  261. dev_err(&data->client->dev,
  262. "error writing config register 0x%x\n",
  263. MMA9553_REG_CONF_CONF_STEPLEN);
  264. return ret;
  265. }
  266. retries = MMA9553_CONFIG_RETRIES;
  267. do {
  268. mma9551_sleep(MMA9553_DEFAULT_SAMPLE_RATE);
  269. ret = mma9551_read_config_word(data->client,
  270. MMA9551_APPID_PEDOMETER,
  271. MMA9553_REG_CONF_CONF_STEPLEN,
  272. &config);
  273. if (ret < 0)
  274. return ret;
  275. } while (mma9553_get_bits(config, MMA9553_MASK_CONF_CONFIG) &&
  276. --retries > 0);
  277. return 0;
  278. }
  279. static int mma9553_read_activity_stepcnt(struct mma9553_data *data,
  280. u8 *activity, u16 *stepcnt)
  281. {
  282. u16 buf[2];
  283. int ret;
  284. ret = mma9551_read_status_words(data->client, MMA9551_APPID_PEDOMETER,
  285. MMA9553_REG_STATUS, ARRAY_SIZE(buf),
  286. buf);
  287. if (ret < 0) {
  288. dev_err(&data->client->dev,
  289. "error reading status and stepcnt\n");
  290. return ret;
  291. }
  292. *activity = mma9553_get_bits(buf[0], MMA9553_MASK_STATUS_ACTIVITY);
  293. *stepcnt = buf[1];
  294. return 0;
  295. }
  296. static int mma9553_conf_gpio(struct mma9553_data *data)
  297. {
  298. u8 bitnum = 0, appid = MMA9551_APPID_PEDOMETER;
  299. int ret;
  300. struct mma9553_event *ev_step_detect;
  301. bool activity_enabled;
  302. activity_enabled = mma9553_is_any_event_enabled(data, true,
  303. IIO_ACTIVITY);
  304. ev_step_detect = mma9553_get_event(data, IIO_STEPS, IIO_NO_MOD,
  305. IIO_EV_DIR_NONE);
  306. /*
  307. * If both step detector and activity are enabled, use the MRGFL bit.
  308. * This bit is the logical OR of the SUSPCHG, STEPCHG, and ACTCHG flags.
  309. */
  310. if (activity_enabled && ev_step_detect->enabled)
  311. bitnum = MMA9553_STATUS_TO_BITNUM(MMA9553_MASK_STATUS_MRGFL);
  312. else if (ev_step_detect->enabled)
  313. bitnum = MMA9553_STATUS_TO_BITNUM(MMA9553_MASK_STATUS_STEPCHG);
  314. else if (activity_enabled)
  315. bitnum = MMA9553_STATUS_TO_BITNUM(MMA9553_MASK_STATUS_ACTCHG);
  316. else /* Reset */
  317. appid = MMA9551_APPID_NONE;
  318. if (data->gpio_bitnum == bitnum)
  319. return 0;
  320. /* Save initial values for activity and stepcnt */
  321. if (activity_enabled || ev_step_detect->enabled) {
  322. ret = mma9553_read_activity_stepcnt(data, &data->activity,
  323. &data->stepcnt);
  324. if (ret < 0)
  325. return ret;
  326. }
  327. ret = mma9551_gpio_config(data->client, MMA9553_DEFAULT_GPIO_PIN, appid,
  328. bitnum, MMA9553_DEFAULT_GPIO_POLARITY);
  329. if (ret < 0)
  330. return ret;
  331. data->gpio_bitnum = bitnum;
  332. return 0;
  333. }
  334. static int mma9553_init(struct mma9553_data *data)
  335. {
  336. int ret;
  337. ret = mma9551_read_version(data->client);
  338. if (ret)
  339. return ret;
  340. /*
  341. * Read all the pedometer configuration registers. This is used as
  342. * a device identification command to differentiate the MMA9553L
  343. * from the MMA9550L.
  344. */
  345. ret = mma9551_read_config_words(data->client, MMA9551_APPID_PEDOMETER,
  346. MMA9553_REG_CONF_SLEEPMIN,
  347. sizeof(data->conf) / sizeof(u16),
  348. (u16 *)&data->conf);
  349. if (ret < 0) {
  350. dev_err(&data->client->dev,
  351. "failed to read configuration registers\n");
  352. return ret;
  353. }
  354. /* Reset GPIO */
  355. data->gpio_bitnum = MMA9553_MAX_BITNUM;
  356. ret = mma9553_conf_gpio(data);
  357. if (ret < 0)
  358. return ret;
  359. ret = mma9551_app_reset(data->client, MMA9551_RSC_PED);
  360. if (ret < 0)
  361. return ret;
  362. /* Init config registers */
  363. data->conf.sleepmin = MMA9553_DEFAULT_SLEEPMIN;
  364. data->conf.sleepmax = MMA9553_DEFAULT_SLEEPMAX;
  365. data->conf.sleepthd = MMA9553_DEFAULT_SLEEPTHD;
  366. data->conf.config = mma9553_set_bits(data->conf.config, 1,
  367. MMA9553_MASK_CONF_CONFIG);
  368. /*
  369. * Clear the activity debounce counter when the activity level changes,
  370. * so that the confidence level applies for any activity level.
  371. */
  372. data->conf.config = mma9553_set_bits(data->conf.config, 1,
  373. MMA9553_MASK_CONF_ACT_DBCNTM);
  374. ret = mma9551_write_config_words(data->client, MMA9551_APPID_PEDOMETER,
  375. MMA9553_REG_CONF_SLEEPMIN,
  376. sizeof(data->conf) / sizeof(u16),
  377. (u16 *)&data->conf);
  378. if (ret < 0) {
  379. dev_err(&data->client->dev,
  380. "failed to write configuration registers\n");
  381. return ret;
  382. }
  383. return mma9551_set_device_state(data->client, true);
  384. }
  385. static int mma9553_read_status_word(struct mma9553_data *data, u16 reg,
  386. u16 *tmp)
  387. {
  388. bool powered_on;
  389. int ret;
  390. /*
  391. * The HW only counts steps and other dependent
  392. * parameters (speed, distance, calories, activity)
  393. * if power is on (from enabling an event or the
  394. * step counter).
  395. */
  396. powered_on = mma9553_is_any_event_enabled(data, false, 0) ||
  397. data->stepcnt_enabled;
  398. if (!powered_on) {
  399. dev_err(&data->client->dev, "No channels enabled\n");
  400. return -EINVAL;
  401. }
  402. mutex_lock(&data->mutex);
  403. ret = mma9551_read_status_word(data->client, MMA9551_APPID_PEDOMETER,
  404. reg, tmp);
  405. mutex_unlock(&data->mutex);
  406. return ret;
  407. }
  408. static int mma9553_read_raw(struct iio_dev *indio_dev,
  409. struct iio_chan_spec const *chan,
  410. int *val, int *val2, long mask)
  411. {
  412. struct mma9553_data *data = iio_priv(indio_dev);
  413. int ret;
  414. u16 tmp;
  415. u8 activity;
  416. switch (mask) {
  417. case IIO_CHAN_INFO_PROCESSED:
  418. switch (chan->type) {
  419. case IIO_STEPS:
  420. ret = mma9553_read_status_word(data,
  421. MMA9553_REG_STEPCNT,
  422. &tmp);
  423. if (ret < 0)
  424. return ret;
  425. *val = tmp;
  426. return IIO_VAL_INT;
  427. case IIO_DISTANCE:
  428. ret = mma9553_read_status_word(data,
  429. MMA9553_REG_DISTANCE,
  430. &tmp);
  431. if (ret < 0)
  432. return ret;
  433. *val = tmp;
  434. return IIO_VAL_INT;
  435. case IIO_ACTIVITY:
  436. ret = mma9553_read_status_word(data,
  437. MMA9553_REG_STATUS,
  438. &tmp);
  439. if (ret < 0)
  440. return ret;
  441. activity =
  442. mma9553_get_bits(tmp, MMA9553_MASK_STATUS_ACTIVITY);
  443. /*
  444. * The device does not support confidence value levels,
  445. * so we will always have 100% for current activity and
  446. * 0% for the others.
  447. */
  448. if (chan->channel2 == mma9553_activity_to_mod(activity))
  449. *val = 100;
  450. else
  451. *val = 0;
  452. return IIO_VAL_INT;
  453. default:
  454. return -EINVAL;
  455. }
  456. case IIO_CHAN_INFO_RAW:
  457. switch (chan->type) {
  458. case IIO_VELOCITY: /* m/h */
  459. if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
  460. return -EINVAL;
  461. ret = mma9553_read_status_word(data,
  462. MMA9553_REG_SPEED,
  463. &tmp);
  464. if (ret < 0)
  465. return ret;
  466. *val = tmp;
  467. return IIO_VAL_INT;
  468. case IIO_ENERGY: /* Cal or kcal */
  469. ret = mma9553_read_status_word(data,
  470. MMA9553_REG_CALORIES,
  471. &tmp);
  472. if (ret < 0)
  473. return ret;
  474. *val = tmp;
  475. return IIO_VAL_INT;
  476. case IIO_ACCEL:
  477. mutex_lock(&data->mutex);
  478. ret = mma9551_read_accel_chan(data->client,
  479. chan, val, val2);
  480. mutex_unlock(&data->mutex);
  481. return ret;
  482. default:
  483. return -EINVAL;
  484. }
  485. case IIO_CHAN_INFO_SCALE:
  486. switch (chan->type) {
  487. case IIO_VELOCITY: /* m/h to m/s */
  488. if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
  489. return -EINVAL;
  490. *val = 0;
  491. *val2 = 277; /* 0.000277 */
  492. return IIO_VAL_INT_PLUS_MICRO;
  493. case IIO_ENERGY: /* Cal or kcal to J */
  494. *val = 4184;
  495. return IIO_VAL_INT;
  496. case IIO_ACCEL:
  497. return mma9551_read_accel_scale(val, val2);
  498. default:
  499. return -EINVAL;
  500. }
  501. case IIO_CHAN_INFO_ENABLE:
  502. *val = data->stepcnt_enabled;
  503. return IIO_VAL_INT;
  504. case IIO_CHAN_INFO_CALIBHEIGHT:
  505. tmp = mma9553_get_bits(data->conf.height_weight,
  506. MMA9553_MASK_CONF_HEIGHT);
  507. *val = tmp / 100; /* cm to m */
  508. *val2 = (tmp % 100) * 10000;
  509. return IIO_VAL_INT_PLUS_MICRO;
  510. case IIO_CHAN_INFO_CALIBWEIGHT:
  511. *val = mma9553_get_bits(data->conf.height_weight,
  512. MMA9553_MASK_CONF_WEIGHT);
  513. return IIO_VAL_INT;
  514. case IIO_CHAN_INFO_DEBOUNCE_COUNT:
  515. switch (chan->type) {
  516. case IIO_STEPS:
  517. *val = mma9553_get_bits(data->conf.filter,
  518. MMA9553_MASK_CONF_FILTSTEP);
  519. return IIO_VAL_INT;
  520. default:
  521. return -EINVAL;
  522. }
  523. case IIO_CHAN_INFO_DEBOUNCE_TIME:
  524. switch (chan->type) {
  525. case IIO_STEPS:
  526. *val = mma9553_get_bits(data->conf.filter,
  527. MMA9553_MASK_CONF_FILTTIME);
  528. return IIO_VAL_INT;
  529. default:
  530. return -EINVAL;
  531. }
  532. case IIO_CHAN_INFO_INT_TIME:
  533. switch (chan->type) {
  534. case IIO_VELOCITY:
  535. if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
  536. return -EINVAL;
  537. *val = mma9553_get_bits(data->conf.speed_step,
  538. MMA9553_MASK_CONF_SPDPRD);
  539. return IIO_VAL_INT;
  540. default:
  541. return -EINVAL;
  542. }
  543. default:
  544. return -EINVAL;
  545. }
  546. }
  547. static int mma9553_write_raw(struct iio_dev *indio_dev,
  548. struct iio_chan_spec const *chan,
  549. int val, int val2, long mask)
  550. {
  551. struct mma9553_data *data = iio_priv(indio_dev);
  552. int ret, tmp;
  553. switch (mask) {
  554. case IIO_CHAN_INFO_ENABLE:
  555. if (data->stepcnt_enabled == !!val)
  556. return 0;
  557. mutex_lock(&data->mutex);
  558. ret = mma9551_set_power_state(data->client, val);
  559. if (ret < 0) {
  560. mutex_unlock(&data->mutex);
  561. return ret;
  562. }
  563. data->stepcnt_enabled = val;
  564. mutex_unlock(&data->mutex);
  565. return 0;
  566. case IIO_CHAN_INFO_CALIBHEIGHT:
  567. /* m to cm */
  568. tmp = val * 100 + val2 / 10000;
  569. if (tmp < 0 || tmp > 255)
  570. return -EINVAL;
  571. mutex_lock(&data->mutex);
  572. ret = mma9553_set_config(data,
  573. MMA9553_REG_CONF_HEIGHT_WEIGHT,
  574. &data->conf.height_weight,
  575. tmp, MMA9553_MASK_CONF_HEIGHT);
  576. mutex_unlock(&data->mutex);
  577. return ret;
  578. case IIO_CHAN_INFO_CALIBWEIGHT:
  579. if (val < 0 || val > 255)
  580. return -EINVAL;
  581. mutex_lock(&data->mutex);
  582. ret = mma9553_set_config(data,
  583. MMA9553_REG_CONF_HEIGHT_WEIGHT,
  584. &data->conf.height_weight,
  585. val, MMA9553_MASK_CONF_WEIGHT);
  586. mutex_unlock(&data->mutex);
  587. return ret;
  588. case IIO_CHAN_INFO_DEBOUNCE_COUNT:
  589. switch (chan->type) {
  590. case IIO_STEPS:
  591. /*
  592. * Set to 0 to disable step filtering. If the value
  593. * specified is greater than 6, then 6 will be used.
  594. */
  595. if (val < 0)
  596. return -EINVAL;
  597. if (val > 6)
  598. val = 6;
  599. mutex_lock(&data->mutex);
  600. ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER,
  601. &data->conf.filter, val,
  602. MMA9553_MASK_CONF_FILTSTEP);
  603. mutex_unlock(&data->mutex);
  604. return ret;
  605. default:
  606. return -EINVAL;
  607. }
  608. case IIO_CHAN_INFO_DEBOUNCE_TIME:
  609. switch (chan->type) {
  610. case IIO_STEPS:
  611. if (val < 0 || val > 127)
  612. return -EINVAL;
  613. mutex_lock(&data->mutex);
  614. ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER,
  615. &data->conf.filter, val,
  616. MMA9553_MASK_CONF_FILTTIME);
  617. mutex_unlock(&data->mutex);
  618. return ret;
  619. default:
  620. return -EINVAL;
  621. }
  622. case IIO_CHAN_INFO_INT_TIME:
  623. switch (chan->type) {
  624. case IIO_VELOCITY:
  625. if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
  626. return -EINVAL;
  627. /*
  628. * If set to a value greater than 5, then 5 will be
  629. * used. Warning: Do not set SPDPRD to 0 or 1 as
  630. * this may cause undesirable behavior.
  631. */
  632. if (val < 2)
  633. return -EINVAL;
  634. if (val > 5)
  635. val = 5;
  636. mutex_lock(&data->mutex);
  637. ret = mma9553_set_config(data,
  638. MMA9553_REG_CONF_SPEED_STEP,
  639. &data->conf.speed_step, val,
  640. MMA9553_MASK_CONF_SPDPRD);
  641. mutex_unlock(&data->mutex);
  642. return ret;
  643. default:
  644. return -EINVAL;
  645. }
  646. default:
  647. return -EINVAL;
  648. }
  649. }
  650. static int mma9553_read_event_config(struct iio_dev *indio_dev,
  651. const struct iio_chan_spec *chan,
  652. enum iio_event_type type,
  653. enum iio_event_direction dir)
  654. {
  655. struct mma9553_data *data = iio_priv(indio_dev);
  656. struct mma9553_event *event;
  657. event = mma9553_get_event(data, chan->type, chan->channel2, dir);
  658. if (!event)
  659. return -EINVAL;
  660. return event->enabled;
  661. }
  662. static int mma9553_write_event_config(struct iio_dev *indio_dev,
  663. const struct iio_chan_spec *chan,
  664. enum iio_event_type type,
  665. enum iio_event_direction dir, int state)
  666. {
  667. struct mma9553_data *data = iio_priv(indio_dev);
  668. struct mma9553_event *event;
  669. int ret;
  670. event = mma9553_get_event(data, chan->type, chan->channel2, dir);
  671. if (!event)
  672. return -EINVAL;
  673. if (event->enabled == state)
  674. return 0;
  675. mutex_lock(&data->mutex);
  676. ret = mma9551_set_power_state(data->client, state);
  677. if (ret < 0)
  678. goto err_out;
  679. event->enabled = state;
  680. ret = mma9553_conf_gpio(data);
  681. if (ret < 0)
  682. goto err_conf_gpio;
  683. mutex_unlock(&data->mutex);
  684. return 0;
  685. err_conf_gpio:
  686. if (state) {
  687. event->enabled = false;
  688. mma9551_set_power_state(data->client, false);
  689. }
  690. err_out:
  691. mutex_unlock(&data->mutex);
  692. return ret;
  693. }
  694. static int mma9553_read_event_value(struct iio_dev *indio_dev,
  695. const struct iio_chan_spec *chan,
  696. enum iio_event_type type,
  697. enum iio_event_direction dir,
  698. enum iio_event_info info,
  699. int *val, int *val2)
  700. {
  701. struct mma9553_data *data = iio_priv(indio_dev);
  702. *val2 = 0;
  703. switch (info) {
  704. case IIO_EV_INFO_VALUE:
  705. switch (chan->type) {
  706. case IIO_STEPS:
  707. *val = mma9553_get_bits(data->conf.speed_step,
  708. MMA9553_MASK_CONF_STEPCOALESCE);
  709. return IIO_VAL_INT;
  710. case IIO_ACTIVITY:
  711. /*
  712. * The device does not support confidence value levels.
  713. * We set an average of 50%.
  714. */
  715. *val = 50;
  716. return IIO_VAL_INT;
  717. default:
  718. return -EINVAL;
  719. }
  720. case IIO_EV_INFO_PERIOD:
  721. switch (chan->type) {
  722. case IIO_ACTIVITY:
  723. *val = MMA9553_ACTIVITY_THD_TO_SEC(data->conf.actthd);
  724. return IIO_VAL_INT;
  725. default:
  726. return -EINVAL;
  727. }
  728. default:
  729. return -EINVAL;
  730. }
  731. }
  732. static int mma9553_write_event_value(struct iio_dev *indio_dev,
  733. const struct iio_chan_spec *chan,
  734. enum iio_event_type type,
  735. enum iio_event_direction dir,
  736. enum iio_event_info info,
  737. int val, int val2)
  738. {
  739. struct mma9553_data *data = iio_priv(indio_dev);
  740. int ret;
  741. switch (info) {
  742. case IIO_EV_INFO_VALUE:
  743. switch (chan->type) {
  744. case IIO_STEPS:
  745. if (val < 0 || val > 255)
  746. return -EINVAL;
  747. mutex_lock(&data->mutex);
  748. ret = mma9553_set_config(data,
  749. MMA9553_REG_CONF_SPEED_STEP,
  750. &data->conf.speed_step, val,
  751. MMA9553_MASK_CONF_STEPCOALESCE);
  752. mutex_unlock(&data->mutex);
  753. return ret;
  754. default:
  755. return -EINVAL;
  756. }
  757. case IIO_EV_INFO_PERIOD:
  758. switch (chan->type) {
  759. case IIO_ACTIVITY:
  760. if (val < 0 || val > MMA9553_ACTIVITY_THD_TO_SEC(
  761. MMA9553_MAX_ACTTHD))
  762. return -EINVAL;
  763. mutex_lock(&data->mutex);
  764. ret = mma9553_set_config(data, MMA9553_REG_CONF_ACTTHD,
  765. &data->conf.actthd,
  766. MMA9553_ACTIVITY_SEC_TO_THD
  767. (val), MMA9553_MASK_CONF_WORD);
  768. mutex_unlock(&data->mutex);
  769. return ret;
  770. default:
  771. return -EINVAL;
  772. }
  773. default:
  774. return -EINVAL;
  775. }
  776. }
  777. static int mma9553_get_calibgender_mode(struct iio_dev *indio_dev,
  778. const struct iio_chan_spec *chan)
  779. {
  780. struct mma9553_data *data = iio_priv(indio_dev);
  781. u8 gender;
  782. gender = mma9553_get_bits(data->conf.filter, MMA9553_MASK_CONF_MALE);
  783. /*
  784. * HW expects 0 for female and 1 for male,
  785. * while iio index is 0 for male and 1 for female.
  786. */
  787. return !gender;
  788. }
  789. static int mma9553_set_calibgender_mode(struct iio_dev *indio_dev,
  790. const struct iio_chan_spec *chan,
  791. unsigned int mode)
  792. {
  793. struct mma9553_data *data = iio_priv(indio_dev);
  794. u8 gender = !mode;
  795. int ret;
  796. if ((mode != 0) && (mode != 1))
  797. return -EINVAL;
  798. mutex_lock(&data->mutex);
  799. ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER,
  800. &data->conf.filter, gender,
  801. MMA9553_MASK_CONF_MALE);
  802. mutex_unlock(&data->mutex);
  803. return ret;
  804. }
  805. static const struct iio_event_spec mma9553_step_event = {
  806. .type = IIO_EV_TYPE_CHANGE,
  807. .dir = IIO_EV_DIR_NONE,
  808. .mask_separate = BIT(IIO_EV_INFO_ENABLE) | BIT(IIO_EV_INFO_VALUE),
  809. };
  810. static const struct iio_event_spec mma9553_activity_events[] = {
  811. {
  812. .type = IIO_EV_TYPE_THRESH,
  813. .dir = IIO_EV_DIR_RISING,
  814. .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
  815. BIT(IIO_EV_INFO_VALUE) |
  816. BIT(IIO_EV_INFO_PERIOD),
  817. },
  818. {
  819. .type = IIO_EV_TYPE_THRESH,
  820. .dir = IIO_EV_DIR_FALLING,
  821. .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
  822. BIT(IIO_EV_INFO_VALUE) |
  823. BIT(IIO_EV_INFO_PERIOD),
  824. },
  825. };
  826. static const char * const mma9553_calibgender_modes[] = { "male", "female" };
  827. static const struct iio_enum mma9553_calibgender_enum = {
  828. .items = mma9553_calibgender_modes,
  829. .num_items = ARRAY_SIZE(mma9553_calibgender_modes),
  830. .get = mma9553_get_calibgender_mode,
  831. .set = mma9553_set_calibgender_mode,
  832. };
  833. static const struct iio_chan_spec_ext_info mma9553_ext_info[] = {
  834. IIO_ENUM("calibgender", IIO_SHARED_BY_TYPE, &mma9553_calibgender_enum),
  835. IIO_ENUM_AVAILABLE("calibgender", &mma9553_calibgender_enum),
  836. {},
  837. };
  838. #define MMA9553_PEDOMETER_CHANNEL(_type, _mask) { \
  839. .type = _type, \
  840. .info_mask_separate = BIT(IIO_CHAN_INFO_ENABLE) | \
  841. BIT(IIO_CHAN_INFO_CALIBHEIGHT) | \
  842. _mask, \
  843. .ext_info = mma9553_ext_info, \
  844. }
  845. #define MMA9553_ACTIVITY_CHANNEL(_chan2) { \
  846. .type = IIO_ACTIVITY, \
  847. .modified = 1, \
  848. .channel2 = _chan2, \
  849. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
  850. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBHEIGHT) | \
  851. BIT(IIO_CHAN_INFO_ENABLE), \
  852. .event_spec = mma9553_activity_events, \
  853. .num_event_specs = ARRAY_SIZE(mma9553_activity_events), \
  854. .ext_info = mma9553_ext_info, \
  855. }
  856. static const struct iio_chan_spec mma9553_channels[] = {
  857. MMA9551_ACCEL_CHANNEL(IIO_MOD_X),
  858. MMA9551_ACCEL_CHANNEL(IIO_MOD_Y),
  859. MMA9551_ACCEL_CHANNEL(IIO_MOD_Z),
  860. {
  861. .type = IIO_STEPS,
  862. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  863. BIT(IIO_CHAN_INFO_ENABLE) |
  864. BIT(IIO_CHAN_INFO_DEBOUNCE_COUNT) |
  865. BIT(IIO_CHAN_INFO_DEBOUNCE_TIME),
  866. .event_spec = &mma9553_step_event,
  867. .num_event_specs = 1,
  868. },
  869. MMA9553_PEDOMETER_CHANNEL(IIO_DISTANCE, BIT(IIO_CHAN_INFO_PROCESSED)),
  870. {
  871. .type = IIO_VELOCITY,
  872. .modified = 1,
  873. .channel2 = IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z,
  874. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  875. BIT(IIO_CHAN_INFO_SCALE) |
  876. BIT(IIO_CHAN_INFO_INT_TIME) |
  877. BIT(IIO_CHAN_INFO_ENABLE),
  878. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBHEIGHT),
  879. .ext_info = mma9553_ext_info,
  880. },
  881. MMA9553_PEDOMETER_CHANNEL(IIO_ENERGY, BIT(IIO_CHAN_INFO_RAW) |
  882. BIT(IIO_CHAN_INFO_SCALE) |
  883. BIT(IIO_CHAN_INFO_CALIBWEIGHT)),
  884. MMA9553_ACTIVITY_CHANNEL(IIO_MOD_RUNNING),
  885. MMA9553_ACTIVITY_CHANNEL(IIO_MOD_JOGGING),
  886. MMA9553_ACTIVITY_CHANNEL(IIO_MOD_WALKING),
  887. MMA9553_ACTIVITY_CHANNEL(IIO_MOD_STILL),
  888. };
  889. static const struct iio_info mma9553_info = {
  890. .driver_module = THIS_MODULE,
  891. .read_raw = mma9553_read_raw,
  892. .write_raw = mma9553_write_raw,
  893. .read_event_config = mma9553_read_event_config,
  894. .write_event_config = mma9553_write_event_config,
  895. .read_event_value = mma9553_read_event_value,
  896. .write_event_value = mma9553_write_event_value,
  897. };
  898. static irqreturn_t mma9553_irq_handler(int irq, void *private)
  899. {
  900. struct iio_dev *indio_dev = private;
  901. struct mma9553_data *data = iio_priv(indio_dev);
  902. data->timestamp = iio_get_time_ns();
  903. /*
  904. * Since we only configure the interrupt pin when an
  905. * event is enabled, we are sure we have at least
  906. * one event enabled at this point.
  907. */
  908. return IRQ_WAKE_THREAD;
  909. }
  910. static irqreturn_t mma9553_event_handler(int irq, void *private)
  911. {
  912. struct iio_dev *indio_dev = private;
  913. struct mma9553_data *data = iio_priv(indio_dev);
  914. u16 stepcnt;
  915. u8 activity;
  916. struct mma9553_event *ev_activity, *ev_prev_activity, *ev_step_detect;
  917. int ret;
  918. mutex_lock(&data->mutex);
  919. ret = mma9553_read_activity_stepcnt(data, &activity, &stepcnt);
  920. if (ret < 0) {
  921. mutex_unlock(&data->mutex);
  922. return IRQ_HANDLED;
  923. }
  924. ev_prev_activity = mma9553_get_event(data, IIO_ACTIVITY,
  925. mma9553_activity_to_mod(
  926. data->activity),
  927. IIO_EV_DIR_FALLING);
  928. ev_activity = mma9553_get_event(data, IIO_ACTIVITY,
  929. mma9553_activity_to_mod(activity),
  930. IIO_EV_DIR_RISING);
  931. ev_step_detect = mma9553_get_event(data, IIO_STEPS, IIO_NO_MOD,
  932. IIO_EV_DIR_NONE);
  933. if (ev_step_detect->enabled && (stepcnt != data->stepcnt)) {
  934. data->stepcnt = stepcnt;
  935. iio_push_event(indio_dev,
  936. IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD,
  937. IIO_EV_DIR_NONE,
  938. IIO_EV_TYPE_CHANGE, 0, 0, 0),
  939. data->timestamp);
  940. }
  941. if (activity != data->activity) {
  942. data->activity = activity;
  943. /* ev_activity can be NULL if activity == ACTIVITY_UNKNOWN */
  944. if (ev_prev_activity && ev_prev_activity->enabled)
  945. iio_push_event(indio_dev,
  946. IIO_EVENT_CODE(IIO_ACTIVITY, 0,
  947. ev_prev_activity->info->mod,
  948. IIO_EV_DIR_FALLING,
  949. IIO_EV_TYPE_THRESH, 0, 0,
  950. 0),
  951. data->timestamp);
  952. if (ev_activity && ev_activity->enabled)
  953. iio_push_event(indio_dev,
  954. IIO_EVENT_CODE(IIO_ACTIVITY, 0,
  955. ev_activity->info->mod,
  956. IIO_EV_DIR_RISING,
  957. IIO_EV_TYPE_THRESH, 0, 0,
  958. 0),
  959. data->timestamp);
  960. }
  961. mutex_unlock(&data->mutex);
  962. return IRQ_HANDLED;
  963. }
  964. static const char *mma9553_match_acpi_device(struct device *dev)
  965. {
  966. const struct acpi_device_id *id;
  967. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  968. if (!id)
  969. return NULL;
  970. return dev_name(dev);
  971. }
  972. static int mma9553_probe(struct i2c_client *client,
  973. const struct i2c_device_id *id)
  974. {
  975. struct mma9553_data *data;
  976. struct iio_dev *indio_dev;
  977. const char *name = NULL;
  978. int ret;
  979. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  980. if (!indio_dev)
  981. return -ENOMEM;
  982. data = iio_priv(indio_dev);
  983. i2c_set_clientdata(client, indio_dev);
  984. data->client = client;
  985. if (id)
  986. name = id->name;
  987. else if (ACPI_HANDLE(&client->dev))
  988. name = mma9553_match_acpi_device(&client->dev);
  989. else
  990. return -ENOSYS;
  991. mutex_init(&data->mutex);
  992. mma9553_init_events(data);
  993. ret = mma9553_init(data);
  994. if (ret < 0)
  995. return ret;
  996. indio_dev->dev.parent = &client->dev;
  997. indio_dev->channels = mma9553_channels;
  998. indio_dev->num_channels = ARRAY_SIZE(mma9553_channels);
  999. indio_dev->name = name;
  1000. indio_dev->modes = INDIO_DIRECT_MODE;
  1001. indio_dev->info = &mma9553_info;
  1002. if (client->irq > 0) {
  1003. ret = devm_request_threaded_irq(&client->dev, client->irq,
  1004. mma9553_irq_handler,
  1005. mma9553_event_handler,
  1006. IRQF_TRIGGER_RISING,
  1007. MMA9553_IRQ_NAME, indio_dev);
  1008. if (ret < 0) {
  1009. dev_err(&client->dev, "request irq %d failed\n",
  1010. client->irq);
  1011. goto out_poweroff;
  1012. }
  1013. }
  1014. ret = iio_device_register(indio_dev);
  1015. if (ret < 0) {
  1016. dev_err(&client->dev, "unable to register iio device\n");
  1017. goto out_poweroff;
  1018. }
  1019. ret = pm_runtime_set_active(&client->dev);
  1020. if (ret < 0)
  1021. goto out_iio_unregister;
  1022. pm_runtime_enable(&client->dev);
  1023. pm_runtime_set_autosuspend_delay(&client->dev,
  1024. MMA9551_AUTO_SUSPEND_DELAY_MS);
  1025. pm_runtime_use_autosuspend(&client->dev);
  1026. dev_dbg(&indio_dev->dev, "Registered device %s\n", name);
  1027. return 0;
  1028. out_iio_unregister:
  1029. iio_device_unregister(indio_dev);
  1030. out_poweroff:
  1031. mma9551_set_device_state(client, false);
  1032. return ret;
  1033. }
  1034. static int mma9553_remove(struct i2c_client *client)
  1035. {
  1036. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  1037. struct mma9553_data *data = iio_priv(indio_dev);
  1038. pm_runtime_disable(&client->dev);
  1039. pm_runtime_set_suspended(&client->dev);
  1040. pm_runtime_put_noidle(&client->dev);
  1041. iio_device_unregister(indio_dev);
  1042. mutex_lock(&data->mutex);
  1043. mma9551_set_device_state(data->client, false);
  1044. mutex_unlock(&data->mutex);
  1045. return 0;
  1046. }
  1047. #ifdef CONFIG_PM
  1048. static int mma9553_runtime_suspend(struct device *dev)
  1049. {
  1050. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  1051. struct mma9553_data *data = iio_priv(indio_dev);
  1052. int ret;
  1053. mutex_lock(&data->mutex);
  1054. ret = mma9551_set_device_state(data->client, false);
  1055. mutex_unlock(&data->mutex);
  1056. if (ret < 0) {
  1057. dev_err(&data->client->dev, "powering off device failed\n");
  1058. return -EAGAIN;
  1059. }
  1060. return 0;
  1061. }
  1062. static int mma9553_runtime_resume(struct device *dev)
  1063. {
  1064. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  1065. struct mma9553_data *data = iio_priv(indio_dev);
  1066. int ret;
  1067. ret = mma9551_set_device_state(data->client, true);
  1068. if (ret < 0)
  1069. return ret;
  1070. mma9551_sleep(MMA9553_DEFAULT_SAMPLE_RATE);
  1071. return 0;
  1072. }
  1073. #endif
  1074. #ifdef CONFIG_PM_SLEEP
  1075. static int mma9553_suspend(struct device *dev)
  1076. {
  1077. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  1078. struct mma9553_data *data = iio_priv(indio_dev);
  1079. int ret;
  1080. mutex_lock(&data->mutex);
  1081. ret = mma9551_set_device_state(data->client, false);
  1082. mutex_unlock(&data->mutex);
  1083. return ret;
  1084. }
  1085. static int mma9553_resume(struct device *dev)
  1086. {
  1087. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  1088. struct mma9553_data *data = iio_priv(indio_dev);
  1089. int ret;
  1090. mutex_lock(&data->mutex);
  1091. ret = mma9551_set_device_state(data->client, true);
  1092. mutex_unlock(&data->mutex);
  1093. return ret;
  1094. }
  1095. #endif
  1096. static const struct dev_pm_ops mma9553_pm_ops = {
  1097. SET_SYSTEM_SLEEP_PM_OPS(mma9553_suspend, mma9553_resume)
  1098. SET_RUNTIME_PM_OPS(mma9553_runtime_suspend,
  1099. mma9553_runtime_resume, NULL)
  1100. };
  1101. static const struct acpi_device_id mma9553_acpi_match[] = {
  1102. {"MMA9553", 0},
  1103. {},
  1104. };
  1105. MODULE_DEVICE_TABLE(acpi, mma9553_acpi_match);
  1106. static const struct i2c_device_id mma9553_id[] = {
  1107. {"mma9553", 0},
  1108. {},
  1109. };
  1110. MODULE_DEVICE_TABLE(i2c, mma9553_id);
  1111. static struct i2c_driver mma9553_driver = {
  1112. .driver = {
  1113. .name = MMA9553_DRV_NAME,
  1114. .acpi_match_table = ACPI_PTR(mma9553_acpi_match),
  1115. .pm = &mma9553_pm_ops,
  1116. },
  1117. .probe = mma9553_probe,
  1118. .remove = mma9553_remove,
  1119. .id_table = mma9553_id,
  1120. };
  1121. module_i2c_driver(mma9553_driver);
  1122. MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
  1123. MODULE_LICENSE("GPL v2");
  1124. MODULE_DESCRIPTION("MMA9553L pedometer platform driver");