mma9553.c 32 KB

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