bmc150-accel.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. /*
  2. * 3-axis accelerometer driver supporting following Bosch-Sensortec chips:
  3. * - BMC150
  4. * - BMI055
  5. * - BMA255
  6. * - BMA250E
  7. * - BMA222E
  8. * - BMA280
  9. *
  10. * Copyright (c) 2014, Intel Corporation.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms and conditions of the GNU General Public License,
  14. * version 2, as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/i2c.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <linux/acpi.h>
  27. #include <linux/gpio/consumer.h>
  28. #include <linux/pm.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/iio/iio.h>
  31. #include <linux/iio/sysfs.h>
  32. #include <linux/iio/buffer.h>
  33. #include <linux/iio/events.h>
  34. #include <linux/iio/trigger.h>
  35. #include <linux/iio/trigger_consumer.h>
  36. #include <linux/iio/triggered_buffer.h>
  37. #define BMC150_ACCEL_DRV_NAME "bmc150_accel"
  38. #define BMC150_ACCEL_IRQ_NAME "bmc150_accel_event"
  39. #define BMC150_ACCEL_GPIO_NAME "bmc150_accel_int"
  40. #define BMC150_ACCEL_REG_CHIP_ID 0x00
  41. #define BMC150_ACCEL_REG_INT_STATUS_2 0x0B
  42. #define BMC150_ACCEL_ANY_MOTION_MASK 0x07
  43. #define BMC150_ACCEL_ANY_MOTION_BIT_X BIT(0)
  44. #define BMC150_ACCEL_ANY_MOTION_BIT_Y BIT(1)
  45. #define BMC150_ACCEL_ANY_MOTION_BIT_Z BIT(2)
  46. #define BMC150_ACCEL_ANY_MOTION_BIT_SIGN BIT(3)
  47. #define BMC150_ACCEL_REG_PMU_LPW 0x11
  48. #define BMC150_ACCEL_PMU_MODE_MASK 0xE0
  49. #define BMC150_ACCEL_PMU_MODE_SHIFT 5
  50. #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_MASK 0x17
  51. #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT 1
  52. #define BMC150_ACCEL_REG_PMU_RANGE 0x0F
  53. #define BMC150_ACCEL_DEF_RANGE_2G 0x03
  54. #define BMC150_ACCEL_DEF_RANGE_4G 0x05
  55. #define BMC150_ACCEL_DEF_RANGE_8G 0x08
  56. #define BMC150_ACCEL_DEF_RANGE_16G 0x0C
  57. /* Default BW: 125Hz */
  58. #define BMC150_ACCEL_REG_PMU_BW 0x10
  59. #define BMC150_ACCEL_DEF_BW 125
  60. #define BMC150_ACCEL_REG_INT_MAP_0 0x19
  61. #define BMC150_ACCEL_INT_MAP_0_BIT_SLOPE BIT(2)
  62. #define BMC150_ACCEL_REG_INT_MAP_1 0x1A
  63. #define BMC150_ACCEL_INT_MAP_1_BIT_DATA BIT(0)
  64. #define BMC150_ACCEL_REG_INT_RST_LATCH 0x21
  65. #define BMC150_ACCEL_INT_MODE_LATCH_RESET 0x80
  66. #define BMC150_ACCEL_INT_MODE_LATCH_INT 0x0F
  67. #define BMC150_ACCEL_INT_MODE_NON_LATCH_INT 0x00
  68. #define BMC150_ACCEL_REG_INT_EN_0 0x16
  69. #define BMC150_ACCEL_INT_EN_BIT_SLP_X BIT(0)
  70. #define BMC150_ACCEL_INT_EN_BIT_SLP_Y BIT(1)
  71. #define BMC150_ACCEL_INT_EN_BIT_SLP_Z BIT(2)
  72. #define BMC150_ACCEL_REG_INT_EN_1 0x17
  73. #define BMC150_ACCEL_INT_EN_BIT_DATA_EN BIT(4)
  74. #define BMC150_ACCEL_REG_INT_OUT_CTRL 0x20
  75. #define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL BIT(0)
  76. #define BMC150_ACCEL_REG_INT_5 0x27
  77. #define BMC150_ACCEL_SLOPE_DUR_MASK 0x03
  78. #define BMC150_ACCEL_REG_INT_6 0x28
  79. #define BMC150_ACCEL_SLOPE_THRES_MASK 0xFF
  80. /* Slope duration in terms of number of samples */
  81. #define BMC150_ACCEL_DEF_SLOPE_DURATION 1
  82. /* in terms of multiples of g's/LSB, based on range */
  83. #define BMC150_ACCEL_DEF_SLOPE_THRESHOLD 1
  84. #define BMC150_ACCEL_REG_XOUT_L 0x02
  85. #define BMC150_ACCEL_MAX_STARTUP_TIME_MS 100
  86. /* Sleep Duration values */
  87. #define BMC150_ACCEL_SLEEP_500_MICRO 0x05
  88. #define BMC150_ACCEL_SLEEP_1_MS 0x06
  89. #define BMC150_ACCEL_SLEEP_2_MS 0x07
  90. #define BMC150_ACCEL_SLEEP_4_MS 0x08
  91. #define BMC150_ACCEL_SLEEP_6_MS 0x09
  92. #define BMC150_ACCEL_SLEEP_10_MS 0x0A
  93. #define BMC150_ACCEL_SLEEP_25_MS 0x0B
  94. #define BMC150_ACCEL_SLEEP_50_MS 0x0C
  95. #define BMC150_ACCEL_SLEEP_100_MS 0x0D
  96. #define BMC150_ACCEL_SLEEP_500_MS 0x0E
  97. #define BMC150_ACCEL_SLEEP_1_SEC 0x0F
  98. #define BMC150_ACCEL_REG_TEMP 0x08
  99. #define BMC150_ACCEL_TEMP_CENTER_VAL 24
  100. #define BMC150_ACCEL_AXIS_TO_REG(axis) (BMC150_ACCEL_REG_XOUT_L + (axis * 2))
  101. #define BMC150_AUTO_SUSPEND_DELAY_MS 2000
  102. enum bmc150_accel_axis {
  103. AXIS_X,
  104. AXIS_Y,
  105. AXIS_Z,
  106. };
  107. enum bmc150_power_modes {
  108. BMC150_ACCEL_SLEEP_MODE_NORMAL,
  109. BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND,
  110. BMC150_ACCEL_SLEEP_MODE_LPM,
  111. BMC150_ACCEL_SLEEP_MODE_SUSPEND = 0x04,
  112. };
  113. struct bmc150_scale_info {
  114. int scale;
  115. u8 reg_range;
  116. };
  117. struct bmc150_accel_chip_info {
  118. u8 chip_id;
  119. const struct iio_chan_spec *channels;
  120. int num_channels;
  121. const struct bmc150_scale_info scale_table[4];
  122. };
  123. struct bmc150_accel_data {
  124. struct i2c_client *client;
  125. struct iio_trigger *dready_trig;
  126. struct iio_trigger *motion_trig;
  127. struct mutex mutex;
  128. s16 buffer[8];
  129. u8 bw_bits;
  130. u32 slope_dur;
  131. u32 slope_thres;
  132. u32 range;
  133. int ev_enable_state;
  134. bool dready_trigger_on;
  135. bool motion_trigger_on;
  136. int64_t timestamp;
  137. const struct bmc150_accel_chip_info *chip_info;
  138. };
  139. static const struct {
  140. int val;
  141. int val2;
  142. u8 bw_bits;
  143. } bmc150_accel_samp_freq_table[] = { {15, 620000, 0x08},
  144. {31, 260000, 0x09},
  145. {62, 500000, 0x0A},
  146. {125, 0, 0x0B},
  147. {250, 0, 0x0C},
  148. {500, 0, 0x0D},
  149. {1000, 0, 0x0E},
  150. {2000, 0, 0x0F} };
  151. static const struct {
  152. int bw_bits;
  153. int msec;
  154. } bmc150_accel_sample_upd_time[] = { {0x08, 64},
  155. {0x09, 32},
  156. {0x0A, 16},
  157. {0x0B, 8},
  158. {0x0C, 4},
  159. {0x0D, 2},
  160. {0x0E, 1},
  161. {0x0F, 1} };
  162. static const struct {
  163. int sleep_dur;
  164. u8 reg_value;
  165. } bmc150_accel_sleep_value_table[] = { {0, 0},
  166. {500, BMC150_ACCEL_SLEEP_500_MICRO},
  167. {1000, BMC150_ACCEL_SLEEP_1_MS},
  168. {2000, BMC150_ACCEL_SLEEP_2_MS},
  169. {4000, BMC150_ACCEL_SLEEP_4_MS},
  170. {6000, BMC150_ACCEL_SLEEP_6_MS},
  171. {10000, BMC150_ACCEL_SLEEP_10_MS},
  172. {25000, BMC150_ACCEL_SLEEP_25_MS},
  173. {50000, BMC150_ACCEL_SLEEP_50_MS},
  174. {100000, BMC150_ACCEL_SLEEP_100_MS},
  175. {500000, BMC150_ACCEL_SLEEP_500_MS},
  176. {1000000, BMC150_ACCEL_SLEEP_1_SEC} };
  177. static int bmc150_accel_set_mode(struct bmc150_accel_data *data,
  178. enum bmc150_power_modes mode,
  179. int dur_us)
  180. {
  181. int i;
  182. int ret;
  183. u8 lpw_bits;
  184. int dur_val = -1;
  185. if (dur_us > 0) {
  186. for (i = 0; i < ARRAY_SIZE(bmc150_accel_sleep_value_table);
  187. ++i) {
  188. if (bmc150_accel_sleep_value_table[i].sleep_dur ==
  189. dur_us)
  190. dur_val =
  191. bmc150_accel_sleep_value_table[i].reg_value;
  192. }
  193. } else
  194. dur_val = 0;
  195. if (dur_val < 0)
  196. return -EINVAL;
  197. lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT;
  198. lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT);
  199. dev_dbg(&data->client->dev, "Set Mode bits %x\n", lpw_bits);
  200. ret = i2c_smbus_write_byte_data(data->client,
  201. BMC150_ACCEL_REG_PMU_LPW, lpw_bits);
  202. if (ret < 0) {
  203. dev_err(&data->client->dev, "Error writing reg_pmu_lpw\n");
  204. return ret;
  205. }
  206. return 0;
  207. }
  208. static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val,
  209. int val2)
  210. {
  211. int i;
  212. int ret;
  213. for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
  214. if (bmc150_accel_samp_freq_table[i].val == val &&
  215. bmc150_accel_samp_freq_table[i].val2 == val2) {
  216. ret = i2c_smbus_write_byte_data(
  217. data->client,
  218. BMC150_ACCEL_REG_PMU_BW,
  219. bmc150_accel_samp_freq_table[i].bw_bits);
  220. if (ret < 0)
  221. return ret;
  222. data->bw_bits =
  223. bmc150_accel_samp_freq_table[i].bw_bits;
  224. return 0;
  225. }
  226. }
  227. return -EINVAL;
  228. }
  229. static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
  230. {
  231. int ret;
  232. ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_CHIP_ID);
  233. if (ret < 0) {
  234. dev_err(&data->client->dev,
  235. "Error: Reading chip id\n");
  236. return ret;
  237. }
  238. dev_dbg(&data->client->dev, "Chip Id %x\n", ret);
  239. if (ret != data->chip_info->chip_id) {
  240. dev_err(&data->client->dev, "Invalid chip %x\n", ret);
  241. return -ENODEV;
  242. }
  243. ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
  244. if (ret < 0)
  245. return ret;
  246. /* Set Bandwidth */
  247. ret = bmc150_accel_set_bw(data, BMC150_ACCEL_DEF_BW, 0);
  248. if (ret < 0)
  249. return ret;
  250. /* Set Default Range */
  251. ret = i2c_smbus_write_byte_data(data->client,
  252. BMC150_ACCEL_REG_PMU_RANGE,
  253. BMC150_ACCEL_DEF_RANGE_4G);
  254. if (ret < 0) {
  255. dev_err(&data->client->dev,
  256. "Error writing reg_pmu_range\n");
  257. return ret;
  258. }
  259. data->range = BMC150_ACCEL_DEF_RANGE_4G;
  260. /* Set default slope duration */
  261. ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_INT_5);
  262. if (ret < 0) {
  263. dev_err(&data->client->dev, "Error reading reg_int_5\n");
  264. return ret;
  265. }
  266. data->slope_dur |= BMC150_ACCEL_DEF_SLOPE_DURATION;
  267. ret = i2c_smbus_write_byte_data(data->client,
  268. BMC150_ACCEL_REG_INT_5,
  269. data->slope_dur);
  270. if (ret < 0) {
  271. dev_err(&data->client->dev, "Error writing reg_int_5\n");
  272. return ret;
  273. }
  274. dev_dbg(&data->client->dev, "slope_dur %x\n", data->slope_dur);
  275. /* Set default slope thresholds */
  276. ret = i2c_smbus_write_byte_data(data->client,
  277. BMC150_ACCEL_REG_INT_6,
  278. BMC150_ACCEL_DEF_SLOPE_THRESHOLD);
  279. if (ret < 0) {
  280. dev_err(&data->client->dev, "Error writing reg_int_6\n");
  281. return ret;
  282. }
  283. data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD;
  284. dev_dbg(&data->client->dev, "slope_thres %x\n", data->slope_thres);
  285. /* Set default as latched interrupts */
  286. ret = i2c_smbus_write_byte_data(data->client,
  287. BMC150_ACCEL_REG_INT_RST_LATCH,
  288. BMC150_ACCEL_INT_MODE_LATCH_INT |
  289. BMC150_ACCEL_INT_MODE_LATCH_RESET);
  290. if (ret < 0) {
  291. dev_err(&data->client->dev,
  292. "Error writing reg_int_rst_latch\n");
  293. return ret;
  294. }
  295. return 0;
  296. }
  297. static int bmc150_accel_setup_any_motion_interrupt(
  298. struct bmc150_accel_data *data,
  299. bool status)
  300. {
  301. int ret;
  302. /* Enable/Disable INT1 mapping */
  303. ret = i2c_smbus_read_byte_data(data->client,
  304. BMC150_ACCEL_REG_INT_MAP_0);
  305. if (ret < 0) {
  306. dev_err(&data->client->dev, "Error reading reg_int_map_0\n");
  307. return ret;
  308. }
  309. if (status)
  310. ret |= BMC150_ACCEL_INT_MAP_0_BIT_SLOPE;
  311. else
  312. ret &= ~BMC150_ACCEL_INT_MAP_0_BIT_SLOPE;
  313. ret = i2c_smbus_write_byte_data(data->client,
  314. BMC150_ACCEL_REG_INT_MAP_0,
  315. ret);
  316. if (ret < 0) {
  317. dev_err(&data->client->dev, "Error writing reg_int_map_0\n");
  318. return ret;
  319. }
  320. if (status) {
  321. /* Set slope duration (no of samples) */
  322. ret = i2c_smbus_write_byte_data(data->client,
  323. BMC150_ACCEL_REG_INT_5,
  324. data->slope_dur);
  325. if (ret < 0) {
  326. dev_err(&data->client->dev, "Error write reg_int_5\n");
  327. return ret;
  328. }
  329. /* Set slope thresholds */
  330. ret = i2c_smbus_write_byte_data(data->client,
  331. BMC150_ACCEL_REG_INT_6,
  332. data->slope_thres);
  333. if (ret < 0) {
  334. dev_err(&data->client->dev, "Error write reg_int_6\n");
  335. return ret;
  336. }
  337. /*
  338. * New data interrupt is always non-latched,
  339. * which will have higher priority, so no need
  340. * to set latched mode, we will be flooded anyway with INTR
  341. */
  342. if (!data->dready_trigger_on) {
  343. ret = i2c_smbus_write_byte_data(data->client,
  344. BMC150_ACCEL_REG_INT_RST_LATCH,
  345. BMC150_ACCEL_INT_MODE_LATCH_INT |
  346. BMC150_ACCEL_INT_MODE_LATCH_RESET);
  347. if (ret < 0) {
  348. dev_err(&data->client->dev,
  349. "Error writing reg_int_rst_latch\n");
  350. return ret;
  351. }
  352. }
  353. ret = i2c_smbus_write_byte_data(data->client,
  354. BMC150_ACCEL_REG_INT_EN_0,
  355. BMC150_ACCEL_INT_EN_BIT_SLP_X |
  356. BMC150_ACCEL_INT_EN_BIT_SLP_Y |
  357. BMC150_ACCEL_INT_EN_BIT_SLP_Z);
  358. } else
  359. ret = i2c_smbus_write_byte_data(data->client,
  360. BMC150_ACCEL_REG_INT_EN_0,
  361. 0);
  362. if (ret < 0) {
  363. dev_err(&data->client->dev, "Error writing reg_int_en_0\n");
  364. return ret;
  365. }
  366. return 0;
  367. }
  368. static int bmc150_accel_setup_new_data_interrupt(struct bmc150_accel_data *data,
  369. bool status)
  370. {
  371. int ret;
  372. /* Enable/Disable INT1 mapping */
  373. ret = i2c_smbus_read_byte_data(data->client,
  374. BMC150_ACCEL_REG_INT_MAP_1);
  375. if (ret < 0) {
  376. dev_err(&data->client->dev, "Error reading reg_int_map_1\n");
  377. return ret;
  378. }
  379. if (status)
  380. ret |= BMC150_ACCEL_INT_MAP_1_BIT_DATA;
  381. else
  382. ret &= ~BMC150_ACCEL_INT_MAP_1_BIT_DATA;
  383. ret = i2c_smbus_write_byte_data(data->client,
  384. BMC150_ACCEL_REG_INT_MAP_1,
  385. ret);
  386. if (ret < 0) {
  387. dev_err(&data->client->dev, "Error writing reg_int_map_1\n");
  388. return ret;
  389. }
  390. if (status) {
  391. /*
  392. * Set non latched mode interrupt and clear any latched
  393. * interrupt
  394. */
  395. ret = i2c_smbus_write_byte_data(data->client,
  396. BMC150_ACCEL_REG_INT_RST_LATCH,
  397. BMC150_ACCEL_INT_MODE_NON_LATCH_INT |
  398. BMC150_ACCEL_INT_MODE_LATCH_RESET);
  399. if (ret < 0) {
  400. dev_err(&data->client->dev,
  401. "Error writing reg_int_rst_latch\n");
  402. return ret;
  403. }
  404. ret = i2c_smbus_write_byte_data(data->client,
  405. BMC150_ACCEL_REG_INT_EN_1,
  406. BMC150_ACCEL_INT_EN_BIT_DATA_EN);
  407. } else {
  408. /* Restore default interrupt mode */
  409. ret = i2c_smbus_write_byte_data(data->client,
  410. BMC150_ACCEL_REG_INT_RST_LATCH,
  411. BMC150_ACCEL_INT_MODE_LATCH_INT |
  412. BMC150_ACCEL_INT_MODE_LATCH_RESET);
  413. if (ret < 0) {
  414. dev_err(&data->client->dev,
  415. "Error writing reg_int_rst_latch\n");
  416. return ret;
  417. }
  418. ret = i2c_smbus_write_byte_data(data->client,
  419. BMC150_ACCEL_REG_INT_EN_1,
  420. 0);
  421. }
  422. if (ret < 0) {
  423. dev_err(&data->client->dev, "Error writing reg_int_en_1\n");
  424. return ret;
  425. }
  426. return 0;
  427. }
  428. static int bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val,
  429. int *val2)
  430. {
  431. int i;
  432. for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
  433. if (bmc150_accel_samp_freq_table[i].bw_bits == data->bw_bits) {
  434. *val = bmc150_accel_samp_freq_table[i].val;
  435. *val2 = bmc150_accel_samp_freq_table[i].val2;
  436. return IIO_VAL_INT_PLUS_MICRO;
  437. }
  438. }
  439. return -EINVAL;
  440. }
  441. #ifdef CONFIG_PM_RUNTIME
  442. static int bmc150_accel_get_startup_times(struct bmc150_accel_data *data)
  443. {
  444. int i;
  445. for (i = 0; i < ARRAY_SIZE(bmc150_accel_sample_upd_time); ++i) {
  446. if (bmc150_accel_sample_upd_time[i].bw_bits == data->bw_bits)
  447. return bmc150_accel_sample_upd_time[i].msec;
  448. }
  449. return BMC150_ACCEL_MAX_STARTUP_TIME_MS;
  450. }
  451. static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
  452. {
  453. int ret;
  454. if (on)
  455. ret = pm_runtime_get_sync(&data->client->dev);
  456. else {
  457. pm_runtime_mark_last_busy(&data->client->dev);
  458. ret = pm_runtime_put_autosuspend(&data->client->dev);
  459. }
  460. if (ret < 0) {
  461. dev_err(&data->client->dev,
  462. "Failed: bmc150_accel_set_power_state for %d\n", on);
  463. if (on)
  464. pm_runtime_put_noidle(&data->client->dev);
  465. return ret;
  466. }
  467. return 0;
  468. }
  469. #else
  470. static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
  471. {
  472. return 0;
  473. }
  474. #endif
  475. static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val)
  476. {
  477. int ret, i;
  478. for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) {
  479. if (data->chip_info->scale_table[i].scale == val) {
  480. ret = i2c_smbus_write_byte_data(
  481. data->client,
  482. BMC150_ACCEL_REG_PMU_RANGE,
  483. data->chip_info->scale_table[i].reg_range);
  484. if (ret < 0) {
  485. dev_err(&data->client->dev,
  486. "Error writing pmu_range\n");
  487. return ret;
  488. }
  489. data->range = data->chip_info->scale_table[i].reg_range;
  490. return 0;
  491. }
  492. }
  493. return -EINVAL;
  494. }
  495. static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val)
  496. {
  497. int ret;
  498. mutex_lock(&data->mutex);
  499. ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_TEMP);
  500. if (ret < 0) {
  501. dev_err(&data->client->dev, "Error reading reg_temp\n");
  502. mutex_unlock(&data->mutex);
  503. return ret;
  504. }
  505. *val = sign_extend32(ret, 7);
  506. mutex_unlock(&data->mutex);
  507. return IIO_VAL_INT;
  508. }
  509. static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
  510. struct iio_chan_spec const *chan,
  511. int *val)
  512. {
  513. int ret;
  514. int axis = chan->scan_index;
  515. mutex_lock(&data->mutex);
  516. ret = bmc150_accel_set_power_state(data, true);
  517. if (ret < 0) {
  518. mutex_unlock(&data->mutex);
  519. return ret;
  520. }
  521. ret = i2c_smbus_read_word_data(data->client,
  522. BMC150_ACCEL_AXIS_TO_REG(axis));
  523. if (ret < 0) {
  524. dev_err(&data->client->dev, "Error reading axis %d\n", axis);
  525. bmc150_accel_set_power_state(data, false);
  526. mutex_unlock(&data->mutex);
  527. return ret;
  528. }
  529. *val = sign_extend32(ret >> chan->scan_type.shift,
  530. chan->scan_type.realbits - 1);
  531. ret = bmc150_accel_set_power_state(data, false);
  532. mutex_unlock(&data->mutex);
  533. if (ret < 0)
  534. return ret;
  535. return IIO_VAL_INT;
  536. }
  537. static int bmc150_accel_read_raw(struct iio_dev *indio_dev,
  538. struct iio_chan_spec const *chan,
  539. int *val, int *val2, long mask)
  540. {
  541. struct bmc150_accel_data *data = iio_priv(indio_dev);
  542. int ret;
  543. switch (mask) {
  544. case IIO_CHAN_INFO_RAW:
  545. switch (chan->type) {
  546. case IIO_TEMP:
  547. return bmc150_accel_get_temp(data, val);
  548. case IIO_ACCEL:
  549. if (iio_buffer_enabled(indio_dev))
  550. return -EBUSY;
  551. else
  552. return bmc150_accel_get_axis(data, chan, val);
  553. default:
  554. return -EINVAL;
  555. }
  556. case IIO_CHAN_INFO_OFFSET:
  557. if (chan->type == IIO_TEMP) {
  558. *val = BMC150_ACCEL_TEMP_CENTER_VAL;
  559. return IIO_VAL_INT;
  560. } else
  561. return -EINVAL;
  562. case IIO_CHAN_INFO_SCALE:
  563. *val = 0;
  564. switch (chan->type) {
  565. case IIO_TEMP:
  566. *val2 = 500000;
  567. return IIO_VAL_INT_PLUS_MICRO;
  568. case IIO_ACCEL:
  569. {
  570. int i;
  571. const struct bmc150_scale_info *si;
  572. int st_size = ARRAY_SIZE(data->chip_info->scale_table);
  573. for (i = 0; i < st_size; ++i) {
  574. si = &data->chip_info->scale_table[i];
  575. if (si->reg_range == data->range) {
  576. *val2 = si->scale;
  577. return IIO_VAL_INT_PLUS_MICRO;
  578. }
  579. }
  580. return -EINVAL;
  581. }
  582. default:
  583. return -EINVAL;
  584. }
  585. case IIO_CHAN_INFO_SAMP_FREQ:
  586. mutex_lock(&data->mutex);
  587. ret = bmc150_accel_get_bw(data, val, val2);
  588. mutex_unlock(&data->mutex);
  589. return ret;
  590. default:
  591. return -EINVAL;
  592. }
  593. }
  594. static int bmc150_accel_write_raw(struct iio_dev *indio_dev,
  595. struct iio_chan_spec const *chan,
  596. int val, int val2, long mask)
  597. {
  598. struct bmc150_accel_data *data = iio_priv(indio_dev);
  599. int ret;
  600. switch (mask) {
  601. case IIO_CHAN_INFO_SAMP_FREQ:
  602. mutex_lock(&data->mutex);
  603. ret = bmc150_accel_set_bw(data, val, val2);
  604. mutex_unlock(&data->mutex);
  605. break;
  606. case IIO_CHAN_INFO_SCALE:
  607. if (val)
  608. return -EINVAL;
  609. mutex_lock(&data->mutex);
  610. ret = bmc150_accel_set_scale(data, val2);
  611. mutex_unlock(&data->mutex);
  612. return ret;
  613. default:
  614. ret = -EINVAL;
  615. }
  616. return ret;
  617. }
  618. static int bmc150_accel_read_event(struct iio_dev *indio_dev,
  619. const struct iio_chan_spec *chan,
  620. enum iio_event_type type,
  621. enum iio_event_direction dir,
  622. enum iio_event_info info,
  623. int *val, int *val2)
  624. {
  625. struct bmc150_accel_data *data = iio_priv(indio_dev);
  626. *val2 = 0;
  627. switch (info) {
  628. case IIO_EV_INFO_VALUE:
  629. *val = data->slope_thres;
  630. break;
  631. case IIO_EV_INFO_PERIOD:
  632. *val = data->slope_dur & BMC150_ACCEL_SLOPE_DUR_MASK;
  633. break;
  634. default:
  635. return -EINVAL;
  636. }
  637. return IIO_VAL_INT;
  638. }
  639. static int bmc150_accel_write_event(struct iio_dev *indio_dev,
  640. const struct iio_chan_spec *chan,
  641. enum iio_event_type type,
  642. enum iio_event_direction dir,
  643. enum iio_event_info info,
  644. int val, int val2)
  645. {
  646. struct bmc150_accel_data *data = iio_priv(indio_dev);
  647. if (data->ev_enable_state)
  648. return -EBUSY;
  649. switch (info) {
  650. case IIO_EV_INFO_VALUE:
  651. data->slope_thres = val;
  652. break;
  653. case IIO_EV_INFO_PERIOD:
  654. data->slope_dur &= ~BMC150_ACCEL_SLOPE_DUR_MASK;
  655. data->slope_dur |= val & BMC150_ACCEL_SLOPE_DUR_MASK;
  656. break;
  657. default:
  658. return -EINVAL;
  659. }
  660. return 0;
  661. }
  662. static int bmc150_accel_read_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)
  666. {
  667. struct bmc150_accel_data *data = iio_priv(indio_dev);
  668. return data->ev_enable_state;
  669. }
  670. static int bmc150_accel_write_event_config(struct iio_dev *indio_dev,
  671. const struct iio_chan_spec *chan,
  672. enum iio_event_type type,
  673. enum iio_event_direction dir,
  674. int state)
  675. {
  676. struct bmc150_accel_data *data = iio_priv(indio_dev);
  677. int ret;
  678. if (state && data->ev_enable_state)
  679. return 0;
  680. mutex_lock(&data->mutex);
  681. if (!state && data->motion_trigger_on) {
  682. data->ev_enable_state = 0;
  683. mutex_unlock(&data->mutex);
  684. return 0;
  685. }
  686. /*
  687. * We will expect the enable and disable to do operation in
  688. * in reverse order. This will happen here anyway as our
  689. * resume operation uses sync mode runtime pm calls, the
  690. * suspend operation will be delayed by autosuspend delay
  691. * So the disable operation will still happen in reverse of
  692. * enable operation. When runtime pm is disabled the mode
  693. * is always on so sequence doesn't matter
  694. */
  695. ret = bmc150_accel_set_power_state(data, state);
  696. if (ret < 0) {
  697. mutex_unlock(&data->mutex);
  698. return ret;
  699. }
  700. ret = bmc150_accel_setup_any_motion_interrupt(data, state);
  701. if (ret < 0) {
  702. bmc150_accel_set_power_state(data, false);
  703. mutex_unlock(&data->mutex);
  704. return ret;
  705. }
  706. data->ev_enable_state = state;
  707. mutex_unlock(&data->mutex);
  708. return 0;
  709. }
  710. static int bmc150_accel_validate_trigger(struct iio_dev *indio_dev,
  711. struct iio_trigger *trig)
  712. {
  713. struct bmc150_accel_data *data = iio_priv(indio_dev);
  714. if (data->dready_trig != trig && data->motion_trig != trig)
  715. return -EINVAL;
  716. return 0;
  717. }
  718. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
  719. "15.620000 31.260000 62.50000 125 250 500 1000 2000");
  720. static struct attribute *bmc150_accel_attributes[] = {
  721. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  722. NULL,
  723. };
  724. static const struct attribute_group bmc150_accel_attrs_group = {
  725. .attrs = bmc150_accel_attributes,
  726. };
  727. static const struct iio_event_spec bmc150_accel_event = {
  728. .type = IIO_EV_TYPE_ROC,
  729. .dir = IIO_EV_DIR_EITHER,
  730. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  731. BIT(IIO_EV_INFO_ENABLE) |
  732. BIT(IIO_EV_INFO_PERIOD)
  733. };
  734. #define BMC150_ACCEL_CHANNEL(_axis, bits) { \
  735. .type = IIO_ACCEL, \
  736. .modified = 1, \
  737. .channel2 = IIO_MOD_##_axis, \
  738. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  739. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  740. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  741. .scan_index = AXIS_##_axis, \
  742. .scan_type = { \
  743. .sign = 's', \
  744. .realbits = (bits), \
  745. .storagebits = 16, \
  746. .shift = 16 - (bits), \
  747. }, \
  748. .event_spec = &bmc150_accel_event, \
  749. .num_event_specs = 1 \
  750. }
  751. #define BMC150_ACCEL_CHANNELS(bits) { \
  752. { \
  753. .type = IIO_TEMP, \
  754. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  755. BIT(IIO_CHAN_INFO_SCALE) | \
  756. BIT(IIO_CHAN_INFO_OFFSET), \
  757. .scan_index = -1, \
  758. }, \
  759. BMC150_ACCEL_CHANNEL(X, bits), \
  760. BMC150_ACCEL_CHANNEL(Y, bits), \
  761. BMC150_ACCEL_CHANNEL(Z, bits), \
  762. IIO_CHAN_SOFT_TIMESTAMP(3), \
  763. }
  764. static const struct iio_chan_spec bma222e_accel_channels[] =
  765. BMC150_ACCEL_CHANNELS(8);
  766. static const struct iio_chan_spec bma250e_accel_channels[] =
  767. BMC150_ACCEL_CHANNELS(10);
  768. static const struct iio_chan_spec bmc150_accel_channels[] =
  769. BMC150_ACCEL_CHANNELS(12);
  770. static const struct iio_chan_spec bma280_accel_channels[] =
  771. BMC150_ACCEL_CHANNELS(14);
  772. enum {
  773. bmc150,
  774. bmi055,
  775. bma255,
  776. bma250e,
  777. bma222e,
  778. bma280,
  779. };
  780. static const struct bmc150_accel_chip_info bmc150_accel_chip_info_tbl[] = {
  781. [bmc150] = {
  782. .chip_id = 0xFA,
  783. .channels = bmc150_accel_channels,
  784. .num_channels = ARRAY_SIZE(bmc150_accel_channels),
  785. .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
  786. {19122, BMC150_ACCEL_DEF_RANGE_4G},
  787. {38344, BMC150_ACCEL_DEF_RANGE_8G},
  788. {76590, BMC150_ACCEL_DEF_RANGE_16G} },
  789. },
  790. [bmi055] = {
  791. .chip_id = 0xFA,
  792. .channels = bmc150_accel_channels,
  793. .num_channels = ARRAY_SIZE(bmc150_accel_channels),
  794. .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
  795. {19122, BMC150_ACCEL_DEF_RANGE_4G},
  796. {38344, BMC150_ACCEL_DEF_RANGE_8G},
  797. {76590, BMC150_ACCEL_DEF_RANGE_16G} },
  798. },
  799. [bma255] = {
  800. .chip_id = 0xFA,
  801. .channels = bmc150_accel_channels,
  802. .num_channels = ARRAY_SIZE(bmc150_accel_channels),
  803. .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
  804. {19122, BMC150_ACCEL_DEF_RANGE_4G},
  805. {38344, BMC150_ACCEL_DEF_RANGE_8G},
  806. {76590, BMC150_ACCEL_DEF_RANGE_16G} },
  807. },
  808. [bma250e] = {
  809. .chip_id = 0xF9,
  810. .channels = bma250e_accel_channels,
  811. .num_channels = ARRAY_SIZE(bma250e_accel_channels),
  812. .scale_table = { {38344, BMC150_ACCEL_DEF_RANGE_2G},
  813. {76590, BMC150_ACCEL_DEF_RANGE_4G},
  814. {153277, BMC150_ACCEL_DEF_RANGE_8G},
  815. {306457, BMC150_ACCEL_DEF_RANGE_16G} },
  816. },
  817. [bma222e] = {
  818. .chip_id = 0xF8,
  819. .channels = bma222e_accel_channels,
  820. .num_channels = ARRAY_SIZE(bma222e_accel_channels),
  821. .scale_table = { {153277, BMC150_ACCEL_DEF_RANGE_2G},
  822. {306457, BMC150_ACCEL_DEF_RANGE_4G},
  823. {612915, BMC150_ACCEL_DEF_RANGE_8G},
  824. {1225831, BMC150_ACCEL_DEF_RANGE_16G} },
  825. },
  826. [bma280] = {
  827. .chip_id = 0xFB,
  828. .channels = bma280_accel_channels,
  829. .num_channels = ARRAY_SIZE(bma280_accel_channels),
  830. .scale_table = { {2392, BMC150_ACCEL_DEF_RANGE_2G},
  831. {4785, BMC150_ACCEL_DEF_RANGE_4G},
  832. {9581, BMC150_ACCEL_DEF_RANGE_8G},
  833. {19152, BMC150_ACCEL_DEF_RANGE_16G} },
  834. },
  835. };
  836. static const struct iio_info bmc150_accel_info = {
  837. .attrs = &bmc150_accel_attrs_group,
  838. .read_raw = bmc150_accel_read_raw,
  839. .write_raw = bmc150_accel_write_raw,
  840. .read_event_value = bmc150_accel_read_event,
  841. .write_event_value = bmc150_accel_write_event,
  842. .write_event_config = bmc150_accel_write_event_config,
  843. .read_event_config = bmc150_accel_read_event_config,
  844. .validate_trigger = bmc150_accel_validate_trigger,
  845. .driver_module = THIS_MODULE,
  846. };
  847. static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
  848. {
  849. struct iio_poll_func *pf = p;
  850. struct iio_dev *indio_dev = pf->indio_dev;
  851. struct bmc150_accel_data *data = iio_priv(indio_dev);
  852. int bit, ret, i = 0;
  853. mutex_lock(&data->mutex);
  854. for_each_set_bit(bit, indio_dev->buffer->scan_mask,
  855. indio_dev->masklength) {
  856. ret = i2c_smbus_read_word_data(data->client,
  857. BMC150_ACCEL_AXIS_TO_REG(bit));
  858. if (ret < 0) {
  859. mutex_unlock(&data->mutex);
  860. goto err_read;
  861. }
  862. data->buffer[i++] = ret;
  863. }
  864. mutex_unlock(&data->mutex);
  865. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  866. data->timestamp);
  867. err_read:
  868. iio_trigger_notify_done(indio_dev->trig);
  869. return IRQ_HANDLED;
  870. }
  871. static int bmc150_accel_trig_try_reen(struct iio_trigger *trig)
  872. {
  873. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  874. struct bmc150_accel_data *data = iio_priv(indio_dev);
  875. int ret;
  876. /* new data interrupts don't need ack */
  877. if (data->dready_trigger_on)
  878. return 0;
  879. mutex_lock(&data->mutex);
  880. /* clear any latched interrupt */
  881. ret = i2c_smbus_write_byte_data(data->client,
  882. BMC150_ACCEL_REG_INT_RST_LATCH,
  883. BMC150_ACCEL_INT_MODE_LATCH_INT |
  884. BMC150_ACCEL_INT_MODE_LATCH_RESET);
  885. mutex_unlock(&data->mutex);
  886. if (ret < 0) {
  887. dev_err(&data->client->dev,
  888. "Error writing reg_int_rst_latch\n");
  889. return ret;
  890. }
  891. return 0;
  892. }
  893. static int bmc150_accel_data_rdy_trigger_set_state(struct iio_trigger *trig,
  894. bool state)
  895. {
  896. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  897. struct bmc150_accel_data *data = iio_priv(indio_dev);
  898. int ret;
  899. mutex_lock(&data->mutex);
  900. if (!state && data->ev_enable_state && data->motion_trigger_on) {
  901. data->motion_trigger_on = false;
  902. mutex_unlock(&data->mutex);
  903. return 0;
  904. }
  905. /*
  906. * Refer to comment in bmc150_accel_write_event_config for
  907. * enable/disable operation order
  908. */
  909. ret = bmc150_accel_set_power_state(data, state);
  910. if (ret < 0) {
  911. mutex_unlock(&data->mutex);
  912. return ret;
  913. }
  914. if (data->motion_trig == trig)
  915. ret = bmc150_accel_setup_any_motion_interrupt(data, state);
  916. else
  917. ret = bmc150_accel_setup_new_data_interrupt(data, state);
  918. if (ret < 0) {
  919. bmc150_accel_set_power_state(data, false);
  920. mutex_unlock(&data->mutex);
  921. return ret;
  922. }
  923. if (data->motion_trig == trig)
  924. data->motion_trigger_on = state;
  925. else
  926. data->dready_trigger_on = state;
  927. mutex_unlock(&data->mutex);
  928. return ret;
  929. }
  930. static const struct iio_trigger_ops bmc150_accel_trigger_ops = {
  931. .set_trigger_state = bmc150_accel_data_rdy_trigger_set_state,
  932. .try_reenable = bmc150_accel_trig_try_reen,
  933. .owner = THIS_MODULE,
  934. };
  935. static irqreturn_t bmc150_accel_event_handler(int irq, void *private)
  936. {
  937. struct iio_dev *indio_dev = private;
  938. struct bmc150_accel_data *data = iio_priv(indio_dev);
  939. int ret;
  940. int dir;
  941. ret = i2c_smbus_read_byte_data(data->client,
  942. BMC150_ACCEL_REG_INT_STATUS_2);
  943. if (ret < 0) {
  944. dev_err(&data->client->dev, "Error reading reg_int_status_2\n");
  945. goto ack_intr_status;
  946. }
  947. if (ret & BMC150_ACCEL_ANY_MOTION_BIT_SIGN)
  948. dir = IIO_EV_DIR_FALLING;
  949. else
  950. dir = IIO_EV_DIR_RISING;
  951. if (ret & BMC150_ACCEL_ANY_MOTION_BIT_X)
  952. iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ACCEL,
  953. 0,
  954. IIO_MOD_X,
  955. IIO_EV_TYPE_ROC,
  956. dir),
  957. data->timestamp);
  958. if (ret & BMC150_ACCEL_ANY_MOTION_BIT_Y)
  959. iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ACCEL,
  960. 0,
  961. IIO_MOD_Y,
  962. IIO_EV_TYPE_ROC,
  963. dir),
  964. data->timestamp);
  965. if (ret & BMC150_ACCEL_ANY_MOTION_BIT_Z)
  966. iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ACCEL,
  967. 0,
  968. IIO_MOD_Z,
  969. IIO_EV_TYPE_ROC,
  970. dir),
  971. data->timestamp);
  972. ack_intr_status:
  973. if (!data->dready_trigger_on)
  974. ret = i2c_smbus_write_byte_data(data->client,
  975. BMC150_ACCEL_REG_INT_RST_LATCH,
  976. BMC150_ACCEL_INT_MODE_LATCH_INT |
  977. BMC150_ACCEL_INT_MODE_LATCH_RESET);
  978. return IRQ_HANDLED;
  979. }
  980. static irqreturn_t bmc150_accel_data_rdy_trig_poll(int irq, void *private)
  981. {
  982. struct iio_dev *indio_dev = private;
  983. struct bmc150_accel_data *data = iio_priv(indio_dev);
  984. data->timestamp = iio_get_time_ns();
  985. if (data->dready_trigger_on)
  986. iio_trigger_poll(data->dready_trig);
  987. else if (data->motion_trigger_on)
  988. iio_trigger_poll(data->motion_trig);
  989. if (data->ev_enable_state)
  990. return IRQ_WAKE_THREAD;
  991. else
  992. return IRQ_HANDLED;
  993. }
  994. static const char *bmc150_accel_match_acpi_device(struct device *dev, int *data)
  995. {
  996. const struct acpi_device_id *id;
  997. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  998. if (!id)
  999. return NULL;
  1000. *data = (int) id->driver_data;
  1001. return dev_name(dev);
  1002. }
  1003. static int bmc150_accel_gpio_probe(struct i2c_client *client,
  1004. struct bmc150_accel_data *data)
  1005. {
  1006. struct device *dev;
  1007. struct gpio_desc *gpio;
  1008. int ret;
  1009. if (!client)
  1010. return -EINVAL;
  1011. dev = &client->dev;
  1012. /* data ready gpio interrupt pin */
  1013. gpio = devm_gpiod_get_index(dev, BMC150_ACCEL_GPIO_NAME, 0);
  1014. if (IS_ERR(gpio)) {
  1015. dev_err(dev, "Failed: gpio get index\n");
  1016. return PTR_ERR(gpio);
  1017. }
  1018. ret = gpiod_direction_input(gpio);
  1019. if (ret)
  1020. return ret;
  1021. ret = gpiod_to_irq(gpio);
  1022. dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
  1023. return ret;
  1024. }
  1025. static int bmc150_accel_probe(struct i2c_client *client,
  1026. const struct i2c_device_id *id)
  1027. {
  1028. struct bmc150_accel_data *data;
  1029. struct iio_dev *indio_dev;
  1030. int ret;
  1031. const char *name = NULL;
  1032. int chip_id = 0;
  1033. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  1034. if (!indio_dev)
  1035. return -ENOMEM;
  1036. data = iio_priv(indio_dev);
  1037. i2c_set_clientdata(client, indio_dev);
  1038. data->client = client;
  1039. if (id) {
  1040. name = id->name;
  1041. chip_id = id->driver_data;
  1042. }
  1043. if (ACPI_HANDLE(&client->dev))
  1044. name = bmc150_accel_match_acpi_device(&client->dev, &chip_id);
  1045. data->chip_info = &bmc150_accel_chip_info_tbl[chip_id];
  1046. ret = bmc150_accel_chip_init(data);
  1047. if (ret < 0)
  1048. return ret;
  1049. mutex_init(&data->mutex);
  1050. indio_dev->dev.parent = &client->dev;
  1051. indio_dev->channels = data->chip_info->channels;
  1052. indio_dev->num_channels = data->chip_info->num_channels;
  1053. indio_dev->name = name;
  1054. indio_dev->modes = INDIO_DIRECT_MODE;
  1055. indio_dev->info = &bmc150_accel_info;
  1056. if (client->irq < 0)
  1057. client->irq = bmc150_accel_gpio_probe(client, data);
  1058. if (client->irq >= 0) {
  1059. ret = devm_request_threaded_irq(
  1060. &client->dev, client->irq,
  1061. bmc150_accel_data_rdy_trig_poll,
  1062. bmc150_accel_event_handler,
  1063. IRQF_TRIGGER_RISING,
  1064. BMC150_ACCEL_IRQ_NAME,
  1065. indio_dev);
  1066. if (ret)
  1067. return ret;
  1068. data->dready_trig = devm_iio_trigger_alloc(&client->dev,
  1069. "%s-dev%d",
  1070. indio_dev->name,
  1071. indio_dev->id);
  1072. if (!data->dready_trig)
  1073. return -ENOMEM;
  1074. data->motion_trig = devm_iio_trigger_alloc(&client->dev,
  1075. "%s-any-motion-dev%d",
  1076. indio_dev->name,
  1077. indio_dev->id);
  1078. if (!data->motion_trig)
  1079. return -ENOMEM;
  1080. data->dready_trig->dev.parent = &client->dev;
  1081. data->dready_trig->ops = &bmc150_accel_trigger_ops;
  1082. iio_trigger_set_drvdata(data->dready_trig, indio_dev);
  1083. ret = iio_trigger_register(data->dready_trig);
  1084. if (ret)
  1085. return ret;
  1086. data->motion_trig->dev.parent = &client->dev;
  1087. data->motion_trig->ops = &bmc150_accel_trigger_ops;
  1088. iio_trigger_set_drvdata(data->motion_trig, indio_dev);
  1089. ret = iio_trigger_register(data->motion_trig);
  1090. if (ret) {
  1091. data->motion_trig = NULL;
  1092. goto err_trigger_unregister;
  1093. }
  1094. ret = iio_triggered_buffer_setup(indio_dev,
  1095. &iio_pollfunc_store_time,
  1096. bmc150_accel_trigger_handler,
  1097. NULL);
  1098. if (ret < 0) {
  1099. dev_err(&client->dev,
  1100. "Failed: iio triggered buffer setup\n");
  1101. goto err_trigger_unregister;
  1102. }
  1103. }
  1104. ret = iio_device_register(indio_dev);
  1105. if (ret < 0) {
  1106. dev_err(&client->dev, "Unable to register iio device\n");
  1107. goto err_buffer_cleanup;
  1108. }
  1109. ret = pm_runtime_set_active(&client->dev);
  1110. if (ret)
  1111. goto err_iio_unregister;
  1112. pm_runtime_enable(&client->dev);
  1113. pm_runtime_set_autosuspend_delay(&client->dev,
  1114. BMC150_AUTO_SUSPEND_DELAY_MS);
  1115. pm_runtime_use_autosuspend(&client->dev);
  1116. return 0;
  1117. err_iio_unregister:
  1118. iio_device_unregister(indio_dev);
  1119. err_buffer_cleanup:
  1120. if (data->dready_trig)
  1121. iio_triggered_buffer_cleanup(indio_dev);
  1122. err_trigger_unregister:
  1123. if (data->dready_trig)
  1124. iio_trigger_unregister(data->dready_trig);
  1125. if (data->motion_trig)
  1126. iio_trigger_unregister(data->motion_trig);
  1127. return ret;
  1128. }
  1129. static int bmc150_accel_remove(struct i2c_client *client)
  1130. {
  1131. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  1132. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1133. pm_runtime_disable(&client->dev);
  1134. pm_runtime_set_suspended(&client->dev);
  1135. pm_runtime_put_noidle(&client->dev);
  1136. iio_device_unregister(indio_dev);
  1137. if (data->dready_trig) {
  1138. iio_triggered_buffer_cleanup(indio_dev);
  1139. iio_trigger_unregister(data->dready_trig);
  1140. iio_trigger_unregister(data->motion_trig);
  1141. }
  1142. mutex_lock(&data->mutex);
  1143. bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0);
  1144. mutex_unlock(&data->mutex);
  1145. return 0;
  1146. }
  1147. #ifdef CONFIG_PM_SLEEP
  1148. static int bmc150_accel_suspend(struct device *dev)
  1149. {
  1150. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  1151. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1152. mutex_lock(&data->mutex);
  1153. bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
  1154. mutex_unlock(&data->mutex);
  1155. return 0;
  1156. }
  1157. static int bmc150_accel_resume(struct device *dev)
  1158. {
  1159. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  1160. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1161. mutex_lock(&data->mutex);
  1162. if (data->dready_trigger_on || data->motion_trigger_on ||
  1163. data->ev_enable_state)
  1164. bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
  1165. mutex_unlock(&data->mutex);
  1166. return 0;
  1167. }
  1168. #endif
  1169. #ifdef CONFIG_PM_RUNTIME
  1170. static int bmc150_accel_runtime_suspend(struct device *dev)
  1171. {
  1172. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  1173. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1174. int ret;
  1175. dev_dbg(&data->client->dev, __func__);
  1176. ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
  1177. if (ret < 0)
  1178. return -EAGAIN;
  1179. return 0;
  1180. }
  1181. static int bmc150_accel_runtime_resume(struct device *dev)
  1182. {
  1183. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  1184. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1185. int ret;
  1186. int sleep_val;
  1187. dev_dbg(&data->client->dev, __func__);
  1188. ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
  1189. if (ret < 0)
  1190. return ret;
  1191. sleep_val = bmc150_accel_get_startup_times(data);
  1192. if (sleep_val < 20)
  1193. usleep_range(sleep_val * 1000, 20000);
  1194. else
  1195. msleep_interruptible(sleep_val);
  1196. return 0;
  1197. }
  1198. #endif
  1199. static const struct dev_pm_ops bmc150_accel_pm_ops = {
  1200. SET_SYSTEM_SLEEP_PM_OPS(bmc150_accel_suspend, bmc150_accel_resume)
  1201. SET_RUNTIME_PM_OPS(bmc150_accel_runtime_suspend,
  1202. bmc150_accel_runtime_resume, NULL)
  1203. };
  1204. static const struct acpi_device_id bmc150_accel_acpi_match[] = {
  1205. {"BSBA0150", bmc150},
  1206. {"BMC150A", bmc150},
  1207. {"BMI055A", bmi055},
  1208. {"BMA0255", bma255},
  1209. {"BMA250E", bma250e},
  1210. {"BMA222E", bma222e},
  1211. {"BMA0280", bma280},
  1212. { },
  1213. };
  1214. MODULE_DEVICE_TABLE(acpi, bmc150_accel_acpi_match);
  1215. static const struct i2c_device_id bmc150_accel_id[] = {
  1216. {"bmc150_accel", bmc150},
  1217. {"bmi055_accel", bmi055},
  1218. {"bma255", bma255},
  1219. {"bma250e", bma250e},
  1220. {"bma222e", bma222e},
  1221. {"bma280", bma280},
  1222. {}
  1223. };
  1224. MODULE_DEVICE_TABLE(i2c, bmc150_accel_id);
  1225. static struct i2c_driver bmc150_accel_driver = {
  1226. .driver = {
  1227. .name = BMC150_ACCEL_DRV_NAME,
  1228. .acpi_match_table = ACPI_PTR(bmc150_accel_acpi_match),
  1229. .pm = &bmc150_accel_pm_ops,
  1230. },
  1231. .probe = bmc150_accel_probe,
  1232. .remove = bmc150_accel_remove,
  1233. .id_table = bmc150_accel_id,
  1234. };
  1235. module_i2c_driver(bmc150_accel_driver);
  1236. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  1237. MODULE_LICENSE("GPL v2");
  1238. MODULE_DESCRIPTION("BMC150 accelerometer driver");